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/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 */
108 "8A42EA14-4F2A-FD45-6422-0087F7A7E608", /* dell-wmi-ddv */
113 * GUID parsing functions
116 static acpi_status find_guid(const char *guid_string, struct wmi_block **out)
119 struct wmi_block *wblock;
122 return AE_BAD_PARAMETER;
124 if (guid_parse(guid_string, &guid_input))
125 return AE_BAD_PARAMETER;
127 list_for_each_entry(wblock, &wmi_block_list, list) {
128 if (guid_equal(&wblock->gblock.guid, &guid_input)) {
139 static bool guid_parse_and_compare(const char *string, const guid_t *guid)
143 if (guid_parse(string, &guid_input))
146 return guid_equal(&guid_input, guid);
149 static const void *find_guid_context(struct wmi_block *wblock,
150 struct wmi_driver *wdriver)
152 const struct wmi_device_id *id;
154 id = wdriver->id_table;
158 while (*id->guid_string) {
159 if (guid_parse_and_compare(id->guid_string, &wblock->gblock.guid))
166 static int get_subobj_info(acpi_handle handle, const char *pathname,
167 struct acpi_device_info **info)
169 struct acpi_device_info *dummy_info, **info_ptr;
170 acpi_handle subobj_handle;
173 status = acpi_get_handle(handle, (char *)pathname, &subobj_handle);
174 if (status == AE_NOT_FOUND)
176 else if (ACPI_FAILURE(status))
179 info_ptr = info ? info : &dummy_info;
180 status = acpi_get_object_info(subobj_handle, info_ptr);
181 if (ACPI_FAILURE(status))
190 static acpi_status wmi_method_enable(struct wmi_block *wblock, bool enable)
192 struct guid_block *block;
197 block = &wblock->gblock;
198 handle = wblock->acpi_device->handle;
200 snprintf(method, 5, "WE%02X", block->notify_id);
201 status = acpi_execute_simple_method(handle, method, enable);
202 if (status == AE_NOT_FOUND)
208 #define WMI_ACPI_METHOD_NAME_SIZE 5
210 static inline void get_acpi_method_name(const struct wmi_block *wblock,
212 char buffer[static WMI_ACPI_METHOD_NAME_SIZE])
214 static_assert(ARRAY_SIZE(wblock->gblock.object_id) == 2);
215 static_assert(WMI_ACPI_METHOD_NAME_SIZE >= 5);
219 buffer[2] = wblock->gblock.object_id[0];
220 buffer[3] = wblock->gblock.object_id[1];
224 static inline acpi_object_type get_param_acpi_type(const struct wmi_block *wblock)
226 if (wblock->gblock.flags & ACPI_WMI_STRING)
227 return ACPI_TYPE_STRING;
229 return ACPI_TYPE_BUFFER;
232 static acpi_status get_event_data(const struct wmi_block *wblock, struct acpi_buffer *out)
234 union acpi_object param = {
236 .type = ACPI_TYPE_INTEGER,
237 .value = wblock->gblock.notify_id,
240 struct acpi_object_list input = {
245 return acpi_evaluate_object(wblock->acpi_device->handle, "_WED", &input, out);
249 * Exported WMI functions
253 * set_required_buffer_size - Sets the buffer size needed for performing IOCTL
254 * @wdev: A wmi bus device from a driver
255 * @length: Required buffer size
257 * Allocates memory needed for buffer, stores the buffer size in that memory.
259 * Return: 0 on success or a negative error code for failure.
261 int set_required_buffer_size(struct wmi_device *wdev, u64 length)
263 struct wmi_block *wblock;
265 wblock = container_of(wdev, struct wmi_block, dev);
266 wblock->req_buf_size = length;
270 EXPORT_SYMBOL_GPL(set_required_buffer_size);
273 * wmi_instance_count - Get number of WMI object instances
274 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
276 * Get the number of WMI object instances.
278 * Returns: Number of WMI object instances or negative error code.
280 int wmi_instance_count(const char *guid_string)
282 struct wmi_block *wblock;
285 status = find_guid(guid_string, &wblock);
286 if (ACPI_FAILURE(status)) {
287 if (status == AE_BAD_PARAMETER)
293 return wmidev_instance_count(&wblock->dev);
295 EXPORT_SYMBOL_GPL(wmi_instance_count);
298 * wmidev_instance_count - Get number of WMI object instances
299 * @wdev: A wmi bus device from a driver
301 * Get the number of WMI object instances.
303 * Returns: Number of WMI object instances.
305 u8 wmidev_instance_count(struct wmi_device *wdev)
307 struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
309 return wblock->gblock.instance_count;
311 EXPORT_SYMBOL_GPL(wmidev_instance_count);
314 * wmi_evaluate_method - Evaluate a WMI method (deprecated)
315 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
316 * @instance: Instance index
317 * @method_id: Method ID to call
318 * @in: Buffer containing input for the method call
319 * @out: Empty buffer to return the method results
321 * Call an ACPI-WMI method, the caller must free @out.
323 * Return: acpi_status signaling success or error.
325 acpi_status wmi_evaluate_method(const char *guid_string, u8 instance, u32 method_id,
326 const struct acpi_buffer *in, struct acpi_buffer *out)
328 struct wmi_block *wblock = NULL;
331 status = find_guid(guid_string, &wblock);
332 if (ACPI_FAILURE(status))
335 return wmidev_evaluate_method(&wblock->dev, instance, method_id,
338 EXPORT_SYMBOL_GPL(wmi_evaluate_method);
341 * wmidev_evaluate_method - Evaluate a WMI method
342 * @wdev: A wmi bus device from a driver
343 * @instance: Instance index
344 * @method_id: Method ID to call
345 * @in: Buffer containing input for the method call
346 * @out: Empty buffer to return the method results
348 * Call an ACPI-WMI method, the caller must free @out.
350 * Return: acpi_status signaling success or error.
352 acpi_status wmidev_evaluate_method(struct wmi_device *wdev, u8 instance, u32 method_id,
353 const struct acpi_buffer *in, struct acpi_buffer *out)
355 struct guid_block *block;
356 struct wmi_block *wblock;
358 struct acpi_object_list input;
359 union acpi_object params[3];
360 char method[WMI_ACPI_METHOD_NAME_SIZE];
362 wblock = container_of(wdev, struct wmi_block, dev);
363 block = &wblock->gblock;
364 handle = wblock->acpi_device->handle;
366 if (!(block->flags & ACPI_WMI_METHOD))
369 if (block->instance_count <= instance)
370 return AE_BAD_PARAMETER;
373 input.pointer = params;
374 params[0].type = ACPI_TYPE_INTEGER;
375 params[0].integer.value = instance;
376 params[1].type = ACPI_TYPE_INTEGER;
377 params[1].integer.value = method_id;
382 params[2].type = get_param_acpi_type(wblock);
383 params[2].buffer.length = in->length;
384 params[2].buffer.pointer = in->pointer;
387 get_acpi_method_name(wblock, 'M', method);
389 return acpi_evaluate_object(handle, method, &input, out);
391 EXPORT_SYMBOL_GPL(wmidev_evaluate_method);
393 static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
394 struct acpi_buffer *out)
396 struct guid_block *block;
398 acpi_status status, wc_status = AE_ERROR;
399 struct acpi_object_list input;
400 union acpi_object wq_params[1];
401 char wc_method[WMI_ACPI_METHOD_NAME_SIZE];
402 char method[WMI_ACPI_METHOD_NAME_SIZE];
405 return AE_BAD_PARAMETER;
407 block = &wblock->gblock;
408 handle = wblock->acpi_device->handle;
410 if (block->instance_count <= instance)
411 return AE_BAD_PARAMETER;
413 /* Check GUID is a data block */
414 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
418 input.pointer = wq_params;
419 wq_params[0].type = ACPI_TYPE_INTEGER;
420 wq_params[0].integer.value = instance;
422 if (instance == 0 && test_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags))
426 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
429 if (block->flags & ACPI_WMI_EXPENSIVE) {
430 get_acpi_method_name(wblock, 'C', wc_method);
433 * Some GUIDs break the specification by declaring themselves
434 * expensive, but have no corresponding WCxx method. So we
435 * should not fail if this happens.
437 wc_status = acpi_execute_simple_method(handle, wc_method, 1);
440 get_acpi_method_name(wblock, 'Q', method);
441 status = acpi_evaluate_object(handle, method, &input, out);
444 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
445 * the WQxx method failed - we should disable collection anyway.
447 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
449 * Ignore whether this WCxx call succeeds or not since
450 * the previously executed WQxx method call might have
451 * succeeded, and returning the failing status code
452 * of this call would throw away the result of the WQxx
453 * call, potentially leaking memory.
455 acpi_execute_simple_method(handle, wc_method, 0);
462 * wmi_query_block - Return contents of a WMI block (deprecated)
463 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
464 * @instance: Instance index
465 * @out: Empty buffer to return the contents of the data block to
467 * Query a ACPI-WMI block, the caller must free @out.
469 * Return: ACPI object containing the content of the WMI block.
471 acpi_status wmi_query_block(const char *guid_string, u8 instance,
472 struct acpi_buffer *out)
474 struct wmi_block *wblock;
477 status = find_guid(guid_string, &wblock);
478 if (ACPI_FAILURE(status))
481 return __query_block(wblock, instance, out);
483 EXPORT_SYMBOL_GPL(wmi_query_block);
486 * wmidev_block_query - Return contents of a WMI block
487 * @wdev: A wmi bus device from a driver
488 * @instance: Instance index
490 * Query an ACPI-WMI block, the caller must free the result.
492 * Return: ACPI object containing the content of the WMI block.
494 union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance)
496 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
497 struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
499 if (ACPI_FAILURE(__query_block(wblock, instance, &out)))
504 EXPORT_SYMBOL_GPL(wmidev_block_query);
507 * wmi_set_block - Write to a WMI block (deprecated)
508 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
509 * @instance: Instance index
510 * @in: Buffer containing new values for the data block
512 * Write the contents of the input buffer to an ACPI-WMI data block.
514 * Return: acpi_status signaling success or error.
516 acpi_status wmi_set_block(const char *guid_string, u8 instance,
517 const struct acpi_buffer *in)
519 struct wmi_block *wblock = NULL;
520 struct guid_block *block;
522 struct acpi_object_list input;
523 union acpi_object params[2];
524 char method[WMI_ACPI_METHOD_NAME_SIZE];
530 status = find_guid(guid_string, &wblock);
531 if (ACPI_FAILURE(status))
534 block = &wblock->gblock;
535 handle = wblock->acpi_device->handle;
537 if (block->instance_count <= instance)
538 return AE_BAD_PARAMETER;
540 /* Check GUID is a data block */
541 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
545 input.pointer = params;
546 params[0].type = ACPI_TYPE_INTEGER;
547 params[0].integer.value = instance;
548 params[1].type = get_param_acpi_type(wblock);
549 params[1].buffer.length = in->length;
550 params[1].buffer.pointer = in->pointer;
552 get_acpi_method_name(wblock, 'S', method);
554 return acpi_evaluate_object(handle, method, &input, NULL);
556 EXPORT_SYMBOL_GPL(wmi_set_block);
558 static void wmi_dump_wdg(const struct guid_block *g)
560 pr_info("%pUL:\n", &g->guid);
561 if (g->flags & ACPI_WMI_EVENT)
562 pr_info("\tnotify_id: 0x%02X\n", g->notify_id);
564 pr_info("\tobject_id: %2pE\n", g->object_id);
565 pr_info("\tinstance_count: %d\n", g->instance_count);
566 pr_info("\tflags: %#x", g->flags);
568 if (g->flags & ACPI_WMI_EXPENSIVE)
569 pr_cont(" ACPI_WMI_EXPENSIVE");
570 if (g->flags & ACPI_WMI_METHOD)
571 pr_cont(" ACPI_WMI_METHOD");
572 if (g->flags & ACPI_WMI_STRING)
573 pr_cont(" ACPI_WMI_STRING");
574 if (g->flags & ACPI_WMI_EVENT)
575 pr_cont(" ACPI_WMI_EVENT");
581 static void wmi_notify_debug(u32 value, void *context)
583 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
584 union acpi_object *obj;
587 status = wmi_get_event_data(value, &response);
588 if (status != AE_OK) {
589 pr_info("bad event status 0x%x\n", status);
593 obj = response.pointer;
597 pr_info("DEBUG: event 0x%02X ", value);
599 case ACPI_TYPE_BUFFER:
600 pr_cont("BUFFER_TYPE - length %u\n", obj->buffer.length);
602 case ACPI_TYPE_STRING:
603 pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
605 case ACPI_TYPE_INTEGER:
606 pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
608 case ACPI_TYPE_PACKAGE:
609 pr_cont("PACKAGE_TYPE - %u elements\n", obj->package.count);
612 pr_cont("object type 0x%X\n", obj->type);
618 * wmi_install_notify_handler - Register handler for WMI events (deprecated)
619 * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
620 * @handler: Function to handle notifications
621 * @data: Data to be returned to handler when event is fired
623 * Register a handler for events sent to the ACPI-WMI mapper device.
625 * Return: acpi_status signaling success or error.
627 acpi_status wmi_install_notify_handler(const char *guid,
628 wmi_notify_handler handler,
631 struct wmi_block *block;
632 acpi_status status = AE_NOT_EXIST;
635 if (!guid || !handler)
636 return AE_BAD_PARAMETER;
638 if (guid_parse(guid, &guid_input))
639 return AE_BAD_PARAMETER;
641 list_for_each_entry(block, &wmi_block_list, list) {
642 acpi_status wmi_status;
644 if (guid_equal(&block->gblock.guid, &guid_input)) {
645 if (block->handler &&
646 block->handler != wmi_notify_debug)
647 return AE_ALREADY_ACQUIRED;
649 block->handler = handler;
650 block->handler_data = data;
652 wmi_status = wmi_method_enable(block, true);
653 if ((wmi_status != AE_OK) ||
654 ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
661 EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
664 * wmi_remove_notify_handler - Unregister handler for WMI events (deprecated)
665 * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
667 * Unregister handler for events sent to the ACPI-WMI mapper device.
669 * Return: acpi_status signaling success or error.
671 acpi_status wmi_remove_notify_handler(const char *guid)
673 struct wmi_block *block;
674 acpi_status status = AE_NOT_EXIST;
678 return AE_BAD_PARAMETER;
680 if (guid_parse(guid, &guid_input))
681 return AE_BAD_PARAMETER;
683 list_for_each_entry(block, &wmi_block_list, list) {
684 acpi_status wmi_status;
686 if (guid_equal(&block->gblock.guid, &guid_input)) {
687 if (!block->handler ||
688 block->handler == wmi_notify_debug)
689 return AE_NULL_ENTRY;
692 block->handler = wmi_notify_debug;
695 wmi_status = wmi_method_enable(block, false);
696 block->handler = NULL;
697 block->handler_data = NULL;
698 if ((wmi_status != AE_OK) ||
699 ((wmi_status == AE_OK) &&
700 (status == AE_NOT_EXIST)))
708 EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
711 * wmi_get_event_data - Get WMI data associated with an event (deprecated)
713 * @event: Event to find
714 * @out: Buffer to hold event data
716 * Get extra data associated with an WMI event, the caller needs to free @out.
718 * Return: acpi_status signaling success or error.
720 acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
722 struct wmi_block *wblock;
724 list_for_each_entry(wblock, &wmi_block_list, list) {
725 struct guid_block *gblock = &wblock->gblock;
727 if ((gblock->flags & ACPI_WMI_EVENT) && gblock->notify_id == event)
728 return get_event_data(wblock, out);
733 EXPORT_SYMBOL_GPL(wmi_get_event_data);
736 * wmi_has_guid - Check if a GUID is available
737 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
739 * Check if a given GUID is defined by _WDG.
741 * Return: True if GUID is available, false otherwise.
743 bool wmi_has_guid(const char *guid_string)
745 return ACPI_SUCCESS(find_guid(guid_string, NULL));
747 EXPORT_SYMBOL_GPL(wmi_has_guid);
750 * wmi_get_acpi_device_uid() - Get _UID name of ACPI device that defines GUID (deprecated)
751 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
753 * Find the _UID of ACPI device associated with this WMI GUID.
755 * Return: The ACPI _UID field value or NULL if the WMI GUID was not found.
757 char *wmi_get_acpi_device_uid(const char *guid_string)
759 struct wmi_block *wblock = NULL;
762 status = find_guid(guid_string, &wblock);
763 if (ACPI_FAILURE(status))
766 return acpi_device_uid(wblock->acpi_device);
768 EXPORT_SYMBOL_GPL(wmi_get_acpi_device_uid);
770 #define dev_to_wblock(__dev) container_of_const(__dev, struct wmi_block, dev.dev)
771 #define dev_to_wdev(__dev) container_of_const(__dev, struct wmi_device, dev)
773 static inline struct wmi_driver *drv_to_wdrv(struct device_driver *drv)
775 return container_of(drv, struct wmi_driver, driver);
781 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
784 struct wmi_block *wblock = dev_to_wblock(dev);
786 return sysfs_emit(buf, "wmi:%pUL\n", &wblock->gblock.guid);
788 static DEVICE_ATTR_RO(modalias);
790 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
793 struct wmi_block *wblock = dev_to_wblock(dev);
795 return sysfs_emit(buf, "%pUL\n", &wblock->gblock.guid);
797 static DEVICE_ATTR_RO(guid);
799 static ssize_t instance_count_show(struct device *dev,
800 struct device_attribute *attr, char *buf)
802 struct wmi_block *wblock = dev_to_wblock(dev);
804 return sysfs_emit(buf, "%d\n", (int)wblock->gblock.instance_count);
806 static DEVICE_ATTR_RO(instance_count);
808 static ssize_t expensive_show(struct device *dev,
809 struct device_attribute *attr, char *buf)
811 struct wmi_block *wblock = dev_to_wblock(dev);
813 return sysfs_emit(buf, "%d\n",
814 (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
816 static DEVICE_ATTR_RO(expensive);
818 static struct attribute *wmi_attrs[] = {
819 &dev_attr_modalias.attr,
821 &dev_attr_instance_count.attr,
822 &dev_attr_expensive.attr,
825 ATTRIBUTE_GROUPS(wmi);
827 static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
830 struct wmi_block *wblock = dev_to_wblock(dev);
832 return sysfs_emit(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
834 static DEVICE_ATTR_RO(notify_id);
836 static struct attribute *wmi_event_attrs[] = {
837 &dev_attr_notify_id.attr,
840 ATTRIBUTE_GROUPS(wmi_event);
842 static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
845 struct wmi_block *wblock = dev_to_wblock(dev);
847 return sysfs_emit(buf, "%c%c\n", wblock->gblock.object_id[0],
848 wblock->gblock.object_id[1]);
850 static DEVICE_ATTR_RO(object_id);
852 static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
855 struct wmi_device *wdev = dev_to_wdev(dev);
857 return sysfs_emit(buf, "%d\n", (int)wdev->setable);
859 static DEVICE_ATTR_RO(setable);
861 static struct attribute *wmi_data_attrs[] = {
862 &dev_attr_object_id.attr,
863 &dev_attr_setable.attr,
866 ATTRIBUTE_GROUPS(wmi_data);
868 static struct attribute *wmi_method_attrs[] = {
869 &dev_attr_object_id.attr,
872 ATTRIBUTE_GROUPS(wmi_method);
874 static int wmi_dev_uevent(const struct device *dev, struct kobj_uevent_env *env)
876 const struct wmi_block *wblock = dev_to_wblock(dev);
878 if (add_uevent_var(env, "MODALIAS=wmi:%pUL", &wblock->gblock.guid))
881 if (add_uevent_var(env, "WMI_GUID=%pUL", &wblock->gblock.guid))
887 static void wmi_dev_release(struct device *dev)
889 struct wmi_block *wblock = dev_to_wblock(dev);
894 static int wmi_dev_match(struct device *dev, struct device_driver *driver)
896 struct wmi_driver *wmi_driver = drv_to_wdrv(driver);
897 struct wmi_block *wblock = dev_to_wblock(dev);
898 const struct wmi_device_id *id = wmi_driver->id_table;
903 while (*id->guid_string) {
904 if (guid_parse_and_compare(id->guid_string, &wblock->gblock.guid))
912 static int wmi_char_open(struct inode *inode, struct file *filp)
914 const char *driver_name = filp->f_path.dentry->d_iname;
915 struct wmi_block *wblock;
916 struct wmi_block *next;
918 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
919 if (!wblock->dev.dev.driver)
921 if (strcmp(driver_name, wblock->dev.dev.driver->name) == 0) {
922 filp->private_data = wblock;
927 if (!filp->private_data)
930 return nonseekable_open(inode, filp);
933 static ssize_t wmi_char_read(struct file *filp, char __user *buffer,
934 size_t length, loff_t *offset)
936 struct wmi_block *wblock = filp->private_data;
938 return simple_read_from_buffer(buffer, length, offset,
939 &wblock->req_buf_size,
940 sizeof(wblock->req_buf_size));
943 static long wmi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
945 struct wmi_ioctl_buffer __user *input =
946 (struct wmi_ioctl_buffer __user *) arg;
947 struct wmi_block *wblock = filp->private_data;
948 struct wmi_ioctl_buffer *buf;
949 struct wmi_driver *wdriver;
952 if (_IOC_TYPE(cmd) != WMI_IOC)
955 /* make sure we're not calling a higher instance than exists*/
956 if (_IOC_NR(cmd) >= wblock->gblock.instance_count)
959 mutex_lock(&wblock->char_mutex);
960 buf = wblock->handler_data;
961 if (get_user(buf->length, &input->length)) {
962 dev_dbg(&wblock->dev.dev, "Read length from user failed\n");
966 /* if it's too small, abort */
967 if (buf->length < wblock->req_buf_size) {
968 dev_err(&wblock->dev.dev,
969 "Buffer %lld too small, need at least %lld\n",
970 buf->length, wblock->req_buf_size);
974 /* if it's too big, warn, driver will only use what is needed */
975 if (buf->length > wblock->req_buf_size)
976 dev_warn(&wblock->dev.dev,
977 "Buffer %lld is bigger than required %lld\n",
978 buf->length, wblock->req_buf_size);
980 /* copy the structure from userspace */
981 if (copy_from_user(buf, input, wblock->req_buf_size)) {
982 dev_dbg(&wblock->dev.dev, "Copy %llu from user failed\n",
983 wblock->req_buf_size);
988 /* let the driver do any filtering and do the call */
989 wdriver = drv_to_wdrv(wblock->dev.dev.driver);
990 if (!try_module_get(wdriver->driver.owner)) {
994 ret = wdriver->filter_callback(&wblock->dev, cmd, buf);
995 module_put(wdriver->driver.owner);
999 /* return the result (only up to our internal buffer size) */
1000 if (copy_to_user(input, buf, wblock->req_buf_size)) {
1001 dev_dbg(&wblock->dev.dev, "Copy %llu to user failed\n",
1002 wblock->req_buf_size);
1007 mutex_unlock(&wblock->char_mutex);
1011 static const struct file_operations wmi_fops = {
1012 .owner = THIS_MODULE,
1013 .read = wmi_char_read,
1014 .open = wmi_char_open,
1015 .unlocked_ioctl = wmi_ioctl,
1016 .compat_ioctl = compat_ptr_ioctl,
1019 static int wmi_dev_probe(struct device *dev)
1021 struct wmi_block *wblock = dev_to_wblock(dev);
1022 struct wmi_driver *wdriver = drv_to_wdrv(dev->driver);
1026 if (ACPI_FAILURE(wmi_method_enable(wblock, true)))
1027 dev_warn(dev, "failed to enable device -- probing anyway\n");
1029 if (wdriver->probe) {
1030 ret = wdriver->probe(dev_to_wdev(dev),
1031 find_guid_context(wblock, wdriver));
1036 /* driver wants a character device made */
1037 if (wdriver->filter_callback) {
1038 /* check that required buffer size declared by driver or MOF */
1039 if (!wblock->req_buf_size) {
1040 dev_err(&wblock->dev.dev,
1041 "Required buffer size not set\n");
1046 wblock->handler_data = kmalloc(wblock->req_buf_size,
1048 if (!wblock->handler_data) {
1053 buf = kasprintf(GFP_KERNEL, "wmi/%s", wdriver->driver.name);
1056 goto probe_string_failure;
1058 wblock->char_dev.minor = MISC_DYNAMIC_MINOR;
1059 wblock->char_dev.name = buf;
1060 wblock->char_dev.fops = &wmi_fops;
1061 wblock->char_dev.mode = 0444;
1062 ret = misc_register(&wblock->char_dev);
1064 dev_warn(dev, "failed to register char dev: %d\n", ret);
1066 goto probe_misc_failure;
1070 set_bit(WMI_PROBED, &wblock->flags);
1075 probe_string_failure:
1076 kfree(wblock->handler_data);
1078 if (ACPI_FAILURE(wmi_method_enable(wblock, false)))
1079 dev_warn(dev, "failed to disable device\n");
1083 static void wmi_dev_remove(struct device *dev)
1085 struct wmi_block *wblock = dev_to_wblock(dev);
1086 struct wmi_driver *wdriver = drv_to_wdrv(dev->driver);
1088 clear_bit(WMI_PROBED, &wblock->flags);
1090 if (wdriver->filter_callback) {
1091 misc_deregister(&wblock->char_dev);
1092 kfree(wblock->char_dev.name);
1093 kfree(wblock->handler_data);
1096 if (wdriver->remove)
1097 wdriver->remove(dev_to_wdev(dev));
1099 if (ACPI_FAILURE(wmi_method_enable(wblock, false)))
1100 dev_warn(dev, "failed to disable device\n");
1103 static struct class wmi_bus_class = {
1107 static struct bus_type wmi_bus_type = {
1109 .dev_groups = wmi_groups,
1110 .match = wmi_dev_match,
1111 .uevent = wmi_dev_uevent,
1112 .probe = wmi_dev_probe,
1113 .remove = wmi_dev_remove,
1116 static const struct device_type wmi_type_event = {
1118 .groups = wmi_event_groups,
1119 .release = wmi_dev_release,
1122 static const struct device_type wmi_type_method = {
1124 .groups = wmi_method_groups,
1125 .release = wmi_dev_release,
1128 static const struct device_type wmi_type_data = {
1130 .groups = wmi_data_groups,
1131 .release = wmi_dev_release,
1135 * _WDG is a static list that is only parsed at startup,
1136 * so it's safe to count entries without extra protection.
1138 static int guid_count(const guid_t *guid)
1140 struct wmi_block *wblock;
1143 list_for_each_entry(wblock, &wmi_block_list, list) {
1144 if (guid_equal(&wblock->gblock.guid, guid))
1151 static int wmi_create_device(struct device *wmi_bus_dev,
1152 struct wmi_block *wblock,
1153 struct acpi_device *device)
1155 struct acpi_device_info *info;
1156 char method[WMI_ACPI_METHOD_NAME_SIZE];
1160 if (wblock->gblock.flags & ACPI_WMI_EVENT) {
1161 wblock->dev.dev.type = &wmi_type_event;
1165 if (wblock->gblock.flags & ACPI_WMI_METHOD) {
1166 wblock->dev.dev.type = &wmi_type_method;
1167 mutex_init(&wblock->char_mutex);
1172 * Data Block Query Control Method (WQxx by convention) is
1173 * required per the WMI documentation. If it is not present,
1174 * we ignore this data block.
1176 get_acpi_method_name(wblock, 'Q', method);
1177 result = get_subobj_info(device->handle, method, &info);
1180 dev_warn(wmi_bus_dev,
1181 "%s data block query control method not found\n",
1186 wblock->dev.dev.type = &wmi_type_data;
1189 * The Microsoft documentation specifically states:
1191 * Data blocks registered with only a single instance
1192 * can ignore the parameter.
1194 * ACPICA will get mad at us if we call the method with the wrong number
1195 * of arguments, so check what our method expects. (On some Dell
1196 * laptops, WQxx may not be a method at all.)
1198 if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
1199 set_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags);
1203 get_acpi_method_name(wblock, 'S', method);
1204 result = get_subobj_info(device->handle, method, NULL);
1207 wblock->dev.setable = true;
1210 wblock->dev.dev.bus = &wmi_bus_type;
1211 wblock->dev.dev.parent = wmi_bus_dev;
1213 count = guid_count(&wblock->gblock.guid);
1215 dev_set_name(&wblock->dev.dev, "%pUL-%d", &wblock->gblock.guid, count);
1217 dev_set_name(&wblock->dev.dev, "%pUL", &wblock->gblock.guid);
1219 device_initialize(&wblock->dev.dev);
1224 static void wmi_free_devices(struct acpi_device *device)
1226 struct wmi_block *wblock, *next;
1228 /* Delete devices for all the GUIDs */
1229 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1230 if (wblock->acpi_device == device) {
1231 list_del(&wblock->list);
1232 device_unregister(&wblock->dev.dev);
1237 static bool guid_already_parsed_for_legacy(struct acpi_device *device, const guid_t *guid)
1239 struct wmi_block *wblock;
1241 list_for_each_entry(wblock, &wmi_block_list, list) {
1242 /* skip warning and register if we know the driver will use struct wmi_driver */
1243 for (int i = 0; allow_duplicates[i] != NULL; i++) {
1246 if (guid_parse(allow_duplicates[i], &tmp))
1248 if (guid_equal(&tmp, guid))
1251 if (guid_equal(&wblock->gblock.guid, guid)) {
1253 * Because we historically didn't track the relationship
1254 * between GUIDs and ACPI nodes, we don't know whether
1255 * we need to suppress GUIDs that are unique on a
1256 * given node but duplicated across nodes.
1258 dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
1259 guid, dev_name(&wblock->acpi_device->dev));
1268 * Parse the _WDG method for the GUID data blocks
1270 static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
1272 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
1273 const struct guid_block *gblock;
1274 struct wmi_block *wblock, *next;
1275 union acpi_object *obj;
1280 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
1281 if (ACPI_FAILURE(status))
1288 if (obj->type != ACPI_TYPE_BUFFER) {
1290 goto out_free_pointer;
1293 gblock = (const struct guid_block *)obj->buffer.pointer;
1294 total = obj->buffer.length / sizeof(struct guid_block);
1296 for (i = 0; i < total; i++) {
1298 wmi_dump_wdg(&gblock[i]);
1300 if (guid_already_parsed_for_legacy(device, &gblock[i].guid))
1303 wblock = kzalloc(sizeof(*wblock), GFP_KERNEL);
1309 wblock->acpi_device = device;
1310 wblock->gblock = gblock[i];
1312 retval = wmi_create_device(wmi_bus_dev, wblock, device);
1318 list_add_tail(&wblock->list, &wmi_block_list);
1321 wblock->handler = wmi_notify_debug;
1322 wmi_method_enable(wblock, true);
1327 * Now that all of the devices are created, add them to the
1328 * device tree and probe subdrivers.
1330 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1331 if (wblock->acpi_device != device)
1334 retval = device_add(&wblock->dev.dev);
1336 dev_err(wmi_bus_dev, "failed to register %pUL\n",
1337 &wblock->gblock.guid);
1339 wmi_method_enable(wblock, false);
1340 list_del(&wblock->list);
1341 put_device(&wblock->dev.dev);
1351 * WMI can have EmbeddedControl access regions. In which case, we just want to
1352 * hand these off to the EC driver.
1355 acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
1356 u32 bits, u64 *value,
1357 void *handler_context, void *region_context)
1359 int result = 0, i = 0;
1362 if ((address > 0xFF) || !value)
1363 return AE_BAD_PARAMETER;
1365 if (function != ACPI_READ && function != ACPI_WRITE)
1366 return AE_BAD_PARAMETER;
1369 return AE_BAD_PARAMETER;
1371 if (function == ACPI_READ) {
1372 result = ec_read(address, &temp);
1373 (*value) |= ((u64)temp) << i;
1375 temp = 0xff & ((*value) >> i);
1376 result = ec_write(address, temp);
1381 return AE_BAD_PARAMETER;
1383 return AE_NOT_FOUND;
1391 static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
1394 struct wmi_block *wblock = NULL, *iter;
1396 list_for_each_entry(iter, &wmi_block_list, list) {
1397 struct guid_block *block = &iter->gblock;
1399 if (iter->acpi_device->handle == handle &&
1400 (block->flags & ACPI_WMI_EVENT) &&
1401 (block->notify_id == event)) {
1410 /* If a driver is bound, then notify the driver. */
1411 if (test_bit(WMI_PROBED, &wblock->flags) && wblock->dev.dev.driver) {
1412 struct wmi_driver *driver = drv_to_wdrv(wblock->dev.dev.driver);
1413 struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
1416 if (!driver->no_notify_data) {
1417 status = get_event_data(wblock, &evdata);
1418 if (ACPI_FAILURE(status)) {
1419 dev_warn(&wblock->dev.dev, "failed to get event data\n");
1425 driver->notify(&wblock->dev, evdata.pointer);
1427 kfree(evdata.pointer);
1428 } else if (wblock->handler) {
1429 /* Legacy handler */
1430 wblock->handler(event, wblock->handler_data);
1434 pr_info("DEBUG: GUID %pUL event 0x%02X\n", &wblock->gblock.guid, event);
1436 acpi_bus_generate_netlink_event(
1437 wblock->acpi_device->pnp.device_class,
1438 dev_name(&wblock->dev.dev),
1442 static void acpi_wmi_remove(struct platform_device *device)
1444 struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
1446 acpi_remove_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY,
1447 acpi_wmi_notify_handler);
1448 acpi_remove_address_space_handler(acpi_device->handle,
1449 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
1450 wmi_free_devices(acpi_device);
1451 device_unregister(dev_get_drvdata(&device->dev));
1454 static int acpi_wmi_probe(struct platform_device *device)
1456 struct acpi_device *acpi_device;
1457 struct device *wmi_bus_dev;
1461 acpi_device = ACPI_COMPANION(&device->dev);
1463 dev_err(&device->dev, "ACPI companion is missing\n");
1467 status = acpi_install_address_space_handler(acpi_device->handle,
1469 &acpi_wmi_ec_space_handler,
1471 if (ACPI_FAILURE(status)) {
1472 dev_err(&device->dev, "Error installing EC region handler\n");
1476 status = acpi_install_notify_handler(acpi_device->handle,
1478 acpi_wmi_notify_handler,
1480 if (ACPI_FAILURE(status)) {
1481 dev_err(&device->dev, "Error installing notify handler\n");
1483 goto err_remove_ec_handler;
1486 wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
1487 NULL, "wmi_bus-%s", dev_name(&device->dev));
1488 if (IS_ERR(wmi_bus_dev)) {
1489 error = PTR_ERR(wmi_bus_dev);
1490 goto err_remove_notify_handler;
1492 dev_set_drvdata(&device->dev, wmi_bus_dev);
1494 error = parse_wdg(wmi_bus_dev, acpi_device);
1496 pr_err("Failed to parse WDG method\n");
1497 goto err_remove_busdev;
1503 device_unregister(wmi_bus_dev);
1505 err_remove_notify_handler:
1506 acpi_remove_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY,
1507 acpi_wmi_notify_handler);
1509 err_remove_ec_handler:
1510 acpi_remove_address_space_handler(acpi_device->handle,
1512 &acpi_wmi_ec_space_handler);
1517 int __must_check __wmi_driver_register(struct wmi_driver *driver,
1518 struct module *owner)
1520 driver->driver.owner = owner;
1521 driver->driver.bus = &wmi_bus_type;
1523 return driver_register(&driver->driver);
1525 EXPORT_SYMBOL(__wmi_driver_register);
1528 * wmi_driver_unregister() - Unregister a WMI driver
1529 * @driver: WMI driver to unregister
1531 * Unregisters a WMI driver from the WMI bus.
1533 void wmi_driver_unregister(struct wmi_driver *driver)
1535 driver_unregister(&driver->driver);
1537 EXPORT_SYMBOL(wmi_driver_unregister);
1539 static struct platform_driver acpi_wmi_driver = {
1542 .acpi_match_table = wmi_device_ids,
1544 .probe = acpi_wmi_probe,
1545 .remove_new = acpi_wmi_remove,
1548 static int __init acpi_wmi_init(void)
1555 error = class_register(&wmi_bus_class);
1559 error = bus_register(&wmi_bus_type);
1561 goto err_unreg_class;
1563 error = platform_driver_register(&acpi_wmi_driver);
1565 pr_err("Error loading mapper\n");
1572 bus_unregister(&wmi_bus_type);
1575 class_unregister(&wmi_bus_class);
1580 static void __exit acpi_wmi_exit(void)
1582 platform_driver_unregister(&acpi_wmi_driver);
1583 bus_unregister(&wmi_bus_type);
1584 class_unregister(&wmi_bus_class);
1587 subsys_initcall_sync(acpi_wmi_init);
1588 module_exit(acpi_wmi_exit);