xref: /freebsd/sys/dev/acpica/acpi_button.c (revision b52b9d56d4e96089873a75f9e29062eec19fabba)
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 
38 #include <dev/acpica/acpivar.h>
39 
40 /*
41  * Hooks for the ACPI CA debugging infrastructure
42  */
43 #define _COMPONENT	ACPI_BUTTON
44 ACPI_MODULE_NAME("BUTTON")
45 
46 struct acpi_button_softc {
47     device_t	button_dev;
48     ACPI_HANDLE	button_handle;
49 #define ACPI_POWER_BUTTON	0
50 #define ACPI_SLEEP_BUTTON	1
51     boolean_t	button_type;	/* Power or Sleep Button */
52 };
53 
54 static int	acpi_button_probe(device_t dev);
55 static int	acpi_button_attach(device_t dev);
56 static int	acpi_button_suspend(device_t dev);
57 static int	acpi_button_resume(device_t dev);
58 static void 	acpi_button_notify_handler(ACPI_HANDLE h,UINT32 notify, void *context);
59 static void	acpi_button_notify_pressed_for_sleep(void *arg);
60 static void	acpi_button_notify_pressed_for_wakeup(void *arg);
61 
62 static device_method_t acpi_button_methods[] = {
63     /* Device interface */
64     DEVMETHOD(device_probe,	acpi_button_probe),
65     DEVMETHOD(device_attach,	acpi_button_attach),
66     DEVMETHOD(device_suspend,	acpi_button_suspend),
67     DEVMETHOD(device_resume,	acpi_button_resume),
68 
69     {0, 0}
70 };
71 
72 static driver_t acpi_button_driver = {
73     "acpi_button",
74     acpi_button_methods,
75     sizeof(struct acpi_button_softc),
76 };
77 
78 static devclass_t acpi_button_devclass;
79 DRIVER_MODULE(acpi_button, acpi, acpi_button_driver, acpi_button_devclass, 0, 0);
80 
81 static int
82 acpi_button_probe(device_t dev)
83 {
84     struct acpi_button_softc	*sc;
85 
86     sc = device_get_softc(dev);
87     if (acpi_get_type(dev) == ACPI_TYPE_DEVICE) {
88 	if (!acpi_disabled("button")) {
89 	    if (acpi_MatchHid(dev, "PNP0C0C")) {
90 		device_set_desc(dev, "Power Button");
91 		sc->button_type = ACPI_POWER_BUTTON;
92 		return(0);
93 	    }
94 	    if (acpi_MatchHid(dev, "PNP0C0E")) {
95 		device_set_desc(dev, "Sleep Button");
96 		sc->button_type = ACPI_SLEEP_BUTTON;
97 		return(0);
98 	    }
99 	}
100     }
101     return(ENXIO);
102 }
103 
104 static int
105 acpi_button_attach(device_t dev)
106 {
107     struct acpi_button_softc	*sc;
108     ACPI_STATUS			status;
109 
110     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
111 
112     sc = device_get_softc(dev);
113     sc->button_dev = dev;
114     sc->button_handle = acpi_get_handle(dev);
115 
116     if (ACPI_FAILURE(status = AcpiInstallNotifyHandler(sc->button_handle, ACPI_DEVICE_NOTIFY,
117 					   acpi_button_notify_handler, sc))) {
118 	device_printf(sc->button_dev, "couldn't install Notify handler - %s\n", AcpiFormatException(status));
119 	return_VALUE(ENXIO);
120     }
121     acpi_device_enable_wake_capability(sc->button_handle, 1);
122     return_VALUE(0);
123 }
124 
125 static int
126 acpi_button_suspend(device_t dev)
127 {
128     struct acpi_button_softc	*sc;
129 
130     sc = device_get_softc(dev);
131     acpi_device_enable_wake_event(sc->button_handle);
132     return (0);
133 }
134 
135 static int
136 acpi_button_resume(device_t dev)
137 {
138 
139     return (0);
140 }
141 
142 static void
143 acpi_button_notify_pressed_for_sleep(void *arg)
144 {
145     struct acpi_button_softc	*sc;
146     struct acpi_softc		*acpi_sc;
147 
148     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
149 
150     sc = (struct acpi_button_softc *)arg;
151     acpi_sc = acpi_device_get_parent_softc(sc->button_dev);
152     if (acpi_sc == NULL) {
153 	return_VOID;
154     }
155 
156     switch (sc->button_type) {
157     case ACPI_POWER_BUTTON:
158 	ACPI_VPRINT(sc->button_dev, acpi_sc, "power button pressed\n");
159 	acpi_eventhandler_power_button_for_sleep((void *)acpi_sc);
160 	break;
161     case ACPI_SLEEP_BUTTON:
162 	ACPI_VPRINT(sc->button_dev, acpi_sc, "sleep button pressed\n");
163 	acpi_eventhandler_sleep_button_for_sleep((void *)acpi_sc);
164 	break;
165     default:
166 	break;		/* unknown button type */
167     }
168     return_VOID;
169 }
170 
171 static void
172 acpi_button_notify_pressed_for_wakeup(void *arg)
173 {
174     struct acpi_button_softc	*sc;
175     struct acpi_softc		*acpi_sc;
176 
177     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
178 
179     sc = (struct acpi_button_softc *)arg;
180     acpi_sc = acpi_device_get_parent_softc(sc->button_dev);
181     if (acpi_sc == NULL) {
182 	return_VOID;
183     }
184 
185     switch (sc->button_type) {
186     case ACPI_POWER_BUTTON:
187 	ACPI_VPRINT(sc->button_dev, acpi_sc, "wakeup by power button\n");
188 	acpi_eventhandler_power_button_for_wakeup((void *)acpi_sc);
189 	break;
190     case ACPI_SLEEP_BUTTON:
191 	ACPI_VPRINT(sc->button_dev, acpi_sc, "wakeup by sleep button\n");
192 	acpi_eventhandler_sleep_button_for_wakeup((void *)acpi_sc);
193 	break;
194     default:
195 	break;		/* unknown button type */
196     }
197     return_VOID;
198 }
199 
200 /* XXX maybe not here */
201 #define ACPI_NOTIFY_BUTTON_PRESSED_FOR_SLEEP	0x80
202 #define ACPI_NOTIFY_BUTTON_PRESSED_FOR_WAKEUP	0x02
203 
204 static void
205 acpi_button_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
206 {
207     struct acpi_button_softc	*sc = (struct acpi_button_softc *)context;
208 
209     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
210 
211     switch (notify) {
212     case ACPI_NOTIFY_BUTTON_PRESSED_FOR_SLEEP:
213 	AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_button_notify_pressed_for_sleep, sc);
214 	break;
215     case ACPI_NOTIFY_BUTTON_PRESSED_FOR_WAKEUP:
216 	AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_button_notify_pressed_for_wakeup, sc);
217 	break;
218     default:
219 	break;		/* unknown notification value */
220     }
221     return_VOID;
222 }
223 
224 
225