diff mbox series

media: v4l2-subdev: Refactor events

Message ID 20241018171104.1624426-1-tomm.merciai@gmail.com
State Superseded
Headers show
Series media: v4l2-subdev: Refactor events | expand

Commit Message

Tommaso Merciai Oct. 18, 2024, 5:11 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.
If a control handler is set for the subdev then set the HAS_EVENTS
flag automatically into v4l2_subdev_init_finalize() and use
v4l2_ctrl_subdev_subscribe_event() and v4l2_event_subdev_unsubscribe()
as default if subdev don't have .(un)subscribe control operations.

Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
---
 drivers/media/v4l2-core/v4l2-subdev.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

Comments

Sakari Ailus Oct. 18, 2024, 7:30 p.m. UTC | #1
Hi Tommaso,

Thanks for working on this.

On Fri, Oct 18, 2024 at 07:11:03PM +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.
> If a control handler is set for the subdev then set the HAS_EVENTS
> flag automatically into v4l2_subdev_init_finalize() and use
> v4l2_ctrl_subdev_subscribe_event() and v4l2_event_subdev_unsubscribe()
> as default if subdev don't have .(un)subscribe control operations.
> 
> Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> ---
>  drivers/media/v4l2-core/v4l2-subdev.c | 22 ++++++++++++++++++++--
>  1 file changed, 20 insertions(+), 2 deletions(-)
> 
> 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;

While this mostly does the same thing, I prefer the order of tests below.
Could you align event subscription with unsubscription?

>  
>  	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);
Tommaso Merciai Oct. 19, 2024, 10:07 p.m. UTC | #2
Hi Sakari,

On Fri, Oct 18, 2024 at 07:30:30PM +0000, Sakari Ailus wrote:
> Hi Tommaso,
> 
> Thanks for working on this.

In real it's a Laurent's suggestion :)

> 
> On Fri, Oct 18, 2024 at 07:11:03PM +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.
> > If a control handler is set for the subdev then set the HAS_EVENTS
> > flag automatically into v4l2_subdev_init_finalize() and use
> > v4l2_ctrl_subdev_subscribe_event() and v4l2_event_subdev_unsubscribe()
> > as default if subdev don't have .(un)subscribe control operations.
> > 
> > Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> > ---
> >  drivers/media/v4l2-core/v4l2-subdev.c | 22 ++++++++++++++++++++--
> >  1 file changed, 20 insertions(+), 2 deletions(-)
> > 
> > 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;
> 
> While this mostly does the same thing, I prefer the order of tests below.
> Could you align event subscription with unsubscription?

What about:

	case VIDIOC_SUBSCRIBE_EVENT:
		if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
			return -ENOIOCTLCMD;

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

		if (!vfh->ctrl_handler)
			return -ENOTTY;

		return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);

	case VIDIOC_UNSUBSCRIBE_EVENT:
		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);

?

Thanks & Regards,
Tommaso

> 
> >  
> >  	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);
> 
> -- 
> Kind regards,
> 
> Sakari Ailus
Sakari Ailus Oct. 20, 2024, 10:45 a.m. UTC | #3
Hi Tommaso,

On Sun, Oct 20, 2024 at 12:07:19AM +0200, Tommaso Merciai wrote:
> Hi Sakari,
> 
> On Fri, Oct 18, 2024 at 07:30:30PM +0000, Sakari Ailus wrote:
> > Hi Tommaso,
> > 
> > Thanks for working on this.
> 
> In real it's a Laurent's suggestion :)
> 
> > 
> > On Fri, Oct 18, 2024 at 07:11:03PM +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.
> > > If a control handler is set for the subdev then set the HAS_EVENTS
> > > flag automatically into v4l2_subdev_init_finalize() and use
> > > v4l2_ctrl_subdev_subscribe_event() and v4l2_event_subdev_unsubscribe()
> > > as default if subdev don't have .(un)subscribe control operations.
> > > 
> > > Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> > > ---
> > >  drivers/media/v4l2-core/v4l2-subdev.c | 22 ++++++++++++++++++++--
> > >  1 file changed, 20 insertions(+), 2 deletions(-)
> > > 
> > > 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;
> > 
> > While this mostly does the same thing, I prefer the order of tests below.
> > Could you align event subscription with unsubscription?
> 
> What about:
> 
> 	case VIDIOC_SUBSCRIBE_EVENT:
> 		if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
> 			return -ENOIOCTLCMD;
> 
> 		if (v4l2_subdev_has_op(sd, core, subscribe_event))
> 			return v4l2_subdev_call(sd, core, subscribe_event,
> 						vfh, arg);
> 
> 		if (!vfh->ctrl_handler)
> 			return -ENOTTY;
> 
> 		return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);

Oh, right. Actually the earlier unsubscription didn't always produce
correct results. I missed that. This is needlessly complicated at the
moment. So I'd do:

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

		return (sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) &&
			vfh->ctrl_handler ?
			v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg) :
			-ENOIOCTLCMD;

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

		return sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS ?
			v4l2_event_subdev_unsubscribe(sd, vfh, arg) :
			-ENOIOCTLCMD;


> 
> 	case VIDIOC_UNSUBSCRIBE_EVENT:
> 		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);
> 
> ?
> 
> Thanks & Regards,
> Tommaso
> 
> > 
> > >  
> > >  	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);
> > 
> > -- 
> > Kind regards,
> > 
> > Sakari Ailus
Laurent Pinchart Oct. 20, 2024, 3:46 p.m. UTC | #4
On Sun, Oct 20, 2024 at 10:45:59AM +0000, Sakari Ailus wrote:
> Hi Tommaso,
> 
> On Sun, Oct 20, 2024 at 12:07:19AM +0200, Tommaso Merciai wrote:
> > Hi Sakari,
> > 
> > On Fri, Oct 18, 2024 at 07:30:30PM +0000, Sakari Ailus wrote:
> > > Hi Tommaso,
> > > 
> > > Thanks for working on this.
> > 
> > In real it's a Laurent's suggestion :)
> > 
> > > 
> > > On Fri, Oct 18, 2024 at 07:11:03PM +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.
> > > > If a control handler is set for the subdev then set the HAS_EVENTS
> > > > flag automatically into v4l2_subdev_init_finalize() and use
> > > > v4l2_ctrl_subdev_subscribe_event() and v4l2_event_subdev_unsubscribe()
> > > > as default if subdev don't have .(un)subscribe control operations.
> > > > 
> > > > Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> > > > ---
> > > >  drivers/media/v4l2-core/v4l2-subdev.c | 22 ++++++++++++++++++++--
> > > >  1 file changed, 20 insertions(+), 2 deletions(-)
> > > > 
> > > > 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;
> > > 
> > > While this mostly does the same thing, I prefer the order of tests below.
> > > Could you align event subscription with unsubscription?
> > 
> > What about:
> > 
> > 	case VIDIOC_SUBSCRIBE_EVENT:
> > 		if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
> > 			return -ENOIOCTLCMD;
> > 
> > 		if (v4l2_subdev_has_op(sd, core, subscribe_event))
> > 			return v4l2_subdev_call(sd, core, subscribe_event,
> > 						vfh, arg);
> > 
> > 		if (!vfh->ctrl_handler)
> > 			return -ENOTTY;
> > 
> > 		return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);
> 
> Oh, right. Actually the earlier unsubscription didn't always produce
> correct results. I missed that. This is needlessly complicated at the
> moment. So I'd do:
> 
> 	case VIDIOC_SUBSCRIBE_EVENT:
> 		if (v4l2_subdev_has_op(sd, core, subscribe_event))
> 			return v4l2_subdev_call(sd, core, subscribe_event,
> 						vfh, arg);
> 
> 		return (sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) &&
> 			vfh->ctrl_handler ?
> 			v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg) :
> 			-ENOIOCTLCMD;

Let's make this clearer:


		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:
> 		if (v4l2_subdev_has_op(sd, core, unsubscribe_event))
> 			return v4l2_subdev_call(sd, core, unsubscribe_event,
> 						vfh, arg);
> 
> 		return sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS ?
> 			v4l2_event_subdev_unsubscribe(sd, vfh, arg) :
> 			-ENOIOCTLCMD;

		if (sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)
			return v4l2_event_subdev_unsubscribe(sd, vfh, arg);

		return -ENOIOCTLCMD;

> 
> 
> > 
> > 	case VIDIOC_UNSUBSCRIBE_EVENT:
> > 		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);
> > 
> > ?
> > 
> > > >  
> > > >  	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);
diff mbox series

Patch

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);