[PATCH v4] media: ivtv: ir-i2c: check I2C transfer errors in get_key_adaptec()

Wenyuan Li posted 1 patch 1 week ago
drivers/media/pci/ivtv/ivtv-i2c.c | 21 +++++++++++++++++++--
1 file changed, 19 insertions(+), 2 deletions(-)
[PATCH v4] media: ivtv: ir-i2c: check I2C transfer errors in get_key_adaptec()
Posted by Wenyuan Li 1 week ago
In get_key_adaptec(), a command byte (0x00) is sent via
i2c_master_send() to initiate a key read, but the return value is not
checked.

If the transfer fails, the IR chip may not receive the command and the
subsequent i2c_master_recv() may return stale or invalid data. In this
case, the driver silently reports "no key", making such failures hard
to diagnose.

Check the return values of both i2c_master_send() and
i2c_master_recv(), and log errors using dev_err_ratelimited().
Short transfers are converted to -EIO while preserving existing
kernel error codes.

On error, still return 0 to keep the current behavior (no key
reported), but emit a diagnostic message to aid debugging.

Fixes: e1e2c5756563 ("[media] ivtv: Add Adaptec Remote Controller")
Cc: stable@vger.kernel.org
Signed-off-by: Wenyuan Li <2063309626@qq.com>

---
v4:
- Reword commit message to improve clarity and rationale
- No functional changes

v3:
- Add correct Fixes tag
- No functional changes

v2:
- Add error handling for i2c_master_send()
- Extend checking to i2c_master_recv()
- Use dev_err_ratelimited()
- Clarify error handling behavior
---
 drivers/media/pci/ivtv/ivtv-i2c.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/drivers/media/pci/ivtv/ivtv-i2c.c b/drivers/media/pci/ivtv/ivtv-i2c.c
index 28cb22d6a892..c011f2246add 100644
--- a/drivers/media/pci/ivtv/ivtv-i2c.c
+++ b/drivers/media/pci/ivtv/ivtv-i2c.c
@@ -138,11 +138,28 @@ static int get_key_adaptec(struct IR_i2c *ir, enum rc_proto *protocol,
 			   u32 *scancode, u8 *toggle)
 {
 	unsigned char keybuf[4];
+	int ret;
 
 	keybuf[0] = 0x00;
-	i2c_master_send(ir->c, keybuf, 1);
+
+	ret = i2c_master_send(ir->c, keybuf, 1);
+	if (ret != 1) {
+		int err = ret < 0 ? ret : -EIO;
+
+		dev_err_ratelimited(&ir->c->dev, "i2c_master_send failed: %pe\n", ERR_PTR(err));
+
+		/* Preserve existing behavior: treat error as no key */
+		return 0;
+	}
+
 	/* poll IR chip */
-	if (i2c_master_recv(ir->c, keybuf, sizeof(keybuf)) != sizeof(keybuf)) {
+	ret = i2c_master_recv(ir->c, keybuf, sizeof(keybuf));
+	if (ret != sizeof(keybuf)) {
+		int err = ret < 0 ? ret : -EIO;
+
+		dev_err_ratelimited(&ir->c->dev, "i2c_master_recv failed: %pe\n", ERR_PTR(err));
+
+		/* Preserve existing behavior */
 		return 0;
 	}
 
-- 
2.43.0