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
);
85 static const struct acpi_device_id wmi_device_ids
[] = {
90 MODULE_DEVICE_TABLE(acpi
, wmi_device_ids
);
92 static struct acpi_driver acpi_wmi_driver
= {
94 .class = ACPI_WMI_CLASS
,
95 .ids
= wmi_device_ids
,
98 .remove
= acpi_wmi_remove
,
103 * GUID parsing functions
107 * wmi_parse_hexbyte - Convert a ASCII hex number to a byte
108 * @src: Pointer to at least 2 characters to convert.
110 * Convert a two character ASCII hex string to a number.
112 * Return: 0-255 Success, the byte was parsed correctly
113 * -1 Error, an invalid character was supplied
115 static int wmi_parse_hexbyte(const u8
*src
)
117 unsigned int x
; /* For correct wrapping */
122 if (x
- '0' <= '9' - '0') {
124 } else if (x
- 'a' <= 'f' - 'a') {
126 } else if (x
- 'A' <= 'F' - 'A') {
135 if (x
- '0' <= '9' - '0')
136 return h
| (x
- '0');
137 if (x
- 'a' <= 'f' - 'a')
138 return h
| (x
- 'a' + 10);
139 if (x
- 'A' <= 'F' - 'A')
140 return h
| (x
- 'A' + 10);
145 * wmi_swap_bytes - Rearrange GUID bytes to match GUID binary
146 * @src: Memory block holding binary GUID (16 bytes)
147 * @dest: Memory block to hold byte swapped binary GUID (16 bytes)
149 * Byte swap a binary GUID to match it's real GUID value
151 static void wmi_swap_bytes(u8
*src
, u8
*dest
)
155 for (i
= 0; i
<= 3; i
++)
156 memcpy(dest
+ i
, src
+ (3 - i
), 1);
158 for (i
= 0; i
<= 1; i
++)
159 memcpy(dest
+ 4 + i
, src
+ (5 - i
), 1);
161 for (i
= 0; i
<= 1; i
++)
162 memcpy(dest
+ 6 + i
, src
+ (7 - i
), 1);
164 memcpy(dest
+ 8, src
+ 8, 8);
168 * wmi_parse_guid - Convert GUID from ASCII to binary
169 * @src: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
170 * @dest: Memory block to hold binary GUID (16 bytes)
172 * N.B. The GUID need not be NULL terminated.
174 * Return: 'true' @dest contains binary GUID
175 * 'false' @dest contents are undefined
177 static bool wmi_parse_guid(const u8
*src
, u8
*dest
)
179 static const int size
[] = { 4, 2, 2, 2, 6 };
182 if (src
[8] != '-' || src
[13] != '-' ||
183 src
[18] != '-' || src
[23] != '-')
186 for (j
= 0; j
< 5; j
++, src
++) {
187 for (i
= 0; i
< size
[j
]; i
++, src
+= 2, *dest
++ = v
) {
188 v
= wmi_parse_hexbyte(src
);
197 static bool find_guid(const char *guid_string
, struct wmi_block
**out
)
199 char tmp
[16], guid_input
[16];
200 struct wmi_block
*wblock
;
201 struct guid_block
*block
;
204 wmi_parse_guid(guid_string
, tmp
);
205 wmi_swap_bytes(tmp
, guid_input
);
207 list_for_each(p
, &wmi_blocks
.list
) {
208 wblock
= list_entry(p
, struct wmi_block
, list
);
209 block
= &wblock
->gblock
;
211 if (memcmp(block
->guid
, guid_input
, 16) == 0) {
221 * Exported WMI functions
224 * wmi_evaluate_method - Evaluate a WMI method
225 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
226 * @instance: Instance index
227 * @method_id: Method ID to call
228 * &in: Buffer containing input for the method call
229 * &out: Empty buffer to return the method results
231 * Call an ACPI-WMI method
233 acpi_status
wmi_evaluate_method(const char *guid_string
, u8 instance
,
234 u32 method_id
, const struct acpi_buffer
*in
, struct acpi_buffer
*out
)
236 struct guid_block
*block
= NULL
;
237 struct wmi_block
*wblock
= NULL
;
240 struct acpi_object_list input
;
241 union acpi_object params
[3];
242 char method
[4] = "WM";
244 if (!find_guid(guid_string
, &wblock
))
245 return AE_BAD_ADDRESS
;
247 block
= &wblock
->gblock
;
248 handle
= wblock
->handle
;
250 if (!(block
->flags
& ACPI_WMI_METHOD
))
253 if (block
->instance_count
< instance
)
254 return AE_BAD_PARAMETER
;
257 input
.pointer
= params
;
258 params
[0].type
= ACPI_TYPE_INTEGER
;
259 params
[0].integer
.value
= instance
;
260 params
[1].type
= ACPI_TYPE_INTEGER
;
261 params
[1].integer
.value
= method_id
;
266 if (block
->flags
& ACPI_WMI_STRING
) {
267 params
[2].type
= ACPI_TYPE_STRING
;
269 params
[2].type
= ACPI_TYPE_BUFFER
;
271 params
[2].buffer
.length
= in
->length
;
272 params
[2].buffer
.pointer
= in
->pointer
;
275 strncat(method
, block
->object_id
, 2);
277 status
= acpi_evaluate_object(handle
, method
, &input
, out
);
281 EXPORT_SYMBOL_GPL(wmi_evaluate_method
);
284 * wmi_query_block - Return contents of a WMI block
285 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
286 * @instance: Instance index
287 * &out: Empty buffer to return the contents of the data block to
289 * Return the contents of an ACPI-WMI data block to a buffer
291 acpi_status
wmi_query_block(const char *guid_string
, u8 instance
,
292 struct acpi_buffer
*out
)
294 struct guid_block
*block
= NULL
;
295 struct wmi_block
*wblock
= NULL
;
297 acpi_status status
, wc_status
= AE_ERROR
;
298 struct acpi_object_list input
, wc_input
;
299 union acpi_object wc_params
[1], wq_params
[1];
301 char wc_method
[4] = "WC";
303 if (!guid_string
|| !out
)
304 return AE_BAD_PARAMETER
;
306 if (!find_guid(guid_string
, &wblock
))
307 return AE_BAD_ADDRESS
;
309 block
= &wblock
->gblock
;
310 handle
= wblock
->handle
;
312 if (block
->instance_count
< instance
)
313 return AE_BAD_PARAMETER
;
315 /* Check GUID is a data block */
316 if (block
->flags
& (ACPI_WMI_EVENT
| ACPI_WMI_METHOD
))
317 return AE_BAD_ADDRESS
;
320 input
.pointer
= wq_params
;
321 wq_params
[0].type
= ACPI_TYPE_INTEGER
;
322 wq_params
[0].integer
.value
= instance
;
325 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
328 if (block
->flags
& ACPI_WMI_EXPENSIVE
) {
330 wc_input
.pointer
= wc_params
;
331 wc_params
[0].type
= ACPI_TYPE_INTEGER
;
332 wc_params
[0].integer
.value
= 1;
334 strncat(wc_method
, block
->object_id
, 2);
337 * Some GUIDs break the specification by declaring themselves
338 * expensive, but have no corresponding WCxx method. So we
339 * should not fail if this happens.
341 wc_status
= acpi_evaluate_object(handle
, wc_method
,
345 strcpy(method
, "WQ");
346 strncat(method
, block
->object_id
, 2);
348 status
= acpi_evaluate_object(handle
, method
, NULL
, out
);
351 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
352 * the WQxx method failed - we should disable collection anyway.
354 if ((block
->flags
& ACPI_WMI_EXPENSIVE
) && wc_status
) {
355 wc_params
[0].integer
.value
= 0;
356 status
= acpi_evaluate_object(handle
,
357 wc_method
, &wc_input
, NULL
);
362 EXPORT_SYMBOL_GPL(wmi_query_block
);
365 * wmi_set_block - Write to a WMI block
366 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
367 * @instance: Instance index
368 * &in: Buffer containing new values for the data block
370 * Write the contents of the input buffer to an ACPI-WMI data block
372 acpi_status
wmi_set_block(const char *guid_string
, u8 instance
,
373 const struct acpi_buffer
*in
)
375 struct guid_block
*block
= NULL
;
376 struct wmi_block
*wblock
= NULL
;
378 struct acpi_object_list input
;
379 union acpi_object params
[2];
380 char method
[4] = "WS";
382 if (!guid_string
|| !in
)
385 if (!find_guid(guid_string
, &wblock
))
386 return AE_BAD_ADDRESS
;
388 block
= &wblock
->gblock
;
389 handle
= wblock
->handle
;
391 if (block
->instance_count
< instance
)
392 return AE_BAD_PARAMETER
;
394 /* Check GUID is a data block */
395 if (block
->flags
& (ACPI_WMI_EVENT
| ACPI_WMI_METHOD
))
396 return AE_BAD_ADDRESS
;
399 input
.pointer
= params
;
400 params
[0].type
= ACPI_TYPE_INTEGER
;
401 params
[0].integer
.value
= instance
;
403 if (block
->flags
& ACPI_WMI_STRING
) {
404 params
[1].type
= ACPI_TYPE_STRING
;
406 params
[1].type
= ACPI_TYPE_BUFFER
;
408 params
[1].buffer
.length
= in
->length
;
409 params
[1].buffer
.pointer
= in
->pointer
;
411 strncat(method
, block
->object_id
, 2);
413 return acpi_evaluate_object(handle
, method
, &input
, NULL
);
415 EXPORT_SYMBOL_GPL(wmi_set_block
);
418 * wmi_install_notify_handler - Register handler for WMI events
419 * @handler: Function to handle notifications
420 * @data: Data to be returned to handler when event is fired
422 * Register a handler for events sent to the ACPI-WMI mapper device.
424 acpi_status
wmi_install_notify_handler(const char *guid
,
425 wmi_notify_handler handler
, void *data
)
427 struct wmi_block
*block
;
429 if (!guid
|| !handler
)
430 return AE_BAD_PARAMETER
;
432 find_guid(guid
, &block
);
437 return AE_ALREADY_ACQUIRED
;
439 block
->handler
= handler
;
440 block
->handler_data
= data
;
444 EXPORT_SYMBOL_GPL(wmi_install_notify_handler
);
447 * wmi_uninstall_notify_handler - Unregister handler for WMI events
449 * Unregister handler for events sent to the ACPI-WMI mapper device.
451 acpi_status
wmi_remove_notify_handler(const char *guid
)
453 struct wmi_block
*block
;
456 return AE_BAD_PARAMETER
;
458 find_guid(guid
, &block
);
463 return AE_NULL_ENTRY
;
465 block
->handler
= NULL
;
466 block
->handler_data
= NULL
;
470 EXPORT_SYMBOL_GPL(wmi_remove_notify_handler
);
473 * wmi_get_event_data - Get WMI data associated with an event
475 * @event - Event to find
476 * &out - Buffer to hold event data
478 * Returns extra data associated with an event in WMI.
480 acpi_status
wmi_get_event_data(u32 event
, struct acpi_buffer
*out
)
482 struct acpi_object_list input
;
483 union acpi_object params
[1];
484 struct guid_block
*gblock
;
485 struct wmi_block
*wblock
;
489 input
.pointer
= params
;
490 params
[0].type
= ACPI_TYPE_INTEGER
;
491 params
[0].integer
.value
= event
;
493 list_for_each(p
, &wmi_blocks
.list
) {
494 wblock
= list_entry(p
, struct wmi_block
, list
);
495 gblock
= &wblock
->gblock
;
497 if ((gblock
->flags
& ACPI_WMI_EVENT
) &&
498 (gblock
->notify_id
== event
))
499 return acpi_evaluate_object(wblock
->handle
, "_WED",
505 EXPORT_SYMBOL_GPL(wmi_get_event_data
);
508 * wmi_has_guid - Check if a GUID is available
509 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
511 * Check if a given GUID is defined by _WDG
513 bool wmi_has_guid(const char *guid_string
)
515 return find_guid(guid_string
, NULL
);
517 EXPORT_SYMBOL_GPL(wmi_has_guid
);
520 * Parse the _WDG method for the GUID data blocks
522 static __init acpi_status
parse_wdg(acpi_handle handle
)
524 struct acpi_buffer out
= {ACPI_ALLOCATE_BUFFER
, NULL
};
525 union acpi_object
*obj
;
526 struct guid_block
*gblock
;
527 struct wmi_block
*wblock
;
531 status
= acpi_evaluate_object(handle
, "_WDG", NULL
, &out
);
533 if (ACPI_FAILURE(status
))
536 obj
= (union acpi_object
*) out
.pointer
;
538 if (obj
->type
!= ACPI_TYPE_BUFFER
)
541 total
= obj
->buffer
.length
/ sizeof(struct guid_block
);
543 gblock
= kzalloc(obj
->buffer
.length
, GFP_KERNEL
);
547 memcpy(gblock
, obj
->buffer
.pointer
, obj
->buffer
.length
);
549 for (i
= 0; i
< total
; i
++) {
550 wblock
= kzalloc(sizeof(struct wmi_block
), GFP_KERNEL
);
554 wblock
->gblock
= gblock
[i
];
555 wblock
->handle
= handle
;
556 list_add_tail(&wblock
->list
, &wmi_blocks
.list
);
566 * WMI can have EmbeddedControl access regions. In which case, we just want to
567 * hand these off to the EC driver.
570 acpi_wmi_ec_space_handler(u32 function
, acpi_physical_address address
,
571 u32 bits
, acpi_integer
* value
,
572 void *handler_context
, void *region_context
)
574 int result
= 0, i
= 0;
577 if ((address
> 0xFF) || !value
)
578 return AE_BAD_PARAMETER
;
580 if (function
!= ACPI_READ
&& function
!= ACPI_WRITE
)
581 return AE_BAD_PARAMETER
;
584 return AE_BAD_PARAMETER
;
586 if (function
== ACPI_READ
) {
587 result
= ec_read(address
, &temp
);
588 (*value
) |= ((acpi_integer
)temp
) << i
;
590 temp
= 0xff & ((*value
) >> i
);
591 result
= ec_write(address
, temp
);
596 return AE_BAD_PARAMETER
;
609 static void acpi_wmi_notify(acpi_handle handle
, u32 event
, void *data
)
611 struct guid_block
*block
;
612 struct wmi_block
*wblock
;
614 struct acpi_device
*device
= data
;
616 list_for_each(p
, &wmi_blocks
.list
) {
617 wblock
= list_entry(p
, struct wmi_block
, list
);
618 block
= &wblock
->gblock
;
620 if ((block
->flags
& ACPI_WMI_EVENT
) &&
621 (block
->notify_id
== event
)) {
623 wblock
->handler(event
, wblock
->handler_data
);
625 acpi_bus_generate_netlink_event(
626 device
->pnp
.device_class
, device
->dev
.bus_id
,
633 static int acpi_wmi_remove(struct acpi_device
*device
, int type
)
635 acpi_remove_notify_handler(device
->handle
, ACPI_DEVICE_NOTIFY
,
638 acpi_remove_address_space_handler(device
->handle
,
639 ACPI_ADR_SPACE_EC
, &acpi_wmi_ec_space_handler
);
644 static int __init
acpi_wmi_add(struct acpi_device
*device
)
649 status
= acpi_install_notify_handler(device
->handle
, ACPI_DEVICE_NOTIFY
,
650 acpi_wmi_notify
, device
);
651 if (ACPI_FAILURE(status
)) {
652 printk(KERN_ERR PREFIX
"Error installing notify handler\n");
656 status
= acpi_install_address_space_handler(device
->handle
,
658 &acpi_wmi_ec_space_handler
,
660 if (ACPI_FAILURE(status
))
663 status
= parse_wdg(device
->handle
);
664 if (ACPI_FAILURE(status
)) {
665 printk(KERN_ERR PREFIX
"Error installing EC region handler\n");
672 static int __init
acpi_wmi_init(void)
676 INIT_LIST_HEAD(&wmi_blocks
.list
);
681 result
= acpi_bus_register_driver(&acpi_wmi_driver
);
684 printk(KERN_INFO PREFIX
"Error loading mapper\n");
686 printk(KERN_INFO PREFIX
"Mapper loaded\n");
692 static void __exit
acpi_wmi_exit(void)
694 struct list_head
*p
, *tmp
;
695 struct wmi_block
*wblock
;
697 acpi_bus_unregister_driver(&acpi_wmi_driver
);
699 list_for_each_safe(p
, tmp
, &wmi_blocks
.list
) {
700 wblock
= list_entry(p
, struct wmi_block
, list
);
706 printk(KERN_INFO PREFIX
"Mapper unloaded\n");
709 subsys_initcall(acpi_wmi_init
);
710 module_exit(acpi_wmi_exit
);