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;
41 pipe = (query & 0x80) ? usb_rcvctrlpipe(dev->udev, 0)
42 : usb_sndctrlpipe(dev->udev, 0);
43 type |= (query & 0x80) ? USB_DIR_IN : USB_DIR_OUT;
45 ret = usb_control_msg(dev->udev, pipe, query, type, cs << 8,
46 unit << 8 | intfnum, data, size, timeout);
49 uvc_printk(KERN_ERR, "Failed to query (%u) UVC control %u "
50 "(unit %u) : %d (exp. %u).\n", query, cs, unit, ret,
58 int uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
59 __u8 intfnum, __u8 cs, void *data, __u16 size)
61 return __uvc_query_ctrl(dev, query, unit, intfnum, cs, data, size,
62 UVC_CTRL_CONTROL_TIMEOUT);
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 ret = __uvc_query_ctrl(video->dev, query, 0, video->streaming->intfnum,
99 probe ? VS_PROBE_CONTROL : VS_COMMIT_CONTROL, &data, size,
100 UVC_CTRL_STREAMING_TIMEOUT);
105 ctrl->bmHint = le16_to_cpup((__le16 *)&data[0]);
106 ctrl->bFormatIndex = data[2];
107 ctrl->bFrameIndex = data[3];
108 ctrl->dwFrameInterval = le32_to_cpup((__le32 *)&data[4]);
109 ctrl->wKeyFrameRate = le16_to_cpup((__le16 *)&data[8]);
110 ctrl->wPFrameRate = le16_to_cpup((__le16 *)&data[10]);
111 ctrl->wCompQuality = le16_to_cpup((__le16 *)&data[12]);
112 ctrl->wCompWindowSize = le16_to_cpup((__le16 *)&data[14]);
113 ctrl->wDelay = le16_to_cpup((__le16 *)&data[16]);
114 ctrl->dwMaxVideoFrameSize =
115 le32_to_cpu(get_unaligned((__le32 *)&data[18]));
116 ctrl->dwMaxPayloadTransferSize =
117 le32_to_cpu(get_unaligned((__le32 *)&data[22]));
120 ctrl->dwClockFrequency =
121 le32_to_cpu(get_unaligned((__le32 *)&data[26]));
122 ctrl->bmFramingInfo = data[30];
123 ctrl->bPreferedVersion = data[31];
124 ctrl->bMinVersion = data[32];
125 ctrl->bMaxVersion = data[33];
127 ctrl->dwClockFrequency = video->dev->clock_frequency;
128 ctrl->bmFramingInfo = 0;
129 ctrl->bPreferedVersion = 0;
130 ctrl->bMinVersion = 0;
131 ctrl->bMaxVersion = 0;
134 /* Some broken devices return a null or wrong dwMaxVideoFrameSize.
135 * Try to get the value from the format and frame descriptor.
137 uvc_fixup_buffer_size(video, ctrl);
142 int uvc_set_video_ctrl(struct uvc_video_device *video,
143 struct uvc_streaming_control *ctrl, int probe)
148 size = video->dev->uvc_version >= 0x0110 ? 34 : 26;
149 memset(data, 0, sizeof data);
151 *(__le16 *)&data[0] = cpu_to_le16(ctrl->bmHint);
152 data[2] = ctrl->bFormatIndex;
153 data[3] = ctrl->bFrameIndex;
154 *(__le32 *)&data[4] = cpu_to_le32(ctrl->dwFrameInterval);
155 *(__le16 *)&data[8] = cpu_to_le16(ctrl->wKeyFrameRate);
156 *(__le16 *)&data[10] = cpu_to_le16(ctrl->wPFrameRate);
157 *(__le16 *)&data[12] = cpu_to_le16(ctrl->wCompQuality);
158 *(__le16 *)&data[14] = cpu_to_le16(ctrl->wCompWindowSize);
159 *(__le16 *)&data[16] = cpu_to_le16(ctrl->wDelay);
160 /* Note: Some of the fields below are not required for IN devices (see
161 * UVC spec, 4.3.1.1), but we still copy them in case support for OUT
162 * devices is added in the future. */
163 put_unaligned(cpu_to_le32(ctrl->dwMaxVideoFrameSize),
164 (__le32 *)&data[18]);
165 put_unaligned(cpu_to_le32(ctrl->dwMaxPayloadTransferSize),
166 (__le32 *)&data[22]);
169 put_unaligned(cpu_to_le32(ctrl->dwClockFrequency),
170 (__le32 *)&data[26]);
171 data[30] = ctrl->bmFramingInfo;
172 data[31] = ctrl->bPreferedVersion;
173 data[32] = ctrl->bMinVersion;
174 data[33] = ctrl->bMaxVersion;
177 return __uvc_query_ctrl(video->dev, SET_CUR, 0,
178 video->streaming->intfnum,
179 probe ? VS_PROBE_CONTROL : VS_COMMIT_CONTROL, &data, size,
180 UVC_CTRL_STREAMING_TIMEOUT);
183 int uvc_probe_video(struct uvc_video_device *video,
184 struct uvc_streaming_control *probe)
186 struct uvc_streaming_control probe_min, probe_max;
191 mutex_lock(&video->streaming->mutex);
193 /* Perform probing. The device should adjust the requested values
194 * according to its capabilities. However, some devices, namely the
195 * first generation UVC Logitech webcams, don't implement the Video
196 * Probe control properly, and just return the needed bandwidth. For
197 * that reason, if the needed bandwidth exceeds the maximum available
198 * bandwidth, try to lower the quality.
200 if ((ret = uvc_set_video_ctrl(video, probe, 1)) < 0)
203 /* Get the minimum and maximum values for compression settings. */
204 if (!(video->dev->quirks & UVC_QUIRK_PROBE_MINMAX)) {
205 ret = uvc_get_video_ctrl(video, &probe_min, 1, GET_MIN);
208 ret = uvc_get_video_ctrl(video, &probe_max, 1, GET_MAX);
212 probe->wCompQuality = probe_max.wCompQuality;
215 for (i = 0; i < 2; ++i) {
216 if ((ret = uvc_set_video_ctrl(video, probe, 1)) < 0 ||
217 (ret = uvc_get_video_ctrl(video, probe, 1, GET_CUR)) < 0)
220 if (video->streaming->intf->num_altsetting == 1)
223 bandwidth = probe->dwMaxPayloadTransferSize;
224 if (bandwidth <= video->streaming->maxpsize)
227 if (video->dev->quirks & UVC_QUIRK_PROBE_MINMAX) {
232 /* TODO: negotiate compression parameters */
233 probe->wKeyFrameRate = probe_min.wKeyFrameRate;
234 probe->wPFrameRate = probe_min.wPFrameRate;
235 probe->wCompQuality = probe_max.wCompQuality;
236 probe->wCompWindowSize = probe_min.wCompWindowSize;
240 mutex_unlock(&video->streaming->mutex);
244 /* ------------------------------------------------------------------------
248 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
249 #define UVC_STREAM_EOH (1 << 7)
250 #define UVC_STREAM_ERR (1 << 6)
251 #define UVC_STREAM_STI (1 << 5)
252 #define UVC_STREAM_RES (1 << 4)
253 #define UVC_STREAM_SCR (1 << 3)
254 #define UVC_STREAM_PTS (1 << 2)
255 #define UVC_STREAM_EOF (1 << 1)
256 #define UVC_STREAM_FID (1 << 0)
258 /* Video payload decoding is handled by uvc_video_decode_start(),
259 * uvc_video_decode_data() and uvc_video_decode_end().
261 * uvc_video_decode_start is called with URB data at the start of a bulk or
262 * isochronous payload. It processes header data and returns the header size
263 * in bytes if successful. If an error occurs, it returns a negative error
264 * code. The following error codes have special meanings.
266 * - EAGAIN informs the caller that the current video buffer should be marked
267 * as done, and that the function should be called again with the same data
268 * and a new video buffer. This is used when end of frame conditions can be
269 * reliably detected at the beginning of the next frame only.
271 * If an error other than -EAGAIN is returned, the caller will drop the current
272 * payload. No call to uvc_video_decode_data and uvc_video_decode_end will be
273 * made until the next payload. -ENODATA can be used to drop the current
274 * payload if no other error code is appropriate.
276 * uvc_video_decode_data is called for every URB with URB data. It copies the
277 * data to the video buffer.
279 * uvc_video_decode_end is called with header data at the end of a bulk or
280 * isochronous payload. It performs any additional header data processing and
281 * returns 0 or a negative error code if an error occured. As header data have
282 * already been processed by uvc_video_decode_start, this functions isn't
283 * required to perform sanity checks a second time.
285 * For isochronous transfers where a payload is always transfered in a single
286 * URB, the three functions will be called in a row.
288 * To let the decoder process header data and update its internal state even
289 * when no video buffer is available, uvc_video_decode_start must be prepared
290 * to be called with a NULL buf parameter. uvc_video_decode_data and
291 * uvc_video_decode_end will never be called with a NULL buffer.
293 static int uvc_video_decode_start(struct uvc_video_device *video,
294 struct uvc_buffer *buf, const __u8 *data, int len)
299 * - packet must be at least 2 bytes long
300 * - bHeaderLength value must be at least 2 bytes (see above)
301 * - bHeaderLength value can't be larger than the packet size.
303 if (len < 2 || data[0] < 2 || data[0] > len)
306 /* Skip payloads marked with the error bit ("error frames"). */
307 if (data[1] & UVC_STREAM_ERR) {
308 uvc_trace(UVC_TRACE_FRAME, "Dropping payload (error bit "
313 fid = data[1] & UVC_STREAM_FID;
315 /* Store the payload FID bit and return immediately when the buffer is
319 video->last_fid = fid;
323 /* Synchronize to the input stream by waiting for the FID bit to be
324 * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE.
325 * queue->last_fid is initialized to -1, so the first isochronous
326 * frame will always be in sync.
328 * If the device doesn't toggle the FID bit, invert video->last_fid
329 * when the EOF bit is set to force synchronisation on the next packet.
331 if (buf->state != UVC_BUF_STATE_ACTIVE) {
332 if (fid == video->last_fid) {
333 uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of "
335 if ((video->dev->quirks & UVC_QUIRK_STREAM_NO_FID) &&
336 (data[1] & UVC_STREAM_EOF))
337 video->last_fid ^= UVC_STREAM_FID;
341 /* TODO: Handle PTS and SCR. */
342 buf->state = UVC_BUF_STATE_ACTIVE;
345 /* Mark the buffer as done if we're at the beginning of a new frame.
346 * End of frame detection is better implemented by checking the EOF
347 * bit (FID bit toggling is delayed by one frame compared to the EOF
348 * bit), but some devices don't set the bit at end of frame (and the
349 * last payload can be lost anyway). We thus must check if the FID has
352 * queue->last_fid is initialized to -1, so the first isochronous
353 * frame will never trigger an end of frame detection.
355 * Empty buffers (bytesused == 0) don't trigger end of frame detection
356 * as it doesn't make sense to return an empty buffer. This also
357 * avoids detecting and of frame conditions at FID toggling if the
358 * previous payload had the EOF bit set.
360 if (fid != video->last_fid && buf->buf.bytesused != 0) {
361 uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit "
363 buf->state = UVC_BUF_STATE_DONE;
367 video->last_fid = fid;
372 static void uvc_video_decode_data(struct uvc_video_device *video,
373 struct uvc_buffer *buf, const __u8 *data, int len)
375 struct uvc_video_queue *queue = &video->queue;
376 unsigned int maxlen, nbytes;
382 /* Copy the video data to the buffer. */
383 maxlen = buf->buf.length - buf->buf.bytesused;
384 mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused;
385 nbytes = min((unsigned int)len, maxlen);
386 memcpy(mem, data, nbytes);
387 buf->buf.bytesused += nbytes;
389 /* Complete the current frame if the buffer size was exceeded. */
391 uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n");
392 buf->state = UVC_BUF_STATE_DONE;
396 static void uvc_video_decode_end(struct uvc_video_device *video,
397 struct uvc_buffer *buf, const __u8 *data, int len)
399 /* Mark the buffer as done if the EOF marker is set. */
400 if (data[1] & UVC_STREAM_EOF && buf->buf.bytesused != 0) {
401 uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n");
403 uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n");
404 buf->state = UVC_BUF_STATE_DONE;
405 if (video->dev->quirks & UVC_QUIRK_STREAM_NO_FID)
406 video->last_fid ^= UVC_STREAM_FID;
410 /* ------------------------------------------------------------------------
415 * Completion handler for video URBs.
417 static void uvc_video_decode_isoc(struct urb *urb,
418 struct uvc_video_device *video, struct uvc_buffer *buf)
423 for (i = 0; i < urb->number_of_packets; ++i) {
424 if (urb->iso_frame_desc[i].status < 0) {
425 uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
426 "lost (%d).\n", urb->iso_frame_desc[i].status);
430 /* Decode the payload header. */
431 mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
433 ret = uvc_video_decode_start(video, buf, mem,
434 urb->iso_frame_desc[i].actual_length);
436 buf = uvc_queue_next_buffer(&video->queue, buf);
437 } while (ret == -EAGAIN);
442 /* Decode the payload data. */
443 uvc_video_decode_data(video, buf, mem + ret,
444 urb->iso_frame_desc[i].actual_length - ret);
446 /* Process the header again. */
447 uvc_video_decode_end(video, buf, mem, ret);
449 if (buf->state == UVC_BUF_STATE_DONE ||
450 buf->state == UVC_BUF_STATE_ERROR)
451 buf = uvc_queue_next_buffer(&video->queue, buf);
455 static void uvc_video_decode_bulk(struct urb *urb,
456 struct uvc_video_device *video, struct uvc_buffer *buf)
461 mem = urb->transfer_buffer;
462 len = urb->actual_length;
463 video->bulk.payload_size += len;
465 /* If the URB is the first of its payload, decode and save the
468 if (video->bulk.header_size == 0) {
470 ret = uvc_video_decode_start(video, buf, mem, len);
472 buf = uvc_queue_next_buffer(&video->queue, buf);
473 } while (ret == -EAGAIN);
475 /* If an error occured skip the rest of the payload. */
476 if (ret < 0 || buf == NULL) {
477 video->bulk.skip_payload = 1;
481 video->bulk.header_size = ret;
482 memcpy(video->bulk.header, mem, video->bulk.header_size);
488 /* The buffer queue might have been cancelled while a bulk transfer
489 * was in progress, so we can reach here with buf equal to NULL. Make
490 * sure buf is never dereferenced if NULL.
493 /* Process video data. */
494 if (!video->bulk.skip_payload && buf != NULL)
495 uvc_video_decode_data(video, buf, mem, len);
497 /* Detect the payload end by a URB smaller than the maximum size (or
498 * a payload size equal to the maximum) and process the header again.
500 if (urb->actual_length < urb->transfer_buffer_length ||
501 video->bulk.payload_size >= video->bulk.max_payload_size) {
502 if (!video->bulk.skip_payload && buf != NULL) {
503 uvc_video_decode_end(video, buf, video->bulk.header,
504 video->bulk.header_size);
505 if (buf->state == UVC_BUF_STATE_DONE ||
506 buf->state == UVC_BUF_STATE_ERROR)
507 buf = uvc_queue_next_buffer(&video->queue, buf);
510 video->bulk.header_size = 0;
511 video->bulk.skip_payload = 0;
512 video->bulk.payload_size = 0;
516 static void uvc_video_complete(struct urb *urb)
518 struct uvc_video_device *video = urb->context;
519 struct uvc_video_queue *queue = &video->queue;
520 struct uvc_buffer *buf = NULL;
524 switch (urb->status) {
529 uvc_printk(KERN_WARNING, "Non-zero status (%d) in video "
530 "completion handler.\n", urb->status);
532 case -ENOENT: /* usb_kill_urb() called. */
536 case -ECONNRESET: /* usb_unlink_urb() called. */
537 case -ESHUTDOWN: /* The endpoint is being disabled. */
538 uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
542 spin_lock_irqsave(&queue->irqlock, flags);
543 if (!list_empty(&queue->irqqueue))
544 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
546 spin_unlock_irqrestore(&queue->irqlock, flags);
548 video->decode(urb, video, buf);
550 if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
551 uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n",
557 * Uninitialize isochronous/bulk URBs and free transfer buffers.
559 static void uvc_uninit_video(struct uvc_video_device *video)
564 for (i = 0; i < UVC_URBS; ++i) {
565 if ((urb = video->urb[i]) == NULL)
569 /* urb->transfer_buffer_length is not touched by USB core, so
570 * we can use it here as the buffer length.
572 if (video->urb_buffer[i]) {
573 usb_buffer_free(video->dev->udev,
574 urb->transfer_buffer_length,
575 video->urb_buffer[i], urb->transfer_dma);
576 video->urb_buffer[i] = NULL;
580 video->urb[i] = NULL;
585 * Initialize isochronous URBs and allocate transfer buffers. The packet size
586 * is given by the endpoint.
588 static int uvc_init_video_isoc(struct uvc_video_device *video,
589 struct usb_host_endpoint *ep)
592 unsigned int npackets, i, j;
596 /* Compute the number of isochronous packets to allocate by dividing
597 * the maximum video frame size by the packet size. Limit the result
598 * to UVC_MAX_ISO_PACKETS.
600 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
601 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
603 size = video->streaming->ctrl.dwMaxVideoFrameSize;
604 if (size > UVC_MAX_FRAME_SIZE)
607 npackets = (size + psize - 1) / psize;
608 if (npackets > UVC_MAX_ISO_PACKETS)
609 npackets = UVC_MAX_ISO_PACKETS;
611 size = npackets * psize;
613 for (i = 0; i < UVC_URBS; ++i) {
614 urb = usb_alloc_urb(npackets, GFP_KERNEL);
616 uvc_uninit_video(video);
620 video->urb_buffer[i] = usb_buffer_alloc(video->dev->udev,
621 size, GFP_KERNEL, &urb->transfer_dma);
622 if (video->urb_buffer[i] == NULL) {
624 uvc_uninit_video(video);
628 urb->dev = video->dev->udev;
629 urb->context = video;
630 urb->pipe = usb_rcvisocpipe(video->dev->udev,
631 ep->desc.bEndpointAddress);
632 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
633 urb->interval = ep->desc.bInterval;
634 urb->transfer_buffer = video->urb_buffer[i];
635 urb->complete = uvc_video_complete;
636 urb->number_of_packets = npackets;
637 urb->transfer_buffer_length = size;
639 for (j = 0; j < npackets; ++j) {
640 urb->iso_frame_desc[j].offset = j * psize;
641 urb->iso_frame_desc[j].length = psize;
651 * Initialize bulk URBs and allocate transfer buffers. The packet size is
652 * given by the endpoint.
654 static int uvc_init_video_bulk(struct uvc_video_device *video,
655 struct usb_host_endpoint *ep)
658 unsigned int pipe, i;
662 /* Compute the bulk URB size. Some devices set the maximum payload
663 * size to a value too high for memory-constrained devices. We must
664 * then transfer the payload accross multiple URBs. To be consistant
665 * with isochronous mode, allocate maximum UVC_MAX_ISO_PACKETS per bulk
668 psize = le16_to_cpu(ep->desc.wMaxPacketSize) & 0x07ff;
669 size = video->streaming->ctrl.dwMaxPayloadTransferSize;
670 video->bulk.max_payload_size = size;
671 if (size > psize * UVC_MAX_ISO_PACKETS)
672 size = psize * UVC_MAX_ISO_PACKETS;
674 pipe = usb_rcvbulkpipe(video->dev->udev, ep->desc.bEndpointAddress);
676 for (i = 0; i < UVC_URBS; ++i) {
677 urb = usb_alloc_urb(0, GFP_KERNEL);
679 uvc_uninit_video(video);
683 video->urb_buffer[i] = usb_buffer_alloc(video->dev->udev,
684 size, GFP_KERNEL, &urb->transfer_dma);
685 if (video->urb_buffer[i] == NULL) {
687 uvc_uninit_video(video);
691 usb_fill_bulk_urb(urb, video->dev->udev, pipe,
692 video->urb_buffer[i], size, uvc_video_complete,
694 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
703 * Initialize isochronous/bulk URBs and allocate transfer buffers.
705 static int uvc_init_video(struct uvc_video_device *video)
707 struct usb_interface *intf = video->streaming->intf;
708 struct usb_host_interface *alts;
709 struct usb_host_endpoint *ep = NULL;
710 int intfnum = video->streaming->intfnum;
711 unsigned int bandwidth, psize, i;
714 video->last_fid = -1;
715 video->bulk.header_size = 0;
716 video->bulk.skip_payload = 0;
717 video->bulk.payload_size = 0;
719 if (intf->num_altsetting > 1) {
720 /* Isochronous endpoint, select the alternate setting. */
721 bandwidth = video->streaming->ctrl.dwMaxPayloadTransferSize;
723 if (bandwidth == 0) {
724 uvc_printk(KERN_WARNING, "device %s requested null "
725 "bandwidth, defaulting to lowest.\n",
730 for (i = 0; i < intf->num_altsetting; ++i) {
731 alts = &intf->altsetting[i];
732 ep = uvc_find_endpoint(alts,
733 video->streaming->header.bEndpointAddress);
737 /* Check if the bandwidth is high enough. */
738 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
739 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
740 if (psize >= bandwidth)
744 if (i >= intf->num_altsetting)
747 if ((ret = usb_set_interface(video->dev->udev, intfnum, i)) < 0)
750 ret = uvc_init_video_isoc(video, ep);
752 /* Bulk endpoint, proceed to URB initialization. */
753 ep = uvc_find_endpoint(&intf->altsetting[0],
754 video->streaming->header.bEndpointAddress);
758 ret = uvc_init_video_bulk(video, ep);
764 /* Submit the URBs. */
765 for (i = 0; i < UVC_URBS; ++i) {
766 if ((ret = usb_submit_urb(video->urb[i], GFP_KERNEL)) < 0) {
767 uvc_printk(KERN_ERR, "Failed to submit URB %u "
769 uvc_uninit_video(video);
777 /* --------------------------------------------------------------------------
782 * Stop streaming without disabling the video queue.
784 * To let userspace applications resume without trouble, we must not touch the
785 * video buffers in any way. We mark the device as frozen to make sure the URB
786 * completion handler won't try to cancel the queue when we kill the URBs.
788 int uvc_video_suspend(struct uvc_video_device *video)
790 if (!uvc_queue_streaming(&video->queue))
794 uvc_uninit_video(video);
795 usb_set_interface(video->dev->udev, video->streaming->intfnum, 0);
800 * Reconfigure the video interface and restart streaming if it was enable
803 * If an error occurs, disable the video queue. This will wake all pending
804 * buffers, making sure userspace applications are notified of the problem
805 * instead of waiting forever.
807 int uvc_video_resume(struct uvc_video_device *video)
813 if ((ret = uvc_set_video_ctrl(video, &video->streaming->ctrl, 0)) < 0) {
814 uvc_queue_enable(&video->queue, 0);
818 if (!uvc_queue_streaming(&video->queue))
821 if ((ret = uvc_init_video(video)) < 0)
822 uvc_queue_enable(&video->queue, 0);
827 /* ------------------------------------------------------------------------
832 * Initialize the UVC video device by retrieving the default format and
835 * Some cameras (namely the Fuji Finepix) set the format and frame
836 * indexes to zero. The UVC standard doesn't clearly make this a spec
837 * violation, so try to silently fix the values if possible.
839 * This function is called before registering the device with V4L.
841 int uvc_video_init(struct uvc_video_device *video)
843 struct uvc_streaming_control *probe = &video->streaming->ctrl;
844 struct uvc_format *format = NULL;
845 struct uvc_frame *frame = NULL;
849 if (video->streaming->nformats == 0) {
850 uvc_printk(KERN_INFO, "No supported video formats found.\n");
854 /* Alternate setting 0 should be the default, yet the XBox Live Vision
855 * Cam (and possibly other devices) crash or otherwise misbehave if
856 * they don't receive a SET_INTERFACE request before any other video
859 usb_set_interface(video->dev->udev, video->streaming->intfnum, 0);
861 /* Some webcams don't suport GET_DEF request on the probe control. We
862 * fall back to GET_CUR if GET_DEF fails.
864 if ((ret = uvc_get_video_ctrl(video, probe, 1, GET_DEF)) < 0 &&
865 (ret = uvc_get_video_ctrl(video, probe, 1, GET_CUR)) < 0)
868 /* Check if the default format descriptor exists. Use the first
869 * available format otherwise.
871 for (i = video->streaming->nformats; i > 0; --i) {
872 format = &video->streaming->format[i-1];
873 if (format->index == probe->bFormatIndex)
877 if (format->nframes == 0) {
878 uvc_printk(KERN_INFO, "No frame descriptor found for the "
879 "default format.\n");
883 /* Zero bFrameIndex might be correct. Stream-based formats (including
884 * MPEG-2 TS and DV) do not support frames but have a dummy frame
885 * descriptor with bFrameIndex set to zero. If the default frame
886 * descriptor is not found, use the first avalable frame.
888 for (i = format->nframes; i > 0; --i) {
889 frame = &format->frame[i-1];
890 if (frame->bFrameIndex == probe->bFrameIndex)
894 /* Commit the default settings. */
895 probe->bFormatIndex = format->index;
896 probe->bFrameIndex = frame->bFrameIndex;
897 if ((ret = uvc_set_video_ctrl(video, probe, 0)) < 0)
900 video->streaming->cur_format = format;
901 video->streaming->cur_frame = frame;
902 atomic_set(&video->active, 0);
904 /* Select the video decoding function */
905 if (video->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
906 video->decode = uvc_video_decode_isight;
907 else if (video->streaming->intf->num_altsetting > 1)
908 video->decode = uvc_video_decode_isoc;
910 video->decode = uvc_video_decode_bulk;
916 * Enable or disable the video stream.
918 int uvc_video_enable(struct uvc_video_device *video, int enable)
923 uvc_uninit_video(video);
924 usb_set_interface(video->dev->udev,
925 video->streaming->intfnum, 0);
926 uvc_queue_enable(&video->queue, 0);
930 if ((ret = uvc_queue_enable(&video->queue, 1)) < 0)
933 return uvc_init_video(video);