2 * Copyright (c) 2000 Mitsaru IWASAKI <iwasaki@jp.freebsd.org>
3 * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
4 * Copyright (c) 2000 BSDi
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
28 * $FreeBSD: src/sys/dev/acpica/acpi_button.c,v 1.26 2004/05/30 20:08:23 phk Exp $
29 * $DragonFly: src/sys/dev/acpica5/acpi_button.c,v 1.3 2004/07/05 00:07:35 dillon Exp $
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
39 #include <dev/acpica5/acpivar.h>
41 /* Hooks for the ACPI CA debugging infrastructure */
42 #define _COMPONENT ACPI_BUTTON
43 ACPI_MODULE_NAME("BUTTON")
45 struct acpi_button_softc {
47 ACPI_HANDLE button_handle;
48 boolean_t button_type;
49 #define ACPI_POWER_BUTTON 0
50 #define ACPI_SLEEP_BUTTON 1
54 #define ACPI_NOTIFY_BUTTON_PRESSED_FOR_SLEEP 0x80
55 #define ACPI_NOTIFY_BUTTON_PRESSED_FOR_WAKEUP 0x02
57 static int acpi_button_probe(device_t dev);
58 static int acpi_button_attach(device_t dev);
59 static int acpi_button_suspend(device_t dev);
60 static int acpi_button_resume(device_t dev);
61 static void acpi_button_notify_handler(ACPI_HANDLE h, UINT32 notify,
64 acpi_button_fixed_handler(void *context);
65 static void acpi_button_notify_sleep(void *arg);
66 static void acpi_button_notify_wakeup(void *arg);
68 static device_method_t acpi_button_methods[] = {
69 /* Device interface */
70 DEVMETHOD(device_probe, acpi_button_probe),
71 DEVMETHOD(device_attach, acpi_button_attach),
72 DEVMETHOD(device_suspend, acpi_button_suspend),
73 DEVMETHOD(device_shutdown, acpi_button_suspend),
74 DEVMETHOD(device_resume, acpi_button_resume),
79 static driver_t acpi_button_driver = {
82 sizeof(struct acpi_button_softc),
85 static devclass_t acpi_button_devclass;
86 DRIVER_MODULE(acpi_button, acpi, acpi_button_driver, acpi_button_devclass,
88 MODULE_DEPEND(acpi_button, acpi, 1, 1, 1);
91 acpi_button_probe(device_t dev)
93 struct acpi_button_softc *sc;
96 sc = device_get_softc(dev);
97 if (acpi_get_type(dev) == ACPI_TYPE_DEVICE && !acpi_disabled("button")) {
98 if (acpi_MatchHid(dev, "PNP0C0C")) {
99 device_set_desc(dev, "Power Button");
100 sc->button_type = ACPI_POWER_BUTTON;
102 } else if (acpi_MatchHid(dev, "ACPI_FPB")) {
103 device_set_desc(dev, "Power Button (fixed)");
104 sc->button_type = ACPI_POWER_BUTTON;
107 } else if (acpi_MatchHid(dev, "PNP0C0E")) {
108 device_set_desc(dev, "Sleep Button");
109 sc->button_type = ACPI_SLEEP_BUTTON;
111 } else if (acpi_MatchHid(dev, "ACPI_FSB")) {
112 device_set_desc(dev, "Sleep Button (fixed)");
113 sc->button_type = ACPI_SLEEP_BUTTON;
122 acpi_button_attach(device_t dev)
124 struct acpi_button_softc *sc;
128 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
130 sc = device_get_softc(dev);
131 sc->button_dev = dev;
132 sc->button_handle = acpi_get_handle(dev);
133 event = (sc->button_type == ACPI_SLEEP_BUTTON) ?
134 ACPI_EVENT_SLEEP_BUTTON : ACPI_EVENT_POWER_BUTTON;
137 * Install the new handler. We could remove any fixed handlers added
138 * from the FADT once we have a duplicate from the AML but some systems
139 * only return events on one or the other so we have to keep both.
142 AcpiClearEvent(event);
143 status = AcpiInstallFixedEventHandler(event,
144 acpi_button_fixed_handler, sc);
147 * If a system does not get lid events, it may make sense to change
148 * the type to ACPI_ALL_NOTIFY. Some systems generate both a wake
149 * and runtime notify in that case though.
151 status = AcpiInstallNotifyHandler(sc->button_handle,
152 ACPI_DEVICE_NOTIFY, acpi_button_notify_handler, sc);
154 if (ACPI_FAILURE(status)) {
155 device_printf(sc->button_dev, "couldn't install notify handler - %s\n",
156 AcpiFormatException(status));
157 return_VALUE (ENXIO);
160 /* Enable the GPE for wake/runtime. */
161 acpi_wake_init(dev, ACPI_GPE_TYPE_WAKE_RUN);
162 acpi_wake_set_enable(dev, 1);
168 acpi_button_suspend(device_t dev)
170 struct acpi_softc *acpi_sc;
172 acpi_sc = acpi_device_get_parent_softc(dev);
173 acpi_wake_sleep_prep(dev, acpi_sc->acpi_sstate);
178 acpi_button_resume(device_t dev)
180 acpi_wake_run_prep(dev);
185 acpi_button_notify_sleep(void *arg)
187 struct acpi_button_softc *sc;
188 struct acpi_softc *acpi_sc;
190 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
192 sc = (struct acpi_button_softc *)arg;
193 acpi_sc = acpi_device_get_parent_softc(sc->button_dev);
197 acpi_UserNotify("Button", sc->button_handle, sc->button_type);
199 switch (sc->button_type) {
200 case ACPI_POWER_BUTTON:
201 ACPI_VPRINT(sc->button_dev, acpi_sc, "power button pressed\n");
202 acpi_event_power_button_sleep(acpi_sc);
204 case ACPI_SLEEP_BUTTON:
205 ACPI_VPRINT(sc->button_dev, acpi_sc, "sleep button pressed\n");
206 acpi_event_sleep_button_sleep(acpi_sc);
209 break; /* unknown button type */
214 acpi_button_notify_wakeup(void *arg)
216 struct acpi_button_softc *sc;
217 struct acpi_softc *acpi_sc;
219 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
221 sc = (struct acpi_button_softc *)arg;
222 acpi_sc = acpi_device_get_parent_softc(sc->button_dev);
226 acpi_UserNotify("Button", sc->button_handle, sc->button_type);
228 switch (sc->button_type) {
229 case ACPI_POWER_BUTTON:
230 ACPI_VPRINT(sc->button_dev, acpi_sc, "wakeup by power button\n");
231 acpi_event_power_button_wake(acpi_sc);
233 case ACPI_SLEEP_BUTTON:
234 ACPI_VPRINT(sc->button_dev, acpi_sc, "wakeup by sleep button\n");
235 acpi_event_sleep_button_wake(acpi_sc);
238 break; /* unknown button type */
243 acpi_button_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
245 struct acpi_button_softc *sc;
247 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
249 sc = (struct acpi_button_softc *)context;
251 case ACPI_NOTIFY_BUTTON_PRESSED_FOR_SLEEP:
252 AcpiOsQueueForExecution(OSD_PRIORITY_LO,
253 acpi_button_notify_sleep, sc);
255 case ACPI_NOTIFY_BUTTON_PRESSED_FOR_WAKEUP:
256 AcpiOsQueueForExecution(OSD_PRIORITY_LO,
257 acpi_button_notify_wakeup, sc);
260 device_printf(sc->button_dev, "unknown notify %#x\n", notify);
266 acpi_button_fixed_handler(void *context)
268 struct acpi_button_softc *sc = (struct acpi_button_softc *)context;
270 ACPI_FUNCTION_TRACE_PTR((char *)(uintptr_t)__func__, context);
273 return_ACPI_STATUS (AE_BAD_PARAMETER);
275 acpi_button_notify_handler(sc->button_handle,
276 ACPI_NOTIFY_BUTTON_PRESSED_FOR_SLEEP, sc);
277 return_ACPI_STATUS (AE_OK);