2 * ACPI-WMI mapping driver
4 * Copyright (C) 2007-2008 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/init.h>
32 #include <linux/types.h>
33 #include <linux/list.h>
34 #include <linux/acpi.h>
35 #include <acpi/acpi_bus.h>
36 #include <acpi/acpi_drivers.h>
38 ACPI_MODULE_NAME("wmi");
39 MODULE_AUTHOR("Carlos Corbacho");
40 MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
41 MODULE_LICENSE("GPL");
43 #define ACPI_WMI_CLASS "wmi"
46 #define PREFIX "ACPI: WMI: "
48 static DEFINE_MUTEX(wmi_data_lock);
55 unsigned char notify_id;
56 unsigned char reserved;
64 struct list_head list;
65 struct guid_block gblock;
67 wmi_notify_handler handler;
71 static struct wmi_block wmi_blocks;
74 * If the GUID data block is marked as expensive, we must enable and
75 * explicitily disable data collection.
77 #define ACPI_WMI_EXPENSIVE 0x1
78 #define ACPI_WMI_METHOD 0x2 /* GUID is a method */
79 #define ACPI_WMI_STRING 0x4 /* GUID takes & returns a string */
80 #define ACPI_WMI_EVENT 0x8 /* GUID is an event */
82 static int acpi_wmi_remove(struct acpi_device *device, int type);
83 static int acpi_wmi_add(struct acpi_device *device);
84 static void acpi_wmi_notify(struct acpi_device *device, u32 event);
86 static const struct acpi_device_id wmi_device_ids[] = {
91 MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
93 static struct acpi_driver acpi_wmi_driver = {
95 .class = ACPI_WMI_CLASS,
96 .ids = wmi_device_ids,
99 .remove = acpi_wmi_remove,
100 .notify = acpi_wmi_notify,
105 * GUID parsing functions
109 * wmi_parse_hexbyte - Convert a ASCII hex number to a byte
110 * @src: Pointer to at least 2 characters to convert.
112 * Convert a two character ASCII hex string to a number.
114 * Return: 0-255 Success, the byte was parsed correctly
115 * -1 Error, an invalid character was supplied
117 static int wmi_parse_hexbyte(const u8 *src)
119 unsigned int x; /* For correct wrapping */
124 if (x - '0' <= '9' - '0') {
126 } else if (x - 'a' <= 'f' - 'a') {
128 } else if (x - 'A' <= 'F' - 'A') {
137 if (x - '0' <= '9' - '0')
138 return h | (x - '0');
139 if (x - 'a' <= 'f' - 'a')
140 return h | (x - 'a' + 10);
141 if (x - 'A' <= 'F' - 'A')
142 return h | (x - 'A' + 10);
147 * wmi_swap_bytes - Rearrange GUID bytes to match GUID binary
148 * @src: Memory block holding binary GUID (16 bytes)
149 * @dest: Memory block to hold byte swapped binary GUID (16 bytes)
151 * Byte swap a binary GUID to match it's real GUID value
153 static void wmi_swap_bytes(u8 *src, u8 *dest)
157 for (i = 0; i <= 3; i++)
158 memcpy(dest + i, src + (3 - i), 1);
160 for (i = 0; i <= 1; i++)
161 memcpy(dest + 4 + i, src + (5 - i), 1);
163 for (i = 0; i <= 1; i++)
164 memcpy(dest + 6 + i, src + (7 - i), 1);
166 memcpy(dest + 8, src + 8, 8);
170 * wmi_parse_guid - Convert GUID from ASCII to binary
171 * @src: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
172 * @dest: Memory block to hold binary GUID (16 bytes)
174 * N.B. The GUID need not be NULL terminated.
176 * Return: 'true' @dest contains binary GUID
177 * 'false' @dest contents are undefined
179 static bool wmi_parse_guid(const u8 *src, u8 *dest)
181 static const int size[] = { 4, 2, 2, 2, 6 };
184 if (src[8] != '-' || src[13] != '-' ||
185 src[18] != '-' || src[23] != '-')
188 for (j = 0; j < 5; j++, src++) {
189 for (i = 0; i < size[j]; i++, src += 2, *dest++ = v) {
190 v = wmi_parse_hexbyte(src);
199 static bool find_guid(const char *guid_string, struct wmi_block **out)
201 char tmp[16], guid_input[16];
202 struct wmi_block *wblock;
203 struct guid_block *block;
206 wmi_parse_guid(guid_string, tmp);
207 wmi_swap_bytes(tmp, guid_input);
209 list_for_each(p, &wmi_blocks.list) {
210 wblock = list_entry(p, struct wmi_block, list);
211 block = &wblock->gblock;
213 if (memcmp(block->guid, guid_input, 16) == 0) {
222 static acpi_status wmi_method_enable(struct wmi_block *wblock, int enable)
224 struct guid_block *block = NULL;
226 struct acpi_object_list input;
227 union acpi_object params[1];
231 block = &wblock->gblock;
232 handle = wblock->handle;
238 input.pointer = params;
239 params[0].type = ACPI_TYPE_INTEGER;
240 params[0].integer.value = enable;
242 snprintf(method, 5, "WE%02X", block->notify_id);
243 status = acpi_evaluate_object(handle, method, &input, NULL);
245 if (status != AE_OK && status != AE_NOT_FOUND)
252 * Exported WMI functions
255 * wmi_evaluate_method - Evaluate a WMI method
256 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
257 * @instance: Instance index
258 * @method_id: Method ID to call
259 * &in: Buffer containing input for the method call
260 * &out: Empty buffer to return the method results
262 * Call an ACPI-WMI method
264 acpi_status wmi_evaluate_method(const char *guid_string, u8 instance,
265 u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
267 struct guid_block *block = NULL;
268 struct wmi_block *wblock = NULL;
271 struct acpi_object_list input;
272 union acpi_object params[3];
273 char method[4] = "WM";
275 if (!find_guid(guid_string, &wblock))
278 block = &wblock->gblock;
279 handle = wblock->handle;
281 if (!(block->flags & ACPI_WMI_METHOD))
284 if (block->instance_count < instance)
285 return AE_BAD_PARAMETER;
288 input.pointer = params;
289 params[0].type = ACPI_TYPE_INTEGER;
290 params[0].integer.value = instance;
291 params[1].type = ACPI_TYPE_INTEGER;
292 params[1].integer.value = method_id;
297 if (block->flags & ACPI_WMI_STRING) {
298 params[2].type = ACPI_TYPE_STRING;
300 params[2].type = ACPI_TYPE_BUFFER;
302 params[2].buffer.length = in->length;
303 params[2].buffer.pointer = in->pointer;
306 strncat(method, block->object_id, 2);
308 status = acpi_evaluate_object(handle, method, &input, out);
312 EXPORT_SYMBOL_GPL(wmi_evaluate_method);
315 * wmi_query_block - Return contents of a WMI block
316 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
317 * @instance: Instance index
318 * &out: Empty buffer to return the contents of the data block to
320 * Return the contents of an ACPI-WMI data block to a buffer
322 acpi_status wmi_query_block(const char *guid_string, u8 instance,
323 struct acpi_buffer *out)
325 struct guid_block *block = NULL;
326 struct wmi_block *wblock = NULL;
327 acpi_handle handle, wc_handle;
328 acpi_status status, wc_status = AE_ERROR;
329 struct acpi_object_list input, wc_input;
330 union acpi_object wc_params[1], wq_params[1];
332 char wc_method[4] = "WC";
334 if (!guid_string || !out)
335 return AE_BAD_PARAMETER;
337 if (!find_guid(guid_string, &wblock))
340 block = &wblock->gblock;
341 handle = wblock->handle;
343 if (block->instance_count < instance)
344 return AE_BAD_PARAMETER;
346 /* Check GUID is a data block */
347 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
351 input.pointer = wq_params;
352 wq_params[0].type = ACPI_TYPE_INTEGER;
353 wq_params[0].integer.value = instance;
356 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
359 if (block->flags & ACPI_WMI_EXPENSIVE) {
361 wc_input.pointer = wc_params;
362 wc_params[0].type = ACPI_TYPE_INTEGER;
363 wc_params[0].integer.value = 1;
365 strncat(wc_method, block->object_id, 2);
368 * Some GUIDs break the specification by declaring themselves
369 * expensive, but have no corresponding WCxx method. So we
370 * should not fail if this happens.
372 wc_status = acpi_get_handle(handle, wc_method, &wc_handle);
373 if (ACPI_SUCCESS(wc_status))
374 wc_status = acpi_evaluate_object(handle, wc_method,
378 strcpy(method, "WQ");
379 strncat(method, block->object_id, 2);
381 status = acpi_evaluate_object(handle, method, &input, out);
384 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
385 * the WQxx method failed - we should disable collection anyway.
387 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
388 wc_params[0].integer.value = 0;
389 status = acpi_evaluate_object(handle,
390 wc_method, &wc_input, NULL);
395 EXPORT_SYMBOL_GPL(wmi_query_block);
398 * wmi_set_block - Write to a WMI block
399 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
400 * @instance: Instance index
401 * &in: Buffer containing new values for the data block
403 * Write the contents of the input buffer to an ACPI-WMI data block
405 acpi_status wmi_set_block(const char *guid_string, u8 instance,
406 const struct acpi_buffer *in)
408 struct guid_block *block = NULL;
409 struct wmi_block *wblock = NULL;
411 struct acpi_object_list input;
412 union acpi_object params[2];
413 char method[4] = "WS";
415 if (!guid_string || !in)
418 if (!find_guid(guid_string, &wblock))
421 block = &wblock->gblock;
422 handle = wblock->handle;
424 if (block->instance_count < instance)
425 return AE_BAD_PARAMETER;
427 /* Check GUID is a data block */
428 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
432 input.pointer = params;
433 params[0].type = ACPI_TYPE_INTEGER;
434 params[0].integer.value = instance;
436 if (block->flags & ACPI_WMI_STRING) {
437 params[1].type = ACPI_TYPE_STRING;
439 params[1].type = ACPI_TYPE_BUFFER;
441 params[1].buffer.length = in->length;
442 params[1].buffer.pointer = in->pointer;
444 strncat(method, block->object_id, 2);
446 return acpi_evaluate_object(handle, method, &input, NULL);
448 EXPORT_SYMBOL_GPL(wmi_set_block);
451 * wmi_install_notify_handler - Register handler for WMI events
452 * @handler: Function to handle notifications
453 * @data: Data to be returned to handler when event is fired
455 * Register a handler for events sent to the ACPI-WMI mapper device.
457 acpi_status wmi_install_notify_handler(const char *guid,
458 wmi_notify_handler handler, void *data)
460 struct wmi_block *block;
463 if (!guid || !handler)
464 return AE_BAD_PARAMETER;
466 find_guid(guid, &block);
471 return AE_ALREADY_ACQUIRED;
473 block->handler = handler;
474 block->handler_data = data;
476 status = wmi_method_enable(block, 1);
480 EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
483 * wmi_uninstall_notify_handler - Unregister handler for WMI events
485 * Unregister handler for events sent to the ACPI-WMI mapper device.
487 acpi_status wmi_remove_notify_handler(const char *guid)
489 struct wmi_block *block;
493 return AE_BAD_PARAMETER;
495 find_guid(guid, &block);
500 return AE_NULL_ENTRY;
502 status = wmi_method_enable(block, 0);
504 block->handler = NULL;
505 block->handler_data = NULL;
509 EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
512 * wmi_get_event_data - Get WMI data associated with an event
514 * @event - Event to find
515 * &out - Buffer to hold event data
517 * Returns extra data associated with an event in WMI.
519 acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
521 struct acpi_object_list input;
522 union acpi_object params[1];
523 struct guid_block *gblock;
524 struct wmi_block *wblock;
528 input.pointer = params;
529 params[0].type = ACPI_TYPE_INTEGER;
530 params[0].integer.value = event;
532 list_for_each(p, &wmi_blocks.list) {
533 wblock = list_entry(p, struct wmi_block, list);
534 gblock = &wblock->gblock;
536 if ((gblock->flags & ACPI_WMI_EVENT) &&
537 (gblock->notify_id == event))
538 return acpi_evaluate_object(wblock->handle, "_WED",
544 EXPORT_SYMBOL_GPL(wmi_get_event_data);
547 * wmi_has_guid - Check if a GUID is available
548 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
550 * Check if a given GUID is defined by _WDG
552 bool wmi_has_guid(const char *guid_string)
554 return find_guid(guid_string, NULL);
556 EXPORT_SYMBOL_GPL(wmi_has_guid);
559 * Parse the _WDG method for the GUID data blocks
561 static __init acpi_status parse_wdg(acpi_handle handle)
563 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
564 union acpi_object *obj;
565 struct guid_block *gblock;
566 struct wmi_block *wblock;
570 status = acpi_evaluate_object(handle, "_WDG", NULL, &out);
572 if (ACPI_FAILURE(status))
575 obj = (union acpi_object *) out.pointer;
577 if (obj->type != ACPI_TYPE_BUFFER)
580 total = obj->buffer.length / sizeof(struct guid_block);
582 gblock = kzalloc(obj->buffer.length, GFP_KERNEL);
586 memcpy(gblock, obj->buffer.pointer, obj->buffer.length);
588 for (i = 0; i < total; i++) {
589 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
593 wblock->gblock = gblock[i];
594 wblock->handle = handle;
595 list_add_tail(&wblock->list, &wmi_blocks.list);
605 * WMI can have EmbeddedControl access regions. In which case, we just want to
606 * hand these off to the EC driver.
609 acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
610 u32 bits, acpi_integer * value,
611 void *handler_context, void *region_context)
613 int result = 0, i = 0;
616 if ((address > 0xFF) || !value)
617 return AE_BAD_PARAMETER;
619 if (function != ACPI_READ && function != ACPI_WRITE)
620 return AE_BAD_PARAMETER;
623 return AE_BAD_PARAMETER;
625 if (function == ACPI_READ) {
626 result = ec_read(address, &temp);
627 (*value) |= ((acpi_integer)temp) << i;
629 temp = 0xff & ((*value) >> i);
630 result = ec_write(address, temp);
635 return AE_BAD_PARAMETER;
648 static void acpi_wmi_notify(struct acpi_device *device, u32 event)
650 struct guid_block *block;
651 struct wmi_block *wblock;
654 list_for_each(p, &wmi_blocks.list) {
655 wblock = list_entry(p, struct wmi_block, list);
656 block = &wblock->gblock;
658 if ((block->flags & ACPI_WMI_EVENT) &&
659 (block->notify_id == event)) {
661 wblock->handler(event, wblock->handler_data);
663 acpi_bus_generate_netlink_event(
664 device->pnp.device_class, dev_name(&device->dev),
671 static int acpi_wmi_remove(struct acpi_device *device, int type)
673 acpi_remove_address_space_handler(device->handle,
674 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
679 static int __init acpi_wmi_add(struct acpi_device *device)
684 status = acpi_install_address_space_handler(device->handle,
686 &acpi_wmi_ec_space_handler,
688 if (ACPI_FAILURE(status))
691 status = parse_wdg(device->handle);
692 if (ACPI_FAILURE(status)) {
693 printk(KERN_ERR PREFIX "Error installing EC region handler\n");
700 static int __init acpi_wmi_init(void)
704 INIT_LIST_HEAD(&wmi_blocks.list);
709 result = acpi_bus_register_driver(&acpi_wmi_driver);
712 printk(KERN_INFO PREFIX "Error loading mapper\n");
714 printk(KERN_INFO PREFIX "Mapper loaded\n");
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);
734 printk(KERN_INFO PREFIX "Mapper unloaded\n");
737 subsys_initcall(acpi_wmi_init);
738 module_exit(acpi_wmi_exit);