From: Chris Bainbridge <chris.bainbridge@canonical.com>
To: linux-media@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, laurent.pinchart@ideasonboard.com,
hansg@kernel.org, mchehab@kernel.org
Subject: [PATCH 2/3] media: uvcvideo: Restore auto controls first and skip inactive slaves
Date: Tue, 24 Nov 2026 10:06:22 +0000 [thread overview]
Message-ID: <20261124100623.2227323-3-chris.bainbridge@canonical.com> (raw)
In-Reply-To: <20261124100623.2227323-1-chris.bainbridge@canonical.com>
A UVC device rejects a SET_CUR request for a manual control while its
master auto control is enabled. On the Logitech C270 (046d:0825),
restoring WHITE_BALANCE_TEMPERATURE while WHITE_BALANCE_TEMPERATURE_AUTO
is enabled stalls the request with UVC error code 7 (Invalid Request)
and leaves automatic white balance wedged until power cycle, while
aborting the restoration of the remaining controls of the processing
unit. Two rules are needed to avoid this.
First, restore the master controls before the other controls of each
entity. uvc_ctrl_commit_entity() sends the controls of an entity in
index order: Auto-Exposure Mode (index 1) precedes Exposure Time
(index 3) in the camera terminal, but Focus Absolute (index 5) precedes
Focus Auto (index 17), and in the processing unit Hue (index 2), White
Balance Temperature (index 6) and White Balance Component (index 7) all
precede their corresponding auto controls (indices 11, 12 and 13). After
a reset the device has auto white balance enabled at its default, so
even when the user has set the auto control to manual mode, the manual
value is written while the device still has auto enabled, and is
rejected. Restore the masters first (committing the first pass only when
at least one master is dirty), so that manual controls are written with
their master in its final state. A control is a master if any of its
mappings has a non-zero slave_ids[] entry.
Second, skip the slave controls that are currently inactive. When the
master was restored in the first pass or is already loaded in the cache,
decode its value without generating any USB traffic; otherwise query the
master via GET_CUR using __uvc_ctrl_load_cur() (taking struct uvc_device
directly). If the master control is write-only or GET_CUR fails, treat
the slave as active rather than decoding an unpopulated zero buffer,
matching uvc_ctrl_is_accessible(). Clear ctrl->loaded when skipping an
inactive slave so that subsequent reads fetch the firmware-driven value
from the hardware, even on devices that omit UVC_CONTROL_CAP_AUTOUPDATE
in GET_INFO.
This addresses the restore order concern noted in the TODO comment for
auto/manual control pairs.
Fixes: c0efd232929c ("V4L/DVB (8145a): USB Video Class driver")
Signed-off-by: Chris Bainbridge <chris.bainbridge@canonical.com>
---
drivers/media/usb/uvc/uvc_ctrl.c | 152 +++++++++++++++++++++++++------
1 file changed, 126 insertions(+), 26 deletions(-)
diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c
index f70fa37800a4..d11ad3da1d50 100644
--- a/drivers/media/usb/uvc/uvc_ctrl.c
+++ b/drivers/media/usb/uvc/uvc_ctrl.c
@@ -1463,7 +1463,7 @@ static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
return 0;
}
-static int __uvc_ctrl_load_cur(struct uvc_video_chain *chain,
+static int __uvc_ctrl_load_cur(struct uvc_device *dev,
struct uvc_control *ctrl)
{
u8 *data;
@@ -1482,13 +1482,12 @@ static int __uvc_ctrl_load_cur(struct uvc_video_chain *chain,
}
if (ctrl->entity->get_cur)
- ret = ctrl->entity->get_cur(chain->dev, ctrl->entity,
+ ret = ctrl->entity->get_cur(dev, ctrl->entity,
ctrl->info.selector, data,
ctrl->info.size);
else
- ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
- ctrl->entity->id, chain->dev->intfnum,
- ctrl->info.selector, data,
+ ret = uvc_query_ctrl(dev, UVC_GET_CUR, ctrl->entity->id,
+ dev->intfnum, ctrl->info.selector, data,
ctrl->info.size);
if (ret < 0)
@@ -1509,7 +1508,7 @@ static int __uvc_ctrl_get(struct uvc_video_chain *chain,
if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
return -EACCES;
- ret = __uvc_ctrl_load_cur(chain, ctrl);
+ ret = __uvc_ctrl_load_cur(chain->dev, ctrl);
if (ret < 0)
return ret;
@@ -2572,7 +2571,7 @@ static int uvc_mapping_get_xctrl_compound(struct uvc_video_chain *chain,
return -ENOMEM;
if (which == V4L2_CTRL_WHICH_CUR_VAL)
- ret = __uvc_ctrl_load_cur(chain, ctrl);
+ ret = __uvc_ctrl_load_cur(chain->dev, ctrl);
else
ret = uvc_ctrl_populate_cache(chain, ctrl);
@@ -2816,7 +2815,7 @@ int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl)
* operation.
*/
if ((ctrl->info.size * 8) != mapping->size) {
- ret = __uvc_ctrl_load_cur(chain, ctrl);
+ ret = __uvc_ctrl_load_cur(chain->dev, ctrl);
if (ret < 0)
return ret;
}
@@ -3227,43 +3226,144 @@ static void uvc_ctrl_restore_error(struct uvc_device *dev,
name, err_ctrl->info.selector, entity->id, ret);
}
+/*
+ * Return true if the control is a master control, i.e. one of its mappings
+ * has slave controls.
+ */
+static bool uvc_ctrl_is_master(const struct uvc_control *ctrl)
+{
+ const struct uvc_control_mapping *mapping;
+ unsigned int i;
+
+ list_for_each_entry(mapping, &ctrl->info.mappings, list) {
+ for (i = 0; i < ARRAY_SIZE(mapping->slave_ids); ++i) {
+ if (mapping->slave_ids[i])
+ return true;
+ }
+ }
+
+ return false;
+}
+
+/*
+ * Return true if the control is currently inactive, i.e. it is a slave
+ * control whose master is not in manual mode.
+ *
+ * The master value is read from the driver's cache when available, or queried
+ * from the device via GET_CUR if the master was neither restored in the first
+ * pass nor loaded yet. If the master control cannot be found in the same
+ * entity, is write-only, or fails GET_CUR, the control is considered active,
+ * matching uvc_ctrl_is_accessible().
+ */
+static bool uvc_ctrl_is_inactive(struct uvc_device *dev,
+ struct uvc_entity *entity,
+ struct uvc_control *ctrl)
+{
+ struct uvc_control_mapping *mapping;
+
+ list_for_each_entry(mapping, &ctrl->info.mappings, list) {
+ struct uvc_control_mapping *master_map = NULL;
+ struct uvc_control *master_ctrl = NULL;
+ s32 val;
+
+ if (!mapping->master_id)
+ continue;
+
+ __uvc_find_control(entity, mapping->master_id, &master_map,
+ &master_ctrl, 0, 0);
+
+ if (!master_ctrl || uvc_ctrl_mapping_is_compound(master_map))
+ continue;
+
+ if (!(master_ctrl->modified &&
+ (master_ctrl->info.flags & UVC_CTRL_FLAG_RESTORE)) &&
+ !master_ctrl->loaded) {
+ if (!(master_ctrl->info.flags &
+ UVC_CTRL_FLAG_GET_CUR) ||
+ __uvc_ctrl_load_cur(dev, master_ctrl) < 0)
+ continue;
+ }
+
+ val = uvc_mapping_get_s32(master_map, UVC_GET_CUR,
+ uvc_ctrl_data(master_ctrl,
+ UVC_CTRL_DATA_CURRENT));
+ if (val != mapping->master_manual)
+ return true;
+ }
+
+ return false;
+}
+
/*
* Restore control values after resume, skipping controls that haven't been
* changed.
*
+ * The controls of each entity are restored in two passes: first the master
+ * controls (the auto modes), then the remaining controls. This way a master
+ * is set to its final state before its slaves are restored.
+ *
+ * Slave controls that are currently inactive, because their master is not
+ * in manual mode, are not restored: devices reject SET_CUR requests for
+ * them and some firmwares misbehave when written in auto mode. The manual
+ * value only takes effect once the master is switched back to manual mode.
+ *
* TODO
* - Don't restore modified controls that are back to their default value.
- * - Handle restore order (Auto-Exposure Mode should be restored before
- * Exposure Time).
*/
int uvc_ctrl_restore_values(struct uvc_device *dev)
{
struct uvc_control *err_ctrl;
struct uvc_control *ctrl;
struct uvc_entity *entity;
- unsigned int i;
+ unsigned int i, pass;
int ret;
/* Walk the entities list and restore controls when possible. */
list_for_each_entry(entity, &dev->entities, list) {
+ for (pass = 0; pass < 2; ++pass) {
+ bool master_pass = pass == 0;
+ bool dirty = false;
+
+ for (i = 0; i < entity->ncontrols; ++i) {
+ ctrl = &entity->controls[i];
+
+ if (!ctrl->initialized || !ctrl->modified ||
+ !(ctrl->info.flags & UVC_CTRL_FLAG_RESTORE))
+ continue;
+
+ if (uvc_ctrl_is_master(ctrl) != master_pass)
+ continue;
+
+ if (!master_pass &&
+ uvc_ctrl_is_inactive(dev, entity, ctrl)) {
+ uvc_dbg(dev, CONTROL,
+ "skipping restore of inactive control %pUl/%u/%u\n",
+ ctrl->info.entity,
+ ctrl->info.index,
+ ctrl->info.selector);
+ ctrl->loaded = 0;
+ continue;
+ }
+
+ uvc_dbg(dev, CONTROL,
+ "restoring control %pUl/%u/%u\n",
+ ctrl->info.entity, ctrl->info.index,
+ ctrl->info.selector);
+ ctrl->dirty = 1;
+ dirty = true;
+ }
- for (i = 0; i < entity->ncontrols; ++i) {
- ctrl = &entity->controls[i];
-
- if (!ctrl->initialized || !ctrl->modified ||
- (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
+ if (master_pass && !dirty)
continue;
- uvc_dbg(dev, CONTROL, "restoring control %pUl/%u/%u\n",
- ctrl->info.entity, ctrl->info.index,
- ctrl->info.selector);
- ctrl->dirty = 1;
- }
- err_ctrl = NULL;
- ret = uvc_ctrl_commit_entity(dev, NULL, entity, 0, &err_ctrl);
- if (ret < 0) {
- uvc_ctrl_restore_error(dev, entity, err_ctrl, ret);
- return ret;
+ err_ctrl = NULL;
+ ret = uvc_ctrl_commit_entity(dev, NULL, entity, 0,
+ &err_ctrl);
+ if (ret < 0) {
+ uvc_ctrl_restore_error(dev, entity, err_ctrl,
+ ret);
+ return ret;
+ }
}
}
--
2.53.0
next prev parent reply other threads:[~2026-11-24 10:06 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 10:06 [PATCH 0/3] media: uvcvideo: fix control restore on reset_resume Chris Bainbridge
2026-11-24 10:06 ` [PATCH 1/3] media: uvcvideo: Report the failing control when restoring controls fails Chris Bainbridge
2026-11-24 10:06 ` Chris Bainbridge [this message]
2026-11-24 10:06 ` [PATCH 3/3] media: uvcvideo: Do not abort resume " Chris Bainbridge
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124100623.2227323-3-chris.bainbridge@canonical.com \
--to=chris.bainbridge@canonical.com \
--cc=hansg@kernel.org \
--cc=laurent.pinchart@ideasonboard.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=mchehab@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®