Skip to contentSkip to conversation

hwmon: (ads7871) Propagate SPI errors in voltage_show

PATCH Applied Tabrez Ahmed 2 participants 2 messages Mar 8 Hardware monitoring

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hwmon: (ads7871) Propagate SPI errors in voltage_show

The voltage_show() function previously ignored negative error codes returned by the underlying SPI read/write functions. Because negative numbers have their most significant bits set in two's complement, a failed SPI read returning -EIO (-5) would incorrectly evaluate to true when masked with MUX_CNV_BM (0x80).

This would cause the driver to enter the polling loop even when the SPI bus failed, eventually returning a misleading -ETIMEDOUT error to userspace instead of the actual hardware error. Furthermore, the return values of the initial SPI write and the final 16-bit SPI read were completely ignored.

Add proper error checking after every SPI operation to ensure hardware failures are immediately propagated back to userspace.

---
 drivers/hwmon/ads7871.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/hwmon/ads7871.c +10 -2
index 753bf77ce19b4..9bfdf9e6bcd77 100644
@@ -104,10 +104,14 @@ static ssize_t voltage_show(struct device *dev, struct device_attribute *da,
 	 */
 	/*MUX_M3_BM forces single ended*/
 	/*This is also where the gain of the PGA would be set*/
-	ads7871_write_reg8(spi, REG_GAIN_MUX,
-		(MUX_CNV_BM | MUX_M3_BM | channel));
+	ret = ads7871_write_reg8(spi, REG_GAIN_MUX,
+				 (MUX_CNV_BM | MUX_M3_BM | channel));
+	if (ret < 0)
+		return ret;
 
 	ret = ads7871_read_reg8(spi, REG_GAIN_MUX);
+	if (ret < 0)
+		return ret;
 	mux_cnv = ((ret & MUX_CNV_BM) >> MUX_CNV_BV);
 	/*
 	 * on 400MHz arm9 platform the conversion
@@ -116,12 +120,16 @@ static ssize_t voltage_show(struct device *dev, struct device_attribute *da,
 	while ((i < 2) && mux_cnv) {
 		i++;
 		ret = ads7871_read_reg8(spi, REG_GAIN_MUX);
+		if (ret < 0)
+			return ret;
 		mux_cnv = ((ret & MUX_CNV_BM) >> MUX_CNV_BV);
 		msleep_interruptible(1);
 	}
 
 	if (mux_cnv == 0) {
 		val = ads7871_read_reg16(spi, REG_LS_BYTE);
+		if (val < 0)
+			return val;
 		/*result in volts*10000 = (val/8192)*2.5*10000*/
 		val = ((val >> 2) * 25000) / 8192;
 		return sysfs_emit(buf, "%d\n", val);
Signature · 1 line
2.43.0
maintainer Applied
#1 lore raw
Quoted 12 lines · Tabrez Ahmed

The voltage_show() function previously ignored negative error codes returned by the underlying SPI read/write functions. Because negative numbers have their most significant bits set in two's complement, a failed SPI read returning -EIO (-5) would incorrectly evaluate to true when masked with MUX_CNV_BM (0x80).

This would cause the driver to enter the polling loop even when the SPI bus failed, eventually returning a misleading -ETIMEDOUT error to userspace instead of the actual hardware error. Furthermore, the return values of the initial SPI write and the final 16-bit SPI read were completely ignored.

Quoted 5 lines

Add proper error checking after every SPI operation to ensure hardware failures are immediately propagated back to userspace.

Applied.

Thanks,
Guenter