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