1 /*- 2 * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org> 3 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 4 * Copyright (c) 2000, 2001 Michael Smith 5 * Copyright (c) 2000 BSDi 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 #include "opt_acpi.h" 33 #include <sys/param.h> 34 #include <sys/kernel.h> 35 #include <sys/proc.h> 36 #include <sys/fcntl.h> 37 #include <sys/malloc.h> 38 #include <sys/bus.h> 39 #include <sys/conf.h> 40 #include <sys/ioccom.h> 41 #include <sys/reboot.h> 42 #include <sys/sysctl.h> 43 #include <sys/ctype.h> 44 #include <sys/linker.h> 45 #include <sys/power.h> 46 47 #include <machine/clock.h> 48 #include <machine/resource.h> 49 50 #include <isa/isavar.h> 51 52 #include "acpi.h" 53 54 #include <dev/acpica/acpica_support.h> 55 56 #include <dev/acpica/acpivar.h> 57 #include <dev/acpica/acpiio.h> 58 59 MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 60 61 /* 62 * Hooks for the ACPI CA debugging infrastructure 63 */ 64 #define _COMPONENT ACPI_BUS 65 ACPI_MODULE_NAME("ACPI") 66 67 /* 68 * Character device 69 */ 70 71 static d_open_t acpiopen; 72 static d_close_t acpiclose; 73 static d_ioctl_t acpiioctl; 74 75 #define CDEV_MAJOR 152 76 static struct cdevsw acpi_cdevsw = { 77 .d_open = acpiopen, 78 .d_close = acpiclose, 79 .d_ioctl = acpiioctl, 80 .d_name = "acpi", 81 .d_maj = CDEV_MAJOR, 82 }; 83 84 static const char* sleep_state_names[] = { 85 "S0", "S1", "S2", "S3", "S4", "S5", "NONE"}; 86 87 /* this has to be static, as the softc is gone when we need it */ 88 static int acpi_off_state = ACPI_STATE_S5; 89 90 #if __FreeBSD_version >= 500000 91 struct mtx acpi_mutex; 92 #endif 93 94 static int acpi_modevent(struct module *mod, int event, void *junk); 95 static void acpi_identify(driver_t *driver, device_t parent); 96 static int acpi_probe(device_t dev); 97 static int acpi_attach(device_t dev); 98 static device_t acpi_add_child(device_t bus, int order, const char *name, int unit); 99 static int acpi_print_child(device_t bus, device_t child); 100 static int acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result); 101 static int acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value); 102 static int acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, 103 u_long count); 104 static int acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, 105 u_long *countp); 106 static struct resource *acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 107 u_long start, u_long end, u_long count, u_int flags); 108 static int acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r); 109 static u_int32_t acpi_isa_get_logicalid(device_t dev); 110 static u_int32_t acpi_isa_get_compatid(device_t dev); 111 static int acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids); 112 113 static void acpi_probe_children(device_t bus); 114 static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status); 115 116 static void acpi_shutdown_pre_sync(void *arg, int howto); 117 static void acpi_shutdown_final(void *arg, int howto); 118 119 static void acpi_enable_fixed_events(struct acpi_softc *sc); 120 121 static void acpi_system_eventhandler_sleep(void *arg, int state); 122 static void acpi_system_eventhandler_wakeup(void *arg, int state); 123 static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 124 static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 125 126 static int acpi_pm_func(u_long cmd, void *arg, ...); 127 128 static device_method_t acpi_methods[] = { 129 /* Device interface */ 130 DEVMETHOD(device_identify, acpi_identify), 131 DEVMETHOD(device_probe, acpi_probe), 132 DEVMETHOD(device_attach, acpi_attach), 133 DEVMETHOD(device_shutdown, bus_generic_shutdown), 134 DEVMETHOD(device_suspend, bus_generic_suspend), 135 DEVMETHOD(device_resume, bus_generic_resume), 136 137 /* Bus interface */ 138 DEVMETHOD(bus_add_child, acpi_add_child), 139 DEVMETHOD(bus_print_child, acpi_print_child), 140 DEVMETHOD(bus_read_ivar, acpi_read_ivar), 141 DEVMETHOD(bus_write_ivar, acpi_write_ivar), 142 DEVMETHOD(bus_set_resource, acpi_set_resource), 143 DEVMETHOD(bus_get_resource, acpi_get_resource), 144 DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 145 DEVMETHOD(bus_release_resource, acpi_release_resource), 146 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 147 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 148 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 149 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 150 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 151 152 /* ISA emulation */ 153 DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe), 154 155 {0, 0} 156 }; 157 158 static driver_t acpi_driver = { 159 "acpi", 160 acpi_methods, 161 sizeof(struct acpi_softc), 162 }; 163 164 static devclass_t acpi_devclass; 165 DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 166 MODULE_VERSION(acpi, 100); 167 168 SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, ""); 169 SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, ""); 170 static int acpi_ca_version = ACPI_CA_VERSION; 171 SYSCTL_INT(_debug, OID_AUTO, acpi_ca_version, CTLFLAG_RD, &acpi_ca_version, 0, ""); 172 173 /* 174 * ACPI can only be loaded as a module by the loader; activating it after 175 * system bootstrap time is not useful, and can be fatal to the system. 176 * It also cannot be unloaded, since the entire system bus heirarchy hangs off it. 177 */ 178 static int 179 acpi_modevent(struct module *mod, int event, void *junk) 180 { 181 switch(event) { 182 case MOD_LOAD: 183 if (!cold) { 184 printf("The ACPI driver cannot be loaded after boot.\n"); 185 return(EPERM); 186 } 187 break; 188 case MOD_UNLOAD: 189 if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 190 return(EBUSY); 191 break; 192 default: 193 break; 194 } 195 return(0); 196 } 197 198 /* 199 * Detect ACPI, perform early initialisation 200 */ 201 static void 202 acpi_identify(driver_t *driver, device_t parent) 203 { 204 device_t child; 205 int error; 206 #ifdef ACPI_DEBUGGER 207 char *debugpoint; 208 #endif 209 210 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 211 212 if (!cold) 213 return_VOID; 214 215 /* 216 * Check that we haven't been disabled with a hint. 217 */ 218 if (resource_disabled("acpi", 0)) 219 return_VOID; 220 221 /* 222 * Make sure we're not being doubly invoked. 223 */ 224 if (device_find_child(parent, "acpi", 0) != NULL) 225 return_VOID; 226 227 #if __FreeBSD_version >= 500000 228 /* initialise the ACPI mutex */ 229 mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 230 #endif 231 232 /* 233 * Start up the ACPI CA subsystem. 234 */ 235 #ifdef ACPI_DEBUGGER 236 debugpoint = getenv("debug.acpi.debugger"); 237 if (debugpoint) { 238 if (!strcmp(debugpoint, "init")) 239 acpi_EnterDebugger(); 240 freeenv(debugpoint); 241 } 242 #endif 243 if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) { 244 printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error)); 245 return_VOID; 246 } 247 #ifdef ACPI_DEBUGGER 248 debugpoint = getenv("debug.acpi.debugger"); 249 if (debugpoint) { 250 if (!strcmp(debugpoint, "tables")) 251 acpi_EnterDebugger(); 252 freeenv(debugpoint); 253 } 254 #endif 255 256 if (ACPI_FAILURE(error = AcpiLoadTables())) { 257 printf("ACPI: table load failed: %s\n", AcpiFormatException(error)); 258 return_VOID; 259 } 260 261 /* 262 * Attach the actual ACPI device. 263 */ 264 if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) { 265 device_printf(parent, "ACPI: could not attach\n"); 266 return_VOID; 267 } 268 } 269 270 /* 271 * Fetch some descriptive data from ACPI to put in our attach message 272 */ 273 static int 274 acpi_probe(device_t dev) 275 { 276 ACPI_TABLE_HEADER th; 277 char buf[20]; 278 ACPI_STATUS status; 279 int error; 280 ACPI_LOCK_DECL; 281 282 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 283 284 if (power_pm_get_type() != POWER_PM_TYPE_NONE && 285 power_pm_get_type() != POWER_PM_TYPE_ACPI) { 286 device_printf(dev, "Other PM system enabled.\n"); 287 return_VALUE(ENXIO); 288 } 289 290 ACPI_LOCK; 291 292 if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) { 293 device_printf(dev, "couldn't get XSDT header: %s\n", AcpiFormatException(status)); 294 error = ENXIO; 295 } else { 296 sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId); 297 device_set_desc_copy(dev, buf); 298 error = 0; 299 } 300 ACPI_UNLOCK; 301 return_VALUE(error); 302 } 303 304 static int 305 acpi_attach(device_t dev) 306 { 307 struct acpi_softc *sc; 308 ACPI_STATUS status; 309 int error; 310 UINT32 flags; 311 char *env; 312 #ifdef ACPI_DEBUGGER 313 char *debugpoint; 314 #endif 315 ACPI_LOCK_DECL; 316 317 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 318 ACPI_LOCK; 319 sc = device_get_softc(dev); 320 bzero(sc, sizeof(*sc)); 321 sc->acpi_dev = dev; 322 323 #ifdef ACPI_DEBUGGER 324 debugpoint = getenv("debug.acpi.debugger"); 325 if (debugpoint) { 326 if (!strcmp(debugpoint, "spaces")) 327 acpi_EnterDebugger(); 328 freeenv(debugpoint); 329 } 330 #endif 331 332 /* 333 * Install the default address space handlers. 334 */ 335 error = ENXIO; 336 if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 337 ACPI_ADR_SPACE_SYSTEM_MEMORY, 338 ACPI_DEFAULT_HANDLER, 339 NULL, NULL))) { 340 device_printf(dev, "could not initialise SystemMemory handler: %s\n", AcpiFormatException(status)); 341 goto out; 342 } 343 if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 344 ACPI_ADR_SPACE_SYSTEM_IO, 345 ACPI_DEFAULT_HANDLER, 346 NULL, NULL))) { 347 device_printf(dev, "could not initialise SystemIO handler: %s\n", AcpiFormatException(status)); 348 goto out; 349 } 350 if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 351 ACPI_ADR_SPACE_PCI_CONFIG, 352 ACPI_DEFAULT_HANDLER, 353 NULL, NULL))) { 354 device_printf(dev, "could not initialise PciConfig handler: %s\n", AcpiFormatException(status)); 355 goto out; 356 } 357 358 /* 359 * Bring ACPI fully online. 360 * 361 * Note that some systems (specifically, those with namespace evaluation issues 362 * that require the avoidance of parts of the namespace) must avoid running _INI 363 * and _STA on everything, as well as dodging the final object init pass. 364 * 365 * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 366 * 367 * XXX We should arrange for the object init pass after we have attached all our 368 * child devices, but on many systems it works here. 369 */ 370 #ifdef ACPI_DEBUGGER 371 debugpoint = getenv("debug.acpi.debugger"); 372 if (debugpoint) { 373 if (!strcmp(debugpoint, "enable")) 374 acpi_EnterDebugger(); 375 freeenv(debugpoint); 376 } 377 #endif 378 flags = 0; 379 if (testenv("debug.acpi.avoid")) 380 flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 381 if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 382 device_printf(dev, "could not enable ACPI: %s\n", AcpiFormatException(status)); 383 goto out; 384 } 385 386 if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 387 device_printf(dev, "could not initialize ACPI objects: %s\n", AcpiFormatException(status)); 388 goto out; 389 } 390 391 /* 392 * Setup our sysctl tree. 393 * 394 * XXX: This doesn't check to make sure that none of these fail. 395 */ 396 sysctl_ctx_init(&sc->acpi_sysctl_ctx); 397 sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 398 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 399 device_get_name(dev), CTLFLAG_RD, 0, ""); 400 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 401 OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD, 402 0, 0, acpi_supported_sleep_state_sysctl, "A", ""); 403 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 404 OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 405 &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 406 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 407 OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 408 &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 409 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 410 OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 411 &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 412 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 413 OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 414 &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 415 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 416 OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 417 &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 418 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 419 OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW, 420 &sc->acpi_sleep_delay, 0, "sleep delay"); 421 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 422 OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW, 423 &sc->acpi_s4bios, 0, "S4BIOS mode"); 424 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 425 OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW, 426 &sc->acpi_verbose, 0, "verbose mode"); 427 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 428 OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW, 429 &sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff"); 430 sc->acpi_disable_on_poweroff = 1; 431 sc->acpi_sleep_delay = 0; 432 sc->acpi_s4bios = 1; 433 if (bootverbose) 434 sc->acpi_verbose = 1; 435 if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) { 436 sc->acpi_verbose = 1; 437 freeenv(env); 438 } 439 440 /* 441 * Dispatch the default sleep state to devices. 442 * TBD: should be configured from userland policy manager. 443 */ 444 sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX; 445 sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX; 446 sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX; 447 sc->acpi_standby_sx = ACPI_STATE_S1; 448 sc->acpi_suspend_sx = ACPI_STATE_S3; 449 450 acpi_enable_fixed_events(sc); 451 452 /* 453 * Scan the namespace and attach/initialise children. 454 */ 455 #ifdef ACPI_DEBUGGER 456 debugpoint = getenv("debug.acpi.debugger"); 457 if (debugpoint) { 458 if (!strcmp(debugpoint, "probe")) 459 acpi_EnterDebugger(); 460 freeenv(debugpoint); 461 } 462 #endif 463 464 /* 465 * Register our shutdown handlers 466 */ 467 EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST); 468 EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST); 469 470 /* 471 * Register our acpi event handlers. 472 * XXX should be configurable eg. via userland policy manager. 473 */ 474 EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST); 475 EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST); 476 477 /* 478 * Flag our initial states. 479 */ 480 sc->acpi_enabled = 1; 481 sc->acpi_sstate = ACPI_STATE_S0; 482 sc->acpi_sleep_disabled = 0; 483 484 /* 485 * Create the control device 486 */ 487 sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644, 488 "acpi"); 489 sc->acpi_dev_t->si_drv1 = sc; 490 491 #ifdef ACPI_DEBUGGER 492 debugpoint = getenv("debug.acpi.debugger"); 493 if (debugpoint) { 494 if (!strcmp(debugpoint, "running")) 495 acpi_EnterDebugger(); 496 freeenv(debugpoint); 497 } 498 #endif 499 500 #ifdef ACPI_USE_THREADS 501 if ((error = acpi_task_thread_init())) { 502 goto out; 503 } 504 #endif 505 506 if ((error = acpi_machdep_init(dev))) { 507 goto out; 508 } 509 510 /* Register ACPI again to pass the correct argument of pm_func. */ 511 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 512 513 if (!acpi_disabled("bus")) 514 acpi_probe_children(dev); 515 516 error = 0; 517 518 out: 519 ACPI_UNLOCK; 520 return_VALUE(error); 521 } 522 523 /* 524 * Handle a new device being added 525 */ 526 static device_t 527 acpi_add_child(device_t bus, int order, const char *name, int unit) 528 { 529 struct acpi_device *ad; 530 device_t child; 531 532 if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL) 533 return(NULL); 534 bzero(ad, sizeof(*ad)); 535 536 resource_list_init(&ad->ad_rl); 537 538 child = device_add_child_ordered(bus, order, name, unit); 539 if (child != NULL) 540 device_set_ivars(child, ad); 541 return(child); 542 } 543 544 static int 545 acpi_print_child(device_t bus, device_t child) 546 { 547 struct acpi_device *adev = device_get_ivars(child); 548 struct resource_list *rl = &adev->ad_rl; 549 int retval = 0; 550 551 retval += bus_print_child_header(bus, child); 552 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 553 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 554 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 555 retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 556 retval += bus_print_child_footer(bus, child); 557 558 return(retval); 559 } 560 561 562 /* 563 * Handle per-device ivars 564 */ 565 static int 566 acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 567 { 568 struct acpi_device *ad; 569 570 if ((ad = device_get_ivars(child)) == NULL) { 571 printf("device has no ivars\n"); 572 return(ENOENT); 573 } 574 575 switch(index) { 576 /* ACPI ivars */ 577 case ACPI_IVAR_HANDLE: 578 *(ACPI_HANDLE *)result = ad->ad_handle; 579 break; 580 case ACPI_IVAR_MAGIC: 581 *(int *)result = ad->ad_magic; 582 break; 583 case ACPI_IVAR_PRIVATE: 584 *(void **)result = ad->ad_private; 585 break; 586 587 /* ISA compatibility */ 588 case ISA_IVAR_VENDORID: 589 case ISA_IVAR_SERIAL: 590 case ISA_IVAR_COMPATID: 591 *(int *)result = -1; 592 break; 593 594 case ISA_IVAR_LOGICALID: 595 *(int *)result = acpi_isa_get_logicalid(child); 596 break; 597 598 default: 599 return(ENOENT); 600 } 601 return(0); 602 } 603 604 static int 605 acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 606 { 607 struct acpi_device *ad; 608 609 if ((ad = device_get_ivars(child)) == NULL) { 610 printf("device has no ivars\n"); 611 return(ENOENT); 612 } 613 614 switch(index) { 615 /* ACPI ivars */ 616 case ACPI_IVAR_HANDLE: 617 ad->ad_handle = (ACPI_HANDLE)value; 618 break; 619 case ACPI_IVAR_MAGIC: 620 ad->ad_magic = (int )value; 621 break; 622 case ACPI_IVAR_PRIVATE: 623 ad->ad_private = (void *)value; 624 break; 625 626 default: 627 panic("bad ivar write request (%d)", index); 628 return(ENOENT); 629 } 630 return(0); 631 } 632 633 /* 634 * Handle child resource allocation/removal 635 */ 636 static int 637 acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count) 638 { 639 struct acpi_device *ad = device_get_ivars(child); 640 struct resource_list *rl = &ad->ad_rl; 641 642 resource_list_add(rl, type, rid, start, start + count -1, count); 643 644 return(0); 645 } 646 647 static int 648 acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp) 649 { 650 struct acpi_device *ad = device_get_ivars(child); 651 struct resource_list *rl = &ad->ad_rl; 652 struct resource_list_entry *rle; 653 654 rle = resource_list_find(rl, type, rid); 655 if (!rle) 656 return(ENOENT); 657 658 if (startp) 659 *startp = rle->start; 660 if (countp) 661 *countp = rle->count; 662 663 return(0); 664 } 665 666 static struct resource * 667 acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 668 u_long start, u_long end, u_long count, u_int flags) 669 { 670 struct acpi_device *ad = device_get_ivars(child); 671 struct resource_list *rl = &ad->ad_rl; 672 673 return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags)); 674 } 675 676 static int 677 acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r) 678 { 679 struct acpi_device *ad = device_get_ivars(child); 680 struct resource_list *rl = &ad->ad_rl; 681 682 return(resource_list_release(rl, bus, child, type, rid, r)); 683 } 684 685 /* 686 * Handle ISA-like devices probing for a PnP ID to match. 687 */ 688 #define PNP_EISAID(s) \ 689 ((((s[0] - '@') & 0x1f) << 2) \ 690 | (((s[1] - '@') & 0x18) >> 3) \ 691 | (((s[1] - '@') & 0x07) << 13) \ 692 | (((s[2] - '@') & 0x1f) << 8) \ 693 | (PNP_HEXTONUM(s[4]) << 16) \ 694 | (PNP_HEXTONUM(s[3]) << 20) \ 695 | (PNP_HEXTONUM(s[6]) << 24) \ 696 | (PNP_HEXTONUM(s[5]) << 28)) 697 698 static u_int32_t 699 acpi_isa_get_logicalid(device_t dev) 700 { 701 ACPI_HANDLE h; 702 ACPI_DEVICE_INFO devinfo; 703 ACPI_STATUS error; 704 u_int32_t pnpid; 705 ACPI_LOCK_DECL; 706 707 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 708 709 pnpid = 0; 710 ACPI_LOCK; 711 712 /* fetch and validate the HID */ 713 if ((h = acpi_get_handle(dev)) == NULL) 714 goto out; 715 if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo))) 716 goto out; 717 if (!(devinfo.Valid & ACPI_VALID_HID)) 718 goto out; 719 720 pnpid = PNP_EISAID(devinfo.HardwareId); 721 out: 722 ACPI_UNLOCK; 723 return_VALUE(pnpid); 724 } 725 726 static u_int32_t 727 acpi_isa_get_compatid(device_t dev) 728 { 729 ACPI_HANDLE h; 730 ACPI_DEVICE_INFO devinfo; 731 ACPI_STATUS error; 732 u_int32_t pnpid; 733 ACPI_LOCK_DECL; 734 735 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 736 737 pnpid = 0; 738 ACPI_LOCK; 739 740 /* fetch and validate the HID */ 741 if ((h = acpi_get_handle(dev)) == NULL) 742 goto out; 743 if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo))) 744 goto out; 745 if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &pnpid))) 746 goto out; 747 748 out: 749 ACPI_UNLOCK; 750 return_VALUE(pnpid); 751 } 752 753 754 static int 755 acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 756 { 757 int result; 758 u_int32_t lid, cid; 759 760 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 761 762 /* 763 * ISA-style drivers attached to ACPI may persist and 764 * probe manually if we return ENOENT. We never want 765 * that to happen, so don't ever return it. 766 */ 767 result = ENXIO; 768 769 /* scan the supplied IDs for a match */ 770 lid = acpi_isa_get_logicalid(child); 771 cid = acpi_isa_get_compatid(child); 772 while (ids && ids->ip_id) { 773 if (lid == ids->ip_id || cid == ids->ip_id) { 774 result = 0; 775 goto out; 776 } 777 ids++; 778 } 779 out: 780 return_VALUE(result); 781 } 782 783 /* 784 * Scan relevant portions of the ACPI namespace and attach child devices. 785 * 786 * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and \_SB_ scopes, 787 * and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec. 788 */ 789 static void 790 acpi_probe_children(device_t bus) 791 { 792 ACPI_HANDLE parent; 793 static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL}; 794 int i; 795 796 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 797 ACPI_ASSERTLOCK; 798 799 /* 800 * Create any static children by calling device identify methods. 801 */ 802 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 803 bus_generic_probe(bus); 804 805 /* 806 * Scan the namespace and insert placeholders for all the devices that 807 * we find. 808 * 809 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 810 * we want to create nodes for all devices, not just those that are currently 811 * present. (This assumes that we don't want to create/remove devices as they 812 * appear, which might be smarter.) 813 */ 814 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 815 for (i = 0; scopes[i] != NULL; i++) 816 if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent))) 817 AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL); 818 819 /* 820 * Scan all of the child devices we have created and let them probe/attach. 821 */ 822 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n")); 823 bus_generic_attach(bus); 824 825 /* 826 * Some of these children may have attached others as part of their attach 827 * process (eg. the root PCI bus driver), so rescan. 828 */ 829 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n")); 830 bus_generic_attach(bus); 831 832 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 833 return_VOID; 834 } 835 836 /* 837 * Evaluate a child device and determine whether we might attach a device to 838 * it. 839 */ 840 static ACPI_STATUS 841 acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 842 { 843 ACPI_OBJECT_TYPE type; 844 device_t child, bus = (device_t)context; 845 846 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 847 848 /* 849 * Skip this device if we think we'll have trouble with it. 850 */ 851 if (acpi_avoid(handle)) 852 return_ACPI_STATUS(AE_OK); 853 854 if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 855 switch(type) { 856 case ACPI_TYPE_DEVICE: 857 case ACPI_TYPE_PROCESSOR: 858 case ACPI_TYPE_THERMAL: 859 case ACPI_TYPE_POWER: 860 if (acpi_disabled("children")) 861 break; 862 /* 863 * Create a placeholder device for this node. Sort the placeholder 864 * so that the probe/attach passes will run breadth-first. 865 */ 866 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", acpi_name(handle))); 867 child = BUS_ADD_CHILD(bus, level * 10, NULL, -1); 868 if (child == NULL) 869 break; 870 acpi_set_handle(child, handle); 871 872 /* 873 * Check that the device is present. If it's not present, 874 * leave it disabled (so that we have a device_t attached to 875 * the handle, but we don't probe it). 876 */ 877 if ((type == ACPI_TYPE_DEVICE) && (!acpi_DeviceIsPresent(child))) { 878 device_disable(child); 879 break; 880 } 881 882 /* 883 * Get the device's resource settings and attach them. 884 * Note that if the device has _PRS but no _CRS, we need 885 * to decide when it's appropriate to try to configure the 886 * device. Ignore the return value here; it's OK for the 887 * device not to have any resources. 888 */ 889 acpi_parse_resources(child, handle, &acpi_res_parse_set); 890 891 /* if we're debugging, probe/attach now rather than later */ 892 ACPI_DEBUG_EXEC(device_probe_and_attach(child)); 893 break; 894 } 895 } 896 return_ACPI_STATUS(AE_OK); 897 } 898 899 static void 900 acpi_shutdown_pre_sync(void *arg, int howto) 901 { 902 903 struct acpi_softc *sc = arg; 904 905 ACPI_ASSERTLOCK; 906 907 /* 908 * Disable all ACPI events before soft off, otherwise the system 909 * will be turned on again on some laptops. 910 * 911 * XXX this should probably be restricted to masking some events just 912 * before powering down, since we may still need ACPI during the 913 * shutdown process. 914 */ 915 if (sc->acpi_disable_on_poweroff) 916 acpi_Disable(sc); 917 } 918 919 static void 920 acpi_shutdown_final(void *arg, int howto) 921 { 922 ACPI_STATUS status; 923 924 ACPI_ASSERTLOCK; 925 926 if (howto & RB_POWEROFF) { 927 printf("Power system off using ACPI...\n"); 928 if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(acpi_off_state))) { 929 printf("AcpiEnterSleepStatePrep failed - %s\n", 930 AcpiFormatException(status)); 931 return; 932 } 933 if (ACPI_FAILURE(status = AcpiEnterSleepState(acpi_off_state))) { 934 printf("ACPI power-off failed - %s\n", AcpiFormatException(status)); 935 } else { 936 DELAY(1000000); 937 printf("ACPI power-off failed - timeout\n"); 938 } 939 } else { 940 printf("Terminate ACPI\n"); 941 AcpiTerminate(); 942 } 943 } 944 945 static void 946 acpi_enable_fixed_events(struct acpi_softc *sc) 947 { 948 static int first_time = 1; 949 #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n" 950 951 ACPI_ASSERTLOCK; 952 953 /* Enable and clear fixed events and install handlers. */ 954 if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) { 955 AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED, 0); 956 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED); 957 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 958 acpi_eventhandler_power_button_for_sleep, sc); 959 if (first_time) { 960 device_printf(sc->acpi_dev, MSGFORMAT, "power"); 961 } 962 } 963 if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) { 964 AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED, 0); 965 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED); 966 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 967 acpi_eventhandler_sleep_button_for_sleep, sc); 968 if (first_time) { 969 device_printf(sc->acpi_dev, MSGFORMAT, "sleep"); 970 } 971 } 972 973 first_time = 0; 974 } 975 976 /* 977 * Returns true if the device is actually present and should 978 * be attached to. This requires the present, enabled, UI-visible 979 * and diagnostics-passed bits to be set. 980 */ 981 BOOLEAN 982 acpi_DeviceIsPresent(device_t dev) 983 { 984 ACPI_HANDLE h; 985 ACPI_DEVICE_INFO devinfo; 986 ACPI_STATUS error; 987 988 ACPI_ASSERTLOCK; 989 990 if ((h = acpi_get_handle(dev)) == NULL) 991 return(FALSE); 992 if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo))) 993 return(FALSE); 994 /* if no _STA method, must be present */ 995 if (!(devinfo.Valid & ACPI_VALID_STA)) 996 return(TRUE); 997 /* return true for 'present' and 'functioning' */ 998 if ((devinfo.CurrentStatus & 0x9) == 0x9) 999 return(TRUE); 1000 return(FALSE); 1001 } 1002 1003 /* 1004 * Returns true if the battery is actually present and inserted. 1005 */ 1006 BOOLEAN 1007 acpi_BatteryIsPresent(device_t dev) 1008 { 1009 ACPI_HANDLE h; 1010 ACPI_DEVICE_INFO devinfo; 1011 ACPI_STATUS error; 1012 1013 ACPI_ASSERTLOCK; 1014 1015 if ((h = acpi_get_handle(dev)) == NULL) 1016 return(FALSE); 1017 if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo))) 1018 return(FALSE); 1019 /* if no _STA method, must be present */ 1020 if (!(devinfo.Valid & ACPI_VALID_STA)) 1021 return(TRUE); 1022 /* return true for 'present' and 'functioning' */ 1023 if ((devinfo.CurrentStatus & 0x19) == 0x19) 1024 return(TRUE); 1025 return(FALSE); 1026 } 1027 1028 /* 1029 * Match a HID string against a device 1030 */ 1031 BOOLEAN 1032 acpi_MatchHid(device_t dev, char *hid) 1033 { 1034 ACPI_HANDLE h; 1035 ACPI_DEVICE_INFO devinfo; 1036 ACPI_STATUS error; 1037 int cid; 1038 1039 ACPI_ASSERTLOCK; 1040 1041 if (hid == NULL) 1042 return(FALSE); 1043 if ((h = acpi_get_handle(dev)) == NULL) 1044 return(FALSE); 1045 if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo))) 1046 return(FALSE); 1047 if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId)) 1048 return(TRUE); 1049 if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &cid))) 1050 return(FALSE); 1051 if (cid == PNP_EISAID(hid)) 1052 return(TRUE); 1053 return(FALSE); 1054 } 1055 1056 /* 1057 * Return the handle of a named object within our scope, ie. that of (parent) 1058 * or one if its parents. 1059 */ 1060 ACPI_STATUS 1061 acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 1062 { 1063 ACPI_HANDLE r; 1064 ACPI_STATUS status; 1065 1066 ACPI_ASSERTLOCK; 1067 1068 /* walk back up the tree to the root */ 1069 for (;;) { 1070 if (ACPI_SUCCESS(status = AcpiGetHandle(parent, path, &r))) { 1071 *result = r; 1072 return(AE_OK); 1073 } 1074 if (status != AE_NOT_FOUND) 1075 return(AE_OK); 1076 if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 1077 return(AE_NOT_FOUND); 1078 parent = r; 1079 } 1080 } 1081 1082 /* 1083 * Allocate a buffer with a preset data size. 1084 */ 1085 ACPI_BUFFER * 1086 acpi_AllocBuffer(int size) 1087 { 1088 ACPI_BUFFER *buf; 1089 1090 if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 1091 return(NULL); 1092 buf->Length = size; 1093 buf->Pointer = (void *)(buf + 1); 1094 return(buf); 1095 } 1096 1097 /* 1098 * Evaluate a path that should return an integer. 1099 */ 1100 ACPI_STATUS 1101 acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number) 1102 { 1103 ACPI_STATUS error; 1104 ACPI_BUFFER buf; 1105 ACPI_OBJECT param; 1106 1107 ACPI_ASSERTLOCK; 1108 1109 if (handle == NULL) 1110 handle = ACPI_ROOT_OBJECT; 1111 1112 /* 1113 * Assume that what we've been pointed at is an Integer object, or 1114 * a method that will return an Integer. 1115 */ 1116 buf.Pointer = ¶m; 1117 buf.Length = sizeof(param); 1118 if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) { 1119 if (param.Type == ACPI_TYPE_INTEGER) { 1120 *number = param.Integer.Value; 1121 } else { 1122 error = AE_TYPE; 1123 } 1124 } 1125 1126 /* 1127 * In some applications, a method that's expected to return an Integer 1128 * may instead return a Buffer (probably to simplify some internal 1129 * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 1130 * convert it into an Integer as best we can. 1131 * 1132 * This is a hack. 1133 */ 1134 if (error == AE_BUFFER_OVERFLOW) { 1135 if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 1136 error = AE_NO_MEMORY; 1137 } else { 1138 if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) { 1139 error = acpi_ConvertBufferToInteger(&buf, number); 1140 } 1141 } 1142 AcpiOsFree(buf.Pointer); 1143 } 1144 return(error); 1145 } 1146 1147 ACPI_STATUS 1148 acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number) 1149 { 1150 ACPI_OBJECT *p; 1151 int i; 1152 1153 p = (ACPI_OBJECT *)bufp->Pointer; 1154 if (p->Type == ACPI_TYPE_INTEGER) { 1155 *number = p->Integer.Value; 1156 return(AE_OK); 1157 } 1158 if (p->Type != ACPI_TYPE_BUFFER) 1159 return(AE_TYPE); 1160 if (p->Buffer.Length > sizeof(int)) 1161 return(AE_BAD_DATA); 1162 *number = 0; 1163 for (i = 0; i < p->Buffer.Length; i++) 1164 *number += (*(p->Buffer.Pointer + i) << (i * 8)); 1165 return(AE_OK); 1166 } 1167 1168 /* 1169 * Iterate over the elements of an a package object, calling the supplied 1170 * function for each element. 1171 * 1172 * XXX possible enhancement might be to abort traversal on error. 1173 */ 1174 ACPI_STATUS 1175 acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg) 1176 { 1177 ACPI_OBJECT *comp; 1178 int i; 1179 1180 if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE)) 1181 return(AE_BAD_PARAMETER); 1182 1183 /* iterate over components */ 1184 for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++) 1185 func(comp, arg); 1186 1187 return(AE_OK); 1188 } 1189 1190 /* 1191 * Find the (index)th resource object in a set. 1192 */ 1193 ACPI_STATUS 1194 acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 1195 { 1196 ACPI_RESOURCE *rp; 1197 int i; 1198 1199 rp = (ACPI_RESOURCE *)buf->Pointer; 1200 i = index; 1201 while (i-- > 0) { 1202 /* range check */ 1203 if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 1204 return(AE_BAD_PARAMETER); 1205 /* check for terminator */ 1206 if ((rp->Id == ACPI_RSTYPE_END_TAG) || 1207 (rp->Length == 0)) 1208 return(AE_NOT_FOUND); 1209 rp = ACPI_RESOURCE_NEXT(rp); 1210 } 1211 if (resp != NULL) 1212 *resp = rp; 1213 return(AE_OK); 1214 } 1215 1216 /* 1217 * Append an ACPI_RESOURCE to an ACPI_BUFFER. 1218 * 1219 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 1220 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 1221 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 1222 * resources. 1223 */ 1224 #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 1225 1226 ACPI_STATUS 1227 acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 1228 { 1229 ACPI_RESOURCE *rp; 1230 void *newp; 1231 1232 /* 1233 * Initialise the buffer if necessary. 1234 */ 1235 if (buf->Pointer == NULL) { 1236 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 1237 if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 1238 return(AE_NO_MEMORY); 1239 rp = (ACPI_RESOURCE *)buf->Pointer; 1240 rp->Id = ACPI_RSTYPE_END_TAG; 1241 rp->Length = 0; 1242 } 1243 if (res == NULL) 1244 return(AE_OK); 1245 1246 /* 1247 * Scan the current buffer looking for the terminator. 1248 * This will either find the terminator or hit the end 1249 * of the buffer and return an error. 1250 */ 1251 rp = (ACPI_RESOURCE *)buf->Pointer; 1252 for (;;) { 1253 /* range check, don't go outside the buffer */ 1254 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 1255 return(AE_BAD_PARAMETER); 1256 if ((rp->Id == ACPI_RSTYPE_END_TAG) || 1257 (rp->Length == 0)) { 1258 break; 1259 } 1260 rp = ACPI_RESOURCE_NEXT(rp); 1261 } 1262 1263 /* 1264 * Check the size of the buffer and expand if required. 1265 * 1266 * Required size is: 1267 * size of existing resources before terminator + 1268 * size of new resource and header + 1269 * size of terminator. 1270 * 1271 * Note that this loop should really only run once, unless 1272 * for some reason we are stuffing a *really* huge resource. 1273 */ 1274 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 1275 res->Length + ACPI_RESOURCE_LENGTH_NO_DATA + 1276 ACPI_RESOURCE_LENGTH) >= buf->Length) { 1277 if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 1278 return(AE_NO_MEMORY); 1279 bcopy(buf->Pointer, newp, buf->Length); 1280 rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 1281 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 1282 AcpiOsFree(buf->Pointer); 1283 buf->Pointer = newp; 1284 buf->Length += buf->Length; 1285 } 1286 1287 /* 1288 * Insert the new resource. 1289 */ 1290 bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA); 1291 1292 /* 1293 * And add the terminator. 1294 */ 1295 rp = ACPI_RESOURCE_NEXT(rp); 1296 rp->Id = ACPI_RSTYPE_END_TAG; 1297 rp->Length = 0; 1298 1299 return(AE_OK); 1300 } 1301 1302 /* 1303 * Set interrupt model. 1304 */ 1305 ACPI_STATUS 1306 acpi_SetIntrModel(int model) 1307 { 1308 ACPI_OBJECT_LIST ArgList; 1309 ACPI_OBJECT Arg; 1310 1311 Arg.Type = ACPI_TYPE_INTEGER; 1312 Arg.Integer.Value = model; 1313 ArgList.Count = 1; 1314 ArgList.Pointer = &Arg; 1315 return (AcpiEvaluateObject(ACPI_ROOT_OBJECT, "_PIC", &ArgList, NULL)); 1316 } 1317 1318 #define ACPI_MINIMUM_AWAKETIME 5 1319 1320 static void 1321 acpi_sleep_enable(void *arg) 1322 { 1323 ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0; 1324 } 1325 1326 /* 1327 * Set the system sleep state 1328 * 1329 * Currently we only support S1 and S5 1330 */ 1331 ACPI_STATUS 1332 acpi_SetSleepState(struct acpi_softc *sc, int state) 1333 { 1334 ACPI_STATUS status = AE_OK; 1335 UINT8 TypeA; 1336 UINT8 TypeB; 1337 1338 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 1339 ACPI_ASSERTLOCK; 1340 1341 if (sc->acpi_sstate != ACPI_STATE_S0) 1342 return_ACPI_STATUS(AE_BAD_PARAMETER); /* avoid reentry */ 1343 1344 if (sc->acpi_sleep_disabled) 1345 return_ACPI_STATUS(AE_OK); 1346 1347 switch (state) { 1348 case ACPI_STATE_S0: /* XXX only for testing */ 1349 if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) { 1350 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status)); 1351 } 1352 break; 1353 1354 case ACPI_STATE_S1: 1355 case ACPI_STATE_S2: 1356 case ACPI_STATE_S3: 1357 case ACPI_STATE_S4: 1358 if (ACPI_FAILURE(status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB))) { 1359 device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", AcpiFormatException(status)); 1360 break; 1361 } 1362 1363 sc->acpi_sstate = state; 1364 sc->acpi_sleep_disabled = 1; 1365 1366 /* 1367 * Inform all devices that we are going to sleep. 1368 */ 1369 if (DEVICE_SUSPEND(root_bus) != 0) { 1370 /* 1371 * Re-wake the system. 1372 * 1373 * XXX note that a better two-pass approach with a 'veto' pass 1374 * followed by a "real thing" pass would be better, but the 1375 * current bus interface does not provide for this. 1376 */ 1377 DEVICE_RESUME(root_bus); 1378 return_ACPI_STATUS(AE_ERROR); 1379 } 1380 1381 if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(state))) { 1382 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 1383 AcpiFormatException(status)); 1384 break; 1385 } 1386 1387 if (sc->acpi_sleep_delay > 0) { 1388 DELAY(sc->acpi_sleep_delay * 1000000); 1389 } 1390 1391 if (state != ACPI_STATE_S1) { 1392 acpi_sleep_machdep(sc, state); 1393 1394 /* AcpiEnterSleepState() maybe incompleted, unlock here if locked. */ 1395 if (AcpiGbl_AcpiMutexInfo[ACPI_MTX_HARDWARE].OwnerId != ACPI_MUTEX_NOT_ACQUIRED) { 1396 AcpiUtReleaseMutex(ACPI_MTX_HARDWARE); 1397 } 1398 1399 /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 1400 if (state == ACPI_STATE_S4) { 1401 AcpiEnable(); 1402 } 1403 } else { 1404 if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) { 1405 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status)); 1406 break; 1407 } 1408 } 1409 AcpiLeaveSleepState((UINT8)state); 1410 DEVICE_RESUME(root_bus); 1411 sc->acpi_sstate = ACPI_STATE_S0; 1412 acpi_enable_fixed_events(sc); 1413 break; 1414 1415 case ACPI_STATE_S5: 1416 /* 1417 * Shut down cleanly and power off. This will call us back through the 1418 * shutdown handlers. 1419 */ 1420 shutdown_nice(RB_POWEROFF); 1421 break; 1422 1423 default: 1424 status = AE_BAD_PARAMETER; 1425 break; 1426 } 1427 1428 if (sc->acpi_sleep_disabled) 1429 timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME); 1430 1431 return_ACPI_STATUS(status); 1432 } 1433 1434 /* 1435 * Enable/Disable ACPI 1436 */ 1437 ACPI_STATUS 1438 acpi_Enable(struct acpi_softc *sc) 1439 { 1440 ACPI_STATUS status; 1441 u_int32_t flags; 1442 1443 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1444 ACPI_ASSERTLOCK; 1445 1446 flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT | 1447 ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 1448 if (!sc->acpi_enabled) { 1449 status = AcpiEnableSubsystem(flags); 1450 } else { 1451 status = AE_OK; 1452 } 1453 if (status == AE_OK) 1454 sc->acpi_enabled = 1; 1455 return_ACPI_STATUS(status); 1456 } 1457 1458 ACPI_STATUS 1459 acpi_Disable(struct acpi_softc *sc) 1460 { 1461 ACPI_STATUS status; 1462 1463 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1464 ACPI_ASSERTLOCK; 1465 1466 if (sc->acpi_enabled) { 1467 status = AcpiDisable(); 1468 } else { 1469 status = AE_OK; 1470 } 1471 if (status == AE_OK) 1472 sc->acpi_enabled = 0; 1473 return_ACPI_STATUS(status); 1474 } 1475 1476 /* 1477 * ACPI Event Handlers 1478 */ 1479 1480 /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 1481 1482 static void 1483 acpi_system_eventhandler_sleep(void *arg, int state) 1484 { 1485 ACPI_LOCK_DECL; 1486 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 1487 1488 ACPI_LOCK; 1489 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 1490 acpi_SetSleepState((struct acpi_softc *)arg, state); 1491 ACPI_UNLOCK; 1492 return_VOID; 1493 } 1494 1495 static void 1496 acpi_system_eventhandler_wakeup(void *arg, int state) 1497 { 1498 ACPI_LOCK_DECL; 1499 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 1500 1501 /* Well, what to do? :-) */ 1502 1503 ACPI_LOCK; 1504 ACPI_UNLOCK; 1505 1506 return_VOID; 1507 } 1508 1509 /* 1510 * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 1511 */ 1512 UINT32 1513 acpi_eventhandler_power_button_for_sleep(void *context) 1514 { 1515 struct acpi_softc *sc = (struct acpi_softc *)context; 1516 1517 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1518 1519 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 1520 1521 return_VALUE(ACPI_INTERRUPT_HANDLED); 1522 } 1523 1524 UINT32 1525 acpi_eventhandler_power_button_for_wakeup(void *context) 1526 { 1527 struct acpi_softc *sc = (struct acpi_softc *)context; 1528 1529 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1530 1531 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 1532 1533 return_VALUE(ACPI_INTERRUPT_HANDLED); 1534 } 1535 1536 UINT32 1537 acpi_eventhandler_sleep_button_for_sleep(void *context) 1538 { 1539 struct acpi_softc *sc = (struct acpi_softc *)context; 1540 1541 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1542 1543 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 1544 1545 return_VALUE(ACPI_INTERRUPT_HANDLED); 1546 } 1547 1548 UINT32 1549 acpi_eventhandler_sleep_button_for_wakeup(void *context) 1550 { 1551 struct acpi_softc *sc = (struct acpi_softc *)context; 1552 1553 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1554 1555 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 1556 1557 return_VALUE(ACPI_INTERRUPT_HANDLED); 1558 } 1559 1560 /* 1561 * XXX This is kinda ugly, and should not be here. 1562 */ 1563 struct acpi_staticbuf { 1564 ACPI_BUFFER buffer; 1565 char data[512]; 1566 }; 1567 1568 char * 1569 acpi_name(ACPI_HANDLE handle) 1570 { 1571 static struct acpi_staticbuf buf; 1572 1573 ACPI_ASSERTLOCK; 1574 1575 buf.buffer.Length = 512; 1576 buf.buffer.Pointer = &buf.data[0]; 1577 1578 if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer))) 1579 return(buf.buffer.Pointer); 1580 return("(unknown path)"); 1581 } 1582 1583 /* 1584 * Debugging/bug-avoidance. Avoid trying to fetch info on various 1585 * parts of the namespace. 1586 */ 1587 int 1588 acpi_avoid(ACPI_HANDLE handle) 1589 { 1590 char *cp, *env, *np; 1591 int len; 1592 1593 np = acpi_name(handle); 1594 if (*np == '\\') 1595 np++; 1596 if ((env = getenv("debug.acpi.avoid")) == NULL) 1597 return(0); 1598 1599 /* scan the avoid list checking for a match */ 1600 cp = env; 1601 for (;;) { 1602 while ((*cp != 0) && isspace(*cp)) 1603 cp++; 1604 if (*cp == 0) 1605 break; 1606 len = 0; 1607 while ((cp[len] != 0) && !isspace(cp[len])) 1608 len++; 1609 if (!strncmp(cp, np, len)) { 1610 freeenv(env); 1611 return(1); 1612 } 1613 cp += len; 1614 } 1615 freeenv(env); 1616 return(0); 1617 } 1618 1619 /* 1620 * Debugging/bug-avoidance. Disable ACPI subsystem components. 1621 */ 1622 int 1623 acpi_disabled(char *subsys) 1624 { 1625 char *cp, *env; 1626 int len; 1627 1628 if ((env = getenv("debug.acpi.disable")) == NULL) 1629 return(0); 1630 if (!strcmp(env, "all")) { 1631 freeenv(env); 1632 return(1); 1633 } 1634 1635 /* scan the disable list checking for a match */ 1636 cp = env; 1637 for (;;) { 1638 while ((*cp != 0) && isspace(*cp)) 1639 cp++; 1640 if (*cp == 0) 1641 break; 1642 len = 0; 1643 while ((cp[len] != 0) && !isspace(cp[len])) 1644 len++; 1645 if (!strncmp(cp, subsys, len)) { 1646 freeenv(env); 1647 return(1); 1648 } 1649 cp += len; 1650 } 1651 freeenv(env); 1652 return(0); 1653 } 1654 1655 /* 1656 * Device wake capability enable/disable. 1657 */ 1658 void 1659 acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable) 1660 { 1661 ACPI_OBJECT_LIST ArgList; 1662 ACPI_OBJECT Arg; 1663 1664 /* 1665 * TBD: All Power Resources referenced by elements 2 through N 1666 * of the _PRW object are put into the ON state. 1667 */ 1668 1669 /* 1670 * enable/disable device wake function. 1671 */ 1672 1673 ArgList.Count = 1; 1674 ArgList.Pointer = &Arg; 1675 1676 Arg.Type = ACPI_TYPE_INTEGER; 1677 Arg.Integer.Value = enable; 1678 1679 (void)AcpiEvaluateObject(h, "_PSW", &ArgList, NULL); 1680 } 1681 1682 void 1683 acpi_device_enable_wake_event(ACPI_HANDLE h) 1684 { 1685 struct acpi_softc *sc; 1686 ACPI_STATUS status; 1687 ACPI_BUFFER prw_buffer; 1688 ACPI_OBJECT *res; 1689 1690 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1691 1692 if ((sc = devclass_get_softc(acpi_devclass, 0)) == NULL) { 1693 return; 1694 } 1695 1696 /* 1697 * _PRW object is only required for devices that have the ability 1698 * to wake the system from a system sleeping state. 1699 */ 1700 prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 1701 status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 1702 if (ACPI_FAILURE(status)) { 1703 return; 1704 } 1705 1706 res = (ACPI_OBJECT *)prw_buffer.Pointer; 1707 if (res == NULL) { 1708 return; 1709 } 1710 1711 if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) { 1712 goto out; 1713 } 1714 1715 /* 1716 * The element 1 of the _PRW object: 1717 * The lowest power system sleeping state that can be entered 1718 * while still providing wake functionality. 1719 * The sleeping state being entered must be greater or equal to 1720 * the power state declared in element 1 of the _PRW object. 1721 */ 1722 if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER) { 1723 goto out; 1724 } 1725 1726 if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value) { 1727 goto out; 1728 } 1729 1730 /* 1731 * The element 0 of the _PRW object: 1732 */ 1733 switch(res->Package.Elements[0].Type) { 1734 case ACPI_TYPE_INTEGER: 1735 /* 1736 * If the data type of this package element is numeric, then this 1737 * _PRW package element is the bit index in the GPEx_EN, in the 1738 * GPE blocks described in the FADT, of the enable bit that is 1739 * enabled for the wake event. 1740 */ 1741 1742 status = AcpiEnableEvent(res->Package.Elements[0].Integer.Value, 1743 ACPI_EVENT_GPE, ACPI_EVENT_WAKE_ENABLE); 1744 if (ACPI_FAILURE(status)) 1745 printf("%s: EnableEvent Failed\n", __func__); 1746 break; 1747 1748 case ACPI_TYPE_PACKAGE: 1749 /* XXX TBD */ 1750 1751 /* 1752 * If the data type of this package element is a package, then this 1753 * _PRW package element is itself a package containing two 1754 * elements. The first is an object reference to the GPE Block 1755 * device that contains the GPE that will be triggered by the wake 1756 * event. The second element is numeric and it contains the bit 1757 * index in the GPEx_EN, in the GPE Block referenced by the 1758 * first element in the package, of the enable bit that is enabled for 1759 * the wake event. 1760 * For example, if this field is a package then it is of the form: 1761 * Package() {\_SB.PCI0.ISA.GPE, 2} 1762 */ 1763 1764 break; 1765 1766 default: 1767 break; 1768 } 1769 1770 out: 1771 if (prw_buffer.Pointer != NULL) 1772 AcpiOsFree(prw_buffer.Pointer); 1773 return; 1774 } 1775 1776 /* 1777 * Control interface. 1778 * 1779 * We multiplex ioctls for all participating ACPI devices here. Individual 1780 * drivers wanting to be accessible via /dev/acpi should use the register/deregister 1781 * interface to make their handlers visible. 1782 */ 1783 struct acpi_ioctl_hook 1784 { 1785 TAILQ_ENTRY(acpi_ioctl_hook) link; 1786 u_long cmd; 1787 int (* fn)(u_long cmd, caddr_t addr, void *arg); 1788 void *arg; 1789 }; 1790 1791 static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 1792 static int acpi_ioctl_hooks_initted; 1793 1794 /* 1795 * Register an ioctl handler. 1796 */ 1797 int 1798 acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg) 1799 { 1800 struct acpi_ioctl_hook *hp; 1801 1802 if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 1803 return(ENOMEM); 1804 hp->cmd = cmd; 1805 hp->fn = fn; 1806 hp->arg = arg; 1807 if (acpi_ioctl_hooks_initted == 0) { 1808 TAILQ_INIT(&acpi_ioctl_hooks); 1809 acpi_ioctl_hooks_initted = 1; 1810 } 1811 TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 1812 return(0); 1813 } 1814 1815 /* 1816 * Deregister an ioctl handler. 1817 */ 1818 void 1819 acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg)) 1820 { 1821 struct acpi_ioctl_hook *hp; 1822 1823 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 1824 if ((hp->cmd == cmd) && (hp->fn == fn)) 1825 break; 1826 1827 if (hp != NULL) { 1828 TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 1829 free(hp, M_ACPIDEV); 1830 } 1831 } 1832 1833 static int 1834 acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td) 1835 { 1836 return(0); 1837 } 1838 1839 static int 1840 acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td) 1841 { 1842 return(0); 1843 } 1844 1845 static int 1846 acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 1847 { 1848 struct acpi_softc *sc; 1849 struct acpi_ioctl_hook *hp; 1850 int error, xerror, state; 1851 ACPI_LOCK_DECL; 1852 1853 ACPI_LOCK; 1854 1855 error = state = 0; 1856 sc = dev->si_drv1; 1857 1858 /* 1859 * Scan the list of registered ioctls, looking for handlers. 1860 */ 1861 if (acpi_ioctl_hooks_initted) { 1862 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 1863 if (hp->cmd == cmd) { 1864 xerror = hp->fn(cmd, addr, hp->arg); 1865 if (xerror != 0) 1866 error = xerror; 1867 goto out; 1868 } 1869 } 1870 } 1871 1872 /* 1873 * Core ioctls are not permitted for non-writable user. 1874 * Currently, other ioctls just fetch information. 1875 * Not changing system behavior. 1876 */ 1877 if(!(flag & FWRITE)){ 1878 return EPERM; 1879 } 1880 1881 /* 1882 * Core system ioctls. 1883 */ 1884 switch (cmd) { 1885 case ACPIIO_ENABLE: 1886 if (ACPI_FAILURE(acpi_Enable(sc))) 1887 error = ENXIO; 1888 break; 1889 1890 case ACPIIO_DISABLE: 1891 if (ACPI_FAILURE(acpi_Disable(sc))) 1892 error = ENXIO; 1893 break; 1894 1895 case ACPIIO_SETSLPSTATE: 1896 if (!sc->acpi_enabled) { 1897 error = ENXIO; 1898 break; 1899 } 1900 state = *(int *)addr; 1901 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) { 1902 acpi_SetSleepState(sc, state); 1903 } else { 1904 error = EINVAL; 1905 } 1906 break; 1907 1908 default: 1909 if (error == 0) 1910 error = EINVAL; 1911 break; 1912 } 1913 1914 out: 1915 ACPI_UNLOCK; 1916 return(error); 1917 } 1918 1919 static int 1920 acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 1921 { 1922 char sleep_state[4]; 1923 char buf[16]; 1924 int error; 1925 UINT8 state, TypeA, TypeB; 1926 1927 buf[0] = '\0'; 1928 for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX+1; state++) { 1929 if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) { 1930 sprintf(sleep_state, "S%d ", state); 1931 strcat(buf, sleep_state); 1932 } 1933 } 1934 error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 1935 return(error); 1936 } 1937 1938 static int 1939 acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 1940 { 1941 char sleep_state[10]; 1942 int error; 1943 u_int new_state, old_state; 1944 1945 old_state = *(u_int *)oidp->oid_arg1; 1946 if (old_state > ACPI_S_STATES_MAX+1) { 1947 strcpy(sleep_state, "unknown"); 1948 } else { 1949 bzero(sleep_state, sizeof(sleep_state)); 1950 strncpy(sleep_state, sleep_state_names[old_state], 1951 sizeof(sleep_state_names[old_state])); 1952 } 1953 error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 1954 if (error == 0 && req->newptr != NULL) { 1955 for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX+1; new_state++) { 1956 if (strncmp(sleep_state, sleep_state_names[new_state], 1957 sizeof(sleep_state)) == 0) 1958 break; 1959 } 1960 if (new_state <= ACPI_S_STATES_MAX+1) { 1961 if (new_state != old_state) { 1962 *(u_int *)oidp->oid_arg1 = new_state; 1963 } 1964 } else { 1965 error = EINVAL; 1966 } 1967 } 1968 return(error); 1969 } 1970 1971 #ifdef ACPI_DEBUG 1972 /* 1973 * Support for parsing debug options from the kernel environment. 1974 * 1975 * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 1976 * by specifying the names of the bits in the debug.acpi.layer and 1977 * debug.acpi.level environment variables. Bits may be unset by 1978 * prefixing the bit name with !. 1979 */ 1980 struct debugtag 1981 { 1982 char *name; 1983 UINT32 value; 1984 }; 1985 1986 static struct debugtag dbg_layer[] = { 1987 {"ACPI_UTILITIES", ACPI_UTILITIES}, 1988 {"ACPI_HARDWARE", ACPI_HARDWARE}, 1989 {"ACPI_EVENTS", ACPI_EVENTS}, 1990 {"ACPI_TABLES", ACPI_TABLES}, 1991 {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 1992 {"ACPI_PARSER", ACPI_PARSER}, 1993 {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 1994 {"ACPI_EXECUTER", ACPI_EXECUTER}, 1995 {"ACPI_RESOURCES", ACPI_RESOURCES}, 1996 {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 1997 {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 1998 {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 1999 2000 {"ACPI_BUS", ACPI_BUS}, 2001 {"ACPI_SYSTEM", ACPI_SYSTEM}, 2002 {"ACPI_POWER", ACPI_POWER}, 2003 {"ACPI_EC", ACPI_EC}, 2004 {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 2005 {"ACPI_BATTERY", ACPI_BATTERY}, 2006 {"ACPI_BUTTON", ACPI_BUTTON}, 2007 {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 2008 {"ACPI_THERMAL", ACPI_THERMAL}, 2009 {"ACPI_FAN", ACPI_FAN}, 2010 2011 {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 2012 {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 2013 {NULL, 0} 2014 }; 2015 2016 static struct debugtag dbg_level[] = { 2017 {"ACPI_LV_ERROR", ACPI_LV_ERROR}, 2018 {"ACPI_LV_WARN", ACPI_LV_WARN}, 2019 {"ACPI_LV_INIT", ACPI_LV_INIT}, 2020 {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 2021 {"ACPI_LV_INFO", ACPI_LV_INFO}, 2022 {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 2023 2024 /* Trace verbosity level 1 [Standard Trace Level] */ 2025 {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 2026 {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 2027 {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 2028 {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 2029 {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 2030 {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 2031 {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 2032 {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 2033 {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 2034 {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 2035 {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 2036 {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 2037 {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 2038 {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 2039 {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 2040 2041 /* Trace verbosity level 2 [Function tracing and memory allocation] */ 2042 {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 2043 {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 2044 {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 2045 {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 2046 {"ACPI_LV_ALL", ACPI_LV_ALL}, 2047 2048 /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 2049 {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 2050 {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 2051 {"ACPI_LV_IO", ACPI_LV_IO}, 2052 {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 2053 {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 2054 2055 /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 2056 {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 2057 {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 2058 {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 2059 {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 2060 {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 2061 {NULL, 0} 2062 }; 2063 2064 static void 2065 acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 2066 { 2067 char *ep; 2068 int i, l; 2069 int set; 2070 2071 while (*cp) { 2072 if (isspace(*cp)) { 2073 cp++; 2074 continue; 2075 } 2076 ep = cp; 2077 while (*ep && !isspace(*ep)) 2078 ep++; 2079 if (*cp == '!') { 2080 set = 0; 2081 cp++; 2082 if (cp == ep) 2083 continue; 2084 } else { 2085 set = 1; 2086 } 2087 l = ep - cp; 2088 for (i = 0; tag[i].name != NULL; i++) { 2089 if (!strncmp(cp, tag[i].name, l)) { 2090 if (set) { 2091 *flag |= tag[i].value; 2092 } else { 2093 *flag &= ~tag[i].value; 2094 } 2095 printf("ACPI_DEBUG: set '%s'\n", tag[i].name); 2096 } 2097 } 2098 cp = ep; 2099 } 2100 } 2101 2102 static void 2103 acpi_set_debugging(void *junk) 2104 { 2105 char *cp; 2106 2107 if (!cold) 2108 return; 2109 2110 AcpiDbgLayer = 0; 2111 AcpiDbgLevel = 0; 2112 if ((cp = getenv("debug.acpi.layer")) != NULL) { 2113 acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer); 2114 freeenv(cp); 2115 } 2116 if ((cp = getenv("debug.acpi.level")) != NULL) { 2117 acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel); 2118 freeenv(cp); 2119 } 2120 2121 printf("ACPI debug layer 0x%x debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel); 2122 } 2123 SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL); 2124 #endif 2125 2126 static int 2127 acpi_pm_func(u_long cmd, void *arg, ...) 2128 { 2129 int state, acpi_state; 2130 int error; 2131 struct acpi_softc *sc; 2132 va_list ap; 2133 2134 error = 0; 2135 switch (cmd) { 2136 case POWER_CMD_SUSPEND: 2137 sc = (struct acpi_softc *)arg; 2138 if (sc == NULL) { 2139 error = EINVAL; 2140 goto out; 2141 } 2142 2143 va_start(ap, arg); 2144 state = va_arg(ap, int); 2145 va_end(ap); 2146 2147 switch (state) { 2148 case POWER_SLEEP_STATE_STANDBY: 2149 acpi_state = sc->acpi_standby_sx; 2150 break; 2151 case POWER_SLEEP_STATE_SUSPEND: 2152 acpi_state = sc->acpi_suspend_sx; 2153 break; 2154 case POWER_SLEEP_STATE_HIBERNATE: 2155 acpi_state = ACPI_STATE_S4; 2156 break; 2157 default: 2158 error = EINVAL; 2159 goto out; 2160 } 2161 2162 acpi_SetSleepState(sc, acpi_state); 2163 break; 2164 2165 default: 2166 error = EINVAL; 2167 goto out; 2168 } 2169 2170 out: 2171 return (error); 2172 } 2173 2174 static void 2175 acpi_pm_register(void *arg) 2176 { 2177 2178 if (!cold) 2179 return; 2180 2181 if (resource_disabled("acpi", 0)) 2182 return; 2183 2184 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 2185 } 2186 2187 SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 2188 2189