blob: 1c4a11766fd1025700a0f7a45f4a53869a6b6641 [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
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300166static int uvc_get_video_ctrl(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300167 struct uvc_streaming_control *ctrl, int probe, __u8 query)
168{
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300169 __u8 *data;
170 __u16 size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300171 int ret;
172
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300173 size = stream->dev->uvc_version >= 0x0110 ? 34 : 26;
Julia Lawalld2be7642009-09-17 23:44:01 -0300174 if ((stream->dev->quirks & UVC_QUIRK_PROBE_DEF) &&
175 query == UVC_GET_DEF)
176 return -EIO;
177
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300178 data = kmalloc(size, GFP_KERNEL);
179 if (data == NULL)
180 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300181
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300182 ret = __uvc_query_ctrl(stream->dev, query, 0, stream->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300183 probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
Laurent Pinchartb232a012009-10-09 20:55:23 -0300184 size, uvc_timeout_param);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300185
Laurent Pinchartb482d922009-06-26 11:39:42 -0300186 if ((query == UVC_GET_MIN || query == UVC_GET_MAX) && ret == 2) {
Laurent Pinchart44f00792008-11-08 19:14:50 -0300187 /* Some cameras, mostly based on Bison Electronics chipsets,
188 * answer a GET_MIN or GET_MAX request with the wCompQuality
189 * field only.
190 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300191 uvc_warn_once(stream->dev, UVC_WARN_MINMAX, "UVC non "
Laurent Pinchart44f00792008-11-08 19:14:50 -0300192 "compliance - GET_MIN/MAX(PROBE) incorrectly "
193 "supported. Enabling workaround.\n");
Julia Lawallddb0b342009-12-10 17:14:27 -0300194 memset(ctrl, 0, sizeof *ctrl);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300195 ctrl->wCompQuality = le16_to_cpup((__le16 *)data);
196 ret = 0;
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300197 goto out;
Laurent Pinchartb482d922009-06-26 11:39:42 -0300198 } else if (query == UVC_GET_DEF && probe == 1 && ret != size) {
Laurent Pinchart44f00792008-11-08 19:14:50 -0300199 /* Many cameras don't support the GET_DEF request on their
200 * video probe control. Warn once and return, the caller will
201 * fall back to GET_CUR.
202 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300203 uvc_warn_once(stream->dev, UVC_WARN_PROBE_DEF, "UVC non "
Laurent Pinchart44f00792008-11-08 19:14:50 -0300204 "compliance - GET_DEF(PROBE) not supported. "
205 "Enabling workaround.\n");
206 ret = -EIO;
207 goto out;
208 } else if (ret != size) {
209 uvc_printk(KERN_ERR, "Failed to query (%u) UVC %s control : "
210 "%d (exp. %u).\n", query, probe ? "probe" : "commit",
211 ret, size);
212 ret = -EIO;
213 goto out;
214 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300215
216 ctrl->bmHint = le16_to_cpup((__le16 *)&data[0]);
217 ctrl->bFormatIndex = data[2];
218 ctrl->bFrameIndex = data[3];
219 ctrl->dwFrameInterval = le32_to_cpup((__le32 *)&data[4]);
220 ctrl->wKeyFrameRate = le16_to_cpup((__le16 *)&data[8]);
221 ctrl->wPFrameRate = le16_to_cpup((__le16 *)&data[10]);
222 ctrl->wCompQuality = le16_to_cpup((__le16 *)&data[12]);
223 ctrl->wCompWindowSize = le16_to_cpup((__le16 *)&data[14]);
224 ctrl->wDelay = le16_to_cpup((__le16 *)&data[16]);
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300225 ctrl->dwMaxVideoFrameSize = get_unaligned_le32(&data[18]);
226 ctrl->dwMaxPayloadTransferSize = get_unaligned_le32(&data[22]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300227
228 if (size == 34) {
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300229 ctrl->dwClockFrequency = get_unaligned_le32(&data[26]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300230 ctrl->bmFramingInfo = data[30];
231 ctrl->bPreferedVersion = data[31];
232 ctrl->bMinVersion = data[32];
233 ctrl->bMaxVersion = data[33];
234 } else {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300235 ctrl->dwClockFrequency = stream->dev->clock_frequency;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300236 ctrl->bmFramingInfo = 0;
237 ctrl->bPreferedVersion = 0;
238 ctrl->bMinVersion = 0;
239 ctrl->bMaxVersion = 0;
240 }
241
Laurent Pinchart50144ae2009-02-16 17:41:52 -0300242 /* Some broken devices return null or wrong dwMaxVideoFrameSize and
243 * dwMaxPayloadTransferSize fields. Try to get the value from the
244 * format and frame descriptors.
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300245 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300246 uvc_fixup_video_ctrl(stream, ctrl);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300247 ret = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300248
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300249out:
250 kfree(data);
251 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300252}
253
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300254static int uvc_set_video_ctrl(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300255 struct uvc_streaming_control *ctrl, int probe)
256{
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300257 __u8 *data;
258 __u16 size;
259 int ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300260
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300261 size = stream->dev->uvc_version >= 0x0110 ? 34 : 26;
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300262 data = kzalloc(size, GFP_KERNEL);
263 if (data == NULL)
264 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300265
266 *(__le16 *)&data[0] = cpu_to_le16(ctrl->bmHint);
267 data[2] = ctrl->bFormatIndex;
268 data[3] = ctrl->bFrameIndex;
269 *(__le32 *)&data[4] = cpu_to_le32(ctrl->dwFrameInterval);
270 *(__le16 *)&data[8] = cpu_to_le16(ctrl->wKeyFrameRate);
271 *(__le16 *)&data[10] = cpu_to_le16(ctrl->wPFrameRate);
272 *(__le16 *)&data[12] = cpu_to_le16(ctrl->wCompQuality);
273 *(__le16 *)&data[14] = cpu_to_le16(ctrl->wCompWindowSize);
274 *(__le16 *)&data[16] = cpu_to_le16(ctrl->wDelay);
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300275 put_unaligned_le32(ctrl->dwMaxVideoFrameSize, &data[18]);
276 put_unaligned_le32(ctrl->dwMaxPayloadTransferSize, &data[22]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300277
278 if (size == 34) {
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300279 put_unaligned_le32(ctrl->dwClockFrequency, &data[26]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300280 data[30] = ctrl->bmFramingInfo;
281 data[31] = ctrl->bPreferedVersion;
282 data[32] = ctrl->bMinVersion;
283 data[33] = ctrl->bMaxVersion;
284 }
285
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300286 ret = __uvc_query_ctrl(stream->dev, UVC_SET_CUR, 0, stream->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300287 probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
Laurent Pinchartb232a012009-10-09 20:55:23 -0300288 size, uvc_timeout_param);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300289 if (ret != size) {
290 uvc_printk(KERN_ERR, "Failed to set UVC %s control : "
291 "%d (exp. %u).\n", probe ? "probe" : "commit",
292 ret, size);
293 ret = -EIO;
294 }
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300295
296 kfree(data);
297 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300298}
299
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300300int uvc_probe_video(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300301 struct uvc_streaming_control *probe)
302{
303 struct uvc_streaming_control probe_min, probe_max;
304 __u16 bandwidth;
305 unsigned int i;
306 int ret;
307
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300308 /* Perform probing. The device should adjust the requested values
309 * according to its capabilities. However, some devices, namely the
310 * first generation UVC Logitech webcams, don't implement the Video
311 * Probe control properly, and just return the needed bandwidth. For
312 * that reason, if the needed bandwidth exceeds the maximum available
313 * bandwidth, try to lower the quality.
314 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300315 ret = uvc_set_video_ctrl(stream, probe, 1);
316 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300317 goto done;
318
319 /* Get the minimum and maximum values for compression settings. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300320 if (!(stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX)) {
321 ret = uvc_get_video_ctrl(stream, &probe_min, 1, UVC_GET_MIN);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300322 if (ret < 0)
323 goto done;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300324 ret = uvc_get_video_ctrl(stream, &probe_max, 1, UVC_GET_MAX);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300325 if (ret < 0)
326 goto done;
327
328 probe->wCompQuality = probe_max.wCompQuality;
329 }
330
331 for (i = 0; i < 2; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300332 ret = uvc_set_video_ctrl(stream, probe, 1);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300333 if (ret < 0)
334 goto done;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300335 ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300336 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300337 goto done;
338
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300339 if (stream->intf->num_altsetting == 1)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300340 break;
341
342 bandwidth = probe->dwMaxPayloadTransferSize;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300343 if (bandwidth <= stream->maxpsize)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300344 break;
345
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300346 if (stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300347 ret = -ENOSPC;
348 goto done;
349 }
350
351 /* TODO: negotiate compression parameters */
352 probe->wKeyFrameRate = probe_min.wKeyFrameRate;
353 probe->wPFrameRate = probe_min.wPFrameRate;
354 probe->wCompQuality = probe_max.wCompQuality;
355 probe->wCompWindowSize = probe_min.wCompWindowSize;
356 }
357
358done:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300359 return ret;
360}
361
Laurent Pinchart0c6a3b22011-11-03 07:22:39 -0300362static int uvc_commit_video(struct uvc_streaming *stream,
363 struct uvc_streaming_control *probe)
Laurent Pinchart44f00792008-11-08 19:14:50 -0300364{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300365 return uvc_set_video_ctrl(stream, probe, 0);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300366}
367
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300368/* -----------------------------------------------------------------------------
369 * Clocks and timestamps
370 */
371
Olivier Langlois3b35fc82014-03-28 02:42:38 -0300372static inline void uvc_video_get_ts(struct timespec *ts)
373{
374 if (uvc_clock_param == CLOCK_MONOTONIC)
375 ktime_get_ts(ts);
376 else
377 ktime_get_real_ts(ts);
378}
379
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300380static void
381uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf,
382 const __u8 *data, int len)
383{
384 struct uvc_clock_sample *sample;
385 unsigned int header_size;
386 bool has_pts = false;
387 bool has_scr = false;
388 unsigned long flags;
389 struct timespec ts;
390 u16 host_sof;
391 u16 dev_sof;
392
393 switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) {
394 case UVC_STREAM_PTS | UVC_STREAM_SCR:
395 header_size = 12;
396 has_pts = true;
397 has_scr = true;
398 break;
399 case UVC_STREAM_PTS:
400 header_size = 6;
401 has_pts = true;
402 break;
403 case UVC_STREAM_SCR:
404 header_size = 8;
405 has_scr = true;
406 break;
407 default:
408 header_size = 2;
409 break;
410 }
411
412 /* Check for invalid headers. */
413 if (len < header_size)
414 return;
415
416 /* Extract the timestamps:
417 *
418 * - store the frame PTS in the buffer structure
419 * - if the SCR field is present, retrieve the host SOF counter and
420 * kernel timestamps and store them with the SCR STC and SOF fields
421 * in the ring buffer
422 */
423 if (has_pts && buf != NULL)
424 buf->pts = get_unaligned_le32(&data[2]);
425
426 if (!has_scr)
427 return;
428
429 /* To limit the amount of data, drop SCRs with an SOF identical to the
430 * previous one.
431 */
432 dev_sof = get_unaligned_le16(&data[header_size - 2]);
433 if (dev_sof == stream->clock.last_sof)
434 return;
435
436 stream->clock.last_sof = dev_sof;
437
438 host_sof = usb_get_current_frame_number(stream->dev->udev);
Olivier Langlois3b35fc82014-03-28 02:42:38 -0300439 uvc_video_get_ts(&ts);
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300440
441 /* The UVC specification allows device implementations that can't obtain
442 * the USB frame number to keep their own frame counters as long as they
443 * match the size and frequency of the frame number associated with USB
444 * SOF tokens. The SOF values sent by such devices differ from the USB
445 * SOF tokens by a fixed offset that needs to be estimated and accounted
446 * for to make timestamp recovery as accurate as possible.
447 *
448 * The offset is estimated the first time a device SOF value is received
449 * as the difference between the host and device SOF values. As the two
450 * SOF values can differ slightly due to transmission delays, consider
451 * that the offset is null if the difference is not higher than 10 ms
452 * (negative differences can not happen and are thus considered as an
453 * offset). The video commit control wDelay field should be used to
454 * compute a dynamic threshold instead of using a fixed 10 ms value, but
455 * devices don't report reliable wDelay values.
456 *
457 * See uvc_video_clock_host_sof() for an explanation regarding why only
458 * the 8 LSBs of the delta are kept.
459 */
460 if (stream->clock.sof_offset == (u16)-1) {
461 u16 delta_sof = (host_sof - dev_sof) & 255;
462 if (delta_sof >= 10)
463 stream->clock.sof_offset = delta_sof;
464 else
465 stream->clock.sof_offset = 0;
466 }
467
468 dev_sof = (dev_sof + stream->clock.sof_offset) & 2047;
469
470 spin_lock_irqsave(&stream->clock.lock, flags);
471
472 sample = &stream->clock.samples[stream->clock.head];
473 sample->dev_stc = get_unaligned_le32(&data[header_size - 6]);
474 sample->dev_sof = dev_sof;
475 sample->host_sof = host_sof;
476 sample->host_ts = ts;
477
478 /* Update the sliding window head and count. */
479 stream->clock.head = (stream->clock.head + 1) % stream->clock.size;
480
481 if (stream->clock.count < stream->clock.size)
482 stream->clock.count++;
483
484 spin_unlock_irqrestore(&stream->clock.lock, flags);
485}
486
Laurent Pincharted0ee0c2012-03-27 05:51:00 -0300487static void uvc_video_clock_reset(struct uvc_streaming *stream)
488{
489 struct uvc_clock *clock = &stream->clock;
490
491 clock->head = 0;
492 clock->count = 0;
493 clock->last_sof = -1;
494 clock->sof_offset = -1;
495}
496
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300497static int uvc_video_clock_init(struct uvc_streaming *stream)
498{
499 struct uvc_clock *clock = &stream->clock;
500
501 spin_lock_init(&clock->lock);
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300502 clock->size = 32;
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300503
504 clock->samples = kmalloc(clock->size * sizeof(*clock->samples),
505 GFP_KERNEL);
506 if (clock->samples == NULL)
507 return -ENOMEM;
508
Laurent Pincharted0ee0c2012-03-27 05:51:00 -0300509 uvc_video_clock_reset(stream);
510
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300511 return 0;
512}
513
514static void uvc_video_clock_cleanup(struct uvc_streaming *stream)
515{
516 kfree(stream->clock.samples);
517 stream->clock.samples = NULL;
518}
519
520/*
521 * uvc_video_clock_host_sof - Return the host SOF value for a clock sample
522 *
523 * Host SOF counters reported by usb_get_current_frame_number() usually don't
524 * cover the whole 11-bits SOF range (0-2047) but are limited to the HCI frame
525 * schedule window. They can be limited to 8, 9 or 10 bits depending on the host
526 * controller and its configuration.
527 *
528 * We thus need to recover the SOF value corresponding to the host frame number.
529 * As the device and host frame numbers are sampled in a short interval, the
530 * difference between their values should be equal to a small delta plus an
531 * integer multiple of 256 caused by the host frame number limited precision.
532 *
533 * To obtain the recovered host SOF value, compute the small delta by masking
534 * the high bits of the host frame counter and device SOF difference and add it
535 * to the device SOF value.
536 */
537static u16 uvc_video_clock_host_sof(const struct uvc_clock_sample *sample)
538{
539 /* The delta value can be negative. */
540 s8 delta_sof;
541
542 delta_sof = (sample->host_sof - sample->dev_sof) & 255;
543
544 return (sample->dev_sof + delta_sof) & 2047;
545}
546
547/*
548 * uvc_video_clock_update - Update the buffer timestamp
549 *
550 * This function converts the buffer PTS timestamp to the host clock domain by
551 * going through the USB SOF clock domain and stores the result in the V4L2
552 * buffer timestamp field.
553 *
554 * The relationship between the device clock and the host clock isn't known.
555 * However, the device and the host share the common USB SOF clock which can be
556 * used to recover that relationship.
557 *
558 * The relationship between the device clock and the USB SOF clock is considered
559 * to be linear over the clock samples sliding window and is given by
560 *
561 * SOF = m * PTS + p
562 *
563 * Several methods to compute the slope (m) and intercept (p) can be used. As
564 * the clock drift should be small compared to the sliding window size, we
565 * assume that the line that goes through the points at both ends of the window
566 * is a good approximation. Naming those points P1 and P2, we get
567 *
568 * SOF = (SOF2 - SOF1) / (STC2 - STC1) * PTS
569 * + (SOF1 * STC2 - SOF2 * STC1) / (STC2 - STC1)
570 *
571 * or
572 *
573 * SOF = ((SOF2 - SOF1) * PTS + SOF1 * STC2 - SOF2 * STC1) / (STC2 - STC1) (1)
574 *
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300575 * to avoid losing precision in the division. Similarly, the host timestamp is
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300576 * computed with
577 *
578 * TS = ((TS2 - TS1) * PTS + TS1 * SOF2 - TS2 * SOF1) / (SOF2 - SOF1) (2)
579 *
580 * SOF values are coded on 11 bits by USB. We extend their precision with 16
581 * decimal bits, leading to a 11.16 coding.
582 *
583 * TODO: To avoid surprises with device clock values, PTS/STC timestamps should
584 * be normalized using the nominal device clock frequency reported through the
585 * UVC descriptors.
586 *
587 * Both the PTS/STC and SOF counters roll over, after a fixed but device
588 * specific amount of time for PTS/STC and after 2048ms for SOF. As long as the
589 * sliding window size is smaller than the rollover period, differences computed
590 * on unsigned integers will produce the correct result. However, the p term in
591 * the linear relations will be miscomputed.
592 *
593 * To fix the issue, we subtract a constant from the PTS and STC values to bring
594 * PTS to half the 32 bit STC range. The sliding window STC values then fit into
595 * the 32 bit range without any rollover.
596 *
597 * Similarly, we add 2048 to the device SOF values to make sure that the SOF
598 * computed by (1) will never be smaller than 0. This offset is then compensated
599 * by adding 2048 to the SOF values used in (2). However, this doesn't prevent
600 * rollovers between (1) and (2): the SOF value computed by (1) can be slightly
601 * lower than 4096, and the host SOF counters can have rolled over to 2048. This
602 * case is handled by subtracting 2048 from the SOF value if it exceeds the host
603 * SOF value at the end of the sliding window.
604 *
605 * Finally we subtract a constant from the host timestamps to bring the first
606 * timestamp of the sliding window to 1s.
607 */
608void uvc_video_clock_update(struct uvc_streaming *stream,
609 struct v4l2_buffer *v4l2_buf,
610 struct uvc_buffer *buf)
611{
612 struct uvc_clock *clock = &stream->clock;
613 struct uvc_clock_sample *first;
614 struct uvc_clock_sample *last;
615 unsigned long flags;
616 struct timespec ts;
617 u32 delta_stc;
618 u32 y1, y2;
619 u32 x1, x2;
620 u32 mean;
621 u32 sof;
622 u32 div;
623 u32 rem;
624 u64 y;
625
Laurent Pinchart5d0fd3c2015-07-27 11:06:48 -0300626 if (!uvc_hw_timestamps_param)
627 return;
628
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300629 spin_lock_irqsave(&clock->lock, flags);
630
631 if (clock->count < clock->size)
632 goto done;
633
634 first = &clock->samples[clock->head];
635 last = &clock->samples[(clock->head - 1) % clock->size];
636
637 /* First step, PTS to SOF conversion. */
638 delta_stc = buf->pts - (1UL << 31);
639 x1 = first->dev_stc - delta_stc;
640 x2 = last->dev_stc - delta_stc;
Laurent Pinchart8e57dec2012-01-15 13:53:45 -0300641 if (x1 == x2)
642 goto done;
643
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300644 y1 = (first->dev_sof + 2048) << 16;
645 y2 = (last->dev_sof + 2048) << 16;
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300646 if (y2 < y1)
647 y2 += 2048 << 16;
648
649 y = (u64)(y2 - y1) * (1ULL << 31) + (u64)y1 * (u64)x2
650 - (u64)y2 * (u64)x1;
651 y = div_u64(y, x2 - x1);
652
653 sof = y;
654
655 uvc_trace(UVC_TRACE_CLOCK, "%s: PTS %u y %llu.%06llu SOF %u.%06llu "
656 "(x1 %u x2 %u y1 %u y2 %u SOF offset %u)\n",
657 stream->dev->name, buf->pts,
658 y >> 16, div_u64((y & 0xffff) * 1000000, 65536),
659 sof >> 16, div_u64(((u64)sof & 0xffff) * 1000000LLU, 65536),
660 x1, x2, y1, y2, clock->sof_offset);
661
662 /* Second step, SOF to host clock conversion. */
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300663 x1 = (uvc_video_clock_host_sof(first) + 2048) << 16;
664 x2 = (uvc_video_clock_host_sof(last) + 2048) << 16;
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300665 if (x2 < x1)
666 x2 += 2048 << 16;
Laurent Pinchart8e57dec2012-01-15 13:53:45 -0300667 if (x1 == x2)
668 goto done;
669
670 ts = timespec_sub(last->host_ts, first->host_ts);
671 y1 = NSEC_PER_SEC;
672 y2 = (ts.tv_sec + 1) * NSEC_PER_SEC + ts.tv_nsec;
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300673
674 /* Interpolated and host SOF timestamps can wrap around at slightly
675 * different times. Handle this by adding or removing 2048 to or from
676 * the computed SOF value to keep it close to the SOF samples mean
677 * value.
678 */
679 mean = (x1 + x2) / 2;
680 if (mean - (1024 << 16) > sof)
681 sof += 2048 << 16;
682 else if (sof > mean + (1024 << 16))
683 sof -= 2048 << 16;
684
685 y = (u64)(y2 - y1) * (u64)sof + (u64)y1 * (u64)x2
686 - (u64)y2 * (u64)x1;
687 y = div_u64(y, x2 - x1);
688
689 div = div_u64_rem(y, NSEC_PER_SEC, &rem);
690 ts.tv_sec = first->host_ts.tv_sec - 1 + div;
691 ts.tv_nsec = first->host_ts.tv_nsec + rem;
692 if (ts.tv_nsec >= NSEC_PER_SEC) {
693 ts.tv_sec++;
694 ts.tv_nsec -= NSEC_PER_SEC;
695 }
696
697 uvc_trace(UVC_TRACE_CLOCK, "%s: SOF %u.%06llu y %llu ts %lu.%06lu "
698 "buf ts %lu.%06lu (x1 %u/%u/%u x2 %u/%u/%u y1 %u y2 %u)\n",
699 stream->dev->name,
700 sof >> 16, div_u64(((u64)sof & 0xffff) * 1000000LLU, 65536),
701 y, ts.tv_sec, ts.tv_nsec / NSEC_PER_USEC,
Mauro Carvalho Chehabce01cbd2013-11-01 15:39:53 -0300702 v4l2_buf->timestamp.tv_sec,
703 (unsigned long)v4l2_buf->timestamp.tv_usec,
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300704 x1, first->host_sof, first->dev_sof,
705 x2, last->host_sof, last->dev_sof, y1, y2);
706
707 /* Update the V4L2 buffer. */
708 v4l2_buf->timestamp.tv_sec = ts.tv_sec;
709 v4l2_buf->timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
710
711done:
712 spin_unlock_irqrestore(&stream->clock.lock, flags);
713}
714
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300715/* ------------------------------------------------------------------------
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300716 * Stream statistics
717 */
718
719static void uvc_video_stats_decode(struct uvc_streaming *stream,
720 const __u8 *data, int len)
721{
722 unsigned int header_size;
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300723 bool has_pts = false;
724 bool has_scr = false;
725 u16 uninitialized_var(scr_sof);
726 u32 uninitialized_var(scr_stc);
727 u32 uninitialized_var(pts);
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300728
729 if (stream->stats.stream.nb_frames == 0 &&
730 stream->stats.frame.nb_packets == 0)
731 ktime_get_ts(&stream->stats.stream.start_ts);
732
733 switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) {
734 case UVC_STREAM_PTS | UVC_STREAM_SCR:
735 header_size = 12;
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300736 has_pts = true;
737 has_scr = true;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300738 break;
739 case UVC_STREAM_PTS:
740 header_size = 6;
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300741 has_pts = true;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300742 break;
743 case UVC_STREAM_SCR:
744 header_size = 8;
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300745 has_scr = true;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300746 break;
747 default:
748 header_size = 2;
749 break;
750 }
751
752 /* Check for invalid headers. */
753 if (len < header_size || data[0] < header_size) {
754 stream->stats.frame.nb_invalid++;
755 return;
756 }
757
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300758 /* Extract the timestamps. */
759 if (has_pts)
760 pts = get_unaligned_le32(&data[2]);
761
762 if (has_scr) {
763 scr_stc = get_unaligned_le32(&data[header_size - 6]);
764 scr_sof = get_unaligned_le16(&data[header_size - 2]);
765 }
766
767 /* Is PTS constant through the whole frame ? */
768 if (has_pts && stream->stats.frame.nb_pts) {
769 if (stream->stats.frame.pts != pts) {
770 stream->stats.frame.nb_pts_diffs++;
771 stream->stats.frame.last_pts_diff =
772 stream->stats.frame.nb_packets;
773 }
774 }
775
776 if (has_pts) {
777 stream->stats.frame.nb_pts++;
778 stream->stats.frame.pts = pts;
779 }
780
781 /* Do all frames have a PTS in their first non-empty packet, or before
782 * their first empty packet ?
783 */
784 if (stream->stats.frame.size == 0) {
785 if (len > header_size)
786 stream->stats.frame.has_initial_pts = has_pts;
787 if (len == header_size && has_pts)
788 stream->stats.frame.has_early_pts = true;
789 }
790
791 /* Do the SCR.STC and SCR.SOF fields vary through the frame ? */
792 if (has_scr && stream->stats.frame.nb_scr) {
793 if (stream->stats.frame.scr_stc != scr_stc)
794 stream->stats.frame.nb_scr_diffs++;
795 }
796
797 if (has_scr) {
798 /* Expand the SOF counter to 32 bits and store its value. */
799 if (stream->stats.stream.nb_frames > 0 ||
800 stream->stats.frame.nb_scr > 0)
801 stream->stats.stream.scr_sof_count +=
802 (scr_sof - stream->stats.stream.scr_sof) % 2048;
803 stream->stats.stream.scr_sof = scr_sof;
804
805 stream->stats.frame.nb_scr++;
806 stream->stats.frame.scr_stc = scr_stc;
807 stream->stats.frame.scr_sof = scr_sof;
808
809 if (scr_sof < stream->stats.stream.min_sof)
810 stream->stats.stream.min_sof = scr_sof;
811 if (scr_sof > stream->stats.stream.max_sof)
812 stream->stats.stream.max_sof = scr_sof;
813 }
814
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300815 /* Record the first non-empty packet number. */
816 if (stream->stats.frame.size == 0 && len > header_size)
817 stream->stats.frame.first_data = stream->stats.frame.nb_packets;
818
819 /* Update the frame size. */
820 stream->stats.frame.size += len - header_size;
821
822 /* Update the packets counters. */
823 stream->stats.frame.nb_packets++;
824 if (len > header_size)
825 stream->stats.frame.nb_empty++;
826
827 if (data[1] & UVC_STREAM_ERR)
828 stream->stats.frame.nb_errors++;
829}
830
831static void uvc_video_stats_update(struct uvc_streaming *stream)
832{
833 struct uvc_stats_frame *frame = &stream->stats.frame;
834
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300835 uvc_trace(UVC_TRACE_STATS, "frame %u stats: %u/%u/%u packets, "
836 "%u/%u/%u pts (%searly %sinitial), %u/%u scr, "
837 "last pts/stc/sof %u/%u/%u\n",
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300838 stream->sequence, frame->first_data,
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300839 frame->nb_packets - frame->nb_empty, frame->nb_packets,
840 frame->nb_pts_diffs, frame->last_pts_diff, frame->nb_pts,
841 frame->has_early_pts ? "" : "!",
842 frame->has_initial_pts ? "" : "!",
843 frame->nb_scr_diffs, frame->nb_scr,
844 frame->pts, frame->scr_stc, frame->scr_sof);
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300845
846 stream->stats.stream.nb_frames++;
847 stream->stats.stream.nb_packets += stream->stats.frame.nb_packets;
848 stream->stats.stream.nb_empty += stream->stats.frame.nb_empty;
849 stream->stats.stream.nb_errors += stream->stats.frame.nb_errors;
850 stream->stats.stream.nb_invalid += stream->stats.frame.nb_invalid;
851
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300852 if (frame->has_early_pts)
853 stream->stats.stream.nb_pts_early++;
854 if (frame->has_initial_pts)
855 stream->stats.stream.nb_pts_initial++;
856 if (frame->last_pts_diff <= frame->first_data)
857 stream->stats.stream.nb_pts_constant++;
858 if (frame->nb_scr >= frame->nb_packets - frame->nb_empty)
859 stream->stats.stream.nb_scr_count_ok++;
860 if (frame->nb_scr_diffs + 1 == frame->nb_scr)
861 stream->stats.stream.nb_scr_diffs_ok++;
862
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300863 memset(&stream->stats.frame, 0, sizeof(stream->stats.frame));
864}
865
866size_t uvc_video_stats_dump(struct uvc_streaming *stream, char *buf,
867 size_t size)
868{
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300869 unsigned int scr_sof_freq;
870 unsigned int duration;
871 struct timespec ts;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300872 size_t count = 0;
873
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300874 ts.tv_sec = stream->stats.stream.stop_ts.tv_sec
875 - stream->stats.stream.start_ts.tv_sec;
876 ts.tv_nsec = stream->stats.stream.stop_ts.tv_nsec
877 - stream->stats.stream.start_ts.tv_nsec;
878 if (ts.tv_nsec < 0) {
879 ts.tv_sec--;
880 ts.tv_nsec += 1000000000;
881 }
882
883 /* Compute the SCR.SOF frequency estimate. At the nominal 1kHz SOF
884 * frequency this will not overflow before more than 1h.
885 */
886 duration = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
887 if (duration != 0)
888 scr_sof_freq = stream->stats.stream.scr_sof_count * 1000
889 / duration;
890 else
891 scr_sof_freq = 0;
892
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300893 count += scnprintf(buf + count, size - count,
894 "frames: %u\npackets: %u\nempty: %u\n"
895 "errors: %u\ninvalid: %u\n",
896 stream->stats.stream.nb_frames,
897 stream->stats.stream.nb_packets,
898 stream->stats.stream.nb_empty,
899 stream->stats.stream.nb_errors,
900 stream->stats.stream.nb_invalid);
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300901 count += scnprintf(buf + count, size - count,
902 "pts: %u early, %u initial, %u ok\n",
903 stream->stats.stream.nb_pts_early,
904 stream->stats.stream.nb_pts_initial,
905 stream->stats.stream.nb_pts_constant);
906 count += scnprintf(buf + count, size - count,
907 "scr: %u count ok, %u diff ok\n",
908 stream->stats.stream.nb_scr_count_ok,
909 stream->stats.stream.nb_scr_diffs_ok);
910 count += scnprintf(buf + count, size - count,
911 "sof: %u <= sof <= %u, freq %u.%03u kHz\n",
912 stream->stats.stream.min_sof,
913 stream->stats.stream.max_sof,
914 scr_sof_freq / 1000, scr_sof_freq % 1000);
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300915
916 return count;
917}
918
919static void uvc_video_stats_start(struct uvc_streaming *stream)
920{
921 memset(&stream->stats, 0, sizeof(stream->stats));
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300922 stream->stats.stream.min_sof = 2048;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300923}
924
925static void uvc_video_stats_stop(struct uvc_streaming *stream)
926{
927 ktime_get_ts(&stream->stats.stream.stop_ts);
928}
929
930/* ------------------------------------------------------------------------
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300931 * Video codecs
932 */
933
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300934/* Video payload decoding is handled by uvc_video_decode_start(),
935 * uvc_video_decode_data() and uvc_video_decode_end().
936 *
937 * uvc_video_decode_start is called with URB data at the start of a bulk or
938 * isochronous payload. It processes header data and returns the header size
939 * in bytes if successful. If an error occurs, it returns a negative error
940 * code. The following error codes have special meanings.
941 *
942 * - EAGAIN informs the caller that the current video buffer should be marked
943 * as done, and that the function should be called again with the same data
944 * and a new video buffer. This is used when end of frame conditions can be
945 * reliably detected at the beginning of the next frame only.
946 *
947 * If an error other than -EAGAIN is returned, the caller will drop the current
948 * payload. No call to uvc_video_decode_data and uvc_video_decode_end will be
949 * made until the next payload. -ENODATA can be used to drop the current
950 * payload if no other error code is appropriate.
951 *
952 * uvc_video_decode_data is called for every URB with URB data. It copies the
953 * data to the video buffer.
954 *
955 * uvc_video_decode_end is called with header data at the end of a bulk or
956 * isochronous payload. It performs any additional header data processing and
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300957 * returns 0 or a negative error code if an error occurred. As header data have
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300958 * already been processed by uvc_video_decode_start, this functions isn't
959 * required to perform sanity checks a second time.
960 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300961 * For isochronous transfers where a payload is always transferred in a single
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300962 * URB, the three functions will be called in a row.
963 *
964 * To let the decoder process header data and update its internal state even
965 * when no video buffer is available, uvc_video_decode_start must be prepared
966 * to be called with a NULL buf parameter. uvc_video_decode_data and
967 * uvc_video_decode_end will never be called with a NULL buffer.
968 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300969static int uvc_video_decode_start(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300970 struct uvc_buffer *buf, const __u8 *data, int len)
971{
972 __u8 fid;
973
974 /* Sanity checks:
975 * - packet must be at least 2 bytes long
976 * - bHeaderLength value must be at least 2 bytes (see above)
977 * - bHeaderLength value can't be larger than the packet size.
978 */
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300979 if (len < 2 || data[0] < 2 || data[0] > len) {
980 stream->stats.frame.nb_invalid++;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300981 return -EINVAL;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300982 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300983
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300984 fid = data[1] & UVC_STREAM_FID;
985
Laurent Pinchart650b95f2010-10-02 11:06:05 -0300986 /* Increase the sequence number regardless of any buffer states, so
987 * that discontinuous sequence numbers always indicate lost frames.
988 */
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300989 if (stream->last_fid != fid) {
Laurent Pinchart650b95f2010-10-02 11:06:05 -0300990 stream->sequence++;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300991 if (stream->sequence)
992 uvc_video_stats_update(stream);
993 }
994
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300995 uvc_video_clock_decode(stream, buf, data, len);
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300996 uvc_video_stats_decode(stream, data, len);
Laurent Pinchart650b95f2010-10-02 11:06:05 -0300997
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300998 /* Store the payload FID bit and return immediately when the buffer is
999 * NULL.
1000 */
1001 if (buf == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001002 stream->last_fid = fid;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001003 return -ENODATA;
1004 }
1005
Laurent Pinchart3afedb92011-11-03 07:24:34 -03001006 /* Mark the buffer as bad if the error bit is set. */
1007 if (data[1] & UVC_STREAM_ERR) {
1008 uvc_trace(UVC_TRACE_FRAME, "Marking buffer as bad (error bit "
1009 "set).\n");
1010 buf->error = 1;
1011 }
1012
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001013 /* Synchronize to the input stream by waiting for the FID bit to be
1014 * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE.
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001015 * stream->last_fid is initialized to -1, so the first isochronous
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001016 * frame will always be in sync.
1017 *
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001018 * If the device doesn't toggle the FID bit, invert stream->last_fid
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001019 * when the EOF bit is set to force synchronisation on the next packet.
1020 */
1021 if (buf->state != UVC_BUF_STATE_ACTIVE) {
Laurent Pinchart310fe522009-12-09 22:57:48 -03001022 struct timespec ts;
1023
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001024 if (fid == stream->last_fid) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001025 uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of "
1026 "sync).\n");
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001027 if ((stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) &&
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001028 (data[1] & UVC_STREAM_EOF))
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001029 stream->last_fid ^= UVC_STREAM_FID;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001030 return -ENODATA;
1031 }
1032
Olivier Langlois3b35fc82014-03-28 02:42:38 -03001033 uvc_video_get_ts(&ts);
Laurent Pinchart310fe522009-12-09 22:57:48 -03001034
Laurent Pinchartaa3d17d2014-10-31 09:56:16 -03001035 buf->buf.v4l2_buf.field = V4L2_FIELD_NONE;
Laurent Pinchart6998b6f2011-10-24 11:53:59 -03001036 buf->buf.v4l2_buf.sequence = stream->sequence;
1037 buf->buf.v4l2_buf.timestamp.tv_sec = ts.tv_sec;
1038 buf->buf.v4l2_buf.timestamp.tv_usec =
1039 ts.tv_nsec / NSEC_PER_USEC;
Laurent Pinchart310fe522009-12-09 22:57:48 -03001040
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001041 /* TODO: Handle PTS and SCR. */
1042 buf->state = UVC_BUF_STATE_ACTIVE;
1043 }
1044
1045 /* Mark the buffer as done if we're at the beginning of a new frame.
1046 * End of frame detection is better implemented by checking the EOF
1047 * bit (FID bit toggling is delayed by one frame compared to the EOF
1048 * bit), but some devices don't set the bit at end of frame (and the
1049 * last payload can be lost anyway). We thus must check if the FID has
1050 * been toggled.
1051 *
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001052 * stream->last_fid is initialized to -1, so the first isochronous
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001053 * frame will never trigger an end of frame detection.
1054 *
1055 * Empty buffers (bytesused == 0) don't trigger end of frame detection
1056 * as it doesn't make sense to return an empty buffer. This also
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03001057 * avoids detecting end of frame conditions at FID toggling if the
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001058 * previous payload had the EOF bit set.
1059 */
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001060 if (fid != stream->last_fid && buf->bytesused != 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001061 uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit "
1062 "toggled).\n");
Laurent Pinchartd7c0d432009-12-16 21:20:45 -03001063 buf->state = UVC_BUF_STATE_READY;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001064 return -EAGAIN;
1065 }
1066
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001067 stream->last_fid = fid;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001068
1069 return data[0];
1070}
1071
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001072static void uvc_video_decode_data(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001073 struct uvc_buffer *buf, const __u8 *data, int len)
1074{
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001075 unsigned int maxlen, nbytes;
1076 void *mem;
1077
1078 if (len <= 0)
1079 return;
1080
1081 /* Copy the video data to the buffer. */
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001082 maxlen = buf->length - buf->bytesused;
1083 mem = buf->mem + buf->bytesused;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001084 nbytes = min((unsigned int)len, maxlen);
1085 memcpy(mem, data, nbytes);
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001086 buf->bytesused += nbytes;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001087
1088 /* Complete the current frame if the buffer size was exceeded. */
1089 if (len > maxlen) {
1090 uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n");
Laurent Pinchartd7c0d432009-12-16 21:20:45 -03001091 buf->state = UVC_BUF_STATE_READY;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001092 }
1093}
1094
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001095static void uvc_video_decode_end(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001096 struct uvc_buffer *buf, const __u8 *data, int len)
1097{
1098 /* Mark the buffer as done if the EOF marker is set. */
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001099 if (data[1] & UVC_STREAM_EOF && buf->bytesused != 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001100 uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n");
1101 if (data[0] == len)
1102 uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n");
Laurent Pinchartd7c0d432009-12-16 21:20:45 -03001103 buf->state = UVC_BUF_STATE_READY;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001104 if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID)
1105 stream->last_fid ^= UVC_STREAM_FID;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001106 }
1107}
1108
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03001109/* Video payload encoding is handled by uvc_video_encode_header() and
1110 * uvc_video_encode_data(). Only bulk transfers are currently supported.
1111 *
1112 * uvc_video_encode_header is called at the start of a payload. It adds header
1113 * data to the transfer buffer and returns the header size. As the only known
1114 * UVC output device transfers a whole frame in a single payload, the EOF bit
1115 * is always set in the header.
1116 *
1117 * uvc_video_encode_data is called for every URB and copies the data from the
1118 * video buffer to the transfer buffer.
1119 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001120static int uvc_video_encode_header(struct uvc_streaming *stream,
Laurent Pinchartff924202008-12-28 22:32:29 -03001121 struct uvc_buffer *buf, __u8 *data, int len)
1122{
1123 data[0] = 2; /* Header length */
1124 data[1] = UVC_STREAM_EOH | UVC_STREAM_EOF
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001125 | (stream->last_fid & UVC_STREAM_FID);
Laurent Pinchartff924202008-12-28 22:32:29 -03001126 return 2;
1127}
1128
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001129static int uvc_video_encode_data(struct uvc_streaming *stream,
Laurent Pinchartff924202008-12-28 22:32:29 -03001130 struct uvc_buffer *buf, __u8 *data, int len)
1131{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001132 struct uvc_video_queue *queue = &stream->queue;
Laurent Pinchartff924202008-12-28 22:32:29 -03001133 unsigned int nbytes;
1134 void *mem;
1135
1136 /* Copy video data to the URB buffer. */
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001137 mem = buf->mem + queue->buf_used;
1138 nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001139 nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size,
Laurent Pinchartff924202008-12-28 22:32:29 -03001140 nbytes);
1141 memcpy(data, mem, nbytes);
1142
1143 queue->buf_used += nbytes;
1144
1145 return nbytes;
1146}
1147
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001148/* ------------------------------------------------------------------------
1149 * URB handling
1150 */
1151
1152/*
Anton Leontievf8918ba2014-03-25 01:40:57 -03001153 * Set error flag for incomplete buffer.
1154 */
1155static void uvc_video_validate_buffer(const struct uvc_streaming *stream,
1156 struct uvc_buffer *buf)
1157{
Laurent Pinchartc601f532014-09-30 18:28:42 -03001158 if (stream->ctrl.dwMaxVideoFrameSize != buf->bytesused &&
Anton Leontievf8918ba2014-03-25 01:40:57 -03001159 !(stream->cur_format->flags & UVC_FMT_FLAG_COMPRESSED))
1160 buf->error = 1;
1161}
1162
1163/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001164 * Completion handler for video URBs.
1165 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001166static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
1167 struct uvc_buffer *buf)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001168{
1169 u8 *mem;
1170 int ret, i;
1171
1172 for (i = 0; i < urb->number_of_packets; ++i) {
1173 if (urb->iso_frame_desc[i].status < 0) {
1174 uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
1175 "lost (%d).\n", urb->iso_frame_desc[i].status);
Laurent Pinchart9bde9f22010-06-17 06:52:37 -03001176 /* Mark the buffer as faulty. */
1177 if (buf != NULL)
1178 buf->error = 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001179 continue;
1180 }
1181
1182 /* Decode the payload header. */
1183 mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1184 do {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001185 ret = uvc_video_decode_start(stream, buf, mem,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001186 urb->iso_frame_desc[i].actual_length);
Anton Leontievf8918ba2014-03-25 01:40:57 -03001187 if (ret == -EAGAIN) {
1188 uvc_video_validate_buffer(stream, buf);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001189 buf = uvc_queue_next_buffer(&stream->queue,
1190 buf);
Anton Leontievf8918ba2014-03-25 01:40:57 -03001191 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001192 } while (ret == -EAGAIN);
1193
1194 if (ret < 0)
1195 continue;
1196
1197 /* Decode the payload data. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001198 uvc_video_decode_data(stream, buf, mem + ret,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001199 urb->iso_frame_desc[i].actual_length - ret);
1200
1201 /* Process the header again. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001202 uvc_video_decode_end(stream, buf, mem,
Laurent Pinchart08ebd002008-08-29 17:37:44 -03001203 urb->iso_frame_desc[i].actual_length);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001204
Laurent Pinchart9bde9f22010-06-17 06:52:37 -03001205 if (buf->state == UVC_BUF_STATE_READY) {
Anton Leontievf8918ba2014-03-25 01:40:57 -03001206 uvc_video_validate_buffer(stream, buf);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001207 buf = uvc_queue_next_buffer(&stream->queue, buf);
Laurent Pinchart9bde9f22010-06-17 06:52:37 -03001208 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001209 }
1210}
1211
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001212static void uvc_video_decode_bulk(struct urb *urb, struct uvc_streaming *stream,
1213 struct uvc_buffer *buf)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001214{
1215 u8 *mem;
1216 int len, ret;
1217
Jayakrishnanc854a482012-03-09 10:10:49 -03001218 /*
1219 * Ignore ZLPs if they're not part of a frame, otherwise process them
1220 * to trigger the end of payload detection.
1221 */
1222 if (urb->actual_length == 0 && stream->bulk.header_size == 0)
Laurent Pinchartc90e7772009-02-14 19:39:08 -03001223 return;
1224
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001225 mem = urb->transfer_buffer;
1226 len = urb->actual_length;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001227 stream->bulk.payload_size += len;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001228
1229 /* If the URB is the first of its payload, decode and save the
1230 * header.
1231 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001232 if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001233 do {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001234 ret = uvc_video_decode_start(stream, buf, mem, len);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001235 if (ret == -EAGAIN)
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001236 buf = uvc_queue_next_buffer(&stream->queue,
1237 buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001238 } while (ret == -EAGAIN);
1239
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001240 /* If an error occurred skip the rest of the payload. */
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001241 if (ret < 0 || buf == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001242 stream->bulk.skip_payload = 1;
Laurent Pinchartf8dd4af2008-12-16 10:41:57 -03001243 } else {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001244 memcpy(stream->bulk.header, mem, ret);
1245 stream->bulk.header_size = ret;
Laurent Pinchartf8dd4af2008-12-16 10:41:57 -03001246
1247 mem += ret;
1248 len -= ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001249 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001250 }
1251
1252 /* The buffer queue might have been cancelled while a bulk transfer
1253 * was in progress, so we can reach here with buf equal to NULL. Make
1254 * sure buf is never dereferenced if NULL.
1255 */
1256
1257 /* Process video data. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001258 if (!stream->bulk.skip_payload && buf != NULL)
1259 uvc_video_decode_data(stream, buf, mem, len);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001260
1261 /* Detect the payload end by a URB smaller than the maximum size (or
1262 * a payload size equal to the maximum) and process the header again.
1263 */
1264 if (urb->actual_length < urb->transfer_buffer_length ||
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001265 stream->bulk.payload_size >= stream->bulk.max_payload_size) {
1266 if (!stream->bulk.skip_payload && buf != NULL) {
1267 uvc_video_decode_end(stream, buf, stream->bulk.header,
1268 stream->bulk.payload_size);
Laurent Pinchartd7c0d432009-12-16 21:20:45 -03001269 if (buf->state == UVC_BUF_STATE_READY)
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001270 buf = uvc_queue_next_buffer(&stream->queue,
1271 buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001272 }
1273
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001274 stream->bulk.header_size = 0;
1275 stream->bulk.skip_payload = 0;
1276 stream->bulk.payload_size = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001277 }
1278}
1279
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001280static void uvc_video_encode_bulk(struct urb *urb, struct uvc_streaming *stream,
1281 struct uvc_buffer *buf)
Laurent Pinchartff924202008-12-28 22:32:29 -03001282{
1283 u8 *mem = urb->transfer_buffer;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001284 int len = stream->urb_size, ret;
Laurent Pinchartff924202008-12-28 22:32:29 -03001285
1286 if (buf == NULL) {
1287 urb->transfer_buffer_length = 0;
1288 return;
1289 }
1290
1291 /* If the URB is the first of its payload, add the header. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001292 if (stream->bulk.header_size == 0) {
1293 ret = uvc_video_encode_header(stream, buf, mem, len);
1294 stream->bulk.header_size = ret;
1295 stream->bulk.payload_size += ret;
Laurent Pinchartff924202008-12-28 22:32:29 -03001296 mem += ret;
1297 len -= ret;
1298 }
1299
1300 /* Process video data. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001301 ret = uvc_video_encode_data(stream, buf, mem, len);
Laurent Pinchartff924202008-12-28 22:32:29 -03001302
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001303 stream->bulk.payload_size += ret;
Laurent Pinchartff924202008-12-28 22:32:29 -03001304 len -= ret;
1305
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001306 if (buf->bytesused == stream->queue.buf_used ||
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001307 stream->bulk.payload_size == stream->bulk.max_payload_size) {
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001308 if (buf->bytesused == stream->queue.buf_used) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001309 stream->queue.buf_used = 0;
Laurent Pinchartd7c0d432009-12-16 21:20:45 -03001310 buf->state = UVC_BUF_STATE_READY;
Laurent Pinchart6998b6f2011-10-24 11:53:59 -03001311 buf->buf.v4l2_buf.sequence = ++stream->sequence;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001312 uvc_queue_next_buffer(&stream->queue, buf);
1313 stream->last_fid ^= UVC_STREAM_FID;
Laurent Pinchartff924202008-12-28 22:32:29 -03001314 }
1315
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001316 stream->bulk.header_size = 0;
1317 stream->bulk.payload_size = 0;
Laurent Pinchartff924202008-12-28 22:32:29 -03001318 }
1319
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001320 urb->transfer_buffer_length = stream->urb_size - len;
Laurent Pinchartff924202008-12-28 22:32:29 -03001321}
1322
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001323static void uvc_video_complete(struct urb *urb)
1324{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001325 struct uvc_streaming *stream = urb->context;
1326 struct uvc_video_queue *queue = &stream->queue;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001327 struct uvc_buffer *buf = NULL;
1328 unsigned long flags;
1329 int ret;
1330
1331 switch (urb->status) {
1332 case 0:
1333 break;
1334
1335 default:
1336 uvc_printk(KERN_WARNING, "Non-zero status (%d) in video "
1337 "completion handler.\n", urb->status);
1338
1339 case -ENOENT: /* usb_kill_urb() called. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001340 if (stream->frozen)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001341 return;
1342
1343 case -ECONNRESET: /* usb_unlink_urb() called. */
1344 case -ESHUTDOWN: /* The endpoint is being disabled. */
1345 uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
1346 return;
1347 }
1348
1349 spin_lock_irqsave(&queue->irqlock, flags);
1350 if (!list_empty(&queue->irqqueue))
1351 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
1352 queue);
1353 spin_unlock_irqrestore(&queue->irqlock, flags);
1354
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001355 stream->decode(urb, stream, buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001356
1357 if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
1358 uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n",
1359 ret);
1360 }
1361}
1362
1363/*
Laurent Pincharte01117c2008-07-04 00:36:21 -03001364 * Free transfer buffers.
1365 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001366static void uvc_free_urb_buffers(struct uvc_streaming *stream)
Laurent Pincharte01117c2008-07-04 00:36:21 -03001367{
1368 unsigned int i;
1369
1370 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001371 if (stream->urb_buffer[i]) {
Al Cooper3e0ac672011-08-18 10:28:29 -03001372#ifndef CONFIG_DMA_NONCOHERENT
Daniel Mack997ea582010-04-12 13:17:25 +02001373 usb_free_coherent(stream->dev->udev, stream->urb_size,
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001374 stream->urb_buffer[i], stream->urb_dma[i]);
Al Cooper3e0ac672011-08-18 10:28:29 -03001375#else
1376 kfree(stream->urb_buffer[i]);
1377#endif
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001378 stream->urb_buffer[i] = NULL;
Laurent Pincharte01117c2008-07-04 00:36:21 -03001379 }
1380 }
1381
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001382 stream->urb_size = 0;
Laurent Pincharte01117c2008-07-04 00:36:21 -03001383}
1384
1385/*
1386 * Allocate transfer buffers. This function can be called with buffers
1387 * already allocated when resuming from suspend, in which case it will
1388 * return without touching the buffers.
1389 *
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001390 * Limit the buffer size to UVC_MAX_PACKETS bulk/isochronous packets. If the
1391 * system is too low on memory try successively smaller numbers of packets
1392 * until allocation succeeds.
1393 *
1394 * Return the number of allocated packets on success or 0 when out of memory.
Laurent Pincharte01117c2008-07-04 00:36:21 -03001395 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001396static int uvc_alloc_urb_buffers(struct uvc_streaming *stream,
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001397 unsigned int size, unsigned int psize, gfp_t gfp_flags)
Laurent Pincharte01117c2008-07-04 00:36:21 -03001398{
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001399 unsigned int npackets;
Laurent Pincharte01117c2008-07-04 00:36:21 -03001400 unsigned int i;
1401
1402 /* Buffers are already allocated, bail out. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001403 if (stream->urb_size)
1404 return stream->urb_size / psize;
Laurent Pincharte01117c2008-07-04 00:36:21 -03001405
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001406 /* Compute the number of packets. Bulk endpoints might transfer UVC
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001407 * payloads across multiple URBs.
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001408 */
1409 npackets = DIV_ROUND_UP(size, psize);
1410 if (npackets > UVC_MAX_PACKETS)
1411 npackets = UVC_MAX_PACKETS;
1412
1413 /* Retry allocations until one succeed. */
1414 for (; npackets > 1; npackets /= 2) {
1415 for (i = 0; i < UVC_URBS; ++i) {
Ming Leifd1b6bb2009-09-27 05:30:34 -03001416 stream->urb_size = psize * npackets;
Al Cooper3e0ac672011-08-18 10:28:29 -03001417#ifndef CONFIG_DMA_NONCOHERENT
Daniel Mack997ea582010-04-12 13:17:25 +02001418 stream->urb_buffer[i] = usb_alloc_coherent(
Ming Leifd1b6bb2009-09-27 05:30:34 -03001419 stream->dev->udev, stream->urb_size,
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001420 gfp_flags | __GFP_NOWARN, &stream->urb_dma[i]);
Al Cooper3e0ac672011-08-18 10:28:29 -03001421#else
1422 stream->urb_buffer[i] =
1423 kmalloc(stream->urb_size, gfp_flags | __GFP_NOWARN);
1424#endif
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001425 if (!stream->urb_buffer[i]) {
1426 uvc_free_urb_buffers(stream);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001427 break;
1428 }
1429 }
1430
1431 if (i == UVC_URBS) {
Laurent Pinchart663a4192009-08-06 11:41:17 -03001432 uvc_trace(UVC_TRACE_VIDEO, "Allocated %u URB buffers "
1433 "of %ux%u bytes each.\n", UVC_URBS, npackets,
1434 psize);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001435 return npackets;
Laurent Pincharte01117c2008-07-04 00:36:21 -03001436 }
1437 }
1438
Laurent Pinchart663a4192009-08-06 11:41:17 -03001439 uvc_trace(UVC_TRACE_VIDEO, "Failed to allocate URB buffers (%u bytes "
1440 "per packet).\n", psize);
Laurent Pincharte01117c2008-07-04 00:36:21 -03001441 return 0;
1442}
1443
1444/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001445 * Uninitialize isochronous/bulk URBs and free transfer buffers.
1446 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001447static void uvc_uninit_video(struct uvc_streaming *stream, int free_buffers)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001448{
1449 struct urb *urb;
1450 unsigned int i;
1451
Alexey Fisher7bc5edb2011-11-03 11:40:08 -03001452 uvc_video_stats_stop(stream);
1453
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001454 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001455 urb = stream->urb[i];
1456 if (urb == NULL)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001457 continue;
1458
1459 usb_kill_urb(urb);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001460 usb_free_urb(urb);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001461 stream->urb[i] = NULL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001462 }
Laurent Pincharte01117c2008-07-04 00:36:21 -03001463
1464 if (free_buffers)
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001465 uvc_free_urb_buffers(stream);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001466}
1467
1468/*
Laurent Pinchart6fd90db2012-07-17 08:58:48 -03001469 * Compute the maximum number of bytes per interval for an endpoint.
1470 */
1471static unsigned int uvc_endpoint_max_bpi(struct usb_device *dev,
1472 struct usb_host_endpoint *ep)
1473{
1474 u16 psize;
1475
1476 switch (dev->speed) {
1477 case USB_SPEED_SUPER:
Hans Verkuild71b0b32014-08-20 17:58:38 -03001478 return le16_to_cpu(ep->ss_ep_comp.wBytesPerInterval);
Laurent Pinchart6fd90db2012-07-17 08:58:48 -03001479 case USB_SPEED_HIGH:
1480 psize = usb_endpoint_maxp(&ep->desc);
1481 return (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
Thomas Pugliese79af67e2014-01-24 18:17:28 -03001482 case USB_SPEED_WIRELESS:
1483 psize = usb_endpoint_maxp(&ep->desc);
1484 return psize;
Laurent Pinchart6fd90db2012-07-17 08:58:48 -03001485 default:
1486 psize = usb_endpoint_maxp(&ep->desc);
1487 return psize & 0x07ff;
1488 }
1489}
1490
1491/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001492 * Initialize isochronous URBs and allocate transfer buffers. The packet size
1493 * is given by the endpoint.
1494 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001495static int uvc_init_video_isoc(struct uvc_streaming *stream,
Laurent Pinchart29135872008-07-04 00:35:26 -03001496 struct usb_host_endpoint *ep, gfp_t gfp_flags)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001497{
1498 struct urb *urb;
1499 unsigned int npackets, i, j;
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001500 u16 psize;
1501 u32 size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001502
Laurent Pinchart6fd90db2012-07-17 08:58:48 -03001503 psize = uvc_endpoint_max_bpi(stream->dev->udev, ep);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001504 size = stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001505
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001506 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001507 if (npackets == 0)
1508 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001509
1510 size = npackets * psize;
1511
1512 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart29135872008-07-04 00:35:26 -03001513 urb = usb_alloc_urb(npackets, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001514 if (urb == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001515 uvc_uninit_video(stream, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001516 return -ENOMEM;
1517 }
1518
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001519 urb->dev = stream->dev->udev;
1520 urb->context = stream;
1521 urb->pipe = usb_rcvisocpipe(stream->dev->udev,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001522 ep->desc.bEndpointAddress);
Al Cooper3e0ac672011-08-18 10:28:29 -03001523#ifndef CONFIG_DMA_NONCOHERENT
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001524 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Al Cooper3e0ac672011-08-18 10:28:29 -03001525 urb->transfer_dma = stream->urb_dma[i];
1526#else
1527 urb->transfer_flags = URB_ISO_ASAP;
1528#endif
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001529 urb->interval = ep->desc.bInterval;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001530 urb->transfer_buffer = stream->urb_buffer[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001531 urb->complete = uvc_video_complete;
1532 urb->number_of_packets = npackets;
1533 urb->transfer_buffer_length = size;
1534
1535 for (j = 0; j < npackets; ++j) {
1536 urb->iso_frame_desc[j].offset = j * psize;
1537 urb->iso_frame_desc[j].length = psize;
1538 }
1539
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001540 stream->urb[i] = urb;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001541 }
1542
1543 return 0;
1544}
1545
1546/*
1547 * Initialize bulk URBs and allocate transfer buffers. The packet size is
1548 * given by the endpoint.
1549 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001550static int uvc_init_video_bulk(struct uvc_streaming *stream,
Laurent Pinchart29135872008-07-04 00:35:26 -03001551 struct usb_host_endpoint *ep, gfp_t gfp_flags)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001552{
1553 struct urb *urb;
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001554 unsigned int npackets, pipe, i;
1555 u16 psize;
1556 u32 size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001557
Laurent Pinchart6fd90db2012-07-17 08:58:48 -03001558 psize = usb_endpoint_maxp(&ep->desc) & 0x7ff;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001559 size = stream->ctrl.dwMaxPayloadTransferSize;
1560 stream->bulk.max_payload_size = size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001561
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001562 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001563 if (npackets == 0)
Laurent Pincharte01117c2008-07-04 00:36:21 -03001564 return -ENOMEM;
1565
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001566 size = npackets * psize;
1567
Laurent Pinchartff924202008-12-28 22:32:29 -03001568 if (usb_endpoint_dir_in(&ep->desc))
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001569 pipe = usb_rcvbulkpipe(stream->dev->udev,
Laurent Pinchartff924202008-12-28 22:32:29 -03001570 ep->desc.bEndpointAddress);
1571 else
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001572 pipe = usb_sndbulkpipe(stream->dev->udev,
Laurent Pinchartff924202008-12-28 22:32:29 -03001573 ep->desc.bEndpointAddress);
1574
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001575 if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
Laurent Pinchartff924202008-12-28 22:32:29 -03001576 size = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001577
1578 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart29135872008-07-04 00:35:26 -03001579 urb = usb_alloc_urb(0, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001580 if (urb == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001581 uvc_uninit_video(stream, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001582 return -ENOMEM;
1583 }
1584
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001585 usb_fill_bulk_urb(urb, stream->dev->udev, pipe,
1586 stream->urb_buffer[i], size, uvc_video_complete,
1587 stream);
Al Cooper3e0ac672011-08-18 10:28:29 -03001588#ifndef CONFIG_DMA_NONCOHERENT
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001589 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001590 urb->transfer_dma = stream->urb_dma[i];
Al Cooper3e0ac672011-08-18 10:28:29 -03001591#endif
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001592
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001593 stream->urb[i] = urb;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001594 }
1595
1596 return 0;
1597}
1598
1599/*
1600 * Initialize isochronous/bulk URBs and allocate transfer buffers.
1601 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001602static int uvc_init_video(struct uvc_streaming *stream, gfp_t gfp_flags)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001603{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001604 struct usb_interface *intf = stream->intf;
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001605 struct usb_host_endpoint *ep;
1606 unsigned int i;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001607 int ret;
1608
Laurent Pinchart650b95f2010-10-02 11:06:05 -03001609 stream->sequence = -1;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001610 stream->last_fid = -1;
1611 stream->bulk.header_size = 0;
1612 stream->bulk.skip_payload = 0;
1613 stream->bulk.payload_size = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001614
Alexey Fisher7bc5edb2011-11-03 11:40:08 -03001615 uvc_video_stats_start(stream);
1616
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001617 if (intf->num_altsetting > 1) {
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001618 struct usb_host_endpoint *best_ep = NULL;
Laurent Pinchart6fd90db2012-07-17 08:58:48 -03001619 unsigned int best_psize = UINT_MAX;
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001620 unsigned int bandwidth;
1621 unsigned int uninitialized_var(altsetting);
1622 int intfnum = stream->intfnum;
1623
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001624 /* Isochronous endpoint, select the alternate setting. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001625 bandwidth = stream->ctrl.dwMaxPayloadTransferSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001626
1627 if (bandwidth == 0) {
Laurent Pinchart663a4192009-08-06 11:41:17 -03001628 uvc_trace(UVC_TRACE_VIDEO, "Device requested null "
1629 "bandwidth, defaulting to lowest.\n");
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001630 bandwidth = 1;
Laurent Pinchart663a4192009-08-06 11:41:17 -03001631 } else {
1632 uvc_trace(UVC_TRACE_VIDEO, "Device requested %u "
1633 "B/frame bandwidth.\n", bandwidth);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001634 }
1635
1636 for (i = 0; i < intf->num_altsetting; ++i) {
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001637 struct usb_host_interface *alts;
1638 unsigned int psize;
1639
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001640 alts = &intf->altsetting[i];
1641 ep = uvc_find_endpoint(alts,
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001642 stream->header.bEndpointAddress);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001643 if (ep == NULL)
1644 continue;
1645
1646 /* Check if the bandwidth is high enough. */
Laurent Pinchart6fd90db2012-07-17 08:58:48 -03001647 psize = uvc_endpoint_max_bpi(stream->dev->udev, ep);
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001648 if (psize >= bandwidth && psize <= best_psize) {
Laurent Pinchart48070632012-06-21 06:35:04 -03001649 altsetting = alts->desc.bAlternateSetting;
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001650 best_psize = psize;
1651 best_ep = ep;
1652 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001653 }
1654
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001655 if (best_ep == NULL) {
Laurent Pinchart663a4192009-08-06 11:41:17 -03001656 uvc_trace(UVC_TRACE_VIDEO, "No fast enough alt setting "
1657 "for requested bandwidth.\n");
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001658 return -EIO;
Laurent Pinchart663a4192009-08-06 11:41:17 -03001659 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001660
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001661 uvc_trace(UVC_TRACE_VIDEO, "Selecting alternate setting %u "
1662 "(%u B/frame bandwidth).\n", altsetting, best_psize);
1663
1664 ret = usb_set_interface(stream->dev->udev, intfnum, altsetting);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001665 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001666 return ret;
1667
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001668 ret = uvc_init_video_isoc(stream, best_ep, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001669 } else {
1670 /* Bulk endpoint, proceed to URB initialization. */
1671 ep = uvc_find_endpoint(&intf->altsetting[0],
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001672 stream->header.bEndpointAddress);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001673 if (ep == NULL)
1674 return -EIO;
1675
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001676 ret = uvc_init_video_bulk(stream, ep, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001677 }
1678
1679 if (ret < 0)
1680 return ret;
1681
1682 /* Submit the URBs. */
1683 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001684 ret = usb_submit_urb(stream->urb[i], gfp_flags);
1685 if (ret < 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001686 uvc_printk(KERN_ERR, "Failed to submit URB %u "
1687 "(%d).\n", i, ret);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001688 uvc_uninit_video(stream, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001689 return ret;
1690 }
1691 }
1692
William Manley17e13192014-03-13 09:38:48 -03001693 /* The Logitech C920 temporarily forgets that it should not be adjusting
1694 * Exposure Absolute during init so restore controls to stored values.
1695 */
1696 if (stream->dev->quirks & UVC_QUIRK_RESTORE_CTRLS_ON_INIT)
1697 uvc_ctrl_restore_values(stream->dev);
1698
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001699 return 0;
1700}
1701
1702/* --------------------------------------------------------------------------
1703 * Suspend/resume
1704 */
1705
1706/*
1707 * Stop streaming without disabling the video queue.
1708 *
1709 * To let userspace applications resume without trouble, we must not touch the
1710 * video buffers in any way. We mark the device as frozen to make sure the URB
1711 * completion handler won't try to cancel the queue when we kill the URBs.
1712 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001713int uvc_video_suspend(struct uvc_streaming *stream)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001714{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001715 if (!uvc_queue_streaming(&stream->queue))
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001716 return 0;
1717
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001718 stream->frozen = 1;
1719 uvc_uninit_video(stream, 0);
1720 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001721 return 0;
1722}
1723
1724/*
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03001725 * Reconfigure the video interface and restart streaming if it was enabled
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001726 * before suspend.
1727 *
1728 * If an error occurs, disable the video queue. This will wake all pending
1729 * buffers, making sure userspace applications are notified of the problem
1730 * instead of waiting forever.
1731 */
Ming Leid59a7b12011-07-16 00:51:00 -03001732int uvc_video_resume(struct uvc_streaming *stream, int reset)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001733{
1734 int ret;
1735
Ming Leid59a7b12011-07-16 00:51:00 -03001736 /* If the bus has been reset on resume, set the alternate setting to 0.
1737 * This should be the default value, but some devices crash or otherwise
1738 * misbehave if they don't receive a SET_INTERFACE request before any
1739 * other video control request.
1740 */
1741 if (reset)
1742 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1743
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001744 stream->frozen = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001745
Laurent Pincharted0ee0c2012-03-27 05:51:00 -03001746 uvc_video_clock_reset(stream);
1747
Aviv Greenberg9fae30a2014-09-02 03:16:28 -03001748 if (!uvc_queue_streaming(&stream->queue))
1749 return 0;
1750
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001751 ret = uvc_commit_video(stream, &stream->ctrl);
Laurent Pinchartb83bba22014-10-13 10:11:35 -03001752 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001753 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001754
Laurent Pinchartb83bba22014-10-13 10:11:35 -03001755 return uvc_init_video(stream, GFP_NOIO);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001756}
1757
1758/* ------------------------------------------------------------------------
1759 * Video device
1760 */
1761
1762/*
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03001763 * Initialize the UVC video device by switching to alternate setting 0 and
1764 * retrieve the default format.
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001765 *
1766 * Some cameras (namely the Fuji Finepix) set the format and frame
1767 * indexes to zero. The UVC standard doesn't clearly make this a spec
1768 * violation, so try to silently fix the values if possible.
1769 *
1770 * This function is called before registering the device with V4L.
1771 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001772int uvc_video_init(struct uvc_streaming *stream)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001773{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001774 struct uvc_streaming_control *probe = &stream->ctrl;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001775 struct uvc_format *format = NULL;
1776 struct uvc_frame *frame = NULL;
1777 unsigned int i;
1778 int ret;
1779
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001780 if (stream->nformats == 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001781 uvc_printk(KERN_INFO, "No supported video formats found.\n");
1782 return -EINVAL;
1783 }
1784
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001785 atomic_set(&stream->active, 0);
1786
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001787 /* Alternate setting 0 should be the default, yet the XBox Live Vision
1788 * Cam (and possibly other devices) crash or otherwise misbehave if
1789 * they don't receive a SET_INTERFACE request before any other video
1790 * control request.
1791 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001792 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001793
Laurent Pinchart72362422009-02-14 19:26:56 -03001794 /* Set the streaming probe control with default streaming parameters
1795 * retrieved from the device. Webcams that don't suport GET_DEF
1796 * requests on the probe control will just keep their current streaming
1797 * parameters.
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001798 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001799 if (uvc_get_video_ctrl(stream, probe, 1, UVC_GET_DEF) == 0)
1800 uvc_set_video_ctrl(stream, probe, 1);
Laurent Pinchart72362422009-02-14 19:26:56 -03001801
1802 /* Initialize the streaming parameters with the probe control current
1803 * value. This makes sure SET_CUR requests on the streaming commit
1804 * control will always use values retrieved from a successful GET_CUR
1805 * request on the probe control, as required by the UVC specification.
1806 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001807 ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
Laurent Pinchartb482d922009-06-26 11:39:42 -03001808 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001809 return ret;
1810
1811 /* Check if the default format descriptor exists. Use the first
1812 * available format otherwise.
1813 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001814 for (i = stream->nformats; i > 0; --i) {
1815 format = &stream->format[i-1];
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001816 if (format->index == probe->bFormatIndex)
1817 break;
1818 }
1819
1820 if (format->nframes == 0) {
1821 uvc_printk(KERN_INFO, "No frame descriptor found for the "
1822 "default format.\n");
1823 return -EINVAL;
1824 }
1825
1826 /* Zero bFrameIndex might be correct. Stream-based formats (including
1827 * MPEG-2 TS and DV) do not support frames but have a dummy frame
1828 * descriptor with bFrameIndex set to zero. If the default frame
Laurent Pinchart078f8942009-05-06 12:30:30 -03001829 * descriptor is not found, use the first available frame.
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001830 */
1831 for (i = format->nframes; i > 0; --i) {
1832 frame = &format->frame[i-1];
1833 if (frame->bFrameIndex == probe->bFrameIndex)
1834 break;
1835 }
1836
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001837 probe->bFormatIndex = format->index;
1838 probe->bFrameIndex = frame->bFrameIndex;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001839
Laurent Pinchart815adc42012-08-28 18:38:58 -03001840 stream->def_format = format;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001841 stream->cur_format = format;
1842 stream->cur_frame = frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001843
1844 /* Select the video decoding function */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001845 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1846 if (stream->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
1847 stream->decode = uvc_video_decode_isight;
1848 else if (stream->intf->num_altsetting > 1)
1849 stream->decode = uvc_video_decode_isoc;
Laurent Pinchartff924202008-12-28 22:32:29 -03001850 else
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001851 stream->decode = uvc_video_decode_bulk;
Laurent Pinchartff924202008-12-28 22:32:29 -03001852 } else {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001853 if (stream->intf->num_altsetting == 1)
1854 stream->decode = uvc_video_encode_bulk;
Laurent Pinchartff924202008-12-28 22:32:29 -03001855 else {
1856 uvc_printk(KERN_INFO, "Isochronous endpoints are not "
1857 "supported for video output devices.\n");
1858 return -EINVAL;
1859 }
1860 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001861
1862 return 0;
1863}
1864
1865/*
1866 * Enable or disable the video stream.
1867 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001868int uvc_video_enable(struct uvc_streaming *stream, int enable)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001869{
1870 int ret;
1871
1872 if (!enable) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001873 uvc_uninit_video(stream, 1);
Oleksij Rempelb1e43f22014-02-16 06:59:32 -03001874 if (stream->intf->num_altsetting > 1) {
1875 usb_set_interface(stream->dev->udev,
1876 stream->intfnum, 0);
1877 } else {
1878 /* UVC doesn't specify how to inform a bulk-based device
1879 * when the video stream is stopped. Windows sends a
1880 * CLEAR_FEATURE(HALT) request to the video streaming
1881 * bulk endpoint, mimic the same behaviour.
1882 */
1883 unsigned int epnum = stream->header.bEndpointAddress
1884 & USB_ENDPOINT_NUMBER_MASK;
1885 unsigned int dir = stream->header.bEndpointAddress
1886 & USB_ENDPOINT_DIR_MASK;
1887 unsigned int pipe;
1888
1889 pipe = usb_sndbulkpipe(stream->dev->udev, epnum) | dir;
1890 usb_clear_halt(stream->dev->udev, pipe);
1891 }
1892
Laurent Pincharted0ee0c2012-03-27 05:51:00 -03001893 uvc_video_clock_cleanup(stream);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001894 return 0;
1895 }
1896
Laurent Pincharted0ee0c2012-03-27 05:51:00 -03001897 ret = uvc_video_clock_init(stream);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001898 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001899 return ret;
1900
Laurent Pinchart23867b22008-11-12 11:46:43 -03001901 /* Commit the streaming parameters. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001902 ret = uvc_commit_video(stream, &stream->ctrl);
Laurent Pincharted0ee0c2012-03-27 05:51:00 -03001903 if (ret < 0)
1904 goto error_commit;
Laurent Pinchart23867b22008-11-12 11:46:43 -03001905
Laurent Pinchart24c3aae2011-10-25 09:03:42 -03001906 ret = uvc_init_video(stream, GFP_KERNEL);
Laurent Pincharted0ee0c2012-03-27 05:51:00 -03001907 if (ret < 0)
1908 goto error_video;
1909
1910 return 0;
1911
1912error_video:
1913 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1914error_commit:
Laurent Pincharted0ee0c2012-03-27 05:51:00 -03001915 uvc_video_clock_cleanup(stream);
Hans Verkuilf87086e2008-07-18 00:50:58 -03001916
Laurent Pinchart24c3aae2011-10-25 09:03:42 -03001917 return ret;
1918}