xref: /freebsd/sys/dev/acpica/acpi.c (revision da14ac9fce9b1466e7eec49c3c30edc8a2ce03de)
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>
36f34fa851SJohn Baldwin #include <sys/lock.h>
3715e32d5dSMike Smith #include <sys/malloc.h>
38b2c9c0daSMitsuru IWASAKI #include <sys/mutex.h>
3915e32d5dSMike Smith #include <sys/bus.h>
4015e32d5dSMike Smith #include <sys/conf.h>
4115e32d5dSMike Smith #include <sys/ioccom.h>
4215e32d5dSMike Smith #include <sys/reboot.h>
4315e32d5dSMike Smith #include <sys/sysctl.h>
4415e32d5dSMike Smith #include <sys/ctype.h>
451611ea87SMitsuru IWASAKI #include <sys/linker.h>
46f9390180SMitsuru IWASAKI #include <sys/power.h>
4715e32d5dSMike Smith 
4815e32d5dSMike Smith #include <machine/clock.h>
4915e32d5dSMike Smith #include <machine/resource.h>
5015e32d5dSMike Smith 
51bc0f2195SMike Smith #include <isa/isavar.h>
52bc0f2195SMike Smith 
5315e32d5dSMike Smith #include "acpi.h"
5415e32d5dSMike Smith 
551611ea87SMitsuru IWASAKI #include <dev/acpica/acpica_support.h>
561611ea87SMitsuru IWASAKI 
5715e32d5dSMike Smith #include <dev/acpica/acpivar.h>
5815e32d5dSMike Smith #include <dev/acpica/acpiio.h>
5915e32d5dSMike Smith 
6015e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
6115e32d5dSMike Smith 
6215e32d5dSMike Smith /*
630ae55423SMike Smith  * Hooks for the ACPI CA debugging infrastructure
640ae55423SMike Smith  */
65cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
66b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
670ae55423SMike Smith 
680ae55423SMike Smith /*
6915e32d5dSMike Smith  * Character device
7015e32d5dSMike Smith  */
7115e32d5dSMike Smith 
7215e32d5dSMike Smith static d_open_t		acpiopen;
7315e32d5dSMike Smith static d_close_t	acpiclose;
7415e32d5dSMike Smith static d_ioctl_t	acpiioctl;
7515e32d5dSMike Smith 
7615e32d5dSMike Smith #define CDEV_MAJOR 152
7715e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
7815e32d5dSMike Smith     acpiopen,
7915e32d5dSMike Smith     acpiclose,
8015e32d5dSMike Smith     noread,
8115e32d5dSMike Smith     nowrite,
8215e32d5dSMike Smith     acpiioctl,
8315e32d5dSMike Smith     nopoll,
8415e32d5dSMike Smith     nommap,
8515e32d5dSMike Smith     nostrategy,
8615e32d5dSMike Smith     "acpi",
8715e32d5dSMike Smith     CDEV_MAJOR,
8815e32d5dSMike Smith     nodump,
8915e32d5dSMike Smith     nopsize,
90606e1d68SJohn Baldwin     0
9115e32d5dSMike Smith };
9215e32d5dSMike Smith 
931d073b1dSJohn Baldwin static const char* sleep_state_names[] = {
94b8670bedSMitsuru IWASAKI     "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
951d073b1dSJohn Baldwin 
962a4ac806SMike Smith /* this has to be static, as the softc is gone when we need it */
972a4ac806SMike Smith static int acpi_off_state = ACPI_STATE_S5;
982a4ac806SMike Smith 
99cb9b0d80SMike Smith struct mtx	acpi_mutex;
100cb9b0d80SMike Smith 
1016d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
10215e32d5dSMike Smith static void	acpi_identify(driver_t *driver, device_t parent);
10315e32d5dSMike Smith static int	acpi_probe(device_t dev);
10415e32d5dSMike Smith static int	acpi_attach(device_t dev);
10515e32d5dSMike Smith static device_t	acpi_add_child(device_t bus, int order, const char *name, int unit);
10615e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
10715e32d5dSMike Smith static int	acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result);
10815e32d5dSMike Smith static int	acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value);
10915e32d5dSMike Smith static int	acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start,
11015e32d5dSMike Smith 				  u_long count);
11115e32d5dSMike Smith static int	acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp,
11215e32d5dSMike Smith 				  u_long *countp);
11315e32d5dSMike Smith static struct resource *acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
11415e32d5dSMike Smith 					    u_long start, u_long end, u_long count, u_int flags);
11515e32d5dSMike Smith static int	acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r);
11632d18aa5SMike Smith static u_int32_t acpi_isa_get_logicalid(device_t dev);
117bc0f2195SMike Smith static int	acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids);
11815e32d5dSMike Smith 
11915e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
12015e32d5dSMike Smith static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status);
12115e32d5dSMike Smith 
12215e32d5dSMike Smith static void	acpi_shutdown_pre_sync(void *arg, int howto);
12315e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
12415e32d5dSMike Smith 
12513d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
12613d4f7baSMitsuru IWASAKI 
12715e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
12815e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
1291d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
13015e32d5dSMike Smith 
131f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
132f9390180SMitsuru IWASAKI 
13315e32d5dSMike Smith static device_method_t acpi_methods[] = {
13415e32d5dSMike Smith     /* Device interface */
13515e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
13615e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
13715e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
13815e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
13915e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
14015e32d5dSMike Smith     DEVMETHOD(device_resume,		bus_generic_resume),
14115e32d5dSMike Smith 
14215e32d5dSMike Smith     /* Bus interface */
14315e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
14415e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
14515e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
14615e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
14715e32d5dSMike Smith     DEVMETHOD(bus_set_resource,		acpi_set_resource),
14815e32d5dSMike Smith     DEVMETHOD(bus_get_resource,		acpi_get_resource),
14915e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
15015e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
15115e32d5dSMike Smith     DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
15215e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
15315e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
15415e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
15515e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
15615e32d5dSMike Smith 
157bc0f2195SMike Smith     /* ISA emulation */
158bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
159bc0f2195SMike Smith 
16015e32d5dSMike Smith     {0, 0}
16115e32d5dSMike Smith };
16215e32d5dSMike Smith 
16315e32d5dSMike Smith static driver_t acpi_driver = {
16415e32d5dSMike Smith     "acpi",
16515e32d5dSMike Smith     acpi_methods,
16615e32d5dSMike Smith     sizeof(struct acpi_softc),
16715e32d5dSMike Smith };
16815e32d5dSMike Smith 
1693273b005SMike Smith static devclass_t acpi_devclass;
1706d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
171dde24897SMike Smith MODULE_VERSION(acpi, 100);
17215e32d5dSMike Smith 
17315e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, "");
17415e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, "");
175dde24897SMike Smith static int acpi_ca_version = ACPI_CA_VERSION;
176dd081ed5SMitsuru IWASAKI SYSCTL_INT(_debug, OID_AUTO, acpi_ca_version, CTLFLAG_RD, &acpi_ca_version, 0, "");
17715e32d5dSMike Smith 
17815e32d5dSMike Smith /*
1796d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
1806d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
1816d63101aSMike Smith  * It also cannot be unloaded, since the entire system bus heirarchy hangs off it.
1826d63101aSMike Smith  */
1836d63101aSMike Smith static int
1846d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
1856d63101aSMike Smith {
1866d63101aSMike Smith     switch(event) {
1876d63101aSMike Smith     case MOD_LOAD:
1886d63101aSMike Smith 	if (!cold)
1896d63101aSMike Smith 	    return(EPERM);
1906d63101aSMike Smith 	break;
1916d63101aSMike Smith     case MOD_UNLOAD:
192f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
1936d63101aSMike Smith 	    return(EBUSY);
194f9390180SMitsuru IWASAKI 	break;
1956d63101aSMike Smith     default:
1966d63101aSMike Smith 	break;
1976d63101aSMike Smith     }
1986d63101aSMike Smith     return(0);
1996d63101aSMike Smith }
2006d63101aSMike Smith 
2016d63101aSMike Smith /*
20215e32d5dSMike Smith  * Detect ACPI, perform early initialisation
20315e32d5dSMike Smith  */
20415e32d5dSMike Smith static void
20515e32d5dSMike Smith acpi_identify(driver_t *driver, device_t parent)
20615e32d5dSMike Smith {
20715e32d5dSMike Smith     device_t			child;
20815e32d5dSMike Smith     int				error;
209d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
210d786139cSMaxime Henrion     char			*debugpoint;
21115e32d5dSMike Smith #endif
21215e32d5dSMike Smith 
213b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2140ae55423SMike Smith 
215840f9b53STakanori Watanabe     if(!cold){
216840f9b53STakanori Watanabe 	    printf("Don't load this driver from userland!!\n");
217840f9b53STakanori Watanabe 	    return ;
218840f9b53STakanori Watanabe     }
219840f9b53STakanori Watanabe 
22015e32d5dSMike Smith     /*
22132d18aa5SMike Smith      * Check that we haven't been disabled with a hint.
22232d18aa5SMike Smith      */
22332d18aa5SMike Smith     if (!resource_int_value("acpi", 0, "disabled", &error) &&
22432d18aa5SMike Smith 	(error != 0))
22532d18aa5SMike Smith 	return_VOID;
22632d18aa5SMike Smith 
22732d18aa5SMike Smith     /*
22815e32d5dSMike Smith      * Make sure we're not being doubly invoked.
22915e32d5dSMike Smith      */
23015e32d5dSMike Smith     if (device_find_child(parent, "acpi", 0) != NULL)
2310ae55423SMike Smith 	return_VOID;
23215e32d5dSMike Smith 
233cb9b0d80SMike Smith     /* initialise the ACPI mutex */
2346008862bSJohn Baldwin     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
235cb9b0d80SMike Smith 
23615e32d5dSMike Smith     /*
2372a4ac806SMike Smith      * Start up the ACPI CA subsystem.
23815e32d5dSMike Smith      */
239d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
240d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
241d786139cSMaxime Henrion     if (debugpoint) {
242d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "init"))
24315e32d5dSMike Smith 	    acpi_EnterDebugger();
244d786139cSMaxime Henrion         freeenv(debugpoint);
245d786139cSMaxime Henrion     }
24615e32d5dSMike Smith #endif
247b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
248bfae45aaSMike Smith 	printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
2490ae55423SMike Smith 	return_VOID;
25015e32d5dSMike Smith     }
251d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
252d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
253d786139cSMaxime Henrion     if (debugpoint) {
254d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "tables"))
25515e32d5dSMike Smith 	    acpi_EnterDebugger();
256d786139cSMaxime Henrion         freeenv(debugpoint);
257d786139cSMaxime Henrion     }
25815e32d5dSMike Smith #endif
2591611ea87SMitsuru IWASAKI 
260b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiLoadTables())) {
261bfae45aaSMike Smith 	printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
2620ae55423SMike Smith 	return_VOID;
26315e32d5dSMike Smith     }
26415e32d5dSMike Smith 
26515e32d5dSMike Smith     /*
26615e32d5dSMike Smith      * Attach the actual ACPI device.
26715e32d5dSMike Smith      */
26815e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
26915e32d5dSMike Smith 	    device_printf(parent, "ACPI: could not attach\n");
2700ae55423SMike Smith 	    return_VOID;
27115e32d5dSMike Smith     }
27215e32d5dSMike Smith }
27315e32d5dSMike Smith 
27415e32d5dSMike Smith /*
27515e32d5dSMike Smith  * Fetch some descriptive data from ACPI to put in our attach message
27615e32d5dSMike Smith  */
27715e32d5dSMike Smith static int
27815e32d5dSMike Smith acpi_probe(device_t dev)
27915e32d5dSMike Smith {
28015e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
28115e32d5dSMike Smith     char		buf[20];
282cb9b0d80SMike Smith     ACPI_STATUS		status;
28315e32d5dSMike Smith     int			error;
28415e32d5dSMike Smith 
285b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
286f9390180SMitsuru IWASAKI 
287f9390180SMitsuru IWASAKI     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
288f9390180SMitsuru IWASAKI         power_pm_get_type() != POWER_PM_TYPE_ACPI) {
289f9390180SMitsuru IWASAKI 	device_printf(dev, "Other PM system enabled.\n");
290f9390180SMitsuru IWASAKI 	return_VALUE(ENXIO);
291f9390180SMitsuru IWASAKI     }
292f9390180SMitsuru IWASAKI 
293cb9b0d80SMike Smith     ACPI_LOCK;
2940ae55423SMike Smith 
295b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
296bfae45aaSMike Smith 	device_printf(dev, "couldn't get XSDT header: %s\n", AcpiFormatException(status));
297cb9b0d80SMike Smith 	error = ENXIO;
298cb9b0d80SMike Smith     } else {
29915e32d5dSMike Smith 	sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId);
30015e32d5dSMike Smith 	device_set_desc_copy(dev, buf);
301cb9b0d80SMike Smith 	error = 0;
302cb9b0d80SMike Smith     }
303cb9b0d80SMike Smith     ACPI_UNLOCK;
304cb9b0d80SMike Smith     return_VALUE(error);
30515e32d5dSMike Smith }
30615e32d5dSMike Smith 
30715e32d5dSMike Smith static int
30815e32d5dSMike Smith acpi_attach(device_t dev)
30915e32d5dSMike Smith {
31015e32d5dSMike Smith     struct acpi_softc	*sc;
311cb9b0d80SMike Smith     ACPI_STATUS		status;
31215e32d5dSMike Smith     int			error;
31332d18aa5SMike Smith     UINT32		flags;
31432d18aa5SMike Smith 
315d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
316d786139cSMaxime Henrion     char		*debugpoint;
31715e32d5dSMike Smith #endif
31815e32d5dSMike Smith 
319b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
320cb9b0d80SMike Smith     ACPI_LOCK;
32115e32d5dSMike Smith     sc = device_get_softc(dev);
32215e32d5dSMike Smith     bzero(sc, sizeof(*sc));
32315e32d5dSMike Smith     sc->acpi_dev = dev;
32415e32d5dSMike Smith 
325d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
326d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
327d786139cSMaxime Henrion     if (debugpoint) {
328d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "spaces"))
32915e32d5dSMike Smith 	    acpi_EnterDebugger();
330d786139cSMaxime Henrion         freeenv(debugpoint);
331d786139cSMaxime Henrion     }
33215e32d5dSMike Smith #endif
33315e32d5dSMike Smith 
33415e32d5dSMike Smith     /*
33515e32d5dSMike Smith      * Install the default address space handlers.
33615e32d5dSMike Smith      */
337cb9b0d80SMike Smith     error = ENXIO;
338b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
3392a4ac806SMike Smith 						ACPI_ADR_SPACE_SYSTEM_MEMORY,
34015e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
341b53f2771SMike Smith 						NULL, NULL))) {
342bfae45aaSMike Smith 	device_printf(dev, "could not initialise SystemMemory handler: %s\n", AcpiFormatException(status));
343cb9b0d80SMike Smith 	goto out;
34415e32d5dSMike Smith     }
345b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
3462a4ac806SMike Smith 						ACPI_ADR_SPACE_SYSTEM_IO,
34715e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
348b53f2771SMike Smith 						NULL, NULL))) {
349bfae45aaSMike Smith 	device_printf(dev, "could not initialise SystemIO handler: %s\n", AcpiFormatException(status));
350cb9b0d80SMike Smith 	goto out;
35115e32d5dSMike Smith     }
352b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
3532a4ac806SMike Smith 						ACPI_ADR_SPACE_PCI_CONFIG,
35415e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
355b53f2771SMike Smith 						NULL, NULL))) {
356bfae45aaSMike Smith 	device_printf(dev, "could not initialise PciConfig handler: %s\n", AcpiFormatException(status));
357cb9b0d80SMike Smith 	goto out;
35815e32d5dSMike Smith     }
35915e32d5dSMike Smith 
36015e32d5dSMike Smith     /*
36115e32d5dSMike Smith      * Bring ACPI fully online.
36215e32d5dSMike Smith      *
36332d18aa5SMike Smith      * Note that some systems (specifically, those with namespace evaluation issues
36432d18aa5SMike Smith      * that require the avoidance of parts of the namespace) must avoid running _INI
36532d18aa5SMike Smith      * and _STA on everything, as well as dodging the final object init pass.
36615e32d5dSMike Smith      *
36732d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
36832d18aa5SMike Smith      *
36932d18aa5SMike Smith      * XXX We should arrange for the object init pass after we have attached all our
37032d18aa5SMike Smith      *     child devices, but on many systems it works here.
37115e32d5dSMike Smith      */
372d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
373d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
374d786139cSMaxime Henrion     if (debugpoint) {
375d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "enable"))
37615e32d5dSMike Smith 	    acpi_EnterDebugger();
377d786139cSMaxime Henrion         freeenv(debugpoint);
378d786139cSMaxime Henrion     }
37915e32d5dSMike Smith #endif
38032d18aa5SMike Smith     flags = 0;
381d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
38232d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
383b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
384bfae45aaSMike Smith 	device_printf(dev, "could not enable ACPI: %s\n", AcpiFormatException(status));
385cb9b0d80SMike Smith 	goto out;
38615e32d5dSMike Smith     }
38715e32d5dSMike Smith 
388b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
389b69ed3f4SMitsuru IWASAKI 	device_printf(dev, "could not initialize ACPI objects: %s\n", AcpiFormatException(status));
390b69ed3f4SMitsuru IWASAKI 	goto out;
391b69ed3f4SMitsuru IWASAKI     }
392b69ed3f4SMitsuru IWASAKI 
39315e32d5dSMike Smith     /*
3941d073b1dSJohn Baldwin      * Setup our sysctl tree.
3951d073b1dSJohn Baldwin      *
3961d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
3971d073b1dSJohn Baldwin      */
3981d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
3991d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
4001d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
4011d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
4021d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4031d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4041d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4051d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4061d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4071d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4081d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4091d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
4101d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
411f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
412f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
413f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
414f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
415f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
416f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
4172d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
418ff01efb5SMitsuru IWASAKI 	OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
419ff01efb5SMitsuru IWASAKI 	&sc->acpi_sleep_delay, 0, "sleep delay");
420ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4211611ea87SMitsuru IWASAKI 	OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
4221611ea87SMitsuru IWASAKI 	&sc->acpi_s4bios, 0, "S4BIOS mode");
4231611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4242d644607SMitsuru IWASAKI 	OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
4252d644607SMitsuru IWASAKI 	&sc->acpi_verbose, 0, "verbose mode");
426f3a6cbb6SMitsuru IWASAKI     sc->acpi_sleep_delay = 0;
427931a10c9SMitsuru IWASAKI     sc->acpi_s4bios = 1;
4282d644607SMitsuru IWASAKI     if (bootverbose)
4292d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
4301d073b1dSJohn Baldwin 
4311d073b1dSJohn Baldwin     /*
43215e32d5dSMike Smith      * Dispatch the default sleep state to devices.
43315e32d5dSMike Smith      * TBD: should be configured from userland policy manager.
43415e32d5dSMike Smith      */
43515e32d5dSMike Smith     sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
43615e32d5dSMike Smith     sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
43715e32d5dSMike Smith     sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
438f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
439f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
44015e32d5dSMike Smith 
44113d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
44215e32d5dSMike Smith 
44315e32d5dSMike Smith     /*
44415e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
44515e32d5dSMike Smith      */
446d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
447d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
448d786139cSMaxime Henrion     if (debugpoint) {
449d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "probe"))
45015e32d5dSMike Smith 	    acpi_EnterDebugger();
451d786139cSMaxime Henrion 	freeenv(debugpoint);
452d786139cSMaxime Henrion     }
45315e32d5dSMike Smith #endif
4540ae55423SMike Smith     if (!acpi_disabled("bus"))
45515e32d5dSMike Smith 	acpi_probe_children(dev);
45615e32d5dSMike Smith 
45715e32d5dSMike Smith     /*
45815e32d5dSMike Smith      * Register our shutdown handlers
45915e32d5dSMike Smith      */
46015e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST);
46115e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST);
46215e32d5dSMike Smith 
46315e32d5dSMike Smith     /*
46415e32d5dSMike Smith      * Register our acpi event handlers.
46515e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
46615e32d5dSMike Smith      */
46715e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST);
46815e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST);
46915e32d5dSMike Smith 
47015e32d5dSMike Smith     /*
47115e32d5dSMike Smith      * Flag our initial states.
47215e32d5dSMike Smith      */
47315e32d5dSMike Smith     sc->acpi_enabled = 1;
47415e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
475ece50487SMitsuru IWASAKI     sc->acpi_sleep_disabled = 0;
47615e32d5dSMike Smith 
47715e32d5dSMike Smith     /*
47815e32d5dSMike Smith      * Create the control device
47915e32d5dSMike Smith      */
48015e32d5dSMike Smith     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, 0, 5, 0660, "acpi");
48115e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
48215e32d5dSMike Smith 
483d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
484d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
485d786139cSMaxime Henrion     if (debugpoint) {
486d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "running"))
48715e32d5dSMike Smith 	    acpi_EnterDebugger();
488d786139cSMaxime Henrion 	freeenv(debugpoint);
489d786139cSMaxime Henrion     }
49015e32d5dSMike Smith #endif
491f86214b6SMitsuru IWASAKI 
492c573e654SMitsuru IWASAKI #if defined(ACPI_MAX_THREADS) && ACPI_MAX_THREADS > 0
493c573e654SMitsuru IWASAKI     if ((error = acpi_task_thread_init())) {
494c573e654SMitsuru IWASAKI 	goto out;
495c573e654SMitsuru IWASAKI     }
496c573e654SMitsuru IWASAKI #endif
497c573e654SMitsuru IWASAKI 
498f86214b6SMitsuru IWASAKI     if ((error = acpi_machdep_init(dev))) {
499f86214b6SMitsuru IWASAKI 	goto out;
500f86214b6SMitsuru IWASAKI     }
501f86214b6SMitsuru IWASAKI 
502f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
503f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
504f9390180SMitsuru IWASAKI 
505cb9b0d80SMike Smith     error = 0;
506cb9b0d80SMike Smith 
507cb9b0d80SMike Smith  out:
508cb9b0d80SMike Smith     ACPI_UNLOCK;
509cb9b0d80SMike Smith     return_VALUE(error);
51015e32d5dSMike Smith }
51115e32d5dSMike Smith 
51215e32d5dSMike Smith /*
51315e32d5dSMike Smith  * Handle a new device being added
51415e32d5dSMike Smith  */
51515e32d5dSMike Smith static device_t
51615e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
51715e32d5dSMike Smith {
51815e32d5dSMike Smith     struct acpi_device	*ad;
51915e32d5dSMike Smith     device_t		child;
52015e32d5dSMike Smith 
52115e32d5dSMike Smith     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL)
52215e32d5dSMike Smith 	return(NULL);
52315e32d5dSMike Smith     bzero(ad, sizeof(*ad));
52415e32d5dSMike Smith 
52515e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
52615e32d5dSMike Smith 
52715e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
52815e32d5dSMike Smith     if (child != NULL)
52915e32d5dSMike Smith 	device_set_ivars(child, ad);
53015e32d5dSMike Smith     return(child);
53115e32d5dSMike Smith }
53215e32d5dSMike Smith 
53315e32d5dSMike Smith static int
53415e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
53515e32d5dSMike Smith {
53615e32d5dSMike Smith     struct acpi_device		*adev = device_get_ivars(child);
53715e32d5dSMike Smith     struct resource_list	*rl = &adev->ad_rl;
53815e32d5dSMike Smith     int retval = 0;
53915e32d5dSMike Smith 
54015e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
5419ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
5429ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
5439ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
5449ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
54515e32d5dSMike Smith     retval += bus_print_child_footer(bus, child);
54615e32d5dSMike Smith 
54715e32d5dSMike Smith     return(retval);
54815e32d5dSMike Smith }
54915e32d5dSMike Smith 
55015e32d5dSMike Smith 
55115e32d5dSMike Smith /*
55215e32d5dSMike Smith  * Handle per-device ivars
55315e32d5dSMike Smith  */
55415e32d5dSMike Smith static int
55515e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
55615e32d5dSMike Smith {
55715e32d5dSMike Smith     struct acpi_device	*ad;
55815e32d5dSMike Smith 
55915e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
56015e32d5dSMike Smith 	printf("device has no ivars\n");
56115e32d5dSMike Smith 	return(ENOENT);
56215e32d5dSMike Smith     }
56315e32d5dSMike Smith 
56415e32d5dSMike Smith     switch(index) {
56515e32d5dSMike Smith 	/* ACPI ivars */
56615e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
56715e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
56815e32d5dSMike Smith 	break;
56915e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
57015e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
57115e32d5dSMike Smith 	break;
57215e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
57315e32d5dSMike Smith 	*(void **)result = ad->ad_private;
57415e32d5dSMike Smith 	break;
57515e32d5dSMike Smith 
57632d18aa5SMike Smith 	/* ISA compatibility */
57732d18aa5SMike Smith     case ISA_IVAR_VENDORID:
57832d18aa5SMike Smith     case ISA_IVAR_SERIAL:
57932d18aa5SMike Smith     case ISA_IVAR_COMPATID:
58032d18aa5SMike Smith 	*(int *)result = -1;
58132d18aa5SMike Smith 	break;
58232d18aa5SMike Smith 
58332d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
58432d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
58532d18aa5SMike Smith 	break;
58632d18aa5SMike Smith 
58715e32d5dSMike Smith     default:
5882ab060eeSWarner Losh 	panic("bad ivar read request (%d)", index);
58915e32d5dSMike Smith 	return(ENOENT);
59015e32d5dSMike Smith     }
59115e32d5dSMike Smith     return(0);
59215e32d5dSMike Smith }
59315e32d5dSMike Smith 
59415e32d5dSMike Smith static int
59515e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
59615e32d5dSMike Smith {
59715e32d5dSMike Smith     struct acpi_device	*ad;
59815e32d5dSMike Smith 
59915e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
60015e32d5dSMike Smith 	printf("device has no ivars\n");
60115e32d5dSMike Smith 	return(ENOENT);
60215e32d5dSMike Smith     }
60315e32d5dSMike Smith 
60415e32d5dSMike Smith     switch(index) {
60515e32d5dSMike Smith 	/* ACPI ivars */
60615e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
60715e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
60815e32d5dSMike Smith 	break;
60915e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
61015e32d5dSMike Smith 	ad->ad_magic = (int )value;
61115e32d5dSMike Smith 	break;
61215e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
61315e32d5dSMike Smith 	ad->ad_private = (void *)value;
61415e32d5dSMike Smith 	break;
61515e32d5dSMike Smith 
61615e32d5dSMike Smith     default:
6172ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
61815e32d5dSMike Smith 	return(ENOENT);
61915e32d5dSMike Smith     }
62015e32d5dSMike Smith     return(0);
62115e32d5dSMike Smith }
62215e32d5dSMike Smith 
62315e32d5dSMike Smith /*
62415e32d5dSMike Smith  * Handle child resource allocation/removal
62515e32d5dSMike Smith  */
62615e32d5dSMike Smith static int
62715e32d5dSMike Smith acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
62815e32d5dSMike Smith {
62915e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
63015e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
63115e32d5dSMike Smith 
63215e32d5dSMike Smith     resource_list_add(rl, type, rid, start, start + count -1, count);
63315e32d5dSMike Smith 
63415e32d5dSMike Smith     return(0);
63515e32d5dSMike Smith }
63615e32d5dSMike Smith 
63715e32d5dSMike Smith static int
63815e32d5dSMike Smith acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
63915e32d5dSMike Smith {
64015e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
64115e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
64215e32d5dSMike Smith     struct resource_list_entry	*rle;
64315e32d5dSMike Smith 
64415e32d5dSMike Smith     rle = resource_list_find(rl, type, rid);
64515e32d5dSMike Smith     if (!rle)
64615e32d5dSMike Smith 	return(ENOENT);
64715e32d5dSMike Smith 
64815e32d5dSMike Smith     if (startp)
64915e32d5dSMike Smith 	*startp = rle->start;
65015e32d5dSMike Smith     if (countp)
65115e32d5dSMike Smith 	*countp = rle->count;
65215e32d5dSMike Smith 
65315e32d5dSMike Smith     return(0);
65415e32d5dSMike Smith }
65515e32d5dSMike Smith 
65615e32d5dSMike Smith static struct resource *
65715e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
65815e32d5dSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
65915e32d5dSMike Smith {
66015e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
66115e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
66215e32d5dSMike Smith 
66315e32d5dSMike Smith     return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags));
66415e32d5dSMike Smith }
66515e32d5dSMike Smith 
66615e32d5dSMike Smith static int
66715e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
66815e32d5dSMike Smith {
66915e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
67015e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
67115e32d5dSMike Smith 
67215e32d5dSMike Smith     return(resource_list_release(rl, bus, child, type, rid, r));
67315e32d5dSMike Smith }
67415e32d5dSMike Smith 
67515e32d5dSMike Smith /*
676bc0f2195SMike Smith  * Handle ISA-like devices probing for a PnP ID to match.
677bc0f2195SMike Smith  */
678bc0f2195SMike Smith #define PNP_EISAID(s)				\
679bc0f2195SMike Smith 	((((s[0] - '@') & 0x1f) << 2)		\
680bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x18) >> 3)		\
681bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x07) << 13)	\
682bc0f2195SMike Smith 	 | (((s[2] - '@') & 0x1f) << 8)		\
683bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[4]) << 16)		\
684bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[3]) << 20)		\
685bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[6]) << 24)		\
686bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[5]) << 28))
687bc0f2195SMike Smith 
68832d18aa5SMike Smith static u_int32_t
68932d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
690bc0f2195SMike Smith {
691bc0f2195SMike Smith     ACPI_HANDLE		h;
692bc0f2195SMike Smith     ACPI_DEVICE_INFO	devinfo;
693bc0f2195SMike Smith     ACPI_STATUS		error;
694bc0f2195SMike Smith     u_int32_t		pnpid;
69532d18aa5SMike Smith 
696b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
69732d18aa5SMike Smith 
69832d18aa5SMike Smith     pnpid = 0;
69932d18aa5SMike Smith     ACPI_LOCK;
70032d18aa5SMike Smith 
70132d18aa5SMike Smith     /* fetch and validate the HID */
70232d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
70332d18aa5SMike Smith 	goto out;
704b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
70532d18aa5SMike Smith 	goto out;
70632d18aa5SMike Smith     if (!(devinfo.Valid & ACPI_VALID_HID))
70732d18aa5SMike Smith 	goto out;
70832d18aa5SMike Smith 
70932d18aa5SMike Smith     pnpid = PNP_EISAID(devinfo.HardwareId);
71032d18aa5SMike Smith out:
71132d18aa5SMike Smith     ACPI_UNLOCK;
71232d18aa5SMike Smith     return_VALUE(pnpid);
71332d18aa5SMike Smith }
71432d18aa5SMike Smith 
71532d18aa5SMike Smith static int
71632d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
71732d18aa5SMike Smith {
718bc0f2195SMike Smith     int			result;
71932d18aa5SMike Smith     u_int32_t		pnpid;
720bc0f2195SMike Smith 
721b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
722bc0f2195SMike Smith 
723bc0f2195SMike Smith     /*
724bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
725bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
726bc0f2195SMike Smith      * that to happen, so don't ever return it.
727bc0f2195SMike Smith      */
728bc0f2195SMike Smith     result = ENXIO;
729bc0f2195SMike Smith 
730bc0f2195SMike Smith     /* scan the supplied IDs for a match */
73132d18aa5SMike Smith     pnpid = acpi_isa_get_logicalid(child);
732bc0f2195SMike Smith     while (ids && ids->ip_id) {
733bc0f2195SMike Smith 	if (pnpid == ids->ip_id) {
734bc0f2195SMike Smith 	    result = 0;
735bc0f2195SMike Smith 	    goto out;
736bc0f2195SMike Smith 	}
737bc0f2195SMike Smith 	ids++;
738bc0f2195SMike Smith     }
739bc0f2195SMike Smith  out:
740bc0f2195SMike Smith     return_VALUE(result);
741bc0f2195SMike Smith }
742bc0f2195SMike Smith 
743bc0f2195SMike Smith /*
74415e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
74515e32d5dSMike Smith  *
7467d3bcec9SMike Smith  * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and \_SB_ scopes,
7477d3bcec9SMike Smith  * and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
74815e32d5dSMike Smith  */
74915e32d5dSMike Smith static void
75015e32d5dSMike Smith acpi_probe_children(device_t bus)
75115e32d5dSMike Smith {
75215e32d5dSMike Smith     ACPI_HANDLE		parent;
7537d3bcec9SMike Smith     static char		*scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
75415e32d5dSMike Smith     int			i;
75515e32d5dSMike Smith 
756b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
757cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
7580ae55423SMike Smith 
75915e32d5dSMike Smith     /*
76015e32d5dSMike Smith      * Create any static children by calling device identify methods.
76115e32d5dSMike Smith      */
7624c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
76315e32d5dSMike Smith     bus_generic_probe(bus);
76415e32d5dSMike Smith 
76515e32d5dSMike Smith     /*
76615e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
76715e32d5dSMike Smith      * we find.
76815e32d5dSMike Smith      *
76915e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
77015e32d5dSMike Smith      * we want to create nodes for all devices, not just those that are currently
77115e32d5dSMike Smith      * present. (This assumes that we don't want to create/remove devices as they
77215e32d5dSMike Smith      * appear, which might be smarter.)
77315e32d5dSMike Smith      */
7744c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
77515e32d5dSMike Smith     for (i = 0; scopes[i] != NULL; i++)
776b53f2771SMike Smith 	if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent)))
77715e32d5dSMike Smith 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL);
77815e32d5dSMike Smith 
77915e32d5dSMike Smith     /*
78015e32d5dSMike Smith      * Scan all of the child devices we have created and let them probe/attach.
78115e32d5dSMike Smith      */
7824c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
78315e32d5dSMike Smith     bus_generic_attach(bus);
78415e32d5dSMike Smith 
78515e32d5dSMike Smith     /*
78615e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
78715e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
78815e32d5dSMike Smith      */
7894c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
79015e32d5dSMike Smith     bus_generic_attach(bus);
7910ae55423SMike Smith 
792bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
7930ae55423SMike Smith     return_VOID;
79415e32d5dSMike Smith }
79515e32d5dSMike Smith 
79615e32d5dSMike Smith /*
79715e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
79815e32d5dSMike Smith  * it.
79915e32d5dSMike Smith  */
80015e32d5dSMike Smith static ACPI_STATUS
80115e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
80215e32d5dSMike Smith {
80315e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
80415e32d5dSMike Smith     device_t		child, bus = (device_t)context;
80515e32d5dSMike Smith 
806b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
8070ae55423SMike Smith 
8080ae55423SMike Smith     /*
8090ae55423SMike Smith      * Skip this device if we think we'll have trouble with it.
8100ae55423SMike Smith      */
8110ae55423SMike Smith     if (acpi_avoid(handle))
8120ae55423SMike Smith 	return_ACPI_STATUS(AE_OK);
8130ae55423SMike Smith 
814b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
81515e32d5dSMike Smith 	switch(type) {
81615e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
81715e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
81815e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
81915e32d5dSMike Smith 	case ACPI_TYPE_POWER:
8200ae55423SMike Smith 	    if (acpi_disabled("children"))
8210ae55423SMike Smith 		break;
82215e32d5dSMike Smith 	    /*
82315e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
82415e32d5dSMike Smith 	     * so that the probe/attach passes will run breadth-first.
82515e32d5dSMike Smith 	     */
8264c1cdee6SMike Smith 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", acpi_name(handle)));
82715e32d5dSMike Smith 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
828bc0f2195SMike Smith 	    if (child == NULL)
829bc0f2195SMike Smith 		break;
83015e32d5dSMike Smith 	    acpi_set_handle(child, handle);
831bc0f2195SMike Smith 
832bc0f2195SMike Smith 	    /*
833b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
834b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
835b519f9d6SMike Smith 	     * the handle, but we don't probe it).
836b519f9d6SMike Smith 	     */
83723b4e251SMitsuru IWASAKI 	    if ((type == ACPI_TYPE_DEVICE) && (!acpi_DeviceIsPresent(child))) {
838b519f9d6SMike Smith 		device_disable(child);
839b519f9d6SMike Smith 		break;
840b519f9d6SMike Smith 	    }
841b519f9d6SMike Smith 
842b519f9d6SMike Smith 	    /*
843bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
844bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
845bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
846bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
847bc0f2195SMike Smith 	     * device not to have any resources.
848bc0f2195SMike Smith 	     */
849bc0f2195SMike Smith 	    acpi_parse_resources(child, handle, &acpi_res_parse_set);
850bc0f2195SMike Smith 
851bc0f2195SMike Smith 	    /* if we're debugging, probe/attach now rather than later */
852b53f2771SMike Smith 	    ACPI_DEBUG_EXEC(device_probe_and_attach(child));
8530ae55423SMike Smith 	    break;
85415e32d5dSMike Smith 	}
85515e32d5dSMike Smith     }
8560ae55423SMike Smith     return_ACPI_STATUS(AE_OK);
85715e32d5dSMike Smith }
85815e32d5dSMike Smith 
85915e32d5dSMike Smith static void
86015e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto)
86115e32d5dSMike Smith {
862cb9b0d80SMike Smith 
863cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
864cb9b0d80SMike Smith 
86515e32d5dSMike Smith     /*
8660ae55423SMike Smith      * Disable all ACPI events before soft off, otherwise the system
86715e32d5dSMike Smith      * will be turned on again on some laptops.
86815e32d5dSMike Smith      *
86915e32d5dSMike Smith      * XXX this should probably be restricted to masking some events just
87015e32d5dSMike Smith      *     before powering down, since we may still need ACPI during the
87115e32d5dSMike Smith      *     shutdown process.
87215e32d5dSMike Smith      */
87315e32d5dSMike Smith     acpi_Disable((struct acpi_softc *)arg);
87415e32d5dSMike Smith }
87515e32d5dSMike Smith 
87615e32d5dSMike Smith static void
87715e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
87815e32d5dSMike Smith {
87915e32d5dSMike Smith     ACPI_STATUS	status;
88015e32d5dSMike Smith 
881cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
882cb9b0d80SMike Smith 
8835f3e4175SMitsuru IWASAKI     if (howto & RB_POWEROFF) {
88415e32d5dSMike Smith 	printf("Power system off using ACPI...\n");
885b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(acpi_off_state))) {
886b9c780d6SMitsuru IWASAKI 	    printf("AcpiEnterSleepStatePrep failed - %s\n",
887b9c780d6SMitsuru IWASAKI 		   AcpiFormatException(status));
888b9c780d6SMitsuru IWASAKI 	    return;
889b9c780d6SMitsuru IWASAKI 	}
890b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepState(acpi_off_state))) {
891bfae45aaSMike Smith 	    printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
89215e32d5dSMike Smith 	} else {
89315e32d5dSMike Smith 	    DELAY(1000000);
89415e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
89515e32d5dSMike Smith 	}
896494ae560SMitsuru IWASAKI     } else {
897494ae560SMitsuru IWASAKI 	printf("Terminate ACPI\n");
898494ae560SMitsuru IWASAKI 	AcpiTerminate();
89915e32d5dSMike Smith     }
90015e32d5dSMike Smith }
90115e32d5dSMike Smith 
90213d4f7baSMitsuru IWASAKI static void
90313d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
90413d4f7baSMitsuru IWASAKI {
90513d4f7baSMitsuru IWASAKI     static int	first_time = 1;
90613d4f7baSMitsuru IWASAKI #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n"
90713d4f7baSMitsuru IWASAKI 
908cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
909cb9b0d80SMike Smith 
91013d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
911cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) {
91243896e91SMike Smith 	AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED, 0);
91313d4f7baSMitsuru IWASAKI 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED);
91413d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
91513d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_power_button_for_sleep, sc);
91613d4f7baSMitsuru IWASAKI 	if (first_time) {
91713d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "power");
91813d4f7baSMitsuru IWASAKI 	}
91913d4f7baSMitsuru IWASAKI     }
920cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) {
92143896e91SMike Smith 	AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED, 0);
92213d4f7baSMitsuru IWASAKI 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED);
92313d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
92413d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_sleep_button_for_sleep, sc);
92513d4f7baSMitsuru IWASAKI 	if (first_time) {
92613d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "sleep");
92713d4f7baSMitsuru IWASAKI 	}
92813d4f7baSMitsuru IWASAKI     }
92913d4f7baSMitsuru IWASAKI 
93013d4f7baSMitsuru IWASAKI     first_time = 0;
93113d4f7baSMitsuru IWASAKI }
93213d4f7baSMitsuru IWASAKI 
93315e32d5dSMike Smith /*
934c5ba0be4SMike Smith  * Returns true if the device is actually present and should
935c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
936c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
937c5ba0be4SMike Smith  */
938c5ba0be4SMike Smith BOOLEAN
939c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
940c5ba0be4SMike Smith {
941c5ba0be4SMike Smith     ACPI_HANDLE		h;
942c5ba0be4SMike Smith     ACPI_DEVICE_INFO	devinfo;
943c5ba0be4SMike Smith     ACPI_STATUS		error;
944c5ba0be4SMike Smith 
945cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
946cb9b0d80SMike Smith 
947c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
948c5ba0be4SMike Smith 	return(FALSE);
949b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
950c5ba0be4SMike Smith 	return(FALSE);
95143896e91SMike Smith     /* if no _STA method, must be present */
95243896e91SMike Smith     if (!(devinfo.Valid & ACPI_VALID_STA))
95343896e91SMike Smith 	return(TRUE);
95443896e91SMike Smith     /* return true for 'present' and 'functioning' */
95543896e91SMike Smith     if ((devinfo.CurrentStatus & 0x9) == 0x9)
956c5ba0be4SMike Smith 	return(TRUE);
957c5ba0be4SMike Smith     return(FALSE);
958c5ba0be4SMike Smith }
959c5ba0be4SMike Smith 
960c5ba0be4SMike Smith /*
961b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
962b53f2771SMike Smith  */
963b53f2771SMike Smith BOOLEAN
964b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
965b53f2771SMike Smith {
966b53f2771SMike Smith     ACPI_HANDLE		h;
967b53f2771SMike Smith     ACPI_DEVICE_INFO	devinfo;
968b53f2771SMike Smith     ACPI_STATUS		error;
969b53f2771SMike Smith 
970b53f2771SMike Smith     ACPI_ASSERTLOCK;
971b53f2771SMike Smith 
972b53f2771SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
973b53f2771SMike Smith 	return(FALSE);
974b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
975b53f2771SMike Smith 	return(FALSE);
976b53f2771SMike Smith     /* if no _STA method, must be present */
977b53f2771SMike Smith     if (!(devinfo.Valid & ACPI_VALID_STA))
978b53f2771SMike Smith 	return(TRUE);
979b53f2771SMike Smith     /* return true for 'present' and 'functioning' */
980b53f2771SMike Smith     if ((devinfo.CurrentStatus & 0x19) == 0x19)
981b53f2771SMike Smith 	return(TRUE);
982b53f2771SMike Smith     return(FALSE);
983b53f2771SMike Smith }
984b53f2771SMike Smith 
985b53f2771SMike Smith /*
98615e32d5dSMike Smith  * Match a HID string against a device
98715e32d5dSMike Smith  */
98815e32d5dSMike Smith BOOLEAN
98915e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid)
99015e32d5dSMike Smith {
99115e32d5dSMike Smith     ACPI_HANDLE		h;
99215e32d5dSMike Smith     ACPI_DEVICE_INFO	devinfo;
99315e32d5dSMike Smith     ACPI_STATUS		error;
994ed136da6SDoug Rabson     int			cid;
99515e32d5dSMike Smith 
996cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
997cb9b0d80SMike Smith 
9984d332989SMike Smith     if (hid == NULL)
99915e32d5dSMike Smith 	return(FALSE);
100015e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
100115e32d5dSMike Smith 	return(FALSE);
1002b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
100315e32d5dSMike Smith 	return(FALSE);
10044d332989SMike Smith     if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId))
100515e32d5dSMike Smith 	return(TRUE);
1006b53f2771SMike Smith     if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &cid)))
1007ed136da6SDoug Rabson 	return(FALSE);
1008ed136da6SDoug Rabson     if (cid == PNP_EISAID(hid))
1009ed136da6SDoug Rabson 	return(TRUE);
101015e32d5dSMike Smith     return(FALSE);
101115e32d5dSMike Smith }
101215e32d5dSMike Smith 
101315e32d5dSMike Smith /*
1014c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1015c5ba0be4SMike Smith  * or one if its parents.
1016c5ba0be4SMike Smith  */
1017c5ba0be4SMike Smith ACPI_STATUS
1018c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1019c5ba0be4SMike Smith {
1020c5ba0be4SMike Smith     ACPI_HANDLE		r;
1021c5ba0be4SMike Smith     ACPI_STATUS		status;
1022c5ba0be4SMike Smith 
1023cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1024cb9b0d80SMike Smith 
1025c5ba0be4SMike Smith     /* walk back up the tree to the root */
1026c5ba0be4SMike Smith     for (;;) {
1027b53f2771SMike Smith 	if (ACPI_SUCCESS(status = AcpiGetHandle(parent, path, &r))) {
1028c5ba0be4SMike Smith 	    *result = r;
1029c5ba0be4SMike Smith 	    return(AE_OK);
1030c5ba0be4SMike Smith 	}
1031c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1032c5ba0be4SMike Smith 	    return(AE_OK);
1033b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1034c5ba0be4SMike Smith 	    return(AE_NOT_FOUND);
1035c5ba0be4SMike Smith 	parent = r;
1036c5ba0be4SMike Smith     }
1037c5ba0be4SMike Smith }
1038c5ba0be4SMike Smith 
1039c5ba0be4SMike Smith /*
1040c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1041c5ba0be4SMike Smith  */
1042c5ba0be4SMike Smith ACPI_BUFFER *
1043c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1044c5ba0be4SMike Smith {
1045c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1046c5ba0be4SMike Smith 
1047c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1048c5ba0be4SMike Smith 	return(NULL);
1049c5ba0be4SMike Smith     buf->Length = size;
1050c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1051c5ba0be4SMike Smith     return(buf);
1052c5ba0be4SMike Smith }
1053c5ba0be4SMike Smith 
1054c5ba0be4SMike Smith /*
1055c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1056c5ba0be4SMike Smith  */
1057c5ba0be4SMike Smith ACPI_STATUS
1058c5ba0be4SMike Smith acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number)
1059c5ba0be4SMike Smith {
1060c5ba0be4SMike Smith     ACPI_STATUS	error;
1061c5ba0be4SMike Smith     ACPI_BUFFER	buf;
1062c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
1063c5ba0be4SMike Smith 
1064cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1065cb9b0d80SMike Smith 
1066c5ba0be4SMike Smith     if (handle == NULL)
1067c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
10680c7da7acSJohn Baldwin 
10690c7da7acSJohn Baldwin     /*
10700c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
10710c7da7acSJohn Baldwin      * a method that will return an Integer.
10720c7da7acSJohn Baldwin      */
1073c5ba0be4SMike Smith     buf.Pointer = &param;
1074c5ba0be4SMike Smith     buf.Length = sizeof(param);
1075b53f2771SMike Smith     if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
1076c5ba0be4SMike Smith 	if (param.Type == ACPI_TYPE_INTEGER) {
1077c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1078c5ba0be4SMike Smith 	} else {
1079c5ba0be4SMike Smith 	    error = AE_TYPE;
1080c5ba0be4SMike Smith 	}
1081c5ba0be4SMike Smith     }
10820c7da7acSJohn Baldwin 
10830c7da7acSJohn Baldwin     /*
10840c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
10850c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
10860c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
10870c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
10880c7da7acSJohn Baldwin      *
10890c7da7acSJohn Baldwin      * This is a hack.
10900c7da7acSJohn Baldwin      */
10910c7da7acSJohn Baldwin     if (error == AE_BUFFER_OVERFLOW) {
1092b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
10930c7da7acSJohn Baldwin 	    error = AE_NO_MEMORY;
10940c7da7acSJohn Baldwin 	} else {
1095b53f2771SMike Smith 	    if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
1096c573e654SMitsuru IWASAKI 		error = acpi_ConvertBufferToInteger(&buf, number);
10970c7da7acSJohn Baldwin 	    }
10980c7da7acSJohn Baldwin 	}
10990c7da7acSJohn Baldwin 	AcpiOsFree(buf.Pointer);
11000c7da7acSJohn Baldwin     }
1101c5ba0be4SMike Smith     return(error);
1102c5ba0be4SMike Smith }
1103c5ba0be4SMike Smith 
1104c573e654SMitsuru IWASAKI ACPI_STATUS
1105c573e654SMitsuru IWASAKI acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number)
1106c573e654SMitsuru IWASAKI {
1107c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
1108c573e654SMitsuru IWASAKI     int		i;
1109c573e654SMitsuru IWASAKI 
1110c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
1111c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
1112c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
1113c573e654SMitsuru IWASAKI 	return(AE_OK);
1114c573e654SMitsuru IWASAKI     }
1115c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
1116c573e654SMitsuru IWASAKI 	return(AE_TYPE);
1117c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
1118c573e654SMitsuru IWASAKI 	return(AE_BAD_DATA);
1119c573e654SMitsuru IWASAKI     *number = 0;
1120c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
1121c573e654SMitsuru IWASAKI 	*number += (*(p->Buffer.Pointer + i) << (i * 8));
1122c573e654SMitsuru IWASAKI     return(AE_OK);
1123c573e654SMitsuru IWASAKI }
1124c573e654SMitsuru IWASAKI 
1125c5ba0be4SMike Smith /*
1126c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1127c5ba0be4SMike Smith  * function for each element.
1128c5ba0be4SMike Smith  *
1129c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1130c5ba0be4SMike Smith  */
1131c5ba0be4SMike Smith ACPI_STATUS
1132c5ba0be4SMike Smith acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg)
1133c5ba0be4SMike Smith {
1134c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1135c5ba0be4SMike Smith     int		i;
1136c5ba0be4SMike Smith 
1137c5ba0be4SMike Smith     if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE))
1138c5ba0be4SMike Smith 	return(AE_BAD_PARAMETER);
1139c5ba0be4SMike Smith 
1140c5ba0be4SMike Smith     /* iterate over components */
1141c5ba0be4SMike Smith     for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++)
1142c5ba0be4SMike Smith 	func(comp, arg);
1143c5ba0be4SMike Smith 
1144c5ba0be4SMike Smith     return(AE_OK);
1145c5ba0be4SMike Smith }
1146c5ba0be4SMike Smith 
11476f69255bSMike Smith /*
11486f69255bSMike Smith  * Find the (index)th resource object in a set.
11496f69255bSMike Smith  */
11506f69255bSMike Smith ACPI_STATUS
11516d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
11526f69255bSMike Smith {
11536d63101aSMike Smith     ACPI_RESOURCE	*rp;
11546f69255bSMike Smith     int			i;
11556f69255bSMike Smith 
11566d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
11576f69255bSMike Smith     i = index;
11586d63101aSMike Smith     while (i-- > 0) {
11596f69255bSMike Smith 	/* range check */
11606d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
11616f69255bSMike Smith 	    return(AE_BAD_PARAMETER);
11626d63101aSMike Smith 	/* check for terminator */
11636d63101aSMike Smith 	if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
11646d63101aSMike Smith 	    (rp->Length == 0))
11656d63101aSMike Smith 	    return(AE_NOT_FOUND);
11666d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
11676f69255bSMike Smith     }
11686f69255bSMike Smith     if (resp != NULL)
11696d63101aSMike Smith 	*resp = rp;
11706d63101aSMike Smith     return(AE_OK);
11716d63101aSMike Smith }
11726d63101aSMike Smith 
11736d63101aSMike Smith /*
11746d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
11756d63101aSMike Smith  *
11766d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
11776d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
11786d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
11796d63101aSMike Smith  * resources.
11806d63101aSMike Smith  */
11816d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
11826d63101aSMike Smith 
11836d63101aSMike Smith ACPI_STATUS
11846d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
11856d63101aSMike Smith {
11866d63101aSMike Smith     ACPI_RESOURCE	*rp;
11876d63101aSMike Smith     void		*newp;
11886d63101aSMike Smith 
11896d63101aSMike Smith     /*
11906d63101aSMike Smith      * Initialise the buffer if necessary.
11916d63101aSMike Smith      */
11926d63101aSMike Smith     if (buf->Pointer == NULL) {
11936d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
11946d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
11956d63101aSMike Smith 	    return(AE_NO_MEMORY);
11966d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
11976d63101aSMike Smith 	rp->Id = ACPI_RSTYPE_END_TAG;
11986d63101aSMike Smith 	rp->Length = 0;
11996d63101aSMike Smith     }
12006d63101aSMike Smith     if (res == NULL)
12016d63101aSMike Smith 	return(AE_OK);
12026d63101aSMike Smith 
12036d63101aSMike Smith     /*
12046d63101aSMike Smith      * Scan the current buffer looking for the terminator.
12056d63101aSMike Smith      * This will either find the terminator or hit the end
12066d63101aSMike Smith      * of the buffer and return an error.
12076d63101aSMike Smith      */
12086d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
12096d63101aSMike Smith     for (;;) {
12106d63101aSMike Smith 	/* range check, don't go outside the buffer */
12116d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
12126d63101aSMike Smith 	    return(AE_BAD_PARAMETER);
12136d63101aSMike Smith 	if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
12146d63101aSMike Smith 	    (rp->Length == 0)) {
12156d63101aSMike Smith 	    break;
12166d63101aSMike Smith 	}
12176d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
12186d63101aSMike Smith     }
12196d63101aSMike Smith 
12206d63101aSMike Smith     /*
12216d63101aSMike Smith      * Check the size of the buffer and expand if required.
12226d63101aSMike Smith      *
12236d63101aSMike Smith      * Required size is:
12246d63101aSMike Smith      *	size of existing resources before terminator +
12256d63101aSMike Smith      *	size of new resource and header +
12266d63101aSMike Smith      * 	size of terminator.
12276d63101aSMike Smith      *
12286d63101aSMike Smith      * Note that this loop should really only run once, unless
12296d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
12306d63101aSMike Smith      */
12316d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
12326d63101aSMike Smith 	    res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
12336d63101aSMike Smith 	    ACPI_RESOURCE_LENGTH) >= buf->Length) {
12346d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
12356d63101aSMike Smith 	    return(AE_NO_MEMORY);
12366d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
1237a692219dSMike Smith         rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1238a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
12396d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
12406d63101aSMike Smith 	buf->Pointer = newp;
12416d63101aSMike Smith 	buf->Length += buf->Length;
12426d63101aSMike Smith     }
12436d63101aSMike Smith 
12446d63101aSMike Smith     /*
12456d63101aSMike Smith      * Insert the new resource.
12466d63101aSMike Smith      */
12476d63101aSMike Smith     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
12486d63101aSMike Smith 
12496d63101aSMike Smith     /*
12506d63101aSMike Smith      * And add the terminator.
12516d63101aSMike Smith      */
12526d63101aSMike Smith     rp = ACPI_RESOURCE_NEXT(rp);
12536d63101aSMike Smith     rp->Id = ACPI_RSTYPE_END_TAG;
12546d63101aSMike Smith     rp->Length = 0;
12556d63101aSMike Smith 
12566f69255bSMike Smith     return(AE_OK);
12576f69255bSMike Smith }
1258c5ba0be4SMike Smith 
1259da14ac9fSJohn Baldwin /*
1260da14ac9fSJohn Baldwin  * Set interrupt model.
1261da14ac9fSJohn Baldwin  */
1262da14ac9fSJohn Baldwin ACPI_STATUS
1263da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
1264da14ac9fSJohn Baldwin {
1265da14ac9fSJohn Baldwin 	ACPI_OBJECT_LIST ArgList;
1266da14ac9fSJohn Baldwin 	ACPI_OBJECT Arg;
1267da14ac9fSJohn Baldwin 
1268da14ac9fSJohn Baldwin 	Arg.Type = ACPI_TYPE_INTEGER;
1269da14ac9fSJohn Baldwin 	Arg.Integer.Value = model;
1270da14ac9fSJohn Baldwin 	ArgList.Count = 1;
1271da14ac9fSJohn Baldwin 	ArgList.Pointer = &Arg;
1272da14ac9fSJohn Baldwin 	return (AcpiEvaluateObject(ACPI_ROOT_OBJECT, "_PIC", &ArgList, NULL));
1273da14ac9fSJohn Baldwin }
1274da14ac9fSJohn Baldwin 
1275ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME	5
1276ece50487SMitsuru IWASAKI 
1277ece50487SMitsuru IWASAKI static void
1278ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
1279ece50487SMitsuru IWASAKI {
1280ece50487SMitsuru IWASAKI     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1281ece50487SMitsuru IWASAKI }
1282c30382dfSMitsuru IWASAKI 
128315e32d5dSMike Smith /*
128415e32d5dSMike Smith  * Set the system sleep state
128515e32d5dSMike Smith  *
128615e32d5dSMike Smith  * Currently we only support S1 and S5
128715e32d5dSMike Smith  */
128815e32d5dSMike Smith ACPI_STATUS
128915e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
129015e32d5dSMike Smith {
129115e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
129256d8cb57SMitsuru IWASAKI     UINT8	TypeA;
129356d8cb57SMitsuru IWASAKI     UINT8	TypeB;
129415e32d5dSMike Smith 
1295b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1296cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
12970ae55423SMike Smith 
12986397624dSMitsuru IWASAKI     if (sc->acpi_sstate != ACPI_STATE_S0)
12996397624dSMitsuru IWASAKI 	return_ACPI_STATUS(AE_BAD_PARAMETER);	/* avoid reentry */
13006397624dSMitsuru IWASAKI 
1301ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1302ece50487SMitsuru IWASAKI 	return_ACPI_STATUS(AE_OK);
1303ece50487SMitsuru IWASAKI 
130415e32d5dSMike Smith     switch (state) {
130515e32d5dSMike Smith     case ACPI_STATE_S0:	/* XXX only for testing */
1306b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
1307bfae45aaSMike Smith 	    device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
130815e32d5dSMike Smith 	}
130915e32d5dSMike Smith 	break;
131015e32d5dSMike Smith 
131115e32d5dSMike Smith     case ACPI_STATE_S1:
13126161544cSTakanori Watanabe     case ACPI_STATE_S2:
13136161544cSTakanori Watanabe     case ACPI_STATE_S3:
13146161544cSTakanori Watanabe     case ACPI_STATE_S4:
131598479b04SMitsuru IWASAKI 	if (ACPI_FAILURE(status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB))) {
131698479b04SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", AcpiFormatException(status));
131756d8cb57SMitsuru IWASAKI 	    break;
131856d8cb57SMitsuru IWASAKI 	}
131956d8cb57SMitsuru IWASAKI 
1320a1fccb47SMitsuru IWASAKI 	sc->acpi_sstate = state;
1321a1fccb47SMitsuru IWASAKI 	sc->acpi_sleep_disabled = 1;
1322a1fccb47SMitsuru IWASAKI 
132315e32d5dSMike Smith 	/*
132415e32d5dSMike Smith 	 * Inform all devices that we are going to sleep.
132515e32d5dSMike Smith 	 */
132615e32d5dSMike Smith 	if (DEVICE_SUSPEND(root_bus) != 0) {
132715e32d5dSMike Smith 	    /*
132815e32d5dSMike Smith 	     * Re-wake the system.
132915e32d5dSMike Smith 	     *
133015e32d5dSMike Smith 	     * XXX note that a better two-pass approach with a 'veto' pass
133115e32d5dSMike Smith 	     *     followed by a "real thing" pass would be better, but the
133215e32d5dSMike Smith 	     *     current bus interface does not provide for this.
133315e32d5dSMike Smith 	     */
133415e32d5dSMike Smith 	    DEVICE_RESUME(root_bus);
13350ae55423SMike Smith 	    return_ACPI_STATUS(AE_ERROR);
133615e32d5dSMike Smith 	}
1337b9c780d6SMitsuru IWASAKI 
1338b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(state))) {
1339b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1340b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
1341b9c780d6SMitsuru IWASAKI 	    break;
1342b9c780d6SMitsuru IWASAKI 	}
1343b9c780d6SMitsuru IWASAKI 
1344ff01efb5SMitsuru IWASAKI 	if (sc->acpi_sleep_delay > 0) {
1345ff01efb5SMitsuru IWASAKI 	    DELAY(sc->acpi_sleep_delay * 1000000);
1346ff01efb5SMitsuru IWASAKI 	}
1347ff01efb5SMitsuru IWASAKI 
13486161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
13496161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
13506161544cSTakanori Watanabe 
1351a1fccb47SMitsuru IWASAKI 	    /* AcpiEnterSleepState() maybe incompleted, unlock here if locked. */
1352a1fccb47SMitsuru IWASAKI 	    if (AcpiGbl_AcpiMutexInfo[ACPI_MTX_HARDWARE].OwnerId != ACPI_MUTEX_NOT_ACQUIRED) {
13536161544cSTakanori Watanabe 		AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
1354a1fccb47SMitsuru IWASAKI 	    }
13556161544cSTakanori Watanabe 
13566161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1357964679ceSMitsuru IWASAKI 	    if (state == ACPI_STATE_S4) {
1358964679ceSMitsuru IWASAKI 		AcpiEnable();
13596161544cSTakanori Watanabe 	    }
13606161544cSTakanori Watanabe 	} else {
1361b53f2771SMike Smith 	    if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
1362bfae45aaSMike Smith 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
1363c30382dfSMitsuru IWASAKI 		break;
136415e32d5dSMike Smith 	    }
13656161544cSTakanori Watanabe 	}
1366ff741befSTakanori Watanabe 	AcpiLeaveSleepState((UINT8)state);
136715e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
136815e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
136913d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
137015e32d5dSMike Smith 	break;
137115e32d5dSMike Smith 
137215e32d5dSMike Smith     case ACPI_STATE_S5:
137315e32d5dSMike Smith 	/*
137415e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
137515e32d5dSMike Smith 	 * shutdown handlers.
137615e32d5dSMike Smith 	 */
137715e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
137815e32d5dSMike Smith 	break;
137915e32d5dSMike Smith 
138015e32d5dSMike Smith     default:
138115e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
138215e32d5dSMike Smith 	break;
138315e32d5dSMike Smith     }
1384ece50487SMitsuru IWASAKI 
1385ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1386ece50487SMitsuru IWASAKI 	timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1387ece50487SMitsuru IWASAKI 
13880ae55423SMike Smith     return_ACPI_STATUS(status);
138915e32d5dSMike Smith }
139015e32d5dSMike Smith 
139115e32d5dSMike Smith /*
139215e32d5dSMike Smith  * Enable/Disable ACPI
139315e32d5dSMike Smith  */
139415e32d5dSMike Smith ACPI_STATUS
139515e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
139615e32d5dSMike Smith {
139715e32d5dSMike Smith     ACPI_STATUS	status;
139815e32d5dSMike Smith     u_int32_t	flags;
139915e32d5dSMike Smith 
1400b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1401cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
14020ae55423SMike Smith 
140315e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
140415e32d5dSMike Smith             ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
140515e32d5dSMike Smith     if (!sc->acpi_enabled) {
140615e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
140715e32d5dSMike Smith     } else {
140815e32d5dSMike Smith 	status = AE_OK;
140915e32d5dSMike Smith     }
141015e32d5dSMike Smith     if (status == AE_OK)
141115e32d5dSMike Smith 	sc->acpi_enabled = 1;
14120ae55423SMike Smith     return_ACPI_STATUS(status);
141315e32d5dSMike Smith }
141415e32d5dSMike Smith 
141515e32d5dSMike Smith ACPI_STATUS
141615e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
141715e32d5dSMike Smith {
141815e32d5dSMike Smith     ACPI_STATUS	status;
141915e32d5dSMike Smith 
1420b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1421cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
14220ae55423SMike Smith 
142315e32d5dSMike Smith     if (sc->acpi_enabled) {
142415e32d5dSMike Smith 	status = AcpiDisable();
142515e32d5dSMike Smith     } else {
142615e32d5dSMike Smith 	status = AE_OK;
142715e32d5dSMike Smith     }
142815e32d5dSMike Smith     if (status == AE_OK)
142915e32d5dSMike Smith 	sc->acpi_enabled = 0;
14300ae55423SMike Smith     return_ACPI_STATUS(status);
143115e32d5dSMike Smith }
143215e32d5dSMike Smith 
143315e32d5dSMike Smith /*
143415e32d5dSMike Smith  * ACPI Event Handlers
143515e32d5dSMike Smith  */
143615e32d5dSMike Smith 
143715e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
143815e32d5dSMike Smith 
143915e32d5dSMike Smith static void
144015e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
144115e32d5dSMike Smith {
1442b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
144315e32d5dSMike Smith 
1444cb9b0d80SMike Smith     ACPI_LOCK;
1445a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
144615e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
1447cb9b0d80SMike Smith     ACPI_UNLOCK;
14480ae55423SMike Smith     return_VOID;
144915e32d5dSMike Smith }
145015e32d5dSMike Smith 
145115e32d5dSMike Smith static void
145215e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
145315e32d5dSMike Smith {
1454b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
14550ae55423SMike Smith 
145615e32d5dSMike Smith     /* Well, what to do? :-) */
14570ae55423SMike Smith 
1458cb9b0d80SMike Smith     ACPI_LOCK;
1459cb9b0d80SMike Smith     ACPI_UNLOCK;
1460cb9b0d80SMike Smith 
14610ae55423SMike Smith     return_VOID;
146215e32d5dSMike Smith }
146315e32d5dSMike Smith 
146415e32d5dSMike Smith /*
146515e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
146615e32d5dSMike Smith  */
146715e32d5dSMike Smith UINT32
146815e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context)
146915e32d5dSMike Smith {
147015e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
147115e32d5dSMike Smith 
1472b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
14730ae55423SMike Smith 
147415e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
14750ae55423SMike Smith 
1476b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
147715e32d5dSMike Smith }
147815e32d5dSMike Smith 
147915e32d5dSMike Smith UINT32
148015e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context)
148115e32d5dSMike Smith {
148215e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
148315e32d5dSMike Smith 
1484b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
14850ae55423SMike Smith 
148615e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
14870ae55423SMike Smith 
1488b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
148915e32d5dSMike Smith }
149015e32d5dSMike Smith 
149115e32d5dSMike Smith UINT32
149215e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context)
149315e32d5dSMike Smith {
149415e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
149515e32d5dSMike Smith 
1496b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
14970ae55423SMike Smith 
149815e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
14990ae55423SMike Smith 
1500b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
150115e32d5dSMike Smith }
150215e32d5dSMike Smith 
150315e32d5dSMike Smith UINT32
150415e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context)
150515e32d5dSMike Smith {
150615e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
150715e32d5dSMike Smith 
1508b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15090ae55423SMike Smith 
151015e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
15110ae55423SMike Smith 
1512b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
151315e32d5dSMike Smith }
151415e32d5dSMike Smith 
151515e32d5dSMike Smith /*
151615e32d5dSMike Smith  * XXX This is kinda ugly, and should not be here.
151715e32d5dSMike Smith  */
151815e32d5dSMike Smith struct acpi_staticbuf {
151915e32d5dSMike Smith     ACPI_BUFFER	buffer;
152015e32d5dSMike Smith     char	data[512];
152115e32d5dSMike Smith };
152215e32d5dSMike Smith 
152315e32d5dSMike Smith char *
152415e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
152515e32d5dSMike Smith {
152615e32d5dSMike Smith     static struct acpi_staticbuf	buf;
152715e32d5dSMike Smith 
1528cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1529cb9b0d80SMike Smith 
153015e32d5dSMike Smith     buf.buffer.Length = 512;
153115e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
153215e32d5dSMike Smith 
1533b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
153415e32d5dSMike Smith 	return(buf.buffer.Pointer);
153515e32d5dSMike Smith     return("(unknown path)");
153615e32d5dSMike Smith }
153715e32d5dSMike Smith 
153815e32d5dSMike Smith /*
153915e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
154015e32d5dSMike Smith  * parts of the namespace.
154115e32d5dSMike Smith  */
154215e32d5dSMike Smith int
154315e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
154415e32d5dSMike Smith {
15453c600cfbSJohn Baldwin     char	*cp, *env, *np;
154615e32d5dSMike Smith     int		len;
154715e32d5dSMike Smith 
154815e32d5dSMike Smith     np = acpi_name(handle);
154915e32d5dSMike Smith     if (*np == '\\')
155015e32d5dSMike Smith 	np++;
15513c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
155215e32d5dSMike Smith 	return(0);
155315e32d5dSMike Smith 
155415e32d5dSMike Smith     /* scan the avoid list checking for a match */
15553c600cfbSJohn Baldwin     cp = env;
155615e32d5dSMike Smith     for (;;) {
155715e32d5dSMike Smith 	while ((*cp != 0) && isspace(*cp))
155815e32d5dSMike Smith 	    cp++;
155915e32d5dSMike Smith 	if (*cp == 0)
156015e32d5dSMike Smith 	    break;
156115e32d5dSMike Smith 	len = 0;
156215e32d5dSMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
156315e32d5dSMike Smith 	    len++;
1564d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
15653c600cfbSJohn Baldwin 	    freeenv(env);
15660ae55423SMike Smith 	    return(1);
1567d786139cSMaxime Henrion 	}
15680ae55423SMike Smith 	cp += len;
15690ae55423SMike Smith     }
15703c600cfbSJohn Baldwin     freeenv(env);
15710ae55423SMike Smith     return(0);
15720ae55423SMike Smith }
15730ae55423SMike Smith 
15740ae55423SMike Smith /*
15750ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
15760ae55423SMike Smith  */
15770ae55423SMike Smith int
15780ae55423SMike Smith acpi_disabled(char *subsys)
15790ae55423SMike Smith {
158078689b15SMaxime Henrion     char	*cp, *env;
15810ae55423SMike Smith     int		len;
15820ae55423SMike Smith 
158378689b15SMaxime Henrion     if ((env = getenv("debug.acpi.disable")) == NULL)
15840ae55423SMike Smith 	return(0);
158578689b15SMaxime Henrion     if (!strcmp(env, "all")) {
158678689b15SMaxime Henrion 	freeenv(env);
15870ae55423SMike Smith 	return(1);
1588d786139cSMaxime Henrion     }
15890ae55423SMike Smith 
15900ae55423SMike Smith     /* scan the disable list checking for a match */
159178689b15SMaxime Henrion     cp = env;
15920ae55423SMike Smith     for (;;) {
15930ae55423SMike Smith 	while ((*cp != 0) && isspace(*cp))
15940ae55423SMike Smith 	    cp++;
15950ae55423SMike Smith 	if (*cp == 0)
15960ae55423SMike Smith 	    break;
15970ae55423SMike Smith 	len = 0;
15980ae55423SMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
15990ae55423SMike Smith 	    len++;
1600d786139cSMaxime Henrion 	if (!strncmp(cp, subsys, len)) {
160178689b15SMaxime Henrion 	    freeenv(env);
160215e32d5dSMike Smith 	    return(1);
1603d786139cSMaxime Henrion 	}
160415e32d5dSMike Smith 	cp += len;
160515e32d5dSMike Smith     }
160678689b15SMaxime Henrion     freeenv(env);
160715e32d5dSMike Smith     return(0);
160815e32d5dSMike Smith }
160915e32d5dSMike Smith 
161015e32d5dSMike Smith /*
1611a1fccb47SMitsuru IWASAKI  * Device wake capability enable/disable.
1612a1fccb47SMitsuru IWASAKI  */
1613a1fccb47SMitsuru IWASAKI void
1614a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable)
1615a1fccb47SMitsuru IWASAKI {
1616a1fccb47SMitsuru IWASAKI     ACPI_OBJECT_LIST		ArgList;
1617a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			Arg;
1618a1fccb47SMitsuru IWASAKI 
1619a1fccb47SMitsuru IWASAKI     /*
1620a1fccb47SMitsuru IWASAKI      * TBD: All Power Resources referenced by elements 2 through N
1621a1fccb47SMitsuru IWASAKI      *      of the _PRW object are put into the ON state.
1622a1fccb47SMitsuru IWASAKI      */
1623a1fccb47SMitsuru IWASAKI 
1624a1fccb47SMitsuru IWASAKI     /*
1625a1fccb47SMitsuru IWASAKI      * enable/disable device wake function.
1626a1fccb47SMitsuru IWASAKI      */
1627a1fccb47SMitsuru IWASAKI 
1628a1fccb47SMitsuru IWASAKI     ArgList.Count = 1;
1629a1fccb47SMitsuru IWASAKI     ArgList.Pointer = &Arg;
1630a1fccb47SMitsuru IWASAKI 
1631a1fccb47SMitsuru IWASAKI     Arg.Type = ACPI_TYPE_INTEGER;
1632a1fccb47SMitsuru IWASAKI     Arg.Integer.Value = enable;
1633a1fccb47SMitsuru IWASAKI 
1634a1fccb47SMitsuru IWASAKI     (void)AcpiEvaluateObject(h, "_PSW", &ArgList, NULL);
1635a1fccb47SMitsuru IWASAKI }
1636a1fccb47SMitsuru IWASAKI 
1637a1fccb47SMitsuru IWASAKI void
1638a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_event(ACPI_HANDLE h)
1639a1fccb47SMitsuru IWASAKI {
1640a1fccb47SMitsuru IWASAKI     struct acpi_softc		*sc;
1641a1fccb47SMitsuru IWASAKI     ACPI_STATUS			status;
1642a1fccb47SMitsuru IWASAKI     ACPI_BUFFER			prw_buffer;
1643a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			*res;
1644a1fccb47SMitsuru IWASAKI 
1645a1fccb47SMitsuru IWASAKI     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1646a1fccb47SMitsuru IWASAKI 
1647a1fccb47SMitsuru IWASAKI     if ((sc = devclass_get_softc(acpi_devclass, 0)) == NULL) {
1648a1fccb47SMitsuru IWASAKI 	return;
1649a1fccb47SMitsuru IWASAKI     }
1650a1fccb47SMitsuru IWASAKI 
1651a1fccb47SMitsuru IWASAKI     /*
1652a1fccb47SMitsuru IWASAKI      * _PRW object is only required for devices that have the ability
1653a1fccb47SMitsuru IWASAKI      * to wake the system from a system sleeping state.
1654a1fccb47SMitsuru IWASAKI      */
1655a1fccb47SMitsuru IWASAKI     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
1656a1fccb47SMitsuru IWASAKI     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
1657a1fccb47SMitsuru IWASAKI     if (ACPI_FAILURE(status)) {
1658a1fccb47SMitsuru IWASAKI 	return;
1659a1fccb47SMitsuru IWASAKI     }
1660a1fccb47SMitsuru IWASAKI 
1661a1fccb47SMitsuru IWASAKI     res = (ACPI_OBJECT *)prw_buffer.Pointer;
1662a1fccb47SMitsuru IWASAKI     if (res == NULL) {
1663a1fccb47SMitsuru IWASAKI 	return;
1664a1fccb47SMitsuru IWASAKI     }
1665a1fccb47SMitsuru IWASAKI 
1666a1fccb47SMitsuru IWASAKI     if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) {
1667a1fccb47SMitsuru IWASAKI 	goto out;
1668a1fccb47SMitsuru IWASAKI     }
1669a1fccb47SMitsuru IWASAKI 
1670a1fccb47SMitsuru IWASAKI     /*
1671a1fccb47SMitsuru IWASAKI      * The element 1 of the _PRW object:
1672a1fccb47SMitsuru IWASAKI      * The lowest power system sleeping state that can be entered
1673a1fccb47SMitsuru IWASAKI      * while still providing wake functionality.
1674a1fccb47SMitsuru IWASAKI      * The sleeping state being entered must be greater or equal to
1675a1fccb47SMitsuru IWASAKI      * the power state declared in element 1 of the _PRW object.
1676a1fccb47SMitsuru IWASAKI      */
1677a1fccb47SMitsuru IWASAKI     if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER) {
1678a1fccb47SMitsuru IWASAKI 	goto out;
1679a1fccb47SMitsuru IWASAKI     }
1680a1fccb47SMitsuru IWASAKI 
1681a1fccb47SMitsuru IWASAKI     if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value) {
1682a1fccb47SMitsuru IWASAKI 	goto out;
1683a1fccb47SMitsuru IWASAKI     }
1684a1fccb47SMitsuru IWASAKI 
1685a1fccb47SMitsuru IWASAKI     /*
1686a1fccb47SMitsuru IWASAKI      * The element 0 of the _PRW object:
1687a1fccb47SMitsuru IWASAKI      */
1688a1fccb47SMitsuru IWASAKI     switch(res->Package.Elements[0].Type) {
1689a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_INTEGER:
1690a1fccb47SMitsuru IWASAKI 	/*
1691a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is numeric, then this
1692a1fccb47SMitsuru IWASAKI 	 * _PRW package element is the bit index in the GPEx_EN, in the
1693a1fccb47SMitsuru IWASAKI 	 * GPE blocks described in the FADT, of the enable bit that is
1694a1fccb47SMitsuru IWASAKI 	 * enabled for the wake event.
1695a1fccb47SMitsuru IWASAKI 	 */
1696a1fccb47SMitsuru IWASAKI 
1697a1fccb47SMitsuru IWASAKI 	status = AcpiEnableEvent(res->Package.Elements[0].Integer.Value,
1698a1fccb47SMitsuru IWASAKI 				 ACPI_EVENT_GPE, ACPI_EVENT_WAKE_ENABLE);
1699a1fccb47SMitsuru IWASAKI 	if (ACPI_FAILURE(status))
1700a1fccb47SMitsuru IWASAKI             printf("%s: EnableEvent Failed\n", __func__);
1701a1fccb47SMitsuru IWASAKI 	break;
1702a1fccb47SMitsuru IWASAKI 
1703a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_PACKAGE:
1704a1fccb47SMitsuru IWASAKI 	/* XXX TBD */
1705a1fccb47SMitsuru IWASAKI 
1706a1fccb47SMitsuru IWASAKI 	/*
1707a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is a package, then this
1708a1fccb47SMitsuru IWASAKI 	 * _PRW package element is itself a package containing two
1709a1fccb47SMitsuru IWASAKI 	 * elements. The first is an object reference to the GPE Block
1710a1fccb47SMitsuru IWASAKI 	 * device that contains the GPE that will be triggered by the wake
1711a1fccb47SMitsuru IWASAKI 	 * event. The second element is numeric and it contains the bit
1712a1fccb47SMitsuru IWASAKI 	 * index in the GPEx_EN, in the GPE Block referenced by the
1713a1fccb47SMitsuru IWASAKI 	 * first element in the package, of the enable bit that is enabled for
1714a1fccb47SMitsuru IWASAKI 	 * the wake event.
1715a1fccb47SMitsuru IWASAKI 	 * For example, if this field is a package then it is of the form:
1716a1fccb47SMitsuru IWASAKI 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
1717a1fccb47SMitsuru IWASAKI 	 */
1718a1fccb47SMitsuru IWASAKI 
1719a1fccb47SMitsuru IWASAKI 	break;
1720a1fccb47SMitsuru IWASAKI 
1721a1fccb47SMitsuru IWASAKI     default:
1722a1fccb47SMitsuru IWASAKI 	break;
1723a1fccb47SMitsuru IWASAKI     }
1724a1fccb47SMitsuru IWASAKI 
1725a1fccb47SMitsuru IWASAKI out:
1726a1fccb47SMitsuru IWASAKI     if (prw_buffer.Pointer != NULL)
1727a1fccb47SMitsuru IWASAKI 	AcpiOsFree(prw_buffer.Pointer);
1728a1fccb47SMitsuru IWASAKI     return;
1729a1fccb47SMitsuru IWASAKI }
1730a1fccb47SMitsuru IWASAKI 
1731a1fccb47SMitsuru IWASAKI /*
173215e32d5dSMike Smith  * Control interface.
173315e32d5dSMike Smith  *
17340ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
17350ae55423SMike Smith  * drivers wanting to be accessible via /dev/acpi should use the register/deregister
17360ae55423SMike Smith  * interface to make their handlers visible.
173715e32d5dSMike Smith  */
17380ae55423SMike Smith struct acpi_ioctl_hook
17390ae55423SMike Smith {
17400ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook)	link;
17410ae55423SMike Smith     u_long				cmd;
17420ae55423SMike Smith     int					(* fn)(u_long cmd, caddr_t addr, void *arg);
17430ae55423SMike Smith     void				*arg;
17440ae55423SMike Smith };
17450ae55423SMike Smith 
17460ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
17470ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
17480ae55423SMike Smith 
17490ae55423SMike Smith /*
17500ae55423SMike Smith  * Register an ioctl handler.
17510ae55423SMike Smith  */
17520ae55423SMike Smith int
17530ae55423SMike Smith acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg)
17540ae55423SMike Smith {
17550ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
17560ae55423SMike Smith 
17570ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
17580ae55423SMike Smith 	return(ENOMEM);
17590ae55423SMike Smith     hp->cmd = cmd;
17600ae55423SMike Smith     hp->fn = fn;
17610ae55423SMike Smith     hp->arg = arg;
17620ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
17630ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
17640ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
17650ae55423SMike Smith     }
17660ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
17670ae55423SMike Smith     return(0);
17680ae55423SMike Smith }
17690ae55423SMike Smith 
17700ae55423SMike Smith /*
17710ae55423SMike Smith  * Deregister an ioctl handler.
17720ae55423SMike Smith  */
17730ae55423SMike Smith void
17740ae55423SMike Smith acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg))
17750ae55423SMike Smith {
17760ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
17770ae55423SMike Smith 
17780ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
17790ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
17800ae55423SMike Smith 	    break;
17810ae55423SMike Smith 
17820ae55423SMike Smith     if (hp != NULL) {
17830ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
17840ae55423SMike Smith 	free(hp, M_ACPIDEV);
17850ae55423SMike Smith     }
17860ae55423SMike Smith }
17870ae55423SMike Smith 
178815e32d5dSMike Smith static int
1789b40ce416SJulian Elischer acpiopen(dev_t dev, int flag, int fmt, struct thread *td)
179015e32d5dSMike Smith {
179115e32d5dSMike Smith     return(0);
179215e32d5dSMike Smith }
179315e32d5dSMike Smith 
179415e32d5dSMike Smith static int
1795b40ce416SJulian Elischer acpiclose(dev_t dev, int flag, int fmt, struct thread *td)
179615e32d5dSMike Smith {
179715e32d5dSMike Smith     return(0);
179815e32d5dSMike Smith }
179915e32d5dSMike Smith 
180015e32d5dSMike Smith static int
1801b40ce416SJulian Elischer acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
180215e32d5dSMike Smith {
180315e32d5dSMike Smith     struct acpi_softc		*sc;
18040ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
18050ae55423SMike Smith     int				error, xerror, state;
180615e32d5dSMike Smith 
1807cb9b0d80SMike Smith     ACPI_LOCK;
1808cb9b0d80SMike Smith 
180915e32d5dSMike Smith     error = state = 0;
181015e32d5dSMike Smith     sc = dev->si_drv1;
181115e32d5dSMike Smith 
18120ae55423SMike Smith     /*
18130ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
18140ae55423SMike Smith      */
18150ae55423SMike Smith     if (acpi_ioctl_hooks_initted) {
18160ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
18170ae55423SMike Smith 	    if (hp->cmd == cmd) {
18180ae55423SMike Smith 		xerror = hp->fn(cmd, addr, hp->arg);
18190ae55423SMike Smith 		if (xerror != 0)
18200ae55423SMike Smith 		    error = xerror;
1821917d44c8SMitsuru IWASAKI 		goto out;
18220ae55423SMike Smith 	    }
18230ae55423SMike Smith 	}
18240ae55423SMike Smith     }
18250ae55423SMike Smith 
18260ae55423SMike Smith     /*
18270ae55423SMike Smith      * Core system ioctls.
18280ae55423SMike Smith      */
182915e32d5dSMike Smith     switch (cmd) {
183015e32d5dSMike Smith     case ACPIIO_ENABLE:
18310ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
183215e32d5dSMike Smith 	    error = ENXIO;
183315e32d5dSMike Smith 	break;
183415e32d5dSMike Smith 
183515e32d5dSMike Smith     case ACPIIO_DISABLE:
18360ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
183715e32d5dSMike Smith 	    error = ENXIO;
183815e32d5dSMike Smith 	break;
183915e32d5dSMike Smith 
184015e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
184115e32d5dSMike Smith 	if (!sc->acpi_enabled) {
184215e32d5dSMike Smith 	    error = ENXIO;
184315e32d5dSMike Smith 	    break;
184415e32d5dSMike Smith 	}
184515e32d5dSMike Smith 	state = *(int *)addr;
1846a5d1879bSMitsuru IWASAKI 	if (state >= ACPI_STATE_S0  && state <= ACPI_S_STATES_MAX) {
184715e32d5dSMike Smith 	    acpi_SetSleepState(sc, state);
184815e32d5dSMike Smith 	} else {
184915e32d5dSMike Smith 	    error = EINVAL;
185015e32d5dSMike Smith 	}
185115e32d5dSMike Smith 	break;
185215e32d5dSMike Smith 
185315e32d5dSMike Smith     default:
18540ae55423SMike Smith 	if (error == 0)
185515e32d5dSMike Smith 	    error = EINVAL;
185615e32d5dSMike Smith 	break;
185715e32d5dSMike Smith     }
1858917d44c8SMitsuru IWASAKI 
1859917d44c8SMitsuru IWASAKI out:
1860cb9b0d80SMike Smith     ACPI_UNLOCK;
186115e32d5dSMike Smith     return(error);
186215e32d5dSMike Smith }
186315e32d5dSMike Smith 
18641d073b1dSJohn Baldwin static int
18651d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
18661d073b1dSJohn Baldwin {
18671d073b1dSJohn Baldwin     char sleep_state[10];
18681d073b1dSJohn Baldwin     int error;
18691d073b1dSJohn Baldwin     u_int new_state, old_state;
18701d073b1dSJohn Baldwin 
18711d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
1872b8670bedSMitsuru IWASAKI     if (old_state > ACPI_S_STATES_MAX+1) {
18731d073b1dSJohn Baldwin 	strcpy(sleep_state, "unknown");
1874cb9b0d80SMike Smith     } else {
1875b8670bedSMitsuru IWASAKI 	bzero(sleep_state, sizeof(sleep_state));
18761d073b1dSJohn Baldwin 	strncpy(sleep_state, sleep_state_names[old_state],
18771d073b1dSJohn Baldwin 		sizeof(sleep_state_names[old_state]));
1878cb9b0d80SMike Smith     }
18791d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
18801d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
1881b8670bedSMitsuru IWASAKI 	for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX+1; new_state++) {
18821d073b1dSJohn Baldwin 	    if (strncmp(sleep_state, sleep_state_names[new_state],
18831d073b1dSJohn Baldwin 			sizeof(sleep_state)) == 0)
18841d073b1dSJohn Baldwin 		break;
1885cb9b0d80SMike Smith 	}
1886931a10c9SMitsuru IWASAKI 	if (new_state <= ACPI_S_STATES_MAX+1) {
1887931a10c9SMitsuru IWASAKI 	    if (new_state != old_state) {
18881d073b1dSJohn Baldwin 		*(u_int *)oidp->oid_arg1 = new_state;
1889931a10c9SMitsuru IWASAKI 	    }
1890cb9b0d80SMike Smith 	} else {
18911d073b1dSJohn Baldwin 	    error = EINVAL;
18921d073b1dSJohn Baldwin 	}
1893cb9b0d80SMike Smith     }
18941d073b1dSJohn Baldwin     return(error);
18951d073b1dSJohn Baldwin }
18961d073b1dSJohn Baldwin 
189715e32d5dSMike Smith #ifdef ACPI_DEBUG
18980ae55423SMike Smith /*
18990ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
19000ae55423SMike Smith  *
19010ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
19020ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
19030ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
19040ae55423SMike Smith  * prefixing the bit name with !.
19050ae55423SMike Smith  */
190615e32d5dSMike Smith struct debugtag
190715e32d5dSMike Smith {
190815e32d5dSMike Smith     char	*name;
190915e32d5dSMike Smith     UINT32	value;
191015e32d5dSMike Smith };
191115e32d5dSMike Smith 
191215e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
1913c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
1914c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
1915c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
1916c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
1917c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
1918c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
1919c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
1920c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
1921c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
1922d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
19234c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
1924d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
19254c1cdee6SMike Smith 
1926cb9b0d80SMike Smith     {"ACPI_BUS",		ACPI_BUS},
19274c1cdee6SMike Smith     {"ACPI_SYSTEM",		ACPI_SYSTEM},
1928cb9b0d80SMike Smith     {"ACPI_POWER",		ACPI_POWER},
1929cb9b0d80SMike Smith     {"ACPI_EC", 		ACPI_EC},
1930c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
1931c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
1932c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
19334c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
1934cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
19354c1cdee6SMike Smith     {"ACPI_FAN",		ACPI_FAN},
19364c1cdee6SMike Smith 
1937b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
1938c5ba0be4SMike Smith     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
193915e32d5dSMike Smith     {NULL, 0}
194015e32d5dSMike Smith };
194115e32d5dSMike Smith 
194215e32d5dSMike Smith static struct debugtag dbg_level[] = {
19434c1cdee6SMike Smith     {"ACPI_LV_OK",		ACPI_LV_OK},
19444c1cdee6SMike Smith     {"ACPI_LV_INFO",		ACPI_LV_INFO},
19454c1cdee6SMike Smith     {"ACPI_LV_WARN",		ACPI_LV_WARN},
19464c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
19474c1cdee6SMike Smith     {"ACPI_LV_FATAL",		ACPI_LV_FATAL},
19484c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
19494c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
1950d62ab2f4SMitsuru IWASAKI 
1951d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
19524c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
19534c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
1954d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
19554c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
19564c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
19574c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
19584c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
19594c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
19604c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
19614c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
19624c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
19634c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
19644c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
19654c1cdee6SMike Smith     {"ACPI_LV_INIT",		ACPI_LV_INIT},
1966d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
1967d62ab2f4SMitsuru IWASAKI 
1968d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
1969d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
1970d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
1971d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
1972d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
19734c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
1974d62ab2f4SMitsuru IWASAKI 
1975d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
1976d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
1977d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
1978d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
1979d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
1980d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
1981d62ab2f4SMitsuru IWASAKI 
1982d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
198398479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
198498479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
198598479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
198698479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
198798479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
198815e32d5dSMike Smith     {NULL, 0}
198915e32d5dSMike Smith };
199015e32d5dSMike Smith 
199115e32d5dSMike Smith static void
199215e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
199315e32d5dSMike Smith {
199415e32d5dSMike Smith     char	*ep;
199515e32d5dSMike Smith     int		i, l;
19960ae55423SMike Smith     int		set;
199715e32d5dSMike Smith 
199815e32d5dSMike Smith     while (*cp) {
199915e32d5dSMike Smith 	if (isspace(*cp)) {
200015e32d5dSMike Smith 	    cp++;
200115e32d5dSMike Smith 	    continue;
200215e32d5dSMike Smith 	}
200315e32d5dSMike Smith 	ep = cp;
200415e32d5dSMike Smith 	while (*ep && !isspace(*ep))
200515e32d5dSMike Smith 	    ep++;
20060ae55423SMike Smith 	if (*cp == '!') {
20070ae55423SMike Smith 	    set = 0;
20080ae55423SMike Smith 	    cp++;
20090ae55423SMike Smith 	    if (cp == ep)
20100ae55423SMike Smith 		continue;
20110ae55423SMike Smith 	} else {
20120ae55423SMike Smith 	    set = 1;
20130ae55423SMike Smith 	}
201415e32d5dSMike Smith 	l = ep - cp;
201515e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
201615e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
20170ae55423SMike Smith 		if (set) {
201815e32d5dSMike Smith 		    *flag |= tag[i].value;
20190ae55423SMike Smith 		} else {
20200ae55423SMike Smith 		    *flag &= ~tag[i].value;
20210ae55423SMike Smith 		}
202215e32d5dSMike Smith 		printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
202315e32d5dSMike Smith 	    }
202415e32d5dSMike Smith 	}
202515e32d5dSMike Smith 	cp = ep;
202615e32d5dSMike Smith     }
202715e32d5dSMike Smith }
202815e32d5dSMike Smith 
202915e32d5dSMike Smith static void
20302a4ac806SMike Smith acpi_set_debugging(void *junk)
203115e32d5dSMike Smith {
203294aae282SMike Barcroft     char	*cp;
203315e32d5dSMike Smith 
20340ae55423SMike Smith     AcpiDbgLayer = 0;
20350ae55423SMike Smith     AcpiDbgLevel = 0;
203694aae282SMike Barcroft     if ((cp = getenv("debug.acpi.layer")) != NULL) {
203715e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
203894aae282SMike Barcroft 	freeenv(cp);
203994aae282SMike Barcroft     }
204094aae282SMike Barcroft     if ((cp = getenv("debug.acpi.level")) != NULL) {
204115e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
204294aae282SMike Barcroft 	freeenv(cp);
204394aae282SMike Barcroft     }
20440ae55423SMike Smith 
20450ae55423SMike Smith     printf("ACPI debug layer 0x%x  debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel);
204615e32d5dSMike Smith }
20472a4ac806SMike Smith SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL);
204815e32d5dSMike Smith #endif
2049f9390180SMitsuru IWASAKI 
2050f9390180SMitsuru IWASAKI static int
2051f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
2052f9390180SMitsuru IWASAKI {
2053f9390180SMitsuru IWASAKI 	int	state, acpi_state;
2054f9390180SMitsuru IWASAKI 	int	error;
2055f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
2056f9390180SMitsuru IWASAKI 	va_list	ap;
2057f9390180SMitsuru IWASAKI 
2058f9390180SMitsuru IWASAKI 	error = 0;
2059f9390180SMitsuru IWASAKI 	switch (cmd) {
2060f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
2061f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
2062f9390180SMitsuru IWASAKI 		if (sc == NULL) {
2063f9390180SMitsuru IWASAKI 			error = EINVAL;
2064f9390180SMitsuru IWASAKI 			goto out;
2065f9390180SMitsuru IWASAKI 		}
2066f9390180SMitsuru IWASAKI 
2067f9390180SMitsuru IWASAKI 		va_start(ap, arg);
2068f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
2069f9390180SMitsuru IWASAKI 		va_end(ap);
2070f9390180SMitsuru IWASAKI 
2071f9390180SMitsuru IWASAKI 		switch (state) {
2072f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
2073f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
2074f9390180SMitsuru IWASAKI 			break;
2075f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
2076f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
2077f9390180SMitsuru IWASAKI 			break;
2078f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
2079f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
2080f9390180SMitsuru IWASAKI 			break;
2081f9390180SMitsuru IWASAKI 		default:
2082f9390180SMitsuru IWASAKI 			error = EINVAL;
2083f9390180SMitsuru IWASAKI 			goto out;
2084f9390180SMitsuru IWASAKI 		}
2085f9390180SMitsuru IWASAKI 
2086f9390180SMitsuru IWASAKI 		acpi_SetSleepState(sc, acpi_state);
2087f9390180SMitsuru IWASAKI 		break;
2088f9390180SMitsuru IWASAKI 
2089f9390180SMitsuru IWASAKI 	default:
2090f9390180SMitsuru IWASAKI 		error = EINVAL;
2091f9390180SMitsuru IWASAKI 		goto out;
2092f9390180SMitsuru IWASAKI 	}
2093f9390180SMitsuru IWASAKI 
2094f9390180SMitsuru IWASAKI out:
2095f9390180SMitsuru IWASAKI 	return (error);
2096f9390180SMitsuru IWASAKI }
2097f9390180SMitsuru IWASAKI 
2098f9390180SMitsuru IWASAKI static void
2099f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
2100f9390180SMitsuru IWASAKI {
21016c407052SMitsuru IWASAKI 	int	error;
21026c407052SMitsuru IWASAKI 
21036c407052SMitsuru IWASAKI     if (!resource_int_value("acpi", 0, "disabled", &error) &&
21046c407052SMitsuru IWASAKI        (error != 0))
21056c407052SMitsuru IWASAKI 		return;
2106f9390180SMitsuru IWASAKI 
2107f9390180SMitsuru IWASAKI 	power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
2108f9390180SMitsuru IWASAKI }
2109f9390180SMitsuru IWASAKI 
2110f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
2111f9390180SMitsuru IWASAKI 
2112