Skip to contentSkip to conversation

gpio: pca953x: avoid to use uninitialized value pinctrl

PATCH In review Andy Shevchenko 2 participants 7 messages Dec 10, 2022 – Dec 30, 2022 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/4 gpio: pca953x: avoid to use uninitialized value pinctrl

From: Haibo Chen <[email protected]>

There is a variable pinctrl declared without initializer. And then has the case (switch operation chose the default case) to directly use this uninitialized value, this is not a safe behavior. So here initialize the pinctrl as 0 to avoid this issue.
This is reported by Coverity.

Fixes 13c5d4ce8060 ("gpio: pca953x: Add support for PCAL6534")

---
v2: used default case (Andy)

 drivers/gpio/gpio-pca953x.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpio/gpio-pca953x.c +3 -0
index a59d61cd44b2..5299e5bb76d6 100644
@@ -474,6 +474,9 @@ static u8 pcal6534_recalc_addr(struct pca953x_chip *chip, int reg, int off)
 	case PCAL6524_DEBOUNCE:
 		pinctrl = ((reg & PCAL_PINCTRL_MASK) >> 1) + 0x1c;
 		break;
+	default:
+		pinctrl = 0;
+		break;
 	}
 
 	return pinctrl + addr + (off / BANK_SZ);
Signature · 1 line
2.35.1
#1 lore raw

PATCH v2 2/4 gpio: pca953x: avoid logically dead code

From: Haibo Chen <[email protected]>

The current code logic make the condition "else if (reg >= 0x54)" can't be true, cause the dead code. So fix it to match the coder expectation. This is reported by Coverity.

Fixes 13c5d4ce8060 ("gpio: pca953x: Add support for PCAL6534")

---
v2: shuffled code to check bigger value first (Andy)

 drivers/gpio/gpio-pca953x.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/gpio/gpio-pca953x.c +10 -10
index 5299e5bb76d6..2c8586b3191f 100644
@@ -309,26 +309,26 @@ static bool pcal6534_check_register(struct pca953x_chip *chip, unsigned int reg,
 	int bank;
 	int offset;
 
-	if (reg >= 0x30) {
+	if (reg >= 0x54) {
 		/*
-		 * Reserved block between 14h and 2Fh does not align on
-		 * expected bank boundaries like other devices.
+		 * Handle lack of reserved registers after output port
+		 * configuration register to form a bank.
 		 */
-		int temp = reg - 0x30;
+		int temp = reg - 0x54;
 
 		bank = temp / NBANK(chip);
 		offset = temp - (bank * NBANK(chip));
-		bank += 8;
-	} else if (reg >= 0x54) {
+		bank += 16;
+	} else if (reg >= 0x30) {
 		/*
-		 * Handle lack of reserved registers after output port
-		 * configuration register to form a bank.
+		 * Reserved block between 14h and 2Fh does not align on
+		 * expected bank boundaries like other devices.
 		 */
-		int temp = reg - 0x54;
+		int temp = reg - 0x30;
 
 		bank = temp / NBANK(chip);
 		offset = temp - (bank * NBANK(chip));
-		bank += 16;
+		bank += 8;
 	} else {
 		bank = reg / NBANK(chip);
 		offset = reg - (bank * NBANK(chip));
Signature · 1 line
2.35.1
#2 lore raw

PATCH v2 3/4 gpio: pca953x: Clean up pcal6534_check_register()

In reply to Andy Shevchenko

The pcal6534_check_register() is a bit too verbose. Clean up it, by deduplicating some operations and switching to the modulo operation as on some architectures / and % can become a single assembly instruction.

---
v2: new patch

 drivers/gpio/gpio-pca953x.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)
drivers/gpio/gpio-pca953x.c +9 -12
index 2c8586b3191f..8aba8df393bd 100644
@@ -306,6 +306,7 @@ static bool pca953x_check_register(struct pca953x_chip *chip, unsigned int reg,
 static bool pcal6534_check_register(struct pca953x_chip *chip, unsigned int reg,
 				    u32 checkbank)
 {
+	int bank_shift;
 	int bank;
 	int offset;
 
@@ -314,26 +315,22 @@ static bool pcal6534_check_register(struct pca953x_chip *chip, unsigned int reg,
 		 * Handle lack of reserved registers after output port
 		 * configuration register to form a bank.
 		 */
-		int temp = reg - 0x54;
-
-		bank = temp / NBANK(chip);
-		offset = temp - (bank * NBANK(chip));
-		bank += 16;
+		reg -= 0x54;
+		bank_shift = 16;
 	} else if (reg >= 0x30) {
 		/*
 		 * Reserved block between 14h and 2Fh does not align on
 		 * expected bank boundaries like other devices.
 		 */
-		int temp = reg - 0x30;
-
-		bank = temp / NBANK(chip);
-		offset = temp - (bank * NBANK(chip));
-		bank += 8;
+		reg -= 0x30;
+		bank_shift = 8;
 	} else {
-		bank = reg / NBANK(chip);
-		offset = reg - (bank * NBANK(chip));
+		bank_shift = 0;
 	}
 
+	bank = bank_shift + reg / NBANK(chip);
+	offset = reg % NBANK(chip);
+
 	/* Register is not in the matching bank. */
 	if (!(BIT(bank) & checkbank))
 		return false;
Signature · 1 line
2.35.1
#3 lore raw

PATCH v2 4/4 gpio: pca953x: Remove unused PCAL953X_OUT_CONF from pcal6534_recalc_addr()

In reply to Andy Shevchenko

First of all, PCAL953X_OUT_CONF is not used in the driver. Second, it's not a per-bank register, it's a single for the chip and should be handled differently anyway.

To avoid confusion, drop PCAL953X_OUT_CONF from pcal6534_recalc_addr().

---
v2: new patch

 drivers/gpio/gpio-pca953x.c | 1 -
 1 file changed, 1 deletion(-)
drivers/gpio/gpio-pca953x.c +0 -1
index 8aba8df393bd..1286b22ef23a 100644
@@ -461,7 +461,6 @@ static u8 pcal6534_recalc_addr(struct pca953x_chip *chip, int reg, int off)
 	case PCAL953X_PULL_SEL:
 	case PCAL953X_INT_MASK:
 	case PCAL953X_INT_STAT:
-	case PCAL953X_OUT_CONF:
 		pinctrl = ((reg & PCAL_PINCTRL_MASK) >> 1) + 0x20;
 		break;
 	case PCAL6524_INT_EDGE:
Signature · 1 line
2.35.1
#4 lore raw
In reply to Andy Shevchenko
Quoted 7 lines · Andy Shevchenko

From: Haibo Chen <[email protected]>

There is a variable pinctrl declared without initializer. And then has the case (switch operation chose the default case) to directly use this uninitialized value, this is not a safe behavior. So here initialize the pinctrl as 0 to avoid this issue.
This is reported by Coverity.

Bart, any comments on the series?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Andy Shevchenko

From: Haibo Chen <[email protected]>

Quoted 8 lines
Quoted 5 lines

There is a variable pinctrl declared without initializer. And then has the case (switch operation chose the default case) to directly use this uninitialized value, this is not a safe behavior. So here initialize the pinctrl as 0 to avoid this issue.
This is reported by Coverity.

Bart, any comments on the series?

Now applied. I just got back from Christmas break, give me a moment. :)

Bart

#6 lore raw
Quoted 11 lines · Bartosz Golaszewski
Quoted 9 lines · Andy Shevchenko
Quoted 7 lines · Andy Shevchenko

From: Haibo Chen <[email protected]>

There is a variable pinctrl declared without initializer. And then has the case (switch operation chose the default case) to directly use this uninitialized value, this is not a safe behavior. So here initialize the pinctrl as 0 to avoid this issue.
This is reported by Coverity.

Quoted 3 lines
Quoted 1 line

Bart, any comments on the series?

Now applied. I just got back from Christmas break, give me a moment. :)

Sure and thank you! Merry xmas!

Signature · 2 lines
With Best Regards,
Andy Shevchenko