mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Andrey Smirnov <andrew.smirnov@gmail.com>
To: barebox@lists.infradead.org
Cc: Andrey Smirnov <andrew.smirnov@gmail.com>
Subject: [PATCH v2 01/11] gpiolib: Fix buggy flag detection code
Date: Wed, 26 Jul 2017 09:31:10 -0700	[thread overview]
Message-ID: <20170726163120.25952-2-andrew.smirnov@gmail.com> (raw)
In-Reply-To: <20170726163120.25952-1-andrew.smirnov@gmail.com>

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>
---
 drivers/gpio/gpiolib.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 1a373ef14..d081e02fd 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;
-- 
2.13.3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2017-07-26 16:31 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-26 16:31 [PATCH v2 00/11] i.MX7 SabreSD support Andrey Smirnov
2017-07-26 16:31 ` Andrey Smirnov [this message]
2017-07-26 16:31 ` [PATCH v2 02/11] gpiolib: Simplify code of gpio_request_one() Andrey Smirnov
2017-07-26 16:31 ` [PATCH v2 03/11] clk: i.MX7: Remove unused UART clocks array Andrey Smirnov
2017-07-26 16:31 ` [PATCH v2 04/11] ARM: i.MX: Import mx7d_pins.h from U-Boot Andrey Smirnov
2017-07-26 16:31 ` [PATCH v2 05/11] ARM: i.MX: Add mx7_setup_pad() Andrey Smirnov
2017-07-26 16:31 ` [PATCH v2 06/11] ARM: i.MX: Add imx7_uart_setup_ll() Andrey Smirnov
2017-07-26 16:31 ` [PATCH v2 07/11] ARM: i.MX: Add minimal imx7-ccm-regs.h Andrey Smirnov
2017-07-26 16:31 ` [PATCH v2 08/11] ARM: i.MX: Add ARCH_HAD_FEC_IMX to ARCH_IMX7 Andrey Smirnov
2017-07-26 16:31 ` [PATCH v2 09/11] ARM: i.MX: Import imx7-iomuxc-gpr.h from Linux kernel Andrey Smirnov
2017-07-26 16:31 ` [PATCH v2 10/11] include: Add phy-id-list.h Andrey Smirnov
2017-07-26 16:31 ` [PATCH v2 11/11] ARM: i.MX: Add support for NXP i.MX7 SABRESD board Andrey Smirnov
2017-07-26 16:55   ` Sam Ravnborg
2017-07-26 16:56 ` [PATCH v2 00/11] i.MX7 SabreSD support Sam Ravnborg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170726163120.25952-2-andrew.smirnov@gmail.com \
    --to=andrew.smirnov@gmail.com \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox