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