media: uvcvideo: Ensure all probed info is returned to v4l2
authorAdam Goode <agoode@google.com>
Sun, 23 Aug 2020 01:21:33 +0000 (03:21 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 29 Oct 2020 08:05:42 +0000 (09:05 +0100)
[ Upstream commit 8a652a17e3c005dcdae31b6c8fdf14382a29cbbe ]

bFrameIndex and bFormatIndex can be negotiated by the camera during
probing, resulting in the camera choosing a different format than
expected. v4l2 can already accommodate such changes, but the code was
not updating the proper fields.

Without such a change, v4l2 would potentially interpret the payload
incorrectly, causing corrupted output. This was happening on the
Elgato HD60 S+, which currently always renegotiates to format 1.

As an aside, the Elgato firmware is buggy and should not be renegotating,
but it is still a valid thing for the camera to do. Both macOS and Windows
will properly probe and read uncorrupted images from this camera.

With this change, both qv4l2 and chromium can now read uncorrupted video
from the Elgato HD60 S+.

[Add blank lines, remove periods at the of messages]

Signed-off-by: Adam Goode <agoode@google.com>
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/media/usb/uvc/uvc_v4l2.c

index 05eed4be25df2ba22e68b016eb18a40cb8516a59..5156c971c241c9d1a183b6e2cc3bdf2e38e20e73 100644 (file)
@@ -257,11 +257,41 @@ static int uvc_v4l2_try_format(struct uvc_streaming *stream,
        if (ret < 0)
                goto done;
 
+       /* After the probe, update fmt with the values returned from
+        * negotiation with the device.
+        */
+       for (i = 0; i < stream->nformats; ++i) {
+               if (probe->bFormatIndex == stream->format[i].index) {
+                       format = &stream->format[i];
+                       break;
+               }
+       }
+
+       if (i == stream->nformats) {
+               uvc_trace(UVC_TRACE_FORMAT, "Unknown bFormatIndex %u\n",
+                         probe->bFormatIndex);
+               return -EINVAL;
+       }
+
+       for (i = 0; i < format->nframes; ++i) {
+               if (probe->bFrameIndex == format->frame[i].bFrameIndex) {
+                       frame = &format->frame[i];
+                       break;
+               }
+       }
+
+       if (i == format->nframes) {
+               uvc_trace(UVC_TRACE_FORMAT, "Unknown bFrameIndex %u\n",
+                         probe->bFrameIndex);
+               return -EINVAL;
+       }
+
        fmt->fmt.pix.width = frame->wWidth;
        fmt->fmt.pix.height = frame->wHeight;
        fmt->fmt.pix.field = V4L2_FIELD_NONE;
        fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
        fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
+       fmt->fmt.pix.pixelformat = format->fcc;
        fmt->fmt.pix.colorspace = format->colorspace;
        fmt->fmt.pix.priv = 0;