* [RFC PATCH v2 1/2] platform/x86: huawei-wmi: serialize WMI retries and validate responses
[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
2026-11-18 16:13 ` [RFC PATCH v2 2/2] platform/x86: huawei-wmi: expose native Turbo mode as platform_profile Fuwei Xie
1 sibling, 1 reply; 3+ messages in thread
From: Fuwei Xie @ 2026-11-18 16:13 UTC (permalink / raw)
To: hansg, ilpo.jarvinen
Cc: platform-driver-x86, linux-kernel, erenozen, Fuwei Xie
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);
+ 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)
{
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);
/* 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;
--
2.55.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [RFC PATCH v2 2/2] platform/x86: huawei-wmi: expose native Turbo mode as platform_profile
[not found] <20261118161311.17252-1-12313607@mail.sustech.edu.cn>
2026-11-18 16:13 ` [RFC PATCH v2 1/2] platform/x86: huawei-wmi: serialize WMI retries and validate responses Fuwei Xie
@ 2026-11-18 16:13 ` Fuwei Xie
1 sibling, 0 replies; 3+ messages in thread
From: Fuwei Xie @ 2026-11-18 16:13 UTC (permalink / raw)
To: hansg, ilpo.jarvinen
Cc: platform-driver-x86, linux-kernel, erenozen, Fuwei Xie
The firmware on Huawei NbF-XX and MateBook 16 CREM-WXX9 exposes GTUB/STUB
behind WMI commands 0x00000e04 and 0x00000f04. The driver does not expose
this state through platform_profile, so userspace cannot select the
native firmware profile through the standard interface.
Map Turbo off/on to balanced/performance and read back the state after
a set. CREM-WXX9 firmware can return status zero even when it refuses a
request; the resulting state must determine whether the change succeeded.
Detect support within the existing Huawei WMI method device, requiring
both ACPI methods and a successful query of a known state (0 or 1).
Do not switch modes during detection. Unsupported or malformed responses
leave profiles unavailable without disabling the driver's other features.
This replaces a model/BIOS allowlist: the same interface was reported on
both Intel NbF-XX BIOS 3.07 and AMD CREM-WXX9 BIOS 2.32.
Refresh the reported state through deferred work on WMI notifications,
power-supply changes and resume. The power-supply notifier also covers
CREM-WXX9 firmware turning Turbo off on unplug without a 0x2a0 event.
Re-query on 0x2a6, which was observed on refused requests on that model,
without assigning a universal meaning to the notification.
Leave AC and battery restrictions to the firmware. NbF-XX BIOS 3.07 checks
AC power and a minimum battery charge, whereas CREM-WXX9 BIOS 2.32 was
reported to check AC power only. Do not enforce a fixed wattage, write
power-limit registers, or override the fan policy.
Eren Ozen reported working mode readback, tuned-ppd integration, Fn+P
and AC transitions on CREM-WXX9 using v1 plus a DMI entry. Testing the
revised capability-based probe on that machine is still requested.
Signed-off-by: Fuwei Xie <12313607@mail.sustech.edu.cn>
---
drivers/platform/x86/Kconfig | 1 +
drivers/platform/x86/huawei-wmi.c | 190 +++++++++++++++++++++++++++++-
2 files changed, 189 insertions(+), 2 deletions(-)
diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
index 9ce384e04..05e65a483 100644
--- a/drivers/platform/x86/Kconfig
+++ b/drivers/platform/x86/Kconfig
@@ -22,6 +22,7 @@ config HUAWEI_WMI
depends on ACPI_EC
depends on ACPI_WMI
depends on INPUT
+ select ACPI_PLATFORM_PROFILE
select INPUT_SPARSEKMAP
select LEDS_CLASS
select NEW_LEDS
diff --git a/drivers/platform/x86/huawei-wmi.c b/drivers/platform/x86/huawei-wmi.c
index 9894424e7..9e79f7452 100644
--- a/drivers/platform/x86/huawei-wmi.c
+++ b/drivers/platform/x86/huawei-wmi.c
@@ -16,9 +16,11 @@
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
+#include <linux/platform_profile.h>
#include <linux/power_supply.h>
#include <linux/sysfs.h>
#include <linux/wmi.h>
+#include <linux/workqueue.h>
#include <acpi/battery.h>
/*
@@ -39,6 +41,8 @@ enum {
FN_LOCK_GET = 0x00000604, /* \GFRS */
FN_LOCK_SET = 0x00000704, /* \SFRS */
MICMUTE_LED_SET = 0x00000b04, /* \SMLS */
+ POWER_MODE_GET = 0x00000e04, /* \GTUB */
+ POWER_MODE_SET = 0x00000f04, /* \STUB */
};
union hwmi_arg {
@@ -68,6 +72,9 @@ struct huawei_wmi {
struct device *dev;
struct mutex wmi_lock;
+ struct device *profile_dev;
+ struct work_struct profile_work;
+ struct notifier_block power_notifier;
};
static struct huawei_wmi *huawei_wmi;
@@ -264,6 +271,170 @@ static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
return huawei_wmi_cmd_min(arg, buf, buflen, buflen);
}
+/* Native firmware profiles: no RAPL writes and no periodic enforcement. */
+
+static int huawei_wmi_profile_get(struct device *dev,
+ enum platform_profile_option *profile)
+{
+ u8 ret[2];
+ int err;
+
+ err = huawei_wmi_cmd(POWER_MODE_GET, ret, sizeof(ret));
+ if (err)
+ return err;
+ if (ret[1] > 1)
+ return -ENODATA;
+ *profile = ret[1] ? PLATFORM_PROFILE_PERFORMANCE : PLATFORM_PROFILE_BALANCED;
+ return 0;
+}
+
+static int huawei_wmi_profile_set(struct device *dev,
+ enum platform_profile_option profile)
+{
+ enum platform_profile_option actual;
+ union hwmi_arg arg = { .cmd = POWER_MODE_SET };
+ int err;
+
+ if (profile != PLATFORM_PROFILE_BALANCED &&
+ profile != PLATFORM_PROFILE_PERFORMANCE)
+ return -EOPNOTSUPP;
+
+ err = huawei_wmi_profile_get(dev, &actual);
+ if (err || actual == profile)
+ return err;
+
+ /* Firmware enforces model-specific AC and battery requirements. */
+ arg.args[2] = profile == PLATFORM_PROFILE_PERFORMANCE;
+ err = huawei_wmi_cmd(arg.cmd, NULL, 0);
+ if (err)
+ return err;
+ /* Some firmware returns success even when it refuses the change. */
+ err = huawei_wmi_profile_get(dev, &actual);
+ if (err)
+ return err;
+ return actual == profile ? 0 : -EAGAIN;
+}
+
+static bool huawei_wmi_has_profile_method(char *path)
+{
+ acpi_object_type type;
+ acpi_handle handle;
+
+ return ACPI_SUCCESS(acpi_get_handle(NULL, path, &handle)) &&
+ ACPI_SUCCESS(acpi_get_type(handle, &type)) &&
+ type == ACPI_TYPE_METHOD;
+}
+
+static int huawei_wmi_profile_detect(struct device *dev)
+{
+ enum platform_profile_option profile;
+
+ if (!huawei_wmi_has_profile_method("\\GTUB") ||
+ !huawei_wmi_has_profile_method("\\STUB"))
+ return -ENODEV;
+
+ /* Query a known state; never switch modes to probe firmware support. */
+ return huawei_wmi_profile_get(dev, &profile);
+}
+
+static int huawei_wmi_profile_probe(void *drvdata, unsigned long *choices)
+{
+ set_bit(PLATFORM_PROFILE_BALANCED, choices);
+ set_bit(PLATFORM_PROFILE_PERFORMANCE, choices);
+ return 0;
+}
+
+static const struct platform_profile_ops huawei_profile_ops = {
+ .probe = huawei_wmi_profile_probe,
+ .profile_get = huawei_wmi_profile_get,
+ .profile_set = huawei_wmi_profile_set,
+};
+
+static void huawei_wmi_profile_work(struct work_struct *work)
+{
+ struct huawei_wmi *huawei = container_of(work, struct huawei_wmi, profile_work);
+ /* Pairs with release publication after profile/notifier registration. */
+ struct device *dev = smp_load_acquire(&huawei->profile_dev);
+ enum platform_profile_option profile;
+ int err;
+
+ if (!dev)
+ return;
+ err = huawei_wmi_profile_get(dev, &profile);
+ if (err) {
+ dev_warn_ratelimited(huawei->dev, "Cannot refresh firmware profile: %d\n", err);
+ return;
+ }
+ /* A cached value can miss a quick userspace change followed by Fn+P. */
+ platform_profile_notify(dev);
+}
+
+static void huawei_wmi_profile_refresh(struct huawei_wmi *huawei)
+{
+ /* Pairs with release publication after profile/notifier registration. */
+ if (smp_load_acquire(&huawei->profile_dev))
+ schedule_work(&huawei->profile_work);
+}
+
+static int huawei_wmi_power_changed(struct notifier_block *nb,
+ unsigned long event, void *data)
+{
+ struct huawei_wmi *huawei = container_of(nb, struct huawei_wmi, power_notifier);
+
+ if (event == PSY_EVENT_PROP_CHANGED)
+ huawei_wmi_profile_refresh(huawei);
+ return NOTIFY_OK;
+}
+
+static void huawei_wmi_profile_setup(struct huawei_wmi *huawei)
+{
+ struct device *dev;
+ int err;
+
+ err = huawei_wmi_profile_detect(huawei->dev);
+ if (err == -ENODEV)
+ return;
+ if (err)
+ goto fail;
+ dev = platform_profile_register(huawei->dev, "huawei-wmi", huawei,
+ &huawei_profile_ops);
+ if (IS_ERR(dev)) {
+ err = PTR_ERR(dev);
+ goto fail;
+ }
+ huawei->power_notifier.notifier_call = huawei_wmi_power_changed;
+ err = power_supply_reg_notifier(&huawei->power_notifier);
+ if (err) {
+ platform_profile_remove(dev);
+ goto fail;
+ }
+ /* Publish only after both registration steps succeed. */
+ smp_store_release(&huawei->profile_dev, dev);
+ huawei_wmi_profile_refresh(huawei);
+ return;
+fail:
+ dev_warn(huawei->dev, "Native platform profiles unavailable: %d\n", err);
+}
+
+/* Call after removing the WMI event handlers. */
+static void huawei_wmi_profile_exit(struct huawei_wmi *huawei)
+{
+ if (!huawei->profile_dev)
+ return;
+ power_supply_unreg_notifier(&huawei->power_notifier);
+ cancel_work_sync(&huawei->profile_work);
+ platform_profile_remove(huawei->profile_dev);
+ huawei->profile_dev = NULL;
+}
+
+static int huawei_wmi_resume(struct device *dev)
+{
+ huawei_wmi_profile_refresh(dev_get_drvdata(dev));
+ return 0;
+}
+
+static DEFINE_SIMPLE_DEV_PM_OPS(huawei_wmi_pm, NULL, huawei_wmi_resume);
+
/* LEDs */
static int huawei_wmi_micmute_led_set(struct led_classdev *led_cdev,
@@ -830,6 +1001,18 @@ static void huawei_wmi_process_key(struct input_dev *idev, int code)
kfree(response.pointer);
}
+ /*
+ * Refresh on mode events and 0x2a6, observed on refused STUB requests
+ * on CREM-WXX9. Do not infer a mode or a universal error from the event.
+ */
+ if (code == 0x2a0 || code == 0x2a1 || code == 0x2a6) {
+ /* Pairs with release publication after profile/notifier registration. */
+ if (smp_load_acquire(&huawei_wmi->profile_dev)) {
+ huawei_wmi_profile_refresh(huawei_wmi);
+ return;
+ }
+ }
+
key = sparse_keymap_entry_from_scancode(idev, code);
if (!key) {
dev_info(&idev->dev, "Unknown key pressed, code: 0x%04x\n", code);
@@ -904,6 +1087,8 @@ static int huawei_wmi_probe(struct platform_device *pdev)
platform_set_drvdata(pdev, huawei_wmi);
huawei_wmi->dev = &pdev->dev;
+ mutex_init(&huawei_wmi->wmi_lock);
+ INIT_WORK(&huawei_wmi->profile_work, huawei_wmi_profile_work);
while (*guid->guid_string) {
if (wmi_has_guid(guid->guid_string)) {
@@ -918,12 +1103,11 @@ static int huawei_wmi_probe(struct platform_device *pdev)
}
if (wmi_has_guid(HWMI_METHOD_GUID)) {
- mutex_init(&huawei_wmi->wmi_lock);
-
huawei_wmi_leds_setup(&pdev->dev);
huawei_wmi_fn_lock_setup(&pdev->dev);
huawei_wmi_battery_setup(&pdev->dev);
huawei_wmi_debugfs_setup(&pdev->dev);
+ huawei_wmi_profile_setup(huawei_wmi);
}
return 0;
@@ -940,6 +1124,7 @@ static void huawei_wmi_remove(struct platform_device *pdev)
guid++;
}
+ huawei_wmi_profile_exit(huawei_wmi);
if (wmi_has_guid(HWMI_METHOD_GUID)) {
huawei_wmi_debugfs_exit(&pdev->dev);
huawei_wmi_battery_exit(&pdev->dev);
@@ -950,6 +1135,7 @@ static void huawei_wmi_remove(struct platform_device *pdev)
static struct platform_driver huawei_wmi_driver = {
.driver = {
.name = "huawei-wmi",
+ .pm = pm_sleep_ptr(&huawei_wmi_pm),
},
.probe = huawei_wmi_probe,
.remove = huawei_wmi_remove,
--
2.55.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [RFC PATCH v2 1/2] platform/x86: huawei-wmi: serialize WMI retries and validate responses
2026-11-18 16:13 ` [RFC PATCH v2 1/2] platform/x86: huawei-wmi: serialize WMI retries and validate responses Fuwei Xie
@ 2026-11-19 10:40 ` Ilpo Järvinen
0 siblings, 0 replies; 3+ messages in thread
From: Ilpo Järvinen @ 2026-11-19 10:40 UTC (permalink / raw)
To: Fuwei Xie; +Cc: Hans de Goede, platform-driver-x86, LKML, erenozen
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;
>
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-19 10:40 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20261118161311.17252-1-12313607@mail.sustech.edu.cn>
2026-11-18 16:13 ` [RFC PATCH v2 1/2] platform/x86: huawei-wmi: serialize WMI retries and validate responses Fuwei Xie
2026-11-19 10:40 ` Ilpo Järvinen
2026-11-18 16:13 ` [RFC PATCH v2 2/2] platform/x86: huawei-wmi: expose native Turbo mode as platform_profile Fuwei Xie
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox