surprise :p
[openwrt.git] / target / linux / generic-2.6 / patches-2.6.25 / 961-backport_gpio_define_gpio_valid.patch
1 From: Guennadi Liakhovetski <g.liakhovetski@pengutronix.de>
2 Date: Mon, 28 Apr 2008 09:14:46 +0000 (-0700)
3 Subject: gpio: define gpio_is_valid()
4 X-Git-Tag: v2.6.26-rc1~849
5 X-Git-Url: http://git.kernel.org/?p=linux%2Fkernel%2Fgit%2Ftorvalds%2Flinux-2.6.git;a=commitdiff_plain;h=e6de1808f8ebfeb7e49f3c5a30cb8f2032beb287
6
7 gpio: define gpio_is_valid()
8
9 Introduce a gpio_is_valid() predicate; use it in gpiolib.
10
11 Signed-off-by: Guennadi Liakhovetski <g.liakhovetski@pengutronix.de>
12     [ use inline function; follow the gpio_* naming convention;
13       work without gpiolib; all programming interfaces need docs ]
14 Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
15 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
16 Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
17 ---
18
19 diff --git a/Documentation/gpio.txt b/Documentation/gpio.txt
20 index 5463009..c35ca9e 100644
21 --- a/Documentation/gpio.txt
22 +++ b/Documentation/gpio.txt
23 @@ -107,6 +107,16 @@ type of GPIO controller, and on one particular board 80-95 with an FPGA.
24  The numbers need not be contiguous; either of those platforms could also
25  use numbers 2000-2063 to identify GPIOs in a bank of I2C GPIO expanders.
26  
27 +If you want to initialize a structure with an invalid GPIO number, use
28 +some negative number (perhaps "-EINVAL"); that will never be valid.  To
29 +test if a number could reference a GPIO, you may use this predicate:
30 +
31 +       int gpio_is_valid(int number);
32 +
33 +A number that's not valid will be rejected by calls which may request
34 +or free GPIOs (see below).  Other numbers may also be rejected; for
35 +example, a number might be valid but unused on a given board.
36 +
37  Whether a platform supports multiple GPIO controllers is currently a
38  platform-specific implementation issue.
39  
40 diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
41 index eb75d12..623fcd9 100644
42 --- a/drivers/gpio/gpiolib.c
43 +++ b/drivers/gpio/gpiolib.c
44 @@ -99,7 +99,7 @@ int gpiochip_add(struct gpio_chip *chip)
45          * dynamic allocation.  We don't currently support that.
46          */
47  
48 -       if (chip->base < 0 || (chip->base  + chip->ngpio) >= ARCH_NR_GPIOS) {
49 +       if (chip->base < 0 || !gpio_is_valid(chip->base  + chip->ngpio)) {
50                 status = -EINVAL;
51                 goto fail;
52         }
53 @@ -174,7 +174,7 @@ int gpio_request(unsigned gpio, const char *label)
54  
55         spin_lock_irqsave(&gpio_lock, flags);
56  
57 -       if (gpio >= ARCH_NR_GPIOS)
58 +       if (!gpio_is_valid(gpio))
59                 goto done;
60         desc = &gpio_desc[gpio];
61         if (desc->chip == NULL)
62 @@ -209,7 +209,7 @@ void gpio_free(unsigned gpio)
63         unsigned long           flags;
64         struct gpio_desc        *desc;
65  
66 -       if (gpio >= ARCH_NR_GPIOS) {
67 +       if (!gpio_is_valid(gpio)) {
68                 WARN_ON(extra_checks);
69                 return;
70         }
71 @@ -245,7 +245,7 @@ const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
72  {
73         unsigned gpio = chip->base + offset;
74  
75 -       if (gpio >= ARCH_NR_GPIOS || gpio_desc[gpio].chip != chip)
76 +       if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
77                 return NULL;
78         if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
79                 return NULL;
80 @@ -276,7 +276,7 @@ int gpio_direction_input(unsigned gpio)
81  
82         spin_lock_irqsave(&gpio_lock, flags);
83  
84 -       if (gpio >= ARCH_NR_GPIOS)
85 +       if (!gpio_is_valid(gpio))
86                 goto fail;
87         chip = desc->chip;
88         if (!chip || !chip->get || !chip->direction_input)
89 @@ -314,7 +314,7 @@ int gpio_direction_output(unsigned gpio, int value)
90  
91         spin_lock_irqsave(&gpio_lock, flags);
92  
93 -       if (gpio >= ARCH_NR_GPIOS)
94 +       if (!gpio_is_valid(gpio))
95                 goto fail;
96         chip = desc->chip;
97         if (!chip || !chip->set || !chip->direction_output)
98 @@ -531,7 +531,7 @@ static int gpiolib_show(struct seq_file *s, void *unused)
99  
100         /* REVISIT this isn't locked against gpio_chip removal ... */
101  
102 -       for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
103 +       for (gpio = 0; gpio_is_valid(gpio); gpio++) {
104                 if (chip == gpio_desc[gpio].chip)
105                         continue;
106                 chip = gpio_desc[gpio].chip;
107 diff --git a/include/asm-generic/gpio.h b/include/asm-generic/gpio.h
108 index 7e77b6f..464c5b3 100644
109 --- a/include/asm-generic/gpio.h
110 +++ b/include/asm-generic/gpio.h
111 @@ -16,6 +16,12 @@
112  #define ARCH_NR_GPIOS          256
113  #endif
114  
115 +static inline int gpio_is_valid(int number)
116 +{
117 +       /* only some non-negative numbers are valid */
118 +       return ((unsigned)number) < ARCH_NR_GPIOS;
119 +}
120 +
121  struct seq_file;
122  struct module;
123  
124 @@ -99,6 +105,12 @@ extern int __gpio_cansleep(unsigned gpio);
125  
126  #else
127  
128 +static inline int gpio_is_valid(int number)
129 +{
130 +       /* only non-negative numbers are valid */
131 +       return number >= 0;
132 +}
133 +
134  /* platforms that don't directly support access to GPIOs through I2C, SPI,
135   * or other blocking infrastructure can use these wrappers.
136   */