xref: /freebsd/sys/dev/acpica/acpi.c (revision 6161544ca763d8c1432b4b68ab5e84407d048e83)
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 
2656161544cSTakanori Watanabe     acpi_install_wakeup_handler(sc);
2666161544cSTakanori Watanabe 
26715e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
26815e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "spaces"))
26915e32d5dSMike Smith 	acpi_EnterDebugger();
27015e32d5dSMike Smith #endif
27115e32d5dSMike Smith 
27215e32d5dSMike Smith     /*
27315e32d5dSMike Smith      * Install the default address space handlers.
27415e32d5dSMike Smith      */
275cb9b0d80SMike Smith     error = ENXIO;
276cb9b0d80SMike Smith     if ((status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
2772a4ac806SMike Smith 						ACPI_ADR_SPACE_SYSTEM_MEMORY,
27815e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
27915e32d5dSMike Smith 						NULL, NULL)) != AE_OK) {
280cb9b0d80SMike Smith 	device_printf(dev, "could not initialise SystemMemory handler: %s\n", acpi_strerror(status));
281cb9b0d80SMike Smith 	goto out;
28215e32d5dSMike Smith     }
283cb9b0d80SMike Smith     if ((status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
2842a4ac806SMike Smith 						ACPI_ADR_SPACE_SYSTEM_IO,
28515e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
28615e32d5dSMike Smith 						NULL, NULL)) != AE_OK) {
287cb9b0d80SMike Smith 	device_printf(dev, "could not initialise SystemIO handler: %s\n", acpi_strerror(status));
288cb9b0d80SMike Smith 	goto out;
28915e32d5dSMike Smith     }
290cb9b0d80SMike Smith     if ((status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
2912a4ac806SMike Smith 						ACPI_ADR_SPACE_PCI_CONFIG,
29215e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
29315e32d5dSMike Smith 						NULL, NULL)) != AE_OK) {
294cb9b0d80SMike Smith 	device_printf(dev, "could not initialise PciConfig handler: %s\n", acpi_strerror(status));
295cb9b0d80SMike Smith 	goto out;
29615e32d5dSMike Smith     }
29715e32d5dSMike Smith 
29815e32d5dSMike Smith     /*
29915e32d5dSMike Smith      * Bring ACPI fully online.
30015e32d5dSMike Smith      *
30115e32d5dSMike Smith      * Note that we request that device _STA and _INI methods not be run (ACPI_NO_DEVICE_INIT)
30215e32d5dSMike Smith      * and the final object initialisation pass be skipped (ACPI_NO_OBJECT_INIT).
30315e32d5dSMike Smith      *
30415e32d5dSMike Smith      * XXX We need to arrange for the object init pass after we have attached all our
30515e32d5dSMike Smith      *     child devices.
30615e32d5dSMike Smith      */
30715e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
30815e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "enable"))
30915e32d5dSMike Smith 	acpi_EnterDebugger();
31015e32d5dSMike Smith #endif
311cb9b0d80SMike Smith     if ((status = AcpiEnableSubsystem(ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT)) != AE_OK) {
312cb9b0d80SMike Smith 	device_printf(dev, "could not enable ACPI: %s\n", acpi_strerror(status));
313cb9b0d80SMike Smith 	goto out;
31415e32d5dSMike Smith     }
31515e32d5dSMike Smith 
31615e32d5dSMike Smith     /*
3171d073b1dSJohn Baldwin      * Setup our sysctl tree.
3181d073b1dSJohn Baldwin      *
3191d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
3201d073b1dSJohn Baldwin      */
3211d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
3221d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
3231d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
3241d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
3251d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
3261d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
3271d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
3281d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
3291d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
3301d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
3311d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
3321d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
3331d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
3341d073b1dSJohn Baldwin 
3351d073b1dSJohn Baldwin     /*
33615e32d5dSMike Smith      * Dispatch the default sleep state to devices.
33715e32d5dSMike Smith      * TBD: should be configured from userland policy manager.
33815e32d5dSMike Smith      */
33915e32d5dSMike Smith     sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
34015e32d5dSMike Smith     sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
34115e32d5dSMike Smith     sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
34215e32d5dSMike Smith 
34313d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
34415e32d5dSMike Smith 
34515e32d5dSMike Smith     /*
34615e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
34715e32d5dSMike Smith      */
34815e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
34915e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "probe"))
35015e32d5dSMike Smith 	acpi_EnterDebugger();
35115e32d5dSMike Smith #endif
3520ae55423SMike Smith     if (!acpi_disabled("bus"))
35315e32d5dSMike Smith 	acpi_probe_children(dev);
35415e32d5dSMike Smith 
35515e32d5dSMike Smith     /*
35615e32d5dSMike Smith      * Register our shutdown handlers
35715e32d5dSMike Smith      */
35815e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST);
35915e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST);
36015e32d5dSMike Smith 
36115e32d5dSMike Smith     /*
36215e32d5dSMike Smith      * Register our acpi event handlers.
36315e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
36415e32d5dSMike Smith      */
36515e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST);
36615e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST);
36715e32d5dSMike Smith 
36815e32d5dSMike Smith     /*
36915e32d5dSMike Smith      * Flag our initial states.
37015e32d5dSMike Smith      */
37115e32d5dSMike Smith     sc->acpi_enabled = 1;
37215e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
37315e32d5dSMike Smith 
37415e32d5dSMike Smith     /*
37515e32d5dSMike Smith      * Create the control device
37615e32d5dSMike Smith      */
37715e32d5dSMike Smith     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, 0, 5, 0660, "acpi");
37815e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
37915e32d5dSMike Smith 
38015e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
38115e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "running"))
38215e32d5dSMike Smith 	acpi_EnterDebugger();
38315e32d5dSMike Smith #endif
384cb9b0d80SMike Smith     error = 0;
385cb9b0d80SMike Smith 
386cb9b0d80SMike Smith  out:
387cb9b0d80SMike Smith     ACPI_UNLOCK;
388cb9b0d80SMike Smith     return_VALUE(error);
38915e32d5dSMike Smith }
39015e32d5dSMike Smith 
39115e32d5dSMike Smith /*
39215e32d5dSMike Smith  * Handle a new device being added
39315e32d5dSMike Smith  */
39415e32d5dSMike Smith static device_t
39515e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
39615e32d5dSMike Smith {
39715e32d5dSMike Smith     struct acpi_device	*ad;
39815e32d5dSMike Smith     device_t		child;
39915e32d5dSMike Smith 
40015e32d5dSMike Smith     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL)
40115e32d5dSMike Smith 	return(NULL);
40215e32d5dSMike Smith     bzero(ad, sizeof(*ad));
40315e32d5dSMike Smith 
40415e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
40515e32d5dSMike Smith 
40615e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
40715e32d5dSMike Smith     if (child != NULL)
40815e32d5dSMike Smith 	device_set_ivars(child, ad);
40915e32d5dSMike Smith     return(child);
41015e32d5dSMike Smith }
41115e32d5dSMike Smith 
41215e32d5dSMike Smith /*
41315e32d5dSMike Smith  * Print child device resource usage
41415e32d5dSMike Smith  */
41515e32d5dSMike Smith static int
41615e32d5dSMike Smith acpi_print_resources(struct resource_list *rl, const char *name, int type, const char *format)
41715e32d5dSMike Smith {
41815e32d5dSMike Smith     struct resource_list_entry	*rle;
41915e32d5dSMike Smith     int				printed, retval;
42015e32d5dSMike Smith 
42115e32d5dSMike Smith     printed = 0;
42215e32d5dSMike Smith     retval = 0;
42315e32d5dSMike Smith 
42415e32d5dSMike Smith     if (!SLIST_FIRST(rl))
42515e32d5dSMike Smith 	return(0);
42615e32d5dSMike Smith 
42715e32d5dSMike Smith     /* Yes, this is kinda cheating */
42815e32d5dSMike Smith     SLIST_FOREACH(rle, rl, link) {
42915e32d5dSMike Smith 	if (rle->type == type) {
43015e32d5dSMike Smith 	    if (printed == 0)
43115e32d5dSMike Smith 		retval += printf(" %s ", name);
43215e32d5dSMike Smith 	    else if (printed > 0)
43315e32d5dSMike Smith 		retval += printf(",");
43415e32d5dSMike Smith 	    printed++;
43515e32d5dSMike Smith 	    retval += printf(format, rle->start);
43615e32d5dSMike Smith 	    if (rle->count > 1) {
43715e32d5dSMike Smith 		retval += printf("-");
43815e32d5dSMike Smith 		retval += printf(format, rle->start +
43915e32d5dSMike Smith 				 rle->count - 1);
44015e32d5dSMike Smith 	    }
44115e32d5dSMike Smith 	}
44215e32d5dSMike Smith     }
44315e32d5dSMike Smith     return(retval);
44415e32d5dSMike Smith }
44515e32d5dSMike Smith 
44615e32d5dSMike Smith static int
44715e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
44815e32d5dSMike Smith {
44915e32d5dSMike Smith     struct acpi_device		*adev = device_get_ivars(child);
45015e32d5dSMike Smith     struct resource_list	*rl = &adev->ad_rl;
45115e32d5dSMike Smith     int retval = 0;
45215e32d5dSMike Smith 
45315e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
45415e32d5dSMike Smith     retval += acpi_print_resources(rl, "port",  SYS_RES_IOPORT, "%#lx");
45515e32d5dSMike Smith     retval += acpi_print_resources(rl, "iomem", SYS_RES_MEMORY, "%#lx");
45615e32d5dSMike Smith     retval += acpi_print_resources(rl, "irq",   SYS_RES_IRQ,    "%ld");
45715e32d5dSMike Smith     retval += bus_print_child_footer(bus, child);
45815e32d5dSMike Smith 
45915e32d5dSMike Smith     return(retval);
46015e32d5dSMike Smith }
46115e32d5dSMike Smith 
46215e32d5dSMike Smith 
46315e32d5dSMike Smith /*
46415e32d5dSMike Smith  * Handle per-device ivars
46515e32d5dSMike Smith  */
46615e32d5dSMike Smith static int
46715e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
46815e32d5dSMike Smith {
46915e32d5dSMike Smith     struct acpi_device	*ad;
47015e32d5dSMike Smith 
47115e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
47215e32d5dSMike Smith 	printf("device has no ivars\n");
47315e32d5dSMike Smith 	return(ENOENT);
47415e32d5dSMike Smith     }
47515e32d5dSMike Smith 
47615e32d5dSMike Smith     switch(index) {
47715e32d5dSMike Smith 	/* ACPI ivars */
47815e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
47915e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
48015e32d5dSMike Smith 	break;
48115e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
48215e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
48315e32d5dSMike Smith 	break;
48415e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
48515e32d5dSMike Smith 	*(void **)result = ad->ad_private;
48615e32d5dSMike Smith 	break;
48715e32d5dSMike Smith 
48815e32d5dSMike Smith     default:
48915e32d5dSMike Smith 	panic("bad ivar read request (%d)\n", index);
49015e32d5dSMike Smith 	return(ENOENT);
49115e32d5dSMike Smith     }
49215e32d5dSMike Smith     return(0);
49315e32d5dSMike Smith }
49415e32d5dSMike Smith 
49515e32d5dSMike Smith static int
49615e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
49715e32d5dSMike Smith {
49815e32d5dSMike Smith     struct acpi_device	*ad;
49915e32d5dSMike Smith 
50015e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
50115e32d5dSMike Smith 	printf("device has no ivars\n");
50215e32d5dSMike Smith 	return(ENOENT);
50315e32d5dSMike Smith     }
50415e32d5dSMike Smith 
50515e32d5dSMike Smith     switch(index) {
50615e32d5dSMike Smith 	/* ACPI ivars */
50715e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
50815e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
50915e32d5dSMike Smith 	break;
51015e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
51115e32d5dSMike Smith 	ad->ad_magic = (int )value;
51215e32d5dSMike Smith 	break;
51315e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
51415e32d5dSMike Smith 	ad->ad_private = (void *)value;
51515e32d5dSMike Smith 	break;
51615e32d5dSMike Smith 
51715e32d5dSMike Smith     default:
51815e32d5dSMike Smith 	panic("bad ivar write request (%d)\n", index);
51915e32d5dSMike Smith 	return(ENOENT);
52015e32d5dSMike Smith     }
52115e32d5dSMike Smith     return(0);
52215e32d5dSMike Smith }
52315e32d5dSMike Smith 
52415e32d5dSMike Smith /*
52515e32d5dSMike Smith  * Handle child resource allocation/removal
52615e32d5dSMike Smith  */
52715e32d5dSMike Smith static int
52815e32d5dSMike Smith acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
52915e32d5dSMike Smith {
53015e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
53115e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
53215e32d5dSMike Smith 
53315e32d5dSMike Smith     resource_list_add(rl, type, rid, start, start + count -1, count);
53415e32d5dSMike Smith 
53515e32d5dSMike Smith     return(0);
53615e32d5dSMike Smith }
53715e32d5dSMike Smith 
53815e32d5dSMike Smith static int
53915e32d5dSMike Smith acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
54015e32d5dSMike Smith {
54115e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
54215e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
54315e32d5dSMike Smith     struct resource_list_entry	*rle;
54415e32d5dSMike Smith 
54515e32d5dSMike Smith     rle = resource_list_find(rl, type, rid);
54615e32d5dSMike Smith     if (!rle)
54715e32d5dSMike Smith 	return(ENOENT);
54815e32d5dSMike Smith 
54915e32d5dSMike Smith     if (startp)
55015e32d5dSMike Smith 	*startp = rle->start;
55115e32d5dSMike Smith     if (countp)
55215e32d5dSMike Smith 	*countp = rle->count;
55315e32d5dSMike Smith 
55415e32d5dSMike Smith     return(0);
55515e32d5dSMike Smith }
55615e32d5dSMike Smith 
55715e32d5dSMike Smith static struct resource *
55815e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
55915e32d5dSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
56015e32d5dSMike Smith {
56115e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
56215e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
56315e32d5dSMike Smith 
56415e32d5dSMike Smith     return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags));
56515e32d5dSMike Smith }
56615e32d5dSMike Smith 
56715e32d5dSMike Smith static int
56815e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
56915e32d5dSMike Smith {
57015e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
57115e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
57215e32d5dSMike Smith 
57315e32d5dSMike Smith     return(resource_list_release(rl, bus, child, type, rid, r));
57415e32d5dSMike Smith }
57515e32d5dSMike Smith 
57615e32d5dSMike Smith /*
57715e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
57815e32d5dSMike Smith  *
5797d3bcec9SMike Smith  * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and \_SB_ scopes,
5807d3bcec9SMike Smith  * and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
58115e32d5dSMike Smith  */
58215e32d5dSMike Smith static void
58315e32d5dSMike Smith acpi_probe_children(device_t bus)
58415e32d5dSMike Smith {
58515e32d5dSMike Smith     ACPI_HANDLE		parent;
5867d3bcec9SMike Smith     static char		*scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
58715e32d5dSMike Smith     int			i;
58815e32d5dSMike Smith 
5892a4ac806SMike Smith     FUNCTION_TRACE(__func__);
590cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
5910ae55423SMike Smith 
59215e32d5dSMike Smith     /*
59315e32d5dSMike Smith      * Create any static children by calling device identify methods.
59415e32d5dSMike Smith      */
5950ae55423SMike Smith     DEBUG_PRINT(TRACE_OBJECTS, ("device identify routines\n"));
59615e32d5dSMike Smith     bus_generic_probe(bus);
59715e32d5dSMike Smith 
59815e32d5dSMike Smith     /*
59915e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
60015e32d5dSMike Smith      * we find.
60115e32d5dSMike Smith      *
60215e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
60315e32d5dSMike Smith      * we want to create nodes for all devices, not just those that are currently
60415e32d5dSMike Smith      * present. (This assumes that we don't want to create/remove devices as they
60515e32d5dSMike Smith      * appear, which might be smarter.)
60615e32d5dSMike Smith      */
6070ae55423SMike Smith     DEBUG_PRINT(TRACE_OBJECTS, ("namespace scan\n"));
60815e32d5dSMike Smith     for (i = 0; scopes[i] != NULL; i++)
60915e32d5dSMike Smith 	if ((AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent)) == AE_OK)
61015e32d5dSMike Smith 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL);
61115e32d5dSMike Smith 
61215e32d5dSMike Smith     /*
61315e32d5dSMike Smith      * Scan all of the child devices we have created and let them probe/attach.
61415e32d5dSMike Smith      */
6150ae55423SMike Smith     DEBUG_PRINT(TRACE_OBJECTS, ("first bus_generic_attach\n"));
61615e32d5dSMike Smith     bus_generic_attach(bus);
61715e32d5dSMike Smith 
61815e32d5dSMike Smith     /*
61915e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
62015e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
62115e32d5dSMike Smith      */
6220ae55423SMike Smith     DEBUG_PRINT(TRACE_OBJECTS, ("second bus_generic_attach\n"));
62315e32d5dSMike Smith     bus_generic_attach(bus);
6240ae55423SMike Smith 
6250ae55423SMike Smith     return_VOID;
62615e32d5dSMike Smith }
62715e32d5dSMike Smith 
62815e32d5dSMike Smith /*
62915e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
63015e32d5dSMike Smith  * it.
63115e32d5dSMike Smith  */
63215e32d5dSMike Smith static ACPI_STATUS
63315e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
63415e32d5dSMike Smith {
63515e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
63615e32d5dSMike Smith     device_t		child, bus = (device_t)context;
63715e32d5dSMike Smith 
6382a4ac806SMike Smith     FUNCTION_TRACE(__func__);
6390ae55423SMike Smith 
6400ae55423SMike Smith     /*
6410ae55423SMike Smith      * Skip this device if we think we'll have trouble with it.
6420ae55423SMike Smith      */
6430ae55423SMike Smith     if (acpi_avoid(handle))
6440ae55423SMike Smith 	return_ACPI_STATUS(AE_OK);
6450ae55423SMike Smith 
64615e32d5dSMike Smith     if (AcpiGetType(handle, &type) == AE_OK) {
64715e32d5dSMike Smith 	switch(type) {
64815e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
64915e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
65015e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
65115e32d5dSMike Smith 	case ACPI_TYPE_POWER:
6520ae55423SMike Smith 	    if (acpi_disabled("children"))
6530ae55423SMike Smith 		break;
65415e32d5dSMike Smith 	    /*
65515e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
65615e32d5dSMike Smith 	     * so that the probe/attach passes will run breadth-first.
65715e32d5dSMike Smith 	     */
6580ae55423SMike Smith 	    DEBUG_PRINT(TRACE_OBJECTS, ("scanning '%s'\n", acpi_name(handle)))
65915e32d5dSMike Smith 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
66015e32d5dSMike Smith 	    acpi_set_handle(child, handle);
6610ae55423SMike Smith 	    DEBUG_EXEC(device_probe_and_attach(child));
6620ae55423SMike Smith 	    break;
66315e32d5dSMike Smith 	}
66415e32d5dSMike Smith     }
6650ae55423SMike Smith     return_ACPI_STATUS(AE_OK);
66615e32d5dSMike Smith }
66715e32d5dSMike Smith 
66815e32d5dSMike Smith static void
66915e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto)
67015e32d5dSMike Smith {
671cb9b0d80SMike Smith 
672cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
673cb9b0d80SMike Smith 
67415e32d5dSMike Smith     /*
6750ae55423SMike Smith      * Disable all ACPI events before soft off, otherwise the system
67615e32d5dSMike Smith      * will be turned on again on some laptops.
67715e32d5dSMike Smith      *
67815e32d5dSMike Smith      * XXX this should probably be restricted to masking some events just
67915e32d5dSMike Smith      *     before powering down, since we may still need ACPI during the
68015e32d5dSMike Smith      *     shutdown process.
68115e32d5dSMike Smith      */
68215e32d5dSMike Smith     acpi_Disable((struct acpi_softc *)arg);
68315e32d5dSMike Smith }
68415e32d5dSMike Smith 
68515e32d5dSMike Smith static void
68615e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
68715e32d5dSMike Smith {
68815e32d5dSMike Smith     ACPI_STATUS	status;
68915e32d5dSMike Smith 
690cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
691cb9b0d80SMike Smith 
6925f3e4175SMitsuru IWASAKI     if (howto & RB_POWEROFF) {
69315e32d5dSMike Smith 	printf("Power system off using ACPI...\n");
6942a4ac806SMike Smith 	if ((status = AcpiEnterSleepState(acpi_off_state)) != AE_OK) {
69515e32d5dSMike Smith 	    printf("ACPI power-off failed - %s\n", acpi_strerror(status));
69615e32d5dSMike Smith 	} else {
69715e32d5dSMike Smith 	    DELAY(1000000);
69815e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
69915e32d5dSMike Smith 	}
70015e32d5dSMike Smith     }
70115e32d5dSMike Smith }
70215e32d5dSMike Smith 
70313d4f7baSMitsuru IWASAKI static void
70413d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
70513d4f7baSMitsuru IWASAKI {
70613d4f7baSMitsuru IWASAKI     static int	first_time = 1;
70713d4f7baSMitsuru IWASAKI #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n"
70813d4f7baSMitsuru IWASAKI 
709cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
710cb9b0d80SMike Smith 
71113d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
712cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) {
71313d4f7baSMitsuru IWASAKI 	AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED);
71413d4f7baSMitsuru IWASAKI 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED);
71513d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
71613d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_power_button_for_sleep, sc);
71713d4f7baSMitsuru IWASAKI 	if (first_time) {
71813d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "power");
71913d4f7baSMitsuru IWASAKI 	}
72013d4f7baSMitsuru IWASAKI     }
721cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) {
72213d4f7baSMitsuru IWASAKI 	AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED);
72313d4f7baSMitsuru IWASAKI 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED);
72413d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
72513d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_sleep_button_for_sleep, sc);
72613d4f7baSMitsuru IWASAKI 	if (first_time) {
72713d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "sleep");
72813d4f7baSMitsuru IWASAKI 	}
72913d4f7baSMitsuru IWASAKI     }
73013d4f7baSMitsuru IWASAKI 
73113d4f7baSMitsuru IWASAKI     first_time = 0;
73213d4f7baSMitsuru IWASAKI }
73313d4f7baSMitsuru IWASAKI 
73415e32d5dSMike Smith /*
735c5ba0be4SMike Smith  * Returns true if the device is actually present and should
736c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
737c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
738c5ba0be4SMike Smith  */
739c5ba0be4SMike Smith BOOLEAN
740c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
741c5ba0be4SMike Smith {
742c5ba0be4SMike Smith     ACPI_HANDLE		h;
743c5ba0be4SMike Smith     ACPI_DEVICE_INFO	devinfo;
744c5ba0be4SMike Smith     ACPI_STATUS		error;
745c5ba0be4SMike Smith 
746cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
747cb9b0d80SMike Smith 
748c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
749c5ba0be4SMike Smith 	return(FALSE);
750c5ba0be4SMike Smith     if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK)
751c5ba0be4SMike Smith 	return(FALSE);
7526f69255bSMike Smith     /* XXX 0xf is probably not appropriate */
753c5ba0be4SMike Smith     if ((devinfo.Valid & ACPI_VALID_HID) && (devinfo.CurrentStatus & 0xf))
754c5ba0be4SMike Smith 	return(TRUE);
755c5ba0be4SMike Smith     return(FALSE);
756c5ba0be4SMike Smith }
757c5ba0be4SMike Smith 
758c5ba0be4SMike Smith /*
75915e32d5dSMike Smith  * Match a HID string against a device
76015e32d5dSMike Smith  */
76115e32d5dSMike Smith BOOLEAN
76215e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid)
76315e32d5dSMike Smith {
76415e32d5dSMike Smith     ACPI_HANDLE		h;
76515e32d5dSMike Smith     ACPI_DEVICE_INFO	devinfo;
76615e32d5dSMike Smith     ACPI_STATUS		error;
76715e32d5dSMike Smith 
768cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
769cb9b0d80SMike Smith 
7704d332989SMike Smith     if (hid == NULL)
77115e32d5dSMike Smith 	return(FALSE);
77215e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
77315e32d5dSMike Smith 	return(FALSE);
77415e32d5dSMike Smith     if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK)
77515e32d5dSMike Smith 	return(FALSE);
7764d332989SMike Smith     if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId))
77715e32d5dSMike Smith 	return(TRUE);
77815e32d5dSMike Smith     return(FALSE);
77915e32d5dSMike Smith }
78015e32d5dSMike Smith 
78115e32d5dSMike Smith /*
782c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
783c5ba0be4SMike Smith  * or one if its parents.
784c5ba0be4SMike Smith  */
785c5ba0be4SMike Smith ACPI_STATUS
786c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
787c5ba0be4SMike Smith {
788c5ba0be4SMike Smith     ACPI_HANDLE		r;
789c5ba0be4SMike Smith     ACPI_STATUS		status;
790c5ba0be4SMike Smith 
791cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
792cb9b0d80SMike Smith 
793c5ba0be4SMike Smith     /* walk back up the tree to the root */
794c5ba0be4SMike Smith     for (;;) {
795c5ba0be4SMike Smith 	status = AcpiGetHandle(parent, path, &r);
796c5ba0be4SMike Smith 	if (status == AE_OK) {
797c5ba0be4SMike Smith 	    *result = r;
798c5ba0be4SMike Smith 	    return(AE_OK);
799c5ba0be4SMike Smith 	}
800c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
801c5ba0be4SMike Smith 	    return(AE_OK);
802c5ba0be4SMike Smith 	if (AcpiGetParent(parent, &r) != AE_OK)
803c5ba0be4SMike Smith 	    return(AE_NOT_FOUND);
804c5ba0be4SMike Smith 	parent = r;
805c5ba0be4SMike Smith     }
806c5ba0be4SMike Smith }
807c5ba0be4SMike Smith 
808c5ba0be4SMike Smith /*
809c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
810c5ba0be4SMike Smith  */
811c5ba0be4SMike Smith ACPI_BUFFER *
812c5ba0be4SMike Smith acpi_AllocBuffer(int size)
813c5ba0be4SMike Smith {
814c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
815c5ba0be4SMike Smith 
816c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
817c5ba0be4SMike Smith 	return(NULL);
818c5ba0be4SMike Smith     buf->Length = size;
819c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
820c5ba0be4SMike Smith     return(buf);
821c5ba0be4SMike Smith }
822c5ba0be4SMike Smith 
823c5ba0be4SMike Smith /*
82415e32d5dSMike Smith  * Perform the tedious double-get procedure required for fetching something into
82515e32d5dSMike Smith  * an ACPI_BUFFER that has not been initialised.
82615e32d5dSMike Smith  */
82715e32d5dSMike Smith ACPI_STATUS
82815e32d5dSMike Smith acpi_GetIntoBuffer(ACPI_HANDLE handle, ACPI_STATUS (*func)(ACPI_HANDLE, ACPI_BUFFER *), ACPI_BUFFER *buf)
82915e32d5dSMike Smith {
83015e32d5dSMike Smith     ACPI_STATUS	status;
83115e32d5dSMike Smith 
832cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
833cb9b0d80SMike Smith 
83415e32d5dSMike Smith     buf->Length = 0;
83515e32d5dSMike Smith     buf->Pointer = NULL;
83615e32d5dSMike Smith 
83715e32d5dSMike Smith     if ((status = func(handle, buf)) != AE_BUFFER_OVERFLOW)
83815e32d5dSMike Smith 	return(status);
83915e32d5dSMike Smith     if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL)
84015e32d5dSMike Smith 	return(AE_NO_MEMORY);
84115e32d5dSMike Smith     return(func(handle, buf));
84215e32d5dSMike Smith }
84315e32d5dSMike Smith 
84415e32d5dSMike Smith /*
8457d3bcec9SMike Smith  * Perform the tedious double-get procedure required for fetching a table into
8467d3bcec9SMike Smith  * an ACPI_BUFFER that has not been initialised.
8477d3bcec9SMike Smith  */
8487d3bcec9SMike Smith ACPI_STATUS
8497d3bcec9SMike Smith acpi_GetTableIntoBuffer(ACPI_TABLE_TYPE table, UINT32 instance, ACPI_BUFFER *buf)
8507d3bcec9SMike Smith {
8517d3bcec9SMike Smith     ACPI_STATUS	status;
8527d3bcec9SMike Smith 
8537d3bcec9SMike Smith     ACPI_ASSERTLOCK;
8547d3bcec9SMike Smith 
8557d3bcec9SMike Smith     buf->Length = 0;
8567d3bcec9SMike Smith     buf->Pointer = NULL;
8577d3bcec9SMike Smith 
8587d3bcec9SMike Smith     if ((status = AcpiGetTable(table, instance, buf)) != AE_BUFFER_OVERFLOW)
8597d3bcec9SMike Smith 	return(status);
8607d3bcec9SMike Smith     if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL)
8617d3bcec9SMike Smith 	return(AE_NO_MEMORY);
8627d3bcec9SMike Smith     return(AcpiGetTable(table, instance, buf));
8637d3bcec9SMike Smith }
8647d3bcec9SMike Smith 
8657d3bcec9SMike Smith /*
866c5ba0be4SMike Smith  * Perform the tedious double-evaluate procedure for evaluating something into
867c5ba0be4SMike Smith  * an ACPI_BUFFER that has not been initialised.  Note that this evaluates
868c5ba0be4SMike Smith  * twice, so avoid applying this to things that may have side-effects.
869c5ba0be4SMike Smith  *
870c5ba0be4SMike Smith  * This is like AcpiEvaluateObject with automatic buffer allocation.
87115e32d5dSMike Smith  */
872c5ba0be4SMike Smith ACPI_STATUS
873c5ba0be4SMike Smith acpi_EvaluateIntoBuffer(ACPI_HANDLE object, ACPI_STRING pathname, ACPI_OBJECT_LIST *params,
874c5ba0be4SMike Smith 			ACPI_BUFFER *buf)
87515e32d5dSMike Smith {
876c5ba0be4SMike Smith     ACPI_STATUS	status;
87715e32d5dSMike Smith 
878cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
879cb9b0d80SMike Smith 
880c5ba0be4SMike Smith     buf->Length = 0;
881c5ba0be4SMike Smith     buf->Pointer = NULL;
882c5ba0be4SMike Smith 
883c5ba0be4SMike Smith     if ((status = AcpiEvaluateObject(object, pathname, params, buf)) != AE_BUFFER_OVERFLOW)
884c5ba0be4SMike Smith 	return(status);
885c5ba0be4SMike Smith     if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL)
886c5ba0be4SMike Smith 	return(AE_NO_MEMORY);
887c5ba0be4SMike Smith     return(AcpiEvaluateObject(object, pathname, params, buf));
88815e32d5dSMike Smith }
88915e32d5dSMike Smith 
890c5ba0be4SMike Smith /*
891c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
892c5ba0be4SMike Smith  */
893c5ba0be4SMike Smith ACPI_STATUS
894c5ba0be4SMike Smith acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number)
895c5ba0be4SMike Smith {
896c5ba0be4SMike Smith     ACPI_STATUS	error;
897c5ba0be4SMike Smith     ACPI_BUFFER	buf;
898c5ba0be4SMike Smith     ACPI_OBJECT	param;
899c5ba0be4SMike Smith 
900cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
901cb9b0d80SMike Smith 
902c5ba0be4SMike Smith     if (handle == NULL)
903c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
904c5ba0be4SMike Smith     buf.Pointer = &param;
905c5ba0be4SMike Smith     buf.Length = sizeof(param);
906c5ba0be4SMike Smith     if ((error = AcpiEvaluateObject(handle, path, NULL, &buf)) == AE_OK) {
907c5ba0be4SMike Smith 	if (param.Type == ACPI_TYPE_INTEGER) {
908c5ba0be4SMike Smith 	    *number = param.Integer.Value;
909c5ba0be4SMike Smith 	} else {
910c5ba0be4SMike Smith 	    error = AE_TYPE;
911c5ba0be4SMike Smith 	}
912c5ba0be4SMike Smith     }
913c5ba0be4SMike Smith     return(error);
914c5ba0be4SMike Smith }
915c5ba0be4SMike Smith 
916c5ba0be4SMike Smith /*
917c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
918c5ba0be4SMike Smith  * function for each element.
919c5ba0be4SMike Smith  *
920c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
921c5ba0be4SMike Smith  */
922c5ba0be4SMike Smith ACPI_STATUS
923c5ba0be4SMike Smith acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg)
924c5ba0be4SMike Smith {
925c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
926c5ba0be4SMike Smith     int		i;
927c5ba0be4SMike Smith 
928c5ba0be4SMike Smith     if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE))
929c5ba0be4SMike Smith 	return(AE_BAD_PARAMETER);
930c5ba0be4SMike Smith 
931c5ba0be4SMike Smith     /* iterate over components */
932c5ba0be4SMike Smith     for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++)
933c5ba0be4SMike Smith 	func(comp, arg);
934c5ba0be4SMike Smith 
935c5ba0be4SMike Smith     return(AE_OK);
936c5ba0be4SMike Smith }
937c5ba0be4SMike Smith 
9386f69255bSMike Smith /*
9396f69255bSMike Smith  * Find the (index)th resource object in a set.
9406f69255bSMike Smith  */
9416f69255bSMike Smith ACPI_STATUS
9426f69255bSMike Smith acpi_FindIndexedResource(ACPI_RESOURCE *resbuf, int index, ACPI_RESOURCE **resp)
9436f69255bSMike Smith {
9446f69255bSMike Smith     u_int8_t		*p;
9456f69255bSMike Smith     int			i;
9466f69255bSMike Smith 
9476f69255bSMike Smith     p = (u_int8_t *)resbuf;
9486f69255bSMike Smith     i = index;
9496f69255bSMike Smith     while (i > 0) {
9506f69255bSMike Smith 	/* range check */
9516f69255bSMike Smith 	if (p > ((u_int8_t *)resbuf + resbuf->Length))
9526f69255bSMike Smith 	    return(AE_BAD_PARAMETER);
9536f69255bSMike Smith 	p += ((ACPI_RESOURCE *)p)->Length;
9546f69255bSMike Smith 	i--;
9556f69255bSMike Smith     }
9566f69255bSMike Smith     if (resp != NULL)
9576f69255bSMike Smith 	*resp = (ACPI_RESOURCE *)p;
9586f69255bSMike Smith     return(AE_OK);
9596f69255bSMike Smith }
960c5ba0be4SMike Smith 
961c30382dfSMitsuru IWASAKI static ACPI_STATUS __inline
962c30382dfSMitsuru IWASAKI acpi_wakeup(UINT8 state)
963c30382dfSMitsuru IWASAKI {
964c30382dfSMitsuru IWASAKI     ACPI_STATUS		Status;
965c30382dfSMitsuru IWASAKI     ACPI_OBJECT_LIST	Arg_list;
966c30382dfSMitsuru IWASAKI     ACPI_OBJECT		Arg;
967c30382dfSMitsuru IWASAKI     ACPI_OBJECT		Objects[3]; /* package plus 2 number objects */
968c30382dfSMitsuru IWASAKI     ACPI_BUFFER		ReturnBuffer;
969c30382dfSMitsuru IWASAKI 
9702a4ac806SMike Smith     FUNCTION_TRACE_U32(__func__, state);
971cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
972c30382dfSMitsuru IWASAKI 
973c30382dfSMitsuru IWASAKI     /*
974c30382dfSMitsuru IWASAKI      * Evaluate the _WAK method
975c30382dfSMitsuru IWASAKI      */
976cb9b0d80SMike Smith     bzero(&Arg_list, sizeof(Arg_list));
977c30382dfSMitsuru IWASAKI     Arg_list.Count = 1;
978c30382dfSMitsuru IWASAKI     Arg_list.Pointer = &Arg;
979c30382dfSMitsuru IWASAKI 
980cb9b0d80SMike Smith     bzero(&Arg, sizeof(Arg));
981c30382dfSMitsuru IWASAKI     Arg.Type = ACPI_TYPE_INTEGER;
982c30382dfSMitsuru IWASAKI     Arg.Integer.Value = state;
983c30382dfSMitsuru IWASAKI 
984cb9b0d80SMike Smith     /*
985cb9b0d80SMike Smith      * Set up _WAK result code buffer.
986cb9b0d80SMike Smith      *
987cb9b0d80SMike Smith      * XXX should use acpi_EvaluateIntoBuffer
988cb9b0d80SMike Smith      */
989cb9b0d80SMike Smith     bzero(Objects, sizeof(Objects));
990c30382dfSMitsuru IWASAKI     ReturnBuffer.Length = sizeof(Objects);
991c30382dfSMitsuru IWASAKI     ReturnBuffer.Pointer = Objects;
992c30382dfSMitsuru IWASAKI     AcpiEvaluateObject (NULL, "\\_WAK", &Arg_list, &ReturnBuffer);
993c30382dfSMitsuru IWASAKI 
994c30382dfSMitsuru IWASAKI     Status = AE_OK;
995cb9b0d80SMike Smith 
996c30382dfSMitsuru IWASAKI     /* Check result code for _WAK */
997c30382dfSMitsuru IWASAKI     if (Objects[0].Type != ACPI_TYPE_PACKAGE ||
998c30382dfSMitsuru IWASAKI 	Objects[1].Type != ACPI_TYPE_INTEGER  ||
999c30382dfSMitsuru IWASAKI 	Objects[2].Type != ACPI_TYPE_INTEGER) {
1000c30382dfSMitsuru IWASAKI 	/*
1001c30382dfSMitsuru IWASAKI 	 * In many BIOSes, _WAK doesn't return a result code.
1002c30382dfSMitsuru IWASAKI 	 * We don't need to worry about it too much :-).
1003c30382dfSMitsuru IWASAKI 	 */
1004c30382dfSMitsuru IWASAKI 	DEBUG_PRINT(ACPI_INFO,
1005c30382dfSMitsuru IWASAKI 		    ("acpi_wakeup: _WAK result code is corrupted, "
1006c30382dfSMitsuru IWASAKI 		     "but should be OK.\n"));
1007c30382dfSMitsuru IWASAKI     } else {
1008c30382dfSMitsuru IWASAKI 	/* evaluate status code */
1009c30382dfSMitsuru IWASAKI 	switch (Objects[1].Integer.Value) {
1010c30382dfSMitsuru IWASAKI 	case 0x00000001:
1011c30382dfSMitsuru IWASAKI 	    DEBUG_PRINT(ACPI_ERROR,
1012c30382dfSMitsuru IWASAKI 			("acpi_wakeup: Wake was signaled "
1013c30382dfSMitsuru IWASAKI 			 "but failed due to lack of power.\n"));
1014c30382dfSMitsuru IWASAKI 	    Status = AE_ERROR;
1015c30382dfSMitsuru IWASAKI 	    break;
1016c30382dfSMitsuru IWASAKI 
1017c30382dfSMitsuru IWASAKI 	case 0x00000002:
1018c30382dfSMitsuru IWASAKI 	    DEBUG_PRINT(ACPI_ERROR,
1019c30382dfSMitsuru IWASAKI 			("acpi_wakeup: Wake was signaled "
1020c30382dfSMitsuru IWASAKI 			 "but failed due to thermal condition.\n"));
1021c30382dfSMitsuru IWASAKI 	    Status = AE_ERROR;
1022c30382dfSMitsuru IWASAKI 	    break;
1023c30382dfSMitsuru IWASAKI 	}
1024c30382dfSMitsuru IWASAKI 	/* evaluate PSS code */
1025c30382dfSMitsuru IWASAKI 	if (Objects[2].Integer.Value == 0) {
1026c30382dfSMitsuru IWASAKI 	    DEBUG_PRINT(ACPI_ERROR,
1027c30382dfSMitsuru IWASAKI 			("acpi_wakeup: The targeted S-state "
1028c30382dfSMitsuru IWASAKI 			 "was not entered because of too much current "
1029c30382dfSMitsuru IWASAKI 			 "being drawn from the power supply.\n"));
1030c30382dfSMitsuru IWASAKI 	    Status = AE_ERROR;
1031c30382dfSMitsuru IWASAKI 	}
1032c30382dfSMitsuru IWASAKI     }
1033c30382dfSMitsuru IWASAKI     return_ACPI_STATUS(Status);
1034c30382dfSMitsuru IWASAKI }
1035c30382dfSMitsuru IWASAKI 
103615e32d5dSMike Smith /*
103715e32d5dSMike Smith  * Set the system sleep state
103815e32d5dSMike Smith  *
103915e32d5dSMike Smith  * Currently we only support S1 and S5
104015e32d5dSMike Smith  */
104115e32d5dSMike Smith ACPI_STATUS
104215e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
104315e32d5dSMike Smith {
104415e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
10456161544cSTakanori Watanabe     UINT16	Count;
104615e32d5dSMike Smith 
10472a4ac806SMike Smith     FUNCTION_TRACE_U32(__func__, state);
1048cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
10490ae55423SMike Smith 
105015e32d5dSMike Smith     switch (state) {
105115e32d5dSMike Smith     case ACPI_STATE_S0:	/* XXX only for testing */
105291467fc6SMike Smith 	status = AcpiEnterSleepState((UINT8)state);
105315e32d5dSMike Smith 	if (status != AE_OK) {
105491467fc6SMike Smith 	    device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", acpi_strerror(status));
105515e32d5dSMike Smith 	}
105615e32d5dSMike Smith 	break;
105715e32d5dSMike Smith 
105815e32d5dSMike Smith     case ACPI_STATE_S1:
10596161544cSTakanori Watanabe     case ACPI_STATE_S2:
10606161544cSTakanori Watanabe     case ACPI_STATE_S3:
10616161544cSTakanori Watanabe     case ACPI_STATE_S4:
106215e32d5dSMike Smith 	/*
106315e32d5dSMike Smith 	 * Inform all devices that we are going to sleep.
106415e32d5dSMike Smith 	 */
106515e32d5dSMike Smith 	if (DEVICE_SUSPEND(root_bus) != 0) {
106615e32d5dSMike Smith 	    /*
106715e32d5dSMike Smith 	     * Re-wake the system.
106815e32d5dSMike Smith 	     *
106915e32d5dSMike Smith 	     * XXX note that a better two-pass approach with a 'veto' pass
107015e32d5dSMike Smith 	     *     followed by a "real thing" pass would be better, but the
107115e32d5dSMike Smith 	     *     current bus interface does not provide for this.
107215e32d5dSMike Smith 	     */
107315e32d5dSMike Smith 	    DEVICE_RESUME(root_bus);
10740ae55423SMike Smith 	    return_ACPI_STATUS(AE_ERROR);
107515e32d5dSMike Smith 	}
107615e32d5dSMike Smith 	sc->acpi_sstate = state;
10776161544cSTakanori Watanabe 
10786161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
10796161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
10806161544cSTakanori Watanabe 
10816161544cSTakanori Watanabe 	    /* AcpiEnterSleepState() maybe incompleted, unlock here. */
10826161544cSTakanori Watanabe 	    AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
10836161544cSTakanori Watanabe 
10846161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
10856161544cSTakanori Watanabe 	    if (state >= ACPI_STATE_S4) {
10866161544cSTakanori Watanabe 		acpi_Disable(sc);
10876161544cSTakanori Watanabe 		acpi_Enable(sc);
10886161544cSTakanori Watanabe 	    }
10896161544cSTakanori Watanabe 	} else {
109091467fc6SMike Smith 	    status = AcpiEnterSleepState((UINT8)state);
109115e32d5dSMike Smith 	    if (status != AE_OK) {
109291467fc6SMike Smith 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", acpi_strerror(status));
1093c30382dfSMitsuru IWASAKI 		break;
109415e32d5dSMike Smith 	    }
10956161544cSTakanori Watanabe 	    /* wait for the WAK_STS bit */
10966161544cSTakanori Watanabe 	    Count = 0;
10976161544cSTakanori Watanabe 	    while (!(AcpiHwRegisterBitAccess(ACPI_READ, ACPI_MTX_LOCK, WAK_STS))) {
10986161544cSTakanori Watanabe 		AcpiOsSleepUsec(1000);
10996161544cSTakanori Watanabe 		/*
11006161544cSTakanori Watanabe 		 * Some BIOSes don't set WAK_STS at all,
11016161544cSTakanori Watanabe 		 * give up waiting for wakeup if we time out.
11026161544cSTakanori Watanabe 		 */
11036161544cSTakanori Watanabe 		if (Count > 1000) {
11046161544cSTakanori Watanabe 		    break;	/* giving up */
11056161544cSTakanori Watanabe 		}
11066161544cSTakanori Watanabe 		Count++;
11076161544cSTakanori Watanabe 	    }
11086161544cSTakanori Watanabe 	}
1109c30382dfSMitsuru IWASAKI 	acpi_wakeup((UINT8)state);
111015e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
111115e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
111213d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
111315e32d5dSMike Smith 	break;
111415e32d5dSMike Smith 
111515e32d5dSMike Smith     case ACPI_STATE_S5:
111615e32d5dSMike Smith 	/*
111715e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
111815e32d5dSMike Smith 	 * shutdown handlers.
111915e32d5dSMike Smith 	 */
112015e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
112115e32d5dSMike Smith 	break;
112215e32d5dSMike Smith 
112315e32d5dSMike Smith     default:
112415e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
112515e32d5dSMike Smith 	break;
112615e32d5dSMike Smith     }
11270ae55423SMike Smith     return_ACPI_STATUS(status);
112815e32d5dSMike Smith }
112915e32d5dSMike Smith 
113015e32d5dSMike Smith /*
113115e32d5dSMike Smith  * Enable/Disable ACPI
113215e32d5dSMike Smith  */
113315e32d5dSMike Smith ACPI_STATUS
113415e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
113515e32d5dSMike Smith {
113615e32d5dSMike Smith     ACPI_STATUS	status;
113715e32d5dSMike Smith     u_int32_t	flags;
113815e32d5dSMike Smith 
11392a4ac806SMike Smith     FUNCTION_TRACE(__func__);
1140cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
11410ae55423SMike Smith 
114215e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
114315e32d5dSMike Smith             ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
114415e32d5dSMike Smith     if (!sc->acpi_enabled) {
114515e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
114615e32d5dSMike Smith     } else {
114715e32d5dSMike Smith 	status = AE_OK;
114815e32d5dSMike Smith     }
114915e32d5dSMike Smith     if (status == AE_OK)
115015e32d5dSMike Smith 	sc->acpi_enabled = 1;
11510ae55423SMike Smith     return_ACPI_STATUS(status);
115215e32d5dSMike Smith }
115315e32d5dSMike Smith 
115415e32d5dSMike Smith ACPI_STATUS
115515e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
115615e32d5dSMike Smith {
115715e32d5dSMike Smith     ACPI_STATUS	status;
115815e32d5dSMike Smith 
11592a4ac806SMike Smith     FUNCTION_TRACE(__func__);
1160cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
11610ae55423SMike Smith 
116215e32d5dSMike Smith     if (sc->acpi_enabled) {
116315e32d5dSMike Smith 	status = AcpiDisable();
116415e32d5dSMike Smith     } else {
116515e32d5dSMike Smith 	status = AE_OK;
116615e32d5dSMike Smith     }
116715e32d5dSMike Smith     if (status == AE_OK)
116815e32d5dSMike Smith 	sc->acpi_enabled = 0;
11690ae55423SMike Smith     return_ACPI_STATUS(status);
117015e32d5dSMike Smith }
117115e32d5dSMike Smith 
117215e32d5dSMike Smith /*
117315e32d5dSMike Smith  * ACPI Event Handlers
117415e32d5dSMike Smith  */
117515e32d5dSMike Smith 
117615e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
117715e32d5dSMike Smith 
117815e32d5dSMike Smith static void
117915e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
118015e32d5dSMike Smith {
11812a4ac806SMike Smith     FUNCTION_TRACE_U32(__func__, state);
118215e32d5dSMike Smith 
1183cb9b0d80SMike Smith     ACPI_LOCK;
1184a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
118515e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
1186cb9b0d80SMike Smith     ACPI_UNLOCK;
11870ae55423SMike Smith     return_VOID;
118815e32d5dSMike Smith }
118915e32d5dSMike Smith 
119015e32d5dSMike Smith static void
119115e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
119215e32d5dSMike Smith {
11932a4ac806SMike Smith     FUNCTION_TRACE_U32(__func__, state);
11940ae55423SMike Smith 
119515e32d5dSMike Smith     /* Well, what to do? :-) */
11960ae55423SMike Smith 
1197cb9b0d80SMike Smith     ACPI_LOCK;
1198cb9b0d80SMike Smith     ACPI_UNLOCK;
1199cb9b0d80SMike Smith 
12000ae55423SMike Smith     return_VOID;
120115e32d5dSMike Smith }
120215e32d5dSMike Smith 
120315e32d5dSMike Smith /*
120415e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
120515e32d5dSMike Smith  */
120615e32d5dSMike Smith UINT32
120715e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context)
120815e32d5dSMike Smith {
120915e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
121015e32d5dSMike Smith 
12112a4ac806SMike Smith     FUNCTION_TRACE(__func__);
12120ae55423SMike Smith 
121315e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
12140ae55423SMike Smith 
12150ae55423SMike Smith     return_VALUE(INTERRUPT_HANDLED);
121615e32d5dSMike Smith }
121715e32d5dSMike Smith 
121815e32d5dSMike Smith UINT32
121915e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context)
122015e32d5dSMike Smith {
122115e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
122215e32d5dSMike Smith 
12232a4ac806SMike Smith     FUNCTION_TRACE(__func__);
12240ae55423SMike Smith 
122515e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
12260ae55423SMike Smith 
12270ae55423SMike Smith     return_VALUE(INTERRUPT_HANDLED);
122815e32d5dSMike Smith }
122915e32d5dSMike Smith 
123015e32d5dSMike Smith UINT32
123115e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context)
123215e32d5dSMike Smith {
123315e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
123415e32d5dSMike Smith 
12352a4ac806SMike Smith     FUNCTION_TRACE(__func__);
12360ae55423SMike Smith 
123715e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
12380ae55423SMike Smith 
12390ae55423SMike Smith     return_VALUE(INTERRUPT_HANDLED);
124015e32d5dSMike Smith }
124115e32d5dSMike Smith 
124215e32d5dSMike Smith UINT32
124315e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context)
124415e32d5dSMike Smith {
124515e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
124615e32d5dSMike Smith 
12472a4ac806SMike Smith     FUNCTION_TRACE(__func__);
12480ae55423SMike Smith 
124915e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
12500ae55423SMike Smith 
12510ae55423SMike Smith     return_VALUE(INTERRUPT_HANDLED);
125215e32d5dSMike Smith }
125315e32d5dSMike Smith 
125415e32d5dSMike Smith /*
125515e32d5dSMike Smith  * XXX This is kinda ugly, and should not be here.
125615e32d5dSMike Smith  */
125715e32d5dSMike Smith struct acpi_staticbuf {
125815e32d5dSMike Smith     ACPI_BUFFER	buffer;
125915e32d5dSMike Smith     char	data[512];
126015e32d5dSMike Smith };
126115e32d5dSMike Smith 
126215e32d5dSMike Smith char *
126315e32d5dSMike Smith acpi_strerror(ACPI_STATUS excep)
126415e32d5dSMike Smith {
126515e32d5dSMike Smith     static struct acpi_staticbuf	buf;
126615e32d5dSMike Smith 
126715e32d5dSMike Smith     buf.buffer.Length = 512;
126815e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
126915e32d5dSMike Smith 
127015e32d5dSMike Smith     if (AcpiFormatException(excep, &buf.buffer) == AE_OK)
127115e32d5dSMike Smith 	return(buf.buffer.Pointer);
127215e32d5dSMike Smith     return("(error formatting exception)");
127315e32d5dSMike Smith }
127415e32d5dSMike Smith 
127515e32d5dSMike Smith char *
127615e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
127715e32d5dSMike Smith {
127815e32d5dSMike Smith     static struct acpi_staticbuf	buf;
127915e32d5dSMike Smith 
1280cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1281cb9b0d80SMike Smith 
128215e32d5dSMike Smith     buf.buffer.Length = 512;
128315e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
128415e32d5dSMike Smith 
128515e32d5dSMike Smith     if (AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer) == AE_OK)
128615e32d5dSMike Smith 	return(buf.buffer.Pointer);
128715e32d5dSMike Smith     return("(unknown path)");
128815e32d5dSMike Smith }
128915e32d5dSMike Smith 
129015e32d5dSMike Smith /*
129115e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
129215e32d5dSMike Smith  * parts of the namespace.
129315e32d5dSMike Smith  */
129415e32d5dSMike Smith int
129515e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
129615e32d5dSMike Smith {
129715e32d5dSMike Smith     char	*cp, *np;
129815e32d5dSMike Smith     int		len;
129915e32d5dSMike Smith 
130015e32d5dSMike Smith     np = acpi_name(handle);
130115e32d5dSMike Smith     if (*np == '\\')
130215e32d5dSMike Smith 	np++;
130315e32d5dSMike Smith     if ((cp = getenv("debug.acpi.avoid")) == NULL)
130415e32d5dSMike Smith 	return(0);
130515e32d5dSMike Smith 
130615e32d5dSMike Smith     /* scan the avoid list checking for a match */
130715e32d5dSMike Smith     for (;;) {
130815e32d5dSMike Smith 	while ((*cp != 0) && isspace(*cp))
130915e32d5dSMike Smith 	    cp++;
131015e32d5dSMike Smith 	if (*cp == 0)
131115e32d5dSMike Smith 	    break;
131215e32d5dSMike Smith 	len = 0;
131315e32d5dSMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
131415e32d5dSMike Smith 	    len++;
131515e32d5dSMike Smith 	if (!strncmp(cp, np, len)) {
13160ae55423SMike Smith 	    DEBUG_PRINT(TRACE_OBJECTS, ("avoiding '%s'\n", np));
13170ae55423SMike Smith 	    return(1);
13180ae55423SMike Smith 	}
13190ae55423SMike Smith 	cp += len;
13200ae55423SMike Smith     }
13210ae55423SMike Smith     return(0);
13220ae55423SMike Smith }
13230ae55423SMike Smith 
13240ae55423SMike Smith /*
13250ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
13260ae55423SMike Smith  */
13270ae55423SMike Smith int
13280ae55423SMike Smith acpi_disabled(char *subsys)
13290ae55423SMike Smith {
13300ae55423SMike Smith     char	*cp;
13310ae55423SMike Smith     int		len;
13320ae55423SMike Smith 
13330ae55423SMike Smith     if ((cp = getenv("debug.acpi.disable")) == NULL)
13340ae55423SMike Smith 	return(0);
13350ae55423SMike Smith     if (!strcmp(cp, "all"))
13360ae55423SMike Smith 	return(1);
13370ae55423SMike Smith 
13380ae55423SMike Smith     /* scan the disable list checking for a match */
13390ae55423SMike Smith     for (;;) {
13400ae55423SMike Smith 	while ((*cp != 0) && isspace(*cp))
13410ae55423SMike Smith 	    cp++;
13420ae55423SMike Smith 	if (*cp == 0)
13430ae55423SMike Smith 	    break;
13440ae55423SMike Smith 	len = 0;
13450ae55423SMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
13460ae55423SMike Smith 	    len++;
13470ae55423SMike Smith 	if (!strncmp(cp, subsys, len)) {
13480ae55423SMike Smith 	    DEBUG_PRINT(TRACE_OBJECTS, ("disabled '%s'\n", subsys));
134915e32d5dSMike Smith 	    return(1);
135015e32d5dSMike Smith 	}
135115e32d5dSMike Smith 	cp += len;
135215e32d5dSMike Smith     }
135315e32d5dSMike Smith     return(0);
135415e32d5dSMike Smith }
135515e32d5dSMike Smith 
135615e32d5dSMike Smith /*
135715e32d5dSMike Smith  * Control interface.
135815e32d5dSMike Smith  *
13590ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
13600ae55423SMike Smith  * drivers wanting to be accessible via /dev/acpi should use the register/deregister
13610ae55423SMike Smith  * interface to make their handlers visible.
136215e32d5dSMike Smith  */
13630ae55423SMike Smith struct acpi_ioctl_hook
13640ae55423SMike Smith {
13650ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook)	link;
13660ae55423SMike Smith     u_long				cmd;
13670ae55423SMike Smith     int					(* fn)(u_long cmd, caddr_t addr, void *arg);
13680ae55423SMike Smith     void				*arg;
13690ae55423SMike Smith };
13700ae55423SMike Smith 
13710ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
13720ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
13730ae55423SMike Smith 
13740ae55423SMike Smith /*
13750ae55423SMike Smith  * Register an ioctl handler.
13760ae55423SMike Smith  */
13770ae55423SMike Smith int
13780ae55423SMike Smith acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg)
13790ae55423SMike Smith {
13800ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
13810ae55423SMike Smith 
13820ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
13830ae55423SMike Smith 	return(ENOMEM);
13840ae55423SMike Smith     hp->cmd = cmd;
13850ae55423SMike Smith     hp->fn = fn;
13860ae55423SMike Smith     hp->arg = arg;
13870ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
13880ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
13890ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
13900ae55423SMike Smith     }
13910ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
13920ae55423SMike Smith     return(0);
13930ae55423SMike Smith }
13940ae55423SMike Smith 
13950ae55423SMike Smith /*
13960ae55423SMike Smith  * Deregister an ioctl handler.
13970ae55423SMike Smith  */
13980ae55423SMike Smith void
13990ae55423SMike Smith acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg))
14000ae55423SMike Smith {
14010ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
14020ae55423SMike Smith 
14030ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
14040ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
14050ae55423SMike Smith 	    break;
14060ae55423SMike Smith 
14070ae55423SMike Smith     if (hp != NULL) {
14080ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
14090ae55423SMike Smith 	free(hp, M_ACPIDEV);
14100ae55423SMike Smith     }
14110ae55423SMike Smith }
14120ae55423SMike Smith 
141315e32d5dSMike Smith static int
141415e32d5dSMike Smith acpiopen(dev_t dev, int flag, int fmt, struct proc *p)
141515e32d5dSMike Smith {
141615e32d5dSMike Smith     return(0);
141715e32d5dSMike Smith }
141815e32d5dSMike Smith 
141915e32d5dSMike Smith static int
142015e32d5dSMike Smith acpiclose(dev_t dev, int flag, int fmt, struct proc *p)
142115e32d5dSMike Smith {
142215e32d5dSMike Smith     return(0);
142315e32d5dSMike Smith }
142415e32d5dSMike Smith 
142515e32d5dSMike Smith static int
142615e32d5dSMike Smith acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
142715e32d5dSMike Smith {
142815e32d5dSMike Smith     struct acpi_softc		*sc;
14290ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
14300ae55423SMike Smith     int				error, xerror, state;
143115e32d5dSMike Smith 
1432cb9b0d80SMike Smith     ACPI_LOCK;
1433cb9b0d80SMike Smith 
143415e32d5dSMike Smith     error = state = 0;
143515e32d5dSMike Smith     sc = dev->si_drv1;
143615e32d5dSMike Smith 
14370ae55423SMike Smith     /*
14380ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
14390ae55423SMike Smith      */
14400ae55423SMike Smith     if (acpi_ioctl_hooks_initted) {
14410ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
14420ae55423SMike Smith 	    if (hp->cmd == cmd) {
14430ae55423SMike Smith 		xerror = hp->fn(cmd, addr, hp->arg);
14440ae55423SMike Smith 		if (xerror != 0)
14450ae55423SMike Smith 		    error = xerror;
1446917d44c8SMitsuru IWASAKI 		goto out;
14470ae55423SMike Smith 	    }
14480ae55423SMike Smith 	}
14490ae55423SMike Smith     }
14500ae55423SMike Smith 
14510ae55423SMike Smith     /*
14520ae55423SMike Smith      * Core system ioctls.
14530ae55423SMike Smith      */
145415e32d5dSMike Smith     switch (cmd) {
145515e32d5dSMike Smith     case ACPIIO_ENABLE:
14560ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
145715e32d5dSMike Smith 	    error = ENXIO;
145815e32d5dSMike Smith 	break;
145915e32d5dSMike Smith 
146015e32d5dSMike Smith     case ACPIIO_DISABLE:
14610ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
146215e32d5dSMike Smith 	    error = ENXIO;
146315e32d5dSMike Smith 	break;
146415e32d5dSMike Smith 
146515e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
146615e32d5dSMike Smith 	if (!sc->acpi_enabled) {
146715e32d5dSMike Smith 	    error = ENXIO;
146815e32d5dSMike Smith 	    break;
146915e32d5dSMike Smith 	}
147015e32d5dSMike Smith 	state = *(int *)addr;
1471a5d1879bSMitsuru IWASAKI 	if (state >= ACPI_STATE_S0  && state <= ACPI_S_STATES_MAX) {
147215e32d5dSMike Smith 	    acpi_SetSleepState(sc, state);
147315e32d5dSMike Smith 	} else {
147415e32d5dSMike Smith 	    error = EINVAL;
147515e32d5dSMike Smith 	}
147615e32d5dSMike Smith 	break;
147715e32d5dSMike Smith 
147815e32d5dSMike Smith     default:
14790ae55423SMike Smith 	if (error == 0)
148015e32d5dSMike Smith 	    error = EINVAL;
148115e32d5dSMike Smith 	break;
148215e32d5dSMike Smith     }
1483917d44c8SMitsuru IWASAKI 
1484917d44c8SMitsuru IWASAKI out:
1485cb9b0d80SMike Smith     ACPI_UNLOCK;
148615e32d5dSMike Smith     return(error);
148715e32d5dSMike Smith }
148815e32d5dSMike Smith 
14891d073b1dSJohn Baldwin static int
14901d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
14911d073b1dSJohn Baldwin {
14921d073b1dSJohn Baldwin     char sleep_state[10];
14931d073b1dSJohn Baldwin     int error;
14941d073b1dSJohn Baldwin     u_int new_state, old_state;
14951d073b1dSJohn Baldwin 
14961d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
1497cb9b0d80SMike Smith     if (old_state > ACPI_S_STATES_MAX) {
14981d073b1dSJohn Baldwin 	strcpy(sleep_state, "unknown");
1499cb9b0d80SMike Smith     } else {
15001d073b1dSJohn Baldwin 	strncpy(sleep_state, sleep_state_names[old_state],
15011d073b1dSJohn Baldwin 		sizeof(sleep_state_names[old_state]));
1502cb9b0d80SMike Smith     }
15031d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
15041d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
1505cb9b0d80SMike Smith 	for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX; new_state++) {
15061d073b1dSJohn Baldwin 	    if (strncmp(sleep_state, sleep_state_names[new_state],
15071d073b1dSJohn Baldwin 			sizeof(sleep_state)) == 0)
15081d073b1dSJohn Baldwin 		break;
1509cb9b0d80SMike Smith 	}
1510cb9b0d80SMike Smith 	if ((new_state != old_state) && (new_state <= ACPI_S_STATES_MAX)) {
15111d073b1dSJohn Baldwin 	    *(u_int *)oidp->oid_arg1 = new_state;
1512cb9b0d80SMike Smith 	} else {
15131d073b1dSJohn Baldwin 	    error = EINVAL;
15141d073b1dSJohn Baldwin 	}
1515cb9b0d80SMike Smith     }
15161d073b1dSJohn Baldwin     return(error);
15171d073b1dSJohn Baldwin }
15181d073b1dSJohn Baldwin 
151915e32d5dSMike Smith #ifdef ACPI_DEBUG
15200ae55423SMike Smith /*
15210ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
15220ae55423SMike Smith  *
15230ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
15240ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
15250ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
15260ae55423SMike Smith  * prefixing the bit name with !.
15270ae55423SMike Smith  */
152815e32d5dSMike Smith struct debugtag
152915e32d5dSMike Smith {
153015e32d5dSMike Smith     char	*name;
153115e32d5dSMike Smith     UINT32	value;
153215e32d5dSMike Smith };
153315e32d5dSMike Smith 
153415e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
1535c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
1536c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
1537c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
1538c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
1539c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
1540c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
1541c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
1542c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
1543c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
1544c5ba0be4SMike Smith     {"ACPI_POWER",		ACPI_POWER},
1545cb9b0d80SMike Smith     {"ACPI_BUS",		ACPI_BUS},
1546cb9b0d80SMike Smith     {"ACPI_POWER",		ACPI_POWER},
1547cb9b0d80SMike Smith     {"ACPI_EC", 		ACPI_EC},
1548cb9b0d80SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
1549c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
1550c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
1551c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
1552c5ba0be4SMike Smith     {"ACPI_SYSTEM",		ACPI_SYSTEM},
1553cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
1554c5ba0be4SMike Smith     {"ACPI_DEBUGGER",		ACPI_DEBUGGER},
1555c5ba0be4SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
1556c5ba0be4SMike Smith     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
155715e32d5dSMike Smith     {NULL, 0}
155815e32d5dSMike Smith };
155915e32d5dSMike Smith 
156015e32d5dSMike Smith static struct debugtag dbg_level[] = {
1561c5ba0be4SMike Smith     {"ACPI_OK",			ACPI_OK},
1562c5ba0be4SMike Smith     {"ACPI_INFO",		ACPI_INFO},
1563c5ba0be4SMike Smith     {"ACPI_WARN",		ACPI_WARN},
1564c5ba0be4SMike Smith     {"ACPI_ERROR",		ACPI_ERROR},
1565c5ba0be4SMike Smith     {"ACPI_FATAL",		ACPI_FATAL},
1566c5ba0be4SMike Smith     {"ACPI_DEBUG_OBJECT",	ACPI_DEBUG_OBJECT},
1567c5ba0be4SMike Smith     {"ACPI_ALL",		ACPI_ALL},
1568c5ba0be4SMike Smith     {"TRACE_THREADS",		TRACE_THREADS},
1569c5ba0be4SMike Smith     {"TRACE_PARSE",		TRACE_PARSE},
1570c5ba0be4SMike Smith     {"TRACE_DISPATCH",		TRACE_DISPATCH},
1571c5ba0be4SMike Smith     {"TRACE_LOAD",		TRACE_LOAD},
1572c5ba0be4SMike Smith     {"TRACE_EXEC",		TRACE_EXEC},
1573c5ba0be4SMike Smith     {"TRACE_NAMES",		TRACE_NAMES},
1574c5ba0be4SMike Smith     {"TRACE_OPREGION",		TRACE_OPREGION},
1575c5ba0be4SMike Smith     {"TRACE_BFIELD",		TRACE_BFIELD},
1576c5ba0be4SMike Smith     {"TRACE_TRASH",		TRACE_TRASH},
1577c5ba0be4SMike Smith     {"TRACE_TABLES",		TRACE_TABLES},
1578c5ba0be4SMike Smith     {"TRACE_FUNCTIONS",		TRACE_FUNCTIONS},
1579c5ba0be4SMike Smith     {"TRACE_VALUES",		TRACE_VALUES},
1580c5ba0be4SMike Smith     {"TRACE_OBJECTS",		TRACE_OBJECTS},
1581c5ba0be4SMike Smith     {"TRACE_ALLOCATIONS",	TRACE_ALLOCATIONS},
1582c5ba0be4SMike Smith     {"TRACE_RESOURCES",		TRACE_RESOURCES},
1583c5ba0be4SMike Smith     {"TRACE_IO",		TRACE_IO},
1584c5ba0be4SMike Smith     {"TRACE_INTERRUPTS",	TRACE_INTERRUPTS},
1585c5ba0be4SMike Smith     {"TRACE_USER_REQUESTS",	TRACE_USER_REQUESTS},
1586c5ba0be4SMike Smith     {"TRACE_PACKAGE",		TRACE_PACKAGE},
1587c5ba0be4SMike Smith     {"TRACE_MUTEX",		TRACE_MUTEX},
1588c5ba0be4SMike Smith     {"TRACE_INIT",		TRACE_INIT},
1589c5ba0be4SMike Smith     {"TRACE_ALL",		TRACE_ALL},
1590c5ba0be4SMike Smith     {"VERBOSE_AML_DISASSEMBLE",	VERBOSE_AML_DISASSEMBLE},
1591c5ba0be4SMike Smith     {"VERBOSE_INFO",		VERBOSE_INFO},
1592c5ba0be4SMike Smith     {"VERBOSE_TABLES",		VERBOSE_TABLES},
1593c5ba0be4SMike Smith     {"VERBOSE_EVENTS",		VERBOSE_EVENTS},
1594c5ba0be4SMike Smith     {"VERBOSE_ALL",		VERBOSE_ALL},
159515e32d5dSMike Smith     {NULL, 0}
159615e32d5dSMike Smith };
159715e32d5dSMike Smith 
159815e32d5dSMike Smith static void
159915e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
160015e32d5dSMike Smith {
160115e32d5dSMike Smith     char	*ep;
160215e32d5dSMike Smith     int		i, l;
16030ae55423SMike Smith     int		set;
160415e32d5dSMike Smith 
160515e32d5dSMike Smith     while (*cp) {
160615e32d5dSMike Smith 	if (isspace(*cp)) {
160715e32d5dSMike Smith 	    cp++;
160815e32d5dSMike Smith 	    continue;
160915e32d5dSMike Smith 	}
161015e32d5dSMike Smith 	ep = cp;
161115e32d5dSMike Smith 	while (*ep && !isspace(*ep))
161215e32d5dSMike Smith 	    ep++;
16130ae55423SMike Smith 	if (*cp == '!') {
16140ae55423SMike Smith 	    set = 0;
16150ae55423SMike Smith 	    cp++;
16160ae55423SMike Smith 	    if (cp == ep)
16170ae55423SMike Smith 		continue;
16180ae55423SMike Smith 	} else {
16190ae55423SMike Smith 	    set = 1;
16200ae55423SMike Smith 	}
162115e32d5dSMike Smith 	l = ep - cp;
162215e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
162315e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
16240ae55423SMike Smith 		if (set) {
162515e32d5dSMike Smith 		    *flag |= tag[i].value;
16260ae55423SMike Smith 		} else {
16270ae55423SMike Smith 		    *flag &= ~tag[i].value;
16280ae55423SMike Smith 		}
162915e32d5dSMike Smith 		printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
163015e32d5dSMike Smith 	    }
163115e32d5dSMike Smith 	}
163215e32d5dSMike Smith 	cp = ep;
163315e32d5dSMike Smith     }
163415e32d5dSMike Smith }
163515e32d5dSMike Smith 
163615e32d5dSMike Smith static void
16372a4ac806SMike Smith acpi_set_debugging(void *junk)
163815e32d5dSMike Smith {
163915e32d5dSMike Smith     char	*cp;
164015e32d5dSMike Smith 
16410ae55423SMike Smith     AcpiDbgLayer = 0;
16420ae55423SMike Smith     AcpiDbgLevel = 0;
164315e32d5dSMike Smith     if ((cp = getenv("debug.acpi.layer")) != NULL)
164415e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
164515e32d5dSMike Smith     if ((cp = getenv("debug.acpi.level")) != NULL)
164615e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
16470ae55423SMike Smith 
16480ae55423SMike Smith     printf("ACPI debug layer 0x%x  debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel);
164915e32d5dSMike Smith }
16502a4ac806SMike Smith SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL);
165115e32d5dSMike Smith #endif
1652