[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <1424386898-19021-1-git-send-email-ricardo.ribalda@gmail.com>
Date: Fri, 20 Feb 2015 00:01:38 +0100
From: Ricardo Ribalda Delgado <ricardo.ribalda@...il.com>
To: Bryan Wu <cooloney@...il.com>, Richard Purdie <rpurdie@...ys.net>,
linux-leds@...r.kernel.org, linux-kernel@...r.kernel.org
Cc: Ricardo Ribalda Delgado <ricardo.ribalda@...il.com>
Subject: [PATCH v2] led/led-class: Handle LEDs with the same name
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@...il.com>
---
v2: Suggested by Bryan Wu <cooloney@...il.com>
-move name search to separated function
-Iterate on class
drivers/leds/led-class.c | 38 ++++++++++++++++++++++++++++++++++++--
1 file changed, 36 insertions(+), 2 deletions(-)
diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c
index 795ec99..5c6505a 100644
--- a/drivers/leds/led-class.c
+++ b/drivers/leds/led-class.c
@@ -212,6 +212,29 @@ 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 char *led_classdev_next_name(const char *init_name)
+{
+ char *name = (char *) init_name;
+ int i = 0;
+
+ while (class_find_device(leds_class, NULL, name, match_name)) {
+ if (name != init_name)
+ kfree(name);
+ name = kasprintf(GFP_KERNEL, "%s_%d", init_name, ++i);
+ if (!name)
+ return NULL;
+ }
+
+ return name;
+}
+
/**
* led_classdev_register - register a new object of led_classdev class.
* @parent: The device to register.
@@ -219,12 +242,23 @@ 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 = led_classdev_next_name(led_cdev->name);
+
+ if (!name)
+ return -ENOMEM;
+
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 (name != led_cdev->name)
+ kfree(name);
+
if (IS_ERR(led_cdev->dev))
return PTR_ERR(led_cdev->dev);
+ if (name != led_cdev->name)
+ 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
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists