[RFC PATCH v3 2/5] platform/x86: huawei-wmi: bind the Huawei method device

From: Fuwei Xie

Date: Tue Nov 24 2026 - 12:28:22 EST


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@xxxxxxxxxxxxxxxxxxx>
---
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@xxxxxxxxx>");
MODULE_DESCRIPTION("Huawei WMI laptop extras driver");
--
2.55.0