Skip to contentSkip to conversation

iio: imu: bmi323: check i2c_transfer() return value

PATCH Superseded Siqi Chen 2 participants 4 messages Nov 19 – Nov 20

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Siqi Chen

author
#0 lore raw

PATCH 1/1 iio: imu: bmi323: check i2c_transfer() return value

i2c_transfer() returns the number of messages transferred or a negative error code. The BMI323 I2C read path only checks for negative values and maps all errors to -EIO.

If only the register-address message is transferred, the function copies data from the receive buffer and reports success even though the read message was not completed.

Require both messages to be transferred, preserve negative error codes, and return -EIO for a short transfer.

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

Compile-tested with CONFIG_BMI323_I2C=m using:

  make O=/tmp/opencode/linux-build-bmi323 W=1 \
       drivers/iio/imu/bmi323/bmi323_i2c.o

No hardware or I2C fault-injection testing was performed.

Fixes 8a636db3aa57 ("iio: imu: Add driver for BMI323 IMU")
---
 drivers/iio/imu/bmi323/bmi323_i2c.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/imu/bmi323/bmi323_i2c.c +2 -2
index e025c88c30b4..18f05f119961 100644
@@ -40,8 +40,8 @@ static int bmi323_regmap_i2c_read(void *context, const void *reg_buf,
 	msgs[1].flags = priv->i2c->flags | I2C_M_RD;
 
 	ret = i2c_transfer(priv->i2c->adapter, msgs, ARRAY_SIZE(msgs));
-	if (ret < 0)
-		return -EIO;
+	if (ret != ARRAY_SIZE(msgs))
+		return ret < 0 ? ret : -EIO;
 
 	memcpy(val_buf, priv->i2c_rx_buffer + BMI323_I2C_DUMMY, val_size);
 

base-commit: c1b99c6982dccade0199879966a2d97f27a0a110

Signature · 1 line
2.20.1
#1 lore raw
Quoted 8 lines · Siqi Chen
 drivers/iio/imu/bmi323/bmi323_i2c.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/imu/bmi323/bmi323_i2c.c +0 -0
index e025c88c30b4..18f05f119961 100644

drivers/iio/imu/bmi323/bmi323_i2c.c static int bmi323_regmap_i2c_read(void *context, const void *reg_buf, · line 44

Quoted 7 lines
drivers/iio/imu/bmi323/bmi323_i2c.c +2 -2
@@ -40,8 +40,8 @@ static int bmi323_regmap_i2c_read(void *context, const void *reg_buf,
 	msgs[1].flags = priv->i2c->flags | I2C_M_RD;
 
 	ret = i2c_transfer(priv->i2c->adapter, msgs, ARRAY_SIZE(msgs));
-	if (ret < 0)
-		return -EIO;
+	if (ret != ARRAY_SIZE(msgs))
+		return ret < 0 ? ret : -EIO;

Better to do

	if (ret < 0)
		return ret;
	if (ret != ARRAY_SIZE(msgs))
		return -EIO;

It improves readability.

Signature · 2 lines
Kind regards,
Joshua Crofts
#2 lore raw
In reply to Siqi Chen
Quoted 11 lines · Siqi Chen

i2c_transfer() returns the number of messages transferred or a negative error code. The BMI323 I2C read path only checks for negative values and maps all errors to -EIO.

If only the register-address message is transferred, the function copies data from the receive buffer and reports success even though the read message was not completed.

Require both messages to be transferred, preserve negative error codes, and return -EIO for a short transfer.

Quoted 8 lines

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

Compile-tested with CONFIG_BMI323_I2C=m using:

  make O=/tmp/opencode/linux-build-bmi323 W=1 \
       drivers/iio/imu/bmi323/bmi323_i2c.o

No hardware or I2C fault-injection testing was performed.

Also the testing info should go under the ---, as it's compile tested only.

Signature · 2 lines
Kind regards,
Joshua Crofts
author
#3 lore raw
Quoted 1 line · Joshua Crofts

Also the testing info should go under the ---, as it's compile tested only.

Thanks for the review. I agree that separate checks are clearer. I will also move the testing information below the separator and send a v2.

Best regards,
Siqi