mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 3/3] media: uvcvideo: Do not abort resume when restoring controls fails
Date: Tue, 24 Nov 2026 10:06:23 +0000	[thread overview]
Message-ID: <20261124100623.2227323-4-chris.bainbridge@canonical.com> (raw)
In-Reply-To: <20261124100623.2227323-1-chris.bainbridge@canonical.com>

uvc_ctrl_restore_values() delegates each pass to uvc_ctrl_commit_entity()
and returns at the first error. This has three problems on failure:

1. Within the failing entity, uvc_ctrl_commit_entity() aborts all
   subsequent dirty controls and overwrites UVC_CTRL_DATA_CURRENT with
   UVC_CTRL_DATA_BACKUP, which is never populated for a restore and
   holds stale data from before the control was last set.
2. If a master control fails to restore in the first pass, continuing to
   the second pass would evaluate uvc_ctrl_is_inactive() against a
   corrupted or unapplied master value and write manual slave controls
   while the hardware is still in auto mode.
3. Remaining entities are never restored, and __uvc_resume() returns
   early, skipping uvc_status_resume() for the status interrupt URB.

The USB core only logs the error returned by a reset_resume() handler:
usb_resume_interface() prints "reset_resume error %d" with dev_err() and
does not set intf->needs_binding on failure (that flag is only set for
drivers that do not implement reset_resume()), and usb_resume_both()
ignores the value returned by usb_resume_interface(). The interface is
therefore neither unbound nor rebound when reset_resume() fails, so it
is safe to report the error.

Restore each control directly with UVC_SET_CUR instead of calling
uvc_ctrl_commit_entity(), preserving UVC_CTRL_DATA_CURRENT and continuing
through all remaining controls and entities on error while recording the
first error encountered. Track whether a restored control succeeded via
ctrl->loaded so that uvc_ctrl_is_inactive() also skips slaves whose
master failed to restore in the first pass, and reset ctrl->loaded
afterwards for auto-update and write-only controls. Finally, always
resume the status endpoint in __uvc_resume(), prioritizing any status
endpoint resume error over already-logged best-effort restore errors.

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   | 80 +++++++++++++++++-------------
 drivers/media/usb/uvc/uvc_driver.c | 22 ++++++--
 2 files changed, 64 insertions(+), 38 deletions(-)

diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c
index d11ad3da1d50..b773efef4441 100644
--- a/drivers/media/usb/uvc/uvc_ctrl.c
+++ b/drivers/media/usb/uvc/uvc_ctrl.c
@@ -3199,31 +3199,44 @@ int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
  */
 
 /*
- * Report a failure to restore the controls of an entity. @err_ctrl points to
- * the control that failed, or is NULL if the failing control is not known.
+ * Report a failure to restore a control.
  */
 static void uvc_ctrl_restore_error(struct uvc_device *dev,
-				   const struct uvc_entity *entity,
-				   struct uvc_control *err_ctrl, int ret)
+				   struct uvc_control *ctrl, int ret)
 {
 	const struct uvc_control_mapping *mapping;
 	const char *name = "Unknown Control";
 
-	if (!err_ctrl) {
-		dev_warn_ratelimited(&dev->intf->dev,
-				     "Failed to restore controls on unit %u: %d\n",
-				     entity->id, ret);
-		return;
-	}
-
-	mapping = list_first_entry_or_null(&err_ctrl->info.mappings,
+	mapping = list_first_entry_or_null(&ctrl->info.mappings,
 					   struct uvc_control_mapping, list);
 	if (mapping)
 		name = uvc_map_get_name(mapping);
 
 	dev_warn_ratelimited(&dev->intf->dev,
 			     "Failed to restore control %s (selector %u on unit %u): %d\n",
-			     name, err_ctrl->info.selector, entity->id, ret);
+			     name, ctrl->info.selector, ctrl->entity->id, ret);
+}
+
+static int uvc_ctrl_restore_ctrl(struct uvc_device *dev,
+				 struct uvc_control *ctrl)
+{
+	int ret;
+
+	uvc_dbg(dev, CONTROL, "restoring control %pUl/%u/%u\n",
+		ctrl->info.entity, ctrl->info.index, ctrl->info.selector);
+
+	ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id, dev->intfnum,
+			     ctrl->info.selector,
+			     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
+			     ctrl->info.size);
+	if (ret < 0) {
+		ctrl->loaded = 0;
+		uvc_ctrl_restore_error(dev, ctrl, ret);
+		return ret;
+	}
+
+	ctrl->loaded = 1;
+	return 0;
 }
 
 /*
@@ -3247,7 +3260,8 @@ static bool uvc_ctrl_is_master(const struct uvc_control *ctrl)
 
 /*
  * Return true if the control is currently inactive, i.e. it is a slave
- * control whose master is not in manual mode.
+ * control whose master is not in manual mode, or whose master failed to
+ * restore in the first pass.
  *
  * 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
@@ -3275,9 +3289,11 @@ static bool uvc_ctrl_is_inactive(struct uvc_device *dev,
 		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->loaded) {
+			if (master_ctrl->modified &&
+			    (master_ctrl->info.flags & UVC_CTRL_FLAG_RESTORE))
+				return true;
+
 			if (!(master_ctrl->info.flags &
 			      UVC_CTRL_FLAG_GET_CUR) ||
 			    __uvc_ctrl_load_cur(dev, master_ctrl) < 0)
@@ -3312,17 +3328,16 @@ static bool uvc_ctrl_is_inactive(struct uvc_device *dev,
  */
 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, pass;
+	int first_err = 0;
 	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];
@@ -3345,29 +3360,24 @@ int uvc_ctrl_restore_values(struct uvc_device *dev)
 					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;
+				ret = uvc_ctrl_restore_ctrl(dev, ctrl);
+				if (ret < 0 && !first_err)
+					first_err = ret;
 			}
+		}
 
-			if (master_pass && !dirty)
+		for (i = 0; i < entity->ncontrols; ++i) {
+			ctrl = &entity->controls[i];
+			if (!ctrl->initialized)
 				continue;
 
-			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;
-			}
+			if ((ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE) ||
+			    !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
+				ctrl->loaded = 0;
 		}
 	}
 
-	return 0;
+	return first_err;
 }
 
 /* --------------------------------------------------------------------------
diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c
index cbc14569716b..ccb5c307c506 100644
--- a/drivers/media/usb/uvc/uvc_driver.c
+++ b/drivers/media/usb/uvc/uvc_driver.c
@@ -2414,13 +2414,29 @@ static int __uvc_resume(struct usb_interface *intf, int reset)
 
 	if (intf->cur_altsetting->desc.bInterfaceSubClass ==
 	    UVC_SC_VIDEOCONTROL) {
+		int status;
+
 		if (reset) {
+			/*
+			 * Restoring the controls is best effort: the USB core
+			 * only logs the error returned by a reset_resume()
+			 * handler (see usb_resume_interface() and
+			 * usb_resume_both()), it does not unbind the driver,
+			 * so keep resuming the rest of the interface.
+			 */
 			ret = uvc_ctrl_restore_values(dev);
-			if (ret < 0)
-				return ret;
 		}
 
-		return uvc_status_resume(dev);
+		/*
+		 * The status endpoint must be resumed even if restoring
+		 * controls failed. Prioritize status resume errors over
+		 * already-logged best-effort control restore errors.
+		 */
+		status = uvc_status_resume(dev);
+		if (status < 0)
+			return status;
+
+		return ret;
 	}
 
 	list_for_each_entry(stream, &dev->streams, list) {
-- 
2.53.0


      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 ` [PATCH 2/3] media: uvcvideo: Restore auto controls first and skip inactive slaves Chris Bainbridge
2026-11-24 10:06 ` Chris Bainbridge [this message]

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-4-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®