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