diff mbox

gpio: pca953x: Fix pca953x_gpio_set_multiple() on 64-bit

Message ID 1457713887-7748-1-git-send-email-geert+renesas@glider.be
State New
Headers show

Commit Message

Geert Uytterhoeven March 11, 2016, 4:31 p.m. UTC
pca953x_gpio_set_multiple() divides by 4 to convert from longs to bytes,
which assumes a 32-bit platform, and is not correct on 64-bit platforms.
Use "sizeof(...)" instead to fix this.

Fixes: b4818afeacbd8182 ("gpio: pca953x: Add set_multiple to allow multiple bits to be set in one write.")
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
---
Untested, found by code inspection.

This bug was introduced in v4.5-rc1, but unless Linus (the other one ;-)
will make a v4.5-rc8, it's probably too late to get this fixed in v4.5.
---
 drivers/gpio/gpio-pca953x.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Phil Reid March 12, 2016, 3:20 p.m. UTC | #1
On 12/03/2016 12:31 AM, Geert Uytterhoeven wrote:
> pca953x_gpio_set_multiple() divides by 4 to convert from longs to bytes,
> which assumes a 32-bit platform, and is not correct on 64-bit platforms.
> Use "sizeof(...)" instead to fix this.
>
> Fixes: b4818afeacbd8182 ("gpio: pca953x: Add set_multiple to allow multiple bits to be set in one write.")
> Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>

Acked-by: Phil Reid <preid@electromag.com.au>

> ---
> Untested, found by code inspection.
>
> This bug was introduced in v4.5-rc1, but unless Linus (the other one ;-)
> will make a v4.5-rc8, it's probably too late to get this fixed in v4.5.
> ---
>   drivers/gpio/gpio-pca953x.c | 6 ++++--
>   1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/gpio/gpio-pca953x.c b/drivers/gpio/gpio-pca953x.c
> index 23196c5fc17ceb6b..99b375c959987fe9 100644
> --- a/drivers/gpio/gpio-pca953x.c
> +++ b/drivers/gpio/gpio-pca953x.c
> @@ -367,9 +367,11 @@ static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
>   	memcpy(reg_val, chip->reg_output, NBANK(chip));
>   	mutex_lock(&chip->i2c_lock);
>   	for(bank=0; bank<NBANK(chip); bank++) {
> -		unsigned bankmask = mask[bank/4] >> ((bank % 4) * 8);
> +		unsigned bankmask = mask[bank / sizeof(*mask)] >>
> +				    ((bank % sizeof(*mask)) * 8);
>   		if(bankmask) {
> -			unsigned bankval  = bits[bank/4] >> ((bank % 4) * 8);
> +			unsigned bankval  = bits[bank / sizeof(*bits)] >>
> +					    ((bank % sizeof(*bits)) * 8);
>   			reg_val[bank] = (reg_val[bank] & ~bankmask) | bankval;
>   		}
>   	}
>
Linus Walleij March 16, 2016, 12:03 p.m. UTC | #2
On Fri, Mar 11, 2016 at 5:31 PM, Geert Uytterhoeven
<geert+renesas@glider.be> wrote:

> pca953x_gpio_set_multiple() divides by 4 to convert from longs to bytes,
> which assumes a 32-bit platform, and is not correct on 64-bit platforms.
> Use "sizeof(...)" instead to fix this.
>
> Fixes: b4818afeacbd8182 ("gpio: pca953x: Add set_multiple to allow multiple bits to be set in one write.")
> Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>

Patch applied for fixes. Tagged for stable.
Ack from Phil.

Yours,
Linus Walleij
--
To unsubscribe from this list: send the line "unsubscribe linux-gpio" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/gpio/gpio-pca953x.c b/drivers/gpio/gpio-pca953x.c
index 23196c5fc17ceb6b..99b375c959987fe9 100644
--- a/drivers/gpio/gpio-pca953x.c
+++ b/drivers/gpio/gpio-pca953x.c
@@ -367,9 +367,11 @@  static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
 	memcpy(reg_val, chip->reg_output, NBANK(chip));
 	mutex_lock(&chip->i2c_lock);
 	for(bank=0; bank<NBANK(chip); bank++) {
-		unsigned bankmask = mask[bank/4] >> ((bank % 4) * 8);
+		unsigned bankmask = mask[bank / sizeof(*mask)] >>
+				    ((bank % sizeof(*mask)) * 8);
 		if(bankmask) {
-			unsigned bankval  = bits[bank/4] >> ((bank % 4) * 8);
+			unsigned bankval  = bits[bank / sizeof(*bits)] >>
+					    ((bank % sizeof(*bits)) * 8);
 			reg_val[bank] = (reg_val[bank] & ~bankmask) | bankval;
 		}
 	}