1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/kernel.h>
15#include <linux/list.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/usb.h>
19#include <linux/videodev2.h>
20#include <linux/vmalloc.h>
21#include <linux/wait.h>
22#include <linux/atomic.h>
23#include <asm/unaligned.h>
24
25#include <media/v4l2-common.h>
26
27#include "uvcvideo.h"
28
29
30
31
32
33static int __uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
34 __u8 intfnum, __u8 cs, void *data, __u16 size,
35 int timeout)
36{
37 __u8 type = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
38 unsigned int pipe;
39
40 pipe = (query & 0x80) ? usb_rcvctrlpipe(dev->udev, 0)
41 : usb_sndctrlpipe(dev->udev, 0);
42 type |= (query & 0x80) ? USB_DIR_IN : USB_DIR_OUT;
43
44 return usb_control_msg(dev->udev, pipe, query, type, cs << 8,
45 unit << 8 | intfnum, data, size, timeout);
46}
47
48static const char *uvc_query_name(__u8 query)
49{
50 switch (query) {
51 case UVC_SET_CUR:
52 return "SET_CUR";
53 case UVC_GET_CUR:
54 return "GET_CUR";
55 case UVC_GET_MIN:
56 return "GET_MIN";
57 case UVC_GET_MAX:
58 return "GET_MAX";
59 case UVC_GET_RES:
60 return "GET_RES";
61 case UVC_GET_LEN:
62 return "GET_LEN";
63 case UVC_GET_INFO:
64 return "GET_INFO";
65 case UVC_GET_DEF:
66 return "GET_DEF";
67 default:
68 return "<invalid>";
69 }
70}
71
72int uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
73 __u8 intfnum, __u8 cs, void *data, __u16 size)
74{
75 int ret;
76
77 ret = __uvc_query_ctrl(dev, query, unit, intfnum, cs, data, size,
78 UVC_CTRL_CONTROL_TIMEOUT);
79 if (ret != size) {
80 uvc_printk(KERN_ERR, "Failed to query (%s) UVC control %u on "
81 "unit %u: %d (exp. %u).\n", uvc_query_name(query), cs,
82 unit, ret, size);
83 return -EIO;
84 }
85
86 return 0;
87}
88
89static void uvc_fixup_video_ctrl(struct uvc_streaming *stream,
90 struct uvc_streaming_control *ctrl)
91{
92 struct uvc_format *format = NULL;
93 struct uvc_frame *frame = NULL;
94 unsigned int i;
95
96 for (i = 0; i < stream->nformats; ++i) {
97 if (stream->format[i].index == ctrl->bFormatIndex) {
98 format = &stream->format[i];
99 break;
100 }
101 }
102
103 if (format == NULL)
104 return;
105
106 for (i = 0; i < format->nframes; ++i) {
107 if (format->frame[i].bFrameIndex == ctrl->bFrameIndex) {
108 frame = &format->frame[i];
109 break;
110 }
111 }
112
113 if (frame == NULL)
114 return;
115
116 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) ||
117 (ctrl->dwMaxVideoFrameSize == 0 &&
118 stream->dev->uvc_version < 0x0110))
119 ctrl->dwMaxVideoFrameSize =
120 frame->dwMaxVideoFrameBufferSize;
121
122
123
124
125
126
127 if ((ctrl->dwMaxPayloadTransferSize & 0xffff0000) == 0xffff0000)
128 ctrl->dwMaxPayloadTransferSize &= ~0xffff0000;
129
130 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) &&
131 stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH &&
132 stream->intf->num_altsetting > 1) {
133 u32 interval;
134 u32 bandwidth;
135
136 interval = (ctrl->dwFrameInterval > 100000)
137 ? ctrl->dwFrameInterval
138 : frame->dwFrameInterval[0];
139
140
141
142
143
144
145
146 bandwidth = frame->wWidth * frame->wHeight / 8 * format->bpp;
147 bandwidth *= 10000000 / interval + 1;
148 bandwidth /= 1000;
149 if (stream->dev->udev->speed == USB_SPEED_HIGH)
150 bandwidth /= 8;
151 bandwidth += 12;
152
153
154
155
156
157
158
159
160 bandwidth = max_t(u32, bandwidth, 1024);
161
162 ctrl->dwMaxPayloadTransferSize = bandwidth;
163 }
164}
165
166static int uvc_get_video_ctrl(struct uvc_streaming *stream,
167 struct uvc_streaming_control *ctrl, int probe, __u8 query)
168{
169 __u8 *data;
170 __u16 size;
171 int ret;
172
173 size = stream->dev->uvc_version >= 0x0110 ? 34 : 26;
174 if ((stream->dev->quirks & UVC_QUIRK_PROBE_DEF) &&
175 query == UVC_GET_DEF)
176 return -EIO;
177
178 data = kmalloc(size, GFP_KERNEL);
179 if (data == NULL)
180 return -ENOMEM;
181
182 ret = __uvc_query_ctrl(stream->dev, query, 0, stream->intfnum,
183 probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
184 size, uvc_timeout_param);
185
186 if ((query == UVC_GET_MIN || query == UVC_GET_MAX) && ret == 2) {
187
188
189
190
191 uvc_warn_once(stream->dev, UVC_WARN_MINMAX, "UVC non "
192 "compliance - GET_MIN/MAX(PROBE) incorrectly "
193 "supported. Enabling workaround.\n");
194 memset(ctrl, 0, sizeof *ctrl);
195 ctrl->wCompQuality = le16_to_cpup((__le16 *)data);
196 ret = 0;
197 goto out;
198 } else if (query == UVC_GET_DEF && probe == 1 && ret != size) {
199
200
201
202
203 uvc_warn_once(stream->dev, UVC_WARN_PROBE_DEF, "UVC non "
204 "compliance - GET_DEF(PROBE) not supported. "
205 "Enabling workaround.\n");
206 ret = -EIO;
207 goto out;
208 } else if (ret != size) {
209 uvc_printk(KERN_ERR, "Failed to query (%u) UVC %s control : "
210 "%d (exp. %u).\n", query, probe ? "probe" : "commit",
211 ret, size);
212 ret = -EIO;
213 goto out;
214 }
215
216 ctrl->bmHint = le16_to_cpup((__le16 *)&data[0]);
217 ctrl->bFormatIndex = data[2];
218 ctrl->bFrameIndex = data[3];
219 ctrl->dwFrameInterval = le32_to_cpup((__le32 *)&data[4]);
220 ctrl->wKeyFrameRate = le16_to_cpup((__le16 *)&data[8]);
221 ctrl->wPFrameRate = le16_to_cpup((__le16 *)&data[10]);
222 ctrl->wCompQuality = le16_to_cpup((__le16 *)&data[12]);
223 ctrl->wCompWindowSize = le16_to_cpup((__le16 *)&data[14]);
224 ctrl->wDelay = le16_to_cpup((__le16 *)&data[16]);
225 ctrl->dwMaxVideoFrameSize = get_unaligned_le32(&data[18]);
226 ctrl->dwMaxPayloadTransferSize = get_unaligned_le32(&data[22]);
227
228 if (size == 34) {
229 ctrl->dwClockFrequency = get_unaligned_le32(&data[26]);
230 ctrl->bmFramingInfo = data[30];
231 ctrl->bPreferedVersion = data[31];
232 ctrl->bMinVersion = data[32];
233 ctrl->bMaxVersion = data[33];
234 } else {
235 ctrl->dwClockFrequency = stream->dev->clock_frequency;
236 ctrl->bmFramingInfo = 0;
237 ctrl->bPreferedVersion = 0;
238 ctrl->bMinVersion = 0;
239 ctrl->bMaxVersion = 0;
240 }
241
242
243
244
245
246 uvc_fixup_video_ctrl(stream, ctrl);
247 ret = 0;
248
249out:
250 kfree(data);
251 return ret;
252}
253
254static int uvc_set_video_ctrl(struct uvc_streaming *stream,
255 struct uvc_streaming_control *ctrl, int probe)
256{
257 __u8 *data;
258 __u16 size;
259 int ret;
260
261 size = stream->dev->uvc_version >= 0x0110 ? 34 : 26;
262 data = kzalloc(size, GFP_KERNEL);
263 if (data == NULL)
264 return -ENOMEM;
265
266 *(__le16 *)&data[0] = cpu_to_le16(ctrl->bmHint);
267 data[2] = ctrl->bFormatIndex;
268 data[3] = ctrl->bFrameIndex;
269 *(__le32 *)&data[4] = cpu_to_le32(ctrl->dwFrameInterval);
270 *(__le16 *)&data[8] = cpu_to_le16(ctrl->wKeyFrameRate);
271 *(__le16 *)&data[10] = cpu_to_le16(ctrl->wPFrameRate);
272 *(__le16 *)&data[12] = cpu_to_le16(ctrl->wCompQuality);
273 *(__le16 *)&data[14] = cpu_to_le16(ctrl->wCompWindowSize);
274 *(__le16 *)&data[16] = cpu_to_le16(ctrl->wDelay);
275 put_unaligned_le32(ctrl->dwMaxVideoFrameSize, &data[18]);
276 put_unaligned_le32(ctrl->dwMaxPayloadTransferSize, &data[22]);
277
278 if (size == 34) {
279 put_unaligned_le32(ctrl->dwClockFrequency, &data[26]);
280 data[30] = ctrl->bmFramingInfo;
281 data[31] = ctrl->bPreferedVersion;
282 data[32] = ctrl->bMinVersion;
283 data[33] = ctrl->bMaxVersion;
284 }
285
286 ret = __uvc_query_ctrl(stream->dev, UVC_SET_CUR, 0, stream->intfnum,
287 probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
288 size, uvc_timeout_param);
289 if (ret != size) {
290 uvc_printk(KERN_ERR, "Failed to set UVC %s control : "
291 "%d (exp. %u).\n", probe ? "probe" : "commit",
292 ret, size);
293 ret = -EIO;
294 }
295
296 kfree(data);
297 return ret;
298}
299
300int uvc_probe_video(struct uvc_streaming *stream,
301 struct uvc_streaming_control *probe)
302{
303 struct uvc_streaming_control probe_min, probe_max;
304 __u16 bandwidth;
305 unsigned int i;
306 int ret;
307
308
309
310
311
312
313
314
315 ret = uvc_set_video_ctrl(stream, probe, 1);
316 if (ret < 0)
317 goto done;
318
319
320 if (!(stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX)) {
321 ret = uvc_get_video_ctrl(stream, &probe_min, 1, UVC_GET_MIN);
322 if (ret < 0)
323 goto done;
324 ret = uvc_get_video_ctrl(stream, &probe_max, 1, UVC_GET_MAX);
325 if (ret < 0)
326 goto done;
327
328 probe->wCompQuality = probe_max.wCompQuality;
329 }
330
331 for (i = 0; i < 2; ++i) {
332 ret = uvc_set_video_ctrl(stream, probe, 1);
333 if (ret < 0)
334 goto done;
335 ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
336 if (ret < 0)
337 goto done;
338
339 if (stream->intf->num_altsetting == 1)
340 break;
341
342 bandwidth = probe->dwMaxPayloadTransferSize;
343 if (bandwidth <= stream->maxpsize)
344 break;
345
346 if (stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX) {
347 ret = -ENOSPC;
348 goto done;
349 }
350
351
352 probe->wKeyFrameRate = probe_min.wKeyFrameRate;
353 probe->wPFrameRate = probe_min.wPFrameRate;
354 probe->wCompQuality = probe_max.wCompQuality;
355 probe->wCompWindowSize = probe_min.wCompWindowSize;
356 }
357
358done:
359 return ret;
360}
361
362static int uvc_commit_video(struct uvc_streaming *stream,
363 struct uvc_streaming_control *probe)
364{
365 return uvc_set_video_ctrl(stream, probe, 0);
366}
367
368
369
370
371
372static inline void uvc_video_get_ts(struct timespec *ts)
373{
374 if (uvc_clock_param == CLOCK_MONOTONIC)
375 ktime_get_ts(ts);
376 else
377 ktime_get_real_ts(ts);
378}
379
380static void
381uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf,
382 const __u8 *data, int len)
383{
384 struct uvc_clock_sample *sample;
385 unsigned int header_size;
386 bool has_pts = false;
387 bool has_scr = false;
388 unsigned long flags;
389 struct timespec ts;
390 u16 host_sof;
391 u16 dev_sof;
392
393 switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) {
394 case UVC_STREAM_PTS | UVC_STREAM_SCR:
395 header_size = 12;
396 has_pts = true;
397 has_scr = true;
398 break;
399 case UVC_STREAM_PTS:
400 header_size = 6;
401 has_pts = true;
402 break;
403 case UVC_STREAM_SCR:
404 header_size = 8;
405 has_scr = true;
406 break;
407 default:
408 header_size = 2;
409 break;
410 }
411
412
413 if (len < header_size)
414 return;
415
416
417
418
419
420
421
422
423 if (has_pts && buf != NULL)
424 buf->pts = get_unaligned_le32(&data[2]);
425
426 if (!has_scr)
427 return;
428
429
430
431
432 dev_sof = get_unaligned_le16(&data[header_size - 2]);
433 if (dev_sof == stream->clock.last_sof)
434 return;
435
436 stream->clock.last_sof = dev_sof;
437
438 host_sof = usb_get_current_frame_number(stream->dev->udev);
439 uvc_video_get_ts(&ts);
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460 if (stream->clock.sof_offset == (u16)-1) {
461 u16 delta_sof = (host_sof - dev_sof) & 255;
462 if (delta_sof >= 10)
463 stream->clock.sof_offset = delta_sof;
464 else
465 stream->clock.sof_offset = 0;
466 }
467
468 dev_sof = (dev_sof + stream->clock.sof_offset) & 2047;
469
470 spin_lock_irqsave(&stream->clock.lock, flags);
471
472 sample = &stream->clock.samples[stream->clock.head];
473 sample->dev_stc = get_unaligned_le32(&data[header_size - 6]);
474 sample->dev_sof = dev_sof;
475 sample->host_sof = host_sof;
476 sample->host_ts = ts;
477
478
479 stream->clock.head = (stream->clock.head + 1) % stream->clock.size;
480
481 if (stream->clock.count < stream->clock.size)
482 stream->clock.count++;
483
484 spin_unlock_irqrestore(&stream->clock.lock, flags);
485}
486
487static void uvc_video_clock_reset(struct uvc_streaming *stream)
488{
489 struct uvc_clock *clock = &stream->clock;
490
491 clock->head = 0;
492 clock->count = 0;
493 clock->last_sof = -1;
494 clock->sof_offset = -1;
495}
496
497static int uvc_video_clock_init(struct uvc_streaming *stream)
498{
499 struct uvc_clock *clock = &stream->clock;
500
501 spin_lock_init(&clock->lock);
502 clock->size = 32;
503
504 clock->samples = kmalloc(clock->size * sizeof(*clock->samples),
505 GFP_KERNEL);
506 if (clock->samples == NULL)
507 return -ENOMEM;
508
509 uvc_video_clock_reset(stream);
510
511 return 0;
512}
513
514static void uvc_video_clock_cleanup(struct uvc_streaming *stream)
515{
516 kfree(stream->clock.samples);
517 stream->clock.samples = NULL;
518}
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537static u16 uvc_video_clock_host_sof(const struct uvc_clock_sample *sample)
538{
539
540 s8 delta_sof;
541
542 delta_sof = (sample->host_sof - sample->dev_sof) & 255;
543
544 return (sample->dev_sof + delta_sof) & 2047;
545}
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608void uvc_video_clock_update(struct uvc_streaming *stream,
609 struct v4l2_buffer *v4l2_buf,
610 struct uvc_buffer *buf)
611{
612 struct uvc_clock *clock = &stream->clock;
613 struct uvc_clock_sample *first;
614 struct uvc_clock_sample *last;
615 unsigned long flags;
616 struct timespec ts;
617 u32 delta_stc;
618 u32 y1, y2;
619 u32 x1, x2;
620 u32 mean;
621 u32 sof;
622 u32 div;
623 u32 rem;
624 u64 y;
625
626 spin_lock_irqsave(&clock->lock, flags);
627
628 if (clock->count < clock->size)
629 goto done;
630
631 first = &clock->samples[clock->head];
632 last = &clock->samples[(clock->head - 1) % clock->size];
633
634
635 delta_stc = buf->pts - (1UL << 31);
636 x1 = first->dev_stc - delta_stc;
637 x2 = last->dev_stc - delta_stc;
638 if (x1 == x2)
639 goto done;
640
641 y1 = (first->dev_sof + 2048) << 16;
642 y2 = (last->dev_sof + 2048) << 16;
643 if (y2 < y1)
644 y2 += 2048 << 16;
645
646 y = (u64)(y2 - y1) * (1ULL << 31) + (u64)y1 * (u64)x2
647 - (u64)y2 * (u64)x1;
648 y = div_u64(y, x2 - x1);
649
650 sof = y;
651
652 uvc_trace(UVC_TRACE_CLOCK, "%s: PTS %u y %llu.%06llu SOF %u.%06llu "
653 "(x1 %u x2 %u y1 %u y2 %u SOF offset %u)\n",
654 stream->dev->name, buf->pts,
655 y >> 16, div_u64((y & 0xffff) * 1000000, 65536),
656 sof >> 16, div_u64(((u64)sof & 0xffff) * 1000000LLU, 65536),
657 x1, x2, y1, y2, clock->sof_offset);
658
659
660 x1 = (uvc_video_clock_host_sof(first) + 2048) << 16;
661 x2 = (uvc_video_clock_host_sof(last) + 2048) << 16;
662 if (x2 < x1)
663 x2 += 2048 << 16;
664 if (x1 == x2)
665 goto done;
666
667 ts = timespec_sub(last->host_ts, first->host_ts);
668 y1 = NSEC_PER_SEC;
669 y2 = (ts.tv_sec + 1) * NSEC_PER_SEC + ts.tv_nsec;
670
671
672
673
674
675
676 mean = (x1 + x2) / 2;
677 if (mean - (1024 << 16) > sof)
678 sof += 2048 << 16;
679 else if (sof > mean + (1024 << 16))
680 sof -= 2048 << 16;
681
682 y = (u64)(y2 - y1) * (u64)sof + (u64)y1 * (u64)x2
683 - (u64)y2 * (u64)x1;
684 y = div_u64(y, x2 - x1);
685
686 div = div_u64_rem(y, NSEC_PER_SEC, &rem);
687 ts.tv_sec = first->host_ts.tv_sec - 1 + div;
688 ts.tv_nsec = first->host_ts.tv_nsec + rem;
689 if (ts.tv_nsec >= NSEC_PER_SEC) {
690 ts.tv_sec++;
691 ts.tv_nsec -= NSEC_PER_SEC;
692 }
693
694 uvc_trace(UVC_TRACE_CLOCK, "%s: SOF %u.%06llu y %llu ts %lu.%06lu "
695 "buf ts %lu.%06lu (x1 %u/%u/%u x2 %u/%u/%u y1 %u y2 %u)\n",
696 stream->dev->name,
697 sof >> 16, div_u64(((u64)sof & 0xffff) * 1000000LLU, 65536),
698 y, ts.tv_sec, ts.tv_nsec / NSEC_PER_USEC,
699 v4l2_buf->timestamp.tv_sec,
700 (unsigned long)v4l2_buf->timestamp.tv_usec,
701 x1, first->host_sof, first->dev_sof,
702 x2, last->host_sof, last->dev_sof, y1, y2);
703
704
705 v4l2_buf->timestamp.tv_sec = ts.tv_sec;
706 v4l2_buf->timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
707
708done:
709 spin_unlock_irqrestore(&stream->clock.lock, flags);
710}
711
712
713
714
715
716static void uvc_video_stats_decode(struct uvc_streaming *stream,
717 const __u8 *data, int len)
718{
719 unsigned int header_size;
720 bool has_pts = false;
721 bool has_scr = false;
722 u16 uninitialized_var(scr_sof);
723 u32 uninitialized_var(scr_stc);
724 u32 uninitialized_var(pts);
725
726 if (stream->stats.stream.nb_frames == 0 &&
727 stream->stats.frame.nb_packets == 0)
728 ktime_get_ts(&stream->stats.stream.start_ts);
729
730 switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) {
731 case UVC_STREAM_PTS | UVC_STREAM_SCR:
732 header_size = 12;
733 has_pts = true;
734 has_scr = true;
735 break;
736 case UVC_STREAM_PTS:
737 header_size = 6;
738 has_pts = true;
739 break;
740 case UVC_STREAM_SCR:
741 header_size = 8;
742 has_scr = true;
743 break;
744 default:
745 header_size = 2;
746 break;
747 }
748
749
750 if (len < header_size || data[0] < header_size) {
751 stream->stats.frame.nb_invalid++;
752 return;
753 }
754
755
756 if (has_pts)
757 pts = get_unaligned_le32(&data[2]);
758
759 if (has_scr) {
760 scr_stc = get_unaligned_le32(&data[header_size - 6]);
761 scr_sof = get_unaligned_le16(&data[header_size - 2]);
762 }
763
764
765 if (has_pts && stream->stats.frame.nb_pts) {
766 if (stream->stats.frame.pts != pts) {
767 stream->stats.frame.nb_pts_diffs++;
768 stream->stats.frame.last_pts_diff =
769 stream->stats.frame.nb_packets;
770 }
771 }
772
773 if (has_pts) {
774 stream->stats.frame.nb_pts++;
775 stream->stats.frame.pts = pts;
776 }
777
778
779
780
781 if (stream->stats.frame.size == 0) {
782 if (len > header_size)
783 stream->stats.frame.has_initial_pts = has_pts;
784 if (len == header_size && has_pts)
785 stream->stats.frame.has_early_pts = true;
786 }
787
788
789 if (has_scr && stream->stats.frame.nb_scr) {
790 if (stream->stats.frame.scr_stc != scr_stc)
791 stream->stats.frame.nb_scr_diffs++;
792 }
793
794 if (has_scr) {
795
796 if (stream->stats.stream.nb_frames > 0 ||
797 stream->stats.frame.nb_scr > 0)
798 stream->stats.stream.scr_sof_count +=
799 (scr_sof - stream->stats.stream.scr_sof) % 2048;
800 stream->stats.stream.scr_sof = scr_sof;
801
802 stream->stats.frame.nb_scr++;
803 stream->stats.frame.scr_stc = scr_stc;
804 stream->stats.frame.scr_sof = scr_sof;
805
806 if (scr_sof < stream->stats.stream.min_sof)
807 stream->stats.stream.min_sof = scr_sof;
808 if (scr_sof > stream->stats.stream.max_sof)
809 stream->stats.stream.max_sof = scr_sof;
810 }
811
812
813 if (stream->stats.frame.size == 0 && len > header_size)
814 stream->stats.frame.first_data = stream->stats.frame.nb_packets;
815
816
817 stream->stats.frame.size += len - header_size;
818
819
820 stream->stats.frame.nb_packets++;
821 if (len > header_size)
822 stream->stats.frame.nb_empty++;
823
824 if (data[1] & UVC_STREAM_ERR)
825 stream->stats.frame.nb_errors++;
826}
827
828static void uvc_video_stats_update(struct uvc_streaming *stream)
829{
830 struct uvc_stats_frame *frame = &stream->stats.frame;
831
832 uvc_trace(UVC_TRACE_STATS, "frame %u stats: %u/%u/%u packets, "
833 "%u/%u/%u pts (%searly %sinitial), %u/%u scr, "
834 "last pts/stc/sof %u/%u/%u\n",
835 stream->sequence, frame->first_data,
836 frame->nb_packets - frame->nb_empty, frame->nb_packets,
837 frame->nb_pts_diffs, frame->last_pts_diff, frame->nb_pts,
838 frame->has_early_pts ? "" : "!",
839 frame->has_initial_pts ? "" : "!",
840 frame->nb_scr_diffs, frame->nb_scr,
841 frame->pts, frame->scr_stc, frame->scr_sof);
842
843 stream->stats.stream.nb_frames++;
844 stream->stats.stream.nb_packets += stream->stats.frame.nb_packets;
845 stream->stats.stream.nb_empty += stream->stats.frame.nb_empty;
846 stream->stats.stream.nb_errors += stream->stats.frame.nb_errors;
847 stream->stats.stream.nb_invalid += stream->stats.frame.nb_invalid;
848
849 if (frame->has_early_pts)
850 stream->stats.stream.nb_pts_early++;
851 if (frame->has_initial_pts)
852 stream->stats.stream.nb_pts_initial++;
853 if (frame->last_pts_diff <= frame->first_data)
854 stream->stats.stream.nb_pts_constant++;
855 if (frame->nb_scr >= frame->nb_packets - frame->nb_empty)
856 stream->stats.stream.nb_scr_count_ok++;
857 if (frame->nb_scr_diffs + 1 == frame->nb_scr)
858 stream->stats.stream.nb_scr_diffs_ok++;
859
860 memset(&stream->stats.frame, 0, sizeof(stream->stats.frame));
861}
862
863size_t uvc_video_stats_dump(struct uvc_streaming *stream, char *buf,
864 size_t size)
865{
866 unsigned int scr_sof_freq;
867 unsigned int duration;
868 struct timespec ts;
869 size_t count = 0;
870
871 ts.tv_sec = stream->stats.stream.stop_ts.tv_sec
872 - stream->stats.stream.start_ts.tv_sec;
873 ts.tv_nsec = stream->stats.stream.stop_ts.tv_nsec
874 - stream->stats.stream.start_ts.tv_nsec;
875 if (ts.tv_nsec < 0) {
876 ts.tv_sec--;
877 ts.tv_nsec += 1000000000;
878 }
879
880
881
882
883 duration = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
884 if (duration != 0)
885 scr_sof_freq = stream->stats.stream.scr_sof_count * 1000
886 / duration;
887 else
888 scr_sof_freq = 0;
889
890 count += scnprintf(buf + count, size - count,
891 "frames: %u\npackets: %u\nempty: %u\n"
892 "errors: %u\ninvalid: %u\n",
893 stream->stats.stream.nb_frames,
894 stream->stats.stream.nb_packets,
895 stream->stats.stream.nb_empty,
896 stream->stats.stream.nb_errors,
897 stream->stats.stream.nb_invalid);
898 count += scnprintf(buf + count, size - count,
899 "pts: %u early, %u initial, %u ok\n",
900 stream->stats.stream.nb_pts_early,
901 stream->stats.stream.nb_pts_initial,
902 stream->stats.stream.nb_pts_constant);
903 count += scnprintf(buf + count, size - count,
904 "scr: %u count ok, %u diff ok\n",
905 stream->stats.stream.nb_scr_count_ok,
906 stream->stats.stream.nb_scr_diffs_ok);
907 count += scnprintf(buf + count, size - count,
908 "sof: %u <= sof <= %u, freq %u.%03u kHz\n",
909 stream->stats.stream.min_sof,
910 stream->stats.stream.max_sof,
911 scr_sof_freq / 1000, scr_sof_freq % 1000);
912
913 return count;
914}
915
916static void uvc_video_stats_start(struct uvc_streaming *stream)
917{
918 memset(&stream->stats, 0, sizeof(stream->stats));
919 stream->stats.stream.min_sof = 2048;
920}
921
922static void uvc_video_stats_stop(struct uvc_streaming *stream)
923{
924 ktime_get_ts(&stream->stats.stream.stop_ts);
925}
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966static int uvc_video_decode_start(struct uvc_streaming *stream,
967 struct uvc_buffer *buf, const __u8 *data, int len)
968{
969 __u8 fid;
970
971
972
973
974
975
976 if (len < 2 || data[0] < 2 || data[0] > len) {
977 stream->stats.frame.nb_invalid++;
978 return -EINVAL;
979 }
980
981 fid = data[1] & UVC_STREAM_FID;
982
983
984
985
986 if (stream->last_fid != fid) {
987 stream->sequence++;
988 if (stream->sequence)
989 uvc_video_stats_update(stream);
990 }
991
992 uvc_video_clock_decode(stream, buf, data, len);
993 uvc_video_stats_decode(stream, data, len);
994
995
996
997
998 if (buf == NULL) {
999 stream->last_fid = fid;
1000 return -ENODATA;
1001 }
1002
1003
1004 if (data[1] & UVC_STREAM_ERR) {
1005 uvc_trace(UVC_TRACE_FRAME, "Marking buffer as bad (error bit "
1006 "set).\n");
1007 buf->error = 1;
1008 }
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018 if (buf->state != UVC_BUF_STATE_ACTIVE) {
1019 struct timespec ts;
1020
1021 if (fid == stream->last_fid) {
1022 uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of "
1023 "sync).\n");
1024 if ((stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) &&
1025 (data[1] & UVC_STREAM_EOF))
1026 stream->last_fid ^= UVC_STREAM_FID;
1027 return -ENODATA;
1028 }
1029
1030 uvc_video_get_ts(&ts);
1031
1032 buf->buf.v4l2_buf.field = V4L2_FIELD_NONE;
1033 buf->buf.v4l2_buf.sequence = stream->sequence;
1034 buf->buf.v4l2_buf.timestamp.tv_sec = ts.tv_sec;
1035 buf->buf.v4l2_buf.timestamp.tv_usec =
1036 ts.tv_nsec / NSEC_PER_USEC;
1037
1038
1039 buf->state = UVC_BUF_STATE_ACTIVE;
1040 }
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057 if (fid != stream->last_fid && buf->bytesused != 0) {
1058 uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit "
1059 "toggled).\n");
1060 buf->state = UVC_BUF_STATE_READY;
1061 return -EAGAIN;
1062 }
1063
1064 stream->last_fid = fid;
1065
1066 return data[0];
1067}
1068
1069static void uvc_video_decode_data(struct uvc_streaming *stream,
1070 struct uvc_buffer *buf, const __u8 *data, int len)
1071{
1072 unsigned int maxlen, nbytes;
1073 void *mem;
1074
1075 if (len <= 0)
1076 return;
1077
1078
1079 maxlen = buf->length - buf->bytesused;
1080 mem = buf->mem + buf->bytesused;
1081 nbytes = min((unsigned int)len, maxlen);
1082 memcpy(mem, data, nbytes);
1083 buf->bytesused += nbytes;
1084
1085
1086 if (len > maxlen) {
1087 uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n");
1088 buf->state = UVC_BUF_STATE_READY;
1089 }
1090}
1091
1092static void uvc_video_decode_end(struct uvc_streaming *stream,
1093 struct uvc_buffer *buf, const __u8 *data, int len)
1094{
1095
1096 if (data[1] & UVC_STREAM_EOF && buf->bytesused != 0) {
1097 uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n");
1098 if (data[0] == len)
1099 uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n");
1100 buf->state = UVC_BUF_STATE_READY;
1101 if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID)
1102 stream->last_fid ^= UVC_STREAM_FID;
1103 }
1104}
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117static int uvc_video_encode_header(struct uvc_streaming *stream,
1118 struct uvc_buffer *buf, __u8 *data, int len)
1119{
1120 data[0] = 2;
1121 data[1] = UVC_STREAM_EOH | UVC_STREAM_EOF
1122 | (stream->last_fid & UVC_STREAM_FID);
1123 return 2;
1124}
1125
1126static int uvc_video_encode_data(struct uvc_streaming *stream,
1127 struct uvc_buffer *buf, __u8 *data, int len)
1128{
1129 struct uvc_video_queue *queue = &stream->queue;
1130 unsigned int nbytes;
1131 void *mem;
1132
1133
1134 mem = buf->mem + queue->buf_used;
1135 nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used);
1136 nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size,
1137 nbytes);
1138 memcpy(data, mem, nbytes);
1139
1140 queue->buf_used += nbytes;
1141
1142 return nbytes;
1143}
1144
1145
1146
1147
1148
1149
1150
1151
1152static void uvc_video_validate_buffer(const struct uvc_streaming *stream,
1153 struct uvc_buffer *buf)
1154{
1155 if (stream->ctrl.dwMaxVideoFrameSize != buf->bytesused &&
1156 !(stream->cur_format->flags & UVC_FMT_FLAG_COMPRESSED))
1157 buf->error = 1;
1158}
1159
1160
1161
1162
1163static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
1164 struct uvc_buffer *buf)
1165{
1166 u8 *mem;
1167 int ret, i;
1168
1169 for (i = 0; i < urb->number_of_packets; ++i) {
1170 if (urb->iso_frame_desc[i].status < 0) {
1171 uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
1172 "lost (%d).\n", urb->iso_frame_desc[i].status);
1173
1174 if (buf != NULL)
1175 buf->error = 1;
1176 continue;
1177 }
1178
1179
1180 mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1181 do {
1182 ret = uvc_video_decode_start(stream, buf, mem,
1183 urb->iso_frame_desc[i].actual_length);
1184 if (ret == -EAGAIN) {
1185 uvc_video_validate_buffer(stream, buf);
1186 buf = uvc_queue_next_buffer(&stream->queue,
1187 buf);
1188 }
1189 } while (ret == -EAGAIN);
1190
1191 if (ret < 0)
1192 continue;
1193
1194
1195 uvc_video_decode_data(stream, buf, mem + ret,
1196 urb->iso_frame_desc[i].actual_length - ret);
1197
1198
1199 uvc_video_decode_end(stream, buf, mem,
1200 urb->iso_frame_desc[i].actual_length);
1201
1202 if (buf->state == UVC_BUF_STATE_READY) {
1203 uvc_video_validate_buffer(stream, buf);
1204 buf = uvc_queue_next_buffer(&stream->queue, buf);
1205 }
1206 }
1207}
1208
1209static void uvc_video_decode_bulk(struct urb *urb, struct uvc_streaming *stream,
1210 struct uvc_buffer *buf)
1211{
1212 u8 *mem;
1213 int len, ret;
1214
1215
1216
1217
1218
1219 if (urb->actual_length == 0 && stream->bulk.header_size == 0)
1220 return;
1221
1222 mem = urb->transfer_buffer;
1223 len = urb->actual_length;
1224 stream->bulk.payload_size += len;
1225
1226
1227
1228
1229 if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) {
1230 do {
1231 ret = uvc_video_decode_start(stream, buf, mem, len);
1232 if (ret == -EAGAIN)
1233 buf = uvc_queue_next_buffer(&stream->queue,
1234 buf);
1235 } while (ret == -EAGAIN);
1236
1237
1238 if (ret < 0 || buf == NULL) {
1239 stream->bulk.skip_payload = 1;
1240 } else {
1241 memcpy(stream->bulk.header, mem, ret);
1242 stream->bulk.header_size = ret;
1243
1244 mem += ret;
1245 len -= ret;
1246 }
1247 }
1248
1249
1250
1251
1252
1253
1254
1255 if (!stream->bulk.skip_payload && buf != NULL)
1256 uvc_video_decode_data(stream, buf, mem, len);
1257
1258
1259
1260
1261 if (urb->actual_length < urb->transfer_buffer_length ||
1262 stream->bulk.payload_size >= stream->bulk.max_payload_size) {
1263 if (!stream->bulk.skip_payload && buf != NULL) {
1264 uvc_video_decode_end(stream, buf, stream->bulk.header,
1265 stream->bulk.payload_size);
1266 if (buf->state == UVC_BUF_STATE_READY)
1267 buf = uvc_queue_next_buffer(&stream->queue,
1268 buf);
1269 }
1270
1271 stream->bulk.header_size = 0;
1272 stream->bulk.skip_payload = 0;
1273 stream->bulk.payload_size = 0;
1274 }
1275}
1276
1277static void uvc_video_encode_bulk(struct urb *urb, struct uvc_streaming *stream,
1278 struct uvc_buffer *buf)
1279{
1280 u8 *mem = urb->transfer_buffer;
1281 int len = stream->urb_size, ret;
1282
1283 if (buf == NULL) {
1284 urb->transfer_buffer_length = 0;
1285 return;
1286 }
1287
1288
1289 if (stream->bulk.header_size == 0) {
1290 ret = uvc_video_encode_header(stream, buf, mem, len);
1291 stream->bulk.header_size = ret;
1292 stream->bulk.payload_size += ret;
1293 mem += ret;
1294 len -= ret;
1295 }
1296
1297
1298 ret = uvc_video_encode_data(stream, buf, mem, len);
1299
1300 stream->bulk.payload_size += ret;
1301 len -= ret;
1302
1303 if (buf->bytesused == stream->queue.buf_used ||
1304 stream->bulk.payload_size == stream->bulk.max_payload_size) {
1305 if (buf->bytesused == stream->queue.buf_used) {
1306 stream->queue.buf_used = 0;
1307 buf->state = UVC_BUF_STATE_READY;
1308 buf->buf.v4l2_buf.sequence = ++stream->sequence;
1309 uvc_queue_next_buffer(&stream->queue, buf);
1310 stream->last_fid ^= UVC_STREAM_FID;
1311 }
1312
1313 stream->bulk.header_size = 0;
1314 stream->bulk.payload_size = 0;
1315 }
1316
1317 urb->transfer_buffer_length = stream->urb_size - len;
1318}
1319
1320static void uvc_video_complete(struct urb *urb)
1321{
1322 struct uvc_streaming *stream = urb->context;
1323 struct uvc_video_queue *queue = &stream->queue;
1324 struct uvc_buffer *buf = NULL;
1325 unsigned long flags;
1326 int ret;
1327
1328 switch (urb->status) {
1329 case 0:
1330 break;
1331
1332 default:
1333 uvc_printk(KERN_WARNING, "Non-zero status (%d) in video "
1334 "completion handler.\n", urb->status);
1335
1336 case -ENOENT:
1337 if (stream->frozen)
1338 return;
1339
1340 case -ECONNRESET:
1341 case -ESHUTDOWN:
1342 uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
1343 return;
1344 }
1345
1346 spin_lock_irqsave(&queue->irqlock, flags);
1347 if (!list_empty(&queue->irqqueue))
1348 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
1349 queue);
1350 spin_unlock_irqrestore(&queue->irqlock, flags);
1351
1352 stream->decode(urb, stream, buf);
1353
1354 if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
1355 uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n",
1356 ret);
1357 }
1358}
1359
1360
1361
1362
1363static void uvc_free_urb_buffers(struct uvc_streaming *stream)
1364{
1365 unsigned int i;
1366
1367 for (i = 0; i < UVC_URBS; ++i) {
1368 if (stream->urb_buffer[i]) {
1369#ifndef CONFIG_DMA_NONCOHERENT
1370 usb_free_coherent(stream->dev->udev, stream->urb_size,
1371 stream->urb_buffer[i], stream->urb_dma[i]);
1372#else
1373 kfree(stream->urb_buffer[i]);
1374#endif
1375 stream->urb_buffer[i] = NULL;
1376 }
1377 }
1378
1379 stream->urb_size = 0;
1380}
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393static int uvc_alloc_urb_buffers(struct uvc_streaming *stream,
1394 unsigned int size, unsigned int psize, gfp_t gfp_flags)
1395{
1396 unsigned int npackets;
1397 unsigned int i;
1398
1399
1400 if (stream->urb_size)
1401 return stream->urb_size / psize;
1402
1403
1404
1405
1406 npackets = DIV_ROUND_UP(size, psize);
1407 if (npackets > UVC_MAX_PACKETS)
1408 npackets = UVC_MAX_PACKETS;
1409
1410
1411 for (; npackets > 1; npackets /= 2) {
1412 for (i = 0; i < UVC_URBS; ++i) {
1413 stream->urb_size = psize * npackets;
1414#ifndef CONFIG_DMA_NONCOHERENT
1415 stream->urb_buffer[i] = usb_alloc_coherent(
1416 stream->dev->udev, stream->urb_size,
1417 gfp_flags | __GFP_NOWARN, &stream->urb_dma[i]);
1418#else
1419 stream->urb_buffer[i] =
1420 kmalloc(stream->urb_size, gfp_flags | __GFP_NOWARN);
1421#endif
1422 if (!stream->urb_buffer[i]) {
1423 uvc_free_urb_buffers(stream);
1424 break;
1425 }
1426 }
1427
1428 if (i == UVC_URBS) {
1429 uvc_trace(UVC_TRACE_VIDEO, "Allocated %u URB buffers "
1430 "of %ux%u bytes each.\n", UVC_URBS, npackets,
1431 psize);
1432 return npackets;
1433 }
1434 }
1435
1436 uvc_trace(UVC_TRACE_VIDEO, "Failed to allocate URB buffers (%u bytes "
1437 "per packet).\n", psize);
1438 return 0;
1439}
1440
1441
1442
1443
1444static void uvc_uninit_video(struct uvc_streaming *stream, int free_buffers)
1445{
1446 struct urb *urb;
1447 unsigned int i;
1448
1449 uvc_video_stats_stop(stream);
1450
1451 for (i = 0; i < UVC_URBS; ++i) {
1452 urb = stream->urb[i];
1453 if (urb == NULL)
1454 continue;
1455
1456 usb_kill_urb(urb);
1457 usb_free_urb(urb);
1458 stream->urb[i] = NULL;
1459 }
1460
1461 if (free_buffers)
1462 uvc_free_urb_buffers(stream);
1463}
1464
1465
1466
1467
1468static unsigned int uvc_endpoint_max_bpi(struct usb_device *dev,
1469 struct usb_host_endpoint *ep)
1470{
1471 u16 psize;
1472
1473 switch (dev->speed) {
1474 case USB_SPEED_SUPER:
1475 return le16_to_cpu(ep->ss_ep_comp.wBytesPerInterval);
1476 case USB_SPEED_HIGH:
1477 psize = usb_endpoint_maxp(&ep->desc);
1478 return (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
1479 case USB_SPEED_WIRELESS:
1480 psize = usb_endpoint_maxp(&ep->desc);
1481 return psize;
1482 default:
1483 psize = usb_endpoint_maxp(&ep->desc);
1484 return psize & 0x07ff;
1485 }
1486}
1487
1488
1489
1490
1491
1492static int uvc_init_video_isoc(struct uvc_streaming *stream,
1493 struct usb_host_endpoint *ep, gfp_t gfp_flags)
1494{
1495 struct urb *urb;
1496 unsigned int npackets, i, j;
1497 u16 psize;
1498 u32 size;
1499
1500 psize = uvc_endpoint_max_bpi(stream->dev->udev, ep);
1501 size = stream->ctrl.dwMaxVideoFrameSize;
1502
1503 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
1504 if (npackets == 0)
1505 return -ENOMEM;
1506
1507 size = npackets * psize;
1508
1509 for (i = 0; i < UVC_URBS; ++i) {
1510 urb = usb_alloc_urb(npackets, gfp_flags);
1511 if (urb == NULL) {
1512 uvc_uninit_video(stream, 1);
1513 return -ENOMEM;
1514 }
1515
1516 urb->dev = stream->dev->udev;
1517 urb->context = stream;
1518 urb->pipe = usb_rcvisocpipe(stream->dev->udev,
1519 ep->desc.bEndpointAddress);
1520#ifndef CONFIG_DMA_NONCOHERENT
1521 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
1522 urb->transfer_dma = stream->urb_dma[i];
1523#else
1524 urb->transfer_flags = URB_ISO_ASAP;
1525#endif
1526 urb->interval = ep->desc.bInterval;
1527 urb->transfer_buffer = stream->urb_buffer[i];
1528 urb->complete = uvc_video_complete;
1529 urb->number_of_packets = npackets;
1530 urb->transfer_buffer_length = size;
1531
1532 for (j = 0; j < npackets; ++j) {
1533 urb->iso_frame_desc[j].offset = j * psize;
1534 urb->iso_frame_desc[j].length = psize;
1535 }
1536
1537 stream->urb[i] = urb;
1538 }
1539
1540 return 0;
1541}
1542
1543
1544
1545
1546
1547static int uvc_init_video_bulk(struct uvc_streaming *stream,
1548 struct usb_host_endpoint *ep, gfp_t gfp_flags)
1549{
1550 struct urb *urb;
1551 unsigned int npackets, pipe, i;
1552 u16 psize;
1553 u32 size;
1554
1555 psize = usb_endpoint_maxp(&ep->desc) & 0x7ff;
1556 size = stream->ctrl.dwMaxPayloadTransferSize;
1557 stream->bulk.max_payload_size = size;
1558
1559 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
1560 if (npackets == 0)
1561 return -ENOMEM;
1562
1563 size = npackets * psize;
1564
1565 if (usb_endpoint_dir_in(&ep->desc))
1566 pipe = usb_rcvbulkpipe(stream->dev->udev,
1567 ep->desc.bEndpointAddress);
1568 else
1569 pipe = usb_sndbulkpipe(stream->dev->udev,
1570 ep->desc.bEndpointAddress);
1571
1572 if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1573 size = 0;
1574
1575 for (i = 0; i < UVC_URBS; ++i) {
1576 urb = usb_alloc_urb(0, gfp_flags);
1577 if (urb == NULL) {
1578 uvc_uninit_video(stream, 1);
1579 return -ENOMEM;
1580 }
1581
1582 usb_fill_bulk_urb(urb, stream->dev->udev, pipe,
1583 stream->urb_buffer[i], size, uvc_video_complete,
1584 stream);
1585#ifndef CONFIG_DMA_NONCOHERENT
1586 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1587 urb->transfer_dma = stream->urb_dma[i];
1588#endif
1589
1590 stream->urb[i] = urb;
1591 }
1592
1593 return 0;
1594}
1595
1596
1597
1598
1599static int uvc_init_video(struct uvc_streaming *stream, gfp_t gfp_flags)
1600{
1601 struct usb_interface *intf = stream->intf;
1602 struct usb_host_endpoint *ep;
1603 unsigned int i;
1604 int ret;
1605
1606 stream->sequence = -1;
1607 stream->last_fid = -1;
1608 stream->bulk.header_size = 0;
1609 stream->bulk.skip_payload = 0;
1610 stream->bulk.payload_size = 0;
1611
1612 uvc_video_stats_start(stream);
1613
1614 if (intf->num_altsetting > 1) {
1615 struct usb_host_endpoint *best_ep = NULL;
1616 unsigned int best_psize = UINT_MAX;
1617 unsigned int bandwidth;
1618 unsigned int uninitialized_var(altsetting);
1619 int intfnum = stream->intfnum;
1620
1621
1622 bandwidth = stream->ctrl.dwMaxPayloadTransferSize;
1623
1624 if (bandwidth == 0) {
1625 uvc_trace(UVC_TRACE_VIDEO, "Device requested null "
1626 "bandwidth, defaulting to lowest.\n");
1627 bandwidth = 1;
1628 } else {
1629 uvc_trace(UVC_TRACE_VIDEO, "Device requested %u "
1630 "B/frame bandwidth.\n", bandwidth);
1631 }
1632
1633 for (i = 0; i < intf->num_altsetting; ++i) {
1634 struct usb_host_interface *alts;
1635 unsigned int psize;
1636
1637 alts = &intf->altsetting[i];
1638 ep = uvc_find_endpoint(alts,
1639 stream->header.bEndpointAddress);
1640 if (ep == NULL)
1641 continue;
1642
1643
1644 psize = uvc_endpoint_max_bpi(stream->dev->udev, ep);
1645 if (psize >= bandwidth && psize <= best_psize) {
1646 altsetting = alts->desc.bAlternateSetting;
1647 best_psize = psize;
1648 best_ep = ep;
1649 }
1650 }
1651
1652 if (best_ep == NULL) {
1653 uvc_trace(UVC_TRACE_VIDEO, "No fast enough alt setting "
1654 "for requested bandwidth.\n");
1655 return -EIO;
1656 }
1657
1658 uvc_trace(UVC_TRACE_VIDEO, "Selecting alternate setting %u "
1659 "(%u B/frame bandwidth).\n", altsetting, best_psize);
1660
1661 ret = usb_set_interface(stream->dev->udev, intfnum, altsetting);
1662 if (ret < 0)
1663 return ret;
1664
1665 ret = uvc_init_video_isoc(stream, best_ep, gfp_flags);
1666 } else {
1667
1668 ep = uvc_find_endpoint(&intf->altsetting[0],
1669 stream->header.bEndpointAddress);
1670 if (ep == NULL)
1671 return -EIO;
1672
1673 ret = uvc_init_video_bulk(stream, ep, gfp_flags);
1674 }
1675
1676 if (ret < 0)
1677 return ret;
1678
1679
1680 for (i = 0; i < UVC_URBS; ++i) {
1681 ret = usb_submit_urb(stream->urb[i], gfp_flags);
1682 if (ret < 0) {
1683 uvc_printk(KERN_ERR, "Failed to submit URB %u "
1684 "(%d).\n", i, ret);
1685 uvc_uninit_video(stream, 1);
1686 return ret;
1687 }
1688 }
1689
1690
1691
1692
1693 if (stream->dev->quirks & UVC_QUIRK_RESTORE_CTRLS_ON_INIT)
1694 uvc_ctrl_restore_values(stream->dev);
1695
1696 return 0;
1697}
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710int uvc_video_suspend(struct uvc_streaming *stream)
1711{
1712 if (!uvc_queue_streaming(&stream->queue))
1713 return 0;
1714
1715 stream->frozen = 1;
1716 uvc_uninit_video(stream, 0);
1717 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1718 return 0;
1719}
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729int uvc_video_resume(struct uvc_streaming *stream, int reset)
1730{
1731 int ret;
1732
1733
1734
1735
1736
1737
1738 if (reset)
1739 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1740
1741 stream->frozen = 0;
1742
1743 uvc_video_clock_reset(stream);
1744
1745 if (!uvc_queue_streaming(&stream->queue))
1746 return 0;
1747
1748 ret = uvc_commit_video(stream, &stream->ctrl);
1749 if (ret < 0)
1750 return ret;
1751
1752 return uvc_init_video(stream, GFP_NOIO);
1753}
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769int uvc_video_init(struct uvc_streaming *stream)
1770{
1771 struct uvc_streaming_control *probe = &stream->ctrl;
1772 struct uvc_format *format = NULL;
1773 struct uvc_frame *frame = NULL;
1774 unsigned int i;
1775 int ret;
1776
1777 if (stream->nformats == 0) {
1778 uvc_printk(KERN_INFO, "No supported video formats found.\n");
1779 return -EINVAL;
1780 }
1781
1782 atomic_set(&stream->active, 0);
1783
1784
1785
1786
1787
1788
1789 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1790
1791
1792
1793
1794
1795
1796 if (uvc_get_video_ctrl(stream, probe, 1, UVC_GET_DEF) == 0)
1797 uvc_set_video_ctrl(stream, probe, 1);
1798
1799
1800
1801
1802
1803
1804 ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
1805 if (ret < 0)
1806 return ret;
1807
1808
1809
1810
1811 for (i = stream->nformats; i > 0; --i) {
1812 format = &stream->format[i-1];
1813 if (format->index == probe->bFormatIndex)
1814 break;
1815 }
1816
1817 if (format->nframes == 0) {
1818 uvc_printk(KERN_INFO, "No frame descriptor found for the "
1819 "default format.\n");
1820 return -EINVAL;
1821 }
1822
1823
1824
1825
1826
1827
1828 for (i = format->nframes; i > 0; --i) {
1829 frame = &format->frame[i-1];
1830 if (frame->bFrameIndex == probe->bFrameIndex)
1831 break;
1832 }
1833
1834 probe->bFormatIndex = format->index;
1835 probe->bFrameIndex = frame->bFrameIndex;
1836
1837 stream->def_format = format;
1838 stream->cur_format = format;
1839 stream->cur_frame = frame;
1840
1841
1842 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1843 if (stream->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
1844 stream->decode = uvc_video_decode_isight;
1845 else if (stream->intf->num_altsetting > 1)
1846 stream->decode = uvc_video_decode_isoc;
1847 else
1848 stream->decode = uvc_video_decode_bulk;
1849 } else {
1850 if (stream->intf->num_altsetting == 1)
1851 stream->decode = uvc_video_encode_bulk;
1852 else {
1853 uvc_printk(KERN_INFO, "Isochronous endpoints are not "
1854 "supported for video output devices.\n");
1855 return -EINVAL;
1856 }
1857 }
1858
1859 return 0;
1860}
1861
1862
1863
1864
1865int uvc_video_enable(struct uvc_streaming *stream, int enable)
1866{
1867 int ret;
1868
1869 if (!enable) {
1870 uvc_uninit_video(stream, 1);
1871 if (stream->intf->num_altsetting > 1) {
1872 usb_set_interface(stream->dev->udev,
1873 stream->intfnum, 0);
1874 } else {
1875
1876
1877
1878
1879
1880 unsigned int epnum = stream->header.bEndpointAddress
1881 & USB_ENDPOINT_NUMBER_MASK;
1882 unsigned int dir = stream->header.bEndpointAddress
1883 & USB_ENDPOINT_DIR_MASK;
1884 unsigned int pipe;
1885
1886 pipe = usb_sndbulkpipe(stream->dev->udev, epnum) | dir;
1887 usb_clear_halt(stream->dev->udev, pipe);
1888 }
1889
1890 uvc_video_clock_cleanup(stream);
1891 return 0;
1892 }
1893
1894 ret = uvc_video_clock_init(stream);
1895 if (ret < 0)
1896 return ret;
1897
1898
1899 ret = uvc_commit_video(stream, &stream->ctrl);
1900 if (ret < 0)
1901 goto error_commit;
1902
1903 ret = uvc_init_video(stream, GFP_KERNEL);
1904 if (ret < 0)
1905 goto error_video;
1906
1907 return 0;
1908
1909error_video:
1910 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1911error_commit:
1912 uvc_video_clock_cleanup(stream);
1913
1914 return ret;
1915}
1916