xref: /freebsd/sys/dev/acpica/acpi.c (revision 13d4f7bae32fcad03df555fc8a138db3eacb06ec)
115e32d5dSMike Smith /*-
215e32d5dSMike Smith  * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org>
315e32d5dSMike Smith  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
415e32d5dSMike Smith  * Copyright (c) 2000 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>
3515e32d5dSMike Smith #include <sys/malloc.h>
36b2c9c0daSMitsuru IWASAKI #include <sys/mutex.h>
3715e32d5dSMike Smith #include <sys/bus.h>
3815e32d5dSMike Smith #include <sys/conf.h>
3915e32d5dSMike Smith #include <sys/ioccom.h>
4015e32d5dSMike Smith #include <sys/reboot.h>
4115e32d5dSMike Smith #include <sys/sysctl.h>
4215e32d5dSMike Smith #include <sys/ctype.h>
4315e32d5dSMike Smith 
4415e32d5dSMike Smith #include <machine/clock.h>
4515e32d5dSMike Smith 
4615e32d5dSMike Smith #include <machine/resource.h>
4715e32d5dSMike Smith 
4815e32d5dSMike Smith #include "acpi.h"
4915e32d5dSMike Smith 
5015e32d5dSMike Smith #include <dev/acpica/acpivar.h>
5115e32d5dSMike Smith #include <dev/acpica/acpiio.h>
5215e32d5dSMike Smith 
5315e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
5415e32d5dSMike Smith 
5515e32d5dSMike Smith /*
560ae55423SMike Smith  * Hooks for the ACPI CA debugging infrastructure
570ae55423SMike Smith  */
580ae55423SMike Smith #define _COMPONENT	BUS_MANAGER
590ae55423SMike Smith MODULE_NAME("ACPI")
600ae55423SMike Smith 
610ae55423SMike Smith /*
6215e32d5dSMike Smith  * Character device
6315e32d5dSMike Smith  */
6415e32d5dSMike Smith 
6515e32d5dSMike Smith static d_open_t		acpiopen;
6615e32d5dSMike Smith static d_close_t	acpiclose;
6715e32d5dSMike Smith static d_ioctl_t	acpiioctl;
6815e32d5dSMike Smith 
6915e32d5dSMike Smith #define CDEV_MAJOR 152
7015e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
7115e32d5dSMike Smith     acpiopen,
7215e32d5dSMike Smith     acpiclose,
7315e32d5dSMike Smith     noread,
7415e32d5dSMike Smith     nowrite,
7515e32d5dSMike Smith     acpiioctl,
7615e32d5dSMike Smith     nopoll,
7715e32d5dSMike Smith     nommap,
7815e32d5dSMike Smith     nostrategy,
7915e32d5dSMike Smith     "acpi",
8015e32d5dSMike Smith     CDEV_MAJOR,
8115e32d5dSMike Smith     nodump,
8215e32d5dSMike Smith     nopsize,
8315e32d5dSMike Smith     0,
8415e32d5dSMike Smith     -1
8515e32d5dSMike Smith };
8615e32d5dSMike Smith 
8715e32d5dSMike Smith static void	acpi_identify(driver_t *driver, device_t parent);
8815e32d5dSMike Smith static int	acpi_probe(device_t dev);
8915e32d5dSMike Smith static int	acpi_attach(device_t dev);
9015e32d5dSMike Smith static device_t	acpi_add_child(device_t bus, int order, const char *name, int unit);
9115e32d5dSMike Smith static int	acpi_print_resources(struct resource_list *rl, const char *name, int type,
9215e32d5dSMike Smith 				     const char *format);
9315e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
9415e32d5dSMike Smith static int	acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result);
9515e32d5dSMike Smith static int	acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value);
9615e32d5dSMike Smith static int	acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start,
9715e32d5dSMike Smith 				  u_long count);
9815e32d5dSMike Smith static int	acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp,
9915e32d5dSMike Smith 				  u_long *countp);
10015e32d5dSMike Smith static struct resource *acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
10115e32d5dSMike Smith 					    u_long start, u_long end, u_long count, u_int flags);
10215e32d5dSMike Smith static int	acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r);
10315e32d5dSMike Smith 
10415e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
10515e32d5dSMike Smith static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status);
10615e32d5dSMike Smith 
10715e32d5dSMike Smith static void	acpi_shutdown_pre_sync(void *arg, int howto);
10815e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
10915e32d5dSMike Smith 
11013d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
11113d4f7baSMitsuru IWASAKI 
11215e32d5dSMike Smith #ifdef ACPI_DEBUG
11315e32d5dSMike Smith static void	acpi_set_debugging(void);
11415e32d5dSMike Smith #endif
11515e32d5dSMike Smith 
11615e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
11715e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
11815e32d5dSMike Smith 
11915e32d5dSMike Smith static device_method_t acpi_methods[] = {
12015e32d5dSMike Smith     /* Device interface */
12115e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
12215e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
12315e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
12415e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
12515e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
12615e32d5dSMike Smith     DEVMETHOD(device_resume,		bus_generic_resume),
12715e32d5dSMike Smith 
12815e32d5dSMike Smith     /* Bus interface */
12915e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
13015e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
13115e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
13215e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
13315e32d5dSMike Smith     DEVMETHOD(bus_set_resource,		acpi_set_resource),
13415e32d5dSMike Smith     DEVMETHOD(bus_get_resource,		acpi_get_resource),
13515e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
13615e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
13715e32d5dSMike Smith     DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
13815e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
13915e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
14015e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
14115e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
14215e32d5dSMike Smith 
14315e32d5dSMike Smith     {0, 0}
14415e32d5dSMike Smith };
14515e32d5dSMike Smith 
14615e32d5dSMike Smith static driver_t acpi_driver = {
14715e32d5dSMike Smith     "acpi",
14815e32d5dSMike Smith     acpi_methods,
14915e32d5dSMike Smith     sizeof(struct acpi_softc),
15015e32d5dSMike Smith };
15115e32d5dSMike Smith 
15215e32d5dSMike Smith devclass_t acpi_devclass;
15315e32d5dSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, 0, 0);
15415e32d5dSMike Smith 
15515e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, "");
15615e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, "");
15715e32d5dSMike Smith 
15815e32d5dSMike Smith /*
15915e32d5dSMike Smith  * Detect ACPI, perform early initialisation
16015e32d5dSMike Smith  */
16115e32d5dSMike Smith static void
16215e32d5dSMike Smith acpi_identify(driver_t *driver, device_t parent)
16315e32d5dSMike Smith {
16415e32d5dSMike Smith     device_t			child;
165042283a6SMike Smith     ACPI_PHYSICAL_ADDRESS	rsdp;
16615e32d5dSMike Smith     int				error;
16715e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
16815e32d5dSMike Smith     char			*debugpoint = getenv("debug.acpi.debugger");
16915e32d5dSMike Smith #endif
17015e32d5dSMike Smith 
1710ae55423SMike Smith     FUNCTION_TRACE(__FUNCTION__);
1720ae55423SMike Smith 
173840f9b53STakanori Watanabe     if(!cold){
174840f9b53STakanori Watanabe 	    printf("Don't load this driver from userland!!\n");
175840f9b53STakanori Watanabe 	    return ;
176840f9b53STakanori Watanabe     }
177840f9b53STakanori Watanabe 
17815e32d5dSMike Smith     /*
17915e32d5dSMike Smith      * Make sure we're not being doubly invoked.
18015e32d5dSMike Smith      */
18115e32d5dSMike Smith     if (device_find_child(parent, "acpi", 0) != NULL)
1820ae55423SMike Smith 	return_VOID;
18315e32d5dSMike Smith 
18415e32d5dSMike Smith #ifdef ACPI_DEBUG
18515e32d5dSMike Smith     acpi_set_debugging();
18615e32d5dSMike Smith #endif
18715e32d5dSMike Smith 
18815e32d5dSMike Smith     /*
18915e32d5dSMike Smith      * Start up ACPICA
19015e32d5dSMike Smith      */
19115e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
19215e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "init"))
19315e32d5dSMike Smith 	acpi_EnterDebugger();
19415e32d5dSMike Smith #endif
19515e32d5dSMike Smith     if ((error = AcpiInitializeSubsystem()) != AE_OK) {
19615e32d5dSMike Smith 	printf("ACPI: initialisation failed: %s\n", acpi_strerror(error));
1970ae55423SMike Smith 	return_VOID;
19815e32d5dSMike Smith     }
19915e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
20015e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "tables"))
20115e32d5dSMike Smith 	acpi_EnterDebugger();
20215e32d5dSMike Smith #endif
20315e32d5dSMike Smith     if (((error = AcpiFindRootPointer(&rsdp)) != AE_OK) ||
20415e32d5dSMike Smith 	((error = AcpiLoadTables(rsdp)) != AE_OK)) {
20515e32d5dSMike Smith 	printf("ACPI: table load failed: %s\n", acpi_strerror(error));
2060ae55423SMike Smith 	return_VOID;
20715e32d5dSMike Smith     }
20815e32d5dSMike Smith 
20915e32d5dSMike Smith     /*
21015e32d5dSMike Smith      * Attach the actual ACPI device.
21115e32d5dSMike Smith      */
21215e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
21315e32d5dSMike Smith 	    device_printf(parent, "ACPI: could not attach\n");
2140ae55423SMike Smith 	    return_VOID;
21515e32d5dSMike Smith     }
21615e32d5dSMike Smith }
21715e32d5dSMike Smith 
21815e32d5dSMike Smith /*
21915e32d5dSMike Smith  * Fetch some descriptive data from ACPI to put in our attach message
22015e32d5dSMike Smith  */
22115e32d5dSMike Smith static int
22215e32d5dSMike Smith acpi_probe(device_t dev)
22315e32d5dSMike Smith {
22415e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
22515e32d5dSMike Smith     char		buf[20];
22615e32d5dSMike Smith     int			error;
22715e32d5dSMike Smith 
2280ae55423SMike Smith     FUNCTION_TRACE(__FUNCTION__);
2290ae55423SMike Smith 
230042283a6SMike Smith     if ((error = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th)) != AE_OK) {
231042283a6SMike Smith 	device_printf(dev, "couldn't get XSDT header: %s\n", acpi_strerror(error));
2320ae55423SMike Smith 	return_VALUE(ENXIO);
23315e32d5dSMike Smith     }
23415e32d5dSMike Smith     sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId);
23515e32d5dSMike Smith     device_set_desc_copy(dev, buf);
23615e32d5dSMike Smith 
2370ae55423SMike Smith     return_VALUE(0);
23815e32d5dSMike Smith }
23915e32d5dSMike Smith 
24015e32d5dSMike Smith static int
24115e32d5dSMike Smith acpi_attach(device_t dev)
24215e32d5dSMike Smith {
24315e32d5dSMike Smith     struct acpi_softc	*sc;
24415e32d5dSMike Smith     int			error;
24515e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
24615e32d5dSMike Smith     char		*debugpoint = getenv("debug.acpi.debugger");
24715e32d5dSMike Smith #endif
24815e32d5dSMike Smith 
2490ae55423SMike Smith     FUNCTION_TRACE(__FUNCTION__);
25015e32d5dSMike Smith 
25115e32d5dSMike Smith     sc = device_get_softc(dev);
25215e32d5dSMike Smith     bzero(sc, sizeof(*sc));
25315e32d5dSMike Smith     sc->acpi_dev = dev;
25415e32d5dSMike Smith 
25515e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
25615e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "spaces"))
25715e32d5dSMike Smith 	acpi_EnterDebugger();
25815e32d5dSMike Smith #endif
25915e32d5dSMike Smith 
26015e32d5dSMike Smith     /*
26115e32d5dSMike Smith      * Install the default address space handlers.
26215e32d5dSMike Smith      */
26315e32d5dSMike Smith     if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
26415e32d5dSMike Smith 						ADDRESS_SPACE_SYSTEM_MEMORY,
26515e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
26615e32d5dSMike Smith 						NULL, NULL)) != AE_OK) {
26715e32d5dSMike Smith 	device_printf(dev, "could not initialise SystemMemory handler: %s\n", acpi_strerror(error));
2680ae55423SMike Smith 	return_VALUE(ENXIO);
26915e32d5dSMike Smith     }
27015e32d5dSMike Smith     if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
27115e32d5dSMike Smith 						ADDRESS_SPACE_SYSTEM_IO,
27215e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
27315e32d5dSMike Smith 						NULL, NULL)) != AE_OK) {
27415e32d5dSMike Smith 	device_printf(dev, "could not initialise SystemIO handler: %s\n", acpi_strerror(error));
2750ae55423SMike Smith 	return_VALUE(ENXIO);
27615e32d5dSMike Smith     }
27715e32d5dSMike Smith     if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
27815e32d5dSMike Smith 						ADDRESS_SPACE_PCI_CONFIG,
27915e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
28015e32d5dSMike Smith 						NULL, NULL)) != AE_OK) {
28115e32d5dSMike Smith 	device_printf(dev, "could not initialise PciConfig handler: %s\n", acpi_strerror(error));
2820ae55423SMike Smith 	return_VALUE(ENXIO);
28315e32d5dSMike Smith     }
28415e32d5dSMike Smith 
28515e32d5dSMike Smith     /*
28615e32d5dSMike Smith      * Bring ACPI fully online.
28715e32d5dSMike Smith      *
28815e32d5dSMike Smith      * Note that we request that device _STA and _INI methods not be run (ACPI_NO_DEVICE_INIT)
28915e32d5dSMike Smith      * and the final object initialisation pass be skipped (ACPI_NO_OBJECT_INIT).
29015e32d5dSMike Smith      *
29115e32d5dSMike Smith      * XXX We need to arrange for the object init pass after we have attached all our
29215e32d5dSMike Smith      *     child devices.
29315e32d5dSMike Smith      */
29415e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
29515e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "enable"))
29615e32d5dSMike Smith 	acpi_EnterDebugger();
29715e32d5dSMike Smith #endif
29815e32d5dSMike Smith     if ((error = AcpiEnableSubsystem(ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT)) != AE_OK) {
29915e32d5dSMike Smith 	device_printf(dev, "could not enable ACPI: %s\n", acpi_strerror(error));
3000ae55423SMike Smith 	return_VALUE(ENXIO);
30115e32d5dSMike Smith     }
30215e32d5dSMike Smith 
30315e32d5dSMike Smith     /*
30415e32d5dSMike Smith      * Dispatch the default sleep state to devices.
30515e32d5dSMike Smith      * TBD: should be configured from userland policy manager.
30615e32d5dSMike Smith      */
30715e32d5dSMike Smith     sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
30815e32d5dSMike Smith     sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
30915e32d5dSMike Smith     sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
31015e32d5dSMike Smith 
31113d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
31215e32d5dSMike Smith 
31315e32d5dSMike Smith     /*
31415e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
31515e32d5dSMike Smith      */
31615e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
31715e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "probe"))
31815e32d5dSMike Smith 	acpi_EnterDebugger();
31915e32d5dSMike Smith #endif
3200ae55423SMike Smith     if (!acpi_disabled("bus"))
32115e32d5dSMike Smith 	acpi_probe_children(dev);
32215e32d5dSMike Smith 
32315e32d5dSMike Smith     /*
32415e32d5dSMike Smith      * Register our shutdown handlers
32515e32d5dSMike Smith      */
32615e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST);
32715e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST);
32815e32d5dSMike Smith 
32915e32d5dSMike Smith     /*
33015e32d5dSMike Smith      * Register our acpi event handlers.
33115e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
33215e32d5dSMike Smith      */
33315e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST);
33415e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST);
33515e32d5dSMike Smith 
33615e32d5dSMike Smith     /*
33715e32d5dSMike Smith      * Flag our initial states.
33815e32d5dSMike Smith      */
33915e32d5dSMike Smith     sc->acpi_enabled = 1;
34015e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
34115e32d5dSMike Smith 
34215e32d5dSMike Smith     /*
34315e32d5dSMike Smith      * Create the control device
34415e32d5dSMike Smith      */
34515e32d5dSMike Smith     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, 0, 5, 0660, "acpi");
34615e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
34715e32d5dSMike Smith 
34815e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
34915e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "running"))
35015e32d5dSMike Smith 	acpi_EnterDebugger();
35115e32d5dSMike Smith #endif
3520ae55423SMike Smith     return_VALUE(0);
35315e32d5dSMike Smith }
35415e32d5dSMike Smith 
35515e32d5dSMike Smith /*
35615e32d5dSMike Smith  * Handle a new device being added
35715e32d5dSMike Smith  */
35815e32d5dSMike Smith static device_t
35915e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
36015e32d5dSMike Smith {
36115e32d5dSMike Smith     struct acpi_device	*ad;
36215e32d5dSMike Smith     device_t		child;
36315e32d5dSMike Smith 
36415e32d5dSMike Smith     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL)
36515e32d5dSMike Smith 	return(NULL);
36615e32d5dSMike Smith     bzero(ad, sizeof(*ad));
36715e32d5dSMike Smith 
36815e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
36915e32d5dSMike Smith 
37015e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
37115e32d5dSMike Smith     if (child != NULL)
37215e32d5dSMike Smith 	device_set_ivars(child, ad);
37315e32d5dSMike Smith     return(child);
37415e32d5dSMike Smith }
37515e32d5dSMike Smith 
37615e32d5dSMike Smith /*
37715e32d5dSMike Smith  * Print child device resource usage
37815e32d5dSMike Smith  */
37915e32d5dSMike Smith static int
38015e32d5dSMike Smith acpi_print_resources(struct resource_list *rl, const char *name, int type, const char *format)
38115e32d5dSMike Smith {
38215e32d5dSMike Smith     struct resource_list_entry	*rle;
38315e32d5dSMike Smith     int				printed, retval;
38415e32d5dSMike Smith 
38515e32d5dSMike Smith     printed = 0;
38615e32d5dSMike Smith     retval = 0;
38715e32d5dSMike Smith 
38815e32d5dSMike Smith     if (!SLIST_FIRST(rl))
38915e32d5dSMike Smith 	return(0);
39015e32d5dSMike Smith 
39115e32d5dSMike Smith     /* Yes, this is kinda cheating */
39215e32d5dSMike Smith     SLIST_FOREACH(rle, rl, link) {
39315e32d5dSMike Smith 	if (rle->type == type) {
39415e32d5dSMike Smith 	    if (printed == 0)
39515e32d5dSMike Smith 		retval += printf(" %s ", name);
39615e32d5dSMike Smith 	    else if (printed > 0)
39715e32d5dSMike Smith 		retval += printf(",");
39815e32d5dSMike Smith 	    printed++;
39915e32d5dSMike Smith 	    retval += printf(format, rle->start);
40015e32d5dSMike Smith 	    if (rle->count > 1) {
40115e32d5dSMike Smith 		retval += printf("-");
40215e32d5dSMike Smith 		retval += printf(format, rle->start +
40315e32d5dSMike Smith 				 rle->count - 1);
40415e32d5dSMike Smith 	    }
40515e32d5dSMike Smith 	}
40615e32d5dSMike Smith     }
40715e32d5dSMike Smith     return(retval);
40815e32d5dSMike Smith }
40915e32d5dSMike Smith 
41015e32d5dSMike Smith static int
41115e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
41215e32d5dSMike Smith {
41315e32d5dSMike Smith     struct acpi_device		*adev = device_get_ivars(child);
41415e32d5dSMike Smith     struct resource_list	*rl = &adev->ad_rl;
41515e32d5dSMike Smith     int retval = 0;
41615e32d5dSMike Smith 
41715e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
41815e32d5dSMike Smith     retval += acpi_print_resources(rl, "port",  SYS_RES_IOPORT, "%#lx");
41915e32d5dSMike Smith     retval += acpi_print_resources(rl, "iomem", SYS_RES_MEMORY, "%#lx");
42015e32d5dSMike Smith     retval += acpi_print_resources(rl, "irq",   SYS_RES_IRQ,    "%ld");
42115e32d5dSMike Smith     retval += bus_print_child_footer(bus, child);
42215e32d5dSMike Smith 
42315e32d5dSMike Smith     return(retval);
42415e32d5dSMike Smith }
42515e32d5dSMike Smith 
42615e32d5dSMike Smith 
42715e32d5dSMike Smith /*
42815e32d5dSMike Smith  * Handle per-device ivars
42915e32d5dSMike Smith  */
43015e32d5dSMike Smith static int
43115e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
43215e32d5dSMike Smith {
43315e32d5dSMike Smith     struct acpi_device	*ad;
43415e32d5dSMike Smith 
43515e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
43615e32d5dSMike Smith 	printf("device has no ivars\n");
43715e32d5dSMike Smith 	return(ENOENT);
43815e32d5dSMike Smith     }
43915e32d5dSMike Smith 
44015e32d5dSMike Smith     switch(index) {
44115e32d5dSMike Smith 	/* ACPI ivars */
44215e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
44315e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
44415e32d5dSMike Smith 	break;
44515e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
44615e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
44715e32d5dSMike Smith 	break;
44815e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
44915e32d5dSMike Smith 	*(void **)result = ad->ad_private;
45015e32d5dSMike Smith 	break;
45115e32d5dSMike Smith 
45215e32d5dSMike Smith     default:
45315e32d5dSMike Smith 	panic("bad ivar read request (%d)\n", index);
45415e32d5dSMike Smith 	return(ENOENT);
45515e32d5dSMike Smith     }
45615e32d5dSMike Smith     return(0);
45715e32d5dSMike Smith }
45815e32d5dSMike Smith 
45915e32d5dSMike Smith static int
46015e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
46115e32d5dSMike Smith {
46215e32d5dSMike Smith     struct acpi_device	*ad;
46315e32d5dSMike Smith 
46415e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
46515e32d5dSMike Smith 	printf("device has no ivars\n");
46615e32d5dSMike Smith 	return(ENOENT);
46715e32d5dSMike Smith     }
46815e32d5dSMike Smith 
46915e32d5dSMike Smith     switch(index) {
47015e32d5dSMike Smith 	/* ACPI ivars */
47115e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
47215e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
47315e32d5dSMike Smith 	break;
47415e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
47515e32d5dSMike Smith 	ad->ad_magic = (int )value;
47615e32d5dSMike Smith 	break;
47715e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
47815e32d5dSMike Smith 	ad->ad_private = (void *)value;
47915e32d5dSMike Smith 	break;
48015e32d5dSMike Smith 
48115e32d5dSMike Smith     default:
48215e32d5dSMike Smith 	panic("bad ivar write request (%d)\n", index);
48315e32d5dSMike Smith 	return(ENOENT);
48415e32d5dSMike Smith     }
48515e32d5dSMike Smith     return(0);
48615e32d5dSMike Smith }
48715e32d5dSMike Smith 
48815e32d5dSMike Smith /*
48915e32d5dSMike Smith  * Handle child resource allocation/removal
49015e32d5dSMike Smith  */
49115e32d5dSMike Smith static int
49215e32d5dSMike Smith acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
49315e32d5dSMike Smith {
49415e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
49515e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
49615e32d5dSMike Smith 
49715e32d5dSMike Smith     resource_list_add(rl, type, rid, start, start + count -1, count);
49815e32d5dSMike Smith 
49915e32d5dSMike Smith     return(0);
50015e32d5dSMike Smith }
50115e32d5dSMike Smith 
50215e32d5dSMike Smith static int
50315e32d5dSMike Smith acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
50415e32d5dSMike Smith {
50515e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
50615e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
50715e32d5dSMike Smith     struct resource_list_entry	*rle;
50815e32d5dSMike Smith 
50915e32d5dSMike Smith     rle = resource_list_find(rl, type, rid);
51015e32d5dSMike Smith     if (!rle)
51115e32d5dSMike Smith 	return(ENOENT);
51215e32d5dSMike Smith 
51315e32d5dSMike Smith     if (startp)
51415e32d5dSMike Smith 	*startp = rle->start;
51515e32d5dSMike Smith     if (countp)
51615e32d5dSMike Smith 	*countp = rle->count;
51715e32d5dSMike Smith 
51815e32d5dSMike Smith     return(0);
51915e32d5dSMike Smith }
52015e32d5dSMike Smith 
52115e32d5dSMike Smith static struct resource *
52215e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
52315e32d5dSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
52415e32d5dSMike Smith {
52515e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
52615e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
52715e32d5dSMike Smith 
52815e32d5dSMike Smith     return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags));
52915e32d5dSMike Smith }
53015e32d5dSMike Smith 
53115e32d5dSMike Smith static int
53215e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
53315e32d5dSMike Smith {
53415e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
53515e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
53615e32d5dSMike Smith 
53715e32d5dSMike Smith     return(resource_list_release(rl, bus, child, type, rid, r));
53815e32d5dSMike Smith }
53915e32d5dSMike Smith 
54015e32d5dSMike Smith /*
54115e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
54215e32d5dSMike Smith  *
54315e32d5dSMike Smith  * Note that we only expect to find devices in the \_TZ_, \_SI_ and \_SB_ scopes,
54415e32d5dSMike Smith  * and \_TZ_ becomes obsolete in the ACPI 2.0 spec.
54515e32d5dSMike Smith  */
54615e32d5dSMike Smith static void
54715e32d5dSMike Smith acpi_probe_children(device_t bus)
54815e32d5dSMike Smith {
54915e32d5dSMike Smith     ACPI_HANDLE		parent;
55015e32d5dSMike Smith     static char		*scopes[] = {"\\_TZ_", "\\_SI", "\\_SB_", NULL};
55115e32d5dSMike Smith     int			i;
55215e32d5dSMike Smith 
5530ae55423SMike Smith     FUNCTION_TRACE(__FUNCTION__);
5540ae55423SMike Smith 
55515e32d5dSMike Smith     /*
55615e32d5dSMike Smith      * Create any static children by calling device identify methods.
55715e32d5dSMike Smith      */
5580ae55423SMike Smith     DEBUG_PRINT(TRACE_OBJECTS, ("device identify routines\n"));
55915e32d5dSMike Smith     bus_generic_probe(bus);
56015e32d5dSMike Smith 
56115e32d5dSMike Smith     /*
56215e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
56315e32d5dSMike Smith      * we find.
56415e32d5dSMike Smith      *
56515e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
56615e32d5dSMike Smith      * we want to create nodes for all devices, not just those that are currently
56715e32d5dSMike Smith      * present. (This assumes that we don't want to create/remove devices as they
56815e32d5dSMike Smith      * appear, which might be smarter.)
56915e32d5dSMike Smith      */
5700ae55423SMike Smith     DEBUG_PRINT(TRACE_OBJECTS, ("namespace scan\n"));
57115e32d5dSMike Smith     for (i = 0; scopes[i] != NULL; i++)
57215e32d5dSMike Smith 	if ((AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent)) == AE_OK)
57315e32d5dSMike Smith 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL);
57415e32d5dSMike Smith 
57515e32d5dSMike Smith     /*
57615e32d5dSMike Smith      * Scan all of the child devices we have created and let them probe/attach.
57715e32d5dSMike Smith      */
5780ae55423SMike Smith     DEBUG_PRINT(TRACE_OBJECTS, ("first bus_generic_attach\n"));
57915e32d5dSMike Smith     bus_generic_attach(bus);
58015e32d5dSMike Smith 
58115e32d5dSMike Smith     /*
58215e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
58315e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
58415e32d5dSMike Smith      */
5850ae55423SMike Smith     DEBUG_PRINT(TRACE_OBJECTS, ("second bus_generic_attach\n"));
58615e32d5dSMike Smith     bus_generic_attach(bus);
5870ae55423SMike Smith 
5880ae55423SMike Smith     return_VOID;
58915e32d5dSMike Smith }
59015e32d5dSMike Smith 
59115e32d5dSMike Smith /*
59215e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
59315e32d5dSMike Smith  * it.
59415e32d5dSMike Smith  */
59515e32d5dSMike Smith static ACPI_STATUS
59615e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
59715e32d5dSMike Smith {
59815e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
59915e32d5dSMike Smith     device_t		child, bus = (device_t)context;
60015e32d5dSMike Smith 
6010ae55423SMike Smith     FUNCTION_TRACE(__FUNCTION__);
6020ae55423SMike Smith 
6030ae55423SMike Smith     /*
6040ae55423SMike Smith      * Skip this device if we think we'll have trouble with it.
6050ae55423SMike Smith      */
6060ae55423SMike Smith     if (acpi_avoid(handle))
6070ae55423SMike Smith 	return_ACPI_STATUS(AE_OK);
6080ae55423SMike Smith 
60915e32d5dSMike Smith     if (AcpiGetType(handle, &type) == AE_OK) {
61015e32d5dSMike Smith 	switch(type) {
61115e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
61215e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
61315e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
61415e32d5dSMike Smith 	case ACPI_TYPE_POWER:
6150ae55423SMike Smith 	    if (acpi_disabled("children"))
6160ae55423SMike Smith 		break;
61715e32d5dSMike Smith 	    /*
61815e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
61915e32d5dSMike Smith 	     * so that the probe/attach passes will run breadth-first.
62015e32d5dSMike Smith 	     */
6210ae55423SMike Smith 	    DEBUG_PRINT(TRACE_OBJECTS, ("scanning '%s'\n", acpi_name(handle)))
62215e32d5dSMike Smith 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
62315e32d5dSMike Smith 	    acpi_set_handle(child, handle);
6240ae55423SMike Smith 	    DEBUG_EXEC(device_probe_and_attach(child));
6250ae55423SMike Smith 	    break;
62615e32d5dSMike Smith 	}
62715e32d5dSMike Smith     }
6280ae55423SMike Smith     return_ACPI_STATUS(AE_OK);
62915e32d5dSMike Smith }
63015e32d5dSMike Smith 
63115e32d5dSMike Smith static void
63215e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto)
63315e32d5dSMike Smith {
63415e32d5dSMike Smith     /*
6350ae55423SMike Smith      * Disable all ACPI events before soft off, otherwise the system
63615e32d5dSMike Smith      * will be turned on again on some laptops.
63715e32d5dSMike Smith      *
63815e32d5dSMike Smith      * XXX this should probably be restricted to masking some events just
63915e32d5dSMike Smith      *     before powering down, since we may still need ACPI during the
64015e32d5dSMike Smith      *     shutdown process.
64115e32d5dSMike Smith      */
64215e32d5dSMike Smith     acpi_Disable((struct acpi_softc *)arg);
64315e32d5dSMike Smith }
64415e32d5dSMike Smith 
64515e32d5dSMike Smith static void
64615e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
64715e32d5dSMike Smith {
64815e32d5dSMike Smith     ACPI_STATUS	status;
64915e32d5dSMike Smith 
6505f3e4175SMitsuru IWASAKI     if (howto & RB_POWEROFF) {
65115e32d5dSMike Smith 	printf("Power system off using ACPI...\n");
65215e32d5dSMike Smith 	if ((status = AcpiSetSystemSleepState(ACPI_STATE_S5)) != AE_OK) {
65315e32d5dSMike Smith 	    printf("ACPI power-off failed - %s\n", acpi_strerror(status));
65415e32d5dSMike Smith 	} else {
65515e32d5dSMike Smith 	    DELAY(1000000);
65615e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
65715e32d5dSMike Smith 	}
65815e32d5dSMike Smith     }
65915e32d5dSMike Smith }
66015e32d5dSMike Smith 
66113d4f7baSMitsuru IWASAKI static void
66213d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
66313d4f7baSMitsuru IWASAKI {
66413d4f7baSMitsuru IWASAKI 	static int	first_time = 1;
66513d4f7baSMitsuru IWASAKI #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n"
66613d4f7baSMitsuru IWASAKI 
66713d4f7baSMitsuru IWASAKI 	/* Enable and clear fixed events and install handlers. */
66813d4f7baSMitsuru IWASAKI 	if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
66913d4f7baSMitsuru IWASAKI 		AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED);
67013d4f7baSMitsuru IWASAKI 		AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED);
67113d4f7baSMitsuru IWASAKI 		AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
67213d4f7baSMitsuru IWASAKI 		    acpi_eventhandler_power_button_for_sleep, sc);
67313d4f7baSMitsuru IWASAKI 		if (first_time) {
67413d4f7baSMitsuru IWASAKI 			device_printf(sc->acpi_dev, MSGFORMAT, "power");
67513d4f7baSMitsuru IWASAKI 		}
67613d4f7baSMitsuru IWASAKI 	}
67713d4f7baSMitsuru IWASAKI 	if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
67813d4f7baSMitsuru IWASAKI 		AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED);
67913d4f7baSMitsuru IWASAKI 		AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED);
68013d4f7baSMitsuru IWASAKI 		AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
68113d4f7baSMitsuru IWASAKI 		    acpi_eventhandler_sleep_button_for_sleep, sc);
68213d4f7baSMitsuru IWASAKI 		if (first_time) {
68313d4f7baSMitsuru IWASAKI 			device_printf(sc->acpi_dev, MSGFORMAT, "sleep");
68413d4f7baSMitsuru IWASAKI 		}
68513d4f7baSMitsuru IWASAKI 	}
68613d4f7baSMitsuru IWASAKI 
68713d4f7baSMitsuru IWASAKI 	first_time = 0;
68813d4f7baSMitsuru IWASAKI }
68913d4f7baSMitsuru IWASAKI 
69015e32d5dSMike Smith /*
69115e32d5dSMike Smith  * Match a HID string against a device
69215e32d5dSMike Smith  */
69315e32d5dSMike Smith BOOLEAN
69415e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid)
69515e32d5dSMike Smith {
69615e32d5dSMike Smith     ACPI_HANDLE		h;
69715e32d5dSMike Smith     ACPI_DEVICE_INFO	devinfo;
69815e32d5dSMike Smith     ACPI_STATUS		error;
69915e32d5dSMike Smith 
7004d332989SMike Smith     if (hid == NULL)
70115e32d5dSMike Smith 	return(FALSE);
70215e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
70315e32d5dSMike Smith 	return(FALSE);
70415e32d5dSMike Smith     if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK)
70515e32d5dSMike Smith 	return(FALSE);
7064d332989SMike Smith     if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId))
70715e32d5dSMike Smith 	return(TRUE);
70815e32d5dSMike Smith     return(FALSE);
70915e32d5dSMike Smith }
71015e32d5dSMike Smith 
71115e32d5dSMike Smith /*
71215e32d5dSMike Smith  * Perform the tedious double-get procedure required for fetching something into
71315e32d5dSMike Smith  * an ACPI_BUFFER that has not been initialised.
71415e32d5dSMike Smith  */
71515e32d5dSMike Smith ACPI_STATUS
71615e32d5dSMike Smith acpi_GetIntoBuffer(ACPI_HANDLE handle, ACPI_STATUS (*func)(ACPI_HANDLE, ACPI_BUFFER *), ACPI_BUFFER *buf)
71715e32d5dSMike Smith {
71815e32d5dSMike Smith     ACPI_STATUS	status;
71915e32d5dSMike Smith 
72015e32d5dSMike Smith     buf->Length = 0;
72115e32d5dSMike Smith     buf->Pointer = NULL;
72215e32d5dSMike Smith 
72315e32d5dSMike Smith     if ((status = func(handle, buf)) != AE_BUFFER_OVERFLOW)
72415e32d5dSMike Smith 	return(status);
72515e32d5dSMike Smith     if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL)
72615e32d5dSMike Smith 	return(AE_NO_MEMORY);
72715e32d5dSMike Smith     return(func(handle, buf));
72815e32d5dSMike Smith }
72915e32d5dSMike Smith 
73015e32d5dSMike Smith /*
73115e32d5dSMike Smith  * Allocate a buffer with a preset data size.
73215e32d5dSMike Smith  */
73315e32d5dSMike Smith ACPI_BUFFER *
73415e32d5dSMike Smith acpi_AllocBuffer(int size)
73515e32d5dSMike Smith {
73615e32d5dSMike Smith     ACPI_BUFFER	*buf;
73715e32d5dSMike Smith 
73815e32d5dSMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
73915e32d5dSMike Smith 	return(NULL);
74015e32d5dSMike Smith     buf->Length = size;
74115e32d5dSMike Smith     buf->Pointer = (void *)(buf + 1);
74215e32d5dSMike Smith     return(buf);
74315e32d5dSMike Smith }
74415e32d5dSMike Smith 
74515e32d5dSMike Smith /*
74615e32d5dSMike Smith  * Set the system sleep state
74715e32d5dSMike Smith  *
74815e32d5dSMike Smith  * Currently we only support S1 and S5
74915e32d5dSMike Smith  */
75015e32d5dSMike Smith ACPI_STATUS
75115e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
75215e32d5dSMike Smith {
75315e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
75415e32d5dSMike Smith 
7550ae55423SMike Smith     FUNCTION_TRACE_U32(__FUNCTION__, state);
7560ae55423SMike Smith 
75715e32d5dSMike Smith     switch (state) {
75815e32d5dSMike Smith     case ACPI_STATE_S0:	/* XXX only for testing */
75915e32d5dSMike Smith 	status = AcpiSetSystemSleepState((UINT8)state);
76015e32d5dSMike Smith 	if (status != AE_OK) {
76115e32d5dSMike Smith 	    device_printf(sc->acpi_dev, "AcpiSetSystemSleepState failed - %s\n", acpi_strerror(status));
76215e32d5dSMike Smith 	}
76315e32d5dSMike Smith 	break;
76415e32d5dSMike Smith 
76515e32d5dSMike Smith     case ACPI_STATE_S1:
76615e32d5dSMike Smith 	/*
76715e32d5dSMike Smith 	 * Inform all devices that we are going to sleep.
76815e32d5dSMike Smith 	 */
76915e32d5dSMike Smith 	if (DEVICE_SUSPEND(root_bus) != 0) {
77015e32d5dSMike Smith 	    /*
77115e32d5dSMike Smith 	     * Re-wake the system.
77215e32d5dSMike Smith 	     *
77315e32d5dSMike Smith 	     * XXX note that a better two-pass approach with a 'veto' pass
77415e32d5dSMike Smith 	     *     followed by a "real thing" pass would be better, but the
77515e32d5dSMike Smith 	     *     current bus interface does not provide for this.
77615e32d5dSMike Smith 	     */
77715e32d5dSMike Smith 	    DEVICE_RESUME(root_bus);
7780ae55423SMike Smith 	    return_ACPI_STATUS(AE_ERROR);
77915e32d5dSMike Smith 	}
78015e32d5dSMike Smith 	sc->acpi_sstate = state;
78115e32d5dSMike Smith 	status = AcpiSetSystemSleepState((UINT8)state);
78215e32d5dSMike Smith 	if (status != AE_OK) {
78315e32d5dSMike Smith 	    device_printf(sc->acpi_dev, "AcpiSetSystemSleepState failed - %s\n", acpi_strerror(status));
78415e32d5dSMike Smith 	}
78515e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
78615e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
78713d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
78815e32d5dSMike Smith 	break;
78915e32d5dSMike Smith 
79015e32d5dSMike Smith     case ACPI_STATE_S5:
79115e32d5dSMike Smith 	/*
79215e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
79315e32d5dSMike Smith 	 * shutdown handlers.
79415e32d5dSMike Smith 	 */
79515e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
79615e32d5dSMike Smith 	break;
79715e32d5dSMike Smith 
79815e32d5dSMike Smith     default:
79915e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
80015e32d5dSMike Smith 	break;
80115e32d5dSMike Smith     }
8020ae55423SMike Smith     return_ACPI_STATUS(status);
80315e32d5dSMike Smith }
80415e32d5dSMike Smith 
80515e32d5dSMike Smith /*
80615e32d5dSMike Smith  * Enable/Disable ACPI
80715e32d5dSMike Smith  */
80815e32d5dSMike Smith ACPI_STATUS
80915e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
81015e32d5dSMike Smith {
81115e32d5dSMike Smith     ACPI_STATUS	status;
81215e32d5dSMike Smith     u_int32_t	flags;
81315e32d5dSMike Smith 
8140ae55423SMike Smith     FUNCTION_TRACE(__FUNCTION__);
8150ae55423SMike Smith 
81615e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
81715e32d5dSMike Smith             ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
81815e32d5dSMike Smith     if (!sc->acpi_enabled) {
81915e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
82015e32d5dSMike Smith     } else {
82115e32d5dSMike Smith 	status = AE_OK;
82215e32d5dSMike Smith     }
82315e32d5dSMike Smith     if (status == AE_OK)
82415e32d5dSMike Smith 	sc->acpi_enabled = 1;
8250ae55423SMike Smith     return_ACPI_STATUS(status);
82615e32d5dSMike Smith }
82715e32d5dSMike Smith 
82815e32d5dSMike Smith ACPI_STATUS
82915e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
83015e32d5dSMike Smith {
83115e32d5dSMike Smith     ACPI_STATUS	status;
83215e32d5dSMike Smith 
8330ae55423SMike Smith     FUNCTION_TRACE(__FUNCTION__);
8340ae55423SMike Smith 
83515e32d5dSMike Smith     if (sc->acpi_enabled) {
83615e32d5dSMike Smith 	status = AcpiDisable();
83715e32d5dSMike Smith     } else {
83815e32d5dSMike Smith 	status = AE_OK;
83915e32d5dSMike Smith     }
84015e32d5dSMike Smith     if (status == AE_OK)
84115e32d5dSMike Smith 	sc->acpi_enabled = 0;
8420ae55423SMike Smith     return_ACPI_STATUS(status);
84315e32d5dSMike Smith }
84415e32d5dSMike Smith 
84515e32d5dSMike Smith /*
84615e32d5dSMike Smith  * Returns true if the device is actually present and should
84715e32d5dSMike Smith  * be attached to.  This requires the present, enabled, UI-visible
84815e32d5dSMike Smith  * and diagnostics-passed bits to be set.
84915e32d5dSMike Smith  */
85015e32d5dSMike Smith BOOLEAN
85115e32d5dSMike Smith acpi_DeviceIsPresent(device_t dev)
85215e32d5dSMike Smith {
85315e32d5dSMike Smith     ACPI_HANDLE		h;
85415e32d5dSMike Smith     ACPI_DEVICE_INFO	devinfo;
85515e32d5dSMike Smith     ACPI_STATUS		error;
85615e32d5dSMike Smith 
85715e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
85815e32d5dSMike Smith 	return(FALSE);
85915e32d5dSMike Smith     if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK)
86015e32d5dSMike Smith 	return(FALSE);
86115e32d5dSMike Smith     if ((devinfo.Valid & ACPI_VALID_HID) && (devinfo.CurrentStatus & 0xf))
86215e32d5dSMike Smith 	return(TRUE);
86315e32d5dSMike Smith     return(FALSE);
86415e32d5dSMike Smith }
86515e32d5dSMike Smith 
86615e32d5dSMike Smith /*
86715e32d5dSMike Smith  * Evaluate a path that should return a number
86815e32d5dSMike Smith  */
86915e32d5dSMike Smith ACPI_STATUS
87015e32d5dSMike Smith acpi_EvaluateNumber(ACPI_HANDLE handle, char *path, int *number)
87115e32d5dSMike Smith {
87215e32d5dSMike Smith     ACPI_STATUS	error;
87315e32d5dSMike Smith     ACPI_BUFFER	buf;
87415e32d5dSMike Smith     int		param[4];
87515e32d5dSMike Smith 
87615e32d5dSMike Smith     if (handle == NULL)
87715e32d5dSMike Smith 	handle = ACPI_ROOT_OBJECT;
87815e32d5dSMike Smith     buf.Pointer = &param[0];
87915e32d5dSMike Smith     buf.Length = sizeof(param);
88015e32d5dSMike Smith     if ((error = AcpiEvaluateObject(handle, path, NULL, &buf)) == AE_OK) {
88115e32d5dSMike Smith 	if (param[0] == ACPI_TYPE_NUMBER) {
88215e32d5dSMike Smith 	    *number = param[1];
88315e32d5dSMike Smith 	} else {
88415e32d5dSMike Smith 	    error = AE_TYPE;
88515e32d5dSMike Smith 	}
88615e32d5dSMike Smith     }
88715e32d5dSMike Smith     return(error);
88815e32d5dSMike Smith }
88915e32d5dSMike Smith 
89015e32d5dSMike Smith /*
89115e32d5dSMike Smith  * ACPI Event Handlers
89215e32d5dSMike Smith  */
89315e32d5dSMike Smith 
89415e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
89515e32d5dSMike Smith 
89615e32d5dSMike Smith static void
89715e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
89815e32d5dSMike Smith {
8990ae55423SMike Smith     FUNCTION_TRACE_U32(__FUNCTION__, state);
90015e32d5dSMike Smith 
9010ae55423SMike Smith     if (state >= ACPI_STATE_S0 && state <= ACPI_STATE_S5)
90215e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
9030ae55423SMike Smith     return_VOID;
90415e32d5dSMike Smith }
90515e32d5dSMike Smith 
90615e32d5dSMike Smith static void
90715e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
90815e32d5dSMike Smith {
9090ae55423SMike Smith     FUNCTION_TRACE_U32(__FUNCTION__, state);
9100ae55423SMike Smith 
91115e32d5dSMike Smith     /* Well, what to do? :-) */
9120ae55423SMike Smith 
9130ae55423SMike Smith     return_VOID;
91415e32d5dSMike Smith }
91515e32d5dSMike Smith 
91615e32d5dSMike Smith /*
91715e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
91815e32d5dSMike Smith  */
91915e32d5dSMike Smith UINT32
92015e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context)
92115e32d5dSMike Smith {
92215e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
92315e32d5dSMike Smith 
9240ae55423SMike Smith     FUNCTION_TRACE(__FUNCTION__);
9250ae55423SMike Smith 
92615e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
9270ae55423SMike Smith 
9280ae55423SMike Smith     return_VALUE(INTERRUPT_HANDLED);
92915e32d5dSMike Smith }
93015e32d5dSMike Smith 
93115e32d5dSMike Smith UINT32
93215e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context)
93315e32d5dSMike Smith {
93415e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
93515e32d5dSMike Smith 
9360ae55423SMike Smith     FUNCTION_TRACE(__FUNCTION__);
9370ae55423SMike Smith 
93815e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
9390ae55423SMike Smith 
9400ae55423SMike Smith     return_VALUE(INTERRUPT_HANDLED);
94115e32d5dSMike Smith }
94215e32d5dSMike Smith 
94315e32d5dSMike Smith UINT32
94415e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context)
94515e32d5dSMike Smith {
94615e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
94715e32d5dSMike Smith 
9480ae55423SMike Smith     FUNCTION_TRACE(__FUNCTION__);
9490ae55423SMike Smith 
95015e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
9510ae55423SMike Smith 
9520ae55423SMike Smith     return_VALUE(INTERRUPT_HANDLED);
95315e32d5dSMike Smith }
95415e32d5dSMike Smith 
95515e32d5dSMike Smith UINT32
95615e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context)
95715e32d5dSMike Smith {
95815e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
95915e32d5dSMike Smith 
9600ae55423SMike Smith     FUNCTION_TRACE(__FUNCTION__);
9610ae55423SMike Smith 
96215e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
9630ae55423SMike Smith 
9640ae55423SMike Smith     return_VALUE(INTERRUPT_HANDLED);
96515e32d5dSMike Smith }
96615e32d5dSMike Smith 
96715e32d5dSMike Smith /*
96815e32d5dSMike Smith  * XXX This is kinda ugly, and should not be here.
96915e32d5dSMike Smith  */
97015e32d5dSMike Smith struct acpi_staticbuf {
97115e32d5dSMike Smith     ACPI_BUFFER	buffer;
97215e32d5dSMike Smith     char	data[512];
97315e32d5dSMike Smith };
97415e32d5dSMike Smith 
97515e32d5dSMike Smith char *
97615e32d5dSMike Smith acpi_strerror(ACPI_STATUS excep)
97715e32d5dSMike Smith {
97815e32d5dSMike Smith     static struct acpi_staticbuf	buf;
97915e32d5dSMike Smith 
98015e32d5dSMike Smith     buf.buffer.Length = 512;
98115e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
98215e32d5dSMike Smith 
98315e32d5dSMike Smith     if (AcpiFormatException(excep, &buf.buffer) == AE_OK)
98415e32d5dSMike Smith 	return(buf.buffer.Pointer);
98515e32d5dSMike Smith     return("(error formatting exception)");
98615e32d5dSMike Smith }
98715e32d5dSMike Smith 
98815e32d5dSMike Smith char *
98915e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
99015e32d5dSMike Smith {
99115e32d5dSMike Smith     static struct acpi_staticbuf	buf;
99215e32d5dSMike Smith 
99315e32d5dSMike Smith     buf.buffer.Length = 512;
99415e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
99515e32d5dSMike Smith 
99615e32d5dSMike Smith     if (AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer) == AE_OK)
99715e32d5dSMike Smith 	return(buf.buffer.Pointer);
99815e32d5dSMike Smith     return("(unknown path)");
99915e32d5dSMike Smith }
100015e32d5dSMike Smith 
100115e32d5dSMike Smith /*
100215e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
100315e32d5dSMike Smith  * parts of the namespace.
100415e32d5dSMike Smith  */
100515e32d5dSMike Smith int
100615e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
100715e32d5dSMike Smith {
100815e32d5dSMike Smith     char	*cp, *np;
100915e32d5dSMike Smith     int		len;
101015e32d5dSMike Smith 
101115e32d5dSMike Smith     np = acpi_name(handle);
101215e32d5dSMike Smith     if (*np == '\\')
101315e32d5dSMike Smith 	np++;
101415e32d5dSMike Smith     if ((cp = getenv("debug.acpi.avoid")) == NULL)
101515e32d5dSMike Smith 	return(0);
101615e32d5dSMike Smith 
101715e32d5dSMike Smith     /* scan the avoid list checking for a match */
101815e32d5dSMike Smith     for (;;) {
101915e32d5dSMike Smith 	while ((*cp != 0) && isspace(*cp))
102015e32d5dSMike Smith 	    cp++;
102115e32d5dSMike Smith 	if (*cp == 0)
102215e32d5dSMike Smith 	    break;
102315e32d5dSMike Smith 	len = 0;
102415e32d5dSMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
102515e32d5dSMike Smith 	    len++;
102615e32d5dSMike Smith 	if (!strncmp(cp, np, len)) {
10270ae55423SMike Smith 	    DEBUG_PRINT(TRACE_OBJECTS, ("avoiding '%s'\n", np));
10280ae55423SMike Smith 	    return(1);
10290ae55423SMike Smith 	}
10300ae55423SMike Smith 	cp += len;
10310ae55423SMike Smith     }
10320ae55423SMike Smith     return(0);
10330ae55423SMike Smith }
10340ae55423SMike Smith 
10350ae55423SMike Smith /*
10360ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
10370ae55423SMike Smith  */
10380ae55423SMike Smith int
10390ae55423SMike Smith acpi_disabled(char *subsys)
10400ae55423SMike Smith {
10410ae55423SMike Smith     char	*cp;
10420ae55423SMike Smith     int		len;
10430ae55423SMike Smith 
10440ae55423SMike Smith     if ((cp = getenv("debug.acpi.disable")) == NULL)
10450ae55423SMike Smith 	return(0);
10460ae55423SMike Smith     if (!strcmp(cp, "all"))
10470ae55423SMike Smith 	return(1);
10480ae55423SMike Smith 
10490ae55423SMike Smith     /* scan the disable list checking for a match */
10500ae55423SMike Smith     for (;;) {
10510ae55423SMike Smith 	while ((*cp != 0) && isspace(*cp))
10520ae55423SMike Smith 	    cp++;
10530ae55423SMike Smith 	if (*cp == 0)
10540ae55423SMike Smith 	    break;
10550ae55423SMike Smith 	len = 0;
10560ae55423SMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
10570ae55423SMike Smith 	    len++;
10580ae55423SMike Smith 	if (!strncmp(cp, subsys, len)) {
10590ae55423SMike Smith 	    DEBUG_PRINT(TRACE_OBJECTS, ("disabled '%s'\n", subsys));
106015e32d5dSMike Smith 	    return(1);
106115e32d5dSMike Smith 	}
106215e32d5dSMike Smith 	cp += len;
106315e32d5dSMike Smith     }
106415e32d5dSMike Smith     return(0);
106515e32d5dSMike Smith }
106615e32d5dSMike Smith 
106715e32d5dSMike Smith /*
106815e32d5dSMike Smith  * Control interface.
106915e32d5dSMike Smith  *
10700ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
10710ae55423SMike Smith  * drivers wanting to be accessible via /dev/acpi should use the register/deregister
10720ae55423SMike Smith  * interface to make their handlers visible.
107315e32d5dSMike Smith  */
10740ae55423SMike Smith struct acpi_ioctl_hook
10750ae55423SMike Smith {
10760ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook)	link;
10770ae55423SMike Smith     u_long				cmd;
10780ae55423SMike Smith     int					(* fn)(u_long cmd, caddr_t addr, void *arg);
10790ae55423SMike Smith     void				*arg;
10800ae55423SMike Smith };
10810ae55423SMike Smith 
10820ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
10830ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
10840ae55423SMike Smith 
10850ae55423SMike Smith /*
10860ae55423SMike Smith  * Register an ioctl handler.
10870ae55423SMike Smith  */
10880ae55423SMike Smith int
10890ae55423SMike Smith acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg)
10900ae55423SMike Smith {
10910ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
10920ae55423SMike Smith 
10930ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
10940ae55423SMike Smith 	return(ENOMEM);
10950ae55423SMike Smith     hp->cmd = cmd;
10960ae55423SMike Smith     hp->fn = fn;
10970ae55423SMike Smith     hp->arg = arg;
10980ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
10990ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
11000ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
11010ae55423SMike Smith     }
11020ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
11030ae55423SMike Smith     return(0);
11040ae55423SMike Smith }
11050ae55423SMike Smith 
11060ae55423SMike Smith /*
11070ae55423SMike Smith  * Deregister an ioctl handler.
11080ae55423SMike Smith  */
11090ae55423SMike Smith void
11100ae55423SMike Smith acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg))
11110ae55423SMike Smith {
11120ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
11130ae55423SMike Smith 
11140ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
11150ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
11160ae55423SMike Smith 	    break;
11170ae55423SMike Smith 
11180ae55423SMike Smith     if (hp != NULL) {
11190ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
11200ae55423SMike Smith 	free(hp, M_ACPIDEV);
11210ae55423SMike Smith     }
11220ae55423SMike Smith }
11230ae55423SMike Smith 
112415e32d5dSMike Smith static int
112515e32d5dSMike Smith acpiopen(dev_t dev, int flag, int fmt, struct proc *p)
112615e32d5dSMike Smith {
112715e32d5dSMike Smith     return(0);
112815e32d5dSMike Smith }
112915e32d5dSMike Smith 
113015e32d5dSMike Smith static int
113115e32d5dSMike Smith acpiclose(dev_t dev, int flag, int fmt, struct proc *p)
113215e32d5dSMike Smith {
113315e32d5dSMike Smith     return(0);
113415e32d5dSMike Smith }
113515e32d5dSMike Smith 
113615e32d5dSMike Smith static int
113715e32d5dSMike Smith acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
113815e32d5dSMike Smith {
113915e32d5dSMike Smith     struct acpi_softc		*sc;
11400ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
11410ae55423SMike Smith     int				error, xerror, state;
114215e32d5dSMike Smith 
114315e32d5dSMike Smith     error = state = 0;
114415e32d5dSMike Smith     sc = dev->si_drv1;
114515e32d5dSMike Smith 
11460ae55423SMike Smith     /*
11470ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
11480ae55423SMike Smith      */
11490ae55423SMike Smith     if (acpi_ioctl_hooks_initted) {
11500ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
11510ae55423SMike Smith 	    if (hp->cmd == cmd) {
11520ae55423SMike Smith 		xerror = hp->fn(cmd, addr, hp->arg);
11530ae55423SMike Smith 		if (xerror != 0)
11540ae55423SMike Smith 		    error = xerror;
1155917d44c8SMitsuru IWASAKI 		goto out;
11560ae55423SMike Smith 	    }
11570ae55423SMike Smith 	}
11580ae55423SMike Smith     }
11590ae55423SMike Smith 
11600ae55423SMike Smith     /*
11610ae55423SMike Smith      * Core system ioctls.
11620ae55423SMike Smith      */
116315e32d5dSMike Smith     switch (cmd) {
116415e32d5dSMike Smith     case ACPIIO_ENABLE:
11650ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
116615e32d5dSMike Smith 	    error = ENXIO;
116715e32d5dSMike Smith 	break;
116815e32d5dSMike Smith 
116915e32d5dSMike Smith     case ACPIIO_DISABLE:
11700ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
117115e32d5dSMike Smith 	    error = ENXIO;
117215e32d5dSMike Smith 	break;
117315e32d5dSMike Smith 
117415e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
117515e32d5dSMike Smith 	if (!sc->acpi_enabled) {
117615e32d5dSMike Smith 	    error = ENXIO;
117715e32d5dSMike Smith 	    break;
117815e32d5dSMike Smith 	}
117915e32d5dSMike Smith 	state = *(int *)addr;
118015e32d5dSMike Smith 	if (state >= ACPI_STATE_S0  && state <= ACPI_STATE_S5) {
118115e32d5dSMike Smith 	    acpi_SetSleepState(sc, state);
118215e32d5dSMike Smith 	} else {
118315e32d5dSMike Smith 	    error = EINVAL;
118415e32d5dSMike Smith 	}
118515e32d5dSMike Smith 	break;
118615e32d5dSMike Smith 
118715e32d5dSMike Smith     default:
11880ae55423SMike Smith 	if (error == 0)
118915e32d5dSMike Smith 	    error = EINVAL;
119015e32d5dSMike Smith 	break;
119115e32d5dSMike Smith     }
1192917d44c8SMitsuru IWASAKI 
1193917d44c8SMitsuru IWASAKI out:
119415e32d5dSMike Smith     return(error);
119515e32d5dSMike Smith }
119615e32d5dSMike Smith 
119715e32d5dSMike Smith #ifdef ACPI_DEBUG
11980ae55423SMike Smith /*
11990ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
12000ae55423SMike Smith  *
12010ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
12020ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
12030ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
12040ae55423SMike Smith  * prefixing the bit name with !.
12050ae55423SMike Smith  */
120615e32d5dSMike Smith struct debugtag
120715e32d5dSMike Smith {
120815e32d5dSMike Smith     char	*name;
120915e32d5dSMike Smith     UINT32	value;
121015e32d5dSMike Smith };
121115e32d5dSMike Smith 
121215e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
121315e32d5dSMike Smith     {"GLOBAL",			0x00000001},
121415e32d5dSMike Smith     {"COMMON",			0x00000002},
121515e32d5dSMike Smith     {"PARSER",			0x00000004},
121615e32d5dSMike Smith     {"DISPATCHER",		0x00000008},
121715e32d5dSMike Smith     {"INTERPRETER",		0x00000010},
121815e32d5dSMike Smith     {"NAMESPACE",		0x00000020},
121915e32d5dSMike Smith     {"RESOURCE_MANAGER",	0x00000040},
122015e32d5dSMike Smith     {"TABLE_MANAGER",		0x00000080},
122115e32d5dSMike Smith     {"EVENT_HANDLING",		0x00000100},
122215e32d5dSMike Smith     {"HARDWARE",		0x00000200},
122315e32d5dSMike Smith     {"MISCELLANEOUS",		0x00000400},
122415e32d5dSMike Smith     {"OS_DEPENDENT",		0x00000800},
122515e32d5dSMike Smith     {"BUS_MANAGER",		0x00001000},
122615e32d5dSMike Smith     {"PROCESSOR_CONTROL",	0x00002000},
122715e32d5dSMike Smith     {"SYSTEM_CONTROL",		0x00004000},
122815e32d5dSMike Smith     {"THERMAL_CONTROL",		0x00008000},
122915e32d5dSMike Smith     {"POWER_CONTROL",		0x00010000},
123015e32d5dSMike Smith     {"EMBEDDED_CONTROLLER",	0x00020000},
123115e32d5dSMike Smith     {"BATTERY",			0x00040000},
123215e32d5dSMike Smith     {"DEBUGGER",		0x00100000},
123315e32d5dSMike Smith     {"ALL_COMPONENTS",		0x001FFFFF},
123415e32d5dSMike Smith     {NULL, 0}
123515e32d5dSMike Smith };
123615e32d5dSMike Smith 
123715e32d5dSMike Smith static struct debugtag dbg_level[] = {
123815e32d5dSMike Smith     {"ACPI_OK",                     0x00000001},
123915e32d5dSMike Smith     {"ACPI_INFO",                   0x00000002},
124015e32d5dSMike Smith     {"ACPI_WARN",                   0x00000004},
124115e32d5dSMike Smith     {"ACPI_ERROR",                  0x00000008},
124215e32d5dSMike Smith     {"ACPI_FATAL",                  0x00000010},
124315e32d5dSMike Smith     {"ACPI_DEBUG_OBJECT",           0x00000020},
124415e32d5dSMike Smith     {"ACPI_ALL",                    0x0000003F},
124515e32d5dSMike Smith     {"TRACE_PARSE",                 0x00000100},
124615e32d5dSMike Smith     {"TRACE_DISPATCH",              0x00000200},
124715e32d5dSMike Smith     {"TRACE_LOAD",                  0x00000400},
124815e32d5dSMike Smith     {"TRACE_EXEC",                  0x00000800},
124915e32d5dSMike Smith     {"TRACE_NAMES",                 0x00001000},
125015e32d5dSMike Smith     {"TRACE_OPREGION",              0x00002000},
125115e32d5dSMike Smith     {"TRACE_BFIELD",                0x00004000},
125215e32d5dSMike Smith     {"TRACE_TRASH",                 0x00008000},
125315e32d5dSMike Smith     {"TRACE_TABLES",                0x00010000},
125415e32d5dSMike Smith     {"TRACE_FUNCTIONS",             0x00020000},
125515e32d5dSMike Smith     {"TRACE_VALUES",                0x00040000},
125615e32d5dSMike Smith     {"TRACE_OBJECTS",               0x00080000},
125715e32d5dSMike Smith     {"TRACE_ALLOCATIONS",           0x00100000},
125815e32d5dSMike Smith     {"TRACE_RESOURCES",             0x00200000},
125915e32d5dSMike Smith     {"TRACE_IO",                    0x00400000},
126015e32d5dSMike Smith     {"TRACE_INTERRUPTS",            0x00800000},
126115e32d5dSMike Smith     {"TRACE_USER_REQUESTS",         0x01000000},
126215e32d5dSMike Smith     {"TRACE_PACKAGE",               0x02000000},
126315e32d5dSMike Smith     {"TRACE_MUTEX",                 0x04000000},
126415e32d5dSMike Smith     {"TRACE_ALL",                   0x0FFFFF00},
126515e32d5dSMike Smith     {"VERBOSE_AML_DISASSEMBLE",     0x10000000},
126615e32d5dSMike Smith     {"VERBOSE_INFO",                0x20000000},
126715e32d5dSMike Smith     {"VERBOSE_TABLES",              0x40000000},
126815e32d5dSMike Smith     {"VERBOSE_EVENTS",              0x80000000},
126915e32d5dSMike Smith     {"VERBOSE_ALL",                 0xF0000000},
127015e32d5dSMike Smith     {NULL, 0}
127115e32d5dSMike Smith };
127215e32d5dSMike Smith 
127315e32d5dSMike Smith static void
127415e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
127515e32d5dSMike Smith {
127615e32d5dSMike Smith     char	*ep;
127715e32d5dSMike Smith     int		i, l;
12780ae55423SMike Smith     int		set;
127915e32d5dSMike Smith 
128015e32d5dSMike Smith     while (*cp) {
128115e32d5dSMike Smith 	if (isspace(*cp)) {
128215e32d5dSMike Smith 	    cp++;
128315e32d5dSMike Smith 	    continue;
128415e32d5dSMike Smith 	}
128515e32d5dSMike Smith 	ep = cp;
128615e32d5dSMike Smith 	while (*ep && !isspace(*ep))
128715e32d5dSMike Smith 	    ep++;
12880ae55423SMike Smith 	if (*cp == '!') {
12890ae55423SMike Smith 	    set = 0;
12900ae55423SMike Smith 	    cp++;
12910ae55423SMike Smith 	    if (cp == ep)
12920ae55423SMike Smith 		continue;
12930ae55423SMike Smith 	} else {
12940ae55423SMike Smith 	    set = 1;
12950ae55423SMike Smith 	}
129615e32d5dSMike Smith 	l = ep - cp;
129715e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
129815e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
12990ae55423SMike Smith 		if (set) {
130015e32d5dSMike Smith 		    *flag |= tag[i].value;
13010ae55423SMike Smith 		} else {
13020ae55423SMike Smith 		    *flag &= ~tag[i].value;
13030ae55423SMike Smith 		}
130415e32d5dSMike Smith 		printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
130515e32d5dSMike Smith 	    }
130615e32d5dSMike Smith 	}
130715e32d5dSMike Smith 	cp = ep;
130815e32d5dSMike Smith     }
130915e32d5dSMike Smith }
131015e32d5dSMike Smith 
131115e32d5dSMike Smith static void
131215e32d5dSMike Smith acpi_set_debugging(void)
131315e32d5dSMike Smith {
131415e32d5dSMike Smith     char	*cp;
131515e32d5dSMike Smith 
13160ae55423SMike Smith     AcpiDbgLayer = 0;
13170ae55423SMike Smith     AcpiDbgLevel = 0;
131815e32d5dSMike Smith     if ((cp = getenv("debug.acpi.layer")) != NULL)
131915e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
132015e32d5dSMike Smith     if ((cp = getenv("debug.acpi.level")) != NULL)
132115e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
13220ae55423SMike Smith 
13230ae55423SMike Smith     printf("ACPI debug layer 0x%x  debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel);
132415e32d5dSMike Smith }
132515e32d5dSMike Smith #endif
1326