Patchwork [U-Boot,PATCHv2,06/13] nomadik-gpio: check for invalid gpio numbers

login
register
mail settings
Submitter Rabin Vincent
Date April 8, 2010, 1:43 p.m.
Message ID <1270734200-17762-7-git-send-email-rabin.vincent@stericsson.com>
Download mbox | patch
Permalink /patch/71742/
State Not Applicable
Delegated to: Albert ARIBAUD
Headers show

Comments

Rabin Vincent - April 8, 2010, 1:43 p.m.
Cc: Alessandro Rubini <rubini@unipv.it>
Acked-by: Michael Brandt <michael.brandt@stericsson.com>
Signed-off-by: Rabin Vincent <rabin.vincent@stericsson.com>
---
 drivers/gpio/nomadik_gpio.c |   17 +++++++++++++++++
 1 files changed, 17 insertions(+), 0 deletions(-)

Patch

diff --git a/drivers/gpio/nomadik_gpio.c b/drivers/gpio/nomadik_gpio.c
index 670b684..d084235 100644
--- a/drivers/gpio/nomadik_gpio.c
+++ b/drivers/gpio/nomadik_gpio.c
@@ -45,6 +45,11 @@  enum gpio_registers {
 
 static inline unsigned long gpio_to_base(int gpio)
 {
+	if ((gpio / 32) >= ARRAY_SIZE(gpio_base)) {
+		printf("nomadik-gpio: invalid gpio %d\n", gpio);
+		return 0;
+	}
+
 	return gpio_base[gpio / 32];
 }
 
@@ -59,6 +64,9 @@  void nmk_gpio_af(int gpio, int alternate_function)
 	u32 bit = gpio_to_bit(gpio);
 	u32 afunc, bfunc;
 
+	if (!base)
+		return;
+
 	/* alternate function is 0..3, with one bit per register */
 	afunc = readl(base + GPIO_AFSLA) & ~bit;
 	bfunc = readl(base + GPIO_AFSLB) & ~bit;
@@ -73,6 +81,9 @@  void nmk_gpio_dir(int gpio, int dir)
 	unsigned long base = gpio_to_base(gpio);
 	u32 bit = gpio_to_bit(gpio);
 
+	if (!base)
+		return;
+
 	if (dir)
 		writel(bit, base + GPIO_DIRS);
 	else
@@ -84,6 +95,9 @@  void nmk_gpio_set(int gpio, int val)
 	unsigned long base = gpio_to_base(gpio);
 	u32 bit = gpio_to_bit(gpio);
 
+	if (!base)
+		return;
+
 	if (val)
 		writel(bit, base + GPIO_DATS);
 	else
@@ -95,5 +109,8 @@  int nmk_gpio_get(int gpio)
 	unsigned long base = gpio_to_base(gpio);
 	u32 bit = gpio_to_bit(gpio);
 
+	if (!base)
+		return -1;
+
 	return readl(base + GPIO_DAT) & bit;
 }