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> 36a89fcf28STakanori Watanabe #include <sys/fcntl.h> 3715e32d5dSMike Smith #include <sys/malloc.h> 3815e32d5dSMike Smith #include <sys/bus.h> 3915e32d5dSMike Smith #include <sys/conf.h> 4015e32d5dSMike Smith #include <sys/ioccom.h> 4115e32d5dSMike Smith #include <sys/reboot.h> 4215e32d5dSMike Smith #include <sys/sysctl.h> 4315e32d5dSMike Smith #include <sys/ctype.h> 441611ea87SMitsuru IWASAKI #include <sys/linker.h> 45f9390180SMitsuru IWASAKI #include <sys/power.h> 4615e32d5dSMike Smith 4715e32d5dSMike Smith #include <machine/clock.h> 4815e32d5dSMike Smith #include <machine/resource.h> 49bc0f2195SMike Smith #include <isa/isavar.h> 50bc0f2195SMike Smith 5115e32d5dSMike Smith #include "acpi.h" 521611ea87SMitsuru IWASAKI #include <dev/acpica/acpica_support.h> 5315e32d5dSMike Smith #include <dev/acpica/acpivar.h> 5415e32d5dSMike Smith #include <dev/acpica/acpiio.h> 5515e32d5dSMike Smith 5615e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 5715e32d5dSMike Smith 58be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */ 59cb9b0d80SMike Smith #define _COMPONENT ACPI_BUS 60b53f2771SMike Smith ACPI_MODULE_NAME("ACPI") 610ae55423SMike Smith 6215e32d5dSMike Smith static d_open_t acpiopen; 6315e32d5dSMike Smith static d_close_t acpiclose; 6415e32d5dSMike Smith static d_ioctl_t acpiioctl; 6515e32d5dSMike Smith 6615e32d5dSMike Smith #define CDEV_MAJOR 152 6715e32d5dSMike Smith static struct cdevsw acpi_cdevsw = { 687ac40f5fSPoul-Henning Kamp .d_open = acpiopen, 697ac40f5fSPoul-Henning Kamp .d_close = acpiclose, 707ac40f5fSPoul-Henning Kamp .d_ioctl = acpiioctl, 717ac40f5fSPoul-Henning Kamp .d_name = "acpi", 727ac40f5fSPoul-Henning Kamp .d_maj = CDEV_MAJOR, 7315e32d5dSMike Smith }; 7415e32d5dSMike Smith 751d073b1dSJohn Baldwin static const char* sleep_state_names[] = { 76b8670bedSMitsuru IWASAKI "S0", "S1", "S2", "S3", "S4", "S5", "NONE"}; 771d073b1dSJohn Baldwin 782a4ac806SMike Smith /* this has to be static, as the softc is gone when we need it */ 792a4ac806SMike Smith static int acpi_off_state = ACPI_STATE_S5; 802a4ac806SMike Smith 81fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000 82cb9b0d80SMike Smith struct mtx acpi_mutex; 83fc0ea94aSJohn Baldwin #endif 84cb9b0d80SMike Smith 856d63101aSMike Smith static int acpi_modevent(struct module *mod, int event, void *junk); 8615e32d5dSMike Smith static void acpi_identify(driver_t *driver, device_t parent); 8715e32d5dSMike Smith static int acpi_probe(device_t dev); 8815e32d5dSMike Smith static int acpi_attach(device_t dev); 89be2b1797SNate Lawson static device_t acpi_add_child(device_t bus, int order, const char *name, 90be2b1797SNate Lawson int unit); 9115e32d5dSMike Smith static int acpi_print_child(device_t bus, device_t child); 92be2b1797SNate Lawson static int acpi_read_ivar(device_t dev, device_t child, int index, 93be2b1797SNate Lawson uintptr_t *result); 94be2b1797SNate Lawson static int acpi_write_ivar(device_t dev, device_t child, int index, 95be2b1797SNate Lawson uintptr_t value); 96be2b1797SNate Lawson static int acpi_set_resource(device_t dev, device_t child, int type, 97be2b1797SNate Lawson int rid, u_long start, u_long count); 98be2b1797SNate Lawson static int acpi_get_resource(device_t dev, device_t child, int type, 99be2b1797SNate Lawson int rid, u_long *startp, u_long *countp); 100be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child, 101be2b1797SNate Lawson int type, int *rid, u_long start, u_long end, 102be2b1797SNate Lawson u_long count, u_int flags); 103be2b1797SNate Lawson static int acpi_release_resource(device_t bus, device_t child, int type, 104be2b1797SNate Lawson int rid, struct resource *r); 10532d18aa5SMike Smith static u_int32_t acpi_isa_get_logicalid(device_t dev); 106b2566207STakanori Watanabe static u_int32_t acpi_isa_get_compatid(device_t dev); 107be2b1797SNate Lawson static int acpi_isa_pnp_probe(device_t bus, device_t child, 108be2b1797SNate Lawson struct isa_pnp_id *ids); 10915e32d5dSMike Smith static void acpi_probe_children(device_t bus); 110be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, 111be2b1797SNate Lawson void *context, void **status); 11215e32d5dSMike Smith static void acpi_shutdown_pre_sync(void *arg, int howto); 11315e32d5dSMike Smith static void acpi_shutdown_final(void *arg, int howto); 11413d4f7baSMitsuru IWASAKI static void acpi_enable_fixed_events(struct acpi_softc *sc); 11515e32d5dSMike Smith static void acpi_system_eventhandler_sleep(void *arg, int state); 11615e32d5dSMike Smith static void acpi_system_eventhandler_wakeup(void *arg, int state); 117d75de536SMitsuru IWASAKI static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 1181d073b1dSJohn Baldwin static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 119f9390180SMitsuru IWASAKI static int acpi_pm_func(u_long cmd, void *arg, ...); 120f9390180SMitsuru IWASAKI 12115e32d5dSMike Smith static device_method_t acpi_methods[] = { 12215e32d5dSMike Smith /* Device interface */ 12315e32d5dSMike Smith DEVMETHOD(device_identify, acpi_identify), 12415e32d5dSMike Smith DEVMETHOD(device_probe, acpi_probe), 12515e32d5dSMike Smith DEVMETHOD(device_attach, acpi_attach), 12615e32d5dSMike Smith DEVMETHOD(device_shutdown, bus_generic_shutdown), 12715e32d5dSMike Smith DEVMETHOD(device_suspend, bus_generic_suspend), 12815e32d5dSMike Smith DEVMETHOD(device_resume, bus_generic_resume), 12915e32d5dSMike Smith 13015e32d5dSMike Smith /* Bus interface */ 13115e32d5dSMike Smith DEVMETHOD(bus_add_child, acpi_add_child), 13215e32d5dSMike Smith DEVMETHOD(bus_print_child, acpi_print_child), 13315e32d5dSMike Smith DEVMETHOD(bus_read_ivar, acpi_read_ivar), 13415e32d5dSMike Smith DEVMETHOD(bus_write_ivar, acpi_write_ivar), 13515e32d5dSMike Smith DEVMETHOD(bus_set_resource, acpi_set_resource), 13615e32d5dSMike Smith DEVMETHOD(bus_get_resource, acpi_get_resource), 13715e32d5dSMike Smith DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 13815e32d5dSMike Smith DEVMETHOD(bus_release_resource, acpi_release_resource), 13915e32d5dSMike Smith DEVMETHOD(bus_driver_added, bus_generic_driver_added), 14015e32d5dSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 14115e32d5dSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 14215e32d5dSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 14315e32d5dSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 14415e32d5dSMike Smith 145bc0f2195SMike Smith /* ISA emulation */ 146bc0f2195SMike Smith DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe), 147bc0f2195SMike Smith 14815e32d5dSMike Smith {0, 0} 14915e32d5dSMike Smith }; 15015e32d5dSMike Smith 15115e32d5dSMike Smith static driver_t acpi_driver = { 15215e32d5dSMike Smith "acpi", 15315e32d5dSMike Smith acpi_methods, 15415e32d5dSMike Smith sizeof(struct acpi_softc), 15515e32d5dSMike Smith }; 15615e32d5dSMike Smith 1573273b005SMike Smith static devclass_t acpi_devclass; 1586d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 159dde24897SMike Smith MODULE_VERSION(acpi, 100); 16015e32d5dSMike Smith 161be2b1797SNate Lawson SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, 162be2b1797SNate Lawson ""); 163be2b1797SNate Lawson SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, 164be2b1797SNate Lawson ""); 165dde24897SMike Smith static int acpi_ca_version = ACPI_CA_VERSION; 166be2b1797SNate Lawson SYSCTL_INT(_debug, OID_AUTO, acpi_ca_version, CTLFLAG_RD, &acpi_ca_version, 0, 167be2b1797SNate Lawson ""); 16815e32d5dSMike Smith 16915e32d5dSMike Smith /* 1706d63101aSMike Smith * ACPI can only be loaded as a module by the loader; activating it after 1716d63101aSMike Smith * system bootstrap time is not useful, and can be fatal to the system. 172be2b1797SNate Lawson * It also cannot be unloaded, since the entire system bus heirarchy hangs 173be2b1797SNate Lawson * off it. 1746d63101aSMike Smith */ 1756d63101aSMike Smith static int 1766d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk) 1776d63101aSMike Smith { 1786d63101aSMike Smith switch(event) { 1796d63101aSMike Smith case MOD_LOAD: 18087b45ed5SMitsuru IWASAKI if (!cold) { 18187b45ed5SMitsuru IWASAKI printf("The ACPI driver cannot be loaded after boot.\n"); 1826d63101aSMike Smith return (EPERM); 18387b45ed5SMitsuru IWASAKI } 1846d63101aSMike Smith break; 1856d63101aSMike Smith case MOD_UNLOAD: 186f9390180SMitsuru IWASAKI if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 1876d63101aSMike Smith return (EBUSY); 188f9390180SMitsuru IWASAKI break; 1896d63101aSMike Smith default: 1906d63101aSMike Smith break; 1916d63101aSMike Smith } 1926d63101aSMike Smith return (0); 1936d63101aSMike Smith } 1946d63101aSMike Smith 1956d63101aSMike Smith /* 19615e32d5dSMike Smith * Detect ACPI, perform early initialisation 19715e32d5dSMike Smith */ 19815e32d5dSMike Smith static void 19915e32d5dSMike Smith acpi_identify(driver_t *driver, device_t parent) 20015e32d5dSMike Smith { 20115e32d5dSMike Smith device_t child; 20215e32d5dSMike Smith int error; 203d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 204d786139cSMaxime Henrion char *debugpoint; 20515e32d5dSMike Smith #endif 20615e32d5dSMike Smith 207b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2080ae55423SMike Smith 20987b45ed5SMitsuru IWASAKI if (!cold) 21087b45ed5SMitsuru IWASAKI return_VOID; 211840f9b53STakanori Watanabe 212be2b1797SNate Lawson /* Check that we haven't been disabled with a hint. */ 2138a9bc9c0SJohn Baldwin if (resource_disabled("acpi", 0)) 21432d18aa5SMike Smith return_VOID; 21532d18aa5SMike Smith 216be2b1797SNate Lawson /* Make sure we're not being doubly invoked. */ 21715e32d5dSMike Smith if (device_find_child(parent, "acpi", 0) != NULL) 2180ae55423SMike Smith return_VOID; 21915e32d5dSMike Smith 220fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000 221be2b1797SNate Lawson /* Initialise the ACPI mutex */ 2226008862bSJohn Baldwin mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 223fc0ea94aSJohn Baldwin #endif 224cb9b0d80SMike Smith 225be2b1797SNate Lawson /* Start up the ACPI CA subsystem. */ 226d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 227d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 228d786139cSMaxime Henrion if (debugpoint) { 229d786139cSMaxime Henrion if (!strcmp(debugpoint, "init")) 23015e32d5dSMike Smith acpi_EnterDebugger(); 231d786139cSMaxime Henrion freeenv(debugpoint); 232d786139cSMaxime Henrion } 23315e32d5dSMike Smith #endif 234b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) { 235bfae45aaSMike Smith printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error)); 2360ae55423SMike Smith return_VOID; 23715e32d5dSMike Smith } 238d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 239d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 240d786139cSMaxime Henrion if (debugpoint) { 241d786139cSMaxime Henrion if (!strcmp(debugpoint, "tables")) 24215e32d5dSMike Smith acpi_EnterDebugger(); 243d786139cSMaxime Henrion freeenv(debugpoint); 244d786139cSMaxime Henrion } 24515e32d5dSMike Smith #endif 2461611ea87SMitsuru IWASAKI 247b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiLoadTables())) { 248bfae45aaSMike Smith printf("ACPI: table load failed: %s\n", AcpiFormatException(error)); 2490ae55423SMike Smith return_VOID; 25015e32d5dSMike Smith } 25115e32d5dSMike Smith 252be2b1797SNate Lawson /* Attach the actual ACPI device. */ 25315e32d5dSMike Smith if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) { 25415e32d5dSMike Smith device_printf(parent, "ACPI: could not attach\n"); 2550ae55423SMike Smith return_VOID; 25615e32d5dSMike Smith } 25715e32d5dSMike Smith } 25815e32d5dSMike Smith 25915e32d5dSMike Smith /* 26015e32d5dSMike Smith * Fetch some descriptive data from ACPI to put in our attach message 26115e32d5dSMike Smith */ 26215e32d5dSMike Smith static int 26315e32d5dSMike Smith acpi_probe(device_t dev) 26415e32d5dSMike Smith { 26515e32d5dSMike Smith ACPI_TABLE_HEADER th; 26615e32d5dSMike Smith char buf[20]; 267cb9b0d80SMike Smith ACPI_STATUS status; 26815e32d5dSMike Smith int error; 269fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 27015e32d5dSMike Smith 271b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 272f9390180SMitsuru IWASAKI 273f9390180SMitsuru IWASAKI if (power_pm_get_type() != POWER_PM_TYPE_NONE && 274f9390180SMitsuru IWASAKI power_pm_get_type() != POWER_PM_TYPE_ACPI) { 275be2b1797SNate Lawson 276f9390180SMitsuru IWASAKI device_printf(dev, "Other PM system enabled.\n"); 277f9390180SMitsuru IWASAKI return_VALUE(ENXIO); 278f9390180SMitsuru IWASAKI } 279f9390180SMitsuru IWASAKI 280cb9b0d80SMike Smith ACPI_LOCK; 2810ae55423SMike Smith 282b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) { 283be2b1797SNate Lawson device_printf(dev, "couldn't get XSDT header: %s\n", 284be2b1797SNate Lawson AcpiFormatException(status)); 285cb9b0d80SMike Smith error = ENXIO; 286cb9b0d80SMike Smith } else { 28715e32d5dSMike Smith sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId); 28815e32d5dSMike Smith device_set_desc_copy(dev, buf); 289cb9b0d80SMike Smith error = 0; 290cb9b0d80SMike Smith } 291cb9b0d80SMike Smith ACPI_UNLOCK; 292cb9b0d80SMike Smith return_VALUE(error); 29315e32d5dSMike Smith } 29415e32d5dSMike Smith 29515e32d5dSMike Smith static int 29615e32d5dSMike Smith acpi_attach(device_t dev) 29715e32d5dSMike Smith { 29815e32d5dSMike Smith struct acpi_softc *sc; 299cb9b0d80SMike Smith ACPI_STATUS status; 30015e32d5dSMike Smith int error; 30132d18aa5SMike Smith UINT32 flags; 302498d464fSMitsuru IWASAKI char *env; 303d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 304d786139cSMaxime Henrion char *debugpoint; 30515e32d5dSMike Smith #endif 306fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 30715e32d5dSMike Smith 308b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 309cb9b0d80SMike Smith ACPI_LOCK; 31015e32d5dSMike Smith sc = device_get_softc(dev); 31115e32d5dSMike Smith bzero(sc, sizeof(*sc)); 31215e32d5dSMike Smith sc->acpi_dev = dev; 31315e32d5dSMike Smith 314d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 315d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 316d786139cSMaxime Henrion if (debugpoint) { 317d786139cSMaxime Henrion if (!strcmp(debugpoint, "spaces")) 31815e32d5dSMike Smith acpi_EnterDebugger(); 319d786139cSMaxime Henrion freeenv(debugpoint); 320d786139cSMaxime Henrion } 32115e32d5dSMike Smith #endif 32215e32d5dSMike Smith 323be2b1797SNate Lawson /* Install the default address space handlers. */ 324cb9b0d80SMike Smith error = ENXIO; 325be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 326be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL); 327be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 328be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemMemory handler: %s\n", 329be2b1797SNate Lawson AcpiFormatException(status)); 330cb9b0d80SMike Smith goto out; 33115e32d5dSMike Smith } 332be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 333be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL); 334be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 335be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemIO handler: %s\n", 336be2b1797SNate Lawson AcpiFormatException(status)); 337cb9b0d80SMike Smith goto out; 33815e32d5dSMike Smith } 339be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 340be2b1797SNate Lawson ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL); 341be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 342be2b1797SNate Lawson device_printf(dev, "could not initialise PciConfig handler: %s\n", 343be2b1797SNate Lawson AcpiFormatException(status)); 344cb9b0d80SMike Smith goto out; 34515e32d5dSMike Smith } 34615e32d5dSMike Smith 34715e32d5dSMike Smith /* 34815e32d5dSMike Smith * Bring ACPI fully online. 34915e32d5dSMike Smith * 350be2b1797SNate Lawson * Note that some systems (specifically, those with namespace evaluation 351be2b1797SNate Lawson * issues that require the avoidance of parts of the namespace) must 352be2b1797SNate Lawson * avoid running _INI and _STA on everything, as well as dodging the final 353be2b1797SNate Lawson * object init pass. 35415e32d5dSMike Smith * 35532d18aa5SMike Smith * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 35632d18aa5SMike Smith * 357be2b1797SNate Lawson * XXX We should arrange for the object init pass after we have attached 358be2b1797SNate Lawson * all our child devices, but on many systems it works here. 35915e32d5dSMike Smith */ 360d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 361d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 362d786139cSMaxime Henrion if (debugpoint) { 363d786139cSMaxime Henrion if (!strcmp(debugpoint, "enable")) 36415e32d5dSMike Smith acpi_EnterDebugger(); 365d786139cSMaxime Henrion freeenv(debugpoint); 366d786139cSMaxime Henrion } 36715e32d5dSMike Smith #endif 36832d18aa5SMike Smith flags = 0; 369d786139cSMaxime Henrion if (testenv("debug.acpi.avoid")) 37032d18aa5SMike Smith flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 371b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 372be2b1797SNate Lawson device_printf(dev, "Could not enable ACPI: %s\n", 373be2b1797SNate Lawson AcpiFormatException(status)); 374cb9b0d80SMike Smith goto out; 37515e32d5dSMike Smith } 37615e32d5dSMike Smith 377f8335e3aSNate Lawson /* 378f8335e3aSNate Lawson * Call the ECDT probe function to provide EC functionality before 379f8335e3aSNate Lawson * the namespace has been evaluated. 380f8335e3aSNate Lawson */ 381f8335e3aSNate Lawson acpi_ec_ecdt_probe(dev); 382f8335e3aSNate Lawson 383b69ed3f4SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 384be2b1797SNate Lawson device_printf(dev, "Could not initialize ACPI objects: %s\n", 385be2b1797SNate Lawson AcpiFormatException(status)); 386b69ed3f4SMitsuru IWASAKI goto out; 387b69ed3f4SMitsuru IWASAKI } 388b69ed3f4SMitsuru IWASAKI 38915e32d5dSMike Smith /* 3901d073b1dSJohn Baldwin * Setup our sysctl tree. 3911d073b1dSJohn Baldwin * 3921d073b1dSJohn Baldwin * XXX: This doesn't check to make sure that none of these fail. 3931d073b1dSJohn Baldwin */ 3941d073b1dSJohn Baldwin sysctl_ctx_init(&sc->acpi_sysctl_ctx); 3951d073b1dSJohn Baldwin sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 3961d073b1dSJohn Baldwin SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 3971d073b1dSJohn Baldwin device_get_name(dev), CTLFLAG_RD, 0, ""); 3981d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 399d75de536SMitsuru IWASAKI OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD, 400d75de536SMitsuru IWASAKI 0, 0, acpi_supported_sleep_state_sysctl, "A", ""); 401d75de536SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4021d073b1dSJohn Baldwin OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 4031d073b1dSJohn Baldwin &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4041d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4051d073b1dSJohn Baldwin OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 4061d073b1dSJohn Baldwin &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4071d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4081d073b1dSJohn Baldwin OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 4091d073b1dSJohn Baldwin &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 410f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 411f86214b6SMitsuru IWASAKI OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 412f86214b6SMitsuru IWASAKI &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 413f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 414f86214b6SMitsuru IWASAKI OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 415f86214b6SMitsuru IWASAKI &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4162d644607SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 417ff01efb5SMitsuru IWASAKI OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW, 418ff01efb5SMitsuru IWASAKI &sc->acpi_sleep_delay, 0, "sleep delay"); 419ff01efb5SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4201611ea87SMitsuru IWASAKI OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW, 4211611ea87SMitsuru IWASAKI &sc->acpi_s4bios, 0, "S4BIOS mode"); 4221611ea87SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4232d644607SMitsuru IWASAKI OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW, 4242d644607SMitsuru IWASAKI &sc->acpi_verbose, 0, "verbose mode"); 425c6a78e98STakanori Watanabe SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 426c6a78e98STakanori Watanabe OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW, 427c6a78e98STakanori Watanabe &sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff"); 428bf0c18ecSNate Lawson 429bf0c18ecSNate Lawson /* 430bf0c18ecSNate Lawson * Default to 5 seconds before sleeping to give some machines time to 431bf0c18ecSNate Lawson * stabilize. 432bf0c18ecSNate Lawson */ 433bf0c18ecSNate Lawson sc->acpi_sleep_delay = 5; 434c6a78e98STakanori Watanabe sc->acpi_disable_on_poweroff = 1; 435931a10c9SMitsuru IWASAKI sc->acpi_s4bios = 1; 4362d644607SMitsuru IWASAKI if (bootverbose) 4372d644607SMitsuru IWASAKI sc->acpi_verbose = 1; 438498d464fSMitsuru IWASAKI if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) { 439498d464fSMitsuru IWASAKI sc->acpi_verbose = 1; 440498d464fSMitsuru IWASAKI freeenv(env); 441498d464fSMitsuru IWASAKI } 4421d073b1dSJohn Baldwin 4431d073b1dSJohn Baldwin /* 44415e32d5dSMike Smith * Dispatch the default sleep state to devices. 44515e32d5dSMike Smith * TBD: should be configured from userland policy manager. 44615e32d5dSMike Smith */ 44715e32d5dSMike Smith sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX; 44815e32d5dSMike Smith sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX; 44915e32d5dSMike Smith sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX; 450f86214b6SMitsuru IWASAKI sc->acpi_standby_sx = ACPI_STATE_S1; 451f86214b6SMitsuru IWASAKI sc->acpi_suspend_sx = ACPI_STATE_S3; 45215e32d5dSMike Smith 45313d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 45415e32d5dSMike Smith 45515e32d5dSMike Smith /* 45615e32d5dSMike Smith * Scan the namespace and attach/initialise children. 45715e32d5dSMike Smith */ 458d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 459d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 460d786139cSMaxime Henrion if (debugpoint) { 461d786139cSMaxime Henrion if (!strcmp(debugpoint, "probe")) 46215e32d5dSMike Smith acpi_EnterDebugger(); 463d786139cSMaxime Henrion freeenv(debugpoint); 464d786139cSMaxime Henrion } 46515e32d5dSMike Smith #endif 46615e32d5dSMike Smith 467be2b1797SNate Lawson /* Register our shutdown handlers */ 468be2b1797SNate Lawson EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, 469be2b1797SNate Lawson SHUTDOWN_PRI_LAST); 470be2b1797SNate Lawson EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, 471be2b1797SNate Lawson SHUTDOWN_PRI_LAST); 47215e32d5dSMike Smith 47315e32d5dSMike Smith /* 47415e32d5dSMike Smith * Register our acpi event handlers. 47515e32d5dSMike Smith * XXX should be configurable eg. via userland policy manager. 47615e32d5dSMike Smith */ 477be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, 478be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 479be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, 480be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 48115e32d5dSMike Smith 482be2b1797SNate Lawson /* Flag our initial states. */ 48315e32d5dSMike Smith sc->acpi_enabled = 1; 48415e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 485ece50487SMitsuru IWASAKI sc->acpi_sleep_disabled = 0; 48615e32d5dSMike Smith 487be2b1797SNate Lawson /* Create the control device */ 488a89fcf28STakanori Watanabe sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644, 4892a4689e8SRobert Watson "acpi"); 49015e32d5dSMike Smith sc->acpi_dev_t->si_drv1 = sc; 49115e32d5dSMike Smith 492d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 493d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 494d786139cSMaxime Henrion if (debugpoint) { 495be2b1797SNate Lawson if (strcmp(debugpoint, "running") == 0) 49615e32d5dSMike Smith acpi_EnterDebugger(); 497d786139cSMaxime Henrion freeenv(debugpoint); 498d786139cSMaxime Henrion } 49915e32d5dSMike Smith #endif 500f86214b6SMitsuru IWASAKI 50191da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS 502be2b1797SNate Lawson if ((error = acpi_task_thread_init())) 503c573e654SMitsuru IWASAKI goto out; 504c573e654SMitsuru IWASAKI #endif 505c573e654SMitsuru IWASAKI 506be2b1797SNate Lawson if ((error = acpi_machdep_init(dev))) 507f86214b6SMitsuru IWASAKI goto out; 508f86214b6SMitsuru IWASAKI 509f9390180SMitsuru IWASAKI /* Register ACPI again to pass the correct argument of pm_func. */ 510f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 511f9390180SMitsuru IWASAKI 512eeb6dba2SJohn Baldwin if (!acpi_disabled("bus")) 513eeb6dba2SJohn Baldwin acpi_probe_children(dev); 514eeb6dba2SJohn Baldwin 515cb9b0d80SMike Smith error = 0; 516cb9b0d80SMike Smith 517cb9b0d80SMike Smith out: 518cb9b0d80SMike Smith ACPI_UNLOCK; 519cb9b0d80SMike Smith return_VALUE (error); 52015e32d5dSMike Smith } 52115e32d5dSMike Smith 52215e32d5dSMike Smith /* 52315e32d5dSMike Smith * Handle a new device being added 52415e32d5dSMike Smith */ 52515e32d5dSMike Smith static device_t 52615e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit) 52715e32d5dSMike Smith { 52815e32d5dSMike Smith struct acpi_device *ad; 52915e32d5dSMike Smith device_t child; 53015e32d5dSMike Smith 531be2b1797SNate Lawson if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL) 53215e32d5dSMike Smith return (NULL); 53315e32d5dSMike Smith 53415e32d5dSMike Smith resource_list_init(&ad->ad_rl); 53515e32d5dSMike Smith 53615e32d5dSMike Smith child = device_add_child_ordered(bus, order, name, unit); 53715e32d5dSMike Smith if (child != NULL) 53815e32d5dSMike Smith device_set_ivars(child, ad); 53915e32d5dSMike Smith return (child); 54015e32d5dSMike Smith } 54115e32d5dSMike Smith 54215e32d5dSMike Smith static int 54315e32d5dSMike Smith acpi_print_child(device_t bus, device_t child) 54415e32d5dSMike Smith { 54515e32d5dSMike Smith struct acpi_device *adev = device_get_ivars(child); 54615e32d5dSMike Smith struct resource_list *rl = &adev->ad_rl; 54715e32d5dSMike Smith int retval = 0; 54815e32d5dSMike Smith 54915e32d5dSMike Smith retval += bus_print_child_header(bus, child); 5509ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 5519ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 5529ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 5539ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 55415e32d5dSMike Smith retval += bus_print_child_footer(bus, child); 55515e32d5dSMike Smith 55615e32d5dSMike Smith return (retval); 55715e32d5dSMike Smith } 55815e32d5dSMike Smith 55915e32d5dSMike Smith 56015e32d5dSMike Smith /* 56115e32d5dSMike Smith * Handle per-device ivars 56215e32d5dSMike Smith */ 56315e32d5dSMike Smith static int 56415e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 56515e32d5dSMike Smith { 56615e32d5dSMike Smith struct acpi_device *ad; 56715e32d5dSMike Smith 56815e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 56915e32d5dSMike Smith printf("device has no ivars\n"); 57015e32d5dSMike Smith return (ENOENT); 57115e32d5dSMike Smith } 57215e32d5dSMike Smith 573be2b1797SNate Lawson /* ACPI and ISA compatibility ivars */ 57415e32d5dSMike Smith switch(index) { 57515e32d5dSMike Smith case ACPI_IVAR_HANDLE: 57615e32d5dSMike Smith *(ACPI_HANDLE *)result = ad->ad_handle; 57715e32d5dSMike Smith break; 57815e32d5dSMike Smith case ACPI_IVAR_MAGIC: 57915e32d5dSMike Smith *(int *)result = ad->ad_magic; 58015e32d5dSMike Smith break; 58115e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 58215e32d5dSMike Smith *(void **)result = ad->ad_private; 58315e32d5dSMike Smith break; 58432d18aa5SMike Smith case ISA_IVAR_VENDORID: 58532d18aa5SMike Smith case ISA_IVAR_SERIAL: 58632d18aa5SMike Smith case ISA_IVAR_COMPATID: 58732d18aa5SMike Smith *(int *)result = -1; 58832d18aa5SMike Smith break; 58932d18aa5SMike Smith case ISA_IVAR_LOGICALID: 59032d18aa5SMike Smith *(int *)result = acpi_isa_get_logicalid(child); 59132d18aa5SMike Smith break; 59215e32d5dSMike Smith default: 59315e32d5dSMike Smith return (ENOENT); 59415e32d5dSMike Smith } 595be2b1797SNate Lawson 59615e32d5dSMike Smith return (0); 59715e32d5dSMike Smith } 59815e32d5dSMike Smith 59915e32d5dSMike Smith static int 60015e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 60115e32d5dSMike Smith { 60215e32d5dSMike Smith struct acpi_device *ad; 60315e32d5dSMike Smith 60415e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 60515e32d5dSMike Smith printf("device has no ivars\n"); 60615e32d5dSMike Smith return (ENOENT); 60715e32d5dSMike Smith } 60815e32d5dSMike Smith 60915e32d5dSMike Smith switch(index) { 61015e32d5dSMike Smith case ACPI_IVAR_HANDLE: 61115e32d5dSMike Smith ad->ad_handle = (ACPI_HANDLE)value; 61215e32d5dSMike Smith break; 61315e32d5dSMike Smith case ACPI_IVAR_MAGIC: 61415e32d5dSMike Smith ad->ad_magic = (int)value; 61515e32d5dSMike Smith break; 61615e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 61715e32d5dSMike Smith ad->ad_private = (void *)value; 61815e32d5dSMike Smith break; 61915e32d5dSMike Smith default: 6202ab060eeSWarner Losh panic("bad ivar write request (%d)", index); 62115e32d5dSMike Smith return (ENOENT); 62215e32d5dSMike Smith } 623be2b1797SNate Lawson 62415e32d5dSMike Smith return (0); 62515e32d5dSMike Smith } 62615e32d5dSMike Smith 627be2b1797SNate Lawson ACPI_HANDLE 628be2b1797SNate Lawson acpi_get_handle(device_t dev) 629be2b1797SNate Lawson { 630be2b1797SNate Lawson uintptr_t up; 631be2b1797SNate Lawson ACPI_HANDLE h; 632be2b1797SNate Lawson 633be2b1797SNate Lawson if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, &up)) 634be2b1797SNate Lawson return(NULL); 635be2b1797SNate Lawson h = (ACPI_HANDLE)up; 636be2b1797SNate Lawson return (h); 637be2b1797SNate Lawson } 638be2b1797SNate Lawson 639be2b1797SNate Lawson int 640be2b1797SNate Lawson acpi_set_handle(device_t dev, ACPI_HANDLE h) 641be2b1797SNate Lawson { 642be2b1797SNate Lawson uintptr_t up; 643be2b1797SNate Lawson 644be2b1797SNate Lawson up = (uintptr_t)h; 645be2b1797SNate Lawson return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, up)); 646be2b1797SNate Lawson } 647be2b1797SNate Lawson 648be2b1797SNate Lawson int 649be2b1797SNate Lawson acpi_get_magic(device_t dev) 650be2b1797SNate Lawson { 651be2b1797SNate Lawson uintptr_t up; 652be2b1797SNate Lawson int m; 653be2b1797SNate Lawson 654be2b1797SNate Lawson if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, &up)) 655be2b1797SNate Lawson return(0); 656be2b1797SNate Lawson m = (int)up; 657be2b1797SNate Lawson return (m); 658be2b1797SNate Lawson } 659be2b1797SNate Lawson 660be2b1797SNate Lawson int 661be2b1797SNate Lawson acpi_set_magic(device_t dev, int m) 662be2b1797SNate Lawson { 663be2b1797SNate Lawson uintptr_t up; 664be2b1797SNate Lawson 665be2b1797SNate Lawson up = (uintptr_t)m; 666be2b1797SNate Lawson return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, up)); 667be2b1797SNate Lawson } 668be2b1797SNate Lawson 669be2b1797SNate Lawson void * 670be2b1797SNate Lawson acpi_get_private(device_t dev) 671be2b1797SNate Lawson { 672be2b1797SNate Lawson uintptr_t up; 673be2b1797SNate Lawson void *p; 674be2b1797SNate Lawson 675be2b1797SNate Lawson if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, &up)) 676be2b1797SNate Lawson return (NULL); 677be2b1797SNate Lawson p = (void *)up; 678be2b1797SNate Lawson return (p); 679be2b1797SNate Lawson } 680be2b1797SNate Lawson 681be2b1797SNate Lawson int 682be2b1797SNate Lawson acpi_set_private(device_t dev, void *p) 683be2b1797SNate Lawson { 684be2b1797SNate Lawson uintptr_t up; 685be2b1797SNate Lawson 686be2b1797SNate Lawson up = (uintptr_t)p; 687be2b1797SNate Lawson return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, up)); 688be2b1797SNate Lawson } 689be2b1797SNate Lawson 690be2b1797SNate Lawson ACPI_OBJECT_TYPE 691be2b1797SNate Lawson acpi_get_type(device_t dev) 692be2b1797SNate Lawson { 693be2b1797SNate Lawson ACPI_HANDLE h; 694be2b1797SNate Lawson ACPI_OBJECT_TYPE t; 695be2b1797SNate Lawson 696be2b1797SNate Lawson if ((h = acpi_get_handle(dev)) == NULL) 697be2b1797SNate Lawson return (ACPI_TYPE_NOT_FOUND); 698be2b1797SNate Lawson if (AcpiGetType(h, &t) != AE_OK) 699be2b1797SNate Lawson return (ACPI_TYPE_NOT_FOUND); 700be2b1797SNate Lawson return (t); 701be2b1797SNate Lawson } 702be2b1797SNate Lawson 70315e32d5dSMike Smith /* 70415e32d5dSMike Smith * Handle child resource allocation/removal 70515e32d5dSMike Smith */ 70615e32d5dSMike Smith static int 707be2b1797SNate Lawson acpi_set_resource(device_t dev, device_t child, int type, int rid, 708be2b1797SNate Lawson u_long start, u_long count) 70915e32d5dSMike Smith { 71015e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 71115e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 71215e32d5dSMike Smith 71315e32d5dSMike Smith resource_list_add(rl, type, rid, start, start + count -1, count); 71415e32d5dSMike Smith 71515e32d5dSMike Smith return(0); 71615e32d5dSMike Smith } 71715e32d5dSMike Smith 71815e32d5dSMike Smith static int 719be2b1797SNate Lawson acpi_get_resource(device_t dev, device_t child, int type, int rid, 720be2b1797SNate Lawson u_long *startp, u_long *countp) 72115e32d5dSMike Smith { 72215e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 72315e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 72415e32d5dSMike Smith struct resource_list_entry *rle; 72515e32d5dSMike Smith 72615e32d5dSMike Smith rle = resource_list_find(rl, type, rid); 72715e32d5dSMike Smith if (!rle) 72815e32d5dSMike Smith return(ENOENT); 72915e32d5dSMike Smith 73015e32d5dSMike Smith if (startp) 73115e32d5dSMike Smith *startp = rle->start; 73215e32d5dSMike Smith if (countp) 73315e32d5dSMike Smith *countp = rle->count; 73415e32d5dSMike Smith 73515e32d5dSMike Smith return (0); 73615e32d5dSMike Smith } 73715e32d5dSMike Smith 73815e32d5dSMike Smith static struct resource * 73915e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 74015e32d5dSMike Smith u_long start, u_long end, u_long count, u_int flags) 74115e32d5dSMike Smith { 74215e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 74315e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 74415e32d5dSMike Smith 745be2b1797SNate Lawson return (resource_list_alloc(rl, bus, child, type, rid, start, end, count, 746be2b1797SNate Lawson flags)); 74715e32d5dSMike Smith } 74815e32d5dSMike Smith 74915e32d5dSMike Smith static int 75015e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r) 75115e32d5dSMike Smith { 75215e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 75315e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 75415e32d5dSMike Smith 75515e32d5dSMike Smith return(resource_list_release(rl, bus, child, type, rid, r)); 75615e32d5dSMike Smith } 75715e32d5dSMike Smith 75815e32d5dSMike Smith /* 759bc0f2195SMike Smith * Handle ISA-like devices probing for a PnP ID to match. 760bc0f2195SMike Smith */ 761bc0f2195SMike Smith #define PNP_EISAID(s) \ 762bc0f2195SMike Smith ((((s[0] - '@') & 0x1f) << 2) \ 763bc0f2195SMike Smith | (((s[1] - '@') & 0x18) >> 3) \ 764bc0f2195SMike Smith | (((s[1] - '@') & 0x07) << 13) \ 765bc0f2195SMike Smith | (((s[2] - '@') & 0x1f) << 8) \ 766bc0f2195SMike Smith | (PNP_HEXTONUM(s[4]) << 16) \ 767bc0f2195SMike Smith | (PNP_HEXTONUM(s[3]) << 20) \ 768bc0f2195SMike Smith | (PNP_HEXTONUM(s[6]) << 24) \ 769bc0f2195SMike Smith | (PNP_HEXTONUM(s[5]) << 28)) 770bc0f2195SMike Smith 77132d18aa5SMike Smith static u_int32_t 77232d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev) 773bc0f2195SMike Smith { 774bc0f2195SMike Smith ACPI_HANDLE h; 775bc0f2195SMike Smith ACPI_DEVICE_INFO devinfo; 7766fca9360SNate Lawson ACPI_BUFFER buf = {sizeof(devinfo), &devinfo}; 777bc0f2195SMike Smith ACPI_STATUS error; 778bc0f2195SMike Smith u_int32_t pnpid; 779fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 78032d18aa5SMike Smith 781b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 78232d18aa5SMike Smith 78332d18aa5SMike Smith pnpid = 0; 78432d18aa5SMike Smith ACPI_LOCK; 78532d18aa5SMike Smith 7866fca9360SNate Lawson /* Fetch and validate the HID. */ 78732d18aa5SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 78832d18aa5SMike Smith goto out; 7896fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 7906fca9360SNate Lawson if (ACPI_FAILURE(error)) 79132d18aa5SMike Smith goto out; 7926fca9360SNate Lawson if ((devinfo.Valid & ACPI_VALID_HID) == 0) 79332d18aa5SMike Smith goto out; 79432d18aa5SMike Smith 7956fca9360SNate Lawson pnpid = PNP_EISAID(devinfo.HardwareId.Value); 796be2b1797SNate Lawson 79732d18aa5SMike Smith out: 79832d18aa5SMike Smith ACPI_UNLOCK; 79932d18aa5SMike Smith return_VALUE (pnpid); 80032d18aa5SMike Smith } 80132d18aa5SMike Smith 802b2566207STakanori Watanabe static u_int32_t 803b2566207STakanori Watanabe acpi_isa_get_compatid(device_t dev) 804b2566207STakanori Watanabe { 805b2566207STakanori Watanabe ACPI_HANDLE h; 806b2566207STakanori Watanabe ACPI_STATUS error; 807b2566207STakanori Watanabe u_int32_t pnpid; 808b2566207STakanori Watanabe ACPI_LOCK_DECL; 809b2566207STakanori Watanabe 810b2566207STakanori Watanabe ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 811b2566207STakanori Watanabe 812b2566207STakanori Watanabe pnpid = 0; 813b2566207STakanori Watanabe ACPI_LOCK; 814b2566207STakanori Watanabe 815be2b1797SNate Lawson /* Fetch and validate the HID */ 816b2566207STakanori Watanabe if ((h = acpi_get_handle(dev)) == NULL) 817b2566207STakanori Watanabe goto out; 818b2566207STakanori Watanabe if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &pnpid))) 819b2566207STakanori Watanabe goto out; 820b2566207STakanori Watanabe 821b2566207STakanori Watanabe out: 822b2566207STakanori Watanabe ACPI_UNLOCK; 823b2566207STakanori Watanabe return_VALUE (pnpid); 824b2566207STakanori Watanabe } 825b2566207STakanori Watanabe 826b2566207STakanori Watanabe 82732d18aa5SMike Smith static int 82832d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 82932d18aa5SMike Smith { 830bc0f2195SMike Smith int result; 831b2566207STakanori Watanabe u_int32_t lid, cid; 832bc0f2195SMike Smith 833b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 834bc0f2195SMike Smith 835bc0f2195SMike Smith /* 836bc0f2195SMike Smith * ISA-style drivers attached to ACPI may persist and 837bc0f2195SMike Smith * probe manually if we return ENOENT. We never want 838bc0f2195SMike Smith * that to happen, so don't ever return it. 839bc0f2195SMike Smith */ 840bc0f2195SMike Smith result = ENXIO; 841bc0f2195SMike Smith 842be2b1797SNate Lawson /* Scan the supplied IDs for a match */ 843b2566207STakanori Watanabe lid = acpi_isa_get_logicalid(child); 844b2566207STakanori Watanabe cid = acpi_isa_get_compatid(child); 845bc0f2195SMike Smith while (ids && ids->ip_id) { 846b2566207STakanori Watanabe if (lid == ids->ip_id || cid == ids->ip_id) { 847bc0f2195SMike Smith result = 0; 848bc0f2195SMike Smith goto out; 849bc0f2195SMike Smith } 850bc0f2195SMike Smith ids++; 851bc0f2195SMike Smith } 852be2b1797SNate Lawson 853bc0f2195SMike Smith out: 854bc0f2195SMike Smith return_VALUE(result); 855bc0f2195SMike Smith } 856bc0f2195SMike Smith 857bc0f2195SMike Smith /* 85815e32d5dSMike Smith * Scan relevant portions of the ACPI namespace and attach child devices. 85915e32d5dSMike Smith * 860be2b1797SNate Lawson * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and 861be2b1797SNate Lawson * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec. 86215e32d5dSMike Smith */ 86315e32d5dSMike Smith static void 86415e32d5dSMike Smith acpi_probe_children(device_t bus) 86515e32d5dSMike Smith { 86615e32d5dSMike Smith ACPI_HANDLE parent; 867be2b1797SNate Lawson ACPI_STATUS status; 8687d3bcec9SMike Smith static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL}; 86915e32d5dSMike Smith int i; 87015e32d5dSMike Smith 871b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 872cb9b0d80SMike Smith ACPI_ASSERTLOCK; 8730ae55423SMike Smith 874be2b1797SNate Lawson /* Create any static children by calling device identify methods. */ 8754c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 87615e32d5dSMike Smith bus_generic_probe(bus); 87715e32d5dSMike Smith 87815e32d5dSMike Smith /* 87915e32d5dSMike Smith * Scan the namespace and insert placeholders for all the devices that 88015e32d5dSMike Smith * we find. 88115e32d5dSMike Smith * 88215e32d5dSMike Smith * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 883be2b1797SNate Lawson * we want to create nodes for all devices, not just those that are 884be2b1797SNate Lawson * currently present. (This assumes that we don't want to create/remove 885be2b1797SNate Lawson * devices as they appear, which might be smarter.) 88615e32d5dSMike Smith */ 8874c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 888be2b1797SNate Lawson for (i = 0; scopes[i] != NULL; i++) { 889be2b1797SNate Lawson status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent); 890be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 891be2b1797SNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, 892be2b1797SNate Lawson bus, NULL); 893be2b1797SNate Lawson } 894be2b1797SNate Lawson } 89515e32d5dSMike Smith 89615e32d5dSMike Smith /* 89715e32d5dSMike Smith * Scan all of the child devices we have created and let them probe/attach. 89815e32d5dSMike Smith */ 8994c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n")); 90015e32d5dSMike Smith bus_generic_attach(bus); 90115e32d5dSMike Smith 90215e32d5dSMike Smith /* 90315e32d5dSMike Smith * Some of these children may have attached others as part of their attach 90415e32d5dSMike Smith * process (eg. the root PCI bus driver), so rescan. 90515e32d5dSMike Smith */ 9064c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n")); 90715e32d5dSMike Smith bus_generic_attach(bus); 9080ae55423SMike Smith 909bc0f2195SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 9100ae55423SMike Smith return_VOID; 91115e32d5dSMike Smith } 91215e32d5dSMike Smith 91315e32d5dSMike Smith /* 91415e32d5dSMike Smith * Evaluate a child device and determine whether we might attach a device to 91515e32d5dSMike Smith * it. 91615e32d5dSMike Smith */ 91715e32d5dSMike Smith static ACPI_STATUS 91815e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 91915e32d5dSMike Smith { 92015e32d5dSMike Smith ACPI_OBJECT_TYPE type; 92115e32d5dSMike Smith device_t child, bus = (device_t)context; 92215e32d5dSMike Smith 923b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 9240ae55423SMike Smith 925be2b1797SNate Lawson /* Skip this device if we think we'll have trouble with it. */ 9260ae55423SMike Smith if (acpi_avoid(handle)) 9270ae55423SMike Smith return_ACPI_STATUS (AE_OK); 9280ae55423SMike Smith 929b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 93015e32d5dSMike Smith switch(type) { 93115e32d5dSMike Smith case ACPI_TYPE_DEVICE: 93215e32d5dSMike Smith case ACPI_TYPE_PROCESSOR: 93315e32d5dSMike Smith case ACPI_TYPE_THERMAL: 93415e32d5dSMike Smith case ACPI_TYPE_POWER: 9350ae55423SMike Smith if (acpi_disabled("children")) 9360ae55423SMike Smith break; 937be2b1797SNate Lawson 93815e32d5dSMike Smith /* 93915e32d5dSMike Smith * Create a placeholder device for this node. Sort the placeholder 94015e32d5dSMike Smith * so that the probe/attach passes will run breadth-first. 94115e32d5dSMike Smith */ 942be2b1797SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", 943be2b1797SNate Lawson acpi_name(handle))); 94415e32d5dSMike Smith child = BUS_ADD_CHILD(bus, level * 10, NULL, -1); 945bc0f2195SMike Smith if (child == NULL) 946bc0f2195SMike Smith break; 94715e32d5dSMike Smith acpi_set_handle(child, handle); 948bc0f2195SMike Smith 949bc0f2195SMike Smith /* 950b519f9d6SMike Smith * Check that the device is present. If it's not present, 951b519f9d6SMike Smith * leave it disabled (so that we have a device_t attached to 952b519f9d6SMike Smith * the handle, but we don't probe it). 953b519f9d6SMike Smith */ 954be2b1797SNate Lawson if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) { 955b519f9d6SMike Smith device_disable(child); 956b519f9d6SMike Smith break; 957b519f9d6SMike Smith } 958b519f9d6SMike Smith 959b519f9d6SMike Smith /* 960bc0f2195SMike Smith * Get the device's resource settings and attach them. 961bc0f2195SMike Smith * Note that if the device has _PRS but no _CRS, we need 962bc0f2195SMike Smith * to decide when it's appropriate to try to configure the 963bc0f2195SMike Smith * device. Ignore the return value here; it's OK for the 964bc0f2195SMike Smith * device not to have any resources. 965bc0f2195SMike Smith */ 966bc0f2195SMike Smith acpi_parse_resources(child, handle, &acpi_res_parse_set); 967bc0f2195SMike Smith 968be2b1797SNate Lawson /* If we're debugging, probe/attach now rather than later */ 969b53f2771SMike Smith ACPI_DEBUG_EXEC(device_probe_and_attach(child)); 9700ae55423SMike Smith break; 97115e32d5dSMike Smith } 97215e32d5dSMike Smith } 973be2b1797SNate Lawson 9740ae55423SMike Smith return_ACPI_STATUS (AE_OK); 97515e32d5dSMike Smith } 97615e32d5dSMike Smith 97715e32d5dSMike Smith static void 97815e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto) 97915e32d5dSMike Smith { 980c6a78e98STakanori Watanabe struct acpi_softc *sc = arg; 981c6a78e98STakanori Watanabe 982cb9b0d80SMike Smith ACPI_ASSERTLOCK; 983cb9b0d80SMike Smith 98415e32d5dSMike Smith /* 9850ae55423SMike Smith * Disable all ACPI events before soft off, otherwise the system 98615e32d5dSMike Smith * will be turned on again on some laptops. 98715e32d5dSMike Smith * 98815e32d5dSMike Smith * XXX this should probably be restricted to masking some events just 98915e32d5dSMike Smith * before powering down, since we may still need ACPI during the 99015e32d5dSMike Smith * shutdown process. 99115e32d5dSMike Smith */ 992c6a78e98STakanori Watanabe if (sc->acpi_disable_on_poweroff) 993c6a78e98STakanori Watanabe acpi_Disable(sc); 99415e32d5dSMike Smith } 99515e32d5dSMike Smith 99615e32d5dSMike Smith static void 99715e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto) 99815e32d5dSMike Smith { 99915e32d5dSMike Smith ACPI_STATUS status; 100015e32d5dSMike Smith 1001cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1002cb9b0d80SMike Smith 1003be2b1797SNate Lawson if ((howto & RB_POWEROFF) != 0) { 1004e1a90ae1SNate Lawson printf("Powering system off using ACPI\n"); 1005be2b1797SNate Lawson status = AcpiEnterSleepStatePrep(acpi_off_state); 1006be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1007b9c780d6SMitsuru IWASAKI printf("AcpiEnterSleepStatePrep failed - %s\n", 1008b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 1009b9c780d6SMitsuru IWASAKI return; 1010b9c780d6SMitsuru IWASAKI } 1011be2b1797SNate Lawson status = AcpiEnterSleepState(acpi_off_state); 1012be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1013bfae45aaSMike Smith printf("ACPI power-off failed - %s\n", AcpiFormatException(status)); 101415e32d5dSMike Smith } else { 101515e32d5dSMike Smith DELAY(1000000); 101615e32d5dSMike Smith printf("ACPI power-off failed - timeout\n"); 101715e32d5dSMike Smith } 1018494ae560SMitsuru IWASAKI } else { 1019e1a90ae1SNate Lawson printf("Shutting down ACPI\n"); 1020494ae560SMitsuru IWASAKI AcpiTerminate(); 102115e32d5dSMike Smith } 102215e32d5dSMike Smith } 102315e32d5dSMike Smith 102413d4f7baSMitsuru IWASAKI static void 102513d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc) 102613d4f7baSMitsuru IWASAKI { 102713d4f7baSMitsuru IWASAKI static int first_time = 1; 102813d4f7baSMitsuru IWASAKI #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n" 102913d4f7baSMitsuru IWASAKI 1030cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1031cb9b0d80SMike Smith 103213d4f7baSMitsuru IWASAKI /* Enable and clear fixed events and install handlers. */ 1033be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) { 103459ddeb18SNate Lawson AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, 0); 103559ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 103613d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 1037be2b1797SNate Lawson acpi_eventhandler_power_button_for_sleep, 1038be2b1797SNate Lawson sc); 1039be2b1797SNate Lawson if (first_time) 104013d4f7baSMitsuru IWASAKI device_printf(sc->acpi_dev, MSGFORMAT, "power"); 104113d4f7baSMitsuru IWASAKI } 1042be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) { 104359ddeb18SNate Lawson AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, 0); 104459ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 104513d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 1046be2b1797SNate Lawson acpi_eventhandler_sleep_button_for_sleep, 1047be2b1797SNate Lawson sc); 1048be2b1797SNate Lawson if (first_time) 104913d4f7baSMitsuru IWASAKI device_printf(sc->acpi_dev, MSGFORMAT, "sleep"); 105013d4f7baSMitsuru IWASAKI } 105113d4f7baSMitsuru IWASAKI 105213d4f7baSMitsuru IWASAKI first_time = 0; 105313d4f7baSMitsuru IWASAKI } 105413d4f7baSMitsuru IWASAKI 105515e32d5dSMike Smith /* 1056c5ba0be4SMike Smith * Returns true if the device is actually present and should 1057c5ba0be4SMike Smith * be attached to. This requires the present, enabled, UI-visible 1058c5ba0be4SMike Smith * and diagnostics-passed bits to be set. 1059c5ba0be4SMike Smith */ 1060c5ba0be4SMike Smith BOOLEAN 1061c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev) 1062c5ba0be4SMike Smith { 1063c5ba0be4SMike Smith ACPI_HANDLE h; 1064c5ba0be4SMike Smith ACPI_DEVICE_INFO devinfo; 10656fca9360SNate Lawson ACPI_BUFFER buf = {sizeof(devinfo), &devinfo}; 1066c5ba0be4SMike Smith ACPI_STATUS error; 1067c5ba0be4SMike Smith 1068cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1069cb9b0d80SMike Smith 1070c5ba0be4SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1071c5ba0be4SMike Smith return (FALSE); 10726fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 10736fca9360SNate Lawson if (ACPI_FAILURE(error)) 1074c5ba0be4SMike Smith return (FALSE); 1075be2b1797SNate Lawson 1076be2b1797SNate Lawson /* If no _STA method, must be present */ 10776fca9360SNate Lawson if ((devinfo.Valid & ACPI_VALID_STA) == 0) 107843896e91SMike Smith return (TRUE); 1079be2b1797SNate Lawson 1080be2b1797SNate Lawson /* Return true for 'present' and 'functioning' */ 108143896e91SMike Smith if ((devinfo.CurrentStatus & 0x9) == 0x9) 1082c5ba0be4SMike Smith return (TRUE); 1083be2b1797SNate Lawson 1084c5ba0be4SMike Smith return (FALSE); 1085c5ba0be4SMike Smith } 1086c5ba0be4SMike Smith 1087c5ba0be4SMike Smith /* 1088b53f2771SMike Smith * Returns true if the battery is actually present and inserted. 1089b53f2771SMike Smith */ 1090b53f2771SMike Smith BOOLEAN 1091b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev) 1092b53f2771SMike Smith { 1093b53f2771SMike Smith ACPI_HANDLE h; 1094b53f2771SMike Smith ACPI_DEVICE_INFO devinfo; 10956fca9360SNate Lawson ACPI_BUFFER buf = {sizeof(devinfo), &devinfo}; 1096b53f2771SMike Smith ACPI_STATUS error; 1097b53f2771SMike Smith 1098b53f2771SMike Smith ACPI_ASSERTLOCK; 1099b53f2771SMike Smith 1100b53f2771SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1101b53f2771SMike Smith return (FALSE); 11026fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 11036fca9360SNate Lawson if (ACPI_FAILURE(error)) 1104b53f2771SMike Smith return (FALSE); 1105be2b1797SNate Lawson 1106be2b1797SNate Lawson /* If no _STA method, must be present */ 11076fca9360SNate Lawson if ((devinfo.Valid & ACPI_VALID_STA) == 0) 1108b53f2771SMike Smith return (TRUE); 1109be2b1797SNate Lawson 1110be2b1797SNate Lawson /* Return true for 'present' and 'functioning' */ 1111b53f2771SMike Smith if ((devinfo.CurrentStatus & 0x19) == 0x19) 1112b53f2771SMike Smith return (TRUE); 1113be2b1797SNate Lawson 1114b53f2771SMike Smith return (FALSE); 1115b53f2771SMike Smith } 1116b53f2771SMike Smith 1117b53f2771SMike Smith /* 111815e32d5dSMike Smith * Match a HID string against a device 111915e32d5dSMike Smith */ 112015e32d5dSMike Smith BOOLEAN 112115e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid) 112215e32d5dSMike Smith { 112315e32d5dSMike Smith ACPI_HANDLE h; 112415e32d5dSMike Smith ACPI_DEVICE_INFO devinfo; 11256fca9360SNate Lawson ACPI_BUFFER buf = {sizeof(devinfo), &devinfo}; 112615e32d5dSMike Smith ACPI_STATUS error; 1127ed136da6SDoug Rabson int cid; 112815e32d5dSMike Smith 1129cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1130cb9b0d80SMike Smith 11314d332989SMike Smith if (hid == NULL) 113215e32d5dSMike Smith return (FALSE); 113315e32d5dSMike Smith if ((h = acpi_get_handle(dev)) == NULL) 113415e32d5dSMike Smith return (FALSE); 11356fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 11366fca9360SNate Lawson if (ACPI_FAILURE(error)) 113715e32d5dSMike Smith return (FALSE); 11386fca9360SNate Lawson if ((devinfo.Valid & ACPI_VALID_HID) != 0 && 11396fca9360SNate Lawson strcmp(hid, devinfo.HardwareId.Value) == 0) 114015e32d5dSMike Smith return (TRUE); 1141be2b1797SNate Lawson 1142b53f2771SMike Smith if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &cid))) 1143ed136da6SDoug Rabson return (FALSE); 1144ed136da6SDoug Rabson if (cid == PNP_EISAID(hid)) 1145ed136da6SDoug Rabson return (TRUE); 1146be2b1797SNate Lawson 114715e32d5dSMike Smith return (FALSE); 114815e32d5dSMike Smith } 114915e32d5dSMike Smith 115015e32d5dSMike Smith /* 1151c5ba0be4SMike Smith * Return the handle of a named object within our scope, ie. that of (parent) 1152c5ba0be4SMike Smith * or one if its parents. 1153c5ba0be4SMike Smith */ 1154c5ba0be4SMike Smith ACPI_STATUS 1155c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 1156c5ba0be4SMike Smith { 1157c5ba0be4SMike Smith ACPI_HANDLE r; 1158c5ba0be4SMike Smith ACPI_STATUS status; 1159c5ba0be4SMike Smith 1160cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1161cb9b0d80SMike Smith 1162be2b1797SNate Lawson /* Walk back up the tree to the root */ 1163c5ba0be4SMike Smith for (;;) { 1164be2b1797SNate Lawson status = AcpiGetHandle(parent, path, &r); 1165be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1166c5ba0be4SMike Smith *result = r; 1167c5ba0be4SMike Smith return (AE_OK); 1168c5ba0be4SMike Smith } 1169c5ba0be4SMike Smith if (status != AE_NOT_FOUND) 1170c5ba0be4SMike Smith return (AE_OK); 1171b53f2771SMike Smith if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 1172c5ba0be4SMike Smith return (AE_NOT_FOUND); 1173c5ba0be4SMike Smith parent = r; 1174c5ba0be4SMike Smith } 1175c5ba0be4SMike Smith } 1176c5ba0be4SMike Smith 1177c5ba0be4SMike Smith /* 1178c5ba0be4SMike Smith * Allocate a buffer with a preset data size. 1179c5ba0be4SMike Smith */ 1180c5ba0be4SMike Smith ACPI_BUFFER * 1181c5ba0be4SMike Smith acpi_AllocBuffer(int size) 1182c5ba0be4SMike Smith { 1183c5ba0be4SMike Smith ACPI_BUFFER *buf; 1184c5ba0be4SMike Smith 1185c5ba0be4SMike Smith if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 1186c5ba0be4SMike Smith return (NULL); 1187c5ba0be4SMike Smith buf->Length = size; 1188c5ba0be4SMike Smith buf->Pointer = (void *)(buf + 1); 1189c5ba0be4SMike Smith return (buf); 1190c5ba0be4SMike Smith } 1191c5ba0be4SMike Smith 1192c5ba0be4SMike Smith /* 1193c5ba0be4SMike Smith * Evaluate a path that should return an integer. 1194c5ba0be4SMike Smith */ 1195c5ba0be4SMike Smith ACPI_STATUS 1196c5ba0be4SMike Smith acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number) 1197c5ba0be4SMike Smith { 1198be2b1797SNate Lawson ACPI_STATUS status; 1199c5ba0be4SMike Smith ACPI_BUFFER buf; 1200c573e654SMitsuru IWASAKI ACPI_OBJECT param; 1201c5ba0be4SMike Smith 1202cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1203cb9b0d80SMike Smith 1204c5ba0be4SMike Smith if (handle == NULL) 1205c5ba0be4SMike Smith handle = ACPI_ROOT_OBJECT; 12060c7da7acSJohn Baldwin 12070c7da7acSJohn Baldwin /* 12080c7da7acSJohn Baldwin * Assume that what we've been pointed at is an Integer object, or 12090c7da7acSJohn Baldwin * a method that will return an Integer. 12100c7da7acSJohn Baldwin */ 1211c5ba0be4SMike Smith buf.Pointer = ¶m; 1212c5ba0be4SMike Smith buf.Length = sizeof(param); 1213be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1214be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1215be2b1797SNate Lawson if (param.Type == ACPI_TYPE_INTEGER) 1216c5ba0be4SMike Smith *number = param.Integer.Value; 1217be2b1797SNate Lawson else 1218be2b1797SNate Lawson status = AE_TYPE; 1219c5ba0be4SMike Smith } 12200c7da7acSJohn Baldwin 12210c7da7acSJohn Baldwin /* 12220c7da7acSJohn Baldwin * In some applications, a method that's expected to return an Integer 12230c7da7acSJohn Baldwin * may instead return a Buffer (probably to simplify some internal 12240c7da7acSJohn Baldwin * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 12250c7da7acSJohn Baldwin * convert it into an Integer as best we can. 12260c7da7acSJohn Baldwin * 12270c7da7acSJohn Baldwin * This is a hack. 12280c7da7acSJohn Baldwin */ 1229be2b1797SNate Lawson if (status == AE_BUFFER_OVERFLOW) { 1230b53f2771SMike Smith if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 1231be2b1797SNate Lawson status = AE_NO_MEMORY; 12320c7da7acSJohn Baldwin } else { 1233be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1234be2b1797SNate Lawson if (ACPI_SUCCESS(status)) 1235be2b1797SNate Lawson status = acpi_ConvertBufferToInteger(&buf, number); 12360c7da7acSJohn Baldwin } 12370c7da7acSJohn Baldwin AcpiOsFree(buf.Pointer); 12380c7da7acSJohn Baldwin } 1239be2b1797SNate Lawson return (status); 1240c5ba0be4SMike Smith } 1241c5ba0be4SMike Smith 1242c573e654SMitsuru IWASAKI ACPI_STATUS 1243c573e654SMitsuru IWASAKI acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number) 1244c573e654SMitsuru IWASAKI { 1245c573e654SMitsuru IWASAKI ACPI_OBJECT *p; 1246c573e654SMitsuru IWASAKI int i; 1247c573e654SMitsuru IWASAKI 1248c573e654SMitsuru IWASAKI p = (ACPI_OBJECT *)bufp->Pointer; 1249c573e654SMitsuru IWASAKI if (p->Type == ACPI_TYPE_INTEGER) { 1250c573e654SMitsuru IWASAKI *number = p->Integer.Value; 1251c573e654SMitsuru IWASAKI return (AE_OK); 1252c573e654SMitsuru IWASAKI } 1253c573e654SMitsuru IWASAKI if (p->Type != ACPI_TYPE_BUFFER) 1254c573e654SMitsuru IWASAKI return (AE_TYPE); 1255c573e654SMitsuru IWASAKI if (p->Buffer.Length > sizeof(int)) 1256c573e654SMitsuru IWASAKI return (AE_BAD_DATA); 1257be2b1797SNate Lawson 1258c573e654SMitsuru IWASAKI *number = 0; 1259c573e654SMitsuru IWASAKI for (i = 0; i < p->Buffer.Length; i++) 1260c573e654SMitsuru IWASAKI *number += (*(p->Buffer.Pointer + i) << (i * 8)); 1261c573e654SMitsuru IWASAKI return (AE_OK); 1262c573e654SMitsuru IWASAKI } 1263c573e654SMitsuru IWASAKI 1264c5ba0be4SMike Smith /* 1265c5ba0be4SMike Smith * Iterate over the elements of an a package object, calling the supplied 1266c5ba0be4SMike Smith * function for each element. 1267c5ba0be4SMike Smith * 1268c5ba0be4SMike Smith * XXX possible enhancement might be to abort traversal on error. 1269c5ba0be4SMike Smith */ 1270c5ba0be4SMike Smith ACPI_STATUS 1271be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg, 1272be2b1797SNate Lawson void (*func)(ACPI_OBJECT *comp, void *arg), void *arg) 1273c5ba0be4SMike Smith { 1274c5ba0be4SMike Smith ACPI_OBJECT *comp; 1275c5ba0be4SMike Smith int i; 1276c5ba0be4SMike Smith 1277be2b1797SNate Lawson if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE) 1278c5ba0be4SMike Smith return (AE_BAD_PARAMETER); 1279c5ba0be4SMike Smith 1280be2b1797SNate Lawson /* Iterate over components */ 1281be2b1797SNate Lawson i = 0; 1282be2b1797SNate Lawson comp = pkg->Package.Elements; 1283be2b1797SNate Lawson for (; i < pkg->Package.Count; i++, comp++) 1284c5ba0be4SMike Smith func(comp, arg); 1285c5ba0be4SMike Smith 1286c5ba0be4SMike Smith return (AE_OK); 1287c5ba0be4SMike Smith } 1288c5ba0be4SMike Smith 12896f69255bSMike Smith /* 12906f69255bSMike Smith * Find the (index)th resource object in a set. 12916f69255bSMike Smith */ 12926f69255bSMike Smith ACPI_STATUS 12936d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 12946f69255bSMike Smith { 12956d63101aSMike Smith ACPI_RESOURCE *rp; 12966f69255bSMike Smith int i; 12976f69255bSMike Smith 12986d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 12996f69255bSMike Smith i = index; 13006d63101aSMike Smith while (i-- > 0) { 1301be2b1797SNate Lawson /* Range check */ 13026d63101aSMike Smith if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 13036f69255bSMike Smith return (AE_BAD_PARAMETER); 1304be2b1797SNate Lawson 1305be2b1797SNate Lawson /* Check for terminator */ 1306be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 13076d63101aSMike Smith return (AE_NOT_FOUND); 13086d63101aSMike Smith rp = ACPI_RESOURCE_NEXT(rp); 13096f69255bSMike Smith } 13106f69255bSMike Smith if (resp != NULL) 13116d63101aSMike Smith *resp = rp; 1312be2b1797SNate Lawson 13136d63101aSMike Smith return (AE_OK); 13146d63101aSMike Smith } 13156d63101aSMike Smith 13166d63101aSMike Smith /* 13176d63101aSMike Smith * Append an ACPI_RESOURCE to an ACPI_BUFFER. 13186d63101aSMike Smith * 13196d63101aSMike Smith * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 13206d63101aSMike Smith * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 13216d63101aSMike Smith * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 13226d63101aSMike Smith * resources. 13236d63101aSMike Smith */ 13246d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 13256d63101aSMike Smith 13266d63101aSMike Smith ACPI_STATUS 13276d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 13286d63101aSMike Smith { 13296d63101aSMike Smith ACPI_RESOURCE *rp; 13306d63101aSMike Smith void *newp; 13316d63101aSMike Smith 1332be2b1797SNate Lawson /* Initialise the buffer if necessary. */ 13336d63101aSMike Smith if (buf->Pointer == NULL) { 13346d63101aSMike Smith buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 13356d63101aSMike Smith if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 13366d63101aSMike Smith return (AE_NO_MEMORY); 13376d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 13386d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 13396d63101aSMike Smith rp->Length = 0; 13406d63101aSMike Smith } 13416d63101aSMike Smith if (res == NULL) 13426d63101aSMike Smith return (AE_OK); 13436d63101aSMike Smith 13446d63101aSMike Smith /* 13456d63101aSMike Smith * Scan the current buffer looking for the terminator. 13466d63101aSMike Smith * This will either find the terminator or hit the end 13476d63101aSMike Smith * of the buffer and return an error. 13486d63101aSMike Smith */ 13496d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 13506d63101aSMike Smith for (;;) { 1351be2b1797SNate Lawson /* Range check, don't go outside the buffer */ 13526d63101aSMike Smith if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 13536d63101aSMike Smith return (AE_BAD_PARAMETER); 1354be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 13556d63101aSMike Smith break; 13566d63101aSMike Smith rp = ACPI_RESOURCE_NEXT(rp); 13576d63101aSMike Smith } 13586d63101aSMike Smith 13596d63101aSMike Smith /* 13606d63101aSMike Smith * Check the size of the buffer and expand if required. 13616d63101aSMike Smith * 13626d63101aSMike Smith * Required size is: 13636d63101aSMike Smith * size of existing resources before terminator + 13646d63101aSMike Smith * size of new resource and header + 13656d63101aSMike Smith * size of terminator. 13666d63101aSMike Smith * 13676d63101aSMike Smith * Note that this loop should really only run once, unless 13686d63101aSMike Smith * for some reason we are stuffing a *really* huge resource. 13696d63101aSMike Smith */ 13706d63101aSMike Smith while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 13716d63101aSMike Smith res->Length + ACPI_RESOURCE_LENGTH_NO_DATA + 13726d63101aSMike Smith ACPI_RESOURCE_LENGTH) >= buf->Length) { 13736d63101aSMike Smith if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 13746d63101aSMike Smith return (AE_NO_MEMORY); 13756d63101aSMike Smith bcopy(buf->Pointer, newp, buf->Length); 1376a692219dSMike Smith rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 1377a692219dSMike Smith ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 13786d63101aSMike Smith AcpiOsFree(buf->Pointer); 13796d63101aSMike Smith buf->Pointer = newp; 13806d63101aSMike Smith buf->Length += buf->Length; 13816d63101aSMike Smith } 13826d63101aSMike Smith 1383be2b1797SNate Lawson /* Insert the new resource. */ 13846d63101aSMike Smith bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA); 13856d63101aSMike Smith 1386be2b1797SNate Lawson /* And add the terminator. */ 13876d63101aSMike Smith rp = ACPI_RESOURCE_NEXT(rp); 13886d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 13896d63101aSMike Smith rp->Length = 0; 13906d63101aSMike Smith 13916f69255bSMike Smith return (AE_OK); 13926f69255bSMike Smith } 1393c5ba0be4SMike Smith 1394da14ac9fSJohn Baldwin /* 1395da14ac9fSJohn Baldwin * Set interrupt model. 1396da14ac9fSJohn Baldwin */ 1397da14ac9fSJohn Baldwin ACPI_STATUS 1398da14ac9fSJohn Baldwin acpi_SetIntrModel(int model) 1399da14ac9fSJohn Baldwin { 1400da14ac9fSJohn Baldwin ACPI_OBJECT_LIST ArgList; 1401da14ac9fSJohn Baldwin ACPI_OBJECT Arg; 1402da14ac9fSJohn Baldwin 1403da14ac9fSJohn Baldwin Arg.Type = ACPI_TYPE_INTEGER; 1404da14ac9fSJohn Baldwin Arg.Integer.Value = model; 1405da14ac9fSJohn Baldwin ArgList.Count = 1; 1406da14ac9fSJohn Baldwin ArgList.Pointer = &Arg; 1407da14ac9fSJohn Baldwin return (AcpiEvaluateObject(ACPI_ROOT_OBJECT, "_PIC", &ArgList, NULL)); 1408da14ac9fSJohn Baldwin } 1409da14ac9fSJohn Baldwin 1410ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME 5 1411ece50487SMitsuru IWASAKI 1412ece50487SMitsuru IWASAKI static void 1413ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg) 1414ece50487SMitsuru IWASAKI { 1415ece50487SMitsuru IWASAKI ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0; 1416ece50487SMitsuru IWASAKI } 1417c30382dfSMitsuru IWASAKI 141815e32d5dSMike Smith /* 141915e32d5dSMike Smith * Set the system sleep state 142015e32d5dSMike Smith * 1421be2b1797SNate Lawson * Currently we support S1-S5 but S4 is only S4BIOS 142215e32d5dSMike Smith */ 142315e32d5dSMike Smith ACPI_STATUS 142415e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state) 142515e32d5dSMike Smith { 142615e32d5dSMike Smith ACPI_STATUS status = AE_OK; 142756d8cb57SMitsuru IWASAKI UINT8 TypeA; 142856d8cb57SMitsuru IWASAKI UINT8 TypeB; 142915e32d5dSMike Smith 1430b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 1431cb9b0d80SMike Smith ACPI_ASSERTLOCK; 14320ae55423SMike Smith 14336397624dSMitsuru IWASAKI if (sc->acpi_sstate != ACPI_STATE_S0) 14346397624dSMitsuru IWASAKI return_ACPI_STATUS (AE_BAD_PARAMETER); /* avoid reentry */ 14356397624dSMitsuru IWASAKI 1436ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1437ece50487SMitsuru IWASAKI return_ACPI_STATUS(AE_OK); 1438ece50487SMitsuru IWASAKI 143915e32d5dSMike Smith switch (state) { 144015e32d5dSMike Smith case ACPI_STATE_S0: /* XXX only for testing */ 1441be2b1797SNate Lawson status = AcpiEnterSleepState((UINT8)state); 1442be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1443be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 1444be2b1797SNate Lawson AcpiFormatException(status)); 144515e32d5dSMike Smith } 144615e32d5dSMike Smith break; 144715e32d5dSMike Smith case ACPI_STATE_S1: 14486161544cSTakanori Watanabe case ACPI_STATE_S2: 14496161544cSTakanori Watanabe case ACPI_STATE_S3: 14506161544cSTakanori Watanabe case ACPI_STATE_S4: 1451be2b1797SNate Lawson status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB); 1452be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1453be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", 1454be2b1797SNate Lawson AcpiFormatException(status)); 145556d8cb57SMitsuru IWASAKI break; 145656d8cb57SMitsuru IWASAKI } 145756d8cb57SMitsuru IWASAKI 1458a1fccb47SMitsuru IWASAKI sc->acpi_sstate = state; 1459a1fccb47SMitsuru IWASAKI sc->acpi_sleep_disabled = 1; 1460a1fccb47SMitsuru IWASAKI 1461be2b1797SNate Lawson /* Inform all devices that we are going to sleep. */ 146215e32d5dSMike Smith if (DEVICE_SUSPEND(root_bus) != 0) { 146315e32d5dSMike Smith /* 146415e32d5dSMike Smith * Re-wake the system. 146515e32d5dSMike Smith * 146615e32d5dSMike Smith * XXX note that a better two-pass approach with a 'veto' pass 146715e32d5dSMike Smith * followed by a "real thing" pass would be better, but the 146815e32d5dSMike Smith * current bus interface does not provide for this. 146915e32d5dSMike Smith */ 147015e32d5dSMike Smith DEVICE_RESUME(root_bus); 14710ae55423SMike Smith return_ACPI_STATUS (AE_ERROR); 147215e32d5dSMike Smith } 1473b9c780d6SMitsuru IWASAKI 1474be2b1797SNate Lawson status = AcpiEnterSleepStatePrep(state); 1475be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1476b9c780d6SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 1477b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 1478b9c780d6SMitsuru IWASAKI break; 1479b9c780d6SMitsuru IWASAKI } 1480b9c780d6SMitsuru IWASAKI 1481be2b1797SNate Lawson if (sc->acpi_sleep_delay > 0) 1482ff01efb5SMitsuru IWASAKI DELAY(sc->acpi_sleep_delay * 1000000); 1483ff01efb5SMitsuru IWASAKI 14846161544cSTakanori Watanabe if (state != ACPI_STATE_S1) { 14856161544cSTakanori Watanabe acpi_sleep_machdep(sc, state); 14866161544cSTakanori Watanabe 1487be2b1797SNate Lawson /* AcpiEnterSleepState() may be incomplete, unlock if locked. */ 148859ddeb18SNate Lawson if (AcpiGbl_MutexInfo[ACPI_MTX_HARDWARE].OwnerId != 148959ddeb18SNate Lawson ACPI_MUTEX_NOT_ACQUIRED) { 149059ddeb18SNate Lawson 14916161544cSTakanori Watanabe AcpiUtReleaseMutex(ACPI_MTX_HARDWARE); 1492a1fccb47SMitsuru IWASAKI } 14936161544cSTakanori Watanabe 14946161544cSTakanori Watanabe /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 1495be2b1797SNate Lawson if (state == ACPI_STATE_S4) 1496964679ceSMitsuru IWASAKI AcpiEnable(); 14976161544cSTakanori Watanabe } else { 1498be2b1797SNate Lawson status = AcpiEnterSleepState((UINT8)state); 1499be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1500be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 1501be2b1797SNate Lawson AcpiFormatException(status)); 1502c30382dfSMitsuru IWASAKI break; 150315e32d5dSMike Smith } 15046161544cSTakanori Watanabe } 1505ff741befSTakanori Watanabe AcpiLeaveSleepState((UINT8)state); 150615e32d5dSMike Smith DEVICE_RESUME(root_bus); 150715e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 150813d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 150915e32d5dSMike Smith break; 151015e32d5dSMike Smith case ACPI_STATE_S5: 151115e32d5dSMike Smith /* 151215e32d5dSMike Smith * Shut down cleanly and power off. This will call us back through the 151315e32d5dSMike Smith * shutdown handlers. 151415e32d5dSMike Smith */ 151515e32d5dSMike Smith shutdown_nice(RB_POWEROFF); 151615e32d5dSMike Smith break; 151715e32d5dSMike Smith default: 151815e32d5dSMike Smith status = AE_BAD_PARAMETER; 151915e32d5dSMike Smith break; 152015e32d5dSMike Smith } 1521ece50487SMitsuru IWASAKI 1522ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1523ece50487SMitsuru IWASAKI timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME); 1524ece50487SMitsuru IWASAKI 15250ae55423SMike Smith return_ACPI_STATUS (status); 152615e32d5dSMike Smith } 152715e32d5dSMike Smith 152815e32d5dSMike Smith /* 152915e32d5dSMike Smith * Enable/Disable ACPI 153015e32d5dSMike Smith */ 153115e32d5dSMike Smith ACPI_STATUS 153215e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc) 153315e32d5dSMike Smith { 153415e32d5dSMike Smith ACPI_STATUS status; 153515e32d5dSMike Smith u_int32_t flags; 153615e32d5dSMike Smith 1537b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1538cb9b0d80SMike Smith ACPI_ASSERTLOCK; 15390ae55423SMike Smith 154015e32d5dSMike Smith flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT | 154115e32d5dSMike Smith ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 1542be2b1797SNate Lawson if (!sc->acpi_enabled) 154315e32d5dSMike Smith status = AcpiEnableSubsystem(flags); 1544be2b1797SNate Lawson else 154515e32d5dSMike Smith status = AE_OK; 1546be2b1797SNate Lawson 154715e32d5dSMike Smith if (status == AE_OK) 154815e32d5dSMike Smith sc->acpi_enabled = 1; 1549be2b1797SNate Lawson 15500ae55423SMike Smith return_ACPI_STATUS (status); 155115e32d5dSMike Smith } 155215e32d5dSMike Smith 155315e32d5dSMike Smith ACPI_STATUS 155415e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc) 155515e32d5dSMike Smith { 155615e32d5dSMike Smith ACPI_STATUS status; 155715e32d5dSMike Smith 1558b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1559cb9b0d80SMike Smith ACPI_ASSERTLOCK; 15600ae55423SMike Smith 1561be2b1797SNate Lawson if (sc->acpi_enabled) 156215e32d5dSMike Smith status = AcpiDisable(); 1563be2b1797SNate Lawson else 156415e32d5dSMike Smith status = AE_OK; 1565be2b1797SNate Lawson 156615e32d5dSMike Smith if (status == AE_OK) 156715e32d5dSMike Smith sc->acpi_enabled = 0; 1568be2b1797SNate Lawson 15690ae55423SMike Smith return_ACPI_STATUS (status); 157015e32d5dSMike Smith } 157115e32d5dSMike Smith 157215e32d5dSMike Smith /* 157315e32d5dSMike Smith * ACPI Event Handlers 157415e32d5dSMike Smith */ 157515e32d5dSMike Smith 157615e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 157715e32d5dSMike Smith 157815e32d5dSMike Smith static void 157915e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state) 158015e32d5dSMike Smith { 1581fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 1582b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 158315e32d5dSMike Smith 1584cb9b0d80SMike Smith ACPI_LOCK; 1585a5d1879bSMitsuru IWASAKI if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 158615e32d5dSMike Smith acpi_SetSleepState((struct acpi_softc *)arg, state); 1587cb9b0d80SMike Smith ACPI_UNLOCK; 15880ae55423SMike Smith return_VOID; 158915e32d5dSMike Smith } 159015e32d5dSMike Smith 159115e32d5dSMike Smith static void 159215e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state) 159315e32d5dSMike Smith { 1594fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 1595b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 15960ae55423SMike Smith 159715e32d5dSMike Smith /* Well, what to do? :-) */ 15980ae55423SMike Smith 1599cb9b0d80SMike Smith ACPI_LOCK; 1600cb9b0d80SMike Smith ACPI_UNLOCK; 1601cb9b0d80SMike Smith 16020ae55423SMike Smith return_VOID; 160315e32d5dSMike Smith } 160415e32d5dSMike Smith 160515e32d5dSMike Smith /* 160615e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 160715e32d5dSMike Smith */ 160815e32d5dSMike Smith UINT32 160915e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context) 161015e32d5dSMike Smith { 161115e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 161215e32d5dSMike Smith 1613b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 16140ae55423SMike Smith 161515e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 16160ae55423SMike Smith 1617b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 161815e32d5dSMike Smith } 161915e32d5dSMike Smith 162015e32d5dSMike Smith UINT32 162115e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context) 162215e32d5dSMike Smith { 162315e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 162415e32d5dSMike Smith 1625b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 16260ae55423SMike Smith 162715e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 16280ae55423SMike Smith 1629b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 163015e32d5dSMike Smith } 163115e32d5dSMike Smith 163215e32d5dSMike Smith UINT32 163315e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context) 163415e32d5dSMike Smith { 163515e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 163615e32d5dSMike Smith 1637b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 16380ae55423SMike Smith 163915e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 16400ae55423SMike Smith 1641b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 164215e32d5dSMike Smith } 164315e32d5dSMike Smith 164415e32d5dSMike Smith UINT32 164515e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context) 164615e32d5dSMike Smith { 164715e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 164815e32d5dSMike Smith 1649b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 16500ae55423SMike Smith 165115e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 16520ae55423SMike Smith 1653b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 165415e32d5dSMike Smith } 165515e32d5dSMike Smith 165615e32d5dSMike Smith /* 165715e32d5dSMike Smith * XXX This is kinda ugly, and should not be here. 165815e32d5dSMike Smith */ 165915e32d5dSMike Smith struct acpi_staticbuf { 166015e32d5dSMike Smith ACPI_BUFFER buffer; 166115e32d5dSMike Smith char data[512]; 166215e32d5dSMike Smith }; 166315e32d5dSMike Smith 166415e32d5dSMike Smith char * 166515e32d5dSMike Smith acpi_name(ACPI_HANDLE handle) 166615e32d5dSMike Smith { 166715e32d5dSMike Smith static struct acpi_staticbuf buf; 166815e32d5dSMike Smith 1669cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1670cb9b0d80SMike Smith 167115e32d5dSMike Smith buf.buffer.Length = 512; 167215e32d5dSMike Smith buf.buffer.Pointer = &buf.data[0]; 167315e32d5dSMike Smith 1674b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer))) 167515e32d5dSMike Smith return (buf.buffer.Pointer); 1676be2b1797SNate Lawson 167715e32d5dSMike Smith return ("(unknown path)"); 167815e32d5dSMike Smith } 167915e32d5dSMike Smith 168015e32d5dSMike Smith /* 168115e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various 168215e32d5dSMike Smith * parts of the namespace. 168315e32d5dSMike Smith */ 168415e32d5dSMike Smith int 168515e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle) 168615e32d5dSMike Smith { 16873c600cfbSJohn Baldwin char *cp, *env, *np; 168815e32d5dSMike Smith int len; 168915e32d5dSMike Smith 169015e32d5dSMike Smith np = acpi_name(handle); 169115e32d5dSMike Smith if (*np == '\\') 169215e32d5dSMike Smith np++; 16933c600cfbSJohn Baldwin if ((env = getenv("debug.acpi.avoid")) == NULL) 169415e32d5dSMike Smith return (0); 169515e32d5dSMike Smith 1696be2b1797SNate Lawson /* Scan the avoid list checking for a match */ 16973c600cfbSJohn Baldwin cp = env; 169815e32d5dSMike Smith for (;;) { 169915e32d5dSMike Smith while ((*cp != 0) && isspace(*cp)) 170015e32d5dSMike Smith cp++; 170115e32d5dSMike Smith if (*cp == 0) 170215e32d5dSMike Smith break; 170315e32d5dSMike Smith len = 0; 170415e32d5dSMike Smith while ((cp[len] != 0) && !isspace(cp[len])) 170515e32d5dSMike Smith len++; 1706d786139cSMaxime Henrion if (!strncmp(cp, np, len)) { 17073c600cfbSJohn Baldwin freeenv(env); 17080ae55423SMike Smith return(1); 1709d786139cSMaxime Henrion } 17100ae55423SMike Smith cp += len; 17110ae55423SMike Smith } 17123c600cfbSJohn Baldwin freeenv(env); 1713be2b1797SNate Lawson 17140ae55423SMike Smith return (0); 17150ae55423SMike Smith } 17160ae55423SMike Smith 17170ae55423SMike Smith /* 17180ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components. 17190ae55423SMike Smith */ 17200ae55423SMike Smith int 17210ae55423SMike Smith acpi_disabled(char *subsys) 17220ae55423SMike Smith { 172378689b15SMaxime Henrion char *cp, *env; 17240ae55423SMike Smith int len; 17250ae55423SMike Smith 172678689b15SMaxime Henrion if ((env = getenv("debug.acpi.disable")) == NULL) 17270ae55423SMike Smith return (0); 172878689b15SMaxime Henrion if (!strcmp(env, "all")) { 172978689b15SMaxime Henrion freeenv(env); 17300ae55423SMike Smith return (1); 1731d786139cSMaxime Henrion } 17320ae55423SMike Smith 17330ae55423SMike Smith /* scan the disable list checking for a match */ 173478689b15SMaxime Henrion cp = env; 17350ae55423SMike Smith for (;;) { 17360ae55423SMike Smith while ((*cp != 0) && isspace(*cp)) 17370ae55423SMike Smith cp++; 17380ae55423SMike Smith if (*cp == 0) 17390ae55423SMike Smith break; 17400ae55423SMike Smith len = 0; 17410ae55423SMike Smith while ((cp[len] != 0) && !isspace(cp[len])) 17420ae55423SMike Smith len++; 1743d786139cSMaxime Henrion if (!strncmp(cp, subsys, len)) { 174478689b15SMaxime Henrion freeenv(env); 174515e32d5dSMike Smith return (1); 1746d786139cSMaxime Henrion } 174715e32d5dSMike Smith cp += len; 174815e32d5dSMike Smith } 174978689b15SMaxime Henrion freeenv(env); 1750be2b1797SNate Lawson 175115e32d5dSMike Smith return (0); 175215e32d5dSMike Smith } 175315e32d5dSMike Smith 175415e32d5dSMike Smith /* 1755a1fccb47SMitsuru IWASAKI * Device wake capability enable/disable. 1756a1fccb47SMitsuru IWASAKI */ 1757a1fccb47SMitsuru IWASAKI void 1758a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable) 1759a1fccb47SMitsuru IWASAKI { 1760a1fccb47SMitsuru IWASAKI ACPI_OBJECT_LIST ArgList; 1761a1fccb47SMitsuru IWASAKI ACPI_OBJECT Arg; 1762a1fccb47SMitsuru IWASAKI 1763a1fccb47SMitsuru IWASAKI /* 1764a1fccb47SMitsuru IWASAKI * TBD: All Power Resources referenced by elements 2 through N 1765a1fccb47SMitsuru IWASAKI * of the _PRW object are put into the ON state. 1766a1fccb47SMitsuru IWASAKI */ 1767a1fccb47SMitsuru IWASAKI 1768a1fccb47SMitsuru IWASAKI ArgList.Count = 1; 1769a1fccb47SMitsuru IWASAKI ArgList.Pointer = &Arg; 1770a1fccb47SMitsuru IWASAKI 1771a1fccb47SMitsuru IWASAKI Arg.Type = ACPI_TYPE_INTEGER; 1772a1fccb47SMitsuru IWASAKI Arg.Integer.Value = enable; 1773a1fccb47SMitsuru IWASAKI 1774a1fccb47SMitsuru IWASAKI (void)AcpiEvaluateObject(h, "_PSW", &ArgList, NULL); 1775a1fccb47SMitsuru IWASAKI } 1776a1fccb47SMitsuru IWASAKI 1777a1fccb47SMitsuru IWASAKI void 1778a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_event(ACPI_HANDLE h) 1779a1fccb47SMitsuru IWASAKI { 1780a1fccb47SMitsuru IWASAKI struct acpi_softc *sc; 1781a1fccb47SMitsuru IWASAKI ACPI_STATUS status; 1782a1fccb47SMitsuru IWASAKI ACPI_BUFFER prw_buffer; 1783a1fccb47SMitsuru IWASAKI ACPI_OBJECT *res; 1784a1fccb47SMitsuru IWASAKI 1785a1fccb47SMitsuru IWASAKI ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1786a1fccb47SMitsuru IWASAKI 1787be2b1797SNate Lawson sc = devclass_get_softc(acpi_devclass, 0); 1788be2b1797SNate Lawson if (sc == NULL) 1789a1fccb47SMitsuru IWASAKI return; 1790a1fccb47SMitsuru IWASAKI 1791a1fccb47SMitsuru IWASAKI /* 1792a1fccb47SMitsuru IWASAKI * _PRW object is only required for devices that have the ability 1793a1fccb47SMitsuru IWASAKI * to wake the system from a system sleeping state. 1794a1fccb47SMitsuru IWASAKI */ 1795a1fccb47SMitsuru IWASAKI prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 1796a1fccb47SMitsuru IWASAKI status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 1797be2b1797SNate Lawson if (ACPI_FAILURE(status)) 1798a1fccb47SMitsuru IWASAKI return; 1799a1fccb47SMitsuru IWASAKI 1800a1fccb47SMitsuru IWASAKI res = (ACPI_OBJECT *)prw_buffer.Pointer; 1801be2b1797SNate Lawson if (res == NULL) 1802a1fccb47SMitsuru IWASAKI return; 1803a1fccb47SMitsuru IWASAKI 1804a1fccb47SMitsuru IWASAKI if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) { 1805a1fccb47SMitsuru IWASAKI goto out; 1806a1fccb47SMitsuru IWASAKI } 1807a1fccb47SMitsuru IWASAKI 1808a1fccb47SMitsuru IWASAKI /* 1809a1fccb47SMitsuru IWASAKI * The element 1 of the _PRW object: 1810a1fccb47SMitsuru IWASAKI * The lowest power system sleeping state that can be entered 1811a1fccb47SMitsuru IWASAKI * while still providing wake functionality. 1812a1fccb47SMitsuru IWASAKI * The sleeping state being entered must be greater or equal to 1813a1fccb47SMitsuru IWASAKI * the power state declared in element 1 of the _PRW object. 1814a1fccb47SMitsuru IWASAKI */ 1815be2b1797SNate Lawson if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER) 1816a1fccb47SMitsuru IWASAKI goto out; 1817a1fccb47SMitsuru IWASAKI 1818be2b1797SNate Lawson if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value) 1819a1fccb47SMitsuru IWASAKI goto out; 1820a1fccb47SMitsuru IWASAKI 1821a1fccb47SMitsuru IWASAKI /* 1822a1fccb47SMitsuru IWASAKI * The element 0 of the _PRW object: 1823a1fccb47SMitsuru IWASAKI */ 1824a1fccb47SMitsuru IWASAKI switch(res->Package.Elements[0].Type) { 1825a1fccb47SMitsuru IWASAKI case ACPI_TYPE_INTEGER: 1826a1fccb47SMitsuru IWASAKI /* 1827a1fccb47SMitsuru IWASAKI * If the data type of this package element is numeric, then this 1828a1fccb47SMitsuru IWASAKI * _PRW package element is the bit index in the GPEx_EN, in the 1829a1fccb47SMitsuru IWASAKI * GPE blocks described in the FADT, of the enable bit that is 1830a1fccb47SMitsuru IWASAKI * enabled for the wake event. 1831a1fccb47SMitsuru IWASAKI */ 1832a1fccb47SMitsuru IWASAKI 18336fca9360SNate Lawson status = AcpiEnableGpe(NULL, res->Package.Elements[0].Integer.Value, 18346fca9360SNate Lawson ACPI_EVENT_WAKE_ENABLE); 1835a1fccb47SMitsuru IWASAKI if (ACPI_FAILURE(status)) 1836a1fccb47SMitsuru IWASAKI printf("%s: EnableEvent Failed\n", __func__); 1837a1fccb47SMitsuru IWASAKI break; 1838a1fccb47SMitsuru IWASAKI case ACPI_TYPE_PACKAGE: 1839a1fccb47SMitsuru IWASAKI /* 1840be2b1797SNate Lawson * XXX TBD 1841be2b1797SNate Lawson * 1842a1fccb47SMitsuru IWASAKI * If the data type of this package element is a package, then this 1843a1fccb47SMitsuru IWASAKI * _PRW package element is itself a package containing two 1844a1fccb47SMitsuru IWASAKI * elements. The first is an object reference to the GPE Block 1845a1fccb47SMitsuru IWASAKI * device that contains the GPE that will be triggered by the wake 1846a1fccb47SMitsuru IWASAKI * event. The second element is numeric and it contains the bit 1847a1fccb47SMitsuru IWASAKI * index in the GPEx_EN, in the GPE Block referenced by the 1848a1fccb47SMitsuru IWASAKI * first element in the package, of the enable bit that is enabled for 1849a1fccb47SMitsuru IWASAKI * the wake event. 1850a1fccb47SMitsuru IWASAKI * For example, if this field is a package then it is of the form: 1851a1fccb47SMitsuru IWASAKI * Package() {\_SB.PCI0.ISA.GPE, 2} 1852a1fccb47SMitsuru IWASAKI */ 1853a1fccb47SMitsuru IWASAKI break; 1854a1fccb47SMitsuru IWASAKI default: 1855a1fccb47SMitsuru IWASAKI break; 1856a1fccb47SMitsuru IWASAKI } 1857a1fccb47SMitsuru IWASAKI 1858a1fccb47SMitsuru IWASAKI out: 1859a1fccb47SMitsuru IWASAKI if (prw_buffer.Pointer != NULL) 1860a1fccb47SMitsuru IWASAKI AcpiOsFree(prw_buffer.Pointer); 1861a1fccb47SMitsuru IWASAKI } 1862a1fccb47SMitsuru IWASAKI 1863a1fccb47SMitsuru IWASAKI /* 186415e32d5dSMike Smith * Control interface. 186515e32d5dSMike Smith * 18660ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual 1867be2b1797SNate Lawson * drivers wanting to be accessible via /dev/acpi should use the 1868be2b1797SNate Lawson * register/deregister interface to make their handlers visible. 186915e32d5dSMike Smith */ 18700ae55423SMike Smith struct acpi_ioctl_hook 18710ae55423SMike Smith { 18720ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link; 18730ae55423SMike Smith u_long cmd; 1874be2b1797SNate Lawson acpi_ioctl_fn fn; 18750ae55423SMike Smith void *arg; 18760ae55423SMike Smith }; 18770ae55423SMike Smith 18780ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 18790ae55423SMike Smith static int acpi_ioctl_hooks_initted; 18800ae55423SMike Smith 18810ae55423SMike Smith /* 18820ae55423SMike Smith * Register an ioctl handler. 18830ae55423SMike Smith */ 18840ae55423SMike Smith int 1885be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 18860ae55423SMike Smith { 18870ae55423SMike Smith struct acpi_ioctl_hook *hp; 18880ae55423SMike Smith 18890ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 18900ae55423SMike Smith return (ENOMEM); 18910ae55423SMike Smith hp->cmd = cmd; 18920ae55423SMike Smith hp->fn = fn; 18930ae55423SMike Smith hp->arg = arg; 18940ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) { 18950ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks); 18960ae55423SMike Smith acpi_ioctl_hooks_initted = 1; 18970ae55423SMike Smith } 18980ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 18990ae55423SMike Smith return (0); 19000ae55423SMike Smith } 19010ae55423SMike Smith 19020ae55423SMike Smith /* 19030ae55423SMike Smith * Deregister an ioctl handler. 19040ae55423SMike Smith */ 19050ae55423SMike Smith void 1906be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 19070ae55423SMike Smith { 19080ae55423SMike Smith struct acpi_ioctl_hook *hp; 19090ae55423SMike Smith 19100ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 19110ae55423SMike Smith if ((hp->cmd == cmd) && (hp->fn == fn)) 19120ae55423SMike Smith break; 19130ae55423SMike Smith 19140ae55423SMike Smith if (hp != NULL) { 19150ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 19160ae55423SMike Smith free(hp, M_ACPIDEV); 19170ae55423SMike Smith } 19180ae55423SMike Smith } 19190ae55423SMike Smith 192015e32d5dSMike Smith static int 19216b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td) 192215e32d5dSMike Smith { 192315e32d5dSMike Smith return (0); 192415e32d5dSMike Smith } 192515e32d5dSMike Smith 192615e32d5dSMike Smith static int 19276b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td) 192815e32d5dSMike Smith { 192915e32d5dSMike Smith return (0); 193015e32d5dSMike Smith } 193115e32d5dSMike Smith 193215e32d5dSMike Smith static int 19336b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 193415e32d5dSMike Smith { 193515e32d5dSMike Smith struct acpi_softc *sc; 19360ae55423SMike Smith struct acpi_ioctl_hook *hp; 19370ae55423SMike Smith int error, xerror, state; 1938fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 193915e32d5dSMike Smith 1940cb9b0d80SMike Smith ACPI_LOCK; 1941cb9b0d80SMike Smith 194215e32d5dSMike Smith error = state = 0; 194315e32d5dSMike Smith sc = dev->si_drv1; 194415e32d5dSMike Smith 19450ae55423SMike Smith /* 19460ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers. 19470ae55423SMike Smith */ 19480ae55423SMike Smith if (acpi_ioctl_hooks_initted) { 19490ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 19500ae55423SMike Smith if (hp->cmd == cmd) { 19510ae55423SMike Smith xerror = hp->fn(cmd, addr, hp->arg); 19520ae55423SMike Smith if (xerror != 0) 19530ae55423SMike Smith error = xerror; 1954917d44c8SMitsuru IWASAKI goto out; 19550ae55423SMike Smith } 19560ae55423SMike Smith } 19570ae55423SMike Smith } 19580ae55423SMike Smith 19590ae55423SMike Smith /* 1960a89fcf28STakanori Watanabe * Core ioctls are not permitted for non-writable user. 1961a89fcf28STakanori Watanabe * Currently, other ioctls just fetch information. 1962a89fcf28STakanori Watanabe * Not changing system behavior. 1963a89fcf28STakanori Watanabe */ 1964be2b1797SNate Lawson if((flag & FWRITE) == 0) 1965be2b1797SNate Lawson return (EPERM); 1966a89fcf28STakanori Watanabe 1967be2b1797SNate Lawson /* Core system ioctls. */ 196815e32d5dSMike Smith switch (cmd) { 196915e32d5dSMike Smith case ACPIIO_ENABLE: 19700ae55423SMike Smith if (ACPI_FAILURE(acpi_Enable(sc))) 197115e32d5dSMike Smith error = ENXIO; 197215e32d5dSMike Smith break; 197315e32d5dSMike Smith case ACPIIO_DISABLE: 19740ae55423SMike Smith if (ACPI_FAILURE(acpi_Disable(sc))) 197515e32d5dSMike Smith error = ENXIO; 197615e32d5dSMike Smith break; 197715e32d5dSMike Smith case ACPIIO_SETSLPSTATE: 197815e32d5dSMike Smith if (!sc->acpi_enabled) { 197915e32d5dSMike Smith error = ENXIO; 198015e32d5dSMike Smith break; 198115e32d5dSMike Smith } 198215e32d5dSMike Smith state = *(int *)addr; 1983be2b1797SNate Lawson if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 198415e32d5dSMike Smith acpi_SetSleepState(sc, state); 1985be2b1797SNate Lawson else 198615e32d5dSMike Smith error = EINVAL; 198715e32d5dSMike Smith break; 198815e32d5dSMike Smith default: 19890ae55423SMike Smith if (error == 0) 199015e32d5dSMike Smith error = EINVAL; 199115e32d5dSMike Smith break; 199215e32d5dSMike Smith } 1993917d44c8SMitsuru IWASAKI 1994917d44c8SMitsuru IWASAKI out: 1995cb9b0d80SMike Smith ACPI_UNLOCK; 199615e32d5dSMike Smith return (error); 199715e32d5dSMike Smith } 199815e32d5dSMike Smith 19991d073b1dSJohn Baldwin static int 2000d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 2001d75de536SMitsuru IWASAKI { 2002d75de536SMitsuru IWASAKI char sleep_state[4]; 2003d75de536SMitsuru IWASAKI char buf[16]; 2004d75de536SMitsuru IWASAKI int error; 2005d75de536SMitsuru IWASAKI UINT8 state, TypeA, TypeB; 2006d75de536SMitsuru IWASAKI 2007d75de536SMitsuru IWASAKI buf[0] = '\0'; 2008d75de536SMitsuru IWASAKI for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX+1; state++) { 2009d75de536SMitsuru IWASAKI if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) { 2010d75de536SMitsuru IWASAKI sprintf(sleep_state, "S%d ", state); 2011d75de536SMitsuru IWASAKI strcat(buf, sleep_state); 2012d75de536SMitsuru IWASAKI } 2013d75de536SMitsuru IWASAKI } 2014d75de536SMitsuru IWASAKI error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 2015d75de536SMitsuru IWASAKI return (error); 2016d75de536SMitsuru IWASAKI } 2017d75de536SMitsuru IWASAKI 2018d75de536SMitsuru IWASAKI static int 20191d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 20201d073b1dSJohn Baldwin { 20211d073b1dSJohn Baldwin char sleep_state[10]; 20221d073b1dSJohn Baldwin int error; 20231d073b1dSJohn Baldwin u_int new_state, old_state; 20241d073b1dSJohn Baldwin 20251d073b1dSJohn Baldwin old_state = *(u_int *)oidp->oid_arg1; 2026b8670bedSMitsuru IWASAKI if (old_state > ACPI_S_STATES_MAX+1) { 20271d073b1dSJohn Baldwin strcpy(sleep_state, "unknown"); 2028cb9b0d80SMike Smith } else { 2029b8670bedSMitsuru IWASAKI bzero(sleep_state, sizeof(sleep_state)); 20301d073b1dSJohn Baldwin strncpy(sleep_state, sleep_state_names[old_state], 20311d073b1dSJohn Baldwin sizeof(sleep_state_names[old_state])); 2032cb9b0d80SMike Smith } 20331d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 20341d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) { 2035be2b1797SNate Lawson new_state = ACPI_STATE_S0; 2036be2b1797SNate Lawson for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) { 20371d073b1dSJohn Baldwin if (strncmp(sleep_state, sleep_state_names[new_state], 20381d073b1dSJohn Baldwin sizeof(sleep_state)) == 0) 20391d073b1dSJohn Baldwin break; 2040cb9b0d80SMike Smith } 2041931a10c9SMitsuru IWASAKI if (new_state <= ACPI_S_STATES_MAX + 1) { 2042be2b1797SNate Lawson if (new_state != old_state) 20431d073b1dSJohn Baldwin *(u_int *)oidp->oid_arg1 = new_state; 2044cb9b0d80SMike Smith } else { 20451d073b1dSJohn Baldwin error = EINVAL; 20461d073b1dSJohn Baldwin } 2047cb9b0d80SMike Smith } 2048be2b1797SNate Lawson 20491d073b1dSJohn Baldwin return (error); 20501d073b1dSJohn Baldwin } 20511d073b1dSJohn Baldwin 205215e32d5dSMike Smith #ifdef ACPI_DEBUG 20530ae55423SMike Smith /* 20540ae55423SMike Smith * Support for parsing debug options from the kernel environment. 20550ae55423SMike Smith * 20560ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 20570ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and 20580ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by 20590ae55423SMike Smith * prefixing the bit name with !. 20600ae55423SMike Smith */ 206115e32d5dSMike Smith struct debugtag 206215e32d5dSMike Smith { 206315e32d5dSMike Smith char *name; 206415e32d5dSMike Smith UINT32 value; 206515e32d5dSMike Smith }; 206615e32d5dSMike Smith 206715e32d5dSMike Smith static struct debugtag dbg_layer[] = { 2068c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES}, 2069c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE}, 2070c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS}, 2071c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES}, 2072c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 2073c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER}, 2074c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 2075c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER}, 2076c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES}, 2077d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 20784c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 2079d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 20804c1cdee6SMike Smith 2081cb9b0d80SMike Smith {"ACPI_BUS", ACPI_BUS}, 20824c1cdee6SMike Smith {"ACPI_SYSTEM", ACPI_SYSTEM}, 2083cb9b0d80SMike Smith {"ACPI_POWER", ACPI_POWER}, 2084cb9b0d80SMike Smith {"ACPI_EC", ACPI_EC}, 2085c5ba0be4SMike Smith {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 2086c5ba0be4SMike Smith {"ACPI_BATTERY", ACPI_BATTERY}, 2087c5ba0be4SMike Smith {"ACPI_BUTTON", ACPI_BUTTON}, 20884c1cdee6SMike Smith {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 2089cb9b0d80SMike Smith {"ACPI_THERMAL", ACPI_THERMAL}, 20904c1cdee6SMike Smith {"ACPI_FAN", ACPI_FAN}, 20914c1cdee6SMike Smith 2092b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 2093c5ba0be4SMike Smith {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 209415e32d5dSMike Smith {NULL, 0} 209515e32d5dSMike Smith }; 209615e32d5dSMike Smith 209715e32d5dSMike Smith static struct debugtag dbg_level[] = { 20984c1cdee6SMike Smith {"ACPI_LV_ERROR", ACPI_LV_ERROR}, 20999501b603SJohn Baldwin {"ACPI_LV_WARN", ACPI_LV_WARN}, 21009501b603SJohn Baldwin {"ACPI_LV_INIT", ACPI_LV_INIT}, 21014c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 21029501b603SJohn Baldwin {"ACPI_LV_INFO", ACPI_LV_INFO}, 21034c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 2104d62ab2f4SMitsuru IWASAKI 2105d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */ 21064c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 21074c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 2108d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 21094c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 21104c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 21114c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 21124c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 21134c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 21144c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 21154c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 21164c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 21174c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 21184c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 21199501b603SJohn Baldwin {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 2120d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 2121d62ab2f4SMitsuru IWASAKI 2122d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */ 2123d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 2124d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 2125d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 2126d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 21274c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL}, 2128d62ab2f4SMitsuru IWASAKI 2129d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 2130d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 2131d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 2132d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO}, 2133d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 2134d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 2135d62ab2f4SMitsuru IWASAKI 2136d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 213798479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 213898479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 213998479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 214098479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 214198479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 214215e32d5dSMike Smith {NULL, 0} 214315e32d5dSMike Smith }; 214415e32d5dSMike Smith 214515e32d5dSMike Smith static void 214615e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 214715e32d5dSMike Smith { 214815e32d5dSMike Smith char *ep; 214915e32d5dSMike Smith int i, l; 21500ae55423SMike Smith int set; 215115e32d5dSMike Smith 215215e32d5dSMike Smith while (*cp) { 215315e32d5dSMike Smith if (isspace(*cp)) { 215415e32d5dSMike Smith cp++; 215515e32d5dSMike Smith continue; 215615e32d5dSMike Smith } 215715e32d5dSMike Smith ep = cp; 215815e32d5dSMike Smith while (*ep && !isspace(*ep)) 215915e32d5dSMike Smith ep++; 21600ae55423SMike Smith if (*cp == '!') { 21610ae55423SMike Smith set = 0; 21620ae55423SMike Smith cp++; 21630ae55423SMike Smith if (cp == ep) 21640ae55423SMike Smith continue; 21650ae55423SMike Smith } else { 21660ae55423SMike Smith set = 1; 21670ae55423SMike Smith } 216815e32d5dSMike Smith l = ep - cp; 216915e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) { 217015e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) { 2171be2b1797SNate Lawson if (set) 217215e32d5dSMike Smith *flag |= tag[i].value; 2173be2b1797SNate Lawson else 21740ae55423SMike Smith *flag &= ~tag[i].value; 217515e32d5dSMike Smith printf("ACPI_DEBUG: set '%s'\n", tag[i].name); 217615e32d5dSMike Smith } 217715e32d5dSMike Smith } 217815e32d5dSMike Smith cp = ep; 217915e32d5dSMike Smith } 218015e32d5dSMike Smith } 218115e32d5dSMike Smith 218215e32d5dSMike Smith static void 21832a4ac806SMike Smith acpi_set_debugging(void *junk) 218415e32d5dSMike Smith { 218594aae282SMike Barcroft char *cp; 218615e32d5dSMike Smith 218787b45ed5SMitsuru IWASAKI if (!cold) 218887b45ed5SMitsuru IWASAKI return; 218987b45ed5SMitsuru IWASAKI 21900ae55423SMike Smith AcpiDbgLayer = 0; 21910ae55423SMike Smith AcpiDbgLevel = 0; 219294aae282SMike Barcroft if ((cp = getenv("debug.acpi.layer")) != NULL) { 219315e32d5dSMike Smith acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer); 219494aae282SMike Barcroft freeenv(cp); 219594aae282SMike Barcroft } 219694aae282SMike Barcroft if ((cp = getenv("debug.acpi.level")) != NULL) { 219715e32d5dSMike Smith acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel); 219894aae282SMike Barcroft freeenv(cp); 219994aae282SMike Barcroft } 22000ae55423SMike Smith 2201be2b1797SNate Lawson printf("ACPI debug layer 0x%x debug level 0x%x\n", AcpiDbgLayer, 2202be2b1797SNate Lawson AcpiDbgLevel); 220315e32d5dSMike Smith } 2204be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 2205be2b1797SNate Lawson NULL); 220615e32d5dSMike Smith #endif 2207f9390180SMitsuru IWASAKI 2208f9390180SMitsuru IWASAKI static int 2209f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...) 2210f9390180SMitsuru IWASAKI { 2211f9390180SMitsuru IWASAKI int state, acpi_state; 2212f9390180SMitsuru IWASAKI int error; 2213f9390180SMitsuru IWASAKI struct acpi_softc *sc; 2214f9390180SMitsuru IWASAKI va_list ap; 2215f9390180SMitsuru IWASAKI 2216f9390180SMitsuru IWASAKI error = 0; 2217f9390180SMitsuru IWASAKI switch (cmd) { 2218f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND: 2219f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg; 2220f9390180SMitsuru IWASAKI if (sc == NULL) { 2221f9390180SMitsuru IWASAKI error = EINVAL; 2222f9390180SMitsuru IWASAKI goto out; 2223f9390180SMitsuru IWASAKI } 2224f9390180SMitsuru IWASAKI 2225f9390180SMitsuru IWASAKI va_start(ap, arg); 2226f9390180SMitsuru IWASAKI state = va_arg(ap, int); 2227f9390180SMitsuru IWASAKI va_end(ap); 2228f9390180SMitsuru IWASAKI 2229f9390180SMitsuru IWASAKI switch (state) { 2230f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY: 2231f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx; 2232f9390180SMitsuru IWASAKI break; 2233f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND: 2234f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx; 2235f9390180SMitsuru IWASAKI break; 2236f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE: 2237f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4; 2238f9390180SMitsuru IWASAKI break; 2239f9390180SMitsuru IWASAKI default: 2240f9390180SMitsuru IWASAKI error = EINVAL; 2241f9390180SMitsuru IWASAKI goto out; 2242f9390180SMitsuru IWASAKI } 2243f9390180SMitsuru IWASAKI 2244f9390180SMitsuru IWASAKI acpi_SetSleepState(sc, acpi_state); 2245f9390180SMitsuru IWASAKI break; 2246f9390180SMitsuru IWASAKI default: 2247f9390180SMitsuru IWASAKI error = EINVAL; 2248f9390180SMitsuru IWASAKI goto out; 2249f9390180SMitsuru IWASAKI } 2250f9390180SMitsuru IWASAKI 2251f9390180SMitsuru IWASAKI out: 2252f9390180SMitsuru IWASAKI return (error); 2253f9390180SMitsuru IWASAKI } 2254f9390180SMitsuru IWASAKI 2255f9390180SMitsuru IWASAKI static void 2256f9390180SMitsuru IWASAKI acpi_pm_register(void *arg) 2257f9390180SMitsuru IWASAKI { 2258be2b1797SNate Lawson if (!cold || resource_disabled("acpi", 0)) 22596c407052SMitsuru IWASAKI return; 2260f9390180SMitsuru IWASAKI 2261f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 2262f9390180SMitsuru IWASAKI } 2263f9390180SMitsuru IWASAKI 2264f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 2265