2 * uvc_video.c -- USB Video Class driver - Video handling
4 * Copyright (C) 2005-2008
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/version.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/usb.h>
19 #include <linux/videodev2.h>
20 #include <linux/vmalloc.h>
21 #include <linux/wait.h>
22 #include <asm/atomic.h>
23 #include <asm/unaligned.h>
25 #include <media/v4l2-common.h>
29 /* ------------------------------------------------------------------------
33 static int __uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
34 __u8 intfnum, __u8 cs, void *data, __u16 size,
37 __u8 type = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
40 pipe = (query & 0x80) ? usb_rcvctrlpipe(dev->udev, 0)
41 : usb_sndctrlpipe(dev->udev, 0);
42 type |= (query & 0x80) ? USB_DIR_IN : USB_DIR_OUT;
44 return usb_control_msg(dev->udev, pipe, query, type, cs << 8,
45 unit << 8 | intfnum, data, size, timeout);
48 int uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
49 __u8 intfnum, __u8 cs, void *data, __u16 size)
53 ret = __uvc_query_ctrl(dev, query, unit, intfnum, cs, data, size,
54 UVC_CTRL_CONTROL_TIMEOUT);
56 uvc_printk(KERN_ERR, "Failed to query (%u) UVC control %u "
57 "(unit %u) : %d (exp. %u).\n", query, cs, unit, ret,
65 static void uvc_fixup_buffer_size(struct uvc_video_device *video,
66 struct uvc_streaming_control *ctrl)
68 struct uvc_format *format;
69 struct uvc_frame *frame;
71 if (ctrl->bFormatIndex <= 0 ||
72 ctrl->bFormatIndex > video->streaming->nformats)
75 format = &video->streaming->format[ctrl->bFormatIndex - 1];
77 if (ctrl->bFrameIndex <= 0 ||
78 ctrl->bFrameIndex > format->nframes)
81 frame = &format->frame[ctrl->bFrameIndex - 1];
83 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) ||
84 (ctrl->dwMaxVideoFrameSize == 0 &&
85 video->dev->uvc_version < 0x0110))
86 ctrl->dwMaxVideoFrameSize =
87 frame->dwMaxVideoFrameBufferSize;
90 static int uvc_get_video_ctrl(struct uvc_video_device *video,
91 struct uvc_streaming_control *ctrl, int probe, __u8 query)
97 size = video->dev->uvc_version >= 0x0110 ? 34 : 26;
98 data = kmalloc(size, GFP_KERNEL);
102 ret = __uvc_query_ctrl(video->dev, query, 0, video->streaming->intfnum,
103 probe ? VS_PROBE_CONTROL : VS_COMMIT_CONTROL, data, size,
104 UVC_CTRL_STREAMING_TIMEOUT);
106 if ((query == GET_MIN || query == GET_MAX) && ret == 2) {
107 /* Some cameras, mostly based on Bison Electronics chipsets,
108 * answer a GET_MIN or GET_MAX request with the wCompQuality
111 uvc_warn_once(video->dev, UVC_WARN_MINMAX, "UVC non "
112 "compliance - GET_MIN/MAX(PROBE) incorrectly "
113 "supported. Enabling workaround.\n");
114 memset(ctrl, 0, sizeof ctrl);
115 ctrl->wCompQuality = le16_to_cpup((__le16 *)data);
118 } else if (query == GET_DEF && probe == 1) {
119 /* Many cameras don't support the GET_DEF request on their
120 * video probe control. Warn once and return, the caller will
121 * fall back to GET_CUR.
123 uvc_warn_once(video->dev, UVC_WARN_PROBE_DEF, "UVC non "
124 "compliance - GET_DEF(PROBE) not supported. "
125 "Enabling workaround.\n");
128 } else if (ret != size) {
129 uvc_printk(KERN_ERR, "Failed to query (%u) UVC %s control : "
130 "%d (exp. %u).\n", query, probe ? "probe" : "commit",
136 ctrl->bmHint = le16_to_cpup((__le16 *)&data[0]);
137 ctrl->bFormatIndex = data[2];
138 ctrl->bFrameIndex = data[3];
139 ctrl->dwFrameInterval = le32_to_cpup((__le32 *)&data[4]);
140 ctrl->wKeyFrameRate = le16_to_cpup((__le16 *)&data[8]);
141 ctrl->wPFrameRate = le16_to_cpup((__le16 *)&data[10]);
142 ctrl->wCompQuality = le16_to_cpup((__le16 *)&data[12]);
143 ctrl->wCompWindowSize = le16_to_cpup((__le16 *)&data[14]);
144 ctrl->wDelay = le16_to_cpup((__le16 *)&data[16]);
145 ctrl->dwMaxVideoFrameSize = get_unaligned_le32(&data[18]);
146 ctrl->dwMaxPayloadTransferSize = get_unaligned_le32(&data[22]);
149 ctrl->dwClockFrequency = get_unaligned_le32(&data[26]);
150 ctrl->bmFramingInfo = data[30];
151 ctrl->bPreferedVersion = data[31];
152 ctrl->bMinVersion = data[32];
153 ctrl->bMaxVersion = data[33];
155 ctrl->dwClockFrequency = video->dev->clock_frequency;
156 ctrl->bmFramingInfo = 0;
157 ctrl->bPreferedVersion = 0;
158 ctrl->bMinVersion = 0;
159 ctrl->bMaxVersion = 0;
162 /* Some broken devices return a null or wrong dwMaxVideoFrameSize.
163 * Try to get the value from the format and frame descriptor.
165 uvc_fixup_buffer_size(video, ctrl);
173 static int uvc_set_video_ctrl(struct uvc_video_device *video,
174 struct uvc_streaming_control *ctrl, int probe)
180 size = video->dev->uvc_version >= 0x0110 ? 34 : 26;
181 data = kzalloc(size, GFP_KERNEL);
185 *(__le16 *)&data[0] = cpu_to_le16(ctrl->bmHint);
186 data[2] = ctrl->bFormatIndex;
187 data[3] = ctrl->bFrameIndex;
188 *(__le32 *)&data[4] = cpu_to_le32(ctrl->dwFrameInterval);
189 *(__le16 *)&data[8] = cpu_to_le16(ctrl->wKeyFrameRate);
190 *(__le16 *)&data[10] = cpu_to_le16(ctrl->wPFrameRate);
191 *(__le16 *)&data[12] = cpu_to_le16(ctrl->wCompQuality);
192 *(__le16 *)&data[14] = cpu_to_le16(ctrl->wCompWindowSize);
193 *(__le16 *)&data[16] = cpu_to_le16(ctrl->wDelay);
194 /* Note: Some of the fields below are not required for IN devices (see
195 * UVC spec, 4.3.1.1), but we still copy them in case support for OUT
196 * devices is added in the future. */
197 put_unaligned_le32(ctrl->dwMaxVideoFrameSize, &data[18]);
198 put_unaligned_le32(ctrl->dwMaxPayloadTransferSize, &data[22]);
201 put_unaligned_le32(ctrl->dwClockFrequency, &data[26]);
202 data[30] = ctrl->bmFramingInfo;
203 data[31] = ctrl->bPreferedVersion;
204 data[32] = ctrl->bMinVersion;
205 data[33] = ctrl->bMaxVersion;
208 ret = __uvc_query_ctrl(video->dev, SET_CUR, 0,
209 video->streaming->intfnum,
210 probe ? VS_PROBE_CONTROL : VS_COMMIT_CONTROL, data, size,
211 UVC_CTRL_STREAMING_TIMEOUT);
213 uvc_printk(KERN_ERR, "Failed to set UVC %s control : "
214 "%d (exp. %u).\n", probe ? "probe" : "commit",
223 int uvc_probe_video(struct uvc_video_device *video,
224 struct uvc_streaming_control *probe)
226 struct uvc_streaming_control probe_min, probe_max;
231 mutex_lock(&video->streaming->mutex);
233 /* Perform probing. The device should adjust the requested values
234 * according to its capabilities. However, some devices, namely the
235 * first generation UVC Logitech webcams, don't implement the Video
236 * Probe control properly, and just return the needed bandwidth. For
237 * that reason, if the needed bandwidth exceeds the maximum available
238 * bandwidth, try to lower the quality.
240 if ((ret = uvc_set_video_ctrl(video, probe, 1)) < 0)
243 /* Get the minimum and maximum values for compression settings. */
244 if (!(video->dev->quirks & UVC_QUIRK_PROBE_MINMAX)) {
245 ret = uvc_get_video_ctrl(video, &probe_min, 1, GET_MIN);
248 ret = uvc_get_video_ctrl(video, &probe_max, 1, GET_MAX);
252 probe->wCompQuality = probe_max.wCompQuality;
255 for (i = 0; i < 2; ++i) {
256 if ((ret = uvc_set_video_ctrl(video, probe, 1)) < 0 ||
257 (ret = uvc_get_video_ctrl(video, probe, 1, GET_CUR)) < 0)
260 if (video->streaming->intf->num_altsetting == 1)
263 bandwidth = probe->dwMaxPayloadTransferSize;
264 if (bandwidth <= video->streaming->maxpsize)
267 if (video->dev->quirks & UVC_QUIRK_PROBE_MINMAX) {
272 /* TODO: negotiate compression parameters */
273 probe->wKeyFrameRate = probe_min.wKeyFrameRate;
274 probe->wPFrameRate = probe_min.wPFrameRate;
275 probe->wCompQuality = probe_max.wCompQuality;
276 probe->wCompWindowSize = probe_min.wCompWindowSize;
280 mutex_unlock(&video->streaming->mutex);
284 int uvc_commit_video(struct uvc_video_device *video,
285 struct uvc_streaming_control *probe)
287 return uvc_set_video_ctrl(video, probe, 0);
290 /* ------------------------------------------------------------------------
294 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
295 #define UVC_STREAM_EOH (1 << 7)
296 #define UVC_STREAM_ERR (1 << 6)
297 #define UVC_STREAM_STI (1 << 5)
298 #define UVC_STREAM_RES (1 << 4)
299 #define UVC_STREAM_SCR (1 << 3)
300 #define UVC_STREAM_PTS (1 << 2)
301 #define UVC_STREAM_EOF (1 << 1)
302 #define UVC_STREAM_FID (1 << 0)
304 /* Video payload decoding is handled by uvc_video_decode_start(),
305 * uvc_video_decode_data() and uvc_video_decode_end().
307 * uvc_video_decode_start is called with URB data at the start of a bulk or
308 * isochronous payload. It processes header data and returns the header size
309 * in bytes if successful. If an error occurs, it returns a negative error
310 * code. The following error codes have special meanings.
312 * - EAGAIN informs the caller that the current video buffer should be marked
313 * as done, and that the function should be called again with the same data
314 * and a new video buffer. This is used when end of frame conditions can be
315 * reliably detected at the beginning of the next frame only.
317 * If an error other than -EAGAIN is returned, the caller will drop the current
318 * payload. No call to uvc_video_decode_data and uvc_video_decode_end will be
319 * made until the next payload. -ENODATA can be used to drop the current
320 * payload if no other error code is appropriate.
322 * uvc_video_decode_data is called for every URB with URB data. It copies the
323 * data to the video buffer.
325 * uvc_video_decode_end is called with header data at the end of a bulk or
326 * isochronous payload. It performs any additional header data processing and
327 * returns 0 or a negative error code if an error occured. As header data have
328 * already been processed by uvc_video_decode_start, this functions isn't
329 * required to perform sanity checks a second time.
331 * For isochronous transfers where a payload is always transfered in a single
332 * URB, the three functions will be called in a row.
334 * To let the decoder process header data and update its internal state even
335 * when no video buffer is available, uvc_video_decode_start must be prepared
336 * to be called with a NULL buf parameter. uvc_video_decode_data and
337 * uvc_video_decode_end will never be called with a NULL buffer.
339 static int uvc_video_decode_start(struct uvc_video_device *video,
340 struct uvc_buffer *buf, const __u8 *data, int len)
345 * - packet must be at least 2 bytes long
346 * - bHeaderLength value must be at least 2 bytes (see above)
347 * - bHeaderLength value can't be larger than the packet size.
349 if (len < 2 || data[0] < 2 || data[0] > len)
352 /* Skip payloads marked with the error bit ("error frames"). */
353 if (data[1] & UVC_STREAM_ERR) {
354 uvc_trace(UVC_TRACE_FRAME, "Dropping payload (error bit "
359 fid = data[1] & UVC_STREAM_FID;
361 /* Store the payload FID bit and return immediately when the buffer is
365 video->last_fid = fid;
369 /* Synchronize to the input stream by waiting for the FID bit to be
370 * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE.
371 * video->last_fid is initialized to -1, so the first isochronous
372 * frame will always be in sync.
374 * If the device doesn't toggle the FID bit, invert video->last_fid
375 * when the EOF bit is set to force synchronisation on the next packet.
377 if (buf->state != UVC_BUF_STATE_ACTIVE) {
378 if (fid == video->last_fid) {
379 uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of "
381 if ((video->dev->quirks & UVC_QUIRK_STREAM_NO_FID) &&
382 (data[1] & UVC_STREAM_EOF))
383 video->last_fid ^= UVC_STREAM_FID;
387 /* TODO: Handle PTS and SCR. */
388 buf->state = UVC_BUF_STATE_ACTIVE;
391 /* Mark the buffer as done if we're at the beginning of a new frame.
392 * End of frame detection is better implemented by checking the EOF
393 * bit (FID bit toggling is delayed by one frame compared to the EOF
394 * bit), but some devices don't set the bit at end of frame (and the
395 * last payload can be lost anyway). We thus must check if the FID has
398 * video->last_fid is initialized to -1, so the first isochronous
399 * frame will never trigger an end of frame detection.
401 * Empty buffers (bytesused == 0) don't trigger end of frame detection
402 * as it doesn't make sense to return an empty buffer. This also
403 * avoids detecting and of frame conditions at FID toggling if the
404 * previous payload had the EOF bit set.
406 if (fid != video->last_fid && buf->buf.bytesused != 0) {
407 uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit "
409 buf->state = UVC_BUF_STATE_DONE;
413 video->last_fid = fid;
418 static void uvc_video_decode_data(struct uvc_video_device *video,
419 struct uvc_buffer *buf, const __u8 *data, int len)
421 struct uvc_video_queue *queue = &video->queue;
422 unsigned int maxlen, nbytes;
428 /* Copy the video data to the buffer. */
429 maxlen = buf->buf.length - buf->buf.bytesused;
430 mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused;
431 nbytes = min((unsigned int)len, maxlen);
432 memcpy(mem, data, nbytes);
433 buf->buf.bytesused += nbytes;
435 /* Complete the current frame if the buffer size was exceeded. */
437 uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n");
438 buf->state = UVC_BUF_STATE_DONE;
442 static void uvc_video_decode_end(struct uvc_video_device *video,
443 struct uvc_buffer *buf, const __u8 *data, int len)
445 /* Mark the buffer as done if the EOF marker is set. */
446 if (data[1] & UVC_STREAM_EOF && buf->buf.bytesused != 0) {
447 uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n");
449 uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n");
450 buf->state = UVC_BUF_STATE_DONE;
451 if (video->dev->quirks & UVC_QUIRK_STREAM_NO_FID)
452 video->last_fid ^= UVC_STREAM_FID;
456 /* ------------------------------------------------------------------------
461 * Completion handler for video URBs.
463 static void uvc_video_decode_isoc(struct urb *urb,
464 struct uvc_video_device *video, struct uvc_buffer *buf)
469 for (i = 0; i < urb->number_of_packets; ++i) {
470 if (urb->iso_frame_desc[i].status < 0) {
471 uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
472 "lost (%d).\n", urb->iso_frame_desc[i].status);
476 /* Decode the payload header. */
477 mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
479 ret = uvc_video_decode_start(video, buf, mem,
480 urb->iso_frame_desc[i].actual_length);
482 buf = uvc_queue_next_buffer(&video->queue, buf);
483 } while (ret == -EAGAIN);
488 /* Decode the payload data. */
489 uvc_video_decode_data(video, buf, mem + ret,
490 urb->iso_frame_desc[i].actual_length - ret);
492 /* Process the header again. */
493 uvc_video_decode_end(video, buf, mem,
494 urb->iso_frame_desc[i].actual_length);
496 if (buf->state == UVC_BUF_STATE_DONE ||
497 buf->state == UVC_BUF_STATE_ERROR)
498 buf = uvc_queue_next_buffer(&video->queue, buf);
502 static void uvc_video_decode_bulk(struct urb *urb,
503 struct uvc_video_device *video, struct uvc_buffer *buf)
508 mem = urb->transfer_buffer;
509 len = urb->actual_length;
510 video->bulk.payload_size += len;
512 /* If the URB is the first of its payload, decode and save the
515 if (video->bulk.header_size == 0 && !video->bulk.skip_payload) {
517 ret = uvc_video_decode_start(video, buf, mem, len);
519 buf = uvc_queue_next_buffer(&video->queue, buf);
520 } while (ret == -EAGAIN);
522 /* If an error occured skip the rest of the payload. */
523 if (ret < 0 || buf == NULL) {
524 video->bulk.skip_payload = 1;
526 memcpy(video->bulk.header, mem, ret);
527 video->bulk.header_size = ret;
534 /* The buffer queue might have been cancelled while a bulk transfer
535 * was in progress, so we can reach here with buf equal to NULL. Make
536 * sure buf is never dereferenced if NULL.
539 /* Process video data. */
540 if (!video->bulk.skip_payload && buf != NULL)
541 uvc_video_decode_data(video, buf, mem, len);
543 /* Detect the payload end by a URB smaller than the maximum size (or
544 * a payload size equal to the maximum) and process the header again.
546 if (urb->actual_length < urb->transfer_buffer_length ||
547 video->bulk.payload_size >= video->bulk.max_payload_size) {
548 if (!video->bulk.skip_payload && buf != NULL) {
549 uvc_video_decode_end(video, buf, video->bulk.header,
550 video->bulk.payload_size);
551 if (buf->state == UVC_BUF_STATE_DONE ||
552 buf->state == UVC_BUF_STATE_ERROR)
553 buf = uvc_queue_next_buffer(&video->queue, buf);
556 video->bulk.header_size = 0;
557 video->bulk.skip_payload = 0;
558 video->bulk.payload_size = 0;
562 static void uvc_video_complete(struct urb *urb)
564 struct uvc_video_device *video = urb->context;
565 struct uvc_video_queue *queue = &video->queue;
566 struct uvc_buffer *buf = NULL;
570 switch (urb->status) {
575 uvc_printk(KERN_WARNING, "Non-zero status (%d) in video "
576 "completion handler.\n", urb->status);
578 case -ENOENT: /* usb_kill_urb() called. */
582 case -ECONNRESET: /* usb_unlink_urb() called. */
583 case -ESHUTDOWN: /* The endpoint is being disabled. */
584 uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
588 spin_lock_irqsave(&queue->irqlock, flags);
589 if (!list_empty(&queue->irqqueue))
590 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
592 spin_unlock_irqrestore(&queue->irqlock, flags);
594 video->decode(urb, video, buf);
596 if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
597 uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n",
603 * Free transfer buffers.
605 static void uvc_free_urb_buffers(struct uvc_video_device *video)
609 for (i = 0; i < UVC_URBS; ++i) {
610 if (video->urb_buffer[i]) {
611 usb_buffer_free(video->dev->udev, video->urb_size,
612 video->urb_buffer[i], video->urb_dma[i]);
613 video->urb_buffer[i] = NULL;
621 * Allocate transfer buffers. This function can be called with buffers
622 * already allocated when resuming from suspend, in which case it will
623 * return without touching the buffers.
625 * Return 0 on success or -ENOMEM when out of memory.
627 static int uvc_alloc_urb_buffers(struct uvc_video_device *video,
632 /* Buffers are already allocated, bail out. */
636 for (i = 0; i < UVC_URBS; ++i) {
637 video->urb_buffer[i] = usb_buffer_alloc(video->dev->udev,
638 size, GFP_KERNEL, &video->urb_dma[i]);
639 if (video->urb_buffer[i] == NULL) {
640 uvc_free_urb_buffers(video);
645 video->urb_size = size;
650 * Uninitialize isochronous/bulk URBs and free transfer buffers.
652 static void uvc_uninit_video(struct uvc_video_device *video, int free_buffers)
657 for (i = 0; i < UVC_URBS; ++i) {
658 if ((urb = video->urb[i]) == NULL)
663 video->urb[i] = NULL;
667 uvc_free_urb_buffers(video);
671 * Initialize isochronous URBs and allocate transfer buffers. The packet size
672 * is given by the endpoint.
674 static int uvc_init_video_isoc(struct uvc_video_device *video,
675 struct usb_host_endpoint *ep, gfp_t gfp_flags)
678 unsigned int npackets, i, j;
682 /* Compute the number of isochronous packets to allocate by dividing
683 * the maximum video frame size by the packet size. Limit the result
684 * to UVC_MAX_ISO_PACKETS.
686 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
687 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
689 size = video->streaming->ctrl.dwMaxVideoFrameSize;
690 if (size > UVC_MAX_FRAME_SIZE)
693 npackets = DIV_ROUND_UP(size, psize);
694 if (npackets > UVC_MAX_ISO_PACKETS)
695 npackets = UVC_MAX_ISO_PACKETS;
697 size = npackets * psize;
699 if (uvc_alloc_urb_buffers(video, size) < 0)
702 for (i = 0; i < UVC_URBS; ++i) {
703 urb = usb_alloc_urb(npackets, gfp_flags);
705 uvc_uninit_video(video, 1);
709 urb->dev = video->dev->udev;
710 urb->context = video;
711 urb->pipe = usb_rcvisocpipe(video->dev->udev,
712 ep->desc.bEndpointAddress);
713 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
714 urb->interval = ep->desc.bInterval;
715 urb->transfer_buffer = video->urb_buffer[i];
716 urb->transfer_dma = video->urb_dma[i];
717 urb->complete = uvc_video_complete;
718 urb->number_of_packets = npackets;
719 urb->transfer_buffer_length = size;
721 for (j = 0; j < npackets; ++j) {
722 urb->iso_frame_desc[j].offset = j * psize;
723 urb->iso_frame_desc[j].length = psize;
733 * Initialize bulk URBs and allocate transfer buffers. The packet size is
734 * given by the endpoint.
736 static int uvc_init_video_bulk(struct uvc_video_device *video,
737 struct usb_host_endpoint *ep, gfp_t gfp_flags)
740 unsigned int pipe, i;
744 /* Compute the bulk URB size. Some devices set the maximum payload
745 * size to a value too high for memory-constrained devices. We must
746 * then transfer the payload accross multiple URBs. To be consistant
747 * with isochronous mode, allocate maximum UVC_MAX_ISO_PACKETS per bulk
750 psize = le16_to_cpu(ep->desc.wMaxPacketSize) & 0x07ff;
751 size = video->streaming->ctrl.dwMaxPayloadTransferSize;
752 video->bulk.max_payload_size = size;
753 if (size > psize * UVC_MAX_ISO_PACKETS)
754 size = psize * UVC_MAX_ISO_PACKETS;
756 if (uvc_alloc_urb_buffers(video, size) < 0)
759 pipe = usb_rcvbulkpipe(video->dev->udev, ep->desc.bEndpointAddress);
761 for (i = 0; i < UVC_URBS; ++i) {
762 urb = usb_alloc_urb(0, gfp_flags);
764 uvc_uninit_video(video, 1);
768 usb_fill_bulk_urb(urb, video->dev->udev, pipe,
769 video->urb_buffer[i], size, uvc_video_complete,
771 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
772 urb->transfer_dma = video->urb_dma[i];
781 * Initialize isochronous/bulk URBs and allocate transfer buffers.
783 static int uvc_init_video(struct uvc_video_device *video, gfp_t gfp_flags)
785 struct usb_interface *intf = video->streaming->intf;
786 struct usb_host_interface *alts;
787 struct usb_host_endpoint *ep = NULL;
788 int intfnum = video->streaming->intfnum;
789 unsigned int bandwidth, psize, i;
792 video->last_fid = -1;
793 video->bulk.header_size = 0;
794 video->bulk.skip_payload = 0;
795 video->bulk.payload_size = 0;
797 if (intf->num_altsetting > 1) {
798 /* Isochronous endpoint, select the alternate setting. */
799 bandwidth = video->streaming->ctrl.dwMaxPayloadTransferSize;
801 if (bandwidth == 0) {
802 uvc_printk(KERN_WARNING, "device %s requested null "
803 "bandwidth, defaulting to lowest.\n",
808 for (i = 0; i < intf->num_altsetting; ++i) {
809 alts = &intf->altsetting[i];
810 ep = uvc_find_endpoint(alts,
811 video->streaming->header.bEndpointAddress);
815 /* Check if the bandwidth is high enough. */
816 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
817 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
818 if (psize >= bandwidth)
822 if (i >= intf->num_altsetting)
825 if ((ret = usb_set_interface(video->dev->udev, intfnum, i)) < 0)
828 ret = uvc_init_video_isoc(video, ep, gfp_flags);
830 /* Bulk endpoint, proceed to URB initialization. */
831 ep = uvc_find_endpoint(&intf->altsetting[0],
832 video->streaming->header.bEndpointAddress);
836 ret = uvc_init_video_bulk(video, ep, gfp_flags);
842 /* Submit the URBs. */
843 for (i = 0; i < UVC_URBS; ++i) {
844 if ((ret = usb_submit_urb(video->urb[i], gfp_flags)) < 0) {
845 uvc_printk(KERN_ERR, "Failed to submit URB %u "
847 uvc_uninit_video(video, 1);
855 /* --------------------------------------------------------------------------
860 * Stop streaming without disabling the video queue.
862 * To let userspace applications resume without trouble, we must not touch the
863 * video buffers in any way. We mark the device as frozen to make sure the URB
864 * completion handler won't try to cancel the queue when we kill the URBs.
866 int uvc_video_suspend(struct uvc_video_device *video)
868 if (!uvc_queue_streaming(&video->queue))
872 uvc_uninit_video(video, 0);
873 usb_set_interface(video->dev->udev, video->streaming->intfnum, 0);
878 * Reconfigure the video interface and restart streaming if it was enable
881 * If an error occurs, disable the video queue. This will wake all pending
882 * buffers, making sure userspace applications are notified of the problem
883 * instead of waiting forever.
885 int uvc_video_resume(struct uvc_video_device *video)
891 if ((ret = uvc_commit_video(video, &video->streaming->ctrl)) < 0) {
892 uvc_queue_enable(&video->queue, 0);
896 if (!uvc_queue_streaming(&video->queue))
899 if ((ret = uvc_init_video(video, GFP_NOIO)) < 0)
900 uvc_queue_enable(&video->queue, 0);
905 /* ------------------------------------------------------------------------
910 * Initialize the UVC video device by retrieving the default format and
913 * Some cameras (namely the Fuji Finepix) set the format and frame
914 * indexes to zero. The UVC standard doesn't clearly make this a spec
915 * violation, so try to silently fix the values if possible.
917 * This function is called before registering the device with V4L.
919 int uvc_video_init(struct uvc_video_device *video)
921 struct uvc_streaming_control *probe = &video->streaming->ctrl;
922 struct uvc_format *format = NULL;
923 struct uvc_frame *frame = NULL;
927 if (video->streaming->nformats == 0) {
928 uvc_printk(KERN_INFO, "No supported video formats found.\n");
932 /* Alternate setting 0 should be the default, yet the XBox Live Vision
933 * Cam (and possibly other devices) crash or otherwise misbehave if
934 * they don't receive a SET_INTERFACE request before any other video
937 usb_set_interface(video->dev->udev, video->streaming->intfnum, 0);
939 /* Some webcams don't suport GET_DEF request on the probe control. We
940 * fall back to GET_CUR if GET_DEF fails.
942 if ((ret = uvc_get_video_ctrl(video, probe, 1, GET_DEF)) < 0 &&
943 (ret = uvc_get_video_ctrl(video, probe, 1, GET_CUR)) < 0)
946 /* Check if the default format descriptor exists. Use the first
947 * available format otherwise.
949 for (i = video->streaming->nformats; i > 0; --i) {
950 format = &video->streaming->format[i-1];
951 if (format->index == probe->bFormatIndex)
955 if (format->nframes == 0) {
956 uvc_printk(KERN_INFO, "No frame descriptor found for the "
957 "default format.\n");
961 /* Zero bFrameIndex might be correct. Stream-based formats (including
962 * MPEG-2 TS and DV) do not support frames but have a dummy frame
963 * descriptor with bFrameIndex set to zero. If the default frame
964 * descriptor is not found, use the first avalable frame.
966 for (i = format->nframes; i > 0; --i) {
967 frame = &format->frame[i-1];
968 if (frame->bFrameIndex == probe->bFrameIndex)
972 probe->bFormatIndex = format->index;
973 probe->bFrameIndex = frame->bFrameIndex;
975 video->streaming->cur_format = format;
976 video->streaming->cur_frame = frame;
977 atomic_set(&video->active, 0);
979 /* Select the video decoding function */
980 if (video->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
981 video->decode = uvc_video_decode_isight;
982 else if (video->streaming->intf->num_altsetting > 1)
983 video->decode = uvc_video_decode_isoc;
985 video->decode = uvc_video_decode_bulk;
991 * Enable or disable the video stream.
993 int uvc_video_enable(struct uvc_video_device *video, int enable)
998 uvc_uninit_video(video, 1);
999 usb_set_interface(video->dev->udev,
1000 video->streaming->intfnum, 0);
1001 uvc_queue_enable(&video->queue, 0);
1005 if ((video->streaming->cur_format->flags & UVC_FMT_FLAG_COMPRESSED) ||
1007 video->queue.flags &= ~UVC_QUEUE_DROP_INCOMPLETE;
1009 video->queue.flags |= UVC_QUEUE_DROP_INCOMPLETE;
1011 if ((ret = uvc_queue_enable(&video->queue, 1)) < 0)
1014 /* Commit the streaming parameters. */
1015 if ((ret = uvc_commit_video(video, &video->streaming->ctrl)) < 0)
1018 return uvc_init_video(video, GFP_KERNEL);