1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ACPI-WMI mapping driver
5 * Copyright (C) 2007-2008 Carlos Corbacho <carlos@strangeworlds.co.uk>
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>
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.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
33 #include <uapi/linux/wmi.h>
35 ACPI_MODULE_NAME("wmi");
36 MODULE_AUTHOR("Carlos Corbacho");
37 MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
38 MODULE_LICENSE("GPL");
40 static LIST_HEAD(wmi_block_list);
47 unsigned char notify_id;
48 unsigned char reserved;
56 struct wmi_device dev;
57 struct list_head list;
58 struct guid_block gblock;
59 struct miscdevice char_dev;
60 struct mutex char_mutex;
61 struct acpi_device *acpi_device;
62 wmi_notify_handler handler;
66 bool read_takes_no_args;
71 * If the GUID data block is marked as expensive, we must enable and
72 * explicitily disable data collection.
74 #define ACPI_WMI_EXPENSIVE 0x1
75 #define ACPI_WMI_METHOD 0x2 /* GUID is a method */
76 #define ACPI_WMI_STRING 0x4 /* GUID takes & returns a string */
77 #define ACPI_WMI_EVENT 0x8 /* GUID is an event */
79 static bool debug_event;
80 module_param(debug_event, bool, 0444);
81 MODULE_PARM_DESC(debug_event,
82 "Log WMI Events [0/1]");
84 static bool debug_dump_wdg;
85 module_param(debug_dump_wdg, bool, 0444);
86 MODULE_PARM_DESC(debug_dump_wdg,
87 "Dump available WMI interfaces [0/1]");
89 static int acpi_wmi_remove(struct platform_device *device);
90 static int acpi_wmi_probe(struct platform_device *device);
92 static const struct acpi_device_id wmi_device_ids[] = {
97 MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
99 static struct platform_driver acpi_wmi_driver = {
102 .acpi_match_table = wmi_device_ids,
104 .probe = acpi_wmi_probe,
105 .remove = acpi_wmi_remove,
109 * GUID parsing functions
112 static bool find_guid(const char *guid_string, struct wmi_block **out)
115 struct wmi_block *wblock;
116 struct guid_block *block;
118 if (guid_parse(guid_string, &guid_input))
121 list_for_each_entry(wblock, &wmi_block_list, list) {
122 block = &wblock->gblock;
124 if (guid_equal(&block->guid, &guid_input)) {
133 static bool guid_parse_and_compare(const char *string, const guid_t *guid)
137 if (guid_parse(string, &guid_input))
140 return guid_equal(&guid_input, guid);
143 static const void *find_guid_context(struct wmi_block *wblock,
144 struct wmi_driver *wdriver)
146 const struct wmi_device_id *id;
148 if (wblock == NULL || wdriver == NULL)
150 if (wdriver->id_table == NULL)
153 id = wdriver->id_table;
154 while (*id->guid_string) {
155 if (guid_parse_and_compare(id->guid_string, &wblock->gblock.guid))
162 static int get_subobj_info(acpi_handle handle, const char *pathname,
163 struct acpi_device_info **info)
165 struct acpi_device_info *dummy_info, **info_ptr;
166 acpi_handle subobj_handle;
169 status = acpi_get_handle(handle, (char *)pathname, &subobj_handle);
170 if (status == AE_NOT_FOUND)
172 else if (ACPI_FAILURE(status))
175 info_ptr = info ? info : &dummy_info;
176 status = acpi_get_object_info(subobj_handle, info_ptr);
177 if (ACPI_FAILURE(status))
186 static acpi_status wmi_method_enable(struct wmi_block *wblock, int enable)
188 struct guid_block *block;
193 block = &wblock->gblock;
194 handle = wblock->acpi_device->handle;
196 snprintf(method, 5, "WE%02X", block->notify_id);
197 status = acpi_execute_simple_method(handle, method, enable);
199 if (status != AE_OK && status != AE_NOT_FOUND)
206 * Exported WMI functions
210 * set_required_buffer_size - Sets the buffer size needed for performing IOCTL
211 * @wdev: A wmi bus device from a driver
212 * @length: Required buffer size
214 * Allocates memory needed for buffer, stores the buffer size in that memory
216 int set_required_buffer_size(struct wmi_device *wdev, u64 length)
218 struct wmi_block *wblock;
220 wblock = container_of(wdev, struct wmi_block, dev);
221 wblock->req_buf_size = length;
225 EXPORT_SYMBOL_GPL(set_required_buffer_size);
228 * wmi_evaluate_method - Evaluate a WMI method
229 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
230 * @instance: Instance index
231 * @method_id: Method ID to call
232 * @in: Buffer containing input for the method call
233 * @out: Empty buffer to return the method results
235 * Call an ACPI-WMI method
237 acpi_status wmi_evaluate_method(const char *guid_string, u8 instance,
238 u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
240 struct wmi_block *wblock = NULL;
242 if (!find_guid(guid_string, &wblock))
244 return wmidev_evaluate_method(&wblock->dev, instance, method_id,
247 EXPORT_SYMBOL_GPL(wmi_evaluate_method);
250 * wmidev_evaluate_method - Evaluate a WMI method
251 * @wdev: A wmi bus device from a driver
252 * @instance: Instance index
253 * @method_id: Method ID to call
254 * @in: Buffer containing input for the method call
255 * @out: Empty buffer to return the method results
257 * Call an ACPI-WMI method
259 acpi_status wmidev_evaluate_method(struct wmi_device *wdev, u8 instance,
260 u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
262 struct guid_block *block;
263 struct wmi_block *wblock;
266 struct acpi_object_list input;
267 union acpi_object params[3];
268 char method[5] = "WM";
270 wblock = container_of(wdev, struct wmi_block, dev);
271 block = &wblock->gblock;
272 handle = wblock->acpi_device->handle;
274 if (!(block->flags & ACPI_WMI_METHOD))
277 if (block->instance_count <= instance)
278 return AE_BAD_PARAMETER;
281 input.pointer = params;
282 params[0].type = ACPI_TYPE_INTEGER;
283 params[0].integer.value = instance;
284 params[1].type = ACPI_TYPE_INTEGER;
285 params[1].integer.value = method_id;
290 if (block->flags & ACPI_WMI_STRING) {
291 params[2].type = ACPI_TYPE_STRING;
293 params[2].type = ACPI_TYPE_BUFFER;
295 params[2].buffer.length = in->length;
296 params[2].buffer.pointer = in->pointer;
299 strncat(method, block->object_id, 2);
301 status = acpi_evaluate_object(handle, method, &input, out);
305 EXPORT_SYMBOL_GPL(wmidev_evaluate_method);
307 static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
308 struct acpi_buffer *out)
310 struct guid_block *block;
312 acpi_status status, wc_status = AE_ERROR;
313 struct acpi_object_list input;
314 union acpi_object wq_params[1];
316 char wc_method[5] = "WC";
319 return AE_BAD_PARAMETER;
321 block = &wblock->gblock;
322 handle = wblock->acpi_device->handle;
324 if (block->instance_count <= instance)
325 return AE_BAD_PARAMETER;
327 /* Check GUID is a data block */
328 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
332 input.pointer = wq_params;
333 wq_params[0].type = ACPI_TYPE_INTEGER;
334 wq_params[0].integer.value = instance;
336 if (instance == 0 && wblock->read_takes_no_args)
340 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
343 if (block->flags & ACPI_WMI_EXPENSIVE) {
344 strncat(wc_method, block->object_id, 2);
347 * Some GUIDs break the specification by declaring themselves
348 * expensive, but have no corresponding WCxx method. So we
349 * should not fail if this happens.
351 wc_status = acpi_execute_simple_method(handle, wc_method, 1);
354 strcpy(method, "WQ");
355 strncat(method, block->object_id, 2);
357 status = acpi_evaluate_object(handle, method, &input, out);
360 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
361 * the WQxx method failed - we should disable collection anyway.
363 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
365 * Ignore whether this WCxx call succeeds or not since
366 * the previously executed WQxx method call might have
367 * succeeded, and returning the failing status code
368 * of this call would throw away the result of the WQxx
369 * call, potentially leaking memory.
371 acpi_execute_simple_method(handle, wc_method, 0);
378 * wmi_query_block - Return contents of a WMI block (deprecated)
379 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
380 * @instance: Instance index
381 * @out: Empty buffer to return the contents of the data block to
383 * Return the contents of an ACPI-WMI data block to a buffer
385 acpi_status wmi_query_block(const char *guid_string, u8 instance,
386 struct acpi_buffer *out)
388 struct wmi_block *wblock;
391 return AE_BAD_PARAMETER;
393 if (!find_guid(guid_string, &wblock))
396 return __query_block(wblock, instance, out);
398 EXPORT_SYMBOL_GPL(wmi_query_block);
400 union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance)
402 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
403 struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
405 if (ACPI_FAILURE(__query_block(wblock, instance, &out)))
408 return (union acpi_object *)out.pointer;
410 EXPORT_SYMBOL_GPL(wmidev_block_query);
413 * wmi_set_block - Write to a WMI block
414 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
415 * @instance: Instance index
416 * @in: Buffer containing new values for the data block
418 * Write the contents of the input buffer to an ACPI-WMI data block
420 acpi_status wmi_set_block(const char *guid_string, u8 instance,
421 const struct acpi_buffer *in)
423 struct wmi_block *wblock = NULL;
424 struct guid_block *block;
426 struct acpi_object_list input;
427 union acpi_object params[2];
428 char method[5] = "WS";
430 if (!guid_string || !in)
433 if (!find_guid(guid_string, &wblock))
436 block = &wblock->gblock;
437 handle = wblock->acpi_device->handle;
439 if (block->instance_count <= instance)
440 return AE_BAD_PARAMETER;
442 /* Check GUID is a data block */
443 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
447 input.pointer = params;
448 params[0].type = ACPI_TYPE_INTEGER;
449 params[0].integer.value = instance;
451 if (block->flags & ACPI_WMI_STRING) {
452 params[1].type = ACPI_TYPE_STRING;
454 params[1].type = ACPI_TYPE_BUFFER;
456 params[1].buffer.length = in->length;
457 params[1].buffer.pointer = in->pointer;
459 strncat(method, block->object_id, 2);
461 return acpi_evaluate_object(handle, method, &input, NULL);
463 EXPORT_SYMBOL_GPL(wmi_set_block);
465 static void wmi_dump_wdg(const struct guid_block *g)
467 pr_info("%pUL:\n", &g->guid);
468 if (g->flags & ACPI_WMI_EVENT)
469 pr_info("\tnotify_id: 0x%02X\n", g->notify_id);
471 pr_info("\tobject_id: %2pE\n", g->object_id);
472 pr_info("\tinstance_count: %d\n", g->instance_count);
473 pr_info("\tflags: %#x", g->flags);
475 if (g->flags & ACPI_WMI_EXPENSIVE)
476 pr_cont(" ACPI_WMI_EXPENSIVE");
477 if (g->flags & ACPI_WMI_METHOD)
478 pr_cont(" ACPI_WMI_METHOD");
479 if (g->flags & ACPI_WMI_STRING)
480 pr_cont(" ACPI_WMI_STRING");
481 if (g->flags & ACPI_WMI_EVENT)
482 pr_cont(" ACPI_WMI_EVENT");
488 static void wmi_notify_debug(u32 value, void *context)
490 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
491 union acpi_object *obj;
494 status = wmi_get_event_data(value, &response);
495 if (status != AE_OK) {
496 pr_info("bad event status 0x%x\n", status);
500 obj = (union acpi_object *)response.pointer;
505 pr_info("DEBUG Event ");
507 case ACPI_TYPE_BUFFER:
508 pr_cont("BUFFER_TYPE - length %d\n", obj->buffer.length);
510 case ACPI_TYPE_STRING:
511 pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
513 case ACPI_TYPE_INTEGER:
514 pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
516 case ACPI_TYPE_PACKAGE:
517 pr_cont("PACKAGE_TYPE - %d elements\n", obj->package.count);
520 pr_cont("object type 0x%X\n", obj->type);
526 * wmi_install_notify_handler - Register handler for WMI events
527 * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
528 * @handler: Function to handle notifications
529 * @data: Data to be returned to handler when event is fired
531 * Register a handler for events sent to the ACPI-WMI mapper device.
533 acpi_status wmi_install_notify_handler(const char *guid,
534 wmi_notify_handler handler, void *data)
536 struct wmi_block *block;
537 acpi_status status = AE_NOT_EXIST;
540 if (!guid || !handler)
541 return AE_BAD_PARAMETER;
543 if (guid_parse(guid, &guid_input))
544 return AE_BAD_PARAMETER;
546 list_for_each_entry(block, &wmi_block_list, list) {
547 acpi_status wmi_status;
549 if (guid_equal(&block->gblock.guid, &guid_input)) {
550 if (block->handler &&
551 block->handler != wmi_notify_debug)
552 return AE_ALREADY_ACQUIRED;
554 block->handler = handler;
555 block->handler_data = data;
557 wmi_status = wmi_method_enable(block, 1);
558 if ((wmi_status != AE_OK) ||
559 ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
566 EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
569 * wmi_uninstall_notify_handler - Unregister handler for WMI events
570 * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
572 * Unregister handler for events sent to the ACPI-WMI mapper device.
574 acpi_status wmi_remove_notify_handler(const char *guid)
576 struct wmi_block *block;
577 acpi_status status = AE_NOT_EXIST;
581 return AE_BAD_PARAMETER;
583 if (guid_parse(guid, &guid_input))
584 return AE_BAD_PARAMETER;
586 list_for_each_entry(block, &wmi_block_list, list) {
587 acpi_status wmi_status;
589 if (guid_equal(&block->gblock.guid, &guid_input)) {
590 if (!block->handler ||
591 block->handler == wmi_notify_debug)
592 return AE_NULL_ENTRY;
595 block->handler = wmi_notify_debug;
598 wmi_status = wmi_method_enable(block, 0);
599 block->handler = NULL;
600 block->handler_data = NULL;
601 if ((wmi_status != AE_OK) ||
602 ((wmi_status == AE_OK) &&
603 (status == AE_NOT_EXIST)))
611 EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
614 * wmi_get_event_data - Get WMI data associated with an event
616 * @event: Event to find
617 * @out: Buffer to hold event data. out->pointer should be freed with kfree()
619 * Returns extra data associated with an event in WMI.
621 acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
623 struct acpi_object_list input;
624 union acpi_object params[1];
625 struct wmi_block *wblock;
628 input.pointer = params;
629 params[0].type = ACPI_TYPE_INTEGER;
630 params[0].integer.value = event;
632 list_for_each_entry(wblock, &wmi_block_list, list) {
633 struct guid_block *gblock = &wblock->gblock;
635 if ((gblock->flags & ACPI_WMI_EVENT) &&
636 (gblock->notify_id == event))
637 return acpi_evaluate_object(wblock->acpi_device->handle,
638 "_WED", &input, out);
643 EXPORT_SYMBOL_GPL(wmi_get_event_data);
646 * wmi_has_guid - Check if a GUID is available
647 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
649 * Check if a given GUID is defined by _WDG
651 bool wmi_has_guid(const char *guid_string)
653 return find_guid(guid_string, NULL);
655 EXPORT_SYMBOL_GPL(wmi_has_guid);
658 * wmi_get_acpi_device_uid() - Get _UID name of ACPI device that defines GUID
659 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
661 * Find the _UID of ACPI device associated with this WMI GUID.
663 * Return: The ACPI _UID field value or NULL if the WMI GUID was not found
665 char *wmi_get_acpi_device_uid(const char *guid_string)
667 struct wmi_block *wblock = NULL;
669 if (!find_guid(guid_string, &wblock))
672 return acpi_device_uid(wblock->acpi_device);
674 EXPORT_SYMBOL_GPL(wmi_get_acpi_device_uid);
676 static struct wmi_block *dev_to_wblock(struct device *dev)
678 return container_of(dev, struct wmi_block, dev.dev);
681 static struct wmi_device *dev_to_wdev(struct device *dev)
683 return container_of(dev, struct wmi_device, dev);
689 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
692 struct wmi_block *wblock = dev_to_wblock(dev);
694 return sprintf(buf, "wmi:%pUL\n", &wblock->gblock.guid);
696 static DEVICE_ATTR_RO(modalias);
698 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
701 struct wmi_block *wblock = dev_to_wblock(dev);
703 return sprintf(buf, "%pUL\n", &wblock->gblock.guid);
705 static DEVICE_ATTR_RO(guid);
707 static ssize_t instance_count_show(struct device *dev,
708 struct device_attribute *attr, char *buf)
710 struct wmi_block *wblock = dev_to_wblock(dev);
712 return sprintf(buf, "%d\n", (int)wblock->gblock.instance_count);
714 static DEVICE_ATTR_RO(instance_count);
716 static ssize_t expensive_show(struct device *dev,
717 struct device_attribute *attr, char *buf)
719 struct wmi_block *wblock = dev_to_wblock(dev);
721 return sprintf(buf, "%d\n",
722 (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
724 static DEVICE_ATTR_RO(expensive);
726 static struct attribute *wmi_attrs[] = {
727 &dev_attr_modalias.attr,
729 &dev_attr_instance_count.attr,
730 &dev_attr_expensive.attr,
733 ATTRIBUTE_GROUPS(wmi);
735 static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
738 struct wmi_block *wblock = dev_to_wblock(dev);
740 return sprintf(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
742 static DEVICE_ATTR_RO(notify_id);
744 static struct attribute *wmi_event_attrs[] = {
745 &dev_attr_notify_id.attr,
748 ATTRIBUTE_GROUPS(wmi_event);
750 static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
753 struct wmi_block *wblock = dev_to_wblock(dev);
755 return sprintf(buf, "%c%c\n", wblock->gblock.object_id[0],
756 wblock->gblock.object_id[1]);
758 static DEVICE_ATTR_RO(object_id);
760 static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
763 struct wmi_device *wdev = dev_to_wdev(dev);
765 return sprintf(buf, "%d\n", (int)wdev->setable);
767 static DEVICE_ATTR_RO(setable);
769 static struct attribute *wmi_data_attrs[] = {
770 &dev_attr_object_id.attr,
771 &dev_attr_setable.attr,
774 ATTRIBUTE_GROUPS(wmi_data);
776 static struct attribute *wmi_method_attrs[] = {
777 &dev_attr_object_id.attr,
780 ATTRIBUTE_GROUPS(wmi_method);
782 static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
784 struct wmi_block *wblock = dev_to_wblock(dev);
786 if (add_uevent_var(env, "MODALIAS=wmi:%pUL", &wblock->gblock.guid))
789 if (add_uevent_var(env, "WMI_GUID=%pUL", &wblock->gblock.guid))
795 static void wmi_dev_release(struct device *dev)
797 struct wmi_block *wblock = dev_to_wblock(dev);
802 static int wmi_dev_match(struct device *dev, struct device_driver *driver)
804 struct wmi_driver *wmi_driver =
805 container_of(driver, struct wmi_driver, driver);
806 struct wmi_block *wblock = dev_to_wblock(dev);
807 const struct wmi_device_id *id = wmi_driver->id_table;
812 while (*id->guid_string) {
813 if (guid_parse_and_compare(id->guid_string, &wblock->gblock.guid))
821 static int wmi_char_open(struct inode *inode, struct file *filp)
824 * The miscdevice already stores a pointer to itself
825 * inside filp->private_data
827 struct wmi_block *wblock = container_of(filp->private_data, struct wmi_block, char_dev);
829 filp->private_data = wblock;
831 return nonseekable_open(inode, filp);
834 static ssize_t wmi_char_read(struct file *filp, char __user *buffer,
835 size_t length, loff_t *offset)
837 struct wmi_block *wblock = filp->private_data;
839 return simple_read_from_buffer(buffer, length, offset,
840 &wblock->req_buf_size,
841 sizeof(wblock->req_buf_size));
844 static long wmi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
846 struct wmi_ioctl_buffer __user *input =
847 (struct wmi_ioctl_buffer __user *) arg;
848 struct wmi_block *wblock = filp->private_data;
849 struct wmi_ioctl_buffer *buf;
850 struct wmi_driver *wdriver;
853 if (_IOC_TYPE(cmd) != WMI_IOC)
856 /* make sure we're not calling a higher instance than exists*/
857 if (_IOC_NR(cmd) >= wblock->gblock.instance_count)
860 mutex_lock(&wblock->char_mutex);
861 buf = wblock->handler_data;
862 if (get_user(buf->length, &input->length)) {
863 dev_dbg(&wblock->dev.dev, "Read length from user failed\n");
867 /* if it's too small, abort */
868 if (buf->length < wblock->req_buf_size) {
869 dev_err(&wblock->dev.dev,
870 "Buffer %lld too small, need at least %lld\n",
871 buf->length, wblock->req_buf_size);
875 /* if it's too big, warn, driver will only use what is needed */
876 if (buf->length > wblock->req_buf_size)
877 dev_warn(&wblock->dev.dev,
878 "Buffer %lld is bigger than required %lld\n",
879 buf->length, wblock->req_buf_size);
881 /* copy the structure from userspace */
882 if (copy_from_user(buf, input, wblock->req_buf_size)) {
883 dev_dbg(&wblock->dev.dev, "Copy %llu from user failed\n",
884 wblock->req_buf_size);
889 /* let the driver do any filtering and do the call */
890 wdriver = container_of(wblock->dev.dev.driver,
891 struct wmi_driver, driver);
892 if (!try_module_get(wdriver->driver.owner)) {
896 ret = wdriver->filter_callback(&wblock->dev, cmd, buf);
897 module_put(wdriver->driver.owner);
901 /* return the result (only up to our internal buffer size) */
902 if (copy_to_user(input, buf, wblock->req_buf_size)) {
903 dev_dbg(&wblock->dev.dev, "Copy %llu to user failed\n",
904 wblock->req_buf_size);
909 mutex_unlock(&wblock->char_mutex);
913 static const struct file_operations wmi_fops = {
914 .owner = THIS_MODULE,
915 .read = wmi_char_read,
916 .open = wmi_char_open,
917 .unlocked_ioctl = wmi_ioctl,
918 .compat_ioctl = compat_ptr_ioctl,
921 static int wmi_dev_probe(struct device *dev)
923 struct wmi_block *wblock = dev_to_wblock(dev);
924 struct wmi_driver *wdriver =
925 container_of(dev->driver, struct wmi_driver, driver);
929 if (ACPI_FAILURE(wmi_method_enable(wblock, 1)))
930 dev_warn(dev, "failed to enable device -- probing anyway\n");
932 if (wdriver->probe) {
933 ret = wdriver->probe(dev_to_wdev(dev),
934 find_guid_context(wblock, wdriver));
939 /* driver wants a character device made */
940 if (wdriver->filter_callback) {
941 /* check that required buffer size declared by driver or MOF */
942 if (!wblock->req_buf_size) {
943 dev_err(&wblock->dev.dev,
944 "Required buffer size not set\n");
949 wblock->handler_data = kmalloc(wblock->req_buf_size,
951 if (!wblock->handler_data) {
956 buf = kasprintf(GFP_KERNEL, "wmi/%s", wdriver->driver.name);
959 goto probe_string_failure;
961 wblock->char_dev.minor = MISC_DYNAMIC_MINOR;
962 wblock->char_dev.name = buf;
963 wblock->char_dev.fops = &wmi_fops;
964 wblock->char_dev.mode = 0444;
965 ret = misc_register(&wblock->char_dev);
967 dev_warn(dev, "failed to register char dev: %d\n", ret);
969 goto probe_misc_failure;
977 probe_string_failure:
978 kfree(wblock->handler_data);
980 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
981 dev_warn(dev, "failed to disable device\n");
985 static int wmi_dev_remove(struct device *dev)
987 struct wmi_block *wblock = dev_to_wblock(dev);
988 struct wmi_driver *wdriver =
989 container_of(dev->driver, struct wmi_driver, driver);
992 if (wdriver->filter_callback) {
993 misc_deregister(&wblock->char_dev);
994 kfree(wblock->char_dev.name);
995 kfree(wblock->handler_data);
999 ret = wdriver->remove(dev_to_wdev(dev));
1001 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
1002 dev_warn(dev, "failed to disable device\n");
1007 static struct class wmi_bus_class = {
1011 static struct bus_type wmi_bus_type = {
1013 .dev_groups = wmi_groups,
1014 .match = wmi_dev_match,
1015 .uevent = wmi_dev_uevent,
1016 .probe = wmi_dev_probe,
1017 .remove = wmi_dev_remove,
1020 static const struct device_type wmi_type_event = {
1022 .groups = wmi_event_groups,
1023 .release = wmi_dev_release,
1026 static const struct device_type wmi_type_method = {
1028 .groups = wmi_method_groups,
1029 .release = wmi_dev_release,
1032 static const struct device_type wmi_type_data = {
1034 .groups = wmi_data_groups,
1035 .release = wmi_dev_release,
1038 static int wmi_create_device(struct device *wmi_bus_dev,
1039 struct wmi_block *wblock,
1040 struct acpi_device *device)
1042 struct acpi_device_info *info;
1046 if (wblock->gblock.flags & ACPI_WMI_EVENT) {
1047 wblock->dev.dev.type = &wmi_type_event;
1051 if (wblock->gblock.flags & ACPI_WMI_METHOD) {
1052 wblock->dev.dev.type = &wmi_type_method;
1053 mutex_init(&wblock->char_mutex);
1058 * Data Block Query Control Method (WQxx by convention) is
1059 * required per the WMI documentation. If it is not present,
1060 * we ignore this data block.
1062 strcpy(method, "WQ");
1063 strncat(method, wblock->gblock.object_id, 2);
1064 result = get_subobj_info(device->handle, method, &info);
1067 dev_warn(wmi_bus_dev,
1068 "%s data block query control method not found\n",
1073 wblock->dev.dev.type = &wmi_type_data;
1076 * The Microsoft documentation specifically states:
1078 * Data blocks registered with only a single instance
1079 * can ignore the parameter.
1081 * ACPICA will get mad at us if we call the method with the wrong number
1082 * of arguments, so check what our method expects. (On some Dell
1083 * laptops, WQxx may not be a method at all.)
1085 if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
1086 wblock->read_takes_no_args = true;
1090 strcpy(method, "WS");
1091 strncat(method, wblock->gblock.object_id, 2);
1092 result = get_subobj_info(device->handle, method, NULL);
1095 wblock->dev.setable = true;
1098 wblock->dev.dev.bus = &wmi_bus_type;
1099 wblock->dev.dev.parent = wmi_bus_dev;
1101 dev_set_name(&wblock->dev.dev, "%pUL", &wblock->gblock.guid);
1103 device_initialize(&wblock->dev.dev);
1108 static void wmi_free_devices(struct acpi_device *device)
1110 struct wmi_block *wblock, *next;
1112 /* Delete devices for all the GUIDs */
1113 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1114 if (wblock->acpi_device == device) {
1115 list_del(&wblock->list);
1116 device_unregister(&wblock->dev.dev);
1121 static bool guid_already_parsed(struct acpi_device *device, const guid_t *guid)
1123 struct wmi_block *wblock;
1125 list_for_each_entry(wblock, &wmi_block_list, list) {
1126 if (guid_equal(&wblock->gblock.guid, guid)) {
1128 * Because we historically didn't track the relationship
1129 * between GUIDs and ACPI nodes, we don't know whether
1130 * we need to suppress GUIDs that are unique on a
1131 * given node but duplicated across nodes.
1133 dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
1134 guid, dev_name(&wblock->acpi_device->dev));
1143 * Parse the _WDG method for the GUID data blocks
1145 static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
1147 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
1148 const struct guid_block *gblock;
1149 struct wmi_block *wblock, *next;
1150 union acpi_object *obj;
1155 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
1156 if (ACPI_FAILURE(status))
1159 obj = (union acpi_object *) out.pointer;
1163 if (obj->type != ACPI_TYPE_BUFFER) {
1168 gblock = (const struct guid_block *)obj->buffer.pointer;
1169 total = obj->buffer.length / sizeof(struct guid_block);
1171 for (i = 0; i < total; i++) {
1173 wmi_dump_wdg(&gblock[i]);
1176 * Some WMI devices, like those for nVidia hooks, have a
1177 * duplicate GUID. It's not clear what we should do in this
1178 * case yet, so for now, we'll just ignore the duplicate
1179 * for device creation.
1181 if (guid_already_parsed(device, &gblock[i].guid))
1184 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
1186 dev_err(wmi_bus_dev, "Failed to allocate %pUL\n", &gblock[i].guid);
1190 wblock->acpi_device = device;
1191 wblock->gblock = gblock[i];
1193 retval = wmi_create_device(wmi_bus_dev, wblock, device);
1199 list_add_tail(&wblock->list, &wmi_block_list);
1202 wblock->handler = wmi_notify_debug;
1203 wmi_method_enable(wblock, 1);
1208 * Now that all of the devices are created, add them to the
1209 * device tree and probe subdrivers.
1211 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1212 if (wblock->acpi_device != device)
1215 retval = device_add(&wblock->dev.dev);
1217 dev_err(wmi_bus_dev, "failed to register %pUL\n",
1218 &wblock->gblock.guid);
1220 wmi_method_enable(wblock, 0);
1221 list_del(&wblock->list);
1222 put_device(&wblock->dev.dev);
1232 * WMI can have EmbeddedControl access regions. In which case, we just want to
1233 * hand these off to the EC driver.
1236 acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
1237 u32 bits, u64 *value,
1238 void *handler_context, void *region_context)
1240 int result = 0, i = 0;
1243 if ((address > 0xFF) || !value)
1244 return AE_BAD_PARAMETER;
1246 if (function != ACPI_READ && function != ACPI_WRITE)
1247 return AE_BAD_PARAMETER;
1250 return AE_BAD_PARAMETER;
1252 if (function == ACPI_READ) {
1253 result = ec_read(address, &temp);
1254 (*value) |= ((u64)temp) << i;
1256 temp = 0xff & ((*value) >> i);
1257 result = ec_write(address, temp);
1262 return AE_BAD_PARAMETER;
1265 return AE_NOT_FOUND;
1275 static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
1278 struct wmi_block *wblock;
1279 bool found_it = false;
1281 list_for_each_entry(wblock, &wmi_block_list, list) {
1282 struct guid_block *block = &wblock->gblock;
1284 if (wblock->acpi_device->handle == handle &&
1285 (block->flags & ACPI_WMI_EVENT) &&
1286 (block->notify_id == event))
1296 /* If a driver is bound, then notify the driver. */
1297 if (wblock->dev.dev.driver) {
1298 struct wmi_driver *driver;
1299 struct acpi_object_list input;
1300 union acpi_object params[1];
1301 struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
1304 driver = container_of(wblock->dev.dev.driver,
1305 struct wmi_driver, driver);
1308 input.pointer = params;
1309 params[0].type = ACPI_TYPE_INTEGER;
1310 params[0].integer.value = event;
1312 status = acpi_evaluate_object(wblock->acpi_device->handle,
1313 "_WED", &input, &evdata);
1314 if (ACPI_FAILURE(status)) {
1315 dev_warn(&wblock->dev.dev,
1316 "failed to get event data\n");
1321 driver->notify(&wblock->dev,
1322 (union acpi_object *)evdata.pointer);
1324 kfree(evdata.pointer);
1325 } else if (wblock->handler) {
1326 /* Legacy handler */
1327 wblock->handler(event, wblock->handler_data);
1331 pr_info("DEBUG Event GUID: %pUL\n", &wblock->gblock.guid);
1333 acpi_bus_generate_netlink_event(
1334 wblock->acpi_device->pnp.device_class,
1335 dev_name(&wblock->dev.dev),
1340 static int acpi_wmi_remove(struct platform_device *device)
1342 struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
1344 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
1345 acpi_wmi_notify_handler);
1346 acpi_remove_address_space_handler(acpi_device->handle,
1347 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
1348 wmi_free_devices(acpi_device);
1349 device_destroy(&wmi_bus_class, MKDEV(0, 0));
1354 static int acpi_wmi_probe(struct platform_device *device)
1356 struct acpi_device *acpi_device;
1357 struct device *wmi_bus_dev;
1361 acpi_device = ACPI_COMPANION(&device->dev);
1363 dev_err(&device->dev, "ACPI companion is missing\n");
1367 status = acpi_install_address_space_handler(acpi_device->handle,
1369 &acpi_wmi_ec_space_handler,
1371 if (ACPI_FAILURE(status)) {
1372 dev_err(&device->dev, "Error installing EC region handler\n");
1376 status = acpi_install_notify_handler(acpi_device->handle,
1378 acpi_wmi_notify_handler,
1380 if (ACPI_FAILURE(status)) {
1381 dev_err(&device->dev, "Error installing notify handler\n");
1383 goto err_remove_ec_handler;
1386 wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
1387 NULL, "wmi_bus-%s", dev_name(&device->dev));
1388 if (IS_ERR(wmi_bus_dev)) {
1389 error = PTR_ERR(wmi_bus_dev);
1390 goto err_remove_notify_handler;
1392 dev_set_drvdata(&device->dev, wmi_bus_dev);
1394 error = parse_wdg(wmi_bus_dev, acpi_device);
1396 pr_err("Failed to parse WDG method\n");
1397 goto err_remove_busdev;
1403 device_destroy(&wmi_bus_class, MKDEV(0, 0));
1405 err_remove_notify_handler:
1406 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
1407 acpi_wmi_notify_handler);
1409 err_remove_ec_handler:
1410 acpi_remove_address_space_handler(acpi_device->handle,
1412 &acpi_wmi_ec_space_handler);
1417 int __must_check __wmi_driver_register(struct wmi_driver *driver,
1418 struct module *owner)
1420 driver->driver.owner = owner;
1421 driver->driver.bus = &wmi_bus_type;
1423 return driver_register(&driver->driver);
1425 EXPORT_SYMBOL(__wmi_driver_register);
1427 void wmi_driver_unregister(struct wmi_driver *driver)
1429 driver_unregister(&driver->driver);
1431 EXPORT_SYMBOL(wmi_driver_unregister);
1433 static int __init acpi_wmi_init(void)
1440 error = class_register(&wmi_bus_class);
1444 error = bus_register(&wmi_bus_type);
1446 goto err_unreg_class;
1448 error = platform_driver_register(&acpi_wmi_driver);
1450 pr_err("Error loading mapper\n");
1457 bus_unregister(&wmi_bus_type);
1460 class_unregister(&wmi_bus_class);
1465 static void __exit acpi_wmi_exit(void)
1467 platform_driver_unregister(&acpi_wmi_driver);
1468 bus_unregister(&wmi_bus_type);
1469 class_unregister(&wmi_bus_class);
1472 subsys_initcall_sync(acpi_wmi_init);
1473 module_exit(acpi_wmi_exit);