xref: /freebsd/sys/dev/acpica/acpi.c (revision a1fccb476542d7b66020eefb0395d7d577c25664)
115e32d5dSMike Smith /*-
215e32d5dSMike Smith  * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org>
315e32d5dSMike Smith  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4cb9b0d80SMike Smith  * Copyright (c) 2000, 2001 Michael Smith
515e32d5dSMike Smith  * Copyright (c) 2000 BSDi
615e32d5dSMike Smith  * All rights reserved.
715e32d5dSMike Smith  *
815e32d5dSMike Smith  * Redistribution and use in source and binary forms, with or without
915e32d5dSMike Smith  * modification, are permitted provided that the following conditions
1015e32d5dSMike Smith  * are met:
1115e32d5dSMike Smith  * 1. Redistributions of source code must retain the above copyright
1215e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer.
1315e32d5dSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
1415e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer in the
1515e32d5dSMike Smith  *    documentation and/or other materials provided with the distribution.
1615e32d5dSMike Smith  *
1715e32d5dSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1815e32d5dSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1915e32d5dSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2015e32d5dSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2115e32d5dSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2215e32d5dSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2315e32d5dSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2415e32d5dSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2515e32d5dSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2615e32d5dSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2715e32d5dSMike Smith  * SUCH DAMAGE.
2815e32d5dSMike Smith  *
2915e32d5dSMike Smith  *	$FreeBSD$
3015e32d5dSMike Smith  */
3115e32d5dSMike Smith 
3215e32d5dSMike Smith #include "opt_acpi.h"
3315e32d5dSMike Smith #include <sys/param.h>
3415e32d5dSMike Smith #include <sys/kernel.h>
35fb919e4dSMark Murray #include <sys/proc.h>
36f34fa851SJohn Baldwin #include <sys/lock.h>
3715e32d5dSMike Smith #include <sys/malloc.h>
38b2c9c0daSMitsuru IWASAKI #include <sys/mutex.h>
3915e32d5dSMike Smith #include <sys/bus.h>
4015e32d5dSMike Smith #include <sys/conf.h>
4115e32d5dSMike Smith #include <sys/ioccom.h>
4215e32d5dSMike Smith #include <sys/reboot.h>
4315e32d5dSMike Smith #include <sys/sysctl.h>
4415e32d5dSMike Smith #include <sys/ctype.h>
451611ea87SMitsuru IWASAKI #include <sys/linker.h>
46f9390180SMitsuru IWASAKI #include <sys/power.h>
4715e32d5dSMike Smith 
4815e32d5dSMike Smith #include <machine/clock.h>
4915e32d5dSMike Smith #include <machine/resource.h>
5015e32d5dSMike Smith 
51bc0f2195SMike Smith #include <isa/isavar.h>
52bc0f2195SMike Smith 
5315e32d5dSMike Smith #include "acpi.h"
5415e32d5dSMike Smith 
551611ea87SMitsuru IWASAKI #include <dev/acpica/acpica_support.h>
561611ea87SMitsuru IWASAKI 
5715e32d5dSMike Smith #include <dev/acpica/acpivar.h>
5815e32d5dSMike Smith #include <dev/acpica/acpiio.h>
5915e32d5dSMike Smith 
6015e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
6115e32d5dSMike Smith 
6215e32d5dSMike Smith /*
630ae55423SMike Smith  * Hooks for the ACPI CA debugging infrastructure
640ae55423SMike Smith  */
65cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
66b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
670ae55423SMike Smith 
680ae55423SMike Smith /*
6915e32d5dSMike Smith  * Character device
7015e32d5dSMike Smith  */
7115e32d5dSMike Smith 
7215e32d5dSMike Smith static d_open_t		acpiopen;
7315e32d5dSMike Smith static d_close_t	acpiclose;
7415e32d5dSMike Smith static d_ioctl_t	acpiioctl;
7515e32d5dSMike Smith 
7615e32d5dSMike Smith #define CDEV_MAJOR 152
7715e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
7815e32d5dSMike Smith     acpiopen,
7915e32d5dSMike Smith     acpiclose,
8015e32d5dSMike Smith     noread,
8115e32d5dSMike Smith     nowrite,
8215e32d5dSMike Smith     acpiioctl,
8315e32d5dSMike Smith     nopoll,
8415e32d5dSMike Smith     nommap,
8515e32d5dSMike Smith     nostrategy,
8615e32d5dSMike Smith     "acpi",
8715e32d5dSMike Smith     CDEV_MAJOR,
8815e32d5dSMike Smith     nodump,
8915e32d5dSMike Smith     nopsize,
90606e1d68SJohn Baldwin     0
9115e32d5dSMike Smith };
9215e32d5dSMike Smith 
931d073b1dSJohn Baldwin static const char* sleep_state_names[] = {
94b8670bedSMitsuru IWASAKI     "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
951d073b1dSJohn Baldwin 
962a4ac806SMike Smith /* this has to be static, as the softc is gone when we need it */
972a4ac806SMike Smith static int acpi_off_state = ACPI_STATE_S5;
982a4ac806SMike Smith 
99cb9b0d80SMike Smith struct mtx	acpi_mutex;
100cb9b0d80SMike Smith 
1016d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
10215e32d5dSMike Smith static void	acpi_identify(driver_t *driver, device_t parent);
10315e32d5dSMike Smith static int	acpi_probe(device_t dev);
10415e32d5dSMike Smith static int	acpi_attach(device_t dev);
10515e32d5dSMike Smith static device_t	acpi_add_child(device_t bus, int order, const char *name, int unit);
10615e32d5dSMike Smith static int	acpi_print_resources(struct resource_list *rl, const char *name, int type,
10715e32d5dSMike Smith 				     const char *format);
10815e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
10915e32d5dSMike Smith static int	acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result);
11015e32d5dSMike Smith static int	acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value);
11115e32d5dSMike Smith static int	acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start,
11215e32d5dSMike Smith 				  u_long count);
11315e32d5dSMike Smith static int	acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp,
11415e32d5dSMike Smith 				  u_long *countp);
11515e32d5dSMike Smith static struct resource *acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
11615e32d5dSMike Smith 					    u_long start, u_long end, u_long count, u_int flags);
11715e32d5dSMike Smith static int	acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r);
11832d18aa5SMike Smith static u_int32_t acpi_isa_get_logicalid(device_t dev);
119bc0f2195SMike Smith static int	acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids);
12015e32d5dSMike Smith 
12115e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
12215e32d5dSMike Smith static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status);
12315e32d5dSMike Smith 
12415e32d5dSMike Smith static void	acpi_shutdown_pre_sync(void *arg, int howto);
12515e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
12615e32d5dSMike Smith 
12713d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
12813d4f7baSMitsuru IWASAKI 
12915e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
13015e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
1311d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
13215e32d5dSMike Smith 
133f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
134f9390180SMitsuru IWASAKI 
13515e32d5dSMike Smith static device_method_t acpi_methods[] = {
13615e32d5dSMike Smith     /* Device interface */
13715e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
13815e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
13915e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
14015e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
14115e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
14215e32d5dSMike Smith     DEVMETHOD(device_resume,		bus_generic_resume),
14315e32d5dSMike Smith 
14415e32d5dSMike Smith     /* Bus interface */
14515e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
14615e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
14715e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
14815e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
14915e32d5dSMike Smith     DEVMETHOD(bus_set_resource,		acpi_set_resource),
15015e32d5dSMike Smith     DEVMETHOD(bus_get_resource,		acpi_get_resource),
15115e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
15215e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
15315e32d5dSMike Smith     DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
15415e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
15515e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
15615e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
15715e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
15815e32d5dSMike Smith 
159bc0f2195SMike Smith     /* ISA emulation */
160bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
161bc0f2195SMike Smith 
16215e32d5dSMike Smith     {0, 0}
16315e32d5dSMike Smith };
16415e32d5dSMike Smith 
16515e32d5dSMike Smith static driver_t acpi_driver = {
16615e32d5dSMike Smith     "acpi",
16715e32d5dSMike Smith     acpi_methods,
16815e32d5dSMike Smith     sizeof(struct acpi_softc),
16915e32d5dSMike Smith };
17015e32d5dSMike Smith 
1713273b005SMike Smith static devclass_t acpi_devclass;
1726d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
173dde24897SMike Smith MODULE_VERSION(acpi, 100);
17415e32d5dSMike Smith 
17515e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, "");
17615e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, "");
177dde24897SMike Smith static int acpi_ca_version = ACPI_CA_VERSION;
178dd081ed5SMitsuru IWASAKI SYSCTL_INT(_debug, OID_AUTO, acpi_ca_version, CTLFLAG_RD, &acpi_ca_version, 0, "");
17915e32d5dSMike Smith 
18015e32d5dSMike Smith /*
1816d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
1826d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
1836d63101aSMike Smith  * It also cannot be unloaded, since the entire system bus heirarchy hangs off it.
1846d63101aSMike Smith  */
1856d63101aSMike Smith static int
1866d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
1876d63101aSMike Smith {
1886d63101aSMike Smith     switch(event) {
1896d63101aSMike Smith     case MOD_LOAD:
1906d63101aSMike Smith 	if (!cold)
1916d63101aSMike Smith 	    return(EPERM);
1926d63101aSMike Smith 	break;
1936d63101aSMike Smith     case MOD_UNLOAD:
194f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
1956d63101aSMike Smith 	    return(EBUSY);
196f9390180SMitsuru IWASAKI 	break;
1976d63101aSMike Smith     default:
1986d63101aSMike Smith 	break;
1996d63101aSMike Smith     }
2006d63101aSMike Smith     return(0);
2016d63101aSMike Smith }
2026d63101aSMike Smith 
2036d63101aSMike Smith /*
20415e32d5dSMike Smith  * Detect ACPI, perform early initialisation
20515e32d5dSMike Smith  */
20615e32d5dSMike Smith static void
20715e32d5dSMike Smith acpi_identify(driver_t *driver, device_t parent)
20815e32d5dSMike Smith {
20915e32d5dSMike Smith     device_t			child;
21015e32d5dSMike Smith     int				error;
21115e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
212d786139cSMaxime Henrion     char			*debugpoint;
21315e32d5dSMike Smith #endif
21415e32d5dSMike Smith 
215b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2160ae55423SMike Smith 
217840f9b53STakanori Watanabe     if(!cold){
218840f9b53STakanori Watanabe 	    printf("Don't load this driver from userland!!\n");
219840f9b53STakanori Watanabe 	    return ;
220840f9b53STakanori Watanabe     }
221840f9b53STakanori Watanabe 
22215e32d5dSMike Smith     /*
22332d18aa5SMike Smith      * Check that we haven't been disabled with a hint.
22432d18aa5SMike Smith      */
22532d18aa5SMike Smith     if (!resource_int_value("acpi", 0, "disabled", &error) &&
22632d18aa5SMike Smith 	(error != 0))
22732d18aa5SMike Smith 	return_VOID;
22832d18aa5SMike Smith 
22932d18aa5SMike Smith     /*
23015e32d5dSMike Smith      * Make sure we're not being doubly invoked.
23115e32d5dSMike Smith      */
23215e32d5dSMike Smith     if (device_find_child(parent, "acpi", 0) != NULL)
2330ae55423SMike Smith 	return_VOID;
23415e32d5dSMike Smith 
235cb9b0d80SMike Smith     /* initialise the ACPI mutex */
2366008862bSJohn Baldwin     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
237cb9b0d80SMike Smith 
23815e32d5dSMike Smith     /*
2392a4ac806SMike Smith      * Start up the ACPI CA subsystem.
24015e32d5dSMike Smith      */
24115e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
242d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
243d786139cSMaxime Henrion     if (debugpoint) {
244d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "init"))
24515e32d5dSMike Smith 	    acpi_EnterDebugger();
246d786139cSMaxime Henrion         freeenv(debugpoint);
247d786139cSMaxime Henrion     }
24815e32d5dSMike Smith #endif
249b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
250bfae45aaSMike Smith 	printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
2510ae55423SMike Smith 	return_VOID;
25215e32d5dSMike Smith     }
25315e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
254d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
255d786139cSMaxime Henrion     if (debugpoint) {
256d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "tables"))
25715e32d5dSMike Smith 	    acpi_EnterDebugger();
258d786139cSMaxime Henrion         freeenv(debugpoint);
259d786139cSMaxime Henrion     }
26015e32d5dSMike Smith #endif
2611611ea87SMitsuru IWASAKI 
262b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiLoadTables())) {
263bfae45aaSMike Smith 	printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
2640ae55423SMike Smith 	return_VOID;
26515e32d5dSMike Smith     }
26615e32d5dSMike Smith 
26715e32d5dSMike Smith     /*
26815e32d5dSMike Smith      * Attach the actual ACPI device.
26915e32d5dSMike Smith      */
27015e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
27115e32d5dSMike Smith 	    device_printf(parent, "ACPI: could not attach\n");
2720ae55423SMike Smith 	    return_VOID;
27315e32d5dSMike Smith     }
27415e32d5dSMike Smith }
27515e32d5dSMike Smith 
27615e32d5dSMike Smith /*
27715e32d5dSMike Smith  * Fetch some descriptive data from ACPI to put in our attach message
27815e32d5dSMike Smith  */
27915e32d5dSMike Smith static int
28015e32d5dSMike Smith acpi_probe(device_t dev)
28115e32d5dSMike Smith {
28215e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
28315e32d5dSMike Smith     char		buf[20];
284cb9b0d80SMike Smith     ACPI_STATUS		status;
28515e32d5dSMike Smith     int			error;
28615e32d5dSMike Smith 
287b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
288f9390180SMitsuru IWASAKI 
289f9390180SMitsuru IWASAKI     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
290f9390180SMitsuru IWASAKI         power_pm_get_type() != POWER_PM_TYPE_ACPI) {
291f9390180SMitsuru IWASAKI 	device_printf(dev, "Other PM system enabled.\n");
292f9390180SMitsuru IWASAKI 	return_VALUE(ENXIO);
293f9390180SMitsuru IWASAKI     }
294f9390180SMitsuru IWASAKI 
295cb9b0d80SMike Smith     ACPI_LOCK;
2960ae55423SMike Smith 
297b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
298bfae45aaSMike Smith 	device_printf(dev, "couldn't get XSDT header: %s\n", AcpiFormatException(status));
299cb9b0d80SMike Smith 	error = ENXIO;
300cb9b0d80SMike Smith     } else {
30115e32d5dSMike Smith 	sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId);
30215e32d5dSMike Smith 	device_set_desc_copy(dev, buf);
303cb9b0d80SMike Smith 	error = 0;
304cb9b0d80SMike Smith     }
305cb9b0d80SMike Smith     ACPI_UNLOCK;
306cb9b0d80SMike Smith     return_VALUE(error);
30715e32d5dSMike Smith }
30815e32d5dSMike Smith 
30915e32d5dSMike Smith static int
31015e32d5dSMike Smith acpi_attach(device_t dev)
31115e32d5dSMike Smith {
31215e32d5dSMike Smith     struct acpi_softc	*sc;
313cb9b0d80SMike Smith     ACPI_STATUS		status;
31415e32d5dSMike Smith     int			error;
31532d18aa5SMike Smith     UINT32		flags;
31632d18aa5SMike Smith 
31715e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
318d786139cSMaxime Henrion     char		*debugpoint;
31915e32d5dSMike Smith #endif
32015e32d5dSMike Smith 
321b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
322cb9b0d80SMike Smith     ACPI_LOCK;
32315e32d5dSMike Smith     sc = device_get_softc(dev);
32415e32d5dSMike Smith     bzero(sc, sizeof(*sc));
32515e32d5dSMike Smith     sc->acpi_dev = dev;
32615e32d5dSMike Smith 
32715e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
328d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
329d786139cSMaxime Henrion     if (debugpoint) {
330d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "spaces"))
33115e32d5dSMike Smith 	    acpi_EnterDebugger();
332d786139cSMaxime Henrion         freeenv(debugpoint);
333d786139cSMaxime Henrion     }
33415e32d5dSMike Smith #endif
33515e32d5dSMike Smith 
33615e32d5dSMike Smith     /*
33715e32d5dSMike Smith      * Install the default address space handlers.
33815e32d5dSMike Smith      */
339cb9b0d80SMike Smith     error = ENXIO;
340b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
3412a4ac806SMike Smith 						ACPI_ADR_SPACE_SYSTEM_MEMORY,
34215e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
343b53f2771SMike Smith 						NULL, NULL))) {
344bfae45aaSMike Smith 	device_printf(dev, "could not initialise SystemMemory handler: %s\n", AcpiFormatException(status));
345cb9b0d80SMike Smith 	goto out;
34615e32d5dSMike Smith     }
347b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
3482a4ac806SMike Smith 						ACPI_ADR_SPACE_SYSTEM_IO,
34915e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
350b53f2771SMike Smith 						NULL, NULL))) {
351bfae45aaSMike Smith 	device_printf(dev, "could not initialise SystemIO handler: %s\n", AcpiFormatException(status));
352cb9b0d80SMike Smith 	goto out;
35315e32d5dSMike Smith     }
354b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
3552a4ac806SMike Smith 						ACPI_ADR_SPACE_PCI_CONFIG,
35615e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
357b53f2771SMike Smith 						NULL, NULL))) {
358bfae45aaSMike Smith 	device_printf(dev, "could not initialise PciConfig handler: %s\n", AcpiFormatException(status));
359cb9b0d80SMike Smith 	goto out;
36015e32d5dSMike Smith     }
36115e32d5dSMike Smith 
36215e32d5dSMike Smith     /*
36315e32d5dSMike Smith      * Bring ACPI fully online.
36415e32d5dSMike Smith      *
36532d18aa5SMike Smith      * Note that some systems (specifically, those with namespace evaluation issues
36632d18aa5SMike Smith      * that require the avoidance of parts of the namespace) must avoid running _INI
36732d18aa5SMike Smith      * and _STA on everything, as well as dodging the final object init pass.
36815e32d5dSMike Smith      *
36932d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
37032d18aa5SMike Smith      *
37132d18aa5SMike Smith      * XXX We should arrange for the object init pass after we have attached all our
37232d18aa5SMike Smith      *     child devices, but on many systems it works here.
37315e32d5dSMike Smith      */
37415e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
375d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
376d786139cSMaxime Henrion     if (debugpoint) {
377d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "enable"))
37815e32d5dSMike Smith 	    acpi_EnterDebugger();
379d786139cSMaxime Henrion         freeenv(debugpoint);
380d786139cSMaxime Henrion     }
38115e32d5dSMike Smith #endif
38232d18aa5SMike Smith     flags = 0;
383d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
38432d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
385b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
386bfae45aaSMike Smith 	device_printf(dev, "could not enable ACPI: %s\n", AcpiFormatException(status));
387cb9b0d80SMike Smith 	goto out;
38815e32d5dSMike Smith     }
38915e32d5dSMike Smith 
39015e32d5dSMike Smith     /*
3911d073b1dSJohn Baldwin      * Setup our sysctl tree.
3921d073b1dSJohn Baldwin      *
3931d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
3941d073b1dSJohn Baldwin      */
3951d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
3961d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
3971d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
3981d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
3991d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4001d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4011d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4021d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4031d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4041d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4051d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4061d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
4071d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
408f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
409f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
410f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
411f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
412f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
413f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
4142d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4151611ea87SMitsuru IWASAKI 	OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
4161611ea87SMitsuru IWASAKI 	&sc->acpi_s4bios, 0, "S4BIOS mode");
4171611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4182d644607SMitsuru IWASAKI 	OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
4192d644607SMitsuru IWASAKI 	&sc->acpi_verbose, 0, "verbose mode");
420931a10c9SMitsuru IWASAKI     sc->acpi_s4bios = 1;
4212d644607SMitsuru IWASAKI     if (bootverbose)
4222d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
4231d073b1dSJohn Baldwin 
4241d073b1dSJohn Baldwin     /*
42515e32d5dSMike Smith      * Dispatch the default sleep state to devices.
42615e32d5dSMike Smith      * TBD: should be configured from userland policy manager.
42715e32d5dSMike Smith      */
42815e32d5dSMike Smith     sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
42915e32d5dSMike Smith     sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
43015e32d5dSMike Smith     sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
431f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
432f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
43315e32d5dSMike Smith 
43413d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
43515e32d5dSMike Smith 
43615e32d5dSMike Smith     /*
43715e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
43815e32d5dSMike Smith      */
43915e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
440d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
441d786139cSMaxime Henrion     if (debugpoint) {
442d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "probe"))
44315e32d5dSMike Smith 	    acpi_EnterDebugger();
444d786139cSMaxime Henrion 	freeenv(debugpoint);
445d786139cSMaxime Henrion     }
44615e32d5dSMike Smith #endif
4470ae55423SMike Smith     if (!acpi_disabled("bus"))
44815e32d5dSMike Smith 	acpi_probe_children(dev);
44915e32d5dSMike Smith 
45015e32d5dSMike Smith     /*
45115e32d5dSMike Smith      * Register our shutdown handlers
45215e32d5dSMike Smith      */
45315e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST);
45415e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST);
45515e32d5dSMike Smith 
45615e32d5dSMike Smith     /*
45715e32d5dSMike Smith      * Register our acpi event handlers.
45815e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
45915e32d5dSMike Smith      */
46015e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST);
46115e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST);
46215e32d5dSMike Smith 
46315e32d5dSMike Smith     /*
46415e32d5dSMike Smith      * Flag our initial states.
46515e32d5dSMike Smith      */
46615e32d5dSMike Smith     sc->acpi_enabled = 1;
46715e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
468ece50487SMitsuru IWASAKI     sc->acpi_sleep_disabled = 0;
46915e32d5dSMike Smith 
47015e32d5dSMike Smith     /*
47115e32d5dSMike Smith      * Create the control device
47215e32d5dSMike Smith      */
47315e32d5dSMike Smith     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, 0, 5, 0660, "acpi");
47415e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
47515e32d5dSMike Smith 
47615e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
477d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
478d786139cSMaxime Henrion     if (debugpoint) {
479d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "running"))
48015e32d5dSMike Smith 	    acpi_EnterDebugger();
481d786139cSMaxime Henrion 	freeenv(debugpoint);
482d786139cSMaxime Henrion     }
48315e32d5dSMike Smith #endif
484f86214b6SMitsuru IWASAKI 
485c573e654SMitsuru IWASAKI #if defined(ACPI_MAX_THREADS) && ACPI_MAX_THREADS > 0
486c573e654SMitsuru IWASAKI     if ((error = acpi_task_thread_init())) {
487c573e654SMitsuru IWASAKI 	goto out;
488c573e654SMitsuru IWASAKI     }
489c573e654SMitsuru IWASAKI #endif
490c573e654SMitsuru IWASAKI 
491f86214b6SMitsuru IWASAKI     if ((error = acpi_machdep_init(dev))) {
492f86214b6SMitsuru IWASAKI 	goto out;
493f86214b6SMitsuru IWASAKI     }
494f86214b6SMitsuru IWASAKI 
495f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
496f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
497f9390180SMitsuru IWASAKI 
498cb9b0d80SMike Smith     error = 0;
499cb9b0d80SMike Smith 
500cb9b0d80SMike Smith  out:
501cb9b0d80SMike Smith     ACPI_UNLOCK;
502cb9b0d80SMike Smith     return_VALUE(error);
50315e32d5dSMike Smith }
50415e32d5dSMike Smith 
50515e32d5dSMike Smith /*
50615e32d5dSMike Smith  * Handle a new device being added
50715e32d5dSMike Smith  */
50815e32d5dSMike Smith static device_t
50915e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
51015e32d5dSMike Smith {
51115e32d5dSMike Smith     struct acpi_device	*ad;
51215e32d5dSMike Smith     device_t		child;
51315e32d5dSMike Smith 
51415e32d5dSMike Smith     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL)
51515e32d5dSMike Smith 	return(NULL);
51615e32d5dSMike Smith     bzero(ad, sizeof(*ad));
51715e32d5dSMike Smith 
51815e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
51915e32d5dSMike Smith 
52015e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
52115e32d5dSMike Smith     if (child != NULL)
52215e32d5dSMike Smith 	device_set_ivars(child, ad);
52315e32d5dSMike Smith     return(child);
52415e32d5dSMike Smith }
52515e32d5dSMike Smith 
52615e32d5dSMike Smith /*
52715e32d5dSMike Smith  * Print child device resource usage
52815e32d5dSMike Smith  */
52915e32d5dSMike Smith static int
53015e32d5dSMike Smith acpi_print_resources(struct resource_list *rl, const char *name, int type, const char *format)
53115e32d5dSMike Smith {
53215e32d5dSMike Smith     struct resource_list_entry	*rle;
53315e32d5dSMike Smith     int				printed, retval;
53415e32d5dSMike Smith 
53515e32d5dSMike Smith     printed = 0;
53615e32d5dSMike Smith     retval = 0;
53715e32d5dSMike Smith 
53815e32d5dSMike Smith     if (!SLIST_FIRST(rl))
53915e32d5dSMike Smith 	return(0);
54015e32d5dSMike Smith 
54115e32d5dSMike Smith     /* Yes, this is kinda cheating */
54215e32d5dSMike Smith     SLIST_FOREACH(rle, rl, link) {
54315e32d5dSMike Smith 	if (rle->type == type) {
54415e32d5dSMike Smith 	    if (printed == 0)
54515e32d5dSMike Smith 		retval += printf(" %s ", name);
54615e32d5dSMike Smith 	    else if (printed > 0)
54715e32d5dSMike Smith 		retval += printf(",");
54815e32d5dSMike Smith 	    printed++;
54915e32d5dSMike Smith 	    retval += printf(format, rle->start);
55015e32d5dSMike Smith 	    if (rle->count > 1) {
55115e32d5dSMike Smith 		retval += printf("-");
55215e32d5dSMike Smith 		retval += printf(format, rle->start +
55315e32d5dSMike Smith 				 rle->count - 1);
55415e32d5dSMike Smith 	    }
55515e32d5dSMike Smith 	}
55615e32d5dSMike Smith     }
55715e32d5dSMike Smith     return(retval);
55815e32d5dSMike Smith }
55915e32d5dSMike Smith 
56015e32d5dSMike Smith static int
56115e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
56215e32d5dSMike Smith {
56315e32d5dSMike Smith     struct acpi_device		*adev = device_get_ivars(child);
56415e32d5dSMike Smith     struct resource_list	*rl = &adev->ad_rl;
56515e32d5dSMike Smith     int retval = 0;
56615e32d5dSMike Smith 
56715e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
56815e32d5dSMike Smith     retval += acpi_print_resources(rl, "port",  SYS_RES_IOPORT, "%#lx");
56915e32d5dSMike Smith     retval += acpi_print_resources(rl, "iomem", SYS_RES_MEMORY, "%#lx");
57015e32d5dSMike Smith     retval += acpi_print_resources(rl, "irq",   SYS_RES_IRQ,    "%ld");
57132b58d13STakanori Watanabe     retval += acpi_print_resources(rl, "drq",   SYS_RES_DRQ,    "%ld");
57215e32d5dSMike Smith     retval += bus_print_child_footer(bus, child);
57315e32d5dSMike Smith 
57415e32d5dSMike Smith     return(retval);
57515e32d5dSMike Smith }
57615e32d5dSMike Smith 
57715e32d5dSMike Smith 
57815e32d5dSMike Smith /*
57915e32d5dSMike Smith  * Handle per-device ivars
58015e32d5dSMike Smith  */
58115e32d5dSMike Smith static int
58215e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
58315e32d5dSMike Smith {
58415e32d5dSMike Smith     struct acpi_device	*ad;
58515e32d5dSMike Smith 
58615e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
58715e32d5dSMike Smith 	printf("device has no ivars\n");
58815e32d5dSMike Smith 	return(ENOENT);
58915e32d5dSMike Smith     }
59015e32d5dSMike Smith 
59115e32d5dSMike Smith     switch(index) {
59215e32d5dSMike Smith 	/* ACPI ivars */
59315e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
59415e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
59515e32d5dSMike Smith 	break;
59615e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
59715e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
59815e32d5dSMike Smith 	break;
59915e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
60015e32d5dSMike Smith 	*(void **)result = ad->ad_private;
60115e32d5dSMike Smith 	break;
60215e32d5dSMike Smith 
60332d18aa5SMike Smith 	/* ISA compatibility */
60432d18aa5SMike Smith     case ISA_IVAR_VENDORID:
60532d18aa5SMike Smith     case ISA_IVAR_SERIAL:
60632d18aa5SMike Smith     case ISA_IVAR_COMPATID:
60732d18aa5SMike Smith 	*(int *)result = -1;
60832d18aa5SMike Smith 	break;
60932d18aa5SMike Smith 
61032d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
61132d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
61232d18aa5SMike Smith 	break;
61332d18aa5SMike Smith 
61415e32d5dSMike Smith     default:
61515e32d5dSMike Smith 	panic("bad ivar read request (%d)\n", index);
61615e32d5dSMike Smith 	return(ENOENT);
61715e32d5dSMike Smith     }
61815e32d5dSMike Smith     return(0);
61915e32d5dSMike Smith }
62015e32d5dSMike Smith 
62115e32d5dSMike Smith static int
62215e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
62315e32d5dSMike Smith {
62415e32d5dSMike Smith     struct acpi_device	*ad;
62515e32d5dSMike Smith 
62615e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
62715e32d5dSMike Smith 	printf("device has no ivars\n");
62815e32d5dSMike Smith 	return(ENOENT);
62915e32d5dSMike Smith     }
63015e32d5dSMike Smith 
63115e32d5dSMike Smith     switch(index) {
63215e32d5dSMike Smith 	/* ACPI ivars */
63315e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
63415e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
63515e32d5dSMike Smith 	break;
63615e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
63715e32d5dSMike Smith 	ad->ad_magic = (int )value;
63815e32d5dSMike Smith 	break;
63915e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
64015e32d5dSMike Smith 	ad->ad_private = (void *)value;
64115e32d5dSMike Smith 	break;
64215e32d5dSMike Smith 
64315e32d5dSMike Smith     default:
64415e32d5dSMike Smith 	panic("bad ivar write request (%d)\n", index);
64515e32d5dSMike Smith 	return(ENOENT);
64615e32d5dSMike Smith     }
64715e32d5dSMike Smith     return(0);
64815e32d5dSMike Smith }
64915e32d5dSMike Smith 
65015e32d5dSMike Smith /*
65115e32d5dSMike Smith  * Handle child resource allocation/removal
65215e32d5dSMike Smith  */
65315e32d5dSMike Smith static int
65415e32d5dSMike Smith acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
65515e32d5dSMike Smith {
65615e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
65715e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
65815e32d5dSMike Smith 
65915e32d5dSMike Smith     resource_list_add(rl, type, rid, start, start + count -1, count);
66015e32d5dSMike Smith 
66115e32d5dSMike Smith     return(0);
66215e32d5dSMike Smith }
66315e32d5dSMike Smith 
66415e32d5dSMike Smith static int
66515e32d5dSMike Smith acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
66615e32d5dSMike Smith {
66715e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
66815e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
66915e32d5dSMike Smith     struct resource_list_entry	*rle;
67015e32d5dSMike Smith 
67115e32d5dSMike Smith     rle = resource_list_find(rl, type, rid);
67215e32d5dSMike Smith     if (!rle)
67315e32d5dSMike Smith 	return(ENOENT);
67415e32d5dSMike Smith 
67515e32d5dSMike Smith     if (startp)
67615e32d5dSMike Smith 	*startp = rle->start;
67715e32d5dSMike Smith     if (countp)
67815e32d5dSMike Smith 	*countp = rle->count;
67915e32d5dSMike Smith 
68015e32d5dSMike Smith     return(0);
68115e32d5dSMike Smith }
68215e32d5dSMike Smith 
68315e32d5dSMike Smith static struct resource *
68415e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
68515e32d5dSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
68615e32d5dSMike Smith {
68715e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
68815e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
68915e32d5dSMike Smith 
69015e32d5dSMike Smith     return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags));
69115e32d5dSMike Smith }
69215e32d5dSMike Smith 
69315e32d5dSMike Smith static int
69415e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
69515e32d5dSMike Smith {
69615e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
69715e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
69815e32d5dSMike Smith 
69915e32d5dSMike Smith     return(resource_list_release(rl, bus, child, type, rid, r));
70015e32d5dSMike Smith }
70115e32d5dSMike Smith 
70215e32d5dSMike Smith /*
703bc0f2195SMike Smith  * Handle ISA-like devices probing for a PnP ID to match.
704bc0f2195SMike Smith  */
705bc0f2195SMike Smith #define PNP_EISAID(s)				\
706bc0f2195SMike Smith 	((((s[0] - '@') & 0x1f) << 2)		\
707bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x18) >> 3)		\
708bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x07) << 13)	\
709bc0f2195SMike Smith 	 | (((s[2] - '@') & 0x1f) << 8)		\
710bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[4]) << 16)		\
711bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[3]) << 20)		\
712bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[6]) << 24)		\
713bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[5]) << 28))
714bc0f2195SMike Smith 
71532d18aa5SMike Smith static u_int32_t
71632d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
717bc0f2195SMike Smith {
718bc0f2195SMike Smith     ACPI_HANDLE		h;
719bc0f2195SMike Smith     ACPI_DEVICE_INFO	devinfo;
720bc0f2195SMike Smith     ACPI_STATUS		error;
721bc0f2195SMike Smith     u_int32_t		pnpid;
72232d18aa5SMike Smith 
723b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
72432d18aa5SMike Smith 
72532d18aa5SMike Smith     pnpid = 0;
72632d18aa5SMike Smith     ACPI_LOCK;
72732d18aa5SMike Smith 
72832d18aa5SMike Smith     /* fetch and validate the HID */
72932d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
73032d18aa5SMike Smith 	goto out;
731b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
73232d18aa5SMike Smith 	goto out;
73332d18aa5SMike Smith     if (!(devinfo.Valid & ACPI_VALID_HID))
73432d18aa5SMike Smith 	goto out;
73532d18aa5SMike Smith 
73632d18aa5SMike Smith     pnpid = PNP_EISAID(devinfo.HardwareId);
73732d18aa5SMike Smith out:
73832d18aa5SMike Smith     ACPI_UNLOCK;
73932d18aa5SMike Smith     return_VALUE(pnpid);
74032d18aa5SMike Smith }
74132d18aa5SMike Smith 
74232d18aa5SMike Smith static int
74332d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
74432d18aa5SMike Smith {
745bc0f2195SMike Smith     int			result;
74632d18aa5SMike Smith     u_int32_t		pnpid;
747bc0f2195SMike Smith 
748b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
749bc0f2195SMike Smith 
750bc0f2195SMike Smith     /*
751bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
752bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
753bc0f2195SMike Smith      * that to happen, so don't ever return it.
754bc0f2195SMike Smith      */
755bc0f2195SMike Smith     result = ENXIO;
756bc0f2195SMike Smith 
757bc0f2195SMike Smith     /* scan the supplied IDs for a match */
75832d18aa5SMike Smith     pnpid = acpi_isa_get_logicalid(child);
759bc0f2195SMike Smith     while (ids && ids->ip_id) {
760bc0f2195SMike Smith 	if (pnpid == ids->ip_id) {
761bc0f2195SMike Smith 	    result = 0;
762bc0f2195SMike Smith 	    goto out;
763bc0f2195SMike Smith 	}
764bc0f2195SMike Smith 	ids++;
765bc0f2195SMike Smith     }
766bc0f2195SMike Smith  out:
767bc0f2195SMike Smith     return_VALUE(result);
768bc0f2195SMike Smith }
769bc0f2195SMike Smith 
770bc0f2195SMike Smith /*
77115e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
77215e32d5dSMike Smith  *
7737d3bcec9SMike Smith  * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and \_SB_ scopes,
7747d3bcec9SMike Smith  * and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
77515e32d5dSMike Smith  */
77615e32d5dSMike Smith static void
77715e32d5dSMike Smith acpi_probe_children(device_t bus)
77815e32d5dSMike Smith {
77915e32d5dSMike Smith     ACPI_HANDLE		parent;
7807d3bcec9SMike Smith     static char		*scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
78115e32d5dSMike Smith     int			i;
78215e32d5dSMike Smith 
783b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
784cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
7850ae55423SMike Smith 
78615e32d5dSMike Smith     /*
78715e32d5dSMike Smith      * Create any static children by calling device identify methods.
78815e32d5dSMike Smith      */
7894c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
79015e32d5dSMike Smith     bus_generic_probe(bus);
79115e32d5dSMike Smith 
79215e32d5dSMike Smith     /*
79315e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
79415e32d5dSMike Smith      * we find.
79515e32d5dSMike Smith      *
79615e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
79715e32d5dSMike Smith      * we want to create nodes for all devices, not just those that are currently
79815e32d5dSMike Smith      * present. (This assumes that we don't want to create/remove devices as they
79915e32d5dSMike Smith      * appear, which might be smarter.)
80015e32d5dSMike Smith      */
8014c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
80215e32d5dSMike Smith     for (i = 0; scopes[i] != NULL; i++)
803b53f2771SMike Smith 	if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent)))
80415e32d5dSMike Smith 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL);
80515e32d5dSMike Smith 
80615e32d5dSMike Smith     /*
80715e32d5dSMike Smith      * Scan all of the child devices we have created and let them probe/attach.
80815e32d5dSMike Smith      */
8094c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
81015e32d5dSMike Smith     bus_generic_attach(bus);
81115e32d5dSMike Smith 
81215e32d5dSMike Smith     /*
81315e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
81415e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
81515e32d5dSMike Smith      */
8164c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
81715e32d5dSMike Smith     bus_generic_attach(bus);
8180ae55423SMike Smith 
819bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
8200ae55423SMike Smith     return_VOID;
82115e32d5dSMike Smith }
82215e32d5dSMike Smith 
82315e32d5dSMike Smith /*
82415e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
82515e32d5dSMike Smith  * it.
82615e32d5dSMike Smith  */
82715e32d5dSMike Smith static ACPI_STATUS
82815e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
82915e32d5dSMike Smith {
83015e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
83115e32d5dSMike Smith     device_t		child, bus = (device_t)context;
83215e32d5dSMike Smith 
833b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
8340ae55423SMike Smith 
8350ae55423SMike Smith     /*
8360ae55423SMike Smith      * Skip this device if we think we'll have trouble with it.
8370ae55423SMike Smith      */
8380ae55423SMike Smith     if (acpi_avoid(handle))
8390ae55423SMike Smith 	return_ACPI_STATUS(AE_OK);
8400ae55423SMike Smith 
841b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
84215e32d5dSMike Smith 	switch(type) {
84315e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
84415e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
84515e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
84615e32d5dSMike Smith 	case ACPI_TYPE_POWER:
8470ae55423SMike Smith 	    if (acpi_disabled("children"))
8480ae55423SMike Smith 		break;
84915e32d5dSMike Smith 	    /*
85015e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
85115e32d5dSMike Smith 	     * so that the probe/attach passes will run breadth-first.
85215e32d5dSMike Smith 	     */
8534c1cdee6SMike Smith 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", acpi_name(handle)));
85415e32d5dSMike Smith 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
855bc0f2195SMike Smith 	    if (child == NULL)
856bc0f2195SMike Smith 		break;
85715e32d5dSMike Smith 	    acpi_set_handle(child, handle);
858bc0f2195SMike Smith 
859bc0f2195SMike Smith 	    /*
860b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
861b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
862b519f9d6SMike Smith 	     * the handle, but we don't probe it).
863b519f9d6SMike Smith 	     */
86423b4e251SMitsuru IWASAKI 	    if ((type == ACPI_TYPE_DEVICE) && (!acpi_DeviceIsPresent(child))) {
865b519f9d6SMike Smith 		device_disable(child);
866b519f9d6SMike Smith 		break;
867b519f9d6SMike Smith 	    }
868b519f9d6SMike Smith 
869b519f9d6SMike Smith 	    /*
870bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
871bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
872bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
873bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
874bc0f2195SMike Smith 	     * device not to have any resources.
875bc0f2195SMike Smith 	     */
876bc0f2195SMike Smith 	    acpi_parse_resources(child, handle, &acpi_res_parse_set);
877bc0f2195SMike Smith 
878bc0f2195SMike Smith 	    /* if we're debugging, probe/attach now rather than later */
879b53f2771SMike Smith 	    ACPI_DEBUG_EXEC(device_probe_and_attach(child));
8800ae55423SMike Smith 	    break;
88115e32d5dSMike Smith 	}
88215e32d5dSMike Smith     }
8830ae55423SMike Smith     return_ACPI_STATUS(AE_OK);
88415e32d5dSMike Smith }
88515e32d5dSMike Smith 
88615e32d5dSMike Smith static void
88715e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto)
88815e32d5dSMike Smith {
889cb9b0d80SMike Smith 
890cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
891cb9b0d80SMike Smith 
89215e32d5dSMike Smith     /*
8930ae55423SMike Smith      * Disable all ACPI events before soft off, otherwise the system
89415e32d5dSMike Smith      * will be turned on again on some laptops.
89515e32d5dSMike Smith      *
89615e32d5dSMike Smith      * XXX this should probably be restricted to masking some events just
89715e32d5dSMike Smith      *     before powering down, since we may still need ACPI during the
89815e32d5dSMike Smith      *     shutdown process.
89915e32d5dSMike Smith      */
90015e32d5dSMike Smith     acpi_Disable((struct acpi_softc *)arg);
90115e32d5dSMike Smith }
90215e32d5dSMike Smith 
90315e32d5dSMike Smith static void
90415e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
90515e32d5dSMike Smith {
90615e32d5dSMike Smith     ACPI_STATUS	status;
90715e32d5dSMike Smith 
908cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
909cb9b0d80SMike Smith 
9105f3e4175SMitsuru IWASAKI     if (howto & RB_POWEROFF) {
91115e32d5dSMike Smith 	printf("Power system off using ACPI...\n");
912b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(acpi_off_state))) {
913b9c780d6SMitsuru IWASAKI 	    printf("AcpiEnterSleepStatePrep failed - %s\n",
914b9c780d6SMitsuru IWASAKI 		   AcpiFormatException(status));
915b9c780d6SMitsuru IWASAKI 	    return;
916b9c780d6SMitsuru IWASAKI 	}
917b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepState(acpi_off_state))) {
918bfae45aaSMike Smith 	    printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
91915e32d5dSMike Smith 	} else {
92015e32d5dSMike Smith 	    DELAY(1000000);
92115e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
92215e32d5dSMike Smith 	}
923494ae560SMitsuru IWASAKI     } else {
924494ae560SMitsuru IWASAKI 	printf("Terminate ACPI\n");
925494ae560SMitsuru IWASAKI 	AcpiTerminate();
92615e32d5dSMike Smith     }
92715e32d5dSMike Smith }
92815e32d5dSMike Smith 
92913d4f7baSMitsuru IWASAKI static void
93013d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
93113d4f7baSMitsuru IWASAKI {
93213d4f7baSMitsuru IWASAKI     static int	first_time = 1;
93313d4f7baSMitsuru IWASAKI #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n"
93413d4f7baSMitsuru IWASAKI 
935cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
936cb9b0d80SMike Smith 
93713d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
938cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) {
93943896e91SMike Smith 	AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED, 0);
94013d4f7baSMitsuru IWASAKI 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED);
94113d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
94213d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_power_button_for_sleep, sc);
94313d4f7baSMitsuru IWASAKI 	if (first_time) {
94413d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "power");
94513d4f7baSMitsuru IWASAKI 	}
94613d4f7baSMitsuru IWASAKI     }
947cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) {
94843896e91SMike Smith 	AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED, 0);
94913d4f7baSMitsuru IWASAKI 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED);
95013d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
95113d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_sleep_button_for_sleep, sc);
95213d4f7baSMitsuru IWASAKI 	if (first_time) {
95313d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "sleep");
95413d4f7baSMitsuru IWASAKI 	}
95513d4f7baSMitsuru IWASAKI     }
95613d4f7baSMitsuru IWASAKI 
95713d4f7baSMitsuru IWASAKI     first_time = 0;
95813d4f7baSMitsuru IWASAKI }
95913d4f7baSMitsuru IWASAKI 
96015e32d5dSMike Smith /*
961c5ba0be4SMike Smith  * Returns true if the device is actually present and should
962c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
963c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
964c5ba0be4SMike Smith  */
965c5ba0be4SMike Smith BOOLEAN
966c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
967c5ba0be4SMike Smith {
968c5ba0be4SMike Smith     ACPI_HANDLE		h;
969c5ba0be4SMike Smith     ACPI_DEVICE_INFO	devinfo;
970c5ba0be4SMike Smith     ACPI_STATUS		error;
971c5ba0be4SMike Smith 
972cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
973cb9b0d80SMike Smith 
974c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
975c5ba0be4SMike Smith 	return(FALSE);
976b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
977c5ba0be4SMike Smith 	return(FALSE);
97843896e91SMike Smith     /* if no _STA method, must be present */
97943896e91SMike Smith     if (!(devinfo.Valid & ACPI_VALID_STA))
98043896e91SMike Smith 	return(TRUE);
98143896e91SMike Smith     /* return true for 'present' and 'functioning' */
98243896e91SMike Smith     if ((devinfo.CurrentStatus & 0x9) == 0x9)
983c5ba0be4SMike Smith 	return(TRUE);
984c5ba0be4SMike Smith     return(FALSE);
985c5ba0be4SMike Smith }
986c5ba0be4SMike Smith 
987c5ba0be4SMike Smith /*
988b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
989b53f2771SMike Smith  */
990b53f2771SMike Smith BOOLEAN
991b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
992b53f2771SMike Smith {
993b53f2771SMike Smith     ACPI_HANDLE		h;
994b53f2771SMike Smith     ACPI_DEVICE_INFO	devinfo;
995b53f2771SMike Smith     ACPI_STATUS		error;
996b53f2771SMike Smith 
997b53f2771SMike Smith     ACPI_ASSERTLOCK;
998b53f2771SMike Smith 
999b53f2771SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1000b53f2771SMike Smith 	return(FALSE);
1001b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
1002b53f2771SMike Smith 	return(FALSE);
1003b53f2771SMike Smith     /* if no _STA method, must be present */
1004b53f2771SMike Smith     if (!(devinfo.Valid & ACPI_VALID_STA))
1005b53f2771SMike Smith 	return(TRUE);
1006b53f2771SMike Smith     /* return true for 'present' and 'functioning' */
1007b53f2771SMike Smith     if ((devinfo.CurrentStatus & 0x19) == 0x19)
1008b53f2771SMike Smith 	return(TRUE);
1009b53f2771SMike Smith     return(FALSE);
1010b53f2771SMike Smith }
1011b53f2771SMike Smith 
1012b53f2771SMike Smith /*
101315e32d5dSMike Smith  * Match a HID string against a device
101415e32d5dSMike Smith  */
101515e32d5dSMike Smith BOOLEAN
101615e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid)
101715e32d5dSMike Smith {
101815e32d5dSMike Smith     ACPI_HANDLE		h;
101915e32d5dSMike Smith     ACPI_DEVICE_INFO	devinfo;
102015e32d5dSMike Smith     ACPI_STATUS		error;
1021ed136da6SDoug Rabson     int			cid;
102215e32d5dSMike Smith 
1023cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1024cb9b0d80SMike Smith 
10254d332989SMike Smith     if (hid == NULL)
102615e32d5dSMike Smith 	return(FALSE);
102715e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
102815e32d5dSMike Smith 	return(FALSE);
1029b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
103015e32d5dSMike Smith 	return(FALSE);
10314d332989SMike Smith     if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId))
103215e32d5dSMike Smith 	return(TRUE);
1033b53f2771SMike Smith     if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &cid)))
1034ed136da6SDoug Rabson 	return(FALSE);
1035ed136da6SDoug Rabson     if (cid == PNP_EISAID(hid))
1036ed136da6SDoug Rabson 	return(TRUE);
103715e32d5dSMike Smith     return(FALSE);
103815e32d5dSMike Smith }
103915e32d5dSMike Smith 
104015e32d5dSMike Smith /*
1041c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1042c5ba0be4SMike Smith  * or one if its parents.
1043c5ba0be4SMike Smith  */
1044c5ba0be4SMike Smith ACPI_STATUS
1045c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1046c5ba0be4SMike Smith {
1047c5ba0be4SMike Smith     ACPI_HANDLE		r;
1048c5ba0be4SMike Smith     ACPI_STATUS		status;
1049c5ba0be4SMike Smith 
1050cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1051cb9b0d80SMike Smith 
1052c5ba0be4SMike Smith     /* walk back up the tree to the root */
1053c5ba0be4SMike Smith     for (;;) {
1054b53f2771SMike Smith 	if (ACPI_SUCCESS(status = AcpiGetHandle(parent, path, &r))) {
1055c5ba0be4SMike Smith 	    *result = r;
1056c5ba0be4SMike Smith 	    return(AE_OK);
1057c5ba0be4SMike Smith 	}
1058c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1059c5ba0be4SMike Smith 	    return(AE_OK);
1060b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1061c5ba0be4SMike Smith 	    return(AE_NOT_FOUND);
1062c5ba0be4SMike Smith 	parent = r;
1063c5ba0be4SMike Smith     }
1064c5ba0be4SMike Smith }
1065c5ba0be4SMike Smith 
1066c5ba0be4SMike Smith /*
1067c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1068c5ba0be4SMike Smith  */
1069c5ba0be4SMike Smith ACPI_BUFFER *
1070c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1071c5ba0be4SMike Smith {
1072c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1073c5ba0be4SMike Smith 
1074c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1075c5ba0be4SMike Smith 	return(NULL);
1076c5ba0be4SMike Smith     buf->Length = size;
1077c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1078c5ba0be4SMike Smith     return(buf);
1079c5ba0be4SMike Smith }
1080c5ba0be4SMike Smith 
1081c5ba0be4SMike Smith /*
1082c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1083c5ba0be4SMike Smith  */
1084c5ba0be4SMike Smith ACPI_STATUS
1085c5ba0be4SMike Smith acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number)
1086c5ba0be4SMike Smith {
1087c5ba0be4SMike Smith     ACPI_STATUS	error;
1088c5ba0be4SMike Smith     ACPI_BUFFER	buf;
1089c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
1090c5ba0be4SMike Smith 
1091cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1092cb9b0d80SMike Smith 
1093c5ba0be4SMike Smith     if (handle == NULL)
1094c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
10950c7da7acSJohn Baldwin 
10960c7da7acSJohn Baldwin     /*
10970c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
10980c7da7acSJohn Baldwin      * a method that will return an Integer.
10990c7da7acSJohn Baldwin      */
1100c5ba0be4SMike Smith     buf.Pointer = &param;
1101c5ba0be4SMike Smith     buf.Length = sizeof(param);
1102b53f2771SMike Smith     if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
1103c5ba0be4SMike Smith 	if (param.Type == ACPI_TYPE_INTEGER) {
1104c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1105c5ba0be4SMike Smith 	} else {
1106c5ba0be4SMike Smith 	    error = AE_TYPE;
1107c5ba0be4SMike Smith 	}
1108c5ba0be4SMike Smith     }
11090c7da7acSJohn Baldwin 
11100c7da7acSJohn Baldwin     /*
11110c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
11120c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
11130c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
11140c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
11150c7da7acSJohn Baldwin      *
11160c7da7acSJohn Baldwin      * This is a hack.
11170c7da7acSJohn Baldwin      */
11180c7da7acSJohn Baldwin     if (error == AE_BUFFER_OVERFLOW) {
1119b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
11200c7da7acSJohn Baldwin 	    error = AE_NO_MEMORY;
11210c7da7acSJohn Baldwin 	} else {
1122b53f2771SMike Smith 	    if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
1123c573e654SMitsuru IWASAKI 		error = acpi_ConvertBufferToInteger(&buf, number);
11240c7da7acSJohn Baldwin 	    }
11250c7da7acSJohn Baldwin 	}
11260c7da7acSJohn Baldwin 	AcpiOsFree(buf.Pointer);
11270c7da7acSJohn Baldwin     }
1128c5ba0be4SMike Smith     return(error);
1129c5ba0be4SMike Smith }
1130c5ba0be4SMike Smith 
1131c573e654SMitsuru IWASAKI ACPI_STATUS
1132c573e654SMitsuru IWASAKI acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number)
1133c573e654SMitsuru IWASAKI {
1134c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
1135c573e654SMitsuru IWASAKI     int		i;
1136c573e654SMitsuru IWASAKI 
1137c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
1138c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
1139c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
1140c573e654SMitsuru IWASAKI 	return(AE_OK);
1141c573e654SMitsuru IWASAKI     }
1142c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
1143c573e654SMitsuru IWASAKI 	return(AE_TYPE);
1144c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
1145c573e654SMitsuru IWASAKI 	return(AE_BAD_DATA);
1146c573e654SMitsuru IWASAKI     *number = 0;
1147c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
1148c573e654SMitsuru IWASAKI 	*number += (*(p->Buffer.Pointer + i) << (i * 8));
1149c573e654SMitsuru IWASAKI     return(AE_OK);
1150c573e654SMitsuru IWASAKI }
1151c573e654SMitsuru IWASAKI 
1152c5ba0be4SMike Smith /*
1153c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1154c5ba0be4SMike Smith  * function for each element.
1155c5ba0be4SMike Smith  *
1156c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1157c5ba0be4SMike Smith  */
1158c5ba0be4SMike Smith ACPI_STATUS
1159c5ba0be4SMike Smith acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg)
1160c5ba0be4SMike Smith {
1161c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1162c5ba0be4SMike Smith     int		i;
1163c5ba0be4SMike Smith 
1164c5ba0be4SMike Smith     if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE))
1165c5ba0be4SMike Smith 	return(AE_BAD_PARAMETER);
1166c5ba0be4SMike Smith 
1167c5ba0be4SMike Smith     /* iterate over components */
1168c5ba0be4SMike Smith     for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++)
1169c5ba0be4SMike Smith 	func(comp, arg);
1170c5ba0be4SMike Smith 
1171c5ba0be4SMike Smith     return(AE_OK);
1172c5ba0be4SMike Smith }
1173c5ba0be4SMike Smith 
11746f69255bSMike Smith /*
11756f69255bSMike Smith  * Find the (index)th resource object in a set.
11766f69255bSMike Smith  */
11776f69255bSMike Smith ACPI_STATUS
11786d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
11796f69255bSMike Smith {
11806d63101aSMike Smith     ACPI_RESOURCE	*rp;
11816f69255bSMike Smith     int			i;
11826f69255bSMike Smith 
11836d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
11846f69255bSMike Smith     i = index;
11856d63101aSMike Smith     while (i-- > 0) {
11866f69255bSMike Smith 	/* range check */
11876d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
11886f69255bSMike Smith 	    return(AE_BAD_PARAMETER);
11896d63101aSMike Smith 	/* check for terminator */
11906d63101aSMike Smith 	if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
11916d63101aSMike Smith 	    (rp->Length == 0))
11926d63101aSMike Smith 	    return(AE_NOT_FOUND);
11936d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
11946f69255bSMike Smith     }
11956f69255bSMike Smith     if (resp != NULL)
11966d63101aSMike Smith 	*resp = rp;
11976d63101aSMike Smith     return(AE_OK);
11986d63101aSMike Smith }
11996d63101aSMike Smith 
12006d63101aSMike Smith /*
12016d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
12026d63101aSMike Smith  *
12036d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
12046d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
12056d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
12066d63101aSMike Smith  * resources.
12076d63101aSMike Smith  */
12086d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
12096d63101aSMike Smith 
12106d63101aSMike Smith ACPI_STATUS
12116d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
12126d63101aSMike Smith {
12136d63101aSMike Smith     ACPI_RESOURCE	*rp;
12146d63101aSMike Smith     void		*newp;
12156d63101aSMike Smith 
12166d63101aSMike Smith     /*
12176d63101aSMike Smith      * Initialise the buffer if necessary.
12186d63101aSMike Smith      */
12196d63101aSMike Smith     if (buf->Pointer == NULL) {
12206d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
12216d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
12226d63101aSMike Smith 	    return(AE_NO_MEMORY);
12236d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
12246d63101aSMike Smith 	rp->Id = ACPI_RSTYPE_END_TAG;
12256d63101aSMike Smith 	rp->Length = 0;
12266d63101aSMike Smith     }
12276d63101aSMike Smith     if (res == NULL)
12286d63101aSMike Smith 	return(AE_OK);
12296d63101aSMike Smith 
12306d63101aSMike Smith     /*
12316d63101aSMike Smith      * Scan the current buffer looking for the terminator.
12326d63101aSMike Smith      * This will either find the terminator or hit the end
12336d63101aSMike Smith      * of the buffer and return an error.
12346d63101aSMike Smith      */
12356d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
12366d63101aSMike Smith     for (;;) {
12376d63101aSMike Smith 	/* range check, don't go outside the buffer */
12386d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
12396d63101aSMike Smith 	    return(AE_BAD_PARAMETER);
12406d63101aSMike Smith 	if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
12416d63101aSMike Smith 	    (rp->Length == 0)) {
12426d63101aSMike Smith 	    break;
12436d63101aSMike Smith 	}
12446d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
12456d63101aSMike Smith     }
12466d63101aSMike Smith 
12476d63101aSMike Smith     /*
12486d63101aSMike Smith      * Check the size of the buffer and expand if required.
12496d63101aSMike Smith      *
12506d63101aSMike Smith      * Required size is:
12516d63101aSMike Smith      *	size of existing resources before terminator +
12526d63101aSMike Smith      *	size of new resource and header +
12536d63101aSMike Smith      * 	size of terminator.
12546d63101aSMike Smith      *
12556d63101aSMike Smith      * Note that this loop should really only run once, unless
12566d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
12576d63101aSMike Smith      */
12586d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
12596d63101aSMike Smith 	    res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
12606d63101aSMike Smith 	    ACPI_RESOURCE_LENGTH) >= buf->Length) {
12616d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
12626d63101aSMike Smith 	    return(AE_NO_MEMORY);
12636d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
1264a692219dSMike Smith         rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1265a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
12666d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
12676d63101aSMike Smith 	buf->Pointer = newp;
12686d63101aSMike Smith 	buf->Length += buf->Length;
12696d63101aSMike Smith     }
12706d63101aSMike Smith 
12716d63101aSMike Smith     /*
12726d63101aSMike Smith      * Insert the new resource.
12736d63101aSMike Smith      */
12746d63101aSMike Smith     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
12756d63101aSMike Smith 
12766d63101aSMike Smith     /*
12776d63101aSMike Smith      * And add the terminator.
12786d63101aSMike Smith      */
12796d63101aSMike Smith     rp = ACPI_RESOURCE_NEXT(rp);
12806d63101aSMike Smith     rp->Id = ACPI_RSTYPE_END_TAG;
12816d63101aSMike Smith     rp->Length = 0;
12826d63101aSMike Smith 
12836f69255bSMike Smith     return(AE_OK);
12846f69255bSMike Smith }
1285c5ba0be4SMike Smith 
1286ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME	5
1287ece50487SMitsuru IWASAKI 
1288ece50487SMitsuru IWASAKI static void
1289ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
1290ece50487SMitsuru IWASAKI {
1291ece50487SMitsuru IWASAKI     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1292ece50487SMitsuru IWASAKI }
1293c30382dfSMitsuru IWASAKI 
129415e32d5dSMike Smith /*
129515e32d5dSMike Smith  * Set the system sleep state
129615e32d5dSMike Smith  *
129715e32d5dSMike Smith  * Currently we only support S1 and S5
129815e32d5dSMike Smith  */
129915e32d5dSMike Smith ACPI_STATUS
130015e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
130115e32d5dSMike Smith {
130215e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
130356d8cb57SMitsuru IWASAKI     UINT8	TypeA;
130456d8cb57SMitsuru IWASAKI     UINT8	TypeB;
130515e32d5dSMike Smith 
1306b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1307cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
13080ae55423SMike Smith 
13096397624dSMitsuru IWASAKI     if (sc->acpi_sstate != ACPI_STATE_S0)
13106397624dSMitsuru IWASAKI 	return_ACPI_STATUS(AE_BAD_PARAMETER);	/* avoid reentry */
13116397624dSMitsuru IWASAKI 
1312ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1313ece50487SMitsuru IWASAKI 	return_ACPI_STATUS(AE_OK);
1314ece50487SMitsuru IWASAKI 
131515e32d5dSMike Smith     switch (state) {
131615e32d5dSMike Smith     case ACPI_STATE_S0:	/* XXX only for testing */
1317b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
1318bfae45aaSMike Smith 	    device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
131915e32d5dSMike Smith 	}
132015e32d5dSMike Smith 	break;
132115e32d5dSMike Smith 
132215e32d5dSMike Smith     case ACPI_STATE_S1:
13236161544cSTakanori Watanabe     case ACPI_STATE_S2:
13246161544cSTakanori Watanabe     case ACPI_STATE_S3:
13256161544cSTakanori Watanabe     case ACPI_STATE_S4:
132698479b04SMitsuru IWASAKI 	if (ACPI_FAILURE(status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB))) {
132798479b04SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", AcpiFormatException(status));
132856d8cb57SMitsuru IWASAKI 	    break;
132956d8cb57SMitsuru IWASAKI 	}
133056d8cb57SMitsuru IWASAKI 
1331a1fccb47SMitsuru IWASAKI 	sc->acpi_sstate = state;
1332a1fccb47SMitsuru IWASAKI 	sc->acpi_sleep_disabled = 1;
1333a1fccb47SMitsuru IWASAKI 
133415e32d5dSMike Smith 	/*
133515e32d5dSMike Smith 	 * Inform all devices that we are going to sleep.
133615e32d5dSMike Smith 	 */
133715e32d5dSMike Smith 	if (DEVICE_SUSPEND(root_bus) != 0) {
133815e32d5dSMike Smith 	    /*
133915e32d5dSMike Smith 	     * Re-wake the system.
134015e32d5dSMike Smith 	     *
134115e32d5dSMike Smith 	     * XXX note that a better two-pass approach with a 'veto' pass
134215e32d5dSMike Smith 	     *     followed by a "real thing" pass would be better, but the
134315e32d5dSMike Smith 	     *     current bus interface does not provide for this.
134415e32d5dSMike Smith 	     */
134515e32d5dSMike Smith 	    DEVICE_RESUME(root_bus);
13460ae55423SMike Smith 	    return_ACPI_STATUS(AE_ERROR);
134715e32d5dSMike Smith 	}
1348b9c780d6SMitsuru IWASAKI 
1349b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(state))) {
1350b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1351b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
1352b9c780d6SMitsuru IWASAKI 	    break;
1353b9c780d6SMitsuru IWASAKI 	}
1354b9c780d6SMitsuru IWASAKI 
13556161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
13566161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
13576161544cSTakanori Watanabe 
1358a1fccb47SMitsuru IWASAKI 	    /* AcpiEnterSleepState() maybe incompleted, unlock here if locked. */
1359a1fccb47SMitsuru IWASAKI 	    if (AcpiGbl_AcpiMutexInfo[ACPI_MTX_HARDWARE].OwnerId != ACPI_MUTEX_NOT_ACQUIRED) {
13606161544cSTakanori Watanabe 		AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
1361a1fccb47SMitsuru IWASAKI 	    }
13626161544cSTakanori Watanabe 
13636161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1364964679ceSMitsuru IWASAKI 	    if (state == ACPI_STATE_S4) {
1365964679ceSMitsuru IWASAKI 		AcpiEnable();
13666161544cSTakanori Watanabe 	    }
13676161544cSTakanori Watanabe 	} else {
1368b53f2771SMike Smith 	    if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
1369bfae45aaSMike Smith 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
1370c30382dfSMitsuru IWASAKI 		break;
137115e32d5dSMike Smith 	    }
13726161544cSTakanori Watanabe 	}
1373ff741befSTakanori Watanabe 	AcpiLeaveSleepState((UINT8)state);
137415e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
137515e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
137613d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
137715e32d5dSMike Smith 	break;
137815e32d5dSMike Smith 
137915e32d5dSMike Smith     case ACPI_STATE_S5:
138015e32d5dSMike Smith 	/*
138115e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
138215e32d5dSMike Smith 	 * shutdown handlers.
138315e32d5dSMike Smith 	 */
138415e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
138515e32d5dSMike Smith 	break;
138615e32d5dSMike Smith 
138715e32d5dSMike Smith     default:
138815e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
138915e32d5dSMike Smith 	break;
139015e32d5dSMike Smith     }
1391ece50487SMitsuru IWASAKI 
1392ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1393ece50487SMitsuru IWASAKI 	timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1394ece50487SMitsuru IWASAKI 
13950ae55423SMike Smith     return_ACPI_STATUS(status);
139615e32d5dSMike Smith }
139715e32d5dSMike Smith 
139815e32d5dSMike Smith /*
139915e32d5dSMike Smith  * Enable/Disable ACPI
140015e32d5dSMike Smith  */
140115e32d5dSMike Smith ACPI_STATUS
140215e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
140315e32d5dSMike Smith {
140415e32d5dSMike Smith     ACPI_STATUS	status;
140515e32d5dSMike Smith     u_int32_t	flags;
140615e32d5dSMike Smith 
1407b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1408cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
14090ae55423SMike Smith 
141015e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
141115e32d5dSMike Smith             ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
141215e32d5dSMike Smith     if (!sc->acpi_enabled) {
141315e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
141415e32d5dSMike Smith     } else {
141515e32d5dSMike Smith 	status = AE_OK;
141615e32d5dSMike Smith     }
141715e32d5dSMike Smith     if (status == AE_OK)
141815e32d5dSMike Smith 	sc->acpi_enabled = 1;
14190ae55423SMike Smith     return_ACPI_STATUS(status);
142015e32d5dSMike Smith }
142115e32d5dSMike Smith 
142215e32d5dSMike Smith ACPI_STATUS
142315e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
142415e32d5dSMike Smith {
142515e32d5dSMike Smith     ACPI_STATUS	status;
142615e32d5dSMike Smith 
1427b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1428cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
14290ae55423SMike Smith 
143015e32d5dSMike Smith     if (sc->acpi_enabled) {
143115e32d5dSMike Smith 	status = AcpiDisable();
143215e32d5dSMike Smith     } else {
143315e32d5dSMike Smith 	status = AE_OK;
143415e32d5dSMike Smith     }
143515e32d5dSMike Smith     if (status == AE_OK)
143615e32d5dSMike Smith 	sc->acpi_enabled = 0;
14370ae55423SMike Smith     return_ACPI_STATUS(status);
143815e32d5dSMike Smith }
143915e32d5dSMike Smith 
144015e32d5dSMike Smith /*
144115e32d5dSMike Smith  * ACPI Event Handlers
144215e32d5dSMike Smith  */
144315e32d5dSMike Smith 
144415e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
144515e32d5dSMike Smith 
144615e32d5dSMike Smith static void
144715e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
144815e32d5dSMike Smith {
1449b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
145015e32d5dSMike Smith 
1451cb9b0d80SMike Smith     ACPI_LOCK;
1452a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
145315e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
1454cb9b0d80SMike Smith     ACPI_UNLOCK;
14550ae55423SMike Smith     return_VOID;
145615e32d5dSMike Smith }
145715e32d5dSMike Smith 
145815e32d5dSMike Smith static void
145915e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
146015e32d5dSMike Smith {
1461b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
14620ae55423SMike Smith 
146315e32d5dSMike Smith     /* Well, what to do? :-) */
14640ae55423SMike Smith 
1465cb9b0d80SMike Smith     ACPI_LOCK;
1466cb9b0d80SMike Smith     ACPI_UNLOCK;
1467cb9b0d80SMike Smith 
14680ae55423SMike Smith     return_VOID;
146915e32d5dSMike Smith }
147015e32d5dSMike Smith 
147115e32d5dSMike Smith /*
147215e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
147315e32d5dSMike Smith  */
147415e32d5dSMike Smith UINT32
147515e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context)
147615e32d5dSMike Smith {
147715e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
147815e32d5dSMike Smith 
1479b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
14800ae55423SMike Smith 
148115e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
14820ae55423SMike Smith 
1483b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
148415e32d5dSMike Smith }
148515e32d5dSMike Smith 
148615e32d5dSMike Smith UINT32
148715e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context)
148815e32d5dSMike Smith {
148915e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
149015e32d5dSMike Smith 
1491b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
14920ae55423SMike Smith 
149315e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
14940ae55423SMike Smith 
1495b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
149615e32d5dSMike Smith }
149715e32d5dSMike Smith 
149815e32d5dSMike Smith UINT32
149915e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context)
150015e32d5dSMike Smith {
150115e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
150215e32d5dSMike Smith 
1503b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15040ae55423SMike Smith 
150515e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
15060ae55423SMike Smith 
1507b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
150815e32d5dSMike Smith }
150915e32d5dSMike Smith 
151015e32d5dSMike Smith UINT32
151115e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context)
151215e32d5dSMike Smith {
151315e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
151415e32d5dSMike Smith 
1515b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15160ae55423SMike Smith 
151715e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
15180ae55423SMike Smith 
1519b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
152015e32d5dSMike Smith }
152115e32d5dSMike Smith 
152215e32d5dSMike Smith /*
152315e32d5dSMike Smith  * XXX This is kinda ugly, and should not be here.
152415e32d5dSMike Smith  */
152515e32d5dSMike Smith struct acpi_staticbuf {
152615e32d5dSMike Smith     ACPI_BUFFER	buffer;
152715e32d5dSMike Smith     char	data[512];
152815e32d5dSMike Smith };
152915e32d5dSMike Smith 
153015e32d5dSMike Smith char *
153115e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
153215e32d5dSMike Smith {
153315e32d5dSMike Smith     static struct acpi_staticbuf	buf;
153415e32d5dSMike Smith 
1535cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1536cb9b0d80SMike Smith 
153715e32d5dSMike Smith     buf.buffer.Length = 512;
153815e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
153915e32d5dSMike Smith 
1540b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
154115e32d5dSMike Smith 	return(buf.buffer.Pointer);
154215e32d5dSMike Smith     return("(unknown path)");
154315e32d5dSMike Smith }
154415e32d5dSMike Smith 
154515e32d5dSMike Smith /*
154615e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
154715e32d5dSMike Smith  * parts of the namespace.
154815e32d5dSMike Smith  */
154915e32d5dSMike Smith int
155015e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
155115e32d5dSMike Smith {
15523c600cfbSJohn Baldwin     char	*cp, *env, *np;
155315e32d5dSMike Smith     int		len;
155415e32d5dSMike Smith 
155515e32d5dSMike Smith     np = acpi_name(handle);
155615e32d5dSMike Smith     if (*np == '\\')
155715e32d5dSMike Smith 	np++;
15583c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
155915e32d5dSMike Smith 	return(0);
156015e32d5dSMike Smith 
156115e32d5dSMike Smith     /* scan the avoid list checking for a match */
15623c600cfbSJohn Baldwin     cp = env;
156315e32d5dSMike Smith     for (;;) {
156415e32d5dSMike Smith 	while ((*cp != 0) && isspace(*cp))
156515e32d5dSMike Smith 	    cp++;
156615e32d5dSMike Smith 	if (*cp == 0)
156715e32d5dSMike Smith 	    break;
156815e32d5dSMike Smith 	len = 0;
156915e32d5dSMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
157015e32d5dSMike Smith 	    len++;
1571d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
15723c600cfbSJohn Baldwin 	    freeenv(env);
15730ae55423SMike Smith 	    return(1);
1574d786139cSMaxime Henrion 	}
15750ae55423SMike Smith 	cp += len;
15760ae55423SMike Smith     }
15773c600cfbSJohn Baldwin     freeenv(env);
15780ae55423SMike Smith     return(0);
15790ae55423SMike Smith }
15800ae55423SMike Smith 
15810ae55423SMike Smith /*
15820ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
15830ae55423SMike Smith  */
15840ae55423SMike Smith int
15850ae55423SMike Smith acpi_disabled(char *subsys)
15860ae55423SMike Smith {
158778689b15SMaxime Henrion     char	*cp, *env;
15880ae55423SMike Smith     int		len;
15890ae55423SMike Smith 
159078689b15SMaxime Henrion     if ((env = getenv("debug.acpi.disable")) == NULL)
15910ae55423SMike Smith 	return(0);
159278689b15SMaxime Henrion     if (!strcmp(env, "all")) {
159378689b15SMaxime Henrion 	freeenv(env);
15940ae55423SMike Smith 	return(1);
1595d786139cSMaxime Henrion     }
15960ae55423SMike Smith 
15970ae55423SMike Smith     /* scan the disable list checking for a match */
159878689b15SMaxime Henrion     cp = env;
15990ae55423SMike Smith     for (;;) {
16000ae55423SMike Smith 	while ((*cp != 0) && isspace(*cp))
16010ae55423SMike Smith 	    cp++;
16020ae55423SMike Smith 	if (*cp == 0)
16030ae55423SMike Smith 	    break;
16040ae55423SMike Smith 	len = 0;
16050ae55423SMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
16060ae55423SMike Smith 	    len++;
1607d786139cSMaxime Henrion 	if (!strncmp(cp, subsys, len)) {
160878689b15SMaxime Henrion 	    freeenv(env);
160915e32d5dSMike Smith 	    return(1);
1610d786139cSMaxime Henrion 	}
161115e32d5dSMike Smith 	cp += len;
161215e32d5dSMike Smith     }
161378689b15SMaxime Henrion     freeenv(env);
161415e32d5dSMike Smith     return(0);
161515e32d5dSMike Smith }
161615e32d5dSMike Smith 
161715e32d5dSMike Smith /*
1618a1fccb47SMitsuru IWASAKI  * Device wake capability enable/disable.
1619a1fccb47SMitsuru IWASAKI  */
1620a1fccb47SMitsuru IWASAKI void
1621a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable)
1622a1fccb47SMitsuru IWASAKI {
1623a1fccb47SMitsuru IWASAKI     ACPI_OBJECT_LIST		ArgList;
1624a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			Arg;
1625a1fccb47SMitsuru IWASAKI 
1626a1fccb47SMitsuru IWASAKI     /*
1627a1fccb47SMitsuru IWASAKI      * TBD: All Power Resources referenced by elements 2 through N
1628a1fccb47SMitsuru IWASAKI      *      of the _PRW object are put into the ON state.
1629a1fccb47SMitsuru IWASAKI      */
1630a1fccb47SMitsuru IWASAKI 
1631a1fccb47SMitsuru IWASAKI     /*
1632a1fccb47SMitsuru IWASAKI      * enable/disable device wake function.
1633a1fccb47SMitsuru IWASAKI      */
1634a1fccb47SMitsuru IWASAKI 
1635a1fccb47SMitsuru IWASAKI     ArgList.Count = 1;
1636a1fccb47SMitsuru IWASAKI     ArgList.Pointer = &Arg;
1637a1fccb47SMitsuru IWASAKI 
1638a1fccb47SMitsuru IWASAKI     Arg.Type = ACPI_TYPE_INTEGER;
1639a1fccb47SMitsuru IWASAKI     Arg.Integer.Value = enable;
1640a1fccb47SMitsuru IWASAKI 
1641a1fccb47SMitsuru IWASAKI     (void)AcpiEvaluateObject(h, "_PSW", &ArgList, NULL);
1642a1fccb47SMitsuru IWASAKI }
1643a1fccb47SMitsuru IWASAKI 
1644a1fccb47SMitsuru IWASAKI void
1645a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_event(ACPI_HANDLE h)
1646a1fccb47SMitsuru IWASAKI {
1647a1fccb47SMitsuru IWASAKI     struct acpi_softc		*sc;
1648a1fccb47SMitsuru IWASAKI     ACPI_STATUS			status;
1649a1fccb47SMitsuru IWASAKI     ACPI_BUFFER			prw_buffer;
1650a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			*res;
1651a1fccb47SMitsuru IWASAKI 
1652a1fccb47SMitsuru IWASAKI     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1653a1fccb47SMitsuru IWASAKI 
1654a1fccb47SMitsuru IWASAKI     if ((sc = devclass_get_softc(acpi_devclass, 0)) == NULL) {
1655a1fccb47SMitsuru IWASAKI 	return;
1656a1fccb47SMitsuru IWASAKI     }
1657a1fccb47SMitsuru IWASAKI 
1658a1fccb47SMitsuru IWASAKI     /*
1659a1fccb47SMitsuru IWASAKI      * _PRW object is only required for devices that have the ability
1660a1fccb47SMitsuru IWASAKI      * to wake the system from a system sleeping state.
1661a1fccb47SMitsuru IWASAKI      */
1662a1fccb47SMitsuru IWASAKI     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
1663a1fccb47SMitsuru IWASAKI     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
1664a1fccb47SMitsuru IWASAKI     if (ACPI_FAILURE(status)) {
1665a1fccb47SMitsuru IWASAKI 	return;
1666a1fccb47SMitsuru IWASAKI     }
1667a1fccb47SMitsuru IWASAKI 
1668a1fccb47SMitsuru IWASAKI     res = (ACPI_OBJECT *)prw_buffer.Pointer;
1669a1fccb47SMitsuru IWASAKI     if (res == NULL) {
1670a1fccb47SMitsuru IWASAKI 	return;
1671a1fccb47SMitsuru IWASAKI     }
1672a1fccb47SMitsuru IWASAKI 
1673a1fccb47SMitsuru IWASAKI     if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) {
1674a1fccb47SMitsuru IWASAKI 	goto out;
1675a1fccb47SMitsuru IWASAKI     }
1676a1fccb47SMitsuru IWASAKI 
1677a1fccb47SMitsuru IWASAKI     /*
1678a1fccb47SMitsuru IWASAKI      * The element 1 of the _PRW object:
1679a1fccb47SMitsuru IWASAKI      * The lowest power system sleeping state that can be entered
1680a1fccb47SMitsuru IWASAKI      * while still providing wake functionality.
1681a1fccb47SMitsuru IWASAKI      * The sleeping state being entered must be greater or equal to
1682a1fccb47SMitsuru IWASAKI      * the power state declared in element 1 of the _PRW object.
1683a1fccb47SMitsuru IWASAKI      */
1684a1fccb47SMitsuru IWASAKI     if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER) {
1685a1fccb47SMitsuru IWASAKI 	goto out;
1686a1fccb47SMitsuru IWASAKI     }
1687a1fccb47SMitsuru IWASAKI 
1688a1fccb47SMitsuru IWASAKI     if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value) {
1689a1fccb47SMitsuru IWASAKI 	goto out;
1690a1fccb47SMitsuru IWASAKI     }
1691a1fccb47SMitsuru IWASAKI 
1692a1fccb47SMitsuru IWASAKI     /*
1693a1fccb47SMitsuru IWASAKI      * The element 0 of the _PRW object:
1694a1fccb47SMitsuru IWASAKI      */
1695a1fccb47SMitsuru IWASAKI     switch(res->Package.Elements[0].Type) {
1696a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_INTEGER:
1697a1fccb47SMitsuru IWASAKI 	/*
1698a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is numeric, then this
1699a1fccb47SMitsuru IWASAKI 	 * _PRW package element is the bit index in the GPEx_EN, in the
1700a1fccb47SMitsuru IWASAKI 	 * GPE blocks described in the FADT, of the enable bit that is
1701a1fccb47SMitsuru IWASAKI 	 * enabled for the wake event.
1702a1fccb47SMitsuru IWASAKI 	 */
1703a1fccb47SMitsuru IWASAKI 
1704a1fccb47SMitsuru IWASAKI 	status = AcpiEnableEvent(res->Package.Elements[0].Integer.Value,
1705a1fccb47SMitsuru IWASAKI 				 ACPI_EVENT_GPE, ACPI_EVENT_WAKE_ENABLE);
1706a1fccb47SMitsuru IWASAKI 	if (ACPI_FAILURE(status))
1707a1fccb47SMitsuru IWASAKI             printf("%s: EnableEvent Failed\n", __func__);
1708a1fccb47SMitsuru IWASAKI 	break;
1709a1fccb47SMitsuru IWASAKI 
1710a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_PACKAGE:
1711a1fccb47SMitsuru IWASAKI 	/* XXX TBD */
1712a1fccb47SMitsuru IWASAKI 
1713a1fccb47SMitsuru IWASAKI 	/*
1714a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is a package, then this
1715a1fccb47SMitsuru IWASAKI 	 * _PRW package element is itself a package containing two
1716a1fccb47SMitsuru IWASAKI 	 * elements. The first is an object reference to the GPE Block
1717a1fccb47SMitsuru IWASAKI 	 * device that contains the GPE that will be triggered by the wake
1718a1fccb47SMitsuru IWASAKI 	 * event. The second element is numeric and it contains the bit
1719a1fccb47SMitsuru IWASAKI 	 * index in the GPEx_EN, in the GPE Block referenced by the
1720a1fccb47SMitsuru IWASAKI 	 * first element in the package, of the enable bit that is enabled for
1721a1fccb47SMitsuru IWASAKI 	 * the wake event.
1722a1fccb47SMitsuru IWASAKI 	 * For example, if this field is a package then it is of the form:
1723a1fccb47SMitsuru IWASAKI 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
1724a1fccb47SMitsuru IWASAKI 	 */
1725a1fccb47SMitsuru IWASAKI 
1726a1fccb47SMitsuru IWASAKI 	break;
1727a1fccb47SMitsuru IWASAKI 
1728a1fccb47SMitsuru IWASAKI     default:
1729a1fccb47SMitsuru IWASAKI 	break;
1730a1fccb47SMitsuru IWASAKI     }
1731a1fccb47SMitsuru IWASAKI 
1732a1fccb47SMitsuru IWASAKI out:
1733a1fccb47SMitsuru IWASAKI     if (prw_buffer.Pointer != NULL)
1734a1fccb47SMitsuru IWASAKI 	AcpiOsFree(prw_buffer.Pointer);
1735a1fccb47SMitsuru IWASAKI     return;
1736a1fccb47SMitsuru IWASAKI }
1737a1fccb47SMitsuru IWASAKI 
1738a1fccb47SMitsuru IWASAKI /*
173915e32d5dSMike Smith  * Control interface.
174015e32d5dSMike Smith  *
17410ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
17420ae55423SMike Smith  * drivers wanting to be accessible via /dev/acpi should use the register/deregister
17430ae55423SMike Smith  * interface to make their handlers visible.
174415e32d5dSMike Smith  */
17450ae55423SMike Smith struct acpi_ioctl_hook
17460ae55423SMike Smith {
17470ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook)	link;
17480ae55423SMike Smith     u_long				cmd;
17490ae55423SMike Smith     int					(* fn)(u_long cmd, caddr_t addr, void *arg);
17500ae55423SMike Smith     void				*arg;
17510ae55423SMike Smith };
17520ae55423SMike Smith 
17530ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
17540ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
17550ae55423SMike Smith 
17560ae55423SMike Smith /*
17570ae55423SMike Smith  * Register an ioctl handler.
17580ae55423SMike Smith  */
17590ae55423SMike Smith int
17600ae55423SMike Smith acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg)
17610ae55423SMike Smith {
17620ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
17630ae55423SMike Smith 
17640ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
17650ae55423SMike Smith 	return(ENOMEM);
17660ae55423SMike Smith     hp->cmd = cmd;
17670ae55423SMike Smith     hp->fn = fn;
17680ae55423SMike Smith     hp->arg = arg;
17690ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
17700ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
17710ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
17720ae55423SMike Smith     }
17730ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
17740ae55423SMike Smith     return(0);
17750ae55423SMike Smith }
17760ae55423SMike Smith 
17770ae55423SMike Smith /*
17780ae55423SMike Smith  * Deregister an ioctl handler.
17790ae55423SMike Smith  */
17800ae55423SMike Smith void
17810ae55423SMike Smith acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg))
17820ae55423SMike Smith {
17830ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
17840ae55423SMike Smith 
17850ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
17860ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
17870ae55423SMike Smith 	    break;
17880ae55423SMike Smith 
17890ae55423SMike Smith     if (hp != NULL) {
17900ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
17910ae55423SMike Smith 	free(hp, M_ACPIDEV);
17920ae55423SMike Smith     }
17930ae55423SMike Smith }
17940ae55423SMike Smith 
179515e32d5dSMike Smith static int
1796b40ce416SJulian Elischer acpiopen(dev_t dev, int flag, int fmt, struct thread *td)
179715e32d5dSMike Smith {
179815e32d5dSMike Smith     return(0);
179915e32d5dSMike Smith }
180015e32d5dSMike Smith 
180115e32d5dSMike Smith static int
1802b40ce416SJulian Elischer acpiclose(dev_t dev, int flag, int fmt, struct thread *td)
180315e32d5dSMike Smith {
180415e32d5dSMike Smith     return(0);
180515e32d5dSMike Smith }
180615e32d5dSMike Smith 
180715e32d5dSMike Smith static int
1808b40ce416SJulian Elischer acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
180915e32d5dSMike Smith {
181015e32d5dSMike Smith     struct acpi_softc		*sc;
18110ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
18120ae55423SMike Smith     int				error, xerror, state;
181315e32d5dSMike Smith 
1814cb9b0d80SMike Smith     ACPI_LOCK;
1815cb9b0d80SMike Smith 
181615e32d5dSMike Smith     error = state = 0;
181715e32d5dSMike Smith     sc = dev->si_drv1;
181815e32d5dSMike Smith 
18190ae55423SMike Smith     /*
18200ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
18210ae55423SMike Smith      */
18220ae55423SMike Smith     if (acpi_ioctl_hooks_initted) {
18230ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
18240ae55423SMike Smith 	    if (hp->cmd == cmd) {
18250ae55423SMike Smith 		xerror = hp->fn(cmd, addr, hp->arg);
18260ae55423SMike Smith 		if (xerror != 0)
18270ae55423SMike Smith 		    error = xerror;
1828917d44c8SMitsuru IWASAKI 		goto out;
18290ae55423SMike Smith 	    }
18300ae55423SMike Smith 	}
18310ae55423SMike Smith     }
18320ae55423SMike Smith 
18330ae55423SMike Smith     /*
18340ae55423SMike Smith      * Core system ioctls.
18350ae55423SMike Smith      */
183615e32d5dSMike Smith     switch (cmd) {
183715e32d5dSMike Smith     case ACPIIO_ENABLE:
18380ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
183915e32d5dSMike Smith 	    error = ENXIO;
184015e32d5dSMike Smith 	break;
184115e32d5dSMike Smith 
184215e32d5dSMike Smith     case ACPIIO_DISABLE:
18430ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
184415e32d5dSMike Smith 	    error = ENXIO;
184515e32d5dSMike Smith 	break;
184615e32d5dSMike Smith 
184715e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
184815e32d5dSMike Smith 	if (!sc->acpi_enabled) {
184915e32d5dSMike Smith 	    error = ENXIO;
185015e32d5dSMike Smith 	    break;
185115e32d5dSMike Smith 	}
185215e32d5dSMike Smith 	state = *(int *)addr;
1853a5d1879bSMitsuru IWASAKI 	if (state >= ACPI_STATE_S0  && state <= ACPI_S_STATES_MAX) {
185415e32d5dSMike Smith 	    acpi_SetSleepState(sc, state);
185515e32d5dSMike Smith 	} else {
185615e32d5dSMike Smith 	    error = EINVAL;
185715e32d5dSMike Smith 	}
185815e32d5dSMike Smith 	break;
185915e32d5dSMike Smith 
186015e32d5dSMike Smith     default:
18610ae55423SMike Smith 	if (error == 0)
186215e32d5dSMike Smith 	    error = EINVAL;
186315e32d5dSMike Smith 	break;
186415e32d5dSMike Smith     }
1865917d44c8SMitsuru IWASAKI 
1866917d44c8SMitsuru IWASAKI out:
1867cb9b0d80SMike Smith     ACPI_UNLOCK;
186815e32d5dSMike Smith     return(error);
186915e32d5dSMike Smith }
187015e32d5dSMike Smith 
18711d073b1dSJohn Baldwin static int
18721d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
18731d073b1dSJohn Baldwin {
18741d073b1dSJohn Baldwin     char sleep_state[10];
18751d073b1dSJohn Baldwin     int error;
18761d073b1dSJohn Baldwin     u_int new_state, old_state;
18771d073b1dSJohn Baldwin 
18781d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
1879b8670bedSMitsuru IWASAKI     if (old_state > ACPI_S_STATES_MAX+1) {
18801d073b1dSJohn Baldwin 	strcpy(sleep_state, "unknown");
1881cb9b0d80SMike Smith     } else {
1882b8670bedSMitsuru IWASAKI 	bzero(sleep_state, sizeof(sleep_state));
18831d073b1dSJohn Baldwin 	strncpy(sleep_state, sleep_state_names[old_state],
18841d073b1dSJohn Baldwin 		sizeof(sleep_state_names[old_state]));
1885cb9b0d80SMike Smith     }
18861d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
18871d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
1888b8670bedSMitsuru IWASAKI 	for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX+1; new_state++) {
18891d073b1dSJohn Baldwin 	    if (strncmp(sleep_state, sleep_state_names[new_state],
18901d073b1dSJohn Baldwin 			sizeof(sleep_state)) == 0)
18911d073b1dSJohn Baldwin 		break;
1892cb9b0d80SMike Smith 	}
1893931a10c9SMitsuru IWASAKI 	if (new_state <= ACPI_S_STATES_MAX+1) {
1894931a10c9SMitsuru IWASAKI 	    if (new_state != old_state) {
18951d073b1dSJohn Baldwin 		*(u_int *)oidp->oid_arg1 = new_state;
1896931a10c9SMitsuru IWASAKI 	    }
1897cb9b0d80SMike Smith 	} else {
18981d073b1dSJohn Baldwin 	    error = EINVAL;
18991d073b1dSJohn Baldwin 	}
1900cb9b0d80SMike Smith     }
19011d073b1dSJohn Baldwin     return(error);
19021d073b1dSJohn Baldwin }
19031d073b1dSJohn Baldwin 
190415e32d5dSMike Smith #ifdef ACPI_DEBUG
19050ae55423SMike Smith /*
19060ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
19070ae55423SMike Smith  *
19080ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
19090ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
19100ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
19110ae55423SMike Smith  * prefixing the bit name with !.
19120ae55423SMike Smith  */
191315e32d5dSMike Smith struct debugtag
191415e32d5dSMike Smith {
191515e32d5dSMike Smith     char	*name;
191615e32d5dSMike Smith     UINT32	value;
191715e32d5dSMike Smith };
191815e32d5dSMike Smith 
191915e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
1920c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
1921c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
1922c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
1923c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
1924c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
1925c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
1926c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
1927c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
1928c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
19294c1cdee6SMike Smith     {"ACPI_DEBUGGER",		ACPI_DEBUGGER},
19304c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
19314c1cdee6SMike Smith 
1932cb9b0d80SMike Smith     {"ACPI_BUS",		ACPI_BUS},
19334c1cdee6SMike Smith     {"ACPI_SYSTEM",		ACPI_SYSTEM},
1934cb9b0d80SMike Smith     {"ACPI_POWER",		ACPI_POWER},
1935cb9b0d80SMike Smith     {"ACPI_EC", 		ACPI_EC},
1936c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
1937c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
1938c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
19394c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
1940cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
19414c1cdee6SMike Smith     {"ACPI_FAN",		ACPI_FAN},
19424c1cdee6SMike Smith 
1943b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
1944c5ba0be4SMike Smith     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
194515e32d5dSMike Smith     {NULL, 0}
194615e32d5dSMike Smith };
194715e32d5dSMike Smith 
194815e32d5dSMike Smith static struct debugtag dbg_level[] = {
19494c1cdee6SMike Smith     {"ACPI_LV_OK",		ACPI_LV_OK},
19504c1cdee6SMike Smith     {"ACPI_LV_INFO",		ACPI_LV_INFO},
19514c1cdee6SMike Smith     {"ACPI_LV_WARN",		ACPI_LV_WARN},
19524c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
19534c1cdee6SMike Smith     {"ACPI_LV_FATAL",		ACPI_LV_FATAL},
19544c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
19554c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
19564c1cdee6SMike Smith     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
19574c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
19584c1cdee6SMike Smith     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
19594c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
19604c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
19614c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
19624c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
19634c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
19644c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
19654c1cdee6SMike Smith     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
19664c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
19674c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
19684c1cdee6SMike Smith     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
19694c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
19704c1cdee6SMike Smith     {"ACPI_LV_IO",		ACPI_LV_IO},
19714c1cdee6SMike Smith     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
19724c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
19734c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
19744c1cdee6SMike Smith     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
19754c1cdee6SMike Smith     {"ACPI_LV_INIT",		ACPI_LV_INIT},
19764c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
197798479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
197898479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
197998479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
198098479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
198198479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
198215e32d5dSMike Smith     {NULL, 0}
198315e32d5dSMike Smith };
198415e32d5dSMike Smith 
198515e32d5dSMike Smith static void
198615e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
198715e32d5dSMike Smith {
198815e32d5dSMike Smith     char	*ep;
198915e32d5dSMike Smith     int		i, l;
19900ae55423SMike Smith     int		set;
199115e32d5dSMike Smith 
199215e32d5dSMike Smith     while (*cp) {
199315e32d5dSMike Smith 	if (isspace(*cp)) {
199415e32d5dSMike Smith 	    cp++;
199515e32d5dSMike Smith 	    continue;
199615e32d5dSMike Smith 	}
199715e32d5dSMike Smith 	ep = cp;
199815e32d5dSMike Smith 	while (*ep && !isspace(*ep))
199915e32d5dSMike Smith 	    ep++;
20000ae55423SMike Smith 	if (*cp == '!') {
20010ae55423SMike Smith 	    set = 0;
20020ae55423SMike Smith 	    cp++;
20030ae55423SMike Smith 	    if (cp == ep)
20040ae55423SMike Smith 		continue;
20050ae55423SMike Smith 	} else {
20060ae55423SMike Smith 	    set = 1;
20070ae55423SMike Smith 	}
200815e32d5dSMike Smith 	l = ep - cp;
200915e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
201015e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
20110ae55423SMike Smith 		if (set) {
201215e32d5dSMike Smith 		    *flag |= tag[i].value;
20130ae55423SMike Smith 		} else {
20140ae55423SMike Smith 		    *flag &= ~tag[i].value;
20150ae55423SMike Smith 		}
201615e32d5dSMike Smith 		printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
201715e32d5dSMike Smith 	    }
201815e32d5dSMike Smith 	}
201915e32d5dSMike Smith 	cp = ep;
202015e32d5dSMike Smith     }
202115e32d5dSMike Smith }
202215e32d5dSMike Smith 
202315e32d5dSMike Smith static void
20242a4ac806SMike Smith acpi_set_debugging(void *junk)
202515e32d5dSMike Smith {
202694aae282SMike Barcroft     char	*cp;
202715e32d5dSMike Smith 
20280ae55423SMike Smith     AcpiDbgLayer = 0;
20290ae55423SMike Smith     AcpiDbgLevel = 0;
203094aae282SMike Barcroft     if ((cp = getenv("debug.acpi.layer")) != NULL) {
203115e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
203294aae282SMike Barcroft 	freeenv(cp);
203394aae282SMike Barcroft     }
203494aae282SMike Barcroft     if ((cp = getenv("debug.acpi.level")) != NULL) {
203515e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
203694aae282SMike Barcroft 	freeenv(cp);
203794aae282SMike Barcroft     }
20380ae55423SMike Smith 
20390ae55423SMike Smith     printf("ACPI debug layer 0x%x  debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel);
204015e32d5dSMike Smith }
20412a4ac806SMike Smith SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL);
204215e32d5dSMike Smith #endif
2043f9390180SMitsuru IWASAKI 
2044f9390180SMitsuru IWASAKI static int
2045f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
2046f9390180SMitsuru IWASAKI {
2047f9390180SMitsuru IWASAKI 	int	state, acpi_state;
2048f9390180SMitsuru IWASAKI 	int	error;
2049f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
2050f9390180SMitsuru IWASAKI 	va_list	ap;
2051f9390180SMitsuru IWASAKI 
2052f9390180SMitsuru IWASAKI 	error = 0;
2053f9390180SMitsuru IWASAKI 	switch (cmd) {
2054f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
2055f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
2056f9390180SMitsuru IWASAKI 		if (sc == NULL) {
2057f9390180SMitsuru IWASAKI 			error = EINVAL;
2058f9390180SMitsuru IWASAKI 			goto out;
2059f9390180SMitsuru IWASAKI 		}
2060f9390180SMitsuru IWASAKI 
2061f9390180SMitsuru IWASAKI 		va_start(ap, arg);
2062f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
2063f9390180SMitsuru IWASAKI 		va_end(ap);
2064f9390180SMitsuru IWASAKI 
2065f9390180SMitsuru IWASAKI 		switch (state) {
2066f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
2067f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
2068f9390180SMitsuru IWASAKI 			break;
2069f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
2070f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
2071f9390180SMitsuru IWASAKI 			break;
2072f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
2073f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
2074f9390180SMitsuru IWASAKI 			break;
2075f9390180SMitsuru IWASAKI 		default:
2076f9390180SMitsuru IWASAKI 			error = EINVAL;
2077f9390180SMitsuru IWASAKI 			goto out;
2078f9390180SMitsuru IWASAKI 		}
2079f9390180SMitsuru IWASAKI 
2080f9390180SMitsuru IWASAKI 		acpi_SetSleepState(sc, acpi_state);
2081f9390180SMitsuru IWASAKI 		break;
2082f9390180SMitsuru IWASAKI 
2083f9390180SMitsuru IWASAKI 	default:
2084f9390180SMitsuru IWASAKI 		error = EINVAL;
2085f9390180SMitsuru IWASAKI 		goto out;
2086f9390180SMitsuru IWASAKI 	}
2087f9390180SMitsuru IWASAKI 
2088f9390180SMitsuru IWASAKI out:
2089f9390180SMitsuru IWASAKI 	return (error);
2090f9390180SMitsuru IWASAKI }
2091f9390180SMitsuru IWASAKI 
2092f9390180SMitsuru IWASAKI static void
2093f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
2094f9390180SMitsuru IWASAKI {
2095f9390180SMitsuru IWASAKI 
2096f9390180SMitsuru IWASAKI 	power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
2097f9390180SMitsuru IWASAKI }
2098f9390180SMitsuru IWASAKI 
2099f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
2100f9390180SMitsuru IWASAKI 
2101