Revert "wmi-acer: Clean (A & B) > 0 tests to (A &= B)"
[acer_acpi.git] / wmi-acer.c
blob29b734d65cc18f73d5db8902980aa28478af13b7
1 /*
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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29 * TODO:
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>
52 #include "wmi-acer.h"
54 #define ACPI_WMI_CLASS "wmi"
56 #undef PREFIX
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...) { \
63 if (debug >= level) \
64 printk(KERN_DEBUG PREFIX message);\
67 /* Workaround needed for older kernels */
68 #ifndef bool
69 #define bool int
70 #endif
72 MODULE_AUTHOR("Carlos Corbacho");
73 MODULE_DESCRIPTION("WMI ACPI Interface Driver");
74 MODULE_LICENSE("GPL");
76 struct guid_block_t
78 char guid[16];
79 union
81 char object_id[2];
82 struct
84 unsigned char notification_value;
85 unsigned char reserved;
88 u8 instance_count;
89 u8 flags;
92 struct guid_list
94 struct guid_block_t *pointer;
95 int total;
98 static struct guid_list guids;
99 static acpi_handle acpi_wmi_handle;
100 static int debug;
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
108 * required
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[] = {
125 {"PNP0C14", 0},
126 {"pnp0c14", 0},
127 {"", 0},
129 MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
130 #endif
132 static struct acpi_driver acpi_wmi_driver = {
133 .name = "wmi",
134 .class = ACPI_WMI_CLASS,
135 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
136 .ids = wmi_device_ids,
137 #else
138 .ids = "PNP0C14,pnp0c14",
139 #endif
140 .ops = {
141 .add = acpi_wmi_add,
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 */
162 int h;
164 /* high part */
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;
168 else return -1;
169 h <<= 4;
171 /* low part */
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);
175 return -1;
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)
187 int i;
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 };
214 int i, j, v;
216 if (src[8] != '-' || src[13] != '-' ||
217 src[18] != '-' || src[23] != '-')
218 return false;
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)
223 return false;
225 return true;
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;
232 int i;
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");
244 if (out != NULL)
245 *out = block;
246 return 1;
249 return 0;
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;
259 acpi_status status;
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");
269 return AE_BAD_DATA;
272 input.count = 2;
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;
279 if (in != NULL) {
280 input.count = 3;
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) > 0) {
293 /* Convert output from ASCIIZ to Unicode */
294 return AE_NOT_IMPLEMENTED;
297 return status;
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;
305 acpi_status status;
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;
321 input.count = 1;
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
328 * enable collection
330 /* FIXME - should INTEGER be STRING? */
331 if ((block->flags & WMIACPI_FLAG_EXPENSIVE) > 0) {
332 wc_input.count = 1;
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,
341 &wc_input, NULL);
343 if (ACPI_FAILURE(status))
344 return AE_ERROR;
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;
369 return status;
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)
382 return AE_BAD_DATA;
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;
391 input.count = 2;
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) > 0)
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 acpi_status parse_wdg(void)
422 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
423 union acpi_object *obj;
424 acpi_status status;
426 status = acpi_evaluate_object(acpi_wmi_handle, "_WDG", NULL, &out);
428 if (ACPI_FAILURE(status))
429 return status;
431 obj = (union acpi_object *) out.pointer;
433 if (obj->type != ACPI_TYPE_BUFFER)
434 return AE_ERROR;
436 guids.pointer = kzalloc(obj->buffer.length, GFP_KERNEL);
437 memcpy(guids.pointer, obj->buffer.pointer, obj->buffer.length);
438 guids.total = obj->buffer.length / sizeof(struct guid_block_t);
440 kfree(out.pointer);
442 return status;
445 static void acpi_wmi_notify(acpi_handle handle, u32 event, void *data)
447 int i;
448 struct guid_block_t *block;
450 DEBUG(1, "Notifier triggered\n");
452 for (i = 0; i < guids.total; i++) {
453 block = guids.pointer + i;
455 /* FIXME - the last comparison may be broken */
456 if ((block->flags & WMIACPI_FLAG_EVENT) > 0 &&
457 block->notification_value == event) {
458 /* Should we generate an event? And how do we pass the results of _WED? */
463 static int acpi_wmi_add(struct acpi_device *device)
465 acpi_status status;
467 acpi_wmi_handle = device->handle;
469 status = acpi_install_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
470 acpi_wmi_notify, device);
472 if (ACPI_FAILURE(status)) {
473 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
474 "Error installing notify handler\n"));
475 return -ENODEV;
478 status = parse_wdg();
480 if (ACPI_FAILURE(status))
481 return -ENODEV;
483 return 0;
486 static int acpi_wmi_remove(struct acpi_device *device, int type)
488 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
489 acpi_wmi_notify);
491 return 0;
494 static int __init acpi_wmi_init(void)
496 acpi_status result;
498 if (acpi_disabled)
499 return -ENODEV;
501 result = acpi_bus_register_driver(&acpi_wmi_driver);
503 if (ACPI_FAILURE(result))
504 return -ENODEV;
506 printk(KERN_INFO PREFIX "Interface device found\n");
507 printk(KERN_INFO PREFIX "Mapper loaded\n");
509 return 0;
512 static void __exit acpi_wmi_exit(void)
514 if (guids.pointer)
515 kfree(guids.pointer);
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);