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)
915 * The miscdevice already stores a pointer to itself
916 * inside filp->private_data
918 struct wmi_block *wblock = container_of(filp->private_data, struct wmi_block, char_dev);
920 filp->private_data = wblock;
922 return nonseekable_open(inode, filp);
925 static ssize_t wmi_char_read(struct file *filp, char __user *buffer,
926 size_t length, loff_t *offset)
928 struct wmi_block *wblock = filp->private_data;
930 return simple_read_from_buffer(buffer, length, offset,
931 &wblock->req_buf_size,
932 sizeof(wblock->req_buf_size));
935 static long wmi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
937 struct wmi_ioctl_buffer __user *input =
938 (struct wmi_ioctl_buffer __user *) arg;
939 struct wmi_block *wblock = filp->private_data;
940 struct wmi_ioctl_buffer *buf;
941 struct wmi_driver *wdriver;
944 if (_IOC_TYPE(cmd) != WMI_IOC)
947 /* make sure we're not calling a higher instance than exists*/
948 if (_IOC_NR(cmd) >= wblock->gblock.instance_count)
951 mutex_lock(&wblock->char_mutex);
952 buf = wblock->handler_data;
953 if (get_user(buf->length, &input->length)) {
954 dev_dbg(&wblock->dev.dev, "Read length from user failed\n");
958 /* if it's too small, abort */
959 if (buf->length < wblock->req_buf_size) {
960 dev_err(&wblock->dev.dev,
961 "Buffer %lld too small, need at least %lld\n",
962 buf->length, wblock->req_buf_size);
966 /* if it's too big, warn, driver will only use what is needed */
967 if (buf->length > wblock->req_buf_size)
968 dev_warn(&wblock->dev.dev,
969 "Buffer %lld is bigger than required %lld\n",
970 buf->length, wblock->req_buf_size);
972 /* copy the structure from userspace */
973 if (copy_from_user(buf, input, wblock->req_buf_size)) {
974 dev_dbg(&wblock->dev.dev, "Copy %llu from user failed\n",
975 wblock->req_buf_size);
980 /* let the driver do any filtering and do the call */
981 wdriver = drv_to_wdrv(wblock->dev.dev.driver);
982 if (!try_module_get(wdriver->driver.owner)) {
986 ret = wdriver->filter_callback(&wblock->dev, cmd, buf);
987 module_put(wdriver->driver.owner);
991 /* return the result (only up to our internal buffer size) */
992 if (copy_to_user(input, buf, wblock->req_buf_size)) {
993 dev_dbg(&wblock->dev.dev, "Copy %llu to user failed\n",
994 wblock->req_buf_size);
999 mutex_unlock(&wblock->char_mutex);
1003 static const struct file_operations wmi_fops = {
1004 .owner = THIS_MODULE,
1005 .read = wmi_char_read,
1006 .open = wmi_char_open,
1007 .unlocked_ioctl = wmi_ioctl,
1008 .compat_ioctl = compat_ptr_ioctl,
1011 static int wmi_dev_probe(struct device *dev)
1013 struct wmi_block *wblock = dev_to_wblock(dev);
1014 struct wmi_driver *wdriver = drv_to_wdrv(dev->driver);
1018 if (ACPI_FAILURE(wmi_method_enable(wblock, true)))
1019 dev_warn(dev, "failed to enable device -- probing anyway\n");
1021 if (wdriver->probe) {
1022 ret = wdriver->probe(dev_to_wdev(dev),
1023 find_guid_context(wblock, wdriver));
1028 /* driver wants a character device made */
1029 if (wdriver->filter_callback) {
1030 /* check that required buffer size declared by driver or MOF */
1031 if (!wblock->req_buf_size) {
1032 dev_err(&wblock->dev.dev,
1033 "Required buffer size not set\n");
1038 wblock->handler_data = kmalloc(wblock->req_buf_size,
1040 if (!wblock->handler_data) {
1045 buf = kasprintf(GFP_KERNEL, "wmi/%s", wdriver->driver.name);
1048 goto probe_string_failure;
1050 wblock->char_dev.minor = MISC_DYNAMIC_MINOR;
1051 wblock->char_dev.name = buf;
1052 wblock->char_dev.fops = &wmi_fops;
1053 wblock->char_dev.mode = 0444;
1054 ret = misc_register(&wblock->char_dev);
1056 dev_warn(dev, "failed to register char dev: %d\n", ret);
1058 goto probe_misc_failure;
1062 set_bit(WMI_PROBED, &wblock->flags);
1067 probe_string_failure:
1068 kfree(wblock->handler_data);
1070 if (ACPI_FAILURE(wmi_method_enable(wblock, false)))
1071 dev_warn(dev, "failed to disable device\n");
1075 static void wmi_dev_remove(struct device *dev)
1077 struct wmi_block *wblock = dev_to_wblock(dev);
1078 struct wmi_driver *wdriver = drv_to_wdrv(dev->driver);
1080 clear_bit(WMI_PROBED, &wblock->flags);
1082 if (wdriver->filter_callback) {
1083 misc_deregister(&wblock->char_dev);
1084 kfree(wblock->char_dev.name);
1085 kfree(wblock->handler_data);
1088 if (wdriver->remove)
1089 wdriver->remove(dev_to_wdev(dev));
1091 if (ACPI_FAILURE(wmi_method_enable(wblock, false)))
1092 dev_warn(dev, "failed to disable device\n");
1095 static struct class wmi_bus_class = {
1099 static struct bus_type wmi_bus_type = {
1101 .dev_groups = wmi_groups,
1102 .match = wmi_dev_match,
1103 .uevent = wmi_dev_uevent,
1104 .probe = wmi_dev_probe,
1105 .remove = wmi_dev_remove,
1108 static const struct device_type wmi_type_event = {
1110 .groups = wmi_event_groups,
1111 .release = wmi_dev_release,
1114 static const struct device_type wmi_type_method = {
1116 .groups = wmi_method_groups,
1117 .release = wmi_dev_release,
1120 static const struct device_type wmi_type_data = {
1122 .groups = wmi_data_groups,
1123 .release = wmi_dev_release,
1127 * _WDG is a static list that is only parsed at startup,
1128 * so it's safe to count entries without extra protection.
1130 static int guid_count(const guid_t *guid)
1132 struct wmi_block *wblock;
1135 list_for_each_entry(wblock, &wmi_block_list, list) {
1136 if (guid_equal(&wblock->gblock.guid, guid))
1143 static int wmi_create_device(struct device *wmi_bus_dev,
1144 struct wmi_block *wblock,
1145 struct acpi_device *device)
1147 struct acpi_device_info *info;
1148 char method[WMI_ACPI_METHOD_NAME_SIZE];
1152 if (wblock->gblock.flags & ACPI_WMI_EVENT) {
1153 wblock->dev.dev.type = &wmi_type_event;
1157 if (wblock->gblock.flags & ACPI_WMI_METHOD) {
1158 wblock->dev.dev.type = &wmi_type_method;
1159 mutex_init(&wblock->char_mutex);
1164 * Data Block Query Control Method (WQxx by convention) is
1165 * required per the WMI documentation. If it is not present,
1166 * we ignore this data block.
1168 get_acpi_method_name(wblock, 'Q', method);
1169 result = get_subobj_info(device->handle, method, &info);
1172 dev_warn(wmi_bus_dev,
1173 "%s data block query control method not found\n",
1178 wblock->dev.dev.type = &wmi_type_data;
1181 * The Microsoft documentation specifically states:
1183 * Data blocks registered with only a single instance
1184 * can ignore the parameter.
1186 * ACPICA will get mad at us if we call the method with the wrong number
1187 * of arguments, so check what our method expects. (On some Dell
1188 * laptops, WQxx may not be a method at all.)
1190 if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
1191 set_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags);
1195 get_acpi_method_name(wblock, 'S', method);
1196 result = get_subobj_info(device->handle, method, NULL);
1199 wblock->dev.setable = true;
1202 wblock->dev.dev.bus = &wmi_bus_type;
1203 wblock->dev.dev.parent = wmi_bus_dev;
1205 count = guid_count(&wblock->gblock.guid);
1207 dev_set_name(&wblock->dev.dev, "%pUL-%d", &wblock->gblock.guid, count);
1209 dev_set_name(&wblock->dev.dev, "%pUL", &wblock->gblock.guid);
1211 device_initialize(&wblock->dev.dev);
1216 static void wmi_free_devices(struct acpi_device *device)
1218 struct wmi_block *wblock, *next;
1220 /* Delete devices for all the GUIDs */
1221 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1222 if (wblock->acpi_device == device) {
1223 list_del(&wblock->list);
1224 device_unregister(&wblock->dev.dev);
1229 static bool guid_already_parsed_for_legacy(struct acpi_device *device, const guid_t *guid)
1231 struct wmi_block *wblock;
1233 list_for_each_entry(wblock, &wmi_block_list, list) {
1234 /* skip warning and register if we know the driver will use struct wmi_driver */
1235 for (int i = 0; allow_duplicates[i] != NULL; i++) {
1236 if (guid_parse_and_compare(allow_duplicates[i], guid))
1239 if (guid_equal(&wblock->gblock.guid, guid)) {
1241 * Because we historically didn't track the relationship
1242 * between GUIDs and ACPI nodes, we don't know whether
1243 * we need to suppress GUIDs that are unique on a
1244 * given node but duplicated across nodes.
1246 dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
1247 guid, dev_name(&wblock->acpi_device->dev));
1256 * Parse the _WDG method for the GUID data blocks
1258 static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
1260 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
1261 const struct guid_block *gblock;
1262 struct wmi_block *wblock, *next;
1263 union acpi_object *obj;
1268 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
1269 if (ACPI_FAILURE(status))
1276 if (obj->type != ACPI_TYPE_BUFFER) {
1281 gblock = (const struct guid_block *)obj->buffer.pointer;
1282 total = obj->buffer.length / sizeof(struct guid_block);
1284 for (i = 0; i < total; i++) {
1286 wmi_dump_wdg(&gblock[i]);
1288 if (!gblock[i].instance_count) {
1289 dev_info(wmi_bus_dev, FW_INFO "%pUL has zero instances\n", &gblock[i].guid);
1293 if (guid_already_parsed_for_legacy(device, &gblock[i].guid))
1296 wblock = kzalloc(sizeof(*wblock), GFP_KERNEL);
1298 dev_err(wmi_bus_dev, "Failed to allocate %pUL\n", &gblock[i].guid);
1302 wblock->acpi_device = device;
1303 wblock->gblock = gblock[i];
1305 retval = wmi_create_device(wmi_bus_dev, wblock, device);
1311 list_add_tail(&wblock->list, &wmi_block_list);
1314 wblock->handler = wmi_notify_debug;
1315 wmi_method_enable(wblock, true);
1320 * Now that all of the devices are created, add them to the
1321 * device tree and probe subdrivers.
1323 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1324 if (wblock->acpi_device != device)
1327 retval = device_add(&wblock->dev.dev);
1329 dev_err(wmi_bus_dev, "failed to register %pUL\n",
1330 &wblock->gblock.guid);
1332 wmi_method_enable(wblock, false);
1333 list_del(&wblock->list);
1334 put_device(&wblock->dev.dev);
1344 * WMI can have EmbeddedControl access regions. In which case, we just want to
1345 * hand these off to the EC driver.
1348 acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
1349 u32 bits, u64 *value,
1350 void *handler_context, void *region_context)
1352 int result = 0, i = 0;
1355 if ((address > 0xFF) || !value)
1356 return AE_BAD_PARAMETER;
1358 if (function != ACPI_READ && function != ACPI_WRITE)
1359 return AE_BAD_PARAMETER;
1362 return AE_BAD_PARAMETER;
1364 if (function == ACPI_READ) {
1365 result = ec_read(address, &temp);
1366 (*value) |= ((u64)temp) << i;
1368 temp = 0xff & ((*value) >> i);
1369 result = ec_write(address, temp);
1374 return AE_BAD_PARAMETER;
1376 return AE_NOT_FOUND;
1384 static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
1387 struct wmi_block *wblock = NULL, *iter;
1389 list_for_each_entry(iter, &wmi_block_list, list) {
1390 struct guid_block *block = &iter->gblock;
1392 if (iter->acpi_device->handle == handle &&
1393 (block->flags & ACPI_WMI_EVENT) &&
1394 (block->notify_id == event)) {
1403 /* If a driver is bound, then notify the driver. */
1404 if (test_bit(WMI_PROBED, &wblock->flags) && wblock->dev.dev.driver) {
1405 struct wmi_driver *driver = drv_to_wdrv(wblock->dev.dev.driver);
1406 struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
1409 if (!driver->no_notify_data) {
1410 status = get_event_data(wblock, &evdata);
1411 if (ACPI_FAILURE(status)) {
1412 dev_warn(&wblock->dev.dev, "failed to get event data\n");
1418 driver->notify(&wblock->dev, evdata.pointer);
1420 kfree(evdata.pointer);
1421 } else if (wblock->handler) {
1422 /* Legacy handler */
1423 wblock->handler(event, wblock->handler_data);
1427 pr_info("DEBUG: GUID %pUL event 0x%02X\n", &wblock->gblock.guid, event);
1429 acpi_bus_generate_netlink_event(
1430 wblock->acpi_device->pnp.device_class,
1431 dev_name(&wblock->dev.dev),
1435 static void acpi_wmi_remove(struct platform_device *device)
1437 struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
1439 acpi_remove_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY,
1440 acpi_wmi_notify_handler);
1441 acpi_remove_address_space_handler(acpi_device->handle,
1442 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
1443 wmi_free_devices(acpi_device);
1444 device_unregister(dev_get_drvdata(&device->dev));
1447 static int acpi_wmi_probe(struct platform_device *device)
1449 struct acpi_device *acpi_device;
1450 struct device *wmi_bus_dev;
1454 acpi_device = ACPI_COMPANION(&device->dev);
1456 dev_err(&device->dev, "ACPI companion is missing\n");
1460 status = acpi_install_address_space_handler(acpi_device->handle,
1462 &acpi_wmi_ec_space_handler,
1464 if (ACPI_FAILURE(status)) {
1465 dev_err(&device->dev, "Error installing EC region handler\n");
1469 status = acpi_install_notify_handler(acpi_device->handle,
1471 acpi_wmi_notify_handler,
1473 if (ACPI_FAILURE(status)) {
1474 dev_err(&device->dev, "Error installing notify handler\n");
1476 goto err_remove_ec_handler;
1479 wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
1480 NULL, "wmi_bus-%s", dev_name(&device->dev));
1481 if (IS_ERR(wmi_bus_dev)) {
1482 error = PTR_ERR(wmi_bus_dev);
1483 goto err_remove_notify_handler;
1485 dev_set_drvdata(&device->dev, wmi_bus_dev);
1487 error = parse_wdg(wmi_bus_dev, acpi_device);
1489 pr_err("Failed to parse WDG method\n");
1490 goto err_remove_busdev;
1496 device_unregister(wmi_bus_dev);
1498 err_remove_notify_handler:
1499 acpi_remove_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY,
1500 acpi_wmi_notify_handler);
1502 err_remove_ec_handler:
1503 acpi_remove_address_space_handler(acpi_device->handle,
1505 &acpi_wmi_ec_space_handler);
1510 int __must_check __wmi_driver_register(struct wmi_driver *driver,
1511 struct module *owner)
1513 driver->driver.owner = owner;
1514 driver->driver.bus = &wmi_bus_type;
1516 return driver_register(&driver->driver);
1518 EXPORT_SYMBOL(__wmi_driver_register);
1521 * wmi_driver_unregister() - Unregister a WMI driver
1522 * @driver: WMI driver to unregister
1524 * Unregisters a WMI driver from the WMI bus.
1526 void wmi_driver_unregister(struct wmi_driver *driver)
1528 driver_unregister(&driver->driver);
1530 EXPORT_SYMBOL(wmi_driver_unregister);
1532 static struct platform_driver acpi_wmi_driver = {
1535 .acpi_match_table = wmi_device_ids,
1537 .probe = acpi_wmi_probe,
1538 .remove_new = acpi_wmi_remove,
1541 static int __init acpi_wmi_init(void)
1548 error = class_register(&wmi_bus_class);
1552 error = bus_register(&wmi_bus_type);
1554 goto err_unreg_class;
1556 error = platform_driver_register(&acpi_wmi_driver);
1558 pr_err("Error loading mapper\n");
1565 bus_unregister(&wmi_bus_type);
1568 class_unregister(&wmi_bus_class);
1573 static void __exit acpi_wmi_exit(void)
1575 platform_driver_unregister(&acpi_wmi_driver);
1576 bus_unregister(&wmi_bus_type);
1577 class_unregister(&wmi_bus_class);
1580 subsys_initcall_sync(acpi_wmi_init);
1581 module_exit(acpi_wmi_exit);