diff options
author | Andrey Smirnov <andrew.smirnov@gmail.com> | 2017-07-28 07:44:17 -0700 |
---|---|---|
committer | Lucas Stach <l.stach@pengutronix.de> | 2017-07-30 16:07:40 +0200 |
commit | 3918ca48a7fb20488786dd9644c31e6cae3195bd (patch) | |
tree | 87255ba3a0c4494f1df3e5aab08fde604a6fc6c8 /drivers/gpio | |
parent | a0c26429e472a175ac6b1736c8687a19a76e97a9 (diff) | |
download | barebox-3918ca48a7fb20488786dd9644c31e6cae3195bd.tar.gz barebox-3918ca48a7fb20488786dd9644c31e6cae3195bd.tar.xz |
gpiolib: Fix buggy flag detection code
Both GPIOF_ACTIVE_LOW and GPIOF_INIT_ACTIVE are multi-bit constants so
detecting their assertion using simple bit-wise and is incorrect and
would lead to false positives.
Fixes: bbc499914 ("gpiolib: Add code to support "active low" GPIOs")
Acked-by: Sam Ravnborg <sam@ravnborg.org>
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
Signed-off-by: Lucas Stach <l.stach@pengutronix.de>
Diffstat (limited to 'drivers/gpio')
-rw-r--r-- | drivers/gpio/gpiolib.c | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 1a373ef149..d081e02fd4 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -120,24 +120,31 @@ int gpio_request_one(unsigned gpio, unsigned long flags, const char *label) { int err; + /* + * Not all of the flags below are mulit-bit, but, for the sake + * of consistency, the code is written as if all of them were. + */ + const bool active_low = (flags & GPIOF_ACTIVE_LOW) == GPIOF_ACTIVE_LOW; + const bool dir_in = (flags & GPIOF_DIR_IN) == GPIOF_DIR_IN; + const bool logical = (flags & GPIOF_LOGICAL) == GPIOF_LOGICAL; + const bool init_active = (flags & GPIOF_INIT_ACTIVE) == GPIOF_INIT_ACTIVE; + const bool init_high = (flags & GPIOF_INIT_HIGH) == GPIOF_INIT_HIGH; + err = gpio_request(gpio, label); if (err) return err; - if (flags & GPIOF_ACTIVE_LOW) { + if (active_low) { struct gpio_info *gi = gpio_to_desc(gpio); gi->active_low = true; } - if (flags & GPIOF_DIR_IN) { + if (dir_in) err = gpio_direction_input(gpio); - } else if (flags & GPIOF_LOGICAL) { - err = gpio_direction_active(gpio, - !!(flags & GPIOF_INIT_ACTIVE)); - } else { - err = gpio_direction_output(gpio, - !!(flags & GPIOF_INIT_HIGH)); - } + else if (logical) + err = gpio_direction_active(gpio, init_active); + else + err = gpio_direction_output(gpio, init_high); if (err) goto free_gpio; |