Commit | Line | Data |
---|---|---|
c0efd232 LP |
1 | /* |
2 | * uvc_video.c -- USB Video Class driver - Video handling | |
3 | * | |
11fc5baf LP |
4 | * Copyright (C) 2005-2010 |
5 | * Laurent Pinchart (laurent.pinchart@ideasonboard.com) | |
c0efd232 LP |
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> | |
c0efd232 LP |
15 | #include <linux/list.h> |
16 | #include <linux/module.h> | |
5a0e3ad6 | 17 | #include <linux/slab.h> |
c0efd232 LP |
18 | #include <linux/usb.h> |
19 | #include <linux/videodev2.h> | |
20 | #include <linux/vmalloc.h> | |
21 | #include <linux/wait.h> | |
60063497 | 22 | #include <linux/atomic.h> |
c0efd232 LP |
23 | #include <asm/unaligned.h> |
24 | ||
25 | #include <media/v4l2-common.h> | |
26 | ||
27 | #include "uvcvideo.h" | |
28 | ||
29 | /* ------------------------------------------------------------------------ | |
30 | * UVC Controls | |
31 | */ | |
32 | ||
33 | static 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; | |
c0efd232 LP |
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 | ||
44f0079e | 44 | return usb_control_msg(dev->udev, pipe, query, type, cs << 8, |
c0efd232 | 45 | unit << 8 | intfnum, data, size, timeout); |
44f0079e LP |
46 | } |
47 | ||
707dcd90 LP |
48 | static const char *uvc_query_name(__u8 query) |
49 | { | |
50 | switch (query) { | |
51 | case UVC_SET_CUR: | |
52 | return "SET_CUR"; | |
53 | case UVC_GET_CUR: | |
54 | return "GET_CUR"; | |
55 | case UVC_GET_MIN: | |
56 | return "GET_MIN"; | |
57 | case UVC_GET_MAX: | |
58 | return "GET_MAX"; | |
59 | case UVC_GET_RES: | |
60 | return "GET_RES"; | |
61 | case UVC_GET_LEN: | |
62 | return "GET_LEN"; | |
63 | case UVC_GET_INFO: | |
64 | return "GET_INFO"; | |
65 | case UVC_GET_DEF: | |
66 | return "GET_DEF"; | |
67 | default: | |
68 | return "<invalid>"; | |
69 | } | |
70 | } | |
71 | ||
44f0079e LP |
72 | int uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit, |
73 | __u8 intfnum, __u8 cs, void *data, __u16 size) | |
74 | { | |
75 | int ret; | |
c0efd232 | 76 | |
44f0079e LP |
77 | ret = __uvc_query_ctrl(dev, query, unit, intfnum, cs, data, size, |
78 | UVC_CTRL_CONTROL_TIMEOUT); | |
c0efd232 | 79 | if (ret != size) { |
707dcd90 LP |
80 | uvc_printk(KERN_ERR, "Failed to query (%s) UVC control %u on " |
81 | "unit %u: %d (exp. %u).\n", uvc_query_name(query), cs, | |
82 | unit, ret, size); | |
c0efd232 LP |
83 | return -EIO; |
84 | } | |
85 | ||
86 | return 0; | |
87 | } | |
88 | ||
35f02a68 | 89 | static void uvc_fixup_video_ctrl(struct uvc_streaming *stream, |
c0efd232 LP |
90 | struct uvc_streaming_control *ctrl) |
91 | { | |
38a66824 | 92 | struct uvc_format *format = NULL; |
078f8947 LP |
93 | struct uvc_frame *frame = NULL; |
94 | unsigned int i; | |
c0efd232 | 95 | |
38a66824 SL |
96 | for (i = 0; i < stream->nformats; ++i) { |
97 | if (stream->format[i].index == ctrl->bFormatIndex) { | |
98 | format = &stream->format[i]; | |
99 | break; | |
100 | } | |
101 | } | |
c0efd232 | 102 | |
38a66824 SL |
103 | if (format == NULL) |
104 | return; | |
c0efd232 | 105 | |
078f8947 LP |
106 | for (i = 0; i < format->nframes; ++i) { |
107 | if (format->frame[i].bFrameIndex == ctrl->bFrameIndex) { | |
108 | frame = &format->frame[i]; | |
109 | break; | |
110 | } | |
111 | } | |
c0efd232 | 112 | |
078f8947 LP |
113 | if (frame == NULL) |
114 | return; | |
c0efd232 LP |
115 | |
116 | if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) || | |
117 | (ctrl->dwMaxVideoFrameSize == 0 && | |
35f02a68 | 118 | stream->dev->uvc_version < 0x0110)) |
c0efd232 LP |
119 | ctrl->dwMaxVideoFrameSize = |
120 | frame->dwMaxVideoFrameBufferSize; | |
50144aee | 121 | |
a2e35af6 LP |
122 | if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) && |
123 | stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH && | |
35f02a68 | 124 | stream->intf->num_altsetting > 1) { |
50144aee LP |
125 | u32 interval; |
126 | u32 bandwidth; | |
127 | ||
128 | interval = (ctrl->dwFrameInterval > 100000) | |
129 | ? ctrl->dwFrameInterval | |
130 | : frame->dwFrameInterval[0]; | |
131 | ||
132 | /* Compute a bandwidth estimation by multiplying the frame | |
133 | * size by the number of video frames per second, divide the | |
134 | * result by the number of USB frames (or micro-frames for | |
135 | * high-speed devices) per second and add the UVC header size | |
136 | * (assumed to be 12 bytes long). | |
137 | */ | |
138 | bandwidth = frame->wWidth * frame->wHeight / 8 * format->bpp; | |
139 | bandwidth *= 10000000 / interval + 1; | |
140 | bandwidth /= 1000; | |
35f02a68 | 141 | if (stream->dev->udev->speed == USB_SPEED_HIGH) |
50144aee LP |
142 | bandwidth /= 8; |
143 | bandwidth += 12; | |
144 | ||
9bb7262d LP |
145 | /* The bandwidth estimate is too low for many cameras. Don't use |
146 | * maximum packet sizes lower than 1024 bytes to try and work | |
147 | * around the problem. According to measurements done on two | |
148 | * different camera models, the value is high enough to get most | |
149 | * resolutions working while not preventing two simultaneous | |
150 | * VGA streams at 15 fps. | |
151 | */ | |
152 | bandwidth = max_t(u32, bandwidth, 1024); | |
153 | ||
50144aee LP |
154 | ctrl->dwMaxPayloadTransferSize = bandwidth; |
155 | } | |
c0efd232 LP |
156 | } |
157 | ||
35f02a68 | 158 | static int uvc_get_video_ctrl(struct uvc_streaming *stream, |
c0efd232 LP |
159 | struct uvc_streaming_control *ctrl, int probe, __u8 query) |
160 | { | |
04793dd0 LP |
161 | __u8 *data; |
162 | __u16 size; | |
c0efd232 LP |
163 | int ret; |
164 | ||
35f02a68 | 165 | size = stream->dev->uvc_version >= 0x0110 ? 34 : 26; |
d2be7643 JL |
166 | if ((stream->dev->quirks & UVC_QUIRK_PROBE_DEF) && |
167 | query == UVC_GET_DEF) | |
168 | return -EIO; | |
169 | ||
04793dd0 LP |
170 | data = kmalloc(size, GFP_KERNEL); |
171 | if (data == NULL) | |
172 | return -ENOMEM; | |
173 | ||
35f02a68 | 174 | ret = __uvc_query_ctrl(stream->dev, query, 0, stream->intfnum, |
b482d923 | 175 | probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data, |
b232a012 | 176 | size, uvc_timeout_param); |
44f0079e | 177 | |
b482d923 | 178 | if ((query == UVC_GET_MIN || query == UVC_GET_MAX) && ret == 2) { |
44f0079e LP |
179 | /* Some cameras, mostly based on Bison Electronics chipsets, |
180 | * answer a GET_MIN or GET_MAX request with the wCompQuality | |
181 | * field only. | |
182 | */ | |
35f02a68 | 183 | uvc_warn_once(stream->dev, UVC_WARN_MINMAX, "UVC non " |
44f0079e LP |
184 | "compliance - GET_MIN/MAX(PROBE) incorrectly " |
185 | "supported. Enabling workaround.\n"); | |
ddb0b34f | 186 | memset(ctrl, 0, sizeof *ctrl); |
44f0079e LP |
187 | ctrl->wCompQuality = le16_to_cpup((__le16 *)data); |
188 | ret = 0; | |
04793dd0 | 189 | goto out; |
b482d923 | 190 | } else if (query == UVC_GET_DEF && probe == 1 && ret != size) { |
44f0079e LP |
191 | /* Many cameras don't support the GET_DEF request on their |
192 | * video probe control. Warn once and return, the caller will | |
193 | * fall back to GET_CUR. | |
194 | */ | |
35f02a68 | 195 | uvc_warn_once(stream->dev, UVC_WARN_PROBE_DEF, "UVC non " |
44f0079e LP |
196 | "compliance - GET_DEF(PROBE) not supported. " |
197 | "Enabling workaround.\n"); | |
198 | ret = -EIO; | |
199 | goto out; | |
200 | } else if (ret != size) { | |
201 | uvc_printk(KERN_ERR, "Failed to query (%u) UVC %s control : " | |
202 | "%d (exp. %u).\n", query, probe ? "probe" : "commit", | |
203 | ret, size); | |
204 | ret = -EIO; | |
205 | goto out; | |
206 | } | |
c0efd232 LP |
207 | |
208 | ctrl->bmHint = le16_to_cpup((__le16 *)&data[0]); | |
209 | ctrl->bFormatIndex = data[2]; | |
210 | ctrl->bFrameIndex = data[3]; | |
211 | ctrl->dwFrameInterval = le32_to_cpup((__le32 *)&data[4]); | |
212 | ctrl->wKeyFrameRate = le16_to_cpup((__le16 *)&data[8]); | |
213 | ctrl->wPFrameRate = le16_to_cpup((__le16 *)&data[10]); | |
214 | ctrl->wCompQuality = le16_to_cpup((__le16 *)&data[12]); | |
215 | ctrl->wCompWindowSize = le16_to_cpup((__le16 *)&data[14]); | |
216 | ctrl->wDelay = le16_to_cpup((__le16 *)&data[16]); | |
4d3939f6 LP |
217 | ctrl->dwMaxVideoFrameSize = get_unaligned_le32(&data[18]); |
218 | ctrl->dwMaxPayloadTransferSize = get_unaligned_le32(&data[22]); | |
c0efd232 LP |
219 | |
220 | if (size == 34) { | |
4d3939f6 | 221 | ctrl->dwClockFrequency = get_unaligned_le32(&data[26]); |
c0efd232 LP |
222 | ctrl->bmFramingInfo = data[30]; |
223 | ctrl->bPreferedVersion = data[31]; | |
224 | ctrl->bMinVersion = data[32]; | |
225 | ctrl->bMaxVersion = data[33]; | |
226 | } else { | |
35f02a68 | 227 | ctrl->dwClockFrequency = stream->dev->clock_frequency; |
c0efd232 LP |
228 | ctrl->bmFramingInfo = 0; |
229 | ctrl->bPreferedVersion = 0; | |
230 | ctrl->bMinVersion = 0; | |
231 | ctrl->bMaxVersion = 0; | |
232 | } | |
233 | ||
50144aee LP |
234 | /* Some broken devices return null or wrong dwMaxVideoFrameSize and |
235 | * dwMaxPayloadTransferSize fields. Try to get the value from the | |
236 | * format and frame descriptors. | |
c0efd232 | 237 | */ |
35f02a68 | 238 | uvc_fixup_video_ctrl(stream, ctrl); |
44f0079e | 239 | ret = 0; |
c0efd232 | 240 | |
04793dd0 LP |
241 | out: |
242 | kfree(data); | |
243 | return ret; | |
c0efd232 LP |
244 | } |
245 | ||
35f02a68 | 246 | static int uvc_set_video_ctrl(struct uvc_streaming *stream, |
c0efd232 LP |
247 | struct uvc_streaming_control *ctrl, int probe) |
248 | { | |
04793dd0 LP |
249 | __u8 *data; |
250 | __u16 size; | |
251 | int ret; | |
c0efd232 | 252 | |
35f02a68 | 253 | size = stream->dev->uvc_version >= 0x0110 ? 34 : 26; |
04793dd0 LP |
254 | data = kzalloc(size, GFP_KERNEL); |
255 | if (data == NULL) | |
256 | return -ENOMEM; | |
c0efd232 LP |
257 | |
258 | *(__le16 *)&data[0] = cpu_to_le16(ctrl->bmHint); | |
259 | data[2] = ctrl->bFormatIndex; | |
260 | data[3] = ctrl->bFrameIndex; | |
261 | *(__le32 *)&data[4] = cpu_to_le32(ctrl->dwFrameInterval); | |
262 | *(__le16 *)&data[8] = cpu_to_le16(ctrl->wKeyFrameRate); | |
263 | *(__le16 *)&data[10] = cpu_to_le16(ctrl->wPFrameRate); | |
264 | *(__le16 *)&data[12] = cpu_to_le16(ctrl->wCompQuality); | |
265 | *(__le16 *)&data[14] = cpu_to_le16(ctrl->wCompWindowSize); | |
266 | *(__le16 *)&data[16] = cpu_to_le16(ctrl->wDelay); | |
4d3939f6 LP |
267 | put_unaligned_le32(ctrl->dwMaxVideoFrameSize, &data[18]); |
268 | put_unaligned_le32(ctrl->dwMaxPayloadTransferSize, &data[22]); | |
c0efd232 LP |
269 | |
270 | if (size == 34) { | |
4d3939f6 | 271 | put_unaligned_le32(ctrl->dwClockFrequency, &data[26]); |
c0efd232 LP |
272 | data[30] = ctrl->bmFramingInfo; |
273 | data[31] = ctrl->bPreferedVersion; | |
274 | data[32] = ctrl->bMinVersion; | |
275 | data[33] = ctrl->bMaxVersion; | |
276 | } | |
277 | ||
35f02a68 | 278 | ret = __uvc_query_ctrl(stream->dev, UVC_SET_CUR, 0, stream->intfnum, |
b482d923 | 279 | probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data, |
b232a012 | 280 | size, uvc_timeout_param); |
44f0079e LP |
281 | if (ret != size) { |
282 | uvc_printk(KERN_ERR, "Failed to set UVC %s control : " | |
283 | "%d (exp. %u).\n", probe ? "probe" : "commit", | |
284 | ret, size); | |
285 | ret = -EIO; | |
286 | } | |
04793dd0 LP |
287 | |
288 | kfree(data); | |
289 | return ret; | |
c0efd232 LP |
290 | } |
291 | ||
35f02a68 | 292 | int uvc_probe_video(struct uvc_streaming *stream, |
c0efd232 LP |
293 | struct uvc_streaming_control *probe) |
294 | { | |
295 | struct uvc_streaming_control probe_min, probe_max; | |
296 | __u16 bandwidth; | |
297 | unsigned int i; | |
298 | int ret; | |
299 | ||
c0efd232 LP |
300 | /* Perform probing. The device should adjust the requested values |
301 | * according to its capabilities. However, some devices, namely the | |
302 | * first generation UVC Logitech webcams, don't implement the Video | |
303 | * Probe control properly, and just return the needed bandwidth. For | |
304 | * that reason, if the needed bandwidth exceeds the maximum available | |
305 | * bandwidth, try to lower the quality. | |
306 | */ | |
35f02a68 LP |
307 | ret = uvc_set_video_ctrl(stream, probe, 1); |
308 | if (ret < 0) | |
c0efd232 LP |
309 | goto done; |
310 | ||
311 | /* Get the minimum and maximum values for compression settings. */ | |
35f02a68 LP |
312 | if (!(stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX)) { |
313 | ret = uvc_get_video_ctrl(stream, &probe_min, 1, UVC_GET_MIN); | |
c0efd232 LP |
314 | if (ret < 0) |
315 | goto done; | |
35f02a68 | 316 | ret = uvc_get_video_ctrl(stream, &probe_max, 1, UVC_GET_MAX); |
c0efd232 LP |
317 | if (ret < 0) |
318 | goto done; | |
319 | ||
320 | probe->wCompQuality = probe_max.wCompQuality; | |
321 | } | |
322 | ||
323 | for (i = 0; i < 2; ++i) { | |
35f02a68 | 324 | ret = uvc_set_video_ctrl(stream, probe, 1); |
b482d923 LP |
325 | if (ret < 0) |
326 | goto done; | |
35f02a68 | 327 | ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR); |
b482d923 | 328 | if (ret < 0) |
c0efd232 LP |
329 | goto done; |
330 | ||
35f02a68 | 331 | if (stream->intf->num_altsetting == 1) |
c0efd232 LP |
332 | break; |
333 | ||
334 | bandwidth = probe->dwMaxPayloadTransferSize; | |
35f02a68 | 335 | if (bandwidth <= stream->maxpsize) |
c0efd232 LP |
336 | break; |
337 | ||
35f02a68 | 338 | if (stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX) { |
c0efd232 LP |
339 | ret = -ENOSPC; |
340 | goto done; | |
341 | } | |
342 | ||
343 | /* TODO: negotiate compression parameters */ | |
344 | probe->wKeyFrameRate = probe_min.wKeyFrameRate; | |
345 | probe->wPFrameRate = probe_min.wPFrameRate; | |
346 | probe->wCompQuality = probe_max.wCompQuality; | |
347 | probe->wCompWindowSize = probe_min.wCompWindowSize; | |
348 | } | |
349 | ||
350 | done: | |
c0efd232 LP |
351 | return ret; |
352 | } | |
353 | ||
0c6a3b26 LP |
354 | static int uvc_commit_video(struct uvc_streaming *stream, |
355 | struct uvc_streaming_control *probe) | |
44f0079e | 356 | { |
35f02a68 | 357 | return uvc_set_video_ctrl(stream, probe, 0); |
44f0079e LP |
358 | } |
359 | ||
66847ef0 LP |
360 | /* ----------------------------------------------------------------------------- |
361 | * Clocks and timestamps | |
362 | */ | |
363 | ||
3b35fc81 OL |
364 | static inline void uvc_video_get_ts(struct timespec *ts) |
365 | { | |
366 | if (uvc_clock_param == CLOCK_MONOTONIC) | |
367 | ktime_get_ts(ts); | |
368 | else | |
369 | ktime_get_real_ts(ts); | |
370 | } | |
371 | ||
66847ef0 LP |
372 | static void |
373 | uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, | |
374 | const __u8 *data, int len) | |
375 | { | |
376 | struct uvc_clock_sample *sample; | |
377 | unsigned int header_size; | |
378 | bool has_pts = false; | |
379 | bool has_scr = false; | |
380 | unsigned long flags; | |
381 | struct timespec ts; | |
382 | u16 host_sof; | |
383 | u16 dev_sof; | |
384 | ||
385 | switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) { | |
386 | case UVC_STREAM_PTS | UVC_STREAM_SCR: | |
387 | header_size = 12; | |
388 | has_pts = true; | |
389 | has_scr = true; | |
390 | break; | |
391 | case UVC_STREAM_PTS: | |
392 | header_size = 6; | |
393 | has_pts = true; | |
394 | break; | |
395 | case UVC_STREAM_SCR: | |
396 | header_size = 8; | |
397 | has_scr = true; | |
398 | break; | |
399 | default: | |
400 | header_size = 2; | |
401 | break; | |
402 | } | |
403 | ||
404 | /* Check for invalid headers. */ | |
405 | if (len < header_size) | |
406 | return; | |
407 | ||
408 | /* Extract the timestamps: | |
409 | * | |
410 | * - store the frame PTS in the buffer structure | |
411 | * - if the SCR field is present, retrieve the host SOF counter and | |
412 | * kernel timestamps and store them with the SCR STC and SOF fields | |
413 | * in the ring buffer | |
414 | */ | |
415 | if (has_pts && buf != NULL) | |
416 | buf->pts = get_unaligned_le32(&data[2]); | |
417 | ||
418 | if (!has_scr) | |
419 | return; | |
420 | ||
421 | /* To limit the amount of data, drop SCRs with an SOF identical to the | |
422 | * previous one. | |
423 | */ | |
424 | dev_sof = get_unaligned_le16(&data[header_size - 2]); | |
425 | if (dev_sof == stream->clock.last_sof) | |
426 | return; | |
427 | ||
428 | stream->clock.last_sof = dev_sof; | |
429 | ||
430 | host_sof = usb_get_current_frame_number(stream->dev->udev); | |
3b35fc81 | 431 | uvc_video_get_ts(&ts); |
66847ef0 LP |
432 | |
433 | /* The UVC specification allows device implementations that can't obtain | |
434 | * the USB frame number to keep their own frame counters as long as they | |
435 | * match the size and frequency of the frame number associated with USB | |
436 | * SOF tokens. The SOF values sent by such devices differ from the USB | |
437 | * SOF tokens by a fixed offset that needs to be estimated and accounted | |
438 | * for to make timestamp recovery as accurate as possible. | |
439 | * | |
440 | * The offset is estimated the first time a device SOF value is received | |
441 | * as the difference between the host and device SOF values. As the two | |
442 | * SOF values can differ slightly due to transmission delays, consider | |
443 | * that the offset is null if the difference is not higher than 10 ms | |
444 | * (negative differences can not happen and are thus considered as an | |
445 | * offset). The video commit control wDelay field should be used to | |
446 | * compute a dynamic threshold instead of using a fixed 10 ms value, but | |
447 | * devices don't report reliable wDelay values. | |
448 | * | |
449 | * See uvc_video_clock_host_sof() for an explanation regarding why only | |
450 | * the 8 LSBs of the delta are kept. | |
451 | */ | |
452 | if (stream->clock.sof_offset == (u16)-1) { | |
453 | u16 delta_sof = (host_sof - dev_sof) & 255; | |
454 | if (delta_sof >= 10) | |
455 | stream->clock.sof_offset = delta_sof; | |
456 | else | |
457 | stream->clock.sof_offset = 0; | |
458 | } | |
459 | ||
460 | dev_sof = (dev_sof + stream->clock.sof_offset) & 2047; | |
461 | ||
462 | spin_lock_irqsave(&stream->clock.lock, flags); | |
463 | ||
464 | sample = &stream->clock.samples[stream->clock.head]; | |
465 | sample->dev_stc = get_unaligned_le32(&data[header_size - 6]); | |
466 | sample->dev_sof = dev_sof; | |
467 | sample->host_sof = host_sof; | |
468 | sample->host_ts = ts; | |
469 | ||
470 | /* Update the sliding window head and count. */ | |
471 | stream->clock.head = (stream->clock.head + 1) % stream->clock.size; | |
472 | ||
473 | if (stream->clock.count < stream->clock.size) | |
474 | stream->clock.count++; | |
475 | ||
476 | spin_unlock_irqrestore(&stream->clock.lock, flags); | |
477 | } | |
478 | ||
ed0ee0ce | 479 | static void uvc_video_clock_reset(struct uvc_streaming *stream) |
66847ef0 LP |
480 | { |
481 | struct uvc_clock *clock = &stream->clock; | |
482 | ||
66847ef0 LP |
483 | clock->head = 0; |
484 | clock->count = 0; | |
66847ef0 LP |
485 | clock->last_sof = -1; |
486 | clock->sof_offset = -1; | |
ed0ee0ce LP |
487 | } |
488 | ||
489 | static int uvc_video_clock_init(struct uvc_streaming *stream) | |
490 | { | |
491 | struct uvc_clock *clock = &stream->clock; | |
492 | ||
493 | spin_lock_init(&clock->lock); | |
494 | clock->size = 32; | |
66847ef0 LP |
495 | |
496 | clock->samples = kmalloc(clock->size * sizeof(*clock->samples), | |
497 | GFP_KERNEL); | |
498 | if (clock->samples == NULL) | |
499 | return -ENOMEM; | |
500 | ||
ed0ee0ce LP |
501 | uvc_video_clock_reset(stream); |
502 | ||
66847ef0 LP |
503 | return 0; |
504 | } | |
505 | ||
506 | static void uvc_video_clock_cleanup(struct uvc_streaming *stream) | |
507 | { | |
508 | kfree(stream->clock.samples); | |
509 | stream->clock.samples = NULL; | |
510 | } | |
511 | ||
512 | /* | |
513 | * uvc_video_clock_host_sof - Return the host SOF value for a clock sample | |
514 | * | |
515 | * Host SOF counters reported by usb_get_current_frame_number() usually don't | |
516 | * cover the whole 11-bits SOF range (0-2047) but are limited to the HCI frame | |
517 | * schedule window. They can be limited to 8, 9 or 10 bits depending on the host | |
518 | * controller and its configuration. | |
519 | * | |
520 | * We thus need to recover the SOF value corresponding to the host frame number. | |
521 | * As the device and host frame numbers are sampled in a short interval, the | |
522 | * difference between their values should be equal to a small delta plus an | |
523 | * integer multiple of 256 caused by the host frame number limited precision. | |
524 | * | |
525 | * To obtain the recovered host SOF value, compute the small delta by masking | |
526 | * the high bits of the host frame counter and device SOF difference and add it | |
527 | * to the device SOF value. | |
528 | */ | |
529 | static u16 uvc_video_clock_host_sof(const struct uvc_clock_sample *sample) | |
530 | { | |
531 | /* The delta value can be negative. */ | |
532 | s8 delta_sof; | |
533 | ||
534 | delta_sof = (sample->host_sof - sample->dev_sof) & 255; | |
535 | ||
536 | return (sample->dev_sof + delta_sof) & 2047; | |
537 | } | |
538 | ||
539 | /* | |
540 | * uvc_video_clock_update - Update the buffer timestamp | |
541 | * | |
542 | * This function converts the buffer PTS timestamp to the host clock domain by | |
543 | * going through the USB SOF clock domain and stores the result in the V4L2 | |
544 | * buffer timestamp field. | |
545 | * | |
546 | * The relationship between the device clock and the host clock isn't known. | |
547 | * However, the device and the host share the common USB SOF clock which can be | |
548 | * used to recover that relationship. | |
549 | * | |
550 | * The relationship between the device clock and the USB SOF clock is considered | |
551 | * to be linear over the clock samples sliding window and is given by | |
552 | * | |
553 | * SOF = m * PTS + p | |
554 | * | |
555 | * Several methods to compute the slope (m) and intercept (p) can be used. As | |
556 | * the clock drift should be small compared to the sliding window size, we | |
557 | * assume that the line that goes through the points at both ends of the window | |
558 | * is a good approximation. Naming those points P1 and P2, we get | |
559 | * | |
560 | * SOF = (SOF2 - SOF1) / (STC2 - STC1) * PTS | |
561 | * + (SOF1 * STC2 - SOF2 * STC1) / (STC2 - STC1) | |
562 | * | |
563 | * or | |
564 | * | |
565 | * SOF = ((SOF2 - SOF1) * PTS + SOF1 * STC2 - SOF2 * STC1) / (STC2 - STC1) (1) | |
566 | * | |
39c1cb2b | 567 | * to avoid losing precision in the division. Similarly, the host timestamp is |
66847ef0 LP |
568 | * computed with |
569 | * | |
570 | * TS = ((TS2 - TS1) * PTS + TS1 * SOF2 - TS2 * SOF1) / (SOF2 - SOF1) (2) | |
571 | * | |
572 | * SOF values are coded on 11 bits by USB. We extend their precision with 16 | |
573 | * decimal bits, leading to a 11.16 coding. | |
574 | * | |
575 | * TODO: To avoid surprises with device clock values, PTS/STC timestamps should | |
576 | * be normalized using the nominal device clock frequency reported through the | |
577 | * UVC descriptors. | |
578 | * | |
579 | * Both the PTS/STC and SOF counters roll over, after a fixed but device | |
580 | * specific amount of time for PTS/STC and after 2048ms for SOF. As long as the | |
581 | * sliding window size is smaller than the rollover period, differences computed | |
582 | * on unsigned integers will produce the correct result. However, the p term in | |
583 | * the linear relations will be miscomputed. | |
584 | * | |
585 | * To fix the issue, we subtract a constant from the PTS and STC values to bring | |
586 | * PTS to half the 32 bit STC range. The sliding window STC values then fit into | |
587 | * the 32 bit range without any rollover. | |
588 | * | |
589 | * Similarly, we add 2048 to the device SOF values to make sure that the SOF | |
590 | * computed by (1) will never be smaller than 0. This offset is then compensated | |
591 | * by adding 2048 to the SOF values used in (2). However, this doesn't prevent | |
592 | * rollovers between (1) and (2): the SOF value computed by (1) can be slightly | |
593 | * lower than 4096, and the host SOF counters can have rolled over to 2048. This | |
594 | * case is handled by subtracting 2048 from the SOF value if it exceeds the host | |
595 | * SOF value at the end of the sliding window. | |
596 | * | |
597 | * Finally we subtract a constant from the host timestamps to bring the first | |
598 | * timestamp of the sliding window to 1s. | |
599 | */ | |
600 | void uvc_video_clock_update(struct uvc_streaming *stream, | |
601 | struct v4l2_buffer *v4l2_buf, | |
602 | struct uvc_buffer *buf) | |
603 | { | |
604 | struct uvc_clock *clock = &stream->clock; | |
605 | struct uvc_clock_sample *first; | |
606 | struct uvc_clock_sample *last; | |
607 | unsigned long flags; | |
608 | struct timespec ts; | |
609 | u32 delta_stc; | |
610 | u32 y1, y2; | |
611 | u32 x1, x2; | |
612 | u32 mean; | |
613 | u32 sof; | |
614 | u32 div; | |
615 | u32 rem; | |
616 | u64 y; | |
617 | ||
618 | spin_lock_irqsave(&clock->lock, flags); | |
619 | ||
620 | if (clock->count < clock->size) | |
621 | goto done; | |
622 | ||
623 | first = &clock->samples[clock->head]; | |
624 | last = &clock->samples[(clock->head - 1) % clock->size]; | |
625 | ||
626 | /* First step, PTS to SOF conversion. */ | |
627 | delta_stc = buf->pts - (1UL << 31); | |
628 | x1 = first->dev_stc - delta_stc; | |
629 | x2 = last->dev_stc - delta_stc; | |
8e57dec0 LP |
630 | if (x1 == x2) |
631 | goto done; | |
632 | ||
66847ef0 LP |
633 | y1 = (first->dev_sof + 2048) << 16; |
634 | y2 = (last->dev_sof + 2048) << 16; | |
66847ef0 LP |
635 | if (y2 < y1) |
636 | y2 += 2048 << 16; | |
637 | ||
638 | y = (u64)(y2 - y1) * (1ULL << 31) + (u64)y1 * (u64)x2 | |
639 | - (u64)y2 * (u64)x1; | |
640 | y = div_u64(y, x2 - x1); | |
641 | ||
642 | sof = y; | |
643 | ||
644 | uvc_trace(UVC_TRACE_CLOCK, "%s: PTS %u y %llu.%06llu SOF %u.%06llu " | |
645 | "(x1 %u x2 %u y1 %u y2 %u SOF offset %u)\n", | |
646 | stream->dev->name, buf->pts, | |
647 | y >> 16, div_u64((y & 0xffff) * 1000000, 65536), | |
648 | sof >> 16, div_u64(((u64)sof & 0xffff) * 1000000LLU, 65536), | |
649 | x1, x2, y1, y2, clock->sof_offset); | |
650 | ||
651 | /* Second step, SOF to host clock conversion. */ | |
66847ef0 LP |
652 | x1 = (uvc_video_clock_host_sof(first) + 2048) << 16; |
653 | x2 = (uvc_video_clock_host_sof(last) + 2048) << 16; | |
66847ef0 LP |
654 | if (x2 < x1) |
655 | x2 += 2048 << 16; | |
8e57dec0 LP |
656 | if (x1 == x2) |
657 | goto done; | |
658 | ||
659 | ts = timespec_sub(last->host_ts, first->host_ts); | |
660 | y1 = NSEC_PER_SEC; | |
661 | y2 = (ts.tv_sec + 1) * NSEC_PER_SEC + ts.tv_nsec; | |
66847ef0 LP |
662 | |
663 | /* Interpolated and host SOF timestamps can wrap around at slightly | |
664 | * different times. Handle this by adding or removing 2048 to or from | |
665 | * the computed SOF value to keep it close to the SOF samples mean | |
666 | * value. | |
667 | */ | |
668 | mean = (x1 + x2) / 2; | |
669 | if (mean - (1024 << 16) > sof) | |
670 | sof += 2048 << 16; | |
671 | else if (sof > mean + (1024 << 16)) | |
672 | sof -= 2048 << 16; | |
673 | ||
674 | y = (u64)(y2 - y1) * (u64)sof + (u64)y1 * (u64)x2 | |
675 | - (u64)y2 * (u64)x1; | |
676 | y = div_u64(y, x2 - x1); | |
677 | ||
678 | div = div_u64_rem(y, NSEC_PER_SEC, &rem); | |
679 | ts.tv_sec = first->host_ts.tv_sec - 1 + div; | |
680 | ts.tv_nsec = first->host_ts.tv_nsec + rem; | |
681 | if (ts.tv_nsec >= NSEC_PER_SEC) { | |
682 | ts.tv_sec++; | |
683 | ts.tv_nsec -= NSEC_PER_SEC; | |
684 | } | |
685 | ||
686 | uvc_trace(UVC_TRACE_CLOCK, "%s: SOF %u.%06llu y %llu ts %lu.%06lu " | |
687 | "buf ts %lu.%06lu (x1 %u/%u/%u x2 %u/%u/%u y1 %u y2 %u)\n", | |
688 | stream->dev->name, | |
689 | sof >> 16, div_u64(((u64)sof & 0xffff) * 1000000LLU, 65536), | |
690 | y, ts.tv_sec, ts.tv_nsec / NSEC_PER_USEC, | |
ce01cbdc MCC |
691 | v4l2_buf->timestamp.tv_sec, |
692 | (unsigned long)v4l2_buf->timestamp.tv_usec, | |
66847ef0 LP |
693 | x1, first->host_sof, first->dev_sof, |
694 | x2, last->host_sof, last->dev_sof, y1, y2); | |
695 | ||
696 | /* Update the V4L2 buffer. */ | |
697 | v4l2_buf->timestamp.tv_sec = ts.tv_sec; | |
698 | v4l2_buf->timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC; | |
699 | ||
700 | done: | |
701 | spin_unlock_irqrestore(&stream->clock.lock, flags); | |
702 | } | |
703 | ||
7bc5edb0 AF |
704 | /* ------------------------------------------------------------------------ |
705 | * Stream statistics | |
706 | */ | |
707 | ||
708 | static void uvc_video_stats_decode(struct uvc_streaming *stream, | |
709 | const __u8 *data, int len) | |
710 | { | |
711 | unsigned int header_size; | |
25738cbd LP |
712 | bool has_pts = false; |
713 | bool has_scr = false; | |
714 | u16 uninitialized_var(scr_sof); | |
715 | u32 uninitialized_var(scr_stc); | |
716 | u32 uninitialized_var(pts); | |
7bc5edb0 AF |
717 | |
718 | if (stream->stats.stream.nb_frames == 0 && | |
719 | stream->stats.frame.nb_packets == 0) | |
720 | ktime_get_ts(&stream->stats.stream.start_ts); | |
721 | ||
722 | switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) { | |
723 | case UVC_STREAM_PTS | UVC_STREAM_SCR: | |
724 | header_size = 12; | |
25738cbd LP |
725 | has_pts = true; |
726 | has_scr = true; | |
7bc5edb0 AF |
727 | break; |
728 | case UVC_STREAM_PTS: | |
729 | header_size = 6; | |
25738cbd | 730 | has_pts = true; |
7bc5edb0 AF |
731 | break; |
732 | case UVC_STREAM_SCR: | |
733 | header_size = 8; | |
25738cbd | 734 | has_scr = true; |
7bc5edb0 AF |
735 | break; |
736 | default: | |
737 | header_size = 2; | |
738 | break; | |
739 | } | |
740 | ||
741 | /* Check for invalid headers. */ | |
742 | if (len < header_size || data[0] < header_size) { | |
743 | stream->stats.frame.nb_invalid++; | |
744 | return; | |
745 | } | |
746 | ||
25738cbd LP |
747 | /* Extract the timestamps. */ |
748 | if (has_pts) | |
749 | pts = get_unaligned_le32(&data[2]); | |
750 | ||
751 | if (has_scr) { | |
752 | scr_stc = get_unaligned_le32(&data[header_size - 6]); | |
753 | scr_sof = get_unaligned_le16(&data[header_size - 2]); | |
754 | } | |
755 | ||
756 | /* Is PTS constant through the whole frame ? */ | |
757 | if (has_pts && stream->stats.frame.nb_pts) { | |
758 | if (stream->stats.frame.pts != pts) { | |
759 | stream->stats.frame.nb_pts_diffs++; | |
760 | stream->stats.frame.last_pts_diff = | |
761 | stream->stats.frame.nb_packets; | |
762 | } | |
763 | } | |
764 | ||
765 | if (has_pts) { | |
766 | stream->stats.frame.nb_pts++; | |
767 | stream->stats.frame.pts = pts; | |
768 | } | |
769 | ||
770 | /* Do all frames have a PTS in their first non-empty packet, or before | |
771 | * their first empty packet ? | |
772 | */ | |
773 | if (stream->stats.frame.size == 0) { | |
774 | if (len > header_size) | |
775 | stream->stats.frame.has_initial_pts = has_pts; | |
776 | if (len == header_size && has_pts) | |
777 | stream->stats.frame.has_early_pts = true; | |
778 | } | |
779 | ||
780 | /* Do the SCR.STC and SCR.SOF fields vary through the frame ? */ | |
781 | if (has_scr && stream->stats.frame.nb_scr) { | |
782 | if (stream->stats.frame.scr_stc != scr_stc) | |
783 | stream->stats.frame.nb_scr_diffs++; | |
784 | } | |
785 | ||
786 | if (has_scr) { | |
787 | /* Expand the SOF counter to 32 bits and store its value. */ | |
788 | if (stream->stats.stream.nb_frames > 0 || | |
789 | stream->stats.frame.nb_scr > 0) | |
790 | stream->stats.stream.scr_sof_count += | |
791 | (scr_sof - stream->stats.stream.scr_sof) % 2048; | |
792 | stream->stats.stream.scr_sof = scr_sof; | |
793 | ||
794 | stream->stats.frame.nb_scr++; | |
795 | stream->stats.frame.scr_stc = scr_stc; | |
796 | stream->stats.frame.scr_sof = scr_sof; | |
797 | ||
798 | if (scr_sof < stream->stats.stream.min_sof) | |
799 | stream->stats.stream.min_sof = scr_sof; | |
800 | if (scr_sof > stream->stats.stream.max_sof) | |
801 | stream->stats.stream.max_sof = scr_sof; | |
802 | } | |
803 | ||
7bc5edb0 AF |
804 | /* Record the first non-empty packet number. */ |
805 | if (stream->stats.frame.size == 0 && len > header_size) | |
806 | stream->stats.frame.first_data = stream->stats.frame.nb_packets; | |
807 | ||
808 | /* Update the frame size. */ | |
809 | stream->stats.frame.size += len - header_size; | |
810 | ||
811 | /* Update the packets counters. */ | |
812 | stream->stats.frame.nb_packets++; | |
813 | if (len > header_size) | |
814 | stream->stats.frame.nb_empty++; | |
815 | ||
816 | if (data[1] & UVC_STREAM_ERR) | |
817 | stream->stats.frame.nb_errors++; | |
818 | } | |
819 | ||
820 | static void uvc_video_stats_update(struct uvc_streaming *stream) | |
821 | { | |
822 | struct uvc_stats_frame *frame = &stream->stats.frame; | |
823 | ||
25738cbd LP |
824 | uvc_trace(UVC_TRACE_STATS, "frame %u stats: %u/%u/%u packets, " |
825 | "%u/%u/%u pts (%searly %sinitial), %u/%u scr, " | |
826 | "last pts/stc/sof %u/%u/%u\n", | |
7bc5edb0 | 827 | stream->sequence, frame->first_data, |
25738cbd LP |
828 | frame->nb_packets - frame->nb_empty, frame->nb_packets, |
829 | frame->nb_pts_diffs, frame->last_pts_diff, frame->nb_pts, | |
830 | frame->has_early_pts ? "" : "!", | |
831 | frame->has_initial_pts ? "" : "!", | |
832 | frame->nb_scr_diffs, frame->nb_scr, | |
833 | frame->pts, frame->scr_stc, frame->scr_sof); | |
7bc5edb0 AF |
834 | |
835 | stream->stats.stream.nb_frames++; | |
836 | stream->stats.stream.nb_packets += stream->stats.frame.nb_packets; | |
837 | stream->stats.stream.nb_empty += stream->stats.frame.nb_empty; | |
838 | stream->stats.stream.nb_errors += stream->stats.frame.nb_errors; | |
839 | stream->stats.stream.nb_invalid += stream->stats.frame.nb_invalid; | |
840 | ||
25738cbd LP |
841 | if (frame->has_early_pts) |
842 | stream->stats.stream.nb_pts_early++; | |
843 | if (frame->has_initial_pts) | |
844 | stream->stats.stream.nb_pts_initial++; | |
845 | if (frame->last_pts_diff <= frame->first_data) | |
846 | stream->stats.stream.nb_pts_constant++; | |
847 | if (frame->nb_scr >= frame->nb_packets - frame->nb_empty) | |
848 | stream->stats.stream.nb_scr_count_ok++; | |
849 | if (frame->nb_scr_diffs + 1 == frame->nb_scr) | |
850 | stream->stats.stream.nb_scr_diffs_ok++; | |
851 | ||
7bc5edb0 AF |
852 | memset(&stream->stats.frame, 0, sizeof(stream->stats.frame)); |
853 | } | |
854 | ||
855 | size_t uvc_video_stats_dump(struct uvc_streaming *stream, char *buf, | |
856 | size_t size) | |
857 | { | |
25738cbd LP |
858 | unsigned int scr_sof_freq; |
859 | unsigned int duration; | |
860 | struct timespec ts; | |
7bc5edb0 AF |
861 | size_t count = 0; |
862 | ||
25738cbd LP |
863 | ts.tv_sec = stream->stats.stream.stop_ts.tv_sec |
864 | - stream->stats.stream.start_ts.tv_sec; | |
865 | ts.tv_nsec = stream->stats.stream.stop_ts.tv_nsec | |
866 | - stream->stats.stream.start_ts.tv_nsec; | |
867 | if (ts.tv_nsec < 0) { | |
868 | ts.tv_sec--; | |
869 | ts.tv_nsec += 1000000000; | |
870 | } | |
871 | ||
872 | /* Compute the SCR.SOF frequency estimate. At the nominal 1kHz SOF | |
873 | * frequency this will not overflow before more than 1h. | |
874 | */ | |
875 | duration = ts.tv_sec * 1000 + ts.tv_nsec / 1000000; | |
876 | if (duration != 0) | |
877 | scr_sof_freq = stream->stats.stream.scr_sof_count * 1000 | |
878 | / duration; | |
879 | else | |
880 | scr_sof_freq = 0; | |
881 | ||
7bc5edb0 AF |
882 | count += scnprintf(buf + count, size - count, |
883 | "frames: %u\npackets: %u\nempty: %u\n" | |
884 | "errors: %u\ninvalid: %u\n", | |
885 | stream->stats.stream.nb_frames, | |
886 | stream->stats.stream.nb_packets, | |
887 | stream->stats.stream.nb_empty, | |
888 | stream->stats.stream.nb_errors, | |
889 | stream->stats.stream.nb_invalid); | |
25738cbd LP |
890 | count += scnprintf(buf + count, size - count, |
891 | "pts: %u early, %u initial, %u ok\n", | |
892 | stream->stats.stream.nb_pts_early, | |
893 | stream->stats.stream.nb_pts_initial, | |
894 | stream->stats.stream.nb_pts_constant); | |
895 | count += scnprintf(buf + count, size - count, | |
896 | "scr: %u count ok, %u diff ok\n", | |
897 | stream->stats.stream.nb_scr_count_ok, | |
898 | stream->stats.stream.nb_scr_diffs_ok); | |
899 | count += scnprintf(buf + count, size - count, | |
900 | "sof: %u <= sof <= %u, freq %u.%03u kHz\n", | |
901 | stream->stats.stream.min_sof, | |
902 | stream->stats.stream.max_sof, | |
903 | scr_sof_freq / 1000, scr_sof_freq % 1000); | |
7bc5edb0 AF |
904 | |
905 | return count; | |
906 | } | |
907 | ||
908 | static void uvc_video_stats_start(struct uvc_streaming *stream) | |
909 | { | |
910 | memset(&stream->stats, 0, sizeof(stream->stats)); | |
25738cbd | 911 | stream->stats.stream.min_sof = 2048; |
7bc5edb0 AF |
912 | } |
913 | ||
914 | static void uvc_video_stats_stop(struct uvc_streaming *stream) | |
915 | { | |
916 | ktime_get_ts(&stream->stats.stream.stop_ts); | |
917 | } | |
918 | ||
c0efd232 LP |
919 | /* ------------------------------------------------------------------------ |
920 | * Video codecs | |
921 | */ | |
922 | ||
c0efd232 LP |
923 | /* Video payload decoding is handled by uvc_video_decode_start(), |
924 | * uvc_video_decode_data() and uvc_video_decode_end(). | |
925 | * | |
926 | * uvc_video_decode_start is called with URB data at the start of a bulk or | |
927 | * isochronous payload. It processes header data and returns the header size | |
928 | * in bytes if successful. If an error occurs, it returns a negative error | |
929 | * code. The following error codes have special meanings. | |
930 | * | |
931 | * - EAGAIN informs the caller that the current video buffer should be marked | |
932 | * as done, and that the function should be called again with the same data | |
933 | * and a new video buffer. This is used when end of frame conditions can be | |
934 | * reliably detected at the beginning of the next frame only. | |
935 | * | |
936 | * If an error other than -EAGAIN is returned, the caller will drop the current | |
937 | * payload. No call to uvc_video_decode_data and uvc_video_decode_end will be | |
938 | * made until the next payload. -ENODATA can be used to drop the current | |
939 | * payload if no other error code is appropriate. | |
940 | * | |
941 | * uvc_video_decode_data is called for every URB with URB data. It copies the | |
942 | * data to the video buffer. | |
943 | * | |
944 | * uvc_video_decode_end is called with header data at the end of a bulk or | |
945 | * isochronous payload. It performs any additional header data processing and | |
25985edc | 946 | * returns 0 or a negative error code if an error occurred. As header data have |
c0efd232 LP |
947 | * already been processed by uvc_video_decode_start, this functions isn't |
948 | * required to perform sanity checks a second time. | |
949 | * | |
25985edc | 950 | * For isochronous transfers where a payload is always transferred in a single |
c0efd232 LP |
951 | * URB, the three functions will be called in a row. |
952 | * | |
953 | * To let the decoder process header data and update its internal state even | |
954 | * when no video buffer is available, uvc_video_decode_start must be prepared | |
955 | * to be called with a NULL buf parameter. uvc_video_decode_data and | |
956 | * uvc_video_decode_end will never be called with a NULL buffer. | |
957 | */ | |
35f02a68 | 958 | static int uvc_video_decode_start(struct uvc_streaming *stream, |
c0efd232 LP |
959 | struct uvc_buffer *buf, const __u8 *data, int len) |
960 | { | |
961 | __u8 fid; | |
962 | ||
963 | /* Sanity checks: | |
964 | * - packet must be at least 2 bytes long | |
965 | * - bHeaderLength value must be at least 2 bytes (see above) | |
966 | * - bHeaderLength value can't be larger than the packet size. | |
967 | */ | |
7bc5edb0 AF |
968 | if (len < 2 || data[0] < 2 || data[0] > len) { |
969 | stream->stats.frame.nb_invalid++; | |
c0efd232 | 970 | return -EINVAL; |
7bc5edb0 | 971 | } |
c0efd232 | 972 | |
c0efd232 LP |
973 | fid = data[1] & UVC_STREAM_FID; |
974 | ||
650b95fe LP |
975 | /* Increase the sequence number regardless of any buffer states, so |
976 | * that discontinuous sequence numbers always indicate lost frames. | |
977 | */ | |
7bc5edb0 | 978 | if (stream->last_fid != fid) { |
650b95fe | 979 | stream->sequence++; |
7bc5edb0 AF |
980 | if (stream->sequence) |
981 | uvc_video_stats_update(stream); | |
982 | } | |
983 | ||
66847ef0 | 984 | uvc_video_clock_decode(stream, buf, data, len); |
7bc5edb0 | 985 | uvc_video_stats_decode(stream, data, len); |
650b95fe | 986 | |
c0efd232 LP |
987 | /* Store the payload FID bit and return immediately when the buffer is |
988 | * NULL. | |
989 | */ | |
990 | if (buf == NULL) { | |
35f02a68 | 991 | stream->last_fid = fid; |
c0efd232 LP |
992 | return -ENODATA; |
993 | } | |
994 | ||
3afedb95 LP |
995 | /* Mark the buffer as bad if the error bit is set. */ |
996 | if (data[1] & UVC_STREAM_ERR) { | |
997 | uvc_trace(UVC_TRACE_FRAME, "Marking buffer as bad (error bit " | |
998 | "set).\n"); | |
999 | buf->error = 1; | |
1000 | } | |
1001 | ||
c0efd232 LP |
1002 | /* Synchronize to the input stream by waiting for the FID bit to be |
1003 | * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE. | |
35f02a68 | 1004 | * stream->last_fid is initialized to -1, so the first isochronous |
c0efd232 LP |
1005 | * frame will always be in sync. |
1006 | * | |
35f02a68 | 1007 | * If the device doesn't toggle the FID bit, invert stream->last_fid |
c0efd232 LP |
1008 | * when the EOF bit is set to force synchronisation on the next packet. |
1009 | */ | |
1010 | if (buf->state != UVC_BUF_STATE_ACTIVE) { | |
310fe524 LP |
1011 | struct timespec ts; |
1012 | ||
35f02a68 | 1013 | if (fid == stream->last_fid) { |
c0efd232 LP |
1014 | uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of " |
1015 | "sync).\n"); | |
35f02a68 | 1016 | if ((stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) && |
c0efd232 | 1017 | (data[1] & UVC_STREAM_EOF)) |
35f02a68 | 1018 | stream->last_fid ^= UVC_STREAM_FID; |
c0efd232 LP |
1019 | return -ENODATA; |
1020 | } | |
1021 | ||
3b35fc81 | 1022 | uvc_video_get_ts(&ts); |
310fe524 | 1023 | |
6998b6fb LP |
1024 | buf->buf.v4l2_buf.sequence = stream->sequence; |
1025 | buf->buf.v4l2_buf.timestamp.tv_sec = ts.tv_sec; | |
1026 | buf->buf.v4l2_buf.timestamp.tv_usec = | |
1027 | ts.tv_nsec / NSEC_PER_USEC; | |
310fe524 | 1028 | |
c0efd232 LP |
1029 | /* TODO: Handle PTS and SCR. */ |
1030 | buf->state = UVC_BUF_STATE_ACTIVE; | |
1031 | } | |
1032 | ||
1033 | /* Mark the buffer as done if we're at the beginning of a new frame. | |
1034 | * End of frame detection is better implemented by checking the EOF | |
1035 | * bit (FID bit toggling is delayed by one frame compared to the EOF | |
1036 | * bit), but some devices don't set the bit at end of frame (and the | |
1037 | * last payload can be lost anyway). We thus must check if the FID has | |
1038 | * been toggled. | |
1039 | * | |
35f02a68 | 1040 | * stream->last_fid is initialized to -1, so the first isochronous |
c0efd232 LP |
1041 | * frame will never trigger an end of frame detection. |
1042 | * | |
1043 | * Empty buffers (bytesused == 0) don't trigger end of frame detection | |
1044 | * as it doesn't make sense to return an empty buffer. This also | |
2c2d264b | 1045 | * avoids detecting end of frame conditions at FID toggling if the |
c0efd232 LP |
1046 | * previous payload had the EOF bit set. |
1047 | */ | |
3d95e932 | 1048 | if (fid != stream->last_fid && buf->bytesused != 0) { |
c0efd232 LP |
1049 | uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit " |
1050 | "toggled).\n"); | |
d7c0d439 | 1051 | buf->state = UVC_BUF_STATE_READY; |
c0efd232 LP |
1052 | return -EAGAIN; |
1053 | } | |
1054 | ||
35f02a68 | 1055 | stream->last_fid = fid; |
c0efd232 LP |
1056 | |
1057 | return data[0]; | |
1058 | } | |
1059 | ||
35f02a68 | 1060 | static void uvc_video_decode_data(struct uvc_streaming *stream, |
c0efd232 LP |
1061 | struct uvc_buffer *buf, const __u8 *data, int len) |
1062 | { | |
c0efd232 LP |
1063 | unsigned int maxlen, nbytes; |
1064 | void *mem; | |
1065 | ||
1066 | if (len <= 0) | |
1067 | return; | |
1068 | ||
1069 | /* Copy the video data to the buffer. */ | |
3d95e932 LP |
1070 | maxlen = buf->length - buf->bytesused; |
1071 | mem = buf->mem + buf->bytesused; | |
c0efd232 LP |
1072 | nbytes = min((unsigned int)len, maxlen); |
1073 | memcpy(mem, data, nbytes); | |
3d95e932 | 1074 | buf->bytesused += nbytes; |
c0efd232 LP |
1075 | |
1076 | /* Complete the current frame if the buffer size was exceeded. */ | |
1077 | if (len > maxlen) { | |
1078 | uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n"); | |
d7c0d439 | 1079 | buf->state = UVC_BUF_STATE_READY; |
c0efd232 LP |
1080 | } |
1081 | } | |
1082 | ||
35f02a68 | 1083 | static void uvc_video_decode_end(struct uvc_streaming *stream, |
c0efd232 LP |
1084 | struct uvc_buffer *buf, const __u8 *data, int len) |
1085 | { | |
1086 | /* Mark the buffer as done if the EOF marker is set. */ | |
3d95e932 | 1087 | if (data[1] & UVC_STREAM_EOF && buf->bytesused != 0) { |
c0efd232 LP |
1088 | uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n"); |
1089 | if (data[0] == len) | |
1090 | uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n"); | |
d7c0d439 | 1091 | buf->state = UVC_BUF_STATE_READY; |
35f02a68 LP |
1092 | if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) |
1093 | stream->last_fid ^= UVC_STREAM_FID; | |
c0efd232 LP |
1094 | } |
1095 | } | |
1096 | ||
2c2d264b LP |
1097 | /* Video payload encoding is handled by uvc_video_encode_header() and |
1098 | * uvc_video_encode_data(). Only bulk transfers are currently supported. | |
1099 | * | |
1100 | * uvc_video_encode_header is called at the start of a payload. It adds header | |
1101 | * data to the transfer buffer and returns the header size. As the only known | |
1102 | * UVC output device transfers a whole frame in a single payload, the EOF bit | |
1103 | * is always set in the header. | |
1104 | * | |
1105 | * uvc_video_encode_data is called for every URB and copies the data from the | |
1106 | * video buffer to the transfer buffer. | |
1107 | */ | |
35f02a68 | 1108 | static int uvc_video_encode_header(struct uvc_streaming *stream, |
ff924203 LP |
1109 | struct uvc_buffer *buf, __u8 *data, int len) |
1110 | { | |
1111 | data[0] = 2; /* Header length */ | |
1112 | data[1] = UVC_STREAM_EOH | UVC_STREAM_EOF | |
35f02a68 | 1113 | | (stream->last_fid & UVC_STREAM_FID); |
ff924203 LP |
1114 | return 2; |
1115 | } | |
1116 | ||
35f02a68 | 1117 | static int uvc_video_encode_data(struct uvc_streaming *stream, |
ff924203 LP |
1118 | struct uvc_buffer *buf, __u8 *data, int len) |
1119 | { | |
35f02a68 | 1120 | struct uvc_video_queue *queue = &stream->queue; |
ff924203 LP |
1121 | unsigned int nbytes; |
1122 | void *mem; | |
1123 | ||
1124 | /* Copy video data to the URB buffer. */ | |
3d95e932 LP |
1125 | mem = buf->mem + queue->buf_used; |
1126 | nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used); | |
35f02a68 | 1127 | nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size, |
ff924203 LP |
1128 | nbytes); |
1129 | memcpy(data, mem, nbytes); | |
1130 | ||
1131 | queue->buf_used += nbytes; | |
1132 | ||
1133 | return nbytes; | |
1134 | } | |
1135 | ||
c0efd232 LP |
1136 | /* ------------------------------------------------------------------------ |
1137 | * URB handling | |
1138 | */ | |
1139 | ||
f8918ba0 AL |
1140 | /* |
1141 | * Set error flag for incomplete buffer. | |
1142 | */ | |
1143 | static void uvc_video_validate_buffer(const struct uvc_streaming *stream, | |
1144 | struct uvc_buffer *buf) | |
1145 | { | |
c601f53f | 1146 | if (stream->ctrl.dwMaxVideoFrameSize != buf->bytesused && |
f8918ba0 AL |
1147 | !(stream->cur_format->flags & UVC_FMT_FLAG_COMPRESSED)) |
1148 | buf->error = 1; | |
1149 | } | |
1150 | ||
c0efd232 LP |
1151 | /* |
1152 | * Completion handler for video URBs. | |
1153 | */ | |
35f02a68 LP |
1154 | static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream, |
1155 | struct uvc_buffer *buf) | |
c0efd232 LP |
1156 | { |
1157 | u8 *mem; | |
1158 | int ret, i; | |
1159 | ||
1160 | for (i = 0; i < urb->number_of_packets; ++i) { | |
1161 | if (urb->iso_frame_desc[i].status < 0) { | |
1162 | uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame " | |
1163 | "lost (%d).\n", urb->iso_frame_desc[i].status); | |
9bde9f26 LP |
1164 | /* Mark the buffer as faulty. */ |
1165 | if (buf != NULL) | |
1166 | buf->error = 1; | |
c0efd232 LP |
1167 | continue; |
1168 | } | |
1169 | ||
1170 | /* Decode the payload header. */ | |
1171 | mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset; | |
1172 | do { | |
35f02a68 | 1173 | ret = uvc_video_decode_start(stream, buf, mem, |
c0efd232 | 1174 | urb->iso_frame_desc[i].actual_length); |
f8918ba0 AL |
1175 | if (ret == -EAGAIN) { |
1176 | uvc_video_validate_buffer(stream, buf); | |
35f02a68 LP |
1177 | buf = uvc_queue_next_buffer(&stream->queue, |
1178 | buf); | |
f8918ba0 | 1179 | } |
c0efd232 LP |
1180 | } while (ret == -EAGAIN); |
1181 | ||
1182 | if (ret < 0) | |
1183 | continue; | |
1184 | ||
1185 | /* Decode the payload data. */ | |
35f02a68 | 1186 | uvc_video_decode_data(stream, buf, mem + ret, |
c0efd232 LP |
1187 | urb->iso_frame_desc[i].actual_length - ret); |
1188 | ||
1189 | /* Process the header again. */ | |
35f02a68 | 1190 | uvc_video_decode_end(stream, buf, mem, |
08ebd003 | 1191 | urb->iso_frame_desc[i].actual_length); |
c0efd232 | 1192 | |
9bde9f26 | 1193 | if (buf->state == UVC_BUF_STATE_READY) { |
f8918ba0 | 1194 | uvc_video_validate_buffer(stream, buf); |
35f02a68 | 1195 | buf = uvc_queue_next_buffer(&stream->queue, buf); |
9bde9f26 | 1196 | } |
c0efd232 LP |
1197 | } |
1198 | } | |
1199 | ||
35f02a68 LP |
1200 | static void uvc_video_decode_bulk(struct urb *urb, struct uvc_streaming *stream, |
1201 | struct uvc_buffer *buf) | |
c0efd232 LP |
1202 | { |
1203 | u8 *mem; | |
1204 | int len, ret; | |
1205 | ||
c854a48a J |
1206 | /* |
1207 | * Ignore ZLPs if they're not part of a frame, otherwise process them | |
1208 | * to trigger the end of payload detection. | |
1209 | */ | |
1210 | if (urb->actual_length == 0 && stream->bulk.header_size == 0) | |
c90e7779 LP |
1211 | return; |
1212 | ||
c0efd232 LP |
1213 | mem = urb->transfer_buffer; |
1214 | len = urb->actual_length; | |
35f02a68 | 1215 | stream->bulk.payload_size += len; |
c0efd232 LP |
1216 | |
1217 | /* If the URB is the first of its payload, decode and save the | |
1218 | * header. | |
1219 | */ | |
35f02a68 | 1220 | if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) { |
c0efd232 | 1221 | do { |
35f02a68 | 1222 | ret = uvc_video_decode_start(stream, buf, mem, len); |
c0efd232 | 1223 | if (ret == -EAGAIN) |
35f02a68 LP |
1224 | buf = uvc_queue_next_buffer(&stream->queue, |
1225 | buf); | |
c0efd232 LP |
1226 | } while (ret == -EAGAIN); |
1227 | ||
25985edc | 1228 | /* If an error occurred skip the rest of the payload. */ |
c0efd232 | 1229 | if (ret < 0 || buf == NULL) { |
35f02a68 | 1230 | stream->bulk.skip_payload = 1; |
f8dd4af6 | 1231 | } else { |
35f02a68 LP |
1232 | memcpy(stream->bulk.header, mem, ret); |
1233 | stream->bulk.header_size = ret; | |
c0efd232 | 1234 | |
f8dd4af6 LP |
1235 | mem += ret; |
1236 | len -= ret; | |
1237 | } | |
c0efd232 LP |
1238 | } |
1239 | ||
1240 | /* The buffer queue might have been cancelled while a bulk transfer | |
1241 | * was in progress, so we can reach here with buf equal to NULL. Make | |
1242 | * sure buf is never dereferenced if NULL. | |
1243 | */ | |
1244 | ||
1245 | /* Process video data. */ | |
35f02a68 LP |
1246 | if (!stream->bulk.skip_payload && buf != NULL) |
1247 | uvc_video_decode_data(stream, buf, mem, len); | |
c0efd232 LP |
1248 | |
1249 | /* Detect the payload end by a URB smaller than the maximum size (or | |
1250 | * a payload size equal to the maximum) and process the header again. | |
1251 | */ | |
1252 | if (urb->actual_length < urb->transfer_buffer_length || | |
35f02a68 LP |
1253 | stream->bulk.payload_size >= stream->bulk.max_payload_size) { |
1254 | if (!stream->bulk.skip_payload && buf != NULL) { | |
1255 | uvc_video_decode_end(stream, buf, stream->bulk.header, | |
1256 | stream->bulk.payload_size); | |
d7c0d439 | 1257 | if (buf->state == UVC_BUF_STATE_READY) |
35f02a68 LP |
1258 | buf = uvc_queue_next_buffer(&stream->queue, |
1259 | buf); | |
c0efd232 LP |
1260 | } |
1261 | ||
35f02a68 LP |
1262 | stream->bulk.header_size = 0; |
1263 | stream->bulk.skip_payload = 0; | |
1264 | stream->bulk.payload_size = 0; | |
c0efd232 LP |
1265 | } |
1266 | } | |
1267 | ||
35f02a68 LP |
1268 | static void uvc_video_encode_bulk(struct urb *urb, struct uvc_streaming *stream, |
1269 | struct uvc_buffer *buf) | |
ff924203 LP |
1270 | { |
1271 | u8 *mem = urb->transfer_buffer; | |
35f02a68 | 1272 | int len = stream->urb_size, ret; |
ff924203 LP |
1273 | |
1274 | if (buf == NULL) { | |
1275 | urb->transfer_buffer_length = 0; | |
1276 | return; | |
1277 | } | |
1278 | ||
1279 | /* If the URB is the first of its payload, add the header. */ | |
35f02a68 LP |
1280 | if (stream->bulk.header_size == 0) { |
1281 | ret = uvc_video_encode_header(stream, buf, mem, len); | |
1282 | stream->bulk.header_size = ret; | |
1283 | stream->bulk.payload_size += ret; | |
ff924203 LP |
1284 | mem += ret; |
1285 | len -= ret; | |
1286 | } | |
1287 | ||
1288 | /* Process video data. */ | |
35f02a68 | 1289 | ret = uvc_video_encode_data(stream, buf, mem, len); |
ff924203 | 1290 | |
35f02a68 | 1291 | stream->bulk.payload_size += ret; |
ff924203 LP |
1292 | len -= ret; |
1293 | ||
3d95e932 | 1294 | if (buf->bytesused == stream->queue.buf_used || |
35f02a68 | 1295 | stream->bulk.payload_size == stream->bulk.max_payload_size) { |
3d95e932 | 1296 | if (buf->bytesused == stream->queue.buf_used) { |
35f02a68 | 1297 | stream->queue.buf_used = 0; |
d7c0d439 | 1298 | buf->state = UVC_BUF_STATE_READY; |
6998b6fb | 1299 | buf->buf.v4l2_buf.sequence = ++stream->sequence; |
35f02a68 LP |
1300 | uvc_queue_next_buffer(&stream->queue, buf); |
1301 | stream->last_fid ^= UVC_STREAM_FID; | |
ff924203 LP |
1302 | } |
1303 | ||
35f02a68 LP |
1304 | stream->bulk.header_size = 0; |
1305 | stream->bulk.payload_size = 0; | |
ff924203 LP |
1306 | } |
1307 | ||
35f02a68 | 1308 | urb->transfer_buffer_length = stream->urb_size - len; |
ff924203 LP |
1309 | } |
1310 | ||
c0efd232 LP |
1311 | static void uvc_video_complete(struct urb *urb) |
1312 | { | |
35f02a68 LP |
1313 | struct uvc_streaming *stream = urb->context; |
1314 | struct uvc_video_queue *queue = &stream->queue; | |
c0efd232 LP |
1315 | struct uvc_buffer *buf = NULL; |
1316 | unsigned long flags; | |
1317 | int ret; | |
1318 | ||
1319 | switch (urb->status) { | |
1320 | case 0: | |
1321 | break; | |
1322 | ||
1323 | default: | |
1324 | uvc_printk(KERN_WARNING, "Non-zero status (%d) in video " | |
1325 | "completion handler.\n", urb->status); | |
1326 | ||
1327 | case -ENOENT: /* usb_kill_urb() called. */ | |
35f02a68 | 1328 | if (stream->frozen) |
c0efd232 LP |
1329 | return; |
1330 | ||
1331 | case -ECONNRESET: /* usb_unlink_urb() called. */ | |
1332 | case -ESHUTDOWN: /* The endpoint is being disabled. */ | |
1333 | uvc_queue_cancel(queue, urb->status == -ESHUTDOWN); | |
1334 | return; | |
1335 | } | |
1336 | ||
1337 | spin_lock_irqsave(&queue->irqlock, flags); | |
1338 | if (!list_empty(&queue->irqqueue)) | |
1339 | buf = list_first_entry(&queue->irqqueue, struct uvc_buffer, | |
1340 | queue); | |
1341 | spin_unlock_irqrestore(&queue->irqlock, flags); | |
1342 | ||
35f02a68 | 1343 | stream->decode(urb, stream, buf); |
c0efd232 LP |
1344 | |
1345 | if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { | |
1346 | uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n", | |
1347 | ret); | |
1348 | } | |
1349 | } | |
1350 | ||
e01117c8 LP |
1351 | /* |
1352 | * Free transfer buffers. | |
1353 | */ | |
35f02a68 | 1354 | static void uvc_free_urb_buffers(struct uvc_streaming *stream) |
e01117c8 LP |
1355 | { |
1356 | unsigned int i; | |
1357 | ||
1358 | for (i = 0; i < UVC_URBS; ++i) { | |
35f02a68 | 1359 | if (stream->urb_buffer[i]) { |
3e0ac671 | 1360 | #ifndef CONFIG_DMA_NONCOHERENT |
997ea58e | 1361 | usb_free_coherent(stream->dev->udev, stream->urb_size, |
35f02a68 | 1362 | stream->urb_buffer[i], stream->urb_dma[i]); |
3e0ac671 AC |
1363 | #else |
1364 | kfree(stream->urb_buffer[i]); | |
1365 | #endif | |
35f02a68 | 1366 | stream->urb_buffer[i] = NULL; |
e01117c8 LP |
1367 | } |
1368 | } | |
1369 | ||
35f02a68 | 1370 | stream->urb_size = 0; |
e01117c8 LP |
1371 | } |
1372 | ||
1373 | /* | |
1374 | * Allocate transfer buffers. This function can be called with buffers | |
1375 | * already allocated when resuming from suspend, in which case it will | |
1376 | * return without touching the buffers. | |
1377 | * | |
efdc8a95 LP |
1378 | * Limit the buffer size to UVC_MAX_PACKETS bulk/isochronous packets. If the |
1379 | * system is too low on memory try successively smaller numbers of packets | |
1380 | * until allocation succeeds. | |
1381 | * | |
1382 | * Return the number of allocated packets on success or 0 when out of memory. | |
e01117c8 | 1383 | */ |
35f02a68 | 1384 | static int uvc_alloc_urb_buffers(struct uvc_streaming *stream, |
efdc8a95 | 1385 | unsigned int size, unsigned int psize, gfp_t gfp_flags) |
e01117c8 | 1386 | { |
efdc8a95 | 1387 | unsigned int npackets; |
e01117c8 LP |
1388 | unsigned int i; |
1389 | ||
1390 | /* Buffers are already allocated, bail out. */ | |
35f02a68 LP |
1391 | if (stream->urb_size) |
1392 | return stream->urb_size / psize; | |
e01117c8 | 1393 | |
efdc8a95 | 1394 | /* Compute the number of packets. Bulk endpoints might transfer UVC |
25985edc | 1395 | * payloads across multiple URBs. |
efdc8a95 LP |
1396 | */ |
1397 | npackets = DIV_ROUND_UP(size, psize); | |
1398 | if (npackets > UVC_MAX_PACKETS) | |
1399 | npackets = UVC_MAX_PACKETS; | |
1400 | ||
1401 | /* Retry allocations until one succeed. */ | |
1402 | for (; npackets > 1; npackets /= 2) { | |
1403 | for (i = 0; i < UVC_URBS; ++i) { | |
fd1b6bbb | 1404 | stream->urb_size = psize * npackets; |
3e0ac671 | 1405 | #ifndef CONFIG_DMA_NONCOHERENT |
997ea58e | 1406 | stream->urb_buffer[i] = usb_alloc_coherent( |
fd1b6bbb | 1407 | stream->dev->udev, stream->urb_size, |
35f02a68 | 1408 | gfp_flags | __GFP_NOWARN, &stream->urb_dma[i]); |
3e0ac671 AC |
1409 | #else |
1410 | stream->urb_buffer[i] = | |
1411 | kmalloc(stream->urb_size, gfp_flags | __GFP_NOWARN); | |
1412 | #endif | |
35f02a68 LP |
1413 | if (!stream->urb_buffer[i]) { |
1414 | uvc_free_urb_buffers(stream); | |
efdc8a95 LP |
1415 | break; |
1416 | } | |
1417 | } | |
1418 | ||
1419 | if (i == UVC_URBS) { | |
663a4192 LP |
1420 | uvc_trace(UVC_TRACE_VIDEO, "Allocated %u URB buffers " |
1421 | "of %ux%u bytes each.\n", UVC_URBS, npackets, | |
1422 | psize); | |
efdc8a95 | 1423 | return npackets; |
e01117c8 LP |
1424 | } |
1425 | } | |
1426 | ||
663a4192 LP |
1427 | uvc_trace(UVC_TRACE_VIDEO, "Failed to allocate URB buffers (%u bytes " |
1428 | "per packet).\n", psize); | |
e01117c8 LP |
1429 | return 0; |
1430 | } | |
1431 | ||
c0efd232 LP |
1432 | /* |
1433 | * Uninitialize isochronous/bulk URBs and free transfer buffers. | |
1434 | */ | |
35f02a68 | 1435 | static void uvc_uninit_video(struct uvc_streaming *stream, int free_buffers) |
c0efd232 LP |
1436 | { |
1437 | struct urb *urb; | |
1438 | unsigned int i; | |
1439 | ||
7bc5edb0 AF |
1440 | uvc_video_stats_stop(stream); |
1441 | ||
c0efd232 | 1442 | for (i = 0; i < UVC_URBS; ++i) { |
35f02a68 LP |
1443 | urb = stream->urb[i]; |
1444 | if (urb == NULL) | |
c0efd232 LP |
1445 | continue; |
1446 | ||
1447 | usb_kill_urb(urb); | |
c0efd232 | 1448 | usb_free_urb(urb); |
35f02a68 | 1449 | stream->urb[i] = NULL; |
c0efd232 | 1450 | } |
e01117c8 LP |
1451 | |
1452 | if (free_buffers) | |
35f02a68 | 1453 | uvc_free_urb_buffers(stream); |
c0efd232 LP |
1454 | } |
1455 | ||
6fd90db8 LP |
1456 | /* |
1457 | * Compute the maximum number of bytes per interval for an endpoint. | |
1458 | */ | |
1459 | static unsigned int uvc_endpoint_max_bpi(struct usb_device *dev, | |
1460 | struct usb_host_endpoint *ep) | |
1461 | { | |
1462 | u16 psize; | |
1463 | ||
1464 | switch (dev->speed) { | |
1465 | case USB_SPEED_SUPER: | |
d71b0b34 | 1466 | return le16_to_cpu(ep->ss_ep_comp.wBytesPerInterval); |
6fd90db8 LP |
1467 | case USB_SPEED_HIGH: |
1468 | psize = usb_endpoint_maxp(&ep->desc); | |
1469 | return (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); | |
79af67e7 TP |
1470 | case USB_SPEED_WIRELESS: |
1471 | psize = usb_endpoint_maxp(&ep->desc); | |
1472 | return psize; | |
6fd90db8 LP |
1473 | default: |
1474 | psize = usb_endpoint_maxp(&ep->desc); | |
1475 | return psize & 0x07ff; | |
1476 | } | |
1477 | } | |
1478 | ||
c0efd232 LP |
1479 | /* |
1480 | * Initialize isochronous URBs and allocate transfer buffers. The packet size | |
1481 | * is given by the endpoint. | |
1482 | */ | |
35f02a68 | 1483 | static int uvc_init_video_isoc(struct uvc_streaming *stream, |
29135878 | 1484 | struct usb_host_endpoint *ep, gfp_t gfp_flags) |
c0efd232 LP |
1485 | { |
1486 | struct urb *urb; | |
1487 | unsigned int npackets, i, j; | |
efdc8a95 LP |
1488 | u16 psize; |
1489 | u32 size; | |
c0efd232 | 1490 | |
6fd90db8 | 1491 | psize = uvc_endpoint_max_bpi(stream->dev->udev, ep); |
35f02a68 | 1492 | size = stream->ctrl.dwMaxVideoFrameSize; |
c0efd232 | 1493 | |
35f02a68 | 1494 | npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags); |
efdc8a95 LP |
1495 | if (npackets == 0) |
1496 | return -ENOMEM; | |
c0efd232 LP |
1497 | |
1498 | size = npackets * psize; | |
1499 | ||
1500 | for (i = 0; i < UVC_URBS; ++i) { | |
29135878 | 1501 | urb = usb_alloc_urb(npackets, gfp_flags); |
c0efd232 | 1502 | if (urb == NULL) { |
35f02a68 | 1503 | uvc_uninit_video(stream, 1); |
c0efd232 LP |
1504 | return -ENOMEM; |
1505 | } | |
1506 | ||
35f02a68 LP |
1507 | urb->dev = stream->dev->udev; |
1508 | urb->context = stream; | |
1509 | urb->pipe = usb_rcvisocpipe(stream->dev->udev, | |
c0efd232 | 1510 | ep->desc.bEndpointAddress); |
3e0ac671 | 1511 | #ifndef CONFIG_DMA_NONCOHERENT |
c0efd232 | 1512 | urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; |
3e0ac671 AC |
1513 | urb->transfer_dma = stream->urb_dma[i]; |
1514 | #else | |
1515 | urb->transfer_flags = URB_ISO_ASAP; | |
1516 | #endif | |
c0efd232 | 1517 | urb->interval = ep->desc.bInterval; |
35f02a68 | 1518 | urb->transfer_buffer = stream->urb_buffer[i]; |
c0efd232 LP |
1519 | urb->complete = uvc_video_complete; |
1520 | urb->number_of_packets = npackets; | |
1521 | urb->transfer_buffer_length = size; | |
1522 | ||
1523 | for (j = 0; j < npackets; ++j) { | |
1524 | urb->iso_frame_desc[j].offset = j * psize; | |
1525 | urb->iso_frame_desc[j].length = psize; | |
1526 | } | |
1527 | ||
35f02a68 | 1528 | stream->urb[i] = urb; |
c0efd232 LP |
1529 | } |
1530 | ||
1531 | return 0; | |
1532 | } | |
1533 | ||
1534 | /* | |
1535 | * Initialize bulk URBs and allocate transfer buffers. The packet size is | |
1536 | * given by the endpoint. | |
1537 | */ | |
35f02a68 | 1538 | static int uvc_init_video_bulk(struct uvc_streaming *stream, |
29135878 | 1539 | struct usb_host_endpoint *ep, gfp_t gfp_flags) |
c0efd232 LP |
1540 | { |
1541 | struct urb *urb; | |
efdc8a95 LP |
1542 | unsigned int npackets, pipe, i; |
1543 | u16 psize; | |
1544 | u32 size; | |
1545 | ||
6fd90db8 | 1546 | psize = usb_endpoint_maxp(&ep->desc) & 0x7ff; |
35f02a68 LP |
1547 | size = stream->ctrl.dwMaxPayloadTransferSize; |
1548 | stream->bulk.max_payload_size = size; | |
c0efd232 | 1549 | |
35f02a68 | 1550 | npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags); |
efdc8a95 | 1551 | if (npackets == 0) |
e01117c8 LP |
1552 | return -ENOMEM; |
1553 | ||
efdc8a95 LP |
1554 | size = npackets * psize; |
1555 | ||
ff924203 | 1556 | if (usb_endpoint_dir_in(&ep->desc)) |
35f02a68 | 1557 | pipe = usb_rcvbulkpipe(stream->dev->udev, |
ff924203 LP |
1558 | ep->desc.bEndpointAddress); |
1559 | else | |
35f02a68 | 1560 | pipe = usb_sndbulkpipe(stream->dev->udev, |
ff924203 LP |
1561 | ep->desc.bEndpointAddress); |
1562 | ||
35f02a68 | 1563 | if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) |
ff924203 | 1564 | size = 0; |
c0efd232 LP |
1565 | |
1566 | for (i = 0; i < UVC_URBS; ++i) { | |
29135878 | 1567 | urb = usb_alloc_urb(0, gfp_flags); |
c0efd232 | 1568 | if (urb == NULL) { |
35f02a68 | 1569 | uvc_uninit_video(stream, 1); |
c0efd232 LP |
1570 | return -ENOMEM; |
1571 | } | |
1572 | ||
35f02a68 LP |
1573 | usb_fill_bulk_urb(urb, stream->dev->udev, pipe, |
1574 | stream->urb_buffer[i], size, uvc_video_complete, | |
1575 | stream); | |
3e0ac671 | 1576 | #ifndef CONFIG_DMA_NONCOHERENT |
c0efd232 | 1577 | urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; |
35f02a68 | 1578 | urb->transfer_dma = stream->urb_dma[i]; |
3e0ac671 | 1579 | #endif |
c0efd232 | 1580 | |
35f02a68 | 1581 | stream->urb[i] = urb; |
c0efd232 LP |
1582 | } |
1583 | ||
1584 | return 0; | |
1585 | } | |
1586 | ||
1587 | /* | |
1588 | * Initialize isochronous/bulk URBs and allocate transfer buffers. | |
1589 | */ | |
35f02a68 | 1590 | static int uvc_init_video(struct uvc_streaming *stream, gfp_t gfp_flags) |
c0efd232 | 1591 | { |
35f02a68 | 1592 | struct usb_interface *intf = stream->intf; |
2c4d9de8 LP |
1593 | struct usb_host_endpoint *ep; |
1594 | unsigned int i; | |
c0efd232 LP |
1595 | int ret; |
1596 | ||
650b95fe | 1597 | stream->sequence = -1; |
35f02a68 LP |
1598 | stream->last_fid = -1; |
1599 | stream->bulk.header_size = 0; | |
1600 | stream->bulk.skip_payload = 0; | |
1601 | stream->bulk.payload_size = 0; | |
c0efd232 | 1602 | |
7bc5edb0 AF |
1603 | uvc_video_stats_start(stream); |
1604 | ||
c0efd232 | 1605 | if (intf->num_altsetting > 1) { |
2c4d9de8 | 1606 | struct usb_host_endpoint *best_ep = NULL; |
6fd90db8 | 1607 | unsigned int best_psize = UINT_MAX; |
2c4d9de8 LP |
1608 | unsigned int bandwidth; |
1609 | unsigned int uninitialized_var(altsetting); | |
1610 | int intfnum = stream->intfnum; | |
1611 | ||
c0efd232 | 1612 | /* Isochronous endpoint, select the alternate setting. */ |
35f02a68 | 1613 | bandwidth = stream->ctrl.dwMaxPayloadTransferSize; |
c0efd232 LP |
1614 | |
1615 | if (bandwidth == 0) { | |
663a4192 LP |
1616 | uvc_trace(UVC_TRACE_VIDEO, "Device requested null " |
1617 | "bandwidth, defaulting to lowest.\n"); | |
c0efd232 | 1618 | bandwidth = 1; |
663a4192 LP |
1619 | } else { |
1620 | uvc_trace(UVC_TRACE_VIDEO, "Device requested %u " | |
1621 | "B/frame bandwidth.\n", bandwidth); | |
c0efd232 LP |
1622 | } |
1623 | ||
1624 | for (i = 0; i < intf->num_altsetting; ++i) { | |
2c4d9de8 LP |
1625 | struct usb_host_interface *alts; |
1626 | unsigned int psize; | |
1627 | ||
c0efd232 LP |
1628 | alts = &intf->altsetting[i]; |
1629 | ep = uvc_find_endpoint(alts, | |
35f02a68 | 1630 | stream->header.bEndpointAddress); |
c0efd232 LP |
1631 | if (ep == NULL) |
1632 | continue; | |
1633 | ||
1634 | /* Check if the bandwidth is high enough. */ | |
6fd90db8 | 1635 | psize = uvc_endpoint_max_bpi(stream->dev->udev, ep); |
2c4d9de8 | 1636 | if (psize >= bandwidth && psize <= best_psize) { |
4807063f | 1637 | altsetting = alts->desc.bAlternateSetting; |
2c4d9de8 LP |
1638 | best_psize = psize; |
1639 | best_ep = ep; | |
1640 | } | |
c0efd232 LP |
1641 | } |
1642 | ||
2c4d9de8 | 1643 | if (best_ep == NULL) { |
663a4192 LP |
1644 | uvc_trace(UVC_TRACE_VIDEO, "No fast enough alt setting " |
1645 | "for requested bandwidth.\n"); | |
c0efd232 | 1646 | return -EIO; |
663a4192 | 1647 | } |
c0efd232 | 1648 | |
2c4d9de8 LP |
1649 | uvc_trace(UVC_TRACE_VIDEO, "Selecting alternate setting %u " |
1650 | "(%u B/frame bandwidth).\n", altsetting, best_psize); | |
1651 | ||
1652 | ret = usb_set_interface(stream->dev->udev, intfnum, altsetting); | |
35f02a68 | 1653 | if (ret < 0) |
c0efd232 LP |
1654 | return ret; |
1655 | ||
2c4d9de8 | 1656 | ret = uvc_init_video_isoc(stream, best_ep, gfp_flags); |
c0efd232 LP |
1657 | } else { |
1658 | /* Bulk endpoint, proceed to URB initialization. */ | |
1659 | ep = uvc_find_endpoint(&intf->altsetting[0], | |
35f02a68 | 1660 | stream->header.bEndpointAddress); |
c0efd232 LP |
1661 | if (ep == NULL) |
1662 | return -EIO; | |
1663 | ||
35f02a68 | 1664 | ret = uvc_init_video_bulk(stream, ep, gfp_flags); |
c0efd232 LP |
1665 | } |
1666 | ||
1667 | if (ret < 0) | |
1668 | return ret; | |
1669 | ||
1670 | /* Submit the URBs. */ | |
1671 | for (i = 0; i < UVC_URBS; ++i) { | |
35f02a68 LP |
1672 | ret = usb_submit_urb(stream->urb[i], gfp_flags); |
1673 | if (ret < 0) { | |
c0efd232 LP |
1674 | uvc_printk(KERN_ERR, "Failed to submit URB %u " |
1675 | "(%d).\n", i, ret); | |
35f02a68 | 1676 | uvc_uninit_video(stream, 1); |
c0efd232 LP |
1677 | return ret; |
1678 | } | |
1679 | } | |
1680 | ||
17e1319f WM |
1681 | /* The Logitech C920 temporarily forgets that it should not be adjusting |
1682 | * Exposure Absolute during init so restore controls to stored values. | |
1683 | */ | |
1684 | if (stream->dev->quirks & UVC_QUIRK_RESTORE_CTRLS_ON_INIT) | |
1685 | uvc_ctrl_restore_values(stream->dev); | |
1686 | ||
c0efd232 LP |
1687 | return 0; |
1688 | } | |
1689 | ||
1690 | /* -------------------------------------------------------------------------- | |
1691 | * Suspend/resume | |
1692 | */ | |
1693 | ||
1694 | /* | |
1695 | * Stop streaming without disabling the video queue. | |
1696 | * | |
1697 | * To let userspace applications resume without trouble, we must not touch the | |
1698 | * video buffers in any way. We mark the device as frozen to make sure the URB | |
1699 | * completion handler won't try to cancel the queue when we kill the URBs. | |
1700 | */ | |
35f02a68 | 1701 | int uvc_video_suspend(struct uvc_streaming *stream) |
c0efd232 | 1702 | { |
35f02a68 | 1703 | if (!uvc_queue_streaming(&stream->queue)) |
c0efd232 LP |
1704 | return 0; |
1705 | ||
35f02a68 LP |
1706 | stream->frozen = 1; |
1707 | uvc_uninit_video(stream, 0); | |
1708 | usb_set_interface(stream->dev->udev, stream->intfnum, 0); | |
c0efd232 LP |
1709 | return 0; |
1710 | } | |
1711 | ||
1712 | /* | |
2c2d264b | 1713 | * Reconfigure the video interface and restart streaming if it was enabled |
c0efd232 LP |
1714 | * before suspend. |
1715 | * | |
1716 | * If an error occurs, disable the video queue. This will wake all pending | |
1717 | * buffers, making sure userspace applications are notified of the problem | |
1718 | * instead of waiting forever. | |
1719 | */ | |
d59a7b1d | 1720 | int uvc_video_resume(struct uvc_streaming *stream, int reset) |
c0efd232 LP |
1721 | { |
1722 | int ret; | |
1723 | ||
d59a7b1d ML |
1724 | /* If the bus has been reset on resume, set the alternate setting to 0. |
1725 | * This should be the default value, but some devices crash or otherwise | |
1726 | * misbehave if they don't receive a SET_INTERFACE request before any | |
1727 | * other video control request. | |
1728 | */ | |
1729 | if (reset) | |
1730 | usb_set_interface(stream->dev->udev, stream->intfnum, 0); | |
1731 | ||
35f02a68 | 1732 | stream->frozen = 0; |
c0efd232 | 1733 | |
ed0ee0ce LP |
1734 | uvc_video_clock_reset(stream); |
1735 | ||
35f02a68 LP |
1736 | ret = uvc_commit_video(stream, &stream->ctrl); |
1737 | if (ret < 0) { | |
1738 | uvc_queue_enable(&stream->queue, 0); | |
c0efd232 LP |
1739 | return ret; |
1740 | } | |
1741 | ||
35f02a68 | 1742 | if (!uvc_queue_streaming(&stream->queue)) |
c0efd232 LP |
1743 | return 0; |
1744 | ||
35f02a68 LP |
1745 | ret = uvc_init_video(stream, GFP_NOIO); |
1746 | if (ret < 0) | |
1747 | uvc_queue_enable(&stream->queue, 0); | |
c0efd232 LP |
1748 | |
1749 | return ret; | |
1750 | } | |
1751 | ||
1752 | /* ------------------------------------------------------------------------ | |
1753 | * Video device | |
1754 | */ | |
1755 | ||
1756 | /* | |
2c2d264b LP |
1757 | * Initialize the UVC video device by switching to alternate setting 0 and |
1758 | * retrieve the default format. | |
c0efd232 LP |
1759 | * |
1760 | * Some cameras (namely the Fuji Finepix) set the format and frame | |
1761 | * indexes to zero. The UVC standard doesn't clearly make this a spec | |
1762 | * violation, so try to silently fix the values if possible. | |
1763 | * | |
1764 | * This function is called before registering the device with V4L. | |
1765 | */ | |
35f02a68 | 1766 | int uvc_video_init(struct uvc_streaming *stream) |
c0efd232 | 1767 | { |
35f02a68 | 1768 | struct uvc_streaming_control *probe = &stream->ctrl; |
c0efd232 LP |
1769 | struct uvc_format *format = NULL; |
1770 | struct uvc_frame *frame = NULL; | |
1771 | unsigned int i; | |
1772 | int ret; | |
1773 | ||
35f02a68 | 1774 | if (stream->nformats == 0) { |
c0efd232 LP |
1775 | uvc_printk(KERN_INFO, "No supported video formats found.\n"); |
1776 | return -EINVAL; | |
1777 | } | |
1778 | ||
35f02a68 LP |
1779 | atomic_set(&stream->active, 0); |
1780 | ||
1781 | /* Initialize the video buffers queue. */ | |
5712661d EG |
1782 | ret = uvc_queue_init(&stream->queue, stream->type, !uvc_no_drop_param); |
1783 | if (ret) | |
1784 | return ret; | |
35f02a68 | 1785 | |
c0efd232 LP |
1786 | /* Alternate setting 0 should be the default, yet the XBox Live Vision |
1787 | * Cam (and possibly other devices) crash or otherwise misbehave if | |
1788 | * they don't receive a SET_INTERFACE request before any other video | |
1789 | * control request. | |
1790 | */ | |
35f02a68 | 1791 | usb_set_interface(stream->dev->udev, stream->intfnum, 0); |
c0efd232 | 1792 | |
72362422 LP |
1793 | /* Set the streaming probe control with default streaming parameters |
1794 | * retrieved from the device. Webcams that don't suport GET_DEF | |
1795 | * requests on the probe control will just keep their current streaming | |
1796 | * parameters. | |
c0efd232 | 1797 | */ |
35f02a68 LP |
1798 | if (uvc_get_video_ctrl(stream, probe, 1, UVC_GET_DEF) == 0) |
1799 | uvc_set_video_ctrl(stream, probe, 1); | |
72362422 LP |
1800 | |
1801 | /* Initialize the streaming parameters with the probe control current | |
1802 | * value. This makes sure SET_CUR requests on the streaming commit | |
1803 | * control will always use values retrieved from a successful GET_CUR | |
1804 | * request on the probe control, as required by the UVC specification. | |
1805 | */ | |
35f02a68 | 1806 | ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR); |
b482d923 | 1807 | if (ret < 0) |
c0efd232 LP |
1808 | return ret; |
1809 | ||
1810 | /* Check if the default format descriptor exists. Use the first | |
1811 | * available format otherwise. | |
1812 | */ | |
35f02a68 LP |
1813 | for (i = stream->nformats; i > 0; --i) { |
1814 | format = &stream->format[i-1]; | |
c0efd232 LP |
1815 | if (format->index == probe->bFormatIndex) |
1816 | break; | |
1817 | } | |
1818 | ||
1819 | if (format->nframes == 0) { | |
1820 | uvc_printk(KERN_INFO, "No frame descriptor found for the " | |
1821 | "default format.\n"); | |
1822 | return -EINVAL; | |
1823 | } | |
1824 | ||
1825 | /* Zero bFrameIndex might be correct. Stream-based formats (including | |
1826 | * MPEG-2 TS and DV) do not support frames but have a dummy frame | |
1827 | * descriptor with bFrameIndex set to zero. If the default frame | |
078f8947 | 1828 | * descriptor is not found, use the first available frame. |
c0efd232 LP |
1829 | */ |
1830 | for (i = format->nframes; i > 0; --i) { | |
1831 | frame = &format->frame[i-1]; | |
1832 | if (frame->bFrameIndex == probe->bFrameIndex) | |
1833 | break; | |
1834 | } | |
1835 | ||
c0efd232 LP |
1836 | probe->bFormatIndex = format->index; |
1837 | probe->bFrameIndex = frame->bFrameIndex; | |
c0efd232 | 1838 | |
815adc46 | 1839 | stream->def_format = format; |
35f02a68 LP |
1840 | stream->cur_format = format; |
1841 | stream->cur_frame = frame; | |
c0efd232 LP |
1842 | |
1843 | /* Select the video decoding function */ | |
35f02a68 LP |
1844 | if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
1845 | if (stream->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT) | |
1846 | stream->decode = uvc_video_decode_isight; | |
1847 | else if (stream->intf->num_altsetting > 1) | |
1848 | stream->decode = uvc_video_decode_isoc; | |
ff924203 | 1849 | else |
35f02a68 | 1850 | stream->decode = uvc_video_decode_bulk; |
ff924203 | 1851 | } else { |
35f02a68 LP |
1852 | if (stream->intf->num_altsetting == 1) |
1853 | stream->decode = uvc_video_encode_bulk; | |
ff924203 LP |
1854 | else { |
1855 | uvc_printk(KERN_INFO, "Isochronous endpoints are not " | |
1856 | "supported for video output devices.\n"); | |
1857 | return -EINVAL; | |
1858 | } | |
1859 | } | |
c0efd232 LP |
1860 | |
1861 | return 0; | |
1862 | } | |
1863 | ||
1864 | /* | |
1865 | * Enable or disable the video stream. | |
1866 | */ | |
35f02a68 | 1867 | int uvc_video_enable(struct uvc_streaming *stream, int enable) |
c0efd232 LP |
1868 | { |
1869 | int ret; | |
1870 | ||
1871 | if (!enable) { | |
35f02a68 | 1872 | uvc_uninit_video(stream, 1); |
b1e43f23 OR |
1873 | if (stream->intf->num_altsetting > 1) { |
1874 | usb_set_interface(stream->dev->udev, | |
1875 | stream->intfnum, 0); | |
1876 | } else { | |
1877 | /* UVC doesn't specify how to inform a bulk-based device | |
1878 | * when the video stream is stopped. Windows sends a | |
1879 | * CLEAR_FEATURE(HALT) request to the video streaming | |
1880 | * bulk endpoint, mimic the same behaviour. | |
1881 | */ | |
1882 | unsigned int epnum = stream->header.bEndpointAddress | |
1883 | & USB_ENDPOINT_NUMBER_MASK; | |
1884 | unsigned int dir = stream->header.bEndpointAddress | |
1885 | & USB_ENDPOINT_DIR_MASK; | |
1886 | unsigned int pipe; | |
1887 | ||
1888 | pipe = usb_sndbulkpipe(stream->dev->udev, epnum) | dir; | |
1889 | usb_clear_halt(stream->dev->udev, pipe); | |
1890 | } | |
1891 | ||
35f02a68 | 1892 | uvc_queue_enable(&stream->queue, 0); |
ed0ee0ce | 1893 | uvc_video_clock_cleanup(stream); |
c0efd232 LP |
1894 | return 0; |
1895 | } | |
1896 | ||
ed0ee0ce | 1897 | ret = uvc_video_clock_init(stream); |
35f02a68 | 1898 | if (ret < 0) |
c0efd232 LP |
1899 | return ret; |
1900 | ||
ed0ee0ce LP |
1901 | ret = uvc_queue_enable(&stream->queue, 1); |
1902 | if (ret < 0) | |
1903 | goto error_queue; | |
1904 | ||
23867b25 | 1905 | /* Commit the streaming parameters. */ |
35f02a68 | 1906 | ret = uvc_commit_video(stream, &stream->ctrl); |
ed0ee0ce LP |
1907 | if (ret < 0) |
1908 | goto error_commit; | |
23867b25 | 1909 | |
24c3aae0 | 1910 | ret = uvc_init_video(stream, GFP_KERNEL); |
ed0ee0ce LP |
1911 | if (ret < 0) |
1912 | goto error_video; | |
1913 | ||
1914 | return 0; | |
1915 | ||
1916 | error_video: | |
1917 | usb_set_interface(stream->dev->udev, stream->intfnum, 0); | |
1918 | error_commit: | |
1919 | uvc_queue_enable(&stream->queue, 0); | |
1920 | error_queue: | |
1921 | uvc_video_clock_cleanup(stream); | |
f87086e3 | 1922 | |
24c3aae0 LP |
1923 | return ret; |
1924 | } |