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