acer_acpi - Fix setting backlight on AMW0 V1
[acer_acpi.git] / wmi-acer.c
blob7b311b5e69041f1604789e18d606c8106c82a5d2
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 (defined(CONFIG_SUSE_KERNEL) && LINUX_VERSION_CODE == KERNEL_VERSION(2,6,22))
115 const static struct acpi_device_id wmi_device_ids[] = {
116 {"PNP0C14", 0},
117 {"pnp0c14", 0},
118 {"", 0},
120 MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
121 #endif
123 static struct acpi_driver acpi_wmi_driver = {
124 .name = "wmi-acer",
125 .class = ACPI_WMI_CLASS,
126 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)) || \
127 (defined(CONFIG_SUSE_KERNEL) && LINUX_VERSION_CODE == KERNEL_VERSION(2,6,22))
128 .ids = wmi_device_ids,
129 #else
130 .ids = "PNP0C14,pnp0c14",
131 #endif
132 .ops = {
133 .add = acpi_wmi_add,
134 .remove = acpi_wmi_remove,
139 * GUID parsing functions
143 * wmi_parse_hexbyte - Convert a ASCII hex number to a byte
144 * @src: Pointer to at least 2 characters to convert.
146 * Convert a two character ASCII hex string to a number.
148 * Return: 0-255 Success, the byte was parsed correctly
149 * -1 Error, an invalid character was supplied
151 static int wmi_parse_hexbyte(const u8 *src)
153 unsigned int x; /* For correct wrapping */
154 int h;
156 /* high part */
157 x = src[0];
158 if (x - '0' <= '9' - '0') {
159 h = x - '0';
160 } else if (x - 'a' <= 'f' - 'a') {
161 h = x - 'a' + 10;
162 } else if (x - 'A' <= 'F' - 'A') {
163 h = x - 'A' + 10;
164 } else {
165 return -1;
167 h <<= 4;
169 /* low part */
170 x = src[1];
171 if (x - '0' <= '9' - '0')
172 return h | (x - '0');
173 if (x - 'a' <= 'f' - 'a')
174 return h | (x - 'a' + 10);
175 if (x - 'A' <= 'F' - 'A')
176 return h | (x - 'A' + 10);
177 return -1;
181 * wmi_swap_bytes - Rearrange GUID bytes to match GUID binary
182 * @src: Memory block holding binary GUID (16 bytes)
183 * @dest: Memory block to hold byte swapped binary GUID (16 bytes)
185 * Byte swap a binary GUID to match it's real GUID value
187 static void wmi_swap_bytes(u8 *src, u8 *dest)
189 int i;
191 for (i = 0; i <= 3; i++)
192 memcpy(dest + i, src + (3 - i), 1);
194 for (i = 0; i <= 1; i++)
195 memcpy(dest + 4 + i, src + (5 - i), 1);
197 for (i = 0; i <= 1; i++)
198 memcpy(dest + 6 + i, src + (7 - i), 1);
200 memcpy(dest + 8, src + 8, 8);
204 * wmi_parse_guid - Convert GUID from ASCII to binary
205 * @src: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
206 * @dest: Memory block to hold binary GUID (16 bytes)
208 * N.B. The GUID need not be NULL terminated.
210 * Return: 'true' @dest contains binary GUID
211 * 'false' @dest contents are undefined
213 static bool wmi_parse_guid(const u8 *src, u8 *dest)
215 static const int size[] = { 4, 2, 2, 2, 6 };
216 int i, j, v;
218 if (src[8] != '-' || src[13] != '-' ||
219 src[18] != '-' || src[23] != '-')
220 return false;
222 for (j = 0; j < 5; j++, src++) {
223 for (i = 0; i < size[j]; i++, src += 2, *dest++ = v) {
224 v = wmi_parse_hexbyte(src);
225 if (v < 0)
226 return false;
230 return true;
233 static bool find_guid(const char *guid_string, struct wmi_block **out)
235 char tmp[16], guid_input[16];
236 struct wmi_block *wblock;
237 struct guid_block *block;
238 struct list_head *p;
240 DEBUG(2, "find_guid called\n");
241 DEBUG(2, "Passing GUID to parser\n");
242 wmi_parse_guid(guid_string, tmp);
243 wmi_swap_bytes(tmp, guid_input);
245 list_for_each(p, &wmi_blocks.list) {
246 wblock = list_entry(p, struct wmi_block, list);
247 block = &wblock->gblock;
249 if (memcmp(block->guid, guid_input, 16) == 0) {
250 DEBUG(2, "GUID found - returning block\n");
251 if (out)
252 *out = wblock;
253 return 1;
256 return 0;
260 * Exported WMI functions
263 * wmi_acer_evaluate_method - Evaluate a WMI method
264 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
265 * @instance: Instance index
266 * @method_id: Method ID to call
267 * &in: Buffer containing input for the method call
268 * &out: Empty buffer to return the method results
270 * Convert a WMI method call to an ACPI one, and return the results
272 acpi_status wmi_acer_evaluate_method(const char *guid_string, u8 instance,
273 u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
275 struct guid_block *block = NULL;
276 struct wmi_block *wblock = NULL;
277 acpi_handle handle;
278 acpi_status status;
279 struct acpi_object_list input;
280 union acpi_object params[3];
281 char method[4] = "WM";
283 if (!find_guid(guid_string, &wblock))
284 return AE_BAD_ADDRESS;
286 block = &wblock->gblock;
287 handle = wblock->handle;
289 if (!block->flags & ACPI_WMI_METHOD) {
290 DEBUG(2, "GUID is not a method\n");
291 return AE_BAD_DATA;
294 if (block->instance_count < instance)
295 return AE_BAD_PARAMETER;
297 input.count = 2;
298 input.pointer = params;
299 params[0].type = ACPI_TYPE_INTEGER;
300 params[0].integer.value = instance;
301 params[1].type = ACPI_TYPE_INTEGER;
302 params[1].integer.value = method_id;
304 if (in != NULL) {
305 input.count = 3;
307 if (block->flags & ACPI_WMI_STRING) {
308 params[2].type = ACPI_TYPE_STRING;
309 } else {
310 params[2].type = ACPI_TYPE_BUFFER;
312 params[2].buffer.length = in->length;
313 params[2].buffer.pointer = in->pointer;
316 strncat(method, block->object_id, 2);
317 DEBUG(2, "Object to call is %s\n", method);
319 status = acpi_evaluate_object(handle, method, &input, out);
321 return status;
323 EXPORT_SYMBOL_GPL(wmi_acer_evaluate_method);
326 * wmi_query_block - Return contents of a WMI block
327 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
328 * @instance: Instance index
329 * &out: Empty buffer to return the contents of the data block to
331 * Return the contents of a data block to a buffer
333 acpi_status wmi_acer_query_block(const char *guid_string, u8 instance,
334 struct acpi_buffer *out)
336 struct guid_block *block = NULL;
337 struct wmi_block *wblock = NULL;
338 acpi_handle handle;
339 acpi_status status, wc_status = AE_ERROR;
340 struct acpi_object_list input, wc_input;
341 union acpi_object wc_params[1], wq_params[1];
342 char method[4];
343 char wc_method[4] = "WC";
345 if (guid_string == NULL || out == NULL)
346 return AE_BAD_PARAMETER;
348 if (!find_guid(guid_string, &wblock))
349 return AE_BAD_ADDRESS;
351 block = &wblock->gblock;
352 handle = wblock->handle;
354 if (block->instance_count < instance)
355 return AE_BAD_PARAMETER;
357 /* Check GUID is a data block */
358 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
359 return AE_BAD_ADDRESS;
361 input.count = 1;
362 input.pointer = wq_params;
363 wq_params[0].type = ACPI_TYPE_INTEGER;
364 wq_params[0].integer.value = instance;
367 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
368 * enable collection
370 if (block->flags & ACPI_WMI_EXPENSIVE) {
371 wc_input.count = 1;
372 wc_input.pointer = wc_params;
373 wc_params[0].type = ACPI_TYPE_INTEGER;
374 wc_params[0].integer.value = 1;
376 strncat(wc_method, block->object_id, 2);
379 * Some GUIDs break the specification by declaring themselves
380 * expensive, but have no corresponding WCxx method. So we
381 * should not fail if this happens.
383 wc_status = acpi_evaluate_object(handle, wc_method,
384 &wc_input, NULL);
387 strcpy(method, "WQ");
388 strncat(method, block->object_id, 2);
389 DEBUG(2, "Object to call is %s\n", method);
391 status = acpi_evaluate_object(handle, method, NULL, out);
394 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
395 * the WQxx method failed - we should disable collection anyway
397 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
398 wc_params[0].integer.value = 0;
399 status = acpi_evaluate_object(handle,
400 wc_method, &wc_input, NULL);
403 return status;
405 EXPORT_SYMBOL_GPL(wmi_acer_query_block);
408 * wmi_acer_set_block - Write to a WMI block
409 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
410 * @instance: Instance index
411 * &in: Buffer containing new values for the data block
413 * Write the contents of the input buffer to ACPI
415 acpi_status wmi_acer_set_block(const char *guid_string, u8 instance,
416 const struct acpi_buffer *in)
418 struct guid_block *block = NULL;
419 struct wmi_block *wblock = NULL;
420 acpi_handle handle;
421 struct acpi_object_list input;
422 union acpi_object params[2];
423 char method[4] = "WS";
425 if (guid_string == NULL || in == NULL)
426 return AE_BAD_DATA;
428 if (!find_guid(guid_string, &wblock))
429 return AE_BAD_ADDRESS;
431 block = &wblock->gblock;
432 handle = wblock->handle;
434 if (block->instance_count < instance)
435 return AE_BAD_PARAMETER;
437 /* Check GUID is a data block */
438 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
439 return AE_BAD_ADDRESS;
441 input.count = 2;
442 input.pointer = params;
443 params[0].type = ACPI_TYPE_INTEGER;
444 params[0].integer.value = instance;
446 if (block->flags & ACPI_WMI_STRING) {
447 params[1].type = ACPI_TYPE_STRING;
448 } else {
449 params[1].type = ACPI_TYPE_BUFFER;
451 params[1].buffer.length = in->length;
452 params[1].buffer.pointer = in->pointer;
454 strncat(method, block->object_id, 2);
455 DEBUG(2, "Object to call is %s\n", method);
457 return acpi_evaluate_object(handle, method, &input, NULL);
459 EXPORT_SYMBOL_GPL(wmi_acer_set_block);
462 * wmi_install_notify_handler - Register handler for WMI events
463 * @handler: Function to handle notifications
464 * @data: Data to be returned to handler when event is fired
466 * Register a handler for events sent to the WMI-ACPI mapper device.
468 acpi_status wmi_acer_install_notify_handler(wmi_notify_handler handler, void *data)
470 if (!handler)
471 return AE_BAD_PARAMETER;
473 if (!wmi_external_handler)
474 return AE_ALREADY_ACQUIRED;
476 wmi_external_handler = handler;
477 wmi_external_data = data;
479 return AE_OK;
481 EXPORT_SYMBOL_GPL(wmi_acer_install_notify_handler);
484 * wmi_acer_uninstall_notify_handler - Unregister handler for WMI events
486 * Unregister handler for events sent to the WMI-ACPI mapper device.
488 acpi_status wmi_acer_remove_notify_handler(void)
490 DEBUG(1, "Notifier triggered\n");
492 if (wmi_external_handler) {
493 wmi_external_handler = NULL;
494 wmi_external_data = NULL;
495 return AE_OK;
497 return AE_ERROR;
499 EXPORT_SYMBOL_GPL(wmi_acer_remove_notify_handler);
502 * wmi_acer_get_event_data - Get WMI data associated with an event
504 * @event - Event to find
505 * &out - Buffer to hold event data
507 * Returns extra data associated with an event in WMI.
509 acpi_status wmi_acer_get_event_data(u32 event, struct acpi_buffer *out)
511 struct acpi_object_list input;
512 union acpi_object params[1];
513 struct guid_block *gblock;
514 struct wmi_block *wblock;
515 struct list_head *p;
517 input.count = 1;
518 input.pointer = params;
519 params[0].type = ACPI_TYPE_INTEGER;
520 params[0].integer.value = event;
522 list_for_each(p, &wmi_blocks.list) {
523 wblock = list_entry(p, struct wmi_block, list);
524 gblock = &wblock->gblock;
526 if ((gblock->flags & ACPI_WMI_EVENT) &&
527 (gblock->notify_id == event))
528 return acpi_evaluate_object(wblock->handle, "_WED",
529 &input, out);
532 return AE_NOT_FOUND;
534 EXPORT_SYMBOL(wmi_acer_get_event_data);
537 * wmi_acer_has_guid - Check if a GUID is available
538 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
540 * Check if a given GUID is defined by _WDG
542 bool wmi_acer_has_guid(const char *guid_string)
544 return find_guid(guid_string, NULL);
546 EXPORT_SYMBOL_GPL(wmi_acer_has_guid);
549 * parse_wdg - Parse the _WDG method for the GUID data blocks
551 static __init acpi_status parse_wdg(acpi_handle handle)
553 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
554 union acpi_object *obj;
555 struct guid_block *gblock;
556 struct wmi_block *wblock;
557 acpi_status status;
558 u32 i, total;
560 status = acpi_evaluate_object(handle, "_WDG", NULL, &out);
562 if (ACPI_FAILURE(status))
563 return status;
565 obj = (union acpi_object *) out.pointer;
567 if (obj->type != ACPI_TYPE_BUFFER)
568 return AE_ERROR;
570 total = obj->buffer.length / sizeof(struct guid_block);
572 gblock = kzalloc(obj->buffer.length, GFP_KERNEL);
573 if (!gblock)
574 return AE_NO_MEMORY;
576 memcpy(gblock, obj->buffer.pointer, obj->buffer.length);
578 for (i = 0; i < total; i++) {
579 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
580 if (!wblock)
581 return AE_NO_MEMORY;
583 wblock->gblock = gblock[i];
584 wblock->handle = handle;
585 list_add_tail(&wblock->list, &wmi_blocks.list);
588 kfree(out.pointer);
589 kfree(gblock);
591 return status;
595 * WMI can have EmbeddedControl access regions. In which case, we just want to
596 * hand these off to the EC driver.
598 static acpi_status
599 acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
600 u32 bits, acpi_integer *value,
601 void *handler_context, void *region_context)
603 int result = 0, i = 0;
604 u8 temp = 0;
606 if ((address > 0xFF) || !value)
607 return AE_BAD_PARAMETER;
609 if (function != ACPI_READ && function != ACPI_WRITE)
610 return AE_BAD_PARAMETER;
612 if (bits != 8)
613 return AE_BAD_PARAMETER;
615 if (function == ACPI_READ) {
616 result = ec_read(address, &temp);
617 (*value) |= ((acpi_integer)temp) << i;
618 } else {
619 temp = 0xff & ((*value) >> i);
620 result = ec_write(address, temp);
623 switch (result) {
624 case -EINVAL:
625 return AE_BAD_PARAMETER;
626 break;
627 case -ENODEV:
628 return AE_NOT_FOUND;
629 break;
630 case -ETIME:
631 return AE_TIME;
632 break;
633 default:
634 return AE_OK;
638 static void acpi_wmi_notify(acpi_handle handle, u32 event, void *data)
640 struct guid_block *block;
641 struct wmi_block *wblock;
642 struct list_head *p;
644 list_for_each(p, &wmi_blocks.list) {
645 wblock = list_entry(p, struct wmi_block, list);
646 block = &wblock->gblock;
648 if ((block->flags & ACPI_WMI_EVENT) &&
649 block->notify_id == event) {
650 if (wmi_external_handler)
651 wmi_external_handler(event, wmi_external_data);
652 break;
657 static int __init acpi_wmi_add(struct acpi_device *device)
659 acpi_status status;
660 int result = 0;
662 status = acpi_install_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
663 acpi_wmi_notify, device);
665 if (ACPI_FAILURE(status)) {
666 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
667 "Error installing notify handler\n"));
668 return -ENODEV;
671 status = acpi_install_address_space_handler(device->handle,
672 ACPI_ADR_SPACE_EC,
673 &acpi_wmi_ec_space_handler,
674 NULL, NULL);
675 if (ACPI_FAILURE(status)) {
676 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
677 "Error installing EC region handler\n"));
678 return -ENODEV;
681 status = parse_wdg(device->handle);
682 if (ACPI_FAILURE(status))
683 return -ENODEV;
685 return result;
688 static int acpi_wmi_remove(struct acpi_device *device, int type)
690 acpi_remove_address_space_handler(device->handle, ACPI_ADR_SPACE_EC,
691 &acpi_wmi_ec_space_handler);
693 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
694 acpi_wmi_notify);
696 return 0;
699 static int __init acpi_wmi_init(void)
701 acpi_status result;
703 if (acpi_disabled)
704 return -ENODEV;
706 INIT_LIST_HEAD(&wmi_blocks.list);
708 result = acpi_bus_register_driver(&acpi_wmi_driver);
710 if (result < 0) {
711 printk(KERN_INFO PREFIX "Interface device not found\n");
712 return result;
715 printk(KERN_INFO PREFIX "Mapper loaded\n");
717 return result;
720 static void __exit acpi_wmi_exit(void)
722 struct list_head *p, *tmp;
723 struct wmi_block *wblock;
725 acpi_bus_unregister_driver(&acpi_wmi_driver);
727 list_for_each_safe(p, tmp, &wmi_blocks.list) {
728 wblock = list_entry(p, struct wmi_block, list);
730 list_del(p);
731 kfree(wblock);
734 printk(KERN_INFO PREFIX "Mapper unloaded\n");
737 module_init(acpi_wmi_init);
738 module_exit(acpi_wmi_exit);