thinkpad_acpi: Add Auto mode support with dynamic max_brightness
Subsystems (3)
PATCH 1/1 thinkpad_acpi: Add Auto mode support with dynamic max_brightness
Dynamically detect keyboard backlight capabilities and set max_brightness correctly (2 for old models, 3 for new models with Auto mode).
--- drivers/platform/x86/lenovo/thinkpad_acpi.c | 33 ++++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-)
drivers/platform/x86/lenovo/thinkpad_acpi.c +29 -4
@@ -5043,6 +5043,9 @@ static struct ibm_struct video_driver_data = {static enum led_brightness kbdlight_brightness;static DEFINE_MUTEX (kbdlight_mutex); +/* Maximum level supported by hardware, will be updated in init */ +static int kbdlight_max_level =2 ; +static int kbdlight_set_level (int level) {int ret =0 ;
@@ -5050,6 +5053,10 @@ static int kbdlight_set_level(int level)if (!hkey_handle)return -ENXIO ; +/* Validate against detected max level */ +if (level <0 || level > kbdlight_max_level) +return -EINVAL ; +mutex_lock (&kbdlight_mutex);if (!acpi_evalf (hkey_handle,NULL ,"MLCS" ,"dd" , level))
@@ -5075,6 +5082,7 @@ static int kbdlight_get_level(void)if (status <0 )return status; +/* Status can be 0, 1, 2, or 3 (Auto) */ return status &0x3 ; }
@@ -5143,7 +5151,7 @@ static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)static struct tpacpi_led_classdev tpacpi_led_kbdlight = { .led_classdev = { .name ="tpacpi::kbd_backlight" , - .max_brightness =2 , + .max_brightness =2 ,/*Initial value, will be updated in init*/ .flags =LED_BRIGHT_HW_CHANGED , .brightness_set_blocking = &kbdlight_sysfs_set, .brightness_get = &kbdlight_sysfs_get,
@@ -5167,6 +5175,17 @@ static int __init kbdlight_init(struct ibm_init_struct *iibm) kbdlight_brightness =kbdlight_sysfs_get (NULL ); tp_features.kbdlight =1 ; +/* Detect hardware capabilities and set max_brightness */ +if (acpi_evalf (hkey_handle,NULL ,"MLCS" ,"dd" ,3 )) { +/* MLCS accepts level 3 - new ThinkPad with Auto mode */ + kbdlight_max_level =3 ; + tpacpi_led_kbdlight.led_classdev.max_brightness =3 ; + }else { +/* MLCS rejects level 3 - old ThinkPad */ + kbdlight_max_level =2 ; + tpacpi_led_kbdlight.led_classdev.max_brightness =2 ; + } + rc =led_classdev_register (&tpacpi_pdev->dev, &tpacpi_led_kbdlight.led_classdev);if (rc <0 ) {
@@ -5201,6 +5220,7 @@ static int kbdlight_set_level_and_update(int level)static int kbdlight_read (struct seq_file *m) {int level; +int i;if (!tp_features.kbdlight) {seq_printf (m,"status:\t\tnot supported\n" );
@@ -5210,9 +5230,13 @@ static int kbdlight_read(struct seq_file *m)seq_printf (m,"status:\t\terror %d\n" , level); else seq_printf(m, "status:\t\t%d\n", level); -seq_printf (m,"commands:\t0, 1, 2\n" ); - } +/* Show available commands based on hardware */ +seq_puts (m,"commands:\t0" ); +for (i =1 ; i <= tpacpi_led_kbdlight.led_classdev.max_brightness; i++) +seq_printf (m,", %d" , i); +seq_puts (m,"\n" ); + }return 0 ; }
@@ -5230,7 +5254,8 @@ static int kbdlight_write(char *buf)return res; } -if (level >=3 || level <0 ) +/* Validate against max level */ +if (level <0 || level > tpacpi_led_kbdlight.led_classdev.max_brightness)return -EINVAL ;return kbdlight_set_level_and_update (level);
Signature · 1 line
2.51.0
Hans de Goede
maintainerHi Vishnu,
Quoted 3 lines · Vishnu Sankar
Dynamically detect keyboard backlight capabilities and set
max_brightnesscorrectly (2 for old models, 3 for new models with Auto mode).
Thank you for your patch.
If I understand this correctly, writing 3 as level does not make the backlight more bright then writing 2, but instead it puts the backlight in some auto mode ?
If I've that correct then userspace should keep seeing a range of 0 - 2 and the special auto mode value should be reported / be made settable through a separate als_enabled sysfs attribute under the LED class device. See:
Documentation/ABI/testing/sysfs-platform-dell-laptop
You can add extra attributes there by setting the groups member of the struct led_classdev, see kbd_led_groups[] in drivers/platform/x86/dell/dell-laptop.c, except that you should use a .is_visible callback to only show this on hw which supports it and you only need 1 group with 1 attribute.
Regards,
Hans
Quoted 101 lines
Suggested-by Mark Pearson <[email protected]>Signed-off-by Vishnu Sankar <[email protected]>--- drivers/platform/x86/lenovo/thinkpad_acpi.c | 33 ++++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-)drivers/platform/x86/lenovo/thinkpad_acpi.c +29
-4@@ -5043,6 +5043,9 @@ static struct ibm_struct video_driver_data = {static enum led_brightness kbdlight_brightness;static DEFINE_MUTEX (kbdlight_mutex); +/* Maximum level supported by hardware, will be updated in init */ +static int kbdlight_max_level =2 ; +static int kbdlight_set_level (int level) {int ret =0 ;@@ -5050,6 +5053,10 @@ static int kbdlight_set_level(int level)if (!hkey_handle)return -ENXIO ; +/* Validate against detected max level */ +if (level <0 || level > kbdlight_max_level) +return -EINVAL ; +mutex_lock (&kbdlight_mutex);if (!acpi_evalf (hkey_handle,NULL ,"MLCS" ,"dd" , level))@@ -5075,6 +5082,7 @@ static int kbdlight_get_level(void)if (status <0 )return status; +/* Status can be 0, 1, 2, or 3 (Auto) */ return status &0x3 ; }@@ -5143,7 +5151,7 @@ static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)static struct tpacpi_led_classdev tpacpi_led_kbdlight = { .led_classdev = { .name ="tpacpi::kbd_backlight" , - .max_brightness =2 , + .max_brightness =2 ,/*Initial value, will be updated in init*/ .flags =LED_BRIGHT_HW_CHANGED , .brightness_set_blocking = &kbdlight_sysfs_set, .brightness_get = &kbdlight_sysfs_get,@@ -5167,6 +5175,17 @@ static int __init kbdlight_init(struct ibm_init_struct *iibm) kbdlight_brightness =kbdlight_sysfs_get (NULL ); tp_features.kbdlight =1 ; +/* Detect hardware capabilities and set max_brightness */ +if (acpi_evalf (hkey_handle,NULL ,"MLCS" ,"dd" ,3 )) { +/* MLCS accepts level 3 - new ThinkPad with Auto mode */ + kbdlight_max_level =3 ; + tpacpi_led_kbdlight.led_classdev.max_brightness =3 ; + }else { +/* MLCS rejects level 3 - old ThinkPad */ + kbdlight_max_level =2 ; + tpacpi_led_kbdlight.led_classdev.max_brightness =2 ; + } + rc =led_classdev_register (&tpacpi_pdev->dev, &tpacpi_led_kbdlight.led_classdev);if (rc <0 ) {@@ -5201,6 +5220,7 @@ static int kbdlight_set_level_and_update(int level)static int kbdlight_read (struct seq_file *m) {int level; +int i;if (!tp_features.kbdlight) {seq_printf (m,"status:\t\tnot supported\n" );@@ -5210,9 +5230,13 @@ static int kbdlight_read(struct seq_file *m)seq_printf (m,"status:\t\terror %d\n" , level); else seq_printf(m, "status:\t\t%d\n", level); -seq_printf (m,"commands:\t0, 1, 2\n" ); - } +/* Show available commands based on hardware */ +seq_puts (m,"commands:\t0" ); +for (i =1 ; i <= tpacpi_led_kbdlight.led_classdev.max_brightness; i++) +seq_printf (m,", %d" , i); +seq_puts (m,"\n" ); + }return 0 ; }@@ -5230,7 +5254,8 @@ static int kbdlight_write(char *buf)return res; } -if (level >=3 || level <0 ) +/* Validate against max level */ +if (level <0 || level > tpacpi_led_kbdlight.led_classdev.max_brightness)return -EINVAL ;return kbdlight_set_level_and_update (level);
Hi Hans, Vishnu and Mark,
Quoted 18 lines · Hans de Goede
Hi Vishnu,
Quoted 3 lines · Vishnu Sankar
Dynamically detect keyboard backlight capabilities and set
max_brightnesscorrectly (2 for old models, 3 for new models with Auto mode).Thank you for your patch.
If I understand this correctly, writing 3 as level does not make the backlight more bright then writing 2, but instead it puts the backlight in some auto mode ?
If I've that correct then userspace should keep seeing a range of 0 - 2 and the special auto mode value should be reported / be made settable through a separate
als_enabledsysfs attribute under the LED class device. See:
Quoted 8 lines
Documentation/ABI/testing/sysfs-platform-dell-laptopYou can add extra attributes there by setting the groups member of the struct
led_classdev, see kbd_led_groups[] indrivers/platform/x86/dell/dell-laptop.c, except that you should use a .is_visible callback to only show this on hw which supports it and you only need 1 group with 1 attribute.
When I implemented "als_enabled" for ideapad-laptop, Mark Pearson suggested it'd better to introduce "something similar to LED_BRIGHT_HW_CHANGED" rather than using custom attributes, as "this is going to be a common feature across multiple vendors it might need doing at a common layer". Also, auto mode can be activated by HW as a result of user input, so we need an approach to notify userspace just like what LED_BRIGHT_HW_CHANGED does. More importantly, the read value of the brightness attribute becomes nonsense when auto mode is on. This matches the semantic of hw control trigger.
I agreed with Mark and had a proposal of allowing HW to initiate a transition from "none" to hw control trigger and vice versa. See the thread in
https://lore.kernel.org/all/[email protected]/
I hadn't push it further due to other things taking the priority, though I already had a PoC back to then. I quickly rebased the PoC with some cleanups and put it here for preview:
https://github.com/Rongronggg9/linux/tree/leds-trigger-hw-changed
I will find some time to refine it and send an RFC series.
Thanks,
Rong
Quoted 110 lines
Regards,
Hans
Quoted 101 lines
Suggested-by Mark Pearson <[email protected]>Signed-off-by Vishnu Sankar <[email protected]>--- drivers/platform/x86/lenovo/thinkpad_acpi.c | 33 ++++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-)drivers/platform/x86/lenovo/thinkpad_acpi.c +29
-4@@ -5043,6 +5043,9 @@ static struct ibm_struct video_driver_data = {static enum led_brightness kbdlight_brightness;static DEFINE_MUTEX (kbdlight_mutex); +/* Maximum level supported by hardware, will be updated in init */ +static int kbdlight_max_level =2 ; +static int kbdlight_set_level (int level) {int ret =0 ;@@ -5050,6 +5053,10 @@ static int kbdlight_set_level(int level)if (!hkey_handle)return -ENXIO ; +/* Validate against detected max level */ +if (level <0 || level > kbdlight_max_level) +return -EINVAL ; +mutex_lock (&kbdlight_mutex);if (!acpi_evalf (hkey_handle,NULL ,"MLCS" ,"dd" , level))@@ -5075,6 +5082,7 @@ static int kbdlight_get_level(void)if (status <0 )return status; +/* Status can be 0, 1, 2, or 3 (Auto) */ return status &0x3 ; }@@ -5143,7 +5151,7 @@ static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)static struct tpacpi_led_classdev tpacpi_led_kbdlight = { .led_classdev = { .name ="tpacpi::kbd_backlight" , - .max_brightness =2 , + .max_brightness =2 ,/*Initial value, will be updated in init*/ .flags =LED_BRIGHT_HW_CHANGED , .brightness_set_blocking = &kbdlight_sysfs_set, .brightness_get = &kbdlight_sysfs_get,@@ -5167,6 +5175,17 @@ static int __init kbdlight_init(struct ibm_init_struct *iibm) kbdlight_brightness =kbdlight_sysfs_get (NULL ); tp_features.kbdlight =1 ; +/* Detect hardware capabilities and set max_brightness */ +if (acpi_evalf (hkey_handle,NULL ,"MLCS" ,"dd" ,3 )) { +/* MLCS accepts level 3 - new ThinkPad with Auto mode */ + kbdlight_max_level =3 ; + tpacpi_led_kbdlight.led_classdev.max_brightness =3 ; + }else { +/* MLCS rejects level 3 - old ThinkPad */ + kbdlight_max_level =2 ; + tpacpi_led_kbdlight.led_classdev.max_brightness =2 ; + } + rc =led_classdev_register (&tpacpi_pdev->dev, &tpacpi_led_kbdlight.led_classdev);if (rc <0 ) {@@ -5201,6 +5220,7 @@ static int kbdlight_set_level_and_update(int level)static int kbdlight_read (struct seq_file *m) {int level; +int i;if (!tp_features.kbdlight) {seq_printf (m,"status:\t\tnot supported\n" );@@ -5210,9 +5230,13 @@ static int kbdlight_read(struct seq_file *m)seq_printf (m,"status:\t\terror %d\n" , level); else seq_printf(m, "status:\t\t%d\n", level); -seq_printf (m,"commands:\t0, 1, 2\n" ); - } +/* Show available commands based on hardware */ +seq_puts (m,"commands:\t0" ); +for (i =1 ; i <= tpacpi_led_kbdlight.led_classdev.max_brightness; i++) +seq_printf (m,", %d" , i); +seq_puts (m,"\n" ); + }return 0 ; }@@ -5230,7 +5254,8 @@ static int kbdlight_write(char *buf)return res; } -if (level >=3 || level <0 ) +/* Validate against max level */ +if (level <0 || level > tpacpi_led_kbdlight.led_classdev.max_brightness)return -EINVAL ;return kbdlight_set_level_and_update (level);
Hi Hans and Rong,
Thank you so much for the comments.
Quoted 17 lines · Rong Zhang
Hi Hans, Vishnu and Mark,
Quoted 13 lines · Hans de Goede
Hi Vishnu,
Quoted 3 lines · Vishnu Sankar
Dynamically detect keyboard backlight capabilities and set
max_brightnesscorrectly (2 for old models, 3 for new models with Auto mode).Thank you for your patch.
If I understand this correctly, writing 3 as level does not make the backlight more bright then writing 2, but instead it puts the backlight in some auto mode ?
This is correct.
Quoted 5 lines
Quoted 5 lines
If I've that correct then userspace should keep seeing a range of 0 - 2 and the special auto mode value should be reported / be made settable through a separate
als_enabledsysfs attribute under the LED class device. See:
Quoted 8 lines
Quoted 8 lines
Documentation/ABI/testing/sysfs-platform-dell-laptopYou can add extra attributes there by setting the groups member of the struct
led_classdev, see kbd_led_groups[] indrivers/platform/x86/dell/dell-laptop.c, except that you should use a .is_visible callback to only show this on hw which supports it and you only need 1 group with 1 attribute.
Got it.
I believe, once Rong's changes come up, We may need to handle it a bit different.
Quoted 11 lines
When I implemented "
als_enabled" for ideapad-laptop, Mark Pearson suggested it'd better to introduce "something similar toLED_BRIGHT_HW_CHANGED" rather than using custom attributes, as "this is going to be a common feature across multiple vendors it might need doing at a common layer". Also, auto mode can be activated by HW as a result of user input, so we need an approach to notify userspace just like whatLED_BRIGHT_HW_CHANGEDdoes. More importantly, the read value of the brightness attribute becomes nonsense when auto mode is on. This matches the semantic of hw control trigger.
Quoted 4 lines
I agreed with Mark and had a proposal of allowing HW to initiate a transition from "none" to hw control trigger and vice versa. See the thread in
https://lore.kernel.org/all/[email protected]/
Thank you Rong.
We had a discussion a few months back about this internally. I will discuss with Mark on this approach again.
Quoted 8 lines
I hadn't push it further due to other things taking the priority, though I already had a PoC back to then. I quickly rebased the PoC with some cleanups and put it here for preview:
https://github.com/Rongronggg9/linux/tree/leds-trigger-hw-changed
I will find some time to refine it and send an RFC series.
Thank you.
Quoted 114 lines
Thanks,
RongQuoted 110 lines
Regards,
Hans
Quoted 101 lines
Suggested-by Mark Pearson <[email protected]>Signed-off-by Vishnu Sankar <[email protected]>--- drivers/platform/x86/lenovo/thinkpad_acpi.c | 33 ++++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-)drivers/platform/x86/lenovo/thinkpad_acpi.c +29
-4@@ -5043,6 +5043,9 @@ static struct ibm_struct video_driver_data = {static enum led_brightness kbdlight_brightness;static DEFINE_MUTEX (kbdlight_mutex); +/* Maximum level supported by hardware, will be updated in init */ +static int kbdlight_max_level =2 ; +static int kbdlight_set_level (int level) {int ret =0 ;@@ -5050,6 +5053,10 @@ static int kbdlight_set_level(int level)if (!hkey_handle)return -ENXIO ; +/* Validate against detected max level */ +if (level <0 || level > kbdlight_max_level) +return -EINVAL ; +mutex_lock (&kbdlight_mutex);if (!acpi_evalf (hkey_handle,NULL ,"MLCS" ,"dd" , level))@@ -5075,6 +5082,7 @@ static int kbdlight_get_level(void)if (status <0 )return status; +/* Status can be 0, 1, 2, or 3 (Auto) */ return status &0x3 ; }@@ -5143,7 +5151,7 @@ static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)static struct tpacpi_led_classdev tpacpi_led_kbdlight = { .led_classdev = { .name ="tpacpi::kbd_backlight" , - .max_brightness =2 , + .max_brightness =2 ,/*Initial value, will be updated in init*/ .flags =LED_BRIGHT_HW_CHANGED , .brightness_set_blocking = &kbdlight_sysfs_set, .brightness_get = &kbdlight_sysfs_get,@@ -5167,6 +5175,17 @@ static int __init kbdlight_init(struct ibm_init_struct *iibm) kbdlight_brightness =kbdlight_sysfs_get (NULL ); tp_features.kbdlight =1 ; +/* Detect hardware capabilities and set max_brightness */ +if (acpi_evalf (hkey_handle,NULL ,"MLCS" ,"dd" ,3 )) { +/* MLCS accepts level 3 - new ThinkPad with Auto mode */ + kbdlight_max_level =3 ; + tpacpi_led_kbdlight.led_classdev.max_brightness =3 ; + }else { +/* MLCS rejects level 3 - old ThinkPad */ + kbdlight_max_level =2 ; + tpacpi_led_kbdlight.led_classdev.max_brightness =2 ; + } + rc =led_classdev_register (&tpacpi_pdev->dev, &tpacpi_led_kbdlight.led_classdev);if (rc <0 ) {@@ -5201,6 +5220,7 @@ static int kbdlight_set_level_and_update(int level)static int kbdlight_read (struct seq_file *m) {int level; +int i;if (!tp_features.kbdlight) {seq_printf (m,"status:\t\tnot supported\n" );@@ -5210,9 +5230,13 @@ static int kbdlight_read(struct seq_file *m)seq_printf (m,"status:\t\terror %d\n" , level); else seq_printf(m, "status:\t\t%d\n", level); -seq_printf (m,"commands:\t0, 1, 2\n" ); - } +/* Show available commands based on hardware */ +seq_puts (m,"commands:\t0" ); +for (i =1 ; i <= tpacpi_led_kbdlight.led_classdev.max_brightness; i++) +seq_printf (m,", %d" , i); +seq_puts (m,"\n" ); + }return 0 ; }@@ -5230,7 +5254,8 @@ static int kbdlight_write(char *buf)return res; } -if (level >=3 || level <0 ) +/* Validate against max level */ +if (level <0 || level > tpacpi_led_kbdlight.led_classdev.max_brightness)return -EINVAL ;return kbdlight_set_level_and_update (level);
Signature · 4 lines
Regards,
Vishnu Sankar
+817015150407 (Japan)Mark Pearson
maintainerQuoted 45 lines · Rong Zhang
Hi Hans, Vishnu and Mark,
Quoted 18 lines · Hans de Goede
Hi Vishnu,
Quoted 3 lines · Vishnu Sankar
Dynamically detect keyboard backlight capabilities and set
max_brightnesscorrectly (2 for old models, 3 for new models with Auto mode).Thank you for your patch.
If I understand this correctly, writing 3 as level does not make the backlight more bright then writing 2, but instead it puts the backlight in some auto mode ?
If I've that correct then userspace should keep seeing a range of 0 - 2 and the special auto mode value should be reported / be made settable through a separate
als_enabledsysfs attribute under the LED class device. See:Quoted 8 lines
Documentation/ABI/testing/sysfs-platform-dell-laptopYou can add extra attributes there by setting the groups member of the struct
led_classdev, see kbd_led_groups[] indrivers/platform/x86/dell/dell-laptop.c, except that you should use a .is_visible callback to only show this on hw which supports it and you only need 1 group with 1 attribute.When I implemented "
als_enabled" for ideapad-laptop, Mark Pearson suggested it'd better to introduce "something similar to LED_BRIGHT_HW_CHANGED" rather than using custom attributes, as "this is going to be a common feature across multiple vendors it might need doing at a common layer". Also, auto mode can be activated by HW as a result of user input, so we need an approach to notify userspace just like whatLED_BRIGHT_HW_CHANGEDdoes. More importantly, the read value of the brightness attribute becomes nonsense when auto mode is on. This matches the semantic of hw control trigger.I agreed with Mark and had a proposal of allowing HW to initiate a transition from "none" to hw control trigger and vice versa. See the thread in
https://lore.kernel.org/all/[email protected]/
Quoted 8 lines
I hadn't push it further due to other things taking the priority, though I already had a PoC back to then. I quickly rebased the PoC with some cleanups and put it here for preview:
https://github.com/Rongronggg9/linux/tree/leds-trigger-hw-changed
I will find some time to refine it and send an RFC series.
Hi Rong,
Thanks for highlighting this (have to be honest - I'd forgotten we'd discussed it).
I think my suggestion may have been understood and I wonder your approach is more complicated than needed.
I was thinking we add a new flag to the led_classdev. e.g
#define LED_AUTO_BRIGHTNESS BIT(26)
Then the platform driver can set this flag and in led_classdev_register_ext we'd handle it appropriately to create a sysfs (e.g. auto_brightness_capable) node so user space knows auto is supported. Other than that:
- When the brightness is read and auton is being used - return "auto" instead of a value. Hopefully that doesn't break anything for user space?
- When the brightness is set, you can use a value or 'auto" as you desire (Documentation would need updating to allow this)
Really I was just looking for a way to advertise to user space that a auto option would be supported :)
Mark
Hi Mark,
Thanks for your reply.
Quoted 51 lines · Mark Pearson
Quoted 49 lines · Rong Zhang
Hi Hans, Vishnu and Mark,
Quoted 18 lines · Hans de Goede
Hi Vishnu,
Quoted 3 lines · Vishnu Sankar
Dynamically detect keyboard backlight capabilities and set
max_brightnesscorrectly (2 for old models, 3 for new models with Auto mode).Thank you for your patch.
If I understand this correctly, writing 3 as level does not make the backlight more bright then writing 2, but instead it puts the backlight in some auto mode ?
If I've that correct then userspace should keep seeing a range of 0 - 2 and the special auto mode value should be reported / be made settable through a separate
als_enabledsysfs attribute under the LED class device. See:Quoted 8 lines
Documentation/ABI/testing/sysfs-platform-dell-laptopYou can add extra attributes there by setting the groups member of the struct
led_classdev, see kbd_led_groups[] indrivers/platform/x86/dell/dell-laptop.c, except that you should use a .is_visible callback to only show this on hw which supports it and you only need 1 group with 1 attribute.When I implemented "
als_enabled" for ideapad-laptop, Mark Pearson suggested it'd better to introduce "something similar to LED_BRIGHT_HW_CHANGED" rather than using custom attributes, as "this is going to be a common feature across multiple vendors it might need doing at a common layer". Also, auto mode can be activated by HW as a result of user input, so we need an approach to notify userspace just like whatLED_BRIGHT_HW_CHANGEDdoes. More importantly, the read value of the brightness attribute becomes nonsense when auto mode is on. This matches the semantic of hw control trigger.I agreed with Mark and had a proposal of allowing HW to initiate a transition from "none" to hw control trigger and vice versa. See the thread in
https://lore.kernel.org/all/[email protected]/I hadn't push it further due to other things taking the priority, though I already had a PoC back to then. I quickly rebased the PoC with some cleanups and put it here for preview:
Quoted 8 lines
Quoted 4 lines
https://github.com/Rongronggg9/linux/tree/leds-trigger-hw-changed
I will find some time to refine it and send an RFC series.
Hi Rong,
Thanks for highlighting this (have to be honest - I'd forgotten we'd discussed it).
I think my suggestion may have been understood and I wonder your approach is more complicated than needed.
If there is a mechanism to set the brightness on specific events or conditions, it is a trigger. If the trigger is controlled by hardware, it's a hw control trigger. That's why I propose using a private hw control trigger to represent this to make it semantically correct.
Quoted 2 lines
I was thinking we add a new flag to the
led_classdev. e.g
#defineLED_AUTO_BRIGHTNESSBIT(26)
Implementing it this way is still complicated as far as I can imagine:
- A new attribute to expose the capability as you've said.
- We need to extend brightness_get/brightness_set[_blocking] interfaces to accept/emit a special brightness value to represent auto mode.
- We should handle brightness setting requests from usersapce and from led triggers separately: the former can put the LED into auto mode while the latter cannot.
- Deprecate brightness and brightness_hw_changed while introducing new attributes. We can't extend existing attributes as I will explain later. That's the most frustrating part :-/
And this approach becomes a bit weird if a future SKU comes with its auto mode tunable: you will have some device attributes which are only meaningful when auto mode is active. This is all because they are fundamentally trigger attributes in the first place...
Quoted 3 lines
Then the platform driver can set this flag and in
led_classdev_register_extwe'd handle it appropriately to create a sysfs (e.g.auto_brightness_capable) node so user space knows auto is supported. Other than that:
- When the brightness is read and auton is being used - return "auto" instead of a value. Hopefully that doesn't break anything for user space?
It will likely break something. We can't extend an interface with new data types.
For example, existing userspace programs may have being using these for long:
- POSIX shell: [ -eq, -ne, -gt, -ge, -lt, -le ]
- Bash: let, (( ))
- C: atoi(), atol(), atoll(), fscanf(), vfscanf()
- Python: int()
- Regex: [0-9], \d (PCRE), [[:digit:]] (POSIX)
- ... And more similar things dealing with integers
I think it's not worth deprecating the existing interface just to introduce something that is not fundamentally "brightness".
Quoted 2 lines
- When the brightness is set, you can use a value or 'auto" as you desire (Documentation would need updating to allow this)
Really I was just looking for a way to advertise to user space that a auto option would be supported :)
That's my goal too.
I admit that my proposal is complicated and may need a lot of time to make it into its right path. It may even be rejected by LED folks. But it's the best approach I can think of considering our requirements on the interface:
1. It shouldn't break any existing interfaces.
2. It's exposed to userspace for getting or setting its status.
3. HW status transition should reach userspace (similar to LED_BRIGHT_HW_CHANGED).
I will see if I can finish my RFC patch this week or next. If it's rejected probably we will have to continue on "als_enabled"...
Thanks,
Rong
Quoted 3 lines
Mark
Mark Pearson
maintainerThanks Rong
Quoted 62 lines · Rong Zhang
Hi Mark,
Thanks for your reply.
Quoted 57 lines · Mark Pearson
Quoted 45 lines · Rong Zhang
Hi Hans, Vishnu and Mark,
Quoted 18 lines · Hans de Goede
Hi Vishnu,
Quoted 3 lines · Vishnu Sankar
Dynamically detect keyboard backlight capabilities and set
max_brightnesscorrectly (2 for old models, 3 for new models with Auto mode).Thank you for your patch.
If I understand this correctly, writing 3 as level does not make the backlight more bright then writing 2, but instead it puts the backlight in some auto mode ?
If I've that correct then userspace should keep seeing a range of 0 - 2 and the special auto mode value should be reported / be made settable through a separate
als_enabledsysfs attribute under the LED class device. See:Quoted 8 lines
Documentation/ABI/testing/sysfs-platform-dell-laptopYou can add extra attributes there by setting the groups member of the struct
led_classdev, see kbd_led_groups[] indrivers/platform/x86/dell/dell-laptop.c, except that you should use a .is_visible callback to only show this on hw which supports it and you only need 1 group with 1 attribute.When I implemented "
als_enabled" for ideapad-laptop, Mark Pearson suggested it'd better to introduce "something similar to LED_BRIGHT_HW_CHANGED" rather than using custom attributes, as "this is going to be a common feature across multiple vendors it might need doing at a common layer". Also, auto mode can be activated by HW as a result of user input, so we need an approach to notify userspace just like whatLED_BRIGHT_HW_CHANGEDdoes. More importantly, the read value of the brightness attribute becomes nonsense when auto mode is on. This matches the semantic of hw control trigger.I agreed with Mark and had a proposal of allowing HW to initiate a transition from "none" to hw control trigger and vice versa. See the thread in
https://lore.kernel.org/all/[email protected]/Quoted 8 lines
I hadn't push it further due to other things taking the priority, though I already had a PoC back to then. I quickly rebased the PoC with some cleanups and put it here for preview:
https://github.com/Rongronggg9/linux/tree/leds-trigger-hw-changed
I will find some time to refine it and send an RFC series.
Hi Rong,
Quoted 8 lines
Quoted 2 lines
Thanks for highlighting this (have to be honest - I'd forgotten we'd discussed it).
I think my suggestion may have been understood and I wonder your approach is more complicated than needed.If there is a mechanism to set the brightness on specific events or conditions, it is a trigger. If the trigger is controlled by hardware, it's a hw control trigger. That's why I propose using a private hw control trigger to represent this to make it semantically correct.
Ah. I think it will be confusing for most users. They're not going to think of it as a trigger (that's my guess anyway)
Quoted 37 lines
Quoted 2 lines
I was thinking we add a new flag to the
led_classdev. e.g
#defineLED_AUTO_BRIGHTNESSBIT(26)Implementing it this way is still complicated as far as I can imagine:
- A new attribute to expose the capability as you've said.
- We need to extend brightness_get/brightness_set[_blocking] interfaces to accept/emit a special brightness value to represent auto mode.
- We should handle brightness setting requests from usersapce and from led triggers separately: the former can put the LED into auto mode while the latter cannot.
- Deprecate brightness andbrightness_hw_changedwhile introducing new attributes. We can't extend existing attributes as I will explain later. That's the most frustrating part :-/And this approach becomes a bit weird if a future SKU comes with its auto mode tunable: you will have some device attributes which are only meaningful when auto mode is active. This is all because they are fundamentally trigger attributes in the first place...
Quoted 3 lines
Then the platform driver can set this flag and in
led_classdev_register_extwe'd handle it appropriately to create a sysfs (e.g.auto_brightness_capable) node so user space knows auto is supported. Other than that:
- When the brightness is read and auton is being used - return "auto" instead of a value. Hopefully that doesn't break anything for user space?It will likely break something. We can't extend an interface with new data types.
For example, existing userspace programs may have being using these for long:
- POSIX shell: [ -eq, -ne, -gt, -ge, -lt, -le ]
- Bash: let, (( ))
- C:atoi(),atol(),atoll(),fscanf(),vfscanf()
- Python:int()
- Regex: [0-9], \d (PCRE), [[:digit:]] (POSIX)
- ... And more similar things dealing with integers
Quoted 3 lines
I think it's not worth deprecating the existing interface just to introduce something that is not fundamentally "brightness".
Yeah - that's fair. You're right - we shouldn't change the brightness field.
So, how about adding two sysfs nodes to the LED class?
- auto_brightness_capable - indicates the LED brightness can go into an auto control mode
- auto_brightness_enabled - indicates if the LED is in the auto_brightness controlled state or not.
Then it's up to the individual drivers (thinkpad/ideapad/whatever) to set the fields appropriately as they change modes. User space will need changing to handle these, but such is life.
Quoted 10 lines
Quoted 2 lines
- When the brightness is set, you can use a value or 'auto" as you desire (Documentation would need updating to allow this)
Really I was just looking for a way to advertise to user space that a auto option would be supported :)That's my goal too.
I admit that my proposal is complicated and may need a lot of time to make it into its right path. It may even be rejected by LED folks. But it's the best approach I can think of considering our requirements on the interface:
Quoted 4 lines
1. It shouldn't break any existing interfaces.
2. It's exposed to userspace for getting or setting its status.
3. HW status transition should reach userspace (similar toLED_BRIGHT_HW_CHANGED).
Just to check - for #3 do you mean it should report the brightness changes when it's in auto mode (i.e. if it got brighter or dimmer); or if it should just report it switched in/out of auto mode. I don't think we need to report every brightness status change - and switching modes should be user directed so is no different to currently. Am I missing something?
Thanks
Mark
Hans de Goede
maintainerHi all,
On 9-Feb-26 19:44, Mark Pearson wrote:
...
Quoted 4 lines
Yeah - that's fair. You're right - we shouldn't change the brightness field.
So, how about adding two sysfs nodes to the LED class?
-auto_brightness_capable- indicates the LED brightness can go into an auto control mode
There is no need for this, the mere presence of the "auto_brightness_enabled" sysfs attribute (which can be in a sysfs-attr-group with an is_visible callback) is enough to indicate that the backlight is auto brightness capable.
Quoted 1 line
-
auto_brightness_enabled- indicates if the LED is in theauto_brightnesscontrolled state or not.
This is for auto-brightness based on an ambient light sensor (ALS), right ?
My vote would go to use "als_enabled", just like is already done in:
Documentation/ABI/testing/sysfs-platform-dell-laptop
adding new sysfs attributes to a LED class device although possible is a bit frowned upon though.
In that sense using a trigger is better because it more closely matches how the LED class API is supposed to be used would maybe be better.
So I've gone and re-read Rong's trigger proposal:
https://lore.kernel.org/all/[email protected]/
Rong, previously you also went a bit further with implementing this already which you described here:
https://lore.kernel.org/all/[email protected]/
One of the problems you encountered there is what to do if the user actually set a trigger themselves and the EC moves between fixed-brightness-value <-> ALS .
My first idea was to just always override the trigger with the special ALS trigger or none.
But thinking more about this this is wrong. E.g. there are triggers which turn the backlight on when user input is detected and then off after a while, which would be a perfect reasonable thing to use together with a kbd-backlight.
Thinking more about this triggers are typically for deciding when to turn the LED on/off not for controlling brightness many of them actually allow still writing the brightness sysfs attr and then when the LED should be on according to that trigger, the trigger use the last written brightness.
Looking at things this way ALS is not really a trigger, it is more of a brightness control mechanism.
So I think the best and also KISS solution here would be to go with adding a "als_enabled" sysfs attr to the LED class device, which is only visible when support, just like is already done in:
Documentation/ABI/testing/sysfs-platform-dell-laptop
I would also call led_classdev_notify_brightness_hw_changed() when the EC moves between fixed-brightness-value <-> ALS.
Userspace will likely already have a poll() going on on the brightness_hw_changed sysfs attr, so this way userspace which is aware of the als_enabled sysfs attr can also check that.
You can then report brightness_max as the new value when calling led_classdev_notify_brightness_hw_changed() since the ALS can go up to brightness_max, likewise you could also always return brightness_max when reading the brightness value while in ALS mode.
The only real question left then is what to do on brightness writes. I would do the same as what triggers do here, ignore writing non 0 values and turn off the backlight (and thus also ALS) when 0 is written.
Note as for actually allowing "auto" for the brightness value (read/write) that would break userspace assumptions that that file always contains an integer, so that is not an option IMHO.
Regards,
Hans
Hi all,
Thanks for your insight.
Quoted 17 lines · Hans de Goede
Hi all,
On 9-Feb-26 19:44, Mark Pearson wrote:
...
Quoted 4 lines
Yeah - that's fair. You're right - we shouldn't change the brightness field.
So, how about adding two sysfs nodes to the LED class?
-auto_brightness_capable- indicates the LED brightness can go into an auto control modeThere is no need for this, the mere presence of the "
auto_brightness_enabled" sysfs attribute (which can be in a sysfs-attr-group with anis_visiblecallback) is enough to indicate that the backlight is auto brightness capable.
Quoted 8 lines
Quoted 1 line
-
auto_brightness_enabled- indicates if the LED is in theauto_brightnesscontrolled state or not.This is for auto-brightness based on an ambient light sensor (ALS), right ?
My vote would go to use "
als_enabled", just like is already done in:
Documentation/ABI/testing/sysfs-platform-dell-laptop
In my previous reply I said 'if my proposal is rejected we will have to continue on "als_enabled"'. But after some consideration, I have a major concern about its naming: extensibility.
More and more devices already come with a human presence detection sensor (HPD). Suppose that a future device implements auto-brightness based on its HPD, the name will be irrelevant and we must introduce "hpd_enabled" then. Userspace programs must be rewritten to handle both.
Mark, do you think such devices may appear in the foreseeable future?
Another concern of mine on the approach is that it may lead to chaos if a future device implements auto-brightness based on multiple sensors. In this case a well-defined interface should have an aggregated attribute^ simply representing whether auto-brightness is on or off.
For this reason, a sensor-neutral name will be better if we should stick with the device attribute approach.
^: A private hw control trigger is like an aggregated attribute while it can provide trigger attributes for fine-grained control.
Quoted 2 lines
adding new sysfs attributes to a LED class device although possible is a bit frowned upon though.
Yeah, That's one of the reasons why I made that proposal.
Quoted 11 lines
In that sense using a trigger is better because it more closely matches how the LED class API is supposed to be used would maybe be better.
So I've gone and re-read Rong's trigger proposal:
https://lore.kernel.org/all/[email protected]/
Rong, previously you also went a bit further with implementing this already which you described here:
Quoted 8 lines
https://lore.kernel.org/all/[email protected]/
One of the problems you encountered there is what to do if the user actually set a trigger themselves and the EC moves between fixed-brightness-value <-> ALS .
My first idea was to just always override the trigger with the special ALS trigger or none.
My PoC does the opposite, see my explanation below.
Quoted 4 lines
But thinking more about this this is wrong. E.g. there are triggers which turn the backlight on when user input is detected and then off after a while, which would be a perfect reasonable thing to use together with a kbd-backlight.
Yes, that's why my PoC intentionally does nothing when an other trigger is active, effectively allowing the active trigger to override the ALS trigger. See led_trigger_do_hw_control_transition().
Quoted 2 lines
Thinking more about this triggers are typically for deciding when to turn the LED on/off not for controlling brightness
The trigger "pattern" can control LED's brightness.
Quoted 3 lines
many of them actually allow still writing the brightness sysfs attr and then when the LED should be on according to that trigger, the trigger use the last written brightness.
Thanks for the information! I didn't know there are triggers behaving like this before.
Quoted 2 lines
Looking at things this way ALS is not really a trigger, it is more of a brightness control mechanism.
IIUC, being able to control something that is not capable for a general purpose LED trigger is the reason why the private trigger interface exists.
Quoted 15 lines
So I think the best and also KISS solution here would be to go with adding a "
als_enabled" sysfs attr to the LED class device, which is only visible when support, just like is already done in:
Documentation/ABI/testing/sysfs-platform-dell-laptopI would also call
led_classdev_notify_brightness_hw_changed()when the EC moves between fixed-brightness-value <-> ALS.Userspace will likely already have a
poll()going on on thebrightness_hw_changedsysfs attr, so this way userspace which is aware of theals_enabledsysfs attr can also check that.
Quoted 4 lines
You can then report
brightness_maxas the new value when callingled_classdev_notify_brightness_hw_changed()since the ALS can go up tobrightness_max, likewise you could also always returnbrightness_maxwhen reading the brightness value while in ALS mode.
Makes sense. If we decide we should stick with the device attribute approach, I will adopt this in v2 of my ideapad-laptop series.
Quoted 4 lines
The only real question left then is what to do on brightness writes. I would do the same as what triggers do here, ignore writing non 0 values and turn off the backlight (and thus also ALS) when 0 is written.
I have two concerns on this behavior:
1.
IIUC, there is no way for a LED device to determine if it is attached to a trigger. The LED core does know this, but it won't tell the LED device driver.
In other words, we can't distinguish trigger requests from userspace ones in our brightness_set[_blocking] callback, so we have to either ignore both or none. As a result, we can't ignore anything and must blindly accept any incoming requests in order not to break triggers.
That's another reason why I made my proposal -- it must become a trigger in order to be aware of other triggers.
Am I missing something? Or did you mean we should add the attribute to the LED core?
2.
I quickly rechecked the LED core's code, and it doesn't behave as you expected. It doesn't ignore non-zero written values when a trigger is attached to the LED, and it will set the LED's brightness to the written value despite the trigger (it will be discarded by next trigger event, though).
When the effective trigger is a hw control trigger, LED core's behavior effectively disables hw control. Hmm... it seems that this side-effect has been documented:
When the LED is in hw control, no software blink is possible and doing so will effectively disable hw control.
So in my perspective the hw control trigger approach is semantically correct here (in an unexpected way).
Quoted 8 lines
Note as for actually allowing "auto" for the brightness value (read/write) that would break userspace assumptions that that file always contains an integer, so that is not an option IMHO.
Regards,
Hans
Quoted 11 lines · Mark Pearson
Thanks Rong
Quoted 6 lines · Rong Zhang
[...]
If there is a mechanism to set the brightness on specific events or conditions, it is a trigger. If the trigger is controlled by hardware, it's a hw control trigger. That's why I propose using a private hw control trigger to represent this to make it semantically correct.
Ah. I think it will be confusing for most users. They're not going to think of it as a trigger (that's my guess anyway)
I guess most users simply tune the keyboard backlight via desktop environments, so it won't directly make them confused. When it comes to desktop environments, this is precisely why we want the interface capable to notify userspace about HW status transition.
And both cros_ec' and turris-omnia' have been using private hw control triggers to represent auto mode.
Quoted 7 lines
Quoted 7 lines
[...]
I admit that my proposal is complicated and may need a lot of time to make it into its right path. It may even be rejected by LED folks. But it's the best approach I can think of considering our requirements on the interface:
Quoted 7 lines
Quoted 4 lines
1. It shouldn't break any existing interfaces.
2. It's exposed to userspace for getting or setting its status.
3. HW status transition should reach userspace (similar toLED_BRIGHT_HW_CHANGED).Just to check - for #3 do you mean it should report the brightness changes when it's in auto mode (i.e. if it got brighter or dimmer); or if it should just report it switched in/out of auto mode. I don't think we need to report every brightness status change - and switching modes should be user directed so is no different to currently. Am I missing something?
I meant auto-brightness on <-> off, or fixed-brightness-value <-> ALS in Hans' words.
Quoted 2 lines
Thanks
Mark
To conclude:
1. My proposal:
Upsides:
- mutually exclusive with other triggers (hence less chaos)
- semantic correctness
- extensibility (through trigger attributes)
- acts as an aggregate switch to turn on/off hw control
Downsides:
- complexity
- needs approval from LED folks
- (I admit both are major blockers...)
2. Device attribute
Upsides:
- simplicity, KISS
- no need to touch LED core
- extensible as long as it has a sensor-neutral name
Downsides:
- must have zero influence on the brightness_set[_blocking] callback in order not to break triggers
- potential interference with triggers and the brightness attribute
- weird semantic
I am actually OK with both approaches. If you still consider the device attribute approach is better after reading my concerns, I will no longer insist on my proposal.
Thanks,
Rong
Mark Pearson
maintainerQuoted 21 lines · Rong Zhang
Hi all,
Thanks for your insight.
Quoted 16 lines · Hans de Goede
Hi all,
On 9-Feb-26 19:44, Mark Pearson wrote:
...
Quoted 4 lines
Yeah - that's fair. You're right - we shouldn't change the brightness field.
So, how about adding two sysfs nodes to the LED class?
-auto_brightness_capable- indicates the LED brightness can go into an auto control modeThere is no need for this, the mere presence of the "
auto_brightness_enabled" sysfs attribute (which can be in a sysfs-attr-group with anis_visiblecallback) is enough to indicate that the backlight is auto brightness capable.
Good point - agreed
Quoted 10 lines
Quoted 9 lines
Quoted 2 lines
-
auto_brightness_enabled- indicates if the LED is in theauto_brightnesscontrolled state or not.This is for auto-brightness based on an ambient light sensor (ALS), right ?
My vote would go to use "
als_enabled", just like is already done in:
Documentation/ABI/testing/sysfs-platform-dell-laptop
I didn't realise it had been done before (2014 too!)
Quoted 4 lines
In my previous reply I said 'if my proposal is rejected we will have to continue on "
als_enabled"'. But after some consideration, I have a major concern about its naming: extensibility.
Quoted 8 lines
More and more devices already come with a human presence detection sensor (HPD). Suppose that a future device implements auto-brightness based on its HPD, the name will be irrelevant and we must introduce "
hpd_enabled" then. Userspace programs must be rewritten to handle both.Mark, do you think such devices may appear in the foreseeable future?
Not on my radar....but I also don't have a crystal ball.
Quoted 8 lines
Another concern of mine on the approach is that it may lead to chaos if a future device implements auto-brightness based on multiple sensors. In this case a well-defined interface should have an aggregated attribute^ simply representing whether auto-brightness is on or off.
For this reason, a sensor-neutral name will be better if we should stick with the device attribute approach.
I'm with Hans that if it's been done before then consistency would be nice.
But then again the Dell implementation is Dell specific and we're trying to do something a bit more generic so I don't feel strongly about it
What about "auto_enabled" and the value can be "off" or "als" for now, and if some other mechanism is added in the future it is extensible to other control mechanisms (and they could be multiple - e.g. "als,hpd"). I'm worried we're over-thinking it though.
Quoted 105 lines
^: A private hw control trigger is like an aggregated attribute while it can provide trigger attributes for fine-grained control.
Quoted 2 lines
adding new sysfs attributes to a LED class device although possible is a bit frowned upon though.
Yeah, That's one of the reasons why I made that proposal.
Quoted 11 lines
In that sense using a trigger is better because it more closely matches how the LED class API is supposed to be used would maybe be better.
So I've gone and re-read Rong's trigger proposal:
https://lore.kernel.org/all/[email protected]/
Rong, previously you also went a bit further with implementing this already which you described here:
Quoted 8 lines
https://lore.kernel.org/all/[email protected]/
One of the problems you encountered there is what to do if the user actually set a trigger themselves and the EC moves between fixed-brightness-value <-> ALS .
My first idea was to just always override the trigger with the special ALS trigger or none.
My PoC does the opposite, see my explanation below.
Quoted 4 lines
But thinking more about this this is wrong. E.g. there are triggers which turn the backlight on when user input is detected and then off after a while, which would be a perfect reasonable thing to use together with a kbd-backlight.
Yes, that's why my PoC intentionally does nothing when an other trigger is active, effectively allowing the active trigger to override the ALS trigger. See
led_trigger_do_hw_control_transition().Quoted 2 lines
Thinking more about this triggers are typically for deciding when to turn the LED on/off not for controlling brightness
The trigger "pattern" can control LED's brightness.
Quoted 3 lines
many of them actually allow still writing the brightness sysfs attr and then when the LED should be on according to that trigger, the trigger use the last written brightness.
Thanks for the information! I didn't know there are triggers behaving like this before.
Quoted 2 lines
Looking at things this way ALS is not really a trigger, it is more of a brightness control mechanism.
IIUC, being able to control something that is not capable for a general purpose LED trigger is the reason why the private trigger interface exists.
Quoted 15 lines
So I think the best and also KISS solution here would be to go with adding a "
als_enabled" sysfs attr to the LED class device, which is only visible when support, just like is already done in:
Documentation/ABI/testing/sysfs-platform-dell-laptopI would also call
led_classdev_notify_brightness_hw_changed()when the EC moves between fixed-brightness-value <-> ALS.Userspace will likely already have a
poll()going on on thebrightness_hw_changedsysfs attr, so this way userspace which is aware of theals_enabledsysfs attr can also check that.Quoted 4 lines
You can then report
brightness_maxas the new value when callingled_classdev_notify_brightness_hw_changed()since the ALS can go up tobrightness_max, likewise you could also always returnbrightness_maxwhen reading the brightness value while in ALS mode.Makes sense. If we decide we should stick with the device attribute approach, I will adopt this in v2 of my ideapad-laptop series.
Quoted 4 lines
The only real question left then is what to do on brightness writes. I would do the same as what triggers do here, ignore writing non 0 values and turn off the backlight (and thus also ALS) when 0 is written.
I have two concerns on this behavior:
1.
IIUC, there is no way for a LED device to determine if it is attached to a trigger. The LED core does know this, but it won't tell the LED device driver.
In other words, we can't distinguish trigger requests from userspace ones in our brightness_set[_blocking] callback, so we have to either ignore both or none. As a result, we can't ignore anything and must blindly accept any incoming requests in order not to break triggers.
That's another reason why I made my proposal -- it must become a trigger in order to be aware of other triggers.
Am I missing something? Or did you mean we should add the attribute to the LED core?
Quoted 8 lines
2.
I quickly rechecked the LED core's code, and it doesn't behave as you expected. It doesn't ignore non-zero written values when a trigger is attached to the LED, and it will set the LED's brightness to the written value despite the trigger (it will be discarded by next trigger event, though).
I'm a bit confused here to be honest.
If a user sets a specific level - then it should disable auto mode shouldn't it?
Quoted 4 lines
When the effective trigger is a hw control trigger, LED core's behavior effectively disables hw control. Hmm... it seems that this side-effect has been documented:
Quoted 10 lines
When the LED is in hw control, no software blink is possible and doing so will effectively disable hw control.
So in my perspective the hw control trigger approach is semantically correct here (in an unexpected way).
Quoted 4 lines
Note as for actually allowing "auto" for the brightness value (read/write) that would break userspace assumptions that that file always contains an integer, so that is not an option IMHO.
Yeah - I think we're all in agreement that it was a bad idea. I have swept it back under the rock it came from
Quoted 38 lines
Quoted 4 lines
Regards,
Hans
Quoted 11 lines · Mark Pearson
Thanks Rong
Quoted 6 lines · Rong Zhang
[...]
If there is a mechanism to set the brightness on specific events or conditions, it is a trigger. If the trigger is controlled by hardware, it's a hw control trigger. That's why I propose using a private hw control trigger to represent this to make it semantically correct.
Ah. I think it will be confusing for most users. They're not going to think of it as a trigger (that's my guess anyway)
I guess most users simply tune the keyboard backlight via desktop environments, so it won't directly make them confused. When it comes to desktop environments, this is precisely why we want the interface capable to notify userspace about HW status transition.
And both
cros_ec' andturris-omnia' have been using private hw control triggers to represent auto mode.Quoted 12 lines
Quoted 11 lines
[...]
I admit that my proposal is complicated and may need a lot of time to make it into its right path. It may even be rejected by LED folks. But it's the best approach I can think of considering our requirements on the interface:
1. It shouldn't break any existing interfaces.
2. It's exposed to userspace for getting or setting its status.
3. HW status transition should reach userspace (similar toLED_BRIGHT_HW_CHANGED).
Quoted 6 lines
Quoted 2 lines
Just to check - for #3 do you mean it should report the brightness changes when it's in auto mode (i.e. if it got brighter or dimmer); or if it should just report it switched in/out of auto mode. I don't think we need to report every brightness status change - and switching modes should be user directed so is no different to currently. Am I missing something?
I meant auto-brightness on <-> off, or fixed-brightness-value <-> ALS in Hans' words.
great - makes sense
Quoted 31 lines
Quoted 2 lines
Thanks
MarkTo conclude:
1. My proposal:
Upsides:
- mutually exclusive with other triggers (hence less chaos)
- semantic correctness
- extensibility (through trigger attributes)
- acts as an aggregate switch to turn on/off hw controlDownsides:
- complexity
- needs approval from LED folks
- (I admit both are major blockers...)2. Device attribute
Upsides:
- simplicity, KISS
- no need to touch LED core
- extensible as long as it has a sensor-neutral nameDownsides:
- must have zero influence on the brightness_set[_blocking] callback in order not to break triggers
- potential interference with triggers and the brightness attribute
- weird semantic
Quoted 4 lines
I am actually OK with both approaches. If you still consider the device attribute approach is better after reading my concerns, I will no longer insist on my proposal.
My (limited) vote is for the simplest solution - because it's just a keyboard light. But I'll defer to Hans here - I don't think I know enough to have a strong opinion.
Mark
Mark Pearson
maintainerQuoted 286 lines · Mark Pearson
Quoted 21 lines · Rong Zhang
Hi all,
Thanks for your insight.
Quoted 16 lines · Hans de Goede
Hi all,
On 9-Feb-26 19:44, Mark Pearson wrote:
...
Quoted 4 lines
Yeah - that's fair. You're right - we shouldn't change the brightness field.
So, how about adding two sysfs nodes to the LED class?
-auto_brightness_capable- indicates the LED brightness can go into an auto control modeThere is no need for this, the mere presence of the "
auto_brightness_enabled" sysfs attribute (which can be in a sysfs-attr-group with anis_visiblecallback) is enough to indicate that the backlight is auto brightness capable.Good point - agreed
Quoted 10 lines
Quoted 9 lines
Quoted 2 lines
-
auto_brightness_enabled- indicates if the LED is in theauto_brightnesscontrolled state or not.This is for auto-brightness based on an ambient light sensor (ALS), right ?
My vote would go to use "
als_enabled", just like is already done in:
Documentation/ABI/testing/sysfs-platform-dell-laptopI didn't realise it had been done before (2014 too!)
Quoted 4 lines
In my previous reply I said 'if my proposal is rejected we will have to continue on "
als_enabled"'. But after some consideration, I have a major concern about its naming: extensibility.Quoted 8 lines
More and more devices already come with a human presence detection sensor (HPD). Suppose that a future device implements auto-brightness based on its HPD, the name will be irrelevant and we must introduce "
hpd_enabled" then. Userspace programs must be rewritten to handle both.Mark, do you think such devices may appear in the foreseeable future?
Not on my radar....but I also don't have a crystal ball.
Quoted 8 lines
Another concern of mine on the approach is that it may lead to chaos if a future device implements auto-brightness based on multiple sensors. In this case a well-defined interface should have an aggregated attribute^ simply representing whether auto-brightness is on or off.
For this reason, a sensor-neutral name will be better if we should stick with the device attribute approach.
I'm with Hans that if it's been done before then consistency would be nice.
But then again the Dell implementation is Dell specific and we're trying to do something a bit more generic so I don't feel strongly about itWhat about "
auto_enabled" and the value can be "off" or "als" for now, and if some other mechanism is added in the future it is extensible to other control mechanisms (and they could be multiple - e.g. "als,hpd"). I'm worried we're over-thinking it though.Quoted 105 lines
^: A private hw control trigger is like an aggregated attribute while it can provide trigger attributes for fine-grained control.
Quoted 2 lines
adding new sysfs attributes to a LED class device although possible is a bit frowned upon though.
Yeah, That's one of the reasons why I made that proposal.
Quoted 11 lines
In that sense using a trigger is better because it more closely matches how the LED class API is supposed to be used would maybe be better.
So I've gone and re-read Rong's trigger proposal:
https://lore.kernel.org/all/[email protected]/
Rong, previously you also went a bit further with implementing this already which you described here:
Quoted 8 lines
https://lore.kernel.org/all/[email protected]/
One of the problems you encountered there is what to do if the user actually set a trigger themselves and the EC moves between fixed-brightness-value <-> ALS .
My first idea was to just always override the trigger with the special ALS trigger or none.
My PoC does the opposite, see my explanation below.
Quoted 4 lines
But thinking more about this this is wrong. E.g. there are triggers which turn the backlight on when user input is detected and then off after a while, which would be a perfect reasonable thing to use together with a kbd-backlight.
Yes, that's why my PoC intentionally does nothing when an other trigger is active, effectively allowing the active trigger to override the ALS trigger. See
led_trigger_do_hw_control_transition().Quoted 2 lines
Thinking more about this triggers are typically for deciding when to turn the LED on/off not for controlling brightness
The trigger "pattern" can control LED's brightness.
Quoted 3 lines
many of them actually allow still writing the brightness sysfs attr and then when the LED should be on according to that trigger, the trigger use the last written brightness.
Thanks for the information! I didn't know there are triggers behaving like this before.
Quoted 2 lines
Looking at things this way ALS is not really a trigger, it is more of a brightness control mechanism.
IIUC, being able to control something that is not capable for a general purpose LED trigger is the reason why the private trigger interface exists.
Quoted 15 lines
So I think the best and also KISS solution here would be to go with adding a "
als_enabled" sysfs attr to the LED class device, which is only visible when support, just like is already done in:
Documentation/ABI/testing/sysfs-platform-dell-laptopI would also call
led_classdev_notify_brightness_hw_changed()when the EC moves between fixed-brightness-value <-> ALS.Userspace will likely already have a
poll()going on on thebrightness_hw_changedsysfs attr, so this way userspace which is aware of theals_enabledsysfs attr can also check that.Quoted 4 lines
You can then report
brightness_maxas the new value when callingled_classdev_notify_brightness_hw_changed()since the ALS can go up tobrightness_max, likewise you could also always returnbrightness_maxwhen reading the brightness value while in ALS mode.Makes sense. If we decide we should stick with the device attribute approach, I will adopt this in v2 of my ideapad-laptop series.
Quoted 4 lines
The only real question left then is what to do on brightness writes. I would do the same as what triggers do here, ignore writing non 0 values and turn off the backlight (and thus also ALS) when 0 is written.
I have two concerns on this behavior:
1.
IIUC, there is no way for a LED device to determine if it is attached to a trigger. The LED core does know this, but it won't tell the LED device driver.
In other words, we can't distinguish trigger requests from userspace ones in our brightness_set[_blocking] callback, so we have to either ignore both or none. As a result, we can't ignore anything and must blindly accept any incoming requests in order not to break triggers.
That's another reason why I made my proposal -- it must become a trigger in order to be aware of other triggers.
Am I missing something? Or did you mean we should add the attribute to the LED core?
Quoted 8 lines
2.
I quickly rechecked the LED core's code, and it doesn't behave as you expected. It doesn't ignore non-zero written values when a trigger is attached to the LED, and it will set the LED's brightness to the written value despite the trigger (it will be discarded by next trigger event, though).
I'm a bit confused here to be honest.
If a user sets a specific level - then it should disable auto mode shouldn't it?Quoted 4 lines
When the effective trigger is a hw control trigger, LED core's behavior effectively disables hw control. Hmm... it seems that this side-effect has been documented:
Quoted 10 lines
When the LED is in hw control, no software blink is possible and doing so will effectively disable hw control.
So in my perspective the hw control trigger approach is semantically correct here (in an unexpected way).
Quoted 4 lines
Note as for actually allowing "auto" for the brightness value (read/write) that would break userspace assumptions that that file always contains an integer, so that is not an option IMHO.
Yeah - I think we're all in agreement that it was a bad idea. I have swept it back under the rock it came from
Quoted 38 lines
Quoted 4 lines
Regards,
Hans
Quoted 11 lines · Mark Pearson
Thanks Rong
Quoted 6 lines · Rong Zhang
[...]
If there is a mechanism to set the brightness on specific events or conditions, it is a trigger. If the trigger is controlled by hardware, it's a hw control trigger. That's why I propose using a private hw control trigger to represent this to make it semantically correct.
Ah. I think it will be confusing for most users. They're not going to think of it as a trigger (that's my guess anyway)
I guess most users simply tune the keyboard backlight via desktop environments, so it won't directly make them confused. When it comes to desktop environments, this is precisely why we want the interface capable to notify userspace about HW status transition.
And both
cros_ec' andturris-omnia' have been using private hw control triggers to represent auto mode.Quoted 12 lines
Quoted 11 lines
[...]
I admit that my proposal is complicated and may need a lot of time to make it into its right path. It may even be rejected by LED folks. But it's the best approach I can think of considering our requirements on the interface:
1. It shouldn't break any existing interfaces.
2. It's exposed to userspace for getting or setting its status.
3. HW status transition should reach userspace (similar toLED_BRIGHT_HW_CHANGED).Quoted 6 lines
Quoted 2 lines
Just to check - for #3 do you mean it should report the brightness changes when it's in auto mode (i.e. if it got brighter or dimmer); or if it should just report it switched in/out of auto mode. I don't think we need to report every brightness status change - and switching modes should be user directed so is no different to currently. Am I missing something?
I meant auto-brightness on <-> off, or fixed-brightness-value <-> ALS in Hans' words.
great - makes sense
Quoted 31 lines
Quoted 2 lines
Thanks
MarkTo conclude:
1. My proposal:
Upsides:
- mutually exclusive with other triggers (hence less chaos)
- semantic correctness
- extensibility (through trigger attributes)
- acts as an aggregate switch to turn on/off hw controlDownsides:
- complexity
- needs approval from LED folks
- (I admit both are major blockers...)2. Device attribute
Upsides:
- simplicity, KISS
- no need to touch LED core
- extensible as long as it has a sensor-neutral nameDownsides:
- must have zero influence on the brightness_set[_blocking] callback in order not to break triggers
- potential interference with triggers and the brightness attribute
- weird semantic
Quoted 9 lines
Quoted 4 lines
I am actually OK with both approaches. If you still consider the device attribute approach is better after reading my concerns, I will no longer insist on my proposal.
My (limited) vote is for the simplest solution - because it's just a keyboard light. But I'll defer to Hans here - I don't think I know enough to have a strong opinion.
Hi Hans - do you have an opinion on the path forwards? Would appreciate your expert guidance here :)
Mark