2023-03-28 16:18:53

by Jean-Jacques Hiblot

[permalink] [raw]
Subject: [PATCH v8 4/6] leds: class: store the color index in struct led_classdev

This information might be useful for more than only deriving the led's
name. And since we have this information, we can expose it in the sysfs.

Signed-off-by: Jean-Jacques Hiblot <[email protected]>
---
Documentation/ABI/testing/sysfs-class-led | 9 +++++++++
drivers/leds/led-class.c | 20 ++++++++++++++++++++
include/linux/leds.h | 1 +
3 files changed, 30 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-class-led b/Documentation/ABI/testing/sysfs-class-led
index 2e24ac3bd7ef..c6e53213b1e7 100644
--- a/Documentation/ABI/testing/sysfs-class-led
+++ b/Documentation/ABI/testing/sysfs-class-led
@@ -59,6 +59,15 @@ Description:
brightness. Reading this file when no hw brightness change
event has happened will return an ENODATA error.

+What: /sys/class/leds/<led>/color
+Date: March 2023
+KernelVersion: 6.3
+Description:
+ Color of the led.
+
+ This is a read-only file. Reading this file returns the color
+ of the led as a string (ex: "red", "green").
+
What: /sys/class/leds/<led>/trigger
Date: March 2006
KernelVersion: 2.6.17
diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c
index 9cddcd908bce..4432e85a5fc6 100644
--- a/drivers/leds/led-class.c
+++ b/drivers/leds/led-class.c
@@ -74,6 +74,18 @@ static ssize_t max_brightness_show(struct device *dev,
}
static DEVICE_ATTR_RO(max_brightness);

+static ssize_t color_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ const char *color_text = "invalid";
+ struct led_classdev *led_cdev = dev_get_drvdata(dev);
+
+ if (led_cdev->color < LED_COLOR_ID_MAX)
+ color_text = led_colors[led_cdev->color];
+ return sysfs_emit(buf, "%s\n", color_text);
+}
+static DEVICE_ATTR_RO(color);
+
#ifdef CONFIG_LEDS_TRIGGERS
static BIN_ATTR(trigger, 0644, led_trigger_read, led_trigger_write, 0);
static struct bin_attribute *led_trigger_bin_attrs[] = {
@@ -88,6 +100,7 @@ static const struct attribute_group led_trigger_group = {
static struct attribute *led_class_attrs[] = {
&dev_attr_brightness.attr,
&dev_attr_max_brightness.attr,
+ &dev_attr_color.attr,
NULL,
};

@@ -375,6 +388,10 @@ int led_classdev_register_ext(struct device *parent,
if (fwnode_property_present(init_data->fwnode,
"retain-state-shutdown"))
led_cdev->flags |= LED_RETAIN_AT_SHUTDOWN;
+
+ if (fwnode_property_present(init_data->fwnode, "color"))
+ fwnode_property_read_u32(init_data->fwnode, "color",
+ &led_cdev->color);
}
} else {
proposed_name = led_cdev->name;
@@ -384,6 +401,9 @@ int led_classdev_register_ext(struct device *parent,
if (ret < 0)
return ret;

+ if (led_cdev->color >= LED_COLOR_ID_MAX)
+ dev_warn(parent, "LED %s color identifier out of range\n", final_name);
+
mutex_init(&led_cdev->led_access);
mutex_lock(&led_cdev->led_access);
led_cdev->dev = device_create_with_groups(leds_class, parent, 0,
diff --git a/include/linux/leds.h b/include/linux/leds.h
index 41df18f42d00..9d38cb90331d 100644
--- a/include/linux/leds.h
+++ b/include/linux/leds.h
@@ -71,6 +71,7 @@ struct led_classdev {
const char *name;
unsigned int brightness;
unsigned int max_brightness;
+ unsigned int color;
int flags;

/* Lower 16 bits reflect status */
--
2.25.1


2023-03-28 17:24:15

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v8 4/6] leds: class: store the color index in struct led_classdev

On Tue, Mar 28, 2023 at 7:15 PM Jean-Jacques Hiblot
<[email protected]> wrote:
>
> This information might be useful for more than only deriving the led's
> name. And since we have this information, we can expose it in the sysfs.

...

> +Date: March 2023
> +KernelVersion: 6.3

Outdated version.

...

> + Color of the led.
> +
> + This is a read-only file. Reading this file returns the color
> + of the led as a string (ex: "red", "green").

There are no strict rules about colour and I don't think it's a good
idea. Why in such a case is it different to label? My proposal here at
least documenting that the colour must follow one of the existing
naming standards (like RGB in hex, HTML, or name in accordance with
chosen standard).

Yet, it won't technically prevent abusing that, but at least will show
the intention and allow pointing out to the bugs or develop user space
tooling based on existing parsers (if any).

--
With Best Regards,
Andy Shevchenko

2023-04-06 06:55:40

by Jean-Jacques Hiblot

[permalink] [raw]
Subject: Re: [PATCH v8 4/6] leds: class: store the color index in struct led_classdev



On 28/03/2023 19:15, Andy Shevchenko wrote:
> On Tue, Mar 28, 2023 at 7:15 PM Jean-Jacques Hiblot
> <[email protected]> wrote:
>>
>> This information might be useful for more than only deriving the led's
>> name. And since we have this information, we can expose it in the sysfs.
>
> ...
>
>> +Date: March 2023
>> +KernelVersion: 6.3
>
> Outdated version.
>
> ...
>
>> + Color of the led.
>> +
>> + This is a read-only file. Reading this file returns the color
>> + of the led as a string (ex: "red", "green").
>
> There are no strict rules about colour and I don't think it's a good
> idea. Why in such a case is it different to label? My proposal here at
> least documenting that the colour must follow one of the existing
> naming standards (like RGB in hex, HTML, or name in accordance with
> chosen standard).
Actually the colors are defined in an array: led_colors (led-core.c: 88)
So the color is one of the following: white, red, reen, blue, amber,
violet, yellow, ir, multicolor, rgb

There is mention in the TODO file of changing the way RGB leds are
handled and the RGB leds would probably show the hex RGB values here.

>
> Yet, it won't technically prevent abusing that, but at least will show
> the intention and allow pointing out to the bugs or develop user space
> tooling based on existing parsers (if any).
>