Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / drivers / acpi / wmi.c
1 /*
2  *  ACPI-WMI mapping driver
3  *
4  *  Copyright (C) 2007-2008 Carlos Corbacho <carlos@strangeworlds.co.uk>
5  *
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>
10  *
11  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12  *
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.
17  *
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.
22  *
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.
26  *
27  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28  */
29
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>
37
38 ACPI_MODULE_NAME("wmi");
39 MODULE_AUTHOR("Carlos Corbacho");
40 MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
41 MODULE_LICENSE("GPL");
42
43 #define ACPI_WMI_CLASS "wmi"
44
45 #undef PREFIX
46 #define PREFIX "ACPI: WMI: "
47
48 static DEFINE_MUTEX(wmi_data_lock);
49
50 struct guid_block {
51         char guid[16];
52         union {
53                 char object_id[2];
54                 struct {
55                         unsigned char notify_id;
56                         unsigned char reserved;
57                 };
58         };
59         u8 instance_count;
60         u8 flags;
61 };
62
63 struct wmi_block {
64         struct list_head list;
65         struct guid_block gblock;
66         acpi_handle handle;
67         wmi_notify_handler handler;
68         void *handler_data;
69 };
70
71 static struct wmi_block wmi_blocks;
72
73 /*
74  * If the GUID data block is marked as expensive, we must enable and
75  * explicitily disable data collection.
76  */
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 */
81
82 static int acpi_wmi_remove(struct acpi_device *device, int type);
83 static int acpi_wmi_add(struct acpi_device *device);
84
85 static const struct acpi_device_id wmi_device_ids[] = {
86         {"PNP0C14", 0},
87         {"pnp0c14", 0},
88         {"", 0},
89 };
90 MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
91
92 static struct acpi_driver acpi_wmi_driver = {
93         .name = "wmi",
94         .class = ACPI_WMI_CLASS,
95         .ids = wmi_device_ids,
96         .ops = {
97                 .add = acpi_wmi_add,
98                 .remove = acpi_wmi_remove,
99                 },
100 };
101
102 /*
103  * GUID parsing functions
104  */
105
106 /**
107  * wmi_parse_hexbyte - Convert a ASCII hex number to a byte
108  * @src:  Pointer to at least 2 characters to convert.
109  *
110  * Convert a two character ASCII hex string to a number.
111  *
112  * Return:  0-255  Success, the byte was parsed correctly
113  *          -1     Error, an invalid character was supplied
114  */
115 static int wmi_parse_hexbyte(const u8 *src)
116 {
117         unsigned int x; /* For correct wrapping */
118         int h;
119
120         /* high part */
121         x = src[0];
122         if (x - '0' <= '9' - '0') {
123                 h = x - '0';
124         } else if (x - 'a' <= 'f' - 'a') {
125                 h = x - 'a' + 10;
126         } else if (x - 'A' <= 'F' - 'A') {
127                 h = x - 'A' + 10;
128         } else {
129                 return -1;
130         }
131         h <<= 4;
132
133         /* low part */
134         x = src[1];
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);
141         return -1;
142 }
143
144 /**
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)
148  *
149  * Byte swap a binary GUID to match it's real GUID value
150  */
151 static void wmi_swap_bytes(u8 *src, u8 *dest)
152 {
153         int i;
154
155         for (i = 0; i <= 3; i++)
156                 memcpy(dest + i, src + (3 - i), 1);
157
158         for (i = 0; i <= 1; i++)
159                 memcpy(dest + 4 + i, src + (5 - i), 1);
160
161         for (i = 0; i <= 1; i++)
162                 memcpy(dest + 6 + i, src + (7 - i), 1);
163
164         memcpy(dest + 8, src + 8, 8);
165 }
166
167 /**
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)
171  *
172  * N.B. The GUID need not be NULL terminated.
173  *
174  * Return:  'true'   @dest contains binary GUID
175  *          'false'  @dest contents are undefined
176  */
177 static bool wmi_parse_guid(const u8 *src, u8 *dest)
178 {
179         static const int size[] = { 4, 2, 2, 2, 6 };
180         int i, j, v;
181
182         if (src[8]  != '-' || src[13] != '-' ||
183                 src[18] != '-' || src[23] != '-')
184                 return false;
185
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);
189                         if (v < 0)
190                                 return false;
191                 }
192         }
193
194         return true;
195 }
196
197 static bool find_guid(const char *guid_string, struct wmi_block **out)
198 {
199         char tmp[16], guid_input[16];
200         struct wmi_block *wblock;
201         struct guid_block *block;
202         struct list_head *p;
203
204         wmi_parse_guid(guid_string, tmp);
205         wmi_swap_bytes(tmp, guid_input);
206
207         list_for_each(p, &wmi_blocks.list) {
208                 wblock = list_entry(p, struct wmi_block, list);
209                 block = &wblock->gblock;
210
211                 if (memcmp(block->guid, guid_input, 16) == 0) {
212                         if (out)
213                                 *out = wblock;
214                         return 1;
215                 }
216         }
217         return 0;
218 }
219
220 /*
221  * Exported WMI functions
222  */
223 /**
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
230  *
231  * Call an ACPI-WMI method
232  */
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)
235 {
236         struct guid_block *block = NULL;
237         struct wmi_block *wblock = NULL;
238         acpi_handle handle;
239         acpi_status status;
240         struct acpi_object_list input;
241         union acpi_object params[3];
242         char method[4] = "WM";
243
244         if (!find_guid(guid_string, &wblock))
245                 return AE_BAD_ADDRESS;
246
247         block = &wblock->gblock;
248         handle = wblock->handle;
249
250         if (!(block->flags & ACPI_WMI_METHOD))
251                 return AE_BAD_DATA;
252
253         if (block->instance_count < instance)
254                 return AE_BAD_PARAMETER;
255
256         input.count = 2;
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;
262
263         if (in) {
264                 input.count = 3;
265
266                 if (block->flags & ACPI_WMI_STRING) {
267                         params[2].type = ACPI_TYPE_STRING;
268                 } else {
269                         params[2].type = ACPI_TYPE_BUFFER;
270                 }
271                 params[2].buffer.length = in->length;
272                 params[2].buffer.pointer = in->pointer;
273         }
274
275         strncat(method, block->object_id, 2);
276
277         status = acpi_evaluate_object(handle, method, &input, out);
278
279         return status;
280 }
281 EXPORT_SYMBOL_GPL(wmi_evaluate_method);
282
283 /**
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
288  *
289  * Return the contents of an ACPI-WMI data block to a buffer
290  */
291 acpi_status wmi_query_block(const char *guid_string, u8 instance,
292 struct acpi_buffer *out)
293 {
294         struct guid_block *block = NULL;
295         struct wmi_block *wblock = NULL;
296         acpi_handle handle, wc_handle;
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];
300         char method[4];
301         char wc_method[4] = "WC";
302
303         if (!guid_string || !out)
304                 return AE_BAD_PARAMETER;
305
306         if (!find_guid(guid_string, &wblock))
307                 return AE_BAD_ADDRESS;
308
309         block = &wblock->gblock;
310         handle = wblock->handle;
311
312         if (block->instance_count < instance)
313                 return AE_BAD_PARAMETER;
314
315         /* Check GUID is a data block */
316         if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
317                 return AE_BAD_ADDRESS;
318
319         input.count = 1;
320         input.pointer = wq_params;
321         wq_params[0].type = ACPI_TYPE_INTEGER;
322         wq_params[0].integer.value = instance;
323
324         /*
325          * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
326          * enable collection.
327          */
328         if (block->flags & ACPI_WMI_EXPENSIVE) {
329                 wc_input.count = 1;
330                 wc_input.pointer = wc_params;
331                 wc_params[0].type = ACPI_TYPE_INTEGER;
332                 wc_params[0].integer.value = 1;
333
334                 strncat(wc_method, block->object_id, 2);
335
336                 /*
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.
340                  */
341                 wc_status = acpi_get_handle(handle, wc_method, &wc_handle);
342                 if (ACPI_SUCCESS(wc_status))
343                         wc_status = acpi_evaluate_object(handle, wc_method,
344                                 &wc_input, NULL);
345         }
346
347         strcpy(method, "WQ");
348         strncat(method, block->object_id, 2);
349
350         status = acpi_evaluate_object(handle, method, NULL, out);
351
352         /*
353          * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
354          * the WQxx method failed - we should disable collection anyway.
355          */
356         if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
357                 wc_params[0].integer.value = 0;
358                 status = acpi_evaluate_object(handle,
359                 wc_method, &wc_input, NULL);
360         }
361
362         return status;
363 }
364 EXPORT_SYMBOL_GPL(wmi_query_block);
365
366 /**
367  * wmi_set_block - Write to a WMI block
368  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
369  * @instance: Instance index
370  * &in: Buffer containing new values for the data block
371  *
372  * Write the contents of the input buffer to an ACPI-WMI data block
373  */
374 acpi_status wmi_set_block(const char *guid_string, u8 instance,
375 const struct acpi_buffer *in)
376 {
377         struct guid_block *block = NULL;
378         struct wmi_block *wblock = NULL;
379         acpi_handle handle;
380         struct acpi_object_list input;
381         union acpi_object params[2];
382         char method[4] = "WS";
383
384         if (!guid_string || !in)
385                 return AE_BAD_DATA;
386
387         if (!find_guid(guid_string, &wblock))
388                 return AE_BAD_ADDRESS;
389
390         block = &wblock->gblock;
391         handle = wblock->handle;
392
393         if (block->instance_count < instance)
394                 return AE_BAD_PARAMETER;
395
396         /* Check GUID is a data block */
397         if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
398                 return AE_BAD_ADDRESS;
399
400         input.count = 2;
401         input.pointer = params;
402         params[0].type = ACPI_TYPE_INTEGER;
403         params[0].integer.value = instance;
404
405         if (block->flags & ACPI_WMI_STRING) {
406                 params[1].type = ACPI_TYPE_STRING;
407         } else {
408                 params[1].type = ACPI_TYPE_BUFFER;
409         }
410         params[1].buffer.length = in->length;
411         params[1].buffer.pointer = in->pointer;
412
413         strncat(method, block->object_id, 2);
414
415         return acpi_evaluate_object(handle, method, &input, NULL);
416 }
417 EXPORT_SYMBOL_GPL(wmi_set_block);
418
419 /**
420  * wmi_install_notify_handler - Register handler for WMI events
421  * @handler: Function to handle notifications
422  * @data: Data to be returned to handler when event is fired
423  *
424  * Register a handler for events sent to the ACPI-WMI mapper device.
425  */
426 acpi_status wmi_install_notify_handler(const char *guid,
427 wmi_notify_handler handler, void *data)
428 {
429         struct wmi_block *block;
430
431         if (!guid || !handler)
432                 return AE_BAD_PARAMETER;
433
434         find_guid(guid, &block);
435         if (!block)
436                 return AE_NOT_EXIST;
437
438         if (block->handler)
439                 return AE_ALREADY_ACQUIRED;
440
441         block->handler = handler;
442         block->handler_data = data;
443
444         return AE_OK;
445 }
446 EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
447
448 /**
449  * wmi_uninstall_notify_handler - Unregister handler for WMI events
450  *
451  * Unregister handler for events sent to the ACPI-WMI mapper device.
452  */
453 acpi_status wmi_remove_notify_handler(const char *guid)
454 {
455         struct wmi_block *block;
456
457         if (!guid)
458                 return AE_BAD_PARAMETER;
459
460         find_guid(guid, &block);
461         if (!block)
462                 return AE_NOT_EXIST;
463
464         if (!block->handler)
465                 return AE_NULL_ENTRY;
466
467         block->handler = NULL;
468         block->handler_data = NULL;
469
470         return AE_OK;
471 }
472 EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
473
474 /**
475  * wmi_get_event_data - Get WMI data associated with an event
476  *
477  * @event - Event to find
478  * &out - Buffer to hold event data
479  *
480  * Returns extra data associated with an event in WMI.
481  */
482 acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
483 {
484         struct acpi_object_list input;
485         union acpi_object params[1];
486         struct guid_block *gblock;
487         struct wmi_block *wblock;
488         struct list_head *p;
489
490         input.count = 1;
491         input.pointer = params;
492         params[0].type = ACPI_TYPE_INTEGER;
493         params[0].integer.value = event;
494
495         list_for_each(p, &wmi_blocks.list) {
496                 wblock = list_entry(p, struct wmi_block, list);
497                 gblock = &wblock->gblock;
498
499                 if ((gblock->flags & ACPI_WMI_EVENT) &&
500                         (gblock->notify_id == event))
501                         return acpi_evaluate_object(wblock->handle, "_WED",
502                                 &input, out);
503         }
504
505         return AE_NOT_FOUND;
506 }
507 EXPORT_SYMBOL_GPL(wmi_get_event_data);
508
509 /**
510  * wmi_has_guid - Check if a GUID is available
511  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
512  *
513  * Check if a given GUID is defined by _WDG
514  */
515 bool wmi_has_guid(const char *guid_string)
516 {
517         return find_guid(guid_string, NULL);
518 }
519 EXPORT_SYMBOL_GPL(wmi_has_guid);
520
521 /*
522  * Parse the _WDG method for the GUID data blocks
523  */
524 static __init acpi_status parse_wdg(acpi_handle handle)
525 {
526         struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
527         union acpi_object *obj;
528         struct guid_block *gblock;
529         struct wmi_block *wblock;
530         acpi_status status;
531         u32 i, total;
532
533         status = acpi_evaluate_object(handle, "_WDG", NULL, &out);
534
535         if (ACPI_FAILURE(status))
536                 return status;
537
538         obj = (union acpi_object *) out.pointer;
539
540         if (obj->type != ACPI_TYPE_BUFFER)
541                 return AE_ERROR;
542
543         total = obj->buffer.length / sizeof(struct guid_block);
544
545         gblock = kzalloc(obj->buffer.length, GFP_KERNEL);
546         if (!gblock)
547                 return AE_NO_MEMORY;
548
549         memcpy(gblock, obj->buffer.pointer, obj->buffer.length);
550
551         for (i = 0; i < total; i++) {
552                 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
553                 if (!wblock)
554                         return AE_NO_MEMORY;
555
556                 wblock->gblock = gblock[i];
557                 wblock->handle = handle;
558                 list_add_tail(&wblock->list, &wmi_blocks.list);
559         }
560
561         kfree(out.pointer);
562         kfree(gblock);
563
564         return status;
565 }
566
567 /*
568  * WMI can have EmbeddedControl access regions. In which case, we just want to
569  * hand these off to the EC driver.
570  */
571 static acpi_status
572 acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
573                       u32 bits, acpi_integer * value,
574                       void *handler_context, void *region_context)
575 {
576         int result = 0, i = 0;
577         u8 temp = 0;
578
579         if ((address > 0xFF) || !value)
580                 return AE_BAD_PARAMETER;
581
582         if (function != ACPI_READ && function != ACPI_WRITE)
583                 return AE_BAD_PARAMETER;
584
585         if (bits != 8)
586                 return AE_BAD_PARAMETER;
587
588         if (function == ACPI_READ) {
589                 result = ec_read(address, &temp);
590                 (*value) |= ((acpi_integer)temp) << i;
591         } else {
592                 temp = 0xff & ((*value) >> i);
593                 result = ec_write(address, temp);
594         }
595
596         switch (result) {
597         case -EINVAL:
598                 return AE_BAD_PARAMETER;
599                 break;
600         case -ENODEV:
601                 return AE_NOT_FOUND;
602                 break;
603         case -ETIME:
604                 return AE_TIME;
605                 break;
606         default:
607                 return AE_OK;
608         }
609 }
610
611 static void acpi_wmi_notify(acpi_handle handle, u32 event, void *data)
612 {
613         struct guid_block *block;
614         struct wmi_block *wblock;
615         struct list_head *p;
616         struct acpi_device *device = data;
617
618         list_for_each(p, &wmi_blocks.list) {
619                 wblock = list_entry(p, struct wmi_block, list);
620                 block = &wblock->gblock;
621
622                 if ((block->flags & ACPI_WMI_EVENT) &&
623                         (block->notify_id == event)) {
624                         if (wblock->handler)
625                                 wblock->handler(event, wblock->handler_data);
626
627                         acpi_bus_generate_netlink_event(
628                                 device->pnp.device_class, device->dev.bus_id,
629                                 event, 0);
630                         break;
631                 }
632         }
633 }
634
635 static int acpi_wmi_remove(struct acpi_device *device, int type)
636 {
637         acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
638                 acpi_wmi_notify);
639
640         acpi_remove_address_space_handler(device->handle,
641                                 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
642
643         return 0;
644 }
645
646 static int __init acpi_wmi_add(struct acpi_device *device)
647 {
648         acpi_status status;
649         int result = 0;
650
651         status = acpi_install_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
652                 acpi_wmi_notify, device);
653         if (ACPI_FAILURE(status)) {
654                 printk(KERN_ERR PREFIX "Error installing notify handler\n");
655                 return -ENODEV;
656         }
657
658         status = acpi_install_address_space_handler(device->handle,
659                                                     ACPI_ADR_SPACE_EC,
660                                                     &acpi_wmi_ec_space_handler,
661                                                     NULL, NULL);
662         if (ACPI_FAILURE(status))
663                 return -ENODEV;
664
665         status = parse_wdg(device->handle);
666         if (ACPI_FAILURE(status)) {
667                 printk(KERN_ERR PREFIX "Error installing EC region handler\n");
668                 return -ENODEV;
669         }
670
671         return result;
672 }
673
674 static int __init acpi_wmi_init(void)
675 {
676         acpi_status result;
677
678         INIT_LIST_HEAD(&wmi_blocks.list);
679
680         if (acpi_disabled)
681                 return -ENODEV;
682
683         result = acpi_bus_register_driver(&acpi_wmi_driver);
684
685         if (result < 0) {
686                 printk(KERN_INFO PREFIX "Error loading mapper\n");
687         } else {
688                 printk(KERN_INFO PREFIX "Mapper loaded\n");
689         }
690
691         return result;
692 }
693
694 static void __exit acpi_wmi_exit(void)
695 {
696         struct list_head *p, *tmp;
697         struct wmi_block *wblock;
698
699         acpi_bus_unregister_driver(&acpi_wmi_driver);
700
701         list_for_each_safe(p, tmp, &wmi_blocks.list) {
702                 wblock = list_entry(p, struct wmi_block, list);
703
704                 list_del(p);
705                 kfree(wblock);
706         }
707
708         printk(KERN_INFO PREFIX "Mapper unloaded\n");
709 }
710
711 subsys_initcall(acpi_wmi_init);
712 module_exit(acpi_wmi_exit);