Linux Input/HID development
 help / color / mirror / Atom feed
From: "Jesper B. Christensen" <jaytersen@gmail.com>
To: sashiko-reviews@lists.linux.dev
Cc: linux-input@vger.kernel.org, dmitry.torokhov@gmail.com
Subject: Re: [PATCH v2] HID: asus: support the Zenbook A14 (UX3407RA) keyboard backlight
Date: Tue, 1 Dec 2026 21:57:07 +0100	[thread overview]
Message-ID: <42627ca9-4df7-482f-87da-f0cd352ae281@gmail.com> (raw)
In-Reply-To: <sashiko-outbox-244006@kernel.org>

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().
>

      reply	other threads:[~2026-12-01 20:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 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=42627ca9-4df7-482f-87da-f0cd352ae281@gmail.com \
    --to=jaytersen@gmail.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=linux-input@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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