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