xref: /freebsd/sys/dev/acpica/acpi.c (revision 6fca9360dbe54a16d8a53ad5c54c6f5e9b52fff6)
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>
4615e32d5dSMike Smith 
4715e32d5dSMike Smith #include <machine/clock.h>
4815e32d5dSMike Smith #include <machine/resource.h>
4915e32d5dSMike Smith 
50bc0f2195SMike Smith #include <isa/isavar.h>
51bc0f2195SMike Smith 
5215e32d5dSMike Smith #include "acpi.h"
5315e32d5dSMike Smith 
541611ea87SMitsuru IWASAKI #include <dev/acpica/acpica_support.h>
551611ea87SMitsuru IWASAKI 
5615e32d5dSMike Smith #include <dev/acpica/acpivar.h>
5715e32d5dSMike Smith #include <dev/acpica/acpiio.h>
5815e32d5dSMike Smith 
5915e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
6015e32d5dSMike Smith 
6115e32d5dSMike Smith /*
620ae55423SMike Smith  * Hooks for the ACPI CA debugging infrastructure
630ae55423SMike Smith  */
64cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
65b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
660ae55423SMike Smith 
670ae55423SMike Smith /*
6815e32d5dSMike Smith  * Character device
6915e32d5dSMike Smith  */
7015e32d5dSMike Smith 
7115e32d5dSMike Smith static d_open_t		acpiopen;
7215e32d5dSMike Smith static d_close_t	acpiclose;
7315e32d5dSMike Smith static d_ioctl_t	acpiioctl;
7415e32d5dSMike Smith 
7515e32d5dSMike Smith #define CDEV_MAJOR 152
7615e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
777ac40f5fSPoul-Henning Kamp 	.d_open =	acpiopen,
787ac40f5fSPoul-Henning Kamp 	.d_close =	acpiclose,
797ac40f5fSPoul-Henning Kamp 	.d_ioctl =	acpiioctl,
807ac40f5fSPoul-Henning Kamp 	.d_name =	"acpi",
817ac40f5fSPoul-Henning Kamp 	.d_maj =	CDEV_MAJOR,
8215e32d5dSMike Smith };
8315e32d5dSMike Smith 
841d073b1dSJohn Baldwin static const char* sleep_state_names[] = {
85b8670bedSMitsuru IWASAKI     "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
861d073b1dSJohn Baldwin 
872a4ac806SMike Smith /* this has to be static, as the softc is gone when we need it */
882a4ac806SMike Smith static int acpi_off_state = ACPI_STATE_S5;
892a4ac806SMike Smith 
90fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000
91cb9b0d80SMike Smith struct mtx	acpi_mutex;
92fc0ea94aSJohn Baldwin #endif
93cb9b0d80SMike Smith 
946d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
9515e32d5dSMike Smith static void	acpi_identify(driver_t *driver, device_t parent);
9615e32d5dSMike Smith static int	acpi_probe(device_t dev);
9715e32d5dSMike Smith static int	acpi_attach(device_t dev);
9815e32d5dSMike Smith static device_t	acpi_add_child(device_t bus, int order, const char *name, int unit);
9915e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
10015e32d5dSMike Smith static int	acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result);
10115e32d5dSMike Smith static int	acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value);
10215e32d5dSMike Smith static int	acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start,
10315e32d5dSMike Smith 				  u_long count);
10415e32d5dSMike Smith static int	acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp,
10515e32d5dSMike Smith 				  u_long *countp);
10615e32d5dSMike Smith static struct resource *acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
10715e32d5dSMike Smith 					    u_long start, u_long end, u_long count, u_int flags);
10815e32d5dSMike Smith static int	acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r);
10932d18aa5SMike Smith static u_int32_t acpi_isa_get_logicalid(device_t dev);
110b2566207STakanori Watanabe static u_int32_t acpi_isa_get_compatid(device_t dev);
111bc0f2195SMike Smith static int	acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids);
11215e32d5dSMike Smith 
11315e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
11415e32d5dSMike Smith static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status);
11515e32d5dSMike Smith 
11615e32d5dSMike Smith static void	acpi_shutdown_pre_sync(void *arg, int howto);
11715e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
11815e32d5dSMike Smith 
11913d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
12013d4f7baSMitsuru IWASAKI 
12115e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
12215e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
123d75de536SMitsuru IWASAKI static int	acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1241d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
12515e32d5dSMike Smith 
126f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
127f9390180SMitsuru IWASAKI 
12815e32d5dSMike Smith static device_method_t acpi_methods[] = {
12915e32d5dSMike Smith     /* Device interface */
13015e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
13115e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
13215e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
13315e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
13415e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
13515e32d5dSMike Smith     DEVMETHOD(device_resume,		bus_generic_resume),
13615e32d5dSMike Smith 
13715e32d5dSMike Smith     /* Bus interface */
13815e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
13915e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
14015e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
14115e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
14215e32d5dSMike Smith     DEVMETHOD(bus_set_resource,		acpi_set_resource),
14315e32d5dSMike Smith     DEVMETHOD(bus_get_resource,		acpi_get_resource),
14415e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
14515e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
14615e32d5dSMike Smith     DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
14715e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
14815e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
14915e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
15015e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
15115e32d5dSMike Smith 
152bc0f2195SMike Smith     /* ISA emulation */
153bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
154bc0f2195SMike Smith 
15515e32d5dSMike Smith     {0, 0}
15615e32d5dSMike Smith };
15715e32d5dSMike Smith 
15815e32d5dSMike Smith static driver_t acpi_driver = {
15915e32d5dSMike Smith     "acpi",
16015e32d5dSMike Smith     acpi_methods,
16115e32d5dSMike Smith     sizeof(struct acpi_softc),
16215e32d5dSMike Smith };
16315e32d5dSMike Smith 
1643273b005SMike Smith static devclass_t acpi_devclass;
1656d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
166dde24897SMike Smith MODULE_VERSION(acpi, 100);
16715e32d5dSMike Smith 
16815e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, "");
16915e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, "");
170dde24897SMike Smith static int acpi_ca_version = ACPI_CA_VERSION;
171dd081ed5SMitsuru IWASAKI SYSCTL_INT(_debug, OID_AUTO, acpi_ca_version, CTLFLAG_RD, &acpi_ca_version, 0, "");
17215e32d5dSMike Smith 
17315e32d5dSMike Smith /*
1746d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
1756d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
1766d63101aSMike Smith  * It also cannot be unloaded, since the entire system bus heirarchy hangs off it.
1776d63101aSMike Smith  */
1786d63101aSMike Smith static int
1796d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
1806d63101aSMike Smith {
1816d63101aSMike Smith     switch(event) {
1826d63101aSMike Smith     case MOD_LOAD:
18387b45ed5SMitsuru IWASAKI 	if (!cold) {
18487b45ed5SMitsuru IWASAKI 	    printf("The ACPI driver cannot be loaded after boot.\n");
1856d63101aSMike Smith 	    return(EPERM);
18687b45ed5SMitsuru IWASAKI 	}
1876d63101aSMike Smith 	break;
1886d63101aSMike Smith     case MOD_UNLOAD:
189f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
1906d63101aSMike Smith 	    return(EBUSY);
191f9390180SMitsuru IWASAKI 	break;
1926d63101aSMike Smith     default:
1936d63101aSMike Smith 	break;
1946d63101aSMike Smith     }
1956d63101aSMike Smith     return(0);
1966d63101aSMike Smith }
1976d63101aSMike Smith 
1986d63101aSMike Smith /*
19915e32d5dSMike Smith  * Detect ACPI, perform early initialisation
20015e32d5dSMike Smith  */
20115e32d5dSMike Smith static void
20215e32d5dSMike Smith acpi_identify(driver_t *driver, device_t parent)
20315e32d5dSMike Smith {
20415e32d5dSMike Smith     device_t			child;
20515e32d5dSMike Smith     int				error;
206d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
207d786139cSMaxime Henrion     char			*debugpoint;
20815e32d5dSMike Smith #endif
20915e32d5dSMike Smith 
210b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2110ae55423SMike Smith 
21287b45ed5SMitsuru IWASAKI     if (!cold)
21387b45ed5SMitsuru IWASAKI 	return_VOID;
214840f9b53STakanori Watanabe 
21515e32d5dSMike Smith     /*
21632d18aa5SMike Smith      * Check that we haven't been disabled with a hint.
21732d18aa5SMike Smith      */
2188a9bc9c0SJohn Baldwin     if (resource_disabled("acpi", 0))
21932d18aa5SMike Smith 	return_VOID;
22032d18aa5SMike Smith 
22132d18aa5SMike Smith     /*
22215e32d5dSMike Smith      * Make sure we're not being doubly invoked.
22315e32d5dSMike Smith      */
22415e32d5dSMike Smith     if (device_find_child(parent, "acpi", 0) != NULL)
2250ae55423SMike Smith 	return_VOID;
22615e32d5dSMike Smith 
227fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000
228cb9b0d80SMike Smith     /* initialise the ACPI mutex */
2296008862bSJohn Baldwin     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
230fc0ea94aSJohn Baldwin #endif
231cb9b0d80SMike Smith 
23215e32d5dSMike Smith     /*
2332a4ac806SMike Smith      * Start up the ACPI CA subsystem.
23415e32d5dSMike Smith      */
235d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
236d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
237d786139cSMaxime Henrion     if (debugpoint) {
238d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "init"))
23915e32d5dSMike Smith 	    acpi_EnterDebugger();
240d786139cSMaxime Henrion         freeenv(debugpoint);
241d786139cSMaxime Henrion     }
24215e32d5dSMike Smith #endif
243b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
244bfae45aaSMike Smith 	printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
2450ae55423SMike Smith 	return_VOID;
24615e32d5dSMike Smith     }
247d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
248d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
249d786139cSMaxime Henrion     if (debugpoint) {
250d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "tables"))
25115e32d5dSMike Smith 	    acpi_EnterDebugger();
252d786139cSMaxime Henrion         freeenv(debugpoint);
253d786139cSMaxime Henrion     }
25415e32d5dSMike Smith #endif
2551611ea87SMitsuru IWASAKI 
256b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiLoadTables())) {
257bfae45aaSMike Smith 	printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
2580ae55423SMike Smith 	return_VOID;
25915e32d5dSMike Smith     }
26015e32d5dSMike Smith 
26115e32d5dSMike Smith     /*
26215e32d5dSMike Smith      * Attach the actual ACPI device.
26315e32d5dSMike Smith      */
26415e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
26515e32d5dSMike Smith 	    device_printf(parent, "ACPI: could not attach\n");
2660ae55423SMike Smith 	    return_VOID;
26715e32d5dSMike Smith     }
26815e32d5dSMike Smith }
26915e32d5dSMike Smith 
27015e32d5dSMike Smith /*
27115e32d5dSMike Smith  * Fetch some descriptive data from ACPI to put in our attach message
27215e32d5dSMike Smith  */
27315e32d5dSMike Smith static int
27415e32d5dSMike Smith acpi_probe(device_t dev)
27515e32d5dSMike Smith {
27615e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
27715e32d5dSMike Smith     char		buf[20];
278cb9b0d80SMike Smith     ACPI_STATUS		status;
27915e32d5dSMike Smith     int			error;
280fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
28115e32d5dSMike Smith 
282b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
283f9390180SMitsuru IWASAKI 
284f9390180SMitsuru IWASAKI     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
285f9390180SMitsuru IWASAKI         power_pm_get_type() != POWER_PM_TYPE_ACPI) {
286f9390180SMitsuru IWASAKI 	device_printf(dev, "Other PM system enabled.\n");
287f9390180SMitsuru IWASAKI 	return_VALUE(ENXIO);
288f9390180SMitsuru IWASAKI     }
289f9390180SMitsuru IWASAKI 
290cb9b0d80SMike Smith     ACPI_LOCK;
2910ae55423SMike Smith 
292b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
293bfae45aaSMike Smith 	device_printf(dev, "couldn't get XSDT header: %s\n", AcpiFormatException(status));
294cb9b0d80SMike Smith 	error = ENXIO;
295cb9b0d80SMike Smith     } else {
29615e32d5dSMike Smith 	sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId);
29715e32d5dSMike Smith 	device_set_desc_copy(dev, buf);
298cb9b0d80SMike Smith 	error = 0;
299cb9b0d80SMike Smith     }
300cb9b0d80SMike Smith     ACPI_UNLOCK;
301cb9b0d80SMike Smith     return_VALUE(error);
30215e32d5dSMike Smith }
30315e32d5dSMike Smith 
30415e32d5dSMike Smith static int
30515e32d5dSMike Smith acpi_attach(device_t dev)
30615e32d5dSMike Smith {
30715e32d5dSMike Smith     struct acpi_softc	*sc;
308cb9b0d80SMike Smith     ACPI_STATUS		status;
30915e32d5dSMike Smith     int			error;
31032d18aa5SMike Smith     UINT32		flags;
311498d464fSMitsuru IWASAKI     char		*env;
312d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
313d786139cSMaxime Henrion     char		*debugpoint;
31415e32d5dSMike Smith #endif
315fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
31615e32d5dSMike Smith 
317b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
318cb9b0d80SMike Smith     ACPI_LOCK;
31915e32d5dSMike Smith     sc = device_get_softc(dev);
32015e32d5dSMike Smith     bzero(sc, sizeof(*sc));
32115e32d5dSMike Smith     sc->acpi_dev = dev;
32215e32d5dSMike Smith 
323d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
324d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
325d786139cSMaxime Henrion     if (debugpoint) {
326d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "spaces"))
32715e32d5dSMike Smith 	    acpi_EnterDebugger();
328d786139cSMaxime Henrion         freeenv(debugpoint);
329d786139cSMaxime Henrion     }
33015e32d5dSMike Smith #endif
33115e32d5dSMike Smith 
33215e32d5dSMike Smith     /*
33315e32d5dSMike Smith      * Install the default address space handlers.
33415e32d5dSMike Smith      */
335cb9b0d80SMike Smith     error = ENXIO;
336b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
3372a4ac806SMike Smith 						ACPI_ADR_SPACE_SYSTEM_MEMORY,
33815e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
339b53f2771SMike Smith 						NULL, NULL))) {
340bfae45aaSMike Smith 	device_printf(dev, "could not initialise SystemMemory handler: %s\n", AcpiFormatException(status));
341cb9b0d80SMike Smith 	goto out;
34215e32d5dSMike Smith     }
343b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
3442a4ac806SMike Smith 						ACPI_ADR_SPACE_SYSTEM_IO,
34515e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
346b53f2771SMike Smith 						NULL, NULL))) {
347bfae45aaSMike Smith 	device_printf(dev, "could not initialise SystemIO handler: %s\n", AcpiFormatException(status));
348cb9b0d80SMike Smith 	goto out;
34915e32d5dSMike Smith     }
350b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
3512a4ac806SMike Smith 						ACPI_ADR_SPACE_PCI_CONFIG,
35215e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
353b53f2771SMike Smith 						NULL, NULL))) {
354bfae45aaSMike Smith 	device_printf(dev, "could not initialise PciConfig handler: %s\n", AcpiFormatException(status));
355cb9b0d80SMike Smith 	goto out;
35615e32d5dSMike Smith     }
35715e32d5dSMike Smith 
35815e32d5dSMike Smith     /*
35915e32d5dSMike Smith      * Bring ACPI fully online.
36015e32d5dSMike Smith      *
36132d18aa5SMike Smith      * Note that some systems (specifically, those with namespace evaluation issues
36232d18aa5SMike Smith      * that require the avoidance of parts of the namespace) must avoid running _INI
36332d18aa5SMike Smith      * and _STA on everything, as well as dodging the final object init pass.
36415e32d5dSMike Smith      *
36532d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
36632d18aa5SMike Smith      *
36732d18aa5SMike Smith      * XXX We should arrange for the object init pass after we have attached all our
36832d18aa5SMike Smith      *     child devices, but on many systems it works here.
36915e32d5dSMike Smith      */
370d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
371d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
372d786139cSMaxime Henrion     if (debugpoint) {
373d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "enable"))
37415e32d5dSMike Smith 	    acpi_EnterDebugger();
375d786139cSMaxime Henrion         freeenv(debugpoint);
376d786139cSMaxime Henrion     }
37715e32d5dSMike Smith #endif
37832d18aa5SMike Smith     flags = 0;
379d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
38032d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
381b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
382bfae45aaSMike Smith 	device_printf(dev, "could not enable ACPI: %s\n", AcpiFormatException(status));
383cb9b0d80SMike Smith 	goto out;
38415e32d5dSMike Smith     }
38515e32d5dSMike Smith 
386b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
387b69ed3f4SMitsuru IWASAKI 	device_printf(dev, "could not initialize ACPI objects: %s\n", AcpiFormatException(status));
388b69ed3f4SMitsuru IWASAKI 	goto out;
389b69ed3f4SMitsuru IWASAKI     }
390b69ed3f4SMitsuru IWASAKI 
39115e32d5dSMike Smith     /*
3921d073b1dSJohn Baldwin      * Setup our sysctl tree.
3931d073b1dSJohn Baldwin      *
3941d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
3951d073b1dSJohn Baldwin      */
3961d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
3971d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
3981d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
3991d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
4001d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
401d75de536SMitsuru IWASAKI 	OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
402d75de536SMitsuru IWASAKI 	0, 0, acpi_supported_sleep_state_sysctl, "A", "");
403d75de536SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4041d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4051d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4061d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4071d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4081d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4091d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4101d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
4111d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
412f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
413f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
414f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
415f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
416f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
417f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
4182d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
419ff01efb5SMitsuru IWASAKI 	OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
420ff01efb5SMitsuru IWASAKI 	&sc->acpi_sleep_delay, 0, "sleep delay");
421ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4221611ea87SMitsuru IWASAKI 	OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
4231611ea87SMitsuru IWASAKI 	&sc->acpi_s4bios, 0, "S4BIOS mode");
4241611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4252d644607SMitsuru IWASAKI 	OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
4262d644607SMitsuru IWASAKI 	&sc->acpi_verbose, 0, "verbose mode");
427c6a78e98STakanori Watanabe     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
428c6a78e98STakanori Watanabe 		   OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW,
429c6a78e98STakanori Watanabe 		   &sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff");
430c6a78e98STakanori Watanabe     sc->acpi_disable_on_poweroff = 1;
431f3a6cbb6SMitsuru IWASAKI     sc->acpi_sleep_delay = 0;
432931a10c9SMitsuru IWASAKI     sc->acpi_s4bios = 1;
4332d644607SMitsuru IWASAKI     if (bootverbose)
4342d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
435498d464fSMitsuru IWASAKI     if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) {
436498d464fSMitsuru IWASAKI 	sc->acpi_verbose = 1;
437498d464fSMitsuru IWASAKI 	freeenv(env);
438498d464fSMitsuru IWASAKI     }
4391d073b1dSJohn Baldwin 
4401d073b1dSJohn Baldwin     /*
44115e32d5dSMike Smith      * Dispatch the default sleep state to devices.
44215e32d5dSMike Smith      * TBD: should be configured from userland policy manager.
44315e32d5dSMike Smith      */
44415e32d5dSMike Smith     sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
44515e32d5dSMike Smith     sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
44615e32d5dSMike Smith     sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
447f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
448f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
44915e32d5dSMike Smith 
45013d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
45115e32d5dSMike Smith 
45215e32d5dSMike Smith     /*
45315e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
45415e32d5dSMike Smith      */
455d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
456d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
457d786139cSMaxime Henrion     if (debugpoint) {
458d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "probe"))
45915e32d5dSMike Smith 	    acpi_EnterDebugger();
460d786139cSMaxime Henrion 	freeenv(debugpoint);
461d786139cSMaxime Henrion     }
46215e32d5dSMike Smith #endif
46315e32d5dSMike Smith 
46415e32d5dSMike Smith     /*
46515e32d5dSMike Smith      * Register our shutdown handlers
46615e32d5dSMike Smith      */
46715e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST);
46815e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST);
46915e32d5dSMike Smith 
47015e32d5dSMike Smith     /*
47115e32d5dSMike Smith      * Register our acpi event handlers.
47215e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
47315e32d5dSMike Smith      */
47415e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST);
47515e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST);
47615e32d5dSMike Smith 
47715e32d5dSMike Smith     /*
47815e32d5dSMike Smith      * Flag our initial states.
47915e32d5dSMike Smith      */
48015e32d5dSMike Smith     sc->acpi_enabled = 1;
48115e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
482ece50487SMitsuru IWASAKI     sc->acpi_sleep_disabled = 0;
48315e32d5dSMike Smith 
48415e32d5dSMike Smith     /*
48515e32d5dSMike Smith      * Create the control device
48615e32d5dSMike Smith      */
487a89fcf28STakanori Watanabe     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
4882a4689e8SRobert Watson 	"acpi");
48915e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
49015e32d5dSMike Smith 
491d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
492d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
493d786139cSMaxime Henrion     if (debugpoint) {
494d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "running"))
49515e32d5dSMike Smith 	    acpi_EnterDebugger();
496d786139cSMaxime Henrion 	freeenv(debugpoint);
497d786139cSMaxime Henrion     }
49815e32d5dSMike Smith #endif
499f86214b6SMitsuru IWASAKI 
50091da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS
501c573e654SMitsuru IWASAKI     if ((error = acpi_task_thread_init())) {
502c573e654SMitsuru IWASAKI 	goto out;
503c573e654SMitsuru IWASAKI     }
504c573e654SMitsuru IWASAKI #endif
505c573e654SMitsuru IWASAKI 
506f86214b6SMitsuru IWASAKI     if ((error = acpi_machdep_init(dev))) {
507f86214b6SMitsuru IWASAKI 	goto out;
508f86214b6SMitsuru IWASAKI     }
509f86214b6SMitsuru IWASAKI 
510f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
511f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
512f9390180SMitsuru IWASAKI 
513eeb6dba2SJohn Baldwin     if (!acpi_disabled("bus"))
514eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
515eeb6dba2SJohn Baldwin 
516cb9b0d80SMike Smith     error = 0;
517cb9b0d80SMike Smith 
518cb9b0d80SMike Smith  out:
519cb9b0d80SMike Smith     ACPI_UNLOCK;
520cb9b0d80SMike Smith     return_VALUE(error);
52115e32d5dSMike Smith }
52215e32d5dSMike Smith 
52315e32d5dSMike Smith /*
52415e32d5dSMike Smith  * Handle a new device being added
52515e32d5dSMike Smith  */
52615e32d5dSMike Smith static device_t
52715e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
52815e32d5dSMike Smith {
52915e32d5dSMike Smith     struct acpi_device	*ad;
53015e32d5dSMike Smith     device_t		child;
53115e32d5dSMike Smith 
53215e32d5dSMike Smith     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL)
53315e32d5dSMike Smith 	return(NULL);
53415e32d5dSMike Smith     bzero(ad, sizeof(*ad));
53515e32d5dSMike Smith 
53615e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
53715e32d5dSMike Smith 
53815e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
53915e32d5dSMike Smith     if (child != NULL)
54015e32d5dSMike Smith 	device_set_ivars(child, ad);
54115e32d5dSMike Smith     return(child);
54215e32d5dSMike Smith }
54315e32d5dSMike Smith 
54415e32d5dSMike Smith static int
54515e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
54615e32d5dSMike Smith {
54715e32d5dSMike Smith     struct acpi_device		*adev = device_get_ivars(child);
54815e32d5dSMike Smith     struct resource_list	*rl = &adev->ad_rl;
54915e32d5dSMike Smith     int retval = 0;
55015e32d5dSMike Smith 
55115e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
5529ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
5539ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
5549ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
5559ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
55615e32d5dSMike Smith     retval += bus_print_child_footer(bus, child);
55715e32d5dSMike Smith 
55815e32d5dSMike Smith     return(retval);
55915e32d5dSMike Smith }
56015e32d5dSMike Smith 
56115e32d5dSMike Smith 
56215e32d5dSMike Smith /*
56315e32d5dSMike Smith  * Handle per-device ivars
56415e32d5dSMike Smith  */
56515e32d5dSMike Smith static int
56615e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
56715e32d5dSMike Smith {
56815e32d5dSMike Smith     struct acpi_device	*ad;
56915e32d5dSMike Smith 
57015e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
57115e32d5dSMike Smith 	printf("device has no ivars\n");
57215e32d5dSMike Smith 	return(ENOENT);
57315e32d5dSMike Smith     }
57415e32d5dSMike Smith 
57515e32d5dSMike Smith     switch(index) {
57615e32d5dSMike Smith 	/* ACPI ivars */
57715e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
57815e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
57915e32d5dSMike Smith 	break;
58015e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
58115e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
58215e32d5dSMike Smith 	break;
58315e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
58415e32d5dSMike Smith 	*(void **)result = ad->ad_private;
58515e32d5dSMike Smith 	break;
58615e32d5dSMike Smith 
58732d18aa5SMike Smith 	/* ISA compatibility */
58832d18aa5SMike Smith     case ISA_IVAR_VENDORID:
58932d18aa5SMike Smith     case ISA_IVAR_SERIAL:
59032d18aa5SMike Smith     case ISA_IVAR_COMPATID:
59132d18aa5SMike Smith 	*(int *)result = -1;
59232d18aa5SMike Smith 	break;
59332d18aa5SMike Smith 
59432d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
59532d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
59632d18aa5SMike Smith 	break;
59732d18aa5SMike Smith 
59815e32d5dSMike Smith     default:
59915e32d5dSMike Smith 	return(ENOENT);
60015e32d5dSMike Smith     }
60115e32d5dSMike Smith     return(0);
60215e32d5dSMike Smith }
60315e32d5dSMike Smith 
60415e32d5dSMike Smith static int
60515e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
60615e32d5dSMike Smith {
60715e32d5dSMike Smith     struct acpi_device	*ad;
60815e32d5dSMike Smith 
60915e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
61015e32d5dSMike Smith 	printf("device has no ivars\n");
61115e32d5dSMike Smith 	return(ENOENT);
61215e32d5dSMike Smith     }
61315e32d5dSMike Smith 
61415e32d5dSMike Smith     switch(index) {
61515e32d5dSMike Smith 	/* ACPI ivars */
61615e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
61715e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
61815e32d5dSMike Smith 	break;
61915e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
62015e32d5dSMike Smith 	ad->ad_magic = (int )value;
62115e32d5dSMike Smith 	break;
62215e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
62315e32d5dSMike Smith 	ad->ad_private = (void *)value;
62415e32d5dSMike Smith 	break;
62515e32d5dSMike Smith 
62615e32d5dSMike Smith     default:
6272ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
62815e32d5dSMike Smith 	return(ENOENT);
62915e32d5dSMike Smith     }
63015e32d5dSMike Smith     return(0);
63115e32d5dSMike Smith }
63215e32d5dSMike Smith 
63315e32d5dSMike Smith /*
63415e32d5dSMike Smith  * Handle child resource allocation/removal
63515e32d5dSMike Smith  */
63615e32d5dSMike Smith static int
63715e32d5dSMike Smith acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
63815e32d5dSMike Smith {
63915e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
64015e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
64115e32d5dSMike Smith 
64215e32d5dSMike Smith     resource_list_add(rl, type, rid, start, start + count -1, count);
64315e32d5dSMike Smith 
64415e32d5dSMike Smith     return(0);
64515e32d5dSMike Smith }
64615e32d5dSMike Smith 
64715e32d5dSMike Smith static int
64815e32d5dSMike Smith acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
64915e32d5dSMike Smith {
65015e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
65115e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
65215e32d5dSMike Smith     struct resource_list_entry	*rle;
65315e32d5dSMike Smith 
65415e32d5dSMike Smith     rle = resource_list_find(rl, type, rid);
65515e32d5dSMike Smith     if (!rle)
65615e32d5dSMike Smith 	return(ENOENT);
65715e32d5dSMike Smith 
65815e32d5dSMike Smith     if (startp)
65915e32d5dSMike Smith 	*startp = rle->start;
66015e32d5dSMike Smith     if (countp)
66115e32d5dSMike Smith 	*countp = rle->count;
66215e32d5dSMike Smith 
66315e32d5dSMike Smith     return(0);
66415e32d5dSMike Smith }
66515e32d5dSMike Smith 
66615e32d5dSMike Smith static struct resource *
66715e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
66815e32d5dSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
66915e32d5dSMike Smith {
67015e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
67115e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
67215e32d5dSMike Smith 
67315e32d5dSMike Smith     return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags));
67415e32d5dSMike Smith }
67515e32d5dSMike Smith 
67615e32d5dSMike Smith static int
67715e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
67815e32d5dSMike Smith {
67915e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
68015e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
68115e32d5dSMike Smith 
68215e32d5dSMike Smith     return(resource_list_release(rl, bus, child, type, rid, r));
68315e32d5dSMike Smith }
68415e32d5dSMike Smith 
68515e32d5dSMike Smith /*
686bc0f2195SMike Smith  * Handle ISA-like devices probing for a PnP ID to match.
687bc0f2195SMike Smith  */
688bc0f2195SMike Smith #define PNP_EISAID(s)				\
689bc0f2195SMike Smith 	((((s[0] - '@') & 0x1f) << 2)		\
690bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x18) >> 3)		\
691bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x07) << 13)	\
692bc0f2195SMike Smith 	 | (((s[2] - '@') & 0x1f) << 8)		\
693bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[4]) << 16)		\
694bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[3]) << 20)		\
695bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[6]) << 24)		\
696bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[5]) << 28))
697bc0f2195SMike Smith 
69832d18aa5SMike Smith static u_int32_t
69932d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
700bc0f2195SMike Smith {
701bc0f2195SMike Smith     ACPI_HANDLE		h;
702bc0f2195SMike Smith     ACPI_DEVICE_INFO	devinfo;
7036fca9360SNate Lawson     ACPI_BUFFER		buf = {sizeof(devinfo), &devinfo};
704bc0f2195SMike Smith     ACPI_STATUS		error;
705bc0f2195SMike Smith     u_int32_t		pnpid;
706fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
70732d18aa5SMike Smith 
708b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
70932d18aa5SMike Smith 
71032d18aa5SMike Smith     pnpid = 0;
71132d18aa5SMike Smith     ACPI_LOCK;
71232d18aa5SMike Smith 
7136fca9360SNate Lawson     /* Fetch and validate the HID. */
71432d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
71532d18aa5SMike Smith 	goto out;
7166fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
7176fca9360SNate Lawson     if (ACPI_FAILURE(error))
71832d18aa5SMike Smith 	goto out;
7196fca9360SNate Lawson     if ((devinfo.Valid & ACPI_VALID_HID) == 0)
72032d18aa5SMike Smith 	goto out;
72132d18aa5SMike Smith 
7226fca9360SNate Lawson     pnpid = PNP_EISAID(devinfo.HardwareId.Value);
72332d18aa5SMike Smith out:
72432d18aa5SMike Smith     ACPI_UNLOCK;
72532d18aa5SMike Smith     return_VALUE(pnpid);
72632d18aa5SMike Smith }
72732d18aa5SMike Smith 
728b2566207STakanori Watanabe static u_int32_t
729b2566207STakanori Watanabe acpi_isa_get_compatid(device_t dev)
730b2566207STakanori Watanabe {
731b2566207STakanori Watanabe     ACPI_HANDLE		h;
732b2566207STakanori Watanabe     ACPI_STATUS		error;
733b2566207STakanori Watanabe     u_int32_t		pnpid;
734b2566207STakanori Watanabe     ACPI_LOCK_DECL;
735b2566207STakanori Watanabe 
736b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
737b2566207STakanori Watanabe 
738b2566207STakanori Watanabe     pnpid = 0;
739b2566207STakanori Watanabe     ACPI_LOCK;
740b2566207STakanori Watanabe 
741b2566207STakanori Watanabe     /* fetch and validate the HID */
742b2566207STakanori Watanabe     if ((h = acpi_get_handle(dev)) == NULL)
743b2566207STakanori Watanabe 	goto out;
744b2566207STakanori Watanabe     if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &pnpid)))
745b2566207STakanori Watanabe 	goto out;
746b2566207STakanori Watanabe 
747b2566207STakanori Watanabe out:
748b2566207STakanori Watanabe     ACPI_UNLOCK;
749b2566207STakanori Watanabe     return_VALUE(pnpid);
750b2566207STakanori Watanabe }
751b2566207STakanori Watanabe 
752b2566207STakanori Watanabe 
75332d18aa5SMike Smith static int
75432d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
75532d18aa5SMike Smith {
756bc0f2195SMike Smith     int			result;
757b2566207STakanori Watanabe     u_int32_t		lid, cid;
758bc0f2195SMike Smith 
759b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
760bc0f2195SMike Smith 
761bc0f2195SMike Smith     /*
762bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
763bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
764bc0f2195SMike Smith      * that to happen, so don't ever return it.
765bc0f2195SMike Smith      */
766bc0f2195SMike Smith     result = ENXIO;
767bc0f2195SMike Smith 
768bc0f2195SMike Smith     /* scan the supplied IDs for a match */
769b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
770b2566207STakanori Watanabe     cid = acpi_isa_get_compatid(child);
771bc0f2195SMike Smith     while (ids && ids->ip_id) {
772b2566207STakanori Watanabe 	if (lid == ids->ip_id || cid == ids->ip_id) {
773bc0f2195SMike Smith 	    result = 0;
774bc0f2195SMike Smith 	    goto out;
775bc0f2195SMike Smith 	}
776bc0f2195SMike Smith 	ids++;
777bc0f2195SMike Smith     }
778bc0f2195SMike Smith  out:
779bc0f2195SMike Smith     return_VALUE(result);
780bc0f2195SMike Smith }
781bc0f2195SMike Smith 
782bc0f2195SMike Smith /*
78315e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
78415e32d5dSMike Smith  *
7857d3bcec9SMike Smith  * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and \_SB_ scopes,
7867d3bcec9SMike Smith  * and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
78715e32d5dSMike Smith  */
78815e32d5dSMike Smith static void
78915e32d5dSMike Smith acpi_probe_children(device_t bus)
79015e32d5dSMike Smith {
79115e32d5dSMike Smith     ACPI_HANDLE		parent;
7927d3bcec9SMike Smith     static char		*scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
79315e32d5dSMike Smith     int			i;
79415e32d5dSMike Smith 
795b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
796cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
7970ae55423SMike Smith 
79815e32d5dSMike Smith     /*
79915e32d5dSMike Smith      * Create any static children by calling device identify methods.
80015e32d5dSMike Smith      */
8014c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
80215e32d5dSMike Smith     bus_generic_probe(bus);
80315e32d5dSMike Smith 
80415e32d5dSMike Smith     /*
80515e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
80615e32d5dSMike Smith      * we find.
80715e32d5dSMike Smith      *
80815e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
80915e32d5dSMike Smith      * we want to create nodes for all devices, not just those that are currently
81015e32d5dSMike Smith      * present. (This assumes that we don't want to create/remove devices as they
81115e32d5dSMike Smith      * appear, which might be smarter.)
81215e32d5dSMike Smith      */
8134c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
81415e32d5dSMike Smith     for (i = 0; scopes[i] != NULL; i++)
815b53f2771SMike Smith 	if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent)))
81615e32d5dSMike Smith 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL);
81715e32d5dSMike Smith 
81815e32d5dSMike Smith     /*
81915e32d5dSMike Smith      * Scan all of the child devices we have created and let them probe/attach.
82015e32d5dSMike Smith      */
8214c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
82215e32d5dSMike Smith     bus_generic_attach(bus);
82315e32d5dSMike Smith 
82415e32d5dSMike Smith     /*
82515e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
82615e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
82715e32d5dSMike Smith      */
8284c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
82915e32d5dSMike Smith     bus_generic_attach(bus);
8300ae55423SMike Smith 
831bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
8320ae55423SMike Smith     return_VOID;
83315e32d5dSMike Smith }
83415e32d5dSMike Smith 
83515e32d5dSMike Smith /*
83615e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
83715e32d5dSMike Smith  * it.
83815e32d5dSMike Smith  */
83915e32d5dSMike Smith static ACPI_STATUS
84015e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
84115e32d5dSMike Smith {
84215e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
84315e32d5dSMike Smith     device_t		child, bus = (device_t)context;
84415e32d5dSMike Smith 
845b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
8460ae55423SMike Smith 
8470ae55423SMike Smith     /*
8480ae55423SMike Smith      * Skip this device if we think we'll have trouble with it.
8490ae55423SMike Smith      */
8500ae55423SMike Smith     if (acpi_avoid(handle))
8510ae55423SMike Smith 	return_ACPI_STATUS(AE_OK);
8520ae55423SMike Smith 
853b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
85415e32d5dSMike Smith 	switch(type) {
85515e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
85615e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
85715e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
85815e32d5dSMike Smith 	case ACPI_TYPE_POWER:
8590ae55423SMike Smith 	    if (acpi_disabled("children"))
8600ae55423SMike Smith 		break;
86115e32d5dSMike Smith 	    /*
86215e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
86315e32d5dSMike Smith 	     * so that the probe/attach passes will run breadth-first.
86415e32d5dSMike Smith 	     */
8654c1cdee6SMike Smith 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", acpi_name(handle)));
86615e32d5dSMike Smith 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
867bc0f2195SMike Smith 	    if (child == NULL)
868bc0f2195SMike Smith 		break;
86915e32d5dSMike Smith 	    acpi_set_handle(child, handle);
870bc0f2195SMike Smith 
871bc0f2195SMike Smith 	    /*
872b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
873b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
874b519f9d6SMike Smith 	     * the handle, but we don't probe it).
875b519f9d6SMike Smith 	     */
87623b4e251SMitsuru IWASAKI 	    if ((type == ACPI_TYPE_DEVICE) && (!acpi_DeviceIsPresent(child))) {
877b519f9d6SMike Smith 		device_disable(child);
878b519f9d6SMike Smith 		break;
879b519f9d6SMike Smith 	    }
880b519f9d6SMike Smith 
881b519f9d6SMike Smith 	    /*
882bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
883bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
884bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
885bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
886bc0f2195SMike Smith 	     * device not to have any resources.
887bc0f2195SMike Smith 	     */
888bc0f2195SMike Smith 	    acpi_parse_resources(child, handle, &acpi_res_parse_set);
889bc0f2195SMike Smith 
890bc0f2195SMike Smith 	    /* if we're debugging, probe/attach now rather than later */
891b53f2771SMike Smith 	    ACPI_DEBUG_EXEC(device_probe_and_attach(child));
8920ae55423SMike Smith 	    break;
89315e32d5dSMike Smith 	}
89415e32d5dSMike Smith     }
8950ae55423SMike Smith     return_ACPI_STATUS(AE_OK);
89615e32d5dSMike Smith }
89715e32d5dSMike Smith 
89815e32d5dSMike Smith static void
89915e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto)
90015e32d5dSMike Smith {
901cb9b0d80SMike Smith 
902c6a78e98STakanori Watanabe     struct acpi_softc *sc = arg;
903c6a78e98STakanori Watanabe 
904cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
905cb9b0d80SMike Smith 
90615e32d5dSMike Smith     /*
9070ae55423SMike Smith      * Disable all ACPI events before soft off, otherwise the system
90815e32d5dSMike Smith      * will be turned on again on some laptops.
90915e32d5dSMike Smith      *
91015e32d5dSMike Smith      * XXX this should probably be restricted to masking some events just
91115e32d5dSMike Smith      *     before powering down, since we may still need ACPI during the
91215e32d5dSMike Smith      *     shutdown process.
91315e32d5dSMike Smith      */
914c6a78e98STakanori Watanabe     if (sc->acpi_disable_on_poweroff)
915c6a78e98STakanori Watanabe 	acpi_Disable(sc);
91615e32d5dSMike Smith }
91715e32d5dSMike Smith 
91815e32d5dSMike Smith static void
91915e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
92015e32d5dSMike Smith {
92115e32d5dSMike Smith     ACPI_STATUS	status;
92215e32d5dSMike Smith 
923cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
924cb9b0d80SMike Smith 
9255f3e4175SMitsuru IWASAKI     if (howto & RB_POWEROFF) {
92615e32d5dSMike Smith 	printf("Power system off using ACPI...\n");
927b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(acpi_off_state))) {
928b9c780d6SMitsuru IWASAKI 	    printf("AcpiEnterSleepStatePrep failed - %s\n",
929b9c780d6SMitsuru IWASAKI 		   AcpiFormatException(status));
930b9c780d6SMitsuru IWASAKI 	    return;
931b9c780d6SMitsuru IWASAKI 	}
932b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepState(acpi_off_state))) {
933bfae45aaSMike Smith 	    printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
93415e32d5dSMike Smith 	} else {
93515e32d5dSMike Smith 	    DELAY(1000000);
93615e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
93715e32d5dSMike Smith 	}
938494ae560SMitsuru IWASAKI     } else {
939494ae560SMitsuru IWASAKI 	printf("Terminate ACPI\n");
940494ae560SMitsuru IWASAKI 	AcpiTerminate();
94115e32d5dSMike Smith     }
94215e32d5dSMike Smith }
94315e32d5dSMike Smith 
94413d4f7baSMitsuru IWASAKI static void
94513d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
94613d4f7baSMitsuru IWASAKI {
94713d4f7baSMitsuru IWASAKI     static int	first_time = 1;
94813d4f7baSMitsuru IWASAKI #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n"
94913d4f7baSMitsuru IWASAKI 
950cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
951cb9b0d80SMike Smith 
95213d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
953cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) {
95413d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
95513d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_power_button_for_sleep, sc);
95613d4f7baSMitsuru IWASAKI 	if (first_time) {
95713d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "power");
95813d4f7baSMitsuru IWASAKI 	}
95913d4f7baSMitsuru IWASAKI     }
960cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) {
96113d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
96213d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_sleep_button_for_sleep, sc);
96313d4f7baSMitsuru IWASAKI 	if (first_time) {
96413d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "sleep");
96513d4f7baSMitsuru IWASAKI 	}
96613d4f7baSMitsuru IWASAKI     }
96713d4f7baSMitsuru IWASAKI 
96813d4f7baSMitsuru IWASAKI     first_time = 0;
96913d4f7baSMitsuru IWASAKI }
97013d4f7baSMitsuru IWASAKI 
97115e32d5dSMike Smith /*
972c5ba0be4SMike Smith  * Returns true if the device is actually present and should
973c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
974c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
975c5ba0be4SMike Smith  */
976c5ba0be4SMike Smith BOOLEAN
977c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
978c5ba0be4SMike Smith {
979c5ba0be4SMike Smith     ACPI_HANDLE		h;
980c5ba0be4SMike Smith     ACPI_DEVICE_INFO	devinfo;
9816fca9360SNate Lawson     ACPI_BUFFER		buf = {sizeof(devinfo), &devinfo};
982c5ba0be4SMike Smith     ACPI_STATUS		error;
983c5ba0be4SMike Smith 
984cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
985cb9b0d80SMike Smith 
986c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
987c5ba0be4SMike Smith 	return(FALSE);
9886fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
9896fca9360SNate Lawson     if (ACPI_FAILURE(error))
990c5ba0be4SMike Smith 	return(FALSE);
99143896e91SMike Smith     /* if no _STA method, must be present */
9926fca9360SNate Lawson     if ((devinfo.Valid & ACPI_VALID_STA) == 0)
99343896e91SMike Smith 	return(TRUE);
99443896e91SMike Smith     /* return true for 'present' and 'functioning' */
99543896e91SMike Smith     if ((devinfo.CurrentStatus & 0x9) == 0x9)
996c5ba0be4SMike Smith 	return(TRUE);
997c5ba0be4SMike Smith     return(FALSE);
998c5ba0be4SMike Smith }
999c5ba0be4SMike Smith 
1000c5ba0be4SMike Smith /*
1001b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
1002b53f2771SMike Smith  */
1003b53f2771SMike Smith BOOLEAN
1004b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
1005b53f2771SMike Smith {
1006b53f2771SMike Smith     ACPI_HANDLE		h;
1007b53f2771SMike Smith     ACPI_DEVICE_INFO	devinfo;
10086fca9360SNate Lawson     ACPI_BUFFER		buf = {sizeof(devinfo), &devinfo};
1009b53f2771SMike Smith     ACPI_STATUS		error;
1010b53f2771SMike Smith 
1011b53f2771SMike Smith     ACPI_ASSERTLOCK;
1012b53f2771SMike Smith 
1013b53f2771SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1014b53f2771SMike Smith 	return(FALSE);
10156fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
10166fca9360SNate Lawson     if (ACPI_FAILURE(error))
1017b53f2771SMike Smith 	return(FALSE);
1018b53f2771SMike Smith     /* if no _STA method, must be present */
10196fca9360SNate Lawson     if ((devinfo.Valid & ACPI_VALID_STA) == 0)
1020b53f2771SMike Smith 	return(TRUE);
1021b53f2771SMike Smith     /* return true for 'present' and 'functioning' */
1022b53f2771SMike Smith     if ((devinfo.CurrentStatus & 0x19) == 0x19)
1023b53f2771SMike Smith 	return(TRUE);
1024b53f2771SMike Smith     return(FALSE);
1025b53f2771SMike Smith }
1026b53f2771SMike Smith 
1027b53f2771SMike Smith /*
102815e32d5dSMike Smith  * Match a HID string against a device
102915e32d5dSMike Smith  */
103015e32d5dSMike Smith BOOLEAN
103115e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid)
103215e32d5dSMike Smith {
103315e32d5dSMike Smith     ACPI_HANDLE		h;
103415e32d5dSMike Smith     ACPI_DEVICE_INFO	devinfo;
10356fca9360SNate Lawson     ACPI_BUFFER		buf = {sizeof(devinfo), &devinfo};
103615e32d5dSMike Smith     ACPI_STATUS		error;
1037ed136da6SDoug Rabson     int			cid;
103815e32d5dSMike Smith 
1039cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1040cb9b0d80SMike Smith 
10414d332989SMike Smith     if (hid == NULL)
104215e32d5dSMike Smith 	return(FALSE);
104315e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
104415e32d5dSMike Smith 	return(FALSE);
10456fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
10466fca9360SNate Lawson     if (ACPI_FAILURE(error))
104715e32d5dSMike Smith 	return(FALSE);
10486fca9360SNate Lawson     if ((devinfo.Valid & ACPI_VALID_HID) != 0 &&
10496fca9360SNate Lawson 	strcmp(hid, devinfo.HardwareId.Value) == 0)
105015e32d5dSMike Smith 	return(TRUE);
1051b53f2771SMike Smith     if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &cid)))
1052ed136da6SDoug Rabson 	return(FALSE);
1053ed136da6SDoug Rabson     if (cid == PNP_EISAID(hid))
1054ed136da6SDoug Rabson 	return(TRUE);
105515e32d5dSMike Smith     return(FALSE);
105615e32d5dSMike Smith }
105715e32d5dSMike Smith 
105815e32d5dSMike Smith /*
1059c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1060c5ba0be4SMike Smith  * or one if its parents.
1061c5ba0be4SMike Smith  */
1062c5ba0be4SMike Smith ACPI_STATUS
1063c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1064c5ba0be4SMike Smith {
1065c5ba0be4SMike Smith     ACPI_HANDLE		r;
1066c5ba0be4SMike Smith     ACPI_STATUS		status;
1067c5ba0be4SMike Smith 
1068cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1069cb9b0d80SMike Smith 
1070c5ba0be4SMike Smith     /* walk back up the tree to the root */
1071c5ba0be4SMike Smith     for (;;) {
1072b53f2771SMike Smith 	if (ACPI_SUCCESS(status = AcpiGetHandle(parent, path, &r))) {
1073c5ba0be4SMike Smith 	    *result = r;
1074c5ba0be4SMike Smith 	    return(AE_OK);
1075c5ba0be4SMike Smith 	}
1076c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1077c5ba0be4SMike Smith 	    return(AE_OK);
1078b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1079c5ba0be4SMike Smith 	    return(AE_NOT_FOUND);
1080c5ba0be4SMike Smith 	parent = r;
1081c5ba0be4SMike Smith     }
1082c5ba0be4SMike Smith }
1083c5ba0be4SMike Smith 
1084c5ba0be4SMike Smith /*
1085c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1086c5ba0be4SMike Smith  */
1087c5ba0be4SMike Smith ACPI_BUFFER *
1088c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1089c5ba0be4SMike Smith {
1090c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1091c5ba0be4SMike Smith 
1092c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1093c5ba0be4SMike Smith 	return(NULL);
1094c5ba0be4SMike Smith     buf->Length = size;
1095c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1096c5ba0be4SMike Smith     return(buf);
1097c5ba0be4SMike Smith }
1098c5ba0be4SMike Smith 
1099c5ba0be4SMike Smith /*
1100c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1101c5ba0be4SMike Smith  */
1102c5ba0be4SMike Smith ACPI_STATUS
1103c5ba0be4SMike Smith acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number)
1104c5ba0be4SMike Smith {
1105c5ba0be4SMike Smith     ACPI_STATUS	error;
1106c5ba0be4SMike Smith     ACPI_BUFFER	buf;
1107c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
1108c5ba0be4SMike Smith 
1109cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1110cb9b0d80SMike Smith 
1111c5ba0be4SMike Smith     if (handle == NULL)
1112c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
11130c7da7acSJohn Baldwin 
11140c7da7acSJohn Baldwin     /*
11150c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
11160c7da7acSJohn Baldwin      * a method that will return an Integer.
11170c7da7acSJohn Baldwin      */
1118c5ba0be4SMike Smith     buf.Pointer = &param;
1119c5ba0be4SMike Smith     buf.Length = sizeof(param);
1120b53f2771SMike Smith     if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
1121c5ba0be4SMike Smith 	if (param.Type == ACPI_TYPE_INTEGER) {
1122c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1123c5ba0be4SMike Smith 	} else {
1124c5ba0be4SMike Smith 	    error = AE_TYPE;
1125c5ba0be4SMike Smith 	}
1126c5ba0be4SMike Smith     }
11270c7da7acSJohn Baldwin 
11280c7da7acSJohn Baldwin     /*
11290c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
11300c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
11310c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
11320c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
11330c7da7acSJohn Baldwin      *
11340c7da7acSJohn Baldwin      * This is a hack.
11350c7da7acSJohn Baldwin      */
11360c7da7acSJohn Baldwin     if (error == AE_BUFFER_OVERFLOW) {
1137b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
11380c7da7acSJohn Baldwin 	    error = AE_NO_MEMORY;
11390c7da7acSJohn Baldwin 	} else {
1140b53f2771SMike Smith 	    if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
1141c573e654SMitsuru IWASAKI 		error = acpi_ConvertBufferToInteger(&buf, number);
11420c7da7acSJohn Baldwin 	    }
11430c7da7acSJohn Baldwin 	}
11440c7da7acSJohn Baldwin 	AcpiOsFree(buf.Pointer);
11450c7da7acSJohn Baldwin     }
1146c5ba0be4SMike Smith     return(error);
1147c5ba0be4SMike Smith }
1148c5ba0be4SMike Smith 
1149c573e654SMitsuru IWASAKI ACPI_STATUS
1150c573e654SMitsuru IWASAKI acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number)
1151c573e654SMitsuru IWASAKI {
1152c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
1153c573e654SMitsuru IWASAKI     int		i;
1154c573e654SMitsuru IWASAKI 
1155c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
1156c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
1157c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
1158c573e654SMitsuru IWASAKI 	return(AE_OK);
1159c573e654SMitsuru IWASAKI     }
1160c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
1161c573e654SMitsuru IWASAKI 	return(AE_TYPE);
1162c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
1163c573e654SMitsuru IWASAKI 	return(AE_BAD_DATA);
1164c573e654SMitsuru IWASAKI     *number = 0;
1165c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
1166c573e654SMitsuru IWASAKI 	*number += (*(p->Buffer.Pointer + i) << (i * 8));
1167c573e654SMitsuru IWASAKI     return(AE_OK);
1168c573e654SMitsuru IWASAKI }
1169c573e654SMitsuru IWASAKI 
1170c5ba0be4SMike Smith /*
1171c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1172c5ba0be4SMike Smith  * function for each element.
1173c5ba0be4SMike Smith  *
1174c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1175c5ba0be4SMike Smith  */
1176c5ba0be4SMike Smith ACPI_STATUS
1177c5ba0be4SMike Smith acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg)
1178c5ba0be4SMike Smith {
1179c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1180c5ba0be4SMike Smith     int		i;
1181c5ba0be4SMike Smith 
1182c5ba0be4SMike Smith     if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE))
1183c5ba0be4SMike Smith 	return(AE_BAD_PARAMETER);
1184c5ba0be4SMike Smith 
1185c5ba0be4SMike Smith     /* iterate over components */
1186c5ba0be4SMike Smith     for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++)
1187c5ba0be4SMike Smith 	func(comp, arg);
1188c5ba0be4SMike Smith 
1189c5ba0be4SMike Smith     return(AE_OK);
1190c5ba0be4SMike Smith }
1191c5ba0be4SMike Smith 
11926f69255bSMike Smith /*
11936f69255bSMike Smith  * Find the (index)th resource object in a set.
11946f69255bSMike Smith  */
11956f69255bSMike Smith ACPI_STATUS
11966d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
11976f69255bSMike Smith {
11986d63101aSMike Smith     ACPI_RESOURCE	*rp;
11996f69255bSMike Smith     int			i;
12006f69255bSMike Smith 
12016d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
12026f69255bSMike Smith     i = index;
12036d63101aSMike Smith     while (i-- > 0) {
12046f69255bSMike Smith 	/* range check */
12056d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
12066f69255bSMike Smith 	    return(AE_BAD_PARAMETER);
12076d63101aSMike Smith 	/* check for terminator */
12086d63101aSMike Smith 	if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
12096d63101aSMike Smith 	    (rp->Length == 0))
12106d63101aSMike Smith 	    return(AE_NOT_FOUND);
12116d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
12126f69255bSMike Smith     }
12136f69255bSMike Smith     if (resp != NULL)
12146d63101aSMike Smith 	*resp = rp;
12156d63101aSMike Smith     return(AE_OK);
12166d63101aSMike Smith }
12176d63101aSMike Smith 
12186d63101aSMike Smith /*
12196d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
12206d63101aSMike Smith  *
12216d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
12226d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
12236d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
12246d63101aSMike Smith  * resources.
12256d63101aSMike Smith  */
12266d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
12276d63101aSMike Smith 
12286d63101aSMike Smith ACPI_STATUS
12296d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
12306d63101aSMike Smith {
12316d63101aSMike Smith     ACPI_RESOURCE	*rp;
12326d63101aSMike Smith     void		*newp;
12336d63101aSMike Smith 
12346d63101aSMike Smith     /*
12356d63101aSMike Smith      * Initialise the buffer if necessary.
12366d63101aSMike Smith      */
12376d63101aSMike Smith     if (buf->Pointer == NULL) {
12386d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
12396d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
12406d63101aSMike Smith 	    return(AE_NO_MEMORY);
12416d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
12426d63101aSMike Smith 	rp->Id = ACPI_RSTYPE_END_TAG;
12436d63101aSMike Smith 	rp->Length = 0;
12446d63101aSMike Smith     }
12456d63101aSMike Smith     if (res == NULL)
12466d63101aSMike Smith 	return(AE_OK);
12476d63101aSMike Smith 
12486d63101aSMike Smith     /*
12496d63101aSMike Smith      * Scan the current buffer looking for the terminator.
12506d63101aSMike Smith      * This will either find the terminator or hit the end
12516d63101aSMike Smith      * of the buffer and return an error.
12526d63101aSMike Smith      */
12536d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
12546d63101aSMike Smith     for (;;) {
12556d63101aSMike Smith 	/* range check, don't go outside the buffer */
12566d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
12576d63101aSMike Smith 	    return(AE_BAD_PARAMETER);
12586d63101aSMike Smith 	if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
12596d63101aSMike Smith 	    (rp->Length == 0)) {
12606d63101aSMike Smith 	    break;
12616d63101aSMike Smith 	}
12626d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
12636d63101aSMike Smith     }
12646d63101aSMike Smith 
12656d63101aSMike Smith     /*
12666d63101aSMike Smith      * Check the size of the buffer and expand if required.
12676d63101aSMike Smith      *
12686d63101aSMike Smith      * Required size is:
12696d63101aSMike Smith      *	size of existing resources before terminator +
12706d63101aSMike Smith      *	size of new resource and header +
12716d63101aSMike Smith      * 	size of terminator.
12726d63101aSMike Smith      *
12736d63101aSMike Smith      * Note that this loop should really only run once, unless
12746d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
12756d63101aSMike Smith      */
12766d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
12776d63101aSMike Smith 	    res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
12786d63101aSMike Smith 	    ACPI_RESOURCE_LENGTH) >= buf->Length) {
12796d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
12806d63101aSMike Smith 	    return(AE_NO_MEMORY);
12816d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
1282a692219dSMike Smith         rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1283a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
12846d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
12856d63101aSMike Smith 	buf->Pointer = newp;
12866d63101aSMike Smith 	buf->Length += buf->Length;
12876d63101aSMike Smith     }
12886d63101aSMike Smith 
12896d63101aSMike Smith     /*
12906d63101aSMike Smith      * Insert the new resource.
12916d63101aSMike Smith      */
12926d63101aSMike Smith     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
12936d63101aSMike Smith 
12946d63101aSMike Smith     /*
12956d63101aSMike Smith      * And add the terminator.
12966d63101aSMike Smith      */
12976d63101aSMike Smith     rp = ACPI_RESOURCE_NEXT(rp);
12986d63101aSMike Smith     rp->Id = ACPI_RSTYPE_END_TAG;
12996d63101aSMike Smith     rp->Length = 0;
13006d63101aSMike Smith 
13016f69255bSMike Smith     return(AE_OK);
13026f69255bSMike Smith }
1303c5ba0be4SMike Smith 
1304da14ac9fSJohn Baldwin /*
1305da14ac9fSJohn Baldwin  * Set interrupt model.
1306da14ac9fSJohn Baldwin  */
1307da14ac9fSJohn Baldwin ACPI_STATUS
1308da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
1309da14ac9fSJohn Baldwin {
1310da14ac9fSJohn Baldwin 	ACPI_OBJECT_LIST ArgList;
1311da14ac9fSJohn Baldwin 	ACPI_OBJECT Arg;
1312da14ac9fSJohn Baldwin 
1313da14ac9fSJohn Baldwin 	Arg.Type = ACPI_TYPE_INTEGER;
1314da14ac9fSJohn Baldwin 	Arg.Integer.Value = model;
1315da14ac9fSJohn Baldwin 	ArgList.Count = 1;
1316da14ac9fSJohn Baldwin 	ArgList.Pointer = &Arg;
1317da14ac9fSJohn Baldwin 	return (AcpiEvaluateObject(ACPI_ROOT_OBJECT, "_PIC", &ArgList, NULL));
1318da14ac9fSJohn Baldwin }
1319da14ac9fSJohn Baldwin 
1320ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME	5
1321ece50487SMitsuru IWASAKI 
1322ece50487SMitsuru IWASAKI static void
1323ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
1324ece50487SMitsuru IWASAKI {
1325ece50487SMitsuru IWASAKI     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1326ece50487SMitsuru IWASAKI }
1327c30382dfSMitsuru IWASAKI 
132815e32d5dSMike Smith /*
132915e32d5dSMike Smith  * Set the system sleep state
133015e32d5dSMike Smith  *
133115e32d5dSMike Smith  * Currently we only support S1 and S5
133215e32d5dSMike Smith  */
133315e32d5dSMike Smith ACPI_STATUS
133415e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
133515e32d5dSMike Smith {
133615e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
133756d8cb57SMitsuru IWASAKI     UINT8	TypeA;
133856d8cb57SMitsuru IWASAKI     UINT8	TypeB;
133915e32d5dSMike Smith 
1340b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1341cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
13420ae55423SMike Smith 
13436397624dSMitsuru IWASAKI     if (sc->acpi_sstate != ACPI_STATE_S0)
13446397624dSMitsuru IWASAKI 	return_ACPI_STATUS(AE_BAD_PARAMETER);	/* avoid reentry */
13456397624dSMitsuru IWASAKI 
1346ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1347ece50487SMitsuru IWASAKI 	return_ACPI_STATUS(AE_OK);
1348ece50487SMitsuru IWASAKI 
134915e32d5dSMike Smith     switch (state) {
135015e32d5dSMike Smith     case ACPI_STATE_S0:	/* XXX only for testing */
1351b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
1352bfae45aaSMike Smith 	    device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
135315e32d5dSMike Smith 	}
135415e32d5dSMike Smith 	break;
135515e32d5dSMike Smith 
135615e32d5dSMike Smith     case ACPI_STATE_S1:
13576161544cSTakanori Watanabe     case ACPI_STATE_S2:
13586161544cSTakanori Watanabe     case ACPI_STATE_S3:
13596161544cSTakanori Watanabe     case ACPI_STATE_S4:
136098479b04SMitsuru IWASAKI 	if (ACPI_FAILURE(status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB))) {
136198479b04SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", AcpiFormatException(status));
136256d8cb57SMitsuru IWASAKI 	    break;
136356d8cb57SMitsuru IWASAKI 	}
136456d8cb57SMitsuru IWASAKI 
1365a1fccb47SMitsuru IWASAKI 	sc->acpi_sstate = state;
1366a1fccb47SMitsuru IWASAKI 	sc->acpi_sleep_disabled = 1;
1367a1fccb47SMitsuru IWASAKI 
136815e32d5dSMike Smith 	/*
136915e32d5dSMike Smith 	 * Inform all devices that we are going to sleep.
137015e32d5dSMike Smith 	 */
137115e32d5dSMike Smith 	if (DEVICE_SUSPEND(root_bus) != 0) {
137215e32d5dSMike Smith 	    /*
137315e32d5dSMike Smith 	     * Re-wake the system.
137415e32d5dSMike Smith 	     *
137515e32d5dSMike Smith 	     * XXX note that a better two-pass approach with a 'veto' pass
137615e32d5dSMike Smith 	     *     followed by a "real thing" pass would be better, but the
137715e32d5dSMike Smith 	     *     current bus interface does not provide for this.
137815e32d5dSMike Smith 	     */
137915e32d5dSMike Smith 	    DEVICE_RESUME(root_bus);
13800ae55423SMike Smith 	    return_ACPI_STATUS(AE_ERROR);
138115e32d5dSMike Smith 	}
1382b9c780d6SMitsuru IWASAKI 
1383b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(state))) {
1384b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1385b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
1386b9c780d6SMitsuru IWASAKI 	    break;
1387b9c780d6SMitsuru IWASAKI 	}
1388b9c780d6SMitsuru IWASAKI 
1389ff01efb5SMitsuru IWASAKI 	if (sc->acpi_sleep_delay > 0) {
1390ff01efb5SMitsuru IWASAKI 	    DELAY(sc->acpi_sleep_delay * 1000000);
1391ff01efb5SMitsuru IWASAKI 	}
1392ff01efb5SMitsuru IWASAKI 
13936161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
13946161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
13956161544cSTakanori Watanabe 
1396a1fccb47SMitsuru IWASAKI 	    /* AcpiEnterSleepState() maybe incompleted, unlock here if locked. */
13976fca9360SNate Lawson 	    if (1/*AcpiGbl_AcpiMutexInfo[ACPI_MTX_HARDWARE].OwnerId != ACPI_MUTEX_NOT_ACQUIRED*/) {
13986161544cSTakanori Watanabe 		AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
1399a1fccb47SMitsuru IWASAKI 	    }
14006161544cSTakanori Watanabe 
14016161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1402964679ceSMitsuru IWASAKI 	    if (state == ACPI_STATE_S4) {
1403964679ceSMitsuru IWASAKI 		AcpiEnable();
14046161544cSTakanori Watanabe 	    }
14056161544cSTakanori Watanabe 	} else {
1406b53f2771SMike Smith 	    if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
1407bfae45aaSMike Smith 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
1408c30382dfSMitsuru IWASAKI 		break;
140915e32d5dSMike Smith 	    }
14106161544cSTakanori Watanabe 	}
1411ff741befSTakanori Watanabe 	AcpiLeaveSleepState((UINT8)state);
141215e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
141315e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
141413d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
141515e32d5dSMike Smith 	break;
141615e32d5dSMike Smith 
141715e32d5dSMike Smith     case ACPI_STATE_S5:
141815e32d5dSMike Smith 	/*
141915e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
142015e32d5dSMike Smith 	 * shutdown handlers.
142115e32d5dSMike Smith 	 */
142215e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
142315e32d5dSMike Smith 	break;
142415e32d5dSMike Smith 
142515e32d5dSMike Smith     default:
142615e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
142715e32d5dSMike Smith 	break;
142815e32d5dSMike Smith     }
1429ece50487SMitsuru IWASAKI 
1430ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1431ece50487SMitsuru IWASAKI 	timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1432ece50487SMitsuru IWASAKI 
14330ae55423SMike Smith     return_ACPI_STATUS(status);
143415e32d5dSMike Smith }
143515e32d5dSMike Smith 
143615e32d5dSMike Smith /*
143715e32d5dSMike Smith  * Enable/Disable ACPI
143815e32d5dSMike Smith  */
143915e32d5dSMike Smith ACPI_STATUS
144015e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
144115e32d5dSMike Smith {
144215e32d5dSMike Smith     ACPI_STATUS	status;
144315e32d5dSMike Smith     u_int32_t	flags;
144415e32d5dSMike Smith 
1445b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1446cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
14470ae55423SMike Smith 
144815e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
144915e32d5dSMike Smith             ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
145015e32d5dSMike Smith     if (!sc->acpi_enabled) {
145115e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
145215e32d5dSMike Smith     } else {
145315e32d5dSMike Smith 	status = AE_OK;
145415e32d5dSMike Smith     }
145515e32d5dSMike Smith     if (status == AE_OK)
145615e32d5dSMike Smith 	sc->acpi_enabled = 1;
14570ae55423SMike Smith     return_ACPI_STATUS(status);
145815e32d5dSMike Smith }
145915e32d5dSMike Smith 
146015e32d5dSMike Smith ACPI_STATUS
146115e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
146215e32d5dSMike Smith {
146315e32d5dSMike Smith     ACPI_STATUS	status;
146415e32d5dSMike Smith 
1465b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1466cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
14670ae55423SMike Smith 
146815e32d5dSMike Smith     if (sc->acpi_enabled) {
146915e32d5dSMike Smith 	status = AcpiDisable();
147015e32d5dSMike Smith     } else {
147115e32d5dSMike Smith 	status = AE_OK;
147215e32d5dSMike Smith     }
147315e32d5dSMike Smith     if (status == AE_OK)
147415e32d5dSMike Smith 	sc->acpi_enabled = 0;
14750ae55423SMike Smith     return_ACPI_STATUS(status);
147615e32d5dSMike Smith }
147715e32d5dSMike Smith 
147815e32d5dSMike Smith /*
147915e32d5dSMike Smith  * ACPI Event Handlers
148015e32d5dSMike Smith  */
148115e32d5dSMike Smith 
148215e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
148315e32d5dSMike Smith 
148415e32d5dSMike Smith static void
148515e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
148615e32d5dSMike Smith {
1487fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1488b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
148915e32d5dSMike Smith 
1490cb9b0d80SMike Smith     ACPI_LOCK;
1491a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
149215e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
1493cb9b0d80SMike Smith     ACPI_UNLOCK;
14940ae55423SMike Smith     return_VOID;
149515e32d5dSMike Smith }
149615e32d5dSMike Smith 
149715e32d5dSMike Smith static void
149815e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
149915e32d5dSMike Smith {
1500fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1501b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
15020ae55423SMike Smith 
150315e32d5dSMike Smith     /* Well, what to do? :-) */
15040ae55423SMike Smith 
1505cb9b0d80SMike Smith     ACPI_LOCK;
1506cb9b0d80SMike Smith     ACPI_UNLOCK;
1507cb9b0d80SMike Smith 
15080ae55423SMike Smith     return_VOID;
150915e32d5dSMike Smith }
151015e32d5dSMike Smith 
151115e32d5dSMike Smith /*
151215e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
151315e32d5dSMike Smith  */
151415e32d5dSMike Smith UINT32
151515e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context)
151615e32d5dSMike Smith {
151715e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
151815e32d5dSMike Smith 
1519b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15200ae55423SMike Smith 
152115e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
15220ae55423SMike Smith 
1523b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
152415e32d5dSMike Smith }
152515e32d5dSMike Smith 
152615e32d5dSMike Smith UINT32
152715e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context)
152815e32d5dSMike Smith {
152915e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
153015e32d5dSMike Smith 
1531b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15320ae55423SMike Smith 
153315e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
15340ae55423SMike Smith 
1535b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
153615e32d5dSMike Smith }
153715e32d5dSMike Smith 
153815e32d5dSMike Smith UINT32
153915e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context)
154015e32d5dSMike Smith {
154115e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
154215e32d5dSMike Smith 
1543b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15440ae55423SMike Smith 
154515e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
15460ae55423SMike Smith 
1547b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
154815e32d5dSMike Smith }
154915e32d5dSMike Smith 
155015e32d5dSMike Smith UINT32
155115e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context)
155215e32d5dSMike Smith {
155315e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
155415e32d5dSMike Smith 
1555b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15560ae55423SMike Smith 
155715e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
15580ae55423SMike Smith 
1559b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
156015e32d5dSMike Smith }
156115e32d5dSMike Smith 
156215e32d5dSMike Smith /*
156315e32d5dSMike Smith  * XXX This is kinda ugly, and should not be here.
156415e32d5dSMike Smith  */
156515e32d5dSMike Smith struct acpi_staticbuf {
156615e32d5dSMike Smith     ACPI_BUFFER	buffer;
156715e32d5dSMike Smith     char	data[512];
156815e32d5dSMike Smith };
156915e32d5dSMike Smith 
157015e32d5dSMike Smith char *
157115e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
157215e32d5dSMike Smith {
157315e32d5dSMike Smith     static struct acpi_staticbuf	buf;
157415e32d5dSMike Smith 
1575cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1576cb9b0d80SMike Smith 
157715e32d5dSMike Smith     buf.buffer.Length = 512;
157815e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
157915e32d5dSMike Smith 
1580b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
158115e32d5dSMike Smith 	return(buf.buffer.Pointer);
158215e32d5dSMike Smith     return("(unknown path)");
158315e32d5dSMike Smith }
158415e32d5dSMike Smith 
158515e32d5dSMike Smith /*
158615e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
158715e32d5dSMike Smith  * parts of the namespace.
158815e32d5dSMike Smith  */
158915e32d5dSMike Smith int
159015e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
159115e32d5dSMike Smith {
15923c600cfbSJohn Baldwin     char	*cp, *env, *np;
159315e32d5dSMike Smith     int		len;
159415e32d5dSMike Smith 
159515e32d5dSMike Smith     np = acpi_name(handle);
159615e32d5dSMike Smith     if (*np == '\\')
159715e32d5dSMike Smith 	np++;
15983c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
159915e32d5dSMike Smith 	return(0);
160015e32d5dSMike Smith 
160115e32d5dSMike Smith     /* scan the avoid list checking for a match */
16023c600cfbSJohn Baldwin     cp = env;
160315e32d5dSMike Smith     for (;;) {
160415e32d5dSMike Smith 	while ((*cp != 0) && isspace(*cp))
160515e32d5dSMike Smith 	    cp++;
160615e32d5dSMike Smith 	if (*cp == 0)
160715e32d5dSMike Smith 	    break;
160815e32d5dSMike Smith 	len = 0;
160915e32d5dSMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
161015e32d5dSMike Smith 	    len++;
1611d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
16123c600cfbSJohn Baldwin 	    freeenv(env);
16130ae55423SMike Smith 	    return(1);
1614d786139cSMaxime Henrion 	}
16150ae55423SMike Smith 	cp += len;
16160ae55423SMike Smith     }
16173c600cfbSJohn Baldwin     freeenv(env);
16180ae55423SMike Smith     return(0);
16190ae55423SMike Smith }
16200ae55423SMike Smith 
16210ae55423SMike Smith /*
16220ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
16230ae55423SMike Smith  */
16240ae55423SMike Smith int
16250ae55423SMike Smith acpi_disabled(char *subsys)
16260ae55423SMike Smith {
162778689b15SMaxime Henrion     char	*cp, *env;
16280ae55423SMike Smith     int		len;
16290ae55423SMike Smith 
163078689b15SMaxime Henrion     if ((env = getenv("debug.acpi.disable")) == NULL)
16310ae55423SMike Smith 	return(0);
163278689b15SMaxime Henrion     if (!strcmp(env, "all")) {
163378689b15SMaxime Henrion 	freeenv(env);
16340ae55423SMike Smith 	return(1);
1635d786139cSMaxime Henrion     }
16360ae55423SMike Smith 
16370ae55423SMike Smith     /* scan the disable list checking for a match */
163878689b15SMaxime Henrion     cp = env;
16390ae55423SMike Smith     for (;;) {
16400ae55423SMike Smith 	while ((*cp != 0) && isspace(*cp))
16410ae55423SMike Smith 	    cp++;
16420ae55423SMike Smith 	if (*cp == 0)
16430ae55423SMike Smith 	    break;
16440ae55423SMike Smith 	len = 0;
16450ae55423SMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
16460ae55423SMike Smith 	    len++;
1647d786139cSMaxime Henrion 	if (!strncmp(cp, subsys, len)) {
164878689b15SMaxime Henrion 	    freeenv(env);
164915e32d5dSMike Smith 	    return(1);
1650d786139cSMaxime Henrion 	}
165115e32d5dSMike Smith 	cp += len;
165215e32d5dSMike Smith     }
165378689b15SMaxime Henrion     freeenv(env);
165415e32d5dSMike Smith     return(0);
165515e32d5dSMike Smith }
165615e32d5dSMike Smith 
165715e32d5dSMike Smith /*
1658a1fccb47SMitsuru IWASAKI  * Device wake capability enable/disable.
1659a1fccb47SMitsuru IWASAKI  */
1660a1fccb47SMitsuru IWASAKI void
1661a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable)
1662a1fccb47SMitsuru IWASAKI {
1663a1fccb47SMitsuru IWASAKI     ACPI_OBJECT_LIST		ArgList;
1664a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			Arg;
1665a1fccb47SMitsuru IWASAKI 
1666a1fccb47SMitsuru IWASAKI     /*
1667a1fccb47SMitsuru IWASAKI      * TBD: All Power Resources referenced by elements 2 through N
1668a1fccb47SMitsuru IWASAKI      *      of the _PRW object are put into the ON state.
1669a1fccb47SMitsuru IWASAKI      */
1670a1fccb47SMitsuru IWASAKI 
1671a1fccb47SMitsuru IWASAKI     /*
1672a1fccb47SMitsuru IWASAKI      * enable/disable device wake function.
1673a1fccb47SMitsuru IWASAKI      */
1674a1fccb47SMitsuru IWASAKI 
1675a1fccb47SMitsuru IWASAKI     ArgList.Count = 1;
1676a1fccb47SMitsuru IWASAKI     ArgList.Pointer = &Arg;
1677a1fccb47SMitsuru IWASAKI 
1678a1fccb47SMitsuru IWASAKI     Arg.Type = ACPI_TYPE_INTEGER;
1679a1fccb47SMitsuru IWASAKI     Arg.Integer.Value = enable;
1680a1fccb47SMitsuru IWASAKI 
1681a1fccb47SMitsuru IWASAKI     (void)AcpiEvaluateObject(h, "_PSW", &ArgList, NULL);
1682a1fccb47SMitsuru IWASAKI }
1683a1fccb47SMitsuru IWASAKI 
1684a1fccb47SMitsuru IWASAKI void
1685a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_event(ACPI_HANDLE h)
1686a1fccb47SMitsuru IWASAKI {
1687a1fccb47SMitsuru IWASAKI     struct acpi_softc		*sc;
1688a1fccb47SMitsuru IWASAKI     ACPI_STATUS			status;
1689a1fccb47SMitsuru IWASAKI     ACPI_BUFFER			prw_buffer;
1690a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			*res;
1691a1fccb47SMitsuru IWASAKI 
1692a1fccb47SMitsuru IWASAKI     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1693a1fccb47SMitsuru IWASAKI 
1694a1fccb47SMitsuru IWASAKI     if ((sc = devclass_get_softc(acpi_devclass, 0)) == NULL) {
1695a1fccb47SMitsuru IWASAKI 	return;
1696a1fccb47SMitsuru IWASAKI     }
1697a1fccb47SMitsuru IWASAKI 
1698a1fccb47SMitsuru IWASAKI     /*
1699a1fccb47SMitsuru IWASAKI      * _PRW object is only required for devices that have the ability
1700a1fccb47SMitsuru IWASAKI      * to wake the system from a system sleeping state.
1701a1fccb47SMitsuru IWASAKI      */
1702a1fccb47SMitsuru IWASAKI     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
1703a1fccb47SMitsuru IWASAKI     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
1704a1fccb47SMitsuru IWASAKI     if (ACPI_FAILURE(status)) {
1705a1fccb47SMitsuru IWASAKI 	return;
1706a1fccb47SMitsuru IWASAKI     }
1707a1fccb47SMitsuru IWASAKI 
1708a1fccb47SMitsuru IWASAKI     res = (ACPI_OBJECT *)prw_buffer.Pointer;
1709a1fccb47SMitsuru IWASAKI     if (res == NULL) {
1710a1fccb47SMitsuru IWASAKI 	return;
1711a1fccb47SMitsuru IWASAKI     }
1712a1fccb47SMitsuru IWASAKI 
1713a1fccb47SMitsuru IWASAKI     if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) {
1714a1fccb47SMitsuru IWASAKI 	goto out;
1715a1fccb47SMitsuru IWASAKI     }
1716a1fccb47SMitsuru IWASAKI 
1717a1fccb47SMitsuru IWASAKI     /*
1718a1fccb47SMitsuru IWASAKI      * The element 1 of the _PRW object:
1719a1fccb47SMitsuru IWASAKI      * The lowest power system sleeping state that can be entered
1720a1fccb47SMitsuru IWASAKI      * while still providing wake functionality.
1721a1fccb47SMitsuru IWASAKI      * The sleeping state being entered must be greater or equal to
1722a1fccb47SMitsuru IWASAKI      * the power state declared in element 1 of the _PRW object.
1723a1fccb47SMitsuru IWASAKI      */
1724a1fccb47SMitsuru IWASAKI     if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER) {
1725a1fccb47SMitsuru IWASAKI 	goto out;
1726a1fccb47SMitsuru IWASAKI     }
1727a1fccb47SMitsuru IWASAKI 
1728a1fccb47SMitsuru IWASAKI     if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value) {
1729a1fccb47SMitsuru IWASAKI 	goto out;
1730a1fccb47SMitsuru IWASAKI     }
1731a1fccb47SMitsuru IWASAKI 
1732a1fccb47SMitsuru IWASAKI     /*
1733a1fccb47SMitsuru IWASAKI      * The element 0 of the _PRW object:
1734a1fccb47SMitsuru IWASAKI      */
1735a1fccb47SMitsuru IWASAKI     switch(res->Package.Elements[0].Type) {
1736a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_INTEGER:
1737a1fccb47SMitsuru IWASAKI 	/*
1738a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is numeric, then this
1739a1fccb47SMitsuru IWASAKI 	 * _PRW package element is the bit index in the GPEx_EN, in the
1740a1fccb47SMitsuru IWASAKI 	 * GPE blocks described in the FADT, of the enable bit that is
1741a1fccb47SMitsuru IWASAKI 	 * enabled for the wake event.
1742a1fccb47SMitsuru IWASAKI 	 */
1743a1fccb47SMitsuru IWASAKI 
17446fca9360SNate Lawson 	status = AcpiEnableGpe(NULL, res->Package.Elements[0].Integer.Value,
17456fca9360SNate Lawson 			       ACPI_EVENT_WAKE_ENABLE);
1746a1fccb47SMitsuru IWASAKI 	if (ACPI_FAILURE(status))
1747a1fccb47SMitsuru IWASAKI             printf("%s: EnableEvent Failed\n", __func__);
1748a1fccb47SMitsuru IWASAKI 	break;
1749a1fccb47SMitsuru IWASAKI 
1750a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_PACKAGE:
1751a1fccb47SMitsuru IWASAKI 	/* XXX TBD */
1752a1fccb47SMitsuru IWASAKI 
1753a1fccb47SMitsuru IWASAKI 	/*
1754a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is a package, then this
1755a1fccb47SMitsuru IWASAKI 	 * _PRW package element is itself a package containing two
1756a1fccb47SMitsuru IWASAKI 	 * elements. The first is an object reference to the GPE Block
1757a1fccb47SMitsuru IWASAKI 	 * device that contains the GPE that will be triggered by the wake
1758a1fccb47SMitsuru IWASAKI 	 * event. The second element is numeric and it contains the bit
1759a1fccb47SMitsuru IWASAKI 	 * index in the GPEx_EN, in the GPE Block referenced by the
1760a1fccb47SMitsuru IWASAKI 	 * first element in the package, of the enable bit that is enabled for
1761a1fccb47SMitsuru IWASAKI 	 * the wake event.
1762a1fccb47SMitsuru IWASAKI 	 * For example, if this field is a package then it is of the form:
1763a1fccb47SMitsuru IWASAKI 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
1764a1fccb47SMitsuru IWASAKI 	 */
1765a1fccb47SMitsuru IWASAKI 
1766a1fccb47SMitsuru IWASAKI 	break;
1767a1fccb47SMitsuru IWASAKI 
1768a1fccb47SMitsuru IWASAKI     default:
1769a1fccb47SMitsuru IWASAKI 	break;
1770a1fccb47SMitsuru IWASAKI     }
1771a1fccb47SMitsuru IWASAKI 
1772a1fccb47SMitsuru IWASAKI out:
1773a1fccb47SMitsuru IWASAKI     if (prw_buffer.Pointer != NULL)
1774a1fccb47SMitsuru IWASAKI 	AcpiOsFree(prw_buffer.Pointer);
1775a1fccb47SMitsuru IWASAKI     return;
1776a1fccb47SMitsuru IWASAKI }
1777a1fccb47SMitsuru IWASAKI 
1778a1fccb47SMitsuru IWASAKI /*
177915e32d5dSMike Smith  * Control interface.
178015e32d5dSMike Smith  *
17810ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
17820ae55423SMike Smith  * drivers wanting to be accessible via /dev/acpi should use the register/deregister
17830ae55423SMike Smith  * interface to make their handlers visible.
178415e32d5dSMike Smith  */
17850ae55423SMike Smith struct acpi_ioctl_hook
17860ae55423SMike Smith {
17870ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook)	link;
17880ae55423SMike Smith     u_long				cmd;
17890ae55423SMike Smith     int					(* fn)(u_long cmd, caddr_t addr, void *arg);
17900ae55423SMike Smith     void				*arg;
17910ae55423SMike Smith };
17920ae55423SMike Smith 
17930ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
17940ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
17950ae55423SMike Smith 
17960ae55423SMike Smith /*
17970ae55423SMike Smith  * Register an ioctl handler.
17980ae55423SMike Smith  */
17990ae55423SMike Smith int
18000ae55423SMike Smith acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg)
18010ae55423SMike Smith {
18020ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
18030ae55423SMike Smith 
18040ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
18050ae55423SMike Smith 	return(ENOMEM);
18060ae55423SMike Smith     hp->cmd = cmd;
18070ae55423SMike Smith     hp->fn = fn;
18080ae55423SMike Smith     hp->arg = arg;
18090ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
18100ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
18110ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
18120ae55423SMike Smith     }
18130ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
18140ae55423SMike Smith     return(0);
18150ae55423SMike Smith }
18160ae55423SMike Smith 
18170ae55423SMike Smith /*
18180ae55423SMike Smith  * Deregister an ioctl handler.
18190ae55423SMike Smith  */
18200ae55423SMike Smith void
18210ae55423SMike Smith acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg))
18220ae55423SMike Smith {
18230ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
18240ae55423SMike Smith 
18250ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
18260ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
18270ae55423SMike Smith 	    break;
18280ae55423SMike Smith 
18290ae55423SMike Smith     if (hp != NULL) {
18300ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
18310ae55423SMike Smith 	free(hp, M_ACPIDEV);
18320ae55423SMike Smith     }
18330ae55423SMike Smith }
18340ae55423SMike Smith 
183515e32d5dSMike Smith static int
18366b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td)
183715e32d5dSMike Smith {
183815e32d5dSMike Smith     return(0);
183915e32d5dSMike Smith }
184015e32d5dSMike Smith 
184115e32d5dSMike Smith static int
18426b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td)
184315e32d5dSMike Smith {
184415e32d5dSMike Smith     return(0);
184515e32d5dSMike Smith }
184615e32d5dSMike Smith 
184715e32d5dSMike Smith static int
18486b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
184915e32d5dSMike Smith {
185015e32d5dSMike Smith     struct acpi_softc		*sc;
18510ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
18520ae55423SMike Smith     int				error, xerror, state;
1853fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
185415e32d5dSMike Smith 
1855cb9b0d80SMike Smith     ACPI_LOCK;
1856cb9b0d80SMike Smith 
185715e32d5dSMike Smith     error = state = 0;
185815e32d5dSMike Smith     sc = dev->si_drv1;
185915e32d5dSMike Smith 
18600ae55423SMike Smith     /*
18610ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
18620ae55423SMike Smith      */
18630ae55423SMike Smith     if (acpi_ioctl_hooks_initted) {
18640ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
18650ae55423SMike Smith 	    if (hp->cmd == cmd) {
18660ae55423SMike Smith 		xerror = hp->fn(cmd, addr, hp->arg);
18670ae55423SMike Smith 		if (xerror != 0)
18680ae55423SMike Smith 		    error = xerror;
1869917d44c8SMitsuru IWASAKI 		goto out;
18700ae55423SMike Smith 	    }
18710ae55423SMike Smith 	}
18720ae55423SMike Smith     }
18730ae55423SMike Smith 
18740ae55423SMike Smith     /*
1875a89fcf28STakanori Watanabe      * Core ioctls are  not permitted for non-writable user.
1876a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
1877a89fcf28STakanori Watanabe      * Not changing system behavior.
1878a89fcf28STakanori Watanabe      */
1879a89fcf28STakanori Watanabe     if(!(flag & FWRITE)){
1880a89fcf28STakanori Watanabe 	    return EPERM;
1881a89fcf28STakanori Watanabe     }
1882a89fcf28STakanori Watanabe 
1883a89fcf28STakanori Watanabe     /*
18840ae55423SMike Smith      * Core system ioctls.
18850ae55423SMike Smith      */
188615e32d5dSMike Smith     switch (cmd) {
188715e32d5dSMike Smith     case ACPIIO_ENABLE:
18880ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
188915e32d5dSMike Smith 	    error = ENXIO;
189015e32d5dSMike Smith 	break;
189115e32d5dSMike Smith 
189215e32d5dSMike Smith     case ACPIIO_DISABLE:
18930ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
189415e32d5dSMike Smith 	    error = ENXIO;
189515e32d5dSMike Smith 	break;
189615e32d5dSMike Smith 
189715e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
189815e32d5dSMike Smith 	if (!sc->acpi_enabled) {
189915e32d5dSMike Smith 	    error = ENXIO;
190015e32d5dSMike Smith 	    break;
190115e32d5dSMike Smith 	}
190215e32d5dSMike Smith 	state = *(int *)addr;
1903a5d1879bSMitsuru IWASAKI 	if (state >= ACPI_STATE_S0  && state <= ACPI_S_STATES_MAX) {
190415e32d5dSMike Smith 	    acpi_SetSleepState(sc, state);
190515e32d5dSMike Smith 	} else {
190615e32d5dSMike Smith 	    error = EINVAL;
190715e32d5dSMike Smith 	}
190815e32d5dSMike Smith 	break;
190915e32d5dSMike Smith 
191015e32d5dSMike Smith     default:
19110ae55423SMike Smith 	if (error == 0)
191215e32d5dSMike Smith 	    error = EINVAL;
191315e32d5dSMike Smith 	break;
191415e32d5dSMike Smith     }
1915917d44c8SMitsuru IWASAKI 
1916917d44c8SMitsuru IWASAKI out:
1917cb9b0d80SMike Smith     ACPI_UNLOCK;
191815e32d5dSMike Smith     return(error);
191915e32d5dSMike Smith }
192015e32d5dSMike Smith 
19211d073b1dSJohn Baldwin static int
1922d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
1923d75de536SMitsuru IWASAKI {
1924d75de536SMitsuru IWASAKI     char sleep_state[4];
1925d75de536SMitsuru IWASAKI     char buf[16];
1926d75de536SMitsuru IWASAKI     int error;
1927d75de536SMitsuru IWASAKI     UINT8 state, TypeA, TypeB;
1928d75de536SMitsuru IWASAKI 
1929d75de536SMitsuru IWASAKI     buf[0] = '\0';
1930d75de536SMitsuru IWASAKI     for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX+1; state++) {
1931d75de536SMitsuru IWASAKI 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
1932d75de536SMitsuru IWASAKI 	    sprintf(sleep_state, "S%d ", state);
1933d75de536SMitsuru IWASAKI 	    strcat(buf, sleep_state);
1934d75de536SMitsuru IWASAKI 	}
1935d75de536SMitsuru IWASAKI     }
1936d75de536SMitsuru IWASAKI     error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1937d75de536SMitsuru IWASAKI     return(error);
1938d75de536SMitsuru IWASAKI }
1939d75de536SMitsuru IWASAKI 
1940d75de536SMitsuru IWASAKI static int
19411d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
19421d073b1dSJohn Baldwin {
19431d073b1dSJohn Baldwin     char sleep_state[10];
19441d073b1dSJohn Baldwin     int error;
19451d073b1dSJohn Baldwin     u_int new_state, old_state;
19461d073b1dSJohn Baldwin 
19471d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
1948b8670bedSMitsuru IWASAKI     if (old_state > ACPI_S_STATES_MAX+1) {
19491d073b1dSJohn Baldwin 	strcpy(sleep_state, "unknown");
1950cb9b0d80SMike Smith     } else {
1951b8670bedSMitsuru IWASAKI 	bzero(sleep_state, sizeof(sleep_state));
19521d073b1dSJohn Baldwin 	strncpy(sleep_state, sleep_state_names[old_state],
19531d073b1dSJohn Baldwin 		sizeof(sleep_state_names[old_state]));
1954cb9b0d80SMike Smith     }
19551d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
19561d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
1957b8670bedSMitsuru IWASAKI 	for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX+1; new_state++) {
19581d073b1dSJohn Baldwin 	    if (strncmp(sleep_state, sleep_state_names[new_state],
19591d073b1dSJohn Baldwin 			sizeof(sleep_state)) == 0)
19601d073b1dSJohn Baldwin 		break;
1961cb9b0d80SMike Smith 	}
1962931a10c9SMitsuru IWASAKI 	if (new_state <= ACPI_S_STATES_MAX+1) {
1963931a10c9SMitsuru IWASAKI 	    if (new_state != old_state) {
19641d073b1dSJohn Baldwin 		*(u_int *)oidp->oid_arg1 = new_state;
1965931a10c9SMitsuru IWASAKI 	    }
1966cb9b0d80SMike Smith 	} else {
19671d073b1dSJohn Baldwin 	    error = EINVAL;
19681d073b1dSJohn Baldwin 	}
1969cb9b0d80SMike Smith     }
19701d073b1dSJohn Baldwin     return(error);
19711d073b1dSJohn Baldwin }
19721d073b1dSJohn Baldwin 
197315e32d5dSMike Smith #ifdef ACPI_DEBUG
19740ae55423SMike Smith /*
19750ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
19760ae55423SMike Smith  *
19770ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
19780ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
19790ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
19800ae55423SMike Smith  * prefixing the bit name with !.
19810ae55423SMike Smith  */
198215e32d5dSMike Smith struct debugtag
198315e32d5dSMike Smith {
198415e32d5dSMike Smith     char	*name;
198515e32d5dSMike Smith     UINT32	value;
198615e32d5dSMike Smith };
198715e32d5dSMike Smith 
198815e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
1989c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
1990c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
1991c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
1992c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
1993c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
1994c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
1995c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
1996c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
1997c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
1998d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
19994c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
2000d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
20014c1cdee6SMike Smith 
2002cb9b0d80SMike Smith     {"ACPI_BUS",		ACPI_BUS},
20034c1cdee6SMike Smith     {"ACPI_SYSTEM",		ACPI_SYSTEM},
2004cb9b0d80SMike Smith     {"ACPI_POWER",		ACPI_POWER},
2005cb9b0d80SMike Smith     {"ACPI_EC", 		ACPI_EC},
2006c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
2007c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
2008c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
20094c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
2010cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
20114c1cdee6SMike Smith     {"ACPI_FAN",		ACPI_FAN},
20124c1cdee6SMike Smith 
2013b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
2014c5ba0be4SMike Smith     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
201515e32d5dSMike Smith     {NULL, 0}
201615e32d5dSMike Smith };
201715e32d5dSMike Smith 
201815e32d5dSMike Smith static struct debugtag dbg_level[] = {
20194c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
20209501b603SJohn Baldwin     {"ACPI_LV_WARN",		ACPI_LV_WARN},
20219501b603SJohn Baldwin     {"ACPI_LV_INIT",		ACPI_LV_INIT},
20224c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
20239501b603SJohn Baldwin     {"ACPI_LV_INFO",		ACPI_LV_INFO},
20244c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
2025d62ab2f4SMitsuru IWASAKI 
2026d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
20274c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
20284c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
2029d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
20304c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
20314c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
20324c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
20334c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
20344c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
20354c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
20364c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
20374c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
20384c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
20394c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
20409501b603SJohn Baldwin     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
2041d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
2042d62ab2f4SMitsuru IWASAKI 
2043d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
2044d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
2045d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
2046d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
2047d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
20484c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
2049d62ab2f4SMitsuru IWASAKI 
2050d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2051d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
2052d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
2053d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
2054d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
2055d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
2056d62ab2f4SMitsuru IWASAKI 
2057d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
205898479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
205998479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
206098479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
206198479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
206298479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
206315e32d5dSMike Smith     {NULL, 0}
206415e32d5dSMike Smith };
206515e32d5dSMike Smith 
206615e32d5dSMike Smith static void
206715e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
206815e32d5dSMike Smith {
206915e32d5dSMike Smith     char	*ep;
207015e32d5dSMike Smith     int		i, l;
20710ae55423SMike Smith     int		set;
207215e32d5dSMike Smith 
207315e32d5dSMike Smith     while (*cp) {
207415e32d5dSMike Smith 	if (isspace(*cp)) {
207515e32d5dSMike Smith 	    cp++;
207615e32d5dSMike Smith 	    continue;
207715e32d5dSMike Smith 	}
207815e32d5dSMike Smith 	ep = cp;
207915e32d5dSMike Smith 	while (*ep && !isspace(*ep))
208015e32d5dSMike Smith 	    ep++;
20810ae55423SMike Smith 	if (*cp == '!') {
20820ae55423SMike Smith 	    set = 0;
20830ae55423SMike Smith 	    cp++;
20840ae55423SMike Smith 	    if (cp == ep)
20850ae55423SMike Smith 		continue;
20860ae55423SMike Smith 	} else {
20870ae55423SMike Smith 	    set = 1;
20880ae55423SMike Smith 	}
208915e32d5dSMike Smith 	l = ep - cp;
209015e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
209115e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
20920ae55423SMike Smith 		if (set) {
209315e32d5dSMike Smith 		    *flag |= tag[i].value;
20940ae55423SMike Smith 		} else {
20950ae55423SMike Smith 		    *flag &= ~tag[i].value;
20960ae55423SMike Smith 		}
209715e32d5dSMike Smith 		printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
209815e32d5dSMike Smith 	    }
209915e32d5dSMike Smith 	}
210015e32d5dSMike Smith 	cp = ep;
210115e32d5dSMike Smith     }
210215e32d5dSMike Smith }
210315e32d5dSMike Smith 
210415e32d5dSMike Smith static void
21052a4ac806SMike Smith acpi_set_debugging(void *junk)
210615e32d5dSMike Smith {
210794aae282SMike Barcroft     char	*cp;
210815e32d5dSMike Smith 
210987b45ed5SMitsuru IWASAKI     if (!cold)
211087b45ed5SMitsuru IWASAKI 	return;
211187b45ed5SMitsuru IWASAKI 
21120ae55423SMike Smith     AcpiDbgLayer = 0;
21130ae55423SMike Smith     AcpiDbgLevel = 0;
211494aae282SMike Barcroft     if ((cp = getenv("debug.acpi.layer")) != NULL) {
211515e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
211694aae282SMike Barcroft 	freeenv(cp);
211794aae282SMike Barcroft     }
211894aae282SMike Barcroft     if ((cp = getenv("debug.acpi.level")) != NULL) {
211915e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
212094aae282SMike Barcroft 	freeenv(cp);
212194aae282SMike Barcroft     }
21220ae55423SMike Smith 
21230ae55423SMike Smith     printf("ACPI debug layer 0x%x  debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel);
212415e32d5dSMike Smith }
21252a4ac806SMike Smith SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL);
212615e32d5dSMike Smith #endif
2127f9390180SMitsuru IWASAKI 
2128f9390180SMitsuru IWASAKI static int
2129f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
2130f9390180SMitsuru IWASAKI {
2131f9390180SMitsuru IWASAKI 	int	state, acpi_state;
2132f9390180SMitsuru IWASAKI 	int	error;
2133f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
2134f9390180SMitsuru IWASAKI 	va_list	ap;
2135f9390180SMitsuru IWASAKI 
2136f9390180SMitsuru IWASAKI 	error = 0;
2137f9390180SMitsuru IWASAKI 	switch (cmd) {
2138f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
2139f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
2140f9390180SMitsuru IWASAKI 		if (sc == NULL) {
2141f9390180SMitsuru IWASAKI 			error = EINVAL;
2142f9390180SMitsuru IWASAKI 			goto out;
2143f9390180SMitsuru IWASAKI 		}
2144f9390180SMitsuru IWASAKI 
2145f9390180SMitsuru IWASAKI 		va_start(ap, arg);
2146f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
2147f9390180SMitsuru IWASAKI 		va_end(ap);
2148f9390180SMitsuru IWASAKI 
2149f9390180SMitsuru IWASAKI 		switch (state) {
2150f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
2151f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
2152f9390180SMitsuru IWASAKI 			break;
2153f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
2154f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
2155f9390180SMitsuru IWASAKI 			break;
2156f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
2157f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
2158f9390180SMitsuru IWASAKI 			break;
2159f9390180SMitsuru IWASAKI 		default:
2160f9390180SMitsuru IWASAKI 			error = EINVAL;
2161f9390180SMitsuru IWASAKI 			goto out;
2162f9390180SMitsuru IWASAKI 		}
2163f9390180SMitsuru IWASAKI 
2164f9390180SMitsuru IWASAKI 		acpi_SetSleepState(sc, acpi_state);
2165f9390180SMitsuru IWASAKI 		break;
2166f9390180SMitsuru IWASAKI 
2167f9390180SMitsuru IWASAKI 	default:
2168f9390180SMitsuru IWASAKI 		error = EINVAL;
2169f9390180SMitsuru IWASAKI 		goto out;
2170f9390180SMitsuru IWASAKI 	}
2171f9390180SMitsuru IWASAKI 
2172f9390180SMitsuru IWASAKI out:
2173f9390180SMitsuru IWASAKI 	return (error);
2174f9390180SMitsuru IWASAKI }
2175f9390180SMitsuru IWASAKI 
2176f9390180SMitsuru IWASAKI static void
2177f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
2178f9390180SMitsuru IWASAKI {
21796c407052SMitsuru IWASAKI 
218087b45ed5SMitsuru IWASAKI     if (!cold)
218187b45ed5SMitsuru IWASAKI 	return;
218287b45ed5SMitsuru IWASAKI 
21838a9bc9c0SJohn Baldwin     if (resource_disabled("acpi", 0))
21846c407052SMitsuru IWASAKI 		return;
2185f9390180SMitsuru IWASAKI 
2186f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
2187f9390180SMitsuru IWASAKI }
2188f9390180SMitsuru IWASAKI 
2189f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
2190f9390180SMitsuru IWASAKI 
2191