Skip to contentSkip to conversation

media: uvc: limit max bandwidth for HDMI capture

PATCH In review Hans de Goede 2 participants 7 messages Jan 6, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover media: uvc: limit max bandwidth for HDMI capture

Hi All,

This is mostly a resend of Mauro's patch from a long time ago to fix the MacroSilicon HDMI capture UVC dongle:
https://patchwork.linuxtv.org/project/linux-media/patch/bca0634691cea503c2c5df62b98ba66f0c85bf85.1624350539.git.mchehab+huawei@kernel.org/

While reviewing this I noticed that the driver hardcodes 10000000 for the UVC frame interval denominator everywhere. So this v2 adds a preparation patch adding a define for that constant.

The only other change from Mauro's v1 is adding a comment that the new uvc_device_info.max_bandwidth field is in pixels per second.

Regards,

Hans

Hans de Goede (1):
media: uvcvideo: Add a UVC_FIVAL_DENOM define

Mauro Carvalho Chehab (1):
media: uvc: limit max bandwidth for HDMI capture

 drivers/media/usb/uvc/uvc_driver.c | 18 ++++++++++++++--
 drivers/media/usb/uvc/uvc_v4l2.c   | 16 +++++++--------
 drivers/media/usb/uvc/uvc_video.c  | 33 +++++++++++++++++++++++++-----
 drivers/media/usb/uvc/uvcvideo.h   |  3 +++
 4 files changed, 55 insertions(+), 15 deletions(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH v2 1/2 media: uvcvideo: Add a UVC_FIVAL_DENOM define

Add a define for the UVC frame interval denominator of 10000000 instead of hardcoding 10000000 everywhere.

---
 drivers/media/usb/uvc/uvc_driver.c |  4 ++--
 drivers/media/usb/uvc/uvc_v4l2.c   | 16 ++++++++--------
 drivers/media/usb/uvc/uvc_video.c  |  4 ++--
 drivers/media/usb/uvc/uvcvideo.h   |  2 ++
 4 files changed, 14 insertions(+), 12 deletions(-)
drivers/media/usb/uvc/uvc_driver.c +2 -2
index c3ccd9be0a47..f754640fddf5 100644
@@ -320,8 +320,8 @@ static int uvc_parse_frame(struct uvc_device *dev,
 
 	uvc_dbg(dev, DESCR, "- %ux%u (%u.%u fps)\n",
 		frame->wWidth, frame->wHeight,
-		10000000 / frame->dwDefaultFrameInterval,
-		(100000000 / frame->dwDefaultFrameInterval) % 10);
+		UVC_FIVAL_DENOM / frame->dwDefaultFrameInterval,
+		((UVC_FIVAL_DENOM * 10) / frame->dwDefaultFrameInterval) % 10);
 
 	*intervals += n;
 
drivers/media/usb/uvc/uvc_v4l2.c +8 -8
index 93c6cdb23881..301999e94759 100644
@@ -282,8 +282,8 @@ static int uvc_v4l2_try_format(struct uvc_streaming *stream,
 	interval = frame->dwDefaultFrameInterval;
 	uvc_dbg(stream->dev, FORMAT,
 		"Using default frame interval %u.%u us (%u.%u fps)\n",
-		interval / 10, interval % 10, 10000000 / interval,
-		(100000000 / interval) % 10);
+		interval / 10, interval % 10, UVC_FIVAL_DENOM / interval,
+		((UVC_FIVAL_DENOM * 10) / interval) % 10);
 
 	/* Set the format index, frame index and frame interval. */
 	memset(probe, 0, sizeof(*probe));
@@ -450,7 +450,7 @@ static int uvc_ioctl_g_parm(struct file *file, void *fh,
 	numerator = stream->ctrl.dwFrameInterval;
 	mutex_unlock(&stream->mutex);
 
-	denominator = 10000000;
+	denominator = UVC_FIVAL_DENOM;
 	v4l2_simplify_fraction(&numerator, &denominator, 8, 333);
 
 	memset(parm, 0, sizeof(*parm));
@@ -551,7 +551,7 @@ static int uvc_ioctl_s_parm(struct file *file, void *fh,
 
 	/* Return the actual frame period. */
 	timeperframe.numerator = probe.dwFrameInterval;
-	timeperframe.denominator = 10000000;
+	timeperframe.denominator = UVC_FIVAL_DENOM;
 	v4l2_simplify_fraction(&timeperframe.numerator,
 		&timeperframe.denominator, 8, 333);
 
@@ -1240,17 +1240,17 @@ static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh,
 		fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
 		fival->discrete.numerator =
 			frame->dwFrameInterval[index];
-		fival->discrete.denominator = 10000000;
+		fival->discrete.denominator = UVC_FIVAL_DENOM;
 		v4l2_simplify_fraction(&fival->discrete.numerator,
 			&fival->discrete.denominator, 8, 333);
 	} else {
 		fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
 		fival->stepwise.min.numerator = frame->dwFrameInterval[0];
-		fival->stepwise.min.denominator = 10000000;
+		fival->stepwise.min.denominator = UVC_FIVAL_DENOM;
 		fival->stepwise.max.numerator = frame->dwFrameInterval[1];
-		fival->stepwise.max.denominator = 10000000;
+		fival->stepwise.max.denominator = UVC_FIVAL_DENOM;
 		fival->stepwise.step.numerator = frame->dwFrameInterval[2];
-		fival->stepwise.step.denominator = 10000000;
+		fival->stepwise.step.denominator = UVC_FIVAL_DENOM;
 		v4l2_simplify_fraction(&fival->stepwise.min.numerator,
 			&fival->stepwise.min.denominator, 8, 333);
 		v4l2_simplify_fraction(&fival->stepwise.max.numerator,
drivers/media/usb/uvc/uvc_video.c +2 -2
index 7efed64b81a2..41cb1b45fa23 100644
@@ -231,7 +231,7 @@ static void uvc_fixup_video_ctrl(struct uvc_streaming *stream,
 		u32 interval;
 		u32 bandwidth;
 
-		interval = (ctrl->dwFrameInterval > 100000)
+		interval = (ctrl->dwFrameInterval > (UVC_FIVAL_DENOM / 100))
 			 ? ctrl->dwFrameInterval
 			 : frame->dwFrameInterval[0];
 
@@ -243,7 +243,7 @@ static void uvc_fixup_video_ctrl(struct uvc_streaming *stream,
 		 * header size (assumed to be 12 bytes long).
 		 */
 		bandwidth = frame->wWidth * frame->wHeight / 8 * format->bpp;
-		bandwidth *= 10000000 / interval + 1;
+		bandwidth *= UVC_FIVAL_DENOM / interval + 1;
 		bandwidth /= 1000;
 		if (stream->dev->udev->speed >= USB_SPEED_HIGH)
 			bandwidth /= 8;
drivers/media/usb/uvc/uvcvideo.h +2 -0
index 3d196a696f46..966ff82c2ba8 100644
@@ -41,6 +41,8 @@
 #define UVC_EXT_GPIO_UNIT		0x7ffe
 #define UVC_EXT_GPIO_UNIT_ID		0x100
 
+#define UVC_FIVAL_DENOM			10000000
+
 /* ------------------------------------------------------------------------
  * Driver specific constants.
  */
Signature · 1 line
2.47.1
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] media: uvcvideo: Add a UVC_FIVAL_DENOM define

Quoted 5 lines · Hans de Goede

Add a define for the UVC frame interval denominator of 10000000 instead of hardcoding 10000000 everywhere.

Quoted 12 lines
---
 drivers/media/usb/uvc/uvc_driver.c |  4 ++--
 drivers/media/usb/uvc/uvc_v4l2.c   | 16 ++++++++--------
 drivers/media/usb/uvc/uvc_video.c  |  4 ++--
 drivers/media/usb/uvc/uvcvideo.h   |  2 ++
 4 files changed, 14 insertions(+), 12 deletions(-)
drivers/media/usb/uvc/uvc_driver.c +0 -0
index c3ccd9be0a47..f754640fddf5 100644

drivers/media/usb/uvc/uvc_driver.c static int uvc_parse_frame(struct uvc_device *dev, · line 324

Quoted 7 lines
drivers/media/usb/uvc/uvc_driver.c +2 -2
@@ -320,8 +320,8 @@ static int uvc_parse_frame(struct uvc_device *dev,
 
        uvc_dbg(dev, DESCR, "- %ux%u (%u.%u fps)\n",
                frame->wWidth, frame->wHeight,
-               10000000 / frame->dwDefaultFrameInterval,
-               (100000000 / frame->dwDefaultFrameInterval) % 10);
+               UVC_FIVAL_DENOM / frame->dwDefaultFrameInterval,
+               ((UVC_FIVAL_DENOM * 10) / frame->dwDefaultFrameInterval) % 10);

I wish there was a nicer way for this expression than ((a*10)%b) % 10
I would have simply written.
"%u 1/10 fps.

Quoted 97 lines
drivers/media/usb/uvc/uvc_driver.c +0 -0
        *intervals += n;
 
drivers/media/usb/uvc/uvc_v4l2.c +8 -8
index 93c6cdb23881..301999e94759 100644
@@ -282,8 +282,8 @@ static int uvc_v4l2_try_format(struct uvc_streaming *stream,
        interval = frame->dwDefaultFrameInterval;
        uvc_dbg(stream->dev, FORMAT,
                "Using default frame interval %u.%u us (%u.%u fps)\n",
-               interval / 10, interval % 10, 10000000 / interval,
-               (100000000 / interval) % 10);
+               interval / 10, interval % 10, UVC_FIVAL_DENOM / interval,
+               ((UVC_FIVAL_DENOM * 10) / interval) % 10);
 
        /* Set the format index, frame index and frame interval. */
        memset(probe, 0, sizeof(*probe));
@@ -450,7 +450,7 @@ static int uvc_ioctl_g_parm(struct file *file, void *fh,
        numerator = stream->ctrl.dwFrameInterval;
        mutex_unlock(&stream->mutex);
 
-       denominator = 10000000;
+       denominator = UVC_FIVAL_DENOM;
        v4l2_simplify_fraction(&numerator, &denominator, 8, 333);
 
        memset(parm, 0, sizeof(*parm));
@@ -551,7 +551,7 @@ static int uvc_ioctl_s_parm(struct file *file, void *fh,
 
        /* Return the actual frame period. */
        timeperframe.numerator = probe.dwFrameInterval;
-       timeperframe.denominator = 10000000;
+       timeperframe.denominator = UVC_FIVAL_DENOM;
        v4l2_simplify_fraction(&timeperframe.numerator,
                &timeperframe.denominator, 8, 333);
 
@@ -1240,17 +1240,17 @@ static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh,
                fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
                fival->discrete.numerator =
                        frame->dwFrameInterval[index];
-               fival->discrete.denominator = 10000000;
+               fival->discrete.denominator = UVC_FIVAL_DENOM;
                v4l2_simplify_fraction(&fival->discrete.numerator,
                        &fival->discrete.denominator, 8, 333);
        } else {
                fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
                fival->stepwise.min.numerator = frame->dwFrameInterval[0];
-               fival->stepwise.min.denominator = 10000000;
+               fival->stepwise.min.denominator = UVC_FIVAL_DENOM;
                fival->stepwise.max.numerator = frame->dwFrameInterval[1];
-               fival->stepwise.max.denominator = 10000000;
+               fival->stepwise.max.denominator = UVC_FIVAL_DENOM;
                fival->stepwise.step.numerator = frame->dwFrameInterval[2];
-               fival->stepwise.step.denominator = 10000000;
+               fival->stepwise.step.denominator = UVC_FIVAL_DENOM;
                v4l2_simplify_fraction(&fival->stepwise.min.numerator,
                        &fival->stepwise.min.denominator, 8, 333);
                v4l2_simplify_fraction(&fival->stepwise.max.numerator,
drivers/media/usb/uvc/uvc_video.c +2 -2
index 7efed64b81a2..41cb1b45fa23 100644
@@ -231,7 +231,7 @@ static void uvc_fixup_video_ctrl(struct uvc_streaming *stream,
                u32 interval;
                u32 bandwidth;
 
-               interval = (ctrl->dwFrameInterval > 100000)
+               interval = (ctrl->dwFrameInterval > (UVC_FIVAL_DENOM / 100))
                         ? ctrl->dwFrameInterval
                         : frame->dwFrameInterval[0];
 
@@ -243,7 +243,7 @@ static void uvc_fixup_video_ctrl(struct uvc_streaming *stream,
                 * header size (assumed to be 12 bytes long).
                 */
                bandwidth = frame->wWidth * frame->wHeight / 8 * format->bpp;
-               bandwidth *= 10000000 / interval + 1;
+               bandwidth *= UVC_FIVAL_DENOM / interval + 1;
                bandwidth /= 1000;
                if (stream->dev->udev->speed >= USB_SPEED_HIGH)
                        bandwidth /= 8;
drivers/media/usb/uvc/uvcvideo.h +2 -1
index 3d196a696f46..966ff82c2ba8 100644
@@ -41,6 +41,8 @@
 #define UVC_EXT_GPIO_UNIT              0x7ffe
 #define UVC_EXT_GPIO_UNIT_ID           0x100
 
+#define UVC_FIVAL_DENOM                        10000000
+
 /* ------------------------------------------------------------------------
  * Driver specific constants.
  */
--

2.47.1

Signature · 1 line
Ricardo Ribalda
#3 lore raw

PATCH v2 2/2 media: uvc: limit max bandwidth for HDMI capture

In reply to Hans de Goede

From: Mauro Carvalho Chehab <[email protected]>

This device:

        534d:2109 MacroSilicon

Announces that it supports several frame intervals for their resolutions for MJPEG compression:

VideoStreaming Interface Descriptor:
bLength 46
bDescriptorType 36
bDescriptorSubtype 7 (FRAME_MJPEG)
bFrameIndex 1
bmCapabilities 0x00
Still image unsupported
wWidth 1920
wHeight 1080
dwMinBitRate 768000
dwMaxBitRate 196608000
dwMaxVideoFrameBufferSize 4147200
dwDefaultFrameInterval 166666
bFrameIntervalType 5
dwFrameInterval( 0) 166666
dwFrameInterval( 1) 333333
dwFrameInterval( 2) 400000
dwFrameInterval( 3) 500000
dwFrameInterval( 4) 1000000

However, the highest frame interval (166666), which means 60 fps is not supported. If set to it, URB packages will be dropped, causing JPEG decoding errors and part of the video frames will be missed.

Also, as specified at the device's documentation, for such resolution, the maximum interval is 30 fps (interval == 333333).

The last format that supports such frame interval is 1280x720.

Add a quirk to estimate a raw bandwidth, by doing:
width * height * framerate
E. g.:

        1920 * 1080 * 30 = 62208000

if the bandwidth is greater than such threshold, get the next value from the dwFrameInterval.

---
 drivers/media/usb/uvc/uvc_driver.c | 14 ++++++++++++++
 drivers/media/usb/uvc/uvc_video.c  | 29 ++++++++++++++++++++++++++---
 drivers/media/usb/uvc/uvcvideo.h   |  1 +
 3 files changed, 41 insertions(+), 3 deletions(-)
drivers/media/usb/uvc/uvc_driver.c +14 -0
index f754640fddf5..6d001d4f0902 100644
@@ -2491,6 +2491,11 @@ static const struct uvc_device_info uvc_quirk_fix_bandwidth = {
 	.quirks = UVC_QUIRK_FIX_BANDWIDTH,
 };
 
+static const struct uvc_device_info uvc_quirk_fix_bw_622 = {
+	.quirks = UVC_QUIRK_FIX_BANDWIDTH,
+	.max_bandwidth = 62208000,
+};
+
 static const struct uvc_device_info uvc_quirk_probe_def = {
 	.quirks = UVC_QUIRK_PROBE_DEF,
 };
@@ -2794,6 +2799,15 @@ static const struct usb_device_id uvc_ids[] = {
 	  .bInterfaceSubClass	= 1,
 	  .bInterfaceProtocol	= 0,
 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
+	/* MacroSilicon HDMI capture */
+	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
+				| USB_DEVICE_ID_MATCH_INT_INFO,
+	  .idVendor		= 0x534d,
+	  .idProduct		= 0x2109,
+	  .bInterfaceClass	= USB_CLASS_VIDEO,
+	  .bInterfaceSubClass	= 1,
+	  .bInterfaceProtocol	= 0,
+	  .driver_info		= (kernel_ulong_t)&uvc_quirk_fix_bw_622 },
 	/* Genesys Logic USB 2.0 PC Camera */
 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
 				| USB_DEVICE_ID_MATCH_INT_INFO,
drivers/media/usb/uvc/uvc_video.c +26 -3
index 41cb1b45fa23..af5233ab667e 100644
@@ -225,9 +225,32 @@ static void uvc_fixup_video_ctrl(struct uvc_streaming *stream,
 	if ((ctrl->dwMaxPayloadTransferSize & 0xffff0000) == 0xffff0000)
 		ctrl->dwMaxPayloadTransferSize &= ~0xffff0000;
 
-	if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) &&
-	    stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH &&
-	    stream->intf->num_altsetting > 1) {
+	if (!(stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH))
+		return;
+
+	/* Handle UVC_QUIRK_FIX_BANDWIDTH */
+
+	if (format->flags & UVC_FMT_FLAG_COMPRESSED) {
+		u32 bandwidth;
+
+		if (!stream->dev->info->max_bandwidth || !frame->bFrameIntervalType)
+			return;
+
+		for (i = 0; i < frame->bFrameIntervalType; ++i) {
+			bandwidth = frame->wWidth * frame->wHeight;
+			bandwidth *= UVC_FIVAL_DENOM / frame->dwFrameInterval[i];
+
+			if (bandwidth <= stream->dev->info->max_bandwidth)
+				break;
+		}
+
+		if (ctrl->dwFrameInterval < frame->dwFrameInterval[i])
+			ctrl->dwFrameInterval = frame->dwFrameInterval[i];
+
+		return;
+	}
+
+	if (stream->intf->num_altsetting > 1) {
 		u32 interval;
 		u32 bandwidth;
 
drivers/media/usb/uvc/uvcvideo.h +1 -0
index 966ff82c2ba8..6b702219173f 100644
@@ -535,6 +535,7 @@ static inline u32 uvc_urb_index(const struct uvc_urb *uvc_urb)
 
 struct uvc_device_info {
 	u32	quirks;
+	u32	max_bandwidth; /* In pixels per second */
 	u32	meta_format;
 	u16	uvc_version;
 };
Signature · 1 line
2.47.1
#4 lore raw
Quoted 95 lines · Hans de Goede

From: Mauro Carvalho Chehab <[email protected]>

This device:

        534d:2109 MacroSilicon

Announces that it supports several frame intervals for their resolutions for MJPEG compression:

VideoStreaming Interface Descriptor:
bLength 46
bDescriptorType 36
bDescriptorSubtype 7 (FRAME_MJPEG)
bFrameIndex 1
bmCapabilities 0x00
Still image unsupported
wWidth 1920
wHeight 1080
dwMinBitRate 768000
dwMaxBitRate 196608000
dwMaxVideoFrameBufferSize 4147200
dwDefaultFrameInterval 166666
bFrameIntervalType 5
dwFrameInterval( 0) 166666
dwFrameInterval( 1) 333333
dwFrameInterval( 2) 400000
dwFrameInterval( 3) 500000
dwFrameInterval( 4) 1000000

However, the highest frame interval (166666), which means 60 fps is not supported. If set to it, URB packages will be dropped, causing JPEG decoding errors and part of the video frames will be missed.

Also, as specified at the device's documentation, for such resolution, the maximum interval is 30 fps (interval == 333333).

The last format that supports such frame interval is 1280x720.

Add a quirk to estimate a raw bandwidth, by doing:
width * height * framerate
E. g.:

        1920 * 1080 * 30 = 62208000

if the bandwidth is greater than such threshold, get the next value from the dwFrameInterval.

---
 drivers/media/usb/uvc/uvc_driver.c | 14 ++++++++++++++
 drivers/media/usb/uvc/uvc_video.c  | 29 ++++++++++++++++++++++++++---
 drivers/media/usb/uvc/uvcvideo.h   |  1 +
 3 files changed, 41 insertions(+), 3 deletions(-)
drivers/media/usb/uvc/uvc_driver.c +14 -0
index f754640fddf5..6d001d4f0902 100644
@@ -2491,6 +2491,11 @@ static const struct uvc_device_info uvc_quirk_fix_bandwidth = {
        .quirks = UVC_QUIRK_FIX_BANDWIDTH,
 };
 
+static const struct uvc_device_info uvc_quirk_fix_bw_622 = {
+       .quirks = UVC_QUIRK_FIX_BANDWIDTH,
+       .max_bandwidth = 62208000,
+};
+
 static const struct uvc_device_info uvc_quirk_probe_def = {
        .quirks = UVC_QUIRK_PROBE_DEF,
 };
@@ -2794,6 +2799,15 @@ static const struct usb_device_id uvc_ids[] = {
          .bInterfaceSubClass   = 1,
          .bInterfaceProtocol   = 0,
          .driver_info          = (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
+       /* MacroSilicon HDMI capture */
+       { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
+                               | USB_DEVICE_ID_MATCH_INT_INFO,
+         .idVendor             = 0x534d,
+         .idProduct            = 0x2109,
+         .bInterfaceClass      = USB_CLASS_VIDEO,
+         .bInterfaceSubClass   = 1,
+         .bInterfaceProtocol   = 0,
+         .driver_info          = (kernel_ulong_t)&uvc_quirk_fix_bw_622 },
        /* Genesys Logic USB 2.0 PC Camera */
        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
                                | USB_DEVICE_ID_MATCH_INT_INFO,
drivers/media/usb/uvc/uvc_video.c +0 -0
index 41cb1b45fa23..af5233ab667e 100644
@@ -225,9 +225,32 @@ static void uvc_fixup_video_ctrl(struct uvc_streaming *stream,
        if ((ctrl->dwMaxPayloadTransferSize & 0xffff0000) == 0xffff0000)

drivers/media/usb/uvc/uvc_video.c static void uvc_fixup_video_ctrl(struct uvc_streaming *stream, · line 232

Quoted 10 lines
drivers/media/usb/uvc/uvc_video.c +5 -3
                ctrl->dwMaxPayloadTransferSize &= ~0xffff0000;
 
-       if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) &&
-           stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH &&
-           stream->intf->num_altsetting > 1) {
+       if (!(stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH))
+               return;
+
+       /* Handle UVC_QUIRK_FIX_BANDWIDTH */
+

Shouldn't this quirk be better implemented in uvc_try_frame_interval() ?

drivers/media/usb/uvc/uvc_video.c static void uvc_fixup_video_ctrl(struct uvc_streaming *stream, · line 241

Quoted 9 lines
drivers/media/usb/uvc/uvc_video.c +9 -0
+       if (format->flags & UVC_FMT_FLAG_COMPRESSED) {
+               u32 bandwidth;
+
+               if (!stream->dev->info->max_bandwidth || !frame->bFrameIntervalType)
+                       return;
+
+               for (i = 0; i < frame->bFrameIntervalType; ++i) {
+                       bandwidth = frame->wWidth * frame->wHeight;
+                       bandwidth *= UVC_FIVAL_DENOM / frame->dwFrameInterval[i];

frame->dwFrameInterval[i] comes from the device, it is not sanitized. We need to make sure it is not zero.

Quoted 31 lines
drivers/media/usb/uvc/uvc_video.c +12 -0
+
+                       if (bandwidth <= stream->dev->info->max_bandwidth)
+                               break;
+               }
+
+               if (ctrl->dwFrameInterval < frame->dwFrameInterval[i])
+                       ctrl->dwFrameInterval = frame->dwFrameInterval[i];
+
+               return;
+       }
+
+       if (stream->intf->num_altsetting > 1) {
                u32 interval;
                u32 bandwidth;
 
drivers/media/usb/uvc/uvcvideo.h +1 -1
index 966ff82c2ba8..6b702219173f 100644
@@ -535,6 +535,7 @@ static inline u32 uvc_urb_index(const struct uvc_urb *uvc_urb)
 
 struct uvc_device_info {
        u32     quirks;
+       u32     max_bandwidth; /* In pixels per second */
        u32     meta_format;
        u16     uvc_version;
 };
--

2.47.1

Signature · 1 line
Ricardo Ribalda
#5 lore raw

Hi,

Quoted 98 lines · Ricardo Ribalda
Quoted 97 lines · Hans de Goede

From: Mauro Carvalho Chehab <[email protected]>

This device:

        534d:2109 MacroSilicon

Announces that it supports several frame intervals for their resolutions for MJPEG compression:

VideoStreaming Interface Descriptor:
bLength 46
bDescriptorType 36
bDescriptorSubtype 7 (FRAME_MJPEG)
bFrameIndex 1
bmCapabilities 0x00
Still image unsupported
wWidth 1920
wHeight 1080
dwMinBitRate 768000
dwMaxBitRate 196608000
dwMaxVideoFrameBufferSize 4147200
dwDefaultFrameInterval 166666
bFrameIntervalType 5
dwFrameInterval( 0) 166666
dwFrameInterval( 1) 333333
dwFrameInterval( 2) 400000
dwFrameInterval( 3) 500000
dwFrameInterval( 4) 1000000

However, the highest frame interval (166666), which means 60 fps is not supported. If set to it, URB packages will be dropped, causing JPEG decoding errors and part of the video frames will be missed.

Also, as specified at the device's documentation, for such resolution, the maximum interval is 30 fps (interval == 333333).

The last format that supports such frame interval is 1280x720.

Add a quirk to estimate a raw bandwidth, by doing:
width * height * framerate
E. g.:

        1920 * 1080 * 30 = 62208000

if the bandwidth is greater than such threshold, get the next value from the dwFrameInterval.

---
 drivers/media/usb/uvc/uvc_driver.c | 14 ++++++++++++++
 drivers/media/usb/uvc/uvc_video.c  | 29 ++++++++++++++++++++++++++---
 drivers/media/usb/uvc/uvcvideo.h   |  1 +
 3 files changed, 41 insertions(+), 3 deletions(-)
drivers/media/usb/uvc/uvc_driver.c +14 -0
index f754640fddf5..6d001d4f0902 100644
@@ -2491,6 +2491,11 @@ static const struct uvc_device_info uvc_quirk_fix_bandwidth = {
        .quirks = UVC_QUIRK_FIX_BANDWIDTH,
 };
 
+static const struct uvc_device_info uvc_quirk_fix_bw_622 = {
+       .quirks = UVC_QUIRK_FIX_BANDWIDTH,
+       .max_bandwidth = 62208000,
+};
+
 static const struct uvc_device_info uvc_quirk_probe_def = {
        .quirks = UVC_QUIRK_PROBE_DEF,
 };
@@ -2794,6 +2799,15 @@ static const struct usb_device_id uvc_ids[] = {
          .bInterfaceSubClass   = 1,
          .bInterfaceProtocol   = 0,
          .driver_info          = (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
+       /* MacroSilicon HDMI capture */
+       { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
+                               | USB_DEVICE_ID_MATCH_INT_INFO,
+         .idVendor             = 0x534d,
+         .idProduct            = 0x2109,
+         .bInterfaceClass      = USB_CLASS_VIDEO,
+         .bInterfaceSubClass   = 1,
+         .bInterfaceProtocol   = 0,
+         .driver_info          = (kernel_ulong_t)&uvc_quirk_fix_bw_622 },
        /* Genesys Logic USB 2.0 PC Camera */
        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
                                | USB_DEVICE_ID_MATCH_INT_INFO,
drivers/media/usb/uvc/uvc_video.c +0 -0
index 41cb1b45fa23..af5233ab667e 100644
@@ -225,9 +225,32 @@ static void uvc_fixup_video_ctrl(struct uvc_streaming *stream,
        if ((ctrl->dwMaxPayloadTransferSize & 0xffff0000) == 0xffff0000)
                ctrl->dwMaxPayloadTransferSize &= ~0xffff0000;
 
Quoted 10 lines

drivers/media/usb/uvc/uvc_video.c static void uvc_fixup_video_ctrl(struct uvc_streaming *stream, · line 232

Quoted 8 lines
drivers/media/usb/uvc/uvc_video.c +5 -3
-       if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) &&
-           stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH &&
-           stream->intf->num_altsetting > 1) {
+       if (!(stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH))
+               return;
+
+       /* Handle UVC_QUIRK_FIX_BANDWIDTH */
+

Shouldn't this quirk be better implemented in uvc_try_frame_interval() ?

This is the place where the other bandwidth checks / UVC_QUIRK_FIX_BANDWIDTH handling is done, so I guess it makes sense to keep this here.

Quoted 4 lines
Quoted 4 lines
drivers/media/usb/uvc/uvc_video.c +4 -0
+       if (format->flags & UVC_FMT_FLAG_COMPRESSED) {
+               u32 bandwidth;
+
+               if (!stream->dev->info->max_bandwidth || !frame->bFrameIntervalType)
Quoted 8 lines

drivers/media/usb/uvc/uvc_video.c static void uvc_fixup_video_ctrl(struct uvc_streaming *stream, · line 241

Quoted 5 lines
drivers/media/usb/uvc/uvc_video.c +5 -0
+                       return;
+
+               for (i = 0; i < frame->bFrameIntervalType; ++i) {
+                       bandwidth = frame->wWidth * frame->wHeight;
+                       bandwidth *= UVC_FIVAL_DENOM / frame->dwFrameInterval[i];

frame->dwFrameInterval[i] comes from the device, it is not sanitized. We need to make sure it is not zero.

That is a good point, in the non-compressed UVC_QUIRK_FIX_BANDWIDTH handling this is handled by doing:

			bandwidth *= UVC_FIVAL_DENOM / (frame->dwFrameInterval[i] + 1);

I guess we should do the same here then ?

Regards,

Hans

Quoted 34 lines
Quoted 32 lines
drivers/media/usb/uvc/uvc_video.c +12 -0
+
+                       if (bandwidth <= stream->dev->info->max_bandwidth)
+                               break;
+               }
+
+               if (ctrl->dwFrameInterval < frame->dwFrameInterval[i])
+                       ctrl->dwFrameInterval = frame->dwFrameInterval[i];
+
+               return;
+       }
+
+       if (stream->intf->num_altsetting > 1) {
                u32 interval;
                u32 bandwidth;
 
drivers/media/usb/uvc/uvcvideo.h +1 -1
index 966ff82c2ba8..6b702219173f 100644
@@ -535,6 +535,7 @@ static inline u32 uvc_urb_index(const struct uvc_urb *uvc_urb)
 
 struct uvc_device_info {
        u32     quirks;
+       u32     max_bandwidth; /* In pixels per second */
        u32     meta_format;
        u16     uvc_version;
 };
--

2.47.1

#6 lore raw

Hi Hans

Quoted 113 lines · Hans de Goede

Hi,

Quoted 98 lines · Ricardo Ribalda
Quoted 97 lines · Hans de Goede

From: Mauro Carvalho Chehab <[email protected]>

This device:

        534d:2109 MacroSilicon

Announces that it supports several frame intervals for their resolutions for MJPEG compression:

VideoStreaming Interface Descriptor:
bLength 46
bDescriptorType 36
bDescriptorSubtype 7 (FRAME_MJPEG)
bFrameIndex 1
bmCapabilities 0x00
Still image unsupported
wWidth 1920
wHeight 1080
dwMinBitRate 768000
dwMaxBitRate 196608000
dwMaxVideoFrameBufferSize 4147200
dwDefaultFrameInterval 166666
bFrameIntervalType 5
dwFrameInterval( 0) 166666
dwFrameInterval( 1) 333333
dwFrameInterval( 2) 400000
dwFrameInterval( 3) 500000
dwFrameInterval( 4) 1000000

However, the highest frame interval (166666), which means 60 fps is not supported. If set to it, URB packages will be dropped, causing JPEG decoding errors and part of the video frames will be missed.

Also, as specified at the device's documentation, for such resolution, the maximum interval is 30 fps (interval == 333333).

The last format that supports such frame interval is 1280x720.

Add a quirk to estimate a raw bandwidth, by doing:
width * height * framerate
E. g.:

        1920 * 1080 * 30 = 62208000

if the bandwidth is greater than such threshold, get the next value from the dwFrameInterval.

---
 drivers/media/usb/uvc/uvc_driver.c | 14 ++++++++++++++
 drivers/media/usb/uvc/uvc_video.c  | 29 ++++++++++++++++++++++++++---
 drivers/media/usb/uvc/uvcvideo.h   |  1 +
 3 files changed, 41 insertions(+), 3 deletions(-)
drivers/media/usb/uvc/uvc_driver.c +14 -0
index f754640fddf5..6d001d4f0902 100644
@@ -2491,6 +2491,11 @@ static const struct uvc_device_info uvc_quirk_fix_bandwidth = {
        .quirks = UVC_QUIRK_FIX_BANDWIDTH,
 };
 
+static const struct uvc_device_info uvc_quirk_fix_bw_622 = {
+       .quirks = UVC_QUIRK_FIX_BANDWIDTH,
+       .max_bandwidth = 62208000,
+};
+
 static const struct uvc_device_info uvc_quirk_probe_def = {
        .quirks = UVC_QUIRK_PROBE_DEF,
 };
@@ -2794,6 +2799,15 @@ static const struct usb_device_id uvc_ids[] = {
          .bInterfaceSubClass   = 1,
          .bInterfaceProtocol   = 0,
          .driver_info          = (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
+       /* MacroSilicon HDMI capture */
+       { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
+                               | USB_DEVICE_ID_MATCH_INT_INFO,
+         .idVendor             = 0x534d,
+         .idProduct            = 0x2109,
+         .bInterfaceClass      = USB_CLASS_VIDEO,
+         .bInterfaceSubClass   = 1,
+         .bInterfaceProtocol   = 0,
+         .driver_info          = (kernel_ulong_t)&uvc_quirk_fix_bw_622 },
        /* Genesys Logic USB 2.0 PC Camera */
        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
                                | USB_DEVICE_ID_MATCH_INT_INFO,
drivers/media/usb/uvc/uvc_video.c +0 -0
index 41cb1b45fa23..af5233ab667e 100644
@@ -225,9 +225,32 @@ static void uvc_fixup_video_ctrl(struct uvc_streaming *stream,
        if ((ctrl->dwMaxPayloadTransferSize & 0xffff0000) == 0xffff0000)
                ctrl->dwMaxPayloadTransferSize &= ~0xffff0000;
 
Quoted 10 lines

drivers/media/usb/uvc/uvc_video.c static void uvc_fixup_video_ctrl(struct uvc_streaming *stream, · line 232

Quoted 8 lines
drivers/media/usb/uvc/uvc_video.c +5 -3
-       if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) &&
-           stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH &&
-           stream->intf->num_altsetting > 1) {
+       if (!(stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH))
+               return;
+
+       /* Handle UVC_QUIRK_FIX_BANDWIDTH */
+

Shouldn't this quirk be better implemented in uvc_try_frame_interval() ?

Quoted 2 lines

This is the place where the other bandwidth checks / UVC_QUIRK_FIX_BANDWIDTH handling is done, so I guess it makes sense to keep this here.

If I understood it correctly the quirk skips one of the intervals. If we add the quirk to uvc_ioctl_enum_frameintervals and uvc_try_frame_interval(), the user will not enumerate frame intervals that will not work.
IMO that is better than accepting an interval and then changing it to something else.

From Mauro's description, the documentation says that the max fps is 30 but the device announces 60. I'd rather add a "denylist" logic than introducing a magic number 62208000.

And regarding the magic number, it should be based on dwMaxVideoFrameBufferSize, not on frame->wWidth , frame->wHeight (if we decide to have a magic number).
or even better, we could have min_dwFrameInterval.

Quoted 14 lines
Quoted 13 lines

drivers/media/usb/uvc/uvc_video.c static void uvc_fixup_video_ctrl(struct uvc_streaming *stream, · line 241

Quoted 10 lines
drivers/media/usb/uvc/uvc_video.c +9 -0
+       if (format->flags & UVC_FMT_FLAG_COMPRESSED) {
+               u32 bandwidth;
+
+               if (!stream->dev->info->max_bandwidth || !frame->bFrameIntervalType)
+                       return;
+
+               for (i = 0; i < frame->bFrameIntervalType; ++i) {
+                       bandwidth = frame->wWidth * frame->wHeight;
+                       bandwidth *= UVC_FIVAL_DENOM / frame->dwFrameInterval[i];

frame->dwFrameInterval[i] comes from the device, it is not sanitized. We need to make sure it is not zero.

Quoted 6 lines

That is a good point, in the non-compressed UVC_QUIRK_FIX_BANDWIDTH handling this is handled by doing:

                        bandwidth *= UVC_FIVAL_DENOM / (frame->dwFrameInterval[i] + 1);

I guess we should do the same here then ?

I prefer (max(1,frame->dwFrameInterval[i])

But you chose :)

Quoted 42 lines

Regards,

Hans

Quoted 34 lines
Quoted 32 lines
drivers/media/usb/uvc/uvc_video.c +12 -0
+
+                       if (bandwidth <= stream->dev->info->max_bandwidth)
+                               break;
+               }
+
+               if (ctrl->dwFrameInterval < frame->dwFrameInterval[i])
+                       ctrl->dwFrameInterval = frame->dwFrameInterval[i];
+
+               return;
+       }
+
+       if (stream->intf->num_altsetting > 1) {
                u32 interval;
                u32 bandwidth;
 
drivers/media/usb/uvc/uvcvideo.h +1 -1
index 966ff82c2ba8..6b702219173f 100644
@@ -535,6 +535,7 @@ static inline u32 uvc_urb_index(const struct uvc_urb *uvc_urb)
 
 struct uvc_device_info {
        u32     quirks;
+       u32     max_bandwidth; /* In pixels per second */
        u32     meta_format;
        u16     uvc_version;
 };
--

2.47.1

Signature · 1 line
Ricardo Ribalda