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 static 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 defined(ACPI_MAX_THREADS) && ACPI_MAX_THREADS > 0 475 if ((error = acpi_task_thread_init())) { 476 goto out; 477 } 478 #endif 479 480 if ((error = acpi_machdep_init(dev))) { 481 goto out; 482 } 483 484 /* Register ACPI again to pass the correct argument of pm_func. */ 485 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 486 487 error = 0; 488 489 out: 490 ACPI_UNLOCK; 491 return_VALUE(error); 492 } 493 494 /* 495 * Handle a new device being added 496 */ 497 static device_t 498 acpi_add_child(device_t bus, int order, const char *name, int unit) 499 { 500 struct acpi_device *ad; 501 device_t child; 502 503 if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL) 504 return(NULL); 505 bzero(ad, sizeof(*ad)); 506 507 resource_list_init(&ad->ad_rl); 508 509 child = device_add_child_ordered(bus, order, name, unit); 510 if (child != NULL) 511 device_set_ivars(child, ad); 512 return(child); 513 } 514 515 /* 516 * Print child device resource usage 517 */ 518 static int 519 acpi_print_resources(struct resource_list *rl, const char *name, int type, const char *format) 520 { 521 struct resource_list_entry *rle; 522 int printed, retval; 523 524 printed = 0; 525 retval = 0; 526 527 if (!SLIST_FIRST(rl)) 528 return(0); 529 530 /* Yes, this is kinda cheating */ 531 SLIST_FOREACH(rle, rl, link) { 532 if (rle->type == type) { 533 if (printed == 0) 534 retval += printf(" %s ", name); 535 else if (printed > 0) 536 retval += printf(","); 537 printed++; 538 retval += printf(format, rle->start); 539 if (rle->count > 1) { 540 retval += printf("-"); 541 retval += printf(format, rle->start + 542 rle->count - 1); 543 } 544 } 545 } 546 return(retval); 547 } 548 549 static int 550 acpi_print_child(device_t bus, device_t child) 551 { 552 struct acpi_device *adev = device_get_ivars(child); 553 struct resource_list *rl = &adev->ad_rl; 554 int retval = 0; 555 556 retval += bus_print_child_header(bus, child); 557 retval += acpi_print_resources(rl, "port", SYS_RES_IOPORT, "%#lx"); 558 retval += acpi_print_resources(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 559 retval += acpi_print_resources(rl, "irq", SYS_RES_IRQ, "%ld"); 560 retval += bus_print_child_footer(bus, child); 561 562 return(retval); 563 } 564 565 566 /* 567 * Handle per-device ivars 568 */ 569 static int 570 acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 571 { 572 struct acpi_device *ad; 573 574 if ((ad = device_get_ivars(child)) == NULL) { 575 printf("device has no ivars\n"); 576 return(ENOENT); 577 } 578 579 switch(index) { 580 /* ACPI ivars */ 581 case ACPI_IVAR_HANDLE: 582 *(ACPI_HANDLE *)result = ad->ad_handle; 583 break; 584 case ACPI_IVAR_MAGIC: 585 *(int *)result = ad->ad_magic; 586 break; 587 case ACPI_IVAR_PRIVATE: 588 *(void **)result = ad->ad_private; 589 break; 590 591 /* ISA compatibility */ 592 case ISA_IVAR_VENDORID: 593 case ISA_IVAR_SERIAL: 594 case ISA_IVAR_COMPATID: 595 *(int *)result = -1; 596 break; 597 598 case ISA_IVAR_LOGICALID: 599 *(int *)result = acpi_isa_get_logicalid(child); 600 break; 601 602 default: 603 panic("bad ivar read request (%d)\n", index); 604 return(ENOENT); 605 } 606 return(0); 607 } 608 609 static int 610 acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 611 { 612 struct acpi_device *ad; 613 614 if ((ad = device_get_ivars(child)) == NULL) { 615 printf("device has no ivars\n"); 616 return(ENOENT); 617 } 618 619 switch(index) { 620 /* ACPI ivars */ 621 case ACPI_IVAR_HANDLE: 622 ad->ad_handle = (ACPI_HANDLE)value; 623 break; 624 case ACPI_IVAR_MAGIC: 625 ad->ad_magic = (int )value; 626 break; 627 case ACPI_IVAR_PRIVATE: 628 ad->ad_private = (void *)value; 629 break; 630 631 default: 632 panic("bad ivar write request (%d)\n", index); 633 return(ENOENT); 634 } 635 return(0); 636 } 637 638 /* 639 * Handle child resource allocation/removal 640 */ 641 static int 642 acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count) 643 { 644 struct acpi_device *ad = device_get_ivars(child); 645 struct resource_list *rl = &ad->ad_rl; 646 647 resource_list_add(rl, type, rid, start, start + count -1, count); 648 649 return(0); 650 } 651 652 static int 653 acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp) 654 { 655 struct acpi_device *ad = device_get_ivars(child); 656 struct resource_list *rl = &ad->ad_rl; 657 struct resource_list_entry *rle; 658 659 rle = resource_list_find(rl, type, rid); 660 if (!rle) 661 return(ENOENT); 662 663 if (startp) 664 *startp = rle->start; 665 if (countp) 666 *countp = rle->count; 667 668 return(0); 669 } 670 671 static struct resource * 672 acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 673 u_long start, u_long end, u_long count, u_int flags) 674 { 675 struct acpi_device *ad = device_get_ivars(child); 676 struct resource_list *rl = &ad->ad_rl; 677 678 return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags)); 679 } 680 681 static int 682 acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r) 683 { 684 struct acpi_device *ad = device_get_ivars(child); 685 struct resource_list *rl = &ad->ad_rl; 686 687 return(resource_list_release(rl, bus, child, type, rid, r)); 688 } 689 690 /* 691 * Handle ISA-like devices probing for a PnP ID to match. 692 */ 693 #define PNP_EISAID(s) \ 694 ((((s[0] - '@') & 0x1f) << 2) \ 695 | (((s[1] - '@') & 0x18) >> 3) \ 696 | (((s[1] - '@') & 0x07) << 13) \ 697 | (((s[2] - '@') & 0x1f) << 8) \ 698 | (PNP_HEXTONUM(s[4]) << 16) \ 699 | (PNP_HEXTONUM(s[3]) << 20) \ 700 | (PNP_HEXTONUM(s[6]) << 24) \ 701 | (PNP_HEXTONUM(s[5]) << 28)) 702 703 static u_int32_t 704 acpi_isa_get_logicalid(device_t dev) 705 { 706 ACPI_HANDLE h; 707 ACPI_DEVICE_INFO devinfo; 708 ACPI_STATUS error; 709 u_int32_t pnpid; 710 711 FUNCTION_TRACE(__func__); 712 713 pnpid = 0; 714 ACPI_LOCK; 715 716 /* fetch and validate the HID */ 717 if ((h = acpi_get_handle(dev)) == NULL) 718 goto out; 719 if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK) 720 goto out; 721 if (!(devinfo.Valid & ACPI_VALID_HID)) 722 goto out; 723 724 pnpid = PNP_EISAID(devinfo.HardwareId); 725 out: 726 ACPI_UNLOCK; 727 return_VALUE(pnpid); 728 } 729 730 static int 731 acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 732 { 733 int result; 734 u_int32_t pnpid; 735 736 FUNCTION_TRACE(__func__); 737 738 /* 739 * ISA-style drivers attached to ACPI may persist and 740 * probe manually if we return ENOENT. We never want 741 * that to happen, so don't ever return it. 742 */ 743 result = ENXIO; 744 745 /* scan the supplied IDs for a match */ 746 pnpid = acpi_isa_get_logicalid(child); 747 while (ids && ids->ip_id) { 748 if (pnpid == ids->ip_id) { 749 result = 0; 750 goto out; 751 } 752 ids++; 753 } 754 out: 755 return_VALUE(result); 756 } 757 758 /* 759 * Scan relevant portions of the ACPI namespace and attach child devices. 760 * 761 * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and \_SB_ scopes, 762 * and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec. 763 */ 764 static void 765 acpi_probe_children(device_t bus) 766 { 767 ACPI_HANDLE parent; 768 static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL}; 769 int i; 770 771 FUNCTION_TRACE(__func__); 772 ACPI_ASSERTLOCK; 773 774 /* 775 * Create any static children by calling device identify methods. 776 */ 777 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 778 bus_generic_probe(bus); 779 780 /* 781 * Scan the namespace and insert placeholders for all the devices that 782 * we find. 783 * 784 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 785 * we want to create nodes for all devices, not just those that are currently 786 * present. (This assumes that we don't want to create/remove devices as they 787 * appear, which might be smarter.) 788 */ 789 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 790 for (i = 0; scopes[i] != NULL; i++) 791 if ((AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent)) == AE_OK) 792 AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL); 793 794 /* 795 * Scan all of the child devices we have created and let them probe/attach. 796 */ 797 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n")); 798 bus_generic_attach(bus); 799 800 /* 801 * Some of these children may have attached others as part of their attach 802 * process (eg. the root PCI bus driver), so rescan. 803 */ 804 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n")); 805 bus_generic_attach(bus); 806 807 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 808 return_VOID; 809 } 810 811 /* 812 * Evaluate a child device and determine whether we might attach a device to 813 * it. 814 */ 815 static ACPI_STATUS 816 acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 817 { 818 ACPI_OBJECT_TYPE type; 819 device_t child, bus = (device_t)context; 820 821 FUNCTION_TRACE(__func__); 822 823 /* 824 * Skip this device if we think we'll have trouble with it. 825 */ 826 if (acpi_avoid(handle)) 827 return_ACPI_STATUS(AE_OK); 828 829 if (AcpiGetType(handle, &type) == AE_OK) { 830 switch(type) { 831 case ACPI_TYPE_DEVICE: 832 case ACPI_TYPE_PROCESSOR: 833 case ACPI_TYPE_THERMAL: 834 case ACPI_TYPE_POWER: 835 if (acpi_disabled("children")) 836 break; 837 /* 838 * Create a placeholder device for this node. Sort the placeholder 839 * so that the probe/attach passes will run breadth-first. 840 */ 841 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", acpi_name(handle))); 842 child = BUS_ADD_CHILD(bus, level * 10, NULL, -1); 843 if (child == NULL) 844 break; 845 acpi_set_handle(child, handle); 846 847 /* 848 * Check that the device is present. If it's not present, 849 * leave it disabled (so that we have a device_t attached to 850 * the handle, but we don't probe it). 851 */ 852 if ((type == ACPI_TYPE_DEVICE) && (!acpi_DeviceIsPresent(child))) { 853 device_disable(child); 854 break; 855 } 856 857 /* 858 * Get the device's resource settings and attach them. 859 * Note that if the device has _PRS but no _CRS, we need 860 * to decide when it's appropriate to try to configure the 861 * device. Ignore the return value here; it's OK for the 862 * device not to have any resources. 863 */ 864 acpi_parse_resources(child, handle, &acpi_res_parse_set); 865 866 /* if we're debugging, probe/attach now rather than later */ 867 DEBUG_EXEC(device_probe_and_attach(child)); 868 break; 869 } 870 } 871 return_ACPI_STATUS(AE_OK); 872 } 873 874 static void 875 acpi_shutdown_pre_sync(void *arg, int howto) 876 { 877 878 ACPI_ASSERTLOCK; 879 880 /* 881 * Disable all ACPI events before soft off, otherwise the system 882 * will be turned on again on some laptops. 883 * 884 * XXX this should probably be restricted to masking some events just 885 * before powering down, since we may still need ACPI during the 886 * shutdown process. 887 */ 888 acpi_Disable((struct acpi_softc *)arg); 889 } 890 891 static void 892 acpi_shutdown_final(void *arg, int howto) 893 { 894 ACPI_STATUS status; 895 896 ACPI_ASSERTLOCK; 897 898 if (howto & RB_POWEROFF) { 899 printf("Power system off using ACPI...\n"); 900 status = AcpiEnterSleepStatePrep(acpi_off_state); 901 if (status != AE_OK) { 902 printf("AcpiEnterSleepStatePrep failed - %s\n", 903 AcpiFormatException(status)); 904 return; 905 } 906 if ((status = AcpiEnterSleepState(acpi_off_state)) != AE_OK) { 907 printf("ACPI power-off failed - %s\n", AcpiFormatException(status)); 908 } else { 909 DELAY(1000000); 910 printf("ACPI power-off failed - timeout\n"); 911 } 912 } 913 } 914 915 static void 916 acpi_enable_fixed_events(struct acpi_softc *sc) 917 { 918 static int first_time = 1; 919 #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n" 920 921 ACPI_ASSERTLOCK; 922 923 /* Enable and clear fixed events and install handlers. */ 924 if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) { 925 AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED, 0); 926 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED); 927 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 928 acpi_eventhandler_power_button_for_sleep, sc); 929 if (first_time) { 930 device_printf(sc->acpi_dev, MSGFORMAT, "power"); 931 } 932 } 933 if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) { 934 AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED, 0); 935 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED); 936 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 937 acpi_eventhandler_sleep_button_for_sleep, sc); 938 if (first_time) { 939 device_printf(sc->acpi_dev, MSGFORMAT, "sleep"); 940 } 941 } 942 943 first_time = 0; 944 } 945 946 /* 947 * Returns true if the device is actually present and should 948 * be attached to. This requires the present, enabled, UI-visible 949 * and diagnostics-passed bits to be set. 950 */ 951 BOOLEAN 952 acpi_DeviceIsPresent(device_t dev) 953 { 954 ACPI_HANDLE h; 955 ACPI_DEVICE_INFO devinfo; 956 ACPI_STATUS error; 957 958 ACPI_ASSERTLOCK; 959 960 if ((h = acpi_get_handle(dev)) == NULL) 961 return(FALSE); 962 if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK) 963 return(FALSE); 964 /* if no _STA method, must be present */ 965 if (!(devinfo.Valid & ACPI_VALID_STA)) 966 return(TRUE); 967 /* return true for 'present' and 'functioning' */ 968 if ((devinfo.CurrentStatus & 0x9) == 0x9) 969 return(TRUE); 970 return(FALSE); 971 } 972 973 /* 974 * Match a HID string against a device 975 */ 976 BOOLEAN 977 acpi_MatchHid(device_t dev, char *hid) 978 { 979 ACPI_HANDLE h; 980 ACPI_DEVICE_INFO devinfo; 981 ACPI_STATUS error; 982 int cid; 983 984 ACPI_ASSERTLOCK; 985 986 if (hid == NULL) 987 return(FALSE); 988 if ((h = acpi_get_handle(dev)) == NULL) 989 return(FALSE); 990 if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK) 991 return(FALSE); 992 if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId)) 993 return(TRUE); 994 if ((error = acpi_EvaluateInteger(h, "_CID", &cid)) != AE_OK) 995 return(FALSE); 996 if (cid == PNP_EISAID(hid)) 997 return(TRUE); 998 return(FALSE); 999 } 1000 1001 /* 1002 * Return the handle of a named object within our scope, ie. that of (parent) 1003 * or one if its parents. 1004 */ 1005 ACPI_STATUS 1006 acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 1007 { 1008 ACPI_HANDLE r; 1009 ACPI_STATUS status; 1010 1011 ACPI_ASSERTLOCK; 1012 1013 /* walk back up the tree to the root */ 1014 for (;;) { 1015 status = AcpiGetHandle(parent, path, &r); 1016 if (status == AE_OK) { 1017 *result = r; 1018 return(AE_OK); 1019 } 1020 if (status != AE_NOT_FOUND) 1021 return(AE_OK); 1022 if (AcpiGetParent(parent, &r) != AE_OK) 1023 return(AE_NOT_FOUND); 1024 parent = r; 1025 } 1026 } 1027 1028 /* 1029 * Allocate a buffer with a preset data size. 1030 */ 1031 ACPI_BUFFER * 1032 acpi_AllocBuffer(int size) 1033 { 1034 ACPI_BUFFER *buf; 1035 1036 if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 1037 return(NULL); 1038 buf->Length = size; 1039 buf->Pointer = (void *)(buf + 1); 1040 return(buf); 1041 } 1042 1043 /* 1044 * Perform the tedious double-get procedure required for fetching something into 1045 * an ACPI_BUFFER that has not been initialised. 1046 */ 1047 ACPI_STATUS 1048 acpi_GetIntoBuffer(ACPI_HANDLE handle, ACPI_STATUS (*func)(ACPI_HANDLE, ACPI_BUFFER *), ACPI_BUFFER *buf) 1049 { 1050 ACPI_STATUS status; 1051 1052 ACPI_ASSERTLOCK; 1053 1054 buf->Length = 0; 1055 buf->Pointer = NULL; 1056 1057 if ((status = func(handle, buf)) != AE_BUFFER_OVERFLOW) 1058 return(status); 1059 if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL) 1060 return(AE_NO_MEMORY); 1061 return(func(handle, buf)); 1062 } 1063 1064 /* 1065 * Perform the tedious double-get procedure required for fetching a table into 1066 * an ACPI_BUFFER that has not been initialised. 1067 */ 1068 ACPI_STATUS 1069 acpi_GetTableIntoBuffer(ACPI_TABLE_TYPE table, UINT32 instance, ACPI_BUFFER *buf) 1070 { 1071 ACPI_STATUS status; 1072 1073 ACPI_ASSERTLOCK; 1074 1075 buf->Length = 0; 1076 buf->Pointer = NULL; 1077 1078 if ((status = AcpiGetTable(table, instance, buf)) != AE_BUFFER_OVERFLOW) 1079 return(status); 1080 if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL) 1081 return(AE_NO_MEMORY); 1082 return(AcpiGetTable(table, instance, buf)); 1083 } 1084 1085 /* 1086 * Perform the tedious double-evaluate procedure for evaluating something into 1087 * an ACPI_BUFFER if it has not been initialised. Note that this evaluates 1088 * twice, so avoid applying this to things that may have side-effects. 1089 * 1090 * This is like AcpiEvaluateObject with automatic buffer allocation. 1091 */ 1092 ACPI_STATUS 1093 acpi_EvaluateIntoBuffer(ACPI_HANDLE object, ACPI_STRING pathname, ACPI_OBJECT_LIST *params, 1094 ACPI_BUFFER *buf) 1095 { 1096 ACPI_STATUS status; 1097 1098 ACPI_ASSERTLOCK; 1099 1100 if ((status = AcpiEvaluateObject(object, pathname, params, buf)) != AE_BUFFER_OVERFLOW) 1101 return(status); 1102 if (buf->Pointer != NULL) 1103 AcpiOsFree(buf->Pointer); 1104 if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL) 1105 return(AE_NO_MEMORY); 1106 return(AcpiEvaluateObject(object, pathname, params, buf)); 1107 } 1108 1109 /* 1110 * Evaluate a path that should return an integer. 1111 */ 1112 ACPI_STATUS 1113 acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number) 1114 { 1115 ACPI_STATUS error; 1116 ACPI_BUFFER buf; 1117 ACPI_OBJECT param; 1118 1119 ACPI_ASSERTLOCK; 1120 1121 if (handle == NULL) 1122 handle = ACPI_ROOT_OBJECT; 1123 1124 /* 1125 * Assume that what we've been pointed at is an Integer object, or 1126 * a method that will return an Integer. 1127 */ 1128 buf.Pointer = ¶m; 1129 buf.Length = sizeof(param); 1130 if ((error = AcpiEvaluateObject(handle, path, NULL, &buf)) == AE_OK) { 1131 if (param.Type == ACPI_TYPE_INTEGER) { 1132 *number = param.Integer.Value; 1133 } else { 1134 error = AE_TYPE; 1135 } 1136 } 1137 1138 /* 1139 * In some applications, a method that's expected to return an Integer 1140 * may instead return a Buffer (probably to simplify some internal 1141 * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 1142 * convert it into an Integer as best we can. 1143 * 1144 * This is a hack. 1145 */ 1146 if (error == AE_BUFFER_OVERFLOW) { 1147 if ((buf.Pointer = AcpiOsCallocate(buf.Length)) == NULL) { 1148 error = AE_NO_MEMORY; 1149 } else { 1150 if ((error = AcpiEvaluateObject(handle, path, NULL, &buf)) == AE_OK) { 1151 error = acpi_ConvertBufferToInteger(&buf, number); 1152 } 1153 } 1154 AcpiOsFree(buf.Pointer); 1155 } 1156 return(error); 1157 } 1158 1159 ACPI_STATUS 1160 acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number) 1161 { 1162 ACPI_OBJECT *p; 1163 int i; 1164 1165 p = (ACPI_OBJECT *)bufp->Pointer; 1166 if (p->Type == ACPI_TYPE_INTEGER) { 1167 *number = p->Integer.Value; 1168 return(AE_OK); 1169 } 1170 if (p->Type != ACPI_TYPE_BUFFER) 1171 return(AE_TYPE); 1172 if (p->Buffer.Length > sizeof(int)) 1173 return(AE_BAD_DATA); 1174 *number = 0; 1175 for (i = 0; i < p->Buffer.Length; i++) 1176 *number += (*(p->Buffer.Pointer + i) << (i * 8)); 1177 return(AE_OK); 1178 } 1179 1180 /* 1181 * Iterate over the elements of an a package object, calling the supplied 1182 * function for each element. 1183 * 1184 * XXX possible enhancement might be to abort traversal on error. 1185 */ 1186 ACPI_STATUS 1187 acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg) 1188 { 1189 ACPI_OBJECT *comp; 1190 int i; 1191 1192 if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE)) 1193 return(AE_BAD_PARAMETER); 1194 1195 /* iterate over components */ 1196 for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++) 1197 func(comp, arg); 1198 1199 return(AE_OK); 1200 } 1201 1202 /* 1203 * Find the (index)th resource object in a set. 1204 */ 1205 ACPI_STATUS 1206 acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 1207 { 1208 ACPI_RESOURCE *rp; 1209 int i; 1210 1211 rp = (ACPI_RESOURCE *)buf->Pointer; 1212 i = index; 1213 while (i-- > 0) { 1214 /* range check */ 1215 if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 1216 return(AE_BAD_PARAMETER); 1217 /* check for terminator */ 1218 if ((rp->Id == ACPI_RSTYPE_END_TAG) || 1219 (rp->Length == 0)) 1220 return(AE_NOT_FOUND); 1221 rp = ACPI_RESOURCE_NEXT(rp); 1222 } 1223 if (resp != NULL) 1224 *resp = rp; 1225 return(AE_OK); 1226 } 1227 1228 /* 1229 * Append an ACPI_RESOURCE to an ACPI_BUFFER. 1230 * 1231 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 1232 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 1233 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 1234 * resources. 1235 */ 1236 #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 1237 1238 ACPI_STATUS 1239 acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 1240 { 1241 ACPI_RESOURCE *rp; 1242 void *newp; 1243 1244 /* 1245 * Initialise the buffer if necessary. 1246 */ 1247 if (buf->Pointer == NULL) { 1248 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 1249 if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 1250 return(AE_NO_MEMORY); 1251 rp = (ACPI_RESOURCE *)buf->Pointer; 1252 rp->Id = ACPI_RSTYPE_END_TAG; 1253 rp->Length = 0; 1254 } 1255 if (res == NULL) 1256 return(AE_OK); 1257 1258 /* 1259 * Scan the current buffer looking for the terminator. 1260 * This will either find the terminator or hit the end 1261 * of the buffer and return an error. 1262 */ 1263 rp = (ACPI_RESOURCE *)buf->Pointer; 1264 for (;;) { 1265 /* range check, don't go outside the buffer */ 1266 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 1267 return(AE_BAD_PARAMETER); 1268 if ((rp->Id == ACPI_RSTYPE_END_TAG) || 1269 (rp->Length == 0)) { 1270 break; 1271 } 1272 rp = ACPI_RESOURCE_NEXT(rp); 1273 } 1274 1275 /* 1276 * Check the size of the buffer and expand if required. 1277 * 1278 * Required size is: 1279 * size of existing resources before terminator + 1280 * size of new resource and header + 1281 * size of terminator. 1282 * 1283 * Note that this loop should really only run once, unless 1284 * for some reason we are stuffing a *really* huge resource. 1285 */ 1286 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 1287 res->Length + ACPI_RESOURCE_LENGTH_NO_DATA + 1288 ACPI_RESOURCE_LENGTH) >= buf->Length) { 1289 if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 1290 return(AE_NO_MEMORY); 1291 bcopy(buf->Pointer, newp, buf->Length); 1292 rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 1293 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 1294 AcpiOsFree(buf->Pointer); 1295 buf->Pointer = newp; 1296 buf->Length += buf->Length; 1297 } 1298 1299 /* 1300 * Insert the new resource. 1301 */ 1302 bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA); 1303 1304 /* 1305 * And add the terminator. 1306 */ 1307 rp = ACPI_RESOURCE_NEXT(rp); 1308 rp->Id = ACPI_RSTYPE_END_TAG; 1309 rp->Length = 0; 1310 1311 return(AE_OK); 1312 } 1313 1314 #define ACPI_MINIMUM_AWAKETIME 5 1315 1316 static void 1317 acpi_sleep_enable(void *arg) 1318 { 1319 ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0; 1320 } 1321 1322 /* 1323 * Set the system sleep state 1324 * 1325 * Currently we only support S1 and S5 1326 */ 1327 ACPI_STATUS 1328 acpi_SetSleepState(struct acpi_softc *sc, int state) 1329 { 1330 ACPI_STATUS status = AE_OK; 1331 UINT8 TypeA; 1332 UINT8 TypeB; 1333 1334 FUNCTION_TRACE_U32(__func__, state); 1335 ACPI_ASSERTLOCK; 1336 1337 if (sc->acpi_sstate != ACPI_STATE_S0) 1338 return_ACPI_STATUS(AE_BAD_PARAMETER); /* avoid reentry */ 1339 1340 if (sc->acpi_sleep_disabled) 1341 return_ACPI_STATUS(AE_OK); 1342 1343 switch (state) { 1344 case ACPI_STATE_S0: /* XXX only for testing */ 1345 status = AcpiEnterSleepState((UINT8)state); 1346 if (status != AE_OK) { 1347 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status)); 1348 } 1349 break; 1350 1351 case ACPI_STATE_S1: 1352 case ACPI_STATE_S2: 1353 case ACPI_STATE_S3: 1354 case ACPI_STATE_S4: 1355 status = AcpiHwObtainSleepTypeRegisterData((UINT8)state, &TypeA, &TypeB); 1356 if (status != AE_OK) { 1357 device_printf(sc->acpi_dev, "AcpiHwObtainSleepTypeRegisterData failed - %s\n", AcpiFormatException(status)); 1358 break; 1359 } 1360 1361 /* 1362 * Inform all devices that we are going to sleep. 1363 */ 1364 if (DEVICE_SUSPEND(root_bus) != 0) { 1365 /* 1366 * Re-wake the system. 1367 * 1368 * XXX note that a better two-pass approach with a 'veto' pass 1369 * followed by a "real thing" pass would be better, but the 1370 * current bus interface does not provide for this. 1371 */ 1372 DEVICE_RESUME(root_bus); 1373 return_ACPI_STATUS(AE_ERROR); 1374 } 1375 1376 status = AcpiEnterSleepStatePrep(state); 1377 if (status != AE_OK) { 1378 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 1379 AcpiFormatException(status)); 1380 break; 1381 } 1382 1383 sc->acpi_sstate = state; 1384 sc->acpi_sleep_disabled = 1; 1385 1386 if (state != ACPI_STATE_S1) { 1387 acpi_sleep_machdep(sc, state); 1388 1389 /* AcpiEnterSleepState() maybe incompleted, unlock here. */ 1390 AcpiUtReleaseMutex(ACPI_MTX_HARDWARE); 1391 1392 /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 1393 if (state == ACPI_STATE_S4) { 1394 AcpiEnable(); 1395 } 1396 } else { 1397 status = AcpiEnterSleepState((UINT8)state); 1398 if (status != AE_OK) { 1399 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status)); 1400 break; 1401 } 1402 } 1403 AcpiLeaveSleepState((UINT8)state); 1404 DEVICE_RESUME(root_bus); 1405 sc->acpi_sstate = ACPI_STATE_S0; 1406 acpi_enable_fixed_events(sc); 1407 break; 1408 1409 case ACPI_STATE_S5: 1410 /* 1411 * Shut down cleanly and power off. This will call us back through the 1412 * shutdown handlers. 1413 */ 1414 shutdown_nice(RB_POWEROFF); 1415 break; 1416 1417 default: 1418 status = AE_BAD_PARAMETER; 1419 break; 1420 } 1421 1422 if (sc->acpi_sleep_disabled) 1423 timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME); 1424 1425 return_ACPI_STATUS(status); 1426 } 1427 1428 /* 1429 * Enable/Disable ACPI 1430 */ 1431 ACPI_STATUS 1432 acpi_Enable(struct acpi_softc *sc) 1433 { 1434 ACPI_STATUS status; 1435 u_int32_t flags; 1436 1437 FUNCTION_TRACE(__func__); 1438 ACPI_ASSERTLOCK; 1439 1440 flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT | 1441 ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 1442 if (!sc->acpi_enabled) { 1443 status = AcpiEnableSubsystem(flags); 1444 } else { 1445 status = AE_OK; 1446 } 1447 if (status == AE_OK) 1448 sc->acpi_enabled = 1; 1449 return_ACPI_STATUS(status); 1450 } 1451 1452 ACPI_STATUS 1453 acpi_Disable(struct acpi_softc *sc) 1454 { 1455 ACPI_STATUS status; 1456 1457 FUNCTION_TRACE(__func__); 1458 ACPI_ASSERTLOCK; 1459 1460 if (sc->acpi_enabled) { 1461 status = AcpiDisable(); 1462 } else { 1463 status = AE_OK; 1464 } 1465 if (status == AE_OK) 1466 sc->acpi_enabled = 0; 1467 return_ACPI_STATUS(status); 1468 } 1469 1470 /* 1471 * ACPI Event Handlers 1472 */ 1473 1474 /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 1475 1476 static void 1477 acpi_system_eventhandler_sleep(void *arg, int state) 1478 { 1479 FUNCTION_TRACE_U32(__func__, state); 1480 1481 ACPI_LOCK; 1482 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 1483 acpi_SetSleepState((struct acpi_softc *)arg, state); 1484 ACPI_UNLOCK; 1485 return_VOID; 1486 } 1487 1488 static void 1489 acpi_system_eventhandler_wakeup(void *arg, int state) 1490 { 1491 FUNCTION_TRACE_U32(__func__, state); 1492 1493 /* Well, what to do? :-) */ 1494 1495 ACPI_LOCK; 1496 ACPI_UNLOCK; 1497 1498 return_VOID; 1499 } 1500 1501 /* 1502 * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 1503 */ 1504 UINT32 1505 acpi_eventhandler_power_button_for_sleep(void *context) 1506 { 1507 struct acpi_softc *sc = (struct acpi_softc *)context; 1508 1509 FUNCTION_TRACE(__func__); 1510 1511 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 1512 1513 return_VALUE(INTERRUPT_HANDLED); 1514 } 1515 1516 UINT32 1517 acpi_eventhandler_power_button_for_wakeup(void *context) 1518 { 1519 struct acpi_softc *sc = (struct acpi_softc *)context; 1520 1521 FUNCTION_TRACE(__func__); 1522 1523 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 1524 1525 return_VALUE(INTERRUPT_HANDLED); 1526 } 1527 1528 UINT32 1529 acpi_eventhandler_sleep_button_for_sleep(void *context) 1530 { 1531 struct acpi_softc *sc = (struct acpi_softc *)context; 1532 1533 FUNCTION_TRACE(__func__); 1534 1535 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 1536 1537 return_VALUE(INTERRUPT_HANDLED); 1538 } 1539 1540 UINT32 1541 acpi_eventhandler_sleep_button_for_wakeup(void *context) 1542 { 1543 struct acpi_softc *sc = (struct acpi_softc *)context; 1544 1545 FUNCTION_TRACE(__func__); 1546 1547 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 1548 1549 return_VALUE(INTERRUPT_HANDLED); 1550 } 1551 1552 /* 1553 * XXX This is kinda ugly, and should not be here. 1554 */ 1555 struct acpi_staticbuf { 1556 ACPI_BUFFER buffer; 1557 char data[512]; 1558 }; 1559 1560 char * 1561 acpi_name(ACPI_HANDLE handle) 1562 { 1563 static struct acpi_staticbuf buf; 1564 1565 ACPI_ASSERTLOCK; 1566 1567 buf.buffer.Length = 512; 1568 buf.buffer.Pointer = &buf.data[0]; 1569 1570 if (AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer) == AE_OK) 1571 return(buf.buffer.Pointer); 1572 return("(unknown path)"); 1573 } 1574 1575 /* 1576 * Debugging/bug-avoidance. Avoid trying to fetch info on various 1577 * parts of the namespace. 1578 */ 1579 int 1580 acpi_avoid(ACPI_HANDLE handle) 1581 { 1582 char *cp, *np; 1583 int len; 1584 1585 np = acpi_name(handle); 1586 if (*np == '\\') 1587 np++; 1588 if ((cp = getenv("debug.acpi.avoid")) == NULL) 1589 return(0); 1590 1591 /* scan the avoid list checking for a match */ 1592 for (;;) { 1593 while ((*cp != 0) && isspace(*cp)) 1594 cp++; 1595 if (*cp == 0) 1596 break; 1597 len = 0; 1598 while ((cp[len] != 0) && !isspace(cp[len])) 1599 len++; 1600 if (!strncmp(cp, np, len)) 1601 return(1); 1602 cp += len; 1603 } 1604 return(0); 1605 } 1606 1607 /* 1608 * Debugging/bug-avoidance. Disable ACPI subsystem components. 1609 */ 1610 int 1611 acpi_disabled(char *subsys) 1612 { 1613 char *cp; 1614 int len; 1615 1616 if ((cp = getenv("debug.acpi.disable")) == NULL) 1617 return(0); 1618 if (!strcmp(cp, "all")) 1619 return(1); 1620 1621 /* scan the disable list checking for a match */ 1622 for (;;) { 1623 while ((*cp != 0) && isspace(*cp)) 1624 cp++; 1625 if (*cp == 0) 1626 break; 1627 len = 0; 1628 while ((cp[len] != 0) && !isspace(cp[len])) 1629 len++; 1630 if (!strncmp(cp, subsys, len)) 1631 return(1); 1632 cp += len; 1633 } 1634 return(0); 1635 } 1636 1637 /* 1638 * Control interface. 1639 * 1640 * We multiplex ioctls for all participating ACPI devices here. Individual 1641 * drivers wanting to be accessible via /dev/acpi should use the register/deregister 1642 * interface to make their handlers visible. 1643 */ 1644 struct acpi_ioctl_hook 1645 { 1646 TAILQ_ENTRY(acpi_ioctl_hook) link; 1647 u_long cmd; 1648 int (* fn)(u_long cmd, caddr_t addr, void *arg); 1649 void *arg; 1650 }; 1651 1652 static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 1653 static int acpi_ioctl_hooks_initted; 1654 1655 /* 1656 * Register an ioctl handler. 1657 */ 1658 int 1659 acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg) 1660 { 1661 struct acpi_ioctl_hook *hp; 1662 1663 if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 1664 return(ENOMEM); 1665 hp->cmd = cmd; 1666 hp->fn = fn; 1667 hp->arg = arg; 1668 if (acpi_ioctl_hooks_initted == 0) { 1669 TAILQ_INIT(&acpi_ioctl_hooks); 1670 acpi_ioctl_hooks_initted = 1; 1671 } 1672 TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 1673 return(0); 1674 } 1675 1676 /* 1677 * Deregister an ioctl handler. 1678 */ 1679 void 1680 acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg)) 1681 { 1682 struct acpi_ioctl_hook *hp; 1683 1684 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 1685 if ((hp->cmd == cmd) && (hp->fn == fn)) 1686 break; 1687 1688 if (hp != NULL) { 1689 TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 1690 free(hp, M_ACPIDEV); 1691 } 1692 } 1693 1694 static int 1695 acpiopen(dev_t dev, int flag, int fmt, struct thread *td) 1696 { 1697 return(0); 1698 } 1699 1700 static int 1701 acpiclose(dev_t dev, int flag, int fmt, struct thread *td) 1702 { 1703 return(0); 1704 } 1705 1706 static int 1707 acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 1708 { 1709 struct acpi_softc *sc; 1710 struct acpi_ioctl_hook *hp; 1711 int error, xerror, state; 1712 1713 ACPI_LOCK; 1714 1715 error = state = 0; 1716 sc = dev->si_drv1; 1717 1718 /* 1719 * Scan the list of registered ioctls, looking for handlers. 1720 */ 1721 if (acpi_ioctl_hooks_initted) { 1722 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 1723 if (hp->cmd == cmd) { 1724 xerror = hp->fn(cmd, addr, hp->arg); 1725 if (xerror != 0) 1726 error = xerror; 1727 goto out; 1728 } 1729 } 1730 } 1731 1732 /* 1733 * Core system ioctls. 1734 */ 1735 switch (cmd) { 1736 case ACPIIO_ENABLE: 1737 if (ACPI_FAILURE(acpi_Enable(sc))) 1738 error = ENXIO; 1739 break; 1740 1741 case ACPIIO_DISABLE: 1742 if (ACPI_FAILURE(acpi_Disable(sc))) 1743 error = ENXIO; 1744 break; 1745 1746 case ACPIIO_SETSLPSTATE: 1747 if (!sc->acpi_enabled) { 1748 error = ENXIO; 1749 break; 1750 } 1751 state = *(int *)addr; 1752 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) { 1753 acpi_SetSleepState(sc, state); 1754 } else { 1755 error = EINVAL; 1756 } 1757 break; 1758 1759 default: 1760 if (error == 0) 1761 error = EINVAL; 1762 break; 1763 } 1764 1765 out: 1766 ACPI_UNLOCK; 1767 return(error); 1768 } 1769 1770 static int 1771 acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 1772 { 1773 char sleep_state[10]; 1774 int error; 1775 u_int new_state, old_state; 1776 1777 old_state = *(u_int *)oidp->oid_arg1; 1778 if (old_state > ACPI_S_STATES_MAX+1) { 1779 strcpy(sleep_state, "unknown"); 1780 } else { 1781 bzero(sleep_state, sizeof(sleep_state)); 1782 strncpy(sleep_state, sleep_state_names[old_state], 1783 sizeof(sleep_state_names[old_state])); 1784 } 1785 error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 1786 if (error == 0 && req->newptr != NULL) { 1787 for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX+1; new_state++) { 1788 if (strncmp(sleep_state, sleep_state_names[new_state], 1789 sizeof(sleep_state)) == 0) 1790 break; 1791 } 1792 if (new_state <= ACPI_S_STATES_MAX+1) { 1793 if (new_state != old_state) { 1794 *(u_int *)oidp->oid_arg1 = new_state; 1795 } 1796 } else { 1797 error = EINVAL; 1798 } 1799 } 1800 return(error); 1801 } 1802 1803 #ifdef ACPI_DEBUG 1804 /* 1805 * Support for parsing debug options from the kernel environment. 1806 * 1807 * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 1808 * by specifying the names of the bits in the debug.acpi.layer and 1809 * debug.acpi.level environment variables. Bits may be unset by 1810 * prefixing the bit name with !. 1811 */ 1812 struct debugtag 1813 { 1814 char *name; 1815 UINT32 value; 1816 }; 1817 1818 static struct debugtag dbg_layer[] = { 1819 {"ACPI_UTILITIES", ACPI_UTILITIES}, 1820 {"ACPI_HARDWARE", ACPI_HARDWARE}, 1821 {"ACPI_EVENTS", ACPI_EVENTS}, 1822 {"ACPI_TABLES", ACPI_TABLES}, 1823 {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 1824 {"ACPI_PARSER", ACPI_PARSER}, 1825 {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 1826 {"ACPI_EXECUTER", ACPI_EXECUTER}, 1827 {"ACPI_RESOURCES", ACPI_RESOURCES}, 1828 {"ACPI_DEBUGGER", ACPI_DEBUGGER}, 1829 {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 1830 1831 {"ACPI_BUS", ACPI_BUS}, 1832 {"ACPI_SYSTEM", ACPI_SYSTEM}, 1833 {"ACPI_POWER", ACPI_POWER}, 1834 {"ACPI_EC", ACPI_EC}, 1835 {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 1836 {"ACPI_BATTERY", ACPI_BATTERY}, 1837 {"ACPI_BUTTON", ACPI_BUTTON}, 1838 {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 1839 {"ACPI_THERMAL", ACPI_THERMAL}, 1840 {"ACPI_FAN", ACPI_FAN}, 1841 1842 {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 1843 {NULL, 0} 1844 }; 1845 1846 static struct debugtag dbg_level[] = { 1847 {"ACPI_LV_OK", ACPI_LV_OK}, 1848 {"ACPI_LV_INFO", ACPI_LV_INFO}, 1849 {"ACPI_LV_WARN", ACPI_LV_WARN}, 1850 {"ACPI_LV_ERROR", ACPI_LV_ERROR}, 1851 {"ACPI_LV_FATAL", ACPI_LV_FATAL}, 1852 {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 1853 {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 1854 {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 1855 {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 1856 {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 1857 {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 1858 {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 1859 {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 1860 {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 1861 {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 1862 {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 1863 {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 1864 {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 1865 {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 1866 {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 1867 {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 1868 {"ACPI_LV_IO", ACPI_LV_IO}, 1869 {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 1870 {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 1871 {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 1872 {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 1873 {"ACPI_LV_INIT", ACPI_LV_INIT}, 1874 {"ACPI_LV_ALL", ACPI_LV_ALL}, 1875 {"ACPI_DB_AML_DISASSEMBLE", ACPI_DB_AML_DISASSEMBLE}, 1876 {"ACPI_DB_VERBOSE_INFO", ACPI_DB_VERBOSE_INFO}, 1877 {"ACPI_DB_FULL_TABLES", ACPI_DB_FULL_TABLES}, 1878 {"ACPI_DB_EVENTS", ACPI_DB_EVENTS}, 1879 {"ACPI_DB_VERBOSE", ACPI_DB_VERBOSE}, 1880 {NULL, 0} 1881 }; 1882 1883 static void 1884 acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 1885 { 1886 char *ep; 1887 int i, l; 1888 int set; 1889 1890 while (*cp) { 1891 if (isspace(*cp)) { 1892 cp++; 1893 continue; 1894 } 1895 ep = cp; 1896 while (*ep && !isspace(*ep)) 1897 ep++; 1898 if (*cp == '!') { 1899 set = 0; 1900 cp++; 1901 if (cp == ep) 1902 continue; 1903 } else { 1904 set = 1; 1905 } 1906 l = ep - cp; 1907 for (i = 0; tag[i].name != NULL; i++) { 1908 if (!strncmp(cp, tag[i].name, l)) { 1909 if (set) { 1910 *flag |= tag[i].value; 1911 } else { 1912 *flag &= ~tag[i].value; 1913 } 1914 printf("ACPI_DEBUG: set '%s'\n", tag[i].name); 1915 } 1916 } 1917 cp = ep; 1918 } 1919 } 1920 1921 static void 1922 acpi_set_debugging(void *junk) 1923 { 1924 char *cp; 1925 1926 AcpiDbgLayer = 0; 1927 AcpiDbgLevel = 0; 1928 if ((cp = getenv("debug.acpi.layer")) != NULL) 1929 acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer); 1930 if ((cp = getenv("debug.acpi.level")) != NULL) 1931 acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel); 1932 1933 printf("ACPI debug layer 0x%x debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel); 1934 } 1935 SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL); 1936 #endif 1937 1938 static int 1939 acpi_pm_func(u_long cmd, void *arg, ...) 1940 { 1941 int state, acpi_state; 1942 int error; 1943 struct acpi_softc *sc; 1944 va_list ap; 1945 1946 error = 0; 1947 switch (cmd) { 1948 case POWER_CMD_SUSPEND: 1949 sc = (struct acpi_softc *)arg; 1950 if (sc == NULL) { 1951 error = EINVAL; 1952 goto out; 1953 } 1954 1955 va_start(ap, arg); 1956 state = va_arg(ap, int); 1957 va_end(ap); 1958 1959 switch (state) { 1960 case POWER_SLEEP_STATE_STANDBY: 1961 acpi_state = sc->acpi_standby_sx; 1962 break; 1963 case POWER_SLEEP_STATE_SUSPEND: 1964 acpi_state = sc->acpi_suspend_sx; 1965 break; 1966 case POWER_SLEEP_STATE_HIBERNATE: 1967 acpi_state = ACPI_STATE_S4; 1968 break; 1969 default: 1970 error = EINVAL; 1971 goto out; 1972 } 1973 1974 acpi_SetSleepState(sc, acpi_state); 1975 break; 1976 1977 default: 1978 error = EINVAL; 1979 goto out; 1980 } 1981 1982 out: 1983 return (error); 1984 } 1985 1986 static void 1987 acpi_pm_register(void *arg) 1988 { 1989 1990 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 1991 } 1992 1993 SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 1994 1995