Merge branch 'master' of git+ssh://repo.or.cz/srv/git/acer_acpi
[acer_acpi.git] / wmi-acer.c
blob24b1a436f91bb2075abcb81544f681135527d15d
1 /*
2 * WMI to ACPI mapping driver (acer_acpi variant)
4 * Copyright (C) 2007 Carlos Corbacho <carlos@strangeworlds.co.uk>
6 * GUID parsing code from ldm.c is:
7 * Copyright (C) 2001,2002 Richard Russon <ldm@flatcap.org>
8 * Copyright (c) 2001-2007 Anton Altaparmakov
9 * Copyright (C) 2001,2002 Jakob Kemi <jakob.kemi@telia.com>
11 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or (at
16 * your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/version.h>
33 #include <linux/init.h>
34 #include <linux/types.h>
35 #include <linux/list.h>
37 #include <acpi/acpi_bus.h>
38 #include <acpi/acpi_drivers.h>
40 #include "wmi-acer.h"
42 #define ACPI_WMI_CLASS "wmi-acer"
44 #undef PREFIX
45 #define PREFIX "ACPI: WMI-Acer: "
46 #define WMI_ERR KERN_ERR PREFIX
47 #define WMI_NOTICE KERN_NOTICE PREFIX
48 #define WMI_INFO KERN_INFO PREFIX
50 #define DEBUG(level, message...) { \
51 if (debug >= level) \
52 printk(KERN_DEBUG PREFIX message);\
55 /* Workaround needed for older kernels */
56 #ifndef bool
57 #define bool int
58 #define true 1
59 #define false 0
60 #endif
62 MODULE_AUTHOR("Carlos Corbacho");
63 MODULE_DESCRIPTION("WMI ACPI Interface Driver - acer_acpi variant");
64 MODULE_LICENSE("GPL");
66 static DEFINE_MUTEX(wmi_data_lock);
68 struct guid_block
70 char guid[16];
71 union
73 char object_id[2];
74 struct
76 unsigned char notify_id;
77 unsigned char reserved;
80 u8 instance_count;
81 u8 flags;
84 struct wmi_block
86 struct list_head list;
87 struct guid_block gblock;
88 acpi_handle handle;
91 static struct wmi_block wmi_blocks;
93 static wmi_notify_handler wmi_external_handler;
94 static void *wmi_external_data;
96 static int debug;
98 module_param(debug, int, 0664);
99 MODULE_PARM_DESC(debug, "Debugging verbosity level (0=least 2=most)");
102 * If the GUID data block is marked as expensive, we must enable and
103 * explicitily disable data collection.
105 #define ACPI_WMI_EXPENSIVE 0x1
106 #define ACPI_WMI_METHOD 0x2 /* GUID is a method */
107 #define ACPI_WMI_STRING 0x4 /* GUID takes & returns a string */
108 #define ACPI_WMI_EVENT 0x8 /* GUID is an event */
110 static int acpi_wmi_remove(struct acpi_device *device, int type);
111 static int acpi_wmi_add(struct acpi_device *device);
113 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
114 const static struct acpi_device_id wmi_device_ids[] = {
115 {"PNP0C14", 0},
116 {"pnp0c14", 0},
117 {"", 0},
119 MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
120 #endif
122 static struct acpi_driver acpi_wmi_driver = {
123 .name = "wmi-acer",
124 .class = ACPI_WMI_CLASS,
125 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
126 .ids = wmi_device_ids,
127 #else
128 .ids = "PNP0C14,pnp0c14",
129 #endif
130 .ops = {
131 .add = acpi_wmi_add,
132 .remove = acpi_wmi_remove,
137 * GUID parsing functions
141 * wmi_parse_hexbyte - Convert a ASCII hex number to a byte
142 * @src: Pointer to at least 2 characters to convert.
144 * Convert a two character ASCII hex string to a number.
146 * Return: 0-255 Success, the byte was parsed correctly
147 * -1 Error, an invalid character was supplied
149 static int wmi_parse_hexbyte(const u8 *src)
151 unsigned int x; /* For correct wrapping */
152 int h;
154 /* high part */
155 x = src[0];
156 if (x - '0' <= '9' - '0') {
157 h = x - '0';
158 } else if (x - 'a' <= 'f' - 'a') {
159 h = x - 'a' + 10;
160 } else if (x - 'A' <= 'F' - 'A') {
161 h = x - 'A' + 10;
162 } else {
163 return -1;
165 h <<= 4;
167 /* low part */
168 x = src[1];
169 if (x - '0' <= '9' - '0')
170 return h | (x - '0');
171 if (x - 'a' <= 'f' - 'a')
172 return h | (x - 'a' + 10);
173 if (x - 'A' <= 'F' - 'A')
174 return h | (x - 'A' + 10);
175 return -1;
179 * wmi_swap_bytes - Rearrange GUID bytes to match GUID binary
180 * @src: Memory block holding binary GUID (16 bytes)
181 * @dest: Memory block to hold byte swapped binary GUID (16 bytes)
183 * Byte swap a binary GUID to match it's real GUID value
185 static void wmi_swap_bytes(u8 *src, u8 *dest)
187 int i;
189 for (i = 0; i <= 3; i++)
190 memcpy(dest + i, src + (3 - i), 1);
192 for (i = 0; i <= 1; i++)
193 memcpy(dest + 4 + i, src + (5 - i), 1);
195 for (i = 0; i <= 1; i++)
196 memcpy(dest + 6 + i, src + (7 - i), 1);
198 memcpy(dest + 8, src + 8, 8);
202 * wmi_parse_guid - Convert GUID from ASCII to binary
203 * @src: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
204 * @dest: Memory block to hold binary GUID (16 bytes)
206 * N.B. The GUID need not be NULL terminated.
208 * Return: 'true' @dest contains binary GUID
209 * 'false' @dest contents are undefined
211 static bool wmi_parse_guid(const u8 *src, u8 *dest)
213 static const int size[] = { 4, 2, 2, 2, 6 };
214 int i, j, v;
216 if (src[8] != '-' || src[13] != '-' ||
217 src[18] != '-' || src[23] != '-')
218 return false;
220 for (j = 0; j < 5; j++, src++) {
221 for (i = 0; i < size[j]; i++, src += 2, *dest++ = v) {
222 v = wmi_parse_hexbyte(src);
223 if (v < 0)
224 return false;
228 return true;
231 static bool find_guid(const char *guid_string, struct wmi_block **out)
233 char tmp[16], guid_input[16];
234 struct wmi_block *wblock;
235 struct guid_block *block;
236 struct list_head *p;
238 DEBUG(2, "find_guid called\n");
239 DEBUG(2, "Passing GUID to parser\n");
240 wmi_parse_guid(guid_string, tmp);
241 wmi_swap_bytes(tmp, guid_input);
243 list_for_each(p, &wmi_blocks.list) {
244 wblock = list_entry(p, struct wmi_block, list);
245 block = &wblock->gblock;
247 if (memcmp(block->guid, guid_input, 16) == 0) {
248 DEBUG(2, "GUID found - returning block\n");
249 if (out)
250 *out = wblock;
251 return 1;
254 return 0;
258 * Exported WMI functions
261 * wmi_acer_evaluate_method - Evaluate a WMI method
262 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
263 * @instance: Instance index
264 * @method_id: Method ID to call
265 * &in: Buffer containing input for the method call
266 * &out: Empty buffer to return the method results
268 * Convert a WMI method call to an ACPI one, and return the results
270 acpi_status wmi_acer_evaluate_method(const char *guid_string, u8 instance,
271 u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
273 struct guid_block *block = NULL;
274 struct wmi_block *wblock = NULL;
275 acpi_handle handle;
276 acpi_status status;
277 struct acpi_object_list input;
278 union acpi_object params[3];
279 char method[4] = "WM";
281 if (!find_guid(guid_string, &wblock))
282 return AE_BAD_ADDRESS;
284 block = &wblock->gblock;
285 handle = wblock->handle;
287 if (!block->flags & ACPI_WMI_METHOD) {
288 DEBUG(2, "GUID is not a method\n");
289 return AE_BAD_DATA;
292 if (block->instance_count < instance)
293 return AE_BAD_PARAMETER;
295 input.count = 2;
296 input.pointer = params;
297 params[0].type = ACPI_TYPE_INTEGER;
298 params[0].integer.value = instance;
299 params[1].type = ACPI_TYPE_INTEGER;
300 params[1].integer.value = method_id;
302 if (in != NULL) {
303 input.count = 3;
305 if (block->flags & ACPI_WMI_STRING) {
306 params[2].type = ACPI_TYPE_STRING;
307 } else {
308 params[2].type = ACPI_TYPE_BUFFER;
310 params[2].buffer.length = in->length;
311 params[2].buffer.pointer = in->pointer;
314 strncat(method, block->object_id, 2);
315 DEBUG(2, "Object to call is %s\n", method);
317 status = acpi_evaluate_object(handle, method, &input, out);
319 return status;
321 EXPORT_SYMBOL_GPL(wmi_acer_evaluate_method);
324 * wmi_query_block - Return contents of a WMI block
325 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
326 * @instance: Instance index
327 * &out: Empty buffer to return the contents of the data block to
329 * Return the contents of a data block to a buffer
331 acpi_status wmi_acer_query_block(const char *guid_string, u8 instance,
332 struct acpi_buffer *out)
334 struct guid_block *block = NULL;
335 struct wmi_block *wblock = NULL;
336 acpi_handle handle;
337 acpi_status status;
338 struct acpi_object_list input, wc_input;
339 union acpi_object wc_params[1], wq_params[1];
340 char method[4] = "WQ";
341 char wc_method[4] = "WC";
343 if (guid_string == NULL || out == NULL)
344 return AE_BAD_PARAMETER;
346 if (!find_guid(guid_string, &wblock))
347 return AE_BAD_ADDRESS;
349 block = &wblock->gblock;
350 handle = wblock->handle;
352 if (block->instance_count < instance)
353 return AE_BAD_PARAMETER;
355 /* Check GUID is a data block */
356 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
357 return AE_BAD_ADDRESS;
359 input.count = 1;
360 input.pointer = wq_params;
361 wq_params[0].type = ACPI_TYPE_INTEGER;
362 wq_params[0].integer.value = instance;
365 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
366 * enable collection
368 if (block->flags & ACPI_WMI_EXPENSIVE) {
369 wc_input.count = 1;
370 wc_input.pointer = wc_params;
371 wc_params[0].type = ACPI_TYPE_INTEGER;
372 wc_params[0].integer.value = 1;
374 strncat(wc_method, block->object_id, 2);
376 status = acpi_evaluate_object(handle, wc_method,
377 &wc_input, NULL);
379 if (ACPI_FAILURE(status))
380 return AE_ERROR;
383 strncat(method, block->object_id, 2);
384 DEBUG(2, "Object to call is %s\n", method);
386 status = acpi_evaluate_object(handle, method, NULL, out);
389 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
390 * the WQxx method failed - we should disable collection anyway
392 if (block->flags & ACPI_WMI_EXPENSIVE) {
393 wc_params[0].integer.value = 0;
394 status = acpi_evaluate_object(handle,
395 wc_method, &wc_input, NULL);
398 return status;
400 EXPORT_SYMBOL_GPL(wmi_acer_query_block);
403 * wmi_acer_set_block - Write to a WMI block
404 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
405 * @instance: Instance index
406 * &in: Buffer containing new values for the data block
408 * Write the contents of the input buffer to ACPI
410 acpi_status wmi_acer_set_block(const char *guid_string, u8 instance,
411 const struct acpi_buffer *in)
413 struct guid_block *block = NULL;
414 struct wmi_block *wblock = NULL;
415 acpi_handle handle;
416 struct acpi_object_list input;
417 union acpi_object params[2];
418 char method[4] = "WS";
420 if (guid_string == NULL || in == NULL)
421 return AE_BAD_DATA;
423 if (!find_guid(guid_string, &wblock))
424 return AE_BAD_ADDRESS;
426 block = &wblock->gblock;
427 handle = wblock->handle;
429 if (block->instance_count < instance)
430 return AE_BAD_PARAMETER;
432 /* Check GUID is a data block */
433 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
434 return AE_BAD_ADDRESS;
436 input.count = 2;
437 input.pointer = params;
438 params[0].type = ACPI_TYPE_INTEGER;
439 params[0].integer.value = instance;
441 if (block->flags & ACPI_WMI_STRING) {
442 params[1].type = ACPI_TYPE_STRING;
443 } else {
444 params[1].type = ACPI_TYPE_BUFFER;
446 params[1].buffer.length = in->length;
447 params[1].buffer.pointer = in->pointer;
449 strncat(method, block->object_id, 2);
450 DEBUG(2, "Object to call is %s\n", method);
452 return acpi_evaluate_object(handle, method, &input, NULL);
454 EXPORT_SYMBOL_GPL(wmi_acer_set_block);
457 * wmi_install_notify_handler - Register handler for WMI events
458 * @handler: Function to handle notifications
459 * @data: Data to be returned to handler when event is fired
461 * Register a handler for events sent to the WMI-ACPI mapper device.
463 acpi_status wmi_acer_install_notify_handler(wmi_notify_handler handler, void *data)
465 if (!handler)
466 return AE_BAD_PARAMETER;
468 if (!wmi_external_handler)
469 return AE_ALREADY_ACQUIRED;
471 wmi_external_handler = handler;
472 wmi_external_data = data;
474 return AE_OK;
476 EXPORT_SYMBOL_GPL(wmi_acer_install_notify_handler);
479 * wmi_acer_uninstall_notify_handler - Unregister handler for WMI events
481 * Unregister handler for events sent to the WMI-ACPI mapper device.
483 acpi_status wmi_acer_remove_notify_handler(void)
485 DEBUG(1, "Notifier triggered\n");
487 if (wmi_external_handler) {
488 wmi_external_handler = NULL;
489 wmi_external_data = NULL;
490 return AE_OK;
492 return AE_ERROR;
494 EXPORT_SYMBOL_GPL(wmi_acer_remove_notify_handler);
497 * wmi_acer_get_event_data - Get WMI data associated with an event
499 * @event - Event to find
500 * &out - Buffer to hold event data
502 * Returns extra data associated with an event in WMI.
504 acpi_status wmi_acer_get_event_data(u32 event, struct acpi_buffer *out)
506 struct acpi_object_list input;
507 union acpi_object params[1];
508 struct guid_block *gblock;
509 struct wmi_block *wblock;
510 struct list_head *p;
512 input.count = 1;
513 input.pointer = params;
514 params[0].type = ACPI_TYPE_INTEGER;
515 params[0].integer.value = event;
517 list_for_each(p, &wmi_blocks.list) {
518 wblock = list_entry(p, struct wmi_block, list);
519 gblock = &wblock->gblock;
521 if ((gblock->flags & ACPI_WMI_EVENT) &&
522 (gblock->notify_id == event))
523 return acpi_evaluate_object(wblock->handle, "_WED",
524 &input, out);
527 return AE_NOT_FOUND;
529 EXPORT_SYMBOL(wmi_acer_get_event_data);
532 * wmi_acer_has_guid - Check if a GUID is available
533 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
535 * Check if a given GUID is defined by _WDG
537 bool wmi_acer_has_guid(const char *guid_string)
539 return find_guid(guid_string, NULL);
541 EXPORT_SYMBOL_GPL(wmi_acer_has_guid);
544 * parse_wdg - Parse the _WDG method for the GUID data blocks
546 static __init acpi_status parse_wdg(acpi_handle handle)
548 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
549 union acpi_object *obj;
550 struct guid_block *gblock;
551 struct wmi_block *wblock;
552 acpi_status status;
553 u32 i, total;
555 status = acpi_evaluate_object(handle, "_WDG", NULL, &out);
557 if (ACPI_FAILURE(status))
558 return status;
560 obj = (union acpi_object *) out.pointer;
562 if (obj->type != ACPI_TYPE_BUFFER)
563 return AE_ERROR;
565 total = obj->buffer.length / sizeof(struct guid_block);
567 gblock = kzalloc(obj->buffer.length, GFP_KERNEL);
568 if (!gblock)
569 return AE_NO_MEMORY;
571 memcpy(gblock, obj->buffer.pointer, obj->buffer.length);
573 for (i = 0; i < total; i++) {
574 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
575 if (!wblock)
576 return AE_NO_MEMORY;
578 wblock->gblock = gblock[i];
579 wblock->handle = handle;
580 list_add_tail(&wblock->list, &wmi_blocks.list);
583 kfree(out.pointer);
584 kfree(gblock);
586 return status;
589 static void acpi_wmi_notify(acpi_handle handle, u32 event, void *data)
591 struct guid_block *block;
592 struct wmi_block *wblock;
593 struct list_head *p;
595 list_for_each(p, &wmi_blocks.list) {
596 wblock = list_entry(p, struct wmi_block, list);
597 block = &wblock->gblock;
599 if ((block->flags & ACPI_WMI_EVENT) &&
600 block->notify_id == event) {
601 if (wmi_external_handler)
602 wmi_external_handler(event, wmi_external_data);
603 break;
608 static int __init acpi_wmi_add(struct acpi_device *device)
610 acpi_status status;
611 int result = 0;
613 status = acpi_install_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
614 acpi_wmi_notify, device);
616 if (ACPI_FAILURE(status)) {
617 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
618 "Error installing notify handler\n"));
619 return -ENODEV;
622 status = parse_wdg(device->handle);
623 if (ACPI_FAILURE(status))
624 return -ENODEV;
626 return result;
629 static int acpi_wmi_remove(struct acpi_device *device, int type)
631 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
632 acpi_wmi_notify);
634 return 0;
637 static int __init acpi_wmi_init(void)
639 acpi_status result;
641 if (acpi_disabled)
642 return -ENODEV;
644 INIT_LIST_HEAD(&wmi_blocks.list);
646 result = acpi_bus_register_driver(&acpi_wmi_driver);
648 if (ACPI_FAILURE(result))
649 printk(KERN_INFO PREFIX "Interface device not found\n");
651 printk(KERN_INFO PREFIX "Mapper loaded\n");
653 return result;
656 static void __exit acpi_wmi_exit(void)
658 struct list_head *p, *tmp;
659 struct wmi_block *wblock;
661 acpi_bus_unregister_driver(&acpi_wmi_driver);
663 list_for_each_safe(p, tmp, &wmi_blocks.list) {
664 wblock = list_entry(p, struct wmi_block, list);
666 list_del(p);
667 kfree(wblock);
670 printk(KERN_INFO PREFIX "Mapper unloaded\n");
673 module_init(acpi_wmi_init);
674 module_exit(acpi_wmi_exit);