mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/7] platform/x86: ideapad-laptop: Trigger the EC on YMC events following Windows behavior
@ 2026-11-15 19:27 Rong Zhang
  2026-11-15 19:27 ` [PATCH 1/7] platform/x86: lenovo-ymc: Use enumeration to represent device posture Rong Zhang
                   ` (7 more replies)
  0 siblings, 8 replies; 10+ messages in thread
From: Rong Zhang @ 2026-11-15 19:27 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Hans de Goede, Ilpo Järvinen,
	Gergo Koteles, Ike Panhc, Andrew Kallmeyer
  Cc: platform-driver-x86, linux-kernel, Mahir Faisal, Rong Zhang, stable

Currently, the ideapad-laptop driver triggers the EC with YMC command
0x01, which seems to be randomly chosen with trail-and-error and turns
out to be not a command for YMC events. While the devices in the DMI
quirk table recognize it, some other devices do not, making the
trigger_ec functionality a no-op.

Lenovo's YMC driver for Windows (YMC.exe) emits YMC command 0x08/0x09 on
fold/unfold YMC events, in which "unfold" means laptop mode and "fold"
means other modes.

Adopt the said sequence to make it work on all relevant devices. For the
sake of caution, only emit YMC command 0x08 only for the known fold
postures. For all other postures (including unknown ones), emit 0x09 to
make sure the touchpad and keyboard are still functional.

Beforehand, also fix pre-existing issues, modernize relevant WMI
drivers, and add a resume callback refreshing the posture and poke
ideapad-laptop to trigger the EC. See each patch for details.

With these changes, add a DMI quirk table entry the Lenovo Yoga 7
14IRL8 as its EC must be triggered by software on YMC events properly
(i.e., with command 0x08/0x09), or else the enable state of touchpad,
keyboard, and keyboard backlight become out of sync.

Reported-by: Mahir Faisal <mahirfaisalantiq33@gmail.com>
Closes: https://msgid.link/CAGAHqH6o0TUa1HXMrNV6e+HNYwU3xxHkOM665756n8Cp3AzhVQ@mail.gmail.com
Signed-off-by: Rong Zhang <i@rong.moe>
---
Rong Zhang (7):
      platform/x86: lenovo-ymc: Use enumeration to represent device posture
      platform/x86: lenovo-ymc: Serialize WMI event handling
      platform/x86: lenovo-wmi-events: Modernize WMI event handling
      platform/x86: lenovo-ymc: Modernize WMI driver and factor out events
      platform/x86: lenovo-ymc: Refresh posture on resume
      platform/x86: ideapad-laptop: Trigger the EC on YMC events following Windows behavior
      platform/x86: ideapad-laptop: Enable ymc_ec_trigger for Lenovo Yoga 7 14IRL8

 drivers/platform/x86/lenovo/Kconfig          |   2 +
 drivers/platform/x86/lenovo/ideapad-laptop.c |  42 +++++++++-
 drivers/platform/x86/lenovo/ideapad-laptop.h |   9 ++
 drivers/platform/x86/lenovo/wmi-events.c     |  38 +++++----
 drivers/platform/x86/lenovo/wmi-events.h     |   1 +
 drivers/platform/x86/lenovo/ymc.c            | 118 +++++++++++++++++----------
 6 files changed, 147 insertions(+), 63 deletions(-)
---
base-commit: 7e1392a42ea288d70a266f6553fe06f57f8647ea
change-id: 46a18a06-ymc-82yl-d59b4f8f891e

Thanks,
Rong


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 1/7] platform/x86: lenovo-ymc: Use enumeration to represent device posture
  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 ` Rong Zhang
  2026-11-15 19:27 ` [PATCH 2/7] platform/x86: lenovo-ymc: Serialize WMI event handling Rong Zhang
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Rong Zhang @ 2026-11-15 19:27 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Hans de Goede, Ilpo Järvinen,
	Gergo Koteles, Ike Panhc, Andrew Kallmeyer
  Cc: platform-driver-x86, linux-kernel, Mahir Faisal, Rong Zhang, stable

There are five known Yoga modes: unknown, laptop, tablet, drawing board,
and tent, but lacks symbolic representations for reuse.

Introduce an enumeration called ymc_posture to collect and represent
them. The lenovo_ymc_keymap is also refactored to use the enumeration.

Cc: stable@vger.kernel.org
Signed-off-by: Rong Zhang <i@rong.moe>
---
Cc stable as the later fixes depend on this.
---
 drivers/platform/x86/lenovo/ideapad-laptop.h |  9 +++++++++
 drivers/platform/x86/lenovo/ymc.c            | 14 +++++---------
 2 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/drivers/platform/x86/lenovo/ideapad-laptop.h b/drivers/platform/x86/lenovo/ideapad-laptop.h
index 1e52f2aa0aac..e29f741ffbf3 100644
--- a/drivers/platform/x86/lenovo/ideapad-laptop.h
+++ b/drivers/platform/x86/lenovo/ideapad-laptop.h
@@ -15,6 +15,15 @@ enum ideapad_laptop_notifier_actions {
 	IDEAPAD_LAPTOP_YMC_EVENT,
 };
 
+enum ymc_posture {
+	/* Some devices cannot recognize their postures on boot. */
+	YMC_POSTURE_UNKNOWN		= 0,
+	YMC_POSTURE_LAPTOP		= 1,
+	YMC_POSTURE_TABLET		= 2,
+	YMC_POSTURE_DRAWING_BOARD	= 3,
+	YMC_POSTURE_TENT		= 4,
+};
+
 int ideapad_laptop_register_notifier(struct notifier_block *nb);
 int ideapad_laptop_unregister_notifier(struct notifier_block *nb);
 void ideapad_laptop_call_notifier(unsigned long action, void *data);
diff --git a/drivers/platform/x86/lenovo/ymc.c b/drivers/platform/x86/lenovo/ymc.c
index 91de02d989a0..70f3ae20d3e3 100644
--- a/drivers/platform/x86/lenovo/ymc.c
+++ b/drivers/platform/x86/lenovo/ymc.c
@@ -64,15 +64,11 @@ struct lenovo_ymc_private {
 
 static const struct key_entry lenovo_ymc_keymap[] = {
 	/* Ignore the uninitialized state */
-	{ KE_IGNORE, 0x00 },
-	/* Laptop */
-	{ KE_SW, 0x01, { .sw = { SW_TABLET_MODE, 0 } } },
-	/* Tablet */
-	{ KE_SW, 0x02, { .sw = { SW_TABLET_MODE, 1 } } },
-	/* Drawing Board */
-	{ KE_SW, 0x03, { .sw = { SW_TABLET_MODE, 1 } } },
-	/* Tent */
-	{ KE_SW, 0x04, { .sw = { SW_TABLET_MODE, 1 } } },
+	{ KE_IGNORE,	YMC_POSTURE_UNKNOWN },
+	{ KE_SW,	YMC_POSTURE_LAPTOP,		{ .sw = { SW_TABLET_MODE, 0 } } },
+	{ KE_SW,	YMC_POSTURE_TABLET,		{ .sw = { SW_TABLET_MODE, 1 } } },
+	{ KE_SW,	YMC_POSTURE_DRAWING_BOARD,	{ .sw = { SW_TABLET_MODE, 1 } } },
+	{ KE_SW,	YMC_POSTURE_TENT,		{ .sw = { SW_TABLET_MODE, 1 } } },
 	{ KE_END },
 };
 

-- 
2.55.0


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 2/7] platform/x86: lenovo-ymc: Serialize WMI event handling
  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 ` Rong Zhang
  2026-11-15 19:27 ` [PATCH 3/7] platform/x86: lenovo-wmi-events: Modernize " Rong Zhang
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Rong Zhang @ 2026-11-15 19:27 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Hans de Goede, Ilpo Järvinen,
	Gergo Koteles, Ike Panhc, Andrew Kallmeyer
  Cc: platform-driver-x86, linux-kernel, Mahir Faisal, Rong Zhang, stable

WMI events may arrive in parallel, causing race conditions, e.g.,

  CPU1				CPU2
  query: posture A
  				query: posture B
  				input report B
  input report A
  				trigger EC with B
  trigger EC with A

Fix it by serializing lenovo_ymc_notify() with a mutex.

Fixes: e82882cdd241 ("platform/x86: Add driver for Yoga Tablet Mode switch")
Cc: stable@vger.kernel.org
Signed-off-by: Rong Zhang <i@rong.moe>
---
 drivers/platform/x86/lenovo/ymc.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/drivers/platform/x86/lenovo/ymc.c b/drivers/platform/x86/lenovo/ymc.c
index 70f3ae20d3e3..eba22600161e 100644
--- a/drivers/platform/x86/lenovo/ymc.c
+++ b/drivers/platform/x86/lenovo/ymc.c
@@ -10,9 +10,11 @@
 #include <linux/acpi.h>
 #include <linux/bitfield.h>
 #include <linux/bits.h>
+#include <linux/cleanup.h>
 #include <linux/dmi.h>
 #include <linux/input.h>
 #include <linux/input/sparse-keymap.h>
+#include <linux/mutex.h>
 #include <linux/wmi.h>
 #include "ideapad-laptop.h"
 
@@ -60,6 +62,7 @@ static const struct dmi_system_id allowed_chasis_types_dmi_table[] = {
 
 struct lenovo_ymc_private {
 	struct input_dev *input_dev;
+	struct mutex mutex; /* Protects event handling. */
 };
 
 static const struct key_entry lenovo_ymc_keymap[] = {
@@ -82,6 +85,8 @@ static void lenovo_ymc_notify(struct wmi_device *wdev, union acpi_object *data)
 	acpi_status status;
 	int code;
 
+	guard(mutex)(&priv->mutex);
+
 	status = wmi_evaluate_method(LENOVO_YMC_QUERY_GUID,
 				LENOVO_YMC_QUERY_INSTANCE,
 				LENOVO_YMC_QUERY_METHOD,
@@ -137,6 +142,10 @@ static int lenovo_ymc_probe(struct wmi_device *wdev, const void *ctx)
 	if (!input_dev)
 		return -ENOMEM;
 
+	err = devm_mutex_init(&wdev->dev, &priv->mutex);
+	if (err)
+		return err;
+
 	input_dev->name = "Lenovo Yoga Tablet Mode Control switch";
 	input_dev->phys = LENOVO_YMC_EVENT_GUID "/input0";
 	input_dev->id.bustype = BUS_HOST;

-- 
2.55.0


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 3/7] platform/x86: lenovo-wmi-events: Modernize WMI event handling
  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 ` Rong Zhang
  2026-11-15 19:27 ` [PATCH 4/7] platform/x86: lenovo-ymc: Modernize WMI driver and factor out events Rong Zhang
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Rong Zhang @ 2026-11-15 19:27 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Hans de Goede, Ilpo Järvinen,
	Gergo Koteles, Ike Panhc, Andrew Kallmeyer
  Cc: platform-driver-x86, linux-kernel, Mahir Faisal, Rong Zhang

The WMI core calls the new notify_new() callback with unmarshaled event
data and size check, aligning with the Windows behavior that the ACPI
object type doesn't matter but the size does.

Adopt it to modernize the driver and hopefully improve robustness on
various firmware, like previously did for lenovo-wmi-{capdata,helpers}.

The blocking_notifier_call_chain() code path is also moved, so that it
will be easier when adding new GUIDs to the driver.

Signed-off-by: Rong Zhang <i@rong.moe>
---
 drivers/platform/x86/lenovo/wmi-events.c | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)

diff --git a/drivers/platform/x86/lenovo/wmi-events.c b/drivers/platform/x86/lenovo/wmi-events.c
index 3a82192dc9e5..01ad5bcf0437 100644
--- a/drivers/platform/x86/lenovo/wmi-events.c
+++ b/drivers/platform/x86/lenovo/wmi-events.c
@@ -9,13 +9,14 @@
  * Copyright (C) 2025 Derek J. Clark <derekjohn.clark@gmail.com>
  */
 
-#include <linux/acpi.h>
 #include <linux/export.h>
 #include <linux/module.h>
 #include <linux/notifier.h>
 #include <linux/types.h>
 #include <linux/wmi.h>
 
+#include <asm/byteorder.h>
+
 #include "wmi-events.h"
 #include "wmi-helpers.h"
 
@@ -110,25 +111,23 @@ EXPORT_SYMBOL_NS_GPL(devm_lwmi_events_register_notifier, "LENOVO_WMI_EVENTS");
 /**
  * lwmi_events_notify() - Call functions for the notifier call chain.
  * @wdev: The parent WMI device of the driver.
- * @obj: ACPI object passed by the registered WMI Event.
+ * @buf: WMI buffer passed by the registered WMI Event.
  *
  * Validate WMI event data and notify all registered drivers of the event and
  * its output.
  *
  * Return: 0 on success, or an error code.
  */
-static void lwmi_events_notify(struct wmi_device *wdev, union acpi_object *obj)
+static void lwmi_events_notify(struct wmi_device *wdev, const struct wmi_buffer *buf)
 {
 	struct lwmi_events_priv *priv = dev_get_drvdata(&wdev->dev);
+	void *notif_data = NULL;
 	int sel_prof;
 	int ret;
 
 	switch (priv->type) {
 	case LWMI_EVENT_THERMAL_MODE:
-		if (obj->type != ACPI_TYPE_INTEGER)
-			return;
-
-		sel_prof = obj->integer.value;
+		sel_prof = le32_to_cpup(buf->data);
 
 		switch (sel_prof) {
 		case LWMI_GZ_THERMAL_MODE_QUIET:
@@ -136,13 +135,8 @@ static void lwmi_events_notify(struct wmi_device *wdev, union acpi_object *obj)
 		case LWMI_GZ_THERMAL_MODE_PERFORMANCE:
 		case LWMI_GZ_THERMAL_MODE_EXTREME:
 		case LWMI_GZ_THERMAL_MODE_CUSTOM:
-			ret = blocking_notifier_call_chain(&events_chain_head,
-							   LWMI_EVENT_THERMAL_MODE,
-							   &sel_prof);
-			if (ret == NOTIFY_BAD)
-				dev_err(&wdev->dev,
-					"Failed to send notification to call chain for WMI Events\n");
-			return;
+			notif_data = &sel_prof;
+			break;
 		default:
 			dev_err(&wdev->dev, "Got invalid thermal mode: %x",
 				sel_prof);
@@ -152,6 +146,13 @@ static void lwmi_events_notify(struct wmi_device *wdev, union acpi_object *obj)
 	default:
 		return;
 	}
+
+	ret = blocking_notifier_call_chain(&events_chain_head, priv->type, notif_data);
+	if (ret == NOTIFY_BAD) {
+		dev_err(&wdev->dev,
+			"Failed to send notification to call chain for WMI event type %d\n",
+			priv->type);
+	}
 }
 
 static int lwmi_events_probe(struct wmi_device *wdev, const void *context)
@@ -185,7 +186,7 @@ static struct wmi_driver lwmi_events_driver = {
 	.id_table = lwmi_events_id_table,
 	.min_event_size = sizeof(u32),
 	.probe = lwmi_events_probe,
-	.notify = lwmi_events_notify,
+	.notify_new = lwmi_events_notify,
 	.no_singleton = true,
 };
 

-- 
2.55.0


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 4/7] platform/x86: lenovo-ymc: Modernize WMI driver and factor out events
  2026-11-15 19:27 [PATCH 0/7] platform/x86: ideapad-laptop: Trigger the EC on YMC events following Windows behavior Rong Zhang
                   ` (2 preceding siblings ...)
  2026-11-15 19:27 ` [PATCH 3/7] platform/x86: lenovo-wmi-events: Modernize " Rong Zhang
@ 2026-11-15 19:27 ` Rong Zhang
  2026-11-15 22:39   ` Mark Pearson
  2026-11-15 19:27 ` [PATCH 5/7] platform/x86: lenovo-ymc: Refresh posture on resume Rong Zhang
                   ` (3 subsequent siblings)
  7 siblings, 1 reply; 10+ messages in thread
From: Rong Zhang @ 2026-11-15 19:27 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Hans de Goede, Ilpo Järvinen,
	Gergo Koteles, Ike Panhc, Andrew Kallmeyer
  Cc: platform-driver-x86, linux-kernel, Mahir Faisal, Rong Zhang

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
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);
+	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@irl.hu>");
+MODULE_AUTHOR("Rong Zhang <i@rong.moe>");
 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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 5/7] platform/x86: lenovo-ymc: Refresh posture on resume
  2026-11-15 19:27 [PATCH 0/7] platform/x86: ideapad-laptop: Trigger the EC on YMC events following Windows behavior Rong Zhang
                   ` (3 preceding siblings ...)
  2026-11-15 19:27 ` [PATCH 4/7] platform/x86: lenovo-ymc: Modernize WMI driver and factor out events Rong Zhang
@ 2026-11-15 19:27 ` 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
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Rong Zhang @ 2026-11-15 19:27 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Hans de Goede, Ilpo Järvinen,
	Gergo Koteles, Ike Panhc, Andrew Kallmeyer
  Cc: platform-driver-x86, linux-kernel, Mahir Faisal, Rong Zhang

When the device's posture is changed during suspend, the software
posture may become out of sync after resume.

Add a resume callback refreshing the posture and poke ideapad-laptop to
trigger the EC.

Reported-by: Mahir Faisal <mahirfaisalantiq33@gmail.com>
Closes: https://msgid.link/CAGAHqH62UiDKktoqg6cJ_oqj02NqH6FDjj32ucw7w+L2-_-NOg@mail.gmail.com
Signed-off-by: Rong Zhang <i@rong.moe>
---
 drivers/platform/x86/lenovo/ymc.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/drivers/platform/x86/lenovo/ymc.c b/drivers/platform/x86/lenovo/ymc.c
index cb3ae49dfd9e..b2bb8ad9de0c 100644
--- a/drivers/platform/x86/lenovo/ymc.c
+++ b/drivers/platform/x86/lenovo/ymc.c
@@ -17,6 +17,7 @@
 #include <linux/input/sparse-keymap.h>
 #include <linux/mutex.h>
 #include <linux/notifier.h>
+#include <linux/pm.h>
 #include <linux/wmi.h>
 #include "ideapad-laptop.h"
 #include "wmi-events.h"
@@ -118,6 +119,16 @@ static int lenovo_ymc_notify(struct notifier_block *nb, unsigned long action, vo
 	}
 }
 
+static int lenovo_ymc_resume(struct device *dev)
+{
+	struct lenovo_ymc_private *priv = dev_get_drvdata(dev);
+
+	lenovo_ymc_refresh(priv, YMC_POSTURE_UNKNOWN);
+	return 0;
+}
+
+static DEFINE_SIMPLE_DEV_PM_OPS(lenovo_ymc_pm_ops, NULL, lenovo_ymc_resume);
+
 static int lenovo_ymc_probe(struct wmi_device *wdev, const void *ctx)
 {
 	struct lenovo_ymc_private *priv;
@@ -189,6 +200,7 @@ MODULE_DEVICE_TABLE(wmi, lenovo_ymc_wmi_id_table);
 static struct wmi_driver lenovo_ymc_driver = {
 	.driver = {
 		.name = "lenovo-ymc",
+		.pm = pm_sleep_ptr(&lenovo_ymc_pm_ops),
 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
 	},
 	.id_table = lenovo_ymc_wmi_id_table,

-- 
2.55.0


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 6/7] platform/x86: ideapad-laptop: Trigger the EC on YMC events following Windows behavior
  2026-11-15 19:27 [PATCH 0/7] platform/x86: ideapad-laptop: Trigger the EC on YMC events following Windows behavior Rong Zhang
                   ` (4 preceding siblings ...)
  2026-11-15 19:27 ` [PATCH 5/7] platform/x86: lenovo-ymc: Refresh posture on resume Rong Zhang
@ 2026-11-15 19:27 ` 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
  7 siblings, 0 replies; 10+ messages in thread
From: Rong Zhang @ 2026-11-15 19:27 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Hans de Goede, Ilpo Järvinen,
	Gergo Koteles, Ike Panhc, Andrew Kallmeyer
  Cc: platform-driver-x86, linux-kernel, Mahir Faisal, Rong Zhang

Currently, the ideapad-laptop driver triggers the EC with YMC command
0x01, which seems to be randomly chosen with trail-and-error and turns
out to be not a command for YMC events. While the devices in the DMI
quirk table recognize it, some other devices do not, making the
trigger_ec functionality a no-op.

Lenovo's YMC driver for Windows (YMC.exe) emits YMC command 0x08/0x09 on
fold/unfold YMC events, in which "unfold" means laptop mode and "fold"
means other modes.

Adopt the said sequence to make it work on all relevant devices. For the
sake of caution, only emit YMC command 0x08 only for the known fold
postures. For all other postures (including unknown ones), emit 0x09 to
make sure the touchpad and keyboard are still functional.

Reported-by: Mahir Faisal <mahirfaisalantiq33@gmail.com>
Closes: https://msgid.link/CAGAHqH6o0TUa1HXMrNV6e+HNYwU3xxHkOM665756n8Cp3AzhVQ@mail.gmail.com
Signed-off-by: Rong Zhang <i@rong.moe>
---
 drivers/platform/x86/lenovo/ideapad-laptop.c | 35 +++++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)

diff --git a/drivers/platform/x86/lenovo/ideapad-laptop.c b/drivers/platform/x86/lenovo/ideapad-laptop.c
index 91a8d11c79c5..4f9dc95d8c65 100644
--- a/drivers/platform/x86/lenovo/ideapad-laptop.c
+++ b/drivers/platform/x86/lenovo/ideapad-laptop.c
@@ -133,6 +133,11 @@ enum {
 	VPCCMD_W_BL_POWER = 0x33,
 };
 
+enum ymc_cmd {
+	YMC_CMD_FOLD	= 0x08,
+	YMC_CMD_UNFOLD	= 0x09,
+};
+
 /*
  * These correspond to the number of supported states - 1
  * Future keyboard types may need a new system, if there's a collision
@@ -1870,9 +1875,10 @@ static const struct dmi_system_id ymc_ec_trigger_quirk_dmi_table[] = {
 	{ }
 };
 
-static void ideapad_laptop_trigger_ec(void)
+static void ideapad_laptop_trigger_ec(u32 code)
 {
 	struct ideapad_private *priv;
+	enum ymc_cmd cmd;
 	int ret;
 
 	guard(mutex)(&ideapad_shared_mutex);
@@ -1884,8 +1890,29 @@ static void ideapad_laptop_trigger_ec(void)
 	if (!priv->features.ymc_ec_trigger)
 		return;
 
+	switch (code) {
+	case YMC_POSTURE_TABLET:
+	case YMC_POSTURE_DRAWING_BOARD:
+	case YMC_POSTURE_TENT:
+		/*
+		 * Sync keyboard backlight and disable the touchapd and keyboard
+		 * only when the device is folded...
+		 */
+		cmd = YMC_CMD_FOLD;
+		break;
+	default:
+		/*
+		 * ...IOW, whenever there is a chance that the device may be
+		 * unfolded, e.g., in laptop mode or any other unknown modes,
+		 * sync keyboard backlight and enable the touchapd and keyboard,
+		 * so that users don't lose input.
+		 */
+		cmd = YMC_CMD_UNFOLD;
+		break;
+	}
+
 	scoped_guard(mutex, &priv->vpc_mutex)
-		ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_YMC, 1);
+		ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_YMC, cmd);
 	if (ret)
 		dev_warn(&priv->platform_device->dev, "Could not write YMC: %d\n", ret);
 }
@@ -1893,9 +1920,11 @@ static void ideapad_laptop_trigger_ec(void)
 static int ideapad_laptop_nb_notify(struct notifier_block *nb,
 				    unsigned long action, void *data)
 {
+	const u32 *p = data;
+
 	switch (action) {
 	case IDEAPAD_LAPTOP_YMC_EVENT:
-		ideapad_laptop_trigger_ec();
+		ideapad_laptop_trigger_ec(*p);
 		break;
 	}
 

-- 
2.55.0


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 7/7] platform/x86: ideapad-laptop: Enable ymc_ec_trigger for Lenovo Yoga 7 14IRL8
  2026-11-15 19:27 [PATCH 0/7] platform/x86: ideapad-laptop: Trigger the EC on YMC events following Windows behavior Rong Zhang
                   ` (5 preceding siblings ...)
  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 ` 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
  7 siblings, 0 replies; 10+ messages in thread
From: Rong Zhang @ 2026-11-15 19:27 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Hans de Goede, Ilpo Järvinen,
	Gergo Koteles, Ike Panhc, Andrew Kallmeyer
  Cc: platform-driver-x86, linux-kernel, Mahir Faisal, Rong Zhang

The Lenovo Yoga 7 14IRL8's EC must be triggered by software on YMC
events, or else the enable state of touchpad, keyboard, and keyboard
backlight become out of sync.

Add a DMI quirk table entry for the said device.

Reported-by: Mahir Faisal <mahirfaisalantiq33@gmail.com>
Closes: https://msgid.link/CAGAHqH6o0TUa1HXMrNV6e+HNYwU3xxHkOM665756n8Cp3AzhVQ@mail.gmail.com
Signed-off-by: Rong Zhang <i@rong.moe>
---
 drivers/platform/x86/lenovo/ideapad-laptop.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/platform/x86/lenovo/ideapad-laptop.c b/drivers/platform/x86/lenovo/ideapad-laptop.c
index 4f9dc95d8c65..f9453de6df76 100644
--- a/drivers/platform/x86/lenovo/ideapad-laptop.c
+++ b/drivers/platform/x86/lenovo/ideapad-laptop.c
@@ -1872,6 +1872,13 @@ static const struct dmi_system_id ymc_ec_trigger_quirk_dmi_table[] = {
 			DMI_MATCH(DMI_PRODUCT_NAME, "82N7"),
 		},
 	},
+	{
+		/* Lenovo Yoga 7 14IRL8 */
+		.matches = {
+			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
+			DMI_MATCH(DMI_PRODUCT_NAME, "82YL"),
+		},
+	},
 	{ }
 };
 

-- 
2.55.0


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 0/7] platform/x86: ideapad-laptop: Trigger the EC on YMC events following Windows behavior
  2026-11-15 19:27 [PATCH 0/7] platform/x86: ideapad-laptop: Trigger the EC on YMC events following Windows behavior Rong Zhang
                   ` (6 preceding siblings ...)
  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 ` Mark Pearson
  7 siblings, 0 replies; 10+ messages in thread
From: Mark Pearson @ 2026-11-15 22:38 UTC (permalink / raw)
  To: Rong Zhang, Derek J . Clark, Hans de Goede, Ilpo Järvinen,
	Gergo Koteles, Ike Panhc, Andrew Kallmeyer
  Cc: platform-driver-x86, linux-kernel, Mahir Faisal, stable



On Sun, Nov 15, 2026, at 2:27 PM, Rong Zhang wrote:
> Currently, the ideapad-laptop driver triggers the EC with YMC command
> 0x01, which seems to be randomly chosen with trail-and-error and turns

typo: trial-and-error (this is in patch 6 too)

> out to be not a command for YMC events. While the devices in the DMI
> quirk table recognize it, some other devices do not, making the
> trigger_ec functionality a no-op.
>
> Lenovo's YMC driver for Windows (YMC.exe) emits YMC command 0x08/0x09 on
> fold/unfold YMC events, in which "unfold" means laptop mode and "fold"
> means other modes.
>
> Adopt the said sequence to make it work on all relevant devices. For the
> sake of caution, only emit YMC command 0x08 only for the known fold
> postures. For all other postures (including unknown ones), emit 0x09 to
> make sure the touchpad and keyboard are still functional.
>
> Beforehand, also fix pre-existing issues, modernize relevant WMI
> drivers, and add a resume callback refreshing the posture and poke
> ideapad-laptop to trigger the EC. See each patch for details.
>
> With these changes, add a DMI quirk table entry the Lenovo Yoga 7
> 14IRL8 as its EC must be triggered by software on YMC events properly
> (i.e., with command 0x08/0x09), or else the enable state of touchpad,
> keyboard, and keyboard backlight become out of sync.
>
> Reported-by: Mahir Faisal <mahirfaisalantiq33@gmail.com>
> Closes: 
> https://msgid.link/CAGAHqH6o0TUa1HXMrNV6e+HNYwU3xxHkOM665756n8Cp3AzhVQ@mail.gmail.com
> Signed-off-by: Rong Zhang <i@rong.moe>
> ---
> Rong Zhang (7):
>       platform/x86: lenovo-ymc: Use enumeration to represent device 
> posture
>       platform/x86: lenovo-ymc: Serialize WMI event handling
>       platform/x86: lenovo-wmi-events: Modernize WMI event handling
>       platform/x86: lenovo-ymc: Modernize WMI driver and factor out 
> events
>       platform/x86: lenovo-ymc: Refresh posture on resume
>       platform/x86: ideapad-laptop: Trigger the EC on YMC events 
> following Windows behavior
>       platform/x86: ideapad-laptop: Enable ymc_ec_trigger for Lenovo 
> Yoga 7 14IRL8
>
>  drivers/platform/x86/lenovo/Kconfig          |   2 +
>  drivers/platform/x86/lenovo/ideapad-laptop.c |  42 +++++++++-
>  drivers/platform/x86/lenovo/ideapad-laptop.h |   9 ++
>  drivers/platform/x86/lenovo/wmi-events.c     |  38 +++++----
>  drivers/platform/x86/lenovo/wmi-events.h     |   1 +
>  drivers/platform/x86/lenovo/ymc.c            | 118 +++++++++++++++++----------
>  6 files changed, 147 insertions(+), 63 deletions(-)
> ---
> base-commit: 7e1392a42ea288d70a266f6553fe06f57f8647ea
> change-id: 46a18a06-ymc-82yl-d59b4f8f891e
>
> Thanks,
> Rong

I feel like I should be able to find some more formal documentation on this, but as these platforms aren't in the Linux program not sure what I'll get (or how long it will take) and the series looks good to me. I'll see what I can find so hopefully we can avoid a long list of quirks (as needed in patch 7).

One open question on patch 4 - but patches 1 to 3 and 5 to 7 look good to me. For those:
Reviewed-by: Mark Pearson <mpearson-lenovo@squebb.ca>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 4/7] platform/x86: lenovo-ymc: Modernize WMI driver and factor out events
  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
  0 siblings, 0 replies; 10+ messages in thread
From: Mark Pearson @ 2026-11-15 22:39 UTC (permalink / raw)
  To: Rong Zhang, Derek J . Clark, Hans de Goede, Ilpo Järvinen,
	Gergo Koteles, Ike Panhc, Andrew Kallmeyer
  Cc: platform-driver-x86, linux-kernel, Mahir Faisal



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?

> 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?

> +	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@irl.hu>");
> +MODULE_AUTHOR("Rong Zhang <i@rong.moe>");
>  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

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2026-11-15 22:40 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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-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

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®