[PATCH v5 2/3] platform/x86: huawei-wmi: use the bound HWMI device for laptop controls

From: Fuwei Xie

Date: Tue Dec 01 2026 - 09:42:37 EST


Bind the HWMI method GUID and tie the controller and LED lifetime to
that binding. Keep the platform device as the parent of the existing
sysfs controls, including on event-only machines. Pass the controller
to the battery, Fn Lock and LED paths and remove the controls on unbind.

Use wmidev_invoke_method() for commands. The WMI core marshals both
firmware reply formats into four reserved bytes, a status byte and
command data. Release each reply in the iteration that received it.
Hold the mutex across the retry when the firmware returns nonzero
status. The debugfs call displays the marshalled bytes as one list.

Return the number of data bytes copied after the status byte. This
leaves up to 255 bytes of data from a 256-byte result. Battery reads
require the two threshold bytes and skip trailing padding. Fn Lock
reads require one byte and skip leading padding. Wire values 1 and 2
report off and on. A nonempty all-zero reply reports -1. Keep the
historical value minus one for other encodings.

Signed-off-by: Fuwei Xie <12313607@xxxxxxxxxxxxxxxxxxx>
---
Eren tested the battery paths on v4 with his charge-mode changes applied.
His Tested-by is deferred for this patch pending a test after the rebase.

drivers/platform/x86/huawei-wmi.c | 474 ++++++++++++++----------------
1 file changed, 213 insertions(+), 261 deletions(-)

diff --git a/drivers/platform/x86/huawei-wmi.c b/drivers/platform/x86/huawei-wmi.c
index 94e3f34b3..adba3b5c4 100644
--- a/drivers/platform/x86/huawei-wmi.c
+++ b/drivers/platform/x86/huawei-wmi.c
@@ -53,7 +53,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;
@@ -61,6 +62,7 @@ struct huawei_wmi_debug {
};

struct huawei_wmi {
+ struct wmi_device *wdev;
bool battery_available;
bool fn_lock_available;

@@ -71,7 +73,8 @@ struct huawei_wmi {
struct mutex wmi_lock;
};

-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);

@@ -117,8 +120,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,
@@ -153,119 +154,55 @@ 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 = 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;
- }
+/* Both HWMI reply formats start with four reserved bytes and a status. */
+struct hwmi_reply {
+ u8 reserved[4];
+ u8 status;
+ u8 data[];
+};

- return 0;
-}
+/* The HWMI result buffer includes its one-byte status. */
+#define HWMI_RESULT_SIZE 0x100
+#define HWMI_MAX_DATA_SIZE (HWMI_RESULT_SIZE - sizeof_field(struct hwmi_reply, status))

-/* 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_cmd(u64 arg, u8 *buf, size_t buflen)
+/* Return the number of command data bytes copied, or a negative errno. */
+static int huawei_wmi_cmd(struct huawei_wmi *huawei, 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;
-
- in.length = sizeof(arg);
- in.pointer = &arg;
-
- /* 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);
- if (err)
- goto fail_cmd;
-
- obj = out.pointer;
- if (!obj) {
- err = -EIO;
- goto fail_cmd;
- }
+ struct wmi_buffer in = { .length = sizeof(arg), .data = &arg };
+ int i;

- 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;
- }
+ lockdep_assert_held(&huawei->wmi_lock);
+ for (i = 0; i < 2; i++) {
+ struct wmi_buffer out;
+ size_t len;
+ int err;

- 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;
- }
- len = obj->buffer.length;
+ err = wmidev_invoke_method(huawei->wdev, 0, 1, &in, &out,
+ sizeof(struct hwmi_reply));
+ if (err)
+ return err;

- break;
- /* Shouldn't get here! */
- default:
- dev_err(huawei->dev, "Unexpected obj type, got: %d\n", obj->type);
- err = -EIO;
- goto fail_cmd;
- }
+ struct hwmi_reply *reply __free(kfree) = out.data;

- if (!*obj->buffer.pointer)
- break;
- }
+ /* Some machines require a second call after a nonzero status. */
+ if (reply->status)
+ continue;

- err = (*obj->buffer.pointer) ? -ENODEV : 0;
+ len = min(buflen, out.length - sizeof(*reply));
+ if (len)
+ memcpy(buf, reply->data, len);

- if (buf) {
- len = min(buflen, len);
- memcpy(buf, obj->buffer.pointer, len);
+ return len;
}

-fail_cmd:
- kfree(out.pointer);
- return err;
+ return -ENODEV;
}

-/* LEDs */
-
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;
@@ -307,10 +244,16 @@ 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_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);
@@ -319,36 +262,41 @@ 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)
+#define HWMI_BATTERY_THRESHOLD_PAIR_SIZE 2
+
+static int huawei_wmi_battery_get(struct huawei_wmi *huawei, int *start, int *end)
{
- u8 ret[0x100];
- int err, i;
+ u8 data[HWMI_MAX_DATA_SIZE];
+ int len;

- err = huawei_wmi_cmd(BATTERY_THRESH_GET, ret, sizeof(ret));
- if (err)
- return err;
+ scoped_guard(mutex, &huawei->wmi_lock)
+ len = huawei_wmi_cmd(huawei, BATTERY_THRESH_GET, data, sizeof(data));
+ if (len < 0)
+ return len;
+ if (len < HWMI_BATTERY_THRESHOLD_PAIR_SIZE)
+ 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 > HWMI_BATTERY_THRESHOLD_PAIR_SIZE && !data[len - 1])
+ len--;
+ if (start)
+ *start = data[len - HWMI_BATTERY_THRESHOLD_PAIR_SIZE];
+ if (end)
+ *end = data[len - 1];

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;
@@ -366,25 +314,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);
}

- err = huawei_wmi_cmd(arg.cmd, NULL, 0);
-
- return err;
+ 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;

@@ -395,9 +347,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;

@@ -408,9 +361,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;

@@ -421,16 +375,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;

@@ -441,16 +396,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;

@@ -461,12 +417,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;

@@ -511,7 +468,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;
}
@@ -533,8 +490,9 @@ static void huawei_wmi_battery_exit(struct device *dev)
/* Fn lock */

/* GFRS byte[1] / SFRS byte[2] (FRSR) fn-lock state values */
-#define FN_LOCK_ACPI_OFF 1
-#define FN_LOCK_ACPI_ON 2
+#define FN_LOCK_UNDEFINED 0
+#define FN_LOCK_OFF 1
+#define FN_LOCK_ON 2
#define FN_LOCK_ACPI_STAT_OK 0

/*
@@ -544,13 +502,13 @@ static void huawei_wmi_battery_exit(struct device *dev)
*
* GFRS response buffer layout:
* byte[0] = STAT (FN_LOCK_ACPI_STAT_OK = success)
- * byte[1] = FN_LOCK_ACPI_OFF (fn-lock off) or FN_LOCK_ACPI_ON (fn-lock on)
+ * byte[1] = FN_LOCK_OFF (fn-lock off) or FN_LOCK_ON (fn-lock on)
*
* SFRS argument layout (CreateByteField(Arg0, 0x02, FRSR)):
* Value is read from byte[2] of the integer argument, so it must be
* passed as (value << 16):
- * (FN_LOCK_ACPI_OFF << 16) = fn-lock off (writes 0x55 to EC 0x6C)
- * (FN_LOCK_ACPI_ON << 16) = fn-lock on (writes 0x5A to EC 0x6C)
+ * (FN_LOCK_OFF << 16) = fn-lock off (writes 0x55 to EC 0x6C)
+ * (FN_LOCK_ON << 16) = fn-lock on (writes 0x5A to EC 0x6C)
*/

static int huawei_acpi_fn_lock_get(int *on)
@@ -576,11 +534,11 @@ static int huawei_acpi_fn_lock_get(int *on)
return -EIO;

switch (obj->buffer.pointer[1]) {
- case FN_LOCK_ACPI_OFF:
+ case FN_LOCK_OFF:
if (on)
*on = 0;
break;
- case FN_LOCK_ACPI_ON:
+ case FN_LOCK_ON:
if (on)
*on = 1;
break;
@@ -600,11 +558,11 @@ static int huawei_acpi_fn_lock_set(int on)

/*
* SFRS reads byte[2] of its argument via CreateByteField(Arg0, 0x02).
- * on=0 → FRSR=FN_LOCK_ACPI_OFF → EC gets 0x55 (fn-lock off)
- * on=1 → FRSR=FN_LOCK_ACPI_ON → EC gets 0x5A (fn-lock on)
+ * on=0 → FRSR=FN_LOCK_OFF → EC gets 0x55 (fn-lock off)
+ * on=1 → FRSR=FN_LOCK_ON → EC gets 0x5A (fn-lock on)
*/
acpi_arg.type = ACPI_TYPE_INTEGER;
- acpi_arg.integer.value = (on ? FN_LOCK_ACPI_ON : FN_LOCK_ACPI_OFF) << 16;
+ acpi_arg.integer.value = (on ? FN_LOCK_ON : FN_LOCK_OFF) << 16;

status = acpi_evaluate_object(NULL, "\\SFRS", &arg_list, &output);
if (ACPI_FAILURE(status))
@@ -620,31 +578,45 @@ 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 ret[0x100] = { 0 };
- int err, i;
+ u8 data[HWMI_MAX_DATA_SIZE];
+ 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;
+ scoped_guard(mutex, &huawei->wmi_lock)
+ len = huawei_wmi_cmd(huawei, 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);
+
+ switch (state ? *state : FN_LOCK_UNDEFINED) {
+ case FN_LOCK_UNDEFINED:
+ *on = -1;
+ break;
+ case FN_LOCK_OFF:
+ *on = 0;
+ break;
+ case FN_LOCK_ON:
+ *on = 1;
+ break;
+ default:
+ /* Preserve the legacy sysfs value for unknown encodings. */
+ *on = *state - 1;
+ break;
+ }
+ }

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;

@@ -654,9 +626,10 @@ static int huawei_wmi_fn_lock_set(int on)

/* WMI fallback */
arg.cmd = FN_LOCK_SET;
- arg.args[2] = on + 1; // 0 undefined, 1 off, 2 on.
+ arg.args[2] = on ? FN_LOCK_ON : FN_LOCK_OFF;

- return huawei_wmi_cmd(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,
@@ -665,7 +638,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;

@@ -682,7 +655,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;

@@ -696,7 +669,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;
}
@@ -714,69 +687,27 @@ 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;
- }
-}
-
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;
+ u64 arg = READ_ONCE(huawei->debug.arg);
+ struct wmi_buffer in = { .length = sizeof(arg), .data = &arg };
+ struct wmi_buffer out;
int err;

- in.length = sizeof(u64);
- in.pointer = &huawei->debug.arg;
-
- err = huawei_wmi_call(huawei, &in, &out);
+ scoped_guard(mutex, &huawei->wmi_lock)
+ err = wmidev_invoke_method(huawei->wdev, 0, 1, &in, &out, 0);
if (err)
return err;

- obj = out.pointer;
- if (!obj) {
- err = -EIO;
- goto fail_debugfs_call;
- }
+ u8 *bytes __free(kfree) = out.data;

- huawei_wmi_debugfs_call_dump(m, huawei, obj);
+ seq_putc(m, '{');
+ for (size_t i = 0; i < out.length; i++)
+ seq_printf(m, "%s0x%02x", i ? "," : "", bytes[i]);
+ seq_putc(m, '}');

-fail_debugfs_call:
- kfree(out.pointer);
- return err;
+ return 0;
}

DEFINE_SHOW_ATTRIBUTE(huawei_wmi_debugfs_call);
@@ -797,7 +728,7 @@ 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 */
@@ -907,7 +838,7 @@ static int huawei_wmi_input_probe(struct wmi_device *wdev, const void *context)
idev->name = "Huawei WMI hotkeys";
idev->phys = "wmi/input0";
idev->id.bustype = BUS_HOST;
- idev->dev.parent = huawei_wmi->dev;
+ idev->dev.parent = &huawei_pdev->dev;

err = sparse_keymap_setup(idev, huawei_wmi_keymap, NULL);
if (!err)
@@ -943,12 +874,58 @@ static struct wmi_driver huawei_wmi_event_driver = {
.no_singleton = true,
};

+/* 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)
+{
+ struct device *dev = &huawei_pdev->dev;
+ struct huawei_wmi *huawei;
+
+ huawei = devm_kzalloc(&wdev->dev, sizeof(*huawei), GFP_KERNEL);
+ if (!huawei)
+ return -ENOMEM;
+
+ huawei->wdev = wdev;
+ huawei->dev = dev;
+ mutex_init(&huawei->wmi_lock);
+
+ 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);
+ return 0;
+}
+
+static void huawei_wmi_method_remove(struct wmi_device *wdev)
+{
+ struct huawei_wmi *huawei = dev_get_drvdata(&wdev->dev);
+
+ 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);
+}
+
+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 int huawei_wmi_probe(struct platform_device *pdev)
{
int err;

- platform_set_drvdata(pdev, huawei_wmi);
- huawei_wmi->dev = &pdev->dev;
+ if (pdev != huawei_pdev)
+ return -ENODEV;

err = wmi_driver_register(&huawei_wmi_keyblock_driver);
if (err)
@@ -958,17 +935,14 @@ static int huawei_wmi_probe(struct platform_device *pdev)
if (err)
goto err_unregister_keyblock;

- 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);
- }
+ err = wmi_driver_register(&huawei_wmi_method_driver);
+ if (err)
+ goto err_unregister_events;

return 0;

+err_unregister_events:
+ wmi_driver_unregister(&huawei_wmi_event_driver);
err_unregister_keyblock:
wmi_driver_unregister(&huawei_wmi_keyblock_driver);
return err;
@@ -977,72 +951,50 @@ err_unregister_keyblock:
static void huawei_wmi_remove(struct platform_device *pdev)
{
wmi_driver_unregister(&huawei_wmi_event_driver);
+ wmi_driver_unregister(&huawei_wmi_method_driver);
wmi_driver_unregister(&huawei_wmi_keyblock_driver);
-
- if (wmi_has_guid(HWMI_METHOD_GUID)) {
- huawei_wmi_debugfs_exit(&pdev->dev);
- huawei_wmi_battery_exit(&pdev->dev);
- huawei_wmi_fn_lock_exit(&pdev->dev);
- }
}

static struct platform_driver huawei_wmi_driver = {
- .driver = {
- .name = "huawei-wmi",
- },
+ .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)
{
- struct platform_device *pdev;
int err;

- huawei_wmi = kzalloc_obj(struct huawei_wmi);
- if (!huawei_wmi)
- return -ENOMEM;
-
- 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;

+ huawei_pdev = platform_device_register_simple("huawei-wmi", PLATFORM_DEVID_NONE, NULL, 0);
+ if (IS_ERR(huawei_pdev))
+ return PTR_ERR(huawei_pdev);
+
err = platform_driver_register(&huawei_wmi_driver);
if (err)
- goto pdrv_err;
-
- pdev = platform_device_register_simple("huawei-wmi", PLATFORM_DEVID_NONE, NULL, 0);
- if (IS_ERR(pdev)) {
- err = PTR_ERR(pdev);
- goto pdev_err;
- }
+ goto err_unregister_pdev;

return 0;

-pdev_err:
- platform_driver_unregister(&huawei_wmi_driver);
-pdrv_err:
- kfree(huawei_wmi);
+err_unregister_pdev:
+ platform_device_unregister(huawei_pdev);
return err;
}

-static __exit void huawei_wmi_exit(void)
+static void __exit huawei_wmi_exit(void)
{
- struct platform_device *pdev = to_platform_device(huawei_wmi->dev);
-
- platform_device_unregister(pdev);
platform_driver_unregister(&huawei_wmi_driver);
-
- kfree(huawei_wmi);
+ platform_device_unregister(huawei_pdev);
}

module_init(huawei_wmi_init);
module_exit(huawei_wmi_exit);

-MODULE_ALIAS("wmi:"HWMI_METHOD_GUID);
MODULE_AUTHOR("Ayman Bagabas <ayman.bagabas@xxxxxxxxx>");
MODULE_DESCRIPTION("Huawei WMI laptop extras driver");
MODULE_LICENSE("GPL v2");
--
2.55.0