Re: [patch/rfc 1/4] GPIO implementation framework

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: David Brownell <david-b@...>
Cc: Linux Kernel list <linux-kernel@...>, Felipe Balbi <felipebalbi@...>, Bill Gatliff <bgat@...>, Haavard Skinnemoen <hskinnemoen@...>, Andrew Victor <andrew@...>, Tony Lindgren <tony@...>, Jean Delvare <khali@...>, Kevin Hilman <khilman@...>, Paul Mundt <lethal@...>, Ben Dooks <ben@...>
Date: Tuesday, November 13, 2007 - 9:00 pm

Here comes a bunch of patches to illustrate my idea, some are not related to
the point I mentioned, and they are not mature for now :-)

Subject: [PATCH 1/5] add gpio_is_onchip() for commonly used gpio range checking

---
 lib/gpiolib.c |   32 ++++++++++++++++++++++----------
 1 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/lib/gpiolib.c b/lib/gpiolib.c
index f2ae689..c627efb 100644
--- a/lib/gpiolib.c
+++ b/lib/gpiolib.c
@@ -33,6 +33,14 @@ static DEFINE_SPINLOCK(gpio_lock);
 static struct gpio_chip *chips[DIV_ROUND_UP(ARCH_NR_GPIOS,
 					ARCH_GPIOS_PER_CHIP)];

+static inline int gpio_is_onchip(unsigned gpio, struct gpio_chip *chip)
+{
+	if (chip && gpio >= chip->base && gpio < chip->base + chip->ngpio)
+		return 1;
+	else
+		return 0;
+}
+
 /* Warn when drivers omit gpio_request() calls -- legal but
  * ill-advised when setting direction, and otherwise illegal.
  */
@@ -139,11 +147,11 @@ int gpio_request(unsigned gpio, const char *label)

 	spin_lock_irqsave(&gpio_lock, flags);
 	chip = gpio_to_chip(gpio);
-	if (!chip)
+
+	if (!gpio_is_onchip(gpio, chip))
 		goto done;
+
 	gpio -= chip->base;
-	if (gpio >= chip->ngpio)
-		goto done;

 	/* NOTE:  gpio_request() can be called in early boot,
 	 * before IRQs are enabled.
@@ -176,11 +184,11 @@ void gpio_free(unsigned gpio)
 	spin_lock_irqsave(&gpio_lock, flags);

 	chip = gpio_to_chip(gpio);
-	if (!chip)
+
+	if (!gpio_is_onchip(gpio, chip))
 		goto done;
+
 	gpio -= chip->base;
-	if (gpio >= chip->ngpio)
-		goto done;

 #ifdef CONFIG_DEBUG_FS
 	if (chip->requested[gpio])
@@ -218,9 +226,11 @@ int gpio_direction_input(unsigned gpio)
 	chip = gpio_to_chip(gpio);
 	if (!chip || !chip->get || !chip->direction_input)
 		goto fail;
-	gpio -= chip->base;
-	if (gpio >= chip->ngpio)
+
+	if (!gpio_is_onchip(gpio, chip))
 		goto fail;
+
+	gpio -= chip->base;
 	gpio_ensure_requested(chip, gpio);

 	/* now we know the gpio is valid and chip won't vanish */
@@ -250,9 +260,11 @@ int gpio_direction_output(unsigned gpio, int value)
 	chip = gpio_to_chip(gpio);
 	if (!chip || !chip->get || !chip->direction_output)
 		goto fail;
-	gpio -= chip->base;
-	if (gpio >= chip->ngpio)
+	
+	if (!gpio_is_onchip(gpio, chip))
 		goto fail;
+
+	gpio -= chip->base;
 	gpio_ensure_requested(chip, gpio);

 	/* now we know the gpio is valid and chip won't vanish */
-- 
1.5.2.5.GIT
-
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[patch/rfc 1/4] GPIO implementation framework, David Brownell, (Mon Oct 29, 9:51 pm)
Re: [patch/rfc 1/4] GPIO implementation framework, David Brownell, (Mon Nov 5, 5:05 pm)
Re: [patch/rfc 1/4] GPIO implementation framework, eric miao, (Mon Nov 12, 10:28 pm)
Re: [patch/rfc 1/4] GPIO implementation framework, David Brownell, (Tue Nov 13, 3:06 pm)
Re: [patch/rfc 1/4] GPIO implementation framework, eric miao, (Tue Nov 13, 8:57 pm)
Re: [patch/rfc 1/4] GPIO implementation framework, David Brownell, (Tue Nov 13, 11:30 pm)
Re: [patch/rfc 1/4] GPIO implementation framework, eric miao, (Wed Nov 14, 2:40 am)
Re: [patch/rfc 1/4] GPIO implementation framework, David Brownell, (Wed Nov 14, 3:08 am)
Re: [patch/rfc 1/4] GPIO implementation framework, David Brownell, (Mon Nov 26, 9:46 pm)
Re: [patch/rfc 1/4] GPIO implementation framework, eric miao, (Tue Nov 27, 6:58 am)
[patch/rfc 2.6.24-rc3-mm] gpiolib grows a gpio_desc, David Brownell, (Tue Nov 27, 11:15 pm)
Re: [patch/rfc 2.6.24-rc3-mm] gpiolib grows a gpio_desc, David Brownell, (Wed Nov 28, 5:53 am)
Re: [patch/rfc 1/4] GPIO implementation framework, David Brownell, (Tue Nov 27, 3:29 pm)
Re: [patch/rfc 1/4] GPIO implementation framework, eric miao, (Wed Nov 28, 1:11 am)
Re: [patch/rfc 1/4] GPIO implementation framework, David Brownell, (Tue Nov 27, 3:03 pm)
Re: [patch/rfc 1/4] GPIO implementation framework, David Brownell, (Tue Nov 27, 1:26 pm)
Re: [patch/rfc 1/4] GPIO implementation framework, eric miao, (Tue Nov 13, 9:00 pm)
Re: [patch/rfc 1/4] GPIO implementation framework, David Brownell, (Tue Nov 13, 11:25 pm)
Re: [patch/rfc 1/4] GPIO implementation framework, eric miao, (Wed Nov 14, 2:37 am)
Re: [patch/rfc 1/4] GPIO implementation framework, David Brownell, (Tue Nov 13, 11:53 pm)
Re: [patch/rfc 1/4] GPIO implementation framework, eric miao, (Tue Nov 13, 9:02 pm)
Re: [patch/rfc 1/4] GPIO implementation framework, David Brownell, (Tue Nov 13, 11:28 pm)
Re: [patch/rfc 1/4] GPIO implementation framework, eric miao, (Tue Nov 13, 9:03 pm)
Re: [patch/rfc 1/4] GPIO implementation framework, David Brownell, (Wed Nov 14, 12:18 am)
Re: [patch/rfc 1/4] GPIO implementation framework, eric miao, (Wed Nov 14, 2:46 am)
Re: [patch/rfc 1/4] GPIO implementation framework, eric miao, (Tue Nov 13, 9:04 pm)
Re: [patch/rfc 1/4] GPIO implementation framework, eric miao, (Tue Nov 13, 9:04 pm)
Re: [patch/rfc 1/4] GPIO implementation framework, David Brownell, (Wed Nov 14, 12:36 am)
Re: [patch/rfc 1/4] GPIO implementation framework, Jean Delvare, (Sat Nov 17, 6:38 am)
Re: [patch/rfc 1/4] GPIO implementation framework, David Brownell, (Sat Nov 17, 1:36 pm)
Re: [patch/rfc 1/4] GPIO implementation framework, Jean Delvare, (Tue Nov 20, 11:20 am)
Re: [patch/rfc 1/4] GPIO implementation framework, eric miao, (Wed Nov 14, 2:51 am)
Re: [patch/rfc 1/4] GPIO implementation framework, David Brownell, (Wed Nov 14, 3:19 am)
Re: [patch/rfc 1/4] GPIO implementation framework, eric miao, (Wed Nov 14, 3:36 am)