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