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 <asm/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
354int 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
364
365#define UVC_STREAM_EOH (1 << 7)
366#define UVC_STREAM_ERR (1 << 6)
367#define UVC_STREAM_STI (1 << 5)
368#define UVC_STREAM_RES (1 << 4)
369#define UVC_STREAM_SCR (1 << 3)
370#define UVC_STREAM_PTS (1 << 2)
371#define UVC_STREAM_EOF (1 << 1)
372#define UVC_STREAM_FID (1 << 0)
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409static int uvc_video_decode_start(struct uvc_streaming *stream,
410 struct uvc_buffer *buf, const __u8 *data, int len)
411{
412 __u8 fid;
413
414
415
416
417
418
419 if (len < 2 || data[0] < 2 || data[0] > len)
420 return -EINVAL;
421
422
423 if (data[1] & UVC_STREAM_ERR) {
424 uvc_trace(UVC_TRACE_FRAME, "Dropping payload (error bit "
425 "set).\n");
426 return -ENODATA;
427 }
428
429 fid = data[1] & UVC_STREAM_FID;
430
431
432
433
434 if (stream->last_fid != fid)
435 stream->sequence++;
436
437
438
439
440 if (buf == NULL) {
441 stream->last_fid = fid;
442 return -ENODATA;
443 }
444
445
446
447
448
449
450
451
452
453 if (buf->state != UVC_BUF_STATE_ACTIVE) {
454 struct timespec ts;
455
456 if (fid == stream->last_fid) {
457 uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of "
458 "sync).\n");
459 if ((stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) &&
460 (data[1] & UVC_STREAM_EOF))
461 stream->last_fid ^= UVC_STREAM_FID;
462 return -ENODATA;
463 }
464
465 if (uvc_clock_param == CLOCK_MONOTONIC)
466 ktime_get_ts(&ts);
467 else
468 ktime_get_real_ts(&ts);
469
470 buf->buf.sequence = stream->sequence;
471 buf->buf.timestamp.tv_sec = ts.tv_sec;
472 buf->buf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
473
474
475 buf->state = UVC_BUF_STATE_ACTIVE;
476 }
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493 if (fid != stream->last_fid && buf->buf.bytesused != 0) {
494 uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit "
495 "toggled).\n");
496 buf->state = UVC_BUF_STATE_READY;
497 return -EAGAIN;
498 }
499
500 stream->last_fid = fid;
501
502 return data[0];
503}
504
505static void uvc_video_decode_data(struct uvc_streaming *stream,
506 struct uvc_buffer *buf, const __u8 *data, int len)
507{
508 struct uvc_video_queue *queue = &stream->queue;
509 unsigned int maxlen, nbytes;
510 void *mem;
511
512 if (len <= 0)
513 return;
514
515
516 maxlen = buf->buf.length - buf->buf.bytesused;
517 mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused;
518 nbytes = min((unsigned int)len, maxlen);
519 memcpy(mem, data, nbytes);
520 buf->buf.bytesused += nbytes;
521
522
523 if (len > maxlen) {
524 uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n");
525 buf->state = UVC_BUF_STATE_READY;
526 }
527}
528
529static void uvc_video_decode_end(struct uvc_streaming *stream,
530 struct uvc_buffer *buf, const __u8 *data, int len)
531{
532
533 if (data[1] & UVC_STREAM_EOF && buf->buf.bytesused != 0) {
534 uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n");
535 if (data[0] == len)
536 uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n");
537 buf->state = UVC_BUF_STATE_READY;
538 if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID)
539 stream->last_fid ^= UVC_STREAM_FID;
540 }
541}
542
543
544
545
546
547
548
549
550
551
552
553
554static int uvc_video_encode_header(struct uvc_streaming *stream,
555 struct uvc_buffer *buf, __u8 *data, int len)
556{
557 data[0] = 2;
558 data[1] = UVC_STREAM_EOH | UVC_STREAM_EOF
559 | (stream->last_fid & UVC_STREAM_FID);
560 return 2;
561}
562
563static int uvc_video_encode_data(struct uvc_streaming *stream,
564 struct uvc_buffer *buf, __u8 *data, int len)
565{
566 struct uvc_video_queue *queue = &stream->queue;
567 unsigned int nbytes;
568 void *mem;
569
570
571 mem = queue->mem + buf->buf.m.offset + queue->buf_used;
572 nbytes = min((unsigned int)len, buf->buf.bytesused - queue->buf_used);
573 nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size,
574 nbytes);
575 memcpy(data, mem, nbytes);
576
577 queue->buf_used += nbytes;
578
579 return nbytes;
580}
581
582
583
584
585
586
587
588
589static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
590 struct uvc_buffer *buf)
591{
592 u8 *mem;
593 int ret, i;
594
595 for (i = 0; i < urb->number_of_packets; ++i) {
596 if (urb->iso_frame_desc[i].status < 0) {
597 uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
598 "lost (%d).\n", urb->iso_frame_desc[i].status);
599
600 if (buf != NULL)
601 buf->error = 1;
602 continue;
603 }
604
605
606 mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
607 do {
608 ret = uvc_video_decode_start(stream, buf, mem,
609 urb->iso_frame_desc[i].actual_length);
610 if (ret == -EAGAIN)
611 buf = uvc_queue_next_buffer(&stream->queue,
612 buf);
613 } while (ret == -EAGAIN);
614
615 if (ret < 0)
616 continue;
617
618
619 uvc_video_decode_data(stream, buf, mem + ret,
620 urb->iso_frame_desc[i].actual_length - ret);
621
622
623 uvc_video_decode_end(stream, buf, mem,
624 urb->iso_frame_desc[i].actual_length);
625
626 if (buf->state == UVC_BUF_STATE_READY) {
627 if (buf->buf.length != buf->buf.bytesused &&
628 !(stream->cur_format->flags &
629 UVC_FMT_FLAG_COMPRESSED))
630 buf->error = 1;
631
632 buf = uvc_queue_next_buffer(&stream->queue, buf);
633 }
634 }
635}
636
637static void uvc_video_decode_bulk(struct urb *urb, struct uvc_streaming *stream,
638 struct uvc_buffer *buf)
639{
640 u8 *mem;
641 int len, ret;
642
643 if (urb->actual_length == 0)
644 return;
645
646 mem = urb->transfer_buffer;
647 len = urb->actual_length;
648 stream->bulk.payload_size += len;
649
650
651
652
653 if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) {
654 do {
655 ret = uvc_video_decode_start(stream, buf, mem, len);
656 if (ret == -EAGAIN)
657 buf = uvc_queue_next_buffer(&stream->queue,
658 buf);
659 } while (ret == -EAGAIN);
660
661
662 if (ret < 0 || buf == NULL) {
663 stream->bulk.skip_payload = 1;
664 } else {
665 memcpy(stream->bulk.header, mem, ret);
666 stream->bulk.header_size = ret;
667
668 mem += ret;
669 len -= ret;
670 }
671 }
672
673
674
675
676
677
678
679 if (!stream->bulk.skip_payload && buf != NULL)
680 uvc_video_decode_data(stream, buf, mem, len);
681
682
683
684
685 if (urb->actual_length < urb->transfer_buffer_length ||
686 stream->bulk.payload_size >= stream->bulk.max_payload_size) {
687 if (!stream->bulk.skip_payload && buf != NULL) {
688 uvc_video_decode_end(stream, buf, stream->bulk.header,
689 stream->bulk.payload_size);
690 if (buf->state == UVC_BUF_STATE_READY)
691 buf = uvc_queue_next_buffer(&stream->queue,
692 buf);
693 }
694
695 stream->bulk.header_size = 0;
696 stream->bulk.skip_payload = 0;
697 stream->bulk.payload_size = 0;
698 }
699}
700
701static void uvc_video_encode_bulk(struct urb *urb, struct uvc_streaming *stream,
702 struct uvc_buffer *buf)
703{
704 u8 *mem = urb->transfer_buffer;
705 int len = stream->urb_size, ret;
706
707 if (buf == NULL) {
708 urb->transfer_buffer_length = 0;
709 return;
710 }
711
712
713 if (stream->bulk.header_size == 0) {
714 ret = uvc_video_encode_header(stream, buf, mem, len);
715 stream->bulk.header_size = ret;
716 stream->bulk.payload_size += ret;
717 mem += ret;
718 len -= ret;
719 }
720
721
722 ret = uvc_video_encode_data(stream, buf, mem, len);
723
724 stream->bulk.payload_size += ret;
725 len -= ret;
726
727 if (buf->buf.bytesused == stream->queue.buf_used ||
728 stream->bulk.payload_size == stream->bulk.max_payload_size) {
729 if (buf->buf.bytesused == stream->queue.buf_used) {
730 stream->queue.buf_used = 0;
731 buf->state = UVC_BUF_STATE_READY;
732 buf->buf.sequence = ++stream->sequence;
733 uvc_queue_next_buffer(&stream->queue, buf);
734 stream->last_fid ^= UVC_STREAM_FID;
735 }
736
737 stream->bulk.header_size = 0;
738 stream->bulk.payload_size = 0;
739 }
740
741 urb->transfer_buffer_length = stream->urb_size - len;
742}
743
744static void uvc_video_complete(struct urb *urb)
745{
746 struct uvc_streaming *stream = urb->context;
747 struct uvc_video_queue *queue = &stream->queue;
748 struct uvc_buffer *buf = NULL;
749 unsigned long flags;
750 int ret;
751
752 switch (urb->status) {
753 case 0:
754 break;
755
756 default:
757 uvc_printk(KERN_WARNING, "Non-zero status (%d) in video "
758 "completion handler.\n", urb->status);
759
760 case -ENOENT:
761 if (stream->frozen)
762 return;
763
764 case -ECONNRESET:
765 case -ESHUTDOWN:
766 uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
767 return;
768 }
769
770 spin_lock_irqsave(&queue->irqlock, flags);
771 if (!list_empty(&queue->irqqueue))
772 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
773 queue);
774 spin_unlock_irqrestore(&queue->irqlock, flags);
775
776 stream->decode(urb, stream, buf);
777
778 if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
779 uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n",
780 ret);
781 }
782}
783
784
785
786
787static void uvc_free_urb_buffers(struct uvc_streaming *stream)
788{
789 unsigned int i;
790
791 for (i = 0; i < UVC_URBS; ++i) {
792 if (stream->urb_buffer[i]) {
793 usb_free_coherent(stream->dev->udev, stream->urb_size,
794 stream->urb_buffer[i], stream->urb_dma[i]);
795 stream->urb_buffer[i] = NULL;
796 }
797 }
798
799 stream->urb_size = 0;
800}
801
802
803
804
805
806
807
808
809
810
811
812
813static int uvc_alloc_urb_buffers(struct uvc_streaming *stream,
814 unsigned int size, unsigned int psize, gfp_t gfp_flags)
815{
816 unsigned int npackets;
817 unsigned int i;
818
819
820 if (stream->urb_size)
821 return stream->urb_size / psize;
822
823
824
825
826 npackets = DIV_ROUND_UP(size, psize);
827 if (npackets > UVC_MAX_PACKETS)
828 npackets = UVC_MAX_PACKETS;
829
830
831 for (; npackets > 1; npackets /= 2) {
832 for (i = 0; i < UVC_URBS; ++i) {
833 stream->urb_size = psize * npackets;
834 stream->urb_buffer[i] = usb_alloc_coherent(
835 stream->dev->udev, stream->urb_size,
836 gfp_flags | __GFP_NOWARN, &stream->urb_dma[i]);
837 if (!stream->urb_buffer[i]) {
838 uvc_free_urb_buffers(stream);
839 break;
840 }
841 }
842
843 if (i == UVC_URBS) {
844 uvc_trace(UVC_TRACE_VIDEO, "Allocated %u URB buffers "
845 "of %ux%u bytes each.\n", UVC_URBS, npackets,
846 psize);
847 return npackets;
848 }
849 }
850
851 uvc_trace(UVC_TRACE_VIDEO, "Failed to allocate URB buffers (%u bytes "
852 "per packet).\n", psize);
853 return 0;
854}
855
856
857
858
859static void uvc_uninit_video(struct uvc_streaming *stream, int free_buffers)
860{
861 struct urb *urb;
862 unsigned int i;
863
864 for (i = 0; i < UVC_URBS; ++i) {
865 urb = stream->urb[i];
866 if (urb == NULL)
867 continue;
868
869 usb_kill_urb(urb);
870 usb_free_urb(urb);
871 stream->urb[i] = NULL;
872 }
873
874 if (free_buffers)
875 uvc_free_urb_buffers(stream);
876}
877
878
879
880
881
882static int uvc_init_video_isoc(struct uvc_streaming *stream,
883 struct usb_host_endpoint *ep, gfp_t gfp_flags)
884{
885 struct urb *urb;
886 unsigned int npackets, i, j;
887 u16 psize;
888 u32 size;
889
890 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
891 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
892 size = stream->ctrl.dwMaxVideoFrameSize;
893
894 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
895 if (npackets == 0)
896 return -ENOMEM;
897
898 size = npackets * psize;
899
900 for (i = 0; i < UVC_URBS; ++i) {
901 urb = usb_alloc_urb(npackets, gfp_flags);
902 if (urb == NULL) {
903 uvc_uninit_video(stream, 1);
904 return -ENOMEM;
905 }
906
907 urb->dev = stream->dev->udev;
908 urb->context = stream;
909 urb->pipe = usb_rcvisocpipe(stream->dev->udev,
910 ep->desc.bEndpointAddress);
911 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
912 urb->interval = ep->desc.bInterval;
913 urb->transfer_buffer = stream->urb_buffer[i];
914 urb->transfer_dma = stream->urb_dma[i];
915 urb->complete = uvc_video_complete;
916 urb->number_of_packets = npackets;
917 urb->transfer_buffer_length = size;
918
919 for (j = 0; j < npackets; ++j) {
920 urb->iso_frame_desc[j].offset = j * psize;
921 urb->iso_frame_desc[j].length = psize;
922 }
923
924 stream->urb[i] = urb;
925 }
926
927 return 0;
928}
929
930
931
932
933
934static int uvc_init_video_bulk(struct uvc_streaming *stream,
935 struct usb_host_endpoint *ep, gfp_t gfp_flags)
936{
937 struct urb *urb;
938 unsigned int npackets, pipe, i;
939 u16 psize;
940 u32 size;
941
942 psize = le16_to_cpu(ep->desc.wMaxPacketSize) & 0x07ff;
943 size = stream->ctrl.dwMaxPayloadTransferSize;
944 stream->bulk.max_payload_size = size;
945
946 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
947 if (npackets == 0)
948 return -ENOMEM;
949
950 size = npackets * psize;
951
952 if (usb_endpoint_dir_in(&ep->desc))
953 pipe = usb_rcvbulkpipe(stream->dev->udev,
954 ep->desc.bEndpointAddress);
955 else
956 pipe = usb_sndbulkpipe(stream->dev->udev,
957 ep->desc.bEndpointAddress);
958
959 if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
960 size = 0;
961
962 for (i = 0; i < UVC_URBS; ++i) {
963 urb = usb_alloc_urb(0, gfp_flags);
964 if (urb == NULL) {
965 uvc_uninit_video(stream, 1);
966 return -ENOMEM;
967 }
968
969 usb_fill_bulk_urb(urb, stream->dev->udev, pipe,
970 stream->urb_buffer[i], size, uvc_video_complete,
971 stream);
972 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
973 urb->transfer_dma = stream->urb_dma[i];
974
975 stream->urb[i] = urb;
976 }
977
978 return 0;
979}
980
981
982
983
984static int uvc_init_video(struct uvc_streaming *stream, gfp_t gfp_flags)
985{
986 struct usb_interface *intf = stream->intf;
987 struct usb_host_endpoint *ep;
988 unsigned int i;
989 int ret;
990
991 stream->sequence = -1;
992 stream->last_fid = -1;
993 stream->bulk.header_size = 0;
994 stream->bulk.skip_payload = 0;
995 stream->bulk.payload_size = 0;
996
997 if (intf->num_altsetting > 1) {
998 struct usb_host_endpoint *best_ep = NULL;
999 unsigned int best_psize = 3 * 1024;
1000 unsigned int bandwidth;
1001 unsigned int uninitialized_var(altsetting);
1002 int intfnum = stream->intfnum;
1003
1004
1005 bandwidth = stream->ctrl.dwMaxPayloadTransferSize;
1006
1007 if (bandwidth == 0) {
1008 uvc_trace(UVC_TRACE_VIDEO, "Device requested null "
1009 "bandwidth, defaulting to lowest.\n");
1010 bandwidth = 1;
1011 } else {
1012 uvc_trace(UVC_TRACE_VIDEO, "Device requested %u "
1013 "B/frame bandwidth.\n", bandwidth);
1014 }
1015
1016 for (i = 0; i < intf->num_altsetting; ++i) {
1017 struct usb_host_interface *alts;
1018 unsigned int psize;
1019
1020 alts = &intf->altsetting[i];
1021 ep = uvc_find_endpoint(alts,
1022 stream->header.bEndpointAddress);
1023 if (ep == NULL)
1024 continue;
1025
1026
1027 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
1028 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
1029 if (psize >= bandwidth && psize <= best_psize) {
1030 altsetting = i;
1031 best_psize = psize;
1032 best_ep = ep;
1033 }
1034 }
1035
1036 if (best_ep == NULL) {
1037 uvc_trace(UVC_TRACE_VIDEO, "No fast enough alt setting "
1038 "for requested bandwidth.\n");
1039 return -EIO;
1040 }
1041
1042 uvc_trace(UVC_TRACE_VIDEO, "Selecting alternate setting %u "
1043 "(%u B/frame bandwidth).\n", altsetting, best_psize);
1044
1045 ret = usb_set_interface(stream->dev->udev, intfnum, altsetting);
1046 if (ret < 0)
1047 return ret;
1048
1049 ret = uvc_init_video_isoc(stream, best_ep, gfp_flags);
1050 } else {
1051
1052 ep = uvc_find_endpoint(&intf->altsetting[0],
1053 stream->header.bEndpointAddress);
1054 if (ep == NULL)
1055 return -EIO;
1056
1057 ret = uvc_init_video_bulk(stream, ep, gfp_flags);
1058 }
1059
1060 if (ret < 0)
1061 return ret;
1062
1063
1064 for (i = 0; i < UVC_URBS; ++i) {
1065 ret = usb_submit_urb(stream->urb[i], gfp_flags);
1066 if (ret < 0) {
1067 uvc_printk(KERN_ERR, "Failed to submit URB %u "
1068 "(%d).\n", i, ret);
1069 uvc_uninit_video(stream, 1);
1070 return ret;
1071 }
1072 }
1073
1074 return 0;
1075}
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088int uvc_video_suspend(struct uvc_streaming *stream)
1089{
1090 if (!uvc_queue_streaming(&stream->queue))
1091 return 0;
1092
1093 stream->frozen = 1;
1094 uvc_uninit_video(stream, 0);
1095 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1096 return 0;
1097}
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107int uvc_video_resume(struct uvc_streaming *stream)
1108{
1109 int ret;
1110
1111 stream->frozen = 0;
1112
1113 ret = uvc_commit_video(stream, &stream->ctrl);
1114 if (ret < 0) {
1115 uvc_queue_enable(&stream->queue, 0);
1116 return ret;
1117 }
1118
1119 if (!uvc_queue_streaming(&stream->queue))
1120 return 0;
1121
1122 ret = uvc_init_video(stream, GFP_NOIO);
1123 if (ret < 0)
1124 uvc_queue_enable(&stream->queue, 0);
1125
1126 return ret;
1127}
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143int uvc_video_init(struct uvc_streaming *stream)
1144{
1145 struct uvc_streaming_control *probe = &stream->ctrl;
1146 struct uvc_format *format = NULL;
1147 struct uvc_frame *frame = NULL;
1148 unsigned int i;
1149 int ret;
1150
1151 if (stream->nformats == 0) {
1152 uvc_printk(KERN_INFO, "No supported video formats found.\n");
1153 return -EINVAL;
1154 }
1155
1156 atomic_set(&stream->active, 0);
1157
1158
1159 uvc_queue_init(&stream->queue, stream->type, !uvc_no_drop_param);
1160
1161
1162
1163
1164
1165
1166 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1167
1168
1169
1170
1171
1172
1173 if (uvc_get_video_ctrl(stream, probe, 1, UVC_GET_DEF) == 0)
1174 uvc_set_video_ctrl(stream, probe, 1);
1175
1176
1177
1178
1179
1180
1181 ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
1182 if (ret < 0)
1183 return ret;
1184
1185
1186
1187
1188 for (i = stream->nformats; i > 0; --i) {
1189 format = &stream->format[i-1];
1190 if (format->index == probe->bFormatIndex)
1191 break;
1192 }
1193
1194 if (format->nframes == 0) {
1195 uvc_printk(KERN_INFO, "No frame descriptor found for the "
1196 "default format.\n");
1197 return -EINVAL;
1198 }
1199
1200
1201
1202
1203
1204
1205 for (i = format->nframes; i > 0; --i) {
1206 frame = &format->frame[i-1];
1207 if (frame->bFrameIndex == probe->bFrameIndex)
1208 break;
1209 }
1210
1211 probe->bFormatIndex = format->index;
1212 probe->bFrameIndex = frame->bFrameIndex;
1213
1214 stream->cur_format = format;
1215 stream->cur_frame = frame;
1216
1217
1218 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1219 if (stream->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
1220 stream->decode = uvc_video_decode_isight;
1221 else if (stream->intf->num_altsetting > 1)
1222 stream->decode = uvc_video_decode_isoc;
1223 else
1224 stream->decode = uvc_video_decode_bulk;
1225 } else {
1226 if (stream->intf->num_altsetting == 1)
1227 stream->decode = uvc_video_encode_bulk;
1228 else {
1229 uvc_printk(KERN_INFO, "Isochronous endpoints are not "
1230 "supported for video output devices.\n");
1231 return -EINVAL;
1232 }
1233 }
1234
1235 return 0;
1236}
1237
1238
1239
1240
1241int uvc_video_enable(struct uvc_streaming *stream, int enable)
1242{
1243 int ret;
1244
1245 if (!enable) {
1246 uvc_uninit_video(stream, 1);
1247 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1248 uvc_queue_enable(&stream->queue, 0);
1249 return 0;
1250 }
1251
1252 ret = uvc_queue_enable(&stream->queue, 1);
1253 if (ret < 0)
1254 return ret;
1255
1256
1257 ret = uvc_commit_video(stream, &stream->ctrl);
1258 if (ret < 0) {
1259 uvc_queue_enable(&stream->queue, 0);
1260 return ret;
1261 }
1262
1263 return uvc_init_video(stream, GFP_KERNEL);
1264}
1265
1266