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