1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ACPI-WMI mapping driver
5 * Copyright (C) 2007-2008 Carlos Corbacho <carlos@strangeworlds.co.uk>
7 * GUID parsing code from ldm.c is:
8 * Copyright (C) 2001,2002 Richard Russon <ldm@flatcap.org>
9 * Copyright (c) 2001-2007 Anton Altaparmakov
10 * Copyright (C) 2001,2002 Jakob Kemi <jakob.kemi@telia.com>
12 * WMI bus infrastructure by Andrew Lutomirski and Darren Hart:
13 * Copyright (C) 2015 Andrew Lutomirski
14 * Copyright (C) 2017 VMware, Inc. All Rights Reserved.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/acpi.h>
20 #include <linux/device.h>
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/miscdevice.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
28 #include <linux/types.h>
29 #include <linux/uaccess.h>
30 #include <linux/uuid.h>
31 #include <linux/wmi.h>
33 #include <uapi/linux/wmi.h>
35 ACPI_MODULE_NAME("wmi");
36 MODULE_AUTHOR("Carlos Corbacho");
37 MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
38 MODULE_LICENSE("GPL");
40 static LIST_HEAD(wmi_block_list);
47 unsigned char notify_id;
48 unsigned char reserved;
56 struct wmi_device dev;
57 struct list_head list;
58 struct guid_block gblock;
59 struct miscdevice char_dev;
60 struct mutex char_mutex;
61 struct acpi_device *acpi_device;
62 wmi_notify_handler handler;
66 bool read_takes_no_args;
71 * If the GUID data block is marked as expensive, we must enable and
72 * explicitily disable data collection.
74 #define ACPI_WMI_EXPENSIVE 0x1
75 #define ACPI_WMI_METHOD 0x2 /* GUID is a method */
76 #define ACPI_WMI_STRING 0x4 /* GUID takes & returns a string */
77 #define ACPI_WMI_EVENT 0x8 /* GUID is an event */
79 static bool debug_event;
80 module_param(debug_event, bool, 0444);
81 MODULE_PARM_DESC(debug_event,
82 "Log WMI Events [0/1]");
84 static bool debug_dump_wdg;
85 module_param(debug_dump_wdg, bool, 0444);
86 MODULE_PARM_DESC(debug_dump_wdg,
87 "Dump available WMI interfaces [0/1]");
89 static int acpi_wmi_remove(struct platform_device *device);
90 static int acpi_wmi_probe(struct platform_device *device);
92 static const struct acpi_device_id wmi_device_ids[] = {
97 MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
99 static struct platform_driver acpi_wmi_driver = {
102 .acpi_match_table = wmi_device_ids,
104 .probe = acpi_wmi_probe,
105 .remove = acpi_wmi_remove,
109 * GUID parsing functions
112 static bool find_guid(const char *guid_string, struct wmi_block **out)
115 struct wmi_block *wblock;
116 struct guid_block *block;
118 if (guid_parse(guid_string, &guid_input))
121 list_for_each_entry(wblock, &wmi_block_list, list) {
122 block = &wblock->gblock;
124 if (memcmp(block->guid, &guid_input, 16) == 0) {
133 static const void *find_guid_context(struct wmi_block *wblock,
134 struct wmi_driver *wdriver)
136 const struct wmi_device_id *id;
139 if (wblock == NULL || wdriver == NULL)
141 if (wdriver->id_table == NULL)
144 id = wdriver->id_table;
145 while (*id->guid_string) {
146 if (guid_parse(id->guid_string, &guid_input))
148 if (!memcmp(wblock->gblock.guid, &guid_input, 16))
155 static int get_subobj_info(acpi_handle handle, const char *pathname,
156 struct acpi_device_info **info)
158 struct acpi_device_info *dummy_info, **info_ptr;
159 acpi_handle subobj_handle;
162 status = acpi_get_handle(handle, (char *)pathname, &subobj_handle);
163 if (status == AE_NOT_FOUND)
165 else if (ACPI_FAILURE(status))
168 info_ptr = info ? info : &dummy_info;
169 status = acpi_get_object_info(subobj_handle, info_ptr);
170 if (ACPI_FAILURE(status))
179 static acpi_status wmi_method_enable(struct wmi_block *wblock, int enable)
181 struct guid_block *block = NULL;
186 block = &wblock->gblock;
187 handle = wblock->acpi_device->handle;
189 snprintf(method, 5, "WE%02X", block->notify_id);
190 status = acpi_execute_simple_method(handle, method, enable);
192 if (status != AE_OK && status != AE_NOT_FOUND)
199 * Exported WMI functions
203 * set_required_buffer_size - Sets the buffer size needed for performing IOCTL
204 * @wdev: A wmi bus device from a driver
205 * @length: Required buffer size
207 * Allocates memory needed for buffer, stores the buffer size in that memory
209 int set_required_buffer_size(struct wmi_device *wdev, u64 length)
211 struct wmi_block *wblock;
213 wblock = container_of(wdev, struct wmi_block, dev);
214 wblock->req_buf_size = length;
218 EXPORT_SYMBOL_GPL(set_required_buffer_size);
221 * wmi_evaluate_method - Evaluate a WMI method
222 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
223 * @instance: Instance index
224 * @method_id: Method ID to call
225 * @in: Buffer containing input for the method call
226 * @out: Empty buffer to return the method results
228 * Call an ACPI-WMI method
230 acpi_status wmi_evaluate_method(const char *guid_string, u8 instance,
231 u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
233 struct wmi_block *wblock = NULL;
235 if (!find_guid(guid_string, &wblock))
237 return wmidev_evaluate_method(&wblock->dev, instance, method_id,
240 EXPORT_SYMBOL_GPL(wmi_evaluate_method);
243 * wmidev_evaluate_method - Evaluate a WMI method
244 * @wdev: A wmi bus device from a driver
245 * @instance: Instance index
246 * @method_id: Method ID to call
247 * @in: Buffer containing input for the method call
248 * @out: Empty buffer to return the method results
250 * Call an ACPI-WMI method
252 acpi_status wmidev_evaluate_method(struct wmi_device *wdev, u8 instance,
253 u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
255 struct guid_block *block = NULL;
256 struct wmi_block *wblock = NULL;
259 struct acpi_object_list input;
260 union acpi_object params[3];
261 char method[5] = "WM";
263 wblock = container_of(wdev, struct wmi_block, dev);
264 block = &wblock->gblock;
265 handle = wblock->acpi_device->handle;
267 if (!(block->flags & ACPI_WMI_METHOD))
270 if (block->instance_count <= instance)
271 return AE_BAD_PARAMETER;
274 input.pointer = params;
275 params[0].type = ACPI_TYPE_INTEGER;
276 params[0].integer.value = instance;
277 params[1].type = ACPI_TYPE_INTEGER;
278 params[1].integer.value = method_id;
283 if (block->flags & ACPI_WMI_STRING) {
284 params[2].type = ACPI_TYPE_STRING;
286 params[2].type = ACPI_TYPE_BUFFER;
288 params[2].buffer.length = in->length;
289 params[2].buffer.pointer = in->pointer;
292 strncat(method, block->object_id, 2);
294 status = acpi_evaluate_object(handle, method, &input, out);
298 EXPORT_SYMBOL_GPL(wmidev_evaluate_method);
300 static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
301 struct acpi_buffer *out)
303 struct guid_block *block = NULL;
305 acpi_status status, wc_status = AE_ERROR;
306 struct acpi_object_list input;
307 union acpi_object wq_params[1];
309 char wc_method[5] = "WC";
312 return AE_BAD_PARAMETER;
314 block = &wblock->gblock;
315 handle = wblock->acpi_device->handle;
317 if (block->instance_count <= instance)
318 return AE_BAD_PARAMETER;
320 /* Check GUID is a data block */
321 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
325 input.pointer = wq_params;
326 wq_params[0].type = ACPI_TYPE_INTEGER;
327 wq_params[0].integer.value = instance;
329 if (instance == 0 && wblock->read_takes_no_args)
333 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
336 if (block->flags & ACPI_WMI_EXPENSIVE) {
337 strncat(wc_method, block->object_id, 2);
340 * Some GUIDs break the specification by declaring themselves
341 * expensive, but have no corresponding WCxx method. So we
342 * should not fail if this happens.
344 wc_status = acpi_execute_simple_method(handle, wc_method, 1);
347 strcpy(method, "WQ");
348 strncat(method, block->object_id, 2);
350 status = acpi_evaluate_object(handle, method, &input, out);
353 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
354 * the WQxx method failed - we should disable collection anyway.
356 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
358 * Ignore whether this WCxx call succeeds or not since
359 * the previously executed WQxx method call might have
360 * succeeded, and returning the failing status code
361 * of this call would throw away the result of the WQxx
362 * call, potentially leaking memory.
364 acpi_execute_simple_method(handle, wc_method, 0);
371 * wmi_query_block - Return contents of a WMI block (deprecated)
372 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
373 * @instance: Instance index
374 * @out: Empty buffer to return the contents of the data block to
376 * Return the contents of an ACPI-WMI data block to a buffer
378 acpi_status wmi_query_block(const char *guid_string, u8 instance,
379 struct acpi_buffer *out)
381 struct wmi_block *wblock;
384 return AE_BAD_PARAMETER;
386 if (!find_guid(guid_string, &wblock))
389 return __query_block(wblock, instance, out);
391 EXPORT_SYMBOL_GPL(wmi_query_block);
393 union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance)
395 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
396 struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
398 if (ACPI_FAILURE(__query_block(wblock, instance, &out)))
401 return (union acpi_object *)out.pointer;
403 EXPORT_SYMBOL_GPL(wmidev_block_query);
406 * wmi_set_block - Write to a WMI block
407 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
408 * @instance: Instance index
409 * @in: Buffer containing new values for the data block
411 * Write the contents of the input buffer to an ACPI-WMI data block
413 acpi_status wmi_set_block(const char *guid_string, u8 instance,
414 const struct acpi_buffer *in)
416 struct guid_block *block = NULL;
417 struct wmi_block *wblock = NULL;
419 struct acpi_object_list input;
420 union acpi_object params[2];
421 char method[5] = "WS";
423 if (!guid_string || !in)
426 if (!find_guid(guid_string, &wblock))
429 block = &wblock->gblock;
430 handle = wblock->acpi_device->handle;
432 if (block->instance_count <= instance)
433 return AE_BAD_PARAMETER;
435 /* Check GUID is a data block */
436 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
440 input.pointer = params;
441 params[0].type = ACPI_TYPE_INTEGER;
442 params[0].integer.value = instance;
444 if (block->flags & ACPI_WMI_STRING) {
445 params[1].type = ACPI_TYPE_STRING;
447 params[1].type = ACPI_TYPE_BUFFER;
449 params[1].buffer.length = in->length;
450 params[1].buffer.pointer = in->pointer;
452 strncat(method, block->object_id, 2);
454 return acpi_evaluate_object(handle, method, &input, NULL);
456 EXPORT_SYMBOL_GPL(wmi_set_block);
458 static void wmi_dump_wdg(const struct guid_block *g)
460 pr_info("%pUL:\n", g->guid);
461 if (g->flags & ACPI_WMI_EVENT)
462 pr_info("\tnotify_id: 0x%02X\n", g->notify_id);
464 pr_info("\tobject_id: %2pE\n", g->object_id);
465 pr_info("\tinstance_count: %d\n", g->instance_count);
466 pr_info("\tflags: %#x", g->flags);
468 if (g->flags & ACPI_WMI_EXPENSIVE)
469 pr_cont(" ACPI_WMI_EXPENSIVE");
470 if (g->flags & ACPI_WMI_METHOD)
471 pr_cont(" ACPI_WMI_METHOD");
472 if (g->flags & ACPI_WMI_STRING)
473 pr_cont(" ACPI_WMI_STRING");
474 if (g->flags & ACPI_WMI_EVENT)
475 pr_cont(" ACPI_WMI_EVENT");
481 static void wmi_notify_debug(u32 value, void *context)
483 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
484 union acpi_object *obj;
487 status = wmi_get_event_data(value, &response);
488 if (status != AE_OK) {
489 pr_info("bad event status 0x%x\n", status);
493 obj = (union acpi_object *)response.pointer;
498 pr_info("DEBUG Event ");
500 case ACPI_TYPE_BUFFER:
501 pr_cont("BUFFER_TYPE - length %d\n", obj->buffer.length);
503 case ACPI_TYPE_STRING:
504 pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
506 case ACPI_TYPE_INTEGER:
507 pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
509 case ACPI_TYPE_PACKAGE:
510 pr_cont("PACKAGE_TYPE - %d elements\n", obj->package.count);
513 pr_cont("object type 0x%X\n", obj->type);
519 * wmi_install_notify_handler - Register handler for WMI events
520 * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
521 * @handler: Function to handle notifications
522 * @data: Data to be returned to handler when event is fired
524 * Register a handler for events sent to the ACPI-WMI mapper device.
526 acpi_status wmi_install_notify_handler(const char *guid,
527 wmi_notify_handler handler, void *data)
529 struct wmi_block *block;
530 acpi_status status = AE_NOT_EXIST;
533 if (!guid || !handler)
534 return AE_BAD_PARAMETER;
536 if (guid_parse(guid, &guid_input))
537 return AE_BAD_PARAMETER;
539 list_for_each_entry(block, &wmi_block_list, list) {
540 acpi_status wmi_status;
542 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
543 if (block->handler &&
544 block->handler != wmi_notify_debug)
545 return AE_ALREADY_ACQUIRED;
547 block->handler = handler;
548 block->handler_data = data;
550 wmi_status = wmi_method_enable(block, 1);
551 if ((wmi_status != AE_OK) ||
552 ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
559 EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
562 * wmi_uninstall_notify_handler - Unregister handler for WMI events
563 * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
565 * Unregister handler for events sent to the ACPI-WMI mapper device.
567 acpi_status wmi_remove_notify_handler(const char *guid)
569 struct wmi_block *block;
570 acpi_status status = AE_NOT_EXIST;
574 return AE_BAD_PARAMETER;
576 if (guid_parse(guid, &guid_input))
577 return AE_BAD_PARAMETER;
579 list_for_each_entry(block, &wmi_block_list, list) {
580 acpi_status wmi_status;
582 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
583 if (!block->handler ||
584 block->handler == wmi_notify_debug)
585 return AE_NULL_ENTRY;
588 block->handler = wmi_notify_debug;
591 wmi_status = wmi_method_enable(block, 0);
592 block->handler = NULL;
593 block->handler_data = NULL;
594 if ((wmi_status != AE_OK) ||
595 ((wmi_status == AE_OK) &&
596 (status == AE_NOT_EXIST)))
604 EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
607 * wmi_get_event_data - Get WMI data associated with an event
609 * @event: Event to find
610 * @out: Buffer to hold event data. out->pointer should be freed with kfree()
612 * Returns extra data associated with an event in WMI.
614 acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
616 struct acpi_object_list input;
617 union acpi_object params[1];
618 struct guid_block *gblock;
619 struct wmi_block *wblock;
622 input.pointer = params;
623 params[0].type = ACPI_TYPE_INTEGER;
624 params[0].integer.value = event;
626 list_for_each_entry(wblock, &wmi_block_list, list) {
627 gblock = &wblock->gblock;
629 if ((gblock->flags & ACPI_WMI_EVENT) &&
630 (gblock->notify_id == event))
631 return acpi_evaluate_object(wblock->acpi_device->handle,
632 "_WED", &input, out);
637 EXPORT_SYMBOL_GPL(wmi_get_event_data);
640 * wmi_has_guid - Check if a GUID is available
641 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
643 * Check if a given GUID is defined by _WDG
645 bool wmi_has_guid(const char *guid_string)
647 return find_guid(guid_string, NULL);
649 EXPORT_SYMBOL_GPL(wmi_has_guid);
652 * wmi_get_acpi_device_uid() - Get _UID name of ACPI device that defines GUID
653 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
655 * Find the _UID of ACPI device associated with this WMI GUID.
657 * Return: The ACPI _UID field value or NULL if the WMI GUID was not found
659 char *wmi_get_acpi_device_uid(const char *guid_string)
661 struct wmi_block *wblock = NULL;
663 if (!find_guid(guid_string, &wblock))
666 return acpi_device_uid(wblock->acpi_device);
668 EXPORT_SYMBOL_GPL(wmi_get_acpi_device_uid);
670 static struct wmi_block *dev_to_wblock(struct device *dev)
672 return container_of(dev, struct wmi_block, dev.dev);
675 static struct wmi_device *dev_to_wdev(struct device *dev)
677 return container_of(dev, struct wmi_device, dev);
683 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
686 struct wmi_block *wblock = dev_to_wblock(dev);
688 return sprintf(buf, "wmi:%pUL\n", wblock->gblock.guid);
690 static DEVICE_ATTR_RO(modalias);
692 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
695 struct wmi_block *wblock = dev_to_wblock(dev);
697 return sprintf(buf, "%pUL\n", wblock->gblock.guid);
699 static DEVICE_ATTR_RO(guid);
701 static ssize_t instance_count_show(struct device *dev,
702 struct device_attribute *attr, char *buf)
704 struct wmi_block *wblock = dev_to_wblock(dev);
706 return sprintf(buf, "%d\n", (int)wblock->gblock.instance_count);
708 static DEVICE_ATTR_RO(instance_count);
710 static ssize_t expensive_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",
716 (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
718 static DEVICE_ATTR_RO(expensive);
720 static struct attribute *wmi_attrs[] = {
721 &dev_attr_modalias.attr,
723 &dev_attr_instance_count.attr,
724 &dev_attr_expensive.attr,
727 ATTRIBUTE_GROUPS(wmi);
729 static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
732 struct wmi_block *wblock = dev_to_wblock(dev);
734 return sprintf(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
736 static DEVICE_ATTR_RO(notify_id);
738 static struct attribute *wmi_event_attrs[] = {
739 &dev_attr_notify_id.attr,
742 ATTRIBUTE_GROUPS(wmi_event);
744 static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
747 struct wmi_block *wblock = dev_to_wblock(dev);
749 return sprintf(buf, "%c%c\n", wblock->gblock.object_id[0],
750 wblock->gblock.object_id[1]);
752 static DEVICE_ATTR_RO(object_id);
754 static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
757 struct wmi_device *wdev = dev_to_wdev(dev);
759 return sprintf(buf, "%d\n", (int)wdev->setable);
761 static DEVICE_ATTR_RO(setable);
763 static struct attribute *wmi_data_attrs[] = {
764 &dev_attr_object_id.attr,
765 &dev_attr_setable.attr,
768 ATTRIBUTE_GROUPS(wmi_data);
770 static struct attribute *wmi_method_attrs[] = {
771 &dev_attr_object_id.attr,
774 ATTRIBUTE_GROUPS(wmi_method);
776 static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
778 struct wmi_block *wblock = dev_to_wblock(dev);
780 if (add_uevent_var(env, "MODALIAS=wmi:%pUL", wblock->gblock.guid))
783 if (add_uevent_var(env, "WMI_GUID=%pUL", wblock->gblock.guid))
789 static void wmi_dev_release(struct device *dev)
791 struct wmi_block *wblock = dev_to_wblock(dev);
796 static int wmi_dev_match(struct device *dev, struct device_driver *driver)
798 struct wmi_driver *wmi_driver =
799 container_of(driver, struct wmi_driver, driver);
800 struct wmi_block *wblock = dev_to_wblock(dev);
801 const struct wmi_device_id *id = wmi_driver->id_table;
806 while (*id->guid_string) {
809 if (WARN_ON(guid_parse(id->guid_string, &driver_guid)))
811 if (!memcmp(&driver_guid, wblock->gblock.guid, 16))
819 static int wmi_char_open(struct inode *inode, struct file *filp)
821 const char *driver_name = filp->f_path.dentry->d_iname;
822 struct wmi_block *wblock = NULL;
823 struct wmi_block *next = NULL;
825 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
826 if (!wblock->dev.dev.driver)
828 if (strcmp(driver_name, wblock->dev.dev.driver->name) == 0) {
829 filp->private_data = wblock;
834 if (!filp->private_data)
837 return nonseekable_open(inode, filp);
840 static ssize_t wmi_char_read(struct file *filp, char __user *buffer,
841 size_t length, loff_t *offset)
843 struct wmi_block *wblock = filp->private_data;
845 return simple_read_from_buffer(buffer, length, offset,
846 &wblock->req_buf_size,
847 sizeof(wblock->req_buf_size));
850 static long wmi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
852 struct wmi_ioctl_buffer __user *input =
853 (struct wmi_ioctl_buffer __user *) arg;
854 struct wmi_block *wblock = filp->private_data;
855 struct wmi_ioctl_buffer *buf = NULL;
856 struct wmi_driver *wdriver = NULL;
859 if (_IOC_TYPE(cmd) != WMI_IOC)
862 /* make sure we're not calling a higher instance than exists*/
863 if (_IOC_NR(cmd) >= wblock->gblock.instance_count)
866 mutex_lock(&wblock->char_mutex);
867 buf = wblock->handler_data;
868 if (get_user(buf->length, &input->length)) {
869 dev_dbg(&wblock->dev.dev, "Read length from user failed\n");
873 /* if it's too small, abort */
874 if (buf->length < wblock->req_buf_size) {
875 dev_err(&wblock->dev.dev,
876 "Buffer %lld too small, need at least %lld\n",
877 buf->length, wblock->req_buf_size);
881 /* if it's too big, warn, driver will only use what is needed */
882 if (buf->length > wblock->req_buf_size)
883 dev_warn(&wblock->dev.dev,
884 "Buffer %lld is bigger than required %lld\n",
885 buf->length, wblock->req_buf_size);
887 /* copy the structure from userspace */
888 if (copy_from_user(buf, input, wblock->req_buf_size)) {
889 dev_dbg(&wblock->dev.dev, "Copy %llu from user failed\n",
890 wblock->req_buf_size);
895 /* let the driver do any filtering and do the call */
896 wdriver = container_of(wblock->dev.dev.driver,
897 struct wmi_driver, driver);
898 if (!try_module_get(wdriver->driver.owner)) {
902 ret = wdriver->filter_callback(&wblock->dev, cmd, buf);
903 module_put(wdriver->driver.owner);
907 /* return the result (only up to our internal buffer size) */
908 if (copy_to_user(input, buf, wblock->req_buf_size)) {
909 dev_dbg(&wblock->dev.dev, "Copy %llu to user failed\n",
910 wblock->req_buf_size);
915 mutex_unlock(&wblock->char_mutex);
919 static const struct file_operations wmi_fops = {
920 .owner = THIS_MODULE,
921 .read = wmi_char_read,
922 .open = wmi_char_open,
923 .unlocked_ioctl = wmi_ioctl,
924 .compat_ioctl = compat_ptr_ioctl,
927 static int wmi_dev_probe(struct device *dev)
929 struct wmi_block *wblock = dev_to_wblock(dev);
930 struct wmi_driver *wdriver =
931 container_of(dev->driver, struct wmi_driver, driver);
935 if (ACPI_FAILURE(wmi_method_enable(wblock, 1)))
936 dev_warn(dev, "failed to enable device -- probing anyway\n");
938 if (wdriver->probe) {
939 ret = wdriver->probe(dev_to_wdev(dev),
940 find_guid_context(wblock, wdriver));
945 /* driver wants a character device made */
946 if (wdriver->filter_callback) {
947 /* check that required buffer size declared by driver or MOF */
948 if (!wblock->req_buf_size) {
949 dev_err(&wblock->dev.dev,
950 "Required buffer size not set\n");
955 wblock->handler_data = kmalloc(wblock->req_buf_size,
957 if (!wblock->handler_data) {
962 buf = kasprintf(GFP_KERNEL, "wmi/%s", wdriver->driver.name);
965 goto probe_string_failure;
967 wblock->char_dev.minor = MISC_DYNAMIC_MINOR;
968 wblock->char_dev.name = buf;
969 wblock->char_dev.fops = &wmi_fops;
970 wblock->char_dev.mode = 0444;
971 ret = misc_register(&wblock->char_dev);
973 dev_warn(dev, "failed to register char dev: %d\n", ret);
975 goto probe_misc_failure;
983 probe_string_failure:
984 kfree(wblock->handler_data);
986 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
987 dev_warn(dev, "failed to disable device\n");
991 static int wmi_dev_remove(struct device *dev)
993 struct wmi_block *wblock = dev_to_wblock(dev);
994 struct wmi_driver *wdriver =
995 container_of(dev->driver, struct wmi_driver, driver);
998 if (wdriver->filter_callback) {
999 misc_deregister(&wblock->char_dev);
1000 kfree(wblock->char_dev.name);
1001 kfree(wblock->handler_data);
1004 if (wdriver->remove)
1005 ret = wdriver->remove(dev_to_wdev(dev));
1007 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
1008 dev_warn(dev, "failed to disable device\n");
1013 static struct class wmi_bus_class = {
1017 static struct bus_type wmi_bus_type = {
1019 .dev_groups = wmi_groups,
1020 .match = wmi_dev_match,
1021 .uevent = wmi_dev_uevent,
1022 .probe = wmi_dev_probe,
1023 .remove = wmi_dev_remove,
1026 static const struct device_type wmi_type_event = {
1028 .groups = wmi_event_groups,
1029 .release = wmi_dev_release,
1032 static const struct device_type wmi_type_method = {
1034 .groups = wmi_method_groups,
1035 .release = wmi_dev_release,
1038 static const struct device_type wmi_type_data = {
1040 .groups = wmi_data_groups,
1041 .release = wmi_dev_release,
1044 static int wmi_create_device(struct device *wmi_bus_dev,
1045 const struct guid_block *gblock,
1046 struct wmi_block *wblock,
1047 struct acpi_device *device)
1049 struct acpi_device_info *info;
1053 if (gblock->flags & ACPI_WMI_EVENT) {
1054 wblock->dev.dev.type = &wmi_type_event;
1058 if (gblock->flags & ACPI_WMI_METHOD) {
1059 wblock->dev.dev.type = &wmi_type_method;
1060 mutex_init(&wblock->char_mutex);
1065 * Data Block Query Control Method (WQxx by convention) is
1066 * required per the WMI documentation. If it is not present,
1067 * we ignore this data block.
1069 strcpy(method, "WQ");
1070 strncat(method, wblock->gblock.object_id, 2);
1071 result = get_subobj_info(device->handle, method, &info);
1074 dev_warn(wmi_bus_dev,
1075 "%s data block query control method not found\n",
1080 wblock->dev.dev.type = &wmi_type_data;
1083 * The Microsoft documentation specifically states:
1085 * Data blocks registered with only a single instance
1086 * can ignore the parameter.
1088 * ACPICA will get mad at us if we call the method with the wrong number
1089 * of arguments, so check what our method expects. (On some Dell
1090 * laptops, WQxx may not be a method at all.)
1092 if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
1093 wblock->read_takes_no_args = true;
1097 strcpy(method, "WS");
1098 strncat(method, wblock->gblock.object_id, 2);
1099 result = get_subobj_info(device->handle, method, NULL);
1102 wblock->dev.setable = true;
1105 wblock->dev.dev.bus = &wmi_bus_type;
1106 wblock->dev.dev.parent = wmi_bus_dev;
1108 dev_set_name(&wblock->dev.dev, "%pUL", gblock->guid);
1110 device_initialize(&wblock->dev.dev);
1115 static void wmi_free_devices(struct acpi_device *device)
1117 struct wmi_block *wblock, *next;
1119 /* Delete devices for all the GUIDs */
1120 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1121 if (wblock->acpi_device == device) {
1122 list_del(&wblock->list);
1123 device_unregister(&wblock->dev.dev);
1128 static bool guid_already_parsed(struct acpi_device *device, const u8 *guid)
1130 struct wmi_block *wblock;
1132 list_for_each_entry(wblock, &wmi_block_list, list) {
1133 if (memcmp(wblock->gblock.guid, guid, 16) == 0) {
1135 * Because we historically didn't track the relationship
1136 * between GUIDs and ACPI nodes, we don't know whether
1137 * we need to suppress GUIDs that are unique on a
1138 * given node but duplicated across nodes.
1140 dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
1141 guid, dev_name(&wblock->acpi_device->dev));
1150 * Parse the _WDG method for the GUID data blocks
1152 static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
1154 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
1155 const struct guid_block *gblock;
1156 struct wmi_block *wblock, *next;
1157 union acpi_object *obj;
1162 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
1163 if (ACPI_FAILURE(status))
1166 obj = (union acpi_object *) out.pointer;
1170 if (obj->type != ACPI_TYPE_BUFFER) {
1172 goto out_free_pointer;
1175 gblock = (const struct guid_block *)obj->buffer.pointer;
1176 total = obj->buffer.length / sizeof(struct guid_block);
1178 for (i = 0; i < total; i++) {
1180 wmi_dump_wdg(&gblock[i]);
1183 * Some WMI devices, like those for nVidia hooks, have a
1184 * duplicate GUID. It's not clear what we should do in this
1185 * case yet, so for now, we'll just ignore the duplicate
1186 * for device creation.
1188 if (guid_already_parsed(device, gblock[i].guid))
1191 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
1197 wblock->acpi_device = device;
1198 wblock->gblock = gblock[i];
1200 retval = wmi_create_device(wmi_bus_dev, &gblock[i], wblock, device);
1206 list_add_tail(&wblock->list, &wmi_block_list);
1209 wblock->handler = wmi_notify_debug;
1210 wmi_method_enable(wblock, 1);
1215 * Now that all of the devices are created, add them to the
1216 * device tree and probe subdrivers.
1218 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1219 if (wblock->acpi_device != device)
1222 retval = device_add(&wblock->dev.dev);
1224 dev_err(wmi_bus_dev, "failed to register %pUL\n",
1225 wblock->gblock.guid);
1227 wmi_method_enable(wblock, 0);
1228 list_del(&wblock->list);
1229 put_device(&wblock->dev.dev);
1239 * WMI can have EmbeddedControl access regions. In which case, we just want to
1240 * hand these off to the EC driver.
1243 acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
1244 u32 bits, u64 *value,
1245 void *handler_context, void *region_context)
1247 int result = 0, i = 0;
1250 if ((address > 0xFF) || !value)
1251 return AE_BAD_PARAMETER;
1253 if (function != ACPI_READ && function != ACPI_WRITE)
1254 return AE_BAD_PARAMETER;
1257 return AE_BAD_PARAMETER;
1259 if (function == ACPI_READ) {
1260 result = ec_read(address, &temp);
1261 (*value) |= ((u64)temp) << i;
1263 temp = 0xff & ((*value) >> i);
1264 result = ec_write(address, temp);
1269 return AE_BAD_PARAMETER;
1272 return AE_NOT_FOUND;
1282 static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
1285 struct guid_block *block;
1286 struct wmi_block *wblock;
1287 bool found_it = false;
1289 list_for_each_entry(wblock, &wmi_block_list, list) {
1290 block = &wblock->gblock;
1292 if (wblock->acpi_device->handle == handle &&
1293 (block->flags & ACPI_WMI_EVENT) &&
1294 (block->notify_id == event))
1304 /* If a driver is bound, then notify the driver. */
1305 if (wblock->dev.dev.driver) {
1306 struct wmi_driver *driver;
1307 struct acpi_object_list input;
1308 union acpi_object params[1];
1309 struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
1312 driver = container_of(wblock->dev.dev.driver,
1313 struct wmi_driver, driver);
1316 input.pointer = params;
1317 params[0].type = ACPI_TYPE_INTEGER;
1318 params[0].integer.value = event;
1320 status = acpi_evaluate_object(wblock->acpi_device->handle,
1321 "_WED", &input, &evdata);
1322 if (ACPI_FAILURE(status)) {
1323 dev_warn(&wblock->dev.dev,
1324 "failed to get event data\n");
1329 driver->notify(&wblock->dev,
1330 (union acpi_object *)evdata.pointer);
1332 kfree(evdata.pointer);
1333 } else if (wblock->handler) {
1334 /* Legacy handler */
1335 wblock->handler(event, wblock->handler_data);
1339 pr_info("DEBUG Event GUID: %pUL\n", wblock->gblock.guid);
1341 acpi_bus_generate_netlink_event(
1342 wblock->acpi_device->pnp.device_class,
1343 dev_name(&wblock->dev.dev),
1348 static int acpi_wmi_remove(struct platform_device *device)
1350 struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
1352 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
1353 acpi_wmi_notify_handler);
1354 acpi_remove_address_space_handler(acpi_device->handle,
1355 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
1356 wmi_free_devices(acpi_device);
1357 device_destroy(&wmi_bus_class, MKDEV(0, 0));
1362 static int acpi_wmi_probe(struct platform_device *device)
1364 struct acpi_device *acpi_device;
1365 struct device *wmi_bus_dev;
1369 acpi_device = ACPI_COMPANION(&device->dev);
1371 dev_err(&device->dev, "ACPI companion is missing\n");
1375 status = acpi_install_address_space_handler(acpi_device->handle,
1377 &acpi_wmi_ec_space_handler,
1379 if (ACPI_FAILURE(status)) {
1380 dev_err(&device->dev, "Error installing EC region handler\n");
1384 status = acpi_install_notify_handler(acpi_device->handle,
1386 acpi_wmi_notify_handler,
1388 if (ACPI_FAILURE(status)) {
1389 dev_err(&device->dev, "Error installing notify handler\n");
1391 goto err_remove_ec_handler;
1394 wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
1395 NULL, "wmi_bus-%s", dev_name(&device->dev));
1396 if (IS_ERR(wmi_bus_dev)) {
1397 error = PTR_ERR(wmi_bus_dev);
1398 goto err_remove_notify_handler;
1400 dev_set_drvdata(&device->dev, wmi_bus_dev);
1402 error = parse_wdg(wmi_bus_dev, acpi_device);
1404 pr_err("Failed to parse WDG method\n");
1405 goto err_remove_busdev;
1411 device_destroy(&wmi_bus_class, MKDEV(0, 0));
1413 err_remove_notify_handler:
1414 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
1415 acpi_wmi_notify_handler);
1417 err_remove_ec_handler:
1418 acpi_remove_address_space_handler(acpi_device->handle,
1420 &acpi_wmi_ec_space_handler);
1425 int __must_check __wmi_driver_register(struct wmi_driver *driver,
1426 struct module *owner)
1428 driver->driver.owner = owner;
1429 driver->driver.bus = &wmi_bus_type;
1431 return driver_register(&driver->driver);
1433 EXPORT_SYMBOL(__wmi_driver_register);
1435 void wmi_driver_unregister(struct wmi_driver *driver)
1437 driver_unregister(&driver->driver);
1439 EXPORT_SYMBOL(wmi_driver_unregister);
1441 static int __init acpi_wmi_init(void)
1448 error = class_register(&wmi_bus_class);
1452 error = bus_register(&wmi_bus_type);
1454 goto err_unreg_class;
1456 error = platform_driver_register(&acpi_wmi_driver);
1458 pr_err("Error loading mapper\n");
1465 bus_unregister(&wmi_bus_type);
1468 class_unregister(&wmi_bus_class);
1473 static void __exit acpi_wmi_exit(void)
1475 platform_driver_unregister(&acpi_wmi_driver);
1476 bus_unregister(&wmi_bus_type);
1477 class_unregister(&wmi_bus_class);
1480 subsys_initcall_sync(acpi_wmi_init);
1481 module_exit(acpi_wmi_exit);