All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] gpiolib: Disallow identical line names in the same chip
@ 2020-12-12  0:34 Linus Walleij
  2020-12-12  9:23 ` Geert Uytterhoeven
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Linus Walleij @ 2020-12-12  0:34 UTC (permalink / raw)
  To: linux-gpio
  Cc: Bartosz Golaszewski, Linus Walleij, Geert Uytterhoeven, Johan Hovold

We need to make this namespace hierarchical: at least do not
allow two lines on the same chip to have the same name, this
is just too much flexibility. If we name a line on a chip,
name it uniquely on that chip.

I don't know what happens if we just apply this, I *hope* there
are not a lot of systems out there breaking this simple and
intuitive rule.

As a side effect, this makes the device tree naming code
scream a bit if names are not globally unique.

I think there are not super-many device trees out there naming
their lines so let's fix this before the problem becomes
widespread.

Cc: Geert Uytterhoeven <geert+renesas@glider.be>
Cc: Johan Hovold <johan@kernel.org>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
This may be just the first step in tightening this up.
Googling gives at hand that the colission warning doesn't
happen much so we might go as far as to say the name can
be required to be globally unique, but that creates a flat
namespace so I don't know if that is desireable.
---
 drivers/gpio/gpiolib.c | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 5ce0c14c637b..be4b3e9032b2 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -330,11 +330,9 @@ static struct gpio_desc *gpio_name_to_desc(const char * const name)
 
 /*
  * Take the names from gc->names and assign them to their GPIO descriptors.
- * Warn if a name is already used for a GPIO line on a different GPIO chip.
  *
- * Note that:
- *   1. Non-unique names are still accepted,
- *   2. Name collisions within the same GPIO chip are not reported.
+ * - Fail if a name is already used for a GPIO line on the same chip.
+ * - Allow names to not be globally unique but warn about it.
  */
 static int gpiochip_set_desc_names(struct gpio_chip *gc)
 {
@@ -343,13 +341,19 @@ static int gpiochip_set_desc_names(struct gpio_chip *gc)
 
 	/* First check all names if they are unique */
 	for (i = 0; i != gc->ngpio; ++i) {
-		struct gpio_desc *gpio;
+		struct gpio_desc *gpiod;
 
-		gpio = gpio_name_to_desc(gc->names[i]);
-		if (gpio)
+		gpiod = gpio_name_to_desc(gc->names[i]);
+		if (gpiod) {
 			dev_warn(&gdev->dev,
 				 "Detected name collision for GPIO name '%s'\n",
 				 gc->names[i]);
+			if (gpiod->gdev == gdev) {
+				dev_err(&gdev->dev,
+					"GPIO name collision on the same chip, this is not allowed, fix all lines on the chip to have unique names\n");
+				return -EINVAL;
+			}
+		}
 	}
 
 	/* Then add all names to the GPIO descriptors */
@@ -402,8 +406,22 @@ static int devprop_gpiochip_set_names(struct gpio_chip *chip)
 		return ret;
 	}
 
-	for (i = 0; i < count; i++)
+	for (i = 0; i < count; i++) {
+		struct gpio_desc *gpiod;
+
+		gpiod = gpio_name_to_desc(names[i]);
+		if (gpiod) {
+			dev_warn(&gdev->dev,
+                                 "Detected name collision for GPIO name '%s'\n",
+                                 names[i]);
+			if (gpiod->gdev == gdev) {
+				dev_err(&gdev->dev,
+					"GPIO name collision on the same chip, this is not allowed, fix all lines on the chip to have unique names\n");
+				return -EINVAL;
+			}
+		}
 		gdev->descs[i].name = names[i];
+	}
 
 	kfree(names);
 
-- 
2.28.0


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

end of thread, other threads:[~2020-12-15  8:25 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-12  0:34 [PATCH] gpiolib: Disallow identical line names in the same chip Linus Walleij
2020-12-12  9:23 ` Geert Uytterhoeven
2020-12-12 12:51   ` Linus Walleij
2020-12-14  9:23     ` Johan Hovold
2020-12-13 15:00 ` Andy Shevchenko
2020-12-14  8:45   ` Bartosz Golaszewski
2020-12-14  9:17 ` Linus Walleij
2020-12-14  9:29 ` Johan Hovold
2020-12-14 22:39   ` Linus Walleij
2020-12-15  8:24     ` Geert Uytterhoeven

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.