[PATCH v6 3/3] hwmon: (pmbus/max31785) check for partial i2c_transfer in read_long_data

Pradhan, Sanman posted 3 patches 2 weeks, 1 day ago
[PATCH v6 3/3] hwmon: (pmbus/max31785) check for partial i2c_transfer in read_long_data
Posted by Pradhan, Sanman 2 weeks, 1 day ago
From: Sanman Pradhan <psanman@juniper.net>

i2c_transfer() returns the number of messages successfully
transferred, not only a negative errno on failure. When called with
two messages (write command byte followed by a read of the 4-byte
response), a return value of 1 means the command write succeeded but
the read did not complete. In that case, rspbuf remains uninitialized
and must not be interpreted as valid data.

Treat any return value other than ARRAY_SIZE(msg) as an error, and
return -EIO for partial completion. Also return 0 on success instead
of the message count, since the caller only needs to distinguish
success from failure.

Signed-off-by: Sanman Pradhan <psanman@juniper.net>
---
v6:
- No changes to this patch in this version.

v5:
- New patch in the series. Fixes a pre-existing bug where partial
  i2c_transfer() returns left rspbuf uninitialized.
---
 drivers/hwmon/pmbus/max31785.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/hwmon/pmbus/max31785.c b/drivers/hwmon/pmbus/max31785.c
index 260aa8fb50f13..3caa76bcbeb5e 100644
--- a/drivers/hwmon/pmbus/max31785.c
+++ b/drivers/hwmon/pmbus/max31785.c
@@ -141,13 +141,13 @@ static int max31785_read_long_data(struct i2c_client *client, int page,
 	 */
 	pmbus_update_ts(client, 0);
 
-	if (rc < 0)
-		return rc;
+	if (rc != ARRAY_SIZE(msg))
+		return rc < 0 ? rc : -EIO;
 
 	*data = (rspbuf[0] << (0 * 8)) | (rspbuf[1] << (1 * 8)) |
 		(rspbuf[2] << (2 * 8)) | (rspbuf[3] << (3 * 8));
 
-	return rc;
+	return 0;
 }
 
 static int max31785_get_pwm(struct i2c_client *client, int page)
-- 
2.34.1

Re: [PATCH v6 3/3] hwmon: (pmbus/max31785) check for partial i2c_transfer in read_long_data
Posted by Guenter Roeck 2 weeks, 1 day ago
On Sat, Mar 21, 2026 at 06:12:05PM +0000, Pradhan, Sanman wrote:
> From: Sanman Pradhan <psanman@juniper.net>
> 
> i2c_transfer() returns the number of messages successfully
> transferred, not only a negative errno on failure. When called with
> two messages (write command byte followed by a read of the 4-byte
> response), a return value of 1 means the command write succeeded but
> the read did not complete. In that case, rspbuf remains uninitialized
> and must not be interpreted as valid data.
> 
> Treat any return value other than ARRAY_SIZE(msg) as an error, and
> return -EIO for partial completion. Also return 0 on success instead
> of the message count, since the caller only needs to distinguish
> success from failure.
> 
> Signed-off-by: Sanman Pradhan <psanman@juniper.net>

Applied.

Thanks,
Guenter