xref: /freebsd/sys/dev/acpica/acpi.c (revision b53f27716720fc5e6b458b280793d8d2160d36e1)
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_resources(struct resource_list *rl, const char *name, int type,
10715e32d5dSMike Smith 				     const char *format);
10815e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
10915e32d5dSMike Smith static int	acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result);
11015e32d5dSMike Smith static int	acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value);
11115e32d5dSMike Smith static int	acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start,
11215e32d5dSMike Smith 				  u_long count);
11315e32d5dSMike Smith static int	acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp,
11415e32d5dSMike Smith 				  u_long *countp);
11515e32d5dSMike Smith static struct resource *acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
11615e32d5dSMike Smith 					    u_long start, u_long end, u_long count, u_int flags);
11715e32d5dSMike Smith static int	acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r);
11832d18aa5SMike Smith static u_int32_t acpi_isa_get_logicalid(device_t dev);
119bc0f2195SMike Smith static int	acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids);
12015e32d5dSMike Smith 
12115e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
12215e32d5dSMike Smith static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status);
12315e32d5dSMike Smith 
12415e32d5dSMike Smith static void	acpi_shutdown_pre_sync(void *arg, int howto);
12515e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
12615e32d5dSMike Smith 
12713d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
12813d4f7baSMitsuru IWASAKI 
12915e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
13015e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
1311d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
13215e32d5dSMike Smith 
133f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
134f9390180SMitsuru IWASAKI 
13515e32d5dSMike Smith static device_method_t acpi_methods[] = {
13615e32d5dSMike Smith     /* Device interface */
13715e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
13815e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
13915e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
14015e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
14115e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
14215e32d5dSMike Smith     DEVMETHOD(device_resume,		bus_generic_resume),
14315e32d5dSMike Smith 
14415e32d5dSMike Smith     /* Bus interface */
14515e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
14615e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
14715e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
14815e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
14915e32d5dSMike Smith     DEVMETHOD(bus_set_resource,		acpi_set_resource),
15015e32d5dSMike Smith     DEVMETHOD(bus_get_resource,		acpi_get_resource),
15115e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
15215e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
15315e32d5dSMike Smith     DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
15415e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
15515e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
15615e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
15715e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
15815e32d5dSMike Smith 
159bc0f2195SMike Smith     /* ISA emulation */
160bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
161bc0f2195SMike Smith 
16215e32d5dSMike Smith     {0, 0}
16315e32d5dSMike Smith };
16415e32d5dSMike Smith 
16515e32d5dSMike Smith static driver_t acpi_driver = {
16615e32d5dSMike Smith     "acpi",
16715e32d5dSMike Smith     acpi_methods,
16815e32d5dSMike Smith     sizeof(struct acpi_softc),
16915e32d5dSMike Smith };
17015e32d5dSMike Smith 
1713273b005SMike Smith static devclass_t acpi_devclass;
1726d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
173dde24897SMike Smith MODULE_VERSION(acpi, 100);
17415e32d5dSMike Smith 
17515e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, "");
17615e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, "");
177dde24897SMike Smith static int acpi_ca_version = ACPI_CA_VERSION;
178dd081ed5SMitsuru IWASAKI SYSCTL_INT(_debug, OID_AUTO, acpi_ca_version, CTLFLAG_RD, &acpi_ca_version, 0, "");
17915e32d5dSMike Smith 
18015e32d5dSMike Smith /*
1816d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
1826d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
1836d63101aSMike Smith  * It also cannot be unloaded, since the entire system bus heirarchy hangs off it.
1846d63101aSMike Smith  */
1856d63101aSMike Smith static int
1866d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
1876d63101aSMike Smith {
1886d63101aSMike Smith     switch(event) {
1896d63101aSMike Smith     case MOD_LOAD:
1906d63101aSMike Smith 	if (!cold)
1916d63101aSMike Smith 	    return(EPERM);
1926d63101aSMike Smith 	break;
1936d63101aSMike Smith     case MOD_UNLOAD:
194f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
1956d63101aSMike Smith 	    return(EBUSY);
196f9390180SMitsuru IWASAKI 	break;
1976d63101aSMike Smith     default:
1986d63101aSMike Smith 	break;
1996d63101aSMike Smith     }
2006d63101aSMike Smith     return(0);
2016d63101aSMike Smith }
2026d63101aSMike Smith 
2036d63101aSMike Smith /*
20415e32d5dSMike Smith  * Detect ACPI, perform early initialisation
20515e32d5dSMike Smith  */
20615e32d5dSMike Smith static void
20715e32d5dSMike Smith acpi_identify(driver_t *driver, device_t parent)
20815e32d5dSMike Smith {
20915e32d5dSMike Smith     device_t			child;
21015e32d5dSMike Smith     int				error;
2111611ea87SMitsuru IWASAKI     caddr_t			acpi_dsdt, p;
21215e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
21315e32d5dSMike Smith     char			*debugpoint = getenv("debug.acpi.debugger");
21415e32d5dSMike Smith #endif
21515e32d5dSMike Smith 
216b53f2771SMike Smith     ACPI_FUNCTION_TRACE(__func__);
2170ae55423SMike Smith 
218840f9b53STakanori Watanabe     if(!cold){
219840f9b53STakanori Watanabe 	    printf("Don't load this driver from userland!!\n");
220840f9b53STakanori Watanabe 	    return ;
221840f9b53STakanori Watanabe     }
222840f9b53STakanori Watanabe 
22315e32d5dSMike Smith     /*
22432d18aa5SMike Smith      * Check that we haven't been disabled with a hint.
22532d18aa5SMike Smith      */
22632d18aa5SMike Smith     if (!resource_int_value("acpi", 0, "disabled", &error) &&
22732d18aa5SMike Smith 	(error != 0))
22832d18aa5SMike Smith 	return_VOID;
22932d18aa5SMike Smith 
23032d18aa5SMike Smith     /*
23115e32d5dSMike Smith      * Make sure we're not being doubly invoked.
23215e32d5dSMike Smith      */
23315e32d5dSMike Smith     if (device_find_child(parent, "acpi", 0) != NULL)
2340ae55423SMike Smith 	return_VOID;
23515e32d5dSMike Smith 
236cb9b0d80SMike Smith     /* initialise the ACPI mutex */
237cb9b0d80SMike Smith     mtx_init(&acpi_mutex, "ACPI global lock", MTX_DEF);
238cb9b0d80SMike Smith 
23915e32d5dSMike Smith     /*
2402a4ac806SMike Smith      * Start up the ACPI CA subsystem.
24115e32d5dSMike Smith      */
24215e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
24315e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "init"))
24415e32d5dSMike Smith 	acpi_EnterDebugger();
24515e32d5dSMike Smith #endif
246b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
247bfae45aaSMike Smith 	printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
2480ae55423SMike Smith 	return_VOID;
24915e32d5dSMike Smith     }
25015e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
25115e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "tables"))
25215e32d5dSMike Smith 	acpi_EnterDebugger();
25315e32d5dSMike Smith #endif
2541611ea87SMitsuru IWASAKI 
2551611ea87SMitsuru IWASAKI     if ((acpi_dsdt = preload_search_by_type("acpi_dsdt")) != NULL) {
2561611ea87SMitsuru IWASAKI         if ((p = preload_search_info(acpi_dsdt, MODINFO_ADDR)) != NULL) {
257b53f2771SMike Smith 	    if (ACPI_FAILURE(error = AcpiSetDsdtTablePtr(*(void **)p))) {
2581611ea87SMitsuru IWASAKI 		printf("ACPI: DSDT overriding failed: %s\n",
2591611ea87SMitsuru IWASAKI 		       AcpiFormatException(error));
2601611ea87SMitsuru IWASAKI 	    } else {
2611611ea87SMitsuru IWASAKI 		printf("ACPI: DSDT was overridden.\n");
2621611ea87SMitsuru IWASAKI 	    }
2631611ea87SMitsuru IWASAKI         }
2641611ea87SMitsuru IWASAKI     }
2651611ea87SMitsuru IWASAKI 
266b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiLoadTables())) {
267bfae45aaSMike Smith 	printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
2680ae55423SMike Smith 	return_VOID;
26915e32d5dSMike Smith     }
27015e32d5dSMike Smith 
27115e32d5dSMike Smith     /*
27215e32d5dSMike Smith      * Attach the actual ACPI device.
27315e32d5dSMike Smith      */
27415e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
27515e32d5dSMike Smith 	    device_printf(parent, "ACPI: could not attach\n");
2760ae55423SMike Smith 	    return_VOID;
27715e32d5dSMike Smith     }
27815e32d5dSMike Smith }
27915e32d5dSMike Smith 
28015e32d5dSMike Smith /*
28115e32d5dSMike Smith  * Fetch some descriptive data from ACPI to put in our attach message
28215e32d5dSMike Smith  */
28315e32d5dSMike Smith static int
28415e32d5dSMike Smith acpi_probe(device_t dev)
28515e32d5dSMike Smith {
28615e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
28715e32d5dSMike Smith     char		buf[20];
288cb9b0d80SMike Smith     ACPI_STATUS		status;
28915e32d5dSMike Smith     int			error;
29015e32d5dSMike Smith 
291b53f2771SMike Smith     ACPI_FUNCTION_TRACE(__func__);
292f9390180SMitsuru IWASAKI 
293f9390180SMitsuru IWASAKI     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
294f9390180SMitsuru IWASAKI         power_pm_get_type() != POWER_PM_TYPE_ACPI) {
295f9390180SMitsuru IWASAKI 	device_printf(dev, "Other PM system enabled.\n");
296f9390180SMitsuru IWASAKI 	return_VALUE(ENXIO);
297f9390180SMitsuru IWASAKI     }
298f9390180SMitsuru IWASAKI 
299cb9b0d80SMike Smith     ACPI_LOCK;
3000ae55423SMike Smith 
301b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
302bfae45aaSMike Smith 	device_printf(dev, "couldn't get XSDT header: %s\n", AcpiFormatException(status));
303cb9b0d80SMike Smith 	error = ENXIO;
304cb9b0d80SMike Smith     } else {
30515e32d5dSMike Smith 	sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId);
30615e32d5dSMike Smith 	device_set_desc_copy(dev, buf);
307cb9b0d80SMike Smith 	error = 0;
308cb9b0d80SMike Smith     }
309cb9b0d80SMike Smith     ACPI_UNLOCK;
310cb9b0d80SMike Smith     return_VALUE(error);
31115e32d5dSMike Smith }
31215e32d5dSMike Smith 
31315e32d5dSMike Smith static int
31415e32d5dSMike Smith acpi_attach(device_t dev)
31515e32d5dSMike Smith {
31615e32d5dSMike Smith     struct acpi_softc	*sc;
317cb9b0d80SMike Smith     ACPI_STATUS		status;
31815e32d5dSMike Smith     int			error;
31932d18aa5SMike Smith     UINT32		flags;
32032d18aa5SMike Smith 
32115e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
32215e32d5dSMike Smith     char		*debugpoint = getenv("debug.acpi.debugger");
32315e32d5dSMike Smith #endif
32415e32d5dSMike Smith 
325b53f2771SMike Smith     ACPI_FUNCTION_TRACE(__func__);
326cb9b0d80SMike Smith     ACPI_LOCK;
32715e32d5dSMike Smith     sc = device_get_softc(dev);
32815e32d5dSMike Smith     bzero(sc, sizeof(*sc));
32915e32d5dSMike Smith     sc->acpi_dev = dev;
33015e32d5dSMike Smith 
33115e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
33215e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "spaces"))
33315e32d5dSMike Smith 	acpi_EnterDebugger();
33415e32d5dSMike Smith #endif
33515e32d5dSMike Smith 
33615e32d5dSMike Smith     /*
33715e32d5dSMike Smith      * Install the default address space handlers.
33815e32d5dSMike Smith      */
339cb9b0d80SMike Smith     error = ENXIO;
340b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
3412a4ac806SMike Smith 						ACPI_ADR_SPACE_SYSTEM_MEMORY,
34215e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
343b53f2771SMike Smith 						NULL, NULL))) {
344bfae45aaSMike Smith 	device_printf(dev, "could not initialise SystemMemory handler: %s\n", AcpiFormatException(status));
345cb9b0d80SMike Smith 	goto out;
34615e32d5dSMike Smith     }
347b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
3482a4ac806SMike Smith 						ACPI_ADR_SPACE_SYSTEM_IO,
34915e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
350b53f2771SMike Smith 						NULL, NULL))) {
351bfae45aaSMike Smith 	device_printf(dev, "could not initialise SystemIO handler: %s\n", AcpiFormatException(status));
352cb9b0d80SMike Smith 	goto out;
35315e32d5dSMike Smith     }
354b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
3552a4ac806SMike Smith 						ACPI_ADR_SPACE_PCI_CONFIG,
35615e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
357b53f2771SMike Smith 						NULL, NULL))) {
358bfae45aaSMike Smith 	device_printf(dev, "could not initialise PciConfig handler: %s\n", AcpiFormatException(status));
359cb9b0d80SMike Smith 	goto out;
36015e32d5dSMike Smith     }
36115e32d5dSMike Smith 
36215e32d5dSMike Smith     /*
36315e32d5dSMike Smith      * Bring ACPI fully online.
36415e32d5dSMike Smith      *
36532d18aa5SMike Smith      * Note that some systems (specifically, those with namespace evaluation issues
36632d18aa5SMike Smith      * that require the avoidance of parts of the namespace) must avoid running _INI
36732d18aa5SMike Smith      * and _STA on everything, as well as dodging the final object init pass.
36815e32d5dSMike Smith      *
36932d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
37032d18aa5SMike Smith      *
37132d18aa5SMike Smith      * XXX We should arrange for the object init pass after we have attached all our
37232d18aa5SMike Smith      *     child devices, but on many systems it works here.
37315e32d5dSMike Smith      */
37415e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
37515e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "enable"))
37615e32d5dSMike Smith 	acpi_EnterDebugger();
37715e32d5dSMike Smith #endif
37832d18aa5SMike Smith     flags = 0;
379b525621aSMike Smith     if (getenv("debug.acpi.avoid") != NULL)
38032d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
381b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
382bfae45aaSMike Smith 	device_printf(dev, "could not enable ACPI: %s\n", AcpiFormatException(status));
383cb9b0d80SMike Smith 	goto out;
38415e32d5dSMike Smith     }
38515e32d5dSMike Smith 
38615e32d5dSMike Smith     /*
3871d073b1dSJohn Baldwin      * Setup our sysctl tree.
3881d073b1dSJohn Baldwin      *
3891d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
3901d073b1dSJohn Baldwin      */
3911d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
3921d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
3931d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
3941d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
3951d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
3961d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
3971d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
3981d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
3991d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4001d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4011d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4021d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
4031d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
404f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
405f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
406f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
407f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
408f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
409f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
4102d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4111611ea87SMitsuru IWASAKI 	OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
4121611ea87SMitsuru IWASAKI 	&sc->acpi_s4bios, 0, "S4BIOS mode");
4131611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4142d644607SMitsuru IWASAKI 	OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
4152d644607SMitsuru IWASAKI 	&sc->acpi_verbose, 0, "verbose mode");
416931a10c9SMitsuru IWASAKI     sc->acpi_s4bios = 1;
4172d644607SMitsuru IWASAKI     if (bootverbose)
4182d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
4191d073b1dSJohn Baldwin 
4201d073b1dSJohn Baldwin     /*
42115e32d5dSMike Smith      * Dispatch the default sleep state to devices.
42215e32d5dSMike Smith      * TBD: should be configured from userland policy manager.
42315e32d5dSMike Smith      */
42415e32d5dSMike Smith     sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
42515e32d5dSMike Smith     sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
42615e32d5dSMike Smith     sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
427f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
428f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
42915e32d5dSMike Smith 
43013d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
43115e32d5dSMike Smith 
43215e32d5dSMike Smith     /*
43315e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
43415e32d5dSMike Smith      */
43515e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
43615e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "probe"))
43715e32d5dSMike Smith 	acpi_EnterDebugger();
43815e32d5dSMike Smith #endif
4390ae55423SMike Smith     if (!acpi_disabled("bus"))
44015e32d5dSMike Smith 	acpi_probe_children(dev);
44115e32d5dSMike Smith 
44215e32d5dSMike Smith     /*
44315e32d5dSMike Smith      * Register our shutdown handlers
44415e32d5dSMike Smith      */
44515e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST);
44615e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST);
44715e32d5dSMike Smith 
44815e32d5dSMike Smith     /*
44915e32d5dSMike Smith      * Register our acpi event handlers.
45015e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
45115e32d5dSMike Smith      */
45215e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST);
45315e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST);
45415e32d5dSMike Smith 
45515e32d5dSMike Smith     /*
45615e32d5dSMike Smith      * Flag our initial states.
45715e32d5dSMike Smith      */
45815e32d5dSMike Smith     sc->acpi_enabled = 1;
45915e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
460ece50487SMitsuru IWASAKI     sc->acpi_sleep_disabled = 0;
46115e32d5dSMike Smith 
46215e32d5dSMike Smith     /*
46315e32d5dSMike Smith      * Create the control device
46415e32d5dSMike Smith      */
46515e32d5dSMike Smith     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, 0, 5, 0660, "acpi");
46615e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
46715e32d5dSMike Smith 
46815e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
46915e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "running"))
47015e32d5dSMike Smith 	acpi_EnterDebugger();
47115e32d5dSMike Smith #endif
472f86214b6SMitsuru IWASAKI 
473c573e654SMitsuru IWASAKI #if defined(ACPI_MAX_THREADS) && ACPI_MAX_THREADS > 0
474c573e654SMitsuru IWASAKI     if ((error = acpi_task_thread_init())) {
475c573e654SMitsuru IWASAKI 	goto out;
476c573e654SMitsuru IWASAKI     }
477c573e654SMitsuru IWASAKI #endif
478c573e654SMitsuru IWASAKI 
479f86214b6SMitsuru IWASAKI     if ((error = acpi_machdep_init(dev))) {
480f86214b6SMitsuru IWASAKI 	goto out;
481f86214b6SMitsuru IWASAKI     }
482f86214b6SMitsuru IWASAKI 
483f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
484f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
485f9390180SMitsuru IWASAKI 
486cb9b0d80SMike Smith     error = 0;
487cb9b0d80SMike Smith 
488cb9b0d80SMike Smith  out:
489cb9b0d80SMike Smith     ACPI_UNLOCK;
490cb9b0d80SMike Smith     return_VALUE(error);
49115e32d5dSMike Smith }
49215e32d5dSMike Smith 
49315e32d5dSMike Smith /*
49415e32d5dSMike Smith  * Handle a new device being added
49515e32d5dSMike Smith  */
49615e32d5dSMike Smith static device_t
49715e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
49815e32d5dSMike Smith {
49915e32d5dSMike Smith     struct acpi_device	*ad;
50015e32d5dSMike Smith     device_t		child;
50115e32d5dSMike Smith 
50215e32d5dSMike Smith     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL)
50315e32d5dSMike Smith 	return(NULL);
50415e32d5dSMike Smith     bzero(ad, sizeof(*ad));
50515e32d5dSMike Smith 
50615e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
50715e32d5dSMike Smith 
50815e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
50915e32d5dSMike Smith     if (child != NULL)
51015e32d5dSMike Smith 	device_set_ivars(child, ad);
51115e32d5dSMike Smith     return(child);
51215e32d5dSMike Smith }
51315e32d5dSMike Smith 
51415e32d5dSMike Smith /*
51515e32d5dSMike Smith  * Print child device resource usage
51615e32d5dSMike Smith  */
51715e32d5dSMike Smith static int
51815e32d5dSMike Smith acpi_print_resources(struct resource_list *rl, const char *name, int type, const char *format)
51915e32d5dSMike Smith {
52015e32d5dSMike Smith     struct resource_list_entry	*rle;
52115e32d5dSMike Smith     int				printed, retval;
52215e32d5dSMike Smith 
52315e32d5dSMike Smith     printed = 0;
52415e32d5dSMike Smith     retval = 0;
52515e32d5dSMike Smith 
52615e32d5dSMike Smith     if (!SLIST_FIRST(rl))
52715e32d5dSMike Smith 	return(0);
52815e32d5dSMike Smith 
52915e32d5dSMike Smith     /* Yes, this is kinda cheating */
53015e32d5dSMike Smith     SLIST_FOREACH(rle, rl, link) {
53115e32d5dSMike Smith 	if (rle->type == type) {
53215e32d5dSMike Smith 	    if (printed == 0)
53315e32d5dSMike Smith 		retval += printf(" %s ", name);
53415e32d5dSMike Smith 	    else if (printed > 0)
53515e32d5dSMike Smith 		retval += printf(",");
53615e32d5dSMike Smith 	    printed++;
53715e32d5dSMike Smith 	    retval += printf(format, rle->start);
53815e32d5dSMike Smith 	    if (rle->count > 1) {
53915e32d5dSMike Smith 		retval += printf("-");
54015e32d5dSMike Smith 		retval += printf(format, rle->start +
54115e32d5dSMike Smith 				 rle->count - 1);
54215e32d5dSMike Smith 	    }
54315e32d5dSMike Smith 	}
54415e32d5dSMike Smith     }
54515e32d5dSMike Smith     return(retval);
54615e32d5dSMike Smith }
54715e32d5dSMike Smith 
54815e32d5dSMike Smith static int
54915e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
55015e32d5dSMike Smith {
55115e32d5dSMike Smith     struct acpi_device		*adev = device_get_ivars(child);
55215e32d5dSMike Smith     struct resource_list	*rl = &adev->ad_rl;
55315e32d5dSMike Smith     int retval = 0;
55415e32d5dSMike Smith 
55515e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
55615e32d5dSMike Smith     retval += acpi_print_resources(rl, "port",  SYS_RES_IOPORT, "%#lx");
55715e32d5dSMike Smith     retval += acpi_print_resources(rl, "iomem", SYS_RES_MEMORY, "%#lx");
55815e32d5dSMike Smith     retval += acpi_print_resources(rl, "irq",   SYS_RES_IRQ,    "%ld");
55915e32d5dSMike Smith     retval += bus_print_child_footer(bus, child);
56015e32d5dSMike Smith 
56115e32d5dSMike Smith     return(retval);
56215e32d5dSMike Smith }
56315e32d5dSMike Smith 
56415e32d5dSMike Smith 
56515e32d5dSMike Smith /*
56615e32d5dSMike Smith  * Handle per-device ivars
56715e32d5dSMike Smith  */
56815e32d5dSMike Smith static int
56915e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
57015e32d5dSMike Smith {
57115e32d5dSMike Smith     struct acpi_device	*ad;
57215e32d5dSMike Smith 
57315e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
57415e32d5dSMike Smith 	printf("device has no ivars\n");
57515e32d5dSMike Smith 	return(ENOENT);
57615e32d5dSMike Smith     }
57715e32d5dSMike Smith 
57815e32d5dSMike Smith     switch(index) {
57915e32d5dSMike Smith 	/* ACPI ivars */
58015e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
58115e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
58215e32d5dSMike Smith 	break;
58315e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
58415e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
58515e32d5dSMike Smith 	break;
58615e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
58715e32d5dSMike Smith 	*(void **)result = ad->ad_private;
58815e32d5dSMike Smith 	break;
58915e32d5dSMike Smith 
59032d18aa5SMike Smith 	/* ISA compatibility */
59132d18aa5SMike Smith     case ISA_IVAR_VENDORID:
59232d18aa5SMike Smith     case ISA_IVAR_SERIAL:
59332d18aa5SMike Smith     case ISA_IVAR_COMPATID:
59432d18aa5SMike Smith 	*(int *)result = -1;
59532d18aa5SMike Smith 	break;
59632d18aa5SMike Smith 
59732d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
59832d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
59932d18aa5SMike Smith 	break;
60032d18aa5SMike Smith 
60115e32d5dSMike Smith     default:
60215e32d5dSMike Smith 	panic("bad ivar read request (%d)\n", index);
60315e32d5dSMike Smith 	return(ENOENT);
60415e32d5dSMike Smith     }
60515e32d5dSMike Smith     return(0);
60615e32d5dSMike Smith }
60715e32d5dSMike Smith 
60815e32d5dSMike Smith static int
60915e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
61015e32d5dSMike Smith {
61115e32d5dSMike Smith     struct acpi_device	*ad;
61215e32d5dSMike Smith 
61315e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
61415e32d5dSMike Smith 	printf("device has no ivars\n");
61515e32d5dSMike Smith 	return(ENOENT);
61615e32d5dSMike Smith     }
61715e32d5dSMike Smith 
61815e32d5dSMike Smith     switch(index) {
61915e32d5dSMike Smith 	/* ACPI ivars */
62015e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
62115e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
62215e32d5dSMike Smith 	break;
62315e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
62415e32d5dSMike Smith 	ad->ad_magic = (int )value;
62515e32d5dSMike Smith 	break;
62615e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
62715e32d5dSMike Smith 	ad->ad_private = (void *)value;
62815e32d5dSMike Smith 	break;
62915e32d5dSMike Smith 
63015e32d5dSMike Smith     default:
63115e32d5dSMike Smith 	panic("bad ivar write request (%d)\n", index);
63215e32d5dSMike Smith 	return(ENOENT);
63315e32d5dSMike Smith     }
63415e32d5dSMike Smith     return(0);
63515e32d5dSMike Smith }
63615e32d5dSMike Smith 
63715e32d5dSMike Smith /*
63815e32d5dSMike Smith  * Handle child resource allocation/removal
63915e32d5dSMike Smith  */
64015e32d5dSMike Smith static int
64115e32d5dSMike Smith acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
64215e32d5dSMike Smith {
64315e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
64415e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
64515e32d5dSMike Smith 
64615e32d5dSMike Smith     resource_list_add(rl, type, rid, start, start + count -1, count);
64715e32d5dSMike Smith 
64815e32d5dSMike Smith     return(0);
64915e32d5dSMike Smith }
65015e32d5dSMike Smith 
65115e32d5dSMike Smith static int
65215e32d5dSMike Smith acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
65315e32d5dSMike Smith {
65415e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
65515e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
65615e32d5dSMike Smith     struct resource_list_entry	*rle;
65715e32d5dSMike Smith 
65815e32d5dSMike Smith     rle = resource_list_find(rl, type, rid);
65915e32d5dSMike Smith     if (!rle)
66015e32d5dSMike Smith 	return(ENOENT);
66115e32d5dSMike Smith 
66215e32d5dSMike Smith     if (startp)
66315e32d5dSMike Smith 	*startp = rle->start;
66415e32d5dSMike Smith     if (countp)
66515e32d5dSMike Smith 	*countp = rle->count;
66615e32d5dSMike Smith 
66715e32d5dSMike Smith     return(0);
66815e32d5dSMike Smith }
66915e32d5dSMike Smith 
67015e32d5dSMike Smith static struct resource *
67115e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
67215e32d5dSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
67315e32d5dSMike Smith {
67415e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
67515e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
67615e32d5dSMike Smith 
67715e32d5dSMike Smith     return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags));
67815e32d5dSMike Smith }
67915e32d5dSMike Smith 
68015e32d5dSMike Smith static int
68115e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
68215e32d5dSMike Smith {
68315e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
68415e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
68515e32d5dSMike Smith 
68615e32d5dSMike Smith     return(resource_list_release(rl, bus, child, type, rid, r));
68715e32d5dSMike Smith }
68815e32d5dSMike Smith 
68915e32d5dSMike Smith /*
690bc0f2195SMike Smith  * Handle ISA-like devices probing for a PnP ID to match.
691bc0f2195SMike Smith  */
692bc0f2195SMike Smith #define PNP_EISAID(s)				\
693bc0f2195SMike Smith 	((((s[0] - '@') & 0x1f) << 2)		\
694bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x18) >> 3)		\
695bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x07) << 13)	\
696bc0f2195SMike Smith 	 | (((s[2] - '@') & 0x1f) << 8)		\
697bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[4]) << 16)		\
698bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[3]) << 20)		\
699bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[6]) << 24)		\
700bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[5]) << 28))
701bc0f2195SMike Smith 
70232d18aa5SMike Smith static u_int32_t
70332d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
704bc0f2195SMike Smith {
705bc0f2195SMike Smith     ACPI_HANDLE		h;
706bc0f2195SMike Smith     ACPI_DEVICE_INFO	devinfo;
707bc0f2195SMike Smith     ACPI_STATUS		error;
708bc0f2195SMike Smith     u_int32_t		pnpid;
70932d18aa5SMike Smith 
710b53f2771SMike Smith     ACPI_FUNCTION_TRACE(__func__);
71132d18aa5SMike Smith 
71232d18aa5SMike Smith     pnpid = 0;
71332d18aa5SMike Smith     ACPI_LOCK;
71432d18aa5SMike Smith 
71532d18aa5SMike Smith     /* fetch and validate the HID */
71632d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
71732d18aa5SMike Smith 	goto out;
718b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
71932d18aa5SMike Smith 	goto out;
72032d18aa5SMike Smith     if (!(devinfo.Valid & ACPI_VALID_HID))
72132d18aa5SMike Smith 	goto out;
72232d18aa5SMike Smith 
72332d18aa5SMike Smith     pnpid = PNP_EISAID(devinfo.HardwareId);
72432d18aa5SMike Smith out:
72532d18aa5SMike Smith     ACPI_UNLOCK;
72632d18aa5SMike Smith     return_VALUE(pnpid);
72732d18aa5SMike Smith }
72832d18aa5SMike Smith 
72932d18aa5SMike Smith static int
73032d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
73132d18aa5SMike Smith {
732bc0f2195SMike Smith     int			result;
73332d18aa5SMike Smith     u_int32_t		pnpid;
734bc0f2195SMike Smith 
735b53f2771SMike Smith     ACPI_FUNCTION_TRACE(__func__);
736bc0f2195SMike Smith 
737bc0f2195SMike Smith     /*
738bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
739bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
740bc0f2195SMike Smith      * that to happen, so don't ever return it.
741bc0f2195SMike Smith      */
742bc0f2195SMike Smith     result = ENXIO;
743bc0f2195SMike Smith 
744bc0f2195SMike Smith     /* scan the supplied IDs for a match */
74532d18aa5SMike Smith     pnpid = acpi_isa_get_logicalid(child);
746bc0f2195SMike Smith     while (ids && ids->ip_id) {
747bc0f2195SMike Smith 	if (pnpid == ids->ip_id) {
748bc0f2195SMike Smith 	    result = 0;
749bc0f2195SMike Smith 	    goto out;
750bc0f2195SMike Smith 	}
751bc0f2195SMike Smith 	ids++;
752bc0f2195SMike Smith     }
753bc0f2195SMike Smith  out:
754bc0f2195SMike Smith     return_VALUE(result);
755bc0f2195SMike Smith }
756bc0f2195SMike Smith 
757bc0f2195SMike Smith /*
75815e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
75915e32d5dSMike Smith  *
7607d3bcec9SMike Smith  * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and \_SB_ scopes,
7617d3bcec9SMike Smith  * and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
76215e32d5dSMike Smith  */
76315e32d5dSMike Smith static void
76415e32d5dSMike Smith acpi_probe_children(device_t bus)
76515e32d5dSMike Smith {
76615e32d5dSMike Smith     ACPI_HANDLE		parent;
7677d3bcec9SMike Smith     static char		*scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
76815e32d5dSMike Smith     int			i;
76915e32d5dSMike Smith 
770b53f2771SMike Smith     ACPI_FUNCTION_TRACE(__func__);
771cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
7720ae55423SMike Smith 
77315e32d5dSMike Smith     /*
77415e32d5dSMike Smith      * Create any static children by calling device identify methods.
77515e32d5dSMike Smith      */
7764c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
77715e32d5dSMike Smith     bus_generic_probe(bus);
77815e32d5dSMike Smith 
77915e32d5dSMike Smith     /*
78015e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
78115e32d5dSMike Smith      * we find.
78215e32d5dSMike Smith      *
78315e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
78415e32d5dSMike Smith      * we want to create nodes for all devices, not just those that are currently
78515e32d5dSMike Smith      * present. (This assumes that we don't want to create/remove devices as they
78615e32d5dSMike Smith      * appear, which might be smarter.)
78715e32d5dSMike Smith      */
7884c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
78915e32d5dSMike Smith     for (i = 0; scopes[i] != NULL; i++)
790b53f2771SMike Smith 	if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent)))
79115e32d5dSMike Smith 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL);
79215e32d5dSMike Smith 
79315e32d5dSMike Smith     /*
79415e32d5dSMike Smith      * Scan all of the child devices we have created and let them probe/attach.
79515e32d5dSMike Smith      */
7964c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
79715e32d5dSMike Smith     bus_generic_attach(bus);
79815e32d5dSMike Smith 
79915e32d5dSMike Smith     /*
80015e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
80115e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
80215e32d5dSMike Smith      */
8034c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
80415e32d5dSMike Smith     bus_generic_attach(bus);
8050ae55423SMike Smith 
806bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
8070ae55423SMike Smith     return_VOID;
80815e32d5dSMike Smith }
80915e32d5dSMike Smith 
81015e32d5dSMike Smith /*
81115e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
81215e32d5dSMike Smith  * it.
81315e32d5dSMike Smith  */
81415e32d5dSMike Smith static ACPI_STATUS
81515e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
81615e32d5dSMike Smith {
81715e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
81815e32d5dSMike Smith     device_t		child, bus = (device_t)context;
81915e32d5dSMike Smith 
820b53f2771SMike Smith     ACPI_FUNCTION_TRACE(__func__);
8210ae55423SMike Smith 
8220ae55423SMike Smith     /*
8230ae55423SMike Smith      * Skip this device if we think we'll have trouble with it.
8240ae55423SMike Smith      */
8250ae55423SMike Smith     if (acpi_avoid(handle))
8260ae55423SMike Smith 	return_ACPI_STATUS(AE_OK);
8270ae55423SMike Smith 
828b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
82915e32d5dSMike Smith 	switch(type) {
83015e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
83115e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
83215e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
83315e32d5dSMike Smith 	case ACPI_TYPE_POWER:
8340ae55423SMike Smith 	    if (acpi_disabled("children"))
8350ae55423SMike Smith 		break;
83615e32d5dSMike Smith 	    /*
83715e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
83815e32d5dSMike Smith 	     * so that the probe/attach passes will run breadth-first.
83915e32d5dSMike Smith 	     */
8404c1cdee6SMike Smith 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", acpi_name(handle)));
84115e32d5dSMike Smith 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
842bc0f2195SMike Smith 	    if (child == NULL)
843bc0f2195SMike Smith 		break;
84415e32d5dSMike Smith 	    acpi_set_handle(child, handle);
845bc0f2195SMike Smith 
846bc0f2195SMike Smith 	    /*
847b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
848b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
849b519f9d6SMike Smith 	     * the handle, but we don't probe it).
850b519f9d6SMike Smith 	     */
85123b4e251SMitsuru IWASAKI 	    if ((type == ACPI_TYPE_DEVICE) && (!acpi_DeviceIsPresent(child))) {
852b519f9d6SMike Smith 		device_disable(child);
853b519f9d6SMike Smith 		break;
854b519f9d6SMike Smith 	    }
855b519f9d6SMike Smith 
856b519f9d6SMike Smith 	    /*
857bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
858bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
859bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
860bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
861bc0f2195SMike Smith 	     * device not to have any resources.
862bc0f2195SMike Smith 	     */
863bc0f2195SMike Smith 	    acpi_parse_resources(child, handle, &acpi_res_parse_set);
864bc0f2195SMike Smith 
865bc0f2195SMike Smith 	    /* if we're debugging, probe/attach now rather than later */
866b53f2771SMike Smith 	    ACPI_DEBUG_EXEC(device_probe_and_attach(child));
8670ae55423SMike Smith 	    break;
86815e32d5dSMike Smith 	}
86915e32d5dSMike Smith     }
8700ae55423SMike Smith     return_ACPI_STATUS(AE_OK);
87115e32d5dSMike Smith }
87215e32d5dSMike Smith 
87315e32d5dSMike Smith static void
87415e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto)
87515e32d5dSMike Smith {
876cb9b0d80SMike Smith 
877cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
878cb9b0d80SMike Smith 
87915e32d5dSMike Smith     /*
8800ae55423SMike Smith      * Disable all ACPI events before soft off, otherwise the system
88115e32d5dSMike Smith      * will be turned on again on some laptops.
88215e32d5dSMike Smith      *
88315e32d5dSMike Smith      * XXX this should probably be restricted to masking some events just
88415e32d5dSMike Smith      *     before powering down, since we may still need ACPI during the
88515e32d5dSMike Smith      *     shutdown process.
88615e32d5dSMike Smith      */
88715e32d5dSMike Smith     acpi_Disable((struct acpi_softc *)arg);
88815e32d5dSMike Smith }
88915e32d5dSMike Smith 
89015e32d5dSMike Smith static void
89115e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
89215e32d5dSMike Smith {
89315e32d5dSMike Smith     ACPI_STATUS	status;
89415e32d5dSMike Smith 
895cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
896cb9b0d80SMike Smith 
8975f3e4175SMitsuru IWASAKI     if (howto & RB_POWEROFF) {
89815e32d5dSMike Smith 	printf("Power system off using ACPI...\n");
899b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(acpi_off_state))) {
900b9c780d6SMitsuru IWASAKI 	    printf("AcpiEnterSleepStatePrep failed - %s\n",
901b9c780d6SMitsuru IWASAKI 		   AcpiFormatException(status));
902b9c780d6SMitsuru IWASAKI 	    return;
903b9c780d6SMitsuru IWASAKI 	}
904b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepState(acpi_off_state))) {
905bfae45aaSMike Smith 	    printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
90615e32d5dSMike Smith 	} else {
90715e32d5dSMike Smith 	    DELAY(1000000);
90815e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
90915e32d5dSMike Smith 	}
91015e32d5dSMike Smith     }
91115e32d5dSMike Smith }
91215e32d5dSMike Smith 
91313d4f7baSMitsuru IWASAKI static void
91413d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
91513d4f7baSMitsuru IWASAKI {
91613d4f7baSMitsuru IWASAKI     static int	first_time = 1;
91713d4f7baSMitsuru IWASAKI #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n"
91813d4f7baSMitsuru IWASAKI 
919cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
920cb9b0d80SMike Smith 
92113d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
922cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) {
92343896e91SMike Smith 	AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED, 0);
92413d4f7baSMitsuru IWASAKI 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED);
92513d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
92613d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_power_button_for_sleep, sc);
92713d4f7baSMitsuru IWASAKI 	if (first_time) {
92813d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "power");
92913d4f7baSMitsuru IWASAKI 	}
93013d4f7baSMitsuru IWASAKI     }
931cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) {
93243896e91SMike Smith 	AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED, 0);
93313d4f7baSMitsuru IWASAKI 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED);
93413d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
93513d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_sleep_button_for_sleep, sc);
93613d4f7baSMitsuru IWASAKI 	if (first_time) {
93713d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "sleep");
93813d4f7baSMitsuru IWASAKI 	}
93913d4f7baSMitsuru IWASAKI     }
94013d4f7baSMitsuru IWASAKI 
94113d4f7baSMitsuru IWASAKI     first_time = 0;
94213d4f7baSMitsuru IWASAKI }
94313d4f7baSMitsuru IWASAKI 
94415e32d5dSMike Smith /*
945c5ba0be4SMike Smith  * Returns true if the device is actually present and should
946c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
947c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
948c5ba0be4SMike Smith  */
949c5ba0be4SMike Smith BOOLEAN
950c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
951c5ba0be4SMike Smith {
952c5ba0be4SMike Smith     ACPI_HANDLE		h;
953c5ba0be4SMike Smith     ACPI_DEVICE_INFO	devinfo;
954c5ba0be4SMike Smith     ACPI_STATUS		error;
955c5ba0be4SMike Smith 
956cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
957cb9b0d80SMike Smith 
958c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
959c5ba0be4SMike Smith 	return(FALSE);
960b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
961c5ba0be4SMike Smith 	return(FALSE);
96243896e91SMike Smith     /* if no _STA method, must be present */
96343896e91SMike Smith     if (!(devinfo.Valid & ACPI_VALID_STA))
96443896e91SMike Smith 	return(TRUE);
96543896e91SMike Smith     /* return true for 'present' and 'functioning' */
96643896e91SMike Smith     if ((devinfo.CurrentStatus & 0x9) == 0x9)
967c5ba0be4SMike Smith 	return(TRUE);
968c5ba0be4SMike Smith     return(FALSE);
969c5ba0be4SMike Smith }
970c5ba0be4SMike Smith 
971c5ba0be4SMike Smith /*
972b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
973b53f2771SMike Smith  */
974b53f2771SMike Smith BOOLEAN
975b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
976b53f2771SMike Smith {
977b53f2771SMike Smith     ACPI_HANDLE		h;
978b53f2771SMike Smith     ACPI_DEVICE_INFO	devinfo;
979b53f2771SMike Smith     ACPI_STATUS		error;
980b53f2771SMike Smith 
981b53f2771SMike Smith     ACPI_ASSERTLOCK;
982b53f2771SMike Smith 
983b53f2771SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
984b53f2771SMike Smith 	return(FALSE);
985b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
986b53f2771SMike Smith 	return(FALSE);
987b53f2771SMike Smith     /* if no _STA method, must be present */
988b53f2771SMike Smith     if (!(devinfo.Valid & ACPI_VALID_STA))
989b53f2771SMike Smith 	return(TRUE);
990b53f2771SMike Smith     /* return true for 'present' and 'functioning' */
991b53f2771SMike Smith     if ((devinfo.CurrentStatus & 0x19) == 0x19)
992b53f2771SMike Smith 	return(TRUE);
993b53f2771SMike Smith     return(FALSE);
994b53f2771SMike Smith }
995b53f2771SMike Smith 
996b53f2771SMike Smith /*
99715e32d5dSMike Smith  * Match a HID string against a device
99815e32d5dSMike Smith  */
99915e32d5dSMike Smith BOOLEAN
100015e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid)
100115e32d5dSMike Smith {
100215e32d5dSMike Smith     ACPI_HANDLE		h;
100315e32d5dSMike Smith     ACPI_DEVICE_INFO	devinfo;
100415e32d5dSMike Smith     ACPI_STATUS		error;
1005ed136da6SDoug Rabson     int			cid;
100615e32d5dSMike Smith 
1007cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1008cb9b0d80SMike Smith 
10094d332989SMike Smith     if (hid == NULL)
101015e32d5dSMike Smith 	return(FALSE);
101115e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
101215e32d5dSMike Smith 	return(FALSE);
1013b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
101415e32d5dSMike Smith 	return(FALSE);
10154d332989SMike Smith     if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId))
101615e32d5dSMike Smith 	return(TRUE);
1017b53f2771SMike Smith     if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &cid)))
1018ed136da6SDoug Rabson 	return(FALSE);
1019ed136da6SDoug Rabson     if (cid == PNP_EISAID(hid))
1020ed136da6SDoug Rabson 	return(TRUE);
102115e32d5dSMike Smith     return(FALSE);
102215e32d5dSMike Smith }
102315e32d5dSMike Smith 
102415e32d5dSMike Smith /*
1025c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1026c5ba0be4SMike Smith  * or one if its parents.
1027c5ba0be4SMike Smith  */
1028c5ba0be4SMike Smith ACPI_STATUS
1029c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1030c5ba0be4SMike Smith {
1031c5ba0be4SMike Smith     ACPI_HANDLE		r;
1032c5ba0be4SMike Smith     ACPI_STATUS		status;
1033c5ba0be4SMike Smith 
1034cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1035cb9b0d80SMike Smith 
1036c5ba0be4SMike Smith     /* walk back up the tree to the root */
1037c5ba0be4SMike Smith     for (;;) {
1038b53f2771SMike Smith 	if (ACPI_SUCCESS(status = AcpiGetHandle(parent, path, &r))) {
1039c5ba0be4SMike Smith 	    *result = r;
1040c5ba0be4SMike Smith 	    return(AE_OK);
1041c5ba0be4SMike Smith 	}
1042c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1043c5ba0be4SMike Smith 	    return(AE_OK);
1044b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1045c5ba0be4SMike Smith 	    return(AE_NOT_FOUND);
1046c5ba0be4SMike Smith 	parent = r;
1047c5ba0be4SMike Smith     }
1048c5ba0be4SMike Smith }
1049c5ba0be4SMike Smith 
1050c5ba0be4SMike Smith /*
1051c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1052c5ba0be4SMike Smith  */
1053c5ba0be4SMike Smith ACPI_BUFFER *
1054c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1055c5ba0be4SMike Smith {
1056c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1057c5ba0be4SMike Smith 
1058c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1059c5ba0be4SMike Smith 	return(NULL);
1060c5ba0be4SMike Smith     buf->Length = size;
1061c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1062c5ba0be4SMike Smith     return(buf);
1063c5ba0be4SMike Smith }
1064c5ba0be4SMike Smith 
1065c5ba0be4SMike Smith /*
1066c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1067c5ba0be4SMike Smith  */
1068c5ba0be4SMike Smith ACPI_STATUS
1069c5ba0be4SMike Smith acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number)
1070c5ba0be4SMike Smith {
1071c5ba0be4SMike Smith     ACPI_STATUS	error;
1072c5ba0be4SMike Smith     ACPI_BUFFER	buf;
1073c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
1074c5ba0be4SMike Smith 
1075cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1076cb9b0d80SMike Smith 
1077c5ba0be4SMike Smith     if (handle == NULL)
1078c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
10790c7da7acSJohn Baldwin 
10800c7da7acSJohn Baldwin     /*
10810c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
10820c7da7acSJohn Baldwin      * a method that will return an Integer.
10830c7da7acSJohn Baldwin      */
1084c5ba0be4SMike Smith     buf.Pointer = &param;
1085c5ba0be4SMike Smith     buf.Length = sizeof(param);
1086b53f2771SMike Smith     if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
1087c5ba0be4SMike Smith 	if (param.Type == ACPI_TYPE_INTEGER) {
1088c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1089c5ba0be4SMike Smith 	} else {
1090c5ba0be4SMike Smith 	    error = AE_TYPE;
1091c5ba0be4SMike Smith 	}
1092c5ba0be4SMike Smith     }
10930c7da7acSJohn Baldwin 
10940c7da7acSJohn Baldwin     /*
10950c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
10960c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
10970c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
10980c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
10990c7da7acSJohn Baldwin      *
11000c7da7acSJohn Baldwin      * This is a hack.
11010c7da7acSJohn Baldwin      */
11020c7da7acSJohn Baldwin     if (error == AE_BUFFER_OVERFLOW) {
1103b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
11040c7da7acSJohn Baldwin 	    error = AE_NO_MEMORY;
11050c7da7acSJohn Baldwin 	} else {
1106b53f2771SMike Smith 	    if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
1107c573e654SMitsuru IWASAKI 		error = acpi_ConvertBufferToInteger(&buf, number);
11080c7da7acSJohn Baldwin 	    }
11090c7da7acSJohn Baldwin 	}
11100c7da7acSJohn Baldwin 	AcpiOsFree(buf.Pointer);
11110c7da7acSJohn Baldwin     }
1112c5ba0be4SMike Smith     return(error);
1113c5ba0be4SMike Smith }
1114c5ba0be4SMike Smith 
1115c573e654SMitsuru IWASAKI ACPI_STATUS
1116c573e654SMitsuru IWASAKI acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number)
1117c573e654SMitsuru IWASAKI {
1118c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
1119c573e654SMitsuru IWASAKI     int		i;
1120c573e654SMitsuru IWASAKI 
1121c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
1122c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
1123c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
1124c573e654SMitsuru IWASAKI 	return(AE_OK);
1125c573e654SMitsuru IWASAKI     }
1126c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
1127c573e654SMitsuru IWASAKI 	return(AE_TYPE);
1128c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
1129c573e654SMitsuru IWASAKI 	return(AE_BAD_DATA);
1130c573e654SMitsuru IWASAKI     *number = 0;
1131c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
1132c573e654SMitsuru IWASAKI 	*number += (*(p->Buffer.Pointer + i) << (i * 8));
1133c573e654SMitsuru IWASAKI     return(AE_OK);
1134c573e654SMitsuru IWASAKI }
1135c573e654SMitsuru IWASAKI 
1136c5ba0be4SMike Smith /*
1137c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1138c5ba0be4SMike Smith  * function for each element.
1139c5ba0be4SMike Smith  *
1140c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1141c5ba0be4SMike Smith  */
1142c5ba0be4SMike Smith ACPI_STATUS
1143c5ba0be4SMike Smith acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg)
1144c5ba0be4SMike Smith {
1145c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1146c5ba0be4SMike Smith     int		i;
1147c5ba0be4SMike Smith 
1148c5ba0be4SMike Smith     if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE))
1149c5ba0be4SMike Smith 	return(AE_BAD_PARAMETER);
1150c5ba0be4SMike Smith 
1151c5ba0be4SMike Smith     /* iterate over components */
1152c5ba0be4SMike Smith     for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++)
1153c5ba0be4SMike Smith 	func(comp, arg);
1154c5ba0be4SMike Smith 
1155c5ba0be4SMike Smith     return(AE_OK);
1156c5ba0be4SMike Smith }
1157c5ba0be4SMike Smith 
11586f69255bSMike Smith /*
11596f69255bSMike Smith  * Find the (index)th resource object in a set.
11606f69255bSMike Smith  */
11616f69255bSMike Smith ACPI_STATUS
11626d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
11636f69255bSMike Smith {
11646d63101aSMike Smith     ACPI_RESOURCE	*rp;
11656f69255bSMike Smith     int			i;
11666f69255bSMike Smith 
11676d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
11686f69255bSMike Smith     i = index;
11696d63101aSMike Smith     while (i-- > 0) {
11706f69255bSMike Smith 	/* range check */
11716d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
11726f69255bSMike Smith 	    return(AE_BAD_PARAMETER);
11736d63101aSMike Smith 	/* check for terminator */
11746d63101aSMike Smith 	if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
11756d63101aSMike Smith 	    (rp->Length == 0))
11766d63101aSMike Smith 	    return(AE_NOT_FOUND);
11776d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
11786f69255bSMike Smith     }
11796f69255bSMike Smith     if (resp != NULL)
11806d63101aSMike Smith 	*resp = rp;
11816d63101aSMike Smith     return(AE_OK);
11826d63101aSMike Smith }
11836d63101aSMike Smith 
11846d63101aSMike Smith /*
11856d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
11866d63101aSMike Smith  *
11876d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
11886d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
11896d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
11906d63101aSMike Smith  * resources.
11916d63101aSMike Smith  */
11926d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
11936d63101aSMike Smith 
11946d63101aSMike Smith ACPI_STATUS
11956d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
11966d63101aSMike Smith {
11976d63101aSMike Smith     ACPI_RESOURCE	*rp;
11986d63101aSMike Smith     void		*newp;
11996d63101aSMike Smith 
12006d63101aSMike Smith     /*
12016d63101aSMike Smith      * Initialise the buffer if necessary.
12026d63101aSMike Smith      */
12036d63101aSMike Smith     if (buf->Pointer == NULL) {
12046d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
12056d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
12066d63101aSMike Smith 	    return(AE_NO_MEMORY);
12076d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
12086d63101aSMike Smith 	rp->Id = ACPI_RSTYPE_END_TAG;
12096d63101aSMike Smith 	rp->Length = 0;
12106d63101aSMike Smith     }
12116d63101aSMike Smith     if (res == NULL)
12126d63101aSMike Smith 	return(AE_OK);
12136d63101aSMike Smith 
12146d63101aSMike Smith     /*
12156d63101aSMike Smith      * Scan the current buffer looking for the terminator.
12166d63101aSMike Smith      * This will either find the terminator or hit the end
12176d63101aSMike Smith      * of the buffer and return an error.
12186d63101aSMike Smith      */
12196d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
12206d63101aSMike Smith     for (;;) {
12216d63101aSMike Smith 	/* range check, don't go outside the buffer */
12226d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
12236d63101aSMike Smith 	    return(AE_BAD_PARAMETER);
12246d63101aSMike Smith 	if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
12256d63101aSMike Smith 	    (rp->Length == 0)) {
12266d63101aSMike Smith 	    break;
12276d63101aSMike Smith 	}
12286d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
12296d63101aSMike Smith     }
12306d63101aSMike Smith 
12316d63101aSMike Smith     /*
12326d63101aSMike Smith      * Check the size of the buffer and expand if required.
12336d63101aSMike Smith      *
12346d63101aSMike Smith      * Required size is:
12356d63101aSMike Smith      *	size of existing resources before terminator +
12366d63101aSMike Smith      *	size of new resource and header +
12376d63101aSMike Smith      * 	size of terminator.
12386d63101aSMike Smith      *
12396d63101aSMike Smith      * Note that this loop should really only run once, unless
12406d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
12416d63101aSMike Smith      */
12426d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
12436d63101aSMike Smith 	    res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
12446d63101aSMike Smith 	    ACPI_RESOURCE_LENGTH) >= buf->Length) {
12456d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
12466d63101aSMike Smith 	    return(AE_NO_MEMORY);
12476d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
1248a692219dSMike Smith         rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1249a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
12506d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
12516d63101aSMike Smith 	buf->Pointer = newp;
12526d63101aSMike Smith 	buf->Length += buf->Length;
12536d63101aSMike Smith     }
12546d63101aSMike Smith 
12556d63101aSMike Smith     /*
12566d63101aSMike Smith      * Insert the new resource.
12576d63101aSMike Smith      */
12586d63101aSMike Smith     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
12596d63101aSMike Smith 
12606d63101aSMike Smith     /*
12616d63101aSMike Smith      * And add the terminator.
12626d63101aSMike Smith      */
12636d63101aSMike Smith     rp = ACPI_RESOURCE_NEXT(rp);
12646d63101aSMike Smith     rp->Id = ACPI_RSTYPE_END_TAG;
12656d63101aSMike Smith     rp->Length = 0;
12666d63101aSMike Smith 
12676f69255bSMike Smith     return(AE_OK);
12686f69255bSMike Smith }
1269c5ba0be4SMike Smith 
1270ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME	5
1271ece50487SMitsuru IWASAKI 
1272ece50487SMitsuru IWASAKI static void
1273ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
1274ece50487SMitsuru IWASAKI {
1275ece50487SMitsuru IWASAKI     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1276ece50487SMitsuru IWASAKI }
1277c30382dfSMitsuru IWASAKI 
127815e32d5dSMike Smith /*
127915e32d5dSMike Smith  * Set the system sleep state
128015e32d5dSMike Smith  *
128115e32d5dSMike Smith  * Currently we only support S1 and S5
128215e32d5dSMike Smith  */
128315e32d5dSMike Smith ACPI_STATUS
128415e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
128515e32d5dSMike Smith {
128615e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
128756d8cb57SMitsuru IWASAKI     UINT8	TypeA;
128856d8cb57SMitsuru IWASAKI     UINT8	TypeB;
128915e32d5dSMike Smith 
1290b53f2771SMike Smith     ACPI_FUNCTION_TRACE_U32(__func__, state);
1291cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
12920ae55423SMike Smith 
12936397624dSMitsuru IWASAKI     if (sc->acpi_sstate != ACPI_STATE_S0)
12946397624dSMitsuru IWASAKI 	return_ACPI_STATUS(AE_BAD_PARAMETER);	/* avoid reentry */
12956397624dSMitsuru IWASAKI 
1296ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1297ece50487SMitsuru IWASAKI 	return_ACPI_STATUS(AE_OK);
1298ece50487SMitsuru IWASAKI 
129915e32d5dSMike Smith     switch (state) {
130015e32d5dSMike Smith     case ACPI_STATE_S0:	/* XXX only for testing */
1301b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
1302bfae45aaSMike Smith 	    device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
130315e32d5dSMike Smith 	}
130415e32d5dSMike Smith 	break;
130515e32d5dSMike Smith 
130615e32d5dSMike Smith     case ACPI_STATE_S1:
13076161544cSTakanori Watanabe     case ACPI_STATE_S2:
13086161544cSTakanori Watanabe     case ACPI_STATE_S3:
13096161544cSTakanori Watanabe     case ACPI_STATE_S4:
1310b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiHwGetSleepTypeData((UINT8)state, &TypeA, &TypeB))) {
1311b53f2771SMike Smith 	    device_printf(sc->acpi_dev, "AcpiHwGetSleepTypeData failed - %s\n", AcpiFormatException(status));
131256d8cb57SMitsuru IWASAKI 	    break;
131356d8cb57SMitsuru IWASAKI 	}
131456d8cb57SMitsuru IWASAKI 
131515e32d5dSMike Smith 	/*
131615e32d5dSMike Smith 	 * Inform all devices that we are going to sleep.
131715e32d5dSMike Smith 	 */
131815e32d5dSMike Smith 	if (DEVICE_SUSPEND(root_bus) != 0) {
131915e32d5dSMike Smith 	    /*
132015e32d5dSMike Smith 	     * Re-wake the system.
132115e32d5dSMike Smith 	     *
132215e32d5dSMike Smith 	     * XXX note that a better two-pass approach with a 'veto' pass
132315e32d5dSMike Smith 	     *     followed by a "real thing" pass would be better, but the
132415e32d5dSMike Smith 	     *     current bus interface does not provide for this.
132515e32d5dSMike Smith 	     */
132615e32d5dSMike Smith 	    DEVICE_RESUME(root_bus);
13270ae55423SMike Smith 	    return_ACPI_STATUS(AE_ERROR);
132815e32d5dSMike Smith 	}
1329b9c780d6SMitsuru IWASAKI 
1330b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(state))) {
1331b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1332b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
1333b9c780d6SMitsuru IWASAKI 	    break;
1334b9c780d6SMitsuru IWASAKI 	}
1335b9c780d6SMitsuru IWASAKI 
133615e32d5dSMike Smith 	sc->acpi_sstate = state;
1337ece50487SMitsuru IWASAKI 	sc->acpi_sleep_disabled = 1;
13386161544cSTakanori Watanabe 
13396161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
13406161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
13416161544cSTakanori Watanabe 
13426161544cSTakanori Watanabe 	    /* AcpiEnterSleepState() maybe incompleted, unlock here. */
13436161544cSTakanori Watanabe 	    AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
13446161544cSTakanori Watanabe 
13456161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1346964679ceSMitsuru IWASAKI 	    if (state == ACPI_STATE_S4) {
1347964679ceSMitsuru IWASAKI 		AcpiEnable();
13486161544cSTakanori Watanabe 	    }
13496161544cSTakanori Watanabe 	} else {
1350b53f2771SMike Smith 	    if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
1351bfae45aaSMike Smith 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
1352c30382dfSMitsuru IWASAKI 		break;
135315e32d5dSMike Smith 	    }
13546161544cSTakanori Watanabe 	}
1355ff741befSTakanori Watanabe 	AcpiLeaveSleepState((UINT8)state);
135615e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
135715e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
135813d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
135915e32d5dSMike Smith 	break;
136015e32d5dSMike Smith 
136115e32d5dSMike Smith     case ACPI_STATE_S5:
136215e32d5dSMike Smith 	/*
136315e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
136415e32d5dSMike Smith 	 * shutdown handlers.
136515e32d5dSMike Smith 	 */
136615e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
136715e32d5dSMike Smith 	break;
136815e32d5dSMike Smith 
136915e32d5dSMike Smith     default:
137015e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
137115e32d5dSMike Smith 	break;
137215e32d5dSMike Smith     }
1373ece50487SMitsuru IWASAKI 
1374ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1375ece50487SMitsuru IWASAKI 	timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1376ece50487SMitsuru IWASAKI 
13770ae55423SMike Smith     return_ACPI_STATUS(status);
137815e32d5dSMike Smith }
137915e32d5dSMike Smith 
138015e32d5dSMike Smith /*
138115e32d5dSMike Smith  * Enable/Disable ACPI
138215e32d5dSMike Smith  */
138315e32d5dSMike Smith ACPI_STATUS
138415e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
138515e32d5dSMike Smith {
138615e32d5dSMike Smith     ACPI_STATUS	status;
138715e32d5dSMike Smith     u_int32_t	flags;
138815e32d5dSMike Smith 
1389b53f2771SMike Smith     ACPI_FUNCTION_TRACE(__func__);
1390cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
13910ae55423SMike Smith 
139215e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
139315e32d5dSMike Smith             ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
139415e32d5dSMike Smith     if (!sc->acpi_enabled) {
139515e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
139615e32d5dSMike Smith     } else {
139715e32d5dSMike Smith 	status = AE_OK;
139815e32d5dSMike Smith     }
139915e32d5dSMike Smith     if (status == AE_OK)
140015e32d5dSMike Smith 	sc->acpi_enabled = 1;
14010ae55423SMike Smith     return_ACPI_STATUS(status);
140215e32d5dSMike Smith }
140315e32d5dSMike Smith 
140415e32d5dSMike Smith ACPI_STATUS
140515e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
140615e32d5dSMike Smith {
140715e32d5dSMike Smith     ACPI_STATUS	status;
140815e32d5dSMike Smith 
1409b53f2771SMike Smith     ACPI_FUNCTION_TRACE(__func__);
1410cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
14110ae55423SMike Smith 
141215e32d5dSMike Smith     if (sc->acpi_enabled) {
141315e32d5dSMike Smith 	status = AcpiDisable();
141415e32d5dSMike Smith     } else {
141515e32d5dSMike Smith 	status = AE_OK;
141615e32d5dSMike Smith     }
141715e32d5dSMike Smith     if (status == AE_OK)
141815e32d5dSMike Smith 	sc->acpi_enabled = 0;
14190ae55423SMike Smith     return_ACPI_STATUS(status);
142015e32d5dSMike Smith }
142115e32d5dSMike Smith 
142215e32d5dSMike Smith /*
142315e32d5dSMike Smith  * ACPI Event Handlers
142415e32d5dSMike Smith  */
142515e32d5dSMike Smith 
142615e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
142715e32d5dSMike Smith 
142815e32d5dSMike Smith static void
142915e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
143015e32d5dSMike Smith {
1431b53f2771SMike Smith     ACPI_FUNCTION_TRACE_U32(__func__, state);
143215e32d5dSMike Smith 
1433cb9b0d80SMike Smith     ACPI_LOCK;
1434a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
143515e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
1436cb9b0d80SMike Smith     ACPI_UNLOCK;
14370ae55423SMike Smith     return_VOID;
143815e32d5dSMike Smith }
143915e32d5dSMike Smith 
144015e32d5dSMike Smith static void
144115e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
144215e32d5dSMike Smith {
1443b53f2771SMike Smith     ACPI_FUNCTION_TRACE_U32(__func__, state);
14440ae55423SMike Smith 
144515e32d5dSMike Smith     /* Well, what to do? :-) */
14460ae55423SMike Smith 
1447cb9b0d80SMike Smith     ACPI_LOCK;
1448cb9b0d80SMike Smith     ACPI_UNLOCK;
1449cb9b0d80SMike Smith 
14500ae55423SMike Smith     return_VOID;
145115e32d5dSMike Smith }
145215e32d5dSMike Smith 
145315e32d5dSMike Smith /*
145415e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
145515e32d5dSMike Smith  */
145615e32d5dSMike Smith UINT32
145715e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context)
145815e32d5dSMike Smith {
145915e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
146015e32d5dSMike Smith 
1461b53f2771SMike Smith     ACPI_FUNCTION_TRACE(__func__);
14620ae55423SMike Smith 
146315e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
14640ae55423SMike Smith 
1465b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
146615e32d5dSMike Smith }
146715e32d5dSMike Smith 
146815e32d5dSMike Smith UINT32
146915e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context)
147015e32d5dSMike Smith {
147115e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
147215e32d5dSMike Smith 
1473b53f2771SMike Smith     ACPI_FUNCTION_TRACE(__func__);
14740ae55423SMike Smith 
147515e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
14760ae55423SMike Smith 
1477b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
147815e32d5dSMike Smith }
147915e32d5dSMike Smith 
148015e32d5dSMike Smith UINT32
148115e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context)
148215e32d5dSMike Smith {
148315e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
148415e32d5dSMike Smith 
1485b53f2771SMike Smith     ACPI_FUNCTION_TRACE(__func__);
14860ae55423SMike Smith 
148715e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
14880ae55423SMike Smith 
1489b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
149015e32d5dSMike Smith }
149115e32d5dSMike Smith 
149215e32d5dSMike Smith UINT32
149315e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context)
149415e32d5dSMike Smith {
149515e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
149615e32d5dSMike Smith 
1497b53f2771SMike Smith     ACPI_FUNCTION_TRACE(__func__);
14980ae55423SMike Smith 
149915e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
15000ae55423SMike Smith 
1501b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
150215e32d5dSMike Smith }
150315e32d5dSMike Smith 
150415e32d5dSMike Smith /*
150515e32d5dSMike Smith  * XXX This is kinda ugly, and should not be here.
150615e32d5dSMike Smith  */
150715e32d5dSMike Smith struct acpi_staticbuf {
150815e32d5dSMike Smith     ACPI_BUFFER	buffer;
150915e32d5dSMike Smith     char	data[512];
151015e32d5dSMike Smith };
151115e32d5dSMike Smith 
151215e32d5dSMike Smith char *
151315e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
151415e32d5dSMike Smith {
151515e32d5dSMike Smith     static struct acpi_staticbuf	buf;
151615e32d5dSMike Smith 
1517cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1518cb9b0d80SMike Smith 
151915e32d5dSMike Smith     buf.buffer.Length = 512;
152015e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
152115e32d5dSMike Smith 
1522b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
152315e32d5dSMike Smith 	return(buf.buffer.Pointer);
152415e32d5dSMike Smith     return("(unknown path)");
152515e32d5dSMike Smith }
152615e32d5dSMike Smith 
152715e32d5dSMike Smith /*
152815e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
152915e32d5dSMike Smith  * parts of the namespace.
153015e32d5dSMike Smith  */
153115e32d5dSMike Smith int
153215e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
153315e32d5dSMike Smith {
153415e32d5dSMike Smith     char	*cp, *np;
153515e32d5dSMike Smith     int		len;
153615e32d5dSMike Smith 
153715e32d5dSMike Smith     np = acpi_name(handle);
153815e32d5dSMike Smith     if (*np == '\\')
153915e32d5dSMike Smith 	np++;
154015e32d5dSMike Smith     if ((cp = getenv("debug.acpi.avoid")) == NULL)
154115e32d5dSMike Smith 	return(0);
154215e32d5dSMike Smith 
154315e32d5dSMike Smith     /* scan the avoid list checking for a match */
154415e32d5dSMike Smith     for (;;) {
154515e32d5dSMike Smith 	while ((*cp != 0) && isspace(*cp))
154615e32d5dSMike Smith 	    cp++;
154715e32d5dSMike Smith 	if (*cp == 0)
154815e32d5dSMike Smith 	    break;
154915e32d5dSMike Smith 	len = 0;
155015e32d5dSMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
155115e32d5dSMike Smith 	    len++;
15524c1cdee6SMike Smith 	if (!strncmp(cp, np, len))
15530ae55423SMike Smith 	    return(1);
15540ae55423SMike Smith 	cp += len;
15550ae55423SMike Smith     }
15560ae55423SMike Smith     return(0);
15570ae55423SMike Smith }
15580ae55423SMike Smith 
15590ae55423SMike Smith /*
15600ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
15610ae55423SMike Smith  */
15620ae55423SMike Smith int
15630ae55423SMike Smith acpi_disabled(char *subsys)
15640ae55423SMike Smith {
15650ae55423SMike Smith     char	*cp;
15660ae55423SMike Smith     int		len;
15670ae55423SMike Smith 
15680ae55423SMike Smith     if ((cp = getenv("debug.acpi.disable")) == NULL)
15690ae55423SMike Smith 	return(0);
15700ae55423SMike Smith     if (!strcmp(cp, "all"))
15710ae55423SMike Smith 	return(1);
15720ae55423SMike Smith 
15730ae55423SMike Smith     /* scan the disable list checking for a match */
15740ae55423SMike Smith     for (;;) {
15750ae55423SMike Smith 	while ((*cp != 0) && isspace(*cp))
15760ae55423SMike Smith 	    cp++;
15770ae55423SMike Smith 	if (*cp == 0)
15780ae55423SMike Smith 	    break;
15790ae55423SMike Smith 	len = 0;
15800ae55423SMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
15810ae55423SMike Smith 	    len++;
15824c1cdee6SMike Smith 	if (!strncmp(cp, subsys, len))
158315e32d5dSMike Smith 	    return(1);
158415e32d5dSMike Smith 	cp += len;
158515e32d5dSMike Smith     }
158615e32d5dSMike Smith     return(0);
158715e32d5dSMike Smith }
158815e32d5dSMike Smith 
158915e32d5dSMike Smith /*
159015e32d5dSMike Smith  * Control interface.
159115e32d5dSMike Smith  *
15920ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
15930ae55423SMike Smith  * drivers wanting to be accessible via /dev/acpi should use the register/deregister
15940ae55423SMike Smith  * interface to make their handlers visible.
159515e32d5dSMike Smith  */
15960ae55423SMike Smith struct acpi_ioctl_hook
15970ae55423SMike Smith {
15980ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook)	link;
15990ae55423SMike Smith     u_long				cmd;
16000ae55423SMike Smith     int					(* fn)(u_long cmd, caddr_t addr, void *arg);
16010ae55423SMike Smith     void				*arg;
16020ae55423SMike Smith };
16030ae55423SMike Smith 
16040ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
16050ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
16060ae55423SMike Smith 
16070ae55423SMike Smith /*
16080ae55423SMike Smith  * Register an ioctl handler.
16090ae55423SMike Smith  */
16100ae55423SMike Smith int
16110ae55423SMike Smith acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg)
16120ae55423SMike Smith {
16130ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
16140ae55423SMike Smith 
16150ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
16160ae55423SMike Smith 	return(ENOMEM);
16170ae55423SMike Smith     hp->cmd = cmd;
16180ae55423SMike Smith     hp->fn = fn;
16190ae55423SMike Smith     hp->arg = arg;
16200ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
16210ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
16220ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
16230ae55423SMike Smith     }
16240ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
16250ae55423SMike Smith     return(0);
16260ae55423SMike Smith }
16270ae55423SMike Smith 
16280ae55423SMike Smith /*
16290ae55423SMike Smith  * Deregister an ioctl handler.
16300ae55423SMike Smith  */
16310ae55423SMike Smith void
16320ae55423SMike Smith acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg))
16330ae55423SMike Smith {
16340ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
16350ae55423SMike Smith 
16360ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
16370ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
16380ae55423SMike Smith 	    break;
16390ae55423SMike Smith 
16400ae55423SMike Smith     if (hp != NULL) {
16410ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
16420ae55423SMike Smith 	free(hp, M_ACPIDEV);
16430ae55423SMike Smith     }
16440ae55423SMike Smith }
16450ae55423SMike Smith 
164615e32d5dSMike Smith static int
1647b40ce416SJulian Elischer acpiopen(dev_t dev, int flag, int fmt, struct thread *td)
164815e32d5dSMike Smith {
164915e32d5dSMike Smith     return(0);
165015e32d5dSMike Smith }
165115e32d5dSMike Smith 
165215e32d5dSMike Smith static int
1653b40ce416SJulian Elischer acpiclose(dev_t dev, int flag, int fmt, struct thread *td)
165415e32d5dSMike Smith {
165515e32d5dSMike Smith     return(0);
165615e32d5dSMike Smith }
165715e32d5dSMike Smith 
165815e32d5dSMike Smith static int
1659b40ce416SJulian Elischer acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
166015e32d5dSMike Smith {
166115e32d5dSMike Smith     struct acpi_softc		*sc;
16620ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
16630ae55423SMike Smith     int				error, xerror, state;
166415e32d5dSMike Smith 
1665cb9b0d80SMike Smith     ACPI_LOCK;
1666cb9b0d80SMike Smith 
166715e32d5dSMike Smith     error = state = 0;
166815e32d5dSMike Smith     sc = dev->si_drv1;
166915e32d5dSMike Smith 
16700ae55423SMike Smith     /*
16710ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
16720ae55423SMike Smith      */
16730ae55423SMike Smith     if (acpi_ioctl_hooks_initted) {
16740ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
16750ae55423SMike Smith 	    if (hp->cmd == cmd) {
16760ae55423SMike Smith 		xerror = hp->fn(cmd, addr, hp->arg);
16770ae55423SMike Smith 		if (xerror != 0)
16780ae55423SMike Smith 		    error = xerror;
1679917d44c8SMitsuru IWASAKI 		goto out;
16800ae55423SMike Smith 	    }
16810ae55423SMike Smith 	}
16820ae55423SMike Smith     }
16830ae55423SMike Smith 
16840ae55423SMike Smith     /*
16850ae55423SMike Smith      * Core system ioctls.
16860ae55423SMike Smith      */
168715e32d5dSMike Smith     switch (cmd) {
168815e32d5dSMike Smith     case ACPIIO_ENABLE:
16890ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
169015e32d5dSMike Smith 	    error = ENXIO;
169115e32d5dSMike Smith 	break;
169215e32d5dSMike Smith 
169315e32d5dSMike Smith     case ACPIIO_DISABLE:
16940ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
169515e32d5dSMike Smith 	    error = ENXIO;
169615e32d5dSMike Smith 	break;
169715e32d5dSMike Smith 
169815e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
169915e32d5dSMike Smith 	if (!sc->acpi_enabled) {
170015e32d5dSMike Smith 	    error = ENXIO;
170115e32d5dSMike Smith 	    break;
170215e32d5dSMike Smith 	}
170315e32d5dSMike Smith 	state = *(int *)addr;
1704a5d1879bSMitsuru IWASAKI 	if (state >= ACPI_STATE_S0  && state <= ACPI_S_STATES_MAX) {
170515e32d5dSMike Smith 	    acpi_SetSleepState(sc, state);
170615e32d5dSMike Smith 	} else {
170715e32d5dSMike Smith 	    error = EINVAL;
170815e32d5dSMike Smith 	}
170915e32d5dSMike Smith 	break;
171015e32d5dSMike Smith 
171115e32d5dSMike Smith     default:
17120ae55423SMike Smith 	if (error == 0)
171315e32d5dSMike Smith 	    error = EINVAL;
171415e32d5dSMike Smith 	break;
171515e32d5dSMike Smith     }
1716917d44c8SMitsuru IWASAKI 
1717917d44c8SMitsuru IWASAKI out:
1718cb9b0d80SMike Smith     ACPI_UNLOCK;
171915e32d5dSMike Smith     return(error);
172015e32d5dSMike Smith }
172115e32d5dSMike Smith 
17221d073b1dSJohn Baldwin static int
17231d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
17241d073b1dSJohn Baldwin {
17251d073b1dSJohn Baldwin     char sleep_state[10];
17261d073b1dSJohn Baldwin     int error;
17271d073b1dSJohn Baldwin     u_int new_state, old_state;
17281d073b1dSJohn Baldwin 
17291d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
1730b8670bedSMitsuru IWASAKI     if (old_state > ACPI_S_STATES_MAX+1) {
17311d073b1dSJohn Baldwin 	strcpy(sleep_state, "unknown");
1732cb9b0d80SMike Smith     } else {
1733b8670bedSMitsuru IWASAKI 	bzero(sleep_state, sizeof(sleep_state));
17341d073b1dSJohn Baldwin 	strncpy(sleep_state, sleep_state_names[old_state],
17351d073b1dSJohn Baldwin 		sizeof(sleep_state_names[old_state]));
1736cb9b0d80SMike Smith     }
17371d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
17381d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
1739b8670bedSMitsuru IWASAKI 	for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX+1; new_state++) {
17401d073b1dSJohn Baldwin 	    if (strncmp(sleep_state, sleep_state_names[new_state],
17411d073b1dSJohn Baldwin 			sizeof(sleep_state)) == 0)
17421d073b1dSJohn Baldwin 		break;
1743cb9b0d80SMike Smith 	}
1744931a10c9SMitsuru IWASAKI 	if (new_state <= ACPI_S_STATES_MAX+1) {
1745931a10c9SMitsuru IWASAKI 	    if (new_state != old_state) {
17461d073b1dSJohn Baldwin 		*(u_int *)oidp->oid_arg1 = new_state;
1747931a10c9SMitsuru IWASAKI 	    }
1748cb9b0d80SMike Smith 	} else {
17491d073b1dSJohn Baldwin 	    error = EINVAL;
17501d073b1dSJohn Baldwin 	}
1751cb9b0d80SMike Smith     }
17521d073b1dSJohn Baldwin     return(error);
17531d073b1dSJohn Baldwin }
17541d073b1dSJohn Baldwin 
175515e32d5dSMike Smith #ifdef ACPI_DEBUG
17560ae55423SMike Smith /*
17570ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
17580ae55423SMike Smith  *
17590ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
17600ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
17610ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
17620ae55423SMike Smith  * prefixing the bit name with !.
17630ae55423SMike Smith  */
176415e32d5dSMike Smith struct debugtag
176515e32d5dSMike Smith {
176615e32d5dSMike Smith     char	*name;
176715e32d5dSMike Smith     UINT32	value;
176815e32d5dSMike Smith };
176915e32d5dSMike Smith 
177015e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
1771c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
1772c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
1773c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
1774c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
1775c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
1776c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
1777c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
1778c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
1779c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
17804c1cdee6SMike Smith     {"ACPI_DEBUGGER",		ACPI_DEBUGGER},
17814c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
17824c1cdee6SMike Smith 
1783cb9b0d80SMike Smith     {"ACPI_BUS",		ACPI_BUS},
17844c1cdee6SMike Smith     {"ACPI_SYSTEM",		ACPI_SYSTEM},
1785cb9b0d80SMike Smith     {"ACPI_POWER",		ACPI_POWER},
1786cb9b0d80SMike Smith     {"ACPI_EC", 		ACPI_EC},
1787c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
1788c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
1789c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
17904c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
1791cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
17924c1cdee6SMike Smith     {"ACPI_FAN",		ACPI_FAN},
17934c1cdee6SMike Smith 
1794b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
1795c5ba0be4SMike Smith     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
179615e32d5dSMike Smith     {NULL, 0}
179715e32d5dSMike Smith };
179815e32d5dSMike Smith 
179915e32d5dSMike Smith static struct debugtag dbg_level[] = {
18004c1cdee6SMike Smith     {"ACPI_LV_OK",		ACPI_LV_OK},
18014c1cdee6SMike Smith     {"ACPI_LV_INFO",		ACPI_LV_INFO},
18024c1cdee6SMike Smith     {"ACPI_LV_WARN",		ACPI_LV_WARN},
18034c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
18044c1cdee6SMike Smith     {"ACPI_LV_FATAL",		ACPI_LV_FATAL},
18054c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
18064c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
18074c1cdee6SMike Smith     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
18084c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
18094c1cdee6SMike Smith     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
18104c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
18114c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
18124c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
18134c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
18144c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
18154c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
18164c1cdee6SMike Smith     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
18174c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
18184c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
18194c1cdee6SMike Smith     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
18204c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
18214c1cdee6SMike Smith     {"ACPI_LV_IO",		ACPI_LV_IO},
18224c1cdee6SMike Smith     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
18234c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
18244c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
18254c1cdee6SMike Smith     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
18264c1cdee6SMike Smith     {"ACPI_LV_INIT",		ACPI_LV_INIT},
18274c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
18284c1cdee6SMike Smith     {"ACPI_DB_AML_DISASSEMBLE",	ACPI_DB_AML_DISASSEMBLE},
18294c1cdee6SMike Smith     {"ACPI_DB_VERBOSE_INFO",	ACPI_DB_VERBOSE_INFO},
18304c1cdee6SMike Smith     {"ACPI_DB_FULL_TABLES",	ACPI_DB_FULL_TABLES},
18314c1cdee6SMike Smith     {"ACPI_DB_EVENTS",		ACPI_DB_EVENTS},
18324c1cdee6SMike Smith     {"ACPI_DB_VERBOSE",		ACPI_DB_VERBOSE},
183315e32d5dSMike Smith     {NULL, 0}
183415e32d5dSMike Smith };
183515e32d5dSMike Smith 
183615e32d5dSMike Smith static void
183715e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
183815e32d5dSMike Smith {
183915e32d5dSMike Smith     char	*ep;
184015e32d5dSMike Smith     int		i, l;
18410ae55423SMike Smith     int		set;
184215e32d5dSMike Smith 
184315e32d5dSMike Smith     while (*cp) {
184415e32d5dSMike Smith 	if (isspace(*cp)) {
184515e32d5dSMike Smith 	    cp++;
184615e32d5dSMike Smith 	    continue;
184715e32d5dSMike Smith 	}
184815e32d5dSMike Smith 	ep = cp;
184915e32d5dSMike Smith 	while (*ep && !isspace(*ep))
185015e32d5dSMike Smith 	    ep++;
18510ae55423SMike Smith 	if (*cp == '!') {
18520ae55423SMike Smith 	    set = 0;
18530ae55423SMike Smith 	    cp++;
18540ae55423SMike Smith 	    if (cp == ep)
18550ae55423SMike Smith 		continue;
18560ae55423SMike Smith 	} else {
18570ae55423SMike Smith 	    set = 1;
18580ae55423SMike Smith 	}
185915e32d5dSMike Smith 	l = ep - cp;
186015e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
186115e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
18620ae55423SMike Smith 		if (set) {
186315e32d5dSMike Smith 		    *flag |= tag[i].value;
18640ae55423SMike Smith 		} else {
18650ae55423SMike Smith 		    *flag &= ~tag[i].value;
18660ae55423SMike Smith 		}
186715e32d5dSMike Smith 		printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
186815e32d5dSMike Smith 	    }
186915e32d5dSMike Smith 	}
187015e32d5dSMike Smith 	cp = ep;
187115e32d5dSMike Smith     }
187215e32d5dSMike Smith }
187315e32d5dSMike Smith 
187415e32d5dSMike Smith static void
18752a4ac806SMike Smith acpi_set_debugging(void *junk)
187615e32d5dSMike Smith {
187715e32d5dSMike Smith     char	*cp;
187815e32d5dSMike Smith 
18790ae55423SMike Smith     AcpiDbgLayer = 0;
18800ae55423SMike Smith     AcpiDbgLevel = 0;
188115e32d5dSMike Smith     if ((cp = getenv("debug.acpi.layer")) != NULL)
188215e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
188315e32d5dSMike Smith     if ((cp = getenv("debug.acpi.level")) != NULL)
188415e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
18850ae55423SMike Smith 
18860ae55423SMike Smith     printf("ACPI debug layer 0x%x  debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel);
188715e32d5dSMike Smith }
18882a4ac806SMike Smith SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL);
188915e32d5dSMike Smith #endif
1890f9390180SMitsuru IWASAKI 
1891f9390180SMitsuru IWASAKI static int
1892f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
1893f9390180SMitsuru IWASAKI {
1894f9390180SMitsuru IWASAKI 	int	state, acpi_state;
1895f9390180SMitsuru IWASAKI 	int	error;
1896f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
1897f9390180SMitsuru IWASAKI 	va_list	ap;
1898f9390180SMitsuru IWASAKI 
1899f9390180SMitsuru IWASAKI 	error = 0;
1900f9390180SMitsuru IWASAKI 	switch (cmd) {
1901f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
1902f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
1903f9390180SMitsuru IWASAKI 		if (sc == NULL) {
1904f9390180SMitsuru IWASAKI 			error = EINVAL;
1905f9390180SMitsuru IWASAKI 			goto out;
1906f9390180SMitsuru IWASAKI 		}
1907f9390180SMitsuru IWASAKI 
1908f9390180SMitsuru IWASAKI 		va_start(ap, arg);
1909f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
1910f9390180SMitsuru IWASAKI 		va_end(ap);
1911f9390180SMitsuru IWASAKI 
1912f9390180SMitsuru IWASAKI 		switch (state) {
1913f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
1914f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
1915f9390180SMitsuru IWASAKI 			break;
1916f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
1917f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
1918f9390180SMitsuru IWASAKI 			break;
1919f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
1920f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
1921f9390180SMitsuru IWASAKI 			break;
1922f9390180SMitsuru IWASAKI 		default:
1923f9390180SMitsuru IWASAKI 			error = EINVAL;
1924f9390180SMitsuru IWASAKI 			goto out;
1925f9390180SMitsuru IWASAKI 		}
1926f9390180SMitsuru IWASAKI 
1927f9390180SMitsuru IWASAKI 		acpi_SetSleepState(sc, acpi_state);
1928f9390180SMitsuru IWASAKI 		break;
1929f9390180SMitsuru IWASAKI 
1930f9390180SMitsuru IWASAKI 	default:
1931f9390180SMitsuru IWASAKI 		error = EINVAL;
1932f9390180SMitsuru IWASAKI 		goto out;
1933f9390180SMitsuru IWASAKI 	}
1934f9390180SMitsuru IWASAKI 
1935f9390180SMitsuru IWASAKI out:
1936f9390180SMitsuru IWASAKI 	return (error);
1937f9390180SMitsuru IWASAKI }
1938f9390180SMitsuru IWASAKI 
1939f9390180SMitsuru IWASAKI static void
1940f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
1941f9390180SMitsuru IWASAKI {
1942f9390180SMitsuru IWASAKI 
1943f9390180SMitsuru IWASAKI 	power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
1944f9390180SMitsuru IWASAKI }
1945f9390180SMitsuru IWASAKI 
1946f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
1947f9390180SMitsuru IWASAKI 
1948