blob: 9bc4705be78d8ebb893a9dd24d2e6215c344507f [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 Pinchartc0efd232008-06-30 15:04:50 -030064static void uvc_fixup_buffer_size(struct uvc_video_device *video,
65 struct uvc_streaming_control *ctrl)
66{
67 struct uvc_format *format;
68 struct uvc_frame *frame;
69
70 if (ctrl->bFormatIndex <= 0 ||
71 ctrl->bFormatIndex > video->streaming->nformats)
72 return;
73
74 format = &video->streaming->format[ctrl->bFormatIndex - 1];
75
76 if (ctrl->bFrameIndex <= 0 ||
77 ctrl->bFrameIndex > format->nframes)
78 return;
79
80 frame = &format->frame[ctrl->bFrameIndex - 1];
81
82 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) ||
83 (ctrl->dwMaxVideoFrameSize == 0 &&
84 video->dev->uvc_version < 0x0110))
85 ctrl->dwMaxVideoFrameSize =
86 frame->dwMaxVideoFrameBufferSize;
87}
88
89static int uvc_get_video_ctrl(struct uvc_video_device *video,
90 struct uvc_streaming_control *ctrl, int probe, __u8 query)
91{
Laurent Pinchart04793dd2008-07-31 17:11:12 -030092 __u8 *data;
93 __u16 size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -030094 int ret;
95
96 size = video->dev->uvc_version >= 0x0110 ? 34 : 26;
Laurent Pinchart04793dd2008-07-31 17:11:12 -030097 data = kmalloc(size, GFP_KERNEL);
98 if (data == NULL)
99 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300100
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300101 ret = __uvc_query_ctrl(video->dev, query, 0, video->streaming->intfnum,
102 probe ? VS_PROBE_CONTROL : VS_COMMIT_CONTROL, data, size,
103 UVC_CTRL_STREAMING_TIMEOUT);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300104
105 if ((query == GET_MIN || query == GET_MAX) && ret == 2) {
106 /* Some cameras, mostly based on Bison Electronics chipsets,
107 * answer a GET_MIN or GET_MAX request with the wCompQuality
108 * field only.
109 */
110 uvc_warn_once(video->dev, UVC_WARN_MINMAX, "UVC non "
111 "compliance - GET_MIN/MAX(PROBE) incorrectly "
112 "supported. Enabling workaround.\n");
113 memset(ctrl, 0, sizeof ctrl);
114 ctrl->wCompQuality = le16_to_cpup((__le16 *)data);
115 ret = 0;
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300116 goto out;
Laurent Pinchart51cac8a2009-01-03 20:08:26 -0300117 } else if (query == GET_DEF && probe == 1 && ret != size) {
Laurent Pinchart44f00792008-11-08 19:14:50 -0300118 /* Many cameras don't support the GET_DEF request on their
119 * video probe control. Warn once and return, the caller will
120 * fall back to GET_CUR.
121 */
122 uvc_warn_once(video->dev, UVC_WARN_PROBE_DEF, "UVC non "
123 "compliance - GET_DEF(PROBE) not supported. "
124 "Enabling workaround.\n");
125 ret = -EIO;
126 goto out;
127 } else if (ret != size) {
128 uvc_printk(KERN_ERR, "Failed to query (%u) UVC %s control : "
129 "%d (exp. %u).\n", query, probe ? "probe" : "commit",
130 ret, size);
131 ret = -EIO;
132 goto out;
133 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300134
135 ctrl->bmHint = le16_to_cpup((__le16 *)&data[0]);
136 ctrl->bFormatIndex = data[2];
137 ctrl->bFrameIndex = data[3];
138 ctrl->dwFrameInterval = le32_to_cpup((__le32 *)&data[4]);
139 ctrl->wKeyFrameRate = le16_to_cpup((__le16 *)&data[8]);
140 ctrl->wPFrameRate = le16_to_cpup((__le16 *)&data[10]);
141 ctrl->wCompQuality = le16_to_cpup((__le16 *)&data[12]);
142 ctrl->wCompWindowSize = le16_to_cpup((__le16 *)&data[14]);
143 ctrl->wDelay = le16_to_cpup((__le16 *)&data[16]);
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300144 ctrl->dwMaxVideoFrameSize = get_unaligned_le32(&data[18]);
145 ctrl->dwMaxPayloadTransferSize = get_unaligned_le32(&data[22]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300146
147 if (size == 34) {
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300148 ctrl->dwClockFrequency = get_unaligned_le32(&data[26]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300149 ctrl->bmFramingInfo = data[30];
150 ctrl->bPreferedVersion = data[31];
151 ctrl->bMinVersion = data[32];
152 ctrl->bMaxVersion = data[33];
153 } else {
154 ctrl->dwClockFrequency = video->dev->clock_frequency;
155 ctrl->bmFramingInfo = 0;
156 ctrl->bPreferedVersion = 0;
157 ctrl->bMinVersion = 0;
158 ctrl->bMaxVersion = 0;
159 }
160
161 /* Some broken devices return a null or wrong dwMaxVideoFrameSize.
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300162 * Try to get the value from the format and frame descriptors.
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300163 */
164 uvc_fixup_buffer_size(video, ctrl);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300165 ret = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300166
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300167out:
168 kfree(data);
169 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300170}
171
Laurent Pinchart44f00792008-11-08 19:14:50 -0300172static int uvc_set_video_ctrl(struct uvc_video_device *video,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300173 struct uvc_streaming_control *ctrl, int probe)
174{
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300175 __u8 *data;
176 __u16 size;
177 int ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300178
179 size = video->dev->uvc_version >= 0x0110 ? 34 : 26;
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300180 data = kzalloc(size, GFP_KERNEL);
181 if (data == NULL)
182 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300183
184 *(__le16 *)&data[0] = cpu_to_le16(ctrl->bmHint);
185 data[2] = ctrl->bFormatIndex;
186 data[3] = ctrl->bFrameIndex;
187 *(__le32 *)&data[4] = cpu_to_le32(ctrl->dwFrameInterval);
188 *(__le16 *)&data[8] = cpu_to_le16(ctrl->wKeyFrameRate);
189 *(__le16 *)&data[10] = cpu_to_le16(ctrl->wPFrameRate);
190 *(__le16 *)&data[12] = cpu_to_le16(ctrl->wCompQuality);
191 *(__le16 *)&data[14] = cpu_to_le16(ctrl->wCompWindowSize);
192 *(__le16 *)&data[16] = cpu_to_le16(ctrl->wDelay);
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300193 put_unaligned_le32(ctrl->dwMaxVideoFrameSize, &data[18]);
194 put_unaligned_le32(ctrl->dwMaxPayloadTransferSize, &data[22]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300195
196 if (size == 34) {
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300197 put_unaligned_le32(ctrl->dwClockFrequency, &data[26]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300198 data[30] = ctrl->bmFramingInfo;
199 data[31] = ctrl->bPreferedVersion;
200 data[32] = ctrl->bMinVersion;
201 data[33] = ctrl->bMaxVersion;
202 }
203
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300204 ret = __uvc_query_ctrl(video->dev, SET_CUR, 0,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300205 video->streaming->intfnum,
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300206 probe ? VS_PROBE_CONTROL : VS_COMMIT_CONTROL, data, size,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300207 UVC_CTRL_STREAMING_TIMEOUT);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300208 if (ret != size) {
209 uvc_printk(KERN_ERR, "Failed to set UVC %s control : "
210 "%d (exp. %u).\n", probe ? "probe" : "commit",
211 ret, size);
212 ret = -EIO;
213 }
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300214
215 kfree(data);
216 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300217}
218
219int uvc_probe_video(struct uvc_video_device *video,
220 struct uvc_streaming_control *probe)
221{
222 struct uvc_streaming_control probe_min, probe_max;
223 __u16 bandwidth;
224 unsigned int i;
225 int ret;
226
227 mutex_lock(&video->streaming->mutex);
228
229 /* Perform probing. The device should adjust the requested values
230 * according to its capabilities. However, some devices, namely the
231 * first generation UVC Logitech webcams, don't implement the Video
232 * Probe control properly, and just return the needed bandwidth. For
233 * that reason, if the needed bandwidth exceeds the maximum available
234 * bandwidth, try to lower the quality.
235 */
236 if ((ret = uvc_set_video_ctrl(video, probe, 1)) < 0)
237 goto done;
238
239 /* Get the minimum and maximum values for compression settings. */
240 if (!(video->dev->quirks & UVC_QUIRK_PROBE_MINMAX)) {
241 ret = uvc_get_video_ctrl(video, &probe_min, 1, GET_MIN);
242 if (ret < 0)
243 goto done;
244 ret = uvc_get_video_ctrl(video, &probe_max, 1, GET_MAX);
245 if (ret < 0)
246 goto done;
247
248 probe->wCompQuality = probe_max.wCompQuality;
249 }
250
251 for (i = 0; i < 2; ++i) {
252 if ((ret = uvc_set_video_ctrl(video, probe, 1)) < 0 ||
253 (ret = uvc_get_video_ctrl(video, probe, 1, GET_CUR)) < 0)
254 goto done;
255
256 if (video->streaming->intf->num_altsetting == 1)
257 break;
258
259 bandwidth = probe->dwMaxPayloadTransferSize;
260 if (bandwidth <= video->streaming->maxpsize)
261 break;
262
263 if (video->dev->quirks & UVC_QUIRK_PROBE_MINMAX) {
264 ret = -ENOSPC;
265 goto done;
266 }
267
268 /* TODO: negotiate compression parameters */
269 probe->wKeyFrameRate = probe_min.wKeyFrameRate;
270 probe->wPFrameRate = probe_min.wPFrameRate;
271 probe->wCompQuality = probe_max.wCompQuality;
272 probe->wCompWindowSize = probe_min.wCompWindowSize;
273 }
274
275done:
276 mutex_unlock(&video->streaming->mutex);
277 return ret;
278}
279
Laurent Pinchart44f00792008-11-08 19:14:50 -0300280int uvc_commit_video(struct uvc_video_device *video,
281 struct uvc_streaming_control *probe)
282{
283 return uvc_set_video_ctrl(video, probe, 0);
284}
285
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300286/* ------------------------------------------------------------------------
287 * Video codecs
288 */
289
290/* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
291#define UVC_STREAM_EOH (1 << 7)
292#define UVC_STREAM_ERR (1 << 6)
293#define UVC_STREAM_STI (1 << 5)
294#define UVC_STREAM_RES (1 << 4)
295#define UVC_STREAM_SCR (1 << 3)
296#define UVC_STREAM_PTS (1 << 2)
297#define UVC_STREAM_EOF (1 << 1)
298#define UVC_STREAM_FID (1 << 0)
299
300/* Video payload decoding is handled by uvc_video_decode_start(),
301 * uvc_video_decode_data() and uvc_video_decode_end().
302 *
303 * uvc_video_decode_start is called with URB data at the start of a bulk or
304 * isochronous payload. It processes header data and returns the header size
305 * in bytes if successful. If an error occurs, it returns a negative error
306 * code. The following error codes have special meanings.
307 *
308 * - EAGAIN informs the caller that the current video buffer should be marked
309 * as done, and that the function should be called again with the same data
310 * and a new video buffer. This is used when end of frame conditions can be
311 * reliably detected at the beginning of the next frame only.
312 *
313 * If an error other than -EAGAIN is returned, the caller will drop the current
314 * payload. No call to uvc_video_decode_data and uvc_video_decode_end will be
315 * made until the next payload. -ENODATA can be used to drop the current
316 * payload if no other error code is appropriate.
317 *
318 * uvc_video_decode_data is called for every URB with URB data. It copies the
319 * data to the video buffer.
320 *
321 * uvc_video_decode_end is called with header data at the end of a bulk or
322 * isochronous payload. It performs any additional header data processing and
323 * returns 0 or a negative error code if an error occured. As header data have
324 * already been processed by uvc_video_decode_start, this functions isn't
325 * required to perform sanity checks a second time.
326 *
327 * For isochronous transfers where a payload is always transfered in a single
328 * URB, the three functions will be called in a row.
329 *
330 * To let the decoder process header data and update its internal state even
331 * when no video buffer is available, uvc_video_decode_start must be prepared
332 * to be called with a NULL buf parameter. uvc_video_decode_data and
333 * uvc_video_decode_end will never be called with a NULL buffer.
334 */
335static int uvc_video_decode_start(struct uvc_video_device *video,
336 struct uvc_buffer *buf, const __u8 *data, int len)
337{
338 __u8 fid;
339
340 /* Sanity checks:
341 * - packet must be at least 2 bytes long
342 * - bHeaderLength value must be at least 2 bytes (see above)
343 * - bHeaderLength value can't be larger than the packet size.
344 */
345 if (len < 2 || data[0] < 2 || data[0] > len)
346 return -EINVAL;
347
348 /* Skip payloads marked with the error bit ("error frames"). */
349 if (data[1] & UVC_STREAM_ERR) {
350 uvc_trace(UVC_TRACE_FRAME, "Dropping payload (error bit "
351 "set).\n");
352 return -ENODATA;
353 }
354
355 fid = data[1] & UVC_STREAM_FID;
356
357 /* Store the payload FID bit and return immediately when the buffer is
358 * NULL.
359 */
360 if (buf == NULL) {
361 video->last_fid = fid;
362 return -ENODATA;
363 }
364
365 /* Synchronize to the input stream by waiting for the FID bit to be
366 * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE.
Laurent Pinchartf8dd4af2008-12-16 10:41:57 -0300367 * video->last_fid is initialized to -1, so the first isochronous
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300368 * frame will always be in sync.
369 *
370 * If the device doesn't toggle the FID bit, invert video->last_fid
371 * when the EOF bit is set to force synchronisation on the next packet.
372 */
373 if (buf->state != UVC_BUF_STATE_ACTIVE) {
374 if (fid == video->last_fid) {
375 uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of "
376 "sync).\n");
377 if ((video->dev->quirks & UVC_QUIRK_STREAM_NO_FID) &&
378 (data[1] & UVC_STREAM_EOF))
379 video->last_fid ^= UVC_STREAM_FID;
380 return -ENODATA;
381 }
382
383 /* TODO: Handle PTS and SCR. */
384 buf->state = UVC_BUF_STATE_ACTIVE;
385 }
386
387 /* Mark the buffer as done if we're at the beginning of a new frame.
388 * End of frame detection is better implemented by checking the EOF
389 * bit (FID bit toggling is delayed by one frame compared to the EOF
390 * bit), but some devices don't set the bit at end of frame (and the
391 * last payload can be lost anyway). We thus must check if the FID has
392 * been toggled.
393 *
Laurent Pinchartf8dd4af2008-12-16 10:41:57 -0300394 * video->last_fid is initialized to -1, so the first isochronous
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300395 * frame will never trigger an end of frame detection.
396 *
397 * Empty buffers (bytesused == 0) don't trigger end of frame detection
398 * as it doesn't make sense to return an empty buffer. This also
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300399 * avoids detecting end of frame conditions at FID toggling if the
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300400 * previous payload had the EOF bit set.
401 */
402 if (fid != video->last_fid && buf->buf.bytesused != 0) {
403 uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit "
404 "toggled).\n");
405 buf->state = UVC_BUF_STATE_DONE;
406 return -EAGAIN;
407 }
408
409 video->last_fid = fid;
410
411 return data[0];
412}
413
414static void uvc_video_decode_data(struct uvc_video_device *video,
415 struct uvc_buffer *buf, const __u8 *data, int len)
416{
417 struct uvc_video_queue *queue = &video->queue;
418 unsigned int maxlen, nbytes;
419 void *mem;
420
421 if (len <= 0)
422 return;
423
424 /* Copy the video data to the buffer. */
425 maxlen = buf->buf.length - buf->buf.bytesused;
426 mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused;
427 nbytes = min((unsigned int)len, maxlen);
428 memcpy(mem, data, nbytes);
429 buf->buf.bytesused += nbytes;
430
431 /* Complete the current frame if the buffer size was exceeded. */
432 if (len > maxlen) {
433 uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n");
434 buf->state = UVC_BUF_STATE_DONE;
435 }
436}
437
438static void uvc_video_decode_end(struct uvc_video_device *video,
439 struct uvc_buffer *buf, const __u8 *data, int len)
440{
441 /* Mark the buffer as done if the EOF marker is set. */
442 if (data[1] & UVC_STREAM_EOF && buf->buf.bytesused != 0) {
443 uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n");
444 if (data[0] == len)
445 uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n");
446 buf->state = UVC_BUF_STATE_DONE;
447 if (video->dev->quirks & UVC_QUIRK_STREAM_NO_FID)
448 video->last_fid ^= UVC_STREAM_FID;
449 }
450}
451
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300452/* Video payload encoding is handled by uvc_video_encode_header() and
453 * uvc_video_encode_data(). Only bulk transfers are currently supported.
454 *
455 * uvc_video_encode_header is called at the start of a payload. It adds header
456 * data to the transfer buffer and returns the header size. As the only known
457 * UVC output device transfers a whole frame in a single payload, the EOF bit
458 * is always set in the header.
459 *
460 * uvc_video_encode_data is called for every URB and copies the data from the
461 * video buffer to the transfer buffer.
462 */
Laurent Pinchartff924202008-12-28 22:32:29 -0300463static int uvc_video_encode_header(struct uvc_video_device *video,
464 struct uvc_buffer *buf, __u8 *data, int len)
465{
466 data[0] = 2; /* Header length */
467 data[1] = UVC_STREAM_EOH | UVC_STREAM_EOF
468 | (video->last_fid & UVC_STREAM_FID);
469 return 2;
470}
471
472static int uvc_video_encode_data(struct uvc_video_device *video,
473 struct uvc_buffer *buf, __u8 *data, int len)
474{
475 struct uvc_video_queue *queue = &video->queue;
476 unsigned int nbytes;
477 void *mem;
478
479 /* Copy video data to the URB buffer. */
480 mem = queue->mem + buf->buf.m.offset + queue->buf_used;
481 nbytes = min((unsigned int)len, buf->buf.bytesused - queue->buf_used);
482 nbytes = min(video->bulk.max_payload_size - video->bulk.payload_size,
483 nbytes);
484 memcpy(data, mem, nbytes);
485
486 queue->buf_used += nbytes;
487
488 return nbytes;
489}
490
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300491/* ------------------------------------------------------------------------
492 * URB handling
493 */
494
495/*
496 * Completion handler for video URBs.
497 */
498static void uvc_video_decode_isoc(struct urb *urb,
499 struct uvc_video_device *video, struct uvc_buffer *buf)
500{
501 u8 *mem;
502 int ret, i;
503
504 for (i = 0; i < urb->number_of_packets; ++i) {
505 if (urb->iso_frame_desc[i].status < 0) {
506 uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
507 "lost (%d).\n", urb->iso_frame_desc[i].status);
508 continue;
509 }
510
511 /* Decode the payload header. */
512 mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
513 do {
514 ret = uvc_video_decode_start(video, buf, mem,
515 urb->iso_frame_desc[i].actual_length);
516 if (ret == -EAGAIN)
517 buf = uvc_queue_next_buffer(&video->queue, buf);
518 } while (ret == -EAGAIN);
519
520 if (ret < 0)
521 continue;
522
523 /* Decode the payload data. */
524 uvc_video_decode_data(video, buf, mem + ret,
525 urb->iso_frame_desc[i].actual_length - ret);
526
527 /* Process the header again. */
Laurent Pinchart08ebd002008-08-29 17:37:44 -0300528 uvc_video_decode_end(video, buf, mem,
529 urb->iso_frame_desc[i].actual_length);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300530
531 if (buf->state == UVC_BUF_STATE_DONE ||
532 buf->state == UVC_BUF_STATE_ERROR)
533 buf = uvc_queue_next_buffer(&video->queue, buf);
534 }
535}
536
537static void uvc_video_decode_bulk(struct urb *urb,
538 struct uvc_video_device *video, struct uvc_buffer *buf)
539{
540 u8 *mem;
541 int len, ret;
542
543 mem = urb->transfer_buffer;
544 len = urb->actual_length;
545 video->bulk.payload_size += len;
546
547 /* If the URB is the first of its payload, decode and save the
548 * header.
549 */
Laurent Pinchartf8dd4af2008-12-16 10:41:57 -0300550 if (video->bulk.header_size == 0 && !video->bulk.skip_payload) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300551 do {
552 ret = uvc_video_decode_start(video, buf, mem, len);
553 if (ret == -EAGAIN)
554 buf = uvc_queue_next_buffer(&video->queue, buf);
555 } while (ret == -EAGAIN);
556
557 /* If an error occured skip the rest of the payload. */
558 if (ret < 0 || buf == NULL) {
559 video->bulk.skip_payload = 1;
Laurent Pinchartf8dd4af2008-12-16 10:41:57 -0300560 } else {
561 memcpy(video->bulk.header, mem, ret);
562 video->bulk.header_size = ret;
563
564 mem += ret;
565 len -= ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300566 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300567 }
568
569 /* The buffer queue might have been cancelled while a bulk transfer
570 * was in progress, so we can reach here with buf equal to NULL. Make
571 * sure buf is never dereferenced if NULL.
572 */
573
574 /* Process video data. */
575 if (!video->bulk.skip_payload && buf != NULL)
576 uvc_video_decode_data(video, buf, mem, len);
577
578 /* Detect the payload end by a URB smaller than the maximum size (or
579 * a payload size equal to the maximum) and process the header again.
580 */
581 if (urb->actual_length < urb->transfer_buffer_length ||
582 video->bulk.payload_size >= video->bulk.max_payload_size) {
583 if (!video->bulk.skip_payload && buf != NULL) {
584 uvc_video_decode_end(video, buf, video->bulk.header,
Laurent Pinchart08ebd002008-08-29 17:37:44 -0300585 video->bulk.payload_size);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300586 if (buf->state == UVC_BUF_STATE_DONE ||
587 buf->state == UVC_BUF_STATE_ERROR)
588 buf = uvc_queue_next_buffer(&video->queue, buf);
589 }
590
591 video->bulk.header_size = 0;
592 video->bulk.skip_payload = 0;
593 video->bulk.payload_size = 0;
594 }
595}
596
Laurent Pinchartff924202008-12-28 22:32:29 -0300597static void uvc_video_encode_bulk(struct urb *urb,
598 struct uvc_video_device *video, struct uvc_buffer *buf)
599{
600 u8 *mem = urb->transfer_buffer;
601 int len = video->urb_size, ret;
602
603 if (buf == NULL) {
604 urb->transfer_buffer_length = 0;
605 return;
606 }
607
608 /* If the URB is the first of its payload, add the header. */
609 if (video->bulk.header_size == 0) {
610 ret = uvc_video_encode_header(video, buf, mem, len);
611 video->bulk.header_size = ret;
612 video->bulk.payload_size += ret;
613 mem += ret;
614 len -= ret;
615 }
616
617 /* Process video data. */
618 ret = uvc_video_encode_data(video, buf, mem, len);
619
620 video->bulk.payload_size += ret;
621 len -= ret;
622
623 if (buf->buf.bytesused == video->queue.buf_used ||
624 video->bulk.payload_size == video->bulk.max_payload_size) {
625 if (buf->buf.bytesused == video->queue.buf_used) {
626 video->queue.buf_used = 0;
627 buf->state = UVC_BUF_STATE_DONE;
628 uvc_queue_next_buffer(&video->queue, buf);
629 video->last_fid ^= UVC_STREAM_FID;
630 }
631
632 video->bulk.header_size = 0;
633 video->bulk.payload_size = 0;
634 }
635
636 urb->transfer_buffer_length = video->urb_size - len;
637}
638
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300639static void uvc_video_complete(struct urb *urb)
640{
641 struct uvc_video_device *video = urb->context;
642 struct uvc_video_queue *queue = &video->queue;
643 struct uvc_buffer *buf = NULL;
644 unsigned long flags;
645 int ret;
646
647 switch (urb->status) {
648 case 0:
649 break;
650
651 default:
652 uvc_printk(KERN_WARNING, "Non-zero status (%d) in video "
653 "completion handler.\n", urb->status);
654
655 case -ENOENT: /* usb_kill_urb() called. */
656 if (video->frozen)
657 return;
658
659 case -ECONNRESET: /* usb_unlink_urb() called. */
660 case -ESHUTDOWN: /* The endpoint is being disabled. */
661 uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
662 return;
663 }
664
665 spin_lock_irqsave(&queue->irqlock, flags);
666 if (!list_empty(&queue->irqqueue))
667 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
668 queue);
669 spin_unlock_irqrestore(&queue->irqlock, flags);
670
671 video->decode(urb, video, buf);
672
673 if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
674 uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n",
675 ret);
676 }
677}
678
679/*
Laurent Pincharte01117c2008-07-04 00:36:21 -0300680 * Free transfer buffers.
681 */
682static void uvc_free_urb_buffers(struct uvc_video_device *video)
683{
684 unsigned int i;
685
686 for (i = 0; i < UVC_URBS; ++i) {
687 if (video->urb_buffer[i]) {
688 usb_buffer_free(video->dev->udev, video->urb_size,
689 video->urb_buffer[i], video->urb_dma[i]);
690 video->urb_buffer[i] = NULL;
691 }
692 }
693
694 video->urb_size = 0;
695}
696
697/*
698 * Allocate transfer buffers. This function can be called with buffers
699 * already allocated when resuming from suspend, in which case it will
700 * return without touching the buffers.
701 *
702 * Return 0 on success or -ENOMEM when out of memory.
703 */
704static int uvc_alloc_urb_buffers(struct uvc_video_device *video,
705 unsigned int size)
706{
707 unsigned int i;
708
709 /* Buffers are already allocated, bail out. */
710 if (video->urb_size)
711 return 0;
712
713 for (i = 0; i < UVC_URBS; ++i) {
714 video->urb_buffer[i] = usb_buffer_alloc(video->dev->udev,
715 size, GFP_KERNEL, &video->urb_dma[i]);
716 if (video->urb_buffer[i] == NULL) {
717 uvc_free_urb_buffers(video);
718 return -ENOMEM;
719 }
720 }
721
722 video->urb_size = size;
723 return 0;
724}
725
726/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300727 * Uninitialize isochronous/bulk URBs and free transfer buffers.
728 */
Laurent Pincharte01117c2008-07-04 00:36:21 -0300729static void uvc_uninit_video(struct uvc_video_device *video, int free_buffers)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300730{
731 struct urb *urb;
732 unsigned int i;
733
734 for (i = 0; i < UVC_URBS; ++i) {
735 if ((urb = video->urb[i]) == NULL)
736 continue;
737
738 usb_kill_urb(urb);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300739 usb_free_urb(urb);
740 video->urb[i] = NULL;
741 }
Laurent Pincharte01117c2008-07-04 00:36:21 -0300742
743 if (free_buffers)
744 uvc_free_urb_buffers(video);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300745}
746
747/*
748 * Initialize isochronous URBs and allocate transfer buffers. The packet size
749 * is given by the endpoint.
750 */
751static int uvc_init_video_isoc(struct uvc_video_device *video,
Laurent Pinchart29135872008-07-04 00:35:26 -0300752 struct usb_host_endpoint *ep, gfp_t gfp_flags)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300753{
754 struct urb *urb;
755 unsigned int npackets, i, j;
756 __u16 psize;
757 __u32 size;
758
759 /* Compute the number of isochronous packets to allocate by dividing
760 * the maximum video frame size by the packet size. Limit the result
761 * to UVC_MAX_ISO_PACKETS.
762 */
763 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
764 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
765
766 size = video->streaming->ctrl.dwMaxVideoFrameSize;
767 if (size > UVC_MAX_FRAME_SIZE)
768 return -EINVAL;
769
Julia Lawalle9e24ce2008-08-20 20:44:53 -0300770 npackets = DIV_ROUND_UP(size, psize);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300771 if (npackets > UVC_MAX_ISO_PACKETS)
772 npackets = UVC_MAX_ISO_PACKETS;
773
774 size = npackets * psize;
775
Laurent Pincharte01117c2008-07-04 00:36:21 -0300776 if (uvc_alloc_urb_buffers(video, size) < 0)
777 return -ENOMEM;
778
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300779 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart29135872008-07-04 00:35:26 -0300780 urb = usb_alloc_urb(npackets, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300781 if (urb == NULL) {
Laurent Pincharte01117c2008-07-04 00:36:21 -0300782 uvc_uninit_video(video, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300783 return -ENOMEM;
784 }
785
786 urb->dev = video->dev->udev;
787 urb->context = video;
788 urb->pipe = usb_rcvisocpipe(video->dev->udev,
789 ep->desc.bEndpointAddress);
790 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
791 urb->interval = ep->desc.bInterval;
792 urb->transfer_buffer = video->urb_buffer[i];
Laurent Pincharte01117c2008-07-04 00:36:21 -0300793 urb->transfer_dma = video->urb_dma[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300794 urb->complete = uvc_video_complete;
795 urb->number_of_packets = npackets;
796 urb->transfer_buffer_length = size;
797
798 for (j = 0; j < npackets; ++j) {
799 urb->iso_frame_desc[j].offset = j * psize;
800 urb->iso_frame_desc[j].length = psize;
801 }
802
803 video->urb[i] = urb;
804 }
805
806 return 0;
807}
808
809/*
810 * Initialize bulk URBs and allocate transfer buffers. The packet size is
811 * given by the endpoint.
812 */
813static int uvc_init_video_bulk(struct uvc_video_device *video,
Laurent Pinchart29135872008-07-04 00:35:26 -0300814 struct usb_host_endpoint *ep, gfp_t gfp_flags)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300815{
816 struct urb *urb;
817 unsigned int pipe, i;
818 __u16 psize;
819 __u32 size;
820
821 /* Compute the bulk URB size. Some devices set the maximum payload
822 * size to a value too high for memory-constrained devices. We must
823 * then transfer the payload accross multiple URBs. To be consistant
824 * with isochronous mode, allocate maximum UVC_MAX_ISO_PACKETS per bulk
825 * URB.
826 */
827 psize = le16_to_cpu(ep->desc.wMaxPacketSize) & 0x07ff;
828 size = video->streaming->ctrl.dwMaxPayloadTransferSize;
829 video->bulk.max_payload_size = size;
830 if (size > psize * UVC_MAX_ISO_PACKETS)
831 size = psize * UVC_MAX_ISO_PACKETS;
832
Laurent Pincharte01117c2008-07-04 00:36:21 -0300833 if (uvc_alloc_urb_buffers(video, size) < 0)
834 return -ENOMEM;
835
Laurent Pinchartff924202008-12-28 22:32:29 -0300836 if (usb_endpoint_dir_in(&ep->desc))
837 pipe = usb_rcvbulkpipe(video->dev->udev,
838 ep->desc.bEndpointAddress);
839 else
840 pipe = usb_sndbulkpipe(video->dev->udev,
841 ep->desc.bEndpointAddress);
842
843 if (video->streaming->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
844 size = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300845
846 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart29135872008-07-04 00:35:26 -0300847 urb = usb_alloc_urb(0, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300848 if (urb == NULL) {
Laurent Pincharte01117c2008-07-04 00:36:21 -0300849 uvc_uninit_video(video, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300850 return -ENOMEM;
851 }
852
853 usb_fill_bulk_urb(urb, video->dev->udev, pipe,
854 video->urb_buffer[i], size, uvc_video_complete,
855 video);
856 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Laurent Pincharte01117c2008-07-04 00:36:21 -0300857 urb->transfer_dma = video->urb_dma[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300858
859 video->urb[i] = urb;
860 }
861
862 return 0;
863}
864
865/*
866 * Initialize isochronous/bulk URBs and allocate transfer buffers.
867 */
Laurent Pinchart29135872008-07-04 00:35:26 -0300868static int uvc_init_video(struct uvc_video_device *video, gfp_t gfp_flags)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300869{
870 struct usb_interface *intf = video->streaming->intf;
871 struct usb_host_interface *alts;
872 struct usb_host_endpoint *ep = NULL;
873 int intfnum = video->streaming->intfnum;
874 unsigned int bandwidth, psize, i;
875 int ret;
876
877 video->last_fid = -1;
878 video->bulk.header_size = 0;
879 video->bulk.skip_payload = 0;
880 video->bulk.payload_size = 0;
881
882 if (intf->num_altsetting > 1) {
883 /* Isochronous endpoint, select the alternate setting. */
884 bandwidth = video->streaming->ctrl.dwMaxPayloadTransferSize;
885
886 if (bandwidth == 0) {
887 uvc_printk(KERN_WARNING, "device %s requested null "
888 "bandwidth, defaulting to lowest.\n",
889 video->vdev->name);
890 bandwidth = 1;
891 }
892
893 for (i = 0; i < intf->num_altsetting; ++i) {
894 alts = &intf->altsetting[i];
895 ep = uvc_find_endpoint(alts,
896 video->streaming->header.bEndpointAddress);
897 if (ep == NULL)
898 continue;
899
900 /* Check if the bandwidth is high enough. */
901 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
902 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
903 if (psize >= bandwidth)
904 break;
905 }
906
907 if (i >= intf->num_altsetting)
908 return -EIO;
909
910 if ((ret = usb_set_interface(video->dev->udev, intfnum, i)) < 0)
911 return ret;
912
Laurent Pinchart29135872008-07-04 00:35:26 -0300913 ret = uvc_init_video_isoc(video, ep, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300914 } else {
915 /* Bulk endpoint, proceed to URB initialization. */
916 ep = uvc_find_endpoint(&intf->altsetting[0],
917 video->streaming->header.bEndpointAddress);
918 if (ep == NULL)
919 return -EIO;
920
Laurent Pinchart29135872008-07-04 00:35:26 -0300921 ret = uvc_init_video_bulk(video, ep, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300922 }
923
924 if (ret < 0)
925 return ret;
926
927 /* Submit the URBs. */
928 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart29135872008-07-04 00:35:26 -0300929 if ((ret = usb_submit_urb(video->urb[i], gfp_flags)) < 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300930 uvc_printk(KERN_ERR, "Failed to submit URB %u "
931 "(%d).\n", i, ret);
Laurent Pincharte01117c2008-07-04 00:36:21 -0300932 uvc_uninit_video(video, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300933 return ret;
934 }
935 }
936
937 return 0;
938}
939
940/* --------------------------------------------------------------------------
941 * Suspend/resume
942 */
943
944/*
945 * Stop streaming without disabling the video queue.
946 *
947 * To let userspace applications resume without trouble, we must not touch the
948 * video buffers in any way. We mark the device as frozen to make sure the URB
949 * completion handler won't try to cancel the queue when we kill the URBs.
950 */
951int uvc_video_suspend(struct uvc_video_device *video)
952{
953 if (!uvc_queue_streaming(&video->queue))
954 return 0;
955
956 video->frozen = 1;
Laurent Pincharte01117c2008-07-04 00:36:21 -0300957 uvc_uninit_video(video, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300958 usb_set_interface(video->dev->udev, video->streaming->intfnum, 0);
959 return 0;
960}
961
962/*
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300963 * Reconfigure the video interface and restart streaming if it was enabled
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300964 * before suspend.
965 *
966 * If an error occurs, disable the video queue. This will wake all pending
967 * buffers, making sure userspace applications are notified of the problem
968 * instead of waiting forever.
969 */
970int uvc_video_resume(struct uvc_video_device *video)
971{
972 int ret;
973
974 video->frozen = 0;
975
Laurent Pinchart23867b22008-11-12 11:46:43 -0300976 if ((ret = uvc_commit_video(video, &video->streaming->ctrl)) < 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300977 uvc_queue_enable(&video->queue, 0);
978 return ret;
979 }
980
981 if (!uvc_queue_streaming(&video->queue))
982 return 0;
983
Laurent Pinchart29135872008-07-04 00:35:26 -0300984 if ((ret = uvc_init_video(video, GFP_NOIO)) < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300985 uvc_queue_enable(&video->queue, 0);
986
987 return ret;
988}
989
990/* ------------------------------------------------------------------------
991 * Video device
992 */
993
994/*
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300995 * Initialize the UVC video device by switching to alternate setting 0 and
996 * retrieve the default format.
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300997 *
998 * Some cameras (namely the Fuji Finepix) set the format and frame
999 * indexes to zero. The UVC standard doesn't clearly make this a spec
1000 * violation, so try to silently fix the values if possible.
1001 *
1002 * This function is called before registering the device with V4L.
1003 */
1004int uvc_video_init(struct uvc_video_device *video)
1005{
1006 struct uvc_streaming_control *probe = &video->streaming->ctrl;
1007 struct uvc_format *format = NULL;
1008 struct uvc_frame *frame = NULL;
1009 unsigned int i;
1010 int ret;
1011
1012 if (video->streaming->nformats == 0) {
1013 uvc_printk(KERN_INFO, "No supported video formats found.\n");
1014 return -EINVAL;
1015 }
1016
1017 /* Alternate setting 0 should be the default, yet the XBox Live Vision
1018 * Cam (and possibly other devices) crash or otherwise misbehave if
1019 * they don't receive a SET_INTERFACE request before any other video
1020 * control request.
1021 */
1022 usb_set_interface(video->dev->udev, video->streaming->intfnum, 0);
1023
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03001024 /* Some webcams don't suport GET_DEF requests on the probe control. We
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001025 * fall back to GET_CUR if GET_DEF fails.
1026 */
1027 if ((ret = uvc_get_video_ctrl(video, probe, 1, GET_DEF)) < 0 &&
1028 (ret = uvc_get_video_ctrl(video, probe, 1, GET_CUR)) < 0)
1029 return ret;
1030
1031 /* Check if the default format descriptor exists. Use the first
1032 * available format otherwise.
1033 */
1034 for (i = video->streaming->nformats; i > 0; --i) {
1035 format = &video->streaming->format[i-1];
1036 if (format->index == probe->bFormatIndex)
1037 break;
1038 }
1039
1040 if (format->nframes == 0) {
1041 uvc_printk(KERN_INFO, "No frame descriptor found for the "
1042 "default format.\n");
1043 return -EINVAL;
1044 }
1045
1046 /* Zero bFrameIndex might be correct. Stream-based formats (including
1047 * MPEG-2 TS and DV) do not support frames but have a dummy frame
1048 * descriptor with bFrameIndex set to zero. If the default frame
1049 * descriptor is not found, use the first avalable frame.
1050 */
1051 for (i = format->nframes; i > 0; --i) {
1052 frame = &format->frame[i-1];
1053 if (frame->bFrameIndex == probe->bFrameIndex)
1054 break;
1055 }
1056
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001057 probe->bFormatIndex = format->index;
1058 probe->bFrameIndex = frame->bFrameIndex;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001059
1060 video->streaming->cur_format = format;
1061 video->streaming->cur_frame = frame;
1062 atomic_set(&video->active, 0);
1063
1064 /* Select the video decoding function */
Laurent Pinchartff924202008-12-28 22:32:29 -03001065 if (video->streaming->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1066 if (video->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
1067 video->decode = uvc_video_decode_isight;
1068 else if (video->streaming->intf->num_altsetting > 1)
1069 video->decode = uvc_video_decode_isoc;
1070 else
1071 video->decode = uvc_video_decode_bulk;
1072 } else {
1073 if (video->streaming->intf->num_altsetting == 1)
1074 video->decode = uvc_video_encode_bulk;
1075 else {
1076 uvc_printk(KERN_INFO, "Isochronous endpoints are not "
1077 "supported for video output devices.\n");
1078 return -EINVAL;
1079 }
1080 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001081
1082 return 0;
1083}
1084
1085/*
1086 * Enable or disable the video stream.
1087 */
1088int uvc_video_enable(struct uvc_video_device *video, int enable)
1089{
1090 int ret;
1091
1092 if (!enable) {
Laurent Pincharte01117c2008-07-04 00:36:21 -03001093 uvc_uninit_video(video, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001094 usb_set_interface(video->dev->udev,
1095 video->streaming->intfnum, 0);
1096 uvc_queue_enable(&video->queue, 0);
1097 return 0;
1098 }
1099
Laurent Pinchart0fbd8ee2008-12-06 16:25:14 -03001100 if ((video->streaming->cur_format->flags & UVC_FMT_FLAG_COMPRESSED) ||
1101 uvc_no_drop_param)
Laurent Pinchartd63beb92008-09-15 22:24:29 -03001102 video->queue.flags &= ~UVC_QUEUE_DROP_INCOMPLETE;
1103 else
1104 video->queue.flags |= UVC_QUEUE_DROP_INCOMPLETE;
1105
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001106 if ((ret = uvc_queue_enable(&video->queue, 1)) < 0)
1107 return ret;
1108
Laurent Pinchart23867b22008-11-12 11:46:43 -03001109 /* Commit the streaming parameters. */
1110 if ((ret = uvc_commit_video(video, &video->streaming->ctrl)) < 0)
1111 return ret;
1112
Laurent Pinchart29135872008-07-04 00:35:26 -03001113 return uvc_init_video(video, GFP_KERNEL);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001114}
Hans Verkuilf87086e2008-07-18 00:50:58 -03001115