Re: Re: [PATCH 17/19] media: i2c: imx290: Factor out format retrieval to separate function

Alexander Stein
2022-07-21T11:12:26Z

Hi Laurent,

Am Donnerstag, 21. Juli 2022, 12:36:35 CEST schrieb Laurent Pinchart:
> Hi Alexander,
>
> On Thu, Jul 21, 2022 at 12:11:31PM +0200, Alexander Stein wrote:
> > Am Donnerstag, 21. Juli 2022, 10:35:38 CEST schrieb Laurent Pinchart:
> > > The driver duplicates the same pattern to access the try or active
> > > format in multiple locations. Factor it out to a separate function.
> > >
> > > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > > ---
> > >
> > > drivers/media/i2c/imx290.c | 28 ++++++++++++++++++----------
> > > 1 file changed, 18 insertions(+), 10 deletions(-)
> > >
> > > diff --git a/drivers/media/i2c/imx290.c b/drivers/media/i2c/imx290.c
> > > index fc6e87fada1c..baf9941c5fbe 100644
> > > --- a/drivers/media/i2c/imx290.c
> > > +++ b/drivers/media/i2c/imx290.c
> > > @@ -519,6 +519,20 @@ static const struct v4l2_ctrl_ops imx290_ctrl_ops =
> > > {
> > >
> > > .s_ctrl = imx290_set_ctrl,
> > >
> > > };
> > >
> > > +static struct v4l2_mbus_framefmt *
> > > +imx290_get_pad_format(struct imx290 *imx290, struct v4l2_subdev_state
> > > *state, + u32 which)
> > > +{
> > > + switch (which) {
> > > + case V4L2_SUBDEV_FORMAT_TRY:
> > > + return v4l2_subdev_get_try_format(&imx290->sd, state,
0);
> > > + case V4L2_SUBDEV_FORMAT_ACTIVE:
> > > + return &imx290->current_format;
> > > + default:
> > > + return NULL;
> > > + }
> > > +}
> > > +
> > >
> > > static int imx290_enum_mbus_code(struct v4l2_subdev *sd,
> > >
> > > struct v4l2_subdev_state
*sd_state,
> > > struct v4l2_subdev_mbus_code_enum
*code)
> > >
> > > @@ -562,12 +576,7 @@ static int imx290_get_fmt(struct v4l2_subdev *sd,
> > >
> > > mutex_lock(&imx290->lock);
> > >
> > > - if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
> > > - framefmt = v4l2_subdev_get_try_format(&imx290->sd,
sd_state,
> > > - fmt-
>pad);
> > > - else
> > > - framefmt = &imx290->current_format;
> > > -
> > > + framefmt = imx290_get_pad_format(imx290, sd_state, fmt->which);
> > >
> > > fmt->format = *framefmt;
> >
> > NULL ptr derence here if 'fmt->which' is neither V4L2_SUBDEV_FORMAT_TRY
> > nor
> > V4L2_SUBDEV_FORMAT_ACTIVE. Same for imx290_set_fmt later on.
>
> You're right, but this can't happen, TRY and ACTIVE are the only two
> possible values.

fmt (and therefore which) is only a u32, so there is no benefit to treat it
like an enum. IMHO using the old if-else structure seems better. This might
also help static code analysis which does not have such background
information.
If 'which' would be changed to type enum v4l2_subdev_format_whence, the switch
case seems preferable.

Best regards,
Alexander

> > > mutex_unlock(&imx290->lock);
> > >
> > > @@ -627,10 +636,9 @@ static int imx290_set_fmt(struct v4l2_subdev *sd,
> > >
> > > fmt->format.code = imx290_formats[i].code;
> > > fmt->format.field = V4L2_FIELD_NONE;
> > >
> > > - if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
> > > - format = v4l2_subdev_get_try_format(sd, sd_state, fmt-
>pad);
> > > - } else {
> > > - format = &imx290->current_format;
> > > + format = imx290_get_pad_format(imx290, sd_state, fmt->which);
> > > +
> > > + if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
> > >
> > > imx290->current_mode = mode;
> > > imx290->bpp = imx290_formats[i].bpp;