14 #include <linux/kernel.h>
15 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
19 #include <linux/videodev2.h>
21 #include <linux/wait.h>
23 #include <asm/unaligned.h>
40 pipe = (query & 0x80) ? usb_rcvctrlpipe(dev->
udev, 0)
41 : usb_sndctrlpipe(dev->
udev, 0);
45 unit << 8 | intfnum, data, size, timeout);
48 static const char *uvc_query_name(
__u8 query)
77 ret = __uvc_query_ctrl(dev, query, unit, intfnum, cs, data, size,
81 "unit %u: %d (exp. %u).\n", uvc_query_name(query), cs,
89 static void uvc_fixup_video_ctrl(
struct uvc_streaming *stream,
106 for (i = 0; i < format->
nframes; ++
i) {
108 frame = &format->
frame[
i];
118 stream->
dev->uvc_version < 0x0110))
124 stream->
intf->num_altsetting > 1) {
139 bandwidth *= 10000000 / interval + 1;
152 bandwidth =
max_t(
u32, bandwidth, 1024);
165 size = stream->
dev->uvc_version >= 0x0110 ? 34 : 26;
174 ret = __uvc_query_ctrl(stream->
dev, query, 0, stream->
intfnum,
184 "compliance - GET_MIN/MAX(PROBE) incorrectly "
185 "supported. Enabling workaround.\n");
186 memset(ctrl, 0,
sizeof *ctrl);
190 }
else if (query ==
UVC_GET_DEF && probe == 1 && ret != size) {
196 "compliance - GET_DEF(PROBE) not supported. "
197 "Enabling workaround.\n");
200 }
else if (ret != size) {
202 "%d (exp. %u).\n", query, probe ?
"probe" :
"commit",
238 uvc_fixup_video_ctrl(stream, ctrl);
253 size = stream->
dev->uvc_version >= 0x0110 ? 34 : 26;
283 "%d (exp. %u).\n", probe ?
"probe" :
"commit",
307 ret = uvc_set_video_ctrl(stream, probe, 1);
313 ret = uvc_get_video_ctrl(stream, &probe_min, 1,
UVC_GET_MIN);
316 ret = uvc_get_video_ctrl(stream, &probe_max, 1,
UVC_GET_MAX);
323 for (i = 0; i < 2; ++
i) {
324 ret = uvc_set_video_ctrl(stream, probe, 1);
327 ret = uvc_get_video_ctrl(stream, probe, 1,
UVC_GET_CUR);
331 if (stream->
intf->num_altsetting == 1)
335 if (bandwidth <= stream->maxpsize)
357 return uvc_set_video_ctrl(stream, probe, 0);
366 const __u8 *data,
int len)
368 struct uvc_clock_sample *
sample;
370 bool has_pts =
false;
371 bool has_scr =
false;
397 if (len < header_size)
407 if (has_pts && buf !=
NULL)
416 dev_sof = get_unaligned_le16(&data[header_size - 2]);
417 if (dev_sof == stream->
clock.last_sof)
420 stream->
clock.last_sof = dev_sof;
444 if (stream->
clock.sof_offset == (
u16)-1) {
445 u16 delta_sof = (host_sof - dev_sof) & 255;
447 stream->
clock.sof_offset = delta_sof;
449 stream->
clock.sof_offset = 0;
452 dev_sof = (dev_sof + stream->
clock.sof_offset) & 2047;
456 sample = &stream->
clock.samples[stream->
clock.head];
458 sample->dev_sof = dev_sof;
459 sample->host_sof = host_sof;
460 sample->host_ts =
ts;
466 stream->
clock.count++;
468 spin_unlock_irqrestore(&stream->
clock.lock, flags);
471 static void uvc_video_clock_reset(
struct uvc_streaming *stream)
477 clock->last_sof = -1;
478 clock->sof_offset = -1;
481 static int uvc_video_clock_init(
struct uvc_streaming *stream)
488 clock->samples =
kmalloc(clock->size *
sizeof(*clock->samples),
490 if (clock->samples ==
NULL)
493 uvc_video_clock_reset(stream);
498 static void uvc_video_clock_cleanup(
struct uvc_streaming *stream)
521 static u16 uvc_video_clock_host_sof(
const struct uvc_clock_sample *sample)
526 delta_sof = (sample->host_sof - sample->dev_sof) & 255;
528 return (sample->dev_sof + delta_sof) & 2047;
596 struct uvc_clock *clock = &stream->
clock;
597 struct uvc_clock_sample *
first;
598 struct uvc_clock_sample *
last;
612 if (clock->count < clock->size)
615 first = &clock->samples[clock->head];
616 last = &clock->samples[(clock->head - 1) % clock->size];
619 delta_stc = buf->
pts - (1
UL << 31);
620 x1 = first->dev_stc - delta_stc;
621 x2 = last->dev_stc - delta_stc;
625 y1 = (first->dev_sof + 2048) << 16;
626 y2 = (last->dev_sof + 2048) << 16;
630 y = (
u64)(y2 - y1) * (1ULL << 31) + (
u64)y1 * (
u64)x2
632 y = div_u64(y, x2 - x1);
637 "(x1 %u x2 %u y1 %u y2 %u SOF offset %u)\n",
638 stream->
dev->name, buf->
pts,
639 y >> 16, div_u64((y & 0xffff) * 1000000, 65536),
640 sof >> 16, div_u64(((
u64)sof & 0xffff) * 1000000LLU, 65536),
641 x1, x2, y1, y2, clock->sof_offset);
644 x1 = (uvc_video_clock_host_sof(first) + 2048) << 16;
645 x2 = (uvc_video_clock_host_sof(last) + 2048) << 16;
651 ts = timespec_sub(last->host_ts, first->host_ts);
660 mean = (x1 + x2) / 2;
661 if (mean - (1024 << 16) > sof)
663 else if (sof > mean + (1024 << 16))
668 y = div_u64(y, x2 - x1);
671 ts.
tv_sec = first->host_ts.tv_sec - 1 +
div;
672 ts.
tv_nsec = first->host_ts.tv_nsec + rem;
679 "buf ts %lu.%06lu (x1 %u/%u/%u x2 %u/%u/%u y1 %u y2 %u)\n",
681 sof >> 16, div_u64(((
u64)sof & 0xffff) * 1000000LLU, 65536),
684 x1, first->host_sof, first->dev_sof,
685 x2, last->host_sof, last->dev_sof, y1, y2);
692 spin_unlock_irqrestore(&stream->
clock.lock, flags);
699 static void uvc_video_stats_decode(
struct uvc_streaming *stream,
700 const __u8 *data,
int len)
702 unsigned int header_size;
703 bool has_pts =
false;
704 bool has_scr =
false;
709 if (stream->
stats.stream.nb_frames == 0 &&
710 stream->
stats.frame.nb_packets == 0)
733 if (len < header_size || data[0] < header_size) {
734 stream->
stats.frame.nb_invalid++;
744 scr_sof = get_unaligned_le16(&data[header_size - 2]);
748 if (has_pts && stream->
stats.frame.nb_pts) {
749 if (stream->
stats.frame.pts != pts) {
750 stream->
stats.frame.nb_pts_diffs++;
751 stream->
stats.frame.last_pts_diff =
752 stream->
stats.frame.nb_packets;
757 stream->
stats.frame.nb_pts++;
758 stream->
stats.frame.pts = pts;
764 if (stream->
stats.frame.size == 0) {
765 if (len > header_size)
766 stream->
stats.frame.has_initial_pts = has_pts;
767 if (len == header_size && has_pts)
768 stream->
stats.frame.has_early_pts =
true;
772 if (has_scr && stream->
stats.frame.nb_scr) {
773 if (stream->
stats.frame.scr_stc != scr_stc)
774 stream->
stats.frame.nb_scr_diffs++;
779 if (stream->
stats.stream.nb_frames > 0 ||
780 stream->
stats.frame.nb_scr > 0)
781 stream->
stats.stream.scr_sof_count +=
782 (scr_sof - stream->
stats.stream.scr_sof) % 2048;
783 stream->
stats.stream.scr_sof = scr_sof;
785 stream->
stats.frame.nb_scr++;
786 stream->
stats.frame.scr_stc = scr_stc;
787 stream->
stats.frame.scr_sof = scr_sof;
789 if (scr_sof < stream->
stats.stream.min_sof)
790 stream->
stats.stream.min_sof = scr_sof;
791 if (scr_sof > stream->
stats.stream.max_sof)
792 stream->
stats.stream.max_sof = scr_sof;
796 if (stream->
stats.frame.size == 0 && len > header_size)
797 stream->
stats.frame.first_data = stream->
stats.frame.nb_packets;
800 stream->
stats.frame.size += len - header_size;
803 stream->
stats.frame.nb_packets++;
804 if (len > header_size)
805 stream->
stats.frame.nb_empty++;
808 stream->
stats.frame.nb_errors++;
811 static void uvc_video_stats_update(
struct uvc_streaming *stream)
816 "%u/%u/%u pts (%searly %sinitial), %u/%u scr, "
817 "last pts/stc/sof %u/%u/%u\n",
826 stream->
stats.stream.nb_frames++;
827 stream->
stats.stream.nb_packets += stream->
stats.frame.nb_packets;
828 stream->
stats.stream.nb_empty += stream->
stats.frame.nb_empty;
829 stream->
stats.stream.nb_errors += stream->
stats.frame.nb_errors;
830 stream->
stats.stream.nb_invalid += stream->
stats.frame.nb_invalid;
833 stream->
stats.stream.nb_pts_early++;
835 stream->
stats.stream.nb_pts_initial++;
837 stream->
stats.stream.nb_pts_constant++;
839 stream->
stats.stream.nb_scr_count_ok++;
841 stream->
stats.stream.nb_scr_diffs_ok++;
849 unsigned int scr_sof_freq;
855 - stream->
stats.stream.start_ts.tv_sec;
857 - stream->
stats.stream.start_ts.tv_nsec;
868 scr_sof_freq = stream->
stats.stream.scr_sof_count * 1000
873 count +=
scnprintf(buf + count, size - count,
874 "frames: %u\npackets: %u\nempty: %u\n"
875 "errors: %u\ninvalid: %u\n",
876 stream->
stats.stream.nb_frames,
877 stream->
stats.stream.nb_packets,
878 stream->
stats.stream.nb_empty,
879 stream->
stats.stream.nb_errors,
880 stream->
stats.stream.nb_invalid);
881 count +=
scnprintf(buf + count, size - count,
882 "pts: %u early, %u initial, %u ok\n",
883 stream->
stats.stream.nb_pts_early,
884 stream->
stats.stream.nb_pts_initial,
885 stream->
stats.stream.nb_pts_constant);
886 count +=
scnprintf(buf + count, size - count,
887 "scr: %u count ok, %u diff ok\n",
888 stream->
stats.stream.nb_scr_count_ok,
889 stream->
stats.stream.nb_scr_diffs_ok);
890 count +=
scnprintf(buf + count, size - count,
891 "sof: %u <= sof <= %u, freq %u.%03u kHz\n",
892 stream->
stats.stream.min_sof,
893 stream->
stats.stream.max_sof,
894 scr_sof_freq / 1000, scr_sof_freq % 1000);
899 static void uvc_video_stats_start(
struct uvc_streaming *stream)
902 stream->
stats.stream.min_sof = 2048;
905 static void uvc_video_stats_stop(
struct uvc_streaming *stream)
949 static int uvc_video_decode_start(
struct uvc_streaming *stream,
959 if (len < 2 || data[0] < 2 || data[0] > len) {
960 stream->
stats.frame.nb_invalid++;
972 uvc_video_stats_update(stream);
975 uvc_video_clock_decode(stream, buf, data, len);
976 uvc_video_stats_decode(stream, data, len);
1019 buf->
buf.v4l2_buf.timestamp.tv_sec =
ts.tv_sec;
1020 buf->
buf.v4l2_buf.timestamp.tv_usec =
1054 static void uvc_video_decode_data(
struct uvc_streaming *stream,
1057 unsigned int maxlen,
nbytes;
1066 nbytes =
min((
unsigned int)len, maxlen);
1067 memcpy(mem, data, nbytes);
1077 static void uvc_video_decode_end(
struct uvc_streaming *stream,
1102 static int uvc_video_encode_header(
struct uvc_streaming *stream,
1111 static int uvc_video_encode_data(
struct uvc_streaming *stream,
1121 nbytes =
min(stream->
bulk.max_payload_size - stream->
bulk.payload_size,
1123 memcpy(data, mem, nbytes);
1143 for (i = 0; i < urb->number_of_packets; ++
i) {
1144 if (urb->iso_frame_desc[i].status < 0) {
1146 "lost (%d).\n", urb->iso_frame_desc[i].status);
1154 mem = urb->transfer_buffer + urb->iso_frame_desc[
i].offset;
1156 ret = uvc_video_decode_start(stream, buf, mem,
1157 urb->iso_frame_desc[i].actual_length);
1161 }
while (ret == -
EAGAIN);
1167 uvc_video_decode_data(stream, buf, mem + ret,
1168 urb->iso_frame_desc[i].actual_length - ret);
1171 uvc_video_decode_end(stream, buf, mem,
1172 urb->iso_frame_desc[i].actual_length);
1185 static void uvc_video_decode_bulk(
struct urb *urb,
struct uvc_streaming *stream,
1195 if (urb->actual_length == 0 && stream->
bulk.header_size == 0)
1198 mem = urb->transfer_buffer;
1199 len = urb->actual_length;
1200 stream->
bulk.payload_size += len;
1205 if (stream->
bulk.header_size == 0 && !stream->
bulk.skip_payload) {
1207 ret = uvc_video_decode_start(stream, buf, mem, len);
1211 }
while (ret == -
EAGAIN);
1214 if (ret < 0 || buf ==
NULL) {
1215 stream->
bulk.skip_payload = 1;
1218 stream->
bulk.header_size =
ret;
1231 if (!stream->
bulk.skip_payload && buf !=
NULL)
1232 uvc_video_decode_data(stream, buf, mem, len);
1237 if (urb->actual_length < urb->transfer_buffer_length ||
1238 stream->
bulk.payload_size >= stream->
bulk.max_payload_size) {
1239 if (!stream->
bulk.skip_payload && buf !=
NULL) {
1240 uvc_video_decode_end(stream, buf, stream->
bulk.header,
1241 stream->
bulk.payload_size);
1247 stream->
bulk.header_size = 0;
1248 stream->
bulk.skip_payload = 0;
1249 stream->
bulk.payload_size = 0;
1253 static void uvc_video_encode_bulk(
struct urb *urb,
struct uvc_streaming *stream,
1256 u8 *mem = urb->transfer_buffer;
1260 urb->transfer_buffer_length = 0;
1265 if (stream->
bulk.header_size == 0) {
1266 ret = uvc_video_encode_header(stream, buf, mem, len);
1267 stream->
bulk.header_size =
ret;
1268 stream->
bulk.payload_size +=
ret;
1274 ret = uvc_video_encode_data(stream, buf, mem, len);
1276 stream->
bulk.payload_size +=
ret;
1280 stream->
bulk.payload_size == stream->
bulk.max_payload_size) {
1282 stream->
queue.buf_used = 0;
1289 stream->
bulk.header_size = 0;
1290 stream->
bulk.payload_size = 0;
1293 urb->transfer_buffer_length = stream->
urb_size - len;
1296 static void uvc_video_complete(
struct urb *urb)
1301 unsigned long flags;
1304 switch (urb->status) {
1310 "completion handler.\n", urb->status);
1326 spin_unlock_irqrestore(&queue->
irqlock, flags);
1328 stream->
decode(urb, stream, buf);
1339 static void uvc_free_urb_buffers(
struct uvc_streaming *stream)
1345 #ifndef CONFIG_DMA_NONCOHERENT
1369 static int uvc_alloc_urb_buffers(
struct uvc_streaming *stream,
1370 unsigned int size,
unsigned int psize,
gfp_t gfp_flags)
1372 unsigned int npackets;
1387 for (; npackets > 1; npackets /= 2) {
1389 stream->
urb_size = psize * npackets;
1390 #ifndef CONFIG_DMA_NONCOHERENT
1399 uvc_free_urb_buffers(stream);
1404 if (i == UVC_URBS) {
1406 "of %ux%u bytes each.\n", UVC_URBS, npackets,
1413 "per packet).\n", psize);
1420 static void uvc_uninit_video(
struct uvc_streaming *stream,
int free_buffers)
1425 uvc_video_stats_stop(stream);
1428 urb = stream->
urb[
i];
1438 uvc_free_urb_buffers(stream);
1444 static unsigned int uvc_endpoint_max_bpi(
struct usb_device *dev,
1445 struct usb_host_endpoint *ep)
1449 switch (dev->speed) {
1451 return ep->ss_ep_comp.wBytesPerInterval;
1453 psize = usb_endpoint_maxp(&ep->desc);
1454 return (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
1456 psize = usb_endpoint_maxp(&ep->desc);
1457 return psize & 0x07ff;
1465 static int uvc_init_video_isoc(
struct uvc_streaming *stream,
1466 struct usb_host_endpoint *ep,
gfp_t gfp_flags)
1469 unsigned int npackets,
i,
j;
1473 psize = uvc_endpoint_max_bpi(stream->
dev->udev, ep);
1474 size = stream->
ctrl.dwMaxVideoFrameSize;
1476 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
1480 size = npackets * psize;
1485 uvc_uninit_video(stream, 1);
1489 urb->dev = stream->
dev->udev;
1490 urb->context = stream;
1491 urb->pipe = usb_rcvisocpipe(stream->
dev->udev,
1492 ep->desc.bEndpointAddress);
1493 #ifndef CONFIG_DMA_NONCOHERENT
1494 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
1495 urb->transfer_dma = stream->
urb_dma[
i];
1497 urb->transfer_flags = URB_ISO_ASAP;
1499 urb->interval = ep->desc.bInterval;
1501 urb->complete = uvc_video_complete;
1502 urb->number_of_packets = npackets;
1503 urb->transfer_buffer_length =
size;
1505 for (j = 0; j < npackets; ++
j) {
1506 urb->iso_frame_desc[
j].offset = j * psize;
1507 urb->iso_frame_desc[
j].length = psize;
1520 static int uvc_init_video_bulk(
struct uvc_streaming *stream,
1521 struct usb_host_endpoint *ep,
gfp_t gfp_flags)
1524 unsigned int npackets,
pipe,
i;
1528 psize = usb_endpoint_maxp(&ep->desc) & 0x7ff;
1529 size = stream->
ctrl.dwMaxPayloadTransferSize;
1530 stream->
bulk.max_payload_size =
size;
1532 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
1536 size = npackets * psize;
1538 if (usb_endpoint_dir_in(&ep->desc))
1539 pipe = usb_rcvbulkpipe(stream->
dev->udev,
1540 ep->desc.bEndpointAddress);
1542 pipe = usb_sndbulkpipe(stream->
dev->udev,
1543 ep->desc.bEndpointAddress);
1551 uvc_uninit_video(stream, 1);
1555 usb_fill_bulk_urb(urb, stream->
dev->udev, pipe,
1556 stream->
urb_buffer[i], size, uvc_video_complete,
1558 #ifndef CONFIG_DMA_NONCOHERENT
1559 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1560 urb->transfer_dma = stream->
urb_dma[
i];
1575 struct usb_host_endpoint *ep;
1581 stream->
bulk.header_size = 0;
1582 stream->
bulk.skip_payload = 0;
1583 stream->
bulk.payload_size = 0;
1585 uvc_video_stats_start(stream);
1587 if (intf->num_altsetting > 1) {
1588 struct usb_host_endpoint *best_ep =
NULL;
1589 unsigned int best_psize =
UINT_MAX;
1592 int intfnum = stream->
intfnum;
1595 bandwidth = stream->
ctrl.dwMaxPayloadTransferSize;
1597 if (bandwidth == 0) {
1599 "bandwidth, defaulting to lowest.\n");
1603 "B/frame bandwidth.\n", bandwidth);
1606 for (i = 0; i < intf->num_altsetting; ++
i) {
1607 struct usb_host_interface *alts;
1610 alts = &intf->altsetting[
i];
1612 stream->
header.bEndpointAddress);
1617 psize = uvc_endpoint_max_bpi(stream->
dev->udev, ep);
1618 if (psize >= bandwidth && psize <= best_psize) {
1619 altsetting = alts->desc.bAlternateSetting;
1625 if (best_ep ==
NULL) {
1627 "for requested bandwidth.\n");
1632 "(%u B/frame bandwidth).\n", altsetting, best_psize);
1638 ret = uvc_init_video_isoc(stream, best_ep, gfp_flags);
1642 stream->
header.bEndpointAddress);
1646 ret = uvc_init_video_bulk(stream, ep, gfp_flags);
1658 uvc_uninit_video(stream, 1);
1679 if (!uvc_queue_streaming(&stream->
queue))
1683 uvc_uninit_video(stream, 0);
1710 uvc_video_clock_reset(stream);
1712 ret = uvc_commit_video(stream, &stream->
ctrl);
1718 if (!uvc_queue_streaming(&stream->
queue))
1721 ret = uvc_init_video(stream,
GFP_NOIO);
1774 if (uvc_get_video_ctrl(stream, probe, 1,
UVC_GET_DEF) == 0)
1775 uvc_set_video_ctrl(stream, probe, 1);
1782 ret = uvc_get_video_ctrl(stream, probe, 1,
UVC_GET_CUR);
1789 for (i = stream->
nformats; i > 0; --i) {
1790 format = &stream->
format[i-1];
1797 "default format.\n");
1806 for (i = format->
nframes; i > 0; --i) {
1807 frame = &format->
frame[i-1];
1822 else if (stream->
intf->num_altsetting > 1)
1823 stream->
decode = uvc_video_decode_isoc;
1825 stream->
decode = uvc_video_decode_bulk;
1827 if (stream->
intf->num_altsetting == 1)
1828 stream->
decode = uvc_video_encode_bulk;
1831 "supported for video output devices.\n");
1847 uvc_uninit_video(stream, 1);
1850 uvc_video_clock_cleanup(stream);
1854 ret = uvc_video_clock_init(stream);
1863 ret = uvc_commit_video(stream, &stream->
ctrl);
1878 uvc_video_clock_cleanup(stream);