Skip to contentSkip to conversation

gpiolib: add export/unexport by gpio name

PATCH [email protected] 2 participants 2 messages Aug 4, 2009 GPIO

lore
Share & tools
lore
#0 lore raw

Add the facility to export/unexport a gpio by the name assigned to it as well as the number.

Cc David Brownell <[email protected]>
---
 drivers/gpio/gpiolib.c |   59 +++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 55 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib.c +55 -4
Index: b/drivers/gpio/gpiolib.c
===================================================================
@@ -364,6 +364,53 @@ static const struct attribute_group gpio
 	.attrs = (struct attribute **) gpiochip_attrs,
 };
 
+static int search_names(char **names, const char *match, int ngpio)
+{
+	int ptr;
+
+	if (names) {
+		for (ptr = 0; ptr < ngpio; ptr++) {
+			if (!names[ptr])
+				continue;
+
+			if (strcmp(names[ptr], match) == 0)
+				return ptr;
+		}
+	}
+
+	return -ENOENT;
+}
+
+static long gpio_from_string(const char *buf)
+{
+	struct gpio_chip *chip;
+	struct gpio_desc *desc;
+	long status;
+	long gpio;
+	int ptr, off;
+
+	status = strict_strtol(buf, 0, &gpio);
+	if (status < 0) {
+		for (ptr = 0; ptr < ARCH_NR_GPIOS && status < 0;) {
+			desc = gpio_desc + ptr;
+			chip = desc->chip;
+
+			if (!chip) {
+				ptr++;
+				continue;
+			}
+
+			off = search_names(chip->names, buf, chip->ngpio);
+			if (off >= 0)
+				status = ptr + off;
+
+			ptr += chip->ngpio;
+		}
+	}
+
+	return status;
+}
+
 /*
  * /sys/class/gpio/export ... write-only
  *	integer N ... number of GPIO to export (full access)
@@ -375,9 +422,11 @@ static ssize_t export_store(struct class
 	long	gpio;
 	int	status;
 
-	status = strict_strtol(buf, 0, &gpio);
-	if (status < 0)
+	gpio = gpio_from_string(buf);
+	if (gpio < 0) {
+		status = gpio;
 		goto done;
+	}
 
 	/* No extra locking here; FLAG_SYSFS just signifies that the
 	 * request and export were done by on behalf of userspace, so
@@ -405,9 +454,11 @@ static ssize_t unexport_store(struct cla
 	long	gpio;
 	int	status;
 
-	status = strict_strtol(buf, 0, &gpio);
-	if (status < 0)
+	gpio = gpio_from_string(buf);
+	if (gpio < 0) {
+		status = gpio;
 		goto done;
+	}
 
 	status = -EINVAL;
 
#1 lore raw
Quoted 5 lines · On Tue, Aug 04, 2009 at 11:43:28AM +0100, [email protected] wrote:

Add the facility to export/unexport a gpio by the name assigned to it as well as the number.

Cc David Brownell <[email protected]>

sorry, this one seems to have escaped again.

Quoted 97 lines
---
 drivers/gpio/gpiolib.c |   59 +++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 55 insertions(+), 4 deletions(-)

Index: b/drivers/gpio/gpiolib.c
===================================================================

drivers/gpio/gpiolib.c +55 -4
@@ -364,6 +364,53 @@ static const struct attribute_group gpio
 	.attrs = (struct attribute **) gpiochip_attrs,
 };
 
+static int search_names(char **names, const char *match, int ngpio)
+{
+	int ptr;
+
+	if (names) {
+		for (ptr = 0; ptr < ngpio; ptr++) {
+			if (!names[ptr])
+				continue;
+
+			if (strcmp(names[ptr], match) == 0)
+				return ptr;
+		}
+	}
+
+	return -ENOENT;
+}
+
+static long gpio_from_string(const char *buf)
+{
+	struct gpio_chip *chip;
+	struct gpio_desc *desc;
+	long status;
+	long gpio;
+	int ptr, off;
+
+	status = strict_strtol(buf, 0, &gpio);
+	if (status < 0) {
+		for (ptr = 0; ptr < ARCH_NR_GPIOS && status < 0;) {
+			desc = gpio_desc + ptr;
+			chip = desc->chip;
+
+			if (!chip) {
+				ptr++;
+				continue;
+			}
+
+			off = search_names(chip->names, buf, chip->ngpio);
+			if (off >= 0)
+				status = ptr + off;
+
+			ptr += chip->ngpio;
+		}
+	}
+
+	return status;
+}
+
 /*
  * /sys/class/gpio/export ... write-only
  *	integer N ... number of GPIO to export (full access)
@@ -375,9 +422,11 @@ static ssize_t export_store(struct class
 	long	gpio;
 	int	status;
 
-	status = strict_strtol(buf, 0, &gpio);
-	if (status < 0)
+	gpio = gpio_from_string(buf);
+	if (gpio < 0) {
+		status = gpio;
 		goto done;
+	}
 
 	/* No extra locking here; FLAG_SYSFS just signifies that the
 	 * request and export were done by on behalf of userspace, so
@@ -405,9 +454,11 @@ static ssize_t unexport_store(struct cla
 	long	gpio;
 	int	status;
 
-	status = strict_strtol(buf, 0, &gpio);
-	if (status < 0)
+	gpio = gpio_from_string(buf);
+	if (gpio < 0) {
+		status = gpio;
 		goto done;
+	}
 
 	status = -EINVAL;
 
Signature · 6 lines
-- 
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
Signature · 3 lines
Ben ([email protected], http://www.fluff.org/)

  'a smiley only costs 4 bytes'