Lines Matching +full:clock +full:- +full:duration +full:- +full:ns
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * uvc_video.c -- USB Video Class driver - Video handling
5 * Copyright (C) 2005-2010
9 #include <linux/dma-mapping.h>
23 #include <media/v4l2-common.h>
27 /* ------------------------------------------------------------------------
38 pipe = (query & 0x80) ? usb_rcvctrlpipe(dev->udev, 0) in __uvc_query_ctrl()
39 : usb_sndctrlpipe(dev->udev, 0); in __uvc_query_ctrl()
42 return usb_control_msg(dev->udev, pipe, query, type, cs << 8, in __uvc_query_ctrl()
82 if (ret != -EPIPE) { in uvc_query_ctrl()
83 dev_err(&dev->udev->dev, in uvc_query_ctrl()
86 return ret < 0 ? ret : -EPIPE; in uvc_query_ctrl()
100 return ret < 0 ? ret : -EPIPE; in uvc_query_ctrl()
106 /* Cannot happen - we received a STALL */ in uvc_query_ctrl()
107 return -EPIPE; in uvc_query_ctrl()
109 return -EBUSY; in uvc_query_ctrl()
111 return -EACCES; in uvc_query_ctrl()
113 return -EREMOTE; in uvc_query_ctrl()
115 return -ERANGE; in uvc_query_ctrl()
123 return -EIO; in uvc_query_ctrl()
125 return -EINVAL; in uvc_query_ctrl()
130 return -EPIPE; in uvc_query_ctrl()
153 * Latest Elgato Cam Link 4K firmware as of 2021-03-23 needs this fix. in uvc_fixup_video_ctrl()
156 if (usb_match_one_id(stream->dev->intf, &elgato_cam_link_4k) && in uvc_fixup_video_ctrl()
157 ctrl->bmHint > 255) { in uvc_fixup_video_ctrl()
158 u8 corrected_format_index = ctrl->bmHint >> 8; in uvc_fixup_video_ctrl()
160 uvc_dbg(stream->dev, VIDEO, in uvc_fixup_video_ctrl()
162 ctrl->bmHint, ctrl->bFormatIndex, in uvc_fixup_video_ctrl()
164 ctrl->bmHint = 1; in uvc_fixup_video_ctrl()
165 ctrl->bFormatIndex = corrected_format_index; in uvc_fixup_video_ctrl()
168 for (i = 0; i < stream->nformats; ++i) { in uvc_fixup_video_ctrl()
169 if (stream->formats[i].index == ctrl->bFormatIndex) { in uvc_fixup_video_ctrl()
170 format = &stream->formats[i]; in uvc_fixup_video_ctrl()
178 for (i = 0; i < format->nframes; ++i) { in uvc_fixup_video_ctrl()
179 if (format->frames[i].bFrameIndex == ctrl->bFrameIndex) { in uvc_fixup_video_ctrl()
180 frame = &format->frames[i]; in uvc_fixup_video_ctrl()
188 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) || in uvc_fixup_video_ctrl()
189 (ctrl->dwMaxVideoFrameSize == 0 && in uvc_fixup_video_ctrl()
190 stream->dev->uvc_version < 0x0110)) in uvc_fixup_video_ctrl()
191 ctrl->dwMaxVideoFrameSize = in uvc_fixup_video_ctrl()
192 frame->dwMaxVideoFrameBufferSize; in uvc_fixup_video_ctrl()
195 * The "TOSHIBA Web Camera - 5M" Chicony device (04f2:b50b) seems to in uvc_fixup_video_ctrl()
196 * compute the bandwidth on 16 bits and erroneously sign-extend it to in uvc_fixup_video_ctrl()
200 if ((ctrl->dwMaxPayloadTransferSize & 0xffff0000) == 0xffff0000) in uvc_fixup_video_ctrl()
201 ctrl->dwMaxPayloadTransferSize &= ~0xffff0000; in uvc_fixup_video_ctrl()
203 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) && in uvc_fixup_video_ctrl()
204 stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH && in uvc_fixup_video_ctrl()
205 stream->intf->num_altsetting > 1) { in uvc_fixup_video_ctrl()
209 interval = (ctrl->dwFrameInterval > 100000) in uvc_fixup_video_ctrl()
210 ? ctrl->dwFrameInterval in uvc_fixup_video_ctrl()
211 : frame->dwFrameInterval[0]; in uvc_fixup_video_ctrl()
216 * result by the number of USB frames (or micro-frames for in uvc_fixup_video_ctrl()
217 * high-speed devices) per second and add the UVC header size in uvc_fixup_video_ctrl()
220 bandwidth = frame->wWidth * frame->wHeight / 8 * format->bpp; in uvc_fixup_video_ctrl()
223 if (stream->dev->udev->speed == USB_SPEED_HIGH) in uvc_fixup_video_ctrl()
237 ctrl->dwMaxPayloadTransferSize = bandwidth; in uvc_fixup_video_ctrl()
247 if (stream->dev->uvc_version < 0x0110) in uvc_video_ctrl_size()
249 else if (stream->dev->uvc_version < 0x0150) in uvc_video_ctrl_size()
262 if ((stream->dev->quirks & UVC_QUIRK_PROBE_DEF) && in uvc_get_video_ctrl()
264 return -EIO; in uvc_get_video_ctrl()
268 return -ENOMEM; in uvc_get_video_ctrl()
270 ret = __uvc_query_ctrl(stream->dev, query, 0, stream->intfnum, in uvc_get_video_ctrl()
280 uvc_warn_once(stream->dev, UVC_WARN_MINMAX, "UVC non " in uvc_get_video_ctrl()
281 "compliance - GET_MIN/MAX(PROBE) incorrectly " in uvc_get_video_ctrl()
284 ctrl->wCompQuality = le16_to_cpup((__le16 *)data); in uvc_get_video_ctrl()
293 uvc_warn_once(stream->dev, UVC_WARN_PROBE_DEF, "UVC non " in uvc_get_video_ctrl()
294 "compliance - GET_DEF(PROBE) not supported. " in uvc_get_video_ctrl()
296 ret = -EIO; in uvc_get_video_ctrl()
299 dev_err(&stream->intf->dev, in uvc_get_video_ctrl()
302 ret = (ret == -EPROTO) ? -EPROTO : -EIO; in uvc_get_video_ctrl()
306 ctrl->bmHint = le16_to_cpup((__le16 *)&data[0]); in uvc_get_video_ctrl()
307 ctrl->bFormatIndex = data[2]; in uvc_get_video_ctrl()
308 ctrl->bFrameIndex = data[3]; in uvc_get_video_ctrl()
309 ctrl->dwFrameInterval = le32_to_cpup((__le32 *)&data[4]); in uvc_get_video_ctrl()
310 ctrl->wKeyFrameRate = le16_to_cpup((__le16 *)&data[8]); in uvc_get_video_ctrl()
311 ctrl->wPFrameRate = le16_to_cpup((__le16 *)&data[10]); in uvc_get_video_ctrl()
312 ctrl->wCompQuality = le16_to_cpup((__le16 *)&data[12]); in uvc_get_video_ctrl()
313 ctrl->wCompWindowSize = le16_to_cpup((__le16 *)&data[14]); in uvc_get_video_ctrl()
314 ctrl->wDelay = le16_to_cpup((__le16 *)&data[16]); in uvc_get_video_ctrl()
315 ctrl->dwMaxVideoFrameSize = get_unaligned_le32(&data[18]); in uvc_get_video_ctrl()
316 ctrl->dwMaxPayloadTransferSize = get_unaligned_le32(&data[22]); in uvc_get_video_ctrl()
319 ctrl->dwClockFrequency = get_unaligned_le32(&data[26]); in uvc_get_video_ctrl()
320 ctrl->bmFramingInfo = data[30]; in uvc_get_video_ctrl()
321 ctrl->bPreferedVersion = data[31]; in uvc_get_video_ctrl()
322 ctrl->bMinVersion = data[32]; in uvc_get_video_ctrl()
323 ctrl->bMaxVersion = data[33]; in uvc_get_video_ctrl()
325 ctrl->dwClockFrequency = stream->dev->clock_frequency; in uvc_get_video_ctrl()
326 ctrl->bmFramingInfo = 0; in uvc_get_video_ctrl()
327 ctrl->bPreferedVersion = 0; in uvc_get_video_ctrl()
328 ctrl->bMinVersion = 0; in uvc_get_video_ctrl()
329 ctrl->bMaxVersion = 0; in uvc_get_video_ctrl()
354 return -ENOMEM; in uvc_set_video_ctrl()
356 *(__le16 *)&data[0] = cpu_to_le16(ctrl->bmHint); in uvc_set_video_ctrl()
357 data[2] = ctrl->bFormatIndex; in uvc_set_video_ctrl()
358 data[3] = ctrl->bFrameIndex; in uvc_set_video_ctrl()
359 *(__le32 *)&data[4] = cpu_to_le32(ctrl->dwFrameInterval); in uvc_set_video_ctrl()
360 *(__le16 *)&data[8] = cpu_to_le16(ctrl->wKeyFrameRate); in uvc_set_video_ctrl()
361 *(__le16 *)&data[10] = cpu_to_le16(ctrl->wPFrameRate); in uvc_set_video_ctrl()
362 *(__le16 *)&data[12] = cpu_to_le16(ctrl->wCompQuality); in uvc_set_video_ctrl()
363 *(__le16 *)&data[14] = cpu_to_le16(ctrl->wCompWindowSize); in uvc_set_video_ctrl()
364 *(__le16 *)&data[16] = cpu_to_le16(ctrl->wDelay); in uvc_set_video_ctrl()
365 put_unaligned_le32(ctrl->dwMaxVideoFrameSize, &data[18]); in uvc_set_video_ctrl()
366 put_unaligned_le32(ctrl->dwMaxPayloadTransferSize, &data[22]); in uvc_set_video_ctrl()
369 put_unaligned_le32(ctrl->dwClockFrequency, &data[26]); in uvc_set_video_ctrl()
370 data[30] = ctrl->bmFramingInfo; in uvc_set_video_ctrl()
371 data[31] = ctrl->bPreferedVersion; in uvc_set_video_ctrl()
372 data[32] = ctrl->bMinVersion; in uvc_set_video_ctrl()
373 data[33] = ctrl->bMaxVersion; in uvc_set_video_ctrl()
376 ret = __uvc_query_ctrl(stream->dev, UVC_SET_CUR, 0, stream->intfnum, in uvc_set_video_ctrl()
380 dev_err(&stream->intf->dev, in uvc_set_video_ctrl()
383 ret = -EIO; in uvc_set_video_ctrl()
410 if (!(stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX)) { in uvc_probe_video()
418 probe->wCompQuality = probe_max.wCompQuality; in uvc_probe_video()
429 if (stream->intf->num_altsetting == 1) in uvc_probe_video()
432 if (probe->dwMaxPayloadTransferSize <= stream->maxpsize) in uvc_probe_video()
435 if (stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX) { in uvc_probe_video()
436 ret = -ENOSPC; in uvc_probe_video()
441 probe->wKeyFrameRate = probe_min.wKeyFrameRate; in uvc_probe_video()
442 probe->wPFrameRate = probe_min.wPFrameRate; in uvc_probe_video()
443 probe->wCompQuality = probe_max.wCompQuality; in uvc_probe_video()
444 probe->wCompWindowSize = probe_min.wCompWindowSize; in uvc_probe_video()
457 /* -----------------------------------------------------------------------------
508 * - store the frame PTS in the buffer structure in uvc_video_clock_decode()
509 * - if the SCR field is present, retrieve the host SOF counter and in uvc_video_clock_decode()
514 buf->pts = get_unaligned_le32(&data[2]); in uvc_video_clock_decode()
525 dev_sof = get_unaligned_le16(&data[header_size - 2]); in uvc_video_clock_decode()
526 if (dev_sof == stream->clock.last_sof) in uvc_video_clock_decode()
529 stream->clock.last_sof = dev_sof; in uvc_video_clock_decode()
531 host_sof = usb_get_current_frame_number(stream->dev->udev); in uvc_video_clock_decode()
554 if (stream->clock.sof_offset == (u16)-1) { in uvc_video_clock_decode()
555 u16 delta_sof = (host_sof - dev_sof) & 255; in uvc_video_clock_decode()
557 stream->clock.sof_offset = delta_sof; in uvc_video_clock_decode()
559 stream->clock.sof_offset = 0; in uvc_video_clock_decode()
562 dev_sof = (dev_sof + stream->clock.sof_offset) & 2047; in uvc_video_clock_decode()
564 spin_lock_irqsave(&stream->clock.lock, flags); in uvc_video_clock_decode()
566 sample = &stream->clock.samples[stream->clock.head]; in uvc_video_clock_decode()
567 sample->dev_stc = get_unaligned_le32(&data[header_size - 6]); in uvc_video_clock_decode()
568 sample->dev_sof = dev_sof; in uvc_video_clock_decode()
569 sample->host_sof = host_sof; in uvc_video_clock_decode()
570 sample->host_time = time; in uvc_video_clock_decode()
573 stream->clock.head = (stream->clock.head + 1) % stream->clock.size; in uvc_video_clock_decode()
575 if (stream->clock.count < stream->clock.size) in uvc_video_clock_decode()
576 stream->clock.count++; in uvc_video_clock_decode()
578 spin_unlock_irqrestore(&stream->clock.lock, flags); in uvc_video_clock_decode()
583 struct uvc_clock *clock = &stream->clock; in uvc_video_clock_reset() local
585 clock->head = 0; in uvc_video_clock_reset()
586 clock->count = 0; in uvc_video_clock_reset()
587 clock->last_sof = -1; in uvc_video_clock_reset()
588 clock->sof_offset = -1; in uvc_video_clock_reset()
593 struct uvc_clock *clock = &stream->clock; in uvc_video_clock_init() local
595 spin_lock_init(&clock->lock); in uvc_video_clock_init()
596 clock->size = 32; in uvc_video_clock_init()
598 clock->samples = kmalloc_array(clock->size, sizeof(*clock->samples), in uvc_video_clock_init()
600 if (clock->samples == NULL) in uvc_video_clock_init()
601 return -ENOMEM; in uvc_video_clock_init()
610 kfree(stream->clock.samples); in uvc_video_clock_cleanup()
611 stream->clock.samples = NULL; in uvc_video_clock_cleanup()
615 * uvc_video_clock_host_sof - Return the host SOF value for a clock sample
618 * cover the whole 11-bits SOF range (0-2047) but are limited to the HCI frame
636 delta_sof = (sample->host_sof - sample->dev_sof) & 255; in uvc_video_clock_host_sof()
638 return (sample->dev_sof + delta_sof) & 2047; in uvc_video_clock_host_sof()
642 * uvc_video_clock_update - Update the buffer timestamp
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
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
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
658 * the clock drift should be small compared to the sliding window size, we
662 * SOF = (SOF2 - SOF1) / (STC2 - STC1) * PTS
663 * + (SOF1 * STC2 - SOF2 * STC1) / (STC2 - STC1)
667 * SOF = ((SOF2 - SOF1) * PTS + SOF1 * STC2 - SOF2 * STC1) / (STC2 - STC1) (1)
672 * TS = ((TS2 - TS1) * SOF + TS1 * SOF2 - TS2 * SOF1) / (SOF2 - SOF1) (2)
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
706 struct uvc_clock *clock = &stream->clock; in uvc_video_clock_update() local
726 if (!clock->samples) in uvc_video_clock_update()
729 spin_lock_irqsave(&clock->lock, flags); in uvc_video_clock_update()
731 if (clock->count < clock->size) in uvc_video_clock_update()
734 first = &clock->samples[clock->head]; in uvc_video_clock_update()
735 last = &clock->samples[(clock->head - 1) % clock->size]; in uvc_video_clock_update()
738 delta_stc = buf->pts - (1UL << 31); in uvc_video_clock_update()
739 x1 = first->dev_stc - delta_stc; in uvc_video_clock_update()
740 x2 = last->dev_stc - delta_stc; in uvc_video_clock_update()
744 y1 = (first->dev_sof + 2048) << 16; in uvc_video_clock_update()
745 y2 = (last->dev_sof + 2048) << 16; in uvc_video_clock_update()
749 y = (u64)(y2 - y1) * (1ULL << 31) + (u64)y1 * (u64)x2 in uvc_video_clock_update()
750 - (u64)y2 * (u64)x1; in uvc_video_clock_update()
751 y = div_u64(y, x2 - x1); in uvc_video_clock_update()
755 uvc_dbg(stream->dev, CLOCK, in uvc_video_clock_update()
757 stream->dev->name, buf->pts, in uvc_video_clock_update()
760 x1, x2, y1, y2, clock->sof_offset); in uvc_video_clock_update()
762 /* Second step, SOF to host clock conversion. */ in uvc_video_clock_update()
771 y2 = (u32)ktime_to_ns(ktime_sub(last->host_time, first->host_time)) + y1; in uvc_video_clock_update()
780 if (mean - (1024 << 16) > sof) in uvc_video_clock_update()
783 sof -= 2048 << 16; in uvc_video_clock_update()
785 y = (u64)(y2 - y1) * (u64)sof + (u64)y1 * (u64)x2 in uvc_video_clock_update()
786 - (u64)y2 * (u64)x1; in uvc_video_clock_update()
787 y = div_u64(y, x2 - x1); in uvc_video_clock_update()
789 timestamp = ktime_to_ns(first->host_time) + y - y1; in uvc_video_clock_update()
791 uvc_dbg(stream->dev, CLOCK, in uvc_video_clock_update()
793 stream->dev->name, in uvc_video_clock_update()
795 y, timestamp, vbuf->vb2_buf.timestamp, in uvc_video_clock_update()
796 x1, first->host_sof, first->dev_sof, in uvc_video_clock_update()
797 x2, last->host_sof, last->dev_sof, y1, y2); in uvc_video_clock_update()
800 vbuf->vb2_buf.timestamp = timestamp; in uvc_video_clock_update()
803 spin_unlock_irqrestore(&clock->lock, flags); in uvc_video_clock_update()
806 /* ------------------------------------------------------------------------
820 if (stream->stats.stream.nb_frames == 0 && in uvc_video_stats_decode()
821 stream->stats.frame.nb_packets == 0) in uvc_video_stats_decode()
822 stream->stats.stream.start_ts = ktime_get(); in uvc_video_stats_decode()
845 stream->stats.frame.nb_invalid++; in uvc_video_stats_decode()
854 scr_stc = get_unaligned_le32(&data[header_size - 6]); in uvc_video_stats_decode()
855 scr_sof = get_unaligned_le16(&data[header_size - 2]); in uvc_video_stats_decode()
859 if (has_pts && stream->stats.frame.nb_pts) { in uvc_video_stats_decode()
860 if (stream->stats.frame.pts != pts) { in uvc_video_stats_decode()
861 stream->stats.frame.nb_pts_diffs++; in uvc_video_stats_decode()
862 stream->stats.frame.last_pts_diff = in uvc_video_stats_decode()
863 stream->stats.frame.nb_packets; in uvc_video_stats_decode()
868 stream->stats.frame.nb_pts++; in uvc_video_stats_decode()
869 stream->stats.frame.pts = pts; in uvc_video_stats_decode()
873 * Do all frames have a PTS in their first non-empty packet, or before in uvc_video_stats_decode()
876 if (stream->stats.frame.size == 0) { in uvc_video_stats_decode()
878 stream->stats.frame.has_initial_pts = has_pts; in uvc_video_stats_decode()
880 stream->stats.frame.has_early_pts = true; in uvc_video_stats_decode()
884 if (has_scr && stream->stats.frame.nb_scr) { in uvc_video_stats_decode()
885 if (stream->stats.frame.scr_stc != scr_stc) in uvc_video_stats_decode()
886 stream->stats.frame.nb_scr_diffs++; in uvc_video_stats_decode()
891 if (stream->stats.stream.nb_frames > 0 || in uvc_video_stats_decode()
892 stream->stats.frame.nb_scr > 0) in uvc_video_stats_decode()
893 stream->stats.stream.scr_sof_count += in uvc_video_stats_decode()
894 (scr_sof - stream->stats.stream.scr_sof) % 2048; in uvc_video_stats_decode()
895 stream->stats.stream.scr_sof = scr_sof; in uvc_video_stats_decode()
897 stream->stats.frame.nb_scr++; in uvc_video_stats_decode()
898 stream->stats.frame.scr_stc = scr_stc; in uvc_video_stats_decode()
899 stream->stats.frame.scr_sof = scr_sof; in uvc_video_stats_decode()
901 if (scr_sof < stream->stats.stream.min_sof) in uvc_video_stats_decode()
902 stream->stats.stream.min_sof = scr_sof; in uvc_video_stats_decode()
903 if (scr_sof > stream->stats.stream.max_sof) in uvc_video_stats_decode()
904 stream->stats.stream.max_sof = scr_sof; in uvc_video_stats_decode()
907 /* Record the first non-empty packet number. */ in uvc_video_stats_decode()
908 if (stream->stats.frame.size == 0 && len > header_size) in uvc_video_stats_decode()
909 stream->stats.frame.first_data = stream->stats.frame.nb_packets; in uvc_video_stats_decode()
912 stream->stats.frame.size += len - header_size; in uvc_video_stats_decode()
915 stream->stats.frame.nb_packets++; in uvc_video_stats_decode()
917 stream->stats.frame.nb_empty++; in uvc_video_stats_decode()
920 stream->stats.frame.nb_errors++; in uvc_video_stats_decode()
925 struct uvc_stats_frame *frame = &stream->stats.frame; in uvc_video_stats_update()
927 uvc_dbg(stream->dev, STATS, in uvc_video_stats_update()
929 stream->sequence, frame->first_data, in uvc_video_stats_update()
930 frame->nb_packets - frame->nb_empty, frame->nb_packets, in uvc_video_stats_update()
931 frame->nb_pts_diffs, frame->last_pts_diff, frame->nb_pts, in uvc_video_stats_update()
932 frame->has_early_pts ? "" : "!", in uvc_video_stats_update()
933 frame->has_initial_pts ? "" : "!", in uvc_video_stats_update()
934 frame->nb_scr_diffs, frame->nb_scr, in uvc_video_stats_update()
935 frame->pts, frame->scr_stc, frame->scr_sof); in uvc_video_stats_update()
937 stream->stats.stream.nb_frames++; in uvc_video_stats_update()
938 stream->stats.stream.nb_packets += stream->stats.frame.nb_packets; in uvc_video_stats_update()
939 stream->stats.stream.nb_empty += stream->stats.frame.nb_empty; in uvc_video_stats_update()
940 stream->stats.stream.nb_errors += stream->stats.frame.nb_errors; in uvc_video_stats_update()
941 stream->stats.stream.nb_invalid += stream->stats.frame.nb_invalid; in uvc_video_stats_update()
943 if (frame->has_early_pts) in uvc_video_stats_update()
944 stream->stats.stream.nb_pts_early++; in uvc_video_stats_update()
945 if (frame->has_initial_pts) in uvc_video_stats_update()
946 stream->stats.stream.nb_pts_initial++; in uvc_video_stats_update()
947 if (frame->last_pts_diff <= frame->first_data) in uvc_video_stats_update()
948 stream->stats.stream.nb_pts_constant++; in uvc_video_stats_update()
949 if (frame->nb_scr >= frame->nb_packets - frame->nb_empty) in uvc_video_stats_update()
950 stream->stats.stream.nb_scr_count_ok++; in uvc_video_stats_update()
951 if (frame->nb_scr_diffs + 1 == frame->nb_scr) in uvc_video_stats_update()
952 stream->stats.stream.nb_scr_diffs_ok++; in uvc_video_stats_update()
954 memset(&stream->stats.frame, 0, sizeof(stream->stats.frame)); in uvc_video_stats_update()
961 unsigned int duration; in uvc_video_stats_dump() local
968 duration = ktime_ms_delta(stream->stats.stream.stop_ts, in uvc_video_stats_dump()
969 stream->stats.stream.start_ts); in uvc_video_stats_dump()
970 if (duration != 0) in uvc_video_stats_dump()
971 scr_sof_freq = stream->stats.stream.scr_sof_count * 1000 in uvc_video_stats_dump()
972 / duration; in uvc_video_stats_dump()
976 count += scnprintf(buf + count, size - count, in uvc_video_stats_dump()
979 stream->stats.stream.nb_frames, in uvc_video_stats_dump()
980 stream->stats.stream.nb_packets, in uvc_video_stats_dump()
981 stream->stats.stream.nb_empty, in uvc_video_stats_dump()
982 stream->stats.stream.nb_errors, in uvc_video_stats_dump()
983 stream->stats.stream.nb_invalid); in uvc_video_stats_dump()
984 count += scnprintf(buf + count, size - count, in uvc_video_stats_dump()
986 stream->stats.stream.nb_pts_early, in uvc_video_stats_dump()
987 stream->stats.stream.nb_pts_initial, in uvc_video_stats_dump()
988 stream->stats.stream.nb_pts_constant); in uvc_video_stats_dump()
989 count += scnprintf(buf + count, size - count, in uvc_video_stats_dump()
991 stream->stats.stream.nb_scr_count_ok, in uvc_video_stats_dump()
992 stream->stats.stream.nb_scr_diffs_ok); in uvc_video_stats_dump()
993 count += scnprintf(buf + count, size - count, in uvc_video_stats_dump()
995 stream->stats.stream.min_sof, in uvc_video_stats_dump()
996 stream->stats.stream.max_sof, in uvc_video_stats_dump()
1004 memset(&stream->stats, 0, sizeof(stream->stats)); in uvc_video_stats_start()
1005 stream->stats.stream.min_sof = 2048; in uvc_video_stats_start()
1010 stream->stats.stream.stop_ts = ktime_get(); in uvc_video_stats_stop()
1013 /* ------------------------------------------------------------------------
1026 * - EAGAIN informs the caller that the current video buffer should be marked
1031 * If an error other than -EAGAIN is returned, the caller will drop the current
1033 * made until the next payload. -ENODATA can be used to drop the current
1060 * - packet must be at least 2 bytes long in uvc_video_decode_start()
1061 * - bHeaderLength value must be at least 2 bytes (see above) in uvc_video_decode_start()
1062 * - bHeaderLength value can't be larger than the packet size. in uvc_video_decode_start()
1065 stream->stats.frame.nb_invalid++; in uvc_video_decode_start()
1066 return -EINVAL; in uvc_video_decode_start()
1075 if (stream->last_fid != fid) { in uvc_video_decode_start()
1076 stream->sequence++; in uvc_video_decode_start()
1077 if (stream->sequence) in uvc_video_decode_start()
1089 stream->last_fid = fid; in uvc_video_decode_start()
1090 return -ENODATA; in uvc_video_decode_start()
1095 uvc_dbg(stream->dev, FRAME, in uvc_video_decode_start()
1097 buf->error = 1; in uvc_video_decode_start()
1103 * stream->last_fid is initialized to -1, so the first isochronous in uvc_video_decode_start()
1106 * If the device doesn't toggle the FID bit, invert stream->last_fid in uvc_video_decode_start()
1109 if (buf->state != UVC_BUF_STATE_ACTIVE) { in uvc_video_decode_start()
1110 if (fid == stream->last_fid) { in uvc_video_decode_start()
1111 uvc_dbg(stream->dev, FRAME, in uvc_video_decode_start()
1113 if ((stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) && in uvc_video_decode_start()
1115 stream->last_fid ^= UVC_STREAM_FID; in uvc_video_decode_start()
1116 return -ENODATA; in uvc_video_decode_start()
1119 buf->buf.field = V4L2_FIELD_NONE; in uvc_video_decode_start()
1120 buf->buf.sequence = stream->sequence; in uvc_video_decode_start()
1121 buf->buf.vb2_buf.timestamp = ktime_to_ns(uvc_video_get_time()); in uvc_video_decode_start()
1124 buf->state = UVC_BUF_STATE_ACTIVE; in uvc_video_decode_start()
1135 * stream->last_fid is initialized to -1, so the first isochronous in uvc_video_decode_start()
1143 if (fid != stream->last_fid && buf->bytesused != 0) { in uvc_video_decode_start()
1144 uvc_dbg(stream->dev, FRAME, in uvc_video_decode_start()
1146 buf->state = UVC_BUF_STATE_READY; in uvc_video_decode_start()
1147 return -EAGAIN; in uvc_video_decode_start()
1150 stream->last_fid = fid; in uvc_video_decode_start()
1158 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) in uvc_stream_dir()
1166 return bus_to_hcd(stream->dev->udev->bus)->self.sysdev; in uvc_stream_to_dmadev()
1172 dma_sync_sgtable_for_device(uvc_stream_to_dmadev(uvc_urb->stream), in uvc_submit_urb()
1173 uvc_urb->sgt, in uvc_submit_urb()
1174 uvc_stream_dir(uvc_urb->stream)); in uvc_submit_urb()
1175 return usb_submit_urb(uvc_urb->urb, mem_flags); in uvc_submit_urb()
1190 for (i = 0; i < uvc_urb->async_operations; i++) { in uvc_video_copy_data_work()
1191 struct uvc_copy_op *op = &uvc_urb->copy_operations[i]; in uvc_video_copy_data_work()
1193 memcpy(op->dst, op->src, op->len); in uvc_video_copy_data_work()
1196 uvc_queue_buffer_release(op->buf); in uvc_video_copy_data_work()
1201 dev_err(&uvc_urb->stream->intf->dev, in uvc_video_copy_data_work()
1208 unsigned int active_op = uvc_urb->async_operations; in uvc_video_decode_data()
1209 struct uvc_copy_op *op = &uvc_urb->copy_operations[active_op]; in uvc_video_decode_data()
1215 maxlen = buf->length - buf->bytesused; in uvc_video_decode_data()
1218 kref_get(&buf->ref); in uvc_video_decode_data()
1220 op->buf = buf; in uvc_video_decode_data()
1221 op->src = data; in uvc_video_decode_data()
1222 op->dst = buf->mem + buf->bytesused; in uvc_video_decode_data()
1223 op->len = min_t(unsigned int, len, maxlen); in uvc_video_decode_data()
1225 buf->bytesused += op->len; in uvc_video_decode_data()
1229 uvc_dbg(uvc_urb->stream->dev, FRAME, in uvc_video_decode_data()
1231 buf->error = 1; in uvc_video_decode_data()
1232 buf->state = UVC_BUF_STATE_READY; in uvc_video_decode_data()
1235 uvc_urb->async_operations++; in uvc_video_decode_data()
1242 if (data[1] & UVC_STREAM_EOF && buf->bytesused != 0) { in uvc_video_decode_end()
1243 uvc_dbg(stream->dev, FRAME, "Frame complete (EOF found)\n"); in uvc_video_decode_end()
1245 uvc_dbg(stream->dev, FRAME, "EOF in empty payload\n"); in uvc_video_decode_end()
1246 buf->state = UVC_BUF_STATE_READY; in uvc_video_decode_end()
1247 if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) in uvc_video_decode_end()
1248 stream->last_fid ^= UVC_STREAM_FID; in uvc_video_decode_end()
1269 | (stream->last_fid & UVC_STREAM_FID); in uvc_video_encode_header()
1276 struct uvc_video_queue *queue = &stream->queue; in uvc_video_encode_data()
1281 mem = buf->mem + queue->buf_used; in uvc_video_encode_data()
1282 nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used); in uvc_video_encode_data()
1283 nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size, in uvc_video_encode_data()
1287 queue->buf_used += nbytes; in uvc_video_encode_data()
1292 /* ------------------------------------------------------------------------
1299 * of blocks, containing a 64-bit timestamp in nanoseconds, a 16-bit USB Frame
1322 if (meta_buf->length - meta_buf->bytesused < in uvc_video_decode_meta()
1323 length + sizeof(meta->ns) + sizeof(meta->sof)) { in uvc_video_decode_meta()
1324 meta_buf->error = 1; in uvc_video_decode_meta()
1341 if (stream->meta.format == V4L2_META_FMT_UVC) in uvc_video_decode_meta()
1345 !memcmp(scr, stream->clock.last_scr, 6))) in uvc_video_decode_meta()
1348 meta = (struct uvc_meta_buf *)((u8 *)meta_buf->mem + meta_buf->bytesused); in uvc_video_decode_meta()
1351 sof = usb_get_current_frame_number(stream->dev->udev); in uvc_video_decode_meta()
1353 put_unaligned(ktime_to_ns(time), &meta->ns); in uvc_video_decode_meta()
1354 put_unaligned(sof, &meta->sof); in uvc_video_decode_meta()
1357 memcpy(stream->clock.last_scr, scr, 6); in uvc_video_decode_meta()
1359 meta->length = mem[0]; in uvc_video_decode_meta()
1360 meta->flags = mem[1]; in uvc_video_decode_meta()
1361 memcpy(meta->buf, &mem[2], length - 2); in uvc_video_decode_meta()
1362 meta_buf->bytesused += length + sizeof(meta->ns) + sizeof(meta->sof); in uvc_video_decode_meta()
1364 uvc_dbg(stream->dev, FRAME, in uvc_video_decode_meta()
1365 "%s(): t-sys %lluns, SOF %u, len %u, flags 0x%x, PTS %u, STC %u frame SOF %u\n", in uvc_video_decode_meta()
1366 __func__, ktime_to_ns(time), meta->sof, meta->length, in uvc_video_decode_meta()
1367 meta->flags, in uvc_video_decode_meta()
1368 has_pts ? *(u32 *)meta->buf : 0, in uvc_video_decode_meta()
1373 /* ------------------------------------------------------------------------
1383 if (stream->ctrl.dwMaxVideoFrameSize != buf->bytesused && in uvc_video_validate_buffer()
1384 !(stream->cur_format->flags & UVC_FMT_FLAG_COMPRESSED)) in uvc_video_validate_buffer()
1385 buf->error = 1; in uvc_video_validate_buffer()
1398 struct vb2_v4l2_buffer *vb2_meta = &(*meta_buf)->buf; in uvc_video_next_buffers()
1399 const struct vb2_v4l2_buffer *vb2_video = &(*video_buf)->buf; in uvc_video_next_buffers()
1401 vb2_meta->sequence = vb2_video->sequence; in uvc_video_next_buffers()
1402 vb2_meta->field = vb2_video->field; in uvc_video_next_buffers()
1403 vb2_meta->vb2_buf.timestamp = vb2_video->vb2_buf.timestamp; in uvc_video_next_buffers()
1405 (*meta_buf)->state = UVC_BUF_STATE_READY; in uvc_video_next_buffers()
1406 if (!(*meta_buf)->error) in uvc_video_next_buffers()
1407 (*meta_buf)->error = (*video_buf)->error; in uvc_video_next_buffers()
1408 *meta_buf = uvc_queue_next_buffer(&stream->meta.queue, in uvc_video_next_buffers()
1411 *video_buf = uvc_queue_next_buffer(&stream->queue, *video_buf); in uvc_video_next_buffers()
1417 struct urb *urb = uvc_urb->urb; in uvc_video_decode_isoc()
1418 struct uvc_streaming *stream = uvc_urb->stream; in uvc_video_decode_isoc()
1422 for (i = 0; i < urb->number_of_packets; ++i) { in uvc_video_decode_isoc()
1423 if (urb->iso_frame_desc[i].status < 0) { in uvc_video_decode_isoc()
1424 uvc_dbg(stream->dev, FRAME, in uvc_video_decode_isoc()
1426 urb->iso_frame_desc[i].status); in uvc_video_decode_isoc()
1429 buf->error = 1; in uvc_video_decode_isoc()
1434 mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset; in uvc_video_decode_isoc()
1437 urb->iso_frame_desc[i].actual_length); in uvc_video_decode_isoc()
1438 if (ret == -EAGAIN) in uvc_video_decode_isoc()
1440 } while (ret == -EAGAIN); in uvc_video_decode_isoc()
1449 urb->iso_frame_desc[i].actual_length - ret); in uvc_video_decode_isoc()
1453 urb->iso_frame_desc[i].actual_length); in uvc_video_decode_isoc()
1455 if (buf->state == UVC_BUF_STATE_READY) in uvc_video_decode_isoc()
1463 struct urb *urb = uvc_urb->urb; in uvc_video_decode_bulk()
1464 struct uvc_streaming *stream = uvc_urb->stream; in uvc_video_decode_bulk()
1472 if (urb->actual_length == 0 && stream->bulk.header_size == 0) in uvc_video_decode_bulk()
1475 mem = urb->transfer_buffer; in uvc_video_decode_bulk()
1476 len = urb->actual_length; in uvc_video_decode_bulk()
1477 stream->bulk.payload_size += len; in uvc_video_decode_bulk()
1483 if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) { in uvc_video_decode_bulk()
1486 if (ret == -EAGAIN) in uvc_video_decode_bulk()
1488 } while (ret == -EAGAIN); in uvc_video_decode_bulk()
1492 stream->bulk.skip_payload = 1; in uvc_video_decode_bulk()
1494 memcpy(stream->bulk.header, mem, ret); in uvc_video_decode_bulk()
1495 stream->bulk.header_size = ret; in uvc_video_decode_bulk()
1500 len -= ret; in uvc_video_decode_bulk()
1511 if (!stream->bulk.skip_payload && buf != NULL) in uvc_video_decode_bulk()
1518 if (urb->actual_length < urb->transfer_buffer_length || in uvc_video_decode_bulk()
1519 stream->bulk.payload_size >= stream->bulk.max_payload_size) { in uvc_video_decode_bulk()
1520 if (!stream->bulk.skip_payload && buf != NULL) { in uvc_video_decode_bulk()
1521 uvc_video_decode_end(stream, buf, stream->bulk.header, in uvc_video_decode_bulk()
1522 stream->bulk.payload_size); in uvc_video_decode_bulk()
1523 if (buf->state == UVC_BUF_STATE_READY) in uvc_video_decode_bulk()
1527 stream->bulk.header_size = 0; in uvc_video_decode_bulk()
1528 stream->bulk.skip_payload = 0; in uvc_video_decode_bulk()
1529 stream->bulk.payload_size = 0; in uvc_video_decode_bulk()
1536 struct urb *urb = uvc_urb->urb; in uvc_video_encode_bulk()
1537 struct uvc_streaming *stream = uvc_urb->stream; in uvc_video_encode_bulk()
1539 u8 *mem = urb->transfer_buffer; in uvc_video_encode_bulk()
1540 int len = stream->urb_size, ret; in uvc_video_encode_bulk()
1543 urb->transfer_buffer_length = 0; in uvc_video_encode_bulk()
1548 if (stream->bulk.header_size == 0) { in uvc_video_encode_bulk()
1550 stream->bulk.header_size = ret; in uvc_video_encode_bulk()
1551 stream->bulk.payload_size += ret; in uvc_video_encode_bulk()
1553 len -= ret; in uvc_video_encode_bulk()
1559 stream->bulk.payload_size += ret; in uvc_video_encode_bulk()
1560 len -= ret; in uvc_video_encode_bulk()
1562 if (buf->bytesused == stream->queue.buf_used || in uvc_video_encode_bulk()
1563 stream->bulk.payload_size == stream->bulk.max_payload_size) { in uvc_video_encode_bulk()
1564 if (buf->bytesused == stream->queue.buf_used) { in uvc_video_encode_bulk()
1565 stream->queue.buf_used = 0; in uvc_video_encode_bulk()
1566 buf->state = UVC_BUF_STATE_READY; in uvc_video_encode_bulk()
1567 buf->buf.sequence = ++stream->sequence; in uvc_video_encode_bulk()
1568 uvc_queue_next_buffer(&stream->queue, buf); in uvc_video_encode_bulk()
1569 stream->last_fid ^= UVC_STREAM_FID; in uvc_video_encode_bulk()
1572 stream->bulk.header_size = 0; in uvc_video_encode_bulk()
1573 stream->bulk.payload_size = 0; in uvc_video_encode_bulk()
1576 urb->transfer_buffer_length = stream->urb_size - len; in uvc_video_encode_bulk()
1581 struct uvc_urb *uvc_urb = urb->context; in uvc_video_complete()
1582 struct uvc_streaming *stream = uvc_urb->stream; in uvc_video_complete()
1583 struct uvc_video_queue *queue = &stream->queue; in uvc_video_complete()
1584 struct uvc_video_queue *qmeta = &stream->meta.queue; in uvc_video_complete()
1585 struct vb2_queue *vb2_qmeta = stream->meta.vdev.queue; in uvc_video_complete()
1591 switch (urb->status) { in uvc_video_complete()
1596 dev_warn(&stream->intf->dev, in uvc_video_complete()
1597 "Non-zero status (%d) in video completion handler.\n", in uvc_video_complete()
1598 urb->status); in uvc_video_complete()
1600 case -ENOENT: /* usb_poison_urb() called. */ in uvc_video_complete()
1601 if (stream->frozen) in uvc_video_complete()
1604 case -ECONNRESET: /* usb_unlink_urb() called. */ in uvc_video_complete()
1605 case -ESHUTDOWN: /* The endpoint is being disabled. */ in uvc_video_complete()
1606 uvc_queue_cancel(queue, urb->status == -ESHUTDOWN); in uvc_video_complete()
1608 uvc_queue_cancel(qmeta, urb->status == -ESHUTDOWN); in uvc_video_complete()
1615 spin_lock_irqsave(&qmeta->irqlock, flags); in uvc_video_complete()
1616 if (!list_empty(&qmeta->irqqueue)) in uvc_video_complete()
1617 buf_meta = list_first_entry(&qmeta->irqqueue, in uvc_video_complete()
1619 spin_unlock_irqrestore(&qmeta->irqlock, flags); in uvc_video_complete()
1622 /* Re-initialise the URB async work. */ in uvc_video_complete()
1623 uvc_urb->async_operations = 0; in uvc_video_complete()
1626 dma_sync_sgtable_for_cpu(uvc_stream_to_dmadev(uvc_urb->stream), in uvc_video_complete()
1627 uvc_urb->sgt, uvc_stream_dir(stream)); in uvc_video_complete()
1628 invalidate_kernel_vmap_range(uvc_urb->buffer, in uvc_video_complete()
1629 uvc_urb->stream->urb_size); in uvc_video_complete()
1635 stream->decode(uvc_urb, buf, buf_meta); in uvc_video_complete()
1638 if (!uvc_urb->async_operations) { in uvc_video_complete()
1641 dev_err(&stream->intf->dev, in uvc_video_complete()
1646 queue_work(stream->async_wq, &uvc_urb->work); in uvc_video_complete()
1658 if (!uvc_urb->buffer) in uvc_free_urb_buffers()
1661 dma_vunmap_noncontiguous(dma_dev, uvc_urb->buffer); in uvc_free_urb_buffers()
1662 dma_free_noncontiguous(dma_dev, stream->urb_size, uvc_urb->sgt, in uvc_free_urb_buffers()
1665 uvc_urb->buffer = NULL; in uvc_free_urb_buffers()
1666 uvc_urb->sgt = NULL; in uvc_free_urb_buffers()
1669 stream->urb_size = 0; in uvc_free_urb_buffers()
1677 uvc_urb->sgt = dma_alloc_noncontiguous(dma_dev, stream->urb_size, in uvc_alloc_urb_buffer()
1680 if (!uvc_urb->sgt) in uvc_alloc_urb_buffer()
1682 uvc_urb->dma = uvc_urb->sgt->sgl->dma_address; in uvc_alloc_urb_buffer()
1684 uvc_urb->buffer = dma_vmap_noncontiguous(dma_dev, stream->urb_size, in uvc_alloc_urb_buffer()
1685 uvc_urb->sgt); in uvc_alloc_urb_buffer()
1686 if (!uvc_urb->buffer) { in uvc_alloc_urb_buffer()
1687 dma_free_noncontiguous(dma_dev, stream->urb_size, in uvc_alloc_urb_buffer()
1688 uvc_urb->sgt, in uvc_alloc_urb_buffer()
1690 uvc_urb->sgt = NULL; in uvc_alloc_urb_buffer()
1715 if (stream->urb_size) in uvc_alloc_urb_buffers()
1716 return stream->urb_size / psize; in uvc_alloc_urb_buffers()
1728 stream->urb_size = psize * npackets; in uvc_alloc_urb_buffers()
1731 struct uvc_urb *uvc_urb = &stream->uvc_urb[i]; in uvc_alloc_urb_buffers()
1738 uvc_urb->stream = stream; in uvc_alloc_urb_buffers()
1742 uvc_dbg(stream->dev, VIDEO, in uvc_alloc_urb_buffers()
1749 uvc_dbg(stream->dev, VIDEO, in uvc_alloc_urb_buffers()
1771 usb_poison_urb(uvc_urb->urb); in uvc_video_stop_transfer()
1773 flush_workqueue(stream->async_wq); in uvc_video_stop_transfer()
1776 usb_free_urb(uvc_urb->urb); in uvc_video_stop_transfer()
1777 uvc_urb->urb = NULL; in uvc_video_stop_transfer()
1791 switch (dev->speed) { in uvc_endpoint_max_bpi()
1794 return le16_to_cpu(ep->ss_ep_comp.wBytesPerInterval); in uvc_endpoint_max_bpi()
1796 psize = usb_endpoint_maxp(&ep->desc); in uvc_endpoint_max_bpi()
1797 psize *= usb_endpoint_maxp_mult(&ep->desc); in uvc_endpoint_max_bpi()
1815 psize = uvc_endpoint_max_bpi(stream->dev->udev, ep); in uvc_init_video_isoc()
1816 size = stream->ctrl.dwMaxVideoFrameSize; in uvc_init_video_isoc()
1820 return -ENOMEM; in uvc_init_video_isoc()
1828 return -ENOMEM; in uvc_init_video_isoc()
1831 urb->dev = stream->dev->udev; in uvc_init_video_isoc()
1832 urb->context = uvc_urb; in uvc_init_video_isoc()
1833 urb->pipe = usb_rcvisocpipe(stream->dev->udev, in uvc_init_video_isoc()
1834 ep->desc.bEndpointAddress); in uvc_init_video_isoc()
1835 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; in uvc_init_video_isoc()
1836 urb->transfer_dma = uvc_urb->dma; in uvc_init_video_isoc()
1837 urb->interval = ep->desc.bInterval; in uvc_init_video_isoc()
1838 urb->transfer_buffer = uvc_urb->buffer; in uvc_init_video_isoc()
1839 urb->complete = uvc_video_complete; in uvc_init_video_isoc()
1840 urb->number_of_packets = npackets; in uvc_init_video_isoc()
1841 urb->transfer_buffer_length = size; in uvc_init_video_isoc()
1844 urb->iso_frame_desc[i].offset = i * psize; in uvc_init_video_isoc()
1845 urb->iso_frame_desc[i].length = psize; in uvc_init_video_isoc()
1848 uvc_urb->urb = urb; in uvc_init_video_isoc()
1867 psize = usb_endpoint_maxp(&ep->desc); in uvc_init_video_bulk()
1868 size = stream->ctrl.dwMaxPayloadTransferSize; in uvc_init_video_bulk()
1869 stream->bulk.max_payload_size = size; in uvc_init_video_bulk()
1873 return -ENOMEM; in uvc_init_video_bulk()
1877 if (usb_endpoint_dir_in(&ep->desc)) in uvc_init_video_bulk()
1878 pipe = usb_rcvbulkpipe(stream->dev->udev, in uvc_init_video_bulk()
1879 ep->desc.bEndpointAddress); in uvc_init_video_bulk()
1881 pipe = usb_sndbulkpipe(stream->dev->udev, in uvc_init_video_bulk()
1882 ep->desc.bEndpointAddress); in uvc_init_video_bulk()
1884 if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) in uvc_init_video_bulk()
1891 return -ENOMEM; in uvc_init_video_bulk()
1894 usb_fill_bulk_urb(urb, stream->dev->udev, pipe, uvc_urb->buffer, in uvc_init_video_bulk()
1896 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; in uvc_init_video_bulk()
1897 urb->transfer_dma = uvc_urb->dma; in uvc_init_video_bulk()
1899 uvc_urb->urb = urb; in uvc_init_video_bulk()
1911 struct usb_interface *intf = stream->intf; in uvc_video_start_transfer()
1917 stream->sequence = -1; in uvc_video_start_transfer()
1918 stream->last_fid = -1; in uvc_video_start_transfer()
1919 stream->bulk.header_size = 0; in uvc_video_start_transfer()
1920 stream->bulk.skip_payload = 0; in uvc_video_start_transfer()
1921 stream->bulk.payload_size = 0; in uvc_video_start_transfer()
1925 if (intf->num_altsetting > 1) { in uvc_video_start_transfer()
1930 int intfnum = stream->intfnum; in uvc_video_start_transfer()
1933 bandwidth = stream->ctrl.dwMaxPayloadTransferSize; in uvc_video_start_transfer()
1936 uvc_dbg(stream->dev, VIDEO, in uvc_video_start_transfer()
1940 uvc_dbg(stream->dev, VIDEO, in uvc_video_start_transfer()
1945 for (i = 0; i < intf->num_altsetting; ++i) { in uvc_video_start_transfer()
1949 alts = &intf->altsetting[i]; in uvc_video_start_transfer()
1951 stream->header.bEndpointAddress); in uvc_video_start_transfer()
1956 psize = uvc_endpoint_max_bpi(stream->dev->udev, ep); in uvc_video_start_transfer()
1958 altsetting = alts->desc.bAlternateSetting; in uvc_video_start_transfer()
1965 uvc_dbg(stream->dev, VIDEO, in uvc_video_start_transfer()
1967 return -EIO; in uvc_video_start_transfer()
1970 uvc_dbg(stream->dev, VIDEO, in uvc_video_start_transfer()
1979 if (stream->dev->quirks & UVC_QUIRK_WAKE_AUTOSUSPEND) { in uvc_video_start_transfer()
1980 usb_set_interface(stream->dev->udev, intfnum, in uvc_video_start_transfer()
1982 usb_set_interface(stream->dev->udev, intfnum, 0); in uvc_video_start_transfer()
1985 ret = usb_set_interface(stream->dev->udev, intfnum, altsetting); in uvc_video_start_transfer()
1992 ep = uvc_find_endpoint(&intf->altsetting[0], in uvc_video_start_transfer()
1993 stream->header.bEndpointAddress); in uvc_video_start_transfer()
1995 return -EIO; in uvc_video_start_transfer()
1998 if (usb_endpoint_maxp(&ep->desc) == 0) in uvc_video_start_transfer()
1999 return -EIO; in uvc_video_start_transfer()
2011 dev_err(&stream->intf->dev, in uvc_video_start_transfer()
2023 if (stream->dev->quirks & UVC_QUIRK_RESTORE_CTRLS_ON_INIT) in uvc_video_start_transfer()
2024 uvc_ctrl_restore_values(stream->dev); in uvc_video_start_transfer()
2029 /* --------------------------------------------------------------------------
2042 if (!uvc_queue_streaming(&stream->queue)) in uvc_video_suspend()
2045 stream->frozen = 1; in uvc_video_suspend()
2047 usb_set_interface(stream->dev->udev, stream->intfnum, 0); in uvc_video_suspend()
2070 usb_set_interface(stream->dev->udev, stream->intfnum, 0); in uvc_video_resume()
2072 stream->frozen = 0; in uvc_video_resume()
2076 if (!uvc_queue_streaming(&stream->queue)) in uvc_video_resume()
2079 ret = uvc_commit_video(stream, &stream->ctrl); in uvc_video_resume()
2086 /* ------------------------------------------------------------------------
2102 struct uvc_streaming_control *probe = &stream->ctrl; in uvc_video_init()
2109 if (stream->nformats == 0) { in uvc_video_init()
2110 dev_info(&stream->intf->dev, in uvc_video_init()
2112 return -EINVAL; in uvc_video_init()
2115 atomic_set(&stream->active, 0); in uvc_video_init()
2123 usb_set_interface(stream->dev->udev, stream->intfnum, 0); in uvc_video_init()
2149 if (ret == -EPROTO && in uvc_video_init()
2150 usb_match_one_id(stream->dev->intf, &elgato_cam_link_4k)) { in uvc_video_init()
2151 dev_err(&stream->intf->dev, "Elgato Cam Link 4K firmware crash detected\n"); in uvc_video_init()
2152 dev_err(&stream->intf->dev, "Resetting the device, unplug and replug to recover\n"); in uvc_video_init()
2153 usb_reset_device(stream->dev->udev); in uvc_video_init()
2163 for (i = stream->nformats; i > 0; --i) { in uvc_video_init()
2164 format = &stream->formats[i-1]; in uvc_video_init()
2165 if (format->index == probe->bFormatIndex) in uvc_video_init()
2169 if (format->nframes == 0) { in uvc_video_init()
2170 dev_info(&stream->intf->dev, in uvc_video_init()
2172 return -EINVAL; in uvc_video_init()
2176 * Zero bFrameIndex might be correct. Stream-based formats (including in uvc_video_init()
2177 * MPEG-2 TS and DV) do not support frames but have a dummy frame in uvc_video_init()
2181 for (i = format->nframes; i > 0; --i) { in uvc_video_init()
2182 frame = &format->frames[i-1]; in uvc_video_init()
2183 if (frame->bFrameIndex == probe->bFrameIndex) in uvc_video_init()
2187 probe->bFormatIndex = format->index; in uvc_video_init()
2188 probe->bFrameIndex = frame->bFrameIndex; in uvc_video_init()
2190 stream->def_format = format; in uvc_video_init()
2191 stream->cur_format = format; in uvc_video_init()
2192 stream->cur_frame = frame; in uvc_video_init()
2195 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { in uvc_video_init()
2196 if (stream->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT) in uvc_video_init()
2197 stream->decode = uvc_video_decode_isight; in uvc_video_init()
2198 else if (stream->intf->num_altsetting > 1) in uvc_video_init()
2199 stream->decode = uvc_video_decode_isoc; in uvc_video_init()
2201 stream->decode = uvc_video_decode_bulk; in uvc_video_init()
2203 if (stream->intf->num_altsetting == 1) in uvc_video_init()
2204 stream->decode = uvc_video_encode_bulk; in uvc_video_init()
2206 dev_info(&stream->intf->dev, in uvc_video_init()
2208 return -EINVAL; in uvc_video_init()
2214 INIT_WORK(&uvc_urb->work, uvc_video_copy_data_work); in uvc_video_init()
2228 ret = uvc_commit_video(stream, &stream->ctrl); in uvc_video_start_streaming()
2239 usb_set_interface(stream->dev->udev, stream->intfnum, 0); in uvc_video_start_streaming()
2250 if (stream->intf->num_altsetting > 1) { in uvc_video_stop_streaming()
2251 usb_set_interface(stream->dev->udev, stream->intfnum, 0); in uvc_video_stop_streaming()
2254 * UVC doesn't specify how to inform a bulk-based device in uvc_video_stop_streaming()
2259 unsigned int epnum = stream->header.bEndpointAddress in uvc_video_stop_streaming()
2261 unsigned int dir = stream->header.bEndpointAddress in uvc_video_stop_streaming()
2265 pipe = usb_sndbulkpipe(stream->dev->udev, epnum) | dir; in uvc_video_stop_streaming()
2266 usb_clear_halt(stream->dev->udev, pipe); in uvc_video_stop_streaming()