1 /* $NetBSD: sysmon_power.c,v 1.66 2020/12/18 01:46:39 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2007 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 * Copyright (c) 2003 Wasabi Systems, Inc. 30 * All rights reserved. 31 * 32 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 33 * 34 * Redistribution and use in source and binary forms, with or without 35 * modification, are permitted provided that the following conditions 36 * are met: 37 * 1. Redistributions of source code must retain the above copyright 38 * notice, this list of conditions and the following disclaimer. 39 * 2. Redistributions in binary form must reproduce the above copyright 40 * notice, this list of conditions and the following disclaimer in the 41 * documentation and/or other materials provided with the distribution. 42 * 3. All advertising materials mentioning features or use of this software 43 * must display the following acknowledgement: 44 * This product includes software developed for the NetBSD Project by 45 * Wasabi Systems, Inc. 46 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 47 * or promote products derived from this software without specific prior 48 * written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 52 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 53 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 54 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 55 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 56 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 57 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 58 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 59 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 60 * POSSIBILITY OF SUCH DAMAGE. 61 */ 62 63 /* 64 * Power management framework for sysmon. 65 * 66 * We defer to a power management daemon running in userspace, since 67 * power management is largely a policy issue. This merely provides 68 * for power management event notification to that daemon. 69 */ 70 71 #include <sys/cdefs.h> 72 __KERNEL_RCSID(0, "$NetBSD: sysmon_power.c,v 1.66 2020/12/18 01:46:39 thorpej Exp $"); 73 74 #ifdef _KERNEL_OPT 75 #include "opt_compat_netbsd.h" 76 #endif 77 78 #include <sys/param.h> 79 #include <sys/reboot.h> 80 #include <sys/systm.h> 81 #include <sys/poll.h> 82 #include <sys/select.h> 83 #include <sys/vnode.h> 84 #include <sys/condvar.h> 85 #include <sys/mutex.h> 86 #include <sys/kmem.h> 87 #include <sys/proc.h> 88 #include <sys/device.h> 89 #include <sys/rndsource.h> 90 #include <sys/module.h> 91 #include <sys/once.h> 92 #include <sys/compat_stub.h> 93 94 #include <dev/sysmon/sysmonvar.h> 95 #include <prop/proplib.h> 96 97 MODULE(MODULE_CLASS_DRIVER, sysmon_power, "sysmon"); 98 99 /* 100 * Singly linked list for dictionaries to be stored/sent. 101 */ 102 struct power_event_dictionary { 103 SIMPLEQ_ENTRY(power_event_dictionary) pev_dict_head; 104 prop_dictionary_t dict; 105 int flags; 106 }; 107 108 struct power_event_description { 109 int type; 110 const char *desc; 111 }; 112 113 /* 114 * Available events for power switches. 115 */ 116 static const struct power_event_description pswitch_event_desc[] = { 117 { PSWITCH_EVENT_PRESSED, "pressed" }, 118 { PSWITCH_EVENT_RELEASED, "released" }, 119 { -1, NULL } 120 }; 121 122 /* 123 * Available script names for power switches. 124 */ 125 static const struct power_event_description pswitch_type_desc[] = { 126 { PSWITCH_TYPE_POWER, "power_button" }, 127 { PSWITCH_TYPE_SLEEP, "sleep_button" }, 128 { PSWITCH_TYPE_LID, "lid_switch" }, 129 { PSWITCH_TYPE_RESET, "reset_button" }, 130 { PSWITCH_TYPE_ACADAPTER, "acadapter" }, 131 { PSWITCH_TYPE_HOTKEY, "hotkey_button" }, 132 { PSWITCH_TYPE_RADIO, "radio_button" }, 133 { -1, NULL } 134 }; 135 136 /* 137 * Available events for envsys(4). 138 */ 139 static const struct power_event_description penvsys_event_desc[] = { 140 { PENVSYS_EVENT_NORMAL, "normal" }, 141 { PENVSYS_EVENT_CRITICAL, "critical" }, 142 { PENVSYS_EVENT_CRITOVER, "critical-over" }, 143 { PENVSYS_EVENT_CRITUNDER, "critical-under" }, 144 { PENVSYS_EVENT_WARNOVER, "warning-over" }, 145 { PENVSYS_EVENT_WARNUNDER, "warning-under" }, 146 { PENVSYS_EVENT_BATT_CRIT, "critical-capacity" }, 147 { PENVSYS_EVENT_BATT_WARN, "warning-capacity" }, 148 { PENVSYS_EVENT_BATT_HIGH, "high-capacity" }, 149 { PENVSYS_EVENT_BATT_MAX, "maximum-capacity" }, 150 { PENVSYS_EVENT_STATE_CHANGED, "state-changed" }, 151 { PENVSYS_EVENT_LOW_POWER, "low-power" }, 152 { -1, NULL } 153 }; 154 155 /* 156 * Available script names for envsys(4). 157 */ 158 static const struct power_event_description penvsys_type_desc[] = { 159 { PENVSYS_TYPE_BATTERY, "sensor_battery" }, 160 { PENVSYS_TYPE_DRIVE, "sensor_drive" }, 161 { PENVSYS_TYPE_FAN, "sensor_fan" }, 162 { PENVSYS_TYPE_INDICATOR, "sensor_indicator" }, 163 { PENVSYS_TYPE_POWER, "sensor_power" }, 164 { PENVSYS_TYPE_RESISTANCE, "sensor_resistance" }, 165 { PENVSYS_TYPE_TEMP, "sensor_temperature" }, 166 { PENVSYS_TYPE_VOLTAGE, "sensor_voltage" }, 167 { -1, NULL } 168 }; 169 170 #define SYSMON_MAX_POWER_EVENTS 32 171 #define SYSMON_POWER_DICTIONARY_BUSY 0x01 172 #define SYSMON_POWER_DICTIONARY_READY 0x02 173 174 static power_event_t sysmon_power_event_queue[SYSMON_MAX_POWER_EVENTS]; 175 static int sysmon_power_event_queue_head; 176 static int sysmon_power_event_queue_tail; 177 static int sysmon_power_event_queue_count; 178 179 static krndsource_t sysmon_rndsource; 180 181 static SIMPLEQ_HEAD(, power_event_dictionary) pev_dict_list = 182 SIMPLEQ_HEAD_INITIALIZER(pev_dict_list); 183 184 static struct selinfo sysmon_power_event_queue_selinfo; 185 static struct lwp *sysmon_power_daemon; 186 187 static kmutex_t sysmon_power_event_queue_mtx; 188 static kcondvar_t sysmon_power_event_queue_cv; 189 190 static char sysmon_power_type[32]; 191 192 static int sysmon_power_make_dictionary(prop_dictionary_t, void *, int, int); 193 static int sysmon_power_daemon_task(struct power_event_dictionary *, 194 void *, int); 195 static void sysmon_power_destroy_dictionary(struct power_event_dictionary *); 196 197 static struct sysmon_opvec sysmon_power_opvec = { 198 sysmonopen_power, sysmonclose_power, sysmonioctl_power, 199 sysmonread_power, sysmonpoll_power, sysmonkqfilter_power 200 }; 201 202 #define SYSMON_NEXT_EVENT(x) (((x) + 1) % SYSMON_MAX_POWER_EVENTS) 203 204 ONCE_DECL(once_power); 205 206 static int 207 power_preinit(void) 208 { 209 210 mutex_init(&sysmon_power_event_queue_mtx, MUTEX_DEFAULT, IPL_NONE); 211 cv_init(&sysmon_power_event_queue_cv, "smpower"); 212 213 return 0; 214 } 215 216 /* 217 * sysmon_power_init: 218 * 219 * Initializes the mutexes and condition variables in the 220 * boot process via module initialization process. 221 */ 222 int 223 sysmon_power_init(void) 224 { 225 int error; 226 227 (void)RUN_ONCE(&once_power, power_preinit); 228 229 selinit(&sysmon_power_event_queue_selinfo); 230 231 rnd_attach_source(&sysmon_rndsource, "system-power", 232 RND_TYPE_POWER, RND_FLAG_DEFAULT); 233 234 error = sysmon_attach_minor(SYSMON_MINOR_POWER, &sysmon_power_opvec); 235 236 return error; 237 } 238 239 int 240 sysmon_power_fini(void) 241 { 242 int error; 243 244 if (sysmon_power_daemon != NULL) 245 error = EBUSY; 246 else 247 error = sysmon_attach_minor(SYSMON_MINOR_POWER, NULL); 248 249 if (error == 0) { 250 rnd_detach_source(&sysmon_rndsource); 251 seldestroy(&sysmon_power_event_queue_selinfo); 252 cv_destroy(&sysmon_power_event_queue_cv); 253 mutex_destroy(&sysmon_power_event_queue_mtx); 254 } 255 256 return error; 257 } 258 259 /* 260 * sysmon_queue_power_event: 261 * 262 * Enqueue a power event for the power management daemon. Returns 263 * non-zero if we were able to enqueue a power event. 264 */ 265 static int 266 sysmon_queue_power_event(power_event_t *pev) 267 { 268 KASSERT(mutex_owned(&sysmon_power_event_queue_mtx)); 269 270 if (sysmon_power_event_queue_count == SYSMON_MAX_POWER_EVENTS) 271 return 0; 272 273 sysmon_power_event_queue[sysmon_power_event_queue_head] = *pev; 274 sysmon_power_event_queue_head = 275 SYSMON_NEXT_EVENT(sysmon_power_event_queue_head); 276 sysmon_power_event_queue_count++; 277 278 return 1; 279 } 280 281 /* 282 * sysmon_get_power_event: 283 * 284 * Get a power event from the queue. Returns non-zero if there 285 * is an event available. 286 */ 287 static int 288 sysmon_get_power_event(power_event_t *pev) 289 { 290 KASSERT(mutex_owned(&sysmon_power_event_queue_mtx)); 291 292 if (sysmon_power_event_queue_count == 0) 293 return 0; 294 295 *pev = sysmon_power_event_queue[sysmon_power_event_queue_tail]; 296 sysmon_power_event_queue_tail = 297 SYSMON_NEXT_EVENT(sysmon_power_event_queue_tail); 298 sysmon_power_event_queue_count--; 299 300 return 1; 301 } 302 303 /* 304 * sysmon_power_event_queue_flush: 305 * 306 * Flush the event queue, and reset all state. 307 */ 308 static void 309 sysmon_power_event_queue_flush(void) 310 { 311 KASSERT(mutex_owned(&sysmon_power_event_queue_mtx)); 312 313 sysmon_power_event_queue_head = 0; 314 sysmon_power_event_queue_tail = 0; 315 sysmon_power_event_queue_count = 0; 316 } 317 318 /* 319 * sysmon_power_daemon_task: 320 * 321 * Assign required power event members and sends a signal 322 * to the process to notify that an event was enqueued successfully. 323 */ 324 static int 325 sysmon_power_daemon_task(struct power_event_dictionary *ped, 326 void *pev_data, int event) 327 { 328 power_event_t pev; 329 int rv, error = 0; 330 331 if (!ped || !ped->dict || !pev_data) 332 return EINVAL; 333 334 memset(&pev, 0, sizeof(pev)); 335 336 mutex_enter(&sysmon_power_event_queue_mtx); 337 338 switch (event) { 339 /* 340 * Power switch events. 341 */ 342 case PSWITCH_EVENT_PRESSED: 343 case PSWITCH_EVENT_RELEASED: 344 { 345 346 struct sysmon_pswitch *pswitch = 347 (struct sysmon_pswitch *)pev_data; 348 349 pev.pev_type = POWER_EVENT_SWITCH_STATE_CHANGE; 350 351 MODULE_HOOK_CALL_VOID(compat_sysmon_power_40_hook, 352 (&pev, pswitch, event), __nothing); 353 354 error = sysmon_power_make_dictionary(ped->dict, 355 pswitch, 356 event, 357 pev.pev_type); 358 if (error) { 359 mutex_exit(&sysmon_power_event_queue_mtx); 360 goto out; 361 } 362 363 break; 364 } 365 366 /* 367 * ENVSYS events. 368 */ 369 case PENVSYS_EVENT_NORMAL: 370 case PENVSYS_EVENT_CRITICAL: 371 case PENVSYS_EVENT_CRITUNDER: 372 case PENVSYS_EVENT_CRITOVER: 373 case PENVSYS_EVENT_WARNUNDER: 374 case PENVSYS_EVENT_WARNOVER: 375 case PENVSYS_EVENT_BATT_CRIT: 376 case PENVSYS_EVENT_BATT_WARN: 377 case PENVSYS_EVENT_BATT_HIGH: 378 case PENVSYS_EVENT_BATT_MAX: 379 case PENVSYS_EVENT_STATE_CHANGED: 380 case PENVSYS_EVENT_LOW_POWER: 381 { 382 struct penvsys_state *penvsys = 383 (struct penvsys_state *)pev_data; 384 385 pev.pev_type = POWER_EVENT_ENVSYS_STATE_CHANGE; 386 387 error = sysmon_power_make_dictionary(ped->dict, 388 penvsys, 389 event, 390 pev.pev_type); 391 if (error) { 392 mutex_exit(&sysmon_power_event_queue_mtx); 393 goto out; 394 } 395 396 break; 397 } 398 default: 399 error = ENOTTY; 400 mutex_exit(&sysmon_power_event_queue_mtx); 401 goto out; 402 } 403 404 /* 405 * Enqueue the event. 406 */ 407 rv = sysmon_queue_power_event(&pev); 408 if (rv == 0) { 409 printf("%s: WARNING: state change event %d lost; " 410 "queue full\n", __func__, pev.pev_type); 411 mutex_exit(&sysmon_power_event_queue_mtx); 412 error = EINVAL; 413 goto out; 414 } else { 415 /* 416 * Notify the daemon that an event is ready and its 417 * dictionary is ready to be fetched. 418 */ 419 ped->flags |= SYSMON_POWER_DICTIONARY_READY; 420 SIMPLEQ_INSERT_TAIL(&pev_dict_list, ped, pev_dict_head); 421 cv_broadcast(&sysmon_power_event_queue_cv); 422 mutex_exit(&sysmon_power_event_queue_mtx); 423 selnotify(&sysmon_power_event_queue_selinfo, 0, 0); 424 } 425 426 out: 427 return error; 428 } 429 430 /* 431 * sysmonopen_power: 432 * 433 * Open the system monitor device. 434 */ 435 int 436 sysmonopen_power(dev_t dev, int flag, int mode, struct lwp *l) 437 { 438 int error = 0; 439 440 mutex_enter(&sysmon_power_event_queue_mtx); 441 if (sysmon_power_daemon != NULL) 442 error = EBUSY; 443 else { 444 sysmon_power_daemon = l; 445 sysmon_power_event_queue_flush(); 446 } 447 mutex_exit(&sysmon_power_event_queue_mtx); 448 449 return error; 450 } 451 452 /* 453 * sysmonclose_power: 454 * 455 * Close the system monitor device. 456 */ 457 int 458 sysmonclose_power(dev_t dev, int flag, int mode, struct lwp *l) 459 { 460 int count; 461 462 mutex_enter(&sysmon_power_event_queue_mtx); 463 count = sysmon_power_event_queue_count; 464 sysmon_power_daemon = NULL; 465 sysmon_power_event_queue_flush(); 466 mutex_exit(&sysmon_power_event_queue_mtx); 467 468 if (count) 469 printf("WARNING: %d power event%s lost by exiting daemon\n", 470 count, count > 1 ? "s" : ""); 471 472 return 0; 473 } 474 475 /* 476 * sysmonread_power: 477 * 478 * Read the system monitor device. 479 */ 480 int 481 sysmonread_power(dev_t dev, struct uio *uio, int flags) 482 { 483 power_event_t pev; 484 int rv; 485 486 /* We only allow one event to be read at a time. */ 487 if (uio->uio_resid != POWER_EVENT_MSG_SIZE) 488 return EINVAL; 489 490 mutex_enter(&sysmon_power_event_queue_mtx); 491 for (;;) { 492 if (sysmon_get_power_event(&pev)) { 493 rv = uiomove(&pev, POWER_EVENT_MSG_SIZE, uio); 494 break; 495 } 496 497 if (flags & IO_NDELAY) { 498 rv = EWOULDBLOCK; 499 break; 500 } 501 502 cv_wait(&sysmon_power_event_queue_cv, 503 &sysmon_power_event_queue_mtx); 504 } 505 mutex_exit(&sysmon_power_event_queue_mtx); 506 507 return rv; 508 } 509 510 /* 511 * sysmonpoll_power: 512 * 513 * Poll the system monitor device. 514 */ 515 int 516 sysmonpoll_power(dev_t dev, int events, struct lwp *l) 517 { 518 int revents; 519 520 revents = events & (POLLOUT | POLLWRNORM); 521 522 /* Attempt to save some work. */ 523 if ((events & (POLLIN | POLLRDNORM)) == 0) 524 return revents; 525 526 mutex_enter(&sysmon_power_event_queue_mtx); 527 if (sysmon_power_event_queue_count) 528 revents |= events & (POLLIN | POLLRDNORM); 529 else 530 selrecord(l, &sysmon_power_event_queue_selinfo); 531 mutex_exit(&sysmon_power_event_queue_mtx); 532 533 return revents; 534 } 535 536 static void 537 filt_sysmon_power_rdetach(struct knote *kn) 538 { 539 540 mutex_enter(&sysmon_power_event_queue_mtx); 541 selremove_knote(&sysmon_power_event_queue_selinfo, kn); 542 mutex_exit(&sysmon_power_event_queue_mtx); 543 } 544 545 static int 546 filt_sysmon_power_read(struct knote *kn, long hint) 547 { 548 549 mutex_enter(&sysmon_power_event_queue_mtx); 550 kn->kn_data = sysmon_power_event_queue_count; 551 mutex_exit(&sysmon_power_event_queue_mtx); 552 553 return kn->kn_data > 0; 554 } 555 556 static const struct filterops sysmon_power_read_filtops = { 557 .f_isfd = 1, 558 .f_attach = NULL, 559 .f_detach = filt_sysmon_power_rdetach, 560 .f_event = filt_sysmon_power_read, 561 }; 562 563 static const struct filterops sysmon_power_write_filtops = { 564 .f_isfd = 1, 565 .f_attach = NULL, 566 .f_detach = filt_sysmon_power_rdetach, 567 .f_event = filt_seltrue, 568 }; 569 570 /* 571 * sysmonkqfilter_power: 572 * 573 * Kqueue filter for the system monitor device. 574 */ 575 int 576 sysmonkqfilter_power(dev_t dev, struct knote *kn) 577 { 578 579 switch (kn->kn_filter) { 580 case EVFILT_READ: 581 kn->kn_fop = &sysmon_power_read_filtops; 582 break; 583 584 case EVFILT_WRITE: 585 kn->kn_fop = &sysmon_power_write_filtops; 586 break; 587 588 default: 589 return EINVAL; 590 } 591 592 mutex_enter(&sysmon_power_event_queue_mtx); 593 selrecord_knote(&sysmon_power_event_queue_selinfo, kn); 594 mutex_exit(&sysmon_power_event_queue_mtx); 595 596 return 0; 597 } 598 599 /* 600 * sysmonioctl_power: 601 * 602 * Perform a power management control request. 603 */ 604 int 605 sysmonioctl_power(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 606 { 607 int error = 0; 608 609 switch (cmd) { 610 case POWER_IOC_GET_TYPE: 611 case POWER_IOC_GET_TYPE_WITH_LOSSAGE: 612 { 613 struct power_type *power_type = (void *) data; 614 615 (void)strlcpy(power_type->power_type, 616 sysmon_power_type, 617 sizeof(power_type->power_type)); 618 break; 619 } 620 case POWER_EVENT_RECVDICT: 621 { 622 struct plistref *plist = (struct plistref *)data; 623 struct power_event_dictionary *ped; 624 625 /* 626 * Get the first dictionary enqueued and mark it 627 * as busy. 628 */ 629 mutex_enter(&sysmon_power_event_queue_mtx); 630 ped = SIMPLEQ_FIRST(&pev_dict_list); 631 if (!ped || !ped->dict) { 632 mutex_exit(&sysmon_power_event_queue_mtx); 633 error = ENOTSUP; 634 break; 635 } 636 637 if ((ped->flags & SYSMON_POWER_DICTIONARY_READY) == 0) { 638 mutex_exit(&sysmon_power_event_queue_mtx); 639 error = EINVAL; 640 break; 641 } 642 643 if (ped->flags & SYSMON_POWER_DICTIONARY_BUSY) { 644 mutex_exit(&sysmon_power_event_queue_mtx); 645 error = EBUSY; 646 break; 647 } 648 649 ped->flags |= SYSMON_POWER_DICTIONARY_BUSY; 650 mutex_exit(&sysmon_power_event_queue_mtx); 651 652 /* 653 * Send it now. 654 */ 655 error = prop_dictionary_copyout_ioctl(plist, 656 cmd, 657 ped->dict); 658 659 /* 660 * Remove the dictionary now that we don't need it. 661 */ 662 mutex_enter(&sysmon_power_event_queue_mtx); 663 ped->flags &= ~SYSMON_POWER_DICTIONARY_BUSY; 664 ped->flags &= ~SYSMON_POWER_DICTIONARY_READY; 665 SIMPLEQ_REMOVE_HEAD(&pev_dict_list, pev_dict_head); 666 mutex_exit(&sysmon_power_event_queue_mtx); 667 sysmon_power_destroy_dictionary(ped); 668 669 break; 670 } 671 default: 672 error = ENOTTY; 673 } 674 675 return error; 676 } 677 678 /* 679 * sysmon_power_make_dictionary: 680 * 681 * Adds the properties for an event in a dictionary. 682 */ 683 int 684 sysmon_power_make_dictionary(prop_dictionary_t dict, void *power_data, 685 int event, int type) 686 { 687 int i; 688 689 KASSERT(mutex_owned(&sysmon_power_event_queue_mtx)); 690 691 switch (type) { 692 /* 693 * create the dictionary for a power switch event. 694 */ 695 case POWER_EVENT_SWITCH_STATE_CHANGE: 696 { 697 const struct power_event_description *peevent = 698 pswitch_event_desc; 699 const struct power_event_description *petype = 700 pswitch_type_desc; 701 struct sysmon_pswitch *smpsw = 702 (struct sysmon_pswitch *)power_data; 703 const char *pwrtype = "pswitch"; 704 705 #define SETPROP(key, str) \ 706 do { \ 707 if ((str) != NULL && !prop_dictionary_set_string(dict, \ 708 (key), \ 709 (str))) { \ 710 printf("%s: failed to set %s\n", __func__, (str)); \ 711 return EINVAL; \ 712 } \ 713 } while (/* CONSTCOND */ 0) 714 715 716 SETPROP("driver-name", smpsw->smpsw_name); 717 718 for (i = 0; peevent[i].type != -1; i++) 719 if (peevent[i].type == event) 720 break; 721 722 SETPROP("powerd-event-name", peevent[i].desc); 723 724 for (i = 0; petype[i].type != -1; i++) 725 if (petype[i].type == smpsw->smpsw_type) 726 break; 727 728 SETPROP("powerd-script-name", petype[i].desc); 729 SETPROP("power-type", pwrtype); 730 break; 731 } 732 /* 733 * create a dictionary for power envsys event. 734 */ 735 case POWER_EVENT_ENVSYS_STATE_CHANGE: 736 { 737 const struct power_event_description *peevent = 738 penvsys_event_desc; 739 const struct power_event_description *petype = 740 penvsys_type_desc; 741 struct penvsys_state *pes = 742 (struct penvsys_state *)power_data; 743 const char *pwrtype = "envsys"; 744 745 SETPROP("driver-name", pes->pes_dvname); 746 SETPROP("sensor-name", pes->pes_sensname); 747 SETPROP("state-description", pes->pes_statedesc); 748 749 for (i = 0; peevent[i].type != -1; i++) 750 if (peevent[i].type == event) 751 break; 752 753 SETPROP("powerd-event-name", peevent[i].desc); 754 755 for (i = 0; petype[i].type != -1; i++) 756 if (petype[i].type == pes->pes_type) 757 break; 758 759 SETPROP("powerd-script-name", petype[i].desc); 760 SETPROP("power-type", pwrtype); 761 break; 762 } 763 default: 764 return ENOTSUP; 765 } 766 767 return 0; 768 } 769 770 /* 771 * sysmon_power_destroy_dictionary: 772 * 773 * Destroys a power_event_dictionary object and all its 774 * properties in the dictionary. 775 */ 776 static void 777 sysmon_power_destroy_dictionary(struct power_event_dictionary *ped) 778 { 779 prop_object_iterator_t iter; 780 prop_object_t obj; 781 782 KASSERT(ped != NULL); 783 KASSERT((ped->flags & SYSMON_POWER_DICTIONARY_BUSY) == 0); 784 785 iter = prop_dictionary_iterator(ped->dict); 786 if (iter == NULL) 787 return; 788 789 while ((obj = prop_object_iterator_next(iter)) != NULL) { 790 prop_dictionary_remove(ped->dict, 791 prop_dictionary_keysym_value(obj)); 792 prop_object_iterator_reset(iter); 793 } 794 795 prop_object_iterator_release(iter); 796 prop_object_release(ped->dict); 797 798 kmem_free(ped, sizeof(*ped)); 799 } 800 801 /* 802 * sysmon_power_settype: 803 * 804 * Sets the back-end power management type. This information can 805 * be used by the power management daemon. 806 */ 807 void 808 sysmon_power_settype(const char *type) 809 { 810 811 /* 812 * Don't bother locking this; it's going to be set 813 * during autoconfiguration, and then only read from 814 * then on. 815 */ 816 (void)strlcpy(sysmon_power_type, type, sizeof(sysmon_power_type)); 817 } 818 819 #define PENVSYS_SHOWSTATE(str) \ 820 do { \ 821 printf("%s: %s limit on '%s'\n", \ 822 pes->pes_dvname, (str), pes->pes_sensname); \ 823 } while (/* CONSTCOND */ 0) 824 825 /* 826 * sysmon_penvsys_event: 827 * 828 * Puts an event onto the sysmon power queue and sends the 829 * appropriate event if the daemon is running, otherwise a 830 * message is shown. 831 */ 832 void 833 sysmon_penvsys_event(struct penvsys_state *pes, int event) 834 { 835 struct power_event_dictionary *ped; 836 const char *mystr = NULL; 837 838 KASSERT(pes != NULL); 839 840 rnd_add_uint32(&sysmon_rndsource, pes->pes_type); 841 842 if (sysmon_power_daemon != NULL) { 843 /* 844 * Create a dictionary for the new event. 845 */ 846 ped = kmem_zalloc(sizeof(*ped), KM_NOSLEEP); 847 if (!ped) 848 return; 849 ped->dict = prop_dictionary_create(); 850 851 if (sysmon_power_daemon_task(ped, pes, event) == 0) 852 return; 853 /* We failed */ 854 prop_object_release(ped->dict); 855 kmem_free(ped, sizeof(*ped)); 856 } 857 858 switch (pes->pes_type) { 859 case PENVSYS_TYPE_BATTERY: 860 switch (event) { 861 case PENVSYS_EVENT_LOW_POWER: 862 printf("sysmon: LOW POWER! SHUTTING DOWN.\n"); 863 kern_reboot(RB_POWERDOWN, NULL); 864 break; 865 case PENVSYS_EVENT_STATE_CHANGED: 866 printf("%s: state changed on '%s' to '%s'\n", 867 pes->pes_dvname, pes->pes_sensname, 868 pes->pes_statedesc); 869 break; 870 case PENVSYS_EVENT_BATT_CRIT: 871 mystr = "critical capacity"; 872 PENVSYS_SHOWSTATE(mystr); 873 break; 874 case PENVSYS_EVENT_BATT_WARN: 875 mystr = "warning capacity"; 876 PENVSYS_SHOWSTATE(mystr); 877 break; 878 case PENVSYS_EVENT_BATT_HIGH: 879 mystr = "high capacity"; 880 PENVSYS_SHOWSTATE(mystr); 881 break; 882 case PENVSYS_EVENT_BATT_MAX: 883 mystr = "maximum capacity"; 884 PENVSYS_SHOWSTATE(mystr); 885 break; 886 case PENVSYS_EVENT_NORMAL: 887 printf("%s: normal capacity on '%s'\n", 888 pes->pes_dvname, pes->pes_sensname); 889 break; 890 } 891 break; 892 case PENVSYS_TYPE_FAN: 893 case PENVSYS_TYPE_INDICATOR: 894 case PENVSYS_TYPE_TEMP: 895 case PENVSYS_TYPE_POWER: 896 case PENVSYS_TYPE_RESISTANCE: 897 case PENVSYS_TYPE_VOLTAGE: 898 switch (event) { 899 case PENVSYS_EVENT_CRITICAL: 900 mystr = "critical"; 901 PENVSYS_SHOWSTATE(mystr); 902 break; 903 case PENVSYS_EVENT_CRITOVER: 904 mystr = "critical over"; 905 PENVSYS_SHOWSTATE(mystr); 906 break; 907 case PENVSYS_EVENT_CRITUNDER: 908 mystr = "critical under"; 909 PENVSYS_SHOWSTATE(mystr); 910 break; 911 case PENVSYS_EVENT_WARNOVER: 912 mystr = "warning over"; 913 PENVSYS_SHOWSTATE(mystr); 914 break; 915 case PENVSYS_EVENT_WARNUNDER: 916 mystr = "warning under"; 917 PENVSYS_SHOWSTATE(mystr); 918 break; 919 case PENVSYS_EVENT_NORMAL: 920 printf("%s: normal state on '%s'\n", 921 pes->pes_dvname, pes->pes_sensname); 922 break; 923 default: 924 printf("%s: unknown event\n", __func__); 925 } 926 break; 927 case PENVSYS_TYPE_DRIVE: 928 switch (event) { 929 case PENVSYS_EVENT_STATE_CHANGED: 930 printf("%s: state changed on '%s' to '%s'\n", 931 pes->pes_dvname, pes->pes_sensname, 932 pes->pes_statedesc); 933 break; 934 case PENVSYS_EVENT_NORMAL: 935 printf("%s: normal state on '%s' (%s)\n", 936 pes->pes_dvname, pes->pes_sensname, 937 pes->pes_statedesc); 938 break; 939 } 940 break; 941 default: 942 printf("%s: unknown power type\n", __func__); 943 break; 944 } 945 } 946 947 /* 948 * sysmon_pswitch_register: 949 * 950 * Register a power switch device. 951 */ 952 int 953 sysmon_pswitch_register(struct sysmon_pswitch *smpsw) 954 { 955 (void)RUN_ONCE(&once_power, power_preinit); 956 957 return 0; 958 } 959 960 /* 961 * sysmon_pswitch_unregister: 962 * 963 * Unregister a power switch device. 964 */ 965 void 966 sysmon_pswitch_unregister(struct sysmon_pswitch *smpsw) 967 { 968 /* nada */ 969 } 970 971 /* 972 * sysmon_pswitch_event: 973 * 974 * Register an event on a power switch device. 975 */ 976 void 977 sysmon_pswitch_event(struct sysmon_pswitch *smpsw, int event) 978 { 979 struct power_event_dictionary *ped = NULL; 980 981 KASSERT(smpsw != NULL); 982 983 /* 984 * For pnp specific events, we don't care if the power daemon 985 * is running or not 986 */ 987 if (smpsw->smpsw_type == PSWITCH_TYPE_LID) { 988 switch (event) { 989 case PSWITCH_EVENT_PRESSED: 990 pmf_event_inject(NULL, PMFE_CHASSIS_LID_CLOSE); 991 break; 992 case PSWITCH_EVENT_RELEASED: 993 pmf_event_inject(NULL, PMFE_CHASSIS_LID_OPEN); 994 break; 995 default: 996 break; 997 } 998 } 999 1000 if (sysmon_power_daemon != NULL) { 1001 /* 1002 * Create a new dictionary for the event. 1003 */ 1004 ped = kmem_zalloc(sizeof(*ped), KM_NOSLEEP); 1005 if (!ped) 1006 return; 1007 ped->dict = prop_dictionary_create(); 1008 1009 if (sysmon_power_daemon_task(ped, smpsw, event) == 0) 1010 return; 1011 /* We failed */ 1012 prop_object_release(ped->dict); 1013 kmem_free(ped, sizeof(*ped)); 1014 } 1015 1016 switch (smpsw->smpsw_type) { 1017 case PSWITCH_TYPE_POWER: 1018 if (event != PSWITCH_EVENT_PRESSED) { 1019 /* just ignore it */ 1020 return; 1021 } 1022 1023 /* 1024 * Attempt a somewhat graceful shutdown of the system, 1025 * as if the user has issued a reboot(2) call with 1026 * RB_POWERDOWN. 1027 */ 1028 printf("%s: power button pressed, shutting down!\n", 1029 smpsw->smpsw_name); 1030 kern_reboot(RB_POWERDOWN, NULL); 1031 break; 1032 1033 case PSWITCH_TYPE_RESET: 1034 if (event != PSWITCH_EVENT_PRESSED) { 1035 /* just ignore it */ 1036 return; 1037 } 1038 1039 /* 1040 * Attempt a somewhat graceful reboot of the system, 1041 * as if the user had issued a reboot(2) call. 1042 */ 1043 printf("%s: reset button pressed, rebooting!\n", 1044 smpsw->smpsw_name); 1045 kern_reboot(0, NULL); 1046 break; 1047 1048 case PSWITCH_TYPE_SLEEP: 1049 if (event != PSWITCH_EVENT_PRESSED) { 1050 /* just ignore it */ 1051 return; 1052 } 1053 1054 /* 1055 * Try to enter a "sleep" state. 1056 */ 1057 /* XXX */ 1058 printf("%s: sleep button pressed.\n", smpsw->smpsw_name); 1059 break; 1060 1061 case PSWITCH_TYPE_HOTKEY: 1062 /* 1063 * Eat up the event, there's nothing we can do 1064 */ 1065 break; 1066 1067 case PSWITCH_TYPE_LID: 1068 switch (event) { 1069 case PSWITCH_EVENT_PRESSED: 1070 /* 1071 * Try to enter a "standby" state. 1072 */ 1073 /* XXX */ 1074 printf("%s: lid closed.\n", smpsw->smpsw_name); 1075 break; 1076 1077 case PSWITCH_EVENT_RELEASED: 1078 /* 1079 * Come out of "standby" state. 1080 */ 1081 /* XXX */ 1082 printf("%s: lid opened.\n", smpsw->smpsw_name); 1083 break; 1084 1085 default: 1086 printf("%s: unknown lid switch event: %d\n", 1087 smpsw->smpsw_name, event); 1088 } 1089 break; 1090 1091 case PSWITCH_TYPE_ACADAPTER: 1092 switch (event) { 1093 case PSWITCH_EVENT_PRESSED: 1094 /* 1095 * Come out of power-save state. 1096 */ 1097 aprint_normal("%s: AC adapter online.\n", 1098 smpsw->smpsw_name); 1099 break; 1100 1101 case PSWITCH_EVENT_RELEASED: 1102 /* 1103 * Try to enter a power-save state. 1104 */ 1105 aprint_normal("%s: AC adapter offline.\n", 1106 smpsw->smpsw_name); 1107 break; 1108 } 1109 break; 1110 1111 } 1112 } 1113 1114 static 1115 int 1116 sysmon_power_modcmd(modcmd_t cmd, void *arg) 1117 { 1118 int ret; 1119 1120 switch (cmd) { 1121 case MODULE_CMD_INIT: 1122 ret = sysmon_power_init(); 1123 break; 1124 1125 case MODULE_CMD_FINI: 1126 ret = sysmon_power_fini(); 1127 break; 1128 1129 case MODULE_CMD_STAT: 1130 default: 1131 ret = ENOTTY; 1132 } 1133 1134 return ret; 1135 } 1136 1137