2 * ACPI-WMI mapping driver
4 * Copyright (C) 2007-2008 Carlos Corbacho <carlos@strangeworlds.co.uk>
6 * GUID parsing code from ldm.c is:
7 * Copyright (C) 2001,2002 Richard Russon <ldm@flatcap.org>
8 * Copyright (c) 2001-2007 Anton Altaparmakov
9 * Copyright (C) 2001,2002 Jakob Kemi <jakob.kemi@telia.com>
11 * WMI bus infrastructure by Andrew Lutomirski and Darren Hart:
12 * Copyright (C) 2015 Andrew Lutomirski
13 * Copyright (C) 2017 VMware, Inc. All Rights Reserved.
15 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or (at
20 * your option) any later version.
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
31 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36 #include <linux/acpi.h>
37 #include <linux/device.h>
38 #include <linux/init.h>
39 #include <linux/kernel.h>
40 #include <linux/list.h>
41 #include <linux/miscdevice.h>
42 #include <linux/module.h>
43 #include <linux/platform_device.h>
44 #include <linux/slab.h>
45 #include <linux/types.h>
46 #include <linux/uaccess.h>
47 #include <linux/uuid.h>
48 #include <linux/wmi.h>
49 #include <uapi/linux/wmi.h>
51 ACPI_MODULE_NAME("wmi");
52 MODULE_AUTHOR("Carlos Corbacho");
53 MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
54 MODULE_LICENSE("GPL");
56 static LIST_HEAD(wmi_block_list);
63 unsigned char notify_id;
64 unsigned char reserved;
72 struct wmi_device dev;
73 struct list_head list;
74 struct guid_block gblock;
75 struct miscdevice char_dev;
76 struct mutex char_mutex;
77 struct acpi_device *acpi_device;
78 wmi_notify_handler handler;
82 bool read_takes_no_args;
87 * If the GUID data block is marked as expensive, we must enable and
88 * explicitily disable data collection.
90 #define ACPI_WMI_EXPENSIVE 0x1
91 #define ACPI_WMI_METHOD 0x2 /* GUID is a method */
92 #define ACPI_WMI_STRING 0x4 /* GUID takes & returns a string */
93 #define ACPI_WMI_EVENT 0x8 /* GUID is an event */
95 static bool debug_event;
96 module_param(debug_event, bool, 0444);
97 MODULE_PARM_DESC(debug_event,
98 "Log WMI Events [0/1]");
100 static bool debug_dump_wdg;
101 module_param(debug_dump_wdg, bool, 0444);
102 MODULE_PARM_DESC(debug_dump_wdg,
103 "Dump available WMI interfaces [0/1]");
105 static int acpi_wmi_remove(struct platform_device *device);
106 static int acpi_wmi_probe(struct platform_device *device);
108 static const struct acpi_device_id wmi_device_ids[] = {
113 MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
115 static struct platform_driver acpi_wmi_driver = {
118 .acpi_match_table = wmi_device_ids,
120 .probe = acpi_wmi_probe,
121 .remove = acpi_wmi_remove,
125 * GUID parsing functions
128 static bool find_guid(const char *guid_string, struct wmi_block **out)
131 struct wmi_block *wblock;
132 struct guid_block *block;
134 if (uuid_le_to_bin(guid_string, &guid_input))
137 list_for_each_entry(wblock, &wmi_block_list, list) {
138 block = &wblock->gblock;
140 if (memcmp(block->guid, &guid_input, 16) == 0) {
149 static const void *find_guid_context(struct wmi_block *wblock,
150 struct wmi_driver *wdriver)
152 const struct wmi_device_id *id;
155 if (wblock == NULL || wdriver == NULL)
157 if (wdriver->id_table == NULL)
160 id = wdriver->id_table;
161 while (*id->guid_string) {
162 if (uuid_le_to_bin(id->guid_string, &guid_input))
164 if (!memcmp(wblock->gblock.guid, &guid_input, 16))
171 static int get_subobj_info(acpi_handle handle, const char *pathname,
172 struct acpi_device_info **info)
174 struct acpi_device_info *dummy_info, **info_ptr;
175 acpi_handle subobj_handle;
178 status = acpi_get_handle(handle, (char *)pathname, &subobj_handle);
179 if (status == AE_NOT_FOUND)
181 else if (ACPI_FAILURE(status))
184 info_ptr = info ? info : &dummy_info;
185 status = acpi_get_object_info(subobj_handle, info_ptr);
186 if (ACPI_FAILURE(status))
195 static acpi_status wmi_method_enable(struct wmi_block *wblock, int enable)
197 struct guid_block *block = NULL;
202 block = &wblock->gblock;
203 handle = wblock->acpi_device->handle;
205 snprintf(method, 5, "WE%02X", block->notify_id);
206 status = acpi_execute_simple_method(handle, method, enable);
208 if (status != AE_OK && status != AE_NOT_FOUND)
215 * Exported WMI functions
219 * set_required_buffer_size - Sets the buffer size needed for performing IOCTL
220 * @wdev: A wmi bus device from a driver
221 * @instance: Instance index
223 * Allocates memory needed for buffer, stores the buffer size in that memory
225 int set_required_buffer_size(struct wmi_device *wdev, u64 length)
227 struct wmi_block *wblock;
229 wblock = container_of(wdev, struct wmi_block, dev);
230 wblock->req_buf_size = length;
234 EXPORT_SYMBOL_GPL(set_required_buffer_size);
237 * wmi_evaluate_method - Evaluate a WMI method
238 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
239 * @instance: Instance index
240 * @method_id: Method ID to call
241 * &in: Buffer containing input for the method call
242 * &out: Empty buffer to return the method results
244 * Call an ACPI-WMI method
246 acpi_status wmi_evaluate_method(const char *guid_string, u8 instance,
247 u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
249 struct wmi_block *wblock = NULL;
251 if (!find_guid(guid_string, &wblock))
253 return wmidev_evaluate_method(&wblock->dev, instance, method_id,
256 EXPORT_SYMBOL_GPL(wmi_evaluate_method);
259 * wmidev_evaluate_method - Evaluate a WMI method
260 * @wdev: A wmi bus device from a driver
261 * @instance: Instance index
262 * @method_id: Method ID to call
263 * &in: Buffer containing input for the method call
264 * &out: Empty buffer to return the method results
266 * Call an ACPI-WMI method
268 acpi_status wmidev_evaluate_method(struct wmi_device *wdev, u8 instance,
269 u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
271 struct guid_block *block = NULL;
272 struct wmi_block *wblock = NULL;
275 struct acpi_object_list input;
276 union acpi_object params[3];
277 char method[5] = "WM";
279 wblock = container_of(wdev, struct wmi_block, dev);
280 block = &wblock->gblock;
281 handle = wblock->acpi_device->handle;
283 if (!(block->flags & ACPI_WMI_METHOD))
286 if (block->instance_count <= instance)
287 return AE_BAD_PARAMETER;
290 input.pointer = params;
291 params[0].type = ACPI_TYPE_INTEGER;
292 params[0].integer.value = instance;
293 params[1].type = ACPI_TYPE_INTEGER;
294 params[1].integer.value = method_id;
299 if (block->flags & ACPI_WMI_STRING) {
300 params[2].type = ACPI_TYPE_STRING;
302 params[2].type = ACPI_TYPE_BUFFER;
304 params[2].buffer.length = in->length;
305 params[2].buffer.pointer = in->pointer;
308 strncat(method, block->object_id, 2);
310 status = acpi_evaluate_object(handle, method, &input, out);
314 EXPORT_SYMBOL_GPL(wmidev_evaluate_method);
316 static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
317 struct acpi_buffer *out)
319 struct guid_block *block = NULL;
321 acpi_status status, wc_status = AE_ERROR;
322 struct acpi_object_list input;
323 union acpi_object wq_params[1];
325 char wc_method[5] = "WC";
328 return AE_BAD_PARAMETER;
330 block = &wblock->gblock;
331 handle = wblock->acpi_device->handle;
333 if (block->instance_count <= instance)
334 return AE_BAD_PARAMETER;
336 /* Check GUID is a data block */
337 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
341 input.pointer = wq_params;
342 wq_params[0].type = ACPI_TYPE_INTEGER;
343 wq_params[0].integer.value = instance;
345 if (instance == 0 && wblock->read_takes_no_args)
349 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
352 if (block->flags & ACPI_WMI_EXPENSIVE) {
353 strncat(wc_method, block->object_id, 2);
356 * Some GUIDs break the specification by declaring themselves
357 * expensive, but have no corresponding WCxx method. So we
358 * should not fail if this happens.
360 if (acpi_has_method(handle, wc_method))
361 wc_status = acpi_execute_simple_method(handle,
365 strcpy(method, "WQ");
366 strncat(method, block->object_id, 2);
368 status = acpi_evaluate_object(handle, method, &input, out);
371 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
372 * the WQxx method failed - we should disable collection anyway.
374 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
375 status = acpi_execute_simple_method(handle, wc_method, 0);
382 * wmi_query_block - Return contents of a WMI block (deprecated)
383 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
384 * @instance: Instance index
385 * &out: Empty buffer to return the contents of the data block to
387 * Return the contents of an ACPI-WMI data block to a buffer
389 acpi_status wmi_query_block(const char *guid_string, u8 instance,
390 struct acpi_buffer *out)
392 struct wmi_block *wblock;
395 return AE_BAD_PARAMETER;
397 if (!find_guid(guid_string, &wblock))
400 return __query_block(wblock, instance, out);
402 EXPORT_SYMBOL_GPL(wmi_query_block);
404 union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance)
406 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
407 struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
409 if (ACPI_FAILURE(__query_block(wblock, instance, &out)))
412 return (union acpi_object *)out.pointer;
414 EXPORT_SYMBOL_GPL(wmidev_block_query);
417 * wmi_set_block - Write to a WMI block
418 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
419 * @instance: Instance index
420 * &in: Buffer containing new values for the data block
422 * Write the contents of the input buffer to an ACPI-WMI data block
424 acpi_status wmi_set_block(const char *guid_string, u8 instance,
425 const struct acpi_buffer *in)
427 struct guid_block *block = NULL;
428 struct wmi_block *wblock = NULL;
430 struct acpi_object_list input;
431 union acpi_object params[2];
432 char method[5] = "WS";
434 if (!guid_string || !in)
437 if (!find_guid(guid_string, &wblock))
440 block = &wblock->gblock;
441 handle = wblock->acpi_device->handle;
443 if (block->instance_count <= instance)
444 return AE_BAD_PARAMETER;
446 /* Check GUID is a data block */
447 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
451 input.pointer = params;
452 params[0].type = ACPI_TYPE_INTEGER;
453 params[0].integer.value = instance;
455 if (block->flags & ACPI_WMI_STRING) {
456 params[1].type = ACPI_TYPE_STRING;
458 params[1].type = ACPI_TYPE_BUFFER;
460 params[1].buffer.length = in->length;
461 params[1].buffer.pointer = in->pointer;
463 strncat(method, block->object_id, 2);
465 return acpi_evaluate_object(handle, method, &input, NULL);
467 EXPORT_SYMBOL_GPL(wmi_set_block);
469 static void wmi_dump_wdg(const struct guid_block *g)
471 pr_info("%pUL:\n", g->guid);
472 if (g->flags & ACPI_WMI_EVENT)
473 pr_info("\tnotify_id: 0x%02X\n", g->notify_id);
475 pr_info("\tobject_id: %2pE\n", g->object_id);
476 pr_info("\tinstance_count: %d\n", g->instance_count);
477 pr_info("\tflags: %#x", g->flags);
479 if (g->flags & ACPI_WMI_EXPENSIVE)
480 pr_cont(" ACPI_WMI_EXPENSIVE");
481 if (g->flags & ACPI_WMI_METHOD)
482 pr_cont(" ACPI_WMI_METHOD");
483 if (g->flags & ACPI_WMI_STRING)
484 pr_cont(" ACPI_WMI_STRING");
485 if (g->flags & ACPI_WMI_EVENT)
486 pr_cont(" ACPI_WMI_EVENT");
492 static void wmi_notify_debug(u32 value, void *context)
494 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
495 union acpi_object *obj;
498 status = wmi_get_event_data(value, &response);
499 if (status != AE_OK) {
500 pr_info("bad event status 0x%x\n", status);
504 obj = (union acpi_object *)response.pointer;
509 pr_info("DEBUG Event ");
511 case ACPI_TYPE_BUFFER:
512 pr_cont("BUFFER_TYPE - length %d\n", obj->buffer.length);
514 case ACPI_TYPE_STRING:
515 pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
517 case ACPI_TYPE_INTEGER:
518 pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
520 case ACPI_TYPE_PACKAGE:
521 pr_cont("PACKAGE_TYPE - %d elements\n", obj->package.count);
524 pr_cont("object type 0x%X\n", obj->type);
530 * wmi_install_notify_handler - Register handler for WMI events
531 * @handler: Function to handle notifications
532 * @data: Data to be returned to handler when event is fired
534 * Register a handler for events sent to the ACPI-WMI mapper device.
536 acpi_status wmi_install_notify_handler(const char *guid,
537 wmi_notify_handler handler, void *data)
539 struct wmi_block *block;
540 acpi_status status = AE_NOT_EXIST;
543 if (!guid || !handler)
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_ALREADY_ACQUIRED;
557 block->handler = handler;
558 block->handler_data = data;
560 wmi_status = wmi_method_enable(block, 1);
561 if ((wmi_status != AE_OK) ||
562 ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
569 EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
572 * wmi_uninstall_notify_handler - Unregister handler for WMI events
574 * Unregister handler for events sent to the ACPI-WMI mapper device.
576 acpi_status wmi_remove_notify_handler(const char *guid)
578 struct wmi_block *block;
579 acpi_status status = AE_NOT_EXIST;
583 return AE_BAD_PARAMETER;
585 if (uuid_le_to_bin(guid, &guid_input))
586 return AE_BAD_PARAMETER;
588 list_for_each_entry(block, &wmi_block_list, list) {
589 acpi_status wmi_status;
591 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
592 if (!block->handler ||
593 block->handler == wmi_notify_debug)
594 return AE_NULL_ENTRY;
597 block->handler = wmi_notify_debug;
600 wmi_status = wmi_method_enable(block, 0);
601 block->handler = NULL;
602 block->handler_data = NULL;
603 if ((wmi_status != AE_OK) ||
604 ((wmi_status == AE_OK) &&
605 (status == AE_NOT_EXIST)))
613 EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
616 * wmi_get_event_data - Get WMI data associated with an event
618 * @event: Event to find
619 * @out: Buffer to hold event data. out->pointer should be freed with kfree()
621 * Returns extra data associated with an event in WMI.
623 acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
625 struct acpi_object_list input;
626 union acpi_object params[1];
627 struct guid_block *gblock;
628 struct wmi_block *wblock;
631 input.pointer = params;
632 params[0].type = ACPI_TYPE_INTEGER;
633 params[0].integer.value = event;
635 list_for_each_entry(wblock, &wmi_block_list, list) {
636 gblock = &wblock->gblock;
638 if ((gblock->flags & ACPI_WMI_EVENT) &&
639 (gblock->notify_id == event))
640 return acpi_evaluate_object(wblock->acpi_device->handle,
641 "_WED", &input, out);
646 EXPORT_SYMBOL_GPL(wmi_get_event_data);
649 * wmi_has_guid - Check if a GUID is available
650 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
652 * Check if a given GUID is defined by _WDG
654 bool wmi_has_guid(const char *guid_string)
656 return find_guid(guid_string, NULL);
658 EXPORT_SYMBOL_GPL(wmi_has_guid);
661 * wmi_get_acpi_device_uid() - Get _UID name of ACPI device that defines GUID
662 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
664 * Find the _UID of ACPI device associated with this WMI GUID.
666 * Return: The ACPI _UID field value or NULL if the WMI GUID was not found
668 char *wmi_get_acpi_device_uid(const char *guid_string)
670 struct wmi_block *wblock = NULL;
672 if (!find_guid(guid_string, &wblock))
675 return acpi_device_uid(wblock->acpi_device);
677 EXPORT_SYMBOL_GPL(wmi_get_acpi_device_uid);
679 static struct wmi_block *dev_to_wblock(struct device *dev)
681 return container_of(dev, struct wmi_block, dev.dev);
684 static struct wmi_device *dev_to_wdev(struct device *dev)
686 return container_of(dev, struct wmi_device, dev);
692 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
695 struct wmi_block *wblock = dev_to_wblock(dev);
697 return sprintf(buf, "wmi:%pUL\n", wblock->gblock.guid);
699 static DEVICE_ATTR_RO(modalias);
701 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
704 struct wmi_block *wblock = dev_to_wblock(dev);
706 return sprintf(buf, "%pUL\n", wblock->gblock.guid);
708 static DEVICE_ATTR_RO(guid);
710 static ssize_t instance_count_show(struct device *dev,
711 struct device_attribute *attr, char *buf)
713 struct wmi_block *wblock = dev_to_wblock(dev);
715 return sprintf(buf, "%d\n", (int)wblock->gblock.instance_count);
717 static DEVICE_ATTR_RO(instance_count);
719 static ssize_t expensive_show(struct device *dev,
720 struct device_attribute *attr, char *buf)
722 struct wmi_block *wblock = dev_to_wblock(dev);
724 return sprintf(buf, "%d\n",
725 (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
727 static DEVICE_ATTR_RO(expensive);
729 static struct attribute *wmi_attrs[] = {
730 &dev_attr_modalias.attr,
732 &dev_attr_instance_count.attr,
733 &dev_attr_expensive.attr,
736 ATTRIBUTE_GROUPS(wmi);
738 static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
741 struct wmi_block *wblock = dev_to_wblock(dev);
743 return sprintf(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
745 static DEVICE_ATTR_RO(notify_id);
747 static struct attribute *wmi_event_attrs[] = {
748 &dev_attr_notify_id.attr,
751 ATTRIBUTE_GROUPS(wmi_event);
753 static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
756 struct wmi_block *wblock = dev_to_wblock(dev);
758 return sprintf(buf, "%c%c\n", wblock->gblock.object_id[0],
759 wblock->gblock.object_id[1]);
761 static DEVICE_ATTR_RO(object_id);
763 static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
766 struct wmi_device *wdev = dev_to_wdev(dev);
768 return sprintf(buf, "%d\n", (int)wdev->setable);
770 static DEVICE_ATTR_RO(setable);
772 static struct attribute *wmi_data_attrs[] = {
773 &dev_attr_object_id.attr,
774 &dev_attr_setable.attr,
777 ATTRIBUTE_GROUPS(wmi_data);
779 static struct attribute *wmi_method_attrs[] = {
780 &dev_attr_object_id.attr,
783 ATTRIBUTE_GROUPS(wmi_method);
785 static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
787 struct wmi_block *wblock = dev_to_wblock(dev);
789 if (add_uevent_var(env, "MODALIAS=wmi:%pUL", wblock->gblock.guid))
792 if (add_uevent_var(env, "WMI_GUID=%pUL", wblock->gblock.guid))
798 static void wmi_dev_release(struct device *dev)
800 struct wmi_block *wblock = dev_to_wblock(dev);
805 static int wmi_dev_match(struct device *dev, struct device_driver *driver)
807 struct wmi_driver *wmi_driver =
808 container_of(driver, struct wmi_driver, driver);
809 struct wmi_block *wblock = dev_to_wblock(dev);
810 const struct wmi_device_id *id = wmi_driver->id_table;
815 while (*id->guid_string) {
818 if (WARN_ON(uuid_le_to_bin(id->guid_string, &driver_guid)))
820 if (!memcmp(&driver_guid, wblock->gblock.guid, 16))
828 static int wmi_char_open(struct inode *inode, struct file *filp)
830 const char *driver_name = filp->f_path.dentry->d_iname;
831 struct wmi_block *wblock = NULL;
832 struct wmi_block *next = NULL;
834 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
835 if (!wblock->dev.dev.driver)
837 if (strcmp(driver_name, wblock->dev.dev.driver->name) == 0) {
838 filp->private_data = wblock;
843 if (!filp->private_data)
846 return nonseekable_open(inode, filp);
849 static ssize_t wmi_char_read(struct file *filp, char __user *buffer,
850 size_t length, loff_t *offset)
852 struct wmi_block *wblock = filp->private_data;
854 return simple_read_from_buffer(buffer, length, offset,
855 &wblock->req_buf_size,
856 sizeof(wblock->req_buf_size));
859 static long wmi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
861 struct wmi_ioctl_buffer __user *input =
862 (struct wmi_ioctl_buffer __user *) arg;
863 struct wmi_block *wblock = filp->private_data;
864 struct wmi_ioctl_buffer *buf = NULL;
865 struct wmi_driver *wdriver = NULL;
868 if (_IOC_TYPE(cmd) != WMI_IOC)
871 /* make sure we're not calling a higher instance than exists*/
872 if (_IOC_NR(cmd) >= wblock->gblock.instance_count)
875 mutex_lock(&wblock->char_mutex);
876 buf = wblock->handler_data;
877 if (get_user(buf->length, &input->length)) {
878 dev_dbg(&wblock->dev.dev, "Read length from user failed\n");
882 /* if it's too small, abort */
883 if (buf->length < wblock->req_buf_size) {
884 dev_err(&wblock->dev.dev,
885 "Buffer %lld too small, need at least %lld\n",
886 buf->length, wblock->req_buf_size);
890 /* if it's too big, warn, driver will only use what is needed */
891 if (buf->length > wblock->req_buf_size)
892 dev_warn(&wblock->dev.dev,
893 "Buffer %lld is bigger than required %lld\n",
894 buf->length, wblock->req_buf_size);
896 /* copy the structure from userspace */
897 if (copy_from_user(buf, input, wblock->req_buf_size)) {
898 dev_dbg(&wblock->dev.dev, "Copy %llu from user failed\n",
899 wblock->req_buf_size);
904 /* let the driver do any filtering and do the call */
905 wdriver = container_of(wblock->dev.dev.driver,
906 struct wmi_driver, driver);
907 if (!try_module_get(wdriver->driver.owner)) {
911 ret = wdriver->filter_callback(&wblock->dev, cmd, buf);
912 module_put(wdriver->driver.owner);
916 /* return the result (only up to our internal buffer size) */
917 if (copy_to_user(input, buf, wblock->req_buf_size)) {
918 dev_dbg(&wblock->dev.dev, "Copy %llu to user failed\n",
919 wblock->req_buf_size);
924 mutex_unlock(&wblock->char_mutex);
928 static const struct file_operations wmi_fops = {
929 .owner = THIS_MODULE,
930 .read = wmi_char_read,
931 .open = wmi_char_open,
932 .unlocked_ioctl = wmi_ioctl,
933 .compat_ioctl = wmi_ioctl,
936 static int wmi_dev_probe(struct device *dev)
938 struct wmi_block *wblock = dev_to_wblock(dev);
939 struct wmi_driver *wdriver =
940 container_of(dev->driver, struct wmi_driver, driver);
944 if (ACPI_FAILURE(wmi_method_enable(wblock, 1)))
945 dev_warn(dev, "failed to enable device -- probing anyway\n");
947 if (wdriver->probe) {
948 ret = wdriver->probe(dev_to_wdev(dev),
949 find_guid_context(wblock, wdriver));
954 /* driver wants a character device made */
955 if (wdriver->filter_callback) {
956 /* check that required buffer size declared by driver or MOF */
957 if (!wblock->req_buf_size) {
958 dev_err(&wblock->dev.dev,
959 "Required buffer size not set\n");
964 wblock->handler_data = kmalloc(wblock->req_buf_size,
966 if (!wblock->handler_data) {
971 buf = kasprintf(GFP_KERNEL, "wmi/%s", wdriver->driver.name);
974 goto probe_string_failure;
976 wblock->char_dev.minor = MISC_DYNAMIC_MINOR;
977 wblock->char_dev.name = buf;
978 wblock->char_dev.fops = &wmi_fops;
979 wblock->char_dev.mode = 0444;
980 ret = misc_register(&wblock->char_dev);
982 dev_warn(dev, "failed to register char dev: %d\n", ret);
984 goto probe_misc_failure;
992 probe_string_failure:
993 kfree(wblock->handler_data);
995 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
996 dev_warn(dev, "failed to disable device\n");
1000 static int wmi_dev_remove(struct device *dev)
1002 struct wmi_block *wblock = dev_to_wblock(dev);
1003 struct wmi_driver *wdriver =
1004 container_of(dev->driver, struct wmi_driver, driver);
1007 if (wdriver->filter_callback) {
1008 misc_deregister(&wblock->char_dev);
1009 kfree(wblock->char_dev.name);
1010 kfree(wblock->handler_data);
1013 if (wdriver->remove)
1014 ret = wdriver->remove(dev_to_wdev(dev));
1016 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
1017 dev_warn(dev, "failed to disable device\n");
1022 static struct class wmi_bus_class = {
1026 static struct bus_type wmi_bus_type = {
1028 .dev_groups = wmi_groups,
1029 .match = wmi_dev_match,
1030 .uevent = wmi_dev_uevent,
1031 .probe = wmi_dev_probe,
1032 .remove = wmi_dev_remove,
1035 static const struct device_type wmi_type_event = {
1037 .groups = wmi_event_groups,
1038 .release = wmi_dev_release,
1041 static const struct device_type wmi_type_method = {
1043 .groups = wmi_method_groups,
1044 .release = wmi_dev_release,
1047 static const struct device_type wmi_type_data = {
1049 .groups = wmi_data_groups,
1050 .release = wmi_dev_release,
1053 static int wmi_create_device(struct device *wmi_bus_dev,
1054 const struct guid_block *gblock,
1055 struct wmi_block *wblock,
1056 struct acpi_device *device)
1058 struct acpi_device_info *info;
1062 if (gblock->flags & ACPI_WMI_EVENT) {
1063 wblock->dev.dev.type = &wmi_type_event;
1067 if (gblock->flags & ACPI_WMI_METHOD) {
1068 wblock->dev.dev.type = &wmi_type_method;
1069 mutex_init(&wblock->char_mutex);
1074 * Data Block Query Control Method (WQxx by convention) is
1075 * required per the WMI documentation. If it is not present,
1076 * we ignore this data block.
1078 strcpy(method, "WQ");
1079 strncat(method, wblock->gblock.object_id, 2);
1080 result = get_subobj_info(device->handle, method, &info);
1083 dev_warn(wmi_bus_dev,
1084 "%s data block query control method not found\n",
1089 wblock->dev.dev.type = &wmi_type_data;
1092 * The Microsoft documentation specifically states:
1094 * Data blocks registered with only a single instance
1095 * can ignore the parameter.
1097 * ACPICA will get mad at us if we call the method with the wrong number
1098 * of arguments, so check what our method expects. (On some Dell
1099 * laptops, WQxx may not be a method at all.)
1101 if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
1102 wblock->read_takes_no_args = true;
1106 strcpy(method, "WS");
1107 strncat(method, wblock->gblock.object_id, 2);
1108 result = get_subobj_info(device->handle, method, NULL);
1111 wblock->dev.setable = true;
1114 wblock->dev.dev.bus = &wmi_bus_type;
1115 wblock->dev.dev.parent = wmi_bus_dev;
1117 dev_set_name(&wblock->dev.dev, "%pUL", gblock->guid);
1119 device_initialize(&wblock->dev.dev);
1124 static void wmi_free_devices(struct acpi_device *device)
1126 struct wmi_block *wblock, *next;
1128 /* Delete devices for all the GUIDs */
1129 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1130 if (wblock->acpi_device == device) {
1131 list_del(&wblock->list);
1132 device_unregister(&wblock->dev.dev);
1137 static bool guid_already_parsed(struct acpi_device *device,
1140 struct wmi_block *wblock;
1142 list_for_each_entry(wblock, &wmi_block_list, list) {
1143 if (memcmp(wblock->gblock.guid, guid, 16) == 0) {
1145 * Because we historically didn't track the relationship
1146 * between GUIDs and ACPI nodes, we don't know whether
1147 * we need to suppress GUIDs that are unique on a
1148 * given node but duplicated across nodes.
1150 dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
1151 guid, dev_name(&wblock->acpi_device->dev));
1160 * Parse the _WDG method for the GUID data blocks
1162 static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
1164 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
1165 const struct guid_block *gblock;
1166 struct wmi_block *wblock, *next;
1167 union acpi_object *obj;
1172 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
1173 if (ACPI_FAILURE(status))
1176 obj = (union acpi_object *) out.pointer;
1180 if (obj->type != ACPI_TYPE_BUFFER) {
1182 goto out_free_pointer;
1185 gblock = (const struct guid_block *)obj->buffer.pointer;
1186 total = obj->buffer.length / sizeof(struct guid_block);
1188 for (i = 0; i < total; i++) {
1190 wmi_dump_wdg(&gblock[i]);
1193 * Some WMI devices, like those for nVidia hooks, have a
1194 * duplicate GUID. It's not clear what we should do in this
1195 * case yet, so for now, we'll just ignore the duplicate
1196 * for device creation.
1198 if (guid_already_parsed(device, gblock[i].guid))
1201 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
1207 wblock->acpi_device = device;
1208 wblock->gblock = gblock[i];
1210 retval = wmi_create_device(wmi_bus_dev, &gblock[i], wblock, device);
1216 list_add_tail(&wblock->list, &wmi_block_list);
1219 wblock->handler = wmi_notify_debug;
1220 wmi_method_enable(wblock, 1);
1225 * Now that all of the devices are created, add them to the
1226 * device tree and probe subdrivers.
1228 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1229 if (wblock->acpi_device != device)
1232 retval = device_add(&wblock->dev.dev);
1234 dev_err(wmi_bus_dev, "failed to register %pUL\n",
1235 wblock->gblock.guid);
1237 wmi_method_enable(wblock, 0);
1238 list_del(&wblock->list);
1239 put_device(&wblock->dev.dev);
1249 * WMI can have EmbeddedControl access regions. In which case, we just want to
1250 * hand these off to the EC driver.
1253 acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
1254 u32 bits, u64 *value,
1255 void *handler_context, void *region_context)
1257 int result = 0, i = 0;
1260 if ((address > 0xFF) || !value)
1261 return AE_BAD_PARAMETER;
1263 if (function != ACPI_READ && function != ACPI_WRITE)
1264 return AE_BAD_PARAMETER;
1267 return AE_BAD_PARAMETER;
1269 if (function == ACPI_READ) {
1270 result = ec_read(address, &temp);
1271 (*value) |= ((u64)temp) << i;
1273 temp = 0xff & ((*value) >> i);
1274 result = ec_write(address, temp);
1279 return AE_BAD_PARAMETER;
1282 return AE_NOT_FOUND;
1292 static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
1295 struct guid_block *block;
1296 struct wmi_block *wblock;
1297 bool found_it = false;
1299 list_for_each_entry(wblock, &wmi_block_list, list) {
1300 block = &wblock->gblock;
1302 if (wblock->acpi_device->handle == handle &&
1303 (block->flags & ACPI_WMI_EVENT) &&
1304 (block->notify_id == event))
1314 /* If a driver is bound, then notify the driver. */
1315 if (wblock->dev.dev.driver) {
1316 struct wmi_driver *driver;
1317 struct acpi_object_list input;
1318 union acpi_object params[1];
1319 struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
1322 driver = container_of(wblock->dev.dev.driver,
1323 struct wmi_driver, driver);
1326 input.pointer = params;
1327 params[0].type = ACPI_TYPE_INTEGER;
1328 params[0].integer.value = event;
1330 status = acpi_evaluate_object(wblock->acpi_device->handle,
1331 "_WED", &input, &evdata);
1332 if (ACPI_FAILURE(status)) {
1333 dev_warn(&wblock->dev.dev,
1334 "failed to get event data\n");
1339 driver->notify(&wblock->dev,
1340 (union acpi_object *)evdata.pointer);
1342 kfree(evdata.pointer);
1343 } else if (wblock->handler) {
1344 /* Legacy handler */
1345 wblock->handler(event, wblock->handler_data);
1349 pr_info("DEBUG Event GUID: %pUL\n",
1350 wblock->gblock.guid);
1353 acpi_bus_generate_netlink_event(
1354 wblock->acpi_device->pnp.device_class,
1355 dev_name(&wblock->dev.dev),
1360 static int acpi_wmi_remove(struct platform_device *device)
1362 struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
1364 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
1365 acpi_wmi_notify_handler);
1366 acpi_remove_address_space_handler(acpi_device->handle,
1367 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
1368 wmi_free_devices(acpi_device);
1369 device_destroy(&wmi_bus_class, MKDEV(0, 0));
1374 static int acpi_wmi_probe(struct platform_device *device)
1376 struct acpi_device *acpi_device;
1377 struct device *wmi_bus_dev;
1381 acpi_device = ACPI_COMPANION(&device->dev);
1383 dev_err(&device->dev, "ACPI companion is missing\n");
1387 status = acpi_install_address_space_handler(acpi_device->handle,
1389 &acpi_wmi_ec_space_handler,
1391 if (ACPI_FAILURE(status)) {
1392 dev_err(&device->dev, "Error installing EC region handler\n");
1396 status = acpi_install_notify_handler(acpi_device->handle,
1398 acpi_wmi_notify_handler,
1400 if (ACPI_FAILURE(status)) {
1401 dev_err(&device->dev, "Error installing notify handler\n");
1403 goto err_remove_ec_handler;
1406 wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
1407 NULL, "wmi_bus-%s", dev_name(&device->dev));
1408 if (IS_ERR(wmi_bus_dev)) {
1409 error = PTR_ERR(wmi_bus_dev);
1410 goto err_remove_notify_handler;
1412 dev_set_drvdata(&device->dev, wmi_bus_dev);
1414 error = parse_wdg(wmi_bus_dev, acpi_device);
1416 pr_err("Failed to parse WDG method\n");
1417 goto err_remove_busdev;
1423 device_destroy(&wmi_bus_class, MKDEV(0, 0));
1425 err_remove_notify_handler:
1426 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
1427 acpi_wmi_notify_handler);
1429 err_remove_ec_handler:
1430 acpi_remove_address_space_handler(acpi_device->handle,
1432 &acpi_wmi_ec_space_handler);
1437 int __must_check __wmi_driver_register(struct wmi_driver *driver,
1438 struct module *owner)
1440 driver->driver.owner = owner;
1441 driver->driver.bus = &wmi_bus_type;
1443 return driver_register(&driver->driver);
1445 EXPORT_SYMBOL(__wmi_driver_register);
1447 void wmi_driver_unregister(struct wmi_driver *driver)
1449 driver_unregister(&driver->driver);
1451 EXPORT_SYMBOL(wmi_driver_unregister);
1453 static int __init acpi_wmi_init(void)
1460 error = class_register(&wmi_bus_class);
1464 error = bus_register(&wmi_bus_type);
1466 goto err_unreg_class;
1468 error = platform_driver_register(&acpi_wmi_driver);
1470 pr_err("Error loading mapper\n");
1477 bus_unregister(&wmi_bus_type);
1480 class_unregister(&wmi_bus_class);
1485 static void __exit acpi_wmi_exit(void)
1487 platform_driver_unregister(&acpi_wmi_driver);
1488 bus_unregister(&wmi_bus_type);
1489 class_unregister(&wmi_bus_class);
1492 subsys_initcall_sync(acpi_wmi_init);
1493 module_exit(acpi_wmi_exit);