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