2 * uvc_video.c -- USB Video Class driver - Video handling
4 * Copyright (C) 2005-2009
5 * Laurent Pinchart (laurent.pinchart@skynet.be)
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.
14 #include <linux/kernel.h>
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>
24 #include <media/v4l2-common.h>
28 /* ------------------------------------------------------------------------
32 static int __uvc_query_ctrl(struct uvc_device
*dev
, __u8 query
, __u8 unit
,
33 __u8 intfnum
, __u8 cs
, void *data
, __u16 size
,
36 __u8 type
= USB_TYPE_CLASS
| USB_RECIP_INTERFACE
;
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
;
43 return usb_control_msg(dev
->udev
, pipe
, query
, type
, cs
<< 8,
44 unit
<< 8 | intfnum
, data
, size
, timeout
);
47 int uvc_query_ctrl(struct uvc_device
*dev
, __u8 query
, __u8 unit
,
48 __u8 intfnum
, __u8 cs
, void *data
, __u16 size
)
52 ret
= __uvc_query_ctrl(dev
, query
, unit
, intfnum
, cs
, data
, size
,
53 UVC_CTRL_CONTROL_TIMEOUT
);
55 uvc_printk(KERN_ERR
, "Failed to query (%u) UVC control %u "
56 "(unit %u) : %d (exp. %u).\n", query
, cs
, unit
, ret
,
64 static void uvc_fixup_video_ctrl(struct uvc_streaming
*stream
,
65 struct uvc_streaming_control
*ctrl
)
67 struct uvc_format
*format
;
68 struct uvc_frame
*frame
= NULL
;
71 if (ctrl
->bFormatIndex
<= 0 ||
72 ctrl
->bFormatIndex
> stream
->nformats
)
75 format
= &stream
->format
[ctrl
->bFormatIndex
- 1];
77 for (i
= 0; i
< format
->nframes
; ++i
) {
78 if (format
->frame
[i
].bFrameIndex
== ctrl
->bFrameIndex
) {
79 frame
= &format
->frame
[i
];
87 if (!(format
->flags
& UVC_FMT_FLAG_COMPRESSED
) ||
88 (ctrl
->dwMaxVideoFrameSize
== 0 &&
89 stream
->dev
->uvc_version
< 0x0110))
90 ctrl
->dwMaxVideoFrameSize
=
91 frame
->dwMaxVideoFrameBufferSize
;
93 if (stream
->dev
->quirks
& UVC_QUIRK_FIX_BANDWIDTH
&&
94 stream
->intf
->num_altsetting
> 1) {
98 interval
= (ctrl
->dwFrameInterval
> 100000)
99 ? ctrl
->dwFrameInterval
100 : frame
->dwFrameInterval
[0];
102 /* Compute a bandwidth estimation by multiplying the frame
103 * size by the number of video frames per second, divide the
104 * result by the number of USB frames (or micro-frames for
105 * high-speed devices) per second and add the UVC header size
106 * (assumed to be 12 bytes long).
108 bandwidth
= frame
->wWidth
* frame
->wHeight
/ 8 * format
->bpp
;
109 bandwidth
*= 10000000 / interval
+ 1;
111 if (stream
->dev
->udev
->speed
== USB_SPEED_HIGH
)
115 ctrl
->dwMaxPayloadTransferSize
= bandwidth
;
119 static int uvc_get_video_ctrl(struct uvc_streaming
*stream
,
120 struct uvc_streaming_control
*ctrl
, int probe
, __u8 query
)
126 size
= stream
->dev
->uvc_version
>= 0x0110 ? 34 : 26;
127 data
= kmalloc(size
, GFP_KERNEL
);
131 if ((stream
->dev
->quirks
& UVC_QUIRK_PROBE_DEF
) && query
== UVC_GET_DEF
)
134 ret
= __uvc_query_ctrl(stream
->dev
, query
, 0, stream
->intfnum
,
135 probe
? UVC_VS_PROBE_CONTROL
: UVC_VS_COMMIT_CONTROL
, data
,
136 size
, UVC_CTRL_STREAMING_TIMEOUT
);
138 if ((query
== UVC_GET_MIN
|| query
== UVC_GET_MAX
) && ret
== 2) {
139 /* Some cameras, mostly based on Bison Electronics chipsets,
140 * answer a GET_MIN or GET_MAX request with the wCompQuality
143 uvc_warn_once(stream
->dev
, UVC_WARN_MINMAX
, "UVC non "
144 "compliance - GET_MIN/MAX(PROBE) incorrectly "
145 "supported. Enabling workaround.\n");
146 memset(ctrl
, 0, sizeof ctrl
);
147 ctrl
->wCompQuality
= le16_to_cpup((__le16
*)data
);
150 } else if (query
== UVC_GET_DEF
&& probe
== 1 && ret
!= size
) {
151 /* Many cameras don't support the GET_DEF request on their
152 * video probe control. Warn once and return, the caller will
153 * fall back to GET_CUR.
155 uvc_warn_once(stream
->dev
, UVC_WARN_PROBE_DEF
, "UVC non "
156 "compliance - GET_DEF(PROBE) not supported. "
157 "Enabling workaround.\n");
160 } else if (ret
!= size
) {
161 uvc_printk(KERN_ERR
, "Failed to query (%u) UVC %s control : "
162 "%d (exp. %u).\n", query
, probe
? "probe" : "commit",
168 ctrl
->bmHint
= le16_to_cpup((__le16
*)&data
[0]);
169 ctrl
->bFormatIndex
= data
[2];
170 ctrl
->bFrameIndex
= data
[3];
171 ctrl
->dwFrameInterval
= le32_to_cpup((__le32
*)&data
[4]);
172 ctrl
->wKeyFrameRate
= le16_to_cpup((__le16
*)&data
[8]);
173 ctrl
->wPFrameRate
= le16_to_cpup((__le16
*)&data
[10]);
174 ctrl
->wCompQuality
= le16_to_cpup((__le16
*)&data
[12]);
175 ctrl
->wCompWindowSize
= le16_to_cpup((__le16
*)&data
[14]);
176 ctrl
->wDelay
= le16_to_cpup((__le16
*)&data
[16]);
177 ctrl
->dwMaxVideoFrameSize
= get_unaligned_le32(&data
[18]);
178 ctrl
->dwMaxPayloadTransferSize
= get_unaligned_le32(&data
[22]);
181 ctrl
->dwClockFrequency
= get_unaligned_le32(&data
[26]);
182 ctrl
->bmFramingInfo
= data
[30];
183 ctrl
->bPreferedVersion
= data
[31];
184 ctrl
->bMinVersion
= data
[32];
185 ctrl
->bMaxVersion
= data
[33];
187 ctrl
->dwClockFrequency
= stream
->dev
->clock_frequency
;
188 ctrl
->bmFramingInfo
= 0;
189 ctrl
->bPreferedVersion
= 0;
190 ctrl
->bMinVersion
= 0;
191 ctrl
->bMaxVersion
= 0;
194 /* Some broken devices return null or wrong dwMaxVideoFrameSize and
195 * dwMaxPayloadTransferSize fields. Try to get the value from the
196 * format and frame descriptors.
198 uvc_fixup_video_ctrl(stream
, ctrl
);
206 static int uvc_set_video_ctrl(struct uvc_streaming
*stream
,
207 struct uvc_streaming_control
*ctrl
, int probe
)
213 size
= stream
->dev
->uvc_version
>= 0x0110 ? 34 : 26;
214 data
= kzalloc(size
, GFP_KERNEL
);
218 *(__le16
*)&data
[0] = cpu_to_le16(ctrl
->bmHint
);
219 data
[2] = ctrl
->bFormatIndex
;
220 data
[3] = ctrl
->bFrameIndex
;
221 *(__le32
*)&data
[4] = cpu_to_le32(ctrl
->dwFrameInterval
);
222 *(__le16
*)&data
[8] = cpu_to_le16(ctrl
->wKeyFrameRate
);
223 *(__le16
*)&data
[10] = cpu_to_le16(ctrl
->wPFrameRate
);
224 *(__le16
*)&data
[12] = cpu_to_le16(ctrl
->wCompQuality
);
225 *(__le16
*)&data
[14] = cpu_to_le16(ctrl
->wCompWindowSize
);
226 *(__le16
*)&data
[16] = cpu_to_le16(ctrl
->wDelay
);
227 put_unaligned_le32(ctrl
->dwMaxVideoFrameSize
, &data
[18]);
228 put_unaligned_le32(ctrl
->dwMaxPayloadTransferSize
, &data
[22]);
231 put_unaligned_le32(ctrl
->dwClockFrequency
, &data
[26]);
232 data
[30] = ctrl
->bmFramingInfo
;
233 data
[31] = ctrl
->bPreferedVersion
;
234 data
[32] = ctrl
->bMinVersion
;
235 data
[33] = ctrl
->bMaxVersion
;
238 ret
= __uvc_query_ctrl(stream
->dev
, UVC_SET_CUR
, 0, stream
->intfnum
,
239 probe
? UVC_VS_PROBE_CONTROL
: UVC_VS_COMMIT_CONTROL
, data
,
240 size
, UVC_CTRL_STREAMING_TIMEOUT
);
242 uvc_printk(KERN_ERR
, "Failed to set UVC %s control : "
243 "%d (exp. %u).\n", probe
? "probe" : "commit",
252 int uvc_probe_video(struct uvc_streaming
*stream
,
253 struct uvc_streaming_control
*probe
)
255 struct uvc_streaming_control probe_min
, probe_max
;
260 mutex_lock(&stream
->mutex
);
262 /* Perform probing. The device should adjust the requested values
263 * according to its capabilities. However, some devices, namely the
264 * first generation UVC Logitech webcams, don't implement the Video
265 * Probe control properly, and just return the needed bandwidth. For
266 * that reason, if the needed bandwidth exceeds the maximum available
267 * bandwidth, try to lower the quality.
269 ret
= uvc_set_video_ctrl(stream
, probe
, 1);
273 /* Get the minimum and maximum values for compression settings. */
274 if (!(stream
->dev
->quirks
& UVC_QUIRK_PROBE_MINMAX
)) {
275 ret
= uvc_get_video_ctrl(stream
, &probe_min
, 1, UVC_GET_MIN
);
278 ret
= uvc_get_video_ctrl(stream
, &probe_max
, 1, UVC_GET_MAX
);
282 probe
->wCompQuality
= probe_max
.wCompQuality
;
285 for (i
= 0; i
< 2; ++i
) {
286 ret
= uvc_set_video_ctrl(stream
, probe
, 1);
289 ret
= uvc_get_video_ctrl(stream
, probe
, 1, UVC_GET_CUR
);
293 if (stream
->intf
->num_altsetting
== 1)
296 bandwidth
= probe
->dwMaxPayloadTransferSize
;
297 if (bandwidth
<= stream
->maxpsize
)
300 if (stream
->dev
->quirks
& UVC_QUIRK_PROBE_MINMAX
) {
305 /* TODO: negotiate compression parameters */
306 probe
->wKeyFrameRate
= probe_min
.wKeyFrameRate
;
307 probe
->wPFrameRate
= probe_min
.wPFrameRate
;
308 probe
->wCompQuality
= probe_max
.wCompQuality
;
309 probe
->wCompWindowSize
= probe_min
.wCompWindowSize
;
313 mutex_unlock(&stream
->mutex
);
317 int uvc_commit_video(struct uvc_streaming
*stream
,
318 struct uvc_streaming_control
*probe
)
320 return uvc_set_video_ctrl(stream
, probe
, 0);
323 /* ------------------------------------------------------------------------
327 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
328 #define UVC_STREAM_EOH (1 << 7)
329 #define UVC_STREAM_ERR (1 << 6)
330 #define UVC_STREAM_STI (1 << 5)
331 #define UVC_STREAM_RES (1 << 4)
332 #define UVC_STREAM_SCR (1 << 3)
333 #define UVC_STREAM_PTS (1 << 2)
334 #define UVC_STREAM_EOF (1 << 1)
335 #define UVC_STREAM_FID (1 << 0)
337 /* Video payload decoding is handled by uvc_video_decode_start(),
338 * uvc_video_decode_data() and uvc_video_decode_end().
340 * uvc_video_decode_start is called with URB data at the start of a bulk or
341 * isochronous payload. It processes header data and returns the header size
342 * in bytes if successful. If an error occurs, it returns a negative error
343 * code. The following error codes have special meanings.
345 * - EAGAIN informs the caller that the current video buffer should be marked
346 * as done, and that the function should be called again with the same data
347 * and a new video buffer. This is used when end of frame conditions can be
348 * reliably detected at the beginning of the next frame only.
350 * If an error other than -EAGAIN is returned, the caller will drop the current
351 * payload. No call to uvc_video_decode_data and uvc_video_decode_end will be
352 * made until the next payload. -ENODATA can be used to drop the current
353 * payload if no other error code is appropriate.
355 * uvc_video_decode_data is called for every URB with URB data. It copies the
356 * data to the video buffer.
358 * uvc_video_decode_end is called with header data at the end of a bulk or
359 * isochronous payload. It performs any additional header data processing and
360 * returns 0 or a negative error code if an error occured. As header data have
361 * already been processed by uvc_video_decode_start, this functions isn't
362 * required to perform sanity checks a second time.
364 * For isochronous transfers where a payload is always transfered in a single
365 * URB, the three functions will be called in a row.
367 * To let the decoder process header data and update its internal state even
368 * when no video buffer is available, uvc_video_decode_start must be prepared
369 * to be called with a NULL buf parameter. uvc_video_decode_data and
370 * uvc_video_decode_end will never be called with a NULL buffer.
372 static int uvc_video_decode_start(struct uvc_streaming
*stream
,
373 struct uvc_buffer
*buf
, const __u8
*data
, int len
)
378 * - packet must be at least 2 bytes long
379 * - bHeaderLength value must be at least 2 bytes (see above)
380 * - bHeaderLength value can't be larger than the packet size.
382 if (len
< 2 || data
[0] < 2 || data
[0] > len
)
385 /* Skip payloads marked with the error bit ("error frames"). */
386 if (data
[1] & UVC_STREAM_ERR
) {
387 uvc_trace(UVC_TRACE_FRAME
, "Dropping payload (error bit "
392 fid
= data
[1] & UVC_STREAM_FID
;
394 /* Store the payload FID bit and return immediately when the buffer is
398 stream
->last_fid
= fid
;
402 /* Synchronize to the input stream by waiting for the FID bit to be
403 * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE.
404 * stream->last_fid is initialized to -1, so the first isochronous
405 * frame will always be in sync.
407 * If the device doesn't toggle the FID bit, invert stream->last_fid
408 * when the EOF bit is set to force synchronisation on the next packet.
410 if (buf
->state
!= UVC_BUF_STATE_ACTIVE
) {
411 if (fid
== stream
->last_fid
) {
412 uvc_trace(UVC_TRACE_FRAME
, "Dropping payload (out of "
414 if ((stream
->dev
->quirks
& UVC_QUIRK_STREAM_NO_FID
) &&
415 (data
[1] & UVC_STREAM_EOF
))
416 stream
->last_fid
^= UVC_STREAM_FID
;
420 /* TODO: Handle PTS and SCR. */
421 buf
->state
= UVC_BUF_STATE_ACTIVE
;
424 /* Mark the buffer as done if we're at the beginning of a new frame.
425 * End of frame detection is better implemented by checking the EOF
426 * bit (FID bit toggling is delayed by one frame compared to the EOF
427 * bit), but some devices don't set the bit at end of frame (and the
428 * last payload can be lost anyway). We thus must check if the FID has
431 * stream->last_fid is initialized to -1, so the first isochronous
432 * frame will never trigger an end of frame detection.
434 * Empty buffers (bytesused == 0) don't trigger end of frame detection
435 * as it doesn't make sense to return an empty buffer. This also
436 * avoids detecting end of frame conditions at FID toggling if the
437 * previous payload had the EOF bit set.
439 if (fid
!= stream
->last_fid
&& buf
->buf
.bytesused
!= 0) {
440 uvc_trace(UVC_TRACE_FRAME
, "Frame complete (FID bit "
442 buf
->state
= UVC_BUF_STATE_DONE
;
446 stream
->last_fid
= fid
;
451 static void uvc_video_decode_data(struct uvc_streaming
*stream
,
452 struct uvc_buffer
*buf
, const __u8
*data
, int len
)
454 struct uvc_video_queue
*queue
= &stream
->queue
;
455 unsigned int maxlen
, nbytes
;
461 /* Copy the video data to the buffer. */
462 maxlen
= buf
->buf
.length
- buf
->buf
.bytesused
;
463 mem
= queue
->mem
+ buf
->buf
.m
.offset
+ buf
->buf
.bytesused
;
464 nbytes
= min((unsigned int)len
, maxlen
);
465 memcpy(mem
, data
, nbytes
);
466 buf
->buf
.bytesused
+= nbytes
;
468 /* Complete the current frame if the buffer size was exceeded. */
470 uvc_trace(UVC_TRACE_FRAME
, "Frame complete (overflow).\n");
471 buf
->state
= UVC_BUF_STATE_DONE
;
475 static void uvc_video_decode_end(struct uvc_streaming
*stream
,
476 struct uvc_buffer
*buf
, const __u8
*data
, int len
)
478 /* Mark the buffer as done if the EOF marker is set. */
479 if (data
[1] & UVC_STREAM_EOF
&& buf
->buf
.bytesused
!= 0) {
480 uvc_trace(UVC_TRACE_FRAME
, "Frame complete (EOF found).\n");
482 uvc_trace(UVC_TRACE_FRAME
, "EOF in empty payload.\n");
483 buf
->state
= UVC_BUF_STATE_DONE
;
484 if (stream
->dev
->quirks
& UVC_QUIRK_STREAM_NO_FID
)
485 stream
->last_fid
^= UVC_STREAM_FID
;
489 /* Video payload encoding is handled by uvc_video_encode_header() and
490 * uvc_video_encode_data(). Only bulk transfers are currently supported.
492 * uvc_video_encode_header is called at the start of a payload. It adds header
493 * data to the transfer buffer and returns the header size. As the only known
494 * UVC output device transfers a whole frame in a single payload, the EOF bit
495 * is always set in the header.
497 * uvc_video_encode_data is called for every URB and copies the data from the
498 * video buffer to the transfer buffer.
500 static int uvc_video_encode_header(struct uvc_streaming
*stream
,
501 struct uvc_buffer
*buf
, __u8
*data
, int len
)
503 data
[0] = 2; /* Header length */
504 data
[1] = UVC_STREAM_EOH
| UVC_STREAM_EOF
505 | (stream
->last_fid
& UVC_STREAM_FID
);
509 static int uvc_video_encode_data(struct uvc_streaming
*stream
,
510 struct uvc_buffer
*buf
, __u8
*data
, int len
)
512 struct uvc_video_queue
*queue
= &stream
->queue
;
516 /* Copy video data to the URB buffer. */
517 mem
= queue
->mem
+ buf
->buf
.m
.offset
+ queue
->buf_used
;
518 nbytes
= min((unsigned int)len
, buf
->buf
.bytesused
- queue
->buf_used
);
519 nbytes
= min(stream
->bulk
.max_payload_size
- stream
->bulk
.payload_size
,
521 memcpy(data
, mem
, nbytes
);
523 queue
->buf_used
+= nbytes
;
528 /* ------------------------------------------------------------------------
533 * Completion handler for video URBs.
535 static void uvc_video_decode_isoc(struct urb
*urb
, struct uvc_streaming
*stream
,
536 struct uvc_buffer
*buf
)
541 for (i
= 0; i
< urb
->number_of_packets
; ++i
) {
542 if (urb
->iso_frame_desc
[i
].status
< 0) {
543 uvc_trace(UVC_TRACE_FRAME
, "USB isochronous frame "
544 "lost (%d).\n", urb
->iso_frame_desc
[i
].status
);
548 /* Decode the payload header. */
549 mem
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
551 ret
= uvc_video_decode_start(stream
, buf
, mem
,
552 urb
->iso_frame_desc
[i
].actual_length
);
554 buf
= uvc_queue_next_buffer(&stream
->queue
,
556 } while (ret
== -EAGAIN
);
561 /* Decode the payload data. */
562 uvc_video_decode_data(stream
, buf
, mem
+ ret
,
563 urb
->iso_frame_desc
[i
].actual_length
- ret
);
565 /* Process the header again. */
566 uvc_video_decode_end(stream
, buf
, mem
,
567 urb
->iso_frame_desc
[i
].actual_length
);
569 if (buf
->state
== UVC_BUF_STATE_DONE
||
570 buf
->state
== UVC_BUF_STATE_ERROR
)
571 buf
= uvc_queue_next_buffer(&stream
->queue
, buf
);
575 static void uvc_video_decode_bulk(struct urb
*urb
, struct uvc_streaming
*stream
,
576 struct uvc_buffer
*buf
)
581 if (urb
->actual_length
== 0)
584 mem
= urb
->transfer_buffer
;
585 len
= urb
->actual_length
;
586 stream
->bulk
.payload_size
+= len
;
588 /* If the URB is the first of its payload, decode and save the
591 if (stream
->bulk
.header_size
== 0 && !stream
->bulk
.skip_payload
) {
593 ret
= uvc_video_decode_start(stream
, buf
, mem
, len
);
595 buf
= uvc_queue_next_buffer(&stream
->queue
,
597 } while (ret
== -EAGAIN
);
599 /* If an error occured skip the rest of the payload. */
600 if (ret
< 0 || buf
== NULL
) {
601 stream
->bulk
.skip_payload
= 1;
603 memcpy(stream
->bulk
.header
, mem
, ret
);
604 stream
->bulk
.header_size
= ret
;
611 /* The buffer queue might have been cancelled while a bulk transfer
612 * was in progress, so we can reach here with buf equal to NULL. Make
613 * sure buf is never dereferenced if NULL.
616 /* Process video data. */
617 if (!stream
->bulk
.skip_payload
&& buf
!= NULL
)
618 uvc_video_decode_data(stream
, buf
, mem
, len
);
620 /* Detect the payload end by a URB smaller than the maximum size (or
621 * a payload size equal to the maximum) and process the header again.
623 if (urb
->actual_length
< urb
->transfer_buffer_length
||
624 stream
->bulk
.payload_size
>= stream
->bulk
.max_payload_size
) {
625 if (!stream
->bulk
.skip_payload
&& buf
!= NULL
) {
626 uvc_video_decode_end(stream
, buf
, stream
->bulk
.header
,
627 stream
->bulk
.payload_size
);
628 if (buf
->state
== UVC_BUF_STATE_DONE
||
629 buf
->state
== UVC_BUF_STATE_ERROR
)
630 buf
= uvc_queue_next_buffer(&stream
->queue
,
634 stream
->bulk
.header_size
= 0;
635 stream
->bulk
.skip_payload
= 0;
636 stream
->bulk
.payload_size
= 0;
640 static void uvc_video_encode_bulk(struct urb
*urb
, struct uvc_streaming
*stream
,
641 struct uvc_buffer
*buf
)
643 u8
*mem
= urb
->transfer_buffer
;
644 int len
= stream
->urb_size
, ret
;
647 urb
->transfer_buffer_length
= 0;
651 /* If the URB is the first of its payload, add the header. */
652 if (stream
->bulk
.header_size
== 0) {
653 ret
= uvc_video_encode_header(stream
, buf
, mem
, len
);
654 stream
->bulk
.header_size
= ret
;
655 stream
->bulk
.payload_size
+= ret
;
660 /* Process video data. */
661 ret
= uvc_video_encode_data(stream
, buf
, mem
, len
);
663 stream
->bulk
.payload_size
+= ret
;
666 if (buf
->buf
.bytesused
== stream
->queue
.buf_used
||
667 stream
->bulk
.payload_size
== stream
->bulk
.max_payload_size
) {
668 if (buf
->buf
.bytesused
== stream
->queue
.buf_used
) {
669 stream
->queue
.buf_used
= 0;
670 buf
->state
= UVC_BUF_STATE_DONE
;
671 uvc_queue_next_buffer(&stream
->queue
, buf
);
672 stream
->last_fid
^= UVC_STREAM_FID
;
675 stream
->bulk
.header_size
= 0;
676 stream
->bulk
.payload_size
= 0;
679 urb
->transfer_buffer_length
= stream
->urb_size
- len
;
682 static void uvc_video_complete(struct urb
*urb
)
684 struct uvc_streaming
*stream
= urb
->context
;
685 struct uvc_video_queue
*queue
= &stream
->queue
;
686 struct uvc_buffer
*buf
= NULL
;
690 switch (urb
->status
) {
695 uvc_printk(KERN_WARNING
, "Non-zero status (%d) in video "
696 "completion handler.\n", urb
->status
);
698 case -ENOENT
: /* usb_kill_urb() called. */
702 case -ECONNRESET
: /* usb_unlink_urb() called. */
703 case -ESHUTDOWN
: /* The endpoint is being disabled. */
704 uvc_queue_cancel(queue
, urb
->status
== -ESHUTDOWN
);
708 spin_lock_irqsave(&queue
->irqlock
, flags
);
709 if (!list_empty(&queue
->irqqueue
))
710 buf
= list_first_entry(&queue
->irqqueue
, struct uvc_buffer
,
712 spin_unlock_irqrestore(&queue
->irqlock
, flags
);
714 stream
->decode(urb
, stream
, buf
);
716 if ((ret
= usb_submit_urb(urb
, GFP_ATOMIC
)) < 0) {
717 uvc_printk(KERN_ERR
, "Failed to resubmit video URB (%d).\n",
723 * Free transfer buffers.
725 static void uvc_free_urb_buffers(struct uvc_streaming
*stream
)
729 for (i
= 0; i
< UVC_URBS
; ++i
) {
730 if (stream
->urb_buffer
[i
]) {
731 usb_buffer_free(stream
->dev
->udev
, stream
->urb_size
,
732 stream
->urb_buffer
[i
], stream
->urb_dma
[i
]);
733 stream
->urb_buffer
[i
] = NULL
;
737 stream
->urb_size
= 0;
741 * Allocate transfer buffers. This function can be called with buffers
742 * already allocated when resuming from suspend, in which case it will
743 * return without touching the buffers.
745 * Limit the buffer size to UVC_MAX_PACKETS bulk/isochronous packets. If the
746 * system is too low on memory try successively smaller numbers of packets
747 * until allocation succeeds.
749 * Return the number of allocated packets on success or 0 when out of memory.
751 static int uvc_alloc_urb_buffers(struct uvc_streaming
*stream
,
752 unsigned int size
, unsigned int psize
, gfp_t gfp_flags
)
754 unsigned int npackets
;
757 /* Buffers are already allocated, bail out. */
758 if (stream
->urb_size
)
759 return stream
->urb_size
/ psize
;
761 /* Compute the number of packets. Bulk endpoints might transfer UVC
762 * payloads accross multiple URBs.
764 npackets
= DIV_ROUND_UP(size
, psize
);
765 if (npackets
> UVC_MAX_PACKETS
)
766 npackets
= UVC_MAX_PACKETS
;
768 /* Retry allocations until one succeed. */
769 for (; npackets
> 1; npackets
/= 2) {
770 for (i
= 0; i
< UVC_URBS
; ++i
) {
771 stream
->urb_buffer
[i
] = usb_buffer_alloc(
772 stream
->dev
->udev
, psize
* npackets
,
773 gfp_flags
| __GFP_NOWARN
, &stream
->urb_dma
[i
]);
774 if (!stream
->urb_buffer
[i
]) {
775 uvc_free_urb_buffers(stream
);
781 stream
->urb_size
= psize
* npackets
;
790 * Uninitialize isochronous/bulk URBs and free transfer buffers.
792 static void uvc_uninit_video(struct uvc_streaming
*stream
, int free_buffers
)
797 for (i
= 0; i
< UVC_URBS
; ++i
) {
798 urb
= stream
->urb
[i
];
804 stream
->urb
[i
] = NULL
;
808 uvc_free_urb_buffers(stream
);
812 * Initialize isochronous URBs and allocate transfer buffers. The packet size
813 * is given by the endpoint.
815 static int uvc_init_video_isoc(struct uvc_streaming
*stream
,
816 struct usb_host_endpoint
*ep
, gfp_t gfp_flags
)
819 unsigned int npackets
, i
, j
;
823 psize
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
824 psize
= (psize
& 0x07ff) * (1 + ((psize
>> 11) & 3));
825 size
= stream
->ctrl
.dwMaxVideoFrameSize
;
827 npackets
= uvc_alloc_urb_buffers(stream
, size
, psize
, gfp_flags
);
831 size
= npackets
* psize
;
833 for (i
= 0; i
< UVC_URBS
; ++i
) {
834 urb
= usb_alloc_urb(npackets
, gfp_flags
);
836 uvc_uninit_video(stream
, 1);
840 urb
->dev
= stream
->dev
->udev
;
841 urb
->context
= stream
;
842 urb
->pipe
= usb_rcvisocpipe(stream
->dev
->udev
,
843 ep
->desc
.bEndpointAddress
);
844 urb
->transfer_flags
= URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP
;
845 urb
->interval
= ep
->desc
.bInterval
;
846 urb
->transfer_buffer
= stream
->urb_buffer
[i
];
847 urb
->transfer_dma
= stream
->urb_dma
[i
];
848 urb
->complete
= uvc_video_complete
;
849 urb
->number_of_packets
= npackets
;
850 urb
->transfer_buffer_length
= size
;
852 for (j
= 0; j
< npackets
; ++j
) {
853 urb
->iso_frame_desc
[j
].offset
= j
* psize
;
854 urb
->iso_frame_desc
[j
].length
= psize
;
857 stream
->urb
[i
] = urb
;
864 * Initialize bulk URBs and allocate transfer buffers. The packet size is
865 * given by the endpoint.
867 static int uvc_init_video_bulk(struct uvc_streaming
*stream
,
868 struct usb_host_endpoint
*ep
, gfp_t gfp_flags
)
871 unsigned int npackets
, pipe
, i
;
875 psize
= le16_to_cpu(ep
->desc
.wMaxPacketSize
) & 0x07ff;
876 size
= stream
->ctrl
.dwMaxPayloadTransferSize
;
877 stream
->bulk
.max_payload_size
= size
;
879 npackets
= uvc_alloc_urb_buffers(stream
, size
, psize
, gfp_flags
);
883 size
= npackets
* psize
;
885 if (usb_endpoint_dir_in(&ep
->desc
))
886 pipe
= usb_rcvbulkpipe(stream
->dev
->udev
,
887 ep
->desc
.bEndpointAddress
);
889 pipe
= usb_sndbulkpipe(stream
->dev
->udev
,
890 ep
->desc
.bEndpointAddress
);
892 if (stream
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
895 for (i
= 0; i
< UVC_URBS
; ++i
) {
896 urb
= usb_alloc_urb(0, gfp_flags
);
898 uvc_uninit_video(stream
, 1);
902 usb_fill_bulk_urb(urb
, stream
->dev
->udev
, pipe
,
903 stream
->urb_buffer
[i
], size
, uvc_video_complete
,
905 urb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
906 urb
->transfer_dma
= stream
->urb_dma
[i
];
908 stream
->urb
[i
] = urb
;
915 * Initialize isochronous/bulk URBs and allocate transfer buffers.
917 static int uvc_init_video(struct uvc_streaming
*stream
, gfp_t gfp_flags
)
919 struct usb_interface
*intf
= stream
->intf
;
920 struct usb_host_interface
*alts
;
921 struct usb_host_endpoint
*ep
= NULL
;
922 int intfnum
= stream
->intfnum
;
923 unsigned int bandwidth
, psize
, i
;
926 stream
->last_fid
= -1;
927 stream
->bulk
.header_size
= 0;
928 stream
->bulk
.skip_payload
= 0;
929 stream
->bulk
.payload_size
= 0;
931 if (intf
->num_altsetting
> 1) {
932 /* Isochronous endpoint, select the alternate setting. */
933 bandwidth
= stream
->ctrl
.dwMaxPayloadTransferSize
;
935 if (bandwidth
== 0) {
936 uvc_printk(KERN_WARNING
, "device %s requested null "
937 "bandwidth, defaulting to lowest.\n",
942 for (i
= 0; i
< intf
->num_altsetting
; ++i
) {
943 alts
= &intf
->altsetting
[i
];
944 ep
= uvc_find_endpoint(alts
,
945 stream
->header
.bEndpointAddress
);
949 /* Check if the bandwidth is high enough. */
950 psize
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
951 psize
= (psize
& 0x07ff) * (1 + ((psize
>> 11) & 3));
952 if (psize
>= bandwidth
)
956 if (i
>= intf
->num_altsetting
)
959 ret
= usb_set_interface(stream
->dev
->udev
, intfnum
, i
);
963 ret
= uvc_init_video_isoc(stream
, ep
, gfp_flags
);
965 /* Bulk endpoint, proceed to URB initialization. */
966 ep
= uvc_find_endpoint(&intf
->altsetting
[0],
967 stream
->header
.bEndpointAddress
);
971 ret
= uvc_init_video_bulk(stream
, ep
, gfp_flags
);
977 /* Submit the URBs. */
978 for (i
= 0; i
< UVC_URBS
; ++i
) {
979 ret
= usb_submit_urb(stream
->urb
[i
], gfp_flags
);
981 uvc_printk(KERN_ERR
, "Failed to submit URB %u "
983 uvc_uninit_video(stream
, 1);
991 /* --------------------------------------------------------------------------
996 * Stop streaming without disabling the video queue.
998 * To let userspace applications resume without trouble, we must not touch the
999 * video buffers in any way. We mark the device as frozen to make sure the URB
1000 * completion handler won't try to cancel the queue when we kill the URBs.
1002 int uvc_video_suspend(struct uvc_streaming
*stream
)
1004 if (!uvc_queue_streaming(&stream
->queue
))
1008 uvc_uninit_video(stream
, 0);
1009 usb_set_interface(stream
->dev
->udev
, stream
->intfnum
, 0);
1014 * Reconfigure the video interface and restart streaming if it was enabled
1017 * If an error occurs, disable the video queue. This will wake all pending
1018 * buffers, making sure userspace applications are notified of the problem
1019 * instead of waiting forever.
1021 int uvc_video_resume(struct uvc_streaming
*stream
)
1027 ret
= uvc_commit_video(stream
, &stream
->ctrl
);
1029 uvc_queue_enable(&stream
->queue
, 0);
1033 if (!uvc_queue_streaming(&stream
->queue
))
1036 ret
= uvc_init_video(stream
, GFP_NOIO
);
1038 uvc_queue_enable(&stream
->queue
, 0);
1043 /* ------------------------------------------------------------------------
1048 * Initialize the UVC video device by switching to alternate setting 0 and
1049 * retrieve the default format.
1051 * Some cameras (namely the Fuji Finepix) set the format and frame
1052 * indexes to zero. The UVC standard doesn't clearly make this a spec
1053 * violation, so try to silently fix the values if possible.
1055 * This function is called before registering the device with V4L.
1057 int uvc_video_init(struct uvc_streaming
*stream
)
1059 struct uvc_streaming_control
*probe
= &stream
->ctrl
;
1060 struct uvc_format
*format
= NULL
;
1061 struct uvc_frame
*frame
= NULL
;
1065 if (stream
->nformats
== 0) {
1066 uvc_printk(KERN_INFO
, "No supported video formats found.\n");
1070 atomic_set(&stream
->active
, 0);
1072 /* Initialize the video buffers queue. */
1073 uvc_queue_init(&stream
->queue
, stream
->type
);
1075 /* Alternate setting 0 should be the default, yet the XBox Live Vision
1076 * Cam (and possibly other devices) crash or otherwise misbehave if
1077 * they don't receive a SET_INTERFACE request before any other video
1080 usb_set_interface(stream
->dev
->udev
, stream
->intfnum
, 0);
1082 /* Set the streaming probe control with default streaming parameters
1083 * retrieved from the device. Webcams that don't suport GET_DEF
1084 * requests on the probe control will just keep their current streaming
1087 if (uvc_get_video_ctrl(stream
, probe
, 1, UVC_GET_DEF
) == 0)
1088 uvc_set_video_ctrl(stream
, probe
, 1);
1090 /* Initialize the streaming parameters with the probe control current
1091 * value. This makes sure SET_CUR requests on the streaming commit
1092 * control will always use values retrieved from a successful GET_CUR
1093 * request on the probe control, as required by the UVC specification.
1095 ret
= uvc_get_video_ctrl(stream
, probe
, 1, UVC_GET_CUR
);
1099 /* Check if the default format descriptor exists. Use the first
1100 * available format otherwise.
1102 for (i
= stream
->nformats
; i
> 0; --i
) {
1103 format
= &stream
->format
[i
-1];
1104 if (format
->index
== probe
->bFormatIndex
)
1108 if (format
->nframes
== 0) {
1109 uvc_printk(KERN_INFO
, "No frame descriptor found for the "
1110 "default format.\n");
1114 /* Zero bFrameIndex might be correct. Stream-based formats (including
1115 * MPEG-2 TS and DV) do not support frames but have a dummy frame
1116 * descriptor with bFrameIndex set to zero. If the default frame
1117 * descriptor is not found, use the first available frame.
1119 for (i
= format
->nframes
; i
> 0; --i
) {
1120 frame
= &format
->frame
[i
-1];
1121 if (frame
->bFrameIndex
== probe
->bFrameIndex
)
1125 probe
->bFormatIndex
= format
->index
;
1126 probe
->bFrameIndex
= frame
->bFrameIndex
;
1128 stream
->cur_format
= format
;
1129 stream
->cur_frame
= frame
;
1131 /* Select the video decoding function */
1132 if (stream
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1133 if (stream
->dev
->quirks
& UVC_QUIRK_BUILTIN_ISIGHT
)
1134 stream
->decode
= uvc_video_decode_isight
;
1135 else if (stream
->intf
->num_altsetting
> 1)
1136 stream
->decode
= uvc_video_decode_isoc
;
1138 stream
->decode
= uvc_video_decode_bulk
;
1140 if (stream
->intf
->num_altsetting
== 1)
1141 stream
->decode
= uvc_video_encode_bulk
;
1143 uvc_printk(KERN_INFO
, "Isochronous endpoints are not "
1144 "supported for video output devices.\n");
1153 * Enable or disable the video stream.
1155 int uvc_video_enable(struct uvc_streaming
*stream
, int enable
)
1160 uvc_uninit_video(stream
, 1);
1161 usb_set_interface(stream
->dev
->udev
, stream
->intfnum
, 0);
1162 uvc_queue_enable(&stream
->queue
, 0);
1166 if ((stream
->cur_format
->flags
& UVC_FMT_FLAG_COMPRESSED
) ||
1168 stream
->queue
.flags
&= ~UVC_QUEUE_DROP_INCOMPLETE
;
1170 stream
->queue
.flags
|= UVC_QUEUE_DROP_INCOMPLETE
;
1172 ret
= uvc_queue_enable(&stream
->queue
, 1);
1176 /* Commit the streaming parameters. */
1177 ret
= uvc_commit_video(stream
, &stream
->ctrl
);
1181 return uvc_init_video(stream
, GFP_KERNEL
);