1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ACPI-WMI mapping driver
7 * GUID parsing code from ldm.c is:
9 * Copyright (c) 2001-2007 Anton Altaparmakov
12 * WMI bus infrastructure by Andrew Lutomirski and Darren Hart:
13 * Copyright (C) 2015 Andrew Lutomirski
14 * Copyright (C) 2017 VMware, Inc. All Rights Reserved.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/acpi.h>
20 #include <linux/bits.h>
21 #include <linux/build_bug.h>
22 #include <linux/device.h>
23 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/list.h>
26 #include <linux/miscdevice.h>
27 #include <linux/module.h>
28 #include <linux/platform_device.h>
29 #include <linux/slab.h>
30 #include <linux/sysfs.h>
31 #include <linux/types.h>
32 #include <linux/uaccess.h>
33 #include <linux/uuid.h>
34 #include <linux/wmi.h>
36 #include <uapi/linux/wmi.h>
38 MODULE_AUTHOR("Carlos Corbacho");
39 MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
40 MODULE_LICENSE("GPL");
42 static LIST_HEAD(wmi_block_list);
49 unsigned char notify_id;
50 unsigned char reserved;
56 static_assert(sizeof(typeof_member(struct guid_block, guid)) == 16);
57 static_assert(sizeof(struct guid_block) == 20);
58 static_assert(__alignof__(struct guid_block) == 1);
60 enum { /* wmi_block flags */
61 WMI_READ_TAKES_NO_ARGS,
66 struct wmi_device dev;
67 struct list_head list;
68 struct guid_block gblock;
69 struct miscdevice char_dev;
70 struct mutex char_mutex;
71 struct acpi_device *acpi_device;
72 wmi_notify_handler handler;
80 * If the GUID data block is marked as expensive, we must enable and
81 * explicitily disable data collection.
83 #define ACPI_WMI_EXPENSIVE BIT(0)
84 #define ACPI_WMI_METHOD BIT(1) /* GUID is a method */
85 #define ACPI_WMI_STRING BIT(2) /* GUID takes & returns a string */
86 #define ACPI_WMI_EVENT BIT(3) /* GUID is an event */
88 static bool debug_event;
89 module_param(debug_event, bool, 0444);
90 MODULE_PARM_DESC(debug_event,
91 "Log WMI Events [0/1]");
93 static bool debug_dump_wdg;
94 module_param(debug_dump_wdg, bool, 0444);
95 MODULE_PARM_DESC(debug_dump_wdg,
96 "Dump available WMI interfaces [0/1]");
98 static const struct acpi_device_id wmi_device_ids[] = {
103 MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
105 /* allow duplicate GUIDs as these device drivers use struct wmi_driver */
106 static const char * const allow_duplicates[] = {
107 "05901221-D566-11D1-B2F0-00A0C9062910", /* wmi-bmof */
112 * GUID parsing functions
115 static acpi_status find_guid(const char *guid_string, struct wmi_block **out)
118 struct wmi_block *wblock;
121 return AE_BAD_PARAMETER;
123 if (guid_parse(guid_string, &guid_input))
124 return AE_BAD_PARAMETER;
126 list_for_each_entry(wblock, &wmi_block_list, list) {
127 if (guid_equal(&wblock->gblock.guid, &guid_input)) {
138 static const void *find_guid_context(struct wmi_block *wblock,
139 struct wmi_driver *wdriver)
141 const struct wmi_device_id *id;
143 id = wdriver->id_table;
147 while (*id->guid_string) {
150 if (guid_parse(id->guid_string, &guid_input))
152 if (guid_equal(&wblock->gblock.guid, &guid_input))
159 static int get_subobj_info(acpi_handle handle, const char *pathname,
160 struct acpi_device_info **info)
162 struct acpi_device_info *dummy_info, **info_ptr;
163 acpi_handle subobj_handle;
166 status = acpi_get_handle(handle, (char *)pathname, &subobj_handle);
167 if (status == AE_NOT_FOUND)
169 else if (ACPI_FAILURE(status))
172 info_ptr = info ? info : &dummy_info;
173 status = acpi_get_object_info(subobj_handle, info_ptr);
174 if (ACPI_FAILURE(status))
183 static acpi_status wmi_method_enable(struct wmi_block *wblock, bool enable)
185 struct guid_block *block;
190 block = &wblock->gblock;
191 handle = wblock->acpi_device->handle;
193 snprintf(method, 5, "WE%02X", block->notify_id);
194 status = acpi_execute_simple_method(handle, method, enable);
195 if (status == AE_NOT_FOUND)
201 #define WMI_ACPI_METHOD_NAME_SIZE 5
203 static inline void get_acpi_method_name(const struct wmi_block *wblock,
205 char buffer[static WMI_ACPI_METHOD_NAME_SIZE])
207 static_assert(ARRAY_SIZE(wblock->gblock.object_id) == 2);
208 static_assert(WMI_ACPI_METHOD_NAME_SIZE >= 5);
212 buffer[2] = wblock->gblock.object_id[0];
213 buffer[3] = wblock->gblock.object_id[1];
217 static inline acpi_object_type get_param_acpi_type(const struct wmi_block *wblock)
219 if (wblock->gblock.flags & ACPI_WMI_STRING)
220 return ACPI_TYPE_STRING;
222 return ACPI_TYPE_BUFFER;
225 static acpi_status get_event_data(const struct wmi_block *wblock, struct acpi_buffer *out)
227 union acpi_object param = {
229 .type = ACPI_TYPE_INTEGER,
230 .value = wblock->gblock.notify_id,
233 struct acpi_object_list input = {
238 return acpi_evaluate_object(wblock->acpi_device->handle, "_WED", &input, out);
242 * Exported WMI functions
246 * set_required_buffer_size - Sets the buffer size needed for performing IOCTL
247 * @wdev: A wmi bus device from a driver
248 * @length: Required buffer size
250 * Allocates memory needed for buffer, stores the buffer size in that memory
252 int set_required_buffer_size(struct wmi_device *wdev, u64 length)
254 struct wmi_block *wblock;
256 wblock = container_of(wdev, struct wmi_block, dev);
257 wblock->req_buf_size = length;
261 EXPORT_SYMBOL_GPL(set_required_buffer_size);
264 * wmi_evaluate_method - Evaluate a WMI method
265 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
266 * @instance: Instance index
267 * @method_id: Method ID to call
268 * @in: Buffer containing input for the method call
269 * @out: Empty buffer to return the method results
271 * Call an ACPI-WMI method
273 acpi_status wmi_evaluate_method(const char *guid_string, u8 instance, u32 method_id,
274 const struct acpi_buffer *in, struct acpi_buffer *out)
276 struct wmi_block *wblock = NULL;
279 status = find_guid(guid_string, &wblock);
280 if (ACPI_FAILURE(status))
283 return wmidev_evaluate_method(&wblock->dev, instance, method_id,
286 EXPORT_SYMBOL_GPL(wmi_evaluate_method);
289 * wmidev_evaluate_method - Evaluate a WMI method
290 * @wdev: A wmi bus device from a driver
291 * @instance: Instance index
292 * @method_id: Method ID to call
293 * @in: Buffer containing input for the method call
294 * @out: Empty buffer to return the method results
296 * Call an ACPI-WMI method
298 acpi_status wmidev_evaluate_method(struct wmi_device *wdev, u8 instance, u32 method_id,
299 const struct acpi_buffer *in, struct acpi_buffer *out)
301 struct guid_block *block;
302 struct wmi_block *wblock;
304 struct acpi_object_list input;
305 union acpi_object params[3];
306 char method[WMI_ACPI_METHOD_NAME_SIZE];
308 wblock = container_of(wdev, struct wmi_block, dev);
309 block = &wblock->gblock;
310 handle = wblock->acpi_device->handle;
312 if (!(block->flags & ACPI_WMI_METHOD))
315 if (block->instance_count <= instance)
316 return AE_BAD_PARAMETER;
319 input.pointer = params;
320 params[0].type = ACPI_TYPE_INTEGER;
321 params[0].integer.value = instance;
322 params[1].type = ACPI_TYPE_INTEGER;
323 params[1].integer.value = method_id;
328 params[2].type = get_param_acpi_type(wblock);
329 params[2].buffer.length = in->length;
330 params[2].buffer.pointer = in->pointer;
333 get_acpi_method_name(wblock, 'M', method);
335 return acpi_evaluate_object(handle, method, &input, out);
337 EXPORT_SYMBOL_GPL(wmidev_evaluate_method);
339 static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
340 struct acpi_buffer *out)
342 struct guid_block *block;
344 acpi_status status, wc_status = AE_ERROR;
345 struct acpi_object_list input;
346 union acpi_object wq_params[1];
347 char wc_method[WMI_ACPI_METHOD_NAME_SIZE];
348 char method[WMI_ACPI_METHOD_NAME_SIZE];
351 return AE_BAD_PARAMETER;
353 block = &wblock->gblock;
354 handle = wblock->acpi_device->handle;
356 if (block->instance_count <= instance)
357 return AE_BAD_PARAMETER;
359 /* Check GUID is a data block */
360 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
364 input.pointer = wq_params;
365 wq_params[0].type = ACPI_TYPE_INTEGER;
366 wq_params[0].integer.value = instance;
368 if (instance == 0 && test_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags))
372 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
375 if (block->flags & ACPI_WMI_EXPENSIVE) {
376 get_acpi_method_name(wblock, 'C', wc_method);
379 * Some GUIDs break the specification by declaring themselves
380 * expensive, but have no corresponding WCxx method. So we
381 * should not fail if this happens.
383 wc_status = acpi_execute_simple_method(handle, wc_method, 1);
386 get_acpi_method_name(wblock, 'Q', method);
387 status = acpi_evaluate_object(handle, method, &input, out);
390 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
391 * the WQxx method failed - we should disable collection anyway.
393 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
395 * Ignore whether this WCxx call succeeds or not since
396 * the previously executed WQxx method call might have
397 * succeeded, and returning the failing status code
398 * of this call would throw away the result of the WQxx
399 * call, potentially leaking memory.
401 acpi_execute_simple_method(handle, wc_method, 0);
408 * wmi_query_block - Return contents of a WMI block (deprecated)
409 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
410 * @instance: Instance index
411 * @out: Empty buffer to return the contents of the data block to
413 * Return the contents of an ACPI-WMI data block to a buffer
415 acpi_status wmi_query_block(const char *guid_string, u8 instance,
416 struct acpi_buffer *out)
418 struct wmi_block *wblock;
421 status = find_guid(guid_string, &wblock);
422 if (ACPI_FAILURE(status))
425 return __query_block(wblock, instance, out);
427 EXPORT_SYMBOL_GPL(wmi_query_block);
429 union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance)
431 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
432 struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
434 if (ACPI_FAILURE(__query_block(wblock, instance, &out)))
439 EXPORT_SYMBOL_GPL(wmidev_block_query);
442 * wmi_set_block - Write to a WMI block
443 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
444 * @instance: Instance index
445 * @in: Buffer containing new values for the data block
447 * Write the contents of the input buffer to an ACPI-WMI data block
449 acpi_status wmi_set_block(const char *guid_string, u8 instance,
450 const struct acpi_buffer *in)
452 struct wmi_block *wblock = NULL;
453 struct guid_block *block;
455 struct acpi_object_list input;
456 union acpi_object params[2];
457 char method[WMI_ACPI_METHOD_NAME_SIZE];
463 status = find_guid(guid_string, &wblock);
464 if (ACPI_FAILURE(status))
467 block = &wblock->gblock;
468 handle = wblock->acpi_device->handle;
470 if (block->instance_count <= instance)
471 return AE_BAD_PARAMETER;
473 /* Check GUID is a data block */
474 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
478 input.pointer = params;
479 params[0].type = ACPI_TYPE_INTEGER;
480 params[0].integer.value = instance;
481 params[1].type = get_param_acpi_type(wblock);
482 params[1].buffer.length = in->length;
483 params[1].buffer.pointer = in->pointer;
485 get_acpi_method_name(wblock, 'S', method);
487 return acpi_evaluate_object(handle, method, &input, NULL);
489 EXPORT_SYMBOL_GPL(wmi_set_block);
491 static void wmi_dump_wdg(const struct guid_block *g)
493 pr_info("%pUL:\n", &g->guid);
494 if (g->flags & ACPI_WMI_EVENT)
495 pr_info("\tnotify_id: 0x%02X\n", g->notify_id);
497 pr_info("\tobject_id: %2pE\n", g->object_id);
498 pr_info("\tinstance_count: %d\n", g->instance_count);
499 pr_info("\tflags: %#x", g->flags);
501 if (g->flags & ACPI_WMI_EXPENSIVE)
502 pr_cont(" ACPI_WMI_EXPENSIVE");
503 if (g->flags & ACPI_WMI_METHOD)
504 pr_cont(" ACPI_WMI_METHOD");
505 if (g->flags & ACPI_WMI_STRING)
506 pr_cont(" ACPI_WMI_STRING");
507 if (g->flags & ACPI_WMI_EVENT)
508 pr_cont(" ACPI_WMI_EVENT");
514 static void wmi_notify_debug(u32 value, void *context)
516 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
517 union acpi_object *obj;
520 status = wmi_get_event_data(value, &response);
521 if (status != AE_OK) {
522 pr_info("bad event status 0x%x\n", status);
526 obj = response.pointer;
530 pr_info("DEBUG: event 0x%02X ", value);
532 case ACPI_TYPE_BUFFER:
533 pr_cont("BUFFER_TYPE - length %u\n", obj->buffer.length);
535 case ACPI_TYPE_STRING:
536 pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
538 case ACPI_TYPE_INTEGER:
539 pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
541 case ACPI_TYPE_PACKAGE:
542 pr_cont("PACKAGE_TYPE - %u elements\n", obj->package.count);
545 pr_cont("object type 0x%X\n", obj->type);
551 * wmi_install_notify_handler - Register handler for WMI events
552 * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
553 * @handler: Function to handle notifications
554 * @data: Data to be returned to handler when event is fired
556 * Register a handler for events sent to the ACPI-WMI mapper device.
558 acpi_status wmi_install_notify_handler(const char *guid,
559 wmi_notify_handler handler,
562 struct wmi_block *block;
563 acpi_status status = AE_NOT_EXIST;
566 if (!guid || !handler)
567 return AE_BAD_PARAMETER;
569 if (guid_parse(guid, &guid_input))
570 return AE_BAD_PARAMETER;
572 list_for_each_entry(block, &wmi_block_list, list) {
573 acpi_status wmi_status;
575 if (guid_equal(&block->gblock.guid, &guid_input)) {
576 if (block->handler &&
577 block->handler != wmi_notify_debug)
578 return AE_ALREADY_ACQUIRED;
580 block->handler = handler;
581 block->handler_data = data;
583 wmi_status = wmi_method_enable(block, true);
584 if ((wmi_status != AE_OK) ||
585 ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
592 EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
595 * wmi_remove_notify_handler - Unregister handler for WMI events
596 * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
598 * Unregister handler for events sent to the ACPI-WMI mapper device.
600 acpi_status wmi_remove_notify_handler(const char *guid)
602 struct wmi_block *block;
603 acpi_status status = AE_NOT_EXIST;
607 return AE_BAD_PARAMETER;
609 if (guid_parse(guid, &guid_input))
610 return AE_BAD_PARAMETER;
612 list_for_each_entry(block, &wmi_block_list, list) {
613 acpi_status wmi_status;
615 if (guid_equal(&block->gblock.guid, &guid_input)) {
616 if (!block->handler ||
617 block->handler == wmi_notify_debug)
618 return AE_NULL_ENTRY;
621 block->handler = wmi_notify_debug;
624 wmi_status = wmi_method_enable(block, false);
625 block->handler = NULL;
626 block->handler_data = NULL;
627 if ((wmi_status != AE_OK) ||
628 ((wmi_status == AE_OK) &&
629 (status == AE_NOT_EXIST)))
637 EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
640 * wmi_get_event_data - Get WMI data associated with an event
642 * @event: Event to find
643 * @out: Buffer to hold event data. out->pointer should be freed with kfree()
645 * Returns extra data associated with an event in WMI.
647 acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
649 struct wmi_block *wblock;
651 list_for_each_entry(wblock, &wmi_block_list, list) {
652 struct guid_block *gblock = &wblock->gblock;
654 if ((gblock->flags & ACPI_WMI_EVENT) && gblock->notify_id == event)
655 return get_event_data(wblock, out);
660 EXPORT_SYMBOL_GPL(wmi_get_event_data);
663 * wmi_has_guid - Check if a GUID is available
664 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
666 * Check if a given GUID is defined by _WDG
668 bool wmi_has_guid(const char *guid_string)
670 return ACPI_SUCCESS(find_guid(guid_string, NULL));
672 EXPORT_SYMBOL_GPL(wmi_has_guid);
675 * wmi_get_acpi_device_uid() - Get _UID name of ACPI device that defines GUID
676 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
678 * Find the _UID of ACPI device associated with this WMI GUID.
680 * Return: The ACPI _UID field value or NULL if the WMI GUID was not found
682 char *wmi_get_acpi_device_uid(const char *guid_string)
684 struct wmi_block *wblock = NULL;
687 status = find_guid(guid_string, &wblock);
688 if (ACPI_FAILURE(status))
691 return acpi_device_uid(wblock->acpi_device);
693 EXPORT_SYMBOL_GPL(wmi_get_acpi_device_uid);
695 static struct wmi_block *dev_to_wblock(struct device *dev)
697 return container_of(dev, struct wmi_block, dev.dev);
700 static struct wmi_device *dev_to_wdev(struct device *dev)
702 return container_of(dev, struct wmi_device, dev);
705 static inline struct wmi_driver *drv_to_wdrv(struct device_driver *drv)
707 return container_of(drv, struct wmi_driver, driver);
713 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
716 struct wmi_block *wblock = dev_to_wblock(dev);
718 return sysfs_emit(buf, "wmi:%pUL\n", &wblock->gblock.guid);
720 static DEVICE_ATTR_RO(modalias);
722 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
725 struct wmi_block *wblock = dev_to_wblock(dev);
727 return sysfs_emit(buf, "%pUL\n", &wblock->gblock.guid);
729 static DEVICE_ATTR_RO(guid);
731 static ssize_t instance_count_show(struct device *dev,
732 struct device_attribute *attr, char *buf)
734 struct wmi_block *wblock = dev_to_wblock(dev);
736 return sysfs_emit(buf, "%d\n", (int)wblock->gblock.instance_count);
738 static DEVICE_ATTR_RO(instance_count);
740 static ssize_t expensive_show(struct device *dev,
741 struct device_attribute *attr, char *buf)
743 struct wmi_block *wblock = dev_to_wblock(dev);
745 return sysfs_emit(buf, "%d\n",
746 (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
748 static DEVICE_ATTR_RO(expensive);
750 static struct attribute *wmi_attrs[] = {
751 &dev_attr_modalias.attr,
753 &dev_attr_instance_count.attr,
754 &dev_attr_expensive.attr,
757 ATTRIBUTE_GROUPS(wmi);
759 static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
762 struct wmi_block *wblock = dev_to_wblock(dev);
764 return sysfs_emit(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
766 static DEVICE_ATTR_RO(notify_id);
768 static struct attribute *wmi_event_attrs[] = {
769 &dev_attr_notify_id.attr,
772 ATTRIBUTE_GROUPS(wmi_event);
774 static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
777 struct wmi_block *wblock = dev_to_wblock(dev);
779 return sysfs_emit(buf, "%c%c\n", wblock->gblock.object_id[0],
780 wblock->gblock.object_id[1]);
782 static DEVICE_ATTR_RO(object_id);
784 static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
787 struct wmi_device *wdev = dev_to_wdev(dev);
789 return sysfs_emit(buf, "%d\n", (int)wdev->setable);
791 static DEVICE_ATTR_RO(setable);
793 static struct attribute *wmi_data_attrs[] = {
794 &dev_attr_object_id.attr,
795 &dev_attr_setable.attr,
798 ATTRIBUTE_GROUPS(wmi_data);
800 static struct attribute *wmi_method_attrs[] = {
801 &dev_attr_object_id.attr,
804 ATTRIBUTE_GROUPS(wmi_method);
806 static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
808 struct wmi_block *wblock = dev_to_wblock(dev);
810 if (add_uevent_var(env, "MODALIAS=wmi:%pUL", &wblock->gblock.guid))
813 if (add_uevent_var(env, "WMI_GUID=%pUL", &wblock->gblock.guid))
819 static void wmi_dev_release(struct device *dev)
821 struct wmi_block *wblock = dev_to_wblock(dev);
826 static int wmi_dev_match(struct device *dev, struct device_driver *driver)
828 struct wmi_driver *wmi_driver = drv_to_wdrv(driver);
829 struct wmi_block *wblock = dev_to_wblock(dev);
830 const struct wmi_device_id *id = wmi_driver->id_table;
835 while (*id->guid_string) {
838 if (WARN_ON(guid_parse(id->guid_string, &driver_guid)))
840 if (guid_equal(&driver_guid, &wblock->gblock.guid))
848 static int wmi_char_open(struct inode *inode, struct file *filp)
850 const char *driver_name = filp->f_path.dentry->d_iname;
851 struct wmi_block *wblock;
852 struct wmi_block *next;
854 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
855 if (!wblock->dev.dev.driver)
857 if (strcmp(driver_name, wblock->dev.dev.driver->name) == 0) {
858 filp->private_data = wblock;
863 if (!filp->private_data)
866 return nonseekable_open(inode, filp);
869 static ssize_t wmi_char_read(struct file *filp, char __user *buffer,
870 size_t length, loff_t *offset)
872 struct wmi_block *wblock = filp->private_data;
874 return simple_read_from_buffer(buffer, length, offset,
875 &wblock->req_buf_size,
876 sizeof(wblock->req_buf_size));
879 static long wmi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
881 struct wmi_ioctl_buffer __user *input =
882 (struct wmi_ioctl_buffer __user *) arg;
883 struct wmi_block *wblock = filp->private_data;
884 struct wmi_ioctl_buffer *buf;
885 struct wmi_driver *wdriver;
888 if (_IOC_TYPE(cmd) != WMI_IOC)
891 /* make sure we're not calling a higher instance than exists*/
892 if (_IOC_NR(cmd) >= wblock->gblock.instance_count)
895 mutex_lock(&wblock->char_mutex);
896 buf = wblock->handler_data;
897 if (get_user(buf->length, &input->length)) {
898 dev_dbg(&wblock->dev.dev, "Read length from user failed\n");
902 /* if it's too small, abort */
903 if (buf->length < wblock->req_buf_size) {
904 dev_err(&wblock->dev.dev,
905 "Buffer %lld too small, need at least %lld\n",
906 buf->length, wblock->req_buf_size);
910 /* if it's too big, warn, driver will only use what is needed */
911 if (buf->length > wblock->req_buf_size)
912 dev_warn(&wblock->dev.dev,
913 "Buffer %lld is bigger than required %lld\n",
914 buf->length, wblock->req_buf_size);
916 /* copy the structure from userspace */
917 if (copy_from_user(buf, input, wblock->req_buf_size)) {
918 dev_dbg(&wblock->dev.dev, "Copy %llu from user failed\n",
919 wblock->req_buf_size);
924 /* let the driver do any filtering and do the call */
925 wdriver = drv_to_wdrv(wblock->dev.dev.driver);
926 if (!try_module_get(wdriver->driver.owner)) {
930 ret = wdriver->filter_callback(&wblock->dev, cmd, buf);
931 module_put(wdriver->driver.owner);
935 /* return the result (only up to our internal buffer size) */
936 if (copy_to_user(input, buf, wblock->req_buf_size)) {
937 dev_dbg(&wblock->dev.dev, "Copy %llu to user failed\n",
938 wblock->req_buf_size);
943 mutex_unlock(&wblock->char_mutex);
947 static const struct file_operations wmi_fops = {
948 .owner = THIS_MODULE,
949 .read = wmi_char_read,
950 .open = wmi_char_open,
951 .unlocked_ioctl = wmi_ioctl,
952 .compat_ioctl = compat_ptr_ioctl,
955 static int wmi_dev_probe(struct device *dev)
957 struct wmi_block *wblock = dev_to_wblock(dev);
958 struct wmi_driver *wdriver = drv_to_wdrv(dev->driver);
962 if (ACPI_FAILURE(wmi_method_enable(wblock, true)))
963 dev_warn(dev, "failed to enable device -- probing anyway\n");
965 if (wdriver->probe) {
966 ret = wdriver->probe(dev_to_wdev(dev),
967 find_guid_context(wblock, wdriver));
972 /* driver wants a character device made */
973 if (wdriver->filter_callback) {
974 /* check that required buffer size declared by driver or MOF */
975 if (!wblock->req_buf_size) {
976 dev_err(&wblock->dev.dev,
977 "Required buffer size not set\n");
982 wblock->handler_data = kmalloc(wblock->req_buf_size,
984 if (!wblock->handler_data) {
989 buf = kasprintf(GFP_KERNEL, "wmi/%s", wdriver->driver.name);
992 goto probe_string_failure;
994 wblock->char_dev.minor = MISC_DYNAMIC_MINOR;
995 wblock->char_dev.name = buf;
996 wblock->char_dev.fops = &wmi_fops;
997 wblock->char_dev.mode = 0444;
998 ret = misc_register(&wblock->char_dev);
1000 dev_warn(dev, "failed to register char dev: %d\n", ret);
1002 goto probe_misc_failure;
1006 set_bit(WMI_PROBED, &wblock->flags);
1011 probe_string_failure:
1012 kfree(wblock->handler_data);
1014 if (ACPI_FAILURE(wmi_method_enable(wblock, false)))
1015 dev_warn(dev, "failed to disable device\n");
1019 static void wmi_dev_remove(struct device *dev)
1021 struct wmi_block *wblock = dev_to_wblock(dev);
1022 struct wmi_driver *wdriver = drv_to_wdrv(dev->driver);
1024 clear_bit(WMI_PROBED, &wblock->flags);
1026 if (wdriver->filter_callback) {
1027 misc_deregister(&wblock->char_dev);
1028 kfree(wblock->char_dev.name);
1029 kfree(wblock->handler_data);
1032 if (wdriver->remove)
1033 wdriver->remove(dev_to_wdev(dev));
1035 if (ACPI_FAILURE(wmi_method_enable(wblock, false)))
1036 dev_warn(dev, "failed to disable device\n");
1039 static struct class wmi_bus_class = {
1043 static struct bus_type wmi_bus_type = {
1045 .dev_groups = wmi_groups,
1046 .match = wmi_dev_match,
1047 .uevent = wmi_dev_uevent,
1048 .probe = wmi_dev_probe,
1049 .remove = wmi_dev_remove,
1052 static const struct device_type wmi_type_event = {
1054 .groups = wmi_event_groups,
1055 .release = wmi_dev_release,
1058 static const struct device_type wmi_type_method = {
1060 .groups = wmi_method_groups,
1061 .release = wmi_dev_release,
1064 static const struct device_type wmi_type_data = {
1066 .groups = wmi_data_groups,
1067 .release = wmi_dev_release,
1071 * _WDG is a static list that is only parsed at startup,
1072 * so it's safe to count entries without extra protection.
1074 static int guid_count(const guid_t *guid)
1076 struct wmi_block *wblock;
1079 list_for_each_entry(wblock, &wmi_block_list, list) {
1080 if (guid_equal(&wblock->gblock.guid, guid))
1087 static int wmi_create_device(struct device *wmi_bus_dev,
1088 struct wmi_block *wblock,
1089 struct acpi_device *device)
1091 struct acpi_device_info *info;
1092 char method[WMI_ACPI_METHOD_NAME_SIZE];
1096 if (wblock->gblock.flags & ACPI_WMI_EVENT) {
1097 wblock->dev.dev.type = &wmi_type_event;
1101 if (wblock->gblock.flags & ACPI_WMI_METHOD) {
1102 wblock->dev.dev.type = &wmi_type_method;
1103 mutex_init(&wblock->char_mutex);
1108 * Data Block Query Control Method (WQxx by convention) is
1109 * required per the WMI documentation. If it is not present,
1110 * we ignore this data block.
1112 get_acpi_method_name(wblock, 'Q', method);
1113 result = get_subobj_info(device->handle, method, &info);
1116 dev_warn(wmi_bus_dev,
1117 "%s data block query control method not found\n",
1122 wblock->dev.dev.type = &wmi_type_data;
1125 * The Microsoft documentation specifically states:
1127 * Data blocks registered with only a single instance
1128 * can ignore the parameter.
1130 * ACPICA will get mad at us if we call the method with the wrong number
1131 * of arguments, so check what our method expects. (On some Dell
1132 * laptops, WQxx may not be a method at all.)
1134 if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
1135 set_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags);
1139 get_acpi_method_name(wblock, 'S', method);
1140 result = get_subobj_info(device->handle, method, NULL);
1143 wblock->dev.setable = true;
1146 wblock->dev.dev.bus = &wmi_bus_type;
1147 wblock->dev.dev.parent = wmi_bus_dev;
1149 count = guid_count(&wblock->gblock.guid);
1151 dev_set_name(&wblock->dev.dev, "%pUL-%d", &wblock->gblock.guid, count);
1153 dev_set_name(&wblock->dev.dev, "%pUL", &wblock->gblock.guid);
1155 device_initialize(&wblock->dev.dev);
1160 static void wmi_free_devices(struct acpi_device *device)
1162 struct wmi_block *wblock, *next;
1164 /* Delete devices for all the GUIDs */
1165 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1166 if (wblock->acpi_device == device) {
1167 list_del(&wblock->list);
1168 device_unregister(&wblock->dev.dev);
1173 static bool guid_already_parsed_for_legacy(struct acpi_device *device, const guid_t *guid)
1175 struct wmi_block *wblock;
1177 list_for_each_entry(wblock, &wmi_block_list, list) {
1178 /* skip warning and register if we know the driver will use struct wmi_driver */
1179 for (int i = 0; allow_duplicates[i] != NULL; i++) {
1182 if (guid_parse(allow_duplicates[i], &tmp))
1184 if (guid_equal(&tmp, guid))
1187 if (guid_equal(&wblock->gblock.guid, guid)) {
1189 * Because we historically didn't track the relationship
1190 * between GUIDs and ACPI nodes, we don't know whether
1191 * we need to suppress GUIDs that are unique on a
1192 * given node but duplicated across nodes.
1194 dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
1195 guid, dev_name(&wblock->acpi_device->dev));
1204 * Parse the _WDG method for the GUID data blocks
1206 static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
1208 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
1209 const struct guid_block *gblock;
1210 struct wmi_block *wblock, *next;
1211 union acpi_object *obj;
1216 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
1217 if (ACPI_FAILURE(status))
1224 if (obj->type != ACPI_TYPE_BUFFER) {
1226 goto out_free_pointer;
1229 gblock = (const struct guid_block *)obj->buffer.pointer;
1230 total = obj->buffer.length / sizeof(struct guid_block);
1232 for (i = 0; i < total; i++) {
1234 wmi_dump_wdg(&gblock[i]);
1236 if (guid_already_parsed_for_legacy(device, &gblock[i].guid))
1239 wblock = kzalloc(sizeof(*wblock), GFP_KERNEL);
1245 wblock->acpi_device = device;
1246 wblock->gblock = gblock[i];
1248 retval = wmi_create_device(wmi_bus_dev, wblock, device);
1254 list_add_tail(&wblock->list, &wmi_block_list);
1257 wblock->handler = wmi_notify_debug;
1258 wmi_method_enable(wblock, true);
1263 * Now that all of the devices are created, add them to the
1264 * device tree and probe subdrivers.
1266 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1267 if (wblock->acpi_device != device)
1270 retval = device_add(&wblock->dev.dev);
1272 dev_err(wmi_bus_dev, "failed to register %pUL\n",
1273 &wblock->gblock.guid);
1275 wmi_method_enable(wblock, false);
1276 list_del(&wblock->list);
1277 put_device(&wblock->dev.dev);
1287 * WMI can have EmbeddedControl access regions. In which case, we just want to
1288 * hand these off to the EC driver.
1291 acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
1292 u32 bits, u64 *value,
1293 void *handler_context, void *region_context)
1295 int result = 0, i = 0;
1298 if ((address > 0xFF) || !value)
1299 return AE_BAD_PARAMETER;
1301 if (function != ACPI_READ && function != ACPI_WRITE)
1302 return AE_BAD_PARAMETER;
1305 return AE_BAD_PARAMETER;
1307 if (function == ACPI_READ) {
1308 result = ec_read(address, &temp);
1309 (*value) |= ((u64)temp) << i;
1311 temp = 0xff & ((*value) >> i);
1312 result = ec_write(address, temp);
1317 return AE_BAD_PARAMETER;
1319 return AE_NOT_FOUND;
1327 static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
1330 struct wmi_block *wblock = NULL, *iter;
1332 list_for_each_entry(iter, &wmi_block_list, list) {
1333 struct guid_block *block = &iter->gblock;
1335 if (iter->acpi_device->handle == handle &&
1336 (block->flags & ACPI_WMI_EVENT) &&
1337 (block->notify_id == event)) {
1346 /* If a driver is bound, then notify the driver. */
1347 if (test_bit(WMI_PROBED, &wblock->flags) && wblock->dev.dev.driver) {
1348 struct wmi_driver *driver = drv_to_wdrv(wblock->dev.dev.driver);
1349 struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
1352 if (!driver->no_notify_data) {
1353 status = get_event_data(wblock, &evdata);
1354 if (ACPI_FAILURE(status)) {
1355 dev_warn(&wblock->dev.dev, "failed to get event data\n");
1361 driver->notify(&wblock->dev, evdata.pointer);
1363 kfree(evdata.pointer);
1364 } else if (wblock->handler) {
1365 /* Legacy handler */
1366 wblock->handler(event, wblock->handler_data);
1370 pr_info("DEBUG: GUID %pUL event 0x%02X\n", &wblock->gblock.guid, event);
1372 acpi_bus_generate_netlink_event(
1373 wblock->acpi_device->pnp.device_class,
1374 dev_name(&wblock->dev.dev),
1378 static int acpi_wmi_remove(struct platform_device *device)
1380 struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
1382 acpi_remove_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY,
1383 acpi_wmi_notify_handler);
1384 acpi_remove_address_space_handler(acpi_device->handle,
1385 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
1386 wmi_free_devices(acpi_device);
1387 device_unregister(dev_get_drvdata(&device->dev));
1392 static int acpi_wmi_probe(struct platform_device *device)
1394 struct acpi_device *acpi_device;
1395 struct device *wmi_bus_dev;
1399 acpi_device = ACPI_COMPANION(&device->dev);
1401 dev_err(&device->dev, "ACPI companion is missing\n");
1405 status = acpi_install_address_space_handler(acpi_device->handle,
1407 &acpi_wmi_ec_space_handler,
1409 if (ACPI_FAILURE(status)) {
1410 dev_err(&device->dev, "Error installing EC region handler\n");
1414 status = acpi_install_notify_handler(acpi_device->handle,
1416 acpi_wmi_notify_handler,
1418 if (ACPI_FAILURE(status)) {
1419 dev_err(&device->dev, "Error installing notify handler\n");
1421 goto err_remove_ec_handler;
1424 wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
1425 NULL, "wmi_bus-%s", dev_name(&device->dev));
1426 if (IS_ERR(wmi_bus_dev)) {
1427 error = PTR_ERR(wmi_bus_dev);
1428 goto err_remove_notify_handler;
1430 dev_set_drvdata(&device->dev, wmi_bus_dev);
1432 error = parse_wdg(wmi_bus_dev, acpi_device);
1434 pr_err("Failed to parse WDG method\n");
1435 goto err_remove_busdev;
1441 device_unregister(wmi_bus_dev);
1443 err_remove_notify_handler:
1444 acpi_remove_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY,
1445 acpi_wmi_notify_handler);
1447 err_remove_ec_handler:
1448 acpi_remove_address_space_handler(acpi_device->handle,
1450 &acpi_wmi_ec_space_handler);
1455 int __must_check __wmi_driver_register(struct wmi_driver *driver,
1456 struct module *owner)
1458 driver->driver.owner = owner;
1459 driver->driver.bus = &wmi_bus_type;
1461 return driver_register(&driver->driver);
1463 EXPORT_SYMBOL(__wmi_driver_register);
1465 void wmi_driver_unregister(struct wmi_driver *driver)
1467 driver_unregister(&driver->driver);
1469 EXPORT_SYMBOL(wmi_driver_unregister);
1471 static struct platform_driver acpi_wmi_driver = {
1474 .acpi_match_table = wmi_device_ids,
1476 .probe = acpi_wmi_probe,
1477 .remove = acpi_wmi_remove,
1480 static int __init acpi_wmi_init(void)
1487 error = class_register(&wmi_bus_class);
1491 error = bus_register(&wmi_bus_type);
1493 goto err_unreg_class;
1495 error = platform_driver_register(&acpi_wmi_driver);
1497 pr_err("Error loading mapper\n");
1504 bus_unregister(&wmi_bus_type);
1507 class_unregister(&wmi_bus_class);
1512 static void __exit acpi_wmi_exit(void)
1514 platform_driver_unregister(&acpi_wmi_driver);
1515 bus_unregister(&wmi_bus_type);
1516 class_unregister(&wmi_bus_class);
1519 subsys_initcall_sync(acpi_wmi_init);
1520 module_exit(acpi_wmi_exit);