Commit e6de1808f8ebfeb7e49f3c5a30cb8f2032beb287

Authored by Guennadi Liakhovetski
Committed by Linus Torvalds
1 parent d72cbed0c4

gpio: define gpio_is_valid()

Introduce a gpio_is_valid() predicate; use it in gpiolib.

Signed-off-by: Guennadi Liakhovetski <g.liakhovetski@pengutronix.de>
    [ use inline function; follow the gpio_* naming convention;
      work without gpiolib; all programming interfaces need docs ]
Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Showing 3 changed files with 29 additions and 7 deletions Side-by-side Diff

Documentation/gpio.txt
... ... @@ -107,6 +107,16 @@
107 107 The numbers need not be contiguous; either of those platforms could also
108 108 use numbers 2000-2063 to identify GPIOs in a bank of I2C GPIO expanders.
109 109  
  110 +If you want to initialize a structure with an invalid GPIO number, use
  111 +some negative number (perhaps "-EINVAL"); that will never be valid. To
  112 +test if a number could reference a GPIO, you may use this predicate:
  113 +
  114 + int gpio_is_valid(int number);
  115 +
  116 +A number that's not valid will be rejected by calls which may request
  117 +or free GPIOs (see below). Other numbers may also be rejected; for
  118 +example, a number might be valid but unused on a given board.
  119 +
110 120 Whether a platform supports multiple GPIO controllers is currently a
111 121 platform-specific implementation issue.
112 122  
drivers/gpio/gpiolib.c
... ... @@ -99,7 +99,7 @@
99 99 * dynamic allocation. We don't currently support that.
100 100 */
101 101  
102   - if (chip->base < 0 || (chip->base + chip->ngpio) >= ARCH_NR_GPIOS) {
  102 + if (chip->base < 0 || !gpio_is_valid(chip->base + chip->ngpio)) {
103 103 status = -EINVAL;
104 104 goto fail;
105 105 }
... ... @@ -174,7 +174,7 @@
174 174  
175 175 spin_lock_irqsave(&gpio_lock, flags);
176 176  
177   - if (gpio >= ARCH_NR_GPIOS)
  177 + if (!gpio_is_valid(gpio))
178 178 goto done;
179 179 desc = &gpio_desc[gpio];
180 180 if (desc->chip == NULL)
... ... @@ -209,7 +209,7 @@
209 209 unsigned long flags;
210 210 struct gpio_desc *desc;
211 211  
212   - if (gpio >= ARCH_NR_GPIOS) {
  212 + if (!gpio_is_valid(gpio)) {
213 213 WARN_ON(extra_checks);
214 214 return;
215 215 }
... ... @@ -245,7 +245,7 @@
245 245 {
246 246 unsigned gpio = chip->base + offset;
247 247  
248   - if (gpio >= ARCH_NR_GPIOS || gpio_desc[gpio].chip != chip)
  248 + if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
249 249 return NULL;
250 250 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
251 251 return NULL;
... ... @@ -276,7 +276,7 @@
276 276  
277 277 spin_lock_irqsave(&gpio_lock, flags);
278 278  
279   - if (gpio >= ARCH_NR_GPIOS)
  279 + if (!gpio_is_valid(gpio))
280 280 goto fail;
281 281 chip = desc->chip;
282 282 if (!chip || !chip->get || !chip->direction_input)
... ... @@ -314,7 +314,7 @@
314 314  
315 315 spin_lock_irqsave(&gpio_lock, flags);
316 316  
317   - if (gpio >= ARCH_NR_GPIOS)
  317 + if (!gpio_is_valid(gpio))
318 318 goto fail;
319 319 chip = desc->chip;
320 320 if (!chip || !chip->set || !chip->direction_output)
... ... @@ -531,7 +531,7 @@
531 531  
532 532 /* REVISIT this isn't locked against gpio_chip removal ... */
533 533  
534   - for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
  534 + for (gpio = 0; gpio_is_valid(gpio); gpio++) {
535 535 if (chip == gpio_desc[gpio].chip)
536 536 continue;
537 537 chip = gpio_desc[gpio].chip;
include/asm-generic/gpio.h
... ... @@ -16,6 +16,12 @@
16 16 #define ARCH_NR_GPIOS 256
17 17 #endif
18 18  
  19 +static inline int gpio_is_valid(int number)
  20 +{
  21 + /* only some non-negative numbers are valid */
  22 + return ((unsigned)number) < ARCH_NR_GPIOS;
  23 +}
  24 +
19 25 struct seq_file;
20 26 struct module;
21 27  
... ... @@ -98,6 +104,12 @@
98 104  
99 105  
100 106 #else
  107 +
  108 +static inline int gpio_is_valid(int number)
  109 +{
  110 + /* only non-negative numbers are valid */
  111 + return number >= 0;
  112 +}
101 113  
102 114 /* platforms that don't directly support access to GPIOs through I2C, SPI,
103 115 * or other blocking infrastructure can use these wrappers.