aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/uvc
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/video/uvc')
-rw-r--r--drivers/media/video/uvc/Kconfig1
-rw-r--r--drivers/media/video/uvc/Makefile2
-rw-r--r--drivers/media/video/uvc/uvc_ctrl.c19
-rw-r--r--drivers/media/video/uvc/uvc_debugfs.c136
-rw-r--r--drivers/media/video/uvc/uvc_driver.c30
-rw-r--r--drivers/media/video/uvc/uvc_isight.c10
-rw-r--r--drivers/media/video/uvc/uvc_queue.c564
-rw-r--r--drivers/media/video/uvc/uvc_v4l2.c29
-rw-r--r--drivers/media/video/uvc/uvc_video.c625
-rw-r--r--drivers/media/video/uvc/uvcvideo.h128
10 files changed, 1041 insertions, 503 deletions
diff --git a/drivers/media/video/uvc/Kconfig b/drivers/media/video/uvc/Kconfig
index 2956a7637219..6c197da531b2 100644
--- a/drivers/media/video/uvc/Kconfig
+++ b/drivers/media/video/uvc/Kconfig
@@ -1,5 +1,6 @@
1config USB_VIDEO_CLASS 1config USB_VIDEO_CLASS
2 tristate "USB Video Class (UVC)" 2 tristate "USB Video Class (UVC)"
3 select VIDEOBUF2_VMALLOC
3 ---help--- 4 ---help---
4 Support for the USB Video Class (UVC). Currently only video 5 Support for the USB Video Class (UVC). Currently only video
5 input devices, such as webcams, are supported. 6 input devices, such as webcams, are supported.
diff --git a/drivers/media/video/uvc/Makefile b/drivers/media/video/uvc/Makefile
index 2071ca8a2f03..c26d12fdb8f4 100644
--- a/drivers/media/video/uvc/Makefile
+++ b/drivers/media/video/uvc/Makefile
@@ -1,5 +1,5 @@
1uvcvideo-objs := uvc_driver.o uvc_queue.o uvc_v4l2.o uvc_video.o uvc_ctrl.o \ 1uvcvideo-objs := uvc_driver.o uvc_queue.o uvc_v4l2.o uvc_video.o uvc_ctrl.o \
2 uvc_status.o uvc_isight.o 2 uvc_status.o uvc_isight.o uvc_debugfs.o
3ifeq ($(CONFIG_MEDIA_CONTROLLER),y) 3ifeq ($(CONFIG_MEDIA_CONTROLLER),y)
4uvcvideo-objs += uvc_entity.o 4uvcvideo-objs += uvc_entity.o
5endif 5endif
diff --git a/drivers/media/video/uvc/uvc_ctrl.c b/drivers/media/video/uvc/uvc_ctrl.c
index 254d32688843..0efd3b10b353 100644
--- a/drivers/media/video/uvc/uvc_ctrl.c
+++ b/drivers/media/video/uvc/uvc_ctrl.c
@@ -878,8 +878,21 @@ static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
878 chain->dev->intfnum, ctrl->info.selector, 878 chain->dev->intfnum, ctrl->info.selector,
879 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 879 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
880 ctrl->info.size); 880 ctrl->info.size);
881 if (ret < 0) 881 if (ret < 0) {
882 return ret; 882 if (UVC_ENTITY_TYPE(ctrl->entity) !=
883 UVC_VC_EXTENSION_UNIT)
884 return ret;
885
886 /* GET_RES is mandatory for XU controls, but some
887 * cameras still choke on it. Ignore errors and set the
888 * resolution value to zero.
889 */
890 uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
891 "UVC non compliance - GET_RES failed on "
892 "an XU control. Enabling workaround.\n");
893 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
894 ctrl->info.size);
895 }
883 } 896 }
884 897
885 ctrl->cached = 1; 898 ctrl->cached = 1;
@@ -1861,7 +1874,7 @@ int uvc_ctrl_init_device(struct uvc_device *dev)
1861 if (ncontrols == 0) 1874 if (ncontrols == 0)
1862 continue; 1875 continue;
1863 1876
1864 entity->controls = kzalloc(ncontrols * sizeof(*ctrl), 1877 entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
1865 GFP_KERNEL); 1878 GFP_KERNEL);
1866 if (entity->controls == NULL) 1879 if (entity->controls == NULL)
1867 return -ENOMEM; 1880 return -ENOMEM;
diff --git a/drivers/media/video/uvc/uvc_debugfs.c b/drivers/media/video/uvc/uvc_debugfs.c
new file mode 100644
index 000000000000..14561a5abb79
--- /dev/null
+++ b/drivers/media/video/uvc/uvc_debugfs.c
@@ -0,0 +1,136 @@
1/*
2 * uvc_debugfs.c -- USB Video Class driver - Debugging support
3 *
4 * Copyright (C) 2011
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/debugfs.h>
16#include <linux/slab.h>
17#include <linux/usb.h>
18
19#include "uvcvideo.h"
20
21/* -----------------------------------------------------------------------------
22 * Statistics
23 */
24
25#define UVC_DEBUGFS_BUF_SIZE 1024
26
27struct uvc_debugfs_buffer {
28 size_t count;
29 char data[UVC_DEBUGFS_BUF_SIZE];
30};
31
32static int uvc_debugfs_stats_open(struct inode *inode, struct file *file)
33{
34 struct uvc_streaming *stream = inode->i_private;
35 struct uvc_debugfs_buffer *buf;
36
37 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
38 if (buf == NULL)
39 return -ENOMEM;
40
41 buf->count = uvc_video_stats_dump(stream, buf->data, sizeof(buf->data));
42
43 file->private_data = buf;
44 return 0;
45}
46
47static ssize_t uvc_debugfs_stats_read(struct file *file, char __user *user_buf,
48 size_t nbytes, loff_t *ppos)
49{
50 struct uvc_debugfs_buffer *buf = file->private_data;
51
52 return simple_read_from_buffer(user_buf, nbytes, ppos, buf->data,
53 buf->count);
54}
55
56static int uvc_debugfs_stats_release(struct inode *inode, struct file *file)
57{
58 kfree(file->private_data);
59 file->private_data = NULL;
60
61 return 0;
62}
63
64static const struct file_operations uvc_debugfs_stats_fops = {
65 .owner = THIS_MODULE,
66 .open = uvc_debugfs_stats_open,
67 .llseek = no_llseek,
68 .read = uvc_debugfs_stats_read,
69 .release = uvc_debugfs_stats_release,
70};
71
72/* -----------------------------------------------------------------------------
73 * Global and stream initialization/cleanup
74 */
75
76static struct dentry *uvc_debugfs_root_dir;
77
78int uvc_debugfs_init_stream(struct uvc_streaming *stream)
79{
80 struct usb_device *udev = stream->dev->udev;
81 struct dentry *dent;
82 char dir_name[32];
83
84 if (uvc_debugfs_root_dir == NULL)
85 return -ENODEV;
86
87 sprintf(dir_name, "%u-%u", udev->bus->busnum, udev->devnum);
88
89 dent = debugfs_create_dir(dir_name, uvc_debugfs_root_dir);
90 if (IS_ERR_OR_NULL(dent)) {
91 uvc_printk(KERN_INFO, "Unable to create debugfs %s "
92 "directory.\n", dir_name);
93 return -ENODEV;
94 }
95
96 stream->debugfs_dir = dent;
97
98 dent = debugfs_create_file("stats", 0444, stream->debugfs_dir,
99 stream, &uvc_debugfs_stats_fops);
100 if (IS_ERR_OR_NULL(dent)) {
101 uvc_printk(KERN_INFO, "Unable to create debugfs stats file.\n");
102 uvc_debugfs_cleanup_stream(stream);
103 return -ENODEV;
104 }
105
106 return 0;
107}
108
109void uvc_debugfs_cleanup_stream(struct uvc_streaming *stream)
110{
111 if (stream->debugfs_dir == NULL)
112 return;
113
114 debugfs_remove_recursive(stream->debugfs_dir);
115 stream->debugfs_dir = NULL;
116}
117
118int uvc_debugfs_init(void)
119{
120 struct dentry *dir;
121
122 dir = debugfs_create_dir("uvcvideo", usb_debug_root);
123 if (IS_ERR_OR_NULL(dir)) {
124 uvc_printk(KERN_INFO, "Unable to create debugfs directory\n");
125 return -ENODATA;
126 }
127
128 uvc_debugfs_root_dir = dir;
129 return 0;
130}
131
132void uvc_debugfs_cleanup(void)
133{
134 if (uvc_debugfs_root_dir != NULL)
135 debugfs_remove_recursive(uvc_debugfs_root_dir);
136}
diff --git a/drivers/media/video/uvc/uvc_driver.c b/drivers/media/video/uvc/uvc_driver.c
index 656d4c9e3b9f..a240d43d15d1 100644
--- a/drivers/media/video/uvc/uvc_driver.c
+++ b/drivers/media/video/uvc/uvc_driver.c
@@ -1675,6 +1675,8 @@ static void uvc_unregister_video(struct uvc_device *dev)
1675 1675
1676 video_unregister_device(stream->vdev); 1676 video_unregister_device(stream->vdev);
1677 stream->vdev = NULL; 1677 stream->vdev = NULL;
1678
1679 uvc_debugfs_cleanup_stream(stream);
1678 } 1680 }
1679 1681
1680 /* Decrement the stream count and call uvc_delete explicitly if there 1682 /* Decrement the stream count and call uvc_delete explicitly if there
@@ -1700,6 +1702,8 @@ static int uvc_register_video(struct uvc_device *dev,
1700 return ret; 1702 return ret;
1701 } 1703 }
1702 1704
1705 uvc_debugfs_init_stream(stream);
1706
1703 /* Register the device with V4L. */ 1707 /* Register the device with V4L. */
1704 vdev = video_device_alloc(); 1708 vdev = video_device_alloc();
1705 if (vdev == NULL) { 1709 if (vdev == NULL) {
@@ -2033,6 +2037,15 @@ MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
2033 * though they are compliant. 2037 * though they are compliant.
2034 */ 2038 */
2035static struct usb_device_id uvc_ids[] = { 2039static struct usb_device_id uvc_ids[] = {
2040 /* LogiLink Wireless Webcam */
2041 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2042 | USB_DEVICE_ID_MATCH_INT_INFO,
2043 .idVendor = 0x0416,
2044 .idProduct = 0xa91a,
2045 .bInterfaceClass = USB_CLASS_VIDEO,
2046 .bInterfaceSubClass = 1,
2047 .bInterfaceProtocol = 0,
2048 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2036 /* Genius eFace 2025 */ 2049 /* Genius eFace 2025 */
2037 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2050 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2038 | USB_DEVICE_ID_MATCH_INT_INFO, 2051 | USB_DEVICE_ID_MATCH_INT_INFO,
@@ -2396,17 +2409,24 @@ struct uvc_driver uvc_driver = {
2396 2409
2397static int __init uvc_init(void) 2410static int __init uvc_init(void)
2398{ 2411{
2399 int result; 2412 int ret;
2413
2414 uvc_debugfs_init();
2400 2415
2401 result = usb_register(&uvc_driver.driver); 2416 ret = usb_register(&uvc_driver.driver);
2402 if (result == 0) 2417 if (ret < 0) {
2403 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n"); 2418 uvc_debugfs_cleanup();
2404 return result; 2419 return ret;
2420 }
2421
2422 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
2423 return 0;
2405} 2424}
2406 2425
2407static void __exit uvc_cleanup(void) 2426static void __exit uvc_cleanup(void)
2408{ 2427{
2409 usb_deregister(&uvc_driver.driver); 2428 usb_deregister(&uvc_driver.driver);
2429 uvc_debugfs_cleanup();
2410} 2430}
2411 2431
2412module_init(uvc_init); 2432module_init(uvc_init);
diff --git a/drivers/media/video/uvc/uvc_isight.c b/drivers/media/video/uvc/uvc_isight.c
index 74bbe8f18f3e..8510e7259e76 100644
--- a/drivers/media/video/uvc/uvc_isight.c
+++ b/drivers/media/video/uvc/uvc_isight.c
@@ -74,7 +74,7 @@ static int isight_decode(struct uvc_video_queue *queue, struct uvc_buffer *buf,
74 * Empty buffers (bytesused == 0) don't trigger end of frame detection 74 * Empty buffers (bytesused == 0) don't trigger end of frame detection
75 * as it doesn't make sense to return an empty buffer. 75 * as it doesn't make sense to return an empty buffer.
76 */ 76 */
77 if (is_header && buf->buf.bytesused != 0) { 77 if (is_header && buf->bytesused != 0) {
78 buf->state = UVC_BUF_STATE_DONE; 78 buf->state = UVC_BUF_STATE_DONE;
79 return -EAGAIN; 79 return -EAGAIN;
80 } 80 }
@@ -83,13 +83,13 @@ static int isight_decode(struct uvc_video_queue *queue, struct uvc_buffer *buf,
83 * contain no data. 83 * contain no data.
84 */ 84 */
85 if (!is_header) { 85 if (!is_header) {
86 maxlen = buf->buf.length - buf->buf.bytesused; 86 maxlen = buf->length - buf->bytesused;
87 mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused; 87 mem = buf->mem + buf->bytesused;
88 nbytes = min(len, maxlen); 88 nbytes = min(len, maxlen);
89 memcpy(mem, data, nbytes); 89 memcpy(mem, data, nbytes);
90 buf->buf.bytesused += nbytes; 90 buf->bytesused += nbytes;
91 91
92 if (len > maxlen || buf->buf.bytesused == buf->buf.length) { 92 if (len > maxlen || buf->bytesused == buf->length) {
93 uvc_trace(UVC_TRACE_FRAME, "Frame complete " 93 uvc_trace(UVC_TRACE_FRAME, "Frame complete "
94 "(overflow).\n"); 94 "(overflow).\n");
95 buf->state = UVC_BUF_STATE_DONE; 95 buf->state = UVC_BUF_STATE_DONE;
diff --git a/drivers/media/video/uvc/uvc_queue.c b/drivers/media/video/uvc/uvc_queue.c
index 677691c44500..518f77d3a4d8 100644
--- a/drivers/media/video/uvc/uvc_queue.c
+++ b/drivers/media/video/uvc/uvc_queue.c
@@ -11,6 +11,7 @@
11 * 11 *
12 */ 12 */
13 13
14#include <linux/atomic.h>
14#include <linux/kernel.h> 15#include <linux/kernel.h>
15#include <linux/mm.h> 16#include <linux/mm.h>
16#include <linux/list.h> 17#include <linux/list.h>
@@ -19,7 +20,7 @@
19#include <linux/videodev2.h> 20#include <linux/videodev2.h>
20#include <linux/vmalloc.h> 21#include <linux/vmalloc.h>
21#include <linux/wait.h> 22#include <linux/wait.h>
22#include <linux/atomic.h> 23#include <media/videobuf2-vmalloc.h>
23 24
24#include "uvcvideo.h" 25#include "uvcvideo.h"
25 26
@@ -29,467 +30,211 @@
29 * Video queues is initialized by uvc_queue_init(). The function performs 30 * Video queues is initialized by uvc_queue_init(). The function performs
30 * basic initialization of the uvc_video_queue struct and never fails. 31 * basic initialization of the uvc_video_queue struct and never fails.
31 * 32 *
32 * Video buffer allocation and freeing are performed by uvc_alloc_buffers and 33 * Video buffers are managed by videobuf2. The driver uses a mutex to protect
33 * uvc_free_buffers respectively. The former acquires the video queue lock, 34 * the videobuf2 queue operations by serializing calls to videobuf2 and a
34 * while the later must be called with the lock held (so that allocation can 35 * spinlock to protect the IRQ queue that holds the buffers to be processed by
35 * free previously allocated buffers). Trying to free buffers that are mapped 36 * the driver.
36 * to user space will return -EBUSY.
37 *
38 * Video buffers are managed using two queues. However, unlike most USB video
39 * drivers that use an in queue and an out queue, we use a main queue to hold
40 * all queued buffers (both 'empty' and 'done' buffers), and an irq queue to
41 * hold empty buffers. This design (copied from video-buf) minimizes locking
42 * in interrupt, as only one queue is shared between interrupt and user
43 * contexts.
44 *
45 * Use cases
46 * ---------
47 *
48 * Unless stated otherwise, all operations that modify the irq buffers queue
49 * are protected by the irq spinlock.
50 *
51 * 1. The user queues the buffers, starts streaming and dequeues a buffer.
52 *
53 * The buffers are added to the main and irq queues. Both operations are
54 * protected by the queue lock, and the later is protected by the irq
55 * spinlock as well.
56 *
57 * The completion handler fetches a buffer from the irq queue and fills it
58 * with video data. If no buffer is available (irq queue empty), the handler
59 * returns immediately.
60 *
61 * When the buffer is full, the completion handler removes it from the irq
62 * queue, marks it as done (UVC_BUF_STATE_DONE) and wakes its wait queue.
63 * At that point, any process waiting on the buffer will be woken up. If a
64 * process tries to dequeue a buffer after it has been marked done, the
65 * dequeing will succeed immediately.
66 *
67 * 2. Buffers are queued, user is waiting on a buffer and the device gets
68 * disconnected.
69 *
70 * When the device is disconnected, the kernel calls the completion handler
71 * with an appropriate status code. The handler marks all buffers in the
72 * irq queue as being erroneous (UVC_BUF_STATE_ERROR) and wakes them up so
73 * that any process waiting on a buffer gets woken up.
74 *
75 * Waking up up the first buffer on the irq list is not enough, as the
76 * process waiting on the buffer might restart the dequeue operation
77 * immediately.
78 *
79 */ 37 */
80 38
81void uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type, 39/* -----------------------------------------------------------------------------
82 int drop_corrupted) 40 * videobuf2 queue operations
83{
84 mutex_init(&queue->mutex);
85 spin_lock_init(&queue->irqlock);
86 INIT_LIST_HEAD(&queue->mainqueue);
87 INIT_LIST_HEAD(&queue->irqqueue);
88 queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0;
89 queue->type = type;
90}
91
92/*
93 * Free the video buffers.
94 *
95 * This function must be called with the queue lock held.
96 */ 41 */
97static int __uvc_free_buffers(struct uvc_video_queue *queue) 42
43static int uvc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
44 unsigned int *nbuffers, unsigned int *nplanes,
45 unsigned int sizes[], void *alloc_ctxs[])
98{ 46{
99 unsigned int i; 47 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
48 struct uvc_streaming *stream =
49 container_of(queue, struct uvc_streaming, queue);
100 50
101 for (i = 0; i < queue->count; ++i) { 51 if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
102 if (queue->buffer[i].vma_use_count != 0) 52 *nbuffers = UVC_MAX_VIDEO_BUFFERS;
103 return -EBUSY;
104 }
105 53
106 if (queue->count) { 54 *nplanes = 1;
107 uvc_queue_cancel(queue, 0); 55
108 INIT_LIST_HEAD(&queue->mainqueue); 56 sizes[0] = stream->ctrl.dwMaxVideoFrameSize;
109 vfree(queue->mem);
110 queue->count = 0;
111 }
112 57
113 return 0; 58 return 0;
114} 59}
115 60
116int uvc_free_buffers(struct uvc_video_queue *queue) 61static int uvc_buffer_prepare(struct vb2_buffer *vb)
117{ 62{
118 int ret; 63 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
64 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
119 65
120 mutex_lock(&queue->mutex); 66 if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
121 ret = __uvc_free_buffers(queue); 67 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
122 mutex_unlock(&queue->mutex); 68 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
69 return -EINVAL;
70 }
123 71
124 return ret; 72 if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
125} 73 return -ENODEV;
126 74
127/* 75 buf->state = UVC_BUF_STATE_QUEUED;
128 * Allocate the video buffers. 76 buf->error = 0;
129 * 77 buf->mem = vb2_plane_vaddr(vb, 0);
130 * Pages are reserved to make sure they will not be swapped, as they will be 78 buf->length = vb2_plane_size(vb, 0);
131 * filled in the URB completion handler. 79 if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
132 * 80 buf->bytesused = 0;
133 * Buffers will be individually mapped, so they must all be page aligned. 81 else
134 */ 82 buf->bytesused = vb2_get_plane_payload(vb, 0);
135int uvc_alloc_buffers(struct uvc_video_queue *queue, unsigned int nbuffers,
136 unsigned int buflength)
137{
138 unsigned int bufsize = PAGE_ALIGN(buflength);
139 unsigned int i;
140 void *mem = NULL;
141 int ret;
142 83
143 if (nbuffers > UVC_MAX_VIDEO_BUFFERS) 84 return 0;
144 nbuffers = UVC_MAX_VIDEO_BUFFERS; 85}
145 86
146 mutex_lock(&queue->mutex); 87static void uvc_buffer_queue(struct vb2_buffer *vb)
88{
89 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
90 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
91 unsigned long flags;
147 92
148 if ((ret = __uvc_free_buffers(queue)) < 0) 93 spin_lock_irqsave(&queue->irqlock, flags);
149 goto done; 94 if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
95 list_add_tail(&buf->queue, &queue->irqqueue);
96 } else {
97 /* If the device is disconnected return the buffer to userspace
98 * directly. The next QBUF call will fail with -ENODEV.
99 */
100 buf->state = UVC_BUF_STATE_ERROR;
101 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
102 }
150 103
151 /* Bail out if no buffers should be allocated. */ 104 spin_unlock_irqrestore(&queue->irqlock, flags);
152 if (nbuffers == 0) 105}
153 goto done;
154 106
155 /* Decrement the number of buffers until allocation succeeds. */ 107static int uvc_buffer_finish(struct vb2_buffer *vb)
156 for (; nbuffers > 0; --nbuffers) { 108{
157 mem = vmalloc_32(nbuffers * bufsize); 109 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
158 if (mem != NULL) 110 struct uvc_streaming *stream =
159 break; 111 container_of(queue, struct uvc_streaming, queue);
160 } 112 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
161 113
162 if (mem == NULL) { 114 uvc_video_clock_update(stream, &vb->v4l2_buf, buf);
163 ret = -ENOMEM; 115 return 0;
164 goto done; 116}
165 }
166 117
167 for (i = 0; i < nbuffers; ++i) { 118static struct vb2_ops uvc_queue_qops = {
168 memset(&queue->buffer[i], 0, sizeof queue->buffer[i]); 119 .queue_setup = uvc_queue_setup,
169 queue->buffer[i].buf.index = i; 120 .buf_prepare = uvc_buffer_prepare,
170 queue->buffer[i].buf.m.offset = i * bufsize; 121 .buf_queue = uvc_buffer_queue,
171 queue->buffer[i].buf.length = buflength; 122 .buf_finish = uvc_buffer_finish,
172 queue->buffer[i].buf.type = queue->type; 123};
173 queue->buffer[i].buf.field = V4L2_FIELD_NONE;
174 queue->buffer[i].buf.memory = V4L2_MEMORY_MMAP;
175 queue->buffer[i].buf.flags = 0;
176 init_waitqueue_head(&queue->buffer[i].wait);
177 }
178 124
179 queue->mem = mem; 125void uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
180 queue->count = nbuffers; 126 int drop_corrupted)
181 queue->buf_size = bufsize; 127{
182 ret = nbuffers; 128 queue->queue.type = type;
129 queue->queue.io_modes = VB2_MMAP;
130 queue->queue.drv_priv = queue;
131 queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
132 queue->queue.ops = &uvc_queue_qops;
133 queue->queue.mem_ops = &vb2_vmalloc_memops;
134 vb2_queue_init(&queue->queue);
183 135
184done: 136 mutex_init(&queue->mutex);
185 mutex_unlock(&queue->mutex); 137 spin_lock_init(&queue->irqlock);
186 return ret; 138 INIT_LIST_HEAD(&queue->irqqueue);
139 queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0;
187} 140}
188 141
189/* 142/* -----------------------------------------------------------------------------
190 * Check if buffers have been allocated. 143 * V4L2 queue operations
191 */ 144 */
192int uvc_queue_allocated(struct uvc_video_queue *queue) 145
146int uvc_alloc_buffers(struct uvc_video_queue *queue,
147 struct v4l2_requestbuffers *rb)
193{ 148{
194 int allocated; 149 int ret;
195 150
196 mutex_lock(&queue->mutex); 151 mutex_lock(&queue->mutex);
197 allocated = queue->count != 0; 152 ret = vb2_reqbufs(&queue->queue, rb);
198 mutex_unlock(&queue->mutex); 153 mutex_unlock(&queue->mutex);
199 154
200 return allocated; 155 return ret ? ret : rb->count;
201} 156}
202 157
203static void __uvc_query_buffer(struct uvc_buffer *buf, 158void uvc_free_buffers(struct uvc_video_queue *queue)
204 struct v4l2_buffer *v4l2_buf)
205{ 159{
206 memcpy(v4l2_buf, &buf->buf, sizeof *v4l2_buf); 160 mutex_lock(&queue->mutex);
207 161 vb2_queue_release(&queue->queue);
208 if (buf->vma_use_count) 162 mutex_unlock(&queue->mutex);
209 v4l2_buf->flags |= V4L2_BUF_FLAG_MAPPED;
210
211 switch (buf->state) {
212 case UVC_BUF_STATE_ERROR:
213 case UVC_BUF_STATE_DONE:
214 v4l2_buf->flags |= V4L2_BUF_FLAG_DONE;
215 break;
216 case UVC_BUF_STATE_QUEUED:
217 case UVC_BUF_STATE_ACTIVE:
218 case UVC_BUF_STATE_READY:
219 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
220 break;
221 case UVC_BUF_STATE_IDLE:
222 default:
223 break;
224 }
225} 163}
226 164
227int uvc_query_buffer(struct uvc_video_queue *queue, 165int uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
228 struct v4l2_buffer *v4l2_buf)
229{ 166{
230 int ret = 0; 167 int ret;
231 168
232 mutex_lock(&queue->mutex); 169 mutex_lock(&queue->mutex);
233 if (v4l2_buf->index >= queue->count) { 170 ret = vb2_querybuf(&queue->queue, buf);
234 ret = -EINVAL;
235 goto done;
236 }
237
238 __uvc_query_buffer(&queue->buffer[v4l2_buf->index], v4l2_buf);
239
240done:
241 mutex_unlock(&queue->mutex); 171 mutex_unlock(&queue->mutex);
172
242 return ret; 173 return ret;
243} 174}
244 175
245/* 176int uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
246 * Queue a video buffer. Attempting to queue a buffer that has already been
247 * queued will return -EINVAL.
248 */
249int uvc_queue_buffer(struct uvc_video_queue *queue,
250 struct v4l2_buffer *v4l2_buf)
251{ 177{
252 struct uvc_buffer *buf; 178 int ret;
253 unsigned long flags;
254 int ret = 0;
255
256 uvc_trace(UVC_TRACE_CAPTURE, "Queuing buffer %u.\n", v4l2_buf->index);
257
258 if (v4l2_buf->type != queue->type ||
259 v4l2_buf->memory != V4L2_MEMORY_MMAP) {
260 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
261 "and/or memory (%u).\n", v4l2_buf->type,
262 v4l2_buf->memory);
263 return -EINVAL;
264 }
265 179
266 mutex_lock(&queue->mutex); 180 mutex_lock(&queue->mutex);
267 if (v4l2_buf->index >= queue->count) { 181 ret = vb2_qbuf(&queue->queue, buf);
268 uvc_trace(UVC_TRACE_CAPTURE, "[E] Out of range index.\n");
269 ret = -EINVAL;
270 goto done;
271 }
272
273 buf = &queue->buffer[v4l2_buf->index];
274 if (buf->state != UVC_BUF_STATE_IDLE) {
275 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state "
276 "(%u).\n", buf->state);
277 ret = -EINVAL;
278 goto done;
279 }
280
281 if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
282 v4l2_buf->bytesused > buf->buf.length) {
283 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
284 ret = -EINVAL;
285 goto done;
286 }
287
288 spin_lock_irqsave(&queue->irqlock, flags);
289 if (queue->flags & UVC_QUEUE_DISCONNECTED) {
290 spin_unlock_irqrestore(&queue->irqlock, flags);
291 ret = -ENODEV;
292 goto done;
293 }
294 buf->state = UVC_BUF_STATE_QUEUED;
295 if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
296 buf->buf.bytesused = 0;
297 else
298 buf->buf.bytesused = v4l2_buf->bytesused;
299
300 list_add_tail(&buf->stream, &queue->mainqueue);
301 list_add_tail(&buf->queue, &queue->irqqueue);
302 spin_unlock_irqrestore(&queue->irqlock, flags);
303
304done:
305 mutex_unlock(&queue->mutex); 182 mutex_unlock(&queue->mutex);
306 return ret;
307}
308 183
309static int uvc_queue_waiton(struct uvc_buffer *buf, int nonblocking) 184 return ret;
310{
311 if (nonblocking) {
312 return (buf->state != UVC_BUF_STATE_QUEUED &&
313 buf->state != UVC_BUF_STATE_ACTIVE &&
314 buf->state != UVC_BUF_STATE_READY)
315 ? 0 : -EAGAIN;
316 }
317
318 return wait_event_interruptible(buf->wait,
319 buf->state != UVC_BUF_STATE_QUEUED &&
320 buf->state != UVC_BUF_STATE_ACTIVE &&
321 buf->state != UVC_BUF_STATE_READY);
322} 185}
323 186
324/* 187int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
325 * Dequeue a video buffer. If nonblocking is false, block until a buffer is 188 int nonblocking)
326 * available.
327 */
328int uvc_dequeue_buffer(struct uvc_video_queue *queue,
329 struct v4l2_buffer *v4l2_buf, int nonblocking)
330{ 189{
331 struct uvc_buffer *buf; 190 int ret;
332 int ret = 0;
333
334 if (v4l2_buf->type != queue->type ||
335 v4l2_buf->memory != V4L2_MEMORY_MMAP) {
336 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
337 "and/or memory (%u).\n", v4l2_buf->type,
338 v4l2_buf->memory);
339 return -EINVAL;
340 }
341 191
342 mutex_lock(&queue->mutex); 192 mutex_lock(&queue->mutex);
343 if (list_empty(&queue->mainqueue)) { 193 ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
344 uvc_trace(UVC_TRACE_CAPTURE, "[E] Empty buffer queue.\n");
345 ret = -EINVAL;
346 goto done;
347 }
348
349 buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
350 if ((ret = uvc_queue_waiton(buf, nonblocking)) < 0)
351 goto done;
352
353 uvc_trace(UVC_TRACE_CAPTURE, "Dequeuing buffer %u (%u, %u bytes).\n",
354 buf->buf.index, buf->state, buf->buf.bytesused);
355
356 switch (buf->state) {
357 case UVC_BUF_STATE_ERROR:
358 uvc_trace(UVC_TRACE_CAPTURE, "[W] Corrupted data "
359 "(transmission error).\n");
360 ret = -EIO;
361 case UVC_BUF_STATE_DONE:
362 buf->state = UVC_BUF_STATE_IDLE;
363 break;
364
365 case UVC_BUF_STATE_IDLE:
366 case UVC_BUF_STATE_QUEUED:
367 case UVC_BUF_STATE_ACTIVE:
368 case UVC_BUF_STATE_READY:
369 default:
370 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state %u "
371 "(driver bug?).\n", buf->state);
372 ret = -EINVAL;
373 goto done;
374 }
375
376 list_del(&buf->stream);
377 __uvc_query_buffer(buf, v4l2_buf);
378
379done:
380 mutex_unlock(&queue->mutex); 194 mutex_unlock(&queue->mutex);
381 return ret;
382}
383 195
384/* 196 return ret;
385 * VMA operations.
386 */
387static void uvc_vm_open(struct vm_area_struct *vma)
388{
389 struct uvc_buffer *buffer = vma->vm_private_data;
390 buffer->vma_use_count++;
391}
392
393static void uvc_vm_close(struct vm_area_struct *vma)
394{
395 struct uvc_buffer *buffer = vma->vm_private_data;
396 buffer->vma_use_count--;
397} 197}
398 198
399static const struct vm_operations_struct uvc_vm_ops = {
400 .open = uvc_vm_open,
401 .close = uvc_vm_close,
402};
403
404/*
405 * Memory-map a video buffer.
406 *
407 * This function implements video buffers memory mapping and is intended to be
408 * used by the device mmap handler.
409 */
410int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma) 199int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
411{ 200{
412 struct uvc_buffer *uninitialized_var(buffer); 201 int ret;
413 struct page *page;
414 unsigned long addr, start, size;
415 unsigned int i;
416 int ret = 0;
417
418 start = vma->vm_start;
419 size = vma->vm_end - vma->vm_start;
420 202
421 mutex_lock(&queue->mutex); 203 mutex_lock(&queue->mutex);
204 ret = vb2_mmap(&queue->queue, vma);
205 mutex_unlock(&queue->mutex);
422 206
423 for (i = 0; i < queue->count; ++i) { 207 return ret;
424 buffer = &queue->buffer[i]; 208}
425 if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
426 break;
427 }
428
429 if (i == queue->count || PAGE_ALIGN(size) != queue->buf_size) {
430 ret = -EINVAL;
431 goto done;
432 }
433
434 /*
435 * VM_IO marks the area as being an mmaped region for I/O to a
436 * device. It also prevents the region from being core dumped.
437 */
438 vma->vm_flags |= VM_IO;
439
440 addr = (unsigned long)queue->mem + buffer->buf.m.offset;
441#ifdef CONFIG_MMU
442 while (size > 0) {
443 page = vmalloc_to_page((void *)addr);
444 if ((ret = vm_insert_page(vma, start, page)) < 0)
445 goto done;
446
447 start += PAGE_SIZE;
448 addr += PAGE_SIZE;
449 size -= PAGE_SIZE;
450 }
451#endif
452 209
453 vma->vm_ops = &uvc_vm_ops; 210unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
454 vma->vm_private_data = buffer; 211 poll_table *wait)
455 uvc_vm_open(vma); 212{
213 unsigned int ret;
456 214
457done: 215 mutex_lock(&queue->mutex);
216 ret = vb2_poll(&queue->queue, file, wait);
458 mutex_unlock(&queue->mutex); 217 mutex_unlock(&queue->mutex);
218
459 return ret; 219 return ret;
460} 220}
461 221
462/* 222/* -----------------------------------------------------------------------------
463 * Poll the video queue.
464 * 223 *
465 * This function implements video queue polling and is intended to be used by
466 * the device poll handler.
467 */ 224 */
468unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file, 225
469 poll_table *wait) 226/*
227 * Check if buffers have been allocated.
228 */
229int uvc_queue_allocated(struct uvc_video_queue *queue)
470{ 230{
471 struct uvc_buffer *buf; 231 int allocated;
472 unsigned int mask = 0;
473 232
474 mutex_lock(&queue->mutex); 233 mutex_lock(&queue->mutex);
475 if (list_empty(&queue->mainqueue)) { 234 allocated = vb2_is_busy(&queue->queue);
476 mask |= POLLERR;
477 goto done;
478 }
479 buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
480
481 poll_wait(file, &buf->wait, wait);
482 if (buf->state == UVC_BUF_STATE_DONE ||
483 buf->state == UVC_BUF_STATE_ERROR) {
484 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
485 mask |= POLLIN | POLLRDNORM;
486 else
487 mask |= POLLOUT | POLLWRNORM;
488 }
489
490done:
491 mutex_unlock(&queue->mutex); 235 mutex_unlock(&queue->mutex);
492 return mask; 236
237 return allocated;
493} 238}
494 239
495#ifndef CONFIG_MMU 240#ifndef CONFIG_MMU
@@ -515,7 +260,7 @@ unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
515 ret = -EINVAL; 260 ret = -EINVAL;
516 goto done; 261 goto done;
517 } 262 }
518 ret = (unsigned long)queue->mem + buffer->buf.m.offset; 263 ret = (unsigned long)buf->mem;
519done: 264done:
520 mutex_unlock(&queue->mutex); 265 mutex_unlock(&queue->mutex);
521 return ret; 266 return ret;
@@ -540,27 +285,24 @@ done:
540 */ 285 */
541int uvc_queue_enable(struct uvc_video_queue *queue, int enable) 286int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
542{ 287{
543 unsigned int i; 288 unsigned long flags;
544 int ret = 0; 289 int ret;
545 290
546 mutex_lock(&queue->mutex); 291 mutex_lock(&queue->mutex);
547 if (enable) { 292 if (enable) {
548 if (uvc_queue_streaming(queue)) { 293 ret = vb2_streamon(&queue->queue, queue->queue.type);
549 ret = -EBUSY; 294 if (ret < 0)
550 goto done; 295 goto done;
551 } 296
552 queue->flags |= UVC_QUEUE_STREAMING;
553 queue->buf_used = 0; 297 queue->buf_used = 0;
554 } else { 298 } else {
555 uvc_queue_cancel(queue, 0); 299 ret = vb2_streamoff(&queue->queue, queue->queue.type);
556 INIT_LIST_HEAD(&queue->mainqueue); 300 if (ret < 0)
557 301 goto done;
558 for (i = 0; i < queue->count; ++i) {
559 queue->buffer[i].error = 0;
560 queue->buffer[i].state = UVC_BUF_STATE_IDLE;
561 }
562 302
563 queue->flags &= ~UVC_QUEUE_STREAMING; 303 spin_lock_irqsave(&queue->irqlock, flags);
304 INIT_LIST_HEAD(&queue->irqqueue);
305 spin_unlock_irqrestore(&queue->irqlock, flags);
564 } 306 }
565 307
566done: 308done:
@@ -591,12 +333,12 @@ void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
591 queue); 333 queue);
592 list_del(&buf->queue); 334 list_del(&buf->queue);
593 buf->state = UVC_BUF_STATE_ERROR; 335 buf->state = UVC_BUF_STATE_ERROR;
594 wake_up(&buf->wait); 336 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
595 } 337 }
596 /* This must be protected by the irqlock spinlock to avoid race 338 /* This must be protected by the irqlock spinlock to avoid race
597 * conditions between uvc_queue_buffer and the disconnection event that 339 * conditions between uvc_buffer_queue and the disconnection event that
598 * could result in an interruptible wait in uvc_dequeue_buffer. Do not 340 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
599 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED 341 * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED
600 * state outside the queue code. 342 * state outside the queue code.
601 */ 343 */
602 if (disconnect) 344 if (disconnect)
@@ -613,14 +355,12 @@ struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
613 if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) { 355 if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
614 buf->error = 0; 356 buf->error = 0;
615 buf->state = UVC_BUF_STATE_QUEUED; 357 buf->state = UVC_BUF_STATE_QUEUED;
616 buf->buf.bytesused = 0; 358 vb2_set_plane_payload(&buf->buf, 0, 0);
617 return buf; 359 return buf;
618 } 360 }
619 361
620 spin_lock_irqsave(&queue->irqlock, flags); 362 spin_lock_irqsave(&queue->irqlock, flags);
621 list_del(&buf->queue); 363 list_del(&buf->queue);
622 buf->error = 0;
623 buf->state = UVC_BUF_STATE_DONE;
624 if (!list_empty(&queue->irqqueue)) 364 if (!list_empty(&queue->irqqueue))
625 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer, 365 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
626 queue); 366 queue);
@@ -628,7 +368,9 @@ struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
628 nextbuf = NULL; 368 nextbuf = NULL;
629 spin_unlock_irqrestore(&queue->irqlock, flags); 369 spin_unlock_irqrestore(&queue->irqlock, flags);
630 370
631 wake_up(&buf->wait); 371 buf->state = buf->error ? VB2_BUF_STATE_ERROR : UVC_BUF_STATE_DONE;
372 vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
373 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
374
632 return nextbuf; 375 return nextbuf;
633} 376}
634
diff --git a/drivers/media/video/uvc/uvc_v4l2.c b/drivers/media/video/uvc/uvc_v4l2.c
index dadf11f704dc..2ae4f880ea05 100644
--- a/drivers/media/video/uvc/uvc_v4l2.c
+++ b/drivers/media/video/uvc/uvc_v4l2.c
@@ -58,6 +58,15 @@ static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
58 break; 58 break;
59 59
60 case V4L2_CTRL_TYPE_MENU: 60 case V4L2_CTRL_TYPE_MENU:
61 /* Prevent excessive memory consumption, as well as integer
62 * overflows.
63 */
64 if (xmap->menu_count == 0 ||
65 xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) {
66 ret = -EINVAL;
67 goto done;
68 }
69
61 size = xmap->menu_count * sizeof(*map->menu_info); 70 size = xmap->menu_count * sizeof(*map->menu_info);
62 map->menu_info = kmalloc(size, GFP_KERNEL); 71 map->menu_info = kmalloc(size, GFP_KERNEL);
63 if (map->menu_info == NULL) { 72 if (map->menu_info == NULL) {
@@ -513,10 +522,7 @@ static int uvc_v4l2_release(struct file *file)
513 /* Only free resources if this is a privileged handle. */ 522 /* Only free resources if this is a privileged handle. */
514 if (uvc_has_privileges(handle)) { 523 if (uvc_has_privileges(handle)) {
515 uvc_video_enable(stream, 0); 524 uvc_video_enable(stream, 0);
516 525 uvc_free_buffers(&stream->queue);
517 if (uvc_free_buffers(&stream->queue) < 0)
518 uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
519 "free buffers.\n");
520 } 526 }
521 527
522 /* Release the file handle. */ 528 /* Release the file handle. */
@@ -914,19 +920,11 @@ static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
914 920
915 /* Buffers & streaming */ 921 /* Buffers & streaming */
916 case VIDIOC_REQBUFS: 922 case VIDIOC_REQBUFS:
917 {
918 struct v4l2_requestbuffers *rb = arg;
919
920 if (rb->type != stream->type ||
921 rb->memory != V4L2_MEMORY_MMAP)
922 return -EINVAL;
923
924 if ((ret = uvc_acquire_privileges(handle)) < 0) 923 if ((ret = uvc_acquire_privileges(handle)) < 0)
925 return ret; 924 return ret;
926 925
927 mutex_lock(&stream->mutex); 926 mutex_lock(&stream->mutex);
928 ret = uvc_alloc_buffers(&stream->queue, rb->count, 927 ret = uvc_alloc_buffers(&stream->queue, arg);
929 stream->ctrl.dwMaxVideoFrameSize);
930 mutex_unlock(&stream->mutex); 928 mutex_unlock(&stream->mutex);
931 if (ret < 0) 929 if (ret < 0)
932 return ret; 930 return ret;
@@ -934,18 +932,13 @@ static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
934 if (ret == 0) 932 if (ret == 0)
935 uvc_dismiss_privileges(handle); 933 uvc_dismiss_privileges(handle);
936 934
937 rb->count = ret;
938 ret = 0; 935 ret = 0;
939 break; 936 break;
940 }
941 937
942 case VIDIOC_QUERYBUF: 938 case VIDIOC_QUERYBUF:
943 { 939 {
944 struct v4l2_buffer *buf = arg; 940 struct v4l2_buffer *buf = arg;
945 941
946 if (buf->type != stream->type)
947 return -EINVAL;
948
949 if (!uvc_has_privileges(handle)) 942 if (!uvc_has_privileges(handle))
950 return -EBUSY; 943 return -EBUSY;
951 944
diff --git a/drivers/media/video/uvc/uvc_video.c b/drivers/media/video/uvc/uvc_video.c
index b015e8e5e8b0..c7e69b8f81c9 100644
--- a/drivers/media/video/uvc/uvc_video.c
+++ b/drivers/media/video/uvc/uvc_video.c
@@ -351,25 +351,553 @@ done:
351 return ret; 351 return ret;
352} 352}
353 353
354int uvc_commit_video(struct uvc_streaming *stream, 354static int uvc_commit_video(struct uvc_streaming *stream,
355 struct uvc_streaming_control *probe) 355 struct uvc_streaming_control *probe)
356{ 356{
357 return uvc_set_video_ctrl(stream, probe, 0); 357 return uvc_set_video_ctrl(stream, probe, 0);
358} 358}
359 359
360/* -----------------------------------------------------------------------------
361 * Clocks and timestamps
362 */
363
364static void
365uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf,
366 const __u8 *data, int len)
367{
368 struct uvc_clock_sample *sample;
369 unsigned int header_size;
370 bool has_pts = false;
371 bool has_scr = false;
372 unsigned long flags;
373 struct timespec ts;
374 u16 host_sof;
375 u16 dev_sof;
376
377 switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) {
378 case UVC_STREAM_PTS | UVC_STREAM_SCR:
379 header_size = 12;
380 has_pts = true;
381 has_scr = true;
382 break;
383 case UVC_STREAM_PTS:
384 header_size = 6;
385 has_pts = true;
386 break;
387 case UVC_STREAM_SCR:
388 header_size = 8;
389 has_scr = true;
390 break;
391 default:
392 header_size = 2;
393 break;
394 }
395
396 /* Check for invalid headers. */
397 if (len < header_size)
398 return;
399
400 /* Extract the timestamps:
401 *
402 * - store the frame PTS in the buffer structure
403 * - if the SCR field is present, retrieve the host SOF counter and
404 * kernel timestamps and store them with the SCR STC and SOF fields
405 * in the ring buffer
406 */
407 if (has_pts && buf != NULL)
408 buf->pts = get_unaligned_le32(&data[2]);
409
410 if (!has_scr)
411 return;
412
413 /* To limit the amount of data, drop SCRs with an SOF identical to the
414 * previous one.
415 */
416 dev_sof = get_unaligned_le16(&data[header_size - 2]);
417 if (dev_sof == stream->clock.last_sof)
418 return;
419
420 stream->clock.last_sof = dev_sof;
421
422 host_sof = usb_get_current_frame_number(stream->dev->udev);
423 ktime_get_ts(&ts);
424
425 /* The UVC specification allows device implementations that can't obtain
426 * the USB frame number to keep their own frame counters as long as they
427 * match the size and frequency of the frame number associated with USB
428 * SOF tokens. The SOF values sent by such devices differ from the USB
429 * SOF tokens by a fixed offset that needs to be estimated and accounted
430 * for to make timestamp recovery as accurate as possible.
431 *
432 * The offset is estimated the first time a device SOF value is received
433 * as the difference between the host and device SOF values. As the two
434 * SOF values can differ slightly due to transmission delays, consider
435 * that the offset is null if the difference is not higher than 10 ms
436 * (negative differences can not happen and are thus considered as an
437 * offset). The video commit control wDelay field should be used to
438 * compute a dynamic threshold instead of using a fixed 10 ms value, but
439 * devices don't report reliable wDelay values.
440 *
441 * See uvc_video_clock_host_sof() for an explanation regarding why only
442 * the 8 LSBs of the delta are kept.
443 */
444 if (stream->clock.sof_offset == (u16)-1) {
445 u16 delta_sof = (host_sof - dev_sof) & 255;
446 if (delta_sof >= 10)
447 stream->clock.sof_offset = delta_sof;
448 else
449 stream->clock.sof_offset = 0;
450 }
451
452 dev_sof = (dev_sof + stream->clock.sof_offset) & 2047;
453
454 spin_lock_irqsave(&stream->clock.lock, flags);
455
456 sample = &stream->clock.samples[stream->clock.head];
457 sample->dev_stc = get_unaligned_le32(&data[header_size - 6]);
458 sample->dev_sof = dev_sof;
459 sample->host_sof = host_sof;
460 sample->host_ts = ts;
461
462 /* Update the sliding window head and count. */
463 stream->clock.head = (stream->clock.head + 1) % stream->clock.size;
464
465 if (stream->clock.count < stream->clock.size)
466 stream->clock.count++;
467
468 spin_unlock_irqrestore(&stream->clock.lock, flags);
469}
470
471static int uvc_video_clock_init(struct uvc_streaming *stream)
472{
473 struct uvc_clock *clock = &stream->clock;
474
475 spin_lock_init(&clock->lock);
476 clock->head = 0;
477 clock->count = 0;
478 clock->size = 32;
479 clock->last_sof = -1;
480 clock->sof_offset = -1;
481
482 clock->samples = kmalloc(clock->size * sizeof(*clock->samples),
483 GFP_KERNEL);
484 if (clock->samples == NULL)
485 return -ENOMEM;
486
487 return 0;
488}
489
490static void uvc_video_clock_cleanup(struct uvc_streaming *stream)
491{
492 kfree(stream->clock.samples);
493 stream->clock.samples = NULL;
494}
495
496/*
497 * uvc_video_clock_host_sof - Return the host SOF value for a clock sample
498 *
499 * Host SOF counters reported by usb_get_current_frame_number() usually don't
500 * cover the whole 11-bits SOF range (0-2047) but are limited to the HCI frame
501 * schedule window. They can be limited to 8, 9 or 10 bits depending on the host
502 * controller and its configuration.
503 *
504 * We thus need to recover the SOF value corresponding to the host frame number.
505 * As the device and host frame numbers are sampled in a short interval, the
506 * difference between their values should be equal to a small delta plus an
507 * integer multiple of 256 caused by the host frame number limited precision.
508 *
509 * To obtain the recovered host SOF value, compute the small delta by masking
510 * the high bits of the host frame counter and device SOF difference and add it
511 * to the device SOF value.
512 */
513static u16 uvc_video_clock_host_sof(const struct uvc_clock_sample *sample)
514{
515 /* The delta value can be negative. */
516 s8 delta_sof;
517
518 delta_sof = (sample->host_sof - sample->dev_sof) & 255;
519
520 return (sample->dev_sof + delta_sof) & 2047;
521}
522
523/*
524 * uvc_video_clock_update - Update the buffer timestamp
525 *
526 * This function converts the buffer PTS timestamp to the host clock domain by
527 * going through the USB SOF clock domain and stores the result in the V4L2
528 * buffer timestamp field.
529 *
530 * The relationship between the device clock and the host clock isn't known.
531 * However, the device and the host share the common USB SOF clock which can be
532 * used to recover that relationship.
533 *
534 * The relationship between the device clock and the USB SOF clock is considered
535 * to be linear over the clock samples sliding window and is given by
536 *
537 * SOF = m * PTS + p
538 *
539 * Several methods to compute the slope (m) and intercept (p) can be used. As
540 * the clock drift should be small compared to the sliding window size, we
541 * assume that the line that goes through the points at both ends of the window
542 * is a good approximation. Naming those points P1 and P2, we get
543 *
544 * SOF = (SOF2 - SOF1) / (STC2 - STC1) * PTS
545 * + (SOF1 * STC2 - SOF2 * STC1) / (STC2 - STC1)
546 *
547 * or
548 *
549 * SOF = ((SOF2 - SOF1) * PTS + SOF1 * STC2 - SOF2 * STC1) / (STC2 - STC1) (1)
550 *
551 * to avoid loosing precision in the division. Similarly, the host timestamp is
552 * computed with
553 *
554 * TS = ((TS2 - TS1) * PTS + TS1 * SOF2 - TS2 * SOF1) / (SOF2 - SOF1) (2)
555 *
556 * SOF values are coded on 11 bits by USB. We extend their precision with 16
557 * decimal bits, leading to a 11.16 coding.
558 *
559 * TODO: To avoid surprises with device clock values, PTS/STC timestamps should
560 * be normalized using the nominal device clock frequency reported through the
561 * UVC descriptors.
562 *
563 * Both the PTS/STC and SOF counters roll over, after a fixed but device
564 * specific amount of time for PTS/STC and after 2048ms for SOF. As long as the
565 * sliding window size is smaller than the rollover period, differences computed
566 * on unsigned integers will produce the correct result. However, the p term in
567 * the linear relations will be miscomputed.
568 *
569 * To fix the issue, we subtract a constant from the PTS and STC values to bring
570 * PTS to half the 32 bit STC range. The sliding window STC values then fit into
571 * the 32 bit range without any rollover.
572 *
573 * Similarly, we add 2048 to the device SOF values to make sure that the SOF
574 * computed by (1) will never be smaller than 0. This offset is then compensated
575 * by adding 2048 to the SOF values used in (2). However, this doesn't prevent
576 * rollovers between (1) and (2): the SOF value computed by (1) can be slightly
577 * lower than 4096, and the host SOF counters can have rolled over to 2048. This
578 * case is handled by subtracting 2048 from the SOF value if it exceeds the host
579 * SOF value at the end of the sliding window.
580 *
581 * Finally we subtract a constant from the host timestamps to bring the first
582 * timestamp of the sliding window to 1s.
583 */
584void uvc_video_clock_update(struct uvc_streaming *stream,
585 struct v4l2_buffer *v4l2_buf,
586 struct uvc_buffer *buf)
587{
588 struct uvc_clock *clock = &stream->clock;
589 struct uvc_clock_sample *first;
590 struct uvc_clock_sample *last;
591 unsigned long flags;
592 struct timespec ts;
593 u32 delta_stc;
594 u32 y1, y2;
595 u32 x1, x2;
596 u32 mean;
597 u32 sof;
598 u32 div;
599 u32 rem;
600 u64 y;
601
602 spin_lock_irqsave(&clock->lock, flags);
603
604 if (clock->count < clock->size)
605 goto done;
606
607 first = &clock->samples[clock->head];
608 last = &clock->samples[(clock->head - 1) % clock->size];
609
610 /* First step, PTS to SOF conversion. */
611 delta_stc = buf->pts - (1UL << 31);
612 x1 = first->dev_stc - delta_stc;
613 x2 = last->dev_stc - delta_stc;
614 y1 = (first->dev_sof + 2048) << 16;
615 y2 = (last->dev_sof + 2048) << 16;
616
617 if (y2 < y1)
618 y2 += 2048 << 16;
619
620 y = (u64)(y2 - y1) * (1ULL << 31) + (u64)y1 * (u64)x2
621 - (u64)y2 * (u64)x1;
622 y = div_u64(y, x2 - x1);
623
624 sof = y;
625
626 uvc_trace(UVC_TRACE_CLOCK, "%s: PTS %u y %llu.%06llu SOF %u.%06llu "
627 "(x1 %u x2 %u y1 %u y2 %u SOF offset %u)\n",
628 stream->dev->name, buf->pts,
629 y >> 16, div_u64((y & 0xffff) * 1000000, 65536),
630 sof >> 16, div_u64(((u64)sof & 0xffff) * 1000000LLU, 65536),
631 x1, x2, y1, y2, clock->sof_offset);
632
633 /* Second step, SOF to host clock conversion. */
634 ts = timespec_sub(last->host_ts, first->host_ts);
635 x1 = (uvc_video_clock_host_sof(first) + 2048) << 16;
636 x2 = (uvc_video_clock_host_sof(last) + 2048) << 16;
637 y1 = NSEC_PER_SEC;
638 y2 = (ts.tv_sec + 1) * NSEC_PER_SEC + ts.tv_nsec;
639
640 if (x2 < x1)
641 x2 += 2048 << 16;
642
643 /* Interpolated and host SOF timestamps can wrap around at slightly
644 * different times. Handle this by adding or removing 2048 to or from
645 * the computed SOF value to keep it close to the SOF samples mean
646 * value.
647 */
648 mean = (x1 + x2) / 2;
649 if (mean - (1024 << 16) > sof)
650 sof += 2048 << 16;
651 else if (sof > mean + (1024 << 16))
652 sof -= 2048 << 16;
653
654 y = (u64)(y2 - y1) * (u64)sof + (u64)y1 * (u64)x2
655 - (u64)y2 * (u64)x1;
656 y = div_u64(y, x2 - x1);
657
658 div = div_u64_rem(y, NSEC_PER_SEC, &rem);
659 ts.tv_sec = first->host_ts.tv_sec - 1 + div;
660 ts.tv_nsec = first->host_ts.tv_nsec + rem;
661 if (ts.tv_nsec >= NSEC_PER_SEC) {
662 ts.tv_sec++;
663 ts.tv_nsec -= NSEC_PER_SEC;
664 }
665
666 uvc_trace(UVC_TRACE_CLOCK, "%s: SOF %u.%06llu y %llu ts %lu.%06lu "
667 "buf ts %lu.%06lu (x1 %u/%u/%u x2 %u/%u/%u y1 %u y2 %u)\n",
668 stream->dev->name,
669 sof >> 16, div_u64(((u64)sof & 0xffff) * 1000000LLU, 65536),
670 y, ts.tv_sec, ts.tv_nsec / NSEC_PER_USEC,
671 v4l2_buf->timestamp.tv_sec, v4l2_buf->timestamp.tv_usec,
672 x1, first->host_sof, first->dev_sof,
673 x2, last->host_sof, last->dev_sof, y1, y2);
674
675 /* Update the V4L2 buffer. */
676 v4l2_buf->timestamp.tv_sec = ts.tv_sec;
677 v4l2_buf->timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
678
679done:
680 spin_unlock_irqrestore(&stream->clock.lock, flags);
681}
682
360/* ------------------------------------------------------------------------ 683/* ------------------------------------------------------------------------
361 * Video codecs 684 * Stream statistics
362 */ 685 */
363 686
364/* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */ 687static void uvc_video_stats_decode(struct uvc_streaming *stream,
365#define UVC_STREAM_EOH (1 << 7) 688 const __u8 *data, int len)
366#define UVC_STREAM_ERR (1 << 6) 689{
367#define UVC_STREAM_STI (1 << 5) 690 unsigned int header_size;
368#define UVC_STREAM_RES (1 << 4) 691 bool has_pts = false;
369#define UVC_STREAM_SCR (1 << 3) 692 bool has_scr = false;
370#define UVC_STREAM_PTS (1 << 2) 693 u16 uninitialized_var(scr_sof);
371#define UVC_STREAM_EOF (1 << 1) 694 u32 uninitialized_var(scr_stc);
372#define UVC_STREAM_FID (1 << 0) 695 u32 uninitialized_var(pts);
696
697 if (stream->stats.stream.nb_frames == 0 &&
698 stream->stats.frame.nb_packets == 0)
699 ktime_get_ts(&stream->stats.stream.start_ts);
700
701 switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) {
702 case UVC_STREAM_PTS | UVC_STREAM_SCR:
703 header_size = 12;
704 has_pts = true;
705 has_scr = true;
706 break;
707 case UVC_STREAM_PTS:
708 header_size = 6;
709 has_pts = true;
710 break;
711 case UVC_STREAM_SCR:
712 header_size = 8;
713 has_scr = true;
714 break;
715 default:
716 header_size = 2;
717 break;
718 }
719
720 /* Check for invalid headers. */
721 if (len < header_size || data[0] < header_size) {
722 stream->stats.frame.nb_invalid++;
723 return;
724 }
725
726 /* Extract the timestamps. */
727 if (has_pts)
728 pts = get_unaligned_le32(&data[2]);
729
730 if (has_scr) {
731 scr_stc = get_unaligned_le32(&data[header_size - 6]);
732 scr_sof = get_unaligned_le16(&data[header_size - 2]);
733 }
734
735 /* Is PTS constant through the whole frame ? */
736 if (has_pts && stream->stats.frame.nb_pts) {
737 if (stream->stats.frame.pts != pts) {
738 stream->stats.frame.nb_pts_diffs++;
739 stream->stats.frame.last_pts_diff =
740 stream->stats.frame.nb_packets;
741 }
742 }
743
744 if (has_pts) {
745 stream->stats.frame.nb_pts++;
746 stream->stats.frame.pts = pts;
747 }
748
749 /* Do all frames have a PTS in their first non-empty packet, or before
750 * their first empty packet ?
751 */
752 if (stream->stats.frame.size == 0) {
753 if (len > header_size)
754 stream->stats.frame.has_initial_pts = has_pts;
755 if (len == header_size && has_pts)
756 stream->stats.frame.has_early_pts = true;
757 }
758
759 /* Do the SCR.STC and SCR.SOF fields vary through the frame ? */
760 if (has_scr && stream->stats.frame.nb_scr) {
761 if (stream->stats.frame.scr_stc != scr_stc)
762 stream->stats.frame.nb_scr_diffs++;
763 }
764
765 if (has_scr) {
766 /* Expand the SOF counter to 32 bits and store its value. */
767 if (stream->stats.stream.nb_frames > 0 ||
768 stream->stats.frame.nb_scr > 0)
769 stream->stats.stream.scr_sof_count +=
770 (scr_sof - stream->stats.stream.scr_sof) % 2048;
771 stream->stats.stream.scr_sof = scr_sof;
772
773 stream->stats.frame.nb_scr++;
774 stream->stats.frame.scr_stc = scr_stc;
775 stream->stats.frame.scr_sof = scr_sof;
776
777 if (scr_sof < stream->stats.stream.min_sof)
778 stream->stats.stream.min_sof = scr_sof;
779 if (scr_sof > stream->stats.stream.max_sof)
780 stream->stats.stream.max_sof = scr_sof;
781 }
782
783 /* Record the first non-empty packet number. */
784 if (stream->stats.frame.size == 0 && len > header_size)
785 stream->stats.frame.first_data = stream->stats.frame.nb_packets;
786
787 /* Update the frame size. */
788 stream->stats.frame.size += len - header_size;
789
790 /* Update the packets counters. */
791 stream->stats.frame.nb_packets++;
792 if (len > header_size)
793 stream->stats.frame.nb_empty++;
794
795 if (data[1] & UVC_STREAM_ERR)
796 stream->stats.frame.nb_errors++;
797}
798
799static void uvc_video_stats_update(struct uvc_streaming *stream)
800{
801 struct uvc_stats_frame *frame = &stream->stats.frame;
802
803 uvc_trace(UVC_TRACE_STATS, "frame %u stats: %u/%u/%u packets, "
804 "%u/%u/%u pts (%searly %sinitial), %u/%u scr, "
805 "last pts/stc/sof %u/%u/%u\n",
806 stream->sequence, frame->first_data,
807 frame->nb_packets - frame->nb_empty, frame->nb_packets,
808 frame->nb_pts_diffs, frame->last_pts_diff, frame->nb_pts,
809 frame->has_early_pts ? "" : "!",
810 frame->has_initial_pts ? "" : "!",
811 frame->nb_scr_diffs, frame->nb_scr,
812 frame->pts, frame->scr_stc, frame->scr_sof);
813
814 stream->stats.stream.nb_frames++;
815 stream->stats.stream.nb_packets += stream->stats.frame.nb_packets;
816 stream->stats.stream.nb_empty += stream->stats.frame.nb_empty;
817 stream->stats.stream.nb_errors += stream->stats.frame.nb_errors;
818 stream->stats.stream.nb_invalid += stream->stats.frame.nb_invalid;
819
820 if (frame->has_early_pts)
821 stream->stats.stream.nb_pts_early++;
822 if (frame->has_initial_pts)
823 stream->stats.stream.nb_pts_initial++;
824 if (frame->last_pts_diff <= frame->first_data)
825 stream->stats.stream.nb_pts_constant++;
826 if (frame->nb_scr >= frame->nb_packets - frame->nb_empty)
827 stream->stats.stream.nb_scr_count_ok++;
828 if (frame->nb_scr_diffs + 1 == frame->nb_scr)
829 stream->stats.stream.nb_scr_diffs_ok++;
830
831 memset(&stream->stats.frame, 0, sizeof(stream->stats.frame));
832}
833
834size_t uvc_video_stats_dump(struct uvc_streaming *stream, char *buf,
835 size_t size)
836{
837 unsigned int scr_sof_freq;
838 unsigned int duration;
839 struct timespec ts;
840 size_t count = 0;
841
842 ts.tv_sec = stream->stats.stream.stop_ts.tv_sec
843 - stream->stats.stream.start_ts.tv_sec;
844 ts.tv_nsec = stream->stats.stream.stop_ts.tv_nsec
845 - stream->stats.stream.start_ts.tv_nsec;
846 if (ts.tv_nsec < 0) {
847 ts.tv_sec--;
848 ts.tv_nsec += 1000000000;
849 }
850
851 /* Compute the SCR.SOF frequency estimate. At the nominal 1kHz SOF
852 * frequency this will not overflow before more than 1h.
853 */
854 duration = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
855 if (duration != 0)
856 scr_sof_freq = stream->stats.stream.scr_sof_count * 1000
857 / duration;
858 else
859 scr_sof_freq = 0;
860
861 count += scnprintf(buf + count, size - count,
862 "frames: %u\npackets: %u\nempty: %u\n"
863 "errors: %u\ninvalid: %u\n",
864 stream->stats.stream.nb_frames,
865 stream->stats.stream.nb_packets,
866 stream->stats.stream.nb_empty,
867 stream->stats.stream.nb_errors,
868 stream->stats.stream.nb_invalid);
869 count += scnprintf(buf + count, size - count,
870 "pts: %u early, %u initial, %u ok\n",
871 stream->stats.stream.nb_pts_early,
872 stream->stats.stream.nb_pts_initial,
873 stream->stats.stream.nb_pts_constant);
874 count += scnprintf(buf + count, size - count,
875 "scr: %u count ok, %u diff ok\n",
876 stream->stats.stream.nb_scr_count_ok,
877 stream->stats.stream.nb_scr_diffs_ok);
878 count += scnprintf(buf + count, size - count,
879 "sof: %u <= sof <= %u, freq %u.%03u kHz\n",
880 stream->stats.stream.min_sof,
881 stream->stats.stream.max_sof,
882 scr_sof_freq / 1000, scr_sof_freq % 1000);
883
884 return count;
885}
886
887static void uvc_video_stats_start(struct uvc_streaming *stream)
888{
889 memset(&stream->stats, 0, sizeof(stream->stats));
890 stream->stats.stream.min_sof = 2048;
891}
892
893static void uvc_video_stats_stop(struct uvc_streaming *stream)
894{
895 ktime_get_ts(&stream->stats.stream.stop_ts);
896}
897
898/* ------------------------------------------------------------------------
899 * Video codecs
900 */
373 901
374/* Video payload decoding is handled by uvc_video_decode_start(), 902/* Video payload decoding is handled by uvc_video_decode_start(),
375 * uvc_video_decode_data() and uvc_video_decode_end(). 903 * uvc_video_decode_data() and uvc_video_decode_end().
@@ -416,14 +944,9 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
416 * - bHeaderLength value must be at least 2 bytes (see above) 944 * - bHeaderLength value must be at least 2 bytes (see above)
417 * - bHeaderLength value can't be larger than the packet size. 945 * - bHeaderLength value can't be larger than the packet size.
418 */ 946 */
419 if (len < 2 || data[0] < 2 || data[0] > len) 947 if (len < 2 || data[0] < 2 || data[0] > len) {
948 stream->stats.frame.nb_invalid++;
420 return -EINVAL; 949 return -EINVAL;
421
422 /* Skip payloads marked with the error bit ("error frames"). */
423 if (data[1] & UVC_STREAM_ERR) {
424 uvc_trace(UVC_TRACE_FRAME, "Dropping payload (error bit "
425 "set).\n");
426 return -ENODATA;
427 } 950 }
428 951
429 fid = data[1] & UVC_STREAM_FID; 952 fid = data[1] & UVC_STREAM_FID;
@@ -431,8 +954,14 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
431 /* Increase the sequence number regardless of any buffer states, so 954 /* Increase the sequence number regardless of any buffer states, so
432 * that discontinuous sequence numbers always indicate lost frames. 955 * that discontinuous sequence numbers always indicate lost frames.
433 */ 956 */
434 if (stream->last_fid != fid) 957 if (stream->last_fid != fid) {
435 stream->sequence++; 958 stream->sequence++;
959 if (stream->sequence)
960 uvc_video_stats_update(stream);
961 }
962
963 uvc_video_clock_decode(stream, buf, data, len);
964 uvc_video_stats_decode(stream, data, len);
436 965
437 /* Store the payload FID bit and return immediately when the buffer is 966 /* Store the payload FID bit and return immediately when the buffer is
438 * NULL. 967 * NULL.
@@ -442,6 +971,13 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
442 return -ENODATA; 971 return -ENODATA;
443 } 972 }
444 973
974 /* Mark the buffer as bad if the error bit is set. */
975 if (data[1] & UVC_STREAM_ERR) {
976 uvc_trace(UVC_TRACE_FRAME, "Marking buffer as bad (error bit "
977 "set).\n");
978 buf->error = 1;
979 }
980
445 /* Synchronize to the input stream by waiting for the FID bit to be 981 /* Synchronize to the input stream by waiting for the FID bit to be
446 * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE. 982 * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE.
447 * stream->last_fid is initialized to -1, so the first isochronous 983 * stream->last_fid is initialized to -1, so the first isochronous
@@ -467,9 +1003,10 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
467 else 1003 else
468 ktime_get_real_ts(&ts); 1004 ktime_get_real_ts(&ts);
469 1005
470 buf->buf.sequence = stream->sequence; 1006 buf->buf.v4l2_buf.sequence = stream->sequence;
471 buf->buf.timestamp.tv_sec = ts.tv_sec; 1007 buf->buf.v4l2_buf.timestamp.tv_sec = ts.tv_sec;
472 buf->buf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC; 1008 buf->buf.v4l2_buf.timestamp.tv_usec =
1009 ts.tv_nsec / NSEC_PER_USEC;
473 1010
474 /* TODO: Handle PTS and SCR. */ 1011 /* TODO: Handle PTS and SCR. */
475 buf->state = UVC_BUF_STATE_ACTIVE; 1012 buf->state = UVC_BUF_STATE_ACTIVE;
@@ -490,7 +1027,7 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
490 * avoids detecting end of frame conditions at FID toggling if the 1027 * avoids detecting end of frame conditions at FID toggling if the
491 * previous payload had the EOF bit set. 1028 * previous payload had the EOF bit set.
492 */ 1029 */
493 if (fid != stream->last_fid && buf->buf.bytesused != 0) { 1030 if (fid != stream->last_fid && buf->bytesused != 0) {
494 uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit " 1031 uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit "
495 "toggled).\n"); 1032 "toggled).\n");
496 buf->state = UVC_BUF_STATE_READY; 1033 buf->state = UVC_BUF_STATE_READY;
@@ -505,7 +1042,6 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
505static void uvc_video_decode_data(struct uvc_streaming *stream, 1042static void uvc_video_decode_data(struct uvc_streaming *stream,
506 struct uvc_buffer *buf, const __u8 *data, int len) 1043 struct uvc_buffer *buf, const __u8 *data, int len)
507{ 1044{
508 struct uvc_video_queue *queue = &stream->queue;
509 unsigned int maxlen, nbytes; 1045 unsigned int maxlen, nbytes;
510 void *mem; 1046 void *mem;
511 1047
@@ -513,11 +1049,11 @@ static void uvc_video_decode_data(struct uvc_streaming *stream,
513 return; 1049 return;
514 1050
515 /* Copy the video data to the buffer. */ 1051 /* Copy the video data to the buffer. */
516 maxlen = buf->buf.length - buf->buf.bytesused; 1052 maxlen = buf->length - buf->bytesused;
517 mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused; 1053 mem = buf->mem + buf->bytesused;
518 nbytes = min((unsigned int)len, maxlen); 1054 nbytes = min((unsigned int)len, maxlen);
519 memcpy(mem, data, nbytes); 1055 memcpy(mem, data, nbytes);
520 buf->buf.bytesused += nbytes; 1056 buf->bytesused += nbytes;
521 1057
522 /* Complete the current frame if the buffer size was exceeded. */ 1058 /* Complete the current frame if the buffer size was exceeded. */
523 if (len > maxlen) { 1059 if (len > maxlen) {
@@ -530,7 +1066,7 @@ static void uvc_video_decode_end(struct uvc_streaming *stream,
530 struct uvc_buffer *buf, const __u8 *data, int len) 1066 struct uvc_buffer *buf, const __u8 *data, int len)
531{ 1067{
532 /* Mark the buffer as done if the EOF marker is set. */ 1068 /* Mark the buffer as done if the EOF marker is set. */
533 if (data[1] & UVC_STREAM_EOF && buf->buf.bytesused != 0) { 1069 if (data[1] & UVC_STREAM_EOF && buf->bytesused != 0) {
534 uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n"); 1070 uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n");
535 if (data[0] == len) 1071 if (data[0] == len)
536 uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n"); 1072 uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n");
@@ -568,8 +1104,8 @@ static int uvc_video_encode_data(struct uvc_streaming *stream,
568 void *mem; 1104 void *mem;
569 1105
570 /* Copy video data to the URB buffer. */ 1106 /* Copy video data to the URB buffer. */
571 mem = queue->mem + buf->buf.m.offset + queue->buf_used; 1107 mem = buf->mem + queue->buf_used;
572 nbytes = min((unsigned int)len, buf->buf.bytesused - queue->buf_used); 1108 nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used);
573 nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size, 1109 nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size,
574 nbytes); 1110 nbytes);
575 memcpy(data, mem, nbytes); 1111 memcpy(data, mem, nbytes);
@@ -624,7 +1160,7 @@ static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
624 urb->iso_frame_desc[i].actual_length); 1160 urb->iso_frame_desc[i].actual_length);
625 1161
626 if (buf->state == UVC_BUF_STATE_READY) { 1162 if (buf->state == UVC_BUF_STATE_READY) {
627 if (buf->buf.length != buf->buf.bytesused && 1163 if (buf->length != buf->bytesused &&
628 !(stream->cur_format->flags & 1164 !(stream->cur_format->flags &
629 UVC_FMT_FLAG_COMPRESSED)) 1165 UVC_FMT_FLAG_COMPRESSED))
630 buf->error = 1; 1166 buf->error = 1;
@@ -724,12 +1260,12 @@ static void uvc_video_encode_bulk(struct urb *urb, struct uvc_streaming *stream,
724 stream->bulk.payload_size += ret; 1260 stream->bulk.payload_size += ret;
725 len -= ret; 1261 len -= ret;
726 1262
727 if (buf->buf.bytesused == stream->queue.buf_used || 1263 if (buf->bytesused == stream->queue.buf_used ||
728 stream->bulk.payload_size == stream->bulk.max_payload_size) { 1264 stream->bulk.payload_size == stream->bulk.max_payload_size) {
729 if (buf->buf.bytesused == stream->queue.buf_used) { 1265 if (buf->bytesused == stream->queue.buf_used) {
730 stream->queue.buf_used = 0; 1266 stream->queue.buf_used = 0;
731 buf->state = UVC_BUF_STATE_READY; 1267 buf->state = UVC_BUF_STATE_READY;
732 buf->buf.sequence = ++stream->sequence; 1268 buf->buf.v4l2_buf.sequence = ++stream->sequence;
733 uvc_queue_next_buffer(&stream->queue, buf); 1269 uvc_queue_next_buffer(&stream->queue, buf);
734 stream->last_fid ^= UVC_STREAM_FID; 1270 stream->last_fid ^= UVC_STREAM_FID;
735 } 1271 }
@@ -870,6 +1406,8 @@ static void uvc_uninit_video(struct uvc_streaming *stream, int free_buffers)
870 struct urb *urb; 1406 struct urb *urb;
871 unsigned int i; 1407 unsigned int i;
872 1408
1409 uvc_video_stats_stop(stream);
1410
873 for (i = 0; i < UVC_URBS; ++i) { 1411 for (i = 0; i < UVC_URBS; ++i) {
874 urb = stream->urb[i]; 1412 urb = stream->urb[i];
875 if (urb == NULL) 1413 if (urb == NULL)
@@ -882,6 +1420,8 @@ static void uvc_uninit_video(struct uvc_streaming *stream, int free_buffers)
882 1420
883 if (free_buffers) 1421 if (free_buffers)
884 uvc_free_urb_buffers(stream); 1422 uvc_free_urb_buffers(stream);
1423
1424 uvc_video_clock_cleanup(stream);
885} 1425}
886 1426
887/* 1427/*
@@ -1009,6 +1549,12 @@ static int uvc_init_video(struct uvc_streaming *stream, gfp_t gfp_flags)
1009 stream->bulk.skip_payload = 0; 1549 stream->bulk.skip_payload = 0;
1010 stream->bulk.payload_size = 0; 1550 stream->bulk.payload_size = 0;
1011 1551
1552 uvc_video_stats_start(stream);
1553
1554 ret = uvc_video_clock_init(stream);
1555 if (ret < 0)
1556 return ret;
1557
1012 if (intf->num_altsetting > 1) { 1558 if (intf->num_altsetting > 1) {
1013 struct usb_host_endpoint *best_ep = NULL; 1559 struct usb_host_endpoint *best_ep = NULL;
1014 unsigned int best_psize = 3 * 1024; 1560 unsigned int best_psize = 3 * 1024;
@@ -1283,6 +1829,11 @@ int uvc_video_enable(struct uvc_streaming *stream, int enable)
1283 return ret; 1829 return ret;
1284 } 1830 }
1285 1831
1286 return uvc_init_video(stream, GFP_KERNEL); 1832 ret = uvc_init_video(stream, GFP_KERNEL);
1287} 1833 if (ret < 0) {
1834 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1835 uvc_queue_enable(&stream->queue, 0);
1836 }
1288 1837
1838 return ret;
1839}
diff --git a/drivers/media/video/uvc/uvcvideo.h b/drivers/media/video/uvc/uvcvideo.h
index 4c1392ebcd4b..67f88d85bb16 100644
--- a/drivers/media/video/uvc/uvcvideo.h
+++ b/drivers/media/video/uvc/uvcvideo.h
@@ -13,6 +13,7 @@
13#include <linux/videodev2.h> 13#include <linux/videodev2.h>
14#include <media/media-device.h> 14#include <media/media-device.h>
15#include <media/v4l2-device.h> 15#include <media/v4l2-device.h>
16#include <media/videobuf2-core.h>
16 17
17/* -------------------------------------------------------------------------- 18/* --------------------------------------------------------------------------
18 * UVC constants 19 * UVC constants
@@ -113,6 +114,7 @@
113 114
114/* Maximum allowed number of control mappings per device */ 115/* Maximum allowed number of control mappings per device */
115#define UVC_MAX_CONTROL_MAPPINGS 1024 116#define UVC_MAX_CONTROL_MAPPINGS 1024
117#define UVC_MAX_CONTROL_MENU_ENTRIES 32
116 118
117/* Devices quirks */ 119/* Devices quirks */
118#define UVC_QUIRK_STATUS_INTERVAL 0x00000001 120#define UVC_QUIRK_STATUS_INTERVAL 0x00000001
@@ -319,35 +321,30 @@ enum uvc_buffer_state {
319}; 321};
320 322
321struct uvc_buffer { 323struct uvc_buffer {
322 unsigned long vma_use_count; 324 struct vb2_buffer buf;
323 struct list_head stream;
324
325 /* Touched by interrupt handler. */
326 struct v4l2_buffer buf;
327 struct list_head queue; 325 struct list_head queue;
328 wait_queue_head_t wait; 326
329 enum uvc_buffer_state state; 327 enum uvc_buffer_state state;
330 unsigned int error; 328 unsigned int error;
329
330 void *mem;
331 unsigned int length;
332 unsigned int bytesused;
333
334 u32 pts;
331}; 335};
332 336
333#define UVC_QUEUE_STREAMING (1 << 0) 337#define UVC_QUEUE_DISCONNECTED (1 << 0)
334#define UVC_QUEUE_DISCONNECTED (1 << 1) 338#define UVC_QUEUE_DROP_CORRUPTED (1 << 1)
335#define UVC_QUEUE_DROP_CORRUPTED (1 << 2)
336 339
337struct uvc_video_queue { 340struct uvc_video_queue {
338 enum v4l2_buf_type type; 341 struct vb2_queue queue;
342 struct mutex mutex; /* Protects queue */
339 343
340 void *mem;
341 unsigned int flags; 344 unsigned int flags;
342
343 unsigned int count;
344 unsigned int buf_size;
345 unsigned int buf_used; 345 unsigned int buf_used;
346 struct uvc_buffer buffer[UVC_MAX_VIDEO_BUFFERS];
347 struct mutex mutex; /* protects buffers and mainqueue */
348 spinlock_t irqlock; /* protects irqqueue */
349 346
350 struct list_head mainqueue; 347 spinlock_t irqlock; /* Protects irqqueue */
351 struct list_head irqqueue; 348 struct list_head irqqueue;
352}; 349};
353 350
@@ -362,6 +359,51 @@ struct uvc_video_chain {
362 struct mutex ctrl_mutex; /* Protects ctrl.info */ 359 struct mutex ctrl_mutex; /* Protects ctrl.info */
363}; 360};
364 361
362struct uvc_stats_frame {
363 unsigned int size; /* Number of bytes captured */
364 unsigned int first_data; /* Index of the first non-empty packet */
365
366 unsigned int nb_packets; /* Number of packets */
367 unsigned int nb_empty; /* Number of empty packets */
368 unsigned int nb_invalid; /* Number of packets with an invalid header */
369 unsigned int nb_errors; /* Number of packets with the error bit set */
370
371 unsigned int nb_pts; /* Number of packets with a PTS timestamp */
372 unsigned int nb_pts_diffs; /* Number of PTS differences inside a frame */
373 unsigned int last_pts_diff; /* Index of the last PTS difference */
374 bool has_initial_pts; /* Whether the first non-empty packet has a PTS */
375 bool has_early_pts; /* Whether a PTS is present before the first non-empty packet */
376 u32 pts; /* PTS of the last packet */
377
378 unsigned int nb_scr; /* Number of packets with a SCR timestamp */
379 unsigned int nb_scr_diffs; /* Number of SCR.STC differences inside a frame */
380 u16 scr_sof; /* SCR.SOF of the last packet */
381 u32 scr_stc; /* SCR.STC of the last packet */
382};
383
384struct uvc_stats_stream {
385 struct timespec start_ts; /* Stream start timestamp */
386 struct timespec stop_ts; /* Stream stop timestamp */
387
388 unsigned int nb_frames; /* Number of frames */
389
390 unsigned int nb_packets; /* Number of packets */
391 unsigned int nb_empty; /* Number of empty packets */
392 unsigned int nb_invalid; /* Number of packets with an invalid header */
393 unsigned int nb_errors; /* Number of packets with the error bit set */
394
395 unsigned int nb_pts_constant; /* Number of frames with constant PTS */
396 unsigned int nb_pts_early; /* Number of frames with early PTS */
397 unsigned int nb_pts_initial; /* Number of frames with initial PTS */
398
399 unsigned int nb_scr_count_ok; /* Number of frames with at least one SCR per non empty packet */
400 unsigned int nb_scr_diffs_ok; /* Number of frames with varying SCR.STC */
401 unsigned int scr_sof_count; /* STC.SOF counter accumulated since stream start */
402 unsigned int scr_sof; /* STC.SOF of the last packet */
403 unsigned int min_sof; /* Minimum STC.SOF value */
404 unsigned int max_sof; /* Maximum STC.SOF value */
405};
406
365struct uvc_streaming { 407struct uvc_streaming {
366 struct list_head list; 408 struct list_head list;
367 struct uvc_device *dev; 409 struct uvc_device *dev;
@@ -387,6 +429,7 @@ struct uvc_streaming {
387 */ 429 */
388 struct mutex mutex; 430 struct mutex mutex;
389 431
432 /* Buffers queue. */
390 unsigned int frozen : 1; 433 unsigned int frozen : 1;
391 struct uvc_video_queue queue; 434 struct uvc_video_queue queue;
392 void (*decode) (struct urb *urb, struct uvc_streaming *video, 435 void (*decode) (struct urb *urb, struct uvc_streaming *video,
@@ -408,6 +451,32 @@ struct uvc_streaming {
408 451
409 __u32 sequence; 452 __u32 sequence;
410 __u8 last_fid; 453 __u8 last_fid;
454
455 /* debugfs */
456 struct dentry *debugfs_dir;
457 struct {
458 struct uvc_stats_frame frame;
459 struct uvc_stats_stream stream;
460 } stats;
461
462 /* Timestamps support. */
463 struct uvc_clock {
464 struct uvc_clock_sample {
465 u32 dev_stc;
466 u16 dev_sof;
467 struct timespec host_ts;
468 u16 host_sof;
469 } *samples;
470
471 unsigned int head;
472 unsigned int count;
473 unsigned int size;
474
475 u16 last_sof;
476 u16 sof_offset;
477
478 spinlock_t lock;
479 } clock;
411}; 480};
412 481
413enum uvc_device_state { 482enum uvc_device_state {
@@ -479,9 +548,12 @@ struct uvc_driver {
479#define UVC_TRACE_SUSPEND (1 << 8) 548#define UVC_TRACE_SUSPEND (1 << 8)
480#define UVC_TRACE_STATUS (1 << 9) 549#define UVC_TRACE_STATUS (1 << 9)
481#define UVC_TRACE_VIDEO (1 << 10) 550#define UVC_TRACE_VIDEO (1 << 10)
551#define UVC_TRACE_STATS (1 << 11)
552#define UVC_TRACE_CLOCK (1 << 12)
482 553
483#define UVC_WARN_MINMAX 0 554#define UVC_WARN_MINMAX 0
484#define UVC_WARN_PROBE_DEF 1 555#define UVC_WARN_PROBE_DEF 1
556#define UVC_WARN_XU_GET_RES 2
485 557
486extern unsigned int uvc_clock_param; 558extern unsigned int uvc_clock_param;
487extern unsigned int uvc_no_drop_param; 559extern unsigned int uvc_no_drop_param;
@@ -516,8 +588,8 @@ extern struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id);
516extern void uvc_queue_init(struct uvc_video_queue *queue, 588extern void uvc_queue_init(struct uvc_video_queue *queue,
517 enum v4l2_buf_type type, int drop_corrupted); 589 enum v4l2_buf_type type, int drop_corrupted);
518extern int uvc_alloc_buffers(struct uvc_video_queue *queue, 590extern int uvc_alloc_buffers(struct uvc_video_queue *queue,
519 unsigned int nbuffers, unsigned int buflength); 591 struct v4l2_requestbuffers *rb);
520extern int uvc_free_buffers(struct uvc_video_queue *queue); 592extern void uvc_free_buffers(struct uvc_video_queue *queue);
521extern int uvc_query_buffer(struct uvc_video_queue *queue, 593extern int uvc_query_buffer(struct uvc_video_queue *queue,
522 struct v4l2_buffer *v4l2_buf); 594 struct v4l2_buffer *v4l2_buf);
523extern int uvc_queue_buffer(struct uvc_video_queue *queue, 595extern int uvc_queue_buffer(struct uvc_video_queue *queue,
@@ -539,7 +611,7 @@ extern unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
539extern int uvc_queue_allocated(struct uvc_video_queue *queue); 611extern int uvc_queue_allocated(struct uvc_video_queue *queue);
540static inline int uvc_queue_streaming(struct uvc_video_queue *queue) 612static inline int uvc_queue_streaming(struct uvc_video_queue *queue)
541{ 613{
542 return queue->flags & UVC_QUEUE_STREAMING; 614 return vb2_is_streaming(&queue->queue);
543} 615}
544 616
545/* V4L2 interface */ 617/* V4L2 interface */
@@ -556,10 +628,11 @@ extern int uvc_video_resume(struct uvc_streaming *stream, int reset);
556extern int uvc_video_enable(struct uvc_streaming *stream, int enable); 628extern int uvc_video_enable(struct uvc_streaming *stream, int enable);
557extern int uvc_probe_video(struct uvc_streaming *stream, 629extern int uvc_probe_video(struct uvc_streaming *stream,
558 struct uvc_streaming_control *probe); 630 struct uvc_streaming_control *probe);
559extern int uvc_commit_video(struct uvc_streaming *stream,
560 struct uvc_streaming_control *ctrl);
561extern int uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit, 631extern int uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
562 __u8 intfnum, __u8 cs, void *data, __u16 size); 632 __u8 intfnum, __u8 cs, void *data, __u16 size);
633void uvc_video_clock_update(struct uvc_streaming *stream,
634 struct v4l2_buffer *v4l2_buf,
635 struct uvc_buffer *buf);
563 636
564/* Status */ 637/* Status */
565extern int uvc_status_init(struct uvc_device *dev); 638extern int uvc_status_init(struct uvc_device *dev);
@@ -612,4 +685,13 @@ extern struct usb_host_endpoint *uvc_find_endpoint(
612void uvc_video_decode_isight(struct urb *urb, struct uvc_streaming *stream, 685void uvc_video_decode_isight(struct urb *urb, struct uvc_streaming *stream,
613 struct uvc_buffer *buf); 686 struct uvc_buffer *buf);
614 687
688/* debugfs and statistics */
689int uvc_debugfs_init(void);
690void uvc_debugfs_cleanup(void);
691int uvc_debugfs_init_stream(struct uvc_streaming *stream);
692void uvc_debugfs_cleanup_stream(struct uvc_streaming *stream);
693
694size_t uvc_video_stats_dump(struct uvc_streaming *stream, char *buf,
695 size_t size);
696
615#endif 697#endif