Skip to contentSkip to conversation

media: uvcvideo: Automatically handle cameras with invalid uvc_version

PATCH Superseded Ricardo Ribalda 1 participant 1 message Sep 7

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: uvcvideo: Automatically handle cameras with invalid uvc_version

Currently, the driver expects that cameras properly implement the spec version that they announce, and if they fail to do so, we do not continue probing the driver.

To make drivers more fun, some vendors decided to announce that they are a UVC version that they are not. Until now, we handled those cameras via quirks.

Unfortunately, reality has shown us that there are more cameras out there with an invalid uvc_version than we initially predicted.

This patch tries to handle these cameras with an identity crisis automatically. We still shame them in dmesg. But now they will work.

---
Hi Edwin, could you please try if this patch alone (without the previous one) works for you?

Could you share the dmesg output with it?

Thanks!

---
 drivers/media/usb/uvc/uvc_driver.c | 11 +++++++----
 drivers/media/usb/uvc/uvc_video.c  | 23 ++++++++++++++++++-----
 2 files changed, 25 insertions(+), 9 deletions(-)
drivers/media/usb/uvc/uvc_driver.c +7 -4
index e289cc71ba98..ca75f8d1ec46 100644
@@ -1169,9 +1169,7 @@ static int uvc_parse_standard_control(struct uvc_device *dev,
 
 	case UVC_VC_PROCESSING_UNIT:
 		n = buflen >= 8 ? buffer[7] : 0;
-		p = dev->uvc_version >= 0x0110 ? 10 : 9;
-
-		if (buflen < p + n) {
+		if (buflen < 9 + n) {
 			uvc_dbg(dev, DESCR,
 				"device %d videocontrol interface %d PROCESSING_UNIT error\n",
 				udev->devnum, alts->desc.bInterfaceNumber);
@@ -1188,7 +1186,12 @@ static int uvc_parse_standard_control(struct uvc_device *dev,
 		unit->processing.bControlSize = buffer[7];
 		unit->processing.bmControls = (u8 *)unit + sizeof(*unit);
 		memcpy(unit->processing.bmControls, &buffer[8], n);
-		if (dev->uvc_version >= 0x0110)
+
+		/*
+		 * We are not using bmVideoStandards, so there is no need to
+		 * warn the user if it is missing.
+		 */
+		if (dev->uvc_version >= 0x0110 && buflen >= (n + 10))
 			unit->processing.bmVideoStandards = buffer[9+n];
 
 		uvc_entity_set_name(dev, unit, "Processing", buffer[8+n]);
drivers/media/usb/uvc/uvc_video.c +18 -5
index fc3536a4399f..58e1c1fa6af2 100644
@@ -273,6 +273,8 @@ static void uvc_fixup_video_ctrl(struct uvc_streaming *stream,
 	}
 }
 
+#define UVC_VIDEO_CTRL_MIN_SIZE 26
+
 static size_t uvc_video_ctrl_size(struct uvc_streaming *stream)
 {
 	/*
@@ -280,7 +282,7 @@ static size_t uvc_video_ctrl_size(struct uvc_streaming *stream)
 	 * on the protocol version.
 	 */
 	if (stream->dev->uvc_version < 0x0110)
-		return 26;
+		return UVC_VIDEO_CTRL_MIN_SIZE;
 	else if (stream->dev->uvc_version < 0x0150)
 		return 34;
 	else
@@ -319,7 +321,8 @@ static int uvc_get_video_ctrl(struct uvc_streaming *stream,
 		ctrl->wCompQuality = le16_to_cpup((__le16 *)data);
 		ret = 0;
 		goto out;
-	} else if (query == UVC_GET_DEF && probe == 1 && ret != size) {
+	} else if (query == UVC_GET_DEF && probe == 1 &&
+		   ret < UVC_VIDEO_CTRL_MIN_SIZE) {
 		/*
 		 * Many cameras don't support the GET_DEF request on their
 		 * video probe control. Warn once and return, the caller will
@@ -330,7 +333,7 @@ static int uvc_get_video_ctrl(struct uvc_streaming *stream,
 			"Enabling workaround.\n");
 		ret = -EIO;
 		goto out;
-	} else if (ret != size) {
+	} else if (ret < UVC_VIDEO_CTRL_MIN_SIZE) {
 		dev_err(&stream->intf->dev,
 			"Failed to query (%s) UVC %s control : %d (exp. %u).\n",
 			uvc_query_name(query), probe ? "probe" : "commit",
@@ -339,6 +342,12 @@ static int uvc_get_video_ctrl(struct uvc_streaming *stream,
 		goto out;
 	}
 
+	if (ret != size)
+		dev_warn_once(&stream->intf->dev,
+			      "UVC non compliance: Query (%s) UVC %s control had a size of %d instead of %u.\n",
+			       uvc_query_name(query),
+			       probe ? "probe" : "commit", ret, size);
+
 	ctrl->bmHint = le16_to_cpup((__le16 *)&data[0]);
 	ctrl->bFormatIndex = data[2];
 	ctrl->bFrameIndex = data[3];
@@ -351,7 +360,7 @@ static int uvc_get_video_ctrl(struct uvc_streaming *stream,
 	ctrl->dwMaxVideoFrameSize = get_unaligned_le32(&data[18]);
 	ctrl->dwMaxPayloadTransferSize = get_unaligned_le32(&data[22]);
 
-	if (size >= 34) {
+	if (ret >= 34) {
 		ctrl->dwClockFrequency = get_unaligned_le32(&data[26]);
 		ctrl->bmFramingInfo = data[30];
 		ctrl->bPreferedVersion = data[31];
@@ -412,11 +421,15 @@ static int uvc_set_video_ctrl(struct uvc_streaming *stream,
 	ret = __uvc_query_ctrl(stream->dev, UVC_SET_CUR, 0, stream->intfnum,
 		probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
 		size, uvc_timeout_param);
-	if (ret != size) {
+	if (ret < UVC_VIDEO_CTRL_MIN_SIZE) {
 		dev_err(&stream->intf->dev,
 			"Failed to set UVC %s control : %d (exp. %u).\n",
 			probe ? "probe" : "commit", ret, size);
 		ret = -EIO;
+	} else if (ret != size) {
+		dev_warn_once(&stream->intf->dev,
+			      "UVC non compliance: Set UVC %s control had a size of %d instead of %u.\n",
+			       probe ? "probe" : "commit", ret, size);
 	}
 
 	kfree(data);

---
base-commit: f9536a8065d9eed0d9114878c56ffc4e5a18bdfc
change-id: 20260907-uvc-version-aedefc4384ce

Best regards,

Signature · 1 line
Ricardo Ribalda <[email protected]>