[PATCH v2 04/10] media: v4l2-dev: Add helpers to get current format from the state

Jai Luthra posted 10 patches 1 week, 5 days ago
[PATCH v2 04/10] media: v4l2-dev: Add helpers to get current format from the state
Posted by Jai Luthra 1 week, 5 days ago
Add a helper function that drivers can call to retrieve the current
v4l2_format stored in a video device state for internal use.

Additionally, provide a G_FMT ioctl implementation that drivers can use
instead of writing their own when the format is stored in the active
state.

Signed-off-by: Jai Luthra <jai.luthra@ideasonboard.com>
--
Cc: Mauro Carvalho Chehab <mchehab@kernel.org>
Cc: Hans Verkuil <hverkuil@kernel.org>
Cc: Jai Luthra <jai.luthra@ideasonboard.com>
Cc: Ricardo Ribalda <ribalda@chromium.org>
Cc: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
Cc: Ma Ke <make24@iscas.ac.cn>
Cc: linux-media@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
---
 drivers/media/v4l2-core/v4l2-dev.c | 23 +++++++++++++++++++++++
 include/media/v4l2-dev.h           | 18 ++++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/drivers/media/v4l2-core/v4l2-dev.c b/drivers/media/v4l2-core/v4l2-dev.c
index dff23c6a0b56fb3d29e1c04e386bb445fa8773bb..2606077538be0e83032c6ae8956c1d67da0d0c5d 100644
--- a/drivers/media/v4l2-core/v4l2-dev.c
+++ b/drivers/media/v4l2-core/v4l2-dev.c
@@ -199,6 +199,29 @@ void __video_device_state_free(struct video_device_state *state)
 }
 EXPORT_SYMBOL_GPL(__video_device_state_free);
 
+struct v4l2_format *video_device_state_get_fmt(struct video_device_state *state)
+{
+	if (WARN_ON_ONCE(!state))
+		return NULL;
+
+	return &state->fmt;
+}
+EXPORT_SYMBOL_GPL(video_device_state_get_fmt);
+
+int video_device_g_fmt(struct file *file, void *priv, struct v4l2_format *fmt)
+{
+	struct video_device_state *state = priv;
+	struct v4l2_format *vfmt = video_device_state_get_fmt(state);
+
+	if (!vfmt)
+		return -EINVAL;
+
+	*fmt = *vfmt;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(video_device_g_fmt);
+
 static inline void video_get(struct video_device *vdev)
 {
 	get_device(&vdev->dev);
diff --git a/include/media/v4l2-dev.h b/include/media/v4l2-dev.h
index d327be16f6def70554a7d92d10436a29384ae32a..b5312823fbff9c236d4394d48fa9a14412b17c68 100644
--- a/include/media/v4l2-dev.h
+++ b/include/media/v4l2-dev.h
@@ -612,6 +612,24 @@ __video_device_state_alloc(struct video_device *vdev,
  */
 void __video_device_state_free(struct video_device_state *state);
 
+/**
+ * video_device_state_get_fmt - get current v4l2_format.
+ *
+ * @state: pointer to struct video_device_state
+ */
+struct v4l2_format *
+video_device_state_get_fmt(struct video_device_state *state);
+
+/**
+ * video_device_g_fmt - fill v4l2_format from the state.
+ *
+ * @file: pointer to struct file
+ * @state: pointer to struct video_device_state
+ * @format: pointer to struct v4l2_format
+ */
+int video_device_g_fmt(struct file *file, void *priv,
+		       struct v4l2_format *format);
+
 /**
  * v4l2_debugfs_root - returns the dentry of the top-level "v4l2" debugfs dir
  *

-- 
2.51.0
Re: [PATCH v2 04/10] media: v4l2-dev: Add helpers to get current format from the state
Posted by Hans Verkuil 1 week, 2 days ago
On 19/09/2025 11:55, Jai Luthra wrote:
> Add a helper function that drivers can call to retrieve the current
> v4l2_format stored in a video device state for internal use.
> 
> Additionally, provide a G_FMT ioctl implementation that drivers can use
> instead of writing their own when the format is stored in the active
> state.
> 
> Signed-off-by: Jai Luthra <jai.luthra@ideasonboard.com>
> --
> Cc: Mauro Carvalho Chehab <mchehab@kernel.org>
> Cc: Hans Verkuil <hverkuil@kernel.org>
> Cc: Jai Luthra <jai.luthra@ideasonboard.com>
> Cc: Ricardo Ribalda <ribalda@chromium.org>
> Cc: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> Cc: Ma Ke <make24@iscas.ac.cn>
> Cc: linux-media@vger.kernel.org
> Cc: linux-kernel@vger.kernel.org
> ---
>  drivers/media/v4l2-core/v4l2-dev.c | 23 +++++++++++++++++++++++
>  include/media/v4l2-dev.h           | 18 ++++++++++++++++++
>  2 files changed, 41 insertions(+)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-dev.c b/drivers/media/v4l2-core/v4l2-dev.c
> index dff23c6a0b56fb3d29e1c04e386bb445fa8773bb..2606077538be0e83032c6ae8956c1d67da0d0c5d 100644
> --- a/drivers/media/v4l2-core/v4l2-dev.c
> +++ b/drivers/media/v4l2-core/v4l2-dev.c
> @@ -199,6 +199,29 @@ void __video_device_state_free(struct video_device_state *state)
>  }
>  EXPORT_SYMBOL_GPL(__video_device_state_free);
>  
> +struct v4l2_format *video_device_state_get_fmt(struct video_device_state *state)
> +{
> +	if (WARN_ON_ONCE(!state))
> +		return NULL;
> +
> +	return &state->fmt;
> +}
> +EXPORT_SYMBOL_GPL(video_device_state_get_fmt);
> +
> +int video_device_g_fmt(struct file *file, void *priv, struct v4l2_format *fmt)
> +{
> +	struct video_device_state *state = priv;
> +	struct v4l2_format *vfmt = video_device_state_get_fmt(state);

Obviously, if multiple format types are stored in state (e.g. video capture and
output), then this code needs to pick the right format based on fmt->type.

I would also suggest moving this to v4l2-common.c and renaming it to v4l2_g_fmt,
consistent with other helpers there (v4l2_g/s_parm_cap).

Regards,

	Hans

> +
> +	if (!vfmt)
> +		return -EINVAL;
> +
> +	*fmt = *vfmt;
> +
> +	return 0;
> +}
> +EXPORT_SYMBOL_GPL(video_device_g_fmt);
> +
>  static inline void video_get(struct video_device *vdev)
>  {
>  	get_device(&vdev->dev);
> diff --git a/include/media/v4l2-dev.h b/include/media/v4l2-dev.h
> index d327be16f6def70554a7d92d10436a29384ae32a..b5312823fbff9c236d4394d48fa9a14412b17c68 100644
> --- a/include/media/v4l2-dev.h
> +++ b/include/media/v4l2-dev.h
> @@ -612,6 +612,24 @@ __video_device_state_alloc(struct video_device *vdev,
>   */
>  void __video_device_state_free(struct video_device_state *state);
>  
> +/**
> + * video_device_state_get_fmt - get current v4l2_format.
> + *
> + * @state: pointer to struct video_device_state
> + */
> +struct v4l2_format *
> +video_device_state_get_fmt(struct video_device_state *state);
> +
> +/**
> + * video_device_g_fmt - fill v4l2_format from the state.
> + *
> + * @file: pointer to struct file
> + * @state: pointer to struct video_device_state
> + * @format: pointer to struct v4l2_format
> + */
> +int video_device_g_fmt(struct file *file, void *priv,
> +		       struct v4l2_format *format);
> +
>  /**
>   * v4l2_debugfs_root - returns the dentry of the top-level "v4l2" debugfs dir
>   *
>
Re: [PATCH v2 04/10] media: v4l2-dev: Add helpers to get current format from the state
Posted by Jai Luthra 2 days, 15 hours ago
Quoting Hans Verkuil (2025-09-22 13:36:59)
> On 19/09/2025 11:55, Jai Luthra wrote:
> > Add a helper function that drivers can call to retrieve the current
> > v4l2_format stored in a video device state for internal use.
> > 
> > Additionally, provide a G_FMT ioctl implementation that drivers can use
> > instead of writing their own when the format is stored in the active
> > state.
> > 
> > Signed-off-by: Jai Luthra <jai.luthra@ideasonboard.com>
> > --
> > Cc: Mauro Carvalho Chehab <mchehab@kernel.org>
> > Cc: Hans Verkuil <hverkuil@kernel.org>
> > Cc: Jai Luthra <jai.luthra@ideasonboard.com>
> > Cc: Ricardo Ribalda <ribalda@chromium.org>
> > Cc: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> > Cc: Ma Ke <make24@iscas.ac.cn>
> > Cc: linux-media@vger.kernel.org
> > Cc: linux-kernel@vger.kernel.org
> > ---
> >  drivers/media/v4l2-core/v4l2-dev.c | 23 +++++++++++++++++++++++
> >  include/media/v4l2-dev.h           | 18 ++++++++++++++++++
> >  2 files changed, 41 insertions(+)
> > 
> > diff --git a/drivers/media/v4l2-core/v4l2-dev.c b/drivers/media/v4l2-core/v4l2-dev.c
> > index dff23c6a0b56fb3d29e1c04e386bb445fa8773bb..2606077538be0e83032c6ae8956c1d67da0d0c5d 100644
> > --- a/drivers/media/v4l2-core/v4l2-dev.c
> > +++ b/drivers/media/v4l2-core/v4l2-dev.c
> > @@ -199,6 +199,29 @@ void __video_device_state_free(struct video_device_state *state)
> >  }
> >  EXPORT_SYMBOL_GPL(__video_device_state_free);
> >  
> > +struct v4l2_format *video_device_state_get_fmt(struct video_device_state *state)
> > +{
> > +     if (WARN_ON_ONCE(!state))
> > +             return NULL;
> > +
> > +     return &state->fmt;
> > +}
> > +EXPORT_SYMBOL_GPL(video_device_state_get_fmt);
> > +
> > +int video_device_g_fmt(struct file *file, void *priv, struct v4l2_format *fmt)
> > +{
> > +     struct video_device_state *state = priv;
> > +     struct v4l2_format *vfmt = video_device_state_get_fmt(state);
> 
> Obviously, if multiple format types are stored in state (e.g. video capture and
> output), then this code needs to pick the right format based on fmt->type.
> 
> I would also suggest moving this to v4l2-common.c and renaming it to v4l2_g_fmt,
> consistent with other helpers there (v4l2_g/s_parm_cap).

Good catch, will fix in next revision.

> 
> Regards,
> 
>         Hans
> 
> > +
> > +     if (!vfmt)
> > +             return -EINVAL;
> > +
> > +     *fmt = *vfmt;
> > +
> > +     return 0;
> > +}
> > +EXPORT_SYMBOL_GPL(video_device_g_fmt);
> > +

Thanks,
Jai

[snip]