]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/video/uvc/uvc_video.c
V4L/DVB (13149): uvcvideo: Add a new UVC_TRACE_VIDEO trace level
[net-next-2.6.git] / drivers / media / video / uvc / uvc_video.c
CommitLineData
c0efd232
LP
1/*
2 * uvc_video.c -- USB Video Class driver - Video handling
3 *
2c2d264b 4 * Copyright (C) 2005-2009
c0efd232
LP
5 * 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>
c0efd232
LP
15#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;
c0efd232
LP
38
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
44f0079e 43 return usb_control_msg(dev->udev, pipe, query, type, cs << 8,
c0efd232 44 unit << 8 | intfnum, data, size, timeout);
44f0079e
LP
45}
46
47int uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
48 __u8 intfnum, __u8 cs, void *data, __u16 size)
49{
50 int ret;
c0efd232 51
44f0079e
LP
52 ret = __uvc_query_ctrl(dev, query, unit, intfnum, cs, data, size,
53 UVC_CTRL_CONTROL_TIMEOUT);
c0efd232
LP
54 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
35f02a68 64static void uvc_fixup_video_ctrl(struct uvc_streaming *stream,
c0efd232
LP
65 struct uvc_streaming_control *ctrl)
66{
67 struct uvc_format *format;
078f8947
LP
68 struct uvc_frame *frame = NULL;
69 unsigned int i;
c0efd232
LP
70
71 if (ctrl->bFormatIndex <= 0 ||
35f02a68 72 ctrl->bFormatIndex > stream->nformats)
c0efd232
LP
73 return;
74
35f02a68 75 format = &stream->format[ctrl->bFormatIndex - 1];
c0efd232 76
078f8947
LP
77 for (i = 0; i < format->nframes; ++i) {
78 if (format->frame[i].bFrameIndex == ctrl->bFrameIndex) {
79 frame = &format->frame[i];
80 break;
81 }
82 }
c0efd232 83
078f8947
LP
84 if (frame == NULL)
85 return;
c0efd232
LP
86
87 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) ||
88 (ctrl->dwMaxVideoFrameSize == 0 &&
35f02a68 89 stream->dev->uvc_version < 0x0110))
c0efd232
LP
90 ctrl->dwMaxVideoFrameSize =
91 frame->dwMaxVideoFrameBufferSize;
50144aee 92
a2e35af6
LP
93 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) &&
94 stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH &&
35f02a68 95 stream->intf->num_altsetting > 1) {
50144aee
LP
96 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;
35f02a68 112 if (stream->dev->udev->speed == USB_SPEED_HIGH)
50144aee
LP
113 bandwidth /= 8;
114 bandwidth += 12;
115
116 ctrl->dwMaxPayloadTransferSize = bandwidth;
117 }
c0efd232
LP
118}
119
35f02a68 120static int uvc_get_video_ctrl(struct uvc_streaming *stream,
c0efd232
LP
121 struct uvc_streaming_control *ctrl, int probe, __u8 query)
122{
04793dd0
LP
123 __u8 *data;
124 __u16 size;
c0efd232
LP
125 int ret;
126
35f02a68 127 size = stream->dev->uvc_version >= 0x0110 ? 34 : 26;
d2be7643
JL
128 if ((stream->dev->quirks & UVC_QUIRK_PROBE_DEF) &&
129 query == UVC_GET_DEF)
130 return -EIO;
131
04793dd0
LP
132 data = kmalloc(size, GFP_KERNEL);
133 if (data == NULL)
134 return -ENOMEM;
135
35f02a68 136 ret = __uvc_query_ctrl(stream->dev, query, 0, stream->intfnum,
b482d923
LP
137 probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
138 size, UVC_CTRL_STREAMING_TIMEOUT);
44f0079e 139
b482d923 140 if ((query == UVC_GET_MIN || query == UVC_GET_MAX) && ret == 2) {
44f0079e
LP
141 /* Some cameras, mostly based on Bison Electronics chipsets,
142 * answer a GET_MIN or GET_MAX request with the wCompQuality
143 * field only.
144 */
35f02a68 145 uvc_warn_once(stream->dev, UVC_WARN_MINMAX, "UVC non "
44f0079e
LP
146 "compliance - GET_MIN/MAX(PROBE) incorrectly "
147 "supported. Enabling workaround.\n");
148 memset(ctrl, 0, sizeof ctrl);
149 ctrl->wCompQuality = le16_to_cpup((__le16 *)data);
150 ret = 0;
04793dd0 151 goto out;
b482d923 152 } else if (query == UVC_GET_DEF && probe == 1 && ret != size) {
44f0079e
LP
153 /* 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 */
35f02a68 157 uvc_warn_once(stream->dev, UVC_WARN_PROBE_DEF, "UVC non "
44f0079e
LP
158 "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 }
c0efd232
LP
169
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]);
4d3939f6
LP
179 ctrl->dwMaxVideoFrameSize = get_unaligned_le32(&data[18]);
180 ctrl->dwMaxPayloadTransferSize = get_unaligned_le32(&data[22]);
c0efd232
LP
181
182 if (size == 34) {
4d3939f6 183 ctrl->dwClockFrequency = get_unaligned_le32(&data[26]);
c0efd232
LP
184 ctrl->bmFramingInfo = data[30];
185 ctrl->bPreferedVersion = data[31];
186 ctrl->bMinVersion = data[32];
187 ctrl->bMaxVersion = data[33];
188 } else {
35f02a68 189 ctrl->dwClockFrequency = stream->dev->clock_frequency;
c0efd232
LP
190 ctrl->bmFramingInfo = 0;
191 ctrl->bPreferedVersion = 0;
192 ctrl->bMinVersion = 0;
193 ctrl->bMaxVersion = 0;
194 }
195
50144aee
LP
196 /* Some broken devices return null or wrong dwMaxVideoFrameSize and
197 * dwMaxPayloadTransferSize fields. Try to get the value from the
198 * format and frame descriptors.
c0efd232 199 */
35f02a68 200 uvc_fixup_video_ctrl(stream, ctrl);
44f0079e 201 ret = 0;
c0efd232 202
04793dd0
LP
203out:
204 kfree(data);
205 return ret;
c0efd232
LP
206}
207
35f02a68 208static int uvc_set_video_ctrl(struct uvc_streaming *stream,
c0efd232
LP
209 struct uvc_streaming_control *ctrl, int probe)
210{
04793dd0
LP
211 __u8 *data;
212 __u16 size;
213 int ret;
c0efd232 214
35f02a68 215 size = stream->dev->uvc_version >= 0x0110 ? 34 : 26;
04793dd0
LP
216 data = kzalloc(size, GFP_KERNEL);
217 if (data == NULL)
218 return -ENOMEM;
c0efd232
LP
219
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);
4d3939f6
LP
229 put_unaligned_le32(ctrl->dwMaxVideoFrameSize, &data[18]);
230 put_unaligned_le32(ctrl->dwMaxPayloadTransferSize, &data[22]);
c0efd232
LP
231
232 if (size == 34) {
4d3939f6 233 put_unaligned_le32(ctrl->dwClockFrequency, &data[26]);
c0efd232
LP
234 data[30] = ctrl->bmFramingInfo;
235 data[31] = ctrl->bPreferedVersion;
236 data[32] = ctrl->bMinVersion;
237 data[33] = ctrl->bMaxVersion;
238 }
239
35f02a68 240 ret = __uvc_query_ctrl(stream->dev, UVC_SET_CUR, 0, stream->intfnum,
b482d923
LP
241 probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
242 size, UVC_CTRL_STREAMING_TIMEOUT);
44f0079e
LP
243 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 }
04793dd0
LP
249
250 kfree(data);
251 return ret;
c0efd232
LP
252}
253
35f02a68 254int uvc_probe_video(struct uvc_streaming *stream,
c0efd232
LP
255 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
35f02a68 262 mutex_lock(&stream->mutex);
c0efd232
LP
263
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 */
35f02a68
LP
271 ret = uvc_set_video_ctrl(stream, probe, 1);
272 if (ret < 0)
c0efd232
LP
273 goto done;
274
275 /* Get the minimum and maximum values for compression settings. */
35f02a68
LP
276 if (!(stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX)) {
277 ret = uvc_get_video_ctrl(stream, &probe_min, 1, UVC_GET_MIN);
c0efd232
LP
278 if (ret < 0)
279 goto done;
35f02a68 280 ret = uvc_get_video_ctrl(stream, &probe_max, 1, UVC_GET_MAX);
c0efd232
LP
281 if (ret < 0)
282 goto done;
283
284 probe->wCompQuality = probe_max.wCompQuality;
285 }
286
287 for (i = 0; i < 2; ++i) {
35f02a68 288 ret = uvc_set_video_ctrl(stream, probe, 1);
b482d923
LP
289 if (ret < 0)
290 goto done;
35f02a68 291 ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
b482d923 292 if (ret < 0)
c0efd232
LP
293 goto done;
294
35f02a68 295 if (stream->intf->num_altsetting == 1)
c0efd232
LP
296 break;
297
298 bandwidth = probe->dwMaxPayloadTransferSize;
35f02a68 299 if (bandwidth <= stream->maxpsize)
c0efd232
LP
300 break;
301
35f02a68 302 if (stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX) {
c0efd232
LP
303 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:
35f02a68 315 mutex_unlock(&stream->mutex);
c0efd232
LP
316 return ret;
317}
318
35f02a68 319int uvc_commit_video(struct uvc_streaming *stream,
44f0079e
LP
320 struct uvc_streaming_control *probe)
321{
35f02a68 322 return uvc_set_video_ctrl(stream, probe, 0);
44f0079e
LP
323}
324
c0efd232
LP
325/* ------------------------------------------------------------------------
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 */
35f02a68 374static int uvc_video_decode_start(struct uvc_streaming *stream,
c0efd232
LP
375 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) {
35f02a68 400 stream->last_fid = fid;
c0efd232
LP
401 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.
35f02a68 406 * stream->last_fid is initialized to -1, so the first isochronous
c0efd232
LP
407 * frame will always be in sync.
408 *
35f02a68 409 * If the device doesn't toggle the FID bit, invert stream->last_fid
c0efd232
LP
410 * when the EOF bit is set to force synchronisation on the next packet.
411 */
412 if (buf->state != UVC_BUF_STATE_ACTIVE) {
35f02a68 413 if (fid == stream->last_fid) {
c0efd232
LP
414 uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of "
415 "sync).\n");
35f02a68 416 if ((stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) &&
c0efd232 417 (data[1] & UVC_STREAM_EOF))
35f02a68 418 stream->last_fid ^= UVC_STREAM_FID;
c0efd232
LP
419 return -ENODATA;
420 }
421
422 /* TODO: Handle PTS and SCR. */
423 buf->state = UVC_BUF_STATE_ACTIVE;
424 }
425
426 /* Mark the buffer as done if we're at the beginning of a new frame.
427 * End of frame detection is better implemented by checking the EOF
428 * bit (FID bit toggling is delayed by one frame compared to the EOF
429 * bit), but some devices don't set the bit at end of frame (and the
430 * last payload can be lost anyway). We thus must check if the FID has
431 * been toggled.
432 *
35f02a68 433 * stream->last_fid is initialized to -1, so the first isochronous
c0efd232
LP
434 * frame will never trigger an end of frame detection.
435 *
436 * Empty buffers (bytesused == 0) don't trigger end of frame detection
437 * as it doesn't make sense to return an empty buffer. This also
2c2d264b 438 * avoids detecting end of frame conditions at FID toggling if the
c0efd232
LP
439 * previous payload had the EOF bit set.
440 */
35f02a68 441 if (fid != stream->last_fid && buf->buf.bytesused != 0) {
c0efd232
LP
442 uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit "
443 "toggled).\n");
444 buf->state = UVC_BUF_STATE_DONE;
445 return -EAGAIN;
446 }
447
35f02a68 448 stream->last_fid = fid;
c0efd232
LP
449
450 return data[0];
451}
452
35f02a68 453static void uvc_video_decode_data(struct uvc_streaming *stream,
c0efd232
LP
454 struct uvc_buffer *buf, const __u8 *data, int len)
455{
35f02a68 456 struct uvc_video_queue *queue = &stream->queue;
c0efd232
LP
457 unsigned int maxlen, nbytes;
458 void *mem;
459
460 if (len <= 0)
461 return;
462
463 /* Copy the video data to the buffer. */
464 maxlen = buf->buf.length - buf->buf.bytesused;
465 mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused;
466 nbytes = min((unsigned int)len, maxlen);
467 memcpy(mem, data, nbytes);
468 buf->buf.bytesused += nbytes;
469
470 /* Complete the current frame if the buffer size was exceeded. */
471 if (len > maxlen) {
472 uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n");
473 buf->state = UVC_BUF_STATE_DONE;
474 }
475}
476
35f02a68 477static void uvc_video_decode_end(struct uvc_streaming *stream,
c0efd232
LP
478 struct uvc_buffer *buf, const __u8 *data, int len)
479{
480 /* Mark the buffer as done if the EOF marker is set. */
481 if (data[1] & UVC_STREAM_EOF && buf->buf.bytesused != 0) {
482 uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n");
483 if (data[0] == len)
484 uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n");
485 buf->state = UVC_BUF_STATE_DONE;
35f02a68
LP
486 if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID)
487 stream->last_fid ^= UVC_STREAM_FID;
c0efd232
LP
488 }
489}
490
2c2d264b
LP
491/* Video payload encoding is handled by uvc_video_encode_header() and
492 * uvc_video_encode_data(). Only bulk transfers are currently supported.
493 *
494 * uvc_video_encode_header is called at the start of a payload. It adds header
495 * data to the transfer buffer and returns the header size. As the only known
496 * UVC output device transfers a whole frame in a single payload, the EOF bit
497 * is always set in the header.
498 *
499 * uvc_video_encode_data is called for every URB and copies the data from the
500 * video buffer to the transfer buffer.
501 */
35f02a68 502static int uvc_video_encode_header(struct uvc_streaming *stream,
ff924203
LP
503 struct uvc_buffer *buf, __u8 *data, int len)
504{
505 data[0] = 2; /* Header length */
506 data[1] = UVC_STREAM_EOH | UVC_STREAM_EOF
35f02a68 507 | (stream->last_fid & UVC_STREAM_FID);
ff924203
LP
508 return 2;
509}
510
35f02a68 511static int uvc_video_encode_data(struct uvc_streaming *stream,
ff924203
LP
512 struct uvc_buffer *buf, __u8 *data, int len)
513{
35f02a68 514 struct uvc_video_queue *queue = &stream->queue;
ff924203
LP
515 unsigned int nbytes;
516 void *mem;
517
518 /* Copy video data to the URB buffer. */
519 mem = queue->mem + buf->buf.m.offset + queue->buf_used;
520 nbytes = min((unsigned int)len, buf->buf.bytesused - queue->buf_used);
35f02a68 521 nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size,
ff924203
LP
522 nbytes);
523 memcpy(data, mem, nbytes);
524
525 queue->buf_used += nbytes;
526
527 return nbytes;
528}
529
c0efd232
LP
530/* ------------------------------------------------------------------------
531 * URB handling
532 */
533
534/*
535 * Completion handler for video URBs.
536 */
35f02a68
LP
537static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
538 struct uvc_buffer *buf)
c0efd232
LP
539{
540 u8 *mem;
541 int ret, i;
542
543 for (i = 0; i < urb->number_of_packets; ++i) {
544 if (urb->iso_frame_desc[i].status < 0) {
545 uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
546 "lost (%d).\n", urb->iso_frame_desc[i].status);
547 continue;
548 }
549
550 /* Decode the payload header. */
551 mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
552 do {
35f02a68 553 ret = uvc_video_decode_start(stream, buf, mem,
c0efd232
LP
554 urb->iso_frame_desc[i].actual_length);
555 if (ret == -EAGAIN)
35f02a68
LP
556 buf = uvc_queue_next_buffer(&stream->queue,
557 buf);
c0efd232
LP
558 } while (ret == -EAGAIN);
559
560 if (ret < 0)
561 continue;
562
563 /* Decode the payload data. */
35f02a68 564 uvc_video_decode_data(stream, buf, mem + ret,
c0efd232
LP
565 urb->iso_frame_desc[i].actual_length - ret);
566
567 /* Process the header again. */
35f02a68 568 uvc_video_decode_end(stream, buf, mem,
08ebd003 569 urb->iso_frame_desc[i].actual_length);
c0efd232
LP
570
571 if (buf->state == UVC_BUF_STATE_DONE ||
572 buf->state == UVC_BUF_STATE_ERROR)
35f02a68 573 buf = uvc_queue_next_buffer(&stream->queue, buf);
c0efd232
LP
574 }
575}
576
35f02a68
LP
577static void uvc_video_decode_bulk(struct urb *urb, struct uvc_streaming *stream,
578 struct uvc_buffer *buf)
c0efd232
LP
579{
580 u8 *mem;
581 int len, ret;
582
c90e7779
LP
583 if (urb->actual_length == 0)
584 return;
585
c0efd232
LP
586 mem = urb->transfer_buffer;
587 len = urb->actual_length;
35f02a68 588 stream->bulk.payload_size += len;
c0efd232
LP
589
590 /* If the URB is the first of its payload, decode and save the
591 * header.
592 */
35f02a68 593 if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) {
c0efd232 594 do {
35f02a68 595 ret = uvc_video_decode_start(stream, buf, mem, len);
c0efd232 596 if (ret == -EAGAIN)
35f02a68
LP
597 buf = uvc_queue_next_buffer(&stream->queue,
598 buf);
c0efd232
LP
599 } while (ret == -EAGAIN);
600
601 /* If an error occured skip the rest of the payload. */
602 if (ret < 0 || buf == NULL) {
35f02a68 603 stream->bulk.skip_payload = 1;
f8dd4af6 604 } else {
35f02a68
LP
605 memcpy(stream->bulk.header, mem, ret);
606 stream->bulk.header_size = ret;
c0efd232 607
f8dd4af6
LP
608 mem += ret;
609 len -= ret;
610 }
c0efd232
LP
611 }
612
613 /* The buffer queue might have been cancelled while a bulk transfer
614 * was in progress, so we can reach here with buf equal to NULL. Make
615 * sure buf is never dereferenced if NULL.
616 */
617
618 /* Process video data. */
35f02a68
LP
619 if (!stream->bulk.skip_payload && buf != NULL)
620 uvc_video_decode_data(stream, buf, mem, len);
c0efd232
LP
621
622 /* Detect the payload end by a URB smaller than the maximum size (or
623 * a payload size equal to the maximum) and process the header again.
624 */
625 if (urb->actual_length < urb->transfer_buffer_length ||
35f02a68
LP
626 stream->bulk.payload_size >= stream->bulk.max_payload_size) {
627 if (!stream->bulk.skip_payload && buf != NULL) {
628 uvc_video_decode_end(stream, buf, stream->bulk.header,
629 stream->bulk.payload_size);
c0efd232
LP
630 if (buf->state == UVC_BUF_STATE_DONE ||
631 buf->state == UVC_BUF_STATE_ERROR)
35f02a68
LP
632 buf = uvc_queue_next_buffer(&stream->queue,
633 buf);
c0efd232
LP
634 }
635
35f02a68
LP
636 stream->bulk.header_size = 0;
637 stream->bulk.skip_payload = 0;
638 stream->bulk.payload_size = 0;
c0efd232
LP
639 }
640}
641
35f02a68
LP
642static void uvc_video_encode_bulk(struct urb *urb, struct uvc_streaming *stream,
643 struct uvc_buffer *buf)
ff924203
LP
644{
645 u8 *mem = urb->transfer_buffer;
35f02a68 646 int len = stream->urb_size, ret;
ff924203
LP
647
648 if (buf == NULL) {
649 urb->transfer_buffer_length = 0;
650 return;
651 }
652
653 /* If the URB is the first of its payload, add the header. */
35f02a68
LP
654 if (stream->bulk.header_size == 0) {
655 ret = uvc_video_encode_header(stream, buf, mem, len);
656 stream->bulk.header_size = ret;
657 stream->bulk.payload_size += ret;
ff924203
LP
658 mem += ret;
659 len -= ret;
660 }
661
662 /* Process video data. */
35f02a68 663 ret = uvc_video_encode_data(stream, buf, mem, len);
ff924203 664
35f02a68 665 stream->bulk.payload_size += ret;
ff924203
LP
666 len -= ret;
667
35f02a68
LP
668 if (buf->buf.bytesused == stream->queue.buf_used ||
669 stream->bulk.payload_size == stream->bulk.max_payload_size) {
670 if (buf->buf.bytesused == stream->queue.buf_used) {
671 stream->queue.buf_used = 0;
ff924203 672 buf->state = UVC_BUF_STATE_DONE;
35f02a68
LP
673 uvc_queue_next_buffer(&stream->queue, buf);
674 stream->last_fid ^= UVC_STREAM_FID;
ff924203
LP
675 }
676
35f02a68
LP
677 stream->bulk.header_size = 0;
678 stream->bulk.payload_size = 0;
ff924203
LP
679 }
680
35f02a68 681 urb->transfer_buffer_length = stream->urb_size - len;
ff924203
LP
682}
683
c0efd232
LP
684static void uvc_video_complete(struct urb *urb)
685{
35f02a68
LP
686 struct uvc_streaming *stream = urb->context;
687 struct uvc_video_queue *queue = &stream->queue;
c0efd232
LP
688 struct uvc_buffer *buf = NULL;
689 unsigned long flags;
690 int ret;
691
692 switch (urb->status) {
693 case 0:
694 break;
695
696 default:
697 uvc_printk(KERN_WARNING, "Non-zero status (%d) in video "
698 "completion handler.\n", urb->status);
699
700 case -ENOENT: /* usb_kill_urb() called. */
35f02a68 701 if (stream->frozen)
c0efd232
LP
702 return;
703
704 case -ECONNRESET: /* usb_unlink_urb() called. */
705 case -ESHUTDOWN: /* The endpoint is being disabled. */
706 uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
707 return;
708 }
709
710 spin_lock_irqsave(&queue->irqlock, flags);
711 if (!list_empty(&queue->irqqueue))
712 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
713 queue);
714 spin_unlock_irqrestore(&queue->irqlock, flags);
715
35f02a68 716 stream->decode(urb, stream, buf);
c0efd232
LP
717
718 if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
719 uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n",
720 ret);
721 }
722}
723
e01117c8
LP
724/*
725 * Free transfer buffers.
726 */
35f02a68 727static void uvc_free_urb_buffers(struct uvc_streaming *stream)
e01117c8
LP
728{
729 unsigned int i;
730
731 for (i = 0; i < UVC_URBS; ++i) {
35f02a68
LP
732 if (stream->urb_buffer[i]) {
733 usb_buffer_free(stream->dev->udev, stream->urb_size,
734 stream->urb_buffer[i], stream->urb_dma[i]);
735 stream->urb_buffer[i] = NULL;
e01117c8
LP
736 }
737 }
738
35f02a68 739 stream->urb_size = 0;
e01117c8
LP
740}
741
742/*
743 * Allocate transfer buffers. This function can be called with buffers
744 * already allocated when resuming from suspend, in which case it will
745 * return without touching the buffers.
746 *
efdc8a95
LP
747 * Limit the buffer size to UVC_MAX_PACKETS bulk/isochronous packets. If the
748 * system is too low on memory try successively smaller numbers of packets
749 * until allocation succeeds.
750 *
751 * Return the number of allocated packets on success or 0 when out of memory.
e01117c8 752 */
35f02a68 753static int uvc_alloc_urb_buffers(struct uvc_streaming *stream,
efdc8a95 754 unsigned int size, unsigned int psize, gfp_t gfp_flags)
e01117c8 755{
efdc8a95 756 unsigned int npackets;
e01117c8
LP
757 unsigned int i;
758
759 /* Buffers are already allocated, bail out. */
35f02a68
LP
760 if (stream->urb_size)
761 return stream->urb_size / psize;
e01117c8 762
efdc8a95
LP
763 /* Compute the number of packets. Bulk endpoints might transfer UVC
764 * payloads accross multiple URBs.
765 */
766 npackets = DIV_ROUND_UP(size, psize);
767 if (npackets > UVC_MAX_PACKETS)
768 npackets = UVC_MAX_PACKETS;
769
770 /* Retry allocations until one succeed. */
771 for (; npackets > 1; npackets /= 2) {
772 for (i = 0; i < UVC_URBS; ++i) {
35f02a68
LP
773 stream->urb_buffer[i] = usb_buffer_alloc(
774 stream->dev->udev, psize * npackets,
775 gfp_flags | __GFP_NOWARN, &stream->urb_dma[i]);
776 if (!stream->urb_buffer[i]) {
777 uvc_free_urb_buffers(stream);
efdc8a95
LP
778 break;
779 }
780 }
781
782 if (i == UVC_URBS) {
35f02a68 783 stream->urb_size = psize * npackets;
663a4192
LP
784 uvc_trace(UVC_TRACE_VIDEO, "Allocated %u URB buffers "
785 "of %ux%u bytes each.\n", UVC_URBS, npackets,
786 psize);
efdc8a95 787 return npackets;
e01117c8
LP
788 }
789 }
790
663a4192
LP
791 uvc_trace(UVC_TRACE_VIDEO, "Failed to allocate URB buffers (%u bytes "
792 "per packet).\n", psize);
e01117c8
LP
793 return 0;
794}
795
c0efd232
LP
796/*
797 * Uninitialize isochronous/bulk URBs and free transfer buffers.
798 */
35f02a68 799static void uvc_uninit_video(struct uvc_streaming *stream, int free_buffers)
c0efd232
LP
800{
801 struct urb *urb;
802 unsigned int i;
803
804 for (i = 0; i < UVC_URBS; ++i) {
35f02a68
LP
805 urb = stream->urb[i];
806 if (urb == NULL)
c0efd232
LP
807 continue;
808
809 usb_kill_urb(urb);
c0efd232 810 usb_free_urb(urb);
35f02a68 811 stream->urb[i] = NULL;
c0efd232 812 }
e01117c8
LP
813
814 if (free_buffers)
35f02a68 815 uvc_free_urb_buffers(stream);
c0efd232
LP
816}
817
818/*
819 * Initialize isochronous URBs and allocate transfer buffers. The packet size
820 * is given by the endpoint.
821 */
35f02a68 822static int uvc_init_video_isoc(struct uvc_streaming *stream,
29135878 823 struct usb_host_endpoint *ep, gfp_t gfp_flags)
c0efd232
LP
824{
825 struct urb *urb;
826 unsigned int npackets, i, j;
efdc8a95
LP
827 u16 psize;
828 u32 size;
c0efd232 829
c0efd232
LP
830 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
831 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
35f02a68 832 size = stream->ctrl.dwMaxVideoFrameSize;
c0efd232 833
35f02a68 834 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
efdc8a95
LP
835 if (npackets == 0)
836 return -ENOMEM;
c0efd232
LP
837
838 size = npackets * psize;
839
840 for (i = 0; i < UVC_URBS; ++i) {
29135878 841 urb = usb_alloc_urb(npackets, gfp_flags);
c0efd232 842 if (urb == NULL) {
35f02a68 843 uvc_uninit_video(stream, 1);
c0efd232
LP
844 return -ENOMEM;
845 }
846
35f02a68
LP
847 urb->dev = stream->dev->udev;
848 urb->context = stream;
849 urb->pipe = usb_rcvisocpipe(stream->dev->udev,
c0efd232
LP
850 ep->desc.bEndpointAddress);
851 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
852 urb->interval = ep->desc.bInterval;
35f02a68
LP
853 urb->transfer_buffer = stream->urb_buffer[i];
854 urb->transfer_dma = stream->urb_dma[i];
c0efd232
LP
855 urb->complete = uvc_video_complete;
856 urb->number_of_packets = npackets;
857 urb->transfer_buffer_length = size;
858
859 for (j = 0; j < npackets; ++j) {
860 urb->iso_frame_desc[j].offset = j * psize;
861 urb->iso_frame_desc[j].length = psize;
862 }
863
35f02a68 864 stream->urb[i] = urb;
c0efd232
LP
865 }
866
867 return 0;
868}
869
870/*
871 * Initialize bulk URBs and allocate transfer buffers. The packet size is
872 * given by the endpoint.
873 */
35f02a68 874static int uvc_init_video_bulk(struct uvc_streaming *stream,
29135878 875 struct usb_host_endpoint *ep, gfp_t gfp_flags)
c0efd232
LP
876{
877 struct urb *urb;
efdc8a95
LP
878 unsigned int npackets, pipe, i;
879 u16 psize;
880 u32 size;
881
c0efd232 882 psize = le16_to_cpu(ep->desc.wMaxPacketSize) & 0x07ff;
35f02a68
LP
883 size = stream->ctrl.dwMaxPayloadTransferSize;
884 stream->bulk.max_payload_size = size;
c0efd232 885
35f02a68 886 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
efdc8a95 887 if (npackets == 0)
e01117c8
LP
888 return -ENOMEM;
889
efdc8a95
LP
890 size = npackets * psize;
891
ff924203 892 if (usb_endpoint_dir_in(&ep->desc))
35f02a68 893 pipe = usb_rcvbulkpipe(stream->dev->udev,
ff924203
LP
894 ep->desc.bEndpointAddress);
895 else
35f02a68 896 pipe = usb_sndbulkpipe(stream->dev->udev,
ff924203
LP
897 ep->desc.bEndpointAddress);
898
35f02a68 899 if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
ff924203 900 size = 0;
c0efd232
LP
901
902 for (i = 0; i < UVC_URBS; ++i) {
29135878 903 urb = usb_alloc_urb(0, gfp_flags);
c0efd232 904 if (urb == NULL) {
35f02a68 905 uvc_uninit_video(stream, 1);
c0efd232
LP
906 return -ENOMEM;
907 }
908
35f02a68
LP
909 usb_fill_bulk_urb(urb, stream->dev->udev, pipe,
910 stream->urb_buffer[i], size, uvc_video_complete,
911 stream);
c0efd232 912 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
35f02a68 913 urb->transfer_dma = stream->urb_dma[i];
c0efd232 914
35f02a68 915 stream->urb[i] = urb;
c0efd232
LP
916 }
917
918 return 0;
919}
920
921/*
922 * Initialize isochronous/bulk URBs and allocate transfer buffers.
923 */
35f02a68 924static int uvc_init_video(struct uvc_streaming *stream, gfp_t gfp_flags)
c0efd232 925{
35f02a68 926 struct usb_interface *intf = stream->intf;
c0efd232
LP
927 struct usb_host_interface *alts;
928 struct usb_host_endpoint *ep = NULL;
35f02a68 929 int intfnum = stream->intfnum;
c0efd232
LP
930 unsigned int bandwidth, psize, i;
931 int ret;
932
35f02a68
LP
933 stream->last_fid = -1;
934 stream->bulk.header_size = 0;
935 stream->bulk.skip_payload = 0;
936 stream->bulk.payload_size = 0;
c0efd232
LP
937
938 if (intf->num_altsetting > 1) {
939 /* Isochronous endpoint, select the alternate setting. */
35f02a68 940 bandwidth = stream->ctrl.dwMaxPayloadTransferSize;
c0efd232
LP
941
942 if (bandwidth == 0) {
663a4192
LP
943 uvc_trace(UVC_TRACE_VIDEO, "Device requested null "
944 "bandwidth, defaulting to lowest.\n");
c0efd232 945 bandwidth = 1;
663a4192
LP
946 } else {
947 uvc_trace(UVC_TRACE_VIDEO, "Device requested %u "
948 "B/frame bandwidth.\n", bandwidth);
c0efd232
LP
949 }
950
951 for (i = 0; i < intf->num_altsetting; ++i) {
952 alts = &intf->altsetting[i];
953 ep = uvc_find_endpoint(alts,
35f02a68 954 stream->header.bEndpointAddress);
c0efd232
LP
955 if (ep == NULL)
956 continue;
957
958 /* Check if the bandwidth is high enough. */
959 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
960 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
961 if (psize >= bandwidth)
962 break;
963 }
964
663a4192
LP
965 if (i >= intf->num_altsetting) {
966 uvc_trace(UVC_TRACE_VIDEO, "No fast enough alt setting "
967 "for requested bandwidth.\n");
c0efd232 968 return -EIO;
663a4192 969 }
c0efd232 970
35f02a68
LP
971 ret = usb_set_interface(stream->dev->udev, intfnum, i);
972 if (ret < 0)
c0efd232
LP
973 return ret;
974
35f02a68 975 ret = uvc_init_video_isoc(stream, ep, gfp_flags);
c0efd232
LP
976 } else {
977 /* Bulk endpoint, proceed to URB initialization. */
978 ep = uvc_find_endpoint(&intf->altsetting[0],
35f02a68 979 stream->header.bEndpointAddress);
c0efd232
LP
980 if (ep == NULL)
981 return -EIO;
982
35f02a68 983 ret = uvc_init_video_bulk(stream, ep, gfp_flags);
c0efd232
LP
984 }
985
986 if (ret < 0)
987 return ret;
988
989 /* Submit the URBs. */
990 for (i = 0; i < UVC_URBS; ++i) {
35f02a68
LP
991 ret = usb_submit_urb(stream->urb[i], gfp_flags);
992 if (ret < 0) {
c0efd232
LP
993 uvc_printk(KERN_ERR, "Failed to submit URB %u "
994 "(%d).\n", i, ret);
35f02a68 995 uvc_uninit_video(stream, 1);
c0efd232
LP
996 return ret;
997 }
998 }
999
1000 return 0;
1001}
1002
1003/* --------------------------------------------------------------------------
1004 * Suspend/resume
1005 */
1006
1007/*
1008 * Stop streaming without disabling the video queue.
1009 *
1010 * To let userspace applications resume without trouble, we must not touch the
1011 * video buffers in any way. We mark the device as frozen to make sure the URB
1012 * completion handler won't try to cancel the queue when we kill the URBs.
1013 */
35f02a68 1014int uvc_video_suspend(struct uvc_streaming *stream)
c0efd232 1015{
35f02a68 1016 if (!uvc_queue_streaming(&stream->queue))
c0efd232
LP
1017 return 0;
1018
35f02a68
LP
1019 stream->frozen = 1;
1020 uvc_uninit_video(stream, 0);
1021 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
c0efd232
LP
1022 return 0;
1023}
1024
1025/*
2c2d264b 1026 * Reconfigure the video interface and restart streaming if it was enabled
c0efd232
LP
1027 * before suspend.
1028 *
1029 * If an error occurs, disable the video queue. This will wake all pending
1030 * buffers, making sure userspace applications are notified of the problem
1031 * instead of waiting forever.
1032 */
35f02a68 1033int uvc_video_resume(struct uvc_streaming *stream)
c0efd232
LP
1034{
1035 int ret;
1036
35f02a68 1037 stream->frozen = 0;
c0efd232 1038
35f02a68
LP
1039 ret = uvc_commit_video(stream, &stream->ctrl);
1040 if (ret < 0) {
1041 uvc_queue_enable(&stream->queue, 0);
c0efd232
LP
1042 return ret;
1043 }
1044
35f02a68 1045 if (!uvc_queue_streaming(&stream->queue))
c0efd232
LP
1046 return 0;
1047
35f02a68
LP
1048 ret = uvc_init_video(stream, GFP_NOIO);
1049 if (ret < 0)
1050 uvc_queue_enable(&stream->queue, 0);
c0efd232
LP
1051
1052 return ret;
1053}
1054
1055/* ------------------------------------------------------------------------
1056 * Video device
1057 */
1058
1059/*
2c2d264b
LP
1060 * Initialize the UVC video device by switching to alternate setting 0 and
1061 * retrieve the default format.
c0efd232
LP
1062 *
1063 * Some cameras (namely the Fuji Finepix) set the format and frame
1064 * indexes to zero. The UVC standard doesn't clearly make this a spec
1065 * violation, so try to silently fix the values if possible.
1066 *
1067 * This function is called before registering the device with V4L.
1068 */
35f02a68 1069int uvc_video_init(struct uvc_streaming *stream)
c0efd232 1070{
35f02a68 1071 struct uvc_streaming_control *probe = &stream->ctrl;
c0efd232
LP
1072 struct uvc_format *format = NULL;
1073 struct uvc_frame *frame = NULL;
1074 unsigned int i;
1075 int ret;
1076
35f02a68 1077 if (stream->nformats == 0) {
c0efd232
LP
1078 uvc_printk(KERN_INFO, "No supported video formats found.\n");
1079 return -EINVAL;
1080 }
1081
35f02a68
LP
1082 atomic_set(&stream->active, 0);
1083
1084 /* Initialize the video buffers queue. */
1085 uvc_queue_init(&stream->queue, stream->type);
1086
c0efd232
LP
1087 /* Alternate setting 0 should be the default, yet the XBox Live Vision
1088 * Cam (and possibly other devices) crash or otherwise misbehave if
1089 * they don't receive a SET_INTERFACE request before any other video
1090 * control request.
1091 */
35f02a68 1092 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
c0efd232 1093
72362422
LP
1094 /* Set the streaming probe control with default streaming parameters
1095 * retrieved from the device. Webcams that don't suport GET_DEF
1096 * requests on the probe control will just keep their current streaming
1097 * parameters.
c0efd232 1098 */
35f02a68
LP
1099 if (uvc_get_video_ctrl(stream, probe, 1, UVC_GET_DEF) == 0)
1100 uvc_set_video_ctrl(stream, probe, 1);
72362422
LP
1101
1102 /* Initialize the streaming parameters with the probe control current
1103 * value. This makes sure SET_CUR requests on the streaming commit
1104 * control will always use values retrieved from a successful GET_CUR
1105 * request on the probe control, as required by the UVC specification.
1106 */
35f02a68 1107 ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
b482d923 1108 if (ret < 0)
c0efd232
LP
1109 return ret;
1110
1111 /* Check if the default format descriptor exists. Use the first
1112 * available format otherwise.
1113 */
35f02a68
LP
1114 for (i = stream->nformats; i > 0; --i) {
1115 format = &stream->format[i-1];
c0efd232
LP
1116 if (format->index == probe->bFormatIndex)
1117 break;
1118 }
1119
1120 if (format->nframes == 0) {
1121 uvc_printk(KERN_INFO, "No frame descriptor found for the "
1122 "default format.\n");
1123 return -EINVAL;
1124 }
1125
1126 /* Zero bFrameIndex might be correct. Stream-based formats (including
1127 * MPEG-2 TS and DV) do not support frames but have a dummy frame
1128 * descriptor with bFrameIndex set to zero. If the default frame
078f8947 1129 * descriptor is not found, use the first available frame.
c0efd232
LP
1130 */
1131 for (i = format->nframes; i > 0; --i) {
1132 frame = &format->frame[i-1];
1133 if (frame->bFrameIndex == probe->bFrameIndex)
1134 break;
1135 }
1136
c0efd232
LP
1137 probe->bFormatIndex = format->index;
1138 probe->bFrameIndex = frame->bFrameIndex;
c0efd232 1139
35f02a68
LP
1140 stream->cur_format = format;
1141 stream->cur_frame = frame;
c0efd232
LP
1142
1143 /* Select the video decoding function */
35f02a68
LP
1144 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1145 if (stream->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
1146 stream->decode = uvc_video_decode_isight;
1147 else if (stream->intf->num_altsetting > 1)
1148 stream->decode = uvc_video_decode_isoc;
ff924203 1149 else
35f02a68 1150 stream->decode = uvc_video_decode_bulk;
ff924203 1151 } else {
35f02a68
LP
1152 if (stream->intf->num_altsetting == 1)
1153 stream->decode = uvc_video_encode_bulk;
ff924203
LP
1154 else {
1155 uvc_printk(KERN_INFO, "Isochronous endpoints are not "
1156 "supported for video output devices.\n");
1157 return -EINVAL;
1158 }
1159 }
c0efd232
LP
1160
1161 return 0;
1162}
1163
1164/*
1165 * Enable or disable the video stream.
1166 */
35f02a68 1167int uvc_video_enable(struct uvc_streaming *stream, int enable)
c0efd232
LP
1168{
1169 int ret;
1170
1171 if (!enable) {
35f02a68
LP
1172 uvc_uninit_video(stream, 1);
1173 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1174 uvc_queue_enable(&stream->queue, 0);
c0efd232
LP
1175 return 0;
1176 }
1177
35f02a68 1178 if ((stream->cur_format->flags & UVC_FMT_FLAG_COMPRESSED) ||
0fbd8ee6 1179 uvc_no_drop_param)
35f02a68 1180 stream->queue.flags &= ~UVC_QUEUE_DROP_INCOMPLETE;
d63beb9e 1181 else
35f02a68 1182 stream->queue.flags |= UVC_QUEUE_DROP_INCOMPLETE;
d63beb9e 1183
35f02a68
LP
1184 ret = uvc_queue_enable(&stream->queue, 1);
1185 if (ret < 0)
c0efd232
LP
1186 return ret;
1187
23867b25 1188 /* Commit the streaming parameters. */
35f02a68
LP
1189 ret = uvc_commit_video(stream, &stream->ctrl);
1190 if (ret < 0)
23867b25
LP
1191 return ret;
1192
35f02a68 1193 return uvc_init_video(stream, GFP_KERNEL);
c0efd232 1194}
f87086e3 1195