[PATCH v2 2/2] platform/asus: asus-wmi: unify WMI and HID keyboard brightness stepping

From: Bartu Alev

Date: Fri Nov 20 2026 - 18:43:00 EST


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@xxxxxxxxxxx>
Signed-off-by: Bartu Alev <bartualev@xxxxxxxxx>
---
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