1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ACPI-WMI mapping driver
7 * GUID parsing code from ldm.c is:
9 * Copyright (c) 2001-2007 Anton Altaparmakov
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>
32 #include <uapi/linux/wmi.h>
34 ACPI_MODULE_NAME("wmi");
35 MODULE_AUTHOR("Carlos Corbacho");
36 MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
37 MODULE_LICENSE("GPL");
39 static LIST_HEAD(wmi_block_list);
46 unsigned char notify_id;
47 unsigned char reserved;
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;
65 bool read_takes_no_args;
70 * If the GUID data block is marked as expensive, we must enable and
71 * explicitily disable data collection.
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 */
78 static bool debug_event;
79 module_param(debug_event, bool, 0444);
80 MODULE_PARM_DESC(debug_event,
81 "Log WMI Events [0/1]");
83 static bool debug_dump_wdg;
84 module_param(debug_dump_wdg, bool, 0444);
85 MODULE_PARM_DESC(debug_dump_wdg,
86 "Dump available WMI interfaces [0/1]");
88 static int acpi_wmi_remove(struct platform_device *device);
89 static int acpi_wmi_probe(struct platform_device *device);
91 static const struct acpi_device_id wmi_device_ids[] = {
96 MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
98 static struct platform_driver acpi_wmi_driver = {
101 .acpi_match_table = wmi_device_ids,
103 .probe = acpi_wmi_probe,
104 .remove = acpi_wmi_remove,
108 * GUID parsing functions
111 static bool find_guid(const char *guid_string, struct wmi_block **out)
114 struct wmi_block *wblock;
115 struct guid_block *block;
117 if (uuid_le_to_bin(guid_string, &guid_input))
120 list_for_each_entry(wblock, &wmi_block_list, list) {
121 block = &wblock->gblock;
123 if (memcmp(block->guid, &guid_input, 16) == 0) {
132 static int get_subobj_info(acpi_handle handle, const char *pathname,
133 struct acpi_device_info **info)
135 struct acpi_device_info *dummy_info, **info_ptr;
136 acpi_handle subobj_handle;
139 status = acpi_get_handle(handle, (char *)pathname, &subobj_handle);
140 if (status == AE_NOT_FOUND)
142 else if (ACPI_FAILURE(status))
145 info_ptr = info ? info : &dummy_info;
146 status = acpi_get_object_info(subobj_handle, info_ptr);
147 if (ACPI_FAILURE(status))
156 static acpi_status wmi_method_enable(struct wmi_block *wblock, int enable)
158 struct guid_block *block = NULL;
163 block = &wblock->gblock;
164 handle = wblock->acpi_device->handle;
166 snprintf(method, 5, "WE%02X", block->notify_id);
167 status = acpi_execute_simple_method(handle, method, enable);
169 if (status != AE_OK && status != AE_NOT_FOUND)
176 * Exported WMI functions
180 * set_required_buffer_size - Sets the buffer size needed for performing IOCTL
181 * @wdev: A wmi bus device from a driver
182 * @instance: Instance index
184 * Allocates memory needed for buffer, stores the buffer size in that memory
186 int set_required_buffer_size(struct wmi_device *wdev, u64 length)
188 struct wmi_block *wblock;
190 wblock = container_of(wdev, struct wmi_block, dev);
191 wblock->req_buf_size = length;
195 EXPORT_SYMBOL_GPL(set_required_buffer_size);
198 * wmi_evaluate_method - Evaluate a WMI method
199 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
200 * @instance: Instance index
201 * @method_id: Method ID to call
202 * &in: Buffer containing input for the method call
203 * &out: Empty buffer to return the method results
205 * Call an ACPI-WMI method
207 acpi_status wmi_evaluate_method(const char *guid_string, u8 instance,
208 u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
210 struct wmi_block *wblock = NULL;
212 if (!find_guid(guid_string, &wblock))
214 return wmidev_evaluate_method(&wblock->dev, instance, method_id,
217 EXPORT_SYMBOL_GPL(wmi_evaluate_method);
220 * wmidev_evaluate_method - Evaluate a WMI method
221 * @wdev: A wmi bus device from a driver
222 * @instance: Instance index
223 * @method_id: Method ID to call
224 * &in: Buffer containing input for the method call
225 * &out: Empty buffer to return the method results
227 * Call an ACPI-WMI method
229 acpi_status wmidev_evaluate_method(struct wmi_device *wdev, u8 instance,
230 u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
232 struct guid_block *block = NULL;
233 struct wmi_block *wblock = NULL;
236 struct acpi_object_list input;
237 union acpi_object params[3];
238 char method[5] = "WM";
240 wblock = container_of(wdev, struct wmi_block, dev);
241 block = &wblock->gblock;
242 handle = wblock->acpi_device->handle;
244 if (!(block->flags & ACPI_WMI_METHOD))
247 if (block->instance_count <= instance)
248 return AE_BAD_PARAMETER;
251 input.pointer = params;
252 params[0].type = ACPI_TYPE_INTEGER;
253 params[0].integer.value = instance;
254 params[1].type = ACPI_TYPE_INTEGER;
255 params[1].integer.value = method_id;
260 if (block->flags & ACPI_WMI_STRING) {
261 params[2].type = ACPI_TYPE_STRING;
263 params[2].type = ACPI_TYPE_BUFFER;
265 params[2].buffer.length = in->length;
266 params[2].buffer.pointer = in->pointer;
269 strncat(method, block->object_id, 2);
271 status = acpi_evaluate_object(handle, method, &input, out);
275 EXPORT_SYMBOL_GPL(wmidev_evaluate_method);
277 static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
278 struct acpi_buffer *out)
280 struct guid_block *block = NULL;
282 acpi_status status, wc_status = AE_ERROR;
283 struct acpi_object_list input;
284 union acpi_object wq_params[1];
286 char wc_method[5] = "WC";
289 return AE_BAD_PARAMETER;
291 block = &wblock->gblock;
292 handle = wblock->acpi_device->handle;
294 if (block->instance_count <= instance)
295 return AE_BAD_PARAMETER;
297 /* Check GUID is a data block */
298 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
302 input.pointer = wq_params;
303 wq_params[0].type = ACPI_TYPE_INTEGER;
304 wq_params[0].integer.value = instance;
306 if (instance == 0 && wblock->read_takes_no_args)
310 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
313 if (block->flags & ACPI_WMI_EXPENSIVE) {
314 strncat(wc_method, block->object_id, 2);
317 * Some GUIDs break the specification by declaring themselves
318 * expensive, but have no corresponding WCxx method. So we
319 * should not fail if this happens.
321 if (acpi_has_method(handle, wc_method))
322 wc_status = acpi_execute_simple_method(handle,
326 strcpy(method, "WQ");
327 strncat(method, block->object_id, 2);
329 status = acpi_evaluate_object(handle, method, &input, out);
332 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
333 * the WQxx method failed - we should disable collection anyway.
335 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
336 status = acpi_execute_simple_method(handle, wc_method, 0);
343 * wmi_query_block - Return contents of a WMI block (deprecated)
344 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
345 * @instance: Instance index
346 * &out: Empty buffer to return the contents of the data block to
348 * Return the contents of an ACPI-WMI data block to a buffer
350 acpi_status wmi_query_block(const char *guid_string, u8 instance,
351 struct acpi_buffer *out)
353 struct wmi_block *wblock;
356 return AE_BAD_PARAMETER;
358 if (!find_guid(guid_string, &wblock))
361 return __query_block(wblock, instance, out);
363 EXPORT_SYMBOL_GPL(wmi_query_block);
365 union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance)
367 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
368 struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
370 if (ACPI_FAILURE(__query_block(wblock, instance, &out)))
373 return (union acpi_object *)out.pointer;
375 EXPORT_SYMBOL_GPL(wmidev_block_query);
378 * wmi_set_block - Write to a WMI block
379 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
380 * @instance: Instance index
381 * &in: Buffer containing new values for the data block
383 * Write the contents of the input buffer to an ACPI-WMI data block
385 acpi_status wmi_set_block(const char *guid_string, u8 instance,
386 const struct acpi_buffer *in)
388 struct guid_block *block = NULL;
389 struct wmi_block *wblock = NULL;
391 struct acpi_object_list input;
392 union acpi_object params[2];
393 char method[5] = "WS";
395 if (!guid_string || !in)
398 if (!find_guid(guid_string, &wblock))
401 block = &wblock->gblock;
402 handle = wblock->acpi_device->handle;
404 if (block->instance_count <= instance)
405 return AE_BAD_PARAMETER;
407 /* Check GUID is a data block */
408 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
412 input.pointer = params;
413 params[0].type = ACPI_TYPE_INTEGER;
414 params[0].integer.value = instance;
416 if (block->flags & ACPI_WMI_STRING) {
417 params[1].type = ACPI_TYPE_STRING;
419 params[1].type = ACPI_TYPE_BUFFER;
421 params[1].buffer.length = in->length;
422 params[1].buffer.pointer = in->pointer;
424 strncat(method, block->object_id, 2);
426 return acpi_evaluate_object(handle, method, &input, NULL);
428 EXPORT_SYMBOL_GPL(wmi_set_block);
430 static void wmi_dump_wdg(const struct guid_block *g)
432 pr_info("%pUL:\n", g->guid);
433 if (g->flags & ACPI_WMI_EVENT)
434 pr_info("\tnotify_id: 0x%02X\n", g->notify_id);
436 pr_info("\tobject_id: %2pE\n", g->object_id);
437 pr_info("\tinstance_count: %d\n", g->instance_count);
438 pr_info("\tflags: %#x", g->flags);
440 if (g->flags & ACPI_WMI_EXPENSIVE)
441 pr_cont(" ACPI_WMI_EXPENSIVE");
442 if (g->flags & ACPI_WMI_METHOD)
443 pr_cont(" ACPI_WMI_METHOD");
444 if (g->flags & ACPI_WMI_STRING)
445 pr_cont(" ACPI_WMI_STRING");
446 if (g->flags & ACPI_WMI_EVENT)
447 pr_cont(" ACPI_WMI_EVENT");
453 static void wmi_notify_debug(u32 value, void *context)
455 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
456 union acpi_object *obj;
459 status = wmi_get_event_data(value, &response);
460 if (status != AE_OK) {
461 pr_info("bad event status 0x%x\n", status);
465 obj = (union acpi_object *)response.pointer;
470 pr_info("DEBUG Event ");
472 case ACPI_TYPE_BUFFER:
473 pr_cont("BUFFER_TYPE - length %d\n", obj->buffer.length);
475 case ACPI_TYPE_STRING:
476 pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
478 case ACPI_TYPE_INTEGER:
479 pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
481 case ACPI_TYPE_PACKAGE:
482 pr_cont("PACKAGE_TYPE - %d elements\n", obj->package.count);
485 pr_cont("object type 0x%X\n", obj->type);
491 * wmi_install_notify_handler - Register handler for WMI events
492 * @handler: Function to handle notifications
493 * @data: Data to be returned to handler when event is fired
495 * Register a handler for events sent to the ACPI-WMI mapper device.
497 acpi_status wmi_install_notify_handler(const char *guid,
498 wmi_notify_handler handler, void *data)
500 struct wmi_block *block;
501 acpi_status status = AE_NOT_EXIST;
504 if (!guid || !handler)
505 return AE_BAD_PARAMETER;
507 if (uuid_le_to_bin(guid, &guid_input))
508 return AE_BAD_PARAMETER;
510 list_for_each_entry(block, &wmi_block_list, list) {
511 acpi_status wmi_status;
513 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
514 if (block->handler &&
515 block->handler != wmi_notify_debug)
516 return AE_ALREADY_ACQUIRED;
518 block->handler = handler;
519 block->handler_data = data;
521 wmi_status = wmi_method_enable(block, 1);
522 if ((wmi_status != AE_OK) ||
523 ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
530 EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
533 * wmi_uninstall_notify_handler - Unregister handler for WMI events
535 * Unregister handler for events sent to the ACPI-WMI mapper device.
537 acpi_status wmi_remove_notify_handler(const char *guid)
539 struct wmi_block *block;
540 acpi_status status = AE_NOT_EXIST;
544 return AE_BAD_PARAMETER;
546 if (uuid_le_to_bin(guid, &guid_input))
547 return AE_BAD_PARAMETER;
549 list_for_each_entry(block, &wmi_block_list, list) {
550 acpi_status wmi_status;
552 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
553 if (!block->handler ||
554 block->handler == wmi_notify_debug)
555 return AE_NULL_ENTRY;
558 block->handler = wmi_notify_debug;
561 wmi_status = wmi_method_enable(block, 0);
562 block->handler = NULL;
563 block->handler_data = NULL;
564 if ((wmi_status != AE_OK) ||
565 ((wmi_status == AE_OK) &&
566 (status == AE_NOT_EXIST)))
574 EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
577 * wmi_get_event_data - Get WMI data associated with an event
579 * @event: Event to find
580 * @out: Buffer to hold event data. out->pointer should be freed with kfree()
582 * Returns extra data associated with an event in WMI.
584 acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
586 struct acpi_object_list input;
587 union acpi_object params[1];
588 struct guid_block *gblock;
589 struct wmi_block *wblock;
592 input.pointer = params;
593 params[0].type = ACPI_TYPE_INTEGER;
594 params[0].integer.value = event;
596 list_for_each_entry(wblock, &wmi_block_list, list) {
597 gblock = &wblock->gblock;
599 if ((gblock->flags & ACPI_WMI_EVENT) &&
600 (gblock->notify_id == event))
601 return acpi_evaluate_object(wblock->acpi_device->handle,
602 "_WED", &input, out);
607 EXPORT_SYMBOL_GPL(wmi_get_event_data);
610 * wmi_has_guid - Check if a GUID is available
611 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
613 * Check if a given GUID is defined by _WDG
615 bool wmi_has_guid(const char *guid_string)
617 return find_guid(guid_string, NULL);
619 EXPORT_SYMBOL_GPL(wmi_has_guid);
621 static struct wmi_block *dev_to_wblock(struct device *dev)
623 return container_of(dev, struct wmi_block, dev.dev);
626 static struct wmi_device *dev_to_wdev(struct device *dev)
628 return container_of(dev, struct wmi_device, dev);
634 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
637 struct wmi_block *wblock = dev_to_wblock(dev);
639 return sprintf(buf, "wmi:%pUL\n", wblock->gblock.guid);
641 static DEVICE_ATTR_RO(modalias);
643 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
646 struct wmi_block *wblock = dev_to_wblock(dev);
648 return sprintf(buf, "%pUL\n", wblock->gblock.guid);
650 static DEVICE_ATTR_RO(guid);
652 static ssize_t instance_count_show(struct device *dev,
653 struct device_attribute *attr, char *buf)
655 struct wmi_block *wblock = dev_to_wblock(dev);
657 return sprintf(buf, "%d\n", (int)wblock->gblock.instance_count);
659 static DEVICE_ATTR_RO(instance_count);
661 static ssize_t expensive_show(struct device *dev,
662 struct device_attribute *attr, char *buf)
664 struct wmi_block *wblock = dev_to_wblock(dev);
666 return sprintf(buf, "%d\n",
667 (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
669 static DEVICE_ATTR_RO(expensive);
671 static struct attribute *wmi_attrs[] = {
672 &dev_attr_modalias.attr,
674 &dev_attr_instance_count.attr,
675 &dev_attr_expensive.attr,
678 ATTRIBUTE_GROUPS(wmi);
680 static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
683 struct wmi_block *wblock = dev_to_wblock(dev);
685 return sprintf(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
687 static DEVICE_ATTR_RO(notify_id);
689 static struct attribute *wmi_event_attrs[] = {
690 &dev_attr_notify_id.attr,
693 ATTRIBUTE_GROUPS(wmi_event);
695 static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
698 struct wmi_block *wblock = dev_to_wblock(dev);
700 return sprintf(buf, "%c%c\n", wblock->gblock.object_id[0],
701 wblock->gblock.object_id[1]);
703 static DEVICE_ATTR_RO(object_id);
705 static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
708 struct wmi_device *wdev = dev_to_wdev(dev);
710 return sprintf(buf, "%d\n", (int)wdev->setable);
712 static DEVICE_ATTR_RO(setable);
714 static struct attribute *wmi_data_attrs[] = {
715 &dev_attr_object_id.attr,
716 &dev_attr_setable.attr,
719 ATTRIBUTE_GROUPS(wmi_data);
721 static struct attribute *wmi_method_attrs[] = {
722 &dev_attr_object_id.attr,
725 ATTRIBUTE_GROUPS(wmi_method);
727 static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
729 struct wmi_block *wblock = dev_to_wblock(dev);
731 if (add_uevent_var(env, "MODALIAS=wmi:%pUL", wblock->gblock.guid))
734 if (add_uevent_var(env, "WMI_GUID=%pUL", wblock->gblock.guid))
740 static void wmi_dev_release(struct device *dev)
742 struct wmi_block *wblock = dev_to_wblock(dev);
747 static int wmi_dev_match(struct device *dev, struct device_driver *driver)
749 struct wmi_driver *wmi_driver =
750 container_of(driver, struct wmi_driver, driver);
751 struct wmi_block *wblock = dev_to_wblock(dev);
752 const struct wmi_device_id *id = wmi_driver->id_table;
757 while (*id->guid_string) {
760 if (WARN_ON(uuid_le_to_bin(id->guid_string, &driver_guid)))
762 if (!memcmp(&driver_guid, wblock->gblock.guid, 16))
770 static int wmi_char_open(struct inode *inode, struct file *filp)
772 const char *driver_name = filp->f_path.dentry->d_iname;
773 struct wmi_block *wblock = NULL;
774 struct wmi_block *next = NULL;
776 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
777 if (!wblock->dev.dev.driver)
779 if (strcmp(driver_name, wblock->dev.dev.driver->name) == 0) {
780 filp->private_data = wblock;
785 if (!filp->private_data)
788 return nonseekable_open(inode, filp);
791 static ssize_t wmi_char_read(struct file *filp, char __user *buffer,
792 size_t length, loff_t *offset)
794 struct wmi_block *wblock = filp->private_data;
796 return simple_read_from_buffer(buffer, length, offset,
797 &wblock->req_buf_size,
798 sizeof(wblock->req_buf_size));
801 static long wmi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
803 struct wmi_ioctl_buffer __user *input =
804 (struct wmi_ioctl_buffer __user *) arg;
805 struct wmi_block *wblock = filp->private_data;
806 struct wmi_ioctl_buffer *buf = NULL;
807 struct wmi_driver *wdriver = NULL;
810 if (_IOC_TYPE(cmd) != WMI_IOC)
813 /* make sure we're not calling a higher instance than exists*/
814 if (_IOC_NR(cmd) >= wblock->gblock.instance_count)
817 mutex_lock(&wblock->char_mutex);
818 buf = wblock->handler_data;
819 if (get_user(buf->length, &input->length)) {
820 dev_dbg(&wblock->dev.dev, "Read length from user failed\n");
824 /* if it's too small, abort */
825 if (buf->length < wblock->req_buf_size) {
826 dev_err(&wblock->dev.dev,
827 "Buffer %lld too small, need at least %lld\n",
828 buf->length, wblock->req_buf_size);
832 /* if it's too big, warn, driver will only use what is needed */
833 if (buf->length > wblock->req_buf_size)
834 dev_warn(&wblock->dev.dev,
835 "Buffer %lld is bigger than required %lld\n",
836 buf->length, wblock->req_buf_size);
838 /* copy the structure from userspace */
839 if (copy_from_user(buf, input, wblock->req_buf_size)) {
840 dev_dbg(&wblock->dev.dev, "Copy %llu from user failed\n",
841 wblock->req_buf_size);
846 /* let the driver do any filtering and do the call */
847 wdriver = container_of(wblock->dev.dev.driver,
848 struct wmi_driver, driver);
849 if (!try_module_get(wdriver->driver.owner)) {
853 ret = wdriver->filter_callback(&wblock->dev, cmd, buf);
854 module_put(wdriver->driver.owner);
858 /* return the result (only up to our internal buffer size) */
859 if (copy_to_user(input, buf, wblock->req_buf_size)) {
860 dev_dbg(&wblock->dev.dev, "Copy %llu to user failed\n",
861 wblock->req_buf_size);
866 mutex_unlock(&wblock->char_mutex);
870 static const struct file_operations wmi_fops = {
871 .owner = THIS_MODULE,
872 .read = wmi_char_read,
873 .open = wmi_char_open,
874 .unlocked_ioctl = wmi_ioctl,
875 .compat_ioctl = wmi_ioctl,
878 static int wmi_dev_probe(struct device *dev)
880 struct wmi_block *wblock = dev_to_wblock(dev);
881 struct wmi_driver *wdriver =
882 container_of(dev->driver, struct wmi_driver, driver);
886 if (ACPI_FAILURE(wmi_method_enable(wblock, 1)))
887 dev_warn(dev, "failed to enable device -- probing anyway\n");
889 if (wdriver->probe) {
890 ret = wdriver->probe(dev_to_wdev(dev));
895 /* driver wants a character device made */
896 if (wdriver->filter_callback) {
897 /* check that required buffer size declared by driver or MOF */
898 if (!wblock->req_buf_size) {
899 dev_err(&wblock->dev.dev,
900 "Required buffer size not set\n");
905 wblock->handler_data = kmalloc(wblock->req_buf_size,
907 if (!wblock->handler_data) {
912 buf = kasprintf(GFP_KERNEL, "wmi/%s", wdriver->driver.name);
915 goto probe_string_failure;
917 wblock->char_dev.minor = MISC_DYNAMIC_MINOR;
918 wblock->char_dev.name = buf;
919 wblock->char_dev.fops = &wmi_fops;
920 wblock->char_dev.mode = 0444;
921 ret = misc_register(&wblock->char_dev);
923 dev_warn(dev, "failed to register char dev: %d\n", ret);
925 goto probe_misc_failure;
933 probe_string_failure:
934 kfree(wblock->handler_data);
936 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
937 dev_warn(dev, "failed to disable device\n");
941 static int wmi_dev_remove(struct device *dev)
943 struct wmi_block *wblock = dev_to_wblock(dev);
944 struct wmi_driver *wdriver =
945 container_of(dev->driver, struct wmi_driver, driver);
948 if (wdriver->filter_callback) {
949 misc_deregister(&wblock->char_dev);
950 kfree(wblock->char_dev.name);
951 kfree(wblock->handler_data);
955 ret = wdriver->remove(dev_to_wdev(dev));
957 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
958 dev_warn(dev, "failed to disable device\n");
963 static struct class wmi_bus_class = {
967 static struct bus_type wmi_bus_type = {
969 .dev_groups = wmi_groups,
970 .match = wmi_dev_match,
971 .uevent = wmi_dev_uevent,
972 .probe = wmi_dev_probe,
973 .remove = wmi_dev_remove,
976 static const struct device_type wmi_type_event = {
978 .groups = wmi_event_groups,
979 .release = wmi_dev_release,
982 static const struct device_type wmi_type_method = {
984 .groups = wmi_method_groups,
985 .release = wmi_dev_release,
988 static const struct device_type wmi_type_data = {
990 .groups = wmi_data_groups,
991 .release = wmi_dev_release,
994 static int wmi_create_device(struct device *wmi_bus_dev,
995 const struct guid_block *gblock,
996 struct wmi_block *wblock,
997 struct acpi_device *device)
999 struct acpi_device_info *info;
1003 if (gblock->flags & ACPI_WMI_EVENT) {
1004 wblock->dev.dev.type = &wmi_type_event;
1008 if (gblock->flags & ACPI_WMI_METHOD) {
1009 wblock->dev.dev.type = &wmi_type_method;
1010 mutex_init(&wblock->char_mutex);
1015 * Data Block Query Control Method (WQxx by convention) is
1016 * required per the WMI documentation. If it is not present,
1017 * we ignore this data block.
1019 strcpy(method, "WQ");
1020 strncat(method, wblock->gblock.object_id, 2);
1021 result = get_subobj_info(device->handle, method, &info);
1024 dev_warn(wmi_bus_dev,
1025 "%s data block query control method not found\n",
1030 wblock->dev.dev.type = &wmi_type_data;
1033 * The Microsoft documentation specifically states:
1035 * Data blocks registered with only a single instance
1036 * can ignore the parameter.
1038 * ACPICA will get mad at us if we call the method with the wrong number
1039 * of arguments, so check what our method expects. (On some Dell
1040 * laptops, WQxx may not be a method at all.)
1042 if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
1043 wblock->read_takes_no_args = true;
1047 strcpy(method, "WS");
1048 strncat(method, wblock->gblock.object_id, 2);
1049 result = get_subobj_info(device->handle, method, NULL);
1052 wblock->dev.setable = true;
1055 wblock->dev.dev.bus = &wmi_bus_type;
1056 wblock->dev.dev.parent = wmi_bus_dev;
1058 dev_set_name(&wblock->dev.dev, "%pUL", gblock->guid);
1060 device_initialize(&wblock->dev.dev);
1065 static void wmi_free_devices(struct acpi_device *device)
1067 struct wmi_block *wblock, *next;
1069 /* Delete devices for all the GUIDs */
1070 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1071 if (wblock->acpi_device == device) {
1072 list_del(&wblock->list);
1073 device_unregister(&wblock->dev.dev);
1078 static bool guid_already_parsed(struct acpi_device *device,
1081 struct wmi_block *wblock;
1083 list_for_each_entry(wblock, &wmi_block_list, list) {
1084 if (memcmp(wblock->gblock.guid, guid, 16) == 0) {
1086 * Because we historically didn't track the relationship
1087 * between GUIDs and ACPI nodes, we don't know whether
1088 * we need to suppress GUIDs that are unique on a
1089 * given node but duplicated across nodes.
1091 dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
1092 guid, dev_name(&wblock->acpi_device->dev));
1101 * Parse the _WDG method for the GUID data blocks
1103 static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
1105 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
1106 const struct guid_block *gblock;
1107 struct wmi_block *wblock, *next;
1108 union acpi_object *obj;
1113 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
1114 if (ACPI_FAILURE(status))
1117 obj = (union acpi_object *) out.pointer;
1121 if (obj->type != ACPI_TYPE_BUFFER) {
1123 goto out_free_pointer;
1126 gblock = (const struct guid_block *)obj->buffer.pointer;
1127 total = obj->buffer.length / sizeof(struct guid_block);
1129 for (i = 0; i < total; i++) {
1131 wmi_dump_wdg(&gblock[i]);
1134 * Some WMI devices, like those for nVidia hooks, have a
1135 * duplicate GUID. It's not clear what we should do in this
1136 * case yet, so for now, we'll just ignore the duplicate
1137 * for device creation.
1139 if (guid_already_parsed(device, gblock[i].guid))
1142 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
1148 wblock->acpi_device = device;
1149 wblock->gblock = gblock[i];
1151 retval = wmi_create_device(wmi_bus_dev, &gblock[i], wblock, device);
1157 list_add_tail(&wblock->list, &wmi_block_list);
1160 wblock->handler = wmi_notify_debug;
1161 wmi_method_enable(wblock, 1);
1166 * Now that all of the devices are created, add them to the
1167 * device tree and probe subdrivers.
1169 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1170 if (wblock->acpi_device != device)
1173 retval = device_add(&wblock->dev.dev);
1175 dev_err(wmi_bus_dev, "failed to register %pUL\n",
1176 wblock->gblock.guid);
1178 wmi_method_enable(wblock, 0);
1179 list_del(&wblock->list);
1180 put_device(&wblock->dev.dev);
1190 * WMI can have EmbeddedControl access regions. In which case, we just want to
1191 * hand these off to the EC driver.
1194 acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
1195 u32 bits, u64 *value,
1196 void *handler_context, void *region_context)
1198 int result = 0, i = 0;
1201 if ((address > 0xFF) || !value)
1202 return AE_BAD_PARAMETER;
1204 if (function != ACPI_READ && function != ACPI_WRITE)
1205 return AE_BAD_PARAMETER;
1208 return AE_BAD_PARAMETER;
1210 if (function == ACPI_READ) {
1211 result = ec_read(address, &temp);
1212 (*value) |= ((u64)temp) << i;
1214 temp = 0xff & ((*value) >> i);
1215 result = ec_write(address, temp);
1220 return AE_BAD_PARAMETER;
1223 return AE_NOT_FOUND;
1233 static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
1236 struct guid_block *block;
1237 struct wmi_block *wblock;
1238 bool found_it = false;
1240 list_for_each_entry(wblock, &wmi_block_list, list) {
1241 block = &wblock->gblock;
1243 if (wblock->acpi_device->handle == handle &&
1244 (block->flags & ACPI_WMI_EVENT) &&
1245 (block->notify_id == event))
1255 /* If a driver is bound, then notify the driver. */
1256 if (wblock->dev.dev.driver) {
1257 struct wmi_driver *driver;
1258 struct acpi_object_list input;
1259 union acpi_object params[1];
1260 struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
1263 driver = container_of(wblock->dev.dev.driver,
1264 struct wmi_driver, driver);
1267 input.pointer = params;
1268 params[0].type = ACPI_TYPE_INTEGER;
1269 params[0].integer.value = event;
1271 status = acpi_evaluate_object(wblock->acpi_device->handle,
1272 "_WED", &input, &evdata);
1273 if (ACPI_FAILURE(status)) {
1274 dev_warn(&wblock->dev.dev,
1275 "failed to get event data\n");
1280 driver->notify(&wblock->dev,
1281 (union acpi_object *)evdata.pointer);
1283 kfree(evdata.pointer);
1284 } else if (wblock->handler) {
1285 /* Legacy handler */
1286 wblock->handler(event, wblock->handler_data);
1290 pr_info("DEBUG Event GUID: %pUL\n",
1291 wblock->gblock.guid);
1294 acpi_bus_generate_netlink_event(
1295 wblock->acpi_device->pnp.device_class,
1296 dev_name(&wblock->dev.dev),
1301 static int acpi_wmi_remove(struct platform_device *device)
1303 struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
1305 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
1306 acpi_wmi_notify_handler);
1307 acpi_remove_address_space_handler(acpi_device->handle,
1308 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
1309 wmi_free_devices(acpi_device);
1310 device_destroy(&wmi_bus_class, MKDEV(0, 0));
1315 static int acpi_wmi_probe(struct platform_device *device)
1317 struct acpi_device *acpi_device;
1318 struct device *wmi_bus_dev;
1322 acpi_device = ACPI_COMPANION(&device->dev);
1324 dev_err(&device->dev, "ACPI companion is missing\n");
1328 status = acpi_install_address_space_handler(acpi_device->handle,
1330 &acpi_wmi_ec_space_handler,
1332 if (ACPI_FAILURE(status)) {
1333 dev_err(&device->dev, "Error installing EC region handler\n");
1337 status = acpi_install_notify_handler(acpi_device->handle,
1339 acpi_wmi_notify_handler,
1341 if (ACPI_FAILURE(status)) {
1342 dev_err(&device->dev, "Error installing notify handler\n");
1344 goto err_remove_ec_handler;
1347 wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
1348 NULL, "wmi_bus-%s", dev_name(&device->dev));
1349 if (IS_ERR(wmi_bus_dev)) {
1350 error = PTR_ERR(wmi_bus_dev);
1351 goto err_remove_notify_handler;
1353 dev_set_drvdata(&device->dev, wmi_bus_dev);
1355 error = parse_wdg(wmi_bus_dev, acpi_device);
1357 pr_err("Failed to parse WDG method\n");
1358 goto err_remove_busdev;
1364 device_destroy(&wmi_bus_class, MKDEV(0, 0));
1366 err_remove_notify_handler:
1367 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
1368 acpi_wmi_notify_handler);
1370 err_remove_ec_handler:
1371 acpi_remove_address_space_handler(acpi_device->handle,
1373 &acpi_wmi_ec_space_handler);
1378 int __must_check __wmi_driver_register(struct wmi_driver *driver,
1379 struct module *owner)
1381 driver->driver.owner = owner;
1382 driver->driver.bus = &wmi_bus_type;
1384 return driver_register(&driver->driver);
1386 EXPORT_SYMBOL(__wmi_driver_register);
1388 void wmi_driver_unregister(struct wmi_driver *driver)
1390 driver_unregister(&driver->driver);
1392 EXPORT_SYMBOL(wmi_driver_unregister);
1394 static int __init acpi_wmi_init(void)
1401 error = class_register(&wmi_bus_class);
1405 error = bus_register(&wmi_bus_type);
1407 goto err_unreg_class;
1409 error = platform_driver_register(&acpi_wmi_driver);
1411 pr_err("Error loading mapper\n");
1418 bus_unregister(&wmi_bus_type);
1421 class_unregister(&wmi_bus_class);
1426 static void __exit acpi_wmi_exit(void)
1428 platform_driver_unregister(&acpi_wmi_driver);
1429 bus_unregister(&wmi_bus_type);
1430 class_unregister(&wmi_bus_class);
1433 subsys_initcall_sync(acpi_wmi_init);
1434 module_exit(acpi_wmi_exit);