mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bartu Alev <bartualev@gmail.com>
To: "Corentin Chary" <corentin.chary@gmail.com>,
	"Luke D. Jones" <luke@ljones.dev>,
	"Denis Benato" <denis.benato@linux.dev>,
	"Hans de Goede" <hansg@kernel.org>,
	"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
	"Antheas Kapenekakis" <lkml@antheas.dev>,
	"Benjamin Tissoires" <bentiss@kernel.org>
Cc: Alex Bogusta <alex@bogusta.com>,
	 Denis Benato <benato.denis96@gmail.com>,
	 platform-driver-x86@vger.kernel.org,
	linux-kernel@vger.kernel.org,  Bartu Alev <bartualev@gmail.com>
Subject: [PATCH v2 2/2] platform/asus: asus-wmi: unify WMI and HID keyboard brightness stepping
Date: Sat, 21 Nov 2026 02:42:28 +0300	[thread overview]
Message-ID: <20261121-b4-asus-kbd-fixes-v2-2-52cda07d233f@gmail.com> (raw)
In-Reply-To: <20261121-b4-asus-kbd-fixes-v2-0-52cda07d233f@gmail.com>

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


      parent reply	other threads:[~2026-11-20 23:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

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=20261121-b4-asus-kbd-fixes-v2-2-52cda07d233f@gmail.com \
    --to=bartualev@gmail.com \
    --cc=alex@bogusta.com \
    --cc=benato.denis96@gmail.com \
    --cc=bentiss@kernel.org \
    --cc=corentin.chary@gmail.com \
    --cc=denis.benato@linux.dev \
    --cc=hansg@kernel.org \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lkml@antheas.dev \
    --cc=luke@ljones.dev \
    --cc=platform-driver-x86@vger.kernel.org \
    /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®