Skip to contentSkip to conversation

IIO: Clean out superfluous I2C checks for single transfers

PATCH RESEND In review Jonathan Cameron 6 participants 32 messages Jul 16 – Jul 17

Subsystems (14)
lore
Share & tools
lore
#0 lore raw

cover IIO: Clean out superfluous I2C checks for single transfers

Resend to include linux-iio. Sorry to all who get it twice.

This came up in a recent review and I realised I have been giving incorrect review feedback for years. Both i2_master_send() and i2c_master_recv() plus the dma safe variants only ever return either an error or the full buffer size. Short accesses do not occur.

This series (also my first upstream series prepared by poking claude) removes the redundant checks from all IIO drivers.

Thanks,

Jonathan

Jonathan Cameron (22):
iio: adc: ltc2471: Remove redundant i2c_master_recv() length check
iio: adc: ti-ads7138: Remove redundant i2c_master_send() length checks
iio: adc: versal-sysmon: Remove redundant i2c_master_{send,recv}()
length checks
iio: chemical: scd30: Remove redundant i2c_master_{send,recv}() length
checks
iio: chemical: scd4x: Remove redundant i2c_master_{send,recv}() length
checks
iio: chemical: sgp30: Remove redundant i2c_master_recv() length check
iio: chemical: sgp40: Remove redundant i2c_master_recv() length check
iio: chemical: sps30: Remove redundant i2c_master_{send,recv}() length
checks
iio: dac: ad5446: Remove redundant i2c_master_send() length check
iio: dac: ad5696: Remove redundant i2c_master_send() length check
iio: dac: m62332: Remove redundant i2c_master_send() length check
iio: dac: max517: Remove redundant i2c_master_send() length check
iio: dac: max5821: Remove redundant i2c_master_{send,recv}() length
checks
iio: dac: mcp4725: Remove redundant i2c_master_{send,recv}() length
checks
iio: dac: mcp4728: Remove redundant i2c_master_{send,recv}() length
checks
iio: light: lv0104cs: Remove redundant i2c_master_{send,recv}() length
checks
iio: potentiometer: ad5110: Remove redundant i2c_master_{send,recv}()
length checks
iio: pressure: abp2030pa: Remove redundant i2c_master_{send,recv}()
length checks
iio: pressure: adp810: Remove redundant i2c_master_{send,recv}()
length checks
iio: pressure: icp10100: Remove redundant i2c_master_send() length
check
iio: pressure: mprls0025pa: Remove redundant i2c_master_{send,recv}()
length checks
iio: pressure: sdp500: Remove redundant i2c_master_recv() length check

 drivers/iio/adc/ltc2471.c              |  2 --
 drivers/iio/adc/ti-ads7138.c           |  4 ----
 drivers/iio/adc/versal-sysmon-i2c.c    |  6 ------
 drivers/iio/chemical/scd30_i2c.c       |  4 ----
 drivers/iio/chemical/scd4x.c           |  8 --------
 drivers/iio/chemical/sgp30.c           |  2 --
 drivers/iio/chemical/sgp40.c           |  4 ----
 drivers/iio/chemical/sps30_i2c.c       |  4 ----
 drivers/iio/dac/ad5446-i2c.c           |  2 --
 drivers/iio/dac/ad5696-i2c.c           |  2 +-
 drivers/iio/dac/m62332.c               |  2 --
 drivers/iio/dac/max517.c               |  6 ++----
 drivers/iio/dac/max5821.c              | 14 ++------------
 drivers/iio/dac/mcp4725.c              | 22 ++++++----------------
 drivers/iio/dac/mcp4728.c              | 13 +------------
 drivers/iio/light/lv0104cs.c           |  4 ----
 drivers/iio/potentiometer/ad5110.c     | 18 +++---------------
 drivers/iio/pressure/abp2030pa_i2c.c   |  4 ----
 drivers/iio/pressure/adp810.c          |  4 ----
 drivers/iio/pressure/icp10100.c        |  2 --
 drivers/iio/pressure/mprls0025pa_i2c.c |  4 ----
 drivers/iio/pressure/sdp500.c          |  4 ----
 22 files changed, 15 insertions(+), 120 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/22 iio: adc: ltc2471: Remove redundant i2c_master_recv() length check

i2c_master_recv() returns either a negative errno or exactly the count passed in, so the length check after the 'ret < 0' test can never be true. Drop the dead branch.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/adc/ltc2471.c | 2 --
 1 file changed, 2 deletions(-)
drivers/iio/adc/ltc2471.c +0 -2
index 369af700969f..0223824782de 100644
@@ -35,8 +35,6 @@ static int ltc2471_get_value(struct i2c_client *client)
 	ret = i2c_master_recv(client, (char *)&buf, sizeof(buf));
 	if (ret < 0)
 		return ret;
-	if (ret != sizeof(buf))
-		return -EIO;
 
 	/* MSB first */
 	return be16_to_cpu(buf);
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/22 iio: adc: ti-ads7138: Remove redundant i2c_master_send() length checks

In reply to Jonathan Cameron

i2c_master_send() returns either a negative errno or exactly the count passed in, so a length check after the 'ret < 0' test can never be true. Drop the dead branches.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/adc/ti-ads7138.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/iio/adc/ti-ads7138.c +0 -4
index af87f5f19a0f..afb20accc170 100644
@@ -116,8 +116,6 @@ static int ads7138_i2c_write_block(const struct i2c_client *client, u8 reg,
 	ret = i2c_master_send(client, buf, len);
 	if (ret < 0)
 		return ret;
-	if (ret != len)
-		return -EIO;
 
 	return 0;
 }
@@ -131,8 +129,6 @@ static int ads7138_i2c_write_with_opcode(const struct i2c_client *client,
 	ret = i2c_master_send(client, buf, ARRAY_SIZE(buf));
 	if (ret < 0)
 		return ret;
-	if (ret != ARRAY_SIZE(buf))
-		return -EIO;
 
 	return 0;
 }
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/22 iio: adc: versal-sysmon: Remove redundant i2c_master_{send,recv}() length checks

In reply to Jonathan Cameron

i2c_master_send() and i2c_master_recv() return either a negative errno or exactly the count passed in, so a length check after the 'ret < 0' test can never be true. Drop the dead branches.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/adc/versal-sysmon-i2c.c | 6 ------
 1 file changed, 6 deletions(-)
drivers/iio/adc/versal-sysmon-i2c.c +0 -6
index e9a7629159ab..afdcc4e93771 100644
@@ -44,14 +44,10 @@ static int sysmon_i2c_reg_read(void *context, unsigned int reg,
 	ret = i2c_master_send(client, write_buf, sizeof(write_buf));
 	if (ret < 0)
 		return ret;
-	if (ret != sizeof(write_buf))
-		return -EIO;
 
 	ret = i2c_master_recv(client, read_buf, sizeof(read_buf));
 	if (ret < 0)
 		return ret;
-	if (ret != sizeof(read_buf))
-		return -EIO;
 
 	*val = get_unaligned_le32(read_buf);
 
@@ -72,8 +68,6 @@ static int sysmon_i2c_reg_write(void *context, unsigned int reg,
 	ret = i2c_master_send(client, write_buf, sizeof(write_buf));
 	if (ret < 0)
 		return ret;
-	if (ret != sizeof(write_buf))
-		return -EIO;
 
 	return 0;
 }
Signature · 1 line
2.43.0
#4 lore raw

PATCH 4/22 iio: chemical: scd30: Remove redundant i2c_master_{send,recv}() length checks

In reply to Jonathan Cameron

i2c_master_send() and i2c_master_recv() return either a negative errno or exactly the count passed in, so a length check after the 'ret < 0' test can never be true. Drop the dead branches.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/chemical/scd30_i2c.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/iio/chemical/scd30_i2c.c +0 -4
index bcef1e473e34..78acc965b0f7 100644
@@ -47,8 +47,6 @@ static int scd30_i2c_xfer(struct scd30_state *state, char *txbuf, int txsize,
 	ret = i2c_master_send(client, txbuf, txsize);
 	if (ret < 0)
 		return ret;
-	if (ret != txsize)
-		return -EIO;
 
 	if (!rxbuf)
 		return 0;
@@ -56,8 +54,6 @@ static int scd30_i2c_xfer(struct scd30_state *state, char *txbuf, int txsize,
 	ret = i2c_master_recv(client, rxbuf, rxsize);
 	if (ret < 0)
 		return ret;
-	if (ret != rxsize)
-		return -EIO;
 
 	return 0;
 }
Signature · 1 line
2.43.0
maintainer
#5 lore raw

Re: [PATCH 04/22] iio: chemical: scd30: Remove redundant i2c_master_{send,recv}() length checks

Quoted 10 lines · CDT Jonathan Cameron

i2c_master_send() and i2c_master_recv() return either a negative errno or exactly the count passed in, so a length check after the 'ret < 0' test can never be true. Drop the dead branches.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/chemical/scd30_i2c.c | 4 ----
 1 file changed, 4 deletions(-)

I'm not 100% sure that's the case but since the i2c docs aren't very helpful on this I will probably pester the i2c maintainers about this.

Signature · 2 lines
best regards,
max
#6 lore raw

PATCH 5/22 iio: chemical: scd4x: Remove redundant i2c_master_{send,recv}() length checks

In reply to Jonathan Cameron

i2c_master_send() and i2c_master_recv() return either a negative errno or exactly the count passed in, so a length check after the 'ret < 0' test can never be true. Drop the dead branches.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/chemical/scd4x.c | 8 --------
 1 file changed, 8 deletions(-)
drivers/iio/chemical/scd4x.c +0 -8
index 23a326fb62a7..b52072a4eb65 100644
@@ -82,8 +82,6 @@ static int scd4x_i2c_xfer(struct scd4x_state *state, char *txbuf, int txsize,
 
 	if (ret < 0)
 		return ret;
-	if (ret != txsize)
-		return -EIO;
 
 	if (rxsize == 0)
 		return 0;
@@ -91,8 +89,6 @@ static int scd4x_i2c_xfer(struct scd4x_state *state, char *txbuf, int txsize,
 	ret = i2c_master_recv(client, rxbuf, rxsize);
 	if (ret < 0)
 		return ret;
-	if (ret != rxsize)
-		return -EIO;
 
 	return 0;
 }
@@ -241,10 +237,6 @@ static int scd4x_write_and_fetch(struct scd4x_state *state, enum scd4x_cmd cmd,
 	ret = i2c_master_recv(client, buf, response_sz);
 	if (ret < 0)
 		goto err;
-	if (ret != response_sz) {
-		ret = -EIO;
-		goto err;
-	}
 
 	for (i = 0; i < response_sz; i += 3) {
 		crc = crc8(scd4x_crc8_table, buf + i, 2, CRC8_INIT_VALUE);
Signature · 1 line
2.43.0
#7 lore raw

PATCH 6/22 iio: chemical: sgp30: Remove redundant i2c_master_recv() length check

In reply to Jonathan Cameron

i2c_master_recv() returns either a negative errno or exactly the count passed in, so the length check after the 'ret < 0' test can never be true. Drop the dead branch.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/chemical/sgp30.c | 2 --
 1 file changed, 2 deletions(-)
drivers/iio/chemical/sgp30.c +0 -2
index f10bbebc29e4..8152606c6495 100644
@@ -254,8 +254,6 @@ static int sgp_read_cmd(struct sgp_data *data, enum sgp_cmd cmd,
 	ret = i2c_master_recv(client, data_buf, size);
 	if (ret < 0)
 		return ret;
-	if (ret != size)
-		return -EIO;
 
 	return sgp_verify_buffer(data, buf, word_count);
 }
Signature · 1 line
2.43.0
#8 lore raw

PATCH 7/22 iio: chemical: sgp40: Remove redundant i2c_master_recv() length check

In reply to Jonathan Cameron

i2c_master_recv() returns either a negative errno or exactly the count passed in, so the length check after the 'ret < 0' test can never be true. Drop the dead branch.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/chemical/sgp40.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/iio/chemical/sgp40.c +0 -4
index b2b5e32a9eb2..090469e620fd 100644
@@ -196,10 +196,6 @@ static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance
 	ret = i2c_master_recv(client, (u8 *)&tgres, sizeof(tgres));
 	if (ret < 0)
 		return ret;
-	if (ret != sizeof(tgres)) {
-		dev_warn(data->dev, "i2c_master_recv ret: %d sizeof: %zu\n", ret, sizeof(tgres));
-		return -EIO;
-	}
 
 	crc = crc8(sgp40_crc8_table, (u8 *)&tgres.res_ticks, 2, SGP40_CRC8_INIT);
 	if (crc != tgres.res_crc) {
Signature · 1 line
2.43.0
#9 lore raw

PATCH 8/22 iio: chemical: sps30: Remove redundant i2c_master_{send,recv}() length checks

In reply to Jonathan Cameron

i2c_master_send() and i2c_master_recv() return either a negative errno or exactly the count passed in, so a length check after the 'ret < 0' test can never be true. Drop the dead branches.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/chemical/sps30_i2c.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/iio/chemical/sps30_i2c.c +0 -4
index 2d3a876cc06e..284b10ef63e9 100644
@@ -46,8 +46,6 @@ static int sps30_i2c_xfer(struct sps30_state *state, unsigned char *txbuf, size_
 	ret = i2c_master_send(client, txbuf, txsize);
 	if (ret < 0)
 		return ret;
-	if (ret != txsize)
-		return -EIO;
 
 	if (!rxsize)
 		return 0;
@@ -55,8 +53,6 @@ static int sps30_i2c_xfer(struct sps30_state *state, unsigned char *txbuf, size_
 	ret = i2c_master_recv(client, rxbuf, rxsize);
 	if (ret < 0)
 		return ret;
-	if (ret != rxsize)
-		return -EIO;
 
 	return 0;
 }
Signature · 1 line
2.43.0
#10 lore raw

PATCH 9/22 iio: dac: ad5446: Remove redundant i2c_master_send() length check

In reply to Jonathan Cameron

i2c_master_send_dmasafe() returns either a negative errno or exactly the count passed in, so the length check after the 'ret < 0' test can never be true. Drop the dead branch.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/dac/ad5446-i2c.c | 2 --
 1 file changed, 2 deletions(-)
drivers/iio/dac/ad5446-i2c.c +0 -2
index 9797fc3e57a9..0a01445f7a82 100644
@@ -22,8 +22,6 @@ static int ad5622_write(struct ad5446_state *st, unsigned int val)
 	ret = i2c_master_send_dmasafe(client, (char *)&st->d16, sizeof(st->d16));
 	if (ret < 0)
 		return ret;
-	if (ret != sizeof(st->d16))
-		return -EIO;
 
 	return 0;
 }
Signature · 1 line
2.43.0
#11 lore raw

PATCH 10/22 iio: dac: ad5696: Remove redundant i2c_master_send() length check

In reply to Jonathan Cameron

i2c_master_send() returns either a negative errno or exactly the count passed in, so the length check after the 'ret < 0' test can never be true. Drop the dead branch.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/dac/ad5696-i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/dac/ad5696-i2c.c +1 -1
index d49946adbde3..95b4956f9376 100644
@@ -58,7 +58,7 @@ static int ad5686_i2c_write(struct ad5686_state *st,
 	if (ret < 0)
 		return ret;
 
-	return (ret != 3) ? -EIO : 0;
+	return 0;
 }
 
 static const struct ad5686_bus_ops ad5686_i2c_ops = {
Signature · 1 line
2.43.0
#12 lore raw

PATCH 11/22 iio: dac: m62332: Remove redundant i2c_master_send() length check

In reply to Jonathan Cameron

i2c_master_send() returns either a negative errno or exactly the count passed in, so the 'res >= 0 && res != ARRAY_SIZE(outbuf)' condition can never be true. Drop the dead branch.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/dac/m62332.c | 2 --
 1 file changed, 2 deletions(-)
drivers/iio/dac/m62332.c +0 -2
index 7e80c0eb5cc1..a945533a596e 100644
@@ -50,8 +50,6 @@ static int m62332_set_value(struct iio_dev *indio_dev, u8 val, int channel)
 	}
 
 	res = i2c_master_send(client, outbuf, ARRAY_SIZE(outbuf));
-	if (res >= 0 && res != ARRAY_SIZE(outbuf))
-		res = -EIO;
 	if (res < 0)
 		goto out;
 
Signature · 1 line
2.43.0
#13 lore raw

PATCH 12/22 iio: dac: max517: Remove redundant i2c_master_send() length check

In reply to Jonathan Cameron

i2c_master_send() returns either a negative errno or exactly the count passed in, so the length check after the 'ret < 0' test can never be true. Drop the dead branch.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/dac/max517.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/iio/dac/max517.c +2 -4
index d2ddc72e0e50..e0d1a26c4f73 100644
@@ -55,10 +55,8 @@ static int max517_set_value(struct iio_dev *indio_dev,
 	res = i2c_master_send(client, outbuf, 2);
 	if (res < 0)
 		return res;
-	else if (res != 2)
-		return -EIO;
-	else
-		return 0;
+
+	return 0;
 }
 
 static int max517_read_raw(struct iio_dev *indio_dev,
Signature · 1 line
2.43.0
#14 lore raw

Re: [PATCH 12/22] iio: dac: max517: Remove redundant i2c_master_send() length check

Quoted 15 lines · On Wed, 15 Jul 2026 17:57:27 -0700 Jonathan Cameron…

i2c_master_send() returns either a negative errno or exactly the count passed in, so the length check after the 'ret < 0' test can never be true. Drop the dead branch.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/dac/max517.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/iio/dac/max517.c +0 -0
index d2ddc72e0e50..e0d1a26c4f73 100644

drivers/iio/dac/max517.c static int max517_set_value(struct iio_dev *indio_dev, · line 55

Quoted 1 line
drivers/iio/dac/max517.c +0 -0
@@ -55,10 +55,8 @@ static int max517_set_value(struct iio_dev *indio_dev,
 	res = i2c_master_send(client, outbuf, 2);

Someone should also remove all the hard-coded sizes, they appear in multiple drivers you're touching.

Signature · 3 lines
Kind regards

CJD
#15 lore raw

PATCH 13/22 iio: dac: max5821: Remove redundant i2c_master_{send,recv}() length checks

In reply to Jonathan Cameron

i2c_master_send() and i2c_master_recv() return either a negative errno or exactly the count passed in, so a length check after the 'ret < 0' test can never be true. Drop the dead branches.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/dac/max5821.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
drivers/iio/dac/max5821.c +2 -12
index 45def245626b..fadc37eec999 100644
@@ -103,8 +103,6 @@ static int max5821_sync_powerdown_mode(struct max5821_data *data,
 	ret = i2c_master_send(data->client, outbuf, sizeof(outbuf));
 	if (ret < 0)
 		return ret;
-	if (ret != sizeof(outbuf))
-		return -EIO;
 
 	return 0;
 }
@@ -188,18 +186,12 @@ static int max5821_get_value(struct iio_dev *indio_dev,
 	if (ret < 0) {
 		mutex_unlock(&data->lock);
 		return ret;
-	} else if (ret != 1) {
-		mutex_unlock(&data->lock);
-		return -EIO;
 	}
 
 	ret = i2c_master_recv(client, inbuf, 2);
 	if (ret < 0) {
 		mutex_unlock(&data->lock);
 		return ret;
-	} else if (ret != 2) {
-		mutex_unlock(&data->lock);
-		return -EIO;
 	}
 
 	mutex_unlock(&data->lock);
@@ -230,10 +222,8 @@ static int max5821_set_value(struct iio_dev *indio_dev,
 	ret = i2c_master_send(client, outbuf, 2);
 	if (ret < 0)
 		return ret;
-	else if (ret != 2)
-		return -EIO;
-	else
-		return 0;
+
+	return 0;
 }
 
 static int max5821_read_raw(struct iio_dev *indio_dev,
Signature · 1 line
2.43.0
#16 lore raw

PATCH 14/22 iio: dac: mcp4725: Remove redundant i2c_master_{send,recv}() length checks

In reply to Jonathan Cameron

i2c_master_send() and i2c_master_recv() return either a negative errno or exactly the count passed in, so a length check after the 'ret < 0' test can never be true. Drop the dead branches.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/dac/mcp4725.c | 22 ++++++----------------
 1 file changed, 6 insertions(+), 16 deletions(-)
drivers/iio/dac/mcp4725.c +6 -16
index 3fa7acc69aeb..92c1f0ca369c 100644
@@ -58,8 +58,7 @@ static int mcp4725_suspend(struct device *dev)
 	ret = i2c_master_send(data->client, outbuf, 2);
 	if (ret < 0)
 		return ret;
-	else if (ret != 2)
-		return -EIO;
+
 	return 0;
 }
 
@@ -78,8 +77,7 @@ static int mcp4725_resume(struct device *dev)
 	ret = i2c_master_send(data->client, outbuf, 2);
 	if (ret < 0)
 		return ret;
-	else if (ret != 2)
-		return -EIO;
+
 	return 0;
 }
 static DEFINE_SIMPLE_DEV_PM_OPS(mcp4725_pm_ops, mcp4725_suspend,
@@ -111,8 +109,6 @@ static ssize_t mcp4725_store_eeprom(struct device *dev,
 	ret = i2c_master_send(data->client, inoutbuf, 3);
 	if (ret < 0)
 		return ret;
-	else if (ret != 3)
-		return -EIO;
 
 	/* wait for write complete, takes up to 50ms */
 	while (tries--) {
@@ -120,8 +116,6 @@ static ssize_t mcp4725_store_eeprom(struct device *dev,
 		ret = i2c_master_recv(data->client, inoutbuf, 3);
 		if (ret < 0)
 			return ret;
-		else if (ret != 3)
-			return -EIO;
 
 		if (inoutbuf[0] & 0x80)
 			break;
@@ -291,10 +285,8 @@ static int mcp4725_set_value(struct iio_dev *indio_dev, int val)
 	ret = i2c_master_send(data->client, outbuf, 2);
 	if (ret < 0)
 		return ret;
-	else if (ret != 2)
-		return -EIO;
-	else
-		return 0;
+
+	return 0;
 }
 
 static int mcp4726_set_cfg(struct iio_dev *indio_dev)
@@ -313,10 +305,8 @@ static int mcp4726_set_cfg(struct iio_dev *indio_dev)
 	ret = i2c_master_send(data->client, outbuf, 3);
 	if (ret < 0)
 		return ret;
-	else if (ret != 3)
-		return -EIO;
-	else
-		return 0;
+
+	return 0;
 }
 
 static int mcp4725_read_raw(struct iio_dev *indio_dev,
Signature · 1 line
2.43.0
#17 lore raw

PATCH 15/22 iio: dac: mcp4728: Remove redundant i2c_master_{send,recv}() length checks

In reply to Jonathan Cameron

i2c_master_send() and i2c_master_recv() return either a negative errno or exactly the count passed in, so a length check after the 'ret < 0' test can never be true. Drop the dead branches.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/dac/mcp4728.c | 13 +------------
 1 file changed, 1 insertion(+), 12 deletions(-)
drivers/iio/dac/mcp4728.c +1 -12
index 1fe184d49fc5..c238bc572014 100644
@@ -147,8 +147,6 @@ static ssize_t mcp4728_store_eeprom(struct device *dev,
 	ret = i2c_master_send(data->client, outbuf, MCP4728_WRITE_EEPROM_LEN);
 	if (ret < 0)
 		return ret;
-	else if (ret != MCP4728_WRITE_EEPROM_LEN)
-		return -EIO;
 
 	/* wait RDY signal for write complete, takes up to 50ms */
 	while (tries--) {
@@ -156,8 +154,6 @@ static ssize_t mcp4728_store_eeprom(struct device *dev,
 		ret = i2c_master_recv(data->client, inbuf, 3);
 		if (ret < 0)
 			return ret;
-		else if (ret != 3)
-			return -EIO;
 
 		if (FIELD_GET(MCP4728_RDY_MASK, inbuf[0]))
 			break;
@@ -205,8 +201,6 @@ static int mcp4728_program_channel_cfg(int channel, struct iio_dev *indio_dev)
 	ret = i2c_master_send(data->client, outbuf, 3);
 	if (ret < 0)
 		return ret;
-	else if (ret != 3)
-		return -EIO;
 
 	return 0;
 }
@@ -498,14 +492,9 @@ static int mcp4728_init_channels_data(struct mcp4728_data *data)
 	unsigned int i;
 
 	ret = i2c_master_recv(data->client, inbuf, MCP4728_READ_RESPONSE_LEN);
-	if (ret < 0) {
+	if (ret < 0)
 		return dev_err_probe(&data->client->dev, ret,
 				     "failed to read mcp4728 conf.\n");
-	} else if (ret != MCP4728_READ_RESPONSE_LEN) {
-		return dev_err_probe(&data->client->dev, -EIO,
-			"failed to read mcp4728 conf. Wrong Response Len ret=%d\n",
-			ret);
-	}
 
 	for (i = 0; i < MCP4728_N_CHANNELS; i++) {
 		struct mcp4728_channel_data *ch = &data->chdata[i];
Signature · 1 line
2.43.0
#18 lore raw

PATCH 16/22 iio: light: lv0104cs: Remove redundant i2c_master_{send,recv}() length checks

In reply to Jonathan Cameron

i2c_master_send() and i2c_master_recv() return either a negative errno or exactly the count passed in, so a length check after the 'ret < 0' test can never be true. Drop the dead branches.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/light/lv0104cs.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/iio/light/lv0104cs.c +0 -4
index eba82c334d70..41526c4886d7 100644
@@ -135,8 +135,6 @@ static int lv0104cs_write_reg(struct i2c_client *client, u8 regval)
 	ret = i2c_master_send(client, (char *)&regval, sizeof(regval));
 	if (ret < 0)
 		return ret;
-	if (ret != sizeof(regval))
-		return -EIO;
 
 	return 0;
 }
@@ -149,8 +147,6 @@ static int lv0104cs_read_adc(struct i2c_client *client, u16 *adc_output)
 	ret = i2c_master_recv(client, (char *)&regval, sizeof(regval));
 	if (ret < 0)
 		return ret;
-	if (ret != sizeof(regval))
-		return -EIO;
 
 	*adc_output = be16_to_cpu(regval);
 
Signature · 1 line
2.43.0
#19 lore raw

PATCH 17/22 iio: potentiometer: ad5110: Remove redundant i2c_master_{send,recv}() length checks

In reply to Jonathan Cameron

i2c_master_send_dmasafe() and i2c_master_recv_dmasafe() return either a negative errno or exactly the count passed in, so a length check after the 'ret < 0' test can never be true. Drop the dead branches.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/potentiometer/ad5110.c | 18 +++---------------
 1 file changed, 3 insertions(+), 15 deletions(-)
drivers/iio/potentiometer/ad5110.c +3 -15
index aaf02cc7aeba..0885f972ccea 100644
@@ -87,20 +87,12 @@ static int ad5110_read(struct ad5110_data *data, u8 cmd, int *val)
 	data->buf[1] = *val;
 
 	ret = i2c_master_send_dmasafe(data->client, data->buf, sizeof(data->buf));
-	if (ret < 0) {
+	if (ret < 0)
 		goto error;
-	} else if (ret != sizeof(data->buf)) {
-		ret = -EIO;
-		goto error;
-	}
 
 	ret = i2c_master_recv_dmasafe(data->client, data->buf, 1);
-	if (ret < 0) {
-		goto error;
-	} else if (ret != 1) {
-		ret = -EIO;
+	if (ret < 0)
 		goto error;
-	}
 
 	*val = data->buf[0];
 	ret = 0;
@@ -119,12 +111,8 @@ static int ad5110_write(struct ad5110_data *data, u8 cmd, u8 val)
 	data->buf[1] = val;
 
 	ret = i2c_master_send_dmasafe(data->client, data->buf, sizeof(data->buf));
-	if (ret < 0) {
-		goto error;
-	} else if (ret != sizeof(data->buf)) {
-		ret = -EIO;
+	if (ret < 0)
 		goto error;
-	}
 
 	ret = 0;
 
Signature · 1 line
2.43.0
#20 lore raw

PATCH 18/22 iio: pressure: abp2030pa: Remove redundant i2c_master_{send,recv}() length checks

In reply to Jonathan Cameron

i2c_master_send() and i2c_master_recv() return either a negative errno or exactly the count passed in, so a length check after the 'ret < 0' test can never be true. Drop the dead branches.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/pressure/abp2030pa_i2c.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/iio/pressure/abp2030pa_i2c.c +0 -4
index fa4d290cfd3e..bb9c0a1a5888 100644
@@ -24,8 +24,6 @@ static int abp2_i2c_read(struct abp2_data *data, u8 unused, u8 nbytes)
 	ret = i2c_master_recv(client, data->rx_buf, nbytes);
 	if (ret < 0)
 		return ret;
-	if (ret != nbytes)
-		return -EIO;
 
 	return 0;
 }
@@ -42,8 +40,6 @@ static int abp2_i2c_write(struct abp2_data *data, u8 cmd, u8 nbytes)
 	ret = i2c_master_send(client, data->tx_buf, nbytes);
 	if (ret < 0)
 		return ret;
-	if (ret != nbytes)
-		return -EIO;
 
 	return 0;
 }
Signature · 1 line
2.43.0
#21 lore raw

PATCH 19/22 iio: pressure: adp810: Remove redundant i2c_master_{send,recv}() length checks

In reply to Jonathan Cameron

i2c_master_send() and i2c_master_recv() return either a negative errno or exactly the count passed in, so a length check after the 'ret < 0' test can never be true. Drop the dead branches.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/pressure/adp810.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/iio/pressure/adp810.c +0 -4
index 82aa433548d2..6c740a9383e5 100644
@@ -64,8 +64,6 @@ static int adp810_measure(struct adp810_data *data, struct adp810_read_buf *buf)
 		dev_err(dev, "Error sending trigger command\n");
 		return ret;
 	}
-	if (ret != sizeof(trig_cmd))
-		return -EIO;
 
 	/*
 	 * Wait for the sensor to acquire data. As per datasheet section 5.3.1,
@@ -81,8 +79,6 @@ static int adp810_measure(struct adp810_data *data, struct adp810_read_buf *buf)
 		dev_err(dev, "Error reading from sensor\n");
 		return ret;
 	}
-	if (ret != sizeof(*buf))
-		return -EIO;
 
 	/* CRC checks */
 	crc8_populate_msb(crc_table, ADP810_CRC8_POLYNOMIAL);
Signature · 1 line
2.43.0
#22 lore raw

PATCH 20/22 iio: pressure: icp10100: Remove redundant i2c_master_send() length check

In reply to Jonathan Cameron

i2c_master_send() returns either a negative errno or exactly the count passed in, so the length check after the 'ret < 0' test can never be true. Drop the dead branch.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/pressure/icp10100.c | 2 --
 1 file changed, 2 deletions(-)
drivers/iio/pressure/icp10100.c +0 -2
index 82824e3e8db9..783d306bd959 100644
@@ -201,8 +201,6 @@ static int icp10100_read_cal_otp(struct icp10100_state *st)
 			      ARRAY_SIZE(icp10100_switch_mode_otp));
 	if (ret < 0)
 		return ret;
-	if (ret != ARRAY_SIZE(icp10100_switch_mode_otp))
-		return -EIO;
 
 	/* read 4 calibration values */
 	for (i = 0; i < 4; ++i) {
Signature · 1 line
2.43.0
#23 lore raw

PATCH 21/22 iio: pressure: mprls0025pa: Remove redundant i2c_master_{send,recv}() length checks

In reply to Jonathan Cameron

i2c_master_send() and i2c_master_recv() return either a negative errno or exactly the count passed in, so a length check after the 'ret < 0' test can never be true. Drop the dead branches.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/pressure/mprls0025pa_i2c.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/iio/pressure/mprls0025pa_i2c.c +0 -4
index 06907b64a596..061d9156b3db 100644
@@ -27,8 +27,6 @@ static int mpr_i2c_read(struct mpr_data *data, const u8 unused, const u8 cnt)
 	ret = i2c_master_recv(client, data->rx_buf, cnt);
 	if (ret < 0)
 		return ret;
-	else if (ret != cnt)
-		return -EIO;
 
 	return 0;
 }
@@ -42,8 +40,6 @@ static int mpr_i2c_write(struct mpr_data *data, const u8 cmd, const u8 unused)
 	ret = i2c_master_send(client, data->tx_buf, MPR_PKT_SYNC_LEN);
 	if (ret < 0)
 		return ret;
-	else if (ret != MPR_PKT_SYNC_LEN)
-		return -EIO;
 
 	return 0;
 }
Signature · 1 line
2.43.0
#24 lore raw

PATCH 22/22 iio: pressure: sdp500: Remove redundant i2c_master_recv() length check

In reply to Jonathan Cameron

i2c_master_recv() returns either a negative errno or exactly the count passed in, so the length check after the 'ret < 0' test can never be true. Drop the dead branch.

Assisted-by Claude:claude-opus-4-8
---
 drivers/iio/pressure/sdp500.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/iio/pressure/sdp500.c +0 -4
index 153f335ebf1e..f18b459114dd 100644
@@ -54,10 +54,6 @@ static int sdp500_read_raw(struct iio_dev *indio_dev,
 			dev_err(data->dev, "Failed to receive data");
 			return ret;
 		}
-		if (ret != SDP500_READ_SIZE) {
-			dev_err(data->dev, "Data is received wrongly");
-			return -EIO;
-		}
 
 		received_crc = rxbuf[2];
 		calculated_crc = crc8(sdp500_crc8_table, rxbuf,
Signature · 1 line
2.43.0
Reviewed-by on the v1 series
#25 lore raw
In reply to Jonathan Cameron
Quoted 7 lines · On Wed, 15 Jul 2026 17:57:15 -0700 Jonathan Cameron…

Resend to include linux-iio. Sorry to all who get it twice.

This came up in a recent review and I realised I have been giving incorrect review feedback for years. Both i2_master_send() and i2c_master_recv() plus the dma safe variants only ever return either an error or the full buffer size. Short accesses do not occur.

Quoted 6 lines

This series (also my first upstream series prepared by poking claude) removes the redundant checks from all IIO drivers.

Thanks,

Jonathan

FWIW, for the whole series:

Also, as trivial as this change is - if you were hoping on Sashiko picking this up then it failed to apply, as you didn't add a baseline.

Signature · 3 lines
Kind regards

CJD
#26 lore raw
Quoted 15 lines · On Thu, 16 Jul 2026 10:01:04 +0200 Joshua Crofts <[email protected]>…
Quoted 12 lines · On Wed, 15 Jul 2026 17:57:15 -0700 Jonathan Cameron…

Resend to include linux-iio. Sorry to all who get it twice.

This came up in a recent review and I realised I have been giving incorrect review feedback for years. Both i2_master_send() and i2c_master_recv() plus the dma safe variants only ever return either an error or the full buffer size. Short accesses do not occur.

This series (also my first upstream series prepared by poking claude) removes the redundant checks from all IIO drivers.

Thanks,

Quoted 9 lines
Quoted 1 line

Jonathan

FWIW, for the whole series:

Also, as trivial as this change is - if you were hoping on Sashiko picking this up then it failed to apply, as you didn't add a baseline.

Given one of the drivers is only on the IIO tree. I'm not sure how well Sashiko deals with that. Still I'll give it go next time!

Jonathan

Reviewed-by on the v1 series
#27 lore raw
In reply to Jonathan Cameron
Quoted 7 lines · Jonathan Cameron

Resend to include linux-iio. Sorry to all who get it twice.

This came up in a recent review and I realised I have been giving incorrect review feedback for years. Both i2_master_send() and i2c_master_recv() plus the dma safe variants only ever return either an error or the full buffer size. Short accesses do not occur.

Interesting! I had the same wrong assumptions! I guess the same is true for the smbus variants?

Anyways,

Quoted 71 lines

This series (also my first upstream series prepared by poking claude) removes the redundant checks from all IIO drivers.

Thanks,

Jonathan

Jonathan Cameron (22):
iio: adc: ltc2471: Remove redundant i2c_master_recv() length check
iio: adc: ti-ads7138: Remove redundant i2c_master_send() length checks
iio: adc: versal-sysmon: Remove redundant i2c_master_{send,recv}()
length checks
iio: chemical: scd30: Remove redundant i2c_master_{send,recv}() length
checks
iio: chemical: scd4x: Remove redundant i2c_master_{send,recv}() length
checks
iio: chemical: sgp30: Remove redundant i2c_master_recv() length check
iio: chemical: sgp40: Remove redundant i2c_master_recv() length check
iio: chemical: sps30: Remove redundant i2c_master_{send,recv}() length
checks
iio: dac: ad5446: Remove redundant i2c_master_send() length check
iio: dac: ad5696: Remove redundant i2c_master_send() length check
iio: dac: m62332: Remove redundant i2c_master_send() length check
iio: dac: max517: Remove redundant i2c_master_send() length check
iio: dac: max5821: Remove redundant i2c_master_{send,recv}() length
checks
iio: dac: mcp4725: Remove redundant i2c_master_{send,recv}() length
checks
iio: dac: mcp4728: Remove redundant i2c_master_{send,recv}() length
checks
iio: light: lv0104cs: Remove redundant i2c_master_{send,recv}() length
checks
iio: potentiometer: ad5110: Remove redundant i2c_master_{send,recv}()
length checks
iio: pressure: abp2030pa: Remove redundant i2c_master_{send,recv}()
length checks
iio: pressure: adp810: Remove redundant i2c_master_{send,recv}()
length checks
iio: pressure: icp10100: Remove redundant i2c_master_send() length
check
iio: pressure: mprls0025pa: Remove redundant i2c_master_{send,recv}()
length checks
iio: pressure: sdp500: Remove redundant i2c_master_recv() length check

 drivers/iio/adc/ltc2471.c              |  2 --
 drivers/iio/adc/ti-ads7138.c           |  4 ----
 drivers/iio/adc/versal-sysmon-i2c.c    |  6 ------
 drivers/iio/chemical/scd30_i2c.c       |  4 ----
 drivers/iio/chemical/scd4x.c           |  8 --------
 drivers/iio/chemical/sgp30.c           |  2 --
 drivers/iio/chemical/sgp40.c           |  4 ----
 drivers/iio/chemical/sps30_i2c.c       |  4 ----
 drivers/iio/dac/ad5446-i2c.c           |  2 --
 drivers/iio/dac/ad5696-i2c.c           |  2 +-
 drivers/iio/dac/m62332.c               |  2 --
 drivers/iio/dac/max517.c               |  6 ++----
 drivers/iio/dac/max5821.c              | 14 ++------------
 drivers/iio/dac/mcp4725.c              | 22 ++++++----------------
 drivers/iio/dac/mcp4728.c              | 13 +------------
 drivers/iio/light/lv0104cs.c           |  4 ----
 drivers/iio/potentiometer/ad5110.c     | 18 +++---------------
 drivers/iio/pressure/abp2030pa_i2c.c   |  4 ----
 drivers/iio/pressure/adp810.c          |  4 ----
 drivers/iio/pressure/icp10100.c        |  2 --
 drivers/iio/pressure/mprls0025pa_i2c.c |  4 ----
 drivers/iio/pressure/sdp500.c          |  4 ----
 22 files changed, 15 insertions(+), 120 deletions(-)
Signature · 1 line
2.43.0
#28 lore raw
In reply to Jonathan Cameron
Quoted 6 lines · Jonathan Cameron

Resend to include linux-iio. Sorry to all who get it twice.

This came up in a recent review and I realised I have been giving incorrect review feedback for years. Both i2_master_send() and i2c_master_recv() plus the dma safe variants only ever return either an error or the full buffer size. Short accesses do not occur.

There is actually one interesting corner case. If i2c_transfer_buffer_flags() returns 0 i2c_master_{send,recv}() will also return 0, in which case the updated drivers will now read uninitialized memory. Maybe i2c_master_{send,recv}() needs to be updated to translate that to -EIO.

Quoted 69 lines

This series (also my first upstream series prepared by poking claude) removes the redundant checks from all IIO drivers.

Thanks,

Jonathan

Jonathan Cameron (22):
iio: adc: ltc2471: Remove redundant i2c_master_recv() length check
iio: adc: ti-ads7138: Remove redundant i2c_master_send() length checks
iio: adc: versal-sysmon: Remove redundant i2c_master_{send,recv}()
length checks
iio: chemical: scd30: Remove redundant i2c_master_{send,recv}() length
checks
iio: chemical: scd4x: Remove redundant i2c_master_{send,recv}() length
checks
iio: chemical: sgp30: Remove redundant i2c_master_recv() length check
iio: chemical: sgp40: Remove redundant i2c_master_recv() length check
iio: chemical: sps30: Remove redundant i2c_master_{send,recv}() length
checks
iio: dac: ad5446: Remove redundant i2c_master_send() length check
iio: dac: ad5696: Remove redundant i2c_master_send() length check
iio: dac: m62332: Remove redundant i2c_master_send() length check
iio: dac: max517: Remove redundant i2c_master_send() length check
iio: dac: max5821: Remove redundant i2c_master_{send,recv}() length
checks
iio: dac: mcp4725: Remove redundant i2c_master_{send,recv}() length
checks
iio: dac: mcp4728: Remove redundant i2c_master_{send,recv}() length
checks
iio: light: lv0104cs: Remove redundant i2c_master_{send,recv}() length
checks
iio: potentiometer: ad5110: Remove redundant i2c_master_{send,recv}()
length checks
iio: pressure: abp2030pa: Remove redundant i2c_master_{send,recv}()
length checks
iio: pressure: adp810: Remove redundant i2c_master_{send,recv}()
length checks
iio: pressure: icp10100: Remove redundant i2c_master_send() length
check
iio: pressure: mprls0025pa: Remove redundant i2c_master_{send,recv}()
length checks
iio: pressure: sdp500: Remove redundant i2c_master_recv() length check

  drivers/iio/adc/ltc2471.c              |  2 --
  drivers/iio/adc/ti-ads7138.c           |  4 ----
  drivers/iio/adc/versal-sysmon-i2c.c    |  6 ------
  drivers/iio/chemical/scd30_i2c.c       |  4 ----
  drivers/iio/chemical/scd4x.c           |  8 --------
  drivers/iio/chemical/sgp30.c           |  2 --
  drivers/iio/chemical/sgp40.c           |  4 ----
  drivers/iio/chemical/sps30_i2c.c       |  4 ----
  drivers/iio/dac/ad5446-i2c.c           |  2 --
  drivers/iio/dac/ad5696-i2c.c           |  2 +-
  drivers/iio/dac/m62332.c               |  2 --
  drivers/iio/dac/max517.c               |  6 ++----
  drivers/iio/dac/max5821.c              | 14 ++------------
  drivers/iio/dac/mcp4725.c              | 22 ++++++----------------
  drivers/iio/dac/mcp4728.c              | 13 +------------
  drivers/iio/light/lv0104cs.c           |  4 ----
  drivers/iio/potentiometer/ad5110.c     | 18 +++---------------
  drivers/iio/pressure/abp2030pa_i2c.c   |  4 ----
  drivers/iio/pressure/adp810.c          |  4 ----
  drivers/iio/pressure/icp10100.c        |  2 --
  drivers/iio/pressure/mprls0025pa_i2c.c |  4 ----
  drivers/iio/pressure/sdp500.c          |  4 ----
  22 files changed, 15 insertions(+), 120 deletions(-)
#29 lore raw
Quoted 7 lines · On Thu, 16 Jul 2026 07:55:32 -0700 Lars-Peter Clausen <[email protected]> wrote:
Quoted 6 lines · Jonathan Cameron

Resend to include linux-iio. Sorry to all who get it twice.

This came up in a recent review and I realised I have been giving incorrect review feedback for years. Both i2_master_send() and i2c_master_recv() plus the dma safe variants only ever return either an error or the full buffer size. Short accesses do not occur.

Quoted 5 lines

There is actually one interesting corner case. If i2c_transfer_buffer_flags() returns 0 i2c_master_{send,recv}() will also return 0, in which case the updated drivers will now read uninitialized memory. Maybe i2c_master_{send,recv}() needs to be updated to translate that to -EIO.

That's indeed curious (and I'd missed it entirely!).

So ends up being either the master_xfer_atomic() or master_xfer() callbacks returning 0. master_xfer_atomic() is novel, it's a union with xfer_atomic() and no one ever sets it via that union element but given the types are the same all calls will actually be to xfer_atomic().

Could remove that complexity whilst here but there a bunch of related callbacks (e.g. master_xfer() in union with xfer() where drivers are still using the old naming and I'm not sure I want to chase that through various subsystem trees right now (though for fun - and maybe profit) I had claude work out a series. Was fun watching it write a semantic patch and spend a while debugging it. Glad it has as much trouble with coccinelle scripts as the rest of us. There are still a few hundred instances and I haven't touched all the callbacks, just this pair.

Anyhow, back to the question of whether they do return 0. Yup they do :(

https://elixir.bootlin.com/linux/v7.1.3/source/drivers/i2c/busses/i2c-cadence.c#L1153

So any defense against that case would require hardening further up the stack.

+CC Andi and the i2c list. Would anyone mind a patch to do something like:

static inline int i2c_master_recv(const struct i2c_client *client,
char *buf, int count)
{
- return i2c_transfer_buffer_flags(client, buf, count, I2C_M_RD);
+ int ret;
+
+ ret = i2c_transfer_buffer_flags(client, buf, count, I2C_M_RD);
+ if (ret == 0)
+ return -EIO;
+
+ return ret;
};

Just to avoid the need for special case checks on ever call from every driver so that they return ret if negative or else have to check for non 0 return
value and return -EIO or similar. Maybe somewhere out there is someone using
that 0 to mean something magic, but I doubt it.

Jonathan

`

Quoted 70 lines
Quoted 69 lines

This series (also my first upstream series prepared by poking claude) removes the redundant checks from all IIO drivers.

Thanks,

Jonathan

Jonathan Cameron (22):
iio: adc: ltc2471: Remove redundant i2c_master_recv() length check
iio: adc: ti-ads7138: Remove redundant i2c_master_send() length checks
iio: adc: versal-sysmon: Remove redundant i2c_master_{send,recv}()
length checks
iio: chemical: scd30: Remove redundant i2c_master_{send,recv}() length
checks
iio: chemical: scd4x: Remove redundant i2c_master_{send,recv}() length
checks
iio: chemical: sgp30: Remove redundant i2c_master_recv() length check
iio: chemical: sgp40: Remove redundant i2c_master_recv() length check
iio: chemical: sps30: Remove redundant i2c_master_{send,recv}() length
checks
iio: dac: ad5446: Remove redundant i2c_master_send() length check
iio: dac: ad5696: Remove redundant i2c_master_send() length check
iio: dac: m62332: Remove redundant i2c_master_send() length check
iio: dac: max517: Remove redundant i2c_master_send() length check
iio: dac: max5821: Remove redundant i2c_master_{send,recv}() length
checks
iio: dac: mcp4725: Remove redundant i2c_master_{send,recv}() length
checks
iio: dac: mcp4728: Remove redundant i2c_master_{send,recv}() length
checks
iio: light: lv0104cs: Remove redundant i2c_master_{send,recv}() length
checks
iio: potentiometer: ad5110: Remove redundant i2c_master_{send,recv}()
length checks
iio: pressure: abp2030pa: Remove redundant i2c_master_{send,recv}()
length checks
iio: pressure: adp810: Remove redundant i2c_master_{send,recv}()
length checks
iio: pressure: icp10100: Remove redundant i2c_master_send() length
check
iio: pressure: mprls0025pa: Remove redundant i2c_master_{send,recv}()
length checks
iio: pressure: sdp500: Remove redundant i2c_master_recv() length check

  drivers/iio/adc/ltc2471.c              |  2 --
  drivers/iio/adc/ti-ads7138.c           |  4 ----
  drivers/iio/adc/versal-sysmon-i2c.c    |  6 ------
  drivers/iio/chemical/scd30_i2c.c       |  4 ----
  drivers/iio/chemical/scd4x.c           |  8 --------
  drivers/iio/chemical/sgp30.c           |  2 --
  drivers/iio/chemical/sgp40.c           |  4 ----
  drivers/iio/chemical/sps30_i2c.c       |  4 ----
  drivers/iio/dac/ad5446-i2c.c           |  2 --
  drivers/iio/dac/ad5696-i2c.c           |  2 +-
  drivers/iio/dac/m62332.c               |  2 --
  drivers/iio/dac/max517.c               |  6 ++----
  drivers/iio/dac/max5821.c              | 14 ++------------
  drivers/iio/dac/mcp4725.c              | 22 ++++++----------------
  drivers/iio/dac/mcp4728.c              | 13 +------------
  drivers/iio/light/lv0104cs.c           |  4 ----
  drivers/iio/potentiometer/ad5110.c     | 18 +++---------------
  drivers/iio/pressure/abp2030pa_i2c.c   |  4 ----
  drivers/iio/pressure/adp810.c          |  4 ----
  drivers/iio/pressure/icp10100.c        |  2 --
  drivers/iio/pressure/mprls0025pa_i2c.c |  4 ----
  drivers/iio/pressure/sdp500.c          |  4 ----
  22 files changed, 15 insertions(+), 120 deletions(-)
#30 lore raw
Quoted 32 lines · Jonathan Cameron
Quoted 7 lines · On Thu, 16 Jul 2026 07:55:32 -0700 Lars-Peter Clausen <[email protected]> wrote:
Quoted 6 lines · Jonathan Cameron

Resend to include linux-iio. Sorry to all who get it twice.

This came up in a recent review and I realised I have been giving incorrect review feedback for years. Both i2_master_send() and i2c_master_recv() plus the dma safe variants only ever return either an error or the full buffer size. Short accesses do not occur.

Quoted 5 lines

There is actually one interesting corner case. If i2c_transfer_buffer_flags() returns 0 i2c_master_{send,recv}() will also return 0, in which case the updated drivers will now read uninitialized memory. Maybe i2c_master_{send,recv}() needs to be updated to translate that to -EIO.

That's indeed curious (and I'd missed it entirely!).

So ends up being either the master_xfer_atomic() or master_xfer() callbacks returning 0. master_xfer_atomic() is novel, it's a union with xfer_atomic() and no one ever sets it via that union element but given the types are the same all calls will actually be to xfer_atomic().

Could remove that complexity whilst here but there a bunch of related callbacks (e.g. master_xfer() in union with xfer() where drivers are still using the old naming and I'm not sure I want to chase that through various subsystem trees right now (though for fun - and maybe profit) I had claude work out a series. Was fun watching it write a semantic patch and spend a while debugging it. Glad it has as much trouble with coccinelle scripts as the rest of us. There are still a few hundred instances and I haven't touched all the callbacks, just this pair.

Quoted 7 lines

Anyhow, back to the question of whether they do return 0. Yup they do :(

https://elixir.bootlin.com/linux/v7.1.3/source/drivers/i2c/busses/i2c-cadence.c#L1153

So any defense against that case would require hardening further up the stack.

+CC Andi and the i2c list. Would anyone mind a patch to do something like:

I am fine with the approach but return value conversion needs a good comprehensive comment on top.

Quoted 20 lines

static inline int i2c_master_recv(const struct i2c_client *client,
char *buf, int count)
{

(quoted diff) +7 -1
-       return i2c_transfer_buffer_flags(client, buf, count, I2C_M_RD);
+       int ret;
+
+       ret = i2c_transfer_buffer_flags(client, buf, count, I2C_M_RD);
+       if (ret == 0)
+               return -EIO;
+
+       return ret;
 };

Just to avoid the need for special case checks on ever call from every driver so that they return ret if negative or else have to check for non 0 return
value and return -EIO or similar. Maybe somewhere out there is someone using
that 0 to mean something magic, but I doubt it.

Quoted 2 lines
Quoted 2 lines

This series (also my first upstream series prepared by poking claude) removes the redundant checks from all IIO drivers.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#31 lore raw
In reply to Jonathan Cameron
Quoted 43 lines · Jonathan Cameron
Quoted 7 lines · On Thu, 16 Jul 2026 07:55:32 -0700 Lars-Peter Clausen <[email protected]> wrote:
Quoted 6 lines · Jonathan Cameron

Resend to include linux-iio. Sorry to all who get it twice.

This came up in a recent review and I realised I have been giving incorrect review feedback for years. Both i2_master_send() and i2c_master_recv() plus the dma safe variants only ever return either an error or the full buffer size. Short accesses do not occur.

Quoted 5 lines

There is actually one interesting corner case. If i2c_transfer_buffer_flags() returns 0 i2c_master_{send,recv}() will also return 0, in which case the updated drivers will now read uninitialized memory. Maybe i2c_master_{send,recv}() needs to be updated to translate that to -EIO.

That's indeed curious (and I'd missed it entirely!).

So ends up being either the master_xfer_atomic() or master_xfer() callbacks returning 0. master_xfer_atomic() is novel, it's a union with xfer_atomic() and no one ever sets it via that union element but given the types are the same all calls will actually be to xfer_atomic().

Could remove that complexity whilst here but there a bunch of related callbacks (e.g. master_xfer() in union with xfer() where drivers are still using the old naming and I'm not sure I want to chase that through various subsystem trees right now (though for fun - and maybe profit) I had claude work out a series. Was fun watching it write a semantic patch and spend a while debugging it. Glad it has as much trouble with coccinelle scripts as the rest of us. There are still a few hundred instances and I haven't touched all the callbacks, just this pair.

Anyhow, back to the question of whether they do return 0. Yup they do :(

https://elixir.bootlin.com/linux/v7.1.3/source/drivers/i2c/busses/i2c-cadence.c#L1153

So any defense against that case would require hardening further up the stack.

(quoted diff) +1 -0
+CC Andi and the i2c list.  Would anyone mind a patch to do something like:
 
 static inline int i2c_master_recv(const struct i2c_client *client,
                                  char *buf, int count)

Review

Quoted 10 lines
(quoted diff) +7 -1
 {
-       return i2c_transfer_buffer_flags(client, buf, count, I2C_M_RD);
+       int ret;
+
+       ret = i2c_transfer_buffer_flags(client, buf, count, I2C_M_RD);
+       if (ret == 0)
+               return -EIO;
+
+       return ret;
 };

I do think something like the above makes sense. It goes in accordance with the expectations given by the docs.

- Nuno Sá