blob: f960e8ea4f17e3d632547c5b1551d406cf54f736 [file] [log] [blame]
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001/*
2 * uvc_video.c -- USB Video Class driver - Video handling
3 *
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03004 * Copyright (C) 2005-2009
Laurent Pinchartc0efd232008-06-30 15:04:50 -03005 * 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>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030015#include <linux/list.h>
16#include <linux/module.h>
17#include <linux/usb.h>
18#include <linux/videodev2.h>
19#include <linux/vmalloc.h>
20#include <linux/wait.h>
21#include <asm/atomic.h>
22#include <asm/unaligned.h>
23
24#include <media/v4l2-common.h>
25
26#include "uvcvideo.h"
27
28/* ------------------------------------------------------------------------
29 * UVC Controls
30 */
31
32static int __uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
33 __u8 intfnum, __u8 cs, void *data, __u16 size,
34 int timeout)
35{
36 __u8 type = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
37 unsigned int pipe;
Laurent Pinchartc0efd232008-06-30 15:04:50 -030038
39 pipe = (query & 0x80) ? usb_rcvctrlpipe(dev->udev, 0)
40 : usb_sndctrlpipe(dev->udev, 0);
41 type |= (query & 0x80) ? USB_DIR_IN : USB_DIR_OUT;
42
Laurent Pinchart44f00792008-11-08 19:14:50 -030043 return usb_control_msg(dev->udev, pipe, query, type, cs << 8,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030044 unit << 8 | intfnum, data, size, timeout);
Laurent Pinchart44f00792008-11-08 19:14:50 -030045}
Laurent Pinchartc0efd232008-06-30 15:04:50 -030046
Laurent Pinchart44f00792008-11-08 19:14:50 -030047int uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
48 __u8 intfnum, __u8 cs, void *data, __u16 size)
49{
50 int ret;
51
52 ret = __uvc_query_ctrl(dev, query, unit, intfnum, cs, data, size,
53 UVC_CTRL_CONTROL_TIMEOUT);
Laurent Pinchartc0efd232008-06-30 15:04:50 -030054 if (ret != size) {
55 uvc_printk(KERN_ERR, "Failed to query (%u) UVC control %u "
56 "(unit %u) : %d (exp. %u).\n", query, cs, unit, ret,
57 size);
58 return -EIO;
59 }
60
61 return 0;
62}
63
Laurent Pinchart35f02a62009-06-28 08:37:50 -030064static void uvc_fixup_video_ctrl(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030065 struct uvc_streaming_control *ctrl)
66{
67 struct uvc_format *format;
Laurent Pinchart078f8942009-05-06 12:30:30 -030068 struct uvc_frame *frame = NULL;
69 unsigned int i;
Laurent Pinchartc0efd232008-06-30 15:04:50 -030070
71 if (ctrl->bFormatIndex <= 0 ||
Laurent Pinchart35f02a62009-06-28 08:37:50 -030072 ctrl->bFormatIndex > stream->nformats)
Laurent Pinchartc0efd232008-06-30 15:04:50 -030073 return;
74
Laurent Pinchart35f02a62009-06-28 08:37:50 -030075 format = &stream->format[ctrl->bFormatIndex - 1];
Laurent Pinchartc0efd232008-06-30 15:04:50 -030076
Laurent Pinchart078f8942009-05-06 12:30:30 -030077 for (i = 0; i < format->nframes; ++i) {
78 if (format->frame[i].bFrameIndex == ctrl->bFrameIndex) {
79 frame = &format->frame[i];
80 break;
81 }
82 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -030083
Laurent Pinchart078f8942009-05-06 12:30:30 -030084 if (frame == NULL)
85 return;
Laurent Pinchartc0efd232008-06-30 15:04:50 -030086
87 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) ||
88 (ctrl->dwMaxVideoFrameSize == 0 &&
Laurent Pinchart35f02a62009-06-28 08:37:50 -030089 stream->dev->uvc_version < 0x0110))
Laurent Pinchartc0efd232008-06-30 15:04:50 -030090 ctrl->dwMaxVideoFrameSize =
91 frame->dwMaxVideoFrameBufferSize;
Laurent Pinchart50144ae2009-02-16 17:41:52 -030092
Laurent Pinchart35f02a62009-06-28 08:37:50 -030093 if (stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH &&
94 stream->intf->num_altsetting > 1) {
Laurent Pinchart50144ae2009-02-16 17:41:52 -030095 u32 interval;
96 u32 bandwidth;
97
98 interval = (ctrl->dwFrameInterval > 100000)
99 ? ctrl->dwFrameInterval
100 : frame->dwFrameInterval[0];
101
102 /* Compute a bandwidth estimation by multiplying the frame
103 * size by the number of video frames per second, divide the
104 * result by the number of USB frames (or micro-frames for
105 * high-speed devices) per second and add the UVC header size
106 * (assumed to be 12 bytes long).
107 */
108 bandwidth = frame->wWidth * frame->wHeight / 8 * format->bpp;
109 bandwidth *= 10000000 / interval + 1;
110 bandwidth /= 1000;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300111 if (stream->dev->udev->speed == USB_SPEED_HIGH)
Laurent Pinchart50144ae2009-02-16 17:41:52 -0300112 bandwidth /= 8;
113 bandwidth += 12;
114
115 ctrl->dwMaxPayloadTransferSize = bandwidth;
116 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300117}
118
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300119static int uvc_get_video_ctrl(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300120 struct uvc_streaming_control *ctrl, int probe, __u8 query)
121{
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300122 __u8 *data;
123 __u16 size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300124 int ret;
125
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300126 size = stream->dev->uvc_version >= 0x0110 ? 34 : 26;
Julia Lawalld2be7642009-09-17 23:44:01 -0300127 if ((stream->dev->quirks & UVC_QUIRK_PROBE_DEF) &&
128 query == UVC_GET_DEF)
129 return -EIO;
130
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300131 data = kmalloc(size, GFP_KERNEL);
132 if (data == NULL)
133 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300134
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300135 ret = __uvc_query_ctrl(stream->dev, query, 0, stream->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300136 probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
137 size, UVC_CTRL_STREAMING_TIMEOUT);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300138
Laurent Pinchartb482d922009-06-26 11:39:42 -0300139 if ((query == UVC_GET_MIN || query == UVC_GET_MAX) && ret == 2) {
Laurent Pinchart44f00792008-11-08 19:14:50 -0300140 /* Some cameras, mostly based on Bison Electronics chipsets,
141 * answer a GET_MIN or GET_MAX request with the wCompQuality
142 * field only.
143 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300144 uvc_warn_once(stream->dev, UVC_WARN_MINMAX, "UVC non "
Laurent Pinchart44f00792008-11-08 19:14:50 -0300145 "compliance - GET_MIN/MAX(PROBE) incorrectly "
146 "supported. Enabling workaround.\n");
147 memset(ctrl, 0, sizeof ctrl);
148 ctrl->wCompQuality = le16_to_cpup((__le16 *)data);
149 ret = 0;
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300150 goto out;
Laurent Pinchartb482d922009-06-26 11:39:42 -0300151 } else if (query == UVC_GET_DEF && probe == 1 && ret != size) {
Laurent Pinchart44f00792008-11-08 19:14:50 -0300152 /* Many cameras don't support the GET_DEF request on their
153 * video probe control. Warn once and return, the caller will
154 * fall back to GET_CUR.
155 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300156 uvc_warn_once(stream->dev, UVC_WARN_PROBE_DEF, "UVC non "
Laurent Pinchart44f00792008-11-08 19:14:50 -0300157 "compliance - GET_DEF(PROBE) not supported. "
158 "Enabling workaround.\n");
159 ret = -EIO;
160 goto out;
161 } else if (ret != size) {
162 uvc_printk(KERN_ERR, "Failed to query (%u) UVC %s control : "
163 "%d (exp. %u).\n", query, probe ? "probe" : "commit",
164 ret, size);
165 ret = -EIO;
166 goto out;
167 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300168
169 ctrl->bmHint = le16_to_cpup((__le16 *)&data[0]);
170 ctrl->bFormatIndex = data[2];
171 ctrl->bFrameIndex = data[3];
172 ctrl->dwFrameInterval = le32_to_cpup((__le32 *)&data[4]);
173 ctrl->wKeyFrameRate = le16_to_cpup((__le16 *)&data[8]);
174 ctrl->wPFrameRate = le16_to_cpup((__le16 *)&data[10]);
175 ctrl->wCompQuality = le16_to_cpup((__le16 *)&data[12]);
176 ctrl->wCompWindowSize = le16_to_cpup((__le16 *)&data[14]);
177 ctrl->wDelay = le16_to_cpup((__le16 *)&data[16]);
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300178 ctrl->dwMaxVideoFrameSize = get_unaligned_le32(&data[18]);
179 ctrl->dwMaxPayloadTransferSize = get_unaligned_le32(&data[22]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300180
181 if (size == 34) {
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300182 ctrl->dwClockFrequency = get_unaligned_le32(&data[26]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300183 ctrl->bmFramingInfo = data[30];
184 ctrl->bPreferedVersion = data[31];
185 ctrl->bMinVersion = data[32];
186 ctrl->bMaxVersion = data[33];
187 } else {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300188 ctrl->dwClockFrequency = stream->dev->clock_frequency;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300189 ctrl->bmFramingInfo = 0;
190 ctrl->bPreferedVersion = 0;
191 ctrl->bMinVersion = 0;
192 ctrl->bMaxVersion = 0;
193 }
194
Laurent Pinchart50144ae2009-02-16 17:41:52 -0300195 /* Some broken devices return null or wrong dwMaxVideoFrameSize and
196 * dwMaxPayloadTransferSize fields. Try to get the value from the
197 * format and frame descriptors.
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300198 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300199 uvc_fixup_video_ctrl(stream, ctrl);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300200 ret = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300201
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300202out:
203 kfree(data);
204 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300205}
206
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300207static int uvc_set_video_ctrl(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300208 struct uvc_streaming_control *ctrl, int probe)
209{
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300210 __u8 *data;
211 __u16 size;
212 int ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300213
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300214 size = stream->dev->uvc_version >= 0x0110 ? 34 : 26;
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300215 data = kzalloc(size, GFP_KERNEL);
216 if (data == NULL)
217 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300218
219 *(__le16 *)&data[0] = cpu_to_le16(ctrl->bmHint);
220 data[2] = ctrl->bFormatIndex;
221 data[3] = ctrl->bFrameIndex;
222 *(__le32 *)&data[4] = cpu_to_le32(ctrl->dwFrameInterval);
223 *(__le16 *)&data[8] = cpu_to_le16(ctrl->wKeyFrameRate);
224 *(__le16 *)&data[10] = cpu_to_le16(ctrl->wPFrameRate);
225 *(__le16 *)&data[12] = cpu_to_le16(ctrl->wCompQuality);
226 *(__le16 *)&data[14] = cpu_to_le16(ctrl->wCompWindowSize);
227 *(__le16 *)&data[16] = cpu_to_le16(ctrl->wDelay);
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300228 put_unaligned_le32(ctrl->dwMaxVideoFrameSize, &data[18]);
229 put_unaligned_le32(ctrl->dwMaxPayloadTransferSize, &data[22]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300230
231 if (size == 34) {
Laurent Pinchart4d3939f2008-11-11 14:04:30 -0300232 put_unaligned_le32(ctrl->dwClockFrequency, &data[26]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300233 data[30] = ctrl->bmFramingInfo;
234 data[31] = ctrl->bPreferedVersion;
235 data[32] = ctrl->bMinVersion;
236 data[33] = ctrl->bMaxVersion;
237 }
238
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300239 ret = __uvc_query_ctrl(stream->dev, UVC_SET_CUR, 0, stream->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300240 probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
241 size, UVC_CTRL_STREAMING_TIMEOUT);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300242 if (ret != size) {
243 uvc_printk(KERN_ERR, "Failed to set UVC %s control : "
244 "%d (exp. %u).\n", probe ? "probe" : "commit",
245 ret, size);
246 ret = -EIO;
247 }
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300248
249 kfree(data);
250 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300251}
252
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300253int uvc_probe_video(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300254 struct uvc_streaming_control *probe)
255{
256 struct uvc_streaming_control probe_min, probe_max;
257 __u16 bandwidth;
258 unsigned int i;
259 int ret;
260
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300261 mutex_lock(&stream->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300262
263 /* Perform probing. The device should adjust the requested values
264 * according to its capabilities. However, some devices, namely the
265 * first generation UVC Logitech webcams, don't implement the Video
266 * Probe control properly, and just return the needed bandwidth. For
267 * that reason, if the needed bandwidth exceeds the maximum available
268 * bandwidth, try to lower the quality.
269 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300270 ret = uvc_set_video_ctrl(stream, probe, 1);
271 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300272 goto done;
273
274 /* Get the minimum and maximum values for compression settings. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300275 if (!(stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX)) {
276 ret = uvc_get_video_ctrl(stream, &probe_min, 1, UVC_GET_MIN);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300277 if (ret < 0)
278 goto done;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300279 ret = uvc_get_video_ctrl(stream, &probe_max, 1, UVC_GET_MAX);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300280 if (ret < 0)
281 goto done;
282
283 probe->wCompQuality = probe_max.wCompQuality;
284 }
285
286 for (i = 0; i < 2; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300287 ret = uvc_set_video_ctrl(stream, probe, 1);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300288 if (ret < 0)
289 goto done;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300290 ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300291 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300292 goto done;
293
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300294 if (stream->intf->num_altsetting == 1)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300295 break;
296
297 bandwidth = probe->dwMaxPayloadTransferSize;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300298 if (bandwidth <= stream->maxpsize)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300299 break;
300
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300301 if (stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300302 ret = -ENOSPC;
303 goto done;
304 }
305
306 /* TODO: negotiate compression parameters */
307 probe->wKeyFrameRate = probe_min.wKeyFrameRate;
308 probe->wPFrameRate = probe_min.wPFrameRate;
309 probe->wCompQuality = probe_max.wCompQuality;
310 probe->wCompWindowSize = probe_min.wCompWindowSize;
311 }
312
313done:
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300314 mutex_unlock(&stream->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300315 return ret;
316}
317
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300318int uvc_commit_video(struct uvc_streaming *stream,
Laurent Pinchart44f00792008-11-08 19:14:50 -0300319 struct uvc_streaming_control *probe)
320{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300321 return uvc_set_video_ctrl(stream, probe, 0);
Laurent Pinchart44f00792008-11-08 19:14:50 -0300322}
323
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300324/* ------------------------------------------------------------------------
325 * Video codecs
326 */
327
328/* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
329#define UVC_STREAM_EOH (1 << 7)
330#define UVC_STREAM_ERR (1 << 6)
331#define UVC_STREAM_STI (1 << 5)
332#define UVC_STREAM_RES (1 << 4)
333#define UVC_STREAM_SCR (1 << 3)
334#define UVC_STREAM_PTS (1 << 2)
335#define UVC_STREAM_EOF (1 << 1)
336#define UVC_STREAM_FID (1 << 0)
337
338/* Video payload decoding is handled by uvc_video_decode_start(),
339 * uvc_video_decode_data() and uvc_video_decode_end().
340 *
341 * uvc_video_decode_start is called with URB data at the start of a bulk or
342 * isochronous payload. It processes header data and returns the header size
343 * in bytes if successful. If an error occurs, it returns a negative error
344 * code. The following error codes have special meanings.
345 *
346 * - EAGAIN informs the caller that the current video buffer should be marked
347 * as done, and that the function should be called again with the same data
348 * and a new video buffer. This is used when end of frame conditions can be
349 * reliably detected at the beginning of the next frame only.
350 *
351 * If an error other than -EAGAIN is returned, the caller will drop the current
352 * payload. No call to uvc_video_decode_data and uvc_video_decode_end will be
353 * made until the next payload. -ENODATA can be used to drop the current
354 * payload if no other error code is appropriate.
355 *
356 * uvc_video_decode_data is called for every URB with URB data. It copies the
357 * data to the video buffer.
358 *
359 * uvc_video_decode_end is called with header data at the end of a bulk or
360 * isochronous payload. It performs any additional header data processing and
361 * returns 0 or a negative error code if an error occured. As header data have
362 * already been processed by uvc_video_decode_start, this functions isn't
363 * required to perform sanity checks a second time.
364 *
365 * For isochronous transfers where a payload is always transfered in a single
366 * URB, the three functions will be called in a row.
367 *
368 * To let the decoder process header data and update its internal state even
369 * when no video buffer is available, uvc_video_decode_start must be prepared
370 * to be called with a NULL buf parameter. uvc_video_decode_data and
371 * uvc_video_decode_end will never be called with a NULL buffer.
372 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300373static int uvc_video_decode_start(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300374 struct uvc_buffer *buf, const __u8 *data, int len)
375{
376 __u8 fid;
377
378 /* Sanity checks:
379 * - packet must be at least 2 bytes long
380 * - bHeaderLength value must be at least 2 bytes (see above)
381 * - bHeaderLength value can't be larger than the packet size.
382 */
383 if (len < 2 || data[0] < 2 || data[0] > len)
384 return -EINVAL;
385
386 /* Skip payloads marked with the error bit ("error frames"). */
387 if (data[1] & UVC_STREAM_ERR) {
388 uvc_trace(UVC_TRACE_FRAME, "Dropping payload (error bit "
389 "set).\n");
390 return -ENODATA;
391 }
392
393 fid = data[1] & UVC_STREAM_FID;
394
395 /* Store the payload FID bit and return immediately when the buffer is
396 * NULL.
397 */
398 if (buf == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300399 stream->last_fid = fid;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300400 return -ENODATA;
401 }
402
403 /* Synchronize to the input stream by waiting for the FID bit to be
404 * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE.
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300405 * stream->last_fid is initialized to -1, so the first isochronous
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300406 * frame will always be in sync.
407 *
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300408 * If the device doesn't toggle the FID bit, invert stream->last_fid
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300409 * when the EOF bit is set to force synchronisation on the next packet.
410 */
411 if (buf->state != UVC_BUF_STATE_ACTIVE) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300412 if (fid == stream->last_fid) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300413 uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of "
414 "sync).\n");
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300415 if ((stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) &&
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300416 (data[1] & UVC_STREAM_EOF))
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300417 stream->last_fid ^= UVC_STREAM_FID;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300418 return -ENODATA;
419 }
420
421 /* TODO: Handle PTS and SCR. */
422 buf->state = UVC_BUF_STATE_ACTIVE;
423 }
424
425 /* Mark the buffer as done if we're at the beginning of a new frame.
426 * End of frame detection is better implemented by checking the EOF
427 * bit (FID bit toggling is delayed by one frame compared to the EOF
428 * bit), but some devices don't set the bit at end of frame (and the
429 * last payload can be lost anyway). We thus must check if the FID has
430 * been toggled.
431 *
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300432 * stream->last_fid is initialized to -1, so the first isochronous
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300433 * frame will never trigger an end of frame detection.
434 *
435 * Empty buffers (bytesused == 0) don't trigger end of frame detection
436 * as it doesn't make sense to return an empty buffer. This also
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300437 * avoids detecting end of frame conditions at FID toggling if the
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300438 * previous payload had the EOF bit set.
439 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300440 if (fid != stream->last_fid && buf->buf.bytesused != 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300441 uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit "
442 "toggled).\n");
443 buf->state = UVC_BUF_STATE_DONE;
444 return -EAGAIN;
445 }
446
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300447 stream->last_fid = fid;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300448
449 return data[0];
450}
451
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300452static void uvc_video_decode_data(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300453 struct uvc_buffer *buf, const __u8 *data, int len)
454{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300455 struct uvc_video_queue *queue = &stream->queue;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300456 unsigned int maxlen, nbytes;
457 void *mem;
458
459 if (len <= 0)
460 return;
461
462 /* Copy the video data to the buffer. */
463 maxlen = buf->buf.length - buf->buf.bytesused;
464 mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused;
465 nbytes = min((unsigned int)len, maxlen);
466 memcpy(mem, data, nbytes);
467 buf->buf.bytesused += nbytes;
468
469 /* Complete the current frame if the buffer size was exceeded. */
470 if (len > maxlen) {
471 uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n");
472 buf->state = UVC_BUF_STATE_DONE;
473 }
474}
475
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300476static void uvc_video_decode_end(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300477 struct uvc_buffer *buf, const __u8 *data, int len)
478{
479 /* Mark the buffer as done if the EOF marker is set. */
480 if (data[1] & UVC_STREAM_EOF && buf->buf.bytesused != 0) {
481 uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n");
482 if (data[0] == len)
483 uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n");
484 buf->state = UVC_BUF_STATE_DONE;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300485 if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID)
486 stream->last_fid ^= UVC_STREAM_FID;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300487 }
488}
489
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300490/* Video payload encoding is handled by uvc_video_encode_header() and
491 * uvc_video_encode_data(). Only bulk transfers are currently supported.
492 *
493 * uvc_video_encode_header is called at the start of a payload. It adds header
494 * data to the transfer buffer and returns the header size. As the only known
495 * UVC output device transfers a whole frame in a single payload, the EOF bit
496 * is always set in the header.
497 *
498 * uvc_video_encode_data is called for every URB and copies the data from the
499 * video buffer to the transfer buffer.
500 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300501static int uvc_video_encode_header(struct uvc_streaming *stream,
Laurent Pinchartff924202008-12-28 22:32:29 -0300502 struct uvc_buffer *buf, __u8 *data, int len)
503{
504 data[0] = 2; /* Header length */
505 data[1] = UVC_STREAM_EOH | UVC_STREAM_EOF
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300506 | (stream->last_fid & UVC_STREAM_FID);
Laurent Pinchartff924202008-12-28 22:32:29 -0300507 return 2;
508}
509
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300510static int uvc_video_encode_data(struct uvc_streaming *stream,
Laurent Pinchartff924202008-12-28 22:32:29 -0300511 struct uvc_buffer *buf, __u8 *data, int len)
512{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300513 struct uvc_video_queue *queue = &stream->queue;
Laurent Pinchartff924202008-12-28 22:32:29 -0300514 unsigned int nbytes;
515 void *mem;
516
517 /* Copy video data to the URB buffer. */
518 mem = queue->mem + buf->buf.m.offset + queue->buf_used;
519 nbytes = min((unsigned int)len, buf->buf.bytesused - queue->buf_used);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300520 nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size,
Laurent Pinchartff924202008-12-28 22:32:29 -0300521 nbytes);
522 memcpy(data, mem, nbytes);
523
524 queue->buf_used += nbytes;
525
526 return nbytes;
527}
528
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300529/* ------------------------------------------------------------------------
530 * URB handling
531 */
532
533/*
534 * Completion handler for video URBs.
535 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300536static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
537 struct uvc_buffer *buf)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300538{
539 u8 *mem;
540 int ret, i;
541
542 for (i = 0; i < urb->number_of_packets; ++i) {
543 if (urb->iso_frame_desc[i].status < 0) {
544 uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
545 "lost (%d).\n", urb->iso_frame_desc[i].status);
546 continue;
547 }
548
549 /* Decode the payload header. */
550 mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
551 do {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300552 ret = uvc_video_decode_start(stream, buf, mem,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300553 urb->iso_frame_desc[i].actual_length);
554 if (ret == -EAGAIN)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300555 buf = uvc_queue_next_buffer(&stream->queue,
556 buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300557 } while (ret == -EAGAIN);
558
559 if (ret < 0)
560 continue;
561
562 /* Decode the payload data. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300563 uvc_video_decode_data(stream, buf, mem + ret,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300564 urb->iso_frame_desc[i].actual_length - ret);
565
566 /* Process the header again. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300567 uvc_video_decode_end(stream, buf, mem,
Laurent Pinchart08ebd002008-08-29 17:37:44 -0300568 urb->iso_frame_desc[i].actual_length);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300569
570 if (buf->state == UVC_BUF_STATE_DONE ||
571 buf->state == UVC_BUF_STATE_ERROR)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300572 buf = uvc_queue_next_buffer(&stream->queue, buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300573 }
574}
575
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300576static void uvc_video_decode_bulk(struct urb *urb, struct uvc_streaming *stream,
577 struct uvc_buffer *buf)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300578{
579 u8 *mem;
580 int len, ret;
581
Laurent Pinchartc90e7772009-02-14 19:39:08 -0300582 if (urb->actual_length == 0)
583 return;
584
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300585 mem = urb->transfer_buffer;
586 len = urb->actual_length;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300587 stream->bulk.payload_size += len;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300588
589 /* If the URB is the first of its payload, decode and save the
590 * header.
591 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300592 if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300593 do {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300594 ret = uvc_video_decode_start(stream, buf, mem, len);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300595 if (ret == -EAGAIN)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300596 buf = uvc_queue_next_buffer(&stream->queue,
597 buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300598 } while (ret == -EAGAIN);
599
600 /* If an error occured skip the rest of the payload. */
601 if (ret < 0 || buf == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300602 stream->bulk.skip_payload = 1;
Laurent Pinchartf8dd4af2008-12-16 10:41:57 -0300603 } else {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300604 memcpy(stream->bulk.header, mem, ret);
605 stream->bulk.header_size = ret;
Laurent Pinchartf8dd4af2008-12-16 10:41:57 -0300606
607 mem += ret;
608 len -= ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300609 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300610 }
611
612 /* The buffer queue might have been cancelled while a bulk transfer
613 * was in progress, so we can reach here with buf equal to NULL. Make
614 * sure buf is never dereferenced if NULL.
615 */
616
617 /* Process video data. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300618 if (!stream->bulk.skip_payload && buf != NULL)
619 uvc_video_decode_data(stream, buf, mem, len);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300620
621 /* Detect the payload end by a URB smaller than the maximum size (or
622 * a payload size equal to the maximum) and process the header again.
623 */
624 if (urb->actual_length < urb->transfer_buffer_length ||
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300625 stream->bulk.payload_size >= stream->bulk.max_payload_size) {
626 if (!stream->bulk.skip_payload && buf != NULL) {
627 uvc_video_decode_end(stream, buf, stream->bulk.header,
628 stream->bulk.payload_size);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300629 if (buf->state == UVC_BUF_STATE_DONE ||
630 buf->state == UVC_BUF_STATE_ERROR)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300631 buf = uvc_queue_next_buffer(&stream->queue,
632 buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300633 }
634
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300635 stream->bulk.header_size = 0;
636 stream->bulk.skip_payload = 0;
637 stream->bulk.payload_size = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300638 }
639}
640
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300641static void uvc_video_encode_bulk(struct urb *urb, struct uvc_streaming *stream,
642 struct uvc_buffer *buf)
Laurent Pinchartff924202008-12-28 22:32:29 -0300643{
644 u8 *mem = urb->transfer_buffer;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300645 int len = stream->urb_size, ret;
Laurent Pinchartff924202008-12-28 22:32:29 -0300646
647 if (buf == NULL) {
648 urb->transfer_buffer_length = 0;
649 return;
650 }
651
652 /* If the URB is the first of its payload, add the header. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300653 if (stream->bulk.header_size == 0) {
654 ret = uvc_video_encode_header(stream, buf, mem, len);
655 stream->bulk.header_size = ret;
656 stream->bulk.payload_size += ret;
Laurent Pinchartff924202008-12-28 22:32:29 -0300657 mem += ret;
658 len -= ret;
659 }
660
661 /* Process video data. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300662 ret = uvc_video_encode_data(stream, buf, mem, len);
Laurent Pinchartff924202008-12-28 22:32:29 -0300663
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300664 stream->bulk.payload_size += ret;
Laurent Pinchartff924202008-12-28 22:32:29 -0300665 len -= ret;
666
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300667 if (buf->buf.bytesused == stream->queue.buf_used ||
668 stream->bulk.payload_size == stream->bulk.max_payload_size) {
669 if (buf->buf.bytesused == stream->queue.buf_used) {
670 stream->queue.buf_used = 0;
Laurent Pinchartff924202008-12-28 22:32:29 -0300671 buf->state = UVC_BUF_STATE_DONE;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300672 uvc_queue_next_buffer(&stream->queue, buf);
673 stream->last_fid ^= UVC_STREAM_FID;
Laurent Pinchartff924202008-12-28 22:32:29 -0300674 }
675
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300676 stream->bulk.header_size = 0;
677 stream->bulk.payload_size = 0;
Laurent Pinchartff924202008-12-28 22:32:29 -0300678 }
679
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300680 urb->transfer_buffer_length = stream->urb_size - len;
Laurent Pinchartff924202008-12-28 22:32:29 -0300681}
682
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300683static void uvc_video_complete(struct urb *urb)
684{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300685 struct uvc_streaming *stream = urb->context;
686 struct uvc_video_queue *queue = &stream->queue;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300687 struct uvc_buffer *buf = NULL;
688 unsigned long flags;
689 int ret;
690
691 switch (urb->status) {
692 case 0:
693 break;
694
695 default:
696 uvc_printk(KERN_WARNING, "Non-zero status (%d) in video "
697 "completion handler.\n", urb->status);
698
699 case -ENOENT: /* usb_kill_urb() called. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300700 if (stream->frozen)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300701 return;
702
703 case -ECONNRESET: /* usb_unlink_urb() called. */
704 case -ESHUTDOWN: /* The endpoint is being disabled. */
705 uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
706 return;
707 }
708
709 spin_lock_irqsave(&queue->irqlock, flags);
710 if (!list_empty(&queue->irqqueue))
711 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
712 queue);
713 spin_unlock_irqrestore(&queue->irqlock, flags);
714
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300715 stream->decode(urb, stream, buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300716
717 if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
718 uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n",
719 ret);
720 }
721}
722
723/*
Laurent Pincharte01117c2008-07-04 00:36:21 -0300724 * Free transfer buffers.
725 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300726static void uvc_free_urb_buffers(struct uvc_streaming *stream)
Laurent Pincharte01117c2008-07-04 00:36:21 -0300727{
728 unsigned int i;
729
730 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300731 if (stream->urb_buffer[i]) {
732 usb_buffer_free(stream->dev->udev, stream->urb_size,
733 stream->urb_buffer[i], stream->urb_dma[i]);
734 stream->urb_buffer[i] = NULL;
Laurent Pincharte01117c2008-07-04 00:36:21 -0300735 }
736 }
737
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300738 stream->urb_size = 0;
Laurent Pincharte01117c2008-07-04 00:36:21 -0300739}
740
741/*
742 * Allocate transfer buffers. This function can be called with buffers
743 * already allocated when resuming from suspend, in which case it will
744 * return without touching the buffers.
745 *
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300746 * Limit the buffer size to UVC_MAX_PACKETS bulk/isochronous packets. If the
747 * system is too low on memory try successively smaller numbers of packets
748 * until allocation succeeds.
749 *
750 * Return the number of allocated packets on success or 0 when out of memory.
Laurent Pincharte01117c2008-07-04 00:36:21 -0300751 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300752static int uvc_alloc_urb_buffers(struct uvc_streaming *stream,
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300753 unsigned int size, unsigned int psize, gfp_t gfp_flags)
Laurent Pincharte01117c2008-07-04 00:36:21 -0300754{
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300755 unsigned int npackets;
Laurent Pincharte01117c2008-07-04 00:36:21 -0300756 unsigned int i;
757
758 /* Buffers are already allocated, bail out. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300759 if (stream->urb_size)
760 return stream->urb_size / psize;
Laurent Pincharte01117c2008-07-04 00:36:21 -0300761
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300762 /* Compute the number of packets. Bulk endpoints might transfer UVC
763 * payloads accross multiple URBs.
764 */
765 npackets = DIV_ROUND_UP(size, psize);
766 if (npackets > UVC_MAX_PACKETS)
767 npackets = UVC_MAX_PACKETS;
768
769 /* Retry allocations until one succeed. */
770 for (; npackets > 1; npackets /= 2) {
771 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300772 stream->urb_buffer[i] = usb_buffer_alloc(
773 stream->dev->udev, psize * npackets,
774 gfp_flags | __GFP_NOWARN, &stream->urb_dma[i]);
775 if (!stream->urb_buffer[i]) {
776 uvc_free_urb_buffers(stream);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300777 break;
778 }
779 }
780
781 if (i == UVC_URBS) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300782 stream->urb_size = psize * npackets;
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300783 return npackets;
Laurent Pincharte01117c2008-07-04 00:36:21 -0300784 }
785 }
786
Laurent Pincharte01117c2008-07-04 00:36:21 -0300787 return 0;
788}
789
790/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300791 * Uninitialize isochronous/bulk URBs and free transfer buffers.
792 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300793static void uvc_uninit_video(struct uvc_streaming *stream, int free_buffers)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300794{
795 struct urb *urb;
796 unsigned int i;
797
798 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300799 urb = stream->urb[i];
800 if (urb == NULL)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300801 continue;
802
803 usb_kill_urb(urb);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300804 usb_free_urb(urb);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300805 stream->urb[i] = NULL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300806 }
Laurent Pincharte01117c2008-07-04 00:36:21 -0300807
808 if (free_buffers)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300809 uvc_free_urb_buffers(stream);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300810}
811
812/*
813 * Initialize isochronous URBs and allocate transfer buffers. The packet size
814 * is given by the endpoint.
815 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300816static int uvc_init_video_isoc(struct uvc_streaming *stream,
Laurent Pinchart29135872008-07-04 00:35:26 -0300817 struct usb_host_endpoint *ep, gfp_t gfp_flags)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300818{
819 struct urb *urb;
820 unsigned int npackets, i, j;
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300821 u16 psize;
822 u32 size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300823
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300824 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
825 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300826 size = stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300827
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300828 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300829 if (npackets == 0)
830 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300831
832 size = npackets * psize;
833
834 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart29135872008-07-04 00:35:26 -0300835 urb = usb_alloc_urb(npackets, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300836 if (urb == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300837 uvc_uninit_video(stream, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300838 return -ENOMEM;
839 }
840
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300841 urb->dev = stream->dev->udev;
842 urb->context = stream;
843 urb->pipe = usb_rcvisocpipe(stream->dev->udev,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300844 ep->desc.bEndpointAddress);
845 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
846 urb->interval = ep->desc.bInterval;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300847 urb->transfer_buffer = stream->urb_buffer[i];
848 urb->transfer_dma = stream->urb_dma[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300849 urb->complete = uvc_video_complete;
850 urb->number_of_packets = npackets;
851 urb->transfer_buffer_length = size;
852
853 for (j = 0; j < npackets; ++j) {
854 urb->iso_frame_desc[j].offset = j * psize;
855 urb->iso_frame_desc[j].length = psize;
856 }
857
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300858 stream->urb[i] = urb;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300859 }
860
861 return 0;
862}
863
864/*
865 * Initialize bulk URBs and allocate transfer buffers. The packet size is
866 * given by the endpoint.
867 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300868static int uvc_init_video_bulk(struct uvc_streaming *stream,
Laurent Pinchart29135872008-07-04 00:35:26 -0300869 struct usb_host_endpoint *ep, gfp_t gfp_flags)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300870{
871 struct urb *urb;
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300872 unsigned int npackets, pipe, i;
873 u16 psize;
874 u32 size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300875
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300876 psize = le16_to_cpu(ep->desc.wMaxPacketSize) & 0x07ff;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300877 size = stream->ctrl.dwMaxPayloadTransferSize;
878 stream->bulk.max_payload_size = size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300879
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300880 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300881 if (npackets == 0)
Laurent Pincharte01117c2008-07-04 00:36:21 -0300882 return -ENOMEM;
883
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300884 size = npackets * psize;
885
Laurent Pinchartff924202008-12-28 22:32:29 -0300886 if (usb_endpoint_dir_in(&ep->desc))
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300887 pipe = usb_rcvbulkpipe(stream->dev->udev,
Laurent Pinchartff924202008-12-28 22:32:29 -0300888 ep->desc.bEndpointAddress);
889 else
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300890 pipe = usb_sndbulkpipe(stream->dev->udev,
Laurent Pinchartff924202008-12-28 22:32:29 -0300891 ep->desc.bEndpointAddress);
892
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300893 if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
Laurent Pinchartff924202008-12-28 22:32:29 -0300894 size = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300895
896 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart29135872008-07-04 00:35:26 -0300897 urb = usb_alloc_urb(0, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300898 if (urb == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300899 uvc_uninit_video(stream, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300900 return -ENOMEM;
901 }
902
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300903 usb_fill_bulk_urb(urb, stream->dev->udev, pipe,
904 stream->urb_buffer[i], size, uvc_video_complete,
905 stream);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300906 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300907 urb->transfer_dma = stream->urb_dma[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300908
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300909 stream->urb[i] = urb;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300910 }
911
912 return 0;
913}
914
915/*
916 * Initialize isochronous/bulk URBs and allocate transfer buffers.
917 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300918static int uvc_init_video(struct uvc_streaming *stream, gfp_t gfp_flags)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300919{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300920 struct usb_interface *intf = stream->intf;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300921 struct usb_host_interface *alts;
922 struct usb_host_endpoint *ep = NULL;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300923 int intfnum = stream->intfnum;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300924 unsigned int bandwidth, psize, i;
925 int ret;
926
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300927 stream->last_fid = -1;
928 stream->bulk.header_size = 0;
929 stream->bulk.skip_payload = 0;
930 stream->bulk.payload_size = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300931
932 if (intf->num_altsetting > 1) {
933 /* Isochronous endpoint, select the alternate setting. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300934 bandwidth = stream->ctrl.dwMaxPayloadTransferSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300935
936 if (bandwidth == 0) {
937 uvc_printk(KERN_WARNING, "device %s requested null "
938 "bandwidth, defaulting to lowest.\n",
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300939 stream->dev->name);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300940 bandwidth = 1;
941 }
942
943 for (i = 0; i < intf->num_altsetting; ++i) {
944 alts = &intf->altsetting[i];
945 ep = uvc_find_endpoint(alts,
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300946 stream->header.bEndpointAddress);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300947 if (ep == NULL)
948 continue;
949
950 /* Check if the bandwidth is high enough. */
951 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
952 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
953 if (psize >= bandwidth)
954 break;
955 }
956
957 if (i >= intf->num_altsetting)
958 return -EIO;
959
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300960 ret = usb_set_interface(stream->dev->udev, intfnum, i);
961 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300962 return ret;
963
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300964 ret = uvc_init_video_isoc(stream, ep, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300965 } else {
966 /* Bulk endpoint, proceed to URB initialization. */
967 ep = uvc_find_endpoint(&intf->altsetting[0],
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300968 stream->header.bEndpointAddress);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300969 if (ep == NULL)
970 return -EIO;
971
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300972 ret = uvc_init_video_bulk(stream, ep, gfp_flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300973 }
974
975 if (ret < 0)
976 return ret;
977
978 /* Submit the URBs. */
979 for (i = 0; i < UVC_URBS; ++i) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300980 ret = usb_submit_urb(stream->urb[i], gfp_flags);
981 if (ret < 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300982 uvc_printk(KERN_ERR, "Failed to submit URB %u "
983 "(%d).\n", i, ret);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300984 uvc_uninit_video(stream, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300985 return ret;
986 }
987 }
988
989 return 0;
990}
991
992/* --------------------------------------------------------------------------
993 * Suspend/resume
994 */
995
996/*
997 * Stop streaming without disabling the video queue.
998 *
999 * To let userspace applications resume without trouble, we must not touch the
1000 * video buffers in any way. We mark the device as frozen to make sure the URB
1001 * completion handler won't try to cancel the queue when we kill the URBs.
1002 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001003int uvc_video_suspend(struct uvc_streaming *stream)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001004{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001005 if (!uvc_queue_streaming(&stream->queue))
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001006 return 0;
1007
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001008 stream->frozen = 1;
1009 uvc_uninit_video(stream, 0);
1010 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001011 return 0;
1012}
1013
1014/*
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03001015 * Reconfigure the video interface and restart streaming if it was enabled
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001016 * before suspend.
1017 *
1018 * If an error occurs, disable the video queue. This will wake all pending
1019 * buffers, making sure userspace applications are notified of the problem
1020 * instead of waiting forever.
1021 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001022int uvc_video_resume(struct uvc_streaming *stream)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001023{
1024 int ret;
1025
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001026 stream->frozen = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001027
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001028 ret = uvc_commit_video(stream, &stream->ctrl);
1029 if (ret < 0) {
1030 uvc_queue_enable(&stream->queue, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001031 return ret;
1032 }
1033
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001034 if (!uvc_queue_streaming(&stream->queue))
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001035 return 0;
1036
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001037 ret = uvc_init_video(stream, GFP_NOIO);
1038 if (ret < 0)
1039 uvc_queue_enable(&stream->queue, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001040
1041 return ret;
1042}
1043
1044/* ------------------------------------------------------------------------
1045 * Video device
1046 */
1047
1048/*
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03001049 * Initialize the UVC video device by switching to alternate setting 0 and
1050 * retrieve the default format.
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001051 *
1052 * Some cameras (namely the Fuji Finepix) set the format and frame
1053 * indexes to zero. The UVC standard doesn't clearly make this a spec
1054 * violation, so try to silently fix the values if possible.
1055 *
1056 * This function is called before registering the device with V4L.
1057 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001058int uvc_video_init(struct uvc_streaming *stream)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001059{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001060 struct uvc_streaming_control *probe = &stream->ctrl;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001061 struct uvc_format *format = NULL;
1062 struct uvc_frame *frame = NULL;
1063 unsigned int i;
1064 int ret;
1065
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001066 if (stream->nformats == 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001067 uvc_printk(KERN_INFO, "No supported video formats found.\n");
1068 return -EINVAL;
1069 }
1070
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001071 atomic_set(&stream->active, 0);
1072
1073 /* Initialize the video buffers queue. */
1074 uvc_queue_init(&stream->queue, stream->type);
1075
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001076 /* Alternate setting 0 should be the default, yet the XBox Live Vision
1077 * Cam (and possibly other devices) crash or otherwise misbehave if
1078 * they don't receive a SET_INTERFACE request before any other video
1079 * control request.
1080 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001081 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001082
Laurent Pinchart72362422009-02-14 19:26:56 -03001083 /* Set the streaming probe control with default streaming parameters
1084 * retrieved from the device. Webcams that don't suport GET_DEF
1085 * requests on the probe control will just keep their current streaming
1086 * parameters.
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001087 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001088 if (uvc_get_video_ctrl(stream, probe, 1, UVC_GET_DEF) == 0)
1089 uvc_set_video_ctrl(stream, probe, 1);
Laurent Pinchart72362422009-02-14 19:26:56 -03001090
1091 /* Initialize the streaming parameters with the probe control current
1092 * value. This makes sure SET_CUR requests on the streaming commit
1093 * control will always use values retrieved from a successful GET_CUR
1094 * request on the probe control, as required by the UVC specification.
1095 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001096 ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
Laurent Pinchartb482d922009-06-26 11:39:42 -03001097 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001098 return ret;
1099
1100 /* Check if the default format descriptor exists. Use the first
1101 * available format otherwise.
1102 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001103 for (i = stream->nformats; i > 0; --i) {
1104 format = &stream->format[i-1];
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001105 if (format->index == probe->bFormatIndex)
1106 break;
1107 }
1108
1109 if (format->nframes == 0) {
1110 uvc_printk(KERN_INFO, "No frame descriptor found for the "
1111 "default format.\n");
1112 return -EINVAL;
1113 }
1114
1115 /* Zero bFrameIndex might be correct. Stream-based formats (including
1116 * MPEG-2 TS and DV) do not support frames but have a dummy frame
1117 * descriptor with bFrameIndex set to zero. If the default frame
Laurent Pinchart078f8942009-05-06 12:30:30 -03001118 * descriptor is not found, use the first available frame.
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001119 */
1120 for (i = format->nframes; i > 0; --i) {
1121 frame = &format->frame[i-1];
1122 if (frame->bFrameIndex == probe->bFrameIndex)
1123 break;
1124 }
1125
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001126 probe->bFormatIndex = format->index;
1127 probe->bFrameIndex = frame->bFrameIndex;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001128
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001129 stream->cur_format = format;
1130 stream->cur_frame = frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001131
1132 /* Select the video decoding function */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001133 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1134 if (stream->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
1135 stream->decode = uvc_video_decode_isight;
1136 else if (stream->intf->num_altsetting > 1)
1137 stream->decode = uvc_video_decode_isoc;
Laurent Pinchartff924202008-12-28 22:32:29 -03001138 else
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001139 stream->decode = uvc_video_decode_bulk;
Laurent Pinchartff924202008-12-28 22:32:29 -03001140 } else {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001141 if (stream->intf->num_altsetting == 1)
1142 stream->decode = uvc_video_encode_bulk;
Laurent Pinchartff924202008-12-28 22:32:29 -03001143 else {
1144 uvc_printk(KERN_INFO, "Isochronous endpoints are not "
1145 "supported for video output devices.\n");
1146 return -EINVAL;
1147 }
1148 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001149
1150 return 0;
1151}
1152
1153/*
1154 * Enable or disable the video stream.
1155 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001156int uvc_video_enable(struct uvc_streaming *stream, int enable)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001157{
1158 int ret;
1159
1160 if (!enable) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001161 uvc_uninit_video(stream, 1);
1162 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1163 uvc_queue_enable(&stream->queue, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001164 return 0;
1165 }
1166
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001167 if ((stream->cur_format->flags & UVC_FMT_FLAG_COMPRESSED) ||
Laurent Pinchart0fbd8ee2008-12-06 16:25:14 -03001168 uvc_no_drop_param)
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001169 stream->queue.flags &= ~UVC_QUEUE_DROP_INCOMPLETE;
Laurent Pinchartd63beb92008-09-15 22:24:29 -03001170 else
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001171 stream->queue.flags |= UVC_QUEUE_DROP_INCOMPLETE;
Laurent Pinchartd63beb92008-09-15 22:24:29 -03001172
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001173 ret = uvc_queue_enable(&stream->queue, 1);
1174 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001175 return ret;
1176
Laurent Pinchart23867b22008-11-12 11:46:43 -03001177 /* Commit the streaming parameters. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001178 ret = uvc_commit_video(stream, &stream->ctrl);
1179 if (ret < 0)
Laurent Pinchart23867b22008-11-12 11:46:43 -03001180 return ret;
1181
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001182 return uvc_init_video(stream, GFP_KERNEL);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001183}
Hans Verkuilf87086e2008-07-18 00:50:58 -03001184