Skip to contentSkip to conversation

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

PATCH In review Siqi Chen 3 participants 3 messages Nov 20 – Nov 21

Subsystems (2)
lore
Share & tools
lore

Siqi Chen

author
#0 lore raw

PATCH v2 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.

Fixes 8a636db3aa57 ("iio: imu: Add driver for BMI323 IMU")

---
Changes in v2:
- Split the transfer error and short-transfer checks for readability.
- Move testing information below the separator.

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.

Link to v1:
https://lore.kernel.org/r/[email protected]

 drivers/iio/imu/bmi323/bmi323_i2c.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/imu/bmi323/bmi323_i2c.c +2 -0
index e025c88c30b4..27eddd1346d0 100644
@@ -41,6 +41,8 @@ static int bmi323_regmap_i2c_read(void *context, const void *reg_buf,
 
 	ret = i2c_transfer(priv->i2c->adapter, msgs, ARRAY_SIZE(msgs));
 	if (ret < 0)
+		return ret;
+	if (ret != ARRAY_SIZE(msgs))
 		return -EIO;
 
 	memcpy(val_buf, priv->i2c_rx_buffer + BMI323_I2C_DUMMY, val_size);

base-commit: c1b99c6982dccade0199879966a2d97f27a0a110

Signature · 1 line
2.20.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 4 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.

Quoted 8 lines

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.

This line should be in the comment block, behind the '---' line. No need to resend (just remember for the future submissions).

Quoted 7 lines
Fixes 8a636db3aa57 ("iio: imu: Add driver for BMI323 IMU")

---
Changes in v2:
- Split the transfer error and short-transfer checks for readability.
- Move testing information below the separator.

Quoted 6 lines

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.

Code wise LGTM.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#2 lore raw
Quoted 9 lines · On Fri, 20 Nov 2026 11:38:21 +0200 Andy Shevchenko…
Quoted 8 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.

Quoted 7 lines
Quoted 4 lines

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.

This line should be in the comment block, behind the '---' line. No need to resend (just remember for the future submissions).

This time I took this and dropped that bit whilst doing so.

Applied to the testing branch of iio.git as not high urgency. In practice short reads only occur in very very rare circumstances.

Thanks

Jonathan

Quoted 18 lines
Quoted 8 lines
Fixes 8a636db3aa57 ("iio: imu: Add driver for BMI323 IMU")

---
Changes in v2:
- Split the transfer error and short-transfer checks for readability.
- Move testing information below the separator.

Quoted 6 lines

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.

Code wise LGTM.