* [PATCH v4 1/5] media: uvcvideo: Keep streaming state in the file handle
2025-02-26 14:23 [PATCH v4 0/5] media: uvcvideo: Implement Granular Power Saving Ricardo Ribalda
@ 2025-02-26 14:23 ` Ricardo Ribalda
2025-03-03 14:07 ` Hans de Goede
2025-02-26 14:23 ` [PATCH v4 2/5] media: uvcvideo: Create uvc_pm_(get|put) functions Ricardo Ribalda
` (3 subsequent siblings)
4 siblings, 1 reply; 11+ messages in thread
From: Ricardo Ribalda @ 2025-02-26 14:23 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.
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
drivers/media/usb/uvc/uvc_v4l2.c | 18 +++++++++++++-----
drivers/media/usb/uvc/uvcvideo.h | 1 +
2 files changed, 14 insertions(+), 5 deletions(-)
diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
index 93c6cdb23881..f9cd6db759c5 100644
--- a/drivers/media/usb/uvc/uvc_v4l2.c
+++ b/drivers/media/usb/uvc/uvc_v4l2.c
@@ -835,11 +835,18 @@ 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)
+ return ret;
- return ret;
+ handle->is_streaming = true;
+
+ return 0;
}
static int uvc_ioctl_streamoff(struct file *file, void *fh,
@@ -851,9 +858,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.658.g4767266eb4-goog
^ permalink raw reply [flat|nested] 11+ messages in thread* Re: [PATCH v4 1/5] media: uvcvideo: Keep streaming state in the file handle
2025-02-26 14:23 ` [PATCH v4 1/5] media: uvcvideo: Keep streaming state in the file handle Ricardo Ribalda
@ 2025-03-03 14:07 ` Hans de Goede
0 siblings, 0 replies; 11+ messages in thread
From: Hans de Goede @ 2025-03-03 14:07 UTC (permalink / raw)
To: Ricardo Ribalda, Laurent Pinchart, Mauro Carvalho Chehab,
Guennadi Liakhovetski
Cc: linux-media, linux-kernel, Mauro Carvalho Chehab
Hi,
On 26-Feb-25 15:23, Ricardo Ribalda wrote:
> 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.
>
> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
Thanks, patch looks good to me:
Reviewed-by: Hans de Goede <hdegoede@redhat.com>
Regards,
Hans
> ---
> drivers/media/usb/uvc/uvc_v4l2.c | 18 +++++++++++++-----
> drivers/media/usb/uvc/uvcvideo.h | 1 +
> 2 files changed, 14 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
> index 93c6cdb23881..f9cd6db759c5 100644
> --- a/drivers/media/usb/uvc/uvc_v4l2.c
> +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> @@ -835,11 +835,18 @@ 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)
> + return ret;
>
> - return ret;
> + handle->is_streaming = true;
> +
> + return 0;
> }
>
> static int uvc_ioctl_streamoff(struct file *file, void *fh,
> @@ -851,9 +858,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 {
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v4 2/5] media: uvcvideo: Create uvc_pm_(get|put) functions
2025-02-26 14:23 [PATCH v4 0/5] media: uvcvideo: Implement Granular Power Saving Ricardo Ribalda
2025-02-26 14:23 ` [PATCH v4 1/5] media: uvcvideo: Keep streaming state in the file handle Ricardo Ribalda
@ 2025-02-26 14:23 ` Ricardo Ribalda
2025-03-03 14:26 ` Hans de Goede
2025-02-26 14:23 ` [PATCH v4 3/5] media: uvcvideo: Increase/decrease the PM counter per IOCTL Ricardo Ribalda
` (2 subsequent siblings)
4 siblings, 1 reply; 11+ messages in thread
From: Ricardo Ribalda @ 2025-02-26 14:23 UTC (permalink / raw)
To: Laurent Pinchart, Hans de Goede, Mauro Carvalho Chehab,
Guennadi Liakhovetski
Cc: linux-media, linux-kernel, Mauro Carvalho Chehab, Ricardo Ribalda
Most of the times that we have to call uvc_status_(get|put) we need to
call the usb_autopm_ functions.
Create a new pair of functions that automate this for us. 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_v4l2.c | 36 ++++++++++++++++++++++++------------
drivers/media/usb/uvc/uvcvideo.h | 4 ++++
2 files changed, 28 insertions(+), 12 deletions(-)
diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
index f9cd6db759c5..de1e105f7263 100644
--- a/drivers/media/usb/uvc/uvc_v4l2.c
+++ b/drivers/media/usb/uvc/uvc_v4l2.c
@@ -26,6 +26,27 @@
#include "uvcvideo.h"
+int uvc_pm_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;
+}
+
+void uvc_pm_put(struct uvc_device *dev)
+{
+ uvc_status_put(dev);
+ usb_autopm_put_interface(dev->intf);
+}
+
static int uvc_acquire_privileges(struct uvc_fh *handle);
static int uvc_control_add_xu_mapping(struct uvc_video_chain *chain,
@@ -636,20 +657,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);
+ ret = uvc_pm_get(stream->dev);
if (ret) {
- usb_autopm_put_interface(stream->dev->intf);
kfree(handle);
return ret;
}
@@ -684,9 +698,7 @@ static int uvc_v4l2_release(struct file *file)
kfree(handle);
file->private_data = NULL;
- uvc_status_put(stream->dev);
-
- usb_autopm_put_interface(stream->dev->intf);
+ uvc_pm_put(stream->dev);
return 0;
}
diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h
index bc87e1f2c669..fbe3649c7cd6 100644
--- a/drivers/media/usb/uvc/uvcvideo.h
+++ b/drivers/media/usb/uvc/uvcvideo.h
@@ -763,6 +763,10 @@ void uvc_status_suspend(struct uvc_device *dev);
int uvc_status_get(struct uvc_device *dev);
void uvc_status_put(struct uvc_device *dev);
+/* PM */
+int uvc_pm_get(struct uvc_device *dev);
+void uvc_pm_put(struct uvc_device *dev);
+
/* Controls */
extern const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops;
--
2.48.1.658.g4767266eb4-goog
^ permalink raw reply [flat|nested] 11+ messages in thread* Re: [PATCH v4 2/5] media: uvcvideo: Create uvc_pm_(get|put) functions
2025-02-26 14:23 ` [PATCH v4 2/5] media: uvcvideo: Create uvc_pm_(get|put) functions Ricardo Ribalda
@ 2025-03-03 14:26 ` Hans de Goede
0 siblings, 0 replies; 11+ messages in thread
From: Hans de Goede @ 2025-03-03 14:26 UTC (permalink / raw)
To: Ricardo Ribalda, Laurent Pinchart, Mauro Carvalho Chehab,
Guennadi Liakhovetski
Cc: linux-media, linux-kernel, Mauro Carvalho Chehab
Hi,
On 26-Feb-25 15:23, Ricardo Ribalda wrote:
> Most of the times that we have to call uvc_status_(get|put) we need to
> call the usb_autopm_ functions.
>
> Create a new pair of functions that automate this for us. This
> simplifies the current code and future PM changes in the driver.
>
> Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
Thanks, patch looks good to me:
Reviewed-by: Hans de Goede <hdegoede@redhat.com>
Regards,
Hans
> ---
> drivers/media/usb/uvc/uvc_v4l2.c | 36 ++++++++++++++++++++++++------------
> drivers/media/usb/uvc/uvcvideo.h | 4 ++++
> 2 files changed, 28 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
> index f9cd6db759c5..de1e105f7263 100644
> --- a/drivers/media/usb/uvc/uvc_v4l2.c
> +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> @@ -26,6 +26,27 @@
>
> #include "uvcvideo.h"
>
> +int uvc_pm_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;
> +}
> +
> +void uvc_pm_put(struct uvc_device *dev)
> +{
> + uvc_status_put(dev);
> + usb_autopm_put_interface(dev->intf);
> +}
> +
> static int uvc_acquire_privileges(struct uvc_fh *handle);
>
> static int uvc_control_add_xu_mapping(struct uvc_video_chain *chain,
> @@ -636,20 +657,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);
> + ret = uvc_pm_get(stream->dev);
> if (ret) {
> - usb_autopm_put_interface(stream->dev->intf);
> kfree(handle);
> return ret;
> }
> @@ -684,9 +698,7 @@ static int uvc_v4l2_release(struct file *file)
> kfree(handle);
> file->private_data = NULL;
>
> - uvc_status_put(stream->dev);
> -
> - usb_autopm_put_interface(stream->dev->intf);
> + uvc_pm_put(stream->dev);
> return 0;
> }
>
> diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h
> index bc87e1f2c669..fbe3649c7cd6 100644
> --- a/drivers/media/usb/uvc/uvcvideo.h
> +++ b/drivers/media/usb/uvc/uvcvideo.h
> @@ -763,6 +763,10 @@ void uvc_status_suspend(struct uvc_device *dev);
> int uvc_status_get(struct uvc_device *dev);
> void uvc_status_put(struct uvc_device *dev);
>
> +/* PM */
> +int uvc_pm_get(struct uvc_device *dev);
> +void uvc_pm_put(struct uvc_device *dev);
> +
> /* Controls */
> extern const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops;
>
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v4 3/5] media: uvcvideo: Increase/decrease the PM counter per IOCTL
2025-02-26 14:23 [PATCH v4 0/5] media: uvcvideo: Implement Granular Power Saving Ricardo Ribalda
2025-02-26 14:23 ` [PATCH v4 1/5] media: uvcvideo: Keep streaming state in the file handle Ricardo Ribalda
2025-02-26 14:23 ` [PATCH v4 2/5] media: uvcvideo: Create uvc_pm_(get|put) functions Ricardo Ribalda
@ 2025-02-26 14:23 ` Ricardo Ribalda
2025-03-03 14:34 ` Hans de Goede
2025-02-26 14:23 ` [PATCH v4 4/5] media: uvcvideo: Make power management granular Ricardo Ribalda
2025-02-26 14:23 ` [PATCH v4 5/5] media: uvcvideo: Do not turn on the camera for some ioctls Ricardo Ribalda
4 siblings, 1 reply; 11+ messages in thread
From: Ricardo Ribalda @ 2025-02-26 14:23 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_pm_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_pm_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 | 23 +++++++++++++++++++++--
drivers/media/usb/uvc/uvcvideo.h | 1 +
3 files changed, 33 insertions(+), 4 deletions(-)
diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c
index 4e58476d305e..47188c7f96c7 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_pm_put(handle->chain->dev);
+ }
}
ctrl->handle = new_handle;
handle->pending_async_ctrls++;
+ uvc_pm_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_pm_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_pm_put(handle->stream->dev);
}
/*
diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
index de1e105f7263..1c9ac72be58a 100644
--- a/drivers/media/usb/uvc/uvc_v4l2.c
+++ b/drivers/media/usb/uvc/uvc_v4l2.c
@@ -691,6 +691,9 @@ static int uvc_v4l2_release(struct file *file)
if (uvc_has_privileges(handle))
uvc_queue_release(&stream->queue);
+ if (handle->is_streaming)
+ uvc_pm_put(stream->dev);
+
/* Release the file handle. */
uvc_dismiss_privileges(handle);
v4l2_fh_del(&handle->vfh);
@@ -857,6 +860,7 @@ static int uvc_ioctl_streamon(struct file *file, void *fh,
return ret;
handle->is_streaming = true;
+ uvc_pm_get(stream->dev);
return 0;
}
@@ -873,7 +877,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_pm_put(stream->dev);
+ }
return 0;
}
@@ -1410,6 +1417,8 @@ static long uvc_v4l2_compat_ioctl32(struct file *file,
void __user *up = compat_ptr(arg);
long ret;
+ guard(uvc_pm)(handle->stream->dev);
+
switch (cmd) {
case UVCIOC_CTRL_MAP32:
ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
@@ -1444,6 +1453,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_pm)(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)
{
@@ -1529,7 +1548,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
diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h
index fbe3649c7cd6..eb8e374fa4c5 100644
--- a/drivers/media/usb/uvc/uvcvideo.h
+++ b/drivers/media/usb/uvc/uvcvideo.h
@@ -766,6 +766,7 @@ void uvc_status_put(struct uvc_device *dev);
/* PM */
int uvc_pm_get(struct uvc_device *dev);
void uvc_pm_put(struct uvc_device *dev);
+DEFINE_GUARD(uvc_pm, struct uvc_device *, uvc_pm_get(_T), uvc_pm_put(_T))
/* Controls */
extern const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops;
--
2.48.1.658.g4767266eb4-goog
^ permalink raw reply [flat|nested] 11+ messages in thread* Re: [PATCH v4 3/5] media: uvcvideo: Increase/decrease the PM counter per IOCTL
2025-02-26 14:23 ` [PATCH v4 3/5] media: uvcvideo: Increase/decrease the PM counter per IOCTL Ricardo Ribalda
@ 2025-03-03 14:34 ` Hans de Goede
0 siblings, 0 replies; 11+ messages in thread
From: Hans de Goede @ 2025-03-03 14:34 UTC (permalink / raw)
To: Ricardo Ribalda, Laurent Pinchart, Mauro Carvalho Chehab,
Guennadi Liakhovetski
Cc: linux-media, linux-kernel, Mauro Carvalho Chehab
Hi,
On 26-Feb-25 15:23, Ricardo Ribalda wrote:
> Now we call uvc_pm_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_pm_get/put from open/close.
>
> Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
Thanks, patch looks good to me:
Reviewed-by: Hans de Goede <hdegoede@redhat.com>
Regards,
Hans
> ---
> drivers/media/usb/uvc/uvc_ctrl.c | 13 +++++++++++--
> drivers/media/usb/uvc/uvc_v4l2.c | 23 +++++++++++++++++++++--
> drivers/media/usb/uvc/uvcvideo.h | 1 +
> 3 files changed, 33 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c
> index 4e58476d305e..47188c7f96c7 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_pm_put(handle->chain->dev);
> + }
> }
>
> ctrl->handle = new_handle;
> handle->pending_async_ctrls++;
> + uvc_pm_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_pm_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_pm_put(handle->stream->dev);
> }
>
> /*
> diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
> index de1e105f7263..1c9ac72be58a 100644
> --- a/drivers/media/usb/uvc/uvc_v4l2.c
> +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> @@ -691,6 +691,9 @@ static int uvc_v4l2_release(struct file *file)
> if (uvc_has_privileges(handle))
> uvc_queue_release(&stream->queue);
>
> + if (handle->is_streaming)
> + uvc_pm_put(stream->dev);
> +
> /* Release the file handle. */
> uvc_dismiss_privileges(handle);
> v4l2_fh_del(&handle->vfh);
> @@ -857,6 +860,7 @@ static int uvc_ioctl_streamon(struct file *file, void *fh,
> return ret;
>
> handle->is_streaming = true;
> + uvc_pm_get(stream->dev);
>
> return 0;
> }
> @@ -873,7 +877,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_pm_put(stream->dev);
> + }
>
> return 0;
> }
> @@ -1410,6 +1417,8 @@ static long uvc_v4l2_compat_ioctl32(struct file *file,
> void __user *up = compat_ptr(arg);
> long ret;
>
> + guard(uvc_pm)(handle->stream->dev);
> +
> switch (cmd) {
> case UVCIOC_CTRL_MAP32:
> ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
> @@ -1444,6 +1453,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_pm)(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)
> {
> @@ -1529,7 +1548,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
> diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h
> index fbe3649c7cd6..eb8e374fa4c5 100644
> --- a/drivers/media/usb/uvc/uvcvideo.h
> +++ b/drivers/media/usb/uvc/uvcvideo.h
> @@ -766,6 +766,7 @@ void uvc_status_put(struct uvc_device *dev);
> /* PM */
> int uvc_pm_get(struct uvc_device *dev);
> void uvc_pm_put(struct uvc_device *dev);
> +DEFINE_GUARD(uvc_pm, struct uvc_device *, uvc_pm_get(_T), uvc_pm_put(_T))
>
> /* Controls */
> extern const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops;
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v4 4/5] media: uvcvideo: Make power management granular
2025-02-26 14:23 [PATCH v4 0/5] media: uvcvideo: Implement Granular Power Saving Ricardo Ribalda
` (2 preceding siblings ...)
2025-02-26 14:23 ` [PATCH v4 3/5] media: uvcvideo: Increase/decrease the PM counter per IOCTL Ricardo Ribalda
@ 2025-02-26 14:23 ` Ricardo Ribalda
2025-03-03 14:37 ` Hans de Goede
2025-02-26 14:23 ` [PATCH v4 5/5] media: uvcvideo: Do not turn on the camera for some ioctls Ricardo Ribalda
4 siblings, 1 reply; 11+ messages in thread
From: Ricardo Ribalda @ 2025-02-26 14:23 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 1c9ac72be58a..6af93e00b304 100644
--- a/drivers/media/usb/uvc/uvc_v4l2.c
+++ b/drivers/media/usb/uvc/uvc_v4l2.c
@@ -652,7 +652,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__);
@@ -662,12 +661,6 @@ static int uvc_v4l2_open(struct file *file)
if (!handle)
return -ENOMEM;
- ret = uvc_pm_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;
@@ -701,7 +694,6 @@ static int uvc_v4l2_release(struct file *file)
kfree(handle);
file->private_data = NULL;
- uvc_pm_put(stream->dev);
return 0;
}
--
2.48.1.658.g4767266eb4-goog
^ permalink raw reply [flat|nested] 11+ messages in thread* Re: [PATCH v4 4/5] media: uvcvideo: Make power management granular
2025-02-26 14:23 ` [PATCH v4 4/5] media: uvcvideo: Make power management granular Ricardo Ribalda
@ 2025-03-03 14:37 ` Hans de Goede
0 siblings, 0 replies; 11+ messages in thread
From: Hans de Goede @ 2025-03-03 14:37 UTC (permalink / raw)
To: Ricardo Ribalda, Laurent Pinchart, Mauro Carvalho Chehab,
Guennadi Liakhovetski
Cc: linux-media, linux-kernel, Mauro Carvalho Chehab
Hi,
On 26-Feb-25 15:23, Ricardo Ribalda wrote:
> 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.
You may want to expand this part of the commit message a bit and make it
clear that before the "trigger" button would work as long as an open has
/dev/video# open and now it only works when the camera is actually
streaming.
I agree that in practice this is not an issue since apps only open
the /dev/video# node for a longer period at a time (vs short open
for query/probe) when they actually want to stream.
Otherwise the patch looks good to me:
Reviewed-by: Hans de Goede <hdegoede@redhat.com>
Regards,
Hans
>
> 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 1c9ac72be58a..6af93e00b304 100644
> --- a/drivers/media/usb/uvc/uvc_v4l2.c
> +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> @@ -652,7 +652,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__);
> @@ -662,12 +661,6 @@ static int uvc_v4l2_open(struct file *file)
> if (!handle)
> return -ENOMEM;
>
> - ret = uvc_pm_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;
> @@ -701,7 +694,6 @@ static int uvc_v4l2_release(struct file *file)
> kfree(handle);
> file->private_data = NULL;
>
> - uvc_pm_put(stream->dev);
> return 0;
> }
>
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v4 5/5] media: uvcvideo: Do not turn on the camera for some ioctls
2025-02-26 14:23 [PATCH v4 0/5] media: uvcvideo: Implement Granular Power Saving Ricardo Ribalda
` (3 preceding siblings ...)
2025-02-26 14:23 ` [PATCH v4 4/5] media: uvcvideo: Make power management granular Ricardo Ribalda
@ 2025-02-26 14:23 ` Ricardo Ribalda
2025-03-03 14:42 ` Hans de Goede
4 siblings, 1 reply; 11+ messages in thread
From: Ricardo Ribalda @ 2025-02-26 14:23 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_pm_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 6af93e00b304..de8d26164996 100644
--- a/drivers/media/usb/uvc/uvc_v4l2.c
+++ b/drivers/media/usb/uvc/uvc_v4l2.c
@@ -1450,6 +1450,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_pm)(handle->stream->dev);
return video_ioctl2(file, cmd, arg);
--
2.48.1.658.g4767266eb4-goog
^ permalink raw reply [flat|nested] 11+ messages in thread* Re: [PATCH v4 5/5] media: uvcvideo: Do not turn on the camera for some ioctls
2025-02-26 14:23 ` [PATCH v4 5/5] media: uvcvideo: Do not turn on the camera for some ioctls Ricardo Ribalda
@ 2025-03-03 14:42 ` Hans de Goede
0 siblings, 0 replies; 11+ messages in thread
From: Hans de Goede @ 2025-03-03 14:42 UTC (permalink / raw)
To: Ricardo Ribalda, Laurent Pinchart, Mauro Carvalho Chehab,
Guennadi Liakhovetski
Cc: linux-media, linux-kernel, Mauro Carvalho Chehab
Hi Ricardo,
On 26-Feb-25 15:23, Ricardo Ribalda wrote:
> There are some ioctls that do not need to turn on the camera. Do not
> call uvc_pm_get in those cases.
>
> Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
Thanks, patch looks good to me:
Reviewed-by: Hans de Goede <hdegoede@redhat.com>
Regards,
Hans
> ---
> 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 6af93e00b304..de8d26164996 100644
> --- a/drivers/media/usb/uvc/uvc_v4l2.c
> +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> @@ -1450,6 +1450,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_pm)(handle->stream->dev);
>
> return video_ioctl2(file, cmd, arg);
>
^ permalink raw reply [flat|nested] 11+ messages in thread