1 /*- 2 * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org> 3 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 4 * Copyright (c) 2000, 2001 Michael Smith 5 * Copyright (c) 2000 BSDi 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 #include "opt_acpi.h" 33 #include <sys/param.h> 34 #include <sys/kernel.h> 35 #include <sys/proc.h> 36 #include <sys/lock.h> 37 #include <sys/malloc.h> 38 #include <sys/mutex.h> 39 #include <sys/bus.h> 40 #include <sys/conf.h> 41 #include <sys/ioccom.h> 42 #include <sys/reboot.h> 43 #include <sys/sysctl.h> 44 #include <sys/ctype.h> 45 46 #include <machine/clock.h> 47 48 #include <machine/resource.h> 49 50 #include "acpi.h" 51 52 #include <dev/acpica/acpivar.h> 53 #include <dev/acpica/acpiio.h> 54 55 MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 56 57 /* 58 * Hooks for the ACPI CA debugging infrastructure 59 */ 60 #define _COMPONENT ACPI_BUS 61 MODULE_NAME("ACPI") 62 63 /* 64 * Character device 65 */ 66 67 static d_open_t acpiopen; 68 static d_close_t acpiclose; 69 static d_ioctl_t acpiioctl; 70 71 #define CDEV_MAJOR 152 72 static struct cdevsw acpi_cdevsw = { 73 acpiopen, 74 acpiclose, 75 noread, 76 nowrite, 77 acpiioctl, 78 nopoll, 79 nommap, 80 nostrategy, 81 "acpi", 82 CDEV_MAJOR, 83 nodump, 84 nopsize, 85 0 86 }; 87 88 static const char* sleep_state_names[] = { 89 "S0", "S1", "S2", "S3", "S4", "S5", "S4B" }; 90 91 /* this has to be static, as the softc is gone when we need it */ 92 static int acpi_off_state = ACPI_STATE_S5; 93 94 struct mtx acpi_mutex; 95 96 static void acpi_identify(driver_t *driver, device_t parent); 97 static int acpi_probe(device_t dev); 98 static int acpi_attach(device_t dev); 99 static device_t acpi_add_child(device_t bus, int order, const char *name, int unit); 100 static int acpi_print_resources(struct resource_list *rl, const char *name, int type, 101 const char *format); 102 static int acpi_print_child(device_t bus, device_t child); 103 static int acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result); 104 static int acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value); 105 static int acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, 106 u_long count); 107 static int acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, 108 u_long *countp); 109 static struct resource *acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 110 u_long start, u_long end, u_long count, u_int flags); 111 static int acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r); 112 113 static void acpi_probe_children(device_t bus); 114 static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status); 115 116 static void acpi_shutdown_pre_sync(void *arg, int howto); 117 static void acpi_shutdown_final(void *arg, int howto); 118 119 static void acpi_enable_fixed_events(struct acpi_softc *sc); 120 121 static void acpi_system_eventhandler_sleep(void *arg, int state); 122 static void acpi_system_eventhandler_wakeup(void *arg, int state); 123 static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 124 125 static device_method_t acpi_methods[] = { 126 /* Device interface */ 127 DEVMETHOD(device_identify, acpi_identify), 128 DEVMETHOD(device_probe, acpi_probe), 129 DEVMETHOD(device_attach, acpi_attach), 130 DEVMETHOD(device_shutdown, bus_generic_shutdown), 131 DEVMETHOD(device_suspend, bus_generic_suspend), 132 DEVMETHOD(device_resume, bus_generic_resume), 133 134 /* Bus interface */ 135 DEVMETHOD(bus_add_child, acpi_add_child), 136 DEVMETHOD(bus_print_child, acpi_print_child), 137 DEVMETHOD(bus_read_ivar, acpi_read_ivar), 138 DEVMETHOD(bus_write_ivar, acpi_write_ivar), 139 DEVMETHOD(bus_set_resource, acpi_set_resource), 140 DEVMETHOD(bus_get_resource, acpi_get_resource), 141 DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 142 DEVMETHOD(bus_release_resource, acpi_release_resource), 143 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 144 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 145 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 146 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 147 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 148 149 {0, 0} 150 }; 151 152 static driver_t acpi_driver = { 153 "acpi", 154 acpi_methods, 155 sizeof(struct acpi_softc), 156 }; 157 158 devclass_t acpi_devclass; 159 DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, 0, 0); 160 161 SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, ""); 162 SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, ""); 163 164 /* 165 * Detect ACPI, perform early initialisation 166 */ 167 static void 168 acpi_identify(driver_t *driver, device_t parent) 169 { 170 device_t child; 171 ACPI_PHYSICAL_ADDRESS rsdp; 172 int error; 173 #ifdef ENABLE_DEBUGGER 174 char *debugpoint = getenv("debug.acpi.debugger"); 175 #endif 176 177 FUNCTION_TRACE(__func__); 178 179 if(!cold){ 180 printf("Don't load this driver from userland!!\n"); 181 return ; 182 } 183 184 /* 185 * Make sure we're not being doubly invoked. 186 */ 187 if (device_find_child(parent, "acpi", 0) != NULL) 188 return_VOID; 189 190 /* initialise the ACPI mutex */ 191 mtx_init(&acpi_mutex, "ACPI global lock", MTX_DEF); 192 193 /* 194 * Start up the ACPI CA subsystem. 195 */ 196 #ifdef ENABLE_DEBUGGER 197 if (debugpoint && !strcmp(debugpoint, "init")) 198 acpi_EnterDebugger(); 199 #endif 200 if ((error = AcpiInitializeSubsystem()) != AE_OK) { 201 printf("ACPI: initialisation failed: %s\n", acpi_strerror(error)); 202 return_VOID; 203 } 204 #ifdef ENABLE_DEBUGGER 205 if (debugpoint && !strcmp(debugpoint, "tables")) 206 acpi_EnterDebugger(); 207 #endif 208 if (((error = AcpiFindRootPointer(&rsdp)) != AE_OK) || 209 ((error = AcpiLoadTables(rsdp)) != AE_OK)) { 210 printf("ACPI: table load failed: %s\n", acpi_strerror(error)); 211 return_VOID; 212 } 213 214 /* 215 * Attach the actual ACPI device. 216 */ 217 if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) { 218 device_printf(parent, "ACPI: could not attach\n"); 219 return_VOID; 220 } 221 } 222 223 /* 224 * Fetch some descriptive data from ACPI to put in our attach message 225 */ 226 static int 227 acpi_probe(device_t dev) 228 { 229 ACPI_TABLE_HEADER th; 230 char buf[20]; 231 ACPI_STATUS status; 232 int error; 233 234 FUNCTION_TRACE(__func__); 235 ACPI_LOCK; 236 237 if ((status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th)) != AE_OK) { 238 device_printf(dev, "couldn't get XSDT header: %s\n", acpi_strerror(status)); 239 error = ENXIO; 240 } else { 241 sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId); 242 device_set_desc_copy(dev, buf); 243 error = 0; 244 } 245 ACPI_UNLOCK; 246 return_VALUE(error); 247 } 248 249 static int 250 acpi_attach(device_t dev) 251 { 252 struct acpi_softc *sc; 253 ACPI_STATUS status; 254 int error; 255 #ifdef ENABLE_DEBUGGER 256 char *debugpoint = getenv("debug.acpi.debugger"); 257 #endif 258 259 FUNCTION_TRACE(__func__); 260 ACPI_LOCK; 261 sc = device_get_softc(dev); 262 bzero(sc, sizeof(*sc)); 263 sc->acpi_dev = dev; 264 265 #ifdef ENABLE_DEBUGGER 266 if (debugpoint && !strcmp(debugpoint, "spaces")) 267 acpi_EnterDebugger(); 268 #endif 269 270 /* 271 * Install the default address space handlers. 272 */ 273 error = ENXIO; 274 if ((status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 275 ACPI_ADR_SPACE_SYSTEM_MEMORY, 276 ACPI_DEFAULT_HANDLER, 277 NULL, NULL)) != AE_OK) { 278 device_printf(dev, "could not initialise SystemMemory handler: %s\n", acpi_strerror(status)); 279 goto out; 280 } 281 if ((status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 282 ACPI_ADR_SPACE_SYSTEM_IO, 283 ACPI_DEFAULT_HANDLER, 284 NULL, NULL)) != AE_OK) { 285 device_printf(dev, "could not initialise SystemIO handler: %s\n", acpi_strerror(status)); 286 goto out; 287 } 288 if ((status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 289 ACPI_ADR_SPACE_PCI_CONFIG, 290 ACPI_DEFAULT_HANDLER, 291 NULL, NULL)) != AE_OK) { 292 device_printf(dev, "could not initialise PciConfig handler: %s\n", acpi_strerror(status)); 293 goto out; 294 } 295 296 /* 297 * Bring ACPI fully online. 298 * 299 * Note that we request that device _STA and _INI methods not be run (ACPI_NO_DEVICE_INIT) 300 * and the final object initialisation pass be skipped (ACPI_NO_OBJECT_INIT). 301 * 302 * XXX We need to arrange for the object init pass after we have attached all our 303 * child devices. 304 */ 305 #ifdef ENABLE_DEBUGGER 306 if (debugpoint && !strcmp(debugpoint, "enable")) 307 acpi_EnterDebugger(); 308 #endif 309 if ((status = AcpiEnableSubsystem(ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT)) != AE_OK) { 310 device_printf(dev, "could not enable ACPI: %s\n", acpi_strerror(status)); 311 goto out; 312 } 313 314 /* 315 * Setup our sysctl tree. 316 * 317 * XXX: This doesn't check to make sure that none of these fail. 318 */ 319 sysctl_ctx_init(&sc->acpi_sysctl_ctx); 320 sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 321 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 322 device_get_name(dev), CTLFLAG_RD, 0, ""); 323 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 324 OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 325 &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 326 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 327 OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 328 &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 329 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 330 OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 331 &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 332 333 /* 334 * Dispatch the default sleep state to devices. 335 * TBD: should be configured from userland policy manager. 336 */ 337 sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX; 338 sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX; 339 sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX; 340 341 acpi_enable_fixed_events(sc); 342 343 /* 344 * Scan the namespace and attach/initialise children. 345 */ 346 #ifdef ENABLE_DEBUGGER 347 if (debugpoint && !strcmp(debugpoint, "probe")) 348 acpi_EnterDebugger(); 349 #endif 350 if (!acpi_disabled("bus")) 351 acpi_probe_children(dev); 352 353 /* 354 * Register our shutdown handlers 355 */ 356 EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST); 357 EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST); 358 359 /* 360 * Register our acpi event handlers. 361 * XXX should be configurable eg. via userland policy manager. 362 */ 363 EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST); 364 EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST); 365 366 /* 367 * Flag our initial states. 368 */ 369 sc->acpi_enabled = 1; 370 sc->acpi_sstate = ACPI_STATE_S0; 371 372 /* 373 * Create the control device 374 */ 375 sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, 0, 5, 0660, "acpi"); 376 sc->acpi_dev_t->si_drv1 = sc; 377 378 #ifdef ENABLE_DEBUGGER 379 if (debugpoint && !strcmp(debugpoint, "running")) 380 acpi_EnterDebugger(); 381 #endif 382 error = 0; 383 384 out: 385 ACPI_UNLOCK; 386 return_VALUE(error); 387 } 388 389 /* 390 * Handle a new device being added 391 */ 392 static device_t 393 acpi_add_child(device_t bus, int order, const char *name, int unit) 394 { 395 struct acpi_device *ad; 396 device_t child; 397 398 if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL) 399 return(NULL); 400 bzero(ad, sizeof(*ad)); 401 402 resource_list_init(&ad->ad_rl); 403 404 child = device_add_child_ordered(bus, order, name, unit); 405 if (child != NULL) 406 device_set_ivars(child, ad); 407 return(child); 408 } 409 410 /* 411 * Print child device resource usage 412 */ 413 static int 414 acpi_print_resources(struct resource_list *rl, const char *name, int type, const char *format) 415 { 416 struct resource_list_entry *rle; 417 int printed, retval; 418 419 printed = 0; 420 retval = 0; 421 422 if (!SLIST_FIRST(rl)) 423 return(0); 424 425 /* Yes, this is kinda cheating */ 426 SLIST_FOREACH(rle, rl, link) { 427 if (rle->type == type) { 428 if (printed == 0) 429 retval += printf(" %s ", name); 430 else if (printed > 0) 431 retval += printf(","); 432 printed++; 433 retval += printf(format, rle->start); 434 if (rle->count > 1) { 435 retval += printf("-"); 436 retval += printf(format, rle->start + 437 rle->count - 1); 438 } 439 } 440 } 441 return(retval); 442 } 443 444 static int 445 acpi_print_child(device_t bus, device_t child) 446 { 447 struct acpi_device *adev = device_get_ivars(child); 448 struct resource_list *rl = &adev->ad_rl; 449 int retval = 0; 450 451 retval += bus_print_child_header(bus, child); 452 retval += acpi_print_resources(rl, "port", SYS_RES_IOPORT, "%#lx"); 453 retval += acpi_print_resources(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 454 retval += acpi_print_resources(rl, "irq", SYS_RES_IRQ, "%ld"); 455 retval += bus_print_child_footer(bus, child); 456 457 return(retval); 458 } 459 460 461 /* 462 * Handle per-device ivars 463 */ 464 static int 465 acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 466 { 467 struct acpi_device *ad; 468 469 if ((ad = device_get_ivars(child)) == NULL) { 470 printf("device has no ivars\n"); 471 return(ENOENT); 472 } 473 474 switch(index) { 475 /* ACPI ivars */ 476 case ACPI_IVAR_HANDLE: 477 *(ACPI_HANDLE *)result = ad->ad_handle; 478 break; 479 case ACPI_IVAR_MAGIC: 480 *(int *)result = ad->ad_magic; 481 break; 482 case ACPI_IVAR_PRIVATE: 483 *(void **)result = ad->ad_private; 484 break; 485 486 default: 487 panic("bad ivar read request (%d)\n", index); 488 return(ENOENT); 489 } 490 return(0); 491 } 492 493 static int 494 acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 495 { 496 struct acpi_device *ad; 497 498 if ((ad = device_get_ivars(child)) == NULL) { 499 printf("device has no ivars\n"); 500 return(ENOENT); 501 } 502 503 switch(index) { 504 /* ACPI ivars */ 505 case ACPI_IVAR_HANDLE: 506 ad->ad_handle = (ACPI_HANDLE)value; 507 break; 508 case ACPI_IVAR_MAGIC: 509 ad->ad_magic = (int )value; 510 break; 511 case ACPI_IVAR_PRIVATE: 512 ad->ad_private = (void *)value; 513 break; 514 515 default: 516 panic("bad ivar write request (%d)\n", index); 517 return(ENOENT); 518 } 519 return(0); 520 } 521 522 /* 523 * Handle child resource allocation/removal 524 */ 525 static int 526 acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count) 527 { 528 struct acpi_device *ad = device_get_ivars(child); 529 struct resource_list *rl = &ad->ad_rl; 530 531 resource_list_add(rl, type, rid, start, start + count -1, count); 532 533 return(0); 534 } 535 536 static int 537 acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp) 538 { 539 struct acpi_device *ad = device_get_ivars(child); 540 struct resource_list *rl = &ad->ad_rl; 541 struct resource_list_entry *rle; 542 543 rle = resource_list_find(rl, type, rid); 544 if (!rle) 545 return(ENOENT); 546 547 if (startp) 548 *startp = rle->start; 549 if (countp) 550 *countp = rle->count; 551 552 return(0); 553 } 554 555 static struct resource * 556 acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 557 u_long start, u_long end, u_long count, u_int flags) 558 { 559 struct acpi_device *ad = device_get_ivars(child); 560 struct resource_list *rl = &ad->ad_rl; 561 562 return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags)); 563 } 564 565 static int 566 acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r) 567 { 568 struct acpi_device *ad = device_get_ivars(child); 569 struct resource_list *rl = &ad->ad_rl; 570 571 return(resource_list_release(rl, bus, child, type, rid, r)); 572 } 573 574 /* 575 * Scan relevant portions of the ACPI namespace and attach child devices. 576 * 577 * Note that we only expect to find devices in the \_TZ_, \_SI_ and \_SB_ scopes, 578 * and \_TZ_ becomes obsolete in the ACPI 2.0 spec. 579 */ 580 static void 581 acpi_probe_children(device_t bus) 582 { 583 ACPI_HANDLE parent; 584 static char *scopes[] = {"\\_TZ_", "\\_SI", "\\_SB_", NULL}; 585 int i; 586 587 FUNCTION_TRACE(__func__); 588 ACPI_ASSERTLOCK; 589 590 /* 591 * Create any static children by calling device identify methods. 592 */ 593 DEBUG_PRINT(TRACE_OBJECTS, ("device identify routines\n")); 594 bus_generic_probe(bus); 595 596 /* 597 * Scan the namespace and insert placeholders for all the devices that 598 * we find. 599 * 600 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 601 * we want to create nodes for all devices, not just those that are currently 602 * present. (This assumes that we don't want to create/remove devices as they 603 * appear, which might be smarter.) 604 */ 605 DEBUG_PRINT(TRACE_OBJECTS, ("namespace scan\n")); 606 for (i = 0; scopes[i] != NULL; i++) 607 if ((AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent)) == AE_OK) 608 AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL); 609 610 /* 611 * Scan all of the child devices we have created and let them probe/attach. 612 */ 613 DEBUG_PRINT(TRACE_OBJECTS, ("first bus_generic_attach\n")); 614 bus_generic_attach(bus); 615 616 /* 617 * Some of these children may have attached others as part of their attach 618 * process (eg. the root PCI bus driver), so rescan. 619 */ 620 DEBUG_PRINT(TRACE_OBJECTS, ("second bus_generic_attach\n")); 621 bus_generic_attach(bus); 622 623 return_VOID; 624 } 625 626 /* 627 * Evaluate a child device and determine whether we might attach a device to 628 * it. 629 */ 630 static ACPI_STATUS 631 acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 632 { 633 ACPI_OBJECT_TYPE type; 634 device_t child, bus = (device_t)context; 635 636 FUNCTION_TRACE(__func__); 637 638 /* 639 * Skip this device if we think we'll have trouble with it. 640 */ 641 if (acpi_avoid(handle)) 642 return_ACPI_STATUS(AE_OK); 643 644 if (AcpiGetType(handle, &type) == AE_OK) { 645 switch(type) { 646 case ACPI_TYPE_DEVICE: 647 case ACPI_TYPE_PROCESSOR: 648 case ACPI_TYPE_THERMAL: 649 case ACPI_TYPE_POWER: 650 if (acpi_disabled("children")) 651 break; 652 /* 653 * Create a placeholder device for this node. Sort the placeholder 654 * so that the probe/attach passes will run breadth-first. 655 */ 656 DEBUG_PRINT(TRACE_OBJECTS, ("scanning '%s'\n", acpi_name(handle))) 657 child = BUS_ADD_CHILD(bus, level * 10, NULL, -1); 658 acpi_set_handle(child, handle); 659 DEBUG_EXEC(device_probe_and_attach(child)); 660 break; 661 } 662 } 663 return_ACPI_STATUS(AE_OK); 664 } 665 666 static void 667 acpi_shutdown_pre_sync(void *arg, int howto) 668 { 669 670 ACPI_ASSERTLOCK; 671 672 /* 673 * Disable all ACPI events before soft off, otherwise the system 674 * will be turned on again on some laptops. 675 * 676 * XXX this should probably be restricted to masking some events just 677 * before powering down, since we may still need ACPI during the 678 * shutdown process. 679 */ 680 acpi_Disable((struct acpi_softc *)arg); 681 } 682 683 static void 684 acpi_shutdown_final(void *arg, int howto) 685 { 686 ACPI_STATUS status; 687 688 ACPI_ASSERTLOCK; 689 690 if (howto & RB_POWEROFF) { 691 printf("Power system off using ACPI...\n"); 692 if ((status = AcpiEnterSleepState(acpi_off_state)) != AE_OK) { 693 printf("ACPI power-off failed - %s\n", acpi_strerror(status)); 694 } else { 695 DELAY(1000000); 696 printf("ACPI power-off failed - timeout\n"); 697 } 698 } 699 } 700 701 static void 702 acpi_enable_fixed_events(struct acpi_softc *sc) 703 { 704 static int first_time = 1; 705 #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n" 706 707 ACPI_ASSERTLOCK; 708 709 /* Enable and clear fixed events and install handlers. */ 710 if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) { 711 AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED); 712 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED); 713 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 714 acpi_eventhandler_power_button_for_sleep, sc); 715 if (first_time) { 716 device_printf(sc->acpi_dev, MSGFORMAT, "power"); 717 } 718 } 719 if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) { 720 AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED); 721 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED); 722 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 723 acpi_eventhandler_sleep_button_for_sleep, sc); 724 if (first_time) { 725 device_printf(sc->acpi_dev, MSGFORMAT, "sleep"); 726 } 727 } 728 729 first_time = 0; 730 } 731 732 /* 733 * Returns true if the device is actually present and should 734 * be attached to. This requires the present, enabled, UI-visible 735 * and diagnostics-passed bits to be set. 736 */ 737 BOOLEAN 738 acpi_DeviceIsPresent(device_t dev) 739 { 740 ACPI_HANDLE h; 741 ACPI_DEVICE_INFO devinfo; 742 ACPI_STATUS error; 743 744 ACPI_ASSERTLOCK; 745 746 if ((h = acpi_get_handle(dev)) == NULL) 747 return(FALSE); 748 if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK) 749 return(FALSE); 750 if ((devinfo.Valid & ACPI_VALID_HID) && (devinfo.CurrentStatus & 0xf)) 751 return(TRUE); 752 return(FALSE); 753 } 754 755 /* 756 * Match a HID string against a device 757 */ 758 BOOLEAN 759 acpi_MatchHid(device_t dev, char *hid) 760 { 761 ACPI_HANDLE h; 762 ACPI_DEVICE_INFO devinfo; 763 ACPI_STATUS error; 764 765 ACPI_ASSERTLOCK; 766 767 if (hid == NULL) 768 return(FALSE); 769 if ((h = acpi_get_handle(dev)) == NULL) 770 return(FALSE); 771 if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK) 772 return(FALSE); 773 if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId)) 774 return(TRUE); 775 return(FALSE); 776 } 777 778 /* 779 * Return the handle of a named object within our scope, ie. that of (parent) 780 * or one if its parents. 781 */ 782 ACPI_STATUS 783 acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 784 { 785 ACPI_HANDLE r; 786 ACPI_STATUS status; 787 788 ACPI_ASSERTLOCK; 789 790 /* walk back up the tree to the root */ 791 for (;;) { 792 status = AcpiGetHandle(parent, path, &r); 793 if (status == AE_OK) { 794 *result = r; 795 return(AE_OK); 796 } 797 if (status != AE_NOT_FOUND) 798 return(AE_OK); 799 if (AcpiGetParent(parent, &r) != AE_OK) 800 return(AE_NOT_FOUND); 801 parent = r; 802 } 803 } 804 805 /* 806 * Allocate a buffer with a preset data size. 807 */ 808 ACPI_BUFFER * 809 acpi_AllocBuffer(int size) 810 { 811 ACPI_BUFFER *buf; 812 813 if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 814 return(NULL); 815 buf->Length = size; 816 buf->Pointer = (void *)(buf + 1); 817 return(buf); 818 } 819 820 /* 821 * Perform the tedious double-get procedure required for fetching something into 822 * an ACPI_BUFFER that has not been initialised. 823 */ 824 ACPI_STATUS 825 acpi_GetIntoBuffer(ACPI_HANDLE handle, ACPI_STATUS (*func)(ACPI_HANDLE, ACPI_BUFFER *), ACPI_BUFFER *buf) 826 { 827 ACPI_STATUS status; 828 829 ACPI_ASSERTLOCK; 830 831 buf->Length = 0; 832 buf->Pointer = NULL; 833 834 if ((status = func(handle, buf)) != AE_BUFFER_OVERFLOW) 835 return(status); 836 if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL) 837 return(AE_NO_MEMORY); 838 return(func(handle, buf)); 839 } 840 841 /* 842 * Perform the tedious double-evaluate procedure for evaluating something into 843 * an ACPI_BUFFER that has not been initialised. Note that this evaluates 844 * twice, so avoid applying this to things that may have side-effects. 845 * 846 * This is like AcpiEvaluateObject with automatic buffer allocation. 847 */ 848 ACPI_STATUS 849 acpi_EvaluateIntoBuffer(ACPI_HANDLE object, ACPI_STRING pathname, ACPI_OBJECT_LIST *params, 850 ACPI_BUFFER *buf) 851 { 852 ACPI_STATUS status; 853 854 ACPI_ASSERTLOCK; 855 856 buf->Length = 0; 857 buf->Pointer = NULL; 858 859 if ((status = AcpiEvaluateObject(object, pathname, params, buf)) != AE_BUFFER_OVERFLOW) 860 return(status); 861 if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL) 862 return(AE_NO_MEMORY); 863 return(AcpiEvaluateObject(object, pathname, params, buf)); 864 } 865 866 /* 867 * Evaluate a path that should return an integer. 868 */ 869 ACPI_STATUS 870 acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number) 871 { 872 ACPI_STATUS error; 873 ACPI_BUFFER buf; 874 ACPI_OBJECT param; 875 876 ACPI_ASSERTLOCK; 877 878 if (handle == NULL) 879 handle = ACPI_ROOT_OBJECT; 880 buf.Pointer = ¶m; 881 buf.Length = sizeof(param); 882 if ((error = AcpiEvaluateObject(handle, path, NULL, &buf)) == AE_OK) { 883 if (param.Type == ACPI_TYPE_INTEGER) { 884 *number = param.Integer.Value; 885 } else { 886 error = AE_TYPE; 887 } 888 } 889 return(error); 890 } 891 892 /* 893 * Iterate over the elements of an a package object, calling the supplied 894 * function for each element. 895 * 896 * XXX possible enhancement might be to abort traversal on error. 897 */ 898 ACPI_STATUS 899 acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg) 900 { 901 ACPI_OBJECT *comp; 902 int i; 903 904 if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE)) 905 return(AE_BAD_PARAMETER); 906 907 /* iterate over components */ 908 for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++) 909 func(comp, arg); 910 911 return(AE_OK); 912 } 913 914 915 static ACPI_STATUS __inline 916 acpi_wakeup(UINT8 state) 917 { 918 UINT16 Count; 919 ACPI_STATUS Status; 920 ACPI_OBJECT_LIST Arg_list; 921 ACPI_OBJECT Arg; 922 ACPI_OBJECT Objects[3]; /* package plus 2 number objects */ 923 ACPI_BUFFER ReturnBuffer; 924 925 FUNCTION_TRACE_U32(__func__, state); 926 ACPI_ASSERTLOCK; 927 928 /* wait for the WAK_STS bit */ 929 Count = 0; 930 while (!(AcpiHwRegisterBitAccess(ACPI_READ, ACPI_MTX_LOCK, WAK_STS))) { 931 AcpiOsSleepUsec(1000); 932 /* 933 * Some BIOSes don't set WAK_STS at all, 934 * give up waiting for wakeup if we time out. 935 */ 936 if (Count > 1000) { 937 printf("ACPI: timed out waiting for WAK_STS, continuing\n"); 938 break; /* giving up */ 939 } 940 Count++; 941 } 942 943 /* 944 * Evaluate the _WAK method 945 */ 946 bzero(&Arg_list, sizeof(Arg_list)); 947 Arg_list.Count = 1; 948 Arg_list.Pointer = &Arg; 949 950 bzero(&Arg, sizeof(Arg)); 951 Arg.Type = ACPI_TYPE_INTEGER; 952 Arg.Integer.Value = state; 953 954 /* 955 * Set up _WAK result code buffer. 956 * 957 * XXX should use acpi_EvaluateIntoBuffer 958 */ 959 bzero(Objects, sizeof(Objects)); 960 ReturnBuffer.Length = sizeof(Objects); 961 ReturnBuffer.Pointer = Objects; 962 AcpiEvaluateObject (NULL, "\\_WAK", &Arg_list, &ReturnBuffer); 963 964 Status = AE_OK; 965 966 /* Check result code for _WAK */ 967 if (Objects[0].Type != ACPI_TYPE_PACKAGE || 968 Objects[1].Type != ACPI_TYPE_INTEGER || 969 Objects[2].Type != ACPI_TYPE_INTEGER) { 970 /* 971 * In many BIOSes, _WAK doesn't return a result code. 972 * We don't need to worry about it too much :-). 973 */ 974 DEBUG_PRINT(ACPI_INFO, 975 ("acpi_wakeup: _WAK result code is corrupted, " 976 "but should be OK.\n")); 977 } else { 978 /* evaluate status code */ 979 switch (Objects[1].Integer.Value) { 980 case 0x00000001: 981 DEBUG_PRINT(ACPI_ERROR, 982 ("acpi_wakeup: Wake was signaled " 983 "but failed due to lack of power.\n")); 984 Status = AE_ERROR; 985 break; 986 987 case 0x00000002: 988 DEBUG_PRINT(ACPI_ERROR, 989 ("acpi_wakeup: Wake was signaled " 990 "but failed due to thermal condition.\n")); 991 Status = AE_ERROR; 992 break; 993 } 994 /* evaluate PSS code */ 995 if (Objects[2].Integer.Value == 0) { 996 DEBUG_PRINT(ACPI_ERROR, 997 ("acpi_wakeup: The targeted S-state " 998 "was not entered because of too much current " 999 "being drawn from the power supply.\n")); 1000 Status = AE_ERROR; 1001 } 1002 } 1003 return_ACPI_STATUS(Status); 1004 } 1005 1006 /* 1007 * Set the system sleep state 1008 * 1009 * Currently we only support S1 and S5 1010 */ 1011 ACPI_STATUS 1012 acpi_SetSleepState(struct acpi_softc *sc, int state) 1013 { 1014 ACPI_STATUS status = AE_OK; 1015 1016 FUNCTION_TRACE_U32(__func__, state); 1017 ACPI_ASSERTLOCK; 1018 1019 switch (state) { 1020 case ACPI_STATE_S0: /* XXX only for testing */ 1021 status = AcpiEnterSleepState((UINT8)state); 1022 if (status != AE_OK) { 1023 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", acpi_strerror(status)); 1024 } 1025 break; 1026 1027 case ACPI_STATE_S1: 1028 /* 1029 * Inform all devices that we are going to sleep. 1030 */ 1031 if (DEVICE_SUSPEND(root_bus) != 0) { 1032 /* 1033 * Re-wake the system. 1034 * 1035 * XXX note that a better two-pass approach with a 'veto' pass 1036 * followed by a "real thing" pass would be better, but the 1037 * current bus interface does not provide for this. 1038 */ 1039 DEVICE_RESUME(root_bus); 1040 return_ACPI_STATUS(AE_ERROR); 1041 } 1042 sc->acpi_sstate = state; 1043 status = AcpiEnterSleepState((UINT8)state); 1044 if (status != AE_OK) { 1045 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", acpi_strerror(status)); 1046 break; 1047 } 1048 acpi_wakeup((UINT8)state); 1049 DEVICE_RESUME(root_bus); 1050 sc->acpi_sstate = ACPI_STATE_S0; 1051 acpi_enable_fixed_events(sc); 1052 break; 1053 1054 case ACPI_STATE_S3: 1055 acpi_off_state = ACPI_STATE_S3; 1056 /* FALLTHROUGH */ 1057 case ACPI_STATE_S5: 1058 /* 1059 * Shut down cleanly and power off. This will call us back through the 1060 * shutdown handlers. 1061 */ 1062 shutdown_nice(RB_POWEROFF); 1063 break; 1064 1065 default: 1066 status = AE_BAD_PARAMETER; 1067 break; 1068 } 1069 return_ACPI_STATUS(status); 1070 } 1071 1072 /* 1073 * Enable/Disable ACPI 1074 */ 1075 ACPI_STATUS 1076 acpi_Enable(struct acpi_softc *sc) 1077 { 1078 ACPI_STATUS status; 1079 u_int32_t flags; 1080 1081 FUNCTION_TRACE(__func__); 1082 ACPI_ASSERTLOCK; 1083 1084 flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT | 1085 ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 1086 if (!sc->acpi_enabled) { 1087 status = AcpiEnableSubsystem(flags); 1088 } else { 1089 status = AE_OK; 1090 } 1091 if (status == AE_OK) 1092 sc->acpi_enabled = 1; 1093 return_ACPI_STATUS(status); 1094 } 1095 1096 ACPI_STATUS 1097 acpi_Disable(struct acpi_softc *sc) 1098 { 1099 ACPI_STATUS status; 1100 1101 FUNCTION_TRACE(__func__); 1102 ACPI_ASSERTLOCK; 1103 1104 if (sc->acpi_enabled) { 1105 status = AcpiDisable(); 1106 } else { 1107 status = AE_OK; 1108 } 1109 if (status == AE_OK) 1110 sc->acpi_enabled = 0; 1111 return_ACPI_STATUS(status); 1112 } 1113 1114 /* 1115 * ACPI Event Handlers 1116 */ 1117 1118 /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 1119 1120 static void 1121 acpi_system_eventhandler_sleep(void *arg, int state) 1122 { 1123 FUNCTION_TRACE_U32(__func__, state); 1124 1125 ACPI_LOCK; 1126 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 1127 acpi_SetSleepState((struct acpi_softc *)arg, state); 1128 ACPI_UNLOCK; 1129 return_VOID; 1130 } 1131 1132 static void 1133 acpi_system_eventhandler_wakeup(void *arg, int state) 1134 { 1135 FUNCTION_TRACE_U32(__func__, state); 1136 1137 /* Well, what to do? :-) */ 1138 1139 ACPI_LOCK; 1140 ACPI_UNLOCK; 1141 1142 return_VOID; 1143 } 1144 1145 /* 1146 * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 1147 */ 1148 UINT32 1149 acpi_eventhandler_power_button_for_sleep(void *context) 1150 { 1151 struct acpi_softc *sc = (struct acpi_softc *)context; 1152 1153 FUNCTION_TRACE(__func__); 1154 1155 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 1156 1157 return_VALUE(INTERRUPT_HANDLED); 1158 } 1159 1160 UINT32 1161 acpi_eventhandler_power_button_for_wakeup(void *context) 1162 { 1163 struct acpi_softc *sc = (struct acpi_softc *)context; 1164 1165 FUNCTION_TRACE(__func__); 1166 1167 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 1168 1169 return_VALUE(INTERRUPT_HANDLED); 1170 } 1171 1172 UINT32 1173 acpi_eventhandler_sleep_button_for_sleep(void *context) 1174 { 1175 struct acpi_softc *sc = (struct acpi_softc *)context; 1176 1177 FUNCTION_TRACE(__func__); 1178 1179 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 1180 1181 return_VALUE(INTERRUPT_HANDLED); 1182 } 1183 1184 UINT32 1185 acpi_eventhandler_sleep_button_for_wakeup(void *context) 1186 { 1187 struct acpi_softc *sc = (struct acpi_softc *)context; 1188 1189 FUNCTION_TRACE(__func__); 1190 1191 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 1192 1193 return_VALUE(INTERRUPT_HANDLED); 1194 } 1195 1196 /* 1197 * XXX This is kinda ugly, and should not be here. 1198 */ 1199 struct acpi_staticbuf { 1200 ACPI_BUFFER buffer; 1201 char data[512]; 1202 }; 1203 1204 char * 1205 acpi_strerror(ACPI_STATUS excep) 1206 { 1207 static struct acpi_staticbuf buf; 1208 1209 buf.buffer.Length = 512; 1210 buf.buffer.Pointer = &buf.data[0]; 1211 1212 if (AcpiFormatException(excep, &buf.buffer) == AE_OK) 1213 return(buf.buffer.Pointer); 1214 return("(error formatting exception)"); 1215 } 1216 1217 char * 1218 acpi_name(ACPI_HANDLE handle) 1219 { 1220 static struct acpi_staticbuf buf; 1221 1222 ACPI_ASSERTLOCK; 1223 1224 buf.buffer.Length = 512; 1225 buf.buffer.Pointer = &buf.data[0]; 1226 1227 if (AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer) == AE_OK) 1228 return(buf.buffer.Pointer); 1229 return("(unknown path)"); 1230 } 1231 1232 /* 1233 * Debugging/bug-avoidance. Avoid trying to fetch info on various 1234 * parts of the namespace. 1235 */ 1236 int 1237 acpi_avoid(ACPI_HANDLE handle) 1238 { 1239 char *cp, *np; 1240 int len; 1241 1242 np = acpi_name(handle); 1243 if (*np == '\\') 1244 np++; 1245 if ((cp = getenv("debug.acpi.avoid")) == NULL) 1246 return(0); 1247 1248 /* scan the avoid list checking for a match */ 1249 for (;;) { 1250 while ((*cp != 0) && isspace(*cp)) 1251 cp++; 1252 if (*cp == 0) 1253 break; 1254 len = 0; 1255 while ((cp[len] != 0) && !isspace(cp[len])) 1256 len++; 1257 if (!strncmp(cp, np, len)) { 1258 DEBUG_PRINT(TRACE_OBJECTS, ("avoiding '%s'\n", np)); 1259 return(1); 1260 } 1261 cp += len; 1262 } 1263 return(0); 1264 } 1265 1266 /* 1267 * Debugging/bug-avoidance. Disable ACPI subsystem components. 1268 */ 1269 int 1270 acpi_disabled(char *subsys) 1271 { 1272 char *cp; 1273 int len; 1274 1275 if ((cp = getenv("debug.acpi.disable")) == NULL) 1276 return(0); 1277 if (!strcmp(cp, "all")) 1278 return(1); 1279 1280 /* scan the disable list checking for a match */ 1281 for (;;) { 1282 while ((*cp != 0) && isspace(*cp)) 1283 cp++; 1284 if (*cp == 0) 1285 break; 1286 len = 0; 1287 while ((cp[len] != 0) && !isspace(cp[len])) 1288 len++; 1289 if (!strncmp(cp, subsys, len)) { 1290 DEBUG_PRINT(TRACE_OBJECTS, ("disabled '%s'\n", subsys)); 1291 return(1); 1292 } 1293 cp += len; 1294 } 1295 return(0); 1296 } 1297 1298 /* 1299 * Control interface. 1300 * 1301 * We multiplex ioctls for all participating ACPI devices here. Individual 1302 * drivers wanting to be accessible via /dev/acpi should use the register/deregister 1303 * interface to make their handlers visible. 1304 */ 1305 struct acpi_ioctl_hook 1306 { 1307 TAILQ_ENTRY(acpi_ioctl_hook) link; 1308 u_long cmd; 1309 int (* fn)(u_long cmd, caddr_t addr, void *arg); 1310 void *arg; 1311 }; 1312 1313 static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 1314 static int acpi_ioctl_hooks_initted; 1315 1316 /* 1317 * Register an ioctl handler. 1318 */ 1319 int 1320 acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg) 1321 { 1322 struct acpi_ioctl_hook *hp; 1323 1324 if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 1325 return(ENOMEM); 1326 hp->cmd = cmd; 1327 hp->fn = fn; 1328 hp->arg = arg; 1329 if (acpi_ioctl_hooks_initted == 0) { 1330 TAILQ_INIT(&acpi_ioctl_hooks); 1331 acpi_ioctl_hooks_initted = 1; 1332 } 1333 TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 1334 return(0); 1335 } 1336 1337 /* 1338 * Deregister an ioctl handler. 1339 */ 1340 void 1341 acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg)) 1342 { 1343 struct acpi_ioctl_hook *hp; 1344 1345 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 1346 if ((hp->cmd == cmd) && (hp->fn == fn)) 1347 break; 1348 1349 if (hp != NULL) { 1350 TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 1351 free(hp, M_ACPIDEV); 1352 } 1353 } 1354 1355 static int 1356 acpiopen(dev_t dev, int flag, int fmt, struct proc *p) 1357 { 1358 return(0); 1359 } 1360 1361 static int 1362 acpiclose(dev_t dev, int flag, int fmt, struct proc *p) 1363 { 1364 return(0); 1365 } 1366 1367 static int 1368 acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 1369 { 1370 struct acpi_softc *sc; 1371 struct acpi_ioctl_hook *hp; 1372 int error, xerror, state; 1373 1374 ACPI_LOCK; 1375 1376 error = state = 0; 1377 sc = dev->si_drv1; 1378 1379 /* 1380 * Scan the list of registered ioctls, looking for handlers. 1381 */ 1382 if (acpi_ioctl_hooks_initted) { 1383 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 1384 if (hp->cmd == cmd) { 1385 xerror = hp->fn(cmd, addr, hp->arg); 1386 if (xerror != 0) 1387 error = xerror; 1388 goto out; 1389 } 1390 } 1391 } 1392 1393 /* 1394 * Core system ioctls. 1395 */ 1396 switch (cmd) { 1397 case ACPIIO_ENABLE: 1398 if (ACPI_FAILURE(acpi_Enable(sc))) 1399 error = ENXIO; 1400 break; 1401 1402 case ACPIIO_DISABLE: 1403 if (ACPI_FAILURE(acpi_Disable(sc))) 1404 error = ENXIO; 1405 break; 1406 1407 case ACPIIO_SETSLPSTATE: 1408 if (!sc->acpi_enabled) { 1409 error = ENXIO; 1410 break; 1411 } 1412 state = *(int *)addr; 1413 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) { 1414 acpi_SetSleepState(sc, state); 1415 } else { 1416 error = EINVAL; 1417 } 1418 break; 1419 1420 default: 1421 if (error == 0) 1422 error = EINVAL; 1423 break; 1424 } 1425 1426 out: 1427 ACPI_UNLOCK; 1428 return(error); 1429 } 1430 1431 static int 1432 acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 1433 { 1434 char sleep_state[10]; 1435 int error; 1436 u_int new_state, old_state; 1437 1438 old_state = *(u_int *)oidp->oid_arg1; 1439 if (old_state > ACPI_S_STATES_MAX) { 1440 strcpy(sleep_state, "unknown"); 1441 } else { 1442 strncpy(sleep_state, sleep_state_names[old_state], 1443 sizeof(sleep_state_names[old_state])); 1444 } 1445 error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 1446 if (error == 0 && req->newptr != NULL) { 1447 for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX; new_state++) { 1448 if (strncmp(sleep_state, sleep_state_names[new_state], 1449 sizeof(sleep_state)) == 0) 1450 break; 1451 } 1452 if ((new_state != old_state) && (new_state <= ACPI_S_STATES_MAX)) { 1453 *(u_int *)oidp->oid_arg1 = new_state; 1454 } else { 1455 error = EINVAL; 1456 } 1457 } 1458 return(error); 1459 } 1460 1461 #ifdef ACPI_DEBUG 1462 /* 1463 * Support for parsing debug options from the kernel environment. 1464 * 1465 * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 1466 * by specifying the names of the bits in the debug.acpi.layer and 1467 * debug.acpi.level environment variables. Bits may be unset by 1468 * prefixing the bit name with !. 1469 */ 1470 struct debugtag 1471 { 1472 char *name; 1473 UINT32 value; 1474 }; 1475 1476 static struct debugtag dbg_layer[] = { 1477 {"ACPI_UTILITIES", ACPI_UTILITIES}, 1478 {"ACPI_HARDWARE", ACPI_HARDWARE}, 1479 {"ACPI_EVENTS", ACPI_EVENTS}, 1480 {"ACPI_TABLES", ACPI_TABLES}, 1481 {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 1482 {"ACPI_PARSER", ACPI_PARSER}, 1483 {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 1484 {"ACPI_EXECUTER", ACPI_EXECUTER}, 1485 {"ACPI_RESOURCES", ACPI_RESOURCES}, 1486 {"ACPI_POWER", ACPI_POWER}, 1487 {"ACPI_BUS", ACPI_BUS}, 1488 {"ACPI_POWER", ACPI_POWER}, 1489 {"ACPI_EC", ACPI_EC}, 1490 {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 1491 {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 1492 {"ACPI_BATTERY", ACPI_BATTERY}, 1493 {"ACPI_BUTTON", ACPI_BUTTON}, 1494 {"ACPI_SYSTEM", ACPI_SYSTEM}, 1495 {"ACPI_THERMAL", ACPI_THERMAL}, 1496 {"ACPI_DEBUGGER", ACPI_DEBUGGER}, 1497 {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 1498 {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 1499 {NULL, 0} 1500 }; 1501 1502 static struct debugtag dbg_level[] = { 1503 {"ACPI_OK", ACPI_OK}, 1504 {"ACPI_INFO", ACPI_INFO}, 1505 {"ACPI_WARN", ACPI_WARN}, 1506 {"ACPI_ERROR", ACPI_ERROR}, 1507 {"ACPI_FATAL", ACPI_FATAL}, 1508 {"ACPI_DEBUG_OBJECT", ACPI_DEBUG_OBJECT}, 1509 {"ACPI_ALL", ACPI_ALL}, 1510 {"TRACE_THREADS", TRACE_THREADS}, 1511 {"TRACE_PARSE", TRACE_PARSE}, 1512 {"TRACE_DISPATCH", TRACE_DISPATCH}, 1513 {"TRACE_LOAD", TRACE_LOAD}, 1514 {"TRACE_EXEC", TRACE_EXEC}, 1515 {"TRACE_NAMES", TRACE_NAMES}, 1516 {"TRACE_OPREGION", TRACE_OPREGION}, 1517 {"TRACE_BFIELD", TRACE_BFIELD}, 1518 {"TRACE_TRASH", TRACE_TRASH}, 1519 {"TRACE_TABLES", TRACE_TABLES}, 1520 {"TRACE_FUNCTIONS", TRACE_FUNCTIONS}, 1521 {"TRACE_VALUES", TRACE_VALUES}, 1522 {"TRACE_OBJECTS", TRACE_OBJECTS}, 1523 {"TRACE_ALLOCATIONS", TRACE_ALLOCATIONS}, 1524 {"TRACE_RESOURCES", TRACE_RESOURCES}, 1525 {"TRACE_IO", TRACE_IO}, 1526 {"TRACE_INTERRUPTS", TRACE_INTERRUPTS}, 1527 {"TRACE_USER_REQUESTS", TRACE_USER_REQUESTS}, 1528 {"TRACE_PACKAGE", TRACE_PACKAGE}, 1529 {"TRACE_MUTEX", TRACE_MUTEX}, 1530 {"TRACE_INIT", TRACE_INIT}, 1531 {"TRACE_ALL", TRACE_ALL}, 1532 {"VERBOSE_AML_DISASSEMBLE", VERBOSE_AML_DISASSEMBLE}, 1533 {"VERBOSE_INFO", VERBOSE_INFO}, 1534 {"VERBOSE_TABLES", VERBOSE_TABLES}, 1535 {"VERBOSE_EVENTS", VERBOSE_EVENTS}, 1536 {"VERBOSE_ALL", VERBOSE_ALL}, 1537 {NULL, 0} 1538 }; 1539 1540 static void 1541 acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 1542 { 1543 char *ep; 1544 int i, l; 1545 int set; 1546 1547 while (*cp) { 1548 if (isspace(*cp)) { 1549 cp++; 1550 continue; 1551 } 1552 ep = cp; 1553 while (*ep && !isspace(*ep)) 1554 ep++; 1555 if (*cp == '!') { 1556 set = 0; 1557 cp++; 1558 if (cp == ep) 1559 continue; 1560 } else { 1561 set = 1; 1562 } 1563 l = ep - cp; 1564 for (i = 0; tag[i].name != NULL; i++) { 1565 if (!strncmp(cp, tag[i].name, l)) { 1566 if (set) { 1567 *flag |= tag[i].value; 1568 } else { 1569 *flag &= ~tag[i].value; 1570 } 1571 printf("ACPI_DEBUG: set '%s'\n", tag[i].name); 1572 } 1573 } 1574 cp = ep; 1575 } 1576 } 1577 1578 static void 1579 acpi_set_debugging(void *junk) 1580 { 1581 char *cp; 1582 1583 AcpiDbgLayer = 0; 1584 AcpiDbgLevel = 0; 1585 if ((cp = getenv("debug.acpi.layer")) != NULL) 1586 acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer); 1587 if ((cp = getenv("debug.acpi.level")) != NULL) 1588 acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel); 1589 1590 printf("ACPI debug layer 0x%x debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel); 1591 } 1592 SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL); 1593 #endif 1594 1595 /* 1596 * ACPI Battery Abstruction Layer 1597 */ 1598 1599 struct acpi_batteries { 1600 TAILQ_ENTRY(acpi_batteries) link; 1601 struct acpi_battdesc battdesc; 1602 }; 1603 1604 static TAILQ_HEAD(,acpi_batteries) acpi_batteries; 1605 static int acpi_batteries_initted = 0; 1606 static int acpi_batteries_units = 0; 1607 static struct acpi_battinfo acpi_battery_battinfo; 1608 1609 static int 1610 acpi_battery_get_units(void) 1611 { 1612 1613 return (acpi_batteries_units); 1614 } 1615 1616 static int 1617 acpi_battery_get_battdesc(int logical_unit, struct acpi_battdesc *battdesc) 1618 { 1619 int i; 1620 struct acpi_batteries *bp; 1621 1622 if (logical_unit < 0 || logical_unit >= acpi_batteries_units) { 1623 return (ENXIO); 1624 } 1625 1626 i = 0; 1627 TAILQ_FOREACH(bp, &acpi_batteries, link) { 1628 if (logical_unit == i) { 1629 battdesc->type = bp->battdesc.type; 1630 battdesc->phys_unit = bp->battdesc.phys_unit; 1631 return (0); 1632 } 1633 i++; 1634 } 1635 1636 return (ENXIO); 1637 } 1638 1639 static int 1640 acpi_battery_get_battinfo(int unit, struct acpi_battinfo *battinfo) 1641 { 1642 int error; 1643 struct acpi_battdesc battdesc; 1644 1645 error = 0; 1646 if (unit == -1) { 1647 error = acpi_cmbat_get_battinfo(-1, battinfo); 1648 goto out; 1649 } else { 1650 if ((error = acpi_battery_get_battdesc(unit, &battdesc)) != 0) { 1651 goto out; 1652 } 1653 switch (battdesc.type) { 1654 case ACPI_BATT_TYPE_CMBAT: 1655 error = acpi_cmbat_get_battinfo(battdesc.phys_unit, 1656 battinfo); 1657 break; 1658 default: 1659 error = ENXIO; 1660 break; 1661 } 1662 } 1663 out: 1664 return (error); 1665 } 1666 1667 static int 1668 acpi_battery_ioctl(u_long cmd, caddr_t addr, void *arg) 1669 { 1670 int error; 1671 int logical_unit; 1672 union acpi_battery_ioctl_arg *ioctl_arg; 1673 1674 ioctl_arg = (union acpi_battery_ioctl_arg *)addr; 1675 error = 0; 1676 switch (cmd) { 1677 case ACPIIO_BATT_GET_UNITS: 1678 *(int *)addr = acpi_battery_get_units(); 1679 break; 1680 1681 case ACPIIO_BATT_GET_BATTDESC: 1682 logical_unit = ioctl_arg->unit; 1683 error = acpi_battery_get_battdesc(logical_unit, &ioctl_arg->battdesc); 1684 break; 1685 1686 case ACPIIO_BATT_GET_BATTINFO: 1687 logical_unit = ioctl_arg->unit; 1688 error = acpi_battery_get_battinfo(logical_unit, 1689 &ioctl_arg->battinfo); 1690 break; 1691 1692 default: 1693 error = EINVAL; 1694 break; 1695 } 1696 1697 return (error); 1698 } 1699 1700 static int 1701 acpi_battery_sysctl(SYSCTL_HANDLER_ARGS) 1702 { 1703 int val; 1704 int error; 1705 1706 acpi_battery_get_battinfo(-1, &acpi_battery_battinfo); 1707 val = *(u_int *)oidp->oid_arg1; 1708 error = sysctl_handle_int(oidp, &val, 0, req); 1709 return (error); 1710 } 1711 1712 static int 1713 acpi_battery_init(void) 1714 { 1715 device_t dev; 1716 struct acpi_softc *sc; 1717 int error; 1718 1719 if ((dev = devclass_get_device(acpi_devclass, 0)) == NULL) { 1720 return (ENXIO); 1721 } 1722 if ((sc = device_get_softc(dev)) == NULL) { 1723 return (ENXIO); 1724 } 1725 1726 error = 0; 1727 1728 TAILQ_INIT(&acpi_batteries); 1729 acpi_batteries_initted = 1; 1730 1731 if ((error = acpi_register_ioctl(ACPIIO_BATT_GET_UNITS, 1732 acpi_battery_ioctl, NULL)) != 0) { 1733 return (error); 1734 } 1735 if ((error = acpi_register_ioctl(ACPIIO_BATT_GET_BATTDESC, 1736 acpi_battery_ioctl, NULL)) != 0) { 1737 return (error); 1738 } 1739 if ((error = acpi_register_ioctl(ACPIIO_BATT_GET_BATTINFO, 1740 acpi_battery_ioctl, NULL)) != 0) { 1741 return (error); 1742 } 1743 1744 sysctl_ctx_init(&sc->acpi_battery_sysctl_ctx); 1745 sc->acpi_battery_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_battery_sysctl_ctx, 1746 SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 1747 OID_AUTO, "battery", CTLFLAG_RD, 0, ""); 1748 SYSCTL_ADD_PROC(&sc->acpi_battery_sysctl_ctx, 1749 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree), 1750 OID_AUTO, "life", CTLTYPE_INT | CTLFLAG_RD, 1751 &acpi_battery_battinfo.cap, 0, acpi_battery_sysctl, "I", ""); 1752 SYSCTL_ADD_PROC(&sc->acpi_battery_sysctl_ctx, 1753 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree), 1754 OID_AUTO, "time", CTLTYPE_INT | CTLFLAG_RD, 1755 &acpi_battery_battinfo.min, 0, acpi_battery_sysctl, "I", ""); 1756 SYSCTL_ADD_PROC(&sc->acpi_battery_sysctl_ctx, 1757 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree), 1758 OID_AUTO, "state", CTLTYPE_INT | CTLFLAG_RD, 1759 &acpi_battery_battinfo.state, 0, acpi_battery_sysctl, "I", ""); 1760 SYSCTL_ADD_INT(&sc->acpi_battery_sysctl_ctx, 1761 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree), 1762 OID_AUTO, "units", CTLFLAG_RD, &acpi_batteries_units, 0, ""); 1763 1764 return (error); 1765 } 1766 1767 int 1768 acpi_battery_register(int type, int phys_unit) 1769 { 1770 int error; 1771 struct acpi_batteries *bp; 1772 1773 error = 0; 1774 if ((bp = malloc(sizeof(*bp), M_ACPIDEV, M_NOWAIT)) == NULL) { 1775 return(ENOMEM); 1776 } 1777 1778 bp->battdesc.type = type; 1779 bp->battdesc.phys_unit = phys_unit; 1780 if (acpi_batteries_initted == 0) { 1781 if ((error = acpi_battery_init()) != 0) { 1782 free(bp, M_ACPIDEV); 1783 return(error); 1784 } 1785 } 1786 1787 TAILQ_INSERT_TAIL(&acpi_batteries, bp, link); 1788 acpi_batteries_units++; 1789 1790 return(0); 1791 } 1792