* [PATCH v3 1/6] media: uvcvideo: Keep streaming state in the file handle
2025-02-06 19:46 [PATCH v3 0/6] media: uvcvideo: Implement Granular Power Saving Ricardo Ribalda
@ 2025-02-06 19:47 ` Ricardo Ribalda
2025-02-23 15:46 ` Laurent Pinchart
2025-02-06 19:47 ` [PATCH v3 2/6] media: uvcvideo: Move usb_autopm_(get|put)_interface to status_get Ricardo Ribalda
` (4 subsequent siblings)
5 siblings, 1 reply; 11+ messages in thread
From: Ricardo Ribalda @ 2025-02-06 19:47 UTC (permalink / raw)
To: Laurent Pinchart, Hans de Goede, Mauro Carvalho Chehab,
Guennadi Liakhovetski
Cc: linux-media, linux-kernel, Mauro Carvalho Chehab, Ricardo Ribalda
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(-)
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 {
--
2.48.1.502.g6dc24dfdaf-goog
^ permalink raw reply related [flat|nested] 11+ messages in thread* Re: [PATCH v3 1/6] media: uvcvideo: Keep streaming state in the file handle
2025-02-06 19:47 ` [PATCH v3 1/6] media: uvcvideo: Keep streaming state in the file handle Ricardo Ribalda
@ 2025-02-23 15:46 ` Laurent Pinchart
0 siblings, 0 replies; 11+ messages in thread
From: Laurent Pinchart @ 2025-02-23 15:46 UTC (permalink / raw)
To: Ricardo Ribalda
Cc: Hans de Goede, Mauro Carvalho Chehab, Guennadi Liakhovetski,
linux-media, linux-kernel, Mauro Carvalho Chehab
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 {
--
Regards,
Laurent Pinchart
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v3 2/6] media: uvcvideo: Move usb_autopm_(get|put)_interface to status_get
2025-02-06 19:46 [PATCH v3 0/6] media: uvcvideo: Implement Granular Power Saving Ricardo Ribalda
2025-02-06 19:47 ` [PATCH v3 1/6] media: uvcvideo: Keep streaming state in the file handle Ricardo Ribalda
@ 2025-02-06 19:47 ` Ricardo Ribalda
2025-02-23 16:05 ` Laurent Pinchart
2025-02-06 19:47 ` [PATCH v3 3/6] media: uvcvideo: Add a uvc_status guard Ricardo Ribalda
` (3 subsequent siblings)
5 siblings, 1 reply; 11+ messages in thread
From: Ricardo Ribalda @ 2025-02-06 19:47 UTC (permalink / raw)
To: Laurent Pinchart, Hans de Goede, Mauro Carvalho Chehab,
Guennadi Liakhovetski
Cc: linux-media, linux-kernel, Mauro Carvalho Chehab, Ricardo Ribalda
Right now PM operations are always called at the same locations as
uvc_status_(get|put).
Combine them into uvc_status_(get|put). This simplifies the current
code and future PM changes in the driver.
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
drivers/media/usb/uvc/uvc_status.c | 38 +++++++++++++++++++++++++++++++++-----
drivers/media/usb/uvc/uvc_v4l2.c | 11 +----------
2 files changed, 34 insertions(+), 15 deletions(-)
diff --git a/drivers/media/usb/uvc/uvc_status.c b/drivers/media/usb/uvc/uvc_status.c
index ee01dce4b783..caa673b0279d 100644
--- a/drivers/media/usb/uvc/uvc_status.c
+++ b/drivers/media/usb/uvc/uvc_status.c
@@ -382,7 +382,7 @@ void uvc_status_suspend(struct uvc_device *dev)
uvc_status_stop(dev);
}
-int uvc_status_get(struct uvc_device *dev)
+static int _uvc_status_get(struct uvc_device *dev)
{
int ret;
@@ -399,13 +399,41 @@ int uvc_status_get(struct uvc_device *dev)
return 0;
}
-void uvc_status_put(struct uvc_device *dev)
+int uvc_status_get(struct uvc_device *dev)
+{
+ int ret;
+
+ ret = usb_autopm_get_interface(dev->intf);
+ if (ret)
+ return ret;
+
+ ret = _uvc_status_get(dev);
+
+ if (ret)
+ usb_autopm_put_interface(dev->intf);
+
+ return ret;
+}
+
+static int _uvc_status_put(struct uvc_device *dev)
{
guard(mutex)(&dev->status_lock);
if (dev->status_users == 1)
uvc_status_stop(dev);
- WARN_ON(!dev->status_users);
- if (dev->status_users)
- dev->status_users--;
+
+ if (WARN_ON(!dev->status_users))
+ return -EIO;
+
+ dev->status_users--;
+ return 0;
+}
+
+void uvc_status_put(struct uvc_device *dev)
+{
+ int ret;
+
+ ret = _uvc_status_put(dev);
+ if (!ret)
+ usb_autopm_put_interface(dev->intf);
}
diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
index 856eaa23e703..5d4e967938af 100644
--- a/drivers/media/usb/uvc/uvc_v4l2.c
+++ b/drivers/media/usb/uvc/uvc_v4l2.c
@@ -636,20 +636,13 @@ static int uvc_v4l2_open(struct file *file)
stream = video_drvdata(file);
uvc_dbg(stream->dev, CALLS, "%s\n", __func__);
- ret = usb_autopm_get_interface(stream->dev->intf);
- if (ret < 0)
- return ret;
-
/* Create the device handle. */
handle = kzalloc(sizeof(*handle), GFP_KERNEL);
- if (handle == NULL) {
- usb_autopm_put_interface(stream->dev->intf);
+ if (!handle)
return -ENOMEM;
- }
ret = uvc_status_get(stream->dev);
if (ret) {
- usb_autopm_put_interface(stream->dev->intf);
kfree(handle);
return ret;
}
@@ -685,8 +678,6 @@ static int uvc_v4l2_release(struct file *file)
file->private_data = NULL;
uvc_status_put(stream->dev);
-
- usb_autopm_put_interface(stream->dev->intf);
return 0;
}
--
2.48.1.502.g6dc24dfdaf-goog
^ permalink raw reply related [flat|nested] 11+ messages in thread* Re: [PATCH v3 2/6] media: uvcvideo: Move usb_autopm_(get|put)_interface to status_get
2025-02-06 19:47 ` [PATCH v3 2/6] media: uvcvideo: Move usb_autopm_(get|put)_interface to status_get Ricardo Ribalda
@ 2025-02-23 16:05 ` Laurent Pinchart
0 siblings, 0 replies; 11+ messages in thread
From: Laurent Pinchart @ 2025-02-23 16:05 UTC (permalink / raw)
To: Ricardo Ribalda
Cc: Hans de Goede, Mauro Carvalho Chehab, Guennadi Liakhovetski,
linux-media, linux-kernel, Mauro Carvalho Chehab
Hi Ricardo,
Thank you for the patch.
On Thu, Feb 06, 2025 at 07:47:01PM +0000, Ricardo Ribalda wrote:
> Right now PM operations are always called at the same locations as
> uvc_status_(get|put).
>
> Combine them into uvc_status_(get|put). This simplifies the current
> code and future PM changes in the driver.
>
> Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> ---
> drivers/media/usb/uvc/uvc_status.c | 38 +++++++++++++++++++++++++++++++++-----
> drivers/media/usb/uvc/uvc_v4l2.c | 11 +----------
> 2 files changed, 34 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/media/usb/uvc/uvc_status.c b/drivers/media/usb/uvc/uvc_status.c
> index ee01dce4b783..caa673b0279d 100644
> --- a/drivers/media/usb/uvc/uvc_status.c
> +++ b/drivers/media/usb/uvc/uvc_status.c
> @@ -382,7 +382,7 @@ void uvc_status_suspend(struct uvc_device *dev)
> uvc_status_stop(dev);
> }
>
> -int uvc_status_get(struct uvc_device *dev)
> +static int _uvc_status_get(struct uvc_device *dev)
s/_uvc_status_get/__uvc_status_get/
> {
> int ret;
>
> @@ -399,13 +399,41 @@ int uvc_status_get(struct uvc_device *dev)
> return 0;
> }
>
> -void uvc_status_put(struct uvc_device *dev)
> +int uvc_status_get(struct uvc_device *dev)
> +{
> + int ret;
> +
> + ret = usb_autopm_get_interface(dev->intf);
> + if (ret)
> + return ret;
> +
> + ret = _uvc_status_get(dev);
> +
> + if (ret)
> + usb_autopm_put_interface(dev->intf);
> +
> + return ret;
> +}
> +
> +static int _uvc_status_put(struct uvc_device *dev)
s/_uvc_status_put/__uvc_status_put/
But unless you need to call this function in subsequent patches in the
series, I would merge it with uvc_status_put(). I think the same could
be done for get() too.
> {
> guard(mutex)(&dev->status_lock);
>
> if (dev->status_users == 1)
> uvc_status_stop(dev);
> - WARN_ON(!dev->status_users);
> - if (dev->status_users)
> - dev->status_users--;
> +
> + if (WARN_ON(!dev->status_users))
> + return -EIO;
That's a change in behaviour that should be at least explained in the
commit message.
> +
> + dev->status_users--;
> + return 0;
> +}
> +
> +void uvc_status_put(struct uvc_device *dev)
> +{
> + int ret;
> +
> + ret = _uvc_status_put(dev);
> + if (!ret)
> + usb_autopm_put_interface(dev->intf);
> }
> diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
> index 856eaa23e703..5d4e967938af 100644
> --- a/drivers/media/usb/uvc/uvc_v4l2.c
> +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> @@ -636,20 +636,13 @@ static int uvc_v4l2_open(struct file *file)
> stream = video_drvdata(file);
> uvc_dbg(stream->dev, CALLS, "%s\n", __func__);
>
> - ret = usb_autopm_get_interface(stream->dev->intf);
> - if (ret < 0)
> - return ret;
> -
> /* Create the device handle. */
> handle = kzalloc(sizeof(*handle), GFP_KERNEL);
> - if (handle == NULL) {
> - usb_autopm_put_interface(stream->dev->intf);
> + if (!handle)
> return -ENOMEM;
> - }
>
> ret = uvc_status_get(stream->dev);
> if (ret) {
> - usb_autopm_put_interface(stream->dev->intf);
> kfree(handle);
> return ret;
> }
> @@ -685,8 +678,6 @@ static int uvc_v4l2_release(struct file *file)
> file->private_data = NULL;
>
> uvc_status_put(stream->dev);
> -
> - usb_autopm_put_interface(stream->dev->intf);
This isn't right. The usb_autopm_get_interface() and
usb_autopm_put_interface() calls here are not mean to support UVC status
operation only. Sure, the patch doesn't introduce an issue as such, but
it bundles two things that are not related in a way that is confusing.
I expect that the code will improve in subsequent patches and the reason
will become clear, but at least the commit message here really needs to
explain why there's a temporary step backwards. Ideally the series
should be reorganized to avoid this.
> return 0;
> }
>
--
Regards,
Laurent Pinchart
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v3 3/6] media: uvcvideo: Add a uvc_status guard
2025-02-06 19:46 [PATCH v3 0/6] media: uvcvideo: Implement Granular Power Saving Ricardo Ribalda
2025-02-06 19:47 ` [PATCH v3 1/6] media: uvcvideo: Keep streaming state in the file handle Ricardo Ribalda
2025-02-06 19:47 ` [PATCH v3 2/6] media: uvcvideo: Move usb_autopm_(get|put)_interface to status_get Ricardo Ribalda
@ 2025-02-06 19:47 ` Ricardo Ribalda
2025-02-23 16:06 ` Laurent Pinchart
2025-02-06 19:47 ` [PATCH v3 4/6] media: uvcvideo: Increase/decrease the PM counter per IOCTL Ricardo Ribalda
` (2 subsequent siblings)
5 siblings, 1 reply; 11+ messages in thread
From: Ricardo Ribalda @ 2025-02-06 19:47 UTC (permalink / raw)
To: Laurent Pinchart, Hans de Goede, Mauro Carvalho Chehab,
Guennadi Liakhovetski
Cc: linux-media, linux-kernel, Mauro Carvalho Chehab, Ricardo Ribalda
It helps will help will the error handling of PM functions.
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
drivers/media/usb/uvc/uvcvideo.h | 2 ++
1 file changed, 2 insertions(+)
diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h
index bc87e1f2c669..be0817da538c 100644
--- a/drivers/media/usb/uvc/uvcvideo.h
+++ b/drivers/media/usb/uvc/uvcvideo.h
@@ -763,6 +763,8 @@ void uvc_status_suspend(struct uvc_device *dev);
int uvc_status_get(struct uvc_device *dev);
void uvc_status_put(struct uvc_device *dev);
+DEFINE_GUARD(uvc_status, struct uvc_device *, uvc_status_get(_T), uvc_status_put(_T))
+
/* Controls */
extern const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops;
--
2.48.1.502.g6dc24dfdaf-goog
^ permalink raw reply related [flat|nested] 11+ messages in thread* Re: [PATCH v3 3/6] media: uvcvideo: Add a uvc_status guard
2025-02-06 19:47 ` [PATCH v3 3/6] media: uvcvideo: Add a uvc_status guard Ricardo Ribalda
@ 2025-02-23 16:06 ` Laurent Pinchart
0 siblings, 0 replies; 11+ messages in thread
From: Laurent Pinchart @ 2025-02-23 16:06 UTC (permalink / raw)
To: Ricardo Ribalda
Cc: Hans de Goede, Mauro Carvalho Chehab, Guennadi Liakhovetski,
linux-media, linux-kernel, Mauro Carvalho Chehab
Hi Ricardo,
Thank you for the patch.
On Thu, Feb 06, 2025 at 07:47:02PM +0000, Ricardo Ribalda wrote:
> It helps will help will the error handling of PM functions.
This is a one-liner without a user. Bundle it with the patch that makes
use of the feature, the result will be easier to review.
> Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> ---
> drivers/media/usb/uvc/uvcvideo.h | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h
> index bc87e1f2c669..be0817da538c 100644
> --- a/drivers/media/usb/uvc/uvcvideo.h
> +++ b/drivers/media/usb/uvc/uvcvideo.h
> @@ -763,6 +763,8 @@ void uvc_status_suspend(struct uvc_device *dev);
> int uvc_status_get(struct uvc_device *dev);
> void uvc_status_put(struct uvc_device *dev);
>
> +DEFINE_GUARD(uvc_status, struct uvc_device *, uvc_status_get(_T), uvc_status_put(_T))
> +
> /* Controls */
> extern const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops;
>
--
Regards,
Laurent Pinchart
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v3 4/6] media: uvcvideo: Increase/decrease the PM counter per IOCTL
2025-02-06 19:46 [PATCH v3 0/6] media: uvcvideo: Implement Granular Power Saving Ricardo Ribalda
` (2 preceding siblings ...)
2025-02-06 19:47 ` [PATCH v3 3/6] media: uvcvideo: Add a uvc_status guard Ricardo Ribalda
@ 2025-02-06 19:47 ` Ricardo Ribalda
2025-02-23 16:55 ` Laurent Pinchart
2025-02-06 19:47 ` [PATCH v3 5/6] media: uvcvideo: Make power management granular Ricardo Ribalda
2025-02-06 19:47 ` [PATCH v3 6/6] media: uvcvideo: Do not turn on the camera for some ioctls Ricardo Ribalda
5 siblings, 1 reply; 11+ messages in thread
From: Ricardo Ribalda @ 2025-02-06 19:47 UTC (permalink / raw)
To: Laurent Pinchart, Hans de Goede, Mauro Carvalho Chehab,
Guennadi Liakhovetski
Cc: linux-media, linux-kernel, Mauro Carvalho Chehab, Ricardo Ribalda
Now we call uvc_status_get/put from the device open/close. This low
level of granularity might leave the camera powered on in situations
where it is not needed.
Increase the granularity by increasing and decreasing the Power
Management counter per ioctl. There are two special cases where the
power management outlives the ioctl: async controls and streamon. Handle
those cases as well.
In a future patch, we will remove the uvc_status_get/put from open/close.
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
drivers/media/usb/uvc/uvc_ctrl.c | 13 +++++++++++--
drivers/media/usb/uvc/uvc_v4l2.c | 26 +++++++++++++++++++++++---
2 files changed, 34 insertions(+), 5 deletions(-)
diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c
index 4e58476d305e..97c1141a45b3 100644
--- a/drivers/media/usb/uvc/uvc_ctrl.c
+++ b/drivers/media/usb/uvc/uvc_ctrl.c
@@ -1594,12 +1594,15 @@ static void uvc_ctrl_set_handle(struct uvc_fh *handle, struct uvc_control *ctrl,
if (ctrl->handle) {
WARN_ON(!ctrl->handle->pending_async_ctrls);
- if (ctrl->handle->pending_async_ctrls)
+ if (ctrl->handle->pending_async_ctrls) {
ctrl->handle->pending_async_ctrls--;
+ uvc_status_put(handle->chain->dev);
+ }
}
ctrl->handle = new_handle;
handle->pending_async_ctrls++;
+ uvc_status_get(handle->chain->dev);
return;
}
@@ -1611,6 +1614,7 @@ static void uvc_ctrl_set_handle(struct uvc_fh *handle, struct uvc_control *ctrl,
if (WARN_ON(!handle->pending_async_ctrls))
return;
handle->pending_async_ctrls--;
+ uvc_status_put(handle->chain->dev);
}
void uvc_ctrl_status_event(struct uvc_video_chain *chain,
@@ -2815,6 +2819,7 @@ int uvc_ctrl_init_device(struct uvc_device *dev)
void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
{
struct uvc_entity *entity;
+ int i;
guard(mutex)(&handle->chain->ctrl_mutex);
@@ -2829,7 +2834,11 @@ void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
}
}
- WARN_ON(handle->pending_async_ctrls);
+ if (!WARN_ON(handle->pending_async_ctrls))
+ return;
+
+ for (i = 0; i < handle->pending_async_ctrls; i++)
+ uvc_status_put(handle->stream->dev);
}
/*
diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
index 5d4e967938af..63d1d06d3ff6 100644
--- a/drivers/media/usb/uvc/uvc_v4l2.c
+++ b/drivers/media/usb/uvc/uvc_v4l2.c
@@ -670,6 +670,9 @@ static int uvc_v4l2_release(struct file *file)
if (uvc_has_privileges(handle))
uvc_queue_release(&stream->queue);
+ if (handle->is_streaming)
+ uvc_status_put(stream->dev);
+
/* Release the file handle. */
uvc_dismiss_privileges(handle);
v4l2_fh_del(&handle->vfh);
@@ -832,8 +835,10 @@ static int uvc_ioctl_streamon(struct file *file, void *fh,
return 0;
ret = uvc_queue_streamon(&stream->queue, type);
- if (!ret)
+ if (!ret) {
handle->is_streaming = true;
+ uvc_status_get(stream->dev);
+ }
return ret;
@@ -851,7 +856,10 @@ static int uvc_ioctl_streamoff(struct file *file, void *fh,
guard(mutex)(&stream->mutex);
uvc_queue_streamoff(&stream->queue, type);
- handle->is_streaming = false;
+ if (handle->is_streaming) {
+ handle->is_streaming = false;
+ uvc_status_put(stream->dev);
+ }
return 0;
}
@@ -1388,6 +1396,8 @@ static long uvc_v4l2_compat_ioctl32(struct file *file,
void __user *up = compat_ptr(arg);
long ret;
+ guard(uvc_status)(handle->stream->dev);
+
switch (cmd) {
case UVCIOC_CTRL_MAP32:
ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
@@ -1422,6 +1432,16 @@ static long uvc_v4l2_compat_ioctl32(struct file *file,
}
#endif
+static long uvc_v4l2_video_ioctl2(struct file *file,
+ unsigned int cmd, unsigned long arg)
+{
+ struct uvc_fh *handle = file->private_data;
+
+ guard(uvc_status)(handle->stream->dev);
+
+ return video_ioctl2(file, cmd, arg);
+}
+
static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
size_t count, loff_t *ppos)
{
@@ -1507,7 +1527,7 @@ const struct v4l2_file_operations uvc_fops = {
.owner = THIS_MODULE,
.open = uvc_v4l2_open,
.release = uvc_v4l2_release,
- .unlocked_ioctl = video_ioctl2,
+ .unlocked_ioctl = uvc_v4l2_video_ioctl2,
#ifdef CONFIG_COMPAT
.compat_ioctl32 = uvc_v4l2_compat_ioctl32,
#endif
--
2.48.1.502.g6dc24dfdaf-goog
^ permalink raw reply related [flat|nested] 11+ messages in thread* Re: [PATCH v3 4/6] media: uvcvideo: Increase/decrease the PM counter per IOCTL
2025-02-06 19:47 ` [PATCH v3 4/6] media: uvcvideo: Increase/decrease the PM counter per IOCTL Ricardo Ribalda
@ 2025-02-23 16:55 ` Laurent Pinchart
0 siblings, 0 replies; 11+ messages in thread
From: Laurent Pinchart @ 2025-02-23 16:55 UTC (permalink / raw)
To: Ricardo Ribalda
Cc: Hans de Goede, Mauro Carvalho Chehab, Guennadi Liakhovetski,
linux-media, linux-kernel, Mauro Carvalho Chehab
Hi Ricardo,
Thank you for the patch.
On Thu, Feb 06, 2025 at 07:47:03PM +0000, Ricardo Ribalda wrote:
> Now we call uvc_status_get/put from the device open/close. This low
> level of granularity might leave the camera powered on in situations
> where it is not needed.
>
> Increase the granularity by increasing and decreasing the Power
> Management counter per ioctl. There are two special cases where the
> power management outlives the ioctl: async controls and streamon. Handle
> those cases as well.
>
> In a future patch, we will remove the uvc_status_get/put from open/close.
>
> Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> ---
> drivers/media/usb/uvc/uvc_ctrl.c | 13 +++++++++++--
> drivers/media/usb/uvc/uvc_v4l2.c | 26 +++++++++++++++++++++++---
> 2 files changed, 34 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c
> index 4e58476d305e..97c1141a45b3 100644
> --- a/drivers/media/usb/uvc/uvc_ctrl.c
> +++ b/drivers/media/usb/uvc/uvc_ctrl.c
> @@ -1594,12 +1594,15 @@ static void uvc_ctrl_set_handle(struct uvc_fh *handle, struct uvc_control *ctrl,
>
> if (ctrl->handle) {
> WARN_ON(!ctrl->handle->pending_async_ctrls);
> - if (ctrl->handle->pending_async_ctrls)
> + if (ctrl->handle->pending_async_ctrls) {
> ctrl->handle->pending_async_ctrls--;
> + uvc_status_put(handle->chain->dev);
That's not the right API. uvc_status_* is about the status endpoint, not
power management in general.
> + }
> }
>
> ctrl->handle = new_handle;
> handle->pending_async_ctrls++;
> + uvc_status_get(handle->chain->dev);
> return;
> }
>
> @@ -1611,6 +1614,7 @@ static void uvc_ctrl_set_handle(struct uvc_fh *handle, struct uvc_control *ctrl,
> if (WARN_ON(!handle->pending_async_ctrls))
> return;
> handle->pending_async_ctrls--;
> + uvc_status_put(handle->chain->dev);
> }
>
> void uvc_ctrl_status_event(struct uvc_video_chain *chain,
> @@ -2815,6 +2819,7 @@ int uvc_ctrl_init_device(struct uvc_device *dev)
> void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
> {
> struct uvc_entity *entity;
> + int i;
>
> guard(mutex)(&handle->chain->ctrl_mutex);
>
> @@ -2829,7 +2834,11 @@ void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
> }
> }
>
> - WARN_ON(handle->pending_async_ctrls);
> + if (!WARN_ON(handle->pending_async_ctrls))
> + return;
> +
> + for (i = 0; i < handle->pending_async_ctrls; i++)
> + uvc_status_put(handle->stream->dev);
> }
>
> /*
> diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
> index 5d4e967938af..63d1d06d3ff6 100644
> --- a/drivers/media/usb/uvc/uvc_v4l2.c
> +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> @@ -670,6 +670,9 @@ static int uvc_v4l2_release(struct file *file)
> if (uvc_has_privileges(handle))
> uvc_queue_release(&stream->queue);
>
> + if (handle->is_streaming)
> + uvc_status_put(stream->dev);
> +
> /* Release the file handle. */
> uvc_dismiss_privileges(handle);
> v4l2_fh_del(&handle->vfh);
> @@ -832,8 +835,10 @@ static int uvc_ioctl_streamon(struct file *file, void *fh,
> return 0;
>
> ret = uvc_queue_streamon(&stream->queue, type);
> - if (!ret)
> + if (!ret) {
> handle->is_streaming = true;
> + uvc_status_get(stream->dev);
> + }
>
> return ret;
>
> @@ -851,7 +856,10 @@ static int uvc_ioctl_streamoff(struct file *file, void *fh,
> guard(mutex)(&stream->mutex);
>
> uvc_queue_streamoff(&stream->queue, type);
> - handle->is_streaming = false;
> + if (handle->is_streaming) {
> + handle->is_streaming = false;
> + uvc_status_put(stream->dev);
> + }
>
> return 0;
> }
> @@ -1388,6 +1396,8 @@ static long uvc_v4l2_compat_ioctl32(struct file *file,
> void __user *up = compat_ptr(arg);
> long ret;
>
> + guard(uvc_status)(handle->stream->dev);
> +
> switch (cmd) {
> case UVCIOC_CTRL_MAP32:
> ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
> @@ -1422,6 +1432,16 @@ static long uvc_v4l2_compat_ioctl32(struct file *file,
> }
> #endif
>
> +static long uvc_v4l2_video_ioctl2(struct file *file,
> + unsigned int cmd, unsigned long arg)
> +{
> + struct uvc_fh *handle = file->private_data;
> +
> + guard(uvc_status)(handle->stream->dev);
> +
> + return video_ioctl2(file, cmd, arg);
> +}
> +
> static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
> size_t count, loff_t *ppos)
> {
> @@ -1507,7 +1527,7 @@ const struct v4l2_file_operations uvc_fops = {
> .owner = THIS_MODULE,
> .open = uvc_v4l2_open,
> .release = uvc_v4l2_release,
> - .unlocked_ioctl = video_ioctl2,
> + .unlocked_ioctl = uvc_v4l2_video_ioctl2,
> #ifdef CONFIG_COMPAT
> .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
> #endif
--
Regards,
Laurent Pinchart
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v3 5/6] media: uvcvideo: Make power management granular
2025-02-06 19:46 [PATCH v3 0/6] media: uvcvideo: Implement Granular Power Saving Ricardo Ribalda
` (3 preceding siblings ...)
2025-02-06 19:47 ` [PATCH v3 4/6] media: uvcvideo: Increase/decrease the PM counter per IOCTL Ricardo Ribalda
@ 2025-02-06 19:47 ` Ricardo Ribalda
2025-02-06 19:47 ` [PATCH v3 6/6] media: uvcvideo: Do not turn on the camera for some ioctls Ricardo Ribalda
5 siblings, 0 replies; 11+ messages in thread
From: Ricardo Ribalda @ 2025-02-06 19:47 UTC (permalink / raw)
To: Laurent Pinchart, Hans de Goede, Mauro Carvalho Chehab,
Guennadi Liakhovetski
Cc: linux-media, linux-kernel, Mauro Carvalho Chehab, Ricardo Ribalda
Now that every ioctl takes care of their power management we can remove
the "global" power management.
Despite its size, this is a relatively big change. We hope that there
are no size effects of it. If there are some specific devices that
miss-behave, we can add a small quirk for them.
This patch introduces a behavioral change for the uvc "trigger" button.
It will not work unless the camera is streaming. We consider that this
the most common (if not the only) usecase and therefore we do not consider
it a regression.
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
drivers/media/usb/uvc/uvc_v4l2.c | 8 --------
1 file changed, 8 deletions(-)
diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
index 63d1d06d3ff6..7fddea100ace 100644
--- a/drivers/media/usb/uvc/uvc_v4l2.c
+++ b/drivers/media/usb/uvc/uvc_v4l2.c
@@ -631,7 +631,6 @@ static int uvc_v4l2_open(struct file *file)
{
struct uvc_streaming *stream;
struct uvc_fh *handle;
- int ret = 0;
stream = video_drvdata(file);
uvc_dbg(stream->dev, CALLS, "%s\n", __func__);
@@ -641,12 +640,6 @@ static int uvc_v4l2_open(struct file *file)
if (!handle)
return -ENOMEM;
- ret = uvc_status_get(stream->dev);
- if (ret) {
- kfree(handle);
- return ret;
- }
-
v4l2_fh_init(&handle->vfh, &stream->vdev);
v4l2_fh_add(&handle->vfh);
handle->chain = stream->chain;
@@ -680,7 +673,6 @@ static int uvc_v4l2_release(struct file *file)
kfree(handle);
file->private_data = NULL;
- uvc_status_put(stream->dev);
return 0;
}
--
2.48.1.502.g6dc24dfdaf-goog
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH v3 6/6] media: uvcvideo: Do not turn on the camera for some ioctls
2025-02-06 19:46 [PATCH v3 0/6] media: uvcvideo: Implement Granular Power Saving Ricardo Ribalda
` (4 preceding siblings ...)
2025-02-06 19:47 ` [PATCH v3 5/6] media: uvcvideo: Make power management granular Ricardo Ribalda
@ 2025-02-06 19:47 ` Ricardo Ribalda
5 siblings, 0 replies; 11+ messages in thread
From: Ricardo Ribalda @ 2025-02-06 19:47 UTC (permalink / raw)
To: Laurent Pinchart, Hans de Goede, Mauro Carvalho Chehab,
Guennadi Liakhovetski
Cc: linux-media, linux-kernel, Mauro Carvalho Chehab, Ricardo Ribalda
There are some ioctls that do not need to turn on the camera. Do not
call uvc_status_get in those cases.
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
drivers/media/usb/uvc/uvc_v4l2.c | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
index 7fddea100ace..0f022174285f 100644
--- a/drivers/media/usb/uvc/uvc_v4l2.c
+++ b/drivers/media/usb/uvc/uvc_v4l2.c
@@ -1429,6 +1429,26 @@ static long uvc_v4l2_video_ioctl2(struct file *file,
{
struct uvc_fh *handle = file->private_data;
+ /* The following IOCTLs do not need to turn on the camera. */
+ switch (cmd) {
+ case VIDIOC_CREATE_BUFS:
+ case VIDIOC_DQBUF:
+ case VIDIOC_ENUM_FMT:
+ case VIDIOC_ENUM_FRAMEINTERVALS:
+ case VIDIOC_ENUM_FRAMESIZES:
+ case VIDIOC_ENUMINPUT:
+ case VIDIOC_EXPBUF:
+ case VIDIOC_G_FMT:
+ case VIDIOC_G_PARM:
+ case VIDIOC_G_SELECTION:
+ case VIDIOC_QBUF:
+ case VIDIOC_QUERYCAP:
+ case VIDIOC_REQBUFS:
+ case VIDIOC_SUBSCRIBE_EVENT:
+ case VIDIOC_UNSUBSCRIBE_EVENT:
+ return video_ioctl2(file, cmd, arg);
+ }
+
guard(uvc_status)(handle->stream->dev);
return video_ioctl2(file, cmd, arg);
--
2.48.1.502.g6dc24dfdaf-goog
^ permalink raw reply related [flat|nested] 11+ messages in thread