mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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>,
	erenozen@protonmail.com
Subject: Re: [RFC PATCH v2 1/2] platform/x86: huawei-wmi: serialize WMI retries and validate responses
Date: Thu, 19 Nov 2026 12:40:25 +0200 (EET)	[thread overview]
Message-ID: <196e894e-6f66-eb2b-765d-8eacee81a979@linux.intel.com> (raw)
In-Reply-To: <B27C5CB9EBA10EE0+20261118161311.17252-2-12313607@mail.sustech.edu.cn>

On Thu, 19 Nov 2026, Fuwei Xie wrote:

> Serialize the two-call firmware handshake and release the first
> ACPI allocation before retrying. Check the response shape before
> reading the firmware status.
> 
> Accept short package payloads for Fn lock and battery thresholds
> when they contain the required fields. Zero-fill the rest of the
> destination so the legacy parsers can scan their buffers safely.
> 
> Signed-off-by: Fuwei Xie <12313607@mail.sustech.edu.cn>
> ---
>  drivers/platform/x86/huawei-wmi.c | 144 ++++++++++++++++--------------
>  1 file changed, 75 insertions(+), 69 deletions(-)
> 
> diff --git a/drivers/platform/x86/huawei-wmi.c b/drivers/platform/x86/huawei-wmi.c
> index 96ee9a97c..9894424e7 100644
> --- a/drivers/platform/x86/huawei-wmi.c
> +++ b/drivers/platform/x86/huawei-wmi.c
> @@ -150,14 +150,12 @@ 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)
> +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 = wmi_evaluate_method(HWMI_METHOD_GUID, 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;
> @@ -166,98 +164,106 @@ static int huawei_wmi_call(struct huawei_wmi *huawei,
>  	return 0;
>  }
>  
> -/* 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).
> +static int huawei_wmi_call(struct huawei_wmi *huawei,
> +			   struct acpi_buffer *in, struct acpi_buffer *out)
> +{
> +	int ret;
> +
> +	mutex_lock(&huawei->wmi_lock);
> +	ret = __huawei_wmi_call(huawei, in, out);

Use guard and direct return.

> +	mutex_unlock(&huawei->wmi_lock);
> +	return ret;
> +}
> +
> +/* Return a borrowed payload without modifying the ACPI allocation. */
> +static int huawei_wmi_response(const union acpi_object *obj,
> +			       const u8 **data, size_t *len)
> +{
> +	if (!obj)
> +		return -EIO;
> +
> +	if (obj->type == ACPI_TYPE_BUFFER) {
> +		if (obj->buffer.length != 0x104 || !obj->buffer.pointer)
> +			return -EIO;
> +		*data = obj->buffer.pointer + 4;
> +		*len = 0x100;
> +	} else if (obj->type == ACPI_TYPE_PACKAGE) {
> +		if (obj->package.count != 2 || !obj->package.elements)
> +			return -EIO;
> +		obj = &obj->package.elements[1];
> +		if (obj->type != ACPI_TYPE_BUFFER || !obj->buffer.length ||
> +		    !obj->buffer.pointer)
> +			return -EIO;
> +		*data = obj->buffer.pointer;
> +		*len = obj->buffer.length;
> +	} else {
> +		return -EIO;
> +	}
> +
> +	return 0;
> +}
> +
> +/*
> + * The payload starts with the firmware status byte. Legacy variable-length
> + * replies may omit trailing zeroes. Require the fields used by the caller
> + * and zero-fill any unused part of its destination.
>   */
> -static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
> +static int huawei_wmi_cmd_min(u64 arg, u8 *buf, size_t buflen, size_t minlen)

The new WMI API provides min_size functionality so this driver should be 
converted to that instead.

>  {
>  	struct huawei_wmi *huawei = huawei_wmi;
>  	struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
>  	struct acpi_buffer in;
> -	union acpi_object *obj;
> +	const u8 *data;
>  	size_t len;
>  	int err, i;
>  
>  	in.length = sizeof(arg);
>  	in.pointer = &arg;
> +	/* Keep both halves of the firmware handshake in one transaction. */
> +	mutex_lock(&huawei->wmi_lock);

guard()

You should also convert the kfree() in another patch before this to use 
__free().

-- 
 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.
>  	 */
>  	for (i = 0; i < 2; i++) {
> -		err = huawei_wmi_call(huawei, &in, &out);
> +		out.length = ACPI_ALLOCATE_BUFFER;
> +		out.pointer = NULL;
> +		err = __huawei_wmi_call(huawei, &in, &out);
>  		if (err)
>  			goto fail_cmd;
>  
> -		obj = out.pointer;
> -		if (!obj) {
> -			err = -EIO;
> +		err = huawei_wmi_response(out.pointer, &data, &len);
> +		if (err)
>  			goto fail_cmd;
> -		}
> -
> -		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);
> -				err = -EIO;
> -				goto fail_cmd;
> -			}
> -
> -			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);
> -				err = -EIO;
> -				goto fail_cmd;
> -			}
> -
> -			obj = &obj->package.elements[1];
> -			if (obj->type != ACPI_TYPE_BUFFER) {
> -				dev_err(huawei->dev, "Bad package element type, got %d\n", obj->type);
> -				err = -EIO;
> -				goto fail_cmd;
> +		if (!data[0] || i == 1) {
> +			err = data[0] ? -ENODEV : 0;
> +			if (buf) {
> +				if (len < minlen) {
> +					if (!err)
> +						err = -ENODATA;
> +					goto fail_cmd;
> +				}
> +				len = min(len, buflen);
> +				memcpy(buf, data, len);
> +				memset(buf + len, 0, buflen - len);
>  			}
> -			len = obj->buffer.length;
> -
> -			break;
> -		/* Shouldn't get here! */
> -		default:
> -			dev_err(huawei->dev, "Unexpected obj type, got: %d\n", obj->type);
> -			err = -EIO;
>  			goto fail_cmd;
>  		}
> -
> -		if (!*obj->buffer.pointer)
> -			break;
> -	}
> -
> -	err = (*obj->buffer.pointer) ? -ENODEV : 0;
> -
> -	if (buf) {
> -		len = min(buflen, len);
> -		memcpy(buf, obj->buffer.pointer, len);
> +		kfree(out.pointer);
> +		out.pointer = NULL;
>  	}
>  
>  fail_cmd:
>  	kfree(out.pointer);
> +	mutex_unlock(&huawei->wmi_lock);
>  	return err;
>  }
>  
> +static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
> +{
> +	return huawei_wmi_cmd_min(arg, buf, buflen, buflen);
> +}
> +
>  /* LEDs */
>  
>  static int huawei_wmi_micmute_led_set(struct led_classdev *led_cdev,
> @@ -329,7 +335,7 @@ static int huawei_wmi_battery_get(int *start, int *end)
>  	u8 ret[0x100];
>  	int err, i;
>  
> -	err = huawei_wmi_cmd(BATTERY_THRESH_GET, ret, sizeof(ret));
> +	err = huawei_wmi_cmd_min(BATTERY_THRESH_GET, ret, sizeof(ret), 3);
>  	if (err)
>  		return err;
>  
> @@ -627,7 +633,7 @@ static int huawei_wmi_fn_lock_get(int *on)
>  		return huawei_acpi_fn_lock_get(on);
>  
>  	/* WMI fallback */
> -	err = huawei_wmi_cmd(FN_LOCK_GET, ret, 0x100);
> +	err = huawei_wmi_cmd_min(FN_LOCK_GET, ret, sizeof(ret), 2);
>  	if (err)
>  		return err;
>  
> 

  reply	other threads:[~2026-11-19 10:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20261118161311.17252-1-12313607@mail.sustech.edu.cn>
2026-11-18 16:13 ` Fuwei Xie
2026-11-19 10:40   ` Ilpo Järvinen [this message]
2026-11-18 16:13 ` [RFC PATCH v2 2/2] platform/x86: huawei-wmi: expose native Turbo mode as platform_profile 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=196e894e-6f66-eb2b-765d-8eacee81a979@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 \
    /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®