xref: /linux/drivers/media/usb/uvc/uvc_video.c (revision b1992c3772e69a6fd0e3fc81cd4d2820c8b6eca0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *      uvc_video.c  --  USB Video Class driver - Video handling
4  *
5  *      Copyright (C) 2005-2010
6  *          Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7  */
8 
9 #include <linux/dma-mapping.h>
10 #include <linux/highmem.h>
11 #include <linux/kernel.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/usb.h>
16 #include <linux/usb/hcd.h>
17 #include <linux/videodev2.h>
18 #include <linux/vmalloc.h>
19 #include <linux/wait.h>
20 #include <linux/atomic.h>
21 #include <asm/unaligned.h>
22 
23 #include <media/v4l2-common.h>
24 
25 #include "uvcvideo.h"
26 
27 /* ------------------------------------------------------------------------
28  * UVC Controls
29  */
30 
31 static int __uvc_query_ctrl(struct uvc_device *dev, u8 query, u8 unit,
32 			u8 intfnum, u8 cs, void *data, u16 size,
33 			int timeout)
34 {
35 	u8 type = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
36 	unsigned int pipe;
37 
38 	pipe = (query & 0x80) ? usb_rcvctrlpipe(dev->udev, 0)
39 			      : usb_sndctrlpipe(dev->udev, 0);
40 	type |= (query & 0x80) ? USB_DIR_IN : USB_DIR_OUT;
41 
42 	return usb_control_msg(dev->udev, pipe, query, type, cs << 8,
43 			unit << 8 | intfnum, data, size, timeout);
44 }
45 
46 static const char *uvc_query_name(u8 query)
47 {
48 	switch (query) {
49 	case UVC_SET_CUR:
50 		return "SET_CUR";
51 	case UVC_GET_CUR:
52 		return "GET_CUR";
53 	case UVC_GET_MIN:
54 		return "GET_MIN";
55 	case UVC_GET_MAX:
56 		return "GET_MAX";
57 	case UVC_GET_RES:
58 		return "GET_RES";
59 	case UVC_GET_LEN:
60 		return "GET_LEN";
61 	case UVC_GET_INFO:
62 		return "GET_INFO";
63 	case UVC_GET_DEF:
64 		return "GET_DEF";
65 	default:
66 		return "<invalid>";
67 	}
68 }
69 
70 int uvc_query_ctrl(struct uvc_device *dev, u8 query, u8 unit,
71 			u8 intfnum, u8 cs, void *data, u16 size)
72 {
73 	int ret;
74 	u8 error;
75 	u8 tmp;
76 
77 	ret = __uvc_query_ctrl(dev, query, unit, intfnum, cs, data, size,
78 				UVC_CTRL_CONTROL_TIMEOUT);
79 	if (likely(ret == size))
80 		return 0;
81 
82 	if (ret != -EPIPE) {
83 		dev_err(&dev->udev->dev,
84 			"Failed to query (%s) UVC control %u on unit %u: %d (exp. %u).\n",
85 			uvc_query_name(query), cs, unit, ret, size);
86 		return ret < 0 ? ret : -EPIPE;
87 	}
88 
89 	/* Reuse data[0] to request the error code. */
90 	tmp = *(u8 *)data;
91 
92 	ret = __uvc_query_ctrl(dev, UVC_GET_CUR, 0, intfnum,
93 			       UVC_VC_REQUEST_ERROR_CODE_CONTROL, data, 1,
94 			       UVC_CTRL_CONTROL_TIMEOUT);
95 
96 	error = *(u8 *)data;
97 	*(u8 *)data = tmp;
98 
99 	if (ret != 1)
100 		return ret < 0 ? ret : -EPIPE;
101 
102 	uvc_dbg(dev, CONTROL, "Control error %u\n", error);
103 
104 	switch (error) {
105 	case 0:
106 		/* Cannot happen - we received a STALL */
107 		return -EPIPE;
108 	case 1: /* Not ready */
109 		return -EBUSY;
110 	case 2: /* Wrong state */
111 		return -EACCES;
112 	case 3: /* Power */
113 		return -EREMOTE;
114 	case 4: /* Out of range */
115 		return -ERANGE;
116 	case 5: /* Invalid unit */
117 	case 6: /* Invalid control */
118 	case 7: /* Invalid Request */
119 		/*
120 		 * The firmware has not properly implemented
121 		 * the control or there has been a HW error.
122 		 */
123 		return -EIO;
124 	case 8: /* Invalid value within range */
125 		return -EINVAL;
126 	default: /* reserved or unknown */
127 		break;
128 	}
129 
130 	return -EPIPE;
131 }
132 
133 static const struct usb_device_id elgato_cam_link_4k = {
134 	USB_DEVICE(0x0fd9, 0x0066)
135 };
136 
137 static void uvc_fixup_video_ctrl(struct uvc_streaming *stream,
138 	struct uvc_streaming_control *ctrl)
139 {
140 	const struct uvc_format *format = NULL;
141 	const struct uvc_frame *frame = NULL;
142 	unsigned int i;
143 
144 	/*
145 	 * The response of the Elgato Cam Link 4K is incorrect: The second byte
146 	 * contains bFormatIndex (instead of being the second byte of bmHint).
147 	 * The first byte is always zero. The third byte is always 1.
148 	 *
149 	 * The UVC 1.5 class specification defines the first five bits in the
150 	 * bmHint bitfield. The remaining bits are reserved and should be zero.
151 	 * Therefore a valid bmHint will be less than 32.
152 	 *
153 	 * Latest Elgato Cam Link 4K firmware as of 2021-03-23 needs this fix.
154 	 * MCU: 20.02.19, FPGA: 67
155 	 */
156 	if (usb_match_one_id(stream->dev->intf, &elgato_cam_link_4k) &&
157 	    ctrl->bmHint > 255) {
158 		u8 corrected_format_index = ctrl->bmHint >> 8;
159 
160 		uvc_dbg(stream->dev, VIDEO,
161 			"Correct USB video probe response from {bmHint: 0x%04x, bFormatIndex: %u} to {bmHint: 0x%04x, bFormatIndex: %u}\n",
162 			ctrl->bmHint, ctrl->bFormatIndex,
163 			1, corrected_format_index);
164 		ctrl->bmHint = 1;
165 		ctrl->bFormatIndex = corrected_format_index;
166 	}
167 
168 	for (i = 0; i < stream->nformats; ++i) {
169 		if (stream->formats[i].index == ctrl->bFormatIndex) {
170 			format = &stream->formats[i];
171 			break;
172 		}
173 	}
174 
175 	if (format == NULL)
176 		return;
177 
178 	for (i = 0; i < format->nframes; ++i) {
179 		if (format->frames[i].bFrameIndex == ctrl->bFrameIndex) {
180 			frame = &format->frames[i];
181 			break;
182 		}
183 	}
184 
185 	if (frame == NULL)
186 		return;
187 
188 	if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) ||
189 	     (ctrl->dwMaxVideoFrameSize == 0 &&
190 	      stream->dev->uvc_version < 0x0110))
191 		ctrl->dwMaxVideoFrameSize =
192 			frame->dwMaxVideoFrameBufferSize;
193 
194 	/*
195 	 * The "TOSHIBA Web Camera - 5M" Chicony device (04f2:b50b) seems to
196 	 * compute the bandwidth on 16 bits and erroneously sign-extend it to
197 	 * 32 bits, resulting in a huge bandwidth value. Detect and fix that
198 	 * condition by setting the 16 MSBs to 0 when they're all equal to 1.
199 	 */
200 	if ((ctrl->dwMaxPayloadTransferSize & 0xffff0000) == 0xffff0000)
201 		ctrl->dwMaxPayloadTransferSize &= ~0xffff0000;
202 
203 	if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) &&
204 	    stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH &&
205 	    stream->intf->num_altsetting > 1) {
206 		u32 interval;
207 		u32 bandwidth;
208 
209 		interval = (ctrl->dwFrameInterval > 100000)
210 			 ? ctrl->dwFrameInterval
211 			 : frame->dwFrameInterval[0];
212 
213 		/*
214 		 * Compute a bandwidth estimation by multiplying the frame
215 		 * size by the number of video frames per second, divide the
216 		 * result by the number of USB frames (or micro-frames for
217 		 * high-speed devices) per second and add the UVC header size
218 		 * (assumed to be 12 bytes long).
219 		 */
220 		bandwidth = frame->wWidth * frame->wHeight / 8 * format->bpp;
221 		bandwidth *= 10000000 / interval + 1;
222 		bandwidth /= 1000;
223 		if (stream->dev->udev->speed == USB_SPEED_HIGH)
224 			bandwidth /= 8;
225 		bandwidth += 12;
226 
227 		/*
228 		 * The bandwidth estimate is too low for many cameras. Don't use
229 		 * maximum packet sizes lower than 1024 bytes to try and work
230 		 * around the problem. According to measurements done on two
231 		 * different camera models, the value is high enough to get most
232 		 * resolutions working while not preventing two simultaneous
233 		 * VGA streams at 15 fps.
234 		 */
235 		bandwidth = max_t(u32, bandwidth, 1024);
236 
237 		ctrl->dwMaxPayloadTransferSize = bandwidth;
238 	}
239 }
240 
241 static size_t uvc_video_ctrl_size(struct uvc_streaming *stream)
242 {
243 	/*
244 	 * Return the size of the video probe and commit controls, which depends
245 	 * on the protocol version.
246 	 */
247 	if (stream->dev->uvc_version < 0x0110)
248 		return 26;
249 	else if (stream->dev->uvc_version < 0x0150)
250 		return 34;
251 	else
252 		return 48;
253 }
254 
255 static int uvc_get_video_ctrl(struct uvc_streaming *stream,
256 	struct uvc_streaming_control *ctrl, int probe, u8 query)
257 {
258 	u16 size = uvc_video_ctrl_size(stream);
259 	u8 *data;
260 	int ret;
261 
262 	if ((stream->dev->quirks & UVC_QUIRK_PROBE_DEF) &&
263 			query == UVC_GET_DEF)
264 		return -EIO;
265 
266 	data = kmalloc(size, GFP_KERNEL);
267 	if (data == NULL)
268 		return -ENOMEM;
269 
270 	ret = __uvc_query_ctrl(stream->dev, query, 0, stream->intfnum,
271 		probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
272 		size, uvc_timeout_param);
273 
274 	if ((query == UVC_GET_MIN || query == UVC_GET_MAX) && ret == 2) {
275 		/*
276 		 * Some cameras, mostly based on Bison Electronics chipsets,
277 		 * answer a GET_MIN or GET_MAX request with the wCompQuality
278 		 * field only.
279 		 */
280 		uvc_warn_once(stream->dev, UVC_WARN_MINMAX, "UVC non "
281 			"compliance - GET_MIN/MAX(PROBE) incorrectly "
282 			"supported. Enabling workaround.\n");
283 		memset(ctrl, 0, sizeof(*ctrl));
284 		ctrl->wCompQuality = le16_to_cpup((__le16 *)data);
285 		ret = 0;
286 		goto out;
287 	} else if (query == UVC_GET_DEF && probe == 1 && ret != size) {
288 		/*
289 		 * Many cameras don't support the GET_DEF request on their
290 		 * video probe control. Warn once and return, the caller will
291 		 * fall back to GET_CUR.
292 		 */
293 		uvc_warn_once(stream->dev, UVC_WARN_PROBE_DEF, "UVC non "
294 			"compliance - GET_DEF(PROBE) not supported. "
295 			"Enabling workaround.\n");
296 		ret = -EIO;
297 		goto out;
298 	} else if (ret != size) {
299 		dev_err(&stream->intf->dev,
300 			"Failed to query (%u) UVC %s control : %d (exp. %u).\n",
301 			query, probe ? "probe" : "commit", ret, size);
302 		ret = (ret == -EPROTO) ? -EPROTO : -EIO;
303 		goto out;
304 	}
305 
306 	ctrl->bmHint = le16_to_cpup((__le16 *)&data[0]);
307 	ctrl->bFormatIndex = data[2];
308 	ctrl->bFrameIndex = data[3];
309 	ctrl->dwFrameInterval = le32_to_cpup((__le32 *)&data[4]);
310 	ctrl->wKeyFrameRate = le16_to_cpup((__le16 *)&data[8]);
311 	ctrl->wPFrameRate = le16_to_cpup((__le16 *)&data[10]);
312 	ctrl->wCompQuality = le16_to_cpup((__le16 *)&data[12]);
313 	ctrl->wCompWindowSize = le16_to_cpup((__le16 *)&data[14]);
314 	ctrl->wDelay = le16_to_cpup((__le16 *)&data[16]);
315 	ctrl->dwMaxVideoFrameSize = get_unaligned_le32(&data[18]);
316 	ctrl->dwMaxPayloadTransferSize = get_unaligned_le32(&data[22]);
317 
318 	if (size >= 34) {
319 		ctrl->dwClockFrequency = get_unaligned_le32(&data[26]);
320 		ctrl->bmFramingInfo = data[30];
321 		ctrl->bPreferedVersion = data[31];
322 		ctrl->bMinVersion = data[32];
323 		ctrl->bMaxVersion = data[33];
324 	} else {
325 		ctrl->dwClockFrequency = stream->dev->clock_frequency;
326 		ctrl->bmFramingInfo = 0;
327 		ctrl->bPreferedVersion = 0;
328 		ctrl->bMinVersion = 0;
329 		ctrl->bMaxVersion = 0;
330 	}
331 
332 	/*
333 	 * Some broken devices return null or wrong dwMaxVideoFrameSize and
334 	 * dwMaxPayloadTransferSize fields. Try to get the value from the
335 	 * format and frame descriptors.
336 	 */
337 	uvc_fixup_video_ctrl(stream, ctrl);
338 	ret = 0;
339 
340 out:
341 	kfree(data);
342 	return ret;
343 }
344 
345 static int uvc_set_video_ctrl(struct uvc_streaming *stream,
346 	struct uvc_streaming_control *ctrl, int probe)
347 {
348 	u16 size = uvc_video_ctrl_size(stream);
349 	u8 *data;
350 	int ret;
351 
352 	data = kzalloc(size, GFP_KERNEL);
353 	if (data == NULL)
354 		return -ENOMEM;
355 
356 	*(__le16 *)&data[0] = cpu_to_le16(ctrl->bmHint);
357 	data[2] = ctrl->bFormatIndex;
358 	data[3] = ctrl->bFrameIndex;
359 	*(__le32 *)&data[4] = cpu_to_le32(ctrl->dwFrameInterval);
360 	*(__le16 *)&data[8] = cpu_to_le16(ctrl->wKeyFrameRate);
361 	*(__le16 *)&data[10] = cpu_to_le16(ctrl->wPFrameRate);
362 	*(__le16 *)&data[12] = cpu_to_le16(ctrl->wCompQuality);
363 	*(__le16 *)&data[14] = cpu_to_le16(ctrl->wCompWindowSize);
364 	*(__le16 *)&data[16] = cpu_to_le16(ctrl->wDelay);
365 	put_unaligned_le32(ctrl->dwMaxVideoFrameSize, &data[18]);
366 	put_unaligned_le32(ctrl->dwMaxPayloadTransferSize, &data[22]);
367 
368 	if (size >= 34) {
369 		put_unaligned_le32(ctrl->dwClockFrequency, &data[26]);
370 		data[30] = ctrl->bmFramingInfo;
371 		data[31] = ctrl->bPreferedVersion;
372 		data[32] = ctrl->bMinVersion;
373 		data[33] = ctrl->bMaxVersion;
374 	}
375 
376 	ret = __uvc_query_ctrl(stream->dev, UVC_SET_CUR, 0, stream->intfnum,
377 		probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
378 		size, uvc_timeout_param);
379 	if (ret != size) {
380 		dev_err(&stream->intf->dev,
381 			"Failed to set UVC %s control : %d (exp. %u).\n",
382 			probe ? "probe" : "commit", ret, size);
383 		ret = -EIO;
384 	}
385 
386 	kfree(data);
387 	return ret;
388 }
389 
390 int uvc_probe_video(struct uvc_streaming *stream,
391 	struct uvc_streaming_control *probe)
392 {
393 	struct uvc_streaming_control probe_min, probe_max;
394 	unsigned int i;
395 	int ret;
396 
397 	/*
398 	 * Perform probing. The device should adjust the requested values
399 	 * according to its capabilities. However, some devices, namely the
400 	 * first generation UVC Logitech webcams, don't implement the Video
401 	 * Probe control properly, and just return the needed bandwidth. For
402 	 * that reason, if the needed bandwidth exceeds the maximum available
403 	 * bandwidth, try to lower the quality.
404 	 */
405 	ret = uvc_set_video_ctrl(stream, probe, 1);
406 	if (ret < 0)
407 		goto done;
408 
409 	/* Get the minimum and maximum values for compression settings. */
410 	if (!(stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX)) {
411 		ret = uvc_get_video_ctrl(stream, &probe_min, 1, UVC_GET_MIN);
412 		if (ret < 0)
413 			goto done;
414 		ret = uvc_get_video_ctrl(stream, &probe_max, 1, UVC_GET_MAX);
415 		if (ret < 0)
416 			goto done;
417 
418 		probe->wCompQuality = probe_max.wCompQuality;
419 	}
420 
421 	for (i = 0; i < 2; ++i) {
422 		ret = uvc_set_video_ctrl(stream, probe, 1);
423 		if (ret < 0)
424 			goto done;
425 		ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
426 		if (ret < 0)
427 			goto done;
428 
429 		if (stream->intf->num_altsetting == 1)
430 			break;
431 
432 		if (probe->dwMaxPayloadTransferSize <= stream->maxpsize)
433 			break;
434 
435 		if (stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX) {
436 			ret = -ENOSPC;
437 			goto done;
438 		}
439 
440 		/* TODO: negotiate compression parameters */
441 		probe->wKeyFrameRate = probe_min.wKeyFrameRate;
442 		probe->wPFrameRate = probe_min.wPFrameRate;
443 		probe->wCompQuality = probe_max.wCompQuality;
444 		probe->wCompWindowSize = probe_min.wCompWindowSize;
445 	}
446 
447 done:
448 	return ret;
449 }
450 
451 static int uvc_commit_video(struct uvc_streaming *stream,
452 			    struct uvc_streaming_control *probe)
453 {
454 	return uvc_set_video_ctrl(stream, probe, 0);
455 }
456 
457 /* -----------------------------------------------------------------------------
458  * Clocks and timestamps
459  */
460 
461 static inline ktime_t uvc_video_get_time(void)
462 {
463 	if (uvc_clock_param == CLOCK_MONOTONIC)
464 		return ktime_get();
465 	else
466 		return ktime_get_real();
467 }
468 
469 static void
470 uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf,
471 		       const u8 *data, int len)
472 {
473 	struct uvc_clock_sample *sample;
474 	unsigned int header_size;
475 	bool has_pts = false;
476 	bool has_scr = false;
477 	unsigned long flags;
478 	ktime_t time;
479 	u16 host_sof;
480 	u16 dev_sof;
481 
482 	switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) {
483 	case UVC_STREAM_PTS | UVC_STREAM_SCR:
484 		header_size = 12;
485 		has_pts = true;
486 		has_scr = true;
487 		break;
488 	case UVC_STREAM_PTS:
489 		header_size = 6;
490 		has_pts = true;
491 		break;
492 	case UVC_STREAM_SCR:
493 		header_size = 8;
494 		has_scr = true;
495 		break;
496 	default:
497 		header_size = 2;
498 		break;
499 	}
500 
501 	/* Check for invalid headers. */
502 	if (len < header_size)
503 		return;
504 
505 	/*
506 	 * Extract the timestamps:
507 	 *
508 	 * - store the frame PTS in the buffer structure
509 	 * - if the SCR field is present, retrieve the host SOF counter and
510 	 *   kernel timestamps and store them with the SCR STC and SOF fields
511 	 *   in the ring buffer
512 	 */
513 	if (has_pts && buf != NULL)
514 		buf->pts = get_unaligned_le32(&data[2]);
515 
516 	if (!has_scr)
517 		return;
518 
519 	/*
520 	 * To limit the amount of data, drop SCRs with an SOF identical to the
521 	 * previous one. This filtering is also needed to support UVC 1.5, where
522 	 * all the data packets of the same frame contains the same SOF. In that
523 	 * case only the first one will match the host_sof.
524 	 */
525 	dev_sof = get_unaligned_le16(&data[header_size - 2]);
526 	if (dev_sof == stream->clock.last_sof)
527 		return;
528 
529 	stream->clock.last_sof = dev_sof;
530 
531 	host_sof = usb_get_current_frame_number(stream->dev->udev);
532 	time = uvc_video_get_time();
533 
534 	/*
535 	 * The UVC specification allows device implementations that can't obtain
536 	 * the USB frame number to keep their own frame counters as long as they
537 	 * match the size and frequency of the frame number associated with USB
538 	 * SOF tokens. The SOF values sent by such devices differ from the USB
539 	 * SOF tokens by a fixed offset that needs to be estimated and accounted
540 	 * for to make timestamp recovery as accurate as possible.
541 	 *
542 	 * The offset is estimated the first time a device SOF value is received
543 	 * as the difference between the host and device SOF values. As the two
544 	 * SOF values can differ slightly due to transmission delays, consider
545 	 * that the offset is null if the difference is not higher than 10 ms
546 	 * (negative differences can not happen and are thus considered as an
547 	 * offset). The video commit control wDelay field should be used to
548 	 * compute a dynamic threshold instead of using a fixed 10 ms value, but
549 	 * devices don't report reliable wDelay values.
550 	 *
551 	 * See uvc_video_clock_host_sof() for an explanation regarding why only
552 	 * the 8 LSBs of the delta are kept.
553 	 */
554 	if (stream->clock.sof_offset == (u16)-1) {
555 		u16 delta_sof = (host_sof - dev_sof) & 255;
556 		if (delta_sof >= 10)
557 			stream->clock.sof_offset = delta_sof;
558 		else
559 			stream->clock.sof_offset = 0;
560 	}
561 
562 	dev_sof = (dev_sof + stream->clock.sof_offset) & 2047;
563 
564 	spin_lock_irqsave(&stream->clock.lock, flags);
565 
566 	sample = &stream->clock.samples[stream->clock.head];
567 	sample->dev_stc = get_unaligned_le32(&data[header_size - 6]);
568 	sample->dev_sof = dev_sof;
569 	sample->host_sof = host_sof;
570 	sample->host_time = time;
571 
572 	/* Update the sliding window head and count. */
573 	stream->clock.head = (stream->clock.head + 1) % stream->clock.size;
574 
575 	if (stream->clock.count < stream->clock.size)
576 		stream->clock.count++;
577 
578 	spin_unlock_irqrestore(&stream->clock.lock, flags);
579 }
580 
581 static void uvc_video_clock_reset(struct uvc_streaming *stream)
582 {
583 	struct uvc_clock *clock = &stream->clock;
584 
585 	clock->head = 0;
586 	clock->count = 0;
587 	clock->last_sof = -1;
588 	clock->sof_offset = -1;
589 }
590 
591 static int uvc_video_clock_init(struct uvc_streaming *stream)
592 {
593 	struct uvc_clock *clock = &stream->clock;
594 
595 	spin_lock_init(&clock->lock);
596 	clock->size = 32;
597 
598 	clock->samples = kmalloc_array(clock->size, sizeof(*clock->samples),
599 				       GFP_KERNEL);
600 	if (clock->samples == NULL)
601 		return -ENOMEM;
602 
603 	uvc_video_clock_reset(stream);
604 
605 	return 0;
606 }
607 
608 static void uvc_video_clock_cleanup(struct uvc_streaming *stream)
609 {
610 	kfree(stream->clock.samples);
611 	stream->clock.samples = NULL;
612 }
613 
614 /*
615  * uvc_video_clock_host_sof - Return the host SOF value for a clock sample
616  *
617  * Host SOF counters reported by usb_get_current_frame_number() usually don't
618  * cover the whole 11-bits SOF range (0-2047) but are limited to the HCI frame
619  * schedule window. They can be limited to 8, 9 or 10 bits depending on the host
620  * controller and its configuration.
621  *
622  * We thus need to recover the SOF value corresponding to the host frame number.
623  * As the device and host frame numbers are sampled in a short interval, the
624  * difference between their values should be equal to a small delta plus an
625  * integer multiple of 256 caused by the host frame number limited precision.
626  *
627  * To obtain the recovered host SOF value, compute the small delta by masking
628  * the high bits of the host frame counter and device SOF difference and add it
629  * to the device SOF value.
630  */
631 static u16 uvc_video_clock_host_sof(const struct uvc_clock_sample *sample)
632 {
633 	/* The delta value can be negative. */
634 	s8 delta_sof;
635 
636 	delta_sof = (sample->host_sof - sample->dev_sof) & 255;
637 
638 	return (sample->dev_sof + delta_sof) & 2047;
639 }
640 
641 /*
642  * uvc_video_clock_update - Update the buffer timestamp
643  *
644  * This function converts the buffer PTS timestamp to the host clock domain by
645  * going through the USB SOF clock domain and stores the result in the V4L2
646  * buffer timestamp field.
647  *
648  * The relationship between the device clock and the host clock isn't known.
649  * However, the device and the host share the common USB SOF clock which can be
650  * used to recover that relationship.
651  *
652  * The relationship between the device clock and the USB SOF clock is considered
653  * to be linear over the clock samples sliding window and is given by
654  *
655  * SOF = m * PTS + p
656  *
657  * Several methods to compute the slope (m) and intercept (p) can be used. As
658  * the clock drift should be small compared to the sliding window size, we
659  * assume that the line that goes through the points at both ends of the window
660  * is a good approximation. Naming those points P1 and P2, we get
661  *
662  * SOF = (SOF2 - SOF1) / (STC2 - STC1) * PTS
663  *     + (SOF1 * STC2 - SOF2 * STC1) / (STC2 - STC1)
664  *
665  * or
666  *
667  * SOF = ((SOF2 - SOF1) * PTS + SOF1 * STC2 - SOF2 * STC1) / (STC2 - STC1)   (1)
668  *
669  * to avoid losing precision in the division. Similarly, the host timestamp is
670  * computed with
671  *
672  * TS = ((TS2 - TS1) * SOF + TS1 * SOF2 - TS2 * SOF1) / (SOF2 - SOF1)	     (2)
673  *
674  * SOF values are coded on 11 bits by USB. We extend their precision with 16
675  * decimal bits, leading to a 11.16 coding.
676  *
677  * TODO: To avoid surprises with device clock values, PTS/STC timestamps should
678  * be normalized using the nominal device clock frequency reported through the
679  * UVC descriptors.
680  *
681  * Both the PTS/STC and SOF counters roll over, after a fixed but device
682  * specific amount of time for PTS/STC and after 2048ms for SOF. As long as the
683  * sliding window size is smaller than the rollover period, differences computed
684  * on unsigned integers will produce the correct result. However, the p term in
685  * the linear relations will be miscomputed.
686  *
687  * To fix the issue, we subtract a constant from the PTS and STC values to bring
688  * PTS to half the 32 bit STC range. The sliding window STC values then fit into
689  * the 32 bit range without any rollover.
690  *
691  * Similarly, we add 2048 to the device SOF values to make sure that the SOF
692  * computed by (1) will never be smaller than 0. This offset is then compensated
693  * by adding 2048 to the SOF values used in (2). However, this doesn't prevent
694  * rollovers between (1) and (2): the SOF value computed by (1) can be slightly
695  * lower than 4096, and the host SOF counters can have rolled over to 2048. This
696  * case is handled by subtracting 2048 from the SOF value if it exceeds the host
697  * SOF value at the end of the sliding window.
698  *
699  * Finally we subtract a constant from the host timestamps to bring the first
700  * timestamp of the sliding window to 1s.
701  */
702 void uvc_video_clock_update(struct uvc_streaming *stream,
703 			    struct vb2_v4l2_buffer *vbuf,
704 			    struct uvc_buffer *buf)
705 {
706 	struct uvc_clock *clock = &stream->clock;
707 	struct uvc_clock_sample *first;
708 	struct uvc_clock_sample *last;
709 	unsigned long flags;
710 	u64 timestamp;
711 	u32 delta_stc;
712 	u32 y1, y2;
713 	u32 x1, x2;
714 	u32 mean;
715 	u32 sof;
716 	u64 y;
717 
718 	if (!uvc_hw_timestamps_param)
719 		return;
720 
721 	/*
722 	 * We will get called from __vb2_queue_cancel() if there are buffers
723 	 * done but not dequeued by the user, but the sample array has already
724 	 * been released at that time. Just bail out in that case.
725 	 */
726 	if (!clock->samples)
727 		return;
728 
729 	spin_lock_irqsave(&clock->lock, flags);
730 
731 	if (clock->count < clock->size)
732 		goto done;
733 
734 	first = &clock->samples[clock->head];
735 	last = &clock->samples[(clock->head - 1) % clock->size];
736 
737 	/* First step, PTS to SOF conversion. */
738 	delta_stc = buf->pts - (1UL << 31);
739 	x1 = first->dev_stc - delta_stc;
740 	x2 = last->dev_stc - delta_stc;
741 	if (x1 == x2)
742 		goto done;
743 
744 	y1 = (first->dev_sof + 2048) << 16;
745 	y2 = (last->dev_sof + 2048) << 16;
746 	if (y2 < y1)
747 		y2 += 2048 << 16;
748 
749 	y = (u64)(y2 - y1) * (1ULL << 31) + (u64)y1 * (u64)x2
750 	  - (u64)y2 * (u64)x1;
751 	y = div_u64(y, x2 - x1);
752 
753 	sof = y;
754 
755 	uvc_dbg(stream->dev, CLOCK,
756 		"%s: PTS %u y %llu.%06llu SOF %u.%06llu (x1 %u x2 %u y1 %u y2 %u SOF offset %u)\n",
757 		stream->dev->name, buf->pts,
758 		y >> 16, div_u64((y & 0xffff) * 1000000, 65536),
759 		sof >> 16, div_u64(((u64)sof & 0xffff) * 1000000LLU, 65536),
760 		x1, x2, y1, y2, clock->sof_offset);
761 
762 	/* Second step, SOF to host clock conversion. */
763 	x1 = (uvc_video_clock_host_sof(first) + 2048) << 16;
764 	x2 = (uvc_video_clock_host_sof(last) + 2048) << 16;
765 	if (x2 < x1)
766 		x2 += 2048 << 16;
767 	if (x1 == x2)
768 		goto done;
769 
770 	y1 = NSEC_PER_SEC;
771 	y2 = (u32)ktime_to_ns(ktime_sub(last->host_time, first->host_time)) + y1;
772 
773 	/*
774 	 * Interpolated and host SOF timestamps can wrap around at slightly
775 	 * different times. Handle this by adding or removing 2048 to or from
776 	 * the computed SOF value to keep it close to the SOF samples mean
777 	 * value.
778 	 */
779 	mean = (x1 + x2) / 2;
780 	if (mean - (1024 << 16) > sof)
781 		sof += 2048 << 16;
782 	else if (sof > mean + (1024 << 16))
783 		sof -= 2048 << 16;
784 
785 	y = (u64)(y2 - y1) * (u64)sof + (u64)y1 * (u64)x2
786 	  - (u64)y2 * (u64)x1;
787 	y = div_u64(y, x2 - x1);
788 
789 	timestamp = ktime_to_ns(first->host_time) + y - y1;
790 
791 	uvc_dbg(stream->dev, CLOCK,
792 		"%s: SOF %u.%06llu y %llu ts %llu buf ts %llu (x1 %u/%u/%u x2 %u/%u/%u y1 %u y2 %u)\n",
793 		stream->dev->name,
794 		sof >> 16, div_u64(((u64)sof & 0xffff) * 1000000LLU, 65536),
795 		y, timestamp, vbuf->vb2_buf.timestamp,
796 		x1, first->host_sof, first->dev_sof,
797 		x2, last->host_sof, last->dev_sof, y1, y2);
798 
799 	/* Update the V4L2 buffer. */
800 	vbuf->vb2_buf.timestamp = timestamp;
801 
802 done:
803 	spin_unlock_irqrestore(&clock->lock, flags);
804 }
805 
806 /* ------------------------------------------------------------------------
807  * Stream statistics
808  */
809 
810 static void uvc_video_stats_decode(struct uvc_streaming *stream,
811 		const u8 *data, int len)
812 {
813 	unsigned int header_size;
814 	bool has_pts = false;
815 	bool has_scr = false;
816 	u16 scr_sof;
817 	u32 scr_stc;
818 	u32 pts;
819 
820 	if (stream->stats.stream.nb_frames == 0 &&
821 	    stream->stats.frame.nb_packets == 0)
822 		stream->stats.stream.start_ts = ktime_get();
823 
824 	switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) {
825 	case UVC_STREAM_PTS | UVC_STREAM_SCR:
826 		header_size = 12;
827 		has_pts = true;
828 		has_scr = true;
829 		break;
830 	case UVC_STREAM_PTS:
831 		header_size = 6;
832 		has_pts = true;
833 		break;
834 	case UVC_STREAM_SCR:
835 		header_size = 8;
836 		has_scr = true;
837 		break;
838 	default:
839 		header_size = 2;
840 		break;
841 	}
842 
843 	/* Check for invalid headers. */
844 	if (len < header_size || data[0] < header_size) {
845 		stream->stats.frame.nb_invalid++;
846 		return;
847 	}
848 
849 	/* Extract the timestamps. */
850 	if (has_pts)
851 		pts = get_unaligned_le32(&data[2]);
852 
853 	if (has_scr) {
854 		scr_stc = get_unaligned_le32(&data[header_size - 6]);
855 		scr_sof = get_unaligned_le16(&data[header_size - 2]);
856 	}
857 
858 	/* Is PTS constant through the whole frame ? */
859 	if (has_pts && stream->stats.frame.nb_pts) {
860 		if (stream->stats.frame.pts != pts) {
861 			stream->stats.frame.nb_pts_diffs++;
862 			stream->stats.frame.last_pts_diff =
863 				stream->stats.frame.nb_packets;
864 		}
865 	}
866 
867 	if (has_pts) {
868 		stream->stats.frame.nb_pts++;
869 		stream->stats.frame.pts = pts;
870 	}
871 
872 	/*
873 	 * Do all frames have a PTS in their first non-empty packet, or before
874 	 * their first empty packet ?
875 	 */
876 	if (stream->stats.frame.size == 0) {
877 		if (len > header_size)
878 			stream->stats.frame.has_initial_pts = has_pts;
879 		if (len == header_size && has_pts)
880 			stream->stats.frame.has_early_pts = true;
881 	}
882 
883 	/* Do the SCR.STC and SCR.SOF fields vary through the frame ? */
884 	if (has_scr && stream->stats.frame.nb_scr) {
885 		if (stream->stats.frame.scr_stc != scr_stc)
886 			stream->stats.frame.nb_scr_diffs++;
887 	}
888 
889 	if (has_scr) {
890 		/* Expand the SOF counter to 32 bits and store its value. */
891 		if (stream->stats.stream.nb_frames > 0 ||
892 		    stream->stats.frame.nb_scr > 0)
893 			stream->stats.stream.scr_sof_count +=
894 				(scr_sof - stream->stats.stream.scr_sof) % 2048;
895 		stream->stats.stream.scr_sof = scr_sof;
896 
897 		stream->stats.frame.nb_scr++;
898 		stream->stats.frame.scr_stc = scr_stc;
899 		stream->stats.frame.scr_sof = scr_sof;
900 
901 		if (scr_sof < stream->stats.stream.min_sof)
902 			stream->stats.stream.min_sof = scr_sof;
903 		if (scr_sof > stream->stats.stream.max_sof)
904 			stream->stats.stream.max_sof = scr_sof;
905 	}
906 
907 	/* Record the first non-empty packet number. */
908 	if (stream->stats.frame.size == 0 && len > header_size)
909 		stream->stats.frame.first_data = stream->stats.frame.nb_packets;
910 
911 	/* Update the frame size. */
912 	stream->stats.frame.size += len - header_size;
913 
914 	/* Update the packets counters. */
915 	stream->stats.frame.nb_packets++;
916 	if (len <= header_size)
917 		stream->stats.frame.nb_empty++;
918 
919 	if (data[1] & UVC_STREAM_ERR)
920 		stream->stats.frame.nb_errors++;
921 }
922 
923 static void uvc_video_stats_update(struct uvc_streaming *stream)
924 {
925 	struct uvc_stats_frame *frame = &stream->stats.frame;
926 
927 	uvc_dbg(stream->dev, STATS,
928 		"frame %u stats: %u/%u/%u packets, %u/%u/%u pts (%searly %sinitial), %u/%u scr, last pts/stc/sof %u/%u/%u\n",
929 		stream->sequence, frame->first_data,
930 		frame->nb_packets - frame->nb_empty, frame->nb_packets,
931 		frame->nb_pts_diffs, frame->last_pts_diff, frame->nb_pts,
932 		frame->has_early_pts ? "" : "!",
933 		frame->has_initial_pts ? "" : "!",
934 		frame->nb_scr_diffs, frame->nb_scr,
935 		frame->pts, frame->scr_stc, frame->scr_sof);
936 
937 	stream->stats.stream.nb_frames++;
938 	stream->stats.stream.nb_packets += stream->stats.frame.nb_packets;
939 	stream->stats.stream.nb_empty += stream->stats.frame.nb_empty;
940 	stream->stats.stream.nb_errors += stream->stats.frame.nb_errors;
941 	stream->stats.stream.nb_invalid += stream->stats.frame.nb_invalid;
942 
943 	if (frame->has_early_pts)
944 		stream->stats.stream.nb_pts_early++;
945 	if (frame->has_initial_pts)
946 		stream->stats.stream.nb_pts_initial++;
947 	if (frame->last_pts_diff <= frame->first_data)
948 		stream->stats.stream.nb_pts_constant++;
949 	if (frame->nb_scr >= frame->nb_packets - frame->nb_empty)
950 		stream->stats.stream.nb_scr_count_ok++;
951 	if (frame->nb_scr_diffs + 1 == frame->nb_scr)
952 		stream->stats.stream.nb_scr_diffs_ok++;
953 
954 	memset(&stream->stats.frame, 0, sizeof(stream->stats.frame));
955 }
956 
957 size_t uvc_video_stats_dump(struct uvc_streaming *stream, char *buf,
958 			    size_t size)
959 {
960 	unsigned int scr_sof_freq;
961 	unsigned int duration;
962 	size_t count = 0;
963 
964 	/*
965 	 * Compute the SCR.SOF frequency estimate. At the nominal 1kHz SOF
966 	 * frequency this will not overflow before more than 1h.
967 	 */
968 	duration = ktime_ms_delta(stream->stats.stream.stop_ts,
969 				  stream->stats.stream.start_ts);
970 	if (duration != 0)
971 		scr_sof_freq = stream->stats.stream.scr_sof_count * 1000
972 			     / duration;
973 	else
974 		scr_sof_freq = 0;
975 
976 	count += scnprintf(buf + count, size - count,
977 			   "frames:  %u\npackets: %u\nempty:   %u\n"
978 			   "errors:  %u\ninvalid: %u\n",
979 			   stream->stats.stream.nb_frames,
980 			   stream->stats.stream.nb_packets,
981 			   stream->stats.stream.nb_empty,
982 			   stream->stats.stream.nb_errors,
983 			   stream->stats.stream.nb_invalid);
984 	count += scnprintf(buf + count, size - count,
985 			   "pts: %u early, %u initial, %u ok\n",
986 			   stream->stats.stream.nb_pts_early,
987 			   stream->stats.stream.nb_pts_initial,
988 			   stream->stats.stream.nb_pts_constant);
989 	count += scnprintf(buf + count, size - count,
990 			   "scr: %u count ok, %u diff ok\n",
991 			   stream->stats.stream.nb_scr_count_ok,
992 			   stream->stats.stream.nb_scr_diffs_ok);
993 	count += scnprintf(buf + count, size - count,
994 			   "sof: %u <= sof <= %u, freq %u.%03u kHz\n",
995 			   stream->stats.stream.min_sof,
996 			   stream->stats.stream.max_sof,
997 			   scr_sof_freq / 1000, scr_sof_freq % 1000);
998 
999 	return count;
1000 }
1001 
1002 static void uvc_video_stats_start(struct uvc_streaming *stream)
1003 {
1004 	memset(&stream->stats, 0, sizeof(stream->stats));
1005 	stream->stats.stream.min_sof = 2048;
1006 }
1007 
1008 static void uvc_video_stats_stop(struct uvc_streaming *stream)
1009 {
1010 	stream->stats.stream.stop_ts = ktime_get();
1011 }
1012 
1013 /* ------------------------------------------------------------------------
1014  * Video codecs
1015  */
1016 
1017 /*
1018  * Video payload decoding is handled by uvc_video_decode_start(),
1019  * uvc_video_decode_data() and uvc_video_decode_end().
1020  *
1021  * uvc_video_decode_start is called with URB data at the start of a bulk or
1022  * isochronous payload. It processes header data and returns the header size
1023  * in bytes if successful. If an error occurs, it returns a negative error
1024  * code. The following error codes have special meanings.
1025  *
1026  * - EAGAIN informs the caller that the current video buffer should be marked
1027  *   as done, and that the function should be called again with the same data
1028  *   and a new video buffer. This is used when end of frame conditions can be
1029  *   reliably detected at the beginning of the next frame only.
1030  *
1031  * If an error other than -EAGAIN is returned, the caller will drop the current
1032  * payload. No call to uvc_video_decode_data and uvc_video_decode_end will be
1033  * made until the next payload. -ENODATA can be used to drop the current
1034  * payload if no other error code is appropriate.
1035  *
1036  * uvc_video_decode_data is called for every URB with URB data. It copies the
1037  * data to the video buffer.
1038  *
1039  * uvc_video_decode_end is called with header data at the end of a bulk or
1040  * isochronous payload. It performs any additional header data processing and
1041  * returns 0 or a negative error code if an error occurred. As header data have
1042  * already been processed by uvc_video_decode_start, this functions isn't
1043  * required to perform sanity checks a second time.
1044  *
1045  * For isochronous transfers where a payload is always transferred in a single
1046  * URB, the three functions will be called in a row.
1047  *
1048  * To let the decoder process header data and update its internal state even
1049  * when no video buffer is available, uvc_video_decode_start must be prepared
1050  * to be called with a NULL buf parameter. uvc_video_decode_data and
1051  * uvc_video_decode_end will never be called with a NULL buffer.
1052  */
1053 static int uvc_video_decode_start(struct uvc_streaming *stream,
1054 		struct uvc_buffer *buf, const u8 *data, int len)
1055 {
1056 	u8 fid;
1057 
1058 	/*
1059 	 * Sanity checks:
1060 	 * - packet must be at least 2 bytes long
1061 	 * - bHeaderLength value must be at least 2 bytes (see above)
1062 	 * - bHeaderLength value can't be larger than the packet size.
1063 	 */
1064 	if (len < 2 || data[0] < 2 || data[0] > len) {
1065 		stream->stats.frame.nb_invalid++;
1066 		return -EINVAL;
1067 	}
1068 
1069 	fid = data[1] & UVC_STREAM_FID;
1070 
1071 	/*
1072 	 * Increase the sequence number regardless of any buffer states, so
1073 	 * that discontinuous sequence numbers always indicate lost frames.
1074 	 */
1075 	if (stream->last_fid != fid) {
1076 		stream->sequence++;
1077 		if (stream->sequence)
1078 			uvc_video_stats_update(stream);
1079 	}
1080 
1081 	uvc_video_clock_decode(stream, buf, data, len);
1082 	uvc_video_stats_decode(stream, data, len);
1083 
1084 	/*
1085 	 * Store the payload FID bit and return immediately when the buffer is
1086 	 * NULL.
1087 	 */
1088 	if (buf == NULL) {
1089 		stream->last_fid = fid;
1090 		return -ENODATA;
1091 	}
1092 
1093 	/* Mark the buffer as bad if the error bit is set. */
1094 	if (data[1] & UVC_STREAM_ERR) {
1095 		uvc_dbg(stream->dev, FRAME,
1096 			"Marking buffer as bad (error bit set)\n");
1097 		buf->error = 1;
1098 	}
1099 
1100 	/*
1101 	 * Synchronize to the input stream by waiting for the FID bit to be
1102 	 * toggled when the buffer state is not UVC_BUF_STATE_ACTIVE.
1103 	 * stream->last_fid is initialized to -1, so the first isochronous
1104 	 * frame will always be in sync.
1105 	 *
1106 	 * If the device doesn't toggle the FID bit, invert stream->last_fid
1107 	 * when the EOF bit is set to force synchronisation on the next packet.
1108 	 */
1109 	if (buf->state != UVC_BUF_STATE_ACTIVE) {
1110 		if (fid == stream->last_fid) {
1111 			uvc_dbg(stream->dev, FRAME,
1112 				"Dropping payload (out of sync)\n");
1113 			if ((stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) &&
1114 			    (data[1] & UVC_STREAM_EOF))
1115 				stream->last_fid ^= UVC_STREAM_FID;
1116 			return -ENODATA;
1117 		}
1118 
1119 		buf->buf.field = V4L2_FIELD_NONE;
1120 		buf->buf.sequence = stream->sequence;
1121 		buf->buf.vb2_buf.timestamp = ktime_to_ns(uvc_video_get_time());
1122 
1123 		/* TODO: Handle PTS and SCR. */
1124 		buf->state = UVC_BUF_STATE_ACTIVE;
1125 	}
1126 
1127 	/*
1128 	 * Mark the buffer as done if we're at the beginning of a new frame.
1129 	 * End of frame detection is better implemented by checking the EOF
1130 	 * bit (FID bit toggling is delayed by one frame compared to the EOF
1131 	 * bit), but some devices don't set the bit at end of frame (and the
1132 	 * last payload can be lost anyway). We thus must check if the FID has
1133 	 * been toggled.
1134 	 *
1135 	 * stream->last_fid is initialized to -1, so the first isochronous
1136 	 * frame will never trigger an end of frame detection.
1137 	 *
1138 	 * Empty buffers (bytesused == 0) don't trigger end of frame detection
1139 	 * as it doesn't make sense to return an empty buffer. This also
1140 	 * avoids detecting end of frame conditions at FID toggling if the
1141 	 * previous payload had the EOF bit set.
1142 	 */
1143 	if (fid != stream->last_fid && buf->bytesused != 0) {
1144 		uvc_dbg(stream->dev, FRAME,
1145 			"Frame complete (FID bit toggled)\n");
1146 		buf->state = UVC_BUF_STATE_READY;
1147 		return -EAGAIN;
1148 	}
1149 
1150 	stream->last_fid = fid;
1151 
1152 	return data[0];
1153 }
1154 
1155 static inline enum dma_data_direction uvc_stream_dir(
1156 				struct uvc_streaming *stream)
1157 {
1158 	if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1159 		return DMA_FROM_DEVICE;
1160 	else
1161 		return DMA_TO_DEVICE;
1162 }
1163 
1164 static inline struct device *uvc_stream_to_dmadev(struct uvc_streaming *stream)
1165 {
1166 	return bus_to_hcd(stream->dev->udev->bus)->self.sysdev;
1167 }
1168 
1169 static int uvc_submit_urb(struct uvc_urb *uvc_urb, gfp_t mem_flags)
1170 {
1171 	/* Sync DMA. */
1172 	dma_sync_sgtable_for_device(uvc_stream_to_dmadev(uvc_urb->stream),
1173 				    uvc_urb->sgt,
1174 				    uvc_stream_dir(uvc_urb->stream));
1175 	return usb_submit_urb(uvc_urb->urb, mem_flags);
1176 }
1177 
1178 /*
1179  * uvc_video_decode_data_work: Asynchronous memcpy processing
1180  *
1181  * Copy URB data to video buffers in process context, releasing buffer
1182  * references and requeuing the URB when done.
1183  */
1184 static void uvc_video_copy_data_work(struct work_struct *work)
1185 {
1186 	struct uvc_urb *uvc_urb = container_of(work, struct uvc_urb, work);
1187 	unsigned int i;
1188 	int ret;
1189 
1190 	for (i = 0; i < uvc_urb->async_operations; i++) {
1191 		struct uvc_copy_op *op = &uvc_urb->copy_operations[i];
1192 
1193 		memcpy(op->dst, op->src, op->len);
1194 
1195 		/* Release reference taken on this buffer. */
1196 		uvc_queue_buffer_release(op->buf);
1197 	}
1198 
1199 	ret = uvc_submit_urb(uvc_urb, GFP_KERNEL);
1200 	if (ret < 0)
1201 		dev_err(&uvc_urb->stream->intf->dev,
1202 			"Failed to resubmit video URB (%d).\n", ret);
1203 }
1204 
1205 static void uvc_video_decode_data(struct uvc_urb *uvc_urb,
1206 		struct uvc_buffer *buf, const u8 *data, int len)
1207 {
1208 	unsigned int active_op = uvc_urb->async_operations;
1209 	struct uvc_copy_op *op = &uvc_urb->copy_operations[active_op];
1210 	unsigned int maxlen;
1211 
1212 	if (len <= 0)
1213 		return;
1214 
1215 	maxlen = buf->length - buf->bytesused;
1216 
1217 	/* Take a buffer reference for async work. */
1218 	kref_get(&buf->ref);
1219 
1220 	op->buf = buf;
1221 	op->src = data;
1222 	op->dst = buf->mem + buf->bytesused;
1223 	op->len = min_t(unsigned int, len, maxlen);
1224 
1225 	buf->bytesused += op->len;
1226 
1227 	/* Complete the current frame if the buffer size was exceeded. */
1228 	if (len > maxlen) {
1229 		uvc_dbg(uvc_urb->stream->dev, FRAME,
1230 			"Frame complete (overflow)\n");
1231 		buf->error = 1;
1232 		buf->state = UVC_BUF_STATE_READY;
1233 	}
1234 
1235 	uvc_urb->async_operations++;
1236 }
1237 
1238 static void uvc_video_decode_end(struct uvc_streaming *stream,
1239 		struct uvc_buffer *buf, const u8 *data, int len)
1240 {
1241 	/* Mark the buffer as done if the EOF marker is set. */
1242 	if (data[1] & UVC_STREAM_EOF && buf->bytesused != 0) {
1243 		uvc_dbg(stream->dev, FRAME, "Frame complete (EOF found)\n");
1244 		if (data[0] == len)
1245 			uvc_dbg(stream->dev, FRAME, "EOF in empty payload\n");
1246 		buf->state = UVC_BUF_STATE_READY;
1247 		if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID)
1248 			stream->last_fid ^= UVC_STREAM_FID;
1249 	}
1250 }
1251 
1252 /*
1253  * Video payload encoding is handled by uvc_video_encode_header() and
1254  * uvc_video_encode_data(). Only bulk transfers are currently supported.
1255  *
1256  * uvc_video_encode_header is called at the start of a payload. It adds header
1257  * data to the transfer buffer and returns the header size. As the only known
1258  * UVC output device transfers a whole frame in a single payload, the EOF bit
1259  * is always set in the header.
1260  *
1261  * uvc_video_encode_data is called for every URB and copies the data from the
1262  * video buffer to the transfer buffer.
1263  */
1264 static int uvc_video_encode_header(struct uvc_streaming *stream,
1265 		struct uvc_buffer *buf, u8 *data, int len)
1266 {
1267 	data[0] = 2;	/* Header length */
1268 	data[1] = UVC_STREAM_EOH | UVC_STREAM_EOF
1269 		| (stream->last_fid & UVC_STREAM_FID);
1270 	return 2;
1271 }
1272 
1273 static int uvc_video_encode_data(struct uvc_streaming *stream,
1274 		struct uvc_buffer *buf, u8 *data, int len)
1275 {
1276 	struct uvc_video_queue *queue = &stream->queue;
1277 	unsigned int nbytes;
1278 	void *mem;
1279 
1280 	/* Copy video data to the URB buffer. */
1281 	mem = buf->mem + queue->buf_used;
1282 	nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used);
1283 	nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size,
1284 			nbytes);
1285 	memcpy(data, mem, nbytes);
1286 
1287 	queue->buf_used += nbytes;
1288 
1289 	return nbytes;
1290 }
1291 
1292 /* ------------------------------------------------------------------------
1293  * Metadata
1294  */
1295 
1296 /*
1297  * Additionally to the payload headers we also want to provide the user with USB
1298  * Frame Numbers and system time values. The resulting buffer is thus composed
1299  * of blocks, containing a 64-bit timestamp in  nanoseconds, a 16-bit USB Frame
1300  * Number, and a copy of the payload header.
1301  *
1302  * Ideally we want to capture all payload headers for each frame. However, their
1303  * number is unknown and unbound. We thus drop headers that contain no vendor
1304  * data and that either contain no SCR value or an SCR value identical to the
1305  * previous header.
1306  */
1307 static void uvc_video_decode_meta(struct uvc_streaming *stream,
1308 				  struct uvc_buffer *meta_buf,
1309 				  const u8 *mem, unsigned int length)
1310 {
1311 	struct uvc_meta_buf *meta;
1312 	size_t len_std = 2;
1313 	bool has_pts, has_scr;
1314 	unsigned long flags;
1315 	unsigned int sof;
1316 	ktime_t time;
1317 	const u8 *scr;
1318 
1319 	if (!meta_buf || length == 2)
1320 		return;
1321 
1322 	if (meta_buf->length - meta_buf->bytesused <
1323 	    length + sizeof(meta->ns) + sizeof(meta->sof)) {
1324 		meta_buf->error = 1;
1325 		return;
1326 	}
1327 
1328 	has_pts = mem[1] & UVC_STREAM_PTS;
1329 	has_scr = mem[1] & UVC_STREAM_SCR;
1330 
1331 	if (has_pts) {
1332 		len_std += 4;
1333 		scr = mem + 6;
1334 	} else {
1335 		scr = mem + 2;
1336 	}
1337 
1338 	if (has_scr)
1339 		len_std += 6;
1340 
1341 	if (stream->meta.format == V4L2_META_FMT_UVC)
1342 		length = len_std;
1343 
1344 	if (length == len_std && (!has_scr ||
1345 				  !memcmp(scr, stream->clock.last_scr, 6)))
1346 		return;
1347 
1348 	meta = (struct uvc_meta_buf *)((u8 *)meta_buf->mem + meta_buf->bytesused);
1349 	local_irq_save(flags);
1350 	time = uvc_video_get_time();
1351 	sof = usb_get_current_frame_number(stream->dev->udev);
1352 	local_irq_restore(flags);
1353 	put_unaligned(ktime_to_ns(time), &meta->ns);
1354 	put_unaligned(sof, &meta->sof);
1355 
1356 	if (has_scr)
1357 		memcpy(stream->clock.last_scr, scr, 6);
1358 
1359 	meta->length = mem[0];
1360 	meta->flags  = mem[1];
1361 	memcpy(meta->buf, &mem[2], length - 2);
1362 	meta_buf->bytesused += length + sizeof(meta->ns) + sizeof(meta->sof);
1363 
1364 	uvc_dbg(stream->dev, FRAME,
1365 		"%s(): t-sys %lluns, SOF %u, len %u, flags 0x%x, PTS %u, STC %u frame SOF %u\n",
1366 		__func__, ktime_to_ns(time), meta->sof, meta->length,
1367 		meta->flags,
1368 		has_pts ? *(u32 *)meta->buf : 0,
1369 		has_scr ? *(u32 *)scr : 0,
1370 		has_scr ? *(u32 *)(scr + 4) & 0x7ff : 0);
1371 }
1372 
1373 /* ------------------------------------------------------------------------
1374  * URB handling
1375  */
1376 
1377 /*
1378  * Set error flag for incomplete buffer.
1379  */
1380 static void uvc_video_validate_buffer(const struct uvc_streaming *stream,
1381 				      struct uvc_buffer *buf)
1382 {
1383 	if (stream->ctrl.dwMaxVideoFrameSize != buf->bytesused &&
1384 	    !(stream->cur_format->flags & UVC_FMT_FLAG_COMPRESSED))
1385 		buf->error = 1;
1386 }
1387 
1388 /*
1389  * Completion handler for video URBs.
1390  */
1391 
1392 static void uvc_video_next_buffers(struct uvc_streaming *stream,
1393 		struct uvc_buffer **video_buf, struct uvc_buffer **meta_buf)
1394 {
1395 	uvc_video_validate_buffer(stream, *video_buf);
1396 
1397 	if (*meta_buf) {
1398 		struct vb2_v4l2_buffer *vb2_meta = &(*meta_buf)->buf;
1399 		const struct vb2_v4l2_buffer *vb2_video = &(*video_buf)->buf;
1400 
1401 		vb2_meta->sequence = vb2_video->sequence;
1402 		vb2_meta->field = vb2_video->field;
1403 		vb2_meta->vb2_buf.timestamp = vb2_video->vb2_buf.timestamp;
1404 
1405 		(*meta_buf)->state = UVC_BUF_STATE_READY;
1406 		if (!(*meta_buf)->error)
1407 			(*meta_buf)->error = (*video_buf)->error;
1408 		*meta_buf = uvc_queue_next_buffer(&stream->meta.queue,
1409 						  *meta_buf);
1410 	}
1411 	*video_buf = uvc_queue_next_buffer(&stream->queue, *video_buf);
1412 }
1413 
1414 static void uvc_video_decode_isoc(struct uvc_urb *uvc_urb,
1415 			struct uvc_buffer *buf, struct uvc_buffer *meta_buf)
1416 {
1417 	struct urb *urb = uvc_urb->urb;
1418 	struct uvc_streaming *stream = uvc_urb->stream;
1419 	u8 *mem;
1420 	int ret, i;
1421 
1422 	for (i = 0; i < urb->number_of_packets; ++i) {
1423 		if (urb->iso_frame_desc[i].status < 0) {
1424 			uvc_dbg(stream->dev, FRAME,
1425 				"USB isochronous frame lost (%d)\n",
1426 				urb->iso_frame_desc[i].status);
1427 			/* Mark the buffer as faulty. */
1428 			if (buf != NULL)
1429 				buf->error = 1;
1430 			continue;
1431 		}
1432 
1433 		/* Decode the payload header. */
1434 		mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1435 		do {
1436 			ret = uvc_video_decode_start(stream, buf, mem,
1437 				urb->iso_frame_desc[i].actual_length);
1438 			if (ret == -EAGAIN)
1439 				uvc_video_next_buffers(stream, &buf, &meta_buf);
1440 		} while (ret == -EAGAIN);
1441 
1442 		if (ret < 0)
1443 			continue;
1444 
1445 		uvc_video_decode_meta(stream, meta_buf, mem, ret);
1446 
1447 		/* Decode the payload data. */
1448 		uvc_video_decode_data(uvc_urb, buf, mem + ret,
1449 			urb->iso_frame_desc[i].actual_length - ret);
1450 
1451 		/* Process the header again. */
1452 		uvc_video_decode_end(stream, buf, mem,
1453 			urb->iso_frame_desc[i].actual_length);
1454 
1455 		if (buf->state == UVC_BUF_STATE_READY)
1456 			uvc_video_next_buffers(stream, &buf, &meta_buf);
1457 	}
1458 }
1459 
1460 static void uvc_video_decode_bulk(struct uvc_urb *uvc_urb,
1461 			struct uvc_buffer *buf, struct uvc_buffer *meta_buf)
1462 {
1463 	struct urb *urb = uvc_urb->urb;
1464 	struct uvc_streaming *stream = uvc_urb->stream;
1465 	u8 *mem;
1466 	int len, ret;
1467 
1468 	/*
1469 	 * Ignore ZLPs if they're not part of a frame, otherwise process them
1470 	 * to trigger the end of payload detection.
1471 	 */
1472 	if (urb->actual_length == 0 && stream->bulk.header_size == 0)
1473 		return;
1474 
1475 	mem = urb->transfer_buffer;
1476 	len = urb->actual_length;
1477 	stream->bulk.payload_size += len;
1478 
1479 	/*
1480 	 * If the URB is the first of its payload, decode and save the
1481 	 * header.
1482 	 */
1483 	if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) {
1484 		do {
1485 			ret = uvc_video_decode_start(stream, buf, mem, len);
1486 			if (ret == -EAGAIN)
1487 				uvc_video_next_buffers(stream, &buf, &meta_buf);
1488 		} while (ret == -EAGAIN);
1489 
1490 		/* If an error occurred skip the rest of the payload. */
1491 		if (ret < 0 || buf == NULL) {
1492 			stream->bulk.skip_payload = 1;
1493 		} else {
1494 			memcpy(stream->bulk.header, mem, ret);
1495 			stream->bulk.header_size = ret;
1496 
1497 			uvc_video_decode_meta(stream, meta_buf, mem, ret);
1498 
1499 			mem += ret;
1500 			len -= ret;
1501 		}
1502 	}
1503 
1504 	/*
1505 	 * The buffer queue might have been cancelled while a bulk transfer
1506 	 * was in progress, so we can reach here with buf equal to NULL. Make
1507 	 * sure buf is never dereferenced if NULL.
1508 	 */
1509 
1510 	/* Prepare video data for processing. */
1511 	if (!stream->bulk.skip_payload && buf != NULL)
1512 		uvc_video_decode_data(uvc_urb, buf, mem, len);
1513 
1514 	/*
1515 	 * Detect the payload end by a URB smaller than the maximum size (or
1516 	 * a payload size equal to the maximum) and process the header again.
1517 	 */
1518 	if (urb->actual_length < urb->transfer_buffer_length ||
1519 	    stream->bulk.payload_size >= stream->bulk.max_payload_size) {
1520 		if (!stream->bulk.skip_payload && buf != NULL) {
1521 			uvc_video_decode_end(stream, buf, stream->bulk.header,
1522 				stream->bulk.payload_size);
1523 			if (buf->state == UVC_BUF_STATE_READY)
1524 				uvc_video_next_buffers(stream, &buf, &meta_buf);
1525 		}
1526 
1527 		stream->bulk.header_size = 0;
1528 		stream->bulk.skip_payload = 0;
1529 		stream->bulk.payload_size = 0;
1530 	}
1531 }
1532 
1533 static void uvc_video_encode_bulk(struct uvc_urb *uvc_urb,
1534 	struct uvc_buffer *buf, struct uvc_buffer *meta_buf)
1535 {
1536 	struct urb *urb = uvc_urb->urb;
1537 	struct uvc_streaming *stream = uvc_urb->stream;
1538 
1539 	u8 *mem = urb->transfer_buffer;
1540 	int len = stream->urb_size, ret;
1541 
1542 	if (buf == NULL) {
1543 		urb->transfer_buffer_length = 0;
1544 		return;
1545 	}
1546 
1547 	/* If the URB is the first of its payload, add the header. */
1548 	if (stream->bulk.header_size == 0) {
1549 		ret = uvc_video_encode_header(stream, buf, mem, len);
1550 		stream->bulk.header_size = ret;
1551 		stream->bulk.payload_size += ret;
1552 		mem += ret;
1553 		len -= ret;
1554 	}
1555 
1556 	/* Process video data. */
1557 	ret = uvc_video_encode_data(stream, buf, mem, len);
1558 
1559 	stream->bulk.payload_size += ret;
1560 	len -= ret;
1561 
1562 	if (buf->bytesused == stream->queue.buf_used ||
1563 	    stream->bulk.payload_size == stream->bulk.max_payload_size) {
1564 		if (buf->bytesused == stream->queue.buf_used) {
1565 			stream->queue.buf_used = 0;
1566 			buf->state = UVC_BUF_STATE_READY;
1567 			buf->buf.sequence = ++stream->sequence;
1568 			uvc_queue_next_buffer(&stream->queue, buf);
1569 			stream->last_fid ^= UVC_STREAM_FID;
1570 		}
1571 
1572 		stream->bulk.header_size = 0;
1573 		stream->bulk.payload_size = 0;
1574 	}
1575 
1576 	urb->transfer_buffer_length = stream->urb_size - len;
1577 }
1578 
1579 static void uvc_video_complete(struct urb *urb)
1580 {
1581 	struct uvc_urb *uvc_urb = urb->context;
1582 	struct uvc_streaming *stream = uvc_urb->stream;
1583 	struct uvc_video_queue *queue = &stream->queue;
1584 	struct uvc_video_queue *qmeta = &stream->meta.queue;
1585 	struct vb2_queue *vb2_qmeta = stream->meta.vdev.queue;
1586 	struct uvc_buffer *buf = NULL;
1587 	struct uvc_buffer *buf_meta = NULL;
1588 	unsigned long flags;
1589 	int ret;
1590 
1591 	switch (urb->status) {
1592 	case 0:
1593 		break;
1594 
1595 	default:
1596 		dev_warn(&stream->intf->dev,
1597 			 "Non-zero status (%d) in video completion handler.\n",
1598 			 urb->status);
1599 		fallthrough;
1600 	case -ENOENT:		/* usb_poison_urb() called. */
1601 		if (stream->frozen)
1602 			return;
1603 		fallthrough;
1604 	case -ECONNRESET:	/* usb_unlink_urb() called. */
1605 	case -ESHUTDOWN:	/* The endpoint is being disabled. */
1606 		uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
1607 		if (vb2_qmeta)
1608 			uvc_queue_cancel(qmeta, urb->status == -ESHUTDOWN);
1609 		return;
1610 	}
1611 
1612 	buf = uvc_queue_get_current_buffer(queue);
1613 
1614 	if (vb2_qmeta) {
1615 		spin_lock_irqsave(&qmeta->irqlock, flags);
1616 		if (!list_empty(&qmeta->irqqueue))
1617 			buf_meta = list_first_entry(&qmeta->irqqueue,
1618 						    struct uvc_buffer, queue);
1619 		spin_unlock_irqrestore(&qmeta->irqlock, flags);
1620 	}
1621 
1622 	/* Re-initialise the URB async work. */
1623 	uvc_urb->async_operations = 0;
1624 
1625 	/* Sync DMA and invalidate vmap range. */
1626 	dma_sync_sgtable_for_cpu(uvc_stream_to_dmadev(uvc_urb->stream),
1627 				 uvc_urb->sgt, uvc_stream_dir(stream));
1628 	invalidate_kernel_vmap_range(uvc_urb->buffer,
1629 				     uvc_urb->stream->urb_size);
1630 
1631 	/*
1632 	 * Process the URB headers, and optionally queue expensive memcpy tasks
1633 	 * to be deferred to a work queue.
1634 	 */
1635 	stream->decode(uvc_urb, buf, buf_meta);
1636 
1637 	/* If no async work is needed, resubmit the URB immediately. */
1638 	if (!uvc_urb->async_operations) {
1639 		ret = uvc_submit_urb(uvc_urb, GFP_ATOMIC);
1640 		if (ret < 0)
1641 			dev_err(&stream->intf->dev,
1642 				"Failed to resubmit video URB (%d).\n", ret);
1643 		return;
1644 	}
1645 
1646 	queue_work(stream->async_wq, &uvc_urb->work);
1647 }
1648 
1649 /*
1650  * Free transfer buffers.
1651  */
1652 static void uvc_free_urb_buffers(struct uvc_streaming *stream)
1653 {
1654 	struct device *dma_dev = uvc_stream_to_dmadev(stream);
1655 	struct uvc_urb *uvc_urb;
1656 
1657 	for_each_uvc_urb(uvc_urb, stream) {
1658 		if (!uvc_urb->buffer)
1659 			continue;
1660 
1661 		dma_vunmap_noncontiguous(dma_dev, uvc_urb->buffer);
1662 		dma_free_noncontiguous(dma_dev, stream->urb_size, uvc_urb->sgt,
1663 				       uvc_stream_dir(stream));
1664 
1665 		uvc_urb->buffer = NULL;
1666 		uvc_urb->sgt = NULL;
1667 	}
1668 
1669 	stream->urb_size = 0;
1670 }
1671 
1672 static bool uvc_alloc_urb_buffer(struct uvc_streaming *stream,
1673 				 struct uvc_urb *uvc_urb, gfp_t gfp_flags)
1674 {
1675 	struct device *dma_dev = uvc_stream_to_dmadev(stream);
1676 
1677 	uvc_urb->sgt = dma_alloc_noncontiguous(dma_dev, stream->urb_size,
1678 					       uvc_stream_dir(stream),
1679 					       gfp_flags, 0);
1680 	if (!uvc_urb->sgt)
1681 		return false;
1682 	uvc_urb->dma = uvc_urb->sgt->sgl->dma_address;
1683 
1684 	uvc_urb->buffer = dma_vmap_noncontiguous(dma_dev, stream->urb_size,
1685 						 uvc_urb->sgt);
1686 	if (!uvc_urb->buffer) {
1687 		dma_free_noncontiguous(dma_dev, stream->urb_size,
1688 				       uvc_urb->sgt,
1689 				       uvc_stream_dir(stream));
1690 		uvc_urb->sgt = NULL;
1691 		return false;
1692 	}
1693 
1694 	return true;
1695 }
1696 
1697 /*
1698  * Allocate transfer buffers. This function can be called with buffers
1699  * already allocated when resuming from suspend, in which case it will
1700  * return without touching the buffers.
1701  *
1702  * Limit the buffer size to UVC_MAX_PACKETS bulk/isochronous packets. If the
1703  * system is too low on memory try successively smaller numbers of packets
1704  * until allocation succeeds.
1705  *
1706  * Return the number of allocated packets on success or 0 when out of memory.
1707  */
1708 static int uvc_alloc_urb_buffers(struct uvc_streaming *stream,
1709 	unsigned int size, unsigned int psize, gfp_t gfp_flags)
1710 {
1711 	unsigned int npackets;
1712 	unsigned int i;
1713 
1714 	/* Buffers are already allocated, bail out. */
1715 	if (stream->urb_size)
1716 		return stream->urb_size / psize;
1717 
1718 	/*
1719 	 * Compute the number of packets. Bulk endpoints might transfer UVC
1720 	 * payloads across multiple URBs.
1721 	 */
1722 	npackets = DIV_ROUND_UP(size, psize);
1723 	if (npackets > UVC_MAX_PACKETS)
1724 		npackets = UVC_MAX_PACKETS;
1725 
1726 	/* Retry allocations until one succeed. */
1727 	for (; npackets > 1; npackets /= 2) {
1728 		stream->urb_size = psize * npackets;
1729 
1730 		for (i = 0; i < UVC_URBS; ++i) {
1731 			struct uvc_urb *uvc_urb = &stream->uvc_urb[i];
1732 
1733 			if (!uvc_alloc_urb_buffer(stream, uvc_urb, gfp_flags)) {
1734 				uvc_free_urb_buffers(stream);
1735 				break;
1736 			}
1737 
1738 			uvc_urb->stream = stream;
1739 		}
1740 
1741 		if (i == UVC_URBS) {
1742 			uvc_dbg(stream->dev, VIDEO,
1743 				"Allocated %u URB buffers of %ux%u bytes each\n",
1744 				UVC_URBS, npackets, psize);
1745 			return npackets;
1746 		}
1747 	}
1748 
1749 	uvc_dbg(stream->dev, VIDEO,
1750 		"Failed to allocate URB buffers (%u bytes per packet)\n",
1751 		psize);
1752 	return 0;
1753 }
1754 
1755 /*
1756  * Uninitialize isochronous/bulk URBs and free transfer buffers.
1757  */
1758 static void uvc_video_stop_transfer(struct uvc_streaming *stream,
1759 				    int free_buffers)
1760 {
1761 	struct uvc_urb *uvc_urb;
1762 
1763 	uvc_video_stats_stop(stream);
1764 
1765 	/*
1766 	 * We must poison the URBs rather than kill them to ensure that even
1767 	 * after the completion handler returns, any asynchronous workqueues
1768 	 * will be prevented from resubmitting the URBs.
1769 	 */
1770 	for_each_uvc_urb(uvc_urb, stream)
1771 		usb_poison_urb(uvc_urb->urb);
1772 
1773 	flush_workqueue(stream->async_wq);
1774 
1775 	for_each_uvc_urb(uvc_urb, stream) {
1776 		usb_free_urb(uvc_urb->urb);
1777 		uvc_urb->urb = NULL;
1778 	}
1779 
1780 	if (free_buffers)
1781 		uvc_free_urb_buffers(stream);
1782 }
1783 
1784 /*
1785  * Compute the maximum number of bytes per interval for an endpoint.
1786  */
1787 u16 uvc_endpoint_max_bpi(struct usb_device *dev, struct usb_host_endpoint *ep)
1788 {
1789 	u16 psize;
1790 
1791 	switch (dev->speed) {
1792 	case USB_SPEED_SUPER:
1793 	case USB_SPEED_SUPER_PLUS:
1794 		return le16_to_cpu(ep->ss_ep_comp.wBytesPerInterval);
1795 	default:
1796 		psize = usb_endpoint_maxp(&ep->desc);
1797 		psize *= usb_endpoint_maxp_mult(&ep->desc);
1798 		return psize;
1799 	}
1800 }
1801 
1802 /*
1803  * Initialize isochronous URBs and allocate transfer buffers. The packet size
1804  * is given by the endpoint.
1805  */
1806 static int uvc_init_video_isoc(struct uvc_streaming *stream,
1807 	struct usb_host_endpoint *ep, gfp_t gfp_flags)
1808 {
1809 	struct urb *urb;
1810 	struct uvc_urb *uvc_urb;
1811 	unsigned int npackets, i;
1812 	u16 psize;
1813 	u32 size;
1814 
1815 	psize = uvc_endpoint_max_bpi(stream->dev->udev, ep);
1816 	size = stream->ctrl.dwMaxVideoFrameSize;
1817 
1818 	npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
1819 	if (npackets == 0)
1820 		return -ENOMEM;
1821 
1822 	size = npackets * psize;
1823 
1824 	for_each_uvc_urb(uvc_urb, stream) {
1825 		urb = usb_alloc_urb(npackets, gfp_flags);
1826 		if (urb == NULL) {
1827 			uvc_video_stop_transfer(stream, 1);
1828 			return -ENOMEM;
1829 		}
1830 
1831 		urb->dev = stream->dev->udev;
1832 		urb->context = uvc_urb;
1833 		urb->pipe = usb_rcvisocpipe(stream->dev->udev,
1834 				ep->desc.bEndpointAddress);
1835 		urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
1836 		urb->transfer_dma = uvc_urb->dma;
1837 		urb->interval = ep->desc.bInterval;
1838 		urb->transfer_buffer = uvc_urb->buffer;
1839 		urb->complete = uvc_video_complete;
1840 		urb->number_of_packets = npackets;
1841 		urb->transfer_buffer_length = size;
1842 
1843 		for (i = 0; i < npackets; ++i) {
1844 			urb->iso_frame_desc[i].offset = i * psize;
1845 			urb->iso_frame_desc[i].length = psize;
1846 		}
1847 
1848 		uvc_urb->urb = urb;
1849 	}
1850 
1851 	return 0;
1852 }
1853 
1854 /*
1855  * Initialize bulk URBs and allocate transfer buffers. The packet size is
1856  * given by the endpoint.
1857  */
1858 static int uvc_init_video_bulk(struct uvc_streaming *stream,
1859 	struct usb_host_endpoint *ep, gfp_t gfp_flags)
1860 {
1861 	struct urb *urb;
1862 	struct uvc_urb *uvc_urb;
1863 	unsigned int npackets, pipe;
1864 	u16 psize;
1865 	u32 size;
1866 
1867 	psize = usb_endpoint_maxp(&ep->desc);
1868 	size = stream->ctrl.dwMaxPayloadTransferSize;
1869 	stream->bulk.max_payload_size = size;
1870 
1871 	npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
1872 	if (npackets == 0)
1873 		return -ENOMEM;
1874 
1875 	size = npackets * psize;
1876 
1877 	if (usb_endpoint_dir_in(&ep->desc))
1878 		pipe = usb_rcvbulkpipe(stream->dev->udev,
1879 				       ep->desc.bEndpointAddress);
1880 	else
1881 		pipe = usb_sndbulkpipe(stream->dev->udev,
1882 				       ep->desc.bEndpointAddress);
1883 
1884 	if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1885 		size = 0;
1886 
1887 	for_each_uvc_urb(uvc_urb, stream) {
1888 		urb = usb_alloc_urb(0, gfp_flags);
1889 		if (urb == NULL) {
1890 			uvc_video_stop_transfer(stream, 1);
1891 			return -ENOMEM;
1892 		}
1893 
1894 		usb_fill_bulk_urb(urb, stream->dev->udev, pipe,	uvc_urb->buffer,
1895 				  size, uvc_video_complete, uvc_urb);
1896 		urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1897 		urb->transfer_dma = uvc_urb->dma;
1898 
1899 		uvc_urb->urb = urb;
1900 	}
1901 
1902 	return 0;
1903 }
1904 
1905 /*
1906  * Initialize isochronous/bulk URBs and allocate transfer buffers.
1907  */
1908 static int uvc_video_start_transfer(struct uvc_streaming *stream,
1909 				    gfp_t gfp_flags)
1910 {
1911 	struct usb_interface *intf = stream->intf;
1912 	struct usb_host_endpoint *ep;
1913 	struct uvc_urb *uvc_urb;
1914 	unsigned int i;
1915 	int ret;
1916 
1917 	stream->sequence = -1;
1918 	stream->last_fid = -1;
1919 	stream->bulk.header_size = 0;
1920 	stream->bulk.skip_payload = 0;
1921 	stream->bulk.payload_size = 0;
1922 
1923 	uvc_video_stats_start(stream);
1924 
1925 	if (intf->num_altsetting > 1) {
1926 		struct usb_host_endpoint *best_ep = NULL;
1927 		unsigned int best_psize = UINT_MAX;
1928 		unsigned int bandwidth;
1929 		unsigned int altsetting;
1930 		int intfnum = stream->intfnum;
1931 
1932 		/* Isochronous endpoint, select the alternate setting. */
1933 		bandwidth = stream->ctrl.dwMaxPayloadTransferSize;
1934 
1935 		if (bandwidth == 0) {
1936 			uvc_dbg(stream->dev, VIDEO,
1937 				"Device requested null bandwidth, defaulting to lowest\n");
1938 			bandwidth = 1;
1939 		} else {
1940 			uvc_dbg(stream->dev, VIDEO,
1941 				"Device requested %u B/frame bandwidth\n",
1942 				bandwidth);
1943 		}
1944 
1945 		for (i = 0; i < intf->num_altsetting; ++i) {
1946 			struct usb_host_interface *alts;
1947 			unsigned int psize;
1948 
1949 			alts = &intf->altsetting[i];
1950 			ep = uvc_find_endpoint(alts,
1951 				stream->header.bEndpointAddress);
1952 			if (ep == NULL)
1953 				continue;
1954 
1955 			/* Check if the bandwidth is high enough. */
1956 			psize = uvc_endpoint_max_bpi(stream->dev->udev, ep);
1957 			if (psize >= bandwidth && psize < best_psize) {
1958 				altsetting = alts->desc.bAlternateSetting;
1959 				best_psize = psize;
1960 				best_ep = ep;
1961 			}
1962 		}
1963 
1964 		if (best_ep == NULL) {
1965 			uvc_dbg(stream->dev, VIDEO,
1966 				"No fast enough alt setting for requested bandwidth\n");
1967 			return -EIO;
1968 		}
1969 
1970 		uvc_dbg(stream->dev, VIDEO,
1971 			"Selecting alternate setting %u (%u B/frame bandwidth)\n",
1972 			altsetting, best_psize);
1973 
1974 		/*
1975 		 * Some devices, namely the Logitech C910 and B910, are unable
1976 		 * to recover from a USB autosuspend, unless the alternate
1977 		 * setting of the streaming interface is toggled.
1978 		 */
1979 		if (stream->dev->quirks & UVC_QUIRK_WAKE_AUTOSUSPEND) {
1980 			usb_set_interface(stream->dev->udev, intfnum,
1981 					  altsetting);
1982 			usb_set_interface(stream->dev->udev, intfnum, 0);
1983 		}
1984 
1985 		ret = usb_set_interface(stream->dev->udev, intfnum, altsetting);
1986 		if (ret < 0)
1987 			return ret;
1988 
1989 		ret = uvc_init_video_isoc(stream, best_ep, gfp_flags);
1990 	} else {
1991 		/* Bulk endpoint, proceed to URB initialization. */
1992 		ep = uvc_find_endpoint(&intf->altsetting[0],
1993 				stream->header.bEndpointAddress);
1994 		if (ep == NULL)
1995 			return -EIO;
1996 
1997 		/* Reject broken descriptors. */
1998 		if (usb_endpoint_maxp(&ep->desc) == 0)
1999 			return -EIO;
2000 
2001 		ret = uvc_init_video_bulk(stream, ep, gfp_flags);
2002 	}
2003 
2004 	if (ret < 0)
2005 		return ret;
2006 
2007 	/* Submit the URBs. */
2008 	for_each_uvc_urb(uvc_urb, stream) {
2009 		ret = uvc_submit_urb(uvc_urb, gfp_flags);
2010 		if (ret < 0) {
2011 			dev_err(&stream->intf->dev,
2012 				"Failed to submit URB %u (%d).\n",
2013 				uvc_urb_index(uvc_urb), ret);
2014 			uvc_video_stop_transfer(stream, 1);
2015 			return ret;
2016 		}
2017 	}
2018 
2019 	/*
2020 	 * The Logitech C920 temporarily forgets that it should not be adjusting
2021 	 * Exposure Absolute during init so restore controls to stored values.
2022 	 */
2023 	if (stream->dev->quirks & UVC_QUIRK_RESTORE_CTRLS_ON_INIT)
2024 		uvc_ctrl_restore_values(stream->dev);
2025 
2026 	return 0;
2027 }
2028 
2029 /* --------------------------------------------------------------------------
2030  * Suspend/resume
2031  */
2032 
2033 /*
2034  * Stop streaming without disabling the video queue.
2035  *
2036  * To let userspace applications resume without trouble, we must not touch the
2037  * video buffers in any way. We mark the device as frozen to make sure the URB
2038  * completion handler won't try to cancel the queue when we kill the URBs.
2039  */
2040 int uvc_video_suspend(struct uvc_streaming *stream)
2041 {
2042 	if (!uvc_queue_streaming(&stream->queue))
2043 		return 0;
2044 
2045 	stream->frozen = 1;
2046 	uvc_video_stop_transfer(stream, 0);
2047 	usb_set_interface(stream->dev->udev, stream->intfnum, 0);
2048 	return 0;
2049 }
2050 
2051 /*
2052  * Reconfigure the video interface and restart streaming if it was enabled
2053  * before suspend.
2054  *
2055  * If an error occurs, disable the video queue. This will wake all pending
2056  * buffers, making sure userspace applications are notified of the problem
2057  * instead of waiting forever.
2058  */
2059 int uvc_video_resume(struct uvc_streaming *stream, int reset)
2060 {
2061 	int ret;
2062 
2063 	/*
2064 	 * If the bus has been reset on resume, set the alternate setting to 0.
2065 	 * This should be the default value, but some devices crash or otherwise
2066 	 * misbehave if they don't receive a SET_INTERFACE request before any
2067 	 * other video control request.
2068 	 */
2069 	if (reset)
2070 		usb_set_interface(stream->dev->udev, stream->intfnum, 0);
2071 
2072 	stream->frozen = 0;
2073 
2074 	uvc_video_clock_reset(stream);
2075 
2076 	if (!uvc_queue_streaming(&stream->queue))
2077 		return 0;
2078 
2079 	ret = uvc_commit_video(stream, &stream->ctrl);
2080 	if (ret < 0)
2081 		return ret;
2082 
2083 	return uvc_video_start_transfer(stream, GFP_NOIO);
2084 }
2085 
2086 /* ------------------------------------------------------------------------
2087  * Video device
2088  */
2089 
2090 /*
2091  * Initialize the UVC video device by switching to alternate setting 0 and
2092  * retrieve the default format.
2093  *
2094  * Some cameras (namely the Fuji Finepix) set the format and frame
2095  * indexes to zero. The UVC standard doesn't clearly make this a spec
2096  * violation, so try to silently fix the values if possible.
2097  *
2098  * This function is called before registering the device with V4L.
2099  */
2100 int uvc_video_init(struct uvc_streaming *stream)
2101 {
2102 	struct uvc_streaming_control *probe = &stream->ctrl;
2103 	const struct uvc_format *format = NULL;
2104 	const struct uvc_frame *frame = NULL;
2105 	struct uvc_urb *uvc_urb;
2106 	unsigned int i;
2107 	int ret;
2108 
2109 	if (stream->nformats == 0) {
2110 		dev_info(&stream->intf->dev,
2111 			 "No supported video formats found.\n");
2112 		return -EINVAL;
2113 	}
2114 
2115 	atomic_set(&stream->active, 0);
2116 
2117 	/*
2118 	 * Alternate setting 0 should be the default, yet the XBox Live Vision
2119 	 * Cam (and possibly other devices) crash or otherwise misbehave if
2120 	 * they don't receive a SET_INTERFACE request before any other video
2121 	 * control request.
2122 	 */
2123 	usb_set_interface(stream->dev->udev, stream->intfnum, 0);
2124 
2125 	/*
2126 	 * Set the streaming probe control with default streaming parameters
2127 	 * retrieved from the device. Webcams that don't support GET_DEF
2128 	 * requests on the probe control will just keep their current streaming
2129 	 * parameters.
2130 	 */
2131 	if (uvc_get_video_ctrl(stream, probe, 1, UVC_GET_DEF) == 0)
2132 		uvc_set_video_ctrl(stream, probe, 1);
2133 
2134 	/*
2135 	 * Initialize the streaming parameters with the probe control current
2136 	 * value. This makes sure SET_CUR requests on the streaming commit
2137 	 * control will always use values retrieved from a successful GET_CUR
2138 	 * request on the probe control, as required by the UVC specification.
2139 	 */
2140 	ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
2141 
2142 	/*
2143 	 * Elgato Cam Link 4k can be in a stalled state if the resolution of
2144 	 * the external source has changed while the firmware initializes.
2145 	 * Once in this state, the device is useless until it receives a
2146 	 * USB reset. It has even been observed that the stalled state will
2147 	 * continue even after unplugging the device.
2148 	 */
2149 	if (ret == -EPROTO &&
2150 	    usb_match_one_id(stream->dev->intf, &elgato_cam_link_4k)) {
2151 		dev_err(&stream->intf->dev, "Elgato Cam Link 4K firmware crash detected\n");
2152 		dev_err(&stream->intf->dev, "Resetting the device, unplug and replug to recover\n");
2153 		usb_reset_device(stream->dev->udev);
2154 	}
2155 
2156 	if (ret < 0)
2157 		return ret;
2158 
2159 	/*
2160 	 * Check if the default format descriptor exists. Use the first
2161 	 * available format otherwise.
2162 	 */
2163 	for (i = stream->nformats; i > 0; --i) {
2164 		format = &stream->formats[i-1];
2165 		if (format->index == probe->bFormatIndex)
2166 			break;
2167 	}
2168 
2169 	if (format->nframes == 0) {
2170 		dev_info(&stream->intf->dev,
2171 			 "No frame descriptor found for the default format.\n");
2172 		return -EINVAL;
2173 	}
2174 
2175 	/*
2176 	 * Zero bFrameIndex might be correct. Stream-based formats (including
2177 	 * MPEG-2 TS and DV) do not support frames but have a dummy frame
2178 	 * descriptor with bFrameIndex set to zero. If the default frame
2179 	 * descriptor is not found, use the first available frame.
2180 	 */
2181 	for (i = format->nframes; i > 0; --i) {
2182 		frame = &format->frames[i-1];
2183 		if (frame->bFrameIndex == probe->bFrameIndex)
2184 			break;
2185 	}
2186 
2187 	probe->bFormatIndex = format->index;
2188 	probe->bFrameIndex = frame->bFrameIndex;
2189 
2190 	stream->def_format = format;
2191 	stream->cur_format = format;
2192 	stream->cur_frame = frame;
2193 
2194 	/* Select the video decoding function */
2195 	if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2196 		if (stream->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
2197 			stream->decode = uvc_video_decode_isight;
2198 		else if (stream->intf->num_altsetting > 1)
2199 			stream->decode = uvc_video_decode_isoc;
2200 		else
2201 			stream->decode = uvc_video_decode_bulk;
2202 	} else {
2203 		if (stream->intf->num_altsetting == 1)
2204 			stream->decode = uvc_video_encode_bulk;
2205 		else {
2206 			dev_info(&stream->intf->dev,
2207 				 "Isochronous endpoints are not supported for video output devices.\n");
2208 			return -EINVAL;
2209 		}
2210 	}
2211 
2212 	/* Prepare asynchronous work items. */
2213 	for_each_uvc_urb(uvc_urb, stream)
2214 		INIT_WORK(&uvc_urb->work, uvc_video_copy_data_work);
2215 
2216 	return 0;
2217 }
2218 
2219 int uvc_video_start_streaming(struct uvc_streaming *stream)
2220 {
2221 	int ret;
2222 
2223 	ret = uvc_video_clock_init(stream);
2224 	if (ret < 0)
2225 		return ret;
2226 
2227 	/* Commit the streaming parameters. */
2228 	ret = uvc_commit_video(stream, &stream->ctrl);
2229 	if (ret < 0)
2230 		goto error_commit;
2231 
2232 	ret = uvc_video_start_transfer(stream, GFP_KERNEL);
2233 	if (ret < 0)
2234 		goto error_video;
2235 
2236 	return 0;
2237 
2238 error_video:
2239 	usb_set_interface(stream->dev->udev, stream->intfnum, 0);
2240 error_commit:
2241 	uvc_video_clock_cleanup(stream);
2242 
2243 	return ret;
2244 }
2245 
2246 void uvc_video_stop_streaming(struct uvc_streaming *stream)
2247 {
2248 	uvc_video_stop_transfer(stream, 1);
2249 
2250 	if (stream->intf->num_altsetting > 1) {
2251 		usb_set_interface(stream->dev->udev, stream->intfnum, 0);
2252 	} else {
2253 		/*
2254 		 * UVC doesn't specify how to inform a bulk-based device
2255 		 * when the video stream is stopped. Windows sends a
2256 		 * CLEAR_FEATURE(HALT) request to the video streaming
2257 		 * bulk endpoint, mimic the same behaviour.
2258 		 */
2259 		unsigned int epnum = stream->header.bEndpointAddress
2260 				   & USB_ENDPOINT_NUMBER_MASK;
2261 		unsigned int dir = stream->header.bEndpointAddress
2262 				 & USB_ENDPOINT_DIR_MASK;
2263 		unsigned int pipe;
2264 
2265 		pipe = usb_sndbulkpipe(stream->dev->udev, epnum) | dir;
2266 		usb_clear_halt(stream->dev->udev, pipe);
2267 	}
2268 
2269 	uvc_video_clock_cleanup(stream);
2270 }
2271