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 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;
54 enum { /* wmi_block flags */
55 WMI_READ_TAKES_NO_ARGS,
60 struct wmi_device dev;
61 struct list_head list;
62 struct guid_block gblock;
63 struct miscdevice char_dev;
64 struct mutex char_mutex;
65 struct acpi_device *acpi_device;
66 wmi_notify_handler handler;
74 * If the GUID data block is marked as expensive, we must enable and
75 * explicitily disable data collection.
77 #define ACPI_WMI_EXPENSIVE 0x1
78 #define ACPI_WMI_METHOD 0x2 /* GUID is a method */
79 #define ACPI_WMI_STRING 0x4 /* GUID takes & returns a string */
80 #define ACPI_WMI_EVENT 0x8 /* GUID is an event */
82 static bool debug_event;
83 module_param(debug_event, bool, 0444);
84 MODULE_PARM_DESC(debug_event,
85 "Log WMI Events [0/1]");
87 static bool debug_dump_wdg;
88 module_param(debug_dump_wdg, bool, 0444);
89 MODULE_PARM_DESC(debug_dump_wdg,
90 "Dump available WMI interfaces [0/1]");
92 static int acpi_wmi_remove(struct platform_device *device);
93 static int acpi_wmi_probe(struct platform_device *device);
95 static const struct acpi_device_id wmi_device_ids[] = {
100 MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
102 static struct platform_driver acpi_wmi_driver = {
105 .acpi_match_table = wmi_device_ids,
107 .probe = acpi_wmi_probe,
108 .remove = acpi_wmi_remove,
112 * GUID parsing functions
115 static bool find_guid(const char *guid_string, struct wmi_block **out)
118 struct wmi_block *wblock;
119 struct guid_block *block;
121 if (guid_parse(guid_string, &guid_input))
124 list_for_each_entry(wblock, &wmi_block_list, list) {
125 block = &wblock->gblock;
127 if (memcmp(block->guid, &guid_input, 16) == 0) {
136 static const void *find_guid_context(struct wmi_block *wblock,
137 struct wmi_driver *wdriver)
139 const struct wmi_device_id *id;
142 if (wblock == NULL || wdriver == NULL)
144 if (wdriver->id_table == NULL)
147 id = wdriver->id_table;
148 while (*id->guid_string) {
149 if (guid_parse(id->guid_string, &guid_input))
151 if (!memcmp(wblock->gblock.guid, &guid_input, 16))
158 static int get_subobj_info(acpi_handle handle, const char *pathname,
159 struct acpi_device_info **info)
161 struct acpi_device_info *dummy_info, **info_ptr;
162 acpi_handle subobj_handle;
165 status = acpi_get_handle(handle, (char *)pathname, &subobj_handle);
166 if (status == AE_NOT_FOUND)
168 else if (ACPI_FAILURE(status))
171 info_ptr = info ? info : &dummy_info;
172 status = acpi_get_object_info(subobj_handle, info_ptr);
173 if (ACPI_FAILURE(status))
182 static acpi_status wmi_method_enable(struct wmi_block *wblock, int enable)
184 struct guid_block *block = NULL;
189 block = &wblock->gblock;
190 handle = wblock->acpi_device->handle;
192 snprintf(method, 5, "WE%02X", block->notify_id);
193 status = acpi_execute_simple_method(handle, method, enable);
195 if (status != AE_OK && status != AE_NOT_FOUND)
202 * Exported WMI functions
206 * set_required_buffer_size - Sets the buffer size needed for performing IOCTL
207 * @wdev: A wmi bus device from a driver
208 * @length: Required buffer size
210 * Allocates memory needed for buffer, stores the buffer size in that memory
212 int set_required_buffer_size(struct wmi_device *wdev, u64 length)
214 struct wmi_block *wblock;
216 wblock = container_of(wdev, struct wmi_block, dev);
217 wblock->req_buf_size = length;
221 EXPORT_SYMBOL_GPL(set_required_buffer_size);
224 * wmi_evaluate_method - Evaluate a WMI method
225 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
226 * @instance: Instance index
227 * @method_id: Method ID to call
228 * @in: Buffer containing input for the method call
229 * @out: Empty buffer to return the method results
231 * Call an ACPI-WMI method
233 acpi_status wmi_evaluate_method(const char *guid_string, u8 instance,
234 u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
236 struct wmi_block *wblock = NULL;
238 if (!find_guid(guid_string, &wblock))
240 return wmidev_evaluate_method(&wblock->dev, instance, method_id,
243 EXPORT_SYMBOL_GPL(wmi_evaluate_method);
246 * wmidev_evaluate_method - Evaluate a WMI method
247 * @wdev: A wmi bus device from a driver
248 * @instance: Instance index
249 * @method_id: Method ID to call
250 * @in: Buffer containing input for the method call
251 * @out: Empty buffer to return the method results
253 * Call an ACPI-WMI method
255 acpi_status wmidev_evaluate_method(struct wmi_device *wdev, u8 instance,
256 u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
258 struct guid_block *block = NULL;
259 struct wmi_block *wblock = NULL;
262 struct acpi_object_list input;
263 union acpi_object params[3];
264 char method[5] = "WM";
266 wblock = container_of(wdev, struct wmi_block, dev);
267 block = &wblock->gblock;
268 handle = wblock->acpi_device->handle;
270 if (!(block->flags & ACPI_WMI_METHOD))
273 if (block->instance_count <= instance)
274 return AE_BAD_PARAMETER;
277 input.pointer = params;
278 params[0].type = ACPI_TYPE_INTEGER;
279 params[0].integer.value = instance;
280 params[1].type = ACPI_TYPE_INTEGER;
281 params[1].integer.value = method_id;
286 if (block->flags & ACPI_WMI_STRING) {
287 params[2].type = ACPI_TYPE_STRING;
289 params[2].type = ACPI_TYPE_BUFFER;
291 params[2].buffer.length = in->length;
292 params[2].buffer.pointer = in->pointer;
295 strncat(method, block->object_id, 2);
297 status = acpi_evaluate_object(handle, method, &input, out);
301 EXPORT_SYMBOL_GPL(wmidev_evaluate_method);
303 static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
304 struct acpi_buffer *out)
306 struct guid_block *block = NULL;
308 acpi_status status, wc_status = AE_ERROR;
309 struct acpi_object_list input;
310 union acpi_object wq_params[1];
312 char wc_method[5] = "WC";
315 return AE_BAD_PARAMETER;
317 block = &wblock->gblock;
318 handle = wblock->acpi_device->handle;
320 if (block->instance_count <= instance)
321 return AE_BAD_PARAMETER;
323 /* Check GUID is a data block */
324 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
328 input.pointer = wq_params;
329 wq_params[0].type = ACPI_TYPE_INTEGER;
330 wq_params[0].integer.value = instance;
332 if (instance == 0 && test_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags))
336 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
339 if (block->flags & ACPI_WMI_EXPENSIVE) {
340 strncat(wc_method, block->object_id, 2);
343 * Some GUIDs break the specification by declaring themselves
344 * expensive, but have no corresponding WCxx method. So we
345 * should not fail if this happens.
347 wc_status = acpi_execute_simple_method(handle, wc_method, 1);
350 strcpy(method, "WQ");
351 strncat(method, block->object_id, 2);
353 status = acpi_evaluate_object(handle, method, &input, out);
356 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
357 * the WQxx method failed - we should disable collection anyway.
359 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
361 * Ignore whether this WCxx call succeeds or not since
362 * the previously executed WQxx method call might have
363 * succeeded, and returning the failing status code
364 * of this call would throw away the result of the WQxx
365 * call, potentially leaking memory.
367 acpi_execute_simple_method(handle, wc_method, 0);
374 * wmi_query_block - Return contents of a WMI block (deprecated)
375 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
376 * @instance: Instance index
377 * @out: Empty buffer to return the contents of the data block to
379 * Return the contents of an ACPI-WMI data block to a buffer
381 acpi_status wmi_query_block(const char *guid_string, u8 instance,
382 struct acpi_buffer *out)
384 struct wmi_block *wblock;
387 return AE_BAD_PARAMETER;
389 if (!find_guid(guid_string, &wblock))
392 return __query_block(wblock, instance, out);
394 EXPORT_SYMBOL_GPL(wmi_query_block);
396 union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance)
398 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
399 struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
401 if (ACPI_FAILURE(__query_block(wblock, instance, &out)))
404 return (union acpi_object *)out.pointer;
406 EXPORT_SYMBOL_GPL(wmidev_block_query);
409 * wmi_set_block - Write to a WMI block
410 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
411 * @instance: Instance index
412 * @in: Buffer containing new values for the data block
414 * Write the contents of the input buffer to an ACPI-WMI data block
416 acpi_status wmi_set_block(const char *guid_string, u8 instance,
417 const struct acpi_buffer *in)
419 struct guid_block *block = NULL;
420 struct wmi_block *wblock = NULL;
422 struct acpi_object_list input;
423 union acpi_object params[2];
424 char method[5] = "WS";
426 if (!guid_string || !in)
429 if (!find_guid(guid_string, &wblock))
432 block = &wblock->gblock;
433 handle = wblock->acpi_device->handle;
435 if (block->instance_count <= instance)
436 return AE_BAD_PARAMETER;
438 /* Check GUID is a data block */
439 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
443 input.pointer = params;
444 params[0].type = ACPI_TYPE_INTEGER;
445 params[0].integer.value = instance;
447 if (block->flags & ACPI_WMI_STRING) {
448 params[1].type = ACPI_TYPE_STRING;
450 params[1].type = ACPI_TYPE_BUFFER;
452 params[1].buffer.length = in->length;
453 params[1].buffer.pointer = in->pointer;
455 strncat(method, block->object_id, 2);
457 return acpi_evaluate_object(handle, method, &input, NULL);
459 EXPORT_SYMBOL_GPL(wmi_set_block);
461 static void wmi_dump_wdg(const struct guid_block *g)
463 pr_info("%pUL:\n", g->guid);
464 if (g->flags & ACPI_WMI_EVENT)
465 pr_info("\tnotify_id: 0x%02X\n", g->notify_id);
467 pr_info("\tobject_id: %2pE\n", g->object_id);
468 pr_info("\tinstance_count: %d\n", g->instance_count);
469 pr_info("\tflags: %#x", g->flags);
471 if (g->flags & ACPI_WMI_EXPENSIVE)
472 pr_cont(" ACPI_WMI_EXPENSIVE");
473 if (g->flags & ACPI_WMI_METHOD)
474 pr_cont(" ACPI_WMI_METHOD");
475 if (g->flags & ACPI_WMI_STRING)
476 pr_cont(" ACPI_WMI_STRING");
477 if (g->flags & ACPI_WMI_EVENT)
478 pr_cont(" ACPI_WMI_EVENT");
484 static void wmi_notify_debug(u32 value, void *context)
486 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
487 union acpi_object *obj;
490 status = wmi_get_event_data(value, &response);
491 if (status != AE_OK) {
492 pr_info("bad event status 0x%x\n", status);
496 obj = (union acpi_object *)response.pointer;
501 pr_info("DEBUG Event ");
503 case ACPI_TYPE_BUFFER:
504 pr_cont("BUFFER_TYPE - length %d\n", obj->buffer.length);
506 case ACPI_TYPE_STRING:
507 pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
509 case ACPI_TYPE_INTEGER:
510 pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
512 case ACPI_TYPE_PACKAGE:
513 pr_cont("PACKAGE_TYPE - %d elements\n", obj->package.count);
516 pr_cont("object type 0x%X\n", obj->type);
522 * wmi_install_notify_handler - Register handler for WMI events
523 * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
524 * @handler: Function to handle notifications
525 * @data: Data to be returned to handler when event is fired
527 * Register a handler for events sent to the ACPI-WMI mapper device.
529 acpi_status wmi_install_notify_handler(const char *guid,
530 wmi_notify_handler handler, void *data)
532 struct wmi_block *block;
533 acpi_status status = AE_NOT_EXIST;
536 if (!guid || !handler)
537 return AE_BAD_PARAMETER;
539 if (guid_parse(guid, &guid_input))
540 return AE_BAD_PARAMETER;
542 list_for_each_entry(block, &wmi_block_list, list) {
543 acpi_status wmi_status;
545 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
546 if (block->handler &&
547 block->handler != wmi_notify_debug)
548 return AE_ALREADY_ACQUIRED;
550 block->handler = handler;
551 block->handler_data = data;
553 wmi_status = wmi_method_enable(block, 1);
554 if ((wmi_status != AE_OK) ||
555 ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
562 EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
565 * wmi_uninstall_notify_handler - Unregister handler for WMI events
566 * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
568 * Unregister handler for events sent to the ACPI-WMI mapper device.
570 acpi_status wmi_remove_notify_handler(const char *guid)
572 struct wmi_block *block;
573 acpi_status status = AE_NOT_EXIST;
577 return AE_BAD_PARAMETER;
579 if (guid_parse(guid, &guid_input))
580 return AE_BAD_PARAMETER;
582 list_for_each_entry(block, &wmi_block_list, list) {
583 acpi_status wmi_status;
585 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
586 if (!block->handler ||
587 block->handler == wmi_notify_debug)
588 return AE_NULL_ENTRY;
591 block->handler = wmi_notify_debug;
594 wmi_status = wmi_method_enable(block, 0);
595 block->handler = NULL;
596 block->handler_data = NULL;
597 if ((wmi_status != AE_OK) ||
598 ((wmi_status == AE_OK) &&
599 (status == AE_NOT_EXIST)))
607 EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
610 * wmi_get_event_data - Get WMI data associated with an event
612 * @event: Event to find
613 * @out: Buffer to hold event data. out->pointer should be freed with kfree()
615 * Returns extra data associated with an event in WMI.
617 acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
619 struct acpi_object_list input;
620 union acpi_object params[1];
621 struct guid_block *gblock;
622 struct wmi_block *wblock;
625 input.pointer = params;
626 params[0].type = ACPI_TYPE_INTEGER;
627 params[0].integer.value = event;
629 list_for_each_entry(wblock, &wmi_block_list, list) {
630 gblock = &wblock->gblock;
632 if ((gblock->flags & ACPI_WMI_EVENT) &&
633 (gblock->notify_id == event))
634 return acpi_evaluate_object(wblock->acpi_device->handle,
635 "_WED", &input, out);
640 EXPORT_SYMBOL_GPL(wmi_get_event_data);
643 * wmi_has_guid - Check if a GUID is available
644 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
646 * Check if a given GUID is defined by _WDG
648 bool wmi_has_guid(const char *guid_string)
650 return find_guid(guid_string, NULL);
652 EXPORT_SYMBOL_GPL(wmi_has_guid);
655 * wmi_get_acpi_device_uid() - Get _UID name of ACPI device that defines GUID
656 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
658 * Find the _UID of ACPI device associated with this WMI GUID.
660 * Return: The ACPI _UID field value or NULL if the WMI GUID was not found
662 char *wmi_get_acpi_device_uid(const char *guid_string)
664 struct wmi_block *wblock = NULL;
666 if (!find_guid(guid_string, &wblock))
669 return acpi_device_uid(wblock->acpi_device);
671 EXPORT_SYMBOL_GPL(wmi_get_acpi_device_uid);
673 static struct wmi_block *dev_to_wblock(struct device *dev)
675 return container_of(dev, struct wmi_block, dev.dev);
678 static struct wmi_device *dev_to_wdev(struct device *dev)
680 return container_of(dev, struct wmi_device, dev);
683 static inline struct wmi_driver *drv_to_wdrv(struct device_driver *drv)
685 return container_of(drv, struct wmi_driver, driver);
691 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
694 struct wmi_block *wblock = dev_to_wblock(dev);
696 return sprintf(buf, "wmi:%pUL\n", wblock->gblock.guid);
698 static DEVICE_ATTR_RO(modalias);
700 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
703 struct wmi_block *wblock = dev_to_wblock(dev);
705 return sprintf(buf, "%pUL\n", wblock->gblock.guid);
707 static DEVICE_ATTR_RO(guid);
709 static ssize_t instance_count_show(struct device *dev,
710 struct device_attribute *attr, char *buf)
712 struct wmi_block *wblock = dev_to_wblock(dev);
714 return sprintf(buf, "%d\n", (int)wblock->gblock.instance_count);
716 static DEVICE_ATTR_RO(instance_count);
718 static ssize_t expensive_show(struct device *dev,
719 struct device_attribute *attr, char *buf)
721 struct wmi_block *wblock = dev_to_wblock(dev);
723 return sprintf(buf, "%d\n",
724 (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
726 static DEVICE_ATTR_RO(expensive);
728 static struct attribute *wmi_attrs[] = {
729 &dev_attr_modalias.attr,
731 &dev_attr_instance_count.attr,
732 &dev_attr_expensive.attr,
735 ATTRIBUTE_GROUPS(wmi);
737 static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
740 struct wmi_block *wblock = dev_to_wblock(dev);
742 return sprintf(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
744 static DEVICE_ATTR_RO(notify_id);
746 static struct attribute *wmi_event_attrs[] = {
747 &dev_attr_notify_id.attr,
750 ATTRIBUTE_GROUPS(wmi_event);
752 static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
755 struct wmi_block *wblock = dev_to_wblock(dev);
757 return sprintf(buf, "%c%c\n", wblock->gblock.object_id[0],
758 wblock->gblock.object_id[1]);
760 static DEVICE_ATTR_RO(object_id);
762 static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
765 struct wmi_device *wdev = dev_to_wdev(dev);
767 return sprintf(buf, "%d\n", (int)wdev->setable);
769 static DEVICE_ATTR_RO(setable);
771 static struct attribute *wmi_data_attrs[] = {
772 &dev_attr_object_id.attr,
773 &dev_attr_setable.attr,
776 ATTRIBUTE_GROUPS(wmi_data);
778 static struct attribute *wmi_method_attrs[] = {
779 &dev_attr_object_id.attr,
782 ATTRIBUTE_GROUPS(wmi_method);
784 static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
786 struct wmi_block *wblock = dev_to_wblock(dev);
788 if (add_uevent_var(env, "MODALIAS=wmi:%pUL", wblock->gblock.guid))
791 if (add_uevent_var(env, "WMI_GUID=%pUL", wblock->gblock.guid))
797 static void wmi_dev_release(struct device *dev)
799 struct wmi_block *wblock = dev_to_wblock(dev);
804 static int wmi_dev_match(struct device *dev, struct device_driver *driver)
806 struct wmi_driver *wmi_driver = drv_to_wdrv(driver);
807 struct wmi_block *wblock = dev_to_wblock(dev);
808 const struct wmi_device_id *id = wmi_driver->id_table;
813 while (*id->guid_string) {
816 if (WARN_ON(guid_parse(id->guid_string, &driver_guid)))
818 if (!memcmp(&driver_guid, wblock->gblock.guid, 16))
826 static int wmi_char_open(struct inode *inode, struct file *filp)
828 const char *driver_name = filp->f_path.dentry->d_iname;
829 struct wmi_block *wblock = NULL;
830 struct wmi_block *next = NULL;
832 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
833 if (!wblock->dev.dev.driver)
835 if (strcmp(driver_name, wblock->dev.dev.driver->name) == 0) {
836 filp->private_data = wblock;
841 if (!filp->private_data)
844 return nonseekable_open(inode, filp);
847 static ssize_t wmi_char_read(struct file *filp, char __user *buffer,
848 size_t length, loff_t *offset)
850 struct wmi_block *wblock = filp->private_data;
852 return simple_read_from_buffer(buffer, length, offset,
853 &wblock->req_buf_size,
854 sizeof(wblock->req_buf_size));
857 static long wmi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
859 struct wmi_ioctl_buffer __user *input =
860 (struct wmi_ioctl_buffer __user *) arg;
861 struct wmi_block *wblock = filp->private_data;
862 struct wmi_ioctl_buffer *buf = NULL;
863 struct wmi_driver *wdriver = NULL;
866 if (_IOC_TYPE(cmd) != WMI_IOC)
869 /* make sure we're not calling a higher instance than exists*/
870 if (_IOC_NR(cmd) >= wblock->gblock.instance_count)
873 mutex_lock(&wblock->char_mutex);
874 buf = wblock->handler_data;
875 if (get_user(buf->length, &input->length)) {
876 dev_dbg(&wblock->dev.dev, "Read length from user failed\n");
880 /* if it's too small, abort */
881 if (buf->length < wblock->req_buf_size) {
882 dev_err(&wblock->dev.dev,
883 "Buffer %lld too small, need at least %lld\n",
884 buf->length, wblock->req_buf_size);
888 /* if it's too big, warn, driver will only use what is needed */
889 if (buf->length > wblock->req_buf_size)
890 dev_warn(&wblock->dev.dev,
891 "Buffer %lld is bigger than required %lld\n",
892 buf->length, wblock->req_buf_size);
894 /* copy the structure from userspace */
895 if (copy_from_user(buf, input, wblock->req_buf_size)) {
896 dev_dbg(&wblock->dev.dev, "Copy %llu from user failed\n",
897 wblock->req_buf_size);
902 /* let the driver do any filtering and do the call */
903 wdriver = drv_to_wdrv(wblock->dev.dev.driver);
904 if (!try_module_get(wdriver->driver.owner)) {
908 ret = wdriver->filter_callback(&wblock->dev, cmd, buf);
909 module_put(wdriver->driver.owner);
913 /* return the result (only up to our internal buffer size) */
914 if (copy_to_user(input, buf, wblock->req_buf_size)) {
915 dev_dbg(&wblock->dev.dev, "Copy %llu to user failed\n",
916 wblock->req_buf_size);
921 mutex_unlock(&wblock->char_mutex);
925 static const struct file_operations wmi_fops = {
926 .owner = THIS_MODULE,
927 .read = wmi_char_read,
928 .open = wmi_char_open,
929 .unlocked_ioctl = wmi_ioctl,
930 .compat_ioctl = compat_ptr_ioctl,
933 static int wmi_dev_probe(struct device *dev)
935 struct wmi_block *wblock = dev_to_wblock(dev);
936 struct wmi_driver *wdriver = drv_to_wdrv(dev->driver);
940 if (ACPI_FAILURE(wmi_method_enable(wblock, 1)))
941 dev_warn(dev, "failed to enable device -- probing anyway\n");
943 if (wdriver->probe) {
944 ret = wdriver->probe(dev_to_wdev(dev),
945 find_guid_context(wblock, wdriver));
950 /* driver wants a character device made */
951 if (wdriver->filter_callback) {
952 /* check that required buffer size declared by driver or MOF */
953 if (!wblock->req_buf_size) {
954 dev_err(&wblock->dev.dev,
955 "Required buffer size not set\n");
960 wblock->handler_data = kmalloc(wblock->req_buf_size,
962 if (!wblock->handler_data) {
967 buf = kasprintf(GFP_KERNEL, "wmi/%s", wdriver->driver.name);
970 goto probe_string_failure;
972 wblock->char_dev.minor = MISC_DYNAMIC_MINOR;
973 wblock->char_dev.name = buf;
974 wblock->char_dev.fops = &wmi_fops;
975 wblock->char_dev.mode = 0444;
976 ret = misc_register(&wblock->char_dev);
978 dev_warn(dev, "failed to register char dev: %d\n", ret);
980 goto probe_misc_failure;
984 set_bit(WMI_PROBED, &wblock->flags);
989 probe_string_failure:
990 kfree(wblock->handler_data);
992 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
993 dev_warn(dev, "failed to disable device\n");
997 static void wmi_dev_remove(struct device *dev)
999 struct wmi_block *wblock = dev_to_wblock(dev);
1000 struct wmi_driver *wdriver = drv_to_wdrv(dev->driver);
1002 clear_bit(WMI_PROBED, &wblock->flags);
1004 if (wdriver->filter_callback) {
1005 misc_deregister(&wblock->char_dev);
1006 kfree(wblock->char_dev.name);
1007 kfree(wblock->handler_data);
1010 if (wdriver->remove)
1011 wdriver->remove(dev_to_wdev(dev));
1013 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
1014 dev_warn(dev, "failed to disable device\n");
1017 static struct class wmi_bus_class = {
1021 static struct bus_type wmi_bus_type = {
1023 .dev_groups = wmi_groups,
1024 .match = wmi_dev_match,
1025 .uevent = wmi_dev_uevent,
1026 .probe = wmi_dev_probe,
1027 .remove = wmi_dev_remove,
1030 static const struct device_type wmi_type_event = {
1032 .groups = wmi_event_groups,
1033 .release = wmi_dev_release,
1036 static const struct device_type wmi_type_method = {
1038 .groups = wmi_method_groups,
1039 .release = wmi_dev_release,
1042 static const struct device_type wmi_type_data = {
1044 .groups = wmi_data_groups,
1045 .release = wmi_dev_release,
1048 static int wmi_create_device(struct device *wmi_bus_dev,
1049 const struct guid_block *gblock,
1050 struct wmi_block *wblock,
1051 struct acpi_device *device)
1053 struct acpi_device_info *info;
1057 if (gblock->flags & ACPI_WMI_EVENT) {
1058 wblock->dev.dev.type = &wmi_type_event;
1062 if (gblock->flags & ACPI_WMI_METHOD) {
1063 wblock->dev.dev.type = &wmi_type_method;
1064 mutex_init(&wblock->char_mutex);
1069 * Data Block Query Control Method (WQxx by convention) is
1070 * required per the WMI documentation. If it is not present,
1071 * we ignore this data block.
1073 strcpy(method, "WQ");
1074 strncat(method, wblock->gblock.object_id, 2);
1075 result = get_subobj_info(device->handle, method, &info);
1078 dev_warn(wmi_bus_dev,
1079 "%s data block query control method not found\n",
1084 wblock->dev.dev.type = &wmi_type_data;
1087 * The Microsoft documentation specifically states:
1089 * Data blocks registered with only a single instance
1090 * can ignore the parameter.
1092 * ACPICA will get mad at us if we call the method with the wrong number
1093 * of arguments, so check what our method expects. (On some Dell
1094 * laptops, WQxx may not be a method at all.)
1096 if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
1097 set_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags);
1101 strcpy(method, "WS");
1102 strncat(method, wblock->gblock.object_id, 2);
1103 result = get_subobj_info(device->handle, method, NULL);
1106 wblock->dev.setable = true;
1109 wblock->dev.dev.bus = &wmi_bus_type;
1110 wblock->dev.dev.parent = wmi_bus_dev;
1112 dev_set_name(&wblock->dev.dev, "%pUL", gblock->guid);
1114 device_initialize(&wblock->dev.dev);
1119 static void wmi_free_devices(struct acpi_device *device)
1121 struct wmi_block *wblock, *next;
1123 /* Delete devices for all the GUIDs */
1124 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1125 if (wblock->acpi_device == device) {
1126 list_del(&wblock->list);
1127 device_unregister(&wblock->dev.dev);
1132 static bool guid_already_parsed(struct acpi_device *device, const u8 *guid)
1134 struct wmi_block *wblock;
1136 list_for_each_entry(wblock, &wmi_block_list, list) {
1137 if (memcmp(wblock->gblock.guid, guid, 16) == 0) {
1139 * Because we historically didn't track the relationship
1140 * between GUIDs and ACPI nodes, we don't know whether
1141 * we need to suppress GUIDs that are unique on a
1142 * given node but duplicated across nodes.
1144 dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
1145 guid, dev_name(&wblock->acpi_device->dev));
1154 * Parse the _WDG method for the GUID data blocks
1156 static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
1158 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
1159 const struct guid_block *gblock;
1160 struct wmi_block *wblock, *next;
1161 union acpi_object *obj;
1166 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
1167 if (ACPI_FAILURE(status))
1170 obj = (union acpi_object *) out.pointer;
1174 if (obj->type != ACPI_TYPE_BUFFER) {
1176 goto out_free_pointer;
1179 gblock = (const struct guid_block *)obj->buffer.pointer;
1180 total = obj->buffer.length / sizeof(struct guid_block);
1182 for (i = 0; i < total; i++) {
1184 wmi_dump_wdg(&gblock[i]);
1187 * Some WMI devices, like those for nVidia hooks, have a
1188 * duplicate GUID. It's not clear what we should do in this
1189 * case yet, so for now, we'll just ignore the duplicate
1190 * for device creation.
1192 if (guid_already_parsed(device, gblock[i].guid))
1195 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
1201 wblock->acpi_device = device;
1202 wblock->gblock = gblock[i];
1204 retval = wmi_create_device(wmi_bus_dev, &gblock[i], wblock, device);
1210 list_add_tail(&wblock->list, &wmi_block_list);
1213 wblock->handler = wmi_notify_debug;
1214 wmi_method_enable(wblock, 1);
1219 * Now that all of the devices are created, add them to the
1220 * device tree and probe subdrivers.
1222 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1223 if (wblock->acpi_device != device)
1226 retval = device_add(&wblock->dev.dev);
1228 dev_err(wmi_bus_dev, "failed to register %pUL\n",
1229 wblock->gblock.guid);
1231 wmi_method_enable(wblock, 0);
1232 list_del(&wblock->list);
1233 put_device(&wblock->dev.dev);
1243 * WMI can have EmbeddedControl access regions. In which case, we just want to
1244 * hand these off to the EC driver.
1247 acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
1248 u32 bits, u64 *value,
1249 void *handler_context, void *region_context)
1251 int result = 0, i = 0;
1254 if ((address > 0xFF) || !value)
1255 return AE_BAD_PARAMETER;
1257 if (function != ACPI_READ && function != ACPI_WRITE)
1258 return AE_BAD_PARAMETER;
1261 return AE_BAD_PARAMETER;
1263 if (function == ACPI_READ) {
1264 result = ec_read(address, &temp);
1265 (*value) |= ((u64)temp) << i;
1267 temp = 0xff & ((*value) >> i);
1268 result = ec_write(address, temp);
1273 return AE_BAD_PARAMETER;
1275 return AE_NOT_FOUND;
1283 static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
1286 struct guid_block *block;
1287 struct wmi_block *wblock;
1288 bool found_it = false;
1290 list_for_each_entry(wblock, &wmi_block_list, list) {
1291 block = &wblock->gblock;
1293 if (wblock->acpi_device->handle == handle &&
1294 (block->flags & ACPI_WMI_EVENT) &&
1295 (block->notify_id == event))
1305 /* If a driver is bound, then notify the driver. */
1306 if (test_bit(WMI_PROBED, &wblock->flags) && wblock->dev.dev.driver) {
1307 struct wmi_driver *driver = drv_to_wdrv(wblock->dev.dev.driver);
1308 struct acpi_object_list input;
1309 union acpi_object params[1];
1310 struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
1314 input.pointer = params;
1315 params[0].type = ACPI_TYPE_INTEGER;
1316 params[0].integer.value = event;
1318 status = acpi_evaluate_object(wblock->acpi_device->handle,
1319 "_WED", &input, &evdata);
1320 if (ACPI_FAILURE(status)) {
1321 dev_warn(&wblock->dev.dev,
1322 "failed to get event data\n");
1327 driver->notify(&wblock->dev,
1328 (union acpi_object *)evdata.pointer);
1330 kfree(evdata.pointer);
1331 } else if (wblock->handler) {
1332 /* Legacy handler */
1333 wblock->handler(event, wblock->handler_data);
1337 pr_info("DEBUG Event GUID: %pUL\n", wblock->gblock.guid);
1339 acpi_bus_generate_netlink_event(
1340 wblock->acpi_device->pnp.device_class,
1341 dev_name(&wblock->dev.dev),
1346 static int acpi_wmi_remove(struct platform_device *device)
1348 struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
1350 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
1351 acpi_wmi_notify_handler);
1352 acpi_remove_address_space_handler(acpi_device->handle,
1353 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
1354 wmi_free_devices(acpi_device);
1355 device_unregister((struct device *)dev_get_drvdata(&device->dev));
1360 static int acpi_wmi_probe(struct platform_device *device)
1362 struct acpi_device *acpi_device;
1363 struct device *wmi_bus_dev;
1367 acpi_device = ACPI_COMPANION(&device->dev);
1369 dev_err(&device->dev, "ACPI companion is missing\n");
1373 status = acpi_install_address_space_handler(acpi_device->handle,
1375 &acpi_wmi_ec_space_handler,
1377 if (ACPI_FAILURE(status)) {
1378 dev_err(&device->dev, "Error installing EC region handler\n");
1382 status = acpi_install_notify_handler(acpi_device->handle,
1384 acpi_wmi_notify_handler,
1386 if (ACPI_FAILURE(status)) {
1387 dev_err(&device->dev, "Error installing notify handler\n");
1389 goto err_remove_ec_handler;
1392 wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
1393 NULL, "wmi_bus-%s", dev_name(&device->dev));
1394 if (IS_ERR(wmi_bus_dev)) {
1395 error = PTR_ERR(wmi_bus_dev);
1396 goto err_remove_notify_handler;
1398 dev_set_drvdata(&device->dev, wmi_bus_dev);
1400 error = parse_wdg(wmi_bus_dev, acpi_device);
1402 pr_err("Failed to parse WDG method\n");
1403 goto err_remove_busdev;
1409 device_unregister(wmi_bus_dev);
1411 err_remove_notify_handler:
1412 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
1413 acpi_wmi_notify_handler);
1415 err_remove_ec_handler:
1416 acpi_remove_address_space_handler(acpi_device->handle,
1418 &acpi_wmi_ec_space_handler);
1423 int __must_check __wmi_driver_register(struct wmi_driver *driver,
1424 struct module *owner)
1426 driver->driver.owner = owner;
1427 driver->driver.bus = &wmi_bus_type;
1429 return driver_register(&driver->driver);
1431 EXPORT_SYMBOL(__wmi_driver_register);
1433 void wmi_driver_unregister(struct wmi_driver *driver)
1435 driver_unregister(&driver->driver);
1437 EXPORT_SYMBOL(wmi_driver_unregister);
1439 static int __init acpi_wmi_init(void)
1446 error = class_register(&wmi_bus_class);
1450 error = bus_register(&wmi_bus_type);
1452 goto err_unreg_class;
1454 error = platform_driver_register(&acpi_wmi_driver);
1456 pr_err("Error loading mapper\n");
1463 bus_unregister(&wmi_bus_type);
1466 class_unregister(&wmi_bus_class);
1471 static void __exit acpi_wmi_exit(void)
1473 platform_driver_unregister(&acpi_wmi_driver);
1474 bus_unregister(&wmi_bus_type);
1475 class_unregister(&wmi_bus_class);
1478 subsys_initcall_sync(acpi_wmi_init);
1479 module_exit(acpi_wmi_exit);