public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: ben@fluff.org
To: linux-kernel@vger.kernel.org, akpm@linux-foundation.org
Cc: David Brownell <dbrownell@users.sourceforge.net>
Subject: gpiolib: add export/unexport by gpio name
Date: Tue, 04 Aug 2009 11:43:28 +0100	[thread overview]
Message-ID: <20090804104328.770414513@fluff.org> (raw)

[-- 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;
 

-- 

             reply	other threads:[~2009-08-04 10:43 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-04 10:43 ben [this message]
2009-08-04 10:56 ` gpiolib: add export/unexport by gpio name Ben Dooks
  -- strict thread matches above, loose matches on Subject: below --
2009-08-04 10:55 ben
2009-08-04 20:58 ` David Brownell
2009-08-03 16:55 ben
2009-08-03 17:04 ` Ben Dooks

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090804104328.770414513@fluff.org \
    --to=ben@fluff.org \
    --cc=akpm@linux-foundation.org \
    --cc=dbrownell@users.sourceforge.net \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox