[PATCH 3/4] media: renesas: Use state cleanup macros

Tomi Valkeinen posted 4 patches 2 months, 1 week ago
[PATCH 3/4] media: renesas: Use state cleanup macros
Posted by Tomi Valkeinen 2 months, 1 week ago
From: Tomi Valkeinen <tomi.valkeinen+renesas@ideasonboard.com>

Use the new subdev state cleanup macros.

Signed-off-by: Tomi Valkeinen <tomi.valkeinen+renesas@ideasonboard.com>
---
 drivers/media/platform/renesas/rcar-csi2.c            | 14 ++++----------
 drivers/media/platform/renesas/rzg2l-cru/rzg2l-csi2.c |  9 ++++-----
 drivers/media/platform/renesas/rzg2l-cru/rzg2l-ip.c   |  9 ++-------
 3 files changed, 10 insertions(+), 22 deletions(-)

diff --git a/drivers/media/platform/renesas/rcar-csi2.c b/drivers/media/platform/renesas/rcar-csi2.c
index c419ddb4c5a2..03ef6566271f 100644
--- a/drivers/media/platform/renesas/rcar-csi2.c
+++ b/drivers/media/platform/renesas/rcar-csi2.c
@@ -1163,27 +1163,24 @@ static void rcsi2_stop(struct rcar_csi2 *priv)
 static int rcsi2_s_stream(struct v4l2_subdev *sd, int enable)
 {
 	struct rcar_csi2 *priv = sd_to_csi2(sd);
-	struct v4l2_subdev_state *state;
 	int ret = 0;
 
 	if (!priv->remote)
 		return -ENODEV;
 
-	state = v4l2_subdev_lock_and_get_active_state(&priv->subdev);
+	CLASS(v4l2_subdev_lock_and_get_active_state, state)(&priv->subdev);
 
 	if (enable && priv->stream_count == 0) {
 		ret = rcsi2_start(priv, state);
 		if (ret)
-			goto out;
+			return ret;
 	} else if (!enable && priv->stream_count == 1) {
 		rcsi2_stop(priv);
 	}
 
 	priv->stream_count += enable ? 1 : -1;
-out:
-	v4l2_subdev_unlock_state(state);
 
-	return ret;
+	return 0;
 }
 
 static int rcsi2_set_pad_format(struct v4l2_subdev *sd,
@@ -1274,18 +1271,15 @@ static irqreturn_t rcsi2_irq(int irq, void *data)
 
 static irqreturn_t rcsi2_irq_thread(int irq, void *data)
 {
-	struct v4l2_subdev_state *state;
 	struct rcar_csi2 *priv = data;
 
-	state = v4l2_subdev_lock_and_get_active_state(&priv->subdev);
+	CLASS(v4l2_subdev_lock_and_get_active_state, state)(&priv->subdev);
 
 	rcsi2_stop(priv);
 	usleep_range(1000, 2000);
 	if (rcsi2_start(priv, state))
 		dev_warn(priv->dev, "Failed to restart CSI-2 receiver\n");
 
-	v4l2_subdev_unlock_state(state);
-
 	return IRQ_HANDLED;
 }
 
diff --git a/drivers/media/platform/renesas/rzg2l-cru/rzg2l-csi2.c b/drivers/media/platform/renesas/rzg2l-cru/rzg2l-csi2.c
index e68fcdaea207..63b846f3e468 100644
--- a/drivers/media/platform/renesas/rzg2l-cru/rzg2l-csi2.c
+++ b/drivers/media/platform/renesas/rzg2l-cru/rzg2l-csi2.c
@@ -238,7 +238,6 @@ static int rzg2l_csi2_calc_mbps(struct rzg2l_csi2 *csi2)
 	struct v4l2_subdev *source = csi2->remote_source;
 	const struct rzg2l_csi2_format *format;
 	const struct v4l2_mbus_framefmt *fmt;
-	struct v4l2_subdev_state *state;
 	struct v4l2_ctrl *ctrl;
 	u64 mbps;
 
@@ -250,10 +249,10 @@ static int rzg2l_csi2_calc_mbps(struct rzg2l_csi2 *csi2)
 		return -EINVAL;
 	}
 
-	state = v4l2_subdev_lock_and_get_active_state(&csi2->subdev);
-	fmt = v4l2_subdev_state_get_format(state, RZG2L_CSI2_SINK);
-	format = rzg2l_csi2_code_to_fmt(fmt->code);
-	v4l2_subdev_unlock_state(state);
+	scoped_v4l2_subdev_lock_and_get_active_state(&csi2->subdev) {
+		fmt = v4l2_subdev_state_get_format(state, RZG2L_CSI2_SINK);
+		format = rzg2l_csi2_code_to_fmt(fmt->code);
+	}
 
 	/*
 	 * Calculate hsfreq in Mbps
diff --git a/drivers/media/platform/renesas/rzg2l-cru/rzg2l-ip.c b/drivers/media/platform/renesas/rzg2l-cru/rzg2l-ip.c
index ac8ebae4ed07..0b9e8a7cf22a 100644
--- a/drivers/media/platform/renesas/rzg2l-cru/rzg2l-ip.c
+++ b/drivers/media/platform/renesas/rzg2l-cru/rzg2l-ip.c
@@ -36,14 +36,9 @@ static const struct rzg2l_cru_ip_format *rzg2l_cru_ip_code_to_fmt(unsigned int c
 
 struct v4l2_mbus_framefmt *rzg2l_cru_ip_get_src_fmt(struct rzg2l_cru_dev *cru)
 {
-	struct v4l2_subdev_state *state;
-	struct v4l2_mbus_framefmt *fmt;
+	CLASS(v4l2_subdev_lock_and_get_active_state, state)(&cru->ip.subdev);
 
-	state = v4l2_subdev_lock_and_get_active_state(&cru->ip.subdev);
-	fmt = v4l2_subdev_state_get_format(state, 1);
-	v4l2_subdev_unlock_state(state);
-
-	return fmt;
+	return v4l2_subdev_state_get_format(state, 1);
 }
 
 static int rzg2l_cru_ip_s_stream(struct v4l2_subdev *sd, int enable)

-- 
2.43.0
Re: [PATCH 3/4] media: renesas: Use state cleanup macros
Posted by Niklas Söderlund 2 months, 1 week ago
Hi Tomi,

Thanks for your work. I like the scoped management.

On 2024-09-17 17:09:31 +0300, Tomi Valkeinen wrote:
> From: Tomi Valkeinen <tomi.valkeinen+renesas@ideasonboard.com>
> 
> Use the new subdev state cleanup macros.
> 
> Signed-off-by: Tomi Valkeinen <tomi.valkeinen+renesas@ideasonboard.com>
> ---
>  drivers/media/platform/renesas/rcar-csi2.c            | 14 ++++----------
>  drivers/media/platform/renesas/rzg2l-cru/rzg2l-csi2.c |  9 ++++-----
>  drivers/media/platform/renesas/rzg2l-cru/rzg2l-ip.c   |  9 ++-------
>  3 files changed, 10 insertions(+), 22 deletions(-)
> 
> diff --git a/drivers/media/platform/renesas/rcar-csi2.c b/drivers/media/platform/renesas/rcar-csi2.c
> index c419ddb4c5a2..03ef6566271f 100644
> --- a/drivers/media/platform/renesas/rcar-csi2.c
> +++ b/drivers/media/platform/renesas/rcar-csi2.c
> @@ -1163,27 +1163,24 @@ static void rcsi2_stop(struct rcar_csi2 *priv)
>  static int rcsi2_s_stream(struct v4l2_subdev *sd, int enable)
>  {
>  	struct rcar_csi2 *priv = sd_to_csi2(sd);
> -	struct v4l2_subdev_state *state;
>  	int ret = 0;
>  
>  	if (!priv->remote)
>  		return -ENODEV;
>  
> -	state = v4l2_subdev_lock_and_get_active_state(&priv->subdev);
> +	CLASS(v4l2_subdev_lock_and_get_active_state, state)(&priv->subdev);
>  
>  	if (enable && priv->stream_count == 0) {
>  		ret = rcsi2_start(priv, state);
>  		if (ret)
> -			goto out;
> +			return ret;

As ret is now only used in this branch maybe we can move the declaration 
of it here? At least I think you should remove the assignment to 0 above 
as that behavior is not needed anymore but, at lest to me, keeping it 
indicates there is an intent in initializing it.

With that fixed,

Reviewed-by: Niklas Söderlund <niklas.soderlund+renesas@ragnatech.se>

>  	} else if (!enable && priv->stream_count == 1) {
>  		rcsi2_stop(priv);
>  	}
>  
>  	priv->stream_count += enable ? 1 : -1;
> -out:
> -	v4l2_subdev_unlock_state(state);
>  
> -	return ret;
> +	return 0;
>  }
>  
>  static int rcsi2_set_pad_format(struct v4l2_subdev *sd,
> @@ -1274,18 +1271,15 @@ static irqreturn_t rcsi2_irq(int irq, void *data)
>  
>  static irqreturn_t rcsi2_irq_thread(int irq, void *data)
>  {
> -	struct v4l2_subdev_state *state;
>  	struct rcar_csi2 *priv = data;
>  
> -	state = v4l2_subdev_lock_and_get_active_state(&priv->subdev);
> +	CLASS(v4l2_subdev_lock_and_get_active_state, state)(&priv->subdev);
>  
>  	rcsi2_stop(priv);
>  	usleep_range(1000, 2000);
>  	if (rcsi2_start(priv, state))
>  		dev_warn(priv->dev, "Failed to restart CSI-2 receiver\n");
>  
> -	v4l2_subdev_unlock_state(state);
> -
>  	return IRQ_HANDLED;
>  }
>  
> diff --git a/drivers/media/platform/renesas/rzg2l-cru/rzg2l-csi2.c b/drivers/media/platform/renesas/rzg2l-cru/rzg2l-csi2.c
> index e68fcdaea207..63b846f3e468 100644
> --- a/drivers/media/platform/renesas/rzg2l-cru/rzg2l-csi2.c
> +++ b/drivers/media/platform/renesas/rzg2l-cru/rzg2l-csi2.c
> @@ -238,7 +238,6 @@ static int rzg2l_csi2_calc_mbps(struct rzg2l_csi2 *csi2)
>  	struct v4l2_subdev *source = csi2->remote_source;
>  	const struct rzg2l_csi2_format *format;
>  	const struct v4l2_mbus_framefmt *fmt;
> -	struct v4l2_subdev_state *state;
>  	struct v4l2_ctrl *ctrl;
>  	u64 mbps;
>  
> @@ -250,10 +249,10 @@ static int rzg2l_csi2_calc_mbps(struct rzg2l_csi2 *csi2)
>  		return -EINVAL;
>  	}
>  
> -	state = v4l2_subdev_lock_and_get_active_state(&csi2->subdev);
> -	fmt = v4l2_subdev_state_get_format(state, RZG2L_CSI2_SINK);
> -	format = rzg2l_csi2_code_to_fmt(fmt->code);
> -	v4l2_subdev_unlock_state(state);
> +	scoped_v4l2_subdev_lock_and_get_active_state(&csi2->subdev) {
> +		fmt = v4l2_subdev_state_get_format(state, RZG2L_CSI2_SINK);
> +		format = rzg2l_csi2_code_to_fmt(fmt->code);
> +	}
>  
>  	/*
>  	 * Calculate hsfreq in Mbps
> diff --git a/drivers/media/platform/renesas/rzg2l-cru/rzg2l-ip.c b/drivers/media/platform/renesas/rzg2l-cru/rzg2l-ip.c
> index ac8ebae4ed07..0b9e8a7cf22a 100644
> --- a/drivers/media/platform/renesas/rzg2l-cru/rzg2l-ip.c
> +++ b/drivers/media/platform/renesas/rzg2l-cru/rzg2l-ip.c
> @@ -36,14 +36,9 @@ static const struct rzg2l_cru_ip_format *rzg2l_cru_ip_code_to_fmt(unsigned int c
>  
>  struct v4l2_mbus_framefmt *rzg2l_cru_ip_get_src_fmt(struct rzg2l_cru_dev *cru)
>  {
> -	struct v4l2_subdev_state *state;
> -	struct v4l2_mbus_framefmt *fmt;
> +	CLASS(v4l2_subdev_lock_and_get_active_state, state)(&cru->ip.subdev);
>  
> -	state = v4l2_subdev_lock_and_get_active_state(&cru->ip.subdev);
> -	fmt = v4l2_subdev_state_get_format(state, 1);
> -	v4l2_subdev_unlock_state(state);
> -
> -	return fmt;
> +	return v4l2_subdev_state_get_format(state, 1);
>  }
>  
>  static int rzg2l_cru_ip_s_stream(struct v4l2_subdev *sd, int enable)
> 
> -- 
> 2.43.0
> 

-- 
Kind Regards,
Niklas Söderlund
Re: [PATCH 3/4] media: renesas: Use state cleanup macros
Posted by Laurent Pinchart 2 months ago
On Sun, Sep 22, 2024 at 12:15:19PM +0200, Niklas Söderlund wrote:
> Hi Tomi,
> 
> Thanks for your work. I like the scoped management.
> 
> On 2024-09-17 17:09:31 +0300, Tomi Valkeinen wrote:
> > From: Tomi Valkeinen <tomi.valkeinen+renesas@ideasonboard.com>
> > 
> > Use the new subdev state cleanup macros.
> > 
> > Signed-off-by: Tomi Valkeinen <tomi.valkeinen+renesas@ideasonboard.com>
> > ---
> >  drivers/media/platform/renesas/rcar-csi2.c            | 14 ++++----------
> >  drivers/media/platform/renesas/rzg2l-cru/rzg2l-csi2.c |  9 ++++-----
> >  drivers/media/platform/renesas/rzg2l-cru/rzg2l-ip.c   |  9 ++-------
> >  3 files changed, 10 insertions(+), 22 deletions(-)
> > 
> > diff --git a/drivers/media/platform/renesas/rcar-csi2.c b/drivers/media/platform/renesas/rcar-csi2.c
> > index c419ddb4c5a2..03ef6566271f 100644
> > --- a/drivers/media/platform/renesas/rcar-csi2.c
> > +++ b/drivers/media/platform/renesas/rcar-csi2.c
> > @@ -1163,27 +1163,24 @@ static void rcsi2_stop(struct rcar_csi2 *priv)
> >  static int rcsi2_s_stream(struct v4l2_subdev *sd, int enable)
> >  {
> >  	struct rcar_csi2 *priv = sd_to_csi2(sd);
> > -	struct v4l2_subdev_state *state;
> >  	int ret = 0;
> >  
> >  	if (!priv->remote)
> >  		return -ENODEV;
> >  
> > -	state = v4l2_subdev_lock_and_get_active_state(&priv->subdev);
> > +	CLASS(v4l2_subdev_lock_and_get_active_state, state)(&priv->subdev);
> >  
> >  	if (enable && priv->stream_count == 0) {
> >  		ret = rcsi2_start(priv, state);
> >  		if (ret)
> > -			goto out;
> > +			return ret;
> 
> As ret is now only used in this branch maybe we can move the declaration 
> of it here? At least I think you should remove the assignment to 0 above 
> as that behavior is not needed anymore but, at lest to me, keeping it 
> indicates there is an intent in initializing it.
> 
> With that fixed,
> 
> Reviewed-by: Niklas Söderlund <niklas.soderlund+renesas@ragnatech.se>
> 
> >  	} else if (!enable && priv->stream_count == 1) {
> >  		rcsi2_stop(priv);
> >  	}
> >  
> >  	priv->stream_count += enable ? 1 : -1;
> > -out:
> > -	v4l2_subdev_unlock_state(state);
> >  
> > -	return ret;
> > +	return 0;
> >  }
> >  
> >  static int rcsi2_set_pad_format(struct v4l2_subdev *sd,
> > @@ -1274,18 +1271,15 @@ static irqreturn_t rcsi2_irq(int irq, void *data)
> >  
> >  static irqreturn_t rcsi2_irq_thread(int irq, void *data)
> >  {
> > -	struct v4l2_subdev_state *state;
> >  	struct rcar_csi2 *priv = data;
> >  
> > -	state = v4l2_subdev_lock_and_get_active_state(&priv->subdev);
> > +	CLASS(v4l2_subdev_lock_and_get_active_state, state)(&priv->subdev);
> >  
> >  	rcsi2_stop(priv);
> >  	usleep_range(1000, 2000);
> >  	if (rcsi2_start(priv, state))
> >  		dev_warn(priv->dev, "Failed to restart CSI-2 receiver\n");
> >  
> > -	v4l2_subdev_unlock_state(state);
> > -
> >  	return IRQ_HANDLED;
> >  }
> >  
> > diff --git a/drivers/media/platform/renesas/rzg2l-cru/rzg2l-csi2.c b/drivers/media/platform/renesas/rzg2l-cru/rzg2l-csi2.c
> > index e68fcdaea207..63b846f3e468 100644
> > --- a/drivers/media/platform/renesas/rzg2l-cru/rzg2l-csi2.c
> > +++ b/drivers/media/platform/renesas/rzg2l-cru/rzg2l-csi2.c
> > @@ -238,7 +238,6 @@ static int rzg2l_csi2_calc_mbps(struct rzg2l_csi2 *csi2)
> >  	struct v4l2_subdev *source = csi2->remote_source;
> >  	const struct rzg2l_csi2_format *format;
> >  	const struct v4l2_mbus_framefmt *fmt;
> > -	struct v4l2_subdev_state *state;
> >  	struct v4l2_ctrl *ctrl;
> >  	u64 mbps;
> >  
> > @@ -250,10 +249,10 @@ static int rzg2l_csi2_calc_mbps(struct rzg2l_csi2 *csi2)
> >  		return -EINVAL;
> >  	}
> >  
> > -	state = v4l2_subdev_lock_and_get_active_state(&csi2->subdev);
> > -	fmt = v4l2_subdev_state_get_format(state, RZG2L_CSI2_SINK);
> > -	format = rzg2l_csi2_code_to_fmt(fmt->code);
> > -	v4l2_subdev_unlock_state(state);
> > +	scoped_v4l2_subdev_lock_and_get_active_state(&csi2->subdev) {
> > +		fmt = v4l2_subdev_state_get_format(state, RZG2L_CSI2_SINK);

fmt could also become a local variable.

Now that I'm looking at this, another issue with
scoped_v4l2_subdev_lock_and_get_active_state() is that it creates a
non-const state variable, while there are use cases for const states.
I'm increasingly thinking we should use __free(), as neither the
scoped_* macro nor CLASS() allow the caller to indicate if the local
variable should be const or not.

> > +		format = rzg2l_csi2_code_to_fmt(fmt->code);
> > +	}
> >  
> >  	/*
> >  	 * Calculate hsfreq in Mbps
> > diff --git a/drivers/media/platform/renesas/rzg2l-cru/rzg2l-ip.c b/drivers/media/platform/renesas/rzg2l-cru/rzg2l-ip.c
> > index ac8ebae4ed07..0b9e8a7cf22a 100644
> > --- a/drivers/media/platform/renesas/rzg2l-cru/rzg2l-ip.c
> > +++ b/drivers/media/platform/renesas/rzg2l-cru/rzg2l-ip.c
> > @@ -36,14 +36,9 @@ static const struct rzg2l_cru_ip_format *rzg2l_cru_ip_code_to_fmt(unsigned int c
> >  
> >  struct v4l2_mbus_framefmt *rzg2l_cru_ip_get_src_fmt(struct rzg2l_cru_dev *cru)
> >  {
> > -	struct v4l2_subdev_state *state;
> > -	struct v4l2_mbus_framefmt *fmt;
> > +	CLASS(v4l2_subdev_lock_and_get_active_state, state)(&cru->ip.subdev);
> >  
> > -	state = v4l2_subdev_lock_and_get_active_state(&cru->ip.subdev);
> > -	fmt = v4l2_subdev_state_get_format(state, 1);
> > -	v4l2_subdev_unlock_state(state);
> > -
> > -	return fmt;
> > +	return v4l2_subdev_state_get_format(state, 1);
> >  }
> >  
> >  static int rzg2l_cru_ip_s_stream(struct v4l2_subdev *sd, int enable)
> > 

-- 
Regards,

Laurent Pinchart