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