blob: a57f813418495586e1c927282220b8ee438fba0c [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 Pinchartc0efd232008-06-30 15:04:50 -0300360/* ------------------------------------------------------------------------
361 * Video codecs
362 */
363
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300364/* Video payload decoding is handled by uvc_video_decode_start(),
365 * uvc_video_decode_data() and uvc_video_decode_end().
366 *
367 * uvc_video_decode_start is called with URB data at the start of a bulk or
368 * isochronous payload. It processes header data and returns the header size
369 * in bytes if successful. If an error occurs, it returns a negative error
370 * code. The following error codes have special meanings.
371 *
372 * - EAGAIN informs the caller that the current video buffer should be marked
373 * as done, and that the function should be called again with the same data
374 * and a new video buffer. This is used when end of frame conditions can be
375 * reliably detected at the beginning of the next frame only.
376 *
377 * If an error other than -EAGAIN is returned, the caller will drop the current
378 * payload. No call to uvc_video_decode_data and uvc_video_decode_end will be
379 * made until the next payload. -ENODATA can be used to drop the current
380 * payload if no other error code is appropriate.
381 *
382 * uvc_video_decode_data is called for every URB with URB data. It copies the
383 * data to the video buffer.
384 *
385 * uvc_video_decode_end is called with header data at the end of a bulk or
386 * isochronous payload. It performs any additional header data processing and
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300387 * returns 0 or a negative error code if an error occurred. As header data have
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300388 * already been processed by uvc_video_decode_start, this functions isn't
389 * required to perform sanity checks a second time.
390 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300391 * For isochronous transfers where a payload is always transferred in a single
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300392 * URB, the three functions will be called in a row.
393 *
394 * To let the decoder process header data and update its internal state even
395 * when no video buffer is available, uvc_video_decode_start must be prepared
396 * to be called with a NULL buf parameter. uvc_video_decode_data and
397 * uvc_video_decode_end will never be called with a NULL buffer.
398 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300399static int uvc_video_decode_start(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300400 struct uvc_buffer *buf, const __u8 *data, int len)
401{
402 __u8 fid;
403
404 /* Sanity checks:
405 * - packet must be at least 2 bytes long
406 * - bHeaderLength value must be at least 2 bytes (see above)
407 * - bHeaderLength value can't be larger than the packet size.
408 */
409 if (len < 2 || data[0] < 2 || data[0] > len)
410 return -EINVAL;
411
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300412 fid = data[1] & UVC_STREAM_FID;
413
Laurent Pinchart650b95f2010-10-02 11:06:05 -0300414 /* Increase the sequence number regardless of any buffer states, so
415 * that discontinuous sequence numbers always indicate lost frames.
416 */
417 if (stream->last_fid != fid)
418 stream->sequence++;
419
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300420 /* Store the payload FID bit and return immediately when the buffer is
421 * NULL.
422 */
423 if (buf == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300424 stream->last_fid = fid;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300425 return -ENODATA;
426 }
427
Laurent Pinchart3afedb92011-11-03 07:24:34 -0300428 /* Mark the buffer as bad if the error bit is set. */
429 if (data[1] & UVC_STREAM_ERR) {
430 uvc_trace(UVC_TRACE_FRAME, "Marking buffer as bad (error bit "
431 "set).\n");
432 buf->error = 1;
433 }
434
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300435 /* Synchronize to the input stream by waiting for the FID bit to be
436 * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE.
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300437 * stream->last_fid is initialized to -1, so the first isochronous
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300438 * frame will always be in sync.
439 *
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300440 * If the device doesn't toggle the FID bit, invert stream->last_fid
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300441 * when the EOF bit is set to force synchronisation on the next packet.
442 */
443 if (buf->state != UVC_BUF_STATE_ACTIVE) {
Laurent Pinchart310fe522009-12-09 22:57:48 -0300444 struct timespec ts;
445
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300446 if (fid == stream->last_fid) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300447 uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of "
448 "sync).\n");
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300449 if ((stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) &&
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300450 (data[1] & UVC_STREAM_EOF))
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300451 stream->last_fid ^= UVC_STREAM_FID;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300452 return -ENODATA;
453 }
454
Laurent Pinchart310fe522009-12-09 22:57:48 -0300455 if (uvc_clock_param == CLOCK_MONOTONIC)
456 ktime_get_ts(&ts);
457 else
458 ktime_get_real_ts(&ts);
459
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300460 buf->buf.v4l2_buf.sequence = stream->sequence;
461 buf->buf.v4l2_buf.timestamp.tv_sec = ts.tv_sec;
462 buf->buf.v4l2_buf.timestamp.tv_usec =
463 ts.tv_nsec / NSEC_PER_USEC;
Laurent Pinchart310fe522009-12-09 22:57:48 -0300464
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300465 /* TODO: Handle PTS and SCR. */
466 buf->state = UVC_BUF_STATE_ACTIVE;
467 }
468
469 /* Mark the buffer as done if we're at the beginning of a new frame.
470 * End of frame detection is better implemented by checking the EOF
471 * bit (FID bit toggling is delayed by one frame compared to the EOF
472 * bit), but some devices don't set the bit at end of frame (and the
473 * last payload can be lost anyway). We thus must check if the FID has
474 * been toggled.
475 *
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300476 * stream->last_fid is initialized to -1, so the first isochronous
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300477 * frame will never trigger an end of frame detection.
478 *
479 * Empty buffers (bytesused == 0) don't trigger end of frame detection
480 * as it doesn't make sense to return an empty buffer. This also
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300481 * avoids detecting end of frame conditions at FID toggling if the
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300482 * previous payload had the EOF bit set.
483 */
Laurent Pinchart3d95e932011-10-24 11:49:19 -0300484 if (fid != stream->last_fid && buf->bytesused != 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300485 uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit "
486 "toggled).\n");
Laurent Pinchartd7c0d432009-12-16 21:20:45 -0300487 buf->state = UVC_BUF_STATE_READY;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300488 return -EAGAIN;
489 }
490
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300491 stream->last_fid = fid;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300492
493 return data[0];
494}
495
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300496static void uvc_video_decode_data(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300497 struct uvc_buffer *buf, const __u8 *data, int len)
498{
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300499 unsigned int maxlen, nbytes;
500 void *mem;
501
502 if (len <= 0)
503 return;
504
505 /* Copy the video data to the buffer. */
Laurent Pinchart3d95e932011-10-24 11:49:19 -0300506 maxlen = buf->length - buf->bytesused;
507 mem = buf->mem + buf->bytesused;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300508 nbytes = min((unsigned int)len, maxlen);
509 memcpy(mem, data, nbytes);
Laurent Pinchart3d95e932011-10-24 11:49:19 -0300510 buf->bytesused += nbytes;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300511
512 /* Complete the current frame if the buffer size was exceeded. */
513 if (len > maxlen) {
514 uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n");
Laurent Pinchartd7c0d432009-12-16 21:20:45 -0300515 buf->state = UVC_BUF_STATE_READY;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300516 }
517}
518
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300519static void uvc_video_decode_end(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300520 struct uvc_buffer *buf, const __u8 *data, int len)
521{
522 /* Mark the buffer as done if the EOF marker is set. */
Laurent Pinchart3d95e932011-10-24 11:49:19 -0300523 if (data[1] & UVC_STREAM_EOF && buf->bytesused != 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300524 uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n");
525 if (data[0] == len)
526 uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n");
Laurent Pinchartd7c0d432009-12-16 21:20:45 -0300527 buf->state = UVC_BUF_STATE_READY;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300528 if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID)
529 stream->last_fid ^= UVC_STREAM_FID;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300530 }
531}
532
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300533/* Video payload encoding is handled by uvc_video_encode_header() and
534 * uvc_video_encode_data(). Only bulk transfers are currently supported.
535 *
536 * uvc_video_encode_header is called at the start of a payload. It adds header
537 * data to the transfer buffer and returns the header size. As the only known
538 * UVC output device transfers a whole frame in a single payload, the EOF bit
539 * is always set in the header.
540 *
541 * uvc_video_encode_data is called for every URB and copies the data from the
542 * video buffer to the transfer buffer.
543 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300544static int uvc_video_encode_header(struct uvc_streaming *stream,
Laurent Pinchartff924202008-12-28 22:32:29 -0300545 struct uvc_buffer *buf, __u8 *data, int len)
546{
547 data[0] = 2; /* Header length */
548 data[1] = UVC_STREAM_EOH | UVC_STREAM_EOF
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300549 | (stream->last_fid & UVC_STREAM_FID);
Laurent Pinchartff924202008-12-28 22:32:29 -0300550 return 2;
551}
552
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300553static int uvc_video_encode_data(struct uvc_streaming *stream,
Laurent Pinchartff924202008-12-28 22:32:29 -0300554 struct uvc_buffer *buf, __u8 *data, int len)
555{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300556 struct uvc_video_queue *queue = &stream->queue;
Laurent Pinchartff924202008-12-28 22:32:29 -0300557 unsigned int nbytes;
558 void *mem;
559
560 /* Copy video data to the URB buffer. */
Laurent Pinchart3d95e932011-10-24 11:49:19 -0300561 mem = buf->mem + queue->buf_used;
562 nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300563 nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size,
Laurent Pinchartff924202008-12-28 22:32:29 -0300564 nbytes);
565 memcpy(data, mem, nbytes);
566
567 queue->buf_used += nbytes;
568
569 return nbytes;
570}
571
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300572/* ------------------------------------------------------------------------
573 * URB handling
574 */
575
576/*
577 * Completion handler for video URBs.
578 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300579static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
580 struct uvc_buffer *buf)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300581{
582 u8 *mem;
583 int ret, i;
584
585 for (i = 0; i < urb->number_of_packets; ++i) {
586 if (urb->iso_frame_desc[i].status < 0) {
587 uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
588 "lost (%d).\n", urb->iso_frame_desc[i].status);
Laurent Pinchart9bde9f22010-06-17 06:52:37 -0300589 /* Mark the buffer as faulty. */
590 if (buf != NULL)
591 buf->error = 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300592 continue;
593 }
594
595 /* Decode the payload header. */
596 mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
597 do {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300598 ret = uvc_video_decode_start(stream, buf, mem,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300599 urb->iso_frame_desc[i].actual_length);
600 if (ret == -EAGAIN)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300601 buf = uvc_queue_next_buffer(&stream->queue,
602 buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300603 } while (ret == -EAGAIN);
604
605 if (ret < 0)
606 continue;
607
608 /* Decode the payload data. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300609 uvc_video_decode_data(stream, buf, mem + ret,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300610 urb->iso_frame_desc[i].actual_length - ret);
611
612 /* Process the header again. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300613 uvc_video_decode_end(stream, buf, mem,
Laurent Pinchart08ebd002008-08-29 17:37:44 -0300614 urb->iso_frame_desc[i].actual_length);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300615
Laurent Pinchart9bde9f22010-06-17 06:52:37 -0300616 if (buf->state == UVC_BUF_STATE_READY) {
Laurent Pinchart3d95e932011-10-24 11:49:19 -0300617 if (buf->length != buf->bytesused &&
Laurent Pinchart9bde9f22010-06-17 06:52:37 -0300618 !(stream->cur_format->flags &
619 UVC_FMT_FLAG_COMPRESSED))
620 buf->error = 1;
621
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300622 buf = uvc_queue_next_buffer(&stream->queue, buf);
Laurent Pinchart9bde9f22010-06-17 06:52:37 -0300623 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300624 }
625}
626
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300627static void uvc_video_decode_bulk(struct urb *urb, struct uvc_streaming *stream,
628 struct uvc_buffer *buf)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300629{
630 u8 *mem;
631 int len, ret;
632
Laurent Pinchartc90e7772009-02-14 19:39:08 -0300633 if (urb->actual_length == 0)
634 return;
635
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300636 mem = urb->transfer_buffer;
637 len = urb->actual_length;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300638 stream->bulk.payload_size += len;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300639
640 /* If the URB is the first of its payload, decode and save the
641 * header.
642 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300643 if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300644 do {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300645 ret = uvc_video_decode_start(stream, buf, mem, len);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300646 if (ret == -EAGAIN)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300647 buf = uvc_queue_next_buffer(&stream->queue,
648 buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300649 } while (ret == -EAGAIN);
650
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300651 /* If an error occurred skip the rest of the payload. */
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300652 if (ret < 0 || buf == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300653 stream->bulk.skip_payload = 1;
Laurent Pinchartf8dd4af2008-12-16 10:41:57 -0300654 } else {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300655 memcpy(stream->bulk.header, mem, ret);
656 stream->bulk.header_size = ret;
Laurent Pinchartf8dd4af2008-12-16 10:41:57 -0300657
658 mem += ret;
659 len -= ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300660 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300661 }
662
663 /* The buffer queue might have been cancelled while a bulk transfer
664 * was in progress, so we can reach here with buf equal to NULL. Make
665 * sure buf is never dereferenced if NULL.
666 */
667
668 /* Process video data. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300669 if (!stream->bulk.skip_payload && buf != NULL)
670 uvc_video_decode_data(stream, buf, mem, len);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300671
672 /* Detect the payload end by a URB smaller than the maximum size (or
673 * a payload size equal to the maximum) and process the header again.
674 */
675 if (urb->actual_length < urb->transfer_buffer_length ||
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300676 stream->bulk.payload_size >= stream->bulk.max_payload_size) {
677 if (!stream->bulk.skip_payload && buf != NULL) {
678 uvc_video_decode_end(stream, buf, stream->bulk.header,
679 stream->bulk.payload_size);
Laurent Pinchartd7c0d432009-12-16 21:20:45 -0300680 if (buf->state == UVC_BUF_STATE_READY)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300681 buf = uvc_queue_next_buffer(&stream->queue,
682 buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300683 }
684
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300685 stream->bulk.header_size = 0;
686 stream->bulk.skip_payload = 0;
687 stream->bulk.payload_size = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300688 }
689}
690
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300691static void uvc_video_encode_bulk(struct urb *urb, struct uvc_streaming *stream,
692 struct uvc_buffer *buf)
Laurent Pinchartff924202008-12-28 22:32:29 -0300693{
694 u8 *mem = urb->transfer_buffer;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300695 int len = stream->urb_size, ret;
Laurent Pinchartff924202008-12-28 22:32:29 -0300696
697 if (buf == NULL) {
698 urb->transfer_buffer_length = 0;
699 return;
700 }
701
702 /* If the URB is the first of its payload, add the header. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300703 if (stream->bulk.header_size == 0) {
704 ret = uvc_video_encode_header(stream, buf, mem, len);
705 stream->bulk.header_size = ret;
706 stream->bulk.payload_size += ret;
Laurent Pinchartff924202008-12-28 22:32:29 -0300707 mem += ret;
708 len -= ret;
709 }
710
711 /* Process video data. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300712 ret = uvc_video_encode_data(stream, buf, mem, len);
Laurent Pinchartff924202008-12-28 22:32:29 -0300713
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300714 stream->bulk.payload_size += ret;
Laurent Pinchartff924202008-12-28 22:32:29 -0300715 len -= ret;
716
Laurent Pinchart3d95e932011-10-24 11:49:19 -0300717 if (buf->bytesused == stream->queue.buf_used ||
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300718 stream->bulk.payload_size == stream->bulk.max_payload_size) {
Laurent Pinchart3d95e932011-10-24 11:49:19 -0300719 if (buf->bytesused == stream->queue.buf_used) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300720 stream->queue.buf_used = 0;
Laurent Pinchartd7c0d432009-12-16 21:20:45 -0300721 buf->state = UVC_BUF_STATE_READY;
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300722 buf->buf.v4l2_buf.sequence = ++stream->sequence;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300723 uvc_queue_next_buffer(&stream->queue, buf);
724 stream->last_fid ^= UVC_STREAM_FID;
Laurent Pinchartff924202008-12-28 22:32:29 -0300725 }
726
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300727 stream->bulk.header_size = 0;
728 stream->bulk.payload_size = 0;
Laurent Pinchartff924202008-12-28 22:32:29 -0300729 }
730
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300731 urb->transfer_buffer_length = stream->urb_size - len;
Laurent Pinchartff924202008-12-28 22:32:29 -0300732}
733
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300734static void uvc_video_complete(struct urb *urb)
735{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300736 struct uvc_streaming *stream = urb->context;
737 struct uvc_video_queue *queue = &stream->queue;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300738 struct uvc_buffer *buf = NULL;
739 unsigned long flags;
740 int ret;
741
742 switch (urb->status) {
743 case 0:
744 break;
745
746 default:
747 uvc_printk(KERN_WARNING, "Non-zero status (%d) in video "
748 "completion handler.\n", urb->status);
749
750 case -ENOENT: /* usb_kill_urb() called. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300751 if (stream->frozen)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300752 return;
753
754 case -ECONNRESET: /* usb_unlink_urb() called. */
755 case -ESHUTDOWN: /* The endpoint is being disabled. */
756 uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
757 return;
758 }
759
760 spin_lock_irqsave(&queue->irqlock, flags);
761 if (!list_empty(&queue->irqqueue))
762 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
763 queue);
764 spin_unlock_irqrestore(&queue->irqlock, flags);
765
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300766 stream->decode(urb, stream, buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300767
768 if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
769 uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n",
770 ret);
771 }
772}
773
774/*
Laurent Pincharte01117c2008-07-04 00:36:21 -0300775 * Free transfer buffers.
776 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300777static void uvc_free_urb_buffers(struct uvc_streaming *stream)
Laurent Pincharte01117c2008-07-04 00:36:21 -0300778{
779 unsigned int i;
780
781 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300782 if (stream->urb_buffer[i]) {
Al Cooper3e0ac672011-08-18 10:28:29 -0300783#ifndef CONFIG_DMA_NONCOHERENT
Daniel Mack997ea582010-04-12 13:17:25 +0200784 usb_free_coherent(stream->dev->udev, stream->urb_size,
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300785 stream->urb_buffer[i], stream->urb_dma[i]);
Al Cooper3e0ac672011-08-18 10:28:29 -0300786#else
787 kfree(stream->urb_buffer[i]);
788#endif
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300789 stream->urb_buffer[i] = NULL;
Laurent Pincharte01117c2008-07-04 00:36:21 -0300790 }
791 }
792
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300793 stream->urb_size = 0;
Laurent Pincharte01117c2008-07-04 00:36:21 -0300794}
795
796/*
797 * Allocate transfer buffers. This function can be called with buffers
798 * already allocated when resuming from suspend, in which case it will
799 * return without touching the buffers.
800 *
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300801 * Limit the buffer size to UVC_MAX_PACKETS bulk/isochronous packets. If the
802 * system is too low on memory try successively smaller numbers of packets
803 * until allocation succeeds.
804 *
805 * Return the number of allocated packets on success or 0 when out of memory.
Laurent Pincharte01117c2008-07-04 00:36:21 -0300806 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300807static int uvc_alloc_urb_buffers(struct uvc_streaming *stream,
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300808 unsigned int size, unsigned int psize, gfp_t gfp_flags)
Laurent Pincharte01117c2008-07-04 00:36:21 -0300809{
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300810 unsigned int npackets;
Laurent Pincharte01117c2008-07-04 00:36:21 -0300811 unsigned int i;
812
813 /* Buffers are already allocated, bail out. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300814 if (stream->urb_size)
815 return stream->urb_size / psize;
Laurent Pincharte01117c2008-07-04 00:36:21 -0300816
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300817 /* Compute the number of packets. Bulk endpoints might transfer UVC
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300818 * payloads across multiple URBs.
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300819 */
820 npackets = DIV_ROUND_UP(size, psize);
821 if (npackets > UVC_MAX_PACKETS)
822 npackets = UVC_MAX_PACKETS;
823
824 /* Retry allocations until one succeed. */
825 for (; npackets > 1; npackets /= 2) {
826 for (i = 0; i < UVC_URBS; ++i) {
Ming Leifd1b6bb2009-09-27 05:30:34 -0300827 stream->urb_size = psize * npackets;
Al Cooper3e0ac672011-08-18 10:28:29 -0300828#ifndef CONFIG_DMA_NONCOHERENT
Daniel Mack997ea582010-04-12 13:17:25 +0200829 stream->urb_buffer[i] = usb_alloc_coherent(
Ming Leifd1b6bb2009-09-27 05:30:34 -0300830 stream->dev->udev, stream->urb_size,
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300831 gfp_flags | __GFP_NOWARN, &stream->urb_dma[i]);
Al Cooper3e0ac672011-08-18 10:28:29 -0300832#else
833 stream->urb_buffer[i] =
834 kmalloc(stream->urb_size, gfp_flags | __GFP_NOWARN);
835#endif
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300836 if (!stream->urb_buffer[i]) {
837 uvc_free_urb_buffers(stream);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300838 break;
839 }
840 }
841
842 if (i == UVC_URBS) {
Laurent Pinchart663a4192009-08-06 11:41:17 -0300843 uvc_trace(UVC_TRACE_VIDEO, "Allocated %u URB buffers "
844 "of %ux%u bytes each.\n", UVC_URBS, npackets,
845 psize);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300846 return npackets;
Laurent Pincharte01117c2008-07-04 00:36:21 -0300847 }
848 }
849
Laurent Pinchart663a4192009-08-06 11:41:17 -0300850 uvc_trace(UVC_TRACE_VIDEO, "Failed to allocate URB buffers (%u bytes "
851 "per packet).\n", psize);
Laurent Pincharte01117c2008-07-04 00:36:21 -0300852 return 0;
853}
854
855/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300856 * Uninitialize isochronous/bulk URBs and free transfer buffers.
857 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300858static void uvc_uninit_video(struct uvc_streaming *stream, int free_buffers)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300859{
860 struct urb *urb;
861 unsigned int i;
862
863 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300864 urb = stream->urb[i];
865 if (urb == NULL)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300866 continue;
867
868 usb_kill_urb(urb);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300869 usb_free_urb(urb);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300870 stream->urb[i] = NULL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300871 }
Laurent Pincharte01117c2008-07-04 00:36:21 -0300872
873 if (free_buffers)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300874 uvc_free_urb_buffers(stream);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300875}
876
877/*
878 * Initialize isochronous URBs and allocate transfer buffers. The packet size
879 * is given by the endpoint.
880 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300881static int uvc_init_video_isoc(struct uvc_streaming *stream,
Laurent Pinchart29135872008-07-04 00:35:26 -0300882 struct usb_host_endpoint *ep, gfp_t gfp_flags)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300883{
884 struct urb *urb;
885 unsigned int npackets, i, j;
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300886 u16 psize;
887 u32 size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300888
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300889 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
890 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300891 size = stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300892
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300893 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300894 if (npackets == 0)
895 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300896
897 size = npackets * psize;
898
899 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart29135872008-07-04 00:35:26 -0300900 urb = usb_alloc_urb(npackets, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300901 if (urb == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300902 uvc_uninit_video(stream, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300903 return -ENOMEM;
904 }
905
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300906 urb->dev = stream->dev->udev;
907 urb->context = stream;
908 urb->pipe = usb_rcvisocpipe(stream->dev->udev,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300909 ep->desc.bEndpointAddress);
Al Cooper3e0ac672011-08-18 10:28:29 -0300910#ifndef CONFIG_DMA_NONCOHERENT
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300911 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Al Cooper3e0ac672011-08-18 10:28:29 -0300912 urb->transfer_dma = stream->urb_dma[i];
913#else
914 urb->transfer_flags = URB_ISO_ASAP;
915#endif
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300916 urb->interval = ep->desc.bInterval;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300917 urb->transfer_buffer = stream->urb_buffer[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300918 urb->complete = uvc_video_complete;
919 urb->number_of_packets = npackets;
920 urb->transfer_buffer_length = size;
921
922 for (j = 0; j < npackets; ++j) {
923 urb->iso_frame_desc[j].offset = j * psize;
924 urb->iso_frame_desc[j].length = psize;
925 }
926
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300927 stream->urb[i] = urb;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300928 }
929
930 return 0;
931}
932
933/*
934 * Initialize bulk URBs and allocate transfer buffers. The packet size is
935 * given by the endpoint.
936 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300937static int uvc_init_video_bulk(struct uvc_streaming *stream,
Laurent Pinchart29135872008-07-04 00:35:26 -0300938 struct usb_host_endpoint *ep, gfp_t gfp_flags)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300939{
940 struct urb *urb;
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300941 unsigned int npackets, pipe, i;
942 u16 psize;
943 u32 size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300944
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300945 psize = le16_to_cpu(ep->desc.wMaxPacketSize) & 0x07ff;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300946 size = stream->ctrl.dwMaxPayloadTransferSize;
947 stream->bulk.max_payload_size = size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300948
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300949 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300950 if (npackets == 0)
Laurent Pincharte01117c2008-07-04 00:36:21 -0300951 return -ENOMEM;
952
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300953 size = npackets * psize;
954
Laurent Pinchartff924202008-12-28 22:32:29 -0300955 if (usb_endpoint_dir_in(&ep->desc))
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300956 pipe = usb_rcvbulkpipe(stream->dev->udev,
Laurent Pinchartff924202008-12-28 22:32:29 -0300957 ep->desc.bEndpointAddress);
958 else
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300959 pipe = usb_sndbulkpipe(stream->dev->udev,
Laurent Pinchartff924202008-12-28 22:32:29 -0300960 ep->desc.bEndpointAddress);
961
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300962 if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
Laurent Pinchartff924202008-12-28 22:32:29 -0300963 size = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300964
965 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart29135872008-07-04 00:35:26 -0300966 urb = usb_alloc_urb(0, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300967 if (urb == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300968 uvc_uninit_video(stream, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300969 return -ENOMEM;
970 }
971
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300972 usb_fill_bulk_urb(urb, stream->dev->udev, pipe,
973 stream->urb_buffer[i], size, uvc_video_complete,
974 stream);
Al Cooper3e0ac672011-08-18 10:28:29 -0300975#ifndef CONFIG_DMA_NONCOHERENT
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300976 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300977 urb->transfer_dma = stream->urb_dma[i];
Al Cooper3e0ac672011-08-18 10:28:29 -0300978#endif
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300979
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300980 stream->urb[i] = urb;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300981 }
982
983 return 0;
984}
985
986/*
987 * Initialize isochronous/bulk URBs and allocate transfer buffers.
988 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300989static int uvc_init_video(struct uvc_streaming *stream, gfp_t gfp_flags)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300990{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300991 struct usb_interface *intf = stream->intf;
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -0300992 struct usb_host_endpoint *ep;
993 unsigned int i;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300994 int ret;
995
Laurent Pinchart650b95f2010-10-02 11:06:05 -0300996 stream->sequence = -1;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300997 stream->last_fid = -1;
998 stream->bulk.header_size = 0;
999 stream->bulk.skip_payload = 0;
1000 stream->bulk.payload_size = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001001
1002 if (intf->num_altsetting > 1) {
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001003 struct usb_host_endpoint *best_ep = NULL;
1004 unsigned int best_psize = 3 * 1024;
1005 unsigned int bandwidth;
1006 unsigned int uninitialized_var(altsetting);
1007 int intfnum = stream->intfnum;
1008
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001009 /* Isochronous endpoint, select the alternate setting. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001010 bandwidth = stream->ctrl.dwMaxPayloadTransferSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001011
1012 if (bandwidth == 0) {
Laurent Pinchart663a4192009-08-06 11:41:17 -03001013 uvc_trace(UVC_TRACE_VIDEO, "Device requested null "
1014 "bandwidth, defaulting to lowest.\n");
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001015 bandwidth = 1;
Laurent Pinchart663a4192009-08-06 11:41:17 -03001016 } else {
1017 uvc_trace(UVC_TRACE_VIDEO, "Device requested %u "
1018 "B/frame bandwidth.\n", bandwidth);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001019 }
1020
1021 for (i = 0; i < intf->num_altsetting; ++i) {
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001022 struct usb_host_interface *alts;
1023 unsigned int psize;
1024
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001025 alts = &intf->altsetting[i];
1026 ep = uvc_find_endpoint(alts,
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001027 stream->header.bEndpointAddress);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001028 if (ep == NULL)
1029 continue;
1030
1031 /* Check if the bandwidth is high enough. */
1032 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
1033 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001034 if (psize >= bandwidth && psize <= best_psize) {
1035 altsetting = i;
1036 best_psize = psize;
1037 best_ep = ep;
1038 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001039 }
1040
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001041 if (best_ep == NULL) {
Laurent Pinchart663a4192009-08-06 11:41:17 -03001042 uvc_trace(UVC_TRACE_VIDEO, "No fast enough alt setting "
1043 "for requested bandwidth.\n");
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001044 return -EIO;
Laurent Pinchart663a4192009-08-06 11:41:17 -03001045 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001046
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001047 uvc_trace(UVC_TRACE_VIDEO, "Selecting alternate setting %u "
1048 "(%u B/frame bandwidth).\n", altsetting, best_psize);
1049
1050 ret = usb_set_interface(stream->dev->udev, intfnum, altsetting);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001051 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001052 return ret;
1053
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -03001054 ret = uvc_init_video_isoc(stream, best_ep, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001055 } else {
1056 /* Bulk endpoint, proceed to URB initialization. */
1057 ep = uvc_find_endpoint(&intf->altsetting[0],
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001058 stream->header.bEndpointAddress);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001059 if (ep == NULL)
1060 return -EIO;
1061
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001062 ret = uvc_init_video_bulk(stream, ep, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001063 }
1064
1065 if (ret < 0)
1066 return ret;
1067
1068 /* Submit the URBs. */
1069 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001070 ret = usb_submit_urb(stream->urb[i], gfp_flags);
1071 if (ret < 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001072 uvc_printk(KERN_ERR, "Failed to submit URB %u "
1073 "(%d).\n", i, ret);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001074 uvc_uninit_video(stream, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001075 return ret;
1076 }
1077 }
1078
1079 return 0;
1080}
1081
1082/* --------------------------------------------------------------------------
1083 * Suspend/resume
1084 */
1085
1086/*
1087 * Stop streaming without disabling the video queue.
1088 *
1089 * To let userspace applications resume without trouble, we must not touch the
1090 * video buffers in any way. We mark the device as frozen to make sure the URB
1091 * completion handler won't try to cancel the queue when we kill the URBs.
1092 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001093int uvc_video_suspend(struct uvc_streaming *stream)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001094{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001095 if (!uvc_queue_streaming(&stream->queue))
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001096 return 0;
1097
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001098 stream->frozen = 1;
1099 uvc_uninit_video(stream, 0);
1100 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001101 return 0;
1102}
1103
1104/*
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03001105 * Reconfigure the video interface and restart streaming if it was enabled
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001106 * before suspend.
1107 *
1108 * If an error occurs, disable the video queue. This will wake all pending
1109 * buffers, making sure userspace applications are notified of the problem
1110 * instead of waiting forever.
1111 */
Ming Leid59a7b12011-07-16 00:51:00 -03001112int uvc_video_resume(struct uvc_streaming *stream, int reset)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001113{
1114 int ret;
1115
Ming Leid59a7b12011-07-16 00:51:00 -03001116 /* If the bus has been reset on resume, set the alternate setting to 0.
1117 * This should be the default value, but some devices crash or otherwise
1118 * misbehave if they don't receive a SET_INTERFACE request before any
1119 * other video control request.
1120 */
1121 if (reset)
1122 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1123
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001124 stream->frozen = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001125
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001126 ret = uvc_commit_video(stream, &stream->ctrl);
1127 if (ret < 0) {
1128 uvc_queue_enable(&stream->queue, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001129 return ret;
1130 }
1131
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001132 if (!uvc_queue_streaming(&stream->queue))
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001133 return 0;
1134
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001135 ret = uvc_init_video(stream, GFP_NOIO);
1136 if (ret < 0)
1137 uvc_queue_enable(&stream->queue, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001138
1139 return ret;
1140}
1141
1142/* ------------------------------------------------------------------------
1143 * Video device
1144 */
1145
1146/*
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03001147 * Initialize the UVC video device by switching to alternate setting 0 and
1148 * retrieve the default format.
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001149 *
1150 * Some cameras (namely the Fuji Finepix) set the format and frame
1151 * indexes to zero. The UVC standard doesn't clearly make this a spec
1152 * violation, so try to silently fix the values if possible.
1153 *
1154 * This function is called before registering the device with V4L.
1155 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001156int uvc_video_init(struct uvc_streaming *stream)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001157{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001158 struct uvc_streaming_control *probe = &stream->ctrl;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001159 struct uvc_format *format = NULL;
1160 struct uvc_frame *frame = NULL;
1161 unsigned int i;
1162 int ret;
1163
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001164 if (stream->nformats == 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001165 uvc_printk(KERN_INFO, "No supported video formats found.\n");
1166 return -EINVAL;
1167 }
1168
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001169 atomic_set(&stream->active, 0);
1170
1171 /* Initialize the video buffers queue. */
Laurent Pinchart9bde9f22010-06-17 06:52:37 -03001172 uvc_queue_init(&stream->queue, stream->type, !uvc_no_drop_param);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001173
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001174 /* Alternate setting 0 should be the default, yet the XBox Live Vision
1175 * Cam (and possibly other devices) crash or otherwise misbehave if
1176 * they don't receive a SET_INTERFACE request before any other video
1177 * control request.
1178 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001179 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001180
Laurent Pinchart72362422009-02-14 19:26:56 -03001181 /* Set the streaming probe control with default streaming parameters
1182 * retrieved from the device. Webcams that don't suport GET_DEF
1183 * requests on the probe control will just keep their current streaming
1184 * parameters.
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001185 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001186 if (uvc_get_video_ctrl(stream, probe, 1, UVC_GET_DEF) == 0)
1187 uvc_set_video_ctrl(stream, probe, 1);
Laurent Pinchart72362422009-02-14 19:26:56 -03001188
1189 /* Initialize the streaming parameters with the probe control current
1190 * value. This makes sure SET_CUR requests on the streaming commit
1191 * control will always use values retrieved from a successful GET_CUR
1192 * request on the probe control, as required by the UVC specification.
1193 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001194 ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
Laurent Pinchartb482d922009-06-26 11:39:42 -03001195 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001196 return ret;
1197
1198 /* Check if the default format descriptor exists. Use the first
1199 * available format otherwise.
1200 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001201 for (i = stream->nformats; i > 0; --i) {
1202 format = &stream->format[i-1];
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001203 if (format->index == probe->bFormatIndex)
1204 break;
1205 }
1206
1207 if (format->nframes == 0) {
1208 uvc_printk(KERN_INFO, "No frame descriptor found for the "
1209 "default format.\n");
1210 return -EINVAL;
1211 }
1212
1213 /* Zero bFrameIndex might be correct. Stream-based formats (including
1214 * MPEG-2 TS and DV) do not support frames but have a dummy frame
1215 * descriptor with bFrameIndex set to zero. If the default frame
Laurent Pinchart078f8942009-05-06 12:30:30 -03001216 * descriptor is not found, use the first available frame.
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001217 */
1218 for (i = format->nframes; i > 0; --i) {
1219 frame = &format->frame[i-1];
1220 if (frame->bFrameIndex == probe->bFrameIndex)
1221 break;
1222 }
1223
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001224 probe->bFormatIndex = format->index;
1225 probe->bFrameIndex = frame->bFrameIndex;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001226
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001227 stream->cur_format = format;
1228 stream->cur_frame = frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001229
1230 /* Select the video decoding function */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001231 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1232 if (stream->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
1233 stream->decode = uvc_video_decode_isight;
1234 else if (stream->intf->num_altsetting > 1)
1235 stream->decode = uvc_video_decode_isoc;
Laurent Pinchartff924202008-12-28 22:32:29 -03001236 else
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001237 stream->decode = uvc_video_decode_bulk;
Laurent Pinchartff924202008-12-28 22:32:29 -03001238 } else {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001239 if (stream->intf->num_altsetting == 1)
1240 stream->decode = uvc_video_encode_bulk;
Laurent Pinchartff924202008-12-28 22:32:29 -03001241 else {
1242 uvc_printk(KERN_INFO, "Isochronous endpoints are not "
1243 "supported for video output devices.\n");
1244 return -EINVAL;
1245 }
1246 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001247
1248 return 0;
1249}
1250
1251/*
1252 * Enable or disable the video stream.
1253 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001254int uvc_video_enable(struct uvc_streaming *stream, int enable)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001255{
1256 int ret;
1257
1258 if (!enable) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001259 uvc_uninit_video(stream, 1);
1260 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1261 uvc_queue_enable(&stream->queue, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001262 return 0;
1263 }
1264
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001265 ret = uvc_queue_enable(&stream->queue, 1);
1266 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001267 return ret;
1268
Laurent Pinchart23867b22008-11-12 11:46:43 -03001269 /* Commit the streaming parameters. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001270 ret = uvc_commit_video(stream, &stream->ctrl);
Sjoerd Simonsaa122d42011-05-30 15:02:00 -03001271 if (ret < 0) {
1272 uvc_queue_enable(&stream->queue, 0);
Laurent Pinchart23867b22008-11-12 11:46:43 -03001273 return ret;
Sjoerd Simonsaa122d42011-05-30 15:02:00 -03001274 }
Laurent Pinchart23867b22008-11-12 11:46:43 -03001275
Laurent Pinchart24c3aae2011-10-25 09:03:42 -03001276 ret = uvc_init_video(stream, GFP_KERNEL);
1277 if (ret < 0) {
1278 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1279 uvc_queue_enable(&stream->queue, 0);
1280 }
Hans Verkuilf87086e2008-07-18 00:50:58 -03001281
Laurent Pinchart24c3aae2011-10-25 09:03:42 -03001282 return ret;
1283}