Skip to contentSkip to conversation

introduce is_valid_gpio() predicate and use it in gpiolib.c

PATCH In review Guennadi Liakhovetski 2 participants 2 messages Feb 10, 2008

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 introduce is_valid_gpio() predicate and use it in gpiolib.c

Introduce a is_valid_gpio() predicate to be used in gpiolib core and drivers.

---

drivers/gpio/gpiolib.c +7 -7
index d8db2f8..5390f46 100644
@@ -96,7 +96,7 @@ int gpiochip_add(struct gpio_chip *chip)
 	 * dynamic allocation.  We don't currently support that.
 	 */
 
-	if (chip->base < 0 || (chip->base  + chip->ngpio) >= ARCH_NR_GPIOS) {
+	if (chip->base < 0 || !is_valid_gpio(chip->base  + chip->ngpio)) {
 		status = -EINVAL;
 		goto fail;
 	}
@@ -171,7 +171,7 @@ int gpio_request(unsigned gpio, const char *label)
 
 	spin_lock_irqsave(&gpio_lock, flags);
 
-	if (gpio >= ARCH_NR_GPIOS)
+	if (!is_valid_gpio(gpio))
 		goto done;
 	desc = &gpio_desc[gpio];
 	if (desc->chip == NULL)
@@ -201,7 +201,7 @@ void gpio_free(unsigned gpio)
 	unsigned long		flags;
 	struct gpio_desc	*desc;
 
-	if (gpio >= ARCH_NR_GPIOS) {
+	if (!is_valid_gpio(gpio)) {
 		WARN_ON(extra_checks);
 		return;
 	}
@@ -236,7 +236,7 @@ const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
 {
 	unsigned gpio = chip->base + offset;
 
-	if (gpio >= ARCH_NR_GPIOS || gpio_desc[gpio].chip != chip)
+	if (!is_valid_gpio(gpio) || gpio_desc[gpio].chip != chip)
 		return NULL;
 	if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
 		return NULL;
@@ -267,7 +267,7 @@ int gpio_direction_input(unsigned gpio)
 
 	spin_lock_irqsave(&gpio_lock, flags);
 
-	if (gpio >= ARCH_NR_GPIOS)
+	if (!is_valid_gpio(gpio))
 		goto fail;
 	chip = desc->chip;
 	if (!chip || !chip->get || !chip->direction_input)
@@ -305,7 +305,7 @@ int gpio_direction_output(unsigned gpio, int value)
 
 	spin_lock_irqsave(&gpio_lock, flags);
 
-	if (gpio >= ARCH_NR_GPIOS)
+	if (!is_valid_gpio(gpio))
 		goto fail;
 	chip = desc->chip;
 	if (!chip || !chip->set || !chip->direction_output)
@@ -522,7 +522,7 @@ static int gpiolib_show(struct seq_file *s, void *unused)
 
 	/* REVISIT this isn't locked against gpio_chip removal ... */
 
-	for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
+	for (gpio = 0; is_valid_gpio(gpio); gpio++) {
 		if (chip == gpio_desc[gpio].chip)
 			continue;
 		chip = gpio_desc[gpio].chip;
include/asm-generic/gpio.h +2 -0
index f29a502..87dab12 100644
@@ -16,6 +16,8 @@
 #define ARCH_NR_GPIOS		256
 #endif
 
+#define is_valid_gpio(gpio)	((unsigned int)(gpio) < ARCH_NR_GPIOS)
+
 struct seq_file;
 
 /**
#1 lore raw

I like this, but can't resist making the "Andrew Comment":

include/asm-generic/gpio.h line 19

Quoted 7 lines
include/asm-generic/gpio.h +1 -0
@@ -16,6 +16,8 @@
 #define ARCH_NR_GPIOS		256
 #endif
 
+#define is_valid_gpio(gpio)	((unsigned int)(gpio) < ARCH_NR_GPIOS)

Please make this an inline C function. :)

Quoted 5 lines
include/asm-generic/gpio.h +1 -0
+
 struct seq_file;
 
 /**