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 */
112 #define dev_to_wblock(__dev) container_of_const(__dev, struct wmi_block, dev.dev)
113 #define dev_to_wdev(__dev) container_of_const(__dev, struct wmi_device, dev)
116 * GUID parsing functions
119 static bool guid_parse_and_compare(const char *string, const guid_t *guid)
123 if (guid_parse(string, &guid_input))
126 return guid_equal(&guid_input, guid);
129 static const void *find_guid_context(struct wmi_block *wblock,
130 struct wmi_driver *wdriver)
132 const struct wmi_device_id *id;
134 id = wdriver->id_table;
138 while (*id->guid_string) {
139 if (guid_parse_and_compare(id->guid_string, &wblock->gblock.guid))
146 static int get_subobj_info(acpi_handle handle, const char *pathname,
147 struct acpi_device_info **info)
149 struct acpi_device_info *dummy_info, **info_ptr;
150 acpi_handle subobj_handle;
153 status = acpi_get_handle(handle, (char *)pathname, &subobj_handle);
154 if (status == AE_NOT_FOUND)
156 else if (ACPI_FAILURE(status))
159 info_ptr = info ? info : &dummy_info;
160 status = acpi_get_object_info(subobj_handle, info_ptr);
161 if (ACPI_FAILURE(status))
170 static acpi_status wmi_method_enable(struct wmi_block *wblock, bool enable)
172 struct guid_block *block;
177 block = &wblock->gblock;
178 handle = wblock->acpi_device->handle;
180 snprintf(method, 5, "WE%02X", block->notify_id);
181 status = acpi_execute_simple_method(handle, method, enable);
182 if (status == AE_NOT_FOUND)
188 #define WMI_ACPI_METHOD_NAME_SIZE 5
190 static inline void get_acpi_method_name(const struct wmi_block *wblock,
192 char buffer[static WMI_ACPI_METHOD_NAME_SIZE])
194 static_assert(ARRAY_SIZE(wblock->gblock.object_id) == 2);
195 static_assert(WMI_ACPI_METHOD_NAME_SIZE >= 5);
199 buffer[2] = wblock->gblock.object_id[0];
200 buffer[3] = wblock->gblock.object_id[1];
204 static inline acpi_object_type get_param_acpi_type(const struct wmi_block *wblock)
206 if (wblock->gblock.flags & ACPI_WMI_STRING)
207 return ACPI_TYPE_STRING;
209 return ACPI_TYPE_BUFFER;
212 static acpi_status get_event_data(const struct wmi_block *wblock, struct acpi_buffer *out)
214 union acpi_object param = {
216 .type = ACPI_TYPE_INTEGER,
217 .value = wblock->gblock.notify_id,
220 struct acpi_object_list input = {
225 return acpi_evaluate_object(wblock->acpi_device->handle, "_WED", &input, out);
228 static int wmidev_match_guid(struct device *dev, const void *data)
230 struct wmi_block *wblock = dev_to_wblock(dev);
231 const guid_t *guid = data;
233 if (guid_equal(guid, &wblock->gblock.guid))
239 static struct bus_type wmi_bus_type;
241 static struct wmi_device *wmi_find_device_by_guid(const char *guid_string)
247 ret = guid_parse(guid_string, &guid);
251 dev = bus_find_device(&wmi_bus_type, NULL, &guid, wmidev_match_guid);
253 return ERR_PTR(-ENODEV);
255 return dev_to_wdev(dev);
258 static void wmi_device_put(struct wmi_device *wdev)
260 put_device(&wdev->dev);
264 * Exported WMI functions
268 * set_required_buffer_size - Sets the buffer size needed for performing IOCTL
269 * @wdev: A wmi bus device from a driver
270 * @length: Required buffer size
272 * Allocates memory needed for buffer, stores the buffer size in that memory.
274 * Return: 0 on success or a negative error code for failure.
276 int set_required_buffer_size(struct wmi_device *wdev, u64 length)
278 struct wmi_block *wblock;
280 wblock = container_of(wdev, struct wmi_block, dev);
281 wblock->req_buf_size = length;
285 EXPORT_SYMBOL_GPL(set_required_buffer_size);
288 * wmi_instance_count - Get number of WMI object instances
289 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
291 * Get the number of WMI object instances.
293 * Returns: Number of WMI object instances or negative error code.
295 int wmi_instance_count(const char *guid_string)
297 struct wmi_device *wdev;
300 wdev = wmi_find_device_by_guid(guid_string);
302 return PTR_ERR(wdev);
304 ret = wmidev_instance_count(wdev);
305 wmi_device_put(wdev);
309 EXPORT_SYMBOL_GPL(wmi_instance_count);
312 * wmidev_instance_count - Get number of WMI object instances
313 * @wdev: A wmi bus device from a driver
315 * Get the number of WMI object instances.
317 * Returns: Number of WMI object instances.
319 u8 wmidev_instance_count(struct wmi_device *wdev)
321 struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
323 return wblock->gblock.instance_count;
325 EXPORT_SYMBOL_GPL(wmidev_instance_count);
328 * wmi_evaluate_method - Evaluate a WMI method (deprecated)
329 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
330 * @instance: Instance index
331 * @method_id: Method ID to call
332 * @in: Buffer containing input for the method call
333 * @out: Empty buffer to return the method results
335 * Call an ACPI-WMI method, the caller must free @out.
337 * Return: acpi_status signaling success or error.
339 acpi_status wmi_evaluate_method(const char *guid_string, u8 instance, u32 method_id,
340 const struct acpi_buffer *in, struct acpi_buffer *out)
342 struct wmi_device *wdev;
345 wdev = wmi_find_device_by_guid(guid_string);
349 status = wmidev_evaluate_method(wdev, instance, method_id, in, out);
351 wmi_device_put(wdev);
355 EXPORT_SYMBOL_GPL(wmi_evaluate_method);
358 * wmidev_evaluate_method - Evaluate a WMI method
359 * @wdev: A wmi bus device from a driver
360 * @instance: Instance index
361 * @method_id: Method ID to call
362 * @in: Buffer containing input for the method call
363 * @out: Empty buffer to return the method results
365 * Call an ACPI-WMI method, the caller must free @out.
367 * Return: acpi_status signaling success or error.
369 acpi_status wmidev_evaluate_method(struct wmi_device *wdev, u8 instance, u32 method_id,
370 const struct acpi_buffer *in, struct acpi_buffer *out)
372 struct guid_block *block;
373 struct wmi_block *wblock;
375 struct acpi_object_list input;
376 union acpi_object params[3];
377 char method[WMI_ACPI_METHOD_NAME_SIZE];
379 wblock = container_of(wdev, struct wmi_block, dev);
380 block = &wblock->gblock;
381 handle = wblock->acpi_device->handle;
383 if (!(block->flags & ACPI_WMI_METHOD))
386 if (block->instance_count <= instance)
387 return AE_BAD_PARAMETER;
390 input.pointer = params;
391 params[0].type = ACPI_TYPE_INTEGER;
392 params[0].integer.value = instance;
393 params[1].type = ACPI_TYPE_INTEGER;
394 params[1].integer.value = method_id;
399 params[2].type = get_param_acpi_type(wblock);
400 params[2].buffer.length = in->length;
401 params[2].buffer.pointer = in->pointer;
404 get_acpi_method_name(wblock, 'M', method);
406 return acpi_evaluate_object(handle, method, &input, out);
408 EXPORT_SYMBOL_GPL(wmidev_evaluate_method);
410 static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
411 struct acpi_buffer *out)
413 struct guid_block *block;
415 acpi_status status, wc_status = AE_ERROR;
416 struct acpi_object_list input;
417 union acpi_object wq_params[1];
418 char wc_method[WMI_ACPI_METHOD_NAME_SIZE];
419 char method[WMI_ACPI_METHOD_NAME_SIZE];
422 return AE_BAD_PARAMETER;
424 block = &wblock->gblock;
425 handle = wblock->acpi_device->handle;
427 if (block->instance_count <= instance)
428 return AE_BAD_PARAMETER;
430 /* Check GUID is a data block */
431 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
435 input.pointer = wq_params;
436 wq_params[0].type = ACPI_TYPE_INTEGER;
437 wq_params[0].integer.value = instance;
439 if (instance == 0 && test_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags))
443 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
446 if (block->flags & ACPI_WMI_EXPENSIVE) {
447 get_acpi_method_name(wblock, 'C', wc_method);
450 * Some GUIDs break the specification by declaring themselves
451 * expensive, but have no corresponding WCxx method. So we
452 * should not fail if this happens.
454 wc_status = acpi_execute_simple_method(handle, wc_method, 1);
457 get_acpi_method_name(wblock, 'Q', method);
458 status = acpi_evaluate_object(handle, method, &input, out);
461 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
462 * the WQxx method failed - we should disable collection anyway.
464 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
466 * Ignore whether this WCxx call succeeds or not since
467 * the previously executed WQxx method call might have
468 * succeeded, and returning the failing status code
469 * of this call would throw away the result of the WQxx
470 * call, potentially leaking memory.
472 acpi_execute_simple_method(handle, wc_method, 0);
479 * wmi_query_block - Return contents of a WMI block (deprecated)
480 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
481 * @instance: Instance index
482 * @out: Empty buffer to return the contents of the data block to
484 * Query a ACPI-WMI block, the caller must free @out.
486 * Return: ACPI object containing the content of the WMI block.
488 acpi_status wmi_query_block(const char *guid_string, u8 instance,
489 struct acpi_buffer *out)
491 struct wmi_block *wblock;
492 struct wmi_device *wdev;
495 wdev = wmi_find_device_by_guid(guid_string);
499 wblock = container_of(wdev, struct wmi_block, dev);
500 status = __query_block(wblock, instance, out);
502 wmi_device_put(wdev);
506 EXPORT_SYMBOL_GPL(wmi_query_block);
509 * wmidev_block_query - Return contents of a WMI block
510 * @wdev: A wmi bus device from a driver
511 * @instance: Instance index
513 * Query an ACPI-WMI block, the caller must free the result.
515 * Return: ACPI object containing the content of the WMI block.
517 union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance)
519 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
520 struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
522 if (ACPI_FAILURE(__query_block(wblock, instance, &out)))
527 EXPORT_SYMBOL_GPL(wmidev_block_query);
530 * wmi_set_block - Write to a WMI block (deprecated)
531 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
532 * @instance: Instance index
533 * @in: Buffer containing new values for the data block
535 * Write the contents of the input buffer to an ACPI-WMI data block.
537 * Return: acpi_status signaling success or error.
539 acpi_status wmi_set_block(const char *guid_string, u8 instance,
540 const struct acpi_buffer *in)
542 struct wmi_block *wblock;
543 struct guid_block *block;
544 struct wmi_device *wdev;
546 struct acpi_object_list input;
547 union acpi_object params[2];
548 char method[WMI_ACPI_METHOD_NAME_SIZE];
554 wdev = wmi_find_device_by_guid(guid_string);
558 wblock = container_of(wdev, struct wmi_block, dev);
559 block = &wblock->gblock;
560 handle = wblock->acpi_device->handle;
562 if (block->instance_count <= instance) {
563 status = AE_BAD_PARAMETER;
568 /* Check GUID is a data block */
569 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD)) {
576 input.pointer = params;
577 params[0].type = ACPI_TYPE_INTEGER;
578 params[0].integer.value = instance;
579 params[1].type = get_param_acpi_type(wblock);
580 params[1].buffer.length = in->length;
581 params[1].buffer.pointer = in->pointer;
583 get_acpi_method_name(wblock, 'S', method);
585 status = acpi_evaluate_object(handle, method, &input, NULL);
588 wmi_device_put(wdev);
592 EXPORT_SYMBOL_GPL(wmi_set_block);
594 static void wmi_dump_wdg(const struct guid_block *g)
596 pr_info("%pUL:\n", &g->guid);
597 if (g->flags & ACPI_WMI_EVENT)
598 pr_info("\tnotify_id: 0x%02X\n", g->notify_id);
600 pr_info("\tobject_id: %2pE\n", g->object_id);
601 pr_info("\tinstance_count: %d\n", g->instance_count);
602 pr_info("\tflags: %#x", g->flags);
604 if (g->flags & ACPI_WMI_EXPENSIVE)
605 pr_cont(" ACPI_WMI_EXPENSIVE");
606 if (g->flags & ACPI_WMI_METHOD)
607 pr_cont(" ACPI_WMI_METHOD");
608 if (g->flags & ACPI_WMI_STRING)
609 pr_cont(" ACPI_WMI_STRING");
610 if (g->flags & ACPI_WMI_EVENT)
611 pr_cont(" ACPI_WMI_EVENT");
617 static void wmi_notify_debug(u32 value, void *context)
619 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
620 union acpi_object *obj;
623 status = wmi_get_event_data(value, &response);
624 if (status != AE_OK) {
625 pr_info("bad event status 0x%x\n", status);
629 obj = response.pointer;
633 pr_info("DEBUG: event 0x%02X ", value);
635 case ACPI_TYPE_BUFFER:
636 pr_cont("BUFFER_TYPE - length %u\n", obj->buffer.length);
638 case ACPI_TYPE_STRING:
639 pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
641 case ACPI_TYPE_INTEGER:
642 pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
644 case ACPI_TYPE_PACKAGE:
645 pr_cont("PACKAGE_TYPE - %u elements\n", obj->package.count);
648 pr_cont("object type 0x%X\n", obj->type);
654 * wmi_install_notify_handler - Register handler for WMI events (deprecated)
655 * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
656 * @handler: Function to handle notifications
657 * @data: Data to be returned to handler when event is fired
659 * Register a handler for events sent to the ACPI-WMI mapper device.
661 * Return: acpi_status signaling success or error.
663 acpi_status wmi_install_notify_handler(const char *guid,
664 wmi_notify_handler handler,
667 struct wmi_block *block;
668 acpi_status status = AE_NOT_EXIST;
671 if (!guid || !handler)
672 return AE_BAD_PARAMETER;
674 if (guid_parse(guid, &guid_input))
675 return AE_BAD_PARAMETER;
677 list_for_each_entry(block, &wmi_block_list, list) {
678 acpi_status wmi_status;
680 if (guid_equal(&block->gblock.guid, &guid_input)) {
681 if (block->handler &&
682 block->handler != wmi_notify_debug)
683 return AE_ALREADY_ACQUIRED;
685 block->handler = handler;
686 block->handler_data = data;
688 wmi_status = wmi_method_enable(block, true);
689 if ((wmi_status != AE_OK) ||
690 ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
697 EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
700 * wmi_remove_notify_handler - Unregister handler for WMI events (deprecated)
701 * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
703 * Unregister handler for events sent to the ACPI-WMI mapper device.
705 * Return: acpi_status signaling success or error.
707 acpi_status wmi_remove_notify_handler(const char *guid)
709 struct wmi_block *block;
710 acpi_status status = AE_NOT_EXIST;
714 return AE_BAD_PARAMETER;
716 if (guid_parse(guid, &guid_input))
717 return AE_BAD_PARAMETER;
719 list_for_each_entry(block, &wmi_block_list, list) {
720 acpi_status wmi_status;
722 if (guid_equal(&block->gblock.guid, &guid_input)) {
723 if (!block->handler ||
724 block->handler == wmi_notify_debug)
725 return AE_NULL_ENTRY;
728 block->handler = wmi_notify_debug;
731 wmi_status = wmi_method_enable(block, false);
732 block->handler = NULL;
733 block->handler_data = NULL;
734 if ((wmi_status != AE_OK) ||
735 ((wmi_status == AE_OK) &&
736 (status == AE_NOT_EXIST)))
744 EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
747 * wmi_get_event_data - Get WMI data associated with an event (deprecated)
749 * @event: Event to find
750 * @out: Buffer to hold event data
752 * Get extra data associated with an WMI event, the caller needs to free @out.
754 * Return: acpi_status signaling success or error.
756 acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
758 struct wmi_block *wblock;
760 list_for_each_entry(wblock, &wmi_block_list, list) {
761 struct guid_block *gblock = &wblock->gblock;
763 if ((gblock->flags & ACPI_WMI_EVENT) && gblock->notify_id == event)
764 return get_event_data(wblock, out);
769 EXPORT_SYMBOL_GPL(wmi_get_event_data);
772 * wmi_has_guid - Check if a GUID is available
773 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
775 * Check if a given GUID is defined by _WDG.
777 * Return: True if GUID is available, false otherwise.
779 bool wmi_has_guid(const char *guid_string)
781 struct wmi_device *wdev;
783 wdev = wmi_find_device_by_guid(guid_string);
787 wmi_device_put(wdev);
791 EXPORT_SYMBOL_GPL(wmi_has_guid);
794 * wmi_get_acpi_device_uid() - Get _UID name of ACPI device that defines GUID (deprecated)
795 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
797 * Find the _UID of ACPI device associated with this WMI GUID.
799 * Return: The ACPI _UID field value or NULL if the WMI GUID was not found.
801 char *wmi_get_acpi_device_uid(const char *guid_string)
803 struct wmi_block *wblock;
804 struct wmi_device *wdev;
807 wdev = wmi_find_device_by_guid(guid_string);
811 wblock = container_of(wdev, struct wmi_block, dev);
812 uid = acpi_device_uid(wblock->acpi_device);
814 wmi_device_put(wdev);
818 EXPORT_SYMBOL_GPL(wmi_get_acpi_device_uid);
820 static inline struct wmi_driver *drv_to_wdrv(struct device_driver *drv)
822 return container_of(drv, struct wmi_driver, driver);
828 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
831 struct wmi_block *wblock = dev_to_wblock(dev);
833 return sysfs_emit(buf, "wmi:%pUL\n", &wblock->gblock.guid);
835 static DEVICE_ATTR_RO(modalias);
837 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
840 struct wmi_block *wblock = dev_to_wblock(dev);
842 return sysfs_emit(buf, "%pUL\n", &wblock->gblock.guid);
844 static DEVICE_ATTR_RO(guid);
846 static ssize_t instance_count_show(struct device *dev,
847 struct device_attribute *attr, char *buf)
849 struct wmi_block *wblock = dev_to_wblock(dev);
851 return sysfs_emit(buf, "%d\n", (int)wblock->gblock.instance_count);
853 static DEVICE_ATTR_RO(instance_count);
855 static ssize_t expensive_show(struct device *dev,
856 struct device_attribute *attr, char *buf)
858 struct wmi_block *wblock = dev_to_wblock(dev);
860 return sysfs_emit(buf, "%d\n",
861 (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
863 static DEVICE_ATTR_RO(expensive);
865 static struct attribute *wmi_attrs[] = {
866 &dev_attr_modalias.attr,
868 &dev_attr_instance_count.attr,
869 &dev_attr_expensive.attr,
872 ATTRIBUTE_GROUPS(wmi);
874 static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
877 struct wmi_block *wblock = dev_to_wblock(dev);
879 return sysfs_emit(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
881 static DEVICE_ATTR_RO(notify_id);
883 static struct attribute *wmi_event_attrs[] = {
884 &dev_attr_notify_id.attr,
887 ATTRIBUTE_GROUPS(wmi_event);
889 static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
892 struct wmi_block *wblock = dev_to_wblock(dev);
894 return sysfs_emit(buf, "%c%c\n", wblock->gblock.object_id[0],
895 wblock->gblock.object_id[1]);
897 static DEVICE_ATTR_RO(object_id);
899 static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
902 struct wmi_device *wdev = dev_to_wdev(dev);
904 return sysfs_emit(buf, "%d\n", (int)wdev->setable);
906 static DEVICE_ATTR_RO(setable);
908 static struct attribute *wmi_data_attrs[] = {
909 &dev_attr_object_id.attr,
910 &dev_attr_setable.attr,
913 ATTRIBUTE_GROUPS(wmi_data);
915 static struct attribute *wmi_method_attrs[] = {
916 &dev_attr_object_id.attr,
919 ATTRIBUTE_GROUPS(wmi_method);
921 static int wmi_dev_uevent(const struct device *dev, struct kobj_uevent_env *env)
923 const struct wmi_block *wblock = dev_to_wblock(dev);
925 if (add_uevent_var(env, "MODALIAS=wmi:%pUL", &wblock->gblock.guid))
928 if (add_uevent_var(env, "WMI_GUID=%pUL", &wblock->gblock.guid))
934 static void wmi_dev_release(struct device *dev)
936 struct wmi_block *wblock = dev_to_wblock(dev);
941 static int wmi_dev_match(struct device *dev, struct device_driver *driver)
943 struct wmi_driver *wmi_driver = drv_to_wdrv(driver);
944 struct wmi_block *wblock = dev_to_wblock(dev);
945 const struct wmi_device_id *id = wmi_driver->id_table;
950 while (*id->guid_string) {
951 if (guid_parse_and_compare(id->guid_string, &wblock->gblock.guid))
959 static int wmi_char_open(struct inode *inode, struct file *filp)
962 * The miscdevice already stores a pointer to itself
963 * inside filp->private_data
965 struct wmi_block *wblock = container_of(filp->private_data, struct wmi_block, char_dev);
967 filp->private_data = wblock;
969 return nonseekable_open(inode, filp);
972 static ssize_t wmi_char_read(struct file *filp, char __user *buffer,
973 size_t length, loff_t *offset)
975 struct wmi_block *wblock = filp->private_data;
977 return simple_read_from_buffer(buffer, length, offset,
978 &wblock->req_buf_size,
979 sizeof(wblock->req_buf_size));
982 static long wmi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
984 struct wmi_ioctl_buffer __user *input =
985 (struct wmi_ioctl_buffer __user *) arg;
986 struct wmi_block *wblock = filp->private_data;
987 struct wmi_ioctl_buffer *buf;
988 struct wmi_driver *wdriver;
991 if (_IOC_TYPE(cmd) != WMI_IOC)
994 /* make sure we're not calling a higher instance than exists*/
995 if (_IOC_NR(cmd) >= wblock->gblock.instance_count)
998 mutex_lock(&wblock->char_mutex);
999 buf = wblock->handler_data;
1000 if (get_user(buf->length, &input->length)) {
1001 dev_dbg(&wblock->dev.dev, "Read length from user failed\n");
1005 /* if it's too small, abort */
1006 if (buf->length < wblock->req_buf_size) {
1007 dev_err(&wblock->dev.dev,
1008 "Buffer %lld too small, need at least %lld\n",
1009 buf->length, wblock->req_buf_size);
1013 /* if it's too big, warn, driver will only use what is needed */
1014 if (buf->length > wblock->req_buf_size)
1015 dev_warn(&wblock->dev.dev,
1016 "Buffer %lld is bigger than required %lld\n",
1017 buf->length, wblock->req_buf_size);
1019 /* copy the structure from userspace */
1020 if (copy_from_user(buf, input, wblock->req_buf_size)) {
1021 dev_dbg(&wblock->dev.dev, "Copy %llu from user failed\n",
1022 wblock->req_buf_size);
1027 /* let the driver do any filtering and do the call */
1028 wdriver = drv_to_wdrv(wblock->dev.dev.driver);
1029 if (!try_module_get(wdriver->driver.owner)) {
1033 ret = wdriver->filter_callback(&wblock->dev, cmd, buf);
1034 module_put(wdriver->driver.owner);
1038 /* return the result (only up to our internal buffer size) */
1039 if (copy_to_user(input, buf, wblock->req_buf_size)) {
1040 dev_dbg(&wblock->dev.dev, "Copy %llu to user failed\n",
1041 wblock->req_buf_size);
1046 mutex_unlock(&wblock->char_mutex);
1050 static const struct file_operations wmi_fops = {
1051 .owner = THIS_MODULE,
1052 .read = wmi_char_read,
1053 .open = wmi_char_open,
1054 .unlocked_ioctl = wmi_ioctl,
1055 .compat_ioctl = compat_ptr_ioctl,
1058 static int wmi_dev_probe(struct device *dev)
1060 struct wmi_block *wblock = dev_to_wblock(dev);
1061 struct wmi_driver *wdriver = drv_to_wdrv(dev->driver);
1065 if (ACPI_FAILURE(wmi_method_enable(wblock, true)))
1066 dev_warn(dev, "failed to enable device -- probing anyway\n");
1068 if (wdriver->probe) {
1069 ret = wdriver->probe(dev_to_wdev(dev),
1070 find_guid_context(wblock, wdriver));
1075 /* driver wants a character device made */
1076 if (wdriver->filter_callback) {
1077 /* check that required buffer size declared by driver or MOF */
1078 if (!wblock->req_buf_size) {
1079 dev_err(&wblock->dev.dev,
1080 "Required buffer size not set\n");
1085 wblock->handler_data = kmalloc(wblock->req_buf_size,
1087 if (!wblock->handler_data) {
1092 buf = kasprintf(GFP_KERNEL, "wmi/%s", wdriver->driver.name);
1095 goto probe_string_failure;
1097 wblock->char_dev.minor = MISC_DYNAMIC_MINOR;
1098 wblock->char_dev.name = buf;
1099 wblock->char_dev.fops = &wmi_fops;
1100 wblock->char_dev.mode = 0444;
1101 ret = misc_register(&wblock->char_dev);
1103 dev_warn(dev, "failed to register char dev: %d\n", ret);
1105 goto probe_misc_failure;
1109 set_bit(WMI_PROBED, &wblock->flags);
1114 probe_string_failure:
1115 kfree(wblock->handler_data);
1117 if (ACPI_FAILURE(wmi_method_enable(wblock, false)))
1118 dev_warn(dev, "failed to disable device\n");
1122 static void wmi_dev_remove(struct device *dev)
1124 struct wmi_block *wblock = dev_to_wblock(dev);
1125 struct wmi_driver *wdriver = drv_to_wdrv(dev->driver);
1127 clear_bit(WMI_PROBED, &wblock->flags);
1129 if (wdriver->filter_callback) {
1130 misc_deregister(&wblock->char_dev);
1131 kfree(wblock->char_dev.name);
1132 kfree(wblock->handler_data);
1135 if (wdriver->remove)
1136 wdriver->remove(dev_to_wdev(dev));
1138 if (ACPI_FAILURE(wmi_method_enable(wblock, false)))
1139 dev_warn(dev, "failed to disable device\n");
1142 static struct class wmi_bus_class = {
1146 static struct bus_type wmi_bus_type = {
1148 .dev_groups = wmi_groups,
1149 .match = wmi_dev_match,
1150 .uevent = wmi_dev_uevent,
1151 .probe = wmi_dev_probe,
1152 .remove = wmi_dev_remove,
1155 static const struct device_type wmi_type_event = {
1157 .groups = wmi_event_groups,
1158 .release = wmi_dev_release,
1161 static const struct device_type wmi_type_method = {
1163 .groups = wmi_method_groups,
1164 .release = wmi_dev_release,
1167 static const struct device_type wmi_type_data = {
1169 .groups = wmi_data_groups,
1170 .release = wmi_dev_release,
1174 * _WDG is a static list that is only parsed at startup,
1175 * so it's safe to count entries without extra protection.
1177 static int guid_count(const guid_t *guid)
1179 struct wmi_block *wblock;
1182 list_for_each_entry(wblock, &wmi_block_list, list) {
1183 if (guid_equal(&wblock->gblock.guid, guid))
1190 static int wmi_create_device(struct device *wmi_bus_dev,
1191 struct wmi_block *wblock,
1192 struct acpi_device *device)
1194 struct acpi_device_info *info;
1195 char method[WMI_ACPI_METHOD_NAME_SIZE];
1199 if (wblock->gblock.flags & ACPI_WMI_EVENT) {
1200 wblock->dev.dev.type = &wmi_type_event;
1204 if (wblock->gblock.flags & ACPI_WMI_METHOD) {
1205 wblock->dev.dev.type = &wmi_type_method;
1206 mutex_init(&wblock->char_mutex);
1211 * Data Block Query Control Method (WQxx by convention) is
1212 * required per the WMI documentation. If it is not present,
1213 * we ignore this data block.
1215 get_acpi_method_name(wblock, 'Q', method);
1216 result = get_subobj_info(device->handle, method, &info);
1219 dev_warn(wmi_bus_dev,
1220 "%s data block query control method not found\n",
1225 wblock->dev.dev.type = &wmi_type_data;
1228 * The Microsoft documentation specifically states:
1230 * Data blocks registered with only a single instance
1231 * can ignore the parameter.
1233 * ACPICA will get mad at us if we call the method with the wrong number
1234 * of arguments, so check what our method expects. (On some Dell
1235 * laptops, WQxx may not be a method at all.)
1237 if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
1238 set_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags);
1242 get_acpi_method_name(wblock, 'S', method);
1243 result = get_subobj_info(device->handle, method, NULL);
1246 wblock->dev.setable = true;
1249 wblock->dev.dev.bus = &wmi_bus_type;
1250 wblock->dev.dev.parent = wmi_bus_dev;
1252 count = guid_count(&wblock->gblock.guid);
1254 dev_set_name(&wblock->dev.dev, "%pUL-%d", &wblock->gblock.guid, count);
1256 dev_set_name(&wblock->dev.dev, "%pUL", &wblock->gblock.guid);
1258 device_initialize(&wblock->dev.dev);
1263 static int wmi_add_device(struct platform_device *pdev, struct wmi_device *wdev)
1265 struct device_link *link;
1268 * Many aggregate WMI drivers do not use -EPROBE_DEFER when they
1269 * are unable to find a WMI device during probe, instead they require
1270 * all WMI devices associated with an platform device to become available
1271 * at once. This device link thus prevents WMI drivers from probing until
1272 * the associated platform device has finished probing (and has registered
1273 * all discovered WMI devices).
1276 link = device_link_add(&wdev->dev, &pdev->dev, DL_FLAG_AUTOREMOVE_SUPPLIER);
1280 return device_add(&wdev->dev);
1283 static bool guid_already_parsed_for_legacy(struct acpi_device *device, const guid_t *guid)
1285 struct wmi_block *wblock;
1287 list_for_each_entry(wblock, &wmi_block_list, list) {
1288 /* skip warning and register if we know the driver will use struct wmi_driver */
1289 for (int i = 0; allow_duplicates[i] != NULL; i++) {
1290 if (guid_parse_and_compare(allow_duplicates[i], guid))
1293 if (guid_equal(&wblock->gblock.guid, guid)) {
1295 * Because we historically didn't track the relationship
1296 * between GUIDs and ACPI nodes, we don't know whether
1297 * we need to suppress GUIDs that are unique on a
1298 * given node but duplicated across nodes.
1300 dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
1301 guid, dev_name(&wblock->acpi_device->dev));
1310 * Parse the _WDG method for the GUID data blocks
1312 static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev)
1314 struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
1315 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
1316 const struct guid_block *gblock;
1317 struct wmi_block *wblock;
1318 union acpi_object *obj;
1323 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
1324 if (ACPI_FAILURE(status))
1331 if (obj->type != ACPI_TYPE_BUFFER) {
1336 gblock = (const struct guid_block *)obj->buffer.pointer;
1337 total = obj->buffer.length / sizeof(struct guid_block);
1339 for (i = 0; i < total; i++) {
1341 wmi_dump_wdg(&gblock[i]);
1343 if (guid_already_parsed_for_legacy(device, &gblock[i].guid))
1346 wblock = kzalloc(sizeof(*wblock), GFP_KERNEL);
1348 dev_err(wmi_bus_dev, "Failed to allocate %pUL\n", &gblock[i].guid);
1352 wblock->acpi_device = device;
1353 wblock->gblock = gblock[i];
1355 retval = wmi_create_device(wmi_bus_dev, wblock, device);
1361 list_add_tail(&wblock->list, &wmi_block_list);
1364 wblock->handler = wmi_notify_debug;
1365 wmi_method_enable(wblock, true);
1368 retval = wmi_add_device(pdev, &wblock->dev);
1370 dev_err(wmi_bus_dev, "failed to register %pUL\n",
1371 &wblock->gblock.guid);
1373 wmi_method_enable(wblock, false);
1375 list_del(&wblock->list);
1376 put_device(&wblock->dev.dev);
1386 * WMI can have EmbeddedControl access regions. In which case, we just want to
1387 * hand these off to the EC driver.
1390 acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
1391 u32 bits, u64 *value,
1392 void *handler_context, void *region_context)
1394 int result = 0, i = 0;
1397 if ((address > 0xFF) || !value)
1398 return AE_BAD_PARAMETER;
1400 if (function != ACPI_READ && function != ACPI_WRITE)
1401 return AE_BAD_PARAMETER;
1404 return AE_BAD_PARAMETER;
1406 if (function == ACPI_READ) {
1407 result = ec_read(address, &temp);
1408 (*value) |= ((u64)temp) << i;
1410 temp = 0xff & ((*value) >> i);
1411 result = ec_write(address, temp);
1416 return AE_BAD_PARAMETER;
1418 return AE_NOT_FOUND;
1426 static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
1429 struct wmi_block *wblock = NULL, *iter;
1431 list_for_each_entry(iter, &wmi_block_list, list) {
1432 struct guid_block *block = &iter->gblock;
1434 if (iter->acpi_device->handle == handle &&
1435 (block->flags & ACPI_WMI_EVENT) &&
1436 (block->notify_id == event)) {
1445 /* If a driver is bound, then notify the driver. */
1446 if (test_bit(WMI_PROBED, &wblock->flags) && wblock->dev.dev.driver) {
1447 struct wmi_driver *driver = drv_to_wdrv(wblock->dev.dev.driver);
1448 struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
1451 if (!driver->no_notify_data) {
1452 status = get_event_data(wblock, &evdata);
1453 if (ACPI_FAILURE(status)) {
1454 dev_warn(&wblock->dev.dev, "failed to get event data\n");
1460 driver->notify(&wblock->dev, evdata.pointer);
1462 kfree(evdata.pointer);
1463 } else if (wblock->handler) {
1464 /* Legacy handler */
1465 wblock->handler(event, wblock->handler_data);
1469 pr_info("DEBUG: GUID %pUL event 0x%02X\n", &wblock->gblock.guid, event);
1471 acpi_bus_generate_netlink_event(
1472 wblock->acpi_device->pnp.device_class,
1473 dev_name(&wblock->dev.dev),
1477 static int wmi_remove_device(struct device *dev, void *data)
1479 struct wmi_block *wblock = dev_to_wblock(dev);
1481 list_del(&wblock->list);
1482 device_unregister(dev);
1487 static void acpi_wmi_remove(struct platform_device *device)
1489 struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
1490 struct device *wmi_bus_device = dev_get_drvdata(&device->dev);
1492 acpi_remove_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY,
1493 acpi_wmi_notify_handler);
1494 acpi_remove_address_space_handler(acpi_device->handle,
1495 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
1497 device_for_each_child_reverse(wmi_bus_device, NULL, wmi_remove_device);
1498 device_unregister(wmi_bus_device);
1501 static int acpi_wmi_probe(struct platform_device *device)
1503 struct acpi_device *acpi_device;
1504 struct device *wmi_bus_dev;
1508 acpi_device = ACPI_COMPANION(&device->dev);
1510 dev_err(&device->dev, "ACPI companion is missing\n");
1514 status = acpi_install_address_space_handler(acpi_device->handle,
1516 &acpi_wmi_ec_space_handler,
1518 if (ACPI_FAILURE(status)) {
1519 dev_err(&device->dev, "Error installing EC region handler\n");
1523 status = acpi_install_notify_handler(acpi_device->handle,
1525 acpi_wmi_notify_handler,
1527 if (ACPI_FAILURE(status)) {
1528 dev_err(&device->dev, "Error installing notify handler\n");
1530 goto err_remove_ec_handler;
1533 wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
1534 NULL, "wmi_bus-%s", dev_name(&device->dev));
1535 if (IS_ERR(wmi_bus_dev)) {
1536 error = PTR_ERR(wmi_bus_dev);
1537 goto err_remove_notify_handler;
1539 dev_set_drvdata(&device->dev, wmi_bus_dev);
1541 error = parse_wdg(wmi_bus_dev, device);
1543 pr_err("Failed to parse WDG method\n");
1544 goto err_remove_busdev;
1550 device_unregister(wmi_bus_dev);
1552 err_remove_notify_handler:
1553 acpi_remove_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY,
1554 acpi_wmi_notify_handler);
1556 err_remove_ec_handler:
1557 acpi_remove_address_space_handler(acpi_device->handle,
1559 &acpi_wmi_ec_space_handler);
1564 int __must_check __wmi_driver_register(struct wmi_driver *driver,
1565 struct module *owner)
1567 driver->driver.owner = owner;
1568 driver->driver.bus = &wmi_bus_type;
1570 return driver_register(&driver->driver);
1572 EXPORT_SYMBOL(__wmi_driver_register);
1575 * wmi_driver_unregister() - Unregister a WMI driver
1576 * @driver: WMI driver to unregister
1578 * Unregisters a WMI driver from the WMI bus.
1580 void wmi_driver_unregister(struct wmi_driver *driver)
1582 driver_unregister(&driver->driver);
1584 EXPORT_SYMBOL(wmi_driver_unregister);
1586 static struct platform_driver acpi_wmi_driver = {
1589 .acpi_match_table = wmi_device_ids,
1591 .probe = acpi_wmi_probe,
1592 .remove_new = acpi_wmi_remove,
1595 static int __init acpi_wmi_init(void)
1602 error = class_register(&wmi_bus_class);
1606 error = bus_register(&wmi_bus_type);
1608 goto err_unreg_class;
1610 error = platform_driver_register(&acpi_wmi_driver);
1612 pr_err("Error loading mapper\n");
1619 bus_unregister(&wmi_bus_type);
1622 class_unregister(&wmi_bus_class);
1627 static void __exit acpi_wmi_exit(void)
1629 platform_driver_unregister(&acpi_wmi_driver);
1630 bus_unregister(&wmi_bus_type);
1631 class_unregister(&wmi_bus_class);
1634 subsys_initcall_sync(acpi_wmi_init);
1635 module_exit(acpi_wmi_exit);