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