Skip to contentSkip to conversation

iio: gyro: adxrs290: propagate chip ID read errors

PATCH Superseded Siqi Chen 3 participants 3 messages Nov 19

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Siqi Chen

author
#0 lore raw

PATCH 1/1 iio: gyro: adxrs290: propagate chip ID read errors

spi_w8r8() returns either an unsigned 8-bit value or a negative error. The probe function stores its return value in a u8, truncating errors and treating them as unexpected chip IDs. Consequently, an SPI transfer failure is reported as -ENODEV instead of the actual error.

Store each result in an int and return negative errors before validating the chip IDs.

OpenCode was used to identify the issue and draft the code and changelog.

Compile-tested with CONFIG_ADXRS290=m using:

  make O=/tmp/opencode/linux-build-adxrs290 W=1 \
       drivers/iio/gyro/adxrs290.o

No hardware or SPI fault-injection testing was performed.

Fixes 2c8920fff145 ("iio: gyro: Add driver support for ADXRS290")
---
 drivers/iio/gyro/adxrs290.c | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)
drivers/iio/gyro/adxrs290.c +16 -10
index b61cc81122a6..eb3306215217 100644
@@ -604,7 +604,7 @@ static int adxrs290_probe(struct spi_device *spi)
 	struct iio_dev *indio_dev;
 	struct adxrs290_state *st;
 	u8 val, val2;
-	int ret;
+	int id, ret;
 
 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
 	if (!indio_dev)
@@ -622,21 +622,27 @@ static int adxrs290_probe(struct spi_device *spi)
 
 	mutex_init(&st->lock);
 
-	val = spi_w8r8(spi, ADXRS290_READ_REG(ADXRS290_REG_ADI_ID));
-	if (val != ADXRS290_ADI_ID) {
-		dev_err(&spi->dev, "Wrong ADI ID 0x%02x\n", val);
+	id = spi_w8r8(spi, ADXRS290_READ_REG(ADXRS290_REG_ADI_ID));
+	if (id < 0)
+		return dev_err_probe(&spi->dev, id, "failed to read ADI ID\n");
+	if (id != ADXRS290_ADI_ID) {
+		dev_err(&spi->dev, "Wrong ADI ID 0x%02x\n", id);
 		return -ENODEV;
 	}
 
-	val = spi_w8r8(spi, ADXRS290_READ_REG(ADXRS290_REG_MEMS_ID));
-	if (val != ADXRS290_MEMS_ID) {
-		dev_err(&spi->dev, "Wrong MEMS ID 0x%02x\n", val);
+	id = spi_w8r8(spi, ADXRS290_READ_REG(ADXRS290_REG_MEMS_ID));
+	if (id < 0)
+		return dev_err_probe(&spi->dev, id, "failed to read MEMS ID\n");
+	if (id != ADXRS290_MEMS_ID) {
+		dev_err(&spi->dev, "Wrong MEMS ID 0x%02x\n", id);
 		return -ENODEV;
 	}
 
-	val = spi_w8r8(spi, ADXRS290_READ_REG(ADXRS290_REG_DEV_ID));
-	if (val != ADXRS290_DEV_ID) {
-		dev_err(&spi->dev, "Wrong DEV ID 0x%02x\n", val);
+	id = spi_w8r8(spi, ADXRS290_READ_REG(ADXRS290_REG_DEV_ID));
+	if (id < 0)
+		return dev_err_probe(&spi->dev, id, "failed to read DEV ID\n");
+	if (id != ADXRS290_DEV_ID) {
+		dev_err(&spi->dev, "Wrong DEV ID 0x%02x\n", id);
 		return -ENODEV;
 	}
 

base-commit: 903941a9b06ae445c70646ab061b604c2dea17d3

Signature · 1 line
2.20.1
#1 lore raw
Quoted 7 lines · Siqi Chen

spi_w8r8() returns either an unsigned 8-bit value or a negative error. The probe function stores its return value in a u8, truncating errors and treating them as unexpected chip IDs. Consequently, an SPI transfer failure is reported as -ENODEV instead of the actual error.

Store each result in an int and return negative errors before validating the chip IDs.

Starting from here the rest of the commit message should be moved to the comment block (after '---' line).

Quoted 8 lines

OpenCode was used to identify the issue and draft the code and changelog.

Compile-tested with CONFIG_ADXRS290=m using:

  make O=/tmp/opencode/linux-build-adxrs290 W=1 \
       drivers/iio/gyro/adxrs290.o

No hardware or SPI fault-injection testing was performed.

...

Code wise seems good to me.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
In reply to Siqi Chen
Quoted 8 lines · Siqi Chen

spi_w8r8() returns either an unsigned 8-bit value or a negative error. The probe function stores its return value in a u8, truncating errors and treating them as unexpected chip IDs. Consequently, an SPI transfer failure is reported as -ENODEV instead of the actual error.

Store each result in an int and return negative errors before validating the chip IDs.

Quoted 8 lines

OpenCode was used to identify the issue and draft the code and changelog.

Compile-tested with CONFIG_ADXRS290=m using:

  make O=/tmp/opencode/linux-build-adxrs290 W=1 \
       drivers/iio/gyro/adxrs290.o

No hardware or SPI fault-injection testing was performed.

Remove the testing info from the commit message and put it under the ---. Unless you tested the patch on specific hardware then no need to immortalize it in the git log.

Side note: one would expect that patches sent to the mailing list are at least compile tested :)

Quoted 12 lines
Fixes 2c8920fff145 ("iio: gyro: Add driver support for ADXRS290")
---
 drivers/iio/gyro/adxrs290.c | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)
drivers/iio/gyro/adxrs290.c +0 -0
index b61cc81122a6..eb3306215217 100644

drivers/iio/gyro/adxrs290.c static int adxrs290_probe(struct spi_device *spi) · line 607

Quoted 5 lines
drivers/iio/gyro/adxrs290.c +1 -1
@@ -604,7 +604,7 @@ static int adxrs290_probe(struct spi_device *spi)
 	struct iio_dev *indio_dev;
 	struct adxrs290_state *st;
 	u8 val, val2;
-	int ret;
+	int id, ret;

Why a new variable? ret should be sufficient in this case.

Quoted 6 lines
drivers/iio/gyro/adxrs290.c +0 -0
 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
 	if (!indio_dev)
@@ -622,21 +622,27 @@ static int adxrs290_probe(struct spi_device *spi)
 
 	mutex_init(&st->lock);

drivers/iio/gyro/adxrs290.c static int adxrs290_probe(struct spi_device *spi) · line 630

Quoted 10 lines
drivers/iio/gyro/adxrs290.c +5 -3
 
-	val = spi_w8r8(spi, ADXRS290_READ_REG(ADXRS290_REG_ADI_ID));
-	if (val != ADXRS290_ADI_ID) {
-		dev_err(&spi->dev, "Wrong ADI ID 0x%02x\n", val);
+	id = spi_w8r8(spi, ADXRS290_READ_REG(ADXRS290_REG_ADI_ID));
+	if (id < 0)
+		return dev_err_probe(&spi->dev, id, "failed to read ADI ID\n");
+	if (id != ADXRS290_ADI_ID) {
+		dev_err(&spi->dev, "Wrong ADI ID 0x%02x\n", id);
 		return -ENODEV;

Not sure how this device works, but better to just call dev_info() to ensure that any potential compatible fallback can be matched.

Also, why not just use dev_err_probe() if the previous if check uses it? Style unification is a good idea.

Signature · 2 lines
Kind regards,
Joshua Crofts