[PATCH] media: ir-i2c: add error handling for I2C send in get_key_adaptec()

Wenyuan Li posted 1 patch 1 week, 2 days ago
There is a newer version of this series
drivers/media/pci/ivtv/ivtv-i2c.c | 16 ++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)
[PATCH] media: ir-i2c: add error handling for I2C send in get_key_adaptec()
Posted by Wenyuan Li 1 week, 2 days ago
In get_key_adaptec(), a command byte (0x00) is sent to the IR chip via
i2c_master_send() to initiate a key read. However, the return value of
i2c_master_send() is not checked.

If this I2C transfer fails, the IR chip may not receive the read
command, causing the subsequent i2c_master_recv() to read stale or
invalid data. The driver would then return 0 (no key) without logging
the error, making debugging difficult.

Fix this by:
- Checking the return value of i2c_master_send()
- Converting partial sends to -EIO while preserving kernel error codes
- Adding dev_err() logging with %pe format for human-readable errors
- Adding similar error logging for the i2c_master_recv() path

If the send fails, return 0 to maintain the existing behavior (no key
detected), but log the error for debugging purposes.

Signed-off-by: Wenyuan Li <2063309626@qq.com>
---
 drivers/media/pci/ivtv/ivtv-i2c.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/drivers/media/pci/ivtv/ivtv-i2c.c b/drivers/media/pci/ivtv/ivtv-i2c.c
index 28cb22d6a892..c31bb4f6dd52 100644
--- a/drivers/media/pci/ivtv/ivtv-i2c.c
+++ b/drivers/media/pci/ivtv/ivtv-i2c.c
@@ -138,11 +138,23 @@ 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(&ir->c->dev, "send command failed: %pe\n", ERR_PTR(err));
+		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(&ir->c->dev, "receive failed: %pe\n", ERR_PTR(err));
 		return 0;
 	}
 
-- 
2.43.0
Re: [PATCH] media: ir-i2c: add error handling for I2C send in get_key_adaptec()
Posted by Markus Elfring 1 week, 2 days ago
> In get_key_adaptec(), a command byte (0x00) is sent to the IR chip via
> i2c_master_send() to initiate a key read. However, the return value of
> i2c_master_send() is not checked.

How do you think about to add any tags (like “Fixes” and “Cc”) accordingly?

See also:
* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v7.0-rc5#n145
* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/stable-kernel-rules.rst?h=v7.0-rc5#n34


…
> If the send fails, return 0 to maintain the existing behavior (no key
> detected), but log the error for debugging purposes.

Will such a technical aspect be reconsidered in more detail?

Regards,
Markus