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