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"
45 #define PREFIX "ACPI: WMI: "
47 static DEFINE_MUTEX(wmi_data_lock
);
54 unsigned char notify_id
;
55 unsigned char reserved
;
63 struct list_head list
;
64 struct guid_block gblock
;
66 wmi_notify_handler handler
;
70 static struct wmi_block wmi_blocks
;
73 * If the GUID data block is marked as expensive, we must enable and
74 * explicitily disable data collection.
76 #define ACPI_WMI_EXPENSIVE 0x1
77 #define ACPI_WMI_METHOD 0x2 /* GUID is a method */
78 #define ACPI_WMI_STRING 0x4 /* GUID takes & returns a string */
79 #define ACPI_WMI_EVENT 0x8 /* GUID is an event */
81 static int acpi_wmi_remove(struct acpi_device
*device
, int type
);
82 static int acpi_wmi_add(struct acpi_device
*device
);
83 static void acpi_wmi_notify(struct acpi_device
*device
, u32 event
);
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
,
99 .notify
= acpi_wmi_notify
,
104 * GUID parsing functions
108 * wmi_parse_hexbyte - Convert a ASCII hex number to a byte
109 * @src: Pointer to at least 2 characters to convert.
111 * Convert a two character ASCII hex string to a number.
113 * Return: 0-255 Success, the byte was parsed correctly
114 * -1 Error, an invalid character was supplied
116 static int wmi_parse_hexbyte(const u8
*src
)
118 unsigned int x
; /* For correct wrapping */
123 if (x
- '0' <= '9' - '0') {
125 } else if (x
- 'a' <= 'f' - 'a') {
127 } else if (x
- 'A' <= 'F' - 'A') {
136 if (x
- '0' <= '9' - '0')
137 return h
| (x
- '0');
138 if (x
- 'a' <= 'f' - 'a')
139 return h
| (x
- 'a' + 10);
140 if (x
- 'A' <= 'F' - 'A')
141 return h
| (x
- 'A' + 10);
146 * wmi_swap_bytes - Rearrange GUID bytes to match GUID binary
147 * @src: Memory block holding binary GUID (16 bytes)
148 * @dest: Memory block to hold byte swapped binary GUID (16 bytes)
150 * Byte swap a binary GUID to match it's real GUID value
152 static void wmi_swap_bytes(u8
*src
, u8
*dest
)
156 for (i
= 0; i
<= 3; i
++)
157 memcpy(dest
+ i
, src
+ (3 - i
), 1);
159 for (i
= 0; i
<= 1; i
++)
160 memcpy(dest
+ 4 + i
, src
+ (5 - i
), 1);
162 for (i
= 0; i
<= 1; i
++)
163 memcpy(dest
+ 6 + i
, src
+ (7 - i
), 1);
165 memcpy(dest
+ 8, src
+ 8, 8);
169 * wmi_parse_guid - Convert GUID from ASCII to binary
170 * @src: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
171 * @dest: Memory block to hold binary GUID (16 bytes)
173 * N.B. The GUID need not be NULL terminated.
175 * Return: 'true' @dest contains binary GUID
176 * 'false' @dest contents are undefined
178 static bool wmi_parse_guid(const u8
*src
, u8
*dest
)
180 static const int size
[] = { 4, 2, 2, 2, 6 };
183 if (src
[8] != '-' || src
[13] != '-' ||
184 src
[18] != '-' || src
[23] != '-')
187 for (j
= 0; j
< 5; j
++, src
++) {
188 for (i
= 0; i
< size
[j
]; i
++, src
+= 2, *dest
++ = v
) {
189 v
= wmi_parse_hexbyte(src
);
198 static bool find_guid(const char *guid_string
, struct wmi_block
**out
)
200 char tmp
[16], guid_input
[16];
201 struct wmi_block
*wblock
;
202 struct guid_block
*block
;
205 wmi_parse_guid(guid_string
, tmp
);
206 wmi_swap_bytes(tmp
, guid_input
);
208 list_for_each(p
, &wmi_blocks
.list
) {
209 wblock
= list_entry(p
, struct wmi_block
, list
);
210 block
= &wblock
->gblock
;
212 if (memcmp(block
->guid
, guid_input
, 16) == 0) {
221 static acpi_status
wmi_method_enable(struct wmi_block
*wblock
, int enable
)
223 struct guid_block
*block
= NULL
;
225 struct acpi_object_list input
;
226 union acpi_object params
[1];
230 block
= &wblock
->gblock
;
231 handle
= wblock
->handle
;
237 input
.pointer
= params
;
238 params
[0].type
= ACPI_TYPE_INTEGER
;
239 params
[0].integer
.value
= enable
;
241 snprintf(method
, 5, "WE%02X", block
->notify_id
);
242 status
= acpi_evaluate_object(handle
, method
, &input
, NULL
);
244 if (status
!= AE_OK
&& status
!= AE_NOT_FOUND
)
251 * Exported WMI functions
254 * wmi_evaluate_method - Evaluate a WMI method
255 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
256 * @instance: Instance index
257 * @method_id: Method ID to call
258 * &in: Buffer containing input for the method call
259 * &out: Empty buffer to return the method results
261 * Call an ACPI-WMI method
263 acpi_status
wmi_evaluate_method(const char *guid_string
, u8 instance
,
264 u32 method_id
, const struct acpi_buffer
*in
, struct acpi_buffer
*out
)
266 struct guid_block
*block
= NULL
;
267 struct wmi_block
*wblock
= NULL
;
270 struct acpi_object_list input
;
271 union acpi_object params
[3];
272 char method
[5] = "WM";
274 if (!find_guid(guid_string
, &wblock
))
277 block
= &wblock
->gblock
;
278 handle
= wblock
->handle
;
280 if (!(block
->flags
& ACPI_WMI_METHOD
))
283 if (block
->instance_count
< instance
)
284 return AE_BAD_PARAMETER
;
287 input
.pointer
= params
;
288 params
[0].type
= ACPI_TYPE_INTEGER
;
289 params
[0].integer
.value
= instance
;
290 params
[1].type
= ACPI_TYPE_INTEGER
;
291 params
[1].integer
.value
= method_id
;
296 if (block
->flags
& ACPI_WMI_STRING
) {
297 params
[2].type
= ACPI_TYPE_STRING
;
299 params
[2].type
= ACPI_TYPE_BUFFER
;
301 params
[2].buffer
.length
= in
->length
;
302 params
[2].buffer
.pointer
= in
->pointer
;
305 strncat(method
, block
->object_id
, 2);
307 status
= acpi_evaluate_object(handle
, method
, &input
, out
);
311 EXPORT_SYMBOL_GPL(wmi_evaluate_method
);
314 * wmi_query_block - Return contents of a WMI block
315 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
316 * @instance: Instance index
317 * &out: Empty buffer to return the contents of the data block to
319 * Return the contents of an ACPI-WMI data block to a buffer
321 acpi_status
wmi_query_block(const char *guid_string
, u8 instance
,
322 struct acpi_buffer
*out
)
324 struct guid_block
*block
= NULL
;
325 struct wmi_block
*wblock
= NULL
;
326 acpi_handle handle
, wc_handle
;
327 acpi_status status
, wc_status
= AE_ERROR
;
328 struct acpi_object_list input
, wc_input
;
329 union acpi_object wc_params
[1], wq_params
[1];
331 char wc_method
[5] = "WC";
333 if (!guid_string
|| !out
)
334 return AE_BAD_PARAMETER
;
336 if (!find_guid(guid_string
, &wblock
))
339 block
= &wblock
->gblock
;
340 handle
= wblock
->handle
;
342 if (block
->instance_count
< instance
)
343 return AE_BAD_PARAMETER
;
345 /* Check GUID is a data block */
346 if (block
->flags
& (ACPI_WMI_EVENT
| ACPI_WMI_METHOD
))
350 input
.pointer
= wq_params
;
351 wq_params
[0].type
= ACPI_TYPE_INTEGER
;
352 wq_params
[0].integer
.value
= instance
;
355 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
358 if (block
->flags
& ACPI_WMI_EXPENSIVE
) {
360 wc_input
.pointer
= wc_params
;
361 wc_params
[0].type
= ACPI_TYPE_INTEGER
;
362 wc_params
[0].integer
.value
= 1;
364 strncat(wc_method
, block
->object_id
, 2);
367 * Some GUIDs break the specification by declaring themselves
368 * expensive, but have no corresponding WCxx method. So we
369 * should not fail if this happens.
371 wc_status
= acpi_get_handle(handle
, wc_method
, &wc_handle
);
372 if (ACPI_SUCCESS(wc_status
))
373 wc_status
= acpi_evaluate_object(handle
, wc_method
,
377 strcpy(method
, "WQ");
378 strncat(method
, block
->object_id
, 2);
380 status
= acpi_evaluate_object(handle
, method
, &input
, out
);
383 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
384 * the WQxx method failed - we should disable collection anyway.
386 if ((block
->flags
& ACPI_WMI_EXPENSIVE
) && ACPI_SUCCESS(wc_status
)) {
387 wc_params
[0].integer
.value
= 0;
388 status
= acpi_evaluate_object(handle
,
389 wc_method
, &wc_input
, NULL
);
394 EXPORT_SYMBOL_GPL(wmi_query_block
);
397 * wmi_set_block - Write to a WMI block
398 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
399 * @instance: Instance index
400 * &in: Buffer containing new values for the data block
402 * Write the contents of the input buffer to an ACPI-WMI data block
404 acpi_status
wmi_set_block(const char *guid_string
, u8 instance
,
405 const struct acpi_buffer
*in
)
407 struct guid_block
*block
= NULL
;
408 struct wmi_block
*wblock
= NULL
;
410 struct acpi_object_list input
;
411 union acpi_object params
[2];
412 char method
[5] = "WS";
414 if (!guid_string
|| !in
)
417 if (!find_guid(guid_string
, &wblock
))
420 block
= &wblock
->gblock
;
421 handle
= wblock
->handle
;
423 if (block
->instance_count
< instance
)
424 return AE_BAD_PARAMETER
;
426 /* Check GUID is a data block */
427 if (block
->flags
& (ACPI_WMI_EVENT
| ACPI_WMI_METHOD
))
431 input
.pointer
= params
;
432 params
[0].type
= ACPI_TYPE_INTEGER
;
433 params
[0].integer
.value
= instance
;
435 if (block
->flags
& ACPI_WMI_STRING
) {
436 params
[1].type
= ACPI_TYPE_STRING
;
438 params
[1].type
= ACPI_TYPE_BUFFER
;
440 params
[1].buffer
.length
= in
->length
;
441 params
[1].buffer
.pointer
= in
->pointer
;
443 strncat(method
, block
->object_id
, 2);
445 return acpi_evaluate_object(handle
, method
, &input
, NULL
);
447 EXPORT_SYMBOL_GPL(wmi_set_block
);
450 * wmi_install_notify_handler - Register handler for WMI events
451 * @handler: Function to handle notifications
452 * @data: Data to be returned to handler when event is fired
454 * Register a handler for events sent to the ACPI-WMI mapper device.
456 acpi_status
wmi_install_notify_handler(const char *guid
,
457 wmi_notify_handler handler
, void *data
)
459 struct wmi_block
*block
;
462 if (!guid
|| !handler
)
463 return AE_BAD_PARAMETER
;
465 find_guid(guid
, &block
);
470 return AE_ALREADY_ACQUIRED
;
472 block
->handler
= handler
;
473 block
->handler_data
= data
;
475 status
= wmi_method_enable(block
, 1);
479 EXPORT_SYMBOL_GPL(wmi_install_notify_handler
);
482 * wmi_uninstall_notify_handler - Unregister handler for WMI events
484 * Unregister handler for events sent to the ACPI-WMI mapper device.
486 acpi_status
wmi_remove_notify_handler(const char *guid
)
488 struct wmi_block
*block
;
492 return AE_BAD_PARAMETER
;
494 find_guid(guid
, &block
);
499 return AE_NULL_ENTRY
;
501 status
= wmi_method_enable(block
, 0);
503 block
->handler
= NULL
;
504 block
->handler_data
= NULL
;
508 EXPORT_SYMBOL_GPL(wmi_remove_notify_handler
);
511 * wmi_get_event_data - Get WMI data associated with an event
513 * @event - Event to find
514 * &out - Buffer to hold event data
516 * Returns extra data associated with an event in WMI.
518 acpi_status
wmi_get_event_data(u32 event
, struct acpi_buffer
*out
)
520 struct acpi_object_list input
;
521 union acpi_object params
[1];
522 struct guid_block
*gblock
;
523 struct wmi_block
*wblock
;
527 input
.pointer
= params
;
528 params
[0].type
= ACPI_TYPE_INTEGER
;
529 params
[0].integer
.value
= event
;
531 list_for_each(p
, &wmi_blocks
.list
) {
532 wblock
= list_entry(p
, struct wmi_block
, list
);
533 gblock
= &wblock
->gblock
;
535 if ((gblock
->flags
& ACPI_WMI_EVENT
) &&
536 (gblock
->notify_id
== event
))
537 return acpi_evaluate_object(wblock
->handle
, "_WED",
543 EXPORT_SYMBOL_GPL(wmi_get_event_data
);
546 * wmi_has_guid - Check if a GUID is available
547 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
549 * Check if a given GUID is defined by _WDG
551 bool wmi_has_guid(const char *guid_string
)
553 return find_guid(guid_string
, NULL
);
555 EXPORT_SYMBOL_GPL(wmi_has_guid
);
558 * Parse the _WDG method for the GUID data blocks
560 static __init acpi_status
parse_wdg(acpi_handle handle
)
562 struct acpi_buffer out
= {ACPI_ALLOCATE_BUFFER
, NULL
};
563 union acpi_object
*obj
;
564 struct guid_block
*gblock
;
565 struct wmi_block
*wblock
;
569 status
= acpi_evaluate_object(handle
, "_WDG", NULL
, &out
);
571 if (ACPI_FAILURE(status
))
574 obj
= (union acpi_object
*) out
.pointer
;
576 if (obj
->type
!= ACPI_TYPE_BUFFER
)
579 total
= obj
->buffer
.length
/ sizeof(struct guid_block
);
581 gblock
= kzalloc(obj
->buffer
.length
, GFP_KERNEL
);
585 memcpy(gblock
, obj
->buffer
.pointer
, obj
->buffer
.length
);
587 for (i
= 0; i
< total
; i
++) {
588 wblock
= kzalloc(sizeof(struct wmi_block
), GFP_KERNEL
);
592 wblock
->gblock
= gblock
[i
];
593 wblock
->handle
= handle
;
594 list_add_tail(&wblock
->list
, &wmi_blocks
.list
);
604 * WMI can have EmbeddedControl access regions. In which case, we just want to
605 * hand these off to the EC driver.
608 acpi_wmi_ec_space_handler(u32 function
, acpi_physical_address address
,
609 u32 bits
, acpi_integer
* value
,
610 void *handler_context
, void *region_context
)
612 int result
= 0, i
= 0;
615 if ((address
> 0xFF) || !value
)
616 return AE_BAD_PARAMETER
;
618 if (function
!= ACPI_READ
&& function
!= ACPI_WRITE
)
619 return AE_BAD_PARAMETER
;
622 return AE_BAD_PARAMETER
;
624 if (function
== ACPI_READ
) {
625 result
= ec_read(address
, &temp
);
626 (*value
) |= ((acpi_integer
)temp
) << i
;
628 temp
= 0xff & ((*value
) >> i
);
629 result
= ec_write(address
, temp
);
634 return AE_BAD_PARAMETER
;
647 static void acpi_wmi_notify(struct acpi_device
*device
, u32 event
)
649 struct guid_block
*block
;
650 struct wmi_block
*wblock
;
653 list_for_each(p
, &wmi_blocks
.list
) {
654 wblock
= list_entry(p
, struct wmi_block
, list
);
655 block
= &wblock
->gblock
;
657 if ((block
->flags
& ACPI_WMI_EVENT
) &&
658 (block
->notify_id
== event
)) {
660 wblock
->handler(event
, wblock
->handler_data
);
662 acpi_bus_generate_netlink_event(
663 device
->pnp
.device_class
, dev_name(&device
->dev
),
670 static int acpi_wmi_remove(struct acpi_device
*device
, int type
)
672 acpi_remove_address_space_handler(device
->handle
,
673 ACPI_ADR_SPACE_EC
, &acpi_wmi_ec_space_handler
);
678 static int __init
acpi_wmi_add(struct acpi_device
*device
)
683 status
= acpi_install_address_space_handler(device
->handle
,
685 &acpi_wmi_ec_space_handler
,
687 if (ACPI_FAILURE(status
))
690 status
= parse_wdg(device
->handle
);
691 if (ACPI_FAILURE(status
)) {
692 printk(KERN_ERR PREFIX
"Error installing EC region handler\n");
699 static int __init
acpi_wmi_init(void)
703 INIT_LIST_HEAD(&wmi_blocks
.list
);
708 result
= acpi_bus_register_driver(&acpi_wmi_driver
);
711 printk(KERN_INFO PREFIX
"Error loading mapper\n");
713 printk(KERN_INFO PREFIX
"Mapper loaded\n");
719 static void __exit
acpi_wmi_exit(void)
721 struct list_head
*p
, *tmp
;
722 struct wmi_block
*wblock
;
724 acpi_bus_unregister_driver(&acpi_wmi_driver
);
726 list_for_each_safe(p
, tmp
, &wmi_blocks
.list
) {
727 wblock
= list_entry(p
, struct wmi_block
, list
);
733 printk(KERN_INFO PREFIX
"Mapper unloaded\n");
736 subsys_initcall(acpi_wmi_init
);
737 module_exit(acpi_wmi_exit
);