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