]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/platform/x86/wmi.c
platform/x86: wmi: add context argument to the probe function
[linux.git] / 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  *  WMI bus infrastructure by Andrew Lutomirski and Darren Hart:
12  *    Copyright (C) 2015 Andrew Lutomirski
13  *    Copyright (C) 2017 VMware, Inc. All Rights Reserved.
14  *
15  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16  *
17  *  This program is free software; you can redistribute it and/or modify
18  *  it under the terms of the GNU General Public License as published by
19  *  the Free Software Foundation; either version 2 of the License, or (at
20  *  your option) any later version.
21  *
22  *  This program is distributed in the hope that it will be useful, but
23  *  WITHOUT ANY WARRANTY; without even the implied warranty of
24  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25  *  General Public License for more details.
26  *
27  *  You should have received a copy of the GNU General Public License along
28  *  with this program; if not, write to the Free Software Foundation, Inc.,
29  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
30  *
31  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32  */
33
34 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
35
36 #include <linux/acpi.h>
37 #include <linux/device.h>
38 #include <linux/init.h>
39 #include <linux/kernel.h>
40 #include <linux/list.h>
41 #include <linux/miscdevice.h>
42 #include <linux/module.h>
43 #include <linux/platform_device.h>
44 #include <linux/slab.h>
45 #include <linux/types.h>
46 #include <linux/uaccess.h>
47 #include <linux/uuid.h>
48 #include <linux/wmi.h>
49 #include <uapi/linux/wmi.h>
50
51 ACPI_MODULE_NAME("wmi");
52 MODULE_AUTHOR("Carlos Corbacho");
53 MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
54 MODULE_LICENSE("GPL");
55
56 static LIST_HEAD(wmi_block_list);
57
58 struct guid_block {
59         char guid[16];
60         union {
61                 char object_id[2];
62                 struct {
63                         unsigned char notify_id;
64                         unsigned char reserved;
65                 };
66         };
67         u8 instance_count;
68         u8 flags;
69 };
70
71 struct wmi_block {
72         struct wmi_device dev;
73         struct list_head list;
74         struct guid_block gblock;
75         struct miscdevice char_dev;
76         struct mutex char_mutex;
77         struct acpi_device *acpi_device;
78         wmi_notify_handler handler;
79         void *handler_data;
80         u64 req_buf_size;
81
82         bool read_takes_no_args;
83 };
84
85
86 /*
87  * If the GUID data block is marked as expensive, we must enable and
88  * explicitily disable data collection.
89  */
90 #define ACPI_WMI_EXPENSIVE   0x1
91 #define ACPI_WMI_METHOD      0x2        /* GUID is a method */
92 #define ACPI_WMI_STRING      0x4        /* GUID takes & returns a string */
93 #define ACPI_WMI_EVENT       0x8        /* GUID is an event */
94
95 static bool debug_event;
96 module_param(debug_event, bool, 0444);
97 MODULE_PARM_DESC(debug_event,
98                  "Log WMI Events [0/1]");
99
100 static bool debug_dump_wdg;
101 module_param(debug_dump_wdg, bool, 0444);
102 MODULE_PARM_DESC(debug_dump_wdg,
103                  "Dump available WMI interfaces [0/1]");
104
105 static int acpi_wmi_remove(struct platform_device *device);
106 static int acpi_wmi_probe(struct platform_device *device);
107
108 static const struct acpi_device_id wmi_device_ids[] = {
109         {"PNP0C14", 0},
110         {"pnp0c14", 0},
111         {"", 0},
112 };
113 MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
114
115 static struct platform_driver acpi_wmi_driver = {
116         .driver = {
117                 .name = "acpi-wmi",
118                 .acpi_match_table = wmi_device_ids,
119         },
120         .probe = acpi_wmi_probe,
121         .remove = acpi_wmi_remove,
122 };
123
124 /*
125  * GUID parsing functions
126  */
127
128 static bool find_guid(const char *guid_string, struct wmi_block **out)
129 {
130         uuid_le guid_input;
131         struct wmi_block *wblock;
132         struct guid_block *block;
133
134         if (uuid_le_to_bin(guid_string, &guid_input))
135                 return false;
136
137         list_for_each_entry(wblock, &wmi_block_list, list) {
138                 block = &wblock->gblock;
139
140                 if (memcmp(block->guid, &guid_input, 16) == 0) {
141                         if (out)
142                                 *out = wblock;
143                         return true;
144                 }
145         }
146         return false;
147 }
148
149 static const void *find_guid_context(struct wmi_block *wblock,
150                                       struct wmi_driver *wdriver)
151 {
152         const struct wmi_device_id *id;
153         uuid_le guid_input;
154
155         if (wblock == NULL || wdriver == NULL)
156                 return NULL;
157         if (wdriver->id_table == NULL)
158                 return NULL;
159
160         id = wdriver->id_table;
161         while (*id->guid_string) {
162                 if (uuid_le_to_bin(id->guid_string, &guid_input))
163                         continue;
164                 if (!memcmp(wblock->gblock.guid, &guid_input, 16))
165                         return id->context;
166                 id++;
167         }
168         return NULL;
169 }
170
171 static int get_subobj_info(acpi_handle handle, const char *pathname,
172                            struct acpi_device_info **info)
173 {
174         struct acpi_device_info *dummy_info, **info_ptr;
175         acpi_handle subobj_handle;
176         acpi_status status;
177
178         status = acpi_get_handle(handle, (char *)pathname, &subobj_handle);
179         if (status == AE_NOT_FOUND)
180                 return -ENOENT;
181         else if (ACPI_FAILURE(status))
182                 return -EIO;
183
184         info_ptr = info ? info : &dummy_info;
185         status = acpi_get_object_info(subobj_handle, info_ptr);
186         if (ACPI_FAILURE(status))
187                 return -EIO;
188
189         if (!info)
190                 kfree(dummy_info);
191
192         return 0;
193 }
194
195 static acpi_status wmi_method_enable(struct wmi_block *wblock, int enable)
196 {
197         struct guid_block *block = NULL;
198         char method[5];
199         acpi_status status;
200         acpi_handle handle;
201
202         block = &wblock->gblock;
203         handle = wblock->acpi_device->handle;
204
205         snprintf(method, 5, "WE%02X", block->notify_id);
206         status = acpi_execute_simple_method(handle, method, enable);
207
208         if (status != AE_OK && status != AE_NOT_FOUND)
209                 return status;
210         else
211                 return AE_OK;
212 }
213
214 /*
215  * Exported WMI functions
216  */
217
218 /**
219  * set_required_buffer_size - Sets the buffer size needed for performing IOCTL
220  * @wdev: A wmi bus device from a driver
221  * @instance: Instance index
222  *
223  * Allocates memory needed for buffer, stores the buffer size in that memory
224  */
225 int set_required_buffer_size(struct wmi_device *wdev, u64 length)
226 {
227         struct wmi_block *wblock;
228
229         wblock = container_of(wdev, struct wmi_block, dev);
230         wblock->req_buf_size = length;
231
232         return 0;
233 }
234 EXPORT_SYMBOL_GPL(set_required_buffer_size);
235
236 /**
237  * wmi_evaluate_method - Evaluate a WMI method
238  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
239  * @instance: Instance index
240  * @method_id: Method ID to call
241  * &in: Buffer containing input for the method call
242  * &out: Empty buffer to return the method results
243  *
244  * Call an ACPI-WMI method
245  */
246 acpi_status wmi_evaluate_method(const char *guid_string, u8 instance,
247 u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
248 {
249         struct wmi_block *wblock = NULL;
250
251         if (!find_guid(guid_string, &wblock))
252                 return AE_ERROR;
253         return wmidev_evaluate_method(&wblock->dev, instance, method_id,
254                                       in, out);
255 }
256 EXPORT_SYMBOL_GPL(wmi_evaluate_method);
257
258 /**
259  * wmidev_evaluate_method - Evaluate a WMI method
260  * @wdev: A wmi bus device from a driver
261  * @instance: Instance index
262  * @method_id: Method ID to call
263  * &in: Buffer containing input for the method call
264  * &out: Empty buffer to return the method results
265  *
266  * Call an ACPI-WMI method
267  */
268 acpi_status wmidev_evaluate_method(struct wmi_device *wdev, u8 instance,
269         u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
270 {
271         struct guid_block *block = NULL;
272         struct wmi_block *wblock = NULL;
273         acpi_handle handle;
274         acpi_status status;
275         struct acpi_object_list input;
276         union acpi_object params[3];
277         char method[5] = "WM";
278
279         wblock = container_of(wdev, struct wmi_block, dev);
280         block = &wblock->gblock;
281         handle = wblock->acpi_device->handle;
282
283         if (!(block->flags & ACPI_WMI_METHOD))
284                 return AE_BAD_DATA;
285
286         if (block->instance_count <= instance)
287                 return AE_BAD_PARAMETER;
288
289         input.count = 2;
290         input.pointer = params;
291         params[0].type = ACPI_TYPE_INTEGER;
292         params[0].integer.value = instance;
293         params[1].type = ACPI_TYPE_INTEGER;
294         params[1].integer.value = method_id;
295
296         if (in) {
297                 input.count = 3;
298
299                 if (block->flags & ACPI_WMI_STRING) {
300                         params[2].type = ACPI_TYPE_STRING;
301                 } else {
302                         params[2].type = ACPI_TYPE_BUFFER;
303                 }
304                 params[2].buffer.length = in->length;
305                 params[2].buffer.pointer = in->pointer;
306         }
307
308         strncat(method, block->object_id, 2);
309
310         status = acpi_evaluate_object(handle, method, &input, out);
311
312         return status;
313 }
314 EXPORT_SYMBOL_GPL(wmidev_evaluate_method);
315
316 static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
317                                  struct acpi_buffer *out)
318 {
319         struct guid_block *block = NULL;
320         acpi_handle handle;
321         acpi_status status, wc_status = AE_ERROR;
322         struct acpi_object_list input;
323         union acpi_object wq_params[1];
324         char method[5];
325         char wc_method[5] = "WC";
326
327         if (!out)
328                 return AE_BAD_PARAMETER;
329
330         block = &wblock->gblock;
331         handle = wblock->acpi_device->handle;
332
333         if (block->instance_count <= instance)
334                 return AE_BAD_PARAMETER;
335
336         /* Check GUID is a data block */
337         if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
338                 return AE_ERROR;
339
340         input.count = 1;
341         input.pointer = wq_params;
342         wq_params[0].type = ACPI_TYPE_INTEGER;
343         wq_params[0].integer.value = instance;
344
345         if (instance == 0 && wblock->read_takes_no_args)
346                 input.count = 0;
347
348         /*
349          * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
350          * enable collection.
351          */
352         if (block->flags & ACPI_WMI_EXPENSIVE) {
353                 strncat(wc_method, block->object_id, 2);
354
355                 /*
356                  * Some GUIDs break the specification by declaring themselves
357                  * expensive, but have no corresponding WCxx method. So we
358                  * should not fail if this happens.
359                  */
360                 if (acpi_has_method(handle, wc_method))
361                         wc_status = acpi_execute_simple_method(handle,
362                                                                 wc_method, 1);
363         }
364
365         strcpy(method, "WQ");
366         strncat(method, block->object_id, 2);
367
368         status = acpi_evaluate_object(handle, method, &input, out);
369
370         /*
371          * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
372          * the WQxx method failed - we should disable collection anyway.
373          */
374         if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
375                 status = acpi_execute_simple_method(handle, wc_method, 0);
376         }
377
378         return status;
379 }
380
381 /**
382  * wmi_query_block - Return contents of a WMI block (deprecated)
383  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
384  * @instance: Instance index
385  * &out: Empty buffer to return the contents of the data block to
386  *
387  * Return the contents of an ACPI-WMI data block to a buffer
388  */
389 acpi_status wmi_query_block(const char *guid_string, u8 instance,
390                             struct acpi_buffer *out)
391 {
392         struct wmi_block *wblock;
393
394         if (!guid_string)
395                 return AE_BAD_PARAMETER;
396
397         if (!find_guid(guid_string, &wblock))
398                 return AE_ERROR;
399
400         return __query_block(wblock, instance, out);
401 }
402 EXPORT_SYMBOL_GPL(wmi_query_block);
403
404 union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance)
405 {
406         struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
407         struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
408
409         if (ACPI_FAILURE(__query_block(wblock, instance, &out)))
410                 return NULL;
411
412         return (union acpi_object *)out.pointer;
413 }
414 EXPORT_SYMBOL_GPL(wmidev_block_query);
415
416 /**
417  * wmi_set_block - Write to a WMI block
418  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
419  * @instance: Instance index
420  * &in: Buffer containing new values for the data block
421  *
422  * Write the contents of the input buffer to an ACPI-WMI data block
423  */
424 acpi_status wmi_set_block(const char *guid_string, u8 instance,
425                           const struct acpi_buffer *in)
426 {
427         struct guid_block *block = NULL;
428         struct wmi_block *wblock = NULL;
429         acpi_handle handle;
430         struct acpi_object_list input;
431         union acpi_object params[2];
432         char method[5] = "WS";
433
434         if (!guid_string || !in)
435                 return AE_BAD_DATA;
436
437         if (!find_guid(guid_string, &wblock))
438                 return AE_ERROR;
439
440         block = &wblock->gblock;
441         handle = wblock->acpi_device->handle;
442
443         if (block->instance_count <= instance)
444                 return AE_BAD_PARAMETER;
445
446         /* Check GUID is a data block */
447         if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
448                 return AE_ERROR;
449
450         input.count = 2;
451         input.pointer = params;
452         params[0].type = ACPI_TYPE_INTEGER;
453         params[0].integer.value = instance;
454
455         if (block->flags & ACPI_WMI_STRING) {
456                 params[1].type = ACPI_TYPE_STRING;
457         } else {
458                 params[1].type = ACPI_TYPE_BUFFER;
459         }
460         params[1].buffer.length = in->length;
461         params[1].buffer.pointer = in->pointer;
462
463         strncat(method, block->object_id, 2);
464
465         return acpi_evaluate_object(handle, method, &input, NULL);
466 }
467 EXPORT_SYMBOL_GPL(wmi_set_block);
468
469 static void wmi_dump_wdg(const struct guid_block *g)
470 {
471         pr_info("%pUL:\n", g->guid);
472         if (g->flags & ACPI_WMI_EVENT)
473                 pr_info("\tnotify_id: 0x%02X\n", g->notify_id);
474         else
475                 pr_info("\tobject_id: %2pE\n", g->object_id);
476         pr_info("\tinstance_count: %d\n", g->instance_count);
477         pr_info("\tflags: %#x", g->flags);
478         if (g->flags) {
479                 if (g->flags & ACPI_WMI_EXPENSIVE)
480                         pr_cont(" ACPI_WMI_EXPENSIVE");
481                 if (g->flags & ACPI_WMI_METHOD)
482                         pr_cont(" ACPI_WMI_METHOD");
483                 if (g->flags & ACPI_WMI_STRING)
484                         pr_cont(" ACPI_WMI_STRING");
485                 if (g->flags & ACPI_WMI_EVENT)
486                         pr_cont(" ACPI_WMI_EVENT");
487         }
488         pr_cont("\n");
489
490 }
491
492 static void wmi_notify_debug(u32 value, void *context)
493 {
494         struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
495         union acpi_object *obj;
496         acpi_status status;
497
498         status = wmi_get_event_data(value, &response);
499         if (status != AE_OK) {
500                 pr_info("bad event status 0x%x\n", status);
501                 return;
502         }
503
504         obj = (union acpi_object *)response.pointer;
505
506         if (!obj)
507                 return;
508
509         pr_info("DEBUG Event ");
510         switch(obj->type) {
511         case ACPI_TYPE_BUFFER:
512                 pr_cont("BUFFER_TYPE - length %d\n", obj->buffer.length);
513                 break;
514         case ACPI_TYPE_STRING:
515                 pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
516                 break;
517         case ACPI_TYPE_INTEGER:
518                 pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
519                 break;
520         case ACPI_TYPE_PACKAGE:
521                 pr_cont("PACKAGE_TYPE - %d elements\n", obj->package.count);
522                 break;
523         default:
524                 pr_cont("object type 0x%X\n", obj->type);
525         }
526         kfree(obj);
527 }
528
529 /**
530  * wmi_install_notify_handler - Register handler for WMI events
531  * @handler: Function to handle notifications
532  * @data: Data to be returned to handler when event is fired
533  *
534  * Register a handler for events sent to the ACPI-WMI mapper device.
535  */
536 acpi_status wmi_install_notify_handler(const char *guid,
537 wmi_notify_handler handler, void *data)
538 {
539         struct wmi_block *block;
540         acpi_status status = AE_NOT_EXIST;
541         uuid_le guid_input;
542
543         if (!guid || !handler)
544                 return AE_BAD_PARAMETER;
545
546         if (uuid_le_to_bin(guid, &guid_input))
547                 return AE_BAD_PARAMETER;
548
549         list_for_each_entry(block, &wmi_block_list, list) {
550                 acpi_status wmi_status;
551
552                 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
553                         if (block->handler &&
554                             block->handler != wmi_notify_debug)
555                                 return AE_ALREADY_ACQUIRED;
556
557                         block->handler = handler;
558                         block->handler_data = data;
559
560                         wmi_status = wmi_method_enable(block, 1);
561                         if ((wmi_status != AE_OK) ||
562                             ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
563                                 status = wmi_status;
564                 }
565         }
566
567         return status;
568 }
569 EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
570
571 /**
572  * wmi_uninstall_notify_handler - Unregister handler for WMI events
573  *
574  * Unregister handler for events sent to the ACPI-WMI mapper device.
575  */
576 acpi_status wmi_remove_notify_handler(const char *guid)
577 {
578         struct wmi_block *block;
579         acpi_status status = AE_NOT_EXIST;
580         uuid_le guid_input;
581
582         if (!guid)
583                 return AE_BAD_PARAMETER;
584
585         if (uuid_le_to_bin(guid, &guid_input))
586                 return AE_BAD_PARAMETER;
587
588         list_for_each_entry(block, &wmi_block_list, list) {
589                 acpi_status wmi_status;
590
591                 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
592                         if (!block->handler ||
593                             block->handler == wmi_notify_debug)
594                                 return AE_NULL_ENTRY;
595
596                         if (debug_event) {
597                                 block->handler = wmi_notify_debug;
598                                 status = AE_OK;
599                         } else {
600                                 wmi_status = wmi_method_enable(block, 0);
601                                 block->handler = NULL;
602                                 block->handler_data = NULL;
603                                 if ((wmi_status != AE_OK) ||
604                                     ((wmi_status == AE_OK) &&
605                                      (status == AE_NOT_EXIST)))
606                                         status = wmi_status;
607                         }
608                 }
609         }
610
611         return status;
612 }
613 EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
614
615 /**
616  * wmi_get_event_data - Get WMI data associated with an event
617  *
618  * @event: Event to find
619  * @out: Buffer to hold event data. out->pointer should be freed with kfree()
620  *
621  * Returns extra data associated with an event in WMI.
622  */
623 acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
624 {
625         struct acpi_object_list input;
626         union acpi_object params[1];
627         struct guid_block *gblock;
628         struct wmi_block *wblock;
629
630         input.count = 1;
631         input.pointer = params;
632         params[0].type = ACPI_TYPE_INTEGER;
633         params[0].integer.value = event;
634
635         list_for_each_entry(wblock, &wmi_block_list, list) {
636                 gblock = &wblock->gblock;
637
638                 if ((gblock->flags & ACPI_WMI_EVENT) &&
639                         (gblock->notify_id == event))
640                         return acpi_evaluate_object(wblock->acpi_device->handle,
641                                 "_WED", &input, out);
642         }
643
644         return AE_NOT_FOUND;
645 }
646 EXPORT_SYMBOL_GPL(wmi_get_event_data);
647
648 /**
649  * wmi_has_guid - Check if a GUID is available
650  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
651  *
652  * Check if a given GUID is defined by _WDG
653  */
654 bool wmi_has_guid(const char *guid_string)
655 {
656         return find_guid(guid_string, NULL);
657 }
658 EXPORT_SYMBOL_GPL(wmi_has_guid);
659
660 /**
661  * wmi_get_acpi_device_uid() - Get _UID name of ACPI device that defines GUID
662  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
663  *
664  * Find the _UID of ACPI device associated with this WMI GUID.
665  *
666  * Return: The ACPI _UID field value or NULL if the WMI GUID was not found
667  */
668 char *wmi_get_acpi_device_uid(const char *guid_string)
669 {
670         struct wmi_block *wblock = NULL;
671
672         if (!find_guid(guid_string, &wblock))
673                 return NULL;
674
675         return acpi_device_uid(wblock->acpi_device);
676 }
677 EXPORT_SYMBOL_GPL(wmi_get_acpi_device_uid);
678
679 static struct wmi_block *dev_to_wblock(struct device *dev)
680 {
681         return container_of(dev, struct wmi_block, dev.dev);
682 }
683
684 static struct wmi_device *dev_to_wdev(struct device *dev)
685 {
686         return container_of(dev, struct wmi_device, dev);
687 }
688
689 /*
690  * sysfs interface
691  */
692 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
693                              char *buf)
694 {
695         struct wmi_block *wblock = dev_to_wblock(dev);
696
697         return sprintf(buf, "wmi:%pUL\n", wblock->gblock.guid);
698 }
699 static DEVICE_ATTR_RO(modalias);
700
701 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
702                          char *buf)
703 {
704         struct wmi_block *wblock = dev_to_wblock(dev);
705
706         return sprintf(buf, "%pUL\n", wblock->gblock.guid);
707 }
708 static DEVICE_ATTR_RO(guid);
709
710 static ssize_t instance_count_show(struct device *dev,
711                                    struct device_attribute *attr, char *buf)
712 {
713         struct wmi_block *wblock = dev_to_wblock(dev);
714
715         return sprintf(buf, "%d\n", (int)wblock->gblock.instance_count);
716 }
717 static DEVICE_ATTR_RO(instance_count);
718
719 static ssize_t expensive_show(struct device *dev,
720                               struct device_attribute *attr, char *buf)
721 {
722         struct wmi_block *wblock = dev_to_wblock(dev);
723
724         return sprintf(buf, "%d\n",
725                        (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
726 }
727 static DEVICE_ATTR_RO(expensive);
728
729 static struct attribute *wmi_attrs[] = {
730         &dev_attr_modalias.attr,
731         &dev_attr_guid.attr,
732         &dev_attr_instance_count.attr,
733         &dev_attr_expensive.attr,
734         NULL,
735 };
736 ATTRIBUTE_GROUPS(wmi);
737
738 static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
739                               char *buf)
740 {
741         struct wmi_block *wblock = dev_to_wblock(dev);
742
743         return sprintf(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
744 }
745 static DEVICE_ATTR_RO(notify_id);
746
747 static struct attribute *wmi_event_attrs[] = {
748         &dev_attr_notify_id.attr,
749         NULL,
750 };
751 ATTRIBUTE_GROUPS(wmi_event);
752
753 static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
754                               char *buf)
755 {
756         struct wmi_block *wblock = dev_to_wblock(dev);
757
758         return sprintf(buf, "%c%c\n", wblock->gblock.object_id[0],
759                        wblock->gblock.object_id[1]);
760 }
761 static DEVICE_ATTR_RO(object_id);
762
763 static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
764                             char *buf)
765 {
766         struct wmi_device *wdev = dev_to_wdev(dev);
767
768         return sprintf(buf, "%d\n", (int)wdev->setable);
769 }
770 static DEVICE_ATTR_RO(setable);
771
772 static struct attribute *wmi_data_attrs[] = {
773         &dev_attr_object_id.attr,
774         &dev_attr_setable.attr,
775         NULL,
776 };
777 ATTRIBUTE_GROUPS(wmi_data);
778
779 static struct attribute *wmi_method_attrs[] = {
780         &dev_attr_object_id.attr,
781         NULL,
782 };
783 ATTRIBUTE_GROUPS(wmi_method);
784
785 static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
786 {
787         struct wmi_block *wblock = dev_to_wblock(dev);
788
789         if (add_uevent_var(env, "MODALIAS=wmi:%pUL", wblock->gblock.guid))
790                 return -ENOMEM;
791
792         if (add_uevent_var(env, "WMI_GUID=%pUL", wblock->gblock.guid))
793                 return -ENOMEM;
794
795         return 0;
796 }
797
798 static void wmi_dev_release(struct device *dev)
799 {
800         struct wmi_block *wblock = dev_to_wblock(dev);
801
802         kfree(wblock);
803 }
804
805 static int wmi_dev_match(struct device *dev, struct device_driver *driver)
806 {
807         struct wmi_driver *wmi_driver =
808                 container_of(driver, struct wmi_driver, driver);
809         struct wmi_block *wblock = dev_to_wblock(dev);
810         const struct wmi_device_id *id = wmi_driver->id_table;
811
812         if (id == NULL)
813                 return 0;
814
815         while (*id->guid_string) {
816                 uuid_le driver_guid;
817
818                 if (WARN_ON(uuid_le_to_bin(id->guid_string, &driver_guid)))
819                         continue;
820                 if (!memcmp(&driver_guid, wblock->gblock.guid, 16))
821                         return 1;
822
823                 id++;
824         }
825
826         return 0;
827 }
828 static int wmi_char_open(struct inode *inode, struct file *filp)
829 {
830         const char *driver_name = filp->f_path.dentry->d_iname;
831         struct wmi_block *wblock = NULL;
832         struct wmi_block *next = NULL;
833
834         list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
835                 if (!wblock->dev.dev.driver)
836                         continue;
837                 if (strcmp(driver_name, wblock->dev.dev.driver->name) == 0) {
838                         filp->private_data = wblock;
839                         break;
840                 }
841         }
842
843         if (!filp->private_data)
844                 return -ENODEV;
845
846         return nonseekable_open(inode, filp);
847 }
848
849 static ssize_t wmi_char_read(struct file *filp, char __user *buffer,
850         size_t length, loff_t *offset)
851 {
852         struct wmi_block *wblock = filp->private_data;
853
854         return simple_read_from_buffer(buffer, length, offset,
855                                        &wblock->req_buf_size,
856                                        sizeof(wblock->req_buf_size));
857 }
858
859 static long wmi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
860 {
861         struct wmi_ioctl_buffer __user *input =
862                 (struct wmi_ioctl_buffer __user *) arg;
863         struct wmi_block *wblock = filp->private_data;
864         struct wmi_ioctl_buffer *buf = NULL;
865         struct wmi_driver *wdriver = NULL;
866         int ret;
867
868         if (_IOC_TYPE(cmd) != WMI_IOC)
869                 return -ENOTTY;
870
871         /* make sure we're not calling a higher instance than exists*/
872         if (_IOC_NR(cmd) >= wblock->gblock.instance_count)
873                 return -EINVAL;
874
875         mutex_lock(&wblock->char_mutex);
876         buf = wblock->handler_data;
877         if (get_user(buf->length, &input->length)) {
878                 dev_dbg(&wblock->dev.dev, "Read length from user failed\n");
879                 ret = -EFAULT;
880                 goto out_ioctl;
881         }
882         /* if it's too small, abort */
883         if (buf->length < wblock->req_buf_size) {
884                 dev_err(&wblock->dev.dev,
885                         "Buffer %lld too small, need at least %lld\n",
886                         buf->length, wblock->req_buf_size);
887                 ret = -EINVAL;
888                 goto out_ioctl;
889         }
890         /* if it's too big, warn, driver will only use what is needed */
891         if (buf->length > wblock->req_buf_size)
892                 dev_warn(&wblock->dev.dev,
893                         "Buffer %lld is bigger than required %lld\n",
894                         buf->length, wblock->req_buf_size);
895
896         /* copy the structure from userspace */
897         if (copy_from_user(buf, input, wblock->req_buf_size)) {
898                 dev_dbg(&wblock->dev.dev, "Copy %llu from user failed\n",
899                         wblock->req_buf_size);
900                 ret = -EFAULT;
901                 goto out_ioctl;
902         }
903
904         /* let the driver do any filtering and do the call */
905         wdriver = container_of(wblock->dev.dev.driver,
906                                struct wmi_driver, driver);
907         if (!try_module_get(wdriver->driver.owner)) {
908                 ret = -EBUSY;
909                 goto out_ioctl;
910         }
911         ret = wdriver->filter_callback(&wblock->dev, cmd, buf);
912         module_put(wdriver->driver.owner);
913         if (ret)
914                 goto out_ioctl;
915
916         /* return the result (only up to our internal buffer size) */
917         if (copy_to_user(input, buf, wblock->req_buf_size)) {
918                 dev_dbg(&wblock->dev.dev, "Copy %llu to user failed\n",
919                         wblock->req_buf_size);
920                 ret = -EFAULT;
921         }
922
923 out_ioctl:
924         mutex_unlock(&wblock->char_mutex);
925         return ret;
926 }
927
928 static const struct file_operations wmi_fops = {
929         .owner          = THIS_MODULE,
930         .read           = wmi_char_read,
931         .open           = wmi_char_open,
932         .unlocked_ioctl = wmi_ioctl,
933         .compat_ioctl   = wmi_ioctl,
934 };
935
936 static int wmi_dev_probe(struct device *dev)
937 {
938         struct wmi_block *wblock = dev_to_wblock(dev);
939         struct wmi_driver *wdriver =
940                 container_of(dev->driver, struct wmi_driver, driver);
941         int ret = 0;
942         char *buf;
943
944         if (ACPI_FAILURE(wmi_method_enable(wblock, 1)))
945                 dev_warn(dev, "failed to enable device -- probing anyway\n");
946
947         if (wdriver->probe) {
948                 ret = wdriver->probe(dev_to_wdev(dev),
949                                 find_guid_context(wblock, wdriver));
950                 if (ret != 0)
951                         goto probe_failure;
952         }
953
954         /* driver wants a character device made */
955         if (wdriver->filter_callback) {
956                 /* check that required buffer size declared by driver or MOF */
957                 if (!wblock->req_buf_size) {
958                         dev_err(&wblock->dev.dev,
959                                 "Required buffer size not set\n");
960                         ret = -EINVAL;
961                         goto probe_failure;
962                 }
963
964                 wblock->handler_data = kmalloc(wblock->req_buf_size,
965                                                GFP_KERNEL);
966                 if (!wblock->handler_data) {
967                         ret = -ENOMEM;
968                         goto probe_failure;
969                 }
970
971                 buf = kasprintf(GFP_KERNEL, "wmi/%s", wdriver->driver.name);
972                 if (!buf) {
973                         ret = -ENOMEM;
974                         goto probe_string_failure;
975                 }
976                 wblock->char_dev.minor = MISC_DYNAMIC_MINOR;
977                 wblock->char_dev.name = buf;
978                 wblock->char_dev.fops = &wmi_fops;
979                 wblock->char_dev.mode = 0444;
980                 ret = misc_register(&wblock->char_dev);
981                 if (ret) {
982                         dev_warn(dev, "failed to register char dev: %d\n", ret);
983                         ret = -ENOMEM;
984                         goto probe_misc_failure;
985                 }
986         }
987
988         return 0;
989
990 probe_misc_failure:
991         kfree(buf);
992 probe_string_failure:
993         kfree(wblock->handler_data);
994 probe_failure:
995         if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
996                 dev_warn(dev, "failed to disable device\n");
997         return ret;
998 }
999
1000 static int wmi_dev_remove(struct device *dev)
1001 {
1002         struct wmi_block *wblock = dev_to_wblock(dev);
1003         struct wmi_driver *wdriver =
1004                 container_of(dev->driver, struct wmi_driver, driver);
1005         int ret = 0;
1006
1007         if (wdriver->filter_callback) {
1008                 misc_deregister(&wblock->char_dev);
1009                 kfree(wblock->char_dev.name);
1010                 kfree(wblock->handler_data);
1011         }
1012
1013         if (wdriver->remove)
1014                 ret = wdriver->remove(dev_to_wdev(dev));
1015
1016         if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
1017                 dev_warn(dev, "failed to disable device\n");
1018
1019         return ret;
1020 }
1021
1022 static struct class wmi_bus_class = {
1023         .name = "wmi_bus",
1024 };
1025
1026 static struct bus_type wmi_bus_type = {
1027         .name = "wmi",
1028         .dev_groups = wmi_groups,
1029         .match = wmi_dev_match,
1030         .uevent = wmi_dev_uevent,
1031         .probe = wmi_dev_probe,
1032         .remove = wmi_dev_remove,
1033 };
1034
1035 static const struct device_type wmi_type_event = {
1036         .name = "event",
1037         .groups = wmi_event_groups,
1038         .release = wmi_dev_release,
1039 };
1040
1041 static const struct device_type wmi_type_method = {
1042         .name = "method",
1043         .groups = wmi_method_groups,
1044         .release = wmi_dev_release,
1045 };
1046
1047 static const struct device_type wmi_type_data = {
1048         .name = "data",
1049         .groups = wmi_data_groups,
1050         .release = wmi_dev_release,
1051 };
1052
1053 static int wmi_create_device(struct device *wmi_bus_dev,
1054                              const struct guid_block *gblock,
1055                              struct wmi_block *wblock,
1056                              struct acpi_device *device)
1057 {
1058         struct acpi_device_info *info;
1059         char method[5];
1060         int result;
1061
1062         if (gblock->flags & ACPI_WMI_EVENT) {
1063                 wblock->dev.dev.type = &wmi_type_event;
1064                 goto out_init;
1065         }
1066
1067         if (gblock->flags & ACPI_WMI_METHOD) {
1068                 wblock->dev.dev.type = &wmi_type_method;
1069                 mutex_init(&wblock->char_mutex);
1070                 goto out_init;
1071         }
1072
1073         /*
1074          * Data Block Query Control Method (WQxx by convention) is
1075          * required per the WMI documentation. If it is not present,
1076          * we ignore this data block.
1077          */
1078         strcpy(method, "WQ");
1079         strncat(method, wblock->gblock.object_id, 2);
1080         result = get_subobj_info(device->handle, method, &info);
1081
1082         if (result) {
1083                 dev_warn(wmi_bus_dev,
1084                          "%s data block query control method not found\n",
1085                          method);
1086                 return result;
1087         }
1088
1089         wblock->dev.dev.type = &wmi_type_data;
1090
1091         /*
1092          * The Microsoft documentation specifically states:
1093          *
1094          *   Data blocks registered with only a single instance
1095          *   can ignore the parameter.
1096          *
1097          * ACPICA will get mad at us if we call the method with the wrong number
1098          * of arguments, so check what our method expects.  (On some Dell
1099          * laptops, WQxx may not be a method at all.)
1100          */
1101         if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
1102                 wblock->read_takes_no_args = true;
1103
1104         kfree(info);
1105
1106         strcpy(method, "WS");
1107         strncat(method, wblock->gblock.object_id, 2);
1108         result = get_subobj_info(device->handle, method, NULL);
1109
1110         if (result == 0)
1111                 wblock->dev.setable = true;
1112
1113  out_init:
1114         wblock->dev.dev.bus = &wmi_bus_type;
1115         wblock->dev.dev.parent = wmi_bus_dev;
1116
1117         dev_set_name(&wblock->dev.dev, "%pUL", gblock->guid);
1118
1119         device_initialize(&wblock->dev.dev);
1120
1121         return 0;
1122 }
1123
1124 static void wmi_free_devices(struct acpi_device *device)
1125 {
1126         struct wmi_block *wblock, *next;
1127
1128         /* Delete devices for all the GUIDs */
1129         list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1130                 if (wblock->acpi_device == device) {
1131                         list_del(&wblock->list);
1132                         device_unregister(&wblock->dev.dev);
1133                 }
1134         }
1135 }
1136
1137 static bool guid_already_parsed(struct acpi_device *device,
1138                                 const u8 *guid)
1139 {
1140         struct wmi_block *wblock;
1141
1142         list_for_each_entry(wblock, &wmi_block_list, list) {
1143                 if (memcmp(wblock->gblock.guid, guid, 16) == 0) {
1144                         /*
1145                          * Because we historically didn't track the relationship
1146                          * between GUIDs and ACPI nodes, we don't know whether
1147                          * we need to suppress GUIDs that are unique on a
1148                          * given node but duplicated across nodes.
1149                          */
1150                         dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
1151                                  guid, dev_name(&wblock->acpi_device->dev));
1152                         return true;
1153                 }
1154         }
1155
1156         return false;
1157 }
1158
1159 /*
1160  * Parse the _WDG method for the GUID data blocks
1161  */
1162 static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
1163 {
1164         struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
1165         const struct guid_block *gblock;
1166         struct wmi_block *wblock, *next;
1167         union acpi_object *obj;
1168         acpi_status status;
1169         int retval = 0;
1170         u32 i, total;
1171
1172         status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
1173         if (ACPI_FAILURE(status))
1174                 return -ENXIO;
1175
1176         obj = (union acpi_object *) out.pointer;
1177         if (!obj)
1178                 return -ENXIO;
1179
1180         if (obj->type != ACPI_TYPE_BUFFER) {
1181                 retval = -ENXIO;
1182                 goto out_free_pointer;
1183         }
1184
1185         gblock = (const struct guid_block *)obj->buffer.pointer;
1186         total = obj->buffer.length / sizeof(struct guid_block);
1187
1188         for (i = 0; i < total; i++) {
1189                 if (debug_dump_wdg)
1190                         wmi_dump_wdg(&gblock[i]);
1191
1192                 /*
1193                  * Some WMI devices, like those for nVidia hooks, have a
1194                  * duplicate GUID. It's not clear what we should do in this
1195                  * case yet, so for now, we'll just ignore the duplicate
1196                  * for device creation.
1197                  */
1198                 if (guid_already_parsed(device, gblock[i].guid))
1199                         continue;
1200
1201                 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
1202                 if (!wblock) {
1203                         retval = -ENOMEM;
1204                         break;
1205                 }
1206
1207                 wblock->acpi_device = device;
1208                 wblock->gblock = gblock[i];
1209
1210                 retval = wmi_create_device(wmi_bus_dev, &gblock[i], wblock, device);
1211                 if (retval) {
1212                         kfree(wblock);
1213                         continue;
1214                 }
1215
1216                 list_add_tail(&wblock->list, &wmi_block_list);
1217
1218                 if (debug_event) {
1219                         wblock->handler = wmi_notify_debug;
1220                         wmi_method_enable(wblock, 1);
1221                 }
1222         }
1223
1224         /*
1225          * Now that all of the devices are created, add them to the
1226          * device tree and probe subdrivers.
1227          */
1228         list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1229                 if (wblock->acpi_device != device)
1230                         continue;
1231
1232                 retval = device_add(&wblock->dev.dev);
1233                 if (retval) {
1234                         dev_err(wmi_bus_dev, "failed to register %pUL\n",
1235                                 wblock->gblock.guid);
1236                         if (debug_event)
1237                                 wmi_method_enable(wblock, 0);
1238                         list_del(&wblock->list);
1239                         put_device(&wblock->dev.dev);
1240                 }
1241         }
1242
1243 out_free_pointer:
1244         kfree(out.pointer);
1245         return retval;
1246 }
1247
1248 /*
1249  * WMI can have EmbeddedControl access regions. In which case, we just want to
1250  * hand these off to the EC driver.
1251  */
1252 static acpi_status
1253 acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
1254                       u32 bits, u64 *value,
1255                       void *handler_context, void *region_context)
1256 {
1257         int result = 0, i = 0;
1258         u8 temp = 0;
1259
1260         if ((address > 0xFF) || !value)
1261                 return AE_BAD_PARAMETER;
1262
1263         if (function != ACPI_READ && function != ACPI_WRITE)
1264                 return AE_BAD_PARAMETER;
1265
1266         if (bits != 8)
1267                 return AE_BAD_PARAMETER;
1268
1269         if (function == ACPI_READ) {
1270                 result = ec_read(address, &temp);
1271                 (*value) |= ((u64)temp) << i;
1272         } else {
1273                 temp = 0xff & ((*value) >> i);
1274                 result = ec_write(address, temp);
1275         }
1276
1277         switch (result) {
1278         case -EINVAL:
1279                 return AE_BAD_PARAMETER;
1280                 break;
1281         case -ENODEV:
1282                 return AE_NOT_FOUND;
1283                 break;
1284         case -ETIME:
1285                 return AE_TIME;
1286                 break;
1287         default:
1288                 return AE_OK;
1289         }
1290 }
1291
1292 static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
1293                                     void *context)
1294 {
1295         struct guid_block *block;
1296         struct wmi_block *wblock;
1297         bool found_it = false;
1298
1299         list_for_each_entry(wblock, &wmi_block_list, list) {
1300                 block = &wblock->gblock;
1301
1302                 if (wblock->acpi_device->handle == handle &&
1303                     (block->flags & ACPI_WMI_EVENT) &&
1304                     (block->notify_id == event))
1305                 {
1306                         found_it = true;
1307                         break;
1308                 }
1309         }
1310
1311         if (!found_it)
1312                 return;
1313
1314         /* If a driver is bound, then notify the driver. */
1315         if (wblock->dev.dev.driver) {
1316                 struct wmi_driver *driver;
1317                 struct acpi_object_list input;
1318                 union acpi_object params[1];
1319                 struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
1320                 acpi_status status;
1321
1322                 driver = container_of(wblock->dev.dev.driver,
1323                                       struct wmi_driver, driver);
1324
1325                 input.count = 1;
1326                 input.pointer = params;
1327                 params[0].type = ACPI_TYPE_INTEGER;
1328                 params[0].integer.value = event;
1329
1330                 status = acpi_evaluate_object(wblock->acpi_device->handle,
1331                                               "_WED", &input, &evdata);
1332                 if (ACPI_FAILURE(status)) {
1333                         dev_warn(&wblock->dev.dev,
1334                                  "failed to get event data\n");
1335                         return;
1336                 }
1337
1338                 if (driver->notify)
1339                         driver->notify(&wblock->dev,
1340                                        (union acpi_object *)evdata.pointer);
1341
1342                 kfree(evdata.pointer);
1343         } else if (wblock->handler) {
1344                 /* Legacy handler */
1345                 wblock->handler(event, wblock->handler_data);
1346         }
1347
1348         if (debug_event) {
1349                 pr_info("DEBUG Event GUID: %pUL\n",
1350                         wblock->gblock.guid);
1351         }
1352
1353         acpi_bus_generate_netlink_event(
1354                 wblock->acpi_device->pnp.device_class,
1355                 dev_name(&wblock->dev.dev),
1356                 event, 0);
1357
1358 }
1359
1360 static int acpi_wmi_remove(struct platform_device *device)
1361 {
1362         struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
1363
1364         acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
1365                                    acpi_wmi_notify_handler);
1366         acpi_remove_address_space_handler(acpi_device->handle,
1367                                 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
1368         wmi_free_devices(acpi_device);
1369         device_destroy(&wmi_bus_class, MKDEV(0, 0));
1370
1371         return 0;
1372 }
1373
1374 static int acpi_wmi_probe(struct platform_device *device)
1375 {
1376         struct acpi_device *acpi_device;
1377         struct device *wmi_bus_dev;
1378         acpi_status status;
1379         int error;
1380
1381         acpi_device = ACPI_COMPANION(&device->dev);
1382         if (!acpi_device) {
1383                 dev_err(&device->dev, "ACPI companion is missing\n");
1384                 return -ENODEV;
1385         }
1386
1387         status = acpi_install_address_space_handler(acpi_device->handle,
1388                                                     ACPI_ADR_SPACE_EC,
1389                                                     &acpi_wmi_ec_space_handler,
1390                                                     NULL, NULL);
1391         if (ACPI_FAILURE(status)) {
1392                 dev_err(&device->dev, "Error installing EC region handler\n");
1393                 return -ENODEV;
1394         }
1395
1396         status = acpi_install_notify_handler(acpi_device->handle,
1397                                              ACPI_DEVICE_NOTIFY,
1398                                              acpi_wmi_notify_handler,
1399                                              NULL);
1400         if (ACPI_FAILURE(status)) {
1401                 dev_err(&device->dev, "Error installing notify handler\n");
1402                 error = -ENODEV;
1403                 goto err_remove_ec_handler;
1404         }
1405
1406         wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
1407                                     NULL, "wmi_bus-%s", dev_name(&device->dev));
1408         if (IS_ERR(wmi_bus_dev)) {
1409                 error = PTR_ERR(wmi_bus_dev);
1410                 goto err_remove_notify_handler;
1411         }
1412         dev_set_drvdata(&device->dev, wmi_bus_dev);
1413
1414         error = parse_wdg(wmi_bus_dev, acpi_device);
1415         if (error) {
1416                 pr_err("Failed to parse WDG method\n");
1417                 goto err_remove_busdev;
1418         }
1419
1420         return 0;
1421
1422 err_remove_busdev:
1423         device_destroy(&wmi_bus_class, MKDEV(0, 0));
1424
1425 err_remove_notify_handler:
1426         acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
1427                                    acpi_wmi_notify_handler);
1428
1429 err_remove_ec_handler:
1430         acpi_remove_address_space_handler(acpi_device->handle,
1431                                           ACPI_ADR_SPACE_EC,
1432                                           &acpi_wmi_ec_space_handler);
1433
1434         return error;
1435 }
1436
1437 int __must_check __wmi_driver_register(struct wmi_driver *driver,
1438                                        struct module *owner)
1439 {
1440         driver->driver.owner = owner;
1441         driver->driver.bus = &wmi_bus_type;
1442
1443         return driver_register(&driver->driver);
1444 }
1445 EXPORT_SYMBOL(__wmi_driver_register);
1446
1447 void wmi_driver_unregister(struct wmi_driver *driver)
1448 {
1449         driver_unregister(&driver->driver);
1450 }
1451 EXPORT_SYMBOL(wmi_driver_unregister);
1452
1453 static int __init acpi_wmi_init(void)
1454 {
1455         int error;
1456
1457         if (acpi_disabled)
1458                 return -ENODEV;
1459
1460         error = class_register(&wmi_bus_class);
1461         if (error)
1462                 return error;
1463
1464         error = bus_register(&wmi_bus_type);
1465         if (error)
1466                 goto err_unreg_class;
1467
1468         error = platform_driver_register(&acpi_wmi_driver);
1469         if (error) {
1470                 pr_err("Error loading mapper\n");
1471                 goto err_unreg_bus;
1472         }
1473
1474         return 0;
1475
1476 err_unreg_bus:
1477         bus_unregister(&wmi_bus_type);
1478
1479 err_unreg_class:
1480         class_unregister(&wmi_bus_class);
1481
1482         return error;
1483 }
1484
1485 static void __exit acpi_wmi_exit(void)
1486 {
1487         platform_driver_unregister(&acpi_wmi_driver);
1488         bus_unregister(&wmi_bus_type);
1489         class_unregister(&wmi_bus_class);
1490 }
1491
1492 subsys_initcall_sync(acpi_wmi_init);
1493 module_exit(acpi_wmi_exit);