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