aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget/function/uvc_queue.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/gadget/function/uvc_queue.c')
-rw-r--r--drivers/usb/gadget/function/uvc_queue.c407
1 files changed, 407 insertions, 0 deletions
diff --git a/drivers/usb/gadget/function/uvc_queue.c b/drivers/usb/gadget/function/uvc_queue.c
new file mode 100644
index 000000000000..1c29bc954db9
--- /dev/null
+++ b/drivers/usb/gadget/function/uvc_queue.c
@@ -0,0 +1,407 @@
1/*
2 * uvc_queue.c -- USB Video Class driver - Buffers management
3 *
4 * Copyright (C) 2005-2010
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#include <linux/atomic.h>
14#include <linux/kernel.h>
15#include <linux/mm.h>
16#include <linux/list.h>
17#include <linux/module.h>
18#include <linux/usb.h>
19#include <linux/videodev2.h>
20#include <linux/vmalloc.h>
21#include <linux/wait.h>
22
23#include <media/v4l2-common.h>
24#include <media/videobuf2-vmalloc.h>
25
26#include "uvc.h"
27
28/* ------------------------------------------------------------------------
29 * Video buffers queue management.
30 *
31 * Video queues is initialized by uvc_queue_init(). The function performs
32 * basic initialization of the uvc_video_queue struct and never fails.
33 *
34 * Video buffers are managed by videobuf2. The driver uses a mutex to protect
35 * the videobuf2 queue operations by serializing calls to videobuf2 and a
36 * spinlock to protect the IRQ queue that holds the buffers to be processed by
37 * the driver.
38 */
39
40/* -----------------------------------------------------------------------------
41 * videobuf2 queue operations
42 */
43
44static int uvc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
45 unsigned int *nbuffers, unsigned int *nplanes,
46 unsigned int sizes[], void *alloc_ctxs[])
47{
48 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
49 struct uvc_video *video = container_of(queue, struct uvc_video, queue);
50
51 if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
52 *nbuffers = UVC_MAX_VIDEO_BUFFERS;
53
54 *nplanes = 1;
55
56 sizes[0] = video->imagesize;
57
58 return 0;
59}
60
61static int uvc_buffer_prepare(struct vb2_buffer *vb)
62{
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);
65
66 if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
67 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
68 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
69 return -EINVAL;
70 }
71
72 if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
73 return -ENODEV;
74
75 buf->state = UVC_BUF_STATE_QUEUED;
76 buf->mem = vb2_plane_vaddr(vb, 0);
77 buf->length = vb2_plane_size(vb, 0);
78 if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
79 buf->bytesused = 0;
80 else
81 buf->bytesused = vb2_get_plane_payload(vb, 0);
82
83 return 0;
84}
85
86static void uvc_buffer_queue(struct vb2_buffer *vb)
87{
88 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
89 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
90 unsigned long flags;
91
92 spin_lock_irqsave(&queue->irqlock, flags);
93
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 }
103
104 spin_unlock_irqrestore(&queue->irqlock, flags);
105}
106
107static void uvc_wait_prepare(struct vb2_queue *vq)
108{
109 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
110
111 mutex_unlock(&queue->mutex);
112}
113
114static void uvc_wait_finish(struct vb2_queue *vq)
115{
116 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
117
118 mutex_lock(&queue->mutex);
119}
120
121static struct vb2_ops uvc_queue_qops = {
122 .queue_setup = uvc_queue_setup,
123 .buf_prepare = uvc_buffer_prepare,
124 .buf_queue = uvc_buffer_queue,
125 .wait_prepare = uvc_wait_prepare,
126 .wait_finish = uvc_wait_finish,
127};
128
129static int uvc_queue_init(struct uvc_video_queue *queue,
130 enum v4l2_buf_type type)
131{
132 int ret;
133
134 queue->queue.type = type;
135 queue->queue.io_modes = VB2_MMAP | VB2_USERPTR;
136 queue->queue.drv_priv = queue;
137 queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
138 queue->queue.ops = &uvc_queue_qops;
139 queue->queue.mem_ops = &vb2_vmalloc_memops;
140 queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
141 | V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
142 ret = vb2_queue_init(&queue->queue);
143 if (ret)
144 return ret;
145
146 mutex_init(&queue->mutex);
147 spin_lock_init(&queue->irqlock);
148 INIT_LIST_HEAD(&queue->irqqueue);
149 queue->flags = 0;
150
151 return 0;
152}
153
154/*
155 * Free the video buffers.
156 */
157static void uvc_free_buffers(struct uvc_video_queue *queue)
158{
159 mutex_lock(&queue->mutex);
160 vb2_queue_release(&queue->queue);
161 mutex_unlock(&queue->mutex);
162}
163
164/*
165 * Allocate the video buffers.
166 */
167static int uvc_alloc_buffers(struct uvc_video_queue *queue,
168 struct v4l2_requestbuffers *rb)
169{
170 int ret;
171
172 mutex_lock(&queue->mutex);
173 ret = vb2_reqbufs(&queue->queue, rb);
174 mutex_unlock(&queue->mutex);
175
176 return ret ? ret : rb->count;
177}
178
179static int uvc_query_buffer(struct uvc_video_queue *queue,
180 struct v4l2_buffer *buf)
181{
182 int ret;
183
184 mutex_lock(&queue->mutex);
185 ret = vb2_querybuf(&queue->queue, buf);
186 mutex_unlock(&queue->mutex);
187
188 return ret;
189}
190
191static int uvc_queue_buffer(struct uvc_video_queue *queue,
192 struct v4l2_buffer *buf)
193{
194 unsigned long flags;
195 int ret;
196
197 mutex_lock(&queue->mutex);
198 ret = vb2_qbuf(&queue->queue, buf);
199 if (ret < 0)
200 goto done;
201
202 spin_lock_irqsave(&queue->irqlock, flags);
203 ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
204 queue->flags &= ~UVC_QUEUE_PAUSED;
205 spin_unlock_irqrestore(&queue->irqlock, flags);