Linux Input/HID development
 help / color / mirror / Atom feed
* [PATCH v2] HID: asus: support the Zenbook A14 (UX3407RA) keyboard backlight
@ 2026-12-01 18:50 Jesper B. Christensen
  2026-12-01 19:02 ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: Jesper B. Christensen @ 2026-12-01 18:50 UTC (permalink / raw)
  To: linux-kernel; +Cc: linux-input, jikos, bentiss, Jesper B. Christensen

The ASUS Zenbook A14 (UX3407RA), a Qualcomm Snapdragon X2 Elite
(Hamoa)-based laptop, carries its main keyboard on an I2C-HID device
with the ID 0B05:0220. Its Fn/media hotkeys are emitted as vendor-page
(HID_UP_ASUSVENDOR) usages on report 0x5A.

In addition to the the standard I2C keyboard quirks and the keyboard
backlight quirk, a few new have been added:

  QUIRK_KBD_REPORT_SIZE: The UX3407RA seems to only accept 64 byte
   reports
  QUIRK_KBD_OOBE: Refactor existing Out-Of-Box mitigation into a
   quirk.

The keyboard backlight is software controlled. Since WMI is not
available on Snapdragon based platforms this change registers a
separate led device that sends hid reports to the Embedded Controller
(EC).

Signed-off-by: Jesper B. Christensen <jaytersen@gmail.com>
---
 drivers/hid/hid-asus.c | 90 +++++++++++++++++++++++++++++++++++++++---
 drivers/hid/hid-ids.h  |  1 +
 2 files changed, 86 insertions(+), 5 deletions(-)

diff --git a/drivers/hid/hid-asus.c b/drivers/hid/hid-asus.c
index f8339740b9ba..c932b624dba5 100644
--- a/drivers/hid/hid-asus.c
+++ b/drivers/hid/hid-asus.c
@@ -68,6 +68,7 @@ MODULE_DESCRIPTION("Asus HID Keyboard and TouchPad");
 #define ASUS_FAN_CTRL_KEY_CODE 0xae
 
 #define SUPPORT_KBD_BACKLIGHT BIT(0)
+#define EC_KBD_MAX_BACKLIGHT 3
 
 #define MAX_TOUCH_MAJOR 8
 #define MAX_PRESSURE 128
@@ -102,6 +103,8 @@ MODULE_DESCRIPTION("Asus HID Keyboard and TouchPad");
 #define QUIRK_ROG_ALLY_XPAD		BIT(13)
 #define QUIRK_HID_FN_LOCK		BIT(14)
 #define QUIRK_FILTER_CAMERA_COMPANION	BIT(15)
+#define QUIRK_KBD_REPORT_SIZE           BIT(16)
+#define QUIRK_KBD_OOBE			BIT(17)
 
 #define I2C_KEYBOARD_QUIRKS			(QUIRK_FIX_NOTEBOOK_REPORT | \
 						 QUIRK_NO_INIT_REPORTS | \
@@ -176,11 +179,13 @@ struct asus_drvdata {
 	bool battery_in_query;
 	unsigned long battery_next_query;
 	struct asus_hid_listener listener;
+	struct led_classdev kbd_led_cdev;
 	bool fn_lock;
 	struct asus_xgm_led *xgm_led;
 };
 
 static int asus_report_battery(struct asus_drvdata *, u8 *, int);
+static void asus_kbd_illum_toggle(struct asus_drvdata *);
 
 static const struct asus_touchpad_info asus_i2c_tp = {
 	.max_x = 2794,
@@ -477,6 +482,7 @@ static int asus_event(struct hid_device *hdev, struct hid_field *field,
 		case KEY_KBDILLUMDOWN:
 			return !asus_hid_event(ASUS_EV_BRTDOWN);
 		case KEY_KBDILLUMTOGGLE:
+			asus_kbd_illum_toggle(drvdata);
 			return !asus_hid_event(ASUS_EV_BRTTOGGLE);
 		case KEY_FN_ESC:
 			if (drvdata->quirks & QUIRK_HID_FN_LOCK) {
@@ -585,10 +591,18 @@ static int asus_raw_event(struct hid_device *hdev,
 static int asus_kbd_set_report(struct hid_device *hdev, const u8 *buf, size_t buf_size)
 {
 	int ret;
-	u8 *dmabuf __free(kfree) = kmemdup(buf, buf_size, GFP_KERNEL);
+	size_t alloc_size = buf_size;
+
+	if (QUIRK_KBD_REPORT_SIZE)
+		alloc_size = (buf_size > FEATURE_KBD_REPORT_SIZE) ?
+			buf_size : FEATURE_KBD_REPORT_SIZE;
+
+	u8 *dmabuf __free(kfree) = kzalloc(alloc_size, GFP_KERNEL);
 	if (!dmabuf)
 		return -ENOMEM;
 
+	memcpy(dmabuf, buf, buf_size);
+
 	/* The device may be runtime suspended, e.g. for a deferred backlight change */
 	ret = hid_hw_power(hdev, PM_HINT_FULLON);
 	if (ret < 0)
@@ -598,8 +612,8 @@ static int asus_kbd_set_report(struct hid_device *hdev, const u8 *buf, size_t bu
 	 * The report ID should be set from the incoming buffer due to LED and key
 	 * interfaces having different pages
 	 */
-	ret = hid_hw_raw_request(hdev, buf[0], dmabuf, buf_size, HID_FEATURE_REPORT,
-				 HID_REQ_SET_REPORT);
+	ret = hid_hw_raw_request(hdev, buf[0], dmabuf, alloc_size, HID_FEATURE_REPORT,
+				  HID_REQ_SET_REPORT);
 	hid_hw_power(hdev, PM_HINT_NORMAL);
 	return ret;
 }
@@ -718,6 +732,38 @@ static void asus_kbd_set_brightness(struct hid_device *hdev, u8 brightness)
 		hid_err(hdev, "Asus failed to set keyboard backlight: %d\n", ret);
 }
 
+static void asus_kbd_set_brightness_cdev(struct led_classdev *led_cdev,
+				    enum led_brightness brightness)
+{
+	struct asus_drvdata *drvdata = container_of(led_cdev, struct asus_drvdata, kbd_led_cdev);
+	u8 level = (u8)brightness;
+
+	if (level > EC_KBD_MAX_BACKLIGHT)
+		level = EC_KBD_MAX_BACKLIGHT;
+
+	asus_kbd_set_brightness(drvdata->hdev, level);
+	drvdata->kbd_backlight_brightness = level;
+}
+
+static enum led_brightness asus_kbd_get_brightness_cdev(struct led_classdev *led_cdev)
+{
+	struct asus_drvdata *drvdata = container_of(led_cdev, struct asus_drvdata, kbd_led_cdev);
+
+	return (enum led_brightness)drvdata->kbd_backlight_brightness;
+}
+
+static void asus_kbd_illum_toggle(struct asus_drvdata *drvdata)
+{
+	if (!drvdata->kbd_led_cdev.brightness_set)
+		return;
+
+	u8 current_level = drvdata->kbd_backlight_brightness;
+	u8 max_level = EC_KBD_MAX_BACKLIGHT;
+	u8 next_level = (current_level + 1) % (max_level + 1);
+
+	asus_kbd_set_brightness_cdev(&drvdata->kbd_led_cdev, next_level);
+}
+
 static void asus_kbd_wmi_fan(struct hid_device *hdev, struct hid_raw_event_data *data)
 {
 	struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
@@ -996,7 +1042,7 @@ static int asus_kbd_register_leds(struct hid_device *hdev)
 	if (!(kbd_func & SUPPORT_KBD_BACKLIGHT))
 		return -ENODEV;
 
-	if (dmi_match(DMI_PRODUCT_FAMILY, "ProArt P16")) {
+	if (drvdata->quirks & QUIRK_KBD_OOBE) {
 		ret = asus_kbd_disable_oobe(hdev);
 		if (ret < 0)
 			return ret;
@@ -1012,10 +1058,28 @@ static int asus_kbd_register_leds(struct hid_device *hdev)
 	drvdata->listener.brightness_set = asus_kbd_backlight_set;
 	ret = asus_hid_register_listener(&drvdata->listener);
 	if (ret < 0) {
-		hid_err(hdev, "Unable to register kbd brightness listener: %d\n", ret);
+		if (ret != -ENODEV) {
+			hid_err(hdev, "Unable to register kbd brightness listener: %d\n", ret);
+		}
 		drvdata->listener.brightness_set = NULL;
 	}
 
+	// Non-x86 architectures do not have WMI, just register a led
+	// device directly
+	if (ret == -ENODEV) {
+		drvdata->kbd_led_cdev.name = "asus::kbd_backlight";
+		drvdata->kbd_led_cdev.brightness_set = asus_kbd_set_brightness_cdev;
+		drvdata->kbd_led_cdev.brightness_get = asus_kbd_get_brightness_cdev;
+		drvdata->kbd_led_cdev.max_brightness = EC_KBD_MAX_BACKLIGHT;
+		ret = led_classdev_register(&hdev->dev, &drvdata->kbd_led_cdev);
+		if (ret < 0) {
+			hid_err(hdev, "Unable to register kbd brightness led: %d\n", ret);
+			drvdata->kbd_led_cdev.brightness_set = NULL;
+		}
+
+		asus_kbd_set_brightness_cdev(&drvdata->kbd_led_cdev, 1);
+	}
+
 	return ret;
 }
 
@@ -1499,6 +1563,9 @@ static int __maybe_unused asus_resume(struct hid_device *hdev)
 	if (drvdata->listener.brightness_set)
 		asus_kbd_backlight_set(&drvdata->listener, drvdata->kbd_backlight_brightness);
 
+	if (drvdata->kbd_led_cdev.brightness_set)
+		asus_kbd_set_brightness_cdev(&drvdata->kbd_led_cdev, drvdata->kbd_backlight_brightness);
+
 	return 0;
 }
 
@@ -1636,6 +1703,9 @@ static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
 		}
 	}
 
+	if (dmi_match(DMI_PRODUCT_FAMILY, "ProArt P16"))
+		drvdata->quirks |= QUIRK_KBD_OOBE;
+
 	/* Laptops keyboard backlight is always at 0x5a */
 	if (is_vendor && (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT) &&
 	    (asus_has_report_id(hdev, FEATURE_KBD_REPORT_ID)) &&
@@ -1676,6 +1746,9 @@ static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
 	if (drvdata->xgm_led)
 		devm_led_classdev_unregister(&hdev->dev, &drvdata->xgm_led->cdev);
 
+	if (drvdata->kbd_led_cdev.brightness_set)
+		led_classdev_unregister(&drvdata->kbd_led_cdev);
+
 	asus_worker_stop(drvdata->worker);
 	hid_hw_stop(hdev);
 	return ret;
@@ -1691,6 +1764,9 @@ static void asus_remove(struct hid_device *hdev)
 	if (drvdata->xgm_led)
 		devm_led_classdev_unregister(&hdev->dev, &drvdata->xgm_led->cdev);
 
+	if (drvdata->kbd_led_cdev.brightness_set)
+		led_classdev_unregister(&drvdata->kbd_led_cdev);
+
 	asus_worker_stop(drvdata->worker);
 	hid_hw_stop(hdev);
 }
@@ -1813,6 +1889,10 @@ static const struct hid_device_id asus_devices[] = {
 	{ HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
 		USB_DEVICE_ID_ASUSTEK_I2C_ZENBOOK_KEYBOARD),
 	  I2C_KEYBOARD_QUIRKS | QUIRK_FILTER_CAMERA_COMPANION },
+	{ HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
+		USB_DEVICE_ID_ASUSTEK_I2C_ZENBOOK_A14_KEYBOARD),
+	  I2C_KEYBOARD_QUIRKS | QUIRK_USE_KBD_BACKLIGHT | QUIRK_KBD_REPORT_SIZE |
+	  QUIRK_KBD_OOBE},
 	{ HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
 		USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD), I2C_TOUCHPAD_QUIRKS },
 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
index 61c1c6673de1..fc6d93338fec 100644
--- a/drivers/hid/hid-ids.h
+++ b/drivers/hid/hid-ids.h
@@ -222,6 +222,7 @@
 #define USB_DEVICE_ID_ASUSTEK_T304_KEYBOARD	0x184a
 #define USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD	0x8585
 #define USB_DEVICE_ID_ASUSTEK_I2C_ZENBOOK_KEYBOARD	0x4b42
+#define USB_DEVICE_ID_ASUSTEK_I2C_ZENBOOK_A14_KEYBOARD	0x0220
 #define USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD	0x0101
 #define USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD1 0x1854
 #define USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD2 0x1837
-- 
2.53.0


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

* Re: [PATCH v2] HID: asus: support the Zenbook A14 (UX3407RA) keyboard backlight
  2026-12-01 18:50 [PATCH v2] HID: asus: support the Zenbook A14 (UX3407RA) keyboard backlight Jesper B. Christensen
@ 2026-12-01 19:02 ` sashiko-bot
  2026-12-01 20:57   ` Jesper B. Christensen
  0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-12-01 19:02 UTC (permalink / raw)
  To: Jesper B. Christensen; +Cc: linux-input, dmitry.torokhov

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] HID: asus: always-true QUIRK_KBD_REPORT_SIZE check in
  asus_kbd_set_report()
- [High] HID: asus: sleep in atomic context in
  asus_kbd_set_brightness_cdev()
- [Medium] HID: asus: unconsumed KEY_KBDILLUMTOGGLE and LED desync in
  asus_event()
- [Medium] HID: asus: error fallthrough and LED init order in
  asus_kbd_register_leds()
- [Medium] HID: asus: teardown race on kbd_led_cdev in asus_remove() and
  asus_probe()
- [Medium] HID: asus: unsynchronized kbd_backlight_brightness updates
  across LED paths
--

commit 88e8e96de263e1806c05ad0eaf08fe87ba34473c
Author: Jesper B. Christensen <jaytersen@gmail.com>

HID: asus: support the Zenbook A14 (UX3407RA) keyboard backlight

This commit adds HID device ID 0B05:0220 for the ASUS Zenbook A14
(UX3407RA) I2C keyboard, introduces QUIRK_KBD_REPORT_SIZE and
QUIRK_KBD_OOBE, and registers a direct LED class device for the keyboard
backlight when asus-wmi is unavailable.

> diff --git a/drivers/hid/hid-asus.c b/drivers/hid/hid-asus.c
> index 4f5749c9b2dbb..1e940a495c41c 100644
> --- a/drivers/hid/hid-asus.c
> +++ b/drivers/hid/hid-asus.c

[ ... ]

> @@ -477,6 +482,7 @@ static int asus_event(struct hid_device *hdev, struct hid_field *field,
>  		case KEY_KBDILLUMDOWN:
>  			return !asus_hid_event(ASUS_EV_BRTDOWN);
>  		case KEY_KBDILLUMTOGGLE:
> +			asus_kbd_illum_toggle(drvdata);
>  			return !asus_hid_event(ASUS_EV_BRTTOGGLE);
>  		case KEY_FN_ESC:
>  			if (drvdata->quirks & QUIRK_HID_FN_LOCK) {

[Severity: Medium]
Does KEY_KBDILLUMTOGGLE both toggle the backlight in the kernel and get
forwarded to userspace when asus-wmi is absent?

When asus-wmi is unavailable, asus_hid_event(ASUS_EV_BRTTOGGLE) returns
-ENODEV, so !asus_hid_event() returns 0 and hid_process_event() reports
KEY_KBDILLUMTOGGLE to userspace after asus_kbd_illum_toggle() has already
advanced the hardware brightness.  In addition, asus_kbd_illum_toggle()
does not update drvdata->kbd_led_cdev.brightness or notify userspace via
led_classdev_notify_brightness_hw_changed(), so a userspace power daemon
handling the key event will toggle sysfs a second time from a stale
brightness value.

> @@ -585,10 +591,18 @@ static int asus_raw_event(struct hid_device *hdev,
>  static int asus_kbd_set_report(struct hid_device *hdev, const u8 *buf, size_t buf_size)
>  {
>  	int ret;
> -	u8 *dmabuf __free(kfree) = kmemdup(buf, buf_size, GFP_KERNEL);
> +	size_t alloc_size = buf_size;
> +
> +	if (QUIRK_KBD_REPORT_SIZE)
> +		alloc_size = (buf_size > FEATURE_KBD_REPORT_SIZE) ?
> +			buf_size : FEATURE_KBD_REPORT_SIZE;
> +
> +	u8 *dmabuf __free(kfree) = kzalloc(alloc_size, GFP_KERNEL);
>  	if (!dmabuf)
>  		return -ENOMEM;
>  
> +	memcpy(dmabuf, buf, buf_size);
> +
>  	/* The device may be runtime suspended, e.g. for a deferred backlight change */
>  	ret = hid_hw_power(hdev, PM_HINT_FULLON);
>  	if (ret < 0)
> @@ -598,8 +612,8 @@ static int asus_kbd_set_report(struct hid_device *hdev, const u8 *buf, size_t bu
>  	 * The report ID should be set from the incoming buffer due to LED and key
>  	 * interfaces having different pages
>  	 */
> -	ret = hid_hw_raw_request(hdev, buf[0], dmabuf, buf_size, HID_FEATURE_REPORT,
> -				 HID_REQ_SET_REPORT);
> +	ret = hid_hw_raw_request(hdev, buf[0], dmabuf, alloc_size, HID_FEATURE_REPORT,
> +				  HID_REQ_SET_REPORT);
>  	hid_hw_power(hdev, PM_HINT_NORMAL);
>  	return ret;
>  }

[Severity: High]
Should this test drvdata->quirks & QUIRK_KBD_REPORT_SIZE instead of the
constant macro QUIRK_KBD_REPORT_SIZE?  Because QUIRK_KBD_REPORT_SIZE is
BIT(16), the condition is always true, forcing every feature report sent
through asus_kbd_set_report() on all Asus devices (such as the 6-byte
asus_kbd_get_functions() and 16-byte asus_kbd_init() reports) to 64 bytes.

> @@ -718,6 +732,38 @@ static void asus_kbd_set_brightness(struct hid_device *hdev, u8 brightness)
>  		hid_err(hdev, "Asus failed to set keyboard backlight: %d\n", ret);
>  }
>  
> +static void asus_kbd_set_brightness_cdev(struct led_classdev *led_cdev,
> +				    enum led_brightness brightness)
> +{
> +	struct asus_drvdata *drvdata = container_of(led_cdev, struct asus_drvdata, kbd_led_cdev);
> +	u8 level = (u8)brightness;
> +
> +	if (level > EC_KBD_MAX_BACKLIGHT)
> +		level = EC_KBD_MAX_BACKLIGHT;
> +
> +	asus_kbd_set_brightness(drvdata->hdev, level);
> +	drvdata->kbd_backlight_brightness = level;
> +}

[Severity: High]
Can asus_kbd_set_brightness_cdev() sleep in atomic context?

asus_kbd_set_brightness() calls asus_kbd_set_report(), which sleeps in
kzalloc(..., GFP_KERNEL), hid_hw_power() and hid_hw_raw_request().
However, asus_kbd_set_brightness_cdev() is wired to the non-sleeping
led_classdev.brightness_set callback (which the LED core invokes from
atomic contexts such as the software blink timer and LED triggers) and is
also called directly from asus_event() -> asus_kbd_illum_toggle(), which
runs in USB interrupt URB completion context on USB HID keyboards when
asus-wmi is absent.

Using brightness_set_blocking for kbd_led_cdev (or deferring the update
through drvdata->worker like asus_kbd_backlight_set()) would avoid
sleeping in atomic context.

> +
> +static enum led_brightness asus_kbd_get_brightness_cdev(struct led_classdev *led_cdev)
> +{
> +	struct asus_drvdata *drvdata = container_of(led_cdev, struct asus_drvdata, kbd_led_cdev);
> +
> +	return (enum led_brightness)drvdata->kbd_backlight_brightness;
> +}
> +
> +static void asus_kbd_illum_toggle(struct asus_drvdata *drvdata)
> +{
> +	if (!drvdata->kbd_led_cdev.brightness_set)
> +		return;
> +
> +	u8 current_level = drvdata->kbd_backlight_brightness;
> +	u8 max_level = EC_KBD_MAX_BACKLIGHT;
> +	u8 next_level = (current_level + 1) % (max_level + 1);
> +
> +	asus_kbd_set_brightness_cdev(&drvdata->kbd_led_cdev, next_level);
> +}

[Severity: Medium]
Can concurrent calls to asus_kbd_set_brightness_cdev() race on
drvdata->kbd_backlight_brightness?

CPU0 (sysfs brightness_store)     CPU1 (KEY_KBDILLUMTOGGLE)
asus_kbd_set_brightness_cdev(2)
  asus_kbd_set_brightness(2)
                                  asus_kbd_illum_toggle()
                                    current_level = 0
                                    asus_kbd_set_brightness_cdev(1)
                                      asus_kbd_set_brightness(1)
                                      drvdata->kbd_backlight_brightness = 1
  drvdata->kbd_backlight_brightness = 2
                                    <-- out of sync with hardware

sysfs writes hold led_cdev->led_access, asus_event() holds
hdev->driver_input_lock, and asus_resume() holds neither.  Because
drvdata->kbd_backlight_brightness is updated only after the blocking HID
transfer in asus_kbd_set_brightness(), overlapping updates read stale
levels and can finish out of order.

[ ... ]

> @@ -1012,10 +1058,28 @@ static int asus_kbd_register_leds(struct hid_device *hdev)
>  	drvdata->listener.brightness_set = asus_kbd_backlight_set;
>  	ret = asus_hid_register_listener(&drvdata->listener);
>  	if (ret < 0) {
> -		hid_err(hdev, "Unable to register kbd brightness listener: %d\n", ret);
> +		if (ret != -ENODEV) {
> +			hid_err(hdev, "Unable to register kbd brightness listener: %d\n", ret);
> +		}
>  		drvdata->listener.brightness_set = NULL;
>  	}
>  
> +	// Non-x86 architectures do not have WMI, just register a led
> +	// device directly
> +	if (ret == -ENODEV) {
> +		drvdata->kbd_led_cdev.name = "asus::kbd_backlight";
> +		drvdata->kbd_led_cdev.brightness_set = asus_kbd_set_brightness_cdev;
> +		drvdata->kbd_led_cdev.brightness_get = asus_kbd_get_brightness_cdev;
> +		drvdata->kbd_led_cdev.max_brightness = EC_KBD_MAX_BACKLIGHT;
> +		ret = led_classdev_register(&hdev->dev, &drvdata->kbd_led_cdev);
> +		if (ret < 0) {
> +			hid_err(hdev, "Unable to register kbd brightness led: %d\n", ret);
> +			drvdata->kbd_led_cdev.brightness_set = NULL;
> +		}
> +
> +		asus_kbd_set_brightness_cdev(&drvdata->kbd_led_cdev, 1);
> +	}
> +
>  	return ret;
>  }

[Severity: Medium]
Does this still call asus_kbd_set_brightness_cdev() when
led_classdev_register() fails?  The error branch logs the failure and
clears brightness_set, but falls through without returning.

Also, when registration succeeds, led_classdev_register() calls
led_update_brightness() -> asus_kbd_get_brightness_cdev() while
drvdata->kbd_backlight_brightness is still 0, so calling
asus_kbd_set_brightness_cdev(&drvdata->kbd_led_cdev, 1) afterwards sets
the hardware and drvdata->kbd_backlight_brightness to 1 while leaving
drvdata->kbd_led_cdev.brightness at 0.

[ ... ]

> @@ -1676,6 +1746,9 @@ static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
>  	if (drvdata->xgm_led)
>  		devm_led_classdev_unregister(&hdev->dev, &drvdata->xgm_led->cdev);
>  
> +	if (drvdata->kbd_led_cdev.brightness_set)
> +		led_classdev_unregister(&drvdata->kbd_led_cdev);
> +
>  	asus_worker_stop(drvdata->worker);
>  	hid_hw_stop(hdev);
>  	return ret;
> @@ -1691,6 +1764,9 @@ static void asus_remove(struct hid_device *hdev)
>  	if (drvdata->xgm_led)
>  		devm_led_classdev_unregister(&hdev->dev, &drvdata->xgm_led->cdev);
>  
> +	if (drvdata->kbd_led_cdev.brightness_set)
> +		led_classdev_unregister(&drvdata->kbd_led_cdev);
> +
>  	asus_worker_stop(drvdata->worker);
>  	hid_hw_stop(hdev);
>  }

[Severity: Medium]
Can an incoming KEY_KBDILLUMTOGGLE event use drvdata->kbd_led_cdev after
led_classdev_unregister()?

CPU0                              CPU1
asus_remove()
  led_classdev_unregister(&drvdata->kbd_led_cdev)
                                  asus_event()
                                    asus_kbd_illum_toggle()
                                      if (!drvdata->kbd_led_cdev.brightness_set)
                                      asus_kbd_set_brightness_cdev()
                                        <-- uses unregistered LED and turns backlight back on
  hid_hw_stop(hdev)

led_classdev_unregister() turns the LED off and unregisters the device,
but does not clear drvdata->kbd_led_cdev.brightness_set, while HID input
events remain active until hid_hw_stop().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201185118.342672-1-jaytersen@gmail.com?part=1

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

* Re: [PATCH v2] HID: asus: support the Zenbook A14 (UX3407RA) keyboard backlight
  2026-12-01 19:02 ` sashiko-bot
@ 2026-12-01 20:57   ` Jesper B. Christensen
  0 siblings, 0 replies; 3+ messages in thread
From: Jesper B. Christensen @ 2026-12-01 20:57 UTC (permalink / raw)
  To: sashiko-reviews; +Cc: linux-input, dmitry.torokhov

Please do not merge this, its crap. I'm working on some fixes.

/Jesper

On 12/1/26 8:02 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 6 potential
> issues to consider:
> - [High] HID: asus: always-true QUIRK_KBD_REPORT_SIZE check in
>    asus_kbd_set_report()
> - [High] HID: asus: sleep in atomic context in
>    asus_kbd_set_brightness_cdev()
> - [Medium] HID: asus: unconsumed KEY_KBDILLUMTOGGLE and LED desync in
>    asus_event()
> - [Medium] HID: asus: error fallthrough and LED init order in
>    asus_kbd_register_leds()
> - [Medium] HID: asus: teardown race on kbd_led_cdev in asus_remove() and
>    asus_probe()
> - [Medium] HID: asus: unsynchronized kbd_backlight_brightness updates
>    across LED paths
> --
>
> commit 88e8e96de263e1806c05ad0eaf08fe87ba34473c
> Author: Jesper B. Christensen <jaytersen@gmail.com>
>
> HID: asus: support the Zenbook A14 (UX3407RA) keyboard backlight
>
> This commit adds HID device ID 0B05:0220 for the ASUS Zenbook A14
> (UX3407RA) I2C keyboard, introduces QUIRK_KBD_REPORT_SIZE and
> QUIRK_KBD_OOBE, and registers a direct LED class device for the keyboard
> backlight when asus-wmi is unavailable.
>
>> diff --git a/drivers/hid/hid-asus.c b/drivers/hid/hid-asus.c
>> index 4f5749c9b2dbb..1e940a495c41c 100644
>> --- a/drivers/hid/hid-asus.c
>> +++ b/drivers/hid/hid-asus.c
> [ ... ]
>
>> @@ -477,6 +482,7 @@ static int asus_event(struct hid_device *hdev, struct hid_field *field,
>>   		case KEY_KBDILLUMDOWN:
>>   			return !asus_hid_event(ASUS_EV_BRTDOWN);
>>   		case KEY_KBDILLUMTOGGLE:
>> +			asus_kbd_illum_toggle(drvdata);
>>   			return !asus_hid_event(ASUS_EV_BRTTOGGLE);
>>   		case KEY_FN_ESC:
>>   			if (drvdata->quirks & QUIRK_HID_FN_LOCK) {
> [Severity: Medium]
> Does KEY_KBDILLUMTOGGLE both toggle the backlight in the kernel and get
> forwarded to userspace when asus-wmi is absent?
>
> When asus-wmi is unavailable, asus_hid_event(ASUS_EV_BRTTOGGLE) returns
> -ENODEV, so !asus_hid_event() returns 0 and hid_process_event() reports
> KEY_KBDILLUMTOGGLE to userspace after asus_kbd_illum_toggle() has already
> advanced the hardware brightness.  In addition, asus_kbd_illum_toggle()
> does not update drvdata->kbd_led_cdev.brightness or notify userspace via
> led_classdev_notify_brightness_hw_changed(), so a userspace power daemon
> handling the key event will toggle sysfs a second time from a stale
> brightness value.
>
>> @@ -585,10 +591,18 @@ static int asus_raw_event(struct hid_device *hdev,
>>   static int asus_kbd_set_report(struct hid_device *hdev, const u8 *buf, size_t buf_size)
>>   {
>>   	int ret;
>> -	u8 *dmabuf __free(kfree) = kmemdup(buf, buf_size, GFP_KERNEL);
>> +	size_t alloc_size = buf_size;
>> +
>> +	if (QUIRK_KBD_REPORT_SIZE)
>> +		alloc_size = (buf_size > FEATURE_KBD_REPORT_SIZE) ?
>> +			buf_size : FEATURE_KBD_REPORT_SIZE;
>> +
>> +	u8 *dmabuf __free(kfree) = kzalloc(alloc_size, GFP_KERNEL);
>>   	if (!dmabuf)
>>   		return -ENOMEM;
>>   
>> +	memcpy(dmabuf, buf, buf_size);
>> +
>>   	/* The device may be runtime suspended, e.g. for a deferred backlight change */
>>   	ret = hid_hw_power(hdev, PM_HINT_FULLON);
>>   	if (ret < 0)
>> @@ -598,8 +612,8 @@ static int asus_kbd_set_report(struct hid_device *hdev, const u8 *buf, size_t bu
>>   	 * The report ID should be set from the incoming buffer due to LED and key
>>   	 * interfaces having different pages
>>   	 */
>> -	ret = hid_hw_raw_request(hdev, buf[0], dmabuf, buf_size, HID_FEATURE_REPORT,
>> -				 HID_REQ_SET_REPORT);
>> +	ret = hid_hw_raw_request(hdev, buf[0], dmabuf, alloc_size, HID_FEATURE_REPORT,
>> +				  HID_REQ_SET_REPORT);
>>   	hid_hw_power(hdev, PM_HINT_NORMAL);
>>   	return ret;
>>   }
> [Severity: High]
> Should this test drvdata->quirks & QUIRK_KBD_REPORT_SIZE instead of the
> constant macro QUIRK_KBD_REPORT_SIZE?  Because QUIRK_KBD_REPORT_SIZE is
> BIT(16), the condition is always true, forcing every feature report sent
> through asus_kbd_set_report() on all Asus devices (such as the 6-byte
> asus_kbd_get_functions() and 16-byte asus_kbd_init() reports) to 64 bytes.
>
>> @@ -718,6 +732,38 @@ static void asus_kbd_set_brightness(struct hid_device *hdev, u8 brightness)
>>   		hid_err(hdev, "Asus failed to set keyboard backlight: %d\n", ret);
>>   }
>>   
>> +static void asus_kbd_set_brightness_cdev(struct led_classdev *led_cdev,
>> +				    enum led_brightness brightness)
>> +{
>> +	struct asus_drvdata *drvdata = container_of(led_cdev, struct asus_drvdata, kbd_led_cdev);
>> +	u8 level = (u8)brightness;
>> +
>> +	if (level > EC_KBD_MAX_BACKLIGHT)
>> +		level = EC_KBD_MAX_BACKLIGHT;
>> +
>> +	asus_kbd_set_brightness(drvdata->hdev, level);
>> +	drvdata->kbd_backlight_brightness = level;
>> +}
> [Severity: High]
> Can asus_kbd_set_brightness_cdev() sleep in atomic context?
>
> asus_kbd_set_brightness() calls asus_kbd_set_report(), which sleeps in
> kzalloc(..., GFP_KERNEL), hid_hw_power() and hid_hw_raw_request().
> However, asus_kbd_set_brightness_cdev() is wired to the non-sleeping
> led_classdev.brightness_set callback (which the LED core invokes from
> atomic contexts such as the software blink timer and LED triggers) and is
> also called directly from asus_event() -> asus_kbd_illum_toggle(), which
> runs in USB interrupt URB completion context on USB HID keyboards when
> asus-wmi is absent.
>
> Using brightness_set_blocking for kbd_led_cdev (or deferring the update
> through drvdata->worker like asus_kbd_backlight_set()) would avoid
> sleeping in atomic context.
>
>> +
>> +static enum led_brightness asus_kbd_get_brightness_cdev(struct led_classdev *led_cdev)
>> +{
>> +	struct asus_drvdata *drvdata = container_of(led_cdev, struct asus_drvdata, kbd_led_cdev);
>> +
>> +	return (enum led_brightness)drvdata->kbd_backlight_brightness;
>> +}
>> +
>> +static void asus_kbd_illum_toggle(struct asus_drvdata *drvdata)
>> +{
>> +	if (!drvdata->kbd_led_cdev.brightness_set)
>> +		return;
>> +
>> +	u8 current_level = drvdata->kbd_backlight_brightness;
>> +	u8 max_level = EC_KBD_MAX_BACKLIGHT;
>> +	u8 next_level = (current_level + 1) % (max_level + 1);
>> +
>> +	asus_kbd_set_brightness_cdev(&drvdata->kbd_led_cdev, next_level);
>> +}
> [Severity: Medium]
> Can concurrent calls to asus_kbd_set_brightness_cdev() race on
> drvdata->kbd_backlight_brightness?
>
> CPU0 (sysfs brightness_store)     CPU1 (KEY_KBDILLUMTOGGLE)
> asus_kbd_set_brightness_cdev(2)
>    asus_kbd_set_brightness(2)
>                                    asus_kbd_illum_toggle()
>                                      current_level = 0
>                                      asus_kbd_set_brightness_cdev(1)
>                                        asus_kbd_set_brightness(1)
>                                        drvdata->kbd_backlight_brightness = 1
>    drvdata->kbd_backlight_brightness = 2
>                                      <-- out of sync with hardware
>
> sysfs writes hold led_cdev->led_access, asus_event() holds
> hdev->driver_input_lock, and asus_resume() holds neither.  Because
> drvdata->kbd_backlight_brightness is updated only after the blocking HID
> transfer in asus_kbd_set_brightness(), overlapping updates read stale
> levels and can finish out of order.
>
> [ ... ]
>
>> @@ -1012,10 +1058,28 @@ static int asus_kbd_register_leds(struct hid_device *hdev)
>>   	drvdata->listener.brightness_set = asus_kbd_backlight_set;
>>   	ret = asus_hid_register_listener(&drvdata->listener);
>>   	if (ret < 0) {
>> -		hid_err(hdev, "Unable to register kbd brightness listener: %d\n", ret);
>> +		if (ret != -ENODEV) {
>> +			hid_err(hdev, "Unable to register kbd brightness listener: %d\n", ret);
>> +		}
>>   		drvdata->listener.brightness_set = NULL;
>>   	}
>>   
>> +	// Non-x86 architectures do not have WMI, just register a led
>> +	// device directly
>> +	if (ret == -ENODEV) {
>> +		drvdata->kbd_led_cdev.name = "asus::kbd_backlight";
>> +		drvdata->kbd_led_cdev.brightness_set = asus_kbd_set_brightness_cdev;
>> +		drvdata->kbd_led_cdev.brightness_get = asus_kbd_get_brightness_cdev;
>> +		drvdata->kbd_led_cdev.max_brightness = EC_KBD_MAX_BACKLIGHT;
>> +		ret = led_classdev_register(&hdev->dev, &drvdata->kbd_led_cdev);
>> +		if (ret < 0) {
>> +			hid_err(hdev, "Unable to register kbd brightness led: %d\n", ret);
>> +			drvdata->kbd_led_cdev.brightness_set = NULL;
>> +		}
>> +
>> +		asus_kbd_set_brightness_cdev(&drvdata->kbd_led_cdev, 1);
>> +	}
>> +
>>   	return ret;
>>   }
> [Severity: Medium]
> Does this still call asus_kbd_set_brightness_cdev() when
> led_classdev_register() fails?  The error branch logs the failure and
> clears brightness_set, but falls through without returning.
>
> Also, when registration succeeds, led_classdev_register() calls
> led_update_brightness() -> asus_kbd_get_brightness_cdev() while
> drvdata->kbd_backlight_brightness is still 0, so calling
> asus_kbd_set_brightness_cdev(&drvdata->kbd_led_cdev, 1) afterwards sets
> the hardware and drvdata->kbd_backlight_brightness to 1 while leaving
> drvdata->kbd_led_cdev.brightness at 0.
>
> [ ... ]
>
>> @@ -1676,6 +1746,9 @@ static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
>>   	if (drvdata->xgm_led)
>>   		devm_led_classdev_unregister(&hdev->dev, &drvdata->xgm_led->cdev);
>>   
>> +	if (drvdata->kbd_led_cdev.brightness_set)
>> +		led_classdev_unregister(&drvdata->kbd_led_cdev);
>> +
>>   	asus_worker_stop(drvdata->worker);
>>   	hid_hw_stop(hdev);
>>   	return ret;
>> @@ -1691,6 +1764,9 @@ static void asus_remove(struct hid_device *hdev)
>>   	if (drvdata->xgm_led)
>>   		devm_led_classdev_unregister(&hdev->dev, &drvdata->xgm_led->cdev);
>>   
>> +	if (drvdata->kbd_led_cdev.brightness_set)
>> +		led_classdev_unregister(&drvdata->kbd_led_cdev);
>> +
>>   	asus_worker_stop(drvdata->worker);
>>   	hid_hw_stop(hdev);
>>   }
> [Severity: Medium]
> Can an incoming KEY_KBDILLUMTOGGLE event use drvdata->kbd_led_cdev after
> led_classdev_unregister()?
>
> CPU0                              CPU1
> asus_remove()
>    led_classdev_unregister(&drvdata->kbd_led_cdev)
>                                    asus_event()
>                                      asus_kbd_illum_toggle()
>                                        if (!drvdata->kbd_led_cdev.brightness_set)
>                                        asus_kbd_set_brightness_cdev()
>                                          <-- uses unregistered LED and turns backlight back on
>    hid_hw_stop(hdev)
>
> led_classdev_unregister() turns the LED off and unregisters the device,
> but does not clear drvdata->kbd_led_cdev.brightness_set, while HID input
> events remain active until hid_hw_stop().
>

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

end of thread, other threads:[~2026-12-01 20:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 18:50 [PATCH v2] HID: asus: support the Zenbook A14 (UX3407RA) keyboard backlight Jesper B. Christensen
2026-12-01 19:02 ` sashiko-bot
2026-12-01 20:57   ` Jesper B. Christensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox