Re: [PATCH 1/2] sh: kfr2r09: fix i2c adapter leak on USB gdaget setup

Geert Uytterhoeven <[email protected]> Wed, 24 Jun 2026 10:29:41 +0200
Newsgroups org.kernel.vger.linux-sh,org.kernel.vger.linux-kernel,org.kernel.vger.stable
Message-ID <CAMuHMdXAoZ6+Ch-qUhwbV=47PDHfgkARnZpo4h8y0h_uZP73Qw@mail.gmail.com>
Hi Johan,

On Fri, 8 May 2026 at 14:06, Johan Hovold <[email protected]> wrote:
> Make sure to drop the reference taken to the I2C adapter (and its
> module) when enabling USB gadget mode which prevents the adapter from
> ever being deregistered.
>
> Fixes: 5a1c4cb5bc22 ("sh: add r8a66597 usb0 gadget to the kfr2r09 board")
> Cc: [email protected]      # 2.6.32
> Cc: Magnus Damm <[email protected]>
> Signed-off-by: Johan Hovold <[email protected]>

Thanks for your patch!

> --- a/arch/sh/boards/mach-kfr2r09/setup.c
> +++ b/arch/sh/boards/mach-kfr2r09/setup.c
> @@ -368,7 +368,7 @@ static int kfr2r09_usb0_gadget_i2c_setup(void)
>         msg.flags = 0;
>         ret = i2c_transfer(a, &msg, 1);
>         if (ret != 1)
> -               return -ENODEV;
> +               goto err_put_adapter;
>
>         buf[0] = 0;
>         msg.addr = 0x09;
> @@ -377,7 +377,7 @@ static int kfr2r09_usb0_gadget_i2c_setup(void)
>         msg.flags = I2C_M_RD;
>         ret = i2c_transfer(a, &msg, 1);
>         if (ret != 1)
> -               return -ENODEV;
> +               goto err_put_adapter;
>
>         buf[1] = buf[0] | (1 << 1);
>         buf[0] = 0x13;
> @@ -387,9 +387,16 @@ static int kfr2r09_usb0_gadget_i2c_setup(void)
>         msg.flags = 0;
>         ret = i2c_transfer(a, &msg, 1);
>         if (ret != 1)
> -               return -ENODEV;
> +               goto err_put_adapter;
> +
> +       i2c_put_adapter(a);
>
>         return 0;
> +
> +err_put_adapter:
> +       i2c_put_adapter(a);
> +
> +       return -ENODEV;

I case i2c_transfer() returns a negative error code (the other
possible value is zero, right?), you might want to propagate that to
the caller. However, the single caller replaces it by -ENODEV anyway,
so I guess your patch is fine.

Reviewed-by: Geert Uytterhoeven <[email protected]>

>  }
>
>  static int kfr2r09_serial_i2c_setup(void)

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds