LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH 0/3] Handle LEDs with the same name
@ 2015-03-30 17:45 Ricardo Ribalda Delgado
  2015-03-30 17:45 ` [PATCH 1/3] leds/led-class: " Ricardo Ribalda Delgado
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Ricardo Ribalda Delgado @ 2015-03-30 17:45 UTC (permalink / raw)
  To: Geert Uytterhoeven, sakari.ailus, Bryan Wu, Richard Purdie,
	linux-leds, linux-kernel
  Cc: Ricardo Ribalda Delgado

This is a rework of the original patch + 3 fixup patches from me.
Contains a lot of feedback from Geert Uytterhoeven <geert@linux-m68k.org>
Thanks!

Ricardo Ribalda Delgado (1):
  leds/led-class: Handle LEDs with the same name

Sakari Ailus (2):
  leds: Use log level warn instead of info when telling about a name
    clash
  leds: Don't treat the LED name as a format string

 drivers/leds/led-class.c | 39 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 37 insertions(+), 2 deletions(-)

-- 
2.1.4


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

* [PATCH 1/3] leds/led-class: Handle LEDs with the same name
  2015-03-30 17:45 [PATCH 0/3] Handle LEDs with the same name Ricardo Ribalda Delgado
@ 2015-03-30 17:45 ` Ricardo Ribalda Delgado
  2015-03-30 17:46 ` [PATCH 2/3] leds: Use log level warn instead of info when telling about a name clash Ricardo Ribalda Delgado
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Ricardo Ribalda Delgado @ 2015-03-30 17:45 UTC (permalink / raw)
  To: Geert Uytterhoeven, sakari.ailus, Bryan Wu, Richard Purdie,
	linux-leds, linux-kernel
  Cc: Ricardo Ribalda Delgado

The current code expected that every LED had an unique name. This is a
legit expectation when the device tree can no be modified or extended.
But with device tree overlays this requirement can be easily broken.

This patch finds out if the name is already in use and adds the suffix
_1, _2... if not.

Signed-off-by: Ricardo Ribalda Delgado <ricardo.ribalda@gmail.com>
Reported-by: Geert Uytterhoeven <geert@linux-m68k.org>
---
 drivers/leds/led-class.c | 39 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 37 insertions(+), 2 deletions(-)

diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c
index 768d33a..fc57d9c 100644
--- a/drivers/leds/led-class.c
+++ b/drivers/leds/led-class.c
@@ -212,6 +212,31 @@ static const struct dev_pm_ops leds_class_dev_pm_ops = {
 	.resume         = led_resume,
 };
 
+static int match_name(struct device *dev, const void *data)
+{
+	if (!dev_name(dev))
+		return 0;
+	return !strcmp(dev_name(dev), (char *)data);
+}
+
+static int led_classdev_next_name(const char *init_name, char *name,
+				  size_t len)
+{
+	unsigned int i = 0;
+	int ret = 0;
+
+	strlcpy(name, init_name, len);
+
+	while (class_find_device(leds_class, NULL, name, match_name) &&
+	       (ret < len))
+		ret = snprintf(name, len, "%s_%u", init_name, ++i);
+
+	if (ret >= len)
+		return -ENOMEM;
+
+	return i;
+}
+
 /**
  * led_classdev_register - register a new object of led_classdev class.
  * @parent: The device to register.
@@ -219,12 +244,22 @@ static const struct dev_pm_ops leds_class_dev_pm_ops = {
  */
 int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
 {
+	char name[64];
+	int ret;
+
+	ret = led_classdev_next_name(led_cdev->name, name, sizeof(name));
+	if (ret < 0)
+		return ret;
+
 	led_cdev->dev = device_create_with_groups(leds_class, parent, 0,
-					led_cdev, led_cdev->groups,
-					"%s", led_cdev->name);
+					led_cdev, led_cdev->groups, name);
 	if (IS_ERR(led_cdev->dev))
 		return PTR_ERR(led_cdev->dev);
 
+	if (ret)
+		dev_info(parent, "Led %s renamed to %s due to name collision",
+				led_cdev->name, dev_name(led_cdev->dev));
+
 #ifdef CONFIG_LEDS_TRIGGERS
 	init_rwsem(&led_cdev->trigger_lock);
 #endif
-- 
2.1.4


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

* [PATCH 2/3] leds: Use log level warn instead of info when telling about a name clash
  2015-03-30 17:45 [PATCH 0/3] Handle LEDs with the same name Ricardo Ribalda Delgado
  2015-03-30 17:45 ` [PATCH 1/3] leds/led-class: " Ricardo Ribalda Delgado
@ 2015-03-30 17:46 ` Ricardo Ribalda Delgado
  2015-03-30 17:46 ` [PATCH 3/3] leds: Don't treat the LED name as a format string Ricardo Ribalda Delgado
  2015-03-30 22:08 ` [PATCH 0/3] Handle LEDs with the same name Bryan Wu
  3 siblings, 0 replies; 5+ messages in thread
From: Ricardo Ribalda Delgado @ 2015-03-30 17:46 UTC (permalink / raw)
  To: Geert Uytterhoeven, sakari.ailus, Bryan Wu, Richard Purdie,
	linux-leds, linux-kernel
  Cc: Sakari Ailus, Ricardo Ribalda Delgado

From: Sakari Ailus <sakari.ailus@iki.fi>

The LED names are expected to be unique in the system. Use KERN_WARNING log
level to notify the user about the matter.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Signed-off-by: Bryan Wu <cooloney@gmail.com>
Signed-off-by: Ricardo Ribalda Delgado <ricardo.ribalda@gmail.com>
---
 drivers/leds/led-class.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c
index fc57d9c..82e3844 100644
--- a/drivers/leds/led-class.c
+++ b/drivers/leds/led-class.c
@@ -257,7 +257,7 @@ int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
 		return PTR_ERR(led_cdev->dev);
 
 	if (ret)
-		dev_info(parent, "Led %s renamed to %s due to name collision",
+		dev_warn(parent, "Led %s renamed to %s due to name collision",
 				led_cdev->name, dev_name(led_cdev->dev));
 
 #ifdef CONFIG_LEDS_TRIGGERS
-- 
2.1.4


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

* [PATCH 3/3] leds: Don't treat the LED name as a format string
  2015-03-30 17:45 [PATCH 0/3] Handle LEDs with the same name Ricardo Ribalda Delgado
  2015-03-30 17:45 ` [PATCH 1/3] leds/led-class: " Ricardo Ribalda Delgado
  2015-03-30 17:46 ` [PATCH 2/3] leds: Use log level warn instead of info when telling about a name clash Ricardo Ribalda Delgado
@ 2015-03-30 17:46 ` Ricardo Ribalda Delgado
  2015-03-30 22:08 ` [PATCH 0/3] Handle LEDs with the same name Bryan Wu
  3 siblings, 0 replies; 5+ messages in thread
From: Ricardo Ribalda Delgado @ 2015-03-30 17:46 UTC (permalink / raw)
  To: Geert Uytterhoeven, sakari.ailus, Bryan Wu, Richard Purdie,
	linux-leds, linux-kernel
  Cc: Sakari Ailus, Ricardo Ribalda Delgado

From: Sakari Ailus <sakari.ailus@iki.fi>

The LED name was wrongly interpreted as format string. Stop doing that.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Signed-off-by: Bryan Wu <cooloney@gmail.com>
Signed-off-by: Ricardo Ribalda Delgado <ricardo.ribalda@gmail.com>
---
 drivers/leds/led-class.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c
index 82e3844..728681d 100644
--- a/drivers/leds/led-class.c
+++ b/drivers/leds/led-class.c
@@ -252,7 +252,7 @@ int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
 		return ret;
 
 	led_cdev->dev = device_create_with_groups(leds_class, parent, 0,
-					led_cdev, led_cdev->groups, name);
+				led_cdev, led_cdev->groups, "%s", name);
 	if (IS_ERR(led_cdev->dev))
 		return PTR_ERR(led_cdev->dev);
 
-- 
2.1.4


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

* Re: [PATCH 0/3] Handle LEDs with the same name
  2015-03-30 17:45 [PATCH 0/3] Handle LEDs with the same name Ricardo Ribalda Delgado
                   ` (2 preceding siblings ...)
  2015-03-30 17:46 ` [PATCH 3/3] leds: Don't treat the LED name as a format string Ricardo Ribalda Delgado
@ 2015-03-30 22:08 ` Bryan Wu
  3 siblings, 0 replies; 5+ messages in thread
From: Bryan Wu @ 2015-03-30 22:08 UTC (permalink / raw)
  To: Ricardo Ribalda Delgado
  Cc: Geert Uytterhoeven, sakari.ailus, Richard Purdie,
	Linux LED Subsystem, lkml

On Mon, Mar 30, 2015 at 10:45 AM, Ricardo Ribalda Delgado
<ricardo.ribalda@gmail.com> wrote:
> This is a rework of the original patch + 3 fixup patches from me.
> Contains a lot of feedback from Geert Uytterhoeven <geert@linux-m68k.org>
> Thanks!
>
> Ricardo Ribalda Delgado (1):
>   leds/led-class: Handle LEDs with the same name
>
> Sakari Ailus (2):
>   leds: Use log level warn instead of info when telling about a name
>     clash
>   leds: Don't treat the LED name as a format string
>
>  drivers/leds/led-class.c | 39 +++++++++++++++++++++++++++++++++++++--
>  1 file changed, 37 insertions(+), 2 deletions(-)
>
Applied again.

Thanks,
-Bryan

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

end of thread, other threads:[~2015-03-30 22:08 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-30 17:45 [PATCH 0/3] Handle LEDs with the same name Ricardo Ribalda Delgado
2015-03-30 17:45 ` [PATCH 1/3] leds/led-class: " Ricardo Ribalda Delgado
2015-03-30 17:46 ` [PATCH 2/3] leds: Use log level warn instead of info when telling about a name clash Ricardo Ribalda Delgado
2015-03-30 17:46 ` [PATCH 3/3] leds: Don't treat the LED name as a format string Ricardo Ribalda Delgado
2015-03-30 22:08 ` [PATCH 0/3] Handle LEDs with the same name Bryan Wu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).