]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
media: uvcvideo: Implement dual stream quirk to fix loss of usb packets
authorIsaac Scott <isaac.scott@ideasonboard.com>
Thu, 28 Nov 2024 14:51:43 +0000 (14:51 +0000)
committerMauro Carvalho Chehab <mchehab+huawei@kernel.org>
Thu, 2 Jan 2025 08:58:30 +0000 (09:58 +0100)
Some cameras, such as the Sonix Technology Co. 292A, exhibit issues when
running two parallel streams, causing USB packets to be dropped when an
H.264 stream posts a keyframe while an MJPEG stream is running
simultaneously. This occasionally causes the driver to erroneously
output two consecutive JPEG images as a single frame.

To fix this, we inspect the buffer, and trigger a new frame when we
find an SOI.

Signed-off-by: Isaac Scott <isaac.scott@ideasonboard.com>
Reviewed-by: Ricardo Ribalda <ribalda@chromium.org>
Link: https://lore.kernel.org/r/20241128145144.61475-2-isaac.scott@ideasonboard.com
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
drivers/media/usb/uvc/uvc_video.c
drivers/media/usb/uvc/uvcvideo.h

index 7efed64b81a2862e6a290e7bf43f506aaad4fc24..e3567aeb0007c1f0a766f331e4e744359e95a863 100644 (file)
@@ -20,6 +20,7 @@
 #include <linux/atomic.h>
 #include <linux/unaligned.h>
 
+#include <media/jpeg.h>
 #include <media/v4l2-common.h>
 
 #include "uvcvideo.h"
@@ -1142,6 +1143,7 @@ static void uvc_video_stats_stop(struct uvc_streaming *stream)
 static int uvc_video_decode_start(struct uvc_streaming *stream,
                struct uvc_buffer *buf, const u8 *data, int len)
 {
+       u8 header_len;
        u8 fid;
 
        /*
@@ -1155,6 +1157,7 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
                return -EINVAL;
        }
 
+       header_len = data[0];
        fid = data[1] & UVC_STREAM_FID;
 
        /*
@@ -1236,9 +1239,31 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
                return -EAGAIN;
        }
 
+       /*
+        * Some cameras, when running two parallel streams (one MJPEG alongside
+        * another non-MJPEG stream), are known to lose the EOF packet for a frame.
+        * We can detect the end of a frame by checking for a new SOI marker, as
+        * the SOI always lies on the packet boundary between two frames for
+        * these devices.
+        */
+       if (stream->dev->quirks & UVC_QUIRK_MJPEG_NO_EOF &&
+           (stream->cur_format->fcc == V4L2_PIX_FMT_MJPEG ||
+           stream->cur_format->fcc == V4L2_PIX_FMT_JPEG)) {
+               const u8 *packet = data + header_len;
+
+               if (len >= header_len + 2 &&
+                   packet[0] == 0xff && packet[1] == JPEG_MARKER_SOI &&
+                   buf->bytesused != 0) {
+                       buf->state = UVC_BUF_STATE_READY;
+                       buf->error = 1;
+                       stream->last_fid ^= UVC_STREAM_FID;
+                       return -EAGAIN;
+               }
+       }
+
        stream->last_fid = fid;
 
-       return data[0];
+       return header_len;
 }
 
 static inline enum dma_data_direction uvc_stream_dir(
index 3d196a696f46d5857c5f99bec62cc0d29b59eb9c..5e388f05f3fcaf0e4c503a34745d05837ecb0184 100644 (file)
@@ -76,6 +76,7 @@
 #define UVC_QUIRK_NO_RESET_RESUME      0x00004000
 #define UVC_QUIRK_DISABLE_AUTOSUSPEND  0x00008000
 #define UVC_QUIRK_INVALID_DEVICE_SOF   0x00010000
+#define UVC_QUIRK_MJPEG_NO_EOF         0x00020000
 
 /* Format flags */
 #define UVC_FMT_FLAG_COMPRESSED                0x00000001