blob: 48503f30b3a2fcb2f4fd5a015879b41fa52473ce [file] [log] [blame]
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001/*
2 * uvc_video.c -- USB Video Class driver - Video handling
3 *
Laurent Pinchart11fc5ba2010-09-20 06:10:10 -03004 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 */
13
14#include <linux/kernel.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030015#include <linux/list.h>
16#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030018#include <linux/usb.h>
19#include <linux/videodev2.h>
20#include <linux/vmalloc.h>
21#include <linux/wait.h>
Arun Sharma600634972011-07-26 16:09:06 -070022#include <linux/atomic.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030023#include <asm/unaligned.h>
24
25#include <media/v4l2-common.h>
26
27#include "uvcvideo.h"
28
29/* ------------------------------------------------------------------------
30 * UVC Controls
31 */
32
33static int __uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
34 __u8 intfnum, __u8 cs, void *data, __u16 size,
35 int timeout)
36{
37 __u8 type = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
38 unsigned int pipe;
Laurent Pinchartc0efd232008-06-30 15:04:50 -030039
40 pipe = (query & 0x80) ? usb_rcvctrlpipe(dev->udev, 0)
41 : usb_sndctrlpipe(dev->udev, 0);
42 type |= (query & 0x80) ? USB_DIR_IN : USB_DIR_OUT;
43
Laurent Pinchart44f00792008-11-08 19:14:50 -030044 return usb_control_msg(dev->udev, pipe, query, type, cs << 8,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030045 unit << 8 | intfnum, data, size, timeout);
Laurent Pinchart44f00792008-11-08 19:14:50 -030046}
Laurent Pinchartc0efd232008-06-30 15:04:50 -030047
Laurent Pinchart707dcd92010-09-17 05:37:26 -030048static const char *uvc_query_name(__u8 query)
49{
50 switch (query) {
51 case UVC_SET_CUR:
52 return "SET_CUR";
53 case UVC_GET_CUR:
54 return "GET_CUR";
55 case UVC_GET_MIN:
56 return "GET_MIN";
57 case UVC_GET_MAX:
58 return "GET_MAX";
59 case UVC_GET_RES:
60 return "GET_RES";
61 case UVC_GET_LEN:
62 return "GET_LEN";
63 case UVC_GET_INFO:
64 return "GET_INFO";
65 case UVC_GET_DEF:
66 return "GET_DEF";
67 default:
68 return "<invalid>";
69 }
70}
71
Laurent Pinchart44f00792008-11-08 19:14:50 -030072int uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
73 __u8 intfnum, __u8 cs, void *data, __u16 size)
74{
75 int ret;
76
77 ret = __uvc_query_ctrl(dev, query, unit, intfnum, cs, data, size,
78 UVC_CTRL_CONTROL_TIMEOUT);
Laurent Pinchartc0efd232008-06-30 15:04:50 -030079 if (ret != size) {
Laurent Pinchart707dcd92010-09-17 05:37:26 -030080 uvc_printk(KERN_ERR, "Failed to query (%s) UVC control %u on "
81 "unit %u: %d (exp. %u).\n", uvc_query_name(query), cs,
82 unit, ret, size);
Laurent Pinchartc0efd232008-06-30 15:04:50 -030083 return -EIO;
84 }
85
86 return 0;
87}
88
Laurent Pinchart35f02a62009-06-28 08:37:50 -030089static void uvc_fixup_video_ctrl(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030090 struct uvc_streaming_control *ctrl)
91{
Stephan Lachowsky38a66822011-01-27 23:04:33 -030092 struct uvc_format *format = NULL;
Laurent Pinchart078f8942009-05-06 12:30:30 -030093 struct uvc_frame *frame = NULL;
94 unsigned int i;
Laurent Pinchartc0efd232008-06-30 15:04:50 -030095
Stephan Lachowsky38a66822011-01-27 23:04:33 -030096 for (i = 0; i < stream->nformats; ++i) {
97 if (stream->format[i].index == ctrl->bFormatIndex) {
98 format = &stream->format[i];
99 break;
100 }
101 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300102
Stephan Lachowsky38a66822011-01-27 23:04:33 -0300103 if (format == NULL)
104 return;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300105
Laurent Pinchart078f8942009-05-06 12:30:30 -0300106 for (i = 0; i < format->nframes; ++i) {
107 if (format->frame[i].bFrameIndex == ctrl->bFrameIndex) {
108 frame = &format->frame[i];
109 break;
110 }
111 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300112
Laurent Pinchart078f8942009-05-06 12:30:30 -0300113 if (frame == NULL)
114 return;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300115
116 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) ||
117 (ctrl->dwMaxVideoFrameSize == 0 &&
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300118 stream->dev->uvc_version < 0x0110))
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300119 ctrl->dwMaxVideoFrameSize =
120 frame->dwMaxVideoFrameBufferSize;
Laurent Pinchart50144ae2009-02-16 17:41:52 -0300121
Laurent Pinchart37f85b272015-05-12 18:57:53 -0300122 /* The "TOSHIBA Web Camera - 5M" Chicony device (04f2:b50b) seems to
123 * compute the bandwidth on 16 bits and erroneously sign-extend it to
124 * 32 bits, resulting in a huge bandwidth value. Detect and fix that
125 * condition by setting the 16 MSBs to 0 when they're all equal to 1.
126 */
127 if ((ctrl->dwMaxPayloadTransferSize & 0xffff0000) == 0xffff0000)
128 ctrl->dwMaxPayloadTransferSize &= ~0xffff0000;
129
Laurent Pincharta2e35af2009-11-04 11:09:12 -0300130 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) &&
131 stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH &&
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300132 stream->intf->num_altsetting > 1) {
Laurent Pinchart50144ae2009-02-16 17:41:52 -0300133 u32 interval;
134 u32 bandwidth;
135
136 interval = (ctrl->dwFrameInterval > 100000)
137 ? ctrl->dwFrameInterval
138 : frame->dwFrameInterval[0];
139
140 /* Compute a bandwidth estimation by multiplying the frame
141 * size by the number of video frames per second, divide the
142 * result by the number of USB frames (or micro-frames for
143 * high-speed devices) per second and add the UVC header size
144 * (assumed to be 12 bytes long).
145 */
146 bandwidth = frame->wWidth * frame->wHeight / 8 * format->bpp;
147 bandwidth *= 10000000 / interval + 1;
148 bandwidth /= 1000;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300149 if (stream->dev->udev->speed == USB_SPEED_HIGH)
Laurent Pinchart50144ae2009-02-16 17:41:52 -0300150 bandwidth /= 8;
151 bandwidth += 12;
152
Laurent Pinchart9bb72622010-10-03 17:40:29 -0300153 /* The bandwidth estimate is too low for many cameras. Don't use
154 * maximum packet sizes lower than 1024 bytes to try and work
155 * around the problem. According to measurements done on two
156 * different camera models, the value is high enough to get most
157 * resolutions working while not preventing two simultaneous
158 * VGA streams at 15 fps.
159 */
160 bandwidth = max_t(u32, bandwidth, 1024);
161
Laurent Pinchart50144ae2009-02-16 17:41:52 -0300162 ctrl->dwMaxPayloadTransferSize = bandwidth;
163 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300164}
165
ming_qian143c0f12018-05-08 22:13:08 -0400166static size_t uvc_video_ctrl_size(struct uvc_streaming *stream)
167{
168 /*
169 * Return the size of the video probe and commit controls, which depends
170 * on the protocol version.
171 */
172 if (stream->dev->uvc_version < 0x0110)
173 return 26;
174 else if (stream->dev->uvc_version < 0x0150)
175 return 34;
176 else
177 return 48;
178}
179
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300180static int uvc_get_video_ctrl(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300181 struct uvc_streaming_control *ctrl, int probe, __u8 query)
182{
ming_qian143c0f12018-05-08 22:13:08 -0400183 __u16 size = uvc_video_ctrl_size(stream);
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300184 __u8 *data;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300185 int ret;
186
Julia Lawalld2be7642009-09-17 23:44:01 -0300187 if ((stream->dev->quirks & UVC_QUIRK_PROBE_DEF) &&
188 query == UVC_GET_DEF)
189 return -EIO;
190
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300191 data = kmalloc(size, GFP_KERNEL);
192 if (data == NULL)
193 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300194
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300195 ret = __uvc_query_ctrl(stream->dev, query, 0, stream->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300196 probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
Laurent Pinchartb232a012009-10-09 20:55:23 -0300197 size, uvc_timeout_param);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300198
Laurent Pinchartb482d922009-06-26 11:39:42 -0300199 if ((query == UVC_GET_MIN || query == UVC_GET_MAX) && ret == 2) {
Laurent Pinchart44f00792008-11-08 19:14:50 -0300200 /* Some cameras, mostly based on Bison Electronics chipsets,
201 * answer a GET_MIN or GET_MAX request with the wCompQuality
202 * field only.
203 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300204 uvc_warn_once(stream->dev, UVC_WARN_MINMAX, "UVC non "
Laurent Pinchart44f00792008-11-08 19:14:50 -0300205 "compliance - GET_MIN/MAX(PROBE) incorrectly "
206 "supported. Enabling workaround.\n");
Julia Lawallddb0b342009-12-10 17:14:27 -0300207 memset(ctrl, 0, sizeof *ctrl);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300208 ctrl->wCompQuality = le16_to_cpup((__le16 *)data);
209 ret = 0;
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300210 goto out;
Laurent Pinchartb482d922009-06-26 11:39:42 -0300211 } else if (query == UVC_GET_DEF && probe == 1 && ret != size) {
Laurent Pinchart44f00792008-11-08 19:14:50 -0300212 /* Many cameras don't support the GET_DEF request on their
213 * video probe control. Warn once and return, the caller will
214 * fall back to GET_CUR.
215 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300216 uvc_warn_once(stream->dev, UVC_WARN_PROBE_DEF, "UVC non "
Laurent Pinchart44f00792008-11-08 19:14:50 -0300217 "compliance - GET_DEF(PROBE) not supported. "
218 "Enabling workaround.\n");
219 ret = -EIO;
220 goto out;
221 } else if (ret != size) {
222 uvc_printk(KERN_ERR, "Failed to query (%u) UVC %s control : "
223 "%d (exp. %u).\n", query, probe ? "probe" : "commit",
224 ret, size);
225 ret = -EIO;
226 goto out;
227 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300228
229 ctrl->bmHint = le16_to_cpup((__le16 *)&data[0]);
230 ctrl->bFormatIndex = data[2];
231 ctrl->bFrameIndex = data[3];
232 ctrl->dwFrameInterval = le32_to_cpup((__le32 *)&data[4]);
233 ctrl->wKeyFrameRate = le16_to_cpup((__le16 *)&data[8]);
234 ctrl->wPFrameRate = le16_to_cpup((__le16 *)&data[10]);
235 ctrl->wCompQuality = le16_to_cpup((__le16 *)&data[12]);
236 ctrl->wCompWindowSize = le16_to_cpup((__le16 *)&data[14]);
237 ctrl->wDelay = le16_to_cpup((__le16 *)&data[16]);
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300238 ctrl->dwMaxVideoFrameSize = get_unaligned_le32(&data[18]);
239 ctrl->dwMaxPayloadTransferSize = get_unaligned_le32(&data[22]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300240
ming_qian143c0f12018-05-08 22:13:08 -0400241 if (size >= 34) {
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300242 ctrl->dwClockFrequency = get_unaligned_le32(&data[26]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300243 ctrl->bmFramingInfo = data[30];
244 ctrl->bPreferedVersion = data[31];
245 ctrl->bMinVersion = data[32];
246 ctrl->bMaxVersion = data[33];
247 } else {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300248 ctrl->dwClockFrequency = stream->dev->clock_frequency;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300249 ctrl->bmFramingInfo = 0;
250 ctrl->bPreferedVersion = 0;
251 ctrl->bMinVersion = 0;
252 ctrl->bMaxVersion = 0;
253 }
254
Laurent Pinchart50144ae2009-02-16 17:41:52 -0300255 /* Some broken devices return null or wrong dwMaxVideoFrameSize and
256 * dwMaxPayloadTransferSize fields. Try to get the value from the
257 * format and frame descriptors.
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300258 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300259 uvc_fixup_video_ctrl(stream, ctrl);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300260 ret = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300261
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300262out:
263 kfree(data);
264 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300265}
266
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300267static int uvc_set_video_ctrl(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300268 struct uvc_streaming_control *ctrl, int probe)
269{
ming_qian143c0f12018-05-08 22:13:08 -0400270 __u16 size = uvc_video_ctrl_size(stream);
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300271 __u8 *data;
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300272 int ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300273
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300274 data = kzalloc(size, GFP_KERNEL);
275 if (data == NULL)
276 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300277
278 *(__le16 *)&data[0] = cpu_to_le16(ctrl->bmHint);
279 data[2] = ctrl->bFormatIndex;
280 data[3] = ctrl->bFrameIndex;
281 *(__le32 *)&data[4] = cpu_to_le32(ctrl->dwFrameInterval);
282 *(__le16 *)&data[8] = cpu_to_le16(ctrl->wKeyFrameRate);
283 *(__le16 *)&data[10] = cpu_to_le16(ctrl->wPFrameRate);
284 *(__le16 *)&data[12] = cpu_to_le16(ctrl->wCompQuality);
285 *(__le16 *)&data[14] = cpu_to_le16(ctrl->wCompWindowSize);
286 *(__le16 *)&data[16] = cpu_to_le16(ctrl->wDelay);
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300287 put_unaligned_le32(ctrl->dwMaxVideoFrameSize, &data[18]);
288 put_unaligned_le32(ctrl->dwMaxPayloadTransferSize, &data[22]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300289
ming_qian143c0f12018-05-08 22:13:08 -0400290 if (size >= 34) {
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300291 put_unaligned_le32(ctrl->dwClockFrequency, &data[26]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300292 data[30] = ctrl->bmFramingInfo;
293 data[31] = ctrl->bPreferedVersion;
294 data[32] = ctrl->bMinVersion;
295 data[33] = ctrl->bMaxVersion;
296 }
297
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300298 ret = __uvc_query_ctrl(stream->dev, UVC_SET_CUR, 0, stream->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300299 probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
Laurent Pinchartb232a012009-10-09 20:55:23 -0300300 size, uvc_timeout_param);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300301 if (ret != size) {
302 uvc_printk(KERN_ERR, "Failed to set UVC %s control : "
303 "%d (exp. %u).\n", probe ? "probe" : "commit",
304 ret, size);
305 ret = -EIO;
306 }
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300307
308 kfree(data);
309 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300310}
311
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300312int uvc_probe_video(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300313 struct uvc_streaming_control *probe)
314{
315 struct uvc_streaming_control probe_min, probe_max;
316 __u16 bandwidth;
317 unsigned int i;
318 int ret;
319
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300320 /* Perform probing. The device should adjust the requested values
321 * according to its capabilities. However, some devices, namely the
322 * first generation UVC Logitech webcams, don't implement the Video
323 * Probe control properly, and just return the needed bandwidth. For
324 * that reason, if the needed bandwidth exceeds the maximum available
325 * bandwidth, try to lower the quality.
326 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300327 ret = uvc_set_video_ctrl(stream, probe, 1);
328 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300329 goto done;
330
331 /* Get the minimum and maximum values for compression settings. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300332 if (!(stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX)) {
333 ret = uvc_get_video_ctrl(stream, &probe_min, 1, UVC_GET_MIN);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300334 if (ret < 0)
335 goto done;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300336 ret = uvc_get_video_ctrl(stream, &probe_max, 1, UVC_GET_MAX);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300337 if (ret < 0)
338 goto done;
339
340 probe->wCompQuality = probe_max.wCompQuality;
341 }
342
343 for (i = 0; i < 2; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300344 ret = uvc_set_video_ctrl(stream, probe, 1);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300345 if (ret < 0)
346 goto done;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300347 ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300348 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300349 goto done;
350
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300351 if (stream->intf->num_altsetting == 1)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300352 break;
353
354 bandwidth = probe->dwMaxPayloadTransferSize;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300355 if (bandwidth <= stream->maxpsize)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300356 break;
357
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300358 if (stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300359 ret = -ENOSPC;
360 goto done;
361 }
362
363 /* TODO: negotiate compression parameters */
364 probe->wKeyFrameRate = probe_min.wKeyFrameRate;
365 probe->wPFrameRate = probe_min.wPFrameRate;
366 probe->wCompQuality = probe_max.wCompQuality;
367 probe->wCompWindowSize = probe_min.wCompWindowSize;
368 }
369
370done:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300371 return ret;
372}
373
Laurent Pinchart0c6a3b22011-11-03 07:22:39 -0300374static int uvc_commit_video(struct uvc_streaming *stream,
375 struct uvc_streaming_control *probe)
Laurent Pinchart44f00792008-11-08 19:14:50 -0300376{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300377 return uvc_set_video_ctrl(stream, probe, 0);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300378}
379
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300380/* -----------------------------------------------------------------------------
381 * Clocks and timestamps
382 */
383
Olivier Langlois3b35fc82014-03-28 02:42:38 -0300384static inline void uvc_video_get_ts(struct timespec *ts)
385{
386 if (uvc_clock_param == CLOCK_MONOTONIC)
387 ktime_get_ts(ts);
388 else
389 ktime_get_real_ts(ts);
390}
391
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300392static void
393uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf,
394 const __u8 *data, int len)
395{
396 struct uvc_clock_sample *sample;
397 unsigned int header_size;
398 bool has_pts = false;
399 bool has_scr = false;
400 unsigned long flags;
401 struct timespec ts;
402 u16 host_sof;
403 u16 dev_sof;
404
405 switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) {
406 case UVC_STREAM_PTS | UVC_STREAM_SCR:
407 header_size = 12;
408 has_pts = true;
409 has_scr = true;
410 break;
411 case UVC_STREAM_PTS:
412 header_size = 6;
413 has_pts = true;
414 break;
415 case UVC_STREAM_SCR:
416 header_size = 8;
417 has_scr = true;
418 break;
419 default:
420 header_size = 2;
421 break;
422 }
423
424 /* Check for invalid headers. */
425 if (len < header_size)
426 return;
427
428 /* Extract the timestamps:
429 *
430 * - store the frame PTS in the buffer structure
431 * - if the SCR field is present, retrieve the host SOF counter and
432 * kernel timestamps and store them with the SCR STC and SOF fields
433 * in the ring buffer
434 */
435 if (has_pts && buf != NULL)
436 buf->pts = get_unaligned_le32(&data[2]);
437
438 if (!has_scr)
439 return;
440
441 /* To limit the amount of data, drop SCRs with an SOF identical to the
442 * previous one.
443 */
444 dev_sof = get_unaligned_le16(&data[header_size - 2]);
445 if (dev_sof == stream->clock.last_sof)
446 return;
447
448 stream->clock.last_sof = dev_sof;
449
450 host_sof = usb_get_current_frame_number(stream->dev->udev);
Olivier Langlois3b35fc82014-03-28 02:42:38 -0300451 uvc_video_get_ts(&ts);
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300452
453 /* The UVC specification allows device implementations that can't obtain
454 * the USB frame number to keep their own frame counters as long as they
455 * match the size and frequency of the frame number associated with USB
456 * SOF tokens. The SOF values sent by such devices differ from the USB
457 * SOF tokens by a fixed offset that needs to be estimated and accounted
458 * for to make timestamp recovery as accurate as possible.
459 *
460 * The offset is estimated the first time a device SOF value is received
461 * as the difference between the host and device SOF values. As the two
462 * SOF values can differ slightly due to transmission delays, consider
463 * that the offset is null if the difference is not higher than 10 ms
464 * (negative differences can not happen and are thus considered as an
465 * offset). The video commit control wDelay field should be used to
466 * compute a dynamic threshold instead of using a fixed 10 ms value, but
467 * devices don't report reliable wDelay values.
468 *
469 * See uvc_video_clock_host_sof() for an explanation regarding why only
470 * the 8 LSBs of the delta are kept.
471 */
472 if (stream->clock.sof_offset == (u16)-1) {
473 u16 delta_sof = (host_sof - dev_sof) & 255;
474 if (delta_sof >= 10)
475 stream->clock.sof_offset = delta_sof;
476 else
477 stream->clock.sof_offset = 0;
478 }
479
480 dev_sof = (dev_sof + stream->clock.sof_offset) & 2047;
481
482 spin_lock_irqsave(&stream->clock.lock, flags);
483
484 sample = &stream->clock.samples[stream->clock.head];
485 sample->dev_stc = get_unaligned_le32(&data[header_size - 6]);
486 sample->dev_sof = dev_sof;
487 sample->host_sof = host_sof;
488 sample->host_ts = ts;
489
490 /* Update the sliding window head and count. */
491 stream->clock.head = (stream->clock.head + 1) % stream->clock.size;
492
493 if (stream->clock.count < stream->clock.size)
494 stream->clock.count++;
495
496 spin_unlock_irqrestore(&stream->clock.lock, flags);
497}
498
Laurent Pincharted0ee0c2012-03-27 05:51:00 -0300499static void uvc_video_clock_reset(struct uvc_streaming *stream)
500{
501 struct uvc_clock *clock = &stream->clock;
502
503 clock->head = 0;
504 clock->count = 0;
505 clock->last_sof = -1;
506 clock->sof_offset = -1;
507}
508
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300509static int uvc_video_clock_init(struct uvc_streaming *stream)
510{
511 struct uvc_clock *clock = &stream->clock;
512
513 spin_lock_init(&clock->lock);
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300514 clock->size = 32;
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300515
516 clock->samples = kmalloc(clock->size * sizeof(*clock->samples),
517 GFP_KERNEL);
518 if (clock->samples == NULL)
519 return -ENOMEM;
520
Laurent Pincharted0ee0c2012-03-27 05:51:00 -0300521 uvc_video_clock_reset(stream);
522
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300523 return 0;
524}
525
526static void uvc_video_clock_cleanup(struct uvc_streaming *stream)
527{
528 kfree(stream->clock.samples);
529 stream->clock.samples = NULL;
530}
531
532/*
533 * uvc_video_clock_host_sof - Return the host SOF value for a clock sample
534 *
535 * Host SOF counters reported by usb_get_current_frame_number() usually don't
536 * cover the whole 11-bits SOF range (0-2047) but are limited to the HCI frame
537 * schedule window. They can be limited to 8, 9 or 10 bits depending on the host
538 * controller and its configuration.
539 *
540 * We thus need to recover the SOF value corresponding to the host frame number.
541 * As the device and host frame numbers are sampled in a short interval, the
542 * difference between their values should be equal to a small delta plus an
543 * integer multiple of 256 caused by the host frame number limited precision.
544 *
545 * To obtain the recovered host SOF value, compute the small delta by masking
546 * the high bits of the host frame counter and device SOF difference and add it
547 * to the device SOF value.
548 */
549static u16 uvc_video_clock_host_sof(const struct uvc_clock_sample *sample)
550{
551 /* The delta value can be negative. */
552 s8 delta_sof;
553
554 delta_sof = (sample->host_sof - sample->dev_sof) & 255;
555
556 return (sample->dev_sof + delta_sof) & 2047;
557}
558
559/*
560 * uvc_video_clock_update - Update the buffer timestamp
561 *
562 * This function converts the buffer PTS timestamp to the host clock domain by
563 * going through the USB SOF clock domain and stores the result in the V4L2
564 * buffer timestamp field.
565 *
566 * The relationship between the device clock and the host clock isn't known.
567 * However, the device and the host share the common USB SOF clock which can be
568 * used to recover that relationship.
569 *
570 * The relationship between the device clock and the USB SOF clock is considered
571 * to be linear over the clock samples sliding window and is given by
572 *
573 * SOF = m * PTS + p
574 *
575 * Several methods to compute the slope (m) and intercept (p) can be used. As
576 * the clock drift should be small compared to the sliding window size, we
577 * assume that the line that goes through the points at both ends of the window
578 * is a good approximation. Naming those points P1 and P2, we get
579 *
580 * SOF = (SOF2 - SOF1) / (STC2 - STC1) * PTS
581 * + (SOF1 * STC2 - SOF2 * STC1) / (STC2 - STC1)
582 *
583 * or
584 *
585 * SOF = ((SOF2 - SOF1) * PTS + SOF1 * STC2 - SOF2 * STC1) / (STC2 - STC1) (1)
586 *
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300587 * to avoid losing precision in the division. Similarly, the host timestamp is
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300588 * computed with
589 *
590 * TS = ((TS2 - TS1) * PTS + TS1 * SOF2 - TS2 * SOF1) / (SOF2 - SOF1) (2)
591 *
592 * SOF values are coded on 11 bits by USB. We extend their precision with 16
593 * decimal bits, leading to a 11.16 coding.
594 *
595 * TODO: To avoid surprises with device clock values, PTS/STC timestamps should
596 * be normalized using the nominal device clock frequency reported through the
597 * UVC descriptors.
598 *
599 * Both the PTS/STC and SOF counters roll over, after a fixed but device
600 * specific amount of time for PTS/STC and after 2048ms for SOF. As long as the
601 * sliding window size is smaller than the rollover period, differences computed
602 * on unsigned integers will produce the correct result. However, the p term in
603 * the linear relations will be miscomputed.
604 *
605 * To fix the issue, we subtract a constant from the PTS and STC values to bring
606 * PTS to half the 32 bit STC range. The sliding window STC values then fit into
607 * the 32 bit range without any rollover.
608 *
609 * Similarly, we add 2048 to the device SOF values to make sure that the SOF
610 * computed by (1) will never be smaller than 0. This offset is then compensated
611 * by adding 2048 to the SOF values used in (2). However, this doesn't prevent
612 * rollovers between (1) and (2): the SOF value computed by (1) can be slightly
613 * lower than 4096, and the host SOF counters can have rolled over to 2048. This
614 * case is handled by subtracting 2048 from the SOF value if it exceeds the host
615 * SOF value at the end of the sliding window.
616 *
617 * Finally we subtract a constant from the host timestamps to bring the first
618 * timestamp of the sliding window to 1s.
619 */
620void uvc_video_clock_update(struct uvc_streaming *stream,
Junghak Sung2d700712015-09-22 10:30:30 -0300621 struct vb2_v4l2_buffer *vbuf,
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300622 struct uvc_buffer *buf)
623{
624 struct uvc_clock *clock = &stream->clock;
625 struct uvc_clock_sample *first;
626 struct uvc_clock_sample *last;
627 unsigned long flags;
628 struct timespec ts;
629 u32 delta_stc;
630 u32 y1, y2;
631 u32 x1, x2;
632 u32 mean;
633 u32 sof;
634 u32 div;
635 u32 rem;
636 u64 y;
637
Laurent Pinchart5d0fd3c2015-07-27 11:06:48 -0300638 if (!uvc_hw_timestamps_param)
639 return;
640
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300641 spin_lock_irqsave(&clock->lock, flags);
642
643 if (clock->count < clock->size)
644 goto done;
645
646 first = &clock->samples[clock->head];
647 last = &clock->samples[(clock->head - 1) % clock->size];
648
649 /* First step, PTS to SOF conversion. */
650 delta_stc = buf->pts - (1UL << 31);
651 x1 = first->dev_stc - delta_stc;
652 x2 = last->dev_stc - delta_stc;
Laurent Pinchart8e57dec2012-01-15 13:53:45 -0300653 if (x1 == x2)
654 goto done;
655
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300656 y1 = (first->dev_sof + 2048) << 16;
657 y2 = (last->dev_sof + 2048) << 16;
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300658 if (y2 < y1)
659 y2 += 2048 << 16;
660
661 y = (u64)(y2 - y1) * (1ULL << 31) + (u64)y1 * (u64)x2
662 - (u64)y2 * (u64)x1;
663 y = div_u64(y, x2 - x1);
664
665 sof = y;
666
667 uvc_trace(UVC_TRACE_CLOCK, "%s: PTS %u y %llu.%06llu SOF %u.%06llu "
668 "(x1 %u x2 %u y1 %u y2 %u SOF offset %u)\n",
669 stream->dev->name, buf->pts,
670 y >> 16, div_u64((y & 0xffff) * 1000000, 65536),
671 sof >> 16, div_u64(((u64)sof & 0xffff) * 1000000LLU, 65536),
672 x1, x2, y1, y2, clock->sof_offset);
673
674 /* Second step, SOF to host clock conversion. */
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300675 x1 = (uvc_video_clock_host_sof(first) + 2048) << 16;
676 x2 = (uvc_video_clock_host_sof(last) + 2048) << 16;
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300677 if (x2 < x1)
678 x2 += 2048 << 16;
Laurent Pinchart8e57dec2012-01-15 13:53:45 -0300679 if (x1 == x2)
680 goto done;
681
682 ts = timespec_sub(last->host_ts, first->host_ts);
683 y1 = NSEC_PER_SEC;
684 y2 = (ts.tv_sec + 1) * NSEC_PER_SEC + ts.tv_nsec;
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300685
686 /* Interpolated and host SOF timestamps can wrap around at slightly
687 * different times. Handle this by adding or removing 2048 to or from
688 * the computed SOF value to keep it close to the SOF samples mean
689 * value.
690 */
691 mean = (x1 + x2) / 2;
692 if (mean - (1024 << 16) > sof)
693 sof += 2048 << 16;
694 else if (sof > mean + (1024 << 16))
695 sof -= 2048 << 16;
696
697 y = (u64)(y2 - y1) * (u64)sof + (u64)y1 * (u64)x2
698 - (u64)y2 * (u64)x1;
699 y = div_u64(y, x2 - x1);
700
701 div = div_u64_rem(y, NSEC_PER_SEC, &rem);
702 ts.tv_sec = first->host_ts.tv_sec - 1 + div;
703 ts.tv_nsec = first->host_ts.tv_nsec + rem;
704 if (ts.tv_nsec >= NSEC_PER_SEC) {
705 ts.tv_sec++;
706 ts.tv_nsec -= NSEC_PER_SEC;
707 }
708
Junghak Sungd6dd6452015-11-03 08:16:37 -0200709 uvc_trace(UVC_TRACE_CLOCK, "%s: SOF %u.%06llu y %llu ts %llu "
710 "buf ts %llu (x1 %u/%u/%u x2 %u/%u/%u y1 %u y2 %u)\n",
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300711 stream->dev->name,
712 sof >> 16, div_u64(((u64)sof & 0xffff) * 1000000LLU, 65536),
Junghak Sungd6dd6452015-11-03 08:16:37 -0200713 y, timespec_to_ns(&ts), vbuf->vb2_buf.timestamp,
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300714 x1, first->host_sof, first->dev_sof,
715 x2, last->host_sof, last->dev_sof, y1, y2);
716
717 /* Update the V4L2 buffer. */
Junghak Sungd6dd6452015-11-03 08:16:37 -0200718 vbuf->vb2_buf.timestamp = timespec_to_ns(&ts);
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300719
720done:
Dan Carpenter0aff8a82015-10-22 07:09:05 -0200721 spin_unlock_irqrestore(&clock->lock, flags);
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300722}
723
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300724/* ------------------------------------------------------------------------
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300725 * Stream statistics
726 */
727
728static void uvc_video_stats_decode(struct uvc_streaming *stream,
729 const __u8 *data, int len)
730{
731 unsigned int header_size;
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300732 bool has_pts = false;
733 bool has_scr = false;
734 u16 uninitialized_var(scr_sof);
735 u32 uninitialized_var(scr_stc);
736 u32 uninitialized_var(pts);
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300737
738 if (stream->stats.stream.nb_frames == 0 &&
739 stream->stats.frame.nb_packets == 0)
740 ktime_get_ts(&stream->stats.stream.start_ts);
741
742 switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) {
743 case UVC_STREAM_PTS | UVC_STREAM_SCR:
744 header_size = 12;
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300745 has_pts = true;
746 has_scr = true;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300747 break;
748 case UVC_STREAM_PTS:
749 header_size = 6;
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300750 has_pts = true;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300751 break;
752 case UVC_STREAM_SCR:
753 header_size = 8;
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300754 has_scr = true;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300755 break;
756 default:
757 header_size = 2;
758 break;
759 }
760
761 /* Check for invalid headers. */
762 if (len < header_size || data[0] < header_size) {
763 stream->stats.frame.nb_invalid++;
764 return;
765 }
766
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300767 /* Extract the timestamps. */
768 if (has_pts)
769 pts = get_unaligned_le32(&data[2]);
770
771 if (has_scr) {
772 scr_stc = get_unaligned_le32(&data[header_size - 6]);
773 scr_sof = get_unaligned_le16(&data[header_size - 2]);
774 }
775
776 /* Is PTS constant through the whole frame ? */
777 if (has_pts && stream->stats.frame.nb_pts) {
778 if (stream->stats.frame.pts != pts) {
779 stream->stats.frame.nb_pts_diffs++;
780 stream->stats.frame.last_pts_diff =
781 stream->stats.frame.nb_packets;
782 }
783 }
784
785 if (has_pts) {
786 stream->stats.frame.nb_pts++;
787 stream->stats.frame.pts = pts;
788 }
789
790 /* Do all frames have a PTS in their first non-empty packet, or before
791 * their first empty packet ?
792 */
793 if (stream->stats.frame.size == 0) {
794 if (len > header_size)
795 stream->stats.frame.has_initial_pts = has_pts;
796 if (len == header_size && has_pts)
797 stream->stats.frame.has_early_pts = true;
798 }
799
800 /* Do the SCR.STC and SCR.SOF fields vary through the frame ? */
801 if (has_scr && stream->stats.frame.nb_scr) {
802 if (stream->stats.frame.scr_stc != scr_stc)
803 stream->stats.frame.nb_scr_diffs++;
804 }
805
806 if (has_scr) {
807 /* Expand the SOF counter to 32 bits and store its value. */
808 if (stream->stats.stream.nb_frames > 0 ||
809 stream->stats.frame.nb_scr > 0)
810 stream->stats.stream.scr_sof_count +=
811 (scr_sof - stream->stats.stream.scr_sof) % 2048;
812 stream->stats.stream.scr_sof = scr_sof;
813
814 stream->stats.frame.nb_scr++;
815 stream->stats.frame.scr_stc = scr_stc;
816 stream->stats.frame.scr_sof = scr_sof;
817
818 if (scr_sof < stream->stats.stream.min_sof)
819 stream->stats.stream.min_sof = scr_sof;
820 if (scr_sof > stream->stats.stream.max_sof)
821 stream->stats.stream.max_sof = scr_sof;
822 }
823
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300824 /* Record the first non-empty packet number. */
825 if (stream->stats.frame.size == 0 && len > header_size)
826 stream->stats.frame.first_data = stream->stats.frame.nb_packets;
827
828 /* Update the frame size. */
829 stream->stats.frame.size += len - header_size;
830
831 /* Update the packets counters. */
832 stream->stats.frame.nb_packets++;
833 if (len > header_size)
834 stream->stats.frame.nb_empty++;
835
836 if (data[1] & UVC_STREAM_ERR)
837 stream->stats.frame.nb_errors++;
838}
839
840static void uvc_video_stats_update(struct uvc_streaming *stream)
841{
842 struct uvc_stats_frame *frame = &stream->stats.frame;
843
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300844 uvc_trace(UVC_TRACE_STATS, "frame %u stats: %u/%u/%u packets, "
845 "%u/%u/%u pts (%searly %sinitial), %u/%u scr, "
846 "last pts/stc/sof %u/%u/%u\n",
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300847 stream->sequence, frame->first_data,
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300848 frame->nb_packets - frame->nb_empty, frame->nb_packets,
849 frame->nb_pts_diffs, frame->last_pts_diff, frame->nb_pts,
850 frame->has_early_pts ? "" : "!",
851 frame->has_initial_pts ? "" : "!",
852 frame->nb_scr_diffs, frame->nb_scr,
853 frame->pts, frame->scr_stc, frame->scr_sof);
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300854
855 stream->stats.stream.nb_frames++;
856 stream->stats.stream.nb_packets += stream->stats.frame.nb_packets;
857 stream->stats.stream.nb_empty += stream->stats.frame.nb_empty;
858 stream->stats.stream.nb_errors += stream->stats.frame.nb_errors;
859 stream->stats.stream.nb_invalid += stream->stats.frame.nb_invalid;
860
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300861 if (frame->has_early_pts)
862 stream->stats.stream.nb_pts_early++;
863 if (frame->has_initial_pts)
864 stream->stats.stream.nb_pts_initial++;
865 if (frame->last_pts_diff <= frame->first_data)
866 stream->stats.stream.nb_pts_constant++;
867 if (frame->nb_scr >= frame->nb_packets - frame->nb_empty)
868 stream->stats.stream.nb_scr_count_ok++;
869 if (frame->nb_scr_diffs + 1 == frame->nb_scr)
870 stream->stats.stream.nb_scr_diffs_ok++;
871
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300872 memset(&stream->stats.frame, 0, sizeof(stream->stats.frame));
873}
874
875size_t uvc_video_stats_dump(struct uvc_streaming *stream, char *buf,
876 size_t size)
877{
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300878 unsigned int scr_sof_freq;
879 unsigned int duration;
880 struct timespec ts;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300881 size_t count = 0;
882
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300883 ts.tv_sec = stream->stats.stream.stop_ts.tv_sec
884 - stream->stats.stream.start_ts.tv_sec;
885 ts.tv_nsec = stream->stats.stream.stop_ts.tv_nsec
886 - stream->stats.stream.start_ts.tv_nsec;
887 if (ts.tv_nsec < 0) {
888 ts.tv_sec--;
889 ts.tv_nsec += 1000000000;
890 }
891
892 /* Compute the SCR.SOF frequency estimate. At the nominal 1kHz SOF
893 * frequency this will not overflow before more than 1h.
894 */
895 duration = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
896 if (duration != 0)
897 scr_sof_freq = stream->stats.stream.scr_sof_count * 1000
898 / duration;
899 else
900 scr_sof_freq = 0;
901
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300902 count += scnprintf(buf + count, size - count,
903 "frames: %u\npackets: %u\nempty: %u\n"
904 "errors: %u\ninvalid: %u\n",
905 stream->stats.stream.nb_frames,
906 stream->stats.stream.nb_packets,
907 stream->stats.stream.nb_empty,
908 stream->stats.stream.nb_errors,
909 stream->stats.stream.nb_invalid);
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300910 count += scnprintf(buf + count, size - count,
911 "pts: %u early, %u initial, %u ok\n",
912 stream->stats.stream.nb_pts_early,
913 stream->stats.stream.nb_pts_initial,
914 stream->stats.stream.nb_pts_constant);
915 count += scnprintf(buf + count, size - count,
916 "scr: %u count ok, %u diff ok\n",
917 stream->stats.stream.nb_scr_count_ok,
918 stream->stats.stream.nb_scr_diffs_ok);
919 count += scnprintf(buf + count, size - count,
920 "sof: %u <= sof <= %u, freq %u.%03u kHz\n",
921 stream->stats.stream.min_sof,
922 stream->stats.stream.max_sof,
923 scr_sof_freq / 1000, scr_sof_freq % 1000);
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300924
925 return count;
926}
927
928static void uvc_video_stats_start(struct uvc_streaming *stream)
929{
930 memset(&stream->stats, 0, sizeof(stream->stats));
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300931 stream->stats.stream.min_sof = 2048;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300932}
933
934static void uvc_video_stats_stop(struct uvc_streaming *stream)
935{
936 ktime_get_ts(&stream->stats.stream.stop_ts);
937}
938
939/* ------------------------------------------------------------------------
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300940 * Video codecs
941 */
942
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300943/* Video payload decoding is handled by uvc_video_decode_start(),
944 * uvc_video_decode_data() and uvc_video_decode_end().
945 *
946 * uvc_video_decode_start is called with URB data at the start of a bulk or
947 * isochronous payload. It processes header data and returns the header size
948 * in bytes if successful. If an error occurs, it returns a negative error
949 * code. The following error codes have special meanings.
950 *
951 * - EAGAIN informs the caller that the current video buffer should be marked
952 * as done, and that the function should be called again with the same data
953 * and a new video buffer. This is used when end of frame conditions can be
954 * reliably detected at the beginning of the next frame only.
955 *
956 * If an error other than -EAGAIN is returned, the caller will drop the current
957 * payload. No call to uvc_video_decode_data and uvc_video_decode_end will be
958 * made until the next payload. -ENODATA can be used to drop the current
959 * payload if no other error code is appropriate.
960 *
961 * uvc_video_decode_data is called for every URB with URB data. It copies the
962 * data to the video buffer.
963 *
964 * uvc_video_decode_end is called with header data at the end of a bulk or
965 * isochronous payload. It performs any additional header data processing and
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300966 * returns 0 or a negative error code if an error occurred. As header data have
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300967 * already been processed by uvc_video_decode_start, this functions isn't
968 * required to perform sanity checks a second time.
969 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300970 * For isochronous transfers where a payload is always transferred in a single
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300971 * URB, the three functions will be called in a row.
972 *
973 * To let the decoder process header data and update its internal state even
974 * when no video buffer is available, uvc_video_decode_start must be prepared
975 * to be called with a NULL buf parameter. uvc_video_decode_data and
976 * uvc_video_decode_end will never be called with a NULL buffer.
977 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300978static int uvc_video_decode_start(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300979 struct uvc_buffer *buf, const __u8 *data, int len)
980{
981 __u8 fid;
982
983 /* Sanity checks:
984 * - packet must be at least 2 bytes long
985 * - bHeaderLength value must be at least 2 bytes (see above)
986 * - bHeaderLength value can't be larger than the packet size.
987 */
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300988 if (len < 2 || data[0] < 2 || data[0] > len) {
989 stream->stats.frame.nb_invalid++;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300990 return -EINVAL;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300991 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300992
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300993 fid = data[1] & UVC_STREAM_FID;
994
Laurent Pinchart650b95f2010-10-02 11:06:05 -0300995 /* Increase the sequence number regardless of any buffer states, so
996 * that discontinuous sequence numbers always indicate lost frames.
997 */
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300998 if (stream->last_fid != fid) {
Laurent Pinchart650b95f2010-10-02 11:06:05 -0300999 stream->sequence++;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -03001000 if (stream->sequence)
1001 uvc_video_stats_update(stream);
1002 }
1003
Laurent Pinchart66847ef2011-09-24 10:46:55 -03001004 uvc_video_clock_decode(stream, buf, data, len);
Alexey Fisher7bc5edb2011-11-03 11:40:08 -03001005 uvc_video_stats_decode(stream, data, len);
Laurent Pinchart650b95f2010-10-02 11:06:05 -03001006
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001007 /* Store the payload FID bit and return immediately when the buffer is
1008 * NULL.
1009 */
1010 if (buf == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001011 stream->last_fid = fid;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001012 return -ENODATA;
1013 }
1014
Laurent Pinchart3afedb92011-11-03 07:24:34 -03001015 /* Mark the buffer as bad if the error bit is set. */
1016 if (data[1] & UVC_STREAM_ERR) {
1017 uvc_trace(UVC_TRACE_FRAME, "Marking buffer as bad (error bit "
1018 "set).\n");
1019 buf->error = 1;
1020 }
1021
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001022 /* Synchronize to the input stream by waiting for the FID bit to be
1023 * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE.
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001024 * stream->last_fid is initialized to -1, so the first isochronous
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001025 * frame will always be in sync.
1026 *
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001027 * If the device doesn't toggle the FID bit, invert stream->last_fid
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001028 * when the EOF bit is set to force synchronisation on the next packet.
1029 */
1030 if (buf->state != UVC_BUF_STATE_ACTIVE) {
Laurent Pinchart310fe522009-12-09 22:57:48 -03001031 struct timespec ts;
1032
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001033 if (fid == stream->last_fid) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001034 uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of "
1035 "sync).\n");
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001036 if ((stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) &&
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001037 (data[1] & UVC_STREAM_EOF))
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001038 stream->last_fid ^= UVC_STREAM_FID;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001039 return -ENODATA;
1040 }
1041
Olivier Langlois3b35fc82014-03-28 02:42:38 -03001042 uvc_video_get_ts(&ts);
Laurent Pinchart310fe522009-12-09 22:57:48 -03001043
Junghak Sung2d700712015-09-22 10:30:30 -03001044 buf->buf.field = V4L2_FIELD_NONE;
1045 buf->buf.sequence = stream->sequence;
Junghak Sungd6dd6452015-11-03 08:16:37 -02001046 buf->buf.vb2_buf.timestamp = timespec_to_ns(&ts);
Laurent Pinchart310fe522009-12-09 22:57:48 -03001047
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001048 /* TODO: Handle PTS and SCR. */
1049 buf->state = UVC_BUF_STATE_ACTIVE;
1050 }
1051
1052 /* Mark the buffer as done if we're at the beginning of a new frame.
1053 * End of frame detection is better implemented by checking the EOF
1054 * bit (FID bit toggling is delayed by one frame compared to the EOF
1055 * bit), but some devices don't set the bit at end of frame (and the
1056 * last payload can be lost anyway). We thus must check if the FID has
1057 * been toggled.
1058 *
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001059 * stream->last_fid is initialized to -1, so the first isochronous
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001060 * frame will never trigger an end of frame detection.
1061 *
1062 * Empty buffers (bytesused == 0) don't trigger end of frame detection
1063 * as it doesn't make sense to return an empty buffer. This also
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03001064 * avoids detecting end of frame conditions at FID toggling if the
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001065 * previous payload had the EOF bit set.
1066 */
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001067 if (fid != stream->last_fid && buf->bytesused != 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001068 uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit "
1069 "toggled).\n");
Laurent Pinchartd7c0d432009-12-16 21:20:45 -03001070 buf->state = UVC_BUF_STATE_READY;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001071 return -EAGAIN;
1072 }
1073
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001074 stream->last_fid = fid;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001075
1076 return data[0];
1077}
1078
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001079static void uvc_video_decode_data(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001080 struct uvc_buffer *buf, const __u8 *data, int len)
1081{
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001082 unsigned int maxlen, nbytes;
1083 void *mem;
1084
1085 if (len <= 0)
1086 return;
1087
1088 /* Copy the video data to the buffer. */
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001089 maxlen = buf->length - buf->bytesused;
1090 mem = buf->mem + buf->bytesused;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001091 nbytes = min((unsigned int)len, maxlen);
1092 memcpy(mem, data, nbytes);
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001093 buf->bytesused += nbytes;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001094
1095 /* Complete the current frame if the buffer size was exceeded. */
1096 if (len > maxlen) {
1097 uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n");
Laurent Pinchartd7c0d432009-12-16 21:20:45 -03001098 buf->state = UVC_BUF_STATE_READY;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001099 }
1100}
1101
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001102static void uvc_video_decode_end(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001103 struct uvc_buffer *buf, const __u8 *data, int len)
1104{
1105 /* Mark the buffer as done if the EOF marker is set. */
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001106 if (data[1] & UVC_STREAM_EOF && buf->bytesused != 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001107 uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n");
1108 if (data[0] == len)
1109 uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n");
Laurent Pinchartd7c0d432009-12-16 21:20:45 -03001110 buf->state = UVC_BUF_STATE_READY;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001111 if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID)
1112 stream->last_fid ^= UVC_STREAM_FID;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001113 }
1114}
1115
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03001116/* Video payload encoding is handled by uvc_video_encode_header() and
1117 * uvc_video_encode_data(). Only bulk transfers are currently supported.
1118 *
1119 * uvc_video_encode_header is called at the start of a payload. It adds header
1120 * data to the transfer buffer and returns the header size. As the only known
1121 * UVC output device transfers a whole frame in a single payload, the EOF bit
1122 * is always set in the header.
1123 *
1124 * uvc_video_encode_data is called for every URB and copies the data from the
1125 * video buffer to the transfer buffer.
1126 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001127static int uvc_video_encode_header(struct uvc_streaming *stream,
Laurent Pinchartff924202008-12-28 22:32:29 -03001128 struct uvc_buffer *buf, __u8 *data, int len)
1129{
1130 data[0] = 2; /* Header length */
1131 data[1] = UVC_STREAM_EOH | UVC_STREAM_EOF
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001132 | (stream->last_fid & UVC_STREAM_FID);
Laurent Pinchartff924202008-12-28 22:32:29 -03001133 return 2;
1134}
1135
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001136static int uvc_video_encode_data(struct uvc_streaming *stream,
Laurent Pinchartff924202008-12-28 22:32:29 -03001137 struct uvc_buffer *buf, __u8 *data, int len)
1138{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001139 struct uvc_video_queue *queue = &stream->queue;
Laurent Pinchartff924202008-12-28 22:32:29 -03001140 unsigned int nbytes;
1141 void *mem;
1142
1143 /* Copy video data to the URB buffer. */
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001144 mem = buf->mem + queue->buf_used;
1145 nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001146 nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size,
Laurent Pinchartff924202008-12-28 22:32:29 -03001147 nbytes);
1148 memcpy(data, mem, nbytes);
1149
1150 queue->buf_used += nbytes;
1151
1152 return nbytes;
1153}
1154
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001155/* ------------------------------------------------------------------------
1156 * URB handling
1157 */
1158
1159/*
Anton Leontievf8918ba2014-03-25 01:40:57 -03001160 * Set error flag for incomplete buffer.
1161 */
1162static void uvc_video_validate_buffer(const struct uvc_streaming *stream,
1163 struct uvc_buffer *buf)
1164{
Laurent Pinchartc601f532014-09-30 18:28:42 -03001165 if (stream->ctrl.dwMaxVideoFrameSize != buf->bytesused &&
Anton Leontievf8918ba2014-03-25 01:40:57 -03001166 !(stream->cur_format->flags & UVC_FMT_FLAG_COMPRESSED))
1167 buf->error = 1;
1168}
1169
1170/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001171 * Completion handler for video URBs.
1172 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001173static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
1174 struct uvc_buffer *buf)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001175{
1176 u8 *mem;
1177 int ret, i;
1178
1179 for (i = 0; i < urb->number_of_packets; ++i) {
1180 if (urb->iso_frame_desc[i].status < 0) {
1181 uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
1182 "lost (%d).\n", urb->iso_frame_desc[i].status);
Laurent Pinchart9bde9f22010-06-17 06:52:37 -03001183 /* Mark the buffer as faulty. */
1184 if (buf != NULL)
1185 buf->error = 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001186 continue;
1187 }
1188
1189 /* Decode the payload header. */
1190 mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1191 do {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001192 ret = uvc_video_decode_start(stream, buf, mem,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001193 urb->iso_frame_desc[i].actual_length);
Anton Leontievf8918ba2014-03-25 01:40:57 -03001194 if (ret == -EAGAIN) {
1195 uvc_video_validate_buffer(stream, buf);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001196 buf = uvc_queue_next_buffer(&stream->queue,
1197 buf);
Anton Leontievf8918ba2014-03-25 01:40:57 -03001198 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001199 } while (ret == -EAGAIN);
1200
1201 if (ret < 0)
1202 continue;
1203
1204 /* Decode the payload data. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001205 uvc_video_decode_data(stream, buf, mem + ret,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001206 urb->iso_frame_desc[i].actual_length - ret);
1207
1208 /* Process the header again. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001209 uvc_video_decode_end(stream, buf, mem,
Laurent Pinchart08ebd002008-08-29 17:37:44 -03001210 urb->iso_frame_desc[i].actual_length);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001211
Laurent Pinchart9bde9f22010-06-17 06:52:37 -03001212 if (buf->state == UVC_BUF_STATE_READY) {
Anton Leontievf8918ba2014-03-25 01:40:57 -03001213 uvc_video_validate_buffer(stream, buf);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001214 buf = uvc_queue_next_buffer(&stream->queue, buf);
Laurent Pinchart9bde9f22010-06-17 06:52:37 -03001215 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001216 }
1217}
1218
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001219static void uvc_video_decode_bulk(struct urb *urb, struct uvc_streaming *stream,
1220 struct uvc_buffer *buf)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001221{
1222 u8 *mem;
1223 int len, ret;
1224
Jayakrishnanc854a482012-03-09 10:10:49 -03001225 /*
1226 * Ignore ZLPs if they're not part of a frame, otherwise process them
1227 * to trigger the end of payload detection.
1228 */
1229 if (urb->actual_length == 0 && stream->bulk.header_size == 0)
Laurent Pinchartc90e7772009-02-14 19:39:08 -03001230 return;
1231
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001232 mem = urb->transfer_buffer;
1233 len = urb->actual_length;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001234 stream->bulk.payload_size += len;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001235
1236 /* If the URB is the first of its payload, decode and save the
1237 * header.
1238 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001239 if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001240 do {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001241 ret = uvc_video_decode_start(stream, buf, mem, len);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001242 if (ret == -EAGAIN)
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001243 buf = uvc_queue_next_buffer(&stream->queue,
1244 buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001245 } while (ret == -EAGAIN);
1246
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001247 /* If an error occurred skip the rest of the payload. */
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001248 if (ret < 0 || buf == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001249 stream->bulk.skip_payload = 1;
Laurent Pinchartf8dd4af2008-12-16 10:41:57 -03001250 } else {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001251 memcpy(stream->bulk.header, mem, ret);
1252 stream->bulk.header_size = ret;
Laurent Pinchartf8dd4af2008-12-16 10:41:57 -03001253
1254 mem += ret;
1255 len -= ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001256 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001257 }
1258
1259 /* The buffer queue might have been cancelled while a bulk transfer
1260 * was in progress, so we can reach here with buf equal to NULL. Make
1261 * sure buf is never dereferenced if NULL.
1262 */
1263
1264 /* Process video data. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001265 if (!stream->bulk.skip_payload && buf != NULL)
1266 uvc_video_decode_data(stream, buf, mem, len);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001267
1268 /* Detect the payload end by a URB smaller than the maximum size (or
1269 * a payload size equal to the maximum) and process the header again.
1270 */
1271 if (urb->actual_length < urb->transfer_buffer_length ||
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001272 stream->bulk.payload_size >= stream->bulk.max_payload_size) {
1273 if (!stream->bulk.skip_payload && buf != NULL) {
1274 uvc_video_decode_end(stream, buf, stream->bulk.header,
1275 stream->bulk.payload_size);
Laurent Pinchartd7c0d432009-12-16 21:20:45 -03001276 if (buf->state == UVC_BUF_STATE_READY)
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001277 buf = uvc_queue_next_buffer(&stream->queue,
1278 buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001279 }
1280
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001281 stream->bulk.header_size = 0;
1282 stream->bulk.skip_payload = 0;
1283 stream->bulk.payload_size = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001284 }
1285}
1286
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001287static void uvc_video_encode_bulk(struct urb *urb, struct uvc_streaming *stream,
1288 struct uvc_buffer *buf)
Laurent Pinchartff924202008-12-28 22:32:29 -03001289{
1290 u8 *mem = urb->transfer_buffer;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001291 int len = stream->urb_size, ret;
Laurent Pinchartff924202008-12-28 22:32:29 -03001292
1293 if (buf == NULL) {
1294 urb->transfer_buffer_length = 0;
1295 return;
1296 }
1297
1298 /* If the URB is the first of its payload, add the header. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001299 if (stream->bulk.header_size == 0) {
1300 ret = uvc_video_encode_header(stream, buf, mem, len);
1301 stream->bulk.header_size = ret;
1302 stream->bulk.payload_size += ret;
Laurent Pinchartff924202008-12-28 22:32:29 -03001303 mem += ret;
1304 len -= ret;
1305 }
1306
1307 /* Process video data. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001308 ret = uvc_video_encode_data(stream, buf, mem, len);
Laurent Pinchartff924202008-12-28 22:32:29 -03001309
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001310 stream->bulk.payload_size += ret;
Laurent Pinchartff924202008-12-28 22:32:29 -03001311 len -= ret;
1312
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001313 if (buf->bytesused == stream->queue.buf_used ||
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001314 stream->bulk.payload_size == stream->bulk.max_payload_size) {
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001315 if (buf->bytesused == stream->queue.buf_used) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001316 stream->queue.buf_used = 0;
Laurent Pinchartd7c0d432009-12-16 21:20:45 -03001317 buf->state = UVC_BUF_STATE_READY;
Junghak Sung2d700712015-09-22 10:30:30 -03001318 buf->buf.sequence = ++stream->sequence;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001319 uvc_queue_next_buffer(&stream->queue, buf);
1320 stream->last_fid ^= UVC_STREAM_FID;
Laurent Pinchartff924202008-12-28 22:32:29 -03001321 }
1322
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001323 stream->bulk.header_size = 0;
1324 stream->bulk.payload_size = 0;
Laurent Pinchartff924202008-12-28 22:32:29 -03001325 }
1326
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001327 urb->transfer_buffer_length = stream->urb_size - len;
Laurent Pinchartff924202008-12-28 22:32:29 -03001328}
1329
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001330static void uvc_video_complete(struct urb *urb)
1331{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001332 struct uvc_streaming *stream = urb->context;
1333 struct uvc_video_queue *queue = &stream->queue;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001334 struct uvc_buffer *buf = NULL;
1335 unsigned long flags;
1336 int ret;
1337
1338 switch (urb->status) {
1339 case 0:
1340 break;
1341
1342 default:
1343 uvc_printk(KERN_WARNING, "Non-zero status (%d) in video "
1344 "completion handler.\n", urb->status);
1345
1346 case -ENOENT: /* usb_kill_urb() called. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001347 if (stream->frozen)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001348 return;
1349
1350 case -ECONNRESET: /* usb_unlink_urb() called. */
1351 case -ESHUTDOWN: /* The endpoint is being disabled. */
1352 uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
1353 return;
1354 }
1355
1356 spin_lock_irqsave(&queue->irqlock, flags);
1357 if (!list_empty(&queue->irqqueue))
1358 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
1359 queue);
1360 spin_unlock_irqrestore(&queue->irqlock, flags);
1361
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001362 stream->decode(urb, stream, buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001363
1364 if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
1365 uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n",
1366 ret);
1367 }
1368}
1369
1370/*
Laurent Pincharte01117c2008-07-04 00:36:21 -03001371 * Free transfer buffers.
1372 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001373static void uvc_free_urb_buffers(struct uvc_streaming *stream)
Laurent Pincharte01117c2008-07-04 00:36:21 -03001374{
1375 unsigned int i;
1376
1377 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001378 if (stream->urb_buffer[i]) {
Al Cooper3e0ac672011-08-18 10:28:29 -03001379#ifndef CONFIG_DMA_NONCOHERENT
Daniel Mack997ea582010-04-12 13:17:25 +02001380 usb_free_coherent(stream->dev->udev, stream->urb_size,
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001381 stream->urb_buffer[i], stream->urb_dma[i]);
Al Cooper3e0ac672011-08-18 10:28:29 -03001382#else
1383 kfree(stream->urb_buffer[i]);
1384#endif
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001385 stream->urb_buffer[i] = NULL;
Laurent Pincharte01117c2008-07-04 00:36:21 -03001386 }
1387 }
1388
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001389 stream->urb_size = 0;
Laurent Pincharte01117c2008-07-04 00:36:21 -03001390}
1391
1392/*
1393 * Allocate transfer buffers. This function can be called with buffers
1394 * already allocated when resuming from suspend, in which case it will
1395 * return without touching the buffers.
1396 *
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001397 * Limit the buffer size to UVC_MAX_PACKETS bulk/isochronous packets. If the
1398 * system is too low on memory try successively smaller numbers of packets
1399 * until allocation succeeds.
1400 *
1401 * Return the number of allocated packets on success or 0 when out of memory.
Laurent Pincharte01117c2008-07-04 00:36:21 -03001402 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001403static int uvc_alloc_urb_buffers(struct uvc_streaming *stream,
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001404 unsigned int size, unsigned int psize, gfp_t gfp_flags)
Laurent Pincharte01117c2008-07-04 00:36:21 -03001405{
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001406 unsigned int npackets;
Laurent Pincharte01117c2008-07-04 00:36:21 -03001407 unsigned int i;
1408
1409 /* Buffers are already allocated, bail out. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001410 if (stream->urb_size)
1411 return stream->urb_size / psize;
Laurent Pincharte01117c2008-07-04 00:36:21 -03001412
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001413 /* Compute the number of packets. Bulk endpoints might transfer UVC
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001414 * payloads across multiple URBs.
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001415 */
1416 npackets = DIV_ROUND_UP(size, psize);
1417 if (npackets > UVC_MAX_PACKETS)
1418 npackets = UVC_MAX_PACKETS;
1419
1420 /* Retry allocations until one succeed. */
1421 for (; npackets > 1; npackets /= 2) {
1422 for (i = 0; i < UVC_URBS; ++i) {
Ming Leifd1b6bb2009-09-27 05:30:34 -03001423 stream->urb_size = psize * npackets;
Al Cooper3e0ac672011-08-18 10:28:29 -03001424#ifndef CONFIG_DMA_NONCOHERENT
Daniel Mack997ea582010-04-12 13:17:25 +02001425 stream->urb_buffer[i] = usb_alloc_coherent(
Ming Leifd1b6bb2009-09-27 05:30:34 -03001426 stream->dev->udev, stream->urb_size,
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001427 gfp_flags | __GFP_NOWARN, &stream->urb_dma[i]);
Al Cooper3e0ac672011-08-18 10:28:29 -03001428#else
1429 stream->urb_buffer[i] =
1430 kmalloc(stream->urb_size, gfp_flags | __GFP_NOWARN);
1431#endif
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001432 if (!stream->urb_buffer[i]) {
1433 uvc_free_urb_buffers(stream);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001434 break;
1435 }
1436 }
1437
1438 if (i == UVC_URBS) {
Laurent Pinchart663a4192009-08-06 11:41:17 -03001439 uvc_trace(UVC_TRACE_VIDEO, "Allocated %u URB buffers "
1440 "of %ux%u bytes each.\n", UVC_URBS, npackets,
1441 psize);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001442 return npackets;
Laurent Pincharte01117c2008-07-04 00:36:21 -03001443 }
1444 }
1445
Laurent Pinchart663a4192009-08-06 11:41:17 -03001446 uvc_trace(UVC_TRACE_VIDEO, "Failed to allocate URB buffers (%u bytes "
1447 "per packet).\n", psize);
Laurent Pincharte01117c2008-07-04 00:36:21 -03001448 return 0;
1449}
1450
1451/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001452 * Uninitialize isochronous/bulk URBs and free transfer buffers.
1453 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001454static void uvc_uninit_video(struct uvc_streaming *stream, int free_buffers)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001455{
1456 struct urb *urb;
1457 unsigned int i;
1458
Alexey Fisher7bc5edb2011-11-03 11:40:08 -03001459 uvc_video_stats_stop(stream);
1460
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001461 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001462 urb = stream->urb[i];
1463 if (urb == NULL)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001464 continue;
1465
1466 usb_kill_urb(urb);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001467 usb_free_urb(urb);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001468 stream->urb[i] = NULL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001469 }
Laurent Pincharte01117c2008-07-04 00:36:21 -03001470
1471 if (free_buffers)
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001472 uvc_free_urb_buffers(stream);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001473}
1474
1475/*
Laurent Pinchart6fd90db2012-07-17 08:58:48 -03001476 * Compute the maximum number of bytes per interval for an endpoint.
1477 */
1478static unsigned int uvc_endpoint_max_bpi(struct usb_device *dev,
1479 struct usb_host_endpoint *ep)
1480{
1481 u16 psize;
1482
1483 switch (dev->speed) {
1484 case USB_SPEED_SUPER:
Oliver Neukum92a63452016-05-02 08:23:21 -03001485 case USB_SPEED_SUPER_PLUS:
Hans Verkuild71b0b32014-08-20 17:58:38 -03001486 return le16_to_cpu(ep->ss_ep_comp.wBytesPerInterval);
Laurent Pinchart6fd90db2012-07-17 08:58:48 -03001487 case USB_SPEED_HIGH:
1488 psize = usb_endpoint_maxp(&ep->desc);
1489 return (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
Thomas Pugliese79af67e2014-01-24 18:17:28 -03001490 case USB_SPEED_WIRELESS:
1491 psize = usb_endpoint_maxp(&ep->desc);
1492 return psize;
Laurent Pinchart6fd90db2012-07-17 08:58:48 -03001493 default:
1494 psize = usb_endpoint_maxp(&ep->desc);
1495 return psize & 0x07ff;
1496 }
1497}
1498
1499/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001500 * Initialize isochronous URBs and allocate transfer buffers. The packet size
1501 * is given by the endpoint.
1502 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001503static int uvc_init_video_isoc(struct uvc_streaming *stream,
Laurent Pinchart29135872008-07-04 00:35:26 -03001504 struct usb_host_endpoint *ep, gfp_t gfp_flags)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001505{
1506 struct urb *urb;
1507 unsigned int npackets, i, j;
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001508 u16 psize;
1509 u32 size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001510
Laurent Pinchart6fd90db2012-07-17 08:58:48 -03001511 psize = uvc_endpoint_max_bpi(stream->dev->udev, ep);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001512 size = stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001513
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001514 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001515 if (npackets == 0)
1516 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001517
1518 size = npackets * psize;
1519
1520 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart29135872008-07-04 00:35:26 -03001521 urb = usb_alloc_urb(npackets, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001522 if (urb == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001523 uvc_uninit_video(stream, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001524 return -ENOMEM;
1525 }
1526
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001527 urb->dev = stream->dev->udev;
1528 urb->context = stream;
1529 urb->pipe = usb_rcvisocpipe(stream->dev->udev,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001530 ep->desc.bEndpointAddress);
Al Cooper3e0ac672011-08-18 10:28:29 -03001531#ifndef CONFIG_DMA_NONCOHERENT
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001532 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Al Cooper3e0ac672011-08-18 10:28:29 -03001533 urb->transfer_dma = stream->urb_dma[i];
1534#else
1535 urb->transfer_flags = URB_ISO_ASAP;
1536#endif
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001537 urb->interval = ep->desc.bInterval;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001538 urb->transfer_buffer = stream->urb_buffer[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001539 urb->complete = uvc_video_complete;
1540 urb->number_of_packets = npackets;
1541 urb->transfer_buffer_length = size;
1542
1543 for (j = 0; j < npackets; ++j) {
1544 urb->iso_frame_desc[j].offset = j * psize;
1545 urb->iso_frame_desc[j].length = psize;
1546 }
1547
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001548 stream->urb[i] = urb;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001549 }
1550
1551 return 0;
1552}
1553
1554/*
1555 * Initialize bulk URBs and allocate transfer buffers. The packet size is
1556 * given by the endpoint.
1557 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001558static int uvc_init_video_bulk(struct uvc_streaming *stream,
Laurent Pinchart29135872008-07-04 00:35:26 -03001559 struct usb_host_endpoint *ep, gfp_t gfp_flags)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001560{
1561 struct urb *urb;
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001562 unsigned int npackets, pipe, i;
1563 u16 psize;
1564 u32 size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001565
Laurent Pinchart6fd90db2012-07-17 08:58:48 -03001566 psize = usb_endpoint_maxp(&ep->desc) & 0x7ff;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001567 size = stream->ctrl.dwMaxPayloadTransferSize;
1568 stream->bulk.max_payload_size = size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001569
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001570 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001571 if (npackets == 0)
Laurent Pincharte01117c2008-07-04 00:36:21 -03001572 return -ENOMEM;
1573
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001574 size = npackets * psize;
1575
Laurent Pinchartff924202008-12-28 22:32:29 -03001576 if (usb_endpoint_dir_in(&ep->desc))
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001577 pipe = usb_rcvbulkpipe(stream->dev->udev,
Laurent Pinchartff924202008-12-28 22:32:29 -03001578 ep->desc.bEndpointAddress);
1579 else
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001580 pipe = usb_sndbulkpipe(stream->dev->udev,
Laurent Pinchartff924202008-12-28 22:32:29 -03001581 ep->desc.bEndpointAddress);
1582
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001583 if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
Laurent Pinchartff924202008-12-28 22:32:29 -03001584 size = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001585
1586 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart29135872008-07-04 00:35:26 -03001587 urb = usb_alloc_urb(0, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001588 if (urb == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001589 uvc_uninit_video(stream, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001590 return -ENOMEM;
1591 }
1592
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001593 usb_fill_bulk_urb(urb, stream->dev->udev, pipe,
1594 stream->urb_buffer[i], size, uvc_video_complete,
1595 stream);
Al Cooper3e0ac672011-08-18 10:28:29 -03001596#ifndef CONFIG_DMA_NONCOHERENT
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001597 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001598 urb->transfer_dma = stream->urb_dma[i];
Al Cooper3e0ac672011-08-18 10:28:29 -03001599#endif
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001600
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001601 stream->urb[i] = urb;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001602 }
1603
1604 return 0;
1605}
1606
1607/*
1608 * Initialize isochronous/bulk URBs and allocate transfer buffers.
1609 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001610static int uvc_init_video(struct uvc_streaming *stream, gfp_t gfp_flags)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001611{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001612 struct usb_interface *intf = stream->intf;
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001613 struct usb_host_endpoint *ep;
1614 unsigned int i;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001615 int ret;
1616
Laurent Pinchart650b95f2010-10-02 11:06:05 -03001617 stream->sequence = -1;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001618 stream->last_fid = -1;
1619 stream->bulk.header_size = 0;
1620 stream->bulk.skip_payload = 0;
1621 stream->bulk.payload_size = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001622
Alexey Fisher7bc5edb2011-11-03 11:40:08 -03001623 uvc_video_stats_start(stream);
1624
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001625 if (intf->num_altsetting > 1) {
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001626 struct usb_host_endpoint *best_ep = NULL;
Laurent Pinchart6fd90db2012-07-17 08:58:48 -03001627 unsigned int best_psize = UINT_MAX;
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001628 unsigned int bandwidth;
1629 unsigned int uninitialized_var(altsetting);
1630 int intfnum = stream->intfnum;
1631
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001632 /* Isochronous endpoint, select the alternate setting. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001633 bandwidth = stream->ctrl.dwMaxPayloadTransferSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001634
1635 if (bandwidth == 0) {
Laurent Pinchart663a4192009-08-06 11:41:17 -03001636 uvc_trace(UVC_TRACE_VIDEO, "Device requested null "
1637 "bandwidth, defaulting to lowest.\n");
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001638 bandwidth = 1;
Laurent Pinchart663a4192009-08-06 11:41:17 -03001639 } else {
1640 uvc_trace(UVC_TRACE_VIDEO, "Device requested %u "
1641 "B/frame bandwidth.\n", bandwidth);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001642 }
1643
1644 for (i = 0; i < intf->num_altsetting; ++i) {
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001645 struct usb_host_interface *alts;
1646 unsigned int psize;
1647
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001648 alts = &intf->altsetting[i];
1649 ep = uvc_find_endpoint(alts,
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001650 stream->header.bEndpointAddress);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001651 if (ep == NULL)
1652 continue;
1653
1654 /* Check if the bandwidth is high enough. */
Laurent Pinchart6fd90db2012-07-17 08:58:48 -03001655 psize = uvc_endpoint_max_bpi(stream->dev->udev, ep);
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001656 if (psize >= bandwidth && psize <= best_psize) {
Laurent Pinchart48070632012-06-21 06:35:04 -03001657 altsetting = alts->desc.bAlternateSetting;
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001658 best_psize = psize;
1659 best_ep = ep;
1660 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001661 }
1662
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001663 if (best_ep == NULL) {
Laurent Pinchart663a4192009-08-06 11:41:17 -03001664 uvc_trace(UVC_TRACE_VIDEO, "No fast enough alt setting "
1665 "for requested bandwidth.\n");
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001666 return -EIO;
Laurent Pinchart663a4192009-08-06 11:41:17 -03001667 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001668
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001669 uvc_trace(UVC_TRACE_VIDEO, "Selecting alternate setting %u "
1670 "(%u B/frame bandwidth).\n", altsetting, best_psize);
1671
1672 ret = usb_set_interface(stream->dev->udev, intfnum, altsetting);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001673 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001674 return ret;
1675
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001676 ret = uvc_init_video_isoc(stream, best_ep, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001677 } else {
1678 /* Bulk endpoint, proceed to URB initialization. */
1679 ep = uvc_find_endpoint(&intf->altsetting[0],
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001680 stream->header.bEndpointAddress);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001681 if (ep == NULL)
1682 return -EIO;
1683
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001684 ret = uvc_init_video_bulk(stream, ep, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001685 }
1686
1687 if (ret < 0)
1688 return ret;
1689
1690 /* Submit the URBs. */
1691 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001692 ret = usb_submit_urb(stream->urb[i], gfp_flags);
1693 if (ret < 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001694 uvc_printk(KERN_ERR, "Failed to submit URB %u "
1695 "(%d).\n", i, ret);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001696 uvc_uninit_video(stream, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001697 return ret;
1698 }
1699 }
1700
William Manley17e13192014-03-13 09:38:48 -03001701 /* The Logitech C920 temporarily forgets that it should not be adjusting
1702 * Exposure Absolute during init so restore controls to stored values.
1703 */
1704 if (stream->dev->quirks & UVC_QUIRK_RESTORE_CTRLS_ON_INIT)
1705 uvc_ctrl_restore_values(stream->dev);
1706
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001707 return 0;
1708}
1709
1710/* --------------------------------------------------------------------------
1711 * Suspend/resume
1712 */
1713
1714/*
1715 * Stop streaming without disabling the video queue.
1716 *
1717 * To let userspace applications resume without trouble, we must not touch the
1718 * video buffers in any way. We mark the device as frozen to make sure the URB
1719 * completion handler won't try to cancel the queue when we kill the URBs.
1720 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001721int uvc_video_suspend(struct uvc_streaming *stream)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001722{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001723 if (!uvc_queue_streaming(&stream->queue))
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001724 return 0;
1725
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001726 stream->frozen = 1;
1727 uvc_uninit_video(stream, 0);
1728 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001729 return 0;
1730}
1731
1732/*
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03001733 * Reconfigure the video interface and restart streaming if it was enabled
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001734 * before suspend.
1735 *
1736 * If an error occurs, disable the video queue. This will wake all pending
1737 * buffers, making sure userspace applications are notified of the problem
1738 * instead of waiting forever.
1739 */
Ming Leid59a7b12011-07-16 00:51:00 -03001740int uvc_video_resume(struct uvc_streaming *stream, int reset)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001741{
1742 int ret;
1743
Ming Leid59a7b12011-07-16 00:51:00 -03001744 /* If the bus has been reset on resume, set the alternate setting to 0.
1745 * This should be the default value, but some devices crash or otherwise
1746 * misbehave if they don't receive a SET_INTERFACE request before any
1747 * other video control request.
1748 */
1749 if (reset)
1750 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1751
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001752 stream->frozen = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001753
Laurent Pincharted0ee0c2012-03-27 05:51:00 -03001754 uvc_video_clock_reset(stream);
1755
Aviv Greenberg9fae30a2014-09-02 03:16:28 -03001756 if (!uvc_queue_streaming(&stream->queue))
1757 return 0;
1758
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001759 ret = uvc_commit_video(stream, &stream->ctrl);
Laurent Pinchartb83bba22014-10-13 10:11:35 -03001760 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001761 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001762
Laurent Pinchartb83bba22014-10-13 10:11:35 -03001763 return uvc_init_video(stream, GFP_NOIO);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001764}
1765
1766/* ------------------------------------------------------------------------
1767 * Video device
1768 */
1769
1770/*
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03001771 * Initialize the UVC video device by switching to alternate setting 0 and
1772 * retrieve the default format.
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001773 *
1774 * Some cameras (namely the Fuji Finepix) set the format and frame
1775 * indexes to zero. The UVC standard doesn't clearly make this a spec
1776 * violation, so try to silently fix the values if possible.
1777 *
1778 * This function is called before registering the device with V4L.
1779 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001780int uvc_video_init(struct uvc_streaming *stream)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001781{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001782 struct uvc_streaming_control *probe = &stream->ctrl;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001783 struct uvc_format *format = NULL;
1784 struct uvc_frame *frame = NULL;
1785 unsigned int i;
1786 int ret;
1787
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001788 if (stream->nformats == 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001789 uvc_printk(KERN_INFO, "No supported video formats found.\n");
1790 return -EINVAL;
1791 }
1792
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001793 atomic_set(&stream->active, 0);
1794
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001795 /* Alternate setting 0 should be the default, yet the XBox Live Vision
1796 * Cam (and possibly other devices) crash or otherwise misbehave if
1797 * they don't receive a SET_INTERFACE request before any other video
1798 * control request.
1799 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001800 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001801
Laurent Pinchart72362422009-02-14 19:26:56 -03001802 /* Set the streaming probe control with default streaming parameters
1803 * retrieved from the device. Webcams that don't suport GET_DEF
1804 * requests on the probe control will just keep their current streaming
1805 * parameters.
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001806 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001807 if (uvc_get_video_ctrl(stream, probe, 1, UVC_GET_DEF) == 0)
1808 uvc_set_video_ctrl(stream, probe, 1);
Laurent Pinchart72362422009-02-14 19:26:56 -03001809
1810 /* Initialize the streaming parameters with the probe control current
1811 * value. This makes sure SET_CUR requests on the streaming commit
1812 * control will always use values retrieved from a successful GET_CUR
1813 * request on the probe control, as required by the UVC specification.
1814 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001815 ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
Laurent Pinchartb482d922009-06-26 11:39:42 -03001816 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001817 return ret;
1818
1819 /* Check if the default format descriptor exists. Use the first
1820 * available format otherwise.
1821 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001822 for (i = stream->nformats; i > 0; --i) {
1823 format = &stream->format[i-1];
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001824 if (format->index == probe->bFormatIndex)
1825 break;
1826 }
1827
1828 if (format->nframes == 0) {
1829 uvc_printk(KERN_INFO, "No frame descriptor found for the "
1830 "default format.\n");
1831 return -EINVAL;
1832 }
1833
1834 /* Zero bFrameIndex might be correct. Stream-based formats (including
1835 * MPEG-2 TS and DV) do not support frames but have a dummy frame
1836 * descriptor with bFrameIndex set to zero. If the default frame
Laurent Pinchart078f8942009-05-06 12:30:30 -03001837 * descriptor is not found, use the first available frame.
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001838 */
1839 for (i = format->nframes; i > 0; --i) {
1840 frame = &format->frame[i-1];
1841 if (frame->bFrameIndex == probe->bFrameIndex)
1842 break;
1843 }
1844
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001845 probe->bFormatIndex = format->index;
1846 probe->bFrameIndex = frame->bFrameIndex;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001847
Laurent Pinchart815adc42012-08-28 18:38:58 -03001848 stream->def_format = format;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001849 stream->cur_format = format;
1850 stream->cur_frame = frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001851
1852 /* Select the video decoding function */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001853 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1854 if (stream->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
1855 stream->decode = uvc_video_decode_isight;
1856 else if (stream->intf->num_altsetting > 1)
1857 stream->decode = uvc_video_decode_isoc;
Laurent Pinchartff924202008-12-28 22:32:29 -03001858 else
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001859 stream->decode = uvc_video_decode_bulk;
Laurent Pinchartff924202008-12-28 22:32:29 -03001860 } else {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001861 if (stream->intf->num_altsetting == 1)
1862 stream->decode = uvc_video_encode_bulk;
Laurent Pinchartff924202008-12-28 22:32:29 -03001863 else {
1864 uvc_printk(KERN_INFO, "Isochronous endpoints are not "
1865 "supported for video output devices.\n");
1866 return -EINVAL;
1867 }
1868 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001869
1870 return 0;
1871}
1872
1873/*
1874 * Enable or disable the video stream.
1875 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001876int uvc_video_enable(struct uvc_streaming *stream, int enable)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001877{
1878 int ret;
1879
1880 if (!enable) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001881 uvc_uninit_video(stream, 1);
Oleksij Rempelb1e43f22014-02-16 06:59:32 -03001882 if (stream->intf->num_altsetting > 1) {
1883 usb_set_interface(stream->dev->udev,
1884 stream->intfnum, 0);
1885 } else {
1886 /* UVC doesn't specify how to inform a bulk-based device
1887 * when the video stream is stopped. Windows sends a
1888 * CLEAR_FEATURE(HALT) request to the video streaming
1889 * bulk endpoint, mimic the same behaviour.
1890 */
1891 unsigned int epnum = stream->header.bEndpointAddress
1892 & USB_ENDPOINT_NUMBER_MASK;
1893 unsigned int dir = stream->header.bEndpointAddress
1894 & USB_ENDPOINT_DIR_MASK;
1895 unsigned int pipe;
1896
1897 pipe = usb_sndbulkpipe(stream->dev->udev, epnum) | dir;
1898 usb_clear_halt(stream->dev->udev, pipe);
1899 }
1900
Laurent Pincharted0ee0c2012-03-27 05:51:00 -03001901 uvc_video_clock_cleanup(stream);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001902 return 0;
1903 }
1904
Laurent Pincharted0ee0c2012-03-27 05:51:00 -03001905 ret = uvc_video_clock_init(stream);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001906 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001907 return ret;
1908
Laurent Pinchart23867b22008-11-12 11:46:43 -03001909 /* Commit the streaming parameters. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001910 ret = uvc_commit_video(stream, &stream->ctrl);
Laurent Pincharted0ee0c2012-03-27 05:51:00 -03001911 if (ret < 0)
1912 goto error_commit;
Laurent Pinchart23867b22008-11-12 11:46:43 -03001913
Laurent Pinchart24c3aae2011-10-25 09:03:42 -03001914 ret = uvc_init_video(stream, GFP_KERNEL);
Laurent Pincharted0ee0c2012-03-27 05:51:00 -03001915 if (ret < 0)
1916 goto error_video;
1917
1918 return 0;
1919
1920error_video:
1921 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1922error_commit:
Laurent Pincharted0ee0c2012-03-27 05:51:00 -03001923 uvc_video_clock_cleanup(stream);
Hans Verkuilf87086e2008-07-18 00:50:58 -03001924
Laurent Pinchart24c3aae2011-10-25 09:03:42 -03001925 return ret;
1926}