xref: /freebsd/sys/dev/acpica/acpi.c (revision 2d64460768cf7972c961c38d88b3ae0764ad3293)
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>
4515e32d5dSMike Smith 
4615e32d5dSMike Smith #include <machine/clock.h>
4715e32d5dSMike Smith #include <machine/resource.h>
4815e32d5dSMike Smith 
49bc0f2195SMike Smith #include <isa/isavar.h>
50bc0f2195SMike Smith 
5115e32d5dSMike Smith #include "acpi.h"
5215e32d5dSMike Smith 
5315e32d5dSMike Smith #include <dev/acpica/acpivar.h>
5415e32d5dSMike Smith #include <dev/acpica/acpiio.h>
5515e32d5dSMike Smith 
5615e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
5715e32d5dSMike Smith 
5815e32d5dSMike Smith /*
590ae55423SMike Smith  * Hooks for the ACPI CA debugging infrastructure
600ae55423SMike Smith  */
61cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
620ae55423SMike Smith MODULE_NAME("ACPI")
630ae55423SMike Smith 
640ae55423SMike Smith /*
6515e32d5dSMike Smith  * Character device
6615e32d5dSMike Smith  */
6715e32d5dSMike Smith 
6815e32d5dSMike Smith static d_open_t		acpiopen;
6915e32d5dSMike Smith static d_close_t	acpiclose;
7015e32d5dSMike Smith static d_ioctl_t	acpiioctl;
7115e32d5dSMike Smith 
7215e32d5dSMike Smith #define CDEV_MAJOR 152
7315e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
7415e32d5dSMike Smith     acpiopen,
7515e32d5dSMike Smith     acpiclose,
7615e32d5dSMike Smith     noread,
7715e32d5dSMike Smith     nowrite,
7815e32d5dSMike Smith     acpiioctl,
7915e32d5dSMike Smith     nopoll,
8015e32d5dSMike Smith     nommap,
8115e32d5dSMike Smith     nostrategy,
8215e32d5dSMike Smith     "acpi",
8315e32d5dSMike Smith     CDEV_MAJOR,
8415e32d5dSMike Smith     nodump,
8515e32d5dSMike Smith     nopsize,
86606e1d68SJohn Baldwin     0
8715e32d5dSMike Smith };
8815e32d5dSMike Smith 
891d073b1dSJohn Baldwin static const char* sleep_state_names[] = {
90a5d1879bSMitsuru IWASAKI     "S0", "S1", "S2", "S3", "S4", "S5", "S4B" };
911d073b1dSJohn Baldwin 
922a4ac806SMike Smith /* this has to be static, as the softc is gone when we need it */
932a4ac806SMike Smith static int acpi_off_state = ACPI_STATE_S5;
942a4ac806SMike Smith 
95cb9b0d80SMike Smith struct mtx	acpi_mutex;
96cb9b0d80SMike Smith 
976d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
9815e32d5dSMike Smith static void	acpi_identify(driver_t *driver, device_t parent);
9915e32d5dSMike Smith static int	acpi_probe(device_t dev);
10015e32d5dSMike Smith static int	acpi_attach(device_t dev);
10115e32d5dSMike Smith static device_t	acpi_add_child(device_t bus, int order, const char *name, int unit);
10215e32d5dSMike Smith static int	acpi_print_resources(struct resource_list *rl, const char *name, int type,
10315e32d5dSMike Smith 				     const char *format);
10415e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
10515e32d5dSMike Smith static int	acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result);
10615e32d5dSMike Smith static int	acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value);
10715e32d5dSMike Smith static int	acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start,
10815e32d5dSMike Smith 				  u_long count);
10915e32d5dSMike Smith static int	acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp,
11015e32d5dSMike Smith 				  u_long *countp);
11115e32d5dSMike Smith static struct resource *acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
11215e32d5dSMike Smith 					    u_long start, u_long end, u_long count, u_int flags);
11315e32d5dSMike Smith static int	acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r);
11432d18aa5SMike Smith static u_int32_t acpi_isa_get_logicalid(device_t dev);
115bc0f2195SMike Smith static int	acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids);
11615e32d5dSMike Smith 
11715e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
11815e32d5dSMike Smith static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status);
11915e32d5dSMike Smith 
12015e32d5dSMike Smith static void	acpi_shutdown_pre_sync(void *arg, int howto);
12115e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
12215e32d5dSMike Smith 
12313d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
12413d4f7baSMitsuru IWASAKI 
12515e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
12615e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
1271d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
12815e32d5dSMike Smith 
12915e32d5dSMike Smith static device_method_t acpi_methods[] = {
13015e32d5dSMike Smith     /* Device interface */
13115e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
13215e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
13315e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
13415e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
13515e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
13615e32d5dSMike Smith     DEVMETHOD(device_resume,		bus_generic_resume),
13715e32d5dSMike Smith 
13815e32d5dSMike Smith     /* Bus interface */
13915e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
14015e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
14115e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
14215e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
14315e32d5dSMike Smith     DEVMETHOD(bus_set_resource,		acpi_set_resource),
14415e32d5dSMike Smith     DEVMETHOD(bus_get_resource,		acpi_get_resource),
14515e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
14615e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
14715e32d5dSMike Smith     DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
14815e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
14915e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
15015e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
15115e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
15215e32d5dSMike Smith 
153bc0f2195SMike Smith     /* ISA emulation */
154bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
155bc0f2195SMike Smith 
15615e32d5dSMike Smith     {0, 0}
15715e32d5dSMike Smith };
15815e32d5dSMike Smith 
15915e32d5dSMike Smith static driver_t acpi_driver = {
16015e32d5dSMike Smith     "acpi",
16115e32d5dSMike Smith     acpi_methods,
16215e32d5dSMike Smith     sizeof(struct acpi_softc),
16315e32d5dSMike Smith };
16415e32d5dSMike Smith 
16515e32d5dSMike Smith devclass_t acpi_devclass;
1666d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
167dde24897SMike Smith MODULE_VERSION(acpi, 100);
16815e32d5dSMike Smith 
16915e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, "");
17015e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, "");
171dde24897SMike Smith static int acpi_ca_version = ACPI_CA_VERSION;
172dd081ed5SMitsuru IWASAKI SYSCTL_INT(_debug, OID_AUTO, acpi_ca_version, CTLFLAG_RD, &acpi_ca_version, 0, "");
17315e32d5dSMike Smith 
17415e32d5dSMike Smith /*
1756d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
1766d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
1776d63101aSMike Smith  * It also cannot be unloaded, since the entire system bus heirarchy hangs off it.
1786d63101aSMike Smith  */
1796d63101aSMike Smith static int
1806d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
1816d63101aSMike Smith {
1826d63101aSMike Smith     switch(event) {
1836d63101aSMike Smith     case MOD_LOAD:
1846d63101aSMike Smith 	if (!cold)
1856d63101aSMike Smith 	    return(EPERM);
1866d63101aSMike Smith 	break;
1876d63101aSMike Smith     case MOD_UNLOAD:
1886d63101aSMike Smith 	return(EBUSY);
1896d63101aSMike Smith     default:
1906d63101aSMike Smith 	break;
1916d63101aSMike Smith     }
1926d63101aSMike Smith     return(0);
1936d63101aSMike Smith }
1946d63101aSMike Smith 
1956d63101aSMike Smith /*
19615e32d5dSMike Smith  * Detect ACPI, perform early initialisation
19715e32d5dSMike Smith  */
19815e32d5dSMike Smith static void
19915e32d5dSMike Smith acpi_identify(driver_t *driver, device_t parent)
20015e32d5dSMike Smith {
20115e32d5dSMike Smith     device_t			child;
20215e32d5dSMike Smith     int				error;
20315e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
20415e32d5dSMike Smith     char			*debugpoint = getenv("debug.acpi.debugger");
20515e32d5dSMike Smith #endif
20615e32d5dSMike Smith 
2072a4ac806SMike Smith     FUNCTION_TRACE(__func__);
2080ae55423SMike Smith 
209840f9b53STakanori Watanabe     if(!cold){
210840f9b53STakanori Watanabe 	    printf("Don't load this driver from userland!!\n");
211840f9b53STakanori Watanabe 	    return ;
212840f9b53STakanori Watanabe     }
213840f9b53STakanori Watanabe 
21415e32d5dSMike Smith     /*
21532d18aa5SMike Smith      * Check that we haven't been disabled with a hint.
21632d18aa5SMike Smith      */
21732d18aa5SMike Smith     if (!resource_int_value("acpi", 0, "disabled", &error) &&
21832d18aa5SMike Smith 	(error != 0))
21932d18aa5SMike Smith 	return_VOID;
22032d18aa5SMike Smith 
22132d18aa5SMike Smith     /*
22215e32d5dSMike Smith      * Make sure we're not being doubly invoked.
22315e32d5dSMike Smith      */
22415e32d5dSMike Smith     if (device_find_child(parent, "acpi", 0) != NULL)
2250ae55423SMike Smith 	return_VOID;
22615e32d5dSMike Smith 
227cb9b0d80SMike Smith     /* initialise the ACPI mutex */
228cb9b0d80SMike Smith     mtx_init(&acpi_mutex, "ACPI global lock", MTX_DEF);
229cb9b0d80SMike Smith 
23015e32d5dSMike Smith     /*
2312a4ac806SMike Smith      * Start up the ACPI CA subsystem.
23215e32d5dSMike Smith      */
23315e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
23415e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "init"))
23515e32d5dSMike Smith 	acpi_EnterDebugger();
23615e32d5dSMike Smith #endif
23715e32d5dSMike Smith     if ((error = AcpiInitializeSubsystem()) != AE_OK) {
238bfae45aaSMike Smith 	printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
2390ae55423SMike Smith 	return_VOID;
24015e32d5dSMike Smith     }
24115e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
24215e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "tables"))
24315e32d5dSMike Smith 	acpi_EnterDebugger();
24415e32d5dSMike Smith #endif
245acf72ef4SMike Smith     if ((error = AcpiLoadTables()) != AE_OK) {
246bfae45aaSMike Smith 	printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
2470ae55423SMike Smith 	return_VOID;
24815e32d5dSMike Smith     }
24915e32d5dSMike Smith 
25015e32d5dSMike Smith     /*
25115e32d5dSMike Smith      * Attach the actual ACPI device.
25215e32d5dSMike Smith      */
25315e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
25415e32d5dSMike Smith 	    device_printf(parent, "ACPI: could not attach\n");
2550ae55423SMike Smith 	    return_VOID;
25615e32d5dSMike Smith     }
25715e32d5dSMike Smith }
25815e32d5dSMike Smith 
25915e32d5dSMike Smith /*
26015e32d5dSMike Smith  * Fetch some descriptive data from ACPI to put in our attach message
26115e32d5dSMike Smith  */
26215e32d5dSMike Smith static int
26315e32d5dSMike Smith acpi_probe(device_t dev)
26415e32d5dSMike Smith {
26515e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
26615e32d5dSMike Smith     char		buf[20];
267cb9b0d80SMike Smith     ACPI_STATUS		status;
26815e32d5dSMike Smith     int			error;
26915e32d5dSMike Smith 
2702a4ac806SMike Smith     FUNCTION_TRACE(__func__);
271cb9b0d80SMike Smith     ACPI_LOCK;
2720ae55423SMike Smith 
273cb9b0d80SMike Smith     if ((status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th)) != AE_OK) {
274bfae45aaSMike Smith 	device_printf(dev, "couldn't get XSDT header: %s\n", AcpiFormatException(status));
275cb9b0d80SMike Smith 	error = ENXIO;
276cb9b0d80SMike Smith     } else {
27715e32d5dSMike Smith 	sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId);
27815e32d5dSMike Smith 	device_set_desc_copy(dev, buf);
279cb9b0d80SMike Smith 	error = 0;
280cb9b0d80SMike Smith     }
281cb9b0d80SMike Smith     ACPI_UNLOCK;
282cb9b0d80SMike Smith     return_VALUE(error);
28315e32d5dSMike Smith }
28415e32d5dSMike Smith 
28515e32d5dSMike Smith static int
28615e32d5dSMike Smith acpi_attach(device_t dev)
28715e32d5dSMike Smith {
28815e32d5dSMike Smith     struct acpi_softc	*sc;
289cb9b0d80SMike Smith     ACPI_STATUS		status;
29015e32d5dSMike Smith     int			error;
29132d18aa5SMike Smith     UINT32		flags;
29232d18aa5SMike Smith 
29315e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
29415e32d5dSMike Smith     char		*debugpoint = getenv("debug.acpi.debugger");
29515e32d5dSMike Smith #endif
29615e32d5dSMike Smith 
2972a4ac806SMike Smith     FUNCTION_TRACE(__func__);
298cb9b0d80SMike Smith     ACPI_LOCK;
29915e32d5dSMike Smith     sc = device_get_softc(dev);
30015e32d5dSMike Smith     bzero(sc, sizeof(*sc));
30115e32d5dSMike Smith     sc->acpi_dev = dev;
30215e32d5dSMike Smith 
30315e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
30415e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "spaces"))
30515e32d5dSMike Smith 	acpi_EnterDebugger();
30615e32d5dSMike Smith #endif
30715e32d5dSMike Smith 
30815e32d5dSMike Smith     /*
30915e32d5dSMike Smith      * Install the default address space handlers.
31015e32d5dSMike Smith      */
311cb9b0d80SMike Smith     error = ENXIO;
312cb9b0d80SMike Smith     if ((status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
3132a4ac806SMike Smith 						ACPI_ADR_SPACE_SYSTEM_MEMORY,
31415e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
31515e32d5dSMike Smith 						NULL, NULL)) != AE_OK) {
316bfae45aaSMike Smith 	device_printf(dev, "could not initialise SystemMemory handler: %s\n", AcpiFormatException(status));
317cb9b0d80SMike Smith 	goto out;
31815e32d5dSMike Smith     }
319cb9b0d80SMike Smith     if ((status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
3202a4ac806SMike Smith 						ACPI_ADR_SPACE_SYSTEM_IO,
32115e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
32215e32d5dSMike Smith 						NULL, NULL)) != AE_OK) {
323bfae45aaSMike Smith 	device_printf(dev, "could not initialise SystemIO handler: %s\n", AcpiFormatException(status));
324cb9b0d80SMike Smith 	goto out;
32515e32d5dSMike Smith     }
326cb9b0d80SMike Smith     if ((status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
3272a4ac806SMike Smith 						ACPI_ADR_SPACE_PCI_CONFIG,
32815e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
32915e32d5dSMike Smith 						NULL, NULL)) != AE_OK) {
330bfae45aaSMike Smith 	device_printf(dev, "could not initialise PciConfig handler: %s\n", AcpiFormatException(status));
331cb9b0d80SMike Smith 	goto out;
33215e32d5dSMike Smith     }
33315e32d5dSMike Smith 
33415e32d5dSMike Smith     /*
33515e32d5dSMike Smith      * Bring ACPI fully online.
33615e32d5dSMike Smith      *
33732d18aa5SMike Smith      * Note that some systems (specifically, those with namespace evaluation issues
33832d18aa5SMike Smith      * that require the avoidance of parts of the namespace) must avoid running _INI
33932d18aa5SMike Smith      * and _STA on everything, as well as dodging the final object init pass.
34015e32d5dSMike Smith      *
34132d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
34232d18aa5SMike Smith      *
34332d18aa5SMike Smith      * XXX We should arrange for the object init pass after we have attached all our
34432d18aa5SMike Smith      *     child devices, but on many systems it works here.
34515e32d5dSMike Smith      */
34615e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
34715e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "enable"))
34815e32d5dSMike Smith 	acpi_EnterDebugger();
34915e32d5dSMike Smith #endif
35032d18aa5SMike Smith     flags = 0;
351b525621aSMike Smith     if (getenv("debug.acpi.avoid") != NULL)
35232d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
35332d18aa5SMike Smith     if ((status = AcpiEnableSubsystem(flags)) != AE_OK) {
354bfae45aaSMike Smith 	device_printf(dev, "could not enable ACPI: %s\n", AcpiFormatException(status));
355cb9b0d80SMike Smith 	goto out;
35615e32d5dSMike Smith     }
35715e32d5dSMike Smith 
35815e32d5dSMike Smith     /*
3591d073b1dSJohn Baldwin      * Setup our sysctl tree.
3601d073b1dSJohn Baldwin      *
3611d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
3621d073b1dSJohn Baldwin      */
3631d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
3641d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
3651d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
3661d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
3671d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
3681d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
3691d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
3701d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
3711d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
3721d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
3731d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
3741d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
3751d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
376f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
377f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
378f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
379f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
380f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
381f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
3822d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
3832d644607SMitsuru IWASAKI 	OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
3842d644607SMitsuru IWASAKI 	&sc->acpi_verbose, 0, "verbose mode");
3852d644607SMitsuru IWASAKI     if (bootverbose)
3862d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
3871d073b1dSJohn Baldwin 
3881d073b1dSJohn Baldwin     /*
38915e32d5dSMike Smith      * Dispatch the default sleep state to devices.
39015e32d5dSMike Smith      * TBD: should be configured from userland policy manager.
39115e32d5dSMike Smith      */
39215e32d5dSMike Smith     sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
39315e32d5dSMike Smith     sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
39415e32d5dSMike Smith     sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
395f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
396f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
39715e32d5dSMike Smith 
39813d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
39915e32d5dSMike Smith 
40015e32d5dSMike Smith     /*
40115e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
40215e32d5dSMike Smith      */
40315e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
40415e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "probe"))
40515e32d5dSMike Smith 	acpi_EnterDebugger();
40615e32d5dSMike Smith #endif
4070ae55423SMike Smith     if (!acpi_disabled("bus"))
40815e32d5dSMike Smith 	acpi_probe_children(dev);
40915e32d5dSMike Smith 
41015e32d5dSMike Smith     /*
41115e32d5dSMike Smith      * Register our shutdown handlers
41215e32d5dSMike Smith      */
41315e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST);
41415e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST);
41515e32d5dSMike Smith 
41615e32d5dSMike Smith     /*
41715e32d5dSMike Smith      * Register our acpi event handlers.
41815e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
41915e32d5dSMike Smith      */
42015e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST);
42115e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST);
42215e32d5dSMike Smith 
42315e32d5dSMike Smith     /*
42415e32d5dSMike Smith      * Flag our initial states.
42515e32d5dSMike Smith      */
42615e32d5dSMike Smith     sc->acpi_enabled = 1;
42715e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
42815e32d5dSMike Smith 
42915e32d5dSMike Smith     /*
43015e32d5dSMike Smith      * Create the control device
43115e32d5dSMike Smith      */
43215e32d5dSMike Smith     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, 0, 5, 0660, "acpi");
43315e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
43415e32d5dSMike Smith 
43515e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
43615e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "running"))
43715e32d5dSMike Smith 	acpi_EnterDebugger();
43815e32d5dSMike Smith #endif
439f86214b6SMitsuru IWASAKI 
440f86214b6SMitsuru IWASAKI     if ((error = acpi_machdep_init(dev))) {
441f86214b6SMitsuru IWASAKI 	goto out;
442f86214b6SMitsuru IWASAKI     }
443f86214b6SMitsuru IWASAKI 
444cb9b0d80SMike Smith     error = 0;
445cb9b0d80SMike Smith 
446cb9b0d80SMike Smith  out:
447cb9b0d80SMike Smith     ACPI_UNLOCK;
448cb9b0d80SMike Smith     return_VALUE(error);
44915e32d5dSMike Smith }
45015e32d5dSMike Smith 
45115e32d5dSMike Smith /*
45215e32d5dSMike Smith  * Handle a new device being added
45315e32d5dSMike Smith  */
45415e32d5dSMike Smith static device_t
45515e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
45615e32d5dSMike Smith {
45715e32d5dSMike Smith     struct acpi_device	*ad;
45815e32d5dSMike Smith     device_t		child;
45915e32d5dSMike Smith 
46015e32d5dSMike Smith     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL)
46115e32d5dSMike Smith 	return(NULL);
46215e32d5dSMike Smith     bzero(ad, sizeof(*ad));
46315e32d5dSMike Smith 
46415e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
46515e32d5dSMike Smith 
46615e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
46715e32d5dSMike Smith     if (child != NULL)
46815e32d5dSMike Smith 	device_set_ivars(child, ad);
46915e32d5dSMike Smith     return(child);
47015e32d5dSMike Smith }
47115e32d5dSMike Smith 
47215e32d5dSMike Smith /*
47315e32d5dSMike Smith  * Print child device resource usage
47415e32d5dSMike Smith  */
47515e32d5dSMike Smith static int
47615e32d5dSMike Smith acpi_print_resources(struct resource_list *rl, const char *name, int type, const char *format)
47715e32d5dSMike Smith {
47815e32d5dSMike Smith     struct resource_list_entry	*rle;
47915e32d5dSMike Smith     int				printed, retval;
48015e32d5dSMike Smith 
48115e32d5dSMike Smith     printed = 0;
48215e32d5dSMike Smith     retval = 0;
48315e32d5dSMike Smith 
48415e32d5dSMike Smith     if (!SLIST_FIRST(rl))
48515e32d5dSMike Smith 	return(0);
48615e32d5dSMike Smith 
48715e32d5dSMike Smith     /* Yes, this is kinda cheating */
48815e32d5dSMike Smith     SLIST_FOREACH(rle, rl, link) {
48915e32d5dSMike Smith 	if (rle->type == type) {
49015e32d5dSMike Smith 	    if (printed == 0)
49115e32d5dSMike Smith 		retval += printf(" %s ", name);
49215e32d5dSMike Smith 	    else if (printed > 0)
49315e32d5dSMike Smith 		retval += printf(",");
49415e32d5dSMike Smith 	    printed++;
49515e32d5dSMike Smith 	    retval += printf(format, rle->start);
49615e32d5dSMike Smith 	    if (rle->count > 1) {
49715e32d5dSMike Smith 		retval += printf("-");
49815e32d5dSMike Smith 		retval += printf(format, rle->start +
49915e32d5dSMike Smith 				 rle->count - 1);
50015e32d5dSMike Smith 	    }
50115e32d5dSMike Smith 	}
50215e32d5dSMike Smith     }
50315e32d5dSMike Smith     return(retval);
50415e32d5dSMike Smith }
50515e32d5dSMike Smith 
50615e32d5dSMike Smith static int
50715e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
50815e32d5dSMike Smith {
50915e32d5dSMike Smith     struct acpi_device		*adev = device_get_ivars(child);
51015e32d5dSMike Smith     struct resource_list	*rl = &adev->ad_rl;
51115e32d5dSMike Smith     int retval = 0;
51215e32d5dSMike Smith 
51315e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
51415e32d5dSMike Smith     retval += acpi_print_resources(rl, "port",  SYS_RES_IOPORT, "%#lx");
51515e32d5dSMike Smith     retval += acpi_print_resources(rl, "iomem", SYS_RES_MEMORY, "%#lx");
51615e32d5dSMike Smith     retval += acpi_print_resources(rl, "irq",   SYS_RES_IRQ,    "%ld");
51715e32d5dSMike Smith     retval += bus_print_child_footer(bus, child);
51815e32d5dSMike Smith 
51915e32d5dSMike Smith     return(retval);
52015e32d5dSMike Smith }
52115e32d5dSMike Smith 
52215e32d5dSMike Smith 
52315e32d5dSMike Smith /*
52415e32d5dSMike Smith  * Handle per-device ivars
52515e32d5dSMike Smith  */
52615e32d5dSMike Smith static int
52715e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
52815e32d5dSMike Smith {
52915e32d5dSMike Smith     struct acpi_device	*ad;
53015e32d5dSMike Smith 
53115e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
53215e32d5dSMike Smith 	printf("device has no ivars\n");
53315e32d5dSMike Smith 	return(ENOENT);
53415e32d5dSMike Smith     }
53515e32d5dSMike Smith 
53615e32d5dSMike Smith     switch(index) {
53715e32d5dSMike Smith 	/* ACPI ivars */
53815e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
53915e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
54015e32d5dSMike Smith 	break;
54115e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
54215e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
54315e32d5dSMike Smith 	break;
54415e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
54515e32d5dSMike Smith 	*(void **)result = ad->ad_private;
54615e32d5dSMike Smith 	break;
54715e32d5dSMike Smith 
54832d18aa5SMike Smith 	/* ISA compatibility */
54932d18aa5SMike Smith     case ISA_IVAR_VENDORID:
55032d18aa5SMike Smith     case ISA_IVAR_SERIAL:
55132d18aa5SMike Smith     case ISA_IVAR_COMPATID:
55232d18aa5SMike Smith 	*(int *)result = -1;
55332d18aa5SMike Smith 	break;
55432d18aa5SMike Smith 
55532d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
55632d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
55732d18aa5SMike Smith 	break;
55832d18aa5SMike Smith 
55915e32d5dSMike Smith     default:
56015e32d5dSMike Smith 	panic("bad ivar read request (%d)\n", index);
56115e32d5dSMike Smith 	return(ENOENT);
56215e32d5dSMike Smith     }
56315e32d5dSMike Smith     return(0);
56415e32d5dSMike Smith }
56515e32d5dSMike Smith 
56615e32d5dSMike Smith static int
56715e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
56815e32d5dSMike Smith {
56915e32d5dSMike Smith     struct acpi_device	*ad;
57015e32d5dSMike Smith 
57115e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
57215e32d5dSMike Smith 	printf("device has no ivars\n");
57315e32d5dSMike Smith 	return(ENOENT);
57415e32d5dSMike Smith     }
57515e32d5dSMike Smith 
57615e32d5dSMike Smith     switch(index) {
57715e32d5dSMike Smith 	/* ACPI ivars */
57815e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
57915e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
58015e32d5dSMike Smith 	break;
58115e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
58215e32d5dSMike Smith 	ad->ad_magic = (int )value;
58315e32d5dSMike Smith 	break;
58415e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
58515e32d5dSMike Smith 	ad->ad_private = (void *)value;
58615e32d5dSMike Smith 	break;
58715e32d5dSMike Smith 
58815e32d5dSMike Smith     default:
58915e32d5dSMike Smith 	panic("bad ivar write request (%d)\n", index);
59015e32d5dSMike Smith 	return(ENOENT);
59115e32d5dSMike Smith     }
59215e32d5dSMike Smith     return(0);
59315e32d5dSMike Smith }
59415e32d5dSMike Smith 
59515e32d5dSMike Smith /*
59615e32d5dSMike Smith  * Handle child resource allocation/removal
59715e32d5dSMike Smith  */
59815e32d5dSMike Smith static int
59915e32d5dSMike Smith acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
60015e32d5dSMike Smith {
60115e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
60215e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
60315e32d5dSMike Smith 
60415e32d5dSMike Smith     resource_list_add(rl, type, rid, start, start + count -1, count);
60515e32d5dSMike Smith 
60615e32d5dSMike Smith     return(0);
60715e32d5dSMike Smith }
60815e32d5dSMike Smith 
60915e32d5dSMike Smith static int
61015e32d5dSMike Smith acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
61115e32d5dSMike Smith {
61215e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
61315e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
61415e32d5dSMike Smith     struct resource_list_entry	*rle;
61515e32d5dSMike Smith 
61615e32d5dSMike Smith     rle = resource_list_find(rl, type, rid);
61715e32d5dSMike Smith     if (!rle)
61815e32d5dSMike Smith 	return(ENOENT);
61915e32d5dSMike Smith 
62015e32d5dSMike Smith     if (startp)
62115e32d5dSMike Smith 	*startp = rle->start;
62215e32d5dSMike Smith     if (countp)
62315e32d5dSMike Smith 	*countp = rle->count;
62415e32d5dSMike Smith 
62515e32d5dSMike Smith     return(0);
62615e32d5dSMike Smith }
62715e32d5dSMike Smith 
62815e32d5dSMike Smith static struct resource *
62915e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
63015e32d5dSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
63115e32d5dSMike Smith {
63215e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
63315e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
63415e32d5dSMike Smith 
63515e32d5dSMike Smith     return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags));
63615e32d5dSMike Smith }
63715e32d5dSMike Smith 
63815e32d5dSMike Smith static int
63915e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
64015e32d5dSMike Smith {
64115e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
64215e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
64315e32d5dSMike Smith 
64415e32d5dSMike Smith     return(resource_list_release(rl, bus, child, type, rid, r));
64515e32d5dSMike Smith }
64615e32d5dSMike Smith 
64715e32d5dSMike Smith /*
648bc0f2195SMike Smith  * Handle ISA-like devices probing for a PnP ID to match.
649bc0f2195SMike Smith  */
650bc0f2195SMike Smith #define PNP_EISAID(s)				\
651bc0f2195SMike Smith 	((((s[0] - '@') & 0x1f) << 2)		\
652bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x18) >> 3)		\
653bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x07) << 13)	\
654bc0f2195SMike Smith 	 | (((s[2] - '@') & 0x1f) << 8)		\
655bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[4]) << 16)		\
656bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[3]) << 20)		\
657bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[6]) << 24)		\
658bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[5]) << 28))
659bc0f2195SMike Smith 
66032d18aa5SMike Smith static u_int32_t
66132d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
662bc0f2195SMike Smith {
663bc0f2195SMike Smith     ACPI_HANDLE		h;
664bc0f2195SMike Smith     ACPI_DEVICE_INFO	devinfo;
665bc0f2195SMike Smith     ACPI_STATUS		error;
666bc0f2195SMike Smith     u_int32_t		pnpid;
66732d18aa5SMike Smith 
66832d18aa5SMike Smith     FUNCTION_TRACE(__func__);
66932d18aa5SMike Smith 
67032d18aa5SMike Smith     pnpid = 0;
67132d18aa5SMike Smith     ACPI_LOCK;
67232d18aa5SMike Smith 
67332d18aa5SMike Smith     /* fetch and validate the HID */
67432d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
67532d18aa5SMike Smith 	goto out;
67632d18aa5SMike Smith     if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK)
67732d18aa5SMike Smith 	goto out;
67832d18aa5SMike Smith     if (!(devinfo.Valid & ACPI_VALID_HID))
67932d18aa5SMike Smith 	goto out;
68032d18aa5SMike Smith 
68132d18aa5SMike Smith     pnpid = PNP_EISAID(devinfo.HardwareId);
68232d18aa5SMike Smith out:
68332d18aa5SMike Smith     ACPI_UNLOCK;
68432d18aa5SMike Smith     return_VALUE(pnpid);
68532d18aa5SMike Smith }
68632d18aa5SMike Smith 
68732d18aa5SMike Smith static int
68832d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
68932d18aa5SMike Smith {
690bc0f2195SMike Smith     int			result;
69132d18aa5SMike Smith     u_int32_t		pnpid;
692bc0f2195SMike Smith 
693bc0f2195SMike Smith     FUNCTION_TRACE(__func__);
694bc0f2195SMike Smith 
695bc0f2195SMike Smith     /*
696bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
697bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
698bc0f2195SMike Smith      * that to happen, so don't ever return it.
699bc0f2195SMike Smith      */
700bc0f2195SMike Smith     result = ENXIO;
701bc0f2195SMike Smith 
702bc0f2195SMike Smith     /* scan the supplied IDs for a match */
70332d18aa5SMike Smith     pnpid = acpi_isa_get_logicalid(child);
704bc0f2195SMike Smith     while (ids && ids->ip_id) {
705bc0f2195SMike Smith 	if (pnpid == ids->ip_id) {
706bc0f2195SMike Smith 	    result = 0;
707bc0f2195SMike Smith 	    goto out;
708bc0f2195SMike Smith 	}
709bc0f2195SMike Smith 	ids++;
710bc0f2195SMike Smith     }
711bc0f2195SMike Smith  out:
712bc0f2195SMike Smith     return_VALUE(result);
713bc0f2195SMike Smith }
714bc0f2195SMike Smith 
715bc0f2195SMike Smith /*
71615e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
71715e32d5dSMike Smith  *
7187d3bcec9SMike Smith  * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and \_SB_ scopes,
7197d3bcec9SMike Smith  * and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
72015e32d5dSMike Smith  */
72115e32d5dSMike Smith static void
72215e32d5dSMike Smith acpi_probe_children(device_t bus)
72315e32d5dSMike Smith {
72415e32d5dSMike Smith     ACPI_HANDLE		parent;
7257d3bcec9SMike Smith     static char		*scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
72615e32d5dSMike Smith     int			i;
72715e32d5dSMike Smith 
7282a4ac806SMike Smith     FUNCTION_TRACE(__func__);
729cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
7300ae55423SMike Smith 
73115e32d5dSMike Smith     /*
73215e32d5dSMike Smith      * Create any static children by calling device identify methods.
73315e32d5dSMike Smith      */
7344c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
73515e32d5dSMike Smith     bus_generic_probe(bus);
73615e32d5dSMike Smith 
73715e32d5dSMike Smith     /*
73815e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
73915e32d5dSMike Smith      * we find.
74015e32d5dSMike Smith      *
74115e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
74215e32d5dSMike Smith      * we want to create nodes for all devices, not just those that are currently
74315e32d5dSMike Smith      * present. (This assumes that we don't want to create/remove devices as they
74415e32d5dSMike Smith      * appear, which might be smarter.)
74515e32d5dSMike Smith      */
7464c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
74715e32d5dSMike Smith     for (i = 0; scopes[i] != NULL; i++)
74815e32d5dSMike Smith 	if ((AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent)) == AE_OK)
74915e32d5dSMike Smith 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL);
75015e32d5dSMike Smith 
75115e32d5dSMike Smith     /*
75215e32d5dSMike Smith      * Scan all of the child devices we have created and let them probe/attach.
75315e32d5dSMike Smith      */
7544c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
75515e32d5dSMike Smith     bus_generic_attach(bus);
75615e32d5dSMike Smith 
75715e32d5dSMike Smith     /*
75815e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
75915e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
76015e32d5dSMike Smith      */
7614c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
76215e32d5dSMike Smith     bus_generic_attach(bus);
7630ae55423SMike Smith 
764bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
7650ae55423SMike Smith     return_VOID;
76615e32d5dSMike Smith }
76715e32d5dSMike Smith 
76815e32d5dSMike Smith /*
76915e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
77015e32d5dSMike Smith  * it.
77115e32d5dSMike Smith  */
77215e32d5dSMike Smith static ACPI_STATUS
77315e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
77415e32d5dSMike Smith {
77515e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
77615e32d5dSMike Smith     device_t		child, bus = (device_t)context;
77715e32d5dSMike Smith 
7782a4ac806SMike Smith     FUNCTION_TRACE(__func__);
7790ae55423SMike Smith 
7800ae55423SMike Smith     /*
7810ae55423SMike Smith      * Skip this device if we think we'll have trouble with it.
7820ae55423SMike Smith      */
7830ae55423SMike Smith     if (acpi_avoid(handle))
7840ae55423SMike Smith 	return_ACPI_STATUS(AE_OK);
7850ae55423SMike Smith 
78615e32d5dSMike Smith     if (AcpiGetType(handle, &type) == AE_OK) {
78715e32d5dSMike Smith 	switch(type) {
78815e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
78915e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
79015e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
79115e32d5dSMike Smith 	case ACPI_TYPE_POWER:
7920ae55423SMike Smith 	    if (acpi_disabled("children"))
7930ae55423SMike Smith 		break;
79415e32d5dSMike Smith 	    /*
79515e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
79615e32d5dSMike Smith 	     * so that the probe/attach passes will run breadth-first.
79715e32d5dSMike Smith 	     */
7984c1cdee6SMike Smith 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", acpi_name(handle)));
79915e32d5dSMike Smith 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
800bc0f2195SMike Smith 	    if (child == NULL)
801bc0f2195SMike Smith 		break;
80215e32d5dSMike Smith 	    acpi_set_handle(child, handle);
803bc0f2195SMike Smith 
804bc0f2195SMike Smith 	    /*
805b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
806b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
807b519f9d6SMike Smith 	     * the handle, but we don't probe it).
808b519f9d6SMike Smith 	     */
80923b4e251SMitsuru IWASAKI 	    if ((type == ACPI_TYPE_DEVICE) && (!acpi_DeviceIsPresent(child))) {
810b519f9d6SMike Smith 		device_disable(child);
811b519f9d6SMike Smith 		break;
812b519f9d6SMike Smith 	    }
813b519f9d6SMike Smith 
814b519f9d6SMike Smith 	    /*
815bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
816bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
817bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
818bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
819bc0f2195SMike Smith 	     * device not to have any resources.
820bc0f2195SMike Smith 	     */
821bc0f2195SMike Smith 	    acpi_parse_resources(child, handle, &acpi_res_parse_set);
822bc0f2195SMike Smith 
823bc0f2195SMike Smith 	    /* if we're debugging, probe/attach now rather than later */
8240ae55423SMike Smith 	    DEBUG_EXEC(device_probe_and_attach(child));
8250ae55423SMike Smith 	    break;
82615e32d5dSMike Smith 	}
82715e32d5dSMike Smith     }
8280ae55423SMike Smith     return_ACPI_STATUS(AE_OK);
82915e32d5dSMike Smith }
83015e32d5dSMike Smith 
83115e32d5dSMike Smith static void
83215e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto)
83315e32d5dSMike Smith {
834cb9b0d80SMike Smith 
835cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
836cb9b0d80SMike Smith 
83715e32d5dSMike Smith     /*
8380ae55423SMike Smith      * Disable all ACPI events before soft off, otherwise the system
83915e32d5dSMike Smith      * will be turned on again on some laptops.
84015e32d5dSMike Smith      *
84115e32d5dSMike Smith      * XXX this should probably be restricted to masking some events just
84215e32d5dSMike Smith      *     before powering down, since we may still need ACPI during the
84315e32d5dSMike Smith      *     shutdown process.
84415e32d5dSMike Smith      */
84515e32d5dSMike Smith     acpi_Disable((struct acpi_softc *)arg);
84615e32d5dSMike Smith }
84715e32d5dSMike Smith 
84815e32d5dSMike Smith static void
84915e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
85015e32d5dSMike Smith {
85115e32d5dSMike Smith     ACPI_STATUS	status;
85215e32d5dSMike Smith 
853cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
854cb9b0d80SMike Smith 
8555f3e4175SMitsuru IWASAKI     if (howto & RB_POWEROFF) {
85615e32d5dSMike Smith 	printf("Power system off using ACPI...\n");
8572a4ac806SMike Smith 	if ((status = AcpiEnterSleepState(acpi_off_state)) != AE_OK) {
858bfae45aaSMike Smith 	    printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
85915e32d5dSMike Smith 	} else {
86015e32d5dSMike Smith 	    DELAY(1000000);
86115e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
86215e32d5dSMike Smith 	}
86315e32d5dSMike Smith     }
86415e32d5dSMike Smith }
86515e32d5dSMike Smith 
86613d4f7baSMitsuru IWASAKI static void
86713d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
86813d4f7baSMitsuru IWASAKI {
86913d4f7baSMitsuru IWASAKI     static int	first_time = 1;
87013d4f7baSMitsuru IWASAKI #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n"
87113d4f7baSMitsuru IWASAKI 
872cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
873cb9b0d80SMike Smith 
87413d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
875cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) {
87643896e91SMike Smith 	AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED, 0);
87713d4f7baSMitsuru IWASAKI 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED);
87813d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
87913d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_power_button_for_sleep, sc);
88013d4f7baSMitsuru IWASAKI 	if (first_time) {
88113d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "power");
88213d4f7baSMitsuru IWASAKI 	}
88313d4f7baSMitsuru IWASAKI     }
884cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) {
88543896e91SMike Smith 	AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED, 0);
88613d4f7baSMitsuru IWASAKI 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED);
88713d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
88813d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_sleep_button_for_sleep, sc);
88913d4f7baSMitsuru IWASAKI 	if (first_time) {
89013d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "sleep");
89113d4f7baSMitsuru IWASAKI 	}
89213d4f7baSMitsuru IWASAKI     }
89313d4f7baSMitsuru IWASAKI 
89413d4f7baSMitsuru IWASAKI     first_time = 0;
89513d4f7baSMitsuru IWASAKI }
89613d4f7baSMitsuru IWASAKI 
89715e32d5dSMike Smith /*
898c5ba0be4SMike Smith  * Returns true if the device is actually present and should
899c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
900c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
901c5ba0be4SMike Smith  */
902c5ba0be4SMike Smith BOOLEAN
903c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
904c5ba0be4SMike Smith {
905c5ba0be4SMike Smith     ACPI_HANDLE		h;
906c5ba0be4SMike Smith     ACPI_DEVICE_INFO	devinfo;
907c5ba0be4SMike Smith     ACPI_STATUS		error;
908c5ba0be4SMike Smith 
909cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
910cb9b0d80SMike Smith 
911c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
912c5ba0be4SMike Smith 	return(FALSE);
913c5ba0be4SMike Smith     if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK)
914c5ba0be4SMike Smith 	return(FALSE);
91543896e91SMike Smith     /* if no _STA method, must be present */
91643896e91SMike Smith     if (!(devinfo.Valid & ACPI_VALID_STA))
91743896e91SMike Smith 	return(TRUE);
91843896e91SMike Smith     /* return true for 'present' and 'functioning' */
91943896e91SMike Smith     if ((devinfo.CurrentStatus & 0x9) == 0x9)
920c5ba0be4SMike Smith 	return(TRUE);
921c5ba0be4SMike Smith     return(FALSE);
922c5ba0be4SMike Smith }
923c5ba0be4SMike Smith 
924c5ba0be4SMike Smith /*
92515e32d5dSMike Smith  * Match a HID string against a device
92615e32d5dSMike Smith  */
92715e32d5dSMike Smith BOOLEAN
92815e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid)
92915e32d5dSMike Smith {
93015e32d5dSMike Smith     ACPI_HANDLE		h;
93115e32d5dSMike Smith     ACPI_DEVICE_INFO	devinfo;
93215e32d5dSMike Smith     ACPI_STATUS		error;
933ed136da6SDoug Rabson     int			cid;
93415e32d5dSMike Smith 
935cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
936cb9b0d80SMike Smith 
9374d332989SMike Smith     if (hid == NULL)
93815e32d5dSMike Smith 	return(FALSE);
93915e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
94015e32d5dSMike Smith 	return(FALSE);
94115e32d5dSMike Smith     if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK)
94215e32d5dSMike Smith 	return(FALSE);
9434d332989SMike Smith     if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId))
94415e32d5dSMike Smith 	return(TRUE);
945ed136da6SDoug Rabson     if ((error = acpi_EvaluateInteger(h, "_CID", &cid)) != AE_OK)
946ed136da6SDoug Rabson 	return(FALSE);
947ed136da6SDoug Rabson     if (cid == PNP_EISAID(hid))
948ed136da6SDoug Rabson 	return(TRUE);
94915e32d5dSMike Smith     return(FALSE);
95015e32d5dSMike Smith }
95115e32d5dSMike Smith 
95215e32d5dSMike Smith /*
953c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
954c5ba0be4SMike Smith  * or one if its parents.
955c5ba0be4SMike Smith  */
956c5ba0be4SMike Smith ACPI_STATUS
957c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
958c5ba0be4SMike Smith {
959c5ba0be4SMike Smith     ACPI_HANDLE		r;
960c5ba0be4SMike Smith     ACPI_STATUS		status;
961c5ba0be4SMike Smith 
962cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
963cb9b0d80SMike Smith 
964c5ba0be4SMike Smith     /* walk back up the tree to the root */
965c5ba0be4SMike Smith     for (;;) {
966c5ba0be4SMike Smith 	status = AcpiGetHandle(parent, path, &r);
967c5ba0be4SMike Smith 	if (status == AE_OK) {
968c5ba0be4SMike Smith 	    *result = r;
969c5ba0be4SMike Smith 	    return(AE_OK);
970c5ba0be4SMike Smith 	}
971c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
972c5ba0be4SMike Smith 	    return(AE_OK);
973c5ba0be4SMike Smith 	if (AcpiGetParent(parent, &r) != AE_OK)
974c5ba0be4SMike Smith 	    return(AE_NOT_FOUND);
975c5ba0be4SMike Smith 	parent = r;
976c5ba0be4SMike Smith     }
977c5ba0be4SMike Smith }
978c5ba0be4SMike Smith 
979c5ba0be4SMike Smith /*
980c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
981c5ba0be4SMike Smith  */
982c5ba0be4SMike Smith ACPI_BUFFER *
983c5ba0be4SMike Smith acpi_AllocBuffer(int size)
984c5ba0be4SMike Smith {
985c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
986c5ba0be4SMike Smith 
987c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
988c5ba0be4SMike Smith 	return(NULL);
989c5ba0be4SMike Smith     buf->Length = size;
990c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
991c5ba0be4SMike Smith     return(buf);
992c5ba0be4SMike Smith }
993c5ba0be4SMike Smith 
994c5ba0be4SMike Smith /*
99515e32d5dSMike Smith  * Perform the tedious double-get procedure required for fetching something into
99615e32d5dSMike Smith  * an ACPI_BUFFER that has not been initialised.
99715e32d5dSMike Smith  */
99815e32d5dSMike Smith ACPI_STATUS
99915e32d5dSMike Smith acpi_GetIntoBuffer(ACPI_HANDLE handle, ACPI_STATUS (*func)(ACPI_HANDLE, ACPI_BUFFER *), ACPI_BUFFER *buf)
100015e32d5dSMike Smith {
100115e32d5dSMike Smith     ACPI_STATUS	status;
100215e32d5dSMike Smith 
1003cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1004cb9b0d80SMike Smith 
100515e32d5dSMike Smith     buf->Length = 0;
100615e32d5dSMike Smith     buf->Pointer = NULL;
100715e32d5dSMike Smith 
100815e32d5dSMike Smith     if ((status = func(handle, buf)) != AE_BUFFER_OVERFLOW)
100915e32d5dSMike Smith 	return(status);
101015e32d5dSMike Smith     if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL)
101115e32d5dSMike Smith 	return(AE_NO_MEMORY);
101215e32d5dSMike Smith     return(func(handle, buf));
101315e32d5dSMike Smith }
101415e32d5dSMike Smith 
101515e32d5dSMike Smith /*
10167d3bcec9SMike Smith  * Perform the tedious double-get procedure required for fetching a table into
10177d3bcec9SMike Smith  * an ACPI_BUFFER that has not been initialised.
10187d3bcec9SMike Smith  */
10197d3bcec9SMike Smith ACPI_STATUS
10207d3bcec9SMike Smith acpi_GetTableIntoBuffer(ACPI_TABLE_TYPE table, UINT32 instance, ACPI_BUFFER *buf)
10217d3bcec9SMike Smith {
10227d3bcec9SMike Smith     ACPI_STATUS	status;
10237d3bcec9SMike Smith 
10247d3bcec9SMike Smith     ACPI_ASSERTLOCK;
10257d3bcec9SMike Smith 
10267d3bcec9SMike Smith     buf->Length = 0;
10277d3bcec9SMike Smith     buf->Pointer = NULL;
10287d3bcec9SMike Smith 
10297d3bcec9SMike Smith     if ((status = AcpiGetTable(table, instance, buf)) != AE_BUFFER_OVERFLOW)
10307d3bcec9SMike Smith 	return(status);
10317d3bcec9SMike Smith     if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL)
10327d3bcec9SMike Smith 	return(AE_NO_MEMORY);
10337d3bcec9SMike Smith     return(AcpiGetTable(table, instance, buf));
10347d3bcec9SMike Smith }
10357d3bcec9SMike Smith 
10367d3bcec9SMike Smith /*
1037c5ba0be4SMike Smith  * Perform the tedious double-evaluate procedure for evaluating something into
1038c5ba0be4SMike Smith  * an ACPI_BUFFER that has not been initialised.  Note that this evaluates
1039c5ba0be4SMike Smith  * twice, so avoid applying this to things that may have side-effects.
1040c5ba0be4SMike Smith  *
1041c5ba0be4SMike Smith  * This is like AcpiEvaluateObject with automatic buffer allocation.
104215e32d5dSMike Smith  */
1043c5ba0be4SMike Smith ACPI_STATUS
1044c5ba0be4SMike Smith acpi_EvaluateIntoBuffer(ACPI_HANDLE object, ACPI_STRING pathname, ACPI_OBJECT_LIST *params,
1045c5ba0be4SMike Smith 			ACPI_BUFFER *buf)
104615e32d5dSMike Smith {
1047c5ba0be4SMike Smith     ACPI_STATUS	status;
104815e32d5dSMike Smith 
1049cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1050cb9b0d80SMike Smith 
1051c5ba0be4SMike Smith     buf->Length = 0;
1052c5ba0be4SMike Smith     buf->Pointer = NULL;
1053c5ba0be4SMike Smith 
1054c5ba0be4SMike Smith     if ((status = AcpiEvaluateObject(object, pathname, params, buf)) != AE_BUFFER_OVERFLOW)
1055c5ba0be4SMike Smith 	return(status);
1056c5ba0be4SMike Smith     if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL)
1057c5ba0be4SMike Smith 	return(AE_NO_MEMORY);
1058c5ba0be4SMike Smith     return(AcpiEvaluateObject(object, pathname, params, buf));
105915e32d5dSMike Smith }
106015e32d5dSMike Smith 
1061c5ba0be4SMike Smith /*
1062c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1063c5ba0be4SMike Smith  */
1064c5ba0be4SMike Smith ACPI_STATUS
1065c5ba0be4SMike Smith acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number)
1066c5ba0be4SMike Smith {
1067c5ba0be4SMike Smith     ACPI_STATUS	error;
1068c5ba0be4SMike Smith     ACPI_BUFFER	buf;
10690c7da7acSJohn Baldwin     ACPI_OBJECT	param, *p;
10700c7da7acSJohn Baldwin     int		i;
1071c5ba0be4SMike Smith 
1072cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1073cb9b0d80SMike Smith 
1074c5ba0be4SMike Smith     if (handle == NULL)
1075c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
10760c7da7acSJohn Baldwin 
10770c7da7acSJohn Baldwin     /*
10780c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
10790c7da7acSJohn Baldwin      * a method that will return an Integer.
10800c7da7acSJohn Baldwin      */
1081c5ba0be4SMike Smith     buf.Pointer = &param;
1082c5ba0be4SMike Smith     buf.Length = sizeof(param);
1083c5ba0be4SMike Smith     if ((error = AcpiEvaluateObject(handle, path, NULL, &buf)) == AE_OK) {
1084c5ba0be4SMike Smith 	if (param.Type == ACPI_TYPE_INTEGER) {
1085c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1086c5ba0be4SMike Smith 	} else {
1087c5ba0be4SMike Smith 	    error = AE_TYPE;
1088c5ba0be4SMike Smith 	}
1089c5ba0be4SMike Smith     }
10900c7da7acSJohn Baldwin 
10910c7da7acSJohn Baldwin     /*
10920c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
10930c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
10940c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
10950c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
10960c7da7acSJohn Baldwin      *
10970c7da7acSJohn Baldwin      * This is a hack.
10980c7da7acSJohn Baldwin      */
10990c7da7acSJohn Baldwin     if (error == AE_BUFFER_OVERFLOW) {
11000c7da7acSJohn Baldwin 	if ((buf.Pointer = AcpiOsCallocate(buf.Length)) == NULL) {
11010c7da7acSJohn Baldwin 	    error = AE_NO_MEMORY;
11020c7da7acSJohn Baldwin 	} else {
11030c7da7acSJohn Baldwin 	    if ((error = AcpiEvaluateObject(handle, path, NULL, &buf)) == AE_OK) {
11040c7da7acSJohn Baldwin 		p = (ACPI_OBJECT *)buf.Pointer;
11050c7da7acSJohn Baldwin 		if (p->Type != ACPI_TYPE_BUFFER) {
11060c7da7acSJohn Baldwin 		    error = AE_TYPE;
11070c7da7acSJohn Baldwin 		} else {
11080c7da7acSJohn Baldwin 		    if (p->Buffer.Length > sizeof(int)) {
11090c7da7acSJohn Baldwin 			error = AE_BAD_DATA;
11100c7da7acSJohn Baldwin 		    } else {
11110c7da7acSJohn Baldwin 			*number = 0;
11120c7da7acSJohn Baldwin 			for (i = 0; i < p->Buffer.Length; i++)
11130c7da7acSJohn Baldwin 			    *number += (*(p->Buffer.Pointer + i) << (i * 8));
11140c7da7acSJohn Baldwin 		    }
11150c7da7acSJohn Baldwin 		}
11160c7da7acSJohn Baldwin 	    }
11170c7da7acSJohn Baldwin 	}
11180c7da7acSJohn Baldwin 	AcpiOsFree(buf.Pointer);
11190c7da7acSJohn Baldwin     }
1120c5ba0be4SMike Smith     return(error);
1121c5ba0be4SMike Smith }
1122c5ba0be4SMike Smith 
1123c5ba0be4SMike Smith /*
1124c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1125c5ba0be4SMike Smith  * function for each element.
1126c5ba0be4SMike Smith  *
1127c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1128c5ba0be4SMike Smith  */
1129c5ba0be4SMike Smith ACPI_STATUS
1130c5ba0be4SMike Smith acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg)
1131c5ba0be4SMike Smith {
1132c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1133c5ba0be4SMike Smith     int		i;
1134c5ba0be4SMike Smith 
1135c5ba0be4SMike Smith     if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE))
1136c5ba0be4SMike Smith 	return(AE_BAD_PARAMETER);
1137c5ba0be4SMike Smith 
1138c5ba0be4SMike Smith     /* iterate over components */
1139c5ba0be4SMike Smith     for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++)
1140c5ba0be4SMike Smith 	func(comp, arg);
1141c5ba0be4SMike Smith 
1142c5ba0be4SMike Smith     return(AE_OK);
1143c5ba0be4SMike Smith }
1144c5ba0be4SMike Smith 
11456f69255bSMike Smith /*
11466f69255bSMike Smith  * Find the (index)th resource object in a set.
11476f69255bSMike Smith  */
11486f69255bSMike Smith ACPI_STATUS
11496d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
11506f69255bSMike Smith {
11516d63101aSMike Smith     ACPI_RESOURCE	*rp;
11526f69255bSMike Smith     int			i;
11536f69255bSMike Smith 
11546d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
11556f69255bSMike Smith     i = index;
11566d63101aSMike Smith     while (i-- > 0) {
11576f69255bSMike Smith 	/* range check */
11586d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
11596f69255bSMike Smith 	    return(AE_BAD_PARAMETER);
11606d63101aSMike Smith 	/* check for terminator */
11616d63101aSMike Smith 	if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
11626d63101aSMike Smith 	    (rp->Length == 0))
11636d63101aSMike Smith 	    return(AE_NOT_FOUND);
11646d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
11656f69255bSMike Smith     }
11666f69255bSMike Smith     if (resp != NULL)
11676d63101aSMike Smith 	*resp = rp;
11686d63101aSMike Smith     return(AE_OK);
11696d63101aSMike Smith }
11706d63101aSMike Smith 
11716d63101aSMike Smith /*
11726d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
11736d63101aSMike Smith  *
11746d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
11756d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
11766d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
11776d63101aSMike Smith  * resources.
11786d63101aSMike Smith  */
11796d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
11806d63101aSMike Smith 
11816d63101aSMike Smith ACPI_STATUS
11826d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
11836d63101aSMike Smith {
11846d63101aSMike Smith     ACPI_RESOURCE	*rp;
11856d63101aSMike Smith     void		*newp;
11866d63101aSMike Smith 
11876d63101aSMike Smith     /*
11886d63101aSMike Smith      * Initialise the buffer if necessary.
11896d63101aSMike Smith      */
11906d63101aSMike Smith     if (buf->Pointer == NULL) {
11916d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
11926d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
11936d63101aSMike Smith 	    return(AE_NO_MEMORY);
11946d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
11956d63101aSMike Smith 	rp->Id = ACPI_RSTYPE_END_TAG;
11966d63101aSMike Smith 	rp->Length = 0;
11976d63101aSMike Smith     }
11986d63101aSMike Smith     if (res == NULL)
11996d63101aSMike Smith 	return(AE_OK);
12006d63101aSMike Smith 
12016d63101aSMike Smith     /*
12026d63101aSMike Smith      * Scan the current buffer looking for the terminator.
12036d63101aSMike Smith      * This will either find the terminator or hit the end
12046d63101aSMike Smith      * of the buffer and return an error.
12056d63101aSMike Smith      */
12066d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
12076d63101aSMike Smith     for (;;) {
12086d63101aSMike Smith 	/* range check, don't go outside the buffer */
12096d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
12106d63101aSMike Smith 	    return(AE_BAD_PARAMETER);
12116d63101aSMike Smith 	if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
12126d63101aSMike Smith 	    (rp->Length == 0)) {
12136d63101aSMike Smith 	    break;
12146d63101aSMike Smith 	}
12156d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
12166d63101aSMike Smith     }
12176d63101aSMike Smith 
12186d63101aSMike Smith     /*
12196d63101aSMike Smith      * Check the size of the buffer and expand if required.
12206d63101aSMike Smith      *
12216d63101aSMike Smith      * Required size is:
12226d63101aSMike Smith      *	size of existing resources before terminator +
12236d63101aSMike Smith      *	size of new resource and header +
12246d63101aSMike Smith      * 	size of terminator.
12256d63101aSMike Smith      *
12266d63101aSMike Smith      * Note that this loop should really only run once, unless
12276d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
12286d63101aSMike Smith      */
12296d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
12306d63101aSMike Smith 	    res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
12316d63101aSMike Smith 	    ACPI_RESOURCE_LENGTH) >= buf->Length) {
12326d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
12336d63101aSMike Smith 	    return(AE_NO_MEMORY);
12346d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
1235a692219dSMike Smith         rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1236a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
12376d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
12386d63101aSMike Smith 	buf->Pointer = newp;
12396d63101aSMike Smith 	buf->Length += buf->Length;
12406d63101aSMike Smith     }
12416d63101aSMike Smith 
12426d63101aSMike Smith     /*
12436d63101aSMike Smith      * Insert the new resource.
12446d63101aSMike Smith      */
12456d63101aSMike Smith     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
12466d63101aSMike Smith 
12476d63101aSMike Smith     /*
12486d63101aSMike Smith      * And add the terminator.
12496d63101aSMike Smith      */
12506d63101aSMike Smith     rp = ACPI_RESOURCE_NEXT(rp);
12516d63101aSMike Smith     rp->Id = ACPI_RSTYPE_END_TAG;
12526d63101aSMike Smith     rp->Length = 0;
12536d63101aSMike Smith 
12546f69255bSMike Smith     return(AE_OK);
12556f69255bSMike Smith }
1256c5ba0be4SMike Smith 
1257c30382dfSMitsuru IWASAKI 
125815e32d5dSMike Smith /*
125915e32d5dSMike Smith  * Set the system sleep state
126015e32d5dSMike Smith  *
126115e32d5dSMike Smith  * Currently we only support S1 and S5
126215e32d5dSMike Smith  */
126315e32d5dSMike Smith ACPI_STATUS
126415e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
126515e32d5dSMike Smith {
126615e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
12676161544cSTakanori Watanabe     UINT16	Count;
126856d8cb57SMitsuru IWASAKI     UINT8	TypeA;
126956d8cb57SMitsuru IWASAKI     UINT8	TypeB;
127015e32d5dSMike Smith 
12712a4ac806SMike Smith     FUNCTION_TRACE_U32(__func__, state);
1272cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
12730ae55423SMike Smith 
127415e32d5dSMike Smith     switch (state) {
127515e32d5dSMike Smith     case ACPI_STATE_S0:	/* XXX only for testing */
127691467fc6SMike Smith 	status = AcpiEnterSleepState((UINT8)state);
127715e32d5dSMike Smith 	if (status != AE_OK) {
1278bfae45aaSMike Smith 	    device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
127915e32d5dSMike Smith 	}
128015e32d5dSMike Smith 	break;
128115e32d5dSMike Smith 
128215e32d5dSMike Smith     case ACPI_STATE_S1:
12836161544cSTakanori Watanabe     case ACPI_STATE_S2:
12846161544cSTakanori Watanabe     case ACPI_STATE_S3:
12856161544cSTakanori Watanabe     case ACPI_STATE_S4:
128656d8cb57SMitsuru IWASAKI 	status = AcpiHwObtainSleepTypeRegisterData((UINT8)state, &TypeA, &TypeB);
128756d8cb57SMitsuru IWASAKI 	if (status != AE_OK) {
128856d8cb57SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiHwObtainSleepTypeRegisterData failed - %s\n", AcpiFormatException(status));
128956d8cb57SMitsuru IWASAKI 	    break;
129056d8cb57SMitsuru IWASAKI 	}
129156d8cb57SMitsuru IWASAKI 
129215e32d5dSMike Smith 	/*
129315e32d5dSMike Smith 	 * Inform all devices that we are going to sleep.
129415e32d5dSMike Smith 	 */
129515e32d5dSMike Smith 	if (DEVICE_SUSPEND(root_bus) != 0) {
129615e32d5dSMike Smith 	    /*
129715e32d5dSMike Smith 	     * Re-wake the system.
129815e32d5dSMike Smith 	     *
129915e32d5dSMike Smith 	     * XXX note that a better two-pass approach with a 'veto' pass
130015e32d5dSMike Smith 	     *     followed by a "real thing" pass would be better, but the
130115e32d5dSMike Smith 	     *     current bus interface does not provide for this.
130215e32d5dSMike Smith 	     */
130315e32d5dSMike Smith 	    DEVICE_RESUME(root_bus);
13040ae55423SMike Smith 	    return_ACPI_STATUS(AE_ERROR);
130515e32d5dSMike Smith 	}
130615e32d5dSMike Smith 	sc->acpi_sstate = state;
13076161544cSTakanori Watanabe 
13086161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
13096161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
13106161544cSTakanori Watanabe 
13116161544cSTakanori Watanabe 	    /* AcpiEnterSleepState() maybe incompleted, unlock here. */
13126161544cSTakanori Watanabe 	    AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
13136161544cSTakanori Watanabe 
13146161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
13156161544cSTakanori Watanabe 	    if (state >= ACPI_STATE_S4) {
13166161544cSTakanori Watanabe 		acpi_Disable(sc);
13176161544cSTakanori Watanabe 		acpi_Enable(sc);
13186161544cSTakanori Watanabe 	    }
13196161544cSTakanori Watanabe 	} else {
132091467fc6SMike Smith 	    status = AcpiEnterSleepState((UINT8)state);
132115e32d5dSMike Smith 	    if (status != AE_OK) {
1322bfae45aaSMike Smith 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
1323c30382dfSMitsuru IWASAKI 		break;
132415e32d5dSMike Smith 	    }
13256161544cSTakanori Watanabe 	    /* wait for the WAK_STS bit */
13266161544cSTakanori Watanabe 	    Count = 0;
13276161544cSTakanori Watanabe 	    while (!(AcpiHwRegisterBitAccess(ACPI_READ, ACPI_MTX_LOCK, WAK_STS))) {
1328acf72ef4SMike Smith 		AcpiOsSleep(0, 1);
13296161544cSTakanori Watanabe 		/*
13306161544cSTakanori Watanabe 		 * Some BIOSes don't set WAK_STS at all,
13316161544cSTakanori Watanabe 		 * give up waiting for wakeup if we time out.
13326161544cSTakanori Watanabe 		 */
13336161544cSTakanori Watanabe 		if (Count > 1000) {
13346161544cSTakanori Watanabe 		    break;	/* giving up */
13356161544cSTakanori Watanabe 		}
13366161544cSTakanori Watanabe 		Count++;
13376161544cSTakanori Watanabe 	    }
13386161544cSTakanori Watanabe 	}
1339ff741befSTakanori Watanabe 	AcpiLeaveSleepState((UINT8)state);
134015e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
134115e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
134213d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
134315e32d5dSMike Smith 	break;
134415e32d5dSMike Smith 
134515e32d5dSMike Smith     case ACPI_STATE_S5:
134615e32d5dSMike Smith 	/*
134715e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
134815e32d5dSMike Smith 	 * shutdown handlers.
134915e32d5dSMike Smith 	 */
135015e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
135115e32d5dSMike Smith 	break;
135215e32d5dSMike Smith 
135315e32d5dSMike Smith     default:
135415e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
135515e32d5dSMike Smith 	break;
135615e32d5dSMike Smith     }
13570ae55423SMike Smith     return_ACPI_STATUS(status);
135815e32d5dSMike Smith }
135915e32d5dSMike Smith 
136015e32d5dSMike Smith /*
136115e32d5dSMike Smith  * Enable/Disable ACPI
136215e32d5dSMike Smith  */
136315e32d5dSMike Smith ACPI_STATUS
136415e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
136515e32d5dSMike Smith {
136615e32d5dSMike Smith     ACPI_STATUS	status;
136715e32d5dSMike Smith     u_int32_t	flags;
136815e32d5dSMike Smith 
13692a4ac806SMike Smith     FUNCTION_TRACE(__func__);
1370cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
13710ae55423SMike Smith 
137215e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
137315e32d5dSMike Smith             ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
137415e32d5dSMike Smith     if (!sc->acpi_enabled) {
137515e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
137615e32d5dSMike Smith     } else {
137715e32d5dSMike Smith 	status = AE_OK;
137815e32d5dSMike Smith     }
137915e32d5dSMike Smith     if (status == AE_OK)
138015e32d5dSMike Smith 	sc->acpi_enabled = 1;
13810ae55423SMike Smith     return_ACPI_STATUS(status);
138215e32d5dSMike Smith }
138315e32d5dSMike Smith 
138415e32d5dSMike Smith ACPI_STATUS
138515e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
138615e32d5dSMike Smith {
138715e32d5dSMike Smith     ACPI_STATUS	status;
138815e32d5dSMike Smith 
13892a4ac806SMike Smith     FUNCTION_TRACE(__func__);
1390cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
13910ae55423SMike Smith 
139215e32d5dSMike Smith     if (sc->acpi_enabled) {
139315e32d5dSMike Smith 	status = AcpiDisable();
139415e32d5dSMike Smith     } else {
139515e32d5dSMike Smith 	status = AE_OK;
139615e32d5dSMike Smith     }
139715e32d5dSMike Smith     if (status == AE_OK)
139815e32d5dSMike Smith 	sc->acpi_enabled = 0;
13990ae55423SMike Smith     return_ACPI_STATUS(status);
140015e32d5dSMike Smith }
140115e32d5dSMike Smith 
140215e32d5dSMike Smith /*
140315e32d5dSMike Smith  * ACPI Event Handlers
140415e32d5dSMike Smith  */
140515e32d5dSMike Smith 
140615e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
140715e32d5dSMike Smith 
140815e32d5dSMike Smith static void
140915e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
141015e32d5dSMike Smith {
14112a4ac806SMike Smith     FUNCTION_TRACE_U32(__func__, state);
141215e32d5dSMike Smith 
1413cb9b0d80SMike Smith     ACPI_LOCK;
1414a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
141515e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
1416cb9b0d80SMike Smith     ACPI_UNLOCK;
14170ae55423SMike Smith     return_VOID;
141815e32d5dSMike Smith }
141915e32d5dSMike Smith 
142015e32d5dSMike Smith static void
142115e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
142215e32d5dSMike Smith {
14232a4ac806SMike Smith     FUNCTION_TRACE_U32(__func__, state);
14240ae55423SMike Smith 
142515e32d5dSMike Smith     /* Well, what to do? :-) */
14260ae55423SMike Smith 
1427cb9b0d80SMike Smith     ACPI_LOCK;
1428cb9b0d80SMike Smith     ACPI_UNLOCK;
1429cb9b0d80SMike Smith 
14300ae55423SMike Smith     return_VOID;
143115e32d5dSMike Smith }
143215e32d5dSMike Smith 
143315e32d5dSMike Smith /*
143415e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
143515e32d5dSMike Smith  */
143615e32d5dSMike Smith UINT32
143715e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context)
143815e32d5dSMike Smith {
143915e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
144015e32d5dSMike Smith 
14412a4ac806SMike Smith     FUNCTION_TRACE(__func__);
14420ae55423SMike Smith 
144315e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
14440ae55423SMike Smith 
14450ae55423SMike Smith     return_VALUE(INTERRUPT_HANDLED);
144615e32d5dSMike Smith }
144715e32d5dSMike Smith 
144815e32d5dSMike Smith UINT32
144915e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context)
145015e32d5dSMike Smith {
145115e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
145215e32d5dSMike Smith 
14532a4ac806SMike Smith     FUNCTION_TRACE(__func__);
14540ae55423SMike Smith 
145515e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
14560ae55423SMike Smith 
14570ae55423SMike Smith     return_VALUE(INTERRUPT_HANDLED);
145815e32d5dSMike Smith }
145915e32d5dSMike Smith 
146015e32d5dSMike Smith UINT32
146115e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context)
146215e32d5dSMike Smith {
146315e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
146415e32d5dSMike Smith 
14652a4ac806SMike Smith     FUNCTION_TRACE(__func__);
14660ae55423SMike Smith 
146715e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
14680ae55423SMike Smith 
14690ae55423SMike Smith     return_VALUE(INTERRUPT_HANDLED);
147015e32d5dSMike Smith }
147115e32d5dSMike Smith 
147215e32d5dSMike Smith UINT32
147315e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context)
147415e32d5dSMike Smith {
147515e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
147615e32d5dSMike Smith 
14772a4ac806SMike Smith     FUNCTION_TRACE(__func__);
14780ae55423SMike Smith 
147915e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
14800ae55423SMike Smith 
14810ae55423SMike Smith     return_VALUE(INTERRUPT_HANDLED);
148215e32d5dSMike Smith }
148315e32d5dSMike Smith 
148415e32d5dSMike Smith /*
148515e32d5dSMike Smith  * XXX This is kinda ugly, and should not be here.
148615e32d5dSMike Smith  */
148715e32d5dSMike Smith struct acpi_staticbuf {
148815e32d5dSMike Smith     ACPI_BUFFER	buffer;
148915e32d5dSMike Smith     char	data[512];
149015e32d5dSMike Smith };
149115e32d5dSMike Smith 
149215e32d5dSMike Smith char *
149315e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
149415e32d5dSMike Smith {
149515e32d5dSMike Smith     static struct acpi_staticbuf	buf;
149615e32d5dSMike Smith 
1497cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1498cb9b0d80SMike Smith 
149915e32d5dSMike Smith     buf.buffer.Length = 512;
150015e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
150115e32d5dSMike Smith 
150215e32d5dSMike Smith     if (AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer) == AE_OK)
150315e32d5dSMike Smith 	return(buf.buffer.Pointer);
150415e32d5dSMike Smith     return("(unknown path)");
150515e32d5dSMike Smith }
150615e32d5dSMike Smith 
150715e32d5dSMike Smith /*
150815e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
150915e32d5dSMike Smith  * parts of the namespace.
151015e32d5dSMike Smith  */
151115e32d5dSMike Smith int
151215e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
151315e32d5dSMike Smith {
151415e32d5dSMike Smith     char	*cp, *np;
151515e32d5dSMike Smith     int		len;
151615e32d5dSMike Smith 
151715e32d5dSMike Smith     np = acpi_name(handle);
151815e32d5dSMike Smith     if (*np == '\\')
151915e32d5dSMike Smith 	np++;
152015e32d5dSMike Smith     if ((cp = getenv("debug.acpi.avoid")) == NULL)
152115e32d5dSMike Smith 	return(0);
152215e32d5dSMike Smith 
152315e32d5dSMike Smith     /* scan the avoid list checking for a match */
152415e32d5dSMike Smith     for (;;) {
152515e32d5dSMike Smith 	while ((*cp != 0) && isspace(*cp))
152615e32d5dSMike Smith 	    cp++;
152715e32d5dSMike Smith 	if (*cp == 0)
152815e32d5dSMike Smith 	    break;
152915e32d5dSMike Smith 	len = 0;
153015e32d5dSMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
153115e32d5dSMike Smith 	    len++;
15324c1cdee6SMike Smith 	if (!strncmp(cp, np, len))
15330ae55423SMike Smith 	    return(1);
15340ae55423SMike Smith 	cp += len;
15350ae55423SMike Smith     }
15360ae55423SMike Smith     return(0);
15370ae55423SMike Smith }
15380ae55423SMike Smith 
15390ae55423SMike Smith /*
15400ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
15410ae55423SMike Smith  */
15420ae55423SMike Smith int
15430ae55423SMike Smith acpi_disabled(char *subsys)
15440ae55423SMike Smith {
15450ae55423SMike Smith     char	*cp;
15460ae55423SMike Smith     int		len;
15470ae55423SMike Smith 
15480ae55423SMike Smith     if ((cp = getenv("debug.acpi.disable")) == NULL)
15490ae55423SMike Smith 	return(0);
15500ae55423SMike Smith     if (!strcmp(cp, "all"))
15510ae55423SMike Smith 	return(1);
15520ae55423SMike Smith 
15530ae55423SMike Smith     /* scan the disable list checking for a match */
15540ae55423SMike Smith     for (;;) {
15550ae55423SMike Smith 	while ((*cp != 0) && isspace(*cp))
15560ae55423SMike Smith 	    cp++;
15570ae55423SMike Smith 	if (*cp == 0)
15580ae55423SMike Smith 	    break;
15590ae55423SMike Smith 	len = 0;
15600ae55423SMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
15610ae55423SMike Smith 	    len++;
15624c1cdee6SMike Smith 	if (!strncmp(cp, subsys, len))
156315e32d5dSMike Smith 	    return(1);
156415e32d5dSMike Smith 	cp += len;
156515e32d5dSMike Smith     }
156615e32d5dSMike Smith     return(0);
156715e32d5dSMike Smith }
156815e32d5dSMike Smith 
156915e32d5dSMike Smith /*
157015e32d5dSMike Smith  * Control interface.
157115e32d5dSMike Smith  *
15720ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
15730ae55423SMike Smith  * drivers wanting to be accessible via /dev/acpi should use the register/deregister
15740ae55423SMike Smith  * interface to make their handlers visible.
157515e32d5dSMike Smith  */
15760ae55423SMike Smith struct acpi_ioctl_hook
15770ae55423SMike Smith {
15780ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook)	link;
15790ae55423SMike Smith     u_long				cmd;
15800ae55423SMike Smith     int					(* fn)(u_long cmd, caddr_t addr, void *arg);
15810ae55423SMike Smith     void				*arg;
15820ae55423SMike Smith };
15830ae55423SMike Smith 
15840ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
15850ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
15860ae55423SMike Smith 
15870ae55423SMike Smith /*
15880ae55423SMike Smith  * Register an ioctl handler.
15890ae55423SMike Smith  */
15900ae55423SMike Smith int
15910ae55423SMike Smith acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg)
15920ae55423SMike Smith {
15930ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
15940ae55423SMike Smith 
15950ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
15960ae55423SMike Smith 	return(ENOMEM);
15970ae55423SMike Smith     hp->cmd = cmd;
15980ae55423SMike Smith     hp->fn = fn;
15990ae55423SMike Smith     hp->arg = arg;
16000ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
16010ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
16020ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
16030ae55423SMike Smith     }
16040ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
16050ae55423SMike Smith     return(0);
16060ae55423SMike Smith }
16070ae55423SMike Smith 
16080ae55423SMike Smith /*
16090ae55423SMike Smith  * Deregister an ioctl handler.
16100ae55423SMike Smith  */
16110ae55423SMike Smith void
16120ae55423SMike Smith acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg))
16130ae55423SMike Smith {
16140ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
16150ae55423SMike Smith 
16160ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
16170ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
16180ae55423SMike Smith 	    break;
16190ae55423SMike Smith 
16200ae55423SMike Smith     if (hp != NULL) {
16210ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
16220ae55423SMike Smith 	free(hp, M_ACPIDEV);
16230ae55423SMike Smith     }
16240ae55423SMike Smith }
16250ae55423SMike Smith 
162615e32d5dSMike Smith static int
1627b40ce416SJulian Elischer acpiopen(dev_t dev, int flag, int fmt, struct thread *td)
162815e32d5dSMike Smith {
162915e32d5dSMike Smith     return(0);
163015e32d5dSMike Smith }
163115e32d5dSMike Smith 
163215e32d5dSMike Smith static int
1633b40ce416SJulian Elischer acpiclose(dev_t dev, int flag, int fmt, struct thread *td)
163415e32d5dSMike Smith {
163515e32d5dSMike Smith     return(0);
163615e32d5dSMike Smith }
163715e32d5dSMike Smith 
163815e32d5dSMike Smith static int
1639b40ce416SJulian Elischer acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
164015e32d5dSMike Smith {
164115e32d5dSMike Smith     struct acpi_softc		*sc;
16420ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
16430ae55423SMike Smith     int				error, xerror, state;
164415e32d5dSMike Smith 
1645cb9b0d80SMike Smith     ACPI_LOCK;
1646cb9b0d80SMike Smith 
164715e32d5dSMike Smith     error = state = 0;
164815e32d5dSMike Smith     sc = dev->si_drv1;
164915e32d5dSMike Smith 
16500ae55423SMike Smith     /*
16510ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
16520ae55423SMike Smith      */
16530ae55423SMike Smith     if (acpi_ioctl_hooks_initted) {
16540ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
16550ae55423SMike Smith 	    if (hp->cmd == cmd) {
16560ae55423SMike Smith 		xerror = hp->fn(cmd, addr, hp->arg);
16570ae55423SMike Smith 		if (xerror != 0)
16580ae55423SMike Smith 		    error = xerror;
1659917d44c8SMitsuru IWASAKI 		goto out;
16600ae55423SMike Smith 	    }
16610ae55423SMike Smith 	}
16620ae55423SMike Smith     }
16630ae55423SMike Smith 
16640ae55423SMike Smith     /*
16650ae55423SMike Smith      * Core system ioctls.
16660ae55423SMike Smith      */
166715e32d5dSMike Smith     switch (cmd) {
166815e32d5dSMike Smith     case ACPIIO_ENABLE:
16690ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
167015e32d5dSMike Smith 	    error = ENXIO;
167115e32d5dSMike Smith 	break;
167215e32d5dSMike Smith 
167315e32d5dSMike Smith     case ACPIIO_DISABLE:
16740ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
167515e32d5dSMike Smith 	    error = ENXIO;
167615e32d5dSMike Smith 	break;
167715e32d5dSMike Smith 
167815e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
167915e32d5dSMike Smith 	if (!sc->acpi_enabled) {
168015e32d5dSMike Smith 	    error = ENXIO;
168115e32d5dSMike Smith 	    break;
168215e32d5dSMike Smith 	}
168315e32d5dSMike Smith 	state = *(int *)addr;
1684a5d1879bSMitsuru IWASAKI 	if (state >= ACPI_STATE_S0  && state <= ACPI_S_STATES_MAX) {
168515e32d5dSMike Smith 	    acpi_SetSleepState(sc, state);
168615e32d5dSMike Smith 	} else {
168715e32d5dSMike Smith 	    error = EINVAL;
168815e32d5dSMike Smith 	}
168915e32d5dSMike Smith 	break;
169015e32d5dSMike Smith 
169115e32d5dSMike Smith     default:
16920ae55423SMike Smith 	if (error == 0)
169315e32d5dSMike Smith 	    error = EINVAL;
169415e32d5dSMike Smith 	break;
169515e32d5dSMike Smith     }
1696917d44c8SMitsuru IWASAKI 
1697917d44c8SMitsuru IWASAKI out:
1698cb9b0d80SMike Smith     ACPI_UNLOCK;
169915e32d5dSMike Smith     return(error);
170015e32d5dSMike Smith }
170115e32d5dSMike Smith 
17021d073b1dSJohn Baldwin static int
17031d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
17041d073b1dSJohn Baldwin {
17051d073b1dSJohn Baldwin     char sleep_state[10];
17061d073b1dSJohn Baldwin     int error;
17071d073b1dSJohn Baldwin     u_int new_state, old_state;
17081d073b1dSJohn Baldwin 
17091d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
1710cb9b0d80SMike Smith     if (old_state > ACPI_S_STATES_MAX) {
17111d073b1dSJohn Baldwin 	strcpy(sleep_state, "unknown");
1712cb9b0d80SMike Smith     } else {
17131d073b1dSJohn Baldwin 	strncpy(sleep_state, sleep_state_names[old_state],
17141d073b1dSJohn Baldwin 		sizeof(sleep_state_names[old_state]));
1715cb9b0d80SMike Smith     }
17161d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
17171d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
1718cb9b0d80SMike Smith 	for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX; new_state++) {
17191d073b1dSJohn Baldwin 	    if (strncmp(sleep_state, sleep_state_names[new_state],
17201d073b1dSJohn Baldwin 			sizeof(sleep_state)) == 0)
17211d073b1dSJohn Baldwin 		break;
1722cb9b0d80SMike Smith 	}
1723cb9b0d80SMike Smith 	if ((new_state != old_state) && (new_state <= ACPI_S_STATES_MAX)) {
17241d073b1dSJohn Baldwin 	    *(u_int *)oidp->oid_arg1 = new_state;
1725cb9b0d80SMike Smith 	} else {
17261d073b1dSJohn Baldwin 	    error = EINVAL;
17271d073b1dSJohn Baldwin 	}
1728cb9b0d80SMike Smith     }
17291d073b1dSJohn Baldwin     return(error);
17301d073b1dSJohn Baldwin }
17311d073b1dSJohn Baldwin 
173215e32d5dSMike Smith #ifdef ACPI_DEBUG
17330ae55423SMike Smith /*
17340ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
17350ae55423SMike Smith  *
17360ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
17370ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
17380ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
17390ae55423SMike Smith  * prefixing the bit name with !.
17400ae55423SMike Smith  */
174115e32d5dSMike Smith struct debugtag
174215e32d5dSMike Smith {
174315e32d5dSMike Smith     char	*name;
174415e32d5dSMike Smith     UINT32	value;
174515e32d5dSMike Smith };
174615e32d5dSMike Smith 
174715e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
1748c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
1749c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
1750c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
1751c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
1752c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
1753c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
1754c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
1755c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
1756c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
17574c1cdee6SMike Smith     {"ACPI_DEBUGGER",		ACPI_DEBUGGER},
17584c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
17594c1cdee6SMike Smith 
1760cb9b0d80SMike Smith     {"ACPI_BUS",		ACPI_BUS},
17614c1cdee6SMike Smith     {"ACPI_SYSTEM",		ACPI_SYSTEM},
1762cb9b0d80SMike Smith     {"ACPI_POWER",		ACPI_POWER},
1763cb9b0d80SMike Smith     {"ACPI_EC", 		ACPI_EC},
1764c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
1765c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
1766c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
17674c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
1768cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
17694c1cdee6SMike Smith     {"ACPI_FAN",		ACPI_FAN},
17704c1cdee6SMike Smith 
1771c5ba0be4SMike Smith     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
177215e32d5dSMike Smith     {NULL, 0}
177315e32d5dSMike Smith };
177415e32d5dSMike Smith 
177515e32d5dSMike Smith static struct debugtag dbg_level[] = {
17764c1cdee6SMike Smith     {"ACPI_LV_OK",		ACPI_LV_OK},
17774c1cdee6SMike Smith     {"ACPI_LV_INFO",		ACPI_LV_INFO},
17784c1cdee6SMike Smith     {"ACPI_LV_WARN",		ACPI_LV_WARN},
17794c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
17804c1cdee6SMike Smith     {"ACPI_LV_FATAL",		ACPI_LV_FATAL},
17814c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
17824c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
17834c1cdee6SMike Smith     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
17844c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
17854c1cdee6SMike Smith     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
17864c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
17874c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
17884c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
17894c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
17904c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
17914c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
17924c1cdee6SMike Smith     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
17934c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
17944c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
17954c1cdee6SMike Smith     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
17964c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
17974c1cdee6SMike Smith     {"ACPI_LV_IO",		ACPI_LV_IO},
17984c1cdee6SMike Smith     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
17994c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
18004c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
18014c1cdee6SMike Smith     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
18024c1cdee6SMike Smith     {"ACPI_LV_INIT",		ACPI_LV_INIT},
18034c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
18044c1cdee6SMike Smith     {"ACPI_DB_AML_DISASSEMBLE",	ACPI_DB_AML_DISASSEMBLE},
18054c1cdee6SMike Smith     {"ACPI_DB_VERBOSE_INFO",	ACPI_DB_VERBOSE_INFO},
18064c1cdee6SMike Smith     {"ACPI_DB_FULL_TABLES",	ACPI_DB_FULL_TABLES},
18074c1cdee6SMike Smith     {"ACPI_DB_EVENTS",		ACPI_DB_EVENTS},
18084c1cdee6SMike Smith     {"ACPI_DB_VERBOSE",		ACPI_DB_VERBOSE},
180915e32d5dSMike Smith     {NULL, 0}
181015e32d5dSMike Smith };
181115e32d5dSMike Smith 
181215e32d5dSMike Smith static void
181315e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
181415e32d5dSMike Smith {
181515e32d5dSMike Smith     char	*ep;
181615e32d5dSMike Smith     int		i, l;
18170ae55423SMike Smith     int		set;
181815e32d5dSMike Smith 
181915e32d5dSMike Smith     while (*cp) {
182015e32d5dSMike Smith 	if (isspace(*cp)) {
182115e32d5dSMike Smith 	    cp++;
182215e32d5dSMike Smith 	    continue;
182315e32d5dSMike Smith 	}
182415e32d5dSMike Smith 	ep = cp;
182515e32d5dSMike Smith 	while (*ep && !isspace(*ep))
182615e32d5dSMike Smith 	    ep++;
18270ae55423SMike Smith 	if (*cp == '!') {
18280ae55423SMike Smith 	    set = 0;
18290ae55423SMike Smith 	    cp++;
18300ae55423SMike Smith 	    if (cp == ep)
18310ae55423SMike Smith 		continue;
18320ae55423SMike Smith 	} else {
18330ae55423SMike Smith 	    set = 1;
18340ae55423SMike Smith 	}
183515e32d5dSMike Smith 	l = ep - cp;
183615e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
183715e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
18380ae55423SMike Smith 		if (set) {
183915e32d5dSMike Smith 		    *flag |= tag[i].value;
18400ae55423SMike Smith 		} else {
18410ae55423SMike Smith 		    *flag &= ~tag[i].value;
18420ae55423SMike Smith 		}
184315e32d5dSMike Smith 		printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
184415e32d5dSMike Smith 	    }
184515e32d5dSMike Smith 	}
184615e32d5dSMike Smith 	cp = ep;
184715e32d5dSMike Smith     }
184815e32d5dSMike Smith }
184915e32d5dSMike Smith 
185015e32d5dSMike Smith static void
18512a4ac806SMike Smith acpi_set_debugging(void *junk)
185215e32d5dSMike Smith {
185315e32d5dSMike Smith     char	*cp;
185415e32d5dSMike Smith 
18550ae55423SMike Smith     AcpiDbgLayer = 0;
18560ae55423SMike Smith     AcpiDbgLevel = 0;
185715e32d5dSMike Smith     if ((cp = getenv("debug.acpi.layer")) != NULL)
185815e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
185915e32d5dSMike Smith     if ((cp = getenv("debug.acpi.level")) != NULL)
186015e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
18610ae55423SMike Smith 
18620ae55423SMike Smith     printf("ACPI debug layer 0x%x  debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel);
186315e32d5dSMike Smith }
18642a4ac806SMike Smith SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL);
186515e32d5dSMike Smith #endif
1866