1
2
3
4
5
6
7
8
9
10
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/videobuf2-vmalloc.h>
24
25#include "uvc.h"
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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[])
46{
47 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
48 struct uvc_video *video = container_of(queue, struct uvc_video, queue);
49
50 if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
51 *nbuffers = UVC_MAX_VIDEO_BUFFERS;
52
53 *nplanes = 1;
54
55 sizes[0] = video->imagesize;
56
57 return 0;
58}
59
60static int uvc_buffer_prepare(struct vb2_buffer *vb)
61{
62 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
63 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
64
65 if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
66 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
67 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
68 return -EINVAL;
69 }
70
71 if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
72 return -ENODEV;
73
74 buf->state = UVC_BUF_STATE_QUEUED;
75 buf->mem = vb2_plane_vaddr(vb, 0);
76 buf->length = vb2_plane_size(vb, 0);
77 if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
78 buf->bytesused = 0;
79 else
80 buf->bytesused = vb2_get_plane_payload(vb, 0);
81
82 return 0;
83}
84
85static void uvc_buffer_queue(struct vb2_buffer *vb)
86{
87 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
88 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
89 unsigned long flags;
90
91 spin_lock_irqsave(&queue->irqlock, flags);
92
93 if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
94 list_add_tail(&buf->queue, &queue->irqqueue);
95 } else {
96
97
98
99 buf->state = UVC_BUF_STATE_ERROR;
100 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
101 }
102
103 spin_unlock_irqrestore(&queue->irqlock, flags);
104}
105
106static struct vb2_ops uvc_queue_qops = {
107 .queue_setup = uvc_queue_setup,
108 .buf_prepare = uvc_buffer_prepare,
109 .buf_queue = uvc_buffer_queue,
110};
111
112static int uvc_queue_init(struct uvc_video_queue *queue,
113 enum v4l2_buf_type type)
114{
115 int ret;
116
117 queue->queue.type = type;
118 queue->queue.io_modes = VB2_MMAP | VB2_USERPTR;
119 queue->queue.drv_priv = queue;
120 queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
121 queue->queue.ops = &uvc_queue_qops;
122 queue->queue.mem_ops = &vb2_vmalloc_memops;
123 ret = vb2_queue_init(&queue->queue);
124 if (ret)
125 return ret;
126
127 mutex_init(&queue->mutex);
128 spin_lock_init(&queue->irqlock);
129 INIT_LIST_HEAD(&queue->irqqueue);
130 queue->flags = 0;
131
132 return 0;
133}
134
135
136
137
138static void uvc_free_buffers(struct uvc_video_queue *queue)
139{
140 mutex_lock(&queue->mutex);
141 vb2_queue_release(&queue->queue);
142 mutex_unlock(&queue->mutex);
143}
144
145
146
147
148static int uvc_alloc_buffers(struct uvc_video_queue *queue,
149 struct v4l2_requestbuffers *rb)
150{
151 int ret;
152
153 mutex_lock(&queue->mutex);
154 ret = vb2_reqbufs(&queue->queue, rb);
155 mutex_unlock(&queue->mutex);
156
157 return ret ? ret : rb->count;
158}
159
160static int uvc_query_buffer(struct uvc_video_queue *queue,
161 struct v4l2_buffer *buf)
162{
163 int ret;
164
165 mutex_lock(&queue->mutex);
166 ret = vb2_querybuf(&queue->queue, buf);
167 mutex_unlock(&queue->mutex);
168
169 return ret;
170}
171
172static int uvc_queue_buffer(struct uvc_video_queue *queue,
173 struct v4l2_buffer *buf)
174{
175 unsigned long flags;
176 int ret;
177
178 mutex_lock(&queue->mutex);
179 ret = vb2_qbuf(&queue->queue, buf);
180 if (ret < 0)
181 goto done;
182
183 spin_lock_irqsave(&queue->irqlock, flags);
184 ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
185 queue->flags &= ~UVC_QUEUE_PAUSED;
186 spin_unlock_irqrestore(&queue->irqlock, flags);
187
188done:
189 mutex_unlock(&queue->mutex);
190 return ret;
191}
192
193
194
195
196
197static int uvc_dequeue_buffer(struct uvc_video_queue *queue,
198 struct v4l2_buffer *buf, int nonblocking)
199{
200 int ret;
201
202 mutex_lock(&queue->mutex);
203 ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
204 mutex_unlock(&queue->mutex);
205
206 return ret;
207}
208
209
210
211
212
213
214
215static unsigned int uvc_queue_poll(struct uvc_video_queue *queue,
216 struct file *file, poll_table *wait)
217{
218 unsigned int ret;
219
220 mutex_lock(&queue->mutex);
221 ret = vb2_poll(&queue->queue, file, wait);
222 mutex_unlock(&queue->mutex);
223
224 return ret;
225}
226
227static int uvc_queue_mmap(struct uvc_video_queue *queue,
228 struct vm_area_struct *vma)
229{
230 int ret;
231
232 mutex_lock(&queue->mutex);
233 ret = vb2_mmap(&queue->queue, vma);
234 mutex_unlock(&queue->mutex);
235
236 return ret;
237}
238
239#ifndef CONFIG_MMU
240
241
242
243
244
245static unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
246 unsigned long pgoff)
247{
248 unsigned long ret;
249
250 mutex_lock(&queue->mutex);
251 ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
252 mutex_unlock(&queue->mutex);
253 return ret;
254}
255#endif
256
257
258
259
260
261
262
263
264
265
266
267
268
269static void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
270{
271 struct uvc_buffer *buf;
272 unsigned long flags;
273
274 spin_lock_irqsave(&queue->irqlock, flags);
275 while (!list_empty(&queue->irqqueue)) {
276 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
277 queue);
278 list_del(&buf->queue);
279 buf->state = UVC_BUF_STATE_ERROR;
280 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
281 }
282
283
284
285
286
287
288 if (disconnect)
289 queue->flags |= UVC_QUEUE_DISCONNECTED;
290 spin_unlock_irqrestore(&queue->irqlock, flags);
291}
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310static int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
311{
312 unsigned long flags;
313 int ret = 0;
314
315 mutex_lock(&queue->mutex);
316 if (enable) {
317 ret = vb2_streamon(&queue->queue, queue->queue.type);
318 if (ret < 0)
319 goto done;
320
321 queue->sequence = 0;
322 queue->buf_used = 0;
323 } else {
324 ret = vb2_streamoff(&queue->queue, queue->queue.type);
325 if (ret < 0)
326 goto done;
327
328 spin_lock_irqsave(&queue->irqlock, flags);
329 INIT_LIST_HEAD(&queue->irqqueue);
330
331
332
333
334
335
336
337 queue->flags &= ~UVC_QUEUE_DISCONNECTED;
338 spin_unlock_irqrestore(&queue->irqlock, flags);
339 }
340
341done:
342 mutex_unlock(&queue->mutex);
343 return ret;
344}
345
346
347static struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
348 struct uvc_buffer *buf)
349{
350 struct uvc_buffer *nextbuf;
351
352 if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
353 buf->length != buf->bytesused) {
354 buf->state = UVC_BUF_STATE_QUEUED;
355 vb2_set_plane_payload(&buf->buf, 0, 0);
356 return buf;
357 }
358
359 list_del(&buf->queue);
360 if (!list_empty(&queue->irqqueue))
361 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
362 queue);
363 else
364 nextbuf = NULL;
365
366
367
368
369
370
371
372 buf->buf.v4l2_buf.sequence = queue->sequence++;
373 do_gettimeofday(&buf->buf.v4l2_buf.timestamp);
374
375 vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
376 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
377
378 return nextbuf;
379}
380
381static struct uvc_buffer *uvc_queue_head(struct uvc_video_queue *queue)
382{
383 struct uvc_buffer *buf = NULL;
384
385 if (!list_empty(&queue->irqqueue))
386 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
387 queue);
388 else
389 queue->flags |= UVC_QUEUE_PAUSED;
390
391 return buf;
392}
393
394