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> 4654f6bca0SNate Lawson #include <sys/sbuf.h> 47eeb3a05fSNate Lawson #include <sys/smp.h> 4815e32d5dSMike Smith 4915e32d5dSMike Smith #include <machine/clock.h> 5015e32d5dSMike Smith #include <machine/resource.h> 51dc750869SNate Lawson #include <machine/bus.h> 52dc750869SNate Lawson #include <sys/rman.h> 53bc0f2195SMike Smith #include <isa/isavar.h> 54bc0f2195SMike Smith 5515e32d5dSMike Smith #include "acpi.h" 5615e32d5dSMike Smith #include <dev/acpica/acpivar.h> 5715e32d5dSMike Smith #include <dev/acpica/acpiio.h> 589b937d48SNate Lawson #include <contrib/dev/acpica/acnamesp.h> 5915e32d5dSMike Smith 6015e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 6115e32d5dSMike Smith 62be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */ 63cb9b0d80SMike Smith #define _COMPONENT ACPI_BUS 64b53f2771SMike Smith ACPI_MODULE_NAME("ACPI") 650ae55423SMike Smith 6615e32d5dSMike Smith static d_open_t acpiopen; 6715e32d5dSMike Smith static d_close_t acpiclose; 6815e32d5dSMike Smith static d_ioctl_t acpiioctl; 6915e32d5dSMike Smith 7015e32d5dSMike Smith static struct cdevsw acpi_cdevsw = { 71dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 72dc08ffecSPoul-Henning Kamp .d_flags = D_NEEDGIANT, 737ac40f5fSPoul-Henning Kamp .d_open = acpiopen, 747ac40f5fSPoul-Henning Kamp .d_close = acpiclose, 757ac40f5fSPoul-Henning Kamp .d_ioctl = acpiioctl, 767ac40f5fSPoul-Henning Kamp .d_name = "acpi", 7715e32d5dSMike Smith }; 7815e32d5dSMike Smith 791d073b1dSJohn Baldwin static const char* sleep_state_names[] = { 80b8670bedSMitsuru IWASAKI "S0", "S1", "S2", "S3", "S4", "S5", "NONE"}; 811d073b1dSJohn Baldwin 822a4ac806SMike Smith /* this has to be static, as the softc is gone when we need it */ 832a4ac806SMike Smith static int acpi_off_state = ACPI_STATE_S5; 842a4ac806SMike Smith 85fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000 86cb9b0d80SMike Smith struct mtx acpi_mutex; 87fc0ea94aSJohn Baldwin #endif 88cb9b0d80SMike Smith 893184cf5aSNate Lawson struct acpi_quirks { 903184cf5aSNate Lawson char *OemId; 913184cf5aSNate Lawson uint32_t OemRevision; 923184cf5aSNate Lawson char *value; 933184cf5aSNate Lawson }; 943184cf5aSNate Lawson 953184cf5aSNate Lawson #define ACPI_OEM_REV_ANY 0 963184cf5aSNate Lawson 973184cf5aSNate Lawson static struct acpi_quirks acpi_quirks_table[] = { 983184cf5aSNate Lawson #ifdef notyet 993184cf5aSNate Lawson /* Bad PCI routing table. Used on some SuperMicro boards. */ 1003184cf5aSNate Lawson { "PTLTD ", 0x06040000, "pci_link" }, 1013184cf5aSNate Lawson #endif 1023184cf5aSNate Lawson 1033184cf5aSNate Lawson { NULL, 0, NULL } 1043184cf5aSNate Lawson }; 1053184cf5aSNate Lawson 1066d63101aSMike Smith static int acpi_modevent(struct module *mod, int event, void *junk); 10715e32d5dSMike Smith static void acpi_identify(driver_t *driver, device_t parent); 10815e32d5dSMike Smith static int acpi_probe(device_t dev); 10915e32d5dSMike Smith static int acpi_attach(device_t dev); 1103184cf5aSNate Lawson static void acpi_quirks_set(void); 111be2b1797SNate Lawson static device_t acpi_add_child(device_t bus, int order, const char *name, 112be2b1797SNate Lawson int unit); 11315e32d5dSMike Smith static int acpi_print_child(device_t bus, device_t child); 114be2b1797SNate Lawson static int acpi_read_ivar(device_t dev, device_t child, int index, 115be2b1797SNate Lawson uintptr_t *result); 116be2b1797SNate Lawson static int acpi_write_ivar(device_t dev, device_t child, int index, 117be2b1797SNate Lawson uintptr_t value); 118be2b1797SNate Lawson static int acpi_set_resource(device_t dev, device_t child, int type, 119be2b1797SNate Lawson int rid, u_long start, u_long count); 120be2b1797SNate Lawson static int acpi_get_resource(device_t dev, device_t child, int type, 121be2b1797SNate Lawson int rid, u_long *startp, u_long *countp); 122be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child, 123be2b1797SNate Lawson int type, int *rid, u_long start, u_long end, 124be2b1797SNate Lawson u_long count, u_int flags); 125be2b1797SNate Lawson static int acpi_release_resource(device_t bus, device_t child, int type, 126be2b1797SNate Lawson int rid, struct resource *r); 1271e4925e8SNate Lawson static uint32_t acpi_isa_get_logicalid(device_t dev); 1281e4925e8SNate Lawson static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count); 129be2b1797SNate Lawson static int acpi_isa_pnp_probe(device_t bus, device_t child, 130be2b1797SNate Lawson struct isa_pnp_id *ids); 13115e32d5dSMike Smith static void acpi_probe_children(device_t bus); 132be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, 133be2b1797SNate Lawson void *context, void **status); 13415e32d5dSMike Smith static void acpi_shutdown_pre_sync(void *arg, int howto); 13515e32d5dSMike Smith static void acpi_shutdown_final(void *arg, int howto); 136eeb3a05fSNate Lawson static void acpi_shutdown_poweroff(void *arg); 13713d4f7baSMitsuru IWASAKI static void acpi_enable_fixed_events(struct acpi_softc *sc); 13815e32d5dSMike Smith static void acpi_system_eventhandler_sleep(void *arg, int state); 13915e32d5dSMike Smith static void acpi_system_eventhandler_wakeup(void *arg, int state); 140d75de536SMitsuru IWASAKI static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 1411d073b1dSJohn Baldwin static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 142f9390180SMitsuru IWASAKI static int acpi_pm_func(u_long cmd, void *arg, ...); 143879d6c23STakanori Watanabe static int acpi_child_location_str_method(device_t acdev, device_t child, 144879d6c23STakanori Watanabe char *buf, size_t buflen); 145879d6c23STakanori Watanabe static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child, 146879d6c23STakanori Watanabe char *buf, size_t buflen); 147879d6c23STakanori Watanabe 148f9390180SMitsuru IWASAKI 14915e32d5dSMike Smith static device_method_t acpi_methods[] = { 15015e32d5dSMike Smith /* Device interface */ 15115e32d5dSMike Smith DEVMETHOD(device_identify, acpi_identify), 15215e32d5dSMike Smith DEVMETHOD(device_probe, acpi_probe), 15315e32d5dSMike Smith DEVMETHOD(device_attach, acpi_attach), 15456a70eadSNate Lawson DEVMETHOD(device_detach, bus_generic_detach), 15515e32d5dSMike Smith DEVMETHOD(device_shutdown, bus_generic_shutdown), 15615e32d5dSMike Smith DEVMETHOD(device_suspend, bus_generic_suspend), 15715e32d5dSMike Smith DEVMETHOD(device_resume, bus_generic_resume), 15815e32d5dSMike Smith 15915e32d5dSMike Smith /* Bus interface */ 16015e32d5dSMike Smith DEVMETHOD(bus_add_child, acpi_add_child), 16115e32d5dSMike Smith DEVMETHOD(bus_print_child, acpi_print_child), 16215e32d5dSMike Smith DEVMETHOD(bus_read_ivar, acpi_read_ivar), 16315e32d5dSMike Smith DEVMETHOD(bus_write_ivar, acpi_write_ivar), 16415e32d5dSMike Smith DEVMETHOD(bus_set_resource, acpi_set_resource), 16515e32d5dSMike Smith DEVMETHOD(bus_get_resource, acpi_get_resource), 16615e32d5dSMike Smith DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 16715e32d5dSMike Smith DEVMETHOD(bus_release_resource, acpi_release_resource), 168879d6c23STakanori Watanabe DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method), 169879d6c23STakanori Watanabe DEVMETHOD(bus_child_location_str, acpi_child_location_str_method), 17015e32d5dSMike Smith DEVMETHOD(bus_driver_added, bus_generic_driver_added), 17115e32d5dSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 17215e32d5dSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 17315e32d5dSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 17415e32d5dSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 17515e32d5dSMike Smith 176bc0f2195SMike Smith /* ISA emulation */ 177bc0f2195SMike Smith DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe), 178bc0f2195SMike Smith 17915e32d5dSMike Smith {0, 0} 18015e32d5dSMike Smith }; 18115e32d5dSMike Smith 18215e32d5dSMike Smith static driver_t acpi_driver = { 18315e32d5dSMike Smith "acpi", 18415e32d5dSMike Smith acpi_methods, 18515e32d5dSMike Smith sizeof(struct acpi_softc), 18615e32d5dSMike Smith }; 18715e32d5dSMike Smith 1883273b005SMike Smith static devclass_t acpi_devclass; 1896d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 190dde24897SMike Smith MODULE_VERSION(acpi, 100); 19115e32d5dSMike Smith 1921d7b121cSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging"); 1931d7b121cSNate Lawson static char acpi_ca_version[12]; 1941d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD, 1951d7b121cSNate Lawson acpi_ca_version, 0, "Version of Intel ACPI-CA"); 19615e32d5dSMike Smith 19715e32d5dSMike Smith /* 198413081d7SNate Lawson * Allow override of whether methods execute in parallel or not. 199c9b8d77dSNate Lawson * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS" 200c9b8d77dSNate Lawson * errors for AML that really can't handle parallel method execution. 201c9b8d77dSNate Lawson * It is off by default since this breaks recursive methods and 202c9b8d77dSNate Lawson * some IBMs use such code. 203413081d7SNate Lawson */ 204c9b8d77dSNate Lawson static int acpi_serialize_methods; 205413081d7SNate Lawson TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods); 206413081d7SNate Lawson 207c9b8d77dSNate Lawson /* 208c9b8d77dSNate Lawson * Allow override of whether to support the _OSI method. This allows us 209c9b8d77dSNate Lawson * to claim compatibility with various MS OSs without changing the value 210c9b8d77dSNate Lawson * we report for _OS. This is enabled by default since it fixes some 211c9b8d77dSNate Lawson * problems with interrupt routing although it can be disabled if it 212c9b8d77dSNate Lawson * causes problems. See the definition of "AcpiGbl_ValidOsiStrings" for 213c9b8d77dSNate Lawson * a list of systems we claim. 214c9b8d77dSNate Lawson */ 215c9b8d77dSNate Lawson static int acpi_osi_method = 1; 216413081d7SNate Lawson TUNABLE_INT("hw.acpi.osi_method", &acpi_osi_method); 217413081d7SNate Lawson 218413081d7SNate Lawson /* 2196d63101aSMike Smith * ACPI can only be loaded as a module by the loader; activating it after 2206d63101aSMike Smith * system bootstrap time is not useful, and can be fatal to the system. 221be2b1797SNate Lawson * It also cannot be unloaded, since the entire system bus heirarchy hangs 222be2b1797SNate Lawson * off it. 2236d63101aSMike Smith */ 2246d63101aSMike Smith static int 2256d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk) 2266d63101aSMike Smith { 2276d63101aSMike Smith switch(event) { 2286d63101aSMike Smith case MOD_LOAD: 22987b45ed5SMitsuru IWASAKI if (!cold) { 23087b45ed5SMitsuru IWASAKI printf("The ACPI driver cannot be loaded after boot.\n"); 2316d63101aSMike Smith return (EPERM); 23287b45ed5SMitsuru IWASAKI } 2336d63101aSMike Smith break; 2346d63101aSMike Smith case MOD_UNLOAD: 235f9390180SMitsuru IWASAKI if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 2366d63101aSMike Smith return (EBUSY); 237f9390180SMitsuru IWASAKI break; 2386d63101aSMike Smith default: 2396d63101aSMike Smith break; 2406d63101aSMike Smith } 2416d63101aSMike Smith return (0); 2426d63101aSMike Smith } 2436d63101aSMike Smith 2446d63101aSMike Smith /* 245bbc2815cSJohn Baldwin * Perform early initialization. 24615e32d5dSMike Smith */ 247bbc2815cSJohn Baldwin ACPI_STATUS 248bbc2815cSJohn Baldwin acpi_Startup(void) 24915e32d5dSMike Smith { 250d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 251d786139cSMaxime Henrion char *debugpoint; 25215e32d5dSMike Smith #endif 253bbc2815cSJohn Baldwin static int error, started = 0; 25415e32d5dSMike Smith 2551b8c233dSPeter Pentchev ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2561b8c233dSPeter Pentchev 257bbc2815cSJohn Baldwin if (started) 258bbc2815cSJohn Baldwin return_VALUE (error); 259bbc2815cSJohn Baldwin started = 1; 26015e32d5dSMike Smith 261fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000 262be2b1797SNate Lawson /* Initialise the ACPI mutex */ 2636008862bSJohn Baldwin mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 264fc0ea94aSJohn Baldwin #endif 265cb9b0d80SMike Smith 266413081d7SNate Lawson /* 267413081d7SNate Lawson * Set the globals from our tunables. This is needed because ACPI-CA 268413081d7SNate Lawson * uses UINT8 for some values and we have no tunable_uint8. 269413081d7SNate Lawson */ 270413081d7SNate Lawson AcpiGbl_AllMethodsSerialized = acpi_serialize_methods; 271413081d7SNate Lawson AcpiGbl_CreateOsiMethod = acpi_osi_method; 272413081d7SNate Lawson 273be2b1797SNate Lawson /* Start up the ACPI CA subsystem. */ 274d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 275d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 276d786139cSMaxime Henrion if (debugpoint) { 277d786139cSMaxime Henrion if (!strcmp(debugpoint, "init")) 27815e32d5dSMike Smith acpi_EnterDebugger(); 279d786139cSMaxime Henrion freeenv(debugpoint); 280d786139cSMaxime Henrion } 28115e32d5dSMike Smith #endif 282b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) { 283bfae45aaSMike Smith printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error)); 284bbc2815cSJohn Baldwin return_VALUE (error); 28515e32d5dSMike Smith } 286d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 287d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 288d786139cSMaxime Henrion if (debugpoint) { 289d786139cSMaxime Henrion if (!strcmp(debugpoint, "tables")) 29015e32d5dSMike Smith acpi_EnterDebugger(); 291d786139cSMaxime Henrion freeenv(debugpoint); 292d786139cSMaxime Henrion } 29315e32d5dSMike Smith #endif 2941611ea87SMitsuru IWASAKI 295b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiLoadTables())) { 296bfae45aaSMike Smith printf("ACPI: table load failed: %s\n", AcpiFormatException(error)); 297bbc2815cSJohn Baldwin return_VALUE(error); 29815e32d5dSMike Smith } 2993184cf5aSNate Lawson 3003184cf5aSNate Lawson /* Set up any quirks we have for this XSDT. */ 3013184cf5aSNate Lawson acpi_quirks_set(); 3024e376d58SNate Lawson if (acpi_disabled("acpi")) 3034e376d58SNate Lawson return_VALUE (AE_ERROR); 3043184cf5aSNate Lawson 305bbc2815cSJohn Baldwin return_VALUE (AE_OK); 306bbc2815cSJohn Baldwin } 307bbc2815cSJohn Baldwin 308bbc2815cSJohn Baldwin /* 309bbc2815cSJohn Baldwin * Detect ACPI, perform early initialisation 310bbc2815cSJohn Baldwin */ 311bbc2815cSJohn Baldwin static void 312bbc2815cSJohn Baldwin acpi_identify(driver_t *driver, device_t parent) 313bbc2815cSJohn Baldwin { 314bbc2815cSJohn Baldwin device_t child; 315bbc2815cSJohn Baldwin 316bbc2815cSJohn Baldwin ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 317bbc2815cSJohn Baldwin 318bbc2815cSJohn Baldwin if (!cold) 319bbc2815cSJohn Baldwin return_VOID; 320bbc2815cSJohn Baldwin 321bbc2815cSJohn Baldwin /* Check that we haven't been disabled with a hint. */ 322bbc2815cSJohn Baldwin if (resource_disabled("acpi", 0)) 323bbc2815cSJohn Baldwin return_VOID; 324bbc2815cSJohn Baldwin 325bbc2815cSJohn Baldwin /* Make sure we're not being doubly invoked. */ 326bbc2815cSJohn Baldwin if (device_find_child(parent, "acpi", 0) != NULL) 327bbc2815cSJohn Baldwin return_VOID; 328bbc2815cSJohn Baldwin 329bbc2815cSJohn Baldwin /* Initialize ACPI-CA. */ 330bbc2815cSJohn Baldwin if (ACPI_FAILURE(acpi_Startup())) 331bbc2815cSJohn Baldwin return_VOID; 33215e32d5dSMike Smith 3334e376d58SNate Lawson snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION); 3344e376d58SNate Lawson 335be2b1797SNate Lawson /* Attach the actual ACPI device. */ 33615e32d5dSMike Smith if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) { 33715e32d5dSMike Smith device_printf(parent, "ACPI: could not attach\n"); 3380ae55423SMike Smith return_VOID; 33915e32d5dSMike Smith } 34015e32d5dSMike Smith } 34115e32d5dSMike Smith 34215e32d5dSMike Smith /* 34315e32d5dSMike Smith * Fetch some descriptive data from ACPI to put in our attach message 34415e32d5dSMike Smith */ 34515e32d5dSMike Smith static int 34615e32d5dSMike Smith acpi_probe(device_t dev) 34715e32d5dSMike Smith { 34815e32d5dSMike Smith ACPI_TABLE_HEADER th; 34915e32d5dSMike Smith char buf[20]; 35015e32d5dSMike Smith int error; 3512ccd1cacSNate Lawson struct sbuf sb; 3522ccd1cacSNate Lawson ACPI_STATUS status; 353fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 35415e32d5dSMike Smith 355b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 356f9390180SMitsuru IWASAKI 357f9390180SMitsuru IWASAKI if (power_pm_get_type() != POWER_PM_TYPE_NONE && 358f9390180SMitsuru IWASAKI power_pm_get_type() != POWER_PM_TYPE_ACPI) { 359be2b1797SNate Lawson 360f9390180SMitsuru IWASAKI device_printf(dev, "Other PM system enabled.\n"); 361f9390180SMitsuru IWASAKI return_VALUE(ENXIO); 362f9390180SMitsuru IWASAKI } 363f9390180SMitsuru IWASAKI 364cb9b0d80SMike Smith ACPI_LOCK; 3650ae55423SMike Smith 366b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) { 367be2b1797SNate Lawson device_printf(dev, "couldn't get XSDT header: %s\n", 368be2b1797SNate Lawson AcpiFormatException(status)); 369cb9b0d80SMike Smith error = ENXIO; 370cb9b0d80SMike Smith } else { 3712ccd1cacSNate Lawson sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN); 3722ccd1cacSNate Lawson sbuf_bcat(&sb, th.OemId, 6); 3732ccd1cacSNate Lawson sbuf_trim(&sb); 3742ccd1cacSNate Lawson sbuf_putc(&sb, ' '); 3752ccd1cacSNate Lawson sbuf_bcat(&sb, th.OemTableId, 8); 3762ccd1cacSNate Lawson sbuf_trim(&sb); 3772ccd1cacSNate Lawson sbuf_finish(&sb); 3782ccd1cacSNate Lawson device_set_desc_copy(dev, sbuf_data(&sb)); 3792ccd1cacSNate Lawson sbuf_delete(&sb); 380cb9b0d80SMike Smith error = 0; 381cb9b0d80SMike Smith } 382cb9b0d80SMike Smith ACPI_UNLOCK; 383cb9b0d80SMike Smith return_VALUE(error); 38415e32d5dSMike Smith } 38515e32d5dSMike Smith 38615e32d5dSMike Smith static int 38715e32d5dSMike Smith acpi_attach(device_t dev) 38815e32d5dSMike Smith { 38915e32d5dSMike Smith struct acpi_softc *sc; 390cb9b0d80SMike Smith ACPI_STATUS status; 39115e32d5dSMike Smith int error; 39232d18aa5SMike Smith UINT32 flags; 393498d464fSMitsuru IWASAKI char *env; 394d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 395d786139cSMaxime Henrion char *debugpoint; 39615e32d5dSMike Smith #endif 397fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 39815e32d5dSMike Smith 399b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 400cb9b0d80SMike Smith ACPI_LOCK; 40115e32d5dSMike Smith sc = device_get_softc(dev); 40215e32d5dSMike Smith bzero(sc, sizeof(*sc)); 40315e32d5dSMike Smith sc->acpi_dev = dev; 40415e32d5dSMike Smith 405d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 406d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 407d786139cSMaxime Henrion if (debugpoint) { 408d786139cSMaxime Henrion if (!strcmp(debugpoint, "spaces")) 40915e32d5dSMike Smith acpi_EnterDebugger(); 410d786139cSMaxime Henrion freeenv(debugpoint); 411d786139cSMaxime Henrion } 41215e32d5dSMike Smith #endif 41315e32d5dSMike Smith 414be2b1797SNate Lawson /* Install the default address space handlers. */ 415cb9b0d80SMike Smith error = ENXIO; 416be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 417be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL); 418be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 419be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemMemory handler: %s\n", 420be2b1797SNate Lawson AcpiFormatException(status)); 421cb9b0d80SMike Smith goto out; 42215e32d5dSMike Smith } 423be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 424be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL); 425be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 426be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemIO handler: %s\n", 427be2b1797SNate Lawson AcpiFormatException(status)); 428cb9b0d80SMike Smith goto out; 42915e32d5dSMike Smith } 430be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 431be2b1797SNate Lawson ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL); 432be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 433be2b1797SNate Lawson device_printf(dev, "could not initialise PciConfig handler: %s\n", 434be2b1797SNate Lawson AcpiFormatException(status)); 435cb9b0d80SMike Smith goto out; 43615e32d5dSMike Smith } 43715e32d5dSMike Smith 43815e32d5dSMike Smith /* 43915e32d5dSMike Smith * Bring ACPI fully online. 44015e32d5dSMike Smith * 441be2b1797SNate Lawson * Note that some systems (specifically, those with namespace evaluation 442be2b1797SNate Lawson * issues that require the avoidance of parts of the namespace) must 443be2b1797SNate Lawson * avoid running _INI and _STA on everything, as well as dodging the final 444be2b1797SNate Lawson * object init pass. 44515e32d5dSMike Smith * 44632d18aa5SMike Smith * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 44732d18aa5SMike Smith * 448be2b1797SNate Lawson * XXX We should arrange for the object init pass after we have attached 449be2b1797SNate Lawson * all our child devices, but on many systems it works here. 45015e32d5dSMike Smith */ 451d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 452d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 453d786139cSMaxime Henrion if (debugpoint) { 454d786139cSMaxime Henrion if (!strcmp(debugpoint, "enable")) 45515e32d5dSMike Smith acpi_EnterDebugger(); 456d786139cSMaxime Henrion freeenv(debugpoint); 457d786139cSMaxime Henrion } 45815e32d5dSMike Smith #endif 45932d18aa5SMike Smith flags = 0; 460d786139cSMaxime Henrion if (testenv("debug.acpi.avoid")) 46132d18aa5SMike Smith flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 462b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 463be2b1797SNate Lawson device_printf(dev, "Could not enable ACPI: %s\n", 464be2b1797SNate Lawson AcpiFormatException(status)); 465cb9b0d80SMike Smith goto out; 46615e32d5dSMike Smith } 46715e32d5dSMike Smith 468f8335e3aSNate Lawson /* 469f8335e3aSNate Lawson * Call the ECDT probe function to provide EC functionality before 470f8335e3aSNate Lawson * the namespace has been evaluated. 471f8335e3aSNate Lawson */ 472f8335e3aSNate Lawson acpi_ec_ecdt_probe(dev); 473f8335e3aSNate Lawson 474b69ed3f4SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 475be2b1797SNate Lawson device_printf(dev, "Could not initialize ACPI objects: %s\n", 476be2b1797SNate Lawson AcpiFormatException(status)); 477b69ed3f4SMitsuru IWASAKI goto out; 478b69ed3f4SMitsuru IWASAKI } 479b69ed3f4SMitsuru IWASAKI 48015e32d5dSMike Smith /* 4811d073b1dSJohn Baldwin * Setup our sysctl tree. 4821d073b1dSJohn Baldwin * 4831d073b1dSJohn Baldwin * XXX: This doesn't check to make sure that none of these fail. 4841d073b1dSJohn Baldwin */ 4851d073b1dSJohn Baldwin sysctl_ctx_init(&sc->acpi_sysctl_ctx); 4861d073b1dSJohn Baldwin sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 4871d073b1dSJohn Baldwin SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 4881d073b1dSJohn Baldwin device_get_name(dev), CTLFLAG_RD, 0, ""); 4891d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 490d75de536SMitsuru IWASAKI OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD, 491d75de536SMitsuru IWASAKI 0, 0, acpi_supported_sleep_state_sysctl, "A", ""); 492d75de536SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4931d073b1dSJohn Baldwin OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 4941d073b1dSJohn Baldwin &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4951d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4961d073b1dSJohn Baldwin OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 4971d073b1dSJohn Baldwin &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4981d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4991d073b1dSJohn Baldwin OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 5001d073b1dSJohn Baldwin &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 501f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 502f86214b6SMitsuru IWASAKI OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 503f86214b6SMitsuru IWASAKI &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 504f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 505f86214b6SMitsuru IWASAKI OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 506f86214b6SMitsuru IWASAKI &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5072d644607SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 508ff01efb5SMitsuru IWASAKI OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW, 509ff01efb5SMitsuru IWASAKI &sc->acpi_sleep_delay, 0, "sleep delay"); 510ff01efb5SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5111611ea87SMitsuru IWASAKI OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW, 5121611ea87SMitsuru IWASAKI &sc->acpi_s4bios, 0, "S4BIOS mode"); 5131611ea87SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5142d644607SMitsuru IWASAKI OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW, 5152d644607SMitsuru IWASAKI &sc->acpi_verbose, 0, "verbose mode"); 516c6a78e98STakanori Watanabe SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 517c6a78e98STakanori Watanabe OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW, 518c6a78e98STakanori Watanabe &sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff"); 519bf0c18ecSNate Lawson 520bf0c18ecSNate Lawson /* 521bf0c18ecSNate Lawson * Default to 5 seconds before sleeping to give some machines time to 522bf0c18ecSNate Lawson * stabilize. 523bf0c18ecSNate Lawson */ 524bf0c18ecSNate Lawson sc->acpi_sleep_delay = 5; 525c6a78e98STakanori Watanabe sc->acpi_disable_on_poweroff = 1; 5262d644607SMitsuru IWASAKI if (bootverbose) 5272d644607SMitsuru IWASAKI sc->acpi_verbose = 1; 528498d464fSMitsuru IWASAKI if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) { 529498d464fSMitsuru IWASAKI sc->acpi_verbose = 1; 530498d464fSMitsuru IWASAKI freeenv(env); 531498d464fSMitsuru IWASAKI } 5321d073b1dSJohn Baldwin 5332d610c46SNate Lawson /* Only enable S4BIOS by default if the FACS says it is available. */ 5342d610c46SNate Lawson if (AcpiGbl_FACS->S4Bios_f != 0) 5352d610c46SNate Lawson sc->acpi_s4bios = 1; 5362d610c46SNate Lawson 5371d073b1dSJohn Baldwin /* 53815e32d5dSMike Smith * Dispatch the default sleep state to devices. 53915e32d5dSMike Smith * TBD: should be configured from userland policy manager. 54015e32d5dSMike Smith */ 54115e32d5dSMike Smith sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX; 54215e32d5dSMike Smith sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX; 54315e32d5dSMike Smith sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX; 544f86214b6SMitsuru IWASAKI sc->acpi_standby_sx = ACPI_STATE_S1; 545f86214b6SMitsuru IWASAKI sc->acpi_suspend_sx = ACPI_STATE_S3; 54615e32d5dSMike Smith 54713d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 54815e32d5dSMike Smith 54915e32d5dSMike Smith /* 55015e32d5dSMike Smith * Scan the namespace and attach/initialise children. 55115e32d5dSMike Smith */ 552d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 553d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 554d786139cSMaxime Henrion if (debugpoint) { 555d786139cSMaxime Henrion if (!strcmp(debugpoint, "probe")) 55615e32d5dSMike Smith acpi_EnterDebugger(); 557d786139cSMaxime Henrion freeenv(debugpoint); 558d786139cSMaxime Henrion } 55915e32d5dSMike Smith #endif 56015e32d5dSMike Smith 561be2b1797SNate Lawson /* Register our shutdown handlers */ 562be2b1797SNate Lawson EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, 563be2b1797SNate Lawson SHUTDOWN_PRI_LAST); 564be2b1797SNate Lawson EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, 565be2b1797SNate Lawson SHUTDOWN_PRI_LAST); 56615e32d5dSMike Smith 56715e32d5dSMike Smith /* 56815e32d5dSMike Smith * Register our acpi event handlers. 56915e32d5dSMike Smith * XXX should be configurable eg. via userland policy manager. 57015e32d5dSMike Smith */ 571be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, 572be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 573be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, 574be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 57515e32d5dSMike Smith 576be2b1797SNate Lawson /* Flag our initial states. */ 57715e32d5dSMike Smith sc->acpi_enabled = 1; 57815e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 579ece50487SMitsuru IWASAKI sc->acpi_sleep_disabled = 0; 58015e32d5dSMike Smith 581be2b1797SNate Lawson /* Create the control device */ 582a89fcf28STakanori Watanabe sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644, 5832a4689e8SRobert Watson "acpi"); 58415e32d5dSMike Smith sc->acpi_dev_t->si_drv1 = sc; 58515e32d5dSMike Smith 586d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 587d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 588d786139cSMaxime Henrion if (debugpoint) { 589be2b1797SNate Lawson if (strcmp(debugpoint, "running") == 0) 59015e32d5dSMike Smith acpi_EnterDebugger(); 591d786139cSMaxime Henrion freeenv(debugpoint); 592d786139cSMaxime Henrion } 59315e32d5dSMike Smith #endif 594f86214b6SMitsuru IWASAKI 59591da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS 596be2b1797SNate Lawson if ((error = acpi_task_thread_init())) 597c573e654SMitsuru IWASAKI goto out; 598c573e654SMitsuru IWASAKI #endif 599c573e654SMitsuru IWASAKI 600be2b1797SNate Lawson if ((error = acpi_machdep_init(dev))) 601f86214b6SMitsuru IWASAKI goto out; 602f86214b6SMitsuru IWASAKI 603f9390180SMitsuru IWASAKI /* Register ACPI again to pass the correct argument of pm_func. */ 604f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 605f9390180SMitsuru IWASAKI 606eeb6dba2SJohn Baldwin if (!acpi_disabled("bus")) 607eeb6dba2SJohn Baldwin acpi_probe_children(dev); 608eeb6dba2SJohn Baldwin 609cb9b0d80SMike Smith error = 0; 610cb9b0d80SMike Smith 611cb9b0d80SMike Smith out: 612cb9b0d80SMike Smith ACPI_UNLOCK; 613cb9b0d80SMike Smith return_VALUE (error); 61415e32d5dSMike Smith } 61515e32d5dSMike Smith 6163184cf5aSNate Lawson static void 6173184cf5aSNate Lawson acpi_quirks_set() 6183184cf5aSNate Lawson { 6193184cf5aSNate Lawson XSDT_DESCRIPTOR *xsdt; 6203184cf5aSNate Lawson struct acpi_quirks *quirk; 6213184cf5aSNate Lawson char *env, *tmp; 6223184cf5aSNate Lawson int len; 6233184cf5aSNate Lawson 6244e376d58SNate Lawson /* 6254e376d58SNate Lawson * If the user loaded a custom table or disabled "quirks", leave 6264e376d58SNate Lawson * the settings alone. 6274e376d58SNate Lawson */ 6283184cf5aSNate Lawson len = 0; 6294e376d58SNate Lawson if ((env = getenv("acpi_dsdt_load")) != NULL) { 6304e376d58SNate Lawson /* XXX No strcasecmp but this is good enough. */ 6314e376d58SNate Lawson if (*env == 'Y' || *env == 'y') 6324e376d58SNate Lawson goto out; 6334e376d58SNate Lawson freeenv(env); 6344e376d58SNate Lawson } 6353184cf5aSNate Lawson if ((env = getenv("debug.acpi.disabled")) != NULL) { 6364e376d58SNate Lawson if (strstr("quirks", env) != NULL) 6373184cf5aSNate Lawson goto out; 6383184cf5aSNate Lawson len = strlen(env); 6393184cf5aSNate Lawson } 6403184cf5aSNate Lawson 6413184cf5aSNate Lawson /* 6423184cf5aSNate Lawson * Search through our quirk table and concatenate the disabled 6433184cf5aSNate Lawson * values with whatever we find. 6443184cf5aSNate Lawson */ 6453184cf5aSNate Lawson xsdt = AcpiGbl_XSDT; 6463184cf5aSNate Lawson for (quirk = acpi_quirks_table; quirk->OemId; quirk++) { 6473184cf5aSNate Lawson if (!strncmp(xsdt->OemId, quirk->OemId, strlen(quirk->OemId)) && 6483184cf5aSNate Lawson (xsdt->OemRevision == quirk->OemRevision || 6493184cf5aSNate Lawson quirk->OemRevision == ACPI_OEM_REV_ANY)) { 6503184cf5aSNate Lawson len += strlen(quirk->value) + 2; 6513184cf5aSNate Lawson if ((tmp = malloc(len, M_TEMP, M_NOWAIT)) == NULL) 6523184cf5aSNate Lawson goto out; 6533184cf5aSNate Lawson sprintf(tmp, "%s %s", env ? env : "", quirk->value); 6543184cf5aSNate Lawson setenv("debug.acpi.disabled", tmp); 6553184cf5aSNate Lawson free(tmp, M_TEMP); 6563184cf5aSNate Lawson break; 6573184cf5aSNate Lawson } 6583184cf5aSNate Lawson } 6593184cf5aSNate Lawson 6603184cf5aSNate Lawson out: 6613184cf5aSNate Lawson if (env) 6623184cf5aSNate Lawson freeenv(env); 6633184cf5aSNate Lawson } 6643184cf5aSNate Lawson 66515e32d5dSMike Smith /* 66615e32d5dSMike Smith * Handle a new device being added 66715e32d5dSMike Smith */ 66815e32d5dSMike Smith static device_t 66915e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit) 67015e32d5dSMike Smith { 67115e32d5dSMike Smith struct acpi_device *ad; 67215e32d5dSMike Smith device_t child; 67315e32d5dSMike Smith 674be2b1797SNate Lawson if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL) 67515e32d5dSMike Smith return (NULL); 67615e32d5dSMike Smith 67715e32d5dSMike Smith resource_list_init(&ad->ad_rl); 67815e32d5dSMike Smith 67915e32d5dSMike Smith child = device_add_child_ordered(bus, order, name, unit); 68015e32d5dSMike Smith if (child != NULL) 68115e32d5dSMike Smith device_set_ivars(child, ad); 68215e32d5dSMike Smith return (child); 68315e32d5dSMike Smith } 68415e32d5dSMike Smith 68515e32d5dSMike Smith static int 68615e32d5dSMike Smith acpi_print_child(device_t bus, device_t child) 68715e32d5dSMike Smith { 68815e32d5dSMike Smith struct acpi_device *adev = device_get_ivars(child); 68915e32d5dSMike Smith struct resource_list *rl = &adev->ad_rl; 69015e32d5dSMike Smith int retval = 0; 69115e32d5dSMike Smith 69215e32d5dSMike Smith retval += bus_print_child_header(bus, child); 6939ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 6949ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 6959ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 6969ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 69715e32d5dSMike Smith retval += bus_print_child_footer(bus, child); 69815e32d5dSMike Smith 69915e32d5dSMike Smith return (retval); 70015e32d5dSMike Smith } 70115e32d5dSMike Smith 70215e32d5dSMike Smith /* 703247648afSTakanori Watanabe * Location hint for devctl(8) 704247648afSTakanori Watanabe */ 705247648afSTakanori Watanabe 706247648afSTakanori Watanabe int 707247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf, 708247648afSTakanori Watanabe size_t buflen) 709247648afSTakanori Watanabe { 710247648afSTakanori Watanabe struct acpi_device *dinfo = device_get_ivars(child); 711247648afSTakanori Watanabe 712247648afSTakanori Watanabe if (dinfo->ad_handle) 713247648afSTakanori Watanabe snprintf(buf, buflen, "path=%s", acpi_name(dinfo->ad_handle)); 714247648afSTakanori Watanabe else 715247648afSTakanori Watanabe snprintf(buf, buflen, "magic=unknown"); 716247648afSTakanori Watanabe return (0); 717247648afSTakanori Watanabe } 718247648afSTakanori Watanabe 719247648afSTakanori Watanabe /* 720247648afSTakanori Watanabe * PnP information for devctl(8) 721247648afSTakanori Watanabe */ 722247648afSTakanori Watanabe 723247648afSTakanori Watanabe int 724247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf, 725247648afSTakanori Watanabe size_t buflen) 726247648afSTakanori Watanabe { 727247648afSTakanori Watanabe struct acpi_device *dinfo = device_get_ivars(child); 728247648afSTakanori Watanabe ACPI_DEVICE_INFO *adinfo; 729247648afSTakanori Watanabe ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL}; 730247648afSTakanori Watanabe char *end; 731247648afSTakanori Watanabe int error; 732247648afSTakanori Watanabe 733247648afSTakanori Watanabe error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf); 734247648afSTakanori Watanabe adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer; 735247648afSTakanori Watanabe 736247648afSTakanori Watanabe if (error) 737247648afSTakanori Watanabe snprintf(buf, buflen, "Unknown"); 738247648afSTakanori Watanabe else 739247648afSTakanori Watanabe snprintf(buf, buflen, "_HID=%s _UID=%lu", 740247648afSTakanori Watanabe (adinfo->Valid & ACPI_VALID_HID)? 741247648afSTakanori Watanabe adinfo->HardwareId.Value : "UNKNOWN", 742247648afSTakanori Watanabe ((adinfo->Valid & ACPI_VALID_UID)? 743247648afSTakanori Watanabe strtoul(adinfo->UniqueId.Value, &end, 10):0 )); 744247648afSTakanori Watanabe 745247648afSTakanori Watanabe if (adinfo) 746247648afSTakanori Watanabe AcpiOsFree(adinfo); 747247648afSTakanori Watanabe 748247648afSTakanori Watanabe return (0); 749247648afSTakanori Watanabe } 750247648afSTakanori Watanabe 751247648afSTakanori Watanabe /* 75215e32d5dSMike Smith * Handle per-device ivars 75315e32d5dSMike Smith */ 75415e32d5dSMike Smith static int 75515e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 75615e32d5dSMike Smith { 75715e32d5dSMike Smith struct acpi_device *ad; 75815e32d5dSMike Smith 75915e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 76015e32d5dSMike Smith printf("device has no ivars\n"); 76115e32d5dSMike Smith return (ENOENT); 76215e32d5dSMike Smith } 76315e32d5dSMike Smith 764be2b1797SNate Lawson /* ACPI and ISA compatibility ivars */ 76515e32d5dSMike Smith switch(index) { 76615e32d5dSMike Smith case ACPI_IVAR_HANDLE: 76715e32d5dSMike Smith *(ACPI_HANDLE *)result = ad->ad_handle; 76815e32d5dSMike Smith break; 76915e32d5dSMike Smith case ACPI_IVAR_MAGIC: 77015e32d5dSMike Smith *(int *)result = ad->ad_magic; 77115e32d5dSMike Smith break; 77215e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 77315e32d5dSMike Smith *(void **)result = ad->ad_private; 77415e32d5dSMike Smith break; 77532d18aa5SMike Smith case ISA_IVAR_VENDORID: 77632d18aa5SMike Smith case ISA_IVAR_SERIAL: 77732d18aa5SMike Smith case ISA_IVAR_COMPATID: 77832d18aa5SMike Smith *(int *)result = -1; 77932d18aa5SMike Smith break; 78032d18aa5SMike Smith case ISA_IVAR_LOGICALID: 78132d18aa5SMike Smith *(int *)result = acpi_isa_get_logicalid(child); 78232d18aa5SMike Smith break; 78315e32d5dSMike Smith default: 78415e32d5dSMike Smith return (ENOENT); 78515e32d5dSMike Smith } 786be2b1797SNate Lawson 78715e32d5dSMike Smith return (0); 78815e32d5dSMike Smith } 78915e32d5dSMike Smith 79015e32d5dSMike Smith static int 79115e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 79215e32d5dSMike Smith { 79315e32d5dSMike Smith struct acpi_device *ad; 79415e32d5dSMike Smith 79515e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 79615e32d5dSMike Smith printf("device has no ivars\n"); 79715e32d5dSMike Smith return (ENOENT); 79815e32d5dSMike Smith } 79915e32d5dSMike Smith 80015e32d5dSMike Smith switch(index) { 80115e32d5dSMike Smith case ACPI_IVAR_HANDLE: 80215e32d5dSMike Smith ad->ad_handle = (ACPI_HANDLE)value; 80315e32d5dSMike Smith break; 80415e32d5dSMike Smith case ACPI_IVAR_MAGIC: 80515e32d5dSMike Smith ad->ad_magic = (int)value; 80615e32d5dSMike Smith break; 80715e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 80815e32d5dSMike Smith ad->ad_private = (void *)value; 80915e32d5dSMike Smith break; 81015e32d5dSMike Smith default: 8112ab060eeSWarner Losh panic("bad ivar write request (%d)", index); 81215e32d5dSMike Smith return (ENOENT); 81315e32d5dSMike Smith } 814be2b1797SNate Lawson 81515e32d5dSMike Smith return (0); 81615e32d5dSMike Smith } 81715e32d5dSMike Smith 818be2b1797SNate Lawson ACPI_HANDLE 819be2b1797SNate Lawson acpi_get_handle(device_t dev) 820be2b1797SNate Lawson { 821be2b1797SNate Lawson uintptr_t up; 822be2b1797SNate Lawson ACPI_HANDLE h; 823be2b1797SNate Lawson 824be2b1797SNate Lawson if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, &up)) 825be2b1797SNate Lawson return(NULL); 826be2b1797SNate Lawson h = (ACPI_HANDLE)up; 827be2b1797SNate Lawson return (h); 828be2b1797SNate Lawson } 829be2b1797SNate Lawson 830be2b1797SNate Lawson int 831be2b1797SNate Lawson acpi_set_handle(device_t dev, ACPI_HANDLE h) 832be2b1797SNate Lawson { 833be2b1797SNate Lawson uintptr_t up; 834be2b1797SNate Lawson 835be2b1797SNate Lawson up = (uintptr_t)h; 836be2b1797SNate Lawson return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, up)); 837be2b1797SNate Lawson } 838be2b1797SNate Lawson 839be2b1797SNate Lawson int 840be2b1797SNate Lawson acpi_get_magic(device_t dev) 841be2b1797SNate Lawson { 842be2b1797SNate Lawson uintptr_t up; 843be2b1797SNate Lawson int m; 844be2b1797SNate Lawson 845be2b1797SNate Lawson if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, &up)) 846be2b1797SNate Lawson return(0); 847be2b1797SNate Lawson m = (int)up; 848be2b1797SNate Lawson return (m); 849be2b1797SNate Lawson } 850be2b1797SNate Lawson 851be2b1797SNate Lawson int 852be2b1797SNate Lawson acpi_set_magic(device_t dev, int m) 853be2b1797SNate Lawson { 854be2b1797SNate Lawson uintptr_t up; 855be2b1797SNate Lawson 856be2b1797SNate Lawson up = (uintptr_t)m; 857be2b1797SNate Lawson return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, up)); 858be2b1797SNate Lawson } 859be2b1797SNate Lawson 860be2b1797SNate Lawson void * 861be2b1797SNate Lawson acpi_get_private(device_t dev) 862be2b1797SNate Lawson { 863be2b1797SNate Lawson uintptr_t up; 864be2b1797SNate Lawson void *p; 865be2b1797SNate Lawson 866be2b1797SNate Lawson if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, &up)) 867be2b1797SNate Lawson return (NULL); 868be2b1797SNate Lawson p = (void *)up; 869be2b1797SNate Lawson return (p); 870be2b1797SNate Lawson } 871be2b1797SNate Lawson 872be2b1797SNate Lawson int 873be2b1797SNate Lawson acpi_set_private(device_t dev, void *p) 874be2b1797SNate Lawson { 875be2b1797SNate Lawson uintptr_t up; 876be2b1797SNate Lawson 877be2b1797SNate Lawson up = (uintptr_t)p; 878be2b1797SNate Lawson return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, up)); 879be2b1797SNate Lawson } 880be2b1797SNate Lawson 881be2b1797SNate Lawson ACPI_OBJECT_TYPE 882be2b1797SNate Lawson acpi_get_type(device_t dev) 883be2b1797SNate Lawson { 884be2b1797SNate Lawson ACPI_HANDLE h; 885be2b1797SNate Lawson ACPI_OBJECT_TYPE t; 886be2b1797SNate Lawson 887be2b1797SNate Lawson if ((h = acpi_get_handle(dev)) == NULL) 888be2b1797SNate Lawson return (ACPI_TYPE_NOT_FOUND); 889be2b1797SNate Lawson if (AcpiGetType(h, &t) != AE_OK) 890be2b1797SNate Lawson return (ACPI_TYPE_NOT_FOUND); 891be2b1797SNate Lawson return (t); 892be2b1797SNate Lawson } 893be2b1797SNate Lawson 89415e32d5dSMike Smith /* 89515e32d5dSMike Smith * Handle child resource allocation/removal 89615e32d5dSMike Smith */ 89715e32d5dSMike Smith static int 898be2b1797SNate Lawson acpi_set_resource(device_t dev, device_t child, int type, int rid, 899be2b1797SNate Lawson u_long start, u_long count) 90015e32d5dSMike Smith { 90115e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 90215e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 90315e32d5dSMike Smith 90415e32d5dSMike Smith resource_list_add(rl, type, rid, start, start + count -1, count); 90515e32d5dSMike Smith 90615e32d5dSMike Smith return(0); 90715e32d5dSMike Smith } 90815e32d5dSMike Smith 90915e32d5dSMike Smith static int 910be2b1797SNate Lawson acpi_get_resource(device_t dev, device_t child, int type, int rid, 911be2b1797SNate Lawson u_long *startp, u_long *countp) 91215e32d5dSMike Smith { 91315e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 91415e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 91515e32d5dSMike Smith struct resource_list_entry *rle; 91615e32d5dSMike Smith 91715e32d5dSMike Smith rle = resource_list_find(rl, type, rid); 91815e32d5dSMike Smith if (!rle) 91915e32d5dSMike Smith return(ENOENT); 92015e32d5dSMike Smith 92115e32d5dSMike Smith if (startp) 92215e32d5dSMike Smith *startp = rle->start; 92315e32d5dSMike Smith if (countp) 92415e32d5dSMike Smith *countp = rle->count; 92515e32d5dSMike Smith 92615e32d5dSMike Smith return (0); 92715e32d5dSMike Smith } 92815e32d5dSMike Smith 92915e32d5dSMike Smith static struct resource * 93015e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 93115e32d5dSMike Smith u_long start, u_long end, u_long count, u_int flags) 93215e32d5dSMike Smith { 93315e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 93415e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 93515e32d5dSMike Smith 936be2b1797SNate Lawson return (resource_list_alloc(rl, bus, child, type, rid, start, end, count, 937be2b1797SNate Lawson flags)); 93815e32d5dSMike Smith } 93915e32d5dSMike Smith 94015e32d5dSMike Smith static int 94115e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r) 94215e32d5dSMike Smith { 94315e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 94415e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 94515e32d5dSMike Smith 94615e32d5dSMike Smith return (resource_list_release(rl, bus, child, type, rid, r)); 94715e32d5dSMike Smith } 94815e32d5dSMike Smith 949dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */ 950dc750869SNate Lawson struct resource * 951dc750869SNate Lawson acpi_bus_alloc_gas(device_t dev, int *rid, ACPI_GENERIC_ADDRESS *gas) 952dc750869SNate Lawson { 953dc750869SNate Lawson int type; 954dc750869SNate Lawson 955dc750869SNate Lawson if (gas == NULL || !ACPI_VALID_ADDRESS(gas->Address) || 956dc750869SNate Lawson gas->RegisterBitWidth < 8) 957dc750869SNate Lawson return (NULL); 958dc750869SNate Lawson 959dc750869SNate Lawson switch (gas->AddressSpaceId) { 960dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_MEMORY: 961dc750869SNate Lawson type = SYS_RES_MEMORY; 962dc750869SNate Lawson break; 963dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_IO: 964dc750869SNate Lawson type = SYS_RES_IOPORT; 965dc750869SNate Lawson break; 966dc750869SNate Lawson default: 967dc750869SNate Lawson return (NULL); 968dc750869SNate Lawson } 969dc750869SNate Lawson 970dc750869SNate Lawson bus_set_resource(dev, type, *rid, gas->Address, gas->RegisterBitWidth / 8); 9715f96beb9SNate Lawson return (bus_alloc_resource_any(dev, type, rid, RF_ACTIVE)); 972dc750869SNate Lawson } 973dc750869SNate Lawson 97415e32d5dSMike Smith /* 975bc0f2195SMike Smith * Handle ISA-like devices probing for a PnP ID to match. 976bc0f2195SMike Smith */ 977bc0f2195SMike Smith #define PNP_EISAID(s) \ 978bc0f2195SMike Smith ((((s[0] - '@') & 0x1f) << 2) \ 979bc0f2195SMike Smith | (((s[1] - '@') & 0x18) >> 3) \ 980bc0f2195SMike Smith | (((s[1] - '@') & 0x07) << 13) \ 981bc0f2195SMike Smith | (((s[2] - '@') & 0x1f) << 8) \ 982bc0f2195SMike Smith | (PNP_HEXTONUM(s[4]) << 16) \ 983bc0f2195SMike Smith | (PNP_HEXTONUM(s[3]) << 20) \ 984bc0f2195SMike Smith | (PNP_HEXTONUM(s[6]) << 24) \ 985bc0f2195SMike Smith | (PNP_HEXTONUM(s[5]) << 28)) 986bc0f2195SMike Smith 9871e4925e8SNate Lawson static uint32_t 98832d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev) 989bc0f2195SMike Smith { 9901e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 9911e4925e8SNate Lawson ACPI_BUFFER buf; 992bc0f2195SMike Smith ACPI_HANDLE h; 993bc0f2195SMike Smith ACPI_STATUS error; 994bc0f2195SMike Smith u_int32_t pnpid; 995fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 99632d18aa5SMike Smith 997b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 99832d18aa5SMike Smith 99932d18aa5SMike Smith pnpid = 0; 1000bd189fe7SNate Lawson buf.Pointer = NULL; 1001bd189fe7SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 1002bd189fe7SNate Lawson 100332d18aa5SMike Smith ACPI_LOCK; 100432d18aa5SMike Smith 10056fca9360SNate Lawson /* Fetch and validate the HID. */ 100632d18aa5SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1007bd189fe7SNate Lawson goto out; 10086fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 10096fca9360SNate Lawson if (ACPI_FAILURE(error)) 1010bd189fe7SNate Lawson goto out; 10111e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 101232d18aa5SMike Smith 10131e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_HID) != 0) 10141e4925e8SNate Lawson pnpid = PNP_EISAID(devinfo->HardwareId.Value); 1015be2b1797SNate Lawson 1016bd189fe7SNate Lawson out: 1017bd189fe7SNate Lawson if (buf.Pointer != NULL) 10181e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 101932d18aa5SMike Smith ACPI_UNLOCK; 102032d18aa5SMike Smith return_VALUE (pnpid); 102132d18aa5SMike Smith } 102232d18aa5SMike Smith 10231e4925e8SNate Lawson static int 10241e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count) 1025b2566207STakanori Watanabe { 10261e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 10271e4925e8SNate Lawson ACPI_BUFFER buf; 1028b2566207STakanori Watanabe ACPI_HANDLE h; 1029b2566207STakanori Watanabe ACPI_STATUS error; 10301e4925e8SNate Lawson uint32_t *pnpid; 10311e4925e8SNate Lawson int valid, i; 1032b2566207STakanori Watanabe ACPI_LOCK_DECL; 1033b2566207STakanori Watanabe 1034b2566207STakanori Watanabe ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1035b2566207STakanori Watanabe 10361e4925e8SNate Lawson pnpid = cids; 10371e4925e8SNate Lawson valid = 0; 1038bd189fe7SNate Lawson buf.Pointer = NULL; 1039bd189fe7SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 1040bd189fe7SNate Lawson 1041b2566207STakanori Watanabe ACPI_LOCK; 1042b2566207STakanori Watanabe 10431e4925e8SNate Lawson /* Fetch and validate the CID */ 1044b2566207STakanori Watanabe if ((h = acpi_get_handle(dev)) == NULL) 1045bd189fe7SNate Lawson goto out; 10461e4925e8SNate Lawson error = AcpiGetObjectInfo(h, &buf); 10471e4925e8SNate Lawson if (ACPI_FAILURE(error)) 1048bd189fe7SNate Lawson goto out; 10491e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 10501e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_CID) == 0) 1051b2566207STakanori Watanabe goto out; 1052b2566207STakanori Watanabe 10531e4925e8SNate Lawson if (devinfo->CompatibilityId.Count < count) 10541e4925e8SNate Lawson count = devinfo->CompatibilityId.Count; 10551e4925e8SNate Lawson for (i = 0; i < count; i++) { 10561e4925e8SNate Lawson if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0) 10571e4925e8SNate Lawson continue; 10581e4925e8SNate Lawson *pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value); 10591e4925e8SNate Lawson valid++; 1060b2566207STakanori Watanabe } 1061b2566207STakanori Watanabe 10621e4925e8SNate Lawson out: 1063bd189fe7SNate Lawson if (buf.Pointer != NULL) 10641e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 10651e4925e8SNate Lawson ACPI_UNLOCK; 10661e4925e8SNate Lawson return_VALUE (valid); 10671e4925e8SNate Lawson } 1068b2566207STakanori Watanabe 106932d18aa5SMike Smith static int 107032d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 107132d18aa5SMike Smith { 10721e4925e8SNate Lawson int result, cid_count, i; 10731e4925e8SNate Lawson uint32_t lid, cids[8]; 1074bc0f2195SMike Smith 1075b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1076bc0f2195SMike Smith 1077bc0f2195SMike Smith /* 1078bc0f2195SMike Smith * ISA-style drivers attached to ACPI may persist and 1079bc0f2195SMike Smith * probe manually if we return ENOENT. We never want 1080bc0f2195SMike Smith * that to happen, so don't ever return it. 1081bc0f2195SMike Smith */ 1082bc0f2195SMike Smith result = ENXIO; 1083bc0f2195SMike Smith 1084be2b1797SNate Lawson /* Scan the supplied IDs for a match */ 1085b2566207STakanori Watanabe lid = acpi_isa_get_logicalid(child); 10861e4925e8SNate Lawson cid_count = acpi_isa_get_compatid(child, cids, 8); 1087bc0f2195SMike Smith while (ids && ids->ip_id) { 10881e4925e8SNate Lawson if (lid == ids->ip_id) { 1089bc0f2195SMike Smith result = 0; 1090bc0f2195SMike Smith goto out; 1091bc0f2195SMike Smith } 10921e4925e8SNate Lawson for (i = 0; i < cid_count; i++) { 10931e4925e8SNate Lawson if (cids[i] == ids->ip_id) { 10941e4925e8SNate Lawson result = 0; 10951e4925e8SNate Lawson goto out; 10961e4925e8SNate Lawson } 10971e4925e8SNate Lawson } 1098bc0f2195SMike Smith ids++; 1099bc0f2195SMike Smith } 1100be2b1797SNate Lawson 1101bc0f2195SMike Smith out: 1102bc0f2195SMike Smith return_VALUE (result); 1103bc0f2195SMike Smith } 1104bc0f2195SMike Smith 1105bc0f2195SMike Smith /* 110615e32d5dSMike Smith * Scan relevant portions of the ACPI namespace and attach child devices. 110715e32d5dSMike Smith * 1108be2b1797SNate Lawson * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and 1109be2b1797SNate Lawson * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec. 111015e32d5dSMike Smith */ 111115e32d5dSMike Smith static void 111215e32d5dSMike Smith acpi_probe_children(device_t bus) 111315e32d5dSMike Smith { 111415e32d5dSMike Smith ACPI_HANDLE parent; 1115be2b1797SNate Lawson ACPI_STATUS status; 11167d3bcec9SMike Smith static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL}; 111715e32d5dSMike Smith int i; 111815e32d5dSMike Smith 1119b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1120cb9b0d80SMike Smith ACPI_ASSERTLOCK; 11210ae55423SMike Smith 1122be2b1797SNate Lawson /* Create any static children by calling device identify methods. */ 11234c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 112415e32d5dSMike Smith bus_generic_probe(bus); 112515e32d5dSMike Smith 112615e32d5dSMike Smith /* 112715e32d5dSMike Smith * Scan the namespace and insert placeholders for all the devices that 112815e32d5dSMike Smith * we find. 112915e32d5dSMike Smith * 113015e32d5dSMike Smith * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 1131be2b1797SNate Lawson * we want to create nodes for all devices, not just those that are 1132be2b1797SNate Lawson * currently present. (This assumes that we don't want to create/remove 1133be2b1797SNate Lawson * devices as they appear, which might be smarter.) 113415e32d5dSMike Smith */ 11354c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 1136be2b1797SNate Lawson for (i = 0; scopes[i] != NULL; i++) { 1137be2b1797SNate Lawson status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent); 1138be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1139be2b1797SNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, 1140be2b1797SNate Lawson bus, NULL); 1141be2b1797SNate Lawson } 1142be2b1797SNate Lawson } 114315e32d5dSMike Smith 114415e32d5dSMike Smith /* 114515e32d5dSMike Smith * Scan all of the child devices we have created and let them probe/attach. 114615e32d5dSMike Smith */ 11474c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n")); 114815e32d5dSMike Smith bus_generic_attach(bus); 114915e32d5dSMike Smith 115015e32d5dSMike Smith /* 115115e32d5dSMike Smith * Some of these children may have attached others as part of their attach 115215e32d5dSMike Smith * process (eg. the root PCI bus driver), so rescan. 115315e32d5dSMike Smith */ 11544c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n")); 115515e32d5dSMike Smith bus_generic_attach(bus); 11560ae55423SMike Smith 1157bc0f2195SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 11580ae55423SMike Smith return_VOID; 115915e32d5dSMike Smith } 116015e32d5dSMike Smith 116115e32d5dSMike Smith /* 116215e32d5dSMike Smith * Evaluate a child device and determine whether we might attach a device to 116315e32d5dSMike Smith * it. 116415e32d5dSMike Smith */ 116515e32d5dSMike Smith static ACPI_STATUS 116615e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 116715e32d5dSMike Smith { 116815e32d5dSMike Smith ACPI_OBJECT_TYPE type; 116915e32d5dSMike Smith device_t child, bus = (device_t)context; 117015e32d5dSMike Smith 1171b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 11720ae55423SMike Smith 1173be2b1797SNate Lawson /* Skip this device if we think we'll have trouble with it. */ 11740ae55423SMike Smith if (acpi_avoid(handle)) 11750ae55423SMike Smith return_ACPI_STATUS (AE_OK); 11760ae55423SMike Smith 1177b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 117815e32d5dSMike Smith switch(type) { 117915e32d5dSMike Smith case ACPI_TYPE_DEVICE: 118015e32d5dSMike Smith case ACPI_TYPE_PROCESSOR: 118115e32d5dSMike Smith case ACPI_TYPE_THERMAL: 118215e32d5dSMike Smith case ACPI_TYPE_POWER: 11830ae55423SMike Smith if (acpi_disabled("children")) 11840ae55423SMike Smith break; 1185be2b1797SNate Lawson 118615e32d5dSMike Smith /* 118715e32d5dSMike Smith * Create a placeholder device for this node. Sort the placeholder 118815e32d5dSMike Smith * so that the probe/attach passes will run breadth-first. 118915e32d5dSMike Smith */ 1190be2b1797SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", 1191be2b1797SNate Lawson acpi_name(handle))); 119215e32d5dSMike Smith child = BUS_ADD_CHILD(bus, level * 10, NULL, -1); 1193bc0f2195SMike Smith if (child == NULL) 1194bc0f2195SMike Smith break; 119515e32d5dSMike Smith acpi_set_handle(child, handle); 1196bc0f2195SMike Smith 1197bc0f2195SMike Smith /* 1198b519f9d6SMike Smith * Check that the device is present. If it's not present, 1199b519f9d6SMike Smith * leave it disabled (so that we have a device_t attached to 1200b519f9d6SMike Smith * the handle, but we don't probe it). 1201b519f9d6SMike Smith */ 1202be2b1797SNate Lawson if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) { 1203b519f9d6SMike Smith device_disable(child); 1204b519f9d6SMike Smith break; 1205b519f9d6SMike Smith } 1206b519f9d6SMike Smith 1207b519f9d6SMike Smith /* 1208bc0f2195SMike Smith * Get the device's resource settings and attach them. 1209bc0f2195SMike Smith * Note that if the device has _PRS but no _CRS, we need 1210bc0f2195SMike Smith * to decide when it's appropriate to try to configure the 1211bc0f2195SMike Smith * device. Ignore the return value here; it's OK for the 1212bc0f2195SMike Smith * device not to have any resources. 1213bc0f2195SMike Smith */ 121472ad60adSNate Lawson acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL); 1215bc0f2195SMike Smith 1216be2b1797SNate Lawson /* If we're debugging, probe/attach now rather than later */ 1217b53f2771SMike Smith ACPI_DEBUG_EXEC(device_probe_and_attach(child)); 12180ae55423SMike Smith break; 121915e32d5dSMike Smith } 122015e32d5dSMike Smith } 1221be2b1797SNate Lawson 12220ae55423SMike Smith return_ACPI_STATUS (AE_OK); 122315e32d5dSMike Smith } 122415e32d5dSMike Smith 122515e32d5dSMike Smith static void 122615e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto) 122715e32d5dSMike Smith { 1228c6a78e98STakanori Watanabe struct acpi_softc *sc = arg; 1229c6a78e98STakanori Watanabe 1230cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1231cb9b0d80SMike Smith 123215e32d5dSMike Smith /* 12330ae55423SMike Smith * Disable all ACPI events before soft off, otherwise the system 123415e32d5dSMike Smith * will be turned on again on some laptops. 123515e32d5dSMike Smith * 123615e32d5dSMike Smith * XXX this should probably be restricted to masking some events just 123715e32d5dSMike Smith * before powering down, since we may still need ACPI during the 123815e32d5dSMike Smith * shutdown process. 123915e32d5dSMike Smith */ 1240c6a78e98STakanori Watanabe if (sc->acpi_disable_on_poweroff) 1241c6a78e98STakanori Watanabe acpi_Disable(sc); 124215e32d5dSMike Smith } 124315e32d5dSMike Smith 124415e32d5dSMike Smith static void 124515e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto) 124615e32d5dSMike Smith { 1247eeb3a05fSNate Lawson 1248eeb3a05fSNate Lawson ACPI_ASSERTLOCK; 1249eeb3a05fSNate Lawson 1250eeb3a05fSNate Lawson /* 1251eeb3a05fSNate Lawson * If powering off, run the actual shutdown code on each processor. 1252eeb3a05fSNate Lawson * It will only perform the shutdown on the BSP. Some chipsets do 1253eeb3a05fSNate Lawson * not power off the system correctly if called from an AP. 1254eeb3a05fSNate Lawson */ 1255eeb3a05fSNate Lawson if ((howto & RB_POWEROFF) != 0) { 1256eeb3a05fSNate Lawson printf("Powering system off using ACPI\n"); 1257eeb3a05fSNate Lawson smp_rendezvous(NULL, acpi_shutdown_poweroff, NULL, NULL); 1258eeb3a05fSNate Lawson } else { 1259eeb3a05fSNate Lawson printf("Shutting down ACPI\n"); 1260eeb3a05fSNate Lawson AcpiTerminate(); 1261eeb3a05fSNate Lawson } 1262eeb3a05fSNate Lawson } 1263eeb3a05fSNate Lawson 1264eeb3a05fSNate Lawson static void 1265eeb3a05fSNate Lawson acpi_shutdown_poweroff(void *arg) 1266eeb3a05fSNate Lawson { 126715e32d5dSMike Smith ACPI_STATUS status; 126815e32d5dSMike Smith 1269cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1270cb9b0d80SMike Smith 1271eeb3a05fSNate Lawson /* Only attempt to power off if this is the BSP (cpuid 0). */ 1272eeb3a05fSNate Lawson if (PCPU_GET(cpuid) != 0) 1273eeb3a05fSNate Lawson return; 1274eeb3a05fSNate Lawson 1275be2b1797SNate Lawson status = AcpiEnterSleepStatePrep(acpi_off_state); 1276be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1277b9c780d6SMitsuru IWASAKI printf("AcpiEnterSleepStatePrep failed - %s\n", 1278b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 1279b9c780d6SMitsuru IWASAKI return; 1280b9c780d6SMitsuru IWASAKI } 128155398047SNate Lawson ACPI_DISABLE_IRQS(); 1282be2b1797SNate Lawson status = AcpiEnterSleepState(acpi_off_state); 1283be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1284bfae45aaSMike Smith printf("ACPI power-off failed - %s\n", AcpiFormatException(status)); 128515e32d5dSMike Smith } else { 128615e32d5dSMike Smith DELAY(1000000); 128715e32d5dSMike Smith printf("ACPI power-off failed - timeout\n"); 128815e32d5dSMike Smith } 128915e32d5dSMike Smith } 129015e32d5dSMike Smith 129113d4f7baSMitsuru IWASAKI static void 129213d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc) 129313d4f7baSMitsuru IWASAKI { 129413d4f7baSMitsuru IWASAKI static int first_time = 1; 129513d4f7baSMitsuru IWASAKI 1296cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1297cb9b0d80SMike Smith 129813d4f7baSMitsuru IWASAKI /* Enable and clear fixed events and install handlers. */ 1299be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) { 130059ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 130113d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 130233febf93SNate Lawson acpi_event_power_button_sleep, sc); 1303be2b1797SNate Lawson if (first_time) 13046c0e8467SNate Lawson device_printf(sc->acpi_dev, "Power Button (fixed)\n"); 130513d4f7baSMitsuru IWASAKI } 1306be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) { 130759ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 130813d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 130933febf93SNate Lawson acpi_event_sleep_button_sleep, sc); 1310be2b1797SNate Lawson if (first_time) 13116c0e8467SNate Lawson device_printf(sc->acpi_dev, "Sleep Button (fixed)\n"); 131213d4f7baSMitsuru IWASAKI } 131313d4f7baSMitsuru IWASAKI 131413d4f7baSMitsuru IWASAKI first_time = 0; 131513d4f7baSMitsuru IWASAKI } 131613d4f7baSMitsuru IWASAKI 131715e32d5dSMike Smith /* 1318c5ba0be4SMike Smith * Returns true if the device is actually present and should 1319c5ba0be4SMike Smith * be attached to. This requires the present, enabled, UI-visible 1320c5ba0be4SMike Smith * and diagnostics-passed bits to be set. 1321c5ba0be4SMike Smith */ 1322c5ba0be4SMike Smith BOOLEAN 1323c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev) 1324c5ba0be4SMike Smith { 13251e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1326c5ba0be4SMike Smith ACPI_HANDLE h; 13271e4925e8SNate Lawson ACPI_BUFFER buf; 1328c5ba0be4SMike Smith ACPI_STATUS error; 13291e4925e8SNate Lawson int ret; 1330c5ba0be4SMike Smith 1331cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1332cb9b0d80SMike Smith 13331e4925e8SNate Lawson ret = FALSE; 1334c5ba0be4SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1335c5ba0be4SMike Smith return (FALSE); 13361e4925e8SNate Lawson buf.Pointer = NULL; 13371e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 13386fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 13396fca9360SNate Lawson if (ACPI_FAILURE(error)) 1340c5ba0be4SMike Smith return (FALSE); 13411e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1342be2b1797SNate Lawson 1343be2b1797SNate Lawson /* If no _STA method, must be present */ 13441e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_STA) == 0) 13451e4925e8SNate Lawson ret = TRUE; 1346be2b1797SNate Lawson 1347be2b1797SNate Lawson /* Return true for 'present' and 'functioning' */ 13481e4925e8SNate Lawson if ((devinfo->CurrentStatus & 0x9) == 0x9) 13491e4925e8SNate Lawson ret = TRUE; 1350be2b1797SNate Lawson 13511e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 13521e4925e8SNate Lawson return (ret); 1353c5ba0be4SMike Smith } 1354c5ba0be4SMike Smith 1355c5ba0be4SMike Smith /* 1356b53f2771SMike Smith * Returns true if the battery is actually present and inserted. 1357b53f2771SMike Smith */ 1358b53f2771SMike Smith BOOLEAN 1359b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev) 1360b53f2771SMike Smith { 13611e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1362b53f2771SMike Smith ACPI_HANDLE h; 13631e4925e8SNate Lawson ACPI_BUFFER buf; 1364b53f2771SMike Smith ACPI_STATUS error; 13651e4925e8SNate Lawson int ret; 1366b53f2771SMike Smith 1367b53f2771SMike Smith ACPI_ASSERTLOCK; 1368b53f2771SMike Smith 13691e4925e8SNate Lawson ret = FALSE; 1370b53f2771SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1371b53f2771SMike Smith return (FALSE); 13721e4925e8SNate Lawson buf.Pointer = NULL; 13731e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 13746fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 13756fca9360SNate Lawson if (ACPI_FAILURE(error)) 1376b53f2771SMike Smith return (FALSE); 13771e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1378be2b1797SNate Lawson 1379be2b1797SNate Lawson /* If no _STA method, must be present */ 13801e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_STA) == 0) 13811e4925e8SNate Lawson ret = TRUE; 1382be2b1797SNate Lawson 1383be2b1797SNate Lawson /* Return true for 'present' and 'functioning' */ 13841e4925e8SNate Lawson if ((devinfo->CurrentStatus & 0x19) == 0x19) 13851e4925e8SNate Lawson ret = TRUE; 1386be2b1797SNate Lawson 13871e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 13881e4925e8SNate Lawson return (ret); 1389b53f2771SMike Smith } 1390b53f2771SMike Smith 1391b53f2771SMike Smith /* 139215e32d5dSMike Smith * Match a HID string against a device 139315e32d5dSMike Smith */ 139415e32d5dSMike Smith BOOLEAN 139515e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid) 139615e32d5dSMike Smith { 13971e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 139815e32d5dSMike Smith ACPI_HANDLE h; 13991e4925e8SNate Lawson ACPI_BUFFER buf; 140015e32d5dSMike Smith ACPI_STATUS error; 14011e4925e8SNate Lawson int ret, i; 140215e32d5dSMike Smith 1403cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1404cb9b0d80SMike Smith 14051e4925e8SNate Lawson ret = FALSE; 14064d332989SMike Smith if (hid == NULL) 140715e32d5dSMike Smith return (FALSE); 140815e32d5dSMike Smith if ((h = acpi_get_handle(dev)) == NULL) 140915e32d5dSMike Smith return (FALSE); 14101e4925e8SNate Lawson buf.Pointer = NULL; 14111e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 14126fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 14136fca9360SNate Lawson if (ACPI_FAILURE(error)) 141415e32d5dSMike Smith return (FALSE); 14151e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1416be2b1797SNate Lawson 1417c59c9a8eSJohn Baldwin if ((devinfo->Valid & ACPI_VALID_HID) != 0 && 1418c59c9a8eSJohn Baldwin strcmp(hid, devinfo->HardwareId.Value) == 0) 14191e4925e8SNate Lawson ret = TRUE; 1420c59c9a8eSJohn Baldwin else if ((devinfo->Valid & ACPI_VALID_CID) != 0) { 14211e4925e8SNate Lawson for (i = 0; i < devinfo->CompatibilityId.Count; i++) { 14221e4925e8SNate Lawson if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) { 14231e4925e8SNate Lawson ret = TRUE; 14241e4925e8SNate Lawson break; 14251e4925e8SNate Lawson } 14261e4925e8SNate Lawson } 14271e4925e8SNate Lawson } 1428be2b1797SNate Lawson 14291e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 14301e4925e8SNate Lawson return (ret); 143115e32d5dSMike Smith } 143215e32d5dSMike Smith 143315e32d5dSMike Smith /* 1434c5ba0be4SMike Smith * Return the handle of a named object within our scope, ie. that of (parent) 1435c5ba0be4SMike Smith * or one if its parents. 1436c5ba0be4SMike Smith */ 1437c5ba0be4SMike Smith ACPI_STATUS 1438c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 1439c5ba0be4SMike Smith { 1440c5ba0be4SMike Smith ACPI_HANDLE r; 1441c5ba0be4SMike Smith ACPI_STATUS status; 1442c5ba0be4SMike Smith 1443cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1444cb9b0d80SMike Smith 1445be2b1797SNate Lawson /* Walk back up the tree to the root */ 1446c5ba0be4SMike Smith for (;;) { 1447be2b1797SNate Lawson status = AcpiGetHandle(parent, path, &r); 1448be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1449c5ba0be4SMike Smith *result = r; 1450c5ba0be4SMike Smith return (AE_OK); 1451c5ba0be4SMike Smith } 1452c5ba0be4SMike Smith if (status != AE_NOT_FOUND) 1453c5ba0be4SMike Smith return (AE_OK); 1454b53f2771SMike Smith if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 1455c5ba0be4SMike Smith return (AE_NOT_FOUND); 1456c5ba0be4SMike Smith parent = r; 1457c5ba0be4SMike Smith } 1458c5ba0be4SMike Smith } 1459c5ba0be4SMike Smith 1460c5ba0be4SMike Smith /* 1461c5ba0be4SMike Smith * Allocate a buffer with a preset data size. 1462c5ba0be4SMike Smith */ 1463c5ba0be4SMike Smith ACPI_BUFFER * 1464c5ba0be4SMike Smith acpi_AllocBuffer(int size) 1465c5ba0be4SMike Smith { 1466c5ba0be4SMike Smith ACPI_BUFFER *buf; 1467c5ba0be4SMike Smith 1468c5ba0be4SMike Smith if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 1469c5ba0be4SMike Smith return (NULL); 1470c5ba0be4SMike Smith buf->Length = size; 1471c5ba0be4SMike Smith buf->Pointer = (void *)(buf + 1); 1472c5ba0be4SMike Smith return (buf); 1473c5ba0be4SMike Smith } 1474c5ba0be4SMike Smith 1475c310653eSNate Lawson ACPI_STATUS 1476cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number) 1477c310653eSNate Lawson { 1478c310653eSNate Lawson ACPI_OBJECT arg1; 1479c310653eSNate Lawson ACPI_OBJECT_LIST args; 1480c310653eSNate Lawson 1481c310653eSNate Lawson ACPI_ASSERTLOCK; 1482c310653eSNate Lawson 1483c310653eSNate Lawson arg1.Type = ACPI_TYPE_INTEGER; 1484c310653eSNate Lawson arg1.Integer.Value = number; 1485c310653eSNate Lawson args.Count = 1; 1486c310653eSNate Lawson args.Pointer = &arg1; 1487c310653eSNate Lawson 1488c310653eSNate Lawson return (AcpiEvaluateObject(handle, path, &args, NULL)); 1489c310653eSNate Lawson } 1490c310653eSNate Lawson 1491c5ba0be4SMike Smith /* 1492c5ba0be4SMike Smith * Evaluate a path that should return an integer. 1493c5ba0be4SMike Smith */ 1494c5ba0be4SMike Smith ACPI_STATUS 1495cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number) 1496c5ba0be4SMike Smith { 1497be2b1797SNate Lawson ACPI_STATUS status; 1498c5ba0be4SMike Smith ACPI_BUFFER buf; 1499c573e654SMitsuru IWASAKI ACPI_OBJECT param; 1500c5ba0be4SMike Smith 1501cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1502cb9b0d80SMike Smith 1503c5ba0be4SMike Smith if (handle == NULL) 1504c5ba0be4SMike Smith handle = ACPI_ROOT_OBJECT; 15050c7da7acSJohn Baldwin 15060c7da7acSJohn Baldwin /* 15070c7da7acSJohn Baldwin * Assume that what we've been pointed at is an Integer object, or 15080c7da7acSJohn Baldwin * a method that will return an Integer. 15090c7da7acSJohn Baldwin */ 1510c5ba0be4SMike Smith buf.Pointer = ¶m; 1511c5ba0be4SMike Smith buf.Length = sizeof(param); 1512be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1513be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1514be2b1797SNate Lawson if (param.Type == ACPI_TYPE_INTEGER) 1515c5ba0be4SMike Smith *number = param.Integer.Value; 1516be2b1797SNate Lawson else 1517be2b1797SNate Lawson status = AE_TYPE; 1518c5ba0be4SMike Smith } 15190c7da7acSJohn Baldwin 15200c7da7acSJohn Baldwin /* 15210c7da7acSJohn Baldwin * In some applications, a method that's expected to return an Integer 15220c7da7acSJohn Baldwin * may instead return a Buffer (probably to simplify some internal 15230c7da7acSJohn Baldwin * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 15240c7da7acSJohn Baldwin * convert it into an Integer as best we can. 15250c7da7acSJohn Baldwin * 15260c7da7acSJohn Baldwin * This is a hack. 15270c7da7acSJohn Baldwin */ 1528be2b1797SNate Lawson if (status == AE_BUFFER_OVERFLOW) { 1529b53f2771SMike Smith if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 1530be2b1797SNate Lawson status = AE_NO_MEMORY; 15310c7da7acSJohn Baldwin } else { 1532be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1533be2b1797SNate Lawson if (ACPI_SUCCESS(status)) 1534be2b1797SNate Lawson status = acpi_ConvertBufferToInteger(&buf, number); 15350c7da7acSJohn Baldwin AcpiOsFree(buf.Pointer); 15360c7da7acSJohn Baldwin } 1537f97739daSNate Lawson } 1538be2b1797SNate Lawson return (status); 1539c5ba0be4SMike Smith } 1540c5ba0be4SMike Smith 1541c573e654SMitsuru IWASAKI ACPI_STATUS 1542cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number) 1543c573e654SMitsuru IWASAKI { 1544c573e654SMitsuru IWASAKI ACPI_OBJECT *p; 1545dba55fa2SNate Lawson UINT8 *val; 1546c573e654SMitsuru IWASAKI int i; 1547c573e654SMitsuru IWASAKI 1548c573e654SMitsuru IWASAKI p = (ACPI_OBJECT *)bufp->Pointer; 1549c573e654SMitsuru IWASAKI if (p->Type == ACPI_TYPE_INTEGER) { 1550c573e654SMitsuru IWASAKI *number = p->Integer.Value; 1551c573e654SMitsuru IWASAKI return (AE_OK); 1552c573e654SMitsuru IWASAKI } 1553c573e654SMitsuru IWASAKI if (p->Type != ACPI_TYPE_BUFFER) 1554c573e654SMitsuru IWASAKI return (AE_TYPE); 1555c573e654SMitsuru IWASAKI if (p->Buffer.Length > sizeof(int)) 1556c573e654SMitsuru IWASAKI return (AE_BAD_DATA); 1557be2b1797SNate Lawson 1558c573e654SMitsuru IWASAKI *number = 0; 1559dba55fa2SNate Lawson val = p->Buffer.Pointer; 1560c573e654SMitsuru IWASAKI for (i = 0; i < p->Buffer.Length; i++) 1561dba55fa2SNate Lawson *number += val[i] << (i * 8); 1562c573e654SMitsuru IWASAKI return (AE_OK); 1563c573e654SMitsuru IWASAKI } 1564c573e654SMitsuru IWASAKI 1565c5ba0be4SMike Smith /* 1566c5ba0be4SMike Smith * Iterate over the elements of an a package object, calling the supplied 1567c5ba0be4SMike Smith * function for each element. 1568c5ba0be4SMike Smith * 1569c5ba0be4SMike Smith * XXX possible enhancement might be to abort traversal on error. 1570c5ba0be4SMike Smith */ 1571c5ba0be4SMike Smith ACPI_STATUS 1572be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg, 1573be2b1797SNate Lawson void (*func)(ACPI_OBJECT *comp, void *arg), void *arg) 1574c5ba0be4SMike Smith { 1575c5ba0be4SMike Smith ACPI_OBJECT *comp; 1576c5ba0be4SMike Smith int i; 1577c5ba0be4SMike Smith 1578be2b1797SNate Lawson if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE) 1579c5ba0be4SMike Smith return (AE_BAD_PARAMETER); 1580c5ba0be4SMike Smith 1581be2b1797SNate Lawson /* Iterate over components */ 1582be2b1797SNate Lawson i = 0; 1583be2b1797SNate Lawson comp = pkg->Package.Elements; 1584be2b1797SNate Lawson for (; i < pkg->Package.Count; i++, comp++) 1585c5ba0be4SMike Smith func(comp, arg); 1586c5ba0be4SMike Smith 1587c5ba0be4SMike Smith return (AE_OK); 1588c5ba0be4SMike Smith } 1589c5ba0be4SMike Smith 15906f69255bSMike Smith /* 15916f69255bSMike Smith * Find the (index)th resource object in a set. 15926f69255bSMike Smith */ 15936f69255bSMike Smith ACPI_STATUS 15946d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 15956f69255bSMike Smith { 15966d63101aSMike Smith ACPI_RESOURCE *rp; 15976f69255bSMike Smith int i; 15986f69255bSMike Smith 15996d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 16006f69255bSMike Smith i = index; 16016d63101aSMike Smith while (i-- > 0) { 1602be2b1797SNate Lawson /* Range check */ 16036d63101aSMike Smith if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 16046f69255bSMike Smith return (AE_BAD_PARAMETER); 1605be2b1797SNate Lawson 1606be2b1797SNate Lawson /* Check for terminator */ 1607be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 16086d63101aSMike Smith return (AE_NOT_FOUND); 1609abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 16106f69255bSMike Smith } 16116f69255bSMike Smith if (resp != NULL) 16126d63101aSMike Smith *resp = rp; 1613be2b1797SNate Lawson 16146d63101aSMike Smith return (AE_OK); 16156d63101aSMike Smith } 16166d63101aSMike Smith 16176d63101aSMike Smith /* 16186d63101aSMike Smith * Append an ACPI_RESOURCE to an ACPI_BUFFER. 16196d63101aSMike Smith * 16206d63101aSMike Smith * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 16216d63101aSMike Smith * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 16226d63101aSMike Smith * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 16236d63101aSMike Smith * resources. 16246d63101aSMike Smith */ 16256d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 16266d63101aSMike Smith 16276d63101aSMike Smith ACPI_STATUS 16286d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 16296d63101aSMike Smith { 16306d63101aSMike Smith ACPI_RESOURCE *rp; 16316d63101aSMike Smith void *newp; 16326d63101aSMike Smith 1633be2b1797SNate Lawson /* Initialise the buffer if necessary. */ 16346d63101aSMike Smith if (buf->Pointer == NULL) { 16356d63101aSMike Smith buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 16366d63101aSMike Smith if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 16376d63101aSMike Smith return (AE_NO_MEMORY); 16386d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 16396d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 16406d63101aSMike Smith rp->Length = 0; 16416d63101aSMike Smith } 16426d63101aSMike Smith if (res == NULL) 16436d63101aSMike Smith return (AE_OK); 16446d63101aSMike Smith 16456d63101aSMike Smith /* 16466d63101aSMike Smith * Scan the current buffer looking for the terminator. 16476d63101aSMike Smith * This will either find the terminator or hit the end 16486d63101aSMike Smith * of the buffer and return an error. 16496d63101aSMike Smith */ 16506d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 16516d63101aSMike Smith for (;;) { 1652be2b1797SNate Lawson /* Range check, don't go outside the buffer */ 16536d63101aSMike Smith if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 16546d63101aSMike Smith return (AE_BAD_PARAMETER); 1655be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 16566d63101aSMike Smith break; 1657abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 16586d63101aSMike Smith } 16596d63101aSMike Smith 16606d63101aSMike Smith /* 16616d63101aSMike Smith * Check the size of the buffer and expand if required. 16626d63101aSMike Smith * 16636d63101aSMike Smith * Required size is: 16646d63101aSMike Smith * size of existing resources before terminator + 16656d63101aSMike Smith * size of new resource and header + 16666d63101aSMike Smith * size of terminator. 16676d63101aSMike Smith * 16686d63101aSMike Smith * Note that this loop should really only run once, unless 16696d63101aSMike Smith * for some reason we are stuffing a *really* huge resource. 16706d63101aSMike Smith */ 16716d63101aSMike Smith while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 16726d63101aSMike Smith res->Length + ACPI_RESOURCE_LENGTH_NO_DATA + 16736d63101aSMike Smith ACPI_RESOURCE_LENGTH) >= buf->Length) { 16746d63101aSMike Smith if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 16756d63101aSMike Smith return (AE_NO_MEMORY); 16766d63101aSMike Smith bcopy(buf->Pointer, newp, buf->Length); 1677a692219dSMike Smith rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 1678a692219dSMike Smith ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 16796d63101aSMike Smith AcpiOsFree(buf->Pointer); 16806d63101aSMike Smith buf->Pointer = newp; 16816d63101aSMike Smith buf->Length += buf->Length; 16826d63101aSMike Smith } 16836d63101aSMike Smith 1684be2b1797SNate Lawson /* Insert the new resource. */ 16856d63101aSMike Smith bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA); 16866d63101aSMike Smith 1687be2b1797SNate Lawson /* And add the terminator. */ 1688abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 16896d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 16906d63101aSMike Smith rp->Length = 0; 16916d63101aSMike Smith 16926f69255bSMike Smith return (AE_OK); 16936f69255bSMike Smith } 1694c5ba0be4SMike Smith 1695da14ac9fSJohn Baldwin /* 1696da14ac9fSJohn Baldwin * Set interrupt model. 1697da14ac9fSJohn Baldwin */ 1698da14ac9fSJohn Baldwin ACPI_STATUS 1699da14ac9fSJohn Baldwin acpi_SetIntrModel(int model) 1700da14ac9fSJohn Baldwin { 1701da14ac9fSJohn Baldwin 1702c310653eSNate Lawson return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model)); 1703da14ac9fSJohn Baldwin } 1704da14ac9fSJohn Baldwin 1705ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME 5 1706ece50487SMitsuru IWASAKI 1707ece50487SMitsuru IWASAKI static void 1708ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg) 1709ece50487SMitsuru IWASAKI { 1710ece50487SMitsuru IWASAKI ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0; 1711ece50487SMitsuru IWASAKI } 1712c30382dfSMitsuru IWASAKI 171315e32d5dSMike Smith /* 171415e32d5dSMike Smith * Set the system sleep state 171515e32d5dSMike Smith * 1716be2b1797SNate Lawson * Currently we support S1-S5 but S4 is only S4BIOS 171715e32d5dSMike Smith */ 171815e32d5dSMike Smith ACPI_STATUS 171915e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state) 172015e32d5dSMike Smith { 172115e32d5dSMike Smith ACPI_STATUS status = AE_OK; 172256d8cb57SMitsuru IWASAKI UINT8 TypeA; 172356d8cb57SMitsuru IWASAKI UINT8 TypeB; 172415e32d5dSMike Smith 1725b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 1726cb9b0d80SMike Smith ACPI_ASSERTLOCK; 17270ae55423SMike Smith 172898175614SNate Lawson /* Avoid reentry if already attempting to suspend. */ 17296397624dSMitsuru IWASAKI if (sc->acpi_sstate != ACPI_STATE_S0) 173098175614SNate Lawson return_ACPI_STATUS (AE_BAD_PARAMETER); 17316397624dSMitsuru IWASAKI 173298175614SNate Lawson /* We recently woke up so don't suspend again for a while. */ 1733ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1734ece50487SMitsuru IWASAKI return_ACPI_STATUS (AE_OK); 1735ece50487SMitsuru IWASAKI 173615e32d5dSMike Smith switch (state) { 173715e32d5dSMike Smith case ACPI_STATE_S1: 17386161544cSTakanori Watanabe case ACPI_STATE_S2: 17396161544cSTakanori Watanabe case ACPI_STATE_S3: 17406161544cSTakanori Watanabe case ACPI_STATE_S4: 1741be2b1797SNate Lawson status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB); 174298175614SNate Lawson if (status == AE_NOT_FOUND) { 174398175614SNate Lawson device_printf(sc->acpi_dev, 174498175614SNate Lawson "Sleep state S%d not supported by BIOS\n", state); 174598175614SNate Lawson break; 174698175614SNate Lawson } else if (ACPI_FAILURE(status)) { 1747be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", 1748be2b1797SNate Lawson AcpiFormatException(status)); 174956d8cb57SMitsuru IWASAKI break; 175056d8cb57SMitsuru IWASAKI } 175156d8cb57SMitsuru IWASAKI 1752a1fccb47SMitsuru IWASAKI sc->acpi_sstate = state; 1753a1fccb47SMitsuru IWASAKI sc->acpi_sleep_disabled = 1; 1754a1fccb47SMitsuru IWASAKI 1755be2b1797SNate Lawson /* Inform all devices that we are going to sleep. */ 175615e32d5dSMike Smith if (DEVICE_SUSPEND(root_bus) != 0) { 175715e32d5dSMike Smith /* 175815e32d5dSMike Smith * Re-wake the system. 175915e32d5dSMike Smith * 176015e32d5dSMike Smith * XXX note that a better two-pass approach with a 'veto' pass 176115e32d5dSMike Smith * followed by a "real thing" pass would be better, but the 176215e32d5dSMike Smith * current bus interface does not provide for this. 176315e32d5dSMike Smith */ 176415e32d5dSMike Smith DEVICE_RESUME(root_bus); 17650ae55423SMike Smith return_ACPI_STATUS (AE_ERROR); 176615e32d5dSMike Smith } 1767b9c780d6SMitsuru IWASAKI 1768be2b1797SNate Lawson status = AcpiEnterSleepStatePrep(state); 1769be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1770b9c780d6SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 1771b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 1772b9c780d6SMitsuru IWASAKI break; 1773b9c780d6SMitsuru IWASAKI } 1774b9c780d6SMitsuru IWASAKI 1775be2b1797SNate Lawson if (sc->acpi_sleep_delay > 0) 1776ff01efb5SMitsuru IWASAKI DELAY(sc->acpi_sleep_delay * 1000000); 1777ff01efb5SMitsuru IWASAKI 17786161544cSTakanori Watanabe if (state != ACPI_STATE_S1) { 17796161544cSTakanori Watanabe acpi_sleep_machdep(sc, state); 17806161544cSTakanori Watanabe 1781be2b1797SNate Lawson /* AcpiEnterSleepState() may be incomplete, unlock if locked. */ 178259ddeb18SNate Lawson if (AcpiGbl_MutexInfo[ACPI_MTX_HARDWARE].OwnerId != 178359ddeb18SNate Lawson ACPI_MUTEX_NOT_ACQUIRED) { 178459ddeb18SNate Lawson 17856161544cSTakanori Watanabe AcpiUtReleaseMutex(ACPI_MTX_HARDWARE); 1786a1fccb47SMitsuru IWASAKI } 17876161544cSTakanori Watanabe 17886161544cSTakanori Watanabe /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 1789be2b1797SNate Lawson if (state == ACPI_STATE_S4) 1790964679ceSMitsuru IWASAKI AcpiEnable(); 17916161544cSTakanori Watanabe } else { 1792be2b1797SNate Lawson status = AcpiEnterSleepState((UINT8)state); 1793be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1794be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 1795be2b1797SNate Lawson AcpiFormatException(status)); 1796c30382dfSMitsuru IWASAKI break; 179715e32d5dSMike Smith } 17986161544cSTakanori Watanabe } 1799ff741befSTakanori Watanabe AcpiLeaveSleepState((UINT8)state); 180015e32d5dSMike Smith DEVICE_RESUME(root_bus); 180115e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 180213d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 180315e32d5dSMike Smith break; 180415e32d5dSMike Smith case ACPI_STATE_S5: 180515e32d5dSMike Smith /* 180615e32d5dSMike Smith * Shut down cleanly and power off. This will call us back through the 180715e32d5dSMike Smith * shutdown handlers. 180815e32d5dSMike Smith */ 180915e32d5dSMike Smith shutdown_nice(RB_POWEROFF); 181015e32d5dSMike Smith break; 181198175614SNate Lawson case ACPI_STATE_S0: 181215e32d5dSMike Smith default: 181315e32d5dSMike Smith status = AE_BAD_PARAMETER; 181415e32d5dSMike Smith break; 181515e32d5dSMike Smith } 1816ece50487SMitsuru IWASAKI 181798175614SNate Lawson /* Disable a second sleep request for a short period */ 1818ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1819ece50487SMitsuru IWASAKI timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME); 1820ece50487SMitsuru IWASAKI 18210ae55423SMike Smith return_ACPI_STATUS (status); 182215e32d5dSMike Smith } 182315e32d5dSMike Smith 182415e32d5dSMike Smith /* 182515e32d5dSMike Smith * Enable/Disable ACPI 182615e32d5dSMike Smith */ 182715e32d5dSMike Smith ACPI_STATUS 182815e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc) 182915e32d5dSMike Smith { 183015e32d5dSMike Smith ACPI_STATUS status; 183115e32d5dSMike Smith u_int32_t flags; 183215e32d5dSMike Smith 1833b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1834cb9b0d80SMike Smith ACPI_ASSERTLOCK; 18350ae55423SMike Smith 183615e32d5dSMike Smith flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT | 183715e32d5dSMike Smith ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 1838be2b1797SNate Lawson if (!sc->acpi_enabled) 183915e32d5dSMike Smith status = AcpiEnableSubsystem(flags); 1840be2b1797SNate Lawson else 184115e32d5dSMike Smith status = AE_OK; 1842be2b1797SNate Lawson 184315e32d5dSMike Smith if (status == AE_OK) 184415e32d5dSMike Smith sc->acpi_enabled = 1; 1845be2b1797SNate Lawson 18460ae55423SMike Smith return_ACPI_STATUS (status); 184715e32d5dSMike Smith } 184815e32d5dSMike Smith 184915e32d5dSMike Smith ACPI_STATUS 185015e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc) 185115e32d5dSMike Smith { 185215e32d5dSMike Smith ACPI_STATUS status; 185315e32d5dSMike Smith 1854b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1855cb9b0d80SMike Smith ACPI_ASSERTLOCK; 18560ae55423SMike Smith 1857be2b1797SNate Lawson if (sc->acpi_enabled) 185815e32d5dSMike Smith status = AcpiDisable(); 1859be2b1797SNate Lawson else 186015e32d5dSMike Smith status = AE_OK; 1861be2b1797SNate Lawson 186215e32d5dSMike Smith if (status == AE_OK) 186315e32d5dSMike Smith sc->acpi_enabled = 0; 1864be2b1797SNate Lawson 18650ae55423SMike Smith return_ACPI_STATUS (status); 186615e32d5dSMike Smith } 186715e32d5dSMike Smith 186815e32d5dSMike Smith /* 186915e32d5dSMike Smith * ACPI Event Handlers 187015e32d5dSMike Smith */ 187115e32d5dSMike Smith 187215e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 187315e32d5dSMike Smith 187415e32d5dSMike Smith static void 187515e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state) 187615e32d5dSMike Smith { 1877fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 1878b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 187915e32d5dSMike Smith 1880cb9b0d80SMike Smith ACPI_LOCK; 1881a5d1879bSMitsuru IWASAKI if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 188215e32d5dSMike Smith acpi_SetSleepState((struct acpi_softc *)arg, state); 1883cb9b0d80SMike Smith ACPI_UNLOCK; 18840ae55423SMike Smith return_VOID; 188515e32d5dSMike Smith } 188615e32d5dSMike Smith 188715e32d5dSMike Smith static void 188815e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state) 188915e32d5dSMike Smith { 1890fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 1891b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 18920ae55423SMike Smith 189315e32d5dSMike Smith /* Well, what to do? :-) */ 18940ae55423SMike Smith 1895cb9b0d80SMike Smith ACPI_LOCK; 1896cb9b0d80SMike Smith ACPI_UNLOCK; 1897cb9b0d80SMike Smith 18980ae55423SMike Smith return_VOID; 189915e32d5dSMike Smith } 190015e32d5dSMike Smith 190115e32d5dSMike Smith /* 190215e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 190315e32d5dSMike Smith */ 190415e32d5dSMike Smith UINT32 190533febf93SNate Lawson acpi_event_power_button_sleep(void *context) 190615e32d5dSMike Smith { 190715e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 190815e32d5dSMike Smith 1909b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 19100ae55423SMike Smith 191115e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 19120ae55423SMike Smith 1913b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 191415e32d5dSMike Smith } 191515e32d5dSMike Smith 191615e32d5dSMike Smith UINT32 191733febf93SNate Lawson acpi_event_power_button_wake(void *context) 191815e32d5dSMike Smith { 191915e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 192015e32d5dSMike Smith 1921b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 19220ae55423SMike Smith 192315e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 19240ae55423SMike Smith 1925b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 192615e32d5dSMike Smith } 192715e32d5dSMike Smith 192815e32d5dSMike Smith UINT32 192933febf93SNate Lawson acpi_event_sleep_button_sleep(void *context) 193015e32d5dSMike Smith { 193115e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 193215e32d5dSMike Smith 1933b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 19340ae55423SMike Smith 193515e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 19360ae55423SMike Smith 1937b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 193815e32d5dSMike Smith } 193915e32d5dSMike Smith 194015e32d5dSMike Smith UINT32 194133febf93SNate Lawson acpi_event_sleep_button_wake(void *context) 194215e32d5dSMike Smith { 194315e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 194415e32d5dSMike Smith 1945b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 19460ae55423SMike Smith 194715e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 19480ae55423SMike Smith 1949b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 195015e32d5dSMike Smith } 195115e32d5dSMike Smith 195215e32d5dSMike Smith /* 195315e32d5dSMike Smith * XXX This is kinda ugly, and should not be here. 195415e32d5dSMike Smith */ 195515e32d5dSMike Smith struct acpi_staticbuf { 195615e32d5dSMike Smith ACPI_BUFFER buffer; 195715e32d5dSMike Smith char data[512]; 195815e32d5dSMike Smith }; 195915e32d5dSMike Smith 196015e32d5dSMike Smith char * 196115e32d5dSMike Smith acpi_name(ACPI_HANDLE handle) 196215e32d5dSMike Smith { 196315e32d5dSMike Smith static struct acpi_staticbuf buf; 196415e32d5dSMike Smith 1965cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1966cb9b0d80SMike Smith 196715e32d5dSMike Smith buf.buffer.Length = 512; 196815e32d5dSMike Smith buf.buffer.Pointer = &buf.data[0]; 196915e32d5dSMike Smith 1970b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer))) 197115e32d5dSMike Smith return (buf.buffer.Pointer); 1972be2b1797SNate Lawson 197315e32d5dSMike Smith return ("(unknown path)"); 197415e32d5dSMike Smith } 197515e32d5dSMike Smith 197615e32d5dSMike Smith /* 197715e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various 197815e32d5dSMike Smith * parts of the namespace. 197915e32d5dSMike Smith */ 198015e32d5dSMike Smith int 198115e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle) 198215e32d5dSMike Smith { 19833c600cfbSJohn Baldwin char *cp, *env, *np; 198415e32d5dSMike Smith int len; 198515e32d5dSMike Smith 198615e32d5dSMike Smith np = acpi_name(handle); 198715e32d5dSMike Smith if (*np == '\\') 198815e32d5dSMike Smith np++; 19893c600cfbSJohn Baldwin if ((env = getenv("debug.acpi.avoid")) == NULL) 199015e32d5dSMike Smith return (0); 199115e32d5dSMike Smith 1992be2b1797SNate Lawson /* Scan the avoid list checking for a match */ 19933c600cfbSJohn Baldwin cp = env; 199415e32d5dSMike Smith for (;;) { 199515e32d5dSMike Smith while ((*cp != 0) && isspace(*cp)) 199615e32d5dSMike Smith cp++; 199715e32d5dSMike Smith if (*cp == 0) 199815e32d5dSMike Smith break; 199915e32d5dSMike Smith len = 0; 200015e32d5dSMike Smith while ((cp[len] != 0) && !isspace(cp[len])) 200115e32d5dSMike Smith len++; 2002d786139cSMaxime Henrion if (!strncmp(cp, np, len)) { 20033c600cfbSJohn Baldwin freeenv(env); 20040ae55423SMike Smith return(1); 2005d786139cSMaxime Henrion } 20060ae55423SMike Smith cp += len; 20070ae55423SMike Smith } 20083c600cfbSJohn Baldwin freeenv(env); 2009be2b1797SNate Lawson 20100ae55423SMike Smith return (0); 20110ae55423SMike Smith } 20120ae55423SMike Smith 20130ae55423SMike Smith /* 20140ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components. 20150ae55423SMike Smith */ 20160ae55423SMike Smith int 20170ae55423SMike Smith acpi_disabled(char *subsys) 20180ae55423SMike Smith { 201978689b15SMaxime Henrion char *cp, *env; 20200ae55423SMike Smith int len; 20210ae55423SMike Smith 20223184cf5aSNate Lawson if ((env = getenv("debug.acpi.disabled")) == NULL) 20230ae55423SMike Smith return (0); 20243184cf5aSNate Lawson if (strcmp(env, "all") == 0) { 202578689b15SMaxime Henrion freeenv(env); 20260ae55423SMike Smith return (1); 2027d786139cSMaxime Henrion } 20280ae55423SMike Smith 20293184cf5aSNate Lawson /* Scan the disable list, checking for a match. */ 203078689b15SMaxime Henrion cp = env; 20310ae55423SMike Smith for (;;) { 20323184cf5aSNate Lawson while (*cp != '\0' && isspace(*cp)) 20330ae55423SMike Smith cp++; 20343184cf5aSNate Lawson if (*cp == '\0') 20350ae55423SMike Smith break; 20360ae55423SMike Smith len = 0; 20373184cf5aSNate Lawson while (cp[len] != '\0' && !isspace(cp[len])) 20380ae55423SMike Smith len++; 20393184cf5aSNate Lawson if (strncmp(cp, subsys, len) == 0) { 204078689b15SMaxime Henrion freeenv(env); 204115e32d5dSMike Smith return (1); 2042d786139cSMaxime Henrion } 204315e32d5dSMike Smith cp += len; 204415e32d5dSMike Smith } 204578689b15SMaxime Henrion freeenv(env); 2046be2b1797SNate Lawson 204715e32d5dSMike Smith return (0); 204815e32d5dSMike Smith } 204915e32d5dSMike Smith 205015e32d5dSMike Smith /* 2051a1fccb47SMitsuru IWASAKI * Device wake capability enable/disable. 2052a1fccb47SMitsuru IWASAKI */ 2053a1fccb47SMitsuru IWASAKI void 2054a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable) 2055a1fccb47SMitsuru IWASAKI { 2056a1fccb47SMitsuru IWASAKI /* 2057a1fccb47SMitsuru IWASAKI * TBD: All Power Resources referenced by elements 2 through N 2058a1fccb47SMitsuru IWASAKI * of the _PRW object are put into the ON state. 2059a1fccb47SMitsuru IWASAKI */ 2060a1fccb47SMitsuru IWASAKI 2061c310653eSNate Lawson (void)acpi_SetInteger(h, "_PSW", enable); 2062a1fccb47SMitsuru IWASAKI } 2063a1fccb47SMitsuru IWASAKI 2064a1fccb47SMitsuru IWASAKI void 2065a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_event(ACPI_HANDLE h) 2066a1fccb47SMitsuru IWASAKI { 2067a1fccb47SMitsuru IWASAKI struct acpi_softc *sc; 2068a1fccb47SMitsuru IWASAKI ACPI_STATUS status; 2069a1fccb47SMitsuru IWASAKI ACPI_BUFFER prw_buffer; 2070a1fccb47SMitsuru IWASAKI ACPI_OBJECT *res; 2071a1fccb47SMitsuru IWASAKI 2072a1fccb47SMitsuru IWASAKI ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2073a1fccb47SMitsuru IWASAKI 2074be2b1797SNate Lawson sc = devclass_get_softc(acpi_devclass, 0); 2075be2b1797SNate Lawson if (sc == NULL) 2076a1fccb47SMitsuru IWASAKI return; 2077a1fccb47SMitsuru IWASAKI 2078a1fccb47SMitsuru IWASAKI /* 2079a1fccb47SMitsuru IWASAKI * _PRW object is only required for devices that have the ability 2080a1fccb47SMitsuru IWASAKI * to wake the system from a system sleeping state. 2081a1fccb47SMitsuru IWASAKI */ 2082a1fccb47SMitsuru IWASAKI prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 2083a1fccb47SMitsuru IWASAKI status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 2084be2b1797SNate Lawson if (ACPI_FAILURE(status)) 2085a1fccb47SMitsuru IWASAKI return; 2086a1fccb47SMitsuru IWASAKI 2087a1fccb47SMitsuru IWASAKI res = (ACPI_OBJECT *)prw_buffer.Pointer; 2088be2b1797SNate Lawson if (res == NULL) 2089a1fccb47SMitsuru IWASAKI return; 2090a1fccb47SMitsuru IWASAKI 2091a1fccb47SMitsuru IWASAKI if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) { 2092a1fccb47SMitsuru IWASAKI goto out; 2093a1fccb47SMitsuru IWASAKI } 2094a1fccb47SMitsuru IWASAKI 2095a1fccb47SMitsuru IWASAKI /* 2096a1fccb47SMitsuru IWASAKI * The element 1 of the _PRW object: 2097a1fccb47SMitsuru IWASAKI * The lowest power system sleeping state that can be entered 2098a1fccb47SMitsuru IWASAKI * while still providing wake functionality. 2099a1fccb47SMitsuru IWASAKI * The sleeping state being entered must be greater or equal to 2100a1fccb47SMitsuru IWASAKI * the power state declared in element 1 of the _PRW object. 2101a1fccb47SMitsuru IWASAKI */ 2102be2b1797SNate Lawson if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER) 2103a1fccb47SMitsuru IWASAKI goto out; 2104a1fccb47SMitsuru IWASAKI 2105be2b1797SNate Lawson if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value) 2106a1fccb47SMitsuru IWASAKI goto out; 2107a1fccb47SMitsuru IWASAKI 2108a1fccb47SMitsuru IWASAKI /* 2109a1fccb47SMitsuru IWASAKI * The element 0 of the _PRW object: 2110a1fccb47SMitsuru IWASAKI */ 2111a1fccb47SMitsuru IWASAKI switch(res->Package.Elements[0].Type) { 2112a1fccb47SMitsuru IWASAKI case ACPI_TYPE_INTEGER: 2113a1fccb47SMitsuru IWASAKI /* 2114a1fccb47SMitsuru IWASAKI * If the data type of this package element is numeric, then this 2115a1fccb47SMitsuru IWASAKI * _PRW package element is the bit index in the GPEx_EN, in the 2116a1fccb47SMitsuru IWASAKI * GPE blocks described in the FADT, of the enable bit that is 2117a1fccb47SMitsuru IWASAKI * enabled for the wake event. 2118a1fccb47SMitsuru IWASAKI */ 2119a1fccb47SMitsuru IWASAKI 21206fca9360SNate Lawson status = AcpiEnableGpe(NULL, res->Package.Elements[0].Integer.Value, 21216fca9360SNate Lawson ACPI_EVENT_WAKE_ENABLE); 2122a1fccb47SMitsuru IWASAKI if (ACPI_FAILURE(status)) 2123a1fccb47SMitsuru IWASAKI printf("%s: EnableEvent Failed\n", __func__); 2124a1fccb47SMitsuru IWASAKI break; 2125a1fccb47SMitsuru IWASAKI case ACPI_TYPE_PACKAGE: 2126a1fccb47SMitsuru IWASAKI /* 2127be2b1797SNate Lawson * XXX TBD 2128be2b1797SNate Lawson * 2129a1fccb47SMitsuru IWASAKI * If the data type of this package element is a package, then this 2130a1fccb47SMitsuru IWASAKI * _PRW package element is itself a package containing two 2131a1fccb47SMitsuru IWASAKI * elements. The first is an object reference to the GPE Block 2132a1fccb47SMitsuru IWASAKI * device that contains the GPE that will be triggered by the wake 2133a1fccb47SMitsuru IWASAKI * event. The second element is numeric and it contains the bit 2134a1fccb47SMitsuru IWASAKI * index in the GPEx_EN, in the GPE Block referenced by the 2135a1fccb47SMitsuru IWASAKI * first element in the package, of the enable bit that is enabled for 2136a1fccb47SMitsuru IWASAKI * the wake event. 2137a1fccb47SMitsuru IWASAKI * For example, if this field is a package then it is of the form: 2138a1fccb47SMitsuru IWASAKI * Package() {\_SB.PCI0.ISA.GPE, 2} 2139a1fccb47SMitsuru IWASAKI */ 2140a1fccb47SMitsuru IWASAKI break; 2141a1fccb47SMitsuru IWASAKI default: 2142a1fccb47SMitsuru IWASAKI break; 2143a1fccb47SMitsuru IWASAKI } 2144a1fccb47SMitsuru IWASAKI 2145a1fccb47SMitsuru IWASAKI out: 2146a1fccb47SMitsuru IWASAKI if (prw_buffer.Pointer != NULL) 2147a1fccb47SMitsuru IWASAKI AcpiOsFree(prw_buffer.Pointer); 2148a1fccb47SMitsuru IWASAKI } 2149a1fccb47SMitsuru IWASAKI 2150a1fccb47SMitsuru IWASAKI /* 215115e32d5dSMike Smith * Control interface. 215215e32d5dSMike Smith * 21530ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual 2154be2b1797SNate Lawson * drivers wanting to be accessible via /dev/acpi should use the 2155be2b1797SNate Lawson * register/deregister interface to make their handlers visible. 215615e32d5dSMike Smith */ 21570ae55423SMike Smith struct acpi_ioctl_hook 21580ae55423SMike Smith { 21590ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link; 21600ae55423SMike Smith u_long cmd; 2161be2b1797SNate Lawson acpi_ioctl_fn fn; 21620ae55423SMike Smith void *arg; 21630ae55423SMike Smith }; 21640ae55423SMike Smith 21650ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 21660ae55423SMike Smith static int acpi_ioctl_hooks_initted; 21670ae55423SMike Smith 21680ae55423SMike Smith /* 21690ae55423SMike Smith * Register an ioctl handler. 21700ae55423SMike Smith */ 21710ae55423SMike Smith int 2172be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 21730ae55423SMike Smith { 21740ae55423SMike Smith struct acpi_ioctl_hook *hp; 21750ae55423SMike Smith 21760ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 21770ae55423SMike Smith return (ENOMEM); 21780ae55423SMike Smith hp->cmd = cmd; 21790ae55423SMike Smith hp->fn = fn; 21800ae55423SMike Smith hp->arg = arg; 21810ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) { 21820ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks); 21830ae55423SMike Smith acpi_ioctl_hooks_initted = 1; 21840ae55423SMike Smith } 21850ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 21860ae55423SMike Smith return (0); 21870ae55423SMike Smith } 21880ae55423SMike Smith 21890ae55423SMike Smith /* 21900ae55423SMike Smith * Deregister an ioctl handler. 21910ae55423SMike Smith */ 21920ae55423SMike Smith void 2193be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 21940ae55423SMike Smith { 21950ae55423SMike Smith struct acpi_ioctl_hook *hp; 21960ae55423SMike Smith 21970ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 21980ae55423SMike Smith if ((hp->cmd == cmd) && (hp->fn == fn)) 21990ae55423SMike Smith break; 22000ae55423SMike Smith 22010ae55423SMike Smith if (hp != NULL) { 22020ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 22030ae55423SMike Smith free(hp, M_ACPIDEV); 22040ae55423SMike Smith } 22050ae55423SMike Smith } 22060ae55423SMike Smith 220715e32d5dSMike Smith static int 22086b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td) 220915e32d5dSMike Smith { 221015e32d5dSMike Smith return (0); 221115e32d5dSMike Smith } 221215e32d5dSMike Smith 221315e32d5dSMike Smith static int 22146b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td) 221515e32d5dSMike Smith { 221615e32d5dSMike Smith return (0); 221715e32d5dSMike Smith } 221815e32d5dSMike Smith 221915e32d5dSMike Smith static int 22206b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 222115e32d5dSMike Smith { 222215e32d5dSMike Smith struct acpi_softc *sc; 22230ae55423SMike Smith struct acpi_ioctl_hook *hp; 22240ae55423SMike Smith int error, xerror, state; 2225fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 222615e32d5dSMike Smith 2227cb9b0d80SMike Smith ACPI_LOCK; 2228cb9b0d80SMike Smith 222915e32d5dSMike Smith error = state = 0; 223015e32d5dSMike Smith sc = dev->si_drv1; 223115e32d5dSMike Smith 22320ae55423SMike Smith /* 22330ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers. 22340ae55423SMike Smith */ 22350ae55423SMike Smith if (acpi_ioctl_hooks_initted) { 22360ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 22370ae55423SMike Smith if (hp->cmd == cmd) { 22380ae55423SMike Smith xerror = hp->fn(cmd, addr, hp->arg); 22390ae55423SMike Smith if (xerror != 0) 22400ae55423SMike Smith error = xerror; 2241917d44c8SMitsuru IWASAKI goto out; 22420ae55423SMike Smith } 22430ae55423SMike Smith } 22440ae55423SMike Smith } 22450ae55423SMike Smith 22460ae55423SMike Smith /* 2247a89fcf28STakanori Watanabe * Core ioctls are not permitted for non-writable user. 2248a89fcf28STakanori Watanabe * Currently, other ioctls just fetch information. 2249a89fcf28STakanori Watanabe * Not changing system behavior. 2250a89fcf28STakanori Watanabe */ 2251be2b1797SNate Lawson if((flag & FWRITE) == 0) 2252be2b1797SNate Lawson return (EPERM); 2253a89fcf28STakanori Watanabe 2254be2b1797SNate Lawson /* Core system ioctls. */ 225515e32d5dSMike Smith switch (cmd) { 225615e32d5dSMike Smith case ACPIIO_ENABLE: 22570ae55423SMike Smith if (ACPI_FAILURE(acpi_Enable(sc))) 225815e32d5dSMike Smith error = ENXIO; 225915e32d5dSMike Smith break; 226015e32d5dSMike Smith case ACPIIO_DISABLE: 22610ae55423SMike Smith if (ACPI_FAILURE(acpi_Disable(sc))) 226215e32d5dSMike Smith error = ENXIO; 226315e32d5dSMike Smith break; 226415e32d5dSMike Smith case ACPIIO_SETSLPSTATE: 226515e32d5dSMike Smith if (!sc->acpi_enabled) { 226615e32d5dSMike Smith error = ENXIO; 226715e32d5dSMike Smith break; 226815e32d5dSMike Smith } 226915e32d5dSMike Smith state = *(int *)addr; 22702d610c46SNate Lawson if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) { 22712d610c46SNate Lawson if (ACPI_FAILURE(acpi_SetSleepState(sc, state))) 227215e32d5dSMike Smith error = EINVAL; 22732d610c46SNate Lawson } else { 22742d610c46SNate Lawson error = EINVAL; 22752d610c46SNate Lawson } 227615e32d5dSMike Smith break; 227715e32d5dSMike Smith default: 22780ae55423SMike Smith if (error == 0) 227915e32d5dSMike Smith error = EINVAL; 228015e32d5dSMike Smith break; 228115e32d5dSMike Smith } 2282917d44c8SMitsuru IWASAKI 2283917d44c8SMitsuru IWASAKI out: 2284cb9b0d80SMike Smith ACPI_UNLOCK; 228515e32d5dSMike Smith return (error); 228615e32d5dSMike Smith } 228715e32d5dSMike Smith 22881d073b1dSJohn Baldwin static int 2289d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 2290d75de536SMitsuru IWASAKI { 2291d75de536SMitsuru IWASAKI char sleep_state[4]; 2292d75de536SMitsuru IWASAKI char buf[16]; 2293d75de536SMitsuru IWASAKI int error; 2294d75de536SMitsuru IWASAKI UINT8 state, TypeA, TypeB; 2295d75de536SMitsuru IWASAKI 2296d75de536SMitsuru IWASAKI buf[0] = '\0'; 2297d75de536SMitsuru IWASAKI for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX+1; state++) { 2298d75de536SMitsuru IWASAKI if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) { 2299d75de536SMitsuru IWASAKI sprintf(sleep_state, "S%d ", state); 2300d75de536SMitsuru IWASAKI strcat(buf, sleep_state); 2301d75de536SMitsuru IWASAKI } 2302d75de536SMitsuru IWASAKI } 2303d75de536SMitsuru IWASAKI error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 2304d75de536SMitsuru IWASAKI return (error); 2305d75de536SMitsuru IWASAKI } 2306d75de536SMitsuru IWASAKI 2307d75de536SMitsuru IWASAKI static int 23081d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 23091d073b1dSJohn Baldwin { 23101d073b1dSJohn Baldwin char sleep_state[10]; 23111d073b1dSJohn Baldwin int error; 23121d073b1dSJohn Baldwin u_int new_state, old_state; 23131d073b1dSJohn Baldwin 23141d073b1dSJohn Baldwin old_state = *(u_int *)oidp->oid_arg1; 2315b8670bedSMitsuru IWASAKI if (old_state > ACPI_S_STATES_MAX+1) { 23161d073b1dSJohn Baldwin strcpy(sleep_state, "unknown"); 2317cb9b0d80SMike Smith } else { 2318b8670bedSMitsuru IWASAKI bzero(sleep_state, sizeof(sleep_state)); 23191d073b1dSJohn Baldwin strncpy(sleep_state, sleep_state_names[old_state], 23201d073b1dSJohn Baldwin sizeof(sleep_state_names[old_state])); 2321cb9b0d80SMike Smith } 23221d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 23231d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) { 2324be2b1797SNate Lawson new_state = ACPI_STATE_S0; 2325be2b1797SNate Lawson for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) { 23261d073b1dSJohn Baldwin if (strncmp(sleep_state, sleep_state_names[new_state], 23271d073b1dSJohn Baldwin sizeof(sleep_state)) == 0) 23281d073b1dSJohn Baldwin break; 2329cb9b0d80SMike Smith } 2330931a10c9SMitsuru IWASAKI if (new_state <= ACPI_S_STATES_MAX + 1) { 2331be2b1797SNate Lawson if (new_state != old_state) 23321d073b1dSJohn Baldwin *(u_int *)oidp->oid_arg1 = new_state; 2333cb9b0d80SMike Smith } else { 23341d073b1dSJohn Baldwin error = EINVAL; 23351d073b1dSJohn Baldwin } 2336cb9b0d80SMike Smith } 2337be2b1797SNate Lawson 23381d073b1dSJohn Baldwin return (error); 23391d073b1dSJohn Baldwin } 23401d073b1dSJohn Baldwin 23419b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */ 23429b937d48SNate Lawson void 23439b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify) 23449b937d48SNate Lawson { 23459b937d48SNate Lawson char notify_buf[16]; 23469b937d48SNate Lawson ACPI_BUFFER handle_buf; 23479b937d48SNate Lawson ACPI_STATUS status; 23489b937d48SNate Lawson 23499b937d48SNate Lawson if (subsystem == NULL) 23509b937d48SNate Lawson return; 23519b937d48SNate Lawson 23529b937d48SNate Lawson handle_buf.Pointer = NULL; 23539b937d48SNate Lawson handle_buf.Length = ACPI_ALLOCATE_BUFFER; 23549b937d48SNate Lawson status = AcpiNsHandleToPathname(h, &handle_buf); 23559b937d48SNate Lawson if (ACPI_FAILURE(status)) 23569b937d48SNate Lawson return; 23579b937d48SNate Lawson snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify); 23589b937d48SNate Lawson devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf); 23599b937d48SNate Lawson AcpiOsFree(handle_buf.Pointer); 23609b937d48SNate Lawson } 23619b937d48SNate Lawson 236215e32d5dSMike Smith #ifdef ACPI_DEBUG 23630ae55423SMike Smith /* 23640ae55423SMike Smith * Support for parsing debug options from the kernel environment. 23650ae55423SMike Smith * 23660ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 23670ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and 23680ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by 23690ae55423SMike Smith * prefixing the bit name with !. 23700ae55423SMike Smith */ 237115e32d5dSMike Smith struct debugtag 237215e32d5dSMike Smith { 237315e32d5dSMike Smith char *name; 237415e32d5dSMike Smith UINT32 value; 237515e32d5dSMike Smith }; 237615e32d5dSMike Smith 237715e32d5dSMike Smith static struct debugtag dbg_layer[] = { 2378c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES}, 2379c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE}, 2380c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS}, 2381c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES}, 2382c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 2383c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER}, 2384c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 2385c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER}, 2386c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES}, 2387d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 23884c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 2389d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 2390297835bcSNate Lawson {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 23914c1cdee6SMike Smith 2392c5ba0be4SMike Smith {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 2393c5ba0be4SMike Smith {"ACPI_BATTERY", ACPI_BATTERY}, 23943184cf5aSNate Lawson {"ACPI_BUS", ACPI_BUS}, 2395c5ba0be4SMike Smith {"ACPI_BUTTON", ACPI_BUTTON}, 23963184cf5aSNate Lawson {"ACPI_EC", ACPI_EC}, 23973184cf5aSNate Lawson {"ACPI_FAN", ACPI_FAN}, 23983184cf5aSNate Lawson {"ACPI_POWERRES", ACPI_POWERRES}, 23994c1cdee6SMike Smith {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 2400cb9b0d80SMike Smith {"ACPI_THERMAL", ACPI_THERMAL}, 24013184cf5aSNate Lawson {"ACPI_TIMER", ACPI_TIMER}, 2402b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 240315e32d5dSMike Smith {NULL, 0} 240415e32d5dSMike Smith }; 240515e32d5dSMike Smith 240615e32d5dSMike Smith static struct debugtag dbg_level[] = { 24074c1cdee6SMike Smith {"ACPI_LV_ERROR", ACPI_LV_ERROR}, 24089501b603SJohn Baldwin {"ACPI_LV_WARN", ACPI_LV_WARN}, 24099501b603SJohn Baldwin {"ACPI_LV_INIT", ACPI_LV_INIT}, 24104c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 24119501b603SJohn Baldwin {"ACPI_LV_INFO", ACPI_LV_INFO}, 24124c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 2413d62ab2f4SMitsuru IWASAKI 2414d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */ 2415297835bcSNate Lawson {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 24164c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 24174c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 2418d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 24194c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 24204c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 24214c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 24224c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 24234c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 24244c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 24254c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 24264c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 24274c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 24284c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 2429d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 2430d62ab2f4SMitsuru IWASAKI 2431d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */ 2432d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 2433d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 2434d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 2435d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 24364c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL}, 2437d62ab2f4SMitsuru IWASAKI 2438d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 2439d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 2440d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 2441d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO}, 2442d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 2443d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 2444d62ab2f4SMitsuru IWASAKI 2445d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 244698479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 244798479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 244898479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 244998479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 245098479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 245115e32d5dSMike Smith {NULL, 0} 245215e32d5dSMike Smith }; 245315e32d5dSMike Smith 245415e32d5dSMike Smith static void 245515e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 245615e32d5dSMike Smith { 245715e32d5dSMike Smith char *ep; 245815e32d5dSMike Smith int i, l; 24590ae55423SMike Smith int set; 246015e32d5dSMike Smith 246115e32d5dSMike Smith while (*cp) { 246215e32d5dSMike Smith if (isspace(*cp)) { 246315e32d5dSMike Smith cp++; 246415e32d5dSMike Smith continue; 246515e32d5dSMike Smith } 246615e32d5dSMike Smith ep = cp; 246715e32d5dSMike Smith while (*ep && !isspace(*ep)) 246815e32d5dSMike Smith ep++; 24690ae55423SMike Smith if (*cp == '!') { 24700ae55423SMike Smith set = 0; 24710ae55423SMike Smith cp++; 24720ae55423SMike Smith if (cp == ep) 24730ae55423SMike Smith continue; 24740ae55423SMike Smith } else { 24750ae55423SMike Smith set = 1; 24760ae55423SMike Smith } 247715e32d5dSMike Smith l = ep - cp; 247815e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) { 247915e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) { 2480be2b1797SNate Lawson if (set) 248115e32d5dSMike Smith *flag |= tag[i].value; 2482be2b1797SNate Lawson else 24830ae55423SMike Smith *flag &= ~tag[i].value; 248415e32d5dSMike Smith printf("ACPI_DEBUG: set '%s'\n", tag[i].name); 248515e32d5dSMike Smith } 248615e32d5dSMike Smith } 248715e32d5dSMike Smith cp = ep; 248815e32d5dSMike Smith } 248915e32d5dSMike Smith } 249015e32d5dSMike Smith 249115e32d5dSMike Smith static void 24922a4ac806SMike Smith acpi_set_debugging(void *junk) 249315e32d5dSMike Smith { 249494aae282SMike Barcroft char *cp; 249515e32d5dSMike Smith 24961d7b121cSNate Lawson if (cold) { 24970ae55423SMike Smith AcpiDbgLayer = 0; 24980ae55423SMike Smith AcpiDbgLevel = 0; 24991d7b121cSNate Lawson } 25001d7b121cSNate Lawson 250194aae282SMike Barcroft if ((cp = getenv("debug.acpi.layer")) != NULL) { 250215e32d5dSMike Smith acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer); 250394aae282SMike Barcroft freeenv(cp); 250494aae282SMike Barcroft } 250594aae282SMike Barcroft if ((cp = getenv("debug.acpi.level")) != NULL) { 250615e32d5dSMike Smith acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel); 250794aae282SMike Barcroft freeenv(cp); 250894aae282SMike Barcroft } 25090ae55423SMike Smith 25101d7b121cSNate Lawson if (cold) { 25111d7b121cSNate Lawson printf("ACPI debug layer 0x%x debug level 0x%x\n", 25121d7b121cSNate Lawson AcpiDbgLayer, AcpiDbgLevel); 25131d7b121cSNate Lawson } 251415e32d5dSMike Smith } 2515be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 2516be2b1797SNate Lawson NULL); 25171d7b121cSNate Lawson 25181d7b121cSNate Lawson static int 25191d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS) 25201d7b121cSNate Lawson { 252154f6bca0SNate Lawson int error, *dbg; 25221d7b121cSNate Lawson struct debugtag *tag; 252354f6bca0SNate Lawson struct sbuf sb; 25241d7b121cSNate Lawson 252554f6bca0SNate Lawson if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) 252654f6bca0SNate Lawson return (ENOMEM); 25271d7b121cSNate Lawson if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) { 25281d7b121cSNate Lawson tag = &dbg_layer[0]; 25291d7b121cSNate Lawson dbg = &AcpiDbgLayer; 25301d7b121cSNate Lawson } else { 25311d7b121cSNate Lawson tag = &dbg_level[0]; 25321d7b121cSNate Lawson dbg = &AcpiDbgLevel; 25331d7b121cSNate Lawson } 25341d7b121cSNate Lawson 25351d7b121cSNate Lawson /* Get old values if this is a get request. */ 25361d7b121cSNate Lawson if (*dbg == 0) { 253754f6bca0SNate Lawson sbuf_cpy(&sb, "NONE"); 25381d7b121cSNate Lawson } else if (req->newptr == NULL) { 25391d7b121cSNate Lawson for (; tag->name != NULL; tag++) { 254054f6bca0SNate Lawson if ((*dbg & tag->value) == tag->value) 254154f6bca0SNate Lawson sbuf_printf(&sb, "%s ", tag->name); 25421d7b121cSNate Lawson } 25431d7b121cSNate Lawson } 254454f6bca0SNate Lawson sbuf_trim(&sb); 254554f6bca0SNate Lawson sbuf_finish(&sb); 25461d7b121cSNate Lawson 254754f6bca0SNate Lawson error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req); 254854f6bca0SNate Lawson sbuf_delete(&sb); 25491d7b121cSNate Lawson 25501d7b121cSNate Lawson /* If the user is setting a string, parse it. */ 25511d7b121cSNate Lawson if (error == 0 && req->newptr != NULL) { 25521d7b121cSNate Lawson *dbg = 0; 25531d7b121cSNate Lawson setenv((char *)oidp->oid_arg1, (char *)req->newptr); 25541d7b121cSNate Lawson acpi_set_debugging(NULL); 25551d7b121cSNate Lawson } 25561d7b121cSNate Lawson 25571d7b121cSNate Lawson return (error); 25581d7b121cSNate Lawson } 25591d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING, 25601d7b121cSNate Lawson "debug.acpi.layer", 0, acpi_debug_sysctl, "A", ""); 25611d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING, 25621d7b121cSNate Lawson "debug.acpi.level", 0, acpi_debug_sysctl, "A", ""); 256315e32d5dSMike Smith #endif 2564f9390180SMitsuru IWASAKI 2565f9390180SMitsuru IWASAKI static int 2566f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...) 2567f9390180SMitsuru IWASAKI { 2568f9390180SMitsuru IWASAKI int state, acpi_state; 2569f9390180SMitsuru IWASAKI int error; 2570f9390180SMitsuru IWASAKI struct acpi_softc *sc; 2571f9390180SMitsuru IWASAKI va_list ap; 2572f9390180SMitsuru IWASAKI 2573f9390180SMitsuru IWASAKI error = 0; 2574f9390180SMitsuru IWASAKI switch (cmd) { 2575f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND: 2576f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg; 2577f9390180SMitsuru IWASAKI if (sc == NULL) { 2578f9390180SMitsuru IWASAKI error = EINVAL; 2579f9390180SMitsuru IWASAKI goto out; 2580f9390180SMitsuru IWASAKI } 2581f9390180SMitsuru IWASAKI 2582f9390180SMitsuru IWASAKI va_start(ap, arg); 2583f9390180SMitsuru IWASAKI state = va_arg(ap, int); 2584f9390180SMitsuru IWASAKI va_end(ap); 2585f9390180SMitsuru IWASAKI 2586f9390180SMitsuru IWASAKI switch (state) { 2587f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY: 2588f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx; 2589f9390180SMitsuru IWASAKI break; 2590f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND: 2591f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx; 2592f9390180SMitsuru IWASAKI break; 2593f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE: 2594f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4; 2595f9390180SMitsuru IWASAKI break; 2596f9390180SMitsuru IWASAKI default: 2597f9390180SMitsuru IWASAKI error = EINVAL; 2598f9390180SMitsuru IWASAKI goto out; 2599f9390180SMitsuru IWASAKI } 2600f9390180SMitsuru IWASAKI 2601f9390180SMitsuru IWASAKI acpi_SetSleepState(sc, acpi_state); 2602f9390180SMitsuru IWASAKI break; 2603f9390180SMitsuru IWASAKI default: 2604f9390180SMitsuru IWASAKI error = EINVAL; 2605f9390180SMitsuru IWASAKI goto out; 2606f9390180SMitsuru IWASAKI } 2607f9390180SMitsuru IWASAKI 2608f9390180SMitsuru IWASAKI out: 2609f9390180SMitsuru IWASAKI return (error); 2610f9390180SMitsuru IWASAKI } 2611f9390180SMitsuru IWASAKI 2612f9390180SMitsuru IWASAKI static void 2613f9390180SMitsuru IWASAKI acpi_pm_register(void *arg) 2614f9390180SMitsuru IWASAKI { 2615be2b1797SNate Lawson if (!cold || resource_disabled("acpi", 0)) 26166c407052SMitsuru IWASAKI return; 2617f9390180SMitsuru IWASAKI 2618f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 2619f9390180SMitsuru IWASAKI } 2620f9390180SMitsuru IWASAKI 2621f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 2622