diff mbox series

[v3,1/6] media: uvcvideo: Keep streaming state in the file handle

Message ID 20250206-uvc-granpower-ng-v3-1-32d0d7b0c5d8@chromium.org
State Superseded
Headers show
Series [v3,1/6] media: uvcvideo: Keep streaming state in the file handle | expand

Commit Message

Ricardo Ribalda Feb. 6, 2025, 7:47 p.m. UTC
Add a variable in the file handle state to figure out if a camera is in
the streaming state or not.
This variable will be used in the future for power management policies.

Now that we are at it, make use of guards to simplify the code.

Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
 drivers/media/usb/uvc/uvc_v4l2.c | 15 +++++++++++----
 drivers/media/usb/uvc/uvcvideo.h |  1 +
 2 files changed, 12 insertions(+), 4 deletions(-)

Comments

Laurent Pinchart Feb. 23, 2025, 3:46 p.m. UTC | #1
Hi Ricardo,

Thank you for the patch.

On Thu, Feb 06, 2025 at 07:47:00PM +0000, Ricardo Ribalda wrote:
> Add a variable in the file handle state to figure out if a camera is in
> the streaming state or not.

s/\n/ / (and reflow) or s/$/\n/ depending on what you meant.

> This variable will be used in the future for power management policies.
> 
> Now that we are at it, make use of guards to simplify the code.
> 
> Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> ---
>  drivers/media/usb/uvc/uvc_v4l2.c | 15 +++++++++++----
>  drivers/media/usb/uvc/uvcvideo.h |  1 +
>  2 files changed, 12 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
> index 93c6cdb23881..856eaa23e703 100644
> --- a/drivers/media/usb/uvc/uvc_v4l2.c
> +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> @@ -835,11 +835,17 @@ static int uvc_ioctl_streamon(struct file *file, void *fh,
>  	if (!uvc_has_privileges(handle))
>  		return -EBUSY;
>  
> -	mutex_lock(&stream->mutex);
> +	guard(mutex)(&stream->mutex);
> +
> +	if (handle->is_streaming)
> +		return 0;
> +
>  	ret = uvc_queue_streamon(&stream->queue, type);
> -	mutex_unlock(&stream->mutex);
> +	if (!ret)
> +		handle->is_streaming = true;
>  
>  	return ret;

Now that you use a mutex guard, you can write

	if (ret)
		return ret;

	handle->is_streaming = true;
	return 0;

apart from that,

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> +
>  }
>  
>  static int uvc_ioctl_streamoff(struct file *file, void *fh,
> @@ -851,9 +857,10 @@ static int uvc_ioctl_streamoff(struct file *file, void *fh,
>  	if (!uvc_has_privileges(handle))
>  		return -EBUSY;
>  
> -	mutex_lock(&stream->mutex);
> +	guard(mutex)(&stream->mutex);
> +
>  	uvc_queue_streamoff(&stream->queue, type);
> -	mutex_unlock(&stream->mutex);
> +	handle->is_streaming = false;
>  
>  	return 0;
>  }
> diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h
> index 5e388f05f3fc..bc87e1f2c669 100644
> --- a/drivers/media/usb/uvc/uvcvideo.h
> +++ b/drivers/media/usb/uvc/uvcvideo.h
> @@ -618,6 +618,7 @@ struct uvc_fh {
>  	struct uvc_streaming *stream;
>  	enum uvc_handle_state state;
>  	unsigned int pending_async_ctrls;
> +	bool is_streaming;
>  };
>  
>  struct uvc_driver {
diff mbox series

Patch

diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
index 93c6cdb23881..856eaa23e703 100644
--- a/drivers/media/usb/uvc/uvc_v4l2.c
+++ b/drivers/media/usb/uvc/uvc_v4l2.c
@@ -835,11 +835,17 @@  static int uvc_ioctl_streamon(struct file *file, void *fh,
 	if (!uvc_has_privileges(handle))
 		return -EBUSY;
 
-	mutex_lock(&stream->mutex);
+	guard(mutex)(&stream->mutex);
+
+	if (handle->is_streaming)
+		return 0;
+
 	ret = uvc_queue_streamon(&stream->queue, type);
-	mutex_unlock(&stream->mutex);
+	if (!ret)
+		handle->is_streaming = true;
 
 	return ret;
+
 }
 
 static int uvc_ioctl_streamoff(struct file *file, void *fh,
@@ -851,9 +857,10 @@  static int uvc_ioctl_streamoff(struct file *file, void *fh,
 	if (!uvc_has_privileges(handle))
 		return -EBUSY;
 
-	mutex_lock(&stream->mutex);
+	guard(mutex)(&stream->mutex);
+
 	uvc_queue_streamoff(&stream->queue, type);
-	mutex_unlock(&stream->mutex);
+	handle->is_streaming = false;
 
 	return 0;
 }
diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h
index 5e388f05f3fc..bc87e1f2c669 100644
--- a/drivers/media/usb/uvc/uvcvideo.h
+++ b/drivers/media/usb/uvc/uvcvideo.h
@@ -618,6 +618,7 @@  struct uvc_fh {
 	struct uvc_streaming *stream;
 	enum uvc_handle_state state;
 	unsigned int pending_async_ctrls;
+	bool is_streaming;
 };
 
 struct uvc_driver {