2 * WMI to ACPI mapping driver
4 * Copyright (C) 2007 Carlos Corbacho <cathectic@gmail.com>
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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31 * 1) Handle method & data blocks flagged as WMIACPI_FLAG_STRING
33 * The MS spec says that we should translate the input from "UNICODE to ASCIIZ"
34 * and the output from "ASCIIZ to UNICODE". But by UNICODE, they probably mean
35 * UTF-16, where as we are using UTF-8/ ASCII here - so, what to do? Converting
36 * to UTF-16 is probably pointless, since most of the clients of this mapper
37 * will be Linux drivers using UTF-8/ ASCII anyway, not UTF-16.
39 * So, for the moment, the mapper should just convert input from UTF-8 to ASCII
41 * 2) Handle WMIACPI_FLAG_EVENT - What should we do with the events? We would
42 * also need to handle WExx (expensive events)
45 #include <linux/kernel.h>
46 #include <linux/module.h>
47 #include <linux/init.h>
48 #include <linux/types.h>
49 #include <acpi/acpi_drivers.h>
50 #include <linux/version.h>
54 #define ACPI_WMI_CLASS "wmi"
57 #define PREFIX "ACPI: WMI: "
58 #define WMI_ERR KERN_ERR PREFIX
59 #define WMI_NOTICE KERN_NOTICE PREFIX
60 #define WMI_INFO KERN_INFO PREFIX
62 #define DEBUG(level, message...) { \
64 printk(KERN_DEBUG PREFIX message);\
67 /* Workaround needed for older kernels */
72 MODULE_AUTHOR("Carlos Corbacho");
73 MODULE_DESCRIPTION("WMI ACPI Interface Driver");
74 MODULE_LICENSE("GPL");
84 unsigned char notification_value
;
85 unsigned char reserved
;
94 struct guid_block_t
*pointer
;
98 static struct guid_list guids
;
99 static acpi_handle acpi_wmi_handle
;
102 module_param(debug
, int, 0664);
103 MODULE_PARM_DESC(debug
, "Debugging verbosity level (0=least 2=most)");
106 * If the GUID is a datablock, we must enable and explicitily disable data
107 * collection. If the GUID is an event, we must arm and reenable the event if
110 #define WMIACPI_FLAG_EXPENSIVE 0x1
111 #define WMIACPI_FLAG_METHOD 0x2 /* GUID is a method */
114 * Data block is a string, and must be converted from ASCII to Unicode (output)
115 * or Unicode to Ascii (input)
117 #define WMIACPI_FLAG_STRING 0x4
118 #define WMIACPI_FLAG_EVENT 0x8 /* GUID is an event */
120 static int acpi_wmi_remove(struct acpi_device
*device
, int type
);
121 static int acpi_wmi_add(struct acpi_device
*device
);
123 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
124 const static struct acpi_device_id wmi_device_ids
[] = {
129 MODULE_DEVICE_TABLE(acpi
, wmi_device_ids
);
132 static struct acpi_driver acpi_wmi_driver
= {
134 .class = ACPI_WMI_CLASS
,
135 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
136 .ids
= wmi_device_ids
,
142 .remove
= acpi_wmi_remove
,
147 * GUID parsing functions
151 * wmi_parse_hexbyte - Convert a ASCII hex number to a byte
152 * @src: Pointer to at least 2 characters to convert.
154 * Convert a two character ASCII hex string to a number.
156 * Return: 0-255 Success, the byte was parsed correctly
157 * -1 Error, an invalid character was supplied
159 static int wmi_parse_hexbyte (const u8
*src
)
161 unsigned int x
; /* For correct wrapping */
165 if ((x
= src
[0] - '0') <= '9'-'0') h
= x
;
166 else if ((x
= src
[0] - 'a') <= 'f'-'a') h
= x
+10;
167 else if ((x
= src
[0] - 'A') <= 'F'-'A') h
= x
+10;
172 if ((x
= src
[1] - '0') <= '9'-'0') return h
| x
;
173 if ((x
= src
[1] - 'a') <= 'f'-'a') return h
| (x
+10);
174 if ((x
= src
[1] - 'A') <= 'F'-'A') return h
| (x
+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 if ((v
= wmi_parse_hexbyte (src
)) < 0)
228 static bool find_guid(const char *guid_string
, struct guid_block_t
**out
)
230 char tmp
[16], guid_input
[16];
231 struct guid_block_t
*block
;
234 DEBUG(2, "find_guid called\n");
235 DEBUG(2, "Passing GUID to parser\n");
236 wmi_parse_guid(guid_string
, tmp
);
237 wmi_swap_bytes(tmp
, guid_input
);
239 for (i
= 0; i
< guids
.total
; i
++) {
240 block
= guids
.pointer
+ i
;
242 if (memcmp(block
->guid
, guid_input
, 16) == 0) {
243 DEBUG(2, "GUID found - returning block...\n");
253 * Externally callable WMI functions
255 acpi_status
wmi_acer_evaluate_method(const char *guid_string
, u32 instance
,
256 u32 method_id
, const struct acpi_buffer
*in
, struct acpi_buffer
*out
)
258 struct guid_block_t
*block
= NULL
;
260 struct acpi_object_list input
;
261 union acpi_object wm_params
[3];
262 char method
[4] = "WM";
264 if (!find_guid(guid_string
, &block
))
265 return AE_BAD_ADDRESS
;
267 if (!block
->flags
& WMIACPI_FLAG_METHOD
) {
268 DEBUG(2, "GUID is not a method\n");
273 input
.pointer
= wm_params
;
274 wm_params
[0].type
= ACPI_TYPE_INTEGER
;
275 wm_params
[0].integer
.value
= instance
;
276 wm_params
[1].type
= ACPI_TYPE_INTEGER
;
277 wm_params
[1].integer
.value
= method_id
;
281 wm_params
[2].type
= ACPI_TYPE_BUFFER
;
282 wm_params
[2].buffer
.length
= in
->length
;
283 wm_params
[2].buffer
.pointer
= in
->pointer
;
286 strncat(method
, block
->object_id
, 2);
287 strcat(method
, "\0");
288 DEBUG(2, "Object to call is %s\n", method
);
290 status
= acpi_evaluate_object(acpi_wmi_handle
, method
, &input
, out
);
292 if (block
->flags
& WMIACPI_FLAG_STRING
) {
293 /* Convert output from ASCIIZ to Unicode */
294 return AE_NOT_IMPLEMENTED
;
299 EXPORT_SYMBOL(wmi_acer_evaluate_method
);
301 acpi_status
wmi_acer_query_block(const char *guid_string
, u32 instance
,
302 u32 method_id
, struct acpi_buffer
*out
)
304 struct guid_block_t
*block
= NULL
;
306 struct acpi_object_list input
, wc_input
;
307 union acpi_object wc_params
[1], wq_params
[1];
308 char method
[4] = "WQ";
309 char wc_method
[4] = "WC";
311 if (guid_string
== NULL
|| out
== NULL
)
312 return AE_BAD_PARAMETER
;
314 if (!find_guid(guid_string
, &block
))
315 return AE_BAD_ADDRESS
;
317 /* Check GUID is a data block */
318 if ((block
->flags
& (WMIACPI_FLAG_EVENT
| WMIACPI_FLAG_METHOD
)) > 0)
319 return AE_BAD_ADDRESS
;
322 input
.pointer
= wq_params
;
323 wq_params
[0].type
= ACPI_TYPE_INTEGER
;
324 wq_params
[0].integer
.value
= instance
;
327 * If WMIACPI_FLAG_EXPENSIVE, call the relevant WCxx method first to
330 /* FIXME - should INTEGER be STRING? */
331 if (block
->flags
& WMIACPI_FLAG_EXPENSIVE
) {
333 wc_input
.pointer
= wc_params
;
334 wc_params
[0].type
= ACPI_TYPE_INTEGER
;
335 wc_params
[0].integer
.value
= instance
;
337 strncat(wc_method
, block
->object_id
, 2);
338 strcat(wc_method
, "\0");
340 status
= acpi_evaluate_object(acpi_wmi_handle
, wc_method
,
343 if (ACPI_FAILURE(status
))
347 strncat(method
, block
->object_id
, 2);
348 strcat(method
, "\0");
349 DEBUG(2, "Object to call is %s\n", method
);
351 status
= acpi_evaluate_object(acpi_wmi_handle
, method
, NULL
, out
);
354 * If WMIACPI_FLAG_EXPENSIVE, call the relevant WCxx method, even if
355 * the WQxx method failed - we should disable collection anyway
357 if ((block
->flags
& WMIACPI_FLAG_EXPENSIVE
) > 0) {
358 wc_params
[0].integer
.value
= 0x0;
359 status
= acpi_evaluate_object(acpi_wmi_handle
,
360 wc_method
, &wc_input
, NULL
);
363 if (ACPI_SUCCESS(status
)) {
364 /* Convert output from ASCIIZ to Unicode */
365 if ((block
->flags
& WMIACPI_FLAG_STRING
) > 0)
366 return AE_NOT_IMPLEMENTED
;
371 EXPORT_SYMBOL(wmi_acer_query_block
);
373 acpi_status
wmi_acer_set_block(const char *guid_string
, u32 instance
, u32 method_id
,
374 const struct acpi_buffer
*in
)
376 struct guid_block_t
*block
= NULL
;
377 struct acpi_object_list input
;
378 union acpi_object params
[2];
379 char method
[4] = "WS";
381 if (guid_string
== NULL
|| in
== NULL
)
384 if (!find_guid(guid_string
, &block
))
385 return AE_BAD_ADDRESS
;
387 /* Check GUID is a data block */
388 if ((block
->flags
& (WMIACPI_FLAG_EVENT
| WMIACPI_FLAG_METHOD
)) > 0)
389 return AE_BAD_ADDRESS
;
392 input
.pointer
= params
;
393 params
[0].type
= ACPI_TYPE_INTEGER
;
394 params
[0].integer
.value
= instance
;
395 params
[1].type
= ACPI_TYPE_BUFFER
;
396 params
[1].buffer
.length
= in
->length
;
397 params
[1].buffer
.pointer
= in
->pointer
;
399 /* Convert input from Unicode to ASCIIZ */
400 if (block
->flags
& WMIACPI_FLAG_STRING
)
401 return AE_NOT_IMPLEMENTED
;
403 strncat(method
, block
->object_id
, 2);
404 strcat(method
, "\0");
405 DEBUG(2, "Object to call is %s\n", method
);
407 return acpi_evaluate_object(acpi_wmi_handle
, method
, &input
, NULL
);
409 EXPORT_SYMBOL(wmi_acer_set_block
);
411 bool wmi_acer_has_guid(char *guid_string
)
413 return find_guid(guid_string
, NULL
);
415 EXPORT_SYMBOL(wmi_acer_has_guid
);
418 * Parse the _WDG method for the data blocks
420 static int parse_wdg(void)
422 struct acpi_buffer out
= {ACPI_ALLOCATE_BUFFER
, NULL
};
423 union acpi_object
*obj
;
426 status
= acpi_evaluate_object(acpi_wmi_handle
, "_WDG", NULL
, &out
);
428 if (ACPI_FAILURE(status
))
431 obj
= (union acpi_object
*) out
.pointer
;
433 if (obj
->type
!= ACPI_TYPE_BUFFER
)
436 guids
.pointer
= (struct guid_block_t
*) obj
->buffer
.pointer
;
437 guids
.total
= obj
->buffer
.length
/ sizeof(struct guid_block_t
);
442 static void acpi_wmi_notify(acpi_handle handle
, u32 event
, void *data
)
445 struct guid_block_t
*block
;
447 DEBUG(1, "Notifier triggered\n");
449 for (i
= 0; i
< guids
.total
; i
++) {
450 block
= guids
.pointer
+ i
;
452 /* FIXME - the last comparison may be broken */
453 if (block
->flags
& WMIACPI_FLAG_EVENT
&&
454 block
->notification_value
== event
) {
455 /* Should we generate an event? And how do we pass the results of _WED? */
460 static int acpi_wmi_add(struct acpi_device
*device
)
464 acpi_wmi_handle
= device
->handle
;
466 status
= acpi_install_notify_handler(device
->handle
, ACPI_DEVICE_NOTIFY
,
467 acpi_wmi_notify
, device
);
469 if (ACPI_FAILURE(status
)) {
470 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
471 "Error installing notify handler\n"));
480 static int acpi_wmi_remove(struct acpi_device
*device
, int type
)
482 acpi_remove_notify_handler(device
->handle
, ACPI_DEVICE_NOTIFY
,
488 static int __init
acpi_wmi_init(void)
495 result
= acpi_bus_register_driver(&acpi_wmi_driver
);
497 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
499 /* Try again with the alternate _HID */
500 acpi_wmi_driver
.ids
= "PNP0C14";
501 result
= acpi_bus_register_driver(&acpi_wmi_driver
);
508 printk(KERN_INFO PREFIX
"Interface device found\n");
509 printk(KERN_INFO PREFIX
"Mapper loaded\n");
514 static void __exit
acpi_wmi_exit(void)
516 acpi_bus_unregister_driver(&acpi_wmi_driver
);
517 printk(KERN_INFO PREFIX
"Mapper unloaded\n");
520 module_init(acpi_wmi_init
);
521 module_exit(acpi_wmi_exit
);