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 * Free transfer buffers.
559 static void uvc_free_urb_buffers(struct uvc_video_device *video)
563 for (i = 0; i < UVC_URBS; ++i) {
564 if (video->urb_buffer[i]) {
565 usb_buffer_free(video->dev->udev, video->urb_size,
566 video->urb_buffer[i], video->urb_dma[i]);
567 video->urb_buffer[i] = NULL;
575 * Allocate transfer buffers. This function can be called with buffers
576 * already allocated when resuming from suspend, in which case it will
577 * return without touching the buffers.
579 * Return 0 on success or -ENOMEM when out of memory.
581 static int uvc_alloc_urb_buffers(struct uvc_video_device *video,
586 /* Buffers are already allocated, bail out. */
590 for (i = 0; i < UVC_URBS; ++i) {
591 video->urb_buffer[i] = usb_buffer_alloc(video->dev->udev,
592 size, GFP_KERNEL, &video->urb_dma[i]);
593 if (video->urb_buffer[i] == NULL) {
594 uvc_free_urb_buffers(video);
599 video->urb_size = size;
604 * Uninitialize isochronous/bulk URBs and free transfer buffers.
606 static void uvc_uninit_video(struct uvc_video_device *video, int free_buffers)
611 for (i = 0; i < UVC_URBS; ++i) {
612 if ((urb = video->urb[i]) == NULL)
617 video->urb[i] = NULL;
621 uvc_free_urb_buffers(video);
625 * Initialize isochronous URBs and allocate transfer buffers. The packet size
626 * is given by the endpoint.
628 static int uvc_init_video_isoc(struct uvc_video_device *video,
629 struct usb_host_endpoint *ep, gfp_t gfp_flags)
632 unsigned int npackets, i, j;
636 /* Compute the number of isochronous packets to allocate by dividing
637 * the maximum video frame size by the packet size. Limit the result
638 * to UVC_MAX_ISO_PACKETS.
640 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
641 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
643 size = video->streaming->ctrl.dwMaxVideoFrameSize;
644 if (size > UVC_MAX_FRAME_SIZE)
647 npackets = (size + psize - 1) / psize;
648 if (npackets > UVC_MAX_ISO_PACKETS)
649 npackets = UVC_MAX_ISO_PACKETS;
651 size = npackets * psize;
653 if (uvc_alloc_urb_buffers(video, size) < 0)
656 for (i = 0; i < UVC_URBS; ++i) {
657 urb = usb_alloc_urb(npackets, gfp_flags);
659 uvc_uninit_video(video, 1);
663 urb->dev = video->dev->udev;
664 urb->context = video;
665 urb->pipe = usb_rcvisocpipe(video->dev->udev,
666 ep->desc.bEndpointAddress);
667 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
668 urb->interval = ep->desc.bInterval;
669 urb->transfer_buffer = video->urb_buffer[i];
670 urb->transfer_dma = video->urb_dma[i];
671 urb->complete = uvc_video_complete;
672 urb->number_of_packets = npackets;
673 urb->transfer_buffer_length = size;
675 for (j = 0; j < npackets; ++j) {
676 urb->iso_frame_desc[j].offset = j * psize;
677 urb->iso_frame_desc[j].length = psize;
687 * Initialize bulk URBs and allocate transfer buffers. The packet size is
688 * given by the endpoint.
690 static int uvc_init_video_bulk(struct uvc_video_device *video,
691 struct usb_host_endpoint *ep, gfp_t gfp_flags)
694 unsigned int pipe, i;
698 /* Compute the bulk URB size. Some devices set the maximum payload
699 * size to a value too high for memory-constrained devices. We must
700 * then transfer the payload accross multiple URBs. To be consistant
701 * with isochronous mode, allocate maximum UVC_MAX_ISO_PACKETS per bulk
704 psize = le16_to_cpu(ep->desc.wMaxPacketSize) & 0x07ff;
705 size = video->streaming->ctrl.dwMaxPayloadTransferSize;
706 video->bulk.max_payload_size = size;
707 if (size > psize * UVC_MAX_ISO_PACKETS)
708 size = psize * UVC_MAX_ISO_PACKETS;
710 if (uvc_alloc_urb_buffers(video, size) < 0)
713 pipe = usb_rcvbulkpipe(video->dev->udev, ep->desc.bEndpointAddress);
715 for (i = 0; i < UVC_URBS; ++i) {
716 urb = usb_alloc_urb(0, gfp_flags);
718 uvc_uninit_video(video, 1);
722 usb_fill_bulk_urb(urb, video->dev->udev, pipe,
723 video->urb_buffer[i], size, uvc_video_complete,
725 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
726 urb->transfer_dma = video->urb_dma[i];
735 * Initialize isochronous/bulk URBs and allocate transfer buffers.
737 static int uvc_init_video(struct uvc_video_device *video, gfp_t gfp_flags)
739 struct usb_interface *intf = video->streaming->intf;
740 struct usb_host_interface *alts;
741 struct usb_host_endpoint *ep = NULL;
742 int intfnum = video->streaming->intfnum;
743 unsigned int bandwidth, psize, i;
746 video->last_fid = -1;
747 video->bulk.header_size = 0;
748 video->bulk.skip_payload = 0;
749 video->bulk.payload_size = 0;
751 if (intf->num_altsetting > 1) {
752 /* Isochronous endpoint, select the alternate setting. */
753 bandwidth = video->streaming->ctrl.dwMaxPayloadTransferSize;
755 if (bandwidth == 0) {
756 uvc_printk(KERN_WARNING, "device %s requested null "
757 "bandwidth, defaulting to lowest.\n",
762 for (i = 0; i < intf->num_altsetting; ++i) {
763 alts = &intf->altsetting[i];
764 ep = uvc_find_endpoint(alts,
765 video->streaming->header.bEndpointAddress);
769 /* Check if the bandwidth is high enough. */
770 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
771 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
772 if (psize >= bandwidth)
776 if (i >= intf->num_altsetting)
779 if ((ret = usb_set_interface(video->dev->udev, intfnum, i)) < 0)
782 ret = uvc_init_video_isoc(video, ep, gfp_flags);
784 /* Bulk endpoint, proceed to URB initialization. */
785 ep = uvc_find_endpoint(&intf->altsetting[0],
786 video->streaming->header.bEndpointAddress);
790 ret = uvc_init_video_bulk(video, ep, gfp_flags);
796 /* Submit the URBs. */
797 for (i = 0; i < UVC_URBS; ++i) {
798 if ((ret = usb_submit_urb(video->urb[i], gfp_flags)) < 0) {
799 uvc_printk(KERN_ERR, "Failed to submit URB %u "
801 uvc_uninit_video(video, 1);
809 /* --------------------------------------------------------------------------
814 * Stop streaming without disabling the video queue.
816 * To let userspace applications resume without trouble, we must not touch the
817 * video buffers in any way. We mark the device as frozen to make sure the URB
818 * completion handler won't try to cancel the queue when we kill the URBs.
820 int uvc_video_suspend(struct uvc_video_device *video)
822 if (!uvc_queue_streaming(&video->queue))
826 uvc_uninit_video(video, 0);
827 usb_set_interface(video->dev->udev, video->streaming->intfnum, 0);
832 * Reconfigure the video interface and restart streaming if it was enable
835 * If an error occurs, disable the video queue. This will wake all pending
836 * buffers, making sure userspace applications are notified of the problem
837 * instead of waiting forever.
839 int uvc_video_resume(struct uvc_video_device *video)
845 if ((ret = uvc_set_video_ctrl(video, &video->streaming->ctrl, 0)) < 0) {
846 uvc_queue_enable(&video->queue, 0);
850 if (!uvc_queue_streaming(&video->queue))
853 if ((ret = uvc_init_video(video, GFP_NOIO)) < 0)
854 uvc_queue_enable(&video->queue, 0);
859 /* ------------------------------------------------------------------------
864 * Initialize the UVC video device by retrieving the default format and
867 * Some cameras (namely the Fuji Finepix) set the format and frame
868 * indexes to zero. The UVC standard doesn't clearly make this a spec
869 * violation, so try to silently fix the values if possible.
871 * This function is called before registering the device with V4L.
873 int uvc_video_init(struct uvc_video_device *video)
875 struct uvc_streaming_control *probe = &video->streaming->ctrl;
876 struct uvc_format *format = NULL;
877 struct uvc_frame *frame = NULL;
881 if (video->streaming->nformats == 0) {
882 uvc_printk(KERN_INFO, "No supported video formats found.\n");
886 /* Alternate setting 0 should be the default, yet the XBox Live Vision
887 * Cam (and possibly other devices) crash or otherwise misbehave if
888 * they don't receive a SET_INTERFACE request before any other video
891 usb_set_interface(video->dev->udev, video->streaming->intfnum, 0);
893 /* Some webcams don't suport GET_DEF request on the probe control. We
894 * fall back to GET_CUR if GET_DEF fails.
896 if ((ret = uvc_get_video_ctrl(video, probe, 1, GET_DEF)) < 0 &&
897 (ret = uvc_get_video_ctrl(video, probe, 1, GET_CUR)) < 0)
900 /* Check if the default format descriptor exists. Use the first
901 * available format otherwise.
903 for (i = video->streaming->nformats; i > 0; --i) {
904 format = &video->streaming->format[i-1];
905 if (format->index == probe->bFormatIndex)
909 if (format->nframes == 0) {
910 uvc_printk(KERN_INFO, "No frame descriptor found for the "
911 "default format.\n");
915 /* Zero bFrameIndex might be correct. Stream-based formats (including
916 * MPEG-2 TS and DV) do not support frames but have a dummy frame
917 * descriptor with bFrameIndex set to zero. If the default frame
918 * descriptor is not found, use the first avalable frame.
920 for (i = format->nframes; i > 0; --i) {
921 frame = &format->frame[i-1];
922 if (frame->bFrameIndex == probe->bFrameIndex)
926 /* Commit the default settings. */
927 probe->bFormatIndex = format->index;
928 probe->bFrameIndex = frame->bFrameIndex;
929 if ((ret = uvc_set_video_ctrl(video, probe, 0)) < 0)
932 video->streaming->cur_format = format;
933 video->streaming->cur_frame = frame;
934 atomic_set(&video->active, 0);
936 /* Select the video decoding function */
937 if (video->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
938 video->decode = uvc_video_decode_isight;
939 else if (video->streaming->intf->num_altsetting > 1)
940 video->decode = uvc_video_decode_isoc;
942 video->decode = uvc_video_decode_bulk;
948 * Enable or disable the video stream.
950 int uvc_video_enable(struct uvc_video_device *video, int enable)
955 uvc_uninit_video(video, 1);
956 usb_set_interface(video->dev->udev,
957 video->streaming->intfnum, 0);
958 uvc_queue_enable(&video->queue, 0);
962 if ((ret = uvc_queue_enable(&video->queue, 1)) < 0)
965 return uvc_init_video(video, GFP_KERNEL);