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