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