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

From: Sean Young

Date: Tue May 05 2026 - 08:52:37 EST


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@xxxxxxxxxxxxxxx
> Signed-off-by: Wenyuan Li <2063309626@xxxxxx>
>
> ---
> 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