mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] platform/x86: dell-laptop: rework kbd_led_level_{get,set}()
@ 2026-09-25 19:32 Yury Norov
  2026-09-25 19:32 ` [PATCH 1/2] platform/x86: dell-laptop: Use fns() in kbd_led_level_set() Yury Norov
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Yury Norov @ 2026-09-25 19:32 UTC (permalink / raw)
  To: Matthew Garrett, Pali Rohár, Hans de Goede,
	Ilpo Järvinen, platform-driver-x86, open list
  Cc: Yury Norov, Yury Norov

Simplify both functions by replacing open-coded loops and fix
brightness readback for sparse token masks.

Yury Norov (2):
  platform/x86: dell-laptop: Use fns() in kbd_led_level_set()
  platform/x86: dell-laptop: Fix token-based keyboard brightness
    readback

 drivers/platform/x86/dell/dell-laptop.c | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)

-- 
2.53.0

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

* [PATCH 1/2] platform/x86: dell-laptop: Use fns() in kbd_led_level_set()
  2026-09-25 19:32 [PATCH 0/2] platform/x86: dell-laptop: rework kbd_led_level_{get,set}() Yury Norov
@ 2026-09-25 19:32 ` Yury Norov
  2026-11-18 22:20   ` Pali Rohár
  2026-09-25 19:32 ` [PATCH 2/2] platform/x86: dell-laptop: Fix token-based keyboard brightness readback Yury Norov
  2026-11-18 21:17 ` [PATCH 0/2] platform/x86: dell-laptop: rework kbd_led_level_{get,set}() Yury Norov
  2 siblings, 1 reply; 6+ messages in thread
From: Yury Norov @ 2026-09-25 19:32 UTC (permalink / raw)
  To: Matthew Garrett, Pali Rohár, Hans de Goede,
	Ilpo Järvinen, platform-driver-x86, open list
  Cc: Yury Norov, Yury Norov

Replace the loop clearing lower set bits with fns() to select the
supported token corresponding to the requested brightness. Preserve the
zero return when the requested index is out of range.

Signed-off-by: Yury Norov <ynorov@nvidia.com>
---
 drivers/platform/x86/dell/dell-laptop.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/drivers/platform/x86/dell/dell-laptop.c b/drivers/platform/x86/dell/dell-laptop.c
index 89e85c7f7132..03d3bb70b0d1 100644
--- a/drivers/platform/x86/dell/dell-laptop.c
+++ b/drivers/platform/x86/dell/dell-laptop.c
@@ -17,6 +17,7 @@
 #include <linux/init.h>
 #include <linux/platform_device.h>
 #include <linux/backlight.h>
+#include <linux/bitops.h>
 #include <linux/err.h>
 #include <linux/dmi.h>
 #include <linux/io.h>
@@ -2036,7 +2037,7 @@ static int kbd_led_level_set(struct led_classdev *led_cdev,
 	enum led_brightness new_value = value;
 	struct kbd_state state;
 	struct kbd_state new_state;
-	u16 num;
+	unsigned int bit;
 	int ret;
 
 	mutex_lock(&kbd_led_mutex);
@@ -2051,12 +2052,8 @@ static int kbd_led_level_set(struct led_classdev *led_cdev,
 			goto out;
 		ret = kbd_set_state_safe(&new_state, &state);
 	} else if (kbd_get_valid_token_counts()) {
-		for (num = kbd_token_bits; num != 0 && value > 0; --value)
-			num &= num - 1; /* clear the first bit set */
-		if (num == 0)
-			ret = 0;
-		else
-			ret = kbd_set_token_bit(ffs(num) - 1);
+		bit = fns(kbd_token_bits, value);
+		ret = bit == BITS_PER_LONG ? 0 : kbd_set_token_bit(bit);
 	} else {
 		pr_warn("Keyboard brightness level control not supported\n");
 		ret = -ENXIO;
-- 
2.53.0


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

* [PATCH 2/2] platform/x86: dell-laptop: Fix token-based keyboard brightness readback
  2026-09-25 19:32 [PATCH 0/2] platform/x86: dell-laptop: rework kbd_led_level_{get,set}() Yury Norov
  2026-09-25 19:32 ` [PATCH 1/2] platform/x86: dell-laptop: Use fns() in kbd_led_level_set() Yury Norov
@ 2026-09-25 19:32 ` Yury Norov
  2026-11-18 22:30   ` Pali Rohár
  2026-11-18 21:17 ` [PATCH 0/2] platform/x86: dell-laptop: rework kbd_led_level_{get,set}() Yury Norov
  2 siblings, 1 reply; 6+ messages in thread
From: Yury Norov @ 2026-09-25 19:32 UTC (permalink / raw)
  To: Matthew Garrett, Pali Rohár, Hans de Goede,
	Ilpo Järvinen, platform-driver-x86, open list
  Cc: Yury Norov, Yury Norov

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.

Count the supported token bits below the active token with hweight16()
to recover its zero-based brightness ordinal.

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@nvidia.com>
---
 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

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

* Re: [PATCH 0/2] platform/x86: dell-laptop: rework kbd_led_level_{get,set}()
  2026-09-25 19:32 [PATCH 0/2] platform/x86: dell-laptop: rework kbd_led_level_{get,set}() Yury Norov
  2026-09-25 19:32 ` [PATCH 1/2] platform/x86: dell-laptop: Use fns() in kbd_led_level_set() Yury Norov
  2026-09-25 19:32 ` [PATCH 2/2] platform/x86: dell-laptop: Fix token-based keyboard brightness readback Yury Norov
@ 2026-11-18 21:17 ` Yury Norov
  2 siblings, 0 replies; 6+ messages in thread
From: Yury Norov @ 2026-11-18 21:17 UTC (permalink / raw)
  To: Matthew Garrett, Pali Rohár, Hans de Goede,
	Ilpo Järvinen, platform-driver-x86, open list
  Cc: Yury Norov

Ping?

On Fri, Sep 25, 2026 at 03:32:37PM -0400, Yury Norov wrote:
> Simplify both functions by replacing open-coded loops and fix
> brightness readback for sparse token masks.
> 
> Yury Norov (2):
>   platform/x86: dell-laptop: Use fns() in kbd_led_level_set()
>   platform/x86: dell-laptop: Fix token-based keyboard brightness
>     readback
> 
>  drivers/platform/x86/dell/dell-laptop.c | 21 +++++++--------------
>  1 file changed, 7 insertions(+), 14 deletions(-)
> 
> -- 
> 2.53.0

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

* Re: [PATCH 1/2] platform/x86: dell-laptop: Use fns() in kbd_led_level_set()
  2026-09-25 19:32 ` [PATCH 1/2] platform/x86: dell-laptop: Use fns() in kbd_led_level_set() Yury Norov
@ 2026-11-18 22:20   ` Pali Rohár
  0 siblings, 0 replies; 6+ messages in thread
From: Pali Rohár @ 2026-11-18 22:20 UTC (permalink / raw)
  To: Yury Norov
  Cc: Matthew Garrett, Hans de Goede, Ilpo Järvinen,
	platform-driver-x86, open list, Yury Norov

On Friday 25 September 2026 15:32:38 Yury Norov wrote:
> Replace the loop clearing lower set bits with fns() to select the
> supported token corresponding to the requested brightness. Preserve the
> zero return when the requested index is out of range.
> 
> Signed-off-by: Yury Norov <ynorov@nvidia.com>
> ---
>  drivers/platform/x86/dell/dell-laptop.c | 11 ++++-------
>  1 file changed, 4 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/platform/x86/dell/dell-laptop.c b/drivers/platform/x86/dell/dell-laptop.c
> index 89e85c7f7132..03d3bb70b0d1 100644
> --- a/drivers/platform/x86/dell/dell-laptop.c
> +++ b/drivers/platform/x86/dell/dell-laptop.c
> @@ -17,6 +17,7 @@
>  #include <linux/init.h>
>  #include <linux/platform_device.h>
>  #include <linux/backlight.h>
> +#include <linux/bitops.h>
>  #include <linux/err.h>
>  #include <linux/dmi.h>
>  #include <linux/io.h>
> @@ -2036,7 +2037,7 @@ static int kbd_led_level_set(struct led_classdev *led_cdev,
>  	enum led_brightness new_value = value;
>  	struct kbd_state state;
>  	struct kbd_state new_state;
> -	u16 num;
> +	unsigned int bit;
>  	int ret;
>  
>  	mutex_lock(&kbd_led_mutex);
> @@ -2051,12 +2052,8 @@ static int kbd_led_level_set(struct led_classdev *led_cdev,
>  			goto out;
>  		ret = kbd_set_state_safe(&new_state, &state);
>  	} else if (kbd_get_valid_token_counts()) {
> -		for (num = kbd_token_bits; num != 0 && value > 0; --value)
> -			num &= num - 1; /* clear the first bit set */
> -		if (num == 0)
> -			ret = 0;
> -		else
> -			ret = kbd_set_token_bit(ffs(num) - 1);
> +		bit = fns(kbd_token_bits, value);
> +		ret = bit == BITS_PER_LONG ? 0 : kbd_set_token_bit(bit);

Maybe slightly better readable diff:

-		for (num = kbd_token_bits; num != 0 && value > 0; --value)
-			num &= num - 1; /* clear the first bit set */
+		bit = fns(kbd_token_bits, value);
-		if (num == 0)
+		if (bit == BITS_PER_LONG)
 			ret = 0;
 		else
-			ret = kbd_set_token_bit(ffs(num) - 1);
+			ret = kbd_set_token_bit(bit);

Otherwise looks good.

Reviewed-by: Pali Rohár <pali@kernel.org>

>  	} else {
>  		pr_warn("Keyboard brightness level control not supported\n");
>  		ret = -ENXIO;
> -- 
> 2.53.0
> 

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

* Re: [PATCH 2/2] platform/x86: dell-laptop: Fix token-based keyboard brightness readback
  2026-09-25 19:32 ` [PATCH 2/2] platform/x86: dell-laptop: Fix token-based keyboard brightness readback Yury Norov
@ 2026-11-18 22:30   ` Pali Rohár
  0 siblings, 0 replies; 6+ messages in thread
From: Pali Rohár @ 2026-11-18 22:30 UTC (permalink / raw)
  To: Yury Norov
  Cc: Matthew Garrett, Hans de Goede, Ilpo Järvinen,
	platform-driver-x86, open list, Yury Norov

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@nvidia.com>
> ---
>  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

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

end of thread, other threads:[~2026-11-18 22:30 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-25 19:32 [PATCH 0/2] platform/x86: dell-laptop: rework kbd_led_level_{get,set}() Yury Norov
2026-09-25 19:32 ` [PATCH 1/2] platform/x86: dell-laptop: Use fns() in kbd_led_level_set() Yury Norov
2026-11-18 22:20   ` Pali Rohár
2026-09-25 19:32 ` [PATCH 2/2] platform/x86: dell-laptop: Fix token-based keyboard brightness readback Yury Norov
2026-11-18 22:30   ` Pali Rohár
2026-11-18 21:17 ` [PATCH 0/2] platform/x86: dell-laptop: rework kbd_led_level_{get,set}() Yury Norov

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®