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/sysfs.h>
36 #include <linux/mutex.h>
37 #include <linux/list.h>
39 #include <acpi/acpi_bus.h>
40 #include <acpi/acpi_drivers.h>
44 #define ACPI_WMI_CLASS "wmi-acer"
47 #define PREFIX "ACPI: WMI-Acer: "
48 #define WMI_ERR KERN_ERR PREFIX
49 #define WMI_NOTICE KERN_NOTICE PREFIX
50 #define WMI_INFO KERN_INFO PREFIX
52 #define DEBUG(level, message...) { \
54 printk(KERN_DEBUG PREFIX message);\
57 /* Workaround needed for older kernels */
64 MODULE_AUTHOR("Carlos Corbacho");
65 MODULE_DESCRIPTION("WMI ACPI Interface Driver - acer_acpi variant");
66 MODULE_LICENSE("GPL");
68 static DEFINE_MUTEX(wmi_data_lock
);
78 unsigned char notify_id
;
79 unsigned char reserved
;
88 struct list_head list
;
89 struct guid_block gblock
;
93 static struct wmi_block wmi_blocks
;
95 static wmi_notify_handler wmi_external_handler
;
96 static void *wmi_external_data
;
100 module_param(debug
, int, 0664);
101 MODULE_PARM_DESC(debug
, "Debugging verbosity level (0=least 2=most)");
104 * If the GUID data block is marked as expensive, we must enable and
105 * explicitily disable data collection.
107 #define ACPI_WMI_EXPENSIVE 0x1
108 #define ACPI_WMI_METHOD 0x2 /* GUID is a method */
111 * Data block is a string, and must be converted from ASCII to Unicode (output)
112 * or Unicode to ASCII (input)
114 #define ACPI_WMI_STRING 0x4
115 #define ACPI_WMI_EVENT 0x8 /* GUID is an event */
117 static int acpi_wmi_remove(struct acpi_device
*device
, int type
);
118 static int acpi_wmi_add(struct acpi_device
*device
);
120 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
121 const static struct acpi_device_id wmi_device_ids
[] = {
126 MODULE_DEVICE_TABLE(acpi
, wmi_device_ids
);
129 static struct acpi_driver acpi_wmi_driver
= {
131 .class = ACPI_WMI_CLASS
,
132 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
133 .ids
= wmi_device_ids
,
135 .ids
= "PNP0C14,pnp0c14",
139 .remove
= acpi_wmi_remove
,
144 * GUID parsing functions
148 * wmi_parse_hexbyte - Convert a ASCII hex number to a byte
149 * @src: Pointer to at least 2 characters to convert.
151 * Convert a two character ASCII hex string to a number.
153 * Return: 0-255 Success, the byte was parsed correctly
154 * -1 Error, an invalid character was supplied
156 static int wmi_parse_hexbyte(const u8
*src
)
158 unsigned int x
; /* For correct wrapping */
163 if (x
- '0' <= '9' - '0') {
165 } else if (x
- 'a' <= 'f' - 'a') {
167 } else if (x
- 'A' <= 'F' - 'A') {
176 if (x
- '0' <= '9' - '0')
177 return h
| (x
- '0');
178 if (x
- 'a' <= 'f' - 'a')
179 return h
| (x
- 'a' + 10);
180 if (x
- 'A' <= 'F' - 'A')
181 return h
| (x
- 'A' + 10);
186 * wmi_swap_bytes - Rearrange GUID bytes to match GUID binary
187 * @src: Memory block holding binary GUID (16 bytes)
188 * @dest: Memory block to hold byte swapped binary GUID (16 bytes)
190 * Byte swap a binary GUID to match it's real GUID value
192 static void wmi_swap_bytes(u8
*src
, u8
*dest
)
196 for (i
= 0; i
<= 3; i
++)
197 memcpy(dest
+ i
, src
+ (3 - i
), 1);
199 for (i
= 0; i
<= 1; i
++)
200 memcpy(dest
+ 4 + i
, src
+ (5 - i
), 1);
202 for (i
= 0; i
<= 1; i
++)
203 memcpy(dest
+ 6 + i
, src
+ (7 - i
), 1);
205 memcpy(dest
+ 8, src
+ 8, 8);
209 * wmi_parse_guid - Convert GUID from ASCII to binary
210 * @src: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
211 * @dest: Memory block to hold binary GUID (16 bytes)
213 * N.B. The GUID need not be NULL terminated.
215 * Return: 'true' @dest contains binary GUID
216 * 'false' @dest contents are undefined
218 static bool wmi_parse_guid(const u8
*src
, u8
*dest
)
220 static const int size
[] = { 4, 2, 2, 2, 6 };
223 if (src
[8] != '-' || src
[13] != '-' ||
224 src
[18] != '-' || src
[23] != '-')
227 for (j
= 0; j
< 5; j
++, src
++) {
228 for (i
= 0; i
< size
[j
]; i
++, src
+= 2, *dest
++ = v
) {
229 v
= wmi_parse_hexbyte(src
);
238 static bool find_guid(const char *guid_string
, struct wmi_block
**out
)
240 char tmp
[16], guid_input
[16];
241 struct wmi_block
*wblock
;
242 struct guid_block
*block
;
245 DEBUG(2, "find_guid called\n");
246 DEBUG(2, "Passing GUID to parser\n");
247 wmi_parse_guid(guid_string
, tmp
);
248 wmi_swap_bytes(tmp
, guid_input
);
250 list_for_each(p
, &wmi_blocks
.list
) {
251 wblock
= list_entry(p
, struct wmi_block
, list
);
252 block
= &wblock
->gblock
;
254 if (memcmp(block
->guid
, guid_input
, 16) == 0) {
255 DEBUG(2, "GUID found - returning block\n");
265 * Exported WMI functions
268 * wmi_acer_evaluate_method - Evaluate a WMI method
269 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
270 * @instance: Instance index
271 * @method_id: Method ID to call
272 * &in: Buffer containing input for the method call
273 * &out: Empty buffer to return the method results
275 * Convert a WMI method call to an ACPI one, and return the results
277 acpi_status
wmi_acer_evaluate_method(const char *guid_string
, u8 instance
,
278 u32 method_id
, const struct acpi_buffer
*in
, struct acpi_buffer
*out
)
280 struct guid_block
*block
= NULL
;
281 struct wmi_block
*wblock
= NULL
;
284 struct acpi_object_list input
;
285 union acpi_object params
[3];
286 char method
[4] = "WM";
288 if (!find_guid(guid_string
, &wblock
))
289 return AE_BAD_ADDRESS
;
291 block
= &wblock
->gblock
;
292 handle
= wblock
->handle
;
294 if (!block
->flags
& ACPI_WMI_METHOD
) {
295 DEBUG(2, "GUID is not a method\n");
299 if (block
->instance_count
< instance
)
300 return AE_BAD_PARAMETER
;
303 input
.pointer
= params
;
304 params
[0].type
= ACPI_TYPE_INTEGER
;
305 params
[0].integer
.value
= instance
;
306 params
[1].type
= ACPI_TYPE_INTEGER
;
307 params
[1].integer
.value
= method_id
;
312 if (block
->flags
& ACPI_WMI_STRING
) {
313 params
[2].type
= ACPI_TYPE_STRING
;
315 params
[2].type
= ACPI_TYPE_BUFFER
;
317 params
[2].buffer
.length
= in
->length
;
318 params
[2].buffer
.pointer
= in
->pointer
;
321 strncat(method
, block
->object_id
, 2);
322 DEBUG(2, "Object to call is %s\n", method
);
324 status
= acpi_evaluate_object(handle
, method
, &input
, out
);
328 EXPORT_SYMBOL_GPL(wmi_acer_evaluate_method
);
331 * wmi_query_block - Return contents of a WMI block
332 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
333 * @instance: Instance index
334 * &out: Empty buffer to return the contents of the data block to
336 * Return the contents of a data block to a buffer
338 acpi_status
wmi_acer_query_block(const char *guid_string
, u8 instance
,
339 struct acpi_buffer
*out
)
341 struct guid_block
*block
= NULL
;
342 struct wmi_block
*wblock
= NULL
;
345 struct acpi_object_list input
, wc_input
;
346 union acpi_object wc_params
[1], wq_params
[1];
347 char method
[4] = "WQ";
348 char wc_method
[4] = "WC";
350 if (guid_string
== NULL
|| out
== NULL
)
351 return AE_BAD_PARAMETER
;
353 if (!find_guid(guid_string
, &wblock
))
354 return AE_BAD_ADDRESS
;
356 block
= &wblock
->gblock
;
357 handle
= wblock
->handle
;
359 if (block
->instance_count
< instance
)
360 return AE_BAD_PARAMETER
;
362 /* Check GUID is a data block */
363 if (block
->flags
& (ACPI_WMI_EVENT
| ACPI_WMI_METHOD
))
364 return AE_BAD_ADDRESS
;
367 input
.pointer
= wq_params
;
368 wq_params
[0].type
= ACPI_TYPE_INTEGER
;
369 wq_params
[0].integer
.value
= instance
;
372 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
375 if (block
->flags
& ACPI_WMI_EXPENSIVE
) {
377 wc_input
.pointer
= wc_params
;
378 wc_params
[0].type
= ACPI_TYPE_INTEGER
;
379 wc_params
[0].integer
.value
= 1;
381 strncat(wc_method
, block
->object_id
, 2);
383 status
= acpi_evaluate_object(handle
, wc_method
,
386 if (ACPI_FAILURE(status
))
390 strncat(method
, block
->object_id
, 2);
391 DEBUG(2, "Object to call is %s\n", method
);
393 status
= acpi_evaluate_object(handle
, method
, NULL
, out
);
396 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
397 * the WQxx method failed - we should disable collection anyway
399 if (block
->flags
& ACPI_WMI_EXPENSIVE
) {
400 wc_params
[0].integer
.value
= 0;
401 status
= acpi_evaluate_object(handle
,
402 wc_method
, &wc_input
, NULL
);
407 EXPORT_SYMBOL_GPL(wmi_acer_query_block
);
410 * wmi_acer_set_block - Write to a WMI block
411 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
412 * @instance: Instance index
413 * &in: Buffer containing new values for the data block
415 * Write the contents of the input buffer to ACPI
417 acpi_status
wmi_acer_set_block(const char *guid_string
, u8 instance
,
418 const struct acpi_buffer
*in
)
420 struct guid_block
*block
= NULL
;
421 struct wmi_block
*wblock
= NULL
;
423 struct acpi_object_list input
;
424 union acpi_object params
[2];
425 char method
[4] = "WS";
427 if (guid_string
== NULL
|| in
== NULL
)
430 if (!find_guid(guid_string
, &wblock
))
431 return AE_BAD_ADDRESS
;
433 block
= &wblock
->gblock
;
434 handle
= wblock
->handle
;
436 if (block
->instance_count
< instance
)
437 return AE_BAD_PARAMETER
;
439 /* Check GUID is a data block */
440 if (block
->flags
& (ACPI_WMI_EVENT
| ACPI_WMI_METHOD
))
441 return AE_BAD_ADDRESS
;
444 input
.pointer
= params
;
445 params
[0].type
= ACPI_TYPE_INTEGER
;
446 params
[0].integer
.value
= instance
;
448 if (block
->flags
& ACPI_WMI_STRING
) {
449 params
[1].type
= ACPI_TYPE_STRING
;
451 params
[1].type
= ACPI_TYPE_BUFFER
;
453 params
[1].buffer
.length
= in
->length
;
454 params
[1].buffer
.pointer
= in
->pointer
;
456 strncat(method
, block
->object_id
, 2);
457 DEBUG(2, "Object to call is %s\n", method
);
459 return acpi_evaluate_object(handle
, method
, &input
, NULL
);
461 EXPORT_SYMBOL_GPL(wmi_acer_set_block
);
464 * wmi_install_notify_handler - Register handler for WMI events
465 * @handler: Function to handle notifications
466 * @data: Data to be returned to handler when event is fired
468 * Register a handler for events sent to the WMI-ACPI mapper device.
470 acpi_status
wmi_acer_install_notify_handler(wmi_notify_handler handler
, void *data
)
473 return AE_BAD_PARAMETER
;
475 if (!wmi_external_handler
)
476 return AE_ALREADY_ACQUIRED
;
478 wmi_external_handler
= handler
;
479 wmi_external_data
= data
;
483 EXPORT_SYMBOL_GPL(wmi_acer_install_notify_handler
);
486 * wmi_acer_uninstall_notify_handler - Unregister handler for WMI events
488 * Unregister handler for events sent to the WMI-ACPI mapper device.
490 acpi_status
wmi_acer_remove_notify_handler(void)
492 DEBUG(1, "Notifier triggered\n");
494 if (wmi_external_handler
) {
495 wmi_external_handler
= NULL
;
496 wmi_external_data
= NULL
;
501 EXPORT_SYMBOL_GPL(wmi_acer_remove_notify_handler
);
504 * wmi_acer_get_event_data - Get WMI data associated with an event
506 * @event - Event to find
507 * &out - Buffer to hold event data
509 * Returns extra data associated with an event in WMI.
511 acpi_status
wmi_acer_get_event_data(u32 event
, struct acpi_buffer
*out
)
513 struct acpi_object_list input
;
514 union acpi_object params
[1];
515 struct guid_block
*gblock
;
516 struct wmi_block
*wblock
;
520 input
.pointer
= params
;
521 params
[0].type
= ACPI_TYPE_INTEGER
;
522 params
[0].integer
.value
= event
;
524 list_for_each(p
, &wmi_blocks
.list
) {
525 wblock
= list_entry(p
, struct wmi_block
, list
);
526 gblock
= &wblock
->gblock
;
528 if ((gblock
->flags
& ACPI_WMI_EVENT
) &&
529 (gblock
->notify_id
== event
))
530 return acpi_evaluate_object(wblock
->handle
, "_WED",
536 EXPORT_SYMBOL(wmi_acer_get_event_data
);
539 * wmi_acer_has_guid - Check if a GUID is available
540 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
542 * Check if a given GUID is defined by _WDG
544 bool wmi_acer_has_guid(const char *guid_string
)
546 return find_guid(guid_string
, NULL
);
548 EXPORT_SYMBOL_GPL(wmi_acer_has_guid
);
551 * parse_wdg - Parse the _WDG method for the GUID data blocks
553 static __init acpi_status
parse_wdg(acpi_handle handle
)
555 struct acpi_buffer out
= {ACPI_ALLOCATE_BUFFER
, NULL
};
556 union acpi_object
*obj
;
557 struct guid_block
*gblock
;
558 struct wmi_block
*wblock
;
562 status
= acpi_evaluate_object(handle
, "_WDG", NULL
, &out
);
564 if (ACPI_FAILURE(status
))
567 obj
= (union acpi_object
*) out
.pointer
;
569 if (obj
->type
!= ACPI_TYPE_BUFFER
)
572 total
= obj
->buffer
.length
/ sizeof(struct guid_block
);
574 gblock
= kzalloc(obj
->buffer
.length
, GFP_KERNEL
);
578 memcpy(gblock
, obj
->buffer
.pointer
, obj
->buffer
.length
);
580 for (i
= 0; i
< total
; i
++) {
581 wblock
= kzalloc(sizeof(struct wmi_block
), GFP_KERNEL
);
585 wblock
->gblock
= gblock
[i
];
586 wblock
->handle
= handle
;
587 list_add_tail(&wblock
->list
, &wmi_blocks
.list
);
596 static void acpi_wmi_notify(acpi_handle handle
, u32 event
, void *data
)
598 struct guid_block
*block
;
599 struct wmi_block
*wblock
;
602 list_for_each(p
, &wmi_blocks
.list
) {
603 wblock
= list_entry(p
, struct wmi_block
, list
);
604 block
= &wblock
->gblock
;
606 if ((block
->flags
& ACPI_WMI_EVENT
) &&
607 block
->notify_id
== event
) {
608 if (wmi_external_handler
)
609 wmi_external_handler(event
, wmi_external_data
);
615 static int __init
acpi_wmi_add(struct acpi_device
*device
)
620 status
= acpi_install_notify_handler(device
->handle
, ACPI_DEVICE_NOTIFY
,
621 acpi_wmi_notify
, device
);
623 if (ACPI_FAILURE(status
)) {
624 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
625 "Error installing notify handler\n"));
629 status
= parse_wdg(device
->handle
);
630 if (ACPI_FAILURE(status
))
636 static int acpi_wmi_remove(struct acpi_device
*device
, int type
)
638 acpi_remove_notify_handler(device
->handle
, ACPI_DEVICE_NOTIFY
,
644 static int __init
acpi_wmi_init(void)
651 INIT_LIST_HEAD(&wmi_blocks
.list
);
653 result
= acpi_bus_register_driver(&acpi_wmi_driver
);
655 if (ACPI_FAILURE(result
))
656 printk(KERN_INFO PREFIX
"Interface device not found\n");
658 printk(KERN_INFO PREFIX
"Mapper loaded\n");
663 static void __exit
acpi_wmi_exit(void)
666 struct wmi_block
*wblock
;
668 acpi_bus_unregister_driver(&acpi_wmi_driver
);
670 list_for_each(p
, &wmi_blocks
.list
) {
671 wblock
= list_entry(p
, struct wmi_block
, list
);
673 list_del(&wblock
->list
);
677 printk(KERN_INFO PREFIX
"Mapper unloaded\n");
680 module_init(acpi_wmi_init
);
681 module_exit(acpi_wmi_exit
);