leds: class: Support custom sysfs groups on multicolor and flash LEDs
v4 cover leds: class: Support custom sysfs groups on multicolor and flash LEDs
Until now drivers are unable to atomically register custom sysfs groups below multicolor and flash LED class devices, because the led_cdev->sysfs_groups field is being overwritten inside the associated initialization functions. This patch series tries to fix this.
The first three patches constify the sysfs attributes of all three LED flavors, while the next four patches fix various unrelated issues affecting LED device initialization. The next two patches migrate the multicolor and flash LEDs to use struct device_type for registering their sysfs attributes, so that led_cdev->sysfs_groups is not touched anymore. The last patch contains a minor optimization for multicolor LEDs.
All patches have been tested on real hardware, with the flash LED interface being tested using a custom kernel module.
Changes since v3:
- add Reviewed-by tag for Rong Zhang
- drop patch for the KTD202X driver (issue was already fixed)
- fix checkpatch issues
- replace if (ret < 0) with if (ret)
Changes since v2:
- fix issue inside KTD202X driver
Armin Wolf (10):
leds: class: Constify sysfs attributes
leds: multicolor: Constify sysfs attributes
leds: flash: Constify sysfs attributes
leds: class: Create "brightness_hw_changed" attribute atomically
leds: class: Fix race condition when creating the device
leds: class: Access brightness_hw_changed exactly once
leds: class: Use correct type for brightness_hw_changed field
leds: class: Split led_classdev_register_ext()
leds: Support custom sysfs groups on multicolor and flash LEDs
leds: multicolor: Adjust maximum subled intensity during
initialization
drivers/leds/led-class-flash.c | 159 ++++++++++--------- drivers/leds/led-class-multicolor.c | 78 +++++----- drivers/leds/led-class.c | 233 +++++++++++++++++++--------- drivers/leds/leds.h | 3 + include/linux/led-class-flash.h | 5 - include/linux/leds.h | 2 +- 6 files changed, 284 insertions(+), 196 deletions(-)
Signature · 1 line
2.56.1
PATCH v4 1/10 leds: class: Constify sysfs attributes
Mark all sysfs attributes as const to ensure that they are placed in readonly memory to prevent accidental/malicious modification.
--- drivers/leds/led-class.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-)
drivers/leds/led-class.c +15 -14
@@ -27,8 +27,7 @@ static LIST_HEAD(leds_lookup_list);static struct workqueue_struct *leds_wq; -static ssize_t brightness_show (struct device *dev, -struct device_attribute *attr,char *buf) +static ssize_t brightness_show (struct device *dev,const struct device_attribute *attr,char *buf) {struct led_classdev *led_cdev =dev_get_drvdata (dev);unsigned int brightness;
@@ -44,8 +43,8 @@ static ssize_t brightness_show(struct device *dev,return sysfs_emit (buf,"%u\n" , brightness); } -static ssize_t brightness_store (struct device *dev, -struct device_attribute *attr,const char *buf,size_t size) +static ssize_t brightness_store (struct device *dev,const struct device_attribute *attr, +const char *buf,size_t size) {struct led_classdev *led_cdev =dev_get_drvdata (dev);unsigned long state;
@@ -74,10 +73,11 @@ static ssize_t brightness_store(struct device *dev,mutex_unlock (&led_cdev->led_access);return ret; } -static DEVICE_ATTR_RW (brightness); -static ssize_t max_brightness_show (struct device *dev, -struct device_attribute *attr,char *buf) +static const DEVICE_ATTR_RW (brightness); + +static ssize_t max_brightness_show (struct device *dev,const struct device_attribute *attr, +char *buf) {struct led_classdev *led_cdev =dev_get_drvdata (dev);unsigned int max_brightness;
@@ -88,19 +88,20 @@ static ssize_t max_brightness_show(struct device *dev,return sysfs_emit (buf,"%u\n" , max_brightness); } -static DEVICE_ATTR_RO (max_brightness); -static struct attribute *led_class_attrs[] = { +static const DEVICE_ATTR_RO (max_brightness); + +static const struct attribute *const led_class_attrs[] = { &dev_attr_brightness.attr, &dev_attr_max_brightness.attr,NULL , };static const struct attribute_group led_group = { - .attrs = led_class_attrs, + .attrs_const = led_class_attrs, }; -static const struct attribute_group *led_groups[] = { +static const struct attribute_group *const led_groups[] = { &led_group, #ifdef CONFIG_LEDS_TRIGGERS &led_trigger_group,
@@ -109,8 +110,8 @@ static const struct attribute_group *led_groups[] = { };#ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED -static ssize_t brightness_hw_changed_show (struct device *dev, -struct device_attribute *attr,char *buf) +static ssize_t brightness_hw_changed_show (struct device *dev,const struct device_attribute *attr, +char *buf) {struct led_classdev *led_cdev =dev_get_drvdata (dev);
@@ -120,7 +121,7 @@ static ssize_t brightness_hw_changed_show(struct device *dev,return sysfs_emit (buf,"%u\n" , led_cdev->brightness_hw_changed); } -static DEVICE_ATTR_RO (brightness_hw_changed); +static const DEVICE_ATTR_RO (brightness_hw_changed);static int led_add_brightness_hw_changed (struct led_classdev *led_cdev) {
Signature · 1 line
2.56.1
PATCH v4 2/10 leds: multicolor: Constify sysfs attributes
In reply to Armin WolfMark all sysfs attributes as const to ensure that they are placed in readonly memory to prevent accidental/malicious modification.
--- drivers/leds/led-class-multicolor.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-)
drivers/leds/led-class-multicolor.c +13 -11
@@ -10,6 +10,7 @@#include <linux/minmax.h> #include <linux/module.h> #include <linux/slab.h> +#include <linux/sysfs.h> #include <linux/uaccess.h> static unsigned int led_mc_get_max_intensity (struct led_classdev_mc *mcled_cdev,size_t index)
@@ -47,7 +48,7 @@ int led_mc_calc_color_components(struct led_classdev_mc *mcled_cdev,EXPORT_SYMBOL_GPL (led_mc_calc_color_components);static ssize_t multi_max_intensity_show (struct device *dev, -struct device_attribute *intensity_attr,char *buf) +const struct device_attribute *intensity_attr,char *buf) {struct led_classdev *led_cdev =dev_get_drvdata (dev);struct led_classdev_mc *mcled_cdev =lcdev_to_mccdev (led_cdev);
@@ -64,11 +65,12 @@ static ssize_t multi_max_intensity_show(struct device *dev, buf[len++] ='\n' ;return len; } -static DEVICE_ATTR_RO (multi_max_intensity); + +static const DEVICE_ATTR_RO (multi_max_intensity);static ssize_t multi_intensity_store (struct device *dev, -struct device_attribute *intensity_attr, -const char *buf,size_t size) +const struct device_attribute *intensity_attr,const char *buf, +size_t size) {struct led_classdev *led_cdev =dev_get_drvdata (dev);struct led_classdev_mc *mcled_cdev =lcdev_to_mccdev (led_cdev);
@@ -110,8 +112,7 @@ static ssize_t multi_intensity_store(struct device *dev, }static ssize_t multi_intensity_show (struct device *dev, -struct device_attribute *intensity_attr, -char *buf) +const struct device_attribute *intensity_attr,char *buf) {struct led_classdev *led_cdev =dev_get_drvdata (dev);struct led_classdev_mc *mcled_cdev =lcdev_to_mccdev (led_cdev);
@@ -128,11 +129,11 @@ static ssize_t multi_intensity_show(struct device *dev, buf[len++] ='\n' ;return len; } -static DEVICE_ATTR_RW (multi_intensity); + +static const DEVICE_ATTR_RW (multi_intensity);static ssize_t multi_index_show (struct device *dev, -struct device_attribute *multi_index_attr, -char *buf) +const struct device_attribute *multi_index_attr,char *buf) {struct led_classdev *led_cdev =dev_get_drvdata (dev);struct led_classdev_mc *mcled_cdev =lcdev_to_mccdev (led_cdev);
@@ -150,9 +151,10 @@ static ssize_t multi_index_show(struct device *dev, buf[len++] ='\n' ;return len; } -static DEVICE_ATTR_RO (multi_index); -static struct attribute *led_multicolor_attrs[] = { +static const DEVICE_ATTR_RO (multi_index); + +static const struct attribute *const led_multicolor_attrs[] = { &dev_attr_multi_max_intensity.attr, &dev_attr_multi_intensity.attr, &dev_attr_multi_index.attr,
Signature · 1 line
2.56.1
PATCH v4 3/10 leds: flash: Constify sysfs attributes
In reply to Armin WolfMark all sysfs attributes as const to ensure that they are placed in readonly memory to prevent accidental/malicious modification.
--- drivers/leds/led-class-flash.c | 68 ++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 31 deletions(-)
drivers/leds/led-class-flash.c +37 -31
@@ -12,6 +12,7 @@#include <linux/leds.h> #include <linux/module.h> #include <linux/slab.h> +#include <linux/sysfs.h> #define has_flash_op (fled_cdev, op) \ (fled_cdev && fled_cdev->ops->op)
@@ -34,7 +35,8 @@ static const char * const led_flash_fault_names[] = { };static ssize_t flash_brightness_store (struct device *dev, -struct device_attribute *attr,const char *buf,size_t size) +const struct device_attribute *attr,const char *buf, +size_t size) {struct led_classdev *led_cdev =dev_get_drvdata (dev);struct led_classdev_flash *fled_cdev =lcdev_to_flcdev (led_cdev);
@@ -62,8 +64,8 @@ static ssize_t flash_brightness_store(struct device *dev,return ret; } -static ssize_t flash_brightness_show (struct device *dev, -struct device_attribute *attr,char *buf) +static ssize_t flash_brightness_show (struct device *dev,const struct device_attribute *attr, +char *buf) {struct led_classdev *led_cdev =dev_get_drvdata (dev);struct led_classdev_flash *fled_cdev =lcdev_to_flcdev (led_cdev);
@@ -73,20 +75,22 @@ static ssize_t flash_brightness_show(struct device *dev,return sysfs_emit (buf,"%u\n" , fled_cdev->brightness.val); } -static DEVICE_ATTR_RW (flash_brightness); -static ssize_t max_flash_brightness_show (struct device *dev, -struct device_attribute *attr,char *buf) +static const DEVICE_ATTR_RW (flash_brightness); + +static ssize_t max_flash_brightness_show (struct device *dev,const struct device_attribute *attr, +char *buf) {struct led_classdev *led_cdev =dev_get_drvdata (dev);struct led_classdev_flash *fled_cdev =lcdev_to_flcdev (led_cdev);return sysfs_emit (buf,"%u\n" , fled_cdev->brightness.max); } -static DEVICE_ATTR_RO (max_flash_brightness); -static ssize_t flash_strobe_store (struct device *dev, -struct device_attribute *attr,const char *buf,size_t size) +static const DEVICE_ATTR_RO (max_flash_brightness); + +static ssize_t flash_strobe_store (struct device *dev,const struct device_attribute *attr, +const char *buf,size_t size) {struct led_classdev *led_cdev =dev_get_drvdata (dev);struct led_classdev_flash *fled_cdev =lcdev_to_flcdev (led_cdev);
@@ -116,8 +120,7 @@ static ssize_t flash_strobe_store(struct device *dev,return ret; } -static ssize_t flash_strobe_show (struct device *dev, -struct device_attribute *attr,char *buf) +static ssize_t flash_strobe_show (struct device *dev,const struct device_attribute *attr,char *buf) {struct led_classdev *led_cdev =dev_get_drvdata (dev);struct led_classdev_flash *fled_cdev =lcdev_to_flcdev (led_cdev);
@@ -131,10 +134,11 @@ static ssize_t flash_strobe_show(struct device *dev,return sysfs_emit (buf,"%u\n" , state); } -static DEVICE_ATTR_RW (flash_strobe); -static ssize_t flash_timeout_store (struct device *dev, -struct device_attribute *attr,const char *buf,size_t size) +static const DEVICE_ATTR_RW (flash_strobe); + +static ssize_t flash_timeout_store (struct device *dev,const struct device_attribute *attr, +const char *buf,size_t size) {struct led_classdev *led_cdev =dev_get_drvdata (dev);struct led_classdev_flash *fled_cdev =lcdev_to_flcdev (led_cdev);
@@ -162,28 +166,29 @@ static ssize_t flash_timeout_store(struct device *dev,return ret; } -static ssize_t flash_timeout_show (struct device *dev, -struct device_attribute *attr,char *buf) +static ssize_t flash_timeout_show (struct device *dev,const struct device_attribute *attr, +char *buf) {struct led_classdev *led_cdev =dev_get_drvdata (dev);struct led_classdev_flash *fled_cdev =lcdev_to_flcdev (led_cdev);return sysfs_emit (buf,"%u\n" , fled_cdev->timeout.val); } -static DEVICE_ATTR_RW (flash_timeout); -static ssize_t max_flash_timeout_show (struct device *dev, -struct device_attribute *attr,char *buf) +static const DEVICE_ATTR_RW (flash_timeout); + +static ssize_t max_flash_timeout_show (struct device *dev,const struct device_attribute *attr, +char *buf) {struct led_classdev *led_cdev =dev_get_drvdata (dev);struct led_classdev_flash *fled_cdev =lcdev_to_flcdev (led_cdev);return sysfs_emit (buf,"%u\n" , fled_cdev->timeout.max); } -static DEVICE_ATTR_RO (max_flash_timeout); -static ssize_t flash_fault_show (struct device *dev, -struct device_attribute *attr,char *buf) +static const DEVICE_ATTR_RO (max_flash_timeout); + +static ssize_t flash_fault_show (struct device *dev,const struct device_attribute *attr,char *buf) {struct led_classdev *led_cdev =dev_get_drvdata (dev);struct led_classdev_flash *fled_cdev =lcdev_to_flcdev (led_cdev);
@@ -205,44 +210,45 @@ static ssize_t flash_fault_show(struct device *dev, ret +=sysfs_emit_at (buf, ret,"\n" );return ret; } -static DEVICE_ATTR_RO (flash_fault); -static struct attribute *led_flash_strobe_attrs[] = { +static const DEVICE_ATTR_RO (flash_fault); + +static const struct attribute *const led_flash_strobe_attrs[] = { &dev_attr_flash_strobe.attr,NULL , }; -static struct attribute *led_flash_timeout_attrs[] = { +static const struct attribute *const led_flash_timeout_attrs[] = { &dev_attr_flash_timeout.attr, &dev_attr_max_flash_timeout.attr,NULL , }; -static struct attribute *led_flash_brightness_attrs[] = { +static const struct attribute *const led_flash_brightness_attrs[] = { &dev_attr_flash_brightness.attr, &dev_attr_max_flash_brightness.attr,NULL , }; -static struct attribute *led_flash_fault_attrs[] = { +static const struct attribute *const led_flash_fault_attrs[] = { &dev_attr_flash_fault.attr,NULL , };static const struct attribute_group led_flash_strobe_group = { - .attrs = led_flash_strobe_attrs, + .attrs_const = led_flash_strobe_attrs, };static const struct attribute_group led_flash_timeout_group = { - .attrs = led_flash_timeout_attrs, + .attrs_const = led_flash_timeout_attrs, };static const struct attribute_group led_flash_brightness_group = { - .attrs = led_flash_brightness_attrs, + .attrs_const = led_flash_brightness_attrs, };static const struct attribute_group led_flash_fault_group = { - .attrs = led_flash_fault_attrs, + .attrs_const = led_flash_fault_attrs, };static void led_flash_resume (struct led_classdev *led_cdev)
Signature · 1 line
2.56.1
PATCH v4 4/10 leds: class: Create "brightness_hw_changed" attribute atomically
In reply to Armin WolfUsing device_create_file() might cause userspace applications listening for uevents to inspect a new LED device before the kernel has registered all sysfs attributes for it. This might cause those applications to incorrectly assume that the new LED device does not support the "brightness_hw_changed" attribute.
Fix this by letting the device core create the sysfs attribute before the uevent is being send to userspace.
--- drivers/leds/led-class.c | 58 ++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 20 deletions(-)
drivers/leds/led-class.c +38 -20
@@ -17,6 +17,7 @@#include <linux/property.h> #include <linux/slab.h> #include <linux/spinlock.h> +#include <linux/sysfs.h> #include <linux/timer.h> #include <uapi/linux/uleds.h> #include <linux/of.h>
@@ -101,14 +102,6 @@ static const struct attribute_group led_group = { .attrs_const = led_class_attrs, }; -static const struct attribute_group *const led_groups[] = { - &led_group, -#ifdef CONFIG_LEDS_TRIGGERS - &led_trigger_group, -#endif -NULL , -}; -#ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED static ssize_t brightness_hw_changed_show (struct device *dev,const struct device_attribute *attr,char *buf)
@@ -123,22 +116,48 @@ static ssize_t brightness_hw_changed_show(struct device *dev, const struct devicstatic const DEVICE_ATTR_RO (brightness_hw_changed); -static int led_add_brightness_hw_changed (struct led_classdev *led_cdev) +static const struct attribute *const led_brightness_hw_changed_attrs[] = { + &dev_attr_brightness_hw_changed.attr, +NULL , +}; + +static umode_t led_brightness_hw_changed_is_visible (struct kobject *kobj, +const struct attribute *attr,int n) { -struct device *dev = led_cdev->dev; -int ret; +struct device *dev =kobj_to_dev (kobj); +struct led_classdev *led_cdev =dev_get_drvdata (dev); - ret =device_create_file (dev, &dev_attr_brightness_hw_changed); -if (ret) { -dev_err (dev,"Error creating brightness_hw_changed\n" ); -return ret; - } +if (attr == &dev_attr_brightness_hw_changed.attr && + led_cdev->flags &LED_BRIGHT_HW_CHANGED ) +return attr->mode; + +return 0 ; +} +static const struct attribute_group led_brightness_hw_changed_group = { + .is_visible_const = led_brightness_hw_changed_is_visible, + .attrs_const = led_brightness_hw_changed_attrs, +}; +#endif /* CONFIG_LEDS_BRIGHTNESS_HW_CHANGED */ + +static const struct attribute_group *const led_groups[] = { + &led_group, +#ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED + &led_brightness_hw_changed_group, +#endif +#ifdef CONFIG_LEDS_TRIGGERS + &led_trigger_group, +#endif +NULL , +}; + +#ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED +static int led_add_brightness_hw_changed (struct led_classdev *led_cdev) +{ led_cdev->brightness_hw_changed_kn = -sysfs_get_dirent (dev->kobj.sd,"brightness_hw_changed" ); +sysfs_get_dirent (led_cdev->dev->kobj.sd,"brightness_hw_changed" );if (!led_cdev->brightness_hw_changed_kn) { -dev_err (dev,"Error getting brightness_hw_changed kn\n" ); -device_remove_file (dev, &dev_attr_brightness_hw_changed); +dev_err (led_cdev->dev,"Error getting brightness_hw_changed kn\n" );return -ENXIO ; }
@@ -148,7 +167,6 @@ static int led_add_brightness_hw_changed(struct led_classdev *led_cdev)static void led_remove_brightness_hw_changed (struct led_classdev *led_cdev) {sysfs_put (led_cdev->brightness_hw_changed_kn); -device_remove_file (led_cdev->dev, &dev_attr_brightness_hw_changed); }void led_classdev_notify_brightness_hw_changed (struct led_classdev *led_cdev,unsigned int brightness)
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH v4 04/10] leds: class: Create "brightness_hw_changed" attribute atomically
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] leds: class: missing Fixes trailer for brightness_hw_changed uevent race --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] leds: class: missing Fixes trailer for brightness_hw_changed uevent race
--
commit fd767e19e935bd5ec83057573f0adba8e2d1715e
Author: Armin Wolf <[email protected]>
Quoted 8 lines
leds: class: Create "
brightness_hw_changed" attribute atomicallyUsing
device_create_file()might cause userspace applications listening for uevents to inspect a new LED device before the kernel has registered all sysfs attributes for it. This might cause those applications to incorrectly assume that the new LED device does not support the "brightness_hw_changed" attribute.
Quoted 5 lines
Fix this by letting the device core create the sysfs attribute before the uevent is being send to userspace.
Reviewed-by Rong Zhang <[email protected]>Signed-off-by Armin Wolf <[email protected]>
[Severity: Low]
This fixes a uevent race introduced when brightness_hw_changed was added via device_create_file() after device registration. Should this include:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v4 5/10 leds: class: Fix race condition when creating the device
In reply to Armin WolfUserspace applications can access the sysfs attributes as soon as device_create_with_groups() has returned. Two of those sysfs attributes ("brightness_hw_changed" and "max_brightness") access fields that are not properly initialized at this point, with only "max_brightness" guarding against this by taking the LED access lock.
Fix the race condition affecting "brightness_hw_changed" by creating the device after the associated fields have been properly initialized. This also allows us to drop the LED access lock inside max_brightness_show(), as the maximum brightness does not change after initialization.
--- drivers/leds/led-class.c | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-)
drivers/leds/led-class.c +14 -18
@@ -81,13 +81,8 @@ static ssize_t max_brightness_show(struct device *dev, const struct device_attri char *buf) {struct led_classdev *led_cdev =dev_get_drvdata (dev); -unsigned int max_brightness; -mutex_lock (&led_cdev->led_access); - max_brightness = led_cdev->max_brightness; -mutex_unlock (&led_cdev->led_access); - -return sysfs_emit (buf,"%u\n" , max_brightness); +return sysfs_emit (buf,"%u\n" , led_cdev->max_brightness); }static const DEVICE_ATTR_RO (max_brightness);
@@ -555,6 +550,18 @@ int led_classdev_register_ext(struct device *parent,init_rwsem (&led_cdev->trigger_lock);#endif mutex_init (&led_cdev->led_access); + led_cdev->work_flags =0 ; +#ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED + led_cdev->brightness_hw_changed =-1 ; +#endif +#ifdef CONFIG_LEDS_TRIGGERS_HW_CHANGED +if (led_cdev->flags &LED_TRIG_HW_CHANGED ) +INIT_WORK (&led_cdev->trigger_hw_changed_work, + led_trigger_hw_control_changed_worker); +#endif +if (!led_cdev->max_brightness) + led_cdev->max_brightness =LED_FULL ; +mutex_lock (&led_cdev->led_access); led_cdev->dev =device_create_with_groups (&leds_class, parent,0 , led_cdev, led_cdev->groups,"%s" , final_name);
@@ -562,6 +569,7 @@ int led_classdev_register_ext(struct device *parent,mutex_unlock (&led_cdev->led_access);return PTR_ERR (led_cdev->dev); } +if (init_data && init_data->fwnode)device_set_node (led_cdev->dev, init_data->fwnode);
@@ -576,18 +584,6 @@ int led_classdev_register_ext(struct device *parent, } } - led_cdev->work_flags =0 ; -#ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED - led_cdev->brightness_hw_changed =-1 ; -#endif -#ifdef CONFIG_LEDS_TRIGGERS_HW_CHANGED -if (led_cdev->flags &LED_TRIG_HW_CHANGED ) -INIT_WORK (&led_cdev->trigger_hw_changed_work, - led_trigger_hw_control_changed_worker); -#endif -if (!led_cdev->max_brightness) - led_cdev->max_brightness =LED_FULL ; -led_update_brightness (led_cdev); led_cdev->wq = leds_wq;
Signature · 1 line
2.56.1
PATCH v4 6/10 leds: class: Access brightness_hw_changed exactly once
In reply to Armin WolfReading the brightness_hw_changed field needs to take care against concurrent writes by led_classdev_notify_brightness_hw_changed(). Use WRITE_ONCE()/READ_ONCE() to ensure that the value does not get corrupted by concurrent writes.
--- drivers/leds/led-class.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/leds/led-class.c +5 -3
@@ -102,11 +102,13 @@ static ssize_t brightness_hw_changed_show(struct device *dev, const struct devic char *buf) {struct led_classdev *led_cdev =dev_get_drvdata (dev); +unsigned int brightness_hw_changed; -if (led_cdev->brightness_hw_changed ==-1 ) + brightness_hw_changed =READ_ONCE (led_cdev->brightness_hw_changed); +if (brightness_hw_changed ==-1 )return -ENODATA ; -return sysfs_emit (buf,"%u\n" , led_cdev->brightness_hw_changed); +return sysfs_emit (buf,"%u\n" , brightness_hw_changed); }static const DEVICE_ATTR_RO (brightness_hw_changed);
@@ -169,7 +171,7 @@ void led_classdev_notify_brightness_hw_changed(struct led_classdev *led_cdev, unif (WARN_ON (!led_cdev->brightness_hw_changed_kn))return ; - led_cdev->brightness_hw_changed = brightness; +WRITE_ONCE (led_cdev->brightness_hw_changed, brightness);sysfs_notify_dirent (led_cdev->brightness_hw_changed_kn); }EXPORT_SYMBOL_GPL (led_classdev_notify_brightness_hw_changed);
Signature · 1 line
2.56.1
PATCH v4 7/10 leds: class: Use correct type for brightness_hw_changed field
In reply to Armin WolfDevice drivers can supply a unsigned integer using led_classdev_notify_brightness_hw_changed(), but the associated field can only store signed integers, leading to issues for very large brightness values.
Fix this by using the correct type for the brightness_hw_changed field.
--- include/linux/leds.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/leds.h +1 -1
@@ -236,7 +236,7 @@ struct led_classdev {#endif #ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED -int brightness_hw_changed; +unsigned int brightness_hw_changed;struct kernfs_node *brightness_hw_changed_kn;#endif
Signature · 1 line
2.56.1
PATCH v4 8/10 leds: class: Split led_classdev_register_ext()
In reply to Armin WolfSplit led_classdev_register_ext() into two separate internal functions to prepare for future changes around LED class device initialization.
--- drivers/leds/led-class.c | 102 ++++++++++++++++++++++++++++++++------- drivers/leds/leds.h | 3 ++ 2 files changed, 87 insertions(+), 18 deletions(-)
drivers/leds/led-class.c +84 -18
@@ -253,9 +253,15 @@ static struct led_classdev *led_module_get(struct device *led_dev)return led_cdev; } +static void led_classdev_release (struct device *dev) +{ +kfree (dev); +} +static const struct class leds_class = { .name ="leds" , .dev_groups = led_groups, + .dev_release = led_classdev_release, .pm = &leds_class_dev_pm_ops, };
@@ -490,16 +496,19 @@ static int led_classdev_next_name(const char *init_name, char *name, }/** -* led_classdev_register_ext - register a new object of led_classdev class -* with init data. -* -* @parent: parent of LED device +* led_classdev_init - Initialize a new LED class device. +* @parent: parent of LED device. * @led_cdev: the led_classdev structure for this device. -* @init_data: LED class device initialization data +* @init_data: LED class device initialization data. +* +* Initializes a new LED class device, but does not register with the +* subsystem yet. Useful for performing additional initialization before +* adding the LED class device to the subsystem using led_classdev_add(). +* +* Return: Zero on success or a negative error code on failure. */ -int led_classdev_register_ext (struct device *parent, -struct led_classdev *led_cdev, -struct led_init_data *init_data) +int led_classdev_init (struct device *parent,struct led_classdev *led_cdev, +struct led_init_data *init_data) {char composed_name[LED_MAX_NAME_SIZE ];char final_name[LED_MAX_NAME_SIZE ];
@@ -564,12 +573,52 @@ int led_classdev_register_ext(struct device *parent,if (!led_cdev->max_brightness) led_cdev->max_brightness =LED_FULL ; + led_cdev->dev =kzalloc_obj (*led_cdev->dev); +if (!led_cdev->dev) +return -ENOMEM ; + +device_initialize (led_cdev->dev); + led_cdev->dev->class = &leds_class; + led_cdev->dev->parent = parent; + led_cdev->dev->groups = led_cdev->groups; +dev_set_drvdata (led_cdev->dev, led_cdev); + ret =kobject_set_name (&led_cdev->dev->kobj,"%s" , final_name); +if (ret) { +put_device (led_cdev->dev); + led_cdev->dev =NULL ; +return ret; + } + + led_cdev->wq = leds_wq; + +led_init_core (led_cdev); + +return 0 ; +} +EXPORT_SYMBOL_GPL (led_classdev_init); + +/** +* led_classdev_add - Add a new LED class device to the subsystem. +* @led_cdev: the led_classdev structure for this device. +* @init_data: LED class device initialization data. +* +* Adds a new LED class device to the subsystem, allowing userspace applications +* and/or kernel consumers to access it. +* +* Returns: Zero on success or a negative error code on failure. +*/ +int led_classdev_add (struct led_classdev *led_cdev,struct led_init_data *init_data) +{ +int ret; +mutex_lock (&led_cdev->led_access); - led_cdev->dev =device_create_with_groups (&leds_class, parent,0 , - led_cdev, led_cdev->groups,"%s" , final_name); -if (IS_ERR (led_cdev->dev)) { + + ret =device_add (led_cdev->dev); +if (ret) {mutex_unlock (&led_cdev->led_access); -return PTR_ERR (led_cdev->dev); +put_device (led_cdev->dev); + led_cdev->dev =NULL ; +return ret; }if (init_data && init_data->fwnode)
@@ -588,10 +637,6 @@ int led_classdev_register_ext(struct device *parent,led_update_brightness (led_cdev); - led_cdev->wq = leds_wq; - -led_init_core (led_cdev); -/* add to the list of leds */ down_write (&leds_list_lock);list_add_tail (&led_cdev->node, &leds_list);
@@ -603,11 +648,32 @@ int led_classdev_register_ext(struct device *parent,mutex_unlock (&led_cdev->led_access); -dev_dbg (parent,"Registered led device: %s\n" , - led_cdev->name); +dev_dbg (led_cdev->dev->parent,"Registered led device: %s\n" , led_cdev->name);return 0 ; } +EXPORT_SYMBOL_GPL (led_classdev_add); + +/** +* led_classdev_register_ext - register a new object of led_classdev class +* with init data. +* +* @parent: parent of LED device. +* @led_cdev: the led_classdev structure for this device. +* @init_data: LED class device initialization data. +*/ +int led_classdev_register_ext (struct device *parent, +struct led_classdev *led_cdev, +struct led_init_data *init_data) +{ +int ret; + + ret =led_classdev_init (parent, led_cdev, init_data); +if (ret) +return ret; + +return led_classdev_add (led_cdev, init_data); +}EXPORT_SYMBOL_GPL (led_classdev_register_ext);/**
drivers/leds/leds.h +3 -0
@@ -18,6 +18,9 @@ static inline int led_get_brightness(struct led_classdev *led_cdev) }void led_init_core (struct led_classdev *led_cdev); +int led_classdev_init (struct device *parent,struct led_classdev *led_cdev, +struct led_init_data *init_data); +int led_classdev_add (struct led_classdev *led_cdev,struct led_init_data *init_data);void led_stop_software_blink (struct led_classdev *led_cdev);void led_set_brightness_nopm (struct led_classdev *led_cdev,unsigned int value);void led_set_brightness_nosleep (struct led_classdev *led_cdev,unsigned int value);
Signature · 1 line
2.56.1
PATCH v4 9/10 leds: Support custom sysfs groups on multicolor and flash LEDs
In reply to Armin WolfDrivers registering multicolor/flash LEDs currently cannot use led_cdev->groups due to said field being overwritten by the multicolor/flash LED registration functions. Fix this by assigning each LED type a separate device type ("multicolor" and "flash") that carries the sysfs attributes instead.
Also assign a device type for standard LEDs ("standard") for symmetry.
--- drivers/leds/led-class-flash.c | 107 +++++++++++++--------------- drivers/leds/led-class-multicolor.c | 15 +++- drivers/leds/led-class.c | 6 ++ include/linux/led-class-flash.h | 5 -- 4 files changed, 68 insertions(+), 65 deletions(-)
drivers/leds/led-class-flash.c +49 -58
@@ -13,6 +13,7 @@#include <linux/module.h> #include <linux/slab.h> #include <linux/sysfs.h> +#include "leds.h" #define has_flash_op (fled_cdev, op) \ (fled_cdev && fled_cdev->ops->op)
@@ -213,42 +214,56 @@ static ssize_t flash_fault_show(struct device *dev, const struct device_attributstatic const DEVICE_ATTR_RO (flash_fault); -static const struct attribute *const led_flash_strobe_attrs[] = { +static const struct attribute *const led_flash_attrs[] = { &dev_attr_flash_strobe.attr, -NULL , -}; - -static const struct attribute *const led_flash_timeout_attrs[] = { &dev_attr_flash_timeout.attr, &dev_attr_max_flash_timeout.attr, -NULL , -}; - -static const struct attribute *const led_flash_brightness_attrs[] = { &dev_attr_flash_brightness.attr, &dev_attr_max_flash_brightness.attr, -NULL , -}; - -static const struct attribute *const led_flash_fault_attrs[] = { &dev_attr_flash_fault.attr,NULL , }; -static const struct attribute_group led_flash_strobe_group = { - .attrs_const = led_flash_strobe_attrs, -}; +static umode_t led_flash_is_visible (struct kobject *kobj,const struct attribute *attr,int n) +{ +struct device *dev =kobj_to_dev (kobj); +struct led_classdev *led_cdev =dev_get_drvdata (dev); +struct led_classdev_flash *fled_cdev =lcdev_to_flcdev (led_cdev); + +if (attr == &dev_attr_flash_strobe.attr) +return attr->mode; + +if (attr == &dev_attr_flash_timeout.attr && fled_cdev->ops->timeout_set) +return attr->mode; -static const struct attribute_group led_flash_timeout_group = { - .attrs_const = led_flash_timeout_attrs, +if (attr == &dev_attr_max_flash_timeout.attr && fled_cdev->ops->timeout_set) +return attr->mode; + +if (attr == &dev_attr_flash_brightness.attr && fled_cdev->ops->flash_brightness_set) +return attr->mode; + +if (attr == &dev_attr_max_flash_brightness.attr && fled_cdev->ops->flash_brightness_set) +return attr->mode; + +if (attr == &dev_attr_flash_fault.attr && fled_cdev->ops->fault_get) +return attr->mode; + +return 0 ; +} + +static const struct attribute_group led_flash_group = { + .is_visible_const = led_flash_is_visible, + .attrs_const = led_flash_attrs, }; -static const struct attribute_group led_flash_brightness_group = { - .attrs_const = led_flash_brightness_attrs, +static const struct attribute_group *led_flash_groups[] = { + &led_flash_group, +NULL , }; -static const struct attribute_group led_flash_fault_group = { - .attrs_const = led_flash_fault_attrs, +static const struct device_type led_flash_device_type = { + .name ="flash" , + .groups = led_flash_groups, };static void led_flash_resume (struct led_classdev *led_cdev)
@@ -260,30 +275,6 @@ static void led_flash_resume(struct led_classdev *led_cdev)call_flash_op (fled_cdev, timeout_set, fled_cdev->timeout.val); } -static void led_flash_init_sysfs_groups (struct led_classdev_flash *fled_cdev) -{ -struct led_classdev *led_cdev = &fled_cdev->led_cdev; -const struct led_flash_ops *ops = fled_cdev->ops; -const struct attribute_group **flash_groups = fled_cdev->sysfs_groups; - -int num_sysfs_groups =0 ; - - flash_groups[num_sysfs_groups++] = &led_flash_strobe_group; - -if (ops->flash_brightness_set) - flash_groups[num_sysfs_groups++] = &led_flash_brightness_group; - -if (ops->timeout_set) - flash_groups[num_sysfs_groups++] = &led_flash_timeout_group; - -if (ops->fault_get) - flash_groups[num_sysfs_groups++] = &led_flash_fault_group; - - flash_groups[num_sysfs_groups] =NULL ; - - led_cdev->groups = flash_groups; -} -int led_classdev_flash_register_ext (struct device *parent,struct led_classdev_flash *fled_cdev,struct led_init_data *init_data)
@@ -297,26 +288,26 @@ int led_classdev_flash_register_ext(struct device *parent, led_cdev = &fled_cdev->led_cdev; -if (led_cdev->flags &LED_DEV_CAP_FLASH ) { -if (!led_cdev->brightness_set_blocking) -return -EINVAL ; +if (!(led_cdev->flags &LED_DEV_CAP_FLASH )) +return led_classdev_register_ext (parent, led_cdev, init_data); - ops = fled_cdev->ops; -if (!ops || !ops->strobe_set) -return -EINVAL ; +if (!led_cdev->brightness_set_blocking) +return -EINVAL ; - led_cdev->flash_resume = led_flash_resume; + ops = fled_cdev->ops; +if (!ops || !ops->strobe_set) +return -EINVAL ; -/* Select the sysfs attributes to be created for the device */ -led_flash_init_sysfs_groups (fled_cdev); - } + led_cdev->flash_resume = led_flash_resume;/* Register led class device */ - ret =led_classdev_register_ext (parent, led_cdev, init_data); + ret =led_classdev_init (parent, led_cdev, init_data);if (ret <0 )return ret; -return 0 ; + led_cdev->dev->type = &led_flash_device_type; + +return led_classdev_add (led_cdev, init_data); }EXPORT_SYMBOL_GPL (led_classdev_flash_register_ext);
drivers/leds/led-class-multicolor.c +13 -2
@@ -12,6 +12,7 @@#include <linux/slab.h> #include <linux/sysfs.h> #include <linux/uaccess.h> +#include "leds.h" static unsigned int led_mc_get_max_intensity (struct led_classdev_mc *mcled_cdev,size_t index) {
@@ -162,11 +163,17 @@ static const struct attribute *const led_multicolor_attrs[] = { };ATTRIBUTE_GROUPS (led_multicolor); +static const struct device_type led_multicolor_device_type = { + .name ="multicolor" , + .groups = led_multicolor_groups, +}; +int led_classdev_multicolor_register_ext (struct device *parent,struct led_classdev_mc *mcled_cdev,struct led_init_data *init_data) {struct led_classdev *led_cdev; +int ret;if (!mcled_cdev)return -EINVAL ;
@@ -179,9 +186,13 @@ int led_classdev_multicolor_register_ext(struct device *parent, led_cdev = &mcled_cdev->led_cdev; led_cdev->flags |=LED_MULTI_COLOR ; - mcled_cdev->led_cdev.groups = led_multicolor_groups; + ret =led_classdev_init (parent, led_cdev, init_data); +if (ret <0 ) +return ret; + + led_cdev->dev->type = &led_multicolor_device_type; -return led_classdev_register_ext (parent, led_cdev, init_data); +return led_classdev_add (led_cdev, init_data); }EXPORT_SYMBOL_GPL (led_classdev_multicolor_register_ext);
drivers/leds/led-class.c +6 -0
@@ -654,6 +654,10 @@ int led_classdev_add(struct led_classdev *led_cdev, struct led_init_data *init_d }EXPORT_SYMBOL_GPL (led_classdev_add); +static const struct device_type led_device_type = { + .name ="standard" , +}; +/** * led_classdev_register_ext - register a new object of led_classdev class * with init data.
@@ -672,6 +676,8 @@ int led_classdev_register_ext(struct device *parent,if (ret)return ret; + led_cdev->dev->type = &led_device_type; +return led_classdev_add (led_cdev, init_data); }EXPORT_SYMBOL_GPL (led_classdev_register_ext);
include/linux/led-class-flash.h +0 -5
@@ -28,8 +28,6 @@ struct led_classdev_flash;#define LED_FAULT_LED_OVER_TEMPERATURE (1 << 8)#define LED_NUM_FLASH_FAULTS 9 -#define LED_FLASH_SYSFS_GROUPS_SIZE 5 -struct led_flash_ops {/* set flash brightness */ int (*flash_brightness_set)(struct led_classdev_flash *fled_cdev,
@@ -79,9 +77,6 @@ struct led_classdev_flash {/* flash timeout value in microseconds along with its constraints */ struct led_flash_setting duration; - -/* LED Flash class sysfs groups */ -const struct attribute_group *sysfs_groups[LED_FLASH_SYSFS_GROUPS_SIZE ]; }; static inline struct led_classdev_flash *lcdev_to_flcdev(
Signature · 1 line
2.56.1
PATCH v4 10/10 leds: multicolor: Adjust maximum subled intensity during initialization
In reply to Armin WolfThe maximum brightness of a LED device is fixed after having called led_classdev_init(), so performing the adjustment during runtime is no longer necessary.
--- drivers/leds/led-class-multicolor.c | 39 +++++++++++------------------ 1 file changed, 15 insertions(+), 24 deletions(-)
drivers/leds/led-class-multicolor.c +15 -24
@@ -14,24 +14,6 @@#include <linux/uaccess.h> #include "leds.h" -static unsigned int led_mc_get_max_intensity (struct led_classdev_mc *mcled_cdev,size_t index) -{ -unsigned int max_intensity; - -/* -* The maximum global brightness value might still be changed by -* led_classdev_register_ext() using devicetree properties. This -* prevents us from changing subled_info[X].max_intensity when -* registering a multicolor LED class device, so we have to do -* this during runtime. -*/ - max_intensity = mcled_cdev->subled_info[index].max_intensity; -if (max_intensity) -return max_intensity; - -return mcled_cdev->led_cdev.max_brightness; -} -int led_mc_calc_color_components (struct led_classdev_mc *mcled_cdev,enum led_brightness brightness) {
@@ -53,12 +35,10 @@ static ssize_t multi_max_intensity_show(struct device *dev, {struct led_classdev *led_cdev =dev_get_drvdata (dev);struct led_classdev_mc *mcled_cdev =lcdev_to_mccdev (led_cdev); -unsigned int max_intensity;int len =0 ; -for (int i =0 ; i < mcled_cdev->num_colors; i++) { - max_intensity =led_mc_get_max_intensity (mcled_cdev, i); - len +=sysfs_emit_at (buf, len,"%u" , max_intensity); +for (unsigned int i =0 ; i < mcled_cdev->num_colors; i++) { + len +=sysfs_emit_at (buf, len,"%u" , mcled_cdev->subled_info[i].max_intensity);if (i < mcled_cdev->num_colors -1 ) len +=sprintf (buf + len," " ); }
@@ -99,8 +79,8 @@ static ssize_t multi_intensity_store(struct device *dev, goto err_out; } -for (int i =0 ; i < mcled_cdev->num_colors; i++) { - max_intensity =led_mc_get_max_intensity (mcled_cdev, i); +for (unsigned int i =0 ; i < mcled_cdev->num_colors; i++) { + max_intensity = mcled_cdev->subled_info[i].max_intensity; mcled_cdev->subled_info[i].intensity =min (intensity_value[i], max_intensity); }
@@ -190,6 +170,17 @@ int led_classdev_multicolor_register_ext(struct device *parent,if (ret <0 )return ret; +/* +* The maximum global brightness value might have been changed by +* led_classdev_init() using devicetree properties. Because of this +* we can only adjust subled_info[X].max_intensity after said function +* was called. +*/ +for (unsigned int i =0 ; i < mcled_cdev->num_colors; i++) { +if (!mcled_cdev->subled_info[i].max_intensity) + mcled_cdev->subled_info[i].max_intensity = led_cdev->max_brightness; + } + led_cdev->dev->type = &led_multicolor_device_type;return led_classdev_add (led_cdev, init_data);
Signature · 1 line
2.56.1
[email protected]
AI reviewSashiko has finished reviewing this 10-patch series:
Sashiko has finished reviewing this 10-patch series:
- Patches 1-3 and 5-10: no issues found.
- Patch 4 ("leds: class: Create "brightness_hw_changed" attribut..."): 1 potential issue (1 Low)
A detailed review has been sent as a separate reply to patch 4.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]