5e3eb2b5f0fe4a6fc9a978deed76e4de72485b24
[reactos.git] / drivers / bus / acpi / busmgr / button.c
1 /*
2 * acpi_button.c - ACPI Button Driver ($Revision: 29 $)
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
25 #include <ntddk.h>
26
27 #include <acpi.h>
28 #include <acpi_bus.h>
29 #include <acpi_drivers.h>
30 #include <glue.h>
31
32 #define NDEBUG
33 #include <debug.h>
34
35
36
37 #define _COMPONENT ACPI_BUTTON_COMPONENT
38 ACPI_MODULE_NAME ("acpi_button")
39
40
41 static int acpi_button_add (struct acpi_device *device);
42 static int acpi_button_remove (struct acpi_device *device, int type);
43
44 static struct acpi_driver acpi_button_driver = {
45 .name = ACPI_BUTTON_DRIVER_NAME,
46 .class = ACPI_BUTTON_CLASS,
47 .ids = "ACPI_FPB,ACPI_FSB,PNP0C0D,PNP0C0C,PNP0C0E",
48 .ops = {
49 .add = acpi_button_add,
50 .remove = acpi_button_remove,
51 },
52 };
53
54 struct acpi_button {
55 ACPI_HANDLE handle;
56 struct acpi_device *device; /* Fixed button kludge */
57 UINT8 type;
58 unsigned long pushed;
59 };
60 /* --------------------------------------------------------------------------
61 Driver Interface
62 -------------------------------------------------------------------------- */
63
64 void
65 acpi_button_notify (
66 ACPI_HANDLE handle,
67 UINT32 event,
68 void *data)
69 {
70 struct acpi_button *button = (struct acpi_button *) data;
71
72 ACPI_FUNCTION_TRACE("acpi_button_notify");
73
74 if (!button || !button->device)
75 return_VOID;
76
77 switch (event) {
78 case ACPI_BUTTON_NOTIFY_STATUS:
79 acpi_bus_generate_event(button->device, event, ++button->pushed);
80 break;
81 default:
82 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
83 "Unsupported event [0x%x]\n", event));
84 break;
85 }
86
87 return_VOID;
88 }
89
90
91 ACPI_STATUS
92 acpi_button_notify_fixed (
93 void *data)
94 {
95 struct acpi_button *button = (struct acpi_button *) data;
96
97 ACPI_FUNCTION_TRACE("acpi_button_notify_fixed");
98
99 if (!button)
100 return_ACPI_STATUS(AE_BAD_PARAMETER);
101
102 acpi_button_notify(button->handle, ACPI_BUTTON_NOTIFY_STATUS, button);
103
104 return_ACPI_STATUS(AE_OK);
105 }
106
107
108 static int
109 acpi_button_add (
110 struct acpi_device *device)
111 {
112 int result = 0;
113 ACPI_STATUS status = AE_OK;
114 struct acpi_button *button = NULL;
115
116 static struct acpi_device *power_button;
117 static struct acpi_device *sleep_button;
118 static struct acpi_device *lid_button;
119
120 ACPI_FUNCTION_TRACE("acpi_button_add");
121
122 if (!device)
123 return_VALUE(-1);
124
125 button = ExAllocatePool(NonPagedPool,sizeof(struct acpi_button));
126 if (!button)
127 return_VALUE(-4);
128 memset(button, 0, sizeof(struct acpi_button));
129
130 button->device = device;
131 button->handle = device->handle;
132 acpi_driver_data(device) = button;
133
134 /*
135 * Determine the button type (via hid), as fixed-feature buttons
136 * need to be handled a bit differently than generic-space.
137 */
138 if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
139 button->type = ACPI_BUTTON_TYPE_POWER;
140 sprintf(acpi_device_name(device), "%s",
141 ACPI_BUTTON_DEVICE_NAME_POWER);
142 sprintf(acpi_device_class(device), "%s/%s",
143 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
144 }
145 else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
146 button->type = ACPI_BUTTON_TYPE_POWERF;
147 sprintf(acpi_device_name(device), "%s",
148 ACPI_BUTTON_DEVICE_NAME_POWERF);
149 sprintf(acpi_device_class(device), "%s/%s",
150 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
151 }
152 else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEP)) {
153 button->type = ACPI_BUTTON_TYPE_SLEEP;
154 sprintf(acpi_device_name(device), "%s",
155 ACPI_BUTTON_DEVICE_NAME_SLEEP);
156 sprintf(acpi_device_class(device), "%s/%s",
157 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
158 }
159 else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
160 button->type = ACPI_BUTTON_TYPE_SLEEPF;
161 sprintf(acpi_device_name(device), "%s",
162 ACPI_BUTTON_DEVICE_NAME_SLEEPF);
163 sprintf(acpi_device_class(device), "%s/%s",
164 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
165 }
166 else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_LID)) {
167 button->type = ACPI_BUTTON_TYPE_LID;
168 sprintf(acpi_device_name(device), "%s",
169 ACPI_BUTTON_DEVICE_NAME_LID);
170 sprintf(acpi_device_class(device), "%s/%s",
171 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
172 }
173 else {
174 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unsupported hid [%s]\n",
175 acpi_device_hid(device)));
176 result = -15;
177 goto end;
178 }
179
180 /*
181 * Ensure only one button of each type is used.
182 */
183 switch (button->type) {
184 case ACPI_BUTTON_TYPE_POWER:
185 case ACPI_BUTTON_TYPE_POWERF:
186 if (!power_button)
187 power_button = device;
188 else {
189 ExFreePool(button);
190 return_VALUE(-15);
191 }
192 break;
193 case ACPI_BUTTON_TYPE_SLEEP:
194 case ACPI_BUTTON_TYPE_SLEEPF:
195 if (!sleep_button)
196 sleep_button = device;
197 else {
198 ExFreePool(button);
199 return_VALUE(-15);
200 }
201 break;
202 case ACPI_BUTTON_TYPE_LID:
203 if (!lid_button)
204 lid_button = device;
205 else {
206 ExFreePool(button);
207 return_VALUE(-15);
208 }
209 break;
210 }
211
212 switch (button->type) {
213 case ACPI_BUTTON_TYPE_POWERF:
214 status = AcpiInstallFixedEventHandler (
215 ACPI_EVENT_POWER_BUTTON,
216 acpi_button_notify_fixed,
217 button);
218 break;
219 case ACPI_BUTTON_TYPE_SLEEPF:
220 status = AcpiInstallFixedEventHandler (
221 ACPI_EVENT_SLEEP_BUTTON,
222 acpi_button_notify_fixed,
223 button);
224 break;
225 case ACPI_BUTTON_TYPE_LID:
226 status = AcpiInstallFixedEventHandler (
227 ACPI_BUTTON_TYPE_LID,
228 acpi_button_notify_fixed,
229 button);
230 break;
231 default:
232 status = AcpiInstallNotifyHandler (
233 button->handle,
234 ACPI_DEVICE_NOTIFY,
235 acpi_button_notify,
236 button);
237 break;
238 }
239
240 if (ACPI_FAILURE(status)) {
241 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
242 "Error installing notify handler\n"));
243 result = -15;
244 goto end;
245 }
246
247 DPRINT("%s [%s]\n",
248 acpi_device_name(device), acpi_device_bid(device));
249
250 end:
251 if (result) {
252 ExFreePool(button);
253 }
254
255 return_VALUE(result);
256 }
257
258
259 static int
260 acpi_button_remove (struct acpi_device *device, int type)
261 {
262 ACPI_STATUS status = 0;
263 struct acpi_button *button = NULL;
264
265 ACPI_FUNCTION_TRACE("acpi_button_remove");
266
267 if (!device || !acpi_driver_data(device))
268 return_VALUE(-1);
269
270 button = acpi_driver_data(device);
271
272 /* Unregister for device notifications. */
273 switch (button->type) {
274 case ACPI_BUTTON_TYPE_POWERF:
275 status = AcpiRemoveFixedEventHandler(
276 ACPI_EVENT_POWER_BUTTON, acpi_button_notify_fixed);
277 break;
278 case ACPI_BUTTON_TYPE_SLEEPF:
279 status = AcpiRemoveFixedEventHandler(
280 ACPI_EVENT_SLEEP_BUTTON, acpi_button_notify_fixed);
281 break;
282 case ACPI_BUTTON_TYPE_LID:
283 status = AcpiRemoveFixedEventHandler(
284 ACPI_BUTTON_TYPE_LID, acpi_button_notify_fixed);
285 break;
286 default:
287 status = AcpiRemoveNotifyHandler(button->handle,
288 ACPI_DEVICE_NOTIFY, acpi_button_notify);
289 break;
290 }
291
292 if (ACPI_FAILURE(status))
293 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
294 "Error removing notify handler\n"));
295
296 ExFreePool(button);
297
298 return_VALUE(0);
299 }
300
301
302 int
303 acpi_button_init (void)
304 {
305 int result = 0;
306
307 ACPI_FUNCTION_TRACE("acpi_button_init");
308
309 result = acpi_bus_register_driver(&acpi_button_driver);
310 if (result < 0) {
311 return_VALUE(-15);
312 }
313
314 return_VALUE(0);
315 }
316
317
318 void
319 acpi_button_exit (void)
320 {
321 ACPI_FUNCTION_TRACE("acpi_button_exit");
322
323 acpi_bus_unregister_driver(&acpi_button_driver);
324
325 return_VOID;
326 }
327
328