xref: /freebsd/sys/dev/acpica/acpi.c (revision eeb3a05f9a9c93cddef556c33ed91b037c95eecd)
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>
47eeb3a05fSNate Lawson #include <sys/smp.h>
4815e32d5dSMike Smith 
4915e32d5dSMike Smith #include <machine/clock.h>
5015e32d5dSMike Smith #include <machine/resource.h>
51dc750869SNate Lawson #include <machine/bus.h>
52dc750869SNate Lawson #include <sys/rman.h>
53bc0f2195SMike Smith #include <isa/isavar.h>
54bc0f2195SMike Smith 
5515e32d5dSMike Smith #include "acpi.h"
5615e32d5dSMike Smith #include <dev/acpica/acpivar.h>
5715e32d5dSMike Smith #include <dev/acpica/acpiio.h>
589b937d48SNate Lawson #include <contrib/dev/acpica/acnamesp.h>
5915e32d5dSMike Smith 
6015e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
6115e32d5dSMike Smith 
62be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
63cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
64b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
650ae55423SMike Smith 
6615e32d5dSMike Smith static d_open_t		acpiopen;
6715e32d5dSMike Smith static d_close_t	acpiclose;
6815e32d5dSMike Smith static d_ioctl_t	acpiioctl;
6915e32d5dSMike Smith 
7015e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
71dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
72dc08ffecSPoul-Henning Kamp 	.d_flags =	D_NEEDGIANT,
737ac40f5fSPoul-Henning Kamp 	.d_open =	acpiopen,
747ac40f5fSPoul-Henning Kamp 	.d_close =	acpiclose,
757ac40f5fSPoul-Henning Kamp 	.d_ioctl =	acpiioctl,
767ac40f5fSPoul-Henning Kamp 	.d_name =	"acpi",
7715e32d5dSMike Smith };
7815e32d5dSMike Smith 
791d073b1dSJohn Baldwin static const char* sleep_state_names[] = {
80b8670bedSMitsuru IWASAKI     "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
811d073b1dSJohn Baldwin 
822a4ac806SMike Smith /* this has to be static, as the softc is gone when we need it */
832a4ac806SMike Smith static int acpi_off_state = ACPI_STATE_S5;
842a4ac806SMike Smith 
85fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000
86cb9b0d80SMike Smith struct mtx	acpi_mutex;
87fc0ea94aSJohn Baldwin #endif
88cb9b0d80SMike Smith 
893184cf5aSNate Lawson struct acpi_quirks {
903184cf5aSNate Lawson     char	*OemId;
913184cf5aSNate Lawson     uint32_t	OemRevision;
923184cf5aSNate Lawson     char	*value;
933184cf5aSNate Lawson };
943184cf5aSNate Lawson 
953184cf5aSNate Lawson #define ACPI_OEM_REV_ANY	0
963184cf5aSNate Lawson 
973184cf5aSNate Lawson static struct acpi_quirks acpi_quirks_table[] = {
983184cf5aSNate Lawson #ifdef notyet
993184cf5aSNate Lawson     /* Bad PCI routing table.  Used on some SuperMicro boards. */
1003184cf5aSNate Lawson     { "PTLTD ", 0x06040000, "pci_link" },
1013184cf5aSNate Lawson #endif
1023184cf5aSNate Lawson 
1033184cf5aSNate Lawson     { NULL, 0, NULL }
1043184cf5aSNate Lawson };
1053184cf5aSNate Lawson 
1066d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
10715e32d5dSMike Smith static void	acpi_identify(driver_t *driver, device_t parent);
10815e32d5dSMike Smith static int	acpi_probe(device_t dev);
10915e32d5dSMike Smith static int	acpi_attach(device_t dev);
1103184cf5aSNate Lawson static void	acpi_quirks_set(void);
111be2b1797SNate Lawson static device_t	acpi_add_child(device_t bus, int order, const char *name,
112be2b1797SNate Lawson 			int unit);
11315e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
114be2b1797SNate Lawson static int	acpi_read_ivar(device_t dev, device_t child, int index,
115be2b1797SNate Lawson 			uintptr_t *result);
116be2b1797SNate Lawson static int	acpi_write_ivar(device_t dev, device_t child, int index,
117be2b1797SNate Lawson 			uintptr_t value);
118be2b1797SNate Lawson static int	acpi_set_resource(device_t dev, device_t child, int type,
119be2b1797SNate Lawson 			int rid, u_long start, u_long count);
120be2b1797SNate Lawson static int	acpi_get_resource(device_t dev, device_t child, int type,
121be2b1797SNate Lawson 			int rid, u_long *startp, u_long *countp);
122be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child,
123be2b1797SNate Lawson 			int type, int *rid, u_long start, u_long end,
124be2b1797SNate Lawson 			u_long count, u_int flags);
125be2b1797SNate Lawson static int	acpi_release_resource(device_t bus, device_t child, int type,
126be2b1797SNate Lawson 			int rid, struct resource *r);
1271e4925e8SNate Lawson static uint32_t	acpi_isa_get_logicalid(device_t dev);
1281e4925e8SNate Lawson static int	acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
129be2b1797SNate Lawson static int	acpi_isa_pnp_probe(device_t bus, device_t child,
130be2b1797SNate Lawson 			struct isa_pnp_id *ids);
13115e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
132be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
133be2b1797SNate Lawson 			void *context, void **status);
13415e32d5dSMike Smith static void	acpi_shutdown_pre_sync(void *arg, int howto);
13515e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
136eeb3a05fSNate Lawson static void	acpi_shutdown_poweroff(void *arg);
13713d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
13815e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
13915e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
140d75de536SMitsuru IWASAKI static int	acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1411d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
142f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
143f9390180SMitsuru IWASAKI 
14415e32d5dSMike Smith static device_method_t acpi_methods[] = {
14515e32d5dSMike Smith     /* Device interface */
14615e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
14715e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
14815e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
14956a70eadSNate Lawson     DEVMETHOD(device_detach,		bus_generic_detach),
15015e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
15115e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
15215e32d5dSMike Smith     DEVMETHOD(device_resume,		bus_generic_resume),
15315e32d5dSMike Smith 
15415e32d5dSMike Smith     /* Bus interface */
15515e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
15615e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
15715e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
15815e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
15915e32d5dSMike Smith     DEVMETHOD(bus_set_resource,		acpi_set_resource),
16015e32d5dSMike Smith     DEVMETHOD(bus_get_resource,		acpi_get_resource),
16115e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
16215e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
16315e32d5dSMike Smith     DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
16415e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
16515e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
16615e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
16715e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
16815e32d5dSMike Smith 
169bc0f2195SMike Smith     /* ISA emulation */
170bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
171bc0f2195SMike Smith 
17215e32d5dSMike Smith     {0, 0}
17315e32d5dSMike Smith };
17415e32d5dSMike Smith 
17515e32d5dSMike Smith static driver_t acpi_driver = {
17615e32d5dSMike Smith     "acpi",
17715e32d5dSMike Smith     acpi_methods,
17815e32d5dSMike Smith     sizeof(struct acpi_softc),
17915e32d5dSMike Smith };
18015e32d5dSMike Smith 
1813273b005SMike Smith static devclass_t acpi_devclass;
1826d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
183dde24897SMike Smith MODULE_VERSION(acpi, 100);
18415e32d5dSMike Smith 
1851d7b121cSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging");
1861d7b121cSNate Lawson static char acpi_ca_version[12];
1871d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
1881d7b121cSNate Lawson 	      acpi_ca_version, 0, "Version of Intel ACPI-CA");
18915e32d5dSMike Smith 
19015e32d5dSMike Smith /*
191413081d7SNate Lawson  * Allow override of whether methods execute in parallel or not.
192413081d7SNate Lawson  * Default to serial behavior as this fixes some AE_ALREADY_EXISTS errors
193413081d7SNate Lawson  * and matches the MS interpreter.
194413081d7SNate Lawson  */
195413081d7SNate Lawson static int acpi_serialize_methods = 1;
196413081d7SNate Lawson TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods);
197413081d7SNate Lawson 
198413081d7SNate Lawson /* Allow override of whether to support the _OSI method. */
199413081d7SNate Lawson static int acpi_osi_method;
200413081d7SNate Lawson TUNABLE_INT("hw.acpi.osi_method", &acpi_osi_method);
201413081d7SNate Lawson 
202413081d7SNate Lawson /*
2036d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
2046d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
205be2b1797SNate Lawson  * It also cannot be unloaded, since the entire system bus heirarchy hangs
206be2b1797SNate Lawson  * off it.
2076d63101aSMike Smith  */
2086d63101aSMike Smith static int
2096d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
2106d63101aSMike Smith {
2116d63101aSMike Smith     switch(event) {
2126d63101aSMike Smith     case MOD_LOAD:
21387b45ed5SMitsuru IWASAKI 	if (!cold) {
21487b45ed5SMitsuru IWASAKI 	    printf("The ACPI driver cannot be loaded after boot.\n");
2156d63101aSMike Smith 	    return (EPERM);
21687b45ed5SMitsuru IWASAKI 	}
2176d63101aSMike Smith 	break;
2186d63101aSMike Smith     case MOD_UNLOAD:
219f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
2206d63101aSMike Smith 	    return (EBUSY);
221f9390180SMitsuru IWASAKI 	break;
2226d63101aSMike Smith     default:
2236d63101aSMike Smith 	break;
2246d63101aSMike Smith     }
2256d63101aSMike Smith     return (0);
2266d63101aSMike Smith }
2276d63101aSMike Smith 
2286d63101aSMike Smith /*
229bbc2815cSJohn Baldwin  * Perform early initialization.
23015e32d5dSMike Smith  */
231bbc2815cSJohn Baldwin ACPI_STATUS
232bbc2815cSJohn Baldwin acpi_Startup(void)
23315e32d5dSMike Smith {
234d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
235d786139cSMaxime Henrion     char *debugpoint;
23615e32d5dSMike Smith #endif
237bbc2815cSJohn Baldwin     static int error, started = 0;
23815e32d5dSMike Smith 
2391b8c233dSPeter Pentchev     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2401b8c233dSPeter Pentchev 
241bbc2815cSJohn Baldwin     if (started)
242bbc2815cSJohn Baldwin 	return_VALUE (error);
243bbc2815cSJohn Baldwin     started = 1;
24415e32d5dSMike Smith 
245fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000
246be2b1797SNate Lawson     /* Initialise the ACPI mutex */
2476008862bSJohn Baldwin     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
248fc0ea94aSJohn Baldwin #endif
249cb9b0d80SMike Smith 
250413081d7SNate Lawson     /*
251413081d7SNate Lawson      * Set the globals from our tunables.  This is needed because ACPI-CA
252413081d7SNate Lawson      * uses UINT8 for some values and we have no tunable_uint8.
253413081d7SNate Lawson      */
254413081d7SNate Lawson     AcpiGbl_AllMethodsSerialized = acpi_serialize_methods;
255413081d7SNate Lawson     AcpiGbl_CreateOsiMethod = acpi_osi_method;
256413081d7SNate Lawson 
257be2b1797SNate Lawson     /* Start up the ACPI CA subsystem. */
258d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
259d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
260d786139cSMaxime Henrion     if (debugpoint) {
261d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "init"))
26215e32d5dSMike Smith 	    acpi_EnterDebugger();
263d786139cSMaxime Henrion 	freeenv(debugpoint);
264d786139cSMaxime Henrion     }
26515e32d5dSMike Smith #endif
266b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
267bfae45aaSMike Smith 	printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
268bbc2815cSJohn Baldwin 	return_VALUE (error);
26915e32d5dSMike Smith     }
270d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
271d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
272d786139cSMaxime Henrion     if (debugpoint) {
273d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "tables"))
27415e32d5dSMike Smith 	    acpi_EnterDebugger();
275d786139cSMaxime Henrion 	freeenv(debugpoint);
276d786139cSMaxime Henrion     }
27715e32d5dSMike Smith #endif
2781611ea87SMitsuru IWASAKI 
279b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiLoadTables())) {
280bfae45aaSMike Smith 	printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
281bbc2815cSJohn Baldwin 	return_VALUE(error);
28215e32d5dSMike Smith     }
2833184cf5aSNate Lawson 
2843184cf5aSNate Lawson     /* Set up any quirks we have for this XSDT. */
2853184cf5aSNate Lawson     acpi_quirks_set();
2864e376d58SNate Lawson     if (acpi_disabled("acpi"))
2874e376d58SNate Lawson 	return_VALUE (AE_ERROR);
2883184cf5aSNate Lawson 
289bbc2815cSJohn Baldwin     return_VALUE (AE_OK);
290bbc2815cSJohn Baldwin }
291bbc2815cSJohn Baldwin 
292bbc2815cSJohn Baldwin /*
293bbc2815cSJohn Baldwin  * Detect ACPI, perform early initialisation
294bbc2815cSJohn Baldwin  */
295bbc2815cSJohn Baldwin static void
296bbc2815cSJohn Baldwin acpi_identify(driver_t *driver, device_t parent)
297bbc2815cSJohn Baldwin {
298bbc2815cSJohn Baldwin     device_t	child;
299bbc2815cSJohn Baldwin 
300bbc2815cSJohn Baldwin     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
301bbc2815cSJohn Baldwin 
302bbc2815cSJohn Baldwin     if (!cold)
303bbc2815cSJohn Baldwin 	return_VOID;
304bbc2815cSJohn Baldwin 
305bbc2815cSJohn Baldwin     /* Check that we haven't been disabled with a hint. */
306bbc2815cSJohn Baldwin     if (resource_disabled("acpi", 0))
307bbc2815cSJohn Baldwin 	return_VOID;
308bbc2815cSJohn Baldwin 
309bbc2815cSJohn Baldwin     /* Make sure we're not being doubly invoked. */
310bbc2815cSJohn Baldwin     if (device_find_child(parent, "acpi", 0) != NULL)
311bbc2815cSJohn Baldwin 	return_VOID;
312bbc2815cSJohn Baldwin 
313bbc2815cSJohn Baldwin     /* Initialize ACPI-CA. */
314bbc2815cSJohn Baldwin     if (ACPI_FAILURE(acpi_Startup()))
315bbc2815cSJohn Baldwin 	return_VOID;
31615e32d5dSMike Smith 
3174e376d58SNate Lawson     snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION);
3184e376d58SNate Lawson 
319be2b1797SNate Lawson     /* Attach the actual ACPI device. */
32015e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
32115e32d5dSMike Smith 	device_printf(parent, "ACPI: could not attach\n");
3220ae55423SMike Smith 	return_VOID;
32315e32d5dSMike Smith     }
32415e32d5dSMike Smith }
32515e32d5dSMike Smith 
32615e32d5dSMike Smith /*
32715e32d5dSMike Smith  * Fetch some descriptive data from ACPI to put in our attach message
32815e32d5dSMike Smith  */
32915e32d5dSMike Smith static int
33015e32d5dSMike Smith acpi_probe(device_t dev)
33115e32d5dSMike Smith {
33215e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
33315e32d5dSMike Smith     char		buf[20];
33415e32d5dSMike Smith     int			error;
3352ccd1cacSNate Lawson     struct sbuf		sb;
3362ccd1cacSNate Lawson     ACPI_STATUS		status;
337fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
33815e32d5dSMike Smith 
339b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
340f9390180SMitsuru IWASAKI 
341f9390180SMitsuru IWASAKI     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
342f9390180SMitsuru IWASAKI 	power_pm_get_type() != POWER_PM_TYPE_ACPI) {
343be2b1797SNate Lawson 
344f9390180SMitsuru IWASAKI 	device_printf(dev, "Other PM system enabled.\n");
345f9390180SMitsuru IWASAKI 	return_VALUE(ENXIO);
346f9390180SMitsuru IWASAKI     }
347f9390180SMitsuru IWASAKI 
348cb9b0d80SMike Smith     ACPI_LOCK;
3490ae55423SMike Smith 
350b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
351be2b1797SNate Lawson 	device_printf(dev, "couldn't get XSDT header: %s\n",
352be2b1797SNate Lawson 		      AcpiFormatException(status));
353cb9b0d80SMike Smith 	error = ENXIO;
354cb9b0d80SMike Smith     } else {
3552ccd1cacSNate Lawson 	sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
3562ccd1cacSNate Lawson 	sbuf_bcat(&sb, th.OemId, 6);
3572ccd1cacSNate Lawson 	sbuf_trim(&sb);
3582ccd1cacSNate Lawson 	sbuf_putc(&sb, ' ');
3592ccd1cacSNate Lawson 	sbuf_bcat(&sb, th.OemTableId, 8);
3602ccd1cacSNate Lawson 	sbuf_trim(&sb);
3612ccd1cacSNate Lawson 	sbuf_finish(&sb);
3622ccd1cacSNate Lawson 	device_set_desc_copy(dev, sbuf_data(&sb));
3632ccd1cacSNate Lawson 	sbuf_delete(&sb);
364cb9b0d80SMike Smith 	error = 0;
365cb9b0d80SMike Smith     }
366cb9b0d80SMike Smith     ACPI_UNLOCK;
367cb9b0d80SMike Smith     return_VALUE(error);
36815e32d5dSMike Smith }
36915e32d5dSMike Smith 
37015e32d5dSMike Smith static int
37115e32d5dSMike Smith acpi_attach(device_t dev)
37215e32d5dSMike Smith {
37315e32d5dSMike Smith     struct acpi_softc	*sc;
374cb9b0d80SMike Smith     ACPI_STATUS		status;
37515e32d5dSMike Smith     int			error;
37632d18aa5SMike Smith     UINT32		flags;
377498d464fSMitsuru IWASAKI     char		*env;
378d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
379d786139cSMaxime Henrion     char		*debugpoint;
38015e32d5dSMike Smith #endif
381fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
38215e32d5dSMike Smith 
383b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
384cb9b0d80SMike Smith     ACPI_LOCK;
38515e32d5dSMike Smith     sc = device_get_softc(dev);
38615e32d5dSMike Smith     bzero(sc, sizeof(*sc));
38715e32d5dSMike Smith     sc->acpi_dev = dev;
38815e32d5dSMike Smith 
389d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
390d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
391d786139cSMaxime Henrion     if (debugpoint) {
392d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "spaces"))
39315e32d5dSMike Smith 	    acpi_EnterDebugger();
394d786139cSMaxime Henrion 	freeenv(debugpoint);
395d786139cSMaxime Henrion     }
39615e32d5dSMike Smith #endif
39715e32d5dSMike Smith 
398be2b1797SNate Lawson     /* Install the default address space handlers. */
399cb9b0d80SMike Smith     error = ENXIO;
400be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
401be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
402be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
403be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
404be2b1797SNate Lawson 		      AcpiFormatException(status));
405cb9b0d80SMike Smith 	goto out;
40615e32d5dSMike Smith     }
407be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
408be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
409be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
410be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemIO handler: %s\n",
411be2b1797SNate Lawson 		      AcpiFormatException(status));
412cb9b0d80SMike Smith 	goto out;
41315e32d5dSMike Smith     }
414be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
415be2b1797SNate Lawson 		ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
416be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
417be2b1797SNate Lawson 	device_printf(dev, "could not initialise PciConfig handler: %s\n",
418be2b1797SNate Lawson 		      AcpiFormatException(status));
419cb9b0d80SMike Smith 	goto out;
42015e32d5dSMike Smith     }
42115e32d5dSMike Smith 
42215e32d5dSMike Smith     /*
42315e32d5dSMike Smith      * Bring ACPI fully online.
42415e32d5dSMike Smith      *
425be2b1797SNate Lawson      * Note that some systems (specifically, those with namespace evaluation
426be2b1797SNate Lawson      * issues that require the avoidance of parts of the namespace) must
427be2b1797SNate Lawson      * avoid running _INI and _STA on everything, as well as dodging the final
428be2b1797SNate Lawson      * object init pass.
42915e32d5dSMike Smith      *
43032d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
43132d18aa5SMike Smith      *
432be2b1797SNate Lawson      * XXX We should arrange for the object init pass after we have attached
433be2b1797SNate Lawson      *     all our child devices, but on many systems it works here.
43415e32d5dSMike Smith      */
435d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
436d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
437d786139cSMaxime Henrion     if (debugpoint) {
438d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "enable"))
43915e32d5dSMike Smith 	    acpi_EnterDebugger();
440d786139cSMaxime Henrion 	freeenv(debugpoint);
441d786139cSMaxime Henrion     }
44215e32d5dSMike Smith #endif
44332d18aa5SMike Smith     flags = 0;
444d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
44532d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
446b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
447be2b1797SNate Lawson 	device_printf(dev, "Could not enable ACPI: %s\n",
448be2b1797SNate Lawson 		      AcpiFormatException(status));
449cb9b0d80SMike Smith 	goto out;
45015e32d5dSMike Smith     }
45115e32d5dSMike Smith 
452f8335e3aSNate Lawson     /*
453f8335e3aSNate Lawson      * Call the ECDT probe function to provide EC functionality before
454f8335e3aSNate Lawson      * the namespace has been evaluated.
455f8335e3aSNate Lawson      */
456f8335e3aSNate Lawson     acpi_ec_ecdt_probe(dev);
457f8335e3aSNate Lawson 
458b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
459be2b1797SNate Lawson 	device_printf(dev, "Could not initialize ACPI objects: %s\n",
460be2b1797SNate Lawson 		      AcpiFormatException(status));
461b69ed3f4SMitsuru IWASAKI 	goto out;
462b69ed3f4SMitsuru IWASAKI     }
463b69ed3f4SMitsuru IWASAKI 
46415e32d5dSMike Smith     /*
4651d073b1dSJohn Baldwin      * Setup our sysctl tree.
4661d073b1dSJohn Baldwin      *
4671d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
4681d073b1dSJohn Baldwin      */
4691d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
4701d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
4711d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
4721d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
4731d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
474d75de536SMitsuru IWASAKI 	OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
475d75de536SMitsuru IWASAKI 	0, 0, acpi_supported_sleep_state_sysctl, "A", "");
476d75de536SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4771d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4781d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4791d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4801d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4811d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4821d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4831d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
4841d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
485f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
486f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
487f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
488f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
489f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
490f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
4912d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
492ff01efb5SMitsuru IWASAKI 	OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
493ff01efb5SMitsuru IWASAKI 	&sc->acpi_sleep_delay, 0, "sleep delay");
494ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4951611ea87SMitsuru IWASAKI 	OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
4961611ea87SMitsuru IWASAKI 	&sc->acpi_s4bios, 0, "S4BIOS mode");
4971611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4982d644607SMitsuru IWASAKI 	OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
4992d644607SMitsuru IWASAKI 	&sc->acpi_verbose, 0, "verbose mode");
500c6a78e98STakanori Watanabe     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
501c6a78e98STakanori Watanabe 	OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW,
502c6a78e98STakanori Watanabe 	&sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff");
503bf0c18ecSNate Lawson 
504bf0c18ecSNate Lawson     /*
505bf0c18ecSNate Lawson      * Default to 5 seconds before sleeping to give some machines time to
506bf0c18ecSNate Lawson      * stabilize.
507bf0c18ecSNate Lawson      */
508bf0c18ecSNate Lawson     sc->acpi_sleep_delay = 5;
509c6a78e98STakanori Watanabe     sc->acpi_disable_on_poweroff = 1;
5102d644607SMitsuru IWASAKI     if (bootverbose)
5112d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
512498d464fSMitsuru IWASAKI     if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) {
513498d464fSMitsuru IWASAKI 	sc->acpi_verbose = 1;
514498d464fSMitsuru IWASAKI 	freeenv(env);
515498d464fSMitsuru IWASAKI     }
5161d073b1dSJohn Baldwin 
5172d610c46SNate Lawson     /* Only enable S4BIOS by default if the FACS says it is available. */
5182d610c46SNate Lawson     if (AcpiGbl_FACS->S4Bios_f != 0)
5192d610c46SNate Lawson 	    sc->acpi_s4bios = 1;
5202d610c46SNate Lawson 
5211d073b1dSJohn Baldwin     /*
52215e32d5dSMike Smith      * Dispatch the default sleep state to devices.
52315e32d5dSMike Smith      * TBD: should be configured from userland policy manager.
52415e32d5dSMike Smith      */
52515e32d5dSMike Smith     sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
52615e32d5dSMike Smith     sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
52715e32d5dSMike Smith     sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
528f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
529f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
53015e32d5dSMike Smith 
53113d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
53215e32d5dSMike Smith 
53315e32d5dSMike Smith     /*
53415e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
53515e32d5dSMike Smith      */
536d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
537d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
538d786139cSMaxime Henrion     if (debugpoint) {
539d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "probe"))
54015e32d5dSMike Smith 	    acpi_EnterDebugger();
541d786139cSMaxime Henrion 	freeenv(debugpoint);
542d786139cSMaxime Henrion     }
54315e32d5dSMike Smith #endif
54415e32d5dSMike Smith 
545be2b1797SNate Lawson     /* Register our shutdown handlers */
546be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc,
547be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
548be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
549be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
55015e32d5dSMike Smith 
55115e32d5dSMike Smith     /*
55215e32d5dSMike Smith      * Register our acpi event handlers.
55315e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
55415e32d5dSMike Smith      */
555be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
556be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
557be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
558be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
55915e32d5dSMike Smith 
560be2b1797SNate Lawson     /* Flag our initial states. */
56115e32d5dSMike Smith     sc->acpi_enabled = 1;
56215e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
563ece50487SMitsuru IWASAKI     sc->acpi_sleep_disabled = 0;
56415e32d5dSMike Smith 
565be2b1797SNate Lawson     /* Create the control device */
566a89fcf28STakanori Watanabe     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
5672a4689e8SRobert Watson 			      "acpi");
56815e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
56915e32d5dSMike Smith 
570d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
571d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
572d786139cSMaxime Henrion     if (debugpoint) {
573be2b1797SNate Lawson 	if (strcmp(debugpoint, "running") == 0)
57415e32d5dSMike Smith 	    acpi_EnterDebugger();
575d786139cSMaxime Henrion 	freeenv(debugpoint);
576d786139cSMaxime Henrion     }
57715e32d5dSMike Smith #endif
578f86214b6SMitsuru IWASAKI 
57991da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS
580be2b1797SNate Lawson     if ((error = acpi_task_thread_init()))
581c573e654SMitsuru IWASAKI 	goto out;
582c573e654SMitsuru IWASAKI #endif
583c573e654SMitsuru IWASAKI 
584be2b1797SNate Lawson     if ((error = acpi_machdep_init(dev)))
585f86214b6SMitsuru IWASAKI 	goto out;
586f86214b6SMitsuru IWASAKI 
587f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
588f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
589f9390180SMitsuru IWASAKI 
590eeb6dba2SJohn Baldwin     if (!acpi_disabled("bus"))
591eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
592eeb6dba2SJohn Baldwin 
593cb9b0d80SMike Smith     error = 0;
594cb9b0d80SMike Smith 
595cb9b0d80SMike Smith  out:
596cb9b0d80SMike Smith     ACPI_UNLOCK;
597cb9b0d80SMike Smith     return_VALUE (error);
59815e32d5dSMike Smith }
59915e32d5dSMike Smith 
6003184cf5aSNate Lawson static void
6013184cf5aSNate Lawson acpi_quirks_set()
6023184cf5aSNate Lawson {
6033184cf5aSNate Lawson     XSDT_DESCRIPTOR *xsdt;
6043184cf5aSNate Lawson     struct acpi_quirks *quirk;
6053184cf5aSNate Lawson     char *env, *tmp;
6063184cf5aSNate Lawson     int len;
6073184cf5aSNate Lawson 
6084e376d58SNate Lawson     /*
6094e376d58SNate Lawson      * If the user loaded a custom table or disabled "quirks", leave
6104e376d58SNate Lawson      * the settings alone.
6114e376d58SNate Lawson      */
6123184cf5aSNate Lawson     len = 0;
6134e376d58SNate Lawson     if ((env = getenv("acpi_dsdt_load")) != NULL) {
6144e376d58SNate Lawson 	/* XXX No strcasecmp but this is good enough. */
6154e376d58SNate Lawson 	if (*env == 'Y' || *env == 'y')
6164e376d58SNate Lawson 	    goto out;
6174e376d58SNate Lawson 	freeenv(env);
6184e376d58SNate Lawson     }
6193184cf5aSNate Lawson     if ((env = getenv("debug.acpi.disabled")) != NULL) {
6204e376d58SNate Lawson 	if (strstr("quirks", env) != NULL)
6213184cf5aSNate Lawson 	    goto out;
6223184cf5aSNate Lawson 	len = strlen(env);
6233184cf5aSNate Lawson     }
6243184cf5aSNate Lawson 
6253184cf5aSNate Lawson     /*
6263184cf5aSNate Lawson      * Search through our quirk table and concatenate the disabled
6273184cf5aSNate Lawson      * values with whatever we find.
6283184cf5aSNate Lawson      */
6293184cf5aSNate Lawson     xsdt = AcpiGbl_XSDT;
6303184cf5aSNate Lawson     for (quirk = acpi_quirks_table; quirk->OemId; quirk++) {
6313184cf5aSNate Lawson 	if (!strncmp(xsdt->OemId, quirk->OemId, strlen(quirk->OemId)) &&
6323184cf5aSNate Lawson 	    (xsdt->OemRevision == quirk->OemRevision ||
6333184cf5aSNate Lawson 	    quirk->OemRevision == ACPI_OEM_REV_ANY)) {
6343184cf5aSNate Lawson 		len += strlen(quirk->value) + 2;
6353184cf5aSNate Lawson 		if ((tmp = malloc(len, M_TEMP, M_NOWAIT)) == NULL)
6363184cf5aSNate Lawson 		    goto out;
6373184cf5aSNate Lawson 		sprintf(tmp, "%s %s", env ? env : "", quirk->value);
6383184cf5aSNate Lawson 		setenv("debug.acpi.disabled", tmp);
6393184cf5aSNate Lawson 		free(tmp, M_TEMP);
6403184cf5aSNate Lawson 		break;
6413184cf5aSNate Lawson 	}
6423184cf5aSNate Lawson     }
6433184cf5aSNate Lawson 
6443184cf5aSNate Lawson out:
6453184cf5aSNate Lawson     if (env)
6463184cf5aSNate Lawson 	freeenv(env);
6473184cf5aSNate Lawson }
6483184cf5aSNate Lawson 
64915e32d5dSMike Smith /*
65015e32d5dSMike Smith  * Handle a new device being added
65115e32d5dSMike Smith  */
65215e32d5dSMike Smith static device_t
65315e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
65415e32d5dSMike Smith {
65515e32d5dSMike Smith     struct acpi_device	*ad;
65615e32d5dSMike Smith     device_t		child;
65715e32d5dSMike Smith 
658be2b1797SNate Lawson     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
65915e32d5dSMike Smith 	return (NULL);
66015e32d5dSMike Smith 
66115e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
66215e32d5dSMike Smith 
66315e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
66415e32d5dSMike Smith     if (child != NULL)
66515e32d5dSMike Smith 	device_set_ivars(child, ad);
66615e32d5dSMike Smith     return (child);
66715e32d5dSMike Smith }
66815e32d5dSMike Smith 
66915e32d5dSMike Smith static int
67015e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
67115e32d5dSMike Smith {
67215e32d5dSMike Smith     struct acpi_device	 *adev = device_get_ivars(child);
67315e32d5dSMike Smith     struct resource_list *rl = &adev->ad_rl;
67415e32d5dSMike Smith     int retval = 0;
67515e32d5dSMike Smith 
67615e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
6779ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
6789ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
6799ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
6809ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
68115e32d5dSMike Smith     retval += bus_print_child_footer(bus, child);
68215e32d5dSMike Smith 
68315e32d5dSMike Smith     return (retval);
68415e32d5dSMike Smith }
68515e32d5dSMike Smith 
68615e32d5dSMike Smith /*
68715e32d5dSMike Smith  * Handle per-device ivars
68815e32d5dSMike Smith  */
68915e32d5dSMike Smith static int
69015e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
69115e32d5dSMike Smith {
69215e32d5dSMike Smith     struct acpi_device	*ad;
69315e32d5dSMike Smith 
69415e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
69515e32d5dSMike Smith 	printf("device has no ivars\n");
69615e32d5dSMike Smith 	return (ENOENT);
69715e32d5dSMike Smith     }
69815e32d5dSMike Smith 
699be2b1797SNate Lawson     /* ACPI and ISA compatibility ivars */
70015e32d5dSMike Smith     switch(index) {
70115e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
70215e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
70315e32d5dSMike Smith 	break;
70415e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
70515e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
70615e32d5dSMike Smith 	break;
70715e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
70815e32d5dSMike Smith 	*(void **)result = ad->ad_private;
70915e32d5dSMike Smith 	break;
71032d18aa5SMike Smith     case ISA_IVAR_VENDORID:
71132d18aa5SMike Smith     case ISA_IVAR_SERIAL:
71232d18aa5SMike Smith     case ISA_IVAR_COMPATID:
71332d18aa5SMike Smith 	*(int *)result = -1;
71432d18aa5SMike Smith 	break;
71532d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
71632d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
71732d18aa5SMike Smith 	break;
71815e32d5dSMike Smith     default:
71915e32d5dSMike Smith 	return (ENOENT);
72015e32d5dSMike Smith     }
721be2b1797SNate Lawson 
72215e32d5dSMike Smith     return (0);
72315e32d5dSMike Smith }
72415e32d5dSMike Smith 
72515e32d5dSMike Smith static int
72615e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
72715e32d5dSMike Smith {
72815e32d5dSMike Smith     struct acpi_device	*ad;
72915e32d5dSMike Smith 
73015e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
73115e32d5dSMike Smith 	printf("device has no ivars\n");
73215e32d5dSMike Smith 	return (ENOENT);
73315e32d5dSMike Smith     }
73415e32d5dSMike Smith 
73515e32d5dSMike Smith     switch(index) {
73615e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
73715e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
73815e32d5dSMike Smith 	break;
73915e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
74015e32d5dSMike Smith 	ad->ad_magic = (int)value;
74115e32d5dSMike Smith 	break;
74215e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
74315e32d5dSMike Smith 	ad->ad_private = (void *)value;
74415e32d5dSMike Smith 	break;
74515e32d5dSMike Smith     default:
7462ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
74715e32d5dSMike Smith 	return (ENOENT);
74815e32d5dSMike Smith     }
749be2b1797SNate Lawson 
75015e32d5dSMike Smith     return (0);
75115e32d5dSMike Smith }
75215e32d5dSMike Smith 
753be2b1797SNate Lawson ACPI_HANDLE
754be2b1797SNate Lawson acpi_get_handle(device_t dev)
755be2b1797SNate Lawson {
756be2b1797SNate Lawson     uintptr_t up;
757be2b1797SNate Lawson     ACPI_HANDLE	h;
758be2b1797SNate Lawson 
759be2b1797SNate Lawson     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, &up))
760be2b1797SNate Lawson 	return(NULL);
761be2b1797SNate Lawson     h = (ACPI_HANDLE)up;
762be2b1797SNate Lawson     return (h);
763be2b1797SNate Lawson }
764be2b1797SNate Lawson 
765be2b1797SNate Lawson int
766be2b1797SNate Lawson acpi_set_handle(device_t dev, ACPI_HANDLE h)
767be2b1797SNate Lawson {
768be2b1797SNate Lawson     uintptr_t up;
769be2b1797SNate Lawson 
770be2b1797SNate Lawson     up = (uintptr_t)h;
771be2b1797SNate Lawson     return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, up));
772be2b1797SNate Lawson }
773be2b1797SNate Lawson 
774be2b1797SNate Lawson int
775be2b1797SNate Lawson acpi_get_magic(device_t dev)
776be2b1797SNate Lawson {
777be2b1797SNate Lawson     uintptr_t up;
778be2b1797SNate Lawson     int	m;
779be2b1797SNate Lawson 
780be2b1797SNate Lawson     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, &up))
781be2b1797SNate Lawson 	return(0);
782be2b1797SNate Lawson     m = (int)up;
783be2b1797SNate Lawson     return (m);
784be2b1797SNate Lawson }
785be2b1797SNate Lawson 
786be2b1797SNate Lawson int
787be2b1797SNate Lawson acpi_set_magic(device_t dev, int m)
788be2b1797SNate Lawson {
789be2b1797SNate Lawson     uintptr_t up;
790be2b1797SNate Lawson 
791be2b1797SNate Lawson     up = (uintptr_t)m;
792be2b1797SNate Lawson     return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, up));
793be2b1797SNate Lawson }
794be2b1797SNate Lawson 
795be2b1797SNate Lawson void *
796be2b1797SNate Lawson acpi_get_private(device_t dev)
797be2b1797SNate Lawson {
798be2b1797SNate Lawson     uintptr_t up;
799be2b1797SNate Lawson     void *p;
800be2b1797SNate Lawson 
801be2b1797SNate Lawson     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, &up))
802be2b1797SNate Lawson 	return (NULL);
803be2b1797SNate Lawson     p = (void *)up;
804be2b1797SNate Lawson     return (p);
805be2b1797SNate Lawson }
806be2b1797SNate Lawson 
807be2b1797SNate Lawson int
808be2b1797SNate Lawson acpi_set_private(device_t dev, void *p)
809be2b1797SNate Lawson {
810be2b1797SNate Lawson     uintptr_t up;
811be2b1797SNate Lawson 
812be2b1797SNate Lawson     up = (uintptr_t)p;
813be2b1797SNate Lawson     return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, up));
814be2b1797SNate Lawson }
815be2b1797SNate Lawson 
816be2b1797SNate Lawson ACPI_OBJECT_TYPE
817be2b1797SNate Lawson acpi_get_type(device_t dev)
818be2b1797SNate Lawson {
819be2b1797SNate Lawson     ACPI_HANDLE		h;
820be2b1797SNate Lawson     ACPI_OBJECT_TYPE	t;
821be2b1797SNate Lawson 
822be2b1797SNate Lawson     if ((h = acpi_get_handle(dev)) == NULL)
823be2b1797SNate Lawson 	return (ACPI_TYPE_NOT_FOUND);
824be2b1797SNate Lawson     if (AcpiGetType(h, &t) != AE_OK)
825be2b1797SNate Lawson 	return (ACPI_TYPE_NOT_FOUND);
826be2b1797SNate Lawson     return (t);
827be2b1797SNate Lawson }
828be2b1797SNate Lawson 
82915e32d5dSMike Smith /*
83015e32d5dSMike Smith  * Handle child resource allocation/removal
83115e32d5dSMike Smith  */
83215e32d5dSMike Smith static int
833be2b1797SNate Lawson acpi_set_resource(device_t dev, device_t child, int type, int rid,
834be2b1797SNate Lawson 		  u_long start, u_long count)
83515e32d5dSMike Smith {
83615e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
83715e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
83815e32d5dSMike Smith 
83915e32d5dSMike Smith     resource_list_add(rl, type, rid, start, start + count -1, count);
84015e32d5dSMike Smith 
84115e32d5dSMike Smith     return(0);
84215e32d5dSMike Smith }
84315e32d5dSMike Smith 
84415e32d5dSMike Smith static int
845be2b1797SNate Lawson acpi_get_resource(device_t dev, device_t child, int type, int rid,
846be2b1797SNate Lawson 		  u_long *startp, u_long *countp)
84715e32d5dSMike Smith {
84815e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
84915e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
85015e32d5dSMike Smith     struct resource_list_entry	*rle;
85115e32d5dSMike Smith 
85215e32d5dSMike Smith     rle = resource_list_find(rl, type, rid);
85315e32d5dSMike Smith     if (!rle)
85415e32d5dSMike Smith 	return(ENOENT);
85515e32d5dSMike Smith 
85615e32d5dSMike Smith     if (startp)
85715e32d5dSMike Smith 	*startp = rle->start;
85815e32d5dSMike Smith     if (countp)
85915e32d5dSMike Smith 	*countp = rle->count;
86015e32d5dSMike Smith 
86115e32d5dSMike Smith     return (0);
86215e32d5dSMike Smith }
86315e32d5dSMike Smith 
86415e32d5dSMike Smith static struct resource *
86515e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
86615e32d5dSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
86715e32d5dSMike Smith {
86815e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
86915e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
87015e32d5dSMike Smith 
871be2b1797SNate Lawson     return (resource_list_alloc(rl, bus, child, type, rid, start, end, count,
872be2b1797SNate Lawson 	    flags));
87315e32d5dSMike Smith }
87415e32d5dSMike Smith 
87515e32d5dSMike Smith static int
87615e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
87715e32d5dSMike Smith {
87815e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
87915e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
88015e32d5dSMike Smith 
88115e32d5dSMike Smith     return (resource_list_release(rl, bus, child, type, rid, r));
88215e32d5dSMike Smith }
88315e32d5dSMike Smith 
884dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */
885dc750869SNate Lawson struct resource *
886dc750869SNate Lawson acpi_bus_alloc_gas(device_t dev, int *rid, ACPI_GENERIC_ADDRESS *gas)
887dc750869SNate Lawson {
888dc750869SNate Lawson     int type;
889dc750869SNate Lawson 
890dc750869SNate Lawson     if (gas == NULL || !ACPI_VALID_ADDRESS(gas->Address) ||
891dc750869SNate Lawson 	gas->RegisterBitWidth < 8)
892dc750869SNate Lawson 	return (NULL);
893dc750869SNate Lawson 
894dc750869SNate Lawson     switch (gas->AddressSpaceId) {
895dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
896dc750869SNate Lawson 	type = SYS_RES_MEMORY;
897dc750869SNate Lawson 	break;
898dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_IO:
899dc750869SNate Lawson 	type = SYS_RES_IOPORT;
900dc750869SNate Lawson 	break;
901dc750869SNate Lawson     default:
902dc750869SNate Lawson 	return (NULL);
903dc750869SNate Lawson     }
904dc750869SNate Lawson 
905dc750869SNate Lawson     bus_set_resource(dev, type, *rid, gas->Address, gas->RegisterBitWidth / 8);
9065f96beb9SNate Lawson     return (bus_alloc_resource_any(dev, type, rid, RF_ACTIVE));
907dc750869SNate Lawson }
908dc750869SNate Lawson 
90915e32d5dSMike Smith /*
910bc0f2195SMike Smith  * Handle ISA-like devices probing for a PnP ID to match.
911bc0f2195SMike Smith  */
912bc0f2195SMike Smith #define PNP_EISAID(s)				\
913bc0f2195SMike Smith 	((((s[0] - '@') & 0x1f) << 2)		\
914bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x18) >> 3)		\
915bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x07) << 13)	\
916bc0f2195SMike Smith 	 | (((s[2] - '@') & 0x1f) << 8)		\
917bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[4]) << 16)		\
918bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[3]) << 20)		\
919bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[6]) << 24)		\
920bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[5]) << 28))
921bc0f2195SMike Smith 
9221e4925e8SNate Lawson static uint32_t
92332d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
924bc0f2195SMike Smith {
9251e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
9261e4925e8SNate Lawson     ACPI_BUFFER		buf;
927bc0f2195SMike Smith     ACPI_HANDLE		h;
928bc0f2195SMike Smith     ACPI_STATUS		error;
929bc0f2195SMike Smith     u_int32_t		pnpid;
930fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
93132d18aa5SMike Smith 
932b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
93332d18aa5SMike Smith 
93432d18aa5SMike Smith     pnpid = 0;
935bd189fe7SNate Lawson     buf.Pointer = NULL;
936bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
937bd189fe7SNate Lawson 
93832d18aa5SMike Smith     ACPI_LOCK;
93932d18aa5SMike Smith 
9406fca9360SNate Lawson     /* Fetch and validate the HID. */
94132d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
942bd189fe7SNate Lawson 	goto out;
9436fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
9446fca9360SNate Lawson     if (ACPI_FAILURE(error))
945bd189fe7SNate Lawson 	goto out;
9461e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
94732d18aa5SMike Smith 
9481e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_HID) != 0)
9491e4925e8SNate Lawson 	pnpid = PNP_EISAID(devinfo->HardwareId.Value);
950be2b1797SNate Lawson 
951bd189fe7SNate Lawson out:
952bd189fe7SNate Lawson     if (buf.Pointer != NULL)
9531e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
95432d18aa5SMike Smith     ACPI_UNLOCK;
95532d18aa5SMike Smith     return_VALUE (pnpid);
95632d18aa5SMike Smith }
95732d18aa5SMike Smith 
9581e4925e8SNate Lawson static int
9591e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
960b2566207STakanori Watanabe {
9611e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
9621e4925e8SNate Lawson     ACPI_BUFFER		buf;
963b2566207STakanori Watanabe     ACPI_HANDLE		h;
964b2566207STakanori Watanabe     ACPI_STATUS		error;
9651e4925e8SNate Lawson     uint32_t		*pnpid;
9661e4925e8SNate Lawson     int			valid, i;
967b2566207STakanori Watanabe     ACPI_LOCK_DECL;
968b2566207STakanori Watanabe 
969b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
970b2566207STakanori Watanabe 
9711e4925e8SNate Lawson     pnpid = cids;
9721e4925e8SNate Lawson     valid = 0;
973bd189fe7SNate Lawson     buf.Pointer = NULL;
974bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
975bd189fe7SNate Lawson 
976b2566207STakanori Watanabe     ACPI_LOCK;
977b2566207STakanori Watanabe 
9781e4925e8SNate Lawson     /* Fetch and validate the CID */
979b2566207STakanori Watanabe     if ((h = acpi_get_handle(dev)) == NULL)
980bd189fe7SNate Lawson 	goto out;
9811e4925e8SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
9821e4925e8SNate Lawson     if (ACPI_FAILURE(error))
983bd189fe7SNate Lawson 	goto out;
9841e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
9851e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_CID) == 0)
986b2566207STakanori Watanabe 	goto out;
987b2566207STakanori Watanabe 
9881e4925e8SNate Lawson     if (devinfo->CompatibilityId.Count < count)
9891e4925e8SNate Lawson 	count = devinfo->CompatibilityId.Count;
9901e4925e8SNate Lawson     for (i = 0; i < count; i++) {
9911e4925e8SNate Lawson 	if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0)
9921e4925e8SNate Lawson 	    continue;
9931e4925e8SNate Lawson 	*pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value);
9941e4925e8SNate Lawson 	valid++;
995b2566207STakanori Watanabe     }
996b2566207STakanori Watanabe 
9971e4925e8SNate Lawson out:
998bd189fe7SNate Lawson     if (buf.Pointer != NULL)
9991e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
10001e4925e8SNate Lawson     ACPI_UNLOCK;
10011e4925e8SNate Lawson     return_VALUE (valid);
10021e4925e8SNate Lawson }
1003b2566207STakanori Watanabe 
100432d18aa5SMike Smith static int
100532d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
100632d18aa5SMike Smith {
10071e4925e8SNate Lawson     int			result, cid_count, i;
10081e4925e8SNate Lawson     uint32_t		lid, cids[8];
1009bc0f2195SMike Smith 
1010b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1011bc0f2195SMike Smith 
1012bc0f2195SMike Smith     /*
1013bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
1014bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
1015bc0f2195SMike Smith      * that to happen, so don't ever return it.
1016bc0f2195SMike Smith      */
1017bc0f2195SMike Smith     result = ENXIO;
1018bc0f2195SMike Smith 
1019be2b1797SNate Lawson     /* Scan the supplied IDs for a match */
1020b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
10211e4925e8SNate Lawson     cid_count = acpi_isa_get_compatid(child, cids, 8);
1022bc0f2195SMike Smith     while (ids && ids->ip_id) {
10231e4925e8SNate Lawson 	if (lid == ids->ip_id) {
1024bc0f2195SMike Smith 	    result = 0;
1025bc0f2195SMike Smith 	    goto out;
1026bc0f2195SMike Smith 	}
10271e4925e8SNate Lawson 	for (i = 0; i < cid_count; i++) {
10281e4925e8SNate Lawson 	    if (cids[i] == ids->ip_id) {
10291e4925e8SNate Lawson 		result = 0;
10301e4925e8SNate Lawson 		goto out;
10311e4925e8SNate Lawson 	    }
10321e4925e8SNate Lawson 	}
1033bc0f2195SMike Smith 	ids++;
1034bc0f2195SMike Smith     }
1035be2b1797SNate Lawson 
1036bc0f2195SMike Smith  out:
1037bc0f2195SMike Smith     return_VALUE (result);
1038bc0f2195SMike Smith }
1039bc0f2195SMike Smith 
1040bc0f2195SMike Smith /*
104115e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
104215e32d5dSMike Smith  *
1043be2b1797SNate Lawson  * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and
1044be2b1797SNate Lawson  * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
104515e32d5dSMike Smith  */
104615e32d5dSMike Smith static void
104715e32d5dSMike Smith acpi_probe_children(device_t bus)
104815e32d5dSMike Smith {
104915e32d5dSMike Smith     ACPI_HANDLE	parent;
1050be2b1797SNate Lawson     ACPI_STATUS	status;
10517d3bcec9SMike Smith     static char	*scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
105215e32d5dSMike Smith     int		i;
105315e32d5dSMike Smith 
1054b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1055cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
10560ae55423SMike Smith 
1057be2b1797SNate Lawson     /* Create any static children by calling device identify methods. */
10584c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
105915e32d5dSMike Smith     bus_generic_probe(bus);
106015e32d5dSMike Smith 
106115e32d5dSMike Smith     /*
106215e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
106315e32d5dSMike Smith      * we find.
106415e32d5dSMike Smith      *
106515e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1066be2b1797SNate Lawson      * we want to create nodes for all devices, not just those that are
1067be2b1797SNate Lawson      * currently present. (This assumes that we don't want to create/remove
1068be2b1797SNate Lawson      * devices as they appear, which might be smarter.)
106915e32d5dSMike Smith      */
10704c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
1071be2b1797SNate Lawson     for (i = 0; scopes[i] != NULL; i++) {
1072be2b1797SNate Lawson 	status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent);
1073be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
1074be2b1797SNate Lawson 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child,
1075be2b1797SNate Lawson 			      bus, NULL);
1076be2b1797SNate Lawson 	}
1077be2b1797SNate Lawson     }
107815e32d5dSMike Smith 
107915e32d5dSMike Smith     /*
108015e32d5dSMike Smith      * Scan all of the child devices we have created and let them probe/attach.
108115e32d5dSMike Smith      */
10824c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
108315e32d5dSMike Smith     bus_generic_attach(bus);
108415e32d5dSMike Smith 
108515e32d5dSMike Smith     /*
108615e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
108715e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
108815e32d5dSMike Smith      */
10894c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
109015e32d5dSMike Smith     bus_generic_attach(bus);
10910ae55423SMike Smith 
1092bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
10930ae55423SMike Smith     return_VOID;
109415e32d5dSMike Smith }
109515e32d5dSMike Smith 
109615e32d5dSMike Smith /*
109715e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
109815e32d5dSMike Smith  * it.
109915e32d5dSMike Smith  */
110015e32d5dSMike Smith static ACPI_STATUS
110115e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
110215e32d5dSMike Smith {
110315e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
110415e32d5dSMike Smith     device_t		child, bus = (device_t)context;
110515e32d5dSMike Smith 
1106b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
11070ae55423SMike Smith 
1108be2b1797SNate Lawson     /* Skip this device if we think we'll have trouble with it. */
11090ae55423SMike Smith     if (acpi_avoid(handle))
11100ae55423SMike Smith 	return_ACPI_STATUS (AE_OK);
11110ae55423SMike Smith 
1112b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
111315e32d5dSMike Smith 	switch(type) {
111415e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
111515e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
111615e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
111715e32d5dSMike Smith 	case ACPI_TYPE_POWER:
11180ae55423SMike Smith 	    if (acpi_disabled("children"))
11190ae55423SMike Smith 		break;
1120be2b1797SNate Lawson 
112115e32d5dSMike Smith 	    /*
112215e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
112315e32d5dSMike Smith 	     * so that the probe/attach passes will run breadth-first.
112415e32d5dSMike Smith 	     */
1125be2b1797SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n",
1126be2b1797SNate Lawson 			     acpi_name(handle)));
112715e32d5dSMike Smith 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
1128bc0f2195SMike Smith 	    if (child == NULL)
1129bc0f2195SMike Smith 		break;
113015e32d5dSMike Smith 	    acpi_set_handle(child, handle);
1131bc0f2195SMike Smith 
1132bc0f2195SMike Smith 	    /*
1133b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
1134b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
1135b519f9d6SMike Smith 	     * the handle, but we don't probe it).
1136b519f9d6SMike Smith 	     */
1137be2b1797SNate Lawson 	    if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
1138b519f9d6SMike Smith 		device_disable(child);
1139b519f9d6SMike Smith 		break;
1140b519f9d6SMike Smith 	    }
1141b519f9d6SMike Smith 
1142b519f9d6SMike Smith 	    /*
1143bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
1144bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
1145bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
1146bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
1147bc0f2195SMike Smith 	     * device not to have any resources.
1148bc0f2195SMike Smith 	     */
1149bc0f2195SMike Smith 	    acpi_parse_resources(child, handle, &acpi_res_parse_set);
1150bc0f2195SMike Smith 
1151be2b1797SNate Lawson 	    /* If we're debugging, probe/attach now rather than later */
1152b53f2771SMike Smith 	    ACPI_DEBUG_EXEC(device_probe_and_attach(child));
11530ae55423SMike Smith 	    break;
115415e32d5dSMike Smith 	}
115515e32d5dSMike Smith     }
1156be2b1797SNate Lawson 
11570ae55423SMike Smith     return_ACPI_STATUS (AE_OK);
115815e32d5dSMike Smith }
115915e32d5dSMike Smith 
116015e32d5dSMike Smith static void
116115e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto)
116215e32d5dSMike Smith {
1163c6a78e98STakanori Watanabe     struct acpi_softc *sc = arg;
1164c6a78e98STakanori Watanabe 
1165cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1166cb9b0d80SMike Smith 
116715e32d5dSMike Smith     /*
11680ae55423SMike Smith      * Disable all ACPI events before soft off, otherwise the system
116915e32d5dSMike Smith      * will be turned on again on some laptops.
117015e32d5dSMike Smith      *
117115e32d5dSMike Smith      * XXX this should probably be restricted to masking some events just
117215e32d5dSMike Smith      *     before powering down, since we may still need ACPI during the
117315e32d5dSMike Smith      *     shutdown process.
117415e32d5dSMike Smith      */
1175c6a78e98STakanori Watanabe     if (sc->acpi_disable_on_poweroff)
1176c6a78e98STakanori Watanabe 	acpi_Disable(sc);
117715e32d5dSMike Smith }
117815e32d5dSMike Smith 
117915e32d5dSMike Smith static void
118015e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
118115e32d5dSMike Smith {
1182eeb3a05fSNate Lawson 
1183eeb3a05fSNate Lawson     ACPI_ASSERTLOCK;
1184eeb3a05fSNate Lawson 
1185eeb3a05fSNate Lawson     /*
1186eeb3a05fSNate Lawson      * If powering off, run the actual shutdown code on each processor.
1187eeb3a05fSNate Lawson      * It will only perform the shutdown on the BSP.  Some chipsets do
1188eeb3a05fSNate Lawson      * not power off the system correctly if called from an AP.
1189eeb3a05fSNate Lawson      */
1190eeb3a05fSNate Lawson     if ((howto & RB_POWEROFF) != 0) {
1191eeb3a05fSNate Lawson 	printf("Powering system off using ACPI\n");
1192eeb3a05fSNate Lawson 	smp_rendezvous(NULL, acpi_shutdown_poweroff, NULL, NULL);
1193eeb3a05fSNate Lawson     } else {
1194eeb3a05fSNate Lawson 	printf("Shutting down ACPI\n");
1195eeb3a05fSNate Lawson 	AcpiTerminate();
1196eeb3a05fSNate Lawson     }
1197eeb3a05fSNate Lawson }
1198eeb3a05fSNate Lawson 
1199eeb3a05fSNate Lawson static void
1200eeb3a05fSNate Lawson acpi_shutdown_poweroff(void *arg)
1201eeb3a05fSNate Lawson {
120215e32d5dSMike Smith     ACPI_STATUS	status;
120315e32d5dSMike Smith 
1204cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1205cb9b0d80SMike Smith 
1206eeb3a05fSNate Lawson     /* Only attempt to power off if this is the BSP (cpuid 0). */
1207eeb3a05fSNate Lawson     if (PCPU_GET(cpuid) != 0)
1208eeb3a05fSNate Lawson 	return;
1209eeb3a05fSNate Lawson 
1210be2b1797SNate Lawson     status = AcpiEnterSleepStatePrep(acpi_off_state);
1211be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
1212b9c780d6SMitsuru IWASAKI 	printf("AcpiEnterSleepStatePrep failed - %s\n",
1213b9c780d6SMitsuru IWASAKI 	       AcpiFormatException(status));
1214b9c780d6SMitsuru IWASAKI 	return;
1215b9c780d6SMitsuru IWASAKI     }
121655398047SNate Lawson     ACPI_DISABLE_IRQS();
1217be2b1797SNate Lawson     status = AcpiEnterSleepState(acpi_off_state);
1218be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
1219bfae45aaSMike Smith 	printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
122015e32d5dSMike Smith     } else {
122115e32d5dSMike Smith 	DELAY(1000000);
122215e32d5dSMike Smith 	printf("ACPI power-off failed - timeout\n");
122315e32d5dSMike Smith     }
122415e32d5dSMike Smith }
122515e32d5dSMike Smith 
122613d4f7baSMitsuru IWASAKI static void
122713d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
122813d4f7baSMitsuru IWASAKI {
122913d4f7baSMitsuru IWASAKI     static int	first_time = 1;
123013d4f7baSMitsuru IWASAKI 
1231cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1232cb9b0d80SMike Smith 
123313d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
1234be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
123559ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
123613d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
123733febf93SNate Lawson 				     acpi_event_power_button_sleep, sc);
1238be2b1797SNate Lawson 	if (first_time)
12396c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Power Button (fixed)\n");
124013d4f7baSMitsuru IWASAKI     }
1241be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
124259ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
124313d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
124433febf93SNate Lawson 				     acpi_event_sleep_button_sleep, sc);
1245be2b1797SNate Lawson 	if (first_time)
12466c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
124713d4f7baSMitsuru IWASAKI     }
124813d4f7baSMitsuru IWASAKI 
124913d4f7baSMitsuru IWASAKI     first_time = 0;
125013d4f7baSMitsuru IWASAKI }
125113d4f7baSMitsuru IWASAKI 
125215e32d5dSMike Smith /*
1253c5ba0be4SMike Smith  * Returns true if the device is actually present and should
1254c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
1255c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
1256c5ba0be4SMike Smith  */
1257c5ba0be4SMike Smith BOOLEAN
1258c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
1259c5ba0be4SMike Smith {
12601e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1261c5ba0be4SMike Smith     ACPI_HANDLE		h;
12621e4925e8SNate Lawson     ACPI_BUFFER		buf;
1263c5ba0be4SMike Smith     ACPI_STATUS		error;
12641e4925e8SNate Lawson     int			ret;
1265c5ba0be4SMike Smith 
1266cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1267cb9b0d80SMike Smith 
12681e4925e8SNate Lawson     ret = FALSE;
1269c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1270c5ba0be4SMike Smith 	return (FALSE);
12711e4925e8SNate Lawson     buf.Pointer = NULL;
12721e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
12736fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
12746fca9360SNate Lawson     if (ACPI_FAILURE(error))
1275c5ba0be4SMike Smith 	return (FALSE);
12761e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1277be2b1797SNate Lawson 
1278be2b1797SNate Lawson     /* If no _STA method, must be present */
12791e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
12801e4925e8SNate Lawson 	ret = TRUE;
1281be2b1797SNate Lawson 
1282be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
12831e4925e8SNate Lawson     if ((devinfo->CurrentStatus & 0x9) == 0x9)
12841e4925e8SNate Lawson 	ret = TRUE;
1285be2b1797SNate Lawson 
12861e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
12871e4925e8SNate Lawson     return (ret);
1288c5ba0be4SMike Smith }
1289c5ba0be4SMike Smith 
1290c5ba0be4SMike Smith /*
1291b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
1292b53f2771SMike Smith  */
1293b53f2771SMike Smith BOOLEAN
1294b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
1295b53f2771SMike Smith {
12961e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1297b53f2771SMike Smith     ACPI_HANDLE		h;
12981e4925e8SNate Lawson     ACPI_BUFFER		buf;
1299b53f2771SMike Smith     ACPI_STATUS		error;
13001e4925e8SNate Lawson     int			ret;
1301b53f2771SMike Smith 
1302b53f2771SMike Smith     ACPI_ASSERTLOCK;
1303b53f2771SMike Smith 
13041e4925e8SNate Lawson     ret = FALSE;
1305b53f2771SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1306b53f2771SMike Smith 	return (FALSE);
13071e4925e8SNate Lawson     buf.Pointer = NULL;
13081e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
13096fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
13106fca9360SNate Lawson     if (ACPI_FAILURE(error))
1311b53f2771SMike Smith 	return (FALSE);
13121e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1313be2b1797SNate Lawson 
1314be2b1797SNate Lawson     /* If no _STA method, must be present */
13151e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
13161e4925e8SNate Lawson 	ret = TRUE;
1317be2b1797SNate Lawson 
1318be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
13191e4925e8SNate Lawson     if ((devinfo->CurrentStatus & 0x19) == 0x19)
13201e4925e8SNate Lawson 	ret = TRUE;
1321be2b1797SNate Lawson 
13221e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
13231e4925e8SNate Lawson     return (ret);
1324b53f2771SMike Smith }
1325b53f2771SMike Smith 
1326b53f2771SMike Smith /*
132715e32d5dSMike Smith  * Match a HID string against a device
132815e32d5dSMike Smith  */
132915e32d5dSMike Smith BOOLEAN
133015e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid)
133115e32d5dSMike Smith {
13321e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
133315e32d5dSMike Smith     ACPI_HANDLE		h;
13341e4925e8SNate Lawson     ACPI_BUFFER		buf;
133515e32d5dSMike Smith     ACPI_STATUS		error;
13361e4925e8SNate Lawson     int			ret, i;
133715e32d5dSMike Smith 
1338cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1339cb9b0d80SMike Smith 
13401e4925e8SNate Lawson     ret = FALSE;
13414d332989SMike Smith     if (hid == NULL)
134215e32d5dSMike Smith 	return (FALSE);
134315e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
134415e32d5dSMike Smith 	return (FALSE);
13451e4925e8SNate Lawson     buf.Pointer = NULL;
13461e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
13476fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
13486fca9360SNate Lawson     if (ACPI_FAILURE(error))
134915e32d5dSMike Smith 	return (FALSE);
13501e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1351be2b1797SNate Lawson 
1352c59c9a8eSJohn Baldwin     if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
1353c59c9a8eSJohn Baldwin 	strcmp(hid, devinfo->HardwareId.Value) == 0)
13541e4925e8SNate Lawson 	    ret = TRUE;
1355c59c9a8eSJohn Baldwin     else if ((devinfo->Valid & ACPI_VALID_CID) != 0) {
13561e4925e8SNate Lawson 	for (i = 0; i < devinfo->CompatibilityId.Count; i++) {
13571e4925e8SNate Lawson 	    if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) {
13581e4925e8SNate Lawson 		ret = TRUE;
13591e4925e8SNate Lawson 		break;
13601e4925e8SNate Lawson 	    }
13611e4925e8SNate Lawson 	}
13621e4925e8SNate Lawson     }
1363be2b1797SNate Lawson 
13641e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
13651e4925e8SNate Lawson     return (ret);
136615e32d5dSMike Smith }
136715e32d5dSMike Smith 
136815e32d5dSMike Smith /*
1369c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1370c5ba0be4SMike Smith  * or one if its parents.
1371c5ba0be4SMike Smith  */
1372c5ba0be4SMike Smith ACPI_STATUS
1373c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1374c5ba0be4SMike Smith {
1375c5ba0be4SMike Smith     ACPI_HANDLE		r;
1376c5ba0be4SMike Smith     ACPI_STATUS		status;
1377c5ba0be4SMike Smith 
1378cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1379cb9b0d80SMike Smith 
1380be2b1797SNate Lawson     /* Walk back up the tree to the root */
1381c5ba0be4SMike Smith     for (;;) {
1382be2b1797SNate Lawson 	status = AcpiGetHandle(parent, path, &r);
1383be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
1384c5ba0be4SMike Smith 	    *result = r;
1385c5ba0be4SMike Smith 	    return (AE_OK);
1386c5ba0be4SMike Smith 	}
1387c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1388c5ba0be4SMike Smith 	    return (AE_OK);
1389b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1390c5ba0be4SMike Smith 	    return (AE_NOT_FOUND);
1391c5ba0be4SMike Smith 	parent = r;
1392c5ba0be4SMike Smith     }
1393c5ba0be4SMike Smith }
1394c5ba0be4SMike Smith 
1395c5ba0be4SMike Smith /*
1396c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1397c5ba0be4SMike Smith  */
1398c5ba0be4SMike Smith ACPI_BUFFER *
1399c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1400c5ba0be4SMike Smith {
1401c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1402c5ba0be4SMike Smith 
1403c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1404c5ba0be4SMike Smith 	return (NULL);
1405c5ba0be4SMike Smith     buf->Length = size;
1406c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1407c5ba0be4SMike Smith     return (buf);
1408c5ba0be4SMike Smith }
1409c5ba0be4SMike Smith 
1410c310653eSNate Lawson ACPI_STATUS
1411cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
1412c310653eSNate Lawson {
1413c310653eSNate Lawson     ACPI_OBJECT arg1;
1414c310653eSNate Lawson     ACPI_OBJECT_LIST args;
1415c310653eSNate Lawson 
1416c310653eSNate Lawson     ACPI_ASSERTLOCK;
1417c310653eSNate Lawson 
1418c310653eSNate Lawson     arg1.Type = ACPI_TYPE_INTEGER;
1419c310653eSNate Lawson     arg1.Integer.Value = number;
1420c310653eSNate Lawson     args.Count = 1;
1421c310653eSNate Lawson     args.Pointer = &arg1;
1422c310653eSNate Lawson 
1423c310653eSNate Lawson     return (AcpiEvaluateObject(handle, path, &args, NULL));
1424c310653eSNate Lawson }
1425c310653eSNate Lawson 
1426c5ba0be4SMike Smith /*
1427c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1428c5ba0be4SMike Smith  */
1429c5ba0be4SMike Smith ACPI_STATUS
1430cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
1431c5ba0be4SMike Smith {
1432be2b1797SNate Lawson     ACPI_STATUS	status;
1433c5ba0be4SMike Smith     ACPI_BUFFER	buf;
1434c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
1435c5ba0be4SMike Smith 
1436cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1437cb9b0d80SMike Smith 
1438c5ba0be4SMike Smith     if (handle == NULL)
1439c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
14400c7da7acSJohn Baldwin 
14410c7da7acSJohn Baldwin     /*
14420c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
14430c7da7acSJohn Baldwin      * a method that will return an Integer.
14440c7da7acSJohn Baldwin      */
1445c5ba0be4SMike Smith     buf.Pointer = &param;
1446c5ba0be4SMike Smith     buf.Length = sizeof(param);
1447be2b1797SNate Lawson     status = AcpiEvaluateObject(handle, path, NULL, &buf);
1448be2b1797SNate Lawson     if (ACPI_SUCCESS(status)) {
1449be2b1797SNate Lawson 	if (param.Type == ACPI_TYPE_INTEGER)
1450c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1451be2b1797SNate Lawson 	else
1452be2b1797SNate Lawson 	    status = AE_TYPE;
1453c5ba0be4SMike Smith     }
14540c7da7acSJohn Baldwin 
14550c7da7acSJohn Baldwin     /*
14560c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
14570c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
14580c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
14590c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
14600c7da7acSJohn Baldwin      *
14610c7da7acSJohn Baldwin      * This is a hack.
14620c7da7acSJohn Baldwin      */
1463be2b1797SNate Lawson     if (status == AE_BUFFER_OVERFLOW) {
1464b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1465be2b1797SNate Lawson 	    status = AE_NO_MEMORY;
14660c7da7acSJohn Baldwin 	} else {
1467be2b1797SNate Lawson 	    status = AcpiEvaluateObject(handle, path, NULL, &buf);
1468be2b1797SNate Lawson 	    if (ACPI_SUCCESS(status))
1469be2b1797SNate Lawson 		status = acpi_ConvertBufferToInteger(&buf, number);
14700c7da7acSJohn Baldwin 	    AcpiOsFree(buf.Pointer);
14710c7da7acSJohn Baldwin 	}
1472f97739daSNate Lawson     }
1473be2b1797SNate Lawson     return (status);
1474c5ba0be4SMike Smith }
1475c5ba0be4SMike Smith 
1476c573e654SMitsuru IWASAKI ACPI_STATUS
1477cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
1478c573e654SMitsuru IWASAKI {
1479c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
1480dba55fa2SNate Lawson     UINT8	*val;
1481c573e654SMitsuru IWASAKI     int		i;
1482c573e654SMitsuru IWASAKI 
1483c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
1484c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
1485c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
1486c573e654SMitsuru IWASAKI 	return (AE_OK);
1487c573e654SMitsuru IWASAKI     }
1488c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
1489c573e654SMitsuru IWASAKI 	return (AE_TYPE);
1490c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
1491c573e654SMitsuru IWASAKI 	return (AE_BAD_DATA);
1492be2b1797SNate Lawson 
1493c573e654SMitsuru IWASAKI     *number = 0;
1494dba55fa2SNate Lawson     val = p->Buffer.Pointer;
1495c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
1496dba55fa2SNate Lawson 	*number += val[i] << (i * 8);
1497c573e654SMitsuru IWASAKI     return (AE_OK);
1498c573e654SMitsuru IWASAKI }
1499c573e654SMitsuru IWASAKI 
1500c5ba0be4SMike Smith /*
1501c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1502c5ba0be4SMike Smith  * function for each element.
1503c5ba0be4SMike Smith  *
1504c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1505c5ba0be4SMike Smith  */
1506c5ba0be4SMike Smith ACPI_STATUS
1507be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
1508be2b1797SNate Lawson 	void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
1509c5ba0be4SMike Smith {
1510c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1511c5ba0be4SMike Smith     int		i;
1512c5ba0be4SMike Smith 
1513be2b1797SNate Lawson     if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1514c5ba0be4SMike Smith 	return (AE_BAD_PARAMETER);
1515c5ba0be4SMike Smith 
1516be2b1797SNate Lawson     /* Iterate over components */
1517be2b1797SNate Lawson     i = 0;
1518be2b1797SNate Lawson     comp = pkg->Package.Elements;
1519be2b1797SNate Lawson     for (; i < pkg->Package.Count; i++, comp++)
1520c5ba0be4SMike Smith 	func(comp, arg);
1521c5ba0be4SMike Smith 
1522c5ba0be4SMike Smith     return (AE_OK);
1523c5ba0be4SMike Smith }
1524c5ba0be4SMike Smith 
15256f69255bSMike Smith /*
15266f69255bSMike Smith  * Find the (index)th resource object in a set.
15276f69255bSMike Smith  */
15286f69255bSMike Smith ACPI_STATUS
15296d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
15306f69255bSMike Smith {
15316d63101aSMike Smith     ACPI_RESOURCE	*rp;
15326f69255bSMike Smith     int			i;
15336f69255bSMike Smith 
15346d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
15356f69255bSMike Smith     i = index;
15366d63101aSMike Smith     while (i-- > 0) {
1537be2b1797SNate Lawson 	/* Range check */
15386d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
15396f69255bSMike Smith 	    return (AE_BAD_PARAMETER);
1540be2b1797SNate Lawson 
1541be2b1797SNate Lawson 	/* Check for terminator */
1542be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
15436d63101aSMike Smith 	    return (AE_NOT_FOUND);
1544abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
15456f69255bSMike Smith     }
15466f69255bSMike Smith     if (resp != NULL)
15476d63101aSMike Smith 	*resp = rp;
1548be2b1797SNate Lawson 
15496d63101aSMike Smith     return (AE_OK);
15506d63101aSMike Smith }
15516d63101aSMike Smith 
15526d63101aSMike Smith /*
15536d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
15546d63101aSMike Smith  *
15556d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
15566d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
15576d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
15586d63101aSMike Smith  * resources.
15596d63101aSMike Smith  */
15606d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
15616d63101aSMike Smith 
15626d63101aSMike Smith ACPI_STATUS
15636d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
15646d63101aSMike Smith {
15656d63101aSMike Smith     ACPI_RESOURCE	*rp;
15666d63101aSMike Smith     void		*newp;
15676d63101aSMike Smith 
1568be2b1797SNate Lawson     /* Initialise the buffer if necessary. */
15696d63101aSMike Smith     if (buf->Pointer == NULL) {
15706d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
15716d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
15726d63101aSMike Smith 	    return (AE_NO_MEMORY);
15736d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
15746d63101aSMike Smith 	rp->Id = ACPI_RSTYPE_END_TAG;
15756d63101aSMike Smith 	rp->Length = 0;
15766d63101aSMike Smith     }
15776d63101aSMike Smith     if (res == NULL)
15786d63101aSMike Smith 	return (AE_OK);
15796d63101aSMike Smith 
15806d63101aSMike Smith     /*
15816d63101aSMike Smith      * Scan the current buffer looking for the terminator.
15826d63101aSMike Smith      * This will either find the terminator or hit the end
15836d63101aSMike Smith      * of the buffer and return an error.
15846d63101aSMike Smith      */
15856d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
15866d63101aSMike Smith     for (;;) {
1587be2b1797SNate Lawson 	/* Range check, don't go outside the buffer */
15886d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
15896d63101aSMike Smith 	    return (AE_BAD_PARAMETER);
1590be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
15916d63101aSMike Smith 	    break;
1592abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
15936d63101aSMike Smith     }
15946d63101aSMike Smith 
15956d63101aSMike Smith     /*
15966d63101aSMike Smith      * Check the size of the buffer and expand if required.
15976d63101aSMike Smith      *
15986d63101aSMike Smith      * Required size is:
15996d63101aSMike Smith      *	size of existing resources before terminator +
16006d63101aSMike Smith      *	size of new resource and header +
16016d63101aSMike Smith      * 	size of terminator.
16026d63101aSMike Smith      *
16036d63101aSMike Smith      * Note that this loop should really only run once, unless
16046d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
16056d63101aSMike Smith      */
16066d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
16076d63101aSMike Smith 	    res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
16086d63101aSMike Smith 	    ACPI_RESOURCE_LENGTH) >= buf->Length) {
16096d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
16106d63101aSMike Smith 	    return (AE_NO_MEMORY);
16116d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
1612a692219dSMike Smith 	rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1613a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
16146d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
16156d63101aSMike Smith 	buf->Pointer = newp;
16166d63101aSMike Smith 	buf->Length += buf->Length;
16176d63101aSMike Smith     }
16186d63101aSMike Smith 
1619be2b1797SNate Lawson     /* Insert the new resource. */
16206d63101aSMike Smith     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
16216d63101aSMike Smith 
1622be2b1797SNate Lawson     /* And add the terminator. */
1623abcbc5bcSNate Lawson     rp = ACPI_NEXT_RESOURCE(rp);
16246d63101aSMike Smith     rp->Id = ACPI_RSTYPE_END_TAG;
16256d63101aSMike Smith     rp->Length = 0;
16266d63101aSMike Smith 
16276f69255bSMike Smith     return (AE_OK);
16286f69255bSMike Smith }
1629c5ba0be4SMike Smith 
1630da14ac9fSJohn Baldwin /*
1631da14ac9fSJohn Baldwin  * Set interrupt model.
1632da14ac9fSJohn Baldwin  */
1633da14ac9fSJohn Baldwin ACPI_STATUS
1634da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
1635da14ac9fSJohn Baldwin {
1636da14ac9fSJohn Baldwin 
1637c310653eSNate Lawson     return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
1638da14ac9fSJohn Baldwin }
1639da14ac9fSJohn Baldwin 
1640ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME	5
1641ece50487SMitsuru IWASAKI 
1642ece50487SMitsuru IWASAKI static void
1643ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
1644ece50487SMitsuru IWASAKI {
1645ece50487SMitsuru IWASAKI     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1646ece50487SMitsuru IWASAKI }
1647c30382dfSMitsuru IWASAKI 
164815e32d5dSMike Smith /*
164915e32d5dSMike Smith  * Set the system sleep state
165015e32d5dSMike Smith  *
1651be2b1797SNate Lawson  * Currently we support S1-S5 but S4 is only S4BIOS
165215e32d5dSMike Smith  */
165315e32d5dSMike Smith ACPI_STATUS
165415e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
165515e32d5dSMike Smith {
165615e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
165756d8cb57SMitsuru IWASAKI     UINT8	TypeA;
165856d8cb57SMitsuru IWASAKI     UINT8	TypeB;
165915e32d5dSMike Smith 
1660b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1661cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
16620ae55423SMike Smith 
166398175614SNate Lawson     /* Avoid reentry if already attempting to suspend. */
16646397624dSMitsuru IWASAKI     if (sc->acpi_sstate != ACPI_STATE_S0)
166598175614SNate Lawson 	return_ACPI_STATUS (AE_BAD_PARAMETER);
16666397624dSMitsuru IWASAKI 
166798175614SNate Lawson     /* We recently woke up so don't suspend again for a while. */
1668ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1669ece50487SMitsuru IWASAKI 	return_ACPI_STATUS (AE_OK);
1670ece50487SMitsuru IWASAKI 
167115e32d5dSMike Smith     switch (state) {
167215e32d5dSMike Smith     case ACPI_STATE_S1:
16736161544cSTakanori Watanabe     case ACPI_STATE_S2:
16746161544cSTakanori Watanabe     case ACPI_STATE_S3:
16756161544cSTakanori Watanabe     case ACPI_STATE_S4:
1676be2b1797SNate Lawson 	status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB);
167798175614SNate Lawson 	if (status == AE_NOT_FOUND) {
167898175614SNate Lawson 	    device_printf(sc->acpi_dev,
167998175614SNate Lawson 			  "Sleep state S%d not supported by BIOS\n", state);
168098175614SNate Lawson 	    break;
168198175614SNate Lawson 	} else if (ACPI_FAILURE(status)) {
1682be2b1797SNate Lawson 	    device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n",
1683be2b1797SNate Lawson 			  AcpiFormatException(status));
168456d8cb57SMitsuru IWASAKI 	    break;
168556d8cb57SMitsuru IWASAKI 	}
168656d8cb57SMitsuru IWASAKI 
1687a1fccb47SMitsuru IWASAKI 	sc->acpi_sstate = state;
1688a1fccb47SMitsuru IWASAKI 	sc->acpi_sleep_disabled = 1;
1689a1fccb47SMitsuru IWASAKI 
1690be2b1797SNate Lawson 	/* Inform all devices that we are going to sleep. */
169115e32d5dSMike Smith 	if (DEVICE_SUSPEND(root_bus) != 0) {
169215e32d5dSMike Smith 	    /*
169315e32d5dSMike Smith 	     * Re-wake the system.
169415e32d5dSMike Smith 	     *
169515e32d5dSMike Smith 	     * XXX note that a better two-pass approach with a 'veto' pass
169615e32d5dSMike Smith 	     *     followed by a "real thing" pass would be better, but the
169715e32d5dSMike Smith 	     *     current bus interface does not provide for this.
169815e32d5dSMike Smith 	     */
169915e32d5dSMike Smith 	    DEVICE_RESUME(root_bus);
17000ae55423SMike Smith 	    return_ACPI_STATUS (AE_ERROR);
170115e32d5dSMike Smith 	}
1702b9c780d6SMitsuru IWASAKI 
1703be2b1797SNate Lawson 	status = AcpiEnterSleepStatePrep(state);
1704be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1705b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1706b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
1707b9c780d6SMitsuru IWASAKI 	    break;
1708b9c780d6SMitsuru IWASAKI 	}
1709b9c780d6SMitsuru IWASAKI 
1710be2b1797SNate Lawson 	if (sc->acpi_sleep_delay > 0)
1711ff01efb5SMitsuru IWASAKI 	    DELAY(sc->acpi_sleep_delay * 1000000);
1712ff01efb5SMitsuru IWASAKI 
17136161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
17146161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
17156161544cSTakanori Watanabe 
1716be2b1797SNate Lawson 	    /* AcpiEnterSleepState() may be incomplete, unlock if locked. */
171759ddeb18SNate Lawson 	    if (AcpiGbl_MutexInfo[ACPI_MTX_HARDWARE].OwnerId !=
171859ddeb18SNate Lawson 		ACPI_MUTEX_NOT_ACQUIRED) {
171959ddeb18SNate Lawson 
17206161544cSTakanori Watanabe 		AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
1721a1fccb47SMitsuru IWASAKI 	    }
17226161544cSTakanori Watanabe 
17236161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1724be2b1797SNate Lawson 	    if (state == ACPI_STATE_S4)
1725964679ceSMitsuru IWASAKI 		AcpiEnable();
17266161544cSTakanori Watanabe 	} else {
1727be2b1797SNate Lawson 	    status = AcpiEnterSleepState((UINT8)state);
1728be2b1797SNate Lawson 	    if (ACPI_FAILURE(status)) {
1729be2b1797SNate Lawson 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
1730be2b1797SNate Lawson 			      AcpiFormatException(status));
1731c30382dfSMitsuru IWASAKI 		break;
173215e32d5dSMike Smith 	    }
17336161544cSTakanori Watanabe 	}
1734ff741befSTakanori Watanabe 	AcpiLeaveSleepState((UINT8)state);
173515e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
173615e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
173713d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
173815e32d5dSMike Smith 	break;
173915e32d5dSMike Smith     case ACPI_STATE_S5:
174015e32d5dSMike Smith 	/*
174115e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
174215e32d5dSMike Smith 	 * shutdown handlers.
174315e32d5dSMike Smith 	 */
174415e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
174515e32d5dSMike Smith 	break;
174698175614SNate Lawson     case ACPI_STATE_S0:
174715e32d5dSMike Smith     default:
174815e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
174915e32d5dSMike Smith 	break;
175015e32d5dSMike Smith     }
1751ece50487SMitsuru IWASAKI 
175298175614SNate Lawson     /* Disable a second sleep request for a short period */
1753ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1754ece50487SMitsuru IWASAKI 	timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1755ece50487SMitsuru IWASAKI 
17560ae55423SMike Smith     return_ACPI_STATUS (status);
175715e32d5dSMike Smith }
175815e32d5dSMike Smith 
175915e32d5dSMike Smith /*
176015e32d5dSMike Smith  * Enable/Disable ACPI
176115e32d5dSMike Smith  */
176215e32d5dSMike Smith ACPI_STATUS
176315e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
176415e32d5dSMike Smith {
176515e32d5dSMike Smith     ACPI_STATUS	status;
176615e32d5dSMike Smith     u_int32_t	flags;
176715e32d5dSMike Smith 
1768b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1769cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
17700ae55423SMike Smith 
177115e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
177215e32d5dSMike Smith 	    ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
1773be2b1797SNate Lawson     if (!sc->acpi_enabled)
177415e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
1775be2b1797SNate Lawson     else
177615e32d5dSMike Smith 	status = AE_OK;
1777be2b1797SNate Lawson 
177815e32d5dSMike Smith     if (status == AE_OK)
177915e32d5dSMike Smith 	sc->acpi_enabled = 1;
1780be2b1797SNate Lawson 
17810ae55423SMike Smith     return_ACPI_STATUS (status);
178215e32d5dSMike Smith }
178315e32d5dSMike Smith 
178415e32d5dSMike Smith ACPI_STATUS
178515e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
178615e32d5dSMike Smith {
178715e32d5dSMike Smith     ACPI_STATUS	status;
178815e32d5dSMike Smith 
1789b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1790cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
17910ae55423SMike Smith 
1792be2b1797SNate Lawson     if (sc->acpi_enabled)
179315e32d5dSMike Smith 	status = AcpiDisable();
1794be2b1797SNate Lawson     else
179515e32d5dSMike Smith 	status = AE_OK;
1796be2b1797SNate Lawson 
179715e32d5dSMike Smith     if (status == AE_OK)
179815e32d5dSMike Smith 	sc->acpi_enabled = 0;
1799be2b1797SNate Lawson 
18000ae55423SMike Smith     return_ACPI_STATUS (status);
180115e32d5dSMike Smith }
180215e32d5dSMike Smith 
180315e32d5dSMike Smith /*
180415e32d5dSMike Smith  * ACPI Event Handlers
180515e32d5dSMike Smith  */
180615e32d5dSMike Smith 
180715e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
180815e32d5dSMike Smith 
180915e32d5dSMike Smith static void
181015e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
181115e32d5dSMike Smith {
1812fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1813b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
181415e32d5dSMike Smith 
1815cb9b0d80SMike Smith     ACPI_LOCK;
1816a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
181715e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
1818cb9b0d80SMike Smith     ACPI_UNLOCK;
18190ae55423SMike Smith     return_VOID;
182015e32d5dSMike Smith }
182115e32d5dSMike Smith 
182215e32d5dSMike Smith static void
182315e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
182415e32d5dSMike Smith {
1825fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1826b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
18270ae55423SMike Smith 
182815e32d5dSMike Smith     /* Well, what to do? :-) */
18290ae55423SMike Smith 
1830cb9b0d80SMike Smith     ACPI_LOCK;
1831cb9b0d80SMike Smith     ACPI_UNLOCK;
1832cb9b0d80SMike Smith 
18330ae55423SMike Smith     return_VOID;
183415e32d5dSMike Smith }
183515e32d5dSMike Smith 
183615e32d5dSMike Smith /*
183715e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
183815e32d5dSMike Smith  */
183915e32d5dSMike Smith UINT32
184033febf93SNate Lawson acpi_event_power_button_sleep(void *context)
184115e32d5dSMike Smith {
184215e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
184315e32d5dSMike Smith 
1844b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
18450ae55423SMike Smith 
184615e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
18470ae55423SMike Smith 
1848b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
184915e32d5dSMike Smith }
185015e32d5dSMike Smith 
185115e32d5dSMike Smith UINT32
185233febf93SNate Lawson acpi_event_power_button_wake(void *context)
185315e32d5dSMike Smith {
185415e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
185515e32d5dSMike Smith 
1856b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
18570ae55423SMike Smith 
185815e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
18590ae55423SMike Smith 
1860b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
186115e32d5dSMike Smith }
186215e32d5dSMike Smith 
186315e32d5dSMike Smith UINT32
186433febf93SNate Lawson acpi_event_sleep_button_sleep(void *context)
186515e32d5dSMike Smith {
186615e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
186715e32d5dSMike Smith 
1868b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
18690ae55423SMike Smith 
187015e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
18710ae55423SMike Smith 
1872b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
187315e32d5dSMike Smith }
187415e32d5dSMike Smith 
187515e32d5dSMike Smith UINT32
187633febf93SNate Lawson acpi_event_sleep_button_wake(void *context)
187715e32d5dSMike Smith {
187815e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
187915e32d5dSMike Smith 
1880b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
18810ae55423SMike Smith 
188215e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
18830ae55423SMike Smith 
1884b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
188515e32d5dSMike Smith }
188615e32d5dSMike Smith 
188715e32d5dSMike Smith /*
188815e32d5dSMike Smith  * XXX This is kinda ugly, and should not be here.
188915e32d5dSMike Smith  */
189015e32d5dSMike Smith struct acpi_staticbuf {
189115e32d5dSMike Smith     ACPI_BUFFER	buffer;
189215e32d5dSMike Smith     char	data[512];
189315e32d5dSMike Smith };
189415e32d5dSMike Smith 
189515e32d5dSMike Smith char *
189615e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
189715e32d5dSMike Smith {
189815e32d5dSMike Smith     static struct acpi_staticbuf	buf;
189915e32d5dSMike Smith 
1900cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1901cb9b0d80SMike Smith 
190215e32d5dSMike Smith     buf.buffer.Length = 512;
190315e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
190415e32d5dSMike Smith 
1905b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
190615e32d5dSMike Smith 	return (buf.buffer.Pointer);
1907be2b1797SNate Lawson 
190815e32d5dSMike Smith     return ("(unknown path)");
190915e32d5dSMike Smith }
191015e32d5dSMike Smith 
191115e32d5dSMike Smith /*
191215e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
191315e32d5dSMike Smith  * parts of the namespace.
191415e32d5dSMike Smith  */
191515e32d5dSMike Smith int
191615e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
191715e32d5dSMike Smith {
19183c600cfbSJohn Baldwin     char	*cp, *env, *np;
191915e32d5dSMike Smith     int		len;
192015e32d5dSMike Smith 
192115e32d5dSMike Smith     np = acpi_name(handle);
192215e32d5dSMike Smith     if (*np == '\\')
192315e32d5dSMike Smith 	np++;
19243c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
192515e32d5dSMike Smith 	return (0);
192615e32d5dSMike Smith 
1927be2b1797SNate Lawson     /* Scan the avoid list checking for a match */
19283c600cfbSJohn Baldwin     cp = env;
192915e32d5dSMike Smith     for (;;) {
193015e32d5dSMike Smith 	while ((*cp != 0) && isspace(*cp))
193115e32d5dSMike Smith 	    cp++;
193215e32d5dSMike Smith 	if (*cp == 0)
193315e32d5dSMike Smith 	    break;
193415e32d5dSMike Smith 	len = 0;
193515e32d5dSMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
193615e32d5dSMike Smith 	    len++;
1937d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
19383c600cfbSJohn Baldwin 	    freeenv(env);
19390ae55423SMike Smith 	    return(1);
1940d786139cSMaxime Henrion 	}
19410ae55423SMike Smith 	cp += len;
19420ae55423SMike Smith     }
19433c600cfbSJohn Baldwin     freeenv(env);
1944be2b1797SNate Lawson 
19450ae55423SMike Smith     return (0);
19460ae55423SMike Smith }
19470ae55423SMike Smith 
19480ae55423SMike Smith /*
19490ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
19500ae55423SMike Smith  */
19510ae55423SMike Smith int
19520ae55423SMike Smith acpi_disabled(char *subsys)
19530ae55423SMike Smith {
195478689b15SMaxime Henrion     char	*cp, *env;
19550ae55423SMike Smith     int		len;
19560ae55423SMike Smith 
19573184cf5aSNate Lawson     if ((env = getenv("debug.acpi.disabled")) == NULL)
19580ae55423SMike Smith 	return (0);
19593184cf5aSNate Lawson     if (strcmp(env, "all") == 0) {
196078689b15SMaxime Henrion 	freeenv(env);
19610ae55423SMike Smith 	return (1);
1962d786139cSMaxime Henrion     }
19630ae55423SMike Smith 
19643184cf5aSNate Lawson     /* Scan the disable list, checking for a match. */
196578689b15SMaxime Henrion     cp = env;
19660ae55423SMike Smith     for (;;) {
19673184cf5aSNate Lawson 	while (*cp != '\0' && isspace(*cp))
19680ae55423SMike Smith 	    cp++;
19693184cf5aSNate Lawson 	if (*cp == '\0')
19700ae55423SMike Smith 	    break;
19710ae55423SMike Smith 	len = 0;
19723184cf5aSNate Lawson 	while (cp[len] != '\0' && !isspace(cp[len]))
19730ae55423SMike Smith 	    len++;
19743184cf5aSNate Lawson 	if (strncmp(cp, subsys, len) == 0) {
197578689b15SMaxime Henrion 	    freeenv(env);
197615e32d5dSMike Smith 	    return (1);
1977d786139cSMaxime Henrion 	}
197815e32d5dSMike Smith 	cp += len;
197915e32d5dSMike Smith     }
198078689b15SMaxime Henrion     freeenv(env);
1981be2b1797SNate Lawson 
198215e32d5dSMike Smith     return (0);
198315e32d5dSMike Smith }
198415e32d5dSMike Smith 
198515e32d5dSMike Smith /*
1986a1fccb47SMitsuru IWASAKI  * Device wake capability enable/disable.
1987a1fccb47SMitsuru IWASAKI  */
1988a1fccb47SMitsuru IWASAKI void
1989a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable)
1990a1fccb47SMitsuru IWASAKI {
1991a1fccb47SMitsuru IWASAKI     /*
1992a1fccb47SMitsuru IWASAKI      * TBD: All Power Resources referenced by elements 2 through N
1993a1fccb47SMitsuru IWASAKI      *      of the _PRW object are put into the ON state.
1994a1fccb47SMitsuru IWASAKI      */
1995a1fccb47SMitsuru IWASAKI 
1996c310653eSNate Lawson     (void)acpi_SetInteger(h, "_PSW", enable);
1997a1fccb47SMitsuru IWASAKI }
1998a1fccb47SMitsuru IWASAKI 
1999a1fccb47SMitsuru IWASAKI void
2000a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_event(ACPI_HANDLE h)
2001a1fccb47SMitsuru IWASAKI {
2002a1fccb47SMitsuru IWASAKI     struct acpi_softc		*sc;
2003a1fccb47SMitsuru IWASAKI     ACPI_STATUS			status;
2004a1fccb47SMitsuru IWASAKI     ACPI_BUFFER			prw_buffer;
2005a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			*res;
2006a1fccb47SMitsuru IWASAKI 
2007a1fccb47SMitsuru IWASAKI     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2008a1fccb47SMitsuru IWASAKI 
2009be2b1797SNate Lawson     sc = devclass_get_softc(acpi_devclass, 0);
2010be2b1797SNate Lawson     if (sc == NULL)
2011a1fccb47SMitsuru IWASAKI 	return;
2012a1fccb47SMitsuru IWASAKI 
2013a1fccb47SMitsuru IWASAKI     /*
2014a1fccb47SMitsuru IWASAKI      * _PRW object is only required for devices that have the ability
2015a1fccb47SMitsuru IWASAKI      * to wake the system from a system sleeping state.
2016a1fccb47SMitsuru IWASAKI      */
2017a1fccb47SMitsuru IWASAKI     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
2018a1fccb47SMitsuru IWASAKI     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
2019be2b1797SNate Lawson     if (ACPI_FAILURE(status))
2020a1fccb47SMitsuru IWASAKI 	return;
2021a1fccb47SMitsuru IWASAKI 
2022a1fccb47SMitsuru IWASAKI     res = (ACPI_OBJECT *)prw_buffer.Pointer;
2023be2b1797SNate Lawson     if (res == NULL)
2024a1fccb47SMitsuru IWASAKI 	return;
2025a1fccb47SMitsuru IWASAKI 
2026a1fccb47SMitsuru IWASAKI     if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) {
2027a1fccb47SMitsuru IWASAKI 	goto out;
2028a1fccb47SMitsuru IWASAKI     }
2029a1fccb47SMitsuru IWASAKI 
2030a1fccb47SMitsuru IWASAKI     /*
2031a1fccb47SMitsuru IWASAKI      * The element 1 of the _PRW object:
2032a1fccb47SMitsuru IWASAKI      * The lowest power system sleeping state that can be entered
2033a1fccb47SMitsuru IWASAKI      * while still providing wake functionality.
2034a1fccb47SMitsuru IWASAKI      * The sleeping state being entered must be greater or equal to
2035a1fccb47SMitsuru IWASAKI      * the power state declared in element 1 of the _PRW object.
2036a1fccb47SMitsuru IWASAKI      */
2037be2b1797SNate Lawson     if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER)
2038a1fccb47SMitsuru IWASAKI 	goto out;
2039a1fccb47SMitsuru IWASAKI 
2040be2b1797SNate Lawson     if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value)
2041a1fccb47SMitsuru IWASAKI 	goto out;
2042a1fccb47SMitsuru IWASAKI 
2043a1fccb47SMitsuru IWASAKI     /*
2044a1fccb47SMitsuru IWASAKI      * The element 0 of the _PRW object:
2045a1fccb47SMitsuru IWASAKI      */
2046a1fccb47SMitsuru IWASAKI     switch(res->Package.Elements[0].Type) {
2047a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_INTEGER:
2048a1fccb47SMitsuru IWASAKI 	/*
2049a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is numeric, then this
2050a1fccb47SMitsuru IWASAKI 	 * _PRW package element is the bit index in the GPEx_EN, in the
2051a1fccb47SMitsuru IWASAKI 	 * GPE blocks described in the FADT, of the enable bit that is
2052a1fccb47SMitsuru IWASAKI 	 * enabled for the wake event.
2053a1fccb47SMitsuru IWASAKI 	 */
2054a1fccb47SMitsuru IWASAKI 
20556fca9360SNate Lawson 	status = AcpiEnableGpe(NULL, res->Package.Elements[0].Integer.Value,
20566fca9360SNate Lawson 			       ACPI_EVENT_WAKE_ENABLE);
2057a1fccb47SMitsuru IWASAKI 	if (ACPI_FAILURE(status))
2058a1fccb47SMitsuru IWASAKI 	    printf("%s: EnableEvent Failed\n", __func__);
2059a1fccb47SMitsuru IWASAKI 	break;
2060a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_PACKAGE:
2061a1fccb47SMitsuru IWASAKI 	/*
2062be2b1797SNate Lawson 	 * XXX TBD
2063be2b1797SNate Lawson 	 *
2064a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is a package, then this
2065a1fccb47SMitsuru IWASAKI 	 * _PRW package element is itself a package containing two
2066a1fccb47SMitsuru IWASAKI 	 * elements. The first is an object reference to the GPE Block
2067a1fccb47SMitsuru IWASAKI 	 * device that contains the GPE that will be triggered by the wake
2068a1fccb47SMitsuru IWASAKI 	 * event. The second element is numeric and it contains the bit
2069a1fccb47SMitsuru IWASAKI 	 * index in the GPEx_EN, in the GPE Block referenced by the
2070a1fccb47SMitsuru IWASAKI 	 * first element in the package, of the enable bit that is enabled for
2071a1fccb47SMitsuru IWASAKI 	 * the wake event.
2072a1fccb47SMitsuru IWASAKI 	 * For example, if this field is a package then it is of the form:
2073a1fccb47SMitsuru IWASAKI 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
2074a1fccb47SMitsuru IWASAKI 	 */
2075a1fccb47SMitsuru IWASAKI 	break;
2076a1fccb47SMitsuru IWASAKI     default:
2077a1fccb47SMitsuru IWASAKI 	break;
2078a1fccb47SMitsuru IWASAKI     }
2079a1fccb47SMitsuru IWASAKI 
2080a1fccb47SMitsuru IWASAKI out:
2081a1fccb47SMitsuru IWASAKI     if (prw_buffer.Pointer != NULL)
2082a1fccb47SMitsuru IWASAKI 	AcpiOsFree(prw_buffer.Pointer);
2083a1fccb47SMitsuru IWASAKI }
2084a1fccb47SMitsuru IWASAKI 
2085a1fccb47SMitsuru IWASAKI /*
208615e32d5dSMike Smith  * Control interface.
208715e32d5dSMike Smith  *
20880ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
2089be2b1797SNate Lawson  * drivers wanting to be accessible via /dev/acpi should use the
2090be2b1797SNate Lawson  * register/deregister interface to make their handlers visible.
209115e32d5dSMike Smith  */
20920ae55423SMike Smith struct acpi_ioctl_hook
20930ae55423SMike Smith {
20940ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook) link;
20950ae55423SMike Smith     u_long			 cmd;
2096be2b1797SNate Lawson     acpi_ioctl_fn		 fn;
20970ae55423SMike Smith     void			 *arg;
20980ae55423SMike Smith };
20990ae55423SMike Smith 
21000ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
21010ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
21020ae55423SMike Smith 
21030ae55423SMike Smith /*
21040ae55423SMike Smith  * Register an ioctl handler.
21050ae55423SMike Smith  */
21060ae55423SMike Smith int
2107be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
21080ae55423SMike Smith {
21090ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
21100ae55423SMike Smith 
21110ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
21120ae55423SMike Smith 	return (ENOMEM);
21130ae55423SMike Smith     hp->cmd = cmd;
21140ae55423SMike Smith     hp->fn = fn;
21150ae55423SMike Smith     hp->arg = arg;
21160ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
21170ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
21180ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
21190ae55423SMike Smith     }
21200ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
21210ae55423SMike Smith     return (0);
21220ae55423SMike Smith }
21230ae55423SMike Smith 
21240ae55423SMike Smith /*
21250ae55423SMike Smith  * Deregister an ioctl handler.
21260ae55423SMike Smith  */
21270ae55423SMike Smith void
2128be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
21290ae55423SMike Smith {
21300ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
21310ae55423SMike Smith 
21320ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
21330ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
21340ae55423SMike Smith 	    break;
21350ae55423SMike Smith 
21360ae55423SMike Smith     if (hp != NULL) {
21370ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
21380ae55423SMike Smith 	free(hp, M_ACPIDEV);
21390ae55423SMike Smith     }
21400ae55423SMike Smith }
21410ae55423SMike Smith 
214215e32d5dSMike Smith static int
21436b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td)
214415e32d5dSMike Smith {
214515e32d5dSMike Smith     return (0);
214615e32d5dSMike Smith }
214715e32d5dSMike Smith 
214815e32d5dSMike Smith static int
21496b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td)
215015e32d5dSMike Smith {
215115e32d5dSMike Smith     return (0);
215215e32d5dSMike Smith }
215315e32d5dSMike Smith 
215415e32d5dSMike Smith static int
21556b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
215615e32d5dSMike Smith {
215715e32d5dSMike Smith     struct acpi_softc		*sc;
21580ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
21590ae55423SMike Smith     int				error, xerror, state;
2160fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
216115e32d5dSMike Smith 
2162cb9b0d80SMike Smith     ACPI_LOCK;
2163cb9b0d80SMike Smith 
216415e32d5dSMike Smith     error = state = 0;
216515e32d5dSMike Smith     sc = dev->si_drv1;
216615e32d5dSMike Smith 
21670ae55423SMike Smith     /*
21680ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
21690ae55423SMike Smith      */
21700ae55423SMike Smith     if (acpi_ioctl_hooks_initted) {
21710ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
21720ae55423SMike Smith 	    if (hp->cmd == cmd) {
21730ae55423SMike Smith 		xerror = hp->fn(cmd, addr, hp->arg);
21740ae55423SMike Smith 		if (xerror != 0)
21750ae55423SMike Smith 		    error = xerror;
2176917d44c8SMitsuru IWASAKI 		goto out;
21770ae55423SMike Smith 	    }
21780ae55423SMike Smith 	}
21790ae55423SMike Smith     }
21800ae55423SMike Smith 
21810ae55423SMike Smith     /*
2182a89fcf28STakanori Watanabe      * Core ioctls are not permitted for non-writable user.
2183a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
2184a89fcf28STakanori Watanabe      * Not changing system behavior.
2185a89fcf28STakanori Watanabe      */
2186be2b1797SNate Lawson     if((flag & FWRITE) == 0)
2187be2b1797SNate Lawson 	return (EPERM);
2188a89fcf28STakanori Watanabe 
2189be2b1797SNate Lawson     /* Core system ioctls. */
219015e32d5dSMike Smith     switch (cmd) {
219115e32d5dSMike Smith     case ACPIIO_ENABLE:
21920ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
219315e32d5dSMike Smith 	    error = ENXIO;
219415e32d5dSMike Smith 	break;
219515e32d5dSMike Smith     case ACPIIO_DISABLE:
21960ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
219715e32d5dSMike Smith 	    error = ENXIO;
219815e32d5dSMike Smith 	break;
219915e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
220015e32d5dSMike Smith 	if (!sc->acpi_enabled) {
220115e32d5dSMike Smith 	    error = ENXIO;
220215e32d5dSMike Smith 	    break;
220315e32d5dSMike Smith 	}
220415e32d5dSMike Smith 	state = *(int *)addr;
22052d610c46SNate Lawson 	if (state >= ACPI_STATE_S0  && state <= ACPI_S_STATES_MAX) {
22062d610c46SNate Lawson 	    if (ACPI_FAILURE(acpi_SetSleepState(sc, state)))
220715e32d5dSMike Smith 		error = EINVAL;
22082d610c46SNate Lawson 	} else {
22092d610c46SNate Lawson 	    error = EINVAL;
22102d610c46SNate Lawson 	}
221115e32d5dSMike Smith 	break;
221215e32d5dSMike Smith     default:
22130ae55423SMike Smith 	if (error == 0)
221415e32d5dSMike Smith 	    error = EINVAL;
221515e32d5dSMike Smith 	break;
221615e32d5dSMike Smith     }
2217917d44c8SMitsuru IWASAKI 
2218917d44c8SMitsuru IWASAKI out:
2219cb9b0d80SMike Smith     ACPI_UNLOCK;
222015e32d5dSMike Smith     return (error);
222115e32d5dSMike Smith }
222215e32d5dSMike Smith 
22231d073b1dSJohn Baldwin static int
2224d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2225d75de536SMitsuru IWASAKI {
2226d75de536SMitsuru IWASAKI     char sleep_state[4];
2227d75de536SMitsuru IWASAKI     char buf[16];
2228d75de536SMitsuru IWASAKI     int error;
2229d75de536SMitsuru IWASAKI     UINT8 state, TypeA, TypeB;
2230d75de536SMitsuru IWASAKI 
2231d75de536SMitsuru IWASAKI     buf[0] = '\0';
2232d75de536SMitsuru IWASAKI     for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX+1; state++) {
2233d75de536SMitsuru IWASAKI 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
2234d75de536SMitsuru IWASAKI 	    sprintf(sleep_state, "S%d ", state);
2235d75de536SMitsuru IWASAKI 	    strcat(buf, sleep_state);
2236d75de536SMitsuru IWASAKI 	}
2237d75de536SMitsuru IWASAKI     }
2238d75de536SMitsuru IWASAKI     error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2239d75de536SMitsuru IWASAKI     return (error);
2240d75de536SMitsuru IWASAKI }
2241d75de536SMitsuru IWASAKI 
2242d75de536SMitsuru IWASAKI static int
22431d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
22441d073b1dSJohn Baldwin {
22451d073b1dSJohn Baldwin     char sleep_state[10];
22461d073b1dSJohn Baldwin     int error;
22471d073b1dSJohn Baldwin     u_int new_state, old_state;
22481d073b1dSJohn Baldwin 
22491d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
2250b8670bedSMitsuru IWASAKI     if (old_state > ACPI_S_STATES_MAX+1) {
22511d073b1dSJohn Baldwin 	strcpy(sleep_state, "unknown");
2252cb9b0d80SMike Smith     } else {
2253b8670bedSMitsuru IWASAKI 	bzero(sleep_state, sizeof(sleep_state));
22541d073b1dSJohn Baldwin 	strncpy(sleep_state, sleep_state_names[old_state],
22551d073b1dSJohn Baldwin 		sizeof(sleep_state_names[old_state]));
2256cb9b0d80SMike Smith     }
22571d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
22581d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
2259be2b1797SNate Lawson 	new_state = ACPI_STATE_S0;
2260be2b1797SNate Lawson 	for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) {
22611d073b1dSJohn Baldwin 	    if (strncmp(sleep_state, sleep_state_names[new_state],
22621d073b1dSJohn Baldwin 			sizeof(sleep_state)) == 0)
22631d073b1dSJohn Baldwin 		break;
2264cb9b0d80SMike Smith 	}
2265931a10c9SMitsuru IWASAKI 	if (new_state <= ACPI_S_STATES_MAX + 1) {
2266be2b1797SNate Lawson 	    if (new_state != old_state)
22671d073b1dSJohn Baldwin 		*(u_int *)oidp->oid_arg1 = new_state;
2268cb9b0d80SMike Smith 	} else {
22691d073b1dSJohn Baldwin 	    error = EINVAL;
22701d073b1dSJohn Baldwin 	}
2271cb9b0d80SMike Smith     }
2272be2b1797SNate Lawson 
22731d073b1dSJohn Baldwin     return (error);
22741d073b1dSJohn Baldwin }
22751d073b1dSJohn Baldwin 
22769b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */
22779b937d48SNate Lawson void
22789b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
22799b937d48SNate Lawson {
22809b937d48SNate Lawson     char		notify_buf[16];
22819b937d48SNate Lawson     ACPI_BUFFER		handle_buf;
22829b937d48SNate Lawson     ACPI_STATUS		status;
22839b937d48SNate Lawson 
22849b937d48SNate Lawson     if (subsystem == NULL)
22859b937d48SNate Lawson 	return;
22869b937d48SNate Lawson 
22879b937d48SNate Lawson     handle_buf.Pointer = NULL;
22889b937d48SNate Lawson     handle_buf.Length = ACPI_ALLOCATE_BUFFER;
22899b937d48SNate Lawson     status = AcpiNsHandleToPathname(h, &handle_buf);
22909b937d48SNate Lawson     if (ACPI_FAILURE(status))
22919b937d48SNate Lawson 	return;
22929b937d48SNate Lawson     snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
22939b937d48SNate Lawson     devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
22949b937d48SNate Lawson     AcpiOsFree(handle_buf.Pointer);
22959b937d48SNate Lawson }
22969b937d48SNate Lawson 
229715e32d5dSMike Smith #ifdef ACPI_DEBUG
22980ae55423SMike Smith /*
22990ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
23000ae55423SMike Smith  *
23010ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
23020ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
23030ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
23040ae55423SMike Smith  * prefixing the bit name with !.
23050ae55423SMike Smith  */
230615e32d5dSMike Smith struct debugtag
230715e32d5dSMike Smith {
230815e32d5dSMike Smith     char	*name;
230915e32d5dSMike Smith     UINT32	value;
231015e32d5dSMike Smith };
231115e32d5dSMike Smith 
231215e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
2313c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
2314c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
2315c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
2316c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
2317c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
2318c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
2319c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
2320c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
2321c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
2322d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
23234c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
2324d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
2325297835bcSNate Lawson     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
23264c1cdee6SMike Smith 
2327c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
2328c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
23293184cf5aSNate Lawson     {"ACPI_BUS",		ACPI_BUS},
2330c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
23313184cf5aSNate Lawson     {"ACPI_EC", 		ACPI_EC},
23323184cf5aSNate Lawson     {"ACPI_FAN",		ACPI_FAN},
23333184cf5aSNate Lawson     {"ACPI_POWERRES",		ACPI_POWERRES},
23344c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
2335cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
23363184cf5aSNate Lawson     {"ACPI_TIMER",		ACPI_TIMER},
2337b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
233815e32d5dSMike Smith     {NULL, 0}
233915e32d5dSMike Smith };
234015e32d5dSMike Smith 
234115e32d5dSMike Smith static struct debugtag dbg_level[] = {
23424c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
23439501b603SJohn Baldwin     {"ACPI_LV_WARN",		ACPI_LV_WARN},
23449501b603SJohn Baldwin     {"ACPI_LV_INIT",		ACPI_LV_INIT},
23454c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
23469501b603SJohn Baldwin     {"ACPI_LV_INFO",		ACPI_LV_INFO},
23474c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
2348d62ab2f4SMitsuru IWASAKI 
2349d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
2350297835bcSNate Lawson     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
23514c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
23524c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
2353d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
23544c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
23554c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
23564c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
23574c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
23584c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
23594c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
23604c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
23614c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
23624c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
23634c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
2364d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
2365d62ab2f4SMitsuru IWASAKI 
2366d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
2367d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
2368d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
2369d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
2370d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
23714c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
2372d62ab2f4SMitsuru IWASAKI 
2373d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2374d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
2375d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
2376d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
2377d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
2378d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
2379d62ab2f4SMitsuru IWASAKI 
2380d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
238198479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
238298479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
238398479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
238498479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
238598479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
238615e32d5dSMike Smith     {NULL, 0}
238715e32d5dSMike Smith };
238815e32d5dSMike Smith 
238915e32d5dSMike Smith static void
239015e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
239115e32d5dSMike Smith {
239215e32d5dSMike Smith     char	*ep;
239315e32d5dSMike Smith     int		i, l;
23940ae55423SMike Smith     int		set;
239515e32d5dSMike Smith 
239615e32d5dSMike Smith     while (*cp) {
239715e32d5dSMike Smith 	if (isspace(*cp)) {
239815e32d5dSMike Smith 	    cp++;
239915e32d5dSMike Smith 	    continue;
240015e32d5dSMike Smith 	}
240115e32d5dSMike Smith 	ep = cp;
240215e32d5dSMike Smith 	while (*ep && !isspace(*ep))
240315e32d5dSMike Smith 	    ep++;
24040ae55423SMike Smith 	if (*cp == '!') {
24050ae55423SMike Smith 	    set = 0;
24060ae55423SMike Smith 	    cp++;
24070ae55423SMike Smith 	    if (cp == ep)
24080ae55423SMike Smith 		continue;
24090ae55423SMike Smith 	} else {
24100ae55423SMike Smith 	    set = 1;
24110ae55423SMike Smith 	}
241215e32d5dSMike Smith 	l = ep - cp;
241315e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
241415e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
2415be2b1797SNate Lawson 		if (set)
241615e32d5dSMike Smith 		    *flag |= tag[i].value;
2417be2b1797SNate Lawson 		else
24180ae55423SMike Smith 		    *flag &= ~tag[i].value;
241915e32d5dSMike Smith 		printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
242015e32d5dSMike Smith 	    }
242115e32d5dSMike Smith 	}
242215e32d5dSMike Smith 	cp = ep;
242315e32d5dSMike Smith     }
242415e32d5dSMike Smith }
242515e32d5dSMike Smith 
242615e32d5dSMike Smith static void
24272a4ac806SMike Smith acpi_set_debugging(void *junk)
242815e32d5dSMike Smith {
242994aae282SMike Barcroft     char	*cp;
243015e32d5dSMike Smith 
24311d7b121cSNate Lawson     if (cold) {
24320ae55423SMike Smith 	AcpiDbgLayer = 0;
24330ae55423SMike Smith 	AcpiDbgLevel = 0;
24341d7b121cSNate Lawson     }
24351d7b121cSNate Lawson 
243694aae282SMike Barcroft     if ((cp = getenv("debug.acpi.layer")) != NULL) {
243715e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
243894aae282SMike Barcroft 	freeenv(cp);
243994aae282SMike Barcroft     }
244094aae282SMike Barcroft     if ((cp = getenv("debug.acpi.level")) != NULL) {
244115e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
244294aae282SMike Barcroft 	freeenv(cp);
244394aae282SMike Barcroft     }
24440ae55423SMike Smith 
24451d7b121cSNate Lawson     if (cold) {
24461d7b121cSNate Lawson 	printf("ACPI debug layer 0x%x debug level 0x%x\n",
24471d7b121cSNate Lawson 	       AcpiDbgLayer, AcpiDbgLevel);
24481d7b121cSNate Lawson     }
244915e32d5dSMike Smith }
2450be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
2451be2b1797SNate Lawson 	NULL);
24521d7b121cSNate Lawson 
24531d7b121cSNate Lawson static int
24541d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
24551d7b121cSNate Lawson {
245654f6bca0SNate Lawson     int		 error, *dbg;
24571d7b121cSNate Lawson     struct	 debugtag *tag;
245854f6bca0SNate Lawson     struct	 sbuf sb;
24591d7b121cSNate Lawson 
246054f6bca0SNate Lawson     if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
246154f6bca0SNate Lawson 	return (ENOMEM);
24621d7b121cSNate Lawson     if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
24631d7b121cSNate Lawson 	tag = &dbg_layer[0];
24641d7b121cSNate Lawson 	dbg = &AcpiDbgLayer;
24651d7b121cSNate Lawson     } else {
24661d7b121cSNate Lawson 	tag = &dbg_level[0];
24671d7b121cSNate Lawson 	dbg = &AcpiDbgLevel;
24681d7b121cSNate Lawson     }
24691d7b121cSNate Lawson 
24701d7b121cSNate Lawson     /* Get old values if this is a get request. */
24711d7b121cSNate Lawson     if (*dbg == 0) {
247254f6bca0SNate Lawson 	sbuf_cpy(&sb, "NONE");
24731d7b121cSNate Lawson     } else if (req->newptr == NULL) {
24741d7b121cSNate Lawson 	for (; tag->name != NULL; tag++) {
247554f6bca0SNate Lawson 	    if ((*dbg & tag->value) == tag->value)
247654f6bca0SNate Lawson 		sbuf_printf(&sb, "%s ", tag->name);
24771d7b121cSNate Lawson 	}
24781d7b121cSNate Lawson     }
247954f6bca0SNate Lawson     sbuf_trim(&sb);
248054f6bca0SNate Lawson     sbuf_finish(&sb);
24811d7b121cSNate Lawson 
248254f6bca0SNate Lawson     error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
248354f6bca0SNate Lawson     sbuf_delete(&sb);
24841d7b121cSNate Lawson 
24851d7b121cSNate Lawson     /* If the user is setting a string, parse it. */
24861d7b121cSNate Lawson     if (error == 0 && req->newptr != NULL) {
24871d7b121cSNate Lawson 	*dbg = 0;
24881d7b121cSNate Lawson 	setenv((char *)oidp->oid_arg1, (char *)req->newptr);
24891d7b121cSNate Lawson 	acpi_set_debugging(NULL);
24901d7b121cSNate Lawson     }
24911d7b121cSNate Lawson 
24921d7b121cSNate Lawson     return (error);
24931d7b121cSNate Lawson }
24941d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
24951d7b121cSNate Lawson 	    "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
24961d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
24971d7b121cSNate Lawson 	    "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
249815e32d5dSMike Smith #endif
2499f9390180SMitsuru IWASAKI 
2500f9390180SMitsuru IWASAKI static int
2501f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
2502f9390180SMitsuru IWASAKI {
2503f9390180SMitsuru IWASAKI 	int	state, acpi_state;
2504f9390180SMitsuru IWASAKI 	int	error;
2505f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
2506f9390180SMitsuru IWASAKI 	va_list	ap;
2507f9390180SMitsuru IWASAKI 
2508f9390180SMitsuru IWASAKI 	error = 0;
2509f9390180SMitsuru IWASAKI 	switch (cmd) {
2510f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
2511f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
2512f9390180SMitsuru IWASAKI 		if (sc == NULL) {
2513f9390180SMitsuru IWASAKI 			error = EINVAL;
2514f9390180SMitsuru IWASAKI 			goto out;
2515f9390180SMitsuru IWASAKI 		}
2516f9390180SMitsuru IWASAKI 
2517f9390180SMitsuru IWASAKI 		va_start(ap, arg);
2518f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
2519f9390180SMitsuru IWASAKI 		va_end(ap);
2520f9390180SMitsuru IWASAKI 
2521f9390180SMitsuru IWASAKI 		switch (state) {
2522f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
2523f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
2524f9390180SMitsuru IWASAKI 			break;
2525f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
2526f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
2527f9390180SMitsuru IWASAKI 			break;
2528f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
2529f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
2530f9390180SMitsuru IWASAKI 			break;
2531f9390180SMitsuru IWASAKI 		default:
2532f9390180SMitsuru IWASAKI 			error = EINVAL;
2533f9390180SMitsuru IWASAKI 			goto out;
2534f9390180SMitsuru IWASAKI 		}
2535f9390180SMitsuru IWASAKI 
2536f9390180SMitsuru IWASAKI 		acpi_SetSleepState(sc, acpi_state);
2537f9390180SMitsuru IWASAKI 		break;
2538f9390180SMitsuru IWASAKI 	default:
2539f9390180SMitsuru IWASAKI 		error = EINVAL;
2540f9390180SMitsuru IWASAKI 		goto out;
2541f9390180SMitsuru IWASAKI 	}
2542f9390180SMitsuru IWASAKI 
2543f9390180SMitsuru IWASAKI out:
2544f9390180SMitsuru IWASAKI 	return (error);
2545f9390180SMitsuru IWASAKI }
2546f9390180SMitsuru IWASAKI 
2547f9390180SMitsuru IWASAKI static void
2548f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
2549f9390180SMitsuru IWASAKI {
2550be2b1797SNate Lawson     if (!cold || resource_disabled("acpi", 0))
25516c407052SMitsuru IWASAKI 	return;
2552f9390180SMitsuru IWASAKI 
2553f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
2554f9390180SMitsuru IWASAKI }
2555f9390180SMitsuru IWASAKI 
2556f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
2557