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);
595 acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
597 struct acpi_device *ad;
599 if ((ad = device_get_ivars(child)) == NULL) {
600 printf("device has no ivars\n");
606 case ACPI_IVAR_HANDLE:
607 ad->ad_handle = (ACPI_HANDLE)value;
609 case ACPI_IVAR_MAGIC:
610 ad->ad_magic = (int )value;
612 case ACPI_IVAR_PRIVATE:
613 ad->ad_private = (void *)value;
617 panic("bad ivar write request (%d)", index);
624 * Handle child resource allocation/removal
627 acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
629 struct acpi_device *ad = device_get_ivars(child);
630 struct resource_list *rl = &ad->ad_rl;
632 resource_list_add(rl, type, rid, start, start + count -1, count);
638 acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
640 struct acpi_device *ad = device_get_ivars(child);
641 struct resource_list *rl = &ad->ad_rl;
642 struct resource_list_entry *rle;
644 rle = resource_list_find(rl, type, rid);
649 *startp = rle->start;
651 *countp = rle->count;
656 static struct resource *
657 acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
658 u_long start, u_long end, u_long count, u_int flags)
660 struct acpi_device *ad = device_get_ivars(child);
661 struct resource_list *rl = &ad->ad_rl;
663 return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags));
667 acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
669 struct acpi_device *ad = device_get_ivars(child);
670 struct resource_list *rl = &ad->ad_rl;
672 return(resource_list_release(rl, bus, child, type, rid, r));
676 * Handle ISA-like devices probing for a PnP ID to match.
678 #define PNP_EISAID(s) \
679 ((((s[0] - '@') & 0x1f) << 2) \
680 | (((s[1] - '@') & 0x18) >> 3) \
681 | (((s[1] - '@') & 0x07) << 13) \
682 | (((s[2] - '@') & 0x1f) << 8) \
683 | (PNP_HEXTONUM(s[4]) << 16) \
684 | (PNP_HEXTONUM(s[3]) << 20) \
685 | (PNP_HEXTONUM(s[6]) << 24) \
686 | (PNP_HEXTONUM(s[5]) << 28))
689 acpi_isa_get_logicalid(device_t dev)
692 ACPI_DEVICE_INFO devinfo;
696 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
701 /* fetch and validate the HID */
702 if ((h = acpi_get_handle(dev)) == NULL)
704 if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
706 if (!(devinfo.Valid & ACPI_VALID_HID))
709 pnpid = PNP_EISAID(devinfo.HardwareId);
716 acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
721 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
724 * ISA-style drivers attached to ACPI may persist and
725 * probe manually if we return ENOENT. We never want
726 * that to happen, so don't ever return it.
730 /* scan the supplied IDs for a match */
731 pnpid = acpi_isa_get_logicalid(child);
732 while (ids && ids->ip_id) {
733 if (pnpid == ids->ip_id) {
740 return_VALUE(result);
744 * Scan relevant portions of the ACPI namespace and attach child devices.
746 * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and \_SB_ scopes,
747 * and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
750 acpi_probe_children(device_t bus)
753 static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
756 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
760 * Create any static children by calling device identify methods.
762 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
763 bus_generic_probe(bus);
766 * Scan the namespace and insert placeholders for all the devices that
769 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
770 * we want to create nodes for all devices, not just those that are currently
771 * present. (This assumes that we don't want to create/remove devices as they
772 * appear, which might be smarter.)
774 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
775 for (i = 0; scopes[i] != NULL; i++)
776 if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent)))
777 AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL);
780 * Scan all of the child devices we have created and let them probe/attach.
782 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
783 bus_generic_attach(bus);
786 * Some of these children may have attached others as part of their attach
787 * process (eg. the root PCI bus driver), so rescan.
789 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
790 bus_generic_attach(bus);
792 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
797 * Evaluate a child device and determine whether we might attach a device to
801 acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
803 ACPI_OBJECT_TYPE type;
804 device_t child, bus = (device_t)context;
806 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
809 * Skip this device if we think we'll have trouble with it.
811 if (acpi_avoid(handle))
812 return_ACPI_STATUS(AE_OK);
814 if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
816 case ACPI_TYPE_DEVICE:
817 case ACPI_TYPE_PROCESSOR:
818 case ACPI_TYPE_THERMAL:
819 case ACPI_TYPE_POWER:
820 if (acpi_disabled("children"))
823 * Create a placeholder device for this node. Sort the placeholder
824 * so that the probe/attach passes will run breadth-first.
826 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", acpi_name(handle)));
827 child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
830 acpi_set_handle(child, handle);
833 * Check that the device is present. If it's not present,
834 * leave it disabled (so that we have a device_t attached to
835 * the handle, but we don't probe it).
837 if ((type == ACPI_TYPE_DEVICE) && (!acpi_DeviceIsPresent(child))) {
838 device_disable(child);
843 * Get the device's resource settings and attach them.
844 * Note that if the device has _PRS but no _CRS, we need
845 * to decide when it's appropriate to try to configure the
846 * device. Ignore the return value here; it's OK for the
847 * device not to have any resources.
849 acpi_parse_resources(child, handle, &acpi_res_parse_set);
851 /* if we're debugging, probe/attach now rather than later */
852 ACPI_DEBUG_EXEC(device_probe_and_attach(child));
856 return_ACPI_STATUS(AE_OK);
860 acpi_shutdown_pre_sync(void *arg, int howto)
866 * Disable all ACPI events before soft off, otherwise the system
867 * will be turned on again on some laptops.
869 * XXX this should probably be restricted to masking some events just
870 * before powering down, since we may still need ACPI during the
873 acpi_Disable((struct acpi_softc *)arg);
877 acpi_shutdown_final(void *arg, int howto)
883 if (howto & RB_POWEROFF) {
884 printf("Power system off using ACPI...\n");
885 if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(acpi_off_state))) {
886 printf("AcpiEnterSleepStatePrep failed - %s\n",
887 AcpiFormatException(status));
890 if (ACPI_FAILURE(status = AcpiEnterSleepState(acpi_off_state))) {
891 printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
894 printf("ACPI power-off failed - timeout\n");
897 printf("Terminate ACPI\n");
903 acpi_enable_fixed_events(struct acpi_softc *sc)
905 static int first_time = 1;
906 #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n"
910 /* Enable and clear fixed events and install handlers. */
911 if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) {
912 AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED, 0);
913 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED);
914 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
915 acpi_eventhandler_power_button_for_sleep, sc);
917 device_printf(sc->acpi_dev, MSGFORMAT, "power");
920 if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) {
921 AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED, 0);
922 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED);
923 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
924 acpi_eventhandler_sleep_button_for_sleep, sc);
926 device_printf(sc->acpi_dev, MSGFORMAT, "sleep");
934 * Returns true if the device is actually present and should
935 * be attached to. This requires the present, enabled, UI-visible
936 * and diagnostics-passed bits to be set.
939 acpi_DeviceIsPresent(device_t dev)
942 ACPI_DEVICE_INFO devinfo;
947 if ((h = acpi_get_handle(dev)) == NULL)
949 if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
951 /* if no _STA method, must be present */
952 if (!(devinfo.Valid & ACPI_VALID_STA))
954 /* return true for 'present' and 'functioning' */
955 if ((devinfo.CurrentStatus & 0x9) == 0x9)
961 * Returns true if the battery is actually present and inserted.
964 acpi_BatteryIsPresent(device_t dev)
967 ACPI_DEVICE_INFO devinfo;
972 if ((h = acpi_get_handle(dev)) == NULL)
974 if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
976 /* if no _STA method, must be present */
977 if (!(devinfo.Valid & ACPI_VALID_STA))
979 /* return true for 'present' and 'functioning' */
980 if ((devinfo.CurrentStatus & 0x19) == 0x19)
986 * Match a HID string against a device
989 acpi_MatchHid(device_t dev, char *hid)
992 ACPI_DEVICE_INFO devinfo;
1000 if ((h = acpi_get_handle(dev)) == NULL)
1002 if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
1004 if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId))
1006 if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &cid)))
1008 if (cid == PNP_EISAID(hid))
1014 * Return the handle of a named object within our scope, ie. that of (parent)
1015 * or one if its parents.
1018 acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1025 /* walk back up the tree to the root */
1027 if (ACPI_SUCCESS(status = AcpiGetHandle(parent, path, &r))) {
1031 if (status != AE_NOT_FOUND)
1033 if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1034 return(AE_NOT_FOUND);
1040 * Allocate a buffer with a preset data size.
1043 acpi_AllocBuffer(int size)
1047 if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1050 buf->Pointer = (void *)(buf + 1);
1055 * Evaluate a path that should return an integer.
1058 acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number)
1067 handle = ACPI_ROOT_OBJECT;
1070 * Assume that what we've been pointed at is an Integer object, or
1071 * a method that will return an Integer.
1073 buf.Pointer = ¶m;
1074 buf.Length = sizeof(param);
1075 if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
1076 if (param.Type == ACPI_TYPE_INTEGER) {
1077 *number = param.Integer.Value;
1084 * In some applications, a method that's expected to return an Integer
1085 * may instead return a Buffer (probably to simplify some internal
1086 * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer,
1087 * convert it into an Integer as best we can.
1091 if (error == AE_BUFFER_OVERFLOW) {
1092 if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1093 error = AE_NO_MEMORY;
1095 if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
1096 error = acpi_ConvertBufferToInteger(&buf, number);
1099 AcpiOsFree(buf.Pointer);
1105 acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number)
1110 p = (ACPI_OBJECT *)bufp->Pointer;
1111 if (p->Type == ACPI_TYPE_INTEGER) {
1112 *number = p->Integer.Value;
1115 if (p->Type != ACPI_TYPE_BUFFER)
1117 if (p->Buffer.Length > sizeof(int))
1118 return(AE_BAD_DATA);
1120 for (i = 0; i < p->Buffer.Length; i++)
1121 *number += (*(p->Buffer.Pointer + i) << (i * 8));
1126 * Iterate over the elements of an a package object, calling the supplied
1127 * function for each element.
1129 * XXX possible enhancement might be to abort traversal on error.
1132 acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg)
1137 if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE))
1138 return(AE_BAD_PARAMETER);
1140 /* iterate over components */
1141 for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++)
1148 * Find the (index)th resource object in a set.
1151 acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
1156 rp = (ACPI_RESOURCE *)buf->Pointer;
1160 if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
1161 return(AE_BAD_PARAMETER);
1162 /* check for terminator */
1163 if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
1165 return(AE_NOT_FOUND);
1166 rp = ACPI_RESOURCE_NEXT(rp);
1174 * Append an ACPI_RESOURCE to an ACPI_BUFFER.
1176 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
1177 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible
1178 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of
1181 #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512
1184 acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
1190 * Initialise the buffer if necessary.
1192 if (buf->Pointer == NULL) {
1193 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
1194 if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
1195 return(AE_NO_MEMORY);
1196 rp = (ACPI_RESOURCE *)buf->Pointer;
1197 rp->Id = ACPI_RSTYPE_END_TAG;
1204 * Scan the current buffer looking for the terminator.
1205 * This will either find the terminator or hit the end
1206 * of the buffer and return an error.
1208 rp = (ACPI_RESOURCE *)buf->Pointer;
1210 /* range check, don't go outside the buffer */
1211 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
1212 return(AE_BAD_PARAMETER);
1213 if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
1214 (rp->Length == 0)) {
1217 rp = ACPI_RESOURCE_NEXT(rp);
1221 * Check the size of the buffer and expand if required.
1224 * size of existing resources before terminator +
1225 * size of new resource and header +
1226 * size of terminator.
1228 * Note that this loop should really only run once, unless
1229 * for some reason we are stuffing a *really* huge resource.
1231 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
1232 res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
1233 ACPI_RESOURCE_LENGTH) >= buf->Length) {
1234 if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
1235 return(AE_NO_MEMORY);
1236 bcopy(buf->Pointer, newp, buf->Length);
1237 rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1238 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
1239 AcpiOsFree(buf->Pointer);
1240 buf->Pointer = newp;
1241 buf->Length += buf->Length;
1245 * Insert the new resource.
1247 bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
1250 * And add the terminator.
1252 rp = ACPI_RESOURCE_NEXT(rp);
1253 rp->Id = ACPI_RSTYPE_END_TAG;
1260 * Set interrupt model.
1263 acpi_SetIntrModel(int model)
1265 ACPI_OBJECT_LIST ArgList;
1268 Arg.Type = ACPI_TYPE_INTEGER;
1269 Arg.Integer.Value = model;
1271 ArgList.Pointer = &Arg;
1272 return (AcpiEvaluateObject(ACPI_ROOT_OBJECT, "_PIC", &ArgList, NULL));
1275 #define ACPI_MINIMUM_AWAKETIME 5
1278 acpi_sleep_enable(void *arg)
1280 ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1284 * Set the system sleep state
1286 * Currently we only support S1 and S5
1289 acpi_SetSleepState(struct acpi_softc *sc, int state)
1291 ACPI_STATUS status = AE_OK;
1295 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1298 if (sc->acpi_sstate != ACPI_STATE_S0)
1299 return_ACPI_STATUS(AE_BAD_PARAMETER); /* avoid reentry */
1301 if (sc->acpi_sleep_disabled)
1302 return_ACPI_STATUS(AE_OK);
1305 case ACPI_STATE_S0: /* XXX only for testing */
1306 if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
1307 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
1315 if (ACPI_FAILURE(status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB))) {
1316 device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", AcpiFormatException(status));
1320 sc->acpi_sstate = state;
1321 sc->acpi_sleep_disabled = 1;
1324 * Inform all devices that we are going to sleep.
1326 if (DEVICE_SUSPEND(root_bus) != 0) {
1328 * Re-wake the system.
1330 * XXX note that a better two-pass approach with a 'veto' pass
1331 * followed by a "real thing" pass would be better, but the
1332 * current bus interface does not provide for this.
1334 DEVICE_RESUME(root_bus);
1335 return_ACPI_STATUS(AE_ERROR);
1338 if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(state))) {
1339 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1340 AcpiFormatException(status));
1344 if (sc->acpi_sleep_delay > 0) {
1345 DELAY(sc->acpi_sleep_delay * 1000000);
1348 if (state != ACPI_STATE_S1) {
1349 acpi_sleep_machdep(sc, state);
1351 /* AcpiEnterSleepState() maybe incompleted, unlock here if locked. */
1352 if (AcpiGbl_AcpiMutexInfo[ACPI_MTX_HARDWARE].OwnerId != ACPI_MUTEX_NOT_ACQUIRED) {
1353 AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
1356 /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1357 if (state == ACPI_STATE_S4) {
1361 if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
1362 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
1366 AcpiLeaveSleepState((UINT8)state);
1367 DEVICE_RESUME(root_bus);
1368 sc->acpi_sstate = ACPI_STATE_S0;
1369 acpi_enable_fixed_events(sc);
1374 * Shut down cleanly and power off. This will call us back through the
1375 * shutdown handlers.
1377 shutdown_nice(RB_POWEROFF);
1381 status = AE_BAD_PARAMETER;
1385 if (sc->acpi_sleep_disabled)
1386 timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1388 return_ACPI_STATUS(status);
1392 * Enable/Disable ACPI
1395 acpi_Enable(struct acpi_softc *sc)
1400 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1403 flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
1404 ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
1405 if (!sc->acpi_enabled) {
1406 status = AcpiEnableSubsystem(flags);
1410 if (status == AE_OK)
1411 sc->acpi_enabled = 1;
1412 return_ACPI_STATUS(status);
1416 acpi_Disable(struct acpi_softc *sc)
1420 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1423 if (sc->acpi_enabled) {
1424 status = AcpiDisable();
1428 if (status == AE_OK)
1429 sc->acpi_enabled = 0;
1430 return_ACPI_STATUS(status);
1434 * ACPI Event Handlers
1437 /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
1440 acpi_system_eventhandler_sleep(void *arg, int state)
1442 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1445 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
1446 acpi_SetSleepState((struct acpi_softc *)arg, state);
1452 acpi_system_eventhandler_wakeup(void *arg, int state)
1454 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1456 /* Well, what to do? :-) */
1465 * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
1468 acpi_eventhandler_power_button_for_sleep(void *context)
1470 struct acpi_softc *sc = (struct acpi_softc *)context;
1472 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1474 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
1476 return_VALUE(ACPI_INTERRUPT_HANDLED);
1480 acpi_eventhandler_power_button_for_wakeup(void *context)
1482 struct acpi_softc *sc = (struct acpi_softc *)context;
1484 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1486 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
1488 return_VALUE(ACPI_INTERRUPT_HANDLED);
1492 acpi_eventhandler_sleep_button_for_sleep(void *context)
1494 struct acpi_softc *sc = (struct acpi_softc *)context;
1496 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1498 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
1500 return_VALUE(ACPI_INTERRUPT_HANDLED);
1504 acpi_eventhandler_sleep_button_for_wakeup(void *context)
1506 struct acpi_softc *sc = (struct acpi_softc *)context;
1508 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1510 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
1512 return_VALUE(ACPI_INTERRUPT_HANDLED);
1516 * XXX This is kinda ugly, and should not be here.
1518 struct acpi_staticbuf {
1524 acpi_name(ACPI_HANDLE handle)
1526 static struct acpi_staticbuf buf;
1530 buf.buffer.Length = 512;
1531 buf.buffer.Pointer = &buf.data[0];
1533 if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
1534 return(buf.buffer.Pointer);
1535 return("(unknown path)");
1539 * Debugging/bug-avoidance. Avoid trying to fetch info on various
1540 * parts of the namespace.
1543 acpi_avoid(ACPI_HANDLE handle)
1545 char *cp, *env, *np;
1548 np = acpi_name(handle);
1551 if ((env = getenv("debug.acpi.avoid")) == NULL)
1554 /* scan the avoid list checking for a match */
1557 while ((*cp != 0) && isspace(*cp))
1562 while ((cp[len] != 0) && !isspace(cp[len]))
1564 if (!strncmp(cp, np, len)) {
1575 * Debugging/bug-avoidance. Disable ACPI subsystem components.
1578 acpi_disabled(char *subsys)
1583 if ((env = getenv("debug.acpi.disable")) == NULL)
1585 if (!strcmp(env, "all")) {
1590 /* scan the disable list checking for a match */
1593 while ((*cp != 0) && isspace(*cp))
1598 while ((cp[len] != 0) && !isspace(cp[len]))
1600 if (!strncmp(cp, subsys, len)) {
1611 * Device wake capability enable/disable.
1614 acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable)
1616 ACPI_OBJECT_LIST ArgList;
1620 * TBD: All Power Resources referenced by elements 2 through N
1621 * of the _PRW object are put into the ON state.
1625 * enable/disable device wake function.
1629 ArgList.Pointer = &Arg;
1631 Arg.Type = ACPI_TYPE_INTEGER;
1632 Arg.Integer.Value = enable;
1634 (void)AcpiEvaluateObject(h, "_PSW", &ArgList, NULL);
1638 acpi_device_enable_wake_event(ACPI_HANDLE h)
1640 struct acpi_softc *sc;
1642 ACPI_BUFFER prw_buffer;
1645 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1647 if ((sc = devclass_get_softc(acpi_devclass, 0)) == NULL) {
1652 * _PRW object is only required for devices that have the ability
1653 * to wake the system from a system sleeping state.
1655 prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
1656 status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
1657 if (ACPI_FAILURE(status)) {
1661 res = (ACPI_OBJECT *)prw_buffer.Pointer;
1666 if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) {
1671 * The element 1 of the _PRW object:
1672 * The lowest power system sleeping state that can be entered
1673 * while still providing wake functionality.
1674 * The sleeping state being entered must be greater or equal to
1675 * the power state declared in element 1 of the _PRW object.
1677 if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER) {
1681 if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value) {
1686 * The element 0 of the _PRW object:
1688 switch(res->Package.Elements[0].Type) {
1689 case ACPI_TYPE_INTEGER:
1691 * If the data type of this package element is numeric, then this
1692 * _PRW package element is the bit index in the GPEx_EN, in the
1693 * GPE blocks described in the FADT, of the enable bit that is
1694 * enabled for the wake event.
1697 status = AcpiEnableEvent(res->Package.Elements[0].Integer.Value,
1698 ACPI_EVENT_GPE, ACPI_EVENT_WAKE_ENABLE);
1699 if (ACPI_FAILURE(status))
1700 printf("%s: EnableEvent Failed\n", __func__);
1703 case ACPI_TYPE_PACKAGE:
1707 * If the data type of this package element is a package, then this
1708 * _PRW package element is itself a package containing two
1709 * elements. The first is an object reference to the GPE Block
1710 * device that contains the GPE that will be triggered by the wake
1711 * event. The second element is numeric and it contains the bit
1712 * index in the GPEx_EN, in the GPE Block referenced by the
1713 * first element in the package, of the enable bit that is enabled for
1715 * For example, if this field is a package then it is of the form:
1716 * Package() {\_SB.PCI0.ISA.GPE, 2}
1726 if (prw_buffer.Pointer != NULL)
1727 AcpiOsFree(prw_buffer.Pointer);
1732 * Control interface.
1734 * We multiplex ioctls for all participating ACPI devices here. Individual
1735 * drivers wanting to be accessible via /dev/acpi should use the register/deregister
1736 * interface to make their handlers visible.
1738 struct acpi_ioctl_hook
1740 TAILQ_ENTRY(acpi_ioctl_hook) link;
1742 int (* fn)(u_long cmd, caddr_t addr, void *arg);
1746 static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks;
1747 static int acpi_ioctl_hooks_initted;
1750 * Register an ioctl handler.
1753 acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg)
1755 struct acpi_ioctl_hook *hp;
1757 if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
1762 if (acpi_ioctl_hooks_initted == 0) {
1763 TAILQ_INIT(&acpi_ioctl_hooks);
1764 acpi_ioctl_hooks_initted = 1;
1766 TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
1771 * Deregister an ioctl handler.
1774 acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg))
1776 struct acpi_ioctl_hook *hp;
1778 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
1779 if ((hp->cmd == cmd) && (hp->fn == fn))
1783 TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
1784 free(hp, M_ACPIDEV);
1789 acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td)
1795 acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td)
1801 acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
1803 struct acpi_softc *sc;
1804 struct acpi_ioctl_hook *hp;
1805 int error, xerror, state;
1813 * Scan the list of registered ioctls, looking for handlers.
1815 if (acpi_ioctl_hooks_initted) {
1816 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
1817 if (hp->cmd == cmd) {
1818 xerror = hp->fn(cmd, addr, hp->arg);
1827 * Core system ioctls.
1831 if (ACPI_FAILURE(acpi_Enable(sc)))
1835 case ACPIIO_DISABLE:
1836 if (ACPI_FAILURE(acpi_Disable(sc)))
1840 case ACPIIO_SETSLPSTATE:
1841 if (!sc->acpi_enabled) {
1845 state = *(int *)addr;
1846 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) {
1847 acpi_SetSleepState(sc, state);
1865 acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
1867 char sleep_state[10];
1869 u_int new_state, old_state;
1871 old_state = *(u_int *)oidp->oid_arg1;
1872 if (old_state > ACPI_S_STATES_MAX+1) {
1873 strcpy(sleep_state, "unknown");
1875 bzero(sleep_state, sizeof(sleep_state));
1876 strncpy(sleep_state, sleep_state_names[old_state],
1877 sizeof(sleep_state_names[old_state]));
1879 error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
1880 if (error == 0 && req->newptr != NULL) {
1881 for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX+1; new_state++) {
1882 if (strncmp(sleep_state, sleep_state_names[new_state],
1883 sizeof(sleep_state)) == 0)
1886 if (new_state <= ACPI_S_STATES_MAX+1) {
1887 if (new_state != old_state) {
1888 *(u_int *)oidp->oid_arg1 = new_state;
1899 * Support for parsing debug options from the kernel environment.
1901 * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
1902 * by specifying the names of the bits in the debug.acpi.layer and
1903 * debug.acpi.level environment variables. Bits may be unset by
1904 * prefixing the bit name with !.
1912 static struct debugtag dbg_layer[] = {
1913 {"ACPI_UTILITIES", ACPI_UTILITIES},
1914 {"ACPI_HARDWARE", ACPI_HARDWARE},
1915 {"ACPI_EVENTS", ACPI_EVENTS},
1916 {"ACPI_TABLES", ACPI_TABLES},
1917 {"ACPI_NAMESPACE", ACPI_NAMESPACE},
1918 {"ACPI_PARSER", ACPI_PARSER},
1919 {"ACPI_DISPATCHER", ACPI_DISPATCHER},
1920 {"ACPI_EXECUTER", ACPI_EXECUTER},
1921 {"ACPI_RESOURCES", ACPI_RESOURCES},
1922 {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER},
1923 {"ACPI_OS_SERVICES", ACPI_OS_SERVICES},
1924 {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER},
1926 {"ACPI_BUS", ACPI_BUS},
1927 {"ACPI_SYSTEM", ACPI_SYSTEM},
1928 {"ACPI_POWER", ACPI_POWER},
1929 {"ACPI_EC", ACPI_EC},
1930 {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER},
1931 {"ACPI_BATTERY", ACPI_BATTERY},
1932 {"ACPI_BUTTON", ACPI_BUTTON},
1933 {"ACPI_PROCESSOR", ACPI_PROCESSOR},
1934 {"ACPI_THERMAL", ACPI_THERMAL},
1935 {"ACPI_FAN", ACPI_FAN},
1937 {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS},
1938 {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS},
1942 static struct debugtag dbg_level[] = {
1943 {"ACPI_LV_OK", ACPI_LV_OK},
1944 {"ACPI_LV_INFO", ACPI_LV_INFO},
1945 {"ACPI_LV_WARN", ACPI_LV_WARN},
1946 {"ACPI_LV_ERROR", ACPI_LV_ERROR},
1947 {"ACPI_LV_FATAL", ACPI_LV_FATAL},
1948 {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT},
1949 {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS},
1951 /* Trace verbosity level 1 [Standard Trace Level] */
1952 {"ACPI_LV_PARSE", ACPI_LV_PARSE},
1953 {"ACPI_LV_LOAD", ACPI_LV_LOAD},
1954 {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH},
1955 {"ACPI_LV_EXEC", ACPI_LV_EXEC},
1956 {"ACPI_LV_NAMES", ACPI_LV_NAMES},
1957 {"ACPI_LV_OPREGION", ACPI_LV_OPREGION},
1958 {"ACPI_LV_BFIELD", ACPI_LV_BFIELD},
1959 {"ACPI_LV_TABLES", ACPI_LV_TABLES},
1960 {"ACPI_LV_VALUES", ACPI_LV_VALUES},
1961 {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS},
1962 {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES},
1963 {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS},
1964 {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE},
1965 {"ACPI_LV_INIT", ACPI_LV_INIT},
1966 {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1},
1968 /* Trace verbosity level 2 [Function tracing and memory allocation] */
1969 {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS},
1970 {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS},
1971 {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS},
1972 {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2},
1973 {"ACPI_LV_ALL", ACPI_LV_ALL},
1975 /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
1976 {"ACPI_LV_MUTEX", ACPI_LV_MUTEX},
1977 {"ACPI_LV_THREADS", ACPI_LV_THREADS},
1978 {"ACPI_LV_IO", ACPI_LV_IO},
1979 {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS},
1980 {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3},
1982 /* Exceptionally verbose output -- also used in the global "DebugLevel" */
1983 {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE},
1984 {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO},
1985 {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES},
1986 {"ACPI_LV_EVENTS", ACPI_LV_EVENTS},
1987 {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE},
1992 acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
2004 while (*ep && !isspace(*ep))
2015 for (i = 0; tag[i].name != NULL; i++) {
2016 if (!strncmp(cp, tag[i].name, l)) {
2018 *flag |= tag[i].value;
2020 *flag &= ~tag[i].value;
2022 printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
2030 acpi_set_debugging(void *junk)
2036 if ((cp = getenv("debug.acpi.layer")) != NULL) {
2037 acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
2040 if ((cp = getenv("debug.acpi.level")) != NULL) {
2041 acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
2045 printf("ACPI debug layer 0x%x debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel);
2047 SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL);
2051 acpi_pm_func(u_long cmd, void *arg, ...)
2053 int state, acpi_state;
2055 struct acpi_softc *sc;
2060 case POWER_CMD_SUSPEND:
2061 sc = (struct acpi_softc *)arg;
2068 state = va_arg(ap, int);
2072 case POWER_SLEEP_STATE_STANDBY:
2073 acpi_state = sc->acpi_standby_sx;
2075 case POWER_SLEEP_STATE_SUSPEND:
2076 acpi_state = sc->acpi_suspend_sx;
2078 case POWER_SLEEP_STATE_HIBERNATE:
2079 acpi_state = ACPI_STATE_S4;
2086 acpi_SetSleepState(sc, acpi_state);
2099 acpi_pm_register(void *arg)
2103 if (!resource_int_value("acpi", 0, "disabled", &error) &&
2107 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
2110 SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);