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.27 2004/06/13 22:52:30 njl Exp $
29 * $DragonFly: src/sys/dev/acpica5/acpi_button.c,v 1.5 2007/10/23 03:04:48 y0netan1 Exp $
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
40 #include <dev/acpica5/acpivar.h>
42 /* Hooks for the ACPI CA debugging infrastructure */
43 #define _COMPONENT ACPI_BUTTON
44 ACPI_MODULE_NAME("BUTTON")
46 struct acpi_button_softc {
48 ACPI_HANDLE button_handle;
49 boolean_t button_type;
50 #define ACPI_POWER_BUTTON 0
51 #define ACPI_SLEEP_BUTTON 1
55 #define ACPI_NOTIFY_BUTTON_PRESSED_FOR_SLEEP 0x80
56 #define ACPI_NOTIFY_BUTTON_PRESSED_FOR_WAKEUP 0x02
58 static int acpi_button_probe(device_t dev);
59 static int acpi_button_attach(device_t dev);
60 static int acpi_button_suspend(device_t dev);
61 static int acpi_button_resume(device_t dev);
62 static void acpi_button_notify_handler(ACPI_HANDLE h, UINT32 notify,
65 acpi_button_fixed_handler(void *context);
66 static void acpi_button_notify_sleep(void *arg);
67 static void acpi_button_notify_wakeup(void *arg);
69 static device_method_t acpi_button_methods[] = {
70 /* Device interface */
71 DEVMETHOD(device_probe, acpi_button_probe),
72 DEVMETHOD(device_attach, acpi_button_attach),
73 DEVMETHOD(device_suspend, acpi_button_suspend),
74 DEVMETHOD(device_shutdown, acpi_button_suspend),
75 DEVMETHOD(device_resume, acpi_button_resume),
80 static driver_t acpi_button_driver = {
83 sizeof(struct acpi_button_softc),
86 static devclass_t acpi_button_devclass;
87 DRIVER_MODULE(acpi_button, acpi, acpi_button_driver, acpi_button_devclass,
89 MODULE_DEPEND(acpi_button, acpi, 1, 1, 1);
92 acpi_button_probe(device_t dev)
94 struct acpi_button_softc *sc;
98 h = acpi_get_handle(dev);
99 sc = device_get_softc(dev);
100 if (acpi_get_type(dev) == ACPI_TYPE_DEVICE && !acpi_disabled("button")) {
101 if (acpi_MatchHid(h, "PNP0C0C")) {
102 device_set_desc(dev, "Power Button");
103 sc->button_type = ACPI_POWER_BUTTON;
105 } else if (acpi_MatchHid(h, "ACPI_FPB")) {
106 device_set_desc(dev, "Power Button (fixed)");
107 sc->button_type = ACPI_POWER_BUTTON;
110 } else if (acpi_MatchHid(h, "PNP0C0E")) {
111 device_set_desc(dev, "Sleep Button");
112 sc->button_type = ACPI_SLEEP_BUTTON;
114 } else if (acpi_MatchHid(h, "ACPI_FSB")) {
115 device_set_desc(dev, "Sleep Button (fixed)");
116 sc->button_type = ACPI_SLEEP_BUTTON;
125 acpi_button_attach(device_t dev)
127 struct acpi_button_softc *sc;
131 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
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;
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.
145 AcpiClearEvent(event);
146 status = AcpiInstallFixedEventHandler(event,
147 acpi_button_fixed_handler, sc);
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.
154 status = AcpiInstallNotifyHandler(sc->button_handle,
155 ACPI_DEVICE_NOTIFY, acpi_button_notify_handler, sc);
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);
163 /* Enable the GPE for wake/runtime. */
164 acpi_wake_init(dev, ACPI_GPE_TYPE_WAKE_RUN);
165 acpi_wake_set_enable(dev, 1);
171 acpi_button_suspend(device_t dev)
173 struct acpi_softc *acpi_sc;
175 acpi_sc = acpi_device_get_parent_softc(dev);
176 acpi_wake_sleep_prep(dev, acpi_sc->acpi_sstate);
181 acpi_button_resume(device_t dev)
183 acpi_wake_run_prep(dev);
188 acpi_button_notify_sleep(void *arg)
190 struct acpi_button_softc *sc;
191 struct acpi_softc *acpi_sc;
193 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
195 sc = (struct acpi_button_softc *)arg;
196 acpi_sc = acpi_device_get_parent_softc(sc->button_dev);
200 acpi_UserNotify("Button", sc->button_handle, sc->button_type);
202 switch (sc->button_type) {
203 case ACPI_POWER_BUTTON:
204 ACPI_VPRINT(sc->button_dev, acpi_sc, "power button pressed\n");
205 acpi_event_power_button_sleep(acpi_sc);
207 case ACPI_SLEEP_BUTTON:
208 ACPI_VPRINT(sc->button_dev, acpi_sc, "sleep button pressed\n");
209 acpi_event_sleep_button_sleep(acpi_sc);
212 break; /* unknown button type */
217 acpi_button_notify_wakeup(void *arg)
219 struct acpi_button_softc *sc;
220 struct acpi_softc *acpi_sc;
222 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
224 sc = (struct acpi_button_softc *)arg;
225 acpi_sc = acpi_device_get_parent_softc(sc->button_dev);
229 acpi_UserNotify("Button", sc->button_handle, sc->button_type);
231 switch (sc->button_type) {
232 case ACPI_POWER_BUTTON:
233 ACPI_VPRINT(sc->button_dev, acpi_sc, "wakeup by power button\n");
234 acpi_event_power_button_wake(acpi_sc);
236 case ACPI_SLEEP_BUTTON:
237 ACPI_VPRINT(sc->button_dev, acpi_sc, "wakeup by sleep button\n");
238 acpi_event_sleep_button_wake(acpi_sc);
241 break; /* unknown button type */
246 acpi_button_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
248 struct acpi_button_softc *sc;
250 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
252 sc = (struct acpi_button_softc *)context;
254 case ACPI_NOTIFY_BUTTON_PRESSED_FOR_SLEEP:
255 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_button_notify_sleep, sc);
257 case ACPI_NOTIFY_BUTTON_PRESSED_FOR_WAKEUP:
258 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_button_notify_wakeup, sc);
261 device_printf(sc->button_dev, "unknown notify %#x\n", notify);
267 acpi_button_fixed_handler(void *context)
269 struct acpi_button_softc *sc = (struct acpi_button_softc *)context;
271 ACPI_FUNCTION_TRACE_PTR((char *)(uintptr_t)__func__, context);
274 return_ACPI_STATUS (AE_BAD_PARAMETER);
276 acpi_button_notify_handler(sc->button_handle,
277 ACPI_NOTIFY_BUTTON_PRESSED_FOR_SLEEP, sc);
278 return_ACPI_STATUS (AE_OK);