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
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
33 #include <sys/param.h>
34 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
41 #include <sys/ioccom.h>
42 #include <sys/reboot.h>
43 #include <sys/sysctl.h>
44 #include <sys/ctype.h>
45 #include <sys/linker.h>
46 #include <sys/power.h>
48 #include <machine/clock.h>
49 #include <machine/resource.h>
51 #include <isa/isavar.h>
55 #include <dev/acpica/acpica_support.h>
57 #include <dev/acpica/acpivar.h>
58 #include <dev/acpica/acpiio.h>
60 MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
63 * Hooks for the ACPI CA debugging infrastructure
65 #define _COMPONENT ACPI_BUS
66 ACPI_MODULE_NAME("ACPI")
72 static d_open_t acpiopen;
73 static d_close_t acpiclose;
74 static d_ioctl_t acpiioctl;
76 #define CDEV_MAJOR 152
77 static struct cdevsw acpi_cdevsw = {
93 static const char* sleep_state_names[] = {
94 "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
96 /* this has to be static, as the softc is gone when we need it */
97 static int acpi_off_state = ACPI_STATE_S5;
99 struct mtx acpi_mutex;
101 static int acpi_modevent(struct module *mod, int event, void *junk);
102 static void acpi_identify(driver_t *driver, device_t parent);
103 static int acpi_probe(device_t dev);
104 static int acpi_attach(device_t dev);
105 static device_t acpi_add_child(device_t bus, int order, const char *name, int unit);
106 static int acpi_print_child(device_t bus, device_t child);
107 static int acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result);
108 static int acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value);
109 static int acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start,
111 static int acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp,
113 static struct resource *acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
114 u_long start, u_long end, u_long count, u_int flags);
115 static int acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r);
116 static u_int32_t acpi_isa_get_logicalid(device_t dev);
117 static int acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids);
119 static void acpi_probe_children(device_t bus);
120 static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status);
122 static void acpi_shutdown_pre_sync(void *arg, int howto);
123 static void acpi_shutdown_final(void *arg, int howto);
125 static void acpi_enable_fixed_events(struct acpi_softc *sc);
127 static void acpi_system_eventhandler_sleep(void *arg, int state);
128 static void acpi_system_eventhandler_wakeup(void *arg, int state);
129 static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
131 static int acpi_pm_func(u_long cmd, void *arg, ...);
133 static device_method_t acpi_methods[] = {
134 /* Device interface */
135 DEVMETHOD(device_identify, acpi_identify),
136 DEVMETHOD(device_probe, acpi_probe),
137 DEVMETHOD(device_attach, acpi_attach),
138 DEVMETHOD(device_shutdown, bus_generic_shutdown),
139 DEVMETHOD(device_suspend, bus_generic_suspend),
140 DEVMETHOD(device_resume, bus_generic_resume),
143 DEVMETHOD(bus_add_child, acpi_add_child),
144 DEVMETHOD(bus_print_child, acpi_print_child),
145 DEVMETHOD(bus_read_ivar, acpi_read_ivar),
146 DEVMETHOD(bus_write_ivar, acpi_write_ivar),
147 DEVMETHOD(bus_set_resource, acpi_set_resource),
148 DEVMETHOD(bus_get_resource, acpi_get_resource),
149 DEVMETHOD(bus_alloc_resource, acpi_alloc_resource),
150 DEVMETHOD(bus_release_resource, acpi_release_resource),
151 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
152 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
153 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
154 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
155 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
158 DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe),
163 static driver_t acpi_driver = {
166 sizeof(struct acpi_softc),
169 static devclass_t acpi_devclass;
170 DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
171 MODULE_VERSION(acpi, 100);
173 SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, "");
174 SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, "");
175 static int acpi_ca_version = ACPI_CA_VERSION;
176 SYSCTL_INT(_debug, OID_AUTO, acpi_ca_version, CTLFLAG_RD, &acpi_ca_version, 0, "");
179 * ACPI can only be loaded as a module by the loader; activating it after
180 * system bootstrap time is not useful, and can be fatal to the system.
181 * It also cannot be unloaded, since the entire system bus heirarchy hangs off it.
184 acpi_modevent(struct module *mod, int event, void *junk)
192 if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
202 * Detect ACPI, perform early initialisation
205 acpi_identify(driver_t *driver, device_t parent)
213 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
216 printf("Don't load this driver from userland!!\n");
221 * Check that we haven't been disabled with a hint.
223 if (!resource_int_value("acpi", 0, "disabled", &error) &&
228 * Make sure we're not being doubly invoked.
230 if (device_find_child(parent, "acpi", 0) != NULL)
233 /* initialise the ACPI mutex */
234 mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
237 * Start up the ACPI CA subsystem.
240 debugpoint = getenv("debug.acpi.debugger");
242 if (!strcmp(debugpoint, "init"))
243 acpi_EnterDebugger();
247 if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
248 printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
252 debugpoint = getenv("debug.acpi.debugger");
254 if (!strcmp(debugpoint, "tables"))
255 acpi_EnterDebugger();
260 if (ACPI_FAILURE(error = AcpiLoadTables())) {
261 printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
266 * Attach the actual ACPI device.
268 if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
269 device_printf(parent, "ACPI: could not attach\n");
275 * Fetch some descriptive data from ACPI to put in our attach message
278 acpi_probe(device_t dev)
280 ACPI_TABLE_HEADER th;
285 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
287 if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
288 power_pm_get_type() != POWER_PM_TYPE_ACPI) {
289 device_printf(dev, "Other PM system enabled.\n");
295 if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
296 device_printf(dev, "couldn't get XSDT header: %s\n", AcpiFormatException(status));
299 sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId);
300 device_set_desc_copy(dev, buf);
308 acpi_attach(device_t dev)
310 struct acpi_softc *sc;
319 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
321 sc = device_get_softc(dev);
322 bzero(sc, sizeof(*sc));
326 debugpoint = getenv("debug.acpi.debugger");
328 if (!strcmp(debugpoint, "spaces"))
329 acpi_EnterDebugger();
335 * Install the default address space handlers.
338 if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
339 ACPI_ADR_SPACE_SYSTEM_MEMORY,
340 ACPI_DEFAULT_HANDLER,
342 device_printf(dev, "could not initialise SystemMemory handler: %s\n", AcpiFormatException(status));
345 if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
346 ACPI_ADR_SPACE_SYSTEM_IO,
347 ACPI_DEFAULT_HANDLER,
349 device_printf(dev, "could not initialise SystemIO handler: %s\n", AcpiFormatException(status));
352 if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
353 ACPI_ADR_SPACE_PCI_CONFIG,
354 ACPI_DEFAULT_HANDLER,
356 device_printf(dev, "could not initialise PciConfig handler: %s\n", AcpiFormatException(status));
361 * Bring ACPI fully online.
363 * Note that some systems (specifically, those with namespace evaluation issues
364 * that require the avoidance of parts of the namespace) must avoid running _INI
365 * and _STA on everything, as well as dodging the final object init pass.
367 * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
369 * XXX We should arrange for the object init pass after we have attached all our
370 * child devices, but on many systems it works here.
373 debugpoint = getenv("debug.acpi.debugger");
375 if (!strcmp(debugpoint, "enable"))
376 acpi_EnterDebugger();
381 if (testenv("debug.acpi.avoid"))
382 flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
383 if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
384 device_printf(dev, "could not enable ACPI: %s\n", AcpiFormatException(status));
388 if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
389 device_printf(dev, "could not initialize ACPI objects: %s\n", AcpiFormatException(status));
394 * Setup our sysctl tree.
396 * XXX: This doesn't check to make sure that none of these fail.
398 sysctl_ctx_init(&sc->acpi_sysctl_ctx);
399 sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
400 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
401 device_get_name(dev), CTLFLAG_RD, 0, "");
402 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
403 OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
404 &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
405 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
406 OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
407 &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
408 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
409 OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
410 &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
411 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
412 OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
413 &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
414 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
415 OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
416 &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
417 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
418 OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
419 &sc->acpi_sleep_delay, 0, "sleep delay");
420 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
421 OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
422 &sc->acpi_s4bios, 0, "S4BIOS mode");
423 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
424 OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
425 &sc->acpi_verbose, 0, "verbose mode");
426 sc->acpi_sleep_delay = 0;
429 sc->acpi_verbose = 1;
432 * Dispatch the default sleep state to devices.
433 * TBD: should be configured from userland policy manager.
435 sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
436 sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
437 sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
438 sc->acpi_standby_sx = ACPI_STATE_S1;
439 sc->acpi_suspend_sx = ACPI_STATE_S3;
441 acpi_enable_fixed_events(sc);
444 * Scan the namespace and attach/initialise children.
447 debugpoint = getenv("debug.acpi.debugger");
449 if (!strcmp(debugpoint, "probe"))
450 acpi_EnterDebugger();
456 * Register our shutdown handlers
458 EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST);
459 EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST);
462 * Register our acpi event handlers.
463 * XXX should be configurable eg. via userland policy manager.
465 EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST);
466 EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST);
469 * Flag our initial states.
471 sc->acpi_enabled = 1;
472 sc->acpi_sstate = ACPI_STATE_S0;
473 sc->acpi_sleep_disabled = 0;
476 * Create the control device
478 sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, 0, 5, 0660, "acpi");
479 sc->acpi_dev_t->si_drv1 = sc;
482 debugpoint = getenv("debug.acpi.debugger");
484 if (!strcmp(debugpoint, "running"))
485 acpi_EnterDebugger();
490 #if defined(ACPI_MAX_THREADS) && ACPI_MAX_THREADS > 0
491 if ((error = acpi_task_thread_init())) {
496 if ((error = acpi_machdep_init(dev))) {
500 /* Register ACPI again to pass the correct argument of pm_func. */
501 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
503 if (!acpi_disabled("bus"))
504 acpi_probe_children(dev);
514 * Handle a new device being added
517 acpi_add_child(device_t bus, int order, const char *name, int unit)
519 struct acpi_device *ad;
522 if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL)
524 bzero(ad, sizeof(*ad));
526 resource_list_init(&ad->ad_rl);
528 child = device_add_child_ordered(bus, order, name, unit);
530 device_set_ivars(child, ad);
535 acpi_print_child(device_t bus, device_t child)
537 struct acpi_device *adev = device_get_ivars(child);
538 struct resource_list *rl = &adev->ad_rl;
541 retval += bus_print_child_header(bus, child);
542 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
543 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
544 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
545 retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld");
546 retval += bus_print_child_footer(bus, child);
553 * Handle per-device ivars
556 acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
558 struct acpi_device *ad;
560 if ((ad = device_get_ivars(child)) == NULL) {
561 printf("device has no ivars\n");
567 case ACPI_IVAR_HANDLE:
568 *(ACPI_HANDLE *)result = ad->ad_handle;
570 case ACPI_IVAR_MAGIC:
571 *(int *)result = ad->ad_magic;
573 case ACPI_IVAR_PRIVATE:
574 *(void **)result = ad->ad_private;
577 /* ISA compatibility */
578 case ISA_IVAR_VENDORID:
579 case ISA_IVAR_SERIAL:
580 case ISA_IVAR_COMPATID:
584 case ISA_IVAR_LOGICALID:
585 *(int *)result = acpi_isa_get_logicalid(child);
589 panic("bad ivar read request (%d)", index);
596 acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
598 struct acpi_device *ad;
600 if ((ad = device_get_ivars(child)) == NULL) {
601 printf("device has no ivars\n");
607 case ACPI_IVAR_HANDLE:
608 ad->ad_handle = (ACPI_HANDLE)value;
610 case ACPI_IVAR_MAGIC:
611 ad->ad_magic = (int )value;
613 case ACPI_IVAR_PRIVATE:
614 ad->ad_private = (void *)value;
618 panic("bad ivar write request (%d)", index);
625 * Handle child resource allocation/removal
628 acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
630 struct acpi_device *ad = device_get_ivars(child);
631 struct resource_list *rl = &ad->ad_rl;
633 resource_list_add(rl, type, rid, start, start + count -1, count);
639 acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
641 struct acpi_device *ad = device_get_ivars(child);
642 struct resource_list *rl = &ad->ad_rl;
643 struct resource_list_entry *rle;
645 rle = resource_list_find(rl, type, rid);
650 *startp = rle->start;
652 *countp = rle->count;
657 static struct resource *
658 acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
659 u_long start, u_long end, u_long count, u_int flags)
661 struct acpi_device *ad = device_get_ivars(child);
662 struct resource_list *rl = &ad->ad_rl;
664 return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags));
668 acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
670 struct acpi_device *ad = device_get_ivars(child);
671 struct resource_list *rl = &ad->ad_rl;
673 return(resource_list_release(rl, bus, child, type, rid, r));
677 * Handle ISA-like devices probing for a PnP ID to match.
679 #define PNP_EISAID(s) \
680 ((((s[0] - '@') & 0x1f) << 2) \
681 | (((s[1] - '@') & 0x18) >> 3) \
682 | (((s[1] - '@') & 0x07) << 13) \
683 | (((s[2] - '@') & 0x1f) << 8) \
684 | (PNP_HEXTONUM(s[4]) << 16) \
685 | (PNP_HEXTONUM(s[3]) << 20) \
686 | (PNP_HEXTONUM(s[6]) << 24) \
687 | (PNP_HEXTONUM(s[5]) << 28))
690 acpi_isa_get_logicalid(device_t dev)
693 ACPI_DEVICE_INFO devinfo;
697 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
702 /* fetch and validate the HID */
703 if ((h = acpi_get_handle(dev)) == NULL)
705 if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
707 if (!(devinfo.Valid & ACPI_VALID_HID))
710 pnpid = PNP_EISAID(devinfo.HardwareId);
717 acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
722 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
725 * ISA-style drivers attached to ACPI may persist and
726 * probe manually if we return ENOENT. We never want
727 * that to happen, so don't ever return it.
731 /* scan the supplied IDs for a match */
732 pnpid = acpi_isa_get_logicalid(child);
733 while (ids && ids->ip_id) {
734 if (pnpid == ids->ip_id) {
741 return_VALUE(result);
745 * Scan relevant portions of the ACPI namespace and attach child devices.
747 * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and \_SB_ scopes,
748 * and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
751 acpi_probe_children(device_t bus)
754 static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
757 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
761 * Create any static children by calling device identify methods.
763 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
764 bus_generic_probe(bus);
767 * Scan the namespace and insert placeholders for all the devices that
770 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
771 * we want to create nodes for all devices, not just those that are currently
772 * present. (This assumes that we don't want to create/remove devices as they
773 * appear, which might be smarter.)
775 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
776 for (i = 0; scopes[i] != NULL; i++)
777 if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent)))
778 AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL);
781 * Scan all of the child devices we have created and let them probe/attach.
783 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
784 bus_generic_attach(bus);
787 * Some of these children may have attached others as part of their attach
788 * process (eg. the root PCI bus driver), so rescan.
790 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
791 bus_generic_attach(bus);
793 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
798 * Evaluate a child device and determine whether we might attach a device to
802 acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
804 ACPI_OBJECT_TYPE type;
805 device_t child, bus = (device_t)context;
807 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
810 * Skip this device if we think we'll have trouble with it.
812 if (acpi_avoid(handle))
813 return_ACPI_STATUS(AE_OK);
815 if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
817 case ACPI_TYPE_DEVICE:
818 case ACPI_TYPE_PROCESSOR:
819 case ACPI_TYPE_THERMAL:
820 case ACPI_TYPE_POWER:
821 if (acpi_disabled("children"))
824 * Create a placeholder device for this node. Sort the placeholder
825 * so that the probe/attach passes will run breadth-first.
827 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", acpi_name(handle)));
828 child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
831 acpi_set_handle(child, handle);
834 * Check that the device is present. If it's not present,
835 * leave it disabled (so that we have a device_t attached to
836 * the handle, but we don't probe it).
838 if ((type == ACPI_TYPE_DEVICE) && (!acpi_DeviceIsPresent(child))) {
839 device_disable(child);
844 * Get the device's resource settings and attach them.
845 * Note that if the device has _PRS but no _CRS, we need
846 * to decide when it's appropriate to try to configure the
847 * device. Ignore the return value here; it's OK for the
848 * device not to have any resources.
850 acpi_parse_resources(child, handle, &acpi_res_parse_set);
852 /* if we're debugging, probe/attach now rather than later */
853 ACPI_DEBUG_EXEC(device_probe_and_attach(child));
857 return_ACPI_STATUS(AE_OK);
861 acpi_shutdown_pre_sync(void *arg, int howto)
867 * Disable all ACPI events before soft off, otherwise the system
868 * will be turned on again on some laptops.
870 * XXX this should probably be restricted to masking some events just
871 * before powering down, since we may still need ACPI during the
874 acpi_Disable((struct acpi_softc *)arg);
878 acpi_shutdown_final(void *arg, int howto)
884 if (howto & RB_POWEROFF) {
885 printf("Power system off using ACPI...\n");
886 if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(acpi_off_state))) {
887 printf("AcpiEnterSleepStatePrep failed - %s\n",
888 AcpiFormatException(status));
891 if (ACPI_FAILURE(status = AcpiEnterSleepState(acpi_off_state))) {
892 printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
895 printf("ACPI power-off failed - timeout\n");
898 printf("Terminate ACPI\n");
904 acpi_enable_fixed_events(struct acpi_softc *sc)
906 static int first_time = 1;
907 #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n"
911 /* Enable and clear fixed events and install handlers. */
912 if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) {
913 AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED, 0);
914 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED);
915 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
916 acpi_eventhandler_power_button_for_sleep, sc);
918 device_printf(sc->acpi_dev, MSGFORMAT, "power");
921 if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) {
922 AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED, 0);
923 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED);
924 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
925 acpi_eventhandler_sleep_button_for_sleep, sc);
927 device_printf(sc->acpi_dev, MSGFORMAT, "sleep");
935 * Returns true if the device is actually present and should
936 * be attached to. This requires the present, enabled, UI-visible
937 * and diagnostics-passed bits to be set.
940 acpi_DeviceIsPresent(device_t dev)
943 ACPI_DEVICE_INFO devinfo;
948 if ((h = acpi_get_handle(dev)) == NULL)
950 if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
952 /* if no _STA method, must be present */
953 if (!(devinfo.Valid & ACPI_VALID_STA))
955 /* return true for 'present' and 'functioning' */
956 if ((devinfo.CurrentStatus & 0x9) == 0x9)
962 * Returns true if the battery is actually present and inserted.
965 acpi_BatteryIsPresent(device_t dev)
968 ACPI_DEVICE_INFO devinfo;
973 if ((h = acpi_get_handle(dev)) == NULL)
975 if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
977 /* if no _STA method, must be present */
978 if (!(devinfo.Valid & ACPI_VALID_STA))
980 /* return true for 'present' and 'functioning' */
981 if ((devinfo.CurrentStatus & 0x19) == 0x19)
987 * Match a HID string against a device
990 acpi_MatchHid(device_t dev, char *hid)
993 ACPI_DEVICE_INFO devinfo;
1001 if ((h = acpi_get_handle(dev)) == NULL)
1003 if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
1005 if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId))
1007 if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &cid)))
1009 if (cid == PNP_EISAID(hid))
1015 * Return the handle of a named object within our scope, ie. that of (parent)
1016 * or one if its parents.
1019 acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1026 /* walk back up the tree to the root */
1028 if (ACPI_SUCCESS(status = AcpiGetHandle(parent, path, &r))) {
1032 if (status != AE_NOT_FOUND)
1034 if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1035 return(AE_NOT_FOUND);
1041 * Allocate a buffer with a preset data size.
1044 acpi_AllocBuffer(int size)
1048 if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1051 buf->Pointer = (void *)(buf + 1);
1056 * Evaluate a path that should return an integer.
1059 acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number)
1068 handle = ACPI_ROOT_OBJECT;
1071 * Assume that what we've been pointed at is an Integer object, or
1072 * a method that will return an Integer.
1074 buf.Pointer = ¶m;
1075 buf.Length = sizeof(param);
1076 if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
1077 if (param.Type == ACPI_TYPE_INTEGER) {
1078 *number = param.Integer.Value;
1085 * In some applications, a method that's expected to return an Integer
1086 * may instead return a Buffer (probably to simplify some internal
1087 * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer,
1088 * convert it into an Integer as best we can.
1092 if (error == AE_BUFFER_OVERFLOW) {
1093 if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1094 error = AE_NO_MEMORY;
1096 if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
1097 error = acpi_ConvertBufferToInteger(&buf, number);
1100 AcpiOsFree(buf.Pointer);
1106 acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number)
1111 p = (ACPI_OBJECT *)bufp->Pointer;
1112 if (p->Type == ACPI_TYPE_INTEGER) {
1113 *number = p->Integer.Value;
1116 if (p->Type != ACPI_TYPE_BUFFER)
1118 if (p->Buffer.Length > sizeof(int))
1119 return(AE_BAD_DATA);
1121 for (i = 0; i < p->Buffer.Length; i++)
1122 *number += (*(p->Buffer.Pointer + i) << (i * 8));
1127 * Iterate over the elements of an a package object, calling the supplied
1128 * function for each element.
1130 * XXX possible enhancement might be to abort traversal on error.
1133 acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg)
1138 if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE))
1139 return(AE_BAD_PARAMETER);
1141 /* iterate over components */
1142 for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++)
1149 * Find the (index)th resource object in a set.
1152 acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
1157 rp = (ACPI_RESOURCE *)buf->Pointer;
1161 if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
1162 return(AE_BAD_PARAMETER);
1163 /* check for terminator */
1164 if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
1166 return(AE_NOT_FOUND);
1167 rp = ACPI_RESOURCE_NEXT(rp);
1175 * Append an ACPI_RESOURCE to an ACPI_BUFFER.
1177 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
1178 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible
1179 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of
1182 #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512
1185 acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
1191 * Initialise the buffer if necessary.
1193 if (buf->Pointer == NULL) {
1194 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
1195 if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
1196 return(AE_NO_MEMORY);
1197 rp = (ACPI_RESOURCE *)buf->Pointer;
1198 rp->Id = ACPI_RSTYPE_END_TAG;
1205 * Scan the current buffer looking for the terminator.
1206 * This will either find the terminator or hit the end
1207 * of the buffer and return an error.
1209 rp = (ACPI_RESOURCE *)buf->Pointer;
1211 /* range check, don't go outside the buffer */
1212 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
1213 return(AE_BAD_PARAMETER);
1214 if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
1215 (rp->Length == 0)) {
1218 rp = ACPI_RESOURCE_NEXT(rp);
1222 * Check the size of the buffer and expand if required.
1225 * size of existing resources before terminator +
1226 * size of new resource and header +
1227 * size of terminator.
1229 * Note that this loop should really only run once, unless
1230 * for some reason we are stuffing a *really* huge resource.
1232 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
1233 res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
1234 ACPI_RESOURCE_LENGTH) >= buf->Length) {
1235 if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
1236 return(AE_NO_MEMORY);
1237 bcopy(buf->Pointer, newp, buf->Length);
1238 rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1239 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
1240 AcpiOsFree(buf->Pointer);
1241 buf->Pointer = newp;
1242 buf->Length += buf->Length;
1246 * Insert the new resource.
1248 bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
1251 * And add the terminator.
1253 rp = ACPI_RESOURCE_NEXT(rp);
1254 rp->Id = ACPI_RSTYPE_END_TAG;
1261 * Set interrupt model.
1264 acpi_SetIntrModel(int model)
1266 ACPI_OBJECT_LIST ArgList;
1269 Arg.Type = ACPI_TYPE_INTEGER;
1270 Arg.Integer.Value = model;
1272 ArgList.Pointer = &Arg;
1273 return (AcpiEvaluateObject(ACPI_ROOT_OBJECT, "_PIC", &ArgList, NULL));
1276 #define ACPI_MINIMUM_AWAKETIME 5
1279 acpi_sleep_enable(void *arg)
1281 ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1285 * Set the system sleep state
1287 * Currently we only support S1 and S5
1290 acpi_SetSleepState(struct acpi_softc *sc, int state)
1292 ACPI_STATUS status = AE_OK;
1296 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1299 if (sc->acpi_sstate != ACPI_STATE_S0)
1300 return_ACPI_STATUS(AE_BAD_PARAMETER); /* avoid reentry */
1302 if (sc->acpi_sleep_disabled)
1303 return_ACPI_STATUS(AE_OK);
1306 case ACPI_STATE_S0: /* XXX only for testing */
1307 if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
1308 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
1316 if (ACPI_FAILURE(status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB))) {
1317 device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", AcpiFormatException(status));
1321 sc->acpi_sstate = state;
1322 sc->acpi_sleep_disabled = 1;
1325 * Inform all devices that we are going to sleep.
1327 if (DEVICE_SUSPEND(root_bus) != 0) {
1329 * Re-wake the system.
1331 * XXX note that a better two-pass approach with a 'veto' pass
1332 * followed by a "real thing" pass would be better, but the
1333 * current bus interface does not provide for this.
1335 DEVICE_RESUME(root_bus);
1336 return_ACPI_STATUS(AE_ERROR);
1339 if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(state))) {
1340 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1341 AcpiFormatException(status));
1345 if (sc->acpi_sleep_delay > 0) {
1346 DELAY(sc->acpi_sleep_delay * 1000000);
1349 if (state != ACPI_STATE_S1) {
1350 acpi_sleep_machdep(sc, state);
1352 /* AcpiEnterSleepState() maybe incompleted, unlock here if locked. */
1353 if (AcpiGbl_AcpiMutexInfo[ACPI_MTX_HARDWARE].OwnerId != ACPI_MUTEX_NOT_ACQUIRED) {
1354 AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
1357 /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1358 if (state == ACPI_STATE_S4) {
1362 if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
1363 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
1367 AcpiLeaveSleepState((UINT8)state);
1368 DEVICE_RESUME(root_bus);
1369 sc->acpi_sstate = ACPI_STATE_S0;
1370 acpi_enable_fixed_events(sc);
1375 * Shut down cleanly and power off. This will call us back through the
1376 * shutdown handlers.
1378 shutdown_nice(RB_POWEROFF);
1382 status = AE_BAD_PARAMETER;
1386 if (sc->acpi_sleep_disabled)
1387 timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1389 return_ACPI_STATUS(status);
1393 * Enable/Disable ACPI
1396 acpi_Enable(struct acpi_softc *sc)
1401 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1404 flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
1405 ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
1406 if (!sc->acpi_enabled) {
1407 status = AcpiEnableSubsystem(flags);
1411 if (status == AE_OK)
1412 sc->acpi_enabled = 1;
1413 return_ACPI_STATUS(status);
1417 acpi_Disable(struct acpi_softc *sc)
1421 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1424 if (sc->acpi_enabled) {
1425 status = AcpiDisable();
1429 if (status == AE_OK)
1430 sc->acpi_enabled = 0;
1431 return_ACPI_STATUS(status);
1435 * ACPI Event Handlers
1438 /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
1441 acpi_system_eventhandler_sleep(void *arg, int state)
1443 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1446 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
1447 acpi_SetSleepState((struct acpi_softc *)arg, state);
1453 acpi_system_eventhandler_wakeup(void *arg, int state)
1455 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1457 /* Well, what to do? :-) */
1466 * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
1469 acpi_eventhandler_power_button_for_sleep(void *context)
1471 struct acpi_softc *sc = (struct acpi_softc *)context;
1473 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1475 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
1477 return_VALUE(ACPI_INTERRUPT_HANDLED);
1481 acpi_eventhandler_power_button_for_wakeup(void *context)
1483 struct acpi_softc *sc = (struct acpi_softc *)context;
1485 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1487 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
1489 return_VALUE(ACPI_INTERRUPT_HANDLED);
1493 acpi_eventhandler_sleep_button_for_sleep(void *context)
1495 struct acpi_softc *sc = (struct acpi_softc *)context;
1497 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1499 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
1501 return_VALUE(ACPI_INTERRUPT_HANDLED);
1505 acpi_eventhandler_sleep_button_for_wakeup(void *context)
1507 struct acpi_softc *sc = (struct acpi_softc *)context;
1509 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1511 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
1513 return_VALUE(ACPI_INTERRUPT_HANDLED);
1517 * XXX This is kinda ugly, and should not be here.
1519 struct acpi_staticbuf {
1525 acpi_name(ACPI_HANDLE handle)
1527 static struct acpi_staticbuf buf;
1531 buf.buffer.Length = 512;
1532 buf.buffer.Pointer = &buf.data[0];
1534 if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
1535 return(buf.buffer.Pointer);
1536 return("(unknown path)");
1540 * Debugging/bug-avoidance. Avoid trying to fetch info on various
1541 * parts of the namespace.
1544 acpi_avoid(ACPI_HANDLE handle)
1546 char *cp, *env, *np;
1549 np = acpi_name(handle);
1552 if ((env = getenv("debug.acpi.avoid")) == NULL)
1555 /* scan the avoid list checking for a match */
1558 while ((*cp != 0) && isspace(*cp))
1563 while ((cp[len] != 0) && !isspace(cp[len]))
1565 if (!strncmp(cp, np, len)) {
1576 * Debugging/bug-avoidance. Disable ACPI subsystem components.
1579 acpi_disabled(char *subsys)
1584 if ((env = getenv("debug.acpi.disable")) == NULL)
1586 if (!strcmp(env, "all")) {
1591 /* scan the disable list checking for a match */
1594 while ((*cp != 0) && isspace(*cp))
1599 while ((cp[len] != 0) && !isspace(cp[len]))
1601 if (!strncmp(cp, subsys, len)) {
1612 * Device wake capability enable/disable.
1615 acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable)
1617 ACPI_OBJECT_LIST ArgList;
1621 * TBD: All Power Resources referenced by elements 2 through N
1622 * of the _PRW object are put into the ON state.
1626 * enable/disable device wake function.
1630 ArgList.Pointer = &Arg;
1632 Arg.Type = ACPI_TYPE_INTEGER;
1633 Arg.Integer.Value = enable;
1635 (void)AcpiEvaluateObject(h, "_PSW", &ArgList, NULL);
1639 acpi_device_enable_wake_event(ACPI_HANDLE h)
1641 struct acpi_softc *sc;
1643 ACPI_BUFFER prw_buffer;
1646 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1648 if ((sc = devclass_get_softc(acpi_devclass, 0)) == NULL) {
1653 * _PRW object is only required for devices that have the ability
1654 * to wake the system from a system sleeping state.
1656 prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
1657 status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
1658 if (ACPI_FAILURE(status)) {
1662 res = (ACPI_OBJECT *)prw_buffer.Pointer;
1667 if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) {
1672 * The element 1 of the _PRW object:
1673 * The lowest power system sleeping state that can be entered
1674 * while still providing wake functionality.
1675 * The sleeping state being entered must be greater or equal to
1676 * the power state declared in element 1 of the _PRW object.
1678 if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER) {
1682 if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value) {
1687 * The element 0 of the _PRW object:
1689 switch(res->Package.Elements[0].Type) {
1690 case ACPI_TYPE_INTEGER:
1692 * If the data type of this package element is numeric, then this
1693 * _PRW package element is the bit index in the GPEx_EN, in the
1694 * GPE blocks described in the FADT, of the enable bit that is
1695 * enabled for the wake event.
1698 status = AcpiEnableEvent(res->Package.Elements[0].Integer.Value,
1699 ACPI_EVENT_GPE, ACPI_EVENT_WAKE_ENABLE);
1700 if (ACPI_FAILURE(status))
1701 printf("%s: EnableEvent Failed\n", __func__);
1704 case ACPI_TYPE_PACKAGE:
1708 * If the data type of this package element is a package, then this
1709 * _PRW package element is itself a package containing two
1710 * elements. The first is an object reference to the GPE Block
1711 * device that contains the GPE that will be triggered by the wake
1712 * event. The second element is numeric and it contains the bit
1713 * index in the GPEx_EN, in the GPE Block referenced by the
1714 * first element in the package, of the enable bit that is enabled for
1716 * For example, if this field is a package then it is of the form:
1717 * Package() {\_SB.PCI0.ISA.GPE, 2}
1727 if (prw_buffer.Pointer != NULL)
1728 AcpiOsFree(prw_buffer.Pointer);
1733 * Control interface.
1735 * We multiplex ioctls for all participating ACPI devices here. Individual
1736 * drivers wanting to be accessible via /dev/acpi should use the register/deregister
1737 * interface to make their handlers visible.
1739 struct acpi_ioctl_hook
1741 TAILQ_ENTRY(acpi_ioctl_hook) link;
1743 int (* fn)(u_long cmd, caddr_t addr, void *arg);
1747 static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks;
1748 static int acpi_ioctl_hooks_initted;
1751 * Register an ioctl handler.
1754 acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg)
1756 struct acpi_ioctl_hook *hp;
1758 if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
1763 if (acpi_ioctl_hooks_initted == 0) {
1764 TAILQ_INIT(&acpi_ioctl_hooks);
1765 acpi_ioctl_hooks_initted = 1;
1767 TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
1772 * Deregister an ioctl handler.
1775 acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg))
1777 struct acpi_ioctl_hook *hp;
1779 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
1780 if ((hp->cmd == cmd) && (hp->fn == fn))
1784 TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
1785 free(hp, M_ACPIDEV);
1790 acpiopen(dev_t dev, int flag, int fmt, struct thread *td)
1796 acpiclose(dev_t dev, int flag, int fmt, struct thread *td)
1802 acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1804 struct acpi_softc *sc;
1805 struct acpi_ioctl_hook *hp;
1806 int error, xerror, state;
1814 * Scan the list of registered ioctls, looking for handlers.
1816 if (acpi_ioctl_hooks_initted) {
1817 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
1818 if (hp->cmd == cmd) {
1819 xerror = hp->fn(cmd, addr, hp->arg);
1828 * Core system ioctls.
1832 if (ACPI_FAILURE(acpi_Enable(sc)))
1836 case ACPIIO_DISABLE:
1837 if (ACPI_FAILURE(acpi_Disable(sc)))
1841 case ACPIIO_SETSLPSTATE:
1842 if (!sc->acpi_enabled) {
1846 state = *(int *)addr;
1847 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) {
1848 acpi_SetSleepState(sc, state);
1866 acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
1868 char sleep_state[10];
1870 u_int new_state, old_state;
1872 old_state = *(u_int *)oidp->oid_arg1;
1873 if (old_state > ACPI_S_STATES_MAX+1) {
1874 strcpy(sleep_state, "unknown");
1876 bzero(sleep_state, sizeof(sleep_state));
1877 strncpy(sleep_state, sleep_state_names[old_state],
1878 sizeof(sleep_state_names[old_state]));
1880 error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
1881 if (error == 0 && req->newptr != NULL) {
1882 for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX+1; new_state++) {
1883 if (strncmp(sleep_state, sleep_state_names[new_state],
1884 sizeof(sleep_state)) == 0)
1887 if (new_state <= ACPI_S_STATES_MAX+1) {
1888 if (new_state != old_state) {
1889 *(u_int *)oidp->oid_arg1 = new_state;
1900 * Support for parsing debug options from the kernel environment.
1902 * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
1903 * by specifying the names of the bits in the debug.acpi.layer and
1904 * debug.acpi.level environment variables. Bits may be unset by
1905 * prefixing the bit name with !.
1913 static struct debugtag dbg_layer[] = {
1914 {"ACPI_UTILITIES", ACPI_UTILITIES},
1915 {"ACPI_HARDWARE", ACPI_HARDWARE},
1916 {"ACPI_EVENTS", ACPI_EVENTS},
1917 {"ACPI_TABLES", ACPI_TABLES},
1918 {"ACPI_NAMESPACE", ACPI_NAMESPACE},
1919 {"ACPI_PARSER", ACPI_PARSER},
1920 {"ACPI_DISPATCHER", ACPI_DISPATCHER},
1921 {"ACPI_EXECUTER", ACPI_EXECUTER},
1922 {"ACPI_RESOURCES", ACPI_RESOURCES},
1923 {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER},
1924 {"ACPI_OS_SERVICES", ACPI_OS_SERVICES},
1925 {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER},
1927 {"ACPI_BUS", ACPI_BUS},
1928 {"ACPI_SYSTEM", ACPI_SYSTEM},
1929 {"ACPI_POWER", ACPI_POWER},
1930 {"ACPI_EC", ACPI_EC},
1931 {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER},
1932 {"ACPI_BATTERY", ACPI_BATTERY},
1933 {"ACPI_BUTTON", ACPI_BUTTON},
1934 {"ACPI_PROCESSOR", ACPI_PROCESSOR},
1935 {"ACPI_THERMAL", ACPI_THERMAL},
1936 {"ACPI_FAN", ACPI_FAN},
1938 {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS},
1939 {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS},
1943 static struct debugtag dbg_level[] = {
1944 {"ACPI_LV_OK", ACPI_LV_OK},
1945 {"ACPI_LV_INFO", ACPI_LV_INFO},
1946 {"ACPI_LV_WARN", ACPI_LV_WARN},
1947 {"ACPI_LV_ERROR", ACPI_LV_ERROR},
1948 {"ACPI_LV_FATAL", ACPI_LV_FATAL},
1949 {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT},
1950 {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS},
1952 /* Trace verbosity level 1 [Standard Trace Level] */
1953 {"ACPI_LV_PARSE", ACPI_LV_PARSE},
1954 {"ACPI_LV_LOAD", ACPI_LV_LOAD},
1955 {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH},
1956 {"ACPI_LV_EXEC", ACPI_LV_EXEC},
1957 {"ACPI_LV_NAMES", ACPI_LV_NAMES},
1958 {"ACPI_LV_OPREGION", ACPI_LV_OPREGION},
1959 {"ACPI_LV_BFIELD", ACPI_LV_BFIELD},
1960 {"ACPI_LV_TABLES", ACPI_LV_TABLES},
1961 {"ACPI_LV_VALUES", ACPI_LV_VALUES},
1962 {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS},
1963 {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES},
1964 {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS},
1965 {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE},
1966 {"ACPI_LV_INIT", ACPI_LV_INIT},
1967 {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1},
1969 /* Trace verbosity level 2 [Function tracing and memory allocation] */
1970 {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS},
1971 {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS},
1972 {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS},
1973 {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2},
1974 {"ACPI_LV_ALL", ACPI_LV_ALL},
1976 /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
1977 {"ACPI_LV_MUTEX", ACPI_LV_MUTEX},
1978 {"ACPI_LV_THREADS", ACPI_LV_THREADS},
1979 {"ACPI_LV_IO", ACPI_LV_IO},
1980 {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS},
1981 {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3},
1983 /* Exceptionally verbose output -- also used in the global "DebugLevel" */
1984 {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE},
1985 {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO},
1986 {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES},
1987 {"ACPI_LV_EVENTS", ACPI_LV_EVENTS},
1988 {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE},
1993 acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
2005 while (*ep && !isspace(*ep))
2016 for (i = 0; tag[i].name != NULL; i++) {
2017 if (!strncmp(cp, tag[i].name, l)) {
2019 *flag |= tag[i].value;
2021 *flag &= ~tag[i].value;
2023 printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
2031 acpi_set_debugging(void *junk)
2037 if ((cp = getenv("debug.acpi.layer")) != NULL) {
2038 acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
2041 if ((cp = getenv("debug.acpi.level")) != NULL) {
2042 acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
2046 printf("ACPI debug layer 0x%x debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel);
2048 SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL);
2052 acpi_pm_func(u_long cmd, void *arg, ...)
2054 int state, acpi_state;
2056 struct acpi_softc *sc;
2061 case POWER_CMD_SUSPEND:
2062 sc = (struct acpi_softc *)arg;
2069 state = va_arg(ap, int);
2073 case POWER_SLEEP_STATE_STANDBY:
2074 acpi_state = sc->acpi_standby_sx;
2076 case POWER_SLEEP_STATE_SUSPEND:
2077 acpi_state = sc->acpi_suspend_sx;
2079 case POWER_SLEEP_STATE_HIBERNATE:
2080 acpi_state = ACPI_STATE_S4;
2087 acpi_SetSleepState(sc, acpi_state);
2100 acpi_pm_register(void *arg)
2104 if (!resource_int_value("acpi", 0, "disabled", &error) &&
2108 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
2111 SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);