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
29 * $FreeBSD: src/sys/dev/acpica/acpi.c,v 1.145 2004/05/06 08:04:34 njl Exp $
30 * $DragonFly: src/sys/dev/acpica5/acpi.c,v 1.5 2004/06/27 08:52:39 dillon Exp $
34 #include <sys/param.h>
35 #include <sys/kernel.h>
37 #include <sys/fcntl.h>
38 #include <sys/malloc.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>
49 #include <machine/clock.h>
50 #include <machine/globaldata.h>
51 #include <machine/resource.h>
52 #include <machine/bus.h>
54 #include <bus/isa/isavar.h>
57 #include <dev/acpica5/acpivar.h>
58 #include <dev/acpica5/acpiio.h>
61 MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
63 /* Hooks for the ACPI CA debugging infrastructure */
64 #define _COMPONENT ACPI_BUS
65 ACPI_MODULE_NAME("ACPI")
67 static d_open_t acpiopen;
68 static d_close_t acpiclose;
69 static d_ioctl_t acpiioctl;
71 #define CDEV_MAJOR 152
72 static struct cdevsw acpi_cdevsw = {
79 .old_close = acpiclose,
80 .old_ioctl = acpiioctl
83 #if __FreeBSD_version >= 500000
84 struct mtx acpi_mutex;
93 #define ACPI_OEM_REV_ANY 0
95 static struct acpi_quirks acpi_quirks_table[] = {
97 /* Bad PCI routing table. Used on some SuperMicro boards. */
98 { "PTLTD ", 0x06040000, "pci_link" },
104 static int acpi_modevent(struct module *mod, int event, void *junk);
105 static void acpi_identify(driver_t *driver, device_t parent);
106 static int acpi_probe(device_t dev);
107 static int acpi_attach(device_t dev);
108 static void acpi_quirks_set(void);
109 static device_t acpi_add_child(device_t bus, int order, const char *name,
111 static int acpi_print_child(device_t bus, device_t child);
112 static int acpi_read_ivar(device_t dev, device_t child, int index,
114 static int acpi_write_ivar(device_t dev, device_t child, int index,
116 static int acpi_set_resource(device_t dev, device_t child, int type,
117 int rid, u_long start, u_long count);
118 static int acpi_get_resource(device_t dev, device_t child, int type,
119 int rid, u_long *startp, u_long *countp);
120 static struct resource *acpi_alloc_resource(device_t bus, device_t child,
121 int type, int *rid, u_long start, u_long end,
122 u_long count, u_int flags);
123 static int acpi_release_resource(device_t bus, device_t child, int type,
124 int rid, struct resource *r);
125 static uint32_t acpi_isa_get_logicalid(device_t dev);
126 static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
127 static int acpi_isa_pnp_probe(device_t bus, device_t child,
128 struct isa_pnp_id *ids);
129 static void acpi_probe_children(device_t bus);
130 static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
131 void *context, void **status);
132 static void acpi_shutdown_pre_sync(void *arg, int howto);
133 static void acpi_shutdown_final(void *arg, int howto);
134 static void acpi_shutdown_poweroff(void *arg);
135 static void acpi_enable_fixed_events(struct acpi_softc *sc);
136 static void acpi_system_eventhandler_sleep(void *arg, int state);
137 static void acpi_system_eventhandler_wakeup(void *arg, int state);
138 static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
139 static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
140 static int acpi_pm_func(u_long cmd, void *arg, ...);
141 static int acpi_child_location_str_method(device_t acdev, device_t child,
142 char *buf, size_t buflen);
143 static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child,
144 char *buf, size_t buflen);
146 static device_method_t acpi_methods[] = {
147 /* Device interface */
148 DEVMETHOD(device_identify, acpi_identify),
149 DEVMETHOD(device_probe, acpi_probe),
150 DEVMETHOD(device_attach, acpi_attach),
151 DEVMETHOD(device_detach, bus_generic_detach),
152 DEVMETHOD(device_shutdown, bus_generic_shutdown),
153 DEVMETHOD(device_suspend, bus_generic_suspend),
154 DEVMETHOD(device_resume, bus_generic_resume),
157 DEVMETHOD(bus_add_child, acpi_add_child),
158 DEVMETHOD(bus_print_child, acpi_print_child),
159 DEVMETHOD(bus_read_ivar, acpi_read_ivar),
160 DEVMETHOD(bus_write_ivar, acpi_write_ivar),
161 DEVMETHOD(bus_set_resource, acpi_set_resource),
162 DEVMETHOD(bus_get_resource, acpi_get_resource),
163 DEVMETHOD(bus_alloc_resource, acpi_alloc_resource),
164 DEVMETHOD(bus_release_resource, acpi_release_resource),
165 DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method),
166 DEVMETHOD(bus_child_location_str, acpi_child_location_str_method),
167 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
168 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
169 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
170 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
171 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
174 DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe),
179 static driver_t acpi_driver = {
182 sizeof(struct acpi_softc),
185 static devclass_t acpi_devclass;
186 DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
187 MODULE_VERSION(acpi, 1);
189 static const char* sleep_state_names[] = {
190 "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
192 SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging");
193 static char acpi_ca_version[12];
194 SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
195 acpi_ca_version, 0, "Version of Intel ACPI-CA");
198 * Allow override of whether methods execute in parallel or not.
199 * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS"
200 * errors for AML that really can't handle parallel method execution.
201 * It is off by default since this breaks recursive methods and
202 * some IBMs use such code.
204 static int acpi_serialize_methods;
205 TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods);
208 * Allow override of whether to support the _OSI method. This allows us
209 * to claim compatibility with various MS OSs without changing the value
210 * we report for _OS. This is enabled by default since it fixes some
211 * problems with interrupt routing although it can be disabled if it
212 * causes problems. See the definition of "AcpiGbl_ValidOsiStrings" for
213 * a list of systems we claim.
215 static int acpi_osi_method = TRUE;
216 TUNABLE_INT("hw.acpi.osi_method", &acpi_osi_method);
219 * ACPI can only be loaded as a module by the loader; activating it after
220 * system bootstrap time is not useful, and can be fatal to the system.
221 * It also cannot be unloaded, since the entire system bus heirarchy hangs
225 acpi_modevent(struct module *mod, int event, void *junk)
230 printf("The ACPI driver cannot be loaded after boot.\n");
235 if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
245 * Perform early initialization.
253 static int error, started = 0;
255 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
258 return_VALUE (error);
261 #if __FreeBSD_version >= 500000
262 /* Initialise the ACPI mutex */
263 mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
268 * Set the globals from our tunables. This is needed because ACPI-CA
269 * uses UINT8 for some values and we have no tunable_uint8.
271 AcpiGbl_AllMethodsSerialized = acpi_serialize_methods;
272 AcpiGbl_CreateOsiMethod = acpi_osi_method;
273 AcpiGbl_LeaveWakeGpesDisabled = FALSE;
276 /* Start up the ACPI CA subsystem. */
278 debugpoint = getenv("debug.acpi.debugger");
280 if (!strcmp(debugpoint, "init"))
281 acpi_EnterDebugger();
285 if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
286 printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
287 return_VALUE (error);
290 debugpoint = getenv("debug.acpi.debugger");
292 if (!strcmp(debugpoint, "tables"))
293 acpi_EnterDebugger();
298 if (ACPI_FAILURE(error = AcpiLoadTables())) {
299 printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
303 /* Set up any quirks we have for this XSDT. */
305 if (acpi_disabled("acpi"))
306 return_VALUE (AE_ERROR);
308 return_VALUE (AE_OK);
312 * Detect ACPI, perform early initialisation
315 acpi_identify(driver_t *driver, device_t parent)
319 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
324 /* Check that we haven't been disabled with a hint. */
325 if (resource_disabled("acpi", 0))
328 /* Make sure we're not being doubly invoked. */
329 if (device_find_child(parent, "acpi", 0) != NULL)
332 /* Initialize ACPI-CA. */
333 if (ACPI_FAILURE(acpi_Startup()))
336 snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION);
338 /* Attach the actual ACPI device. */
339 if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
340 device_printf(parent, "ACPI: could not attach\n");
346 * Fetch some descriptive data from ACPI to put in our attach message
349 acpi_probe(device_t dev)
351 ACPI_TABLE_HEADER th;
358 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
360 if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
361 power_pm_get_type() != POWER_PM_TYPE_ACPI) {
363 device_printf(dev, "Other PM system enabled.\n");
369 if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
370 device_printf(dev, "couldn't get XSDT header: %s\n",
371 AcpiFormatException(status));
374 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
375 sbuf_bcat(&sb, th.OemId, 6);
378 sbuf_bcat(&sb, th.OemTableId, 8);
381 device_set_desc_copy(dev, sbuf_data(&sb));
390 acpi_attach(device_t dev)
392 struct acpi_softc *sc;
403 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
405 sc = device_get_softc(dev);
406 bzero(sc, sizeof(*sc));
410 debugpoint = getenv("debug.acpi.debugger");
412 if (!strcmp(debugpoint, "spaces"))
413 acpi_EnterDebugger();
418 /* Install the default address space handlers. */
420 status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
421 ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
422 if (ACPI_FAILURE(status)) {
423 device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
424 AcpiFormatException(status));
427 status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
428 ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
429 if (ACPI_FAILURE(status)) {
430 device_printf(dev, "Could not initialise SystemIO handler: %s\n",
431 AcpiFormatException(status));
434 status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
435 ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
436 if (ACPI_FAILURE(status)) {
437 device_printf(dev, "could not initialise PciConfig handler: %s\n",
438 AcpiFormatException(status));
443 * Bring ACPI fully online.
445 * Note that some systems (specifically, those with namespace evaluation
446 * issues that require the avoidance of parts of the namespace) must
447 * avoid running _INI and _STA on everything, as well as dodging the final
450 * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
452 * XXX We should arrange for the object init pass after we have attached
453 * all our child devices, but on many systems it works here.
456 debugpoint = getenv("debug.acpi.debugger");
458 if (!strcmp(debugpoint, "enable"))
459 acpi_EnterDebugger();
464 if (testenv("debug.acpi.avoid"))
465 flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
466 if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
467 device_printf(dev, "Could not enable ACPI: %s\n",
468 AcpiFormatException(status));
473 * Call the ECDT probe function to provide EC functionality before
474 * the namespace has been evaluated.
476 acpi_ec_ecdt_probe(dev);
478 if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
479 device_printf(dev, "Could not initialize ACPI objects: %s\n",
480 AcpiFormatException(status));
485 * Setup our sysctl tree.
487 * XXX: This doesn't check to make sure that none of these fail.
489 sysctl_ctx_init(&sc->acpi_sysctl_ctx);
490 sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
491 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
492 device_get_name(dev), CTLFLAG_RD, 0, "");
493 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
494 OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
495 0, 0, acpi_supported_sleep_state_sysctl, "A", "");
496 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
497 OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
498 &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
499 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
500 OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
501 &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
502 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
503 OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
504 &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
505 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
506 OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
507 &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
508 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
509 OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
510 &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
511 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
512 OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
513 &sc->acpi_sleep_delay, 0, "sleep delay");
514 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
515 OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
516 &sc->acpi_s4bios, 0, "S4BIOS mode");
517 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
518 OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
519 &sc->acpi_verbose, 0, "verbose mode");
520 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
521 OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW,
522 &sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff");
525 * Default to 5 seconds before sleeping to give some machines time to
528 sc->acpi_sleep_delay = 5;
529 sc->acpi_disable_on_poweroff = 1;
531 sc->acpi_verbose = 1;
532 if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) {
533 sc->acpi_verbose = 1;
537 /* Only enable S4BIOS by default if the FACS says it is available. */
538 if (AcpiGbl_FACS->S4Bios_f != 0)
542 * Dispatch the default sleep state to devices. The lid switch is set
543 * to NONE by default to avoid surprising users.
545 sc->acpi_power_button_sx = ACPI_STATE_S5;
546 sc->acpi_lid_switch_sx = ACPI_S_STATES_MAX + 1;
547 sc->acpi_standby_sx = ACPI_STATE_S1;
548 sc->acpi_suspend_sx = ACPI_STATE_S3;
550 /* Pick the first valid sleep state for the sleep button default. */
551 sc->acpi_sleep_button_sx = ACPI_S_STATES_MAX + 1;
552 for (state = ACPI_STATE_S1; state < ACPI_STATE_S5; state++)
553 if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
554 sc->acpi_sleep_button_sx = state;
558 acpi_enable_fixed_events(sc);
561 * Scan the namespace and attach/initialise children.
564 debugpoint = getenv("debug.acpi.debugger");
566 if (!strcmp(debugpoint, "probe"))
567 acpi_EnterDebugger();
572 /* Register our shutdown handlers */
573 EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc,
575 EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
579 * Register our acpi event handlers.
580 * XXX should be configurable eg. via userland policy manager.
582 EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
583 sc, ACPI_EVENT_PRI_LAST);
584 EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
585 sc, ACPI_EVENT_PRI_LAST);
587 /* Flag our initial states. */
588 sc->acpi_enabled = 1;
589 sc->acpi_sstate = ACPI_STATE_S0;
590 sc->acpi_sleep_disabled = 0;
592 /* Create the control device */
593 cdevsw_add(&acpi_cdevsw, 0, 0);
594 sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
596 sc->acpi_dev_t->si_drv1 = sc;
599 debugpoint = getenv("debug.acpi.debugger");
601 if (strcmp(debugpoint, "running") == 0)
602 acpi_EnterDebugger();
607 #ifdef ACPI_USE_THREADS
608 if ((error = acpi_task_thread_init()))
612 if ((error = acpi_machdep_init(dev)))
615 /* Register ACPI again to pass the correct argument of pm_func. */
616 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
618 if (!acpi_disabled("bus"))
619 acpi_probe_children(dev);
625 return_VALUE (error);
631 XSDT_DESCRIPTOR *xsdt;
632 struct acpi_quirks *quirk;
637 * If the user loaded a custom table or disabled "quirks", leave
638 * the settings alone.
641 if ((env = getenv("acpi_dsdt_load")) != NULL) {
642 /* XXX No strcasecmp but this is good enough. */
643 if (*env == 'Y' || *env == 'y')
647 if ((env = getenv("debug.acpi.disabled")) != NULL) {
648 if (strstr("quirks", env) != NULL)
654 * Search through our quirk table and concatenate the disabled
655 * values with whatever we find.
658 for (quirk = acpi_quirks_table; quirk->OemId; quirk++) {
659 if (!strncmp(xsdt->OemId, quirk->OemId, strlen(quirk->OemId)) &&
660 (xsdt->OemRevision == quirk->OemRevision ||
661 quirk->OemRevision == ACPI_OEM_REV_ANY)) {
662 len += strlen(quirk->value) + 2;
663 if ((tmp = malloc(len, M_TEMP, M_NOWAIT)) == NULL)
665 sprintf(tmp, "%s %s", env ? env : "", quirk->value);
667 setenv("debug.acpi.disabled", tmp);
680 * Handle a new device being added
683 acpi_add_child(device_t bus, int order, const char *name, int unit)
685 struct acpi_device *ad;
688 ad = malloc(sizeof(*ad), M_ACPIDEV, M_INTWAIT | M_ZERO);
690 resource_list_init(&ad->ad_rl);
692 child = device_add_child_ordered(bus, order, name, unit);
694 device_set_ivars(child, ad);
699 acpi_print_child(device_t bus, device_t child)
701 struct acpi_device *adev = device_get_ivars(child);
702 struct resource_list *rl = &adev->ad_rl;
705 retval += bus_print_child_header(bus, child);
706 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
707 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
708 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
709 retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld");
710 retval += bus_print_child_footer(bus, child);
715 /* Location hint for devctl(8) */
717 acpi_child_location_str_method(device_t cbdev, device_t child, char *buf,
720 struct acpi_device *dinfo = device_get_ivars(child);
722 if (dinfo->ad_handle)
723 snprintf(buf, buflen, "path=%s", acpi_name(dinfo->ad_handle));
725 snprintf(buf, buflen, "magic=unknown");
729 /* PnP information for devctl(8) */
731 acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
734 ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL};
735 ACPI_DEVICE_INFO *adinfo;
736 struct acpi_device *dinfo = device_get_ivars(child);
740 error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf);
741 adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer;
744 snprintf(buf, buflen, "Unknown");
746 snprintf(buf, buflen, "_HID=%s _UID=%lu",
747 (adinfo->Valid & ACPI_VALID_HID) ?
748 adinfo->HardwareId.Value : "UNKNOWN",
749 (adinfo->Valid & ACPI_VALID_UID) ?
750 strtoul(adinfo->UniqueId.Value, &end, 10) : 0);
759 * Handle per-device ivars
762 acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
764 struct acpi_device *ad;
766 if ((ad = device_get_ivars(child)) == NULL) {
767 printf("device has no ivars\n");
771 /* ACPI and ISA compatibility ivars */
773 case ACPI_IVAR_HANDLE:
774 *(ACPI_HANDLE *)result = ad->ad_handle;
776 case ACPI_IVAR_MAGIC:
777 *(int *)result = ad->ad_magic;
779 case ACPI_IVAR_PRIVATE:
780 *(void **)result = ad->ad_private;
782 case ISA_IVAR_VENDORID:
783 case ISA_IVAR_SERIAL:
784 case ISA_IVAR_COMPATID:
787 case ISA_IVAR_LOGICALID:
788 *(int *)result = acpi_isa_get_logicalid(child);
798 acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
800 struct acpi_device *ad;
802 if ((ad = device_get_ivars(child)) == NULL) {
803 printf("device has no ivars\n");
808 case ACPI_IVAR_HANDLE:
809 ad->ad_handle = (ACPI_HANDLE)value;
811 case ACPI_IVAR_MAGIC:
812 ad->ad_magic = (int)value;
814 case ACPI_IVAR_PRIVATE:
815 ad->ad_private = (void *)value;
818 panic("bad ivar write request (%d)", index);
826 * Handle child resource allocation/removal
829 acpi_set_resource(device_t dev, device_t child, int type, int rid,
830 u_long start, u_long count)
832 struct acpi_device *ad = device_get_ivars(child);
833 struct resource_list *rl = &ad->ad_rl;
835 resource_list_add(rl, type, rid, start, start + count -1, count);
841 acpi_get_resource(device_t dev, device_t child, int type, int rid,
842 u_long *startp, u_long *countp)
844 struct acpi_device *ad = device_get_ivars(child);
845 struct resource_list *rl = &ad->ad_rl;
846 struct resource_list_entry *rle;
848 rle = resource_list_find(rl, type, rid);
853 *startp = rle->start;
855 *countp = rle->count;
860 static struct resource *
861 acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
862 u_long start, u_long end, u_long count, u_int flags)
864 struct acpi_device *ad = device_get_ivars(child);
865 struct resource_list *rl = &ad->ad_rl;
867 return (resource_list_alloc(rl, bus, child, type, rid, start, end, count,
872 acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
874 struct acpi_device *ad = device_get_ivars(child);
875 struct resource_list *rl = &ad->ad_rl;
877 return (resource_list_release(rl, bus, child, type, rid, r));
880 /* Allocate an IO port or memory resource, given its GAS. */
882 acpi_bus_alloc_gas(device_t dev, int *rid, ACPI_GENERIC_ADDRESS *gas)
886 if (gas == NULL || !ACPI_VALID_ADDRESS(gas->Address) ||
887 gas->RegisterBitWidth < 8)
890 switch (gas->AddressSpaceId) {
891 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
892 type = SYS_RES_MEMORY;
894 case ACPI_ADR_SPACE_SYSTEM_IO:
895 type = SYS_RES_IOPORT;
901 bus_set_resource(dev, type, *rid, gas->Address, gas->RegisterBitWidth / 8);
902 return (bus_alloc_resource_any(dev, type, rid, RF_ACTIVE));
906 * Handle ISA-like devices probing for a PnP ID to match.
908 #define PNP_EISAID(s) \
909 ((((s[0] - '@') & 0x1f) << 2) \
910 | (((s[1] - '@') & 0x18) >> 3) \
911 | (((s[1] - '@') & 0x07) << 13) \
912 | (((s[2] - '@') & 0x1f) << 8) \
913 | (PNP_HEXTONUM(s[4]) << 16) \
914 | (PNP_HEXTONUM(s[3]) << 20) \
915 | (PNP_HEXTONUM(s[6]) << 24) \
916 | (PNP_HEXTONUM(s[5]) << 28))
919 acpi_isa_get_logicalid(device_t dev)
921 ACPI_DEVICE_INFO *devinfo;
928 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
932 buf.Length = ACPI_ALLOCATE_BUFFER;
936 /* Fetch and validate the HID. */
937 if ((h = acpi_get_handle(dev)) == NULL)
939 error = AcpiGetObjectInfo(h, &buf);
940 if (ACPI_FAILURE(error))
942 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
944 if ((devinfo->Valid & ACPI_VALID_HID) != 0)
945 pnpid = PNP_EISAID(devinfo->HardwareId.Value);
948 if (buf.Pointer != NULL)
949 AcpiOsFree(buf.Pointer);
951 return_VALUE (pnpid);
955 acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
957 ACPI_DEVICE_INFO *devinfo;
965 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
970 buf.Length = ACPI_ALLOCATE_BUFFER;
974 /* Fetch and validate the CID */
975 if ((h = acpi_get_handle(dev)) == NULL)
977 error = AcpiGetObjectInfo(h, &buf);
978 if (ACPI_FAILURE(error))
980 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
981 if ((devinfo->Valid & ACPI_VALID_CID) == 0)
984 if (devinfo->CompatibilityId.Count < count)
985 count = devinfo->CompatibilityId.Count;
986 for (i = 0; i < count; i++) {
987 if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0)
989 *pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value);
994 if (buf.Pointer != NULL)
995 AcpiOsFree(buf.Pointer);
997 return_VALUE (valid);
1001 acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
1003 int result, cid_count, i;
1004 uint32_t lid, cids[8];
1006 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1009 * ISA-style drivers attached to ACPI may persist and
1010 * probe manually if we return ENOENT. We never want
1011 * that to happen, so don't ever return it.
1015 /* Scan the supplied IDs for a match */
1016 lid = acpi_isa_get_logicalid(child);
1017 cid_count = acpi_isa_get_compatid(child, cids, 8);
1018 while (ids && ids->ip_id) {
1019 if (lid == ids->ip_id) {
1023 for (i = 0; i < cid_count; i++) {
1024 if (cids[i] == ids->ip_id) {
1033 return_VALUE (result);
1037 * Scan relevant portions of the ACPI namespace and attach child devices.
1039 * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and
1040 * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
1043 acpi_probe_children(device_t bus)
1047 static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
1050 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1053 /* Create any static children by calling device identify methods. */
1054 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
1055 bus_generic_probe(bus);
1058 * Scan the namespace and insert placeholders for all the devices that
1061 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1062 * we want to create nodes for all devices, not just those that are
1063 * currently present. (This assumes that we don't want to create/remove
1064 * devices as they appear, which might be smarter.)
1066 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
1067 for (i = 0; scopes[i] != NULL; i++) {
1068 status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent);
1069 if (ACPI_SUCCESS(status)) {
1070 AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child,
1076 * Scan all of the child devices we have created and let them probe/attach.
1078 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
1079 bus_generic_attach(bus);
1082 * Some of these children may have attached others as part of their attach
1083 * process (eg. the root PCI bus driver), so rescan.
1085 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
1086 bus_generic_attach(bus);
1088 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
1093 * Evaluate a child device and determine whether we might attach a device to
1097 acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
1099 ACPI_OBJECT_TYPE type;
1100 device_t child, bus = (device_t)context;
1102 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1104 /* Skip this device if we think we'll have trouble with it. */
1105 if (acpi_avoid(handle))
1106 return_ACPI_STATUS (AE_OK);
1108 if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
1110 case ACPI_TYPE_DEVICE:
1111 case ACPI_TYPE_PROCESSOR:
1112 case ACPI_TYPE_THERMAL:
1113 case ACPI_TYPE_POWER:
1114 if (acpi_disabled("children"))
1118 * Create a placeholder device for this node. Sort the placeholder
1119 * so that the probe/attach passes will run breadth-first.
1121 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n",
1122 acpi_name(handle)));
1123 child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
1126 acpi_set_handle(child, handle);
1129 * Check that the device is present. If it's not present,
1130 * leave it disabled (so that we have a device_t attached to
1131 * the handle, but we don't probe it).
1133 if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
1134 device_disable(child);
1139 * Get the device's resource settings and attach them.
1140 * Note that if the device has _PRS but no _CRS, we need
1141 * to decide when it's appropriate to try to configure the
1142 * device. Ignore the return value here; it's OK for the
1143 * device not to have any resources.
1145 acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
1147 /* If we're debugging, probe/attach now rather than later */
1148 ACPI_DEBUG_EXEC(device_probe_and_attach(child));
1153 return_ACPI_STATUS (AE_OK);
1157 acpi_shutdown_pre_sync(void *arg, int howto)
1159 struct acpi_softc *sc = arg;
1164 * Disable all ACPI events before soft off, otherwise the system
1165 * will be turned on again on some laptops.
1167 * XXX this should probably be restricted to masking some events just
1168 * before powering down, since we may still need ACPI during the
1171 if (sc->acpi_disable_on_poweroff)
1176 acpi_shutdown_final(void *arg, int howto)
1182 * If powering off, run the actual shutdown code on each processor.
1183 * It will only perform the shutdown on the BSP. Some chipsets do
1184 * not power off the system correctly if called from an AP.
1186 if ((howto & RB_POWEROFF) != 0) {
1187 status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1188 if (ACPI_FAILURE(status)) {
1189 printf("AcpiEnterSleepStatePrep failed - %s\n",
1190 AcpiFormatException(status));
1193 printf("Powering system off using ACPI\n");
1195 smp_rendezvous(NULL, acpi_shutdown_poweroff, NULL, NULL);
1197 acpi_shutdown_poweroff(NULL);
1200 printf("Shutting down ACPI\n");
1206 * Since this function may be called with locks held or in an unknown
1207 * context, it cannot allocate memory, acquire locks, sleep, etc.
1210 acpi_shutdown_poweroff(void *arg)
1216 /* Only attempt to power off if this is the BSP (cpuid 0). */
1217 if (mdcpu->mi.gd_cpuid != 0)
1220 ACPI_DISABLE_IRQS();
1221 status = AcpiEnterSleepState(ACPI_STATE_S5);
1222 if (ACPI_FAILURE(status)) {
1223 printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
1226 printf("ACPI power-off failed - timeout\n");
1231 acpi_enable_fixed_events(struct acpi_softc *sc)
1233 static int first_time = 1;
1237 /* Enable and clear fixed events and install handlers. */
1238 if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
1239 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
1240 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
1241 acpi_event_power_button_sleep, sc);
1243 device_printf(sc->acpi_dev, "Power Button (fixed)\n");
1245 if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
1246 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
1247 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
1248 acpi_event_sleep_button_sleep, sc);
1250 device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
1257 * Returns true if the device is actually present and should
1258 * be attached to. This requires the present, enabled, UI-visible
1259 * and diagnostics-passed bits to be set.
1262 acpi_DeviceIsPresent(device_t dev)
1264 ACPI_DEVICE_INFO *devinfo;
1273 if ((h = acpi_get_handle(dev)) == NULL)
1276 buf.Length = ACPI_ALLOCATE_BUFFER;
1277 error = AcpiGetObjectInfo(h, &buf);
1278 if (ACPI_FAILURE(error))
1280 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1282 /* If no _STA method, must be present */
1283 if ((devinfo->Valid & ACPI_VALID_STA) == 0)
1286 /* Return true for 'present' and 'functioning' */
1287 if ((devinfo->CurrentStatus & 0x9) == 0x9)
1290 AcpiOsFree(buf.Pointer);
1295 * Returns true if the battery is actually present and inserted.
1298 acpi_BatteryIsPresent(device_t dev)
1300 ACPI_DEVICE_INFO *devinfo;
1309 if ((h = acpi_get_handle(dev)) == NULL)
1312 buf.Length = ACPI_ALLOCATE_BUFFER;
1313 error = AcpiGetObjectInfo(h, &buf);
1314 if (ACPI_FAILURE(error))
1316 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1318 /* If no _STA method, must be present */
1319 if ((devinfo->Valid & ACPI_VALID_STA) == 0)
1322 /* Return true for 'present' and 'functioning' */
1323 if ((devinfo->CurrentStatus & 0x19) == 0x19)
1326 AcpiOsFree(buf.Pointer);
1331 * Match a HID string against a device
1334 acpi_MatchHid(device_t dev, char *hid)
1336 ACPI_DEVICE_INFO *devinfo;
1347 if ((h = acpi_get_handle(dev)) == NULL)
1350 buf.Length = ACPI_ALLOCATE_BUFFER;
1351 error = AcpiGetObjectInfo(h, &buf);
1352 if (ACPI_FAILURE(error))
1354 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1356 if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
1357 strcmp(hid, devinfo->HardwareId.Value) == 0)
1359 else if ((devinfo->Valid & ACPI_VALID_CID) != 0) {
1360 for (i = 0; i < devinfo->CompatibilityId.Count; i++) {
1361 if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) {
1368 AcpiOsFree(buf.Pointer);
1373 * Return the handle of a named object within our scope, ie. that of (parent)
1374 * or one if its parents.
1377 acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1384 /* Walk back up the tree to the root */
1386 status = AcpiGetHandle(parent, path, &r);
1387 if (ACPI_SUCCESS(status)) {
1391 if (status != AE_NOT_FOUND)
1393 if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1394 return (AE_NOT_FOUND);
1399 /* Find the difference between two PM tick counts. */
1401 acpi_TimerDelta(uint32_t end, uint32_t start)
1406 delta = end - start;
1407 else if (AcpiGbl_FADT->TmrValExt == 0)
1408 delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF;
1410 delta = ((0xFFFFFFFF - start) + end + 1);
1415 * Allocate a buffer with a preset data size.
1418 acpi_AllocBuffer(int size)
1422 buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_INTWAIT);
1424 buf->Pointer = (void *)(buf + 1);
1429 acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
1432 ACPI_OBJECT_LIST args;
1436 arg1.Type = ACPI_TYPE_INTEGER;
1437 arg1.Integer.Value = number;
1439 args.Pointer = &arg1;
1441 return (AcpiEvaluateObject(handle, path, &args, NULL));
1445 * Evaluate a path that should return an integer.
1448 acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
1457 handle = ACPI_ROOT_OBJECT;
1460 * Assume that what we've been pointed at is an Integer object, or
1461 * a method that will return an Integer.
1463 buf.Pointer = ¶m;
1464 buf.Length = sizeof(param);
1465 status = AcpiEvaluateObject(handle, path, NULL, &buf);
1466 if (ACPI_SUCCESS(status)) {
1467 if (param.Type == ACPI_TYPE_INTEGER)
1468 *number = param.Integer.Value;
1474 * In some applications, a method that's expected to return an Integer
1475 * may instead return a Buffer (probably to simplify some internal
1476 * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer,
1477 * convert it into an Integer as best we can.
1481 if (status == AE_BUFFER_OVERFLOW) {
1482 if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1483 status = AE_NO_MEMORY;
1485 status = AcpiEvaluateObject(handle, path, NULL, &buf);
1486 if (ACPI_SUCCESS(status))
1487 status = acpi_ConvertBufferToInteger(&buf, number);
1488 AcpiOsFree(buf.Pointer);
1495 acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
1501 p = (ACPI_OBJECT *)bufp->Pointer;
1502 if (p->Type == ACPI_TYPE_INTEGER) {
1503 *number = p->Integer.Value;
1506 if (p->Type != ACPI_TYPE_BUFFER)
1508 if (p->Buffer.Length > sizeof(int))
1509 return (AE_BAD_DATA);
1512 val = p->Buffer.Pointer;
1513 for (i = 0; i < p->Buffer.Length; i++)
1514 *number += val[i] << (i * 8);
1519 * Iterate over the elements of an a package object, calling the supplied
1520 * function for each element.
1522 * XXX possible enhancement might be to abort traversal on error.
1525 acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
1526 void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
1531 if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1532 return (AE_BAD_PARAMETER);
1534 /* Iterate over components */
1536 comp = pkg->Package.Elements;
1537 for (; i < pkg->Package.Count; i++, comp++)
1544 * Find the (index)th resource object in a set.
1547 acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
1552 rp = (ACPI_RESOURCE *)buf->Pointer;
1556 if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
1557 return (AE_BAD_PARAMETER);
1559 /* Check for terminator */
1560 if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
1561 return (AE_NOT_FOUND);
1562 rp = ACPI_NEXT_RESOURCE(rp);
1571 * Append an ACPI_RESOURCE to an ACPI_BUFFER.
1573 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
1574 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible
1575 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of
1578 #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512
1581 acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
1586 /* Initialise the buffer if necessary. */
1587 if (buf->Pointer == NULL) {
1588 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
1589 if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
1590 return (AE_NO_MEMORY);
1591 rp = (ACPI_RESOURCE *)buf->Pointer;
1592 rp->Id = ACPI_RSTYPE_END_TAG;
1599 * Scan the current buffer looking for the terminator.
1600 * This will either find the terminator or hit the end
1601 * of the buffer and return an error.
1603 rp = (ACPI_RESOURCE *)buf->Pointer;
1605 /* Range check, don't go outside the buffer */
1606 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
1607 return (AE_BAD_PARAMETER);
1608 if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
1610 rp = ACPI_NEXT_RESOURCE(rp);
1614 * Check the size of the buffer and expand if required.
1617 * size of existing resources before terminator +
1618 * size of new resource and header +
1619 * size of terminator.
1621 * Note that this loop should really only run once, unless
1622 * for some reason we are stuffing a *really* huge resource.
1624 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
1625 res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
1626 ACPI_RESOURCE_LENGTH) >= buf->Length) {
1627 if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
1628 return (AE_NO_MEMORY);
1629 bcopy(buf->Pointer, newp, buf->Length);
1630 rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1631 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
1632 AcpiOsFree(buf->Pointer);
1633 buf->Pointer = newp;
1634 buf->Length += buf->Length;
1637 /* Insert the new resource. */
1638 bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
1640 /* And add the terminator. */
1641 rp = ACPI_NEXT_RESOURCE(rp);
1642 rp->Id = ACPI_RSTYPE_END_TAG;
1649 * Set interrupt model.
1652 acpi_SetIntrModel(int model)
1654 return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
1657 #define ACPI_MINIMUM_AWAKETIME 5
1660 acpi_sleep_enable(void *arg)
1662 ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1666 * Set the system sleep state
1668 * Currently we support S1-S5 but S4 is only S4BIOS
1671 acpi_SetSleepState(struct acpi_softc *sc, int state)
1673 ACPI_STATUS status = AE_OK;
1677 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1680 /* Avoid reentry if already attempting to suspend. */
1681 if (sc->acpi_sstate != ACPI_STATE_S0)
1682 return_ACPI_STATUS (AE_BAD_PARAMETER);
1684 /* We recently woke up so don't suspend again for a while. */
1685 if (sc->acpi_sleep_disabled)
1686 return_ACPI_STATUS (AE_OK);
1693 status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB);
1694 if (status == AE_NOT_FOUND) {
1695 device_printf(sc->acpi_dev,
1696 "Sleep state S%d not supported by BIOS\n", state);
1698 } else if (ACPI_FAILURE(status)) {
1699 device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n",
1700 AcpiFormatException(status));
1704 sc->acpi_sstate = state;
1705 sc->acpi_sleep_disabled = 1;
1707 /* Inform all devices that we are going to sleep. */
1708 if (DEVICE_SUSPEND(root_bus) != 0) {
1710 * Re-wake the system.
1712 * XXX note that a better two-pass approach with a 'veto' pass
1713 * followed by a "real thing" pass would be better, but the
1714 * current bus interface does not provide for this.
1716 DEVICE_RESUME(root_bus);
1717 return_ACPI_STATUS (AE_ERROR);
1720 status = AcpiEnterSleepStatePrep(state);
1721 if (ACPI_FAILURE(status)) {
1722 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1723 AcpiFormatException(status));
1727 if (sc->acpi_sleep_delay > 0)
1728 DELAY(sc->acpi_sleep_delay * 1000000);
1730 if (state != ACPI_STATE_S1) {
1731 acpi_sleep_machdep(sc, state);
1733 /* AcpiEnterSleepState() may be incomplete, unlock if locked. */
1734 if (AcpiGbl_MutexInfo[ACPI_MTX_HARDWARE].OwnerId !=
1735 ACPI_MUTEX_NOT_ACQUIRED) {
1737 AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
1740 /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1741 if (state == ACPI_STATE_S4)
1744 status = AcpiEnterSleepState((UINT8)state);
1745 if (ACPI_FAILURE(status)) {
1746 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
1747 AcpiFormatException(status));
1751 AcpiLeaveSleepState((UINT8)state);
1752 DEVICE_RESUME(root_bus);
1753 sc->acpi_sstate = ACPI_STATE_S0;
1754 acpi_enable_fixed_events(sc);
1758 * Shut down cleanly and power off. This will call us back through the
1759 * shutdown handlers.
1761 shutdown_nice(RB_POWEROFF);
1765 status = AE_BAD_PARAMETER;
1769 /* Disable a second sleep request for a short period */
1770 if (sc->acpi_sleep_disabled)
1771 timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1773 return_ACPI_STATUS (status);
1777 * Enable/Disable ACPI
1780 acpi_Enable(struct acpi_softc *sc)
1785 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1788 flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
1789 ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
1790 if (!sc->acpi_enabled)
1791 status = AcpiEnableSubsystem(flags);
1795 if (status == AE_OK)
1796 sc->acpi_enabled = 1;
1798 return_ACPI_STATUS (status);
1802 acpi_Disable(struct acpi_softc *sc)
1806 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1809 if (sc->acpi_enabled)
1810 status = AcpiDisable();
1814 if (status == AE_OK)
1815 sc->acpi_enabled = 0;
1817 return_ACPI_STATUS (status);
1821 * ACPI Event Handlers
1824 /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
1827 acpi_system_eventhandler_sleep(void *arg, int state)
1830 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1833 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
1834 acpi_SetSleepState((struct acpi_softc *)arg, state);
1840 acpi_system_eventhandler_wakeup(void *arg, int state)
1843 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1845 /* Well, what to do? :-) */
1854 * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
1857 acpi_event_power_button_sleep(void *context)
1859 struct acpi_softc *sc = (struct acpi_softc *)context;
1861 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1863 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
1865 return_VALUE (ACPI_INTERRUPT_HANDLED);
1869 acpi_event_power_button_wake(void *context)
1871 struct acpi_softc *sc = (struct acpi_softc *)context;
1873 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1875 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
1877 return_VALUE (ACPI_INTERRUPT_HANDLED);
1881 acpi_event_sleep_button_sleep(void *context)
1883 struct acpi_softc *sc = (struct acpi_softc *)context;
1885 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1887 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
1889 return_VALUE (ACPI_INTERRUPT_HANDLED);
1893 acpi_event_sleep_button_wake(void *context)
1895 struct acpi_softc *sc = (struct acpi_softc *)context;
1897 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1899 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
1901 return_VALUE (ACPI_INTERRUPT_HANDLED);
1905 * XXX This is kinda ugly, and should not be here.
1907 struct acpi_staticbuf {
1913 acpi_name(ACPI_HANDLE handle)
1915 static struct acpi_staticbuf buf;
1919 buf.buffer.Length = 512;
1920 buf.buffer.Pointer = &buf.data[0];
1922 if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
1923 return (buf.buffer.Pointer);
1925 return ("(unknown path)");
1929 * Debugging/bug-avoidance. Avoid trying to fetch info on various
1930 * parts of the namespace.
1933 acpi_avoid(ACPI_HANDLE handle)
1935 char *cp, *env, *np;
1938 np = acpi_name(handle);
1941 if ((env = getenv("debug.acpi.avoid")) == NULL)
1944 /* Scan the avoid list checking for a match */
1947 while ((*cp != 0) && isspace(*cp))
1952 while ((cp[len] != 0) && !isspace(cp[len]))
1954 if (!strncmp(cp, np, len)) {
1966 * Debugging/bug-avoidance. Disable ACPI subsystem components.
1969 acpi_disabled(char *subsys)
1974 if ((env = getenv("debug.acpi.disabled")) == NULL)
1976 if (strcmp(env, "all") == 0) {
1981 /* Scan the disable list, checking for a match. */
1984 while (*cp != '\0' && isspace(*cp))
1989 while (cp[len] != '\0' && !isspace(cp[len]))
1991 if (strncmp(cp, subsys, len) == 0) {
2003 * Device wake capability enable/disable.
2006 acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable)
2009 * TBD: All Power Resources referenced by elements 2 through N
2010 * of the _PRW object are put into the ON state.
2013 (void)acpi_SetInteger(h, "_PSW", enable);
2017 acpi_device_enable_wake_event(ACPI_HANDLE h)
2019 struct acpi_softc *sc;
2020 uint32_t gpe_bit, lowest_wake;
2023 ACPI_BUFFER prw_buffer;
2024 ACPI_OBJECT *res, *res2;
2026 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2028 sc = devclass_get_softc(acpi_devclass, 0);
2033 * The _PRW object (7.2.9) is only required for devices that have the
2034 * ability to wake the system from a sleeping state.
2036 prw_buffer.Pointer = NULL;
2037 prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
2038 status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
2039 if (ACPI_FAILURE(status))
2041 res = (ACPI_OBJECT *)prw_buffer.Pointer;
2044 if (!ACPI_PKG_VALID(res, 2))
2048 * Element 1 of the _PRW object:
2049 * The lowest power system sleeping state that can be entered while still
2050 * providing wake functionality. The sleeping state being entered must
2051 * be less than (i.e., higher power) or equal to this value.
2053 if (acpi_PkgInt32(res, 1, &lowest_wake) != 0)
2055 if (sc->acpi_sstate > lowest_wake)
2059 * Element 0 of the _PRW object:
2061 switch (res->Package.Elements[0].Type) {
2062 case ACPI_TYPE_INTEGER:
2064 * If the data type of this package element is numeric, then this
2065 * _PRW package element is the bit index in the GPEx_EN, in the
2066 * GPE blocks described in the FADT, of the enable bit that is
2067 * enabled for the wake event.
2069 gpe_bit = res->Package.Elements[0].Integer.Value;
2070 status = AcpiEnableGpe(NULL, gpe_bit, ACPI_EVENT_WAKE_ENABLE);
2071 if (ACPI_FAILURE(status))
2072 printf("wake enable: AcpiEnableGpe failed for %u\n",
2075 case ACPI_TYPE_PACKAGE:
2077 * If the data type of this package element is a package, then this
2078 * _PRW package element is itself a package containing two
2079 * elements. The first is an object reference to the GPE Block
2080 * device that contains the GPE that will be triggered by the wake
2081 * event. The second element is numeric and it contains the bit
2082 * index in the GPEx_EN, in the GPE Block referenced by the
2083 * first element in the package, of the enable bit that is enabled for
2086 * For example, if this field is a package then it is of the form:
2087 * Package() {\_SB.PCI0.ISA.GPE, 2}
2089 res2 = &res->Package.Elements[0];
2090 if (!ACPI_PKG_VALID(res2, 2))
2092 handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
2093 if (handle == NULL || acpi_PkgInt32(res2, 1, &gpe_bit) != 0)
2095 status = AcpiEnableGpe(handle, gpe_bit, ACPI_EVENT_WAKE_ENABLE);
2096 if (ACPI_FAILURE(status))
2097 printf("wake enable: AcpiEnableGpe (package) failed for %u\n",
2105 if (prw_buffer.Pointer != NULL)
2106 AcpiOsFree(prw_buffer.Pointer);
2110 * Control interface.
2112 * We multiplex ioctls for all participating ACPI devices here. Individual
2113 * drivers wanting to be accessible via /dev/acpi should use the
2114 * register/deregister interface to make their handlers visible.
2116 struct acpi_ioctl_hook
2118 TAILQ_ENTRY(acpi_ioctl_hook) link;
2124 static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks;
2125 static int acpi_ioctl_hooks_initted;
2128 * Register an ioctl handler.
2131 acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
2133 struct acpi_ioctl_hook *hp;
2135 hp = malloc(sizeof(*hp), M_ACPIDEV, M_INTWAIT);
2139 if (acpi_ioctl_hooks_initted == 0) {
2140 TAILQ_INIT(&acpi_ioctl_hooks);
2141 acpi_ioctl_hooks_initted = 1;
2143 TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
2148 * Deregister an ioctl handler.
2151 acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
2153 struct acpi_ioctl_hook *hp;
2155 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
2156 if ((hp->cmd == cmd) && (hp->fn == fn))
2160 TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
2161 free(hp, M_ACPIDEV);
2166 acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td)
2172 acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td)
2178 acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
2180 struct acpi_softc *sc;
2181 struct acpi_ioctl_hook *hp;
2182 int error, xerror, state;
2191 * Scan the list of registered ioctls, looking for handlers.
2193 if (acpi_ioctl_hooks_initted) {
2194 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
2195 if (hp->cmd == cmd) {
2196 xerror = hp->fn(cmd, addr, hp->arg);
2205 * Core ioctls are not permitted for non-writable user.
2206 * Currently, other ioctls just fetch information.
2207 * Not changing system behavior.
2209 if((flag & FWRITE) == 0)
2212 /* Core system ioctls. */
2215 if (ACPI_FAILURE(acpi_Enable(sc)))
2218 case ACPIIO_DISABLE:
2219 if (ACPI_FAILURE(acpi_Disable(sc)))
2222 case ACPIIO_SETSLPSTATE:
2223 if (!sc->acpi_enabled) {
2227 state = *(int *)addr;
2228 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) {
2229 if (ACPI_FAILURE(acpi_SetSleepState(sc, state)))
2247 acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2249 char sleep_state[4];
2252 UINT8 state, TypeA, TypeB;
2255 for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++) {
2256 if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
2257 sprintf(sleep_state, "S%d ", state);
2258 strcat(buf, sleep_state);
2261 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2266 acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2268 char sleep_state[10];
2270 u_int new_state, old_state;
2272 old_state = *(u_int *)oidp->oid_arg1;
2273 if (old_state > ACPI_S_STATES_MAX + 1) {
2274 strcpy(sleep_state, "unknown");
2276 bzero(sleep_state, sizeof(sleep_state));
2277 strncpy(sleep_state, sleep_state_names[old_state],
2278 sizeof(sleep_state_names[old_state]));
2280 error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
2281 if (error == 0 && req->newptr != NULL) {
2282 new_state = ACPI_STATE_S0;
2283 for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) {
2284 if (strncmp(sleep_state, sleep_state_names[new_state],
2285 sizeof(sleep_state)) == 0)
2288 if (new_state <= ACPI_S_STATES_MAX + 1) {
2289 if (new_state != old_state)
2290 *(u_int *)oidp->oid_arg1 = new_state;
2299 /* Inform devctl(4) when we receive a Notify. */
2301 acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
2303 char notify_buf[16];
2304 ACPI_BUFFER handle_buf;
2307 if (subsystem == NULL)
2310 handle_buf.Pointer = NULL;
2311 handle_buf.Length = ACPI_ALLOCATE_BUFFER;
2312 status = AcpiNsHandleToPathname(h, &handle_buf);
2313 if (ACPI_FAILURE(status))
2315 snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
2317 devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
2319 AcpiOsFree(handle_buf.Pointer);
2324 * Support for parsing debug options from the kernel environment.
2326 * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
2327 * by specifying the names of the bits in the debug.acpi.layer and
2328 * debug.acpi.level environment variables. Bits may be unset by
2329 * prefixing the bit name with !.
2337 static struct debugtag dbg_layer[] = {
2338 {"ACPI_UTILITIES", ACPI_UTILITIES},
2339 {"ACPI_HARDWARE", ACPI_HARDWARE},
2340 {"ACPI_EVENTS", ACPI_EVENTS},
2341 {"ACPI_TABLES", ACPI_TABLES},
2342 {"ACPI_NAMESPACE", ACPI_NAMESPACE},
2343 {"ACPI_PARSER", ACPI_PARSER},
2344 {"ACPI_DISPATCHER", ACPI_DISPATCHER},
2345 {"ACPI_EXECUTER", ACPI_EXECUTER},
2346 {"ACPI_RESOURCES", ACPI_RESOURCES},
2347 {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER},
2348 {"ACPI_OS_SERVICES", ACPI_OS_SERVICES},
2349 {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER},
2350 {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS},
2352 {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER},
2353 {"ACPI_BATTERY", ACPI_BATTERY},
2354 {"ACPI_BUS", ACPI_BUS},
2355 {"ACPI_BUTTON", ACPI_BUTTON},
2356 {"ACPI_EC", ACPI_EC},
2357 {"ACPI_FAN", ACPI_FAN},
2358 {"ACPI_POWERRES", ACPI_POWERRES},
2359 {"ACPI_PROCESSOR", ACPI_PROCESSOR},
2360 {"ACPI_THERMAL", ACPI_THERMAL},
2361 {"ACPI_TIMER", ACPI_TIMER},
2362 {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS},
2366 static struct debugtag dbg_level[] = {
2367 {"ACPI_LV_ERROR", ACPI_LV_ERROR},
2368 {"ACPI_LV_WARN", ACPI_LV_WARN},
2369 {"ACPI_LV_INIT", ACPI_LV_INIT},
2370 {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT},
2371 {"ACPI_LV_INFO", ACPI_LV_INFO},
2372 {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS},
2374 /* Trace verbosity level 1 [Standard Trace Level] */
2375 {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES},
2376 {"ACPI_LV_PARSE", ACPI_LV_PARSE},
2377 {"ACPI_LV_LOAD", ACPI_LV_LOAD},
2378 {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH},
2379 {"ACPI_LV_EXEC", ACPI_LV_EXEC},
2380 {"ACPI_LV_NAMES", ACPI_LV_NAMES},
2381 {"ACPI_LV_OPREGION", ACPI_LV_OPREGION},
2382 {"ACPI_LV_BFIELD", ACPI_LV_BFIELD},
2383 {"ACPI_LV_TABLES", ACPI_LV_TABLES},
2384 {"ACPI_LV_VALUES", ACPI_LV_VALUES},
2385 {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS},
2386 {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES},
2387 {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS},
2388 {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE},
2389 {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1},
2391 /* Trace verbosity level 2 [Function tracing and memory allocation] */
2392 {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS},
2393 {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS},
2394 {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS},
2395 {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2},
2396 {"ACPI_LV_ALL", ACPI_LV_ALL},
2398 /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2399 {"ACPI_LV_MUTEX", ACPI_LV_MUTEX},
2400 {"ACPI_LV_THREADS", ACPI_LV_THREADS},
2401 {"ACPI_LV_IO", ACPI_LV_IO},
2402 {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS},
2403 {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3},
2405 /* Exceptionally verbose output -- also used in the global "DebugLevel" */
2406 {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE},
2407 {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO},
2408 {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES},
2409 {"ACPI_LV_EVENTS", ACPI_LV_EVENTS},
2410 {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE},
2415 acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
2427 while (*ep && !isspace(*ep))
2438 for (i = 0; tag[i].name != NULL; i++) {
2439 if (!strncmp(cp, tag[i].name, l)) {
2441 *flag |= tag[i].value;
2443 *flag &= ~tag[i].value;
2451 acpi_set_debugging(void *junk)
2453 char *layer, *level;
2460 layer = getenv("debug.acpi.layer");
2461 level = getenv("debug.acpi.level");
2462 if (layer == NULL && level == NULL)
2465 printf("ACPI set debug");
2466 if (layer != NULL) {
2467 if (strcmp("NONE", layer) != 0)
2468 printf(" layer '%s'", layer);
2469 acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer);
2472 if (level != NULL) {
2473 if (strcmp("NONE", level) != 0)
2474 printf(" level '%s'", level);
2475 acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel);
2480 SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
2484 acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
2487 struct debugtag *tag;
2490 if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
2492 if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
2493 tag = &dbg_layer[0];
2494 dbg = &AcpiDbgLayer;
2496 tag = &dbg_level[0];
2497 dbg = &AcpiDbgLevel;
2500 /* Get old values if this is a get request. */
2502 sbuf_cpy(&sb, "NONE");
2503 } else if (req->newptr == NULL) {
2504 for (; tag->name != NULL; tag++) {
2505 if ((*dbg & tag->value) == tag->value)
2506 sbuf_printf(&sb, "%s ", tag->name);
2512 /* Copy out the old values to the user. */
2513 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
2516 /* If the user is setting a string, parse it. */
2517 if (error == 0 && req->newptr != NULL) {
2519 setenv((char *)oidp->oid_arg1, (char *)req->newptr);
2520 acpi_set_debugging(NULL);
2525 SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
2526 "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
2527 SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
2528 "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
2532 acpi_pm_func(u_long cmd, void *arg, ...)
2534 int state, acpi_state;
2536 struct acpi_softc *sc;
2541 case POWER_CMD_SUSPEND:
2542 sc = (struct acpi_softc *)arg;
2549 state = va_arg(ap, int);
2553 case POWER_SLEEP_STATE_STANDBY:
2554 acpi_state = sc->acpi_standby_sx;
2556 case POWER_SLEEP_STATE_SUSPEND:
2557 acpi_state = sc->acpi_suspend_sx;
2559 case POWER_SLEEP_STATE_HIBERNATE:
2560 acpi_state = ACPI_STATE_S4;
2567 acpi_SetSleepState(sc, acpi_state);
2579 acpi_pm_register(void *arg)
2581 if (!cold || resource_disabled("acpi", 0))
2584 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
2587 SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);