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
-
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/

Reply via email to