mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Mark Pearson" <mpearson-lenovo@squebb.ca>
To: "Rong Zhang" <i@rong.moe>,
	"Derek J . Clark" <derekjohn.clark@gmail.com>,
	"Hans de Goede" <hansg@kernel.org>,
	"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
	"Gergo Koteles" <soyer@irl.hu>, "Ike Panhc" <ikepanhc@gmail.com>,
	"Andrew Kallmeyer" <kallmeyeras@gmail.com>
Cc: "platform-driver-x86@vger.kernel.org"
	<platform-driver-x86@vger.kernel.org>,
	linux-kernel@vger.kernel.org,
	"Mahir Faisal" <mahirfaisalantiq33@gmail.com>
Subject: Re: [PATCH 4/7] platform/x86: lenovo-ymc: Modernize WMI driver and factor out events
Date: Tue, 17 Nov 2026 17:33:22 -0500	[thread overview]
Message-ID: <e6d2efbc-7ea0-4115-bfb7-c2731dabb62d@app.fastmail.com> (raw)
In-Reply-To: <55377800b9338b153d776ab8728a557f682a234a.camel@rong.moe>



On Mon, Nov 16, 2026, at 11:51 AM, Rong Zhang wrote:
> 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@rong.moe>
>> > ---
>> >  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@irl.hu>
>> > + * Copyright © 2026 Rong Zhang <i@rong.moe>
>> >   */
>> > 
>> >  #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 for the explainer - all good.

With that:
Reviewed-by: Mark Pearson <mpearson-lenovo@squebb.ca>

Mark


  reply	other threads:[~2026-11-17 22:33 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-15 19:27 [PATCH 0/7] platform/x86: ideapad-laptop: Trigger the EC on YMC events following Windows behavior Rong Zhang
2026-11-15 19:27 ` [PATCH 1/7] platform/x86: lenovo-ymc: Use enumeration to represent device posture Rong Zhang
2026-11-15 19:27 ` [PATCH 2/7] platform/x86: lenovo-ymc: Serialize WMI event handling Rong Zhang
2026-11-15 19:27 ` [PATCH 3/7] platform/x86: lenovo-wmi-events: Modernize " Rong Zhang
2026-11-15 19:27 ` [PATCH 4/7] platform/x86: lenovo-ymc: Modernize WMI driver and factor out events Rong Zhang
2026-11-15 22:39   ` Mark Pearson
2026-11-16 16:51     ` Rong Zhang
2026-11-17 22:33       ` Mark Pearson [this message]
2026-11-15 19:27 ` [PATCH 5/7] platform/x86: lenovo-ymc: Refresh posture on resume Rong Zhang
2026-11-15 19:27 ` [PATCH 6/7] platform/x86: ideapad-laptop: Trigger the EC on YMC events following Windows behavior Rong Zhang
2026-11-15 19:27 ` [PATCH 7/7] platform/x86: ideapad-laptop: Enable ymc_ec_trigger for Lenovo Yoga 7 14IRL8 Rong Zhang
2026-11-15 22:38 ` [PATCH 0/7] platform/x86: ideapad-laptop: Trigger the EC on YMC events following Windows behavior Mark Pearson
2026-11-16 16:49   ` Rong Zhang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e6d2efbc-7ea0-4115-bfb7-c2731dabb62d@app.fastmail.com \
    --to=mpearson-lenovo@squebb.ca \
    --cc=derekjohn.clark@gmail.com \
    --cc=hansg@kernel.org \
    --cc=i@rong.moe \
    --cc=ikepanhc@gmail.com \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=kallmeyeras@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mahirfaisalantiq33@gmail.com \
    --cc=platform-driver-x86@vger.kernel.org \
    --cc=soyer@irl.hu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®