[RFC PATCH v3 3/5] platform/x86: huawei-wmi: use the sized WMI method interface
From: Fuwei Xie
Date: Tue Nov 24 2026 - 12:28:27 EST
Let the WMI core unpack method replies and require enough bytes for
the common header. Hold the mutex across both attempts. A retry status
may arrive without any command data, so validate each successful query
against the fields its caller needs. Scan only bytes actually returned.
Keep the raw debugfs dump on the device-based ACPI interface so it can
display the original object types.
Signed-off-by: Fuwei Xie <12313607@xxxxxxxxxxxxxxxxxxx>
---
drivers/platform/x86/huawei-wmi.c | 170 +++++++++++-------------------
1 file changed, 59 insertions(+), 111 deletions(-)
diff --git a/drivers/platform/x86/huawei-wmi.c b/drivers/platform/x86/huawei-wmi.c
index 690bd0abc..209066260 100644
--- a/drivers/platform/x86/huawei-wmi.c
+++ b/drivers/platform/x86/huawei-wmi.c
@@ -151,101 +151,42 @@ static const struct dmi_system_id huawei_quirks[] = {
/* Utils */
-static int huawei_wmi_call(struct huawei_wmi *huawei,
- struct acpi_buffer *in, struct acpi_buffer *out)
-{
- acpi_status status;
-
- mutex_lock(&huawei->wmi_lock);
- status = wmidev_evaluate_method(huawei->wdev, 0, 1, in, out);
- mutex_unlock(&huawei->wmi_lock);
- if (ACPI_FAILURE(status)) {
- dev_err(huawei->dev, "Failed to evaluate wmi method\n");
- return -ENODEV;
- }
-
- return 0;
-}
+/* Both HWMI reply formats start with four reserved bytes and a status. */
+struct hwmi_reply {
+ u8 reserved[4];
+ u8 status;
+ u8 data[];
+};
-/* HWMI takes a 64 bit input and returns either a package with 2 buffers, one of
- * 4 bytes and the other of 256 bytes, or one buffer of size 0x104 (260) bytes.
- * The first 4 bytes are ignored, we ignore the first 4 bytes buffer if we got a
- * package, or skip the first 4 if a buffer of 0x104 is used. The first byte of
- * the remaining 0x100 sized buffer has the return status of every call. In case
- * the return status is non-zero, we return -ENODEV but still copy the returned
- * buffer to the given buffer parameter (buf).
- */
+/* Return the number of command data bytes copied, or a negative errno. */
static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
{
struct huawei_wmi *huawei = huawei_wmi;
- struct acpi_buffer in;
- size_t len;
- int err, i;
-
- in.length = sizeof(arg);
- in.pointer = &arg;
+ struct wmi_buffer in = { .length = sizeof(arg), .data = &arg };
+ int i;
- /* Some models require calling HWMI twice to execute a command. We evaluate
- * HWMI and if we get a non-zero return status we evaluate it again.
- */
+ guard(mutex)(&huawei->wmi_lock);
for (i = 0; i < 2; i++) {
- struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
+ struct wmi_buffer out;
+ size_t len;
+ int err;
- err = huawei_wmi_call(huawei, &in, &out);
+ err = wmidev_invoke_method(huawei->wdev, 0, 1, &in, &out,
+ sizeof(struct hwmi_reply));
if (err)
return err;
- union acpi_object *allocation __free(kfree) = out.pointer;
- union acpi_object *obj = allocation;
-
- if (!obj)
- return -EIO;
-
- switch (obj->type) {
- /* Models that implement both "legacy" and HWMI tend to return a 0x104
- * sized buffer instead of a package of 0x4 and 0x100 buffers.
- */
- case ACPI_TYPE_BUFFER:
- if (obj->buffer.length == 0x104) {
- // Skip the first 4 bytes.
- obj->buffer.pointer += 4;
- len = 0x100;
- } else {
- dev_err(huawei->dev, "Bad buffer length, got %d\n", obj->buffer.length);
- return -EIO;
- }
-
- break;
- /* HWMI returns a package with 2 buffer elements, one of 4 bytes and the
- * other is 256 bytes.
- */
- case ACPI_TYPE_PACKAGE:
- if (obj->package.count != 2) {
- dev_err(huawei->dev, "Bad package count, got %d\n", obj->package.count);
- return -EIO;
- }
-
- obj = &obj->package.elements[1];
- if (obj->type != ACPI_TYPE_BUFFER) {
- dev_err(huawei->dev, "Bad package element type, got %d\n", obj->type);
- return -EIO;
- }
- len = obj->buffer.length;
-
- break;
- /* Shouldn't get here! */
- default:
- dev_err(huawei->dev, "Unexpected obj type, got: %d\n", obj->type);
- return -EIO;
- }
+ struct hwmi_reply *reply __free(kfree) = out.data;
- if (*obj->buffer.pointer && !i)
+ /* Some machines require a second call after a nonzero status. */
+ if (reply->status)
continue;
- if (buf)
- memcpy(buf, obj->buffer.pointer, min(buflen, len));
+ len = min(buflen, out.length - sizeof(*reply));
+ if (len)
+ memcpy(buf, reply->data, len);
- return *obj->buffer.pointer ? -ENODEV : 0;
+ return len;
}
return -ENODEV;
@@ -319,21 +260,22 @@ static void huawei_wmi_leds_setup(struct device *dev)
static int huawei_wmi_battery_get(int *start, int *end)
{
- u8 ret[0x100];
- int err, i;
+ u8 data[0xff];
+ int len;
- err = huawei_wmi_cmd(BATTERY_THRESH_GET, ret, sizeof(ret));
- if (err)
- return err;
+ len = huawei_wmi_cmd(BATTERY_THRESH_GET, data, sizeof(data));
+ if (len < 0)
+ return len;
+ if (len < 2)
+ return -ENODATA;
- /* Find the last two non-zero values. Return status is ignored. */
- i = ARRAY_SIZE(ret) - 1;
- do {
- if (start)
- *start = ret[i-1];
- if (end)
- *end = ret[i];
- } while (i > 2 && !ret[i--]);
+ /* The threshold pair can be followed by zero padding. */
+ while (len > 2 && !data[len - 1])
+ len--;
+ if (start)
+ *start = data[len - 2];
+ if (end)
+ *end = data[len - 1];
return 0;
}
@@ -612,24 +554,24 @@ static int huawei_acpi_fn_lock_set(int on)
static int huawei_wmi_fn_lock_get(int *on)
{
- u8 ret[0x100] = { 0 };
- int err, i;
+ u8 data[0xff];
+ int len;
- /* Newer models: use direct ACPI \GFRS method */
if (acpi_has_method(NULL, "\\GFRS"))
return huawei_acpi_fn_lock_get(on);
- /* WMI fallback */
- err = huawei_wmi_cmd(FN_LOCK_GET, ret, 0x100);
- if (err)
- return err;
+ len = huawei_wmi_cmd(FN_LOCK_GET, data, sizeof(data));
+ if (len < 0)
+ return len;
+ if (!len)
+ return -ENODATA;
- /* Find the first non-zero value. Return status is ignored. */
- i = 1;
- do {
- if (on)
- *on = ret[i] - 1; // -1 undefined, 0 off, 1 on.
- } while (i < 0xff && !ret[i++]);
+ if (on) {
+ const u8 *state = memchr_inv(data, 0, len);
+
+ /* -1 undefined, 0 off, 1 on. */
+ *on = state ? *state - 1 : -1;
+ }
return 0;
}
@@ -741,19 +683,25 @@ static void huawei_wmi_debugfs_call_dump(struct seq_file *m, void *data,
}
}
+/* The raw debugfs dump also prints the original ACPI object types. */
+static acpi_status huawei_wmi_debugfs_call(struct huawei_wmi *huawei,
+ struct acpi_buffer *in, struct acpi_buffer *out)
+{
+ guard(mutex)(&huawei->wmi_lock);
+ return wmidev_evaluate_method(huawei->wdev, 0, 1, in, out);
+}
+
static int huawei_wmi_debugfs_call_show(struct seq_file *m, void *data)
{
struct huawei_wmi *huawei = m->private;
struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
struct acpi_buffer in;
- int err;
in.length = sizeof(u64);
in.pointer = &huawei->debug.arg;
- err = huawei_wmi_call(huawei, &in, &out);
- if (err)
- return err;
+ if (ACPI_FAILURE(huawei_wmi_debugfs_call(huawei, &in, &out)))
+ return -ENODEV;
union acpi_object *obj __free(kfree) = out.pointer;
--
2.55.0