Lines Matching +full:int +full:- +full:clock +full:- +full:stable +full:- +full:broken

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 /* ------------------------------------------------------------------------
31 static int __uvc_query_ctrl(struct uvc_device *dev, u8 query, u8 unit, in __uvc_query_ctrl()
33 int timeout) in __uvc_query_ctrl()
36 unsigned int pipe; in __uvc_query_ctrl()
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()
70 int uvc_query_ctrl(struct uvc_device *dev, u8 query, u8 unit, in uvc_query_ctrl()
73 int ret; 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()
142 unsigned int i; in uvc_fixup_video_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- and super-speed devices) per second and add the UVC 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()
255 static int uvc_get_video_ctrl(struct uvc_streaming *stream, in uvc_get_video_ctrl()
256 struct uvc_streaming_control *ctrl, int probe, u8 query) in uvc_get_video_ctrl()
260 int ret; in uvc_get_video_ctrl()
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()
333 * Some broken devices return null or wrong dwMaxVideoFrameSize and in uvc_get_video_ctrl()
345 static int uvc_set_video_ctrl(struct uvc_streaming *stream, in uvc_set_video_ctrl()
346 struct uvc_streaming_control *ctrl, int probe) in uvc_set_video_ctrl()
350 int ret; in uvc_set_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()
390 int uvc_probe_video(struct uvc_streaming *stream, in uvc_probe_video()
394 unsigned int i; in uvc_probe_video()
395 int ret; in uvc_probe_video()
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()
451 static int uvc_commit_video(struct uvc_streaming *stream, in uvc_commit_video()
457 /* -----------------------------------------------------------------------------
469 static void uvc_video_clock_add_sample(struct uvc_clock *clock, in uvc_video_clock_add_sample() argument
479 if (clock->head == clock->last_sof_overflow) in uvc_video_clock_add_sample()
480 clock->last_sof_overflow = -1; in uvc_video_clock_add_sample()
482 spin_lock_irqsave(&clock->lock, flags); in uvc_video_clock_add_sample()
484 if (clock->count > 0 && clock->last_sof > sample->dev_sof) { in uvc_video_clock_add_sample()
490 if (clock->last_sof_overflow != -1) in uvc_video_clock_add_sample()
491 clock->count = (clock->head - clock->last_sof_overflow in uvc_video_clock_add_sample()
492 + clock->size) % clock->size; in uvc_video_clock_add_sample()
493 clock->last_sof_overflow = clock->head; in uvc_video_clock_add_sample()
497 clock->samples[clock->head] = *sample; in uvc_video_clock_add_sample()
498 clock->head = (clock->head + 1) % clock->size; in uvc_video_clock_add_sample()
499 clock->count = min(clock->count + 1, clock->size); in uvc_video_clock_add_sample()
501 spin_unlock_irqrestore(&clock->lock, flags); in uvc_video_clock_add_sample()
506 const u8 *data, int len) in uvc_video_clock_decode()
509 unsigned int header_size; in uvc_video_clock_decode()
539 * - store the frame PTS in the buffer structure in uvc_video_clock_decode()
540 * - if the SCR field is present, retrieve the host SOF counter and in uvc_video_clock_decode()
545 buf->pts = get_unaligned_le32(&data[2]); in uvc_video_clock_decode()
556 sample.dev_sof = get_unaligned_le16(&data[header_size - 2]); in uvc_video_clock_decode()
557 if (sample.dev_sof == stream->clock.last_sof) in uvc_video_clock_decode()
560 sample.dev_stc = get_unaligned_le32(&data[header_size - 6]); in uvc_video_clock_decode()
563 * STC (Source Time Clock) is the clock used by the camera. The UVC 1.5 in uvc_video_clock_decode()
581 * clock sample from many and the clock recovery algorithm wouldn't in uvc_video_clock_decode()
584 if (buf && buf->bytesused == 0 && len == header_size && in uvc_video_clock_decode()
588 sample.host_sof = usb_get_current_frame_number(stream->dev->udev); in uvc_video_clock_decode()
592 * at a stable rate of 1kHz. For those devices use the host SOF instead. in uvc_video_clock_decode()
597 if (stream->dev->quirks & UVC_QUIRK_INVALID_DEVICE_SOF) in uvc_video_clock_decode()
622 if (stream->clock.sof_offset == (u16)-1) { in uvc_video_clock_decode()
623 u16 delta_sof = (sample.host_sof - sample.dev_sof) & 255; in uvc_video_clock_decode()
625 stream->clock.sof_offset = delta_sof; in uvc_video_clock_decode()
627 stream->clock.sof_offset = 0; in uvc_video_clock_decode()
630 sample.dev_sof = (sample.dev_sof + stream->clock.sof_offset) & 2047; in uvc_video_clock_decode()
631 uvc_video_clock_add_sample(&stream->clock, &sample); in uvc_video_clock_decode()
632 stream->clock.last_sof = sample.dev_sof; in uvc_video_clock_decode()
635 static void uvc_video_clock_reset(struct uvc_clock *clock) in uvc_video_clock_reset() argument
637 clock->head = 0; in uvc_video_clock_reset()
638 clock->count = 0; in uvc_video_clock_reset()
639 clock->last_sof = -1; in uvc_video_clock_reset()
640 clock->last_sof_overflow = -1; in uvc_video_clock_reset()
641 clock->sof_offset = -1; in uvc_video_clock_reset()
644 static int uvc_video_clock_init(struct uvc_clock *clock) in uvc_video_clock_init() argument
646 spin_lock_init(&clock->lock); in uvc_video_clock_init()
647 clock->size = 32; in uvc_video_clock_init()
649 clock->samples = kmalloc_array(clock->size, sizeof(*clock->samples), in uvc_video_clock_init()
651 if (clock->samples == NULL) in uvc_video_clock_init()
652 return -ENOMEM; in uvc_video_clock_init()
654 uvc_video_clock_reset(clock); in uvc_video_clock_init()
659 static void uvc_video_clock_cleanup(struct uvc_clock *clock) in uvc_video_clock_cleanup() argument
661 kfree(clock->samples); in uvc_video_clock_cleanup()
662 clock->samples = NULL; in uvc_video_clock_cleanup()
666 * uvc_video_clock_host_sof - Return the host SOF value for a clock sample
669 * cover the whole 11-bits SOF range (0-2047) but are limited to the HCI frame
687 delta_sof = (sample->host_sof - sample->dev_sof) & 255; in uvc_video_clock_host_sof()
689 return (sample->dev_sof + delta_sof) & 2047; in uvc_video_clock_host_sof()
693 * uvc_video_clock_update - Update the buffer timestamp
695 * This function converts the buffer PTS timestamp to the host clock domain by
696 * going through the USB SOF clock domain and stores the result in the V4L2
699 * The relationship between the device clock and the host clock isn't known.
700 * However, the device and the host share the common USB SOF clock which can be
703 * The relationship between the device clock and the USB SOF clock is considered
704 * to be linear over the clock samples sliding window and is given by
709 * the clock drift should be small compared to the sliding window size, we
713 * SOF = (SOF2 - SOF1) / (STC2 - STC1) * PTS
714 * + (SOF1 * STC2 - SOF2 * STC1) / (STC2 - STC1)
718 * SOF = ((SOF2 - SOF1) * PTS + SOF1 * STC2 - SOF2 * STC1) / (STC2 - STC1) (1)
723 * TS = ((TS2 - TS1) * SOF + TS1 * SOF2 - TS2 * SOF1) / (SOF2 - SOF1) (2)
728 * TODO: To avoid surprises with device clock values, PTS/STC timestamps should
729 * be normalized using the nominal device clock frequency reported through the
757 struct uvc_clock *clock = &stream->clock; in uvc_video_clock_update() local
777 if (!clock->samples) in uvc_video_clock_update()
780 spin_lock_irqsave(&clock->lock, flags); in uvc_video_clock_update()
782 if (clock->count < 2) in uvc_video_clock_update()
785 first = &clock->samples[(clock->head - clock->count + clock->size) % clock->size]; in uvc_video_clock_update()
786 last = &clock->samples[(clock->head - 1 + clock->size) % clock->size]; in uvc_video_clock_update()
789 delta_stc = buf->pts - (1UL << 31); in uvc_video_clock_update()
790 x1 = first->dev_stc - delta_stc; in uvc_video_clock_update()
791 x2 = last->dev_stc - delta_stc; in uvc_video_clock_update()
795 y1 = (first->dev_sof + 2048) << 16; in uvc_video_clock_update()
796 y2 = (last->dev_sof + 2048) << 16; in uvc_video_clock_update()
809 if ((y2 - y1) < ((1000 / 4) << 16)) in uvc_video_clock_update()
812 y = (u64)(y2 - y1) * (1ULL << 31) + (u64)y1 * (u64)x2 in uvc_video_clock_update()
813 - (u64)y2 * (u64)x1; in uvc_video_clock_update()
814 y = div_u64(y, x2 - x1); in uvc_video_clock_update()
818 uvc_dbg(stream->dev, CLOCK, in uvc_video_clock_update()
820 stream->dev->name, buf->pts, in uvc_video_clock_update()
823 x1, x2, y1, y2, clock->sof_offset); in uvc_video_clock_update()
825 /* Second step, SOF to host clock conversion. */ in uvc_video_clock_update()
834 y2 = ktime_to_ns(ktime_sub(last->host_time, first->host_time)) + y1; in uvc_video_clock_update()
843 if (mean - (1024 << 16) > sof) in uvc_video_clock_update()
846 sof -= 2048 << 16; in uvc_video_clock_update()
848 y = (u64)(y2 - y1) * (u64)sof + (u64)y1 * (u64)x2 in uvc_video_clock_update()
849 - (u64)y2 * (u64)x1; in uvc_video_clock_update()
850 y = div_u64(y, x2 - x1); in uvc_video_clock_update()
852 timestamp = ktime_to_ns(first->host_time) + y - y1; in uvc_video_clock_update()
854 uvc_dbg(stream->dev, CLOCK, in uvc_video_clock_update()
856 stream->dev->name, in uvc_video_clock_update()
858 y, timestamp, vbuf->vb2_buf.timestamp, in uvc_video_clock_update()
859 x1, first->host_sof, first->dev_sof, in uvc_video_clock_update()
860 x2, last->host_sof, last->dev_sof, y1, y2); in uvc_video_clock_update()
863 vbuf->vb2_buf.timestamp = timestamp; in uvc_video_clock_update()
866 spin_unlock_irqrestore(&clock->lock, flags); in uvc_video_clock_update()
869 /* ------------------------------------------------------------------------
874 const u8 *data, int len) in uvc_video_stats_decode()
876 unsigned int header_size; in uvc_video_stats_decode()
883 if (stream->stats.stream.nb_frames == 0 && in uvc_video_stats_decode()
884 stream->stats.frame.nb_packets == 0) in uvc_video_stats_decode()
885 stream->stats.stream.start_ts = ktime_get(); in uvc_video_stats_decode()
908 stream->stats.frame.nb_invalid++; in uvc_video_stats_decode()
917 scr_stc = get_unaligned_le32(&data[header_size - 6]); in uvc_video_stats_decode()
918 scr_sof = get_unaligned_le16(&data[header_size - 2]); in uvc_video_stats_decode()
922 if (has_pts && stream->stats.frame.nb_pts) { in uvc_video_stats_decode()
923 if (stream->stats.frame.pts != pts) { in uvc_video_stats_decode()
924 stream->stats.frame.nb_pts_diffs++; in uvc_video_stats_decode()
925 stream->stats.frame.last_pts_diff = in uvc_video_stats_decode()
926 stream->stats.frame.nb_packets; in uvc_video_stats_decode()
931 stream->stats.frame.nb_pts++; in uvc_video_stats_decode()
932 stream->stats.frame.pts = pts; in uvc_video_stats_decode()
936 * Do all frames have a PTS in their first non-empty packet, or before in uvc_video_stats_decode()
939 if (stream->stats.frame.size == 0) { in uvc_video_stats_decode()
941 stream->stats.frame.has_initial_pts = has_pts; in uvc_video_stats_decode()
943 stream->stats.frame.has_early_pts = true; in uvc_video_stats_decode()
947 if (has_scr && stream->stats.frame.nb_scr) { in uvc_video_stats_decode()
948 if (stream->stats.frame.scr_stc != scr_stc) in uvc_video_stats_decode()
949 stream->stats.frame.nb_scr_diffs++; in uvc_video_stats_decode()
954 if (stream->stats.stream.nb_frames > 0 || in uvc_video_stats_decode()
955 stream->stats.frame.nb_scr > 0) in uvc_video_stats_decode()
956 stream->stats.stream.scr_sof_count += in uvc_video_stats_decode()
957 (scr_sof - stream->stats.stream.scr_sof) % 2048; in uvc_video_stats_decode()
958 stream->stats.stream.scr_sof = scr_sof; in uvc_video_stats_decode()
960 stream->stats.frame.nb_scr++; in uvc_video_stats_decode()
961 stream->stats.frame.scr_stc = scr_stc; in uvc_video_stats_decode()
962 stream->stats.frame.scr_sof = scr_sof; in uvc_video_stats_decode()
964 if (scr_sof < stream->stats.stream.min_sof) in uvc_video_stats_decode()
965 stream->stats.stream.min_sof = scr_sof; in uvc_video_stats_decode()
966 if (scr_sof > stream->stats.stream.max_sof) in uvc_video_stats_decode()
967 stream->stats.stream.max_sof = scr_sof; in uvc_video_stats_decode()
970 /* Record the first non-empty packet number. */ in uvc_video_stats_decode()
971 if (stream->stats.frame.size == 0 && len > header_size) in uvc_video_stats_decode()
972 stream->stats.frame.first_data = stream->stats.frame.nb_packets; in uvc_video_stats_decode()
975 stream->stats.frame.size += len - header_size; in uvc_video_stats_decode()
978 stream->stats.frame.nb_packets++; in uvc_video_stats_decode()
980 stream->stats.frame.nb_empty++; in uvc_video_stats_decode()
983 stream->stats.frame.nb_errors++; in uvc_video_stats_decode()
988 struct uvc_stats_frame *frame = &stream->stats.frame; in uvc_video_stats_update()
990 uvc_dbg(stream->dev, STATS, in uvc_video_stats_update()
992 stream->sequence, frame->first_data, in uvc_video_stats_update()
993 frame->nb_packets - frame->nb_empty, frame->nb_packets, in uvc_video_stats_update()
994 frame->nb_pts_diffs, frame->last_pts_diff, frame->nb_pts, in uvc_video_stats_update()
995 frame->has_early_pts ? "" : "!", in uvc_video_stats_update()
996 frame->has_initial_pts ? "" : "!", in uvc_video_stats_update()
997 frame->nb_scr_diffs, frame->nb_scr, in uvc_video_stats_update()
998 frame->pts, frame->scr_stc, frame->scr_sof); in uvc_video_stats_update()
1000 stream->stats.stream.nb_frames++; in uvc_video_stats_update()
1001 stream->stats.stream.nb_packets += stream->stats.frame.nb_packets; in uvc_video_stats_update()
1002 stream->stats.stream.nb_empty += stream->stats.frame.nb_empty; in uvc_video_stats_update()
1003 stream->stats.stream.nb_errors += stream->stats.frame.nb_errors; in uvc_video_stats_update()
1004 stream->stats.stream.nb_invalid += stream->stats.frame.nb_invalid; in uvc_video_stats_update()
1006 if (frame->has_early_pts) in uvc_video_stats_update()
1007 stream->stats.stream.nb_pts_early++; in uvc_video_stats_update()
1008 if (frame->has_initial_pts) in uvc_video_stats_update()
1009 stream->stats.stream.nb_pts_initial++; in uvc_video_stats_update()
1010 if (frame->last_pts_diff <= frame->first_data) in uvc_video_stats_update()
1011 stream->stats.stream.nb_pts_constant++; in uvc_video_stats_update()
1012 if (frame->nb_scr >= frame->nb_packets - frame->nb_empty) in uvc_video_stats_update()
1013 stream->stats.stream.nb_scr_count_ok++; in uvc_video_stats_update()
1014 if (frame->nb_scr_diffs + 1 == frame->nb_scr) in uvc_video_stats_update()
1015 stream->stats.stream.nb_scr_diffs_ok++; in uvc_video_stats_update()
1017 memset(&stream->stats.frame, 0, sizeof(stream->stats.frame)); in uvc_video_stats_update()
1023 unsigned int scr_sof_freq; in uvc_video_stats_dump()
1024 unsigned int duration; in uvc_video_stats_dump()
1031 duration = ktime_ms_delta(stream->stats.stream.stop_ts, in uvc_video_stats_dump()
1032 stream->stats.stream.start_ts); in uvc_video_stats_dump()
1034 scr_sof_freq = stream->stats.stream.scr_sof_count * 1000 in uvc_video_stats_dump()
1039 count += scnprintf(buf + count, size - count, in uvc_video_stats_dump()
1042 stream->stats.stream.nb_frames, in uvc_video_stats_dump()
1043 stream->stats.stream.nb_packets, in uvc_video_stats_dump()
1044 stream->stats.stream.nb_empty, in uvc_video_stats_dump()
1045 stream->stats.stream.nb_errors, in uvc_video_stats_dump()
1046 stream->stats.stream.nb_invalid); in uvc_video_stats_dump()
1047 count += scnprintf(buf + count, size - count, in uvc_video_stats_dump()
1049 stream->stats.stream.nb_pts_early, in uvc_video_stats_dump()
1050 stream->stats.stream.nb_pts_initial, in uvc_video_stats_dump()
1051 stream->stats.stream.nb_pts_constant); in uvc_video_stats_dump()
1052 count += scnprintf(buf + count, size - count, in uvc_video_stats_dump()
1054 stream->stats.stream.nb_scr_count_ok, in uvc_video_stats_dump()
1055 stream->stats.stream.nb_scr_diffs_ok); in uvc_video_stats_dump()
1056 count += scnprintf(buf + count, size - count, in uvc_video_stats_dump()
1058 stream->stats.stream.min_sof, in uvc_video_stats_dump()
1059 stream->stats.stream.max_sof, in uvc_video_stats_dump()
1067 memset(&stream->stats, 0, sizeof(stream->stats)); in uvc_video_stats_start()
1068 stream->stats.stream.min_sof = 2048; in uvc_video_stats_start()
1073 stream->stats.stream.stop_ts = ktime_get(); in uvc_video_stats_stop()
1076 /* ------------------------------------------------------------------------
1089 * - EAGAIN informs the caller that the current video buffer should be marked
1094 * If an error other than -EAGAIN is returned, the caller will drop the current
1096 * made until the next payload. -ENODATA can be used to drop the current
1116 static int uvc_video_decode_start(struct uvc_streaming *stream, in uvc_video_decode_start()
1117 struct uvc_buffer *buf, const u8 *data, int len) in uvc_video_decode_start()
1123 * - packet must be at least 2 bytes long in uvc_video_decode_start()
1124 * - bHeaderLength value must be at least 2 bytes (see above) in uvc_video_decode_start()
1125 * - bHeaderLength value can't be larger than the packet size. in uvc_video_decode_start()
1128 stream->stats.frame.nb_invalid++; in uvc_video_decode_start()
1129 return -EINVAL; in uvc_video_decode_start()
1138 if (stream->last_fid != fid) { in uvc_video_decode_start()
1139 stream->sequence++; in uvc_video_decode_start()
1140 if (stream->sequence) in uvc_video_decode_start()
1152 stream->last_fid = fid; in uvc_video_decode_start()
1153 return -ENODATA; in uvc_video_decode_start()
1158 uvc_dbg(stream->dev, FRAME, in uvc_video_decode_start()
1160 buf->error = 1; in uvc_video_decode_start()
1166 * stream->last_fid is initialized to -1, so the first isochronous in uvc_video_decode_start()
1169 * If the device doesn't toggle the FID bit, invert stream->last_fid in uvc_video_decode_start()
1172 if (buf->state != UVC_BUF_STATE_ACTIVE) { in uvc_video_decode_start()
1173 if (fid == stream->last_fid) { in uvc_video_decode_start()
1174 uvc_dbg(stream->dev, FRAME, in uvc_video_decode_start()
1176 if ((stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) && in uvc_video_decode_start()
1178 stream->last_fid ^= UVC_STREAM_FID; in uvc_video_decode_start()
1179 return -ENODATA; in uvc_video_decode_start()
1182 buf->buf.field = V4L2_FIELD_NONE; in uvc_video_decode_start()
1183 buf->buf.sequence = stream->sequence; in uvc_video_decode_start()
1184 buf->buf.vb2_buf.timestamp = ktime_to_ns(uvc_video_get_time()); in uvc_video_decode_start()
1187 buf->state = UVC_BUF_STATE_ACTIVE; in uvc_video_decode_start()
1198 * stream->last_fid is initialized to -1, so the first isochronous in uvc_video_decode_start()
1206 if (fid != stream->last_fid && buf->bytesused != 0) { in uvc_video_decode_start()
1207 uvc_dbg(stream->dev, FRAME, in uvc_video_decode_start()
1209 buf->state = UVC_BUF_STATE_READY; in uvc_video_decode_start()
1210 return -EAGAIN; in uvc_video_decode_start()
1213 stream->last_fid = fid; in uvc_video_decode_start()
1221 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) in uvc_stream_dir()
1229 return bus_to_hcd(stream->dev->udev->bus)->self.sysdev; in uvc_stream_to_dmadev()
1232 static int uvc_submit_urb(struct uvc_urb *uvc_urb, gfp_t mem_flags) in uvc_submit_urb()
1235 dma_sync_sgtable_for_device(uvc_stream_to_dmadev(uvc_urb->stream), in uvc_submit_urb()
1236 uvc_urb->sgt, in uvc_submit_urb()
1237 uvc_stream_dir(uvc_urb->stream)); in uvc_submit_urb()
1238 return usb_submit_urb(uvc_urb->urb, mem_flags); in uvc_submit_urb()
1250 unsigned int i; in uvc_video_copy_data_work()
1251 int ret; in uvc_video_copy_data_work()
1253 for (i = 0; i < uvc_urb->async_operations; i++) { in uvc_video_copy_data_work()
1254 struct uvc_copy_op *op = &uvc_urb->copy_operations[i]; in uvc_video_copy_data_work()
1256 memcpy(op->dst, op->src, op->len); in uvc_video_copy_data_work()
1259 uvc_queue_buffer_release(op->buf); in uvc_video_copy_data_work()
1264 dev_err(&uvc_urb->stream->intf->dev, in uvc_video_copy_data_work()
1269 struct uvc_buffer *buf, const u8 *data, int len) in uvc_video_decode_data()
1271 unsigned int active_op = uvc_urb->async_operations; in uvc_video_decode_data()
1272 struct uvc_copy_op *op = &uvc_urb->copy_operations[active_op]; in uvc_video_decode_data()
1273 unsigned int maxlen; in uvc_video_decode_data()
1278 maxlen = buf->length - buf->bytesused; in uvc_video_decode_data()
1281 kref_get(&buf->ref); in uvc_video_decode_data()
1283 op->buf = buf; in uvc_video_decode_data()
1284 op->src = data; in uvc_video_decode_data()
1285 op->dst = buf->mem + buf->bytesused; in uvc_video_decode_data()
1286 op->len = min_t(unsigned int, len, maxlen); in uvc_video_decode_data()
1288 buf->bytesused += op->len; in uvc_video_decode_data()
1292 uvc_dbg(uvc_urb->stream->dev, FRAME, in uvc_video_decode_data()
1294 buf->error = 1; in uvc_video_decode_data()
1295 buf->state = UVC_BUF_STATE_READY; in uvc_video_decode_data()
1298 uvc_urb->async_operations++; in uvc_video_decode_data()
1302 struct uvc_buffer *buf, const u8 *data, int len) in uvc_video_decode_end()
1305 if (data[1] & UVC_STREAM_EOF && buf->bytesused != 0) { in uvc_video_decode_end()
1306 uvc_dbg(stream->dev, FRAME, "Frame complete (EOF found)\n"); in uvc_video_decode_end()
1308 uvc_dbg(stream->dev, FRAME, "EOF in empty payload\n"); in uvc_video_decode_end()
1309 buf->state = UVC_BUF_STATE_READY; in uvc_video_decode_end()
1310 if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) in uvc_video_decode_end()
1311 stream->last_fid ^= UVC_STREAM_FID; in uvc_video_decode_end()
1327 static int uvc_video_encode_header(struct uvc_streaming *stream, in uvc_video_encode_header()
1328 struct uvc_buffer *buf, u8 *data, int len) in uvc_video_encode_header()
1332 | (stream->last_fid & UVC_STREAM_FID); in uvc_video_encode_header()
1336 static int uvc_video_encode_data(struct uvc_streaming *stream, in uvc_video_encode_data()
1337 struct uvc_buffer *buf, u8 *data, int len) in uvc_video_encode_data()
1339 struct uvc_video_queue *queue = &stream->queue; in uvc_video_encode_data()
1340 unsigned int nbytes; in uvc_video_encode_data()
1344 mem = buf->mem + queue->buf_used; in uvc_video_encode_data()
1345 nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used); in uvc_video_encode_data()
1346 nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size, in uvc_video_encode_data()
1350 queue->buf_used += nbytes; in uvc_video_encode_data()
1355 /* ------------------------------------------------------------------------
1362 * of blocks, containing a 64-bit timestamp in nanoseconds, a 16-bit USB Frame
1372 const u8 *mem, unsigned int length) in uvc_video_decode_meta()
1378 unsigned int sof; in uvc_video_decode_meta()
1385 if (meta_buf->length - meta_buf->bytesused < in uvc_video_decode_meta()
1386 length + sizeof(meta->ns) + sizeof(meta->sof)) { in uvc_video_decode_meta()
1387 meta_buf->error = 1; in uvc_video_decode_meta()
1404 if (stream->meta.format == V4L2_META_FMT_UVC) in uvc_video_decode_meta()
1408 !memcmp(scr, stream->clock.last_scr, 6))) in uvc_video_decode_meta()
1411 meta = (struct uvc_meta_buf *)((u8 *)meta_buf->mem + meta_buf->bytesused); in uvc_video_decode_meta()
1414 sof = usb_get_current_frame_number(stream->dev->udev); in uvc_video_decode_meta()
1416 put_unaligned(ktime_to_ns(time), &meta->ns); in uvc_video_decode_meta()
1417 put_unaligned(sof, &meta->sof); in uvc_video_decode_meta()
1420 memcpy(stream->clock.last_scr, scr, 6); in uvc_video_decode_meta()
1422 meta->length = mem[0]; in uvc_video_decode_meta()
1423 meta->flags = mem[1]; in uvc_video_decode_meta()
1424 memcpy(meta->buf, &mem[2], length - 2); in uvc_video_decode_meta()
1425 meta_buf->bytesused += length + sizeof(meta->ns) + sizeof(meta->sof); in uvc_video_decode_meta()
1427 uvc_dbg(stream->dev, FRAME, in uvc_video_decode_meta()
1428 "%s(): t-sys %lluns, SOF %u, len %u, flags 0x%x, PTS %u, STC %u frame SOF %u\n", in uvc_video_decode_meta()
1429 __func__, ktime_to_ns(time), meta->sof, meta->length, in uvc_video_decode_meta()
1430 meta->flags, in uvc_video_decode_meta()
1431 has_pts ? *(u32 *)meta->buf : 0, in uvc_video_decode_meta()
1436 /* ------------------------------------------------------------------------
1446 if (stream->ctrl.dwMaxVideoFrameSize != buf->bytesused && in uvc_video_validate_buffer()
1447 !(stream->cur_format->flags & UVC_FMT_FLAG_COMPRESSED)) in uvc_video_validate_buffer()
1448 buf->error = 1; in uvc_video_validate_buffer()
1461 struct vb2_v4l2_buffer *vb2_meta = &(*meta_buf)->buf; in uvc_video_next_buffers()
1462 const struct vb2_v4l2_buffer *vb2_video = &(*video_buf)->buf; in uvc_video_next_buffers()
1464 vb2_meta->sequence = vb2_video->sequence; in uvc_video_next_buffers()
1465 vb2_meta->field = vb2_video->field; in uvc_video_next_buffers()
1466 vb2_meta->vb2_buf.timestamp = vb2_video->vb2_buf.timestamp; in uvc_video_next_buffers()
1468 (*meta_buf)->state = UVC_BUF_STATE_READY; in uvc_video_next_buffers()
1469 if (!(*meta_buf)->error) in uvc_video_next_buffers()
1470 (*meta_buf)->error = (*video_buf)->error; in uvc_video_next_buffers()
1471 *meta_buf = uvc_queue_next_buffer(&stream->meta.queue, in uvc_video_next_buffers()
1474 *video_buf = uvc_queue_next_buffer(&stream->queue, *video_buf); in uvc_video_next_buffers()
1480 struct urb *urb = uvc_urb->urb; in uvc_video_decode_isoc()
1481 struct uvc_streaming *stream = uvc_urb->stream; in uvc_video_decode_isoc()
1483 int ret, i; in uvc_video_decode_isoc()
1485 for (i = 0; i < urb->number_of_packets; ++i) { in uvc_video_decode_isoc()
1486 if (urb->iso_frame_desc[i].status < 0) { in uvc_video_decode_isoc()
1487 uvc_dbg(stream->dev, FRAME, in uvc_video_decode_isoc()
1489 urb->iso_frame_desc[i].status); in uvc_video_decode_isoc()
1492 buf->error = 1; in uvc_video_decode_isoc()
1497 mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset; in uvc_video_decode_isoc()
1500 urb->iso_frame_desc[i].actual_length); in uvc_video_decode_isoc()
1501 if (ret == -EAGAIN) in uvc_video_decode_isoc()
1503 } while (ret == -EAGAIN); in uvc_video_decode_isoc()
1512 urb->iso_frame_desc[i].actual_length - ret); in uvc_video_decode_isoc()
1516 urb->iso_frame_desc[i].actual_length); in uvc_video_decode_isoc()
1518 if (buf->state == UVC_BUF_STATE_READY) in uvc_video_decode_isoc()
1526 struct urb *urb = uvc_urb->urb; in uvc_video_decode_bulk()
1527 struct uvc_streaming *stream = uvc_urb->stream; in uvc_video_decode_bulk()
1529 int len, ret; in uvc_video_decode_bulk()
1535 if (urb->actual_length == 0 && stream->bulk.header_size == 0) in uvc_video_decode_bulk()
1538 mem = urb->transfer_buffer; in uvc_video_decode_bulk()
1539 len = urb->actual_length; in uvc_video_decode_bulk()
1540 stream->bulk.payload_size += len; in uvc_video_decode_bulk()
1546 if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) { in uvc_video_decode_bulk()
1549 if (ret == -EAGAIN) in uvc_video_decode_bulk()
1551 } while (ret == -EAGAIN); in uvc_video_decode_bulk()
1555 stream->bulk.skip_payload = 1; in uvc_video_decode_bulk()
1557 memcpy(stream->bulk.header, mem, ret); in uvc_video_decode_bulk()
1558 stream->bulk.header_size = ret; in uvc_video_decode_bulk()
1563 len -= ret; in uvc_video_decode_bulk()
1574 if (!stream->bulk.skip_payload && buf != NULL) in uvc_video_decode_bulk()
1581 if (urb->actual_length < urb->transfer_buffer_length || in uvc_video_decode_bulk()
1582 stream->bulk.payload_size >= stream->bulk.max_payload_size) { in uvc_video_decode_bulk()
1583 if (!stream->bulk.skip_payload && buf != NULL) { in uvc_video_decode_bulk()
1584 uvc_video_decode_end(stream, buf, stream->bulk.header, in uvc_video_decode_bulk()
1585 stream->bulk.payload_size); in uvc_video_decode_bulk()
1586 if (buf->state == UVC_BUF_STATE_READY) in uvc_video_decode_bulk()
1590 stream->bulk.header_size = 0; in uvc_video_decode_bulk()
1591 stream->bulk.skip_payload = 0; in uvc_video_decode_bulk()
1592 stream->bulk.payload_size = 0; in uvc_video_decode_bulk()
1599 struct urb *urb = uvc_urb->urb; in uvc_video_encode_bulk()
1600 struct uvc_streaming *stream = uvc_urb->stream; in uvc_video_encode_bulk()
1602 u8 *mem = urb->transfer_buffer; in uvc_video_encode_bulk()
1603 int len = stream->urb_size, ret; in uvc_video_encode_bulk()
1606 urb->transfer_buffer_length = 0; in uvc_video_encode_bulk()
1611 if (stream->bulk.header_size == 0) { in uvc_video_encode_bulk()
1613 stream->bulk.header_size = ret; in uvc_video_encode_bulk()
1614 stream->bulk.payload_size += ret; in uvc_video_encode_bulk()
1616 len -= ret; in uvc_video_encode_bulk()
1622 stream->bulk.payload_size += ret; in uvc_video_encode_bulk()
1623 len -= ret; in uvc_video_encode_bulk()
1625 if (buf->bytesused == stream->queue.buf_used || in uvc_video_encode_bulk()
1626 stream->bulk.payload_size == stream->bulk.max_payload_size) { in uvc_video_encode_bulk()
1627 if (buf->bytesused == stream->queue.buf_used) { in uvc_video_encode_bulk()
1628 stream->queue.buf_used = 0; in uvc_video_encode_bulk()
1629 buf->state = UVC_BUF_STATE_READY; in uvc_video_encode_bulk()
1630 buf->buf.sequence = ++stream->sequence; in uvc_video_encode_bulk()
1631 uvc_queue_next_buffer(&stream->queue, buf); in uvc_video_encode_bulk()
1632 stream->last_fid ^= UVC_STREAM_FID; in uvc_video_encode_bulk()
1635 stream->bulk.header_size = 0; in uvc_video_encode_bulk()
1636 stream->bulk.payload_size = 0; in uvc_video_encode_bulk()
1639 urb->transfer_buffer_length = stream->urb_size - len; in uvc_video_encode_bulk()
1644 struct uvc_urb *uvc_urb = urb->context; in uvc_video_complete()
1645 struct uvc_streaming *stream = uvc_urb->stream; in uvc_video_complete()
1646 struct uvc_video_queue *queue = &stream->queue; in uvc_video_complete()
1647 struct uvc_video_queue *qmeta = &stream->meta.queue; in uvc_video_complete()
1648 struct vb2_queue *vb2_qmeta = stream->meta.vdev.queue; in uvc_video_complete()
1652 int ret; in uvc_video_complete()
1654 switch (urb->status) { in uvc_video_complete()
1659 dev_warn(&stream->intf->dev, in uvc_video_complete()
1660 "Non-zero status (%d) in video completion handler.\n", in uvc_video_complete()
1661 urb->status); in uvc_video_complete()
1663 case -ENOENT: /* usb_poison_urb() called. */ in uvc_video_complete()
1664 if (stream->frozen) in uvc_video_complete()
1667 case -ECONNRESET: /* usb_unlink_urb() called. */ in uvc_video_complete()
1668 case -ESHUTDOWN: /* The endpoint is being disabled. */ in uvc_video_complete()
1669 uvc_queue_cancel(queue, urb->status == -ESHUTDOWN); in uvc_video_complete()
1671 uvc_queue_cancel(qmeta, urb->status == -ESHUTDOWN); in uvc_video_complete()
1678 spin_lock_irqsave(&qmeta->irqlock, flags); in uvc_video_complete()
1679 if (!list_empty(&qmeta->irqqueue)) in uvc_video_complete()
1680 buf_meta = list_first_entry(&qmeta->irqqueue, in uvc_video_complete()
1682 spin_unlock_irqrestore(&qmeta->irqlock, flags); in uvc_video_complete()
1685 /* Re-initialise the URB async work. */ in uvc_video_complete()
1686 uvc_urb->async_operations = 0; in uvc_video_complete()
1689 dma_sync_sgtable_for_cpu(uvc_stream_to_dmadev(uvc_urb->stream), in uvc_video_complete()
1690 uvc_urb->sgt, uvc_stream_dir(stream)); in uvc_video_complete()
1691 invalidate_kernel_vmap_range(uvc_urb->buffer, in uvc_video_complete()
1692 uvc_urb->stream->urb_size); in uvc_video_complete()
1698 stream->decode(uvc_urb, buf, buf_meta); in uvc_video_complete()
1701 if (!uvc_urb->async_operations) { in uvc_video_complete()
1704 dev_err(&stream->intf->dev, in uvc_video_complete()
1709 queue_work(stream->async_wq, &uvc_urb->work); in uvc_video_complete()
1721 if (!uvc_urb->buffer) in uvc_free_urb_buffers()
1724 dma_vunmap_noncontiguous(dma_dev, uvc_urb->buffer); in uvc_free_urb_buffers()
1725 dma_free_noncontiguous(dma_dev, stream->urb_size, uvc_urb->sgt, in uvc_free_urb_buffers()
1728 uvc_urb->buffer = NULL; in uvc_free_urb_buffers()
1729 uvc_urb->sgt = NULL; in uvc_free_urb_buffers()
1732 stream->urb_size = 0; in uvc_free_urb_buffers()
1740 uvc_urb->sgt = dma_alloc_noncontiguous(dma_dev, stream->urb_size, in uvc_alloc_urb_buffer()
1743 if (!uvc_urb->sgt) in uvc_alloc_urb_buffer()
1745 uvc_urb->dma = uvc_urb->sgt->sgl->dma_address; in uvc_alloc_urb_buffer()
1747 uvc_urb->buffer = dma_vmap_noncontiguous(dma_dev, stream->urb_size, in uvc_alloc_urb_buffer()
1748 uvc_urb->sgt); in uvc_alloc_urb_buffer()
1749 if (!uvc_urb->buffer) { in uvc_alloc_urb_buffer()
1750 dma_free_noncontiguous(dma_dev, stream->urb_size, in uvc_alloc_urb_buffer()
1751 uvc_urb->sgt, in uvc_alloc_urb_buffer()
1753 uvc_urb->sgt = NULL; in uvc_alloc_urb_buffer()
1771 static int uvc_alloc_urb_buffers(struct uvc_streaming *stream, in uvc_alloc_urb_buffers()
1772 unsigned int size, unsigned int psize, gfp_t gfp_flags) in uvc_alloc_urb_buffers()
1774 unsigned int npackets; in uvc_alloc_urb_buffers()
1775 unsigned int i; in uvc_alloc_urb_buffers()
1778 if (stream->urb_size) in uvc_alloc_urb_buffers()
1779 return stream->urb_size / psize; in uvc_alloc_urb_buffers()
1791 stream->urb_size = psize * npackets; in uvc_alloc_urb_buffers()
1794 struct uvc_urb *uvc_urb = &stream->uvc_urb[i]; in uvc_alloc_urb_buffers()
1801 uvc_urb->stream = stream; in uvc_alloc_urb_buffers()
1805 uvc_dbg(stream->dev, VIDEO, in uvc_alloc_urb_buffers()
1812 uvc_dbg(stream->dev, VIDEO, in uvc_alloc_urb_buffers()
1822 int free_buffers) in uvc_video_stop_transfer()
1834 usb_poison_urb(uvc_urb->urb); in uvc_video_stop_transfer()
1836 flush_workqueue(stream->async_wq); in uvc_video_stop_transfer()
1839 usb_free_urb(uvc_urb->urb); in uvc_video_stop_transfer()
1840 uvc_urb->urb = NULL; in uvc_video_stop_transfer()
1854 switch (dev->speed) { in uvc_endpoint_max_bpi()
1857 return le16_to_cpu(ep->ss_ep_comp.wBytesPerInterval); in uvc_endpoint_max_bpi()
1859 psize = usb_endpoint_maxp(&ep->desc); in uvc_endpoint_max_bpi()
1860 psize *= usb_endpoint_maxp_mult(&ep->desc); in uvc_endpoint_max_bpi()
1869 static int uvc_init_video_isoc(struct uvc_streaming *stream, in uvc_init_video_isoc()
1874 unsigned int npackets, i; in uvc_init_video_isoc()
1878 psize = uvc_endpoint_max_bpi(stream->dev->udev, ep); in uvc_init_video_isoc()
1879 size = stream->ctrl.dwMaxVideoFrameSize; in uvc_init_video_isoc()
1883 return -ENOMEM; in uvc_init_video_isoc()
1891 return -ENOMEM; in uvc_init_video_isoc()
1894 urb->dev = stream->dev->udev; in uvc_init_video_isoc()
1895 urb->context = uvc_urb; in uvc_init_video_isoc()
1896 urb->pipe = usb_rcvisocpipe(stream->dev->udev, in uvc_init_video_isoc()
1897 ep->desc.bEndpointAddress); in uvc_init_video_isoc()
1898 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; in uvc_init_video_isoc()
1899 urb->transfer_dma = uvc_urb->dma; in uvc_init_video_isoc()
1900 urb->interval = ep->desc.bInterval; in uvc_init_video_isoc()
1901 urb->transfer_buffer = uvc_urb->buffer; in uvc_init_video_isoc()
1902 urb->complete = uvc_video_complete; in uvc_init_video_isoc()
1903 urb->number_of_packets = npackets; in uvc_init_video_isoc()
1904 urb->transfer_buffer_length = size; in uvc_init_video_isoc()
1907 urb->iso_frame_desc[i].offset = i * psize; in uvc_init_video_isoc()
1908 urb->iso_frame_desc[i].length = psize; in uvc_init_video_isoc()
1911 uvc_urb->urb = urb; in uvc_init_video_isoc()
1921 static int uvc_init_video_bulk(struct uvc_streaming *stream, in uvc_init_video_bulk()
1926 unsigned int npackets, pipe; in uvc_init_video_bulk()
1930 psize = usb_endpoint_maxp(&ep->desc); in uvc_init_video_bulk()
1931 size = stream->ctrl.dwMaxPayloadTransferSize; in uvc_init_video_bulk()
1932 stream->bulk.max_payload_size = size; in uvc_init_video_bulk()
1936 return -ENOMEM; in uvc_init_video_bulk()
1940 if (usb_endpoint_dir_in(&ep->desc)) in uvc_init_video_bulk()
1941 pipe = usb_rcvbulkpipe(stream->dev->udev, in uvc_init_video_bulk()
1942 ep->desc.bEndpointAddress); in uvc_init_video_bulk()
1944 pipe = usb_sndbulkpipe(stream->dev->udev, in uvc_init_video_bulk()
1945 ep->desc.bEndpointAddress); in uvc_init_video_bulk()
1947 if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) in uvc_init_video_bulk()
1954 return -ENOMEM; in uvc_init_video_bulk()
1957 usb_fill_bulk_urb(urb, stream->dev->udev, pipe, uvc_urb->buffer, in uvc_init_video_bulk()
1959 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; in uvc_init_video_bulk()
1960 urb->transfer_dma = uvc_urb->dma; in uvc_init_video_bulk()
1962 uvc_urb->urb = urb; in uvc_init_video_bulk()
1971 static int uvc_video_start_transfer(struct uvc_streaming *stream, in uvc_video_start_transfer()
1974 struct usb_interface *intf = stream->intf; in uvc_video_start_transfer()
1977 unsigned int i; in uvc_video_start_transfer()
1978 int ret; in uvc_video_start_transfer()
1980 stream->sequence = -1; in uvc_video_start_transfer()
1981 stream->last_fid = -1; in uvc_video_start_transfer()
1982 stream->bulk.header_size = 0; in uvc_video_start_transfer()
1983 stream->bulk.skip_payload = 0; in uvc_video_start_transfer()
1984 stream->bulk.payload_size = 0; in uvc_video_start_transfer()
1988 if (intf->num_altsetting > 1) { in uvc_video_start_transfer()
1990 unsigned int best_psize = UINT_MAX; in uvc_video_start_transfer()
1991 unsigned int bandwidth; in uvc_video_start_transfer()
1992 unsigned int altsetting; in uvc_video_start_transfer()
1993 int intfnum = stream->intfnum; in uvc_video_start_transfer()
1996 bandwidth = stream->ctrl.dwMaxPayloadTransferSize; in uvc_video_start_transfer()
1999 uvc_dbg(stream->dev, VIDEO, in uvc_video_start_transfer()
2003 uvc_dbg(stream->dev, VIDEO, in uvc_video_start_transfer()
2008 for (i = 0; i < intf->num_altsetting; ++i) { in uvc_video_start_transfer()
2010 unsigned int psize; in uvc_video_start_transfer()
2012 alts = &intf->altsetting[i]; in uvc_video_start_transfer()
2014 stream->header.bEndpointAddress); in uvc_video_start_transfer()
2019 psize = uvc_endpoint_max_bpi(stream->dev->udev, ep); in uvc_video_start_transfer()
2021 altsetting = alts->desc.bAlternateSetting; in uvc_video_start_transfer()
2028 uvc_dbg(stream->dev, VIDEO, in uvc_video_start_transfer()
2030 return -EIO; in uvc_video_start_transfer()
2033 uvc_dbg(stream->dev, VIDEO, in uvc_video_start_transfer()
2042 if (stream->dev->quirks & UVC_QUIRK_WAKE_AUTOSUSPEND) { in uvc_video_start_transfer()
2043 usb_set_interface(stream->dev->udev, intfnum, in uvc_video_start_transfer()
2045 usb_set_interface(stream->dev->udev, intfnum, 0); in uvc_video_start_transfer()
2048 ret = usb_set_interface(stream->dev->udev, intfnum, altsetting); in uvc_video_start_transfer()
2055 ep = uvc_find_endpoint(&intf->altsetting[0], in uvc_video_start_transfer()
2056 stream->header.bEndpointAddress); in uvc_video_start_transfer()
2058 return -EIO; in uvc_video_start_transfer()
2060 /* Reject broken descriptors. */ in uvc_video_start_transfer()
2061 if (usb_endpoint_maxp(&ep->desc) == 0) in uvc_video_start_transfer()
2062 return -EIO; in uvc_video_start_transfer()
2074 dev_err(&stream->intf->dev, in uvc_video_start_transfer()
2086 if (stream->dev->quirks & UVC_QUIRK_RESTORE_CTRLS_ON_INIT) in uvc_video_start_transfer()
2087 uvc_ctrl_restore_values(stream->dev); in uvc_video_start_transfer()
2092 /* --------------------------------------------------------------------------
2103 int uvc_video_suspend(struct uvc_streaming *stream) in uvc_video_suspend()
2105 if (!uvc_queue_streaming(&stream->queue)) in uvc_video_suspend()
2108 stream->frozen = 1; in uvc_video_suspend()
2110 usb_set_interface(stream->dev->udev, stream->intfnum, 0); in uvc_video_suspend()
2122 int uvc_video_resume(struct uvc_streaming *stream, int reset) in uvc_video_resume()
2124 int ret; in uvc_video_resume()
2133 usb_set_interface(stream->dev->udev, stream->intfnum, 0); in uvc_video_resume()
2135 stream->frozen = 0; in uvc_video_resume()
2137 uvc_video_clock_reset(&stream->clock); in uvc_video_resume()
2139 if (!uvc_queue_streaming(&stream->queue)) in uvc_video_resume()
2142 ret = uvc_commit_video(stream, &stream->ctrl); in uvc_video_resume()
2149 /* ------------------------------------------------------------------------
2163 int uvc_video_init(struct uvc_streaming *stream) in uvc_video_init()
2165 struct uvc_streaming_control *probe = &stream->ctrl; in uvc_video_init()
2169 unsigned int i; in uvc_video_init()
2170 int ret; in uvc_video_init()
2172 if (stream->nformats == 0) { in uvc_video_init()
2173 dev_info(&stream->intf->dev, in uvc_video_init()
2175 return -EINVAL; in uvc_video_init()
2178 atomic_set(&stream->active, 0); in uvc_video_init()
2186 usb_set_interface(stream->dev->udev, stream->intfnum, 0); in uvc_video_init()
2212 if (ret == -EPROTO && in uvc_video_init()
2213 usb_match_one_id(stream->dev->intf, &elgato_cam_link_4k)) { in uvc_video_init()
2214 dev_err(&stream->intf->dev, "Elgato Cam Link 4K firmware crash detected\n"); in uvc_video_init()
2215 dev_err(&stream->intf->dev, "Resetting the device, unplug and replug to recover\n"); in uvc_video_init()
2216 usb_reset_device(stream->dev->udev); in uvc_video_init()
2226 for (i = stream->nformats; i > 0; --i) { in uvc_video_init()
2227 format = &stream->formats[i-1]; in uvc_video_init()
2228 if (format->index == probe->bFormatIndex) in uvc_video_init()
2232 if (format->nframes == 0) { in uvc_video_init()
2233 dev_info(&stream->intf->dev, in uvc_video_init()
2235 return -EINVAL; in uvc_video_init()
2239 * Zero bFrameIndex might be correct. Stream-based formats (including in uvc_video_init()
2240 * MPEG-2 TS and DV) do not support frames but have a dummy frame in uvc_video_init()
2244 for (i = format->nframes; i > 0; --i) { in uvc_video_init()
2245 frame = &format->frames[i-1]; in uvc_video_init()
2246 if (frame->bFrameIndex == probe->bFrameIndex) in uvc_video_init()
2250 probe->bFormatIndex = format->index; in uvc_video_init()
2251 probe->bFrameIndex = frame->bFrameIndex; in uvc_video_init()
2253 stream->def_format = format; in uvc_video_init()
2254 stream->cur_format = format; in uvc_video_init()
2255 stream->cur_frame = frame; in uvc_video_init()
2258 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { in uvc_video_init()
2259 if (stream->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT) in uvc_video_init()
2260 stream->decode = uvc_video_decode_isight; in uvc_video_init()
2261 else if (stream->intf->num_altsetting > 1) in uvc_video_init()
2262 stream->decode = uvc_video_decode_isoc; in uvc_video_init()
2264 stream->decode = uvc_video_decode_bulk; in uvc_video_init()
2266 if (stream->intf->num_altsetting == 1) in uvc_video_init()
2267 stream->decode = uvc_video_encode_bulk; in uvc_video_init()
2269 dev_info(&stream->intf->dev, in uvc_video_init()
2271 return -EINVAL; in uvc_video_init()
2277 INIT_WORK(&uvc_urb->work, uvc_video_copy_data_work); in uvc_video_init()
2282 int uvc_video_start_streaming(struct uvc_streaming *stream) in uvc_video_start_streaming()
2284 int ret; in uvc_video_start_streaming()
2286 ret = uvc_video_clock_init(&stream->clock); in uvc_video_start_streaming()
2291 ret = uvc_commit_video(stream, &stream->ctrl); in uvc_video_start_streaming()
2302 usb_set_interface(stream->dev->udev, stream->intfnum, 0); in uvc_video_start_streaming()
2304 uvc_video_clock_cleanup(&stream->clock); in uvc_video_start_streaming()
2313 if (stream->intf->num_altsetting > 1) { in uvc_video_stop_streaming()
2314 usb_set_interface(stream->dev->udev, stream->intfnum, 0); in uvc_video_stop_streaming()
2317 * UVC doesn't specify how to inform a bulk-based device in uvc_video_stop_streaming()
2322 unsigned int epnum = stream->header.bEndpointAddress in uvc_video_stop_streaming()
2324 unsigned int dir = stream->header.bEndpointAddress in uvc_video_stop_streaming()
2326 unsigned int pipe; in uvc_video_stop_streaming()
2328 pipe = usb_sndbulkpipe(stream->dev->udev, epnum) | dir; in uvc_video_stop_streaming()
2329 usb_clear_halt(stream->dev->udev, pipe); in uvc_video_stop_streaming()
2332 uvc_video_clock_cleanup(&stream->clock); in uvc_video_stop_streaming()