acpi_button.c revision 129879
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 129879 2004-05-30 20:08:47Z phk $ 29 */ 30 31#include "opt_acpi.h" 32#include <sys/param.h> 33#include <sys/kernel.h> 34#include <sys/module.h> 35#include <sys/bus.h> 36 37#include "acpi.h" 38#include <dev/acpica/acpivar.h> 39 40/* Hooks for the ACPI CA debugging infrastructure */ 41#define _COMPONENT ACPI_BUTTON 42ACPI_MODULE_NAME("BUTTON") 43 44struct acpi_button_softc { 45 device_t button_dev; 46 ACPI_HANDLE button_handle; 47 boolean_t button_type; 48#define ACPI_POWER_BUTTON 0 49#define ACPI_SLEEP_BUTTON 1 50 boolean_t fixed; 51}; 52 53#define ACPI_NOTIFY_BUTTON_PRESSED_FOR_SLEEP 0x80 54#define ACPI_NOTIFY_BUTTON_PRESSED_FOR_WAKEUP 0x02 55 56static int acpi_button_probe(device_t dev); 57static int acpi_button_attach(device_t dev); 58static int acpi_button_suspend(device_t dev); 59static int acpi_button_resume(device_t dev); 60static void acpi_button_notify_handler(ACPI_HANDLE h, UINT32 notify, 61 void *context); 62static ACPI_STATUS 63 acpi_button_fixed_handler(void *context); 64static void acpi_button_notify_sleep(void *arg); 65static void acpi_button_notify_wakeup(void *arg); 66 67static device_method_t acpi_button_methods[] = { 68 /* Device interface */ 69 DEVMETHOD(device_probe, acpi_button_probe), 70 DEVMETHOD(device_attach, acpi_button_attach), 71 DEVMETHOD(device_suspend, acpi_button_suspend), 72 DEVMETHOD(device_shutdown, acpi_button_suspend), 73 DEVMETHOD(device_resume, acpi_button_resume), 74 75 {0, 0} 76}; 77 78static driver_t acpi_button_driver = { 79 "acpi_button", 80 acpi_button_methods, 81 sizeof(struct acpi_button_softc), 82}; 83 84static devclass_t acpi_button_devclass; 85DRIVER_MODULE(acpi_button, acpi, acpi_button_driver, acpi_button_devclass, 86 0, 0); 87MODULE_DEPEND(acpi_button, acpi, 1, 1, 1); 88 89static int 90acpi_button_probe(device_t dev) 91{ 92 struct acpi_button_softc *sc; 93 int ret = ENXIO; 94 95 sc = device_get_softc(dev); 96 if (acpi_get_type(dev) == ACPI_TYPE_DEVICE && !acpi_disabled("button")) { 97 if (acpi_MatchHid(dev, "PNP0C0C")) { 98 device_set_desc(dev, "Power Button"); 99 sc->button_type = ACPI_POWER_BUTTON; 100 ret = 0; 101 } else if (acpi_MatchHid(dev, "ACPI_FPB")) { 102 device_set_desc(dev, "Power Button (fixed)"); 103 sc->button_type = ACPI_POWER_BUTTON; 104 sc->fixed = 1; 105 ret = 0; 106 } else if (acpi_MatchHid(dev, "PNP0C0E")) { 107 device_set_desc(dev, "Sleep Button"); 108 sc->button_type = ACPI_SLEEP_BUTTON; 109 ret = 0; 110 } else if (acpi_MatchHid(dev, "ACPI_FSB")) { 111 device_set_desc(dev, "Sleep Button (fixed)"); 112 sc->button_type = ACPI_SLEEP_BUTTON; 113 sc->fixed = 1; 114 ret = 0; 115 } 116 } 117 return (ret); 118} 119 120static int 121acpi_button_attach(device_t dev) 122{ 123 struct acpi_button_softc *sc; 124 ACPI_STATUS status; 125 int event; 126 127 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 128 129 sc = device_get_softc(dev); 130 sc->button_dev = dev; 131 sc->button_handle = acpi_get_handle(dev); 132 event = (sc->button_type == ACPI_SLEEP_BUTTON) ? 133 ACPI_EVENT_SLEEP_BUTTON : ACPI_EVENT_POWER_BUTTON; 134 135 /* 136 * Install the new handler. We could remove any fixed handlers added 137 * from the FADT once we have a duplicate from the AML but some systems 138 * only return events on one or the other so we have to keep both. 139 */ 140 if (sc->fixed) { 141 AcpiClearEvent(event); 142 status = AcpiInstallFixedEventHandler(event, 143 acpi_button_fixed_handler, sc); 144 } else { 145 /* 146 * If a system does not get lid events, it may make sense to change 147 * the type to ACPI_ALL_NOTIFY. Some systems generate both a wake 148 * and runtime notify in that case though. 149 */ 150 status = AcpiInstallNotifyHandler(sc->button_handle, 151 ACPI_DEVICE_NOTIFY, acpi_button_notify_handler, sc); 152 } 153 if (ACPI_FAILURE(status)) { 154 device_printf(sc->button_dev, "couldn't install notify handler - %s\n", 155 AcpiFormatException(status)); 156 return_VALUE (ENXIO); 157 } 158 159 /* Enable the GPE for wake/runtime. */ 160 acpi_wake_init(dev, ACPI_GPE_TYPE_WAKE_RUN); 161 acpi_wake_set_enable(dev, 1); 162 163 return_VALUE (0); 164} 165 166static int 167acpi_button_suspend(device_t dev) 168{ 169 struct acpi_softc *acpi_sc; 170 171 acpi_sc = acpi_device_get_parent_softc(dev); 172 acpi_wake_sleep_prep(dev, acpi_sc->acpi_sstate); 173 return (0); 174} 175 176static int 177acpi_button_resume(device_t dev) 178{ 179 acpi_wake_run_prep(dev); 180 return (0); 181} 182 183static void 184acpi_button_notify_sleep(void *arg) 185{ 186 struct acpi_button_softc *sc; 187 struct acpi_softc *acpi_sc; 188 189 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 190 191 sc = (struct acpi_button_softc *)arg; 192 acpi_sc = acpi_device_get_parent_softc(sc->button_dev); 193 if (acpi_sc == NULL) 194 return_VOID; 195 196 acpi_UserNotify("Button", sc->button_handle, sc->button_type); 197 198 switch (sc->button_type) { 199 case ACPI_POWER_BUTTON: 200 ACPI_VPRINT(sc->button_dev, acpi_sc, "power button pressed\n"); 201 acpi_event_power_button_sleep(acpi_sc); 202 break; 203 case ACPI_SLEEP_BUTTON: 204 ACPI_VPRINT(sc->button_dev, acpi_sc, "sleep button pressed\n"); 205 acpi_event_sleep_button_sleep(acpi_sc); 206 break; 207 default: 208 break; /* unknown button type */ 209 } 210} 211 212static void 213acpi_button_notify_wakeup(void *arg) 214{ 215 struct acpi_button_softc *sc; 216 struct acpi_softc *acpi_sc; 217 218 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 219 220 sc = (struct acpi_button_softc *)arg; 221 acpi_sc = acpi_device_get_parent_softc(sc->button_dev); 222 if (acpi_sc == NULL) 223 return_VOID; 224 225 acpi_UserNotify("Button", sc->button_handle, sc->button_type); 226 227 switch (sc->button_type) { 228 case ACPI_POWER_BUTTON: 229 ACPI_VPRINT(sc->button_dev, acpi_sc, "wakeup by power button\n"); 230 acpi_event_power_button_wake(acpi_sc); 231 break; 232 case ACPI_SLEEP_BUTTON: 233 ACPI_VPRINT(sc->button_dev, acpi_sc, "wakeup by sleep button\n"); 234 acpi_event_sleep_button_wake(acpi_sc); 235 break; 236 default: 237 break; /* unknown button type */ 238 } 239} 240 241static void 242acpi_button_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context) 243{ 244 struct acpi_button_softc *sc; 245 246 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify); 247 248 sc = (struct acpi_button_softc *)context; 249 switch (notify) { 250 case ACPI_NOTIFY_BUTTON_PRESSED_FOR_SLEEP: 251 AcpiOsQueueForExecution(OSD_PRIORITY_LO, 252 acpi_button_notify_sleep, sc); 253 break; 254 case ACPI_NOTIFY_BUTTON_PRESSED_FOR_WAKEUP: 255 AcpiOsQueueForExecution(OSD_PRIORITY_LO, 256 acpi_button_notify_wakeup, sc); 257 break; 258 default: 259 device_printf(sc->button_dev, "unknown notify %#x\n", notify); 260 break; 261 } 262} 263 264static ACPI_STATUS 265acpi_button_fixed_handler(void *context) 266{ 267 struct acpi_button_softc *sc = (struct acpi_button_softc *)context; 268 269 ACPI_FUNCTION_TRACE_PTR((char *)(uintptr_t)__func__, context); 270 271 if (context == NULL) 272 return_ACPI_STATUS (AE_BAD_PARAMETER); 273 274 acpi_button_notify_handler(sc->button_handle, 275 ACPI_NOTIFY_BUTTON_PRESSED_FOR_SLEEP, sc); 276 return_ACPI_STATUS (AE_OK); 277} 278