Update documentation for 0.11.2
[acer_acpi.git] / wmi-acer.c
bloba669deb3279bbc78bffddd771b1e2429515b8776
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 MODULE_AUTHOR("Carlos Corbacho");
56 MODULE_DESCRIPTION("ACPI-WMI Mapping Driver - acer_acpi port");
57 MODULE_LICENSE("GPL");
59 static DEFINE_MUTEX(wmi_data_lock);
61 struct guid_block
63 char guid[16];
64 union
66 char object_id[2];
67 struct
69 unsigned char notify_id;
70 unsigned char reserved;
73 u8 instance_count;
74 u8 flags;
77 struct wmi_block
79 struct list_head list;
80 struct guid_block gblock;
81 acpi_handle handle;
84 static struct wmi_block wmi_blocks;
86 static wmi_notify_handler wmi_external_handler;
87 static void *wmi_external_data;
89 static int debug;
91 module_param(debug, int, 0664);
92 MODULE_PARM_DESC(debug, "Debugging verbosity level (0=least 2=most)");
95 * If the GUID data block is marked as expensive, we must enable and
96 * explicitily disable data collection.
98 #define ACPI_WMI_EXPENSIVE 0x1
99 #define ACPI_WMI_METHOD 0x2 /* GUID is a method */
100 #define ACPI_WMI_STRING 0x4 /* GUID takes & returns a string */
101 #define ACPI_WMI_EVENT 0x8 /* GUID is an event */
103 static int acpi_wmi_remove(struct acpi_device *device, int type);
104 static int acpi_wmi_add(struct acpi_device *device);
106 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)) || \
107 (defined(CONFIG_SUSE_KERNEL) && LINUX_VERSION_CODE == KERNEL_VERSION(2,6,22))
108 const static struct acpi_device_id wmi_device_ids[] = {
109 {"PNP0C14", 0},
110 {"pnp0c14", 0},
111 {"", 0},
113 MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
114 #endif
116 static struct acpi_driver acpi_wmi_driver = {
117 .name = "wmi-acer",
118 .class = ACPI_WMI_CLASS,
119 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)) || \
120 (defined(CONFIG_SUSE_KERNEL) && LINUX_VERSION_CODE == KERNEL_VERSION(2,6,22))
121 .ids = wmi_device_ids,
122 #else
123 .ids = "PNP0C14,pnp0c14",
124 #endif
125 .ops = {
126 .add = acpi_wmi_add,
127 .remove = acpi_wmi_remove,
132 * GUID parsing functions
136 * wmi_parse_hexbyte - Convert a ASCII hex number to a byte
137 * @src: Pointer to at least 2 characters to convert.
139 * Convert a two character ASCII hex string to a number.
141 * Return: 0-255 Success, the byte was parsed correctly
142 * -1 Error, an invalid character was supplied
144 static int wmi_parse_hexbyte(const u8 *src)
146 unsigned int x; /* For correct wrapping */
147 int h;
149 /* high part */
150 x = src[0];
151 if (x - '0' <= '9' - '0') {
152 h = x - '0';
153 } else if (x - 'a' <= 'f' - 'a') {
154 h = x - 'a' + 10;
155 } else if (x - 'A' <= 'F' - 'A') {
156 h = x - 'A' + 10;
157 } else {
158 return -1;
160 h <<= 4;
162 /* low part */
163 x = src[1];
164 if (x - '0' <= '9' - '0')
165 return h | (x - '0');
166 if (x - 'a' <= 'f' - 'a')
167 return h | (x - 'a' + 10);
168 if (x - 'A' <= 'F' - 'A')
169 return h | (x - 'A' + 10);
170 return -1;
174 * wmi_swap_bytes - Rearrange GUID bytes to match GUID binary
175 * @src: Memory block holding binary GUID (16 bytes)
176 * @dest: Memory block to hold byte swapped binary GUID (16 bytes)
178 * Byte swap a binary GUID to match it's real GUID value
180 static void wmi_swap_bytes(u8 *src, u8 *dest)
182 int i;
184 for (i = 0; i <= 3; i++)
185 memcpy(dest + i, src + (3 - i), 1);
187 for (i = 0; i <= 1; i++)
188 memcpy(dest + 4 + i, src + (5 - i), 1);
190 for (i = 0; i <= 1; i++)
191 memcpy(dest + 6 + i, src + (7 - i), 1);
193 memcpy(dest + 8, src + 8, 8);
197 * wmi_parse_guid - Convert GUID from ASCII to binary
198 * @src: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
199 * @dest: Memory block to hold binary GUID (16 bytes)
201 * N.B. The GUID need not be NULL terminated.
203 * Return: 'true' @dest contains binary GUID
204 * 'false' @dest contents are undefined
206 static bool wmi_parse_guid(const u8 *src, u8 *dest)
208 static const int size[] = { 4, 2, 2, 2, 6 };
209 int i, j, v;
211 if (src[8] != '-' || src[13] != '-' ||
212 src[18] != '-' || src[23] != '-')
213 return false;
215 for (j = 0; j < 5; j++, src++) {
216 for (i = 0; i < size[j]; i++, src += 2, *dest++ = v) {
217 v = wmi_parse_hexbyte(src);
218 if (v < 0)
219 return false;
223 return true;
226 static bool find_guid(const char *guid_string, struct wmi_block **out)
228 char tmp[16], guid_input[16];
229 struct wmi_block *wblock;
230 struct guid_block *block;
231 struct list_head *p;
233 DEBUG(2, "find_guid called\n");
234 DEBUG(2, "Passing GUID to parser\n");
235 wmi_parse_guid(guid_string, tmp);
236 wmi_swap_bytes(tmp, guid_input);
238 list_for_each(p, &wmi_blocks.list) {
239 wblock = list_entry(p, struct wmi_block, list);
240 block = &wblock->gblock;
242 if (memcmp(block->guid, guid_input, 16) == 0) {
243 DEBUG(2, "GUID found - returning block\n");
244 if (out)
245 *out = wblock;
246 return 1;
249 return 0;
253 * Exported WMI functions
256 * wmi_acer_evaluate_method - Evaluate a WMI method
257 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
258 * @instance: Instance index
259 * @method_id: Method ID to call
260 * &in: Buffer containing input for the method call
261 * &out: Empty buffer to return the method results
263 * Convert a WMI method call to an ACPI one, and return the results
265 acpi_status wmi_acer_evaluate_method(const char *guid_string, u8 instance,
266 u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
268 struct guid_block *block = NULL;
269 struct wmi_block *wblock = NULL;
270 acpi_handle handle;
271 acpi_status status;
272 struct acpi_object_list input;
273 union acpi_object params[3];
274 char method[4] = "WM";
276 if (!find_guid(guid_string, &wblock))
277 return AE_BAD_ADDRESS;
279 block = &wblock->gblock;
280 handle = wblock->handle;
282 if (!block->flags & ACPI_WMI_METHOD) {
283 DEBUG(2, "GUID is not a method\n");
284 return AE_BAD_DATA;
287 if (block->instance_count < instance)
288 return AE_BAD_PARAMETER;
290 input.count = 2;
291 input.pointer = params;
292 params[0].type = ACPI_TYPE_INTEGER;
293 params[0].integer.value = instance;
294 params[1].type = ACPI_TYPE_INTEGER;
295 params[1].integer.value = method_id;
297 if (in != NULL) {
298 input.count = 3;
300 if (block->flags & ACPI_WMI_STRING) {
301 params[2].type = ACPI_TYPE_STRING;
302 } else {
303 params[2].type = ACPI_TYPE_BUFFER;
305 params[2].buffer.length = in->length;
306 params[2].buffer.pointer = in->pointer;
309 strncat(method, block->object_id, 2);
310 DEBUG(2, "Object to call is %s\n", method);
312 status = acpi_evaluate_object(handle, method, &input, out);
314 return status;
316 EXPORT_SYMBOL_GPL(wmi_acer_evaluate_method);
319 * wmi_query_block - Return contents of a WMI block
320 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
321 * @instance: Instance index
322 * &out: Empty buffer to return the contents of the data block to
324 * Return the contents of a data block to a buffer
326 acpi_status wmi_acer_query_block(const char *guid_string, u8 instance,
327 struct acpi_buffer *out)
329 struct guid_block *block = NULL;
330 struct wmi_block *wblock = NULL;
331 acpi_handle handle;
332 acpi_status status, wc_status = AE_ERROR;
333 struct acpi_object_list input, wc_input;
334 union acpi_object wc_params[1], wq_params[1];
335 char method[4];
336 char wc_method[4] = "WC";
338 if (guid_string == NULL || out == NULL)
339 return AE_BAD_PARAMETER;
341 if (!find_guid(guid_string, &wblock))
342 return AE_BAD_ADDRESS;
344 block = &wblock->gblock;
345 handle = wblock->handle;
347 if (block->instance_count < instance)
348 return AE_BAD_PARAMETER;
350 /* Check GUID is a data block */
351 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
352 return AE_BAD_ADDRESS;
354 input.count = 1;
355 input.pointer = wq_params;
356 wq_params[0].type = ACPI_TYPE_INTEGER;
357 wq_params[0].integer.value = instance;
360 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
361 * enable collection
363 if (block->flags & ACPI_WMI_EXPENSIVE) {
364 wc_input.count = 1;
365 wc_input.pointer = wc_params;
366 wc_params[0].type = ACPI_TYPE_INTEGER;
367 wc_params[0].integer.value = 1;
369 strncat(wc_method, block->object_id, 2);
372 * Some GUIDs break the specification by declaring themselves
373 * expensive, but have no corresponding WCxx method. So we
374 * should not fail if this happens.
376 wc_status = acpi_evaluate_object(handle, wc_method,
377 &wc_input, NULL);
380 strcpy(method, "WQ");
381 strncat(method, block->object_id, 2);
382 DEBUG(2, "Object to call is %s\n", method);
384 status = acpi_evaluate_object(handle, method, NULL, out);
387 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
388 * the WQxx method failed - we should disable collection anyway
390 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
391 wc_params[0].integer.value = 0;
392 status = acpi_evaluate_object(handle,
393 wc_method, &wc_input, NULL);
396 return status;
398 EXPORT_SYMBOL_GPL(wmi_acer_query_block);
401 * wmi_acer_set_block - Write to a WMI block
402 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
403 * @instance: Instance index
404 * &in: Buffer containing new values for the data block
406 * Write the contents of the input buffer to ACPI
408 acpi_status wmi_acer_set_block(const char *guid_string, u8 instance,
409 const struct acpi_buffer *in)
411 struct guid_block *block = NULL;
412 struct wmi_block *wblock = NULL;
413 acpi_handle handle;
414 struct acpi_object_list input;
415 union acpi_object params[2];
416 char method[4] = "WS";
418 if (guid_string == NULL || in == NULL)
419 return AE_BAD_DATA;
421 if (!find_guid(guid_string, &wblock))
422 return AE_BAD_ADDRESS;
424 block = &wblock->gblock;
425 handle = wblock->handle;
427 if (block->instance_count < instance)
428 return AE_BAD_PARAMETER;
430 /* Check GUID is a data block */
431 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
432 return AE_BAD_ADDRESS;
434 input.count = 2;
435 input.pointer = params;
436 params[0].type = ACPI_TYPE_INTEGER;
437 params[0].integer.value = instance;
439 if (block->flags & ACPI_WMI_STRING) {
440 params[1].type = ACPI_TYPE_STRING;
441 } else {
442 params[1].type = ACPI_TYPE_BUFFER;
444 params[1].buffer.length = in->length;
445 params[1].buffer.pointer = in->pointer;
447 strncat(method, block->object_id, 2);
448 DEBUG(2, "Object to call is %s\n", method);
450 return acpi_evaluate_object(handle, method, &input, NULL);
452 EXPORT_SYMBOL_GPL(wmi_acer_set_block);
455 * wmi_install_notify_handler - Register handler for WMI events
456 * @handler: Function to handle notifications
457 * @data: Data to be returned to handler when event is fired
459 * Register a handler for events sent to the WMI-ACPI mapper device.
461 acpi_status wmi_acer_install_notify_handler(wmi_notify_handler handler, void *data)
463 if (!handler)
464 return AE_BAD_PARAMETER;
466 if (!wmi_external_handler)
467 return AE_ALREADY_ACQUIRED;
469 wmi_external_handler = handler;
470 wmi_external_data = data;
472 return AE_OK;
474 EXPORT_SYMBOL_GPL(wmi_acer_install_notify_handler);
477 * wmi_acer_uninstall_notify_handler - Unregister handler for WMI events
479 * Unregister handler for events sent to the WMI-ACPI mapper device.
481 acpi_status wmi_acer_remove_notify_handler(void)
483 DEBUG(1, "Notifier triggered\n");
485 if (wmi_external_handler) {
486 wmi_external_handler = NULL;
487 wmi_external_data = NULL;
488 return AE_OK;
490 return AE_ERROR;
492 EXPORT_SYMBOL_GPL(wmi_acer_remove_notify_handler);
495 * wmi_acer_get_event_data - Get WMI data associated with an event
497 * @event - Event to find
498 * &out - Buffer to hold event data
500 * Returns extra data associated with an event in WMI.
502 acpi_status wmi_acer_get_event_data(u32 event, struct acpi_buffer *out)
504 struct acpi_object_list input;
505 union acpi_object params[1];
506 struct guid_block *gblock;
507 struct wmi_block *wblock;
508 struct list_head *p;
510 input.count = 1;
511 input.pointer = params;
512 params[0].type = ACPI_TYPE_INTEGER;
513 params[0].integer.value = event;
515 list_for_each(p, &wmi_blocks.list) {
516 wblock = list_entry(p, struct wmi_block, list);
517 gblock = &wblock->gblock;
519 if ((gblock->flags & ACPI_WMI_EVENT) &&
520 (gblock->notify_id == event))
521 return acpi_evaluate_object(wblock->handle, "_WED",
522 &input, out);
525 return AE_NOT_FOUND;
527 EXPORT_SYMBOL(wmi_acer_get_event_data);
530 * wmi_acer_has_guid - Check if a GUID is available
531 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
533 * Check if a given GUID is defined by _WDG
535 bool wmi_acer_has_guid(const char *guid_string)
537 return find_guid(guid_string, NULL);
539 EXPORT_SYMBOL_GPL(wmi_acer_has_guid);
542 * parse_wdg - Parse the _WDG method for the GUID data blocks
544 static __init acpi_status parse_wdg(acpi_handle handle)
546 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
547 union acpi_object *obj;
548 struct guid_block *gblock;
549 struct wmi_block *wblock;
550 acpi_status status;
551 u32 i, total;
553 status = acpi_evaluate_object(handle, "_WDG", NULL, &out);
555 if (ACPI_FAILURE(status))
556 return status;
558 obj = (union acpi_object *) out.pointer;
560 if (obj->type != ACPI_TYPE_BUFFER)
561 return AE_ERROR;
563 total = obj->buffer.length / sizeof(struct guid_block);
565 gblock = kzalloc(obj->buffer.length, GFP_KERNEL);
566 if (!gblock)
567 return AE_NO_MEMORY;
569 memcpy(gblock, obj->buffer.pointer, obj->buffer.length);
571 for (i = 0; i < total; i++) {
572 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
573 if (!wblock)
574 return AE_NO_MEMORY;
576 wblock->gblock = gblock[i];
577 wblock->handle = handle;
578 list_add_tail(&wblock->list, &wmi_blocks.list);
581 kfree(out.pointer);
582 kfree(gblock);
584 return status;
588 * WMI can have EmbeddedControl access regions. In which case, we just want to
589 * hand these off to the EC driver.
591 static acpi_status
592 acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
593 u32 bits, acpi_integer *value,
594 void *handler_context, void *region_context)
596 int result = 0, i = 0;
597 u8 temp = 0;
599 if ((address > 0xFF) || !value)
600 return AE_BAD_PARAMETER;
602 if (function != ACPI_READ && function != ACPI_WRITE)
603 return AE_BAD_PARAMETER;
605 if (bits != 8)
606 return AE_BAD_PARAMETER;
608 if (function == ACPI_READ) {
609 result = ec_read(address, &temp);
610 (*value) |= ((acpi_integer)temp) << i;
611 } else {
612 temp = 0xff & ((*value) >> i);
613 result = ec_write(address, temp);
616 switch (result) {
617 case -EINVAL:
618 return AE_BAD_PARAMETER;
619 break;
620 case -ENODEV:
621 return AE_NOT_FOUND;
622 break;
623 case -ETIME:
624 return AE_TIME;
625 break;
626 default:
627 return AE_OK;
631 static void acpi_wmi_notify(acpi_handle handle, u32 event, void *data)
633 struct guid_block *block;
634 struct wmi_block *wblock;
635 struct list_head *p;
637 list_for_each(p, &wmi_blocks.list) {
638 wblock = list_entry(p, struct wmi_block, list);
639 block = &wblock->gblock;
641 if ((block->flags & ACPI_WMI_EVENT) &&
642 block->notify_id == event) {
643 if (wmi_external_handler)
644 wmi_external_handler(event, wmi_external_data);
645 break;
650 static int __init acpi_wmi_add(struct acpi_device *device)
652 acpi_status status;
653 int result = 0;
655 status = acpi_install_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
656 acpi_wmi_notify, device);
658 if (ACPI_FAILURE(status)) {
659 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
660 "Error installing notify handler\n"));
661 return -ENODEV;
664 status = acpi_install_address_space_handler(device->handle,
665 ACPI_ADR_SPACE_EC,
666 &acpi_wmi_ec_space_handler,
667 NULL, NULL);
668 if (ACPI_FAILURE(status)) {
669 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
670 "Error installing EC region handler\n"));
671 return -ENODEV;
674 status = parse_wdg(device->handle);
675 if (ACPI_FAILURE(status))
676 return -ENODEV;
678 return result;
681 static int acpi_wmi_remove(struct acpi_device *device, int type)
683 acpi_remove_address_space_handler(device->handle, ACPI_ADR_SPACE_EC,
684 &acpi_wmi_ec_space_handler);
686 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
687 acpi_wmi_notify);
689 return 0;
692 static int __init acpi_wmi_init(void)
694 acpi_status result;
696 if (acpi_disabled)
697 return -ENODEV;
699 INIT_LIST_HEAD(&wmi_blocks.list);
701 result = acpi_bus_register_driver(&acpi_wmi_driver);
703 if (result < 0) {
704 printk(KERN_INFO PREFIX "Interface device not found\n");
705 return result;
708 printk(KERN_INFO PREFIX "Mapper loaded\n");
710 return result;
713 static void __exit acpi_wmi_exit(void)
715 struct list_head *p, *tmp;
716 struct wmi_block *wblock;
718 acpi_bus_unregister_driver(&acpi_wmi_driver);
720 list_for_each_safe(p, tmp, &wmi_blocks.list) {
721 wblock = list_entry(p, struct wmi_block, list);
723 list_del(p);
724 kfree(wblock);
727 printk(KERN_INFO PREFIX "Mapper unloaded\n");
730 module_init(acpi_wmi_init);
731 module_exit(acpi_wmi_exit);