ocfs2: Handle O_DIRECT when writing to a refcounted cluster.
[safe/jmp/linux-2.6] / drivers / platform / x86 / 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/device.h>
34 #include <linux/list.h>
35 #include <linux/acpi.h>
36 #include <acpi/acpi_bus.h>
37 #include <acpi/acpi_drivers.h>
38
39 ACPI_MODULE_NAME("wmi");
40 MODULE_AUTHOR("Carlos Corbacho");
41 MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
42 MODULE_LICENSE("GPL");
43
44 #define ACPI_WMI_CLASS "wmi"
45
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         struct device *dev;
70 };
71
72 static struct wmi_block wmi_blocks;
73
74 /*
75  * If the GUID data block is marked as expensive, we must enable and
76  * explicitily disable data collection.
77  */
78 #define ACPI_WMI_EXPENSIVE   0x1
79 #define ACPI_WMI_METHOD      0x2        /* GUID is a method */
80 #define ACPI_WMI_STRING      0x4        /* GUID takes & returns a string */
81 #define ACPI_WMI_EVENT       0x8        /* GUID is an event */
82
83 static int acpi_wmi_remove(struct acpi_device *device, int type);
84 static int acpi_wmi_add(struct acpi_device *device);
85 static void acpi_wmi_notify(struct acpi_device *device, u32 event);
86
87 static const struct acpi_device_id wmi_device_ids[] = {
88         {"PNP0C14", 0},
89         {"pnp0c14", 0},
90         {"", 0},
91 };
92 MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
93
94 static struct acpi_driver acpi_wmi_driver = {
95         .name = "wmi",
96         .class = ACPI_WMI_CLASS,
97         .ids = wmi_device_ids,
98         .ops = {
99                 .add = acpi_wmi_add,
100                 .remove = acpi_wmi_remove,
101                 .notify = acpi_wmi_notify,
102                 },
103 };
104
105 /*
106  * GUID parsing functions
107  */
108
109 /**
110  * wmi_parse_hexbyte - Convert a ASCII hex number to a byte
111  * @src:  Pointer to at least 2 characters to convert.
112  *
113  * Convert a two character ASCII hex string to a number.
114  *
115  * Return:  0-255  Success, the byte was parsed correctly
116  *          -1     Error, an invalid character was supplied
117  */
118 static int wmi_parse_hexbyte(const u8 *src)
119 {
120         unsigned int x; /* For correct wrapping */
121         int h;
122
123         /* high part */
124         x = src[0];
125         if (x - '0' <= '9' - '0') {
126                 h = x - '0';
127         } else if (x - 'a' <= 'f' - 'a') {
128                 h = x - 'a' + 10;
129         } else if (x - 'A' <= 'F' - 'A') {
130                 h = x - 'A' + 10;
131         } else {
132                 return -1;
133         }
134         h <<= 4;
135
136         /* low part */
137         x = src[1];
138         if (x - '0' <= '9' - '0')
139                 return h | (x - '0');
140         if (x - 'a' <= 'f' - 'a')
141                 return h | (x - 'a' + 10);
142         if (x - 'A' <= 'F' - 'A')
143                 return h | (x - 'A' + 10);
144         return -1;
145 }
146
147 /**
148  * wmi_swap_bytes - Rearrange GUID bytes to match GUID binary
149  * @src:   Memory block holding binary GUID (16 bytes)
150  * @dest:  Memory block to hold byte swapped binary GUID (16 bytes)
151  *
152  * Byte swap a binary GUID to match it's real GUID value
153  */
154 static void wmi_swap_bytes(u8 *src, u8 *dest)
155 {
156         int i;
157
158         for (i = 0; i <= 3; i++)
159                 memcpy(dest + i, src + (3 - i), 1);
160
161         for (i = 0; i <= 1; i++)
162                 memcpy(dest + 4 + i, src + (5 - i), 1);
163
164         for (i = 0; i <= 1; i++)
165                 memcpy(dest + 6 + i, src + (7 - i), 1);
166
167         memcpy(dest + 8, src + 8, 8);
168 }
169
170 /**
171  * wmi_parse_guid - Convert GUID from ASCII to binary
172  * @src:   36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
173  * @dest:  Memory block to hold binary GUID (16 bytes)
174  *
175  * N.B. The GUID need not be NULL terminated.
176  *
177  * Return:  'true'   @dest contains binary GUID
178  *          'false'  @dest contents are undefined
179  */
180 static bool wmi_parse_guid(const u8 *src, u8 *dest)
181 {
182         static const int size[] = { 4, 2, 2, 2, 6 };
183         int i, j, v;
184
185         if (src[8]  != '-' || src[13] != '-' ||
186                 src[18] != '-' || src[23] != '-')
187                 return false;
188
189         for (j = 0; j < 5; j++, src++) {
190                 for (i = 0; i < size[j]; i++, src += 2, *dest++ = v) {
191                         v = wmi_parse_hexbyte(src);
192                         if (v < 0)
193                                 return false;
194                 }
195         }
196
197         return true;
198 }
199
200 /*
201  * Convert a raw GUID to the ACII string representation
202  */
203 static int wmi_gtoa(const char *in, char *out)
204 {
205         int i;
206
207         for (i = 3; i >= 0; i--)
208                 out += sprintf(out, "%02X", in[i] & 0xFF);
209
210         out += sprintf(out, "-");
211         out += sprintf(out, "%02X", in[5] & 0xFF);
212         out += sprintf(out, "%02X", in[4] & 0xFF);
213         out += sprintf(out, "-");
214         out += sprintf(out, "%02X", in[7] & 0xFF);
215         out += sprintf(out, "%02X", in[6] & 0xFF);
216         out += sprintf(out, "-");
217         out += sprintf(out, "%02X", in[8] & 0xFF);
218         out += sprintf(out, "%02X", in[9] & 0xFF);
219         out += sprintf(out, "-");
220
221         for (i = 10; i <= 15; i++)
222                 out += sprintf(out, "%02X", in[i] & 0xFF);
223
224         out = '\0';
225         return 0;
226 }
227
228 static bool find_guid(const char *guid_string, struct wmi_block **out)
229 {
230         char tmp[16], guid_input[16];
231         struct wmi_block *wblock;
232         struct guid_block *block;
233         struct list_head *p;
234
235         wmi_parse_guid(guid_string, tmp);
236         wmi_swap_bytes(tmp, guid_input);
237
238         list_for_each(p, &wmi_blocks.list) {
239                 wblock = list_entry(p, struct wmi_block, list);
240                 block = &wblock->gblock;
241
242                 if (memcmp(block->guid, guid_input, 16) == 0) {
243                         if (out)
244                                 *out = wblock;
245                         return 1;
246                 }
247         }
248         return 0;
249 }
250
251 static acpi_status wmi_method_enable(struct wmi_block *wblock, int enable)
252 {
253         struct guid_block *block = NULL;
254         char method[5];
255         struct acpi_object_list input;
256         union acpi_object params[1];
257         acpi_status status;
258         acpi_handle handle;
259
260         block = &wblock->gblock;
261         handle = wblock->handle;
262
263         if (!block)
264                 return AE_NOT_EXIST;
265
266         input.count = 1;
267         input.pointer = params;
268         params[0].type = ACPI_TYPE_INTEGER;
269         params[0].integer.value = enable;
270
271         snprintf(method, 5, "WE%02X", block->notify_id);
272         status = acpi_evaluate_object(handle, method, &input, NULL);
273
274         if (status != AE_OK && status != AE_NOT_FOUND)
275                 return status;
276         else
277                 return AE_OK;
278 }
279
280 /*
281  * Exported WMI functions
282  */
283 /**
284  * wmi_evaluate_method - Evaluate a WMI method
285  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
286  * @instance: Instance index
287  * @method_id: Method ID to call
288  * &in: Buffer containing input for the method call
289  * &out: Empty buffer to return the method results
290  *
291  * Call an ACPI-WMI method
292  */
293 acpi_status wmi_evaluate_method(const char *guid_string, u8 instance,
294 u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
295 {
296         struct guid_block *block = NULL;
297         struct wmi_block *wblock = NULL;
298         acpi_handle handle;
299         acpi_status status;
300         struct acpi_object_list input;
301         union acpi_object params[3];
302         char method[5] = "WM";
303
304         if (!find_guid(guid_string, &wblock))
305                 return AE_ERROR;
306
307         block = &wblock->gblock;
308         handle = wblock->handle;
309
310         if (!(block->flags & ACPI_WMI_METHOD))
311                 return AE_BAD_DATA;
312
313         if (block->instance_count < instance)
314                 return AE_BAD_PARAMETER;
315
316         input.count = 2;
317         input.pointer = params;
318         params[0].type = ACPI_TYPE_INTEGER;
319         params[0].integer.value = instance;
320         params[1].type = ACPI_TYPE_INTEGER;
321         params[1].integer.value = method_id;
322
323         if (in) {
324                 input.count = 3;
325
326                 if (block->flags & ACPI_WMI_STRING) {
327                         params[2].type = ACPI_TYPE_STRING;
328                 } else {
329                         params[2].type = ACPI_TYPE_BUFFER;
330                 }
331                 params[2].buffer.length = in->length;
332                 params[2].buffer.pointer = in->pointer;
333         }
334
335         strncat(method, block->object_id, 2);
336
337         status = acpi_evaluate_object(handle, method, &input, out);
338
339         return status;
340 }
341 EXPORT_SYMBOL_GPL(wmi_evaluate_method);
342
343 /**
344  * wmi_query_block - Return contents of a WMI block
345  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
346  * @instance: Instance index
347  * &out: Empty buffer to return the contents of the data block to
348  *
349  * Return the contents of an ACPI-WMI data block to a buffer
350  */
351 acpi_status wmi_query_block(const char *guid_string, u8 instance,
352 struct acpi_buffer *out)
353 {
354         struct guid_block *block = NULL;
355         struct wmi_block *wblock = NULL;
356         acpi_handle handle, wc_handle;
357         acpi_status status, wc_status = AE_ERROR;
358         struct acpi_object_list input, wc_input;
359         union acpi_object wc_params[1], wq_params[1];
360         char method[5];
361         char wc_method[5] = "WC";
362
363         if (!guid_string || !out)
364                 return AE_BAD_PARAMETER;
365
366         if (!find_guid(guid_string, &wblock))
367                 return AE_ERROR;
368
369         block = &wblock->gblock;
370         handle = wblock->handle;
371
372         if (block->instance_count < instance)
373                 return AE_BAD_PARAMETER;
374
375         /* Check GUID is a data block */
376         if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
377                 return AE_ERROR;
378
379         input.count = 1;
380         input.pointer = wq_params;
381         wq_params[0].type = ACPI_TYPE_INTEGER;
382         wq_params[0].integer.value = instance;
383
384         /*
385          * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
386          * enable collection.
387          */
388         if (block->flags & ACPI_WMI_EXPENSIVE) {
389                 wc_input.count = 1;
390                 wc_input.pointer = wc_params;
391                 wc_params[0].type = ACPI_TYPE_INTEGER;
392                 wc_params[0].integer.value = 1;
393
394                 strncat(wc_method, block->object_id, 2);
395
396                 /*
397                  * Some GUIDs break the specification by declaring themselves
398                  * expensive, but have no corresponding WCxx method. So we
399                  * should not fail if this happens.
400                  */
401                 wc_status = acpi_get_handle(handle, wc_method, &wc_handle);
402                 if (ACPI_SUCCESS(wc_status))
403                         wc_status = acpi_evaluate_object(handle, wc_method,
404                                 &wc_input, NULL);
405         }
406
407         strcpy(method, "WQ");
408         strncat(method, block->object_id, 2);
409
410         status = acpi_evaluate_object(handle, method, &input, out);
411
412         /*
413          * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
414          * the WQxx method failed - we should disable collection anyway.
415          */
416         if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
417                 wc_params[0].integer.value = 0;
418                 status = acpi_evaluate_object(handle,
419                 wc_method, &wc_input, NULL);
420         }
421
422         return status;
423 }
424 EXPORT_SYMBOL_GPL(wmi_query_block);
425
426 /**
427  * wmi_set_block - Write to a WMI block
428  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
429  * @instance: Instance index
430  * &in: Buffer containing new values for the data block
431  *
432  * Write the contents of the input buffer to an ACPI-WMI data block
433  */
434 acpi_status wmi_set_block(const char *guid_string, u8 instance,
435 const struct acpi_buffer *in)
436 {
437         struct guid_block *block = NULL;
438         struct wmi_block *wblock = NULL;
439         acpi_handle handle;
440         struct acpi_object_list input;
441         union acpi_object params[2];
442         char method[5] = "WS";
443
444         if (!guid_string || !in)
445                 return AE_BAD_DATA;
446
447         if (!find_guid(guid_string, &wblock))
448                 return AE_ERROR;
449
450         block = &wblock->gblock;
451         handle = wblock->handle;
452
453         if (block->instance_count < instance)
454                 return AE_BAD_PARAMETER;
455
456         /* Check GUID is a data block */
457         if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
458                 return AE_ERROR;
459
460         input.count = 2;
461         input.pointer = params;
462         params[0].type = ACPI_TYPE_INTEGER;
463         params[0].integer.value = instance;
464
465         if (block->flags & ACPI_WMI_STRING) {
466                 params[1].type = ACPI_TYPE_STRING;
467         } else {
468                 params[1].type = ACPI_TYPE_BUFFER;
469         }
470         params[1].buffer.length = in->length;
471         params[1].buffer.pointer = in->pointer;
472
473         strncat(method, block->object_id, 2);
474
475         return acpi_evaluate_object(handle, method, &input, NULL);
476 }
477 EXPORT_SYMBOL_GPL(wmi_set_block);
478
479 /**
480  * wmi_install_notify_handler - Register handler for WMI events
481  * @handler: Function to handle notifications
482  * @data: Data to be returned to handler when event is fired
483  *
484  * Register a handler for events sent to the ACPI-WMI mapper device.
485  */
486 acpi_status wmi_install_notify_handler(const char *guid,
487 wmi_notify_handler handler, void *data)
488 {
489         struct wmi_block *block;
490         acpi_status status;
491
492         if (!guid || !handler)
493                 return AE_BAD_PARAMETER;
494
495         find_guid(guid, &block);
496         if (!block)
497                 return AE_NOT_EXIST;
498
499         if (block->handler)
500                 return AE_ALREADY_ACQUIRED;
501
502         block->handler = handler;
503         block->handler_data = data;
504
505         status = wmi_method_enable(block, 1);
506
507         return status;
508 }
509 EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
510
511 /**
512  * wmi_uninstall_notify_handler - Unregister handler for WMI events
513  *
514  * Unregister handler for events sent to the ACPI-WMI mapper device.
515  */
516 acpi_status wmi_remove_notify_handler(const char *guid)
517 {
518         struct wmi_block *block;
519         acpi_status status;
520
521         if (!guid)
522                 return AE_BAD_PARAMETER;
523
524         find_guid(guid, &block);
525         if (!block)
526                 return AE_NOT_EXIST;
527
528         if (!block->handler)
529                 return AE_NULL_ENTRY;
530
531         status = wmi_method_enable(block, 0);
532
533         block->handler = NULL;
534         block->handler_data = NULL;
535
536         return status;
537 }
538 EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
539
540 /**
541  * wmi_get_event_data - Get WMI data associated with an event
542  *
543  * @event: Event to find
544  * @out: Buffer to hold event data. out->pointer should be freed with kfree()
545  *
546  * Returns extra data associated with an event in WMI.
547  */
548 acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
549 {
550         struct acpi_object_list input;
551         union acpi_object params[1];
552         struct guid_block *gblock;
553         struct wmi_block *wblock;
554         struct list_head *p;
555
556         input.count = 1;
557         input.pointer = params;
558         params[0].type = ACPI_TYPE_INTEGER;
559         params[0].integer.value = event;
560
561         list_for_each(p, &wmi_blocks.list) {
562                 wblock = list_entry(p, struct wmi_block, list);
563                 gblock = &wblock->gblock;
564
565                 if ((gblock->flags & ACPI_WMI_EVENT) &&
566                         (gblock->notify_id == event))
567                         return acpi_evaluate_object(wblock->handle, "_WED",
568                                 &input, out);
569         }
570
571         return AE_NOT_FOUND;
572 }
573 EXPORT_SYMBOL_GPL(wmi_get_event_data);
574
575 /**
576  * wmi_has_guid - Check if a GUID is available
577  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
578  *
579  * Check if a given GUID is defined by _WDG
580  */
581 bool wmi_has_guid(const char *guid_string)
582 {
583         return find_guid(guid_string, NULL);
584 }
585 EXPORT_SYMBOL_GPL(wmi_has_guid);
586
587 /*
588  * sysfs interface
589  */
590 static ssize_t show_modalias(struct device *dev, struct device_attribute *attr,
591                              char *buf)
592 {
593         char guid_string[37];
594         struct wmi_block *wblock;
595
596         wblock = dev_get_drvdata(dev);
597         if (!wblock)
598                 return -ENOMEM;
599
600         wmi_gtoa(wblock->gblock.guid, guid_string);
601
602         return sprintf(buf, "wmi:%s\n", guid_string);
603 }
604 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
605
606 static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
607 {
608         char guid_string[37];
609
610         struct wmi_block *wblock;
611
612         if (add_uevent_var(env, "MODALIAS="))
613                 return -ENOMEM;
614
615         wblock = dev_get_drvdata(dev);
616         if (!wblock)
617                 return -ENOMEM;
618
619         wmi_gtoa(wblock->gblock.guid, guid_string);
620
621         strcpy(&env->buf[env->buflen - 1], "wmi:");
622         memcpy(&env->buf[env->buflen - 1 + 4], guid_string, 36);
623         env->buflen += 40;
624
625         return 0;
626 }
627
628 static void wmi_dev_free(struct device *dev)
629 {
630         kfree(dev);
631 }
632
633 static struct class wmi_class = {
634         .name = "wmi",
635         .dev_release = wmi_dev_free,
636         .dev_uevent = wmi_dev_uevent,
637 };
638
639 static int wmi_create_devs(void)
640 {
641         int result;
642         char guid_string[37];
643         struct guid_block *gblock;
644         struct wmi_block *wblock;
645         struct list_head *p;
646         struct device *guid_dev;
647
648         /* Create devices for all the GUIDs */
649         list_for_each(p, &wmi_blocks.list) {
650                 wblock = list_entry(p, struct wmi_block, list);
651
652                 guid_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
653                 if (!guid_dev)
654                         return -ENOMEM;
655
656                 wblock->dev = guid_dev;
657
658                 guid_dev->class = &wmi_class;
659                 dev_set_drvdata(guid_dev, wblock);
660
661                 gblock = &wblock->gblock;
662
663                 wmi_gtoa(gblock->guid, guid_string);
664                 dev_set_name(guid_dev, guid_string);
665
666                 result = device_register(guid_dev);
667                 if (result)
668                         return result;
669
670                 result = device_create_file(guid_dev, &dev_attr_modalias);
671                 if (result)
672                         return result;
673         }
674
675         return 0;
676 }
677
678 static void wmi_remove_devs(void)
679 {
680         struct guid_block *gblock;
681         struct wmi_block *wblock;
682         struct list_head *p;
683         struct device *guid_dev;
684
685         /* Delete devices for all the GUIDs */
686         list_for_each(p, &wmi_blocks.list) {
687                 wblock = list_entry(p, struct wmi_block, list);
688
689                 guid_dev = wblock->dev;
690                 gblock = &wblock->gblock;
691
692                 device_remove_file(guid_dev, &dev_attr_modalias);
693
694                 device_unregister(guid_dev);
695         }
696 }
697
698 static void wmi_class_exit(void)
699 {
700         wmi_remove_devs();
701         class_unregister(&wmi_class);
702 }
703
704 static int wmi_class_init(void)
705 {
706         int ret;
707
708         ret = class_register(&wmi_class);
709         if (ret)
710                 return ret;
711
712         ret = wmi_create_devs();
713         if (ret)
714                 wmi_class_exit();
715
716         return ret;
717 }
718
719 /*
720  * Parse the _WDG method for the GUID data blocks
721  */
722 static __init acpi_status parse_wdg(acpi_handle handle)
723 {
724         struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
725         union acpi_object *obj;
726         struct guid_block *gblock;
727         struct wmi_block *wblock;
728         acpi_status status;
729         u32 i, total;
730
731         status = acpi_evaluate_object(handle, "_WDG", NULL, &out);
732
733         if (ACPI_FAILURE(status))
734                 return status;
735
736         obj = (union acpi_object *) out.pointer;
737
738         if (obj->type != ACPI_TYPE_BUFFER)
739                 return AE_ERROR;
740
741         total = obj->buffer.length / sizeof(struct guid_block);
742
743         gblock = kzalloc(obj->buffer.length, GFP_KERNEL);
744         if (!gblock)
745                 return AE_NO_MEMORY;
746
747         memcpy(gblock, obj->buffer.pointer, obj->buffer.length);
748
749         for (i = 0; i < total; i++) {
750                 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
751                 if (!wblock)
752                         return AE_NO_MEMORY;
753
754                 wblock->gblock = gblock[i];
755                 wblock->handle = handle;
756                 list_add_tail(&wblock->list, &wmi_blocks.list);
757         }
758
759         kfree(out.pointer);
760         kfree(gblock);
761
762         return status;
763 }
764
765 /*
766  * WMI can have EmbeddedControl access regions. In which case, we just want to
767  * hand these off to the EC driver.
768  */
769 static acpi_status
770 acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
771                       u32 bits, acpi_integer * value,
772                       void *handler_context, void *region_context)
773 {
774         int result = 0, i = 0;
775         u8 temp = 0;
776
777         if ((address > 0xFF) || !value)
778                 return AE_BAD_PARAMETER;
779
780         if (function != ACPI_READ && function != ACPI_WRITE)
781                 return AE_BAD_PARAMETER;
782
783         if (bits != 8)
784                 return AE_BAD_PARAMETER;
785
786         if (function == ACPI_READ) {
787                 result = ec_read(address, &temp);
788                 (*value) |= ((acpi_integer)temp) << i;
789         } else {
790                 temp = 0xff & ((*value) >> i);
791                 result = ec_write(address, temp);
792         }
793
794         switch (result) {
795         case -EINVAL:
796                 return AE_BAD_PARAMETER;
797                 break;
798         case -ENODEV:
799                 return AE_NOT_FOUND;
800                 break;
801         case -ETIME:
802                 return AE_TIME;
803                 break;
804         default:
805                 return AE_OK;
806         }
807 }
808
809 static void acpi_wmi_notify(struct acpi_device *device, u32 event)
810 {
811         struct guid_block *block;
812         struct wmi_block *wblock;
813         struct list_head *p;
814
815         list_for_each(p, &wmi_blocks.list) {
816                 wblock = list_entry(p, struct wmi_block, list);
817                 block = &wblock->gblock;
818
819                 if ((block->flags & ACPI_WMI_EVENT) &&
820                         (block->notify_id == event)) {
821                         if (wblock->handler)
822                                 wblock->handler(event, wblock->handler_data);
823
824                         acpi_bus_generate_netlink_event(
825                                 device->pnp.device_class, dev_name(&device->dev),
826                                 event, 0);
827                         break;
828                 }
829         }
830 }
831
832 static int acpi_wmi_remove(struct acpi_device *device, int type)
833 {
834         acpi_remove_address_space_handler(device->handle,
835                                 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
836
837         return 0;
838 }
839
840 static int __init acpi_wmi_add(struct acpi_device *device)
841 {
842         acpi_status status;
843         int result = 0;
844
845         status = acpi_install_address_space_handler(device->handle,
846                                                     ACPI_ADR_SPACE_EC,
847                                                     &acpi_wmi_ec_space_handler,
848                                                     NULL, NULL);
849         if (ACPI_FAILURE(status))
850                 return -ENODEV;
851
852         status = parse_wdg(device->handle);
853         if (ACPI_FAILURE(status)) {
854                 printk(KERN_ERR PREFIX "Error installing EC region handler\n");
855                 return -ENODEV;
856         }
857
858         return result;
859 }
860
861 static int __init acpi_wmi_init(void)
862 {
863         int result;
864
865         INIT_LIST_HEAD(&wmi_blocks.list);
866
867         if (acpi_disabled)
868                 return -ENODEV;
869
870         result = acpi_bus_register_driver(&acpi_wmi_driver);
871
872         if (result < 0) {
873                 printk(KERN_INFO PREFIX "Error loading mapper\n");
874                 return -ENODEV;
875         }
876
877         result = wmi_class_init();
878         if (result) {
879                 acpi_bus_unregister_driver(&acpi_wmi_driver);
880                 return result;
881         }
882
883         printk(KERN_INFO PREFIX "Mapper loaded\n");
884
885         return result;
886 }
887
888 static void __exit acpi_wmi_exit(void)
889 {
890         struct list_head *p, *tmp;
891         struct wmi_block *wblock;
892
893         wmi_class_exit();
894
895         acpi_bus_unregister_driver(&acpi_wmi_driver);
896
897         list_for_each_safe(p, tmp, &wmi_blocks.list) {
898                 wblock = list_entry(p, struct wmi_block, list);
899
900                 list_del(p);
901                 kfree(wblock);
902         }
903
904         printk(KERN_INFO PREFIX "Mapper unloaded\n");
905 }
906
907 subsys_initcall(acpi_wmi_init);
908 module_exit(acpi_wmi_exit);