* [PATCH v4 1/5] platform/x86: huawei-wmi: scope method responses to each call
[not found] <20261130101218.243426-1-12313607@mail.sustech.edu.cn>
@ 2026-11-30 10:12 ` 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
` (3 subsequent siblings)
4 siblings, 1 reply; 8+ messages in thread
From: Fuwei Xie @ 2026-11-30 10:12 UTC (permalink / raw)
To: Ilpo Järvinen, Hans de Goede
Cc: Fuwei Xie, platform-driver-x86, linux-kernel, Eren Özen,
Zheng Shuyang
Keep each ACPI allocation alive while decoding its response and release
it on every exit with __free(kfree). Give each handshake attempt a
fresh output descriptor so retrying cannot reuse the first allocation.
Signed-off-by: Fuwei Xie <12313607@mail.sustech.edu.cn>
---
drivers/platform/x86/huawei-wmi.c | 59 ++++++++++++-------------------
1 file changed, 23 insertions(+), 36 deletions(-)
diff --git a/drivers/platform/x86/huawei-wmi.c b/drivers/platform/x86/huawei-wmi.c
index 96ee9a97c..559731d15 100644
--- a/drivers/platform/x86/huawei-wmi.c
+++ b/drivers/platform/x86/huawei-wmi.c
@@ -177,9 +177,7 @@ static int huawei_wmi_call(struct huawei_wmi *huawei,
static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
{
struct huawei_wmi *huawei = huawei_wmi;
- struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
struct acpi_buffer in;
- union acpi_object *obj;
size_t len;
int err, i;
@@ -190,15 +188,17 @@ static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
* HWMI and if we get a non-zero return status we evaluate it again.
*/
for (i = 0; i < 2; i++) {
+ struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
+
err = huawei_wmi_call(huawei, &in, &out);
if (err)
- goto fail_cmd;
+ return err;
- obj = out.pointer;
- if (!obj) {
- err = -EIO;
- goto fail_cmd;
- }
+ 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
@@ -211,8 +211,7 @@ static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
len = 0x100;
} else {
dev_err(huawei->dev, "Bad buffer length, got %d\n", obj->buffer.length);
- err = -EIO;
- goto fail_cmd;
+ return -EIO;
}
break;
@@ -222,15 +221,13 @@ static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
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;
+ 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);
- err = -EIO;
- goto fail_cmd;
+ return -EIO;
}
len = obj->buffer.length;
@@ -238,24 +235,19 @@ static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
/* Shouldn't get here! */
default:
dev_err(huawei->dev, "Unexpected obj type, got: %d\n", obj->type);
- err = -EIO;
- goto fail_cmd;
+ return -EIO;
}
- if (!*obj->buffer.pointer)
- break;
- }
+ if (*obj->buffer.pointer && !i)
+ continue;
- err = (*obj->buffer.pointer) ? -ENODEV : 0;
+ if (buf)
+ memcpy(buf, obj->buffer.pointer, min(buflen, len));
- if (buf) {
- len = min(buflen, len);
- memcpy(buf, obj->buffer.pointer, len);
+ return *obj->buffer.pointer ? -ENODEV : 0;
}
-fail_cmd:
- kfree(out.pointer);
- return err;
+ return -ENODEV;
}
/* LEDs */
@@ -753,7 +745,6 @@ 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;
- union acpi_object *obj;
int err;
in.length = sizeof(u64);
@@ -763,17 +754,13 @@ static int huawei_wmi_debugfs_call_show(struct seq_file *m, void *data)
if (err)
return err;
- obj = out.pointer;
- if (!obj) {
- err = -EIO;
- goto fail_debugfs_call;
- }
+ union acpi_object *obj __free(kfree) = out.pointer;
- huawei_wmi_debugfs_call_dump(m, huawei, obj);
+ if (!obj)
+ return -EIO;
-fail_debugfs_call:
- kfree(out.pointer);
- return err;
+ huawei_wmi_debugfs_call_dump(m, huawei, obj);
+ return 0;
}
DEFINE_SHOW_ATTRIBUTE(huawei_wmi_debugfs_call);
--
2.55.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v4 2/5] platform/x86: huawei-wmi: bind the Huawei method device
[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-11-30 10:12 ` 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
` (2 subsequent siblings)
4 siblings, 1 reply; 8+ messages in thread
From: Fuwei Xie @ 2026-11-30 10:12 UTC (permalink / raw)
To: Ilpo Järvinen, Hans de Goede
Cc: Fuwei Xie, platform-driver-x86, linux-kernel, Eren Özen,
Zheng Shuyang
Bind the method GUID on the WMI bus and give its platform device the
bound wdev for method calls. Unregister the platform device before the
method driver releases the binding. Keep the platform sysfs location
and the input-only path on machines without the method GUID.
Allocate platform state on probe and unwind previously registered
input handlers if a later input device fails to register.
Signed-off-by: Fuwei Xie <12313607@mail.sustech.edu.cn>
---
drivers/platform/x86/huawei-wmi.c | 109 ++++++++++++++++++++----------
1 file changed, 75 insertions(+), 34 deletions(-)
diff --git a/drivers/platform/x86/huawei-wmi.c b/drivers/platform/x86/huawei-wmi.c
index 559731d15..690bd0abc 100644
--- a/drivers/platform/x86/huawei-wmi.c
+++ b/drivers/platform/x86/huawei-wmi.c
@@ -60,6 +60,7 @@ struct huawei_wmi_debug {
};
struct huawei_wmi {
+ struct wmi_device *wdev;
bool battery_available;
bool fn_lock_available;
@@ -156,7 +157,7 @@ static int huawei_wmi_call(struct huawei_wmi *huawei,
acpi_status status;
mutex_lock(&huawei->wmi_lock);
- status = wmi_evaluate_method(HWMI_METHOD_GUID, 0, 1, in, out);
+ 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");
@@ -880,25 +881,36 @@ static const struct wmi_device_id huawei_wmi_events_id_table[] = {
static int huawei_wmi_probe(struct platform_device *pdev)
{
- const struct wmi_device_id *guid = huawei_wmi_events_id_table;
+ const struct wmi_device_id *guid;
+ struct wmi_device **method = dev_get_platdata(&pdev->dev);
int err;
+ huawei_wmi = devm_kzalloc(&pdev->dev, sizeof(*huawei_wmi), GFP_KERNEL);
+ if (!huawei_wmi)
+ return -ENOMEM;
+
+ huawei_wmi->wdev = method ? *method : NULL;
platform_set_drvdata(pdev, huawei_wmi);
huawei_wmi->dev = &pdev->dev;
- while (*guid->guid_string) {
- if (wmi_has_guid(guid->guid_string)) {
- err = huawei_wmi_input_setup(&pdev->dev, guid->guid_string);
- if (err) {
- dev_err(&pdev->dev, "Failed to setup input on %s\n", guid->guid_string);
- return err;
- }
- }
+ for (guid = huawei_wmi_events_id_table; *guid->guid_string; guid++) {
+ if (!wmi_has_guid(guid->guid_string))
+ continue;
- guid++;
+ err = huawei_wmi_input_setup(&pdev->dev, guid->guid_string);
+ if (!err)
+ continue;
+
+ dev_err(&pdev->dev, "Failed to setup input on %s\n", guid->guid_string);
+ while (guid != huawei_wmi_events_id_table) {
+ guid--;
+ if (wmi_has_guid(guid->guid_string))
+ huawei_wmi_input_exit(&pdev->dev, guid->guid_string);
+ }
+ return err;
}
- if (wmi_has_guid(HWMI_METHOD_GUID)) {
+ if (huawei_wmi->wdev) {
mutex_init(&huawei_wmi->wmi_lock);
huawei_wmi_leds_setup(&pdev->dev);
@@ -921,7 +933,7 @@ static void huawei_wmi_remove(struct platform_device *pdev)
guid++;
}
- if (wmi_has_guid(HWMI_METHOD_GUID)) {
+ if (huawei_wmi->wdev) {
huawei_wmi_debugfs_exit(&pdev->dev);
huawei_wmi_battery_exit(&pdev->dev);
huawei_wmi_fn_lock_exit(&pdev->dev);
@@ -936,14 +948,46 @@ static struct platform_driver huawei_wmi_driver = {
.remove = huawei_wmi_remove,
};
-static __init int huawei_wmi_init(void)
+/*
+ * Keep the platform device and its existing userspace paths on both
+ * method-capable and older event-only machines.
+ */
+static struct platform_device *legacy_pdev;
+
+static int huawei_wmi_method_probe(struct wmi_device *wdev, const void *context)
{
struct platform_device *pdev;
- int err;
- huawei_wmi = kzalloc_obj(struct huawei_wmi);
- if (!huawei_wmi)
- return -ENOMEM;
+ pdev = platform_device_register_data(NULL, "huawei-wmi", PLATFORM_DEVID_NONE,
+ &wdev, sizeof(wdev));
+ if (IS_ERR(pdev))
+ return PTR_ERR(pdev);
+
+ dev_set_drvdata(&wdev->dev, pdev);
+ return 0;
+}
+
+static void huawei_wmi_method_remove(struct wmi_device *wdev)
+{
+ platform_device_unregister(dev_get_drvdata(&wdev->dev));
+}
+
+static const struct wmi_device_id huawei_wmi_method_ids[] = {
+ { HWMI_METHOD_GUID },
+ { }
+};
+MODULE_DEVICE_TABLE(wmi, huawei_wmi_method_ids);
+
+static struct wmi_driver huawei_wmi_method_driver = {
+ .driver.name = "huawei-wmi-method",
+ .id_table = huawei_wmi_method_ids,
+ .probe = huawei_wmi_method_probe,
+ .remove = huawei_wmi_method_remove,
+};
+
+static __init int huawei_wmi_init(void)
+{
+ int err;
quirks = &quirk_unknown;
dmi_check_system(huawei_quirks);
@@ -954,37 +998,34 @@ static __init int huawei_wmi_init(void)
err = platform_driver_register(&huawei_wmi_driver);
if (err)
- goto pdrv_err;
+ return err;
- pdev = platform_device_register_simple("huawei-wmi", PLATFORM_DEVID_NONE, NULL, 0);
- if (IS_ERR(pdev)) {
- err = PTR_ERR(pdev);
- goto pdev_err;
+ if (wmi_has_guid(HWMI_METHOD_GUID)) {
+ err = wmi_driver_register(&huawei_wmi_method_driver);
+ } else {
+ legacy_pdev = platform_device_register_simple("huawei-wmi", PLATFORM_DEVID_NONE,
+ NULL, 0);
+ err = PTR_ERR_OR_ZERO(legacy_pdev);
}
+ if (err)
+ platform_driver_unregister(&huawei_wmi_driver);
- return 0;
-
-pdev_err:
- platform_driver_unregister(&huawei_wmi_driver);
-pdrv_err:
- kfree(huawei_wmi);
return err;
}
static __exit void huawei_wmi_exit(void)
{
- struct platform_device *pdev = to_platform_device(huawei_wmi->dev);
+ if (legacy_pdev)
+ platform_device_unregister(legacy_pdev);
+ else
+ wmi_driver_unregister(&huawei_wmi_method_driver);
- platform_device_unregister(pdev);
platform_driver_unregister(&huawei_wmi_driver);
-
- kfree(huawei_wmi);
}
module_init(huawei_wmi_init);
module_exit(huawei_wmi_exit);
-MODULE_ALIAS("wmi:"HWMI_METHOD_GUID);
MODULE_DEVICE_TABLE(wmi, huawei_wmi_events_id_table);
MODULE_AUTHOR("Ayman Bagabas <ayman.bagabas@gmail.com>");
MODULE_DESCRIPTION("Huawei WMI laptop extras driver");
--
2.55.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v4 3/5] platform/x86: huawei-wmi: use the sized WMI method interface
[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-11-30 10:12 ` [PATCH v4 2/5] platform/x86: huawei-wmi: bind the Huawei method device Fuwei Xie
@ 2026-11-30 10:12 ` Fuwei Xie
2026-12-01 11:09 ` Ilpo Järvinen
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
4 siblings, 1 reply; 8+ messages in thread
From: Fuwei Xie @ 2026-11-30 10:12 UTC (permalink / raw)
To: Ilpo Järvinen, Hans de Goede
Cc: Fuwei Xie, platform-driver-x86, linux-kernel, Eren Özen,
Zheng Shuyang
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];
+ 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
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v4 4/5] platform/x86: huawei-wmi: expose native firmware power profiles
[not found] <20261130101218.243426-1-12313607@mail.sustech.edu.cn>
` (2 preceding siblings ...)
2026-11-30 10:12 ` [PATCH v4 3/5] platform/x86: huawei-wmi: use the sized WMI method interface Fuwei Xie
@ 2026-11-30 10:12 ` 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
4 siblings, 0 replies; 8+ messages in thread
From: Fuwei Xie @ 2026-11-30 10:12 UTC (permalink / raw)
To: Ilpo Järvinen, Hans de Goede
Cc: Fuwei Xie, platform-driver-x86, linux-kernel, Eren Özen,
Zheng Shuyang
Expose balanced and performance through platform_profile on machines
with GTUB and STUB. Read the initial mode at registration, serve reads
from the cache, and update it on a successful write.
CREM-WXX9 can report the same transition twice and deliver a stale mode
event after a newer write. Re-read GTUB on a mode notification under
the same mutex as profile writes. Notify outside that mutex only when
the cached mode changes. Also re-read on resume: CREM-WXX9 can clear
Turbo during s2idle without delivering a mode event.
Claude assisted Eren with the test scripts, the event and resume changes
and his report. Eren ran the tests and verified the results on hardware.
Co-developed-by: Eren Özen <erenozen@protonmail.com>
Signed-off-by: Eren Özen <erenozen@protonmail.com>
Signed-off-by: Fuwei Xie <12313607@mail.sustech.edu.cn>
---
drivers/platform/x86/Kconfig | 1 +
drivers/platform/x86/huawei-wmi.c | 185 ++++++++++++++++++++++++++++--
2 files changed, 174 insertions(+), 12 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 209066260..8eaa21757 100644
--- a/drivers/platform/x86/huawei-wmi.c
+++ b/drivers/platform/x86/huawei-wmi.c
@@ -16,6 +16,8 @@
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
+#include <linux/platform_profile.h>
+#include <linux/pm.h>
#include <linux/power_supply.h>
#include <linux/sysfs.h>
#include <linux/wmi.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 {
@@ -69,6 +73,8 @@ struct huawei_wmi {
struct device *dev;
struct mutex wmi_lock;
+ struct device *profile_dev;
+ enum platform_profile_option reported_profile;
};
static struct huawei_wmi *huawei_wmi;
@@ -159,13 +165,12 @@ struct hwmi_reply {
};
/* Return the number of command data bytes copied, or a negative errno. */
-static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
+static int huawei_wmi_cmd(struct huawei_wmi *huawei, u64 arg, u8 *buf, size_t buflen)
{
- struct huawei_wmi *huawei = huawei_wmi;
struct wmi_buffer in = { .length = sizeof(arg), .data = &arg };
int i;
- guard(mutex)(&huawei->wmi_lock);
+ lockdep_assert_held(&huawei->wmi_lock);
for (i = 0; i < 2; i++) {
struct wmi_buffer out;
size_t len;
@@ -192,6 +197,142 @@ static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
return -ENODEV;
}
+/* Native firmware profiles: no RAPL writes and no periodic enforcement. */
+
+static int huawei_wmi_read_profile(struct huawei_wmi *huawei, enum platform_profile_option *profile)
+{
+ u8 state;
+ int len;
+
+ len = huawei_wmi_cmd(huawei, POWER_MODE_GET, &state, sizeof(state));
+ if (len < 0)
+ return len;
+ if (!len || state > 1)
+ return -ENODATA;
+
+ *profile = state ? PLATFORM_PROFILE_PERFORMANCE : PLATFORM_PROFILE_BALANCED;
+ return 0;
+}
+
+static int huawei_wmi_profile_get(struct device *dev, enum platform_profile_option *profile)
+{
+ struct huawei_wmi *huawei = dev_get_drvdata(dev);
+
+ guard(mutex)(&huawei->wmi_lock);
+ *profile = huawei->reported_profile;
+ return 0;
+}
+
+static int huawei_wmi_profile_set(struct device *dev,
+ enum platform_profile_option profile)
+{
+ struct huawei_wmi *huawei = dev_get_drvdata(dev);
+ union hwmi_arg arg = { .cmd = POWER_MODE_SET };
+ int err;
+
+ if (profile != PLATFORM_PROFILE_BALANCED &&
+ profile != PLATFORM_PROFILE_PERFORMANCE)
+ return -EOPNOTSUPP;
+
+ arg.args[2] = (profile == PLATFORM_PROFILE_PERFORMANCE);
+
+ guard(mutex)(&huawei->wmi_lock);
+ err = huawei_wmi_cmd(huawei, arg.cmd, NULL, 0);
+ if (!err)
+ huawei->reported_profile = profile;
+
+ return err;
+}
+
+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_probe(void *drvdata, unsigned long *choices)
+{
+ struct huawei_wmi *huawei = drvdata;
+
+ set_bit(PLATFORM_PROFILE_BALANCED, choices);
+ set_bit(PLATFORM_PROFILE_PERFORMANCE, choices);
+
+ guard(mutex)(&huawei->wmi_lock);
+ return huawei_wmi_read_profile(huawei, &huawei->reported_profile);
+}
+
+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,
+};
+
+/*
+ * Sample the firmware and notify only when the reported mode changes.
+ * Return false if no profile has been sampled yet. Otherwise the event
+ * is handled, even if the read fails or the mode stays the same.
+ */
+static bool huawei_wmi_profile_sync(struct huawei_wmi *huawei)
+{
+ enum platform_profile_option profile;
+ struct device *dev;
+ int err;
+
+ scoped_guard(mutex, &huawei->wmi_lock) {
+ if (huawei->reported_profile == PLATFORM_PROFILE_LAST)
+ return false;
+ err = huawei_wmi_read_profile(huawei, &profile);
+ if (err) {
+ dev_warn_ratelimited(huawei->dev,
+ "Cannot read firmware profile: %d\n", err);
+ return true;
+ }
+ if (profile == huawei->reported_profile)
+ return true;
+ huawei->reported_profile = profile;
+ dev = huawei->profile_dev;
+ }
+ if (dev)
+ platform_profile_notify(dev);
+ return true;
+}
+
+static void huawei_wmi_profile_setup(struct huawei_wmi *huawei)
+{
+ struct device *dev;
+
+ if (!huawei_wmi_has_profile_method("\\GTUB") ||
+ !huawei_wmi_has_profile_method("\\STUB"))
+ return;
+
+ dev = platform_profile_register(huawei->dev, "huawei-wmi", huawei,
+ &huawei_profile_ops);
+ if (IS_ERR(dev)) {
+ scoped_guard(mutex, &huawei->wmi_lock)
+ huawei->reported_profile = PLATFORM_PROFILE_LAST;
+ dev_warn(huawei->dev, "Native platform profiles unavailable: %pe\n", dev);
+ return;
+ }
+ scoped_guard(mutex, &huawei->wmi_lock)
+ huawei->profile_dev = dev;
+ /* Publish changes captured between the initial sample and registration. */
+ platform_profile_notify(dev);
+}
+
+/* Call after removing the WMI event handlers. */
+static void huawei_wmi_profile_exit(struct huawei_wmi *huawei)
+{
+ if (!huawei->profile_dev)
+ return;
+ platform_profile_remove(huawei->profile_dev);
+ huawei->profile_dev = NULL;
+ huawei->reported_profile = PLATFORM_PROFILE_LAST;
+}
+
/* LEDs */
static int huawei_wmi_micmute_led_set(struct led_classdev *led_cdev,
@@ -238,7 +379,8 @@ static int huawei_wmi_micmute_led_set(struct led_classdev *led_cdev,
arg.cmd = MICMUTE_LED_SET;
arg.args[2] = brightness;
- return huawei_wmi_cmd(arg.cmd, NULL, 0);
+ guard(mutex)(&huawei_wmi->wmi_lock);
+ return huawei_wmi_cmd(huawei_wmi, arg.cmd, NULL, 0);
}
}
@@ -263,7 +405,8 @@ static int huawei_wmi_battery_get(int *start, int *end)
u8 data[0xff];
int len;
- len = huawei_wmi_cmd(BATTERY_THRESH_GET, data, sizeof(data));
+ scoped_guard(mutex, &huawei_wmi->wmi_lock)
+ len = huawei_wmi_cmd(huawei_wmi, BATTERY_THRESH_GET, data, sizeof(data));
if (len < 0)
return len;
if (len < 2)
@@ -305,9 +448,8 @@ static int huawei_wmi_battery_set(int start, int end)
msleep(1000);
}
- err = huawei_wmi_cmd(arg.cmd, NULL, 0);
-
- return err;
+ guard(mutex)(&huawei_wmi->wmi_lock);
+ return huawei_wmi_cmd(huawei_wmi, arg.cmd, NULL, 0);
}
static ssize_t charge_control_start_threshold_show(struct device *dev,
@@ -560,7 +702,8 @@ static int huawei_wmi_fn_lock_get(int *on)
if (acpi_has_method(NULL, "\\GFRS"))
return huawei_acpi_fn_lock_get(on);
- len = huawei_wmi_cmd(FN_LOCK_GET, data, sizeof(data));
+ scoped_guard(mutex, &huawei_wmi->wmi_lock)
+ len = huawei_wmi_cmd(huawei_wmi, FN_LOCK_GET, data, sizeof(data));
if (len < 0)
return len;
if (!len)
@@ -588,7 +731,8 @@ static int huawei_wmi_fn_lock_set(int on)
arg.cmd = FN_LOCK_SET;
arg.args[2] = on + 1; // 0 undefined, 1 off, 2 on.
- return huawei_wmi_cmd(arg.cmd, NULL, 0);
+ guard(mutex)(&huawei_wmi->wmi_lock);
+ return huawei_wmi_cmd(huawei_wmi, arg.cmd, NULL, 0);
}
static ssize_t fn_lock_state_show(struct device *dev,
@@ -760,6 +904,11 @@ static void huawei_wmi_process_key(struct input_dev *idev, int code)
kfree(response.pointer);
}
+ /* A delayed mode event may describe an earlier switch. */
+ if ((code == 0x2a0 || code == 0x2a1 || code == 0x2a6) &&
+ huawei_wmi_profile_sync(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);
@@ -840,6 +989,8 @@ static int huawei_wmi_probe(struct platform_device *pdev)
huawei_wmi->wdev = method ? *method : NULL;
platform_set_drvdata(pdev, huawei_wmi);
huawei_wmi->dev = &pdev->dev;
+ mutex_init(&huawei_wmi->wmi_lock);
+ huawei_wmi->reported_profile = PLATFORM_PROFILE_LAST;
for (guid = huawei_wmi_events_id_table; *guid->guid_string; guid++) {
if (!wmi_has_guid(guid->guid_string))
@@ -859,12 +1010,11 @@ static int huawei_wmi_probe(struct platform_device *pdev)
}
if (huawei_wmi->wdev) {
- 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;
@@ -881,6 +1031,7 @@ static void huawei_wmi_remove(struct platform_device *pdev)
guid++;
}
+ huawei_wmi_profile_exit(huawei_wmi);
if (huawei_wmi->wdev) {
huawei_wmi_debugfs_exit(&pdev->dev);
huawei_wmi_battery_exit(&pdev->dev);
@@ -888,9 +1039,19 @@ static void huawei_wmi_remove(struct platform_device *pdev)
}
}
+/* CREM-WXX9 can leave Turbo during s2idle without a WMI event. */
+static int huawei_wmi_resume(struct device *dev)
+{
+ huawei_wmi_profile_sync(dev_get_drvdata(dev));
+ return 0;
+}
+
+static DEFINE_SIMPLE_DEV_PM_OPS(huawei_wmi_pm, NULL, huawei_wmi_resume);
+
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 [flat|nested] 8+ messages in thread
* [PATCH v4 5/5] platform/x86: huawei-wmi: bind hotkeys and key data on the WMI bus
[not found] <20261130101218.243426-1-12313607@mail.sustech.edu.cn>
` (3 preceding siblings ...)
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 ` Fuwei Xie
4 siblings, 0 replies; 8+ messages in thread
From: Fuwei Xie @ 2026-11-30 10:12 UTC (permalink / raw)
To: Ilpo Järvinen, Hans de Goede
Cc: Fuwei Xie, platform-driver-x86, linux-kernel, Eren Özen,
Zheng Shuyang
Register WMI drivers for both event GUIDs and the legacy key data
block. Resolve the 0x80 marker through the data block on the same
mapper before dispatching the resulting hotkey.
Keep the platform device as the parent of the existing userspace
interfaces. Event-only machines can bind independently. Serialize
profile event handling with method device detach so a hotkey cannot
access a removed profile. Handle system resume on the method device
which now owns the profile state.
Use the sized WMI interface for debugfs calls as well. Display the
marshalled reply bytes and use debugfs_remove() for subtree removal.
Treat 0x2a5 like 0x2a6 and re-read the firmware mode; FMB-P emits it
for TBF2-restricted transitions.
Signed-off-by: Fuwei Xie <12313607@mail.sustech.edu.cn>
---
drivers/platform/x86/huawei-wmi.c | 509 +++++++++++++++---------------
1 file changed, 257 insertions(+), 252 deletions(-)
diff --git a/drivers/platform/x86/huawei-wmi.c b/drivers/platform/x86/huawei-wmi.c
index 8eaa21757..38923b72c 100644
--- a/drivers/platform/x86/huawei-wmi.c
+++ b/drivers/platform/x86/huawei-wmi.c
@@ -15,6 +15,7 @@
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/mutex.h>
+#include <linux/notifier.h>
#include <linux/platform_device.h>
#include <linux/platform_profile.h>
#include <linux/pm.h>
@@ -30,8 +31,8 @@
#define HWMI_EVENT_GUID "ABBC0F5C-8EA1-11D1-A000-C90629100000"
/* Legacy GUIDs */
-#define WMI0_EXPENSIVE_GUID "39142400-C6A3-40fa-BADB-8A2652834100"
-#define WMI0_EVENT_GUID "59142400-C6A3-40fa-BADB-8A2652834100"
+#define WMI0_EXPENSIVE_GUID "39142400-C6A3-40FA-BADB-8A2652834100"
+#define WMI0_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
/* HWMI commands */
@@ -56,7 +57,8 @@ struct quirk_entry {
bool report_brightness;
};
-static struct quirk_entry *quirks;
+static struct quirk_entry quirk_unknown = {};
+static struct quirk_entry *quirks = &quirk_unknown;
struct huawei_wmi_debug {
struct dentry *root;
@@ -77,7 +79,14 @@ struct huawei_wmi {
enum platform_profile_option reported_profile;
};
-static struct huawei_wmi *huawei_wmi;
+/* The existing platform device owns the system-wide userspace paths. */
+static struct platform_device *huawei_pdev;
+
+static BLOCKING_NOTIFIER_HEAD(huawei_wmi_keycode_chain);
+
+/* Pins the method driver's profile state while a hotkey is being handled. */
+static DEFINE_MUTEX(huawei_profile_event_lock);
+static struct huawei_wmi *huawei_profile_owner;
static const struct key_entry huawei_wmi_keymap[] = {
{ KE_KEY, 0x281, { KEY_BRIGHTNESSDOWN } },
@@ -121,8 +130,6 @@ static int __init dmi_matched(const struct dmi_system_id *dmi)
return 1;
}
-static struct quirk_entry quirk_unknown = {
-};
static struct quirk_entry quirk_battery_reset = {
.battery_reset = true,
@@ -323,7 +330,7 @@ static void huawei_wmi_profile_setup(struct huawei_wmi *huawei)
platform_profile_notify(dev);
}
-/* Call after removing the WMI event handlers. */
+/* Call after detaching the method device from the hotkey handler. */
static void huawei_wmi_profile_exit(struct huawei_wmi *huawei)
{
if (!huawei->profile_dev)
@@ -338,6 +345,8 @@ static void huawei_wmi_profile_exit(struct huawei_wmi *huawei)
static int huawei_wmi_micmute_led_set(struct led_classdev *led_cdev,
enum led_brightness brightness)
{
+ struct huawei_wmi *huawei = container_of(led_cdev, struct huawei_wmi, cdev);
+
/* This is a workaround until the "legacy" interface is implemented. */
if (quirks && quirks->ec_micmute) {
char *acpi_method;
@@ -379,11 +388,16 @@ static int huawei_wmi_micmute_led_set(struct led_classdev *led_cdev,
arg.cmd = MICMUTE_LED_SET;
arg.args[2] = brightness;
- guard(mutex)(&huawei_wmi->wmi_lock);
- return huawei_wmi_cmd(huawei_wmi, arg.cmd, NULL, 0);
+ guard(mutex)(&huawei->wmi_lock);
+ return huawei_wmi_cmd(huawei, arg.cmd, NULL, 0);
}
}
+static void huawei_wmi_led_unregister(void *data)
+{
+ led_classdev_unregister(data);
+}
+
static void huawei_wmi_leds_setup(struct device *dev)
{
struct huawei_wmi *huawei = dev_get_drvdata(dev);
@@ -392,21 +406,22 @@ static void huawei_wmi_leds_setup(struct device *dev)
huawei->cdev.max_brightness = 1;
huawei->cdev.brightness_set_blocking = &huawei_wmi_micmute_led_set;
huawei->cdev.default_trigger = "audio-micmute";
- huawei->cdev.dev = dev;
huawei->cdev.flags = LED_CORE_SUSPENDRESUME;
- devm_led_classdev_register(dev, &huawei->cdev);
+ if (!led_classdev_register(dev, &huawei->cdev))
+ devm_add_action_or_reset(&huawei->wdev->dev, huawei_wmi_led_unregister,
+ &huawei->cdev);
}
/* Battery protection */
-static int huawei_wmi_battery_get(int *start, int *end)
+static int huawei_wmi_battery_get(struct huawei_wmi *huawei, int *start, int *end)
{
u8 data[0xff];
int len;
- scoped_guard(mutex, &huawei_wmi->wmi_lock)
- len = huawei_wmi_cmd(huawei_wmi, BATTERY_THRESH_GET, data, sizeof(data));
+ scoped_guard(mutex, &huawei->wmi_lock)
+ len = huawei_wmi_cmd(huawei, BATTERY_THRESH_GET, data, sizeof(data));
if (len < 0)
return len;
if (len < 2)
@@ -423,7 +438,7 @@ static int huawei_wmi_battery_get(int *start, int *end)
return 0;
}
-static int huawei_wmi_battery_set(int start, int end)
+static int huawei_wmi_battery_set(struct huawei_wmi *huawei, int start, int end)
{
union hwmi_arg arg;
int err;
@@ -441,24 +456,29 @@ static int huawei_wmi_battery_set(int start, int end)
* make sure these values make their way to EC memory.
*/
if (quirks && quirks->battery_reset && start == 0 && end == 100) {
- err = huawei_wmi_battery_set(0, 0);
+ err = huawei_wmi_battery_set(huawei, 0, 0);
if (err)
return err;
msleep(1000);
}
- guard(mutex)(&huawei_wmi->wmi_lock);
- return huawei_wmi_cmd(huawei_wmi, arg.cmd, NULL, 0);
+ guard(mutex)(&huawei->wmi_lock);
+ return huawei_wmi_cmd(huawei, arg.cmd, NULL, 0);
}
+/*
+ * The per-battery files belong to the ACPI battery. They access the platform
+ * controller only while its battery hook is registered.
+ */
static ssize_t charge_control_start_threshold_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
+ struct huawei_wmi *huawei = platform_get_drvdata(huawei_pdev);
int err, start;
- err = huawei_wmi_battery_get(&start, NULL);
+ err = huawei_wmi_battery_get(huawei, &start, NULL);
if (err)
return err;
@@ -469,9 +489,10 @@ static ssize_t charge_control_end_threshold_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
+ struct huawei_wmi *huawei = platform_get_drvdata(huawei_pdev);
int err, end;
- err = huawei_wmi_battery_get(NULL, &end);
+ err = huawei_wmi_battery_get(huawei, NULL, &end);
if (err)
return err;
@@ -482,9 +503,10 @@ static ssize_t charge_control_thresholds_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
+ struct huawei_wmi *huawei = dev_get_drvdata(dev);
int err, start, end;
- err = huawei_wmi_battery_get(&start, &end);
+ err = huawei_wmi_battery_get(huawei, &start, &end);
if (err)
return err;
@@ -495,16 +517,17 @@ static ssize_t charge_control_start_threshold_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
+ struct huawei_wmi *huawei = platform_get_drvdata(huawei_pdev);
int err, start, end;
- err = huawei_wmi_battery_get(NULL, &end);
+ err = huawei_wmi_battery_get(huawei, NULL, &end);
if (err)
return err;
if (sscanf(buf, "%d", &start) != 1)
return -EINVAL;
- err = huawei_wmi_battery_set(start, end);
+ err = huawei_wmi_battery_set(huawei, start, end);
if (err)
return err;
@@ -515,16 +538,17 @@ static ssize_t charge_control_end_threshold_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
+ struct huawei_wmi *huawei = platform_get_drvdata(huawei_pdev);
int err, start, end;
- err = huawei_wmi_battery_get(&start, NULL);
+ err = huawei_wmi_battery_get(huawei, &start, NULL);
if (err)
return err;
if (sscanf(buf, "%d", &end) != 1)
return -EINVAL;
- err = huawei_wmi_battery_set(start, end);
+ err = huawei_wmi_battery_set(huawei, start, end);
if (err)
return err;
@@ -535,12 +559,13 @@ static ssize_t charge_control_thresholds_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
+ struct huawei_wmi *huawei = dev_get_drvdata(dev);
int err, start, end;
if (sscanf(buf, "%d %d", &start, &end) != 2)
return -EINVAL;
- err = huawei_wmi_battery_set(start, end);
+ err = huawei_wmi_battery_set(huawei, start, end);
if (err)
return err;
@@ -585,7 +610,7 @@ static void huawei_wmi_battery_setup(struct device *dev)
struct huawei_wmi *huawei = dev_get_drvdata(dev);
huawei->battery_available = true;
- if (huawei_wmi_battery_get(NULL, NULL)) {
+ if (huawei_wmi_battery_get(huawei, NULL, NULL)) {
huawei->battery_available = false;
return;
}
@@ -694,7 +719,7 @@ static int huawei_acpi_fn_lock_set(int on)
return 0;
}
-static int huawei_wmi_fn_lock_get(int *on)
+static int huawei_wmi_fn_lock_get(struct huawei_wmi *huawei, int *on)
{
u8 data[0xff];
int len;
@@ -702,8 +727,8 @@ static int huawei_wmi_fn_lock_get(int *on)
if (acpi_has_method(NULL, "\\GFRS"))
return huawei_acpi_fn_lock_get(on);
- scoped_guard(mutex, &huawei_wmi->wmi_lock)
- len = huawei_wmi_cmd(huawei_wmi, FN_LOCK_GET, data, sizeof(data));
+ scoped_guard(mutex, &huawei->wmi_lock)
+ len = huawei_wmi_cmd(huawei, FN_LOCK_GET, data, sizeof(data));
if (len < 0)
return len;
if (!len)
@@ -719,7 +744,7 @@ static int huawei_wmi_fn_lock_get(int *on)
return 0;
}
-static int huawei_wmi_fn_lock_set(int on)
+static int huawei_wmi_fn_lock_set(struct huawei_wmi *huawei, int on)
{
union hwmi_arg arg;
@@ -731,8 +756,8 @@ static int huawei_wmi_fn_lock_set(int on)
arg.cmd = FN_LOCK_SET;
arg.args[2] = on + 1; // 0 undefined, 1 off, 2 on.
- guard(mutex)(&huawei_wmi->wmi_lock);
- return huawei_wmi_cmd(huawei_wmi, arg.cmd, NULL, 0);
+ guard(mutex)(&huawei->wmi_lock);
+ return huawei_wmi_cmd(huawei, arg.cmd, NULL, 0);
}
static ssize_t fn_lock_state_show(struct device *dev,
@@ -741,7 +766,7 @@ static ssize_t fn_lock_state_show(struct device *dev,
{
int err, on;
- err = huawei_wmi_fn_lock_get(&on);
+ err = huawei_wmi_fn_lock_get(dev_get_drvdata(dev), &on);
if (err)
return err;
@@ -758,7 +783,7 @@ static ssize_t fn_lock_state_store(struct device *dev,
on < 0 || on > 1)
return -EINVAL;
- err = huawei_wmi_fn_lock_set(on);
+ err = huawei_wmi_fn_lock_set(dev_get_drvdata(dev), on);
if (err)
return err;
@@ -772,7 +797,7 @@ static void huawei_wmi_fn_lock_setup(struct device *dev)
struct huawei_wmi *huawei = dev_get_drvdata(dev);
huawei->fn_lock_available = true;
- if (huawei_wmi_fn_lock_get(NULL)) {
+ if (huawei_wmi_fn_lock_get(huawei, NULL)) {
huawei->fn_lock_available = false;
return;
}
@@ -790,69 +815,26 @@ static void huawei_wmi_fn_lock_exit(struct device *dev)
/* debugfs */
-static void huawei_wmi_debugfs_call_dump(struct seq_file *m, void *data,
- union acpi_object *obj)
-{
- struct huawei_wmi *huawei = m->private;
- int i;
-
- switch (obj->type) {
- case ACPI_TYPE_INTEGER:
- seq_printf(m, "0x%llx", obj->integer.value);
- break;
- case ACPI_TYPE_STRING:
- seq_printf(m, "\"%.*s\"", obj->string.length, obj->string.pointer);
- break;
- case ACPI_TYPE_BUFFER:
- seq_puts(m, "{");
- for (i = 0; i < obj->buffer.length; i++) {
- seq_printf(m, "0x%02x", obj->buffer.pointer[i]);
- if (i < obj->buffer.length - 1)
- seq_puts(m, ",");
- }
- seq_puts(m, "}");
- break;
- case ACPI_TYPE_PACKAGE:
- seq_puts(m, "[");
- for (i = 0; i < obj->package.count; i++) {
- huawei_wmi_debugfs_call_dump(m, huawei, &obj->package.elements[i]);
- if (i < obj->package.count - 1)
- seq_puts(m, ",");
- }
- seq_puts(m, "]");
- break;
- default:
- dev_err(huawei->dev, "Unexpected obj type, got %d\n", obj->type);
- return;
- }
-}
-
-/* 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;
-
- in.length = sizeof(u64);
- in.pointer = &huawei->debug.arg;
+ u64 arg = READ_ONCE(huawei->debug.arg);
+ struct wmi_buffer in = { .length = sizeof(arg), .data = &arg };
+ struct wmi_buffer out;
+ int err;
- if (ACPI_FAILURE(huawei_wmi_debugfs_call(huawei, &in, &out)))
- return -ENODEV;
+ scoped_guard(mutex, &huawei->wmi_lock)
+ err = wmidev_invoke_method(huawei->wdev, 0, 1, &in, &out, 0);
+ if (err)
+ return err;
- union acpi_object *obj __free(kfree) = out.pointer;
+ u8 *bytes __free(kfree) = out.data;
- if (!obj)
- return -EIO;
+ seq_putc(m, '{');
+ for (size_t i = 0; i < out.length; i++)
+ seq_printf(m, "%s0x%02x", i ? "," : "", bytes[i]);
+ seq_putc(m, '}');
- huawei_wmi_debugfs_call_dump(m, huawei, obj);
return 0;
}
@@ -874,40 +856,98 @@ static void huawei_wmi_debugfs_exit(struct device *dev)
{
struct huawei_wmi *huawei = dev_get_drvdata(dev);
- debugfs_remove_recursive(huawei->debug.root);
+ debugfs_remove(huawei->debug.root);
}
/* Input */
-static void huawei_wmi_process_key(struct input_dev *idev, int code)
+/* WMI0 reports 0x80 and provides the actual scancode in a sibling data block. */
+struct huawei_wmi_keycode {
+ struct wmi_device *event;
+ u32 code;
+};
+
+struct huawei_wmi_keyblock {
+ struct wmi_device *wdev;
+ struct notifier_block notifier;
+};
+
+static int huawei_wmi_query_keycode(struct notifier_block *nb, unsigned long action, void *data)
+{
+ struct huawei_wmi_keyblock *block = container_of(nb, struct huawei_wmi_keyblock, notifier);
+ struct huawei_wmi_keycode *key = data;
+ struct wmi_buffer out;
+
+ if (block->wdev->dev.parent != key->event->dev.parent)
+ return NOTIFY_DONE;
+
+ if (wmidev_query_block(block->wdev, 0, &out, sizeof(__le32)))
+ return NOTIFY_BAD;
+
+ u8 *buf __free(kfree) = out.data;
+
+ key->code = le32_to_cpup((const __le32 *)buf);
+ return NOTIFY_STOP;
+}
+
+static int huawei_wmi_keyblock_probe(struct wmi_device *wdev, const void *context)
+{
+ struct huawei_wmi_keyblock *block;
+
+ block = devm_kzalloc(&wdev->dev, sizeof(*block), GFP_KERNEL);
+ if (!block)
+ return -ENOMEM;
+
+ block->wdev = wdev;
+ block->notifier.notifier_call = huawei_wmi_query_keycode;
+ dev_set_drvdata(&wdev->dev, block);
+
+ return blocking_notifier_chain_register(&huawei_wmi_keycode_chain, &block->notifier);
+}
+
+static void huawei_wmi_keyblock_remove(struct wmi_device *wdev)
+{
+ struct huawei_wmi_keyblock *block = dev_get_drvdata(&wdev->dev);
+
+ blocking_notifier_chain_unregister(&huawei_wmi_keycode_chain, &block->notifier);
+}
+
+static const struct wmi_device_id huawei_wmi_keyblock_ids[] = {
+ { WMI0_EXPENSIVE_GUID },
+ {}
+};
+MODULE_DEVICE_TABLE(wmi, huawei_wmi_keyblock_ids);
+
+static struct wmi_driver huawei_wmi_keyblock_driver = {
+ .driver.name = "huawei-wmi-keyblock",
+ .id_table = huawei_wmi_keyblock_ids,
+ .probe = huawei_wmi_keyblock_probe,
+ .remove = huawei_wmi_keyblock_remove,
+ .no_singleton = true,
+};
+
+static void huawei_wmi_input_notify(struct wmi_device *wdev, const struct wmi_buffer *data)
{
+ struct input_dev *idev = dev_get_drvdata(&wdev->dev);
+ u32 code = le32_to_cpup(data->data);
const struct key_entry *key;
- /*
- * WMI0 uses code 0x80 to indicate a hotkey event.
- * The actual key is fetched from the method WQ00
- * using WMI0_EXPENSIVE_GUID.
- */
if (code == 0x80) {
- struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
- union acpi_object *obj;
- acpi_status status;
+ struct huawei_wmi_keycode keycode = { .event = wdev };
- status = wmi_query_block(WMI0_EXPENSIVE_GUID, 0, &response);
- if (ACPI_FAILURE(status))
+ if (blocking_notifier_call_chain(&huawei_wmi_keycode_chain, 0, &keycode) !=
+ NOTIFY_STOP)
return;
-
- obj = (union acpi_object *)response.pointer;
- if (obj && obj->type == ACPI_TYPE_INTEGER)
- code = obj->integer.value;
-
- kfree(response.pointer);
+ code = keycode.code;
}
/* A delayed mode event may describe an earlier switch. */
- if ((code == 0x2a0 || code == 0x2a1 || code == 0x2a6) &&
- huawei_wmi_profile_sync(huawei_wmi))
- return;
+ if (code == 0x2a0 || code == 0x2a1 || code == 0x2a5 || code == 0x2a6) {
+ guard(mutex)(&huawei_profile_event_lock);
+
+ if (huawei_profile_owner && huawei_wmi_profile_sync(huawei_profile_owner))
+ return;
+ }
key = sparse_keymap_entry_from_scancode(idev, code);
if (!key) {
@@ -916,127 +956,98 @@ static void huawei_wmi_process_key(struct input_dev *idev, int code)
}
if (quirks && !quirks->report_brightness &&
- (key->sw.code == KEY_BRIGHTNESSDOWN ||
- key->sw.code == KEY_BRIGHTNESSUP))
+ (key->sw.code == KEY_BRIGHTNESSDOWN || key->sw.code == KEY_BRIGHTNESSUP))
return;
sparse_keymap_report_entry(idev, key, 1, true);
}
-static void huawei_wmi_input_notify(union acpi_object *obj, void *context)
-{
- struct input_dev *idev = (struct input_dev *)context;
-
- if (obj && obj->type == ACPI_TYPE_INTEGER)
- huawei_wmi_process_key(idev, obj->integer.value);
- else
- dev_err(&idev->dev, "Bad response type\n");
-}
-
-static int huawei_wmi_input_setup(struct device *dev, const char *guid)
+static int huawei_wmi_input_probe(struct wmi_device *wdev, const void *context)
{
struct input_dev *idev;
- acpi_status status;
int err;
- idev = devm_input_allocate_device(dev);
+ idev = input_allocate_device();
if (!idev)
return -ENOMEM;
idev->name = "Huawei WMI hotkeys";
idev->phys = "wmi/input0";
idev->id.bustype = BUS_HOST;
- idev->dev.parent = dev;
+ idev->dev.parent = &huawei_pdev->dev;
err = sparse_keymap_setup(idev, huawei_wmi_keymap, NULL);
- if (err)
- return err;
-
- err = input_register_device(idev);
- if (err)
+ if (!err)
+ err = input_register_device(idev);
+ if (err) {
+ input_free_device(idev);
return err;
+ }
- status = wmi_install_notify_handler(guid, huawei_wmi_input_notify, idev);
- if (ACPI_FAILURE(status))
- return -EIO;
-
+ dev_set_drvdata(&wdev->dev, idev);
return 0;
}
-static void huawei_wmi_input_exit(struct device *dev, const char *guid)
+static void huawei_wmi_input_remove(struct wmi_device *wdev)
{
- wmi_remove_notify_handler(guid);
+ input_unregister_device(dev_get_drvdata(&wdev->dev));
}
-/* Huawei driver */
-
-static const struct wmi_device_id huawei_wmi_events_id_table[] = {
- { .guid_string = WMI0_EVENT_GUID },
- { .guid_string = HWMI_EVENT_GUID },
- { }
+static const struct wmi_device_id huawei_wmi_event_ids[] = {
+ { WMI0_EVENT_GUID },
+ { HWMI_EVENT_GUID },
+ {}
+};
+MODULE_DEVICE_TABLE(wmi, huawei_wmi_event_ids);
+
+static struct wmi_driver huawei_wmi_event_driver = {
+ .driver.name = "huawei-wmi-events",
+ .id_table = huawei_wmi_event_ids,
+ .probe = huawei_wmi_input_probe,
+ .remove = huawei_wmi_input_remove,
+ .notify_new = huawei_wmi_input_notify,
+ .min_event_size = sizeof(__le32),
+ .no_singleton = true,
};
-static int huawei_wmi_probe(struct platform_device *pdev)
+/* The method device supplies the laptop-wide controls on the platform device. */
+static int huawei_wmi_method_probe(struct wmi_device *wdev, const void *context)
{
- const struct wmi_device_id *guid;
- struct wmi_device **method = dev_get_platdata(&pdev->dev);
- int err;
+ struct device *dev = &huawei_pdev->dev;
+ struct huawei_wmi *huawei;
- huawei_wmi = devm_kzalloc(&pdev->dev, sizeof(*huawei_wmi), GFP_KERNEL);
- if (!huawei_wmi)
+ huawei = devm_kzalloc(&wdev->dev, sizeof(*huawei), GFP_KERNEL);
+ if (!huawei)
return -ENOMEM;
- huawei_wmi->wdev = method ? *method : NULL;
- platform_set_drvdata(pdev, huawei_wmi);
- huawei_wmi->dev = &pdev->dev;
- mutex_init(&huawei_wmi->wmi_lock);
- huawei_wmi->reported_profile = PLATFORM_PROFILE_LAST;
-
- for (guid = huawei_wmi_events_id_table; *guid->guid_string; guid++) {
- if (!wmi_has_guid(guid->guid_string))
- continue;
-
- err = huawei_wmi_input_setup(&pdev->dev, guid->guid_string);
- if (!err)
- continue;
-
- dev_err(&pdev->dev, "Failed to setup input on %s\n", guid->guid_string);
- while (guid != huawei_wmi_events_id_table) {
- guid--;
- if (wmi_has_guid(guid->guid_string))
- huawei_wmi_input_exit(&pdev->dev, guid->guid_string);
- }
- return err;
- }
-
- if (huawei_wmi->wdev) {
- 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);
- }
+ huawei->wdev = wdev;
+ huawei->dev = dev;
+ mutex_init(&huawei->wmi_lock);
+ huawei->reported_profile = PLATFORM_PROFILE_LAST;
+ dev_set_drvdata(&wdev->dev, huawei);
+ platform_set_drvdata(huawei_pdev, huawei);
+ huawei_wmi_leds_setup(dev);
+ huawei_wmi_fn_lock_setup(dev);
+ huawei_wmi_battery_setup(dev);
+ huawei_wmi_debugfs_setup(dev);
+ scoped_guard(mutex, &huawei_profile_event_lock)
+ huawei_profile_owner = huawei;
+ huawei_wmi_profile_setup(huawei);
return 0;
}
-static void huawei_wmi_remove(struct platform_device *pdev)
+static void huawei_wmi_method_remove(struct wmi_device *wdev)
{
- const struct wmi_device_id *guid = huawei_wmi_events_id_table;
-
- while (*guid->guid_string) {
- if (wmi_has_guid(guid->guid_string))
- huawei_wmi_input_exit(&pdev->dev, guid->guid_string);
-
- guid++;
- }
-
- huawei_wmi_profile_exit(huawei_wmi);
- if (huawei_wmi->wdev) {
- huawei_wmi_debugfs_exit(&pdev->dev);
- huawei_wmi_battery_exit(&pdev->dev);
- huawei_wmi_fn_lock_exit(&pdev->dev);
- }
+ struct huawei_wmi *huawei = dev_get_drvdata(&wdev->dev);
+
+ scoped_guard(mutex, &huawei_profile_event_lock)
+ huawei_profile_owner = NULL;
+ huawei_wmi_profile_exit(huawei);
+ huawei_wmi_debugfs_exit(huawei->dev);
+ huawei_wmi_battery_exit(huawei->dev);
+ huawei_wmi_fn_lock_exit(huawei->dev);
+ platform_set_drvdata(huawei_pdev, NULL);
}
/* CREM-WXX9 can leave Turbo during s2idle without a WMI event. */
@@ -1048,94 +1059,88 @@ static int huawei_wmi_resume(struct device *dev)
static DEFINE_SIMPLE_DEV_PM_OPS(huawei_wmi_pm, NULL, huawei_wmi_resume);
-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,
+static const struct wmi_device_id huawei_wmi_method_ids[] = {
+ { HWMI_METHOD_GUID },
+ {}
};
+MODULE_DEVICE_TABLE(wmi, huawei_wmi_method_ids);
-/*
- * Keep the platform device and its existing userspace paths on both
- * method-capable and older event-only machines.
- */
-static struct platform_device *legacy_pdev;
+static struct wmi_driver huawei_wmi_method_driver = {
+ .driver.name = "huawei-wmi-method",
+ .driver.pm = pm_sleep_ptr(&huawei_wmi_pm),
+ .id_table = huawei_wmi_method_ids,
+ .probe = huawei_wmi_method_probe,
+ .remove = huawei_wmi_method_remove,
+};
-static int huawei_wmi_method_probe(struct wmi_device *wdev, const void *context)
+static int huawei_wmi_probe(struct platform_device *pdev)
{
- struct platform_device *pdev;
+ int err;
- pdev = platform_device_register_data(NULL, "huawei-wmi", PLATFORM_DEVID_NONE,
- &wdev, sizeof(wdev));
- if (IS_ERR(pdev))
- return PTR_ERR(pdev);
+ if (pdev != huawei_pdev)
+ return -ENODEV;
- dev_set_drvdata(&wdev->dev, pdev);
- return 0;
+ err = wmi_driver_register(&huawei_wmi_keyblock_driver);
+ if (err)
+ return err;
+
+ err = wmi_driver_register(&huawei_wmi_event_driver);
+ if (err) {
+ wmi_driver_unregister(&huawei_wmi_keyblock_driver);
+ return err;
+ }
+
+ /* Receive profile changes from the time the method driver takes its first sample. */
+ err = wmi_driver_register(&huawei_wmi_method_driver);
+ if (err) {
+ wmi_driver_unregister(&huawei_wmi_event_driver);
+ wmi_driver_unregister(&huawei_wmi_keyblock_driver);
+ }
+ return err;
}
-static void huawei_wmi_method_remove(struct wmi_device *wdev)
+static void huawei_wmi_remove(struct platform_device *pdev)
{
- platform_device_unregister(dev_get_drvdata(&wdev->dev));
+ wmi_driver_unregister(&huawei_wmi_event_driver);
+ wmi_driver_unregister(&huawei_wmi_method_driver);
+ wmi_driver_unregister(&huawei_wmi_keyblock_driver);
}
-static const struct wmi_device_id huawei_wmi_method_ids[] = {
- { HWMI_METHOD_GUID },
- { }
-};
-MODULE_DEVICE_TABLE(wmi, huawei_wmi_method_ids);
-
-static struct wmi_driver huawei_wmi_method_driver = {
- .driver.name = "huawei-wmi-method",
- .id_table = huawei_wmi_method_ids,
- .probe = huawei_wmi_method_probe,
- .remove = huawei_wmi_method_remove,
+static struct platform_driver huawei_wmi_driver = {
+ .driver.name = "huawei-wmi",
+ .probe = huawei_wmi_probe,
+ .remove = huawei_wmi_remove,
};
-static __init int huawei_wmi_init(void)
+static int __init huawei_wmi_init(void)
{
int err;
- quirks = &quirk_unknown;
dmi_check_system(huawei_quirks);
if (battery_reset != -1)
quirks->battery_reset = battery_reset;
if (report_brightness != -1)
quirks->report_brightness = report_brightness;
- err = platform_driver_register(&huawei_wmi_driver);
- if (err)
- return err;
+ huawei_pdev = platform_device_register_simple("huawei-wmi", PLATFORM_DEVID_NONE, NULL, 0);
+ if (IS_ERR(huawei_pdev))
+ return PTR_ERR(huawei_pdev);
- if (wmi_has_guid(HWMI_METHOD_GUID)) {
- err = wmi_driver_register(&huawei_wmi_method_driver);
- } else {
- legacy_pdev = platform_device_register_simple("huawei-wmi", PLATFORM_DEVID_NONE,
- NULL, 0);
- err = PTR_ERR_OR_ZERO(legacy_pdev);
- }
+ err = platform_driver_register(&huawei_wmi_driver);
if (err)
- platform_driver_unregister(&huawei_wmi_driver);
-
+ platform_device_unregister(huawei_pdev);
return err;
}
-static __exit void huawei_wmi_exit(void)
+static void __exit huawei_wmi_exit(void)
{
- if (legacy_pdev)
- platform_device_unregister(legacy_pdev);
- else
- wmi_driver_unregister(&huawei_wmi_method_driver);
-
platform_driver_unregister(&huawei_wmi_driver);
+ platform_device_unregister(huawei_pdev);
}
module_init(huawei_wmi_init);
module_exit(huawei_wmi_exit);
-MODULE_DEVICE_TABLE(wmi, huawei_wmi_events_id_table);
MODULE_AUTHOR("Ayman Bagabas <ayman.bagabas@gmail.com>");
MODULE_DESCRIPTION("Huawei WMI laptop extras driver");
MODULE_LICENSE("GPL v2");
--
2.55.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v4 1/5] platform/x86: huawei-wmi: scope method responses to each call
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
0 siblings, 0 replies; 8+ messages in thread
From: Ilpo Järvinen @ 2026-12-01 10:40 UTC (permalink / raw)
To: Fuwei Xie
Cc: Hans de Goede, platform-driver-x86, LKML, Eren Özen, Zheng Shuyang
On Mon, 30 Nov 2026, Fuwei Xie wrote:
> Keep each ACPI allocation alive while decoding its response and release
> it on every exit with __free(kfree). Give each handshake attempt a
> fresh output descriptor so retrying cannot reuse the first allocation.
>
> Signed-off-by: Fuwei Xie <12313607@mail.sustech.edu.cn>
> ---
> drivers/platform/x86/huawei-wmi.c | 59 ++++++++++++-------------------
> 1 file changed, 23 insertions(+), 36 deletions(-)
>
> diff --git a/drivers/platform/x86/huawei-wmi.c b/drivers/platform/x86/huawei-wmi.c
> index 96ee9a97c..559731d15 100644
> --- a/drivers/platform/x86/huawei-wmi.c
> +++ b/drivers/platform/x86/huawei-wmi.c
> @@ -177,9 +177,7 @@ static int huawei_wmi_call(struct huawei_wmi *huawei,
> static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
> {
> struct huawei_wmi *huawei = huawei_wmi;
> - struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
> struct acpi_buffer in;
> - union acpi_object *obj;
> size_t len;
> int err, i;
>
> @@ -190,15 +188,17 @@ static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
> * HWMI and if we get a non-zero return status we evaluate it again.
> */
> for (i = 0; i < 2; i++) {
> + struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
> +
> err = huawei_wmi_call(huawei, &in, &out);
> if (err)
> - goto fail_cmd;
> + return err;
>
> - obj = out.pointer;
> - if (!obj) {
> - err = -EIO;
> - goto fail_cmd;
> - }
> + union acpi_object *allocation __free(kfree) = out.pointer;
> + union acpi_object *obj = allocation;
It's not clear to me what having allocation achieves over plain:
union acpi_object *obj __free(kfree) = out.pointer;
> +
> + if (!obj)
Don't leave a blank line between the assignment and its error handling
(analoguous to no-blank-lines between call and its error handling).
> + return -EIO;
>
> switch (obj->type) {
> /* Models that implement both "legacy" and HWMI tend to return a 0x104
> @@ -211,8 +211,7 @@ static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
> len = 0x100;
> } else {
> dev_err(huawei->dev, "Bad buffer length, got %d\n", obj->buffer.length);
> - err = -EIO;
> - goto fail_cmd;
> + return -EIO;
> }
>
> break;
> @@ -222,15 +221,13 @@ static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
> 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;
> + 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);
> - err = -EIO;
> - goto fail_cmd;
> + return -EIO;
> }
> len = obj->buffer.length;
>
> @@ -238,24 +235,19 @@ static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
> /* Shouldn't get here! */
> default:
> dev_err(huawei->dev, "Unexpected obj type, got: %d\n", obj->type);
> - err = -EIO;
> - goto fail_cmd;
> + return -EIO;
> }
>
> - if (!*obj->buffer.pointer)
> - break;
> - }
> + if (*obj->buffer.pointer && !i)
> + continue;
>
> - err = (*obj->buffer.pointer) ? -ENODEV : 0;
> + if (buf)
> + memcpy(buf, obj->buffer.pointer, min(buflen, len));
>
> - if (buf) {
> - len = min(buflen, len);
> - memcpy(buf, obj->buffer.pointer, len);
> + return *obj->buffer.pointer ? -ENODEV : 0;
Can this refactoring be done separately. I think there's too much
unrelated stuff going on this patch which makes it hard to review.
> }
>
> -fail_cmd:
> - kfree(out.pointer);
> - return err;
> + return -ENODEV;
> }
>
> /* LEDs */
> @@ -753,7 +745,6 @@ 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;
> - union acpi_object *obj;
> int err;
>
> in.length = sizeof(u64);
> @@ -763,17 +754,13 @@ static int huawei_wmi_debugfs_call_show(struct seq_file *m, void *data)
> if (err)
> return err;
>
> - obj = out.pointer;
> - if (!obj) {
> - err = -EIO;
> - goto fail_debugfs_call;
> - }
> + union acpi_object *obj __free(kfree) = out.pointer;
>
> - huawei_wmi_debugfs_call_dump(m, huawei, obj);
> + if (!obj)
No blank line.
> + return -EIO;
>
> -fail_debugfs_call:
> - kfree(out.pointer);
> - return err;
> + huawei_wmi_debugfs_call_dump(m, huawei, obj);
> + return 0;
> }
>
> DEFINE_SHOW_ATTRIBUTE(huawei_wmi_debugfs_call);
>
This is generally moving into right direction but needs a bit more
work to make it easy to review and see everything is correctly
transformed.
--
i.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v4 2/5] platform/x86: huawei-wmi: bind the Huawei method device
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
0 siblings, 0 replies; 8+ messages in thread
From: Ilpo Järvinen @ 2026-12-01 10:57 UTC (permalink / raw)
To: Fuwei Xie
Cc: Hans de Goede, platform-driver-x86, LKML, Eren Özen, Zheng Shuyang
On Mon, 30 Nov 2026, Fuwei Xie wrote:
> Bind the method GUID on the WMI bus and give its platform device the
> bound wdev for method calls. Unregister the platform device before the
> method driver releases the binding. Keep the platform sysfs location
> and the input-only path on machines without the method GUID.
>
> Allocate platform state on probe and unwind previously registered
> input handlers if a later input device fails to register.
>
> Signed-off-by: Fuwei Xie <12313607@mail.sustech.edu.cn>
> ---
> drivers/platform/x86/huawei-wmi.c | 109 ++++++++++++++++++++----------
> 1 file changed, 75 insertions(+), 34 deletions(-)
>
> diff --git a/drivers/platform/x86/huawei-wmi.c b/drivers/platform/x86/huawei-wmi.c
> index 559731d15..690bd0abc 100644
> --- a/drivers/platform/x86/huawei-wmi.c
> +++ b/drivers/platform/x86/huawei-wmi.c
> @@ -60,6 +60,7 @@ struct huawei_wmi_debug {
> };
>
> struct huawei_wmi {
> + struct wmi_device *wdev;
> bool battery_available;
> bool fn_lock_available;
>
> @@ -156,7 +157,7 @@ static int huawei_wmi_call(struct huawei_wmi *huawei,
> acpi_status status;
>
> mutex_lock(&huawei->wmi_lock);
> - status = wmi_evaluate_method(HWMI_METHOD_GUID, 0, 1, in, out);
> + 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");
> @@ -880,25 +881,36 @@ static const struct wmi_device_id huawei_wmi_events_id_table[] = {
>
> static int huawei_wmi_probe(struct platform_device *pdev)
> {
> - const struct wmi_device_id *guid = huawei_wmi_events_id_table;
> + const struct wmi_device_id *guid;
> + struct wmi_device **method = dev_get_platdata(&pdev->dev);
> int err;
>
> + huawei_wmi = devm_kzalloc(&pdev->dev, sizeof(*huawei_wmi), GFP_KERNEL);
> + if (!huawei_wmi)
> + return -ENOMEM;
> +
> + huawei_wmi->wdev = method ? *method : NULL;
> platform_set_drvdata(pdev, huawei_wmi);
> huawei_wmi->dev = &pdev->dev;
>
> - while (*guid->guid_string) {
> - if (wmi_has_guid(guid->guid_string)) {
> - err = huawei_wmi_input_setup(&pdev->dev, guid->guid_string);
> - if (err) {
> - dev_err(&pdev->dev, "Failed to setup input on %s\n", guid->guid_string);
> - return err;
> - }
> - }
> + for (guid = huawei_wmi_events_id_table; *guid->guid_string; guid++) {
> + if (!wmi_has_guid(guid->guid_string))
> + continue;
>
> - guid++;
> + err = huawei_wmi_input_setup(&pdev->dev, guid->guid_string);
> + if (!err)
> + continue;
> +
> + dev_err(&pdev->dev, "Failed to setup input on %s\n", guid->guid_string);
> + while (guid != huawei_wmi_events_id_table) {
> + guid--;
> + if (wmi_has_guid(guid->guid_string))
> + huawei_wmi_input_exit(&pdev->dev, guid->guid_string);
> + }
> + return err;
> }
>
> - if (wmi_has_guid(HWMI_METHOD_GUID)) {
> + if (huawei_wmi->wdev) {
> mutex_init(&huawei_wmi->wmi_lock);
>
> huawei_wmi_leds_setup(&pdev->dev);
> @@ -921,7 +933,7 @@ static void huawei_wmi_remove(struct platform_device *pdev)
> guid++;
> }
>
> - if (wmi_has_guid(HWMI_METHOD_GUID)) {
> + if (huawei_wmi->wdev) {
> huawei_wmi_debugfs_exit(&pdev->dev);
> huawei_wmi_battery_exit(&pdev->dev);
> huawei_wmi_fn_lock_exit(&pdev->dev);
> @@ -936,14 +948,46 @@ static struct platform_driver huawei_wmi_driver = {
> .remove = huawei_wmi_remove,
> };
>
> -static __init int huawei_wmi_init(void)
> +/*
> + * Keep the platform device and its existing userspace paths on both
> + * method-capable and older event-only machines.
> + */
> +static struct platform_device *legacy_pdev;
> +
> +static int huawei_wmi_method_probe(struct wmi_device *wdev, const void *context)
> {
> struct platform_device *pdev;
> - int err;
>
> - huawei_wmi = kzalloc_obj(struct huawei_wmi);
> - if (!huawei_wmi)
> - return -ENOMEM;
> + pdev = platform_device_register_data(NULL, "huawei-wmi", PLATFORM_DEVID_NONE,
> + &wdev, sizeof(wdev));
> + if (IS_ERR(pdev))
> + return PTR_ERR(pdev);
> +
> + dev_set_drvdata(&wdev->dev, pdev);
> + return 0;
> +}
> +
> +static void huawei_wmi_method_remove(struct wmi_device *wdev)
> +{
> + platform_device_unregister(dev_get_drvdata(&wdev->dev));
> +}
> +
> +static const struct wmi_device_id huawei_wmi_method_ids[] = {
> + { HWMI_METHOD_GUID },
> + { }
> +};
> +MODULE_DEVICE_TABLE(wmi, huawei_wmi_method_ids);
> +
> +static struct wmi_driver huawei_wmi_method_driver = {
> + .driver.name = "huawei-wmi-method",
> + .id_table = huawei_wmi_method_ids,
> + .probe = huawei_wmi_method_probe,
> + .remove = huawei_wmi_method_remove,
> +};
> +
> +static __init int huawei_wmi_init(void)
> +{
> + int err;
>
> quirks = &quirk_unknown;
> dmi_check_system(huawei_quirks);
> @@ -954,37 +998,34 @@ static __init int huawei_wmi_init(void)
>
> err = platform_driver_register(&huawei_wmi_driver);
> if (err)
> - goto pdrv_err;
> + return err;
>
> - pdev = platform_device_register_simple("huawei-wmi", PLATFORM_DEVID_NONE, NULL, 0);
> - if (IS_ERR(pdev)) {
> - err = PTR_ERR(pdev);
> - goto pdev_err;
> + if (wmi_has_guid(HWMI_METHOD_GUID)) {
> + err = wmi_driver_register(&huawei_wmi_method_driver);
> + } else {
> + legacy_pdev = platform_device_register_simple("huawei-wmi", PLATFORM_DEVID_NONE,
> + NULL, 0);
> + err = PTR_ERR_OR_ZERO(legacy_pdev);
> }
> + if (err)
> + platform_driver_unregister(&huawei_wmi_driver);
The old rollback path was better than this.
Armin, is this legacy_pdev approach what is supposed to be used?
> - return 0;
> -
> -pdev_err:
> - platform_driver_unregister(&huawei_wmi_driver);
> -pdrv_err:
> - kfree(huawei_wmi);
> return err;
> }
>
> static __exit void huawei_wmi_exit(void)
> {
> - struct platform_device *pdev = to_platform_device(huawei_wmi->dev);
> + if (legacy_pdev)
> + platform_device_unregister(legacy_pdev);
> + else
> + wmi_driver_unregister(&huawei_wmi_method_driver);
>
> - platform_device_unregister(pdev);
> platform_driver_unregister(&huawei_wmi_driver);
> -
> - kfree(huawei_wmi);
> }
>
> module_init(huawei_wmi_init);
> module_exit(huawei_wmi_exit);
>
> -MODULE_ALIAS("wmi:"HWMI_METHOD_GUID);
> MODULE_DEVICE_TABLE(wmi, huawei_wmi_events_id_table);
> MODULE_AUTHOR("Ayman Bagabas <ayman.bagabas@gmail.com>");
> MODULE_DESCRIPTION("Huawei WMI laptop extras driver");
>
--
i.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v4 3/5] platform/x86: huawei-wmi: use the sized WMI method interface
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
0 siblings, 0 replies; 8+ messages in thread
From: Ilpo Järvinen @ 2026-12-01 11:09 UTC (permalink / raw)
To: Fuwei Xie
Cc: Hans de Goede, platform-driver-x86, LKML, Eren Özen, Zheng Shuyang
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.
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-12-01 11:09 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[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
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
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®