Lines Matching +full:frame +full:- +full:buffer
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * uvc_video.c -- USB Video Class driver - Video handling
5 * Copyright (C) 2005-2010
20 #include <media/v4l2-common.h>
24 /* ------------------------------------------------------------------------
35 pipe = (query & 0x80) ? usb_rcvctrlpipe(dev->udev, 0) in __uvc_query_ctrl()
36 : usb_sndctrlpipe(dev->udev, 0); in __uvc_query_ctrl()
39 return usb_control_msg(dev->udev, pipe, query, type, cs << 8, in __uvc_query_ctrl()
83 if (ret != -EPIPE) in uvc_query_ctrl()
96 return ret < 0 ? ret : -EPIPE; in uvc_query_ctrl()
102 /* Cannot happen - we received a STALL */ in uvc_query_ctrl()
103 return -EPIPE; in uvc_query_ctrl()
105 return -EBUSY; in uvc_query_ctrl()
107 return -EILSEQ; in uvc_query_ctrl()
109 return -EREMOTE; in uvc_query_ctrl()
111 return -ERANGE; in uvc_query_ctrl()
116 return -EINVAL; in uvc_query_ctrl()
121 return -EPIPE; in uvc_query_ctrl()
128 struct uvc_frame *frame = NULL; in uvc_fixup_video_ctrl() local
131 for (i = 0; i < stream->nformats; ++i) { in uvc_fixup_video_ctrl()
132 if (stream->format[i].index == ctrl->bFormatIndex) { in uvc_fixup_video_ctrl()
133 format = &stream->format[i]; in uvc_fixup_video_ctrl()
141 for (i = 0; i < format->nframes; ++i) { in uvc_fixup_video_ctrl()
142 if (format->frame[i].bFrameIndex == ctrl->bFrameIndex) { in uvc_fixup_video_ctrl()
143 frame = &format->frame[i]; in uvc_fixup_video_ctrl()
148 if (frame == NULL) in uvc_fixup_video_ctrl()
151 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) || in uvc_fixup_video_ctrl()
152 (ctrl->dwMaxVideoFrameSize == 0 && in uvc_fixup_video_ctrl()
153 stream->dev->uvc_version < 0x0110)) in uvc_fixup_video_ctrl()
154 ctrl->dwMaxVideoFrameSize = in uvc_fixup_video_ctrl()
155 frame->dwMaxVideoFrameBufferSize; in uvc_fixup_video_ctrl()
157 /* The "TOSHIBA Web Camera - 5M" Chicony device (04f2:b50b) seems to in uvc_fixup_video_ctrl()
158 * compute the bandwidth on 16 bits and erroneously sign-extend it to in uvc_fixup_video_ctrl()
162 if ((ctrl->dwMaxPayloadTransferSize & 0xffff0000) == 0xffff0000) in uvc_fixup_video_ctrl()
163 ctrl->dwMaxPayloadTransferSize &= ~0xffff0000; in uvc_fixup_video_ctrl()
165 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) && in uvc_fixup_video_ctrl()
166 stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH && in uvc_fixup_video_ctrl()
167 stream->intf->num_altsetting > 1) { in uvc_fixup_video_ctrl()
171 interval = (ctrl->dwFrameInterval > 100000) in uvc_fixup_video_ctrl()
172 ? ctrl->dwFrameInterval in uvc_fixup_video_ctrl()
173 : frame->dwFrameInterval[0]; in uvc_fixup_video_ctrl()
175 /* Compute a bandwidth estimation by multiplying the frame in uvc_fixup_video_ctrl()
177 * result by the number of USB frames (or micro-frames for in uvc_fixup_video_ctrl()
178 * high-speed devices) per second and add the UVC header size in uvc_fixup_video_ctrl()
181 bandwidth = frame->wWidth * frame->wHeight / 8 * format->bpp; in uvc_fixup_video_ctrl()
184 if (stream->dev->udev->speed == USB_SPEED_HIGH) in uvc_fixup_video_ctrl()
197 ctrl->dwMaxPayloadTransferSize = bandwidth; in uvc_fixup_video_ctrl()
207 if (stream->dev->uvc_version < 0x0110) in uvc_video_ctrl_size()
209 else if (stream->dev->uvc_version < 0x0150) in uvc_video_ctrl_size()
222 if ((stream->dev->quirks & UVC_QUIRK_PROBE_DEF) && in uvc_get_video_ctrl()
224 return -EIO; in uvc_get_video_ctrl()
228 return -ENOMEM; in uvc_get_video_ctrl()
230 ret = __uvc_query_ctrl(stream->dev, query, 0, stream->intfnum, in uvc_get_video_ctrl()
239 uvc_warn_once(stream->dev, UVC_WARN_MINMAX, "UVC non " in uvc_get_video_ctrl()
240 "compliance - GET_MIN/MAX(PROBE) incorrectly " in uvc_get_video_ctrl()
243 ctrl->wCompQuality = le16_to_cpup((__le16 *)data); in uvc_get_video_ctrl()
251 uvc_warn_once(stream->dev, UVC_WARN_PROBE_DEF, "UVC non " in uvc_get_video_ctrl()
252 "compliance - GET_DEF(PROBE) not supported. " in uvc_get_video_ctrl()
254 ret = -EIO; in uvc_get_video_ctrl()
260 ret = -EIO; in uvc_get_video_ctrl()
264 ctrl->bmHint = le16_to_cpup((__le16 *)&data[0]); in uvc_get_video_ctrl()
265 ctrl->bFormatIndex = data[2]; in uvc_get_video_ctrl()
266 ctrl->bFrameIndex = data[3]; in uvc_get_video_ctrl()
267 ctrl->dwFrameInterval = le32_to_cpup((__le32 *)&data[4]); in uvc_get_video_ctrl()
268 ctrl->wKeyFrameRate = le16_to_cpup((__le16 *)&data[8]); in uvc_get_video_ctrl()
269 ctrl->wPFrameRate = le16_to_cpup((__le16 *)&data[10]); in uvc_get_video_ctrl()
270 ctrl->wCompQuality = le16_to_cpup((__le16 *)&data[12]); in uvc_get_video_ctrl()
271 ctrl->wCompWindowSize = le16_to_cpup((__le16 *)&data[14]); in uvc_get_video_ctrl()
272 ctrl->wDelay = le16_to_cpup((__le16 *)&data[16]); in uvc_get_video_ctrl()
273 ctrl->dwMaxVideoFrameSize = get_unaligned_le32(&data[18]); in uvc_get_video_ctrl()
274 ctrl->dwMaxPayloadTransferSize = get_unaligned_le32(&data[22]); in uvc_get_video_ctrl()
277 ctrl->dwClockFrequency = get_unaligned_le32(&data[26]); in uvc_get_video_ctrl()
278 ctrl->bmFramingInfo = data[30]; in uvc_get_video_ctrl()
279 ctrl->bPreferedVersion = data[31]; in uvc_get_video_ctrl()
280 ctrl->bMinVersion = data[32]; in uvc_get_video_ctrl()
281 ctrl->bMaxVersion = data[33]; in uvc_get_video_ctrl()
283 ctrl->dwClockFrequency = stream->dev->clock_frequency; in uvc_get_video_ctrl()
284 ctrl->bmFramingInfo = 0; in uvc_get_video_ctrl()
285 ctrl->bPreferedVersion = 0; in uvc_get_video_ctrl()
286 ctrl->bMinVersion = 0; in uvc_get_video_ctrl()
287 ctrl->bMaxVersion = 0; in uvc_get_video_ctrl()
292 * format and frame descriptors. in uvc_get_video_ctrl()
311 return -ENOMEM; in uvc_set_video_ctrl()
313 *(__le16 *)&data[0] = cpu_to_le16(ctrl->bmHint); in uvc_set_video_ctrl()
314 data[2] = ctrl->bFormatIndex; in uvc_set_video_ctrl()
315 data[3] = ctrl->bFrameIndex; in uvc_set_video_ctrl()
316 *(__le32 *)&data[4] = cpu_to_le32(ctrl->dwFrameInterval); in uvc_set_video_ctrl()
317 *(__le16 *)&data[8] = cpu_to_le16(ctrl->wKeyFrameRate); in uvc_set_video_ctrl()
318 *(__le16 *)&data[10] = cpu_to_le16(ctrl->wPFrameRate); in uvc_set_video_ctrl()
319 *(__le16 *)&data[12] = cpu_to_le16(ctrl->wCompQuality); in uvc_set_video_ctrl()
320 *(__le16 *)&data[14] = cpu_to_le16(ctrl->wCompWindowSize); in uvc_set_video_ctrl()
321 *(__le16 *)&data[16] = cpu_to_le16(ctrl->wDelay); in uvc_set_video_ctrl()
322 put_unaligned_le32(ctrl->dwMaxVideoFrameSize, &data[18]); in uvc_set_video_ctrl()
323 put_unaligned_le32(ctrl->dwMaxPayloadTransferSize, &data[22]); in uvc_set_video_ctrl()
326 put_unaligned_le32(ctrl->dwClockFrequency, &data[26]); in uvc_set_video_ctrl()
327 data[30] = ctrl->bmFramingInfo; in uvc_set_video_ctrl()
328 data[31] = ctrl->bPreferedVersion; in uvc_set_video_ctrl()
329 data[32] = ctrl->bMinVersion; in uvc_set_video_ctrl()
330 data[33] = ctrl->bMaxVersion; in uvc_set_video_ctrl()
333 ret = __uvc_query_ctrl(stream->dev, UVC_SET_CUR, 0, stream->intfnum, in uvc_set_video_ctrl()
340 ret = -EIO; in uvc_set_video_ctrl()
367 if (!(stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX)) { in uvc_probe_video()
375 probe->wCompQuality = probe_max.wCompQuality; in uvc_probe_video()
386 if (stream->intf->num_altsetting == 1) in uvc_probe_video()
389 bandwidth = probe->dwMaxPayloadTransferSize; in uvc_probe_video()
390 if (bandwidth <= stream->maxpsize) in uvc_probe_video()
393 if (stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX) { in uvc_probe_video()
394 ret = -ENOSPC; in uvc_probe_video()
399 probe->wKeyFrameRate = probe_min.wKeyFrameRate; in uvc_probe_video()
400 probe->wPFrameRate = probe_min.wPFrameRate; in uvc_probe_video()
401 probe->wCompQuality = probe_max.wCompQuality; in uvc_probe_video()
402 probe->wCompWindowSize = probe_min.wCompWindowSize; in uvc_probe_video()
415 /* -----------------------------------------------------------------------------
465 * - store the frame PTS in the buffer structure in uvc_video_clock_decode()
466 * - if the SCR field is present, retrieve the host SOF counter and in uvc_video_clock_decode()
468 * in the ring buffer in uvc_video_clock_decode()
471 buf->pts = get_unaligned_le32(&data[2]); in uvc_video_clock_decode()
479 dev_sof = get_unaligned_le16(&data[header_size - 2]); in uvc_video_clock_decode()
480 if (dev_sof == stream->clock.last_sof) in uvc_video_clock_decode()
483 stream->clock.last_sof = dev_sof; in uvc_video_clock_decode()
485 host_sof = usb_get_current_frame_number(stream->dev->udev); in uvc_video_clock_decode()
489 * the USB frame number to keep their own frame counters as long as they in uvc_video_clock_decode()
490 * match the size and frequency of the frame number associated with USB in uvc_video_clock_decode()
507 if (stream->clock.sof_offset == (u16)-1) { in uvc_video_clock_decode()
508 u16 delta_sof = (host_sof - dev_sof) & 255; in uvc_video_clock_decode()
510 stream->clock.sof_offset = delta_sof; in uvc_video_clock_decode()
512 stream->clock.sof_offset = 0; in uvc_video_clock_decode()
515 dev_sof = (dev_sof + stream->clock.sof_offset) & 2047; in uvc_video_clock_decode()
517 spin_lock_irqsave(&stream->clock.lock, flags); in uvc_video_clock_decode()
519 sample = &stream->clock.samples[stream->clock.head]; in uvc_video_clock_decode()
520 sample->dev_stc = get_unaligned_le32(&data[header_size - 6]); in uvc_video_clock_decode()
521 sample->dev_sof = dev_sof; in uvc_video_clock_decode()
522 sample->host_sof = host_sof; in uvc_video_clock_decode()
523 sample->host_time = time; in uvc_video_clock_decode()
526 stream->clock.head = (stream->clock.head + 1) % stream->clock.size; in uvc_video_clock_decode()
528 if (stream->clock.count < stream->clock.size) in uvc_video_clock_decode()
529 stream->clock.count++; in uvc_video_clock_decode()
531 spin_unlock_irqrestore(&stream->clock.lock, flags); in uvc_video_clock_decode()
536 struct uvc_clock *clock = &stream->clock; in uvc_video_clock_reset()
538 clock->head = 0; in uvc_video_clock_reset()
539 clock->count = 0; in uvc_video_clock_reset()
540 clock->last_sof = -1; in uvc_video_clock_reset()
541 clock->sof_offset = -1; in uvc_video_clock_reset()
546 struct uvc_clock *clock = &stream->clock; in uvc_video_clock_init()
548 spin_lock_init(&clock->lock); in uvc_video_clock_init()
549 clock->size = 32; in uvc_video_clock_init()
551 clock->samples = kmalloc_array(clock->size, sizeof(*clock->samples), in uvc_video_clock_init()
553 if (clock->samples == NULL) in uvc_video_clock_init()
554 return -ENOMEM; in uvc_video_clock_init()
563 kfree(stream->clock.samples); in uvc_video_clock_cleanup()
564 stream->clock.samples = NULL; in uvc_video_clock_cleanup()
568 * uvc_video_clock_host_sof - Return the host SOF value for a clock sample
571 * cover the whole 11-bits SOF range (0-2047) but are limited to the HCI frame
575 * We thus need to recover the SOF value corresponding to the host frame number.
576 * As the device and host frame numbers are sampled in a short interval, the
578 * integer multiple of 256 caused by the host frame number limited precision.
581 * the high bits of the host frame counter and device SOF difference and add it
589 delta_sof = (sample->host_sof - sample->dev_sof) & 255; in uvc_video_clock_host_sof()
591 return (sample->dev_sof + delta_sof) & 2047; in uvc_video_clock_host_sof()
595 * uvc_video_clock_update - Update the buffer timestamp
597 * This function converts the buffer PTS timestamp to the host clock domain by
599 * buffer timestamp field.
615 * SOF = (SOF2 - SOF1) / (STC2 - STC1) * PTS
616 * + (SOF1 * STC2 - SOF2 * STC1) / (STC2 - STC1)
620 * SOF = ((SOF2 - SOF1) * PTS + SOF1 * STC2 - SOF2 * STC1) / (STC2 - STC1) (1)
625 * TS = ((TS2 - TS1) * PTS + TS1 * SOF2 - TS2 * SOF1) / (SOF2 - SOF1) (2)
659 struct uvc_clock *clock = &stream->clock; in uvc_video_clock_update()
679 if (!clock->samples) in uvc_video_clock_update()
682 spin_lock_irqsave(&clock->lock, flags); in uvc_video_clock_update()
684 if (clock->count < clock->size) in uvc_video_clock_update()
687 first = &clock->samples[clock->head]; in uvc_video_clock_update()
688 last = &clock->samples[(clock->head - 1) % clock->size]; in uvc_video_clock_update()
691 delta_stc = buf->pts - (1UL << 31); in uvc_video_clock_update()
692 x1 = first->dev_stc - delta_stc; in uvc_video_clock_update()
693 x2 = last->dev_stc - delta_stc; in uvc_video_clock_update()
697 y1 = (first->dev_sof + 2048) << 16; in uvc_video_clock_update()
698 y2 = (last->dev_sof + 2048) << 16; in uvc_video_clock_update()
702 y = (u64)(y2 - y1) * (1ULL << 31) + (u64)y1 * (u64)x2 in uvc_video_clock_update()
703 - (u64)y2 * (u64)x1; in uvc_video_clock_update()
704 y = div_u64(y, x2 - x1); in uvc_video_clock_update()
710 stream->dev->name, buf->pts, in uvc_video_clock_update()
713 x1, x2, y1, y2, clock->sof_offset); in uvc_video_clock_update()
724 y2 = (u32)ktime_to_ns(ktime_sub(last->host_time, first->host_time)) + y1; in uvc_video_clock_update()
732 if (mean - (1024 << 16) > sof) in uvc_video_clock_update()
735 sof -= 2048 << 16; in uvc_video_clock_update()
737 y = (u64)(y2 - y1) * (u64)sof + (u64)y1 * (u64)x2 in uvc_video_clock_update()
738 - (u64)y2 * (u64)x1; in uvc_video_clock_update()
739 y = div_u64(y, x2 - x1); in uvc_video_clock_update()
741 timestamp = ktime_to_ns(first->host_time) + y - y1; in uvc_video_clock_update()
745 stream->dev->name, in uvc_video_clock_update()
747 y, timestamp, vbuf->vb2_buf.timestamp, in uvc_video_clock_update()
748 x1, first->host_sof, first->dev_sof, in uvc_video_clock_update()
749 x2, last->host_sof, last->dev_sof, y1, y2); in uvc_video_clock_update()
751 /* Update the V4L2 buffer. */ in uvc_video_clock_update()
752 vbuf->vb2_buf.timestamp = timestamp; in uvc_video_clock_update()
755 spin_unlock_irqrestore(&clock->lock, flags); in uvc_video_clock_update()
758 /* ------------------------------------------------------------------------
772 if (stream->stats.stream.nb_frames == 0 && in uvc_video_stats_decode()
773 stream->stats.frame.nb_packets == 0) in uvc_video_stats_decode()
774 stream->stats.stream.start_ts = ktime_get(); in uvc_video_stats_decode()
797 stream->stats.frame.nb_invalid++; in uvc_video_stats_decode()
806 scr_stc = get_unaligned_le32(&data[header_size - 6]); in uvc_video_stats_decode()
807 scr_sof = get_unaligned_le16(&data[header_size - 2]); in uvc_video_stats_decode()
810 /* Is PTS constant through the whole frame ? */ in uvc_video_stats_decode()
811 if (has_pts && stream->stats.frame.nb_pts) { in uvc_video_stats_decode()
812 if (stream->stats.frame.pts != pts) { in uvc_video_stats_decode()
813 stream->stats.frame.nb_pts_diffs++; in uvc_video_stats_decode()
814 stream->stats.frame.last_pts_diff = in uvc_video_stats_decode()
815 stream->stats.frame.nb_packets; in uvc_video_stats_decode()
820 stream->stats.frame.nb_pts++; in uvc_video_stats_decode()
821 stream->stats.frame.pts = pts; in uvc_video_stats_decode()
824 /* Do all frames have a PTS in their first non-empty packet, or before in uvc_video_stats_decode()
827 if (stream->stats.frame.size == 0) { in uvc_video_stats_decode()
829 stream->stats.frame.has_initial_pts = has_pts; in uvc_video_stats_decode()
831 stream->stats.frame.has_early_pts = true; in uvc_video_stats_decode()
834 /* Do the SCR.STC and SCR.SOF fields vary through the frame ? */ in uvc_video_stats_decode()
835 if (has_scr && stream->stats.frame.nb_scr) { in uvc_video_stats_decode()
836 if (stream->stats.frame.scr_stc != scr_stc) in uvc_video_stats_decode()
837 stream->stats.frame.nb_scr_diffs++; in uvc_video_stats_decode()
842 if (stream->stats.stream.nb_frames > 0 || in uvc_video_stats_decode()
843 stream->stats.frame.nb_scr > 0) in uvc_video_stats_decode()
844 stream->stats.stream.scr_sof_count += in uvc_video_stats_decode()
845 (scr_sof - stream->stats.stream.scr_sof) % 2048; in uvc_video_stats_decode()
846 stream->stats.stream.scr_sof = scr_sof; in uvc_video_stats_decode()
848 stream->stats.frame.nb_scr++; in uvc_video_stats_decode()
849 stream->stats.frame.scr_stc = scr_stc; in uvc_video_stats_decode()
850 stream->stats.frame.scr_sof = scr_sof; in uvc_video_stats_decode()
852 if (scr_sof < stream->stats.stream.min_sof) in uvc_video_stats_decode()
853 stream->stats.stream.min_sof = scr_sof; in uvc_video_stats_decode()
854 if (scr_sof > stream->stats.stream.max_sof) in uvc_video_stats_decode()
855 stream->stats.stream.max_sof = scr_sof; in uvc_video_stats_decode()
858 /* Record the first non-empty packet number. */ in uvc_video_stats_decode()
859 if (stream->stats.frame.size == 0 && len > header_size) in uvc_video_stats_decode()
860 stream->stats.frame.first_data = stream->stats.frame.nb_packets; in uvc_video_stats_decode()
862 /* Update the frame size. */ in uvc_video_stats_decode()
863 stream->stats.frame.size += len - header_size; in uvc_video_stats_decode()
866 stream->stats.frame.nb_packets++; in uvc_video_stats_decode()
868 stream->stats.frame.nb_empty++; in uvc_video_stats_decode()
871 stream->stats.frame.nb_errors++; in uvc_video_stats_decode()
876 struct uvc_stats_frame *frame = &stream->stats.frame; in uvc_video_stats_update() local
878 uvc_trace(UVC_TRACE_STATS, "frame %u stats: %u/%u/%u packets, " in uvc_video_stats_update()
881 stream->sequence, frame->first_data, in uvc_video_stats_update()
882 frame->nb_packets - frame->nb_empty, frame->nb_packets, in uvc_video_stats_update()
883 frame->nb_pts_diffs, frame->last_pts_diff, frame->nb_pts, in uvc_video_stats_update()
884 frame->has_early_pts ? "" : "!", in uvc_video_stats_update()
885 frame->has_initial_pts ? "" : "!", in uvc_video_stats_update()
886 frame->nb_scr_diffs, frame->nb_scr, in uvc_video_stats_update()
887 frame->pts, frame->scr_stc, frame->scr_sof); in uvc_video_stats_update()
889 stream->stats.stream.nb_frames++; in uvc_video_stats_update()
890 stream->stats.stream.nb_packets += stream->stats.frame.nb_packets; in uvc_video_stats_update()
891 stream->stats.stream.nb_empty += stream->stats.frame.nb_empty; in uvc_video_stats_update()
892 stream->stats.stream.nb_errors += stream->stats.frame.nb_errors; in uvc_video_stats_update()
893 stream->stats.stream.nb_invalid += stream->stats.frame.nb_invalid; in uvc_video_stats_update()
895 if (frame->has_early_pts) in uvc_video_stats_update()
896 stream->stats.stream.nb_pts_early++; in uvc_video_stats_update()
897 if (frame->has_initial_pts) in uvc_video_stats_update()
898 stream->stats.stream.nb_pts_initial++; in uvc_video_stats_update()
899 if (frame->last_pts_diff <= frame->first_data) in uvc_video_stats_update()
900 stream->stats.stream.nb_pts_constant++; in uvc_video_stats_update()
901 if (frame->nb_scr >= frame->nb_packets - frame->nb_empty) in uvc_video_stats_update()
902 stream->stats.stream.nb_scr_count_ok++; in uvc_video_stats_update()
903 if (frame->nb_scr_diffs + 1 == frame->nb_scr) in uvc_video_stats_update()
904 stream->stats.stream.nb_scr_diffs_ok++; in uvc_video_stats_update()
906 memset(&stream->stats.frame, 0, sizeof(stream->stats.frame)); in uvc_video_stats_update()
919 duration = ktime_ms_delta(stream->stats.stream.stop_ts, in uvc_video_stats_dump()
920 stream->stats.stream.start_ts); in uvc_video_stats_dump()
922 scr_sof_freq = stream->stats.stream.scr_sof_count * 1000 in uvc_video_stats_dump()
927 count += scnprintf(buf + count, size - count, in uvc_video_stats_dump()
930 stream->stats.stream.nb_frames, in uvc_video_stats_dump()
931 stream->stats.stream.nb_packets, in uvc_video_stats_dump()
932 stream->stats.stream.nb_empty, in uvc_video_stats_dump()
933 stream->stats.stream.nb_errors, in uvc_video_stats_dump()
934 stream->stats.stream.nb_invalid); in uvc_video_stats_dump()
935 count += scnprintf(buf + count, size - count, in uvc_video_stats_dump()
937 stream->stats.stream.nb_pts_early, in uvc_video_stats_dump()
938 stream->stats.stream.nb_pts_initial, in uvc_video_stats_dump()
939 stream->stats.stream.nb_pts_constant); in uvc_video_stats_dump()
940 count += scnprintf(buf + count, size - count, in uvc_video_stats_dump()
942 stream->stats.stream.nb_scr_count_ok, in uvc_video_stats_dump()
943 stream->stats.stream.nb_scr_diffs_ok); in uvc_video_stats_dump()
944 count += scnprintf(buf + count, size - count, in uvc_video_stats_dump()
946 stream->stats.stream.min_sof, in uvc_video_stats_dump()
947 stream->stats.stream.max_sof, in uvc_video_stats_dump()
955 memset(&stream->stats, 0, sizeof(stream->stats)); in uvc_video_stats_start()
956 stream->stats.stream.min_sof = 2048; in uvc_video_stats_start()
961 stream->stats.stream.stop_ts = ktime_get(); in uvc_video_stats_stop()
964 /* ------------------------------------------------------------------------
976 * - EAGAIN informs the caller that the current video buffer should be marked
978 * and a new video buffer. This is used when end of frame conditions can be
979 * reliably detected at the beginning of the next frame only.
981 * If an error other than -EAGAIN is returned, the caller will drop the current
983 * made until the next payload. -ENODATA can be used to drop the current
987 * data to the video buffer.
999 * when no video buffer is available, uvc_video_decode_start must be prepared
1001 * uvc_video_decode_end will never be called with a NULL buffer.
1009 * - packet must be at least 2 bytes long in uvc_video_decode_start()
1010 * - bHeaderLength value must be at least 2 bytes (see above) in uvc_video_decode_start()
1011 * - bHeaderLength value can't be larger than the packet size. in uvc_video_decode_start()
1014 stream->stats.frame.nb_invalid++; in uvc_video_decode_start()
1015 return -EINVAL; in uvc_video_decode_start()
1020 /* Increase the sequence number regardless of any buffer states, so in uvc_video_decode_start()
1023 if (stream->last_fid != fid) { in uvc_video_decode_start()
1024 stream->sequence++; in uvc_video_decode_start()
1025 if (stream->sequence) in uvc_video_decode_start()
1032 /* Store the payload FID bit and return immediately when the buffer is in uvc_video_decode_start()
1036 stream->last_fid = fid; in uvc_video_decode_start()
1037 return -ENODATA; in uvc_video_decode_start()
1040 /* Mark the buffer as bad if the error bit is set. */ in uvc_video_decode_start()
1042 uvc_trace(UVC_TRACE_FRAME, "Marking buffer as bad (error bit " in uvc_video_decode_start()
1044 buf->error = 1; in uvc_video_decode_start()
1048 * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE. in uvc_video_decode_start()
1049 * stream->last_fid is initialized to -1, so the first isochronous in uvc_video_decode_start()
1050 * frame will always be in sync. in uvc_video_decode_start()
1052 * If the device doesn't toggle the FID bit, invert stream->last_fid in uvc_video_decode_start()
1055 if (buf->state != UVC_BUF_STATE_ACTIVE) { in uvc_video_decode_start()
1056 if (fid == stream->last_fid) { in uvc_video_decode_start()
1059 if ((stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) && in uvc_video_decode_start()
1061 stream->last_fid ^= UVC_STREAM_FID; in uvc_video_decode_start()
1062 return -ENODATA; in uvc_video_decode_start()
1065 buf->buf.field = V4L2_FIELD_NONE; in uvc_video_decode_start()
1066 buf->buf.sequence = stream->sequence; in uvc_video_decode_start()
1067 buf->buf.vb2_buf.timestamp = ktime_to_ns(uvc_video_get_time()); in uvc_video_decode_start()
1070 buf->state = UVC_BUF_STATE_ACTIVE; in uvc_video_decode_start()
1073 /* Mark the buffer as done if we're at the beginning of a new frame. in uvc_video_decode_start()
1074 * End of frame detection is better implemented by checking the EOF in uvc_video_decode_start()
1075 * bit (FID bit toggling is delayed by one frame compared to the EOF in uvc_video_decode_start()
1076 * bit), but some devices don't set the bit at end of frame (and the in uvc_video_decode_start()
1080 * stream->last_fid is initialized to -1, so the first isochronous in uvc_video_decode_start()
1081 * frame will never trigger an end of frame detection. in uvc_video_decode_start()
1083 * Empty buffers (bytesused == 0) don't trigger end of frame detection in uvc_video_decode_start()
1084 * as it doesn't make sense to return an empty buffer. This also in uvc_video_decode_start()
1085 * avoids detecting end of frame conditions at FID toggling if the in uvc_video_decode_start()
1088 if (fid != stream->last_fid && buf->bytesused != 0) { in uvc_video_decode_start()
1089 uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit " in uvc_video_decode_start()
1091 buf->state = UVC_BUF_STATE_READY; in uvc_video_decode_start()
1092 return -EAGAIN; in uvc_video_decode_start()
1095 stream->last_fid = fid; in uvc_video_decode_start()
1103 * Copy URB data to video buffers in process context, releasing buffer
1112 for (i = 0; i < uvc_urb->async_operations; i++) { in uvc_video_copy_data_work()
1113 struct uvc_copy_op *op = &uvc_urb->copy_operations[i]; in uvc_video_copy_data_work()
1115 memcpy(op->dst, op->src, op->len); in uvc_video_copy_data_work()
1117 /* Release reference taken on this buffer. */ in uvc_video_copy_data_work()
1118 uvc_queue_buffer_release(op->buf); in uvc_video_copy_data_work()
1121 ret = usb_submit_urb(uvc_urb->urb, GFP_KERNEL); in uvc_video_copy_data_work()
1130 unsigned int active_op = uvc_urb->async_operations; in uvc_video_decode_data()
1131 struct uvc_copy_op *op = &uvc_urb->copy_operations[active_op]; in uvc_video_decode_data()
1137 maxlen = buf->length - buf->bytesused; in uvc_video_decode_data()
1139 /* Take a buffer reference for async work. */ in uvc_video_decode_data()
1140 kref_get(&buf->ref); in uvc_video_decode_data()
1142 op->buf = buf; in uvc_video_decode_data()
1143 op->src = data; in uvc_video_decode_data()
1144 op->dst = buf->mem + buf->bytesused; in uvc_video_decode_data()
1145 op->len = min_t(unsigned int, len, maxlen); in uvc_video_decode_data()
1147 buf->bytesused += op->len; in uvc_video_decode_data()
1149 /* Complete the current frame if the buffer size was exceeded. */ in uvc_video_decode_data()
1151 uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n"); in uvc_video_decode_data()
1152 buf->error = 1; in uvc_video_decode_data()
1153 buf->state = UVC_BUF_STATE_READY; in uvc_video_decode_data()
1156 uvc_urb->async_operations++; in uvc_video_decode_data()
1162 /* Mark the buffer as done if the EOF marker is set. */ in uvc_video_decode_end()
1163 if (data[1] & UVC_STREAM_EOF && buf->bytesused != 0) { in uvc_video_decode_end()
1164 uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n"); in uvc_video_decode_end()
1167 buf->state = UVC_BUF_STATE_READY; in uvc_video_decode_end()
1168 if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) in uvc_video_decode_end()
1169 stream->last_fid ^= UVC_STREAM_FID; in uvc_video_decode_end()
1177 * data to the transfer buffer and returns the header size. As the only known
1178 * UVC output device transfers a whole frame in a single payload, the EOF bit
1182 * video buffer to the transfer buffer.
1189 | (stream->last_fid & UVC_STREAM_FID); in uvc_video_encode_header()
1196 struct uvc_video_queue *queue = &stream->queue; in uvc_video_encode_data()
1200 /* Copy video data to the URB buffer. */ in uvc_video_encode_data()
1201 mem = buf->mem + queue->buf_used; in uvc_video_encode_data()
1202 nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used); in uvc_video_encode_data()
1203 nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size, in uvc_video_encode_data()
1207 queue->buf_used += nbytes; in uvc_video_encode_data()
1212 /* ------------------------------------------------------------------------
1218 * Frame Numbers and system time values. The resulting buffer is thus composed
1219 * of blocks, containing a 64-bit timestamp in nanoseconds, a 16-bit USB Frame
1222 * Ideally we want to capture all payload headers for each frame. However, their
1242 if (meta_buf->length - meta_buf->bytesused < in uvc_video_decode_meta()
1243 length + sizeof(meta->ns) + sizeof(meta->sof)) { in uvc_video_decode_meta()
1244 meta_buf->error = 1; in uvc_video_decode_meta()
1261 if (stream->meta.format == V4L2_META_FMT_UVC) in uvc_video_decode_meta()
1265 !memcmp(scr, stream->clock.last_scr, 6))) in uvc_video_decode_meta()
1268 meta = (struct uvc_meta_buf *)((u8 *)meta_buf->mem + meta_buf->bytesused); in uvc_video_decode_meta()
1271 sof = usb_get_current_frame_number(stream->dev->udev); in uvc_video_decode_meta()
1273 put_unaligned(ktime_to_ns(time), &meta->ns); in uvc_video_decode_meta()
1274 put_unaligned(sof, &meta->sof); in uvc_video_decode_meta()
1277 memcpy(stream->clock.last_scr, scr, 6); in uvc_video_decode_meta()
1279 memcpy(&meta->length, mem, length); in uvc_video_decode_meta()
1280 meta_buf->bytesused += length + sizeof(meta->ns) + sizeof(meta->sof); in uvc_video_decode_meta()
1283 "%s(): t-sys %lluns, SOF %u, len %u, flags 0x%x, PTS %u, STC %u frame SOF %u\n", in uvc_video_decode_meta()
1284 __func__, ktime_to_ns(time), meta->sof, meta->length, in uvc_video_decode_meta()
1285 meta->flags, in uvc_video_decode_meta()
1286 has_pts ? *(u32 *)meta->buf : 0, in uvc_video_decode_meta()
1291 /* ------------------------------------------------------------------------
1296 * Set error flag for incomplete buffer.
1301 if (stream->ctrl.dwMaxVideoFrameSize != buf->bytesused && in uvc_video_validate_buffer()
1302 !(stream->cur_format->flags & UVC_FMT_FLAG_COMPRESSED)) in uvc_video_validate_buffer()
1303 buf->error = 1; in uvc_video_validate_buffer()
1316 struct vb2_v4l2_buffer *vb2_meta = &(*meta_buf)->buf; in uvc_video_next_buffers()
1317 const struct vb2_v4l2_buffer *vb2_video = &(*video_buf)->buf; in uvc_video_next_buffers()
1319 vb2_meta->sequence = vb2_video->sequence; in uvc_video_next_buffers()
1320 vb2_meta->field = vb2_video->field; in uvc_video_next_buffers()
1321 vb2_meta->vb2_buf.timestamp = vb2_video->vb2_buf.timestamp; in uvc_video_next_buffers()
1323 (*meta_buf)->state = UVC_BUF_STATE_READY; in uvc_video_next_buffers()
1324 if (!(*meta_buf)->error) in uvc_video_next_buffers()
1325 (*meta_buf)->error = (*video_buf)->error; in uvc_video_next_buffers()
1326 *meta_buf = uvc_queue_next_buffer(&stream->meta.queue, in uvc_video_next_buffers()
1329 *video_buf = uvc_queue_next_buffer(&stream->queue, *video_buf); in uvc_video_next_buffers()
1335 struct urb *urb = uvc_urb->urb; in uvc_video_decode_isoc()
1336 struct uvc_streaming *stream = uvc_urb->stream; in uvc_video_decode_isoc()
1340 for (i = 0; i < urb->number_of_packets; ++i) { in uvc_video_decode_isoc()
1341 if (urb->iso_frame_desc[i].status < 0) { in uvc_video_decode_isoc()
1342 uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame " in uvc_video_decode_isoc()
1343 "lost (%d).\n", urb->iso_frame_desc[i].status); in uvc_video_decode_isoc()
1344 /* Mark the buffer as faulty. */ in uvc_video_decode_isoc()
1346 buf->error = 1; in uvc_video_decode_isoc()
1351 mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset; in uvc_video_decode_isoc()
1354 urb->iso_frame_desc[i].actual_length); in uvc_video_decode_isoc()
1355 if (ret == -EAGAIN) in uvc_video_decode_isoc()
1357 } while (ret == -EAGAIN); in uvc_video_decode_isoc()
1366 urb->iso_frame_desc[i].actual_length - ret); in uvc_video_decode_isoc()
1370 urb->iso_frame_desc[i].actual_length); in uvc_video_decode_isoc()
1372 if (buf->state == UVC_BUF_STATE_READY) in uvc_video_decode_isoc()
1380 struct urb *urb = uvc_urb->urb; in uvc_video_decode_bulk()
1381 struct uvc_streaming *stream = uvc_urb->stream; in uvc_video_decode_bulk()
1386 * Ignore ZLPs if they're not part of a frame, otherwise process them in uvc_video_decode_bulk()
1389 if (urb->actual_length == 0 && stream->bulk.header_size == 0) in uvc_video_decode_bulk()
1392 mem = urb->transfer_buffer; in uvc_video_decode_bulk()
1393 len = urb->actual_length; in uvc_video_decode_bulk()
1394 stream->bulk.payload_size += len; in uvc_video_decode_bulk()
1399 if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) { in uvc_video_decode_bulk()
1402 if (ret == -EAGAIN) in uvc_video_decode_bulk()
1404 } while (ret == -EAGAIN); in uvc_video_decode_bulk()
1408 stream->bulk.skip_payload = 1; in uvc_video_decode_bulk()
1410 memcpy(stream->bulk.header, mem, ret); in uvc_video_decode_bulk()
1411 stream->bulk.header_size = ret; in uvc_video_decode_bulk()
1416 len -= ret; in uvc_video_decode_bulk()
1420 /* The buffer queue might have been cancelled while a bulk transfer in uvc_video_decode_bulk()
1426 if (!stream->bulk.skip_payload && buf != NULL) in uvc_video_decode_bulk()
1432 if (urb->actual_length < urb->transfer_buffer_length || in uvc_video_decode_bulk()
1433 stream->bulk.payload_size >= stream->bulk.max_payload_size) { in uvc_video_decode_bulk()
1434 if (!stream->bulk.skip_payload && buf != NULL) { in uvc_video_decode_bulk()
1435 uvc_video_decode_end(stream, buf, stream->bulk.header, in uvc_video_decode_bulk()
1436 stream->bulk.payload_size); in uvc_video_decode_bulk()
1437 if (buf->state == UVC_BUF_STATE_READY) in uvc_video_decode_bulk()
1441 stream->bulk.header_size = 0; in uvc_video_decode_bulk()
1442 stream->bulk.skip_payload = 0; in uvc_video_decode_bulk()
1443 stream->bulk.payload_size = 0; in uvc_video_decode_bulk()
1450 struct urb *urb = uvc_urb->urb; in uvc_video_encode_bulk()
1451 struct uvc_streaming *stream = uvc_urb->stream; in uvc_video_encode_bulk()
1453 u8 *mem = urb->transfer_buffer; in uvc_video_encode_bulk()
1454 int len = stream->urb_size, ret; in uvc_video_encode_bulk()
1457 urb->transfer_buffer_length = 0; in uvc_video_encode_bulk()
1462 if (stream->bulk.header_size == 0) { in uvc_video_encode_bulk()
1464 stream->bulk.header_size = ret; in uvc_video_encode_bulk()
1465 stream->bulk.payload_size += ret; in uvc_video_encode_bulk()
1467 len -= ret; in uvc_video_encode_bulk()
1473 stream->bulk.payload_size += ret; in uvc_video_encode_bulk()
1474 len -= ret; in uvc_video_encode_bulk()
1476 if (buf->bytesused == stream->queue.buf_used || in uvc_video_encode_bulk()
1477 stream->bulk.payload_size == stream->bulk.max_payload_size) { in uvc_video_encode_bulk()
1478 if (buf->bytesused == stream->queue.buf_used) { in uvc_video_encode_bulk()
1479 stream->queue.buf_used = 0; in uvc_video_encode_bulk()
1480 buf->state = UVC_BUF_STATE_READY; in uvc_video_encode_bulk()
1481 buf->buf.sequence = ++stream->sequence; in uvc_video_encode_bulk()
1482 uvc_queue_next_buffer(&stream->queue, buf); in uvc_video_encode_bulk()
1483 stream->last_fid ^= UVC_STREAM_FID; in uvc_video_encode_bulk()
1486 stream->bulk.header_size = 0; in uvc_video_encode_bulk()
1487 stream->bulk.payload_size = 0; in uvc_video_encode_bulk()
1490 urb->transfer_buffer_length = stream->urb_size - len; in uvc_video_encode_bulk()
1495 struct uvc_urb *uvc_urb = urb->context; in uvc_video_complete()
1496 struct uvc_streaming *stream = uvc_urb->stream; in uvc_video_complete()
1497 struct uvc_video_queue *queue = &stream->queue; in uvc_video_complete()
1498 struct uvc_video_queue *qmeta = &stream->meta.queue; in uvc_video_complete()
1499 struct vb2_queue *vb2_qmeta = stream->meta.vdev.queue; in uvc_video_complete()
1505 switch (urb->status) { in uvc_video_complete()
1510 uvc_printk(KERN_WARNING, "Non-zero status (%d) in video " in uvc_video_complete()
1511 "completion handler.\n", urb->status); in uvc_video_complete()
1513 case -ENOENT: /* usb_poison_urb() called. */ in uvc_video_complete()
1514 if (stream->frozen) in uvc_video_complete()
1517 case -ECONNRESET: /* usb_unlink_urb() called. */ in uvc_video_complete()
1518 case -ESHUTDOWN: /* The endpoint is being disabled. */ in uvc_video_complete()
1519 uvc_queue_cancel(queue, urb->status == -ESHUTDOWN); in uvc_video_complete()
1521 uvc_queue_cancel(qmeta, urb->status == -ESHUTDOWN); in uvc_video_complete()
1528 spin_lock_irqsave(&qmeta->irqlock, flags); in uvc_video_complete()
1529 if (!list_empty(&qmeta->irqqueue)) in uvc_video_complete()
1530 buf_meta = list_first_entry(&qmeta->irqqueue, in uvc_video_complete()
1532 spin_unlock_irqrestore(&qmeta->irqlock, flags); in uvc_video_complete()
1535 /* Re-initialise the URB async work. */ in uvc_video_complete()
1536 uvc_urb->async_operations = 0; in uvc_video_complete()
1542 stream->decode(uvc_urb, buf, buf_meta); in uvc_video_complete()
1545 if (!uvc_urb->async_operations) { in uvc_video_complete()
1546 ret = usb_submit_urb(uvc_urb->urb, GFP_ATOMIC); in uvc_video_complete()
1554 queue_work(stream->async_wq, &uvc_urb->work); in uvc_video_complete()
1565 if (!uvc_urb->buffer) in uvc_free_urb_buffers()
1569 usb_free_coherent(stream->dev->udev, stream->urb_size, in uvc_free_urb_buffers()
1570 uvc_urb->buffer, uvc_urb->dma); in uvc_free_urb_buffers()
1572 kfree(uvc_urb->buffer); in uvc_free_urb_buffers()
1574 uvc_urb->buffer = NULL; in uvc_free_urb_buffers()
1577 stream->urb_size = 0; in uvc_free_urb_buffers()
1585 * Limit the buffer size to UVC_MAX_PACKETS bulk/isochronous packets. If the
1598 if (stream->urb_size) in uvc_alloc_urb_buffers()
1599 return stream->urb_size / psize; in uvc_alloc_urb_buffers()
1611 struct uvc_urb *uvc_urb = &stream->uvc_urb[i]; in uvc_alloc_urb_buffers()
1613 stream->urb_size = psize * npackets; in uvc_alloc_urb_buffers()
1615 uvc_urb->buffer = usb_alloc_coherent( in uvc_alloc_urb_buffers()
1616 stream->dev->udev, stream->urb_size, in uvc_alloc_urb_buffers()
1617 gfp_flags | __GFP_NOWARN, &uvc_urb->dma); in uvc_alloc_urb_buffers()
1619 uvc_urb->buffer = in uvc_alloc_urb_buffers()
1620 kmalloc(stream->urb_size, gfp_flags | __GFP_NOWARN); in uvc_alloc_urb_buffers()
1622 if (!uvc_urb->buffer) { in uvc_alloc_urb_buffers()
1627 uvc_urb->stream = stream; in uvc_alloc_urb_buffers()
1659 usb_poison_urb(uvc_urb->urb); in uvc_video_stop_transfer()
1661 flush_workqueue(stream->async_wq); in uvc_video_stop_transfer()
1664 usb_free_urb(uvc_urb->urb); in uvc_video_stop_transfer()
1665 uvc_urb->urb = NULL; in uvc_video_stop_transfer()
1681 switch (dev->speed) { in uvc_endpoint_max_bpi()
1684 return le16_to_cpu(ep->ss_ep_comp.wBytesPerInterval); in uvc_endpoint_max_bpi()
1686 psize = usb_endpoint_maxp(&ep->desc); in uvc_endpoint_max_bpi()
1687 mult = usb_endpoint_maxp_mult(&ep->desc); in uvc_endpoint_max_bpi()
1690 psize = usb_endpoint_maxp(&ep->desc); in uvc_endpoint_max_bpi()
1693 psize = usb_endpoint_maxp(&ep->desc); in uvc_endpoint_max_bpi()
1711 psize = uvc_endpoint_max_bpi(stream->dev->udev, ep); in uvc_init_video_isoc()
1712 size = stream->ctrl.dwMaxVideoFrameSize; in uvc_init_video_isoc()
1716 return -ENOMEM; in uvc_init_video_isoc()
1724 return -ENOMEM; in uvc_init_video_isoc()
1727 urb->dev = stream->dev->udev; in uvc_init_video_isoc()
1728 urb->context = uvc_urb; in uvc_init_video_isoc()
1729 urb->pipe = usb_rcvisocpipe(stream->dev->udev, in uvc_init_video_isoc()
1730 ep->desc.bEndpointAddress); in uvc_init_video_isoc()
1732 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; in uvc_init_video_isoc()
1733 urb->transfer_dma = uvc_urb->dma; in uvc_init_video_isoc()
1735 urb->transfer_flags = URB_ISO_ASAP; in uvc_init_video_isoc()
1737 urb->interval = ep->desc.bInterval; in uvc_init_video_isoc()
1738 urb->transfer_buffer = uvc_urb->buffer; in uvc_init_video_isoc()
1739 urb->complete = uvc_video_complete; in uvc_init_video_isoc()
1740 urb->number_of_packets = npackets; in uvc_init_video_isoc()
1741 urb->transfer_buffer_length = size; in uvc_init_video_isoc()
1744 urb->iso_frame_desc[i].offset = i * psize; in uvc_init_video_isoc()
1745 urb->iso_frame_desc[i].length = psize; in uvc_init_video_isoc()
1748 uvc_urb->urb = urb; in uvc_init_video_isoc()
1767 psize = usb_endpoint_maxp(&ep->desc); in uvc_init_video_bulk()
1768 size = stream->ctrl.dwMaxPayloadTransferSize; in uvc_init_video_bulk()
1769 stream->bulk.max_payload_size = size; in uvc_init_video_bulk()
1773 return -ENOMEM; in uvc_init_video_bulk()
1777 if (usb_endpoint_dir_in(&ep->desc)) in uvc_init_video_bulk()
1778 pipe = usb_rcvbulkpipe(stream->dev->udev, in uvc_init_video_bulk()
1779 ep->desc.bEndpointAddress); in uvc_init_video_bulk()
1781 pipe = usb_sndbulkpipe(stream->dev->udev, in uvc_init_video_bulk()
1782 ep->desc.bEndpointAddress); in uvc_init_video_bulk()
1784 if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) in uvc_init_video_bulk()
1791 return -ENOMEM; in uvc_init_video_bulk()
1794 usb_fill_bulk_urb(urb, stream->dev->udev, pipe, uvc_urb->buffer, in uvc_init_video_bulk()
1797 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; in uvc_init_video_bulk()
1798 urb->transfer_dma = uvc_urb->dma; in uvc_init_video_bulk()
1801 uvc_urb->urb = urb; in uvc_init_video_bulk()
1813 struct usb_interface *intf = stream->intf; in uvc_video_start_transfer()
1819 stream->sequence = -1; in uvc_video_start_transfer()
1820 stream->last_fid = -1; in uvc_video_start_transfer()
1821 stream->bulk.header_size = 0; in uvc_video_start_transfer()
1822 stream->bulk.skip_payload = 0; in uvc_video_start_transfer()
1823 stream->bulk.payload_size = 0; in uvc_video_start_transfer()
1827 if (intf->num_altsetting > 1) { in uvc_video_start_transfer()
1832 int intfnum = stream->intfnum; in uvc_video_start_transfer()
1835 bandwidth = stream->ctrl.dwMaxPayloadTransferSize; in uvc_video_start_transfer()
1843 "B/frame bandwidth.\n", bandwidth); in uvc_video_start_transfer()
1846 for (i = 0; i < intf->num_altsetting; ++i) { in uvc_video_start_transfer()
1850 alts = &intf->altsetting[i]; in uvc_video_start_transfer()
1852 stream->header.bEndpointAddress); in uvc_video_start_transfer()
1857 psize = uvc_endpoint_max_bpi(stream->dev->udev, ep); in uvc_video_start_transfer()
1859 altsetting = alts->desc.bAlternateSetting; in uvc_video_start_transfer()
1868 return -EIO; in uvc_video_start_transfer()
1872 "(%u B/frame bandwidth).\n", altsetting, best_psize); in uvc_video_start_transfer()
1874 ret = usb_set_interface(stream->dev->udev, intfnum, altsetting); in uvc_video_start_transfer()
1881 ep = uvc_find_endpoint(&intf->altsetting[0], in uvc_video_start_transfer()
1882 stream->header.bEndpointAddress); in uvc_video_start_transfer()
1884 return -EIO; in uvc_video_start_transfer()
1894 ret = usb_submit_urb(uvc_urb->urb, gfp_flags); in uvc_video_start_transfer()
1906 if (stream->dev->quirks & UVC_QUIRK_RESTORE_CTRLS_ON_INIT) in uvc_video_start_transfer()
1907 uvc_ctrl_restore_values(stream->dev); in uvc_video_start_transfer()
1912 /* --------------------------------------------------------------------------
1925 if (!uvc_queue_streaming(&stream->queue)) in uvc_video_suspend()
1928 stream->frozen = 1; in uvc_video_suspend()
1930 usb_set_interface(stream->dev->udev, stream->intfnum, 0); in uvc_video_suspend()
1952 usb_set_interface(stream->dev->udev, stream->intfnum, 0); in uvc_video_resume()
1954 stream->frozen = 0; in uvc_video_resume()
1958 if (!uvc_queue_streaming(&stream->queue)) in uvc_video_resume()
1961 ret = uvc_commit_video(stream, &stream->ctrl); in uvc_video_resume()
1968 /* ------------------------------------------------------------------------
1976 * Some cameras (namely the Fuji Finepix) set the format and frame
1984 struct uvc_streaming_control *probe = &stream->ctrl; in uvc_video_init()
1986 struct uvc_frame *frame = NULL; in uvc_video_init() local
1991 if (stream->nformats == 0) { in uvc_video_init()
1993 return -EINVAL; in uvc_video_init()
1996 atomic_set(&stream->active, 0); in uvc_video_init()
2003 usb_set_interface(stream->dev->udev, stream->intfnum, 0); in uvc_video_init()
2025 for (i = stream->nformats; i > 0; --i) { in uvc_video_init()
2026 format = &stream->format[i-1]; in uvc_video_init()
2027 if (format->index == probe->bFormatIndex) in uvc_video_init()
2031 if (format->nframes == 0) { in uvc_video_init()
2032 uvc_printk(KERN_INFO, "No frame descriptor found for the " in uvc_video_init()
2034 return -EINVAL; in uvc_video_init()
2037 /* Zero bFrameIndex might be correct. Stream-based formats (including in uvc_video_init()
2038 * MPEG-2 TS and DV) do not support frames but have a dummy frame in uvc_video_init()
2039 * descriptor with bFrameIndex set to zero. If the default frame in uvc_video_init()
2040 * descriptor is not found, use the first available frame. in uvc_video_init()
2042 for (i = format->nframes; i > 0; --i) { in uvc_video_init()
2043 frame = &format->frame[i-1]; in uvc_video_init()
2044 if (frame->bFrameIndex == probe->bFrameIndex) in uvc_video_init()
2048 probe->bFormatIndex = format->index; in uvc_video_init()
2049 probe->bFrameIndex = frame->bFrameIndex; in uvc_video_init()
2051 stream->def_format = format; in uvc_video_init()
2052 stream->cur_format = format; in uvc_video_init()
2053 stream->cur_frame = frame; in uvc_video_init()
2056 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { in uvc_video_init()
2057 if (stream->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT) in uvc_video_init()
2058 stream->decode = uvc_video_decode_isight; in uvc_video_init()
2059 else if (stream->intf->num_altsetting > 1) in uvc_video_init()
2060 stream->decode = uvc_video_decode_isoc; in uvc_video_init()
2062 stream->decode = uvc_video_decode_bulk; in uvc_video_init()
2064 if (stream->intf->num_altsetting == 1) in uvc_video_init()
2065 stream->decode = uvc_video_encode_bulk; in uvc_video_init()
2069 return -EINVAL; in uvc_video_init()
2075 INIT_WORK(&uvc_urb->work, uvc_video_copy_data_work); in uvc_video_init()
2089 ret = uvc_commit_video(stream, &stream->ctrl); in uvc_video_start_streaming()
2100 usb_set_interface(stream->dev->udev, stream->intfnum, 0); in uvc_video_start_streaming()
2111 if (stream->intf->num_altsetting > 1) { in uvc_video_stop_streaming()
2112 usb_set_interface(stream->dev->udev, stream->intfnum, 0); in uvc_video_stop_streaming()
2114 /* UVC doesn't specify how to inform a bulk-based device in uvc_video_stop_streaming()
2119 unsigned int epnum = stream->header.bEndpointAddress in uvc_video_stop_streaming()
2121 unsigned int dir = stream->header.bEndpointAddress in uvc_video_stop_streaming()
2125 pipe = usb_sndbulkpipe(stream->dev->udev, epnum) | dir; in uvc_video_stop_streaming()
2126 usb_clear_halt(stream->dev->udev, pipe); in uvc_video_stop_streaming()