Skip to contentSkip to conversation

platform/x86: dell-laptop: rework kbd_led_level_{get,set}()

PATCH In review Yury Norov 2 participants 6 messages Sep 25 – Nov 18

Subsystems (2)
lore
Share & tools
lore

Yury Norov

author
#0 lore raw

cover platform/x86: dell-laptop: rework kbd_led_level_{get,set}()

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(-)
Signature · 1 line
2.53.0
author
#1 lore raw

PATCH 1/2 platform/x86: dell-laptop: Use fns() in kbd_led_level_set()

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.

---
 drivers/platform/x86/dell/dell-laptop.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/platform/x86/dell/dell-laptop.c +4 -7
index 89e85c7f7132..03d3bb70b0d1 100644
@@ -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;
Signature · 1 line
2.53.0
author
#3 lore raw

PATCH 2/2 platform/x86: dell-laptop: Fix token-based keyboard brightness readback

In reply to 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.

---
 drivers/platform/x86/dell/dell-laptop.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/platform/x86/dell/dell-laptop.c +3 -7
index 03d3bb70b0d1..55fd48222e5e 100644
@@ -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");
Signature · 1 line
2.53.0
author
#5 lore raw
In reply to Yury Norov

Ping?

Quoted 13 lines · 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(-)
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] platform/x86: dell-laptop: Use fns() in kbd_led_level_set()

In reply to Yury Norov
Quoted 32 lines · 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.

---
 drivers/platform/x86/dell/dell-laptop.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/platform/x86/dell/dell-laptop.c +2 -1
index 89e85c7f7132..03d3bb70b0d1 100644
@@ -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;

drivers/platform/x86/dell/dell-laptop.c static int kbd_led_level_set(struct led_classdev *led_cdev, · line 2056

Quoted 10 lines
drivers/platform/x86/dell/dell-laptop.c +2 -6
 		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.

Quoted 6 lines
drivers/platform/x86/dell/dell-laptop.c +0 -0
 	} else {
 		pr_warn("Keyboard brightness level control not supported\n");
 		ret = -ENXIO;
Signature · 1 line
2.53.0
maintainer
#4 lore raw

Re: [PATCH 2/2] platform/x86: dell-laptop: Fix token-based keyboard brightness readback

In reply to Yury Norov
Quoted 3 lines · 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.

Quoted 2 lines

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.

Quoted 39 lines

Return zero directly when ret == 0: token bit 0 has no supported tokens below it, so its brightness ordinal is zero.

---
 drivers/platform/x86/dell/dell-laptop.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/platform/x86/dell/dell-laptop.c +3 -7
index 03d3bb70b0d1..55fd48222e5e 100644
@@ -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");
Signature · 1 line
2.53.0