• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/drivers/media/video/uvc/
1/*
2 *      uvc_video.c  --  USB Video Class driver - Video handling
3 *
4 *      Copyright (C) 2005-2009
5 *          Laurent Pinchart (laurent.pinchart@skynet.be)
6 *
7 *      This program is free software; you can redistribute it and/or modify
8 *      it under the terms of the GNU General Public License as published by
9 *      the Free Software Foundation; either version 2 of the License, or
10 *      (at your option) any later version.
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/list.h>
16#include <linux/module.h>
17#include <linux/slab.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>
24
25#include <media/v4l2-common.h>
26
27#include "uvcvideo.h"
28
29/* ------------------------------------------------------------------------
30 * UVC Controls
31 */
32
33static int __uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
34			__u8 intfnum, __u8 cs, void *data, __u16 size,
35			int timeout)
36{
37	__u8 type = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
38	unsigned int pipe;
39
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;
43
44	return usb_control_msg(dev->udev, pipe, query, type, cs << 8,
45			unit << 8 | intfnum, data, size, timeout);
46}
47
48int uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
49			__u8 intfnum, __u8 cs, void *data, __u16 size)
50{
51	int ret;
52
53	ret = __uvc_query_ctrl(dev, query, unit, intfnum, cs, data, size,
54				UVC_CTRL_CONTROL_TIMEOUT);
55	if (ret != size) {
56		uvc_printk(KERN_ERR, "Failed to query (%u) UVC control %u "
57			"(unit %u) : %d (exp. %u).\n", query, cs, unit, ret,
58			size);
59		return -EIO;
60	}
61
62	return 0;
63}
64
65static void uvc_fixup_video_ctrl(struct uvc_streaming *stream,
66	struct uvc_streaming_control *ctrl)
67{
68	struct uvc_format *format;
69	struct uvc_frame *frame = NULL;
70	unsigned int i;
71
72	if (ctrl->bFormatIndex <= 0 ||
73	    ctrl->bFormatIndex > stream->nformats)
74		return;
75
76	format = &stream->format[ctrl->bFormatIndex - 1];
77
78	for (i = 0; i < format->nframes; ++i) {
79		if (format->frame[i].bFrameIndex == ctrl->bFrameIndex) {
80			frame = &format->frame[i];
81			break;
82		}
83	}
84
85	if (frame == NULL)
86		return;
87
88	if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) ||
89	     (ctrl->dwMaxVideoFrameSize == 0 &&
90	      stream->dev->uvc_version < 0x0110))
91		ctrl->dwMaxVideoFrameSize =
92			frame->dwMaxVideoFrameBufferSize;
93
94	if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) &&
95	    stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH &&
96	    stream->intf->num_altsetting > 1) {
97		u32 interval;
98		u32 bandwidth;
99
100		interval = (ctrl->dwFrameInterval > 100000)
101			 ? ctrl->dwFrameInterval
102			 : frame->dwFrameInterval[0];
103
104		/* Compute a bandwidth estimation by multiplying the frame
105		 * size by the number of video frames per second, divide the
106		 * result by the number of USB frames (or micro-frames for
107		 * high-speed devices) per second and add the UVC header size
108		 * (assumed to be 12 bytes long).
109		 */
110		bandwidth = frame->wWidth * frame->wHeight / 8 * format->bpp;
111		bandwidth *= 10000000 / interval + 1;
112		bandwidth /= 1000;
113		if (stream->dev->udev->speed == USB_SPEED_HIGH)
114			bandwidth /= 8;
115		bandwidth += 12;
116
117		ctrl->dwMaxPayloadTransferSize = bandwidth;
118	}
119}
120
121static int uvc_get_video_ctrl(struct uvc_streaming *stream,
122	struct uvc_streaming_control *ctrl, int probe, __u8 query)
123{
124	__u8 *data;
125	__u16 size;
126	int ret;
127
128	size = stream->dev->uvc_version >= 0x0110 ? 34 : 26;
129	if ((stream->dev->quirks & UVC_QUIRK_PROBE_DEF) &&
130			query == UVC_GET_DEF)
131		return -EIO;
132
133	data = kmalloc(size, GFP_KERNEL);
134	if (data == NULL)
135		return -ENOMEM;
136
137	ret = __uvc_query_ctrl(stream->dev, query, 0, stream->intfnum,
138		probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
139		size, uvc_timeout_param);
140
141	if ((query == UVC_GET_MIN || query == UVC_GET_MAX) && ret == 2) {
142		/* Some cameras, mostly based on Bison Electronics chipsets,
143		 * answer a GET_MIN or GET_MAX request with the wCompQuality
144		 * field only.
145		 */
146		uvc_warn_once(stream->dev, UVC_WARN_MINMAX, "UVC non "
147			"compliance - GET_MIN/MAX(PROBE) incorrectly "
148			"supported. Enabling workaround.\n");
149		memset(ctrl, 0, sizeof *ctrl);
150		ctrl->wCompQuality = le16_to_cpup((__le16 *)data);
151		ret = 0;
152		goto out;
153	} else if (query == UVC_GET_DEF && probe == 1 && ret != size) {
154		/* Many cameras don't support the GET_DEF request on their
155		 * video probe control. Warn once and return, the caller will
156		 * fall back to GET_CUR.
157		 */
158		uvc_warn_once(stream->dev, UVC_WARN_PROBE_DEF, "UVC non "
159			"compliance - GET_DEF(PROBE) not supported. "
160			"Enabling workaround.\n");
161		ret = -EIO;
162		goto out;
163	} else if (ret != size) {
164		uvc_printk(KERN_ERR, "Failed to query (%u) UVC %s control : "
165			"%d (exp. %u).\n", query, probe ? "probe" : "commit",
166			ret, size);
167		ret = -EIO;
168		goto out;
169	}
170
171	ctrl->bmHint = le16_to_cpup((__le16 *)&data[0]);
172	ctrl->bFormatIndex = data[2];
173	ctrl->bFrameIndex = data[3];
174	ctrl->dwFrameInterval = le32_to_cpup((__le32 *)&data[4]);
175	ctrl->wKeyFrameRate = le16_to_cpup((__le16 *)&data[8]);
176	ctrl->wPFrameRate = le16_to_cpup((__le16 *)&data[10]);
177	ctrl->wCompQuality = le16_to_cpup((__le16 *)&data[12]);
178	ctrl->wCompWindowSize = le16_to_cpup((__le16 *)&data[14]);
179	ctrl->wDelay = le16_to_cpup((__le16 *)&data[16]);
180	ctrl->dwMaxVideoFrameSize = get_unaligned_le32(&data[18]);
181	ctrl->dwMaxPayloadTransferSize = get_unaligned_le32(&data[22]);
182
183	if (size == 34) {
184		ctrl->dwClockFrequency = get_unaligned_le32(&data[26]);
185		ctrl->bmFramingInfo = data[30];
186		ctrl->bPreferedVersion = data[31];
187		ctrl->bMinVersion = data[32];
188		ctrl->bMaxVersion = data[33];
189	} else {
190		ctrl->dwClockFrequency = stream->dev->clock_frequency;
191		ctrl->bmFramingInfo = 0;
192		ctrl->bPreferedVersion = 0;
193		ctrl->bMinVersion = 0;
194		ctrl->bMaxVersion = 0;
195	}
196
197	/* Some broken devices return null or wrong dwMaxVideoFrameSize and
198	 * dwMaxPayloadTransferSize fields. Try to get the value from the
199	 * format and frame descriptors.
200	 */
201	uvc_fixup_video_ctrl(stream, ctrl);
202	ret = 0;
203
204out:
205	kfree(data);
206	return ret;
207}
208
209static int uvc_set_video_ctrl(struct uvc_streaming *stream,
210	struct uvc_streaming_control *ctrl, int probe)
211{
212	__u8 *data;
213	__u16 size;
214	int ret;
215
216	size = stream->dev->uvc_version >= 0x0110 ? 34 : 26;
217	data = kzalloc(size, GFP_KERNEL);
218	if (data == NULL)
219		return -ENOMEM;
220
221	*(__le16 *)&data[0] = cpu_to_le16(ctrl->bmHint);
222	data[2] = ctrl->bFormatIndex;
223	data[3] = ctrl->bFrameIndex;
224	*(__le32 *)&data[4] = cpu_to_le32(ctrl->dwFrameInterval);
225	*(__le16 *)&data[8] = cpu_to_le16(ctrl->wKeyFrameRate);
226	*(__le16 *)&data[10] = cpu_to_le16(ctrl->wPFrameRate);
227	*(__le16 *)&data[12] = cpu_to_le16(ctrl->wCompQuality);
228	*(__le16 *)&data[14] = cpu_to_le16(ctrl->wCompWindowSize);
229	*(__le16 *)&data[16] = cpu_to_le16(ctrl->wDelay);
230	put_unaligned_le32(ctrl->dwMaxVideoFrameSize, &data[18]);
231	put_unaligned_le32(ctrl->dwMaxPayloadTransferSize, &data[22]);
232
233	if (size == 34) {
234		put_unaligned_le32(ctrl->dwClockFrequency, &data[26]);
235		data[30] = ctrl->bmFramingInfo;
236		data[31] = ctrl->bPreferedVersion;
237		data[32] = ctrl->bMinVersion;
238		data[33] = ctrl->bMaxVersion;
239	}
240
241	ret = __uvc_query_ctrl(stream->dev, UVC_SET_CUR, 0, stream->intfnum,
242		probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
243		size, uvc_timeout_param);
244	if (ret != size) {
245		uvc_printk(KERN_ERR, "Failed to set UVC %s control : "
246			"%d (exp. %u).\n", probe ? "probe" : "commit",
247			ret, size);
248		ret = -EIO;
249	}
250
251	kfree(data);
252	return ret;
253}
254
255int uvc_probe_video(struct uvc_streaming *stream,
256	struct uvc_streaming_control *probe)
257{
258	struct uvc_streaming_control probe_min, probe_max;
259	__u16 bandwidth;
260	unsigned int i;
261	int ret;
262
263	mutex_lock(&stream->mutex);
264
265	/* Perform probing. The device should adjust the requested values
266	 * according to its capabilities. However, some devices, namely the
267	 * first generation UVC Logitech webcams, don't implement the Video
268	 * Probe control properly, and just return the needed bandwidth. For
269	 * that reason, if the needed bandwidth exceeds the maximum available
270	 * bandwidth, try to lower the quality.
271	 */
272	ret = uvc_set_video_ctrl(stream, probe, 1);
273	if (ret < 0)
274		goto done;
275
276	/* Get the minimum and maximum values for compression settings. */
277	if (!(stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX)) {
278		ret = uvc_get_video_ctrl(stream, &probe_min, 1, UVC_GET_MIN);
279		if (ret < 0)
280			goto done;
281		ret = uvc_get_video_ctrl(stream, &probe_max, 1, UVC_GET_MAX);
282		if (ret < 0)
283			goto done;
284
285		probe->wCompQuality = probe_max.wCompQuality;
286	}
287
288	for (i = 0; i < 2; ++i) {
289		ret = uvc_set_video_ctrl(stream, probe, 1);
290		if (ret < 0)
291			goto done;
292		ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
293		if (ret < 0)
294			goto done;
295
296		if (stream->intf->num_altsetting == 1)
297			break;
298
299		bandwidth = probe->dwMaxPayloadTransferSize;
300		if (bandwidth <= stream->maxpsize)
301			break;
302
303		if (stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX) {
304			ret = -ENOSPC;
305			goto done;
306		}
307
308		/* TODO: negotiate compression parameters */
309		probe->wKeyFrameRate = probe_min.wKeyFrameRate;
310		probe->wPFrameRate = probe_min.wPFrameRate;
311		probe->wCompQuality = probe_max.wCompQuality;
312		probe->wCompWindowSize = probe_min.wCompWindowSize;
313	}
314
315done:
316	mutex_unlock(&stream->mutex);
317	return ret;
318}
319
320int uvc_commit_video(struct uvc_streaming *stream,
321	struct uvc_streaming_control *probe)
322{
323	return uvc_set_video_ctrl(stream, probe, 0);
324}
325
326/* ------------------------------------------------------------------------
327 * Video codecs
328 */
329
330/* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
331#define UVC_STREAM_EOH	(1 << 7)
332#define UVC_STREAM_ERR	(1 << 6)
333#define UVC_STREAM_STI	(1 << 5)
334#define UVC_STREAM_RES	(1 << 4)
335#define UVC_STREAM_SCR	(1 << 3)
336#define UVC_STREAM_PTS	(1 << 2)
337#define UVC_STREAM_EOF	(1 << 1)
338#define UVC_STREAM_FID	(1 << 0)
339
340/* Video payload decoding is handled by uvc_video_decode_start(),
341 * uvc_video_decode_data() and uvc_video_decode_end().
342 *
343 * uvc_video_decode_start is called with URB data at the start of a bulk or
344 * isochronous payload. It processes header data and returns the header size
345 * in bytes if successful. If an error occurs, it returns a negative error
346 * code. The following error codes have special meanings.
347 *
348 * - EAGAIN informs the caller that the current video buffer should be marked
349 *   as done, and that the function should be called again with the same data
350 *   and a new video buffer. This is used when end of frame conditions can be
351 *   reliably detected at the beginning of the next frame only.
352 *
353 * If an error other than -EAGAIN is returned, the caller will drop the current
354 * payload. No call to uvc_video_decode_data and uvc_video_decode_end will be
355 * made until the next payload. -ENODATA can be used to drop the current
356 * payload if no other error code is appropriate.
357 *
358 * uvc_video_decode_data is called for every URB with URB data. It copies the
359 * data to the video buffer.
360 *
361 * uvc_video_decode_end is called with header data at the end of a bulk or
362 * isochronous payload. It performs any additional header data processing and
363 * returns 0 or a negative error code if an error occured. As header data have
364 * already been processed by uvc_video_decode_start, this functions isn't
365 * required to perform sanity checks a second time.
366 *
367 * For isochronous transfers where a payload is always transfered in a single
368 * URB, the three functions will be called in a row.
369 *
370 * To let the decoder process header data and update its internal state even
371 * when no video buffer is available, uvc_video_decode_start must be prepared
372 * to be called with a NULL buf parameter. uvc_video_decode_data and
373 * uvc_video_decode_end will never be called with a NULL buffer.
374 */
375static int uvc_video_decode_start(struct uvc_streaming *stream,
376		struct uvc_buffer *buf, const __u8 *data, int len)
377{
378	__u8 fid;
379
380	/* Sanity checks:
381	 * - packet must be at least 2 bytes long
382	 * - bHeaderLength value must be at least 2 bytes (see above)
383	 * - bHeaderLength value can't be larger than the packet size.
384	 */
385	if (len < 2 || data[0] < 2 || data[0] > len)
386		return -EINVAL;
387
388	/* Skip payloads marked with the error bit ("error frames"). */
389	if (data[1] & UVC_STREAM_ERR) {
390		uvc_trace(UVC_TRACE_FRAME, "Dropping payload (error bit "
391			  "set).\n");
392		return -ENODATA;
393	}
394
395	fid = data[1] & UVC_STREAM_FID;
396
397	/* Store the payload FID bit and return immediately when the buffer is
398	 * NULL.
399	 */
400	if (buf == NULL) {
401		stream->last_fid = fid;
402		return -ENODATA;
403	}
404
405	/* Synchronize to the input stream by waiting for the FID bit to be
406	 * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE.
407	 * stream->last_fid is initialized to -1, so the first isochronous
408	 * frame will always be in sync.
409	 *
410	 * If the device doesn't toggle the FID bit, invert stream->last_fid
411	 * when the EOF bit is set to force synchronisation on the next packet.
412	 */
413	if (buf->state != UVC_BUF_STATE_ACTIVE) {
414		struct timespec ts;
415
416		if (fid == stream->last_fid) {
417			uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of "
418				"sync).\n");
419			if ((stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) &&
420			    (data[1] & UVC_STREAM_EOF))
421				stream->last_fid ^= UVC_STREAM_FID;
422			return -ENODATA;
423		}
424
425		if (uvc_clock_param == CLOCK_MONOTONIC)
426			ktime_get_ts(&ts);
427		else
428			ktime_get_real_ts(&ts);
429
430		buf->buf.timestamp.tv_sec = ts.tv_sec;
431		buf->buf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
432
433		/* TODO: Handle PTS and SCR. */
434		buf->state = UVC_BUF_STATE_ACTIVE;
435	}
436
437	/* Mark the buffer as done if we're at the beginning of a new frame.
438	 * End of frame detection is better implemented by checking the EOF
439	 * bit (FID bit toggling is delayed by one frame compared to the EOF
440	 * bit), but some devices don't set the bit at end of frame (and the
441	 * last payload can be lost anyway). We thus must check if the FID has
442	 * been toggled.
443	 *
444	 * stream->last_fid is initialized to -1, so the first isochronous
445	 * frame will never trigger an end of frame detection.
446	 *
447	 * Empty buffers (bytesused == 0) don't trigger end of frame detection
448	 * as it doesn't make sense to return an empty buffer. This also
449	 * avoids detecting end of frame conditions at FID toggling if the
450	 * previous payload had the EOF bit set.
451	 */
452	if (fid != stream->last_fid && buf->buf.bytesused != 0) {
453		uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit "
454				"toggled).\n");
455		buf->state = UVC_BUF_STATE_READY;
456		return -EAGAIN;
457	}
458
459	stream->last_fid = fid;
460
461	return data[0];
462}
463
464static void uvc_video_decode_data(struct uvc_streaming *stream,
465		struct uvc_buffer *buf, const __u8 *data, int len)
466{
467	struct uvc_video_queue *queue = &stream->queue;
468	unsigned int maxlen, nbytes;
469	void *mem;
470
471	if (len <= 0)
472		return;
473
474	/* Copy the video data to the buffer. */
475	maxlen = buf->buf.length - buf->buf.bytesused;
476	mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused;
477	nbytes = min((unsigned int)len, maxlen);
478	memcpy(mem, data, nbytes);
479	buf->buf.bytesused += nbytes;
480
481	/* Complete the current frame if the buffer size was exceeded. */
482	if (len > maxlen) {
483		uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n");
484		buf->state = UVC_BUF_STATE_READY;
485	}
486}
487
488static void uvc_video_decode_end(struct uvc_streaming *stream,
489		struct uvc_buffer *buf, const __u8 *data, int len)
490{
491	/* Mark the buffer as done if the EOF marker is set. */
492	if (data[1] & UVC_STREAM_EOF && buf->buf.bytesused != 0) {
493		uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n");
494		if (data[0] == len)
495			uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n");
496		buf->state = UVC_BUF_STATE_READY;
497		if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID)
498			stream->last_fid ^= UVC_STREAM_FID;
499	}
500}
501
502/* Video payload encoding is handled by uvc_video_encode_header() and
503 * uvc_video_encode_data(). Only bulk transfers are currently supported.
504 *
505 * uvc_video_encode_header is called at the start of a payload. It adds header
506 * data to the transfer buffer and returns the header size. As the only known
507 * UVC output device transfers a whole frame in a single payload, the EOF bit
508 * is always set in the header.
509 *
510 * uvc_video_encode_data is called for every URB and copies the data from the
511 * video buffer to the transfer buffer.
512 */
513static int uvc_video_encode_header(struct uvc_streaming *stream,
514		struct uvc_buffer *buf, __u8 *data, int len)
515{
516	data[0] = 2;	/* Header length */
517	data[1] = UVC_STREAM_EOH | UVC_STREAM_EOF
518		| (stream->last_fid & UVC_STREAM_FID);
519	return 2;
520}
521
522static int uvc_video_encode_data(struct uvc_streaming *stream,
523		struct uvc_buffer *buf, __u8 *data, int len)
524{
525	struct uvc_video_queue *queue = &stream->queue;
526	unsigned int nbytes;
527	void *mem;
528
529	/* Copy video data to the URB buffer. */
530	mem = queue->mem + buf->buf.m.offset + queue->buf_used;
531	nbytes = min((unsigned int)len, buf->buf.bytesused - queue->buf_used);
532	nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size,
533			nbytes);
534	memcpy(data, mem, nbytes);
535
536	queue->buf_used += nbytes;
537
538	return nbytes;
539}
540
541/* ------------------------------------------------------------------------
542 * URB handling
543 */
544
545/*
546 * Completion handler for video URBs.
547 */
548static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
549	struct uvc_buffer *buf)
550{
551	u8 *mem;
552	int ret, i;
553
554	for (i = 0; i < urb->number_of_packets; ++i) {
555		if (urb->iso_frame_desc[i].status < 0) {
556			uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
557				"lost (%d).\n", urb->iso_frame_desc[i].status);
558			/* Mark the buffer as faulty. */
559			if (buf != NULL)
560				buf->error = 1;
561			continue;
562		}
563
564		/* Decode the payload header. */
565		mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
566		do {
567			ret = uvc_video_decode_start(stream, buf, mem,
568				urb->iso_frame_desc[i].actual_length);
569			if (ret == -EAGAIN)
570				buf = uvc_queue_next_buffer(&stream->queue,
571							    buf);
572		} while (ret == -EAGAIN);
573
574		if (ret < 0)
575			continue;
576
577		/* Decode the payload data. */
578		uvc_video_decode_data(stream, buf, mem + ret,
579			urb->iso_frame_desc[i].actual_length - ret);
580
581		/* Process the header again. */
582		uvc_video_decode_end(stream, buf, mem,
583			urb->iso_frame_desc[i].actual_length);
584
585		if (buf->state == UVC_BUF_STATE_READY) {
586			if (buf->buf.length != buf->buf.bytesused &&
587			    !(stream->cur_format->flags &
588			      UVC_FMT_FLAG_COMPRESSED))
589				buf->error = 1;
590
591			buf = uvc_queue_next_buffer(&stream->queue, buf);
592		}
593	}
594}
595
596static void uvc_video_decode_bulk(struct urb *urb, struct uvc_streaming *stream,
597	struct uvc_buffer *buf)
598{
599	u8 *mem;
600	int len, ret;
601
602	if (urb->actual_length == 0)
603		return;
604
605	mem = urb->transfer_buffer;
606	len = urb->actual_length;
607	stream->bulk.payload_size += len;
608
609	/* If the URB is the first of its payload, decode and save the
610	 * header.
611	 */
612	if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) {
613		do {
614			ret = uvc_video_decode_start(stream, buf, mem, len);
615			if (ret == -EAGAIN)
616				buf = uvc_queue_next_buffer(&stream->queue,
617							    buf);
618		} while (ret == -EAGAIN);
619
620		/* If an error occured skip the rest of the payload. */
621		if (ret < 0 || buf == NULL) {
622			stream->bulk.skip_payload = 1;
623		} else {
624			memcpy(stream->bulk.header, mem, ret);
625			stream->bulk.header_size = ret;
626
627			mem += ret;
628			len -= ret;
629		}
630	}
631
632	/* The buffer queue might have been cancelled while a bulk transfer
633	 * was in progress, so we can reach here with buf equal to NULL. Make
634	 * sure buf is never dereferenced if NULL.
635	 */
636
637	/* Process video data. */
638	if (!stream->bulk.skip_payload && buf != NULL)
639		uvc_video_decode_data(stream, buf, mem, len);
640
641	/* Detect the payload end by a URB smaller than the maximum size (or
642	 * a payload size equal to the maximum) and process the header again.
643	 */
644	if (urb->actual_length < urb->transfer_buffer_length ||
645	    stream->bulk.payload_size >= stream->bulk.max_payload_size) {
646		if (!stream->bulk.skip_payload && buf != NULL) {
647			uvc_video_decode_end(stream, buf, stream->bulk.header,
648				stream->bulk.payload_size);
649			if (buf->state == UVC_BUF_STATE_READY)
650				buf = uvc_queue_next_buffer(&stream->queue,
651							    buf);
652		}
653
654		stream->bulk.header_size = 0;
655		stream->bulk.skip_payload = 0;
656		stream->bulk.payload_size = 0;
657	}
658}
659
660static void uvc_video_encode_bulk(struct urb *urb, struct uvc_streaming *stream,
661	struct uvc_buffer *buf)
662{
663	u8 *mem = urb->transfer_buffer;
664	int len = stream->urb_size, ret;
665
666	if (buf == NULL) {
667		urb->transfer_buffer_length = 0;
668		return;
669	}
670
671	/* If the URB is the first of its payload, add the header. */
672	if (stream->bulk.header_size == 0) {
673		ret = uvc_video_encode_header(stream, buf, mem, len);
674		stream->bulk.header_size = ret;
675		stream->bulk.payload_size += ret;
676		mem += ret;
677		len -= ret;
678	}
679
680	/* Process video data. */
681	ret = uvc_video_encode_data(stream, buf, mem, len);
682
683	stream->bulk.payload_size += ret;
684	len -= ret;
685
686	if (buf->buf.bytesused == stream->queue.buf_used ||
687	    stream->bulk.payload_size == stream->bulk.max_payload_size) {
688		if (buf->buf.bytesused == stream->queue.buf_used) {
689			stream->queue.buf_used = 0;
690			buf->state = UVC_BUF_STATE_READY;
691			uvc_queue_next_buffer(&stream->queue, buf);
692			stream->last_fid ^= UVC_STREAM_FID;
693		}
694
695		stream->bulk.header_size = 0;
696		stream->bulk.payload_size = 0;
697	}
698
699	urb->transfer_buffer_length = stream->urb_size - len;
700}
701
702static void uvc_video_complete(struct urb *urb)
703{
704	struct uvc_streaming *stream = urb->context;
705	struct uvc_video_queue *queue = &stream->queue;
706	struct uvc_buffer *buf = NULL;
707	unsigned long flags;
708	int ret;
709
710	switch (urb->status) {
711	case 0:
712		break;
713
714	default:
715		uvc_printk(KERN_WARNING, "Non-zero status (%d) in video "
716			"completion handler.\n", urb->status);
717
718	case -ENOENT:		/* usb_kill_urb() called. */
719		if (stream->frozen)
720			return;
721
722	case -ECONNRESET:	/* usb_unlink_urb() called. */
723	case -ESHUTDOWN:	/* The endpoint is being disabled. */
724		uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
725		return;
726	}
727
728	spin_lock_irqsave(&queue->irqlock, flags);
729	if (!list_empty(&queue->irqqueue))
730		buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
731				       queue);
732	spin_unlock_irqrestore(&queue->irqlock, flags);
733
734	stream->decode(urb, stream, buf);
735
736	if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
737		uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n",
738			ret);
739	}
740}
741
742/*
743 * Free transfer buffers.
744 */
745static void uvc_free_urb_buffers(struct uvc_streaming *stream)
746{
747	unsigned int i;
748
749	for (i = 0; i < UVC_URBS; ++i) {
750		if (stream->urb_buffer[i]) {
751			usb_free_coherent(stream->dev->udev, stream->urb_size,
752				stream->urb_buffer[i], stream->urb_dma[i]);
753			stream->urb_buffer[i] = NULL;
754		}
755	}
756
757	stream->urb_size = 0;
758}
759
760/*
761 * Allocate transfer buffers. This function can be called with buffers
762 * already allocated when resuming from suspend, in which case it will
763 * return without touching the buffers.
764 *
765 * Limit the buffer size to UVC_MAX_PACKETS bulk/isochronous packets. If the
766 * system is too low on memory try successively smaller numbers of packets
767 * until allocation succeeds.
768 *
769 * Return the number of allocated packets on success or 0 when out of memory.
770 */
771static int uvc_alloc_urb_buffers(struct uvc_streaming *stream,
772	unsigned int size, unsigned int psize, gfp_t gfp_flags)
773{
774	unsigned int npackets;
775	unsigned int i;
776
777	/* Buffers are already allocated, bail out. */
778	if (stream->urb_size)
779		return stream->urb_size / psize;
780
781	/* Compute the number of packets. Bulk endpoints might transfer UVC
782	 * payloads accross multiple URBs.
783	 */
784	npackets = DIV_ROUND_UP(size, psize);
785	if (npackets > UVC_MAX_PACKETS)
786		npackets = UVC_MAX_PACKETS;
787
788	/* Retry allocations until one succeed. */
789	for (; npackets > 1; npackets /= 2) {
790		for (i = 0; i < UVC_URBS; ++i) {
791			stream->urb_size = psize * npackets;
792			stream->urb_buffer[i] = usb_alloc_coherent(
793				stream->dev->udev, stream->urb_size,
794				gfp_flags | __GFP_NOWARN, &stream->urb_dma[i]);
795			if (!stream->urb_buffer[i]) {
796				uvc_free_urb_buffers(stream);
797				break;
798			}
799		}
800
801		if (i == UVC_URBS) {
802			uvc_trace(UVC_TRACE_VIDEO, "Allocated %u URB buffers "
803				"of %ux%u bytes each.\n", UVC_URBS, npackets,
804				psize);
805			return npackets;
806		}
807	}
808
809	uvc_trace(UVC_TRACE_VIDEO, "Failed to allocate URB buffers (%u bytes "
810		"per packet).\n", psize);
811	return 0;
812}
813
814/*
815 * Uninitialize isochronous/bulk URBs and free transfer buffers.
816 */
817static void uvc_uninit_video(struct uvc_streaming *stream, int free_buffers)
818{
819	struct urb *urb;
820	unsigned int i;
821
822	for (i = 0; i < UVC_URBS; ++i) {
823		urb = stream->urb[i];
824		if (urb == NULL)
825			continue;
826
827		usb_kill_urb(urb);
828		usb_free_urb(urb);
829		stream->urb[i] = NULL;
830	}
831
832	if (free_buffers)
833		uvc_free_urb_buffers(stream);
834}
835
836/*
837 * Initialize isochronous URBs and allocate transfer buffers. The packet size
838 * is given by the endpoint.
839 */
840static int uvc_init_video_isoc(struct uvc_streaming *stream,
841	struct usb_host_endpoint *ep, gfp_t gfp_flags)
842{
843	struct urb *urb;
844	unsigned int npackets, i, j;
845	u16 psize;
846	u32 size;
847
848	psize = le16_to_cpu(ep->desc.wMaxPacketSize);
849	psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
850	size = stream->ctrl.dwMaxVideoFrameSize;
851
852	npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
853	if (npackets == 0)
854		return -ENOMEM;
855
856	size = npackets * psize;
857
858	for (i = 0; i < UVC_URBS; ++i) {
859		urb = usb_alloc_urb(npackets, gfp_flags);
860		if (urb == NULL) {
861			uvc_uninit_video(stream, 1);
862			return -ENOMEM;
863		}
864
865		urb->dev = stream->dev->udev;
866		urb->context = stream;
867		urb->pipe = usb_rcvisocpipe(stream->dev->udev,
868				ep->desc.bEndpointAddress);
869		urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
870		urb->interval = ep->desc.bInterval;
871		urb->transfer_buffer = stream->urb_buffer[i];
872		urb->transfer_dma = stream->urb_dma[i];
873		urb->complete = uvc_video_complete;
874		urb->number_of_packets = npackets;
875		urb->transfer_buffer_length = size;
876
877		for (j = 0; j < npackets; ++j) {
878			urb->iso_frame_desc[j].offset = j * psize;
879			urb->iso_frame_desc[j].length = psize;
880		}
881
882		stream->urb[i] = urb;
883	}
884
885	return 0;
886}
887
888/*
889 * Initialize bulk URBs and allocate transfer buffers. The packet size is
890 * given by the endpoint.
891 */
892static int uvc_init_video_bulk(struct uvc_streaming *stream,
893	struct usb_host_endpoint *ep, gfp_t gfp_flags)
894{
895	struct urb *urb;
896	unsigned int npackets, pipe, i;
897	u16 psize;
898	u32 size;
899
900	psize = le16_to_cpu(ep->desc.wMaxPacketSize) & 0x07ff;
901	size = stream->ctrl.dwMaxPayloadTransferSize;
902	stream->bulk.max_payload_size = size;
903
904	npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
905	if (npackets == 0)
906		return -ENOMEM;
907
908	size = npackets * psize;
909
910	if (usb_endpoint_dir_in(&ep->desc))
911		pipe = usb_rcvbulkpipe(stream->dev->udev,
912				       ep->desc.bEndpointAddress);
913	else
914		pipe = usb_sndbulkpipe(stream->dev->udev,
915				       ep->desc.bEndpointAddress);
916
917	if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
918		size = 0;
919
920	for (i = 0; i < UVC_URBS; ++i) {
921		urb = usb_alloc_urb(0, gfp_flags);
922		if (urb == NULL) {
923			uvc_uninit_video(stream, 1);
924			return -ENOMEM;
925		}
926
927		usb_fill_bulk_urb(urb, stream->dev->udev, pipe,
928			stream->urb_buffer[i], size, uvc_video_complete,
929			stream);
930		urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
931		urb->transfer_dma = stream->urb_dma[i];
932
933		stream->urb[i] = urb;
934	}
935
936	return 0;
937}
938
939/*
940 * Initialize isochronous/bulk URBs and allocate transfer buffers.
941 */
942static int uvc_init_video(struct uvc_streaming *stream, gfp_t gfp_flags)
943{
944	struct usb_interface *intf = stream->intf;
945	struct usb_host_endpoint *ep;
946	unsigned int i;
947	int ret;
948
949	stream->last_fid = -1;
950	stream->bulk.header_size = 0;
951	stream->bulk.skip_payload = 0;
952	stream->bulk.payload_size = 0;
953
954	if (intf->num_altsetting > 1) {
955		struct usb_host_endpoint *best_ep = NULL;
956		unsigned int best_psize = 3 * 1024;
957		unsigned int bandwidth;
958		unsigned int uninitialized_var(altsetting);
959		int intfnum = stream->intfnum;
960
961		/* Isochronous endpoint, select the alternate setting. */
962		bandwidth = stream->ctrl.dwMaxPayloadTransferSize;
963
964		if (bandwidth == 0) {
965			uvc_trace(UVC_TRACE_VIDEO, "Device requested null "
966				"bandwidth, defaulting to lowest.\n");
967			bandwidth = 1;
968		} else {
969			uvc_trace(UVC_TRACE_VIDEO, "Device requested %u "
970				"B/frame bandwidth.\n", bandwidth);
971		}
972
973		for (i = 0; i < intf->num_altsetting; ++i) {
974			struct usb_host_interface *alts;
975			unsigned int psize;
976
977			alts = &intf->altsetting[i];
978			ep = uvc_find_endpoint(alts,
979				stream->header.bEndpointAddress);
980			if (ep == NULL)
981				continue;
982
983			/* Check if the bandwidth is high enough. */
984			psize = le16_to_cpu(ep->desc.wMaxPacketSize);
985			psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
986			if (psize >= bandwidth && psize <= best_psize) {
987				altsetting = i;
988				best_psize = psize;
989				best_ep = ep;
990			}
991		}
992
993		if (best_ep == NULL) {
994			uvc_trace(UVC_TRACE_VIDEO, "No fast enough alt setting "
995				"for requested bandwidth.\n");
996			return -EIO;
997		}
998
999		uvc_trace(UVC_TRACE_VIDEO, "Selecting alternate setting %u "
1000			"(%u B/frame bandwidth).\n", altsetting, best_psize);
1001
1002		ret = usb_set_interface(stream->dev->udev, intfnum, altsetting);
1003		if (ret < 0)
1004			return ret;
1005
1006		ret = uvc_init_video_isoc(stream, best_ep, gfp_flags);
1007	} else {
1008		/* Bulk endpoint, proceed to URB initialization. */
1009		ep = uvc_find_endpoint(&intf->altsetting[0],
1010				stream->header.bEndpointAddress);
1011		if (ep == NULL)
1012			return -EIO;
1013
1014		ret = uvc_init_video_bulk(stream, ep, gfp_flags);
1015	}
1016
1017	if (ret < 0)
1018		return ret;
1019
1020	/* Submit the URBs. */
1021	for (i = 0; i < UVC_URBS; ++i) {
1022		ret = usb_submit_urb(stream->urb[i], gfp_flags);
1023		if (ret < 0) {
1024			uvc_printk(KERN_ERR, "Failed to submit URB %u "
1025					"(%d).\n", i, ret);
1026			uvc_uninit_video(stream, 1);
1027			return ret;
1028		}
1029	}
1030
1031	return 0;
1032}
1033
1034/* --------------------------------------------------------------------------
1035 * Suspend/resume
1036 */
1037
1038/*
1039 * Stop streaming without disabling the video queue.
1040 *
1041 * To let userspace applications resume without trouble, we must not touch the
1042 * video buffers in any way. We mark the device as frozen to make sure the URB
1043 * completion handler won't try to cancel the queue when we kill the URBs.
1044 */
1045int uvc_video_suspend(struct uvc_streaming *stream)
1046{
1047	if (!uvc_queue_streaming(&stream->queue))
1048		return 0;
1049
1050	stream->frozen = 1;
1051	uvc_uninit_video(stream, 0);
1052	usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1053	return 0;
1054}
1055
1056/*
1057 * Reconfigure the video interface and restart streaming if it was enabled
1058 * before suspend.
1059 *
1060 * If an error occurs, disable the video queue. This will wake all pending
1061 * buffers, making sure userspace applications are notified of the problem
1062 * instead of waiting forever.
1063 */
1064int uvc_video_resume(struct uvc_streaming *stream)
1065{
1066	int ret;
1067
1068	stream->frozen = 0;
1069
1070	ret = uvc_commit_video(stream, &stream->ctrl);
1071	if (ret < 0) {
1072		uvc_queue_enable(&stream->queue, 0);
1073		return ret;
1074	}
1075
1076	if (!uvc_queue_streaming(&stream->queue))
1077		return 0;
1078
1079	ret = uvc_init_video(stream, GFP_NOIO);
1080	if (ret < 0)
1081		uvc_queue_enable(&stream->queue, 0);
1082
1083	return ret;
1084}
1085
1086/* ------------------------------------------------------------------------
1087 * Video device
1088 */
1089
1090/*
1091 * Initialize the UVC video device by switching to alternate setting 0 and
1092 * retrieve the default format.
1093 *
1094 * Some cameras (namely the Fuji Finepix) set the format and frame
1095 * indexes to zero. The UVC standard doesn't clearly make this a spec
1096 * violation, so try to silently fix the values if possible.
1097 *
1098 * This function is called before registering the device with V4L.
1099 */
1100int uvc_video_init(struct uvc_streaming *stream)
1101{
1102	struct uvc_streaming_control *probe = &stream->ctrl;
1103	struct uvc_format *format = NULL;
1104	struct uvc_frame *frame = NULL;
1105	unsigned int i;
1106	int ret;
1107
1108	if (stream->nformats == 0) {
1109		uvc_printk(KERN_INFO, "No supported video formats found.\n");
1110		return -EINVAL;
1111	}
1112
1113	atomic_set(&stream->active, 0);
1114
1115	/* Initialize the video buffers queue. */
1116	uvc_queue_init(&stream->queue, stream->type, !uvc_no_drop_param);
1117
1118	/* Alternate setting 0 should be the default, yet the XBox Live Vision
1119	 * Cam (and possibly other devices) crash or otherwise misbehave if
1120	 * they don't receive a SET_INTERFACE request before any other video
1121	 * control request.
1122	 */
1123	usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1124
1125	/* Set the streaming probe control with default streaming parameters
1126	 * retrieved from the device. Webcams that don't suport GET_DEF
1127	 * requests on the probe control will just keep their current streaming
1128	 * parameters.
1129	 */
1130	if (uvc_get_video_ctrl(stream, probe, 1, UVC_GET_DEF) == 0)
1131		uvc_set_video_ctrl(stream, probe, 1);
1132
1133	/* Initialize the streaming parameters with the probe control current
1134	 * value. This makes sure SET_CUR requests on the streaming commit
1135	 * control will always use values retrieved from a successful GET_CUR
1136	 * request on the probe control, as required by the UVC specification.
1137	 */
1138	ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
1139	if (ret < 0)
1140		return ret;
1141
1142	/* Check if the default format descriptor exists. Use the first
1143	 * available format otherwise.
1144	 */
1145	for (i = stream->nformats; i > 0; --i) {
1146		format = &stream->format[i-1];
1147		if (format->index == probe->bFormatIndex)
1148			break;
1149	}
1150
1151	if (format->nframes == 0) {
1152		uvc_printk(KERN_INFO, "No frame descriptor found for the "
1153			"default format.\n");
1154		return -EINVAL;
1155	}
1156
1157	/* Zero bFrameIndex might be correct. Stream-based formats (including
1158	 * MPEG-2 TS and DV) do not support frames but have a dummy frame
1159	 * descriptor with bFrameIndex set to zero. If the default frame
1160	 * descriptor is not found, use the first available frame.
1161	 */
1162	for (i = format->nframes; i > 0; --i) {
1163		frame = &format->frame[i-1];
1164		if (frame->bFrameIndex == probe->bFrameIndex)
1165			break;
1166	}
1167
1168	probe->bFormatIndex = format->index;
1169	probe->bFrameIndex = frame->bFrameIndex;
1170
1171	stream->cur_format = format;
1172	stream->cur_frame = frame;
1173
1174	/* Select the video decoding function */
1175	if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1176		if (stream->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
1177			stream->decode = uvc_video_decode_isight;
1178		else if (stream->intf->num_altsetting > 1)
1179			stream->decode = uvc_video_decode_isoc;
1180		else
1181			stream->decode = uvc_video_decode_bulk;
1182	} else {
1183		if (stream->intf->num_altsetting == 1)
1184			stream->decode = uvc_video_encode_bulk;
1185		else {
1186			uvc_printk(KERN_INFO, "Isochronous endpoints are not "
1187				"supported for video output devices.\n");
1188			return -EINVAL;
1189		}
1190	}
1191
1192	return 0;
1193}
1194
1195/*
1196 * Enable or disable the video stream.
1197 */
1198int uvc_video_enable(struct uvc_streaming *stream, int enable)
1199{
1200	int ret;
1201
1202	if (!enable) {
1203		uvc_uninit_video(stream, 1);
1204		usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1205		uvc_queue_enable(&stream->queue, 0);
1206		return 0;
1207	}
1208
1209	ret = uvc_queue_enable(&stream->queue, 1);
1210	if (ret < 0)
1211		return ret;
1212
1213	/* Commit the streaming parameters. */
1214	ret = uvc_commit_video(stream, &stream->ctrl);
1215	if (ret < 0)
1216		return ret;
1217
1218	return uvc_init_video(stream, GFP_KERNEL);
1219}
1220