]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/platform/x86/wmi.c
f3be1c008856f40e84abd571bb150aa964794896
[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                 if (ret != 0)
950                         goto probe_failure;
951         }
952
953         /* driver wants a character device made */
954         if (wdriver->filter_callback) {
955                 /* check that required buffer size declared by driver or MOF */
956                 if (!wblock->req_buf_size) {
957                         dev_err(&wblock->dev.dev,
958                                 "Required buffer size not set\n");
959                         ret = -EINVAL;
960                         goto probe_failure;
961                 }
962
963                 wblock->handler_data = kmalloc(wblock->req_buf_size,
964                                                GFP_KERNEL);
965                 if (!wblock->handler_data) {
966                         ret = -ENOMEM;
967                         goto probe_failure;
968                 }
969
970                 buf = kasprintf(GFP_KERNEL, "wmi/%s", wdriver->driver.name);
971                 if (!buf) {
972                         ret = -ENOMEM;
973                         goto probe_string_failure;
974                 }
975                 wblock->char_dev.minor = MISC_DYNAMIC_MINOR;
976                 wblock->char_dev.name = buf;
977                 wblock->char_dev.fops = &wmi_fops;
978                 wblock->char_dev.mode = 0444;
979                 ret = misc_register(&wblock->char_dev);
980                 if (ret) {
981                         dev_warn(dev, "failed to register char dev: %d\n", ret);
982                         ret = -ENOMEM;
983                         goto probe_misc_failure;
984                 }
985         }
986
987         return 0;
988
989 probe_misc_failure:
990         kfree(buf);
991 probe_string_failure:
992         kfree(wblock->handler_data);
993 probe_failure:
994         if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
995                 dev_warn(dev, "failed to disable device\n");
996         return ret;
997 }
998
999 static int wmi_dev_remove(struct device *dev)
1000 {
1001         struct wmi_block *wblock = dev_to_wblock(dev);
1002         struct wmi_driver *wdriver =
1003                 container_of(dev->driver, struct wmi_driver, driver);
1004         int ret = 0;
1005
1006         if (wdriver->filter_callback) {
1007                 misc_deregister(&wblock->char_dev);
1008                 kfree(wblock->char_dev.name);
1009                 kfree(wblock->handler_data);
1010         }
1011
1012         if (wdriver->remove)
1013                 ret = wdriver->remove(dev_to_wdev(dev));
1014
1015         if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
1016                 dev_warn(dev, "failed to disable device\n");
1017
1018         return ret;
1019 }
1020
1021 static struct class wmi_bus_class = {
1022         .name = "wmi_bus",
1023 };
1024
1025 static struct bus_type wmi_bus_type = {
1026         .name = "wmi",
1027         .dev_groups = wmi_groups,
1028         .match = wmi_dev_match,
1029         .uevent = wmi_dev_uevent,
1030         .probe = wmi_dev_probe,
1031         .remove = wmi_dev_remove,
1032 };
1033
1034 static const struct device_type wmi_type_event = {
1035         .name = "event",
1036         .groups = wmi_event_groups,
1037         .release = wmi_dev_release,
1038 };
1039
1040 static const struct device_type wmi_type_method = {
1041         .name = "method",
1042         .groups = wmi_method_groups,
1043         .release = wmi_dev_release,
1044 };
1045
1046 static const struct device_type wmi_type_data = {
1047         .name = "data",
1048         .groups = wmi_data_groups,
1049         .release = wmi_dev_release,
1050 };
1051
1052 static int wmi_create_device(struct device *wmi_bus_dev,
1053                              const struct guid_block *gblock,
1054                              struct wmi_block *wblock,
1055                              struct acpi_device *device)
1056 {
1057         struct acpi_device_info *info;
1058         char method[5];
1059         int result;
1060
1061         if (gblock->flags & ACPI_WMI_EVENT) {
1062                 wblock->dev.dev.type = &wmi_type_event;
1063                 goto out_init;
1064         }
1065
1066         if (gblock->flags & ACPI_WMI_METHOD) {
1067                 wblock->dev.dev.type = &wmi_type_method;
1068                 mutex_init(&wblock->char_mutex);
1069                 goto out_init;
1070         }
1071
1072         /*
1073          * Data Block Query Control Method (WQxx by convention) is
1074          * required per the WMI documentation. If it is not present,
1075          * we ignore this data block.
1076          */
1077         strcpy(method, "WQ");
1078         strncat(method, wblock->gblock.object_id, 2);
1079         result = get_subobj_info(device->handle, method, &info);
1080
1081         if (result) {
1082                 dev_warn(wmi_bus_dev,
1083                          "%s data block query control method not found\n",
1084                          method);
1085                 return result;
1086         }
1087
1088         wblock->dev.dev.type = &wmi_type_data;
1089
1090         /*
1091          * The Microsoft documentation specifically states:
1092          *
1093          *   Data blocks registered with only a single instance
1094          *   can ignore the parameter.
1095          *
1096          * ACPICA will get mad at us if we call the method with the wrong number
1097          * of arguments, so check what our method expects.  (On some Dell
1098          * laptops, WQxx may not be a method at all.)
1099          */
1100         if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
1101                 wblock->read_takes_no_args = true;
1102
1103         kfree(info);
1104
1105         strcpy(method, "WS");
1106         strncat(method, wblock->gblock.object_id, 2);
1107         result = get_subobj_info(device->handle, method, NULL);
1108
1109         if (result == 0)
1110                 wblock->dev.setable = true;
1111
1112  out_init:
1113         wblock->dev.dev.bus = &wmi_bus_type;
1114         wblock->dev.dev.parent = wmi_bus_dev;
1115
1116         dev_set_name(&wblock->dev.dev, "%pUL", gblock->guid);
1117
1118         device_initialize(&wblock->dev.dev);
1119
1120         return 0;
1121 }
1122
1123 static void wmi_free_devices(struct acpi_device *device)
1124 {
1125         struct wmi_block *wblock, *next;
1126
1127         /* Delete devices for all the GUIDs */
1128         list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1129                 if (wblock->acpi_device == device) {
1130                         list_del(&wblock->list);
1131                         device_unregister(&wblock->dev.dev);
1132                 }
1133         }
1134 }
1135
1136 static bool guid_already_parsed(struct acpi_device *device,
1137                                 const u8 *guid)
1138 {
1139         struct wmi_block *wblock;
1140
1141         list_for_each_entry(wblock, &wmi_block_list, list) {
1142                 if (memcmp(wblock->gblock.guid, guid, 16) == 0) {
1143                         /*
1144                          * Because we historically didn't track the relationship
1145                          * between GUIDs and ACPI nodes, we don't know whether
1146                          * we need to suppress GUIDs that are unique on a
1147                          * given node but duplicated across nodes.
1148                          */
1149                         dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
1150                                  guid, dev_name(&wblock->acpi_device->dev));
1151                         return true;
1152                 }
1153         }
1154
1155         return false;
1156 }
1157
1158 /*
1159  * Parse the _WDG method for the GUID data blocks
1160  */
1161 static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
1162 {
1163         struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
1164         const struct guid_block *gblock;
1165         struct wmi_block *wblock, *next;
1166         union acpi_object *obj;
1167         acpi_status status;
1168         int retval = 0;
1169         u32 i, total;
1170
1171         status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
1172         if (ACPI_FAILURE(status))
1173                 return -ENXIO;
1174
1175         obj = (union acpi_object *) out.pointer;
1176         if (!obj)
1177                 return -ENXIO;
1178
1179         if (obj->type != ACPI_TYPE_BUFFER) {
1180                 retval = -ENXIO;
1181                 goto out_free_pointer;
1182         }
1183
1184         gblock = (const struct guid_block *)obj->buffer.pointer;
1185         total = obj->buffer.length / sizeof(struct guid_block);
1186
1187         for (i = 0; i < total; i++) {
1188                 if (debug_dump_wdg)
1189                         wmi_dump_wdg(&gblock[i]);
1190
1191                 /*
1192                  * Some WMI devices, like those for nVidia hooks, have a
1193                  * duplicate GUID. It's not clear what we should do in this
1194                  * case yet, so for now, we'll just ignore the duplicate
1195                  * for device creation.
1196                  */
1197                 if (guid_already_parsed(device, gblock[i].guid))
1198                         continue;
1199
1200                 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
1201                 if (!wblock) {
1202                         retval = -ENOMEM;
1203                         break;
1204                 }
1205
1206                 wblock->acpi_device = device;
1207                 wblock->gblock = gblock[i];
1208
1209                 retval = wmi_create_device(wmi_bus_dev, &gblock[i], wblock, device);
1210                 if (retval) {
1211                         kfree(wblock);
1212                         continue;
1213                 }
1214
1215                 list_add_tail(&wblock->list, &wmi_block_list);
1216
1217                 if (debug_event) {
1218                         wblock->handler = wmi_notify_debug;
1219                         wmi_method_enable(wblock, 1);
1220                 }
1221         }
1222
1223         /*
1224          * Now that all of the devices are created, add them to the
1225          * device tree and probe subdrivers.
1226          */
1227         list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1228                 if (wblock->acpi_device != device)
1229                         continue;
1230
1231                 retval = device_add(&wblock->dev.dev);
1232                 if (retval) {
1233                         dev_err(wmi_bus_dev, "failed to register %pUL\n",
1234                                 wblock->gblock.guid);
1235                         if (debug_event)
1236                                 wmi_method_enable(wblock, 0);
1237                         list_del(&wblock->list);
1238                         put_device(&wblock->dev.dev);
1239                 }
1240         }
1241
1242 out_free_pointer:
1243         kfree(out.pointer);
1244         return retval;
1245 }
1246
1247 /*
1248  * WMI can have EmbeddedControl access regions. In which case, we just want to
1249  * hand these off to the EC driver.
1250  */
1251 static acpi_status
1252 acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
1253                       u32 bits, u64 *value,
1254                       void *handler_context, void *region_context)
1255 {
1256         int result = 0, i = 0;
1257         u8 temp = 0;
1258
1259         if ((address > 0xFF) || !value)
1260                 return AE_BAD_PARAMETER;
1261
1262         if (function != ACPI_READ && function != ACPI_WRITE)
1263                 return AE_BAD_PARAMETER;
1264
1265         if (bits != 8)
1266                 return AE_BAD_PARAMETER;
1267
1268         if (function == ACPI_READ) {
1269                 result = ec_read(address, &temp);
1270                 (*value) |= ((u64)temp) << i;
1271         } else {
1272                 temp = 0xff & ((*value) >> i);
1273                 result = ec_write(address, temp);
1274         }
1275
1276         switch (result) {
1277         case -EINVAL:
1278                 return AE_BAD_PARAMETER;
1279                 break;
1280         case -ENODEV:
1281                 return AE_NOT_FOUND;
1282                 break;
1283         case -ETIME:
1284                 return AE_TIME;
1285                 break;
1286         default:
1287                 return AE_OK;
1288         }
1289 }
1290
1291 static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
1292                                     void *context)
1293 {
1294         struct guid_block *block;
1295         struct wmi_block *wblock;
1296         bool found_it = false;
1297
1298         list_for_each_entry(wblock, &wmi_block_list, list) {
1299                 block = &wblock->gblock;
1300
1301                 if (wblock->acpi_device->handle == handle &&
1302                     (block->flags & ACPI_WMI_EVENT) &&
1303                     (block->notify_id == event))
1304                 {
1305                         found_it = true;
1306                         break;
1307                 }
1308         }
1309
1310         if (!found_it)
1311                 return;
1312
1313         /* If a driver is bound, then notify the driver. */
1314         if (wblock->dev.dev.driver) {
1315                 struct wmi_driver *driver;
1316                 struct acpi_object_list input;
1317                 union acpi_object params[1];
1318                 struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
1319                 acpi_status status;
1320
1321                 driver = container_of(wblock->dev.dev.driver,
1322                                       struct wmi_driver, driver);
1323
1324                 input.count = 1;
1325                 input.pointer = params;
1326                 params[0].type = ACPI_TYPE_INTEGER;
1327                 params[0].integer.value = event;
1328
1329                 status = acpi_evaluate_object(wblock->acpi_device->handle,
1330                                               "_WED", &input, &evdata);
1331                 if (ACPI_FAILURE(status)) {
1332                         dev_warn(&wblock->dev.dev,
1333                                  "failed to get event data\n");
1334                         return;
1335                 }
1336
1337                 if (driver->notify)
1338                         driver->notify(&wblock->dev,
1339                                        (union acpi_object *)evdata.pointer);
1340
1341                 kfree(evdata.pointer);
1342         } else if (wblock->handler) {
1343                 /* Legacy handler */
1344                 wblock->handler(event, wblock->handler_data);
1345         }
1346
1347         if (debug_event) {
1348                 pr_info("DEBUG Event GUID: %pUL\n",
1349                         wblock->gblock.guid);
1350         }
1351
1352         acpi_bus_generate_netlink_event(
1353                 wblock->acpi_device->pnp.device_class,
1354                 dev_name(&wblock->dev.dev),
1355                 event, 0);
1356
1357 }
1358
1359 static int acpi_wmi_remove(struct platform_device *device)
1360 {
1361         struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
1362
1363         acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
1364                                    acpi_wmi_notify_handler);
1365         acpi_remove_address_space_handler(acpi_device->handle,
1366                                 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
1367         wmi_free_devices(acpi_device);
1368         device_destroy(&wmi_bus_class, MKDEV(0, 0));
1369
1370         return 0;
1371 }
1372
1373 static int acpi_wmi_probe(struct platform_device *device)
1374 {
1375         struct acpi_device *acpi_device;
1376         struct device *wmi_bus_dev;
1377         acpi_status status;
1378         int error;
1379
1380         acpi_device = ACPI_COMPANION(&device->dev);
1381         if (!acpi_device) {
1382                 dev_err(&device->dev, "ACPI companion is missing\n");
1383                 return -ENODEV;
1384         }
1385
1386         status = acpi_install_address_space_handler(acpi_device->handle,
1387                                                     ACPI_ADR_SPACE_EC,
1388                                                     &acpi_wmi_ec_space_handler,
1389                                                     NULL, NULL);
1390         if (ACPI_FAILURE(status)) {
1391                 dev_err(&device->dev, "Error installing EC region handler\n");
1392                 return -ENODEV;
1393         }
1394
1395         status = acpi_install_notify_handler(acpi_device->handle,
1396                                              ACPI_DEVICE_NOTIFY,
1397                                              acpi_wmi_notify_handler,
1398                                              NULL);
1399         if (ACPI_FAILURE(status)) {
1400                 dev_err(&device->dev, "Error installing notify handler\n");
1401                 error = -ENODEV;
1402                 goto err_remove_ec_handler;
1403         }
1404
1405         wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
1406                                     NULL, "wmi_bus-%s", dev_name(&device->dev));
1407         if (IS_ERR(wmi_bus_dev)) {
1408                 error = PTR_ERR(wmi_bus_dev);
1409                 goto err_remove_notify_handler;
1410         }
1411         dev_set_drvdata(&device->dev, wmi_bus_dev);
1412
1413         error = parse_wdg(wmi_bus_dev, acpi_device);
1414         if (error) {
1415                 pr_err("Failed to parse WDG method\n");
1416                 goto err_remove_busdev;
1417         }
1418
1419         return 0;
1420
1421 err_remove_busdev:
1422         device_destroy(&wmi_bus_class, MKDEV(0, 0));
1423
1424 err_remove_notify_handler:
1425         acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
1426                                    acpi_wmi_notify_handler);
1427
1428 err_remove_ec_handler:
1429         acpi_remove_address_space_handler(acpi_device->handle,
1430                                           ACPI_ADR_SPACE_EC,
1431                                           &acpi_wmi_ec_space_handler);
1432
1433         return error;
1434 }
1435
1436 int __must_check __wmi_driver_register(struct wmi_driver *driver,
1437                                        struct module *owner)
1438 {
1439         driver->driver.owner = owner;
1440         driver->driver.bus = &wmi_bus_type;
1441
1442         return driver_register(&driver->driver);
1443 }
1444 EXPORT_SYMBOL(__wmi_driver_register);
1445
1446 void wmi_driver_unregister(struct wmi_driver *driver)
1447 {
1448         driver_unregister(&driver->driver);
1449 }
1450 EXPORT_SYMBOL(wmi_driver_unregister);
1451
1452 static int __init acpi_wmi_init(void)
1453 {
1454         int error;
1455
1456         if (acpi_disabled)
1457                 return -ENODEV;
1458
1459         error = class_register(&wmi_bus_class);
1460         if (error)
1461                 return error;
1462
1463         error = bus_register(&wmi_bus_type);
1464         if (error)
1465                 goto err_unreg_class;
1466
1467         error = platform_driver_register(&acpi_wmi_driver);
1468         if (error) {
1469                 pr_err("Error loading mapper\n");
1470                 goto err_unreg_bus;
1471         }
1472
1473         return 0;
1474
1475 err_unreg_bus:
1476         bus_unregister(&wmi_bus_type);
1477
1478 err_unreg_class:
1479         class_unregister(&wmi_bus_class);
1480
1481         return error;
1482 }
1483
1484 static void __exit acpi_wmi_exit(void)
1485 {
1486         platform_driver_unregister(&acpi_wmi_driver);
1487         bus_unregister(&wmi_bus_type);
1488         class_unregister(&wmi_bus_class);
1489 }
1490
1491 subsys_initcall_sync(acpi_wmi_init);
1492 module_exit(acpi_wmi_exit);