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 MODULE_AUTHOR("Carlos Corbacho");
56 MODULE_DESCRIPTION("ACPI-WMI Mapping Driver - acer_acpi port");
57 MODULE_LICENSE("GPL");
59 static DEFINE_MUTEX(wmi_data_lock
);
69 unsigned char notify_id
;
70 unsigned char reserved
;
79 struct list_head list
;
80 struct guid_block gblock
;
84 static struct wmi_block wmi_blocks
;
86 static wmi_notify_handler wmi_external_handler
;
87 static void *wmi_external_data
;
91 module_param(debug
, int, 0664);
92 MODULE_PARM_DESC(debug
, "Debugging verbosity level (0=least 2=most)");
95 * If the GUID data block is marked as expensive, we must enable and
96 * explicitily disable data collection.
98 #define ACPI_WMI_EXPENSIVE 0x1
99 #define ACPI_WMI_METHOD 0x2 /* GUID is a method */
100 #define ACPI_WMI_STRING 0x4 /* GUID takes & returns a string */
101 #define ACPI_WMI_EVENT 0x8 /* GUID is an event */
103 static int acpi_wmi_remove(struct acpi_device
*device
, int type
);
104 static int acpi_wmi_add(struct acpi_device
*device
);
106 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)) || \
107 (defined(CONFIG_SUSE_KERNEL) && LINUX_VERSION_CODE == KERNEL_VERSION(2,6,22))
108 const static struct acpi_device_id wmi_device_ids
[] = {
113 MODULE_DEVICE_TABLE(acpi
, wmi_device_ids
);
116 static struct acpi_driver acpi_wmi_driver
= {
118 .class = ACPI_WMI_CLASS
,
119 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)) || \
120 (defined(CONFIG_SUSE_KERNEL) && LINUX_VERSION_CODE == KERNEL_VERSION(2,6,22))
121 .ids
= wmi_device_ids
,
123 .ids
= "PNP0C14,pnp0c14",
127 .remove
= acpi_wmi_remove
,
132 * GUID parsing functions
136 * wmi_parse_hexbyte - Convert a ASCII hex number to a byte
137 * @src: Pointer to at least 2 characters to convert.
139 * Convert a two character ASCII hex string to a number.
141 * Return: 0-255 Success, the byte was parsed correctly
142 * -1 Error, an invalid character was supplied
144 static int wmi_parse_hexbyte(const u8
*src
)
146 unsigned int x
; /* For correct wrapping */
151 if (x
- '0' <= '9' - '0') {
153 } else if (x
- 'a' <= 'f' - 'a') {
155 } else if (x
- 'A' <= 'F' - 'A') {
164 if (x
- '0' <= '9' - '0')
165 return h
| (x
- '0');
166 if (x
- 'a' <= 'f' - 'a')
167 return h
| (x
- 'a' + 10);
168 if (x
- 'A' <= 'F' - 'A')
169 return h
| (x
- 'A' + 10);
174 * wmi_swap_bytes - Rearrange GUID bytes to match GUID binary
175 * @src: Memory block holding binary GUID (16 bytes)
176 * @dest: Memory block to hold byte swapped binary GUID (16 bytes)
178 * Byte swap a binary GUID to match it's real GUID value
180 static void wmi_swap_bytes(u8
*src
, u8
*dest
)
184 for (i
= 0; i
<= 3; i
++)
185 memcpy(dest
+ i
, src
+ (3 - i
), 1);
187 for (i
= 0; i
<= 1; i
++)
188 memcpy(dest
+ 4 + i
, src
+ (5 - i
), 1);
190 for (i
= 0; i
<= 1; i
++)
191 memcpy(dest
+ 6 + i
, src
+ (7 - i
), 1);
193 memcpy(dest
+ 8, src
+ 8, 8);
197 * wmi_parse_guid - Convert GUID from ASCII to binary
198 * @src: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
199 * @dest: Memory block to hold binary GUID (16 bytes)
201 * N.B. The GUID need not be NULL terminated.
203 * Return: 'true' @dest contains binary GUID
204 * 'false' @dest contents are undefined
206 static bool wmi_parse_guid(const u8
*src
, u8
*dest
)
208 static const int size
[] = { 4, 2, 2, 2, 6 };
211 if (src
[8] != '-' || src
[13] != '-' ||
212 src
[18] != '-' || src
[23] != '-')
215 for (j
= 0; j
< 5; j
++, src
++) {
216 for (i
= 0; i
< size
[j
]; i
++, src
+= 2, *dest
++ = v
) {
217 v
= wmi_parse_hexbyte(src
);
226 static bool find_guid(const char *guid_string
, struct wmi_block
**out
)
228 char tmp
[16], guid_input
[16];
229 struct wmi_block
*wblock
;
230 struct guid_block
*block
;
233 DEBUG(2, "find_guid called\n");
234 DEBUG(2, "Passing GUID to parser\n");
235 wmi_parse_guid(guid_string
, tmp
);
236 wmi_swap_bytes(tmp
, guid_input
);
238 list_for_each(p
, &wmi_blocks
.list
) {
239 wblock
= list_entry(p
, struct wmi_block
, list
);
240 block
= &wblock
->gblock
;
242 if (memcmp(block
->guid
, guid_input
, 16) == 0) {
243 DEBUG(2, "GUID found - returning block\n");
253 * Exported WMI functions
256 * wmi_acer_evaluate_method - Evaluate a WMI method
257 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
258 * @instance: Instance index
259 * @method_id: Method ID to call
260 * &in: Buffer containing input for the method call
261 * &out: Empty buffer to return the method results
263 * Convert a WMI method call to an ACPI one, and return the results
265 acpi_status
wmi_acer_evaluate_method(const char *guid_string
, u8 instance
,
266 u32 method_id
, const struct acpi_buffer
*in
, struct acpi_buffer
*out
)
268 struct guid_block
*block
= NULL
;
269 struct wmi_block
*wblock
= NULL
;
272 struct acpi_object_list input
;
273 union acpi_object params
[3];
274 char method
[4] = "WM";
276 if (!find_guid(guid_string
, &wblock
))
277 return AE_BAD_ADDRESS
;
279 block
= &wblock
->gblock
;
280 handle
= wblock
->handle
;
282 if (!block
->flags
& ACPI_WMI_METHOD
) {
283 DEBUG(2, "GUID is not a method\n");
287 if (block
->instance_count
< instance
)
288 return AE_BAD_PARAMETER
;
291 input
.pointer
= params
;
292 params
[0].type
= ACPI_TYPE_INTEGER
;
293 params
[0].integer
.value
= instance
;
294 params
[1].type
= ACPI_TYPE_INTEGER
;
295 params
[1].integer
.value
= method_id
;
300 if (block
->flags
& ACPI_WMI_STRING
) {
301 params
[2].type
= ACPI_TYPE_STRING
;
303 params
[2].type
= ACPI_TYPE_BUFFER
;
305 params
[2].buffer
.length
= in
->length
;
306 params
[2].buffer
.pointer
= in
->pointer
;
309 strncat(method
, block
->object_id
, 2);
310 DEBUG(2, "Object to call is %s\n", method
);
312 status
= acpi_evaluate_object(handle
, method
, &input
, out
);
316 EXPORT_SYMBOL_GPL(wmi_acer_evaluate_method
);
319 * wmi_query_block - Return contents of a WMI block
320 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
321 * @instance: Instance index
322 * &out: Empty buffer to return the contents of the data block to
324 * Return the contents of a data block to a buffer
326 acpi_status
wmi_acer_query_block(const char *guid_string
, u8 instance
,
327 struct acpi_buffer
*out
)
329 struct guid_block
*block
= NULL
;
330 struct wmi_block
*wblock
= NULL
;
332 acpi_status status
, wc_status
= AE_ERROR
;
333 struct acpi_object_list input
, wc_input
;
334 union acpi_object wc_params
[1], wq_params
[1];
336 char wc_method
[4] = "WC";
338 if (guid_string
== NULL
|| out
== NULL
)
339 return AE_BAD_PARAMETER
;
341 if (!find_guid(guid_string
, &wblock
))
342 return AE_BAD_ADDRESS
;
344 block
= &wblock
->gblock
;
345 handle
= wblock
->handle
;
347 if (block
->instance_count
< instance
)
348 return AE_BAD_PARAMETER
;
350 /* Check GUID is a data block */
351 if (block
->flags
& (ACPI_WMI_EVENT
| ACPI_WMI_METHOD
))
352 return AE_BAD_ADDRESS
;
355 input
.pointer
= wq_params
;
356 wq_params
[0].type
= ACPI_TYPE_INTEGER
;
357 wq_params
[0].integer
.value
= instance
;
360 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
363 if (block
->flags
& ACPI_WMI_EXPENSIVE
) {
365 wc_input
.pointer
= wc_params
;
366 wc_params
[0].type
= ACPI_TYPE_INTEGER
;
367 wc_params
[0].integer
.value
= 1;
369 strncat(wc_method
, block
->object_id
, 2);
372 * Some GUIDs break the specification by declaring themselves
373 * expensive, but have no corresponding WCxx method. So we
374 * should not fail if this happens.
376 wc_status
= acpi_evaluate_object(handle
, wc_method
,
380 strcpy(method
, "WQ");
381 strncat(method
, block
->object_id
, 2);
382 DEBUG(2, "Object to call is %s\n", method
);
384 status
= acpi_evaluate_object(handle
, method
, NULL
, out
);
387 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
388 * the WQxx method failed - we should disable collection anyway
390 if ((block
->flags
& ACPI_WMI_EXPENSIVE
) && ACPI_SUCCESS(wc_status
)) {
391 wc_params
[0].integer
.value
= 0;
392 status
= acpi_evaluate_object(handle
,
393 wc_method
, &wc_input
, NULL
);
398 EXPORT_SYMBOL_GPL(wmi_acer_query_block
);
401 * wmi_acer_set_block - Write to a WMI block
402 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
403 * @instance: Instance index
404 * &in: Buffer containing new values for the data block
406 * Write the contents of the input buffer to ACPI
408 acpi_status
wmi_acer_set_block(const char *guid_string
, u8 instance
,
409 const struct acpi_buffer
*in
)
411 struct guid_block
*block
= NULL
;
412 struct wmi_block
*wblock
= NULL
;
414 struct acpi_object_list input
;
415 union acpi_object params
[2];
416 char method
[4] = "WS";
418 if (guid_string
== NULL
|| in
== NULL
)
421 if (!find_guid(guid_string
, &wblock
))
422 return AE_BAD_ADDRESS
;
424 block
= &wblock
->gblock
;
425 handle
= wblock
->handle
;
427 if (block
->instance_count
< instance
)
428 return AE_BAD_PARAMETER
;
430 /* Check GUID is a data block */
431 if (block
->flags
& (ACPI_WMI_EVENT
| ACPI_WMI_METHOD
))
432 return AE_BAD_ADDRESS
;
435 input
.pointer
= params
;
436 params
[0].type
= ACPI_TYPE_INTEGER
;
437 params
[0].integer
.value
= instance
;
439 if (block
->flags
& ACPI_WMI_STRING
) {
440 params
[1].type
= ACPI_TYPE_STRING
;
442 params
[1].type
= ACPI_TYPE_BUFFER
;
444 params
[1].buffer
.length
= in
->length
;
445 params
[1].buffer
.pointer
= in
->pointer
;
447 strncat(method
, block
->object_id
, 2);
448 DEBUG(2, "Object to call is %s\n", method
);
450 return acpi_evaluate_object(handle
, method
, &input
, NULL
);
452 EXPORT_SYMBOL_GPL(wmi_acer_set_block
);
455 * wmi_install_notify_handler - Register handler for WMI events
456 * @handler: Function to handle notifications
457 * @data: Data to be returned to handler when event is fired
459 * Register a handler for events sent to the WMI-ACPI mapper device.
461 acpi_status
wmi_acer_install_notify_handler(wmi_notify_handler handler
, void *data
)
464 return AE_BAD_PARAMETER
;
466 if (!wmi_external_handler
)
467 return AE_ALREADY_ACQUIRED
;
469 wmi_external_handler
= handler
;
470 wmi_external_data
= data
;
474 EXPORT_SYMBOL_GPL(wmi_acer_install_notify_handler
);
477 * wmi_acer_uninstall_notify_handler - Unregister handler for WMI events
479 * Unregister handler for events sent to the WMI-ACPI mapper device.
481 acpi_status
wmi_acer_remove_notify_handler(void)
483 DEBUG(1, "Notifier triggered\n");
485 if (wmi_external_handler
) {
486 wmi_external_handler
= NULL
;
487 wmi_external_data
= NULL
;
492 EXPORT_SYMBOL_GPL(wmi_acer_remove_notify_handler
);
495 * wmi_acer_get_event_data - Get WMI data associated with an event
497 * @event - Event to find
498 * &out - Buffer to hold event data
500 * Returns extra data associated with an event in WMI.
502 acpi_status
wmi_acer_get_event_data(u32 event
, struct acpi_buffer
*out
)
504 struct acpi_object_list input
;
505 union acpi_object params
[1];
506 struct guid_block
*gblock
;
507 struct wmi_block
*wblock
;
511 input
.pointer
= params
;
512 params
[0].type
= ACPI_TYPE_INTEGER
;
513 params
[0].integer
.value
= event
;
515 list_for_each(p
, &wmi_blocks
.list
) {
516 wblock
= list_entry(p
, struct wmi_block
, list
);
517 gblock
= &wblock
->gblock
;
519 if ((gblock
->flags
& ACPI_WMI_EVENT
) &&
520 (gblock
->notify_id
== event
))
521 return acpi_evaluate_object(wblock
->handle
, "_WED",
527 EXPORT_SYMBOL(wmi_acer_get_event_data
);
530 * wmi_acer_has_guid - Check if a GUID is available
531 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
533 * Check if a given GUID is defined by _WDG
535 bool wmi_acer_has_guid(const char *guid_string
)
537 return find_guid(guid_string
, NULL
);
539 EXPORT_SYMBOL_GPL(wmi_acer_has_guid
);
542 * parse_wdg - Parse the _WDG method for the GUID data blocks
544 static __init acpi_status
parse_wdg(acpi_handle handle
)
546 struct acpi_buffer out
= {ACPI_ALLOCATE_BUFFER
, NULL
};
547 union acpi_object
*obj
;
548 struct guid_block
*gblock
;
549 struct wmi_block
*wblock
;
553 status
= acpi_evaluate_object(handle
, "_WDG", NULL
, &out
);
555 if (ACPI_FAILURE(status
))
558 obj
= (union acpi_object
*) out
.pointer
;
560 if (obj
->type
!= ACPI_TYPE_BUFFER
)
563 total
= obj
->buffer
.length
/ sizeof(struct guid_block
);
565 gblock
= kzalloc(obj
->buffer
.length
, GFP_KERNEL
);
569 memcpy(gblock
, obj
->buffer
.pointer
, obj
->buffer
.length
);
571 for (i
= 0; i
< total
; i
++) {
572 wblock
= kzalloc(sizeof(struct wmi_block
), GFP_KERNEL
);
576 wblock
->gblock
= gblock
[i
];
577 wblock
->handle
= handle
;
578 list_add_tail(&wblock
->list
, &wmi_blocks
.list
);
588 * WMI can have EmbeddedControl access regions. In which case, we just want to
589 * hand these off to the EC driver.
592 acpi_wmi_ec_space_handler(u32 function
, acpi_physical_address address
,
593 u32 bits
, acpi_integer
*value
,
594 void *handler_context
, void *region_context
)
596 int result
= 0, i
= 0;
599 if ((address
> 0xFF) || !value
)
600 return AE_BAD_PARAMETER
;
602 if (function
!= ACPI_READ
&& function
!= ACPI_WRITE
)
603 return AE_BAD_PARAMETER
;
606 return AE_BAD_PARAMETER
;
608 if (function
== ACPI_READ
) {
609 result
= ec_read(address
, &temp
);
610 (*value
) |= ((acpi_integer
)temp
) << i
;
612 temp
= 0xff & ((*value
) >> i
);
613 result
= ec_write(address
, temp
);
618 return AE_BAD_PARAMETER
;
631 static void acpi_wmi_notify(acpi_handle handle
, u32 event
, void *data
)
633 struct guid_block
*block
;
634 struct wmi_block
*wblock
;
637 list_for_each(p
, &wmi_blocks
.list
) {
638 wblock
= list_entry(p
, struct wmi_block
, list
);
639 block
= &wblock
->gblock
;
641 if ((block
->flags
& ACPI_WMI_EVENT
) &&
642 block
->notify_id
== event
) {
643 if (wmi_external_handler
)
644 wmi_external_handler(event
, wmi_external_data
);
650 static int __init
acpi_wmi_add(struct acpi_device
*device
)
655 status
= acpi_install_notify_handler(device
->handle
, ACPI_DEVICE_NOTIFY
,
656 acpi_wmi_notify
, device
);
658 if (ACPI_FAILURE(status
)) {
659 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
660 "Error installing notify handler\n"));
664 status
= acpi_install_address_space_handler(device
->handle
,
666 &acpi_wmi_ec_space_handler
,
668 if (ACPI_FAILURE(status
)) {
669 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
670 "Error installing EC region handler\n"));
674 status
= parse_wdg(device
->handle
);
675 if (ACPI_FAILURE(status
))
681 static int acpi_wmi_remove(struct acpi_device
*device
, int type
)
683 acpi_remove_address_space_handler(device
->handle
, ACPI_ADR_SPACE_EC
,
684 &acpi_wmi_ec_space_handler
);
686 acpi_remove_notify_handler(device
->handle
, ACPI_DEVICE_NOTIFY
,
692 static int __init
acpi_wmi_init(void)
699 INIT_LIST_HEAD(&wmi_blocks
.list
);
701 result
= acpi_bus_register_driver(&acpi_wmi_driver
);
704 printk(KERN_INFO PREFIX
"Interface device not found\n");
708 printk(KERN_INFO PREFIX
"Mapper loaded\n");
713 static void __exit
acpi_wmi_exit(void)
715 struct list_head
*p
, *tmp
;
716 struct wmi_block
*wblock
;
718 acpi_bus_unregister_driver(&acpi_wmi_driver
);
720 list_for_each_safe(p
, tmp
, &wmi_blocks
.list
) {
721 wblock
= list_entry(p
, struct wmi_block
, list
);
727 printk(KERN_INFO PREFIX
"Mapper unloaded\n");
730 module_init(acpi_wmi_init
);
731 module_exit(acpi_wmi_exit
);