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>
42 #define ACPI_WMI_CLASS "wmi-acer"
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...) { \
52 printk(KERN_DEBUG PREFIX message);\
55 /* Workaround needed for older kernels */
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
);
76 unsigned char notify_id
;
77 unsigned char reserved
;
86 struct list_head list
;
87 struct guid_block gblock
;
91 static struct wmi_block wmi_blocks
;
93 static wmi_notify_handler wmi_external_handler
;
94 static void *wmi_external_data
;
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
[] = {
119 MODULE_DEVICE_TABLE(acpi
, wmi_device_ids
);
122 static struct acpi_driver acpi_wmi_driver
= {
124 .class = ACPI_WMI_CLASS
,
125 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
126 .ids
= wmi_device_ids
,
128 .ids
= "PNP0C14,pnp0c14",
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 */
156 if (x
- '0' <= '9' - '0') {
158 } else if (x
- 'a' <= 'f' - 'a') {
160 } else if (x
- 'A' <= 'F' - 'A') {
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);
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
)
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 };
216 if (src
[8] != '-' || src
[13] != '-' ||
217 src
[18] != '-' || src
[23] != '-')
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
);
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
;
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");
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
;
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");
292 if (block
->instance_count
< instance
)
293 return AE_BAD_PARAMETER
;
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
;
305 if (block
->flags
& ACPI_WMI_STRING
) {
306 params
[2].type
= ACPI_TYPE_STRING
;
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
);
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
, wc_handle
= NULL
;
338 struct acpi_object_list input
, wc_input
;
339 union acpi_object wc_params
[1], wq_params
[1];
340 char method
[4] = "WQ";
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
;
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
368 if (block
->flags
& ACPI_WMI_EXPENSIVE
) {
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 status
= acpi_get_handle(handle
, wc_method
, wc_handle
);
382 if (ACPI_SUCCESS(status
)) {
383 status
= acpi_evaluate_object(wc_handle
, NULL
,
386 if (ACPI_FAILURE(status
))
391 strncat(method
, block
->object_id
, 2);
392 DEBUG(2, "Object to call is %s\n", method
);
394 status
= acpi_evaluate_object(handle
, method
, NULL
, out
);
397 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
398 * the WQxx method failed - we should disable collection anyway
400 if (block
->flags
& ACPI_WMI_EXPENSIVE
) {
401 wc_params
[0].integer
.value
= 0;
402 status
= acpi_evaluate_object(handle
,
403 wc_method
, &wc_input
, NULL
);
408 EXPORT_SYMBOL_GPL(wmi_acer_query_block
);
411 * wmi_acer_set_block - Write to a WMI block
412 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
413 * @instance: Instance index
414 * &in: Buffer containing new values for the data block
416 * Write the contents of the input buffer to ACPI
418 acpi_status
wmi_acer_set_block(const char *guid_string
, u8 instance
,
419 const struct acpi_buffer
*in
)
421 struct guid_block
*block
= NULL
;
422 struct wmi_block
*wblock
= NULL
;
424 struct acpi_object_list input
;
425 union acpi_object params
[2];
426 char method
[4] = "WS";
428 if (guid_string
== NULL
|| in
== NULL
)
431 if (!find_guid(guid_string
, &wblock
))
432 return AE_BAD_ADDRESS
;
434 block
= &wblock
->gblock
;
435 handle
= wblock
->handle
;
437 if (block
->instance_count
< instance
)
438 return AE_BAD_PARAMETER
;
440 /* Check GUID is a data block */
441 if (block
->flags
& (ACPI_WMI_EVENT
| ACPI_WMI_METHOD
))
442 return AE_BAD_ADDRESS
;
445 input
.pointer
= params
;
446 params
[0].type
= ACPI_TYPE_INTEGER
;
447 params
[0].integer
.value
= instance
;
449 if (block
->flags
& ACPI_WMI_STRING
) {
450 params
[1].type
= ACPI_TYPE_STRING
;
452 params
[1].type
= ACPI_TYPE_BUFFER
;
454 params
[1].buffer
.length
= in
->length
;
455 params
[1].buffer
.pointer
= in
->pointer
;
457 strncat(method
, block
->object_id
, 2);
458 DEBUG(2, "Object to call is %s\n", method
);
460 return acpi_evaluate_object(handle
, method
, &input
, NULL
);
462 EXPORT_SYMBOL_GPL(wmi_acer_set_block
);
465 * wmi_install_notify_handler - Register handler for WMI events
466 * @handler: Function to handle notifications
467 * @data: Data to be returned to handler when event is fired
469 * Register a handler for events sent to the WMI-ACPI mapper device.
471 acpi_status
wmi_acer_install_notify_handler(wmi_notify_handler handler
, void *data
)
474 return AE_BAD_PARAMETER
;
476 if (!wmi_external_handler
)
477 return AE_ALREADY_ACQUIRED
;
479 wmi_external_handler
= handler
;
480 wmi_external_data
= data
;
484 EXPORT_SYMBOL_GPL(wmi_acer_install_notify_handler
);
487 * wmi_acer_uninstall_notify_handler - Unregister handler for WMI events
489 * Unregister handler for events sent to the WMI-ACPI mapper device.
491 acpi_status
wmi_acer_remove_notify_handler(void)
493 DEBUG(1, "Notifier triggered\n");
495 if (wmi_external_handler
) {
496 wmi_external_handler
= NULL
;
497 wmi_external_data
= NULL
;
502 EXPORT_SYMBOL_GPL(wmi_acer_remove_notify_handler
);
505 * wmi_acer_get_event_data - Get WMI data associated with an event
507 * @event - Event to find
508 * &out - Buffer to hold event data
510 * Returns extra data associated with an event in WMI.
512 acpi_status
wmi_acer_get_event_data(u32 event
, struct acpi_buffer
*out
)
514 struct acpi_object_list input
;
515 union acpi_object params
[1];
516 struct guid_block
*gblock
;
517 struct wmi_block
*wblock
;
521 input
.pointer
= params
;
522 params
[0].type
= ACPI_TYPE_INTEGER
;
523 params
[0].integer
.value
= event
;
525 list_for_each(p
, &wmi_blocks
.list
) {
526 wblock
= list_entry(p
, struct wmi_block
, list
);
527 gblock
= &wblock
->gblock
;
529 if ((gblock
->flags
& ACPI_WMI_EVENT
) &&
530 (gblock
->notify_id
== event
))
531 return acpi_evaluate_object(wblock
->handle
, "_WED",
537 EXPORT_SYMBOL(wmi_acer_get_event_data
);
540 * wmi_acer_has_guid - Check if a GUID is available
541 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
543 * Check if a given GUID is defined by _WDG
545 bool wmi_acer_has_guid(const char *guid_string
)
547 return find_guid(guid_string
, NULL
);
549 EXPORT_SYMBOL_GPL(wmi_acer_has_guid
);
552 * parse_wdg - Parse the _WDG method for the GUID data blocks
554 static __init acpi_status
parse_wdg(acpi_handle handle
)
556 struct acpi_buffer out
= {ACPI_ALLOCATE_BUFFER
, NULL
};
557 union acpi_object
*obj
;
558 struct guid_block
*gblock
;
559 struct wmi_block
*wblock
;
563 status
= acpi_evaluate_object(handle
, "_WDG", NULL
, &out
);
565 if (ACPI_FAILURE(status
))
568 obj
= (union acpi_object
*) out
.pointer
;
570 if (obj
->type
!= ACPI_TYPE_BUFFER
)
573 total
= obj
->buffer
.length
/ sizeof(struct guid_block
);
575 gblock
= kzalloc(obj
->buffer
.length
, GFP_KERNEL
);
579 memcpy(gblock
, obj
->buffer
.pointer
, obj
->buffer
.length
);
581 for (i
= 0; i
< total
; i
++) {
582 wblock
= kzalloc(sizeof(struct wmi_block
), GFP_KERNEL
);
586 wblock
->gblock
= gblock
[i
];
587 wblock
->handle
= handle
;
588 list_add_tail(&wblock
->list
, &wmi_blocks
.list
);
597 static void acpi_wmi_notify(acpi_handle handle
, u32 event
, void *data
)
599 struct guid_block
*block
;
600 struct wmi_block
*wblock
;
603 list_for_each(p
, &wmi_blocks
.list
) {
604 wblock
= list_entry(p
, struct wmi_block
, list
);
605 block
= &wblock
->gblock
;
607 if ((block
->flags
& ACPI_WMI_EVENT
) &&
608 block
->notify_id
== event
) {
609 if (wmi_external_handler
)
610 wmi_external_handler(event
, wmi_external_data
);
616 static int __init
acpi_wmi_add(struct acpi_device
*device
)
621 status
= acpi_install_notify_handler(device
->handle
, ACPI_DEVICE_NOTIFY
,
622 acpi_wmi_notify
, device
);
624 if (ACPI_FAILURE(status
)) {
625 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
626 "Error installing notify handler\n"));
630 status
= parse_wdg(device
->handle
);
631 if (ACPI_FAILURE(status
))
637 static int acpi_wmi_remove(struct acpi_device
*device
, int type
)
639 acpi_remove_notify_handler(device
->handle
, ACPI_DEVICE_NOTIFY
,
645 static int __init
acpi_wmi_init(void)
652 INIT_LIST_HEAD(&wmi_blocks
.list
);
654 result
= acpi_bus_register_driver(&acpi_wmi_driver
);
656 if (ACPI_FAILURE(result
)) {
657 printk(KERN_INFO PREFIX
"Interface device not found\n");
661 printk(KERN_INFO PREFIX
"Mapper loaded\n");
666 static void __exit
acpi_wmi_exit(void)
668 struct list_head
*p
, *tmp
;
669 struct wmi_block
*wblock
;
671 acpi_bus_unregister_driver(&acpi_wmi_driver
);
673 list_for_each_safe(p
, tmp
, &wmi_blocks
.list
) {
674 wblock
= list_entry(p
, struct wmi_block
, list
);
680 printk(KERN_INFO PREFIX
"Mapper unloaded\n");
683 module_init(acpi_wmi_init
);
684 module_exit(acpi_wmi_exit
);