public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4] media: ivtv: ir-i2c: check I2C transfer errors in get_key_adaptec()
@ 2026-03-29 12:41 Wenyuan Li
  2026-05-05 12:49 ` Sean Young
  0 siblings, 1 reply; 2+ messages in thread
From: Wenyuan Li @ 2026-03-29 12:41 UTC (permalink / raw)
  To: Andy Walls, Mauro Carvalho Chehab
  Cc: linux-media, linux-kernel, Markus Elfring, gszhai, 25125332,
	25125283, 23120469, Wenyuan Li, stable

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v4] media: ivtv: ir-i2c: check I2C transfer errors in get_key_adaptec()
  2026-03-29 12:41 [PATCH v4] media: ivtv: ir-i2c: check I2C transfer errors in get_key_adaptec() Wenyuan Li
@ 2026-05-05 12:49 ` Sean Young
  0 siblings, 0 replies; 2+ messages in thread
From: Sean Young @ 2026-05-05 12:49 UTC (permalink / raw)
  To: Wenyuan Li
  Cc: Andy Walls, Mauro Carvalho Chehab, linux-media, linux-kernel,
	Markus Elfring, gszhai, 25125332, 25125283, 23120469, stable

On Sun, Mar 29, 2026 at 08:41:28PM +0800, Wenyuan Li wrote:
> 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));


get_key_adaptec() is called from ir_key_poll(), which already logs errors
with dev_warn(). Other i2c key handlers simply return an error and let
ir_key_poll() log the error code. That's better than duplicating a dev_err()
or dev_warn() here.

> +
> +		/* Preserve existing behavior: treat error as no key */
> +		return 0;
> +	}

Simply return the error.
> +
>  	/* 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;

Same here - simply return the error (or -EIO if rc >= 0).

Thanks,

Sean

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-05-05 12:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-29 12:41 [PATCH v4] media: ivtv: ir-i2c: check I2C transfer errors in get_key_adaptec() Wenyuan Li
2026-05-05 12:49 ` Sean Young

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox