Re: [PATCH 4/7] platform/x86: lenovo-ymc: Modernize WMI driver and factor out events
From: Rong Zhang
Date: Mon Nov 16 2026 - 11:57:16 EST
Hi Mark,
On Sun, 2026-11-15 at 17:39 -0500, Mark Pearson wrote:
>
> On Sun, Nov 15, 2026, at 2:27 PM, Rong Zhang wrote:
> > The lenovo-ymc driver is a legacy WMI driver. It binds to the events
> > GUID as a WMI driver but calls the query GUID with the deprecated GUID-
> > based interface.
> >
> > Handle YMC events in lenovo-wmi-event, and coordinate relevant drivers
> > with notifier chains:
> >
> > +---LWMI_EVENT_THERMAL_MODE---> wmi-gamezone
> > |
> > wmi-events ---+
> > |
> > +---LWMI_EVENT_YMC---> ymc
> > ---IDEAPAD_LAPTOP_YMC_EVENT---> ideapad-laptop
> >
> > Modernize the lenovo-ymc driver by adopting lwmi_dev_evaluate_int() from
> > lenovo-wmi-helpers, which has adopted the modern WMI core interfaces and
> > taken care of buffer's lifecycle. This should hopefully improve
> > robustness on various firmware as it aligns with the Windows behavior
> > that ACPI object type doesn't matter but the size does.
> >
> > Note that the WMI core does not verify min_event_size for the legacy
> > notify() callback (it only ensures the event has data). By factoring out
> > YMC events to lenovo-wmi-events with the new notify_new() callback, now
> > the WMI core can finally verify the size of event data:
> >
> > [WmiDataId(1), read, Description("Lenovo Yoga Mode Change Event")]
> > uint32 ModeDataVal;
> >
> > With these changes, the lenovo-ymc driver become a no_singleton
> > asynchronous-probe driver that only binds to the query GUID. It also
> > retrieve event data as a fallback data source when the query method
>
> Minor typo: retrieves?
Will fix it, thanks.
>
> > fails, so that the EC is always triggered on valid events (i.e., passing
> > the min_event_size check). On probe it's always triggered, too, as we
> > want the EC to initialize the state of keyboard and touchpad on boot.
> >
> > The input device's phys string also adopts the query GUID rather than
> > the events one. It shouldn't break anything, as nothing actually matches
> > the previous phys string.
> >
> > The DMI quirk tables are left untouched, as the contained devices should
> > have both GUIDs.
> >
> > Signed-off-by: Rong Zhang <i@xxxxxxxx>
> > ---
> > drivers/platform/x86/lenovo/Kconfig | 2 +
> > drivers/platform/x86/lenovo/wmi-events.c | 7 +++
> > drivers/platform/x86/lenovo/wmi-events.h | 1 +
> > drivers/platform/x86/lenovo/ymc.c | 85 ++++++++++++++++++--------------
> > 4 files changed, 58 insertions(+), 37 deletions(-)
> >
> > diff --git a/drivers/platform/x86/lenovo/Kconfig
> > b/drivers/platform/x86/lenovo/Kconfig
> > index 516594993073..f16b09bab082 100644
> > --- a/drivers/platform/x86/lenovo/Kconfig
> > +++ b/drivers/platform/x86/lenovo/Kconfig
> > @@ -63,6 +63,8 @@ config LENOVO_YMC
> > depends on INPUT
> > depends on IDEAPAD_LAPTOP
> > select INPUT_SPARSEKMAP
> > + select LENOVO_WMI_EVENTS
> > + select LENOVO_WMI_HELPERS
> > help
> > This driver maps the Tablet Mode Control switch to SW_TABLET_MODE
> > input
> > events for Lenovo Yoga notebooks.
> > diff --git a/drivers/platform/x86/lenovo/wmi-events.c
> > b/drivers/platform/x86/lenovo/wmi-events.c
> > index 01ad5bcf0437..a0ddea01213f 100644
> > --- a/drivers/platform/x86/lenovo/wmi-events.c
> > +++ b/drivers/platform/x86/lenovo/wmi-events.c
> > @@ -21,6 +21,7 @@
> > #include "wmi-helpers.h"
> >
> > #define THERMAL_MODE_EVENT_GUID "D320289E-8FEA-41E0-86F9-911D83151B5F"
> > +#define YOGA_MODE_CHANGE_EVENT_GUID "06129D99-6083-4164-81AD-F092F9D773A6"
> >
> > #define LWMI_EVENT_DEVICE(guid, type) \
> > .guid_string = (guid), .context = &(enum lwmi_events_type) \
> > @@ -123,6 +124,7 @@ static void lwmi_events_notify(struct wmi_device
> > *wdev, const struct wmi_buffer
> > struct lwmi_events_priv *priv = dev_get_drvdata(&wdev->dev);
> > void *notif_data = NULL;
> > int sel_prof;
> > + u32 ymc_mode;
> > int ret;
> >
> > switch (priv->type) {
> > @@ -143,6 +145,10 @@ static void lwmi_events_notify(struct wmi_device
> > *wdev, const struct wmi_buffer
> > return;
> > }
> > break;
> > + case LWMI_EVENT_YOGA_MODE_CHANGE:
> > + ymc_mode = le32_to_cpup(buf->data);
> > + notif_data = &ymc_mode;
> > + break;
> > default:
> > return;
> > }
> > @@ -175,6 +181,7 @@ static int lwmi_events_probe(struct wmi_device
> > *wdev, const void *context)
> >
> > static const struct wmi_device_id lwmi_events_id_table[] = {
> > { LWMI_EVENT_DEVICE(THERMAL_MODE_EVENT_GUID, LWMI_EVENT_THERMAL_MODE)
> > },
> > + { LWMI_EVENT_DEVICE(YOGA_MODE_CHANGE_EVENT_GUID,
> > LWMI_EVENT_YOGA_MODE_CHANGE) },
> > {}
> > };
> >
> > diff --git a/drivers/platform/x86/lenovo/wmi-events.h
> > b/drivers/platform/x86/lenovo/wmi-events.h
> > index cd34e886912c..592da08ff2a3 100644
> > --- a/drivers/platform/x86/lenovo/wmi-events.h
> > +++ b/drivers/platform/x86/lenovo/wmi-events.h
> > @@ -10,6 +10,7 @@ struct notifier_block;
> >
> > enum lwmi_events_type {
> > LWMI_EVENT_THERMAL_MODE = 1,
> > + LWMI_EVENT_YOGA_MODE_CHANGE = 2,
> > };
> >
> > int lwmi_events_register_notifier(struct notifier_block *nb);
> > diff --git a/drivers/platform/x86/lenovo/ymc.c
> > b/drivers/platform/x86/lenovo/ymc.c
> > index eba22600161e..cb3ae49dfd9e 100644
> > --- a/drivers/platform/x86/lenovo/ymc.c
> > +++ b/drivers/platform/x86/lenovo/ymc.c
> > @@ -3,22 +3,25 @@
> > * lenovo-ymc.c - Lenovo Yoga Mode Control driver
> > *
> > * Copyright © 2022 Gergo Koteles <soyer@xxxxxx>
> > + * Copyright © 2026 Rong Zhang <i@xxxxxxxx>
> > */
> >
> > #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
> >
> > -#include <linux/acpi.h>
> > #include <linux/bitfield.h>
> > #include <linux/bits.h>
> > #include <linux/cleanup.h>
> > +#include <linux/container_of.h>
> > #include <linux/dmi.h>
> > #include <linux/input.h>
> > #include <linux/input/sparse-keymap.h>
> > #include <linux/mutex.h>
> > +#include <linux/notifier.h>
> > #include <linux/wmi.h>
> > #include "ideapad-laptop.h"
> > +#include "wmi-events.h"
> > +#include "wmi-helpers.h"
> >
> > -#define LENOVO_YMC_EVENT_GUID "06129D99-6083-4164-81AD-F092F9D773A6"
> > #define LENOVO_YMC_QUERY_GUID "09B0EE6E-C3FD-4243-8DA1-7911FF80BB8C"
> >
> > #define LENOVO_YMC_QUERY_INSTANCE 0
> > @@ -61,8 +64,10 @@ static const struct dmi_system_id
> > allowed_chasis_types_dmi_table[] = {
> > };
> >
> > struct lenovo_ymc_private {
> > + struct wmi_device *wdev;
> > struct input_dev *input_dev;
> > struct mutex mutex; /* Protects event handling. */
> > + struct notifier_block nb;
> > };
> >
> > static const struct key_entry lenovo_ymc_keymap[] = {
> > @@ -75,49 +80,44 @@ static const struct key_entry lenovo_ymc_keymap[] = {
> > { KE_END },
> > };
> >
> > -static void lenovo_ymc_notify(struct wmi_device *wdev, union acpi_object *data)
> > +static void lenovo_ymc_refresh(struct lenovo_ymc_private *priv, u32 code)
> > {
> > - struct lenovo_ymc_private *priv = dev_get_drvdata(&wdev->dev);
> > - u32 input_val = 0;
> > - struct acpi_buffer input = { sizeof(input_val), &input_val };
> > - struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
> > - union acpi_object *obj;
> > - acpi_status status;
> > - int code;
> > + struct wmi_device *wdev = priv->wdev;
> > + u32 input = 0, output;
> > + int ret;
> >
> > guard(mutex)(&priv->mutex);
> >
> > - status = wmi_evaluate_method(LENOVO_YMC_QUERY_GUID,
> > - LENOVO_YMC_QUERY_INSTANCE,
> > - LENOVO_YMC_QUERY_METHOD,
> > - &input, &output);
> > -
> > - if (ACPI_FAILURE(status)) {
> > - dev_warn(&wdev->dev,
> > - "Failed to evaluate query method: %s\n",
> > - acpi_format_exception(status));
> > - return;
> > - }
> > -
> > - obj = output.pointer;
> > -
> > - if (obj->type != ACPI_TYPE_INTEGER) {
> > - dev_warn(&wdev->dev,
> > - "WMI event data is not an integer\n");
> > - goto free_obj;
> > - }
> > + ret = lwmi_dev_evaluate_int(wdev, LENOVO_YMC_QUERY_INSTANCE,
> > LENOVO_YMC_QUERY_METHOD,
> > + (unsigned char *)&input, sizeof(input), &output);
> > + if (ret)
> > + dev_warn(&wdev->dev, "Failed to invoke query method: %d\n", ret);
>
> Should it not bomb out here? Otherwise the chances are high it's reporting an incorrect status isn't it?
It's intentional to fall back to the `code' passed by the argument:
- On probe, fall back to YMC_POSTURE_UNKNOWN
- sparse_keymap_report_event() becomes a no-op
- trigger the EC to ensure the keyboard and touchpad are enabled
(PATCH 6), or else the user may be unable to input anything
- On resume (PATCH 5), same as above
- On event, fall back to the event data
- the event carries data as per the MOF (see below)
- the WMI core has verified that the event has 4-byte-or-more data
- if the event data is correct, everything is fine
- if the event data is bogus, it's probably either YMC_POSTURE_UNKNOWN
(0) or an undefined value (>4). In either case, trigger the EC to
ensure the keyboard and touchpad are enabled for the same reason
The MOF of the relevant WMI devices:
[WMI, Dynamic, Provider("WmiProv"), Locale("MS\\0x409"), Description("Lenovo Yoga Mode Change Event"), guid("{06129D99-6083-4164-81AD-F092F9D773A6}")]
class LENOVO_GSENSOR_EVENT : WMIEvent {
[key, read] String InstanceName;
[read] Boolean Active;
[WmiDataId(1), read, Description("Lenovo Yoga Mode Change Event")] uint32 ModeDataVal;
};
[WMI, Dynamic, Provider("WmiProv"), Locale("MS\\0x409"), Description("LENOVO_GSENSOR_DATA class"), guid("{09B0EE6E-C3FD-4243-8DA1-7911FF80BB8C}")]
class LENOVO_GSENSOR_DATA {
[key, read] String InstanceName;
[read] Boolean Active;
[WmiMethodId(1), Implemented, Description("Mode Data")] void GetUsageMode([out, Description("Mode Data")] uint32 Data);
[WmiMethodId(2..7), DON'T CARE, USUALLY UNIMPLEMENTED]
};
If I were the original author of lenovo-ymc, I would not call
GetUsageMode() on events, but would solely rely on the event data
(ModeDataVal).
I believe the driver was implemented in the opposite way (i.e., calling
GetUsageMode() in events and ignore event data) simply because this was
how https://github.com/lukas-w/yoga-usage-mode was implemented. It was
more like a preference of the original author rather than a technical
issue.
I preserved the call to GetUsageMode() simply because I didn't want to be
too radical here. Prioritizing GetUsageMode() results and falling back to
event data on failure seems safe anyway.
Does this make sense to you now? Maybe I should've elaborated more in the
commit message...
OTOH, if you don't like the fallback, I can ignore event data (again) and
pass YMC_POSTURE_UNKNOWN to ideapad-laptop on error (IMO we need to
trigger the EC anyway to enable keyboard and touchpad anyway if we can't
trust event data).
Thanks,
Rong
>
> > + else
> > + code = output;
> >
> > /* strip upper bits (e.g. 0x50000) on newer devices */
> > - code = FIELD_GET(LENOVO_YMC_STATE_MASK, obj->integer.value);
> > + code = FIELD_GET(LENOVO_YMC_STATE_MASK, code);
> >
> > if (!sparse_keymap_report_event(priv->input_dev, code, 1, true))
> > dev_warn(&wdev->dev, "Unknown key %d pressed\n", code);
> >
> > -free_obj:
> > - kfree(obj);
> > ideapad_laptop_call_notifier(IDEAPAD_LAPTOP_YMC_EVENT, &code);
> > }
> >
> > +static int lenovo_ymc_notify(struct notifier_block *nb, unsigned long
> > action, void *data)
> > +{
> > + struct lenovo_ymc_private *priv = container_of(nb, struct
> > lenovo_ymc_private, nb);
> > + const u32 *p = data;
> > +
> > + switch (action) {
> > + case LWMI_EVENT_YOGA_MODE_CHANGE:
> > + lenovo_ymc_refresh(priv, *p);
> > + return NOTIFY_OK;
> > + default:
> > + return NOTIFY_DONE;
> > + }
> > +}
> > +
> > static int lenovo_ymc_probe(struct wmi_device *wdev, const void *ctx)
> > {
> > struct lenovo_ymc_private *priv;
> > @@ -147,7 +147,7 @@ static int lenovo_ymc_probe(struct wmi_device
> > *wdev, const void *ctx)
> > return err;
> >
> > input_dev->name = "Lenovo Yoga Tablet Mode Control switch";
> > - input_dev->phys = LENOVO_YMC_EVENT_GUID "/input0";
> > + input_dev->phys = LENOVO_YMC_QUERY_GUID "/input0";
> > input_dev->id.bustype = BUS_HOST;
> > input_dev->dev.parent = &wdev->dev;
> > err = sparse_keymap_setup(input_dev, lenovo_ymc_keymap, NULL);
> > @@ -164,16 +164,24 @@ static int lenovo_ymc_probe(struct wmi_device
> > *wdev, const void *ctx)
> > return err;
> > }
> >
> > + priv->wdev = wdev;
> > priv->input_dev = input_dev;
> > + priv->nb.notifier_call = lenovo_ymc_notify;
> > dev_set_drvdata(&wdev->dev, priv);
> >
> > + err = devm_lwmi_events_register_notifier(&wdev->dev, &priv->nb);
> > + if (err) {
> > + dev_err(&wdev->dev, "Could not register YMC events notifier: %d\n", err);
> > + return err;
> > + }
> > +
> > /* Report the state for the first time on probe */
> > - lenovo_ymc_notify(wdev, NULL);
> > + lenovo_ymc_refresh(priv, YMC_POSTURE_UNKNOWN);
> > return 0;
> > }
> >
> > static const struct wmi_device_id lenovo_ymc_wmi_id_table[] = {
> > - { .guid_string = LENOVO_YMC_EVENT_GUID },
> > + { .guid_string = LENOVO_YMC_QUERY_GUID },
> > { }
> > };
> > MODULE_DEVICE_TABLE(wmi, lenovo_ymc_wmi_id_table);
> > @@ -181,16 +189,19 @@ MODULE_DEVICE_TABLE(wmi, lenovo_ymc_wmi_id_table);
> > static struct wmi_driver lenovo_ymc_driver = {
> > .driver = {
> > .name = "lenovo-ymc",
> > + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
> > },
> > .id_table = lenovo_ymc_wmi_id_table,
> > - .min_event_size = sizeof(u32),
> > .probe = lenovo_ymc_probe,
> > - .notify = lenovo_ymc_notify,
> > + .no_singleton = true,
> > };
> >
> > module_wmi_driver(lenovo_ymc_driver);
> >
> > MODULE_AUTHOR("Gergo Koteles <soyer@xxxxxx>");
> > +MODULE_AUTHOR("Rong Zhang <i@xxxxxxxx>");
> > MODULE_DESCRIPTION("Lenovo Yoga Mode Control driver");
> > MODULE_LICENSE("GPL");
> > MODULE_IMPORT_NS("IDEAPAD_LAPTOP");
> > +MODULE_IMPORT_NS("LENOVO_WMI_EVENTS");
> > +MODULE_IMPORT_NS("LENOVO_WMI_HELPERS");
> >
> > --
> > 2.55.0