diff mbox series

media: i2c: ov5645: add HAS_EVENTS support

Message ID 20241014173840.412695-1-tomm.merciai@gmail.com (mailing list archive)
State New
Headers show
Series media: i2c: ov5645: add HAS_EVENTS support | expand

Commit Message

Tommaso Merciai Oct. 14, 2024, 5:38 p.m. UTC
Controls can be exposed to userspace via a v4l-subdevX device, and
userspace has to be able to subscribe to control events so that it is
notified when the control changes value.
Add missing HAS_EVENTS support: flag and .(un)subscribe_event().

Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
---
 drivers/media/i2c/ov5645.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

Comments

Laurent Pinchart Oct. 14, 2024, 5:54 p.m. UTC | #1
Hi Tommaso,

Thank you for the patch.

On Mon, Oct 14, 2024 at 07:38:40PM +0200, Tommaso Merciai wrote:
> Controls can be exposed to userspace via a v4l-subdevX device, and
> userspace has to be able to subscribe to control events so that it is
> notified when the control changes value.
> Add missing HAS_EVENTS support: flag and .(un)subscribe_event().
> 
> Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> ---
>  drivers/media/i2c/ov5645.c | 10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/media/i2c/ov5645.c b/drivers/media/i2c/ov5645.c
> index 0c32bd2940ec..2c5145d5c616 100644
> --- a/drivers/media/i2c/ov5645.c
> +++ b/drivers/media/i2c/ov5645.c
> @@ -29,6 +29,7 @@
>  #include <linux/slab.h>
>  #include <linux/types.h>
>  #include <media/v4l2-ctrls.h>
> +#include <media/v4l2-event.h>
>  #include <media/v4l2-fwnode.h>
>  #include <media/v4l2-subdev.h>
>  
> @@ -1034,6 +1035,11 @@ static const struct v4l2_subdev_video_ops ov5645_video_ops = {
>  	.s_stream = ov5645_s_stream,
>  };
>  
> +static const struct v4l2_subdev_core_ops ov5645_subdev_core_ops = {
> +	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
> +	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
> +};
> +
>  static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
>  	.enum_mbus_code = ov5645_enum_mbus_code,
>  	.enum_frame_size = ov5645_enum_frame_size,
> @@ -1043,6 +1049,7 @@ static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
>  };
>  
>  static const struct v4l2_subdev_ops ov5645_subdev_ops = {
> +	.core = &ov5645_subdev_core_ops,
>  	.video = &ov5645_video_ops,
>  	.pad = &ov5645_subdev_pad_ops,
>  };
> @@ -1178,7 +1185,8 @@ static int ov5645_probe(struct i2c_client *client)
>  
>  	v4l2_i2c_subdev_init(&ov5645->sd, client, &ov5645_subdev_ops);
>  	ov5645->sd.internal_ops = &ov5645_internal_ops;
> -	ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
> +	ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
> +			    V4L2_SUBDEV_FL_HAS_EVENTS;

Instead of patching every subdev driver, should we handle all of this in
the subdev core ? If a control handler is set for the subdev, we could
set the HAS_EVENTS flag automatically, and default to
v4l2_ctrl_subdev_subscribe_event() and v4l2_event_subdev_unsubscribe()
if there are no control operations.

>  	ov5645->pad.flags = MEDIA_PAD_FL_SOURCE;
>  	ov5645->sd.dev = &client->dev;
>  	ov5645->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
Tommaso Merciai Oct. 15, 2024, 6:16 a.m. UTC | #2
Hi Laurent,
Thanks for your review.

On Mon, Oct 14, 2024 at 08:54:52PM +0300, Laurent Pinchart wrote:
> Hi Tommaso,
> 
> Thank you for the patch.
> 
> On Mon, Oct 14, 2024 at 07:38:40PM +0200, Tommaso Merciai wrote:
> > Controls can be exposed to userspace via a v4l-subdevX device, and
> > userspace has to be able to subscribe to control events so that it is
> > notified when the control changes value.
> > Add missing HAS_EVENTS support: flag and .(un)subscribe_event().
> > 
> > Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> > ---
> >  drivers/media/i2c/ov5645.c | 10 +++++++++-
> >  1 file changed, 9 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/media/i2c/ov5645.c b/drivers/media/i2c/ov5645.c
> > index 0c32bd2940ec..2c5145d5c616 100644
> > --- a/drivers/media/i2c/ov5645.c
> > +++ b/drivers/media/i2c/ov5645.c
> > @@ -29,6 +29,7 @@
> >  #include <linux/slab.h>
> >  #include <linux/types.h>
> >  #include <media/v4l2-ctrls.h>
> > +#include <media/v4l2-event.h>
> >  #include <media/v4l2-fwnode.h>
> >  #include <media/v4l2-subdev.h>
> >  
> > @@ -1034,6 +1035,11 @@ static const struct v4l2_subdev_video_ops ov5645_video_ops = {
> >  	.s_stream = ov5645_s_stream,
> >  };
> >  
> > +static const struct v4l2_subdev_core_ops ov5645_subdev_core_ops = {
> > +	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
> > +	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
> > +};
> > +
> >  static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
> >  	.enum_mbus_code = ov5645_enum_mbus_code,
> >  	.enum_frame_size = ov5645_enum_frame_size,
> > @@ -1043,6 +1049,7 @@ static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
> >  };
> >  
> >  static const struct v4l2_subdev_ops ov5645_subdev_ops = {
> > +	.core = &ov5645_subdev_core_ops,
> >  	.video = &ov5645_video_ops,
> >  	.pad = &ov5645_subdev_pad_ops,
> >  };
> > @@ -1178,7 +1185,8 @@ static int ov5645_probe(struct i2c_client *client)
> >  
> >  	v4l2_i2c_subdev_init(&ov5645->sd, client, &ov5645_subdev_ops);
> >  	ov5645->sd.internal_ops = &ov5645_internal_ops;
> > -	ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
> > +	ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
> > +			    V4L2_SUBDEV_FL_HAS_EVENTS;
> 
> Instead of patching every subdev driver, should we handle all of this in
> the subdev core ? If a control handler is set for the subdev, we could
> set the HAS_EVENTS flag automatically, and default to
> v4l2_ctrl_subdev_subscribe_event() and v4l2_event_subdev_unsubscribe()
> if there are no control operations.

Well :)
Not every subdev drivers, but only the ones I'm testing.

Yesterday I was playing with ov5645 :) And I got:

v4l2-compliance -d /dev/v4l-subdev1

test VIDIOC_G/S/TRY_EXT_CTRLS: OK
fail: v4l2-test-controls.cpp(1108): subscribe event for control 'User Controls' failed

Joke apart fully agree and thanks for your hint!
I will take  a look :)

> 
> >  	ov5645->pad.flags = MEDIA_PAD_FL_SOURCE;
> >  	ov5645->sd.dev = &client->dev;
> >  	ov5645->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
> 
> -- 
> Regards,
> 
> Laurent Pinchart

Thanks & Regards,
Tommaso
Tommaso Merciai Oct. 15, 2024, 4:37 p.m. UTC | #3
Hi Laurent,
Back on the topic.

On Tue, Oct 15, 2024 at 08:16:13AM +0200, Tommaso Merciai wrote:
> Hi Laurent,
> Thanks for your review.
> 
> On Mon, Oct 14, 2024 at 08:54:52PM +0300, Laurent Pinchart wrote:
> > Hi Tommaso,
> > 
> > Thank you for the patch.
> > 
> > On Mon, Oct 14, 2024 at 07:38:40PM +0200, Tommaso Merciai wrote:
> > > Controls can be exposed to userspace via a v4l-subdevX device, and
> > > userspace has to be able to subscribe to control events so that it is
> > > notified when the control changes value.
> > > Add missing HAS_EVENTS support: flag and .(un)subscribe_event().
> > > 
> > > Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> > > ---
> > >  drivers/media/i2c/ov5645.c | 10 +++++++++-
> > >  1 file changed, 9 insertions(+), 1 deletion(-)
> > > 
> > > diff --git a/drivers/media/i2c/ov5645.c b/drivers/media/i2c/ov5645.c
> > > index 0c32bd2940ec..2c5145d5c616 100644
> > > --- a/drivers/media/i2c/ov5645.c
> > > +++ b/drivers/media/i2c/ov5645.c
> > > @@ -29,6 +29,7 @@
> > >  #include <linux/slab.h>
> > >  #include <linux/types.h>
> > >  #include <media/v4l2-ctrls.h>
> > > +#include <media/v4l2-event.h>
> > >  #include <media/v4l2-fwnode.h>
> > >  #include <media/v4l2-subdev.h>
> > >  
> > > @@ -1034,6 +1035,11 @@ static const struct v4l2_subdev_video_ops ov5645_video_ops = {
> > >  	.s_stream = ov5645_s_stream,
> > >  };
> > >  
> > > +static const struct v4l2_subdev_core_ops ov5645_subdev_core_ops = {
> > > +	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
> > > +	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
> > > +};
> > > +
> > >  static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
> > >  	.enum_mbus_code = ov5645_enum_mbus_code,
> > >  	.enum_frame_size = ov5645_enum_frame_size,
> > > @@ -1043,6 +1049,7 @@ static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
> > >  };
> > >  
> > >  static const struct v4l2_subdev_ops ov5645_subdev_ops = {
> > > +	.core = &ov5645_subdev_core_ops,
> > >  	.video = &ov5645_video_ops,
> > >  	.pad = &ov5645_subdev_pad_ops,
> > >  };
> > > @@ -1178,7 +1185,8 @@ static int ov5645_probe(struct i2c_client *client)
> > >  
> > >  	v4l2_i2c_subdev_init(&ov5645->sd, client, &ov5645_subdev_ops);
> > >  	ov5645->sd.internal_ops = &ov5645_internal_ops;
> > > -	ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
> > > +	ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
> > > +			    V4L2_SUBDEV_FL_HAS_EVENTS;
> > 
> > Instead of patching every subdev driver, should we handle all of this in
> > the subdev core ? If a control handler is set for the subdev, we could
> > set the HAS_EVENTS flag automatically, and default to
> > v4l2_ctrl_subdev_subscribe_event() and v4l2_event_subdev_unsubscribe()
> > if there are no control operations.

Premit:
 - Don't know if I'm wrong eh.

This can be done into:

__v4l2_subdev_init_finalize()

Adding:

	if (sd->ctrl_handler)
		sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;

And check if there are no control operations using:

bool has_subscribe_event;
bool has_unsubscribe_event;


has_subscribe_event = v4l2_subdev_has_op(sd, core, subscribe_event);
has_unsubscribe_event = v4l2_subdev_has_op(sd, core, unsubscribe_event);

if (!has_subscribe_event)
	assign v4l2_ctrl_subdev_subscribe_event as default .subscribe ops(somehow)

if (!has_unsubscribe_event)
	assign v4l2_event_subdev_unsubscribe as default .unsubscribe ops (somehow)


Or maybe v4l2_subdev_init_finalize() it's too late?
I'm completely wrong? What do you think?

Thanks in advance,
Tommaso

> 
> Well :)
> Not every subdev drivers, but only the ones I'm testing.
> 
> Yesterday I was playing with ov5645 :) And I got:
> 
> v4l2-compliance -d /dev/v4l-subdev1
> 
> test VIDIOC_G/S/TRY_EXT_CTRLS: OK
> fail: v4l2-test-controls.cpp(1108): subscribe event for control 'User Controls' failed
> 
> Joke apart fully agree and thanks for your hint!
> I will take  a look :)
> 
> > 
> > >  	ov5645->pad.flags = MEDIA_PAD_FL_SOURCE;
> > >  	ov5645->sd.dev = &client->dev;
> > >  	ov5645->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
> > 
> > -- 
> > Regards,
> > 
> > Laurent Pinchart
> 
> Thanks & Regards,
> Tommaso
Laurent Pinchart Oct. 16, 2024, 8:08 p.m. UTC | #4
Hi Tommaso,

On Tue, Oct 15, 2024 at 06:37:58PM +0200, Tommaso Merciai wrote:
> On Tue, Oct 15, 2024 at 08:16:13AM +0200, Tommaso Merciai wrote:
> > On Mon, Oct 14, 2024 at 08:54:52PM +0300, Laurent Pinchart wrote:
> > > On Mon, Oct 14, 2024 at 07:38:40PM +0200, Tommaso Merciai wrote:
> > > > Controls can be exposed to userspace via a v4l-subdevX device, and
> > > > userspace has to be able to subscribe to control events so that it is
> > > > notified when the control changes value.
> > > > Add missing HAS_EVENTS support: flag and .(un)subscribe_event().
> > > > 
> > > > Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> > > > ---
> > > >  drivers/media/i2c/ov5645.c | 10 +++++++++-
> > > >  1 file changed, 9 insertions(+), 1 deletion(-)
> > > > 
> > > > diff --git a/drivers/media/i2c/ov5645.c b/drivers/media/i2c/ov5645.c
> > > > index 0c32bd2940ec..2c5145d5c616 100644
> > > > --- a/drivers/media/i2c/ov5645.c
> > > > +++ b/drivers/media/i2c/ov5645.c
> > > > @@ -29,6 +29,7 @@
> > > >  #include <linux/slab.h>
> > > >  #include <linux/types.h>
> > > >  #include <media/v4l2-ctrls.h>
> > > > +#include <media/v4l2-event.h>
> > > >  #include <media/v4l2-fwnode.h>
> > > >  #include <media/v4l2-subdev.h>
> > > >  
> > > > @@ -1034,6 +1035,11 @@ static const struct v4l2_subdev_video_ops ov5645_video_ops = {
> > > >  	.s_stream = ov5645_s_stream,
> > > >  };
> > > >  
> > > > +static const struct v4l2_subdev_core_ops ov5645_subdev_core_ops = {
> > > > +	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
> > > > +	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
> > > > +};
> > > > +
> > > >  static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
> > > >  	.enum_mbus_code = ov5645_enum_mbus_code,
> > > >  	.enum_frame_size = ov5645_enum_frame_size,
> > > > @@ -1043,6 +1049,7 @@ static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
> > > >  };
> > > >  
> > > >  static const struct v4l2_subdev_ops ov5645_subdev_ops = {
> > > > +	.core = &ov5645_subdev_core_ops,
> > > >  	.video = &ov5645_video_ops,
> > > >  	.pad = &ov5645_subdev_pad_ops,
> > > >  };
> > > > @@ -1178,7 +1185,8 @@ static int ov5645_probe(struct i2c_client *client)
> > > >  
> > > >  	v4l2_i2c_subdev_init(&ov5645->sd, client, &ov5645_subdev_ops);
> > > >  	ov5645->sd.internal_ops = &ov5645_internal_ops;
> > > > -	ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
> > > > +	ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
> > > > +			    V4L2_SUBDEV_FL_HAS_EVENTS;
> > > 
> > > Instead of patching every subdev driver, should we handle all of this in
> > > the subdev core ? If a control handler is set for the subdev, we could
> > > set the HAS_EVENTS flag automatically, and default to
> > > v4l2_ctrl_subdev_subscribe_event() and v4l2_event_subdev_unsubscribe()
> > > if there are no control operations.
> 
> Premit:
>  - Don't know if I'm wrong eh.

Nobody knows :-)

> This can be done into:
> 
> __v4l2_subdev_init_finalize()
> 
> Adding:
> 
> 	if (sd->ctrl_handler)
> 		sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
> 
> And check if there are no control operations using:
> 
> bool has_subscribe_event;
> bool has_unsubscribe_event;
> 
> 
> has_subscribe_event = v4l2_subdev_has_op(sd, core, subscribe_event);
> has_unsubscribe_event = v4l2_subdev_has_op(sd, core, unsubscribe_event);
> 
> if (!has_subscribe_event)
> 	assign v4l2_ctrl_subdev_subscribe_event as default .subscribe ops(somehow)

We can't change the ops structure as it's constant. Something like this
could do:

diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
index 3a4ba08810d2..41ae18a0d41e 100644
--- a/drivers/media/v4l2-core/v4l2-subdev.c
+++ b/drivers/media/v4l2-core/v4l2-subdev.c
@@ -691,10 +691,24 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
 		return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);

 	case VIDIOC_SUBSCRIBE_EVENT:
-		return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
+		if (v4l2_subdev_has_op(sd, core, subscribe_event))
+			return v4l2_subdev_call(sd, core, subscribe_event, vfh,
+						arg);
+		else if ((sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) &&
+			 vfh->ctrl_handler)
+			return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);
+		else
+			return -ENOIOCTLCMD;

 	case VIDIOC_UNSUBSCRIBE_EVENT:
-		return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
+		if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
+			return -ENOIOCTLCMD;
+
+		if (v4l2_subdev_has_op(sd, core, unsubscribe_event))
+			return v4l2_subdev_call(sd, core, unsubscribe_event,
+						vfh, arg);
+		else
+			return v4l2_event_subdev_unsubscribe(sd, vfh, sub);

 #ifdef CONFIG_VIDEO_ADV_DEBUG
 	case VIDIOC_DBG_G_REGISTER:

> if (!has_unsubscribe_event)
> 	assign v4l2_event_subdev_unsubscribe as default .unsubscribe ops (somehow)
> 
> 
> Or maybe v4l2_subdev_init_finalize() it's too late?
> I'm completely wrong? What do you think?

I like v4l2_subdev_init_finalize() as we're pushing all subdev drivers
to use it, so it's an extra incentive.

> > Well :)
> > Not every subdev drivers, but only the ones I'm testing.
> > 
> > Yesterday I was playing with ov5645 :) And I got:
> > 
> > v4l2-compliance -d /dev/v4l-subdev1
> > 
> > test VIDIOC_G/S/TRY_EXT_CTRLS: OK
> > fail: v4l2-test-controls.cpp(1108): subscribe event for control 'User Controls' failed
> > 
> > Joke apart fully agree and thanks for your hint!
> > I will take  a look :)
> > 
> > > >  	ov5645->pad.flags = MEDIA_PAD_FL_SOURCE;
> > > >  	ov5645->sd.dev = &client->dev;
> > > >  	ov5645->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
Tommaso Merciai Oct. 18, 2024, 9:20 a.m. UTC | #5
Hi Laurent,

On Wed, Oct 16, 2024 at 11:08:36PM +0300, Laurent Pinchart wrote:
> Hi Tommaso,
> 
> On Tue, Oct 15, 2024 at 06:37:58PM +0200, Tommaso Merciai wrote:
> > On Tue, Oct 15, 2024 at 08:16:13AM +0200, Tommaso Merciai wrote:
> > > On Mon, Oct 14, 2024 at 08:54:52PM +0300, Laurent Pinchart wrote:
> > > > On Mon, Oct 14, 2024 at 07:38:40PM +0200, Tommaso Merciai wrote:
> > > > > Controls can be exposed to userspace via a v4l-subdevX device, and
> > > > > userspace has to be able to subscribe to control events so that it is
> > > > > notified when the control changes value.
> > > > > Add missing HAS_EVENTS support: flag and .(un)subscribe_event().
> > > > > 
> > > > > Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> > > > > ---
> > > > >  drivers/media/i2c/ov5645.c | 10 +++++++++-
> > > > >  1 file changed, 9 insertions(+), 1 deletion(-)
> > > > > 
> > > > > diff --git a/drivers/media/i2c/ov5645.c b/drivers/media/i2c/ov5645.c
> > > > > index 0c32bd2940ec..2c5145d5c616 100644
> > > > > --- a/drivers/media/i2c/ov5645.c
> > > > > +++ b/drivers/media/i2c/ov5645.c
> > > > > @@ -29,6 +29,7 @@
> > > > >  #include <linux/slab.h>
> > > > >  #include <linux/types.h>
> > > > >  #include <media/v4l2-ctrls.h>
> > > > > +#include <media/v4l2-event.h>
> > > > >  #include <media/v4l2-fwnode.h>
> > > > >  #include <media/v4l2-subdev.h>
> > > > >  
> > > > > @@ -1034,6 +1035,11 @@ static const struct v4l2_subdev_video_ops ov5645_video_ops = {
> > > > >  	.s_stream = ov5645_s_stream,
> > > > >  };
> > > > >  
> > > > > +static const struct v4l2_subdev_core_ops ov5645_subdev_core_ops = {
> > > > > +	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
> > > > > +	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
> > > > > +};
> > > > > +
> > > > >  static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
> > > > >  	.enum_mbus_code = ov5645_enum_mbus_code,
> > > > >  	.enum_frame_size = ov5645_enum_frame_size,
> > > > > @@ -1043,6 +1049,7 @@ static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
> > > > >  };
> > > > >  
> > > > >  static const struct v4l2_subdev_ops ov5645_subdev_ops = {
> > > > > +	.core = &ov5645_subdev_core_ops,
> > > > >  	.video = &ov5645_video_ops,
> > > > >  	.pad = &ov5645_subdev_pad_ops,
> > > > >  };
> > > > > @@ -1178,7 +1185,8 @@ static int ov5645_probe(struct i2c_client *client)
> > > > >  
> > > > >  	v4l2_i2c_subdev_init(&ov5645->sd, client, &ov5645_subdev_ops);
> > > > >  	ov5645->sd.internal_ops = &ov5645_internal_ops;
> > > > > -	ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
> > > > > +	ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
> > > > > +			    V4L2_SUBDEV_FL_HAS_EVENTS;
> > > > 
> > > > Instead of patching every subdev driver, should we handle all of this in
> > > > the subdev core ? If a control handler is set for the subdev, we could
> > > > set the HAS_EVENTS flag automatically, and default to
> > > > v4l2_ctrl_subdev_subscribe_event() and v4l2_event_subdev_unsubscribe()
> > > > if there are no control operations.
> > 
> > Premit:
> >  - Don't know if I'm wrong eh.
> 
> Nobody knows :-)
> 
> > This can be done into:
> > 
> > __v4l2_subdev_init_finalize()
> > 
> > Adding:
> > 
> > 	if (sd->ctrl_handler)
> > 		sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
> > 
> > And check if there are no control operations using:
> > 
> > bool has_subscribe_event;
> > bool has_unsubscribe_event;
> > 
> > 
> > has_subscribe_event = v4l2_subdev_has_op(sd, core, subscribe_event);
> > has_unsubscribe_event = v4l2_subdev_has_op(sd, core, unsubscribe_event);
> > 
> > if (!has_subscribe_event)
> > 	assign v4l2_ctrl_subdev_subscribe_event as default .subscribe ops(somehow)
> 
> We can't change the ops structure as it's constant. Something like this
> could do:
> 
> diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> index 3a4ba08810d2..41ae18a0d41e 100644
> --- a/drivers/media/v4l2-core/v4l2-subdev.c
> +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> @@ -691,10 +691,24 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
>  		return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
> 
>  	case VIDIOC_SUBSCRIBE_EVENT:
> -		return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
> +		if (v4l2_subdev_has_op(sd, core, subscribe_event))
> +			return v4l2_subdev_call(sd, core, subscribe_event, vfh,
> +						arg);
> +		else if ((sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) &&
> +			 vfh->ctrl_handler)
> +			return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);
> +		else
> +			return -ENOIOCTLCMD;
> 
>  	case VIDIOC_UNSUBSCRIBE_EVENT:
> -		return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
> +		if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
> +			return -ENOIOCTLCMD;
> +
> +		if (v4l2_subdev_has_op(sd, core, unsubscribe_event))
> +			return v4l2_subdev_call(sd, core, unsubscribe_event,
> +						vfh, arg);
> +		else
+			return v4l2_event_subdev_unsubscribe(sd, vfh, arg);

Thanks for your "more than an hint :)"
I'm able to test this on ov5645:

Adding:

+++ b/drivers/media/i2c/ov5645.c
@@ -1238,6 +1238,12 @@ static int ov5645_probe(struct i2c_client *client)
 
        ov5645_init_state(&ov5645->sd, NULL);
 
+       ret = v4l2_subdev_init_finalize(&ov5645->sd);
+       if (ret < 0) {
+               dev_err(dev, "subdev initialization error %d\n", ret);
+               goto err_free_state;
+       }
+
        ret = v4l2_async_register_subdev(&ov5645->sd);
        if (ret < 0) {
                dev_err(dev, "could not register v4l2 device\n");
@@ -1251,6 +1257,8 @@ static int ov5645_probe(struct i2c_client *client)
 
        return 0;
 
+err_free_state:
+       v4l2_subdev_cleanup(&ov5645->sd);
 err_pm_runtime:
        pm_runtime_disable(dev);
        pm_runtime_put_noidle(dev);
@@ -1272,6 +1280,7 @@ static void ov5645_remove(struct i2c_client *client)
 
        v4l2_async_unregister_subdev(&ov5645->sd);
        media_entity_cleanup(&ov5645->sd.entity);
+       v4l2_subdev_cleanup(&ov5645->sd);
        v4l2_ctrl_handler_free(&ov5645->ctrls);
        pm_runtime_disable(ov5645->dev);
        if (!pm_runtime_status_suspended(ov5645->dev))

Then from the compliance tool I'm getting now good results:

Total for device /dev/v4l-subdev1: 44, Succeeded: 44, Failed: 0, Warnings: 0

I will send these 2 patches later if you agree (1 v4l2-subdev 1 ov5645.c)
Thanks again.

Regards,
Tommaso

> 
>  #ifdef CONFIG_VIDEO_ADV_DEBUG
>  	case VIDIOC_DBG_G_REGISTER:
> 
> > if (!has_unsubscribe_event)
> > 	assign v4l2_event_subdev_unsubscribe as default .unsubscribe ops (somehow)
> > 
> > 
> > Or maybe v4l2_subdev_init_finalize() it's too late?
> > I'm completely wrong? What do you think?
> 
> I like v4l2_subdev_init_finalize() as we're pushing all subdev drivers
> to use it, so it's an extra incentive.
> 
> > > Well :)
> > > Not every subdev drivers, but only the ones I'm testing.
> > > 
> > > Yesterday I was playing with ov5645 :) And I got:
> > > 
> > > v4l2-compliance -d /dev/v4l-subdev1
> > > 
> > > test VIDIOC_G/S/TRY_EXT_CTRLS: OK
> > > fail: v4l2-test-controls.cpp(1108): subscribe event for control 'User Controls' failed
> > > 
> > > Joke apart fully agree and thanks for your hint!
> > > I will take  a look :)
> > > 
> > > > >  	ov5645->pad.flags = MEDIA_PAD_FL_SOURCE;
> > > > >  	ov5645->sd.dev = &client->dev;
> > > > >  	ov5645->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
> 
> -- 
> Regards,
> 
> Laurent Pinchart
Lad, Prabhakar Oct. 18, 2024, 9:57 a.m. UTC | #6
Hi Tommaso,

On Fri, Oct 18, 2024 at 10:28 AM Tommaso Merciai <tomm.merciai@gmail.com> wrote:
>
> Hi Laurent,
>
> On Wed, Oct 16, 2024 at 11:08:36PM +0300, Laurent Pinchart wrote:
> > Hi Tommaso,
> >
> > On Tue, Oct 15, 2024 at 06:37:58PM +0200, Tommaso Merciai wrote:
> > > On Tue, Oct 15, 2024 at 08:16:13AM +0200, Tommaso Merciai wrote:
> > > > On Mon, Oct 14, 2024 at 08:54:52PM +0300, Laurent Pinchart wrote:
> > > > > On Mon, Oct 14, 2024 at 07:38:40PM +0200, Tommaso Merciai wrote:
> > > > > > Controls can be exposed to userspace via a v4l-subdevX device, and
> > > > > > userspace has to be able to subscribe to control events so that it is
> > > > > > notified when the control changes value.
> > > > > > Add missing HAS_EVENTS support: flag and .(un)subscribe_event().
> > > > > >
> > > > > > Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> > > > > > ---
> > > > > >  drivers/media/i2c/ov5645.c | 10 +++++++++-
> > > > > >  1 file changed, 9 insertions(+), 1 deletion(-)
> > > > > >
> > > > > > diff --git a/drivers/media/i2c/ov5645.c b/drivers/media/i2c/ov5645.c
> > > > > > index 0c32bd2940ec..2c5145d5c616 100644
> > > > > > --- a/drivers/media/i2c/ov5645.c
> > > > > > +++ b/drivers/media/i2c/ov5645.c
> > > > > > @@ -29,6 +29,7 @@
> > > > > >  #include <linux/slab.h>
> > > > > >  #include <linux/types.h>
> > > > > >  #include <media/v4l2-ctrls.h>
> > > > > > +#include <media/v4l2-event.h>
> > > > > >  #include <media/v4l2-fwnode.h>
> > > > > >  #include <media/v4l2-subdev.h>
> > > > > >
> > > > > > @@ -1034,6 +1035,11 @@ static const struct v4l2_subdev_video_ops ov5645_video_ops = {
> > > > > >       .s_stream = ov5645_s_stream,
> > > > > >  };
> > > > > >
> > > > > > +static const struct v4l2_subdev_core_ops ov5645_subdev_core_ops = {
> > > > > > +     .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
> > > > > > +     .unsubscribe_event = v4l2_event_subdev_unsubscribe,
> > > > > > +};
> > > > > > +
> > > > > >  static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
> > > > > >       .enum_mbus_code = ov5645_enum_mbus_code,
> > > > > >       .enum_frame_size = ov5645_enum_frame_size,
> > > > > > @@ -1043,6 +1049,7 @@ static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
> > > > > >  };
> > > > > >
> > > > > >  static const struct v4l2_subdev_ops ov5645_subdev_ops = {
> > > > > > +     .core = &ov5645_subdev_core_ops,
> > > > > >       .video = &ov5645_video_ops,
> > > > > >       .pad = &ov5645_subdev_pad_ops,
> > > > > >  };
> > > > > > @@ -1178,7 +1185,8 @@ static int ov5645_probe(struct i2c_client *client)
> > > > > >
> > > > > >       v4l2_i2c_subdev_init(&ov5645->sd, client, &ov5645_subdev_ops);
> > > > > >       ov5645->sd.internal_ops = &ov5645_internal_ops;
> > > > > > -     ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
> > > > > > +     ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
> > > > > > +                         V4L2_SUBDEV_FL_HAS_EVENTS;
> > > > >
> > > > > Instead of patching every subdev driver, should we handle all of this in
> > > > > the subdev core ? If a control handler is set for the subdev, we could
> > > > > set the HAS_EVENTS flag automatically, and default to
> > > > > v4l2_ctrl_subdev_subscribe_event() and v4l2_event_subdev_unsubscribe()
> > > > > if there are no control operations.
> > >
> > > Premit:
> > >  - Don't know if I'm wrong eh.
> >
> > Nobody knows :-)
> >
> > > This can be done into:
> > >
> > > __v4l2_subdev_init_finalize()
> > >
> > > Adding:
> > >
> > >     if (sd->ctrl_handler)
> > >             sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
> > >
> > > And check if there are no control operations using:
> > >
> > > bool has_subscribe_event;
> > > bool has_unsubscribe_event;
> > >
> > >
> > > has_subscribe_event = v4l2_subdev_has_op(sd, core, subscribe_event);
> > > has_unsubscribe_event = v4l2_subdev_has_op(sd, core, unsubscribe_event);
> > >
> > > if (!has_subscribe_event)
> > >     assign v4l2_ctrl_subdev_subscribe_event as default .subscribe ops(somehow)
> >
> > We can't change the ops structure as it's constant. Something like this
> > could do:
> >
> > diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> > index 3a4ba08810d2..41ae18a0d41e 100644
> > --- a/drivers/media/v4l2-core/v4l2-subdev.c
> > +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> > @@ -691,10 +691,24 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
> >               return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
> >
> >       case VIDIOC_SUBSCRIBE_EVENT:
> > -             return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
> > +             if (v4l2_subdev_has_op(sd, core, subscribe_event))
> > +                     return v4l2_subdev_call(sd, core, subscribe_event, vfh,
> > +                                             arg);
> > +             else if ((sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) &&
> > +                      vfh->ctrl_handler)
> > +                     return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);
> > +             else
> > +                     return -ENOIOCTLCMD;
> >
> >       case VIDIOC_UNSUBSCRIBE_EVENT:
> > -             return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
> > +             if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
> > +                     return -ENOIOCTLCMD;
> > +
> > +             if (v4l2_subdev_has_op(sd, core, unsubscribe_event))
> > +                     return v4l2_subdev_call(sd, core, unsubscribe_event,
> > +                                             vfh, arg);
> > +             else
> +                       return v4l2_event_subdev_unsubscribe(sd, vfh, arg);
>
> Thanks for your "more than an hint :)"
> I'm able to test this on ov5645:
>
> Adding:
>
> +++ b/drivers/media/i2c/ov5645.c
> @@ -1238,6 +1238,12 @@ static int ov5645_probe(struct i2c_client *client)
>
>         ov5645_init_state(&ov5645->sd, NULL);
>
> +       ret = v4l2_subdev_init_finalize(&ov5645->sd);
> +       if (ret < 0) {
> +               dev_err(dev, "subdev initialization error %d\n", ret);
> +               goto err_free_state;
> +       }
> +
>         ret = v4l2_async_register_subdev(&ov5645->sd);
>         if (ret < 0) {
>                 dev_err(dev, "could not register v4l2 device\n");
> @@ -1251,6 +1257,8 @@ static int ov5645_probe(struct i2c_client *client)
>
>         return 0;
>
> +err_free_state:
> +       v4l2_subdev_cleanup(&ov5645->sd);
>  err_pm_runtime:
>         pm_runtime_disable(dev);
>         pm_runtime_put_noidle(dev);
> @@ -1272,6 +1280,7 @@ static void ov5645_remove(struct i2c_client *client)
>
>         v4l2_async_unregister_subdev(&ov5645->sd);
>         media_entity_cleanup(&ov5645->sd.entity);
> +       v4l2_subdev_cleanup(&ov5645->sd);
>         v4l2_ctrl_handler_free(&ov5645->ctrls);
>         pm_runtime_disable(ov5645->dev);
>         if (!pm_runtime_status_suspended(ov5645->dev))
>
> Then from the compliance tool I'm getting now good results:
>
> Total for device /dev/v4l-subdev1: 44, Succeeded: 44, Failed: 0, Warnings: 0
>
> I will send these 2 patches later if you agree (1 v4l2-subdev 1 ov5645.c)
> Thanks again.
>
Thank you for the patch.

I am currently working on adding support for V4L2_SUBDEV_FL_HAS_EVENTS
and subscribe hooks[1] (and some more features [0] for ov5645 driver),
since the patch series adds internal pad which needs rework based on
patch series from Sakari which I will do soon and send v3 patches for
ov5645 driver.

[0] https://lore.kernel.org/all/20240910170610.226189-1-prabhakar.mahadev-lad.rj@bp.renesas.com/
[1] https://lore.kernel.org/all/20240910170610.226189-2-prabhakar.mahadev-lad.rj@bp.renesas.com/

Cheers,
Prabhakar
Tommaso Merciai Oct. 18, 2024, 10:18 a.m. UTC | #7
Hi Prabhakar,
Thanks for your comments.

On Fri, Oct 18, 2024 at 10:57:49AM +0100, Lad, Prabhakar wrote:
> Hi Tommaso,
> 
> On Fri, Oct 18, 2024 at 10:28 AM Tommaso Merciai <tomm.merciai@gmail.com> wrote:
> >
> > Hi Laurent,
> >
> > On Wed, Oct 16, 2024 at 11:08:36PM +0300, Laurent Pinchart wrote:
> > > Hi Tommaso,
> > >
> > > On Tue, Oct 15, 2024 at 06:37:58PM +0200, Tommaso Merciai wrote:
> > > > On Tue, Oct 15, 2024 at 08:16:13AM +0200, Tommaso Merciai wrote:
> > > > > On Mon, Oct 14, 2024 at 08:54:52PM +0300, Laurent Pinchart wrote:
> > > > > > On Mon, Oct 14, 2024 at 07:38:40PM +0200, Tommaso Merciai wrote:
> > > > > > > Controls can be exposed to userspace via a v4l-subdevX device, and
> > > > > > > userspace has to be able to subscribe to control events so that it is
> > > > > > > notified when the control changes value.
> > > > > > > Add missing HAS_EVENTS support: flag and .(un)subscribe_event().
> > > > > > >
> > > > > > > Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> > > > > > > ---
> > > > > > >  drivers/media/i2c/ov5645.c | 10 +++++++++-
> > > > > > >  1 file changed, 9 insertions(+), 1 deletion(-)
> > > > > > >
> > > > > > > diff --git a/drivers/media/i2c/ov5645.c b/drivers/media/i2c/ov5645.c
> > > > > > > index 0c32bd2940ec..2c5145d5c616 100644
> > > > > > > --- a/drivers/media/i2c/ov5645.c
> > > > > > > +++ b/drivers/media/i2c/ov5645.c
> > > > > > > @@ -29,6 +29,7 @@
> > > > > > >  #include <linux/slab.h>
> > > > > > >  #include <linux/types.h>
> > > > > > >  #include <media/v4l2-ctrls.h>
> > > > > > > +#include <media/v4l2-event.h>
> > > > > > >  #include <media/v4l2-fwnode.h>
> > > > > > >  #include <media/v4l2-subdev.h>
> > > > > > >
> > > > > > > @@ -1034,6 +1035,11 @@ static const struct v4l2_subdev_video_ops ov5645_video_ops = {
> > > > > > >       .s_stream = ov5645_s_stream,
> > > > > > >  };
> > > > > > >
> > > > > > > +static const struct v4l2_subdev_core_ops ov5645_subdev_core_ops = {
> > > > > > > +     .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
> > > > > > > +     .unsubscribe_event = v4l2_event_subdev_unsubscribe,
> > > > > > > +};
> > > > > > > +
> > > > > > >  static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
> > > > > > >       .enum_mbus_code = ov5645_enum_mbus_code,
> > > > > > >       .enum_frame_size = ov5645_enum_frame_size,
> > > > > > > @@ -1043,6 +1049,7 @@ static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
> > > > > > >  };
> > > > > > >
> > > > > > >  static const struct v4l2_subdev_ops ov5645_subdev_ops = {
> > > > > > > +     .core = &ov5645_subdev_core_ops,
> > > > > > >       .video = &ov5645_video_ops,
> > > > > > >       .pad = &ov5645_subdev_pad_ops,
> > > > > > >  };
> > > > > > > @@ -1178,7 +1185,8 @@ static int ov5645_probe(struct i2c_client *client)
> > > > > > >
> > > > > > >       v4l2_i2c_subdev_init(&ov5645->sd, client, &ov5645_subdev_ops);
> > > > > > >       ov5645->sd.internal_ops = &ov5645_internal_ops;
> > > > > > > -     ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
> > > > > > > +     ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
> > > > > > > +                         V4L2_SUBDEV_FL_HAS_EVENTS;
> > > > > >
> > > > > > Instead of patching every subdev driver, should we handle all of this in
> > > > > > the subdev core ? If a control handler is set for the subdev, we could
> > > > > > set the HAS_EVENTS flag automatically, and default to
> > > > > > v4l2_ctrl_subdev_subscribe_event() and v4l2_event_subdev_unsubscribe()
> > > > > > if there are no control operations.
> > > >
> > > > Premit:
> > > >  - Don't know if I'm wrong eh.
> > >
> > > Nobody knows :-)
> > >
> > > > This can be done into:
> > > >
> > > > __v4l2_subdev_init_finalize()
> > > >
> > > > Adding:
> > > >
> > > >     if (sd->ctrl_handler)
> > > >             sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
> > > >
> > > > And check if there are no control operations using:
> > > >
> > > > bool has_subscribe_event;
> > > > bool has_unsubscribe_event;
> > > >
> > > >
> > > > has_subscribe_event = v4l2_subdev_has_op(sd, core, subscribe_event);
> > > > has_unsubscribe_event = v4l2_subdev_has_op(sd, core, unsubscribe_event);
> > > >
> > > > if (!has_subscribe_event)
> > > >     assign v4l2_ctrl_subdev_subscribe_event as default .subscribe ops(somehow)
> > >
> > > We can't change the ops structure as it's constant. Something like this
> > > could do:
> > >
> > > diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> > > index 3a4ba08810d2..41ae18a0d41e 100644
> > > --- a/drivers/media/v4l2-core/v4l2-subdev.c
> > > +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> > > @@ -691,10 +691,24 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
> > >               return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
> > >
> > >       case VIDIOC_SUBSCRIBE_EVENT:
> > > -             return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
> > > +             if (v4l2_subdev_has_op(sd, core, subscribe_event))
> > > +                     return v4l2_subdev_call(sd, core, subscribe_event, vfh,
> > > +                                             arg);
> > > +             else if ((sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) &&
> > > +                      vfh->ctrl_handler)
> > > +                     return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);
> > > +             else
> > > +                     return -ENOIOCTLCMD;
> > >
> > >       case VIDIOC_UNSUBSCRIBE_EVENT:
> > > -             return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
> > > +             if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
> > > +                     return -ENOIOCTLCMD;
> > > +
> > > +             if (v4l2_subdev_has_op(sd, core, unsubscribe_event))
> > > +                     return v4l2_subdev_call(sd, core, unsubscribe_event,
> > > +                                             vfh, arg);
> > > +             else
> > +                       return v4l2_event_subdev_unsubscribe(sd, vfh, arg);
> >
> > Thanks for your "more than an hint :)"
> > I'm able to test this on ov5645:
> >
> > Adding:
> >
> > +++ b/drivers/media/i2c/ov5645.c
> > @@ -1238,6 +1238,12 @@ static int ov5645_probe(struct i2c_client *client)
> >
> >         ov5645_init_state(&ov5645->sd, NULL);
> >
> > +       ret = v4l2_subdev_init_finalize(&ov5645->sd);
> > +       if (ret < 0) {
> > +               dev_err(dev, "subdev initialization error %d\n", ret);
> > +               goto err_free_state;
> > +       }
> > +
> >         ret = v4l2_async_register_subdev(&ov5645->sd);
> >         if (ret < 0) {
> >                 dev_err(dev, "could not register v4l2 device\n");
> > @@ -1251,6 +1257,8 @@ static int ov5645_probe(struct i2c_client *client)
> >
> >         return 0;
> >
> > +err_free_state:
> > +       v4l2_subdev_cleanup(&ov5645->sd);
> >  err_pm_runtime:
> >         pm_runtime_disable(dev);
> >         pm_runtime_put_noidle(dev);
> > @@ -1272,6 +1280,7 @@ static void ov5645_remove(struct i2c_client *client)
> >
> >         v4l2_async_unregister_subdev(&ov5645->sd);
> >         media_entity_cleanup(&ov5645->sd.entity);
> > +       v4l2_subdev_cleanup(&ov5645->sd);
> >         v4l2_ctrl_handler_free(&ov5645->ctrls);
> >         pm_runtime_disable(ov5645->dev);
> >         if (!pm_runtime_status_suspended(ov5645->dev))
> >
> > Then from the compliance tool I'm getting now good results:
> >
> > Total for device /dev/v4l-subdev1: 44, Succeeded: 44, Failed: 0, Warnings: 0
> >
> > I will send these 2 patches later if you agree (1 v4l2-subdev 1 ov5645.c)
> > Thanks again.
> >
> Thank you for the patch.
> 
> I am currently working on adding support for V4L2_SUBDEV_FL_HAS_EVENTS
> and subscribe hooks[1] (and some more features [0] for ov5645 driver),
> since the patch series adds internal pad which needs rework based on
> patch series from Sakari which I will do soon and send v3 patches for
> ov5645 driver.
> 
> [0] https://lore.kernel.org/all/20240910170610.226189-1-prabhakar.mahadev-lad.rj@bp.renesas.com/
> [1] https://lore.kernel.org/all/20240910170610.226189-2-prabhakar.mahadev-lad.rj@bp.renesas.com/

Thanks for sharing this.
Nice! then I can drop ov5645 patch and send only v4l2-subdev patch :)

Plan as suggested by Laurent is:

"Instead of patching every subdev driver, should we handle all of this in
the subdev core ? If a control handler is set for the subdev, we could
set the HAS_EVENTS flag automatically, and default to
v4l2_ctrl_subdev_subscribe_event() and v4l2_event_subdev_unsubscribe()
if there are no control operations"

Using:

diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
index 3a4ba08810d2..fe584998f5e6 100644
--- a/drivers/media/v4l2-core/v4l2-subdev.c
+++ b/drivers/media/v4l2-core/v4l2-subdev.c
@@ -691,10 +691,24 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
                return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);

        case VIDIOC_SUBSCRIBE_EVENT:
-               return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
+               if (v4l2_subdev_has_op(sd, core, subscribe_event))
+                       return v4l2_subdev_call(sd, core, subscribe_event,
+                                               vfh, arg);
+               else if ((sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) &&
+                       vfh->ctrl_handler)
+                       return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);
+               else
+                       return -ENOIOCTLCMD;

        case VIDIOC_UNSUBSCRIBE_EVENT:
-               return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
+               if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
+                       return -ENOIOCTLCMD;
+
+               if (v4l2_subdev_has_op(sd, core, unsubscribe_event))
+                       return v4l2_subdev_call(sd, core, unsubscribe_event,
+                                               vfh, arg);
+               else
+                       return v4l2_event_subdev_unsubscribe(sd, vfh, arg);

 #ifdef CONFIG_VIDEO_ADV_DEBUG
        case VIDIOC_DBG_G_REGISTER:
@@ -1641,6 +1655,9 @@ int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
                }
        }

+       if (sd->ctrl_handler)
+               sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
+
        state = __v4l2_subdev_state_alloc(sd, name, key);
        if (IS_ERR(state))
                return PTR_ERR(state);

In theory v4l2_subdev_init_finalize() and subdev_do_ioctl() will handle
this.

Please Laurent correct me if I'm wrong.
Prabhakar what do you think?
Thanks in advance.


Regards,
Tommaso

> 
> Cheers,
> Prabhakar
Sakari Ailus Oct. 18, 2024, 10:33 a.m. UTC | #8
Hi folks,

On Fri, Oct 18, 2024 at 12:18:15PM +0200, Tommaso Merciai wrote:
> Hi Prabhakar,
> Thanks for your comments.
> 
> On Fri, Oct 18, 2024 at 10:57:49AM +0100, Lad, Prabhakar wrote:
> > Hi Tommaso,
> > 
> > On Fri, Oct 18, 2024 at 10:28 AM Tommaso Merciai <tomm.merciai@gmail.com> wrote:
> > >
> > > Hi Laurent,
> > >
> > > On Wed, Oct 16, 2024 at 11:08:36PM +0300, Laurent Pinchart wrote:
> > > > Hi Tommaso,
> > > >
> > > > On Tue, Oct 15, 2024 at 06:37:58PM +0200, Tommaso Merciai wrote:
> > > > > On Tue, Oct 15, 2024 at 08:16:13AM +0200, Tommaso Merciai wrote:
> > > > > > On Mon, Oct 14, 2024 at 08:54:52PM +0300, Laurent Pinchart wrote:
> > > > > > > On Mon, Oct 14, 2024 at 07:38:40PM +0200, Tommaso Merciai wrote:
> > > > > > > > Controls can be exposed to userspace via a v4l-subdevX device, and
> > > > > > > > userspace has to be able to subscribe to control events so that it is
> > > > > > > > notified when the control changes value.
> > > > > > > > Add missing HAS_EVENTS support: flag and .(un)subscribe_event().
> > > > > > > >
> > > > > > > > Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> > > > > > > > ---
> > > > > > > >  drivers/media/i2c/ov5645.c | 10 +++++++++-
> > > > > > > >  1 file changed, 9 insertions(+), 1 deletion(-)
> > > > > > > >
> > > > > > > > diff --git a/drivers/media/i2c/ov5645.c b/drivers/media/i2c/ov5645.c
> > > > > > > > index 0c32bd2940ec..2c5145d5c616 100644
> > > > > > > > --- a/drivers/media/i2c/ov5645.c
> > > > > > > > +++ b/drivers/media/i2c/ov5645.c
> > > > > > > > @@ -29,6 +29,7 @@
> > > > > > > >  #include <linux/slab.h>
> > > > > > > >  #include <linux/types.h>
> > > > > > > >  #include <media/v4l2-ctrls.h>
> > > > > > > > +#include <media/v4l2-event.h>
> > > > > > > >  #include <media/v4l2-fwnode.h>
> > > > > > > >  #include <media/v4l2-subdev.h>
> > > > > > > >
> > > > > > > > @@ -1034,6 +1035,11 @@ static const struct v4l2_subdev_video_ops ov5645_video_ops = {
> > > > > > > >       .s_stream = ov5645_s_stream,
> > > > > > > >  };
> > > > > > > >
> > > > > > > > +static const struct v4l2_subdev_core_ops ov5645_subdev_core_ops = {
> > > > > > > > +     .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
> > > > > > > > +     .unsubscribe_event = v4l2_event_subdev_unsubscribe,
> > > > > > > > +};
> > > > > > > > +
> > > > > > > >  static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
> > > > > > > >       .enum_mbus_code = ov5645_enum_mbus_code,
> > > > > > > >       .enum_frame_size = ov5645_enum_frame_size,
> > > > > > > > @@ -1043,6 +1049,7 @@ static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
> > > > > > > >  };
> > > > > > > >
> > > > > > > >  static const struct v4l2_subdev_ops ov5645_subdev_ops = {
> > > > > > > > +     .core = &ov5645_subdev_core_ops,
> > > > > > > >       .video = &ov5645_video_ops,
> > > > > > > >       .pad = &ov5645_subdev_pad_ops,
> > > > > > > >  };
> > > > > > > > @@ -1178,7 +1185,8 @@ static int ov5645_probe(struct i2c_client *client)
> > > > > > > >
> > > > > > > >       v4l2_i2c_subdev_init(&ov5645->sd, client, &ov5645_subdev_ops);
> > > > > > > >       ov5645->sd.internal_ops = &ov5645_internal_ops;
> > > > > > > > -     ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
> > > > > > > > +     ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
> > > > > > > > +                         V4L2_SUBDEV_FL_HAS_EVENTS;
> > > > > > >
> > > > > > > Instead of patching every subdev driver, should we handle all of this in
> > > > > > > the subdev core ? If a control handler is set for the subdev, we could
> > > > > > > set the HAS_EVENTS flag automatically, and default to
> > > > > > > v4l2_ctrl_subdev_subscribe_event() and v4l2_event_subdev_unsubscribe()
> > > > > > > if there are no control operations.
> > > > >
> > > > > Premit:
> > > > >  - Don't know if I'm wrong eh.
> > > >
> > > > Nobody knows :-)
> > > >
> > > > > This can be done into:
> > > > >
> > > > > __v4l2_subdev_init_finalize()
> > > > >
> > > > > Adding:
> > > > >
> > > > >     if (sd->ctrl_handler)
> > > > >             sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
> > > > >
> > > > > And check if there are no control operations using:
> > > > >
> > > > > bool has_subscribe_event;
> > > > > bool has_unsubscribe_event;
> > > > >
> > > > >
> > > > > has_subscribe_event = v4l2_subdev_has_op(sd, core, subscribe_event);
> > > > > has_unsubscribe_event = v4l2_subdev_has_op(sd, core, unsubscribe_event);
> > > > >
> > > > > if (!has_subscribe_event)
> > > > >     assign v4l2_ctrl_subdev_subscribe_event as default .subscribe ops(somehow)
> > > >
> > > > We can't change the ops structure as it's constant. Something like this
> > > > could do:
> > > >
> > > > diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> > > > index 3a4ba08810d2..41ae18a0d41e 100644
> > > > --- a/drivers/media/v4l2-core/v4l2-subdev.c
> > > > +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> > > > @@ -691,10 +691,24 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
> > > >               return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
> > > >
> > > >       case VIDIOC_SUBSCRIBE_EVENT:
> > > > -             return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
> > > > +             if (v4l2_subdev_has_op(sd, core, subscribe_event))
> > > > +                     return v4l2_subdev_call(sd, core, subscribe_event, vfh,
> > > > +                                             arg);
> > > > +             else if ((sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) &&
> > > > +                      vfh->ctrl_handler)
> > > > +                     return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);
> > > > +             else
> > > > +                     return -ENOIOCTLCMD;
> > > >
> > > >       case VIDIOC_UNSUBSCRIBE_EVENT:
> > > > -             return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
> > > > +             if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
> > > > +                     return -ENOIOCTLCMD;
> > > > +
> > > > +             if (v4l2_subdev_has_op(sd, core, unsubscribe_event))
> > > > +                     return v4l2_subdev_call(sd, core, unsubscribe_event,
> > > > +                                             vfh, arg);
> > > > +             else
> > > +                       return v4l2_event_subdev_unsubscribe(sd, vfh, arg);
> > >
> > > Thanks for your "more than an hint :)"
> > > I'm able to test this on ov5645:
> > >
> > > Adding:
> > >
> > > +++ b/drivers/media/i2c/ov5645.c
> > > @@ -1238,6 +1238,12 @@ static int ov5645_probe(struct i2c_client *client)
> > >
> > >         ov5645_init_state(&ov5645->sd, NULL);
> > >
> > > +       ret = v4l2_subdev_init_finalize(&ov5645->sd);
> > > +       if (ret < 0) {
> > > +               dev_err(dev, "subdev initialization error %d\n", ret);
> > > +               goto err_free_state;
> > > +       }
> > > +
> > >         ret = v4l2_async_register_subdev(&ov5645->sd);
> > >         if (ret < 0) {
> > >                 dev_err(dev, "could not register v4l2 device\n");
> > > @@ -1251,6 +1257,8 @@ static int ov5645_probe(struct i2c_client *client)
> > >
> > >         return 0;
> > >
> > > +err_free_state:
> > > +       v4l2_subdev_cleanup(&ov5645->sd);
> > >  err_pm_runtime:
> > >         pm_runtime_disable(dev);
> > >         pm_runtime_put_noidle(dev);
> > > @@ -1272,6 +1280,7 @@ static void ov5645_remove(struct i2c_client *client)
> > >
> > >         v4l2_async_unregister_subdev(&ov5645->sd);
> > >         media_entity_cleanup(&ov5645->sd.entity);
> > > +       v4l2_subdev_cleanup(&ov5645->sd);
> > >         v4l2_ctrl_handler_free(&ov5645->ctrls);
> > >         pm_runtime_disable(ov5645->dev);
> > >         if (!pm_runtime_status_suspended(ov5645->dev))
> > >
> > > Then from the compliance tool I'm getting now good results:
> > >
> > > Total for device /dev/v4l-subdev1: 44, Succeeded: 44, Failed: 0, Warnings: 0
> > >
> > > I will send these 2 patches later if you agree (1 v4l2-subdev 1 ov5645.c)
> > > Thanks again.
> > >
> > Thank you for the patch.
> > 
> > I am currently working on adding support for V4L2_SUBDEV_FL_HAS_EVENTS
> > and subscribe hooks[1] (and some more features [0] for ov5645 driver),
> > since the patch series adds internal pad which needs rework based on
> > patch series from Sakari which I will do soon and send v3 patches for
> > ov5645 driver.
> > 
> > [0] https://lore.kernel.org/all/20240910170610.226189-1-prabhakar.mahadev-lad.rj@bp.renesas.com/
> > [1] https://lore.kernel.org/all/20240910170610.226189-2-prabhakar.mahadev-lad.rj@bp.renesas.com/
> 
> Thanks for sharing this.
> Nice! then I can drop ov5645 patch and send only v4l2-subdev patch :)
> 
> Plan as suggested by Laurent is:
> 
> "Instead of patching every subdev driver, should we handle all of this in
> the subdev core ? If a control handler is set for the subdev, we could
> set the HAS_EVENTS flag automatically, and default to
> v4l2_ctrl_subdev_subscribe_event() and v4l2_event_subdev_unsubscribe()
> if there are no control operations"
> 
> Using:
> 
> diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> index 3a4ba08810d2..fe584998f5e6 100644
> --- a/drivers/media/v4l2-core/v4l2-subdev.c
> +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> @@ -691,10 +691,24 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
>                 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
> 
>         case VIDIOC_SUBSCRIBE_EVENT:
> -               return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
> +               if (v4l2_subdev_has_op(sd, core, subscribe_event))
> +                       return v4l2_subdev_call(sd, core, subscribe_event,
> +                                               vfh, arg);
> +               else if ((sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) &&

Useless use of else. Same below.

> +                       vfh->ctrl_handler)
> +                       return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);
> +               else
> +                       return -ENOIOCTLCMD;
> 
>         case VIDIOC_UNSUBSCRIBE_EVENT:
> -               return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
> +               if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
> +                       return -ENOIOCTLCMD;
> +
> +               if (v4l2_subdev_has_op(sd, core, unsubscribe_event))
> +                       return v4l2_subdev_call(sd, core, unsubscribe_event,
> +                                               vfh, arg);
> +               else
> +                       return v4l2_event_subdev_unsubscribe(sd, vfh, arg);
> 
>  #ifdef CONFIG_VIDEO_ADV_DEBUG
>         case VIDIOC_DBG_G_REGISTER:
> @@ -1641,6 +1655,9 @@ int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
>                 }
>         }
> 
> +       if (sd->ctrl_handler)
> +               sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
> +
>         state = __v4l2_subdev_state_alloc(sd, name, key);
>         if (IS_ERR(state))
>                 return PTR_ERR(state);
> 
> In theory v4l2_subdev_init_finalize() and subdev_do_ioctl() will handle
> this.
> 
> Please Laurent correct me if I'm wrong.
> Prabhakar what do you think?
> Thanks in advance.

Looks good to me, with the above comment.
Tommaso Merciai Oct. 18, 2024, 10:48 a.m. UTC | #9
Hi Sakari,
Thanks for your review.

On Fri, Oct 18, 2024 at 10:33:56AM +0000, Sakari Ailus wrote:
> Hi folks,
> 
> On Fri, Oct 18, 2024 at 12:18:15PM +0200, Tommaso Merciai wrote:
> > Hi Prabhakar,
> > Thanks for your comments.
> > 
> > On Fri, Oct 18, 2024 at 10:57:49AM +0100, Lad, Prabhakar wrote:
> > > Hi Tommaso,
> > > 
> > > On Fri, Oct 18, 2024 at 10:28 AM Tommaso Merciai <tomm.merciai@gmail.com> wrote:
> > > >
> > > > Hi Laurent,
> > > >
> > > > On Wed, Oct 16, 2024 at 11:08:36PM +0300, Laurent Pinchart wrote:
> > > > > Hi Tommaso,
> > > > >
> > > > > On Tue, Oct 15, 2024 at 06:37:58PM +0200, Tommaso Merciai wrote:
> > > > > > On Tue, Oct 15, 2024 at 08:16:13AM +0200, Tommaso Merciai wrote:
> > > > > > > On Mon, Oct 14, 2024 at 08:54:52PM +0300, Laurent Pinchart wrote:
> > > > > > > > On Mon, Oct 14, 2024 at 07:38:40PM +0200, Tommaso Merciai wrote:
> > > > > > > > > Controls can be exposed to userspace via a v4l-subdevX device, and
> > > > > > > > > userspace has to be able to subscribe to control events so that it is
> > > > > > > > > notified when the control changes value.
> > > > > > > > > Add missing HAS_EVENTS support: flag and .(un)subscribe_event().
> > > > > > > > >
> > > > > > > > > Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> > > > > > > > > ---
> > > > > > > > >  drivers/media/i2c/ov5645.c | 10 +++++++++-
> > > > > > > > >  1 file changed, 9 insertions(+), 1 deletion(-)
> > > > > > > > >
> > > > > > > > > diff --git a/drivers/media/i2c/ov5645.c b/drivers/media/i2c/ov5645.c
> > > > > > > > > index 0c32bd2940ec..2c5145d5c616 100644
> > > > > > > > > --- a/drivers/media/i2c/ov5645.c
> > > > > > > > > +++ b/drivers/media/i2c/ov5645.c
> > > > > > > > > @@ -29,6 +29,7 @@
> > > > > > > > >  #include <linux/slab.h>
> > > > > > > > >  #include <linux/types.h>
> > > > > > > > >  #include <media/v4l2-ctrls.h>
> > > > > > > > > +#include <media/v4l2-event.h>
> > > > > > > > >  #include <media/v4l2-fwnode.h>
> > > > > > > > >  #include <media/v4l2-subdev.h>
> > > > > > > > >
> > > > > > > > > @@ -1034,6 +1035,11 @@ static const struct v4l2_subdev_video_ops ov5645_video_ops = {
> > > > > > > > >       .s_stream = ov5645_s_stream,
> > > > > > > > >  };
> > > > > > > > >
> > > > > > > > > +static const struct v4l2_subdev_core_ops ov5645_subdev_core_ops = {
> > > > > > > > > +     .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
> > > > > > > > > +     .unsubscribe_event = v4l2_event_subdev_unsubscribe,
> > > > > > > > > +};
> > > > > > > > > +
> > > > > > > > >  static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
> > > > > > > > >       .enum_mbus_code = ov5645_enum_mbus_code,
> > > > > > > > >       .enum_frame_size = ov5645_enum_frame_size,
> > > > > > > > > @@ -1043,6 +1049,7 @@ static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
> > > > > > > > >  };
> > > > > > > > >
> > > > > > > > >  static const struct v4l2_subdev_ops ov5645_subdev_ops = {
> > > > > > > > > +     .core = &ov5645_subdev_core_ops,
> > > > > > > > >       .video = &ov5645_video_ops,
> > > > > > > > >       .pad = &ov5645_subdev_pad_ops,
> > > > > > > > >  };
> > > > > > > > > @@ -1178,7 +1185,8 @@ static int ov5645_probe(struct i2c_client *client)
> > > > > > > > >
> > > > > > > > >       v4l2_i2c_subdev_init(&ov5645->sd, client, &ov5645_subdev_ops);
> > > > > > > > >       ov5645->sd.internal_ops = &ov5645_internal_ops;
> > > > > > > > > -     ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
> > > > > > > > > +     ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
> > > > > > > > > +                         V4L2_SUBDEV_FL_HAS_EVENTS;
> > > > > > > >
> > > > > > > > Instead of patching every subdev driver, should we handle all of this in
> > > > > > > > the subdev core ? If a control handler is set for the subdev, we could
> > > > > > > > set the HAS_EVENTS flag automatically, and default to
> > > > > > > > v4l2_ctrl_subdev_subscribe_event() and v4l2_event_subdev_unsubscribe()
> > > > > > > > if there are no control operations.
> > > > > >
> > > > > > Premit:
> > > > > >  - Don't know if I'm wrong eh.
> > > > >
> > > > > Nobody knows :-)
> > > > >
> > > > > > This can be done into:
> > > > > >
> > > > > > __v4l2_subdev_init_finalize()
> > > > > >
> > > > > > Adding:
> > > > > >
> > > > > >     if (sd->ctrl_handler)
> > > > > >             sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
> > > > > >
> > > > > > And check if there are no control operations using:
> > > > > >
> > > > > > bool has_subscribe_event;
> > > > > > bool has_unsubscribe_event;
> > > > > >
> > > > > >
> > > > > > has_subscribe_event = v4l2_subdev_has_op(sd, core, subscribe_event);
> > > > > > has_unsubscribe_event = v4l2_subdev_has_op(sd, core, unsubscribe_event);
> > > > > >
> > > > > > if (!has_subscribe_event)
> > > > > >     assign v4l2_ctrl_subdev_subscribe_event as default .subscribe ops(somehow)
> > > > >
> > > > > We can't change the ops structure as it's constant. Something like this
> > > > > could do:
> > > > >
> > > > > diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> > > > > index 3a4ba08810d2..41ae18a0d41e 100644
> > > > > --- a/drivers/media/v4l2-core/v4l2-subdev.c
> > > > > +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> > > > > @@ -691,10 +691,24 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
> > > > >               return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
> > > > >
> > > > >       case VIDIOC_SUBSCRIBE_EVENT:
> > > > > -             return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
> > > > > +             if (v4l2_subdev_has_op(sd, core, subscribe_event))
> > > > > +                     return v4l2_subdev_call(sd, core, subscribe_event, vfh,
> > > > > +                                             arg);
> > > > > +             else if ((sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) &&
> > > > > +                      vfh->ctrl_handler)
> > > > > +                     return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);
> > > > > +             else
> > > > > +                     return -ENOIOCTLCMD;
> > > > >
> > > > >       case VIDIOC_UNSUBSCRIBE_EVENT:
> > > > > -             return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
> > > > > +             if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
> > > > > +                     return -ENOIOCTLCMD;
> > > > > +
> > > > > +             if (v4l2_subdev_has_op(sd, core, unsubscribe_event))
> > > > > +                     return v4l2_subdev_call(sd, core, unsubscribe_event,
> > > > > +                                             vfh, arg);
> > > > > +             else
> > > > +                       return v4l2_event_subdev_unsubscribe(sd, vfh, arg);
> > > >
> > > > Thanks for your "more than an hint :)"
> > > > I'm able to test this on ov5645:
> > > >
> > > > Adding:
> > > >
> > > > +++ b/drivers/media/i2c/ov5645.c
> > > > @@ -1238,6 +1238,12 @@ static int ov5645_probe(struct i2c_client *client)
> > > >
> > > >         ov5645_init_state(&ov5645->sd, NULL);
> > > >
> > > > +       ret = v4l2_subdev_init_finalize(&ov5645->sd);
> > > > +       if (ret < 0) {
> > > > +               dev_err(dev, "subdev initialization error %d\n", ret);
> > > > +               goto err_free_state;
> > > > +       }
> > > > +
> > > >         ret = v4l2_async_register_subdev(&ov5645->sd);
> > > >         if (ret < 0) {
> > > >                 dev_err(dev, "could not register v4l2 device\n");
> > > > @@ -1251,6 +1257,8 @@ static int ov5645_probe(struct i2c_client *client)
> > > >
> > > >         return 0;
> > > >
> > > > +err_free_state:
> > > > +       v4l2_subdev_cleanup(&ov5645->sd);
> > > >  err_pm_runtime:
> > > >         pm_runtime_disable(dev);
> > > >         pm_runtime_put_noidle(dev);
> > > > @@ -1272,6 +1280,7 @@ static void ov5645_remove(struct i2c_client *client)
> > > >
> > > >         v4l2_async_unregister_subdev(&ov5645->sd);
> > > >         media_entity_cleanup(&ov5645->sd.entity);
> > > > +       v4l2_subdev_cleanup(&ov5645->sd);
> > > >         v4l2_ctrl_handler_free(&ov5645->ctrls);
> > > >         pm_runtime_disable(ov5645->dev);
> > > >         if (!pm_runtime_status_suspended(ov5645->dev))
> > > >
> > > > Then from the compliance tool I'm getting now good results:
> > > >
> > > > Total for device /dev/v4l-subdev1: 44, Succeeded: 44, Failed: 0, Warnings: 0
> > > >
> > > > I will send these 2 patches later if you agree (1 v4l2-subdev 1 ov5645.c)
> > > > Thanks again.
> > > >
> > > Thank you for the patch.
> > > 
> > > I am currently working on adding support for V4L2_SUBDEV_FL_HAS_EVENTS
> > > and subscribe hooks[1] (and some more features [0] for ov5645 driver),
> > > since the patch series adds internal pad which needs rework based on
> > > patch series from Sakari which I will do soon and send v3 patches for
> > > ov5645 driver.
> > > 
> > > [0] https://lore.kernel.org/all/20240910170610.226189-1-prabhakar.mahadev-lad.rj@bp.renesas.com/
> > > [1] https://lore.kernel.org/all/20240910170610.226189-2-prabhakar.mahadev-lad.rj@bp.renesas.com/
> > 
> > Thanks for sharing this.
> > Nice! then I can drop ov5645 patch and send only v4l2-subdev patch :)
> > 
> > Plan as suggested by Laurent is:
> > 
> > "Instead of patching every subdev driver, should we handle all of this in
> > the subdev core ? If a control handler is set for the subdev, we could
> > set the HAS_EVENTS flag automatically, and default to
> > v4l2_ctrl_subdev_subscribe_event() and v4l2_event_subdev_unsubscribe()
> > if there are no control operations"
> > 
> > Using:
> > 
> > diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> > index 3a4ba08810d2..fe584998f5e6 100644
> > --- a/drivers/media/v4l2-core/v4l2-subdev.c
> > +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> > @@ -691,10 +691,24 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
> >                 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
> > 
> >         case VIDIOC_SUBSCRIBE_EVENT:
> > -               return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
> > +               if (v4l2_subdev_has_op(sd, core, subscribe_event))
> > +                       return v4l2_subdev_call(sd, core, subscribe_event,
> > +                                               vfh, arg);
> > +               else if ((sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) &&
> 
> Useless use of else. Same below.

Better:

	case VIDIOC_SUBSCRIBE_EVENT:
		if (v4l2_subdev_has_op(sd, core, subscribe_event))
			return v4l2_subdev_call(sd, core, subscribe_event,
						vfh, arg);

		return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);

	case VIDIOC_UNSUBSCRIBE_EVENT:
		if (v4l2_subdev_has_op(sd, core, unsubscribe_event))
			return v4l2_subdev_call(sd, core, unsubscribe_event,
						vfh, arg);

		return v4l2_event_subdev_unsubscribe(sd, vfh, arg);

Because V4L2_SUBDEV_FL_HAS_EVENTS and vfh->ctrl_handler are already
check right?

Thanks & Regards,
Tommaso

> 
> > +                       vfh->ctrl_handler)
> > +                       return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);
> > +               else
> > +                       return -ENOIOCTLCMD;
> > 
> >         case VIDIOC_UNSUBSCRIBE_EVENT:
> > -               return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
> > +               if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
> > +                       return -ENOIOCTLCMD;
> > +
> > +               if (v4l2_subdev_has_op(sd, core, unsubscribe_event))
> > +                       return v4l2_subdev_call(sd, core, unsubscribe_event,
> > +                                               vfh, arg);
> > +               else
> > +                       return v4l2_event_subdev_unsubscribe(sd, vfh, arg);
> > 
> >  #ifdef CONFIG_VIDEO_ADV_DEBUG
> >         case VIDIOC_DBG_G_REGISTER:
> > @@ -1641,6 +1655,9 @@ int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
> >                 }
> >         }
> > 
> > +       if (sd->ctrl_handler)
> > +               sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
> > +
> >         state = __v4l2_subdev_state_alloc(sd, name, key);
> >         if (IS_ERR(state))
> >                 return PTR_ERR(state);
> > 
> > In theory v4l2_subdev_init_finalize() and subdev_do_ioctl() will handle
> > this.
> > 
> > Please Laurent correct me if I'm wrong.
> > Prabhakar what do you think?
> > Thanks in advance.
> 
> Looks good to me, with the above comment.
> 
> -- 
> Kind regards,
> 
> Sakari Ailus
Sakari Ailus Oct. 18, 2024, 10:55 a.m. UTC | #10
Hi Tommaso,

On Fri, Oct 18, 2024 at 12:48:21PM +0200, Tommaso Merciai wrote:
> Hi Sakari,
> Thanks for your review.
> 
> On Fri, Oct 18, 2024 at 10:33:56AM +0000, Sakari Ailus wrote:
> > Hi folks,
> > 
> > On Fri, Oct 18, 2024 at 12:18:15PM +0200, Tommaso Merciai wrote:
> > > Hi Prabhakar,
> > > Thanks for your comments.
> > > 
> > > On Fri, Oct 18, 2024 at 10:57:49AM +0100, Lad, Prabhakar wrote:
> > > > Hi Tommaso,
> > > > 
> > > > On Fri, Oct 18, 2024 at 10:28 AM Tommaso Merciai <tomm.merciai@gmail.com> wrote:
> > > > >
> > > > > Hi Laurent,
> > > > >
> > > > > On Wed, Oct 16, 2024 at 11:08:36PM +0300, Laurent Pinchart wrote:
> > > > > > Hi Tommaso,
> > > > > >
> > > > > > On Tue, Oct 15, 2024 at 06:37:58PM +0200, Tommaso Merciai wrote:
> > > > > > > On Tue, Oct 15, 2024 at 08:16:13AM +0200, Tommaso Merciai wrote:
> > > > > > > > On Mon, Oct 14, 2024 at 08:54:52PM +0300, Laurent Pinchart wrote:
> > > > > > > > > On Mon, Oct 14, 2024 at 07:38:40PM +0200, Tommaso Merciai wrote:
> > > > > > > > > > Controls can be exposed to userspace via a v4l-subdevX device, and
> > > > > > > > > > userspace has to be able to subscribe to control events so that it is
> > > > > > > > > > notified when the control changes value.
> > > > > > > > > > Add missing HAS_EVENTS support: flag and .(un)subscribe_event().
> > > > > > > > > >
> > > > > > > > > > Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> > > > > > > > > > ---
> > > > > > > > > >  drivers/media/i2c/ov5645.c | 10 +++++++++-
> > > > > > > > > >  1 file changed, 9 insertions(+), 1 deletion(-)
> > > > > > > > > >
> > > > > > > > > > diff --git a/drivers/media/i2c/ov5645.c b/drivers/media/i2c/ov5645.c
> > > > > > > > > > index 0c32bd2940ec..2c5145d5c616 100644
> > > > > > > > > > --- a/drivers/media/i2c/ov5645.c
> > > > > > > > > > +++ b/drivers/media/i2c/ov5645.c
> > > > > > > > > > @@ -29,6 +29,7 @@
> > > > > > > > > >  #include <linux/slab.h>
> > > > > > > > > >  #include <linux/types.h>
> > > > > > > > > >  #include <media/v4l2-ctrls.h>
> > > > > > > > > > +#include <media/v4l2-event.h>
> > > > > > > > > >  #include <media/v4l2-fwnode.h>
> > > > > > > > > >  #include <media/v4l2-subdev.h>
> > > > > > > > > >
> > > > > > > > > > @@ -1034,6 +1035,11 @@ static const struct v4l2_subdev_video_ops ov5645_video_ops = {
> > > > > > > > > >       .s_stream = ov5645_s_stream,
> > > > > > > > > >  };
> > > > > > > > > >
> > > > > > > > > > +static const struct v4l2_subdev_core_ops ov5645_subdev_core_ops = {
> > > > > > > > > > +     .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
> > > > > > > > > > +     .unsubscribe_event = v4l2_event_subdev_unsubscribe,
> > > > > > > > > > +};
> > > > > > > > > > +
> > > > > > > > > >  static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
> > > > > > > > > >       .enum_mbus_code = ov5645_enum_mbus_code,
> > > > > > > > > >       .enum_frame_size = ov5645_enum_frame_size,
> > > > > > > > > > @@ -1043,6 +1049,7 @@ static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
> > > > > > > > > >  };
> > > > > > > > > >
> > > > > > > > > >  static const struct v4l2_subdev_ops ov5645_subdev_ops = {
> > > > > > > > > > +     .core = &ov5645_subdev_core_ops,
> > > > > > > > > >       .video = &ov5645_video_ops,
> > > > > > > > > >       .pad = &ov5645_subdev_pad_ops,
> > > > > > > > > >  };
> > > > > > > > > > @@ -1178,7 +1185,8 @@ static int ov5645_probe(struct i2c_client *client)
> > > > > > > > > >
> > > > > > > > > >       v4l2_i2c_subdev_init(&ov5645->sd, client, &ov5645_subdev_ops);
> > > > > > > > > >       ov5645->sd.internal_ops = &ov5645_internal_ops;
> > > > > > > > > > -     ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
> > > > > > > > > > +     ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
> > > > > > > > > > +                         V4L2_SUBDEV_FL_HAS_EVENTS;
> > > > > > > > >
> > > > > > > > > Instead of patching every subdev driver, should we handle all of this in
> > > > > > > > > the subdev core ? If a control handler is set for the subdev, we could
> > > > > > > > > set the HAS_EVENTS flag automatically, and default to
> > > > > > > > > v4l2_ctrl_subdev_subscribe_event() and v4l2_event_subdev_unsubscribe()
> > > > > > > > > if there are no control operations.
> > > > > > >
> > > > > > > Premit:
> > > > > > >  - Don't know if I'm wrong eh.
> > > > > >
> > > > > > Nobody knows :-)
> > > > > >
> > > > > > > This can be done into:
> > > > > > >
> > > > > > > __v4l2_subdev_init_finalize()
> > > > > > >
> > > > > > > Adding:
> > > > > > >
> > > > > > >     if (sd->ctrl_handler)
> > > > > > >             sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
> > > > > > >
> > > > > > > And check if there are no control operations using:
> > > > > > >
> > > > > > > bool has_subscribe_event;
> > > > > > > bool has_unsubscribe_event;
> > > > > > >
> > > > > > >
> > > > > > > has_subscribe_event = v4l2_subdev_has_op(sd, core, subscribe_event);
> > > > > > > has_unsubscribe_event = v4l2_subdev_has_op(sd, core, unsubscribe_event);
> > > > > > >
> > > > > > > if (!has_subscribe_event)
> > > > > > >     assign v4l2_ctrl_subdev_subscribe_event as default .subscribe ops(somehow)
> > > > > >
> > > > > > We can't change the ops structure as it's constant. Something like this
> > > > > > could do:
> > > > > >
> > > > > > diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> > > > > > index 3a4ba08810d2..41ae18a0d41e 100644
> > > > > > --- a/drivers/media/v4l2-core/v4l2-subdev.c
> > > > > > +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> > > > > > @@ -691,10 +691,24 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
> > > > > >               return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
> > > > > >
> > > > > >       case VIDIOC_SUBSCRIBE_EVENT:
> > > > > > -             return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
> > > > > > +             if (v4l2_subdev_has_op(sd, core, subscribe_event))
> > > > > > +                     return v4l2_subdev_call(sd, core, subscribe_event, vfh,
> > > > > > +                                             arg);
> > > > > > +             else if ((sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) &&
> > > > > > +                      vfh->ctrl_handler)
> > > > > > +                     return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);
> > > > > > +             else
> > > > > > +                     return -ENOIOCTLCMD;
> > > > > >
> > > > > >       case VIDIOC_UNSUBSCRIBE_EVENT:
> > > > > > -             return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
> > > > > > +             if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
> > > > > > +                     return -ENOIOCTLCMD;
> > > > > > +
> > > > > > +             if (v4l2_subdev_has_op(sd, core, unsubscribe_event))
> > > > > > +                     return v4l2_subdev_call(sd, core, unsubscribe_event,
> > > > > > +                                             vfh, arg);
> > > > > > +             else
> > > > > +                       return v4l2_event_subdev_unsubscribe(sd, vfh, arg);
> > > > >
> > > > > Thanks for your "more than an hint :)"
> > > > > I'm able to test this on ov5645:
> > > > >
> > > > > Adding:
> > > > >
> > > > > +++ b/drivers/media/i2c/ov5645.c
> > > > > @@ -1238,6 +1238,12 @@ static int ov5645_probe(struct i2c_client *client)
> > > > >
> > > > >         ov5645_init_state(&ov5645->sd, NULL);
> > > > >
> > > > > +       ret = v4l2_subdev_init_finalize(&ov5645->sd);
> > > > > +       if (ret < 0) {
> > > > > +               dev_err(dev, "subdev initialization error %d\n", ret);
> > > > > +               goto err_free_state;
> > > > > +       }
> > > > > +
> > > > >         ret = v4l2_async_register_subdev(&ov5645->sd);
> > > > >         if (ret < 0) {
> > > > >                 dev_err(dev, "could not register v4l2 device\n");
> > > > > @@ -1251,6 +1257,8 @@ static int ov5645_probe(struct i2c_client *client)
> > > > >
> > > > >         return 0;
> > > > >
> > > > > +err_free_state:
> > > > > +       v4l2_subdev_cleanup(&ov5645->sd);
> > > > >  err_pm_runtime:
> > > > >         pm_runtime_disable(dev);
> > > > >         pm_runtime_put_noidle(dev);
> > > > > @@ -1272,6 +1280,7 @@ static void ov5645_remove(struct i2c_client *client)
> > > > >
> > > > >         v4l2_async_unregister_subdev(&ov5645->sd);
> > > > >         media_entity_cleanup(&ov5645->sd.entity);
> > > > > +       v4l2_subdev_cleanup(&ov5645->sd);
> > > > >         v4l2_ctrl_handler_free(&ov5645->ctrls);
> > > > >         pm_runtime_disable(ov5645->dev);
> > > > >         if (!pm_runtime_status_suspended(ov5645->dev))
> > > > >
> > > > > Then from the compliance tool I'm getting now good results:
> > > > >
> > > > > Total for device /dev/v4l-subdev1: 44, Succeeded: 44, Failed: 0, Warnings: 0
> > > > >
> > > > > I will send these 2 patches later if you agree (1 v4l2-subdev 1 ov5645.c)
> > > > > Thanks again.
> > > > >
> > > > Thank you for the patch.
> > > > 
> > > > I am currently working on adding support for V4L2_SUBDEV_FL_HAS_EVENTS
> > > > and subscribe hooks[1] (and some more features [0] for ov5645 driver),
> > > > since the patch series adds internal pad which needs rework based on
> > > > patch series from Sakari which I will do soon and send v3 patches for
> > > > ov5645 driver.
> > > > 
> > > > [0] https://lore.kernel.org/all/20240910170610.226189-1-prabhakar.mahadev-lad.rj@bp.renesas.com/
> > > > [1] https://lore.kernel.org/all/20240910170610.226189-2-prabhakar.mahadev-lad.rj@bp.renesas.com/
> > > 
> > > Thanks for sharing this.
> > > Nice! then I can drop ov5645 patch and send only v4l2-subdev patch :)
> > > 
> > > Plan as suggested by Laurent is:
> > > 
> > > "Instead of patching every subdev driver, should we handle all of this in
> > > the subdev core ? If a control handler is set for the subdev, we could
> > > set the HAS_EVENTS flag automatically, and default to
> > > v4l2_ctrl_subdev_subscribe_event() and v4l2_event_subdev_unsubscribe()
> > > if there are no control operations"
> > > 
> > > Using:
> > > 
> > > diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> > > index 3a4ba08810d2..fe584998f5e6 100644
> > > --- a/drivers/media/v4l2-core/v4l2-subdev.c
> > > +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> > > @@ -691,10 +691,24 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
> > >                 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
> > > 
> > >         case VIDIOC_SUBSCRIBE_EVENT:
> > > -               return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
> > > +               if (v4l2_subdev_has_op(sd, core, subscribe_event))
> > > +                       return v4l2_subdev_call(sd, core, subscribe_event,
> > > +                                               vfh, arg);
> > > +               else if ((sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) &&
> > 
> > Useless use of else. Same below.
> 
> Better:
> 
> 	case VIDIOC_SUBSCRIBE_EVENT:
> 		if (v4l2_subdev_has_op(sd, core, subscribe_event))
> 			return v4l2_subdev_call(sd, core, subscribe_event,
> 						vfh, arg);
> 
> 		return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);
> 
> 	case VIDIOC_UNSUBSCRIBE_EVENT:
> 		if (v4l2_subdev_has_op(sd, core, unsubscribe_event))
> 			return v4l2_subdev_call(sd, core, unsubscribe_event,
> 						vfh, arg);
> 
> 		return v4l2_event_subdev_unsubscribe(sd, vfh, arg);
> 
> Because V4L2_SUBDEV_FL_HAS_EVENTS and vfh->ctrl_handler are already
> check right?

v4l2_ctrl_subdev_{,un}subscribe_event() does check for the presence of the
control handler, but return -EINVAL if one doesn't exist. We should however
return -ENOIOCTLCMD, to indicate there's no such IOCTL if the HAS_EVENTS
flag is missing.

> > 
> > > +                       vfh->ctrl_handler)
> > > +                       return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);
> > > +               else
> > > +                       return -ENOIOCTLCMD;
> > > 
> > >         case VIDIOC_UNSUBSCRIBE_EVENT:
> > > -               return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
> > > +               if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
> > > +                       return -ENOIOCTLCMD;
> > > +
> > > +               if (v4l2_subdev_has_op(sd, core, unsubscribe_event))
> > > +                       return v4l2_subdev_call(sd, core, unsubscribe_event,
> > > +                                               vfh, arg);
> > > +               else
> > > +                       return v4l2_event_subdev_unsubscribe(sd, vfh, arg);
> > > 
> > >  #ifdef CONFIG_VIDEO_ADV_DEBUG
> > >         case VIDIOC_DBG_G_REGISTER:
> > > @@ -1641,6 +1655,9 @@ int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
> > >                 }
> > >         }
> > > 
> > > +       if (sd->ctrl_handler)
> > > +               sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
> > > +
> > >         state = __v4l2_subdev_state_alloc(sd, name, key);
> > >         if (IS_ERR(state))
> > >                 return PTR_ERR(state);
> > > 
> > > In theory v4l2_subdev_init_finalize() and subdev_do_ioctl() will handle
> > > this.
> > > 
> > > Please Laurent correct me if I'm wrong.
> > > Prabhakar what do you think?
> > > Thanks in advance.
> > 
> > Looks good to me, with the above comment.
Tommaso Merciai Oct. 18, 2024, 12:50 p.m. UTC | #11
Hi Sakari,
Thanks for your comment.

On Fri, Oct 18, 2024 at 10:55:26AM +0000, Sakari Ailus wrote:
> Hi Tommaso,
> 
> On Fri, Oct 18, 2024 at 12:48:21PM +0200, Tommaso Merciai wrote:
> > Hi Sakari,
> > Thanks for your review.
> > 
> > On Fri, Oct 18, 2024 at 10:33:56AM +0000, Sakari Ailus wrote:
> > > Hi folks,
> > > 
> > > On Fri, Oct 18, 2024 at 12:18:15PM +0200, Tommaso Merciai wrote:
> > > > Hi Prabhakar,
> > > > Thanks for your comments.
> > > > 
> > > > On Fri, Oct 18, 2024 at 10:57:49AM +0100, Lad, Prabhakar wrote:
> > > > > Hi Tommaso,
> > > > > 
> > > > > On Fri, Oct 18, 2024 at 10:28 AM Tommaso Merciai <tomm.merciai@gmail.com> wrote:
> > > > > >
> > > > > > Hi Laurent,
> > > > > >
> > > > > > On Wed, Oct 16, 2024 at 11:08:36PM +0300, Laurent Pinchart wrote:
> > > > > > > Hi Tommaso,
> > > > > > >
> > > > > > > On Tue, Oct 15, 2024 at 06:37:58PM +0200, Tommaso Merciai wrote:
> > > > > > > > On Tue, Oct 15, 2024 at 08:16:13AM +0200, Tommaso Merciai wrote:
> > > > > > > > > On Mon, Oct 14, 2024 at 08:54:52PM +0300, Laurent Pinchart wrote:
> > > > > > > > > > On Mon, Oct 14, 2024 at 07:38:40PM +0200, Tommaso Merciai wrote:
> > > > > > > > > > > Controls can be exposed to userspace via a v4l-subdevX device, and
> > > > > > > > > > > userspace has to be able to subscribe to control events so that it is
> > > > > > > > > > > notified when the control changes value.
> > > > > > > > > > > Add missing HAS_EVENTS support: flag and .(un)subscribe_event().
> > > > > > > > > > >
> > > > > > > > > > > Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> > > > > > > > > > > ---
> > > > > > > > > > >  drivers/media/i2c/ov5645.c | 10 +++++++++-
> > > > > > > > > > >  1 file changed, 9 insertions(+), 1 deletion(-)
> > > > > > > > > > >
> > > > > > > > > > > diff --git a/drivers/media/i2c/ov5645.c b/drivers/media/i2c/ov5645.c
> > > > > > > > > > > index 0c32bd2940ec..2c5145d5c616 100644
> > > > > > > > > > > --- a/drivers/media/i2c/ov5645.c
> > > > > > > > > > > +++ b/drivers/media/i2c/ov5645.c
> > > > > > > > > > > @@ -29,6 +29,7 @@
> > > > > > > > > > >  #include <linux/slab.h>
> > > > > > > > > > >  #include <linux/types.h>
> > > > > > > > > > >  #include <media/v4l2-ctrls.h>
> > > > > > > > > > > +#include <media/v4l2-event.h>
> > > > > > > > > > >  #include <media/v4l2-fwnode.h>
> > > > > > > > > > >  #include <media/v4l2-subdev.h>
> > > > > > > > > > >
> > > > > > > > > > > @@ -1034,6 +1035,11 @@ static const struct v4l2_subdev_video_ops ov5645_video_ops = {
> > > > > > > > > > >       .s_stream = ov5645_s_stream,
> > > > > > > > > > >  };
> > > > > > > > > > >
> > > > > > > > > > > +static const struct v4l2_subdev_core_ops ov5645_subdev_core_ops = {
> > > > > > > > > > > +     .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
> > > > > > > > > > > +     .unsubscribe_event = v4l2_event_subdev_unsubscribe,
> > > > > > > > > > > +};
> > > > > > > > > > > +
> > > > > > > > > > >  static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
> > > > > > > > > > >       .enum_mbus_code = ov5645_enum_mbus_code,
> > > > > > > > > > >       .enum_frame_size = ov5645_enum_frame_size,
> > > > > > > > > > > @@ -1043,6 +1049,7 @@ static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
> > > > > > > > > > >  };
> > > > > > > > > > >
> > > > > > > > > > >  static const struct v4l2_subdev_ops ov5645_subdev_ops = {
> > > > > > > > > > > +     .core = &ov5645_subdev_core_ops,
> > > > > > > > > > >       .video = &ov5645_video_ops,
> > > > > > > > > > >       .pad = &ov5645_subdev_pad_ops,
> > > > > > > > > > >  };
> > > > > > > > > > > @@ -1178,7 +1185,8 @@ static int ov5645_probe(struct i2c_client *client)
> > > > > > > > > > >
> > > > > > > > > > >       v4l2_i2c_subdev_init(&ov5645->sd, client, &ov5645_subdev_ops);
> > > > > > > > > > >       ov5645->sd.internal_ops = &ov5645_internal_ops;
> > > > > > > > > > > -     ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
> > > > > > > > > > > +     ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
> > > > > > > > > > > +                         V4L2_SUBDEV_FL_HAS_EVENTS;
> > > > > > > > > >
> > > > > > > > > > Instead of patching every subdev driver, should we handle all of this in
> > > > > > > > > > the subdev core ? If a control handler is set for the subdev, we could
> > > > > > > > > > set the HAS_EVENTS flag automatically, and default to
> > > > > > > > > > v4l2_ctrl_subdev_subscribe_event() and v4l2_event_subdev_unsubscribe()
> > > > > > > > > > if there are no control operations.
> > > > > > > >
> > > > > > > > Premit:
> > > > > > > >  - Don't know if I'm wrong eh.
> > > > > > >
> > > > > > > Nobody knows :-)
> > > > > > >
> > > > > > > > This can be done into:
> > > > > > > >
> > > > > > > > __v4l2_subdev_init_finalize()
> > > > > > > >
> > > > > > > > Adding:
> > > > > > > >
> > > > > > > >     if (sd->ctrl_handler)
> > > > > > > >             sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
> > > > > > > >
> > > > > > > > And check if there are no control operations using:
> > > > > > > >
> > > > > > > > bool has_subscribe_event;
> > > > > > > > bool has_unsubscribe_event;
> > > > > > > >
> > > > > > > >
> > > > > > > > has_subscribe_event = v4l2_subdev_has_op(sd, core, subscribe_event);
> > > > > > > > has_unsubscribe_event = v4l2_subdev_has_op(sd, core, unsubscribe_event);
> > > > > > > >
> > > > > > > > if (!has_subscribe_event)
> > > > > > > >     assign v4l2_ctrl_subdev_subscribe_event as default .subscribe ops(somehow)
> > > > > > >
> > > > > > > We can't change the ops structure as it's constant. Something like this
> > > > > > > could do:
> > > > > > >
> > > > > > > diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> > > > > > > index 3a4ba08810d2..41ae18a0d41e 100644
> > > > > > > --- a/drivers/media/v4l2-core/v4l2-subdev.c
> > > > > > > +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> > > > > > > @@ -691,10 +691,24 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
> > > > > > >               return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
> > > > > > >
> > > > > > >       case VIDIOC_SUBSCRIBE_EVENT:
> > > > > > > -             return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
> > > > > > > +             if (v4l2_subdev_has_op(sd, core, subscribe_event))
> > > > > > > +                     return v4l2_subdev_call(sd, core, subscribe_event, vfh,
> > > > > > > +                                             arg);
> > > > > > > +             else if ((sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) &&
> > > > > > > +                      vfh->ctrl_handler)
> > > > > > > +                     return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);
> > > > > > > +             else
> > > > > > > +                     return -ENOIOCTLCMD;
> > > > > > >
> > > > > > >       case VIDIOC_UNSUBSCRIBE_EVENT:
> > > > > > > -             return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
> > > > > > > +             if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
> > > > > > > +                     return -ENOIOCTLCMD;
> > > > > > > +
> > > > > > > +             if (v4l2_subdev_has_op(sd, core, unsubscribe_event))
> > > > > > > +                     return v4l2_subdev_call(sd, core, unsubscribe_event,
> > > > > > > +                                             vfh, arg);
> > > > > > > +             else
> > > > > > +                       return v4l2_event_subdev_unsubscribe(sd, vfh, arg);
> > > > > >
> > > > > > Thanks for your "more than an hint :)"
> > > > > > I'm able to test this on ov5645:
> > > > > >
> > > > > > Adding:
> > > > > >
> > > > > > +++ b/drivers/media/i2c/ov5645.c
> > > > > > @@ -1238,6 +1238,12 @@ static int ov5645_probe(struct i2c_client *client)
> > > > > >
> > > > > >         ov5645_init_state(&ov5645->sd, NULL);
> > > > > >
> > > > > > +       ret = v4l2_subdev_init_finalize(&ov5645->sd);
> > > > > > +       if (ret < 0) {
> > > > > > +               dev_err(dev, "subdev initialization error %d\n", ret);
> > > > > > +               goto err_free_state;
> > > > > > +       }
> > > > > > +
> > > > > >         ret = v4l2_async_register_subdev(&ov5645->sd);
> > > > > >         if (ret < 0) {
> > > > > >                 dev_err(dev, "could not register v4l2 device\n");
> > > > > > @@ -1251,6 +1257,8 @@ static int ov5645_probe(struct i2c_client *client)
> > > > > >
> > > > > >         return 0;
> > > > > >
> > > > > > +err_free_state:
> > > > > > +       v4l2_subdev_cleanup(&ov5645->sd);
> > > > > >  err_pm_runtime:
> > > > > >         pm_runtime_disable(dev);
> > > > > >         pm_runtime_put_noidle(dev);
> > > > > > @@ -1272,6 +1280,7 @@ static void ov5645_remove(struct i2c_client *client)
> > > > > >
> > > > > >         v4l2_async_unregister_subdev(&ov5645->sd);
> > > > > >         media_entity_cleanup(&ov5645->sd.entity);
> > > > > > +       v4l2_subdev_cleanup(&ov5645->sd);
> > > > > >         v4l2_ctrl_handler_free(&ov5645->ctrls);
> > > > > >         pm_runtime_disable(ov5645->dev);
> > > > > >         if (!pm_runtime_status_suspended(ov5645->dev))
> > > > > >
> > > > > > Then from the compliance tool I'm getting now good results:
> > > > > >
> > > > > > Total for device /dev/v4l-subdev1: 44, Succeeded: 44, Failed: 0, Warnings: 0
> > > > > >
> > > > > > I will send these 2 patches later if you agree (1 v4l2-subdev 1 ov5645.c)
> > > > > > Thanks again.
> > > > > >
> > > > > Thank you for the patch.
> > > > > 
> > > > > I am currently working on adding support for V4L2_SUBDEV_FL_HAS_EVENTS
> > > > > and subscribe hooks[1] (and some more features [0] for ov5645 driver),
> > > > > since the patch series adds internal pad which needs rework based on
> > > > > patch series from Sakari which I will do soon and send v3 patches for
> > > > > ov5645 driver.
> > > > > 
> > > > > [0] https://lore.kernel.org/all/20240910170610.226189-1-prabhakar.mahadev-lad.rj@bp.renesas.com/
> > > > > [1] https://lore.kernel.org/all/20240910170610.226189-2-prabhakar.mahadev-lad.rj@bp.renesas.com/
> > > > 
> > > > Thanks for sharing this.
> > > > Nice! then I can drop ov5645 patch and send only v4l2-subdev patch :)
> > > > 
> > > > Plan as suggested by Laurent is:
> > > > 
> > > > "Instead of patching every subdev driver, should we handle all of this in
> > > > the subdev core ? If a control handler is set for the subdev, we could
> > > > set the HAS_EVENTS flag automatically, and default to
> > > > v4l2_ctrl_subdev_subscribe_event() and v4l2_event_subdev_unsubscribe()
> > > > if there are no control operations"
> > > > 
> > > > Using:
> > > > 
> > > > diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> > > > index 3a4ba08810d2..fe584998f5e6 100644
> > > > --- a/drivers/media/v4l2-core/v4l2-subdev.c
> > > > +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> > > > @@ -691,10 +691,24 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
> > > >                 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
> > > > 
> > > >         case VIDIOC_SUBSCRIBE_EVENT:
> > > > -               return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
> > > > +               if (v4l2_subdev_has_op(sd, core, subscribe_event))
> > > > +                       return v4l2_subdev_call(sd, core, subscribe_event,
> > > > +                                               vfh, arg);
> > > > +               else if ((sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) &&
> > > 
> > > Useless use of else. Same below.
> > 
> > Better:
> > 
> > 	case VIDIOC_SUBSCRIBE_EVENT:
> > 		if (v4l2_subdev_has_op(sd, core, subscribe_event))
> > 			return v4l2_subdev_call(sd, core, subscribe_event,
> > 						vfh, arg);
> > 
> > 		return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);
> > 
> > 	case VIDIOC_UNSUBSCRIBE_EVENT:
> > 		if (v4l2_subdev_has_op(sd, core, unsubscribe_event))
> > 			return v4l2_subdev_call(sd, core, unsubscribe_event,
> > 						vfh, arg);
> > 
> > 		return v4l2_event_subdev_unsubscribe(sd, vfh, arg);
> > 
> > Because V4L2_SUBDEV_FL_HAS_EVENTS and vfh->ctrl_handler are already
> > check right?
> 
> v4l2_ctrl_subdev_{,un}subscribe_event() does check for the presence of the
> control handler, but return -EINVAL if one doesn't exist. We should however
> return -ENOIOCTLCMD, to indicate there's no such IOCTL if the HAS_EVENTS
> flag is missing.

Got it thanks.
I think with your review will be:

diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
index 3a4ba08810d2..77ca829b9983 100644
--- a/drivers/media/v4l2-core/v4l2-subdev.c
+++ b/drivers/media/v4l2-core/v4l2-subdev.c
@@ -691,10 +691,25 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
                return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);

        case VIDIOC_SUBSCRIBE_EVENT:
-               return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
+               if (v4l2_subdev_has_op(sd, core, subscribe_event))
+                       return v4l2_subdev_call(sd, core, subscribe_event,
+                                               vfh, arg);
+
+               if ((sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) &&
+                    vfh->ctrl_handler)
+                       return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);
+
+               return -ENOIOCTLCMD;

        case VIDIOC_UNSUBSCRIBE_EVENT:
-               return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
+               if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
+                       return -ENOIOCTLCMD;
+
+               if (v4l2_subdev_has_op(sd, core, unsubscribe_event))
+                       return v4l2_subdev_call(sd, core, unsubscribe_event,
+                                               vfh, arg);
+
+               return v4l2_event_subdev_unsubscribe(sd, vfh, arg);

 #ifdef CONFIG_VIDEO_ADV_DEBUG
        case VIDIOC_DBG_G_REGISTER:
@@ -1641,6 +1656,9 @@ int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
                }
        }

+       if (sd->ctrl_handler)
+               sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
+
        state = __v4l2_subdev_state_alloc(sd, name, key);
        if (IS_ERR(state))
                return PTR_ERR(state);

Just dropping else.
I will send a patch later if you all agree.
Thanks All. :)

Regards,
Tommaso

> 
> > > 
> > > > +                       vfh->ctrl_handler)
> > > > +                       return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);
> > > > +               else
> > > > +                       return -ENOIOCTLCMD;
> > > > 
> > > >         case VIDIOC_UNSUBSCRIBE_EVENT:
> > > > -               return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
> > > > +               if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
> > > > +                       return -ENOIOCTLCMD;
> > > > +
> > > > +               if (v4l2_subdev_has_op(sd, core, unsubscribe_event))
> > > > +                       return v4l2_subdev_call(sd, core, unsubscribe_event,
> > > > +                                               vfh, arg);
> > > > +               else
> > > > +                       return v4l2_event_subdev_unsubscribe(sd, vfh, arg);
> > > > 
> > > >  #ifdef CONFIG_VIDEO_ADV_DEBUG
> > > >         case VIDIOC_DBG_G_REGISTER:
> > > > @@ -1641,6 +1655,9 @@ int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
> > > >                 }
> > > >         }
> > > > 
> > > > +       if (sd->ctrl_handler)
> > > > +               sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
> > > > +
> > > >         state = __v4l2_subdev_state_alloc(sd, name, key);
> > > >         if (IS_ERR(state))
> > > >                 return PTR_ERR(state);
> > > > 
> > > > In theory v4l2_subdev_init_finalize() and subdev_do_ioctl() will handle
> > > > this.
> > > > 
> > > > Please Laurent correct me if I'm wrong.
> > > > Prabhakar what do you think?
> > > > Thanks in advance.
> > > 
> > > Looks good to me, with the above comment.
> 
> -- 
> Kind regards,
> 
> Sakari Ailus

Thanks & Regards,
Tommaso
diff mbox series

Patch

diff --git a/drivers/media/i2c/ov5645.c b/drivers/media/i2c/ov5645.c
index 0c32bd2940ec..2c5145d5c616 100644
--- a/drivers/media/i2c/ov5645.c
+++ b/drivers/media/i2c/ov5645.c
@@ -29,6 +29,7 @@ 
 #include <linux/slab.h>
 #include <linux/types.h>
 #include <media/v4l2-ctrls.h>
+#include <media/v4l2-event.h>
 #include <media/v4l2-fwnode.h>
 #include <media/v4l2-subdev.h>
 
@@ -1034,6 +1035,11 @@  static const struct v4l2_subdev_video_ops ov5645_video_ops = {
 	.s_stream = ov5645_s_stream,
 };
 
+static const struct v4l2_subdev_core_ops ov5645_subdev_core_ops = {
+	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
+	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
+};
+
 static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
 	.enum_mbus_code = ov5645_enum_mbus_code,
 	.enum_frame_size = ov5645_enum_frame_size,
@@ -1043,6 +1049,7 @@  static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
 };
 
 static const struct v4l2_subdev_ops ov5645_subdev_ops = {
+	.core = &ov5645_subdev_core_ops,
 	.video = &ov5645_video_ops,
 	.pad = &ov5645_subdev_pad_ops,
 };
@@ -1178,7 +1185,8 @@  static int ov5645_probe(struct i2c_client *client)
 
 	v4l2_i2c_subdev_init(&ov5645->sd, client, &ov5645_subdev_ops);
 	ov5645->sd.internal_ops = &ov5645_internal_ops;
-	ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
+	ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
+			    V4L2_SUBDEV_FL_HAS_EVENTS;
 	ov5645->pad.flags = MEDIA_PAD_FL_SOURCE;
 	ov5645->sd.dev = &client->dev;
 	ov5645->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;