1 /* $NetBSD: sysmon_power.c,v 1.64 2020/06/11 02:39:31 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.64 2020/06/11 02:39:31 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 SLIST_REMOVE(&sysmon_power_event_queue_selinfo.sel_klist, 542 kn, knote, kn_selnext); 543 mutex_exit(&sysmon_power_event_queue_mtx); 544 } 545 546 static int 547 filt_sysmon_power_read(struct knote *kn, long hint) 548 { 549 550 mutex_enter(&sysmon_power_event_queue_mtx); 551 kn->kn_data = sysmon_power_event_queue_count; 552 mutex_exit(&sysmon_power_event_queue_mtx); 553 554 return kn->kn_data > 0; 555 } 556 557 static const struct filterops sysmon_power_read_filtops = { 558 .f_isfd = 1, 559 .f_attach = NULL, 560 .f_detach = filt_sysmon_power_rdetach, 561 .f_event = filt_sysmon_power_read, 562 }; 563 564 static const struct filterops sysmon_power_write_filtops = { 565 .f_isfd = 1, 566 .f_attach = NULL, 567 .f_detach = filt_sysmon_power_rdetach, 568 .f_event = filt_seltrue, 569 }; 570 571 /* 572 * sysmonkqfilter_power: 573 * 574 * Kqueue filter for the system monitor device. 575 */ 576 int 577 sysmonkqfilter_power(dev_t dev, struct knote *kn) 578 { 579 struct klist *klist; 580 581 switch (kn->kn_filter) { 582 case EVFILT_READ: 583 klist = &sysmon_power_event_queue_selinfo.sel_klist; 584 kn->kn_fop = &sysmon_power_read_filtops; 585 break; 586 587 case EVFILT_WRITE: 588 klist = &sysmon_power_event_queue_selinfo.sel_klist; 589 kn->kn_fop = &sysmon_power_write_filtops; 590 break; 591 592 default: 593 return EINVAL; 594 } 595 596 mutex_enter(&sysmon_power_event_queue_mtx); 597 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 598 mutex_exit(&sysmon_power_event_queue_mtx); 599 600 return 0; 601 } 602 603 /* 604 * sysmonioctl_power: 605 * 606 * Perform a power management control request. 607 */ 608 int 609 sysmonioctl_power(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 610 { 611 int error = 0; 612 613 switch (cmd) { 614 case POWER_IOC_GET_TYPE: 615 case POWER_IOC_GET_TYPE_WITH_LOSSAGE: 616 { 617 struct power_type *power_type = (void *) data; 618 619 (void)strlcpy(power_type->power_type, 620 sysmon_power_type, 621 sizeof(power_type->power_type)); 622 break; 623 } 624 case POWER_EVENT_RECVDICT: 625 { 626 struct plistref *plist = (struct plistref *)data; 627 struct power_event_dictionary *ped; 628 629 /* 630 * Get the first dictionary enqueued and mark it 631 * as busy. 632 */ 633 mutex_enter(&sysmon_power_event_queue_mtx); 634 ped = SIMPLEQ_FIRST(&pev_dict_list); 635 if (!ped || !ped->dict) { 636 mutex_exit(&sysmon_power_event_queue_mtx); 637 error = ENOTSUP; 638 break; 639 } 640 641 if ((ped->flags & SYSMON_POWER_DICTIONARY_READY) == 0) { 642 mutex_exit(&sysmon_power_event_queue_mtx); 643 error = EINVAL; 644 break; 645 } 646 647 if (ped->flags & SYSMON_POWER_DICTIONARY_BUSY) { 648 mutex_exit(&sysmon_power_event_queue_mtx); 649 error = EBUSY; 650 break; 651 } 652 653 ped->flags |= SYSMON_POWER_DICTIONARY_BUSY; 654 mutex_exit(&sysmon_power_event_queue_mtx); 655 656 /* 657 * Send it now. 658 */ 659 error = prop_dictionary_copyout_ioctl(plist, 660 cmd, 661 ped->dict); 662 663 /* 664 * Remove the dictionary now that we don't need it. 665 */ 666 mutex_enter(&sysmon_power_event_queue_mtx); 667 ped->flags &= ~SYSMON_POWER_DICTIONARY_BUSY; 668 ped->flags &= ~SYSMON_POWER_DICTIONARY_READY; 669 SIMPLEQ_REMOVE_HEAD(&pev_dict_list, pev_dict_head); 670 mutex_exit(&sysmon_power_event_queue_mtx); 671 sysmon_power_destroy_dictionary(ped); 672 673 break; 674 } 675 default: 676 error = ENOTTY; 677 } 678 679 return error; 680 } 681 682 /* 683 * sysmon_power_make_dictionary: 684 * 685 * Adds the properties for an event in a dictionary. 686 */ 687 int 688 sysmon_power_make_dictionary(prop_dictionary_t dict, void *power_data, 689 int event, int type) 690 { 691 int i; 692 693 KASSERT(mutex_owned(&sysmon_power_event_queue_mtx)); 694 695 switch (type) { 696 /* 697 * create the dictionary for a power switch event. 698 */ 699 case POWER_EVENT_SWITCH_STATE_CHANGE: 700 { 701 const struct power_event_description *peevent = 702 pswitch_event_desc; 703 const struct power_event_description *petype = 704 pswitch_type_desc; 705 struct sysmon_pswitch *smpsw = 706 (struct sysmon_pswitch *)power_data; 707 const char *pwrtype = "pswitch"; 708 709 #define SETPROP(key, str) \ 710 do { \ 711 if ((str) != NULL && !prop_dictionary_set_string(dict, \ 712 (key), \ 713 (str))) { \ 714 printf("%s: failed to set %s\n", __func__, (str)); \ 715 return EINVAL; \ 716 } \ 717 } while (/* CONSTCOND */ 0) 718 719 720 SETPROP("driver-name", smpsw->smpsw_name); 721 722 for (i = 0; peevent[i].type != -1; i++) 723 if (peevent[i].type == event) 724 break; 725 726 SETPROP("powerd-event-name", peevent[i].desc); 727 728 for (i = 0; petype[i].type != -1; i++) 729 if (petype[i].type == smpsw->smpsw_type) 730 break; 731 732 SETPROP("powerd-script-name", petype[i].desc); 733 SETPROP("power-type", pwrtype); 734 break; 735 } 736 /* 737 * create a dictionary for power envsys event. 738 */ 739 case POWER_EVENT_ENVSYS_STATE_CHANGE: 740 { 741 const struct power_event_description *peevent = 742 penvsys_event_desc; 743 const struct power_event_description *petype = 744 penvsys_type_desc; 745 struct penvsys_state *pes = 746 (struct penvsys_state *)power_data; 747 const char *pwrtype = "envsys"; 748 749 SETPROP("driver-name", pes->pes_dvname); 750 SETPROP("sensor-name", pes->pes_sensname); 751 SETPROP("state-description", pes->pes_statedesc); 752 753 for (i = 0; peevent[i].type != -1; i++) 754 if (peevent[i].type == event) 755 break; 756 757 SETPROP("powerd-event-name", peevent[i].desc); 758 759 for (i = 0; petype[i].type != -1; i++) 760 if (petype[i].type == pes->pes_type) 761 break; 762 763 SETPROP("powerd-script-name", petype[i].desc); 764 SETPROP("power-type", pwrtype); 765 break; 766 } 767 default: 768 return ENOTSUP; 769 } 770 771 return 0; 772 } 773 774 /* 775 * sysmon_power_destroy_dictionary: 776 * 777 * Destroys a power_event_dictionary object and all its 778 * properties in the dictionary. 779 */ 780 static void 781 sysmon_power_destroy_dictionary(struct power_event_dictionary *ped) 782 { 783 prop_object_iterator_t iter; 784 prop_object_t obj; 785 786 KASSERT(ped != NULL); 787 KASSERT((ped->flags & SYSMON_POWER_DICTIONARY_BUSY) == 0); 788 789 iter = prop_dictionary_iterator(ped->dict); 790 if (iter == NULL) 791 return; 792 793 while ((obj = prop_object_iterator_next(iter)) != NULL) { 794 prop_dictionary_remove(ped->dict, 795 prop_dictionary_keysym_value(obj)); 796 prop_object_iterator_reset(iter); 797 } 798 799 prop_object_iterator_release(iter); 800 prop_object_release(ped->dict); 801 802 kmem_free(ped, sizeof(*ped)); 803 } 804 805 /* 806 * sysmon_power_settype: 807 * 808 * Sets the back-end power management type. This information can 809 * be used by the power management daemon. 810 */ 811 void 812 sysmon_power_settype(const char *type) 813 { 814 815 /* 816 * Don't bother locking this; it's going to be set 817 * during autoconfiguration, and then only read from 818 * then on. 819 */ 820 (void)strlcpy(sysmon_power_type, type, sizeof(sysmon_power_type)); 821 } 822 823 #define PENVSYS_SHOWSTATE(str) \ 824 do { \ 825 printf("%s: %s limit on '%s'\n", \ 826 pes->pes_dvname, (str), pes->pes_sensname); \ 827 } while (/* CONSTCOND */ 0) 828 829 /* 830 * sysmon_penvsys_event: 831 * 832 * Puts an event onto the sysmon power queue and sends the 833 * appropriate event if the daemon is running, otherwise a 834 * message is shown. 835 */ 836 void 837 sysmon_penvsys_event(struct penvsys_state *pes, int event) 838 { 839 struct power_event_dictionary *ped; 840 const char *mystr = NULL; 841 842 KASSERT(pes != NULL); 843 844 rnd_add_uint32(&sysmon_rndsource, pes->pes_type); 845 846 if (sysmon_power_daemon != NULL) { 847 /* 848 * Create a dictionary for the new event. 849 */ 850 ped = kmem_zalloc(sizeof(*ped), KM_NOSLEEP); 851 if (!ped) 852 return; 853 ped->dict = prop_dictionary_create(); 854 855 if (sysmon_power_daemon_task(ped, pes, event) == 0) 856 return; 857 /* We failed */ 858 prop_object_release(ped->dict); 859 kmem_free(ped, sizeof(*ped)); 860 } 861 862 switch (pes->pes_type) { 863 case PENVSYS_TYPE_BATTERY: 864 switch (event) { 865 case PENVSYS_EVENT_LOW_POWER: 866 printf("sysmon: LOW POWER! SHUTTING DOWN.\n"); 867 kern_reboot(RB_POWERDOWN, NULL); 868 break; 869 case PENVSYS_EVENT_STATE_CHANGED: 870 printf("%s: state changed on '%s' to '%s'\n", 871 pes->pes_dvname, pes->pes_sensname, 872 pes->pes_statedesc); 873 break; 874 case PENVSYS_EVENT_BATT_CRIT: 875 mystr = "critical capacity"; 876 PENVSYS_SHOWSTATE(mystr); 877 break; 878 case PENVSYS_EVENT_BATT_WARN: 879 mystr = "warning capacity"; 880 PENVSYS_SHOWSTATE(mystr); 881 break; 882 case PENVSYS_EVENT_BATT_HIGH: 883 mystr = "high capacity"; 884 PENVSYS_SHOWSTATE(mystr); 885 break; 886 case PENVSYS_EVENT_BATT_MAX: 887 mystr = "maximum capacity"; 888 PENVSYS_SHOWSTATE(mystr); 889 break; 890 case PENVSYS_EVENT_NORMAL: 891 printf("%s: normal capacity on '%s'\n", 892 pes->pes_dvname, pes->pes_sensname); 893 break; 894 } 895 break; 896 case PENVSYS_TYPE_FAN: 897 case PENVSYS_TYPE_INDICATOR: 898 case PENVSYS_TYPE_TEMP: 899 case PENVSYS_TYPE_POWER: 900 case PENVSYS_TYPE_RESISTANCE: 901 case PENVSYS_TYPE_VOLTAGE: 902 switch (event) { 903 case PENVSYS_EVENT_CRITICAL: 904 mystr = "critical"; 905 PENVSYS_SHOWSTATE(mystr); 906 break; 907 case PENVSYS_EVENT_CRITOVER: 908 mystr = "critical over"; 909 PENVSYS_SHOWSTATE(mystr); 910 break; 911 case PENVSYS_EVENT_CRITUNDER: 912 mystr = "critical under"; 913 PENVSYS_SHOWSTATE(mystr); 914 break; 915 case PENVSYS_EVENT_WARNOVER: 916 mystr = "warning over"; 917 PENVSYS_SHOWSTATE(mystr); 918 break; 919 case PENVSYS_EVENT_WARNUNDER: 920 mystr = "warning under"; 921 PENVSYS_SHOWSTATE(mystr); 922 break; 923 case PENVSYS_EVENT_NORMAL: 924 printf("%s: normal state on '%s'\n", 925 pes->pes_dvname, pes->pes_sensname); 926 break; 927 default: 928 printf("%s: unknown event\n", __func__); 929 } 930 break; 931 case PENVSYS_TYPE_DRIVE: 932 switch (event) { 933 case PENVSYS_EVENT_STATE_CHANGED: 934 printf("%s: state changed on '%s' to '%s'\n", 935 pes->pes_dvname, pes->pes_sensname, 936 pes->pes_statedesc); 937 break; 938 case PENVSYS_EVENT_NORMAL: 939 printf("%s: normal state on '%s' (%s)\n", 940 pes->pes_dvname, pes->pes_sensname, 941 pes->pes_statedesc); 942 break; 943 } 944 break; 945 default: 946 printf("%s: unknown power type\n", __func__); 947 break; 948 } 949 } 950 951 /* 952 * sysmon_pswitch_register: 953 * 954 * Register a power switch device. 955 */ 956 int 957 sysmon_pswitch_register(struct sysmon_pswitch *smpsw) 958 { 959 (void)RUN_ONCE(&once_power, power_preinit); 960 961 return 0; 962 } 963 964 /* 965 * sysmon_pswitch_unregister: 966 * 967 * Unregister a power switch device. 968 */ 969 void 970 sysmon_pswitch_unregister(struct sysmon_pswitch *smpsw) 971 { 972 /* nada */ 973 } 974 975 /* 976 * sysmon_pswitch_event: 977 * 978 * Register an event on a power switch device. 979 */ 980 void 981 sysmon_pswitch_event(struct sysmon_pswitch *smpsw, int event) 982 { 983 struct power_event_dictionary *ped = NULL; 984 985 KASSERT(smpsw != NULL); 986 987 /* 988 * For pnp specific events, we don't care if the power daemon 989 * is running or not 990 */ 991 if (smpsw->smpsw_type == PSWITCH_TYPE_LID) { 992 switch (event) { 993 case PSWITCH_EVENT_PRESSED: 994 pmf_event_inject(NULL, PMFE_CHASSIS_LID_CLOSE); 995 break; 996 case PSWITCH_EVENT_RELEASED: 997 pmf_event_inject(NULL, PMFE_CHASSIS_LID_OPEN); 998 break; 999 default: 1000 break; 1001 } 1002 } 1003 1004 if (sysmon_power_daemon != NULL) { 1005 /* 1006 * Create a new dictionary for the event. 1007 */ 1008 ped = kmem_zalloc(sizeof(*ped), KM_NOSLEEP); 1009 if (!ped) 1010 return; 1011 ped->dict = prop_dictionary_create(); 1012 1013 if (sysmon_power_daemon_task(ped, smpsw, event) == 0) 1014 return; 1015 /* We failed */ 1016 prop_object_release(ped->dict); 1017 kmem_free(ped, sizeof(*ped)); 1018 } 1019 1020 switch (smpsw->smpsw_type) { 1021 case PSWITCH_TYPE_POWER: 1022 if (event != PSWITCH_EVENT_PRESSED) { 1023 /* just ignore it */ 1024 return; 1025 } 1026 1027 /* 1028 * Attempt a somewhat graceful shutdown of the system, 1029 * as if the user has issued a reboot(2) call with 1030 * RB_POWERDOWN. 1031 */ 1032 printf("%s: power button pressed, shutting down!\n", 1033 smpsw->smpsw_name); 1034 kern_reboot(RB_POWERDOWN, NULL); 1035 break; 1036 1037 case PSWITCH_TYPE_RESET: 1038 if (event != PSWITCH_EVENT_PRESSED) { 1039 /* just ignore it */ 1040 return; 1041 } 1042 1043 /* 1044 * Attempt a somewhat graceful reboot of the system, 1045 * as if the user had issued a reboot(2) call. 1046 */ 1047 printf("%s: reset button pressed, rebooting!\n", 1048 smpsw->smpsw_name); 1049 kern_reboot(0, NULL); 1050 break; 1051 1052 case PSWITCH_TYPE_SLEEP: 1053 if (event != PSWITCH_EVENT_PRESSED) { 1054 /* just ignore it */ 1055 return; 1056 } 1057 1058 /* 1059 * Try to enter a "sleep" state. 1060 */ 1061 /* XXX */ 1062 printf("%s: sleep button pressed.\n", smpsw->smpsw_name); 1063 break; 1064 1065 case PSWITCH_TYPE_HOTKEY: 1066 /* 1067 * Eat up the event, there's nothing we can do 1068 */ 1069 break; 1070 1071 case PSWITCH_TYPE_LID: 1072 switch (event) { 1073 case PSWITCH_EVENT_PRESSED: 1074 /* 1075 * Try to enter a "standby" state. 1076 */ 1077 /* XXX */ 1078 printf("%s: lid closed.\n", smpsw->smpsw_name); 1079 break; 1080 1081 case PSWITCH_EVENT_RELEASED: 1082 /* 1083 * Come out of "standby" state. 1084 */ 1085 /* XXX */ 1086 printf("%s: lid opened.\n", smpsw->smpsw_name); 1087 break; 1088 1089 default: 1090 printf("%s: unknown lid switch event: %d\n", 1091 smpsw->smpsw_name, event); 1092 } 1093 break; 1094 1095 case PSWITCH_TYPE_ACADAPTER: 1096 switch (event) { 1097 case PSWITCH_EVENT_PRESSED: 1098 /* 1099 * Come out of power-save state. 1100 */ 1101 aprint_normal("%s: AC adapter online.\n", 1102 smpsw->smpsw_name); 1103 break; 1104 1105 case PSWITCH_EVENT_RELEASED: 1106 /* 1107 * Try to enter a power-save state. 1108 */ 1109 aprint_normal("%s: AC adapter offline.\n", 1110 smpsw->smpsw_name); 1111 break; 1112 } 1113 break; 1114 1115 } 1116 } 1117 1118 static 1119 int 1120 sysmon_power_modcmd(modcmd_t cmd, void *arg) 1121 { 1122 int ret; 1123 1124 switch (cmd) { 1125 case MODULE_CMD_INIT: 1126 ret = sysmon_power_init(); 1127 break; 1128 1129 case MODULE_CMD_FINI: 1130 ret = sysmon_power_fini(); 1131 break; 1132 1133 case MODULE_CMD_STAT: 1134 default: 1135 ret = ENOTTY; 1136 } 1137 1138 return ret; 1139 } 1140 1141