Merge from vendor branch LIBSTDC++:
[dragonfly.git] / sys / dev / acpica / 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.13.2.1 2003/08/22 20:49:20 jhb Exp $
29  *      $DragonFly: src/sys/dev/acpica/Attic/acpi_button.c,v 1.1 2003/09/24 03:32:16 drhodus Exp $ 
30  */
31
32 #include "opt_acpi.h"
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36
37 #include "acpi.h"
38
39 #include <dev/acpica/acpivar.h>
40
41 /*
42  * Hooks for the ACPI CA debugging infrastructure
43  */
44 #define _COMPONENT      ACPI_BUTTON
45 ACPI_MODULE_NAME("BUTTON")
46
47 struct acpi_button_softc {
48     device_t    button_dev;
49     ACPI_HANDLE button_handle;
50 #define ACPI_POWER_BUTTON       0
51 #define ACPI_SLEEP_BUTTON       1
52     boolean_t   button_type;    /* Power or Sleep Button */
53 };
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, 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_resume,    acpi_button_resume),
69
70     {0, 0}
71 };
72
73 static driver_t acpi_button_driver = {
74     "acpi_button",
75     acpi_button_methods,
76     sizeof(struct acpi_button_softc),
77 };
78
79 static devclass_t acpi_button_devclass;
80 DRIVER_MODULE(acpi_button, acpi, acpi_button_driver, acpi_button_devclass, 0, 0);
81
82 static int
83 acpi_button_probe(device_t dev)
84 {
85     struct acpi_button_softc    *sc;
86
87     sc = device_get_softc(dev);
88     if (acpi_get_type(dev) == ACPI_TYPE_DEVICE) {
89         if (!acpi_disabled("button")) {
90             if (acpi_MatchHid(dev, "PNP0C0C")) {
91                 device_set_desc(dev, "Power Button");
92                 sc->button_type = ACPI_POWER_BUTTON;
93                 return(0);
94             }
95             if (acpi_MatchHid(dev, "PNP0C0E")) {
96                 device_set_desc(dev, "Sleep Button");
97                 sc->button_type = ACPI_SLEEP_BUTTON;
98                 return(0);
99             }
100         }
101     }
102     return(ENXIO);
103 }
104
105 static int
106 acpi_button_attach(device_t dev)
107 {
108     struct acpi_button_softc    *sc;
109     ACPI_STATUS                 status;
110
111     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
112
113     sc = device_get_softc(dev);
114     sc->button_dev = dev;
115     sc->button_handle = acpi_get_handle(dev);
116
117     if (ACPI_FAILURE(status = AcpiInstallNotifyHandler(sc->button_handle, ACPI_DEVICE_NOTIFY, 
118                                            acpi_button_notify_handler, sc))) {
119         device_printf(sc->button_dev, "couldn't install Notify handler - %s\n", AcpiFormatException(status));
120         return_VALUE(ENXIO);
121     }
122     acpi_device_enable_wake_capability(sc->button_handle, 1);
123     return_VALUE(0);
124 }
125
126 static int
127 acpi_button_suspend(device_t dev)
128 {
129     struct acpi_button_softc    *sc;
130
131     sc = device_get_softc(dev);
132     acpi_device_enable_wake_event(sc->button_handle);
133     return (0);
134 }
135
136 static int
137 acpi_button_resume(device_t dev)
138 {
139
140     return (0);
141 }
142
143 static void
144 acpi_button_notify_pressed_for_sleep(void *arg)
145 {
146     struct acpi_button_softc    *sc;
147     struct acpi_softc           *acpi_sc;
148
149     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
150
151     sc = (struct acpi_button_softc *)arg;
152     acpi_sc = acpi_device_get_parent_softc(sc->button_dev);
153     if (acpi_sc == NULL) {
154         return_VOID;
155     }
156
157     switch (sc->button_type) {
158     case ACPI_POWER_BUTTON:
159         ACPI_VPRINT(sc->button_dev, acpi_sc, "power button pressed\n");
160         acpi_eventhandler_power_button_for_sleep((void *)acpi_sc);
161         break;
162     case ACPI_SLEEP_BUTTON:
163         ACPI_VPRINT(sc->button_dev, acpi_sc, "sleep button pressed\n");
164         acpi_eventhandler_sleep_button_for_sleep((void *)acpi_sc);
165         break;
166     default:
167         break;          /* unknown button type */
168     }
169     return_VOID;
170 }
171
172 static void
173 acpi_button_notify_pressed_for_wakeup(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
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     return_VOID;
199 }
200
201 /* XXX maybe not here */
202 #define ACPI_NOTIFY_BUTTON_PRESSED_FOR_SLEEP    0x80
203 #define ACPI_NOTIFY_BUTTON_PRESSED_FOR_WAKEUP   0x02
204
205 static void 
206 acpi_button_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
207 {
208     struct acpi_button_softc    *sc = (struct acpi_button_softc *)context;
209
210     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
211
212     switch (notify) {
213     case ACPI_NOTIFY_BUTTON_PRESSED_FOR_SLEEP:
214         AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_button_notify_pressed_for_sleep, sc);
215         break;   
216     case ACPI_NOTIFY_BUTTON_PRESSED_FOR_WAKEUP:
217         AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_button_notify_pressed_for_wakeup, sc);
218         break;   
219     default:
220         break;          /* unknown notification value */
221     }
222     return_VOID;
223 }
224
225