Re: [PATCH 2/2] platform/x86: dell-laptop: Fix token-based keyboard brightness readback
From: Pali Rohár
Date: Wed Nov 18 2026 - 17:30:22 EST
On Friday 25 September 2026 15:32:39 Yury Norov wrote:
> kbd_get_first_active_token_bit() returns an index into kbd_tokens, while
> LED brightness is an ordinal among the supported tokens. These differ
> when supported token bits are not consecutive starting at bit 0.
When these can differ? I think that only in the case when token
KBD_LED_OFF_TOKEN is not supported by the firmware / BIOS.
But when the token KBD_LED_OFF_TOKEN is not supported, it means that the
brightness level 0 is not supported, keyboard backlight cannot be turned
off and so returned value cannot be 0, it has to start at some higher level.
> Count the supported token bits below the active token with hweight16()
> to recover its zero-based brightness ordinal.
So I think that this change will start reporting incorrect brightness
level and the original code was correct.
> Return zero directly when ret == 0: token bit 0 has no supported tokens
> below it, so its brightness ordinal is zero.
>
> Signed-off-by: Yury Norov <ynorov@xxxxxxxxxx>
> ---
> drivers/platform/x86/dell/dell-laptop.c | 10 +++-------
> 1 file changed, 3 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/platform/x86/dell/dell-laptop.c b/drivers/platform/x86/dell/dell-laptop.c
> index 03d3bb70b0d1..55fd48222e5e 100644
> --- a/drivers/platform/x86/dell/dell-laptop.c
> +++ b/drivers/platform/x86/dell/dell-laptop.c
> @@ -2003,7 +2003,6 @@ static const struct attribute_group *kbd_led_groups[] = {
> static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
> {
> int ret;
> - u16 num;
> struct kbd_state state;
>
> if (kbd_get_max_level()) {
> @@ -2018,13 +2017,10 @@ static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
>
> if (kbd_get_valid_token_counts()) {
> ret = kbd_get_first_active_token_bit();
> - if (ret < 0)
> - return 0;
> - for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
> - num &= num - 1; /* clear the first bit set */
> - if (num == 0)
> + if (ret <= 0)
> return 0;
> - return ffs(num) - 1;
> + /* Brightness is the active token's rank among supported tokens. */
> + return hweight16(kbd_token_bits & GENMASK(ret - 1, 0));
> }
>
> pr_warn("Keyboard brightness level control not supported\n");
> --
> 2.53.0