xref: /freebsd/sys/dev/acpica/acpi.c (revision dba55fa26d826040b3061d8a5cf1141cd848ee4a)
115e32d5dSMike Smith /*-
215e32d5dSMike Smith  * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org>
315e32d5dSMike Smith  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4cb9b0d80SMike Smith  * Copyright (c) 2000, 2001 Michael Smith
515e32d5dSMike Smith  * Copyright (c) 2000 BSDi
615e32d5dSMike Smith  * All rights reserved.
715e32d5dSMike Smith  *
815e32d5dSMike Smith  * Redistribution and use in source and binary forms, with or without
915e32d5dSMike Smith  * modification, are permitted provided that the following conditions
1015e32d5dSMike Smith  * are met:
1115e32d5dSMike Smith  * 1. Redistributions of source code must retain the above copyright
1215e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer.
1315e32d5dSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
1415e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer in the
1515e32d5dSMike Smith  *    documentation and/or other materials provided with the distribution.
1615e32d5dSMike Smith  *
1715e32d5dSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1815e32d5dSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1915e32d5dSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2015e32d5dSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2115e32d5dSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2215e32d5dSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2315e32d5dSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2415e32d5dSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2515e32d5dSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2615e32d5dSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2715e32d5dSMike Smith  * SUCH DAMAGE.
2815e32d5dSMike Smith  *
2915e32d5dSMike Smith  *	$FreeBSD$
3015e32d5dSMike Smith  */
3115e32d5dSMike Smith 
3215e32d5dSMike Smith #include "opt_acpi.h"
3315e32d5dSMike Smith #include <sys/param.h>
3415e32d5dSMike Smith #include <sys/kernel.h>
35fb919e4dSMark Murray #include <sys/proc.h>
36a89fcf28STakanori Watanabe #include <sys/fcntl.h>
3715e32d5dSMike Smith #include <sys/malloc.h>
3815e32d5dSMike Smith #include <sys/bus.h>
3915e32d5dSMike Smith #include <sys/conf.h>
4015e32d5dSMike Smith #include <sys/ioccom.h>
4115e32d5dSMike Smith #include <sys/reboot.h>
4215e32d5dSMike Smith #include <sys/sysctl.h>
4315e32d5dSMike Smith #include <sys/ctype.h>
441611ea87SMitsuru IWASAKI #include <sys/linker.h>
45f9390180SMitsuru IWASAKI #include <sys/power.h>
4654f6bca0SNate Lawson #include <sys/sbuf.h>
4715e32d5dSMike Smith 
4815e32d5dSMike Smith #include <machine/clock.h>
4915e32d5dSMike Smith #include <machine/resource.h>
50dc750869SNate Lawson #include <machine/bus.h>
51dc750869SNate Lawson #include <sys/rman.h>
52bc0f2195SMike Smith #include <isa/isavar.h>
53bc0f2195SMike Smith 
5415e32d5dSMike Smith #include "acpi.h"
5515e32d5dSMike Smith #include <dev/acpica/acpivar.h>
5615e32d5dSMike Smith #include <dev/acpica/acpiio.h>
579b937d48SNate Lawson #include <contrib/dev/acpica/acnamesp.h>
5815e32d5dSMike Smith 
5915e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
6015e32d5dSMike Smith 
61be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
62cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
63b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
640ae55423SMike Smith 
6515e32d5dSMike Smith static d_open_t		acpiopen;
6615e32d5dSMike Smith static d_close_t	acpiclose;
6715e32d5dSMike Smith static d_ioctl_t	acpiioctl;
6815e32d5dSMike Smith 
6915e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
70dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
71dc08ffecSPoul-Henning Kamp 	.d_flags =	D_NEEDGIANT,
727ac40f5fSPoul-Henning Kamp 	.d_open =	acpiopen,
737ac40f5fSPoul-Henning Kamp 	.d_close =	acpiclose,
747ac40f5fSPoul-Henning Kamp 	.d_ioctl =	acpiioctl,
757ac40f5fSPoul-Henning Kamp 	.d_name =	"acpi",
7615e32d5dSMike Smith };
7715e32d5dSMike Smith 
781d073b1dSJohn Baldwin static const char* sleep_state_names[] = {
79b8670bedSMitsuru IWASAKI     "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
801d073b1dSJohn Baldwin 
812a4ac806SMike Smith /* this has to be static, as the softc is gone when we need it */
822a4ac806SMike Smith static int acpi_off_state = ACPI_STATE_S5;
832a4ac806SMike Smith 
84fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000
85cb9b0d80SMike Smith struct mtx	acpi_mutex;
86fc0ea94aSJohn Baldwin #endif
87cb9b0d80SMike Smith 
883184cf5aSNate Lawson struct acpi_quirks {
893184cf5aSNate Lawson     char	*OemId;
903184cf5aSNate Lawson     uint32_t	OemRevision;
913184cf5aSNate Lawson     char	*value;
923184cf5aSNate Lawson };
933184cf5aSNate Lawson 
943184cf5aSNate Lawson #define ACPI_OEM_REV_ANY	0
953184cf5aSNate Lawson 
963184cf5aSNate Lawson static struct acpi_quirks acpi_quirks_table[] = {
973184cf5aSNate Lawson #ifdef notyet
983184cf5aSNate Lawson     /* Bad PCI routing table.  Used on some SuperMicro boards. */
993184cf5aSNate Lawson     { "PTLTD ", 0x06040000, "pci_link" },
1003184cf5aSNate Lawson #endif
1013184cf5aSNate Lawson 
1023184cf5aSNate Lawson     { NULL, 0, NULL }
1033184cf5aSNate Lawson };
1043184cf5aSNate Lawson 
1056d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
10615e32d5dSMike Smith static void	acpi_identify(driver_t *driver, device_t parent);
10715e32d5dSMike Smith static int	acpi_probe(device_t dev);
10815e32d5dSMike Smith static int	acpi_attach(device_t dev);
1093184cf5aSNate Lawson static void	acpi_quirks_set(void);
110be2b1797SNate Lawson static device_t	acpi_add_child(device_t bus, int order, const char *name,
111be2b1797SNate Lawson 			int unit);
11215e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
113be2b1797SNate Lawson static int	acpi_read_ivar(device_t dev, device_t child, int index,
114be2b1797SNate Lawson 			uintptr_t *result);
115be2b1797SNate Lawson static int	acpi_write_ivar(device_t dev, device_t child, int index,
116be2b1797SNate Lawson 			uintptr_t value);
117be2b1797SNate Lawson static int	acpi_set_resource(device_t dev, device_t child, int type,
118be2b1797SNate Lawson 			int rid, u_long start, u_long count);
119be2b1797SNate Lawson static int	acpi_get_resource(device_t dev, device_t child, int type,
120be2b1797SNate Lawson 			int rid, u_long *startp, u_long *countp);
121be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child,
122be2b1797SNate Lawson 			int type, int *rid, u_long start, u_long end,
123be2b1797SNate Lawson 			u_long count, u_int flags);
124be2b1797SNate Lawson static int	acpi_release_resource(device_t bus, device_t child, int type,
125be2b1797SNate Lawson 			int rid, struct resource *r);
1261e4925e8SNate Lawson static uint32_t	acpi_isa_get_logicalid(device_t dev);
1271e4925e8SNate Lawson static int	acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
128be2b1797SNate Lawson static int	acpi_isa_pnp_probe(device_t bus, device_t child,
129be2b1797SNate Lawson 			struct isa_pnp_id *ids);
13015e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
131be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
132be2b1797SNate Lawson 			void *context, void **status);
13315e32d5dSMike Smith static void	acpi_shutdown_pre_sync(void *arg, int howto);
13415e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
13513d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
13615e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
13715e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
138d75de536SMitsuru IWASAKI static int	acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1391d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
140f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
141f9390180SMitsuru IWASAKI 
14215e32d5dSMike Smith static device_method_t acpi_methods[] = {
14315e32d5dSMike Smith     /* Device interface */
14415e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
14515e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
14615e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
14756a70eadSNate Lawson     DEVMETHOD(device_detach,		bus_generic_detach),
14815e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
14915e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
15015e32d5dSMike Smith     DEVMETHOD(device_resume,		bus_generic_resume),
15115e32d5dSMike Smith 
15215e32d5dSMike Smith     /* Bus interface */
15315e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
15415e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
15515e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
15615e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
15715e32d5dSMike Smith     DEVMETHOD(bus_set_resource,		acpi_set_resource),
15815e32d5dSMike Smith     DEVMETHOD(bus_get_resource,		acpi_get_resource),
15915e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
16015e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
16115e32d5dSMike Smith     DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
16215e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
16315e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
16415e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
16515e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
16615e32d5dSMike Smith 
167bc0f2195SMike Smith     /* ISA emulation */
168bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
169bc0f2195SMike Smith 
17015e32d5dSMike Smith     {0, 0}
17115e32d5dSMike Smith };
17215e32d5dSMike Smith 
17315e32d5dSMike Smith static driver_t acpi_driver = {
17415e32d5dSMike Smith     "acpi",
17515e32d5dSMike Smith     acpi_methods,
17615e32d5dSMike Smith     sizeof(struct acpi_softc),
17715e32d5dSMike Smith };
17815e32d5dSMike Smith 
1793273b005SMike Smith static devclass_t acpi_devclass;
1806d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
181dde24897SMike Smith MODULE_VERSION(acpi, 100);
18215e32d5dSMike Smith 
1831d7b121cSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging");
1841d7b121cSNate Lawson static char acpi_ca_version[12];
1851d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
1861d7b121cSNate Lawson 	      acpi_ca_version, 0, "Version of Intel ACPI-CA");
18715e32d5dSMike Smith 
18815e32d5dSMike Smith /*
1896d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
1906d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
191be2b1797SNate Lawson  * It also cannot be unloaded, since the entire system bus heirarchy hangs
192be2b1797SNate Lawson  * off it.
1936d63101aSMike Smith  */
1946d63101aSMike Smith static int
1956d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
1966d63101aSMike Smith {
1976d63101aSMike Smith     switch(event) {
1986d63101aSMike Smith     case MOD_LOAD:
19987b45ed5SMitsuru IWASAKI 	if (!cold) {
20087b45ed5SMitsuru IWASAKI 	    printf("The ACPI driver cannot be loaded after boot.\n");
2016d63101aSMike Smith 	    return (EPERM);
20287b45ed5SMitsuru IWASAKI 	}
2036d63101aSMike Smith 	break;
2046d63101aSMike Smith     case MOD_UNLOAD:
205f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
2066d63101aSMike Smith 	    return (EBUSY);
207f9390180SMitsuru IWASAKI 	break;
2086d63101aSMike Smith     default:
2096d63101aSMike Smith 	break;
2106d63101aSMike Smith     }
2116d63101aSMike Smith     return (0);
2126d63101aSMike Smith }
2136d63101aSMike Smith 
2146d63101aSMike Smith /*
215bbc2815cSJohn Baldwin  * Perform early initialization.
21615e32d5dSMike Smith  */
217bbc2815cSJohn Baldwin ACPI_STATUS
218bbc2815cSJohn Baldwin acpi_Startup(void)
21915e32d5dSMike Smith {
220d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
221d786139cSMaxime Henrion     char *debugpoint;
22215e32d5dSMike Smith #endif
223bbc2815cSJohn Baldwin     static int error, started = 0;
22415e32d5dSMike Smith 
2251b8c233dSPeter Pentchev     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2261b8c233dSPeter Pentchev 
227bbc2815cSJohn Baldwin     if (started)
228bbc2815cSJohn Baldwin 	return_VALUE (error);
229bbc2815cSJohn Baldwin     started = 1;
23015e32d5dSMike Smith 
231fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000
232be2b1797SNate Lawson     /* Initialise the ACPI mutex */
2336008862bSJohn Baldwin     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
234fc0ea94aSJohn Baldwin #endif
235cb9b0d80SMike Smith 
236be2b1797SNate Lawson     /* Start up the ACPI CA subsystem. */
237d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
238d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
239d786139cSMaxime Henrion     if (debugpoint) {
240d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "init"))
24115e32d5dSMike Smith 	    acpi_EnterDebugger();
242d786139cSMaxime Henrion 	freeenv(debugpoint);
243d786139cSMaxime Henrion     }
24415e32d5dSMike Smith #endif
245b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
246bfae45aaSMike Smith 	printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
247bbc2815cSJohn Baldwin 	return_VALUE (error);
24815e32d5dSMike Smith     }
249d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
250d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
251d786139cSMaxime Henrion     if (debugpoint) {
252d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "tables"))
25315e32d5dSMike Smith 	    acpi_EnterDebugger();
254d786139cSMaxime Henrion 	freeenv(debugpoint);
255d786139cSMaxime Henrion     }
25615e32d5dSMike Smith #endif
2571611ea87SMitsuru IWASAKI 
258b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiLoadTables())) {
259bfae45aaSMike Smith 	printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
260bbc2815cSJohn Baldwin 	return_VALUE(error);
26115e32d5dSMike Smith     }
2623184cf5aSNate Lawson 
2633184cf5aSNate Lawson     /* Set up any quirks we have for this XSDT. */
2643184cf5aSNate Lawson     acpi_quirks_set();
2654e376d58SNate Lawson     if (acpi_disabled("acpi"))
2664e376d58SNate Lawson 	return_VALUE (AE_ERROR);
2673184cf5aSNate Lawson 
268bbc2815cSJohn Baldwin     return_VALUE (AE_OK);
269bbc2815cSJohn Baldwin }
270bbc2815cSJohn Baldwin 
271bbc2815cSJohn Baldwin /*
272bbc2815cSJohn Baldwin  * Detect ACPI, perform early initialisation
273bbc2815cSJohn Baldwin  */
274bbc2815cSJohn Baldwin static void
275bbc2815cSJohn Baldwin acpi_identify(driver_t *driver, device_t parent)
276bbc2815cSJohn Baldwin {
277bbc2815cSJohn Baldwin     device_t	child;
278bbc2815cSJohn Baldwin 
279bbc2815cSJohn Baldwin     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
280bbc2815cSJohn Baldwin 
281bbc2815cSJohn Baldwin     if (!cold)
282bbc2815cSJohn Baldwin 	return_VOID;
283bbc2815cSJohn Baldwin 
284bbc2815cSJohn Baldwin     /* Check that we haven't been disabled with a hint. */
285bbc2815cSJohn Baldwin     if (resource_disabled("acpi", 0))
286bbc2815cSJohn Baldwin 	return_VOID;
287bbc2815cSJohn Baldwin 
288bbc2815cSJohn Baldwin     /* Make sure we're not being doubly invoked. */
289bbc2815cSJohn Baldwin     if (device_find_child(parent, "acpi", 0) != NULL)
290bbc2815cSJohn Baldwin 	return_VOID;
291bbc2815cSJohn Baldwin 
292bbc2815cSJohn Baldwin     /* Initialize ACPI-CA. */
293bbc2815cSJohn Baldwin     if (ACPI_FAILURE(acpi_Startup()))
294bbc2815cSJohn Baldwin 	return_VOID;
29515e32d5dSMike Smith 
2964e376d58SNate Lawson     snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION);
2974e376d58SNate Lawson 
298be2b1797SNate Lawson     /* Attach the actual ACPI device. */
29915e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
30015e32d5dSMike Smith 	device_printf(parent, "ACPI: could not attach\n");
3010ae55423SMike Smith 	return_VOID;
30215e32d5dSMike Smith     }
30315e32d5dSMike Smith }
30415e32d5dSMike Smith 
30515e32d5dSMike Smith /*
30615e32d5dSMike Smith  * Fetch some descriptive data from ACPI to put in our attach message
30715e32d5dSMike Smith  */
30815e32d5dSMike Smith static int
30915e32d5dSMike Smith acpi_probe(device_t dev)
31015e32d5dSMike Smith {
31115e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
31215e32d5dSMike Smith     char		buf[20];
31315e32d5dSMike Smith     int			error;
3142ccd1cacSNate Lawson     struct sbuf		sb;
3152ccd1cacSNate Lawson     ACPI_STATUS		status;
316fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
31715e32d5dSMike Smith 
318b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
319f9390180SMitsuru IWASAKI 
320f9390180SMitsuru IWASAKI     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
321f9390180SMitsuru IWASAKI 	power_pm_get_type() != POWER_PM_TYPE_ACPI) {
322be2b1797SNate Lawson 
323f9390180SMitsuru IWASAKI 	device_printf(dev, "Other PM system enabled.\n");
324f9390180SMitsuru IWASAKI 	return_VALUE(ENXIO);
325f9390180SMitsuru IWASAKI     }
326f9390180SMitsuru IWASAKI 
327cb9b0d80SMike Smith     ACPI_LOCK;
3280ae55423SMike Smith 
329b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
330be2b1797SNate Lawson 	device_printf(dev, "couldn't get XSDT header: %s\n",
331be2b1797SNate Lawson 		      AcpiFormatException(status));
332cb9b0d80SMike Smith 	error = ENXIO;
333cb9b0d80SMike Smith     } else {
3342ccd1cacSNate Lawson 	sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
3352ccd1cacSNate Lawson 	sbuf_bcat(&sb, th.OemId, 6);
3362ccd1cacSNate Lawson 	sbuf_trim(&sb);
3372ccd1cacSNate Lawson 	sbuf_putc(&sb, ' ');
3382ccd1cacSNate Lawson 	sbuf_bcat(&sb, th.OemTableId, 8);
3392ccd1cacSNate Lawson 	sbuf_trim(&sb);
3402ccd1cacSNate Lawson 	sbuf_finish(&sb);
3412ccd1cacSNate Lawson 	device_set_desc_copy(dev, sbuf_data(&sb));
3422ccd1cacSNate Lawson 	sbuf_delete(&sb);
343cb9b0d80SMike Smith 	error = 0;
344cb9b0d80SMike Smith     }
345cb9b0d80SMike Smith     ACPI_UNLOCK;
346cb9b0d80SMike Smith     return_VALUE(error);
34715e32d5dSMike Smith }
34815e32d5dSMike Smith 
34915e32d5dSMike Smith static int
35015e32d5dSMike Smith acpi_attach(device_t dev)
35115e32d5dSMike Smith {
35215e32d5dSMike Smith     struct acpi_softc	*sc;
353cb9b0d80SMike Smith     ACPI_STATUS		status;
35415e32d5dSMike Smith     int			error;
35532d18aa5SMike Smith     UINT32		flags;
356498d464fSMitsuru IWASAKI     char		*env;
357d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
358d786139cSMaxime Henrion     char		*debugpoint;
35915e32d5dSMike Smith #endif
360fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
36115e32d5dSMike Smith 
362b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
363cb9b0d80SMike Smith     ACPI_LOCK;
36415e32d5dSMike Smith     sc = device_get_softc(dev);
36515e32d5dSMike Smith     bzero(sc, sizeof(*sc));
36615e32d5dSMike Smith     sc->acpi_dev = dev;
36715e32d5dSMike Smith 
368d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
369d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
370d786139cSMaxime Henrion     if (debugpoint) {
371d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "spaces"))
37215e32d5dSMike Smith 	    acpi_EnterDebugger();
373d786139cSMaxime Henrion 	freeenv(debugpoint);
374d786139cSMaxime Henrion     }
37515e32d5dSMike Smith #endif
37615e32d5dSMike Smith 
377be2b1797SNate Lawson     /* Install the default address space handlers. */
378cb9b0d80SMike Smith     error = ENXIO;
379be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
380be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
381be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
382be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
383be2b1797SNate Lawson 		      AcpiFormatException(status));
384cb9b0d80SMike Smith 	goto out;
38515e32d5dSMike Smith     }
386be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
387be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
388be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
389be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemIO handler: %s\n",
390be2b1797SNate Lawson 		      AcpiFormatException(status));
391cb9b0d80SMike Smith 	goto out;
39215e32d5dSMike Smith     }
393be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
394be2b1797SNate Lawson 		ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
395be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
396be2b1797SNate Lawson 	device_printf(dev, "could not initialise PciConfig handler: %s\n",
397be2b1797SNate Lawson 		      AcpiFormatException(status));
398cb9b0d80SMike Smith 	goto out;
39915e32d5dSMike Smith     }
40015e32d5dSMike Smith 
40115e32d5dSMike Smith     /*
40215e32d5dSMike Smith      * Bring ACPI fully online.
40315e32d5dSMike Smith      *
404be2b1797SNate Lawson      * Note that some systems (specifically, those with namespace evaluation
405be2b1797SNate Lawson      * issues that require the avoidance of parts of the namespace) must
406be2b1797SNate Lawson      * avoid running _INI and _STA on everything, as well as dodging the final
407be2b1797SNate Lawson      * object init pass.
40815e32d5dSMike Smith      *
40932d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
41032d18aa5SMike Smith      *
411be2b1797SNate Lawson      * XXX We should arrange for the object init pass after we have attached
412be2b1797SNate Lawson      *     all our child devices, but on many systems it works here.
41315e32d5dSMike Smith      */
414d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
415d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
416d786139cSMaxime Henrion     if (debugpoint) {
417d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "enable"))
41815e32d5dSMike Smith 	    acpi_EnterDebugger();
419d786139cSMaxime Henrion 	freeenv(debugpoint);
420d786139cSMaxime Henrion     }
42115e32d5dSMike Smith #endif
42232d18aa5SMike Smith     flags = 0;
423d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
42432d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
425b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
426be2b1797SNate Lawson 	device_printf(dev, "Could not enable ACPI: %s\n",
427be2b1797SNate Lawson 		      AcpiFormatException(status));
428cb9b0d80SMike Smith 	goto out;
42915e32d5dSMike Smith     }
43015e32d5dSMike Smith 
431f8335e3aSNate Lawson     /*
432f8335e3aSNate Lawson      * Call the ECDT probe function to provide EC functionality before
433f8335e3aSNate Lawson      * the namespace has been evaluated.
434f8335e3aSNate Lawson      */
435f8335e3aSNate Lawson     acpi_ec_ecdt_probe(dev);
436f8335e3aSNate Lawson 
437b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
438be2b1797SNate Lawson 	device_printf(dev, "Could not initialize ACPI objects: %s\n",
439be2b1797SNate Lawson 		      AcpiFormatException(status));
440b69ed3f4SMitsuru IWASAKI 	goto out;
441b69ed3f4SMitsuru IWASAKI     }
442b69ed3f4SMitsuru IWASAKI 
44315e32d5dSMike Smith     /*
4441d073b1dSJohn Baldwin      * Setup our sysctl tree.
4451d073b1dSJohn Baldwin      *
4461d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
4471d073b1dSJohn Baldwin      */
4481d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
4491d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
4501d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
4511d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
4521d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
453d75de536SMitsuru IWASAKI 	OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
454d75de536SMitsuru IWASAKI 	0, 0, acpi_supported_sleep_state_sysctl, "A", "");
455d75de536SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4561d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4571d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4581d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4591d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4601d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4611d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4621d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
4631d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
464f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
465f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
466f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
467f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
468f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
469f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
4702d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
471ff01efb5SMitsuru IWASAKI 	OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
472ff01efb5SMitsuru IWASAKI 	&sc->acpi_sleep_delay, 0, "sleep delay");
473ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4741611ea87SMitsuru IWASAKI 	OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
4751611ea87SMitsuru IWASAKI 	&sc->acpi_s4bios, 0, "S4BIOS mode");
4761611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4772d644607SMitsuru IWASAKI 	OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
4782d644607SMitsuru IWASAKI 	&sc->acpi_verbose, 0, "verbose mode");
479c6a78e98STakanori Watanabe     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
480c6a78e98STakanori Watanabe 	OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW,
481c6a78e98STakanori Watanabe 	&sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff");
482bf0c18ecSNate Lawson 
483bf0c18ecSNate Lawson     /*
484bf0c18ecSNate Lawson      * Default to 5 seconds before sleeping to give some machines time to
485bf0c18ecSNate Lawson      * stabilize.
486bf0c18ecSNate Lawson      */
487bf0c18ecSNate Lawson     sc->acpi_sleep_delay = 5;
488c6a78e98STakanori Watanabe     sc->acpi_disable_on_poweroff = 1;
4892d644607SMitsuru IWASAKI     if (bootverbose)
4902d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
491498d464fSMitsuru IWASAKI     if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) {
492498d464fSMitsuru IWASAKI 	sc->acpi_verbose = 1;
493498d464fSMitsuru IWASAKI 	freeenv(env);
494498d464fSMitsuru IWASAKI     }
4951d073b1dSJohn Baldwin 
4962d610c46SNate Lawson     /* Only enable S4BIOS by default if the FACS says it is available. */
4972d610c46SNate Lawson     if (AcpiGbl_FACS->S4Bios_f != 0)
4982d610c46SNate Lawson 	    sc->acpi_s4bios = 1;
4992d610c46SNate Lawson 
5001d073b1dSJohn Baldwin     /*
50115e32d5dSMike Smith      * Dispatch the default sleep state to devices.
50215e32d5dSMike Smith      * TBD: should be configured from userland policy manager.
50315e32d5dSMike Smith      */
50415e32d5dSMike Smith     sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
50515e32d5dSMike Smith     sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
50615e32d5dSMike Smith     sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
507f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
508f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
50915e32d5dSMike Smith 
51013d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
51115e32d5dSMike Smith 
51215e32d5dSMike Smith     /*
51315e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
51415e32d5dSMike Smith      */
515d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
516d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
517d786139cSMaxime Henrion     if (debugpoint) {
518d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "probe"))
51915e32d5dSMike Smith 	    acpi_EnterDebugger();
520d786139cSMaxime Henrion 	freeenv(debugpoint);
521d786139cSMaxime Henrion     }
52215e32d5dSMike Smith #endif
52315e32d5dSMike Smith 
524be2b1797SNate Lawson     /* Register our shutdown handlers */
525be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc,
526be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
527be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
528be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
52915e32d5dSMike Smith 
53015e32d5dSMike Smith     /*
53115e32d5dSMike Smith      * Register our acpi event handlers.
53215e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
53315e32d5dSMike Smith      */
534be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
535be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
536be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
537be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
53815e32d5dSMike Smith 
539be2b1797SNate Lawson     /* Flag our initial states. */
54015e32d5dSMike Smith     sc->acpi_enabled = 1;
54115e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
542ece50487SMitsuru IWASAKI     sc->acpi_sleep_disabled = 0;
54315e32d5dSMike Smith 
544be2b1797SNate Lawson     /* Create the control device */
545a89fcf28STakanori Watanabe     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
5462a4689e8SRobert Watson 			      "acpi");
54715e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
54815e32d5dSMike Smith 
549d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
550d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
551d786139cSMaxime Henrion     if (debugpoint) {
552be2b1797SNate Lawson 	if (strcmp(debugpoint, "running") == 0)
55315e32d5dSMike Smith 	    acpi_EnterDebugger();
554d786139cSMaxime Henrion 	freeenv(debugpoint);
555d786139cSMaxime Henrion     }
55615e32d5dSMike Smith #endif
557f86214b6SMitsuru IWASAKI 
55891da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS
559be2b1797SNate Lawson     if ((error = acpi_task_thread_init()))
560c573e654SMitsuru IWASAKI 	goto out;
561c573e654SMitsuru IWASAKI #endif
562c573e654SMitsuru IWASAKI 
563be2b1797SNate Lawson     if ((error = acpi_machdep_init(dev)))
564f86214b6SMitsuru IWASAKI 	goto out;
565f86214b6SMitsuru IWASAKI 
566f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
567f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
568f9390180SMitsuru IWASAKI 
569eeb6dba2SJohn Baldwin     if (!acpi_disabled("bus"))
570eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
571eeb6dba2SJohn Baldwin 
572cb9b0d80SMike Smith     error = 0;
573cb9b0d80SMike Smith 
574cb9b0d80SMike Smith  out:
575cb9b0d80SMike Smith     ACPI_UNLOCK;
576cb9b0d80SMike Smith     return_VALUE (error);
57715e32d5dSMike Smith }
57815e32d5dSMike Smith 
5793184cf5aSNate Lawson static void
5803184cf5aSNate Lawson acpi_quirks_set()
5813184cf5aSNate Lawson {
5823184cf5aSNate Lawson     XSDT_DESCRIPTOR *xsdt;
5833184cf5aSNate Lawson     struct acpi_quirks *quirk;
5843184cf5aSNate Lawson     char *env, *tmp;
5853184cf5aSNate Lawson     int len;
5863184cf5aSNate Lawson 
5874e376d58SNate Lawson     /*
5884e376d58SNate Lawson      * If the user loaded a custom table or disabled "quirks", leave
5894e376d58SNate Lawson      * the settings alone.
5904e376d58SNate Lawson      */
5913184cf5aSNate Lawson     len = 0;
5924e376d58SNate Lawson     if ((env = getenv("acpi_dsdt_load")) != NULL) {
5934e376d58SNate Lawson 	/* XXX No strcasecmp but this is good enough. */
5944e376d58SNate Lawson 	if (*env == 'Y' || *env == 'y')
5954e376d58SNate Lawson 	    goto out;
5964e376d58SNate Lawson 	freeenv(env);
5974e376d58SNate Lawson     }
5983184cf5aSNate Lawson     if ((env = getenv("debug.acpi.disabled")) != NULL) {
5994e376d58SNate Lawson 	if (strstr("quirks", env) != NULL)
6003184cf5aSNate Lawson 	    goto out;
6013184cf5aSNate Lawson 	len = strlen(env);
6023184cf5aSNate Lawson     }
6033184cf5aSNate Lawson 
6043184cf5aSNate Lawson     /*
6053184cf5aSNate Lawson      * Search through our quirk table and concatenate the disabled
6063184cf5aSNate Lawson      * values with whatever we find.
6073184cf5aSNate Lawson      */
6083184cf5aSNate Lawson     xsdt = AcpiGbl_XSDT;
6093184cf5aSNate Lawson     for (quirk = acpi_quirks_table; quirk->OemId; quirk++) {
6103184cf5aSNate Lawson 	if (!strncmp(xsdt->OemId, quirk->OemId, strlen(quirk->OemId)) &&
6113184cf5aSNate Lawson 	    (xsdt->OemRevision == quirk->OemRevision ||
6123184cf5aSNate Lawson 	    quirk->OemRevision == ACPI_OEM_REV_ANY)) {
6133184cf5aSNate Lawson 		len += strlen(quirk->value) + 2;
6143184cf5aSNate Lawson 		if ((tmp = malloc(len, M_TEMP, M_NOWAIT)) == NULL)
6153184cf5aSNate Lawson 		    goto out;
6163184cf5aSNate Lawson 		sprintf(tmp, "%s %s", env ? env : "", quirk->value);
6173184cf5aSNate Lawson 		setenv("debug.acpi.disabled", tmp);
6183184cf5aSNate Lawson 		free(tmp, M_TEMP);
6193184cf5aSNate Lawson 		break;
6203184cf5aSNate Lawson 	}
6213184cf5aSNate Lawson     }
6223184cf5aSNate Lawson 
6233184cf5aSNate Lawson out:
6243184cf5aSNate Lawson     if (env)
6253184cf5aSNate Lawson 	freeenv(env);
6263184cf5aSNate Lawson }
6273184cf5aSNate Lawson 
62815e32d5dSMike Smith /*
62915e32d5dSMike Smith  * Handle a new device being added
63015e32d5dSMike Smith  */
63115e32d5dSMike Smith static device_t
63215e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
63315e32d5dSMike Smith {
63415e32d5dSMike Smith     struct acpi_device	*ad;
63515e32d5dSMike Smith     device_t		child;
63615e32d5dSMike Smith 
637be2b1797SNate Lawson     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
63815e32d5dSMike Smith 	return (NULL);
63915e32d5dSMike Smith 
64015e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
64115e32d5dSMike Smith 
64215e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
64315e32d5dSMike Smith     if (child != NULL)
64415e32d5dSMike Smith 	device_set_ivars(child, ad);
64515e32d5dSMike Smith     return (child);
64615e32d5dSMike Smith }
64715e32d5dSMike Smith 
64815e32d5dSMike Smith static int
64915e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
65015e32d5dSMike Smith {
65115e32d5dSMike Smith     struct acpi_device	 *adev = device_get_ivars(child);
65215e32d5dSMike Smith     struct resource_list *rl = &adev->ad_rl;
65315e32d5dSMike Smith     int retval = 0;
65415e32d5dSMike Smith 
65515e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
6569ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
6579ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
6589ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
6599ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
66015e32d5dSMike Smith     retval += bus_print_child_footer(bus, child);
66115e32d5dSMike Smith 
66215e32d5dSMike Smith     return (retval);
66315e32d5dSMike Smith }
66415e32d5dSMike Smith 
66515e32d5dSMike Smith /*
66615e32d5dSMike Smith  * Handle per-device ivars
66715e32d5dSMike Smith  */
66815e32d5dSMike Smith static int
66915e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
67015e32d5dSMike Smith {
67115e32d5dSMike Smith     struct acpi_device	*ad;
67215e32d5dSMike Smith 
67315e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
67415e32d5dSMike Smith 	printf("device has no ivars\n");
67515e32d5dSMike Smith 	return (ENOENT);
67615e32d5dSMike Smith     }
67715e32d5dSMike Smith 
678be2b1797SNate Lawson     /* ACPI and ISA compatibility ivars */
67915e32d5dSMike Smith     switch(index) {
68015e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
68115e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
68215e32d5dSMike Smith 	break;
68315e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
68415e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
68515e32d5dSMike Smith 	break;
68615e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
68715e32d5dSMike Smith 	*(void **)result = ad->ad_private;
68815e32d5dSMike Smith 	break;
68932d18aa5SMike Smith     case ISA_IVAR_VENDORID:
69032d18aa5SMike Smith     case ISA_IVAR_SERIAL:
69132d18aa5SMike Smith     case ISA_IVAR_COMPATID:
69232d18aa5SMike Smith 	*(int *)result = -1;
69332d18aa5SMike Smith 	break;
69432d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
69532d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
69632d18aa5SMike Smith 	break;
69715e32d5dSMike Smith     default:
69815e32d5dSMike Smith 	return (ENOENT);
69915e32d5dSMike Smith     }
700be2b1797SNate Lawson 
70115e32d5dSMike Smith     return (0);
70215e32d5dSMike Smith }
70315e32d5dSMike Smith 
70415e32d5dSMike Smith static int
70515e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
70615e32d5dSMike Smith {
70715e32d5dSMike Smith     struct acpi_device	*ad;
70815e32d5dSMike Smith 
70915e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
71015e32d5dSMike Smith 	printf("device has no ivars\n");
71115e32d5dSMike Smith 	return (ENOENT);
71215e32d5dSMike Smith     }
71315e32d5dSMike Smith 
71415e32d5dSMike Smith     switch(index) {
71515e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
71615e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
71715e32d5dSMike Smith 	break;
71815e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
71915e32d5dSMike Smith 	ad->ad_magic = (int)value;
72015e32d5dSMike Smith 	break;
72115e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
72215e32d5dSMike Smith 	ad->ad_private = (void *)value;
72315e32d5dSMike Smith 	break;
72415e32d5dSMike Smith     default:
7252ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
72615e32d5dSMike Smith 	return (ENOENT);
72715e32d5dSMike Smith     }
728be2b1797SNate Lawson 
72915e32d5dSMike Smith     return (0);
73015e32d5dSMike Smith }
73115e32d5dSMike Smith 
732be2b1797SNate Lawson ACPI_HANDLE
733be2b1797SNate Lawson acpi_get_handle(device_t dev)
734be2b1797SNate Lawson {
735be2b1797SNate Lawson     uintptr_t up;
736be2b1797SNate Lawson     ACPI_HANDLE	h;
737be2b1797SNate Lawson 
738be2b1797SNate Lawson     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, &up))
739be2b1797SNate Lawson 	return(NULL);
740be2b1797SNate Lawson     h = (ACPI_HANDLE)up;
741be2b1797SNate Lawson     return (h);
742be2b1797SNate Lawson }
743be2b1797SNate Lawson 
744be2b1797SNate Lawson int
745be2b1797SNate Lawson acpi_set_handle(device_t dev, ACPI_HANDLE h)
746be2b1797SNate Lawson {
747be2b1797SNate Lawson     uintptr_t up;
748be2b1797SNate Lawson 
749be2b1797SNate Lawson     up = (uintptr_t)h;
750be2b1797SNate Lawson     return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, up));
751be2b1797SNate Lawson }
752be2b1797SNate Lawson 
753be2b1797SNate Lawson int
754be2b1797SNate Lawson acpi_get_magic(device_t dev)
755be2b1797SNate Lawson {
756be2b1797SNate Lawson     uintptr_t up;
757be2b1797SNate Lawson     int	m;
758be2b1797SNate Lawson 
759be2b1797SNate Lawson     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, &up))
760be2b1797SNate Lawson 	return(0);
761be2b1797SNate Lawson     m = (int)up;
762be2b1797SNate Lawson     return (m);
763be2b1797SNate Lawson }
764be2b1797SNate Lawson 
765be2b1797SNate Lawson int
766be2b1797SNate Lawson acpi_set_magic(device_t dev, int m)
767be2b1797SNate Lawson {
768be2b1797SNate Lawson     uintptr_t up;
769be2b1797SNate Lawson 
770be2b1797SNate Lawson     up = (uintptr_t)m;
771be2b1797SNate Lawson     return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, up));
772be2b1797SNate Lawson }
773be2b1797SNate Lawson 
774be2b1797SNate Lawson void *
775be2b1797SNate Lawson acpi_get_private(device_t dev)
776be2b1797SNate Lawson {
777be2b1797SNate Lawson     uintptr_t up;
778be2b1797SNate Lawson     void *p;
779be2b1797SNate Lawson 
780be2b1797SNate Lawson     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, &up))
781be2b1797SNate Lawson 	return (NULL);
782be2b1797SNate Lawson     p = (void *)up;
783be2b1797SNate Lawson     return (p);
784be2b1797SNate Lawson }
785be2b1797SNate Lawson 
786be2b1797SNate Lawson int
787be2b1797SNate Lawson acpi_set_private(device_t dev, void *p)
788be2b1797SNate Lawson {
789be2b1797SNate Lawson     uintptr_t up;
790be2b1797SNate Lawson 
791be2b1797SNate Lawson     up = (uintptr_t)p;
792be2b1797SNate Lawson     return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, up));
793be2b1797SNate Lawson }
794be2b1797SNate Lawson 
795be2b1797SNate Lawson ACPI_OBJECT_TYPE
796be2b1797SNate Lawson acpi_get_type(device_t dev)
797be2b1797SNate Lawson {
798be2b1797SNate Lawson     ACPI_HANDLE		h;
799be2b1797SNate Lawson     ACPI_OBJECT_TYPE	t;
800be2b1797SNate Lawson 
801be2b1797SNate Lawson     if ((h = acpi_get_handle(dev)) == NULL)
802be2b1797SNate Lawson 	return (ACPI_TYPE_NOT_FOUND);
803be2b1797SNate Lawson     if (AcpiGetType(h, &t) != AE_OK)
804be2b1797SNate Lawson 	return (ACPI_TYPE_NOT_FOUND);
805be2b1797SNate Lawson     return (t);
806be2b1797SNate Lawson }
807be2b1797SNate Lawson 
80815e32d5dSMike Smith /*
80915e32d5dSMike Smith  * Handle child resource allocation/removal
81015e32d5dSMike Smith  */
81115e32d5dSMike Smith static int
812be2b1797SNate Lawson acpi_set_resource(device_t dev, device_t child, int type, int rid,
813be2b1797SNate Lawson 		  u_long start, u_long count)
81415e32d5dSMike Smith {
81515e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
81615e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
81715e32d5dSMike Smith 
81815e32d5dSMike Smith     resource_list_add(rl, type, rid, start, start + count -1, count);
81915e32d5dSMike Smith 
82015e32d5dSMike Smith     return(0);
82115e32d5dSMike Smith }
82215e32d5dSMike Smith 
82315e32d5dSMike Smith static int
824be2b1797SNate Lawson acpi_get_resource(device_t dev, device_t child, int type, int rid,
825be2b1797SNate Lawson 		  u_long *startp, u_long *countp)
82615e32d5dSMike Smith {
82715e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
82815e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
82915e32d5dSMike Smith     struct resource_list_entry	*rle;
83015e32d5dSMike Smith 
83115e32d5dSMike Smith     rle = resource_list_find(rl, type, rid);
83215e32d5dSMike Smith     if (!rle)
83315e32d5dSMike Smith 	return(ENOENT);
83415e32d5dSMike Smith 
83515e32d5dSMike Smith     if (startp)
83615e32d5dSMike Smith 	*startp = rle->start;
83715e32d5dSMike Smith     if (countp)
83815e32d5dSMike Smith 	*countp = rle->count;
83915e32d5dSMike Smith 
84015e32d5dSMike Smith     return (0);
84115e32d5dSMike Smith }
84215e32d5dSMike Smith 
84315e32d5dSMike Smith static struct resource *
84415e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
84515e32d5dSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
84615e32d5dSMike Smith {
84715e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
84815e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
84915e32d5dSMike Smith 
850be2b1797SNate Lawson     return (resource_list_alloc(rl, bus, child, type, rid, start, end, count,
851be2b1797SNate Lawson 	    flags));
85215e32d5dSMike Smith }
85315e32d5dSMike Smith 
85415e32d5dSMike Smith static int
85515e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
85615e32d5dSMike Smith {
85715e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
85815e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
85915e32d5dSMike Smith 
86015e32d5dSMike Smith     return (resource_list_release(rl, bus, child, type, rid, r));
86115e32d5dSMike Smith }
86215e32d5dSMike Smith 
863dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */
864dc750869SNate Lawson struct resource *
865dc750869SNate Lawson acpi_bus_alloc_gas(device_t dev, int *rid, ACPI_GENERIC_ADDRESS *gas)
866dc750869SNate Lawson {
867dc750869SNate Lawson     int type;
868dc750869SNate Lawson 
869dc750869SNate Lawson     if (gas == NULL || !ACPI_VALID_ADDRESS(gas->Address) ||
870dc750869SNate Lawson 	gas->RegisterBitWidth < 8)
871dc750869SNate Lawson 	return (NULL);
872dc750869SNate Lawson 
873dc750869SNate Lawson     switch (gas->AddressSpaceId) {
874dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
875dc750869SNate Lawson 	type = SYS_RES_MEMORY;
876dc750869SNate Lawson 	break;
877dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_IO:
878dc750869SNate Lawson 	type = SYS_RES_IOPORT;
879dc750869SNate Lawson 	break;
880dc750869SNate Lawson     default:
881dc750869SNate Lawson 	return (NULL);
882dc750869SNate Lawson     }
883dc750869SNate Lawson 
884dc750869SNate Lawson     bus_set_resource(dev, type, *rid, gas->Address, gas->RegisterBitWidth / 8);
885dc750869SNate Lawson     return (bus_alloc_resource(dev, type, rid, 0, ~0, 1, RF_ACTIVE));
886dc750869SNate Lawson }
887dc750869SNate Lawson 
88815e32d5dSMike Smith /*
889bc0f2195SMike Smith  * Handle ISA-like devices probing for a PnP ID to match.
890bc0f2195SMike Smith  */
891bc0f2195SMike Smith #define PNP_EISAID(s)				\
892bc0f2195SMike Smith 	((((s[0] - '@') & 0x1f) << 2)		\
893bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x18) >> 3)		\
894bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x07) << 13)	\
895bc0f2195SMike Smith 	 | (((s[2] - '@') & 0x1f) << 8)		\
896bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[4]) << 16)		\
897bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[3]) << 20)		\
898bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[6]) << 24)		\
899bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[5]) << 28))
900bc0f2195SMike Smith 
9011e4925e8SNate Lawson static uint32_t
90232d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
903bc0f2195SMike Smith {
9041e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
9051e4925e8SNate Lawson     ACPI_BUFFER		buf;
906bc0f2195SMike Smith     ACPI_HANDLE		h;
907bc0f2195SMike Smith     ACPI_STATUS		error;
908bc0f2195SMike Smith     u_int32_t		pnpid;
909fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
91032d18aa5SMike Smith 
911b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
91232d18aa5SMike Smith 
91332d18aa5SMike Smith     pnpid = 0;
914bd189fe7SNate Lawson     buf.Pointer = NULL;
915bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
916bd189fe7SNate Lawson 
91732d18aa5SMike Smith     ACPI_LOCK;
91832d18aa5SMike Smith 
9196fca9360SNate Lawson     /* Fetch and validate the HID. */
92032d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
921bd189fe7SNate Lawson 	goto out;
9226fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
9236fca9360SNate Lawson     if (ACPI_FAILURE(error))
924bd189fe7SNate Lawson 	goto out;
9251e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
92632d18aa5SMike Smith 
9271e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_HID) != 0)
9281e4925e8SNate Lawson 	pnpid = PNP_EISAID(devinfo->HardwareId.Value);
929be2b1797SNate Lawson 
930bd189fe7SNate Lawson out:
931bd189fe7SNate Lawson     if (buf.Pointer != NULL)
9321e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
93332d18aa5SMike Smith     ACPI_UNLOCK;
93432d18aa5SMike Smith     return_VALUE (pnpid);
93532d18aa5SMike Smith }
93632d18aa5SMike Smith 
9371e4925e8SNate Lawson static int
9381e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
939b2566207STakanori Watanabe {
9401e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
9411e4925e8SNate Lawson     ACPI_BUFFER		buf;
942b2566207STakanori Watanabe     ACPI_HANDLE		h;
943b2566207STakanori Watanabe     ACPI_STATUS		error;
9441e4925e8SNate Lawson     uint32_t		*pnpid;
9451e4925e8SNate Lawson     int			valid, i;
946b2566207STakanori Watanabe     ACPI_LOCK_DECL;
947b2566207STakanori Watanabe 
948b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
949b2566207STakanori Watanabe 
9501e4925e8SNate Lawson     pnpid = cids;
9511e4925e8SNate Lawson     valid = 0;
952bd189fe7SNate Lawson     buf.Pointer = NULL;
953bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
954bd189fe7SNate Lawson 
955b2566207STakanori Watanabe     ACPI_LOCK;
956b2566207STakanori Watanabe 
9571e4925e8SNate Lawson     /* Fetch and validate the CID */
958b2566207STakanori Watanabe     if ((h = acpi_get_handle(dev)) == NULL)
959bd189fe7SNate Lawson 	goto out;
9601e4925e8SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
9611e4925e8SNate Lawson     if (ACPI_FAILURE(error))
962bd189fe7SNate Lawson 	goto out;
9631e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
9641e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_CID) == 0)
965b2566207STakanori Watanabe 	goto out;
966b2566207STakanori Watanabe 
9671e4925e8SNate Lawson     if (devinfo->CompatibilityId.Count < count)
9681e4925e8SNate Lawson 	count = devinfo->CompatibilityId.Count;
9691e4925e8SNate Lawson     for (i = 0; i < count; i++) {
9701e4925e8SNate Lawson 	if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0)
9711e4925e8SNate Lawson 	    continue;
9721e4925e8SNate Lawson 	*pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value);
9731e4925e8SNate Lawson 	valid++;
974b2566207STakanori Watanabe     }
975b2566207STakanori Watanabe 
9761e4925e8SNate Lawson out:
977bd189fe7SNate Lawson     if (buf.Pointer != NULL)
9781e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
9791e4925e8SNate Lawson     ACPI_UNLOCK;
9801e4925e8SNate Lawson     return_VALUE (valid);
9811e4925e8SNate Lawson }
982b2566207STakanori Watanabe 
98332d18aa5SMike Smith static int
98432d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
98532d18aa5SMike Smith {
9861e4925e8SNate Lawson     int			result, cid_count, i;
9871e4925e8SNate Lawson     uint32_t		lid, cids[8];
988bc0f2195SMike Smith 
989b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
990bc0f2195SMike Smith 
991bc0f2195SMike Smith     /*
992bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
993bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
994bc0f2195SMike Smith      * that to happen, so don't ever return it.
995bc0f2195SMike Smith      */
996bc0f2195SMike Smith     result = ENXIO;
997bc0f2195SMike Smith 
998be2b1797SNate Lawson     /* Scan the supplied IDs for a match */
999b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
10001e4925e8SNate Lawson     cid_count = acpi_isa_get_compatid(child, cids, 8);
1001bc0f2195SMike Smith     while (ids && ids->ip_id) {
10021e4925e8SNate Lawson 	if (lid == ids->ip_id) {
1003bc0f2195SMike Smith 	    result = 0;
1004bc0f2195SMike Smith 	    goto out;
1005bc0f2195SMike Smith 	}
10061e4925e8SNate Lawson 	for (i = 0; i < cid_count; i++) {
10071e4925e8SNate Lawson 	    if (cids[i] == ids->ip_id) {
10081e4925e8SNate Lawson 		result = 0;
10091e4925e8SNate Lawson 		goto out;
10101e4925e8SNate Lawson 	    }
10111e4925e8SNate Lawson 	}
1012bc0f2195SMike Smith 	ids++;
1013bc0f2195SMike Smith     }
1014be2b1797SNate Lawson 
1015bc0f2195SMike Smith  out:
1016bc0f2195SMike Smith     return_VALUE (result);
1017bc0f2195SMike Smith }
1018bc0f2195SMike Smith 
1019bc0f2195SMike Smith /*
102015e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
102115e32d5dSMike Smith  *
1022be2b1797SNate Lawson  * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and
1023be2b1797SNate Lawson  * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
102415e32d5dSMike Smith  */
102515e32d5dSMike Smith static void
102615e32d5dSMike Smith acpi_probe_children(device_t bus)
102715e32d5dSMike Smith {
102815e32d5dSMike Smith     ACPI_HANDLE	parent;
1029be2b1797SNate Lawson     ACPI_STATUS	status;
10307d3bcec9SMike Smith     static char	*scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
103115e32d5dSMike Smith     int		i;
103215e32d5dSMike Smith 
1033b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1034cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
10350ae55423SMike Smith 
1036be2b1797SNate Lawson     /* Create any static children by calling device identify methods. */
10374c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
103815e32d5dSMike Smith     bus_generic_probe(bus);
103915e32d5dSMike Smith 
104015e32d5dSMike Smith     /*
104115e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
104215e32d5dSMike Smith      * we find.
104315e32d5dSMike Smith      *
104415e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1045be2b1797SNate Lawson      * we want to create nodes for all devices, not just those that are
1046be2b1797SNate Lawson      * currently present. (This assumes that we don't want to create/remove
1047be2b1797SNate Lawson      * devices as they appear, which might be smarter.)
104815e32d5dSMike Smith      */
10494c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
1050be2b1797SNate Lawson     for (i = 0; scopes[i] != NULL; i++) {
1051be2b1797SNate Lawson 	status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent);
1052be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
1053be2b1797SNate Lawson 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child,
1054be2b1797SNate Lawson 			      bus, NULL);
1055be2b1797SNate Lawson 	}
1056be2b1797SNate Lawson     }
105715e32d5dSMike Smith 
105815e32d5dSMike Smith     /*
105915e32d5dSMike Smith      * Scan all of the child devices we have created and let them probe/attach.
106015e32d5dSMike Smith      */
10614c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
106215e32d5dSMike Smith     bus_generic_attach(bus);
106315e32d5dSMike Smith 
106415e32d5dSMike Smith     /*
106515e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
106615e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
106715e32d5dSMike Smith      */
10684c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
106915e32d5dSMike Smith     bus_generic_attach(bus);
10700ae55423SMike Smith 
1071bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
10720ae55423SMike Smith     return_VOID;
107315e32d5dSMike Smith }
107415e32d5dSMike Smith 
107515e32d5dSMike Smith /*
107615e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
107715e32d5dSMike Smith  * it.
107815e32d5dSMike Smith  */
107915e32d5dSMike Smith static ACPI_STATUS
108015e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
108115e32d5dSMike Smith {
108215e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
108315e32d5dSMike Smith     device_t		child, bus = (device_t)context;
108415e32d5dSMike Smith 
1085b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
10860ae55423SMike Smith 
1087be2b1797SNate Lawson     /* Skip this device if we think we'll have trouble with it. */
10880ae55423SMike Smith     if (acpi_avoid(handle))
10890ae55423SMike Smith 	return_ACPI_STATUS (AE_OK);
10900ae55423SMike Smith 
1091b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
109215e32d5dSMike Smith 	switch(type) {
109315e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
109415e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
109515e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
109615e32d5dSMike Smith 	case ACPI_TYPE_POWER:
10970ae55423SMike Smith 	    if (acpi_disabled("children"))
10980ae55423SMike Smith 		break;
1099be2b1797SNate Lawson 
110015e32d5dSMike Smith 	    /*
110115e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
110215e32d5dSMike Smith 	     * so that the probe/attach passes will run breadth-first.
110315e32d5dSMike Smith 	     */
1104be2b1797SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n",
1105be2b1797SNate Lawson 			     acpi_name(handle)));
110615e32d5dSMike Smith 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
1107bc0f2195SMike Smith 	    if (child == NULL)
1108bc0f2195SMike Smith 		break;
110915e32d5dSMike Smith 	    acpi_set_handle(child, handle);
1110bc0f2195SMike Smith 
1111bc0f2195SMike Smith 	    /*
1112b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
1113b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
1114b519f9d6SMike Smith 	     * the handle, but we don't probe it).
1115b519f9d6SMike Smith 	     */
1116be2b1797SNate Lawson 	    if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
1117b519f9d6SMike Smith 		device_disable(child);
1118b519f9d6SMike Smith 		break;
1119b519f9d6SMike Smith 	    }
1120b519f9d6SMike Smith 
1121b519f9d6SMike Smith 	    /*
1122bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
1123bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
1124bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
1125bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
1126bc0f2195SMike Smith 	     * device not to have any resources.
1127bc0f2195SMike Smith 	     */
1128bc0f2195SMike Smith 	    acpi_parse_resources(child, handle, &acpi_res_parse_set);
1129bc0f2195SMike Smith 
1130be2b1797SNate Lawson 	    /* If we're debugging, probe/attach now rather than later */
1131b53f2771SMike Smith 	    ACPI_DEBUG_EXEC(device_probe_and_attach(child));
11320ae55423SMike Smith 	    break;
113315e32d5dSMike Smith 	}
113415e32d5dSMike Smith     }
1135be2b1797SNate Lawson 
11360ae55423SMike Smith     return_ACPI_STATUS (AE_OK);
113715e32d5dSMike Smith }
113815e32d5dSMike Smith 
113915e32d5dSMike Smith static void
114015e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto)
114115e32d5dSMike Smith {
1142c6a78e98STakanori Watanabe     struct acpi_softc *sc = arg;
1143c6a78e98STakanori Watanabe 
1144cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1145cb9b0d80SMike Smith 
114615e32d5dSMike Smith     /*
11470ae55423SMike Smith      * Disable all ACPI events before soft off, otherwise the system
114815e32d5dSMike Smith      * will be turned on again on some laptops.
114915e32d5dSMike Smith      *
115015e32d5dSMike Smith      * XXX this should probably be restricted to masking some events just
115115e32d5dSMike Smith      *     before powering down, since we may still need ACPI during the
115215e32d5dSMike Smith      *     shutdown process.
115315e32d5dSMike Smith      */
1154c6a78e98STakanori Watanabe     if (sc->acpi_disable_on_poweroff)
1155c6a78e98STakanori Watanabe 	acpi_Disable(sc);
115615e32d5dSMike Smith }
115715e32d5dSMike Smith 
115815e32d5dSMike Smith static void
115915e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
116015e32d5dSMike Smith {
116115e32d5dSMike Smith     ACPI_STATUS	status;
116215e32d5dSMike Smith 
1163cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1164cb9b0d80SMike Smith 
1165be2b1797SNate Lawson     if ((howto & RB_POWEROFF) != 0) {
1166e1a90ae1SNate Lawson 	printf("Powering system off using ACPI\n");
1167be2b1797SNate Lawson 	status = AcpiEnterSleepStatePrep(acpi_off_state);
1168be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1169b9c780d6SMitsuru IWASAKI 	    printf("AcpiEnterSleepStatePrep failed - %s\n",
1170b9c780d6SMitsuru IWASAKI 		   AcpiFormatException(status));
1171b9c780d6SMitsuru IWASAKI 	    return;
1172b9c780d6SMitsuru IWASAKI 	}
117355398047SNate Lawson 	ACPI_DISABLE_IRQS();
1174be2b1797SNate Lawson 	status = AcpiEnterSleepState(acpi_off_state);
1175be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1176bfae45aaSMike Smith 	    printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
117715e32d5dSMike Smith 	} else {
117815e32d5dSMike Smith 	    DELAY(1000000);
117915e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
118015e32d5dSMike Smith 	}
1181494ae560SMitsuru IWASAKI     } else {
1182e1a90ae1SNate Lawson 	printf("Shutting down ACPI\n");
1183494ae560SMitsuru IWASAKI 	AcpiTerminate();
118415e32d5dSMike Smith     }
118515e32d5dSMike Smith }
118615e32d5dSMike Smith 
118713d4f7baSMitsuru IWASAKI static void
118813d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
118913d4f7baSMitsuru IWASAKI {
119013d4f7baSMitsuru IWASAKI     static int	first_time = 1;
119113d4f7baSMitsuru IWASAKI 
1192cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1193cb9b0d80SMike Smith 
119413d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
1195be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
119659ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
119713d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
119833febf93SNate Lawson 				     acpi_event_power_button_sleep, sc);
1199be2b1797SNate Lawson 	if (first_time)
12006c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Power Button (fixed)\n");
120113d4f7baSMitsuru IWASAKI     }
1202be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
120359ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
120413d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
120533febf93SNate Lawson 				     acpi_event_sleep_button_sleep, sc);
1206be2b1797SNate Lawson 	if (first_time)
12076c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
120813d4f7baSMitsuru IWASAKI     }
120913d4f7baSMitsuru IWASAKI 
121013d4f7baSMitsuru IWASAKI     first_time = 0;
121113d4f7baSMitsuru IWASAKI }
121213d4f7baSMitsuru IWASAKI 
121315e32d5dSMike Smith /*
1214c5ba0be4SMike Smith  * Returns true if the device is actually present and should
1215c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
1216c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
1217c5ba0be4SMike Smith  */
1218c5ba0be4SMike Smith BOOLEAN
1219c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
1220c5ba0be4SMike Smith {
12211e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1222c5ba0be4SMike Smith     ACPI_HANDLE		h;
12231e4925e8SNate Lawson     ACPI_BUFFER		buf;
1224c5ba0be4SMike Smith     ACPI_STATUS		error;
12251e4925e8SNate Lawson     int			ret;
1226c5ba0be4SMike Smith 
1227cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1228cb9b0d80SMike Smith 
12291e4925e8SNate Lawson     ret = FALSE;
1230c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1231c5ba0be4SMike Smith 	return (FALSE);
12321e4925e8SNate Lawson     buf.Pointer = NULL;
12331e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
12346fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
12356fca9360SNate Lawson     if (ACPI_FAILURE(error))
1236c5ba0be4SMike Smith 	return (FALSE);
12371e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1238be2b1797SNate Lawson 
1239be2b1797SNate Lawson     /* If no _STA method, must be present */
12401e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
12411e4925e8SNate Lawson 	ret = TRUE;
1242be2b1797SNate Lawson 
1243be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
12441e4925e8SNate Lawson     if ((devinfo->CurrentStatus & 0x9) == 0x9)
12451e4925e8SNate Lawson 	ret = TRUE;
1246be2b1797SNate Lawson 
12471e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
12481e4925e8SNate Lawson     return (ret);
1249c5ba0be4SMike Smith }
1250c5ba0be4SMike Smith 
1251c5ba0be4SMike Smith /*
1252b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
1253b53f2771SMike Smith  */
1254b53f2771SMike Smith BOOLEAN
1255b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
1256b53f2771SMike Smith {
12571e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1258b53f2771SMike Smith     ACPI_HANDLE		h;
12591e4925e8SNate Lawson     ACPI_BUFFER		buf;
1260b53f2771SMike Smith     ACPI_STATUS		error;
12611e4925e8SNate Lawson     int			ret;
1262b53f2771SMike Smith 
1263b53f2771SMike Smith     ACPI_ASSERTLOCK;
1264b53f2771SMike Smith 
12651e4925e8SNate Lawson     ret = FALSE;
1266b53f2771SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1267b53f2771SMike Smith 	return (FALSE);
12681e4925e8SNate Lawson     buf.Pointer = NULL;
12691e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
12706fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
12716fca9360SNate Lawson     if (ACPI_FAILURE(error))
1272b53f2771SMike Smith 	return (FALSE);
12731e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1274be2b1797SNate Lawson 
1275be2b1797SNate Lawson     /* If no _STA method, must be present */
12761e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
12771e4925e8SNate Lawson 	ret = TRUE;
1278be2b1797SNate Lawson 
1279be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
12801e4925e8SNate Lawson     if ((devinfo->CurrentStatus & 0x19) == 0x19)
12811e4925e8SNate Lawson 	ret = TRUE;
1282be2b1797SNate Lawson 
12831e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
12841e4925e8SNate Lawson     return (ret);
1285b53f2771SMike Smith }
1286b53f2771SMike Smith 
1287b53f2771SMike Smith /*
128815e32d5dSMike Smith  * Match a HID string against a device
128915e32d5dSMike Smith  */
129015e32d5dSMike Smith BOOLEAN
129115e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid)
129215e32d5dSMike Smith {
12931e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
129415e32d5dSMike Smith     ACPI_HANDLE		h;
12951e4925e8SNate Lawson     ACPI_BUFFER		buf;
129615e32d5dSMike Smith     ACPI_STATUS		error;
12971e4925e8SNate Lawson     int			ret, i;
129815e32d5dSMike Smith 
1299cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1300cb9b0d80SMike Smith 
13011e4925e8SNate Lawson     ret = FALSE;
13024d332989SMike Smith     if (hid == NULL)
130315e32d5dSMike Smith 	return (FALSE);
130415e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
130515e32d5dSMike Smith 	return (FALSE);
13061e4925e8SNate Lawson     buf.Pointer = NULL;
13071e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
13086fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
13096fca9360SNate Lawson     if (ACPI_FAILURE(error))
131015e32d5dSMike Smith 	return (FALSE);
13111e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1312be2b1797SNate Lawson 
1313c59c9a8eSJohn Baldwin     if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
1314c59c9a8eSJohn Baldwin 	strcmp(hid, devinfo->HardwareId.Value) == 0)
13151e4925e8SNate Lawson 	    ret = TRUE;
1316c59c9a8eSJohn Baldwin     else if ((devinfo->Valid & ACPI_VALID_CID) != 0) {
13171e4925e8SNate Lawson 	for (i = 0; i < devinfo->CompatibilityId.Count; i++) {
13181e4925e8SNate Lawson 	    if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) {
13191e4925e8SNate Lawson 		ret = TRUE;
13201e4925e8SNate Lawson 		break;
13211e4925e8SNate Lawson 	    }
13221e4925e8SNate Lawson 	}
13231e4925e8SNate Lawson     }
1324be2b1797SNate Lawson 
13251e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
13261e4925e8SNate Lawson     return (ret);
132715e32d5dSMike Smith }
132815e32d5dSMike Smith 
132915e32d5dSMike Smith /*
1330c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1331c5ba0be4SMike Smith  * or one if its parents.
1332c5ba0be4SMike Smith  */
1333c5ba0be4SMike Smith ACPI_STATUS
1334c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1335c5ba0be4SMike Smith {
1336c5ba0be4SMike Smith     ACPI_HANDLE		r;
1337c5ba0be4SMike Smith     ACPI_STATUS		status;
1338c5ba0be4SMike Smith 
1339cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1340cb9b0d80SMike Smith 
1341be2b1797SNate Lawson     /* Walk back up the tree to the root */
1342c5ba0be4SMike Smith     for (;;) {
1343be2b1797SNate Lawson 	status = AcpiGetHandle(parent, path, &r);
1344be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
1345c5ba0be4SMike Smith 	    *result = r;
1346c5ba0be4SMike Smith 	    return (AE_OK);
1347c5ba0be4SMike Smith 	}
1348c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1349c5ba0be4SMike Smith 	    return (AE_OK);
1350b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1351c5ba0be4SMike Smith 	    return (AE_NOT_FOUND);
1352c5ba0be4SMike Smith 	parent = r;
1353c5ba0be4SMike Smith     }
1354c5ba0be4SMike Smith }
1355c5ba0be4SMike Smith 
1356c5ba0be4SMike Smith /*
1357c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1358c5ba0be4SMike Smith  */
1359c5ba0be4SMike Smith ACPI_BUFFER *
1360c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1361c5ba0be4SMike Smith {
1362c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1363c5ba0be4SMike Smith 
1364c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1365c5ba0be4SMike Smith 	return (NULL);
1366c5ba0be4SMike Smith     buf->Length = size;
1367c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1368c5ba0be4SMike Smith     return (buf);
1369c5ba0be4SMike Smith }
1370c5ba0be4SMike Smith 
1371c310653eSNate Lawson ACPI_STATUS
1372cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
1373c310653eSNate Lawson {
1374c310653eSNate Lawson     ACPI_OBJECT arg1;
1375c310653eSNate Lawson     ACPI_OBJECT_LIST args;
1376c310653eSNate Lawson 
1377c310653eSNate Lawson     ACPI_ASSERTLOCK;
1378c310653eSNate Lawson 
1379c310653eSNate Lawson     arg1.Type = ACPI_TYPE_INTEGER;
1380c310653eSNate Lawson     arg1.Integer.Value = number;
1381c310653eSNate Lawson     args.Count = 1;
1382c310653eSNate Lawson     args.Pointer = &arg1;
1383c310653eSNate Lawson 
1384c310653eSNate Lawson     return (AcpiEvaluateObject(handle, path, &args, NULL));
1385c310653eSNate Lawson }
1386c310653eSNate Lawson 
1387c5ba0be4SMike Smith /*
1388c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1389c5ba0be4SMike Smith  */
1390c5ba0be4SMike Smith ACPI_STATUS
1391cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
1392c5ba0be4SMike Smith {
1393be2b1797SNate Lawson     ACPI_STATUS	status;
1394c5ba0be4SMike Smith     ACPI_BUFFER	buf;
1395c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
1396c5ba0be4SMike Smith 
1397cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1398cb9b0d80SMike Smith 
1399c5ba0be4SMike Smith     if (handle == NULL)
1400c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
14010c7da7acSJohn Baldwin 
14020c7da7acSJohn Baldwin     /*
14030c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
14040c7da7acSJohn Baldwin      * a method that will return an Integer.
14050c7da7acSJohn Baldwin      */
1406c5ba0be4SMike Smith     buf.Pointer = &param;
1407c5ba0be4SMike Smith     buf.Length = sizeof(param);
1408be2b1797SNate Lawson     status = AcpiEvaluateObject(handle, path, NULL, &buf);
1409be2b1797SNate Lawson     if (ACPI_SUCCESS(status)) {
1410be2b1797SNate Lawson 	if (param.Type == ACPI_TYPE_INTEGER)
1411c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1412be2b1797SNate Lawson 	else
1413be2b1797SNate Lawson 	    status = AE_TYPE;
1414c5ba0be4SMike Smith     }
14150c7da7acSJohn Baldwin 
14160c7da7acSJohn Baldwin     /*
14170c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
14180c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
14190c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
14200c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
14210c7da7acSJohn Baldwin      *
14220c7da7acSJohn Baldwin      * This is a hack.
14230c7da7acSJohn Baldwin      */
1424be2b1797SNate Lawson     if (status == AE_BUFFER_OVERFLOW) {
1425b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1426be2b1797SNate Lawson 	    status = AE_NO_MEMORY;
14270c7da7acSJohn Baldwin 	} else {
1428be2b1797SNate Lawson 	    status = AcpiEvaluateObject(handle, path, NULL, &buf);
1429be2b1797SNate Lawson 	    if (ACPI_SUCCESS(status))
1430be2b1797SNate Lawson 		status = acpi_ConvertBufferToInteger(&buf, number);
14310c7da7acSJohn Baldwin 	    AcpiOsFree(buf.Pointer);
14320c7da7acSJohn Baldwin 	}
1433f97739daSNate Lawson     }
1434be2b1797SNate Lawson     return (status);
1435c5ba0be4SMike Smith }
1436c5ba0be4SMike Smith 
1437c573e654SMitsuru IWASAKI ACPI_STATUS
1438cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
1439c573e654SMitsuru IWASAKI {
1440c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
1441dba55fa2SNate Lawson     UINT8	*val;
1442c573e654SMitsuru IWASAKI     int		i;
1443c573e654SMitsuru IWASAKI 
1444c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
1445c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
1446c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
1447c573e654SMitsuru IWASAKI 	return (AE_OK);
1448c573e654SMitsuru IWASAKI     }
1449c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
1450c573e654SMitsuru IWASAKI 	return (AE_TYPE);
1451c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
1452c573e654SMitsuru IWASAKI 	return (AE_BAD_DATA);
1453be2b1797SNate Lawson 
1454c573e654SMitsuru IWASAKI     *number = 0;
1455dba55fa2SNate Lawson     val = p->Buffer.Pointer;
1456c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
1457dba55fa2SNate Lawson 	*number += val[i] << (i * 8);
1458c573e654SMitsuru IWASAKI     return (AE_OK);
1459c573e654SMitsuru IWASAKI }
1460c573e654SMitsuru IWASAKI 
1461c5ba0be4SMike Smith /*
1462c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1463c5ba0be4SMike Smith  * function for each element.
1464c5ba0be4SMike Smith  *
1465c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1466c5ba0be4SMike Smith  */
1467c5ba0be4SMike Smith ACPI_STATUS
1468be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
1469be2b1797SNate Lawson 	void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
1470c5ba0be4SMike Smith {
1471c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1472c5ba0be4SMike Smith     int		i;
1473c5ba0be4SMike Smith 
1474be2b1797SNate Lawson     if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1475c5ba0be4SMike Smith 	return (AE_BAD_PARAMETER);
1476c5ba0be4SMike Smith 
1477be2b1797SNate Lawson     /* Iterate over components */
1478be2b1797SNate Lawson     i = 0;
1479be2b1797SNate Lawson     comp = pkg->Package.Elements;
1480be2b1797SNate Lawson     for (; i < pkg->Package.Count; i++, comp++)
1481c5ba0be4SMike Smith 	func(comp, arg);
1482c5ba0be4SMike Smith 
1483c5ba0be4SMike Smith     return (AE_OK);
1484c5ba0be4SMike Smith }
1485c5ba0be4SMike Smith 
14866f69255bSMike Smith /*
14876f69255bSMike Smith  * Find the (index)th resource object in a set.
14886f69255bSMike Smith  */
14896f69255bSMike Smith ACPI_STATUS
14906d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
14916f69255bSMike Smith {
14926d63101aSMike Smith     ACPI_RESOURCE	*rp;
14936f69255bSMike Smith     int			i;
14946f69255bSMike Smith 
14956d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
14966f69255bSMike Smith     i = index;
14976d63101aSMike Smith     while (i-- > 0) {
1498be2b1797SNate Lawson 	/* Range check */
14996d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
15006f69255bSMike Smith 	    return (AE_BAD_PARAMETER);
1501be2b1797SNate Lawson 
1502be2b1797SNate Lawson 	/* Check for terminator */
1503be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
15046d63101aSMike Smith 	    return (AE_NOT_FOUND);
1505abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
15066f69255bSMike Smith     }
15076f69255bSMike Smith     if (resp != NULL)
15086d63101aSMike Smith 	*resp = rp;
1509be2b1797SNate Lawson 
15106d63101aSMike Smith     return (AE_OK);
15116d63101aSMike Smith }
15126d63101aSMike Smith 
15136d63101aSMike Smith /*
15146d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
15156d63101aSMike Smith  *
15166d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
15176d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
15186d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
15196d63101aSMike Smith  * resources.
15206d63101aSMike Smith  */
15216d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
15226d63101aSMike Smith 
15236d63101aSMike Smith ACPI_STATUS
15246d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
15256d63101aSMike Smith {
15266d63101aSMike Smith     ACPI_RESOURCE	*rp;
15276d63101aSMike Smith     void		*newp;
15286d63101aSMike Smith 
1529be2b1797SNate Lawson     /* Initialise the buffer if necessary. */
15306d63101aSMike Smith     if (buf->Pointer == NULL) {
15316d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
15326d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
15336d63101aSMike Smith 	    return (AE_NO_MEMORY);
15346d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
15356d63101aSMike Smith 	rp->Id = ACPI_RSTYPE_END_TAG;
15366d63101aSMike Smith 	rp->Length = 0;
15376d63101aSMike Smith     }
15386d63101aSMike Smith     if (res == NULL)
15396d63101aSMike Smith 	return (AE_OK);
15406d63101aSMike Smith 
15416d63101aSMike Smith     /*
15426d63101aSMike Smith      * Scan the current buffer looking for the terminator.
15436d63101aSMike Smith      * This will either find the terminator or hit the end
15446d63101aSMike Smith      * of the buffer and return an error.
15456d63101aSMike Smith      */
15466d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
15476d63101aSMike Smith     for (;;) {
1548be2b1797SNate Lawson 	/* Range check, don't go outside the buffer */
15496d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
15506d63101aSMike Smith 	    return (AE_BAD_PARAMETER);
1551be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
15526d63101aSMike Smith 	    break;
1553abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
15546d63101aSMike Smith     }
15556d63101aSMike Smith 
15566d63101aSMike Smith     /*
15576d63101aSMike Smith      * Check the size of the buffer and expand if required.
15586d63101aSMike Smith      *
15596d63101aSMike Smith      * Required size is:
15606d63101aSMike Smith      *	size of existing resources before terminator +
15616d63101aSMike Smith      *	size of new resource and header +
15626d63101aSMike Smith      * 	size of terminator.
15636d63101aSMike Smith      *
15646d63101aSMike Smith      * Note that this loop should really only run once, unless
15656d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
15666d63101aSMike Smith      */
15676d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
15686d63101aSMike Smith 	    res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
15696d63101aSMike Smith 	    ACPI_RESOURCE_LENGTH) >= buf->Length) {
15706d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
15716d63101aSMike Smith 	    return (AE_NO_MEMORY);
15726d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
1573a692219dSMike Smith 	rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1574a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
15756d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
15766d63101aSMike Smith 	buf->Pointer = newp;
15776d63101aSMike Smith 	buf->Length += buf->Length;
15786d63101aSMike Smith     }
15796d63101aSMike Smith 
1580be2b1797SNate Lawson     /* Insert the new resource. */
15816d63101aSMike Smith     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
15826d63101aSMike Smith 
1583be2b1797SNate Lawson     /* And add the terminator. */
1584abcbc5bcSNate Lawson     rp = ACPI_NEXT_RESOURCE(rp);
15856d63101aSMike Smith     rp->Id = ACPI_RSTYPE_END_TAG;
15866d63101aSMike Smith     rp->Length = 0;
15876d63101aSMike Smith 
15886f69255bSMike Smith     return (AE_OK);
15896f69255bSMike Smith }
1590c5ba0be4SMike Smith 
1591da14ac9fSJohn Baldwin /*
1592da14ac9fSJohn Baldwin  * Set interrupt model.
1593da14ac9fSJohn Baldwin  */
1594da14ac9fSJohn Baldwin ACPI_STATUS
1595da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
1596da14ac9fSJohn Baldwin {
1597da14ac9fSJohn Baldwin 
1598c310653eSNate Lawson     return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
1599da14ac9fSJohn Baldwin }
1600da14ac9fSJohn Baldwin 
1601ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME	5
1602ece50487SMitsuru IWASAKI 
1603ece50487SMitsuru IWASAKI static void
1604ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
1605ece50487SMitsuru IWASAKI {
1606ece50487SMitsuru IWASAKI     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1607ece50487SMitsuru IWASAKI }
1608c30382dfSMitsuru IWASAKI 
160915e32d5dSMike Smith /*
161015e32d5dSMike Smith  * Set the system sleep state
161115e32d5dSMike Smith  *
1612be2b1797SNate Lawson  * Currently we support S1-S5 but S4 is only S4BIOS
161315e32d5dSMike Smith  */
161415e32d5dSMike Smith ACPI_STATUS
161515e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
161615e32d5dSMike Smith {
161715e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
161856d8cb57SMitsuru IWASAKI     UINT8	TypeA;
161956d8cb57SMitsuru IWASAKI     UINT8	TypeB;
162015e32d5dSMike Smith 
1621b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1622cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
16230ae55423SMike Smith 
162498175614SNate Lawson     /* Avoid reentry if already attempting to suspend. */
16256397624dSMitsuru IWASAKI     if (sc->acpi_sstate != ACPI_STATE_S0)
162698175614SNate Lawson 	return_ACPI_STATUS (AE_BAD_PARAMETER);
16276397624dSMitsuru IWASAKI 
162898175614SNate Lawson     /* We recently woke up so don't suspend again for a while. */
1629ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1630ece50487SMitsuru IWASAKI 	return_ACPI_STATUS (AE_OK);
1631ece50487SMitsuru IWASAKI 
163215e32d5dSMike Smith     switch (state) {
163315e32d5dSMike Smith     case ACPI_STATE_S1:
16346161544cSTakanori Watanabe     case ACPI_STATE_S2:
16356161544cSTakanori Watanabe     case ACPI_STATE_S3:
16366161544cSTakanori Watanabe     case ACPI_STATE_S4:
1637be2b1797SNate Lawson 	status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB);
163898175614SNate Lawson 	if (status == AE_NOT_FOUND) {
163998175614SNate Lawson 	    device_printf(sc->acpi_dev,
164098175614SNate Lawson 			  "Sleep state S%d not supported by BIOS\n", state);
164198175614SNate Lawson 	    break;
164298175614SNate Lawson 	} else if (ACPI_FAILURE(status)) {
1643be2b1797SNate Lawson 	    device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n",
1644be2b1797SNate Lawson 			  AcpiFormatException(status));
164556d8cb57SMitsuru IWASAKI 	    break;
164656d8cb57SMitsuru IWASAKI 	}
164756d8cb57SMitsuru IWASAKI 
1648a1fccb47SMitsuru IWASAKI 	sc->acpi_sstate = state;
1649a1fccb47SMitsuru IWASAKI 	sc->acpi_sleep_disabled = 1;
1650a1fccb47SMitsuru IWASAKI 
1651be2b1797SNate Lawson 	/* Inform all devices that we are going to sleep. */
165215e32d5dSMike Smith 	if (DEVICE_SUSPEND(root_bus) != 0) {
165315e32d5dSMike Smith 	    /*
165415e32d5dSMike Smith 	     * Re-wake the system.
165515e32d5dSMike Smith 	     *
165615e32d5dSMike Smith 	     * XXX note that a better two-pass approach with a 'veto' pass
165715e32d5dSMike Smith 	     *     followed by a "real thing" pass would be better, but the
165815e32d5dSMike Smith 	     *     current bus interface does not provide for this.
165915e32d5dSMike Smith 	     */
166015e32d5dSMike Smith 	    DEVICE_RESUME(root_bus);
16610ae55423SMike Smith 	    return_ACPI_STATUS (AE_ERROR);
166215e32d5dSMike Smith 	}
1663b9c780d6SMitsuru IWASAKI 
1664be2b1797SNate Lawson 	status = AcpiEnterSleepStatePrep(state);
1665be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1666b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1667b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
1668b9c780d6SMitsuru IWASAKI 	    break;
1669b9c780d6SMitsuru IWASAKI 	}
1670b9c780d6SMitsuru IWASAKI 
1671be2b1797SNate Lawson 	if (sc->acpi_sleep_delay > 0)
1672ff01efb5SMitsuru IWASAKI 	    DELAY(sc->acpi_sleep_delay * 1000000);
1673ff01efb5SMitsuru IWASAKI 
16746161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
16756161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
16766161544cSTakanori Watanabe 
1677be2b1797SNate Lawson 	    /* AcpiEnterSleepState() may be incomplete, unlock if locked. */
167859ddeb18SNate Lawson 	    if (AcpiGbl_MutexInfo[ACPI_MTX_HARDWARE].OwnerId !=
167959ddeb18SNate Lawson 		ACPI_MUTEX_NOT_ACQUIRED) {
168059ddeb18SNate Lawson 
16816161544cSTakanori Watanabe 		AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
1682a1fccb47SMitsuru IWASAKI 	    }
16836161544cSTakanori Watanabe 
16846161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1685be2b1797SNate Lawson 	    if (state == ACPI_STATE_S4)
1686964679ceSMitsuru IWASAKI 		AcpiEnable();
16876161544cSTakanori Watanabe 	} else {
1688be2b1797SNate Lawson 	    status = AcpiEnterSleepState((UINT8)state);
1689be2b1797SNate Lawson 	    if (ACPI_FAILURE(status)) {
1690be2b1797SNate Lawson 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
1691be2b1797SNate Lawson 			      AcpiFormatException(status));
1692c30382dfSMitsuru IWASAKI 		break;
169315e32d5dSMike Smith 	    }
16946161544cSTakanori Watanabe 	}
1695ff741befSTakanori Watanabe 	AcpiLeaveSleepState((UINT8)state);
169615e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
169715e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
169813d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
169915e32d5dSMike Smith 	break;
170015e32d5dSMike Smith     case ACPI_STATE_S5:
170115e32d5dSMike Smith 	/*
170215e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
170315e32d5dSMike Smith 	 * shutdown handlers.
170415e32d5dSMike Smith 	 */
170515e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
170615e32d5dSMike Smith 	break;
170798175614SNate Lawson     case ACPI_STATE_S0:
170815e32d5dSMike Smith     default:
170915e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
171015e32d5dSMike Smith 	break;
171115e32d5dSMike Smith     }
1712ece50487SMitsuru IWASAKI 
171398175614SNate Lawson     /* Disable a second sleep request for a short period */
1714ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1715ece50487SMitsuru IWASAKI 	timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1716ece50487SMitsuru IWASAKI 
17170ae55423SMike Smith     return_ACPI_STATUS (status);
171815e32d5dSMike Smith }
171915e32d5dSMike Smith 
172015e32d5dSMike Smith /*
172115e32d5dSMike Smith  * Enable/Disable ACPI
172215e32d5dSMike Smith  */
172315e32d5dSMike Smith ACPI_STATUS
172415e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
172515e32d5dSMike Smith {
172615e32d5dSMike Smith     ACPI_STATUS	status;
172715e32d5dSMike Smith     u_int32_t	flags;
172815e32d5dSMike Smith 
1729b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1730cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
17310ae55423SMike Smith 
173215e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
173315e32d5dSMike Smith 	    ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
1734be2b1797SNate Lawson     if (!sc->acpi_enabled)
173515e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
1736be2b1797SNate Lawson     else
173715e32d5dSMike Smith 	status = AE_OK;
1738be2b1797SNate Lawson 
173915e32d5dSMike Smith     if (status == AE_OK)
174015e32d5dSMike Smith 	sc->acpi_enabled = 1;
1741be2b1797SNate Lawson 
17420ae55423SMike Smith     return_ACPI_STATUS (status);
174315e32d5dSMike Smith }
174415e32d5dSMike Smith 
174515e32d5dSMike Smith ACPI_STATUS
174615e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
174715e32d5dSMike Smith {
174815e32d5dSMike Smith     ACPI_STATUS	status;
174915e32d5dSMike Smith 
1750b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1751cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
17520ae55423SMike Smith 
1753be2b1797SNate Lawson     if (sc->acpi_enabled)
175415e32d5dSMike Smith 	status = AcpiDisable();
1755be2b1797SNate Lawson     else
175615e32d5dSMike Smith 	status = AE_OK;
1757be2b1797SNate Lawson 
175815e32d5dSMike Smith     if (status == AE_OK)
175915e32d5dSMike Smith 	sc->acpi_enabled = 0;
1760be2b1797SNate Lawson 
17610ae55423SMike Smith     return_ACPI_STATUS (status);
176215e32d5dSMike Smith }
176315e32d5dSMike Smith 
176415e32d5dSMike Smith /*
176515e32d5dSMike Smith  * ACPI Event Handlers
176615e32d5dSMike Smith  */
176715e32d5dSMike Smith 
176815e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
176915e32d5dSMike Smith 
177015e32d5dSMike Smith static void
177115e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
177215e32d5dSMike Smith {
1773fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1774b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
177515e32d5dSMike Smith 
1776cb9b0d80SMike Smith     ACPI_LOCK;
1777a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
177815e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
1779cb9b0d80SMike Smith     ACPI_UNLOCK;
17800ae55423SMike Smith     return_VOID;
178115e32d5dSMike Smith }
178215e32d5dSMike Smith 
178315e32d5dSMike Smith static void
178415e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
178515e32d5dSMike Smith {
1786fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1787b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
17880ae55423SMike Smith 
178915e32d5dSMike Smith     /* Well, what to do? :-) */
17900ae55423SMike Smith 
1791cb9b0d80SMike Smith     ACPI_LOCK;
1792cb9b0d80SMike Smith     ACPI_UNLOCK;
1793cb9b0d80SMike Smith 
17940ae55423SMike Smith     return_VOID;
179515e32d5dSMike Smith }
179615e32d5dSMike Smith 
179715e32d5dSMike Smith /*
179815e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
179915e32d5dSMike Smith  */
180015e32d5dSMike Smith UINT32
180133febf93SNate Lawson acpi_event_power_button_sleep(void *context)
180215e32d5dSMike Smith {
180315e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
180415e32d5dSMike Smith 
1805b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
18060ae55423SMike Smith 
180715e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
18080ae55423SMike Smith 
1809b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
181015e32d5dSMike Smith }
181115e32d5dSMike Smith 
181215e32d5dSMike Smith UINT32
181333febf93SNate Lawson acpi_event_power_button_wake(void *context)
181415e32d5dSMike Smith {
181515e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
181615e32d5dSMike Smith 
1817b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
18180ae55423SMike Smith 
181915e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
18200ae55423SMike Smith 
1821b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
182215e32d5dSMike Smith }
182315e32d5dSMike Smith 
182415e32d5dSMike Smith UINT32
182533febf93SNate Lawson acpi_event_sleep_button_sleep(void *context)
182615e32d5dSMike Smith {
182715e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
182815e32d5dSMike Smith 
1829b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
18300ae55423SMike Smith 
183115e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
18320ae55423SMike Smith 
1833b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
183415e32d5dSMike Smith }
183515e32d5dSMike Smith 
183615e32d5dSMike Smith UINT32
183733febf93SNate Lawson acpi_event_sleep_button_wake(void *context)
183815e32d5dSMike Smith {
183915e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
184015e32d5dSMike Smith 
1841b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
18420ae55423SMike Smith 
184315e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
18440ae55423SMike Smith 
1845b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
184615e32d5dSMike Smith }
184715e32d5dSMike Smith 
184815e32d5dSMike Smith /*
184915e32d5dSMike Smith  * XXX This is kinda ugly, and should not be here.
185015e32d5dSMike Smith  */
185115e32d5dSMike Smith struct acpi_staticbuf {
185215e32d5dSMike Smith     ACPI_BUFFER	buffer;
185315e32d5dSMike Smith     char	data[512];
185415e32d5dSMike Smith };
185515e32d5dSMike Smith 
185615e32d5dSMike Smith char *
185715e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
185815e32d5dSMike Smith {
185915e32d5dSMike Smith     static struct acpi_staticbuf	buf;
186015e32d5dSMike Smith 
1861cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1862cb9b0d80SMike Smith 
186315e32d5dSMike Smith     buf.buffer.Length = 512;
186415e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
186515e32d5dSMike Smith 
1866b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
186715e32d5dSMike Smith 	return (buf.buffer.Pointer);
1868be2b1797SNate Lawson 
186915e32d5dSMike Smith     return ("(unknown path)");
187015e32d5dSMike Smith }
187115e32d5dSMike Smith 
187215e32d5dSMike Smith /*
187315e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
187415e32d5dSMike Smith  * parts of the namespace.
187515e32d5dSMike Smith  */
187615e32d5dSMike Smith int
187715e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
187815e32d5dSMike Smith {
18793c600cfbSJohn Baldwin     char	*cp, *env, *np;
188015e32d5dSMike Smith     int		len;
188115e32d5dSMike Smith 
188215e32d5dSMike Smith     np = acpi_name(handle);
188315e32d5dSMike Smith     if (*np == '\\')
188415e32d5dSMike Smith 	np++;
18853c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
188615e32d5dSMike Smith 	return (0);
188715e32d5dSMike Smith 
1888be2b1797SNate Lawson     /* Scan the avoid list checking for a match */
18893c600cfbSJohn Baldwin     cp = env;
189015e32d5dSMike Smith     for (;;) {
189115e32d5dSMike Smith 	while ((*cp != 0) && isspace(*cp))
189215e32d5dSMike Smith 	    cp++;
189315e32d5dSMike Smith 	if (*cp == 0)
189415e32d5dSMike Smith 	    break;
189515e32d5dSMike Smith 	len = 0;
189615e32d5dSMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
189715e32d5dSMike Smith 	    len++;
1898d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
18993c600cfbSJohn Baldwin 	    freeenv(env);
19000ae55423SMike Smith 	    return(1);
1901d786139cSMaxime Henrion 	}
19020ae55423SMike Smith 	cp += len;
19030ae55423SMike Smith     }
19043c600cfbSJohn Baldwin     freeenv(env);
1905be2b1797SNate Lawson 
19060ae55423SMike Smith     return (0);
19070ae55423SMike Smith }
19080ae55423SMike Smith 
19090ae55423SMike Smith /*
19100ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
19110ae55423SMike Smith  */
19120ae55423SMike Smith int
19130ae55423SMike Smith acpi_disabled(char *subsys)
19140ae55423SMike Smith {
191578689b15SMaxime Henrion     char	*cp, *env;
19160ae55423SMike Smith     int		len;
19170ae55423SMike Smith 
19183184cf5aSNate Lawson     if ((env = getenv("debug.acpi.disabled")) == NULL)
19190ae55423SMike Smith 	return (0);
19203184cf5aSNate Lawson     if (strcmp(env, "all") == 0) {
192178689b15SMaxime Henrion 	freeenv(env);
19220ae55423SMike Smith 	return (1);
1923d786139cSMaxime Henrion     }
19240ae55423SMike Smith 
19253184cf5aSNate Lawson     /* Scan the disable list, checking for a match. */
192678689b15SMaxime Henrion     cp = env;
19270ae55423SMike Smith     for (;;) {
19283184cf5aSNate Lawson 	while (*cp != '\0' && isspace(*cp))
19290ae55423SMike Smith 	    cp++;
19303184cf5aSNate Lawson 	if (*cp == '\0')
19310ae55423SMike Smith 	    break;
19320ae55423SMike Smith 	len = 0;
19333184cf5aSNate Lawson 	while (cp[len] != '\0' && !isspace(cp[len]))
19340ae55423SMike Smith 	    len++;
19353184cf5aSNate Lawson 	if (strncmp(cp, subsys, len) == 0) {
193678689b15SMaxime Henrion 	    freeenv(env);
193715e32d5dSMike Smith 	    return (1);
1938d786139cSMaxime Henrion 	}
193915e32d5dSMike Smith 	cp += len;
194015e32d5dSMike Smith     }
194178689b15SMaxime Henrion     freeenv(env);
1942be2b1797SNate Lawson 
194315e32d5dSMike Smith     return (0);
194415e32d5dSMike Smith }
194515e32d5dSMike Smith 
194615e32d5dSMike Smith /*
1947a1fccb47SMitsuru IWASAKI  * Device wake capability enable/disable.
1948a1fccb47SMitsuru IWASAKI  */
1949a1fccb47SMitsuru IWASAKI void
1950a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable)
1951a1fccb47SMitsuru IWASAKI {
1952a1fccb47SMitsuru IWASAKI     /*
1953a1fccb47SMitsuru IWASAKI      * TBD: All Power Resources referenced by elements 2 through N
1954a1fccb47SMitsuru IWASAKI      *      of the _PRW object are put into the ON state.
1955a1fccb47SMitsuru IWASAKI      */
1956a1fccb47SMitsuru IWASAKI 
1957c310653eSNate Lawson     (void)acpi_SetInteger(h, "_PSW", enable);
1958a1fccb47SMitsuru IWASAKI }
1959a1fccb47SMitsuru IWASAKI 
1960a1fccb47SMitsuru IWASAKI void
1961a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_event(ACPI_HANDLE h)
1962a1fccb47SMitsuru IWASAKI {
1963a1fccb47SMitsuru IWASAKI     struct acpi_softc		*sc;
1964a1fccb47SMitsuru IWASAKI     ACPI_STATUS			status;
1965a1fccb47SMitsuru IWASAKI     ACPI_BUFFER			prw_buffer;
1966a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			*res;
1967a1fccb47SMitsuru IWASAKI 
1968a1fccb47SMitsuru IWASAKI     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1969a1fccb47SMitsuru IWASAKI 
1970be2b1797SNate Lawson     sc = devclass_get_softc(acpi_devclass, 0);
1971be2b1797SNate Lawson     if (sc == NULL)
1972a1fccb47SMitsuru IWASAKI 	return;
1973a1fccb47SMitsuru IWASAKI 
1974a1fccb47SMitsuru IWASAKI     /*
1975a1fccb47SMitsuru IWASAKI      * _PRW object is only required for devices that have the ability
1976a1fccb47SMitsuru IWASAKI      * to wake the system from a system sleeping state.
1977a1fccb47SMitsuru IWASAKI      */
1978a1fccb47SMitsuru IWASAKI     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
1979a1fccb47SMitsuru IWASAKI     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
1980be2b1797SNate Lawson     if (ACPI_FAILURE(status))
1981a1fccb47SMitsuru IWASAKI 	return;
1982a1fccb47SMitsuru IWASAKI 
1983a1fccb47SMitsuru IWASAKI     res = (ACPI_OBJECT *)prw_buffer.Pointer;
1984be2b1797SNate Lawson     if (res == NULL)
1985a1fccb47SMitsuru IWASAKI 	return;
1986a1fccb47SMitsuru IWASAKI 
1987a1fccb47SMitsuru IWASAKI     if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) {
1988a1fccb47SMitsuru IWASAKI 	goto out;
1989a1fccb47SMitsuru IWASAKI     }
1990a1fccb47SMitsuru IWASAKI 
1991a1fccb47SMitsuru IWASAKI     /*
1992a1fccb47SMitsuru IWASAKI      * The element 1 of the _PRW object:
1993a1fccb47SMitsuru IWASAKI      * The lowest power system sleeping state that can be entered
1994a1fccb47SMitsuru IWASAKI      * while still providing wake functionality.
1995a1fccb47SMitsuru IWASAKI      * The sleeping state being entered must be greater or equal to
1996a1fccb47SMitsuru IWASAKI      * the power state declared in element 1 of the _PRW object.
1997a1fccb47SMitsuru IWASAKI      */
1998be2b1797SNate Lawson     if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER)
1999a1fccb47SMitsuru IWASAKI 	goto out;
2000a1fccb47SMitsuru IWASAKI 
2001be2b1797SNate Lawson     if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value)
2002a1fccb47SMitsuru IWASAKI 	goto out;
2003a1fccb47SMitsuru IWASAKI 
2004a1fccb47SMitsuru IWASAKI     /*
2005a1fccb47SMitsuru IWASAKI      * The element 0 of the _PRW object:
2006a1fccb47SMitsuru IWASAKI      */
2007a1fccb47SMitsuru IWASAKI     switch(res->Package.Elements[0].Type) {
2008a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_INTEGER:
2009a1fccb47SMitsuru IWASAKI 	/*
2010a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is numeric, then this
2011a1fccb47SMitsuru IWASAKI 	 * _PRW package element is the bit index in the GPEx_EN, in the
2012a1fccb47SMitsuru IWASAKI 	 * GPE blocks described in the FADT, of the enable bit that is
2013a1fccb47SMitsuru IWASAKI 	 * enabled for the wake event.
2014a1fccb47SMitsuru IWASAKI 	 */
2015a1fccb47SMitsuru IWASAKI 
20166fca9360SNate Lawson 	status = AcpiEnableGpe(NULL, res->Package.Elements[0].Integer.Value,
20176fca9360SNate Lawson 			       ACPI_EVENT_WAKE_ENABLE);
2018a1fccb47SMitsuru IWASAKI 	if (ACPI_FAILURE(status))
2019a1fccb47SMitsuru IWASAKI 	    printf("%s: EnableEvent Failed\n", __func__);
2020a1fccb47SMitsuru IWASAKI 	break;
2021a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_PACKAGE:
2022a1fccb47SMitsuru IWASAKI 	/*
2023be2b1797SNate Lawson 	 * XXX TBD
2024be2b1797SNate Lawson 	 *
2025a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is a package, then this
2026a1fccb47SMitsuru IWASAKI 	 * _PRW package element is itself a package containing two
2027a1fccb47SMitsuru IWASAKI 	 * elements. The first is an object reference to the GPE Block
2028a1fccb47SMitsuru IWASAKI 	 * device that contains the GPE that will be triggered by the wake
2029a1fccb47SMitsuru IWASAKI 	 * event. The second element is numeric and it contains the bit
2030a1fccb47SMitsuru IWASAKI 	 * index in the GPEx_EN, in the GPE Block referenced by the
2031a1fccb47SMitsuru IWASAKI 	 * first element in the package, of the enable bit that is enabled for
2032a1fccb47SMitsuru IWASAKI 	 * the wake event.
2033a1fccb47SMitsuru IWASAKI 	 * For example, if this field is a package then it is of the form:
2034a1fccb47SMitsuru IWASAKI 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
2035a1fccb47SMitsuru IWASAKI 	 */
2036a1fccb47SMitsuru IWASAKI 	break;
2037a1fccb47SMitsuru IWASAKI     default:
2038a1fccb47SMitsuru IWASAKI 	break;
2039a1fccb47SMitsuru IWASAKI     }
2040a1fccb47SMitsuru IWASAKI 
2041a1fccb47SMitsuru IWASAKI out:
2042a1fccb47SMitsuru IWASAKI     if (prw_buffer.Pointer != NULL)
2043a1fccb47SMitsuru IWASAKI 	AcpiOsFree(prw_buffer.Pointer);
2044a1fccb47SMitsuru IWASAKI }
2045a1fccb47SMitsuru IWASAKI 
2046a1fccb47SMitsuru IWASAKI /*
204715e32d5dSMike Smith  * Control interface.
204815e32d5dSMike Smith  *
20490ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
2050be2b1797SNate Lawson  * drivers wanting to be accessible via /dev/acpi should use the
2051be2b1797SNate Lawson  * register/deregister interface to make their handlers visible.
205215e32d5dSMike Smith  */
20530ae55423SMike Smith struct acpi_ioctl_hook
20540ae55423SMike Smith {
20550ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook) link;
20560ae55423SMike Smith     u_long			 cmd;
2057be2b1797SNate Lawson     acpi_ioctl_fn		 fn;
20580ae55423SMike Smith     void			 *arg;
20590ae55423SMike Smith };
20600ae55423SMike Smith 
20610ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
20620ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
20630ae55423SMike Smith 
20640ae55423SMike Smith /*
20650ae55423SMike Smith  * Register an ioctl handler.
20660ae55423SMike Smith  */
20670ae55423SMike Smith int
2068be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
20690ae55423SMike Smith {
20700ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
20710ae55423SMike Smith 
20720ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
20730ae55423SMike Smith 	return (ENOMEM);
20740ae55423SMike Smith     hp->cmd = cmd;
20750ae55423SMike Smith     hp->fn = fn;
20760ae55423SMike Smith     hp->arg = arg;
20770ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
20780ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
20790ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
20800ae55423SMike Smith     }
20810ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
20820ae55423SMike Smith     return (0);
20830ae55423SMike Smith }
20840ae55423SMike Smith 
20850ae55423SMike Smith /*
20860ae55423SMike Smith  * Deregister an ioctl handler.
20870ae55423SMike Smith  */
20880ae55423SMike Smith void
2089be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
20900ae55423SMike Smith {
20910ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
20920ae55423SMike Smith 
20930ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
20940ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
20950ae55423SMike Smith 	    break;
20960ae55423SMike Smith 
20970ae55423SMike Smith     if (hp != NULL) {
20980ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
20990ae55423SMike Smith 	free(hp, M_ACPIDEV);
21000ae55423SMike Smith     }
21010ae55423SMike Smith }
21020ae55423SMike Smith 
210315e32d5dSMike Smith static int
21046b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td)
210515e32d5dSMike Smith {
210615e32d5dSMike Smith     return (0);
210715e32d5dSMike Smith }
210815e32d5dSMike Smith 
210915e32d5dSMike Smith static int
21106b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td)
211115e32d5dSMike Smith {
211215e32d5dSMike Smith     return (0);
211315e32d5dSMike Smith }
211415e32d5dSMike Smith 
211515e32d5dSMike Smith static int
21166b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
211715e32d5dSMike Smith {
211815e32d5dSMike Smith     struct acpi_softc		*sc;
21190ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
21200ae55423SMike Smith     int				error, xerror, state;
2121fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
212215e32d5dSMike Smith 
2123cb9b0d80SMike Smith     ACPI_LOCK;
2124cb9b0d80SMike Smith 
212515e32d5dSMike Smith     error = state = 0;
212615e32d5dSMike Smith     sc = dev->si_drv1;
212715e32d5dSMike Smith 
21280ae55423SMike Smith     /*
21290ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
21300ae55423SMike Smith      */
21310ae55423SMike Smith     if (acpi_ioctl_hooks_initted) {
21320ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
21330ae55423SMike Smith 	    if (hp->cmd == cmd) {
21340ae55423SMike Smith 		xerror = hp->fn(cmd, addr, hp->arg);
21350ae55423SMike Smith 		if (xerror != 0)
21360ae55423SMike Smith 		    error = xerror;
2137917d44c8SMitsuru IWASAKI 		goto out;
21380ae55423SMike Smith 	    }
21390ae55423SMike Smith 	}
21400ae55423SMike Smith     }
21410ae55423SMike Smith 
21420ae55423SMike Smith     /*
2143a89fcf28STakanori Watanabe      * Core ioctls are not permitted for non-writable user.
2144a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
2145a89fcf28STakanori Watanabe      * Not changing system behavior.
2146a89fcf28STakanori Watanabe      */
2147be2b1797SNate Lawson     if((flag & FWRITE) == 0)
2148be2b1797SNate Lawson 	return (EPERM);
2149a89fcf28STakanori Watanabe 
2150be2b1797SNate Lawson     /* Core system ioctls. */
215115e32d5dSMike Smith     switch (cmd) {
215215e32d5dSMike Smith     case ACPIIO_ENABLE:
21530ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
215415e32d5dSMike Smith 	    error = ENXIO;
215515e32d5dSMike Smith 	break;
215615e32d5dSMike Smith     case ACPIIO_DISABLE:
21570ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
215815e32d5dSMike Smith 	    error = ENXIO;
215915e32d5dSMike Smith 	break;
216015e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
216115e32d5dSMike Smith 	if (!sc->acpi_enabled) {
216215e32d5dSMike Smith 	    error = ENXIO;
216315e32d5dSMike Smith 	    break;
216415e32d5dSMike Smith 	}
216515e32d5dSMike Smith 	state = *(int *)addr;
21662d610c46SNate Lawson 	if (state >= ACPI_STATE_S0  && state <= ACPI_S_STATES_MAX) {
21672d610c46SNate Lawson 	    if (ACPI_FAILURE(acpi_SetSleepState(sc, state)))
216815e32d5dSMike Smith 		error = EINVAL;
21692d610c46SNate Lawson 	} else {
21702d610c46SNate Lawson 	    error = EINVAL;
21712d610c46SNate Lawson 	}
217215e32d5dSMike Smith 	break;
217315e32d5dSMike Smith     default:
21740ae55423SMike Smith 	if (error == 0)
217515e32d5dSMike Smith 	    error = EINVAL;
217615e32d5dSMike Smith 	break;
217715e32d5dSMike Smith     }
2178917d44c8SMitsuru IWASAKI 
2179917d44c8SMitsuru IWASAKI out:
2180cb9b0d80SMike Smith     ACPI_UNLOCK;
218115e32d5dSMike Smith     return (error);
218215e32d5dSMike Smith }
218315e32d5dSMike Smith 
21841d073b1dSJohn Baldwin static int
2185d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2186d75de536SMitsuru IWASAKI {
2187d75de536SMitsuru IWASAKI     char sleep_state[4];
2188d75de536SMitsuru IWASAKI     char buf[16];
2189d75de536SMitsuru IWASAKI     int error;
2190d75de536SMitsuru IWASAKI     UINT8 state, TypeA, TypeB;
2191d75de536SMitsuru IWASAKI 
2192d75de536SMitsuru IWASAKI     buf[0] = '\0';
2193d75de536SMitsuru IWASAKI     for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX+1; state++) {
2194d75de536SMitsuru IWASAKI 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
2195d75de536SMitsuru IWASAKI 	    sprintf(sleep_state, "S%d ", state);
2196d75de536SMitsuru IWASAKI 	    strcat(buf, sleep_state);
2197d75de536SMitsuru IWASAKI 	}
2198d75de536SMitsuru IWASAKI     }
2199d75de536SMitsuru IWASAKI     error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2200d75de536SMitsuru IWASAKI     return (error);
2201d75de536SMitsuru IWASAKI }
2202d75de536SMitsuru IWASAKI 
2203d75de536SMitsuru IWASAKI static int
22041d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
22051d073b1dSJohn Baldwin {
22061d073b1dSJohn Baldwin     char sleep_state[10];
22071d073b1dSJohn Baldwin     int error;
22081d073b1dSJohn Baldwin     u_int new_state, old_state;
22091d073b1dSJohn Baldwin 
22101d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
2211b8670bedSMitsuru IWASAKI     if (old_state > ACPI_S_STATES_MAX+1) {
22121d073b1dSJohn Baldwin 	strcpy(sleep_state, "unknown");
2213cb9b0d80SMike Smith     } else {
2214b8670bedSMitsuru IWASAKI 	bzero(sleep_state, sizeof(sleep_state));
22151d073b1dSJohn Baldwin 	strncpy(sleep_state, sleep_state_names[old_state],
22161d073b1dSJohn Baldwin 		sizeof(sleep_state_names[old_state]));
2217cb9b0d80SMike Smith     }
22181d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
22191d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
2220be2b1797SNate Lawson 	new_state = ACPI_STATE_S0;
2221be2b1797SNate Lawson 	for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) {
22221d073b1dSJohn Baldwin 	    if (strncmp(sleep_state, sleep_state_names[new_state],
22231d073b1dSJohn Baldwin 			sizeof(sleep_state)) == 0)
22241d073b1dSJohn Baldwin 		break;
2225cb9b0d80SMike Smith 	}
2226931a10c9SMitsuru IWASAKI 	if (new_state <= ACPI_S_STATES_MAX + 1) {
2227be2b1797SNate Lawson 	    if (new_state != old_state)
22281d073b1dSJohn Baldwin 		*(u_int *)oidp->oid_arg1 = new_state;
2229cb9b0d80SMike Smith 	} else {
22301d073b1dSJohn Baldwin 	    error = EINVAL;
22311d073b1dSJohn Baldwin 	}
2232cb9b0d80SMike Smith     }
2233be2b1797SNate Lawson 
22341d073b1dSJohn Baldwin     return (error);
22351d073b1dSJohn Baldwin }
22361d073b1dSJohn Baldwin 
22379b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */
22389b937d48SNate Lawson void
22399b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
22409b937d48SNate Lawson {
22419b937d48SNate Lawson     char		notify_buf[16];
22429b937d48SNate Lawson     ACPI_BUFFER		handle_buf;
22439b937d48SNate Lawson     ACPI_STATUS		status;
22449b937d48SNate Lawson 
22459b937d48SNate Lawson     if (subsystem == NULL)
22469b937d48SNate Lawson 	return;
22479b937d48SNate Lawson 
22489b937d48SNate Lawson     handle_buf.Pointer = NULL;
22499b937d48SNate Lawson     handle_buf.Length = ACPI_ALLOCATE_BUFFER;
22509b937d48SNate Lawson     status = AcpiNsHandleToPathname(h, &handle_buf);
22519b937d48SNate Lawson     if (ACPI_FAILURE(status))
22529b937d48SNate Lawson 	return;
22539b937d48SNate Lawson     snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
22549b937d48SNate Lawson     devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
22559b937d48SNate Lawson     AcpiOsFree(handle_buf.Pointer);
22569b937d48SNate Lawson }
22579b937d48SNate Lawson 
225815e32d5dSMike Smith #ifdef ACPI_DEBUG
22590ae55423SMike Smith /*
22600ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
22610ae55423SMike Smith  *
22620ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
22630ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
22640ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
22650ae55423SMike Smith  * prefixing the bit name with !.
22660ae55423SMike Smith  */
226715e32d5dSMike Smith struct debugtag
226815e32d5dSMike Smith {
226915e32d5dSMike Smith     char	*name;
227015e32d5dSMike Smith     UINT32	value;
227115e32d5dSMike Smith };
227215e32d5dSMike Smith 
227315e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
2274c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
2275c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
2276c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
2277c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
2278c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
2279c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
2280c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
2281c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
2282c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
2283d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
22844c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
2285d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
2286297835bcSNate Lawson     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
22874c1cdee6SMike Smith 
2288c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
2289c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
22903184cf5aSNate Lawson     {"ACPI_BUS",		ACPI_BUS},
2291c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
22923184cf5aSNate Lawson     {"ACPI_EC", 		ACPI_EC},
22933184cf5aSNate Lawson     {"ACPI_FAN",		ACPI_FAN},
22943184cf5aSNate Lawson     {"ACPI_POWERRES",		ACPI_POWERRES},
22954c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
2296cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
22973184cf5aSNate Lawson     {"ACPI_TIMER",		ACPI_TIMER},
2298b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
229915e32d5dSMike Smith     {NULL, 0}
230015e32d5dSMike Smith };
230115e32d5dSMike Smith 
230215e32d5dSMike Smith static struct debugtag dbg_level[] = {
23034c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
23049501b603SJohn Baldwin     {"ACPI_LV_WARN",		ACPI_LV_WARN},
23059501b603SJohn Baldwin     {"ACPI_LV_INIT",		ACPI_LV_INIT},
23064c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
23079501b603SJohn Baldwin     {"ACPI_LV_INFO",		ACPI_LV_INFO},
23084c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
2309d62ab2f4SMitsuru IWASAKI 
2310d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
2311297835bcSNate Lawson     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
23124c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
23134c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
2314d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
23154c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
23164c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
23174c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
23184c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
23194c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
23204c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
23214c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
23224c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
23234c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
23244c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
2325d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
2326d62ab2f4SMitsuru IWASAKI 
2327d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
2328d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
2329d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
2330d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
2331d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
23324c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
2333d62ab2f4SMitsuru IWASAKI 
2334d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2335d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
2336d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
2337d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
2338d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
2339d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
2340d62ab2f4SMitsuru IWASAKI 
2341d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
234298479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
234398479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
234498479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
234598479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
234698479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
234715e32d5dSMike Smith     {NULL, 0}
234815e32d5dSMike Smith };
234915e32d5dSMike Smith 
235015e32d5dSMike Smith static void
235115e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
235215e32d5dSMike Smith {
235315e32d5dSMike Smith     char	*ep;
235415e32d5dSMike Smith     int		i, l;
23550ae55423SMike Smith     int		set;
235615e32d5dSMike Smith 
235715e32d5dSMike Smith     while (*cp) {
235815e32d5dSMike Smith 	if (isspace(*cp)) {
235915e32d5dSMike Smith 	    cp++;
236015e32d5dSMike Smith 	    continue;
236115e32d5dSMike Smith 	}
236215e32d5dSMike Smith 	ep = cp;
236315e32d5dSMike Smith 	while (*ep && !isspace(*ep))
236415e32d5dSMike Smith 	    ep++;
23650ae55423SMike Smith 	if (*cp == '!') {
23660ae55423SMike Smith 	    set = 0;
23670ae55423SMike Smith 	    cp++;
23680ae55423SMike Smith 	    if (cp == ep)
23690ae55423SMike Smith 		continue;
23700ae55423SMike Smith 	} else {
23710ae55423SMike Smith 	    set = 1;
23720ae55423SMike Smith 	}
237315e32d5dSMike Smith 	l = ep - cp;
237415e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
237515e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
2376be2b1797SNate Lawson 		if (set)
237715e32d5dSMike Smith 		    *flag |= tag[i].value;
2378be2b1797SNate Lawson 		else
23790ae55423SMike Smith 		    *flag &= ~tag[i].value;
238015e32d5dSMike Smith 		printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
238115e32d5dSMike Smith 	    }
238215e32d5dSMike Smith 	}
238315e32d5dSMike Smith 	cp = ep;
238415e32d5dSMike Smith     }
238515e32d5dSMike Smith }
238615e32d5dSMike Smith 
238715e32d5dSMike Smith static void
23882a4ac806SMike Smith acpi_set_debugging(void *junk)
238915e32d5dSMike Smith {
239094aae282SMike Barcroft     char	*cp;
239115e32d5dSMike Smith 
23921d7b121cSNate Lawson     if (cold) {
23930ae55423SMike Smith 	AcpiDbgLayer = 0;
23940ae55423SMike Smith 	AcpiDbgLevel = 0;
23951d7b121cSNate Lawson     }
23961d7b121cSNate Lawson 
239794aae282SMike Barcroft     if ((cp = getenv("debug.acpi.layer")) != NULL) {
239815e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
239994aae282SMike Barcroft 	freeenv(cp);
240094aae282SMike Barcroft     }
240194aae282SMike Barcroft     if ((cp = getenv("debug.acpi.level")) != NULL) {
240215e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
240394aae282SMike Barcroft 	freeenv(cp);
240494aae282SMike Barcroft     }
24050ae55423SMike Smith 
24061d7b121cSNate Lawson     if (cold) {
24071d7b121cSNate Lawson 	printf("ACPI debug layer 0x%x debug level 0x%x\n",
24081d7b121cSNate Lawson 	       AcpiDbgLayer, AcpiDbgLevel);
24091d7b121cSNate Lawson     }
241015e32d5dSMike Smith }
2411be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
2412be2b1797SNate Lawson 	NULL);
24131d7b121cSNate Lawson 
24141d7b121cSNate Lawson static int
24151d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
24161d7b121cSNate Lawson {
241754f6bca0SNate Lawson     int		 error, *dbg;
24181d7b121cSNate Lawson     struct	 debugtag *tag;
241954f6bca0SNate Lawson     struct	 sbuf sb;
24201d7b121cSNate Lawson 
242154f6bca0SNate Lawson     if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
242254f6bca0SNate Lawson 	return (ENOMEM);
24231d7b121cSNate Lawson     if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
24241d7b121cSNate Lawson 	tag = &dbg_layer[0];
24251d7b121cSNate Lawson 	dbg = &AcpiDbgLayer;
24261d7b121cSNate Lawson     } else {
24271d7b121cSNate Lawson 	tag = &dbg_level[0];
24281d7b121cSNate Lawson 	dbg = &AcpiDbgLevel;
24291d7b121cSNate Lawson     }
24301d7b121cSNate Lawson 
24311d7b121cSNate Lawson     /* Get old values if this is a get request. */
24321d7b121cSNate Lawson     if (*dbg == 0) {
243354f6bca0SNate Lawson 	sbuf_cpy(&sb, "NONE");
24341d7b121cSNate Lawson     } else if (req->newptr == NULL) {
24351d7b121cSNate Lawson 	for (; tag->name != NULL; tag++) {
243654f6bca0SNate Lawson 	    if ((*dbg & tag->value) == tag->value)
243754f6bca0SNate Lawson 		sbuf_printf(&sb, "%s ", tag->name);
24381d7b121cSNate Lawson 	}
24391d7b121cSNate Lawson     }
244054f6bca0SNate Lawson     sbuf_trim(&sb);
244154f6bca0SNate Lawson     sbuf_finish(&sb);
24421d7b121cSNate Lawson 
244354f6bca0SNate Lawson     error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
244454f6bca0SNate Lawson     sbuf_delete(&sb);
24451d7b121cSNate Lawson 
24461d7b121cSNate Lawson     /* If the user is setting a string, parse it. */
24471d7b121cSNate Lawson     if (error == 0 && req->newptr != NULL) {
24481d7b121cSNate Lawson 	*dbg = 0;
24491d7b121cSNate Lawson 	setenv((char *)oidp->oid_arg1, (char *)req->newptr);
24501d7b121cSNate Lawson 	acpi_set_debugging(NULL);
24511d7b121cSNate Lawson     }
24521d7b121cSNate Lawson 
24531d7b121cSNate Lawson     return (error);
24541d7b121cSNate Lawson }
24551d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
24561d7b121cSNate Lawson 	    "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
24571d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
24581d7b121cSNate Lawson 	    "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
245915e32d5dSMike Smith #endif
2460f9390180SMitsuru IWASAKI 
2461f9390180SMitsuru IWASAKI static int
2462f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
2463f9390180SMitsuru IWASAKI {
2464f9390180SMitsuru IWASAKI 	int	state, acpi_state;
2465f9390180SMitsuru IWASAKI 	int	error;
2466f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
2467f9390180SMitsuru IWASAKI 	va_list	ap;
2468f9390180SMitsuru IWASAKI 
2469f9390180SMitsuru IWASAKI 	error = 0;
2470f9390180SMitsuru IWASAKI 	switch (cmd) {
2471f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
2472f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
2473f9390180SMitsuru IWASAKI 		if (sc == NULL) {
2474f9390180SMitsuru IWASAKI 			error = EINVAL;
2475f9390180SMitsuru IWASAKI 			goto out;
2476f9390180SMitsuru IWASAKI 		}
2477f9390180SMitsuru IWASAKI 
2478f9390180SMitsuru IWASAKI 		va_start(ap, arg);
2479f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
2480f9390180SMitsuru IWASAKI 		va_end(ap);
2481f9390180SMitsuru IWASAKI 
2482f9390180SMitsuru IWASAKI 		switch (state) {
2483f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
2484f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
2485f9390180SMitsuru IWASAKI 			break;
2486f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
2487f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
2488f9390180SMitsuru IWASAKI 			break;
2489f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
2490f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
2491f9390180SMitsuru IWASAKI 			break;
2492f9390180SMitsuru IWASAKI 		default:
2493f9390180SMitsuru IWASAKI 			error = EINVAL;
2494f9390180SMitsuru IWASAKI 			goto out;
2495f9390180SMitsuru IWASAKI 		}
2496f9390180SMitsuru IWASAKI 
2497f9390180SMitsuru IWASAKI 		acpi_SetSleepState(sc, acpi_state);
2498f9390180SMitsuru IWASAKI 		break;
2499f9390180SMitsuru IWASAKI 	default:
2500f9390180SMitsuru IWASAKI 		error = EINVAL;
2501f9390180SMitsuru IWASAKI 		goto out;
2502f9390180SMitsuru IWASAKI 	}
2503f9390180SMitsuru IWASAKI 
2504f9390180SMitsuru IWASAKI out:
2505f9390180SMitsuru IWASAKI 	return (error);
2506f9390180SMitsuru IWASAKI }
2507f9390180SMitsuru IWASAKI 
2508f9390180SMitsuru IWASAKI static void
2509f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
2510f9390180SMitsuru IWASAKI {
2511be2b1797SNate Lawson     if (!cold || resource_disabled("acpi", 0))
25126c407052SMitsuru IWASAKI 	return;
2513f9390180SMitsuru IWASAKI 
2514f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
2515f9390180SMitsuru IWASAKI }
2516f9390180SMitsuru IWASAKI 
2517f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
2518