blob: 6b0666be370fdb6e6cc23398f2939b5865fa828d [file] [log] [blame]
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001/*
2 * uvc_video.c -- USB Video Class driver - Video handling
3 *
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03004 * Copyright (C) 2005-2009
Laurent Pinchartc0efd232008-06-30 15:04:50 -03005 * Laurent Pinchart (laurent.pinchart@skynet.be)
6 *
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>
17#include <linux/usb.h>
18#include <linux/videodev2.h>
19#include <linux/vmalloc.h>
20#include <linux/wait.h>
21#include <asm/atomic.h>
22#include <asm/unaligned.h>
23
24#include <media/v4l2-common.h>
25
26#include "uvcvideo.h"
27
28/* ------------------------------------------------------------------------
29 * UVC Controls
30 */
31
32static int __uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
33 __u8 intfnum, __u8 cs, void *data, __u16 size,
34 int timeout)
35{
36 __u8 type = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
37 unsigned int pipe;
Laurent Pinchartc0efd232008-06-30 15:04:50 -030038
39 pipe = (query & 0x80) ? usb_rcvctrlpipe(dev->udev, 0)
40 : usb_sndctrlpipe(dev->udev, 0);
41 type |= (query & 0x80) ? USB_DIR_IN : USB_DIR_OUT;
42
Laurent Pinchart44f00792008-11-08 19:14:50 -030043 return usb_control_msg(dev->udev, pipe, query, type, cs << 8,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030044 unit << 8 | intfnum, data, size, timeout);
Laurent Pinchart44f00792008-11-08 19:14:50 -030045}
Laurent Pinchartc0efd232008-06-30 15:04:50 -030046
Laurent Pinchart44f00792008-11-08 19:14:50 -030047int uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
48 __u8 intfnum, __u8 cs, void *data, __u16 size)
49{
50 int ret;
51
52 ret = __uvc_query_ctrl(dev, query, unit, intfnum, cs, data, size,
53 UVC_CTRL_CONTROL_TIMEOUT);
Laurent Pinchartc0efd232008-06-30 15:04:50 -030054 if (ret != size) {
55 uvc_printk(KERN_ERR, "Failed to query (%u) UVC control %u "
56 "(unit %u) : %d (exp. %u).\n", query, cs, unit, ret,
57 size);
58 return -EIO;
59 }
60
61 return 0;
62}
63
Laurent Pinchart35f02a62009-06-28 08:37:50 -030064static void uvc_fixup_video_ctrl(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030065 struct uvc_streaming_control *ctrl)
66{
67 struct uvc_format *format;
Laurent Pinchart078f8942009-05-06 12:30:30 -030068 struct uvc_frame *frame = NULL;
69 unsigned int i;
Laurent Pinchartc0efd232008-06-30 15:04:50 -030070
71 if (ctrl->bFormatIndex <= 0 ||
Laurent Pinchart35f02a62009-06-28 08:37:50 -030072 ctrl->bFormatIndex > stream->nformats)
Laurent Pinchartc0efd232008-06-30 15:04:50 -030073 return;
74
Laurent Pinchart35f02a62009-06-28 08:37:50 -030075 format = &stream->format[ctrl->bFormatIndex - 1];
Laurent Pinchartc0efd232008-06-30 15:04:50 -030076
Laurent Pinchart078f8942009-05-06 12:30:30 -030077 for (i = 0; i < format->nframes; ++i) {
78 if (format->frame[i].bFrameIndex == ctrl->bFrameIndex) {
79 frame = &format->frame[i];
80 break;
81 }
82 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -030083
Laurent Pinchart078f8942009-05-06 12:30:30 -030084 if (frame == NULL)
85 return;
Laurent Pinchartc0efd232008-06-30 15:04:50 -030086
87 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) ||
88 (ctrl->dwMaxVideoFrameSize == 0 &&
Laurent Pinchart35f02a62009-06-28 08:37:50 -030089 stream->dev->uvc_version < 0x0110))
Laurent Pinchartc0efd232008-06-30 15:04:50 -030090 ctrl->dwMaxVideoFrameSize =
91 frame->dwMaxVideoFrameBufferSize;
Laurent Pinchart50144ae2009-02-16 17:41:52 -030092
Laurent Pincharta2e35af2009-11-04 11:09:12 -030093 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) &&
94 stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH &&
Laurent Pinchart35f02a62009-06-28 08:37:50 -030095 stream->intf->num_altsetting > 1) {
Laurent Pinchart50144ae2009-02-16 17:41:52 -030096 u32 interval;
97 u32 bandwidth;
98
99 interval = (ctrl->dwFrameInterval > 100000)
100 ? ctrl->dwFrameInterval
101 : frame->dwFrameInterval[0];
102
103 /* Compute a bandwidth estimation by multiplying the frame
104 * size by the number of video frames per second, divide the
105 * result by the number of USB frames (or micro-frames for
106 * high-speed devices) per second and add the UVC header size
107 * (assumed to be 12 bytes long).
108 */
109 bandwidth = frame->wWidth * frame->wHeight / 8 * format->bpp;
110 bandwidth *= 10000000 / interval + 1;
111 bandwidth /= 1000;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300112 if (stream->dev->udev->speed == USB_SPEED_HIGH)
Laurent Pinchart50144ae2009-02-16 17:41:52 -0300113 bandwidth /= 8;
114 bandwidth += 12;
115
116 ctrl->dwMaxPayloadTransferSize = bandwidth;
117 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300118}
119
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300120static int uvc_get_video_ctrl(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300121 struct uvc_streaming_control *ctrl, int probe, __u8 query)
122{
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300123 __u8 *data;
124 __u16 size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300125 int ret;
126
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300127 size = stream->dev->uvc_version >= 0x0110 ? 34 : 26;
Julia Lawalld2be7642009-09-17 23:44:01 -0300128 if ((stream->dev->quirks & UVC_QUIRK_PROBE_DEF) &&
129 query == UVC_GET_DEF)
130 return -EIO;
131
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300132 data = kmalloc(size, GFP_KERNEL);
133 if (data == NULL)
134 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300135
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300136 ret = __uvc_query_ctrl(stream->dev, query, 0, stream->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300137 probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
Laurent Pinchartb232a012009-10-09 20:55:23 -0300138 size, uvc_timeout_param);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300139
Laurent Pinchartb482d922009-06-26 11:39:42 -0300140 if ((query == UVC_GET_MIN || query == UVC_GET_MAX) && ret == 2) {
Laurent Pinchart44f00792008-11-08 19:14:50 -0300141 /* Some cameras, mostly based on Bison Electronics chipsets,
142 * answer a GET_MIN or GET_MAX request with the wCompQuality
143 * field only.
144 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300145 uvc_warn_once(stream->dev, UVC_WARN_MINMAX, "UVC non "
Laurent Pinchart44f00792008-11-08 19:14:50 -0300146 "compliance - GET_MIN/MAX(PROBE) incorrectly "
147 "supported. Enabling workaround.\n");
Julia Lawallddb0b342009-12-10 17:14:27 -0300148 memset(ctrl, 0, sizeof *ctrl);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300149 ctrl->wCompQuality = le16_to_cpup((__le16 *)data);
150 ret = 0;
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300151 goto out;
Laurent Pinchartb482d922009-06-26 11:39:42 -0300152 } else if (query == UVC_GET_DEF && probe == 1 && ret != size) {
Laurent Pinchart44f00792008-11-08 19:14:50 -0300153 /* Many cameras don't support the GET_DEF request on their
154 * video probe control. Warn once and return, the caller will
155 * fall back to GET_CUR.
156 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300157 uvc_warn_once(stream->dev, UVC_WARN_PROBE_DEF, "UVC non "
Laurent Pinchart44f00792008-11-08 19:14:50 -0300158 "compliance - GET_DEF(PROBE) not supported. "
159 "Enabling workaround.\n");
160 ret = -EIO;
161 goto out;
162 } else if (ret != size) {
163 uvc_printk(KERN_ERR, "Failed to query (%u) UVC %s control : "
164 "%d (exp. %u).\n", query, probe ? "probe" : "commit",
165 ret, size);
166 ret = -EIO;
167 goto out;
168 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300169
170 ctrl->bmHint = le16_to_cpup((__le16 *)&data[0]);
171 ctrl->bFormatIndex = data[2];
172 ctrl->bFrameIndex = data[3];
173 ctrl->dwFrameInterval = le32_to_cpup((__le32 *)&data[4]);
174 ctrl->wKeyFrameRate = le16_to_cpup((__le16 *)&data[8]);
175 ctrl->wPFrameRate = le16_to_cpup((__le16 *)&data[10]);
176 ctrl->wCompQuality = le16_to_cpup((__le16 *)&data[12]);
177 ctrl->wCompWindowSize = le16_to_cpup((__le16 *)&data[14]);
178 ctrl->wDelay = le16_to_cpup((__le16 *)&data[16]);
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300179 ctrl->dwMaxVideoFrameSize = get_unaligned_le32(&data[18]);
180 ctrl->dwMaxPayloadTransferSize = get_unaligned_le32(&data[22]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300181
182 if (size == 34) {
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300183 ctrl->dwClockFrequency = get_unaligned_le32(&data[26]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300184 ctrl->bmFramingInfo = data[30];
185 ctrl->bPreferedVersion = data[31];
186 ctrl->bMinVersion = data[32];
187 ctrl->bMaxVersion = data[33];
188 } else {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300189 ctrl->dwClockFrequency = stream->dev->clock_frequency;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300190 ctrl->bmFramingInfo = 0;
191 ctrl->bPreferedVersion = 0;
192 ctrl->bMinVersion = 0;
193 ctrl->bMaxVersion = 0;
194 }
195
Laurent Pinchart50144ae2009-02-16 17:41:52 -0300196 /* Some broken devices return null or wrong dwMaxVideoFrameSize and
197 * dwMaxPayloadTransferSize fields. Try to get the value from the
198 * format and frame descriptors.
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300199 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300200 uvc_fixup_video_ctrl(stream, ctrl);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300201 ret = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300202
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300203out:
204 kfree(data);
205 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300206}
207
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300208static int uvc_set_video_ctrl(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300209 struct uvc_streaming_control *ctrl, int probe)
210{
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300211 __u8 *data;
212 __u16 size;
213 int ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300214
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300215 size = stream->dev->uvc_version >= 0x0110 ? 34 : 26;
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300216 data = kzalloc(size, GFP_KERNEL);
217 if (data == NULL)
218 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300219
220 *(__le16 *)&data[0] = cpu_to_le16(ctrl->bmHint);
221 data[2] = ctrl->bFormatIndex;
222 data[3] = ctrl->bFrameIndex;
223 *(__le32 *)&data[4] = cpu_to_le32(ctrl->dwFrameInterval);
224 *(__le16 *)&data[8] = cpu_to_le16(ctrl->wKeyFrameRate);
225 *(__le16 *)&data[10] = cpu_to_le16(ctrl->wPFrameRate);
226 *(__le16 *)&data[12] = cpu_to_le16(ctrl->wCompQuality);
227 *(__le16 *)&data[14] = cpu_to_le16(ctrl->wCompWindowSize);
228 *(__le16 *)&data[16] = cpu_to_le16(ctrl->wDelay);
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300229 put_unaligned_le32(ctrl->dwMaxVideoFrameSize, &data[18]);
230 put_unaligned_le32(ctrl->dwMaxPayloadTransferSize, &data[22]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300231
232 if (size == 34) {
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300233 put_unaligned_le32(ctrl->dwClockFrequency, &data[26]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300234 data[30] = ctrl->bmFramingInfo;
235 data[31] = ctrl->bPreferedVersion;
236 data[32] = ctrl->bMinVersion;
237 data[33] = ctrl->bMaxVersion;
238 }
239
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300240 ret = __uvc_query_ctrl(stream->dev, UVC_SET_CUR, 0, stream->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300241 probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
Laurent Pinchartb232a012009-10-09 20:55:23 -0300242 size, uvc_timeout_param);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300243 if (ret != size) {
244 uvc_printk(KERN_ERR, "Failed to set UVC %s control : "
245 "%d (exp. %u).\n", probe ? "probe" : "commit",
246 ret, size);
247 ret = -EIO;
248 }
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300249
250 kfree(data);
251 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300252}
253
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300254int uvc_probe_video(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300255 struct uvc_streaming_control *probe)
256{
257 struct uvc_streaming_control probe_min, probe_max;
258 __u16 bandwidth;
259 unsigned int i;
260 int ret;
261
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300262 mutex_lock(&stream->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300263
264 /* Perform probing. The device should adjust the requested values
265 * according to its capabilities. However, some devices, namely the
266 * first generation UVC Logitech webcams, don't implement the Video
267 * Probe control properly, and just return the needed bandwidth. For
268 * that reason, if the needed bandwidth exceeds the maximum available
269 * bandwidth, try to lower the quality.
270 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300271 ret = uvc_set_video_ctrl(stream, probe, 1);
272 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300273 goto done;
274
275 /* Get the minimum and maximum values for compression settings. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300276 if (!(stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX)) {
277 ret = uvc_get_video_ctrl(stream, &probe_min, 1, UVC_GET_MIN);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300278 if (ret < 0)
279 goto done;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300280 ret = uvc_get_video_ctrl(stream, &probe_max, 1, UVC_GET_MAX);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300281 if (ret < 0)
282 goto done;
283
284 probe->wCompQuality = probe_max.wCompQuality;
285 }
286
287 for (i = 0; i < 2; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300288 ret = uvc_set_video_ctrl(stream, probe, 1);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300289 if (ret < 0)
290 goto done;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300291 ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300292 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300293 goto done;
294
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300295 if (stream->intf->num_altsetting == 1)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300296 break;
297
298 bandwidth = probe->dwMaxPayloadTransferSize;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300299 if (bandwidth <= stream->maxpsize)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300300 break;
301
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300302 if (stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300303 ret = -ENOSPC;
304 goto done;
305 }
306
307 /* TODO: negotiate compression parameters */
308 probe->wKeyFrameRate = probe_min.wKeyFrameRate;
309 probe->wPFrameRate = probe_min.wPFrameRate;
310 probe->wCompQuality = probe_max.wCompQuality;
311 probe->wCompWindowSize = probe_min.wCompWindowSize;
312 }
313
314done:
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300315 mutex_unlock(&stream->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300316 return ret;
317}
318
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300319int uvc_commit_video(struct uvc_streaming *stream,
Laurent Pinchart44f00792008-11-08 19:14:50 -0300320 struct uvc_streaming_control *probe)
321{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300322 return uvc_set_video_ctrl(stream, probe, 0);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300323}
324
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300325/* ------------------------------------------------------------------------
326 * Video codecs
327 */
328
329/* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
330#define UVC_STREAM_EOH (1 << 7)
331#define UVC_STREAM_ERR (1 << 6)
332#define UVC_STREAM_STI (1 << 5)
333#define UVC_STREAM_RES (1 << 4)
334#define UVC_STREAM_SCR (1 << 3)
335#define UVC_STREAM_PTS (1 << 2)
336#define UVC_STREAM_EOF (1 << 1)
337#define UVC_STREAM_FID (1 << 0)
338
339/* Video payload decoding is handled by uvc_video_decode_start(),
340 * uvc_video_decode_data() and uvc_video_decode_end().
341 *
342 * uvc_video_decode_start is called with URB data at the start of a bulk or
343 * isochronous payload. It processes header data and returns the header size
344 * in bytes if successful. If an error occurs, it returns a negative error
345 * code. The following error codes have special meanings.
346 *
347 * - EAGAIN informs the caller that the current video buffer should be marked
348 * as done, and that the function should be called again with the same data
349 * and a new video buffer. This is used when end of frame conditions can be
350 * reliably detected at the beginning of the next frame only.
351 *
352 * If an error other than -EAGAIN is returned, the caller will drop the current
353 * payload. No call to uvc_video_decode_data and uvc_video_decode_end will be
354 * made until the next payload. -ENODATA can be used to drop the current
355 * payload if no other error code is appropriate.
356 *
357 * uvc_video_decode_data is called for every URB with URB data. It copies the
358 * data to the video buffer.
359 *
360 * uvc_video_decode_end is called with header data at the end of a bulk or
361 * isochronous payload. It performs any additional header data processing and
362 * returns 0 or a negative error code if an error occured. As header data have
363 * already been processed by uvc_video_decode_start, this functions isn't
364 * required to perform sanity checks a second time.
365 *
366 * For isochronous transfers where a payload is always transfered in a single
367 * URB, the three functions will be called in a row.
368 *
369 * To let the decoder process header data and update its internal state even
370 * when no video buffer is available, uvc_video_decode_start must be prepared
371 * to be called with a NULL buf parameter. uvc_video_decode_data and
372 * uvc_video_decode_end will never be called with a NULL buffer.
373 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300374static int uvc_video_decode_start(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300375 struct uvc_buffer *buf, const __u8 *data, int len)
376{
377 __u8 fid;
378
379 /* Sanity checks:
380 * - packet must be at least 2 bytes long
381 * - bHeaderLength value must be at least 2 bytes (see above)
382 * - bHeaderLength value can't be larger than the packet size.
383 */
384 if (len < 2 || data[0] < 2 || data[0] > len)
385 return -EINVAL;
386
387 /* Skip payloads marked with the error bit ("error frames"). */
388 if (data[1] & UVC_STREAM_ERR) {
389 uvc_trace(UVC_TRACE_FRAME, "Dropping payload (error bit "
390 "set).\n");
391 return -ENODATA;
392 }
393
394 fid = data[1] & UVC_STREAM_FID;
395
396 /* Store the payload FID bit and return immediately when the buffer is
397 * NULL.
398 */
399 if (buf == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300400 stream->last_fid = fid;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300401 return -ENODATA;
402 }
403
404 /* Synchronize to the input stream by waiting for the FID bit to be
405 * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE.
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300406 * stream->last_fid is initialized to -1, so the first isochronous
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300407 * frame will always be in sync.
408 *
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300409 * If the device doesn't toggle the FID bit, invert stream->last_fid
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300410 * when the EOF bit is set to force synchronisation on the next packet.
411 */
412 if (buf->state != UVC_BUF_STATE_ACTIVE) {
Laurent Pinchart310fe522009-12-09 22:57:48 -0300413 struct timespec ts;
414
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300415 if (fid == stream->last_fid) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300416 uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of "
417 "sync).\n");
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300418 if ((stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) &&
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300419 (data[1] & UVC_STREAM_EOF))
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300420 stream->last_fid ^= UVC_STREAM_FID;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300421 return -ENODATA;
422 }
423
Laurent Pinchart310fe522009-12-09 22:57:48 -0300424 if (uvc_clock_param == CLOCK_MONOTONIC)
425 ktime_get_ts(&ts);
426 else
427 ktime_get_real_ts(&ts);
428
429 buf->buf.timestamp.tv_sec = ts.tv_sec;
430 buf->buf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
431
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300432 /* TODO: Handle PTS and SCR. */
433 buf->state = UVC_BUF_STATE_ACTIVE;
434 }
435
436 /* Mark the buffer as done if we're at the beginning of a new frame.
437 * End of frame detection is better implemented by checking the EOF
438 * bit (FID bit toggling is delayed by one frame compared to the EOF
439 * bit), but some devices don't set the bit at end of frame (and the
440 * last payload can be lost anyway). We thus must check if the FID has
441 * been toggled.
442 *
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300443 * stream->last_fid is initialized to -1, so the first isochronous
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300444 * frame will never trigger an end of frame detection.
445 *
446 * Empty buffers (bytesused == 0) don't trigger end of frame detection
447 * as it doesn't make sense to return an empty buffer. This also
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300448 * avoids detecting end of frame conditions at FID toggling if the
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300449 * previous payload had the EOF bit set.
450 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300451 if (fid != stream->last_fid && buf->buf.bytesused != 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300452 uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit "
453 "toggled).\n");
Laurent Pinchartd7c0d432009-12-16 21:20:45 -0300454 buf->state = UVC_BUF_STATE_READY;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300455 return -EAGAIN;
456 }
457
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300458 stream->last_fid = fid;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300459
460 return data[0];
461}
462
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300463static void uvc_video_decode_data(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300464 struct uvc_buffer *buf, const __u8 *data, int len)
465{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300466 struct uvc_video_queue *queue = &stream->queue;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300467 unsigned int maxlen, nbytes;
468 void *mem;
469
470 if (len <= 0)
471 return;
472
473 /* Copy the video data to the buffer. */
474 maxlen = buf->buf.length - buf->buf.bytesused;
475 mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused;
476 nbytes = min((unsigned int)len, maxlen);
477 memcpy(mem, data, nbytes);
478 buf->buf.bytesused += nbytes;
479
480 /* Complete the current frame if the buffer size was exceeded. */
481 if (len > maxlen) {
482 uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n");
Laurent Pinchartd7c0d432009-12-16 21:20:45 -0300483 buf->state = UVC_BUF_STATE_READY;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300484 }
485}
486
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300487static void uvc_video_decode_end(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300488 struct uvc_buffer *buf, const __u8 *data, int len)
489{
490 /* Mark the buffer as done if the EOF marker is set. */
491 if (data[1] & UVC_STREAM_EOF && buf->buf.bytesused != 0) {
492 uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n");
493 if (data[0] == len)
494 uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n");
Laurent Pinchartd7c0d432009-12-16 21:20:45 -0300495 buf->state = UVC_BUF_STATE_READY;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300496 if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID)
497 stream->last_fid ^= UVC_STREAM_FID;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300498 }
499}
500
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300501/* Video payload encoding is handled by uvc_video_encode_header() and
502 * uvc_video_encode_data(). Only bulk transfers are currently supported.
503 *
504 * uvc_video_encode_header is called at the start of a payload. It adds header
505 * data to the transfer buffer and returns the header size. As the only known
506 * UVC output device transfers a whole frame in a single payload, the EOF bit
507 * is always set in the header.
508 *
509 * uvc_video_encode_data is called for every URB and copies the data from the
510 * video buffer to the transfer buffer.
511 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300512static int uvc_video_encode_header(struct uvc_streaming *stream,
Laurent Pinchartff924202008-12-28 22:32:29 -0300513 struct uvc_buffer *buf, __u8 *data, int len)
514{
515 data[0] = 2; /* Header length */
516 data[1] = UVC_STREAM_EOH | UVC_STREAM_EOF
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300517 | (stream->last_fid & UVC_STREAM_FID);
Laurent Pinchartff924202008-12-28 22:32:29 -0300518 return 2;
519}
520
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300521static int uvc_video_encode_data(struct uvc_streaming *stream,
Laurent Pinchartff924202008-12-28 22:32:29 -0300522 struct uvc_buffer *buf, __u8 *data, int len)
523{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300524 struct uvc_video_queue *queue = &stream->queue;
Laurent Pinchartff924202008-12-28 22:32:29 -0300525 unsigned int nbytes;
526 void *mem;
527
528 /* Copy video data to the URB buffer. */
529 mem = queue->mem + buf->buf.m.offset + queue->buf_used;
530 nbytes = min((unsigned int)len, buf->buf.bytesused - queue->buf_used);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300531 nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size,
Laurent Pinchartff924202008-12-28 22:32:29 -0300532 nbytes);
533 memcpy(data, mem, nbytes);
534
535 queue->buf_used += nbytes;
536
537 return nbytes;
538}
539
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300540/* ------------------------------------------------------------------------
541 * URB handling
542 */
543
544/*
545 * Completion handler for video URBs.
546 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300547static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
548 struct uvc_buffer *buf)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300549{
550 u8 *mem;
551 int ret, i;
552
553 for (i = 0; i < urb->number_of_packets; ++i) {
554 if (urb->iso_frame_desc[i].status < 0) {
555 uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
556 "lost (%d).\n", urb->iso_frame_desc[i].status);
557 continue;
558 }
559
560 /* Decode the payload header. */
561 mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
562 do {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300563 ret = uvc_video_decode_start(stream, buf, mem,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300564 urb->iso_frame_desc[i].actual_length);
565 if (ret == -EAGAIN)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300566 buf = uvc_queue_next_buffer(&stream->queue,
567 buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300568 } while (ret == -EAGAIN);
569
570 if (ret < 0)
571 continue;
572
573 /* Decode the payload data. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300574 uvc_video_decode_data(stream, buf, mem + ret,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300575 urb->iso_frame_desc[i].actual_length - ret);
576
577 /* Process the header again. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300578 uvc_video_decode_end(stream, buf, mem,
Laurent Pinchart08ebd002008-08-29 17:37:44 -0300579 urb->iso_frame_desc[i].actual_length);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300580
Laurent Pinchartd7c0d432009-12-16 21:20:45 -0300581 if (buf->state == UVC_BUF_STATE_READY)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300582 buf = uvc_queue_next_buffer(&stream->queue, buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300583 }
584}
585
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300586static void uvc_video_decode_bulk(struct urb *urb, struct uvc_streaming *stream,
587 struct uvc_buffer *buf)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300588{
589 u8 *mem;
590 int len, ret;
591
Laurent Pinchartc90e7772009-02-14 19:39:08 -0300592 if (urb->actual_length == 0)
593 return;
594
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300595 mem = urb->transfer_buffer;
596 len = urb->actual_length;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300597 stream->bulk.payload_size += len;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300598
599 /* If the URB is the first of its payload, decode and save the
600 * header.
601 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300602 if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300603 do {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300604 ret = uvc_video_decode_start(stream, buf, mem, len);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300605 if (ret == -EAGAIN)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300606 buf = uvc_queue_next_buffer(&stream->queue,
607 buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300608 } while (ret == -EAGAIN);
609
610 /* If an error occured skip the rest of the payload. */
611 if (ret < 0 || buf == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300612 stream->bulk.skip_payload = 1;
Laurent Pinchartf8dd4af2008-12-16 10:41:57 -0300613 } else {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300614 memcpy(stream->bulk.header, mem, ret);
615 stream->bulk.header_size = ret;
Laurent Pinchartf8dd4af2008-12-16 10:41:57 -0300616
617 mem += ret;
618 len -= ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300619 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300620 }
621
622 /* The buffer queue might have been cancelled while a bulk transfer
623 * was in progress, so we can reach here with buf equal to NULL. Make
624 * sure buf is never dereferenced if NULL.
625 */
626
627 /* Process video data. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300628 if (!stream->bulk.skip_payload && buf != NULL)
629 uvc_video_decode_data(stream, buf, mem, len);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300630
631 /* Detect the payload end by a URB smaller than the maximum size (or
632 * a payload size equal to the maximum) and process the header again.
633 */
634 if (urb->actual_length < urb->transfer_buffer_length ||
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300635 stream->bulk.payload_size >= stream->bulk.max_payload_size) {
636 if (!stream->bulk.skip_payload && buf != NULL) {
637 uvc_video_decode_end(stream, buf, stream->bulk.header,
638 stream->bulk.payload_size);
Laurent Pinchartd7c0d432009-12-16 21:20:45 -0300639 if (buf->state == UVC_BUF_STATE_READY)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300640 buf = uvc_queue_next_buffer(&stream->queue,
641 buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300642 }
643
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300644 stream->bulk.header_size = 0;
645 stream->bulk.skip_payload = 0;
646 stream->bulk.payload_size = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300647 }
648}
649
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300650static void uvc_video_encode_bulk(struct urb *urb, struct uvc_streaming *stream,
651 struct uvc_buffer *buf)
Laurent Pinchartff924202008-12-28 22:32:29 -0300652{
653 u8 *mem = urb->transfer_buffer;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300654 int len = stream->urb_size, ret;
Laurent Pinchartff924202008-12-28 22:32:29 -0300655
656 if (buf == NULL) {
657 urb->transfer_buffer_length = 0;
658 return;
659 }
660
661 /* If the URB is the first of its payload, add the header. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300662 if (stream->bulk.header_size == 0) {
663 ret = uvc_video_encode_header(stream, buf, mem, len);
664 stream->bulk.header_size = ret;
665 stream->bulk.payload_size += ret;
Laurent Pinchartff924202008-12-28 22:32:29 -0300666 mem += ret;
667 len -= ret;
668 }
669
670 /* Process video data. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300671 ret = uvc_video_encode_data(stream, buf, mem, len);
Laurent Pinchartff924202008-12-28 22:32:29 -0300672
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300673 stream->bulk.payload_size += ret;
Laurent Pinchartff924202008-12-28 22:32:29 -0300674 len -= ret;
675
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300676 if (buf->buf.bytesused == stream->queue.buf_used ||
677 stream->bulk.payload_size == stream->bulk.max_payload_size) {
678 if (buf->buf.bytesused == stream->queue.buf_used) {
679 stream->queue.buf_used = 0;
Laurent Pinchartd7c0d432009-12-16 21:20:45 -0300680 buf->state = UVC_BUF_STATE_READY;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300681 uvc_queue_next_buffer(&stream->queue, buf);
682 stream->last_fid ^= UVC_STREAM_FID;
Laurent Pinchartff924202008-12-28 22:32:29 -0300683 }
684
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300685 stream->bulk.header_size = 0;
686 stream->bulk.payload_size = 0;
Laurent Pinchartff924202008-12-28 22:32:29 -0300687 }
688
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300689 urb->transfer_buffer_length = stream->urb_size - len;
Laurent Pinchartff924202008-12-28 22:32:29 -0300690}
691
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300692static void uvc_video_complete(struct urb *urb)
693{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300694 struct uvc_streaming *stream = urb->context;
695 struct uvc_video_queue *queue = &stream->queue;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300696 struct uvc_buffer *buf = NULL;
697 unsigned long flags;
698 int ret;
699
700 switch (urb->status) {
701 case 0:
702 break;
703
704 default:
705 uvc_printk(KERN_WARNING, "Non-zero status (%d) in video "
706 "completion handler.\n", urb->status);
707
708 case -ENOENT: /* usb_kill_urb() called. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300709 if (stream->frozen)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300710 return;
711
712 case -ECONNRESET: /* usb_unlink_urb() called. */
713 case -ESHUTDOWN: /* The endpoint is being disabled. */
714 uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
715 return;
716 }
717
718 spin_lock_irqsave(&queue->irqlock, flags);
719 if (!list_empty(&queue->irqqueue))
720 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
721 queue);
722 spin_unlock_irqrestore(&queue->irqlock, flags);
723
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300724 stream->decode(urb, stream, buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300725
726 if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
727 uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n",
728 ret);
729 }
730}
731
732/*
Laurent Pincharte01117c2008-07-04 00:36:21 -0300733 * Free transfer buffers.
734 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300735static void uvc_free_urb_buffers(struct uvc_streaming *stream)
Laurent Pincharte01117c2008-07-04 00:36:21 -0300736{
737 unsigned int i;
738
739 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300740 if (stream->urb_buffer[i]) {
741 usb_buffer_free(stream->dev->udev, stream->urb_size,
742 stream->urb_buffer[i], stream->urb_dma[i]);
743 stream->urb_buffer[i] = NULL;
Laurent Pincharte01117c2008-07-04 00:36:21 -0300744 }
745 }
746
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300747 stream->urb_size = 0;
Laurent Pincharte01117c2008-07-04 00:36:21 -0300748}
749
750/*
751 * Allocate transfer buffers. This function can be called with buffers
752 * already allocated when resuming from suspend, in which case it will
753 * return without touching the buffers.
754 *
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300755 * Limit the buffer size to UVC_MAX_PACKETS bulk/isochronous packets. If the
756 * system is too low on memory try successively smaller numbers of packets
757 * until allocation succeeds.
758 *
759 * Return the number of allocated packets on success or 0 when out of memory.
Laurent Pincharte01117c2008-07-04 00:36:21 -0300760 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300761static int uvc_alloc_urb_buffers(struct uvc_streaming *stream,
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300762 unsigned int size, unsigned int psize, gfp_t gfp_flags)
Laurent Pincharte01117c2008-07-04 00:36:21 -0300763{
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300764 unsigned int npackets;
Laurent Pincharte01117c2008-07-04 00:36:21 -0300765 unsigned int i;
766
767 /* Buffers are already allocated, bail out. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300768 if (stream->urb_size)
769 return stream->urb_size / psize;
Laurent Pincharte01117c2008-07-04 00:36:21 -0300770
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300771 /* Compute the number of packets. Bulk endpoints might transfer UVC
772 * payloads accross multiple URBs.
773 */
774 npackets = DIV_ROUND_UP(size, psize);
775 if (npackets > UVC_MAX_PACKETS)
776 npackets = UVC_MAX_PACKETS;
777
778 /* Retry allocations until one succeed. */
779 for (; npackets > 1; npackets /= 2) {
780 for (i = 0; i < UVC_URBS; ++i) {
Ming Leifd1b6bb2009-09-27 05:30:34 -0300781 stream->urb_size = psize * npackets;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300782 stream->urb_buffer[i] = usb_buffer_alloc(
Ming Leifd1b6bb2009-09-27 05:30:34 -0300783 stream->dev->udev, stream->urb_size,
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300784 gfp_flags | __GFP_NOWARN, &stream->urb_dma[i]);
785 if (!stream->urb_buffer[i]) {
786 uvc_free_urb_buffers(stream);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300787 break;
788 }
789 }
790
791 if (i == UVC_URBS) {
Laurent Pinchart663a4192009-08-06 11:41:17 -0300792 uvc_trace(UVC_TRACE_VIDEO, "Allocated %u URB buffers "
793 "of %ux%u bytes each.\n", UVC_URBS, npackets,
794 psize);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300795 return npackets;
Laurent Pincharte01117c2008-07-04 00:36:21 -0300796 }
797 }
798
Laurent Pinchart663a4192009-08-06 11:41:17 -0300799 uvc_trace(UVC_TRACE_VIDEO, "Failed to allocate URB buffers (%u bytes "
800 "per packet).\n", psize);
Laurent Pincharte01117c2008-07-04 00:36:21 -0300801 return 0;
802}
803
804/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300805 * Uninitialize isochronous/bulk URBs and free transfer buffers.
806 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300807static void uvc_uninit_video(struct uvc_streaming *stream, int free_buffers)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300808{
809 struct urb *urb;
810 unsigned int i;
811
812 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300813 urb = stream->urb[i];
814 if (urb == NULL)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300815 continue;
816
817 usb_kill_urb(urb);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300818 usb_free_urb(urb);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300819 stream->urb[i] = NULL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300820 }
Laurent Pincharte01117c2008-07-04 00:36:21 -0300821
822 if (free_buffers)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300823 uvc_free_urb_buffers(stream);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300824}
825
826/*
827 * Initialize isochronous URBs and allocate transfer buffers. The packet size
828 * is given by the endpoint.
829 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300830static int uvc_init_video_isoc(struct uvc_streaming *stream,
Laurent Pinchart29135872008-07-04 00:35:26 -0300831 struct usb_host_endpoint *ep, gfp_t gfp_flags)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300832{
833 struct urb *urb;
834 unsigned int npackets, i, j;
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300835 u16 psize;
836 u32 size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300837
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300838 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
839 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300840 size = stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300841
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300842 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300843 if (npackets == 0)
844 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300845
846 size = npackets * psize;
847
848 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart29135872008-07-04 00:35:26 -0300849 urb = usb_alloc_urb(npackets, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300850 if (urb == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300851 uvc_uninit_video(stream, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300852 return -ENOMEM;
853 }
854
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300855 urb->dev = stream->dev->udev;
856 urb->context = stream;
857 urb->pipe = usb_rcvisocpipe(stream->dev->udev,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300858 ep->desc.bEndpointAddress);
859 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
860 urb->interval = ep->desc.bInterval;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300861 urb->transfer_buffer = stream->urb_buffer[i];
862 urb->transfer_dma = stream->urb_dma[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300863 urb->complete = uvc_video_complete;
864 urb->number_of_packets = npackets;
865 urb->transfer_buffer_length = size;
866
867 for (j = 0; j < npackets; ++j) {
868 urb->iso_frame_desc[j].offset = j * psize;
869 urb->iso_frame_desc[j].length = psize;
870 }
871
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300872 stream->urb[i] = urb;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300873 }
874
875 return 0;
876}
877
878/*
879 * Initialize bulk URBs and allocate transfer buffers. The packet size is
880 * given by the endpoint.
881 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300882static int uvc_init_video_bulk(struct uvc_streaming *stream,
Laurent Pinchart29135872008-07-04 00:35:26 -0300883 struct usb_host_endpoint *ep, gfp_t gfp_flags)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300884{
885 struct urb *urb;
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300886 unsigned int npackets, pipe, i;
887 u16 psize;
888 u32 size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300889
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300890 psize = le16_to_cpu(ep->desc.wMaxPacketSize) & 0x07ff;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300891 size = stream->ctrl.dwMaxPayloadTransferSize;
892 stream->bulk.max_payload_size = size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300893
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300894 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300895 if (npackets == 0)
Laurent Pincharte01117c2008-07-04 00:36:21 -0300896 return -ENOMEM;
897
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300898 size = npackets * psize;
899
Laurent Pinchartff924202008-12-28 22:32:29 -0300900 if (usb_endpoint_dir_in(&ep->desc))
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300901 pipe = usb_rcvbulkpipe(stream->dev->udev,
Laurent Pinchartff924202008-12-28 22:32:29 -0300902 ep->desc.bEndpointAddress);
903 else
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300904 pipe = usb_sndbulkpipe(stream->dev->udev,
Laurent Pinchartff924202008-12-28 22:32:29 -0300905 ep->desc.bEndpointAddress);
906
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300907 if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
Laurent Pinchartff924202008-12-28 22:32:29 -0300908 size = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300909
910 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart29135872008-07-04 00:35:26 -0300911 urb = usb_alloc_urb(0, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300912 if (urb == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300913 uvc_uninit_video(stream, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300914 return -ENOMEM;
915 }
916
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300917 usb_fill_bulk_urb(urb, stream->dev->udev, pipe,
918 stream->urb_buffer[i], size, uvc_video_complete,
919 stream);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300920 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300921 urb->transfer_dma = stream->urb_dma[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300922
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300923 stream->urb[i] = urb;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300924 }
925
926 return 0;
927}
928
929/*
930 * Initialize isochronous/bulk URBs and allocate transfer buffers.
931 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300932static int uvc_init_video(struct uvc_streaming *stream, gfp_t gfp_flags)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300933{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300934 struct usb_interface *intf = stream->intf;
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -0300935 struct usb_host_endpoint *ep;
936 unsigned int i;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300937 int ret;
938
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300939 stream->last_fid = -1;
940 stream->bulk.header_size = 0;
941 stream->bulk.skip_payload = 0;
942 stream->bulk.payload_size = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300943
944 if (intf->num_altsetting > 1) {
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -0300945 struct usb_host_endpoint *best_ep = NULL;
946 unsigned int best_psize = 3 * 1024;
947 unsigned int bandwidth;
948 unsigned int uninitialized_var(altsetting);
949 int intfnum = stream->intfnum;
950
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300951 /* Isochronous endpoint, select the alternate setting. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300952 bandwidth = stream->ctrl.dwMaxPayloadTransferSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300953
954 if (bandwidth == 0) {
Laurent Pinchart663a4192009-08-06 11:41:17 -0300955 uvc_trace(UVC_TRACE_VIDEO, "Device requested null "
956 "bandwidth, defaulting to lowest.\n");
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300957 bandwidth = 1;
Laurent Pinchart663a4192009-08-06 11:41:17 -0300958 } else {
959 uvc_trace(UVC_TRACE_VIDEO, "Device requested %u "
960 "B/frame bandwidth.\n", bandwidth);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300961 }
962
963 for (i = 0; i < intf->num_altsetting; ++i) {
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -0300964 struct usb_host_interface *alts;
965 unsigned int psize;
966
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300967 alts = &intf->altsetting[i];
968 ep = uvc_find_endpoint(alts,
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300969 stream->header.bEndpointAddress);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300970 if (ep == NULL)
971 continue;
972
973 /* Check if the bandwidth is high enough. */
974 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
975 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -0300976 if (psize >= bandwidth && psize <= best_psize) {
977 altsetting = i;
978 best_psize = psize;
979 best_ep = ep;
980 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300981 }
982
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -0300983 if (best_ep == NULL) {
Laurent Pinchart663a4192009-08-06 11:41:17 -0300984 uvc_trace(UVC_TRACE_VIDEO, "No fast enough alt setting "
985 "for requested bandwidth.\n");
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300986 return -EIO;
Laurent Pinchart663a4192009-08-06 11:41:17 -0300987 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300988
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -0300989 uvc_trace(UVC_TRACE_VIDEO, "Selecting alternate setting %u "
990 "(%u B/frame bandwidth).\n", altsetting, best_psize);
991
992 ret = usb_set_interface(stream->dev->udev, intfnum, altsetting);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300993 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300994 return ret;
995
Laurent Pinchart2c4d9de2009-12-10 21:19:31 -0300996 ret = uvc_init_video_isoc(stream, best_ep, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300997 } else {
998 /* Bulk endpoint, proceed to URB initialization. */
999 ep = uvc_find_endpoint(&intf->altsetting[0],
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001000 stream->header.bEndpointAddress);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001001 if (ep == NULL)
1002 return -EIO;
1003
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001004 ret = uvc_init_video_bulk(stream, ep, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001005 }
1006
1007 if (ret < 0)
1008 return ret;
1009
1010 /* Submit the URBs. */
1011 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001012 ret = usb_submit_urb(stream->urb[i], gfp_flags);
1013 if (ret < 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001014 uvc_printk(KERN_ERR, "Failed to submit URB %u "
1015 "(%d).\n", i, ret);
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001016 uvc_uninit_video(stream, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001017 return ret;
1018 }
1019 }
1020
1021 return 0;
1022}
1023
1024/* --------------------------------------------------------------------------
1025 * Suspend/resume
1026 */
1027
1028/*
1029 * Stop streaming without disabling the video queue.
1030 *
1031 * To let userspace applications resume without trouble, we must not touch the
1032 * video buffers in any way. We mark the device as frozen to make sure the URB
1033 * completion handler won't try to cancel the queue when we kill the URBs.
1034 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001035int uvc_video_suspend(struct uvc_streaming *stream)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001036{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001037 if (!uvc_queue_streaming(&stream->queue))
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001038 return 0;
1039
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001040 stream->frozen = 1;
1041 uvc_uninit_video(stream, 0);
1042 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001043 return 0;
1044}
1045
1046/*
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03001047 * Reconfigure the video interface and restart streaming if it was enabled
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001048 * before suspend.
1049 *
1050 * If an error occurs, disable the video queue. This will wake all pending
1051 * buffers, making sure userspace applications are notified of the problem
1052 * instead of waiting forever.
1053 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001054int uvc_video_resume(struct uvc_streaming *stream)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001055{
1056 int ret;
1057
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001058 stream->frozen = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001059
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001060 ret = uvc_commit_video(stream, &stream->ctrl);
1061 if (ret < 0) {
1062 uvc_queue_enable(&stream->queue, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001063 return ret;
1064 }
1065
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001066 if (!uvc_queue_streaming(&stream->queue))
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001067 return 0;
1068
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001069 ret = uvc_init_video(stream, GFP_NOIO);
1070 if (ret < 0)
1071 uvc_queue_enable(&stream->queue, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001072
1073 return ret;
1074}
1075
1076/* ------------------------------------------------------------------------
1077 * Video device
1078 */
1079
1080/*
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03001081 * Initialize the UVC video device by switching to alternate setting 0 and
1082 * retrieve the default format.
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001083 *
1084 * Some cameras (namely the Fuji Finepix) set the format and frame
1085 * indexes to zero. The UVC standard doesn't clearly make this a spec
1086 * violation, so try to silently fix the values if possible.
1087 *
1088 * This function is called before registering the device with V4L.
1089 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001090int uvc_video_init(struct uvc_streaming *stream)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001091{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001092 struct uvc_streaming_control *probe = &stream->ctrl;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001093 struct uvc_format *format = NULL;
1094 struct uvc_frame *frame = NULL;
1095 unsigned int i;
1096 int ret;
1097
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001098 if (stream->nformats == 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001099 uvc_printk(KERN_INFO, "No supported video formats found.\n");
1100 return -EINVAL;
1101 }
1102
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001103 atomic_set(&stream->active, 0);
1104
1105 /* Initialize the video buffers queue. */
1106 uvc_queue_init(&stream->queue, stream->type);
1107
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001108 /* Alternate setting 0 should be the default, yet the XBox Live Vision
1109 * Cam (and possibly other devices) crash or otherwise misbehave if
1110 * they don't receive a SET_INTERFACE request before any other video
1111 * control request.
1112 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001113 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001114
Laurent Pinchart72362422009-02-14 19:26:56 -03001115 /* Set the streaming probe control with default streaming parameters
1116 * retrieved from the device. Webcams that don't suport GET_DEF
1117 * requests on the probe control will just keep their current streaming
1118 * parameters.
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001119 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001120 if (uvc_get_video_ctrl(stream, probe, 1, UVC_GET_DEF) == 0)
1121 uvc_set_video_ctrl(stream, probe, 1);
Laurent Pinchart72362422009-02-14 19:26:56 -03001122
1123 /* Initialize the streaming parameters with the probe control current
1124 * value. This makes sure SET_CUR requests on the streaming commit
1125 * control will always use values retrieved from a successful GET_CUR
1126 * request on the probe control, as required by the UVC specification.
1127 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001128 ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
Laurent Pinchartb482d922009-06-26 11:39:42 -03001129 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001130 return ret;
1131
1132 /* Check if the default format descriptor exists. Use the first
1133 * available format otherwise.
1134 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001135 for (i = stream->nformats; i > 0; --i) {
1136 format = &stream->format[i-1];
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001137 if (format->index == probe->bFormatIndex)
1138 break;
1139 }
1140
1141 if (format->nframes == 0) {
1142 uvc_printk(KERN_INFO, "No frame descriptor found for the "
1143 "default format.\n");
1144 return -EINVAL;
1145 }
1146
1147 /* Zero bFrameIndex might be correct. Stream-based formats (including
1148 * MPEG-2 TS and DV) do not support frames but have a dummy frame
1149 * descriptor with bFrameIndex set to zero. If the default frame
Laurent Pinchart078f8942009-05-06 12:30:30 -03001150 * descriptor is not found, use the first available frame.
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001151 */
1152 for (i = format->nframes; i > 0; --i) {
1153 frame = &format->frame[i-1];
1154 if (frame->bFrameIndex == probe->bFrameIndex)
1155 break;
1156 }
1157
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001158 probe->bFormatIndex = format->index;
1159 probe->bFrameIndex = frame->bFrameIndex;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001160
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001161 stream->cur_format = format;
1162 stream->cur_frame = frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001163
1164 /* Select the video decoding function */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001165 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1166 if (stream->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
1167 stream->decode = uvc_video_decode_isight;
1168 else if (stream->intf->num_altsetting > 1)
1169 stream->decode = uvc_video_decode_isoc;
Laurent Pinchartff924202008-12-28 22:32:29 -03001170 else
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001171 stream->decode = uvc_video_decode_bulk;
Laurent Pinchartff924202008-12-28 22:32:29 -03001172 } else {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001173 if (stream->intf->num_altsetting == 1)
1174 stream->decode = uvc_video_encode_bulk;
Laurent Pinchartff924202008-12-28 22:32:29 -03001175 else {
1176 uvc_printk(KERN_INFO, "Isochronous endpoints are not "
1177 "supported for video output devices.\n");
1178 return -EINVAL;
1179 }
1180 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001181
1182 return 0;
1183}
1184
1185/*
1186 * Enable or disable the video stream.
1187 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001188int uvc_video_enable(struct uvc_streaming *stream, int enable)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001189{
1190 int ret;
1191
1192 if (!enable) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001193 uvc_uninit_video(stream, 1);
1194 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1195 uvc_queue_enable(&stream->queue, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001196 return 0;
1197 }
1198
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001199 if ((stream->cur_format->flags & UVC_FMT_FLAG_COMPRESSED) ||
Laurent Pinchart0fbd8ee2008-12-06 16:25:14 -03001200 uvc_no_drop_param)
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001201 stream->queue.flags &= ~UVC_QUEUE_DROP_INCOMPLETE;
Laurent Pinchartd63beb92008-09-15 22:24:29 -03001202 else
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001203 stream->queue.flags |= UVC_QUEUE_DROP_INCOMPLETE;
Laurent Pinchartd63beb92008-09-15 22:24:29 -03001204
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001205 ret = uvc_queue_enable(&stream->queue, 1);
1206 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001207 return ret;
1208
Laurent Pinchart23867b22008-11-12 11:46:43 -03001209 /* Commit the streaming parameters. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001210 ret = uvc_commit_video(stream, &stream->ctrl);
1211 if (ret < 0)
Laurent Pinchart23867b22008-11-12 11:46:43 -03001212 return ret;
1213
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001214 return uvc_init_video(stream, GFP_KERNEL);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001215}
Hans Verkuilf87086e2008-07-18 00:50:58 -03001216