xref: /freebsd/sys/dev/acpica/acpi.c (revision f8335e3a97801ebb49d091a7e09a33711d56a76c)
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");
436c6a78e98STakanori Watanabe     sc->acpi_disable_on_poweroff = 1;
437f3a6cbb6SMitsuru IWASAKI     sc->acpi_sleep_delay = 0;
438931a10c9SMitsuru IWASAKI     sc->acpi_s4bios = 1;
4392d644607SMitsuru IWASAKI     if (bootverbose)
4402d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
441498d464fSMitsuru IWASAKI     if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) {
442498d464fSMitsuru IWASAKI 	sc->acpi_verbose = 1;
443498d464fSMitsuru IWASAKI 	freeenv(env);
444498d464fSMitsuru IWASAKI     }
4451d073b1dSJohn Baldwin 
4461d073b1dSJohn Baldwin     /*
44715e32d5dSMike Smith      * Dispatch the default sleep state to devices.
44815e32d5dSMike Smith      * TBD: should be configured from userland policy manager.
44915e32d5dSMike Smith      */
45015e32d5dSMike Smith     sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
45115e32d5dSMike Smith     sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
45215e32d5dSMike Smith     sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
453f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
454f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
45515e32d5dSMike Smith 
45613d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
45715e32d5dSMike Smith 
45815e32d5dSMike Smith     /*
45915e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
46015e32d5dSMike Smith      */
461d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
462d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
463d786139cSMaxime Henrion     if (debugpoint) {
464d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "probe"))
46515e32d5dSMike Smith 	    acpi_EnterDebugger();
466d786139cSMaxime Henrion 	freeenv(debugpoint);
467d786139cSMaxime Henrion     }
46815e32d5dSMike Smith #endif
46915e32d5dSMike Smith 
47015e32d5dSMike Smith     /*
47115e32d5dSMike Smith      * Register our shutdown handlers
47215e32d5dSMike Smith      */
47315e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST);
47415e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST);
47515e32d5dSMike Smith 
47615e32d5dSMike Smith     /*
47715e32d5dSMike Smith      * Register our acpi event handlers.
47815e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
47915e32d5dSMike Smith      */
48015e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST);
48115e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST);
48215e32d5dSMike Smith 
48315e32d5dSMike Smith     /*
48415e32d5dSMike Smith      * Flag our initial states.
48515e32d5dSMike Smith      */
48615e32d5dSMike Smith     sc->acpi_enabled = 1;
48715e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
488ece50487SMitsuru IWASAKI     sc->acpi_sleep_disabled = 0;
48915e32d5dSMike Smith 
49015e32d5dSMike Smith     /*
49115e32d5dSMike Smith      * Create the control device
49215e32d5dSMike Smith      */
493a89fcf28STakanori Watanabe     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
4942a4689e8SRobert Watson 	"acpi");
49515e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
49615e32d5dSMike Smith 
497d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
498d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
499d786139cSMaxime Henrion     if (debugpoint) {
500d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "running"))
50115e32d5dSMike Smith 	    acpi_EnterDebugger();
502d786139cSMaxime Henrion 	freeenv(debugpoint);
503d786139cSMaxime Henrion     }
50415e32d5dSMike Smith #endif
505f86214b6SMitsuru IWASAKI 
50691da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS
507c573e654SMitsuru IWASAKI     if ((error = acpi_task_thread_init())) {
508c573e654SMitsuru IWASAKI 	goto out;
509c573e654SMitsuru IWASAKI     }
510c573e654SMitsuru IWASAKI #endif
511c573e654SMitsuru IWASAKI 
512f86214b6SMitsuru IWASAKI     if ((error = acpi_machdep_init(dev))) {
513f86214b6SMitsuru IWASAKI 	goto out;
514f86214b6SMitsuru IWASAKI     }
515f86214b6SMitsuru IWASAKI 
516f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
517f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
518f9390180SMitsuru IWASAKI 
519eeb6dba2SJohn Baldwin     if (!acpi_disabled("bus"))
520eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
521eeb6dba2SJohn Baldwin 
522cb9b0d80SMike Smith     error = 0;
523cb9b0d80SMike Smith 
524cb9b0d80SMike Smith  out:
525cb9b0d80SMike Smith     ACPI_UNLOCK;
526cb9b0d80SMike Smith     return_VALUE(error);
52715e32d5dSMike Smith }
52815e32d5dSMike Smith 
52915e32d5dSMike Smith /*
53015e32d5dSMike Smith  * Handle a new device being added
53115e32d5dSMike Smith  */
53215e32d5dSMike Smith static device_t
53315e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
53415e32d5dSMike Smith {
53515e32d5dSMike Smith     struct acpi_device	*ad;
53615e32d5dSMike Smith     device_t		child;
53715e32d5dSMike Smith 
53815e32d5dSMike Smith     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL)
53915e32d5dSMike Smith 	return(NULL);
54015e32d5dSMike Smith     bzero(ad, sizeof(*ad));
54115e32d5dSMike Smith 
54215e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
54315e32d5dSMike Smith 
54415e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
54515e32d5dSMike Smith     if (child != NULL)
54615e32d5dSMike Smith 	device_set_ivars(child, ad);
54715e32d5dSMike Smith     return(child);
54815e32d5dSMike Smith }
54915e32d5dSMike Smith 
55015e32d5dSMike Smith static int
55115e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
55215e32d5dSMike Smith {
55315e32d5dSMike Smith     struct acpi_device		*adev = device_get_ivars(child);
55415e32d5dSMike Smith     struct resource_list	*rl = &adev->ad_rl;
55515e32d5dSMike Smith     int retval = 0;
55615e32d5dSMike Smith 
55715e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
5589ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
5599ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
5609ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
5619ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
56215e32d5dSMike Smith     retval += bus_print_child_footer(bus, child);
56315e32d5dSMike Smith 
56415e32d5dSMike Smith     return(retval);
56515e32d5dSMike Smith }
56615e32d5dSMike Smith 
56715e32d5dSMike Smith 
56815e32d5dSMike Smith /*
56915e32d5dSMike Smith  * Handle per-device ivars
57015e32d5dSMike Smith  */
57115e32d5dSMike Smith static int
57215e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
57315e32d5dSMike Smith {
57415e32d5dSMike Smith     struct acpi_device	*ad;
57515e32d5dSMike Smith 
57615e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
57715e32d5dSMike Smith 	printf("device has no ivars\n");
57815e32d5dSMike Smith 	return(ENOENT);
57915e32d5dSMike Smith     }
58015e32d5dSMike Smith 
58115e32d5dSMike Smith     switch(index) {
58215e32d5dSMike Smith 	/* ACPI ivars */
58315e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
58415e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
58515e32d5dSMike Smith 	break;
58615e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
58715e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
58815e32d5dSMike Smith 	break;
58915e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
59015e32d5dSMike Smith 	*(void **)result = ad->ad_private;
59115e32d5dSMike Smith 	break;
59215e32d5dSMike Smith 
59332d18aa5SMike Smith 	/* ISA compatibility */
59432d18aa5SMike Smith     case ISA_IVAR_VENDORID:
59532d18aa5SMike Smith     case ISA_IVAR_SERIAL:
59632d18aa5SMike Smith     case ISA_IVAR_COMPATID:
59732d18aa5SMike Smith 	*(int *)result = -1;
59832d18aa5SMike Smith 	break;
59932d18aa5SMike Smith 
60032d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
60132d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
60232d18aa5SMike Smith 	break;
60332d18aa5SMike Smith 
60415e32d5dSMike Smith     default:
60515e32d5dSMike Smith 	return(ENOENT);
60615e32d5dSMike Smith     }
60715e32d5dSMike Smith     return(0);
60815e32d5dSMike Smith }
60915e32d5dSMike Smith 
61015e32d5dSMike Smith static int
61115e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
61215e32d5dSMike Smith {
61315e32d5dSMike Smith     struct acpi_device	*ad;
61415e32d5dSMike Smith 
61515e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
61615e32d5dSMike Smith 	printf("device has no ivars\n");
61715e32d5dSMike Smith 	return(ENOENT);
61815e32d5dSMike Smith     }
61915e32d5dSMike Smith 
62015e32d5dSMike Smith     switch(index) {
62115e32d5dSMike Smith 	/* ACPI ivars */
62215e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
62315e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
62415e32d5dSMike Smith 	break;
62515e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
62615e32d5dSMike Smith 	ad->ad_magic = (int )value;
62715e32d5dSMike Smith 	break;
62815e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
62915e32d5dSMike Smith 	ad->ad_private = (void *)value;
63015e32d5dSMike Smith 	break;
63115e32d5dSMike Smith 
63215e32d5dSMike Smith     default:
6332ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
63415e32d5dSMike Smith 	return(ENOENT);
63515e32d5dSMike Smith     }
63615e32d5dSMike Smith     return(0);
63715e32d5dSMike Smith }
63815e32d5dSMike Smith 
63915e32d5dSMike Smith /*
64015e32d5dSMike Smith  * Handle child resource allocation/removal
64115e32d5dSMike Smith  */
64215e32d5dSMike Smith static int
64315e32d5dSMike Smith acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
64415e32d5dSMike Smith {
64515e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
64615e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
64715e32d5dSMike Smith 
64815e32d5dSMike Smith     resource_list_add(rl, type, rid, start, start + count -1, count);
64915e32d5dSMike Smith 
65015e32d5dSMike Smith     return(0);
65115e32d5dSMike Smith }
65215e32d5dSMike Smith 
65315e32d5dSMike Smith static int
65415e32d5dSMike Smith acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
65515e32d5dSMike Smith {
65615e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
65715e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
65815e32d5dSMike Smith     struct resource_list_entry	*rle;
65915e32d5dSMike Smith 
66015e32d5dSMike Smith     rle = resource_list_find(rl, type, rid);
66115e32d5dSMike Smith     if (!rle)
66215e32d5dSMike Smith 	return(ENOENT);
66315e32d5dSMike Smith 
66415e32d5dSMike Smith     if (startp)
66515e32d5dSMike Smith 	*startp = rle->start;
66615e32d5dSMike Smith     if (countp)
66715e32d5dSMike Smith 	*countp = rle->count;
66815e32d5dSMike Smith 
66915e32d5dSMike Smith     return(0);
67015e32d5dSMike Smith }
67115e32d5dSMike Smith 
67215e32d5dSMike Smith static struct resource *
67315e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
67415e32d5dSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
67515e32d5dSMike Smith {
67615e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
67715e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
67815e32d5dSMike Smith 
67915e32d5dSMike Smith     return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags));
68015e32d5dSMike Smith }
68115e32d5dSMike Smith 
68215e32d5dSMike Smith static int
68315e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
68415e32d5dSMike Smith {
68515e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
68615e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
68715e32d5dSMike Smith 
68815e32d5dSMike Smith     return(resource_list_release(rl, bus, child, type, rid, r));
68915e32d5dSMike Smith }
69015e32d5dSMike Smith 
69115e32d5dSMike Smith /*
692bc0f2195SMike Smith  * Handle ISA-like devices probing for a PnP ID to match.
693bc0f2195SMike Smith  */
694bc0f2195SMike Smith #define PNP_EISAID(s)				\
695bc0f2195SMike Smith 	((((s[0] - '@') & 0x1f) << 2)		\
696bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x18) >> 3)		\
697bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x07) << 13)	\
698bc0f2195SMike Smith 	 | (((s[2] - '@') & 0x1f) << 8)		\
699bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[4]) << 16)		\
700bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[3]) << 20)		\
701bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[6]) << 24)		\
702bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[5]) << 28))
703bc0f2195SMike Smith 
70432d18aa5SMike Smith static u_int32_t
70532d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
706bc0f2195SMike Smith {
707bc0f2195SMike Smith     ACPI_HANDLE		h;
708bc0f2195SMike Smith     ACPI_DEVICE_INFO	devinfo;
7096fca9360SNate Lawson     ACPI_BUFFER		buf = {sizeof(devinfo), &devinfo};
710bc0f2195SMike Smith     ACPI_STATUS		error;
711bc0f2195SMike Smith     u_int32_t		pnpid;
712fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
71332d18aa5SMike Smith 
714b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
71532d18aa5SMike Smith 
71632d18aa5SMike Smith     pnpid = 0;
71732d18aa5SMike Smith     ACPI_LOCK;
71832d18aa5SMike Smith 
7196fca9360SNate Lawson     /* Fetch and validate the HID. */
72032d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
72132d18aa5SMike Smith 	goto out;
7226fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
7236fca9360SNate Lawson     if (ACPI_FAILURE(error))
72432d18aa5SMike Smith 	goto out;
7256fca9360SNate Lawson     if ((devinfo.Valid & ACPI_VALID_HID) == 0)
72632d18aa5SMike Smith 	goto out;
72732d18aa5SMike Smith 
7286fca9360SNate Lawson     pnpid = PNP_EISAID(devinfo.HardwareId.Value);
72932d18aa5SMike Smith out:
73032d18aa5SMike Smith     ACPI_UNLOCK;
73132d18aa5SMike Smith     return_VALUE(pnpid);
73232d18aa5SMike Smith }
73332d18aa5SMike Smith 
734b2566207STakanori Watanabe static u_int32_t
735b2566207STakanori Watanabe acpi_isa_get_compatid(device_t dev)
736b2566207STakanori Watanabe {
737b2566207STakanori Watanabe     ACPI_HANDLE		h;
738b2566207STakanori Watanabe     ACPI_STATUS		error;
739b2566207STakanori Watanabe     u_int32_t		pnpid;
740b2566207STakanori Watanabe     ACPI_LOCK_DECL;
741b2566207STakanori Watanabe 
742b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
743b2566207STakanori Watanabe 
744b2566207STakanori Watanabe     pnpid = 0;
745b2566207STakanori Watanabe     ACPI_LOCK;
746b2566207STakanori Watanabe 
747b2566207STakanori Watanabe     /* fetch and validate the HID */
748b2566207STakanori Watanabe     if ((h = acpi_get_handle(dev)) == NULL)
749b2566207STakanori Watanabe 	goto out;
750b2566207STakanori Watanabe     if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &pnpid)))
751b2566207STakanori Watanabe 	goto out;
752b2566207STakanori Watanabe 
753b2566207STakanori Watanabe out:
754b2566207STakanori Watanabe     ACPI_UNLOCK;
755b2566207STakanori Watanabe     return_VALUE(pnpid);
756b2566207STakanori Watanabe }
757b2566207STakanori Watanabe 
758b2566207STakanori Watanabe 
75932d18aa5SMike Smith static int
76032d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
76132d18aa5SMike Smith {
762bc0f2195SMike Smith     int			result;
763b2566207STakanori Watanabe     u_int32_t		lid, cid;
764bc0f2195SMike Smith 
765b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
766bc0f2195SMike Smith 
767bc0f2195SMike Smith     /*
768bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
769bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
770bc0f2195SMike Smith      * that to happen, so don't ever return it.
771bc0f2195SMike Smith      */
772bc0f2195SMike Smith     result = ENXIO;
773bc0f2195SMike Smith 
774bc0f2195SMike Smith     /* scan the supplied IDs for a match */
775b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
776b2566207STakanori Watanabe     cid = acpi_isa_get_compatid(child);
777bc0f2195SMike Smith     while (ids && ids->ip_id) {
778b2566207STakanori Watanabe 	if (lid == ids->ip_id || cid == ids->ip_id) {
779bc0f2195SMike Smith 	    result = 0;
780bc0f2195SMike Smith 	    goto out;
781bc0f2195SMike Smith 	}
782bc0f2195SMike Smith 	ids++;
783bc0f2195SMike Smith     }
784bc0f2195SMike Smith  out:
785bc0f2195SMike Smith     return_VALUE(result);
786bc0f2195SMike Smith }
787bc0f2195SMike Smith 
788bc0f2195SMike Smith /*
78915e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
79015e32d5dSMike Smith  *
7917d3bcec9SMike Smith  * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and \_SB_ scopes,
7927d3bcec9SMike Smith  * and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
79315e32d5dSMike Smith  */
79415e32d5dSMike Smith static void
79515e32d5dSMike Smith acpi_probe_children(device_t bus)
79615e32d5dSMike Smith {
79715e32d5dSMike Smith     ACPI_HANDLE		parent;
7987d3bcec9SMike Smith     static char		*scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
79915e32d5dSMike Smith     int			i;
80015e32d5dSMike Smith 
801b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
802cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
8030ae55423SMike Smith 
80415e32d5dSMike Smith     /*
80515e32d5dSMike Smith      * Create any static children by calling device identify methods.
80615e32d5dSMike Smith      */
8074c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
80815e32d5dSMike Smith     bus_generic_probe(bus);
80915e32d5dSMike Smith 
81015e32d5dSMike Smith     /*
81115e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
81215e32d5dSMike Smith      * we find.
81315e32d5dSMike Smith      *
81415e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
81515e32d5dSMike Smith      * we want to create nodes for all devices, not just those that are currently
81615e32d5dSMike Smith      * present. (This assumes that we don't want to create/remove devices as they
81715e32d5dSMike Smith      * appear, which might be smarter.)
81815e32d5dSMike Smith      */
8194c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
82015e32d5dSMike Smith     for (i = 0; scopes[i] != NULL; i++)
821b53f2771SMike Smith 	if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent)))
82215e32d5dSMike Smith 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL);
82315e32d5dSMike Smith 
82415e32d5dSMike Smith     /*
82515e32d5dSMike Smith      * Scan all of the child devices we have created and let them probe/attach.
82615e32d5dSMike Smith      */
8274c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
82815e32d5dSMike Smith     bus_generic_attach(bus);
82915e32d5dSMike Smith 
83015e32d5dSMike Smith     /*
83115e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
83215e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
83315e32d5dSMike Smith      */
8344c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
83515e32d5dSMike Smith     bus_generic_attach(bus);
8360ae55423SMike Smith 
837bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
8380ae55423SMike Smith     return_VOID;
83915e32d5dSMike Smith }
84015e32d5dSMike Smith 
84115e32d5dSMike Smith /*
84215e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
84315e32d5dSMike Smith  * it.
84415e32d5dSMike Smith  */
84515e32d5dSMike Smith static ACPI_STATUS
84615e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
84715e32d5dSMike Smith {
84815e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
84915e32d5dSMike Smith     device_t		child, bus = (device_t)context;
85015e32d5dSMike Smith 
851b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
8520ae55423SMike Smith 
8530ae55423SMike Smith     /*
8540ae55423SMike Smith      * Skip this device if we think we'll have trouble with it.
8550ae55423SMike Smith      */
8560ae55423SMike Smith     if (acpi_avoid(handle))
8570ae55423SMike Smith 	return_ACPI_STATUS(AE_OK);
8580ae55423SMike Smith 
859b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
86015e32d5dSMike Smith 	switch(type) {
86115e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
86215e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
86315e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
86415e32d5dSMike Smith 	case ACPI_TYPE_POWER:
8650ae55423SMike Smith 	    if (acpi_disabled("children"))
8660ae55423SMike Smith 		break;
86715e32d5dSMike Smith 	    /*
86815e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
86915e32d5dSMike Smith 	     * so that the probe/attach passes will run breadth-first.
87015e32d5dSMike Smith 	     */
8714c1cdee6SMike Smith 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", acpi_name(handle)));
87215e32d5dSMike Smith 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
873bc0f2195SMike Smith 	    if (child == NULL)
874bc0f2195SMike Smith 		break;
87515e32d5dSMike Smith 	    acpi_set_handle(child, handle);
876bc0f2195SMike Smith 
877bc0f2195SMike Smith 	    /*
878b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
879b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
880b519f9d6SMike Smith 	     * the handle, but we don't probe it).
881b519f9d6SMike Smith 	     */
88223b4e251SMitsuru IWASAKI 	    if ((type == ACPI_TYPE_DEVICE) && (!acpi_DeviceIsPresent(child))) {
883b519f9d6SMike Smith 		device_disable(child);
884b519f9d6SMike Smith 		break;
885b519f9d6SMike Smith 	    }
886b519f9d6SMike Smith 
887b519f9d6SMike Smith 	    /*
888bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
889bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
890bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
891bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
892bc0f2195SMike Smith 	     * device not to have any resources.
893bc0f2195SMike Smith 	     */
894bc0f2195SMike Smith 	    acpi_parse_resources(child, handle, &acpi_res_parse_set);
895bc0f2195SMike Smith 
896bc0f2195SMike Smith 	    /* if we're debugging, probe/attach now rather than later */
897b53f2771SMike Smith 	    ACPI_DEBUG_EXEC(device_probe_and_attach(child));
8980ae55423SMike Smith 	    break;
89915e32d5dSMike Smith 	}
90015e32d5dSMike Smith     }
9010ae55423SMike Smith     return_ACPI_STATUS(AE_OK);
90215e32d5dSMike Smith }
90315e32d5dSMike Smith 
90415e32d5dSMike Smith static void
90515e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto)
90615e32d5dSMike Smith {
907cb9b0d80SMike Smith 
908c6a78e98STakanori Watanabe     struct acpi_softc *sc = arg;
909c6a78e98STakanori Watanabe 
910cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
911cb9b0d80SMike Smith 
91215e32d5dSMike Smith     /*
9130ae55423SMike Smith      * Disable all ACPI events before soft off, otherwise the system
91415e32d5dSMike Smith      * will be turned on again on some laptops.
91515e32d5dSMike Smith      *
91615e32d5dSMike Smith      * XXX this should probably be restricted to masking some events just
91715e32d5dSMike Smith      *     before powering down, since we may still need ACPI during the
91815e32d5dSMike Smith      *     shutdown process.
91915e32d5dSMike Smith      */
920c6a78e98STakanori Watanabe     if (sc->acpi_disable_on_poweroff)
921c6a78e98STakanori Watanabe 	acpi_Disable(sc);
92215e32d5dSMike Smith }
92315e32d5dSMike Smith 
92415e32d5dSMike Smith static void
92515e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
92615e32d5dSMike Smith {
92715e32d5dSMike Smith     ACPI_STATUS	status;
92815e32d5dSMike Smith 
929cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
930cb9b0d80SMike Smith 
9315f3e4175SMitsuru IWASAKI     if (howto & RB_POWEROFF) {
93215e32d5dSMike Smith 	printf("Power system off using ACPI...\n");
933b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(acpi_off_state))) {
934b9c780d6SMitsuru IWASAKI 	    printf("AcpiEnterSleepStatePrep failed - %s\n",
935b9c780d6SMitsuru IWASAKI 		   AcpiFormatException(status));
936b9c780d6SMitsuru IWASAKI 	    return;
937b9c780d6SMitsuru IWASAKI 	}
938b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepState(acpi_off_state))) {
939bfae45aaSMike Smith 	    printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
94015e32d5dSMike Smith 	} else {
94115e32d5dSMike Smith 	    DELAY(1000000);
94215e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
94315e32d5dSMike Smith 	}
944494ae560SMitsuru IWASAKI     } else {
945494ae560SMitsuru IWASAKI 	printf("Terminate ACPI\n");
946494ae560SMitsuru IWASAKI 	AcpiTerminate();
94715e32d5dSMike Smith     }
94815e32d5dSMike Smith }
94915e32d5dSMike Smith 
95013d4f7baSMitsuru IWASAKI static void
95113d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
95213d4f7baSMitsuru IWASAKI {
95313d4f7baSMitsuru IWASAKI     static int	first_time = 1;
95413d4f7baSMitsuru IWASAKI #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n"
95513d4f7baSMitsuru IWASAKI 
956cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
957cb9b0d80SMike Smith 
95813d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
959cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) {
96013d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
96113d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_power_button_for_sleep, sc);
96213d4f7baSMitsuru IWASAKI 	if (first_time) {
96313d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "power");
96413d4f7baSMitsuru IWASAKI 	}
96513d4f7baSMitsuru IWASAKI     }
966cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) {
96713d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
96813d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_sleep_button_for_sleep, sc);
96913d4f7baSMitsuru IWASAKI 	if (first_time) {
97013d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "sleep");
97113d4f7baSMitsuru IWASAKI 	}
97213d4f7baSMitsuru IWASAKI     }
97313d4f7baSMitsuru IWASAKI 
97413d4f7baSMitsuru IWASAKI     first_time = 0;
97513d4f7baSMitsuru IWASAKI }
97613d4f7baSMitsuru IWASAKI 
97715e32d5dSMike Smith /*
978c5ba0be4SMike Smith  * Returns true if the device is actually present and should
979c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
980c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
981c5ba0be4SMike Smith  */
982c5ba0be4SMike Smith BOOLEAN
983c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
984c5ba0be4SMike Smith {
985c5ba0be4SMike Smith     ACPI_HANDLE		h;
986c5ba0be4SMike Smith     ACPI_DEVICE_INFO	devinfo;
9876fca9360SNate Lawson     ACPI_BUFFER		buf = {sizeof(devinfo), &devinfo};
988c5ba0be4SMike Smith     ACPI_STATUS		error;
989c5ba0be4SMike Smith 
990cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
991cb9b0d80SMike Smith 
992c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
993c5ba0be4SMike Smith 	return(FALSE);
9946fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
9956fca9360SNate Lawson     if (ACPI_FAILURE(error))
996c5ba0be4SMike Smith 	return(FALSE);
99743896e91SMike Smith     /* if no _STA method, must be present */
9986fca9360SNate Lawson     if ((devinfo.Valid & ACPI_VALID_STA) == 0)
99943896e91SMike Smith 	return(TRUE);
100043896e91SMike Smith     /* return true for 'present' and 'functioning' */
100143896e91SMike Smith     if ((devinfo.CurrentStatus & 0x9) == 0x9)
1002c5ba0be4SMike Smith 	return(TRUE);
1003c5ba0be4SMike Smith     return(FALSE);
1004c5ba0be4SMike Smith }
1005c5ba0be4SMike Smith 
1006c5ba0be4SMike Smith /*
1007b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
1008b53f2771SMike Smith  */
1009b53f2771SMike Smith BOOLEAN
1010b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
1011b53f2771SMike Smith {
1012b53f2771SMike Smith     ACPI_HANDLE		h;
1013b53f2771SMike Smith     ACPI_DEVICE_INFO	devinfo;
10146fca9360SNate Lawson     ACPI_BUFFER		buf = {sizeof(devinfo), &devinfo};
1015b53f2771SMike Smith     ACPI_STATUS		error;
1016b53f2771SMike Smith 
1017b53f2771SMike Smith     ACPI_ASSERTLOCK;
1018b53f2771SMike Smith 
1019b53f2771SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1020b53f2771SMike Smith 	return(FALSE);
10216fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
10226fca9360SNate Lawson     if (ACPI_FAILURE(error))
1023b53f2771SMike Smith 	return(FALSE);
1024b53f2771SMike Smith     /* if no _STA method, must be present */
10256fca9360SNate Lawson     if ((devinfo.Valid & ACPI_VALID_STA) == 0)
1026b53f2771SMike Smith 	return(TRUE);
1027b53f2771SMike Smith     /* return true for 'present' and 'functioning' */
1028b53f2771SMike Smith     if ((devinfo.CurrentStatus & 0x19) == 0x19)
1029b53f2771SMike Smith 	return(TRUE);
1030b53f2771SMike Smith     return(FALSE);
1031b53f2771SMike Smith }
1032b53f2771SMike Smith 
1033b53f2771SMike Smith /*
103415e32d5dSMike Smith  * Match a HID string against a device
103515e32d5dSMike Smith  */
103615e32d5dSMike Smith BOOLEAN
103715e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid)
103815e32d5dSMike Smith {
103915e32d5dSMike Smith     ACPI_HANDLE		h;
104015e32d5dSMike Smith     ACPI_DEVICE_INFO	devinfo;
10416fca9360SNate Lawson     ACPI_BUFFER		buf = {sizeof(devinfo), &devinfo};
104215e32d5dSMike Smith     ACPI_STATUS		error;
1043ed136da6SDoug Rabson     int			cid;
104415e32d5dSMike Smith 
1045cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1046cb9b0d80SMike Smith 
10474d332989SMike Smith     if (hid == NULL)
104815e32d5dSMike Smith 	return(FALSE);
104915e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
105015e32d5dSMike Smith 	return(FALSE);
10516fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
10526fca9360SNate Lawson     if (ACPI_FAILURE(error))
105315e32d5dSMike Smith 	return(FALSE);
10546fca9360SNate Lawson     if ((devinfo.Valid & ACPI_VALID_HID) != 0 &&
10556fca9360SNate Lawson 	strcmp(hid, devinfo.HardwareId.Value) == 0)
105615e32d5dSMike Smith 	return(TRUE);
1057b53f2771SMike Smith     if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &cid)))
1058ed136da6SDoug Rabson 	return(FALSE);
1059ed136da6SDoug Rabson     if (cid == PNP_EISAID(hid))
1060ed136da6SDoug Rabson 	return(TRUE);
106115e32d5dSMike Smith     return(FALSE);
106215e32d5dSMike Smith }
106315e32d5dSMike Smith 
106415e32d5dSMike Smith /*
1065c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1066c5ba0be4SMike Smith  * or one if its parents.
1067c5ba0be4SMike Smith  */
1068c5ba0be4SMike Smith ACPI_STATUS
1069c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1070c5ba0be4SMike Smith {
1071c5ba0be4SMike Smith     ACPI_HANDLE		r;
1072c5ba0be4SMike Smith     ACPI_STATUS		status;
1073c5ba0be4SMike Smith 
1074cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1075cb9b0d80SMike Smith 
1076c5ba0be4SMike Smith     /* walk back up the tree to the root */
1077c5ba0be4SMike Smith     for (;;) {
1078b53f2771SMike Smith 	if (ACPI_SUCCESS(status = AcpiGetHandle(parent, path, &r))) {
1079c5ba0be4SMike Smith 	    *result = r;
1080c5ba0be4SMike Smith 	    return(AE_OK);
1081c5ba0be4SMike Smith 	}
1082c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1083c5ba0be4SMike Smith 	    return(AE_OK);
1084b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1085c5ba0be4SMike Smith 	    return(AE_NOT_FOUND);
1086c5ba0be4SMike Smith 	parent = r;
1087c5ba0be4SMike Smith     }
1088c5ba0be4SMike Smith }
1089c5ba0be4SMike Smith 
1090c5ba0be4SMike Smith /*
1091c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1092c5ba0be4SMike Smith  */
1093c5ba0be4SMike Smith ACPI_BUFFER *
1094c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1095c5ba0be4SMike Smith {
1096c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1097c5ba0be4SMike Smith 
1098c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1099c5ba0be4SMike Smith 	return(NULL);
1100c5ba0be4SMike Smith     buf->Length = size;
1101c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1102c5ba0be4SMike Smith     return(buf);
1103c5ba0be4SMike Smith }
1104c5ba0be4SMike Smith 
1105c5ba0be4SMike Smith /*
1106c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1107c5ba0be4SMike Smith  */
1108c5ba0be4SMike Smith ACPI_STATUS
1109c5ba0be4SMike Smith acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number)
1110c5ba0be4SMike Smith {
1111c5ba0be4SMike Smith     ACPI_STATUS	error;
1112c5ba0be4SMike Smith     ACPI_BUFFER	buf;
1113c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
1114c5ba0be4SMike Smith 
1115cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1116cb9b0d80SMike Smith 
1117c5ba0be4SMike Smith     if (handle == NULL)
1118c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
11190c7da7acSJohn Baldwin 
11200c7da7acSJohn Baldwin     /*
11210c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
11220c7da7acSJohn Baldwin      * a method that will return an Integer.
11230c7da7acSJohn Baldwin      */
1124c5ba0be4SMike Smith     buf.Pointer = &param;
1125c5ba0be4SMike Smith     buf.Length = sizeof(param);
1126b53f2771SMike Smith     if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
1127c5ba0be4SMike Smith 	if (param.Type == ACPI_TYPE_INTEGER) {
1128c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1129c5ba0be4SMike Smith 	} else {
1130c5ba0be4SMike Smith 	    error = AE_TYPE;
1131c5ba0be4SMike Smith 	}
1132c5ba0be4SMike Smith     }
11330c7da7acSJohn Baldwin 
11340c7da7acSJohn Baldwin     /*
11350c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
11360c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
11370c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
11380c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
11390c7da7acSJohn Baldwin      *
11400c7da7acSJohn Baldwin      * This is a hack.
11410c7da7acSJohn Baldwin      */
11420c7da7acSJohn Baldwin     if (error == AE_BUFFER_OVERFLOW) {
1143b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
11440c7da7acSJohn Baldwin 	    error = AE_NO_MEMORY;
11450c7da7acSJohn Baldwin 	} else {
1146b53f2771SMike Smith 	    if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
1147c573e654SMitsuru IWASAKI 		error = acpi_ConvertBufferToInteger(&buf, number);
11480c7da7acSJohn Baldwin 	    }
11490c7da7acSJohn Baldwin 	}
11500c7da7acSJohn Baldwin 	AcpiOsFree(buf.Pointer);
11510c7da7acSJohn Baldwin     }
1152c5ba0be4SMike Smith     return(error);
1153c5ba0be4SMike Smith }
1154c5ba0be4SMike Smith 
1155c573e654SMitsuru IWASAKI ACPI_STATUS
1156c573e654SMitsuru IWASAKI acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number)
1157c573e654SMitsuru IWASAKI {
1158c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
1159c573e654SMitsuru IWASAKI     int		i;
1160c573e654SMitsuru IWASAKI 
1161c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
1162c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
1163c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
1164c573e654SMitsuru IWASAKI 	return(AE_OK);
1165c573e654SMitsuru IWASAKI     }
1166c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
1167c573e654SMitsuru IWASAKI 	return(AE_TYPE);
1168c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
1169c573e654SMitsuru IWASAKI 	return(AE_BAD_DATA);
1170c573e654SMitsuru IWASAKI     *number = 0;
1171c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
1172c573e654SMitsuru IWASAKI 	*number += (*(p->Buffer.Pointer + i) << (i * 8));
1173c573e654SMitsuru IWASAKI     return(AE_OK);
1174c573e654SMitsuru IWASAKI }
1175c573e654SMitsuru IWASAKI 
1176c5ba0be4SMike Smith /*
1177c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1178c5ba0be4SMike Smith  * function for each element.
1179c5ba0be4SMike Smith  *
1180c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1181c5ba0be4SMike Smith  */
1182c5ba0be4SMike Smith ACPI_STATUS
1183c5ba0be4SMike Smith acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg)
1184c5ba0be4SMike Smith {
1185c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1186c5ba0be4SMike Smith     int		i;
1187c5ba0be4SMike Smith 
1188c5ba0be4SMike Smith     if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE))
1189c5ba0be4SMike Smith 	return(AE_BAD_PARAMETER);
1190c5ba0be4SMike Smith 
1191c5ba0be4SMike Smith     /* iterate over components */
1192c5ba0be4SMike Smith     for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++)
1193c5ba0be4SMike Smith 	func(comp, arg);
1194c5ba0be4SMike Smith 
1195c5ba0be4SMike Smith     return(AE_OK);
1196c5ba0be4SMike Smith }
1197c5ba0be4SMike Smith 
11986f69255bSMike Smith /*
11996f69255bSMike Smith  * Find the (index)th resource object in a set.
12006f69255bSMike Smith  */
12016f69255bSMike Smith ACPI_STATUS
12026d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
12036f69255bSMike Smith {
12046d63101aSMike Smith     ACPI_RESOURCE	*rp;
12056f69255bSMike Smith     int			i;
12066f69255bSMike Smith 
12076d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
12086f69255bSMike Smith     i = index;
12096d63101aSMike Smith     while (i-- > 0) {
12106f69255bSMike Smith 	/* range check */
12116d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
12126f69255bSMike Smith 	    return(AE_BAD_PARAMETER);
12136d63101aSMike Smith 	/* check for terminator */
12146d63101aSMike Smith 	if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
12156d63101aSMike Smith 	    (rp->Length == 0))
12166d63101aSMike Smith 	    return(AE_NOT_FOUND);
12176d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
12186f69255bSMike Smith     }
12196f69255bSMike Smith     if (resp != NULL)
12206d63101aSMike Smith 	*resp = rp;
12216d63101aSMike Smith     return(AE_OK);
12226d63101aSMike Smith }
12236d63101aSMike Smith 
12246d63101aSMike Smith /*
12256d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
12266d63101aSMike Smith  *
12276d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
12286d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
12296d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
12306d63101aSMike Smith  * resources.
12316d63101aSMike Smith  */
12326d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
12336d63101aSMike Smith 
12346d63101aSMike Smith ACPI_STATUS
12356d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
12366d63101aSMike Smith {
12376d63101aSMike Smith     ACPI_RESOURCE	*rp;
12386d63101aSMike Smith     void		*newp;
12396d63101aSMike Smith 
12406d63101aSMike Smith     /*
12416d63101aSMike Smith      * Initialise the buffer if necessary.
12426d63101aSMike Smith      */
12436d63101aSMike Smith     if (buf->Pointer == NULL) {
12446d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
12456d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
12466d63101aSMike Smith 	    return(AE_NO_MEMORY);
12476d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
12486d63101aSMike Smith 	rp->Id = ACPI_RSTYPE_END_TAG;
12496d63101aSMike Smith 	rp->Length = 0;
12506d63101aSMike Smith     }
12516d63101aSMike Smith     if (res == NULL)
12526d63101aSMike Smith 	return(AE_OK);
12536d63101aSMike Smith 
12546d63101aSMike Smith     /*
12556d63101aSMike Smith      * Scan the current buffer looking for the terminator.
12566d63101aSMike Smith      * This will either find the terminator or hit the end
12576d63101aSMike Smith      * of the buffer and return an error.
12586d63101aSMike Smith      */
12596d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
12606d63101aSMike Smith     for (;;) {
12616d63101aSMike Smith 	/* range check, don't go outside the buffer */
12626d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
12636d63101aSMike Smith 	    return(AE_BAD_PARAMETER);
12646d63101aSMike Smith 	if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
12656d63101aSMike Smith 	    (rp->Length == 0)) {
12666d63101aSMike Smith 	    break;
12676d63101aSMike Smith 	}
12686d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
12696d63101aSMike Smith     }
12706d63101aSMike Smith 
12716d63101aSMike Smith     /*
12726d63101aSMike Smith      * Check the size of the buffer and expand if required.
12736d63101aSMike Smith      *
12746d63101aSMike Smith      * Required size is:
12756d63101aSMike Smith      *	size of existing resources before terminator +
12766d63101aSMike Smith      *	size of new resource and header +
12776d63101aSMike Smith      * 	size of terminator.
12786d63101aSMike Smith      *
12796d63101aSMike Smith      * Note that this loop should really only run once, unless
12806d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
12816d63101aSMike Smith      */
12826d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
12836d63101aSMike Smith 	    res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
12846d63101aSMike Smith 	    ACPI_RESOURCE_LENGTH) >= buf->Length) {
12856d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
12866d63101aSMike Smith 	    return(AE_NO_MEMORY);
12876d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
1288a692219dSMike Smith         rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1289a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
12906d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
12916d63101aSMike Smith 	buf->Pointer = newp;
12926d63101aSMike Smith 	buf->Length += buf->Length;
12936d63101aSMike Smith     }
12946d63101aSMike Smith 
12956d63101aSMike Smith     /*
12966d63101aSMike Smith      * Insert the new resource.
12976d63101aSMike Smith      */
12986d63101aSMike Smith     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
12996d63101aSMike Smith 
13006d63101aSMike Smith     /*
13016d63101aSMike Smith      * And add the terminator.
13026d63101aSMike Smith      */
13036d63101aSMike Smith     rp = ACPI_RESOURCE_NEXT(rp);
13046d63101aSMike Smith     rp->Id = ACPI_RSTYPE_END_TAG;
13056d63101aSMike Smith     rp->Length = 0;
13066d63101aSMike Smith 
13076f69255bSMike Smith     return(AE_OK);
13086f69255bSMike Smith }
1309c5ba0be4SMike Smith 
1310da14ac9fSJohn Baldwin /*
1311da14ac9fSJohn Baldwin  * Set interrupt model.
1312da14ac9fSJohn Baldwin  */
1313da14ac9fSJohn Baldwin ACPI_STATUS
1314da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
1315da14ac9fSJohn Baldwin {
1316da14ac9fSJohn Baldwin 	ACPI_OBJECT_LIST ArgList;
1317da14ac9fSJohn Baldwin 	ACPI_OBJECT Arg;
1318da14ac9fSJohn Baldwin 
1319da14ac9fSJohn Baldwin 	Arg.Type = ACPI_TYPE_INTEGER;
1320da14ac9fSJohn Baldwin 	Arg.Integer.Value = model;
1321da14ac9fSJohn Baldwin 	ArgList.Count = 1;
1322da14ac9fSJohn Baldwin 	ArgList.Pointer = &Arg;
1323da14ac9fSJohn Baldwin 	return (AcpiEvaluateObject(ACPI_ROOT_OBJECT, "_PIC", &ArgList, NULL));
1324da14ac9fSJohn Baldwin }
1325da14ac9fSJohn Baldwin 
1326ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME	5
1327ece50487SMitsuru IWASAKI 
1328ece50487SMitsuru IWASAKI static void
1329ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
1330ece50487SMitsuru IWASAKI {
1331ece50487SMitsuru IWASAKI     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1332ece50487SMitsuru IWASAKI }
1333c30382dfSMitsuru IWASAKI 
133415e32d5dSMike Smith /*
133515e32d5dSMike Smith  * Set the system sleep state
133615e32d5dSMike Smith  *
133715e32d5dSMike Smith  * Currently we only support S1 and S5
133815e32d5dSMike Smith  */
133915e32d5dSMike Smith ACPI_STATUS
134015e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
134115e32d5dSMike Smith {
134215e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
134356d8cb57SMitsuru IWASAKI     UINT8	TypeA;
134456d8cb57SMitsuru IWASAKI     UINT8	TypeB;
134515e32d5dSMike Smith 
1346b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1347cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
13480ae55423SMike Smith 
13496397624dSMitsuru IWASAKI     if (sc->acpi_sstate != ACPI_STATE_S0)
13506397624dSMitsuru IWASAKI 	return_ACPI_STATUS(AE_BAD_PARAMETER);	/* avoid reentry */
13516397624dSMitsuru IWASAKI 
1352ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1353ece50487SMitsuru IWASAKI 	return_ACPI_STATUS(AE_OK);
1354ece50487SMitsuru IWASAKI 
135515e32d5dSMike Smith     switch (state) {
135615e32d5dSMike Smith     case ACPI_STATE_S0:	/* XXX only for testing */
1357b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
1358bfae45aaSMike Smith 	    device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
135915e32d5dSMike Smith 	}
136015e32d5dSMike Smith 	break;
136115e32d5dSMike Smith 
136215e32d5dSMike Smith     case ACPI_STATE_S1:
13636161544cSTakanori Watanabe     case ACPI_STATE_S2:
13646161544cSTakanori Watanabe     case ACPI_STATE_S3:
13656161544cSTakanori Watanabe     case ACPI_STATE_S4:
136698479b04SMitsuru IWASAKI 	if (ACPI_FAILURE(status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB))) {
136798479b04SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", AcpiFormatException(status));
136856d8cb57SMitsuru IWASAKI 	    break;
136956d8cb57SMitsuru IWASAKI 	}
137056d8cb57SMitsuru IWASAKI 
1371a1fccb47SMitsuru IWASAKI 	sc->acpi_sstate = state;
1372a1fccb47SMitsuru IWASAKI 	sc->acpi_sleep_disabled = 1;
1373a1fccb47SMitsuru IWASAKI 
137415e32d5dSMike Smith 	/*
137515e32d5dSMike Smith 	 * Inform all devices that we are going to sleep.
137615e32d5dSMike Smith 	 */
137715e32d5dSMike Smith 	if (DEVICE_SUSPEND(root_bus) != 0) {
137815e32d5dSMike Smith 	    /*
137915e32d5dSMike Smith 	     * Re-wake the system.
138015e32d5dSMike Smith 	     *
138115e32d5dSMike Smith 	     * XXX note that a better two-pass approach with a 'veto' pass
138215e32d5dSMike Smith 	     *     followed by a "real thing" pass would be better, but the
138315e32d5dSMike Smith 	     *     current bus interface does not provide for this.
138415e32d5dSMike Smith 	     */
138515e32d5dSMike Smith 	    DEVICE_RESUME(root_bus);
13860ae55423SMike Smith 	    return_ACPI_STATUS(AE_ERROR);
138715e32d5dSMike Smith 	}
1388b9c780d6SMitsuru IWASAKI 
1389b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(state))) {
1390b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1391b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
1392b9c780d6SMitsuru IWASAKI 	    break;
1393b9c780d6SMitsuru IWASAKI 	}
1394b9c780d6SMitsuru IWASAKI 
1395ff01efb5SMitsuru IWASAKI 	if (sc->acpi_sleep_delay > 0) {
1396ff01efb5SMitsuru IWASAKI 	    DELAY(sc->acpi_sleep_delay * 1000000);
1397ff01efb5SMitsuru IWASAKI 	}
1398ff01efb5SMitsuru IWASAKI 
13996161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
14006161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
14016161544cSTakanori Watanabe 
1402a1fccb47SMitsuru IWASAKI 	    /* AcpiEnterSleepState() maybe incompleted, unlock here if locked. */
14036fca9360SNate Lawson 	    if (1/*AcpiGbl_AcpiMutexInfo[ACPI_MTX_HARDWARE].OwnerId != ACPI_MUTEX_NOT_ACQUIRED*/) {
14046161544cSTakanori Watanabe 		AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
1405a1fccb47SMitsuru IWASAKI 	    }
14066161544cSTakanori Watanabe 
14076161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1408964679ceSMitsuru IWASAKI 	    if (state == ACPI_STATE_S4) {
1409964679ceSMitsuru IWASAKI 		AcpiEnable();
14106161544cSTakanori Watanabe 	    }
14116161544cSTakanori Watanabe 	} else {
1412b53f2771SMike Smith 	    if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
1413bfae45aaSMike Smith 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
1414c30382dfSMitsuru IWASAKI 		break;
141515e32d5dSMike Smith 	    }
14166161544cSTakanori Watanabe 	}
1417ff741befSTakanori Watanabe 	AcpiLeaveSleepState((UINT8)state);
141815e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
141915e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
142013d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
142115e32d5dSMike Smith 	break;
142215e32d5dSMike Smith 
142315e32d5dSMike Smith     case ACPI_STATE_S5:
142415e32d5dSMike Smith 	/*
142515e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
142615e32d5dSMike Smith 	 * shutdown handlers.
142715e32d5dSMike Smith 	 */
142815e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
142915e32d5dSMike Smith 	break;
143015e32d5dSMike Smith 
143115e32d5dSMike Smith     default:
143215e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
143315e32d5dSMike Smith 	break;
143415e32d5dSMike Smith     }
1435ece50487SMitsuru IWASAKI 
1436ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1437ece50487SMitsuru IWASAKI 	timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1438ece50487SMitsuru IWASAKI 
14390ae55423SMike Smith     return_ACPI_STATUS(status);
144015e32d5dSMike Smith }
144115e32d5dSMike Smith 
144215e32d5dSMike Smith /*
144315e32d5dSMike Smith  * Enable/Disable ACPI
144415e32d5dSMike Smith  */
144515e32d5dSMike Smith ACPI_STATUS
144615e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
144715e32d5dSMike Smith {
144815e32d5dSMike Smith     ACPI_STATUS	status;
144915e32d5dSMike Smith     u_int32_t	flags;
145015e32d5dSMike Smith 
1451b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1452cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
14530ae55423SMike Smith 
145415e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
145515e32d5dSMike Smith             ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
145615e32d5dSMike Smith     if (!sc->acpi_enabled) {
145715e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
145815e32d5dSMike Smith     } else {
145915e32d5dSMike Smith 	status = AE_OK;
146015e32d5dSMike Smith     }
146115e32d5dSMike Smith     if (status == AE_OK)
146215e32d5dSMike Smith 	sc->acpi_enabled = 1;
14630ae55423SMike Smith     return_ACPI_STATUS(status);
146415e32d5dSMike Smith }
146515e32d5dSMike Smith 
146615e32d5dSMike Smith ACPI_STATUS
146715e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
146815e32d5dSMike Smith {
146915e32d5dSMike Smith     ACPI_STATUS	status;
147015e32d5dSMike Smith 
1471b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1472cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
14730ae55423SMike Smith 
147415e32d5dSMike Smith     if (sc->acpi_enabled) {
147515e32d5dSMike Smith 	status = AcpiDisable();
147615e32d5dSMike Smith     } else {
147715e32d5dSMike Smith 	status = AE_OK;
147815e32d5dSMike Smith     }
147915e32d5dSMike Smith     if (status == AE_OK)
148015e32d5dSMike Smith 	sc->acpi_enabled = 0;
14810ae55423SMike Smith     return_ACPI_STATUS(status);
148215e32d5dSMike Smith }
148315e32d5dSMike Smith 
148415e32d5dSMike Smith /*
148515e32d5dSMike Smith  * ACPI Event Handlers
148615e32d5dSMike Smith  */
148715e32d5dSMike Smith 
148815e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
148915e32d5dSMike Smith 
149015e32d5dSMike Smith static void
149115e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
149215e32d5dSMike Smith {
1493fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1494b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
149515e32d5dSMike Smith 
1496cb9b0d80SMike Smith     ACPI_LOCK;
1497a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
149815e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
1499cb9b0d80SMike Smith     ACPI_UNLOCK;
15000ae55423SMike Smith     return_VOID;
150115e32d5dSMike Smith }
150215e32d5dSMike Smith 
150315e32d5dSMike Smith static void
150415e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
150515e32d5dSMike Smith {
1506fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1507b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
15080ae55423SMike Smith 
150915e32d5dSMike Smith     /* Well, what to do? :-) */
15100ae55423SMike Smith 
1511cb9b0d80SMike Smith     ACPI_LOCK;
1512cb9b0d80SMike Smith     ACPI_UNLOCK;
1513cb9b0d80SMike Smith 
15140ae55423SMike Smith     return_VOID;
151515e32d5dSMike Smith }
151615e32d5dSMike Smith 
151715e32d5dSMike Smith /*
151815e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
151915e32d5dSMike Smith  */
152015e32d5dSMike Smith UINT32
152115e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context)
152215e32d5dSMike Smith {
152315e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
152415e32d5dSMike Smith 
1525b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15260ae55423SMike Smith 
152715e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
15280ae55423SMike Smith 
1529b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
153015e32d5dSMike Smith }
153115e32d5dSMike Smith 
153215e32d5dSMike Smith UINT32
153315e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context)
153415e32d5dSMike Smith {
153515e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
153615e32d5dSMike Smith 
1537b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15380ae55423SMike Smith 
153915e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
15400ae55423SMike Smith 
1541b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
154215e32d5dSMike Smith }
154315e32d5dSMike Smith 
154415e32d5dSMike Smith UINT32
154515e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context)
154615e32d5dSMike Smith {
154715e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
154815e32d5dSMike Smith 
1549b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15500ae55423SMike Smith 
155115e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
15520ae55423SMike Smith 
1553b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
155415e32d5dSMike Smith }
155515e32d5dSMike Smith 
155615e32d5dSMike Smith UINT32
155715e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context)
155815e32d5dSMike Smith {
155915e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
156015e32d5dSMike Smith 
1561b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15620ae55423SMike Smith 
156315e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
15640ae55423SMike Smith 
1565b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
156615e32d5dSMike Smith }
156715e32d5dSMike Smith 
156815e32d5dSMike Smith /*
156915e32d5dSMike Smith  * XXX This is kinda ugly, and should not be here.
157015e32d5dSMike Smith  */
157115e32d5dSMike Smith struct acpi_staticbuf {
157215e32d5dSMike Smith     ACPI_BUFFER	buffer;
157315e32d5dSMike Smith     char	data[512];
157415e32d5dSMike Smith };
157515e32d5dSMike Smith 
157615e32d5dSMike Smith char *
157715e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
157815e32d5dSMike Smith {
157915e32d5dSMike Smith     static struct acpi_staticbuf	buf;
158015e32d5dSMike Smith 
1581cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1582cb9b0d80SMike Smith 
158315e32d5dSMike Smith     buf.buffer.Length = 512;
158415e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
158515e32d5dSMike Smith 
1586b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
158715e32d5dSMike Smith 	return(buf.buffer.Pointer);
158815e32d5dSMike Smith     return("(unknown path)");
158915e32d5dSMike Smith }
159015e32d5dSMike Smith 
159115e32d5dSMike Smith /*
159215e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
159315e32d5dSMike Smith  * parts of the namespace.
159415e32d5dSMike Smith  */
159515e32d5dSMike Smith int
159615e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
159715e32d5dSMike Smith {
15983c600cfbSJohn Baldwin     char	*cp, *env, *np;
159915e32d5dSMike Smith     int		len;
160015e32d5dSMike Smith 
160115e32d5dSMike Smith     np = acpi_name(handle);
160215e32d5dSMike Smith     if (*np == '\\')
160315e32d5dSMike Smith 	np++;
16043c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
160515e32d5dSMike Smith 	return(0);
160615e32d5dSMike Smith 
160715e32d5dSMike Smith     /* scan the avoid list checking for a match */
16083c600cfbSJohn Baldwin     cp = env;
160915e32d5dSMike Smith     for (;;) {
161015e32d5dSMike Smith 	while ((*cp != 0) && isspace(*cp))
161115e32d5dSMike Smith 	    cp++;
161215e32d5dSMike Smith 	if (*cp == 0)
161315e32d5dSMike Smith 	    break;
161415e32d5dSMike Smith 	len = 0;
161515e32d5dSMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
161615e32d5dSMike Smith 	    len++;
1617d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
16183c600cfbSJohn Baldwin 	    freeenv(env);
16190ae55423SMike Smith 	    return(1);
1620d786139cSMaxime Henrion 	}
16210ae55423SMike Smith 	cp += len;
16220ae55423SMike Smith     }
16233c600cfbSJohn Baldwin     freeenv(env);
16240ae55423SMike Smith     return(0);
16250ae55423SMike Smith }
16260ae55423SMike Smith 
16270ae55423SMike Smith /*
16280ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
16290ae55423SMike Smith  */
16300ae55423SMike Smith int
16310ae55423SMike Smith acpi_disabled(char *subsys)
16320ae55423SMike Smith {
163378689b15SMaxime Henrion     char	*cp, *env;
16340ae55423SMike Smith     int		len;
16350ae55423SMike Smith 
163678689b15SMaxime Henrion     if ((env = getenv("debug.acpi.disable")) == NULL)
16370ae55423SMike Smith 	return(0);
163878689b15SMaxime Henrion     if (!strcmp(env, "all")) {
163978689b15SMaxime Henrion 	freeenv(env);
16400ae55423SMike Smith 	return(1);
1641d786139cSMaxime Henrion     }
16420ae55423SMike Smith 
16430ae55423SMike Smith     /* scan the disable list checking for a match */
164478689b15SMaxime Henrion     cp = env;
16450ae55423SMike Smith     for (;;) {
16460ae55423SMike Smith 	while ((*cp != 0) && isspace(*cp))
16470ae55423SMike Smith 	    cp++;
16480ae55423SMike Smith 	if (*cp == 0)
16490ae55423SMike Smith 	    break;
16500ae55423SMike Smith 	len = 0;
16510ae55423SMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
16520ae55423SMike Smith 	    len++;
1653d786139cSMaxime Henrion 	if (!strncmp(cp, subsys, len)) {
165478689b15SMaxime Henrion 	    freeenv(env);
165515e32d5dSMike Smith 	    return(1);
1656d786139cSMaxime Henrion 	}
165715e32d5dSMike Smith 	cp += len;
165815e32d5dSMike Smith     }
165978689b15SMaxime Henrion     freeenv(env);
166015e32d5dSMike Smith     return(0);
166115e32d5dSMike Smith }
166215e32d5dSMike Smith 
166315e32d5dSMike Smith /*
1664a1fccb47SMitsuru IWASAKI  * Device wake capability enable/disable.
1665a1fccb47SMitsuru IWASAKI  */
1666a1fccb47SMitsuru IWASAKI void
1667a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable)
1668a1fccb47SMitsuru IWASAKI {
1669a1fccb47SMitsuru IWASAKI     ACPI_OBJECT_LIST		ArgList;
1670a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			Arg;
1671a1fccb47SMitsuru IWASAKI 
1672a1fccb47SMitsuru IWASAKI     /*
1673a1fccb47SMitsuru IWASAKI      * TBD: All Power Resources referenced by elements 2 through N
1674a1fccb47SMitsuru IWASAKI      *      of the _PRW object are put into the ON state.
1675a1fccb47SMitsuru IWASAKI      */
1676a1fccb47SMitsuru IWASAKI 
1677a1fccb47SMitsuru IWASAKI     /*
1678a1fccb47SMitsuru IWASAKI      * enable/disable device wake function.
1679a1fccb47SMitsuru IWASAKI      */
1680a1fccb47SMitsuru IWASAKI 
1681a1fccb47SMitsuru IWASAKI     ArgList.Count = 1;
1682a1fccb47SMitsuru IWASAKI     ArgList.Pointer = &Arg;
1683a1fccb47SMitsuru IWASAKI 
1684a1fccb47SMitsuru IWASAKI     Arg.Type = ACPI_TYPE_INTEGER;
1685a1fccb47SMitsuru IWASAKI     Arg.Integer.Value = enable;
1686a1fccb47SMitsuru IWASAKI 
1687a1fccb47SMitsuru IWASAKI     (void)AcpiEvaluateObject(h, "_PSW", &ArgList, NULL);
1688a1fccb47SMitsuru IWASAKI }
1689a1fccb47SMitsuru IWASAKI 
1690a1fccb47SMitsuru IWASAKI void
1691a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_event(ACPI_HANDLE h)
1692a1fccb47SMitsuru IWASAKI {
1693a1fccb47SMitsuru IWASAKI     struct acpi_softc		*sc;
1694a1fccb47SMitsuru IWASAKI     ACPI_STATUS			status;
1695a1fccb47SMitsuru IWASAKI     ACPI_BUFFER			prw_buffer;
1696a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			*res;
1697a1fccb47SMitsuru IWASAKI 
1698a1fccb47SMitsuru IWASAKI     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1699a1fccb47SMitsuru IWASAKI 
1700a1fccb47SMitsuru IWASAKI     if ((sc = devclass_get_softc(acpi_devclass, 0)) == NULL) {
1701a1fccb47SMitsuru IWASAKI 	return;
1702a1fccb47SMitsuru IWASAKI     }
1703a1fccb47SMitsuru IWASAKI 
1704a1fccb47SMitsuru IWASAKI     /*
1705a1fccb47SMitsuru IWASAKI      * _PRW object is only required for devices that have the ability
1706a1fccb47SMitsuru IWASAKI      * to wake the system from a system sleeping state.
1707a1fccb47SMitsuru IWASAKI      */
1708a1fccb47SMitsuru IWASAKI     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
1709a1fccb47SMitsuru IWASAKI     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
1710a1fccb47SMitsuru IWASAKI     if (ACPI_FAILURE(status)) {
1711a1fccb47SMitsuru IWASAKI 	return;
1712a1fccb47SMitsuru IWASAKI     }
1713a1fccb47SMitsuru IWASAKI 
1714a1fccb47SMitsuru IWASAKI     res = (ACPI_OBJECT *)prw_buffer.Pointer;
1715a1fccb47SMitsuru IWASAKI     if (res == NULL) {
1716a1fccb47SMitsuru IWASAKI 	return;
1717a1fccb47SMitsuru IWASAKI     }
1718a1fccb47SMitsuru IWASAKI 
1719a1fccb47SMitsuru IWASAKI     if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) {
1720a1fccb47SMitsuru IWASAKI 	goto out;
1721a1fccb47SMitsuru IWASAKI     }
1722a1fccb47SMitsuru IWASAKI 
1723a1fccb47SMitsuru IWASAKI     /*
1724a1fccb47SMitsuru IWASAKI      * The element 1 of the _PRW object:
1725a1fccb47SMitsuru IWASAKI      * The lowest power system sleeping state that can be entered
1726a1fccb47SMitsuru IWASAKI      * while still providing wake functionality.
1727a1fccb47SMitsuru IWASAKI      * The sleeping state being entered must be greater or equal to
1728a1fccb47SMitsuru IWASAKI      * the power state declared in element 1 of the _PRW object.
1729a1fccb47SMitsuru IWASAKI      */
1730a1fccb47SMitsuru IWASAKI     if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER) {
1731a1fccb47SMitsuru IWASAKI 	goto out;
1732a1fccb47SMitsuru IWASAKI     }
1733a1fccb47SMitsuru IWASAKI 
1734a1fccb47SMitsuru IWASAKI     if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value) {
1735a1fccb47SMitsuru IWASAKI 	goto out;
1736a1fccb47SMitsuru IWASAKI     }
1737a1fccb47SMitsuru IWASAKI 
1738a1fccb47SMitsuru IWASAKI     /*
1739a1fccb47SMitsuru IWASAKI      * The element 0 of the _PRW object:
1740a1fccb47SMitsuru IWASAKI      */
1741a1fccb47SMitsuru IWASAKI     switch(res->Package.Elements[0].Type) {
1742a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_INTEGER:
1743a1fccb47SMitsuru IWASAKI 	/*
1744a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is numeric, then this
1745a1fccb47SMitsuru IWASAKI 	 * _PRW package element is the bit index in the GPEx_EN, in the
1746a1fccb47SMitsuru IWASAKI 	 * GPE blocks described in the FADT, of the enable bit that is
1747a1fccb47SMitsuru IWASAKI 	 * enabled for the wake event.
1748a1fccb47SMitsuru IWASAKI 	 */
1749a1fccb47SMitsuru IWASAKI 
17506fca9360SNate Lawson 	status = AcpiEnableGpe(NULL, res->Package.Elements[0].Integer.Value,
17516fca9360SNate Lawson 			       ACPI_EVENT_WAKE_ENABLE);
1752a1fccb47SMitsuru IWASAKI 	if (ACPI_FAILURE(status))
1753a1fccb47SMitsuru IWASAKI             printf("%s: EnableEvent Failed\n", __func__);
1754a1fccb47SMitsuru IWASAKI 	break;
1755a1fccb47SMitsuru IWASAKI 
1756a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_PACKAGE:
1757a1fccb47SMitsuru IWASAKI 	/* XXX TBD */
1758a1fccb47SMitsuru IWASAKI 
1759a1fccb47SMitsuru IWASAKI 	/*
1760a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is a package, then this
1761a1fccb47SMitsuru IWASAKI 	 * _PRW package element is itself a package containing two
1762a1fccb47SMitsuru IWASAKI 	 * elements. The first is an object reference to the GPE Block
1763a1fccb47SMitsuru IWASAKI 	 * device that contains the GPE that will be triggered by the wake
1764a1fccb47SMitsuru IWASAKI 	 * event. The second element is numeric and it contains the bit
1765a1fccb47SMitsuru IWASAKI 	 * index in the GPEx_EN, in the GPE Block referenced by the
1766a1fccb47SMitsuru IWASAKI 	 * first element in the package, of the enable bit that is enabled for
1767a1fccb47SMitsuru IWASAKI 	 * the wake event.
1768a1fccb47SMitsuru IWASAKI 	 * For example, if this field is a package then it is of the form:
1769a1fccb47SMitsuru IWASAKI 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
1770a1fccb47SMitsuru IWASAKI 	 */
1771a1fccb47SMitsuru IWASAKI 
1772a1fccb47SMitsuru IWASAKI 	break;
1773a1fccb47SMitsuru IWASAKI 
1774a1fccb47SMitsuru IWASAKI     default:
1775a1fccb47SMitsuru IWASAKI 	break;
1776a1fccb47SMitsuru IWASAKI     }
1777a1fccb47SMitsuru IWASAKI 
1778a1fccb47SMitsuru IWASAKI out:
1779a1fccb47SMitsuru IWASAKI     if (prw_buffer.Pointer != NULL)
1780a1fccb47SMitsuru IWASAKI 	AcpiOsFree(prw_buffer.Pointer);
1781a1fccb47SMitsuru IWASAKI     return;
1782a1fccb47SMitsuru IWASAKI }
1783a1fccb47SMitsuru IWASAKI 
1784a1fccb47SMitsuru IWASAKI /*
178515e32d5dSMike Smith  * Control interface.
178615e32d5dSMike Smith  *
17870ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
17880ae55423SMike Smith  * drivers wanting to be accessible via /dev/acpi should use the register/deregister
17890ae55423SMike Smith  * interface to make their handlers visible.
179015e32d5dSMike Smith  */
17910ae55423SMike Smith struct acpi_ioctl_hook
17920ae55423SMike Smith {
17930ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook)	link;
17940ae55423SMike Smith     u_long				cmd;
17950ae55423SMike Smith     int					(* fn)(u_long cmd, caddr_t addr, void *arg);
17960ae55423SMike Smith     void				*arg;
17970ae55423SMike Smith };
17980ae55423SMike Smith 
17990ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
18000ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
18010ae55423SMike Smith 
18020ae55423SMike Smith /*
18030ae55423SMike Smith  * Register an ioctl handler.
18040ae55423SMike Smith  */
18050ae55423SMike Smith int
18060ae55423SMike Smith acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg)
18070ae55423SMike Smith {
18080ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
18090ae55423SMike Smith 
18100ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
18110ae55423SMike Smith 	return(ENOMEM);
18120ae55423SMike Smith     hp->cmd = cmd;
18130ae55423SMike Smith     hp->fn = fn;
18140ae55423SMike Smith     hp->arg = arg;
18150ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
18160ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
18170ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
18180ae55423SMike Smith     }
18190ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
18200ae55423SMike Smith     return(0);
18210ae55423SMike Smith }
18220ae55423SMike Smith 
18230ae55423SMike Smith /*
18240ae55423SMike Smith  * Deregister an ioctl handler.
18250ae55423SMike Smith  */
18260ae55423SMike Smith void
18270ae55423SMike Smith acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg))
18280ae55423SMike Smith {
18290ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
18300ae55423SMike Smith 
18310ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
18320ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
18330ae55423SMike Smith 	    break;
18340ae55423SMike Smith 
18350ae55423SMike Smith     if (hp != NULL) {
18360ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
18370ae55423SMike Smith 	free(hp, M_ACPIDEV);
18380ae55423SMike Smith     }
18390ae55423SMike Smith }
18400ae55423SMike Smith 
184115e32d5dSMike Smith static int
18426b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td)
184315e32d5dSMike Smith {
184415e32d5dSMike Smith     return(0);
184515e32d5dSMike Smith }
184615e32d5dSMike Smith 
184715e32d5dSMike Smith static int
18486b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td)
184915e32d5dSMike Smith {
185015e32d5dSMike Smith     return(0);
185115e32d5dSMike Smith }
185215e32d5dSMike Smith 
185315e32d5dSMike Smith static int
18546b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
185515e32d5dSMike Smith {
185615e32d5dSMike Smith     struct acpi_softc		*sc;
18570ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
18580ae55423SMike Smith     int				error, xerror, state;
1859fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
186015e32d5dSMike Smith 
1861cb9b0d80SMike Smith     ACPI_LOCK;
1862cb9b0d80SMike Smith 
186315e32d5dSMike Smith     error = state = 0;
186415e32d5dSMike Smith     sc = dev->si_drv1;
186515e32d5dSMike Smith 
18660ae55423SMike Smith     /*
18670ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
18680ae55423SMike Smith      */
18690ae55423SMike Smith     if (acpi_ioctl_hooks_initted) {
18700ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
18710ae55423SMike Smith 	    if (hp->cmd == cmd) {
18720ae55423SMike Smith 		xerror = hp->fn(cmd, addr, hp->arg);
18730ae55423SMike Smith 		if (xerror != 0)
18740ae55423SMike Smith 		    error = xerror;
1875917d44c8SMitsuru IWASAKI 		goto out;
18760ae55423SMike Smith 	    }
18770ae55423SMike Smith 	}
18780ae55423SMike Smith     }
18790ae55423SMike Smith 
18800ae55423SMike Smith     /*
1881a89fcf28STakanori Watanabe      * Core ioctls are  not permitted for non-writable user.
1882a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
1883a89fcf28STakanori Watanabe      * Not changing system behavior.
1884a89fcf28STakanori Watanabe      */
1885a89fcf28STakanori Watanabe     if(!(flag & FWRITE)){
1886a89fcf28STakanori Watanabe 	    return EPERM;
1887a89fcf28STakanori Watanabe     }
1888a89fcf28STakanori Watanabe 
1889a89fcf28STakanori Watanabe     /*
18900ae55423SMike Smith      * Core system ioctls.
18910ae55423SMike Smith      */
189215e32d5dSMike Smith     switch (cmd) {
189315e32d5dSMike Smith     case ACPIIO_ENABLE:
18940ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
189515e32d5dSMike Smith 	    error = ENXIO;
189615e32d5dSMike Smith 	break;
189715e32d5dSMike Smith 
189815e32d5dSMike Smith     case ACPIIO_DISABLE:
18990ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
190015e32d5dSMike Smith 	    error = ENXIO;
190115e32d5dSMike Smith 	break;
190215e32d5dSMike Smith 
190315e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
190415e32d5dSMike Smith 	if (!sc->acpi_enabled) {
190515e32d5dSMike Smith 	    error = ENXIO;
190615e32d5dSMike Smith 	    break;
190715e32d5dSMike Smith 	}
190815e32d5dSMike Smith 	state = *(int *)addr;
1909a5d1879bSMitsuru IWASAKI 	if (state >= ACPI_STATE_S0  && state <= ACPI_S_STATES_MAX) {
191015e32d5dSMike Smith 	    acpi_SetSleepState(sc, state);
191115e32d5dSMike Smith 	} else {
191215e32d5dSMike Smith 	    error = EINVAL;
191315e32d5dSMike Smith 	}
191415e32d5dSMike Smith 	break;
191515e32d5dSMike Smith 
191615e32d5dSMike Smith     default:
19170ae55423SMike Smith 	if (error == 0)
191815e32d5dSMike Smith 	    error = EINVAL;
191915e32d5dSMike Smith 	break;
192015e32d5dSMike Smith     }
1921917d44c8SMitsuru IWASAKI 
1922917d44c8SMitsuru IWASAKI out:
1923cb9b0d80SMike Smith     ACPI_UNLOCK;
192415e32d5dSMike Smith     return(error);
192515e32d5dSMike Smith }
192615e32d5dSMike Smith 
19271d073b1dSJohn Baldwin static int
1928d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
1929d75de536SMitsuru IWASAKI {
1930d75de536SMitsuru IWASAKI     char sleep_state[4];
1931d75de536SMitsuru IWASAKI     char buf[16];
1932d75de536SMitsuru IWASAKI     int error;
1933d75de536SMitsuru IWASAKI     UINT8 state, TypeA, TypeB;
1934d75de536SMitsuru IWASAKI 
1935d75de536SMitsuru IWASAKI     buf[0] = '\0';
1936d75de536SMitsuru IWASAKI     for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX+1; state++) {
1937d75de536SMitsuru IWASAKI 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
1938d75de536SMitsuru IWASAKI 	    sprintf(sleep_state, "S%d ", state);
1939d75de536SMitsuru IWASAKI 	    strcat(buf, sleep_state);
1940d75de536SMitsuru IWASAKI 	}
1941d75de536SMitsuru IWASAKI     }
1942d75de536SMitsuru IWASAKI     error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1943d75de536SMitsuru IWASAKI     return(error);
1944d75de536SMitsuru IWASAKI }
1945d75de536SMitsuru IWASAKI 
1946d75de536SMitsuru IWASAKI static int
19471d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
19481d073b1dSJohn Baldwin {
19491d073b1dSJohn Baldwin     char sleep_state[10];
19501d073b1dSJohn Baldwin     int error;
19511d073b1dSJohn Baldwin     u_int new_state, old_state;
19521d073b1dSJohn Baldwin 
19531d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
1954b8670bedSMitsuru IWASAKI     if (old_state > ACPI_S_STATES_MAX+1) {
19551d073b1dSJohn Baldwin 	strcpy(sleep_state, "unknown");
1956cb9b0d80SMike Smith     } else {
1957b8670bedSMitsuru IWASAKI 	bzero(sleep_state, sizeof(sleep_state));
19581d073b1dSJohn Baldwin 	strncpy(sleep_state, sleep_state_names[old_state],
19591d073b1dSJohn Baldwin 		sizeof(sleep_state_names[old_state]));
1960cb9b0d80SMike Smith     }
19611d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
19621d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
1963b8670bedSMitsuru IWASAKI 	for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX+1; new_state++) {
19641d073b1dSJohn Baldwin 	    if (strncmp(sleep_state, sleep_state_names[new_state],
19651d073b1dSJohn Baldwin 			sizeof(sleep_state)) == 0)
19661d073b1dSJohn Baldwin 		break;
1967cb9b0d80SMike Smith 	}
1968931a10c9SMitsuru IWASAKI 	if (new_state <= ACPI_S_STATES_MAX+1) {
1969931a10c9SMitsuru IWASAKI 	    if (new_state != old_state) {
19701d073b1dSJohn Baldwin 		*(u_int *)oidp->oid_arg1 = new_state;
1971931a10c9SMitsuru IWASAKI 	    }
1972cb9b0d80SMike Smith 	} else {
19731d073b1dSJohn Baldwin 	    error = EINVAL;
19741d073b1dSJohn Baldwin 	}
1975cb9b0d80SMike Smith     }
19761d073b1dSJohn Baldwin     return(error);
19771d073b1dSJohn Baldwin }
19781d073b1dSJohn Baldwin 
197915e32d5dSMike Smith #ifdef ACPI_DEBUG
19800ae55423SMike Smith /*
19810ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
19820ae55423SMike Smith  *
19830ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
19840ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
19850ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
19860ae55423SMike Smith  * prefixing the bit name with !.
19870ae55423SMike Smith  */
198815e32d5dSMike Smith struct debugtag
198915e32d5dSMike Smith {
199015e32d5dSMike Smith     char	*name;
199115e32d5dSMike Smith     UINT32	value;
199215e32d5dSMike Smith };
199315e32d5dSMike Smith 
199415e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
1995c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
1996c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
1997c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
1998c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
1999c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
2000c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
2001c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
2002c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
2003c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
2004d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
20054c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
2006d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
20074c1cdee6SMike Smith 
2008cb9b0d80SMike Smith     {"ACPI_BUS",		ACPI_BUS},
20094c1cdee6SMike Smith     {"ACPI_SYSTEM",		ACPI_SYSTEM},
2010cb9b0d80SMike Smith     {"ACPI_POWER",		ACPI_POWER},
2011cb9b0d80SMike Smith     {"ACPI_EC", 		ACPI_EC},
2012c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
2013c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
2014c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
20154c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
2016cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
20174c1cdee6SMike Smith     {"ACPI_FAN",		ACPI_FAN},
20184c1cdee6SMike Smith 
2019b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
2020c5ba0be4SMike Smith     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
202115e32d5dSMike Smith     {NULL, 0}
202215e32d5dSMike Smith };
202315e32d5dSMike Smith 
202415e32d5dSMike Smith static struct debugtag dbg_level[] = {
20254c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
20269501b603SJohn Baldwin     {"ACPI_LV_WARN",		ACPI_LV_WARN},
20279501b603SJohn Baldwin     {"ACPI_LV_INIT",		ACPI_LV_INIT},
20284c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
20299501b603SJohn Baldwin     {"ACPI_LV_INFO",		ACPI_LV_INFO},
20304c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
2031d62ab2f4SMitsuru IWASAKI 
2032d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
20334c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
20344c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
2035d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
20364c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
20374c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
20384c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
20394c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
20404c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
20414c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
20424c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
20434c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
20444c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
20454c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
20469501b603SJohn Baldwin     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
2047d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
2048d62ab2f4SMitsuru IWASAKI 
2049d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
2050d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
2051d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
2052d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
2053d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
20544c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
2055d62ab2f4SMitsuru IWASAKI 
2056d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2057d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
2058d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
2059d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
2060d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
2061d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
2062d62ab2f4SMitsuru IWASAKI 
2063d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
206498479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
206598479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
206698479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
206798479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
206898479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
206915e32d5dSMike Smith     {NULL, 0}
207015e32d5dSMike Smith };
207115e32d5dSMike Smith 
207215e32d5dSMike Smith static void
207315e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
207415e32d5dSMike Smith {
207515e32d5dSMike Smith     char	*ep;
207615e32d5dSMike Smith     int		i, l;
20770ae55423SMike Smith     int		set;
207815e32d5dSMike Smith 
207915e32d5dSMike Smith     while (*cp) {
208015e32d5dSMike Smith 	if (isspace(*cp)) {
208115e32d5dSMike Smith 	    cp++;
208215e32d5dSMike Smith 	    continue;
208315e32d5dSMike Smith 	}
208415e32d5dSMike Smith 	ep = cp;
208515e32d5dSMike Smith 	while (*ep && !isspace(*ep))
208615e32d5dSMike Smith 	    ep++;
20870ae55423SMike Smith 	if (*cp == '!') {
20880ae55423SMike Smith 	    set = 0;
20890ae55423SMike Smith 	    cp++;
20900ae55423SMike Smith 	    if (cp == ep)
20910ae55423SMike Smith 		continue;
20920ae55423SMike Smith 	} else {
20930ae55423SMike Smith 	    set = 1;
20940ae55423SMike Smith 	}
209515e32d5dSMike Smith 	l = ep - cp;
209615e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
209715e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
20980ae55423SMike Smith 		if (set) {
209915e32d5dSMike Smith 		    *flag |= tag[i].value;
21000ae55423SMike Smith 		} else {
21010ae55423SMike Smith 		    *flag &= ~tag[i].value;
21020ae55423SMike Smith 		}
210315e32d5dSMike Smith 		printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
210415e32d5dSMike Smith 	    }
210515e32d5dSMike Smith 	}
210615e32d5dSMike Smith 	cp = ep;
210715e32d5dSMike Smith     }
210815e32d5dSMike Smith }
210915e32d5dSMike Smith 
211015e32d5dSMike Smith static void
21112a4ac806SMike Smith acpi_set_debugging(void *junk)
211215e32d5dSMike Smith {
211394aae282SMike Barcroft     char	*cp;
211415e32d5dSMike Smith 
211587b45ed5SMitsuru IWASAKI     if (!cold)
211687b45ed5SMitsuru IWASAKI 	return;
211787b45ed5SMitsuru IWASAKI 
21180ae55423SMike Smith     AcpiDbgLayer = 0;
21190ae55423SMike Smith     AcpiDbgLevel = 0;
212094aae282SMike Barcroft     if ((cp = getenv("debug.acpi.layer")) != NULL) {
212115e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
212294aae282SMike Barcroft 	freeenv(cp);
212394aae282SMike Barcroft     }
212494aae282SMike Barcroft     if ((cp = getenv("debug.acpi.level")) != NULL) {
212515e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
212694aae282SMike Barcroft 	freeenv(cp);
212794aae282SMike Barcroft     }
21280ae55423SMike Smith 
21290ae55423SMike Smith     printf("ACPI debug layer 0x%x  debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel);
213015e32d5dSMike Smith }
21312a4ac806SMike Smith SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL);
213215e32d5dSMike Smith #endif
2133f9390180SMitsuru IWASAKI 
2134f9390180SMitsuru IWASAKI static int
2135f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
2136f9390180SMitsuru IWASAKI {
2137f9390180SMitsuru IWASAKI 	int	state, acpi_state;
2138f9390180SMitsuru IWASAKI 	int	error;
2139f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
2140f9390180SMitsuru IWASAKI 	va_list	ap;
2141f9390180SMitsuru IWASAKI 
2142f9390180SMitsuru IWASAKI 	error = 0;
2143f9390180SMitsuru IWASAKI 	switch (cmd) {
2144f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
2145f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
2146f9390180SMitsuru IWASAKI 		if (sc == NULL) {
2147f9390180SMitsuru IWASAKI 			error = EINVAL;
2148f9390180SMitsuru IWASAKI 			goto out;
2149f9390180SMitsuru IWASAKI 		}
2150f9390180SMitsuru IWASAKI 
2151f9390180SMitsuru IWASAKI 		va_start(ap, arg);
2152f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
2153f9390180SMitsuru IWASAKI 		va_end(ap);
2154f9390180SMitsuru IWASAKI 
2155f9390180SMitsuru IWASAKI 		switch (state) {
2156f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
2157f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
2158f9390180SMitsuru IWASAKI 			break;
2159f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
2160f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
2161f9390180SMitsuru IWASAKI 			break;
2162f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
2163f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
2164f9390180SMitsuru IWASAKI 			break;
2165f9390180SMitsuru IWASAKI 		default:
2166f9390180SMitsuru IWASAKI 			error = EINVAL;
2167f9390180SMitsuru IWASAKI 			goto out;
2168f9390180SMitsuru IWASAKI 		}
2169f9390180SMitsuru IWASAKI 
2170f9390180SMitsuru IWASAKI 		acpi_SetSleepState(sc, acpi_state);
2171f9390180SMitsuru IWASAKI 		break;
2172f9390180SMitsuru IWASAKI 
2173f9390180SMitsuru IWASAKI 	default:
2174f9390180SMitsuru IWASAKI 		error = EINVAL;
2175f9390180SMitsuru IWASAKI 		goto out;
2176f9390180SMitsuru IWASAKI 	}
2177f9390180SMitsuru IWASAKI 
2178f9390180SMitsuru IWASAKI out:
2179f9390180SMitsuru IWASAKI 	return (error);
2180f9390180SMitsuru IWASAKI }
2181f9390180SMitsuru IWASAKI 
2182f9390180SMitsuru IWASAKI static void
2183f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
2184f9390180SMitsuru IWASAKI {
21856c407052SMitsuru IWASAKI 
218687b45ed5SMitsuru IWASAKI     if (!cold)
218787b45ed5SMitsuru IWASAKI 	return;
218887b45ed5SMitsuru IWASAKI 
21898a9bc9c0SJohn Baldwin     if (resource_disabled("acpi", 0))
21906c407052SMitsuru IWASAKI 		return;
2191f9390180SMitsuru IWASAKI 
2192f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
2193f9390180SMitsuru IWASAKI }
2194f9390180SMitsuru IWASAKI 
2195f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
2196f9390180SMitsuru IWASAKI 
2197