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