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