xref: /freebsd/sys/dev/acpica/acpi.c (revision a4ecd54325124838af8f8176d14d42ffa8ed3d7c)
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, ...);
143879d6c23STakanori Watanabe static int	acpi_child_location_str_method(device_t acdev, device_t child,
144879d6c23STakanori Watanabe 					       char *buf, size_t buflen);
145879d6c23STakanori Watanabe static int	acpi_child_pnpinfo_str_method(device_t acdev, device_t child,
146879d6c23STakanori Watanabe 					      char *buf, size_t buflen);
147879d6c23STakanori Watanabe 
148f9390180SMitsuru IWASAKI 
14915e32d5dSMike Smith static device_method_t acpi_methods[] = {
15015e32d5dSMike Smith     /* Device interface */
15115e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
15215e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
15315e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
15456a70eadSNate Lawson     DEVMETHOD(device_detach,		bus_generic_detach),
15515e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
15615e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
15715e32d5dSMike Smith     DEVMETHOD(device_resume,		bus_generic_resume),
15815e32d5dSMike Smith 
15915e32d5dSMike Smith     /* Bus interface */
16015e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
16115e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
16215e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
16315e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
16415e32d5dSMike Smith     DEVMETHOD(bus_set_resource,		acpi_set_resource),
16515e32d5dSMike Smith     DEVMETHOD(bus_get_resource,		acpi_get_resource),
16615e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
16715e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
168879d6c23STakanori Watanabe     DEVMETHOD(bus_child_pnpinfo_str,	acpi_child_pnpinfo_str_method),
169879d6c23STakanori Watanabe     DEVMETHOD(bus_child_location_str,	acpi_child_location_str_method),
17015e32d5dSMike Smith     DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
17115e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
17215e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
17315e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
17415e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
17515e32d5dSMike Smith 
176bc0f2195SMike Smith     /* ISA emulation */
177bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
178bc0f2195SMike Smith 
17915e32d5dSMike Smith     {0, 0}
18015e32d5dSMike Smith };
18115e32d5dSMike Smith 
18215e32d5dSMike Smith static driver_t acpi_driver = {
18315e32d5dSMike Smith     "acpi",
18415e32d5dSMike Smith     acpi_methods,
18515e32d5dSMike Smith     sizeof(struct acpi_softc),
18615e32d5dSMike Smith };
18715e32d5dSMike Smith 
1883273b005SMike Smith static devclass_t acpi_devclass;
1896d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
190a4ecd543SNate Lawson MODULE_VERSION(acpi, 1);
19115e32d5dSMike Smith 
1921d7b121cSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging");
1931d7b121cSNate Lawson static char acpi_ca_version[12];
1941d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
1951d7b121cSNate Lawson 	      acpi_ca_version, 0, "Version of Intel ACPI-CA");
19615e32d5dSMike Smith 
19715e32d5dSMike Smith /*
198413081d7SNate Lawson  * Allow override of whether methods execute in parallel or not.
199c9b8d77dSNate Lawson  * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS"
200c9b8d77dSNate Lawson  * errors for AML that really can't handle parallel method execution.
201c9b8d77dSNate Lawson  * It is off by default since this breaks recursive methods and
202c9b8d77dSNate Lawson  * some IBMs use such code.
203413081d7SNate Lawson  */
204c9b8d77dSNate Lawson static int acpi_serialize_methods;
205413081d7SNate Lawson TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods);
206413081d7SNate Lawson 
207c9b8d77dSNate Lawson /*
208c9b8d77dSNate Lawson  * Allow override of whether to support the _OSI method.  This allows us
209c9b8d77dSNate Lawson  * to claim compatibility with various MS OSs without changing the value
210c9b8d77dSNate Lawson  * we report for _OS.  This is enabled by default since it fixes some
211c9b8d77dSNate Lawson  * problems with interrupt routing although it can be disabled if it
212c9b8d77dSNate Lawson  * causes problems.  See the definition of "AcpiGbl_ValidOsiStrings" for
213c9b8d77dSNate Lawson  * a list of systems we claim.
214c9b8d77dSNate Lawson  */
215c9b8d77dSNate Lawson static int acpi_osi_method = 1;
216413081d7SNate Lawson TUNABLE_INT("hw.acpi.osi_method", &acpi_osi_method);
217413081d7SNate Lawson 
218413081d7SNate Lawson /*
2196d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
2206d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
221be2b1797SNate Lawson  * It also cannot be unloaded, since the entire system bus heirarchy hangs
222be2b1797SNate Lawson  * off it.
2236d63101aSMike Smith  */
2246d63101aSMike Smith static int
2256d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
2266d63101aSMike Smith {
2276d63101aSMike Smith     switch(event) {
2286d63101aSMike Smith     case MOD_LOAD:
22987b45ed5SMitsuru IWASAKI 	if (!cold) {
23087b45ed5SMitsuru IWASAKI 	    printf("The ACPI driver cannot be loaded after boot.\n");
2316d63101aSMike Smith 	    return (EPERM);
23287b45ed5SMitsuru IWASAKI 	}
2336d63101aSMike Smith 	break;
2346d63101aSMike Smith     case MOD_UNLOAD:
235f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
2366d63101aSMike Smith 	    return (EBUSY);
237f9390180SMitsuru IWASAKI 	break;
2386d63101aSMike Smith     default:
2396d63101aSMike Smith 	break;
2406d63101aSMike Smith     }
2416d63101aSMike Smith     return (0);
2426d63101aSMike Smith }
2436d63101aSMike Smith 
2446d63101aSMike Smith /*
245bbc2815cSJohn Baldwin  * Perform early initialization.
24615e32d5dSMike Smith  */
247bbc2815cSJohn Baldwin ACPI_STATUS
248bbc2815cSJohn Baldwin acpi_Startup(void)
24915e32d5dSMike Smith {
250d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
251d786139cSMaxime Henrion     char *debugpoint;
25215e32d5dSMike Smith #endif
253bbc2815cSJohn Baldwin     static int error, started = 0;
25415e32d5dSMike Smith 
2551b8c233dSPeter Pentchev     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2561b8c233dSPeter Pentchev 
257bbc2815cSJohn Baldwin     if (started)
258bbc2815cSJohn Baldwin 	return_VALUE (error);
259bbc2815cSJohn Baldwin     started = 1;
26015e32d5dSMike Smith 
261fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000
262be2b1797SNate Lawson     /* Initialise the ACPI mutex */
2636008862bSJohn Baldwin     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
264fc0ea94aSJohn Baldwin #endif
265cb9b0d80SMike Smith 
266413081d7SNate Lawson     /*
267413081d7SNate Lawson      * Set the globals from our tunables.  This is needed because ACPI-CA
268413081d7SNate Lawson      * uses UINT8 for some values and we have no tunable_uint8.
269413081d7SNate Lawson      */
270413081d7SNate Lawson     AcpiGbl_AllMethodsSerialized = acpi_serialize_methods;
271413081d7SNate Lawson     AcpiGbl_CreateOsiMethod = acpi_osi_method;
272413081d7SNate Lawson 
273be2b1797SNate Lawson     /* Start up the ACPI CA subsystem. */
274d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
275d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
276d786139cSMaxime Henrion     if (debugpoint) {
277d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "init"))
27815e32d5dSMike Smith 	    acpi_EnterDebugger();
279d786139cSMaxime Henrion 	freeenv(debugpoint);
280d786139cSMaxime Henrion     }
28115e32d5dSMike Smith #endif
282b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
283bfae45aaSMike Smith 	printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
284bbc2815cSJohn Baldwin 	return_VALUE (error);
28515e32d5dSMike Smith     }
286d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
287d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
288d786139cSMaxime Henrion     if (debugpoint) {
289d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "tables"))
29015e32d5dSMike Smith 	    acpi_EnterDebugger();
291d786139cSMaxime Henrion 	freeenv(debugpoint);
292d786139cSMaxime Henrion     }
29315e32d5dSMike Smith #endif
2941611ea87SMitsuru IWASAKI 
295b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiLoadTables())) {
296bfae45aaSMike Smith 	printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
297bbc2815cSJohn Baldwin 	return_VALUE(error);
29815e32d5dSMike Smith     }
2993184cf5aSNate Lawson 
3003184cf5aSNate Lawson     /* Set up any quirks we have for this XSDT. */
3013184cf5aSNate Lawson     acpi_quirks_set();
3024e376d58SNate Lawson     if (acpi_disabled("acpi"))
3034e376d58SNate Lawson 	return_VALUE (AE_ERROR);
3043184cf5aSNate Lawson 
305bbc2815cSJohn Baldwin     return_VALUE (AE_OK);
306bbc2815cSJohn Baldwin }
307bbc2815cSJohn Baldwin 
308bbc2815cSJohn Baldwin /*
309bbc2815cSJohn Baldwin  * Detect ACPI, perform early initialisation
310bbc2815cSJohn Baldwin  */
311bbc2815cSJohn Baldwin static void
312bbc2815cSJohn Baldwin acpi_identify(driver_t *driver, device_t parent)
313bbc2815cSJohn Baldwin {
314bbc2815cSJohn Baldwin     device_t	child;
315bbc2815cSJohn Baldwin 
316bbc2815cSJohn Baldwin     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
317bbc2815cSJohn Baldwin 
318bbc2815cSJohn Baldwin     if (!cold)
319bbc2815cSJohn Baldwin 	return_VOID;
320bbc2815cSJohn Baldwin 
321bbc2815cSJohn Baldwin     /* Check that we haven't been disabled with a hint. */
322bbc2815cSJohn Baldwin     if (resource_disabled("acpi", 0))
323bbc2815cSJohn Baldwin 	return_VOID;
324bbc2815cSJohn Baldwin 
325bbc2815cSJohn Baldwin     /* Make sure we're not being doubly invoked. */
326bbc2815cSJohn Baldwin     if (device_find_child(parent, "acpi", 0) != NULL)
327bbc2815cSJohn Baldwin 	return_VOID;
328bbc2815cSJohn Baldwin 
329bbc2815cSJohn Baldwin     /* Initialize ACPI-CA. */
330bbc2815cSJohn Baldwin     if (ACPI_FAILURE(acpi_Startup()))
331bbc2815cSJohn Baldwin 	return_VOID;
33215e32d5dSMike Smith 
3334e376d58SNate Lawson     snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION);
3344e376d58SNate Lawson 
335be2b1797SNate Lawson     /* Attach the actual ACPI device. */
33615e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
33715e32d5dSMike Smith 	device_printf(parent, "ACPI: could not attach\n");
3380ae55423SMike Smith 	return_VOID;
33915e32d5dSMike Smith     }
34015e32d5dSMike Smith }
34115e32d5dSMike Smith 
34215e32d5dSMike Smith /*
34315e32d5dSMike Smith  * Fetch some descriptive data from ACPI to put in our attach message
34415e32d5dSMike Smith  */
34515e32d5dSMike Smith static int
34615e32d5dSMike Smith acpi_probe(device_t dev)
34715e32d5dSMike Smith {
34815e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
34915e32d5dSMike Smith     char		buf[20];
35015e32d5dSMike Smith     int			error;
3512ccd1cacSNate Lawson     struct sbuf		sb;
3522ccd1cacSNate Lawson     ACPI_STATUS		status;
353fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
35415e32d5dSMike Smith 
355b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
356f9390180SMitsuru IWASAKI 
357f9390180SMitsuru IWASAKI     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
358f9390180SMitsuru IWASAKI 	power_pm_get_type() != POWER_PM_TYPE_ACPI) {
359be2b1797SNate Lawson 
360f9390180SMitsuru IWASAKI 	device_printf(dev, "Other PM system enabled.\n");
361f9390180SMitsuru IWASAKI 	return_VALUE(ENXIO);
362f9390180SMitsuru IWASAKI     }
363f9390180SMitsuru IWASAKI 
364cb9b0d80SMike Smith     ACPI_LOCK;
3650ae55423SMike Smith 
366b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
367be2b1797SNate Lawson 	device_printf(dev, "couldn't get XSDT header: %s\n",
368be2b1797SNate Lawson 		      AcpiFormatException(status));
369cb9b0d80SMike Smith 	error = ENXIO;
370cb9b0d80SMike Smith     } else {
3712ccd1cacSNate Lawson 	sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
3722ccd1cacSNate Lawson 	sbuf_bcat(&sb, th.OemId, 6);
3732ccd1cacSNate Lawson 	sbuf_trim(&sb);
3742ccd1cacSNate Lawson 	sbuf_putc(&sb, ' ');
3752ccd1cacSNate Lawson 	sbuf_bcat(&sb, th.OemTableId, 8);
3762ccd1cacSNate Lawson 	sbuf_trim(&sb);
3772ccd1cacSNate Lawson 	sbuf_finish(&sb);
3782ccd1cacSNate Lawson 	device_set_desc_copy(dev, sbuf_data(&sb));
3792ccd1cacSNate Lawson 	sbuf_delete(&sb);
380cb9b0d80SMike Smith 	error = 0;
381cb9b0d80SMike Smith     }
382cb9b0d80SMike Smith     ACPI_UNLOCK;
383cb9b0d80SMike Smith     return_VALUE(error);
38415e32d5dSMike Smith }
38515e32d5dSMike Smith 
38615e32d5dSMike Smith static int
38715e32d5dSMike Smith acpi_attach(device_t dev)
38815e32d5dSMike Smith {
38915e32d5dSMike Smith     struct acpi_softc	*sc;
390cb9b0d80SMike Smith     ACPI_STATUS		status;
39115e32d5dSMike Smith     int			error;
39232d18aa5SMike Smith     UINT32		flags;
393498d464fSMitsuru IWASAKI     char		*env;
394d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
395d786139cSMaxime Henrion     char		*debugpoint;
39615e32d5dSMike Smith #endif
397fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
39815e32d5dSMike Smith 
399b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
400cb9b0d80SMike Smith     ACPI_LOCK;
40115e32d5dSMike Smith     sc = device_get_softc(dev);
40215e32d5dSMike Smith     bzero(sc, sizeof(*sc));
40315e32d5dSMike Smith     sc->acpi_dev = dev;
40415e32d5dSMike Smith 
405d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
406d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
407d786139cSMaxime Henrion     if (debugpoint) {
408d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "spaces"))
40915e32d5dSMike Smith 	    acpi_EnterDebugger();
410d786139cSMaxime Henrion 	freeenv(debugpoint);
411d786139cSMaxime Henrion     }
41215e32d5dSMike Smith #endif
41315e32d5dSMike Smith 
414be2b1797SNate Lawson     /* Install the default address space handlers. */
415cb9b0d80SMike Smith     error = ENXIO;
416be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
417be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
418be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
419be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
420be2b1797SNate Lawson 		      AcpiFormatException(status));
421cb9b0d80SMike Smith 	goto out;
42215e32d5dSMike Smith     }
423be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
424be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
425be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
426be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemIO handler: %s\n",
427be2b1797SNate Lawson 		      AcpiFormatException(status));
428cb9b0d80SMike Smith 	goto out;
42915e32d5dSMike Smith     }
430be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
431be2b1797SNate Lawson 		ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
432be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
433be2b1797SNate Lawson 	device_printf(dev, "could not initialise PciConfig handler: %s\n",
434be2b1797SNate Lawson 		      AcpiFormatException(status));
435cb9b0d80SMike Smith 	goto out;
43615e32d5dSMike Smith     }
43715e32d5dSMike Smith 
43815e32d5dSMike Smith     /*
43915e32d5dSMike Smith      * Bring ACPI fully online.
44015e32d5dSMike Smith      *
441be2b1797SNate Lawson      * Note that some systems (specifically, those with namespace evaluation
442be2b1797SNate Lawson      * issues that require the avoidance of parts of the namespace) must
443be2b1797SNate Lawson      * avoid running _INI and _STA on everything, as well as dodging the final
444be2b1797SNate Lawson      * object init pass.
44515e32d5dSMike Smith      *
44632d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
44732d18aa5SMike Smith      *
448be2b1797SNate Lawson      * XXX We should arrange for the object init pass after we have attached
449be2b1797SNate Lawson      *     all our child devices, but on many systems it works here.
45015e32d5dSMike Smith      */
451d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
452d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
453d786139cSMaxime Henrion     if (debugpoint) {
454d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "enable"))
45515e32d5dSMike Smith 	    acpi_EnterDebugger();
456d786139cSMaxime Henrion 	freeenv(debugpoint);
457d786139cSMaxime Henrion     }
45815e32d5dSMike Smith #endif
45932d18aa5SMike Smith     flags = 0;
460d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
46132d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
462b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
463be2b1797SNate Lawson 	device_printf(dev, "Could not enable ACPI: %s\n",
464be2b1797SNate Lawson 		      AcpiFormatException(status));
465cb9b0d80SMike Smith 	goto out;
46615e32d5dSMike Smith     }
46715e32d5dSMike Smith 
468f8335e3aSNate Lawson     /*
469f8335e3aSNate Lawson      * Call the ECDT probe function to provide EC functionality before
470f8335e3aSNate Lawson      * the namespace has been evaluated.
471f8335e3aSNate Lawson      */
472f8335e3aSNate Lawson     acpi_ec_ecdt_probe(dev);
473f8335e3aSNate Lawson 
474b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
475be2b1797SNate Lawson 	device_printf(dev, "Could not initialize ACPI objects: %s\n",
476be2b1797SNate Lawson 		      AcpiFormatException(status));
477b69ed3f4SMitsuru IWASAKI 	goto out;
478b69ed3f4SMitsuru IWASAKI     }
479b69ed3f4SMitsuru IWASAKI 
48015e32d5dSMike Smith     /*
4811d073b1dSJohn Baldwin      * Setup our sysctl tree.
4821d073b1dSJohn Baldwin      *
4831d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
4841d073b1dSJohn Baldwin      */
4851d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
4861d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
4871d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
4881d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
4891d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
490d75de536SMitsuru IWASAKI 	OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
491d75de536SMitsuru IWASAKI 	0, 0, acpi_supported_sleep_state_sysctl, "A", "");
492d75de536SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4931d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4941d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4951d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4961d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4971d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4981d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4991d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
5001d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
501f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
502f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
503f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
504f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
505f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
506f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
5072d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
508ff01efb5SMitsuru IWASAKI 	OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
509ff01efb5SMitsuru IWASAKI 	&sc->acpi_sleep_delay, 0, "sleep delay");
510ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5111611ea87SMitsuru IWASAKI 	OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
5121611ea87SMitsuru IWASAKI 	&sc->acpi_s4bios, 0, "S4BIOS mode");
5131611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5142d644607SMitsuru IWASAKI 	OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
5152d644607SMitsuru IWASAKI 	&sc->acpi_verbose, 0, "verbose mode");
516c6a78e98STakanori Watanabe     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
517c6a78e98STakanori Watanabe 	OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW,
518c6a78e98STakanori Watanabe 	&sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff");
519bf0c18ecSNate Lawson 
520bf0c18ecSNate Lawson     /*
521bf0c18ecSNate Lawson      * Default to 5 seconds before sleeping to give some machines time to
522bf0c18ecSNate Lawson      * stabilize.
523bf0c18ecSNate Lawson      */
524bf0c18ecSNate Lawson     sc->acpi_sleep_delay = 5;
525c6a78e98STakanori Watanabe     sc->acpi_disable_on_poweroff = 1;
5262d644607SMitsuru IWASAKI     if (bootverbose)
5272d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
528498d464fSMitsuru IWASAKI     if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) {
529498d464fSMitsuru IWASAKI 	sc->acpi_verbose = 1;
530498d464fSMitsuru IWASAKI 	freeenv(env);
531498d464fSMitsuru IWASAKI     }
5321d073b1dSJohn Baldwin 
5332d610c46SNate Lawson     /* Only enable S4BIOS by default if the FACS says it is available. */
5342d610c46SNate Lawson     if (AcpiGbl_FACS->S4Bios_f != 0)
5352d610c46SNate Lawson 	    sc->acpi_s4bios = 1;
5362d610c46SNate Lawson 
5371d073b1dSJohn Baldwin     /*
53815e32d5dSMike Smith      * Dispatch the default sleep state to devices.
53915e32d5dSMike Smith      * TBD: should be configured from userland policy manager.
54015e32d5dSMike Smith      */
54115e32d5dSMike Smith     sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
54215e32d5dSMike Smith     sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
54315e32d5dSMike Smith     sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
544f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
545f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
54615e32d5dSMike Smith 
54713d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
54815e32d5dSMike Smith 
54915e32d5dSMike Smith     /*
55015e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
55115e32d5dSMike Smith      */
552d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
553d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
554d786139cSMaxime Henrion     if (debugpoint) {
555d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "probe"))
55615e32d5dSMike Smith 	    acpi_EnterDebugger();
557d786139cSMaxime Henrion 	freeenv(debugpoint);
558d786139cSMaxime Henrion     }
55915e32d5dSMike Smith #endif
56015e32d5dSMike Smith 
561be2b1797SNate Lawson     /* Register our shutdown handlers */
562be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc,
563be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
564be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
565be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
56615e32d5dSMike Smith 
56715e32d5dSMike Smith     /*
56815e32d5dSMike Smith      * Register our acpi event handlers.
56915e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
57015e32d5dSMike Smith      */
571be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
572be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
573be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
574be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
57515e32d5dSMike Smith 
576be2b1797SNate Lawson     /* Flag our initial states. */
57715e32d5dSMike Smith     sc->acpi_enabled = 1;
57815e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
579ece50487SMitsuru IWASAKI     sc->acpi_sleep_disabled = 0;
58015e32d5dSMike Smith 
581be2b1797SNate Lawson     /* Create the control device */
582a89fcf28STakanori Watanabe     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
5832a4689e8SRobert Watson 			      "acpi");
58415e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
58515e32d5dSMike Smith 
586d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
587d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
588d786139cSMaxime Henrion     if (debugpoint) {
589be2b1797SNate Lawson 	if (strcmp(debugpoint, "running") == 0)
59015e32d5dSMike Smith 	    acpi_EnterDebugger();
591d786139cSMaxime Henrion 	freeenv(debugpoint);
592d786139cSMaxime Henrion     }
59315e32d5dSMike Smith #endif
594f86214b6SMitsuru IWASAKI 
59591da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS
596be2b1797SNate Lawson     if ((error = acpi_task_thread_init()))
597c573e654SMitsuru IWASAKI 	goto out;
598c573e654SMitsuru IWASAKI #endif
599c573e654SMitsuru IWASAKI 
600be2b1797SNate Lawson     if ((error = acpi_machdep_init(dev)))
601f86214b6SMitsuru IWASAKI 	goto out;
602f86214b6SMitsuru IWASAKI 
603f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
604f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
605f9390180SMitsuru IWASAKI 
606eeb6dba2SJohn Baldwin     if (!acpi_disabled("bus"))
607eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
608eeb6dba2SJohn Baldwin 
609cb9b0d80SMike Smith     error = 0;
610cb9b0d80SMike Smith 
611cb9b0d80SMike Smith  out:
612cb9b0d80SMike Smith     ACPI_UNLOCK;
613cb9b0d80SMike Smith     return_VALUE (error);
61415e32d5dSMike Smith }
61515e32d5dSMike Smith 
6163184cf5aSNate Lawson static void
6173184cf5aSNate Lawson acpi_quirks_set()
6183184cf5aSNate Lawson {
6193184cf5aSNate Lawson     XSDT_DESCRIPTOR *xsdt;
6203184cf5aSNate Lawson     struct acpi_quirks *quirk;
6213184cf5aSNate Lawson     char *env, *tmp;
6223184cf5aSNate Lawson     int len;
6233184cf5aSNate Lawson 
6244e376d58SNate Lawson     /*
6254e376d58SNate Lawson      * If the user loaded a custom table or disabled "quirks", leave
6264e376d58SNate Lawson      * the settings alone.
6274e376d58SNate Lawson      */
6283184cf5aSNate Lawson     len = 0;
6294e376d58SNate Lawson     if ((env = getenv("acpi_dsdt_load")) != NULL) {
6304e376d58SNate Lawson 	/* XXX No strcasecmp but this is good enough. */
6314e376d58SNate Lawson 	if (*env == 'Y' || *env == 'y')
6324e376d58SNate Lawson 	    goto out;
6334e376d58SNate Lawson 	freeenv(env);
6344e376d58SNate Lawson     }
6353184cf5aSNate Lawson     if ((env = getenv("debug.acpi.disabled")) != NULL) {
6364e376d58SNate Lawson 	if (strstr("quirks", env) != NULL)
6373184cf5aSNate Lawson 	    goto out;
6383184cf5aSNate Lawson 	len = strlen(env);
6393184cf5aSNate Lawson     }
6403184cf5aSNate Lawson 
6413184cf5aSNate Lawson     /*
6423184cf5aSNate Lawson      * Search through our quirk table and concatenate the disabled
6433184cf5aSNate Lawson      * values with whatever we find.
6443184cf5aSNate Lawson      */
6453184cf5aSNate Lawson     xsdt = AcpiGbl_XSDT;
6463184cf5aSNate Lawson     for (quirk = acpi_quirks_table; quirk->OemId; quirk++) {
6473184cf5aSNate Lawson 	if (!strncmp(xsdt->OemId, quirk->OemId, strlen(quirk->OemId)) &&
6483184cf5aSNate Lawson 	    (xsdt->OemRevision == quirk->OemRevision ||
6493184cf5aSNate Lawson 	    quirk->OemRevision == ACPI_OEM_REV_ANY)) {
6503184cf5aSNate Lawson 		len += strlen(quirk->value) + 2;
6513184cf5aSNate Lawson 		if ((tmp = malloc(len, M_TEMP, M_NOWAIT)) == NULL)
6523184cf5aSNate Lawson 		    goto out;
6533184cf5aSNate Lawson 		sprintf(tmp, "%s %s", env ? env : "", quirk->value);
6543184cf5aSNate Lawson 		setenv("debug.acpi.disabled", tmp);
6553184cf5aSNate Lawson 		free(tmp, M_TEMP);
6563184cf5aSNate Lawson 		break;
6573184cf5aSNate Lawson 	}
6583184cf5aSNate Lawson     }
6593184cf5aSNate Lawson 
6603184cf5aSNate Lawson out:
6613184cf5aSNate Lawson     if (env)
6623184cf5aSNate Lawson 	freeenv(env);
6633184cf5aSNate Lawson }
6643184cf5aSNate Lawson 
66515e32d5dSMike Smith /*
66615e32d5dSMike Smith  * Handle a new device being added
66715e32d5dSMike Smith  */
66815e32d5dSMike Smith static device_t
66915e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
67015e32d5dSMike Smith {
67115e32d5dSMike Smith     struct acpi_device	*ad;
67215e32d5dSMike Smith     device_t		child;
67315e32d5dSMike Smith 
674be2b1797SNate Lawson     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
67515e32d5dSMike Smith 	return (NULL);
67615e32d5dSMike Smith 
67715e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
67815e32d5dSMike Smith 
67915e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
68015e32d5dSMike Smith     if (child != NULL)
68115e32d5dSMike Smith 	device_set_ivars(child, ad);
68215e32d5dSMike Smith     return (child);
68315e32d5dSMike Smith }
68415e32d5dSMike Smith 
68515e32d5dSMike Smith static int
68615e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
68715e32d5dSMike Smith {
68815e32d5dSMike Smith     struct acpi_device	 *adev = device_get_ivars(child);
68915e32d5dSMike Smith     struct resource_list *rl = &adev->ad_rl;
69015e32d5dSMike Smith     int retval = 0;
69115e32d5dSMike Smith 
69215e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
6939ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
6949ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
6959ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
6969ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
69715e32d5dSMike Smith     retval += bus_print_child_footer(bus, child);
69815e32d5dSMike Smith 
69915e32d5dSMike Smith     return (retval);
70015e32d5dSMike Smith }
70115e32d5dSMike Smith 
70263600cc3SNate Lawson /* Location hint for devctl(8) */
70363600cc3SNate Lawson static int
704247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf,
705247648afSTakanori Watanabe     size_t buflen)
706247648afSTakanori Watanabe {
707247648afSTakanori Watanabe     struct acpi_device *dinfo = device_get_ivars(child);
708247648afSTakanori Watanabe 
709247648afSTakanori Watanabe     if (dinfo->ad_handle)
710247648afSTakanori Watanabe 	snprintf(buf, buflen, "path=%s", acpi_name(dinfo->ad_handle));
711247648afSTakanori Watanabe     else
712247648afSTakanori Watanabe 	snprintf(buf, buflen, "magic=unknown");
713247648afSTakanori Watanabe     return (0);
714247648afSTakanori Watanabe }
715247648afSTakanori Watanabe 
71663600cc3SNate Lawson /* PnP information for devctl(8) */
71763600cc3SNate Lawson static int
718247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
719247648afSTakanori Watanabe     size_t buflen)
720247648afSTakanori Watanabe {
721247648afSTakanori Watanabe     ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL};
72263600cc3SNate Lawson     ACPI_DEVICE_INFO *adinfo;
72363600cc3SNate Lawson     struct acpi_device *dinfo = device_get_ivars(child);
724247648afSTakanori Watanabe     char *end;
725247648afSTakanori Watanabe     int error;
726247648afSTakanori Watanabe 
727247648afSTakanori Watanabe     error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf);
728247648afSTakanori Watanabe     adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer;
729247648afSTakanori Watanabe 
730247648afSTakanori Watanabe     if (error)
731247648afSTakanori Watanabe 	snprintf(buf, buflen, "Unknown");
732247648afSTakanori Watanabe     else
733247648afSTakanori Watanabe 	snprintf(buf, buflen, "_HID=%s _UID=%lu",
734247648afSTakanori Watanabe 		 (adinfo->Valid & ACPI_VALID_HID) ?
735247648afSTakanori Watanabe 		 adinfo->HardwareId.Value : "UNKNOWN",
73663600cc3SNate Lawson 		 (adinfo->Valid & ACPI_VALID_UID) ?
73763600cc3SNate Lawson 		 strtoul(adinfo->UniqueId.Value, &end, 10) : 0);
738247648afSTakanori Watanabe 
739247648afSTakanori Watanabe     if (adinfo)
740247648afSTakanori Watanabe 	AcpiOsFree(adinfo);
741247648afSTakanori Watanabe 
742247648afSTakanori Watanabe     return (0);
743247648afSTakanori Watanabe }
744247648afSTakanori Watanabe 
745247648afSTakanori Watanabe /*
74615e32d5dSMike Smith  * Handle per-device ivars
74715e32d5dSMike Smith  */
74815e32d5dSMike Smith static int
74915e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
75015e32d5dSMike Smith {
75115e32d5dSMike Smith     struct acpi_device	*ad;
75215e32d5dSMike Smith 
75315e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
75415e32d5dSMike Smith 	printf("device has no ivars\n");
75515e32d5dSMike Smith 	return (ENOENT);
75615e32d5dSMike Smith     }
75715e32d5dSMike Smith 
758be2b1797SNate Lawson     /* ACPI and ISA compatibility ivars */
75915e32d5dSMike Smith     switch(index) {
76015e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
76115e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
76215e32d5dSMike Smith 	break;
76315e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
76415e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
76515e32d5dSMike Smith 	break;
76615e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
76715e32d5dSMike Smith 	*(void **)result = ad->ad_private;
76815e32d5dSMike Smith 	break;
76932d18aa5SMike Smith     case ISA_IVAR_VENDORID:
77032d18aa5SMike Smith     case ISA_IVAR_SERIAL:
77132d18aa5SMike Smith     case ISA_IVAR_COMPATID:
77232d18aa5SMike Smith 	*(int *)result = -1;
77332d18aa5SMike Smith 	break;
77432d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
77532d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
77632d18aa5SMike Smith 	break;
77715e32d5dSMike Smith     default:
77815e32d5dSMike Smith 	return (ENOENT);
77915e32d5dSMike Smith     }
780be2b1797SNate Lawson 
78115e32d5dSMike Smith     return (0);
78215e32d5dSMike Smith }
78315e32d5dSMike Smith 
78415e32d5dSMike Smith static int
78515e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
78615e32d5dSMike Smith {
78715e32d5dSMike Smith     struct acpi_device	*ad;
78815e32d5dSMike Smith 
78915e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
79015e32d5dSMike Smith 	printf("device has no ivars\n");
79115e32d5dSMike Smith 	return (ENOENT);
79215e32d5dSMike Smith     }
79315e32d5dSMike Smith 
79415e32d5dSMike Smith     switch(index) {
79515e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
79615e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
79715e32d5dSMike Smith 	break;
79815e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
79915e32d5dSMike Smith 	ad->ad_magic = (int)value;
80015e32d5dSMike Smith 	break;
80115e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
80215e32d5dSMike Smith 	ad->ad_private = (void *)value;
80315e32d5dSMike Smith 	break;
80415e32d5dSMike Smith     default:
8052ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
80615e32d5dSMike Smith 	return (ENOENT);
80715e32d5dSMike Smith     }
808be2b1797SNate Lawson 
80915e32d5dSMike Smith     return (0);
81015e32d5dSMike Smith }
81115e32d5dSMike Smith 
81215e32d5dSMike Smith /*
81315e32d5dSMike Smith  * Handle child resource allocation/removal
81415e32d5dSMike Smith  */
81515e32d5dSMike Smith static int
816be2b1797SNate Lawson acpi_set_resource(device_t dev, device_t child, int type, int rid,
817be2b1797SNate Lawson 		  u_long start, u_long count)
81815e32d5dSMike Smith {
81915e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
82015e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
82115e32d5dSMike Smith 
82215e32d5dSMike Smith     resource_list_add(rl, type, rid, start, start + count -1, count);
82315e32d5dSMike Smith 
82415e32d5dSMike Smith     return(0);
82515e32d5dSMike Smith }
82615e32d5dSMike Smith 
82715e32d5dSMike Smith static int
828be2b1797SNate Lawson acpi_get_resource(device_t dev, device_t child, int type, int rid,
829be2b1797SNate Lawson 		  u_long *startp, u_long *countp)
83015e32d5dSMike Smith {
83115e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
83215e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
83315e32d5dSMike Smith     struct resource_list_entry	*rle;
83415e32d5dSMike Smith 
83515e32d5dSMike Smith     rle = resource_list_find(rl, type, rid);
83615e32d5dSMike Smith     if (!rle)
83715e32d5dSMike Smith 	return(ENOENT);
83815e32d5dSMike Smith 
83915e32d5dSMike Smith     if (startp)
84015e32d5dSMike Smith 	*startp = rle->start;
84115e32d5dSMike Smith     if (countp)
84215e32d5dSMike Smith 	*countp = rle->count;
84315e32d5dSMike Smith 
84415e32d5dSMike Smith     return (0);
84515e32d5dSMike Smith }
84615e32d5dSMike Smith 
84715e32d5dSMike Smith static struct resource *
84815e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
84915e32d5dSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
85015e32d5dSMike Smith {
85115e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
85215e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
85315e32d5dSMike Smith 
854be2b1797SNate Lawson     return (resource_list_alloc(rl, bus, child, type, rid, start, end, count,
855be2b1797SNate Lawson 	    flags));
85615e32d5dSMike Smith }
85715e32d5dSMike Smith 
85815e32d5dSMike Smith static int
85915e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
86015e32d5dSMike Smith {
86115e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
86215e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
86315e32d5dSMike Smith 
86415e32d5dSMike Smith     return (resource_list_release(rl, bus, child, type, rid, r));
86515e32d5dSMike Smith }
86615e32d5dSMike Smith 
867dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */
868dc750869SNate Lawson struct resource *
869dc750869SNate Lawson acpi_bus_alloc_gas(device_t dev, int *rid, ACPI_GENERIC_ADDRESS *gas)
870dc750869SNate Lawson {
871dc750869SNate Lawson     int type;
872dc750869SNate Lawson 
873dc750869SNate Lawson     if (gas == NULL || !ACPI_VALID_ADDRESS(gas->Address) ||
874dc750869SNate Lawson 	gas->RegisterBitWidth < 8)
875dc750869SNate Lawson 	return (NULL);
876dc750869SNate Lawson 
877dc750869SNate Lawson     switch (gas->AddressSpaceId) {
878dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
879dc750869SNate Lawson 	type = SYS_RES_MEMORY;
880dc750869SNate Lawson 	break;
881dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_IO:
882dc750869SNate Lawson 	type = SYS_RES_IOPORT;
883dc750869SNate Lawson 	break;
884dc750869SNate Lawson     default:
885dc750869SNate Lawson 	return (NULL);
886dc750869SNate Lawson     }
887dc750869SNate Lawson 
888dc750869SNate Lawson     bus_set_resource(dev, type, *rid, gas->Address, gas->RegisterBitWidth / 8);
8895f96beb9SNate Lawson     return (bus_alloc_resource_any(dev, type, rid, RF_ACTIVE));
890dc750869SNate Lawson }
891dc750869SNate Lawson 
89215e32d5dSMike Smith /*
893bc0f2195SMike Smith  * Handle ISA-like devices probing for a PnP ID to match.
894bc0f2195SMike Smith  */
895bc0f2195SMike Smith #define PNP_EISAID(s)				\
896bc0f2195SMike Smith 	((((s[0] - '@') & 0x1f) << 2)		\
897bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x18) >> 3)		\
898bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x07) << 13)	\
899bc0f2195SMike Smith 	 | (((s[2] - '@') & 0x1f) << 8)		\
900bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[4]) << 16)		\
901bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[3]) << 20)		\
902bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[6]) << 24)		\
903bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[5]) << 28))
904bc0f2195SMike Smith 
9051e4925e8SNate Lawson static uint32_t
90632d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
907bc0f2195SMike Smith {
9081e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
9091e4925e8SNate Lawson     ACPI_BUFFER		buf;
910bc0f2195SMike Smith     ACPI_HANDLE		h;
911bc0f2195SMike Smith     ACPI_STATUS		error;
912bc0f2195SMike Smith     u_int32_t		pnpid;
913fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
91432d18aa5SMike Smith 
915b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
91632d18aa5SMike Smith 
91732d18aa5SMike Smith     pnpid = 0;
918bd189fe7SNate Lawson     buf.Pointer = NULL;
919bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
920bd189fe7SNate Lawson 
92132d18aa5SMike Smith     ACPI_LOCK;
92232d18aa5SMike Smith 
9236fca9360SNate Lawson     /* Fetch and validate the HID. */
92432d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
925bd189fe7SNate Lawson 	goto out;
9266fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
9276fca9360SNate Lawson     if (ACPI_FAILURE(error))
928bd189fe7SNate Lawson 	goto out;
9291e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
93032d18aa5SMike Smith 
9311e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_HID) != 0)
9321e4925e8SNate Lawson 	pnpid = PNP_EISAID(devinfo->HardwareId.Value);
933be2b1797SNate Lawson 
934bd189fe7SNate Lawson out:
935bd189fe7SNate Lawson     if (buf.Pointer != NULL)
9361e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
93732d18aa5SMike Smith     ACPI_UNLOCK;
93832d18aa5SMike Smith     return_VALUE (pnpid);
93932d18aa5SMike Smith }
94032d18aa5SMike Smith 
9411e4925e8SNate Lawson static int
9421e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
943b2566207STakanori Watanabe {
9441e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
9451e4925e8SNate Lawson     ACPI_BUFFER		buf;
946b2566207STakanori Watanabe     ACPI_HANDLE		h;
947b2566207STakanori Watanabe     ACPI_STATUS		error;
9481e4925e8SNate Lawson     uint32_t		*pnpid;
9491e4925e8SNate Lawson     int			valid, i;
950b2566207STakanori Watanabe     ACPI_LOCK_DECL;
951b2566207STakanori Watanabe 
952b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
953b2566207STakanori Watanabe 
9541e4925e8SNate Lawson     pnpid = cids;
9551e4925e8SNate Lawson     valid = 0;
956bd189fe7SNate Lawson     buf.Pointer = NULL;
957bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
958bd189fe7SNate Lawson 
959b2566207STakanori Watanabe     ACPI_LOCK;
960b2566207STakanori Watanabe 
9611e4925e8SNate Lawson     /* Fetch and validate the CID */
962b2566207STakanori Watanabe     if ((h = acpi_get_handle(dev)) == NULL)
963bd189fe7SNate Lawson 	goto out;
9641e4925e8SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
9651e4925e8SNate Lawson     if (ACPI_FAILURE(error))
966bd189fe7SNate Lawson 	goto out;
9671e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
9681e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_CID) == 0)
969b2566207STakanori Watanabe 	goto out;
970b2566207STakanori Watanabe 
9711e4925e8SNate Lawson     if (devinfo->CompatibilityId.Count < count)
9721e4925e8SNate Lawson 	count = devinfo->CompatibilityId.Count;
9731e4925e8SNate Lawson     for (i = 0; i < count; i++) {
9741e4925e8SNate Lawson 	if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0)
9751e4925e8SNate Lawson 	    continue;
9761e4925e8SNate Lawson 	*pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value);
9771e4925e8SNate Lawson 	valid++;
978b2566207STakanori Watanabe     }
979b2566207STakanori Watanabe 
9801e4925e8SNate Lawson out:
981bd189fe7SNate Lawson     if (buf.Pointer != NULL)
9821e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
9831e4925e8SNate Lawson     ACPI_UNLOCK;
9841e4925e8SNate Lawson     return_VALUE (valid);
9851e4925e8SNate Lawson }
986b2566207STakanori Watanabe 
98732d18aa5SMike Smith static int
98832d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
98932d18aa5SMike Smith {
9901e4925e8SNate Lawson     int			result, cid_count, i;
9911e4925e8SNate Lawson     uint32_t		lid, cids[8];
992bc0f2195SMike Smith 
993b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
994bc0f2195SMike Smith 
995bc0f2195SMike Smith     /*
996bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
997bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
998bc0f2195SMike Smith      * that to happen, so don't ever return it.
999bc0f2195SMike Smith      */
1000bc0f2195SMike Smith     result = ENXIO;
1001bc0f2195SMike Smith 
1002be2b1797SNate Lawson     /* Scan the supplied IDs for a match */
1003b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
10041e4925e8SNate Lawson     cid_count = acpi_isa_get_compatid(child, cids, 8);
1005bc0f2195SMike Smith     while (ids && ids->ip_id) {
10061e4925e8SNate Lawson 	if (lid == ids->ip_id) {
1007bc0f2195SMike Smith 	    result = 0;
1008bc0f2195SMike Smith 	    goto out;
1009bc0f2195SMike Smith 	}
10101e4925e8SNate Lawson 	for (i = 0; i < cid_count; i++) {
10111e4925e8SNate Lawson 	    if (cids[i] == ids->ip_id) {
10121e4925e8SNate Lawson 		result = 0;
10131e4925e8SNate Lawson 		goto out;
10141e4925e8SNate Lawson 	    }
10151e4925e8SNate Lawson 	}
1016bc0f2195SMike Smith 	ids++;
1017bc0f2195SMike Smith     }
1018be2b1797SNate Lawson 
1019bc0f2195SMike Smith  out:
1020bc0f2195SMike Smith     return_VALUE (result);
1021bc0f2195SMike Smith }
1022bc0f2195SMike Smith 
1023bc0f2195SMike Smith /*
102415e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
102515e32d5dSMike Smith  *
1026be2b1797SNate Lawson  * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and
1027be2b1797SNate Lawson  * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
102815e32d5dSMike Smith  */
102915e32d5dSMike Smith static void
103015e32d5dSMike Smith acpi_probe_children(device_t bus)
103115e32d5dSMike Smith {
103215e32d5dSMike Smith     ACPI_HANDLE	parent;
1033be2b1797SNate Lawson     ACPI_STATUS	status;
10347d3bcec9SMike Smith     static char	*scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
103515e32d5dSMike Smith     int		i;
103615e32d5dSMike Smith 
1037b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1038cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
10390ae55423SMike Smith 
1040be2b1797SNate Lawson     /* Create any static children by calling device identify methods. */
10414c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
104215e32d5dSMike Smith     bus_generic_probe(bus);
104315e32d5dSMike Smith 
104415e32d5dSMike Smith     /*
104515e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
104615e32d5dSMike Smith      * we find.
104715e32d5dSMike Smith      *
104815e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1049be2b1797SNate Lawson      * we want to create nodes for all devices, not just those that are
1050be2b1797SNate Lawson      * currently present. (This assumes that we don't want to create/remove
1051be2b1797SNate Lawson      * devices as they appear, which might be smarter.)
105215e32d5dSMike Smith      */
10534c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
1054be2b1797SNate Lawson     for (i = 0; scopes[i] != NULL; i++) {
1055be2b1797SNate Lawson 	status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent);
1056be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
1057be2b1797SNate Lawson 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child,
1058be2b1797SNate Lawson 			      bus, NULL);
1059be2b1797SNate Lawson 	}
1060be2b1797SNate Lawson     }
106115e32d5dSMike Smith 
106215e32d5dSMike Smith     /*
106315e32d5dSMike Smith      * Scan all of the child devices we have created and let them probe/attach.
106415e32d5dSMike Smith      */
10654c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
106615e32d5dSMike Smith     bus_generic_attach(bus);
106715e32d5dSMike Smith 
106815e32d5dSMike Smith     /*
106915e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
107015e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
107115e32d5dSMike Smith      */
10724c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
107315e32d5dSMike Smith     bus_generic_attach(bus);
10740ae55423SMike Smith 
1075bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
10760ae55423SMike Smith     return_VOID;
107715e32d5dSMike Smith }
107815e32d5dSMike Smith 
107915e32d5dSMike Smith /*
108015e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
108115e32d5dSMike Smith  * it.
108215e32d5dSMike Smith  */
108315e32d5dSMike Smith static ACPI_STATUS
108415e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
108515e32d5dSMike Smith {
108615e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
108715e32d5dSMike Smith     device_t		child, bus = (device_t)context;
108815e32d5dSMike Smith 
1089b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
10900ae55423SMike Smith 
1091be2b1797SNate Lawson     /* Skip this device if we think we'll have trouble with it. */
10920ae55423SMike Smith     if (acpi_avoid(handle))
10930ae55423SMike Smith 	return_ACPI_STATUS (AE_OK);
10940ae55423SMike Smith 
1095b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
109615e32d5dSMike Smith 	switch(type) {
109715e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
109815e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
109915e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
110015e32d5dSMike Smith 	case ACPI_TYPE_POWER:
11010ae55423SMike Smith 	    if (acpi_disabled("children"))
11020ae55423SMike Smith 		break;
1103be2b1797SNate Lawson 
110415e32d5dSMike Smith 	    /*
110515e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
110615e32d5dSMike Smith 	     * so that the probe/attach passes will run breadth-first.
110715e32d5dSMike Smith 	     */
1108be2b1797SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n",
1109be2b1797SNate Lawson 			     acpi_name(handle)));
111015e32d5dSMike Smith 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
1111bc0f2195SMike Smith 	    if (child == NULL)
1112bc0f2195SMike Smith 		break;
111315e32d5dSMike Smith 	    acpi_set_handle(child, handle);
1114bc0f2195SMike Smith 
1115bc0f2195SMike Smith 	    /*
1116b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
1117b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
1118b519f9d6SMike Smith 	     * the handle, but we don't probe it).
1119b519f9d6SMike Smith 	     */
1120be2b1797SNate Lawson 	    if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
1121b519f9d6SMike Smith 		device_disable(child);
1122b519f9d6SMike Smith 		break;
1123b519f9d6SMike Smith 	    }
1124b519f9d6SMike Smith 
1125b519f9d6SMike Smith 	    /*
1126bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
1127bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
1128bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
1129bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
1130bc0f2195SMike Smith 	     * device not to have any resources.
1131bc0f2195SMike Smith 	     */
113272ad60adSNate Lawson 	    acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
1133bc0f2195SMike Smith 
1134be2b1797SNate Lawson 	    /* If we're debugging, probe/attach now rather than later */
1135b53f2771SMike Smith 	    ACPI_DEBUG_EXEC(device_probe_and_attach(child));
11360ae55423SMike Smith 	    break;
113715e32d5dSMike Smith 	}
113815e32d5dSMike Smith     }
1139be2b1797SNate Lawson 
11400ae55423SMike Smith     return_ACPI_STATUS (AE_OK);
114115e32d5dSMike Smith }
114215e32d5dSMike Smith 
114315e32d5dSMike Smith static void
114415e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto)
114515e32d5dSMike Smith {
1146c6a78e98STakanori Watanabe     struct acpi_softc *sc = arg;
1147c6a78e98STakanori Watanabe 
1148cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1149cb9b0d80SMike Smith 
115015e32d5dSMike Smith     /*
11510ae55423SMike Smith      * Disable all ACPI events before soft off, otherwise the system
115215e32d5dSMike Smith      * will be turned on again on some laptops.
115315e32d5dSMike Smith      *
115415e32d5dSMike Smith      * XXX this should probably be restricted to masking some events just
115515e32d5dSMike Smith      *     before powering down, since we may still need ACPI during the
115615e32d5dSMike Smith      *     shutdown process.
115715e32d5dSMike Smith      */
1158c6a78e98STakanori Watanabe     if (sc->acpi_disable_on_poweroff)
1159c6a78e98STakanori Watanabe 	acpi_Disable(sc);
116015e32d5dSMike Smith }
116115e32d5dSMike Smith 
116215e32d5dSMike Smith static void
116315e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
116415e32d5dSMike Smith {
1165eeb3a05fSNate Lawson 
1166eeb3a05fSNate Lawson     ACPI_ASSERTLOCK;
1167eeb3a05fSNate Lawson 
1168eeb3a05fSNate Lawson     /*
1169eeb3a05fSNate Lawson      * If powering off, run the actual shutdown code on each processor.
1170eeb3a05fSNate Lawson      * It will only perform the shutdown on the BSP.  Some chipsets do
1171eeb3a05fSNate Lawson      * not power off the system correctly if called from an AP.
1172eeb3a05fSNate Lawson      */
1173eeb3a05fSNate Lawson     if ((howto & RB_POWEROFF) != 0) {
1174eeb3a05fSNate Lawson 	printf("Powering system off using ACPI\n");
1175eeb3a05fSNate Lawson 	smp_rendezvous(NULL, acpi_shutdown_poweroff, NULL, NULL);
1176eeb3a05fSNate Lawson     } else {
1177eeb3a05fSNate Lawson 	printf("Shutting down ACPI\n");
1178eeb3a05fSNate Lawson 	AcpiTerminate();
1179eeb3a05fSNate Lawson     }
1180eeb3a05fSNate Lawson }
1181eeb3a05fSNate Lawson 
1182eeb3a05fSNate Lawson static void
1183eeb3a05fSNate Lawson acpi_shutdown_poweroff(void *arg)
1184eeb3a05fSNate Lawson {
118515e32d5dSMike Smith     ACPI_STATUS	status;
118615e32d5dSMike Smith 
1187cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1188cb9b0d80SMike Smith 
1189eeb3a05fSNate Lawson     /* Only attempt to power off if this is the BSP (cpuid 0). */
1190eeb3a05fSNate Lawson     if (PCPU_GET(cpuid) != 0)
1191eeb3a05fSNate Lawson 	return;
1192eeb3a05fSNate Lawson 
1193be2b1797SNate Lawson     status = AcpiEnterSleepStatePrep(acpi_off_state);
1194be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
1195b9c780d6SMitsuru IWASAKI 	printf("AcpiEnterSleepStatePrep failed - %s\n",
1196b9c780d6SMitsuru IWASAKI 	       AcpiFormatException(status));
1197b9c780d6SMitsuru IWASAKI 	return;
1198b9c780d6SMitsuru IWASAKI     }
119955398047SNate Lawson     ACPI_DISABLE_IRQS();
1200be2b1797SNate Lawson     status = AcpiEnterSleepState(acpi_off_state);
1201be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
1202bfae45aaSMike Smith 	printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
120315e32d5dSMike Smith     } else {
120415e32d5dSMike Smith 	DELAY(1000000);
120515e32d5dSMike Smith 	printf("ACPI power-off failed - timeout\n");
120615e32d5dSMike Smith     }
120715e32d5dSMike Smith }
120815e32d5dSMike Smith 
120913d4f7baSMitsuru IWASAKI static void
121013d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
121113d4f7baSMitsuru IWASAKI {
121213d4f7baSMitsuru IWASAKI     static int	first_time = 1;
121313d4f7baSMitsuru IWASAKI 
1214cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1215cb9b0d80SMike Smith 
121613d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
1217be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
121859ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
121913d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
122033febf93SNate Lawson 				     acpi_event_power_button_sleep, sc);
1221be2b1797SNate Lawson 	if (first_time)
12226c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Power Button (fixed)\n");
122313d4f7baSMitsuru IWASAKI     }
1224be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
122559ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
122613d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
122733febf93SNate Lawson 				     acpi_event_sleep_button_sleep, sc);
1228be2b1797SNate Lawson 	if (first_time)
12296c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
123013d4f7baSMitsuru IWASAKI     }
123113d4f7baSMitsuru IWASAKI 
123213d4f7baSMitsuru IWASAKI     first_time = 0;
123313d4f7baSMitsuru IWASAKI }
123413d4f7baSMitsuru IWASAKI 
123515e32d5dSMike Smith /*
1236c5ba0be4SMike Smith  * Returns true if the device is actually present and should
1237c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
1238c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
1239c5ba0be4SMike Smith  */
1240c5ba0be4SMike Smith BOOLEAN
1241c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
1242c5ba0be4SMike Smith {
12431e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1244c5ba0be4SMike Smith     ACPI_HANDLE		h;
12451e4925e8SNate Lawson     ACPI_BUFFER		buf;
1246c5ba0be4SMike Smith     ACPI_STATUS		error;
12471e4925e8SNate Lawson     int			ret;
1248c5ba0be4SMike Smith 
1249cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1250cb9b0d80SMike Smith 
12511e4925e8SNate Lawson     ret = FALSE;
1252c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1253c5ba0be4SMike Smith 	return (FALSE);
12541e4925e8SNate Lawson     buf.Pointer = NULL;
12551e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
12566fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
12576fca9360SNate Lawson     if (ACPI_FAILURE(error))
1258c5ba0be4SMike Smith 	return (FALSE);
12591e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1260be2b1797SNate Lawson 
1261be2b1797SNate Lawson     /* If no _STA method, must be present */
12621e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
12631e4925e8SNate Lawson 	ret = TRUE;
1264be2b1797SNate Lawson 
1265be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
12661e4925e8SNate Lawson     if ((devinfo->CurrentStatus & 0x9) == 0x9)
12671e4925e8SNate Lawson 	ret = TRUE;
1268be2b1797SNate Lawson 
12691e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
12701e4925e8SNate Lawson     return (ret);
1271c5ba0be4SMike Smith }
1272c5ba0be4SMike Smith 
1273c5ba0be4SMike Smith /*
1274b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
1275b53f2771SMike Smith  */
1276b53f2771SMike Smith BOOLEAN
1277b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
1278b53f2771SMike Smith {
12791e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1280b53f2771SMike Smith     ACPI_HANDLE		h;
12811e4925e8SNate Lawson     ACPI_BUFFER		buf;
1282b53f2771SMike Smith     ACPI_STATUS		error;
12831e4925e8SNate Lawson     int			ret;
1284b53f2771SMike Smith 
1285b53f2771SMike Smith     ACPI_ASSERTLOCK;
1286b53f2771SMike Smith 
12871e4925e8SNate Lawson     ret = FALSE;
1288b53f2771SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1289b53f2771SMike Smith 	return (FALSE);
12901e4925e8SNate Lawson     buf.Pointer = NULL;
12911e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
12926fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
12936fca9360SNate Lawson     if (ACPI_FAILURE(error))
1294b53f2771SMike Smith 	return (FALSE);
12951e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1296be2b1797SNate Lawson 
1297be2b1797SNate Lawson     /* If no _STA method, must be present */
12981e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
12991e4925e8SNate Lawson 	ret = TRUE;
1300be2b1797SNate Lawson 
1301be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
13021e4925e8SNate Lawson     if ((devinfo->CurrentStatus & 0x19) == 0x19)
13031e4925e8SNate Lawson 	ret = TRUE;
1304be2b1797SNate Lawson 
13051e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
13061e4925e8SNate Lawson     return (ret);
1307b53f2771SMike Smith }
1308b53f2771SMike Smith 
1309b53f2771SMike Smith /*
131015e32d5dSMike Smith  * Match a HID string against a device
131115e32d5dSMike Smith  */
131215e32d5dSMike Smith BOOLEAN
131315e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid)
131415e32d5dSMike Smith {
13151e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
131615e32d5dSMike Smith     ACPI_HANDLE		h;
13171e4925e8SNate Lawson     ACPI_BUFFER		buf;
131815e32d5dSMike Smith     ACPI_STATUS		error;
13191e4925e8SNate Lawson     int			ret, i;
132015e32d5dSMike Smith 
1321cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1322cb9b0d80SMike Smith 
13231e4925e8SNate Lawson     ret = FALSE;
13244d332989SMike Smith     if (hid == NULL)
132515e32d5dSMike Smith 	return (FALSE);
132615e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
132715e32d5dSMike Smith 	return (FALSE);
13281e4925e8SNate Lawson     buf.Pointer = NULL;
13291e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
13306fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
13316fca9360SNate Lawson     if (ACPI_FAILURE(error))
133215e32d5dSMike Smith 	return (FALSE);
13331e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1334be2b1797SNate Lawson 
1335c59c9a8eSJohn Baldwin     if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
1336c59c9a8eSJohn Baldwin 	strcmp(hid, devinfo->HardwareId.Value) == 0)
13371e4925e8SNate Lawson 	    ret = TRUE;
1338c59c9a8eSJohn Baldwin     else if ((devinfo->Valid & ACPI_VALID_CID) != 0) {
13391e4925e8SNate Lawson 	for (i = 0; i < devinfo->CompatibilityId.Count; i++) {
13401e4925e8SNate Lawson 	    if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) {
13411e4925e8SNate Lawson 		ret = TRUE;
13421e4925e8SNate Lawson 		break;
13431e4925e8SNate Lawson 	    }
13441e4925e8SNate Lawson 	}
13451e4925e8SNate Lawson     }
1346be2b1797SNate Lawson 
13471e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
13481e4925e8SNate Lawson     return (ret);
134915e32d5dSMike Smith }
135015e32d5dSMike Smith 
135115e32d5dSMike Smith /*
1352c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1353c5ba0be4SMike Smith  * or one if its parents.
1354c5ba0be4SMike Smith  */
1355c5ba0be4SMike Smith ACPI_STATUS
1356c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1357c5ba0be4SMike Smith {
1358c5ba0be4SMike Smith     ACPI_HANDLE		r;
1359c5ba0be4SMike Smith     ACPI_STATUS		status;
1360c5ba0be4SMike Smith 
1361cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1362cb9b0d80SMike Smith 
1363be2b1797SNate Lawson     /* Walk back up the tree to the root */
1364c5ba0be4SMike Smith     for (;;) {
1365be2b1797SNate Lawson 	status = AcpiGetHandle(parent, path, &r);
1366be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
1367c5ba0be4SMike Smith 	    *result = r;
1368c5ba0be4SMike Smith 	    return (AE_OK);
1369c5ba0be4SMike Smith 	}
1370c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1371c5ba0be4SMike Smith 	    return (AE_OK);
1372b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1373c5ba0be4SMike Smith 	    return (AE_NOT_FOUND);
1374c5ba0be4SMike Smith 	parent = r;
1375c5ba0be4SMike Smith     }
1376c5ba0be4SMike Smith }
1377c5ba0be4SMike Smith 
1378c5ba0be4SMike Smith /*
1379c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1380c5ba0be4SMike Smith  */
1381c5ba0be4SMike Smith ACPI_BUFFER *
1382c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1383c5ba0be4SMike Smith {
1384c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1385c5ba0be4SMike Smith 
1386c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1387c5ba0be4SMike Smith 	return (NULL);
1388c5ba0be4SMike Smith     buf->Length = size;
1389c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1390c5ba0be4SMike Smith     return (buf);
1391c5ba0be4SMike Smith }
1392c5ba0be4SMike Smith 
1393c310653eSNate Lawson ACPI_STATUS
1394cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
1395c310653eSNate Lawson {
1396c310653eSNate Lawson     ACPI_OBJECT arg1;
1397c310653eSNate Lawson     ACPI_OBJECT_LIST args;
1398c310653eSNate Lawson 
1399c310653eSNate Lawson     ACPI_ASSERTLOCK;
1400c310653eSNate Lawson 
1401c310653eSNate Lawson     arg1.Type = ACPI_TYPE_INTEGER;
1402c310653eSNate Lawson     arg1.Integer.Value = number;
1403c310653eSNate Lawson     args.Count = 1;
1404c310653eSNate Lawson     args.Pointer = &arg1;
1405c310653eSNate Lawson 
1406c310653eSNate Lawson     return (AcpiEvaluateObject(handle, path, &args, NULL));
1407c310653eSNate Lawson }
1408c310653eSNate Lawson 
1409c5ba0be4SMike Smith /*
1410c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1411c5ba0be4SMike Smith  */
1412c5ba0be4SMike Smith ACPI_STATUS
1413cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
1414c5ba0be4SMike Smith {
1415be2b1797SNate Lawson     ACPI_STATUS	status;
1416c5ba0be4SMike Smith     ACPI_BUFFER	buf;
1417c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
1418c5ba0be4SMike Smith 
1419cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1420cb9b0d80SMike Smith 
1421c5ba0be4SMike Smith     if (handle == NULL)
1422c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
14230c7da7acSJohn Baldwin 
14240c7da7acSJohn Baldwin     /*
14250c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
14260c7da7acSJohn Baldwin      * a method that will return an Integer.
14270c7da7acSJohn Baldwin      */
1428c5ba0be4SMike Smith     buf.Pointer = &param;
1429c5ba0be4SMike Smith     buf.Length = sizeof(param);
1430be2b1797SNate Lawson     status = AcpiEvaluateObject(handle, path, NULL, &buf);
1431be2b1797SNate Lawson     if (ACPI_SUCCESS(status)) {
1432be2b1797SNate Lawson 	if (param.Type == ACPI_TYPE_INTEGER)
1433c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1434be2b1797SNate Lawson 	else
1435be2b1797SNate Lawson 	    status = AE_TYPE;
1436c5ba0be4SMike Smith     }
14370c7da7acSJohn Baldwin 
14380c7da7acSJohn Baldwin     /*
14390c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
14400c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
14410c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
14420c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
14430c7da7acSJohn Baldwin      *
14440c7da7acSJohn Baldwin      * This is a hack.
14450c7da7acSJohn Baldwin      */
1446be2b1797SNate Lawson     if (status == AE_BUFFER_OVERFLOW) {
1447b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1448be2b1797SNate Lawson 	    status = AE_NO_MEMORY;
14490c7da7acSJohn Baldwin 	} else {
1450be2b1797SNate Lawson 	    status = AcpiEvaluateObject(handle, path, NULL, &buf);
1451be2b1797SNate Lawson 	    if (ACPI_SUCCESS(status))
1452be2b1797SNate Lawson 		status = acpi_ConvertBufferToInteger(&buf, number);
14530c7da7acSJohn Baldwin 	    AcpiOsFree(buf.Pointer);
14540c7da7acSJohn Baldwin 	}
1455f97739daSNate Lawson     }
1456be2b1797SNate Lawson     return (status);
1457c5ba0be4SMike Smith }
1458c5ba0be4SMike Smith 
1459c573e654SMitsuru IWASAKI ACPI_STATUS
1460cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
1461c573e654SMitsuru IWASAKI {
1462c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
1463dba55fa2SNate Lawson     UINT8	*val;
1464c573e654SMitsuru IWASAKI     int		i;
1465c573e654SMitsuru IWASAKI 
1466c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
1467c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
1468c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
1469c573e654SMitsuru IWASAKI 	return (AE_OK);
1470c573e654SMitsuru IWASAKI     }
1471c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
1472c573e654SMitsuru IWASAKI 	return (AE_TYPE);
1473c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
1474c573e654SMitsuru IWASAKI 	return (AE_BAD_DATA);
1475be2b1797SNate Lawson 
1476c573e654SMitsuru IWASAKI     *number = 0;
1477dba55fa2SNate Lawson     val = p->Buffer.Pointer;
1478c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
1479dba55fa2SNate Lawson 	*number += val[i] << (i * 8);
1480c573e654SMitsuru IWASAKI     return (AE_OK);
1481c573e654SMitsuru IWASAKI }
1482c573e654SMitsuru IWASAKI 
1483c5ba0be4SMike Smith /*
1484c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1485c5ba0be4SMike Smith  * function for each element.
1486c5ba0be4SMike Smith  *
1487c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1488c5ba0be4SMike Smith  */
1489c5ba0be4SMike Smith ACPI_STATUS
1490be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
1491be2b1797SNate Lawson 	void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
1492c5ba0be4SMike Smith {
1493c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1494c5ba0be4SMike Smith     int		i;
1495c5ba0be4SMike Smith 
1496be2b1797SNate Lawson     if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1497c5ba0be4SMike Smith 	return (AE_BAD_PARAMETER);
1498c5ba0be4SMike Smith 
1499be2b1797SNate Lawson     /* Iterate over components */
1500be2b1797SNate Lawson     i = 0;
1501be2b1797SNate Lawson     comp = pkg->Package.Elements;
1502be2b1797SNate Lawson     for (; i < pkg->Package.Count; i++, comp++)
1503c5ba0be4SMike Smith 	func(comp, arg);
1504c5ba0be4SMike Smith 
1505c5ba0be4SMike Smith     return (AE_OK);
1506c5ba0be4SMike Smith }
1507c5ba0be4SMike Smith 
15086f69255bSMike Smith /*
15096f69255bSMike Smith  * Find the (index)th resource object in a set.
15106f69255bSMike Smith  */
15116f69255bSMike Smith ACPI_STATUS
15126d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
15136f69255bSMike Smith {
15146d63101aSMike Smith     ACPI_RESOURCE	*rp;
15156f69255bSMike Smith     int			i;
15166f69255bSMike Smith 
15176d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
15186f69255bSMike Smith     i = index;
15196d63101aSMike Smith     while (i-- > 0) {
1520be2b1797SNate Lawson 	/* Range check */
15216d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
15226f69255bSMike Smith 	    return (AE_BAD_PARAMETER);
1523be2b1797SNate Lawson 
1524be2b1797SNate Lawson 	/* Check for terminator */
1525be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
15266d63101aSMike Smith 	    return (AE_NOT_FOUND);
1527abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
15286f69255bSMike Smith     }
15296f69255bSMike Smith     if (resp != NULL)
15306d63101aSMike Smith 	*resp = rp;
1531be2b1797SNate Lawson 
15326d63101aSMike Smith     return (AE_OK);
15336d63101aSMike Smith }
15346d63101aSMike Smith 
15356d63101aSMike Smith /*
15366d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
15376d63101aSMike Smith  *
15386d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
15396d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
15406d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
15416d63101aSMike Smith  * resources.
15426d63101aSMike Smith  */
15436d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
15446d63101aSMike Smith 
15456d63101aSMike Smith ACPI_STATUS
15466d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
15476d63101aSMike Smith {
15486d63101aSMike Smith     ACPI_RESOURCE	*rp;
15496d63101aSMike Smith     void		*newp;
15506d63101aSMike Smith 
1551be2b1797SNate Lawson     /* Initialise the buffer if necessary. */
15526d63101aSMike Smith     if (buf->Pointer == NULL) {
15536d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
15546d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
15556d63101aSMike Smith 	    return (AE_NO_MEMORY);
15566d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
15576d63101aSMike Smith 	rp->Id = ACPI_RSTYPE_END_TAG;
15586d63101aSMike Smith 	rp->Length = 0;
15596d63101aSMike Smith     }
15606d63101aSMike Smith     if (res == NULL)
15616d63101aSMike Smith 	return (AE_OK);
15626d63101aSMike Smith 
15636d63101aSMike Smith     /*
15646d63101aSMike Smith      * Scan the current buffer looking for the terminator.
15656d63101aSMike Smith      * This will either find the terminator or hit the end
15666d63101aSMike Smith      * of the buffer and return an error.
15676d63101aSMike Smith      */
15686d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
15696d63101aSMike Smith     for (;;) {
1570be2b1797SNate Lawson 	/* Range check, don't go outside the buffer */
15716d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
15726d63101aSMike Smith 	    return (AE_BAD_PARAMETER);
1573be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
15746d63101aSMike Smith 	    break;
1575abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
15766d63101aSMike Smith     }
15776d63101aSMike Smith 
15786d63101aSMike Smith     /*
15796d63101aSMike Smith      * Check the size of the buffer and expand if required.
15806d63101aSMike Smith      *
15816d63101aSMike Smith      * Required size is:
15826d63101aSMike Smith      *	size of existing resources before terminator +
15836d63101aSMike Smith      *	size of new resource and header +
15846d63101aSMike Smith      * 	size of terminator.
15856d63101aSMike Smith      *
15866d63101aSMike Smith      * Note that this loop should really only run once, unless
15876d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
15886d63101aSMike Smith      */
15896d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
15906d63101aSMike Smith 	    res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
15916d63101aSMike Smith 	    ACPI_RESOURCE_LENGTH) >= buf->Length) {
15926d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
15936d63101aSMike Smith 	    return (AE_NO_MEMORY);
15946d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
1595a692219dSMike Smith 	rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1596a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
15976d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
15986d63101aSMike Smith 	buf->Pointer = newp;
15996d63101aSMike Smith 	buf->Length += buf->Length;
16006d63101aSMike Smith     }
16016d63101aSMike Smith 
1602be2b1797SNate Lawson     /* Insert the new resource. */
16036d63101aSMike Smith     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
16046d63101aSMike Smith 
1605be2b1797SNate Lawson     /* And add the terminator. */
1606abcbc5bcSNate Lawson     rp = ACPI_NEXT_RESOURCE(rp);
16076d63101aSMike Smith     rp->Id = ACPI_RSTYPE_END_TAG;
16086d63101aSMike Smith     rp->Length = 0;
16096d63101aSMike Smith 
16106f69255bSMike Smith     return (AE_OK);
16116f69255bSMike Smith }
1612c5ba0be4SMike Smith 
1613da14ac9fSJohn Baldwin /*
1614da14ac9fSJohn Baldwin  * Set interrupt model.
1615da14ac9fSJohn Baldwin  */
1616da14ac9fSJohn Baldwin ACPI_STATUS
1617da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
1618da14ac9fSJohn Baldwin {
1619da14ac9fSJohn Baldwin 
1620c310653eSNate Lawson     return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
1621da14ac9fSJohn Baldwin }
1622da14ac9fSJohn Baldwin 
1623ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME	5
1624ece50487SMitsuru IWASAKI 
1625ece50487SMitsuru IWASAKI static void
1626ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
1627ece50487SMitsuru IWASAKI {
1628ece50487SMitsuru IWASAKI     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1629ece50487SMitsuru IWASAKI }
1630c30382dfSMitsuru IWASAKI 
163115e32d5dSMike Smith /*
163215e32d5dSMike Smith  * Set the system sleep state
163315e32d5dSMike Smith  *
1634be2b1797SNate Lawson  * Currently we support S1-S5 but S4 is only S4BIOS
163515e32d5dSMike Smith  */
163615e32d5dSMike Smith ACPI_STATUS
163715e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
163815e32d5dSMike Smith {
163915e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
164056d8cb57SMitsuru IWASAKI     UINT8	TypeA;
164156d8cb57SMitsuru IWASAKI     UINT8	TypeB;
164215e32d5dSMike Smith 
1643b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1644cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
16450ae55423SMike Smith 
164698175614SNate Lawson     /* Avoid reentry if already attempting to suspend. */
16476397624dSMitsuru IWASAKI     if (sc->acpi_sstate != ACPI_STATE_S0)
164898175614SNate Lawson 	return_ACPI_STATUS (AE_BAD_PARAMETER);
16496397624dSMitsuru IWASAKI 
165098175614SNate Lawson     /* We recently woke up so don't suspend again for a while. */
1651ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1652ece50487SMitsuru IWASAKI 	return_ACPI_STATUS (AE_OK);
1653ece50487SMitsuru IWASAKI 
165415e32d5dSMike Smith     switch (state) {
165515e32d5dSMike Smith     case ACPI_STATE_S1:
16566161544cSTakanori Watanabe     case ACPI_STATE_S2:
16576161544cSTakanori Watanabe     case ACPI_STATE_S3:
16586161544cSTakanori Watanabe     case ACPI_STATE_S4:
1659be2b1797SNate Lawson 	status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB);
166098175614SNate Lawson 	if (status == AE_NOT_FOUND) {
166198175614SNate Lawson 	    device_printf(sc->acpi_dev,
166298175614SNate Lawson 			  "Sleep state S%d not supported by BIOS\n", state);
166398175614SNate Lawson 	    break;
166498175614SNate Lawson 	} else if (ACPI_FAILURE(status)) {
1665be2b1797SNate Lawson 	    device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n",
1666be2b1797SNate Lawson 			  AcpiFormatException(status));
166756d8cb57SMitsuru IWASAKI 	    break;
166856d8cb57SMitsuru IWASAKI 	}
166956d8cb57SMitsuru IWASAKI 
1670a1fccb47SMitsuru IWASAKI 	sc->acpi_sstate = state;
1671a1fccb47SMitsuru IWASAKI 	sc->acpi_sleep_disabled = 1;
1672a1fccb47SMitsuru IWASAKI 
1673be2b1797SNate Lawson 	/* Inform all devices that we are going to sleep. */
167415e32d5dSMike Smith 	if (DEVICE_SUSPEND(root_bus) != 0) {
167515e32d5dSMike Smith 	    /*
167615e32d5dSMike Smith 	     * Re-wake the system.
167715e32d5dSMike Smith 	     *
167815e32d5dSMike Smith 	     * XXX note that a better two-pass approach with a 'veto' pass
167915e32d5dSMike Smith 	     *     followed by a "real thing" pass would be better, but the
168015e32d5dSMike Smith 	     *     current bus interface does not provide for this.
168115e32d5dSMike Smith 	     */
168215e32d5dSMike Smith 	    DEVICE_RESUME(root_bus);
16830ae55423SMike Smith 	    return_ACPI_STATUS (AE_ERROR);
168415e32d5dSMike Smith 	}
1685b9c780d6SMitsuru IWASAKI 
1686be2b1797SNate Lawson 	status = AcpiEnterSleepStatePrep(state);
1687be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1688b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1689b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
1690b9c780d6SMitsuru IWASAKI 	    break;
1691b9c780d6SMitsuru IWASAKI 	}
1692b9c780d6SMitsuru IWASAKI 
1693be2b1797SNate Lawson 	if (sc->acpi_sleep_delay > 0)
1694ff01efb5SMitsuru IWASAKI 	    DELAY(sc->acpi_sleep_delay * 1000000);
1695ff01efb5SMitsuru IWASAKI 
16966161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
16976161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
16986161544cSTakanori Watanabe 
1699be2b1797SNate Lawson 	    /* AcpiEnterSleepState() may be incomplete, unlock if locked. */
170059ddeb18SNate Lawson 	    if (AcpiGbl_MutexInfo[ACPI_MTX_HARDWARE].OwnerId !=
170159ddeb18SNate Lawson 		ACPI_MUTEX_NOT_ACQUIRED) {
170259ddeb18SNate Lawson 
17036161544cSTakanori Watanabe 		AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
1704a1fccb47SMitsuru IWASAKI 	    }
17056161544cSTakanori Watanabe 
17066161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1707be2b1797SNate Lawson 	    if (state == ACPI_STATE_S4)
1708964679ceSMitsuru IWASAKI 		AcpiEnable();
17096161544cSTakanori Watanabe 	} else {
1710be2b1797SNate Lawson 	    status = AcpiEnterSleepState((UINT8)state);
1711be2b1797SNate Lawson 	    if (ACPI_FAILURE(status)) {
1712be2b1797SNate Lawson 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
1713be2b1797SNate Lawson 			      AcpiFormatException(status));
1714c30382dfSMitsuru IWASAKI 		break;
171515e32d5dSMike Smith 	    }
17166161544cSTakanori Watanabe 	}
1717ff741befSTakanori Watanabe 	AcpiLeaveSleepState((UINT8)state);
171815e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
171915e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
172013d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
172115e32d5dSMike Smith 	break;
172215e32d5dSMike Smith     case ACPI_STATE_S5:
172315e32d5dSMike Smith 	/*
172415e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
172515e32d5dSMike Smith 	 * shutdown handlers.
172615e32d5dSMike Smith 	 */
172715e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
172815e32d5dSMike Smith 	break;
172998175614SNate Lawson     case ACPI_STATE_S0:
173015e32d5dSMike Smith     default:
173115e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
173215e32d5dSMike Smith 	break;
173315e32d5dSMike Smith     }
1734ece50487SMitsuru IWASAKI 
173598175614SNate Lawson     /* Disable a second sleep request for a short period */
1736ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1737ece50487SMitsuru IWASAKI 	timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1738ece50487SMitsuru IWASAKI 
17390ae55423SMike Smith     return_ACPI_STATUS (status);
174015e32d5dSMike Smith }
174115e32d5dSMike Smith 
174215e32d5dSMike Smith /*
174315e32d5dSMike Smith  * Enable/Disable ACPI
174415e32d5dSMike Smith  */
174515e32d5dSMike Smith ACPI_STATUS
174615e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
174715e32d5dSMike Smith {
174815e32d5dSMike Smith     ACPI_STATUS	status;
174915e32d5dSMike Smith     u_int32_t	flags;
175015e32d5dSMike Smith 
1751b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1752cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
17530ae55423SMike Smith 
175415e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
175515e32d5dSMike Smith 	    ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
1756be2b1797SNate Lawson     if (!sc->acpi_enabled)
175715e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
1758be2b1797SNate Lawson     else
175915e32d5dSMike Smith 	status = AE_OK;
1760be2b1797SNate Lawson 
176115e32d5dSMike Smith     if (status == AE_OK)
176215e32d5dSMike Smith 	sc->acpi_enabled = 1;
1763be2b1797SNate Lawson 
17640ae55423SMike Smith     return_ACPI_STATUS (status);
176515e32d5dSMike Smith }
176615e32d5dSMike Smith 
176715e32d5dSMike Smith ACPI_STATUS
176815e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
176915e32d5dSMike Smith {
177015e32d5dSMike Smith     ACPI_STATUS	status;
177115e32d5dSMike Smith 
1772b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1773cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
17740ae55423SMike Smith 
1775be2b1797SNate Lawson     if (sc->acpi_enabled)
177615e32d5dSMike Smith 	status = AcpiDisable();
1777be2b1797SNate Lawson     else
177815e32d5dSMike Smith 	status = AE_OK;
1779be2b1797SNate Lawson 
178015e32d5dSMike Smith     if (status == AE_OK)
178115e32d5dSMike Smith 	sc->acpi_enabled = 0;
1782be2b1797SNate Lawson 
17830ae55423SMike Smith     return_ACPI_STATUS (status);
178415e32d5dSMike Smith }
178515e32d5dSMike Smith 
178615e32d5dSMike Smith /*
178715e32d5dSMike Smith  * ACPI Event Handlers
178815e32d5dSMike Smith  */
178915e32d5dSMike Smith 
179015e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
179115e32d5dSMike Smith 
179215e32d5dSMike Smith static void
179315e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
179415e32d5dSMike Smith {
1795fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1796b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
179715e32d5dSMike Smith 
1798cb9b0d80SMike Smith     ACPI_LOCK;
1799a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
180015e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
1801cb9b0d80SMike Smith     ACPI_UNLOCK;
18020ae55423SMike Smith     return_VOID;
180315e32d5dSMike Smith }
180415e32d5dSMike Smith 
180515e32d5dSMike Smith static void
180615e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
180715e32d5dSMike Smith {
1808fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1809b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
18100ae55423SMike Smith 
181115e32d5dSMike Smith     /* Well, what to do? :-) */
18120ae55423SMike Smith 
1813cb9b0d80SMike Smith     ACPI_LOCK;
1814cb9b0d80SMike Smith     ACPI_UNLOCK;
1815cb9b0d80SMike Smith 
18160ae55423SMike Smith     return_VOID;
181715e32d5dSMike Smith }
181815e32d5dSMike Smith 
181915e32d5dSMike Smith /*
182015e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
182115e32d5dSMike Smith  */
182215e32d5dSMike Smith UINT32
182333febf93SNate Lawson acpi_event_power_button_sleep(void *context)
182415e32d5dSMike Smith {
182515e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
182615e32d5dSMike Smith 
1827b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
18280ae55423SMike Smith 
182915e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
18300ae55423SMike Smith 
1831b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
183215e32d5dSMike Smith }
183315e32d5dSMike Smith 
183415e32d5dSMike Smith UINT32
183533febf93SNate Lawson acpi_event_power_button_wake(void *context)
183615e32d5dSMike Smith {
183715e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
183815e32d5dSMike Smith 
1839b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
18400ae55423SMike Smith 
184115e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
18420ae55423SMike Smith 
1843b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
184415e32d5dSMike Smith }
184515e32d5dSMike Smith 
184615e32d5dSMike Smith UINT32
184733febf93SNate Lawson acpi_event_sleep_button_sleep(void *context)
184815e32d5dSMike Smith {
184915e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
185015e32d5dSMike Smith 
1851b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
18520ae55423SMike Smith 
185315e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
18540ae55423SMike Smith 
1855b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
185615e32d5dSMike Smith }
185715e32d5dSMike Smith 
185815e32d5dSMike Smith UINT32
185933febf93SNate Lawson acpi_event_sleep_button_wake(void *context)
186015e32d5dSMike Smith {
186115e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
186215e32d5dSMike Smith 
1863b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
18640ae55423SMike Smith 
186515e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
18660ae55423SMike Smith 
1867b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
186815e32d5dSMike Smith }
186915e32d5dSMike Smith 
187015e32d5dSMike Smith /*
187115e32d5dSMike Smith  * XXX This is kinda ugly, and should not be here.
187215e32d5dSMike Smith  */
187315e32d5dSMike Smith struct acpi_staticbuf {
187415e32d5dSMike Smith     ACPI_BUFFER	buffer;
187515e32d5dSMike Smith     char	data[512];
187615e32d5dSMike Smith };
187715e32d5dSMike Smith 
187815e32d5dSMike Smith char *
187915e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
188015e32d5dSMike Smith {
188115e32d5dSMike Smith     static struct acpi_staticbuf	buf;
188215e32d5dSMike Smith 
1883cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1884cb9b0d80SMike Smith 
188515e32d5dSMike Smith     buf.buffer.Length = 512;
188615e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
188715e32d5dSMike Smith 
1888b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
188915e32d5dSMike Smith 	return (buf.buffer.Pointer);
1890be2b1797SNate Lawson 
189115e32d5dSMike Smith     return ("(unknown path)");
189215e32d5dSMike Smith }
189315e32d5dSMike Smith 
189415e32d5dSMike Smith /*
189515e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
189615e32d5dSMike Smith  * parts of the namespace.
189715e32d5dSMike Smith  */
189815e32d5dSMike Smith int
189915e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
190015e32d5dSMike Smith {
19013c600cfbSJohn Baldwin     char	*cp, *env, *np;
190215e32d5dSMike Smith     int		len;
190315e32d5dSMike Smith 
190415e32d5dSMike Smith     np = acpi_name(handle);
190515e32d5dSMike Smith     if (*np == '\\')
190615e32d5dSMike Smith 	np++;
19073c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
190815e32d5dSMike Smith 	return (0);
190915e32d5dSMike Smith 
1910be2b1797SNate Lawson     /* Scan the avoid list checking for a match */
19113c600cfbSJohn Baldwin     cp = env;
191215e32d5dSMike Smith     for (;;) {
191315e32d5dSMike Smith 	while ((*cp != 0) && isspace(*cp))
191415e32d5dSMike Smith 	    cp++;
191515e32d5dSMike Smith 	if (*cp == 0)
191615e32d5dSMike Smith 	    break;
191715e32d5dSMike Smith 	len = 0;
191815e32d5dSMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
191915e32d5dSMike Smith 	    len++;
1920d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
19213c600cfbSJohn Baldwin 	    freeenv(env);
19220ae55423SMike Smith 	    return(1);
1923d786139cSMaxime Henrion 	}
19240ae55423SMike Smith 	cp += len;
19250ae55423SMike Smith     }
19263c600cfbSJohn Baldwin     freeenv(env);
1927be2b1797SNate Lawson 
19280ae55423SMike Smith     return (0);
19290ae55423SMike Smith }
19300ae55423SMike Smith 
19310ae55423SMike Smith /*
19320ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
19330ae55423SMike Smith  */
19340ae55423SMike Smith int
19350ae55423SMike Smith acpi_disabled(char *subsys)
19360ae55423SMike Smith {
193778689b15SMaxime Henrion     char	*cp, *env;
19380ae55423SMike Smith     int		len;
19390ae55423SMike Smith 
19403184cf5aSNate Lawson     if ((env = getenv("debug.acpi.disabled")) == NULL)
19410ae55423SMike Smith 	return (0);
19423184cf5aSNate Lawson     if (strcmp(env, "all") == 0) {
194378689b15SMaxime Henrion 	freeenv(env);
19440ae55423SMike Smith 	return (1);
1945d786139cSMaxime Henrion     }
19460ae55423SMike Smith 
19473184cf5aSNate Lawson     /* Scan the disable list, checking for a match. */
194878689b15SMaxime Henrion     cp = env;
19490ae55423SMike Smith     for (;;) {
19503184cf5aSNate Lawson 	while (*cp != '\0' && isspace(*cp))
19510ae55423SMike Smith 	    cp++;
19523184cf5aSNate Lawson 	if (*cp == '\0')
19530ae55423SMike Smith 	    break;
19540ae55423SMike Smith 	len = 0;
19553184cf5aSNate Lawson 	while (cp[len] != '\0' && !isspace(cp[len]))
19560ae55423SMike Smith 	    len++;
19573184cf5aSNate Lawson 	if (strncmp(cp, subsys, len) == 0) {
195878689b15SMaxime Henrion 	    freeenv(env);
195915e32d5dSMike Smith 	    return (1);
1960d786139cSMaxime Henrion 	}
196115e32d5dSMike Smith 	cp += len;
196215e32d5dSMike Smith     }
196378689b15SMaxime Henrion     freeenv(env);
1964be2b1797SNate Lawson 
196515e32d5dSMike Smith     return (0);
196615e32d5dSMike Smith }
196715e32d5dSMike Smith 
196815e32d5dSMike Smith /*
1969a1fccb47SMitsuru IWASAKI  * Device wake capability enable/disable.
1970a1fccb47SMitsuru IWASAKI  */
1971a1fccb47SMitsuru IWASAKI void
1972a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable)
1973a1fccb47SMitsuru IWASAKI {
1974a1fccb47SMitsuru IWASAKI     /*
1975a1fccb47SMitsuru IWASAKI      * TBD: All Power Resources referenced by elements 2 through N
1976a1fccb47SMitsuru IWASAKI      *      of the _PRW object are put into the ON state.
1977a1fccb47SMitsuru IWASAKI      */
1978a1fccb47SMitsuru IWASAKI 
1979c310653eSNate Lawson     (void)acpi_SetInteger(h, "_PSW", enable);
1980a1fccb47SMitsuru IWASAKI }
1981a1fccb47SMitsuru IWASAKI 
1982a1fccb47SMitsuru IWASAKI void
1983a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_event(ACPI_HANDLE h)
1984a1fccb47SMitsuru IWASAKI {
1985a1fccb47SMitsuru IWASAKI     struct acpi_softc		*sc;
1986a1fccb47SMitsuru IWASAKI     ACPI_STATUS			status;
1987a1fccb47SMitsuru IWASAKI     ACPI_BUFFER			prw_buffer;
1988a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			*res;
1989a1fccb47SMitsuru IWASAKI 
1990a1fccb47SMitsuru IWASAKI     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1991a1fccb47SMitsuru IWASAKI 
1992be2b1797SNate Lawson     sc = devclass_get_softc(acpi_devclass, 0);
1993be2b1797SNate Lawson     if (sc == NULL)
1994a1fccb47SMitsuru IWASAKI 	return;
1995a1fccb47SMitsuru IWASAKI 
1996a1fccb47SMitsuru IWASAKI     /*
1997a1fccb47SMitsuru IWASAKI      * _PRW object is only required for devices that have the ability
1998a1fccb47SMitsuru IWASAKI      * to wake the system from a system sleeping state.
1999a1fccb47SMitsuru IWASAKI      */
2000a1fccb47SMitsuru IWASAKI     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
2001a1fccb47SMitsuru IWASAKI     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
2002be2b1797SNate Lawson     if (ACPI_FAILURE(status))
2003a1fccb47SMitsuru IWASAKI 	return;
2004a1fccb47SMitsuru IWASAKI 
2005a1fccb47SMitsuru IWASAKI     res = (ACPI_OBJECT *)prw_buffer.Pointer;
2006be2b1797SNate Lawson     if (res == NULL)
2007a1fccb47SMitsuru IWASAKI 	return;
2008a1fccb47SMitsuru IWASAKI 
2009a1fccb47SMitsuru IWASAKI     if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) {
2010a1fccb47SMitsuru IWASAKI 	goto out;
2011a1fccb47SMitsuru IWASAKI     }
2012a1fccb47SMitsuru IWASAKI 
2013a1fccb47SMitsuru IWASAKI     /*
2014a1fccb47SMitsuru IWASAKI      * The element 1 of the _PRW object:
2015a1fccb47SMitsuru IWASAKI      * The lowest power system sleeping state that can be entered
2016a1fccb47SMitsuru IWASAKI      * while still providing wake functionality.
2017a1fccb47SMitsuru IWASAKI      * The sleeping state being entered must be greater or equal to
2018a1fccb47SMitsuru IWASAKI      * the power state declared in element 1 of the _PRW object.
2019a1fccb47SMitsuru IWASAKI      */
2020be2b1797SNate Lawson     if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER)
2021a1fccb47SMitsuru IWASAKI 	goto out;
2022a1fccb47SMitsuru IWASAKI 
2023be2b1797SNate Lawson     if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value)
2024a1fccb47SMitsuru IWASAKI 	goto out;
2025a1fccb47SMitsuru IWASAKI 
2026a1fccb47SMitsuru IWASAKI     /*
2027a1fccb47SMitsuru IWASAKI      * The element 0 of the _PRW object:
2028a1fccb47SMitsuru IWASAKI      */
2029a1fccb47SMitsuru IWASAKI     switch(res->Package.Elements[0].Type) {
2030a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_INTEGER:
2031a1fccb47SMitsuru IWASAKI 	/*
2032a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is numeric, then this
2033a1fccb47SMitsuru IWASAKI 	 * _PRW package element is the bit index in the GPEx_EN, in the
2034a1fccb47SMitsuru IWASAKI 	 * GPE blocks described in the FADT, of the enable bit that is
2035a1fccb47SMitsuru IWASAKI 	 * enabled for the wake event.
2036a1fccb47SMitsuru IWASAKI 	 */
2037a1fccb47SMitsuru IWASAKI 
20386fca9360SNate Lawson 	status = AcpiEnableGpe(NULL, res->Package.Elements[0].Integer.Value,
20396fca9360SNate Lawson 			       ACPI_EVENT_WAKE_ENABLE);
2040a1fccb47SMitsuru IWASAKI 	if (ACPI_FAILURE(status))
2041a1fccb47SMitsuru IWASAKI 	    printf("%s: EnableEvent Failed\n", __func__);
2042a1fccb47SMitsuru IWASAKI 	break;
2043a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_PACKAGE:
2044a1fccb47SMitsuru IWASAKI 	/*
2045be2b1797SNate Lawson 	 * XXX TBD
2046be2b1797SNate Lawson 	 *
2047a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is a package, then this
2048a1fccb47SMitsuru IWASAKI 	 * _PRW package element is itself a package containing two
2049a1fccb47SMitsuru IWASAKI 	 * elements. The first is an object reference to the GPE Block
2050a1fccb47SMitsuru IWASAKI 	 * device that contains the GPE that will be triggered by the wake
2051a1fccb47SMitsuru IWASAKI 	 * event. The second element is numeric and it contains the bit
2052a1fccb47SMitsuru IWASAKI 	 * index in the GPEx_EN, in the GPE Block referenced by the
2053a1fccb47SMitsuru IWASAKI 	 * first element in the package, of the enable bit that is enabled for
2054a1fccb47SMitsuru IWASAKI 	 * the wake event.
2055a1fccb47SMitsuru IWASAKI 	 * For example, if this field is a package then it is of the form:
2056a1fccb47SMitsuru IWASAKI 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
2057a1fccb47SMitsuru IWASAKI 	 */
2058a1fccb47SMitsuru IWASAKI 	break;
2059a1fccb47SMitsuru IWASAKI     default:
2060a1fccb47SMitsuru IWASAKI 	break;
2061a1fccb47SMitsuru IWASAKI     }
2062a1fccb47SMitsuru IWASAKI 
2063a1fccb47SMitsuru IWASAKI out:
2064a1fccb47SMitsuru IWASAKI     if (prw_buffer.Pointer != NULL)
2065a1fccb47SMitsuru IWASAKI 	AcpiOsFree(prw_buffer.Pointer);
2066a1fccb47SMitsuru IWASAKI }
2067a1fccb47SMitsuru IWASAKI 
2068a1fccb47SMitsuru IWASAKI /*
206915e32d5dSMike Smith  * Control interface.
207015e32d5dSMike Smith  *
20710ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
2072be2b1797SNate Lawson  * drivers wanting to be accessible via /dev/acpi should use the
2073be2b1797SNate Lawson  * register/deregister interface to make their handlers visible.
207415e32d5dSMike Smith  */
20750ae55423SMike Smith struct acpi_ioctl_hook
20760ae55423SMike Smith {
20770ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook) link;
20780ae55423SMike Smith     u_long			 cmd;
2079be2b1797SNate Lawson     acpi_ioctl_fn		 fn;
20800ae55423SMike Smith     void			 *arg;
20810ae55423SMike Smith };
20820ae55423SMike Smith 
20830ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
20840ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
20850ae55423SMike Smith 
20860ae55423SMike Smith /*
20870ae55423SMike Smith  * Register an ioctl handler.
20880ae55423SMike Smith  */
20890ae55423SMike Smith int
2090be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
20910ae55423SMike Smith {
20920ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
20930ae55423SMike Smith 
20940ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
20950ae55423SMike Smith 	return (ENOMEM);
20960ae55423SMike Smith     hp->cmd = cmd;
20970ae55423SMike Smith     hp->fn = fn;
20980ae55423SMike Smith     hp->arg = arg;
20990ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
21000ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
21010ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
21020ae55423SMike Smith     }
21030ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
21040ae55423SMike Smith     return (0);
21050ae55423SMike Smith }
21060ae55423SMike Smith 
21070ae55423SMike Smith /*
21080ae55423SMike Smith  * Deregister an ioctl handler.
21090ae55423SMike Smith  */
21100ae55423SMike Smith void
2111be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
21120ae55423SMike Smith {
21130ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
21140ae55423SMike Smith 
21150ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
21160ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
21170ae55423SMike Smith 	    break;
21180ae55423SMike Smith 
21190ae55423SMike Smith     if (hp != NULL) {
21200ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
21210ae55423SMike Smith 	free(hp, M_ACPIDEV);
21220ae55423SMike Smith     }
21230ae55423SMike Smith }
21240ae55423SMike Smith 
212515e32d5dSMike Smith static int
21266b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td)
212715e32d5dSMike Smith {
212815e32d5dSMike Smith     return (0);
212915e32d5dSMike Smith }
213015e32d5dSMike Smith 
213115e32d5dSMike Smith static int
21326b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td)
213315e32d5dSMike Smith {
213415e32d5dSMike Smith     return (0);
213515e32d5dSMike Smith }
213615e32d5dSMike Smith 
213715e32d5dSMike Smith static int
21386b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
213915e32d5dSMike Smith {
214015e32d5dSMike Smith     struct acpi_softc		*sc;
21410ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
21420ae55423SMike Smith     int				error, xerror, state;
2143fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
214415e32d5dSMike Smith 
2145cb9b0d80SMike Smith     ACPI_LOCK;
2146cb9b0d80SMike Smith 
214715e32d5dSMike Smith     error = state = 0;
214815e32d5dSMike Smith     sc = dev->si_drv1;
214915e32d5dSMike Smith 
21500ae55423SMike Smith     /*
21510ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
21520ae55423SMike Smith      */
21530ae55423SMike Smith     if (acpi_ioctl_hooks_initted) {
21540ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
21550ae55423SMike Smith 	    if (hp->cmd == cmd) {
21560ae55423SMike Smith 		xerror = hp->fn(cmd, addr, hp->arg);
21570ae55423SMike Smith 		if (xerror != 0)
21580ae55423SMike Smith 		    error = xerror;
2159917d44c8SMitsuru IWASAKI 		goto out;
21600ae55423SMike Smith 	    }
21610ae55423SMike Smith 	}
21620ae55423SMike Smith     }
21630ae55423SMike Smith 
21640ae55423SMike Smith     /*
2165a89fcf28STakanori Watanabe      * Core ioctls are not permitted for non-writable user.
2166a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
2167a89fcf28STakanori Watanabe      * Not changing system behavior.
2168a89fcf28STakanori Watanabe      */
2169be2b1797SNate Lawson     if((flag & FWRITE) == 0)
2170be2b1797SNate Lawson 	return (EPERM);
2171a89fcf28STakanori Watanabe 
2172be2b1797SNate Lawson     /* Core system ioctls. */
217315e32d5dSMike Smith     switch (cmd) {
217415e32d5dSMike Smith     case ACPIIO_ENABLE:
21750ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
217615e32d5dSMike Smith 	    error = ENXIO;
217715e32d5dSMike Smith 	break;
217815e32d5dSMike Smith     case ACPIIO_DISABLE:
21790ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
218015e32d5dSMike Smith 	    error = ENXIO;
218115e32d5dSMike Smith 	break;
218215e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
218315e32d5dSMike Smith 	if (!sc->acpi_enabled) {
218415e32d5dSMike Smith 	    error = ENXIO;
218515e32d5dSMike Smith 	    break;
218615e32d5dSMike Smith 	}
218715e32d5dSMike Smith 	state = *(int *)addr;
21882d610c46SNate Lawson 	if (state >= ACPI_STATE_S0  && state <= ACPI_S_STATES_MAX) {
21892d610c46SNate Lawson 	    if (ACPI_FAILURE(acpi_SetSleepState(sc, state)))
219015e32d5dSMike Smith 		error = EINVAL;
21912d610c46SNate Lawson 	} else {
21922d610c46SNate Lawson 	    error = EINVAL;
21932d610c46SNate Lawson 	}
219415e32d5dSMike Smith 	break;
219515e32d5dSMike Smith     default:
21960ae55423SMike Smith 	if (error == 0)
219715e32d5dSMike Smith 	    error = EINVAL;
219815e32d5dSMike Smith 	break;
219915e32d5dSMike Smith     }
2200917d44c8SMitsuru IWASAKI 
2201917d44c8SMitsuru IWASAKI out:
2202cb9b0d80SMike Smith     ACPI_UNLOCK;
220315e32d5dSMike Smith     return (error);
220415e32d5dSMike Smith }
220515e32d5dSMike Smith 
22061d073b1dSJohn Baldwin static int
2207d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2208d75de536SMitsuru IWASAKI {
2209d75de536SMitsuru IWASAKI     char sleep_state[4];
2210d75de536SMitsuru IWASAKI     char buf[16];
2211d75de536SMitsuru IWASAKI     int error;
2212d75de536SMitsuru IWASAKI     UINT8 state, TypeA, TypeB;
2213d75de536SMitsuru IWASAKI 
2214d75de536SMitsuru IWASAKI     buf[0] = '\0';
2215d75de536SMitsuru IWASAKI     for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX+1; state++) {
2216d75de536SMitsuru IWASAKI 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
2217d75de536SMitsuru IWASAKI 	    sprintf(sleep_state, "S%d ", state);
2218d75de536SMitsuru IWASAKI 	    strcat(buf, sleep_state);
2219d75de536SMitsuru IWASAKI 	}
2220d75de536SMitsuru IWASAKI     }
2221d75de536SMitsuru IWASAKI     error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2222d75de536SMitsuru IWASAKI     return (error);
2223d75de536SMitsuru IWASAKI }
2224d75de536SMitsuru IWASAKI 
2225d75de536SMitsuru IWASAKI static int
22261d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
22271d073b1dSJohn Baldwin {
22281d073b1dSJohn Baldwin     char sleep_state[10];
22291d073b1dSJohn Baldwin     int error;
22301d073b1dSJohn Baldwin     u_int new_state, old_state;
22311d073b1dSJohn Baldwin 
22321d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
2233b8670bedSMitsuru IWASAKI     if (old_state > ACPI_S_STATES_MAX+1) {
22341d073b1dSJohn Baldwin 	strcpy(sleep_state, "unknown");
2235cb9b0d80SMike Smith     } else {
2236b8670bedSMitsuru IWASAKI 	bzero(sleep_state, sizeof(sleep_state));
22371d073b1dSJohn Baldwin 	strncpy(sleep_state, sleep_state_names[old_state],
22381d073b1dSJohn Baldwin 		sizeof(sleep_state_names[old_state]));
2239cb9b0d80SMike Smith     }
22401d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
22411d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
2242be2b1797SNate Lawson 	new_state = ACPI_STATE_S0;
2243be2b1797SNate Lawson 	for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) {
22441d073b1dSJohn Baldwin 	    if (strncmp(sleep_state, sleep_state_names[new_state],
22451d073b1dSJohn Baldwin 			sizeof(sleep_state)) == 0)
22461d073b1dSJohn Baldwin 		break;
2247cb9b0d80SMike Smith 	}
2248931a10c9SMitsuru IWASAKI 	if (new_state <= ACPI_S_STATES_MAX + 1) {
2249be2b1797SNate Lawson 	    if (new_state != old_state)
22501d073b1dSJohn Baldwin 		*(u_int *)oidp->oid_arg1 = new_state;
2251cb9b0d80SMike Smith 	} else {
22521d073b1dSJohn Baldwin 	    error = EINVAL;
22531d073b1dSJohn Baldwin 	}
2254cb9b0d80SMike Smith     }
2255be2b1797SNate Lawson 
22561d073b1dSJohn Baldwin     return (error);
22571d073b1dSJohn Baldwin }
22581d073b1dSJohn Baldwin 
22599b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */
22609b937d48SNate Lawson void
22619b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
22629b937d48SNate Lawson {
22639b937d48SNate Lawson     char		notify_buf[16];
22649b937d48SNate Lawson     ACPI_BUFFER		handle_buf;
22659b937d48SNate Lawson     ACPI_STATUS		status;
22669b937d48SNate Lawson 
22679b937d48SNate Lawson     if (subsystem == NULL)
22689b937d48SNate Lawson 	return;
22699b937d48SNate Lawson 
22709b937d48SNate Lawson     handle_buf.Pointer = NULL;
22719b937d48SNate Lawson     handle_buf.Length = ACPI_ALLOCATE_BUFFER;
22729b937d48SNate Lawson     status = AcpiNsHandleToPathname(h, &handle_buf);
22739b937d48SNate Lawson     if (ACPI_FAILURE(status))
22749b937d48SNate Lawson 	return;
22759b937d48SNate Lawson     snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
22769b937d48SNate Lawson     devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
22779b937d48SNate Lawson     AcpiOsFree(handle_buf.Pointer);
22789b937d48SNate Lawson }
22799b937d48SNate Lawson 
228015e32d5dSMike Smith #ifdef ACPI_DEBUG
22810ae55423SMike Smith /*
22820ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
22830ae55423SMike Smith  *
22840ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
22850ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
22860ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
22870ae55423SMike Smith  * prefixing the bit name with !.
22880ae55423SMike Smith  */
228915e32d5dSMike Smith struct debugtag
229015e32d5dSMike Smith {
229115e32d5dSMike Smith     char	*name;
229215e32d5dSMike Smith     UINT32	value;
229315e32d5dSMike Smith };
229415e32d5dSMike Smith 
229515e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
2296c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
2297c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
2298c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
2299c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
2300c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
2301c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
2302c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
2303c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
2304c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
2305d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
23064c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
2307d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
2308297835bcSNate Lawson     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
23094c1cdee6SMike Smith 
2310c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
2311c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
23123184cf5aSNate Lawson     {"ACPI_BUS",		ACPI_BUS},
2313c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
23143184cf5aSNate Lawson     {"ACPI_EC", 		ACPI_EC},
23153184cf5aSNate Lawson     {"ACPI_FAN",		ACPI_FAN},
23163184cf5aSNate Lawson     {"ACPI_POWERRES",		ACPI_POWERRES},
23174c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
2318cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
23193184cf5aSNate Lawson     {"ACPI_TIMER",		ACPI_TIMER},
2320b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
232115e32d5dSMike Smith     {NULL, 0}
232215e32d5dSMike Smith };
232315e32d5dSMike Smith 
232415e32d5dSMike Smith static struct debugtag dbg_level[] = {
23254c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
23269501b603SJohn Baldwin     {"ACPI_LV_WARN",		ACPI_LV_WARN},
23279501b603SJohn Baldwin     {"ACPI_LV_INIT",		ACPI_LV_INIT},
23284c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
23299501b603SJohn Baldwin     {"ACPI_LV_INFO",		ACPI_LV_INFO},
23304c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
2331d62ab2f4SMitsuru IWASAKI 
2332d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
2333297835bcSNate Lawson     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
23344c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
23354c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
2336d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
23374c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
23384c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
23394c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
23404c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
23414c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
23424c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
23434c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
23444c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
23454c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
23464c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
2347d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
2348d62ab2f4SMitsuru IWASAKI 
2349d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
2350d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
2351d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
2352d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
2353d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
23544c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
2355d62ab2f4SMitsuru IWASAKI 
2356d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2357d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
2358d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
2359d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
2360d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
2361d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
2362d62ab2f4SMitsuru IWASAKI 
2363d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
236498479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
236598479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
236698479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
236798479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
236898479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
236915e32d5dSMike Smith     {NULL, 0}
237015e32d5dSMike Smith };
237115e32d5dSMike Smith 
237215e32d5dSMike Smith static void
237315e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
237415e32d5dSMike Smith {
237515e32d5dSMike Smith     char	*ep;
237615e32d5dSMike Smith     int		i, l;
23770ae55423SMike Smith     int		set;
237815e32d5dSMike Smith 
237915e32d5dSMike Smith     while (*cp) {
238015e32d5dSMike Smith 	if (isspace(*cp)) {
238115e32d5dSMike Smith 	    cp++;
238215e32d5dSMike Smith 	    continue;
238315e32d5dSMike Smith 	}
238415e32d5dSMike Smith 	ep = cp;
238515e32d5dSMike Smith 	while (*ep && !isspace(*ep))
238615e32d5dSMike Smith 	    ep++;
23870ae55423SMike Smith 	if (*cp == '!') {
23880ae55423SMike Smith 	    set = 0;
23890ae55423SMike Smith 	    cp++;
23900ae55423SMike Smith 	    if (cp == ep)
23910ae55423SMike Smith 		continue;
23920ae55423SMike Smith 	} else {
23930ae55423SMike Smith 	    set = 1;
23940ae55423SMike Smith 	}
239515e32d5dSMike Smith 	l = ep - cp;
239615e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
239715e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
2398be2b1797SNate Lawson 		if (set)
239915e32d5dSMike Smith 		    *flag |= tag[i].value;
2400be2b1797SNate Lawson 		else
24010ae55423SMike Smith 		    *flag &= ~tag[i].value;
240215e32d5dSMike Smith 		printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
240315e32d5dSMike Smith 	    }
240415e32d5dSMike Smith 	}
240515e32d5dSMike Smith 	cp = ep;
240615e32d5dSMike Smith     }
240715e32d5dSMike Smith }
240815e32d5dSMike Smith 
240915e32d5dSMike Smith static void
24102a4ac806SMike Smith acpi_set_debugging(void *junk)
241115e32d5dSMike Smith {
241294aae282SMike Barcroft     char	*cp;
241315e32d5dSMike Smith 
24141d7b121cSNate Lawson     if (cold) {
24150ae55423SMike Smith 	AcpiDbgLayer = 0;
24160ae55423SMike Smith 	AcpiDbgLevel = 0;
24171d7b121cSNate Lawson     }
24181d7b121cSNate Lawson 
241994aae282SMike Barcroft     if ((cp = getenv("debug.acpi.layer")) != NULL) {
242015e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
242194aae282SMike Barcroft 	freeenv(cp);
242294aae282SMike Barcroft     }
242394aae282SMike Barcroft     if ((cp = getenv("debug.acpi.level")) != NULL) {
242415e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
242594aae282SMike Barcroft 	freeenv(cp);
242694aae282SMike Barcroft     }
24270ae55423SMike Smith 
24281d7b121cSNate Lawson     if (cold) {
24291d7b121cSNate Lawson 	printf("ACPI debug layer 0x%x debug level 0x%x\n",
24301d7b121cSNate Lawson 	       AcpiDbgLayer, AcpiDbgLevel);
24311d7b121cSNate Lawson     }
243215e32d5dSMike Smith }
2433be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
2434be2b1797SNate Lawson 	NULL);
24351d7b121cSNate Lawson 
24361d7b121cSNate Lawson static int
24371d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
24381d7b121cSNate Lawson {
243954f6bca0SNate Lawson     int		 error, *dbg;
24401d7b121cSNate Lawson     struct	 debugtag *tag;
244154f6bca0SNate Lawson     struct	 sbuf sb;
24421d7b121cSNate Lawson 
244354f6bca0SNate Lawson     if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
244454f6bca0SNate Lawson 	return (ENOMEM);
24451d7b121cSNate Lawson     if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
24461d7b121cSNate Lawson 	tag = &dbg_layer[0];
24471d7b121cSNate Lawson 	dbg = &AcpiDbgLayer;
24481d7b121cSNate Lawson     } else {
24491d7b121cSNate Lawson 	tag = &dbg_level[0];
24501d7b121cSNate Lawson 	dbg = &AcpiDbgLevel;
24511d7b121cSNate Lawson     }
24521d7b121cSNate Lawson 
24531d7b121cSNate Lawson     /* Get old values if this is a get request. */
24541d7b121cSNate Lawson     if (*dbg == 0) {
245554f6bca0SNate Lawson 	sbuf_cpy(&sb, "NONE");
24561d7b121cSNate Lawson     } else if (req->newptr == NULL) {
24571d7b121cSNate Lawson 	for (; tag->name != NULL; tag++) {
245854f6bca0SNate Lawson 	    if ((*dbg & tag->value) == tag->value)
245954f6bca0SNate Lawson 		sbuf_printf(&sb, "%s ", tag->name);
24601d7b121cSNate Lawson 	}
24611d7b121cSNate Lawson     }
246254f6bca0SNate Lawson     sbuf_trim(&sb);
246354f6bca0SNate Lawson     sbuf_finish(&sb);
24641d7b121cSNate Lawson 
246554f6bca0SNate Lawson     error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
246654f6bca0SNate Lawson     sbuf_delete(&sb);
24671d7b121cSNate Lawson 
24681d7b121cSNate Lawson     /* If the user is setting a string, parse it. */
24691d7b121cSNate Lawson     if (error == 0 && req->newptr != NULL) {
24701d7b121cSNate Lawson 	*dbg = 0;
24711d7b121cSNate Lawson 	setenv((char *)oidp->oid_arg1, (char *)req->newptr);
24721d7b121cSNate Lawson 	acpi_set_debugging(NULL);
24731d7b121cSNate Lawson     }
24741d7b121cSNate Lawson 
24751d7b121cSNate Lawson     return (error);
24761d7b121cSNate Lawson }
24771d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
24781d7b121cSNate Lawson 	    "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
24791d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
24801d7b121cSNate Lawson 	    "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
248115e32d5dSMike Smith #endif
2482f9390180SMitsuru IWASAKI 
2483f9390180SMitsuru IWASAKI static int
2484f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
2485f9390180SMitsuru IWASAKI {
2486f9390180SMitsuru IWASAKI 	int	state, acpi_state;
2487f9390180SMitsuru IWASAKI 	int	error;
2488f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
2489f9390180SMitsuru IWASAKI 	va_list	ap;
2490f9390180SMitsuru IWASAKI 
2491f9390180SMitsuru IWASAKI 	error = 0;
2492f9390180SMitsuru IWASAKI 	switch (cmd) {
2493f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
2494f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
2495f9390180SMitsuru IWASAKI 		if (sc == NULL) {
2496f9390180SMitsuru IWASAKI 			error = EINVAL;
2497f9390180SMitsuru IWASAKI 			goto out;
2498f9390180SMitsuru IWASAKI 		}
2499f9390180SMitsuru IWASAKI 
2500f9390180SMitsuru IWASAKI 		va_start(ap, arg);
2501f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
2502f9390180SMitsuru IWASAKI 		va_end(ap);
2503f9390180SMitsuru IWASAKI 
2504f9390180SMitsuru IWASAKI 		switch (state) {
2505f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
2506f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
2507f9390180SMitsuru IWASAKI 			break;
2508f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
2509f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
2510f9390180SMitsuru IWASAKI 			break;
2511f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
2512f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
2513f9390180SMitsuru IWASAKI 			break;
2514f9390180SMitsuru IWASAKI 		default:
2515f9390180SMitsuru IWASAKI 			error = EINVAL;
2516f9390180SMitsuru IWASAKI 			goto out;
2517f9390180SMitsuru IWASAKI 		}
2518f9390180SMitsuru IWASAKI 
2519f9390180SMitsuru IWASAKI 		acpi_SetSleepState(sc, acpi_state);
2520f9390180SMitsuru IWASAKI 		break;
2521f9390180SMitsuru IWASAKI 	default:
2522f9390180SMitsuru IWASAKI 		error = EINVAL;
2523f9390180SMitsuru IWASAKI 		goto out;
2524f9390180SMitsuru IWASAKI 	}
2525f9390180SMitsuru IWASAKI 
2526f9390180SMitsuru IWASAKI out:
2527f9390180SMitsuru IWASAKI 	return (error);
2528f9390180SMitsuru IWASAKI }
2529f9390180SMitsuru IWASAKI 
2530f9390180SMitsuru IWASAKI static void
2531f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
2532f9390180SMitsuru IWASAKI {
2533be2b1797SNate Lawson     if (!cold || resource_disabled("acpi", 0))
25346c407052SMitsuru IWASAKI 	return;
2535f9390180SMitsuru IWASAKI 
2536f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
2537f9390180SMitsuru IWASAKI }
2538f9390180SMitsuru IWASAKI 
2539f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
2540