xref: /freebsd/sys/dev/acpica/acpi.c (revision bf0c18ec43e15dddd3fafd37d253c7afbf6dd507)
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 
386f8335e3aSNate Lawson     /*
387f8335e3aSNate Lawson      * Call the ECDT probe function to provide EC functionality before
388f8335e3aSNate Lawson      * the namespace has been evaluated.
389f8335e3aSNate Lawson      */
390f8335e3aSNate Lawson     acpi_ec_ecdt_probe(dev);
391f8335e3aSNate Lawson 
392b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
393b69ed3f4SMitsuru IWASAKI 	device_printf(dev, "could not initialize ACPI objects: %s\n", AcpiFormatException(status));
394b69ed3f4SMitsuru IWASAKI 	goto out;
395b69ed3f4SMitsuru IWASAKI     }
396b69ed3f4SMitsuru IWASAKI 
39715e32d5dSMike Smith     /*
3981d073b1dSJohn Baldwin      * Setup our sysctl tree.
3991d073b1dSJohn Baldwin      *
4001d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
4011d073b1dSJohn Baldwin      */
4021d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
4031d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
4041d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
4051d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
4061d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
407d75de536SMitsuru IWASAKI 	OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
408d75de536SMitsuru IWASAKI 	0, 0, acpi_supported_sleep_state_sysctl, "A", "");
409d75de536SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4101d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4111d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4121d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4131d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4141d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4151d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4161d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
4171d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
418f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
419f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
420f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
421f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
422f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
423f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
4242d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
425ff01efb5SMitsuru IWASAKI 	OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
426ff01efb5SMitsuru IWASAKI 	&sc->acpi_sleep_delay, 0, "sleep delay");
427ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4281611ea87SMitsuru IWASAKI 	OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
4291611ea87SMitsuru IWASAKI 	&sc->acpi_s4bios, 0, "S4BIOS mode");
4301611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4312d644607SMitsuru IWASAKI 	OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
4322d644607SMitsuru IWASAKI 	&sc->acpi_verbose, 0, "verbose mode");
433c6a78e98STakanori Watanabe     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
434c6a78e98STakanori Watanabe 		   OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW,
435c6a78e98STakanori Watanabe 		   &sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff");
436bf0c18ecSNate Lawson 
437bf0c18ecSNate Lawson     /*
438bf0c18ecSNate Lawson      * Default to 5 seconds before sleeping to give some machines time to
439bf0c18ecSNate Lawson      * stabilize.
440bf0c18ecSNate Lawson      */
441bf0c18ecSNate Lawson     sc->acpi_sleep_delay = 5;
442c6a78e98STakanori Watanabe     sc->acpi_disable_on_poweroff = 1;
443931a10c9SMitsuru IWASAKI     sc->acpi_s4bios = 1;
4442d644607SMitsuru IWASAKI     if (bootverbose)
4452d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
446498d464fSMitsuru IWASAKI     if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) {
447498d464fSMitsuru IWASAKI 	sc->acpi_verbose = 1;
448498d464fSMitsuru IWASAKI 	freeenv(env);
449498d464fSMitsuru IWASAKI     }
4501d073b1dSJohn Baldwin 
4511d073b1dSJohn Baldwin     /*
45215e32d5dSMike Smith      * Dispatch the default sleep state to devices.
45315e32d5dSMike Smith      * TBD: should be configured from userland policy manager.
45415e32d5dSMike Smith      */
45515e32d5dSMike Smith     sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
45615e32d5dSMike Smith     sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
45715e32d5dSMike Smith     sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
458f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
459f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
46015e32d5dSMike Smith 
46113d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
46215e32d5dSMike Smith 
46315e32d5dSMike Smith     /*
46415e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
46515e32d5dSMike Smith      */
466d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
467d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
468d786139cSMaxime Henrion     if (debugpoint) {
469d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "probe"))
47015e32d5dSMike Smith 	    acpi_EnterDebugger();
471d786139cSMaxime Henrion 	freeenv(debugpoint);
472d786139cSMaxime Henrion     }
47315e32d5dSMike Smith #endif
47415e32d5dSMike Smith 
47515e32d5dSMike Smith     /*
47615e32d5dSMike Smith      * Register our shutdown handlers
47715e32d5dSMike Smith      */
47815e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST);
47915e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST);
48015e32d5dSMike Smith 
48115e32d5dSMike Smith     /*
48215e32d5dSMike Smith      * Register our acpi event handlers.
48315e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
48415e32d5dSMike Smith      */
48515e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST);
48615e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST);
48715e32d5dSMike Smith 
48815e32d5dSMike Smith     /*
48915e32d5dSMike Smith      * Flag our initial states.
49015e32d5dSMike Smith      */
49115e32d5dSMike Smith     sc->acpi_enabled = 1;
49215e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
493ece50487SMitsuru IWASAKI     sc->acpi_sleep_disabled = 0;
49415e32d5dSMike Smith 
49515e32d5dSMike Smith     /*
49615e32d5dSMike Smith      * Create the control device
49715e32d5dSMike Smith      */
498a89fcf28STakanori Watanabe     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
4992a4689e8SRobert Watson 	"acpi");
50015e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
50115e32d5dSMike Smith 
502d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
503d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
504d786139cSMaxime Henrion     if (debugpoint) {
505d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "running"))
50615e32d5dSMike Smith 	    acpi_EnterDebugger();
507d786139cSMaxime Henrion 	freeenv(debugpoint);
508d786139cSMaxime Henrion     }
50915e32d5dSMike Smith #endif
510f86214b6SMitsuru IWASAKI 
51191da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS
512c573e654SMitsuru IWASAKI     if ((error = acpi_task_thread_init())) {
513c573e654SMitsuru IWASAKI 	goto out;
514c573e654SMitsuru IWASAKI     }
515c573e654SMitsuru IWASAKI #endif
516c573e654SMitsuru IWASAKI 
517f86214b6SMitsuru IWASAKI     if ((error = acpi_machdep_init(dev))) {
518f86214b6SMitsuru IWASAKI 	goto out;
519f86214b6SMitsuru IWASAKI     }
520f86214b6SMitsuru IWASAKI 
521f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
522f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
523f9390180SMitsuru IWASAKI 
524eeb6dba2SJohn Baldwin     if (!acpi_disabled("bus"))
525eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
526eeb6dba2SJohn Baldwin 
527cb9b0d80SMike Smith     error = 0;
528cb9b0d80SMike Smith 
529cb9b0d80SMike Smith  out:
530cb9b0d80SMike Smith     ACPI_UNLOCK;
531cb9b0d80SMike Smith     return_VALUE(error);
53215e32d5dSMike Smith }
53315e32d5dSMike Smith 
53415e32d5dSMike Smith /*
53515e32d5dSMike Smith  * Handle a new device being added
53615e32d5dSMike Smith  */
53715e32d5dSMike Smith static device_t
53815e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
53915e32d5dSMike Smith {
54015e32d5dSMike Smith     struct acpi_device	*ad;
54115e32d5dSMike Smith     device_t		child;
54215e32d5dSMike Smith 
54315e32d5dSMike Smith     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL)
54415e32d5dSMike Smith 	return(NULL);
54515e32d5dSMike Smith     bzero(ad, sizeof(*ad));
54615e32d5dSMike Smith 
54715e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
54815e32d5dSMike Smith 
54915e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
55015e32d5dSMike Smith     if (child != NULL)
55115e32d5dSMike Smith 	device_set_ivars(child, ad);
55215e32d5dSMike Smith     return(child);
55315e32d5dSMike Smith }
55415e32d5dSMike Smith 
55515e32d5dSMike Smith static int
55615e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
55715e32d5dSMike Smith {
55815e32d5dSMike Smith     struct acpi_device		*adev = device_get_ivars(child);
55915e32d5dSMike Smith     struct resource_list	*rl = &adev->ad_rl;
56015e32d5dSMike Smith     int retval = 0;
56115e32d5dSMike Smith 
56215e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
5639ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
5649ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
5659ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
5669ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
56715e32d5dSMike Smith     retval += bus_print_child_footer(bus, child);
56815e32d5dSMike Smith 
56915e32d5dSMike Smith     return(retval);
57015e32d5dSMike Smith }
57115e32d5dSMike Smith 
57215e32d5dSMike Smith 
57315e32d5dSMike Smith /*
57415e32d5dSMike Smith  * Handle per-device ivars
57515e32d5dSMike Smith  */
57615e32d5dSMike Smith static int
57715e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
57815e32d5dSMike Smith {
57915e32d5dSMike Smith     struct acpi_device	*ad;
58015e32d5dSMike Smith 
58115e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
58215e32d5dSMike Smith 	printf("device has no ivars\n");
58315e32d5dSMike Smith 	return(ENOENT);
58415e32d5dSMike Smith     }
58515e32d5dSMike Smith 
58615e32d5dSMike Smith     switch(index) {
58715e32d5dSMike Smith 	/* ACPI ivars */
58815e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
58915e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
59015e32d5dSMike Smith 	break;
59115e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
59215e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
59315e32d5dSMike Smith 	break;
59415e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
59515e32d5dSMike Smith 	*(void **)result = ad->ad_private;
59615e32d5dSMike Smith 	break;
59715e32d5dSMike Smith 
59832d18aa5SMike Smith 	/* ISA compatibility */
59932d18aa5SMike Smith     case ISA_IVAR_VENDORID:
60032d18aa5SMike Smith     case ISA_IVAR_SERIAL:
60132d18aa5SMike Smith     case ISA_IVAR_COMPATID:
60232d18aa5SMike Smith 	*(int *)result = -1;
60332d18aa5SMike Smith 	break;
60432d18aa5SMike Smith 
60532d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
60632d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
60732d18aa5SMike Smith 	break;
60832d18aa5SMike Smith 
60915e32d5dSMike Smith     default:
61015e32d5dSMike Smith 	return(ENOENT);
61115e32d5dSMike Smith     }
61215e32d5dSMike Smith     return(0);
61315e32d5dSMike Smith }
61415e32d5dSMike Smith 
61515e32d5dSMike Smith static int
61615e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
61715e32d5dSMike Smith {
61815e32d5dSMike Smith     struct acpi_device	*ad;
61915e32d5dSMike Smith 
62015e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
62115e32d5dSMike Smith 	printf("device has no ivars\n");
62215e32d5dSMike Smith 	return(ENOENT);
62315e32d5dSMike Smith     }
62415e32d5dSMike Smith 
62515e32d5dSMike Smith     switch(index) {
62615e32d5dSMike Smith 	/* ACPI ivars */
62715e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
62815e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
62915e32d5dSMike Smith 	break;
63015e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
63115e32d5dSMike Smith 	ad->ad_magic = (int )value;
63215e32d5dSMike Smith 	break;
63315e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
63415e32d5dSMike Smith 	ad->ad_private = (void *)value;
63515e32d5dSMike Smith 	break;
63615e32d5dSMike Smith 
63715e32d5dSMike Smith     default:
6382ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
63915e32d5dSMike Smith 	return(ENOENT);
64015e32d5dSMike Smith     }
64115e32d5dSMike Smith     return(0);
64215e32d5dSMike Smith }
64315e32d5dSMike Smith 
64415e32d5dSMike Smith /*
64515e32d5dSMike Smith  * Handle child resource allocation/removal
64615e32d5dSMike Smith  */
64715e32d5dSMike Smith static int
64815e32d5dSMike Smith acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
64915e32d5dSMike Smith {
65015e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
65115e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
65215e32d5dSMike Smith 
65315e32d5dSMike Smith     resource_list_add(rl, type, rid, start, start + count -1, count);
65415e32d5dSMike Smith 
65515e32d5dSMike Smith     return(0);
65615e32d5dSMike Smith }
65715e32d5dSMike Smith 
65815e32d5dSMike Smith static int
65915e32d5dSMike Smith acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
66015e32d5dSMike Smith {
66115e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
66215e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
66315e32d5dSMike Smith     struct resource_list_entry	*rle;
66415e32d5dSMike Smith 
66515e32d5dSMike Smith     rle = resource_list_find(rl, type, rid);
66615e32d5dSMike Smith     if (!rle)
66715e32d5dSMike Smith 	return(ENOENT);
66815e32d5dSMike Smith 
66915e32d5dSMike Smith     if (startp)
67015e32d5dSMike Smith 	*startp = rle->start;
67115e32d5dSMike Smith     if (countp)
67215e32d5dSMike Smith 	*countp = rle->count;
67315e32d5dSMike Smith 
67415e32d5dSMike Smith     return(0);
67515e32d5dSMike Smith }
67615e32d5dSMike Smith 
67715e32d5dSMike Smith static struct resource *
67815e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
67915e32d5dSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
68015e32d5dSMike Smith {
68115e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
68215e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
68315e32d5dSMike Smith 
68415e32d5dSMike Smith     return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags));
68515e32d5dSMike Smith }
68615e32d5dSMike Smith 
68715e32d5dSMike Smith static int
68815e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
68915e32d5dSMike Smith {
69015e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
69115e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
69215e32d5dSMike Smith 
69315e32d5dSMike Smith     return(resource_list_release(rl, bus, child, type, rid, r));
69415e32d5dSMike Smith }
69515e32d5dSMike Smith 
69615e32d5dSMike Smith /*
697bc0f2195SMike Smith  * Handle ISA-like devices probing for a PnP ID to match.
698bc0f2195SMike Smith  */
699bc0f2195SMike Smith #define PNP_EISAID(s)				\
700bc0f2195SMike Smith 	((((s[0] - '@') & 0x1f) << 2)		\
701bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x18) >> 3)		\
702bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x07) << 13)	\
703bc0f2195SMike Smith 	 | (((s[2] - '@') & 0x1f) << 8)		\
704bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[4]) << 16)		\
705bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[3]) << 20)		\
706bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[6]) << 24)		\
707bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[5]) << 28))
708bc0f2195SMike Smith 
70932d18aa5SMike Smith static u_int32_t
71032d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
711bc0f2195SMike Smith {
712bc0f2195SMike Smith     ACPI_HANDLE		h;
713bc0f2195SMike Smith     ACPI_DEVICE_INFO	devinfo;
7146fca9360SNate Lawson     ACPI_BUFFER		buf = {sizeof(devinfo), &devinfo};
715bc0f2195SMike Smith     ACPI_STATUS		error;
716bc0f2195SMike Smith     u_int32_t		pnpid;
717fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
71832d18aa5SMike Smith 
719b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
72032d18aa5SMike Smith 
72132d18aa5SMike Smith     pnpid = 0;
72232d18aa5SMike Smith     ACPI_LOCK;
72332d18aa5SMike Smith 
7246fca9360SNate Lawson     /* Fetch and validate the HID. */
72532d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
72632d18aa5SMike Smith 	goto out;
7276fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
7286fca9360SNate Lawson     if (ACPI_FAILURE(error))
72932d18aa5SMike Smith 	goto out;
7306fca9360SNate Lawson     if ((devinfo.Valid & ACPI_VALID_HID) == 0)
73132d18aa5SMike Smith 	goto out;
73232d18aa5SMike Smith 
7336fca9360SNate Lawson     pnpid = PNP_EISAID(devinfo.HardwareId.Value);
73432d18aa5SMike Smith out:
73532d18aa5SMike Smith     ACPI_UNLOCK;
73632d18aa5SMike Smith     return_VALUE(pnpid);
73732d18aa5SMike Smith }
73832d18aa5SMike Smith 
739b2566207STakanori Watanabe static u_int32_t
740b2566207STakanori Watanabe acpi_isa_get_compatid(device_t dev)
741b2566207STakanori Watanabe {
742b2566207STakanori Watanabe     ACPI_HANDLE		h;
743b2566207STakanori Watanabe     ACPI_STATUS		error;
744b2566207STakanori Watanabe     u_int32_t		pnpid;
745b2566207STakanori Watanabe     ACPI_LOCK_DECL;
746b2566207STakanori Watanabe 
747b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
748b2566207STakanori Watanabe 
749b2566207STakanori Watanabe     pnpid = 0;
750b2566207STakanori Watanabe     ACPI_LOCK;
751b2566207STakanori Watanabe 
752b2566207STakanori Watanabe     /* fetch and validate the HID */
753b2566207STakanori Watanabe     if ((h = acpi_get_handle(dev)) == NULL)
754b2566207STakanori Watanabe 	goto out;
755b2566207STakanori Watanabe     if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &pnpid)))
756b2566207STakanori Watanabe 	goto out;
757b2566207STakanori Watanabe 
758b2566207STakanori Watanabe out:
759b2566207STakanori Watanabe     ACPI_UNLOCK;
760b2566207STakanori Watanabe     return_VALUE(pnpid);
761b2566207STakanori Watanabe }
762b2566207STakanori Watanabe 
763b2566207STakanori Watanabe 
76432d18aa5SMike Smith static int
76532d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
76632d18aa5SMike Smith {
767bc0f2195SMike Smith     int			result;
768b2566207STakanori Watanabe     u_int32_t		lid, cid;
769bc0f2195SMike Smith 
770b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
771bc0f2195SMike Smith 
772bc0f2195SMike Smith     /*
773bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
774bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
775bc0f2195SMike Smith      * that to happen, so don't ever return it.
776bc0f2195SMike Smith      */
777bc0f2195SMike Smith     result = ENXIO;
778bc0f2195SMike Smith 
779bc0f2195SMike Smith     /* scan the supplied IDs for a match */
780b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
781b2566207STakanori Watanabe     cid = acpi_isa_get_compatid(child);
782bc0f2195SMike Smith     while (ids && ids->ip_id) {
783b2566207STakanori Watanabe 	if (lid == ids->ip_id || cid == ids->ip_id) {
784bc0f2195SMike Smith 	    result = 0;
785bc0f2195SMike Smith 	    goto out;
786bc0f2195SMike Smith 	}
787bc0f2195SMike Smith 	ids++;
788bc0f2195SMike Smith     }
789bc0f2195SMike Smith  out:
790bc0f2195SMike Smith     return_VALUE(result);
791bc0f2195SMike Smith }
792bc0f2195SMike Smith 
793bc0f2195SMike Smith /*
79415e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
79515e32d5dSMike Smith  *
7967d3bcec9SMike Smith  * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and \_SB_ scopes,
7977d3bcec9SMike Smith  * and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
79815e32d5dSMike Smith  */
79915e32d5dSMike Smith static void
80015e32d5dSMike Smith acpi_probe_children(device_t bus)
80115e32d5dSMike Smith {
80215e32d5dSMike Smith     ACPI_HANDLE		parent;
8037d3bcec9SMike Smith     static char		*scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
80415e32d5dSMike Smith     int			i;
80515e32d5dSMike Smith 
806b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
807cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
8080ae55423SMike Smith 
80915e32d5dSMike Smith     /*
81015e32d5dSMike Smith      * Create any static children by calling device identify methods.
81115e32d5dSMike Smith      */
8124c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
81315e32d5dSMike Smith     bus_generic_probe(bus);
81415e32d5dSMike Smith 
81515e32d5dSMike Smith     /*
81615e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
81715e32d5dSMike Smith      * we find.
81815e32d5dSMike Smith      *
81915e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
82015e32d5dSMike Smith      * we want to create nodes for all devices, not just those that are currently
82115e32d5dSMike Smith      * present. (This assumes that we don't want to create/remove devices as they
82215e32d5dSMike Smith      * appear, which might be smarter.)
82315e32d5dSMike Smith      */
8244c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
82515e32d5dSMike Smith     for (i = 0; scopes[i] != NULL; i++)
826b53f2771SMike Smith 	if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent)))
82715e32d5dSMike Smith 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL);
82815e32d5dSMike Smith 
82915e32d5dSMike Smith     /*
83015e32d5dSMike Smith      * Scan all of the child devices we have created and let them probe/attach.
83115e32d5dSMike Smith      */
8324c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
83315e32d5dSMike Smith     bus_generic_attach(bus);
83415e32d5dSMike Smith 
83515e32d5dSMike Smith     /*
83615e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
83715e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
83815e32d5dSMike Smith      */
8394c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
84015e32d5dSMike Smith     bus_generic_attach(bus);
8410ae55423SMike Smith 
842bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
8430ae55423SMike Smith     return_VOID;
84415e32d5dSMike Smith }
84515e32d5dSMike Smith 
84615e32d5dSMike Smith /*
84715e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
84815e32d5dSMike Smith  * it.
84915e32d5dSMike Smith  */
85015e32d5dSMike Smith static ACPI_STATUS
85115e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
85215e32d5dSMike Smith {
85315e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
85415e32d5dSMike Smith     device_t		child, bus = (device_t)context;
85515e32d5dSMike Smith 
856b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
8570ae55423SMike Smith 
8580ae55423SMike Smith     /*
8590ae55423SMike Smith      * Skip this device if we think we'll have trouble with it.
8600ae55423SMike Smith      */
8610ae55423SMike Smith     if (acpi_avoid(handle))
8620ae55423SMike Smith 	return_ACPI_STATUS(AE_OK);
8630ae55423SMike Smith 
864b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
86515e32d5dSMike Smith 	switch(type) {
86615e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
86715e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
86815e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
86915e32d5dSMike Smith 	case ACPI_TYPE_POWER:
8700ae55423SMike Smith 	    if (acpi_disabled("children"))
8710ae55423SMike Smith 		break;
87215e32d5dSMike Smith 	    /*
87315e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
87415e32d5dSMike Smith 	     * so that the probe/attach passes will run breadth-first.
87515e32d5dSMike Smith 	     */
8764c1cdee6SMike Smith 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", acpi_name(handle)));
87715e32d5dSMike Smith 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
878bc0f2195SMike Smith 	    if (child == NULL)
879bc0f2195SMike Smith 		break;
88015e32d5dSMike Smith 	    acpi_set_handle(child, handle);
881bc0f2195SMike Smith 
882bc0f2195SMike Smith 	    /*
883b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
884b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
885b519f9d6SMike Smith 	     * the handle, but we don't probe it).
886b519f9d6SMike Smith 	     */
88723b4e251SMitsuru IWASAKI 	    if ((type == ACPI_TYPE_DEVICE) && (!acpi_DeviceIsPresent(child))) {
888b519f9d6SMike Smith 		device_disable(child);
889b519f9d6SMike Smith 		break;
890b519f9d6SMike Smith 	    }
891b519f9d6SMike Smith 
892b519f9d6SMike Smith 	    /*
893bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
894bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
895bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
896bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
897bc0f2195SMike Smith 	     * device not to have any resources.
898bc0f2195SMike Smith 	     */
899bc0f2195SMike Smith 	    acpi_parse_resources(child, handle, &acpi_res_parse_set);
900bc0f2195SMike Smith 
901bc0f2195SMike Smith 	    /* if we're debugging, probe/attach now rather than later */
902b53f2771SMike Smith 	    ACPI_DEBUG_EXEC(device_probe_and_attach(child));
9030ae55423SMike Smith 	    break;
90415e32d5dSMike Smith 	}
90515e32d5dSMike Smith     }
9060ae55423SMike Smith     return_ACPI_STATUS(AE_OK);
90715e32d5dSMike Smith }
90815e32d5dSMike Smith 
90915e32d5dSMike Smith static void
91015e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto)
91115e32d5dSMike Smith {
912cb9b0d80SMike Smith 
913c6a78e98STakanori Watanabe     struct acpi_softc *sc = arg;
914c6a78e98STakanori Watanabe 
915cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
916cb9b0d80SMike Smith 
91715e32d5dSMike Smith     /*
9180ae55423SMike Smith      * Disable all ACPI events before soft off, otherwise the system
91915e32d5dSMike Smith      * will be turned on again on some laptops.
92015e32d5dSMike Smith      *
92115e32d5dSMike Smith      * XXX this should probably be restricted to masking some events just
92215e32d5dSMike Smith      *     before powering down, since we may still need ACPI during the
92315e32d5dSMike Smith      *     shutdown process.
92415e32d5dSMike Smith      */
925c6a78e98STakanori Watanabe     if (sc->acpi_disable_on_poweroff)
926c6a78e98STakanori Watanabe 	acpi_Disable(sc);
92715e32d5dSMike Smith }
92815e32d5dSMike Smith 
92915e32d5dSMike Smith static void
93015e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
93115e32d5dSMike Smith {
93215e32d5dSMike Smith     ACPI_STATUS	status;
93315e32d5dSMike Smith 
934cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
935cb9b0d80SMike Smith 
9365f3e4175SMitsuru IWASAKI     if (howto & RB_POWEROFF) {
937e1a90ae1SNate Lawson 	printf("Powering system off using ACPI\n");
938b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(acpi_off_state))) {
939b9c780d6SMitsuru IWASAKI 	    printf("AcpiEnterSleepStatePrep failed - %s\n",
940b9c780d6SMitsuru IWASAKI 		   AcpiFormatException(status));
941b9c780d6SMitsuru IWASAKI 	    return;
942b9c780d6SMitsuru IWASAKI 	}
943b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepState(acpi_off_state))) {
944bfae45aaSMike Smith 	    printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
94515e32d5dSMike Smith 	} else {
94615e32d5dSMike Smith 	    DELAY(1000000);
94715e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
94815e32d5dSMike Smith 	}
949494ae560SMitsuru IWASAKI     } else {
950e1a90ae1SNate Lawson 	printf("Shutting down ACPI\n");
951494ae560SMitsuru IWASAKI 	AcpiTerminate();
95215e32d5dSMike Smith     }
95315e32d5dSMike Smith }
95415e32d5dSMike Smith 
95513d4f7baSMitsuru IWASAKI static void
95613d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
95713d4f7baSMitsuru IWASAKI {
95813d4f7baSMitsuru IWASAKI     static int	first_time = 1;
95913d4f7baSMitsuru IWASAKI #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n"
96013d4f7baSMitsuru IWASAKI 
961cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
962cb9b0d80SMike Smith 
96313d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
964cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) {
96513d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
96613d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_power_button_for_sleep, sc);
96713d4f7baSMitsuru IWASAKI 	if (first_time) {
96813d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "power");
96913d4f7baSMitsuru IWASAKI 	}
97013d4f7baSMitsuru IWASAKI     }
971cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) {
97213d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
97313d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_sleep_button_for_sleep, sc);
97413d4f7baSMitsuru IWASAKI 	if (first_time) {
97513d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "sleep");
97613d4f7baSMitsuru IWASAKI 	}
97713d4f7baSMitsuru IWASAKI     }
97813d4f7baSMitsuru IWASAKI 
97913d4f7baSMitsuru IWASAKI     first_time = 0;
98013d4f7baSMitsuru IWASAKI }
98113d4f7baSMitsuru IWASAKI 
98215e32d5dSMike Smith /*
983c5ba0be4SMike Smith  * Returns true if the device is actually present and should
984c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
985c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
986c5ba0be4SMike Smith  */
987c5ba0be4SMike Smith BOOLEAN
988c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
989c5ba0be4SMike Smith {
990c5ba0be4SMike Smith     ACPI_HANDLE		h;
991c5ba0be4SMike Smith     ACPI_DEVICE_INFO	devinfo;
9926fca9360SNate Lawson     ACPI_BUFFER		buf = {sizeof(devinfo), &devinfo};
993c5ba0be4SMike Smith     ACPI_STATUS		error;
994c5ba0be4SMike Smith 
995cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
996cb9b0d80SMike Smith 
997c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
998c5ba0be4SMike Smith 	return(FALSE);
9996fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
10006fca9360SNate Lawson     if (ACPI_FAILURE(error))
1001c5ba0be4SMike Smith 	return(FALSE);
100243896e91SMike Smith     /* if no _STA method, must be present */
10036fca9360SNate Lawson     if ((devinfo.Valid & ACPI_VALID_STA) == 0)
100443896e91SMike Smith 	return(TRUE);
100543896e91SMike Smith     /* return true for 'present' and 'functioning' */
100643896e91SMike Smith     if ((devinfo.CurrentStatus & 0x9) == 0x9)
1007c5ba0be4SMike Smith 	return(TRUE);
1008c5ba0be4SMike Smith     return(FALSE);
1009c5ba0be4SMike Smith }
1010c5ba0be4SMike Smith 
1011c5ba0be4SMike Smith /*
1012b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
1013b53f2771SMike Smith  */
1014b53f2771SMike Smith BOOLEAN
1015b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
1016b53f2771SMike Smith {
1017b53f2771SMike Smith     ACPI_HANDLE		h;
1018b53f2771SMike Smith     ACPI_DEVICE_INFO	devinfo;
10196fca9360SNate Lawson     ACPI_BUFFER		buf = {sizeof(devinfo), &devinfo};
1020b53f2771SMike Smith     ACPI_STATUS		error;
1021b53f2771SMike Smith 
1022b53f2771SMike Smith     ACPI_ASSERTLOCK;
1023b53f2771SMike Smith 
1024b53f2771SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1025b53f2771SMike Smith 	return(FALSE);
10266fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
10276fca9360SNate Lawson     if (ACPI_FAILURE(error))
1028b53f2771SMike Smith 	return(FALSE);
1029b53f2771SMike Smith     /* if no _STA method, must be present */
10306fca9360SNate Lawson     if ((devinfo.Valid & ACPI_VALID_STA) == 0)
1031b53f2771SMike Smith 	return(TRUE);
1032b53f2771SMike Smith     /* return true for 'present' and 'functioning' */
1033b53f2771SMike Smith     if ((devinfo.CurrentStatus & 0x19) == 0x19)
1034b53f2771SMike Smith 	return(TRUE);
1035b53f2771SMike Smith     return(FALSE);
1036b53f2771SMike Smith }
1037b53f2771SMike Smith 
1038b53f2771SMike Smith /*
103915e32d5dSMike Smith  * Match a HID string against a device
104015e32d5dSMike Smith  */
104115e32d5dSMike Smith BOOLEAN
104215e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid)
104315e32d5dSMike Smith {
104415e32d5dSMike Smith     ACPI_HANDLE		h;
104515e32d5dSMike Smith     ACPI_DEVICE_INFO	devinfo;
10466fca9360SNate Lawson     ACPI_BUFFER		buf = {sizeof(devinfo), &devinfo};
104715e32d5dSMike Smith     ACPI_STATUS		error;
1048ed136da6SDoug Rabson     int			cid;
104915e32d5dSMike Smith 
1050cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1051cb9b0d80SMike Smith 
10524d332989SMike Smith     if (hid == NULL)
105315e32d5dSMike Smith 	return(FALSE);
105415e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
105515e32d5dSMike Smith 	return(FALSE);
10566fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
10576fca9360SNate Lawson     if (ACPI_FAILURE(error))
105815e32d5dSMike Smith 	return(FALSE);
10596fca9360SNate Lawson     if ((devinfo.Valid & ACPI_VALID_HID) != 0 &&
10606fca9360SNate Lawson 	strcmp(hid, devinfo.HardwareId.Value) == 0)
106115e32d5dSMike Smith 	return(TRUE);
1062b53f2771SMike Smith     if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &cid)))
1063ed136da6SDoug Rabson 	return(FALSE);
1064ed136da6SDoug Rabson     if (cid == PNP_EISAID(hid))
1065ed136da6SDoug Rabson 	return(TRUE);
106615e32d5dSMike Smith     return(FALSE);
106715e32d5dSMike Smith }
106815e32d5dSMike Smith 
106915e32d5dSMike Smith /*
1070c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1071c5ba0be4SMike Smith  * or one if its parents.
1072c5ba0be4SMike Smith  */
1073c5ba0be4SMike Smith ACPI_STATUS
1074c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1075c5ba0be4SMike Smith {
1076c5ba0be4SMike Smith     ACPI_HANDLE		r;
1077c5ba0be4SMike Smith     ACPI_STATUS		status;
1078c5ba0be4SMike Smith 
1079cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1080cb9b0d80SMike Smith 
1081c5ba0be4SMike Smith     /* walk back up the tree to the root */
1082c5ba0be4SMike Smith     for (;;) {
1083b53f2771SMike Smith 	if (ACPI_SUCCESS(status = AcpiGetHandle(parent, path, &r))) {
1084c5ba0be4SMike Smith 	    *result = r;
1085c5ba0be4SMike Smith 	    return(AE_OK);
1086c5ba0be4SMike Smith 	}
1087c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1088c5ba0be4SMike Smith 	    return(AE_OK);
1089b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1090c5ba0be4SMike Smith 	    return(AE_NOT_FOUND);
1091c5ba0be4SMike Smith 	parent = r;
1092c5ba0be4SMike Smith     }
1093c5ba0be4SMike Smith }
1094c5ba0be4SMike Smith 
1095c5ba0be4SMike Smith /*
1096c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1097c5ba0be4SMike Smith  */
1098c5ba0be4SMike Smith ACPI_BUFFER *
1099c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1100c5ba0be4SMike Smith {
1101c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1102c5ba0be4SMike Smith 
1103c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1104c5ba0be4SMike Smith 	return(NULL);
1105c5ba0be4SMike Smith     buf->Length = size;
1106c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1107c5ba0be4SMike Smith     return(buf);
1108c5ba0be4SMike Smith }
1109c5ba0be4SMike Smith 
1110c5ba0be4SMike Smith /*
1111c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1112c5ba0be4SMike Smith  */
1113c5ba0be4SMike Smith ACPI_STATUS
1114c5ba0be4SMike Smith acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number)
1115c5ba0be4SMike Smith {
1116c5ba0be4SMike Smith     ACPI_STATUS	error;
1117c5ba0be4SMike Smith     ACPI_BUFFER	buf;
1118c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
1119c5ba0be4SMike Smith 
1120cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1121cb9b0d80SMike Smith 
1122c5ba0be4SMike Smith     if (handle == NULL)
1123c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
11240c7da7acSJohn Baldwin 
11250c7da7acSJohn Baldwin     /*
11260c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
11270c7da7acSJohn Baldwin      * a method that will return an Integer.
11280c7da7acSJohn Baldwin      */
1129c5ba0be4SMike Smith     buf.Pointer = &param;
1130c5ba0be4SMike Smith     buf.Length = sizeof(param);
1131b53f2771SMike Smith     if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
1132c5ba0be4SMike Smith 	if (param.Type == ACPI_TYPE_INTEGER) {
1133c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1134c5ba0be4SMike Smith 	} else {
1135c5ba0be4SMike Smith 	    error = AE_TYPE;
1136c5ba0be4SMike Smith 	}
1137c5ba0be4SMike Smith     }
11380c7da7acSJohn Baldwin 
11390c7da7acSJohn Baldwin     /*
11400c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
11410c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
11420c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
11430c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
11440c7da7acSJohn Baldwin      *
11450c7da7acSJohn Baldwin      * This is a hack.
11460c7da7acSJohn Baldwin      */
11470c7da7acSJohn Baldwin     if (error == AE_BUFFER_OVERFLOW) {
1148b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
11490c7da7acSJohn Baldwin 	    error = AE_NO_MEMORY;
11500c7da7acSJohn Baldwin 	} else {
1151b53f2771SMike Smith 	    if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
1152c573e654SMitsuru IWASAKI 		error = acpi_ConvertBufferToInteger(&buf, number);
11530c7da7acSJohn Baldwin 	    }
11540c7da7acSJohn Baldwin 	}
11550c7da7acSJohn Baldwin 	AcpiOsFree(buf.Pointer);
11560c7da7acSJohn Baldwin     }
1157c5ba0be4SMike Smith     return(error);
1158c5ba0be4SMike Smith }
1159c5ba0be4SMike Smith 
1160c573e654SMitsuru IWASAKI ACPI_STATUS
1161c573e654SMitsuru IWASAKI acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number)
1162c573e654SMitsuru IWASAKI {
1163c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
1164c573e654SMitsuru IWASAKI     int		i;
1165c573e654SMitsuru IWASAKI 
1166c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
1167c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
1168c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
1169c573e654SMitsuru IWASAKI 	return(AE_OK);
1170c573e654SMitsuru IWASAKI     }
1171c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
1172c573e654SMitsuru IWASAKI 	return(AE_TYPE);
1173c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
1174c573e654SMitsuru IWASAKI 	return(AE_BAD_DATA);
1175c573e654SMitsuru IWASAKI     *number = 0;
1176c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
1177c573e654SMitsuru IWASAKI 	*number += (*(p->Buffer.Pointer + i) << (i * 8));
1178c573e654SMitsuru IWASAKI     return(AE_OK);
1179c573e654SMitsuru IWASAKI }
1180c573e654SMitsuru IWASAKI 
1181c5ba0be4SMike Smith /*
1182c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1183c5ba0be4SMike Smith  * function for each element.
1184c5ba0be4SMike Smith  *
1185c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1186c5ba0be4SMike Smith  */
1187c5ba0be4SMike Smith ACPI_STATUS
1188c5ba0be4SMike Smith acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg)
1189c5ba0be4SMike Smith {
1190c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1191c5ba0be4SMike Smith     int		i;
1192c5ba0be4SMike Smith 
1193c5ba0be4SMike Smith     if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE))
1194c5ba0be4SMike Smith 	return(AE_BAD_PARAMETER);
1195c5ba0be4SMike Smith 
1196c5ba0be4SMike Smith     /* iterate over components */
1197c5ba0be4SMike Smith     for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++)
1198c5ba0be4SMike Smith 	func(comp, arg);
1199c5ba0be4SMike Smith 
1200c5ba0be4SMike Smith     return(AE_OK);
1201c5ba0be4SMike Smith }
1202c5ba0be4SMike Smith 
12036f69255bSMike Smith /*
12046f69255bSMike Smith  * Find the (index)th resource object in a set.
12056f69255bSMike Smith  */
12066f69255bSMike Smith ACPI_STATUS
12076d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
12086f69255bSMike Smith {
12096d63101aSMike Smith     ACPI_RESOURCE	*rp;
12106f69255bSMike Smith     int			i;
12116f69255bSMike Smith 
12126d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
12136f69255bSMike Smith     i = index;
12146d63101aSMike Smith     while (i-- > 0) {
12156f69255bSMike Smith 	/* range check */
12166d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
12176f69255bSMike Smith 	    return(AE_BAD_PARAMETER);
12186d63101aSMike Smith 	/* check for terminator */
12196d63101aSMike Smith 	if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
12206d63101aSMike Smith 	    (rp->Length == 0))
12216d63101aSMike Smith 	    return(AE_NOT_FOUND);
12226d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
12236f69255bSMike Smith     }
12246f69255bSMike Smith     if (resp != NULL)
12256d63101aSMike Smith 	*resp = rp;
12266d63101aSMike Smith     return(AE_OK);
12276d63101aSMike Smith }
12286d63101aSMike Smith 
12296d63101aSMike Smith /*
12306d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
12316d63101aSMike Smith  *
12326d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
12336d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
12346d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
12356d63101aSMike Smith  * resources.
12366d63101aSMike Smith  */
12376d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
12386d63101aSMike Smith 
12396d63101aSMike Smith ACPI_STATUS
12406d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
12416d63101aSMike Smith {
12426d63101aSMike Smith     ACPI_RESOURCE	*rp;
12436d63101aSMike Smith     void		*newp;
12446d63101aSMike Smith 
12456d63101aSMike Smith     /*
12466d63101aSMike Smith      * Initialise the buffer if necessary.
12476d63101aSMike Smith      */
12486d63101aSMike Smith     if (buf->Pointer == NULL) {
12496d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
12506d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
12516d63101aSMike Smith 	    return(AE_NO_MEMORY);
12526d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
12536d63101aSMike Smith 	rp->Id = ACPI_RSTYPE_END_TAG;
12546d63101aSMike Smith 	rp->Length = 0;
12556d63101aSMike Smith     }
12566d63101aSMike Smith     if (res == NULL)
12576d63101aSMike Smith 	return(AE_OK);
12586d63101aSMike Smith 
12596d63101aSMike Smith     /*
12606d63101aSMike Smith      * Scan the current buffer looking for the terminator.
12616d63101aSMike Smith      * This will either find the terminator or hit the end
12626d63101aSMike Smith      * of the buffer and return an error.
12636d63101aSMike Smith      */
12646d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
12656d63101aSMike Smith     for (;;) {
12666d63101aSMike Smith 	/* range check, don't go outside the buffer */
12676d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
12686d63101aSMike Smith 	    return(AE_BAD_PARAMETER);
12696d63101aSMike Smith 	if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
12706d63101aSMike Smith 	    (rp->Length == 0)) {
12716d63101aSMike Smith 	    break;
12726d63101aSMike Smith 	}
12736d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
12746d63101aSMike Smith     }
12756d63101aSMike Smith 
12766d63101aSMike Smith     /*
12776d63101aSMike Smith      * Check the size of the buffer and expand if required.
12786d63101aSMike Smith      *
12796d63101aSMike Smith      * Required size is:
12806d63101aSMike Smith      *	size of existing resources before terminator +
12816d63101aSMike Smith      *	size of new resource and header +
12826d63101aSMike Smith      * 	size of terminator.
12836d63101aSMike Smith      *
12846d63101aSMike Smith      * Note that this loop should really only run once, unless
12856d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
12866d63101aSMike Smith      */
12876d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
12886d63101aSMike Smith 	    res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
12896d63101aSMike Smith 	    ACPI_RESOURCE_LENGTH) >= buf->Length) {
12906d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
12916d63101aSMike Smith 	    return(AE_NO_MEMORY);
12926d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
1293a692219dSMike Smith         rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1294a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
12956d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
12966d63101aSMike Smith 	buf->Pointer = newp;
12976d63101aSMike Smith 	buf->Length += buf->Length;
12986d63101aSMike Smith     }
12996d63101aSMike Smith 
13006d63101aSMike Smith     /*
13016d63101aSMike Smith      * Insert the new resource.
13026d63101aSMike Smith      */
13036d63101aSMike Smith     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
13046d63101aSMike Smith 
13056d63101aSMike Smith     /*
13066d63101aSMike Smith      * And add the terminator.
13076d63101aSMike Smith      */
13086d63101aSMike Smith     rp = ACPI_RESOURCE_NEXT(rp);
13096d63101aSMike Smith     rp->Id = ACPI_RSTYPE_END_TAG;
13106d63101aSMike Smith     rp->Length = 0;
13116d63101aSMike Smith 
13126f69255bSMike Smith     return(AE_OK);
13136f69255bSMike Smith }
1314c5ba0be4SMike Smith 
1315da14ac9fSJohn Baldwin /*
1316da14ac9fSJohn Baldwin  * Set interrupt model.
1317da14ac9fSJohn Baldwin  */
1318da14ac9fSJohn Baldwin ACPI_STATUS
1319da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
1320da14ac9fSJohn Baldwin {
1321da14ac9fSJohn Baldwin 	ACPI_OBJECT_LIST ArgList;
1322da14ac9fSJohn Baldwin 	ACPI_OBJECT Arg;
1323da14ac9fSJohn Baldwin 
1324da14ac9fSJohn Baldwin 	Arg.Type = ACPI_TYPE_INTEGER;
1325da14ac9fSJohn Baldwin 	Arg.Integer.Value = model;
1326da14ac9fSJohn Baldwin 	ArgList.Count = 1;
1327da14ac9fSJohn Baldwin 	ArgList.Pointer = &Arg;
1328da14ac9fSJohn Baldwin 	return (AcpiEvaluateObject(ACPI_ROOT_OBJECT, "_PIC", &ArgList, NULL));
1329da14ac9fSJohn Baldwin }
1330da14ac9fSJohn Baldwin 
1331ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME	5
1332ece50487SMitsuru IWASAKI 
1333ece50487SMitsuru IWASAKI static void
1334ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
1335ece50487SMitsuru IWASAKI {
1336ece50487SMitsuru IWASAKI     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1337ece50487SMitsuru IWASAKI }
1338c30382dfSMitsuru IWASAKI 
133915e32d5dSMike Smith /*
134015e32d5dSMike Smith  * Set the system sleep state
134115e32d5dSMike Smith  *
134215e32d5dSMike Smith  * Currently we only support S1 and S5
134315e32d5dSMike Smith  */
134415e32d5dSMike Smith ACPI_STATUS
134515e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
134615e32d5dSMike Smith {
134715e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
134856d8cb57SMitsuru IWASAKI     UINT8	TypeA;
134956d8cb57SMitsuru IWASAKI     UINT8	TypeB;
135015e32d5dSMike Smith 
1351b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1352cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
13530ae55423SMike Smith 
13546397624dSMitsuru IWASAKI     if (sc->acpi_sstate != ACPI_STATE_S0)
13556397624dSMitsuru IWASAKI 	return_ACPI_STATUS(AE_BAD_PARAMETER);	/* avoid reentry */
13566397624dSMitsuru IWASAKI 
1357ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1358ece50487SMitsuru IWASAKI 	return_ACPI_STATUS(AE_OK);
1359ece50487SMitsuru IWASAKI 
136015e32d5dSMike Smith     switch (state) {
136115e32d5dSMike Smith     case ACPI_STATE_S0:	/* XXX only for testing */
1362b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
1363bfae45aaSMike Smith 	    device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
136415e32d5dSMike Smith 	}
136515e32d5dSMike Smith 	break;
136615e32d5dSMike Smith 
136715e32d5dSMike Smith     case ACPI_STATE_S1:
13686161544cSTakanori Watanabe     case ACPI_STATE_S2:
13696161544cSTakanori Watanabe     case ACPI_STATE_S3:
13706161544cSTakanori Watanabe     case ACPI_STATE_S4:
137198479b04SMitsuru IWASAKI 	if (ACPI_FAILURE(status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB))) {
137298479b04SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", AcpiFormatException(status));
137356d8cb57SMitsuru IWASAKI 	    break;
137456d8cb57SMitsuru IWASAKI 	}
137556d8cb57SMitsuru IWASAKI 
1376a1fccb47SMitsuru IWASAKI 	sc->acpi_sstate = state;
1377a1fccb47SMitsuru IWASAKI 	sc->acpi_sleep_disabled = 1;
1378a1fccb47SMitsuru IWASAKI 
137915e32d5dSMike Smith 	/*
138015e32d5dSMike Smith 	 * Inform all devices that we are going to sleep.
138115e32d5dSMike Smith 	 */
138215e32d5dSMike Smith 	if (DEVICE_SUSPEND(root_bus) != 0) {
138315e32d5dSMike Smith 	    /*
138415e32d5dSMike Smith 	     * Re-wake the system.
138515e32d5dSMike Smith 	     *
138615e32d5dSMike Smith 	     * XXX note that a better two-pass approach with a 'veto' pass
138715e32d5dSMike Smith 	     *     followed by a "real thing" pass would be better, but the
138815e32d5dSMike Smith 	     *     current bus interface does not provide for this.
138915e32d5dSMike Smith 	     */
139015e32d5dSMike Smith 	    DEVICE_RESUME(root_bus);
13910ae55423SMike Smith 	    return_ACPI_STATUS(AE_ERROR);
139215e32d5dSMike Smith 	}
1393b9c780d6SMitsuru IWASAKI 
1394b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(state))) {
1395b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1396b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
1397b9c780d6SMitsuru IWASAKI 	    break;
1398b9c780d6SMitsuru IWASAKI 	}
1399b9c780d6SMitsuru IWASAKI 
1400ff01efb5SMitsuru IWASAKI 	if (sc->acpi_sleep_delay > 0) {
1401ff01efb5SMitsuru IWASAKI 	    DELAY(sc->acpi_sleep_delay * 1000000);
1402ff01efb5SMitsuru IWASAKI 	}
1403ff01efb5SMitsuru IWASAKI 
14046161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
14056161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
14066161544cSTakanori Watanabe 
1407a1fccb47SMitsuru IWASAKI 	    /* AcpiEnterSleepState() maybe incompleted, unlock here if locked. */
14086fca9360SNate Lawson 	    if (1/*AcpiGbl_AcpiMutexInfo[ACPI_MTX_HARDWARE].OwnerId != ACPI_MUTEX_NOT_ACQUIRED*/) {
14096161544cSTakanori Watanabe 		AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
1410a1fccb47SMitsuru IWASAKI 	    }
14116161544cSTakanori Watanabe 
14126161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1413964679ceSMitsuru IWASAKI 	    if (state == ACPI_STATE_S4) {
1414964679ceSMitsuru IWASAKI 		AcpiEnable();
14156161544cSTakanori Watanabe 	    }
14166161544cSTakanori Watanabe 	} else {
1417b53f2771SMike Smith 	    if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
1418bfae45aaSMike Smith 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
1419c30382dfSMitsuru IWASAKI 		break;
142015e32d5dSMike Smith 	    }
14216161544cSTakanori Watanabe 	}
1422ff741befSTakanori Watanabe 	AcpiLeaveSleepState((UINT8)state);
142315e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
142415e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
142513d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
142615e32d5dSMike Smith 	break;
142715e32d5dSMike Smith 
142815e32d5dSMike Smith     case ACPI_STATE_S5:
142915e32d5dSMike Smith 	/*
143015e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
143115e32d5dSMike Smith 	 * shutdown handlers.
143215e32d5dSMike Smith 	 */
143315e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
143415e32d5dSMike Smith 	break;
143515e32d5dSMike Smith 
143615e32d5dSMike Smith     default:
143715e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
143815e32d5dSMike Smith 	break;
143915e32d5dSMike Smith     }
1440ece50487SMitsuru IWASAKI 
1441ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1442ece50487SMitsuru IWASAKI 	timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1443ece50487SMitsuru IWASAKI 
14440ae55423SMike Smith     return_ACPI_STATUS(status);
144515e32d5dSMike Smith }
144615e32d5dSMike Smith 
144715e32d5dSMike Smith /*
144815e32d5dSMike Smith  * Enable/Disable ACPI
144915e32d5dSMike Smith  */
145015e32d5dSMike Smith ACPI_STATUS
145115e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
145215e32d5dSMike Smith {
145315e32d5dSMike Smith     ACPI_STATUS	status;
145415e32d5dSMike Smith     u_int32_t	flags;
145515e32d5dSMike Smith 
1456b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1457cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
14580ae55423SMike Smith 
145915e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
146015e32d5dSMike Smith             ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
146115e32d5dSMike Smith     if (!sc->acpi_enabled) {
146215e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
146315e32d5dSMike Smith     } else {
146415e32d5dSMike Smith 	status = AE_OK;
146515e32d5dSMike Smith     }
146615e32d5dSMike Smith     if (status == AE_OK)
146715e32d5dSMike Smith 	sc->acpi_enabled = 1;
14680ae55423SMike Smith     return_ACPI_STATUS(status);
146915e32d5dSMike Smith }
147015e32d5dSMike Smith 
147115e32d5dSMike Smith ACPI_STATUS
147215e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
147315e32d5dSMike Smith {
147415e32d5dSMike Smith     ACPI_STATUS	status;
147515e32d5dSMike Smith 
1476b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1477cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
14780ae55423SMike Smith 
147915e32d5dSMike Smith     if (sc->acpi_enabled) {
148015e32d5dSMike Smith 	status = AcpiDisable();
148115e32d5dSMike Smith     } else {
148215e32d5dSMike Smith 	status = AE_OK;
148315e32d5dSMike Smith     }
148415e32d5dSMike Smith     if (status == AE_OK)
148515e32d5dSMike Smith 	sc->acpi_enabled = 0;
14860ae55423SMike Smith     return_ACPI_STATUS(status);
148715e32d5dSMike Smith }
148815e32d5dSMike Smith 
148915e32d5dSMike Smith /*
149015e32d5dSMike Smith  * ACPI Event Handlers
149115e32d5dSMike Smith  */
149215e32d5dSMike Smith 
149315e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
149415e32d5dSMike Smith 
149515e32d5dSMike Smith static void
149615e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
149715e32d5dSMike Smith {
1498fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1499b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
150015e32d5dSMike Smith 
1501cb9b0d80SMike Smith     ACPI_LOCK;
1502a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
150315e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
1504cb9b0d80SMike Smith     ACPI_UNLOCK;
15050ae55423SMike Smith     return_VOID;
150615e32d5dSMike Smith }
150715e32d5dSMike Smith 
150815e32d5dSMike Smith static void
150915e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
151015e32d5dSMike Smith {
1511fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1512b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
15130ae55423SMike Smith 
151415e32d5dSMike Smith     /* Well, what to do? :-) */
15150ae55423SMike Smith 
1516cb9b0d80SMike Smith     ACPI_LOCK;
1517cb9b0d80SMike Smith     ACPI_UNLOCK;
1518cb9b0d80SMike Smith 
15190ae55423SMike Smith     return_VOID;
152015e32d5dSMike Smith }
152115e32d5dSMike Smith 
152215e32d5dSMike Smith /*
152315e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
152415e32d5dSMike Smith  */
152515e32d5dSMike Smith UINT32
152615e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context)
152715e32d5dSMike Smith {
152815e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
152915e32d5dSMike Smith 
1530b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15310ae55423SMike Smith 
153215e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
15330ae55423SMike Smith 
1534b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
153515e32d5dSMike Smith }
153615e32d5dSMike Smith 
153715e32d5dSMike Smith UINT32
153815e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context)
153915e32d5dSMike Smith {
154015e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
154115e32d5dSMike Smith 
1542b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15430ae55423SMike Smith 
154415e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
15450ae55423SMike Smith 
1546b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
154715e32d5dSMike Smith }
154815e32d5dSMike Smith 
154915e32d5dSMike Smith UINT32
155015e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context)
155115e32d5dSMike Smith {
155215e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
155315e32d5dSMike Smith 
1554b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15550ae55423SMike Smith 
155615e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
15570ae55423SMike Smith 
1558b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
155915e32d5dSMike Smith }
156015e32d5dSMike Smith 
156115e32d5dSMike Smith UINT32
156215e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context)
156315e32d5dSMike Smith {
156415e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
156515e32d5dSMike Smith 
1566b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15670ae55423SMike Smith 
156815e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
15690ae55423SMike Smith 
1570b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
157115e32d5dSMike Smith }
157215e32d5dSMike Smith 
157315e32d5dSMike Smith /*
157415e32d5dSMike Smith  * XXX This is kinda ugly, and should not be here.
157515e32d5dSMike Smith  */
157615e32d5dSMike Smith struct acpi_staticbuf {
157715e32d5dSMike Smith     ACPI_BUFFER	buffer;
157815e32d5dSMike Smith     char	data[512];
157915e32d5dSMike Smith };
158015e32d5dSMike Smith 
158115e32d5dSMike Smith char *
158215e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
158315e32d5dSMike Smith {
158415e32d5dSMike Smith     static struct acpi_staticbuf	buf;
158515e32d5dSMike Smith 
1586cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1587cb9b0d80SMike Smith 
158815e32d5dSMike Smith     buf.buffer.Length = 512;
158915e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
159015e32d5dSMike Smith 
1591b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
159215e32d5dSMike Smith 	return(buf.buffer.Pointer);
159315e32d5dSMike Smith     return("(unknown path)");
159415e32d5dSMike Smith }
159515e32d5dSMike Smith 
159615e32d5dSMike Smith /*
159715e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
159815e32d5dSMike Smith  * parts of the namespace.
159915e32d5dSMike Smith  */
160015e32d5dSMike Smith int
160115e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
160215e32d5dSMike Smith {
16033c600cfbSJohn Baldwin     char	*cp, *env, *np;
160415e32d5dSMike Smith     int		len;
160515e32d5dSMike Smith 
160615e32d5dSMike Smith     np = acpi_name(handle);
160715e32d5dSMike Smith     if (*np == '\\')
160815e32d5dSMike Smith 	np++;
16093c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
161015e32d5dSMike Smith 	return(0);
161115e32d5dSMike Smith 
161215e32d5dSMike Smith     /* scan the avoid list checking for a match */
16133c600cfbSJohn Baldwin     cp = env;
161415e32d5dSMike Smith     for (;;) {
161515e32d5dSMike Smith 	while ((*cp != 0) && isspace(*cp))
161615e32d5dSMike Smith 	    cp++;
161715e32d5dSMike Smith 	if (*cp == 0)
161815e32d5dSMike Smith 	    break;
161915e32d5dSMike Smith 	len = 0;
162015e32d5dSMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
162115e32d5dSMike Smith 	    len++;
1622d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
16233c600cfbSJohn Baldwin 	    freeenv(env);
16240ae55423SMike Smith 	    return(1);
1625d786139cSMaxime Henrion 	}
16260ae55423SMike Smith 	cp += len;
16270ae55423SMike Smith     }
16283c600cfbSJohn Baldwin     freeenv(env);
16290ae55423SMike Smith     return(0);
16300ae55423SMike Smith }
16310ae55423SMike Smith 
16320ae55423SMike Smith /*
16330ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
16340ae55423SMike Smith  */
16350ae55423SMike Smith int
16360ae55423SMike Smith acpi_disabled(char *subsys)
16370ae55423SMike Smith {
163878689b15SMaxime Henrion     char	*cp, *env;
16390ae55423SMike Smith     int		len;
16400ae55423SMike Smith 
164178689b15SMaxime Henrion     if ((env = getenv("debug.acpi.disable")) == NULL)
16420ae55423SMike Smith 	return(0);
164378689b15SMaxime Henrion     if (!strcmp(env, "all")) {
164478689b15SMaxime Henrion 	freeenv(env);
16450ae55423SMike Smith 	return(1);
1646d786139cSMaxime Henrion     }
16470ae55423SMike Smith 
16480ae55423SMike Smith     /* scan the disable list checking for a match */
164978689b15SMaxime Henrion     cp = env;
16500ae55423SMike Smith     for (;;) {
16510ae55423SMike Smith 	while ((*cp != 0) && isspace(*cp))
16520ae55423SMike Smith 	    cp++;
16530ae55423SMike Smith 	if (*cp == 0)
16540ae55423SMike Smith 	    break;
16550ae55423SMike Smith 	len = 0;
16560ae55423SMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
16570ae55423SMike Smith 	    len++;
1658d786139cSMaxime Henrion 	if (!strncmp(cp, subsys, len)) {
165978689b15SMaxime Henrion 	    freeenv(env);
166015e32d5dSMike Smith 	    return(1);
1661d786139cSMaxime Henrion 	}
166215e32d5dSMike Smith 	cp += len;
166315e32d5dSMike Smith     }
166478689b15SMaxime Henrion     freeenv(env);
166515e32d5dSMike Smith     return(0);
166615e32d5dSMike Smith }
166715e32d5dSMike Smith 
166815e32d5dSMike Smith /*
1669a1fccb47SMitsuru IWASAKI  * Device wake capability enable/disable.
1670a1fccb47SMitsuru IWASAKI  */
1671a1fccb47SMitsuru IWASAKI void
1672a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable)
1673a1fccb47SMitsuru IWASAKI {
1674a1fccb47SMitsuru IWASAKI     ACPI_OBJECT_LIST		ArgList;
1675a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			Arg;
1676a1fccb47SMitsuru IWASAKI 
1677a1fccb47SMitsuru IWASAKI     /*
1678a1fccb47SMitsuru IWASAKI      * TBD: All Power Resources referenced by elements 2 through N
1679a1fccb47SMitsuru IWASAKI      *      of the _PRW object are put into the ON state.
1680a1fccb47SMitsuru IWASAKI      */
1681a1fccb47SMitsuru IWASAKI 
1682a1fccb47SMitsuru IWASAKI     /*
1683a1fccb47SMitsuru IWASAKI      * enable/disable device wake function.
1684a1fccb47SMitsuru IWASAKI      */
1685a1fccb47SMitsuru IWASAKI 
1686a1fccb47SMitsuru IWASAKI     ArgList.Count = 1;
1687a1fccb47SMitsuru IWASAKI     ArgList.Pointer = &Arg;
1688a1fccb47SMitsuru IWASAKI 
1689a1fccb47SMitsuru IWASAKI     Arg.Type = ACPI_TYPE_INTEGER;
1690a1fccb47SMitsuru IWASAKI     Arg.Integer.Value = enable;
1691a1fccb47SMitsuru IWASAKI 
1692a1fccb47SMitsuru IWASAKI     (void)AcpiEvaluateObject(h, "_PSW", &ArgList, NULL);
1693a1fccb47SMitsuru IWASAKI }
1694a1fccb47SMitsuru IWASAKI 
1695a1fccb47SMitsuru IWASAKI void
1696a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_event(ACPI_HANDLE h)
1697a1fccb47SMitsuru IWASAKI {
1698a1fccb47SMitsuru IWASAKI     struct acpi_softc		*sc;
1699a1fccb47SMitsuru IWASAKI     ACPI_STATUS			status;
1700a1fccb47SMitsuru IWASAKI     ACPI_BUFFER			prw_buffer;
1701a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			*res;
1702a1fccb47SMitsuru IWASAKI 
1703a1fccb47SMitsuru IWASAKI     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1704a1fccb47SMitsuru IWASAKI 
1705a1fccb47SMitsuru IWASAKI     if ((sc = devclass_get_softc(acpi_devclass, 0)) == NULL) {
1706a1fccb47SMitsuru IWASAKI 	return;
1707a1fccb47SMitsuru IWASAKI     }
1708a1fccb47SMitsuru IWASAKI 
1709a1fccb47SMitsuru IWASAKI     /*
1710a1fccb47SMitsuru IWASAKI      * _PRW object is only required for devices that have the ability
1711a1fccb47SMitsuru IWASAKI      * to wake the system from a system sleeping state.
1712a1fccb47SMitsuru IWASAKI      */
1713a1fccb47SMitsuru IWASAKI     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
1714a1fccb47SMitsuru IWASAKI     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
1715a1fccb47SMitsuru IWASAKI     if (ACPI_FAILURE(status)) {
1716a1fccb47SMitsuru IWASAKI 	return;
1717a1fccb47SMitsuru IWASAKI     }
1718a1fccb47SMitsuru IWASAKI 
1719a1fccb47SMitsuru IWASAKI     res = (ACPI_OBJECT *)prw_buffer.Pointer;
1720a1fccb47SMitsuru IWASAKI     if (res == NULL) {
1721a1fccb47SMitsuru IWASAKI 	return;
1722a1fccb47SMitsuru IWASAKI     }
1723a1fccb47SMitsuru IWASAKI 
1724a1fccb47SMitsuru IWASAKI     if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) {
1725a1fccb47SMitsuru IWASAKI 	goto out;
1726a1fccb47SMitsuru IWASAKI     }
1727a1fccb47SMitsuru IWASAKI 
1728a1fccb47SMitsuru IWASAKI     /*
1729a1fccb47SMitsuru IWASAKI      * The element 1 of the _PRW object:
1730a1fccb47SMitsuru IWASAKI      * The lowest power system sleeping state that can be entered
1731a1fccb47SMitsuru IWASAKI      * while still providing wake functionality.
1732a1fccb47SMitsuru IWASAKI      * The sleeping state being entered must be greater or equal to
1733a1fccb47SMitsuru IWASAKI      * the power state declared in element 1 of the _PRW object.
1734a1fccb47SMitsuru IWASAKI      */
1735a1fccb47SMitsuru IWASAKI     if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER) {
1736a1fccb47SMitsuru IWASAKI 	goto out;
1737a1fccb47SMitsuru IWASAKI     }
1738a1fccb47SMitsuru IWASAKI 
1739a1fccb47SMitsuru IWASAKI     if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value) {
1740a1fccb47SMitsuru IWASAKI 	goto out;
1741a1fccb47SMitsuru IWASAKI     }
1742a1fccb47SMitsuru IWASAKI 
1743a1fccb47SMitsuru IWASAKI     /*
1744a1fccb47SMitsuru IWASAKI      * The element 0 of the _PRW object:
1745a1fccb47SMitsuru IWASAKI      */
1746a1fccb47SMitsuru IWASAKI     switch(res->Package.Elements[0].Type) {
1747a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_INTEGER:
1748a1fccb47SMitsuru IWASAKI 	/*
1749a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is numeric, then this
1750a1fccb47SMitsuru IWASAKI 	 * _PRW package element is the bit index in the GPEx_EN, in the
1751a1fccb47SMitsuru IWASAKI 	 * GPE blocks described in the FADT, of the enable bit that is
1752a1fccb47SMitsuru IWASAKI 	 * enabled for the wake event.
1753a1fccb47SMitsuru IWASAKI 	 */
1754a1fccb47SMitsuru IWASAKI 
17556fca9360SNate Lawson 	status = AcpiEnableGpe(NULL, res->Package.Elements[0].Integer.Value,
17566fca9360SNate Lawson 			       ACPI_EVENT_WAKE_ENABLE);
1757a1fccb47SMitsuru IWASAKI 	if (ACPI_FAILURE(status))
1758a1fccb47SMitsuru IWASAKI             printf("%s: EnableEvent Failed\n", __func__);
1759a1fccb47SMitsuru IWASAKI 	break;
1760a1fccb47SMitsuru IWASAKI 
1761a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_PACKAGE:
1762a1fccb47SMitsuru IWASAKI 	/* XXX TBD */
1763a1fccb47SMitsuru IWASAKI 
1764a1fccb47SMitsuru IWASAKI 	/*
1765a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is a package, then this
1766a1fccb47SMitsuru IWASAKI 	 * _PRW package element is itself a package containing two
1767a1fccb47SMitsuru IWASAKI 	 * elements. The first is an object reference to the GPE Block
1768a1fccb47SMitsuru IWASAKI 	 * device that contains the GPE that will be triggered by the wake
1769a1fccb47SMitsuru IWASAKI 	 * event. The second element is numeric and it contains the bit
1770a1fccb47SMitsuru IWASAKI 	 * index in the GPEx_EN, in the GPE Block referenced by the
1771a1fccb47SMitsuru IWASAKI 	 * first element in the package, of the enable bit that is enabled for
1772a1fccb47SMitsuru IWASAKI 	 * the wake event.
1773a1fccb47SMitsuru IWASAKI 	 * For example, if this field is a package then it is of the form:
1774a1fccb47SMitsuru IWASAKI 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
1775a1fccb47SMitsuru IWASAKI 	 */
1776a1fccb47SMitsuru IWASAKI 
1777a1fccb47SMitsuru IWASAKI 	break;
1778a1fccb47SMitsuru IWASAKI 
1779a1fccb47SMitsuru IWASAKI     default:
1780a1fccb47SMitsuru IWASAKI 	break;
1781a1fccb47SMitsuru IWASAKI     }
1782a1fccb47SMitsuru IWASAKI 
1783a1fccb47SMitsuru IWASAKI out:
1784a1fccb47SMitsuru IWASAKI     if (prw_buffer.Pointer != NULL)
1785a1fccb47SMitsuru IWASAKI 	AcpiOsFree(prw_buffer.Pointer);
1786a1fccb47SMitsuru IWASAKI     return;
1787a1fccb47SMitsuru IWASAKI }
1788a1fccb47SMitsuru IWASAKI 
1789a1fccb47SMitsuru IWASAKI /*
179015e32d5dSMike Smith  * Control interface.
179115e32d5dSMike Smith  *
17920ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
17930ae55423SMike Smith  * drivers wanting to be accessible via /dev/acpi should use the register/deregister
17940ae55423SMike Smith  * interface to make their handlers visible.
179515e32d5dSMike Smith  */
17960ae55423SMike Smith struct acpi_ioctl_hook
17970ae55423SMike Smith {
17980ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook)	link;
17990ae55423SMike Smith     u_long				cmd;
18000ae55423SMike Smith     int					(* fn)(u_long cmd, caddr_t addr, void *arg);
18010ae55423SMike Smith     void				*arg;
18020ae55423SMike Smith };
18030ae55423SMike Smith 
18040ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
18050ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
18060ae55423SMike Smith 
18070ae55423SMike Smith /*
18080ae55423SMike Smith  * Register an ioctl handler.
18090ae55423SMike Smith  */
18100ae55423SMike Smith int
18110ae55423SMike Smith acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg)
18120ae55423SMike Smith {
18130ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
18140ae55423SMike Smith 
18150ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
18160ae55423SMike Smith 	return(ENOMEM);
18170ae55423SMike Smith     hp->cmd = cmd;
18180ae55423SMike Smith     hp->fn = fn;
18190ae55423SMike Smith     hp->arg = arg;
18200ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
18210ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
18220ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
18230ae55423SMike Smith     }
18240ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
18250ae55423SMike Smith     return(0);
18260ae55423SMike Smith }
18270ae55423SMike Smith 
18280ae55423SMike Smith /*
18290ae55423SMike Smith  * Deregister an ioctl handler.
18300ae55423SMike Smith  */
18310ae55423SMike Smith void
18320ae55423SMike Smith acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg))
18330ae55423SMike Smith {
18340ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
18350ae55423SMike Smith 
18360ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
18370ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
18380ae55423SMike Smith 	    break;
18390ae55423SMike Smith 
18400ae55423SMike Smith     if (hp != NULL) {
18410ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
18420ae55423SMike Smith 	free(hp, M_ACPIDEV);
18430ae55423SMike Smith     }
18440ae55423SMike Smith }
18450ae55423SMike Smith 
184615e32d5dSMike Smith static int
18476b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td)
184815e32d5dSMike Smith {
184915e32d5dSMike Smith     return(0);
185015e32d5dSMike Smith }
185115e32d5dSMike Smith 
185215e32d5dSMike Smith static int
18536b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td)
185415e32d5dSMike Smith {
185515e32d5dSMike Smith     return(0);
185615e32d5dSMike Smith }
185715e32d5dSMike Smith 
185815e32d5dSMike Smith static int
18596b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
186015e32d5dSMike Smith {
186115e32d5dSMike Smith     struct acpi_softc		*sc;
18620ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
18630ae55423SMike Smith     int				error, xerror, state;
1864fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
186515e32d5dSMike Smith 
1866cb9b0d80SMike Smith     ACPI_LOCK;
1867cb9b0d80SMike Smith 
186815e32d5dSMike Smith     error = state = 0;
186915e32d5dSMike Smith     sc = dev->si_drv1;
187015e32d5dSMike Smith 
18710ae55423SMike Smith     /*
18720ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
18730ae55423SMike Smith      */
18740ae55423SMike Smith     if (acpi_ioctl_hooks_initted) {
18750ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
18760ae55423SMike Smith 	    if (hp->cmd == cmd) {
18770ae55423SMike Smith 		xerror = hp->fn(cmd, addr, hp->arg);
18780ae55423SMike Smith 		if (xerror != 0)
18790ae55423SMike Smith 		    error = xerror;
1880917d44c8SMitsuru IWASAKI 		goto out;
18810ae55423SMike Smith 	    }
18820ae55423SMike Smith 	}
18830ae55423SMike Smith     }
18840ae55423SMike Smith 
18850ae55423SMike Smith     /*
1886a89fcf28STakanori Watanabe      * Core ioctls are  not permitted for non-writable user.
1887a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
1888a89fcf28STakanori Watanabe      * Not changing system behavior.
1889a89fcf28STakanori Watanabe      */
1890a89fcf28STakanori Watanabe     if(!(flag & FWRITE)){
1891a89fcf28STakanori Watanabe 	    return EPERM;
1892a89fcf28STakanori Watanabe     }
1893a89fcf28STakanori Watanabe 
1894a89fcf28STakanori Watanabe     /*
18950ae55423SMike Smith      * Core system ioctls.
18960ae55423SMike Smith      */
189715e32d5dSMike Smith     switch (cmd) {
189815e32d5dSMike Smith     case ACPIIO_ENABLE:
18990ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
190015e32d5dSMike Smith 	    error = ENXIO;
190115e32d5dSMike Smith 	break;
190215e32d5dSMike Smith 
190315e32d5dSMike Smith     case ACPIIO_DISABLE:
19040ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
190515e32d5dSMike Smith 	    error = ENXIO;
190615e32d5dSMike Smith 	break;
190715e32d5dSMike Smith 
190815e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
190915e32d5dSMike Smith 	if (!sc->acpi_enabled) {
191015e32d5dSMike Smith 	    error = ENXIO;
191115e32d5dSMike Smith 	    break;
191215e32d5dSMike Smith 	}
191315e32d5dSMike Smith 	state = *(int *)addr;
1914a5d1879bSMitsuru IWASAKI 	if (state >= ACPI_STATE_S0  && state <= ACPI_S_STATES_MAX) {
191515e32d5dSMike Smith 	    acpi_SetSleepState(sc, state);
191615e32d5dSMike Smith 	} else {
191715e32d5dSMike Smith 	    error = EINVAL;
191815e32d5dSMike Smith 	}
191915e32d5dSMike Smith 	break;
192015e32d5dSMike Smith 
192115e32d5dSMike Smith     default:
19220ae55423SMike Smith 	if (error == 0)
192315e32d5dSMike Smith 	    error = EINVAL;
192415e32d5dSMike Smith 	break;
192515e32d5dSMike Smith     }
1926917d44c8SMitsuru IWASAKI 
1927917d44c8SMitsuru IWASAKI out:
1928cb9b0d80SMike Smith     ACPI_UNLOCK;
192915e32d5dSMike Smith     return(error);
193015e32d5dSMike Smith }
193115e32d5dSMike Smith 
19321d073b1dSJohn Baldwin static int
1933d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
1934d75de536SMitsuru IWASAKI {
1935d75de536SMitsuru IWASAKI     char sleep_state[4];
1936d75de536SMitsuru IWASAKI     char buf[16];
1937d75de536SMitsuru IWASAKI     int error;
1938d75de536SMitsuru IWASAKI     UINT8 state, TypeA, TypeB;
1939d75de536SMitsuru IWASAKI 
1940d75de536SMitsuru IWASAKI     buf[0] = '\0';
1941d75de536SMitsuru IWASAKI     for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX+1; state++) {
1942d75de536SMitsuru IWASAKI 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
1943d75de536SMitsuru IWASAKI 	    sprintf(sleep_state, "S%d ", state);
1944d75de536SMitsuru IWASAKI 	    strcat(buf, sleep_state);
1945d75de536SMitsuru IWASAKI 	}
1946d75de536SMitsuru IWASAKI     }
1947d75de536SMitsuru IWASAKI     error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1948d75de536SMitsuru IWASAKI     return(error);
1949d75de536SMitsuru IWASAKI }
1950d75de536SMitsuru IWASAKI 
1951d75de536SMitsuru IWASAKI static int
19521d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
19531d073b1dSJohn Baldwin {
19541d073b1dSJohn Baldwin     char sleep_state[10];
19551d073b1dSJohn Baldwin     int error;
19561d073b1dSJohn Baldwin     u_int new_state, old_state;
19571d073b1dSJohn Baldwin 
19581d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
1959b8670bedSMitsuru IWASAKI     if (old_state > ACPI_S_STATES_MAX+1) {
19601d073b1dSJohn Baldwin 	strcpy(sleep_state, "unknown");
1961cb9b0d80SMike Smith     } else {
1962b8670bedSMitsuru IWASAKI 	bzero(sleep_state, sizeof(sleep_state));
19631d073b1dSJohn Baldwin 	strncpy(sleep_state, sleep_state_names[old_state],
19641d073b1dSJohn Baldwin 		sizeof(sleep_state_names[old_state]));
1965cb9b0d80SMike Smith     }
19661d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
19671d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
1968b8670bedSMitsuru IWASAKI 	for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX+1; new_state++) {
19691d073b1dSJohn Baldwin 	    if (strncmp(sleep_state, sleep_state_names[new_state],
19701d073b1dSJohn Baldwin 			sizeof(sleep_state)) == 0)
19711d073b1dSJohn Baldwin 		break;
1972cb9b0d80SMike Smith 	}
1973931a10c9SMitsuru IWASAKI 	if (new_state <= ACPI_S_STATES_MAX+1) {
1974931a10c9SMitsuru IWASAKI 	    if (new_state != old_state) {
19751d073b1dSJohn Baldwin 		*(u_int *)oidp->oid_arg1 = new_state;
1976931a10c9SMitsuru IWASAKI 	    }
1977cb9b0d80SMike Smith 	} else {
19781d073b1dSJohn Baldwin 	    error = EINVAL;
19791d073b1dSJohn Baldwin 	}
1980cb9b0d80SMike Smith     }
19811d073b1dSJohn Baldwin     return(error);
19821d073b1dSJohn Baldwin }
19831d073b1dSJohn Baldwin 
198415e32d5dSMike Smith #ifdef ACPI_DEBUG
19850ae55423SMike Smith /*
19860ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
19870ae55423SMike Smith  *
19880ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
19890ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
19900ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
19910ae55423SMike Smith  * prefixing the bit name with !.
19920ae55423SMike Smith  */
199315e32d5dSMike Smith struct debugtag
199415e32d5dSMike Smith {
199515e32d5dSMike Smith     char	*name;
199615e32d5dSMike Smith     UINT32	value;
199715e32d5dSMike Smith };
199815e32d5dSMike Smith 
199915e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
2000c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
2001c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
2002c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
2003c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
2004c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
2005c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
2006c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
2007c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
2008c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
2009d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
20104c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
2011d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
20124c1cdee6SMike Smith 
2013cb9b0d80SMike Smith     {"ACPI_BUS",		ACPI_BUS},
20144c1cdee6SMike Smith     {"ACPI_SYSTEM",		ACPI_SYSTEM},
2015cb9b0d80SMike Smith     {"ACPI_POWER",		ACPI_POWER},
2016cb9b0d80SMike Smith     {"ACPI_EC", 		ACPI_EC},
2017c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
2018c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
2019c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
20204c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
2021cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
20224c1cdee6SMike Smith     {"ACPI_FAN",		ACPI_FAN},
20234c1cdee6SMike Smith 
2024b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
2025c5ba0be4SMike Smith     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
202615e32d5dSMike Smith     {NULL, 0}
202715e32d5dSMike Smith };
202815e32d5dSMike Smith 
202915e32d5dSMike Smith static struct debugtag dbg_level[] = {
20304c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
20319501b603SJohn Baldwin     {"ACPI_LV_WARN",		ACPI_LV_WARN},
20329501b603SJohn Baldwin     {"ACPI_LV_INIT",		ACPI_LV_INIT},
20334c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
20349501b603SJohn Baldwin     {"ACPI_LV_INFO",		ACPI_LV_INFO},
20354c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
2036d62ab2f4SMitsuru IWASAKI 
2037d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
20384c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
20394c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
2040d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
20414c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
20424c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
20434c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
20444c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
20454c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
20464c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
20474c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
20484c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
20494c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
20504c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
20519501b603SJohn Baldwin     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
2052d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
2053d62ab2f4SMitsuru IWASAKI 
2054d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
2055d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
2056d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
2057d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
2058d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
20594c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
2060d62ab2f4SMitsuru IWASAKI 
2061d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2062d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
2063d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
2064d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
2065d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
2066d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
2067d62ab2f4SMitsuru IWASAKI 
2068d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
206998479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
207098479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
207198479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
207298479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
207398479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
207415e32d5dSMike Smith     {NULL, 0}
207515e32d5dSMike Smith };
207615e32d5dSMike Smith 
207715e32d5dSMike Smith static void
207815e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
207915e32d5dSMike Smith {
208015e32d5dSMike Smith     char	*ep;
208115e32d5dSMike Smith     int		i, l;
20820ae55423SMike Smith     int		set;
208315e32d5dSMike Smith 
208415e32d5dSMike Smith     while (*cp) {
208515e32d5dSMike Smith 	if (isspace(*cp)) {
208615e32d5dSMike Smith 	    cp++;
208715e32d5dSMike Smith 	    continue;
208815e32d5dSMike Smith 	}
208915e32d5dSMike Smith 	ep = cp;
209015e32d5dSMike Smith 	while (*ep && !isspace(*ep))
209115e32d5dSMike Smith 	    ep++;
20920ae55423SMike Smith 	if (*cp == '!') {
20930ae55423SMike Smith 	    set = 0;
20940ae55423SMike Smith 	    cp++;
20950ae55423SMike Smith 	    if (cp == ep)
20960ae55423SMike Smith 		continue;
20970ae55423SMike Smith 	} else {
20980ae55423SMike Smith 	    set = 1;
20990ae55423SMike Smith 	}
210015e32d5dSMike Smith 	l = ep - cp;
210115e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
210215e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
21030ae55423SMike Smith 		if (set) {
210415e32d5dSMike Smith 		    *flag |= tag[i].value;
21050ae55423SMike Smith 		} else {
21060ae55423SMike Smith 		    *flag &= ~tag[i].value;
21070ae55423SMike Smith 		}
210815e32d5dSMike Smith 		printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
210915e32d5dSMike Smith 	    }
211015e32d5dSMike Smith 	}
211115e32d5dSMike Smith 	cp = ep;
211215e32d5dSMike Smith     }
211315e32d5dSMike Smith }
211415e32d5dSMike Smith 
211515e32d5dSMike Smith static void
21162a4ac806SMike Smith acpi_set_debugging(void *junk)
211715e32d5dSMike Smith {
211894aae282SMike Barcroft     char	*cp;
211915e32d5dSMike Smith 
212087b45ed5SMitsuru IWASAKI     if (!cold)
212187b45ed5SMitsuru IWASAKI 	return;
212287b45ed5SMitsuru IWASAKI 
21230ae55423SMike Smith     AcpiDbgLayer = 0;
21240ae55423SMike Smith     AcpiDbgLevel = 0;
212594aae282SMike Barcroft     if ((cp = getenv("debug.acpi.layer")) != NULL) {
212615e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
212794aae282SMike Barcroft 	freeenv(cp);
212894aae282SMike Barcroft     }
212994aae282SMike Barcroft     if ((cp = getenv("debug.acpi.level")) != NULL) {
213015e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
213194aae282SMike Barcroft 	freeenv(cp);
213294aae282SMike Barcroft     }
21330ae55423SMike Smith 
21340ae55423SMike Smith     printf("ACPI debug layer 0x%x  debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel);
213515e32d5dSMike Smith }
21362a4ac806SMike Smith SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL);
213715e32d5dSMike Smith #endif
2138f9390180SMitsuru IWASAKI 
2139f9390180SMitsuru IWASAKI static int
2140f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
2141f9390180SMitsuru IWASAKI {
2142f9390180SMitsuru IWASAKI 	int	state, acpi_state;
2143f9390180SMitsuru IWASAKI 	int	error;
2144f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
2145f9390180SMitsuru IWASAKI 	va_list	ap;
2146f9390180SMitsuru IWASAKI 
2147f9390180SMitsuru IWASAKI 	error = 0;
2148f9390180SMitsuru IWASAKI 	switch (cmd) {
2149f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
2150f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
2151f9390180SMitsuru IWASAKI 		if (sc == NULL) {
2152f9390180SMitsuru IWASAKI 			error = EINVAL;
2153f9390180SMitsuru IWASAKI 			goto out;
2154f9390180SMitsuru IWASAKI 		}
2155f9390180SMitsuru IWASAKI 
2156f9390180SMitsuru IWASAKI 		va_start(ap, arg);
2157f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
2158f9390180SMitsuru IWASAKI 		va_end(ap);
2159f9390180SMitsuru IWASAKI 
2160f9390180SMitsuru IWASAKI 		switch (state) {
2161f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
2162f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
2163f9390180SMitsuru IWASAKI 			break;
2164f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
2165f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
2166f9390180SMitsuru IWASAKI 			break;
2167f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
2168f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
2169f9390180SMitsuru IWASAKI 			break;
2170f9390180SMitsuru IWASAKI 		default:
2171f9390180SMitsuru IWASAKI 			error = EINVAL;
2172f9390180SMitsuru IWASAKI 			goto out;
2173f9390180SMitsuru IWASAKI 		}
2174f9390180SMitsuru IWASAKI 
2175f9390180SMitsuru IWASAKI 		acpi_SetSleepState(sc, acpi_state);
2176f9390180SMitsuru IWASAKI 		break;
2177f9390180SMitsuru IWASAKI 
2178f9390180SMitsuru IWASAKI 	default:
2179f9390180SMitsuru IWASAKI 		error = EINVAL;
2180f9390180SMitsuru IWASAKI 		goto out;
2181f9390180SMitsuru IWASAKI 	}
2182f9390180SMitsuru IWASAKI 
2183f9390180SMitsuru IWASAKI out:
2184f9390180SMitsuru IWASAKI 	return (error);
2185f9390180SMitsuru IWASAKI }
2186f9390180SMitsuru IWASAKI 
2187f9390180SMitsuru IWASAKI static void
2188f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
2189f9390180SMitsuru IWASAKI {
21906c407052SMitsuru IWASAKI 
219187b45ed5SMitsuru IWASAKI     if (!cold)
219287b45ed5SMitsuru IWASAKI 	return;
219387b45ed5SMitsuru IWASAKI 
21948a9bc9c0SJohn Baldwin     if (resource_disabled("acpi", 0))
21956c407052SMitsuru IWASAKI 		return;
2196f9390180SMitsuru IWASAKI 
2197f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
2198f9390180SMitsuru IWASAKI }
2199f9390180SMitsuru IWASAKI 
2200f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
2201f9390180SMitsuru IWASAKI 
2202