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