1/* $NetBSD: sysmon_envsys_events.c,v 1.98.8.2 2012/10/17 21:21:43 riz Exp $ */ 2 3/*- 4 * Copyright (c) 2007, 2008 Juan Romero Pardines. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28/* 29 * sysmon_envsys(9) events framework. 30 */ 31 32#include <sys/cdefs.h> 33__KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_events.c,v 1.98.8.2 2012/10/17 21:21:43 riz Exp $"); 34 35#include <sys/param.h> 36#include <sys/types.h> 37#include <sys/conf.h> 38#include <sys/errno.h> 39#include <sys/kernel.h> 40#include <sys/systm.h> 41#include <sys/proc.h> 42#include <sys/mutex.h> 43#include <sys/kmem.h> 44#include <sys/callout.h> 45 46/* #define ENVSYS_DEBUG */ 47/* #define ENVSYS_OBJECTS_DEBUG */ 48 49#include <dev/sysmon/sysmonvar.h> 50#include <dev/sysmon/sysmon_envsysvar.h> 51 52struct sme_sensor_event { 53 int state; 54 int event; 55}; 56 57static const struct sme_sensor_event sme_sensor_event[] = { 58 { ENVSYS_SVALID, PENVSYS_EVENT_NORMAL }, 59 { ENVSYS_SCRITOVER, PENVSYS_EVENT_CRITOVER }, 60 { ENVSYS_SCRITUNDER, PENVSYS_EVENT_CRITUNDER }, 61 { ENVSYS_SWARNOVER, PENVSYS_EVENT_WARNOVER }, 62 { ENVSYS_SWARNUNDER, PENVSYS_EVENT_WARNUNDER }, 63 { ENVSYS_BATTERY_CAPACITY_NORMAL, PENVSYS_EVENT_NORMAL }, 64 { ENVSYS_BATTERY_CAPACITY_WARNING, PENVSYS_EVENT_BATT_WARN }, 65 { ENVSYS_BATTERY_CAPACITY_CRITICAL, PENVSYS_EVENT_BATT_CRIT }, 66 { ENVSYS_BATTERY_CAPACITY_HIGH, PENVSYS_EVENT_BATT_HIGH }, 67 { ENVSYS_BATTERY_CAPACITY_MAX, PENVSYS_EVENT_BATT_MAX }, 68 { -1, -1 } 69}; 70 71static bool sysmon_low_power; 72 73#define SME_EVTIMO (SME_EVENTS_DEFTIMEOUT * hz) 74 75static bool sme_event_check_low_power(void); 76static bool sme_battery_check(void); 77static bool sme_battery_critical(envsys_data_t *); 78static bool sme_acadapter_check(void); 79 80static void sme_remove_event(sme_event_t *, struct sysmon_envsys *); 81 82/* 83 * sme_event_register: 84 * 85 * + Registers a new sysmon envsys event or updates any event 86 * already in the queue. 87 */ 88int 89sme_event_register(prop_dictionary_t sdict, envsys_data_t *edata, 90 struct sysmon_envsys *sme, sysmon_envsys_lim_t *lims, 91 uint32_t props, int crittype, int powertype) 92{ 93 sme_event_t *see = NULL, *osee = NULL; 94 prop_object_t obj; 95 int error = 0; 96 const char *objkey; 97 98 KASSERT(sdict != NULL); 99 KASSERT(edata != NULL); 100 KASSERT(sme != NULL); 101 KASSERT(lims != NULL); 102 103 /* 104 * Some validation first for limit-checking events 105 * 106 * 1. Limits are not permitted if the units is ENVSYS_INDICATOR 107 * or ENVSYS_BATTERY_CHARGE. 108 * 109 * 2. Capacity limits are permitted only if the sensor has the 110 * ENVSYS_FPERCENT flag set and value_max is set. 111 * 112 * 3. It is not permissible for both capacity and value limits 113 * to coexist. 114 * 115 * Note that it permissible for a sensor to have value limits 116 * even if its ENVSYS_FPERCENT flag and value_max are set. 117 */ 118 119 DPRINTF(("%s: units %d props 0x%04x upropset 0x%04x max_val %d" 120 " edata-flags 0x%04x\n", __func__, edata->units, props, 121 edata->upropset, edata->value_max, edata->flags)); 122 123 if (props) 124 if (edata->units == ENVSYS_INDICATOR || 125 edata->units == ENVSYS_BATTERY_CHARGE) 126 return ENOTSUP; 127 128 if ((props & PROP_CAP_LIMITS) && 129 ((edata->value_max == 0) || 130 !(edata->flags & ENVSYS_FPERCENT) || 131 (props & PROP_VAL_LIMITS) || 132 (edata->upropset & PROP_VAL_LIMITS))) 133 props = 0; 134 135 if ((props & PROP_VAL_LIMITS) && (edata->upropset & PROP_CAP_LIMITS)) 136 props = 0; 137 138 /* 139 * check if the event is already on the list and return 140 * EEXIST if value provided hasn't been changed. 141 */ 142 mutex_enter(&sme->sme_mtx); 143 LIST_FOREACH(osee, &sme->sme_events_list, see_list) { 144 if (strcmp(edata->desc, osee->see_pes.pes_sensname) != 0) 145 continue; 146 if (crittype != osee->see_type) 147 continue; 148 149 /* 150 * We found an existing event for this sensor. Make 151 * sure it references the correct edata 152 */ 153 KASSERT(edata == osee->see_edata); 154 155 DPRINTF(("%s: dev %s sensor %s: event type %d exists\n", 156 __func__, sme->sme_name, edata->desc, crittype)); 157 158 see = osee; 159 if (props & edata->upropset & (PROP_CRITMAX | PROP_BATTMAX)) { 160 if (lims->sel_critmax == edata->limits.sel_critmax) { 161 DPRINTF(("%s: critmax exists\n", __func__)); 162 error = EEXIST; 163 props &= ~(PROP_CRITMAX | PROP_BATTMAX); 164 } 165 } 166 if (props & edata->upropset & (PROP_WARNMAX | PROP_BATTHIGH)) { 167 if (lims->sel_warnmax == edata->limits.sel_warnmax) { 168 DPRINTF(("%s: warnmax exists\n", __func__)); 169 error = EEXIST; 170 props &= ~(PROP_WARNMAX | PROP_BATTHIGH); 171 } 172 } 173 if (props & edata->upropset & (PROP_WARNMIN | PROP_BATTWARN)) { 174 if (lims->sel_warnmin == edata->limits.sel_warnmin) { 175 DPRINTF(("%s: warnmin exists\n", __func__)); 176 error = EEXIST; 177 props &= ~(PROP_WARNMIN | PROP_BATTWARN); 178 } 179 } 180 if (props & edata->upropset & (PROP_CRITMIN | PROP_BATTCAP)) { 181 if (lims->sel_critmin == edata->limits.sel_critmin) { 182 DPRINTF(("%s: critmin exists\n", __func__)); 183 error = EEXIST; 184 props &= ~(PROP_CRITMIN | PROP_BATTCAP); 185 } 186 } 187 break; 188 } 189 if (see == NULL) { 190 /* 191 * New event requested - allocate a sysmon_envsys event. 192 */ 193 see = kmem_zalloc(sizeof(*see), KM_SLEEP); 194 if (see == NULL) 195 return ENOMEM; 196 197 DPRINTF(("%s: dev %s sensor %s: new event\n", 198 __func__, sme->sme_name, edata->desc)); 199 200 see->see_type = crittype; 201 see->see_sme = sme; 202 see->see_edata = edata; 203 204 /* Initialize sensor type and previously-sent state */ 205 206 see->see_pes.pes_type = powertype; 207 208 switch (crittype) { 209 case PENVSYS_EVENT_CAPACITY: 210 see->see_evstate = ENVSYS_BATTERY_CAPACITY_NORMAL; 211 break; 212 case PENVSYS_EVENT_STATE_CHANGED: 213 if (edata->units == ENVSYS_BATTERY_CAPACITY) 214 see->see_evstate = 215 ENVSYS_BATTERY_CAPACITY_NORMAL; 216 else if (edata->units == ENVSYS_DRIVE) 217 see->see_evstate = ENVSYS_DRIVE_EMPTY; 218 else if (edata->units == ENVSYS_INDICATOR) 219 see->see_evstate = ENVSYS_SVALID; 220 else 221 panic("%s: bad units for " 222 "PENVSYS_EVENT_STATE_CHANGED", __func__); 223 break; 224 case PENVSYS_EVENT_CRITICAL: 225 case PENVSYS_EVENT_LIMITS: 226 default: 227 see->see_evstate = ENVSYS_SVALID; 228 break; 229 } 230 see->see_evvalue = 0; 231 232 (void)strlcpy(see->see_pes.pes_dvname, sme->sme_name, 233 sizeof(see->see_pes.pes_dvname)); 234 (void)strlcpy(see->see_pes.pes_sensname, edata->desc, 235 sizeof(see->see_pes.pes_sensname)); 236 } 237 238 /* 239 * Limit operation requested. 240 */ 241#define LIMIT_OP(k, l, p) \ 242 if (props & p) { \ 243 objkey = k; \ 244 obj = prop_dictionary_get(sdict, objkey); \ 245 if (obj != NULL && \ 246 prop_object_type(obj) != PROP_TYPE_NUMBER) { \ 247 DPRINTF(("%s: (%s) %s object no TYPE_NUMBER\n", \ 248 __func__, sme->sme_name, objkey)); \ 249 error = ENOTSUP; \ 250 } else { \ 251 edata->limits.l = lims->l; \ 252 error = sme_sensor_upint32(sdict, objkey,lims->l); \ 253 DPRINTF(("%s: (%s) event [sensor=%s type=%d] " \ 254 "(%s updated)\n", __func__, sme->sme_name, \ 255 edata->desc, crittype, objkey)); \ 256 } \ 257 if (error && error != EEXIST) \ 258 goto out; \ 259 edata->upropset |= p; \ 260 } 261 262 /* Value-based limits */ 263 LIMIT_OP("critical-max", sel_critmax, PROP_CRITMAX); 264 LIMIT_OP("warning-max", sel_warnmax, PROP_WARNMAX); 265 LIMIT_OP("warning-min", sel_warnmin, PROP_WARNMIN); 266 LIMIT_OP("critical-min", sel_critmin, PROP_CRITMIN); 267 268 /* %Capacity-based limits */ 269 LIMIT_OP("maximum-capacity", sel_critmax, PROP_BATTMAX); 270 LIMIT_OP("high-capacity", sel_warnmax, PROP_BATTHIGH); 271 LIMIT_OP("warning-capacity", sel_warnmin, PROP_BATTWARN); 272 LIMIT_OP("critical-capacity", sel_critmin, PROP_BATTCAP); 273 274#undef LIMIT_OP 275 276 if (props & PROP_DRIVER_LIMITS) 277 edata->upropset |= PROP_DRIVER_LIMITS; 278 else 279 edata->upropset &= ~PROP_DRIVER_LIMITS; 280 281 DPRINTF(("%s: (%s) event registered (sensor=%s snum=%d type=%d " 282 "critmin=%" PRIu32 " warnmin=%" PRIu32 " warnmax=%" PRIu32 283 " critmax=%" PRIu32 " props 0x%04x)\n", __func__, 284 see->see_sme->sme_name, see->see_pes.pes_sensname, 285 edata->sensor, see->see_type, edata->limits.sel_critmin, 286 edata->limits.sel_warnmin, edata->limits.sel_warnmax, 287 edata->limits.sel_critmax, edata->upropset)); 288 /* 289 * Initialize the events framework if it wasn't initialized before. 290 */ 291 if ((sme->sme_flags & SME_CALLOUT_INITIALIZED) == 0) 292 error = sme_events_init(sme); 293 294 /* 295 * If driver requested notification, advise it of new 296 * limit values 297 */ 298 if (sme->sme_set_limits) 299 (*sme->sme_set_limits)(sme, edata, &(edata->limits), 300 &(edata->upropset)); 301 302out: 303 if ((error == 0 || error == EEXIST) && osee == NULL) 304 LIST_INSERT_HEAD(&sme->sme_events_list, see, see_list); 305 306 mutex_exit(&sme->sme_mtx); 307 308 return error; 309} 310 311/* 312 * sme_event_unregister_all: 313 * 314 * + Unregisters all events associated with a sysmon envsys device. 315 */ 316void 317sme_event_unregister_all(struct sysmon_envsys *sme) 318{ 319 sme_event_t *see; 320 int evcounter = 0; 321 322 KASSERT(sme != NULL); 323 324 mutex_enter(&sme->sme_mtx); 325 LIST_FOREACH(see, &sme->sme_events_list, see_list) { 326 while (see->see_flags & SEE_EVENT_WORKING) 327 cv_wait(&sme->sme_condvar, &sme->sme_mtx); 328 329 if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0) 330 evcounter++; 331 } 332 333 DPRINTF(("%s: total events %d (%s)\n", __func__, 334 evcounter, sme->sme_name)); 335 336 while ((see = LIST_FIRST(&sme->sme_events_list))) { 337 if (evcounter == 0) 338 break; 339 340 if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0) { 341 DPRINTF(("%s: event %s %d removed (%s)\n", __func__, 342 see->see_pes.pes_sensname, see->see_type, 343 sme->sme_name)); 344 sme_remove_event(see, sme); 345 346 evcounter--; 347 } 348 } 349 350 if (LIST_EMPTY(&sme->sme_events_list)) 351 if (sme->sme_flags & SME_CALLOUT_INITIALIZED) 352 sme_events_destroy(sme); 353 mutex_exit(&sme->sme_mtx); 354} 355 356/* 357 * sme_event_unregister: 358 * 359 * + Unregisters an event from the specified sysmon envsys device. 360 */ 361int 362sme_event_unregister(struct sysmon_envsys *sme, const char *sensor, int type) 363{ 364 sme_event_t *see; 365 bool found = false; 366 367 KASSERT(sensor != NULL); 368 369 mutex_enter(&sme->sme_mtx); 370 LIST_FOREACH(see, &sme->sme_events_list, see_list) { 371 if (strcmp(see->see_pes.pes_sensname, sensor) == 0) { 372 if (see->see_type == type) { 373 found = true; 374 break; 375 } 376 } 377 } 378 379 if (!found) { 380 mutex_exit(&sme->sme_mtx); 381 return EINVAL; 382 } 383 384 /* 385 * Wait for the event to finish its work, remove from the list 386 * and release resouces. 387 */ 388 while (see->see_flags & SEE_EVENT_WORKING) 389 cv_wait(&sme->sme_condvar, &sme->sme_mtx); 390 391 DPRINTF(("%s: removed dev=%s sensor=%s type=%d\n", 392 __func__, see->see_pes.pes_dvname, sensor, type)); 393 394 sme_remove_event(see, sme); 395 396 mutex_exit(&sme->sme_mtx); 397 return 0; 398} 399 400/* 401 * sme_event_unregister_sensor: 402 * 403 * + Unregisters any event associated with a specific sensor 404 * The caller must already own the sme_mtx. 405 */ 406int 407sme_event_unregister_sensor(struct sysmon_envsys *sme, envsys_data_t *edata) 408{ 409 sme_event_t *see; 410 bool found = false; 411 412 KASSERT(mutex_owned(&sme->sme_mtx)); 413 LIST_FOREACH(see, &sme->sme_events_list, see_list) { 414 if (see->see_edata == edata) { 415 found = true; 416 break; 417 } 418 } 419 if (!found) 420 return EINVAL; 421 422 /* 423 * Wait for the event to finish its work, remove from the list 424 * and release resouces. 425 */ 426 while (see->see_flags & SEE_EVENT_WORKING) 427 cv_wait(&sme->sme_condvar, &sme->sme_mtx); 428 429 DPRINTF(("%s: removed dev=%s sensor=%s\n", 430 __func__, see->see_pes.pes_dvname, edata->desc)); 431 432 sme_remove_event(see, sme); 433 434 return 0; 435} 436 437static void 438sme_remove_event(sme_event_t *see, struct sysmon_envsys *sme) 439{ 440 441 KASSERT(mutex_owned(&sme->sme_mtx)); 442 443 LIST_REMOVE(see, see_list); 444 /* 445 * So the events list is empty, we'll do the following: 446 * 447 * - stop and destroy the callout. 448 * - destroy the workqueue. 449 */ 450 if (LIST_EMPTY(&sme->sme_events_list)) 451 sme_events_destroy(sme); 452 453 kmem_free(see, sizeof(*see)); 454} 455 456/* 457 * sme_event_drvadd: 458 * 459 * + Registers a new event for a device that had enabled any of 460 * the monitoring flags in the driver. 461 */ 462void 463sme_event_drvadd(void *arg) 464{ 465 sme_event_drv_t *sed_t = arg; 466 sysmon_envsys_lim_t lims; 467 uint32_t props; 468 int error = 0; 469 470 KASSERT(sed_t != NULL); 471 472#define SEE_REGEVENT(a, b, c) \ 473do { \ 474 if (sed_t->sed_edata->flags & (a)) { \ 475 char str[ENVSYS_DESCLEN] = "monitoring-state-"; \ 476 \ 477 error = sme_event_register(sed_t->sed_sdict, \ 478 sed_t->sed_edata, \ 479 sed_t->sed_sme, \ 480 &lims, props, \ 481 (b), \ 482 sed_t->sed_powertype); \ 483 if (error && error != EEXIST) \ 484 printf("%s: failed to add event! " \ 485 "error=%d sensor=%s event=%s\n", \ 486 __func__, error, \ 487 sed_t->sed_edata->desc, (c)); \ 488 else { \ 489 (void)strlcat(str, (c), sizeof(str)); \ 490 prop_dictionary_set_bool(sed_t->sed_sdict, \ 491 str, \ 492 true); \ 493 } \ 494 } \ 495} while (/* CONSTCOND */ 0) 496 497 /* 498 * If driver provides a method to retrieve its internal limit 499 * values, call it and use those returned values as initial 500 * limits for event monitoring. 501 */ 502 props = 0; 503 if (sed_t->sed_edata->flags & ENVSYS_FMONLIMITS) 504 if (sed_t->sed_sme->sme_get_limits) 505 (*sed_t->sed_sme->sme_get_limits)(sed_t->sed_sme, 506 sed_t->sed_edata, 507 &lims, &props); 508 /* 509 * If driver doesn't provide a way to "absorb" user-specified 510 * limit values, we must monitor all limits ourselves 511 */ 512 if (sed_t->sed_sme->sme_set_limits == NULL) 513 props &= ~PROP_DRIVER_LIMITS; 514 515 /* Register the events that were specified */ 516 517 SEE_REGEVENT(ENVSYS_FMONCRITICAL, 518 PENVSYS_EVENT_CRITICAL, 519 "critical"); 520 521 SEE_REGEVENT(ENVSYS_FMONSTCHANGED, 522 PENVSYS_EVENT_STATE_CHANGED, 523 "state-changed"); 524 525 SEE_REGEVENT(ENVSYS_FMONLIMITS, 526 PENVSYS_EVENT_LIMITS, 527 "hw-range-limits"); 528 529 /* 530 * we are done, free memory now. 531 */ 532 kmem_free(sed_t, sizeof(*sed_t)); 533} 534 535/* 536 * sme_events_init: 537 * 538 * + Initialize the events framework for this device. 539 */ 540int 541sme_events_init(struct sysmon_envsys *sme) 542{ 543 int error = 0; 544 545 KASSERT(sme != NULL); 546 KASSERT(mutex_owned(&sme->sme_mtx)); 547 548 error = workqueue_create(&sme->sme_wq, sme->sme_name, 549 sme_events_worker, sme, PRI_NONE, IPL_SOFTCLOCK, WQ_MPSAFE); 550 if (error) 551 return error; 552 553 mutex_init(&sme->sme_callout_mtx, MUTEX_DEFAULT, IPL_SOFTCLOCK); 554 callout_init(&sme->sme_callout, CALLOUT_MPSAFE); 555 callout_setfunc(&sme->sme_callout, sme_events_check, sme); 556 sme->sme_flags |= SME_CALLOUT_INITIALIZED; 557 sme_schedule_callout(sme); 558 DPRINTF(("%s: events framework initialized for '%s'\n", 559 __func__, sme->sme_name)); 560 561 return error; 562} 563 564/* 565 * sme_schedule_callout 566 * 567 * (Re)-schedule the device's callout timer 568 */ 569void 570sme_schedule_callout(struct sysmon_envsys *sme) 571{ 572 uint64_t timo; 573 574 KASSERT(sme != NULL); 575 576 if ((sme->sme_flags & SME_CALLOUT_INITIALIZED) == 0) 577 return; 578 579 if (sme->sme_events_timeout) 580 timo = sme->sme_events_timeout * hz; 581 else 582 timo = SME_EVTIMO; 583 584 callout_stop(&sme->sme_callout); 585 callout_schedule(&sme->sme_callout, timo); 586} 587 588/* 589 * sme_events_destroy: 590 * 591 * + Destroys the event framework for this device: callout 592 * stopped, workqueue destroyed and callout mutex destroyed. 593 */ 594void 595sme_events_destroy(struct sysmon_envsys *sme) 596{ 597 KASSERT(mutex_owned(&sme->sme_mtx)); 598 599 callout_stop(&sme->sme_callout); 600 workqueue_destroy(sme->sme_wq); 601 mutex_destroy(&sme->sme_callout_mtx); 602 callout_destroy(&sme->sme_callout); 603 sme->sme_flags &= ~SME_CALLOUT_INITIALIZED; 604 DPRINTF(("%s: events framework destroyed for '%s'\n", 605 __func__, sme->sme_name)); 606} 607 608/* 609 * sysmon_envsys_update_limits 610 * 611 * + If a driver needs to update the limits that it is providing, 612 * we need to update the dictionary data as well as the limits. 613 * This only makes sense if the driver is capable of providing 614 * its limits, and if there is a limits event-monitor. 615 */ 616int 617sysmon_envsys_update_limits(struct sysmon_envsys *sme, envsys_data_t *edata) 618{ 619 int err; 620 621 sysmon_envsys_acquire(sme, false); 622 if (sme->sme_get_limits == NULL || 623 (edata->flags & ENVSYS_FMONLIMITS) == 0) 624 err = EINVAL; 625 else 626 err = sme_update_limits(sme, edata); 627 sysmon_envsys_release(sme, false); 628 629 return err; 630} 631 632/* 633 * sme_update_limits 634 * 635 * + Internal version of sysmon_envsys_update_limits() to be used 636 * when the device has already been sysmon_envsys_acquire()d. 637 */ 638 639int 640sme_update_limits(struct sysmon_envsys *sme, envsys_data_t *edata) 641{ 642 prop_dictionary_t sdict = NULL; 643 prop_array_t array = NULL; 644 sysmon_envsys_lim_t lims; 645 sme_event_t *see; 646 uint32_t props = 0; 647 648 /* Find the dictionary for this sensor */ 649 array = prop_dictionary_get(sme_propd, sme->sme_name); 650 if (array == NULL || 651 prop_object_type(array) != PROP_TYPE_ARRAY) { 652 DPRINTF(("%s: array device failed\n", __func__)); 653 return EINVAL; 654 } 655 656 sdict = prop_array_get(array, edata->sensor); 657 if (sdict == NULL) { 658 return EINVAL; 659 } 660 661 /* Find the event definition to get its powertype */ 662 LIST_FOREACH(see, &sme->sme_events_list, see_list) { 663 if (edata == see->see_edata && 664 see->see_type == PENVSYS_EVENT_LIMITS) 665 break; 666 } 667 if (see == NULL) 668 return EINVAL; 669 670 /* Update limit values from driver if possible */ 671 if (sme->sme_get_limits != NULL) 672 (*sme->sme_get_limits)(sme, edata, &lims, &props); 673 674 /* Update event and dictionary */ 675 sme_event_register(sdict, edata, sme, &lims, props, 676 PENVSYS_EVENT_LIMITS, see->see_pes.pes_type); 677 678 return 0; 679} 680 681/* 682 * sme_events_check: 683 * 684 * + Passes the events to the workqueue thread and stops 685 * the callout if the 'low-power' condition is triggered. 686 */ 687void 688sme_events_check(void *arg) 689{ 690 struct sysmon_envsys *sme = arg; 691 sme_event_t *see; 692 uint64_t timo; 693 694 KASSERT(sme != NULL); 695 696 mutex_enter(&sme->sme_callout_mtx); 697 LIST_FOREACH(see, &sme->sme_events_list, see_list) { 698 workqueue_enqueue(sme->sme_wq, &see->see_wk, NULL); 699 see->see_edata->flags |= ENVSYS_FNEED_REFRESH; 700 } 701 if (sme->sme_events_timeout) 702 timo = sme->sme_events_timeout * hz; 703 else 704 timo = SME_EVTIMO; 705 if (!sysmon_low_power) 706 sme_schedule_callout(sme); 707 mutex_exit(&sme->sme_callout_mtx); 708} 709 710/* 711 * sme_events_worker: 712 * 713 * + workqueue thread that checks if there's a critical condition 714 * and sends an event if it was triggered. 715 */ 716void 717sme_events_worker(struct work *wk, void *arg) 718{ 719 sme_event_t *see = (void *)wk; 720 struct sysmon_envsys *sme = see->see_sme; 721 envsys_data_t *edata = see->see_edata; 722 723 KASSERT(wk == &see->see_wk); 724 KASSERT(sme != NULL || edata != NULL); 725 726 mutex_enter(&sme->sme_mtx); 727 see->see_flags |= SEE_EVENT_WORKING; 728 /* 729 * sme_events_check marks the sensors to make us refresh them here. 730 * Don't refresh if the driver uses its own method for refreshing. 731 */ 732 if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0) { 733 if ((edata->flags & ENVSYS_FNEED_REFRESH) != 0) { 734 /* refresh sensor in device */ 735 (*sme->sme_refresh)(sme, edata); 736 edata->flags &= ~ENVSYS_FNEED_REFRESH; 737 } 738 } 739 740 DPRINTFOBJ(("%s: (%s) desc=%s sensor=%d type=%d state=%d units=%d " 741 "value_cur=%d upropset=%d\n", __func__, sme->sme_name, edata->desc, 742 edata->sensor, see->see_type, edata->state, edata->units, 743 edata->value_cur, edata->upropset)); 744 745 /* skip the event if current sensor is in invalid state */ 746 if (edata->state == ENVSYS_SINVALID) 747 goto out; 748 749 /* 750 * For range limits, if the driver claims responsibility for 751 * limit/range checking, just user driver-supplied status. 752 * Else calculate our own status. Note that driver must 753 * relinquish responsibility for ALL limits if there is even 754 * one limit that it cannot handle! 755 * 756 * If this is a CAPACITY monitor, but the sensor's max_value 757 * is not set, treat it as though the monitor does not exist. 758 */ 759 if ((see->see_type == PENVSYS_EVENT_LIMITS || 760 see->see_type == PENVSYS_EVENT_CAPACITY) && 761 (edata->upropset & PROP_DRIVER_LIMITS) == 0) { 762 if ((see->see_type == PENVSYS_EVENT_CAPACITY) && 763 (edata->value_max == 0)) 764 edata->state = ENVSYS_SVALID; 765 else if ((edata->upropset & (PROP_CRITMIN | PROP_BATTCAP)) && 766 (edata->value_cur < edata->limits.sel_critmin)) 767 edata->state = ENVSYS_SCRITUNDER; 768 else if ((edata->upropset & (PROP_WARNMIN | PROP_BATTWARN)) && 769 (edata->value_cur < edata->limits.sel_warnmin)) 770 edata->state = ENVSYS_SWARNUNDER; 771 else if ((edata->upropset & (PROP_CRITMAX | PROP_BATTMAX)) && 772 (edata->value_cur > edata->limits.sel_critmax)) 773 edata->state = ENVSYS_SCRITOVER; 774 else if ((edata->upropset & (PROP_WARNMAX | PROP_BATTHIGH)) && 775 (edata->value_cur > edata->limits.sel_warnmax)) 776 edata->state = ENVSYS_SWARNOVER; 777 else 778 edata->state = ENVSYS_SVALID; 779 } 780 sme_deliver_event(see); 781 782out: 783 see->see_flags &= ~SEE_EVENT_WORKING; 784 cv_broadcast(&sme->sme_condvar); 785 mutex_exit(&sme->sme_mtx); 786} 787 788/* 789 * sysmon_envsys_sensor_event 790 * 791 * + Find the monitor event of a particular type for a given sensor 792 * on a device and deliver the event if one is required. If 793 * no event type is specified, deliver all events for the sensor. 794 */ 795void 796sysmon_envsys_sensor_event(struct sysmon_envsys *sme, envsys_data_t *edata, 797 int ev_type) 798{ 799 sme_event_t *see; 800 801 mutex_enter(&sme->sme_mtx); 802 LIST_FOREACH(see, &sme->sme_events_list, see_list) { 803 if (edata != see->see_edata) 804 continue; 805 if (ev_type == 0 || 806 ev_type == see->see_type) { 807 sme_deliver_event(see); 808 if (ev_type != 0) 809 break; 810 } 811 } 812 mutex_exit(&sme->sme_mtx); 813} 814 815/* 816 * sme_deliver_event: 817 * 818 * + If new sensor state requires it, send an event to powerd 819 * 820 * Must be called with the device's sysmon mutex held 821 * see->see_sme->sme_mtx 822 */ 823void 824sme_deliver_event(sme_event_t *see) 825{ 826 envsys_data_t *edata = see->see_edata; 827 const struct sme_descr_entry *sdt = NULL; 828 const struct sme_sensor_event *sse = sme_sensor_event; 829 int i, state = 0; 830 831 switch (see->see_type) { 832 case PENVSYS_EVENT_LIMITS: 833 case PENVSYS_EVENT_CAPACITY: 834 /* 835 * Send event if state has changed 836 */ 837 if (edata->state == see->see_evstate) 838 break; 839 840 for (i = 0; sse[i].state != -1; i++) 841 if (sse[i].state == edata->state) 842 break; 843 844 if (sse[i].state == -1) 845 break; 846 847 if (edata->state == ENVSYS_SVALID) 848 sysmon_penvsys_event(&see->see_pes, 849 PENVSYS_EVENT_NORMAL); 850 else 851 sysmon_penvsys_event(&see->see_pes, sse[i].event); 852 853 see->see_evstate = edata->state; 854 DPRINTFOBJ(("%s: (%s) desc=%s sensor=%d state=%d send_ev=%d\n", 855 __func__, see->see_sme->sme_name, edata->desc, 856 edata->sensor, edata->state, 857 (edata->state == ENVSYS_SVALID) ? PENVSYS_EVENT_NORMAL : 858 sse[i].event)); 859 860 break; 861 862 /* 863 * Send PENVSYS_EVENT_CRITICAL event if: 864 * State has gone from non-CRITICAL to CRITICAL, 865 * State remains CRITICAL and value has changed, or 866 * State has returned from CRITICAL to non-CRITICAL 867 */ 868 case PENVSYS_EVENT_CRITICAL: 869 DPRINTF(("%s: CRITICAL: old/new state %d/%d, old/new value " 870 "%d/%d\n", __func__, see->see_evstate, edata->state, 871 see->see_evvalue, edata->value_cur)); 872 if (edata->state == ENVSYS_SVALID && 873 see->see_evstate != ENVSYS_SVALID) { 874 sysmon_penvsys_event(&see->see_pes, 875 PENVSYS_EVENT_NORMAL); 876 see->see_evstate = ENVSYS_SVALID; 877 break; 878 } else if (edata->state != ENVSYS_SCRITICAL) 879 break; 880 if (see->see_evstate != ENVSYS_SCRITICAL || 881 see->see_evvalue != edata->value_cur) { 882 sysmon_penvsys_event(&see->see_pes, 883 PENVSYS_EVENT_CRITICAL); 884 see->see_evstate = ENVSYS_SCRITICAL; 885 } 886 see->see_evvalue = edata->value_cur; 887 break; 888 889 /* 890 * if value_cur is not normal (battery) or online (drive), 891 * send the event... 892 */ 893 case PENVSYS_EVENT_STATE_CHANGED: 894 /* 895 * the state has not been changed, just ignore the event. 896 */ 897 if (edata->value_cur == see->see_evvalue) 898 break; 899 900 switch (edata->units) { 901 case ENVSYS_DRIVE: 902 sdt = sme_find_table_entry(SME_DESC_DRIVE_STATES, 903 edata->value_cur); 904 state = ENVSYS_DRIVE_ONLINE; 905 break; 906 case ENVSYS_BATTERY_CAPACITY: 907 sdt = sme_find_table_entry(SME_DESC_BATTERY_CAPACITY, 908 edata->value_cur); 909 state = ENVSYS_BATTERY_CAPACITY_NORMAL; 910 break; 911 case ENVSYS_INDICATOR: 912 sdt = sme_find_table_entry(SME_DESC_INDICATOR, 913 edata->value_cur); 914 state = see->see_evvalue; /* force state change */ 915 break; 916 default: 917 panic("%s: bad units for PENVSYS_EVENT_STATE_CHANGED", 918 __func__); 919 } 920 921 if (sdt->type == -1) 922 break; 923 924 /* 925 * copy current state description. 926 */ 927 (void)strlcpy(see->see_pes.pes_statedesc, sdt->desc, 928 sizeof(see->see_pes.pes_statedesc)); 929 930 if (edata->value_cur == state) 931 /* 932 * state returned to normal condition 933 */ 934 sysmon_penvsys_event(&see->see_pes, 935 PENVSYS_EVENT_NORMAL); 936 else 937 /* 938 * state changed to abnormal condition 939 */ 940 sysmon_penvsys_event(&see->see_pes, see->see_type); 941 942 see->see_evvalue = edata->value_cur; 943 944 /* 945 * There's no need to continue if it's a drive sensor. 946 */ 947 if (edata->units == ENVSYS_DRIVE) 948 break; 949 950 /* 951 * Check if the system is running in low power and send the 952 * event to powerd (if running) or shutdown the system 953 * otherwise. 954 */ 955 if (!sysmon_low_power && sme_event_check_low_power()) { 956 struct penvsys_state pes; 957 958 /* 959 * Stop the callout and send the 'low-power' event. 960 */ 961 sysmon_low_power = true; 962 callout_stop(&see->see_sme->sme_callout); 963 pes.pes_type = PENVSYS_TYPE_BATTERY; 964 sysmon_penvsys_event(&pes, PENVSYS_EVENT_LOW_POWER); 965 } 966 break; 967 default: 968 panic("%s: invalid event type %d", __func__, see->see_type); 969 } 970} 971 972/* 973 * Returns true if the system is in low power state: an AC adapter 974 * is OFF and all batteries are in LOW/CRITICAL state. 975 */ 976static bool 977sme_event_check_low_power(void) 978{ 979 if (!sme_acadapter_check()) 980 return false; 981 982 return sme_battery_check(); 983} 984 985/* 986 * Called with the sysmon_envsys device mtx held through the 987 * workqueue thread. 988 */ 989static bool 990sme_acadapter_check(void) 991{ 992 struct sysmon_envsys *sme; 993 envsys_data_t *edata; 994 bool dev = false, sensor = false; 995 996 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) { 997 if (sme->sme_class == SME_CLASS_ACADAPTER) { 998 dev = true; 999 break; 1000 } 1001 } 1002 1003 /* 1004 * No AC Adapter devices were found. 1005 */ 1006 if (!dev) 1007 return false; 1008 1009 /* 1010 * Check if there's an AC adapter device connected. 1011 */ 1012 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 1013 if (edata->units == ENVSYS_INDICATOR) { 1014 sensor = true; 1015 /* refresh current sensor */ 1016 if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0) 1017 (*sme->sme_refresh)(sme, edata); 1018 if (edata->value_cur) 1019 return false; 1020 } 1021 } 1022 1023 if (!sensor) 1024 return false; 1025 1026 /* 1027 * AC adapter found and not connected. 1028 */ 1029 return true; 1030} 1031 1032/* 1033 * Called with the sysmon_envsys device mtx held through the 1034 * workqueue thread. 1035 */ 1036static bool 1037sme_battery_check(void) 1038{ 1039 struct sysmon_envsys *sme; 1040 envsys_data_t *edata; 1041 int batteriesfound = 0; 1042 bool present, batterycap, batterycharge; 1043 1044 /* 1045 * Check for battery devices and its state. 1046 */ 1047 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) { 1048 if (sme->sme_class != SME_CLASS_BATTERY) 1049 continue; 1050 1051 present = true; 1052 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 1053 if (edata->units == ENVSYS_INDICATOR && 1054 !edata->value_cur) { 1055 present = false; 1056 break; 1057 } 1058 } 1059 if (!present) 1060 continue; 1061 /* 1062 * We've found a battery device... 1063 */ 1064 batteriesfound++; 1065 batterycap = batterycharge = false; 1066 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 1067 if (edata->units == ENVSYS_BATTERY_CAPACITY) { 1068 batterycap = true; 1069 if (!sme_battery_critical(edata)) 1070 return false; 1071 } else if (edata->units == ENVSYS_BATTERY_CHARGE) { 1072 batterycharge = true; 1073 if (edata->value_cur) 1074 return false; 1075 } 1076 } 1077 if (!batterycap || !batterycharge) 1078 return false; 1079 } 1080 1081 if (!batteriesfound) 1082 return false; 1083 1084 /* 1085 * All batteries in low/critical capacity and discharging. 1086 */ 1087 return true; 1088} 1089 1090static bool 1091sme_battery_critical(envsys_data_t *edata) 1092{ 1093 if (edata->value_cur == ENVSYS_BATTERY_CAPACITY_CRITICAL || 1094 edata->value_cur == ENVSYS_BATTERY_CAPACITY_LOW) 1095 return true; 1096 1097 return false; 1098} 1099