| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* Lenovo Legion WMI helpers driver. |
| 4 |
* |
| 5 |
* The Lenovo Legion WMI interface is broken up into multiple GUID interfaces |
| 6 |
* that require cross-references between GUID's for some functionality. The |
| 7 |
* "Custom Mode" interface is a legacy interface for managing and displaying |
| 8 |
* CPU & GPU power and hwmon settings and readings. The "Other Mode" interface |
| 9 |
* is a modern interface that replaces or extends the "Custom Mode" interface |
| 10 |
* methods. The "Gamezone" interface adds advanced features such as fan |
| 11 |
* profiles and overclocking. The "Lighting" interface adds control of various |
| 12 |
* status lights related to different hardware components. Each of these |
| 13 |
* drivers uses a common procedure to get data from the WMI interface, |
| 14 |
* enumerated here. |
| 15 |
* |
| 16 |
* Copyright (C) 2025 Derek J. Clark <[email protected]> |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <linux/acpi.h> |
| 20 |
#include <linux/cleanup.h> |
| 21 |
#include <linux/errno.h> |
| 22 |
#include <linux/export.h> |
| 23 |
#include <linux/module.h> |
| 24 |
#include <linux/unaligned.h> |
| 25 |
#include <linux/wmi.h> |
| 26 |
|
| 27 |
#include "wmi-helpers.h" |
| 28 |
|
| 29 |
/** |
| 30 |
* lwmi_dev_evaluate_int() - Helper function for calling WMI methods that |
| 31 |
* return an integer. |
| 32 |
* @wdev: Pointer to the WMI device to be called. |
| 33 |
* @instance: Instance of the called method. |
| 34 |
* @method_id: WMI Method ID for the method to be called. |
| 35 |
* @buf: Buffer of all arguments for the given method_id. |
| 36 |
* @size: Length of the buffer. |
| 37 |
* @retval: Pointer for the return value to be assigned. |
| 38 |
* |
| 39 |
* Calls wmidev_evaluate_method for Lenovo WMI devices that return an ACPI |
| 40 |
* integer. Validates the return value type and assigns the value to the |
| 41 |
* retval pointer. |
| 42 |
* |
| 43 |
* Return: 0 on success, or an error code. |
| 44 |
*/ |
| 45 |
int lwmi_dev_evaluate_int(struct wmi_device *wdev, u8 instance, u32 method_id, |
| 46 |
unsigned char *buf, size_t size, u32 *retval) |
| 47 |
{ |
| 48 |
struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 49 |
union acpi_object *ret_obj __free(kfree) = NULL; |
| 50 |
struct acpi_buffer input = { size, buf }; |
| 51 |
acpi_status status; |
| 52 |
|
| 53 |
status = wmidev_evaluate_method(wdev, instance, method_id, &input, |
| 54 |
&output); |
| 55 |
if (ACPI_FAILURE(status)) |
| 56 |
return -EIO; |
| 57 |
|
| 58 |
if (retval) { |
| 59 |
ret_obj = output.pointer; |
| 60 |
if (!ret_obj) |
| 61 |
return -ENODATA; |
| 62 |
|
| 63 |
switch (ret_obj->type) { |
| 64 |
/* |
| 65 |
* The ACPI method may simply return a buffer when a u32 |
| 66 |
* is expected. This is valid on Windows as its WMI-ACPI |
| 67 |
* driver converts everything to a common buffer. |
| 68 |
*/ |
| 69 |
case ACPI_TYPE_BUFFER: |
| 70 |
if (ret_obj->buffer.length < sizeof(u32)) |
| 71 |
return -ENXIO; |
| 72 |
|
| 73 |
*retval = get_unaligned_le32(ret_obj->buffer.pointer); |
| 74 |
return 0; |
| 75 |
case ACPI_TYPE_INTEGER: |
| 76 |
*retval = (u32)ret_obj->integer.value; |
| 77 |
return 0; |
| 78 |
default: |
| 79 |
return -ENXIO; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return 0; |
| 84 |
}; |
| 85 |
EXPORT_SYMBOL_NS_GPL(lwmi_dev_evaluate_int, "LENOVO_WMI_HELPERS"); |
| 86 |
|
| 87 |
MODULE_AUTHOR("Derek J. Clark <[email protected]>"); |
| 88 |
MODULE_DESCRIPTION("Lenovo WMI Helpers Driver"); |
| 89 |
MODULE_LICENSE("GPL"); |