mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/2] platform/asus: asus-wmi: fix and unify keyboard brightness key handling
@ 2026-11-20 23:42 Bartu Alev
  2026-11-20 23:42 ` [PATCH v2 1/2] platform/asus: asus-wmi: clamp keyboard brightness hotkey updates Bartu Alev
  2026-11-20 23:42 ` [PATCH v2 2/2] platform/asus: asus-wmi: unify WMI and HID keyboard brightness stepping Bartu Alev
  0 siblings, 2 replies; 3+ messages in thread
From: Bartu Alev @ 2026-11-20 23:42 UTC (permalink / raw)
  To: Corentin Chary, Luke D. Jones, Denis Benato, Hans de Goede,
	Ilpo Järvinen, Antheas Kapenekakis, Benjamin Tissoires
  Cc: Alex Bogusta, Denis Benato, platform-driver-x86, linux-kernel,
	Bartu Alev, stable

The WMI hotkey path and the HID event path in asus-wmi step the
keyboard backlight with two separate copies of the same arithmetic, and
the two copies disagree. The WMI copy computes the step in the unsigned
enum led_brightness and stores the new level without clamping it, so
holding brightness-down at zero drives the cached level negative, the
"value >= 0" apply check then drops every following transition, and the
hardware-changed notification hands the wrapped negative to userspace as
4294967294+ (or -ENODATA for -1). Pressing brightness-up at maximum
reports 4, above max_brightness.

The first patch fixes the signedness, bounds clamping, and notification
snapshot on its own so -stable trees can pick it up without the rest of
the series. The second patch removes the duplication: both paths now
step through one shared helper that reads, computes, and writes the
level inside a single hold of asus_ref.lock.

With the series applied, the level stays within
[0, ASUS_EV_MAX_BRIGHTNESS] on every transition and userspace sees no
wrapped or out-of-range values.

Signed-off-by: Bartu Alev <bartualev@gmail.com>
---
Changes in v2:
- Rebase onto review-ilpo-next, where the driver moved to
  drivers/platform/asus/asus-wmi.c, and update subject prefixes to
  'platform/asus:'.
- Patch 1: Fix Fixes: tag to fac55d29581f ("platform/x86: asus-wmi:
  Add support for multiple kbd led handlers") and reference Alex
  Bogusta's earlier patch [1].
- Patch 1: Step in signed int instead of unsigned enum led_brightness
  and add an explanatory comment; prevents 0 - 1 from wrapping and
  clamping to max brightness (3) if patch 1 is applied standalone on
  -stable.
- Patch 1: Clear kbd_led_notify atomically when snapshotting it at the
  top of kbd_led_update_all() instead of after do_kbd_led_set().
- Patch 2: Drop inline from asus_kbd_led_calc_next(), drop the
  unreachable default case (all enum values are covered), drop the
  redundant NULL check in void asus_kbd_led_step(), and normalize an
  unknown (-1) starting level to 0 before stepping.
- Link to v1: https://patch.msgid.link/20261025-b4-asus-kbd-fixes-v1-0-fa4dcf7d2a69@gmail.com

[1] https://lore.kernel.org/r/20261016015117.32174-1-alex@bogusta.com/

---
Bartu Alev (2):
      platform/asus: asus-wmi: clamp keyboard brightness hotkey updates
      platform/asus: asus-wmi: unify WMI and HID keyboard brightness stepping

 drivers/platform/asus/asus-wmi.c | 77 +++++++++++++++++++---------------------
 1 file changed, 36 insertions(+), 41 deletions(-)
---
base-commit: 2268d837dea13a78639fa3dad60472bb67478709
change-id: 20261025-b4-asus-kbd-fixes-6c7e3780597b

Best regards,
--  
Bartu Alev <bartualev@gmail.com>


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

* [PATCH v2 1/2] platform/asus: asus-wmi: clamp keyboard brightness hotkey updates
  2026-11-20 23:42 [PATCH v2 0/2] platform/asus: asus-wmi: fix and unify keyboard brightness key handling Bartu Alev
@ 2026-11-20 23:42 ` Bartu Alev
  2026-11-20 23:42 ` [PATCH v2 2/2] platform/asus: asus-wmi: unify WMI and HID keyboard brightness stepping Bartu Alev
  1 sibling, 0 replies; 3+ messages in thread
From: Bartu Alev @ 2026-11-20 23:42 UTC (permalink / raw)
  To: Corentin Chary, Luke D. Jones, Denis Benato, Hans de Goede,
	Ilpo Järvinen, Antheas Kapenekakis, Benjamin Tissoires
  Cc: Alex Bogusta, Denis Benato, platform-driver-x86, linux-kernel,
	Bartu Alev, stable

The NOTIFY_KBD_BRTUP/DWN/TOGGLE handlers pass led_value ± 1 into
kbd_led_set_by_kbd(), which stores the value into asus->kbd_led_wk
without clamping it. Pressing brightness-down at level 0 drives the
cached level negative (-1, -2, ...); kbd_led_update_all() then drops
every hardware update on its "value >= 0" check while
led_classdev_notify_brightness_hw_changed() hands the wrapped negative
to userspace, so brightness_hw_changed reports 4294967294+ or ENODATA
and the brightness-up key appears dead until the counter climbs back
to 0. Pressing brightness-up at maximum reports 4, above
max_brightness.

Compute the level as int and clamp it in kbd_led_set_by_kbd(): the
stepping must happen in a signed type, because enum led_brightness is
an unsigned type in practice and 0 - 1 passed through it wraps to a
value that clamps to full brightness instead of staying off.

Also clear kbd_led_notify when snapshotting it in
kbd_led_update_all() instead of after updating the hardware, so a
keypress queued while the worker runs is not swallowed by the late
clear.

This addresses the same bug as Alex Bogusta's earlier patch
(reviewed by Denis Benato) [1], while also closing the kbd_led_notify
race in kbd_led_update_all()."

[1] https://lore.kernel.org/r/20261016015117.32174-1-alex@bogusta.com/

Fixes: fac55d29581f ("platform/x86: asus-wmi: Add support for multiple kbd led handlers")
Cc: stable@vger.kernel.org
Cc: Antheas Kapenekakis <lkml@antheas.dev>
Cc: Alex Bogusta <alex@bogusta.com>
Cc: Denis Benato <denis.benato@linux.dev>
Signed-off-by: Bartu Alev <bartualev@gmail.com>
---
 drivers/platform/asus/asus-wmi.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/drivers/platform/asus/asus-wmi.c b/drivers/platform/asus/asus-wmi.c
index b33ebdb21aa2..f1a94bcdb0c0 100644
--- a/drivers/platform/asus/asus-wmi.c
+++ b/drivers/platform/asus/asus-wmi.c
@@ -1784,6 +1784,7 @@ static void kbd_led_update_all(struct work_struct *work)
 		registered = asus->kbd_led_registered;
 		value = asus->kbd_led_wk;
 		notify = asus->kbd_led_notify;
+		asus->kbd_led_notify = false;
 	}
 
 	if (!registered) {
@@ -1806,11 +1807,8 @@ static void kbd_led_update_all(struct work_struct *work)
 
 	if (value >= 0)
 		do_kbd_led_set(&asus->kbd_led, value);
-	if (notify) {
-		scoped_guard(spinlock_irqsave, &asus_ref.lock)
-			asus->kbd_led_notify = false;
+	if (notify)
 		led_classdev_notify_brightness_hw_changed(&asus->kbd_led, value);
-	}
 }
 
 /*
@@ -1958,10 +1956,16 @@ static int kbd_led_set(struct led_classdev *led_cdev, enum led_brightness value)
 	return 0;
 }
 
-static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
+static void kbd_led_set_by_kbd(struct asus_wmi *asus, int value)
 {
+	/*
+	 * The hotkey callers step the level by one, which must happen in
+	 * a signed type: enum led_brightness is an unsigned type in
+	 * practice, so -1 passed through it wraps to a value that clamps
+	 * to ASUS_EV_MAX_BRIGHTNESS instead of staying off.
+	 */
 	scoped_guard(spinlock_irqsave, &asus_ref.lock) {
-		asus->kbd_led_wk = value;
+		asus->kbd_led_wk = clamp_val(value, 0, ASUS_EV_MAX_BRIGHTNESS);
 		asus->kbd_led_notify = true;
 	}
 	queue_work(asus->led_workqueue, &asus->kbd_led_work);
@@ -4677,7 +4681,7 @@ static int asus_wmi_get_event_code(union acpi_object *obj)
 
 static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
 {
-	enum led_brightness led_value;
+	int led_value;
 	unsigned int key_value = 1;
 	bool autorelease = 1;
 

-- 
2.56.1


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

* [PATCH v2 2/2] platform/asus: asus-wmi: unify WMI and HID keyboard brightness stepping
  2026-11-20 23:42 [PATCH v2 0/2] platform/asus: asus-wmi: fix and unify keyboard brightness key handling Bartu Alev
  2026-11-20 23:42 ` [PATCH v2 1/2] platform/asus: asus-wmi: clamp keyboard brightness hotkey updates Bartu Alev
@ 2026-11-20 23:42 ` Bartu Alev
  1 sibling, 0 replies; 3+ messages in thread
From: Bartu Alev @ 2026-11-20 23:42 UTC (permalink / raw)
  To: Corentin Chary, Luke D. Jones, Denis Benato, Hans de Goede,
	Ilpo Järvinen, Antheas Kapenekakis, Benjamin Tissoires
  Cc: Alex Bogusta, Denis Benato, platform-driver-x86, linux-kernel,
	Bartu Alev

The WMI notify handlers and asus_hid_event() each carry their own copy
of the brightness stepping arithmetic. The WMI path also reads
kbd_led_wk under asus_ref.lock, drops the lock, and re-acquires it in
kbd_led_set_by_kbd(), so a concurrent event between the two critical
sections can overwrite the newer state, and every WMI event pays for a
lock hold it does not need.

Move the arithmetic into asus_kbd_led_calc_next() and add
asus_kbd_led_step() to compute and store the new level under a single
asus_ref.lock hold. Route the WMI hotkey handlers through it, drop
kbd_led_set_by_kbd(), and reuse the helper in asus_hid_event().

Normalizing an unknown (-1) starting level to 0 before stepping also
makes the first brightness-up or toggle press turn the backlight on at
level 1 on devices where the WMI readout is unavailable, instead of
leaving it off.

Cc: Antheas Kapenekakis <lkml@antheas.dev>
Signed-off-by: Bartu Alev <bartualev@gmail.com>
---
 drivers/platform/asus/asus-wmi.c | 79 ++++++++++++++++++----------------------
 1 file changed, 35 insertions(+), 44 deletions(-)

diff --git a/drivers/platform/asus/asus-wmi.c b/drivers/platform/asus/asus-wmi.c
index f1a94bcdb0c0..c216f7166c2f 100644
--- a/drivers/platform/asus/asus-wmi.c
+++ b/drivers/platform/asus/asus-wmi.c
@@ -1811,6 +1811,37 @@ static void kbd_led_update_all(struct work_struct *work)
 		led_classdev_notify_brightness_hw_changed(&asus->kbd_led, value);
 }
 
+static int asus_kbd_led_calc_next(int cur, enum asus_hid_event event)
+{
+	/* -1 means the level is unknown; start stepping from off. */
+	if (cur < 0)
+		cur = 0;
+
+	switch (event) {
+	case ASUS_EV_BRTUP:
+		cur += 1;
+		break;
+	case ASUS_EV_BRTDOWN:
+		cur -= 1;
+		break;
+	case ASUS_EV_BRTTOGGLE:
+		cur = (cur >= ASUS_EV_MAX_BRIGHTNESS) ? 0 : cur + 1;
+		break;
+	}
+
+	return clamp(cur, 0, ASUS_EV_MAX_BRIGHTNESS);
+}
+
+static void asus_kbd_led_step(struct asus_wmi *asus, enum asus_hid_event event)
+{
+	scoped_guard(spinlock_irqsave, &asus_ref.lock) {
+		asus->kbd_led_wk = asus_kbd_led_calc_next(asus->kbd_led_wk, event);
+		asus->kbd_led_notify = true;
+	}
+
+	queue_work(asus->led_workqueue, &asus->kbd_led_work);
+}
+
 /*
  * This function is called from hid-asus to inform asus-wmi of brightness
  * changes initiated by the keyboard backlight keys.
@@ -1818,31 +1849,13 @@ static void kbd_led_update_all(struct work_struct *work)
 int asus_hid_event(enum asus_hid_event event)
 {
 	struct asus_wmi *asus;
-	int brightness;
 
 	guard(spinlock_irqsave)(&asus_ref.lock);
 	asus = asus_ref.asus;
 	if (!asus || !asus->kbd_led_registered)
 		return -EBUSY;
 
-	brightness = asus->kbd_led_wk;
-
-	switch (event) {
-	case ASUS_EV_BRTUP:
-		brightness += 1;
-		break;
-	case ASUS_EV_BRTDOWN:
-		brightness -= 1;
-		break;
-	case ASUS_EV_BRTTOGGLE:
-		if (brightness >= ASUS_EV_MAX_BRIGHTNESS)
-			brightness = 0;
-		else
-			brightness += 1;
-		break;
-	}
-
-	asus->kbd_led_wk = clamp_val(brightness, 0, ASUS_EV_MAX_BRIGHTNESS);
+	asus->kbd_led_wk = asus_kbd_led_calc_next(asus->kbd_led_wk, event);
 	asus->kbd_led_notify = true;
 	queue_work(asus->led_workqueue, &asus->kbd_led_work);
 	return 0;
@@ -1956,21 +1969,6 @@ static int kbd_led_set(struct led_classdev *led_cdev, enum led_brightness value)
 	return 0;
 }
 
-static void kbd_led_set_by_kbd(struct asus_wmi *asus, int value)
-{
-	/*
-	 * The hotkey callers step the level by one, which must happen in
-	 * a signed type: enum led_brightness is an unsigned type in
-	 * practice, so -1 passed through it wraps to a value that clamps
-	 * to ASUS_EV_MAX_BRIGHTNESS instead of staying off.
-	 */
-	scoped_guard(spinlock_irqsave, &asus_ref.lock) {
-		asus->kbd_led_wk = clamp_val(value, 0, ASUS_EV_MAX_BRIGHTNESS);
-		asus->kbd_led_notify = true;
-	}
-	queue_work(asus->led_workqueue, &asus->kbd_led_work);
-}
-
 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
 {
 	struct asus_wmi *asus;
@@ -4681,7 +4679,6 @@ static int asus_wmi_get_event_code(union acpi_object *obj)
 
 static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
 {
-	int led_value;
 	unsigned int key_value = 1;
 	bool autorelease = 1;
 
@@ -4698,22 +4695,16 @@ static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
 		return;
 	}
 
-	scoped_guard(spinlock_irqsave, &asus_ref.lock)
-		led_value = asus->kbd_led_wk;
-
 	if (code == NOTIFY_KBD_BRTUP) {
-		kbd_led_set_by_kbd(asus, led_value + 1);
+		asus_kbd_led_step(asus, ASUS_EV_BRTUP);
 		return;
 	}
 	if (code == NOTIFY_KBD_BRTDWN) {
-		kbd_led_set_by_kbd(asus, led_value - 1);
+		asus_kbd_led_step(asus, ASUS_EV_BRTDOWN);
 		return;
 	}
 	if (code == NOTIFY_KBD_BRTTOGGLE) {
-		if (led_value >= ASUS_EV_MAX_BRIGHTNESS)
-			kbd_led_set_by_kbd(asus, 0);
-		else
-			kbd_led_set_by_kbd(asus, led_value + 1);
+		asus_kbd_led_step(asus, ASUS_EV_BRTTOGGLE);
 		return;
 	}
 

-- 
2.56.1


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

end of thread, other threads:[~2026-11-20 23:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20 23:42 [PATCH v2 0/2] platform/asus: asus-wmi: fix and unify keyboard brightness key handling Bartu Alev
2026-11-20 23:42 ` [PATCH v2 1/2] platform/asus: asus-wmi: clamp keyboard brightness hotkey updates Bartu Alev
2026-11-20 23:42 ` [PATCH v2 2/2] platform/asus: asus-wmi: unify WMI and HID keyboard brightness stepping Bartu Alev

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®