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