From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: Fuwei Xie <12313607@mail.sustech.edu.cn>
Cc: "Hans de Goede" <hansg@kernel.org>,
platform-driver-x86@vger.kernel.org,
LKML <linux-kernel@vger.kernel.org>,
"Eren Özen" <erenozen@protonmail.com>,
"Zheng Shuyang" <yhshzh0@gmail.com>
Subject: Re: [PATCH v4 3/5] platform/x86: huawei-wmi: use the sized WMI method interface
Date: Tue, 1 Dec 2026 13:09:25 +0200 (EET) [thread overview]
Message-ID: <7973c2e9-19c3-9a70-c337-b9136e356175@linux.intel.com> (raw)
In-Reply-To: <067B78B9700CD975+20261130101218.243426-4-12313607@mail.sustech.edu.cn>
On Mon, 30 Nov 2026, Fuwei Xie wrote:
> 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@mail.sustech.edu.cn>
> ---
> 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];
Now this was interesting change, I've no idea what's going on.
...Piling up unexplained changes does not get you anywhere.
...Also, it would be nice to name magic numbers with defines.
> + 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;
It apparently was like this earlier, but this should be cleaned up with
named defines and switch/case.
Is this even equivalent to the old code? Is *on written at all if there's
0 (= off as per the comment)?
> + }
>
> 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;
>
>
--
i.
next prev parent reply other threads:[~2026-12-01 11:09 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20261130101218.243426-1-12313607@mail.sustech.edu.cn>
2026-11-30 10:12 ` [PATCH v4 1/5] platform/x86: huawei-wmi: scope method responses to each call Fuwei Xie
2026-12-01 10:40 ` Ilpo Järvinen
2026-11-30 10:12 ` [PATCH v4 2/5] platform/x86: huawei-wmi: bind the Huawei method device Fuwei Xie
2026-12-01 10:57 ` Ilpo Järvinen
2026-11-30 10:12 ` [PATCH v4 3/5] platform/x86: huawei-wmi: use the sized WMI method interface Fuwei Xie
2026-12-01 11:09 ` Ilpo Järvinen [this message]
2026-11-30 10:12 ` [PATCH v4 4/5] platform/x86: huawei-wmi: expose native firmware power profiles Fuwei Xie
2026-11-30 10:12 ` [PATCH v4 5/5] platform/x86: huawei-wmi: bind hotkeys and key data on the WMI bus Fuwei Xie
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=7973c2e9-19c3-9a70-c337-b9136e356175@linux.intel.com \
--to=ilpo.jarvinen@linux.intel.com \
--cc=12313607@mail.sustech.edu.cn \
--cc=erenozen@protonmail.com \
--cc=hansg@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=platform-driver-x86@vger.kernel.org \
--cc=yhshzh0@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®