acpi_button.c revision 129783
1/*- 2 * Copyright (c) 2000 Mitsaru IWASAKI <iwasaki@jp.freebsd.org> 3 * Copyright (c) 2000 Michael Smith <msmith@freebsd.org> 4 * Copyright (c) 2000 BSDi 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD: head/sys/dev/acpica/acpi_button.c 129783 2004-05-27 18:38:45Z njl $ 29 */ 30 31#include "opt_acpi.h" 32#include <sys/param.h> 33#include <sys/kernel.h> 34#include <sys/bus.h> 35 36#include "acpi.h" 37#include <dev/acpica/acpivar.h> 38 39/* Hooks for the ACPI CA debugging infrastructure */ 40#define _COMPONENT ACPI_BUTTON 41ACPI_MODULE_NAME("BUTTON") 42 43struct acpi_button_softc { 44 device_t button_dev; 45 ACPI_HANDLE button_handle; 46 boolean_t button_type; 47#define ACPI_POWER_BUTTON 0 48#define ACPI_SLEEP_BUTTON 1 49 boolean_t fixed; 50}; 51 52#define ACPI_NOTIFY_BUTTON_PRESSED_FOR_SLEEP 0x80 53#define ACPI_NOTIFY_BUTTON_PRESSED_FOR_WAKEUP 0x02 54 55static int acpi_button_probe(device_t dev); 56static int acpi_button_attach(device_t dev); 57static int acpi_button_suspend(device_t dev); 58static int acpi_button_resume(device_t dev); 59static void acpi_button_notify_handler(ACPI_HANDLE h, UINT32 notify, 60 void *context); 61static ACPI_STATUS 62 acpi_button_fixed_handler(void *context); 63static void acpi_button_notify_sleep(void *arg); 64static void acpi_button_notify_wakeup(void *arg); 65 66static device_method_t acpi_button_methods[] = { 67 /* Device interface */ 68 DEVMETHOD(device_probe, acpi_button_probe), 69 DEVMETHOD(device_attach, acpi_button_attach), 70 DEVMETHOD(device_suspend, acpi_button_suspend), 71 DEVMETHOD(device_shutdown, acpi_button_suspend), 72 DEVMETHOD(device_resume, acpi_button_resume), 73 74 {0, 0} 75}; 76 77static driver_t acpi_button_driver = { 78 "acpi_button", 79 acpi_button_methods, 80 sizeof(struct acpi_button_softc), 81}; 82 83static devclass_t acpi_button_devclass; 84DRIVER_MODULE(acpi_button, acpi, acpi_button_driver, acpi_button_devclass, 85 0, 0); 86MODULE_DEPEND(acpi_button, acpi, 1, 1, 1); 87 88static int 89acpi_button_probe(device_t dev) 90{ 91 struct acpi_button_softc *sc; 92 int ret = ENXIO; 93 94 sc = device_get_softc(dev); 95 if (acpi_get_type(dev) == ACPI_TYPE_DEVICE && !acpi_disabled("button")) { 96 if (acpi_MatchHid(dev, "PNP0C0C")) { 97 device_set_desc(dev, "Power Button"); 98 sc->button_type = ACPI_POWER_BUTTON; 99 ret = 0; 100 } else if (acpi_MatchHid(dev, "ACPI_FPB")) { 101 device_set_desc(dev, "Power Button (fixed)"); 102 sc->button_type = ACPI_POWER_BUTTON; 103 sc->fixed = 1; 104 ret = 0; 105 } else if (acpi_MatchHid(dev, "PNP0C0E")) { 106 device_set_desc(dev, "Sleep Button"); 107 sc->button_type = ACPI_SLEEP_BUTTON; 108 ret = 0; 109 } else if (acpi_MatchHid(dev, "ACPI_FSB")) { 110 device_set_desc(dev, "Sleep Button (fixed)"); 111 sc->button_type = ACPI_SLEEP_BUTTON; 112 sc->fixed = 1; 113 ret = 0; 114 } 115 } 116 return (ret); 117} 118 119static int 120acpi_button_attach(device_t dev) 121{ 122 struct acpi_button_softc *sc; 123 ACPI_STATUS status; 124 int event; 125 126 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 127 128 sc = device_get_softc(dev); 129 sc->button_dev = dev; 130 sc->button_handle = acpi_get_handle(dev); 131 event = (sc->button_type == ACPI_SLEEP_BUTTON) ? 132 ACPI_EVENT_SLEEP_BUTTON : ACPI_EVENT_POWER_BUTTON; 133 134 /* 135 * Install the new handler. We could remove any fixed handlers added 136 * from the FADT once we have a duplicate from the AML but some systems 137 * only return events on one or the other so we have to keep both. 138 */ 139 if (sc->fixed) { 140 AcpiClearEvent(event); 141 status = AcpiInstallFixedEventHandler(event, 142 acpi_button_fixed_handler, sc); 143 } else { 144 /* 145 * If a system does not get lid events, it may make sense to change 146 * the type to ACPI_ALL_NOTIFY. Some systems generate both a wake 147 * and runtime notify in that case though. 148 */ 149 status = AcpiInstallNotifyHandler(sc->button_handle, 150 ACPI_DEVICE_NOTIFY, acpi_button_notify_handler, sc); 151 } 152 if (ACPI_FAILURE(status)) { 153 device_printf(sc->button_dev, "couldn't install notify handler - %s\n", 154 AcpiFormatException(status)); 155 return_VALUE (ENXIO); 156 } 157 158 /* Enable the GPE for wake/runtime. */ 159 acpi_wake_init(dev, ACPI_GPE_TYPE_WAKE_RUN); 160 acpi_wake_set_enable(dev, 1); 161 162 return_VALUE (0); 163} 164 165static int 166acpi_button_suspend(device_t dev) 167{ 168 struct acpi_softc *acpi_sc; 169 170 acpi_sc = acpi_device_get_parent_softc(dev); 171 acpi_wake_sleep_prep(dev, acpi_sc->acpi_sstate); 172 return (0); 173} 174 175static int 176acpi_button_resume(device_t dev) 177{ 178 return (0); 179} 180 181static void 182acpi_button_notify_sleep(void *arg) 183{ 184 struct acpi_button_softc *sc; 185 struct acpi_softc *acpi_sc; 186 187 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 188 189 sc = (struct acpi_button_softc *)arg; 190 acpi_sc = acpi_device_get_parent_softc(sc->button_dev); 191 if (acpi_sc == NULL) 192 return_VOID; 193 194 acpi_UserNotify("Button", sc->button_handle, sc->button_type); 195 196 switch (sc->button_type) { 197 case ACPI_POWER_BUTTON: 198 ACPI_VPRINT(sc->button_dev, acpi_sc, "power button pressed\n"); 199 acpi_event_power_button_sleep(acpi_sc); 200 break; 201 case ACPI_SLEEP_BUTTON: 202 ACPI_VPRINT(sc->button_dev, acpi_sc, "sleep button pressed\n"); 203 acpi_event_sleep_button_sleep(acpi_sc); 204 break; 205 default: 206 break; /* unknown button type */ 207 } 208} 209 210static void 211acpi_button_notify_wakeup(void *arg) 212{ 213 struct acpi_button_softc *sc; 214 struct acpi_softc *acpi_sc; 215 216 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 217 218 sc = (struct acpi_button_softc *)arg; 219 acpi_sc = acpi_device_get_parent_softc(sc->button_dev); 220 if (acpi_sc == NULL) 221 return_VOID; 222 223 acpi_UserNotify("Button", sc->button_handle, sc->button_type); 224 225 switch (sc->button_type) { 226 case ACPI_POWER_BUTTON: 227 ACPI_VPRINT(sc->button_dev, acpi_sc, "wakeup by power button\n"); 228 acpi_event_power_button_wake(acpi_sc); 229 break; 230 case ACPI_SLEEP_BUTTON: 231 ACPI_VPRINT(sc->button_dev, acpi_sc, "wakeup by sleep button\n"); 232 acpi_event_sleep_button_wake(acpi_sc); 233 break; 234 default: 235 break; /* unknown button type */ 236 } 237} 238 239static void 240acpi_button_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context) 241{ 242 struct acpi_button_softc *sc; 243 244 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify); 245 246 sc = (struct acpi_button_softc *)context; 247 switch (notify) { 248 case ACPI_NOTIFY_BUTTON_PRESSED_FOR_SLEEP: 249 AcpiOsQueueForExecution(OSD_PRIORITY_LO, 250 acpi_button_notify_sleep, sc); 251 break; 252 case ACPI_NOTIFY_BUTTON_PRESSED_FOR_WAKEUP: 253 AcpiOsQueueForExecution(OSD_PRIORITY_LO, 254 acpi_button_notify_wakeup, sc); 255 break; 256 default: 257 device_printf(sc->button_dev, "unknown notify %#x\n", notify); 258 break; 259 } 260} 261 262static ACPI_STATUS 263acpi_button_fixed_handler(void *context) 264{ 265 struct acpi_button_softc *sc = (struct acpi_button_softc *)context; 266 267 ACPI_FUNCTION_TRACE_PTR((char *)(uintptr_t)__func__, context); 268 269 if (context == NULL) 270 return_ACPI_STATUS (AE_BAD_PARAMETER); 271 272 acpi_button_notify_handler(sc->button_handle, 273 ACPI_NOTIFY_BUTTON_PRESSED_FOR_SLEEP, sc); 274 return_ACPI_STATUS (AE_OK); 275} 276