Commit e6de1808 authored by Guennadi Liakhovetski's avatar Guennadi Liakhovetski Committed by Linus Torvalds

gpio: define gpio_is_valid()

Introduce a gpio_is_valid() predicate; use it in gpiolib.
Signed-off-by: default avatarGuennadi Liakhovetski <g.liakhovetski@pengutronix.de>
    [ use inline function; follow the gpio_* naming convention;
      work without gpiolib; all programming interfaces need docs ]
Signed-off-by: default avatarDavid Brownell <dbrownell@users.sourceforge.net>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent d72cbed0
...@@ -107,6 +107,16 @@ type of GPIO controller, and on one particular board 80-95 with an FPGA. ...@@ -107,6 +107,16 @@ type of GPIO controller, and on one particular board 80-95 with an FPGA.
The numbers need not be contiguous; either of those platforms could also The numbers need not be contiguous; either of those platforms could also
use numbers 2000-2063 to identify GPIOs in a bank of I2C GPIO expanders. use numbers 2000-2063 to identify GPIOs in a bank of I2C GPIO expanders.
If you want to initialize a structure with an invalid GPIO number, use
some negative number (perhaps "-EINVAL"); that will never be valid. To
test if a number could reference a GPIO, you may use this predicate:
int gpio_is_valid(int number);
A number that's not valid will be rejected by calls which may request
or free GPIOs (see below). Other numbers may also be rejected; for
example, a number might be valid but unused on a given board.
Whether a platform supports multiple GPIO controllers is currently a Whether a platform supports multiple GPIO controllers is currently a
platform-specific implementation issue. platform-specific implementation issue.
......
...@@ -99,7 +99,7 @@ int gpiochip_add(struct gpio_chip *chip) ...@@ -99,7 +99,7 @@ int gpiochip_add(struct gpio_chip *chip)
* dynamic allocation. We don't currently support that. * dynamic allocation. We don't currently support that.
*/ */
if (chip->base < 0 || (chip->base + chip->ngpio) >= ARCH_NR_GPIOS) { if (chip->base < 0 || !gpio_is_valid(chip->base + chip->ngpio)) {
status = -EINVAL; status = -EINVAL;
goto fail; goto fail;
} }
...@@ -174,7 +174,7 @@ int gpio_request(unsigned gpio, const char *label) ...@@ -174,7 +174,7 @@ int gpio_request(unsigned gpio, const char *label)
spin_lock_irqsave(&gpio_lock, flags); spin_lock_irqsave(&gpio_lock, flags);
if (gpio >= ARCH_NR_GPIOS) if (!gpio_is_valid(gpio))
goto done; goto done;
desc = &gpio_desc[gpio]; desc = &gpio_desc[gpio];
if (desc->chip == NULL) if (desc->chip == NULL)
...@@ -209,7 +209,7 @@ void gpio_free(unsigned gpio) ...@@ -209,7 +209,7 @@ void gpio_free(unsigned gpio)
unsigned long flags; unsigned long flags;
struct gpio_desc *desc; struct gpio_desc *desc;
if (gpio >= ARCH_NR_GPIOS) { if (!gpio_is_valid(gpio)) {
WARN_ON(extra_checks); WARN_ON(extra_checks);
return; return;
} }
...@@ -245,7 +245,7 @@ const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) ...@@ -245,7 +245,7 @@ const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
{ {
unsigned gpio = chip->base + offset; unsigned gpio = chip->base + offset;
if (gpio >= ARCH_NR_GPIOS || gpio_desc[gpio].chip != chip) if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
return NULL; return NULL;
if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0) if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
return NULL; return NULL;
...@@ -276,7 +276,7 @@ int gpio_direction_input(unsigned gpio) ...@@ -276,7 +276,7 @@ int gpio_direction_input(unsigned gpio)
spin_lock_irqsave(&gpio_lock, flags); spin_lock_irqsave(&gpio_lock, flags);
if (gpio >= ARCH_NR_GPIOS) if (!gpio_is_valid(gpio))
goto fail; goto fail;
chip = desc->chip; chip = desc->chip;
if (!chip || !chip->get || !chip->direction_input) if (!chip || !chip->get || !chip->direction_input)
...@@ -314,7 +314,7 @@ int gpio_direction_output(unsigned gpio, int value) ...@@ -314,7 +314,7 @@ int gpio_direction_output(unsigned gpio, int value)
spin_lock_irqsave(&gpio_lock, flags); spin_lock_irqsave(&gpio_lock, flags);
if (gpio >= ARCH_NR_GPIOS) if (!gpio_is_valid(gpio))
goto fail; goto fail;
chip = desc->chip; chip = desc->chip;
if (!chip || !chip->set || !chip->direction_output) if (!chip || !chip->set || !chip->direction_output)
...@@ -531,7 +531,7 @@ static int gpiolib_show(struct seq_file *s, void *unused) ...@@ -531,7 +531,7 @@ static int gpiolib_show(struct seq_file *s, void *unused)
/* REVISIT this isn't locked against gpio_chip removal ... */ /* REVISIT this isn't locked against gpio_chip removal ... */
for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) { for (gpio = 0; gpio_is_valid(gpio); gpio++) {
if (chip == gpio_desc[gpio].chip) if (chip == gpio_desc[gpio].chip)
continue; continue;
chip = gpio_desc[gpio].chip; chip = gpio_desc[gpio].chip;
......
...@@ -16,6 +16,12 @@ ...@@ -16,6 +16,12 @@
#define ARCH_NR_GPIOS 256 #define ARCH_NR_GPIOS 256
#endif #endif
static inline int gpio_is_valid(int number)
{
/* only some non-negative numbers are valid */
return ((unsigned)number) < ARCH_NR_GPIOS;
}
struct seq_file; struct seq_file;
struct module; struct module;
...@@ -99,6 +105,12 @@ extern int __gpio_cansleep(unsigned gpio); ...@@ -99,6 +105,12 @@ extern int __gpio_cansleep(unsigned gpio);
#else #else
static inline int gpio_is_valid(int number)
{
/* only non-negative numbers are valid */
return number >= 0;
}
/* platforms that don't directly support access to GPIOs through I2C, SPI, /* platforms that don't directly support access to GPIOs through I2C, SPI,
* or other blocking infrastructure can use these wrappers. * or other blocking infrastructure can use these wrappers.
*/ */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment