acer_acpi - Bump version to 0.11
[acer_acpi.git] / wmi-acer.c
blob3b9e7b0ef57d7611dc886962494894249054e834
1 /*
2 * ACPI-WMI mapping driver (acer_acpi port)
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("ACPI-WMI Mapping Driver - acer_acpi port");
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, wc_status = AE_ERROR;
338 struct acpi_object_list input, wc_input;
339 union acpi_object wc_params[1], wq_params[1];
340 char method[4];
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);
377 * Some GUIDs break the specification by declaring themselves
378 * expensive, but have no corresponding WCxx method. So we
379 * should not fail if this happens.
381 wc_status = acpi_evaluate_object(handle, wc_method,
382 &wc_input, NULL);
385 strcpy(method, "WQ");
386 strncat(method, block->object_id, 2);
387 DEBUG(2, "Object to call is %s\n", method);
389 status = acpi_evaluate_object(handle, method, NULL, out);
392 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
393 * the WQxx method failed - we should disable collection anyway
395 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
396 wc_params[0].integer.value = 0;
397 status = acpi_evaluate_object(handle,
398 wc_method, &wc_input, NULL);
401 return status;
403 EXPORT_SYMBOL_GPL(wmi_acer_query_block);
406 * wmi_acer_set_block - Write to a WMI block
407 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
408 * @instance: Instance index
409 * &in: Buffer containing new values for the data block
411 * Write the contents of the input buffer to ACPI
413 acpi_status wmi_acer_set_block(const char *guid_string, u8 instance,
414 const struct acpi_buffer *in)
416 struct guid_block *block = NULL;
417 struct wmi_block *wblock = NULL;
418 acpi_handle handle;
419 struct acpi_object_list input;
420 union acpi_object params[2];
421 char method[4] = "WS";
423 if (guid_string == NULL || in == NULL)
424 return AE_BAD_DATA;
426 if (!find_guid(guid_string, &wblock))
427 return AE_BAD_ADDRESS;
429 block = &wblock->gblock;
430 handle = wblock->handle;
432 if (block->instance_count < instance)
433 return AE_BAD_PARAMETER;
435 /* Check GUID is a data block */
436 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
437 return AE_BAD_ADDRESS;
439 input.count = 2;
440 input.pointer = params;
441 params[0].type = ACPI_TYPE_INTEGER;
442 params[0].integer.value = instance;
444 if (block->flags & ACPI_WMI_STRING) {
445 params[1].type = ACPI_TYPE_STRING;
446 } else {
447 params[1].type = ACPI_TYPE_BUFFER;
449 params[1].buffer.length = in->length;
450 params[1].buffer.pointer = in->pointer;
452 strncat(method, block->object_id, 2);
453 DEBUG(2, "Object to call is %s\n", method);
455 return acpi_evaluate_object(handle, method, &input, NULL);
457 EXPORT_SYMBOL_GPL(wmi_acer_set_block);
460 * wmi_install_notify_handler - Register handler for WMI events
461 * @handler: Function to handle notifications
462 * @data: Data to be returned to handler when event is fired
464 * Register a handler for events sent to the WMI-ACPI mapper device.
466 acpi_status wmi_acer_install_notify_handler(wmi_notify_handler handler, void *data)
468 if (!handler)
469 return AE_BAD_PARAMETER;
471 if (!wmi_external_handler)
472 return AE_ALREADY_ACQUIRED;
474 wmi_external_handler = handler;
475 wmi_external_data = data;
477 return AE_OK;
479 EXPORT_SYMBOL_GPL(wmi_acer_install_notify_handler);
482 * wmi_acer_uninstall_notify_handler - Unregister handler for WMI events
484 * Unregister handler for events sent to the WMI-ACPI mapper device.
486 acpi_status wmi_acer_remove_notify_handler(void)
488 DEBUG(1, "Notifier triggered\n");
490 if (wmi_external_handler) {
491 wmi_external_handler = NULL;
492 wmi_external_data = NULL;
493 return AE_OK;
495 return AE_ERROR;
497 EXPORT_SYMBOL_GPL(wmi_acer_remove_notify_handler);
500 * wmi_acer_get_event_data - Get WMI data associated with an event
502 * @event - Event to find
503 * &out - Buffer to hold event data
505 * Returns extra data associated with an event in WMI.
507 acpi_status wmi_acer_get_event_data(u32 event, struct acpi_buffer *out)
509 struct acpi_object_list input;
510 union acpi_object params[1];
511 struct guid_block *gblock;
512 struct wmi_block *wblock;
513 struct list_head *p;
515 input.count = 1;
516 input.pointer = params;
517 params[0].type = ACPI_TYPE_INTEGER;
518 params[0].integer.value = event;
520 list_for_each(p, &wmi_blocks.list) {
521 wblock = list_entry(p, struct wmi_block, list);
522 gblock = &wblock->gblock;
524 if ((gblock->flags & ACPI_WMI_EVENT) &&
525 (gblock->notify_id == event))
526 return acpi_evaluate_object(wblock->handle, "_WED",
527 &input, out);
530 return AE_NOT_FOUND;
532 EXPORT_SYMBOL(wmi_acer_get_event_data);
535 * wmi_acer_has_guid - Check if a GUID is available
536 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
538 * Check if a given GUID is defined by _WDG
540 bool wmi_acer_has_guid(const char *guid_string)
542 return find_guid(guid_string, NULL);
544 EXPORT_SYMBOL_GPL(wmi_acer_has_guid);
547 * parse_wdg - Parse the _WDG method for the GUID data blocks
549 static __init acpi_status parse_wdg(acpi_handle handle)
551 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
552 union acpi_object *obj;
553 struct guid_block *gblock;
554 struct wmi_block *wblock;
555 acpi_status status;
556 u32 i, total;
558 status = acpi_evaluate_object(handle, "_WDG", NULL, &out);
560 if (ACPI_FAILURE(status))
561 return status;
563 obj = (union acpi_object *) out.pointer;
565 if (obj->type != ACPI_TYPE_BUFFER)
566 return AE_ERROR;
568 total = obj->buffer.length / sizeof(struct guid_block);
570 gblock = kzalloc(obj->buffer.length, GFP_KERNEL);
571 if (!gblock)
572 return AE_NO_MEMORY;
574 memcpy(gblock, obj->buffer.pointer, obj->buffer.length);
576 for (i = 0; i < total; i++) {
577 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
578 if (!wblock)
579 return AE_NO_MEMORY;
581 wblock->gblock = gblock[i];
582 wblock->handle = handle;
583 list_add_tail(&wblock->list, &wmi_blocks.list);
586 kfree(out.pointer);
587 kfree(gblock);
589 return status;
593 * WMI can have EmbeddedControl access regions. In which case, we just want to
594 * hand these off to the EC driver.
596 static acpi_status
597 acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
598 u32 bits, acpi_integer *value,
599 void *handler_context, void *region_context)
601 int result = 0, i = 0;
602 u8 temp = 0;
604 if ((address > 0xFF) || !value)
605 return AE_BAD_PARAMETER;
607 if (function != ACPI_READ && function != ACPI_WRITE)
608 return AE_BAD_PARAMETER;
610 if (bits != 8)
611 return AE_BAD_PARAMETER;
613 if (function == ACPI_READ) {
614 result = ec_read(address, &temp);
615 (*value) |= ((acpi_integer)temp) << i;
616 } else {
617 temp = 0xff & ((*value) >> i);
618 result = ec_write(address, temp);
621 switch (result) {
622 case -EINVAL:
623 return AE_BAD_PARAMETER;
624 break;
625 case -ENODEV:
626 return AE_NOT_FOUND;
627 break;
628 case -ETIME:
629 return AE_TIME;
630 break;
631 default:
632 return AE_OK;
636 static void acpi_wmi_notify(acpi_handle handle, u32 event, void *data)
638 struct guid_block *block;
639 struct wmi_block *wblock;
640 struct list_head *p;
642 list_for_each(p, &wmi_blocks.list) {
643 wblock = list_entry(p, struct wmi_block, list);
644 block = &wblock->gblock;
646 if ((block->flags & ACPI_WMI_EVENT) &&
647 block->notify_id == event) {
648 if (wmi_external_handler)
649 wmi_external_handler(event, wmi_external_data);
650 break;
655 static int __init acpi_wmi_add(struct acpi_device *device)
657 acpi_status status;
658 int result = 0;
660 status = acpi_install_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
661 acpi_wmi_notify, device);
663 if (ACPI_FAILURE(status)) {
664 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
665 "Error installing notify handler\n"));
666 return -ENODEV;
669 status = acpi_install_address_space_handler(device->handle,
670 ACPI_ADR_SPACE_EC,
671 &acpi_wmi_ec_space_handler,
672 NULL, NULL);
673 if (ACPI_FAILURE(status)) {
674 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
675 "Error installing EC region handler\n"));
676 return -ENODEV;
679 status = parse_wdg(device->handle);
680 if (ACPI_FAILURE(status))
681 return -ENODEV;
683 return result;
686 static int acpi_wmi_remove(struct acpi_device *device, int type)
688 acpi_remove_address_space_handler(device->handle, ACPI_ADR_SPACE_EC,
689 &acpi_wmi_ec_space_handler);
691 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
692 acpi_wmi_notify);
694 return 0;
697 static int __init acpi_wmi_init(void)
699 acpi_status result;
701 if (acpi_disabled)
702 return -ENODEV;
704 INIT_LIST_HEAD(&wmi_blocks.list);
706 result = acpi_bus_register_driver(&acpi_wmi_driver);
708 if (result) {
709 printk(KERN_INFO PREFIX "Interface device not found\n");
710 return result;
713 printk(KERN_INFO PREFIX "Mapper loaded\n");
715 return result;
718 static void __exit acpi_wmi_exit(void)
720 struct list_head *p, *tmp;
721 struct wmi_block *wblock;
723 acpi_bus_unregister_driver(&acpi_wmi_driver);
725 list_for_each_safe(p, tmp, &wmi_blocks.list) {
726 wblock = list_entry(p, struct wmi_block, list);
728 list_del(p);
729 kfree(wblock);
732 printk(KERN_INFO PREFIX "Mapper unloaded\n");
735 module_init(acpi_wmi_init);
736 module_exit(acpi_wmi_exit);