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_resources(struct resource_list *rl, const char *name, int type,
108 static int acpi_print_child(device_t bus, device_t child);
109 static int acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result);
110 static int acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value);
111 static int acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start,
113 static int acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp,
115 static struct resource *acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
116 u_long start, u_long end, u_long count, u_int flags);
117 static int acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r);
118 static u_int32_t acpi_isa_get_logicalid(device_t dev);
119 static int acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids);
121 static void acpi_probe_children(device_t bus);
122 static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status);
124 static void acpi_shutdown_pre_sync(void *arg, int howto);
125 static void acpi_shutdown_final(void *arg, int howto);
127 static void acpi_enable_fixed_events(struct acpi_softc *sc);
129 static void acpi_system_eventhandler_sleep(void *arg, int state);
130 static void acpi_system_eventhandler_wakeup(void *arg, int state);
131 static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
133 static int acpi_pm_func(u_long cmd, void *arg, ...);
135 static device_method_t acpi_methods[] = {
136 /* Device interface */
137 DEVMETHOD(device_identify, acpi_identify),
138 DEVMETHOD(device_probe, acpi_probe),
139 DEVMETHOD(device_attach, acpi_attach),
140 DEVMETHOD(device_shutdown, bus_generic_shutdown),
141 DEVMETHOD(device_suspend, bus_generic_suspend),
142 DEVMETHOD(device_resume, bus_generic_resume),
145 DEVMETHOD(bus_add_child, acpi_add_child),
146 DEVMETHOD(bus_print_child, acpi_print_child),
147 DEVMETHOD(bus_read_ivar, acpi_read_ivar),
148 DEVMETHOD(bus_write_ivar, acpi_write_ivar),
149 DEVMETHOD(bus_set_resource, acpi_set_resource),
150 DEVMETHOD(bus_get_resource, acpi_get_resource),
151 DEVMETHOD(bus_alloc_resource, acpi_alloc_resource),
152 DEVMETHOD(bus_release_resource, acpi_release_resource),
153 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
154 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
155 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
156 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
157 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
160 DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe),
165 static driver_t acpi_driver = {
168 sizeof(struct acpi_softc),
171 static devclass_t acpi_devclass;
172 DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
173 MODULE_VERSION(acpi, 100);
175 SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, "");
176 SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, "");
177 static int acpi_ca_version = ACPI_CA_VERSION;
178 SYSCTL_INT(_debug, OID_AUTO, acpi_ca_version, CTLFLAG_RD, &acpi_ca_version, 0, "");
181 * ACPI can only be loaded as a module by the loader; activating it after
182 * system bootstrap time is not useful, and can be fatal to the system.
183 * It also cannot be unloaded, since the entire system bus heirarchy hangs off it.
186 acpi_modevent(struct module *mod, int event, void *junk)
194 if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
204 * Detect ACPI, perform early initialisation
207 acpi_identify(driver_t *driver, device_t parent)
211 caddr_t acpi_dsdt, p;
212 #ifdef ENABLE_DEBUGGER
216 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
219 printf("Don't load this driver from userland!!\n");
224 * Check that we haven't been disabled with a hint.
226 if (!resource_int_value("acpi", 0, "disabled", &error) &&
231 * Make sure we're not being doubly invoked.
233 if (device_find_child(parent, "acpi", 0) != NULL)
236 /* initialise the ACPI mutex */
237 mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
240 * Start up the ACPI CA subsystem.
242 #ifdef ENABLE_DEBUGGER
243 debugpoint = getenv("debug.acpi.debugger");
245 if (!strcmp(debugpoint, "init"))
246 acpi_EnterDebugger();
250 if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
251 printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
254 #ifdef ENABLE_DEBUGGER
255 debugpoint = getenv("debug.acpi.debugger");
257 if (!strcmp(debugpoint, "tables"))
258 acpi_EnterDebugger();
263 if ((acpi_dsdt = preload_search_by_type("acpi_dsdt")) != NULL) {
264 if ((p = preload_search_info(acpi_dsdt, MODINFO_ADDR)) != NULL) {
265 if (ACPI_FAILURE(error = AcpiSetDsdtTablePtr(*(void **)p))) {
266 printf("ACPI: DSDT overriding failed: %s\n",
267 AcpiFormatException(error));
269 printf("ACPI: DSDT was overridden.\n");
274 if (ACPI_FAILURE(error = AcpiLoadTables())) {
275 printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
280 * Attach the actual ACPI device.
282 if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
283 device_printf(parent, "ACPI: could not attach\n");
289 * Fetch some descriptive data from ACPI to put in our attach message
292 acpi_probe(device_t dev)
294 ACPI_TABLE_HEADER th;
299 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
301 if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
302 power_pm_get_type() != POWER_PM_TYPE_ACPI) {
303 device_printf(dev, "Other PM system enabled.\n");
309 if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
310 device_printf(dev, "couldn't get XSDT header: %s\n", AcpiFormatException(status));
313 sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId);
314 device_set_desc_copy(dev, buf);
322 acpi_attach(device_t dev)
324 struct acpi_softc *sc;
329 #ifdef ENABLE_DEBUGGER
333 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
335 sc = device_get_softc(dev);
336 bzero(sc, sizeof(*sc));
339 #ifdef ENABLE_DEBUGGER
340 debugpoint = getenv("debug.acpi.debugger");
342 if (!strcmp(debugpoint, "spaces"))
343 acpi_EnterDebugger();
349 * Install the default address space handlers.
352 if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
353 ACPI_ADR_SPACE_SYSTEM_MEMORY,
354 ACPI_DEFAULT_HANDLER,
356 device_printf(dev, "could not initialise SystemMemory handler: %s\n", AcpiFormatException(status));
359 if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
360 ACPI_ADR_SPACE_SYSTEM_IO,
361 ACPI_DEFAULT_HANDLER,
363 device_printf(dev, "could not initialise SystemIO handler: %s\n", AcpiFormatException(status));
366 if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
367 ACPI_ADR_SPACE_PCI_CONFIG,
368 ACPI_DEFAULT_HANDLER,
370 device_printf(dev, "could not initialise PciConfig handler: %s\n", AcpiFormatException(status));
375 * Bring ACPI fully online.
377 * Note that some systems (specifically, those with namespace evaluation issues
378 * that require the avoidance of parts of the namespace) must avoid running _INI
379 * and _STA on everything, as well as dodging the final object init pass.
381 * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
383 * XXX We should arrange for the object init pass after we have attached all our
384 * child devices, but on many systems it works here.
386 #ifdef ENABLE_DEBUGGER
387 debugpoint = getenv("debug.acpi.debugger");
389 if (!strcmp(debugpoint, "enable"))
390 acpi_EnterDebugger();
395 if (testenv("debug.acpi.avoid"))
396 flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
397 if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
398 device_printf(dev, "could not enable ACPI: %s\n", AcpiFormatException(status));
403 * Setup our sysctl tree.
405 * XXX: This doesn't check to make sure that none of these fail.
407 sysctl_ctx_init(&sc->acpi_sysctl_ctx);
408 sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
409 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
410 device_get_name(dev), CTLFLAG_RD, 0, "");
411 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
412 OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
413 &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
414 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
415 OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
416 &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
417 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
418 OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
419 &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
420 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
421 OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
422 &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
423 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
424 OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
425 &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
426 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
427 OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
428 &sc->acpi_s4bios, 0, "S4BIOS mode");
429 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
430 OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
431 &sc->acpi_verbose, 0, "verbose mode");
434 sc->acpi_verbose = 1;
437 * Dispatch the default sleep state to devices.
438 * TBD: should be configured from userland policy manager.
440 sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
441 sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
442 sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
443 sc->acpi_standby_sx = ACPI_STATE_S1;
444 sc->acpi_suspend_sx = ACPI_STATE_S3;
446 acpi_enable_fixed_events(sc);
449 * Scan the namespace and attach/initialise children.
451 #ifdef ENABLE_DEBUGGER
452 debugpoint = getenv("debug.acpi.debugger");
454 if (!strcmp(debugpoint, "probe"))
455 acpi_EnterDebugger();
459 if (!acpi_disabled("bus"))
460 acpi_probe_children(dev);
463 * Register our shutdown handlers
465 EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST);
466 EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST);
469 * Register our acpi event handlers.
470 * XXX should be configurable eg. via userland policy manager.
472 EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST);
473 EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST);
476 * Flag our initial states.
478 sc->acpi_enabled = 1;
479 sc->acpi_sstate = ACPI_STATE_S0;
480 sc->acpi_sleep_disabled = 0;
483 * Create the control device
485 sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, 0, 5, 0660, "acpi");
486 sc->acpi_dev_t->si_drv1 = sc;
488 #ifdef ENABLE_DEBUGGER
489 debugpoint = getenv("debug.acpi.debugger");
491 if (!strcmp(debugpoint, "running"))
492 acpi_EnterDebugger();
497 #if defined(ACPI_MAX_THREADS) && ACPI_MAX_THREADS > 0
498 if ((error = acpi_task_thread_init())) {
503 if ((error = acpi_machdep_init(dev))) {
507 /* Register ACPI again to pass the correct argument of pm_func. */
508 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
518 * Handle a new device being added
521 acpi_add_child(device_t bus, int order, const char *name, int unit)
523 struct acpi_device *ad;
526 if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL)
528 bzero(ad, sizeof(*ad));
530 resource_list_init(&ad->ad_rl);
532 child = device_add_child_ordered(bus, order, name, unit);
534 device_set_ivars(child, ad);
539 * Print child device resource usage
542 acpi_print_resources(struct resource_list *rl, const char *name, int type, const char *format)
544 struct resource_list_entry *rle;
550 if (!SLIST_FIRST(rl))
553 /* Yes, this is kinda cheating */
554 SLIST_FOREACH(rle, rl, link) {
555 if (rle->type == type) {
557 retval += printf(" %s ", name);
558 else if (printed > 0)
559 retval += printf(",");
561 retval += printf(format, rle->start);
562 if (rle->count > 1) {
563 retval += printf("-");
564 retval += printf(format, rle->start +
573 acpi_print_child(device_t bus, device_t child)
575 struct acpi_device *adev = device_get_ivars(child);
576 struct resource_list *rl = &adev->ad_rl;
579 retval += bus_print_child_header(bus, child);
580 retval += acpi_print_resources(rl, "port", SYS_RES_IOPORT, "%#lx");
581 retval += acpi_print_resources(rl, "iomem", SYS_RES_MEMORY, "%#lx");
582 retval += acpi_print_resources(rl, "irq", SYS_RES_IRQ, "%ld");
583 retval += acpi_print_resources(rl, "drq", SYS_RES_DRQ, "%ld");
584 retval += bus_print_child_footer(bus, child);
591 * Handle per-device ivars
594 acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
596 struct acpi_device *ad;
598 if ((ad = device_get_ivars(child)) == NULL) {
599 printf("device has no ivars\n");
605 case ACPI_IVAR_HANDLE:
606 *(ACPI_HANDLE *)result = ad->ad_handle;
608 case ACPI_IVAR_MAGIC:
609 *(int *)result = ad->ad_magic;
611 case ACPI_IVAR_PRIVATE:
612 *(void **)result = ad->ad_private;
615 /* ISA compatibility */
616 case ISA_IVAR_VENDORID:
617 case ISA_IVAR_SERIAL:
618 case ISA_IVAR_COMPATID:
622 case ISA_IVAR_LOGICALID:
623 *(int *)result = acpi_isa_get_logicalid(child);
627 panic("bad ivar read request (%d)\n", index);
634 acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
636 struct acpi_device *ad;
638 if ((ad = device_get_ivars(child)) == NULL) {
639 printf("device has no ivars\n");
645 case ACPI_IVAR_HANDLE:
646 ad->ad_handle = (ACPI_HANDLE)value;
648 case ACPI_IVAR_MAGIC:
649 ad->ad_magic = (int )value;
651 case ACPI_IVAR_PRIVATE:
652 ad->ad_private = (void *)value;
656 panic("bad ivar write request (%d)\n", index);
663 * Handle child resource allocation/removal
666 acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
668 struct acpi_device *ad = device_get_ivars(child);
669 struct resource_list *rl = &ad->ad_rl;
671 resource_list_add(rl, type, rid, start, start + count -1, count);
677 acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
679 struct acpi_device *ad = device_get_ivars(child);
680 struct resource_list *rl = &ad->ad_rl;
681 struct resource_list_entry *rle;
683 rle = resource_list_find(rl, type, rid);
688 *startp = rle->start;
690 *countp = rle->count;
695 static struct resource *
696 acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
697 u_long start, u_long end, u_long count, u_int flags)
699 struct acpi_device *ad = device_get_ivars(child);
700 struct resource_list *rl = &ad->ad_rl;
702 return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags));
706 acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
708 struct acpi_device *ad = device_get_ivars(child);
709 struct resource_list *rl = &ad->ad_rl;
711 return(resource_list_release(rl, bus, child, type, rid, r));
715 * Handle ISA-like devices probing for a PnP ID to match.
717 #define PNP_EISAID(s) \
718 ((((s[0] - '@') & 0x1f) << 2) \
719 | (((s[1] - '@') & 0x18) >> 3) \
720 | (((s[1] - '@') & 0x07) << 13) \
721 | (((s[2] - '@') & 0x1f) << 8) \
722 | (PNP_HEXTONUM(s[4]) << 16) \
723 | (PNP_HEXTONUM(s[3]) << 20) \
724 | (PNP_HEXTONUM(s[6]) << 24) \
725 | (PNP_HEXTONUM(s[5]) << 28))
728 acpi_isa_get_logicalid(device_t dev)
731 ACPI_DEVICE_INFO devinfo;
735 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
740 /* fetch and validate the HID */
741 if ((h = acpi_get_handle(dev)) == NULL)
743 if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
745 if (!(devinfo.Valid & ACPI_VALID_HID))
748 pnpid = PNP_EISAID(devinfo.HardwareId);
755 acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
760 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
763 * ISA-style drivers attached to ACPI may persist and
764 * probe manually if we return ENOENT. We never want
765 * that to happen, so don't ever return it.
769 /* scan the supplied IDs for a match */
770 pnpid = acpi_isa_get_logicalid(child);
771 while (ids && ids->ip_id) {
772 if (pnpid == ids->ip_id) {
779 return_VALUE(result);
783 * Scan relevant portions of the ACPI namespace and attach child devices.
785 * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and \_SB_ scopes,
786 * and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
789 acpi_probe_children(device_t bus)
792 static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
795 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
799 * Create any static children by calling device identify methods.
801 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
802 bus_generic_probe(bus);
805 * Scan the namespace and insert placeholders for all the devices that
808 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
809 * we want to create nodes for all devices, not just those that are currently
810 * present. (This assumes that we don't want to create/remove devices as they
811 * appear, which might be smarter.)
813 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
814 for (i = 0; scopes[i] != NULL; i++)
815 if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent)))
816 AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL);
819 * Scan all of the child devices we have created and let them probe/attach.
821 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
822 bus_generic_attach(bus);
825 * Some of these children may have attached others as part of their attach
826 * process (eg. the root PCI bus driver), so rescan.
828 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
829 bus_generic_attach(bus);
831 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
836 * Evaluate a child device and determine whether we might attach a device to
840 acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
842 ACPI_OBJECT_TYPE type;
843 device_t child, bus = (device_t)context;
845 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
848 * Skip this device if we think we'll have trouble with it.
850 if (acpi_avoid(handle))
851 return_ACPI_STATUS(AE_OK);
853 if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
855 case ACPI_TYPE_DEVICE:
856 case ACPI_TYPE_PROCESSOR:
857 case ACPI_TYPE_THERMAL:
858 case ACPI_TYPE_POWER:
859 if (acpi_disabled("children"))
862 * Create a placeholder device for this node. Sort the placeholder
863 * so that the probe/attach passes will run breadth-first.
865 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", acpi_name(handle)));
866 child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
869 acpi_set_handle(child, handle);
872 * Check that the device is present. If it's not present,
873 * leave it disabled (so that we have a device_t attached to
874 * the handle, but we don't probe it).
876 if ((type == ACPI_TYPE_DEVICE) && (!acpi_DeviceIsPresent(child))) {
877 device_disable(child);
882 * Get the device's resource settings and attach them.
883 * Note that if the device has _PRS but no _CRS, we need
884 * to decide when it's appropriate to try to configure the
885 * device. Ignore the return value here; it's OK for the
886 * device not to have any resources.
888 acpi_parse_resources(child, handle, &acpi_res_parse_set);
890 /* if we're debugging, probe/attach now rather than later */
891 ACPI_DEBUG_EXEC(device_probe_and_attach(child));
895 return_ACPI_STATUS(AE_OK);
899 acpi_shutdown_pre_sync(void *arg, int howto)
905 * Disable all ACPI events before soft off, otherwise the system
906 * will be turned on again on some laptops.
908 * XXX this should probably be restricted to masking some events just
909 * before powering down, since we may still need ACPI during the
912 acpi_Disable((struct acpi_softc *)arg);
916 acpi_shutdown_final(void *arg, int howto)
922 if (howto & RB_POWEROFF) {
923 printf("Power system off using ACPI...\n");
924 if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(acpi_off_state))) {
925 printf("AcpiEnterSleepStatePrep failed - %s\n",
926 AcpiFormatException(status));
929 if (ACPI_FAILURE(status = AcpiEnterSleepState(acpi_off_state))) {
930 printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
933 printf("ACPI power-off failed - timeout\n");
936 printf("Terminate ACPI\n");
942 acpi_enable_fixed_events(struct acpi_softc *sc)
944 static int first_time = 1;
945 #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n"
949 /* Enable and clear fixed events and install handlers. */
950 if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) {
951 AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED, 0);
952 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED);
953 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
954 acpi_eventhandler_power_button_for_sleep, sc);
956 device_printf(sc->acpi_dev, MSGFORMAT, "power");
959 if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) {
960 AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED, 0);
961 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED);
962 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
963 acpi_eventhandler_sleep_button_for_sleep, sc);
965 device_printf(sc->acpi_dev, MSGFORMAT, "sleep");
973 * Returns true if the device is actually present and should
974 * be attached to. This requires the present, enabled, UI-visible
975 * and diagnostics-passed bits to be set.
978 acpi_DeviceIsPresent(device_t dev)
981 ACPI_DEVICE_INFO devinfo;
986 if ((h = acpi_get_handle(dev)) == NULL)
988 if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
990 /* if no _STA method, must be present */
991 if (!(devinfo.Valid & ACPI_VALID_STA))
993 /* return true for 'present' and 'functioning' */
994 if ((devinfo.CurrentStatus & 0x9) == 0x9)
1000 * Returns true if the battery is actually present and inserted.
1003 acpi_BatteryIsPresent(device_t dev)
1006 ACPI_DEVICE_INFO devinfo;
1011 if ((h = acpi_get_handle(dev)) == NULL)
1013 if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
1015 /* if no _STA method, must be present */
1016 if (!(devinfo.Valid & ACPI_VALID_STA))
1018 /* return true for 'present' and 'functioning' */
1019 if ((devinfo.CurrentStatus & 0x19) == 0x19)
1025 * Match a HID string against a device
1028 acpi_MatchHid(device_t dev, char *hid)
1031 ACPI_DEVICE_INFO devinfo;
1039 if ((h = acpi_get_handle(dev)) == NULL)
1041 if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
1043 if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId))
1045 if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &cid)))
1047 if (cid == PNP_EISAID(hid))
1053 * Return the handle of a named object within our scope, ie. that of (parent)
1054 * or one if its parents.
1057 acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1064 /* walk back up the tree to the root */
1066 if (ACPI_SUCCESS(status = AcpiGetHandle(parent, path, &r))) {
1070 if (status != AE_NOT_FOUND)
1072 if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1073 return(AE_NOT_FOUND);
1079 * Allocate a buffer with a preset data size.
1082 acpi_AllocBuffer(int size)
1086 if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1089 buf->Pointer = (void *)(buf + 1);
1094 * Evaluate a path that should return an integer.
1097 acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number)
1106 handle = ACPI_ROOT_OBJECT;
1109 * Assume that what we've been pointed at is an Integer object, or
1110 * a method that will return an Integer.
1112 buf.Pointer = ¶m;
1113 buf.Length = sizeof(param);
1114 if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
1115 if (param.Type == ACPI_TYPE_INTEGER) {
1116 *number = param.Integer.Value;
1123 * In some applications, a method that's expected to return an Integer
1124 * may instead return a Buffer (probably to simplify some internal
1125 * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer,
1126 * convert it into an Integer as best we can.
1130 if (error == AE_BUFFER_OVERFLOW) {
1131 if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1132 error = AE_NO_MEMORY;
1134 if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
1135 error = acpi_ConvertBufferToInteger(&buf, number);
1138 AcpiOsFree(buf.Pointer);
1144 acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number)
1149 p = (ACPI_OBJECT *)bufp->Pointer;
1150 if (p->Type == ACPI_TYPE_INTEGER) {
1151 *number = p->Integer.Value;
1154 if (p->Type != ACPI_TYPE_BUFFER)
1156 if (p->Buffer.Length > sizeof(int))
1157 return(AE_BAD_DATA);
1159 for (i = 0; i < p->Buffer.Length; i++)
1160 *number += (*(p->Buffer.Pointer + i) << (i * 8));
1165 * Iterate over the elements of an a package object, calling the supplied
1166 * function for each element.
1168 * XXX possible enhancement might be to abort traversal on error.
1171 acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg)
1176 if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE))
1177 return(AE_BAD_PARAMETER);
1179 /* iterate over components */
1180 for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++)
1187 * Find the (index)th resource object in a set.
1190 acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
1195 rp = (ACPI_RESOURCE *)buf->Pointer;
1199 if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
1200 return(AE_BAD_PARAMETER);
1201 /* check for terminator */
1202 if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
1204 return(AE_NOT_FOUND);
1205 rp = ACPI_RESOURCE_NEXT(rp);
1213 * Append an ACPI_RESOURCE to an ACPI_BUFFER.
1215 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
1216 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible
1217 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of
1220 #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512
1223 acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
1229 * Initialise the buffer if necessary.
1231 if (buf->Pointer == NULL) {
1232 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
1233 if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
1234 return(AE_NO_MEMORY);
1235 rp = (ACPI_RESOURCE *)buf->Pointer;
1236 rp->Id = ACPI_RSTYPE_END_TAG;
1243 * Scan the current buffer looking for the terminator.
1244 * This will either find the terminator or hit the end
1245 * of the buffer and return an error.
1247 rp = (ACPI_RESOURCE *)buf->Pointer;
1249 /* range check, don't go outside the buffer */
1250 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
1251 return(AE_BAD_PARAMETER);
1252 if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
1253 (rp->Length == 0)) {
1256 rp = ACPI_RESOURCE_NEXT(rp);
1260 * Check the size of the buffer and expand if required.
1263 * size of existing resources before terminator +
1264 * size of new resource and header +
1265 * size of terminator.
1267 * Note that this loop should really only run once, unless
1268 * for some reason we are stuffing a *really* huge resource.
1270 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
1271 res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
1272 ACPI_RESOURCE_LENGTH) >= buf->Length) {
1273 if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
1274 return(AE_NO_MEMORY);
1275 bcopy(buf->Pointer, newp, buf->Length);
1276 rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1277 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
1278 AcpiOsFree(buf->Pointer);
1279 buf->Pointer = newp;
1280 buf->Length += buf->Length;
1284 * Insert the new resource.
1286 bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
1289 * And add the terminator.
1291 rp = ACPI_RESOURCE_NEXT(rp);
1292 rp->Id = ACPI_RSTYPE_END_TAG;
1298 #define ACPI_MINIMUM_AWAKETIME 5
1301 acpi_sleep_enable(void *arg)
1303 ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1307 * Set the system sleep state
1309 * Currently we only support S1 and S5
1312 acpi_SetSleepState(struct acpi_softc *sc, int state)
1314 ACPI_STATUS status = AE_OK;
1318 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1321 if (sc->acpi_sstate != ACPI_STATE_S0)
1322 return_ACPI_STATUS(AE_BAD_PARAMETER); /* avoid reentry */
1324 if (sc->acpi_sleep_disabled)
1325 return_ACPI_STATUS(AE_OK);
1328 case ACPI_STATE_S0: /* XXX only for testing */
1329 if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
1330 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
1338 if (ACPI_FAILURE(status = AcpiHwGetSleepTypeData((UINT8)state, &TypeA, &TypeB))) {
1339 device_printf(sc->acpi_dev, "AcpiHwGetSleepTypeData failed - %s\n", AcpiFormatException(status));
1344 * Inform all devices that we are going to sleep.
1346 if (DEVICE_SUSPEND(root_bus) != 0) {
1348 * Re-wake the system.
1350 * XXX note that a better two-pass approach with a 'veto' pass
1351 * followed by a "real thing" pass would be better, but the
1352 * current bus interface does not provide for this.
1354 DEVICE_RESUME(root_bus);
1355 return_ACPI_STATUS(AE_ERROR);
1358 if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(state))) {
1359 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1360 AcpiFormatException(status));
1364 sc->acpi_sstate = state;
1365 sc->acpi_sleep_disabled = 1;
1367 if (state != ACPI_STATE_S1) {
1368 acpi_sleep_machdep(sc, state);
1370 /* AcpiEnterSleepState() maybe incompleted, unlock here. */
1371 AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
1373 /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1374 if (state == ACPI_STATE_S4) {
1378 if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
1379 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
1383 AcpiLeaveSleepState((UINT8)state);
1384 DEVICE_RESUME(root_bus);
1385 sc->acpi_sstate = ACPI_STATE_S0;
1386 acpi_enable_fixed_events(sc);
1391 * Shut down cleanly and power off. This will call us back through the
1392 * shutdown handlers.
1394 shutdown_nice(RB_POWEROFF);
1398 status = AE_BAD_PARAMETER;
1402 if (sc->acpi_sleep_disabled)
1403 timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1405 return_ACPI_STATUS(status);
1409 * Enable/Disable ACPI
1412 acpi_Enable(struct acpi_softc *sc)
1417 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1420 flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
1421 ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
1422 if (!sc->acpi_enabled) {
1423 status = AcpiEnableSubsystem(flags);
1427 if (status == AE_OK)
1428 sc->acpi_enabled = 1;
1429 return_ACPI_STATUS(status);
1433 acpi_Disable(struct acpi_softc *sc)
1437 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1440 if (sc->acpi_enabled) {
1441 status = AcpiDisable();
1445 if (status == AE_OK)
1446 sc->acpi_enabled = 0;
1447 return_ACPI_STATUS(status);
1451 * ACPI Event Handlers
1454 /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
1457 acpi_system_eventhandler_sleep(void *arg, int state)
1459 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1462 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
1463 acpi_SetSleepState((struct acpi_softc *)arg, state);
1469 acpi_system_eventhandler_wakeup(void *arg, int state)
1471 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1473 /* Well, what to do? :-) */
1482 * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
1485 acpi_eventhandler_power_button_for_sleep(void *context)
1487 struct acpi_softc *sc = (struct acpi_softc *)context;
1489 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1491 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
1493 return_VALUE(ACPI_INTERRUPT_HANDLED);
1497 acpi_eventhandler_power_button_for_wakeup(void *context)
1499 struct acpi_softc *sc = (struct acpi_softc *)context;
1501 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1503 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
1505 return_VALUE(ACPI_INTERRUPT_HANDLED);
1509 acpi_eventhandler_sleep_button_for_sleep(void *context)
1511 struct acpi_softc *sc = (struct acpi_softc *)context;
1513 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1515 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
1517 return_VALUE(ACPI_INTERRUPT_HANDLED);
1521 acpi_eventhandler_sleep_button_for_wakeup(void *context)
1523 struct acpi_softc *sc = (struct acpi_softc *)context;
1525 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1527 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
1529 return_VALUE(ACPI_INTERRUPT_HANDLED);
1533 * XXX This is kinda ugly, and should not be here.
1535 struct acpi_staticbuf {
1541 acpi_name(ACPI_HANDLE handle)
1543 static struct acpi_staticbuf buf;
1547 buf.buffer.Length = 512;
1548 buf.buffer.Pointer = &buf.data[0];
1550 if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
1551 return(buf.buffer.Pointer);
1552 return("(unknown path)");
1556 * Debugging/bug-avoidance. Avoid trying to fetch info on various
1557 * parts of the namespace.
1560 acpi_avoid(ACPI_HANDLE handle)
1562 char *cp, *env, *np;
1565 np = acpi_name(handle);
1568 if ((env = getenv("debug.acpi.avoid")) == NULL)
1571 /* scan the avoid list checking for a match */
1574 while ((*cp != 0) && isspace(*cp))
1579 while ((cp[len] != 0) && !isspace(cp[len]))
1581 if (!strncmp(cp, np, len)) {
1592 * Debugging/bug-avoidance. Disable ACPI subsystem components.
1595 acpi_disabled(char *subsys)
1600 if ((env = getenv("debug.acpi.disable")) == NULL)
1602 if (!strcmp(env, "all")) {
1607 /* scan the disable list checking for a match */
1610 while ((*cp != 0) && isspace(*cp))
1615 while ((cp[len] != 0) && !isspace(cp[len]))
1617 if (!strncmp(cp, subsys, len)) {
1628 * Control interface.
1630 * We multiplex ioctls for all participating ACPI devices here. Individual
1631 * drivers wanting to be accessible via /dev/acpi should use the register/deregister
1632 * interface to make their handlers visible.
1634 struct acpi_ioctl_hook
1636 TAILQ_ENTRY(acpi_ioctl_hook) link;
1638 int (* fn)(u_long cmd, caddr_t addr, void *arg);
1642 static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks;
1643 static int acpi_ioctl_hooks_initted;
1646 * Register an ioctl handler.
1649 acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg)
1651 struct acpi_ioctl_hook *hp;
1653 if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
1658 if (acpi_ioctl_hooks_initted == 0) {
1659 TAILQ_INIT(&acpi_ioctl_hooks);
1660 acpi_ioctl_hooks_initted = 1;
1662 TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
1667 * Deregister an ioctl handler.
1670 acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg))
1672 struct acpi_ioctl_hook *hp;
1674 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
1675 if ((hp->cmd == cmd) && (hp->fn == fn))
1679 TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
1680 free(hp, M_ACPIDEV);
1685 acpiopen(dev_t dev, int flag, int fmt, struct thread *td)
1691 acpiclose(dev_t dev, int flag, int fmt, struct thread *td)
1697 acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1699 struct acpi_softc *sc;
1700 struct acpi_ioctl_hook *hp;
1701 int error, xerror, state;
1709 * Scan the list of registered ioctls, looking for handlers.
1711 if (acpi_ioctl_hooks_initted) {
1712 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
1713 if (hp->cmd == cmd) {
1714 xerror = hp->fn(cmd, addr, hp->arg);
1723 * Core system ioctls.
1727 if (ACPI_FAILURE(acpi_Enable(sc)))
1731 case ACPIIO_DISABLE:
1732 if (ACPI_FAILURE(acpi_Disable(sc)))
1736 case ACPIIO_SETSLPSTATE:
1737 if (!sc->acpi_enabled) {
1741 state = *(int *)addr;
1742 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) {
1743 acpi_SetSleepState(sc, state);
1761 acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
1763 char sleep_state[10];
1765 u_int new_state, old_state;
1767 old_state = *(u_int *)oidp->oid_arg1;
1768 if (old_state > ACPI_S_STATES_MAX+1) {
1769 strcpy(sleep_state, "unknown");
1771 bzero(sleep_state, sizeof(sleep_state));
1772 strncpy(sleep_state, sleep_state_names[old_state],
1773 sizeof(sleep_state_names[old_state]));
1775 error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
1776 if (error == 0 && req->newptr != NULL) {
1777 for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX+1; new_state++) {
1778 if (strncmp(sleep_state, sleep_state_names[new_state],
1779 sizeof(sleep_state)) == 0)
1782 if (new_state <= ACPI_S_STATES_MAX+1) {
1783 if (new_state != old_state) {
1784 *(u_int *)oidp->oid_arg1 = new_state;
1795 * Support for parsing debug options from the kernel environment.
1797 * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
1798 * by specifying the names of the bits in the debug.acpi.layer and
1799 * debug.acpi.level environment variables. Bits may be unset by
1800 * prefixing the bit name with !.
1808 static struct debugtag dbg_layer[] = {
1809 {"ACPI_UTILITIES", ACPI_UTILITIES},
1810 {"ACPI_HARDWARE", ACPI_HARDWARE},
1811 {"ACPI_EVENTS", ACPI_EVENTS},
1812 {"ACPI_TABLES", ACPI_TABLES},
1813 {"ACPI_NAMESPACE", ACPI_NAMESPACE},
1814 {"ACPI_PARSER", ACPI_PARSER},
1815 {"ACPI_DISPATCHER", ACPI_DISPATCHER},
1816 {"ACPI_EXECUTER", ACPI_EXECUTER},
1817 {"ACPI_RESOURCES", ACPI_RESOURCES},
1818 {"ACPI_DEBUGGER", ACPI_DEBUGGER},
1819 {"ACPI_OS_SERVICES", ACPI_OS_SERVICES},
1821 {"ACPI_BUS", ACPI_BUS},
1822 {"ACPI_SYSTEM", ACPI_SYSTEM},
1823 {"ACPI_POWER", ACPI_POWER},
1824 {"ACPI_EC", ACPI_EC},
1825 {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER},
1826 {"ACPI_BATTERY", ACPI_BATTERY},
1827 {"ACPI_BUTTON", ACPI_BUTTON},
1828 {"ACPI_PROCESSOR", ACPI_PROCESSOR},
1829 {"ACPI_THERMAL", ACPI_THERMAL},
1830 {"ACPI_FAN", ACPI_FAN},
1832 {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS},
1833 {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS},
1837 static struct debugtag dbg_level[] = {
1838 {"ACPI_LV_OK", ACPI_LV_OK},
1839 {"ACPI_LV_INFO", ACPI_LV_INFO},
1840 {"ACPI_LV_WARN", ACPI_LV_WARN},
1841 {"ACPI_LV_ERROR", ACPI_LV_ERROR},
1842 {"ACPI_LV_FATAL", ACPI_LV_FATAL},
1843 {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT},
1844 {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS},
1845 {"ACPI_LV_THREADS", ACPI_LV_THREADS},
1846 {"ACPI_LV_PARSE", ACPI_LV_PARSE},
1847 {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH},
1848 {"ACPI_LV_LOAD", ACPI_LV_LOAD},
1849 {"ACPI_LV_EXEC", ACPI_LV_EXEC},
1850 {"ACPI_LV_NAMES", ACPI_LV_NAMES},
1851 {"ACPI_LV_OPREGION", ACPI_LV_OPREGION},
1852 {"ACPI_LV_BFIELD", ACPI_LV_BFIELD},
1853 {"ACPI_LV_TABLES", ACPI_LV_TABLES},
1854 {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS},
1855 {"ACPI_LV_VALUES", ACPI_LV_VALUES},
1856 {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS},
1857 {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS},
1858 {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES},
1859 {"ACPI_LV_IO", ACPI_LV_IO},
1860 {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS},
1861 {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS},
1862 {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE},
1863 {"ACPI_LV_MUTEX", ACPI_LV_MUTEX},
1864 {"ACPI_LV_INIT", ACPI_LV_INIT},
1865 {"ACPI_LV_ALL", ACPI_LV_ALL},
1866 {"ACPI_DB_AML_DISASSEMBLE", ACPI_DB_AML_DISASSEMBLE},
1867 {"ACPI_DB_VERBOSE_INFO", ACPI_DB_VERBOSE_INFO},
1868 {"ACPI_DB_FULL_TABLES", ACPI_DB_FULL_TABLES},
1869 {"ACPI_DB_EVENTS", ACPI_DB_EVENTS},
1870 {"ACPI_DB_VERBOSE", ACPI_DB_VERBOSE},
1875 acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
1887 while (*ep && !isspace(*ep))
1898 for (i = 0; tag[i].name != NULL; i++) {
1899 if (!strncmp(cp, tag[i].name, l)) {
1901 *flag |= tag[i].value;
1903 *flag &= ~tag[i].value;
1905 printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
1913 acpi_set_debugging(void *junk)
1919 if ((cp = getenv("debug.acpi.layer")) != NULL) {
1920 acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
1923 if ((cp = getenv("debug.acpi.level")) != NULL) {
1924 acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
1928 printf("ACPI debug layer 0x%x debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel);
1930 SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL);
1934 acpi_pm_func(u_long cmd, void *arg, ...)
1936 int state, acpi_state;
1938 struct acpi_softc *sc;
1943 case POWER_CMD_SUSPEND:
1944 sc = (struct acpi_softc *)arg;
1951 state = va_arg(ap, int);
1955 case POWER_SLEEP_STATE_STANDBY:
1956 acpi_state = sc->acpi_standby_sx;
1958 case POWER_SLEEP_STATE_SUSPEND:
1959 acpi_state = sc->acpi_suspend_sx;
1961 case POWER_SLEEP_STATE_HIBERNATE:
1962 acpi_state = ACPI_STATE_S4;
1969 acpi_SetSleepState(sc, acpi_state);
1982 acpi_pm_register(void *arg)
1985 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
1988 SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);