blob: c7e69b8f81c9555b5a1296b96355957b8e91089b [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 Pincharta2e35af2009-11-04 11:09:12 -0300122 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) &&
123 stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH &&
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300124 stream->intf->num_altsetting > 1) {
Laurent Pinchart50144ae2009-02-16 17:41:52 -0300125 u32 interval;
126 u32 bandwidth;
127
128 interval = (ctrl->dwFrameInterval > 100000)
129 ? ctrl->dwFrameInterval
130 : frame->dwFrameInterval[0];
131
132 /* Compute a bandwidth estimation by multiplying the frame
133 * size by the number of video frames per second, divide the
134 * result by the number of USB frames (or micro-frames for
135 * high-speed devices) per second and add the UVC header size
136 * (assumed to be 12 bytes long).
137 */
138 bandwidth = frame->wWidth * frame->wHeight / 8 * format->bpp;
139 bandwidth *= 10000000 / interval + 1;
140 bandwidth /= 1000;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300141 if (stream->dev->udev->speed == USB_SPEED_HIGH)
Laurent Pinchart50144ae2009-02-16 17:41:52 -0300142 bandwidth /= 8;
143 bandwidth += 12;
144
Laurent Pinchart9bb72622010-10-03 17:40:29 -0300145 /* The bandwidth estimate is too low for many cameras. Don't use
146 * maximum packet sizes lower than 1024 bytes to try and work
147 * around the problem. According to measurements done on two
148 * different camera models, the value is high enough to get most
149 * resolutions working while not preventing two simultaneous
150 * VGA streams at 15 fps.
151 */
152 bandwidth = max_t(u32, bandwidth, 1024);
153
Laurent Pinchart50144ae2009-02-16 17:41:52 -0300154 ctrl->dwMaxPayloadTransferSize = bandwidth;
155 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300156}
157
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300158static int uvc_get_video_ctrl(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300159 struct uvc_streaming_control *ctrl, int probe, __u8 query)
160{
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300161 __u8 *data;
162 __u16 size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300163 int ret;
164
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300165 size = stream->dev->uvc_version >= 0x0110 ? 34 : 26;
Julia Lawalld2be7642009-09-17 23:44:01 -0300166 if ((stream->dev->quirks & UVC_QUIRK_PROBE_DEF) &&
167 query == UVC_GET_DEF)
168 return -EIO;
169
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300170 data = kmalloc(size, GFP_KERNEL);
171 if (data == NULL)
172 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300173
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300174 ret = __uvc_query_ctrl(stream->dev, query, 0, stream->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300175 probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
Laurent Pinchartb232a012009-10-09 20:55:23 -0300176 size, uvc_timeout_param);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300177
Laurent Pinchartb482d922009-06-26 11:39:42 -0300178 if ((query == UVC_GET_MIN || query == UVC_GET_MAX) && ret == 2) {
Laurent Pinchart44f00792008-11-08 19:14:50 -0300179 /* Some cameras, mostly based on Bison Electronics chipsets,
180 * answer a GET_MIN or GET_MAX request with the wCompQuality
181 * field only.
182 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300183 uvc_warn_once(stream->dev, UVC_WARN_MINMAX, "UVC non "
Laurent Pinchart44f00792008-11-08 19:14:50 -0300184 "compliance - GET_MIN/MAX(PROBE) incorrectly "
185 "supported. Enabling workaround.\n");
Julia Lawallddb0b342009-12-10 17:14:27 -0300186 memset(ctrl, 0, sizeof *ctrl);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300187 ctrl->wCompQuality = le16_to_cpup((__le16 *)data);
188 ret = 0;
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300189 goto out;
Laurent Pinchartb482d922009-06-26 11:39:42 -0300190 } else if (query == UVC_GET_DEF && probe == 1 && ret != size) {
Laurent Pinchart44f00792008-11-08 19:14:50 -0300191 /* Many cameras don't support the GET_DEF request on their
192 * video probe control. Warn once and return, the caller will
193 * fall back to GET_CUR.
194 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300195 uvc_warn_once(stream->dev, UVC_WARN_PROBE_DEF, "UVC non "
Laurent Pinchart44f00792008-11-08 19:14:50 -0300196 "compliance - GET_DEF(PROBE) not supported. "
197 "Enabling workaround.\n");
198 ret = -EIO;
199 goto out;
200 } else if (ret != size) {
201 uvc_printk(KERN_ERR, "Failed to query (%u) UVC %s control : "
202 "%d (exp. %u).\n", query, probe ? "probe" : "commit",
203 ret, size);
204 ret = -EIO;
205 goto out;
206 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300207
208 ctrl->bmHint = le16_to_cpup((__le16 *)&data[0]);
209 ctrl->bFormatIndex = data[2];
210 ctrl->bFrameIndex = data[3];
211 ctrl->dwFrameInterval = le32_to_cpup((__le32 *)&data[4]);
212 ctrl->wKeyFrameRate = le16_to_cpup((__le16 *)&data[8]);
213 ctrl->wPFrameRate = le16_to_cpup((__le16 *)&data[10]);
214 ctrl->wCompQuality = le16_to_cpup((__le16 *)&data[12]);
215 ctrl->wCompWindowSize = le16_to_cpup((__le16 *)&data[14]);
216 ctrl->wDelay = le16_to_cpup((__le16 *)&data[16]);
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300217 ctrl->dwMaxVideoFrameSize = get_unaligned_le32(&data[18]);
218 ctrl->dwMaxPayloadTransferSize = get_unaligned_le32(&data[22]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300219
220 if (size == 34) {
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300221 ctrl->dwClockFrequency = get_unaligned_le32(&data[26]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300222 ctrl->bmFramingInfo = data[30];
223 ctrl->bPreferedVersion = data[31];
224 ctrl->bMinVersion = data[32];
225 ctrl->bMaxVersion = data[33];
226 } else {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300227 ctrl->dwClockFrequency = stream->dev->clock_frequency;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300228 ctrl->bmFramingInfo = 0;
229 ctrl->bPreferedVersion = 0;
230 ctrl->bMinVersion = 0;
231 ctrl->bMaxVersion = 0;
232 }
233
Laurent Pinchart50144ae2009-02-16 17:41:52 -0300234 /* Some broken devices return null or wrong dwMaxVideoFrameSize and
235 * dwMaxPayloadTransferSize fields. Try to get the value from the
236 * format and frame descriptors.
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300237 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300238 uvc_fixup_video_ctrl(stream, ctrl);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300239 ret = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300240
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300241out:
242 kfree(data);
243 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300244}
245
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300246static int uvc_set_video_ctrl(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300247 struct uvc_streaming_control *ctrl, int probe)
248{
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300249 __u8 *data;
250 __u16 size;
251 int ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300252
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300253 size = stream->dev->uvc_version >= 0x0110 ? 34 : 26;
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300254 data = kzalloc(size, GFP_KERNEL);
255 if (data == NULL)
256 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300257
258 *(__le16 *)&data[0] = cpu_to_le16(ctrl->bmHint);
259 data[2] = ctrl->bFormatIndex;
260 data[3] = ctrl->bFrameIndex;
261 *(__le32 *)&data[4] = cpu_to_le32(ctrl->dwFrameInterval);
262 *(__le16 *)&data[8] = cpu_to_le16(ctrl->wKeyFrameRate);
263 *(__le16 *)&data[10] = cpu_to_le16(ctrl->wPFrameRate);
264 *(__le16 *)&data[12] = cpu_to_le16(ctrl->wCompQuality);
265 *(__le16 *)&data[14] = cpu_to_le16(ctrl->wCompWindowSize);
266 *(__le16 *)&data[16] = cpu_to_le16(ctrl->wDelay);
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300267 put_unaligned_le32(ctrl->dwMaxVideoFrameSize, &data[18]);
268 put_unaligned_le32(ctrl->dwMaxPayloadTransferSize, &data[22]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300269
270 if (size == 34) {
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300271 put_unaligned_le32(ctrl->dwClockFrequency, &data[26]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300272 data[30] = ctrl->bmFramingInfo;
273 data[31] = ctrl->bPreferedVersion;
274 data[32] = ctrl->bMinVersion;
275 data[33] = ctrl->bMaxVersion;
276 }
277
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300278 ret = __uvc_query_ctrl(stream->dev, UVC_SET_CUR, 0, stream->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300279 probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
Laurent Pinchartb232a012009-10-09 20:55:23 -0300280 size, uvc_timeout_param);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300281 if (ret != size) {
282 uvc_printk(KERN_ERR, "Failed to set UVC %s control : "
283 "%d (exp. %u).\n", probe ? "probe" : "commit",
284 ret, size);
285 ret = -EIO;
286 }
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300287
288 kfree(data);
289 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300290}
291
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300292int uvc_probe_video(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300293 struct uvc_streaming_control *probe)
294{
295 struct uvc_streaming_control probe_min, probe_max;
296 __u16 bandwidth;
297 unsigned int i;
298 int ret;
299
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300300 /* Perform probing. The device should adjust the requested values
301 * according to its capabilities. However, some devices, namely the
302 * first generation UVC Logitech webcams, don't implement the Video
303 * Probe control properly, and just return the needed bandwidth. For
304 * that reason, if the needed bandwidth exceeds the maximum available
305 * bandwidth, try to lower the quality.
306 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300307 ret = uvc_set_video_ctrl(stream, probe, 1);
308 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300309 goto done;
310
311 /* Get the minimum and maximum values for compression settings. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300312 if (!(stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX)) {
313 ret = uvc_get_video_ctrl(stream, &probe_min, 1, UVC_GET_MIN);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300314 if (ret < 0)
315 goto done;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300316 ret = uvc_get_video_ctrl(stream, &probe_max, 1, UVC_GET_MAX);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300317 if (ret < 0)
318 goto done;
319
320 probe->wCompQuality = probe_max.wCompQuality;
321 }
322
323 for (i = 0; i < 2; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300324 ret = uvc_set_video_ctrl(stream, probe, 1);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300325 if (ret < 0)
326 goto done;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300327 ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300328 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300329 goto done;
330
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300331 if (stream->intf->num_altsetting == 1)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300332 break;
333
334 bandwidth = probe->dwMaxPayloadTransferSize;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300335 if (bandwidth <= stream->maxpsize)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300336 break;
337
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300338 if (stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300339 ret = -ENOSPC;
340 goto done;
341 }
342
343 /* TODO: negotiate compression parameters */
344 probe->wKeyFrameRate = probe_min.wKeyFrameRate;
345 probe->wPFrameRate = probe_min.wPFrameRate;
346 probe->wCompQuality = probe_max.wCompQuality;
347 probe->wCompWindowSize = probe_min.wCompWindowSize;
348 }
349
350done:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300351 return ret;
352}
353
Laurent Pinchart0c6a3b22011-11-03 07:22:39 -0300354static int uvc_commit_video(struct uvc_streaming *stream,
355 struct uvc_streaming_control *probe)
Laurent Pinchart44f00792008-11-08 19:14:50 -0300356{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300357 return uvc_set_video_ctrl(stream, probe, 0);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300358}
359
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300360/* -----------------------------------------------------------------------------
361 * Clocks and timestamps
362 */
363
364static void
365uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf,
366 const __u8 *data, int len)
367{
368 struct uvc_clock_sample *sample;
369 unsigned int header_size;
370 bool has_pts = false;
371 bool has_scr = false;
372 unsigned long flags;
373 struct timespec ts;
374 u16 host_sof;
375 u16 dev_sof;
376
377 switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) {
378 case UVC_STREAM_PTS | UVC_STREAM_SCR:
379 header_size = 12;
380 has_pts = true;
381 has_scr = true;
382 break;
383 case UVC_STREAM_PTS:
384 header_size = 6;
385 has_pts = true;
386 break;
387 case UVC_STREAM_SCR:
388 header_size = 8;
389 has_scr = true;
390 break;
391 default:
392 header_size = 2;
393 break;
394 }
395
396 /* Check for invalid headers. */
397 if (len < header_size)
398 return;
399
400 /* Extract the timestamps:
401 *
402 * - store the frame PTS in the buffer structure
403 * - if the SCR field is present, retrieve the host SOF counter and
404 * kernel timestamps and store them with the SCR STC and SOF fields
405 * in the ring buffer
406 */
407 if (has_pts && buf != NULL)
408 buf->pts = get_unaligned_le32(&data[2]);
409
410 if (!has_scr)
411 return;
412
413 /* To limit the amount of data, drop SCRs with an SOF identical to the
414 * previous one.
415 */
416 dev_sof = get_unaligned_le16(&data[header_size - 2]);
417 if (dev_sof == stream->clock.last_sof)
418 return;
419
420 stream->clock.last_sof = dev_sof;
421
422 host_sof = usb_get_current_frame_number(stream->dev->udev);
423 ktime_get_ts(&ts);
424
425 /* The UVC specification allows device implementations that can't obtain
426 * the USB frame number to keep their own frame counters as long as they
427 * match the size and frequency of the frame number associated with USB
428 * SOF tokens. The SOF values sent by such devices differ from the USB
429 * SOF tokens by a fixed offset that needs to be estimated and accounted
430 * for to make timestamp recovery as accurate as possible.
431 *
432 * The offset is estimated the first time a device SOF value is received
433 * as the difference between the host and device SOF values. As the two
434 * SOF values can differ slightly due to transmission delays, consider
435 * that the offset is null if the difference is not higher than 10 ms
436 * (negative differences can not happen and are thus considered as an
437 * offset). The video commit control wDelay field should be used to
438 * compute a dynamic threshold instead of using a fixed 10 ms value, but
439 * devices don't report reliable wDelay values.
440 *
441 * See uvc_video_clock_host_sof() for an explanation regarding why only
442 * the 8 LSBs of the delta are kept.
443 */
444 if (stream->clock.sof_offset == (u16)-1) {
445 u16 delta_sof = (host_sof - dev_sof) & 255;
446 if (delta_sof >= 10)
447 stream->clock.sof_offset = delta_sof;
448 else
449 stream->clock.sof_offset = 0;
450 }
451
452 dev_sof = (dev_sof + stream->clock.sof_offset) & 2047;
453
454 spin_lock_irqsave(&stream->clock.lock, flags);
455
456 sample = &stream->clock.samples[stream->clock.head];
457 sample->dev_stc = get_unaligned_le32(&data[header_size - 6]);
458 sample->dev_sof = dev_sof;
459 sample->host_sof = host_sof;
460 sample->host_ts = ts;
461
462 /* Update the sliding window head and count. */
463 stream->clock.head = (stream->clock.head + 1) % stream->clock.size;
464
465 if (stream->clock.count < stream->clock.size)
466 stream->clock.count++;
467
468 spin_unlock_irqrestore(&stream->clock.lock, flags);
469}
470
471static int uvc_video_clock_init(struct uvc_streaming *stream)
472{
473 struct uvc_clock *clock = &stream->clock;
474
475 spin_lock_init(&clock->lock);
476 clock->head = 0;
477 clock->count = 0;
478 clock->size = 32;
479 clock->last_sof = -1;
480 clock->sof_offset = -1;
481
482 clock->samples = kmalloc(clock->size * sizeof(*clock->samples),
483 GFP_KERNEL);
484 if (clock->samples == NULL)
485 return -ENOMEM;
486
487 return 0;
488}
489
490static void uvc_video_clock_cleanup(struct uvc_streaming *stream)
491{
492 kfree(stream->clock.samples);
493 stream->clock.samples = NULL;
494}
495
496/*
497 * uvc_video_clock_host_sof - Return the host SOF value for a clock sample
498 *
499 * Host SOF counters reported by usb_get_current_frame_number() usually don't
500 * cover the whole 11-bits SOF range (0-2047) but are limited to the HCI frame
501 * schedule window. They can be limited to 8, 9 or 10 bits depending on the host
502 * controller and its configuration.
503 *
504 * We thus need to recover the SOF value corresponding to the host frame number.
505 * As the device and host frame numbers are sampled in a short interval, the
506 * difference between their values should be equal to a small delta plus an
507 * integer multiple of 256 caused by the host frame number limited precision.
508 *
509 * To obtain the recovered host SOF value, compute the small delta by masking
510 * the high bits of the host frame counter and device SOF difference and add it
511 * to the device SOF value.
512 */
513static u16 uvc_video_clock_host_sof(const struct uvc_clock_sample *sample)
514{
515 /* The delta value can be negative. */
516 s8 delta_sof;
517
518 delta_sof = (sample->host_sof - sample->dev_sof) & 255;
519
520 return (sample->dev_sof + delta_sof) & 2047;
521}
522
523/*
524 * uvc_video_clock_update - Update the buffer timestamp
525 *
526 * This function converts the buffer PTS timestamp to the host clock domain by
527 * going through the USB SOF clock domain and stores the result in the V4L2
528 * buffer timestamp field.
529 *
530 * The relationship between the device clock and the host clock isn't known.
531 * However, the device and the host share the common USB SOF clock which can be
532 * used to recover that relationship.
533 *
534 * The relationship between the device clock and the USB SOF clock is considered
535 * to be linear over the clock samples sliding window and is given by
536 *
537 * SOF = m * PTS + p
538 *
539 * Several methods to compute the slope (m) and intercept (p) can be used. As
540 * the clock drift should be small compared to the sliding window size, we
541 * assume that the line that goes through the points at both ends of the window
542 * is a good approximation. Naming those points P1 and P2, we get
543 *
544 * SOF = (SOF2 - SOF1) / (STC2 - STC1) * PTS
545 * + (SOF1 * STC2 - SOF2 * STC1) / (STC2 - STC1)
546 *
547 * or
548 *
549 * SOF = ((SOF2 - SOF1) * PTS + SOF1 * STC2 - SOF2 * STC1) / (STC2 - STC1) (1)
550 *
551 * to avoid loosing precision in the division. Similarly, the host timestamp is
552 * computed with
553 *
554 * TS = ((TS2 - TS1) * PTS + TS1 * SOF2 - TS2 * SOF1) / (SOF2 - SOF1) (2)
555 *
556 * SOF values are coded on 11 bits by USB. We extend their precision with 16
557 * decimal bits, leading to a 11.16 coding.
558 *
559 * TODO: To avoid surprises with device clock values, PTS/STC timestamps should
560 * be normalized using the nominal device clock frequency reported through the
561 * UVC descriptors.
562 *
563 * Both the PTS/STC and SOF counters roll over, after a fixed but device
564 * specific amount of time for PTS/STC and after 2048ms for SOF. As long as the
565 * sliding window size is smaller than the rollover period, differences computed
566 * on unsigned integers will produce the correct result. However, the p term in
567 * the linear relations will be miscomputed.
568 *
569 * To fix the issue, we subtract a constant from the PTS and STC values to bring
570 * PTS to half the 32 bit STC range. The sliding window STC values then fit into
571 * the 32 bit range without any rollover.
572 *
573 * Similarly, we add 2048 to the device SOF values to make sure that the SOF
574 * computed by (1) will never be smaller than 0. This offset is then compensated
575 * by adding 2048 to the SOF values used in (2). However, this doesn't prevent
576 * rollovers between (1) and (2): the SOF value computed by (1) can be slightly
577 * lower than 4096, and the host SOF counters can have rolled over to 2048. This
578 * case is handled by subtracting 2048 from the SOF value if it exceeds the host
579 * SOF value at the end of the sliding window.
580 *
581 * Finally we subtract a constant from the host timestamps to bring the first
582 * timestamp of the sliding window to 1s.
583 */
584void uvc_video_clock_update(struct uvc_streaming *stream,
585 struct v4l2_buffer *v4l2_buf,
586 struct uvc_buffer *buf)
587{
588 struct uvc_clock *clock = &stream->clock;
589 struct uvc_clock_sample *first;
590 struct uvc_clock_sample *last;
591 unsigned long flags;
592 struct timespec ts;
593 u32 delta_stc;
594 u32 y1, y2;
595 u32 x1, x2;
596 u32 mean;
597 u32 sof;
598 u32 div;
599 u32 rem;
600 u64 y;
601
602 spin_lock_irqsave(&clock->lock, flags);
603
604 if (clock->count < clock->size)
605 goto done;
606
607 first = &clock->samples[clock->head];
608 last = &clock->samples[(clock->head - 1) % clock->size];
609
610 /* First step, PTS to SOF conversion. */
611 delta_stc = buf->pts - (1UL << 31);
612 x1 = first->dev_stc - delta_stc;
613 x2 = last->dev_stc - delta_stc;
614 y1 = (first->dev_sof + 2048) << 16;
615 y2 = (last->dev_sof + 2048) << 16;
616
617 if (y2 < y1)
618 y2 += 2048 << 16;
619
620 y = (u64)(y2 - y1) * (1ULL << 31) + (u64)y1 * (u64)x2
621 - (u64)y2 * (u64)x1;
622 y = div_u64(y, x2 - x1);
623
624 sof = y;
625
626 uvc_trace(UVC_TRACE_CLOCK, "%s: PTS %u y %llu.%06llu SOF %u.%06llu "
627 "(x1 %u x2 %u y1 %u y2 %u SOF offset %u)\n",
628 stream->dev->name, buf->pts,
629 y >> 16, div_u64((y & 0xffff) * 1000000, 65536),
630 sof >> 16, div_u64(((u64)sof & 0xffff) * 1000000LLU, 65536),
631 x1, x2, y1, y2, clock->sof_offset);
632
633 /* Second step, SOF to host clock conversion. */
634 ts = timespec_sub(last->host_ts, first->host_ts);
635 x1 = (uvc_video_clock_host_sof(first) + 2048) << 16;
636 x2 = (uvc_video_clock_host_sof(last) + 2048) << 16;
637 y1 = NSEC_PER_SEC;
638 y2 = (ts.tv_sec + 1) * NSEC_PER_SEC + ts.tv_nsec;
639
640 if (x2 < x1)
641 x2 += 2048 << 16;
642
643 /* Interpolated and host SOF timestamps can wrap around at slightly
644 * different times. Handle this by adding or removing 2048 to or from
645 * the computed SOF value to keep it close to the SOF samples mean
646 * value.
647 */
648 mean = (x1 + x2) / 2;
649 if (mean - (1024 << 16) > sof)
650 sof += 2048 << 16;
651 else if (sof > mean + (1024 << 16))
652 sof -= 2048 << 16;
653
654 y = (u64)(y2 - y1) * (u64)sof + (u64)y1 * (u64)x2
655 - (u64)y2 * (u64)x1;
656 y = div_u64(y, x2 - x1);
657
658 div = div_u64_rem(y, NSEC_PER_SEC, &rem);
659 ts.tv_sec = first->host_ts.tv_sec - 1 + div;
660 ts.tv_nsec = first->host_ts.tv_nsec + rem;
661 if (ts.tv_nsec >= NSEC_PER_SEC) {
662 ts.tv_sec++;
663 ts.tv_nsec -= NSEC_PER_SEC;
664 }
665
666 uvc_trace(UVC_TRACE_CLOCK, "%s: SOF %u.%06llu y %llu ts %lu.%06lu "
667 "buf ts %lu.%06lu (x1 %u/%u/%u x2 %u/%u/%u y1 %u y2 %u)\n",
668 stream->dev->name,
669 sof >> 16, div_u64(((u64)sof & 0xffff) * 1000000LLU, 65536),
670 y, ts.tv_sec, ts.tv_nsec / NSEC_PER_USEC,
671 v4l2_buf->timestamp.tv_sec, v4l2_buf->timestamp.tv_usec,
672 x1, first->host_sof, first->dev_sof,
673 x2, last->host_sof, last->dev_sof, y1, y2);
674
675 /* Update the V4L2 buffer. */
676 v4l2_buf->timestamp.tv_sec = ts.tv_sec;
677 v4l2_buf->timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
678
679done:
680 spin_unlock_irqrestore(&stream->clock.lock, flags);
681}
682
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300683/* ------------------------------------------------------------------------
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300684 * Stream statistics
685 */
686
687static void uvc_video_stats_decode(struct uvc_streaming *stream,
688 const __u8 *data, int len)
689{
690 unsigned int header_size;
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300691 bool has_pts = false;
692 bool has_scr = false;
693 u16 uninitialized_var(scr_sof);
694 u32 uninitialized_var(scr_stc);
695 u32 uninitialized_var(pts);
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300696
697 if (stream->stats.stream.nb_frames == 0 &&
698 stream->stats.frame.nb_packets == 0)
699 ktime_get_ts(&stream->stats.stream.start_ts);
700
701 switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) {
702 case UVC_STREAM_PTS | UVC_STREAM_SCR:
703 header_size = 12;
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300704 has_pts = true;
705 has_scr = true;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300706 break;
707 case UVC_STREAM_PTS:
708 header_size = 6;
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300709 has_pts = true;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300710 break;
711 case UVC_STREAM_SCR:
712 header_size = 8;
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300713 has_scr = true;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300714 break;
715 default:
716 header_size = 2;
717 break;
718 }
719
720 /* Check for invalid headers. */
721 if (len < header_size || data[0] < header_size) {
722 stream->stats.frame.nb_invalid++;
723 return;
724 }
725
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300726 /* Extract the timestamps. */
727 if (has_pts)
728 pts = get_unaligned_le32(&data[2]);
729
730 if (has_scr) {
731 scr_stc = get_unaligned_le32(&data[header_size - 6]);
732 scr_sof = get_unaligned_le16(&data[header_size - 2]);
733 }
734
735 /* Is PTS constant through the whole frame ? */
736 if (has_pts && stream->stats.frame.nb_pts) {
737 if (stream->stats.frame.pts != pts) {
738 stream->stats.frame.nb_pts_diffs++;
739 stream->stats.frame.last_pts_diff =
740 stream->stats.frame.nb_packets;
741 }
742 }
743
744 if (has_pts) {
745 stream->stats.frame.nb_pts++;
746 stream->stats.frame.pts = pts;
747 }
748
749 /* Do all frames have a PTS in their first non-empty packet, or before
750 * their first empty packet ?
751 */
752 if (stream->stats.frame.size == 0) {
753 if (len > header_size)
754 stream->stats.frame.has_initial_pts = has_pts;
755 if (len == header_size && has_pts)
756 stream->stats.frame.has_early_pts = true;
757 }
758
759 /* Do the SCR.STC and SCR.SOF fields vary through the frame ? */
760 if (has_scr && stream->stats.frame.nb_scr) {
761 if (stream->stats.frame.scr_stc != scr_stc)
762 stream->stats.frame.nb_scr_diffs++;
763 }
764
765 if (has_scr) {
766 /* Expand the SOF counter to 32 bits and store its value. */
767 if (stream->stats.stream.nb_frames > 0 ||
768 stream->stats.frame.nb_scr > 0)
769 stream->stats.stream.scr_sof_count +=
770 (scr_sof - stream->stats.stream.scr_sof) % 2048;
771 stream->stats.stream.scr_sof = scr_sof;
772
773 stream->stats.frame.nb_scr++;
774 stream->stats.frame.scr_stc = scr_stc;
775 stream->stats.frame.scr_sof = scr_sof;
776
777 if (scr_sof < stream->stats.stream.min_sof)
778 stream->stats.stream.min_sof = scr_sof;
779 if (scr_sof > stream->stats.stream.max_sof)
780 stream->stats.stream.max_sof = scr_sof;
781 }
782
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300783 /* Record the first non-empty packet number. */
784 if (stream->stats.frame.size == 0 && len > header_size)
785 stream->stats.frame.first_data = stream->stats.frame.nb_packets;
786
787 /* Update the frame size. */
788 stream->stats.frame.size += len - header_size;
789
790 /* Update the packets counters. */
791 stream->stats.frame.nb_packets++;
792 if (len > header_size)
793 stream->stats.frame.nb_empty++;
794
795 if (data[1] & UVC_STREAM_ERR)
796 stream->stats.frame.nb_errors++;
797}
798
799static void uvc_video_stats_update(struct uvc_streaming *stream)
800{
801 struct uvc_stats_frame *frame = &stream->stats.frame;
802
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300803 uvc_trace(UVC_TRACE_STATS, "frame %u stats: %u/%u/%u packets, "
804 "%u/%u/%u pts (%searly %sinitial), %u/%u scr, "
805 "last pts/stc/sof %u/%u/%u\n",
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300806 stream->sequence, frame->first_data,
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300807 frame->nb_packets - frame->nb_empty, frame->nb_packets,
808 frame->nb_pts_diffs, frame->last_pts_diff, frame->nb_pts,
809 frame->has_early_pts ? "" : "!",
810 frame->has_initial_pts ? "" : "!",
811 frame->nb_scr_diffs, frame->nb_scr,
812 frame->pts, frame->scr_stc, frame->scr_sof);
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300813
814 stream->stats.stream.nb_frames++;
815 stream->stats.stream.nb_packets += stream->stats.frame.nb_packets;
816 stream->stats.stream.nb_empty += stream->stats.frame.nb_empty;
817 stream->stats.stream.nb_errors += stream->stats.frame.nb_errors;
818 stream->stats.stream.nb_invalid += stream->stats.frame.nb_invalid;
819
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300820 if (frame->has_early_pts)
821 stream->stats.stream.nb_pts_early++;
822 if (frame->has_initial_pts)
823 stream->stats.stream.nb_pts_initial++;
824 if (frame->last_pts_diff <= frame->first_data)
825 stream->stats.stream.nb_pts_constant++;
826 if (frame->nb_scr >= frame->nb_packets - frame->nb_empty)
827 stream->stats.stream.nb_scr_count_ok++;
828 if (frame->nb_scr_diffs + 1 == frame->nb_scr)
829 stream->stats.stream.nb_scr_diffs_ok++;
830
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300831 memset(&stream->stats.frame, 0, sizeof(stream->stats.frame));
832}
833
834size_t uvc_video_stats_dump(struct uvc_streaming *stream, char *buf,
835 size_t size)
836{
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300837 unsigned int scr_sof_freq;
838 unsigned int duration;
839 struct timespec ts;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300840 size_t count = 0;
841
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300842 ts.tv_sec = stream->stats.stream.stop_ts.tv_sec
843 - stream->stats.stream.start_ts.tv_sec;
844 ts.tv_nsec = stream->stats.stream.stop_ts.tv_nsec
845 - stream->stats.stream.start_ts.tv_nsec;
846 if (ts.tv_nsec < 0) {
847 ts.tv_sec--;
848 ts.tv_nsec += 1000000000;
849 }
850
851 /* Compute the SCR.SOF frequency estimate. At the nominal 1kHz SOF
852 * frequency this will not overflow before more than 1h.
853 */
854 duration = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
855 if (duration != 0)
856 scr_sof_freq = stream->stats.stream.scr_sof_count * 1000
857 / duration;
858 else
859 scr_sof_freq = 0;
860
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300861 count += scnprintf(buf + count, size - count,
862 "frames: %u\npackets: %u\nempty: %u\n"
863 "errors: %u\ninvalid: %u\n",
864 stream->stats.stream.nb_frames,
865 stream->stats.stream.nb_packets,
866 stream->stats.stream.nb_empty,
867 stream->stats.stream.nb_errors,
868 stream->stats.stream.nb_invalid);
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300869 count += scnprintf(buf + count, size - count,
870 "pts: %u early, %u initial, %u ok\n",
871 stream->stats.stream.nb_pts_early,
872 stream->stats.stream.nb_pts_initial,
873 stream->stats.stream.nb_pts_constant);
874 count += scnprintf(buf + count, size - count,
875 "scr: %u count ok, %u diff ok\n",
876 stream->stats.stream.nb_scr_count_ok,
877 stream->stats.stream.nb_scr_diffs_ok);
878 count += scnprintf(buf + count, size - count,
879 "sof: %u <= sof <= %u, freq %u.%03u kHz\n",
880 stream->stats.stream.min_sof,
881 stream->stats.stream.max_sof,
882 scr_sof_freq / 1000, scr_sof_freq % 1000);
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300883
884 return count;
885}
886
887static void uvc_video_stats_start(struct uvc_streaming *stream)
888{
889 memset(&stream->stats, 0, sizeof(stream->stats));
Laurent Pinchart25738cb2011-11-03 12:30:17 -0300890 stream->stats.stream.min_sof = 2048;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300891}
892
893static void uvc_video_stats_stop(struct uvc_streaming *stream)
894{
895 ktime_get_ts(&stream->stats.stream.stop_ts);
896}
897
898/* ------------------------------------------------------------------------
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300899 * Video codecs
900 */
901
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300902/* Video payload decoding is handled by uvc_video_decode_start(),
903 * uvc_video_decode_data() and uvc_video_decode_end().
904 *
905 * uvc_video_decode_start is called with URB data at the start of a bulk or
906 * isochronous payload. It processes header data and returns the header size
907 * in bytes if successful. If an error occurs, it returns a negative error
908 * code. The following error codes have special meanings.
909 *
910 * - EAGAIN informs the caller that the current video buffer should be marked
911 * as done, and that the function should be called again with the same data
912 * and a new video buffer. This is used when end of frame conditions can be
913 * reliably detected at the beginning of the next frame only.
914 *
915 * If an error other than -EAGAIN is returned, the caller will drop the current
916 * payload. No call to uvc_video_decode_data and uvc_video_decode_end will be
917 * made until the next payload. -ENODATA can be used to drop the current
918 * payload if no other error code is appropriate.
919 *
920 * uvc_video_decode_data is called for every URB with URB data. It copies the
921 * data to the video buffer.
922 *
923 * uvc_video_decode_end is called with header data at the end of a bulk or
924 * isochronous payload. It performs any additional header data processing and
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300925 * returns 0 or a negative error code if an error occurred. As header data have
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300926 * already been processed by uvc_video_decode_start, this functions isn't
927 * required to perform sanity checks a second time.
928 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300929 * For isochronous transfers where a payload is always transferred in a single
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300930 * URB, the three functions will be called in a row.
931 *
932 * To let the decoder process header data and update its internal state even
933 * when no video buffer is available, uvc_video_decode_start must be prepared
934 * to be called with a NULL buf parameter. uvc_video_decode_data and
935 * uvc_video_decode_end will never be called with a NULL buffer.
936 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300937static int uvc_video_decode_start(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300938 struct uvc_buffer *buf, const __u8 *data, int len)
939{
940 __u8 fid;
941
942 /* Sanity checks:
943 * - packet must be at least 2 bytes long
944 * - bHeaderLength value must be at least 2 bytes (see above)
945 * - bHeaderLength value can't be larger than the packet size.
946 */
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300947 if (len < 2 || data[0] < 2 || data[0] > len) {
948 stream->stats.frame.nb_invalid++;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300949 return -EINVAL;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300950 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300951
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300952 fid = data[1] & UVC_STREAM_FID;
953
Laurent Pinchart650b95f2010-10-02 11:06:05 -0300954 /* Increase the sequence number regardless of any buffer states, so
955 * that discontinuous sequence numbers always indicate lost frames.
956 */
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300957 if (stream->last_fid != fid) {
Laurent Pinchart650b95f2010-10-02 11:06:05 -0300958 stream->sequence++;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300959 if (stream->sequence)
960 uvc_video_stats_update(stream);
961 }
962
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300963 uvc_video_clock_decode(stream, buf, data, len);
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300964 uvc_video_stats_decode(stream, data, len);
Laurent Pinchart650b95f2010-10-02 11:06:05 -0300965
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300966 /* Store the payload FID bit and return immediately when the buffer is
967 * NULL.
968 */
969 if (buf == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300970 stream->last_fid = fid;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300971 return -ENODATA;
972 }
973
Laurent Pinchart3afedb92011-11-03 07:24:34 -0300974 /* Mark the buffer as bad if the error bit is set. */
975 if (data[1] & UVC_STREAM_ERR) {
976 uvc_trace(UVC_TRACE_FRAME, "Marking buffer as bad (error bit "
977 "set).\n");
978 buf->error = 1;
979 }
980
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300981 /* Synchronize to the input stream by waiting for the FID bit to be
982 * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE.
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300983 * stream->last_fid is initialized to -1, so the first isochronous
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300984 * frame will always be in sync.
985 *
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300986 * If the device doesn't toggle the FID bit, invert stream->last_fid
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300987 * when the EOF bit is set to force synchronisation on the next packet.
988 */
989 if (buf->state != UVC_BUF_STATE_ACTIVE) {
Laurent Pinchart310fe522009-12-09 22:57:48 -0300990 struct timespec ts;
991
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300992 if (fid == stream->last_fid) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300993 uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of "
994 "sync).\n");
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300995 if ((stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) &&
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300996 (data[1] & UVC_STREAM_EOF))
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300997 stream->last_fid ^= UVC_STREAM_FID;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300998 return -ENODATA;
999 }
1000
Laurent Pinchart310fe522009-12-09 22:57:48 -03001001 if (uvc_clock_param == CLOCK_MONOTONIC)
1002 ktime_get_ts(&ts);
1003 else
1004 ktime_get_real_ts(&ts);
1005
Laurent Pinchart6998b6f2011-10-24 11:53:59 -03001006 buf->buf.v4l2_buf.sequence = stream->sequence;
1007 buf->buf.v4l2_buf.timestamp.tv_sec = ts.tv_sec;
1008 buf->buf.v4l2_buf.timestamp.tv_usec =
1009 ts.tv_nsec / NSEC_PER_USEC;
Laurent Pinchart310fe522009-12-09 22:57:48 -03001010
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001011 /* TODO: Handle PTS and SCR. */
1012 buf->state = UVC_BUF_STATE_ACTIVE;
1013 }
1014
1015 /* Mark the buffer as done if we're at the beginning of a new frame.
1016 * End of frame detection is better implemented by checking the EOF
1017 * bit (FID bit toggling is delayed by one frame compared to the EOF
1018 * bit), but some devices don't set the bit at end of frame (and the
1019 * last payload can be lost anyway). We thus must check if the FID has
1020 * been toggled.
1021 *
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001022 * stream->last_fid is initialized to -1, so the first isochronous
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001023 * frame will never trigger an end of frame detection.
1024 *
1025 * Empty buffers (bytesused == 0) don't trigger end of frame detection
1026 * as it doesn't make sense to return an empty buffer. This also
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03001027 * avoids detecting end of frame conditions at FID toggling if the
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001028 * previous payload had the EOF bit set.
1029 */
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001030 if (fid != stream->last_fid && buf->bytesused != 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001031 uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit "
1032 "toggled).\n");
Laurent Pinchartd7c0d432009-12-16 21:20:45 -03001033 buf->state = UVC_BUF_STATE_READY;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001034 return -EAGAIN;
1035 }
1036
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001037 stream->last_fid = fid;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001038
1039 return data[0];
1040}
1041
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001042static void uvc_video_decode_data(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001043 struct uvc_buffer *buf, const __u8 *data, int len)
1044{
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001045 unsigned int maxlen, nbytes;
1046 void *mem;
1047
1048 if (len <= 0)
1049 return;
1050
1051 /* Copy the video data to the buffer. */
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001052 maxlen = buf->length - buf->bytesused;
1053 mem = buf->mem + buf->bytesused;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001054 nbytes = min((unsigned int)len, maxlen);
1055 memcpy(mem, data, nbytes);
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001056 buf->bytesused += nbytes;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001057
1058 /* Complete the current frame if the buffer size was exceeded. */
1059 if (len > maxlen) {
1060 uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n");
Laurent Pinchartd7c0d432009-12-16 21:20:45 -03001061 buf->state = UVC_BUF_STATE_READY;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001062 }
1063}
1064
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001065static void uvc_video_decode_end(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001066 struct uvc_buffer *buf, const __u8 *data, int len)
1067{
1068 /* Mark the buffer as done if the EOF marker is set. */
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001069 if (data[1] & UVC_STREAM_EOF && buf->bytesused != 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001070 uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n");
1071 if (data[0] == len)
1072 uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n");
Laurent Pinchartd7c0d432009-12-16 21:20:45 -03001073 buf->state = UVC_BUF_STATE_READY;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001074 if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID)
1075 stream->last_fid ^= UVC_STREAM_FID;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001076 }
1077}
1078
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03001079/* Video payload encoding is handled by uvc_video_encode_header() and
1080 * uvc_video_encode_data(). Only bulk transfers are currently supported.
1081 *
1082 * uvc_video_encode_header is called at the start of a payload. It adds header
1083 * data to the transfer buffer and returns the header size. As the only known
1084 * UVC output device transfers a whole frame in a single payload, the EOF bit
1085 * is always set in the header.
1086 *
1087 * uvc_video_encode_data is called for every URB and copies the data from the
1088 * video buffer to the transfer buffer.
1089 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001090static int uvc_video_encode_header(struct uvc_streaming *stream,
Laurent Pinchartff924202008-12-28 22:32:29 -03001091 struct uvc_buffer *buf, __u8 *data, int len)
1092{
1093 data[0] = 2; /* Header length */
1094 data[1] = UVC_STREAM_EOH | UVC_STREAM_EOF
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001095 | (stream->last_fid & UVC_STREAM_FID);
Laurent Pinchartff924202008-12-28 22:32:29 -03001096 return 2;
1097}
1098
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001099static int uvc_video_encode_data(struct uvc_streaming *stream,
Laurent Pinchartff924202008-12-28 22:32:29 -03001100 struct uvc_buffer *buf, __u8 *data, int len)
1101{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001102 struct uvc_video_queue *queue = &stream->queue;
Laurent Pinchartff924202008-12-28 22:32:29 -03001103 unsigned int nbytes;
1104 void *mem;
1105
1106 /* Copy video data to the URB buffer. */
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001107 mem = buf->mem + queue->buf_used;
1108 nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001109 nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size,
Laurent Pinchartff924202008-12-28 22:32:29 -03001110 nbytes);
1111 memcpy(data, mem, nbytes);
1112
1113 queue->buf_used += nbytes;
1114
1115 return nbytes;
1116}
1117
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001118/* ------------------------------------------------------------------------
1119 * URB handling
1120 */
1121
1122/*
1123 * Completion handler for video URBs.
1124 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001125static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
1126 struct uvc_buffer *buf)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001127{
1128 u8 *mem;
1129 int ret, i;
1130
1131 for (i = 0; i < urb->number_of_packets; ++i) {
1132 if (urb->iso_frame_desc[i].status < 0) {
1133 uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
1134 "lost (%d).\n", urb->iso_frame_desc[i].status);
Laurent Pinchart9bde9f22010-06-17 06:52:37 -03001135 /* Mark the buffer as faulty. */
1136 if (buf != NULL)
1137 buf->error = 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001138 continue;
1139 }
1140
1141 /* Decode the payload header. */
1142 mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1143 do {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001144 ret = uvc_video_decode_start(stream, buf, mem,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001145 urb->iso_frame_desc[i].actual_length);
1146 if (ret == -EAGAIN)
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001147 buf = uvc_queue_next_buffer(&stream->queue,
1148 buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001149 } while (ret == -EAGAIN);
1150
1151 if (ret < 0)
1152 continue;
1153
1154 /* Decode the payload data. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001155 uvc_video_decode_data(stream, buf, mem + ret,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001156 urb->iso_frame_desc[i].actual_length - ret);
1157
1158 /* Process the header again. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001159 uvc_video_decode_end(stream, buf, mem,
Laurent Pinchart08ebd002008-08-29 17:37:44 -03001160 urb->iso_frame_desc[i].actual_length);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001161
Laurent Pinchart9bde9f22010-06-17 06:52:37 -03001162 if (buf->state == UVC_BUF_STATE_READY) {
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001163 if (buf->length != buf->bytesused &&
Laurent Pinchart9bde9f22010-06-17 06:52:37 -03001164 !(stream->cur_format->flags &
1165 UVC_FMT_FLAG_COMPRESSED))
1166 buf->error = 1;
1167
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001168 buf = uvc_queue_next_buffer(&stream->queue, buf);
Laurent Pinchart9bde9f22010-06-17 06:52:37 -03001169 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001170 }
1171}
1172
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001173static void uvc_video_decode_bulk(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 len, ret;
1178
Laurent Pinchartc90e7772009-02-14 19:39:08 -03001179 if (urb->actual_length == 0)
1180 return;
1181
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001182 mem = urb->transfer_buffer;
1183 len = urb->actual_length;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001184 stream->bulk.payload_size += len;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001185
1186 /* If the URB is the first of its payload, decode and save the
1187 * header.
1188 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001189 if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001190 do {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001191 ret = uvc_video_decode_start(stream, buf, mem, len);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001192 if (ret == -EAGAIN)
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001193 buf = uvc_queue_next_buffer(&stream->queue,
1194 buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001195 } while (ret == -EAGAIN);
1196
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001197 /* If an error occurred skip the rest of the payload. */
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001198 if (ret < 0 || buf == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001199 stream->bulk.skip_payload = 1;
Laurent Pinchartf8dd4af2008-12-16 10:41:57 -03001200 } else {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001201 memcpy(stream->bulk.header, mem, ret);
1202 stream->bulk.header_size = ret;
Laurent Pinchartf8dd4af2008-12-16 10:41:57 -03001203
1204 mem += ret;
1205 len -= ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001206 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001207 }
1208
1209 /* The buffer queue might have been cancelled while a bulk transfer
1210 * was in progress, so we can reach here with buf equal to NULL. Make
1211 * sure buf is never dereferenced if NULL.
1212 */
1213
1214 /* Process video data. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001215 if (!stream->bulk.skip_payload && buf != NULL)
1216 uvc_video_decode_data(stream, buf, mem, len);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001217
1218 /* Detect the payload end by a URB smaller than the maximum size (or
1219 * a payload size equal to the maximum) and process the header again.
1220 */
1221 if (urb->actual_length < urb->transfer_buffer_length ||
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001222 stream->bulk.payload_size >= stream->bulk.max_payload_size) {
1223 if (!stream->bulk.skip_payload && buf != NULL) {
1224 uvc_video_decode_end(stream, buf, stream->bulk.header,
1225 stream->bulk.payload_size);
Laurent Pinchartd7c0d432009-12-16 21:20:45 -03001226 if (buf->state == UVC_BUF_STATE_READY)
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001227 buf = uvc_queue_next_buffer(&stream->queue,
1228 buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001229 }
1230
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001231 stream->bulk.header_size = 0;
1232 stream->bulk.skip_payload = 0;
1233 stream->bulk.payload_size = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001234 }
1235}
1236
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001237static void uvc_video_encode_bulk(struct urb *urb, struct uvc_streaming *stream,
1238 struct uvc_buffer *buf)
Laurent Pinchartff924202008-12-28 22:32:29 -03001239{
1240 u8 *mem = urb->transfer_buffer;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001241 int len = stream->urb_size, ret;
Laurent Pinchartff924202008-12-28 22:32:29 -03001242
1243 if (buf == NULL) {
1244 urb->transfer_buffer_length = 0;
1245 return;
1246 }
1247
1248 /* If the URB is the first of its payload, add the header. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001249 if (stream->bulk.header_size == 0) {
1250 ret = uvc_video_encode_header(stream, buf, mem, len);
1251 stream->bulk.header_size = ret;
1252 stream->bulk.payload_size += ret;
Laurent Pinchartff924202008-12-28 22:32:29 -03001253 mem += ret;
1254 len -= ret;
1255 }
1256
1257 /* Process video data. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001258 ret = uvc_video_encode_data(stream, buf, mem, len);
Laurent Pinchartff924202008-12-28 22:32:29 -03001259
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001260 stream->bulk.payload_size += ret;
Laurent Pinchartff924202008-12-28 22:32:29 -03001261 len -= ret;
1262
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001263 if (buf->bytesused == stream->queue.buf_used ||
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001264 stream->bulk.payload_size == stream->bulk.max_payload_size) {
Laurent Pinchart3d95e932011-10-24 11:49:19 -03001265 if (buf->bytesused == stream->queue.buf_used) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001266 stream->queue.buf_used = 0;
Laurent Pinchartd7c0d432009-12-16 21:20:45 -03001267 buf->state = UVC_BUF_STATE_READY;
Laurent Pinchart6998b6f2011-10-24 11:53:59 -03001268 buf->buf.v4l2_buf.sequence = ++stream->sequence;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001269 uvc_queue_next_buffer(&stream->queue, buf);
1270 stream->last_fid ^= UVC_STREAM_FID;
Laurent Pinchartff924202008-12-28 22:32:29 -03001271 }
1272
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001273 stream->bulk.header_size = 0;
1274 stream->bulk.payload_size = 0;
Laurent Pinchartff924202008-12-28 22:32:29 -03001275 }
1276
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001277 urb->transfer_buffer_length = stream->urb_size - len;
Laurent Pinchartff924202008-12-28 22:32:29 -03001278}
1279
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001280static void uvc_video_complete(struct urb *urb)
1281{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001282 struct uvc_streaming *stream = urb->context;
1283 struct uvc_video_queue *queue = &stream->queue;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001284 struct uvc_buffer *buf = NULL;
1285 unsigned long flags;
1286 int ret;
1287
1288 switch (urb->status) {
1289 case 0:
1290 break;
1291
1292 default:
1293 uvc_printk(KERN_WARNING, "Non-zero status (%d) in video "
1294 "completion handler.\n", urb->status);
1295
1296 case -ENOENT: /* usb_kill_urb() called. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001297 if (stream->frozen)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001298 return;
1299
1300 case -ECONNRESET: /* usb_unlink_urb() called. */
1301 case -ESHUTDOWN: /* The endpoint is being disabled. */
1302 uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
1303 return;
1304 }
1305
1306 spin_lock_irqsave(&queue->irqlock, flags);
1307 if (!list_empty(&queue->irqqueue))
1308 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
1309 queue);
1310 spin_unlock_irqrestore(&queue->irqlock, flags);
1311
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001312 stream->decode(urb, stream, buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001313
1314 if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
1315 uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n",
1316 ret);
1317 }
1318}
1319
1320/*
Laurent Pincharte01117c2008-07-04 00:36:21 -03001321 * Free transfer buffers.
1322 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001323static void uvc_free_urb_buffers(struct uvc_streaming *stream)
Laurent Pincharte01117c2008-07-04 00:36:21 -03001324{
1325 unsigned int i;
1326
1327 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001328 if (stream->urb_buffer[i]) {
Al Cooper3e0ac672011-08-18 10:28:29 -03001329#ifndef CONFIG_DMA_NONCOHERENT
Daniel Mack997ea582010-04-12 13:17:25 +02001330 usb_free_coherent(stream->dev->udev, stream->urb_size,
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001331 stream->urb_buffer[i], stream->urb_dma[i]);
Al Cooper3e0ac672011-08-18 10:28:29 -03001332#else
1333 kfree(stream->urb_buffer[i]);
1334#endif
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001335 stream->urb_buffer[i] = NULL;
Laurent Pincharte01117c2008-07-04 00:36:21 -03001336 }
1337 }
1338
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001339 stream->urb_size = 0;
Laurent Pincharte01117c2008-07-04 00:36:21 -03001340}
1341
1342/*
1343 * Allocate transfer buffers. This function can be called with buffers
1344 * already allocated when resuming from suspend, in which case it will
1345 * return without touching the buffers.
1346 *
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001347 * Limit the buffer size to UVC_MAX_PACKETS bulk/isochronous packets. If the
1348 * system is too low on memory try successively smaller numbers of packets
1349 * until allocation succeeds.
1350 *
1351 * Return the number of allocated packets on success or 0 when out of memory.
Laurent Pincharte01117c2008-07-04 00:36:21 -03001352 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001353static int uvc_alloc_urb_buffers(struct uvc_streaming *stream,
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001354 unsigned int size, unsigned int psize, gfp_t gfp_flags)
Laurent Pincharte01117c2008-07-04 00:36:21 -03001355{
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001356 unsigned int npackets;
Laurent Pincharte01117c2008-07-04 00:36:21 -03001357 unsigned int i;
1358
1359 /* Buffers are already allocated, bail out. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001360 if (stream->urb_size)
1361 return stream->urb_size / psize;
Laurent Pincharte01117c2008-07-04 00:36:21 -03001362
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001363 /* Compute the number of packets. Bulk endpoints might transfer UVC
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001364 * payloads across multiple URBs.
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001365 */
1366 npackets = DIV_ROUND_UP(size, psize);
1367 if (npackets > UVC_MAX_PACKETS)
1368 npackets = UVC_MAX_PACKETS;
1369
1370 /* Retry allocations until one succeed. */
1371 for (; npackets > 1; npackets /= 2) {
1372 for (i = 0; i < UVC_URBS; ++i) {
Ming Leifd1b6bb2009-09-27 05:30:34 -03001373 stream->urb_size = psize * npackets;
Al Cooper3e0ac672011-08-18 10:28:29 -03001374#ifndef CONFIG_DMA_NONCOHERENT
Daniel Mack997ea582010-04-12 13:17:25 +02001375 stream->urb_buffer[i] = usb_alloc_coherent(
Ming Leifd1b6bb2009-09-27 05:30:34 -03001376 stream->dev->udev, stream->urb_size,
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001377 gfp_flags | __GFP_NOWARN, &stream->urb_dma[i]);
Al Cooper3e0ac672011-08-18 10:28:29 -03001378#else
1379 stream->urb_buffer[i] =
1380 kmalloc(stream->urb_size, gfp_flags | __GFP_NOWARN);
1381#endif
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001382 if (!stream->urb_buffer[i]) {
1383 uvc_free_urb_buffers(stream);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001384 break;
1385 }
1386 }
1387
1388 if (i == UVC_URBS) {
Laurent Pinchart663a4192009-08-06 11:41:17 -03001389 uvc_trace(UVC_TRACE_VIDEO, "Allocated %u URB buffers "
1390 "of %ux%u bytes each.\n", UVC_URBS, npackets,
1391 psize);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001392 return npackets;
Laurent Pincharte01117c2008-07-04 00:36:21 -03001393 }
1394 }
1395
Laurent Pinchart663a4192009-08-06 11:41:17 -03001396 uvc_trace(UVC_TRACE_VIDEO, "Failed to allocate URB buffers (%u bytes "
1397 "per packet).\n", psize);
Laurent Pincharte01117c2008-07-04 00:36:21 -03001398 return 0;
1399}
1400
1401/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001402 * Uninitialize isochronous/bulk URBs and free transfer buffers.
1403 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001404static void uvc_uninit_video(struct uvc_streaming *stream, int free_buffers)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001405{
1406 struct urb *urb;
1407 unsigned int i;
1408
Alexey Fisher7bc5edb2011-11-03 11:40:08 -03001409 uvc_video_stats_stop(stream);
1410
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001411 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001412 urb = stream->urb[i];
1413 if (urb == NULL)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001414 continue;
1415
1416 usb_kill_urb(urb);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001417 usb_free_urb(urb);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001418 stream->urb[i] = NULL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001419 }
Laurent Pincharte01117c2008-07-04 00:36:21 -03001420
1421 if (free_buffers)
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001422 uvc_free_urb_buffers(stream);
Laurent Pinchart66847ef2011-09-24 10:46:55 -03001423
1424 uvc_video_clock_cleanup(stream);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001425}
1426
1427/*
1428 * Initialize isochronous URBs and allocate transfer buffers. The packet size
1429 * is given by the endpoint.
1430 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001431static int uvc_init_video_isoc(struct uvc_streaming *stream,
Laurent Pinchart29135872008-07-04 00:35:26 -03001432 struct usb_host_endpoint *ep, gfp_t gfp_flags)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001433{
1434 struct urb *urb;
1435 unsigned int npackets, i, j;
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001436 u16 psize;
1437 u32 size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001438
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001439 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
1440 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001441 size = stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001442
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001443 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001444 if (npackets == 0)
1445 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001446
1447 size = npackets * psize;
1448
1449 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart29135872008-07-04 00:35:26 -03001450 urb = usb_alloc_urb(npackets, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001451 if (urb == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001452 uvc_uninit_video(stream, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001453 return -ENOMEM;
1454 }
1455
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001456 urb->dev = stream->dev->udev;
1457 urb->context = stream;
1458 urb->pipe = usb_rcvisocpipe(stream->dev->udev,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001459 ep->desc.bEndpointAddress);
Al Cooper3e0ac672011-08-18 10:28:29 -03001460#ifndef CONFIG_DMA_NONCOHERENT
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001461 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Al Cooper3e0ac672011-08-18 10:28:29 -03001462 urb->transfer_dma = stream->urb_dma[i];
1463#else
1464 urb->transfer_flags = URB_ISO_ASAP;
1465#endif
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001466 urb->interval = ep->desc.bInterval;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001467 urb->transfer_buffer = stream->urb_buffer[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001468 urb->complete = uvc_video_complete;
1469 urb->number_of_packets = npackets;
1470 urb->transfer_buffer_length = size;
1471
1472 for (j = 0; j < npackets; ++j) {
1473 urb->iso_frame_desc[j].offset = j * psize;
1474 urb->iso_frame_desc[j].length = psize;
1475 }
1476
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001477 stream->urb[i] = urb;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001478 }
1479
1480 return 0;
1481}
1482
1483/*
1484 * Initialize bulk URBs and allocate transfer buffers. The packet size is
1485 * given by the endpoint.
1486 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001487static int uvc_init_video_bulk(struct uvc_streaming *stream,
Laurent Pinchart29135872008-07-04 00:35:26 -03001488 struct usb_host_endpoint *ep, gfp_t gfp_flags)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001489{
1490 struct urb *urb;
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001491 unsigned int npackets, pipe, i;
1492 u16 psize;
1493 u32 size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001494
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001495 psize = le16_to_cpu(ep->desc.wMaxPacketSize) & 0x07ff;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001496 size = stream->ctrl.dwMaxPayloadTransferSize;
1497 stream->bulk.max_payload_size = size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001498
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001499 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001500 if (npackets == 0)
Laurent Pincharte01117c2008-07-04 00:36:21 -03001501 return -ENOMEM;
1502
Laurent Pinchartefdc8a92009-01-18 17:46:30 -03001503 size = npackets * psize;
1504
Laurent Pinchartff924202008-12-28 22:32:29 -03001505 if (usb_endpoint_dir_in(&ep->desc))
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001506 pipe = usb_rcvbulkpipe(stream->dev->udev,
Laurent Pinchartff924202008-12-28 22:32:29 -03001507 ep->desc.bEndpointAddress);
1508 else
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001509 pipe = usb_sndbulkpipe(stream->dev->udev,
Laurent Pinchartff924202008-12-28 22:32:29 -03001510 ep->desc.bEndpointAddress);
1511
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001512 if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
Laurent Pinchartff924202008-12-28 22:32:29 -03001513 size = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001514
1515 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart29135872008-07-04 00:35:26 -03001516 urb = usb_alloc_urb(0, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001517 if (urb == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001518 uvc_uninit_video(stream, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001519 return -ENOMEM;
1520 }
1521
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001522 usb_fill_bulk_urb(urb, stream->dev->udev, pipe,
1523 stream->urb_buffer[i], size, uvc_video_complete,
1524 stream);
Al Cooper3e0ac672011-08-18 10:28:29 -03001525#ifndef CONFIG_DMA_NONCOHERENT
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001526 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001527 urb->transfer_dma = stream->urb_dma[i];
Al Cooper3e0ac672011-08-18 10:28:29 -03001528#endif
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001529
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001530 stream->urb[i] = urb;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001531 }
1532
1533 return 0;
1534}
1535
1536/*
1537 * Initialize isochronous/bulk URBs and allocate transfer buffers.
1538 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001539static int uvc_init_video(struct uvc_streaming *stream, gfp_t gfp_flags)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001540{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001541 struct usb_interface *intf = stream->intf;
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001542 struct usb_host_endpoint *ep;
1543 unsigned int i;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001544 int ret;
1545
Laurent Pinchart650b95f2010-10-02 11:06:05 -03001546 stream->sequence = -1;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001547 stream->last_fid = -1;
1548 stream->bulk.header_size = 0;
1549 stream->bulk.skip_payload = 0;
1550 stream->bulk.payload_size = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001551
Alexey Fisher7bc5edb2011-11-03 11:40:08 -03001552 uvc_video_stats_start(stream);
1553
Laurent Pinchart66847ef2011-09-24 10:46:55 -03001554 ret = uvc_video_clock_init(stream);
1555 if (ret < 0)
1556 return ret;
1557
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001558 if (intf->num_altsetting > 1) {
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001559 struct usb_host_endpoint *best_ep = NULL;
1560 unsigned int best_psize = 3 * 1024;
1561 unsigned int bandwidth;
1562 unsigned int uninitialized_var(altsetting);
1563 int intfnum = stream->intfnum;
1564
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001565 /* Isochronous endpoint, select the alternate setting. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001566 bandwidth = stream->ctrl.dwMaxPayloadTransferSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001567
1568 if (bandwidth == 0) {
Laurent Pinchart663a4192009-08-06 11:41:17 -03001569 uvc_trace(UVC_TRACE_VIDEO, "Device requested null "
1570 "bandwidth, defaulting to lowest.\n");
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001571 bandwidth = 1;
Laurent Pinchart663a4192009-08-06 11:41:17 -03001572 } else {
1573 uvc_trace(UVC_TRACE_VIDEO, "Device requested %u "
1574 "B/frame bandwidth.\n", bandwidth);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001575 }
1576
1577 for (i = 0; i < intf->num_altsetting; ++i) {
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001578 struct usb_host_interface *alts;
1579 unsigned int psize;
1580
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001581 alts = &intf->altsetting[i];
1582 ep = uvc_find_endpoint(alts,
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001583 stream->header.bEndpointAddress);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001584 if (ep == NULL)
1585 continue;
1586
1587 /* Check if the bandwidth is high enough. */
1588 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
1589 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001590 if (psize >= bandwidth && psize <= best_psize) {
1591 altsetting = i;
1592 best_psize = psize;
1593 best_ep = ep;
1594 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001595 }
1596
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001597 if (best_ep == NULL) {
Laurent Pinchart663a4192009-08-06 11:41:17 -03001598 uvc_trace(UVC_TRACE_VIDEO, "No fast enough alt setting "
1599 "for requested bandwidth.\n");
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001600 return -EIO;
Laurent Pinchart663a4192009-08-06 11:41:17 -03001601 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001602
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001603 uvc_trace(UVC_TRACE_VIDEO, "Selecting alternate setting %u "
1604 "(%u B/frame bandwidth).\n", altsetting, best_psize);
1605
1606 ret = usb_set_interface(stream->dev->udev, intfnum, altsetting);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001607 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001608 return ret;
1609
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001610 ret = uvc_init_video_isoc(stream, best_ep, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001611 } else {
1612 /* Bulk endpoint, proceed to URB initialization. */
1613 ep = uvc_find_endpoint(&intf->altsetting[0],
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001614 stream->header.bEndpointAddress);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001615 if (ep == NULL)
1616 return -EIO;
1617
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001618 ret = uvc_init_video_bulk(stream, ep, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001619 }
1620
1621 if (ret < 0)
1622 return ret;
1623
1624 /* Submit the URBs. */
1625 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001626 ret = usb_submit_urb(stream->urb[i], gfp_flags);
1627 if (ret < 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001628 uvc_printk(KERN_ERR, "Failed to submit URB %u "
1629 "(%d).\n", i, ret);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001630 uvc_uninit_video(stream, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001631 return ret;
1632 }
1633 }
1634
1635 return 0;
1636}
1637
1638/* --------------------------------------------------------------------------
1639 * Suspend/resume
1640 */
1641
1642/*
1643 * Stop streaming without disabling the video queue.
1644 *
1645 * To let userspace applications resume without trouble, we must not touch the
1646 * video buffers in any way. We mark the device as frozen to make sure the URB
1647 * completion handler won't try to cancel the queue when we kill the URBs.
1648 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001649int uvc_video_suspend(struct uvc_streaming *stream)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001650{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001651 if (!uvc_queue_streaming(&stream->queue))
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001652 return 0;
1653
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001654 stream->frozen = 1;
1655 uvc_uninit_video(stream, 0);
1656 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001657 return 0;
1658}
1659
1660/*
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03001661 * Reconfigure the video interface and restart streaming if it was enabled
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001662 * before suspend.
1663 *
1664 * If an error occurs, disable the video queue. This will wake all pending
1665 * buffers, making sure userspace applications are notified of the problem
1666 * instead of waiting forever.
1667 */
Ming Leid59a7b12011-07-16 00:51:00 -03001668int uvc_video_resume(struct uvc_streaming *stream, int reset)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001669{
1670 int ret;
1671
Ming Leid59a7b12011-07-16 00:51:00 -03001672 /* If the bus has been reset on resume, set the alternate setting to 0.
1673 * This should be the default value, but some devices crash or otherwise
1674 * misbehave if they don't receive a SET_INTERFACE request before any
1675 * other video control request.
1676 */
1677 if (reset)
1678 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1679
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001680 stream->frozen = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001681
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001682 ret = uvc_commit_video(stream, &stream->ctrl);
1683 if (ret < 0) {
1684 uvc_queue_enable(&stream->queue, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001685 return ret;
1686 }
1687
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001688 if (!uvc_queue_streaming(&stream->queue))
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001689 return 0;
1690
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001691 ret = uvc_init_video(stream, GFP_NOIO);
1692 if (ret < 0)
1693 uvc_queue_enable(&stream->queue, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001694
1695 return ret;
1696}
1697
1698/* ------------------------------------------------------------------------
1699 * Video device
1700 */
1701
1702/*
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03001703 * Initialize the UVC video device by switching to alternate setting 0 and
1704 * retrieve the default format.
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001705 *
1706 * Some cameras (namely the Fuji Finepix) set the format and frame
1707 * indexes to zero. The UVC standard doesn't clearly make this a spec
1708 * violation, so try to silently fix the values if possible.
1709 *
1710 * This function is called before registering the device with V4L.
1711 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001712int uvc_video_init(struct uvc_streaming *stream)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001713{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001714 struct uvc_streaming_control *probe = &stream->ctrl;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001715 struct uvc_format *format = NULL;
1716 struct uvc_frame *frame = NULL;
1717 unsigned int i;
1718 int ret;
1719
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001720 if (stream->nformats == 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001721 uvc_printk(KERN_INFO, "No supported video formats found.\n");
1722 return -EINVAL;
1723 }
1724
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001725 atomic_set(&stream->active, 0);
1726
1727 /* Initialize the video buffers queue. */
Laurent Pinchart9bde9f22010-06-17 06:52:37 -03001728 uvc_queue_init(&stream->queue, stream->type, !uvc_no_drop_param);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001729
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001730 /* Alternate setting 0 should be the default, yet the XBox Live Vision
1731 * Cam (and possibly other devices) crash or otherwise misbehave if
1732 * they don't receive a SET_INTERFACE request before any other video
1733 * control request.
1734 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001735 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001736
Laurent Pinchart72362422009-02-14 19:26:56 -03001737 /* Set the streaming probe control with default streaming parameters
1738 * retrieved from the device. Webcams that don't suport GET_DEF
1739 * requests on the probe control will just keep their current streaming
1740 * parameters.
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001741 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001742 if (uvc_get_video_ctrl(stream, probe, 1, UVC_GET_DEF) == 0)
1743 uvc_set_video_ctrl(stream, probe, 1);
Laurent Pinchart72362422009-02-14 19:26:56 -03001744
1745 /* Initialize the streaming parameters with the probe control current
1746 * value. This makes sure SET_CUR requests on the streaming commit
1747 * control will always use values retrieved from a successful GET_CUR
1748 * request on the probe control, as required by the UVC specification.
1749 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001750 ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
Laurent Pinchartb482d922009-06-26 11:39:42 -03001751 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001752 return ret;
1753
1754 /* Check if the default format descriptor exists. Use the first
1755 * available format otherwise.
1756 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001757 for (i = stream->nformats; i > 0; --i) {
1758 format = &stream->format[i-1];
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001759 if (format->index == probe->bFormatIndex)
1760 break;
1761 }
1762
1763 if (format->nframes == 0) {
1764 uvc_printk(KERN_INFO, "No frame descriptor found for the "
1765 "default format.\n");
1766 return -EINVAL;
1767 }
1768
1769 /* Zero bFrameIndex might be correct. Stream-based formats (including
1770 * MPEG-2 TS and DV) do not support frames but have a dummy frame
1771 * descriptor with bFrameIndex set to zero. If the default frame
Laurent Pinchart078f8942009-05-06 12:30:30 -03001772 * descriptor is not found, use the first available frame.
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001773 */
1774 for (i = format->nframes; i > 0; --i) {
1775 frame = &format->frame[i-1];
1776 if (frame->bFrameIndex == probe->bFrameIndex)
1777 break;
1778 }
1779
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001780 probe->bFormatIndex = format->index;
1781 probe->bFrameIndex = frame->bFrameIndex;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001782
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001783 stream->cur_format = format;
1784 stream->cur_frame = frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001785
1786 /* Select the video decoding function */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001787 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1788 if (stream->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
1789 stream->decode = uvc_video_decode_isight;
1790 else if (stream->intf->num_altsetting > 1)
1791 stream->decode = uvc_video_decode_isoc;
Laurent Pinchartff924202008-12-28 22:32:29 -03001792 else
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001793 stream->decode = uvc_video_decode_bulk;
Laurent Pinchartff924202008-12-28 22:32:29 -03001794 } else {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001795 if (stream->intf->num_altsetting == 1)
1796 stream->decode = uvc_video_encode_bulk;
Laurent Pinchartff924202008-12-28 22:32:29 -03001797 else {
1798 uvc_printk(KERN_INFO, "Isochronous endpoints are not "
1799 "supported for video output devices.\n");
1800 return -EINVAL;
1801 }
1802 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001803
1804 return 0;
1805}
1806
1807/*
1808 * Enable or disable the video stream.
1809 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001810int uvc_video_enable(struct uvc_streaming *stream, int enable)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001811{
1812 int ret;
1813
1814 if (!enable) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001815 uvc_uninit_video(stream, 1);
1816 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1817 uvc_queue_enable(&stream->queue, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001818 return 0;
1819 }
1820
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001821 ret = uvc_queue_enable(&stream->queue, 1);
1822 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001823 return ret;
1824
Laurent Pinchart23867b22008-11-12 11:46:43 -03001825 /* Commit the streaming parameters. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001826 ret = uvc_commit_video(stream, &stream->ctrl);
Sjoerd Simonsaa122d42011-05-30 15:02:00 -03001827 if (ret < 0) {
1828 uvc_queue_enable(&stream->queue, 0);
Laurent Pinchart23867b22008-11-12 11:46:43 -03001829 return ret;
Sjoerd Simonsaa122d42011-05-30 15:02:00 -03001830 }
Laurent Pinchart23867b22008-11-12 11:46:43 -03001831
Laurent Pinchart24c3aae2011-10-25 09:03:42 -03001832 ret = uvc_init_video(stream, GFP_KERNEL);
1833 if (ret < 0) {
1834 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1835 uvc_queue_enable(&stream->queue, 0);
1836 }
Hans Verkuilf87086e2008-07-18 00:50:58 -03001837
Laurent Pinchart24c3aae2011-10-25 09:03:42 -03001838 return ret;
1839}