public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* gpiolib: add export/unexport by gpio name
@ 2009-08-03 16:55 ben
  2009-08-03 17:04 ` Ben Dooks
  0 siblings, 1 reply; 6+ messages in thread
From: ben @ 2009-08-03 16:55 UTC (permalink / raw)
  To: linux-kernel, akpm; +Cc: David Brownell

[-- Attachment #1: gpio-export-by-name.patch --]
[-- Type: text/plain, Size: 2202 bytes --]

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

Signed-off-by: Ben Dooks <ben@simtec.co.uk>
Cc: David Brownell <dbrownell@users.sourceforge.net>

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

Index: b/drivers/gpio/gpiolib.c
===================================================================
--- a/drivers/gpio/gpiolib.c	2009-08-03 17:53:04.000000000 +0100
+++ b/drivers/gpio/gpiolib.c	2009-08-03 17:53:33.000000000 +0100
@@ -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;
 

-- 

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

* Re: gpiolib: add export/unexport by gpio name
  2009-08-03 16:55 ben
@ 2009-08-03 17:04 ` Ben Dooks
  0 siblings, 0 replies; 6+ messages in thread
From: Ben Dooks @ 2009-08-03 17:04 UTC (permalink / raw)
  To: ben; +Cc: linux-kernel, akpm, David Brownell

On Mon, Aug 03, 2009 at 05:55:44PM +0100, ben@fluff.org wrote:
> Add the facility to export/unexport a gpio by the name assigned to it
> as well as the number.

sorry, forgot to update the patch before sending, please ignore this
one.
 
> Signed-off-by: Ben Dooks <ben@simtec.co.uk>
> Cc: David Brownell <dbrownell@users.sourceforge.net>
> 
> ---
>  drivers/gpio/gpiolib.c |   59 +++++++++++++++++++++++++++++++++++++++++++++----
>  1 file changed, 55 insertions(+), 4 deletions(-)
> 
> Index: b/drivers/gpio/gpiolib.c
> ===================================================================
> --- a/drivers/gpio/gpiolib.c	2009-08-03 17:53:04.000000000 +0100
> +++ b/drivers/gpio/gpiolib.c	2009-08-03 17:53:33.000000000 +0100
> @@ -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;
>  
> 
> -- 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/

-- 
Ben (ben@fluff.org, http://www.fluff.org/)

  'a smiley only costs 4 bytes'

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

* gpiolib: add export/unexport by gpio name
@ 2009-08-04 10:43 ben
  2009-08-04 10:56 ` Ben Dooks
  0 siblings, 1 reply; 6+ messages in thread
From: ben @ 2009-08-04 10:43 UTC (permalink / raw)
  To: linux-kernel, akpm; +Cc: David Brownell

[-- Attachment #1: gpio-export-by-name.patch --]
[-- Type: text/plain, Size: 2202 bytes --]

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

Signed-off-by: Ben Dooks <ben@simtec.co.uk>
Cc: David Brownell <dbrownell@users.sourceforge.net>

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

Index: b/drivers/gpio/gpiolib.c
===================================================================
--- a/drivers/gpio/gpiolib.c	2009-08-03 17:53:04.000000000 +0100
+++ b/drivers/gpio/gpiolib.c	2009-08-03 17:53:33.000000000 +0100
@@ -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;
 

-- 

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

* gpiolib: add export/unexport by gpio name
@ 2009-08-04 10:55 ben
  2009-08-04 20:58 ` David Brownell
  0 siblings, 1 reply; 6+ messages in thread
From: ben @ 2009-08-04 10:55 UTC (permalink / raw)
  To: linux-kernel, akpm; +Cc: David Brownell

[-- Attachment #1: gpio-export-by-name.patch --]
[-- Type: text/plain, Size: 2387 bytes --]

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

Signed-off-by: Ben Dooks <ben@simtec.co.uk>
Cc: David Brownell <dbrownell@users.sourceforge.net>

---
 drivers/gpio/gpiolib.c |   70 ++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 66 insertions(+), 4 deletions(-)

Index: b/drivers/gpio/gpiolib.c
===================================================================
--- a/drivers/gpio/gpiolib.c	2009-08-04 11:53:53.000000000 +0100
+++ b/drivers/gpio/gpiolib.c	2009-08-04 11:54:12.000000000 +0100
@@ -364,6 +364,64 @@ static const struct attribute_group gpio
 	.attrs = (struct attribute **) gpiochip_attrs,
 };
 
+static int search_names(char **names, const char *match, int ngpio)
+{
+	int len = strlen(match);
+	char *m;
+	int ptr;
+
+	/* if cr terminated, remove */
+	if (match[len-1] == '\n')
+		len--;
+
+	if (names) {
+		for (ptr = 0; ptr < ngpio; ptr++) {
+			m = names[ptr];
+			if (!m)
+				continue;
+
+			if (strlen(m) != len)
+				continue;
+
+			if (strncmp(m, match, len) == 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;
+		}
+	} else
+		status = gpio;
+
+	return status;
+}
+
 /*
  * /sys/class/gpio/export ... write-only
  *	integer N ... number of GPIO to export (full access)
@@ -375,9 +433,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 +465,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;
 

-- 

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

* Re: gpiolib: add export/unexport by gpio name
  2009-08-04 10:43 ben
@ 2009-08-04 10:56 ` Ben Dooks
  0 siblings, 0 replies; 6+ messages in thread
From: Ben Dooks @ 2009-08-04 10:56 UTC (permalink / raw)
  To: ben; +Cc: linux-kernel, akpm, David Brownell

On Tue, Aug 04, 2009 at 11:43:28AM +0100, ben@fluff.org wrote:
> Add the facility to export/unexport a gpio by the name assigned to it
> as well as the number.
> 
> Signed-off-by: Ben Dooks <ben@simtec.co.uk>
> Cc: David Brownell <dbrownell@users.sourceforge.net>

sorry, this one seems to have escaped again.
 
> ---
>  drivers/gpio/gpiolib.c |   59 +++++++++++++++++++++++++++++++++++++++++++++----
>  1 file changed, 55 insertions(+), 4 deletions(-)
> 
> Index: b/drivers/gpio/gpiolib.c
> ===================================================================
> --- a/drivers/gpio/gpiolib.c	2009-08-03 17:53:04.000000000 +0100
> +++ b/drivers/gpio/gpiolib.c	2009-08-03 17:53:33.000000000 +0100
> @@ -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;
>  
> 
> -- 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/

-- 
Ben (ben@fluff.org, http://www.fluff.org/)

  'a smiley only costs 4 bytes'

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

* Re: gpiolib: add export/unexport by gpio name
  2009-08-04 10:55 gpiolib: add export/unexport by gpio name ben
@ 2009-08-04 20:58 ` David Brownell
  0 siblings, 0 replies; 6+ messages in thread
From: David Brownell @ 2009-08-04 20:58 UTC (permalink / raw)
  To: ben; +Cc: linux-kernel, akpm

On Tuesday 04 August 2009, ben@fluff.org wrote:
> Add the facility to export/unexport a gpio by the name assigned to it
> as well as the number.

I guess I'm still a bit puzzled by the notion of adding
second/parallel naming scheme for GPIOs.  As a rule, such
duplication is undesirable ...

But assuming such things are OK, this patch has a couple
technical problems.


> Signed-off-by: Ben Dooks <ben@simtec.co.uk>
> Cc: David Brownell <dbrownell@users.sourceforge.net>
> 
> ---
>  drivers/gpio/gpiolib.c |   70 ++++++++++++++++++++++++++++++++++++++++++++++---
>  1 file changed, 66 insertions(+), 4 deletions(-)
> 
> Index: b/drivers/gpio/gpiolib.c
> ===================================================================
> --- a/drivers/gpio/gpiolib.c	2009-08-04 11:53:53.000000000 +0100
> +++ b/drivers/gpio/gpiolib.c	2009-08-04 11:54:12.000000000 +0100
> @@ -364,6 +364,64 @@ static const struct attribute_group gpio
>  	.attrs = (struct attribute **) gpiochip_attrs,
>  };
>  
> +static int search_names(char **names, const char *match, int ngpio)
> +{
> +	int len = strlen(match);
> +	char *m;
> +	int ptr;
> +
> +	/* if cr terminated, remove */
> +	if (match[len-1] == '\n')
> +		len--;
> +
> +	if (names) {
> +		for (ptr = 0; ptr < ngpio; ptr++) {
> +			m = names[ptr];
> +			if (!m)
> +				continue;
> +
> +			if (strlen(m) != len)
> +				continue;
> +
> +			if (strncmp(m, match, len) == 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;) {

Nothing is guarding against
 (a) duplicate chip->names[] entries within or between chips,
 (b) concurrent addition/removal of gpio_chip instances

For (a) maybe the answer is to detect them at chip registration
time ... then either fail, or else warn then null out the dup.

For (b) it seems that gpio_lock is required.

> +			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;
> +		}
> +	} else
> +		status = gpio;
> +
> +	return status;
> +}
> +
>  /*
>   * /sys/class/gpio/export ... write-only
>   *	integer N ... number of GPIO to export (full access)
> @@ -375,9 +433,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 +465,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;
>  
> 
> -- 
> 
> 




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

end of thread, other threads:[~2009-08-05  0:28 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-08-04 10:55 gpiolib: add export/unexport by gpio name ben
2009-08-04 20:58 ` David Brownell
  -- strict thread matches above, loose matches on Subject: below --
2009-08-04 10:43 ben
2009-08-04 10:56 ` Ben Dooks
2009-08-03 16:55 ben
2009-08-03 17:04 ` Ben Dooks

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox