diff mbox series

usb: gadget: uvc: fix multiple opens

Message ID 20201105103119.11419-1-thomas.haemmerle@wolfvision.net
State Superseded
Headers show
Series usb: gadget: uvc: fix multiple opens | expand

Commit Message

Thomas Haemmerle Nov. 5, 2020, 10:31 a.m. UTC
From: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>

Currently, the UVC function is activated when open on the corresponding
v4l2 device is called.
On another open the activation of the function fails since the
deactivation counter in `usb_function_activate` equals 0. However the
error is not returned to userspace since the open of the v4l2 device is
successful.

On a close the function is deactivated (since deactivation counter still
equals 0) and the video is disabled in `uvc_v4l2_release`, although
another process potentially is streaming.

Move activation of UVC function to subscription on UVC_EVENT_SETUP and
keep track of the number of subscribers (limited to 1) because there we
can guarantee for a userspace program utilizing UVC.
Extend the `struct uvc_file_handle` with member `bool setup_subscriber`
to tag it for a deactivation of the function.

With this a process is able to check capabilities of the v4l2 device
without deactivating the function for another process actually using the
device for UVC streaming.

Signed-off-by: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>
---
 drivers/usb/gadget/function/uvc.h      |  2 +
 drivers/usb/gadget/function/uvc_v4l2.c | 57 +++++++++++++++++++++-----
 2 files changed, 49 insertions(+), 10 deletions(-)

Comments

Greg Kroah-Hartman Nov. 5, 2020, 10:37 a.m. UTC | #1
On Thu, Nov 05, 2020 at 11:31:19AM +0100, thomas.haemmerle@wolfvision.net wrote:
> From: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>

> 

> Currently, the UVC function is activated when open on the corresponding

> v4l2 device is called.

> On another open the activation of the function fails since the

> deactivation counter in `usb_function_activate` equals 0. However the

> error is not returned to userspace since the open of the v4l2 device is

> successful.

> 

> On a close the function is deactivated (since deactivation counter still

> equals 0) and the video is disabled in `uvc_v4l2_release`, although

> another process potentially is streaming.

> 

> Move activation of UVC function to subscription on UVC_EVENT_SETUP and

> keep track of the number of subscribers (limited to 1) because there we

> can guarantee for a userspace program utilizing UVC.

> Extend the `struct uvc_file_handle` with member `bool setup_subscriber`

> to tag it for a deactivation of the function.

> 

> With this a process is able to check capabilities of the v4l2 device

> without deactivating the function for another process actually using the

> device for UVC streaming.

> 

> Signed-off-by: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>

> ---

>  drivers/usb/gadget/function/uvc.h      |  2 +

>  drivers/usb/gadget/function/uvc_v4l2.c | 57 +++++++++++++++++++++-----

>  2 files changed, 49 insertions(+), 10 deletions(-)

> 

> diff --git a/drivers/usb/gadget/function/uvc.h b/drivers/usb/gadget/function/uvc.h

> index 23ee25383c1f..deeec2b80786 100644

> --- a/drivers/usb/gadget/function/uvc.h

> +++ b/drivers/usb/gadget/function/uvc.h

> @@ -117,6 +117,7 @@ struct uvc_device {

>  	enum uvc_state state;

>  	struct usb_function func;

>  	struct uvc_video video;

> +	unsigned int connections;

>  

>  	/* Descriptors */

>  	struct {

> @@ -147,6 +148,7 @@ static inline struct uvc_device *to_uvc(struct usb_function *f)

>  struct uvc_file_handle {

>  	struct v4l2_fh vfh;

>  	struct uvc_video *device;

> +	bool connected;

>  };

>  

>  #define to_uvc_file_handle(handle) \

> diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c

> index 4ca89eab6159..c0c2588b0efb 100644

> --- a/drivers/usb/gadget/function/uvc_v4l2.c

> +++ b/drivers/usb/gadget/function/uvc_v4l2.c

> @@ -227,17 +227,60 @@ static int

>  uvc_v4l2_subscribe_event(struct v4l2_fh *fh,

>  			 const struct v4l2_event_subscription *sub)

>  {

> +	struct uvc_device *uvc = video_get_drvdata(fh->vdev);

> +	struct uvc_file_handle *handle = to_uvc_file_handle(fh);

> +	int ret;

> +

>  	if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)

>  		return -EINVAL;

>  

> -	return v4l2_event_subscribe(fh, sub, 2, NULL);

> +	if ((sub->type == UVC_EVENT_SETUP) && (uvc->connections >= 1))

> +		return -EBUSY;

> +

> +	ret = v4l2_event_subscribe(fh, sub, 2, NULL);

> +	if (ret < 0)

> +		return ret;

> +

> +	if (sub->type == UVC_EVENT_SETUP) {

> +		uvc->connections++;

> +		handle->connected = true;

> +		uvc_function_connect(uvc);

> +	}

> +

> +	return 0;

> +}

> +

> +static void uvc_v4l2_disable(struct uvc_device *uvc)

> +{

> +	if (--uvc->connections)

> +		return;

> +

> +	uvc_function_disconnect(uvc);

> +

> +	mutex_lock(&uvc->video.mutex);

> +	uvcg_video_enable(&uvc->video, 0);

> +	uvcg_free_buffers(&uvc->video.queue);

> +	mutex_unlock(&uvc->video.mutex);

>  }

>  

>  static int

>  uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,

>  			   const struct v4l2_event_subscription *sub)

>  {

> -	return v4l2_event_unsubscribe(fh, sub);

> +	struct uvc_device *uvc = video_get_drvdata(fh->vdev);

> +	struct uvc_file_handle *handle = to_uvc_file_handle(fh);

> +	int ret;

> +

> +	ret = v4l2_event_unsubscribe(fh, sub);

> +	if (ret < 0)

> +		return ret;

> +

> +	if ((sub->type == UVC_EVENT_SETUP) && handle->connected) {

> +		uvc_v4l2_disable(uvc);

> +		handle->connected = false;

> +	}

> +

> +	return 0;

>  }

>  

>  static long

> @@ -292,7 +335,6 @@ uvc_v4l2_open(struct file *file)

>  	handle->device = &uvc->video;

>  	file->private_data = &handle->vfh;

>  

> -	uvc_function_connect(uvc);

>  	return 0;

>  }

>  

> @@ -302,14 +344,9 @@ uvc_v4l2_release(struct file *file)

>  	struct video_device *vdev = video_devdata(file);

>  	struct uvc_device *uvc = video_get_drvdata(vdev);

>  	struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);

> -	struct uvc_video *video = handle->device;

> -

> -	uvc_function_disconnect(uvc);

>  

> -	mutex_lock(&video->mutex);

> -	uvcg_video_enable(video, 0);

> -	uvcg_free_buffers(&video->queue);

> -	mutex_unlock(&video->mutex);

> +	if (handle->connected)

> +		uvc_v4l2_disable(uvc);


What prevents connected from changing between the test and the next
call?

I think you need a lock somewhere, a simple integer isn't going to
protect you from anything (hint, and neither will an atomic variable...)

thanks,

greg k-h
Greg Kroah-Hartman Nov. 10, 2020, 8:40 a.m. UTC | #2
On Tue, Nov 10, 2020 at 09:25:04AM +0100, thomas.haemmerle@wolfvision.net wrote:
> From: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>
> 
> Currently, the UVC function is activated when open on the corresponding
> v4l2 device is called.
> On another open the activation of the function fails since the
> deactivation counter in `usb_function_activate` equals 0. However the
> error is not returned to userspace since the open of the v4l2 device is
> successful.
> 
> On a close the function is deactivated (since deactivation counter still
> equals 0) and the video is disabled in `uvc_v4l2_release`, although
> another process potentially is streaming.
> 
> Move activation of UVC function to subscription on UVC_EVENT_SETUP and
> keep track of the number of subscribers (limited to 1) because there we
> can guarantee for a userspace program utilizing UVC.
> Extend the `struct uvc_file_handle` with member `bool connected` to tag 
> it for a deactivation of the function.
> 
> With this a process is able to check capabilities of the v4l2 device
> without deactivating the function for another process actually using the
> device for UVC streaming.
> 
> Signed-off-by: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>
> ---
> v2:
>  - fix deadlock in `uvc_v4l2_unsubscribe_event()` (mutex is already
>    locked in v4l2-core) introduced in v1
>  - lock mutex in `uvc_v4l2_release()` to suppress ioctls and protect
>    connected
> 
>  drivers/usb/gadget/function/uvc.h      |  2 +
>  drivers/usb/gadget/function/uvc_v4l2.c | 56 +++++++++++++++++++++-----
>  2 files changed, 48 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/usb/gadget/function/uvc.h b/drivers/usb/gadget/function/uvc.h
> index 73da4f9a8d4c..0d0bcbffc8fd 100644
> --- a/drivers/usb/gadget/function/uvc.h
> +++ b/drivers/usb/gadget/function/uvc.h
> @@ -117,6 +117,7 @@ struct uvc_device {
>  	enum uvc_state state;
>  	struct usb_function func;
>  	struct uvc_video video;
> +	unsigned int connections;
>  
>  	/* Descriptors */
>  	struct {
> @@ -147,6 +148,7 @@ static inline struct uvc_device *to_uvc(struct usb_function *f)
>  struct uvc_file_handle {
>  	struct v4l2_fh vfh;
>  	struct uvc_video *device;
> +	bool connected;

What protects these two fields you are adding?

>  };
>  
>  #define to_uvc_file_handle(handle) \
> diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c
> index 67922b1355e6..aee4888e17b1 100644
> --- a/drivers/usb/gadget/function/uvc_v4l2.c
> +++ b/drivers/usb/gadget/function/uvc_v4l2.c
> @@ -228,17 +228,57 @@ static int
>  uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
>  			 const struct v4l2_event_subscription *sub)
>  {
> +	struct uvc_device *uvc = video_get_drvdata(fh->vdev);
> +	struct uvc_file_handle *handle = to_uvc_file_handle(fh);
> +	int ret;
> +
>  	if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
>  		return -EINVAL;
>  
> -	return v4l2_event_subscribe(fh, sub, 2, NULL);
> +	if ((sub->type == UVC_EVENT_SETUP) && (uvc->connections >= 1))
> +		return -EBUSY;

Are you sure you can't handle more than one connection?

If so, why is it an integer and not just a boolean?

And what prevents the value from changing right after you test it here?

> +
> +	ret = v4l2_event_subscribe(fh, sub, 2, NULL);
> +	if (ret < 0)
> +		return ret;
> +
> +	if (sub->type == UVC_EVENT_SETUP) {
> +		uvc->connections++;
> +		handle->connected = true;
> +		uvc_function_connect(uvc);
> +	}
> +
> +	return 0;
> +}
> +
> +static void uvc_v4l2_disable(struct uvc_device *uvc)
> +{
> +	if (--uvc->connections)
> +		return;
> +

What prevents "connections" from changing right after testing it here?

thanks,

greg k-h
Thomas Haemmerle Nov. 10, 2020, 9:56 a.m. UTC | #3
On 10.11.20 09:40, Greg KH wrote:
> On Tue, Nov 10, 2020 at 09:25:04AM +0100, thomas.haemmerle@wolfvision.net wrote:
>> From: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>
>>
>> Currently, the UVC function is activated when open on the corresponding
>> v4l2 device is called.
>> On another open the activation of the function fails since the
>> deactivation counter in `usb_function_activate` equals 0. However the
>> error is not returned to userspace since the open of the v4l2 device is
>> successful.
>>
>> On a close the function is deactivated (since deactivation counter still
>> equals 0) and the video is disabled in `uvc_v4l2_release`, although
>> another process potentially is streaming.
>>
>> Move activation of UVC function to subscription on UVC_EVENT_SETUP and
>> keep track of the number of subscribers (limited to 1) because there we
>> can guarantee for a userspace program utilizing UVC.
>> Extend the `struct uvc_file_handle` with member `bool connected` to tag
>> it for a deactivation of the function.
>>
>> With this a process is able to check capabilities of the v4l2 device
>> without deactivating the function for another process actually using the
>> device for UVC streaming.
>>
>> Signed-off-by: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>
>> ---
>> v2:
>>   - fix deadlock in `uvc_v4l2_unsubscribe_event()` (mutex is already
>>     locked in v4l2-core) introduced in v1
>>   - lock mutex in `uvc_v4l2_release()` to suppress ioctls and protect
>>     connected
>>
>>   drivers/usb/gadget/function/uvc.h      |  2 +
>>   drivers/usb/gadget/function/uvc_v4l2.c | 56 +++++++++++++++++++++-----
>>   2 files changed, 48 insertions(+), 10 deletions(-)
>>
>> diff --git a/drivers/usb/gadget/function/uvc.h b/drivers/usb/gadget/function/uvc.h
>> index 73da4f9a8d4c..0d0bcbffc8fd 100644
>> --- a/drivers/usb/gadget/function/uvc.h
>> +++ b/drivers/usb/gadget/function/uvc.h
>> @@ -117,6 +117,7 @@ struct uvc_device {
>>   	enum uvc_state state;
>>   	struct usb_function func;
>>   	struct uvc_video video;
>> +	unsigned int connections;
>>   
>>   	/* Descriptors */
>>   	struct {
>> @@ -147,6 +148,7 @@ static inline struct uvc_device *to_uvc(struct usb_function *f)
>>   struct uvc_file_handle {
>>   	struct v4l2_fh vfh;
>>   	struct uvc_video *device;
>> +	bool connected;
> 
> What protects these two fields you are adding?

The mutex in `struct uvc_video`.  The lock in video_device is set to it 
in `uvc_register_video()` in f_uvc.c.

> 
>>   };
>>   
>>   #define to_uvc_file_handle(handle) \
>> diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c
>> index 67922b1355e6..aee4888e17b1 100644
>> --- a/drivers/usb/gadget/function/uvc_v4l2.c
>> +++ b/drivers/usb/gadget/function/uvc_v4l2.c
>> @@ -228,17 +228,57 @@ static int
>>   uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
>>   			 const struct v4l2_event_subscription *sub)
>>   {
>> +	struct uvc_device *uvc = video_get_drvdata(fh->vdev);
>> +	struct uvc_file_handle *handle = to_uvc_file_handle(fh);
>> +	int ret;
>> +
>>   	if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
>>   		return -EINVAL;
>>   
>> -	return v4l2_event_subscribe(fh, sub, 2, NULL);
>> +	if ((sub->type == UVC_EVENT_SETUP) && (uvc->connections >= 1))
>> +		return -EBUSY;
> 
> Are you sure you can't handle more than one connection?
> 
> If so, why is it an integer and not just a boolean?

It is possible to handle more than one subscription but I can't think of 
a scenario where this would make sense.
For the reason that it is basically possible I decided for integer.

> 
> And what prevents the value from changing right after you test it here?
> 

The ioctl here are serialized, because the mutex pointer in the video 
device is set.

>> +
>> +	ret = v4l2_event_subscribe(fh, sub, 2, NULL);
>> +	if (ret < 0)
>> +		return ret;
>> +
>> +	if (sub->type == UVC_EVENT_SETUP) {
>> +		uvc->connections++;
>> +		handle->connected = true;
>> +		uvc_function_connect(uvc);
>> +	}
>> +
>> +	return 0;
>> +}
>> +
>> +static void uvc_v4l2_disable(struct uvc_device *uvc)
>> +{
>> +	if (--uvc->connections)
>> +		return;
>> +
> 
> What prevents "connections" from changing right after testing it here?

The function is either called from ioctl `uvc_v4l2_unsubscribe_event()` 
or from `uvc_v4l2_release()`. In both cases the video mutex is locked.

Thomas
Greg Kroah-Hartman Nov. 10, 2020, 10:06 a.m. UTC | #4
On Tue, Nov 10, 2020 at 10:56:05AM +0100, Thomas Hämmerle wrote:
> On 10.11.20 09:40, Greg KH wrote:

> > On Tue, Nov 10, 2020 at 09:25:04AM +0100, thomas.haemmerle@wolfvision.net wrote:

> > > From: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>

> > > 

> > > Currently, the UVC function is activated when open on the corresponding

> > > v4l2 device is called.

> > > On another open the activation of the function fails since the

> > > deactivation counter in `usb_function_activate` equals 0. However the

> > > error is not returned to userspace since the open of the v4l2 device is

> > > successful.

> > > 

> > > On a close the function is deactivated (since deactivation counter still

> > > equals 0) and the video is disabled in `uvc_v4l2_release`, although

> > > another process potentially is streaming.

> > > 

> > > Move activation of UVC function to subscription on UVC_EVENT_SETUP and

> > > keep track of the number of subscribers (limited to 1) because there we

> > > can guarantee for a userspace program utilizing UVC.

> > > Extend the `struct uvc_file_handle` with member `bool connected` to tag

> > > it for a deactivation of the function.

> > > 

> > > With this a process is able to check capabilities of the v4l2 device

> > > without deactivating the function for another process actually using the

> > > device for UVC streaming.

> > > 

> > > Signed-off-by: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>

> > > ---

> > > v2:

> > >   - fix deadlock in `uvc_v4l2_unsubscribe_event()` (mutex is already

> > >     locked in v4l2-core) introduced in v1

> > >   - lock mutex in `uvc_v4l2_release()` to suppress ioctls and protect

> > >     connected

> > > 

> > >   drivers/usb/gadget/function/uvc.h      |  2 +

> > >   drivers/usb/gadget/function/uvc_v4l2.c | 56 +++++++++++++++++++++-----

> > >   2 files changed, 48 insertions(+), 10 deletions(-)

> > > 

> > > diff --git a/drivers/usb/gadget/function/uvc.h b/drivers/usb/gadget/function/uvc.h

> > > index 73da4f9a8d4c..0d0bcbffc8fd 100644

> > > --- a/drivers/usb/gadget/function/uvc.h

> > > +++ b/drivers/usb/gadget/function/uvc.h

> > > @@ -117,6 +117,7 @@ struct uvc_device {

> > >   	enum uvc_state state;

> > >   	struct usb_function func;

> > >   	struct uvc_video video;

> > > +	unsigned int connections;

> > >   	/* Descriptors */

> > >   	struct {

> > > @@ -147,6 +148,7 @@ static inline struct uvc_device *to_uvc(struct usb_function *f)

> > >   struct uvc_file_handle {

> > >   	struct v4l2_fh vfh;

> > >   	struct uvc_video *device;

> > > +	bool connected;

> > 

> > What protects these two fields you are adding?

> 

> The mutex in `struct uvc_video`.  The lock in video_device is set to it in

> `uvc_register_video()` in f_uvc.c.

> 

> > 

> > >   };

> > >   #define to_uvc_file_handle(handle) \

> > > diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c

> > > index 67922b1355e6..aee4888e17b1 100644

> > > --- a/drivers/usb/gadget/function/uvc_v4l2.c

> > > +++ b/drivers/usb/gadget/function/uvc_v4l2.c

> > > @@ -228,17 +228,57 @@ static int

> > >   uvc_v4l2_subscribe_event(struct v4l2_fh *fh,

> > >   			 const struct v4l2_event_subscription *sub)

> > >   {

> > > +	struct uvc_device *uvc = video_get_drvdata(fh->vdev);

> > > +	struct uvc_file_handle *handle = to_uvc_file_handle(fh);

> > > +	int ret;

> > > +

> > >   	if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)

> > >   		return -EINVAL;

> > > -	return v4l2_event_subscribe(fh, sub, 2, NULL);

> > > +	if ((sub->type == UVC_EVENT_SETUP) && (uvc->connections >= 1))

> > > +		return -EBUSY;

> > 

> > Are you sure you can't handle more than one connection?

> > 

> > If so, why is it an integer and not just a boolean?

> 

> It is possible to handle more than one subscription but I can't think of a

> scenario where this would make sense.

> For the reason that it is basically possible I decided for integer.


If it is not possible, just make it a bool so that we all can instantly
see that this is the case.  If it needs to be changed in the future,
make that change then.

thanks,

greg k-h
Thomas Haemmerle Nov. 10, 2020, 11:53 a.m. UTC | #5
On 10.11.20 11:31, Hans Verkuil wrote:
> On 10/11/2020 09:25, thomas.haemmerle@wolfvision.net wrote:

>> From: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>

>>

>> Currently, the UVC function is activated when open on the corresponding

>> v4l2 device is called.

>> On another open the activation of the function fails since the

>> deactivation counter in `usb_function_activate` equals 0. However the

>> error is not returned to userspace since the open of the v4l2 device is

>> successful.

>>

>> On a close the function is deactivated (since deactivation counter still

>> equals 0) and the video is disabled in `uvc_v4l2_release`, although

>> another process potentially is streaming.

>>

>> Move activation of UVC function to subscription on UVC_EVENT_SETUP and

>> keep track of the number of subscribers (limited to 1) because there we

>> can guarantee for a userspace program utilizing UVC.

>> Extend the `struct uvc_file_handle` with member `bool connected` to tag

>> it for a deactivation of the function.

>>

>> With this a process is able to check capabilities of the v4l2 device

>> without deactivating the function for another process actually using the

>> device for UVC streaming.

>>

>> Signed-off-by: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>

>> ---

>> v2:

>>   - fix deadlock in `uvc_v4l2_unsubscribe_event()` (mutex is already

>>     locked in v4l2-core) introduced in v1

>>   - lock mutex in `uvc_v4l2_release()` to suppress ioctls and protect

>>     connected

>>

>>   drivers/usb/gadget/function/uvc.h      |  2 +

>>   drivers/usb/gadget/function/uvc_v4l2.c | 56 +++++++++++++++++++++-----

>>   2 files changed, 48 insertions(+), 10 deletions(-)

>>

>> diff --git a/drivers/usb/gadget/function/uvc.h b/drivers/usb/gadget/function/uvc.h

>> index 73da4f9a8d4c..0d0bcbffc8fd 100644

>> --- a/drivers/usb/gadget/function/uvc.h

>> +++ b/drivers/usb/gadget/function/uvc.h

>> @@ -117,6 +117,7 @@ struct uvc_device {

>>   	enum uvc_state state;

>>   	struct usb_function func;

>>   	struct uvc_video video;

>> +	unsigned int connections;

>>   

>>   	/* Descriptors */

>>   	struct {

>> @@ -147,6 +148,7 @@ static inline struct uvc_device *to_uvc(struct usb_function *f)

>>   struct uvc_file_handle {

>>   	struct v4l2_fh vfh;

>>   	struct uvc_video *device;

>> +	bool connected;

>>   };

>>   

>>   #define to_uvc_file_handle(handle) \

>> diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c

>> index 67922b1355e6..aee4888e17b1 100644

>> --- a/drivers/usb/gadget/function/uvc_v4l2.c

>> +++ b/drivers/usb/gadget/function/uvc_v4l2.c

>> @@ -228,17 +228,57 @@ static int

>>   uvc_v4l2_subscribe_event(struct v4l2_fh *fh,

>>   			 const struct v4l2_event_subscription *sub)

>>   {

>> +	struct uvc_device *uvc = video_get_drvdata(fh->vdev);

>> +	struct uvc_file_handle *handle = to_uvc_file_handle(fh);

>> +	int ret;

>> +

>>   	if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)

>>   		return -EINVAL;

>>   

>> -	return v4l2_event_subscribe(fh, sub, 2, NULL);

>> +	if ((sub->type == UVC_EVENT_SETUP) && (uvc->connections >= 1))

>> +		return -EBUSY;

>> +

>> +	ret = v4l2_event_subscribe(fh, sub, 2, NULL);

>> +	if (ret < 0)

>> +		return ret;

>> +

>> +	if (sub->type == UVC_EVENT_SETUP) {

>> +		uvc->connections++;

>> +		handle->connected = true;

>> +		uvc_function_connect(uvc);

>> +	}

> 

> This makes no sense. Why would subscribing to a SETUP event

> mean that you are 'connected'?


Subscribing to a SETUP does not mean that you are connected - on a 
subscription to SETUP we can expect that there is a userspace process, 
utilizing UVC -- which is required for the UVC gadget function -- and 
everything is ready to enable the function by calling uvc_function_connect.
How about calling it `function_connected`?

> 

> It should be possible to open a V4L2 device node any number of times,

> and any filehandle can subscribe to any event, but typically once

> userspace allocates buffers (VIDIOC_REQBUFS or VIDIOC_CREATE_BUFS)

> then that filehandle is marked as the owner of the device and other

> open filehandles are no longer allowed to allocate buffers or stream video.


Sure - this can be also done if userspace allocates buffers.
But why does it make more sense to call uvc_function_connect on 
VIDIOC_REQBUFS or VIDIOC_CREATE_BUFS instead of subscribtion to a UVC event?

> 

> See e.g. drivers/media/common/videobuf2/videobuf2-v4l2.c

> and vb2_ioctl_reqbufs and other vb2_ioctl_* functions.

> 

> Unfortunately this UVC gadget driver is rather old and is not using

> these helper functions.

> 

> Running 'v4l2-compliance' will likely fail on a lot of tests for this

> driver.

> 

> This driver probably could use some TLC.


I totally agree with that, however this change fixes at least one test 
of 'v4l2-compliance'.
Currently a running UVC connection can be corrupted by another process 
which just opens and closes the device.

Thomas

> 

> Regards,

> 

> 	Hans

> 

>> +

>> +	return 0;

>> +}

>> +

>> +static void uvc_v4l2_disable(struct uvc_device *uvc)

>> +{

>> +	if (--uvc->connections)

>> +		return;

>> +

>> +	uvc_function_disconnect(uvc);

>> +	uvcg_video_enable(&uvc->video, 0);

>> +	uvcg_free_buffers(&uvc->video.queue);

>>   }

>>   

>>   static int

>>   uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,

>>   			   const struct v4l2_event_subscription *sub)

>>   {

>> -	return v4l2_event_unsubscribe(fh, sub);

>> +	struct uvc_device *uvc = video_get_drvdata(fh->vdev);

>> +	struct uvc_file_handle *handle = to_uvc_file_handle(fh);

>> +	int ret;

>> +

>> +	ret = v4l2_event_unsubscribe(fh, sub);

>> +	if (ret < 0)

>> +		return ret;

>> +

>> +	if ((sub->type == UVC_EVENT_SETUP) && handle->connected) {

>> +		uvc_v4l2_disable(uvc);

>> +		handle->connected = false;

>> +	}

>> +

>> +	return 0;

>>   }

>>   

>>   static long

>> @@ -293,7 +333,6 @@ uvc_v4l2_open(struct file *file)

>>   	handle->device = &uvc->video;

>>   	file->private_data = &handle->vfh;

>>   

>> -	uvc_function_connect(uvc);

>>   	return 0;

>>   }

>>   

>> @@ -303,14 +342,11 @@ uvc_v4l2_release(struct file *file)

>>   	struct video_device *vdev = video_devdata(file);

>>   	struct uvc_device *uvc = video_get_drvdata(vdev);

>>   	struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);

>> -	struct uvc_video *video = handle->device;

>> -

>> -	uvc_function_disconnect(uvc);

>>   

>> -	mutex_lock(&video->mutex);

>> -	uvcg_video_enable(video, 0);

>> -	uvcg_free_buffers(&video->queue);

>> -	mutex_unlock(&video->mutex);

>> +	mutex_lock(&uvc->video.mutex);

>> +	if (handle->connected)

>> +		uvc_v4l2_disable(uvc);

>> +	mutex_unlock(&uvc->video.mutex);

>>   

>>   	file->private_data = NULL;

>>   	v4l2_fh_del(&handle->vfh);

>>

>
Hans Verkuil Nov. 10, 2020, 2:43 p.m. UTC | #6
On 10/11/2020 12:53, Thomas Hämmerle wrote:
> On 10.11.20 11:31, Hans Verkuil wrote:
>> On 10/11/2020 09:25, thomas.haemmerle@wolfvision.net wrote:
>>> From: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>
>>>
>>> Currently, the UVC function is activated when open on the corresponding
>>> v4l2 device is called.
>>> On another open the activation of the function fails since the
>>> deactivation counter in `usb_function_activate` equals 0. However the
>>> error is not returned to userspace since the open of the v4l2 device is
>>> successful.
>>>
>>> On a close the function is deactivated (since deactivation counter still
>>> equals 0) and the video is disabled in `uvc_v4l2_release`, although
>>> another process potentially is streaming.
>>>
>>> Move activation of UVC function to subscription on UVC_EVENT_SETUP and
>>> keep track of the number of subscribers (limited to 1) because there we
>>> can guarantee for a userspace program utilizing UVC.
>>> Extend the `struct uvc_file_handle` with member `bool connected` to tag
>>> it for a deactivation of the function.
>>>
>>> With this a process is able to check capabilities of the v4l2 device
>>> without deactivating the function for another process actually using the
>>> device for UVC streaming.
>>>
>>> Signed-off-by: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>
>>> ---
>>> v2:
>>>   - fix deadlock in `uvc_v4l2_unsubscribe_event()` (mutex is already
>>>     locked in v4l2-core) introduced in v1
>>>   - lock mutex in `uvc_v4l2_release()` to suppress ioctls and protect
>>>     connected
>>>
>>>   drivers/usb/gadget/function/uvc.h      |  2 +
>>>   drivers/usb/gadget/function/uvc_v4l2.c | 56 +++++++++++++++++++++-----
>>>   2 files changed, 48 insertions(+), 10 deletions(-)
>>>
>>> diff --git a/drivers/usb/gadget/function/uvc.h b/drivers/usb/gadget/function/uvc.h
>>> index 73da4f9a8d4c..0d0bcbffc8fd 100644
>>> --- a/drivers/usb/gadget/function/uvc.h
>>> +++ b/drivers/usb/gadget/function/uvc.h
>>> @@ -117,6 +117,7 @@ struct uvc_device {
>>>   	enum uvc_state state;
>>>   	struct usb_function func;
>>>   	struct uvc_video video;
>>> +	unsigned int connections;
>>>   
>>>   	/* Descriptors */
>>>   	struct {
>>> @@ -147,6 +148,7 @@ static inline struct uvc_device *to_uvc(struct usb_function *f)
>>>   struct uvc_file_handle {
>>>   	struct v4l2_fh vfh;
>>>   	struct uvc_video *device;
>>> +	bool connected;
>>>   };
>>>   
>>>   #define to_uvc_file_handle(handle) \
>>> diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c
>>> index 67922b1355e6..aee4888e17b1 100644
>>> --- a/drivers/usb/gadget/function/uvc_v4l2.c
>>> +++ b/drivers/usb/gadget/function/uvc_v4l2.c
>>> @@ -228,17 +228,57 @@ static int
>>>   uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
>>>   			 const struct v4l2_event_subscription *sub)
>>>   {
>>> +	struct uvc_device *uvc = video_get_drvdata(fh->vdev);
>>> +	struct uvc_file_handle *handle = to_uvc_file_handle(fh);
>>> +	int ret;
>>> +
>>>   	if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
>>>   		return -EINVAL;
>>>   
>>> -	return v4l2_event_subscribe(fh, sub, 2, NULL);
>>> +	if ((sub->type == UVC_EVENT_SETUP) && (uvc->connections >= 1))
>>> +		return -EBUSY;
>>> +
>>> +	ret = v4l2_event_subscribe(fh, sub, 2, NULL);
>>> +	if (ret < 0)
>>> +		return ret;
>>> +
>>> +	if (sub->type == UVC_EVENT_SETUP) {
>>> +		uvc->connections++;
>>> +		handle->connected = true;
>>> +		uvc_function_connect(uvc);
>>> +	}
>>
>> This makes no sense. Why would subscribing to a SETUP event
>> mean that you are 'connected'?
> 
> Subscribing to a SETUP does not mean that you are connected - on a 
> subscription to SETUP we can expect that there is a userspace process, 
> utilizing UVC -- which is required for the UVC gadget function -- and 
> everything is ready to enable the function by calling uvc_function_connect.
> How about calling it `function_connected`?

Any application can open the device node and subscribe to this event.
This is not a good place to call uvc_function_connect(), it's an abuse of
the V4L2 API.

I dug a bit more into this (I have very little gadget driver experience),
and isn't calling uvc_function_connect() something that should be done
on the first open? And when the last filehandle is closed, then
uvc_function_disconnect() can be called to clean up?

That would make much more sense. Grep for v4l2_fh_is_singular_file() on how
to do this, quite a few media drivers need this.

Regards,

	Hans

> 
>>
>> It should be possible to open a V4L2 device node any number of times,
>> and any filehandle can subscribe to any event, but typically once
>> userspace allocates buffers (VIDIOC_REQBUFS or VIDIOC_CREATE_BUFS)
>> then that filehandle is marked as the owner of the device and other
>> open filehandles are no longer allowed to allocate buffers or stream video.
> 
> Sure - this can be also done if userspace allocates buffers.
> But why does it make more sense to call uvc_function_connect on 
> VIDIOC_REQBUFS or VIDIOC_CREATE_BUFS instead of subscribtion to a UVC event?
> 
>>
>> See e.g. drivers/media/common/videobuf2/videobuf2-v4l2.c
>> and vb2_ioctl_reqbufs and other vb2_ioctl_* functions.
>>
>> Unfortunately this UVC gadget driver is rather old and is not using
>> these helper functions.
>>
>> Running 'v4l2-compliance' will likely fail on a lot of tests for this
>> driver.
>>
>> This driver probably could use some TLC.
> 
> I totally agree with that, however this change fixes at least one test 
> of 'v4l2-compliance'.
> Currently a running UVC connection can be corrupted by another process 
> which just opens and closes the device.
> 
> Thomas
> 
>>
>> Regards,
>>
>> 	Hans
>>
>>> +
>>> +	return 0;
>>> +}
>>> +
>>> +static void uvc_v4l2_disable(struct uvc_device *uvc)
>>> +{
>>> +	if (--uvc->connections)
>>> +		return;
>>> +
>>> +	uvc_function_disconnect(uvc);
>>> +	uvcg_video_enable(&uvc->video, 0);
>>> +	uvcg_free_buffers(&uvc->video.queue);
>>>   }
>>>   
>>>   static int
>>>   uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,
>>>   			   const struct v4l2_event_subscription *sub)
>>>   {
>>> -	return v4l2_event_unsubscribe(fh, sub);
>>> +	struct uvc_device *uvc = video_get_drvdata(fh->vdev);
>>> +	struct uvc_file_handle *handle = to_uvc_file_handle(fh);
>>> +	int ret;
>>> +
>>> +	ret = v4l2_event_unsubscribe(fh, sub);
>>> +	if (ret < 0)
>>> +		return ret;
>>> +
>>> +	if ((sub->type == UVC_EVENT_SETUP) && handle->connected) {
>>> +		uvc_v4l2_disable(uvc);
>>> +		handle->connected = false;
>>> +	}
>>> +
>>> +	return 0;
>>>   }
>>>   
>>>   static long
>>> @@ -293,7 +333,6 @@ uvc_v4l2_open(struct file *file)
>>>   	handle->device = &uvc->video;
>>>   	file->private_data = &handle->vfh;
>>>   
>>> -	uvc_function_connect(uvc);
>>>   	return 0;
>>>   }
>>>   
>>> @@ -303,14 +342,11 @@ uvc_v4l2_release(struct file *file)
>>>   	struct video_device *vdev = video_devdata(file);
>>>   	struct uvc_device *uvc = video_get_drvdata(vdev);
>>>   	struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
>>> -	struct uvc_video *video = handle->device;
>>> -
>>> -	uvc_function_disconnect(uvc);
>>>   
>>> -	mutex_lock(&video->mutex);
>>> -	uvcg_video_enable(video, 0);
>>> -	uvcg_free_buffers(&video->queue);
>>> -	mutex_unlock(&video->mutex);
>>> +	mutex_lock(&uvc->video.mutex);
>>> +	if (handle->connected)
>>> +		uvc_v4l2_disable(uvc);
>>> +	mutex_unlock(&uvc->video.mutex);
>>>   
>>>   	file->private_data = NULL;
>>>   	v4l2_fh_del(&handle->vfh);
>>>
>>
Hans Verkuil Nov. 10, 2020, 3:36 p.m. UTC | #7
On 10/11/2020 16:10, Thomas Hämmerle wrote:
> On 10.11.20 15:43, Hans Verkuil wrote:

>> On 10/11/2020 12:53, Thomas Hämmerle wrote:

>>> On 10.11.20 11:31, Hans Verkuil wrote:

>>>> On 10/11/2020 09:25, thomas.haemmerle@wolfvision.net wrote:

>>>>> From: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>

>>>>>

>>>>> Currently, the UVC function is activated when open on the corresponding

>>>>> v4l2 device is called.

>>>>> On another open the activation of the function fails since the

>>>>> deactivation counter in `usb_function_activate` equals 0. However the

>>>>> error is not returned to userspace since the open of the v4l2 device is

>>>>> successful.

>>>>>

>>>>> On a close the function is deactivated (since deactivation counter still

>>>>> equals 0) and the video is disabled in `uvc_v4l2_release`, although

>>>>> another process potentially is streaming.

>>>>>

>>>>> Move activation of UVC function to subscription on UVC_EVENT_SETUP and

>>>>> keep track of the number of subscribers (limited to 1) because there we

>>>>> can guarantee for a userspace program utilizing UVC.

>>>>> Extend the `struct uvc_file_handle` with member `bool connected` to tag

>>>>> it for a deactivation of the function.

>>>>>

>>>>> With this a process is able to check capabilities of the v4l2 device

>>>>> without deactivating the function for another process actually using the

>>>>> device for UVC streaming.

>>>>>

>>>>> Signed-off-by: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>

>>>>> ---

>>>>> v2:

>>>>>    - fix deadlock in `uvc_v4l2_unsubscribe_event()` (mutex is already

>>>>>      locked in v4l2-core) introduced in v1

>>>>>    - lock mutex in `uvc_v4l2_release()` to suppress ioctls and protect

>>>>>      connected

>>>>>

>>>>>    drivers/usb/gadget/function/uvc.h      |  2 +

>>>>>    drivers/usb/gadget/function/uvc_v4l2.c | 56 +++++++++++++++++++++-----

>>>>>    2 files changed, 48 insertions(+), 10 deletions(-)

>>>>>

>>>>> diff --git a/drivers/usb/gadget/function/uvc.h b/drivers/usb/gadget/function/uvc.h

>>>>> index 73da4f9a8d4c..0d0bcbffc8fd 100644

>>>>> --- a/drivers/usb/gadget/function/uvc.h

>>>>> +++ b/drivers/usb/gadget/function/uvc.h

>>>>> @@ -117,6 +117,7 @@ struct uvc_device {

>>>>>    	enum uvc_state state;

>>>>>    	struct usb_function func;

>>>>>    	struct uvc_video video;

>>>>> +	unsigned int connections;

>>>>>    

>>>>>    	/* Descriptors */

>>>>>    	struct {

>>>>> @@ -147,6 +148,7 @@ static inline struct uvc_device *to_uvc(struct usb_function *f)

>>>>>    struct uvc_file_handle {

>>>>>    	struct v4l2_fh vfh;

>>>>>    	struct uvc_video *device;

>>>>> +	bool connected;

>>>>>    };

>>>>>    

>>>>>    #define to_uvc_file_handle(handle) \

>>>>> diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c

>>>>> index 67922b1355e6..aee4888e17b1 100644

>>>>> --- a/drivers/usb/gadget/function/uvc_v4l2.c

>>>>> +++ b/drivers/usb/gadget/function/uvc_v4l2.c

>>>>> @@ -228,17 +228,57 @@ static int

>>>>>    uvc_v4l2_subscribe_event(struct v4l2_fh *fh,

>>>>>    			 const struct v4l2_event_subscription *sub)

>>>>>    {

>>>>> +	struct uvc_device *uvc = video_get_drvdata(fh->vdev);

>>>>> +	struct uvc_file_handle *handle = to_uvc_file_handle(fh);

>>>>> +	int ret;

>>>>> +

>>>>>    	if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)

>>>>>    		return -EINVAL;

>>>>>    

>>>>> -	return v4l2_event_subscribe(fh, sub, 2, NULL);

>>>>> +	if ((sub->type == UVC_EVENT_SETUP) && (uvc->connections >= 1))

>>>>> +		return -EBUSY;

>>>>> +

>>>>> +	ret = v4l2_event_subscribe(fh, sub, 2, NULL);

>>>>> +	if (ret < 0)

>>>>> +		return ret;

>>>>> +

>>>>> +	if (sub->type == UVC_EVENT_SETUP) {

>>>>> +		uvc->connections++;

>>>>> +		handle->connected = true;

>>>>> +		uvc_function_connect(uvc);

>>>>> +	}

>>>>

>>>> This makes no sense. Why would subscribing to a SETUP event

>>>> mean that you are 'connected'?

>>>

>>> Subscribing to a SETUP does not mean that you are connected - on a

>>> subscription to SETUP we can expect that there is a userspace process,

>>> utilizing UVC -- which is required for the UVC gadget function -- and

>>> everything is ready to enable the function by calling uvc_function_connect.

>>> How about calling it `function_connected`?

>>

>> Any application can open the device node and subscribe to this event.

>> This is not a good place to call uvc_function_connect(), it's an abuse of

>> the V4L2 API.

> 

> Of course - any application can subscribe to this event but since the 

> event is defined in g_uvc.h I thought that a subscription is most likely 

> done by a UVC application.

> 

>>

>> I dug a bit more into this (I have very little gadget driver experience),

>> and isn't calling uvc_function_connect() something that should be done

>> on the first open? And when the last filehandle is closed, then

>> uvc_function_disconnect() can be called to clean up?

> 

> Isn't the possibility of the delayed activation of the usb function in 

> composite.c intended to wait for a corresponding userspace application, 

> which is required to make the gadget work?

> 

> If so, a simple open of the v4l2 device does not mean that the gadget is 

> ready to go: starting pipewire for example results in quering the device 

> capabilities of all devices. But this does not mean that the gadget will 

> work.

> Therefore I was looking for a place, where we can expect, that the 

> application opened the device will utilize UVC and found that a 

> subscription to a UVC specific event is the right place.


That's why I suggested to do this when buffers are allocated. That's when
the application commits resources to actually use the device. Until that
point everything that an application does is just querying or configuring,
but not actually using it.

BTW, Laurent Pinchart who maintains this driver is away for a week, so he
might know more about this when he comes back.

Regards,

	Hans

> 

> Regards,

> Thomas

> 

>>

>> That would make much more sense. Grep for v4l2_fh_is_singular_file() on how

>> to do this, quite a few media drivers need this.

>>

>> Regards,

>>

>> 	Hans

>>

>>>

>>>>

>>>> It should be possible to open a V4L2 device node any number of times,

>>>> and any filehandle can subscribe to any event, but typically once

>>>> userspace allocates buffers (VIDIOC_REQBUFS or VIDIOC_CREATE_BUFS)

>>>> then that filehandle is marked as the owner of the device and other

>>>> open filehandles are no longer allowed to allocate buffers or stream video.

>>>

>>> Sure - this can be also done if userspace allocates buffers.

>>> But why does it make more sense to call uvc_function_connect on

>>> VIDIOC_REQBUFS or VIDIOC_CREATE_BUFS instead of subscribtion to a UVC event?

>>>

>>>>

>>>> See e.g. drivers/media/common/videobuf2/videobuf2-v4l2.c

>>>> and vb2_ioctl_reqbufs and other vb2_ioctl_* functions.

>>>>

>>>> Unfortunately this UVC gadget driver is rather old and is not using

>>>> these helper functions.

>>>>

>>>> Running 'v4l2-compliance' will likely fail on a lot of tests for this

>>>> driver.

>>>>

>>>> This driver probably could use some TLC.

>>>

>>> I totally agree with that, however this change fixes at least one test

>>> of 'v4l2-compliance'.

>>> Currently a running UVC connection can be corrupted by another process

>>> which just opens and closes the device.

>>>

>>> Thomas

>>>

>>>>

>>>> Regards,

>>>>

>>>> 	Hans

>>>>

>>>>> +

>>>>> +	return 0;

>>>>> +}

>>>>> +

>>>>> +static void uvc_v4l2_disable(struct uvc_device *uvc)

>>>>> +{

>>>>> +	if (--uvc->connections)

>>>>> +		return;

>>>>> +

>>>>> +	uvc_function_disconnect(uvc);

>>>>> +	uvcg_video_enable(&uvc->video, 0);

>>>>> +	uvcg_free_buffers(&uvc->video.queue);

>>>>>    }

>>>>>    

>>>>>    static int

>>>>>    uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,

>>>>>    			   const struct v4l2_event_subscription *sub)

>>>>>    {

>>>>> -	return v4l2_event_unsubscribe(fh, sub);

>>>>> +	struct uvc_device *uvc = video_get_drvdata(fh->vdev);

>>>>> +	struct uvc_file_handle *handle = to_uvc_file_handle(fh);

>>>>> +	int ret;

>>>>> +

>>>>> +	ret = v4l2_event_unsubscribe(fh, sub);

>>>>> +	if (ret < 0)

>>>>> +		return ret;

>>>>> +

>>>>> +	if ((sub->type == UVC_EVENT_SETUP) && handle->connected) {

>>>>> +		uvc_v4l2_disable(uvc);

>>>>> +		handle->connected = false;

>>>>> +	}

>>>>> +

>>>>> +	return 0;

>>>>>    }

>>>>>    

>>>>>    static long

>>>>> @@ -293,7 +333,6 @@ uvc_v4l2_open(struct file *file)

>>>>>    	handle->device = &uvc->video;

>>>>>    	file->private_data = &handle->vfh;

>>>>>    

>>>>> -	uvc_function_connect(uvc);

>>>>>    	return 0;

>>>>>    }

>>>>>    

>>>>> @@ -303,14 +342,11 @@ uvc_v4l2_release(struct file *file)

>>>>>    	struct video_device *vdev = video_devdata(file);

>>>>>    	struct uvc_device *uvc = video_get_drvdata(vdev);

>>>>>    	struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);

>>>>> -	struct uvc_video *video = handle->device;

>>>>> -

>>>>> -	uvc_function_disconnect(uvc);

>>>>>    

>>>>> -	mutex_lock(&video->mutex);

>>>>> -	uvcg_video_enable(video, 0);

>>>>> -	uvcg_free_buffers(&video->queue);

>>>>> -	mutex_unlock(&video->mutex);

>>>>> +	mutex_lock(&uvc->video.mutex);

>>>>> +	if (handle->connected)

>>>>> +		uvc_v4l2_disable(uvc);

>>>>> +	mutex_unlock(&uvc->video.mutex);

>>>>>    

>>>>>    	file->private_data = NULL;

>>>>>    	v4l2_fh_del(&handle->vfh);

>>>>>

>>>>

>>
Hans Verkuil Nov. 10, 2020, 4:01 p.m. UTC | #8
On 10/11/2020 16:50, Thomas Hämmerle wrote:
> On 10.11.20 16:36, Hans Verkuil wrote:

>> On 10/11/2020 16:10, Thomas Hämmerle wrote:

>>> On 10.11.20 15:43, Hans Verkuil wrote:

>>>> On 10/11/2020 12:53, Thomas Hämmerle wrote:

>>>>> On 10.11.20 11:31, Hans Verkuil wrote:

>>>>>> On 10/11/2020 09:25, thomas.haemmerle@wolfvision.net wrote:

>>>>>>> From: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>

>>>>>>>

>>>>>>> Currently, the UVC function is activated when open on the corresponding

>>>>>>> v4l2 device is called.

>>>>>>> On another open the activation of the function fails since the

>>>>>>> deactivation counter in `usb_function_activate` equals 0. However the

>>>>>>> error is not returned to userspace since the open of the v4l2 device is

>>>>>>> successful.

>>>>>>>

>>>>>>> On a close the function is deactivated (since deactivation counter still

>>>>>>> equals 0) and the video is disabled in `uvc_v4l2_release`, although

>>>>>>> another process potentially is streaming.

>>>>>>>

>>>>>>> Move activation of UVC function to subscription on UVC_EVENT_SETUP and

>>>>>>> keep track of the number of subscribers (limited to 1) because there we

>>>>>>> can guarantee for a userspace program utilizing UVC.

>>>>>>> Extend the `struct uvc_file_handle` with member `bool connected` to tag

>>>>>>> it for a deactivation of the function.

>>>>>>>

>>>>>>> With this a process is able to check capabilities of the v4l2 device

>>>>>>> without deactivating the function for another process actually using the

>>>>>>> device for UVC streaming.

>>>>>>>

>>>>>>> Signed-off-by: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>

>>>>>>> ---

>>>>>>> v2:

>>>>>>>     - fix deadlock in `uvc_v4l2_unsubscribe_event()` (mutex is already

>>>>>>>       locked in v4l2-core) introduced in v1

>>>>>>>     - lock mutex in `uvc_v4l2_release()` to suppress ioctls and protect

>>>>>>>       connected

>>>>>>>

>>>>>>>     drivers/usb/gadget/function/uvc.h      |  2 +

>>>>>>>     drivers/usb/gadget/function/uvc_v4l2.c | 56 +++++++++++++++++++++-----

>>>>>>>     2 files changed, 48 insertions(+), 10 deletions(-)

>>>>>>>

>>>>>>> diff --git a/drivers/usb/gadget/function/uvc.h b/drivers/usb/gadget/function/uvc.h

>>>>>>> index 73da4f9a8d4c..0d0bcbffc8fd 100644

>>>>>>> --- a/drivers/usb/gadget/function/uvc.h

>>>>>>> +++ b/drivers/usb/gadget/function/uvc.h

>>>>>>> @@ -117,6 +117,7 @@ struct uvc_device {

>>>>>>>     	enum uvc_state state;

>>>>>>>     	struct usb_function func;

>>>>>>>     	struct uvc_video video;

>>>>>>> +	unsigned int connections;

>>>>>>>     

>>>>>>>     	/* Descriptors */

>>>>>>>     	struct {

>>>>>>> @@ -147,6 +148,7 @@ static inline struct uvc_device *to_uvc(struct usb_function *f)

>>>>>>>     struct uvc_file_handle {

>>>>>>>     	struct v4l2_fh vfh;

>>>>>>>     	struct uvc_video *device;

>>>>>>> +	bool connected;

>>>>>>>     };

>>>>>>>     

>>>>>>>     #define to_uvc_file_handle(handle) \

>>>>>>> diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c

>>>>>>> index 67922b1355e6..aee4888e17b1 100644

>>>>>>> --- a/drivers/usb/gadget/function/uvc_v4l2.c

>>>>>>> +++ b/drivers/usb/gadget/function/uvc_v4l2.c

>>>>>>> @@ -228,17 +228,57 @@ static int

>>>>>>>     uvc_v4l2_subscribe_event(struct v4l2_fh *fh,

>>>>>>>     			 const struct v4l2_event_subscription *sub)

>>>>>>>     {

>>>>>>> +	struct uvc_device *uvc = video_get_drvdata(fh->vdev);

>>>>>>> +	struct uvc_file_handle *handle = to_uvc_file_handle(fh);

>>>>>>> +	int ret;

>>>>>>> +

>>>>>>>     	if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)

>>>>>>>     		return -EINVAL;

>>>>>>>     

>>>>>>> -	return v4l2_event_subscribe(fh, sub, 2, NULL);

>>>>>>> +	if ((sub->type == UVC_EVENT_SETUP) && (uvc->connections >= 1))

>>>>>>> +		return -EBUSY;

>>>>>>> +

>>>>>>> +	ret = v4l2_event_subscribe(fh, sub, 2, NULL);

>>>>>>> +	if (ret < 0)

>>>>>>> +		return ret;

>>>>>>> +

>>>>>>> +	if (sub->type == UVC_EVENT_SETUP) {

>>>>>>> +		uvc->connections++;

>>>>>>> +		handle->connected = true;

>>>>>>> +		uvc_function_connect(uvc);

>>>>>>> +	}

>>>>>>

>>>>>> This makes no sense. Why would subscribing to a SETUP event

>>>>>> mean that you are 'connected'?

>>>>>

>>>>> Subscribing to a SETUP does not mean that you are connected - on a

>>>>> subscription to SETUP we can expect that there is a userspace process,

>>>>> utilizing UVC -- which is required for the UVC gadget function -- and

>>>>> everything is ready to enable the function by calling uvc_function_connect.

>>>>> How about calling it `function_connected`?

>>>>

>>>> Any application can open the device node and subscribe to this event.

>>>> This is not a good place to call uvc_function_connect(), it's an abuse of

>>>> the V4L2 API.

>>>

>>> Of course - any application can subscribe to this event but since the

>>> event is defined in g_uvc.h I thought that a subscription is most likely

>>> done by a UVC application.

>>>

>>>>

>>>> I dug a bit more into this (I have very little gadget driver experience),

>>>> and isn't calling uvc_function_connect() something that should be done

>>>> on the first open? And when the last filehandle is closed, then

>>>> uvc_function_disconnect() can be called to clean up?

>>>

>>> Isn't the possibility of the delayed activation of the usb function in

>>> composite.c intended to wait for a corresponding userspace application,

>>> which is required to make the gadget work?

>>>

>>> If so, a simple open of the v4l2 device does not mean that the gadget is

>>> ready to go: starting pipewire for example results in quering the device

>>> capabilities of all devices. But this does not mean that the gadget will

>>> work.

>>> Therefore I was looking for a place, where we can expect, that the

>>> application opened the device will utilize UVC and found that a

>>> subscription to a UVC specific event is the right place.

>>

>> That's why I suggested to do this when buffers are allocated. That's when

>> the application commits resources to actually use the device. Until that

>> point everything that an application does is just querying or configuring,

>> but not actually using it.

> 

> The userspace application implementations (including the one from 

> Laurent Pinchart) usually request buffers on the UVC events sent by the 

> host. Therefore we would run into chicken-and-egg problem.


Ah, stupid of me. You're right.

For now I go with calling uvc_function_connect() on the first open. If
nothing else, this is compatible with the current situation, while still
allowing for multiple opens.

Regards,

	Hans

> 

>>

>> BTW, Laurent Pinchart who maintains this driver is away for a week, so he

>> might know more about this when he comes back.

> 

> Okay, so I suggest to wait for him.

> 

> Thanks for the review so far!

> 

> Regards

> Thomas

> 

>>

>> Regards,

>>

>> 	Hans

>>

>>>

>>> Regards,

>>> Thomas

>>>

>>>>

>>>> That would make much more sense. Grep for v4l2_fh_is_singular_file() on how

>>>> to do this, quite a few media drivers need this.

>>>>

>>>> Regards,

>>>>

>>>> 	Hans

>>>>

>>>>>

>>>>>>

>>>>>> It should be possible to open a V4L2 device node any number of times,

>>>>>> and any filehandle can subscribe to any event, but typically once

>>>>>> userspace allocates buffers (VIDIOC_REQBUFS or VIDIOC_CREATE_BUFS)

>>>>>> then that filehandle is marked as the owner of the device and other

>>>>>> open filehandles are no longer allowed to allocate buffers or stream video.

>>>>>

>>>>> Sure - this can be also done if userspace allocates buffers.

>>>>> But why does it make more sense to call uvc_function_connect on

>>>>> VIDIOC_REQBUFS or VIDIOC_CREATE_BUFS instead of subscribtion to a UVC event?

>>>>>

>>>>>>

>>>>>> See e.g. drivers/media/common/videobuf2/videobuf2-v4l2.c

>>>>>> and vb2_ioctl_reqbufs and other vb2_ioctl_* functions.

>>>>>>

>>>>>> Unfortunately this UVC gadget driver is rather old and is not using

>>>>>> these helper functions.

>>>>>>

>>>>>> Running 'v4l2-compliance' will likely fail on a lot of tests for this

>>>>>> driver.

>>>>>>

>>>>>> This driver probably could use some TLC.

>>>>>

>>>>> I totally agree with that, however this change fixes at least one test

>>>>> of 'v4l2-compliance'.

>>>>> Currently a running UVC connection can be corrupted by another process

>>>>> which just opens and closes the device.

>>>>>

>>>>> Thomas

>>>>>

>>>>>>

>>>>>> Regards,

>>>>>>

>>>>>> 	Hans

>>>>>>

>>>>>>> +

>>>>>>> +	return 0;

>>>>>>> +}

>>>>>>> +

>>>>>>> +static void uvc_v4l2_disable(struct uvc_device *uvc)

>>>>>>> +{

>>>>>>> +	if (--uvc->connections)

>>>>>>> +		return;

>>>>>>> +

>>>>>>> +	uvc_function_disconnect(uvc);

>>>>>>> +	uvcg_video_enable(&uvc->video, 0);

>>>>>>> +	uvcg_free_buffers(&uvc->video.queue);

>>>>>>>     }

>>>>>>>     

>>>>>>>     static int

>>>>>>>     uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,

>>>>>>>     			   const struct v4l2_event_subscription *sub)

>>>>>>>     {

>>>>>>> -	return v4l2_event_unsubscribe(fh, sub);

>>>>>>> +	struct uvc_device *uvc = video_get_drvdata(fh->vdev);

>>>>>>> +	struct uvc_file_handle *handle = to_uvc_file_handle(fh);

>>>>>>> +	int ret;

>>>>>>> +

>>>>>>> +	ret = v4l2_event_unsubscribe(fh, sub);

>>>>>>> +	if (ret < 0)

>>>>>>> +		return ret;

>>>>>>> +

>>>>>>> +	if ((sub->type == UVC_EVENT_SETUP) && handle->connected) {

>>>>>>> +		uvc_v4l2_disable(uvc);

>>>>>>> +		handle->connected = false;

>>>>>>> +	}

>>>>>>> +

>>>>>>> +	return 0;

>>>>>>>     }

>>>>>>>     

>>>>>>>     static long

>>>>>>> @@ -293,7 +333,6 @@ uvc_v4l2_open(struct file *file)

>>>>>>>     	handle->device = &uvc->video;

>>>>>>>     	file->private_data = &handle->vfh;

>>>>>>>     

>>>>>>> -	uvc_function_connect(uvc);

>>>>>>>     	return 0;

>>>>>>>     }

>>>>>>>     

>>>>>>> @@ -303,14 +342,11 @@ uvc_v4l2_release(struct file *file)

>>>>>>>     	struct video_device *vdev = video_devdata(file);

>>>>>>>     	struct uvc_device *uvc = video_get_drvdata(vdev);

>>>>>>>     	struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);

>>>>>>> -	struct uvc_video *video = handle->device;

>>>>>>> -

>>>>>>> -	uvc_function_disconnect(uvc);

>>>>>>>     

>>>>>>> -	mutex_lock(&video->mutex);

>>>>>>> -	uvcg_video_enable(video, 0);

>>>>>>> -	uvcg_free_buffers(&video->queue);

>>>>>>> -	mutex_unlock(&video->mutex);

>>>>>>> +	mutex_lock(&uvc->video.mutex);

>>>>>>> +	if (handle->connected)

>>>>>>> +		uvc_v4l2_disable(uvc);

>>>>>>> +	mutex_unlock(&uvc->video.mutex);

>>>>>>>     

>>>>>>>     	file->private_data = NULL;

>>>>>>>     	v4l2_fh_del(&handle->vfh);

>>>>>>>

>>>>>>

>>>>

>>
Greg Kroah-Hartman Nov. 13, 2020, 1:36 p.m. UTC | #9
On Tue, Nov 10, 2020 at 03:30:15PM +0100, thomas.haemmerle@wolfvision.net wrote:
> From: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>

> 

> Currently, the UVC function is activated when open on the corresponding

> v4l2 device is called.

> On another open the activation of the function fails since the

> deactivation counter in `usb_function_activate` equals 0. However the

> error is not returned to userspace since the open of the v4l2 device is

> successful.

> 

> On a close the function is deactivated (since deactivation counter still

> equals 0) and the video is disabled in `uvc_v4l2_release`, although the

> UVC application potentially is streaming.

> 

> Move activation of UVC function to subscription on UVC_EVENT_SETUP

> because there we can guarantee for a userspace application utilizing UVC.

> Block subscription on UVC_EVENT_SETUP while another application already

> is subscribed to it, indicated by `bool func_connected` in

> `struct uvc_device`.

> Extend the `struct uvc_file_handle` with member `bool is_uvc_app_handle`

> to tag it as the handle used by the userspace UVC application.

> 

> With this a process is able to check capabilities of the v4l2 device

> without deactivating the function for the actual UVC application.

> 

> Signed-off-by: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>

> ---

> v3:

>  - replace `unsigned int connections` with `bool func_connected`

>  - rename `bool connected` to `bool is_uvc_app_handle`


Can you fix this up based on Hans's review?

thanks,

greg k-h
Laurent Pinchart Nov. 16, 2020, 3:31 p.m. UTC | #10
Hello everybody,

Sorry for chiming in late.

On Tue, Nov 10, 2020 at 05:01:31PM +0100, Hans Verkuil wrote:
> On 10/11/2020 16:50, Thomas Hämmerle wrote:
> > On 10.11.20 16:36, Hans Verkuil wrote:
> >> On 10/11/2020 16:10, Thomas Hämmerle wrote:
> >>> On 10.11.20 15:43, Hans Verkuil wrote:
> >>>> On 10/11/2020 12:53, Thomas Hämmerle wrote:
> >>>>> On 10.11.20 11:31, Hans Verkuil wrote:
> >>>>>> On 10/11/2020 09:25, thomas.haemmerle@wolfvision.net wrote:
> >>>>>>> From: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>
> >>>>>>>
> >>>>>>> Currently, the UVC function is activated when open on the corresponding
> >>>>>>> v4l2 device is called.
> >>>>>>> On another open the activation of the function fails since the
> >>>>>>> deactivation counter in `usb_function_activate` equals 0. However the
> >>>>>>> error is not returned to userspace since the open of the v4l2 device is
> >>>>>>> successful.
> >>>>>>>
> >>>>>>> On a close the function is deactivated (since deactivation counter still
> >>>>>>> equals 0) and the video is disabled in `uvc_v4l2_release`, although
> >>>>>>> another process potentially is streaming.
> >>>>>>>
> >>>>>>> Move activation of UVC function to subscription on UVC_EVENT_SETUP and
> >>>>>>> keep track of the number of subscribers (limited to 1) because there we
> >>>>>>> can guarantee for a userspace program utilizing UVC.
> >>>>>>> Extend the `struct uvc_file_handle` with member `bool connected` to tag
> >>>>>>> it for a deactivation of the function.
> >>>>>>>
> >>>>>>> With this a process is able to check capabilities of the v4l2 device
> >>>>>>> without deactivating the function for another process actually using the
> >>>>>>> device for UVC streaming.
> >>>>>>>
> >>>>>>> Signed-off-by: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>
> >>>>>>> ---
> >>>>>>> v2:
> >>>>>>>     - fix deadlock in `uvc_v4l2_unsubscribe_event()` (mutex is already
> >>>>>>>       locked in v4l2-core) introduced in v1
> >>>>>>>     - lock mutex in `uvc_v4l2_release()` to suppress ioctls and protect
> >>>>>>>       connected
> >>>>>>>
> >>>>>>>     drivers/usb/gadget/function/uvc.h      |  2 +
> >>>>>>>     drivers/usb/gadget/function/uvc_v4l2.c | 56 +++++++++++++++++++++-----
> >>>>>>>     2 files changed, 48 insertions(+), 10 deletions(-)
> >>>>>>>
> >>>>>>> diff --git a/drivers/usb/gadget/function/uvc.h b/drivers/usb/gadget/function/uvc.h
> >>>>>>> index 73da4f9a8d4c..0d0bcbffc8fd 100644
> >>>>>>> --- a/drivers/usb/gadget/function/uvc.h
> >>>>>>> +++ b/drivers/usb/gadget/function/uvc.h
> >>>>>>> @@ -117,6 +117,7 @@ struct uvc_device {
> >>>>>>>     	enum uvc_state state;
> >>>>>>>     	struct usb_function func;
> >>>>>>>     	struct uvc_video video;
> >>>>>>> +	unsigned int connections;
> >>>>>>>     
> >>>>>>>     	/* Descriptors */
> >>>>>>>     	struct {
> >>>>>>> @@ -147,6 +148,7 @@ static inline struct uvc_device *to_uvc(struct usb_function *f)
> >>>>>>>     struct uvc_file_handle {
> >>>>>>>     	struct v4l2_fh vfh;
> >>>>>>>     	struct uvc_video *device;
> >>>>>>> +	bool connected;
> >>>>>>>     };
> >>>>>>>     
> >>>>>>>     #define to_uvc_file_handle(handle) \
> >>>>>>> diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c
> >>>>>>> index 67922b1355e6..aee4888e17b1 100644
> >>>>>>> --- a/drivers/usb/gadget/function/uvc_v4l2.c
> >>>>>>> +++ b/drivers/usb/gadget/function/uvc_v4l2.c
> >>>>>>> @@ -228,17 +228,57 @@ static int
> >>>>>>>     uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
> >>>>>>>     			 const struct v4l2_event_subscription *sub)
> >>>>>>>     {
> >>>>>>> +	struct uvc_device *uvc = video_get_drvdata(fh->vdev);
> >>>>>>> +	struct uvc_file_handle *handle = to_uvc_file_handle(fh);
> >>>>>>> +	int ret;
> >>>>>>> +
> >>>>>>>     	if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
> >>>>>>>     		return -EINVAL;
> >>>>>>>     
> >>>>>>> -	return v4l2_event_subscribe(fh, sub, 2, NULL);
> >>>>>>> +	if ((sub->type == UVC_EVENT_SETUP) && (uvc->connections >= 1))
> >>>>>>> +		return -EBUSY;
> >>>>>>> +
> >>>>>>> +	ret = v4l2_event_subscribe(fh, sub, 2, NULL);
> >>>>>>> +	if (ret < 0)
> >>>>>>> +		return ret;
> >>>>>>> +
> >>>>>>> +	if (sub->type == UVC_EVENT_SETUP) {
> >>>>>>> +		uvc->connections++;
> >>>>>>> +		handle->connected = true;
> >>>>>>> +		uvc_function_connect(uvc);
> >>>>>>> +	}
> >>>>>>
> >>>>>> This makes no sense. Why would subscribing to a SETUP event
> >>>>>> mean that you are 'connected'?
> >>>>>
> >>>>> Subscribing to a SETUP does not mean that you are connected - on a
> >>>>> subscription to SETUP we can expect that there is a userspace process,
> >>>>> utilizing UVC -- which is required for the UVC gadget function -- and
> >>>>> everything is ready to enable the function by calling uvc_function_connect.
> >>>>> How about calling it `function_connected`?
> >>>>
> >>>> Any application can open the device node and subscribe to this event.
> >>>> This is not a good place to call uvc_function_connect(), it's an abuse of
> >>>> the V4L2 API.
> >>>
> >>> Of course - any application can subscribe to this event but since the
> >>> event is defined in g_uvc.h I thought that a subscription is most likely
> >>> done by a UVC application.

I agree with Thomas here, subscribing to UVC_EVENT_SETUP means that the
application is a UVC gadget application. It's a bit of a hack, but short
of introducing a custom ioctl, I think it's a fairly good option.

> >>>> I dug a bit more into this (I have very little gadget driver experience),
> >>>> and isn't calling uvc_function_connect() something that should be done
> >>>> on the first open? And when the last filehandle is closed, then
> >>>> uvc_function_disconnect() can be called to clean up?
> >>>
> >>> Isn't the possibility of the delayed activation of the usb function in
> >>> composite.c intended to wait for a corresponding userspace application,
> >>> which is required to make the gadget work?
> >>>
> >>> If so, a simple open of the v4l2 device does not mean that the gadget is
> >>> ready to go: starting pipewire for example results in quering the device
> >>> capabilities of all devices. But this does not mean that the gadget will
> >>> work.
> >>> Therefore I was looking for a place, where we can expect, that the
> >>> application opened the device will utilize UVC and found that a
> >>> subscription to a UVC specific event is the right place.
> >>
> >> That's why I suggested to do this when buffers are allocated. That's when
> >> the application commits resources to actually use the device. Until that
> >> point everything that an application does is just querying or configuring,
> >> but not actually using it.
> > 
> > The userspace application implementations (including the one from 
> > Laurent Pinchart) usually request buffers on the UVC events sent by the 
> > host. Therefore we would run into chicken-and-egg problem.
> 
> Ah, stupid of me. You're right.
> 
> For now I go with calling uvc_function_connect() on the first open. If
> nothing else, this is compatible with the current situation, while still
> allowing for multiple opens.

The problem with connecting on first open is that any application
querying the device (v4l_id from udev, pipewire as Thomas mentioned,
...) will generate spurious connect/disconnect events.

This being said, the current code already suffers from the spurious
connect/disconnect issue, so we could possibly fix the race condition
first with a patch that calls uvc_function_connect() on the first open
and uvc_function_disconnect() on last close, and then move the logic to
event subscription in a separate patch. Not sure it would be worth it
though.

> >> BTW, Laurent Pinchart who maintains this driver is away for a week, so he
> >> might know more about this when he comes back.
> > 
> > Okay, so I suggest to wait for him.
> > 
> > Thanks for the review so far!
> > 
> >>>> That would make much more sense. Grep for v4l2_fh_is_singular_file() on how
> >>>> to do this, quite a few media drivers need this.
> >>>>
> >>>>>> It should be possible to open a V4L2 device node any number of times,
> >>>>>> and any filehandle can subscribe to any event, but typically once
> >>>>>> userspace allocates buffers (VIDIOC_REQBUFS or VIDIOC_CREATE_BUFS)
> >>>>>> then that filehandle is marked as the owner of the device and other
> >>>>>> open filehandles are no longer allowed to allocate buffers or stream video.
> >>>>>
> >>>>> Sure - this can be also done if userspace allocates buffers.
> >>>>> But why does it make more sense to call uvc_function_connect on
> >>>>> VIDIOC_REQBUFS or VIDIOC_CREATE_BUFS instead of subscribtion to a UVC event?
> >>>>>
> >>>>>> See e.g. drivers/media/common/videobuf2/videobuf2-v4l2.c
> >>>>>> and vb2_ioctl_reqbufs and other vb2_ioctl_* functions.
> >>>>>>
> >>>>>> Unfortunately this UVC gadget driver is rather old and is not using
> >>>>>> these helper functions.
> >>>>>>
> >>>>>> Running 'v4l2-compliance' will likely fail on a lot of tests for this
> >>>>>> driver.

I don't think this driver will ever be able to pass v4l2-compliance as
it's not a typical V4L2 output device. It uses V4L2 as that was the best
match for a userspace API, but custom ioctls and events are required to
drive the device, and the userspace application needs to work
hand-in-hand with the driver to respond to UVC requests originating from
the host. No other V4L2 application will be able to use the device.

This being said, the driver is indeed old, and I'm sure improvements are
possible, but full v4l2-compliance isn't a reasonable goal.

> >>>>>> This driver probably could use some TLC.
> >>>>>
> >>>>> I totally agree with that, however this change fixes at least one test
> >>>>> of 'v4l2-compliance'.
> >>>>> Currently a running UVC connection can be corrupted by another process
> >>>>> which just opens and closes the device.
> >>>>>
> >>>>>>> +
> >>>>>>> +	return 0;
> >>>>>>> +}
> >>>>>>> +
> >>>>>>> +static void uvc_v4l2_disable(struct uvc_device *uvc)
> >>>>>>> +{
> >>>>>>> +	if (--uvc->connections)
> >>>>>>> +		return;
> >>>>>>> +
> >>>>>>> +	uvc_function_disconnect(uvc);
> >>>>>>> +	uvcg_video_enable(&uvc->video, 0);
> >>>>>>> +	uvcg_free_buffers(&uvc->video.queue);
> >>>>>>>     }
> >>>>>>>     
> >>>>>>>     static int
> >>>>>>>     uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,
> >>>>>>>     			   const struct v4l2_event_subscription *sub)
> >>>>>>>     {
> >>>>>>> -	return v4l2_event_unsubscribe(fh, sub);
> >>>>>>> +	struct uvc_device *uvc = video_get_drvdata(fh->vdev);
> >>>>>>> +	struct uvc_file_handle *handle = to_uvc_file_handle(fh);
> >>>>>>> +	int ret;
> >>>>>>> +
> >>>>>>> +	ret = v4l2_event_unsubscribe(fh, sub);
> >>>>>>> +	if (ret < 0)
> >>>>>>> +		return ret;
> >>>>>>> +
> >>>>>>> +	if ((sub->type == UVC_EVENT_SETUP) && handle->connected) {
> >>>>>>> +		uvc_v4l2_disable(uvc);
> >>>>>>> +		handle->connected = false;
> >>>>>>> +	}
> >>>>>>> +
> >>>>>>> +	return 0;
> >>>>>>>     }
> >>>>>>>     
> >>>>>>>     static long
> >>>>>>> @@ -293,7 +333,6 @@ uvc_v4l2_open(struct file *file)
> >>>>>>>     	handle->device = &uvc->video;
> >>>>>>>     	file->private_data = &handle->vfh;
> >>>>>>>     
> >>>>>>> -	uvc_function_connect(uvc);
> >>>>>>>     	return 0;
> >>>>>>>     }
> >>>>>>>     
> >>>>>>> @@ -303,14 +342,11 @@ uvc_v4l2_release(struct file *file)
> >>>>>>>     	struct video_device *vdev = video_devdata(file);
> >>>>>>>     	struct uvc_device *uvc = video_get_drvdata(vdev);
> >>>>>>>     	struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
> >>>>>>> -	struct uvc_video *video = handle->device;
> >>>>>>> -
> >>>>>>> -	uvc_function_disconnect(uvc);
> >>>>>>>     
> >>>>>>> -	mutex_lock(&video->mutex);
> >>>>>>> -	uvcg_video_enable(video, 0);
> >>>>>>> -	uvcg_free_buffers(&video->queue);
> >>>>>>> -	mutex_unlock(&video->mutex);
> >>>>>>> +	mutex_lock(&uvc->video.mutex);
> >>>>>>> +	if (handle->connected)
> >>>>>>> +		uvc_v4l2_disable(uvc);
> >>>>>>> +	mutex_unlock(&uvc->video.mutex);
> >>>>>>>     
> >>>>>>>     	file->private_data = NULL;
> >>>>>>>     	v4l2_fh_del(&handle->vfh);
Laurent Pinchart Nov. 16, 2020, 3:48 p.m. UTC | #11
Hi Thomas,

Thank you for the patch, and sorry for the late review. I was mostly
absent last week.

On Tue, Nov 10, 2020 at 03:30:15PM +0100, thomas.haemmerle@wolfvision.net wrote:
> From: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>
> 
> Currently, the UVC function is activated when open on the corresponding
> v4l2 device is called.
> On another open the activation of the function fails since the
> deactivation counter in `usb_function_activate` equals 0. However the
> error is not returned to userspace since the open of the v4l2 device is
> successful.
> 
> On a close the function is deactivated (since deactivation counter still
> equals 0) and the video is disabled in `uvc_v4l2_release`, although the
> UVC application potentially is streaming.
> 
> Move activation of UVC function to subscription on UVC_EVENT_SETUP
> because there we can guarantee for a userspace application utilizing UVC.
> Block subscription on UVC_EVENT_SETUP while another application already
> is subscribed to it, indicated by `bool func_connected` in
> `struct uvc_device`.
> Extend the `struct uvc_file_handle` with member `bool is_uvc_app_handle`
> to tag it as the handle used by the userspace UVC application.
> 
> With this a process is able to check capabilities of the v4l2 device
> without deactivating the function for the actual UVC application.
> 
> Signed-off-by: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>
> ---
> v3:
>  - replace `unsigned int connections` with `bool func_connected`
>  - rename `bool connected` to `bool is_uvc_app_handle`
> 
> v2:
>  - fix deadlock in `uvc_v4l2_unsubscribe_event()` (mutex is already
>    locked in v4l2-core) introduced in v1
>  - lock mutex in `uvc_v4l2_release()` to suppress ioctls and protect
>    connected
> 
>  drivers/usb/gadget/function/uvc.h      |  2 +
>  drivers/usb/gadget/function/uvc_v4l2.c | 54 +++++++++++++++++++++-----
>  2 files changed, 46 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/usb/gadget/function/uvc.h b/drivers/usb/gadget/function/uvc.h
> index 73da4f9a8d4c..d6d0fd2dffa0 100644
> --- a/drivers/usb/gadget/function/uvc.h
> +++ b/drivers/usb/gadget/function/uvc.h
> @@ -117,6 +117,7 @@ struct uvc_device {
>  	enum uvc_state state;
>  	struct usb_function func;
>  	struct uvc_video video;
> +	bool func_connected;
>  
>  	/* Descriptors */
>  	struct {
> @@ -147,6 +148,7 @@ static inline struct uvc_device *to_uvc(struct usb_function *f)
>  struct uvc_file_handle {
>  	struct v4l2_fh vfh;
>  	struct uvc_video *device;
> +	bool is_uvc_app_handle;
>  };
>  
>  #define to_uvc_file_handle(handle) \
> diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c
> index 67922b1355e6..3c0b7a969107 100644
> --- a/drivers/usb/gadget/function/uvc_v4l2.c
> +++ b/drivers/usb/gadget/function/uvc_v4l2.c
> @@ -228,17 +228,55 @@ static int
>  uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
>  			 const struct v4l2_event_subscription *sub)
>  {
> +	struct uvc_device *uvc = video_get_drvdata(fh->vdev);
> +	struct uvc_file_handle *handle = to_uvc_file_handle(fh);
> +	int ret;
> +
>  	if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
>  		return -EINVAL;
>  
> -	return v4l2_event_subscribe(fh, sub, 2, NULL);
> +	if ((sub->type == UVC_EVENT_SETUP) && uvc->func_connected)

No need for the inner parentheses.

> +		return -EBUSY;
> +
> +	ret = v4l2_event_subscribe(fh, sub, 2, NULL);
> +	if (ret < 0)
> +		return ret;
> +
> +	if (sub->type == UVC_EVENT_SETUP) {
> +		uvc->func_connected = true;
> +		handle->is_uvc_app_handle = true;
> +		uvc_function_connect(uvc);
> +	}
> +
> +	return 0;
> +}
> +
> +static void uvc_v4l2_disable(struct uvc_device *uvc)
> +{
> +	uvc->func_connected = false;
> +	uvc_function_disconnect(uvc);
> +	uvcg_video_enable(&uvc->video, 0);
> +	uvcg_free_buffers(&uvc->video.queue);
>  }
>  
>  static int
>  uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,
>  			   const struct v4l2_event_subscription *sub)
>  {
> -	return v4l2_event_unsubscribe(fh, sub);
> +	struct uvc_device *uvc = video_get_drvdata(fh->vdev);
> +	struct uvc_file_handle *handle = to_uvc_file_handle(fh);
> +	int ret;
> +
> +	ret = v4l2_event_unsubscribe(fh, sub);
> +	if (ret < 0)
> +		return ret;
> +
> +	if ((sub->type == UVC_EVENT_SETUP) && handle->is_uvc_app_handle) {

No need for the inner parentheses.

> +		uvc_v4l2_disable(uvc);
> +		handle->is_uvc_app_handle = false;

Calling uvc_v4l2_disable() here means that we'll stop everything when
unsubscribing from the event, which sounds like it could cause issues as
that behaviour is not expected. Wouldn't it be enough to only handle
this in uvc_v4l2_release() ?

> +	}
> +
> +	return 0;
>  }
>  
>  static long
> @@ -293,7 +331,6 @@ uvc_v4l2_open(struct file *file)
>  	handle->device = &uvc->video;
>  	file->private_data = &handle->vfh;
>  
> -	uvc_function_connect(uvc);
>  	return 0;
>  }
>  
> @@ -303,14 +340,11 @@ uvc_v4l2_release(struct file *file)
>  	struct video_device *vdev = video_devdata(file);
>  	struct uvc_device *uvc = video_get_drvdata(vdev);
>  	struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
> -	struct uvc_video *video = handle->device;
> -
> -	uvc_function_disconnect(uvc);
>  
> -	mutex_lock(&video->mutex);
> -	uvcg_video_enable(video, 0);
> -	uvcg_free_buffers(&video->queue);
> -	mutex_unlock(&video->mutex);
> +	mutex_lock(&uvc->video.mutex);

Could you please keep keep the local video variable, and use
&video->mutex here ? The driver has a single video device at the moment,
but could be extended in the future with support for multiple video
devices in a single UVC device (lots of changes would be needed though).

> +	if (handle->is_uvc_app_handle)
> +		uvc_v4l2_disable(uvc);
> +	mutex_unlock(&uvc->video.mutex);

Note that this lock isn't the same as the lock taken by
__video_do_ioctl(), which alls uvc_v4l2_subscribe_event() and
uvc_v4l2_unsubscribe_event(). I think Hans got confused in his review,
it appears that there's nothing protecting concurrent access to
is_uvc_app_handle and func_connected in v3. I think you need to take the
driver-specific lock in uvc_v4l2_subscribe_event() and
uvc_v4l2_unsubscribe_event().

>  
>  	file->private_data = NULL;
>  	v4l2_fh_del(&handle->vfh);
Thomas Haemmerle Nov. 21, 2020, 12:50 p.m. UTC | #12
Hi Laurent,

sorry for my late response!

On 16.11.20 16:48, Laurent Pinchart wrote:
> Hi Thomas,

> 

> Thank you for the patch, and sorry for the late review. I was mostly

> absent last week.

> 

> On Tue, Nov 10, 2020 at 03:30:15PM +0100, thomas.haemmerle@wolfvision.net wrote:

>> From: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>

>>

>> Currently, the UVC function is activated when open on the corresponding

>> v4l2 device is called.

>> On another open the activation of the function fails since the

>> deactivation counter in `usb_function_activate` equals 0. However the

>> error is not returned to userspace since the open of the v4l2 device is

>> successful.

>>

>> On a close the function is deactivated (since deactivation counter still

>> equals 0) and the video is disabled in `uvc_v4l2_release`, although the

>> UVC application potentially is streaming.

>>

>> Move activation of UVC function to subscription on UVC_EVENT_SETUP

>> because there we can guarantee for a userspace application utilizing UVC.

>> Block subscription on UVC_EVENT_SETUP while another application already

>> is subscribed to it, indicated by `bool func_connected` in

>> `struct uvc_device`.

>> Extend the `struct uvc_file_handle` with member `bool is_uvc_app_handle`

>> to tag it as the handle used by the userspace UVC application.

>>

>> With this a process is able to check capabilities of the v4l2 device

>> without deactivating the function for the actual UVC application.

>>

>> Signed-off-by: Thomas Haemmerle <thomas.haemmerle@wolfvision.net>

>> ---

>> v3:

>>   - replace `unsigned int connections` with `bool func_connected`

>>   - rename `bool connected` to `bool is_uvc_app_handle`

>>

>> v2:

>>   - fix deadlock in `uvc_v4l2_unsubscribe_event()` (mutex is already

>>     locked in v4l2-core) introduced in v1

>>   - lock mutex in `uvc_v4l2_release()` to suppress ioctls and protect

>>     connected

>>

>>   drivers/usb/gadget/function/uvc.h      |  2 +

>>   drivers/usb/gadget/function/uvc_v4l2.c | 54 +++++++++++++++++++++-----

>>   2 files changed, 46 insertions(+), 10 deletions(-)

>>

>> diff --git a/drivers/usb/gadget/function/uvc.h b/drivers/usb/gadget/function/uvc.h

>> index 73da4f9a8d4c..d6d0fd2dffa0 100644

>> --- a/drivers/usb/gadget/function/uvc.h

>> +++ b/drivers/usb/gadget/function/uvc.h

>> @@ -117,6 +117,7 @@ struct uvc_device {

>>   	enum uvc_state state;

>>   	struct usb_function func;

>>   	struct uvc_video video;

>> +	bool func_connected;

>>   

>>   	/* Descriptors */

>>   	struct {

>> @@ -147,6 +148,7 @@ static inline struct uvc_device *to_uvc(struct usb_function *f)

>>   struct uvc_file_handle {

>>   	struct v4l2_fh vfh;

>>   	struct uvc_video *device;

>> +	bool is_uvc_app_handle;

>>   };

>>   

>>   #define to_uvc_file_handle(handle) \

>> diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c

>> index 67922b1355e6..3c0b7a969107 100644

>> --- a/drivers/usb/gadget/function/uvc_v4l2.c

>> +++ b/drivers/usb/gadget/function/uvc_v4l2.c

>> @@ -228,17 +228,55 @@ static int

>>   uvc_v4l2_subscribe_event(struct v4l2_fh *fh,

>>   			 const struct v4l2_event_subscription *sub)

>>   {

>> +	struct uvc_device *uvc = video_get_drvdata(fh->vdev);

>> +	struct uvc_file_handle *handle = to_uvc_file_handle(fh);

>> +	int ret;

>> +

>>   	if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)

>>   		return -EINVAL;

>>   

>> -	return v4l2_event_subscribe(fh, sub, 2, NULL);

>> +	if ((sub->type == UVC_EVENT_SETUP) && uvc->func_connected)

> 

> No need for the inner parentheses.


I will change this.

> 

>> +		return -EBUSY;

>> +

>> +	ret = v4l2_event_subscribe(fh, sub, 2, NULL);

>> +	if (ret < 0)

>> +		return ret;

>> +

>> +	if (sub->type == UVC_EVENT_SETUP) {

>> +		uvc->func_connected = true;

>> +		handle->is_uvc_app_handle = true;

>> +		uvc_function_connect(uvc);

>> +	}

>> +

>> +	return 0;

>> +}

>> +

>> +static void uvc_v4l2_disable(struct uvc_device *uvc)

>> +{

>> +	uvc->func_connected = false;

>> +	uvc_function_disconnect(uvc);

>> +	uvcg_video_enable(&uvc->video, 0);

>> +	uvcg_free_buffers(&uvc->video.queue);

>>   }

>>   

>>   static int

>>   uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,

>>   			   const struct v4l2_event_subscription *sub)

>>   {

>> -	return v4l2_event_unsubscribe(fh, sub);

>> +	struct uvc_device *uvc = video_get_drvdata(fh->vdev);

>> +	struct uvc_file_handle *handle = to_uvc_file_handle(fh);

>> +	int ret;

>> +

>> +	ret = v4l2_event_unsubscribe(fh, sub);

>> +	if (ret < 0)

>> +		return ret;

>> +

>> +	if ((sub->type == UVC_EVENT_SETUP) && handle->is_uvc_app_handle) {

> 

> No need for the inner parentheses.


I will change this.

> 

>> +		uvc_v4l2_disable(uvc);

>> +		handle->is_uvc_app_handle = false;

> 

> Calling uvc_v4l2_disable() here means that we'll stop everything when

> unsubscribing from the event, which sounds like it could cause issues as

> that behaviour is not expected. Wouldn't it be enough to only handle

> this in uvc_v4l2_release() ?

> 


Of course it would be enough. But maybe a UVC gadget application wants 
to release the device for another application without closing it and 
since the function is activated on subscription the logical consequence 
is to deactivate it on unsubscription.

>> +	}

>> +

>> +	return 0;

>>   }

>>   

>>   static long

>> @@ -293,7 +331,6 @@ uvc_v4l2_open(struct file *file)

>>   	handle->device = &uvc->video;

>>   	file->private_data = &handle->vfh;

>>   

>> -	uvc_function_connect(uvc);

>>   	return 0;

>>   }

>>   

>> @@ -303,14 +340,11 @@ uvc_v4l2_release(struct file *file)

>>   	struct video_device *vdev = video_devdata(file);

>>   	struct uvc_device *uvc = video_get_drvdata(vdev);

>>   	struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);

>> -	struct uvc_video *video = handle->device;

>> -

>> -	uvc_function_disconnect(uvc);

>>   

>> -	mutex_lock(&video->mutex);

>> -	uvcg_video_enable(video, 0);

>> -	uvcg_free_buffers(&video->queue);

>> -	mutex_unlock(&video->mutex);

>> +	mutex_lock(&uvc->video.mutex);

> 

> Could you please keep keep the local video variable, and use

> &video->mutex here ? The driver has a single video device at the moment,

> but could be extended in the future with support for multiple video

> devices in a single UVC device (lots of changes would be needed though).


Yes.

> 

>> +	if (handle->is_uvc_app_handle)

>> +		uvc_v4l2_disable(uvc);

>> +	mutex_unlock(&uvc->video.mutex);

> 

> Note that this lock isn't the same as the lock taken by

> __video_do_ioctl(), which alls uvc_v4l2_subscribe_event() and

> uvc_v4l2_unsubscribe_event(). I think Hans got confused in his review,

> it appears that there's nothing protecting concurrent access to

> is_uvc_app_handle and func_connected in v3. I think you need to take the

> driver-specific lock in uvc_v4l2_subscribe_event() and

> uvc_v4l2_unsubscribe_event().


Why isn't this the same lock taken by __video_do_ioctl()?
The lock in video_device is set to it in `uvc_register_video()` in f_uvc.c:
uvc->vdev.lock = &uvc->video.mutex;

So this should be the same, right?

Regards
Thomas
diff mbox series

Patch

diff --git a/drivers/usb/gadget/function/uvc.h b/drivers/usb/gadget/function/uvc.h
index 23ee25383c1f..deeec2b80786 100644
--- a/drivers/usb/gadget/function/uvc.h
+++ b/drivers/usb/gadget/function/uvc.h
@@ -117,6 +117,7 @@  struct uvc_device {
 	enum uvc_state state;
 	struct usb_function func;
 	struct uvc_video video;
+	unsigned int connections;
 
 	/* Descriptors */
 	struct {
@@ -147,6 +148,7 @@  static inline struct uvc_device *to_uvc(struct usb_function *f)
 struct uvc_file_handle {
 	struct v4l2_fh vfh;
 	struct uvc_video *device;
+	bool connected;
 };
 
 #define to_uvc_file_handle(handle) \
diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c
index 4ca89eab6159..c0c2588b0efb 100644
--- a/drivers/usb/gadget/function/uvc_v4l2.c
+++ b/drivers/usb/gadget/function/uvc_v4l2.c
@@ -227,17 +227,60 @@  static int
 uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 			 const struct v4l2_event_subscription *sub)
 {
+	struct uvc_device *uvc = video_get_drvdata(fh->vdev);
+	struct uvc_file_handle *handle = to_uvc_file_handle(fh);
+	int ret;
+
 	if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
 		return -EINVAL;
 
-	return v4l2_event_subscribe(fh, sub, 2, NULL);
+	if ((sub->type == UVC_EVENT_SETUP) && (uvc->connections >= 1))
+		return -EBUSY;
+
+	ret = v4l2_event_subscribe(fh, sub, 2, NULL);
+	if (ret < 0)
+		return ret;
+
+	if (sub->type == UVC_EVENT_SETUP) {
+		uvc->connections++;
+		handle->connected = true;
+		uvc_function_connect(uvc);
+	}
+
+	return 0;
+}
+
+static void uvc_v4l2_disable(struct uvc_device *uvc)
+{
+	if (--uvc->connections)
+		return;
+
+	uvc_function_disconnect(uvc);
+
+	mutex_lock(&uvc->video.mutex);
+	uvcg_video_enable(&uvc->video, 0);
+	uvcg_free_buffers(&uvc->video.queue);
+	mutex_unlock(&uvc->video.mutex);
 }
 
 static int
 uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,
 			   const struct v4l2_event_subscription *sub)
 {
-	return v4l2_event_unsubscribe(fh, sub);
+	struct uvc_device *uvc = video_get_drvdata(fh->vdev);
+	struct uvc_file_handle *handle = to_uvc_file_handle(fh);
+	int ret;
+
+	ret = v4l2_event_unsubscribe(fh, sub);
+	if (ret < 0)
+		return ret;
+
+	if ((sub->type == UVC_EVENT_SETUP) && handle->connected) {
+		uvc_v4l2_disable(uvc);
+		handle->connected = false;
+	}
+
+	return 0;
 }
 
 static long
@@ -292,7 +335,6 @@  uvc_v4l2_open(struct file *file)
 	handle->device = &uvc->video;
 	file->private_data = &handle->vfh;
 
-	uvc_function_connect(uvc);
 	return 0;
 }
 
@@ -302,14 +344,9 @@  uvc_v4l2_release(struct file *file)
 	struct video_device *vdev = video_devdata(file);
 	struct uvc_device *uvc = video_get_drvdata(vdev);
 	struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
-	struct uvc_video *video = handle->device;
-
-	uvc_function_disconnect(uvc);
 
-	mutex_lock(&video->mutex);
-	uvcg_video_enable(video, 0);
-	uvcg_free_buffers(&video->queue);
-	mutex_unlock(&video->mutex);
+	if (handle->connected)
+		uvc_v4l2_disable(uvc);
 
 	file->private_data = NULL;
 	v4l2_fh_del(&handle->vfh);