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 void uvc_wait_prepare(struct vb2_queue *vq)
107{
108 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
109
110 mutex_unlock(&queue->mutex);
111}
112
113static void uvc_wait_finish(struct vb2_queue *vq)
114{
115 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
116
117 mutex_lock(&queue->mutex);
118}
119
120static struct vb2_ops uvc_queue_qops = {
121 .queue_setup = uvc_queue_setup,
122 .buf_prepare = uvc_buffer_prepare,
123 .buf_queue = uvc_buffer_queue,
124 .wait_prepare = uvc_wait_prepare,
125 .wait_finish = uvc_wait_finish,
126};
127
128static int uvc_queue_init(struct uvc_video_queue *queue,
129 enum v4l2_buf_type type)
130{
131 int ret;
132
133 queue->queue.type = type;
134 queue->queue.io_modes = VB2_MMAP | VB2_USERPTR;
135 queue->queue.drv_priv = queue;
136 queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
137 queue->queue.ops = &uvc_queue_qops;
138 queue->queue.mem_ops = &vb2_vmalloc_memops;
139 ret = vb2_queue_init(&queue->queue);
140 if (ret)
141 return ret;
142
143 mutex_init(&queue->mutex);
144 spin_lock_init(&queue->irqlock);
145 INIT_LIST_HEAD(&queue->irqqueue);
146 queue->flags = 0;
147
148 return 0;
149}
150
151
152
153
154static void uvc_free_buffers(struct uvc_video_queue *queue)
155{
156 mutex_lock(&queue->mutex);
157 vb2_queue_release(&queue->queue);
158 mutex_unlock(&queue->mutex);
159}
160
161
162
163
164static int uvc_alloc_buffers(struct uvc_video_queue *queue,
165 struct v4l2_requestbuffers *rb)
166{
167 int ret;
168
169 mutex_lock(&queue->mutex);
170 ret = vb2_reqbufs(&queue->queue, rb);
171 mutex_unlock(&queue->mutex);
172
173 return ret ? ret : rb->count;
174}
175
176static int uvc_query_buffer(struct uvc_video_queue *queue,
177 struct v4l2_buffer *buf)
178{
179 int ret;
180
181 mutex_lock(&queue->mutex);
182 ret = vb2_querybuf(&queue->queue, buf);
183 mutex_unlock(&queue->mutex);
184
185 return ret;
186}
187
188static int uvc_queue_buffer(struct uvc_video_queue *queue,
189 struct v4l2_buffer *buf)
190{
191 unsigned long flags;
192 int ret;
193
194 mutex_lock(&queue->mutex);
195 ret = vb2_qbuf(&queue->queue, buf);
196 if (ret < 0)
197 goto done;
198
199 spin_lock_irqsave(&queue->irqlock, flags);
200 ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
201 queue->flags &= ~UVC_QUEUE_PAUSED;
202 spin_unlock_irqrestore(&queue->irqlock, flags);
203
204done:
205 mutex_unlock(&queue->mutex);
206 return ret;
207}
208
209
210
211
212
213static int uvc_dequeue_buffer(struct uvc_video_queue *queue,
214 struct v4l2_buffer *buf, int nonblocking)
215{
216 int ret;
217
218 mutex_lock(&queue->mutex);
219 ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
220 mutex_unlock(&queue->mutex);
221
222 return ret;
223}
224
225
226
227
228
229
230
231static unsigned int uvc_queue_poll(struct uvc_video_queue *queue,
232 struct file *file, poll_table *wait)
233{
234 unsigned int ret;
235
236 mutex_lock(&queue->mutex);
237 ret = vb2_poll(&queue->queue, file, wait);
238 mutex_unlock(&queue->mutex);
239
240 return ret;
241}
242
243static int uvc_queue_mmap(struct uvc_video_queue *queue,
244 struct vm_area_struct *vma)
245{
246 int ret;
247
248 mutex_lock(&queue->mutex);
249 ret = vb2_mmap(&queue->queue, vma);
250 mutex_unlock(&queue->mutex);
251
252 return ret;
253}
254
255#ifndef CONFIG_MMU
256
257
258
259
260
261static unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
262 unsigned long pgoff)
263{
264 unsigned long ret;
265
266 mutex_lock(&queue->mutex);
267 ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
268 mutex_unlock(&queue->mutex);
269 return ret;
270}
271#endif
272
273
274
275
276
277
278
279
280
281
282
283
284
285static void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
286{
287 struct uvc_buffer *buf;
288 unsigned long flags;
289
290 spin_lock_irqsave(&queue->irqlock, flags);
291 while (!list_empty(&queue->irqqueue)) {
292 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
293 queue);
294 list_del(&buf->queue);
295 buf->state = UVC_BUF_STATE_ERROR;
296 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
297 }
298
299
300
301
302
303
304 if (disconnect)
305 queue->flags |= UVC_QUEUE_DISCONNECTED;
306 spin_unlock_irqrestore(&queue->irqlock, flags);
307}
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326static int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
327{
328 unsigned long flags;
329 int ret = 0;
330
331 mutex_lock(&queue->mutex);
332 if (enable) {
333 ret = vb2_streamon(&queue->queue, queue->queue.type);
334 if (ret < 0)
335 goto done;
336
337 queue->sequence = 0;
338 queue->buf_used = 0;
339 } else {
340 ret = vb2_streamoff(&queue->queue, queue->queue.type);
341 if (ret < 0)
342 goto done;
343
344 spin_lock_irqsave(&queue->irqlock, flags);
345 INIT_LIST_HEAD(&queue->irqqueue);
346
347
348
349
350
351
352
353 queue->flags &= ~UVC_QUEUE_DISCONNECTED;
354 spin_unlock_irqrestore(&queue->irqlock, flags);
355 }
356
357done:
358 mutex_unlock(&queue->mutex);
359 return ret;
360}
361
362
363static struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
364 struct uvc_buffer *buf)
365{
366 struct uvc_buffer *nextbuf;
367
368 if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
369 buf->length != buf->bytesused) {
370 buf->state = UVC_BUF_STATE_QUEUED;
371 vb2_set_plane_payload(&buf->buf, 0, 0);
372 return buf;
373 }
374
375 list_del(&buf->queue);
376 if (!list_empty(&queue->irqqueue))
377 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
378 queue);
379 else
380 nextbuf = NULL;
381
382
383
384
385
386
387
388 buf->buf.v4l2_buf.sequence = queue->sequence++;
389 do_gettimeofday(&buf->buf.v4l2_buf.timestamp);
390
391 vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
392 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
393
394 return nextbuf;
395}
396
397static struct uvc_buffer *uvc_queue_head(struct uvc_video_queue *queue)
398{
399 struct uvc_buffer *buf = NULL;
400
401 if (!list_empty(&queue->irqqueue))
402 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
403 queue);
404 else
405 queue->flags |= UVC_QUEUE_PAUSED;
406
407 return buf;
408}
409
410