xref: /freebsd/sys/dev/acpica/acpi.c (revision cb9b0d80fbc86819a0e5953b9722a0002f3875b0)
115e32d5dSMike Smith /*-
215e32d5dSMike Smith  * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org>
315e32d5dSMike Smith  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4cb9b0d80SMike Smith  * Copyright (c) 2000, 2001 Michael Smith
515e32d5dSMike Smith  * Copyright (c) 2000 BSDi
615e32d5dSMike Smith  * All rights reserved.
715e32d5dSMike Smith  *
815e32d5dSMike Smith  * Redistribution and use in source and binary forms, with or without
915e32d5dSMike Smith  * modification, are permitted provided that the following conditions
1015e32d5dSMike Smith  * are met:
1115e32d5dSMike Smith  * 1. Redistributions of source code must retain the above copyright
1215e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer.
1315e32d5dSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
1415e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer in the
1515e32d5dSMike Smith  *    documentation and/or other materials provided with the distribution.
1615e32d5dSMike Smith  *
1715e32d5dSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1815e32d5dSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1915e32d5dSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2015e32d5dSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2115e32d5dSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2215e32d5dSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2315e32d5dSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2415e32d5dSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2515e32d5dSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2615e32d5dSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2715e32d5dSMike Smith  * SUCH DAMAGE.
2815e32d5dSMike Smith  *
2915e32d5dSMike Smith  *	$FreeBSD$
3015e32d5dSMike Smith  */
3115e32d5dSMike Smith 
3215e32d5dSMike Smith #include "opt_acpi.h"
3315e32d5dSMike Smith #include <sys/param.h>
3415e32d5dSMike Smith #include <sys/kernel.h>
35fb919e4dSMark Murray #include <sys/proc.h>
36f34fa851SJohn Baldwin #include <sys/lock.h>
3715e32d5dSMike Smith #include <sys/malloc.h>
38b2c9c0daSMitsuru IWASAKI #include <sys/mutex.h>
3915e32d5dSMike Smith #include <sys/bus.h>
4015e32d5dSMike Smith #include <sys/conf.h>
4115e32d5dSMike Smith #include <sys/ioccom.h>
4215e32d5dSMike Smith #include <sys/reboot.h>
4315e32d5dSMike Smith #include <sys/sysctl.h>
4415e32d5dSMike Smith #include <sys/ctype.h>
4515e32d5dSMike Smith 
4615e32d5dSMike Smith #include <machine/clock.h>
4715e32d5dSMike Smith 
4815e32d5dSMike Smith #include <machine/resource.h>
4915e32d5dSMike Smith 
5015e32d5dSMike Smith #include "acpi.h"
5115e32d5dSMike Smith 
5215e32d5dSMike Smith #include <dev/acpica/acpivar.h>
5315e32d5dSMike Smith #include <dev/acpica/acpiio.h>
5415e32d5dSMike Smith 
5515e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
5615e32d5dSMike Smith 
5715e32d5dSMike Smith /*
580ae55423SMike Smith  * Hooks for the ACPI CA debugging infrastructure
590ae55423SMike Smith  */
60cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
610ae55423SMike Smith MODULE_NAME("ACPI")
620ae55423SMike Smith 
630ae55423SMike Smith /*
6415e32d5dSMike Smith  * Character device
6515e32d5dSMike Smith  */
6615e32d5dSMike Smith 
6715e32d5dSMike Smith static d_open_t		acpiopen;
6815e32d5dSMike Smith static d_close_t	acpiclose;
6915e32d5dSMike Smith static d_ioctl_t	acpiioctl;
7015e32d5dSMike Smith 
7115e32d5dSMike Smith #define CDEV_MAJOR 152
7215e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
7315e32d5dSMike Smith     acpiopen,
7415e32d5dSMike Smith     acpiclose,
7515e32d5dSMike Smith     noread,
7615e32d5dSMike Smith     nowrite,
7715e32d5dSMike Smith     acpiioctl,
7815e32d5dSMike Smith     nopoll,
7915e32d5dSMike Smith     nommap,
8015e32d5dSMike Smith     nostrategy,
8115e32d5dSMike Smith     "acpi",
8215e32d5dSMike Smith     CDEV_MAJOR,
8315e32d5dSMike Smith     nodump,
8415e32d5dSMike Smith     nopsize,
85606e1d68SJohn Baldwin     0
8615e32d5dSMike Smith };
8715e32d5dSMike Smith 
881d073b1dSJohn Baldwin static const char* sleep_state_names[] = {
89a5d1879bSMitsuru IWASAKI     "S0", "S1", "S2", "S3", "S4", "S5", "S4B" };
901d073b1dSJohn Baldwin 
912a4ac806SMike Smith /* this has to be static, as the softc is gone when we need it */
922a4ac806SMike Smith static int acpi_off_state = ACPI_STATE_S5;
932a4ac806SMike Smith 
94cb9b0d80SMike Smith struct mtx	acpi_mutex;
95cb9b0d80SMike Smith 
9615e32d5dSMike Smith static void	acpi_identify(driver_t *driver, device_t parent);
9715e32d5dSMike Smith static int	acpi_probe(device_t dev);
9815e32d5dSMike Smith static int	acpi_attach(device_t dev);
9915e32d5dSMike Smith static device_t	acpi_add_child(device_t bus, int order, const char *name, int unit);
10015e32d5dSMike Smith static int	acpi_print_resources(struct resource_list *rl, const char *name, int type,
10115e32d5dSMike Smith 				     const char *format);
10215e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
10315e32d5dSMike Smith static int	acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result);
10415e32d5dSMike Smith static int	acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value);
10515e32d5dSMike Smith static int	acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start,
10615e32d5dSMike Smith 				  u_long count);
10715e32d5dSMike Smith static int	acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp,
10815e32d5dSMike Smith 				  u_long *countp);
10915e32d5dSMike Smith static struct resource *acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
11015e32d5dSMike Smith 					    u_long start, u_long end, u_long count, u_int flags);
11115e32d5dSMike Smith static int	acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r);
11215e32d5dSMike Smith 
11315e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
11415e32d5dSMike Smith static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status);
11515e32d5dSMike Smith 
11615e32d5dSMike Smith static void	acpi_shutdown_pre_sync(void *arg, int howto);
11715e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
11815e32d5dSMike Smith 
11913d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
12013d4f7baSMitsuru IWASAKI 
12115e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
12215e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
1231d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
12415e32d5dSMike Smith 
12515e32d5dSMike Smith static device_method_t acpi_methods[] = {
12615e32d5dSMike Smith     /* Device interface */
12715e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
12815e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
12915e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
13015e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
13115e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
13215e32d5dSMike Smith     DEVMETHOD(device_resume,		bus_generic_resume),
13315e32d5dSMike Smith 
13415e32d5dSMike Smith     /* Bus interface */
13515e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
13615e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
13715e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
13815e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
13915e32d5dSMike Smith     DEVMETHOD(bus_set_resource,		acpi_set_resource),
14015e32d5dSMike Smith     DEVMETHOD(bus_get_resource,		acpi_get_resource),
14115e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
14215e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
14315e32d5dSMike Smith     DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
14415e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
14515e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
14615e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
14715e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
14815e32d5dSMike Smith 
14915e32d5dSMike Smith     {0, 0}
15015e32d5dSMike Smith };
15115e32d5dSMike Smith 
15215e32d5dSMike Smith static driver_t acpi_driver = {
15315e32d5dSMike Smith     "acpi",
15415e32d5dSMike Smith     acpi_methods,
15515e32d5dSMike Smith     sizeof(struct acpi_softc),
15615e32d5dSMike Smith };
15715e32d5dSMike Smith 
15815e32d5dSMike Smith devclass_t acpi_devclass;
15915e32d5dSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, 0, 0);
16015e32d5dSMike Smith 
16115e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, "");
16215e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, "");
16315e32d5dSMike Smith 
16415e32d5dSMike Smith /*
16515e32d5dSMike Smith  * Detect ACPI, perform early initialisation
16615e32d5dSMike Smith  */
16715e32d5dSMike Smith static void
16815e32d5dSMike Smith acpi_identify(driver_t *driver, device_t parent)
16915e32d5dSMike Smith {
17015e32d5dSMike Smith     device_t			child;
171042283a6SMike Smith     ACPI_PHYSICAL_ADDRESS	rsdp;
17215e32d5dSMike Smith     int				error;
17315e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
17415e32d5dSMike Smith     char			*debugpoint = getenv("debug.acpi.debugger");
17515e32d5dSMike Smith #endif
17615e32d5dSMike Smith 
1772a4ac806SMike Smith     FUNCTION_TRACE(__func__);
1780ae55423SMike Smith 
179840f9b53STakanori Watanabe     if(!cold){
180840f9b53STakanori Watanabe 	    printf("Don't load this driver from userland!!\n");
181840f9b53STakanori Watanabe 	    return ;
182840f9b53STakanori Watanabe     }
183840f9b53STakanori Watanabe 
18415e32d5dSMike Smith     /*
18515e32d5dSMike Smith      * Make sure we're not being doubly invoked.
18615e32d5dSMike Smith      */
18715e32d5dSMike Smith     if (device_find_child(parent, "acpi", 0) != NULL)
1880ae55423SMike Smith 	return_VOID;
18915e32d5dSMike Smith 
190cb9b0d80SMike Smith     /* initialise the ACPI mutex */
191cb9b0d80SMike Smith     mtx_init(&acpi_mutex, "ACPI global lock", MTX_DEF);
192cb9b0d80SMike Smith 
19315e32d5dSMike Smith     /*
1942a4ac806SMike Smith      * Start up the ACPI CA subsystem.
19515e32d5dSMike Smith      */
19615e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
19715e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "init"))
19815e32d5dSMike Smith 	acpi_EnterDebugger();
19915e32d5dSMike Smith #endif
20015e32d5dSMike Smith     if ((error = AcpiInitializeSubsystem()) != AE_OK) {
20115e32d5dSMike Smith 	printf("ACPI: initialisation failed: %s\n", acpi_strerror(error));
2020ae55423SMike Smith 	return_VOID;
20315e32d5dSMike Smith     }
20415e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
20515e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "tables"))
20615e32d5dSMike Smith 	acpi_EnterDebugger();
20715e32d5dSMike Smith #endif
20815e32d5dSMike Smith     if (((error = AcpiFindRootPointer(&rsdp)) != AE_OK) ||
20915e32d5dSMike Smith 	((error = AcpiLoadTables(rsdp)) != AE_OK)) {
21015e32d5dSMike Smith 	printf("ACPI: table load failed: %s\n", acpi_strerror(error));
2110ae55423SMike Smith 	return_VOID;
21215e32d5dSMike Smith     }
21315e32d5dSMike Smith 
21415e32d5dSMike Smith     /*
21515e32d5dSMike Smith      * Attach the actual ACPI device.
21615e32d5dSMike Smith      */
21715e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
21815e32d5dSMike Smith 	    device_printf(parent, "ACPI: could not attach\n");
2190ae55423SMike Smith 	    return_VOID;
22015e32d5dSMike Smith     }
22115e32d5dSMike Smith }
22215e32d5dSMike Smith 
22315e32d5dSMike Smith /*
22415e32d5dSMike Smith  * Fetch some descriptive data from ACPI to put in our attach message
22515e32d5dSMike Smith  */
22615e32d5dSMike Smith static int
22715e32d5dSMike Smith acpi_probe(device_t dev)
22815e32d5dSMike Smith {
22915e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
23015e32d5dSMike Smith     char		buf[20];
231cb9b0d80SMike Smith     ACPI_STATUS		status;
23215e32d5dSMike Smith     int			error;
23315e32d5dSMike Smith 
2342a4ac806SMike Smith     FUNCTION_TRACE(__func__);
235cb9b0d80SMike Smith     ACPI_LOCK;
2360ae55423SMike Smith 
237cb9b0d80SMike Smith     if ((status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th)) != AE_OK) {
238cb9b0d80SMike Smith 	device_printf(dev, "couldn't get XSDT header: %s\n", acpi_strerror(status));
239cb9b0d80SMike Smith 	error = ENXIO;
240cb9b0d80SMike Smith     } else {
24115e32d5dSMike Smith 	sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId);
24215e32d5dSMike Smith 	device_set_desc_copy(dev, buf);
243cb9b0d80SMike Smith 	error = 0;
244cb9b0d80SMike Smith     }
245cb9b0d80SMike Smith     ACPI_UNLOCK;
246cb9b0d80SMike Smith     return_VALUE(error);
24715e32d5dSMike Smith }
24815e32d5dSMike Smith 
24915e32d5dSMike Smith static int
25015e32d5dSMike Smith acpi_attach(device_t dev)
25115e32d5dSMike Smith {
25215e32d5dSMike Smith     struct acpi_softc	*sc;
253cb9b0d80SMike Smith     ACPI_STATUS		status;
25415e32d5dSMike Smith     int			error;
25515e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
25615e32d5dSMike Smith     char		*debugpoint = getenv("debug.acpi.debugger");
25715e32d5dSMike Smith #endif
25815e32d5dSMike Smith 
2592a4ac806SMike Smith     FUNCTION_TRACE(__func__);
260cb9b0d80SMike Smith     ACPI_LOCK;
26115e32d5dSMike Smith     sc = device_get_softc(dev);
26215e32d5dSMike Smith     bzero(sc, sizeof(*sc));
26315e32d5dSMike Smith     sc->acpi_dev = dev;
26415e32d5dSMike Smith 
26515e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
26615e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "spaces"))
26715e32d5dSMike Smith 	acpi_EnterDebugger();
26815e32d5dSMike Smith #endif
26915e32d5dSMike Smith 
27015e32d5dSMike Smith     /*
27115e32d5dSMike Smith      * Install the default address space handlers.
27215e32d5dSMike Smith      */
273cb9b0d80SMike Smith     error = ENXIO;
274cb9b0d80SMike Smith     if ((status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
2752a4ac806SMike Smith 						ACPI_ADR_SPACE_SYSTEM_MEMORY,
27615e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
27715e32d5dSMike Smith 						NULL, NULL)) != AE_OK) {
278cb9b0d80SMike Smith 	device_printf(dev, "could not initialise SystemMemory handler: %s\n", acpi_strerror(status));
279cb9b0d80SMike Smith 	goto out;
28015e32d5dSMike Smith     }
281cb9b0d80SMike Smith     if ((status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
2822a4ac806SMike Smith 						ACPI_ADR_SPACE_SYSTEM_IO,
28315e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
28415e32d5dSMike Smith 						NULL, NULL)) != AE_OK) {
285cb9b0d80SMike Smith 	device_printf(dev, "could not initialise SystemIO handler: %s\n", acpi_strerror(status));
286cb9b0d80SMike Smith 	goto out;
28715e32d5dSMike Smith     }
288cb9b0d80SMike Smith     if ((status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
2892a4ac806SMike Smith 						ACPI_ADR_SPACE_PCI_CONFIG,
29015e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
29115e32d5dSMike Smith 						NULL, NULL)) != AE_OK) {
292cb9b0d80SMike Smith 	device_printf(dev, "could not initialise PciConfig handler: %s\n", acpi_strerror(status));
293cb9b0d80SMike Smith 	goto out;
29415e32d5dSMike Smith     }
29515e32d5dSMike Smith 
29615e32d5dSMike Smith     /*
29715e32d5dSMike Smith      * Bring ACPI fully online.
29815e32d5dSMike Smith      *
29915e32d5dSMike Smith      * Note that we request that device _STA and _INI methods not be run (ACPI_NO_DEVICE_INIT)
30015e32d5dSMike Smith      * and the final object initialisation pass be skipped (ACPI_NO_OBJECT_INIT).
30115e32d5dSMike Smith      *
30215e32d5dSMike Smith      * XXX We need to arrange for the object init pass after we have attached all our
30315e32d5dSMike Smith      *     child devices.
30415e32d5dSMike Smith      */
30515e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
30615e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "enable"))
30715e32d5dSMike Smith 	acpi_EnterDebugger();
30815e32d5dSMike Smith #endif
309cb9b0d80SMike Smith     if ((status = AcpiEnableSubsystem(ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT)) != AE_OK) {
310cb9b0d80SMike Smith 	device_printf(dev, "could not enable ACPI: %s\n", acpi_strerror(status));
311cb9b0d80SMike Smith 	goto out;
31215e32d5dSMike Smith     }
31315e32d5dSMike Smith 
31415e32d5dSMike Smith     /*
3151d073b1dSJohn Baldwin      * Setup our sysctl tree.
3161d073b1dSJohn Baldwin      *
3171d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
3181d073b1dSJohn Baldwin      */
3191d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
3201d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
3211d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
3221d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
3231d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
3241d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
3251d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
3261d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
3271d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
3281d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
3291d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
3301d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
3311d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
3321d073b1dSJohn Baldwin 
3331d073b1dSJohn Baldwin     /*
33415e32d5dSMike Smith      * Dispatch the default sleep state to devices.
33515e32d5dSMike Smith      * TBD: should be configured from userland policy manager.
33615e32d5dSMike Smith      */
33715e32d5dSMike Smith     sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
33815e32d5dSMike Smith     sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
33915e32d5dSMike Smith     sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
34015e32d5dSMike Smith 
34113d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
34215e32d5dSMike Smith 
34315e32d5dSMike Smith     /*
34415e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
34515e32d5dSMike Smith      */
34615e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
34715e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "probe"))
34815e32d5dSMike Smith 	acpi_EnterDebugger();
34915e32d5dSMike Smith #endif
3500ae55423SMike Smith     if (!acpi_disabled("bus"))
35115e32d5dSMike Smith 	acpi_probe_children(dev);
35215e32d5dSMike Smith 
35315e32d5dSMike Smith     /*
35415e32d5dSMike Smith      * Register our shutdown handlers
35515e32d5dSMike Smith      */
35615e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST);
35715e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST);
35815e32d5dSMike Smith 
35915e32d5dSMike Smith     /*
36015e32d5dSMike Smith      * Register our acpi event handlers.
36115e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
36215e32d5dSMike Smith      */
36315e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST);
36415e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST);
36515e32d5dSMike Smith 
36615e32d5dSMike Smith     /*
36715e32d5dSMike Smith      * Flag our initial states.
36815e32d5dSMike Smith      */
36915e32d5dSMike Smith     sc->acpi_enabled = 1;
37015e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
37115e32d5dSMike Smith 
37215e32d5dSMike Smith     /*
37315e32d5dSMike Smith      * Create the control device
37415e32d5dSMike Smith      */
37515e32d5dSMike Smith     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, 0, 5, 0660, "acpi");
37615e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
37715e32d5dSMike Smith 
37815e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
37915e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "running"))
38015e32d5dSMike Smith 	acpi_EnterDebugger();
38115e32d5dSMike Smith #endif
382cb9b0d80SMike Smith     error = 0;
383cb9b0d80SMike Smith 
384cb9b0d80SMike Smith  out:
385cb9b0d80SMike Smith     ACPI_UNLOCK;
386cb9b0d80SMike Smith     return_VALUE(error);
38715e32d5dSMike Smith }
38815e32d5dSMike Smith 
38915e32d5dSMike Smith /*
39015e32d5dSMike Smith  * Handle a new device being added
39115e32d5dSMike Smith  */
39215e32d5dSMike Smith static device_t
39315e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
39415e32d5dSMike Smith {
39515e32d5dSMike Smith     struct acpi_device	*ad;
39615e32d5dSMike Smith     device_t		child;
39715e32d5dSMike Smith 
39815e32d5dSMike Smith     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL)
39915e32d5dSMike Smith 	return(NULL);
40015e32d5dSMike Smith     bzero(ad, sizeof(*ad));
40115e32d5dSMike Smith 
40215e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
40315e32d5dSMike Smith 
40415e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
40515e32d5dSMike Smith     if (child != NULL)
40615e32d5dSMike Smith 	device_set_ivars(child, ad);
40715e32d5dSMike Smith     return(child);
40815e32d5dSMike Smith }
40915e32d5dSMike Smith 
41015e32d5dSMike Smith /*
41115e32d5dSMike Smith  * Print child device resource usage
41215e32d5dSMike Smith  */
41315e32d5dSMike Smith static int
41415e32d5dSMike Smith acpi_print_resources(struct resource_list *rl, const char *name, int type, const char *format)
41515e32d5dSMike Smith {
41615e32d5dSMike Smith     struct resource_list_entry	*rle;
41715e32d5dSMike Smith     int				printed, retval;
41815e32d5dSMike Smith 
41915e32d5dSMike Smith     printed = 0;
42015e32d5dSMike Smith     retval = 0;
42115e32d5dSMike Smith 
42215e32d5dSMike Smith     if (!SLIST_FIRST(rl))
42315e32d5dSMike Smith 	return(0);
42415e32d5dSMike Smith 
42515e32d5dSMike Smith     /* Yes, this is kinda cheating */
42615e32d5dSMike Smith     SLIST_FOREACH(rle, rl, link) {
42715e32d5dSMike Smith 	if (rle->type == type) {
42815e32d5dSMike Smith 	    if (printed == 0)
42915e32d5dSMike Smith 		retval += printf(" %s ", name);
43015e32d5dSMike Smith 	    else if (printed > 0)
43115e32d5dSMike Smith 		retval += printf(",");
43215e32d5dSMike Smith 	    printed++;
43315e32d5dSMike Smith 	    retval += printf(format, rle->start);
43415e32d5dSMike Smith 	    if (rle->count > 1) {
43515e32d5dSMike Smith 		retval += printf("-");
43615e32d5dSMike Smith 		retval += printf(format, rle->start +
43715e32d5dSMike Smith 				 rle->count - 1);
43815e32d5dSMike Smith 	    }
43915e32d5dSMike Smith 	}
44015e32d5dSMike Smith     }
44115e32d5dSMike Smith     return(retval);
44215e32d5dSMike Smith }
44315e32d5dSMike Smith 
44415e32d5dSMike Smith static int
44515e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
44615e32d5dSMike Smith {
44715e32d5dSMike Smith     struct acpi_device		*adev = device_get_ivars(child);
44815e32d5dSMike Smith     struct resource_list	*rl = &adev->ad_rl;
44915e32d5dSMike Smith     int retval = 0;
45015e32d5dSMike Smith 
45115e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
45215e32d5dSMike Smith     retval += acpi_print_resources(rl, "port",  SYS_RES_IOPORT, "%#lx");
45315e32d5dSMike Smith     retval += acpi_print_resources(rl, "iomem", SYS_RES_MEMORY, "%#lx");
45415e32d5dSMike Smith     retval += acpi_print_resources(rl, "irq",   SYS_RES_IRQ,    "%ld");
45515e32d5dSMike Smith     retval += bus_print_child_footer(bus, child);
45615e32d5dSMike Smith 
45715e32d5dSMike Smith     return(retval);
45815e32d5dSMike Smith }
45915e32d5dSMike Smith 
46015e32d5dSMike Smith 
46115e32d5dSMike Smith /*
46215e32d5dSMike Smith  * Handle per-device ivars
46315e32d5dSMike Smith  */
46415e32d5dSMike Smith static int
46515e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
46615e32d5dSMike Smith {
46715e32d5dSMike Smith     struct acpi_device	*ad;
46815e32d5dSMike Smith 
46915e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
47015e32d5dSMike Smith 	printf("device has no ivars\n");
47115e32d5dSMike Smith 	return(ENOENT);
47215e32d5dSMike Smith     }
47315e32d5dSMike Smith 
47415e32d5dSMike Smith     switch(index) {
47515e32d5dSMike Smith 	/* ACPI ivars */
47615e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
47715e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
47815e32d5dSMike Smith 	break;
47915e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
48015e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
48115e32d5dSMike Smith 	break;
48215e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
48315e32d5dSMike Smith 	*(void **)result = ad->ad_private;
48415e32d5dSMike Smith 	break;
48515e32d5dSMike Smith 
48615e32d5dSMike Smith     default:
48715e32d5dSMike Smith 	panic("bad ivar read request (%d)\n", index);
48815e32d5dSMike Smith 	return(ENOENT);
48915e32d5dSMike Smith     }
49015e32d5dSMike Smith     return(0);
49115e32d5dSMike Smith }
49215e32d5dSMike Smith 
49315e32d5dSMike Smith static int
49415e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
49515e32d5dSMike Smith {
49615e32d5dSMike Smith     struct acpi_device	*ad;
49715e32d5dSMike Smith 
49815e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
49915e32d5dSMike Smith 	printf("device has no ivars\n");
50015e32d5dSMike Smith 	return(ENOENT);
50115e32d5dSMike Smith     }
50215e32d5dSMike Smith 
50315e32d5dSMike Smith     switch(index) {
50415e32d5dSMike Smith 	/* ACPI ivars */
50515e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
50615e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
50715e32d5dSMike Smith 	break;
50815e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
50915e32d5dSMike Smith 	ad->ad_magic = (int )value;
51015e32d5dSMike Smith 	break;
51115e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
51215e32d5dSMike Smith 	ad->ad_private = (void *)value;
51315e32d5dSMike Smith 	break;
51415e32d5dSMike Smith 
51515e32d5dSMike Smith     default:
51615e32d5dSMike Smith 	panic("bad ivar write request (%d)\n", index);
51715e32d5dSMike Smith 	return(ENOENT);
51815e32d5dSMike Smith     }
51915e32d5dSMike Smith     return(0);
52015e32d5dSMike Smith }
52115e32d5dSMike Smith 
52215e32d5dSMike Smith /*
52315e32d5dSMike Smith  * Handle child resource allocation/removal
52415e32d5dSMike Smith  */
52515e32d5dSMike Smith static int
52615e32d5dSMike Smith acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
52715e32d5dSMike Smith {
52815e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
52915e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
53015e32d5dSMike Smith 
53115e32d5dSMike Smith     resource_list_add(rl, type, rid, start, start + count -1, count);
53215e32d5dSMike Smith 
53315e32d5dSMike Smith     return(0);
53415e32d5dSMike Smith }
53515e32d5dSMike Smith 
53615e32d5dSMike Smith static int
53715e32d5dSMike Smith acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
53815e32d5dSMike Smith {
53915e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
54015e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
54115e32d5dSMike Smith     struct resource_list_entry	*rle;
54215e32d5dSMike Smith 
54315e32d5dSMike Smith     rle = resource_list_find(rl, type, rid);
54415e32d5dSMike Smith     if (!rle)
54515e32d5dSMike Smith 	return(ENOENT);
54615e32d5dSMike Smith 
54715e32d5dSMike Smith     if (startp)
54815e32d5dSMike Smith 	*startp = rle->start;
54915e32d5dSMike Smith     if (countp)
55015e32d5dSMike Smith 	*countp = rle->count;
55115e32d5dSMike Smith 
55215e32d5dSMike Smith     return(0);
55315e32d5dSMike Smith }
55415e32d5dSMike Smith 
55515e32d5dSMike Smith static struct resource *
55615e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
55715e32d5dSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
55815e32d5dSMike Smith {
55915e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
56015e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
56115e32d5dSMike Smith 
56215e32d5dSMike Smith     return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags));
56315e32d5dSMike Smith }
56415e32d5dSMike Smith 
56515e32d5dSMike Smith static int
56615e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
56715e32d5dSMike Smith {
56815e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
56915e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
57015e32d5dSMike Smith 
57115e32d5dSMike Smith     return(resource_list_release(rl, bus, child, type, rid, r));
57215e32d5dSMike Smith }
57315e32d5dSMike Smith 
57415e32d5dSMike Smith /*
57515e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
57615e32d5dSMike Smith  *
57715e32d5dSMike Smith  * Note that we only expect to find devices in the \_TZ_, \_SI_ and \_SB_ scopes,
57815e32d5dSMike Smith  * and \_TZ_ becomes obsolete in the ACPI 2.0 spec.
57915e32d5dSMike Smith  */
58015e32d5dSMike Smith static void
58115e32d5dSMike Smith acpi_probe_children(device_t bus)
58215e32d5dSMike Smith {
58315e32d5dSMike Smith     ACPI_HANDLE		parent;
58415e32d5dSMike Smith     static char		*scopes[] = {"\\_TZ_", "\\_SI", "\\_SB_", NULL};
58515e32d5dSMike Smith     int			i;
58615e32d5dSMike Smith 
5872a4ac806SMike Smith     FUNCTION_TRACE(__func__);
588cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
5890ae55423SMike Smith 
59015e32d5dSMike Smith     /*
59115e32d5dSMike Smith      * Create any static children by calling device identify methods.
59215e32d5dSMike Smith      */
5930ae55423SMike Smith     DEBUG_PRINT(TRACE_OBJECTS, ("device identify routines\n"));
59415e32d5dSMike Smith     bus_generic_probe(bus);
59515e32d5dSMike Smith 
59615e32d5dSMike Smith     /*
59715e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
59815e32d5dSMike Smith      * we find.
59915e32d5dSMike Smith      *
60015e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
60115e32d5dSMike Smith      * we want to create nodes for all devices, not just those that are currently
60215e32d5dSMike Smith      * present. (This assumes that we don't want to create/remove devices as they
60315e32d5dSMike Smith      * appear, which might be smarter.)
60415e32d5dSMike Smith      */
6050ae55423SMike Smith     DEBUG_PRINT(TRACE_OBJECTS, ("namespace scan\n"));
60615e32d5dSMike Smith     for (i = 0; scopes[i] != NULL; i++)
60715e32d5dSMike Smith 	if ((AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent)) == AE_OK)
60815e32d5dSMike Smith 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL);
60915e32d5dSMike Smith 
61015e32d5dSMike Smith     /*
61115e32d5dSMike Smith      * Scan all of the child devices we have created and let them probe/attach.
61215e32d5dSMike Smith      */
6130ae55423SMike Smith     DEBUG_PRINT(TRACE_OBJECTS, ("first bus_generic_attach\n"));
61415e32d5dSMike Smith     bus_generic_attach(bus);
61515e32d5dSMike Smith 
61615e32d5dSMike Smith     /*
61715e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
61815e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
61915e32d5dSMike Smith      */
6200ae55423SMike Smith     DEBUG_PRINT(TRACE_OBJECTS, ("second bus_generic_attach\n"));
62115e32d5dSMike Smith     bus_generic_attach(bus);
6220ae55423SMike Smith 
6230ae55423SMike Smith     return_VOID;
62415e32d5dSMike Smith }
62515e32d5dSMike Smith 
62615e32d5dSMike Smith /*
62715e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
62815e32d5dSMike Smith  * it.
62915e32d5dSMike Smith  */
63015e32d5dSMike Smith static ACPI_STATUS
63115e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
63215e32d5dSMike Smith {
63315e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
63415e32d5dSMike Smith     device_t		child, bus = (device_t)context;
63515e32d5dSMike Smith 
6362a4ac806SMike Smith     FUNCTION_TRACE(__func__);
6370ae55423SMike Smith 
6380ae55423SMike Smith     /*
6390ae55423SMike Smith      * Skip this device if we think we'll have trouble with it.
6400ae55423SMike Smith      */
6410ae55423SMike Smith     if (acpi_avoid(handle))
6420ae55423SMike Smith 	return_ACPI_STATUS(AE_OK);
6430ae55423SMike Smith 
64415e32d5dSMike Smith     if (AcpiGetType(handle, &type) == AE_OK) {
64515e32d5dSMike Smith 	switch(type) {
64615e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
64715e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
64815e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
64915e32d5dSMike Smith 	case ACPI_TYPE_POWER:
6500ae55423SMike Smith 	    if (acpi_disabled("children"))
6510ae55423SMike Smith 		break;
65215e32d5dSMike Smith 	    /*
65315e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
65415e32d5dSMike Smith 	     * so that the probe/attach passes will run breadth-first.
65515e32d5dSMike Smith 	     */
6560ae55423SMike Smith 	    DEBUG_PRINT(TRACE_OBJECTS, ("scanning '%s'\n", acpi_name(handle)))
65715e32d5dSMike Smith 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
65815e32d5dSMike Smith 	    acpi_set_handle(child, handle);
6590ae55423SMike Smith 	    DEBUG_EXEC(device_probe_and_attach(child));
6600ae55423SMike Smith 	    break;
66115e32d5dSMike Smith 	}
66215e32d5dSMike Smith     }
6630ae55423SMike Smith     return_ACPI_STATUS(AE_OK);
66415e32d5dSMike Smith }
66515e32d5dSMike Smith 
66615e32d5dSMike Smith static void
66715e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto)
66815e32d5dSMike Smith {
669cb9b0d80SMike Smith 
670cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
671cb9b0d80SMike Smith 
67215e32d5dSMike Smith     /*
6730ae55423SMike Smith      * Disable all ACPI events before soft off, otherwise the system
67415e32d5dSMike Smith      * will be turned on again on some laptops.
67515e32d5dSMike Smith      *
67615e32d5dSMike Smith      * XXX this should probably be restricted to masking some events just
67715e32d5dSMike Smith      *     before powering down, since we may still need ACPI during the
67815e32d5dSMike Smith      *     shutdown process.
67915e32d5dSMike Smith      */
68015e32d5dSMike Smith     acpi_Disable((struct acpi_softc *)arg);
68115e32d5dSMike Smith }
68215e32d5dSMike Smith 
68315e32d5dSMike Smith static void
68415e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
68515e32d5dSMike Smith {
68615e32d5dSMike Smith     ACPI_STATUS	status;
68715e32d5dSMike Smith 
688cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
689cb9b0d80SMike Smith 
6905f3e4175SMitsuru IWASAKI     if (howto & RB_POWEROFF) {
69115e32d5dSMike Smith 	printf("Power system off using ACPI...\n");
6922a4ac806SMike Smith 	if ((status = AcpiEnterSleepState(acpi_off_state)) != AE_OK) {
69315e32d5dSMike Smith 	    printf("ACPI power-off failed - %s\n", acpi_strerror(status));
69415e32d5dSMike Smith 	} else {
69515e32d5dSMike Smith 	    DELAY(1000000);
69615e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
69715e32d5dSMike Smith 	}
69815e32d5dSMike Smith     }
69915e32d5dSMike Smith }
70015e32d5dSMike Smith 
70113d4f7baSMitsuru IWASAKI static void
70213d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
70313d4f7baSMitsuru IWASAKI {
70413d4f7baSMitsuru IWASAKI     static int	first_time = 1;
70513d4f7baSMitsuru IWASAKI #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n"
70613d4f7baSMitsuru IWASAKI 
707cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
708cb9b0d80SMike Smith 
70913d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
710cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) {
71113d4f7baSMitsuru IWASAKI 	AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED);
71213d4f7baSMitsuru IWASAKI 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED);
71313d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
71413d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_power_button_for_sleep, sc);
71513d4f7baSMitsuru IWASAKI 	if (first_time) {
71613d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "power");
71713d4f7baSMitsuru IWASAKI 	}
71813d4f7baSMitsuru IWASAKI     }
719cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) {
72013d4f7baSMitsuru IWASAKI 	AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED);
72113d4f7baSMitsuru IWASAKI 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED);
72213d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
72313d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_sleep_button_for_sleep, sc);
72413d4f7baSMitsuru IWASAKI 	if (first_time) {
72513d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "sleep");
72613d4f7baSMitsuru IWASAKI 	}
72713d4f7baSMitsuru IWASAKI     }
72813d4f7baSMitsuru IWASAKI 
72913d4f7baSMitsuru IWASAKI     first_time = 0;
73013d4f7baSMitsuru IWASAKI }
73113d4f7baSMitsuru IWASAKI 
73215e32d5dSMike Smith /*
733c5ba0be4SMike Smith  * Returns true if the device is actually present and should
734c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
735c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
736c5ba0be4SMike Smith  */
737c5ba0be4SMike Smith BOOLEAN
738c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
739c5ba0be4SMike Smith {
740c5ba0be4SMike Smith     ACPI_HANDLE		h;
741c5ba0be4SMike Smith     ACPI_DEVICE_INFO	devinfo;
742c5ba0be4SMike Smith     ACPI_STATUS		error;
743c5ba0be4SMike Smith 
744cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
745cb9b0d80SMike Smith 
746c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
747c5ba0be4SMike Smith 	return(FALSE);
748c5ba0be4SMike Smith     if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK)
749c5ba0be4SMike Smith 	return(FALSE);
750c5ba0be4SMike Smith     if ((devinfo.Valid & ACPI_VALID_HID) && (devinfo.CurrentStatus & 0xf))
751c5ba0be4SMike Smith 	return(TRUE);
752c5ba0be4SMike Smith     return(FALSE);
753c5ba0be4SMike Smith }
754c5ba0be4SMike Smith 
755c5ba0be4SMike Smith /*
75615e32d5dSMike Smith  * Match a HID string against a device
75715e32d5dSMike Smith  */
75815e32d5dSMike Smith BOOLEAN
75915e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid)
76015e32d5dSMike Smith {
76115e32d5dSMike Smith     ACPI_HANDLE		h;
76215e32d5dSMike Smith     ACPI_DEVICE_INFO	devinfo;
76315e32d5dSMike Smith     ACPI_STATUS		error;
76415e32d5dSMike Smith 
765cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
766cb9b0d80SMike Smith 
7674d332989SMike Smith     if (hid == NULL)
76815e32d5dSMike Smith 	return(FALSE);
76915e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
77015e32d5dSMike Smith 	return(FALSE);
77115e32d5dSMike Smith     if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK)
77215e32d5dSMike Smith 	return(FALSE);
7734d332989SMike Smith     if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId))
77415e32d5dSMike Smith 	return(TRUE);
77515e32d5dSMike Smith     return(FALSE);
77615e32d5dSMike Smith }
77715e32d5dSMike Smith 
77815e32d5dSMike Smith /*
779c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
780c5ba0be4SMike Smith  * or one if its parents.
781c5ba0be4SMike Smith  */
782c5ba0be4SMike Smith ACPI_STATUS
783c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
784c5ba0be4SMike Smith {
785c5ba0be4SMike Smith     ACPI_HANDLE		r;
786c5ba0be4SMike Smith     ACPI_STATUS		status;
787c5ba0be4SMike Smith 
788cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
789cb9b0d80SMike Smith 
790c5ba0be4SMike Smith     /* walk back up the tree to the root */
791c5ba0be4SMike Smith     for (;;) {
792c5ba0be4SMike Smith 	status = AcpiGetHandle(parent, path, &r);
793c5ba0be4SMike Smith 	if (status == AE_OK) {
794c5ba0be4SMike Smith 	    *result = r;
795c5ba0be4SMike Smith 	    return(AE_OK);
796c5ba0be4SMike Smith 	}
797c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
798c5ba0be4SMike Smith 	    return(AE_OK);
799c5ba0be4SMike Smith 	if (AcpiGetParent(parent, &r) != AE_OK)
800c5ba0be4SMike Smith 	    return(AE_NOT_FOUND);
801c5ba0be4SMike Smith 	parent = r;
802c5ba0be4SMike Smith     }
803c5ba0be4SMike Smith }
804c5ba0be4SMike Smith 
805c5ba0be4SMike Smith /*
806c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
807c5ba0be4SMike Smith  */
808c5ba0be4SMike Smith ACPI_BUFFER *
809c5ba0be4SMike Smith acpi_AllocBuffer(int size)
810c5ba0be4SMike Smith {
811c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
812c5ba0be4SMike Smith 
813c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
814c5ba0be4SMike Smith 	return(NULL);
815c5ba0be4SMike Smith     buf->Length = size;
816c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
817c5ba0be4SMike Smith     return(buf);
818c5ba0be4SMike Smith }
819c5ba0be4SMike Smith 
820c5ba0be4SMike Smith /*
82115e32d5dSMike Smith  * Perform the tedious double-get procedure required for fetching something into
82215e32d5dSMike Smith  * an ACPI_BUFFER that has not been initialised.
82315e32d5dSMike Smith  */
82415e32d5dSMike Smith ACPI_STATUS
82515e32d5dSMike Smith acpi_GetIntoBuffer(ACPI_HANDLE handle, ACPI_STATUS (*func)(ACPI_HANDLE, ACPI_BUFFER *), ACPI_BUFFER *buf)
82615e32d5dSMike Smith {
82715e32d5dSMike Smith     ACPI_STATUS	status;
82815e32d5dSMike Smith 
829cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
830cb9b0d80SMike Smith 
83115e32d5dSMike Smith     buf->Length = 0;
83215e32d5dSMike Smith     buf->Pointer = NULL;
83315e32d5dSMike Smith 
83415e32d5dSMike Smith     if ((status = func(handle, buf)) != AE_BUFFER_OVERFLOW)
83515e32d5dSMike Smith 	return(status);
83615e32d5dSMike Smith     if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL)
83715e32d5dSMike Smith 	return(AE_NO_MEMORY);
83815e32d5dSMike Smith     return(func(handle, buf));
83915e32d5dSMike Smith }
84015e32d5dSMike Smith 
84115e32d5dSMike Smith /*
842c5ba0be4SMike Smith  * Perform the tedious double-evaluate procedure for evaluating something into
843c5ba0be4SMike Smith  * an ACPI_BUFFER that has not been initialised.  Note that this evaluates
844c5ba0be4SMike Smith  * twice, so avoid applying this to things that may have side-effects.
845c5ba0be4SMike Smith  *
846c5ba0be4SMike Smith  * This is like AcpiEvaluateObject with automatic buffer allocation.
84715e32d5dSMike Smith  */
848c5ba0be4SMike Smith ACPI_STATUS
849c5ba0be4SMike Smith acpi_EvaluateIntoBuffer(ACPI_HANDLE object, ACPI_STRING pathname, ACPI_OBJECT_LIST *params,
850c5ba0be4SMike Smith 			ACPI_BUFFER *buf)
85115e32d5dSMike Smith {
852c5ba0be4SMike Smith     ACPI_STATUS	status;
85315e32d5dSMike Smith 
854cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
855cb9b0d80SMike Smith 
856c5ba0be4SMike Smith     buf->Length = 0;
857c5ba0be4SMike Smith     buf->Pointer = NULL;
858c5ba0be4SMike Smith 
859c5ba0be4SMike Smith     if ((status = AcpiEvaluateObject(object, pathname, params, buf)) != AE_BUFFER_OVERFLOW)
860c5ba0be4SMike Smith 	return(status);
861c5ba0be4SMike Smith     if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL)
862c5ba0be4SMike Smith 	return(AE_NO_MEMORY);
863c5ba0be4SMike Smith     return(AcpiEvaluateObject(object, pathname, params, buf));
86415e32d5dSMike Smith }
86515e32d5dSMike Smith 
866c5ba0be4SMike Smith /*
867c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
868c5ba0be4SMike Smith  */
869c5ba0be4SMike Smith ACPI_STATUS
870c5ba0be4SMike Smith acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number)
871c5ba0be4SMike Smith {
872c5ba0be4SMike Smith     ACPI_STATUS	error;
873c5ba0be4SMike Smith     ACPI_BUFFER	buf;
874c5ba0be4SMike Smith     ACPI_OBJECT	param;
875c5ba0be4SMike Smith 
876cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
877cb9b0d80SMike Smith 
878c5ba0be4SMike Smith     if (handle == NULL)
879c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
880c5ba0be4SMike Smith     buf.Pointer = &param;
881c5ba0be4SMike Smith     buf.Length = sizeof(param);
882c5ba0be4SMike Smith     if ((error = AcpiEvaluateObject(handle, path, NULL, &buf)) == AE_OK) {
883c5ba0be4SMike Smith 	if (param.Type == ACPI_TYPE_INTEGER) {
884c5ba0be4SMike Smith 	    *number = param.Integer.Value;
885c5ba0be4SMike Smith 	} else {
886c5ba0be4SMike Smith 	    error = AE_TYPE;
887c5ba0be4SMike Smith 	}
888c5ba0be4SMike Smith     }
889c5ba0be4SMike Smith     return(error);
890c5ba0be4SMike Smith }
891c5ba0be4SMike Smith 
892c5ba0be4SMike Smith /*
893c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
894c5ba0be4SMike Smith  * function for each element.
895c5ba0be4SMike Smith  *
896c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
897c5ba0be4SMike Smith  */
898c5ba0be4SMike Smith ACPI_STATUS
899c5ba0be4SMike Smith acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg)
900c5ba0be4SMike Smith {
901c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
902c5ba0be4SMike Smith     int		i;
903c5ba0be4SMike Smith 
904c5ba0be4SMike Smith     if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE))
905c5ba0be4SMike Smith 	return(AE_BAD_PARAMETER);
906c5ba0be4SMike Smith 
907c5ba0be4SMike Smith     /* iterate over components */
908c5ba0be4SMike Smith     for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++)
909c5ba0be4SMike Smith 	func(comp, arg);
910c5ba0be4SMike Smith 
911c5ba0be4SMike Smith     return(AE_OK);
912c5ba0be4SMike Smith }
913c5ba0be4SMike Smith 
914c5ba0be4SMike Smith 
915c30382dfSMitsuru IWASAKI static ACPI_STATUS __inline
916c30382dfSMitsuru IWASAKI acpi_wakeup(UINT8 state)
917c30382dfSMitsuru IWASAKI {
918c30382dfSMitsuru IWASAKI     UINT16			Count;
919c30382dfSMitsuru IWASAKI     ACPI_STATUS		Status;
920c30382dfSMitsuru IWASAKI     ACPI_OBJECT_LIST	Arg_list;
921c30382dfSMitsuru IWASAKI     ACPI_OBJECT		Arg;
922c30382dfSMitsuru IWASAKI     ACPI_OBJECT		Objects[3]; /* package plus 2 number objects */
923c30382dfSMitsuru IWASAKI     ACPI_BUFFER		ReturnBuffer;
924c30382dfSMitsuru IWASAKI 
9252a4ac806SMike Smith     FUNCTION_TRACE_U32(__func__, state);
926cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
927c30382dfSMitsuru IWASAKI 
928c30382dfSMitsuru IWASAKI     /* wait for the WAK_STS bit */
929c30382dfSMitsuru IWASAKI     Count = 0;
930c30382dfSMitsuru IWASAKI     while (!(AcpiHwRegisterBitAccess(ACPI_READ, ACPI_MTX_LOCK, WAK_STS))) {
931c30382dfSMitsuru IWASAKI 	AcpiOsSleepUsec(1000);
932c30382dfSMitsuru IWASAKI 	/*
933c30382dfSMitsuru IWASAKI 	 * Some BIOSes don't set WAK_STS at all,
934c30382dfSMitsuru IWASAKI 	 * give up waiting for wakeup if we time out.
935c30382dfSMitsuru IWASAKI 	 */
936c30382dfSMitsuru IWASAKI 	if (Count > 1000) {
937cb9b0d80SMike Smith 	    printf("ACPI: timed out waiting for WAK_STS, continuing\n");
938c30382dfSMitsuru IWASAKI 	    break;	/* giving up */
939c30382dfSMitsuru IWASAKI 	}
940c30382dfSMitsuru IWASAKI 	Count++;
941c30382dfSMitsuru IWASAKI     }
942c30382dfSMitsuru IWASAKI 
943c30382dfSMitsuru IWASAKI     /*
944c30382dfSMitsuru IWASAKI      * Evaluate the _WAK method
945c30382dfSMitsuru IWASAKI      */
946cb9b0d80SMike Smith     bzero(&Arg_list, sizeof(Arg_list));
947c30382dfSMitsuru IWASAKI     Arg_list.Count = 1;
948c30382dfSMitsuru IWASAKI     Arg_list.Pointer = &Arg;
949c30382dfSMitsuru IWASAKI 
950cb9b0d80SMike Smith     bzero(&Arg, sizeof(Arg));
951c30382dfSMitsuru IWASAKI     Arg.Type = ACPI_TYPE_INTEGER;
952c30382dfSMitsuru IWASAKI     Arg.Integer.Value = state;
953c30382dfSMitsuru IWASAKI 
954cb9b0d80SMike Smith     /*
955cb9b0d80SMike Smith      * Set up _WAK result code buffer.
956cb9b0d80SMike Smith      *
957cb9b0d80SMike Smith      * XXX should use acpi_EvaluateIntoBuffer
958cb9b0d80SMike Smith      */
959cb9b0d80SMike Smith     bzero(Objects, sizeof(Objects));
960c30382dfSMitsuru IWASAKI     ReturnBuffer.Length = sizeof(Objects);
961c30382dfSMitsuru IWASAKI     ReturnBuffer.Pointer = Objects;
962c30382dfSMitsuru IWASAKI     AcpiEvaluateObject (NULL, "\\_WAK", &Arg_list, &ReturnBuffer);
963c30382dfSMitsuru IWASAKI 
964c30382dfSMitsuru IWASAKI     Status = AE_OK;
965cb9b0d80SMike Smith 
966c30382dfSMitsuru IWASAKI     /* Check result code for _WAK */
967c30382dfSMitsuru IWASAKI     if (Objects[0].Type != ACPI_TYPE_PACKAGE ||
968c30382dfSMitsuru IWASAKI 	Objects[1].Type != ACPI_TYPE_INTEGER  ||
969c30382dfSMitsuru IWASAKI 	Objects[2].Type != ACPI_TYPE_INTEGER) {
970c30382dfSMitsuru IWASAKI 	/*
971c30382dfSMitsuru IWASAKI 	 * In many BIOSes, _WAK doesn't return a result code.
972c30382dfSMitsuru IWASAKI 	 * We don't need to worry about it too much :-).
973c30382dfSMitsuru IWASAKI 	 */
974c30382dfSMitsuru IWASAKI 	DEBUG_PRINT(ACPI_INFO,
975c30382dfSMitsuru IWASAKI 		    ("acpi_wakeup: _WAK result code is corrupted, "
976c30382dfSMitsuru IWASAKI 		     "but should be OK.\n"));
977c30382dfSMitsuru IWASAKI     } else {
978c30382dfSMitsuru IWASAKI 	/* evaluate status code */
979c30382dfSMitsuru IWASAKI 	switch (Objects[1].Integer.Value) {
980c30382dfSMitsuru IWASAKI 	case 0x00000001:
981c30382dfSMitsuru IWASAKI 	    DEBUG_PRINT(ACPI_ERROR,
982c30382dfSMitsuru IWASAKI 			("acpi_wakeup: Wake was signaled "
983c30382dfSMitsuru IWASAKI 			 "but failed due to lack of power.\n"));
984c30382dfSMitsuru IWASAKI 	    Status = AE_ERROR;
985c30382dfSMitsuru IWASAKI 	    break;
986c30382dfSMitsuru IWASAKI 
987c30382dfSMitsuru IWASAKI 	case 0x00000002:
988c30382dfSMitsuru IWASAKI 	    DEBUG_PRINT(ACPI_ERROR,
989c30382dfSMitsuru IWASAKI 			("acpi_wakeup: Wake was signaled "
990c30382dfSMitsuru IWASAKI 			 "but failed due to thermal condition.\n"));
991c30382dfSMitsuru IWASAKI 	    Status = AE_ERROR;
992c30382dfSMitsuru IWASAKI 	    break;
993c30382dfSMitsuru IWASAKI 	}
994c30382dfSMitsuru IWASAKI 	/* evaluate PSS code */
995c30382dfSMitsuru IWASAKI 	if (Objects[2].Integer.Value == 0) {
996c30382dfSMitsuru IWASAKI 	    DEBUG_PRINT(ACPI_ERROR,
997c30382dfSMitsuru IWASAKI 			("acpi_wakeup: The targeted S-state "
998c30382dfSMitsuru IWASAKI 			 "was not entered because of too much current "
999c30382dfSMitsuru IWASAKI 			 "being drawn from the power supply.\n"));
1000c30382dfSMitsuru IWASAKI 	    Status = AE_ERROR;
1001c30382dfSMitsuru IWASAKI 	}
1002c30382dfSMitsuru IWASAKI     }
1003c30382dfSMitsuru IWASAKI     return_ACPI_STATUS(Status);
1004c30382dfSMitsuru IWASAKI }
1005c30382dfSMitsuru IWASAKI 
100615e32d5dSMike Smith /*
100715e32d5dSMike Smith  * Set the system sleep state
100815e32d5dSMike Smith  *
100915e32d5dSMike Smith  * Currently we only support S1 and S5
101015e32d5dSMike Smith  */
101115e32d5dSMike Smith ACPI_STATUS
101215e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
101315e32d5dSMike Smith {
101415e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
101515e32d5dSMike Smith 
10162a4ac806SMike Smith     FUNCTION_TRACE_U32(__func__, state);
1017cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
10180ae55423SMike Smith 
101915e32d5dSMike Smith     switch (state) {
102015e32d5dSMike Smith     case ACPI_STATE_S0:	/* XXX only for testing */
102191467fc6SMike Smith 	status = AcpiEnterSleepState((UINT8)state);
102215e32d5dSMike Smith 	if (status != AE_OK) {
102391467fc6SMike Smith 	    device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", acpi_strerror(status));
102415e32d5dSMike Smith 	}
102515e32d5dSMike Smith 	break;
102615e32d5dSMike Smith 
102715e32d5dSMike Smith     case ACPI_STATE_S1:
102815e32d5dSMike Smith 	/*
102915e32d5dSMike Smith 	 * Inform all devices that we are going to sleep.
103015e32d5dSMike Smith 	 */
103115e32d5dSMike Smith 	if (DEVICE_SUSPEND(root_bus) != 0) {
103215e32d5dSMike Smith 	    /*
103315e32d5dSMike Smith 	     * Re-wake the system.
103415e32d5dSMike Smith 	     *
103515e32d5dSMike Smith 	     * XXX note that a better two-pass approach with a 'veto' pass
103615e32d5dSMike Smith 	     *     followed by a "real thing" pass would be better, but the
103715e32d5dSMike Smith 	     *     current bus interface does not provide for this.
103815e32d5dSMike Smith 	     */
103915e32d5dSMike Smith 	    DEVICE_RESUME(root_bus);
10400ae55423SMike Smith 	    return_ACPI_STATUS(AE_ERROR);
104115e32d5dSMike Smith 	}
104215e32d5dSMike Smith 	sc->acpi_sstate = state;
104391467fc6SMike Smith 	status = AcpiEnterSleepState((UINT8)state);
104415e32d5dSMike Smith 	if (status != AE_OK) {
104591467fc6SMike Smith 	    device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", acpi_strerror(status));
1046c30382dfSMitsuru IWASAKI 	    break;
104715e32d5dSMike Smith 	}
1048c30382dfSMitsuru IWASAKI 	acpi_wakeup((UINT8)state);
104915e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
105015e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
105113d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
105215e32d5dSMike Smith 	break;
105315e32d5dSMike Smith 
10542a4ac806SMike Smith     case ACPI_STATE_S3:
10552a4ac806SMike Smith 	acpi_off_state = ACPI_STATE_S3;
10562a4ac806SMike Smith 	/* FALLTHROUGH */
105715e32d5dSMike Smith     case ACPI_STATE_S5:
105815e32d5dSMike Smith 	/*
105915e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
106015e32d5dSMike Smith 	 * shutdown handlers.
106115e32d5dSMike Smith 	 */
106215e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
106315e32d5dSMike Smith 	break;
106415e32d5dSMike Smith 
106515e32d5dSMike Smith     default:
106615e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
106715e32d5dSMike Smith 	break;
106815e32d5dSMike Smith     }
10690ae55423SMike Smith     return_ACPI_STATUS(status);
107015e32d5dSMike Smith }
107115e32d5dSMike Smith 
107215e32d5dSMike Smith /*
107315e32d5dSMike Smith  * Enable/Disable ACPI
107415e32d5dSMike Smith  */
107515e32d5dSMike Smith ACPI_STATUS
107615e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
107715e32d5dSMike Smith {
107815e32d5dSMike Smith     ACPI_STATUS	status;
107915e32d5dSMike Smith     u_int32_t	flags;
108015e32d5dSMike Smith 
10812a4ac806SMike Smith     FUNCTION_TRACE(__func__);
1082cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
10830ae55423SMike Smith 
108415e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
108515e32d5dSMike Smith             ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
108615e32d5dSMike Smith     if (!sc->acpi_enabled) {
108715e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
108815e32d5dSMike Smith     } else {
108915e32d5dSMike Smith 	status = AE_OK;
109015e32d5dSMike Smith     }
109115e32d5dSMike Smith     if (status == AE_OK)
109215e32d5dSMike Smith 	sc->acpi_enabled = 1;
10930ae55423SMike Smith     return_ACPI_STATUS(status);
109415e32d5dSMike Smith }
109515e32d5dSMike Smith 
109615e32d5dSMike Smith ACPI_STATUS
109715e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
109815e32d5dSMike Smith {
109915e32d5dSMike Smith     ACPI_STATUS	status;
110015e32d5dSMike Smith 
11012a4ac806SMike Smith     FUNCTION_TRACE(__func__);
1102cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
11030ae55423SMike Smith 
110415e32d5dSMike Smith     if (sc->acpi_enabled) {
110515e32d5dSMike Smith 	status = AcpiDisable();
110615e32d5dSMike Smith     } else {
110715e32d5dSMike Smith 	status = AE_OK;
110815e32d5dSMike Smith     }
110915e32d5dSMike Smith     if (status == AE_OK)
111015e32d5dSMike Smith 	sc->acpi_enabled = 0;
11110ae55423SMike Smith     return_ACPI_STATUS(status);
111215e32d5dSMike Smith }
111315e32d5dSMike Smith 
111415e32d5dSMike Smith /*
111515e32d5dSMike Smith  * ACPI Event Handlers
111615e32d5dSMike Smith  */
111715e32d5dSMike Smith 
111815e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
111915e32d5dSMike Smith 
112015e32d5dSMike Smith static void
112115e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
112215e32d5dSMike Smith {
11232a4ac806SMike Smith     FUNCTION_TRACE_U32(__func__, state);
112415e32d5dSMike Smith 
1125cb9b0d80SMike Smith     ACPI_LOCK;
1126a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
112715e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
1128cb9b0d80SMike Smith     ACPI_UNLOCK;
11290ae55423SMike Smith     return_VOID;
113015e32d5dSMike Smith }
113115e32d5dSMike Smith 
113215e32d5dSMike Smith static void
113315e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
113415e32d5dSMike Smith {
11352a4ac806SMike Smith     FUNCTION_TRACE_U32(__func__, state);
11360ae55423SMike Smith 
113715e32d5dSMike Smith     /* Well, what to do? :-) */
11380ae55423SMike Smith 
1139cb9b0d80SMike Smith     ACPI_LOCK;
1140cb9b0d80SMike Smith     ACPI_UNLOCK;
1141cb9b0d80SMike Smith 
11420ae55423SMike Smith     return_VOID;
114315e32d5dSMike Smith }
114415e32d5dSMike Smith 
114515e32d5dSMike Smith /*
114615e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
114715e32d5dSMike Smith  */
114815e32d5dSMike Smith UINT32
114915e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context)
115015e32d5dSMike Smith {
115115e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
115215e32d5dSMike Smith 
11532a4ac806SMike Smith     FUNCTION_TRACE(__func__);
11540ae55423SMike Smith 
115515e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
11560ae55423SMike Smith 
11570ae55423SMike Smith     return_VALUE(INTERRUPT_HANDLED);
115815e32d5dSMike Smith }
115915e32d5dSMike Smith 
116015e32d5dSMike Smith UINT32
116115e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context)
116215e32d5dSMike Smith {
116315e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
116415e32d5dSMike Smith 
11652a4ac806SMike Smith     FUNCTION_TRACE(__func__);
11660ae55423SMike Smith 
116715e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
11680ae55423SMike Smith 
11690ae55423SMike Smith     return_VALUE(INTERRUPT_HANDLED);
117015e32d5dSMike Smith }
117115e32d5dSMike Smith 
117215e32d5dSMike Smith UINT32
117315e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context)
117415e32d5dSMike Smith {
117515e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
117615e32d5dSMike Smith 
11772a4ac806SMike Smith     FUNCTION_TRACE(__func__);
11780ae55423SMike Smith 
117915e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
11800ae55423SMike Smith 
11810ae55423SMike Smith     return_VALUE(INTERRUPT_HANDLED);
118215e32d5dSMike Smith }
118315e32d5dSMike Smith 
118415e32d5dSMike Smith UINT32
118515e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context)
118615e32d5dSMike Smith {
118715e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
118815e32d5dSMike Smith 
11892a4ac806SMike Smith     FUNCTION_TRACE(__func__);
11900ae55423SMike Smith 
119115e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
11920ae55423SMike Smith 
11930ae55423SMike Smith     return_VALUE(INTERRUPT_HANDLED);
119415e32d5dSMike Smith }
119515e32d5dSMike Smith 
119615e32d5dSMike Smith /*
119715e32d5dSMike Smith  * XXX This is kinda ugly, and should not be here.
119815e32d5dSMike Smith  */
119915e32d5dSMike Smith struct acpi_staticbuf {
120015e32d5dSMike Smith     ACPI_BUFFER	buffer;
120115e32d5dSMike Smith     char	data[512];
120215e32d5dSMike Smith };
120315e32d5dSMike Smith 
120415e32d5dSMike Smith char *
120515e32d5dSMike Smith acpi_strerror(ACPI_STATUS excep)
120615e32d5dSMike Smith {
120715e32d5dSMike Smith     static struct acpi_staticbuf	buf;
120815e32d5dSMike Smith 
120915e32d5dSMike Smith     buf.buffer.Length = 512;
121015e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
121115e32d5dSMike Smith 
121215e32d5dSMike Smith     if (AcpiFormatException(excep, &buf.buffer) == AE_OK)
121315e32d5dSMike Smith 	return(buf.buffer.Pointer);
121415e32d5dSMike Smith     return("(error formatting exception)");
121515e32d5dSMike Smith }
121615e32d5dSMike Smith 
121715e32d5dSMike Smith char *
121815e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
121915e32d5dSMike Smith {
122015e32d5dSMike Smith     static struct acpi_staticbuf	buf;
122115e32d5dSMike Smith 
1222cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1223cb9b0d80SMike Smith 
122415e32d5dSMike Smith     buf.buffer.Length = 512;
122515e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
122615e32d5dSMike Smith 
122715e32d5dSMike Smith     if (AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer) == AE_OK)
122815e32d5dSMike Smith 	return(buf.buffer.Pointer);
122915e32d5dSMike Smith     return("(unknown path)");
123015e32d5dSMike Smith }
123115e32d5dSMike Smith 
123215e32d5dSMike Smith /*
123315e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
123415e32d5dSMike Smith  * parts of the namespace.
123515e32d5dSMike Smith  */
123615e32d5dSMike Smith int
123715e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
123815e32d5dSMike Smith {
123915e32d5dSMike Smith     char	*cp, *np;
124015e32d5dSMike Smith     int		len;
124115e32d5dSMike Smith 
124215e32d5dSMike Smith     np = acpi_name(handle);
124315e32d5dSMike Smith     if (*np == '\\')
124415e32d5dSMike Smith 	np++;
124515e32d5dSMike Smith     if ((cp = getenv("debug.acpi.avoid")) == NULL)
124615e32d5dSMike Smith 	return(0);
124715e32d5dSMike Smith 
124815e32d5dSMike Smith     /* scan the avoid list checking for a match */
124915e32d5dSMike Smith     for (;;) {
125015e32d5dSMike Smith 	while ((*cp != 0) && isspace(*cp))
125115e32d5dSMike Smith 	    cp++;
125215e32d5dSMike Smith 	if (*cp == 0)
125315e32d5dSMike Smith 	    break;
125415e32d5dSMike Smith 	len = 0;
125515e32d5dSMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
125615e32d5dSMike Smith 	    len++;
125715e32d5dSMike Smith 	if (!strncmp(cp, np, len)) {
12580ae55423SMike Smith 	    DEBUG_PRINT(TRACE_OBJECTS, ("avoiding '%s'\n", np));
12590ae55423SMike Smith 	    return(1);
12600ae55423SMike Smith 	}
12610ae55423SMike Smith 	cp += len;
12620ae55423SMike Smith     }
12630ae55423SMike Smith     return(0);
12640ae55423SMike Smith }
12650ae55423SMike Smith 
12660ae55423SMike Smith /*
12670ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
12680ae55423SMike Smith  */
12690ae55423SMike Smith int
12700ae55423SMike Smith acpi_disabled(char *subsys)
12710ae55423SMike Smith {
12720ae55423SMike Smith     char	*cp;
12730ae55423SMike Smith     int		len;
12740ae55423SMike Smith 
12750ae55423SMike Smith     if ((cp = getenv("debug.acpi.disable")) == NULL)
12760ae55423SMike Smith 	return(0);
12770ae55423SMike Smith     if (!strcmp(cp, "all"))
12780ae55423SMike Smith 	return(1);
12790ae55423SMike Smith 
12800ae55423SMike Smith     /* scan the disable list checking for a match */
12810ae55423SMike Smith     for (;;) {
12820ae55423SMike Smith 	while ((*cp != 0) && isspace(*cp))
12830ae55423SMike Smith 	    cp++;
12840ae55423SMike Smith 	if (*cp == 0)
12850ae55423SMike Smith 	    break;
12860ae55423SMike Smith 	len = 0;
12870ae55423SMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
12880ae55423SMike Smith 	    len++;
12890ae55423SMike Smith 	if (!strncmp(cp, subsys, len)) {
12900ae55423SMike Smith 	    DEBUG_PRINT(TRACE_OBJECTS, ("disabled '%s'\n", subsys));
129115e32d5dSMike Smith 	    return(1);
129215e32d5dSMike Smith 	}
129315e32d5dSMike Smith 	cp += len;
129415e32d5dSMike Smith     }
129515e32d5dSMike Smith     return(0);
129615e32d5dSMike Smith }
129715e32d5dSMike Smith 
129815e32d5dSMike Smith /*
129915e32d5dSMike Smith  * Control interface.
130015e32d5dSMike Smith  *
13010ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
13020ae55423SMike Smith  * drivers wanting to be accessible via /dev/acpi should use the register/deregister
13030ae55423SMike Smith  * interface to make their handlers visible.
130415e32d5dSMike Smith  */
13050ae55423SMike Smith struct acpi_ioctl_hook
13060ae55423SMike Smith {
13070ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook)	link;
13080ae55423SMike Smith     u_long				cmd;
13090ae55423SMike Smith     int					(* fn)(u_long cmd, caddr_t addr, void *arg);
13100ae55423SMike Smith     void				*arg;
13110ae55423SMike Smith };
13120ae55423SMike Smith 
13130ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
13140ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
13150ae55423SMike Smith 
13160ae55423SMike Smith /*
13170ae55423SMike Smith  * Register an ioctl handler.
13180ae55423SMike Smith  */
13190ae55423SMike Smith int
13200ae55423SMike Smith acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg)
13210ae55423SMike Smith {
13220ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
13230ae55423SMike Smith 
13240ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
13250ae55423SMike Smith 	return(ENOMEM);
13260ae55423SMike Smith     hp->cmd = cmd;
13270ae55423SMike Smith     hp->fn = fn;
13280ae55423SMike Smith     hp->arg = arg;
13290ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
13300ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
13310ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
13320ae55423SMike Smith     }
13330ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
13340ae55423SMike Smith     return(0);
13350ae55423SMike Smith }
13360ae55423SMike Smith 
13370ae55423SMike Smith /*
13380ae55423SMike Smith  * Deregister an ioctl handler.
13390ae55423SMike Smith  */
13400ae55423SMike Smith void
13410ae55423SMike Smith acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg))
13420ae55423SMike Smith {
13430ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
13440ae55423SMike Smith 
13450ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
13460ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
13470ae55423SMike Smith 	    break;
13480ae55423SMike Smith 
13490ae55423SMike Smith     if (hp != NULL) {
13500ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
13510ae55423SMike Smith 	free(hp, M_ACPIDEV);
13520ae55423SMike Smith     }
13530ae55423SMike Smith }
13540ae55423SMike Smith 
135515e32d5dSMike Smith static int
135615e32d5dSMike Smith acpiopen(dev_t dev, int flag, int fmt, struct proc *p)
135715e32d5dSMike Smith {
135815e32d5dSMike Smith     return(0);
135915e32d5dSMike Smith }
136015e32d5dSMike Smith 
136115e32d5dSMike Smith static int
136215e32d5dSMike Smith acpiclose(dev_t dev, int flag, int fmt, struct proc *p)
136315e32d5dSMike Smith {
136415e32d5dSMike Smith     return(0);
136515e32d5dSMike Smith }
136615e32d5dSMike Smith 
136715e32d5dSMike Smith static int
136815e32d5dSMike Smith acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
136915e32d5dSMike Smith {
137015e32d5dSMike Smith     struct acpi_softc		*sc;
13710ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
13720ae55423SMike Smith     int				error, xerror, state;
137315e32d5dSMike Smith 
1374cb9b0d80SMike Smith     ACPI_LOCK;
1375cb9b0d80SMike Smith 
137615e32d5dSMike Smith     error = state = 0;
137715e32d5dSMike Smith     sc = dev->si_drv1;
137815e32d5dSMike Smith 
13790ae55423SMike Smith     /*
13800ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
13810ae55423SMike Smith      */
13820ae55423SMike Smith     if (acpi_ioctl_hooks_initted) {
13830ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
13840ae55423SMike Smith 	    if (hp->cmd == cmd) {
13850ae55423SMike Smith 		xerror = hp->fn(cmd, addr, hp->arg);
13860ae55423SMike Smith 		if (xerror != 0)
13870ae55423SMike Smith 		    error = xerror;
1388917d44c8SMitsuru IWASAKI 		goto out;
13890ae55423SMike Smith 	    }
13900ae55423SMike Smith 	}
13910ae55423SMike Smith     }
13920ae55423SMike Smith 
13930ae55423SMike Smith     /*
13940ae55423SMike Smith      * Core system ioctls.
13950ae55423SMike Smith      */
139615e32d5dSMike Smith     switch (cmd) {
139715e32d5dSMike Smith     case ACPIIO_ENABLE:
13980ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
139915e32d5dSMike Smith 	    error = ENXIO;
140015e32d5dSMike Smith 	break;
140115e32d5dSMike Smith 
140215e32d5dSMike Smith     case ACPIIO_DISABLE:
14030ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
140415e32d5dSMike Smith 	    error = ENXIO;
140515e32d5dSMike Smith 	break;
140615e32d5dSMike Smith 
140715e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
140815e32d5dSMike Smith 	if (!sc->acpi_enabled) {
140915e32d5dSMike Smith 	    error = ENXIO;
141015e32d5dSMike Smith 	    break;
141115e32d5dSMike Smith 	}
141215e32d5dSMike Smith 	state = *(int *)addr;
1413a5d1879bSMitsuru IWASAKI 	if (state >= ACPI_STATE_S0  && state <= ACPI_S_STATES_MAX) {
141415e32d5dSMike Smith 	    acpi_SetSleepState(sc, state);
141515e32d5dSMike Smith 	} else {
141615e32d5dSMike Smith 	    error = EINVAL;
141715e32d5dSMike Smith 	}
141815e32d5dSMike Smith 	break;
141915e32d5dSMike Smith 
142015e32d5dSMike Smith     default:
14210ae55423SMike Smith 	if (error == 0)
142215e32d5dSMike Smith 	    error = EINVAL;
142315e32d5dSMike Smith 	break;
142415e32d5dSMike Smith     }
1425917d44c8SMitsuru IWASAKI 
1426917d44c8SMitsuru IWASAKI out:
1427cb9b0d80SMike Smith     ACPI_UNLOCK;
142815e32d5dSMike Smith     return(error);
142915e32d5dSMike Smith }
143015e32d5dSMike Smith 
14311d073b1dSJohn Baldwin static int
14321d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
14331d073b1dSJohn Baldwin {
14341d073b1dSJohn Baldwin     char sleep_state[10];
14351d073b1dSJohn Baldwin     int error;
14361d073b1dSJohn Baldwin     u_int new_state, old_state;
14371d073b1dSJohn Baldwin 
14381d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
1439cb9b0d80SMike Smith     if (old_state > ACPI_S_STATES_MAX) {
14401d073b1dSJohn Baldwin 	strcpy(sleep_state, "unknown");
1441cb9b0d80SMike Smith     } else {
14421d073b1dSJohn Baldwin 	strncpy(sleep_state, sleep_state_names[old_state],
14431d073b1dSJohn Baldwin 		sizeof(sleep_state_names[old_state]));
1444cb9b0d80SMike Smith     }
14451d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
14461d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
1447cb9b0d80SMike Smith 	for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX; new_state++) {
14481d073b1dSJohn Baldwin 	    if (strncmp(sleep_state, sleep_state_names[new_state],
14491d073b1dSJohn Baldwin 			sizeof(sleep_state)) == 0)
14501d073b1dSJohn Baldwin 		break;
1451cb9b0d80SMike Smith 	}
1452cb9b0d80SMike Smith 	if ((new_state != old_state) && (new_state <= ACPI_S_STATES_MAX)) {
14531d073b1dSJohn Baldwin 	    *(u_int *)oidp->oid_arg1 = new_state;
1454cb9b0d80SMike Smith 	} else {
14551d073b1dSJohn Baldwin 	    error = EINVAL;
14561d073b1dSJohn Baldwin 	}
1457cb9b0d80SMike Smith     }
14581d073b1dSJohn Baldwin     return(error);
14591d073b1dSJohn Baldwin }
14601d073b1dSJohn Baldwin 
146115e32d5dSMike Smith #ifdef ACPI_DEBUG
14620ae55423SMike Smith /*
14630ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
14640ae55423SMike Smith  *
14650ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
14660ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
14670ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
14680ae55423SMike Smith  * prefixing the bit name with !.
14690ae55423SMike Smith  */
147015e32d5dSMike Smith struct debugtag
147115e32d5dSMike Smith {
147215e32d5dSMike Smith     char	*name;
147315e32d5dSMike Smith     UINT32	value;
147415e32d5dSMike Smith };
147515e32d5dSMike Smith 
147615e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
1477c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
1478c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
1479c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
1480c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
1481c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
1482c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
1483c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
1484c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
1485c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
1486c5ba0be4SMike Smith     {"ACPI_POWER",		ACPI_POWER},
1487cb9b0d80SMike Smith     {"ACPI_BUS",		ACPI_BUS},
1488cb9b0d80SMike Smith     {"ACPI_POWER",		ACPI_POWER},
1489cb9b0d80SMike Smith     {"ACPI_EC", 		ACPI_EC},
1490cb9b0d80SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
1491c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
1492c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
1493c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
1494c5ba0be4SMike Smith     {"ACPI_SYSTEM",		ACPI_SYSTEM},
1495cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
1496c5ba0be4SMike Smith     {"ACPI_DEBUGGER",		ACPI_DEBUGGER},
1497c5ba0be4SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
1498c5ba0be4SMike Smith     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
149915e32d5dSMike Smith     {NULL, 0}
150015e32d5dSMike Smith };
150115e32d5dSMike Smith 
150215e32d5dSMike Smith static struct debugtag dbg_level[] = {
1503c5ba0be4SMike Smith     {"ACPI_OK",			ACPI_OK},
1504c5ba0be4SMike Smith     {"ACPI_INFO",		ACPI_INFO},
1505c5ba0be4SMike Smith     {"ACPI_WARN",		ACPI_WARN},
1506c5ba0be4SMike Smith     {"ACPI_ERROR",		ACPI_ERROR},
1507c5ba0be4SMike Smith     {"ACPI_FATAL",		ACPI_FATAL},
1508c5ba0be4SMike Smith     {"ACPI_DEBUG_OBJECT",	ACPI_DEBUG_OBJECT},
1509c5ba0be4SMike Smith     {"ACPI_ALL",		ACPI_ALL},
1510c5ba0be4SMike Smith     {"TRACE_THREADS",		TRACE_THREADS},
1511c5ba0be4SMike Smith     {"TRACE_PARSE",		TRACE_PARSE},
1512c5ba0be4SMike Smith     {"TRACE_DISPATCH",		TRACE_DISPATCH},
1513c5ba0be4SMike Smith     {"TRACE_LOAD",		TRACE_LOAD},
1514c5ba0be4SMike Smith     {"TRACE_EXEC",		TRACE_EXEC},
1515c5ba0be4SMike Smith     {"TRACE_NAMES",		TRACE_NAMES},
1516c5ba0be4SMike Smith     {"TRACE_OPREGION",		TRACE_OPREGION},
1517c5ba0be4SMike Smith     {"TRACE_BFIELD",		TRACE_BFIELD},
1518c5ba0be4SMike Smith     {"TRACE_TRASH",		TRACE_TRASH},
1519c5ba0be4SMike Smith     {"TRACE_TABLES",		TRACE_TABLES},
1520c5ba0be4SMike Smith     {"TRACE_FUNCTIONS",		TRACE_FUNCTIONS},
1521c5ba0be4SMike Smith     {"TRACE_VALUES",		TRACE_VALUES},
1522c5ba0be4SMike Smith     {"TRACE_OBJECTS",		TRACE_OBJECTS},
1523c5ba0be4SMike Smith     {"TRACE_ALLOCATIONS",	TRACE_ALLOCATIONS},
1524c5ba0be4SMike Smith     {"TRACE_RESOURCES",		TRACE_RESOURCES},
1525c5ba0be4SMike Smith     {"TRACE_IO",		TRACE_IO},
1526c5ba0be4SMike Smith     {"TRACE_INTERRUPTS",	TRACE_INTERRUPTS},
1527c5ba0be4SMike Smith     {"TRACE_USER_REQUESTS",	TRACE_USER_REQUESTS},
1528c5ba0be4SMike Smith     {"TRACE_PACKAGE",		TRACE_PACKAGE},
1529c5ba0be4SMike Smith     {"TRACE_MUTEX",		TRACE_MUTEX},
1530c5ba0be4SMike Smith     {"TRACE_INIT",		TRACE_INIT},
1531c5ba0be4SMike Smith     {"TRACE_ALL",		TRACE_ALL},
1532c5ba0be4SMike Smith     {"VERBOSE_AML_DISASSEMBLE",	VERBOSE_AML_DISASSEMBLE},
1533c5ba0be4SMike Smith     {"VERBOSE_INFO",		VERBOSE_INFO},
1534c5ba0be4SMike Smith     {"VERBOSE_TABLES",		VERBOSE_TABLES},
1535c5ba0be4SMike Smith     {"VERBOSE_EVENTS",		VERBOSE_EVENTS},
1536c5ba0be4SMike Smith     {"VERBOSE_ALL",		VERBOSE_ALL},
153715e32d5dSMike Smith     {NULL, 0}
153815e32d5dSMike Smith };
153915e32d5dSMike Smith 
154015e32d5dSMike Smith static void
154115e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
154215e32d5dSMike Smith {
154315e32d5dSMike Smith     char	*ep;
154415e32d5dSMike Smith     int		i, l;
15450ae55423SMike Smith     int		set;
154615e32d5dSMike Smith 
154715e32d5dSMike Smith     while (*cp) {
154815e32d5dSMike Smith 	if (isspace(*cp)) {
154915e32d5dSMike Smith 	    cp++;
155015e32d5dSMike Smith 	    continue;
155115e32d5dSMike Smith 	}
155215e32d5dSMike Smith 	ep = cp;
155315e32d5dSMike Smith 	while (*ep && !isspace(*ep))
155415e32d5dSMike Smith 	    ep++;
15550ae55423SMike Smith 	if (*cp == '!') {
15560ae55423SMike Smith 	    set = 0;
15570ae55423SMike Smith 	    cp++;
15580ae55423SMike Smith 	    if (cp == ep)
15590ae55423SMike Smith 		continue;
15600ae55423SMike Smith 	} else {
15610ae55423SMike Smith 	    set = 1;
15620ae55423SMike Smith 	}
156315e32d5dSMike Smith 	l = ep - cp;
156415e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
156515e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
15660ae55423SMike Smith 		if (set) {
156715e32d5dSMike Smith 		    *flag |= tag[i].value;
15680ae55423SMike Smith 		} else {
15690ae55423SMike Smith 		    *flag &= ~tag[i].value;
15700ae55423SMike Smith 		}
157115e32d5dSMike Smith 		printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
157215e32d5dSMike Smith 	    }
157315e32d5dSMike Smith 	}
157415e32d5dSMike Smith 	cp = ep;
157515e32d5dSMike Smith     }
157615e32d5dSMike Smith }
157715e32d5dSMike Smith 
157815e32d5dSMike Smith static void
15792a4ac806SMike Smith acpi_set_debugging(void *junk)
158015e32d5dSMike Smith {
158115e32d5dSMike Smith     char	*cp;
158215e32d5dSMike Smith 
15830ae55423SMike Smith     AcpiDbgLayer = 0;
15840ae55423SMike Smith     AcpiDbgLevel = 0;
158515e32d5dSMike Smith     if ((cp = getenv("debug.acpi.layer")) != NULL)
158615e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
158715e32d5dSMike Smith     if ((cp = getenv("debug.acpi.level")) != NULL)
158815e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
15890ae55423SMike Smith 
15900ae55423SMike Smith     printf("ACPI debug layer 0x%x  debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel);
159115e32d5dSMike Smith }
15922a4ac806SMike Smith SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL);
159315e32d5dSMike Smith #endif
15944eb77744SMitsuru IWASAKI 
15954eb77744SMitsuru IWASAKI /*
15964eb77744SMitsuru IWASAKI  * ACPI Battery Abstruction Layer
15974eb77744SMitsuru IWASAKI  */
15984eb77744SMitsuru IWASAKI 
15994eb77744SMitsuru IWASAKI struct acpi_batteries {
16004eb77744SMitsuru IWASAKI 	TAILQ_ENTRY(acpi_batteries) link;
16014eb77744SMitsuru IWASAKI 	struct	 acpi_battdesc battdesc;
16024eb77744SMitsuru IWASAKI };
16034eb77744SMitsuru IWASAKI 
16044eb77744SMitsuru IWASAKI static TAILQ_HEAD(,acpi_batteries) acpi_batteries;
16054eb77744SMitsuru IWASAKI static int			acpi_batteries_initted = 0;
16064eb77744SMitsuru IWASAKI static int			acpi_batteries_units = 0;
16074eb77744SMitsuru IWASAKI static struct acpi_battinfo	acpi_battery_battinfo;
16084eb77744SMitsuru IWASAKI 
16094eb77744SMitsuru IWASAKI static int
16104eb77744SMitsuru IWASAKI acpi_battery_get_units(void)
16114eb77744SMitsuru IWASAKI {
16124eb77744SMitsuru IWASAKI 
16134eb77744SMitsuru IWASAKI 	return (acpi_batteries_units);
16144eb77744SMitsuru IWASAKI }
16154eb77744SMitsuru IWASAKI 
16164eb77744SMitsuru IWASAKI static int
16174eb77744SMitsuru IWASAKI acpi_battery_get_battdesc(int logical_unit, struct acpi_battdesc *battdesc)
16184eb77744SMitsuru IWASAKI {
16194eb77744SMitsuru IWASAKI 	int	 i;
16204eb77744SMitsuru IWASAKI 	struct acpi_batteries	*bp;
16214eb77744SMitsuru IWASAKI 
16224eb77744SMitsuru IWASAKI 	if (logical_unit < 0 || logical_unit >= acpi_batteries_units) {
16234eb77744SMitsuru IWASAKI 		return (ENXIO);
16244eb77744SMitsuru IWASAKI 	}
16254eb77744SMitsuru IWASAKI 
16264eb77744SMitsuru IWASAKI 	i = 0;
16274eb77744SMitsuru IWASAKI 	TAILQ_FOREACH(bp, &acpi_batteries, link) {
16284eb77744SMitsuru IWASAKI 		if (logical_unit == i) {
16294eb77744SMitsuru IWASAKI 			battdesc->type = bp->battdesc.type;
16304eb77744SMitsuru IWASAKI 			battdesc->phys_unit = bp->battdesc.phys_unit;
16314eb77744SMitsuru IWASAKI 			return (0);
16324eb77744SMitsuru IWASAKI 		}
16334eb77744SMitsuru IWASAKI 		i++;
16344eb77744SMitsuru IWASAKI 	}
16354eb77744SMitsuru IWASAKI 
16364eb77744SMitsuru IWASAKI 	return (ENXIO);
16374eb77744SMitsuru IWASAKI }
16384eb77744SMitsuru IWASAKI 
16394eb77744SMitsuru IWASAKI static int
16404eb77744SMitsuru IWASAKI acpi_battery_get_battinfo(int unit, struct acpi_battinfo *battinfo)
16414eb77744SMitsuru IWASAKI {
16424eb77744SMitsuru IWASAKI 	int	 error;
16434eb77744SMitsuru IWASAKI 	struct	 acpi_battdesc battdesc;
16444eb77744SMitsuru IWASAKI 
16454eb77744SMitsuru IWASAKI 	error = 0;
16464eb77744SMitsuru IWASAKI 	if (unit == -1) {
16474eb77744SMitsuru IWASAKI 		error = acpi_cmbat_get_battinfo(-1, battinfo);
16484eb77744SMitsuru IWASAKI 		goto out;
16494eb77744SMitsuru IWASAKI 	} else {
16504eb77744SMitsuru IWASAKI 		if ((error = acpi_battery_get_battdesc(unit, &battdesc)) != 0) {
16514eb77744SMitsuru IWASAKI 			goto out;
16524eb77744SMitsuru IWASAKI 		}
16534eb77744SMitsuru IWASAKI 		switch (battdesc.type) {
16544eb77744SMitsuru IWASAKI 		case ACPI_BATT_TYPE_CMBAT:
16554eb77744SMitsuru IWASAKI 			error = acpi_cmbat_get_battinfo(battdesc.phys_unit,
16564eb77744SMitsuru IWASAKI 			   battinfo);
16574eb77744SMitsuru IWASAKI 			break;
16584eb77744SMitsuru IWASAKI 		default:
16594eb77744SMitsuru IWASAKI 			error = ENXIO;
16604eb77744SMitsuru IWASAKI 			break;
16614eb77744SMitsuru IWASAKI 		}
16624eb77744SMitsuru IWASAKI 	}
16634eb77744SMitsuru IWASAKI out:
16644eb77744SMitsuru IWASAKI 	return (error);
16654eb77744SMitsuru IWASAKI }
16664eb77744SMitsuru IWASAKI 
16674eb77744SMitsuru IWASAKI static int
16684eb77744SMitsuru IWASAKI acpi_battery_ioctl(u_long cmd, caddr_t addr, void *arg)
16694eb77744SMitsuru IWASAKI {
16704eb77744SMitsuru IWASAKI 	int	 error;
16714eb77744SMitsuru IWASAKI 	int	 logical_unit;
16724eb77744SMitsuru IWASAKI 	union acpi_battery_ioctl_arg	*ioctl_arg;
16734eb77744SMitsuru IWASAKI 
16744eb77744SMitsuru IWASAKI 	ioctl_arg = (union acpi_battery_ioctl_arg *)addr;
16754eb77744SMitsuru IWASAKI 	error = 0;
16764eb77744SMitsuru IWASAKI 	switch (cmd) {
16774eb77744SMitsuru IWASAKI 	case ACPIIO_BATT_GET_UNITS:
16784eb77744SMitsuru IWASAKI 		*(int *)addr = acpi_battery_get_units();
16794eb77744SMitsuru IWASAKI 		break;
16804eb77744SMitsuru IWASAKI 
16814eb77744SMitsuru IWASAKI 	case ACPIIO_BATT_GET_BATTDESC:
16824eb77744SMitsuru IWASAKI 		logical_unit = ioctl_arg->unit;
16834eb77744SMitsuru IWASAKI 		error = acpi_battery_get_battdesc(logical_unit, &ioctl_arg->battdesc);
16844eb77744SMitsuru IWASAKI 		break;
16854eb77744SMitsuru IWASAKI 
16864eb77744SMitsuru IWASAKI 	case ACPIIO_BATT_GET_BATTINFO:
16874eb77744SMitsuru IWASAKI 		logical_unit = ioctl_arg->unit;
16884eb77744SMitsuru IWASAKI 		error = acpi_battery_get_battinfo(logical_unit,
16894eb77744SMitsuru IWASAKI 		    &ioctl_arg->battinfo);
16904eb77744SMitsuru IWASAKI 		break;
16914eb77744SMitsuru IWASAKI 
16924eb77744SMitsuru IWASAKI 	default:
16934eb77744SMitsuru IWASAKI 		error = EINVAL;
16944eb77744SMitsuru IWASAKI 		break;
16954eb77744SMitsuru IWASAKI 	}
16964eb77744SMitsuru IWASAKI 
16974eb77744SMitsuru IWASAKI 	return (error);
16984eb77744SMitsuru IWASAKI }
16994eb77744SMitsuru IWASAKI 
17004eb77744SMitsuru IWASAKI static int
17014eb77744SMitsuru IWASAKI acpi_battery_sysctl(SYSCTL_HANDLER_ARGS)
17024eb77744SMitsuru IWASAKI {
17034eb77744SMitsuru IWASAKI 	int	val;
17044eb77744SMitsuru IWASAKI 	int	error;
17054eb77744SMitsuru IWASAKI 
17064eb77744SMitsuru IWASAKI 	acpi_battery_get_battinfo(-1, &acpi_battery_battinfo);
17074eb77744SMitsuru IWASAKI 	val = *(u_int *)oidp->oid_arg1;
17084eb77744SMitsuru IWASAKI 	error = sysctl_handle_int(oidp, &val, 0, req);
17094eb77744SMitsuru IWASAKI 	return (error);
17104eb77744SMitsuru IWASAKI }
17114eb77744SMitsuru IWASAKI 
17124eb77744SMitsuru IWASAKI static int
17134eb77744SMitsuru IWASAKI acpi_battery_init(void)
17144eb77744SMitsuru IWASAKI {
17154eb77744SMitsuru IWASAKI 	device_t		 dev;
17164eb77744SMitsuru IWASAKI 	struct acpi_softc	*sc;
17174eb77744SMitsuru IWASAKI 	int	 		 error;
17184eb77744SMitsuru IWASAKI 
17194eb77744SMitsuru IWASAKI 	if ((dev = devclass_get_device(acpi_devclass, 0)) == NULL) {
17204eb77744SMitsuru IWASAKI 		return (ENXIO);
17214eb77744SMitsuru IWASAKI 	}
17224eb77744SMitsuru IWASAKI 	if ((sc = device_get_softc(dev)) == NULL) {
17234eb77744SMitsuru IWASAKI 		return (ENXIO);
17244eb77744SMitsuru IWASAKI 	}
17254eb77744SMitsuru IWASAKI 
17264eb77744SMitsuru IWASAKI 	error = 0;
17274eb77744SMitsuru IWASAKI 
17284eb77744SMitsuru IWASAKI 	TAILQ_INIT(&acpi_batteries);
17294eb77744SMitsuru IWASAKI 	acpi_batteries_initted = 1;
17304eb77744SMitsuru IWASAKI 
17314eb77744SMitsuru IWASAKI 	if ((error = acpi_register_ioctl(ACPIIO_BATT_GET_UNITS,
17324eb77744SMitsuru IWASAKI 			acpi_battery_ioctl, NULL)) != 0) {
17334eb77744SMitsuru IWASAKI 		return (error);
17344eb77744SMitsuru IWASAKI 	}
17354eb77744SMitsuru IWASAKI 	if ((error = acpi_register_ioctl(ACPIIO_BATT_GET_BATTDESC,
17364eb77744SMitsuru IWASAKI 			acpi_battery_ioctl, NULL)) != 0) {
17374eb77744SMitsuru IWASAKI 		return (error);
17384eb77744SMitsuru IWASAKI 	}
17394eb77744SMitsuru IWASAKI 	if ((error = acpi_register_ioctl(ACPIIO_BATT_GET_BATTINFO,
17404eb77744SMitsuru IWASAKI 			acpi_battery_ioctl, NULL)) != 0) {
17414eb77744SMitsuru IWASAKI 		return (error);
17424eb77744SMitsuru IWASAKI 	}
17434eb77744SMitsuru IWASAKI 
17444eb77744SMitsuru IWASAKI 	sysctl_ctx_init(&sc->acpi_battery_sysctl_ctx);
17454eb77744SMitsuru IWASAKI 	sc->acpi_battery_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_battery_sysctl_ctx,
17464eb77744SMitsuru IWASAKI 				SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
17474eb77744SMitsuru IWASAKI 				OID_AUTO, "battery", CTLFLAG_RD, 0, "");
17484eb77744SMitsuru IWASAKI 	SYSCTL_ADD_PROC(&sc->acpi_battery_sysctl_ctx,
17494eb77744SMitsuru IWASAKI 		SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree),
17504eb77744SMitsuru IWASAKI 		OID_AUTO, "life", CTLTYPE_INT | CTLFLAG_RD,
17514eb77744SMitsuru IWASAKI 		&acpi_battery_battinfo.cap, 0, acpi_battery_sysctl, "I", "");
17524eb77744SMitsuru IWASAKI 	SYSCTL_ADD_PROC(&sc->acpi_battery_sysctl_ctx,
17534eb77744SMitsuru IWASAKI 		SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree),
17544eb77744SMitsuru IWASAKI 		OID_AUTO, "time", CTLTYPE_INT | CTLFLAG_RD,
17554eb77744SMitsuru IWASAKI 		&acpi_battery_battinfo.min, 0, acpi_battery_sysctl, "I", "");
17564eb77744SMitsuru IWASAKI 	SYSCTL_ADD_PROC(&sc->acpi_battery_sysctl_ctx,
17574eb77744SMitsuru IWASAKI 		SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree),
17584eb77744SMitsuru IWASAKI 		OID_AUTO, "state", CTLTYPE_INT | CTLFLAG_RD,
17594eb77744SMitsuru IWASAKI 		&acpi_battery_battinfo.state, 0, acpi_battery_sysctl, "I", "");
17604eb77744SMitsuru IWASAKI 	SYSCTL_ADD_INT(&sc->acpi_battery_sysctl_ctx,
17614eb77744SMitsuru IWASAKI 		SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree),
17624eb77744SMitsuru IWASAKI 		OID_AUTO, "units", CTLFLAG_RD, &acpi_batteries_units, 0, "");
17634eb77744SMitsuru IWASAKI 
17644eb77744SMitsuru IWASAKI 	return (error);
17654eb77744SMitsuru IWASAKI }
17664eb77744SMitsuru IWASAKI 
17674eb77744SMitsuru IWASAKI int
17684eb77744SMitsuru IWASAKI acpi_battery_register(int type, int phys_unit)
17694eb77744SMitsuru IWASAKI {
17704eb77744SMitsuru IWASAKI 	int	error;
17714eb77744SMitsuru IWASAKI 	struct acpi_batteries	*bp;
17724eb77744SMitsuru IWASAKI 
17734eb77744SMitsuru IWASAKI 	error = 0;
17744eb77744SMitsuru IWASAKI 	if ((bp = malloc(sizeof(*bp), M_ACPIDEV, M_NOWAIT)) == NULL) {
17754eb77744SMitsuru IWASAKI 		return(ENOMEM);
17764eb77744SMitsuru IWASAKI 	}
17774eb77744SMitsuru IWASAKI 
17784eb77744SMitsuru IWASAKI 	bp->battdesc.type = type;
17794eb77744SMitsuru IWASAKI 	bp->battdesc.phys_unit = phys_unit;
17804eb77744SMitsuru IWASAKI 	if (acpi_batteries_initted == 0) {
17814eb77744SMitsuru IWASAKI 		if ((error = acpi_battery_init()) != 0) {
17824eb77744SMitsuru IWASAKI 			free(bp, M_ACPIDEV);
17834eb77744SMitsuru IWASAKI 			return(error);
17844eb77744SMitsuru IWASAKI 		}
17854eb77744SMitsuru IWASAKI 	}
17864eb77744SMitsuru IWASAKI 
17874eb77744SMitsuru IWASAKI 	TAILQ_INSERT_TAIL(&acpi_batteries, bp, link);
17884eb77744SMitsuru IWASAKI 	acpi_batteries_units++;
17894eb77744SMitsuru IWASAKI 
17904eb77744SMitsuru IWASAKI 	return(0);
17914eb77744SMitsuru IWASAKI }
1792