brcmsmac: rework of mac80211 .flush() callback operation
[pandora-kernel.git] / drivers / media / v4l2-core / videobuf2-core.c
1 /*
2  * videobuf2-core.c - V4L2 driver helper framework
3  *
4  * Copyright (C) 2010 Samsung Electronics
5  *
6  * Author: Pawel Osciak <pawel@osciak.com>
7  *         Marek Szyprowski <m.szyprowski@samsung.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation.
12  */
13
14 #include <linux/err.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mm.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/sched.h>
21
22 #include <media/v4l2-dev.h>
23 #include <media/v4l2-fh.h>
24 #include <media/v4l2-event.h>
25 #include <media/videobuf2-core.h>
26
27 static int debug;
28 module_param(debug, int, 0644);
29
30 #define dprintk(level, fmt, arg...)                                     \
31         do {                                                            \
32                 if (debug >= level)                                     \
33                         printk(KERN_DEBUG "vb2: " fmt, ## arg);         \
34         } while (0)
35
36 #define call_memop(q, op, args...)                                      \
37         (((q)->mem_ops->op) ?                                           \
38                 ((q)->mem_ops->op(args)) : 0)
39
40 #define call_qop(q, op, args...)                                        \
41         (((q)->ops->op) ? ((q)->ops->op(args)) : 0)
42
43 #define V4L2_BUFFER_STATE_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
44                                  V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
45                                  V4L2_BUF_FLAG_PREPARED)
46
47 /**
48  * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
49  */
50 static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
51 {
52         struct vb2_queue *q = vb->vb2_queue;
53         void *mem_priv;
54         int plane;
55
56         /* Allocate memory for all planes in this buffer */
57         for (plane = 0; plane < vb->num_planes; ++plane) {
58                 mem_priv = call_memop(q, alloc, q->alloc_ctx[plane],
59                                       q->plane_sizes[plane]);
60                 if (IS_ERR_OR_NULL(mem_priv))
61                         goto free;
62
63                 /* Associate allocator private data with this plane */
64                 vb->planes[plane].mem_priv = mem_priv;
65                 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
66         }
67
68         return 0;
69 free:
70         /* Free already allocated memory if one of the allocations failed */
71         for (; plane > 0; --plane) {
72                 call_memop(q, put, vb->planes[plane - 1].mem_priv);
73                 vb->planes[plane - 1].mem_priv = NULL;
74         }
75
76         return -ENOMEM;
77 }
78
79 /**
80  * __vb2_buf_mem_free() - free memory of the given buffer
81  */
82 static void __vb2_buf_mem_free(struct vb2_buffer *vb)
83 {
84         struct vb2_queue *q = vb->vb2_queue;
85         unsigned int plane;
86
87         for (plane = 0; plane < vb->num_planes; ++plane) {
88                 call_memop(q, put, vb->planes[plane].mem_priv);
89                 vb->planes[plane].mem_priv = NULL;
90                 dprintk(3, "Freed plane %d of buffer %d\n", plane,
91                         vb->v4l2_buf.index);
92         }
93 }
94
95 /**
96  * __vb2_buf_userptr_put() - release userspace memory associated with
97  * a USERPTR buffer
98  */
99 static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
100 {
101         struct vb2_queue *q = vb->vb2_queue;
102         unsigned int plane;
103
104         for (plane = 0; plane < vb->num_planes; ++plane) {
105                 if (vb->planes[plane].mem_priv)
106                         call_memop(q, put_userptr, vb->planes[plane].mem_priv);
107                 vb->planes[plane].mem_priv = NULL;
108         }
109 }
110
111 /**
112  * __vb2_plane_dmabuf_put() - release memory associated with
113  * a DMABUF shared plane
114  */
115 static void __vb2_plane_dmabuf_put(struct vb2_queue *q, struct vb2_plane *p)
116 {
117         if (!p->mem_priv)
118                 return;
119
120         if (p->dbuf_mapped)
121                 call_memop(q, unmap_dmabuf, p->mem_priv);
122
123         call_memop(q, detach_dmabuf, p->mem_priv);
124         dma_buf_put(p->dbuf);
125         memset(p, 0, sizeof(*p));
126 }
127
128 /**
129  * __vb2_buf_dmabuf_put() - release memory associated with
130  * a DMABUF shared buffer
131  */
132 static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
133 {
134         struct vb2_queue *q = vb->vb2_queue;
135         unsigned int plane;
136
137         for (plane = 0; plane < vb->num_planes; ++plane)
138                 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
139 }
140
141 /**
142  * __setup_offsets() - setup unique offsets ("cookies") for every plane in
143  * every buffer on the queue
144  */
145 static void __setup_offsets(struct vb2_queue *q, unsigned int n)
146 {
147         unsigned int buffer, plane;
148         struct vb2_buffer *vb;
149         unsigned long off;
150
151         if (q->num_buffers) {
152                 struct v4l2_plane *p;
153                 vb = q->bufs[q->num_buffers - 1];
154                 p = &vb->v4l2_planes[vb->num_planes - 1];
155                 off = PAGE_ALIGN(p->m.mem_offset + p->length);
156         } else {
157                 off = 0;
158         }
159
160         for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
161                 vb = q->bufs[buffer];
162                 if (!vb)
163                         continue;
164
165                 for (plane = 0; plane < vb->num_planes; ++plane) {
166                         vb->v4l2_planes[plane].length = q->plane_sizes[plane];
167                         vb->v4l2_planes[plane].m.mem_offset = off;
168
169                         dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
170                                         buffer, plane, off);
171
172                         off += vb->v4l2_planes[plane].length;
173                         off = PAGE_ALIGN(off);
174                 }
175         }
176 }
177
178 /**
179  * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
180  * video buffer memory for all buffers/planes on the queue and initializes the
181  * queue
182  *
183  * Returns the number of buffers successfully allocated.
184  */
185 static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
186                              unsigned int num_buffers, unsigned int num_planes)
187 {
188         unsigned int buffer;
189         struct vb2_buffer *vb;
190         int ret;
191
192         for (buffer = 0; buffer < num_buffers; ++buffer) {
193                 /* Allocate videobuf buffer structures */
194                 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
195                 if (!vb) {
196                         dprintk(1, "Memory alloc for buffer struct failed\n");
197                         break;
198                 }
199
200                 /* Length stores number of planes for multiplanar buffers */
201                 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
202                         vb->v4l2_buf.length = num_planes;
203
204                 vb->state = VB2_BUF_STATE_DEQUEUED;
205                 vb->vb2_queue = q;
206                 vb->num_planes = num_planes;
207                 vb->v4l2_buf.index = q->num_buffers + buffer;
208                 vb->v4l2_buf.type = q->type;
209                 vb->v4l2_buf.memory = memory;
210
211                 /* Allocate video buffer memory for the MMAP type */
212                 if (memory == V4L2_MEMORY_MMAP) {
213                         ret = __vb2_buf_mem_alloc(vb);
214                         if (ret) {
215                                 dprintk(1, "Failed allocating memory for "
216                                                 "buffer %d\n", buffer);
217                                 kfree(vb);
218                                 break;
219                         }
220                         /*
221                          * Call the driver-provided buffer initialization
222                          * callback, if given. An error in initialization
223                          * results in queue setup failure.
224                          */
225                         ret = call_qop(q, buf_init, vb);
226                         if (ret) {
227                                 dprintk(1, "Buffer %d %p initialization"
228                                         " failed\n", buffer, vb);
229                                 __vb2_buf_mem_free(vb);
230                                 kfree(vb);
231                                 break;
232                         }
233                 }
234
235                 q->bufs[q->num_buffers + buffer] = vb;
236         }
237
238         __setup_offsets(q, buffer);
239
240         dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
241                         buffer, num_planes);
242
243         return buffer;
244 }
245
246 /**
247  * __vb2_free_mem() - release all video buffer memory for a given queue
248  */
249 static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
250 {
251         unsigned int buffer;
252         struct vb2_buffer *vb;
253
254         for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
255              ++buffer) {
256                 vb = q->bufs[buffer];
257                 if (!vb)
258                         continue;
259
260                 /* Free MMAP buffers or release USERPTR buffers */
261                 if (q->memory == V4L2_MEMORY_MMAP)
262                         __vb2_buf_mem_free(vb);
263                 else if (q->memory == V4L2_MEMORY_DMABUF)
264                         __vb2_buf_dmabuf_put(vb);
265                 else
266                         __vb2_buf_userptr_put(vb);
267         }
268 }
269
270 /**
271  * __vb2_queue_free() - free buffers at the end of the queue - video memory and
272  * related information, if no buffers are left return the queue to an
273  * uninitialized state. Might be called even if the queue has already been freed.
274  */
275 static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
276 {
277         unsigned int buffer;
278
279         /* Call driver-provided cleanup function for each buffer, if provided */
280         if (q->ops->buf_cleanup) {
281                 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
282                      ++buffer) {
283                         if (NULL == q->bufs[buffer])
284                                 continue;
285                         q->ops->buf_cleanup(q->bufs[buffer]);
286                 }
287         }
288
289         /* Release video buffer memory */
290         __vb2_free_mem(q, buffers);
291
292         /* Free videobuf buffers */
293         for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
294              ++buffer) {
295                 kfree(q->bufs[buffer]);
296                 q->bufs[buffer] = NULL;
297         }
298
299         q->num_buffers -= buffers;
300         if (!q->num_buffers)
301                 q->memory = 0;
302         INIT_LIST_HEAD(&q->queued_list);
303 }
304
305 /**
306  * __verify_planes_array() - verify that the planes array passed in struct
307  * v4l2_buffer from userspace can be safely used
308  */
309 static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
310 {
311         if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
312                 return 0;
313
314         /* Is memory for copying plane information present? */
315         if (NULL == b->m.planes) {
316                 dprintk(1, "Multi-planar buffer passed but "
317                            "planes array not provided\n");
318                 return -EINVAL;
319         }
320
321         if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
322                 dprintk(1, "Incorrect planes array length, "
323                            "expected %d, got %d\n", vb->num_planes, b->length);
324                 return -EINVAL;
325         }
326
327         return 0;
328 }
329
330 /**
331  * __buffer_in_use() - return true if the buffer is in use and
332  * the queue cannot be freed (by the means of REQBUFS(0)) call
333  */
334 static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
335 {
336         unsigned int plane;
337         for (plane = 0; plane < vb->num_planes; ++plane) {
338                 void *mem_priv = vb->planes[plane].mem_priv;
339                 /*
340                  * If num_users() has not been provided, call_memop
341                  * will return 0, apparently nobody cares about this
342                  * case anyway. If num_users() returns more than 1,
343                  * we are not the only user of the plane's memory.
344                  */
345                 if (mem_priv && call_memop(q, num_users, mem_priv) > 1)
346                         return true;
347         }
348         return false;
349 }
350
351 /**
352  * __buffers_in_use() - return true if any buffers on the queue are in use and
353  * the queue cannot be freed (by the means of REQBUFS(0)) call
354  */
355 static bool __buffers_in_use(struct vb2_queue *q)
356 {
357         unsigned int buffer;
358         for (buffer = 0; buffer < q->num_buffers; ++buffer) {
359                 if (__buffer_in_use(q, q->bufs[buffer]))
360                         return true;
361         }
362         return false;
363 }
364
365 /**
366  * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
367  * returned to userspace
368  */
369 static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
370 {
371         struct vb2_queue *q = vb->vb2_queue;
372
373         /* Copy back data such as timestamp, flags, etc. */
374         memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
375         b->reserved2 = vb->v4l2_buf.reserved2;
376         b->reserved = vb->v4l2_buf.reserved;
377
378         if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
379                 /*
380                  * Fill in plane-related data if userspace provided an array
381                  * for it. The caller has already verified memory and size.
382                  */
383                 b->length = vb->num_planes;
384                 memcpy(b->m.planes, vb->v4l2_planes,
385                         b->length * sizeof(struct v4l2_plane));
386         } else {
387                 /*
388                  * We use length and offset in v4l2_planes array even for
389                  * single-planar buffers, but userspace does not.
390                  */
391                 b->length = vb->v4l2_planes[0].length;
392                 b->bytesused = vb->v4l2_planes[0].bytesused;
393                 if (q->memory == V4L2_MEMORY_MMAP)
394                         b->m.offset = vb->v4l2_planes[0].m.mem_offset;
395                 else if (q->memory == V4L2_MEMORY_USERPTR)
396                         b->m.userptr = vb->v4l2_planes[0].m.userptr;
397                 else if (q->memory == V4L2_MEMORY_DMABUF)
398                         b->m.fd = vb->v4l2_planes[0].m.fd;
399         }
400
401         /*
402          * Clear any buffer state related flags.
403          */
404         b->flags &= ~V4L2_BUFFER_STATE_FLAGS;
405
406         switch (vb->state) {
407         case VB2_BUF_STATE_QUEUED:
408         case VB2_BUF_STATE_ACTIVE:
409                 b->flags |= V4L2_BUF_FLAG_QUEUED;
410                 break;
411         case VB2_BUF_STATE_ERROR:
412                 b->flags |= V4L2_BUF_FLAG_ERROR;
413                 /* fall through */
414         case VB2_BUF_STATE_DONE:
415                 b->flags |= V4L2_BUF_FLAG_DONE;
416                 break;
417         case VB2_BUF_STATE_PREPARED:
418                 b->flags |= V4L2_BUF_FLAG_PREPARED;
419                 break;
420         case VB2_BUF_STATE_DEQUEUED:
421                 /* nothing */
422                 break;
423         }
424
425         if (__buffer_in_use(q, vb))
426                 b->flags |= V4L2_BUF_FLAG_MAPPED;
427 }
428
429 /**
430  * vb2_querybuf() - query video buffer information
431  * @q:          videobuf queue
432  * @b:          buffer struct passed from userspace to vidioc_querybuf handler
433  *              in driver
434  *
435  * Should be called from vidioc_querybuf ioctl handler in driver.
436  * This function will verify the passed v4l2_buffer structure and fill the
437  * relevant information for the userspace.
438  *
439  * The return values from this function are intended to be directly returned
440  * from vidioc_querybuf handler in driver.
441  */
442 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
443 {
444         struct vb2_buffer *vb;
445         int ret;
446
447         if (b->type != q->type) {
448                 dprintk(1, "querybuf: wrong buffer type\n");
449                 return -EINVAL;
450         }
451
452         if (b->index >= q->num_buffers) {
453                 dprintk(1, "querybuf: buffer index out of range\n");
454                 return -EINVAL;
455         }
456         vb = q->bufs[b->index];
457         ret = __verify_planes_array(vb, b);
458         if (!ret)
459                 __fill_v4l2_buffer(vb, b);
460         return ret;
461 }
462 EXPORT_SYMBOL(vb2_querybuf);
463
464 /**
465  * __verify_userptr_ops() - verify that all memory operations required for
466  * USERPTR queue type have been provided
467  */
468 static int __verify_userptr_ops(struct vb2_queue *q)
469 {
470         if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
471             !q->mem_ops->put_userptr)
472                 return -EINVAL;
473
474         return 0;
475 }
476
477 /**
478  * __verify_mmap_ops() - verify that all memory operations required for
479  * MMAP queue type have been provided
480  */
481 static int __verify_mmap_ops(struct vb2_queue *q)
482 {
483         if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
484             !q->mem_ops->put || !q->mem_ops->mmap)
485                 return -EINVAL;
486
487         return 0;
488 }
489
490 /**
491  * __verify_dmabuf_ops() - verify that all memory operations required for
492  * DMABUF queue type have been provided
493  */
494 static int __verify_dmabuf_ops(struct vb2_queue *q)
495 {
496         if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
497             !q->mem_ops->detach_dmabuf  || !q->mem_ops->map_dmabuf ||
498             !q->mem_ops->unmap_dmabuf)
499                 return -EINVAL;
500
501         return 0;
502 }
503
504 /**
505  * __verify_memory_type() - Check whether the memory type and buffer type
506  * passed to a buffer operation are compatible with the queue.
507  */
508 static int __verify_memory_type(struct vb2_queue *q,
509                 enum v4l2_memory memory, enum v4l2_buf_type type)
510 {
511         if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR &&
512             memory != V4L2_MEMORY_DMABUF) {
513                 dprintk(1, "reqbufs: unsupported memory type\n");
514                 return -EINVAL;
515         }
516
517         if (type != q->type) {
518                 dprintk(1, "reqbufs: requested type is incorrect\n");
519                 return -EINVAL;
520         }
521
522         /*
523          * Make sure all the required memory ops for given memory type
524          * are available.
525          */
526         if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
527                 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
528                 return -EINVAL;
529         }
530
531         if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
532                 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
533                 return -EINVAL;
534         }
535
536         if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
537                 dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
538                 return -EINVAL;
539         }
540
541         /*
542          * Place the busy tests at the end: -EBUSY can be ignored when
543          * create_bufs is called with count == 0, but count == 0 should still
544          * do the memory and type validation.
545          */
546         if (q->fileio) {
547                 dprintk(1, "reqbufs: file io in progress\n");
548                 return -EBUSY;
549         }
550         return 0;
551 }
552
553 /**
554  * __reqbufs() - Initiate streaming
555  * @q:          videobuf2 queue
556  * @req:        struct passed from userspace to vidioc_reqbufs handler in driver
557  *
558  * Should be called from vidioc_reqbufs ioctl handler of a driver.
559  * This function:
560  * 1) verifies streaming parameters passed from the userspace,
561  * 2) sets up the queue,
562  * 3) negotiates number of buffers and planes per buffer with the driver
563  *    to be used during streaming,
564  * 4) allocates internal buffer structures (struct vb2_buffer), according to
565  *    the agreed parameters,
566  * 5) for MMAP memory type, allocates actual video memory, using the
567  *    memory handling/allocation routines provided during queue initialization
568  *
569  * If req->count is 0, all the memory will be freed instead.
570  * If the queue has been allocated previously (by a previous vb2_reqbufs) call
571  * and the queue is not busy, memory will be reallocated.
572  *
573  * The return values from this function are intended to be directly returned
574  * from vidioc_reqbufs handler in driver.
575  */
576 static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
577 {
578         unsigned int num_buffers, allocated_buffers, num_planes = 0;
579         int ret;
580
581         if (q->streaming) {
582                 dprintk(1, "reqbufs: streaming active\n");
583                 return -EBUSY;
584         }
585
586         if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
587                 /*
588                  * We already have buffers allocated, so first check if they
589                  * are not in use and can be freed.
590                  */
591                 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
592                         dprintk(1, "reqbufs: memory in use, cannot free\n");
593                         return -EBUSY;
594                 }
595
596                 __vb2_queue_free(q, q->num_buffers);
597
598                 /*
599                  * In case of REQBUFS(0) return immediately without calling
600                  * driver's queue_setup() callback and allocating resources.
601                  */
602                 if (req->count == 0)
603                         return 0;
604         }
605
606         /*
607          * Make sure the requested values and current defaults are sane.
608          */
609         num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
610         memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
611         memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
612         q->memory = req->memory;
613
614         /*
615          * Ask the driver how many buffers and planes per buffer it requires.
616          * Driver also sets the size and allocator context for each plane.
617          */
618         ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
619                        q->plane_sizes, q->alloc_ctx);
620         if (ret)
621                 return ret;
622
623         /* Finally, allocate buffers and video memory */
624         ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
625         if (ret == 0) {
626                 dprintk(1, "Memory allocation failed\n");
627                 return -ENOMEM;
628         }
629
630         allocated_buffers = ret;
631
632         /*
633          * Check if driver can handle the allocated number of buffers.
634          */
635         if (allocated_buffers < num_buffers) {
636                 num_buffers = allocated_buffers;
637
638                 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
639                                &num_planes, q->plane_sizes, q->alloc_ctx);
640
641                 if (!ret && allocated_buffers < num_buffers)
642                         ret = -ENOMEM;
643
644                 /*
645                  * Either the driver has accepted a smaller number of buffers,
646                  * or .queue_setup() returned an error
647                  */
648         }
649
650         q->num_buffers = allocated_buffers;
651
652         if (ret < 0) {
653                 __vb2_queue_free(q, allocated_buffers);
654                 return ret;
655         }
656
657         /*
658          * Return the number of successfully allocated buffers
659          * to the userspace.
660          */
661         req->count = allocated_buffers;
662
663         return 0;
664 }
665
666 /**
667  * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
668  * type values.
669  * @q:          videobuf2 queue
670  * @req:        struct passed from userspace to vidioc_reqbufs handler in driver
671  */
672 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
673 {
674         int ret = __verify_memory_type(q, req->memory, req->type);
675
676         return ret ? ret : __reqbufs(q, req);
677 }
678 EXPORT_SYMBOL_GPL(vb2_reqbufs);
679
680 /**
681  * __create_bufs() - Allocate buffers and any required auxiliary structs
682  * @q:          videobuf2 queue
683  * @create:     creation parameters, passed from userspace to vidioc_create_bufs
684  *              handler in driver
685  *
686  * Should be called from vidioc_create_bufs ioctl handler of a driver.
687  * This function:
688  * 1) verifies parameter sanity
689  * 2) calls the .queue_setup() queue operation
690  * 3) performs any necessary memory allocations
691  *
692  * The return values from this function are intended to be directly returned
693  * from vidioc_create_bufs handler in driver.
694  */
695 static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
696 {
697         unsigned int num_planes = 0, num_buffers, allocated_buffers;
698         int ret;
699
700         if (q->num_buffers == VIDEO_MAX_FRAME) {
701                 dprintk(1, "%s(): maximum number of buffers already allocated\n",
702                         __func__);
703                 return -ENOBUFS;
704         }
705
706         if (!q->num_buffers) {
707                 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
708                 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
709                 q->memory = create->memory;
710         }
711
712         num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
713
714         /*
715          * Ask the driver, whether the requested number of buffers, planes per
716          * buffer and their sizes are acceptable
717          */
718         ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
719                        &num_planes, q->plane_sizes, q->alloc_ctx);
720         if (ret)
721                 return ret;
722
723         /* Finally, allocate buffers and video memory */
724         ret = __vb2_queue_alloc(q, create->memory, num_buffers,
725                                 num_planes);
726         if (ret == 0) {
727                 dprintk(1, "Memory allocation failed\n");
728                 return -ENOMEM;
729         }
730
731         allocated_buffers = ret;
732
733         /*
734          * Check if driver can handle the so far allocated number of buffers.
735          */
736         if (ret < num_buffers) {
737                 num_buffers = ret;
738
739                 /*
740                  * q->num_buffers contains the total number of buffers, that the
741                  * queue driver has set up
742                  */
743                 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
744                                &num_planes, q->plane_sizes, q->alloc_ctx);
745
746                 if (!ret && allocated_buffers < num_buffers)
747                         ret = -ENOMEM;
748
749                 /*
750                  * Either the driver has accepted a smaller number of buffers,
751                  * or .queue_setup() returned an error
752                  */
753         }
754
755         q->num_buffers += allocated_buffers;
756
757         if (ret < 0) {
758                 __vb2_queue_free(q, allocated_buffers);
759                 return -ENOMEM;
760         }
761
762         /*
763          * Return the number of successfully allocated buffers
764          * to the userspace.
765          */
766         create->count = allocated_buffers;
767
768         return 0;
769 }
770
771 /**
772  * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
773  * memory and type values.
774  * @q:          videobuf2 queue
775  * @create:     creation parameters, passed from userspace to vidioc_create_bufs
776  *              handler in driver
777  */
778 int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
779 {
780         int ret = __verify_memory_type(q, create->memory, create->format.type);
781
782         create->index = q->num_buffers;
783         if (create->count == 0)
784                 return ret != -EBUSY ? ret : 0;
785         return ret ? ret : __create_bufs(q, create);
786 }
787 EXPORT_SYMBOL_GPL(vb2_create_bufs);
788
789 /**
790  * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
791  * @vb:         vb2_buffer to which the plane in question belongs to
792  * @plane_no:   plane number for which the address is to be returned
793  *
794  * This function returns a kernel virtual address of a given plane if
795  * such a mapping exist, NULL otherwise.
796  */
797 void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
798 {
799         struct vb2_queue *q = vb->vb2_queue;
800
801         if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
802                 return NULL;
803
804         return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
805
806 }
807 EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
808
809 /**
810  * vb2_plane_cookie() - Return allocator specific cookie for the given plane
811  * @vb:         vb2_buffer to which the plane in question belongs to
812  * @plane_no:   plane number for which the cookie is to be returned
813  *
814  * This function returns an allocator specific cookie for a given plane if
815  * available, NULL otherwise. The allocator should provide some simple static
816  * inline function, which would convert this cookie to the allocator specific
817  * type that can be used directly by the driver to access the buffer. This can
818  * be for example physical address, pointer to scatter list or IOMMU mapping.
819  */
820 void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
821 {
822         struct vb2_queue *q = vb->vb2_queue;
823
824         if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
825                 return NULL;
826
827         return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
828 }
829 EXPORT_SYMBOL_GPL(vb2_plane_cookie);
830
831 /**
832  * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
833  * @vb:         vb2_buffer returned from the driver
834  * @state:      either VB2_BUF_STATE_DONE if the operation finished successfully
835  *              or VB2_BUF_STATE_ERROR if the operation finished with an error
836  *
837  * This function should be called by the driver after a hardware operation on
838  * a buffer is finished and the buffer may be returned to userspace. The driver
839  * cannot use this buffer anymore until it is queued back to it by videobuf
840  * by the means of buf_queue callback. Only buffers previously queued to the
841  * driver by buf_queue can be passed to this function.
842  */
843 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
844 {
845         struct vb2_queue *q = vb->vb2_queue;
846         unsigned long flags;
847         unsigned int plane;
848
849         if (vb->state != VB2_BUF_STATE_ACTIVE)
850                 return;
851
852         if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
853                 return;
854
855         dprintk(4, "Done processing on buffer %d, state: %d\n",
856                         vb->v4l2_buf.index, vb->state);
857
858         /* sync buffers */
859         for (plane = 0; plane < vb->num_planes; ++plane)
860                 call_memop(q, finish, vb->planes[plane].mem_priv);
861
862         /* Add the buffer to the done buffers list */
863         spin_lock_irqsave(&q->done_lock, flags);
864         vb->state = state;
865         list_add_tail(&vb->done_entry, &q->done_list);
866         atomic_dec(&q->queued_count);
867         spin_unlock_irqrestore(&q->done_lock, flags);
868
869         /* Inform any processes that may be waiting for buffers */
870         wake_up(&q->done_wq);
871 }
872 EXPORT_SYMBOL_GPL(vb2_buffer_done);
873
874 /**
875  * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
876  * v4l2_buffer by the userspace. The caller has already verified that struct
877  * v4l2_buffer has a valid number of planes.
878  */
879 static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
880                                 struct v4l2_plane *v4l2_planes)
881 {
882         unsigned int plane;
883
884         if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
885                 /* Fill in driver-provided information for OUTPUT types */
886                 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
887                         /*
888                          * Will have to go up to b->length when API starts
889                          * accepting variable number of planes.
890                          */
891                         for (plane = 0; plane < vb->num_planes; ++plane) {
892                                 v4l2_planes[plane].bytesused =
893                                         b->m.planes[plane].bytesused;
894                                 v4l2_planes[plane].data_offset =
895                                         b->m.planes[plane].data_offset;
896                         }
897                 }
898
899                 if (b->memory == V4L2_MEMORY_USERPTR) {
900                         for (plane = 0; plane < vb->num_planes; ++plane) {
901                                 v4l2_planes[plane].m.userptr =
902                                         b->m.planes[plane].m.userptr;
903                                 v4l2_planes[plane].length =
904                                         b->m.planes[plane].length;
905                         }
906                 }
907                 if (b->memory == V4L2_MEMORY_DMABUF) {
908                         for (plane = 0; plane < vb->num_planes; ++plane) {
909                                 v4l2_planes[plane].m.fd =
910                                         b->m.planes[plane].m.fd;
911                                 v4l2_planes[plane].length =
912                                         b->m.planes[plane].length;
913                                 v4l2_planes[plane].data_offset =
914                                         b->m.planes[plane].data_offset;
915                         }
916                 }
917         } else {
918                 /*
919                  * Single-planar buffers do not use planes array,
920                  * so fill in relevant v4l2_buffer struct fields instead.
921                  * In videobuf we use our internal V4l2_planes struct for
922                  * single-planar buffers as well, for simplicity.
923                  */
924                 if (V4L2_TYPE_IS_OUTPUT(b->type))
925                         v4l2_planes[0].bytesused = b->bytesused;
926
927                 if (b->memory == V4L2_MEMORY_USERPTR) {
928                         v4l2_planes[0].m.userptr = b->m.userptr;
929                         v4l2_planes[0].length = b->length;
930                 }
931
932                 if (b->memory == V4L2_MEMORY_DMABUF) {
933                         v4l2_planes[0].m.fd = b->m.fd;
934                         v4l2_planes[0].length = b->length;
935                         v4l2_planes[0].data_offset = 0;
936                 }
937
938         }
939
940         vb->v4l2_buf.field = b->field;
941         vb->v4l2_buf.timestamp = b->timestamp;
942         vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS;
943 }
944
945 /**
946  * __qbuf_userptr() - handle qbuf of a USERPTR buffer
947  */
948 static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
949 {
950         struct v4l2_plane planes[VIDEO_MAX_PLANES];
951         struct vb2_queue *q = vb->vb2_queue;
952         void *mem_priv;
953         unsigned int plane;
954         int ret;
955         int write = !V4L2_TYPE_IS_OUTPUT(q->type);
956
957         /* Copy relevant information provided by the userspace */
958         __fill_vb2_buffer(vb, b, planes);
959
960         for (plane = 0; plane < vb->num_planes; ++plane) {
961                 /* Skip the plane if already verified */
962                 if (vb->v4l2_planes[plane].m.userptr &&
963                     vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
964                     && vb->v4l2_planes[plane].length == planes[plane].length)
965                         continue;
966
967                 dprintk(3, "qbuf: userspace address for plane %d changed, "
968                                 "reacquiring memory\n", plane);
969
970                 /* Check if the provided plane buffer is large enough */
971                 if (planes[plane].length < q->plane_sizes[plane]) {
972                         ret = -EINVAL;
973                         goto err;
974                 }
975
976                 /* Release previously acquired memory if present */
977                 if (vb->planes[plane].mem_priv)
978                         call_memop(q, put_userptr, vb->planes[plane].mem_priv);
979
980                 vb->planes[plane].mem_priv = NULL;
981                 vb->v4l2_planes[plane].m.userptr = 0;
982                 vb->v4l2_planes[plane].length = 0;
983
984                 /* Acquire each plane's memory */
985                 mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
986                                       planes[plane].m.userptr,
987                                       planes[plane].length, write);
988                 if (IS_ERR_OR_NULL(mem_priv)) {
989                         dprintk(1, "qbuf: failed acquiring userspace "
990                                                 "memory for plane %d\n", plane);
991                         ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
992                         goto err;
993                 }
994                 vb->planes[plane].mem_priv = mem_priv;
995         }
996
997         /*
998          * Call driver-specific initialization on the newly acquired buffer,
999          * if provided.
1000          */
1001         ret = call_qop(q, buf_init, vb);
1002         if (ret) {
1003                 dprintk(1, "qbuf: buffer initialization failed\n");
1004                 goto err;
1005         }
1006
1007         /*
1008          * Now that everything is in order, copy relevant information
1009          * provided by userspace.
1010          */
1011         for (plane = 0; plane < vb->num_planes; ++plane)
1012                 vb->v4l2_planes[plane] = planes[plane];
1013
1014         return 0;
1015 err:
1016         /* In case of errors, release planes that were already acquired */
1017         for (plane = 0; plane < vb->num_planes; ++plane) {
1018                 if (vb->planes[plane].mem_priv)
1019                         call_memop(q, put_userptr, vb->planes[plane].mem_priv);
1020                 vb->planes[plane].mem_priv = NULL;
1021                 vb->v4l2_planes[plane].m.userptr = 0;
1022                 vb->v4l2_planes[plane].length = 0;
1023         }
1024
1025         return ret;
1026 }
1027
1028 /**
1029  * __qbuf_mmap() - handle qbuf of an MMAP buffer
1030  */
1031 static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1032 {
1033         __fill_vb2_buffer(vb, b, vb->v4l2_planes);
1034         return 0;
1035 }
1036
1037 /**
1038  * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1039  */
1040 static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1041 {
1042         struct v4l2_plane planes[VIDEO_MAX_PLANES];
1043         struct vb2_queue *q = vb->vb2_queue;
1044         void *mem_priv;
1045         unsigned int plane;
1046         int ret;
1047         int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1048
1049         /* Verify and copy relevant information provided by the userspace */
1050         __fill_vb2_buffer(vb, b, planes);
1051
1052         for (plane = 0; plane < vb->num_planes; ++plane) {
1053                 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1054
1055                 if (IS_ERR_OR_NULL(dbuf)) {
1056                         dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
1057                                 plane);
1058                         ret = -EINVAL;
1059                         goto err;
1060                 }
1061
1062                 /* use DMABUF size if length is not provided */
1063                 if (planes[plane].length == 0)
1064                         planes[plane].length = dbuf->size;
1065
1066                 if (planes[plane].length < planes[plane].data_offset +
1067                     q->plane_sizes[plane]) {
1068                         ret = -EINVAL;
1069                         goto err;
1070                 }
1071
1072                 /* Skip the plane if already verified */
1073                 if (dbuf == vb->planes[plane].dbuf &&
1074                     vb->v4l2_planes[plane].length == planes[plane].length) {
1075                         dma_buf_put(dbuf);
1076                         continue;
1077                 }
1078
1079                 dprintk(1, "qbuf: buffer for plane %d changed\n", plane);
1080
1081                 /* Release previously acquired memory if present */
1082                 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
1083                 memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
1084
1085                 /* Acquire each plane's memory */
1086                 mem_priv = call_memop(q, attach_dmabuf, q->alloc_ctx[plane],
1087                         dbuf, planes[plane].length, write);
1088                 if (IS_ERR(mem_priv)) {
1089                         dprintk(1, "qbuf: failed to attach dmabuf\n");
1090                         ret = PTR_ERR(mem_priv);
1091                         dma_buf_put(dbuf);
1092                         goto err;
1093                 }
1094
1095                 vb->planes[plane].dbuf = dbuf;
1096                 vb->planes[plane].mem_priv = mem_priv;
1097         }
1098
1099         /* TODO: This pins the buffer(s) with  dma_buf_map_attachment()).. but
1100          * really we want to do this just before the DMA, not while queueing
1101          * the buffer(s)..
1102          */
1103         for (plane = 0; plane < vb->num_planes; ++plane) {
1104                 ret = call_memop(q, map_dmabuf, vb->planes[plane].mem_priv);
1105                 if (ret) {
1106                         dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
1107                                 plane);
1108                         goto err;
1109                 }
1110                 vb->planes[plane].dbuf_mapped = 1;
1111         }
1112
1113         /*
1114          * Call driver-specific initialization on the newly acquired buffer,
1115          * if provided.
1116          */
1117         ret = call_qop(q, buf_init, vb);
1118         if (ret) {
1119                 dprintk(1, "qbuf: buffer initialization failed\n");
1120                 goto err;
1121         }
1122
1123         /*
1124          * Now that everything is in order, copy relevant information
1125          * provided by userspace.
1126          */
1127         for (plane = 0; plane < vb->num_planes; ++plane)
1128                 vb->v4l2_planes[plane] = planes[plane];
1129
1130         return 0;
1131 err:
1132         /* In case of errors, release planes that were already acquired */
1133         __vb2_buf_dmabuf_put(vb);
1134
1135         return ret;
1136 }
1137
1138 /**
1139  * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1140  */
1141 static void __enqueue_in_driver(struct vb2_buffer *vb)
1142 {
1143         struct vb2_queue *q = vb->vb2_queue;
1144         unsigned int plane;
1145
1146         vb->state = VB2_BUF_STATE_ACTIVE;
1147         atomic_inc(&q->queued_count);
1148
1149         /* sync buffers */
1150         for (plane = 0; plane < vb->num_planes; ++plane)
1151                 call_memop(q, prepare, vb->planes[plane].mem_priv);
1152
1153         q->ops->buf_queue(vb);
1154 }
1155
1156 static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1157 {
1158         struct vb2_queue *q = vb->vb2_queue;
1159         int ret;
1160
1161         switch (q->memory) {
1162         case V4L2_MEMORY_MMAP:
1163                 ret = __qbuf_mmap(vb, b);
1164                 break;
1165         case V4L2_MEMORY_USERPTR:
1166                 ret = __qbuf_userptr(vb, b);
1167                 break;
1168         case V4L2_MEMORY_DMABUF:
1169                 ret = __qbuf_dmabuf(vb, b);
1170                 break;
1171         default:
1172                 WARN(1, "Invalid queue type\n");
1173                 ret = -EINVAL;
1174         }
1175
1176         if (!ret)
1177                 ret = call_qop(q, buf_prepare, vb);
1178         if (ret)
1179                 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
1180         else
1181                 vb->state = VB2_BUF_STATE_PREPARED;
1182
1183         return ret;
1184 }
1185
1186 /**
1187  * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1188  * @q:          videobuf2 queue
1189  * @b:          buffer structure passed from userspace to vidioc_prepare_buf
1190  *              handler in driver
1191  *
1192  * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1193  * This function:
1194  * 1) verifies the passed buffer,
1195  * 2) calls buf_prepare callback in the driver (if provided), in which
1196  *    driver-specific buffer initialization can be performed,
1197  *
1198  * The return values from this function are intended to be directly returned
1199  * from vidioc_prepare_buf handler in driver.
1200  */
1201 int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1202 {
1203         struct vb2_buffer *vb;
1204         int ret;
1205
1206         if (q->fileio) {
1207                 dprintk(1, "%s(): file io in progress\n", __func__);
1208                 return -EBUSY;
1209         }
1210
1211         if (b->type != q->type) {
1212                 dprintk(1, "%s(): invalid buffer type\n", __func__);
1213                 return -EINVAL;
1214         }
1215
1216         if (b->index >= q->num_buffers) {
1217                 dprintk(1, "%s(): buffer index out of range\n", __func__);
1218                 return -EINVAL;
1219         }
1220
1221         vb = q->bufs[b->index];
1222         if (NULL == vb) {
1223                 /* Should never happen */
1224                 dprintk(1, "%s(): buffer is NULL\n", __func__);
1225                 return -EINVAL;
1226         }
1227
1228         if (b->memory != q->memory) {
1229                 dprintk(1, "%s(): invalid memory type\n", __func__);
1230                 return -EINVAL;
1231         }
1232
1233         if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1234                 dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state);
1235                 return -EINVAL;
1236         }
1237         ret = __verify_planes_array(vb, b);
1238         if (ret < 0)
1239                 return ret;
1240         ret = __buf_prepare(vb, b);
1241         if (ret < 0)
1242                 return ret;
1243
1244         __fill_v4l2_buffer(vb, b);
1245
1246         return 0;
1247 }
1248 EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1249
1250 /**
1251  * vb2_qbuf() - Queue a buffer from userspace
1252  * @q:          videobuf2 queue
1253  * @b:          buffer structure passed from userspace to vidioc_qbuf handler
1254  *              in driver
1255  *
1256  * Should be called from vidioc_qbuf ioctl handler of a driver.
1257  * This function:
1258  * 1) verifies the passed buffer,
1259  * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1260  *    which driver-specific buffer initialization can be performed,
1261  * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1262  *    callback for processing.
1263  *
1264  * The return values from this function are intended to be directly returned
1265  * from vidioc_qbuf handler in driver.
1266  */
1267 int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1268 {
1269         struct rw_semaphore *mmap_sem = NULL;
1270         struct vb2_buffer *vb;
1271         int ret = 0;
1272
1273         /*
1274          * In case of user pointer buffers vb2 allocator needs to get direct
1275          * access to userspace pages. This requires getting read access on
1276          * mmap semaphore in the current process structure. The same
1277          * semaphore is taken before calling mmap operation, while both mmap
1278          * and qbuf are called by the driver or v4l2 core with driver's lock
1279          * held. To avoid a AB-BA deadlock (mmap_sem then driver's lock in
1280          * mmap and driver's lock then mmap_sem in qbuf) the videobuf2 core
1281          * release driver's lock, takes mmap_sem and then takes again driver's
1282          * lock.
1283          *
1284          * To avoid race with other vb2 calls, which might be called after
1285          * releasing driver's lock, this operation is performed at the
1286          * beggining of qbuf processing. This way the queue status is
1287          * consistent after getting driver's lock back.
1288          */
1289         if (q->memory == V4L2_MEMORY_USERPTR) {
1290                 mmap_sem = &current->mm->mmap_sem;
1291                 call_qop(q, wait_prepare, q);
1292                 down_read(mmap_sem);
1293                 call_qop(q, wait_finish, q);
1294         }
1295
1296         if (q->fileio) {
1297                 dprintk(1, "qbuf: file io in progress\n");
1298                 ret = -EBUSY;
1299                 goto unlock;
1300         }
1301
1302         if (b->type != q->type) {
1303                 dprintk(1, "qbuf: invalid buffer type\n");
1304                 ret = -EINVAL;
1305                 goto unlock;
1306         }
1307
1308         if (b->index >= q->num_buffers) {
1309                 dprintk(1, "qbuf: buffer index out of range\n");
1310                 ret = -EINVAL;
1311                 goto unlock;
1312         }
1313
1314         vb = q->bufs[b->index];
1315         if (NULL == vb) {
1316                 /* Should never happen */
1317                 dprintk(1, "qbuf: buffer is NULL\n");
1318                 ret = -EINVAL;
1319                 goto unlock;
1320         }
1321
1322         if (b->memory != q->memory) {
1323                 dprintk(1, "qbuf: invalid memory type\n");
1324                 ret = -EINVAL;
1325                 goto unlock;
1326         }
1327         ret = __verify_planes_array(vb, b);
1328         if (ret)
1329                 goto unlock;
1330
1331         switch (vb->state) {
1332         case VB2_BUF_STATE_DEQUEUED:
1333                 ret = __buf_prepare(vb, b);
1334                 if (ret)
1335                         goto unlock;
1336         case VB2_BUF_STATE_PREPARED:
1337                 break;
1338         default:
1339                 dprintk(1, "qbuf: buffer already in use\n");
1340                 ret = -EINVAL;
1341                 goto unlock;
1342         }
1343
1344         /*
1345          * Add to the queued buffers list, a buffer will stay on it until
1346          * dequeued in dqbuf.
1347          */
1348         list_add_tail(&vb->queued_entry, &q->queued_list);
1349         vb->state = VB2_BUF_STATE_QUEUED;
1350
1351         /*
1352          * If already streaming, give the buffer to driver for processing.
1353          * If not, the buffer will be given to driver on next streamon.
1354          */
1355         if (q->streaming)
1356                 __enqueue_in_driver(vb);
1357
1358         /* Fill buffer information for the userspace */
1359         __fill_v4l2_buffer(vb, b);
1360
1361         dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
1362 unlock:
1363         if (mmap_sem)
1364                 up_read(mmap_sem);
1365         return ret;
1366 }
1367 EXPORT_SYMBOL_GPL(vb2_qbuf);
1368
1369 /**
1370  * __vb2_wait_for_done_vb() - wait for a buffer to become available
1371  * for dequeuing
1372  *
1373  * Will sleep if required for nonblocking == false.
1374  */
1375 static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1376 {
1377         /*
1378          * All operations on vb_done_list are performed under done_lock
1379          * spinlock protection. However, buffers may be removed from
1380          * it and returned to userspace only while holding both driver's
1381          * lock and the done_lock spinlock. Thus we can be sure that as
1382          * long as we hold the driver's lock, the list will remain not
1383          * empty if list_empty() check succeeds.
1384          */
1385
1386         for (;;) {
1387                 int ret;
1388
1389                 if (!q->streaming) {
1390                         dprintk(1, "Streaming off, will not wait for buffers\n");
1391                         return -EINVAL;
1392                 }
1393
1394                 if (!list_empty(&q->done_list)) {
1395                         /*
1396                          * Found a buffer that we were waiting for.
1397                          */
1398                         break;
1399                 }
1400
1401                 if (nonblocking) {
1402                         dprintk(1, "Nonblocking and no buffers to dequeue, "
1403                                                                 "will not wait\n");
1404                         return -EAGAIN;
1405                 }
1406
1407                 /*
1408                  * We are streaming and blocking, wait for another buffer to
1409                  * become ready or for streamoff. Driver's lock is released to
1410                  * allow streamoff or qbuf to be called while waiting.
1411                  */
1412                 call_qop(q, wait_prepare, q);
1413
1414                 /*
1415                  * All locks have been released, it is safe to sleep now.
1416                  */
1417                 dprintk(3, "Will sleep waiting for buffers\n");
1418                 ret = wait_event_interruptible(q->done_wq,
1419                                 !list_empty(&q->done_list) || !q->streaming);
1420
1421                 /*
1422                  * We need to reevaluate both conditions again after reacquiring
1423                  * the locks or return an error if one occurred.
1424                  */
1425                 call_qop(q, wait_finish, q);
1426                 if (ret) {
1427                         dprintk(1, "Sleep was interrupted\n");
1428                         return ret;
1429                 }
1430         }
1431         return 0;
1432 }
1433
1434 /**
1435  * __vb2_get_done_vb() - get a buffer ready for dequeuing
1436  *
1437  * Will sleep if required for nonblocking == false.
1438  */
1439 static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1440                                 struct v4l2_buffer *b, int nonblocking)
1441 {
1442         unsigned long flags;
1443         int ret;
1444
1445         /*
1446          * Wait for at least one buffer to become available on the done_list.
1447          */
1448         ret = __vb2_wait_for_done_vb(q, nonblocking);
1449         if (ret)
1450                 return ret;
1451
1452         /*
1453          * Driver's lock has been held since we last verified that done_list
1454          * is not empty, so no need for another list_empty(done_list) check.
1455          */
1456         spin_lock_irqsave(&q->done_lock, flags);
1457         *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1458         /*
1459          * Only remove the buffer from done_list if v4l2_buffer can handle all
1460          * the planes.
1461          */
1462         ret = __verify_planes_array(*vb, b);
1463         if (!ret)
1464                 list_del(&(*vb)->done_entry);
1465         spin_unlock_irqrestore(&q->done_lock, flags);
1466
1467         return ret;
1468 }
1469
1470 /**
1471  * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1472  * @q:          videobuf2 queue
1473  *
1474  * This function will wait until all buffers that have been given to the driver
1475  * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1476  * wait_prepare, wait_finish pair. It is intended to be called with all locks
1477  * taken, for example from stop_streaming() callback.
1478  */
1479 int vb2_wait_for_all_buffers(struct vb2_queue *q)
1480 {
1481         if (!q->streaming) {
1482                 dprintk(1, "Streaming off, will not wait for buffers\n");
1483                 return -EINVAL;
1484         }
1485
1486         wait_event(q->done_wq, !atomic_read(&q->queued_count));
1487         return 0;
1488 }
1489 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1490
1491 /**
1492  * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1493  */
1494 static void __vb2_dqbuf(struct vb2_buffer *vb)
1495 {
1496         struct vb2_queue *q = vb->vb2_queue;
1497         unsigned int i;
1498
1499         /* nothing to do if the buffer is already dequeued */
1500         if (vb->state == VB2_BUF_STATE_DEQUEUED)
1501                 return;
1502
1503         vb->state = VB2_BUF_STATE_DEQUEUED;
1504
1505         /* unmap DMABUF buffer */
1506         if (q->memory == V4L2_MEMORY_DMABUF)
1507                 for (i = 0; i < vb->num_planes; ++i) {
1508                         if (!vb->planes[i].dbuf_mapped)
1509                                 continue;
1510                         call_memop(q, unmap_dmabuf, vb->planes[i].mem_priv);
1511                         vb->planes[i].dbuf_mapped = 0;
1512                 }
1513 }
1514
1515 /**
1516  * vb2_dqbuf() - Dequeue a buffer to the userspace
1517  * @q:          videobuf2 queue
1518  * @b:          buffer structure passed from userspace to vidioc_dqbuf handler
1519  *              in driver
1520  * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1521  *               buffers ready for dequeuing are present. Normally the driver
1522  *               would be passing (file->f_flags & O_NONBLOCK) here
1523  *
1524  * Should be called from vidioc_dqbuf ioctl handler of a driver.
1525  * This function:
1526  * 1) verifies the passed buffer,
1527  * 2) calls buf_finish callback in the driver (if provided), in which
1528  *    driver can perform any additional operations that may be required before
1529  *    returning the buffer to userspace, such as cache sync,
1530  * 3) the buffer struct members are filled with relevant information for
1531  *    the userspace.
1532  *
1533  * The return values from this function are intended to be directly returned
1534  * from vidioc_dqbuf handler in driver.
1535  */
1536 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1537 {
1538         struct vb2_buffer *vb = NULL;
1539         int ret;
1540
1541         if (q->fileio) {
1542                 dprintk(1, "dqbuf: file io in progress\n");
1543                 return -EBUSY;
1544         }
1545
1546         if (b->type != q->type) {
1547                 dprintk(1, "dqbuf: invalid buffer type\n");
1548                 return -EINVAL;
1549         }
1550         ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
1551         if (ret < 0)
1552                 return ret;
1553
1554         ret = call_qop(q, buf_finish, vb);
1555         if (ret) {
1556                 dprintk(1, "dqbuf: buffer finish failed\n");
1557                 return ret;
1558         }
1559
1560         switch (vb->state) {
1561         case VB2_BUF_STATE_DONE:
1562                 dprintk(3, "dqbuf: Returning done buffer\n");
1563                 break;
1564         case VB2_BUF_STATE_ERROR:
1565                 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1566                 break;
1567         default:
1568                 dprintk(1, "dqbuf: Invalid buffer state\n");
1569                 return -EINVAL;
1570         }
1571
1572         /* Fill buffer information for the userspace */
1573         __fill_v4l2_buffer(vb, b);
1574         /* Remove from videobuf queue */
1575         list_del(&vb->queued_entry);
1576         /* go back to dequeued state */
1577         __vb2_dqbuf(vb);
1578
1579         dprintk(1, "dqbuf of buffer %d, with state %d\n",
1580                         vb->v4l2_buf.index, vb->state);
1581
1582         return 0;
1583 }
1584 EXPORT_SYMBOL_GPL(vb2_dqbuf);
1585
1586 /**
1587  * __vb2_queue_cancel() - cancel and stop (pause) streaming
1588  *
1589  * Removes all queued buffers from driver's queue and all buffers queued by
1590  * userspace from videobuf's queue. Returns to state after reqbufs.
1591  */
1592 static void __vb2_queue_cancel(struct vb2_queue *q)
1593 {
1594         unsigned int i;
1595
1596         /*
1597          * Tell driver to stop all transactions and release all queued
1598          * buffers.
1599          */
1600         if (q->streaming)
1601                 call_qop(q, stop_streaming, q);
1602         q->streaming = 0;
1603
1604         /*
1605          * Remove all buffers from videobuf's list...
1606          */
1607         INIT_LIST_HEAD(&q->queued_list);
1608         /*
1609          * ...and done list; userspace will not receive any buffers it
1610          * has not already dequeued before initiating cancel.
1611          */
1612         INIT_LIST_HEAD(&q->done_list);
1613         atomic_set(&q->queued_count, 0);
1614         wake_up_all(&q->done_wq);
1615
1616         /*
1617          * Reinitialize all buffers for next use.
1618          */
1619         for (i = 0; i < q->num_buffers; ++i)
1620                 __vb2_dqbuf(q->bufs[i]);
1621 }
1622
1623 /**
1624  * vb2_streamon - start streaming
1625  * @q:          videobuf2 queue
1626  * @type:       type argument passed from userspace to vidioc_streamon handler
1627  *
1628  * Should be called from vidioc_streamon handler of a driver.
1629  * This function:
1630  * 1) verifies current state
1631  * 2) passes any previously queued buffers to the driver and starts streaming
1632  *
1633  * The return values from this function are intended to be directly returned
1634  * from vidioc_streamon handler in the driver.
1635  */
1636 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1637 {
1638         struct vb2_buffer *vb;
1639         int ret;
1640
1641         if (q->fileio) {
1642                 dprintk(1, "streamon: file io in progress\n");
1643                 return -EBUSY;
1644         }
1645
1646         if (type != q->type) {
1647                 dprintk(1, "streamon: invalid stream type\n");
1648                 return -EINVAL;
1649         }
1650
1651         if (q->streaming) {
1652                 dprintk(1, "streamon: already streaming\n");
1653                 return -EBUSY;
1654         }
1655
1656         /*
1657          * If any buffers were queued before streamon,
1658          * we can now pass them to driver for processing.
1659          */
1660         list_for_each_entry(vb, &q->queued_list, queued_entry)
1661                 __enqueue_in_driver(vb);
1662
1663         /*
1664          * Let driver notice that streaming state has been enabled.
1665          */
1666         ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1667         if (ret) {
1668                 dprintk(1, "streamon: driver refused to start streaming\n");
1669                 __vb2_queue_cancel(q);
1670                 return ret;
1671         }
1672
1673         q->streaming = 1;
1674
1675         dprintk(3, "Streamon successful\n");
1676         return 0;
1677 }
1678 EXPORT_SYMBOL_GPL(vb2_streamon);
1679
1680
1681 /**
1682  * vb2_streamoff - stop streaming
1683  * @q:          videobuf2 queue
1684  * @type:       type argument passed from userspace to vidioc_streamoff handler
1685  *
1686  * Should be called from vidioc_streamoff handler of a driver.
1687  * This function:
1688  * 1) verifies current state,
1689  * 2) stop streaming and dequeues any queued buffers, including those previously
1690  *    passed to the driver (after waiting for the driver to finish).
1691  *
1692  * This call can be used for pausing playback.
1693  * The return values from this function are intended to be directly returned
1694  * from vidioc_streamoff handler in the driver
1695  */
1696 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1697 {
1698         if (q->fileio) {
1699                 dprintk(1, "streamoff: file io in progress\n");
1700                 return -EBUSY;
1701         }
1702
1703         if (type != q->type) {
1704                 dprintk(1, "streamoff: invalid stream type\n");
1705                 return -EINVAL;
1706         }
1707
1708         if (!q->streaming) {
1709                 dprintk(1, "streamoff: not streaming\n");
1710                 return -EINVAL;
1711         }
1712
1713         /*
1714          * Cancel will pause streaming and remove all buffers from the driver
1715          * and videobuf, effectively returning control over them to userspace.
1716          */
1717         __vb2_queue_cancel(q);
1718
1719         dprintk(3, "Streamoff successful\n");
1720         return 0;
1721 }
1722 EXPORT_SYMBOL_GPL(vb2_streamoff);
1723
1724 /**
1725  * __find_plane_by_offset() - find plane associated with the given offset off
1726  */
1727 static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1728                         unsigned int *_buffer, unsigned int *_plane)
1729 {
1730         struct vb2_buffer *vb;
1731         unsigned int buffer, plane;
1732
1733         /*
1734          * Go over all buffers and their planes, comparing the given offset
1735          * with an offset assigned to each plane. If a match is found,
1736          * return its buffer and plane numbers.
1737          */
1738         for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1739                 vb = q->bufs[buffer];
1740
1741                 for (plane = 0; plane < vb->num_planes; ++plane) {
1742                         if (vb->v4l2_planes[plane].m.mem_offset == off) {
1743                                 *_buffer = buffer;
1744                                 *_plane = plane;
1745                                 return 0;
1746                         }
1747                 }
1748         }
1749
1750         return -EINVAL;
1751 }
1752
1753 /**
1754  * vb2_expbuf() - Export a buffer as a file descriptor
1755  * @q:          videobuf2 queue
1756  * @eb:         export buffer structure passed from userspace to vidioc_expbuf
1757  *              handler in driver
1758  *
1759  * The return values from this function are intended to be directly returned
1760  * from vidioc_expbuf handler in driver.
1761  */
1762 int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
1763 {
1764         struct vb2_buffer *vb = NULL;
1765         struct vb2_plane *vb_plane;
1766         int ret;
1767         struct dma_buf *dbuf;
1768
1769         if (q->memory != V4L2_MEMORY_MMAP) {
1770                 dprintk(1, "Queue is not currently set up for mmap\n");
1771                 return -EINVAL;
1772         }
1773
1774         if (!q->mem_ops->get_dmabuf) {
1775                 dprintk(1, "Queue does not support DMA buffer exporting\n");
1776                 return -EINVAL;
1777         }
1778
1779         if (eb->flags & ~O_CLOEXEC) {
1780                 dprintk(1, "Queue does support only O_CLOEXEC flag\n");
1781                 return -EINVAL;
1782         }
1783
1784         if (eb->type != q->type) {
1785                 dprintk(1, "qbuf: invalid buffer type\n");
1786                 return -EINVAL;
1787         }
1788
1789         if (eb->index >= q->num_buffers) {
1790                 dprintk(1, "buffer index out of range\n");
1791                 return -EINVAL;
1792         }
1793
1794         vb = q->bufs[eb->index];
1795
1796         if (eb->plane >= vb->num_planes) {
1797                 dprintk(1, "buffer plane out of range\n");
1798                 return -EINVAL;
1799         }
1800
1801         vb_plane = &vb->planes[eb->plane];
1802
1803         dbuf = call_memop(q, get_dmabuf, vb_plane->mem_priv);
1804         if (IS_ERR_OR_NULL(dbuf)) {
1805                 dprintk(1, "Failed to export buffer %d, plane %d\n",
1806                         eb->index, eb->plane);
1807                 return -EINVAL;
1808         }
1809
1810         ret = dma_buf_fd(dbuf, eb->flags);
1811         if (ret < 0) {
1812                 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
1813                         eb->index, eb->plane, ret);
1814                 dma_buf_put(dbuf);
1815                 return ret;
1816         }
1817
1818         dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
1819                 eb->index, eb->plane, ret);
1820         eb->fd = ret;
1821
1822         return 0;
1823 }
1824 EXPORT_SYMBOL_GPL(vb2_expbuf);
1825
1826 /**
1827  * vb2_mmap() - map video buffers into application address space
1828  * @q:          videobuf2 queue
1829  * @vma:        vma passed to the mmap file operation handler in the driver
1830  *
1831  * Should be called from mmap file operation handler of a driver.
1832  * This function maps one plane of one of the available video buffers to
1833  * userspace. To map whole video memory allocated on reqbufs, this function
1834  * has to be called once per each plane per each buffer previously allocated.
1835  *
1836  * When the userspace application calls mmap, it passes to it an offset returned
1837  * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1838  * a "cookie", which is then used to identify the plane to be mapped.
1839  * This function finds a plane with a matching offset and a mapping is performed
1840  * by the means of a provided memory operation.
1841  *
1842  * The return values from this function are intended to be directly returned
1843  * from the mmap handler in driver.
1844  */
1845 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1846 {
1847         unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
1848         struct vb2_buffer *vb;
1849         unsigned int buffer, plane;
1850         int ret;
1851
1852         if (q->memory != V4L2_MEMORY_MMAP) {
1853                 dprintk(1, "Queue is not currently set up for mmap\n");
1854                 return -EINVAL;
1855         }
1856
1857         /*
1858          * Check memory area access mode.
1859          */
1860         if (!(vma->vm_flags & VM_SHARED)) {
1861                 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1862                 return -EINVAL;
1863         }
1864         if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1865                 if (!(vma->vm_flags & VM_WRITE)) {
1866                         dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1867                         return -EINVAL;
1868                 }
1869         } else {
1870                 if (!(vma->vm_flags & VM_READ)) {
1871                         dprintk(1, "Invalid vma flags, VM_READ needed\n");
1872                         return -EINVAL;
1873                 }
1874         }
1875
1876         /*
1877          * Find the plane corresponding to the offset passed by userspace.
1878          */
1879         ret = __find_plane_by_offset(q, off, &buffer, &plane);
1880         if (ret)
1881                 return ret;
1882
1883         vb = q->bufs[buffer];
1884
1885         ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
1886         if (ret)
1887                 return ret;
1888
1889         dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1890         return 0;
1891 }
1892 EXPORT_SYMBOL_GPL(vb2_mmap);
1893
1894 #ifndef CONFIG_MMU
1895 unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1896                                     unsigned long addr,
1897                                     unsigned long len,
1898                                     unsigned long pgoff,
1899                                     unsigned long flags)
1900 {
1901         unsigned long off = pgoff << PAGE_SHIFT;
1902         struct vb2_buffer *vb;
1903         unsigned int buffer, plane;
1904         int ret;
1905
1906         if (q->memory != V4L2_MEMORY_MMAP) {
1907                 dprintk(1, "Queue is not currently set up for mmap\n");
1908                 return -EINVAL;
1909         }
1910
1911         /*
1912          * Find the plane corresponding to the offset passed by userspace.
1913          */
1914         ret = __find_plane_by_offset(q, off, &buffer, &plane);
1915         if (ret)
1916                 return ret;
1917
1918         vb = q->bufs[buffer];
1919
1920         return (unsigned long)vb2_plane_vaddr(vb, plane);
1921 }
1922 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
1923 #endif
1924
1925 static int __vb2_init_fileio(struct vb2_queue *q, int read);
1926 static int __vb2_cleanup_fileio(struct vb2_queue *q);
1927
1928 /**
1929  * vb2_poll() - implements poll userspace operation
1930  * @q:          videobuf2 queue
1931  * @file:       file argument passed to the poll file operation handler
1932  * @wait:       wait argument passed to the poll file operation handler
1933  *
1934  * This function implements poll file operation handler for a driver.
1935  * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1936  * be informed that the file descriptor of a video device is available for
1937  * reading.
1938  * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1939  * will be reported as available for writing.
1940  *
1941  * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
1942  * pending events.
1943  *
1944  * The return values from this function are intended to be directly returned
1945  * from poll handler in driver.
1946  */
1947 unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
1948 {
1949         struct video_device *vfd = video_devdata(file);
1950         unsigned long req_events = poll_requested_events(wait);
1951         struct vb2_buffer *vb = NULL;
1952         unsigned int res = 0;
1953         unsigned long flags;
1954
1955         if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
1956                 struct v4l2_fh *fh = file->private_data;
1957
1958                 if (v4l2_event_pending(fh))
1959                         res = POLLPRI;
1960                 else if (req_events & POLLPRI)
1961                         poll_wait(file, &fh->wait, wait);
1962         }
1963
1964         /*
1965          * Start file I/O emulator only if streaming API has not been used yet.
1966          */
1967         if (q->num_buffers == 0 && q->fileio == NULL) {
1968                 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
1969                                 (req_events & (POLLIN | POLLRDNORM))) {
1970                         if (__vb2_init_fileio(q, 1))
1971                                 return res | POLLERR;
1972                 }
1973                 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
1974                                 (req_events & (POLLOUT | POLLWRNORM))) {
1975                         if (__vb2_init_fileio(q, 0))
1976                                 return res | POLLERR;
1977                         /*
1978                          * Write to OUTPUT queue can be done immediately.
1979                          */
1980                         return res | POLLOUT | POLLWRNORM;
1981                 }
1982         }
1983
1984         /*
1985          * There is nothing to wait for if no buffers have already been queued.
1986          */
1987         if (list_empty(&q->queued_list))
1988                 return res | POLLERR;
1989
1990         poll_wait(file, &q->done_wq, wait);
1991
1992         /*
1993          * Take first buffer available for dequeuing.
1994          */
1995         spin_lock_irqsave(&q->done_lock, flags);
1996         if (!list_empty(&q->done_list))
1997                 vb = list_first_entry(&q->done_list, struct vb2_buffer,
1998                                         done_entry);
1999         spin_unlock_irqrestore(&q->done_lock, flags);
2000
2001         if (vb && (vb->state == VB2_BUF_STATE_DONE
2002                         || vb->state == VB2_BUF_STATE_ERROR)) {
2003                 return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
2004                                 res | POLLOUT | POLLWRNORM :
2005                                 res | POLLIN | POLLRDNORM;
2006         }
2007         return res;
2008 }
2009 EXPORT_SYMBOL_GPL(vb2_poll);
2010
2011 /**
2012  * vb2_queue_init() - initialize a videobuf2 queue
2013  * @q:          videobuf2 queue; this structure should be allocated in driver
2014  *
2015  * The vb2_queue structure should be allocated by the driver. The driver is
2016  * responsible of clearing it's content and setting initial values for some
2017  * required entries before calling this function.
2018  * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2019  * to the struct vb2_queue description in include/media/videobuf2-core.h
2020  * for more information.
2021  */
2022 int vb2_queue_init(struct vb2_queue *q)
2023 {
2024         /*
2025          * Sanity check
2026          */
2027         if (WARN_ON(!q)                   ||
2028             WARN_ON(!q->ops)              ||
2029             WARN_ON(!q->mem_ops)          ||
2030             WARN_ON(!q->type)             ||
2031             WARN_ON(!q->io_modes)         ||
2032             WARN_ON(!q->ops->queue_setup) ||
2033             WARN_ON(!q->ops->buf_queue))
2034                 return -EINVAL;
2035
2036         INIT_LIST_HEAD(&q->queued_list);
2037         INIT_LIST_HEAD(&q->done_list);
2038         spin_lock_init(&q->done_lock);
2039         init_waitqueue_head(&q->done_wq);
2040
2041         if (q->buf_struct_size == 0)
2042                 q->buf_struct_size = sizeof(struct vb2_buffer);
2043
2044         return 0;
2045 }
2046 EXPORT_SYMBOL_GPL(vb2_queue_init);
2047
2048 /**
2049  * vb2_queue_release() - stop streaming, release the queue and free memory
2050  * @q:          videobuf2 queue
2051  *
2052  * This function stops streaming and performs necessary clean ups, including
2053  * freeing video buffer memory. The driver is responsible for freeing
2054  * the vb2_queue structure itself.
2055  */
2056 void vb2_queue_release(struct vb2_queue *q)
2057 {
2058         __vb2_cleanup_fileio(q);
2059         __vb2_queue_cancel(q);
2060         __vb2_queue_free(q, q->num_buffers);
2061 }
2062 EXPORT_SYMBOL_GPL(vb2_queue_release);
2063
2064 /**
2065  * struct vb2_fileio_buf - buffer context used by file io emulator
2066  *
2067  * vb2 provides a compatibility layer and emulator of file io (read and
2068  * write) calls on top of streaming API. This structure is used for
2069  * tracking context related to the buffers.
2070  */
2071 struct vb2_fileio_buf {
2072         void *vaddr;
2073         unsigned int size;
2074         unsigned int pos;
2075         unsigned int queued:1;
2076 };
2077
2078 /**
2079  * struct vb2_fileio_data - queue context used by file io emulator
2080  *
2081  * vb2 provides a compatibility layer and emulator of file io (read and
2082  * write) calls on top of streaming API. For proper operation it required
2083  * this structure to save the driver state between each call of the read
2084  * or write function.
2085  */
2086 struct vb2_fileio_data {
2087         struct v4l2_requestbuffers req;
2088         struct v4l2_buffer b;
2089         struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
2090         unsigned int index;
2091         unsigned int q_count;
2092         unsigned int dq_count;
2093         unsigned int flags;
2094 };
2095
2096 /**
2097  * __vb2_init_fileio() - initialize file io emulator
2098  * @q:          videobuf2 queue
2099  * @read:       mode selector (1 means read, 0 means write)
2100  */
2101 static int __vb2_init_fileio(struct vb2_queue *q, int read)
2102 {
2103         struct vb2_fileio_data *fileio;
2104         int i, ret;
2105         unsigned int count = 0;
2106
2107         /*
2108          * Sanity check
2109          */
2110         if ((read && !(q->io_modes & VB2_READ)) ||
2111            (!read && !(q->io_modes & VB2_WRITE)))
2112                 BUG();
2113
2114         /*
2115          * Check if device supports mapping buffers to kernel virtual space.
2116          */
2117         if (!q->mem_ops->vaddr)
2118                 return -EBUSY;
2119
2120         /*
2121          * Check if streaming api has not been already activated.
2122          */
2123         if (q->streaming || q->num_buffers > 0)
2124                 return -EBUSY;
2125
2126         /*
2127          * Start with count 1, driver can increase it in queue_setup()
2128          */
2129         count = 1;
2130
2131         dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
2132                 (read) ? "read" : "write", count, q->io_flags);
2133
2134         fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
2135         if (fileio == NULL)
2136                 return -ENOMEM;
2137
2138         fileio->flags = q->io_flags;
2139
2140         /*
2141          * Request buffers and use MMAP type to force driver
2142          * to allocate buffers by itself.
2143          */
2144         fileio->req.count = count;
2145         fileio->req.memory = V4L2_MEMORY_MMAP;
2146         fileio->req.type = q->type;
2147         ret = vb2_reqbufs(q, &fileio->req);
2148         if (ret)
2149                 goto err_kfree;
2150
2151         /*
2152          * Check if plane_count is correct
2153          * (multiplane buffers are not supported).
2154          */
2155         if (q->bufs[0]->num_planes != 1) {
2156                 ret = -EBUSY;
2157                 goto err_reqbufs;
2158         }
2159
2160         /*
2161          * Get kernel address of each buffer.
2162          */
2163         for (i = 0; i < q->num_buffers; i++) {
2164                 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
2165                 if (fileio->bufs[i].vaddr == NULL)
2166                         goto err_reqbufs;
2167                 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
2168         }
2169
2170         /*
2171          * Read mode requires pre queuing of all buffers.
2172          */
2173         if (read) {
2174                 /*
2175                  * Queue all buffers.
2176                  */
2177                 for (i = 0; i < q->num_buffers; i++) {
2178                         struct v4l2_buffer *b = &fileio->b;
2179                         memset(b, 0, sizeof(*b));
2180                         b->type = q->type;
2181                         b->memory = q->memory;
2182                         b->index = i;
2183                         ret = vb2_qbuf(q, b);
2184                         if (ret)
2185                                 goto err_reqbufs;
2186                         fileio->bufs[i].queued = 1;
2187                 }
2188
2189                 /*
2190                  * Start streaming.
2191                  */
2192                 ret = vb2_streamon(q, q->type);
2193                 if (ret)
2194                         goto err_reqbufs;
2195         }
2196
2197         q->fileio = fileio;
2198
2199         return ret;
2200
2201 err_reqbufs:
2202         fileio->req.count = 0;
2203         vb2_reqbufs(q, &fileio->req);
2204
2205 err_kfree:
2206         kfree(fileio);
2207         return ret;
2208 }
2209
2210 /**
2211  * __vb2_cleanup_fileio() - free resourced used by file io emulator
2212  * @q:          videobuf2 queue
2213  */
2214 static int __vb2_cleanup_fileio(struct vb2_queue *q)
2215 {
2216         struct vb2_fileio_data *fileio = q->fileio;
2217
2218         if (fileio) {
2219                 /*
2220                  * Hack fileio context to enable direct calls to vb2 ioctl
2221                  * interface.
2222                  */
2223                 q->fileio = NULL;
2224
2225                 vb2_streamoff(q, q->type);
2226                 fileio->req.count = 0;
2227                 vb2_reqbufs(q, &fileio->req);
2228                 kfree(fileio);
2229                 dprintk(3, "file io emulator closed\n");
2230         }
2231         return 0;
2232 }
2233
2234 /**
2235  * __vb2_perform_fileio() - perform a single file io (read or write) operation
2236  * @q:          videobuf2 queue
2237  * @data:       pointed to target userspace buffer
2238  * @count:      number of bytes to read or write
2239  * @ppos:       file handle position tracking pointer
2240  * @nonblock:   mode selector (1 means blocking calls, 0 means nonblocking)
2241  * @read:       access mode selector (1 means read, 0 means write)
2242  */
2243 static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
2244                 loff_t *ppos, int nonblock, int read)
2245 {
2246         struct vb2_fileio_data *fileio;
2247         struct vb2_fileio_buf *buf;
2248         int ret, index;
2249
2250         dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
2251                 read ? "read" : "write", (long)*ppos, count,
2252                 nonblock ? "non" : "");
2253
2254         if (!data)
2255                 return -EINVAL;
2256
2257         /*
2258          * Initialize emulator on first call.
2259          */
2260         if (!q->fileio) {
2261                 ret = __vb2_init_fileio(q, read);
2262                 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
2263                 if (ret)
2264                         return ret;
2265         }
2266         fileio = q->fileio;
2267
2268         /*
2269          * Hack fileio context to enable direct calls to vb2 ioctl interface.
2270          * The pointer will be restored before returning from this function.
2271          */
2272         q->fileio = NULL;
2273
2274         index = fileio->index;
2275         buf = &fileio->bufs[index];
2276
2277         /*
2278          * Check if we need to dequeue the buffer.
2279          */
2280         if (buf->queued) {
2281                 struct vb2_buffer *vb;
2282
2283                 /*
2284                  * Call vb2_dqbuf to get buffer back.
2285                  */
2286                 memset(&fileio->b, 0, sizeof(fileio->b));
2287                 fileio->b.type = q->type;
2288                 fileio->b.memory = q->memory;
2289                 fileio->b.index = index;
2290                 ret = vb2_dqbuf(q, &fileio->b, nonblock);
2291                 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2292                 if (ret)
2293                         goto end;
2294                 fileio->dq_count += 1;
2295
2296                 /*
2297                  * Get number of bytes filled by the driver
2298                  */
2299                 vb = q->bufs[index];
2300                 buf->size = vb2_get_plane_payload(vb, 0);
2301                 buf->queued = 0;
2302         }
2303
2304         /*
2305          * Limit count on last few bytes of the buffer.
2306          */
2307         if (buf->pos + count > buf->size) {
2308                 count = buf->size - buf->pos;
2309                 dprintk(5, "reducing read count: %zd\n", count);
2310         }
2311
2312         /*
2313          * Transfer data to userspace.
2314          */
2315         dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
2316                 count, index, buf->pos);
2317         if (read)
2318                 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2319         else
2320                 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2321         if (ret) {
2322                 dprintk(3, "file io: error copying data\n");
2323                 ret = -EFAULT;
2324                 goto end;
2325         }
2326
2327         /*
2328          * Update counters.
2329          */
2330         buf->pos += count;
2331         *ppos += count;
2332
2333         /*
2334          * Queue next buffer if required.
2335          */
2336         if (buf->pos == buf->size ||
2337            (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2338                 /*
2339                  * Check if this is the last buffer to read.
2340                  */
2341                 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2342                     fileio->dq_count == 1) {
2343                         dprintk(3, "file io: read limit reached\n");
2344                         /*
2345                          * Restore fileio pointer and release the context.
2346                          */
2347                         q->fileio = fileio;
2348                         return __vb2_cleanup_fileio(q);
2349                 }
2350
2351                 /*
2352                  * Call vb2_qbuf and give buffer to the driver.
2353                  */
2354                 memset(&fileio->b, 0, sizeof(fileio->b));
2355                 fileio->b.type = q->type;
2356                 fileio->b.memory = q->memory;
2357                 fileio->b.index = index;
2358                 fileio->b.bytesused = buf->pos;
2359                 ret = vb2_qbuf(q, &fileio->b);
2360                 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2361                 if (ret)
2362                         goto end;
2363
2364                 /*
2365                  * Buffer has been queued, update the status
2366                  */
2367                 buf->pos = 0;
2368                 buf->queued = 1;
2369                 buf->size = q->bufs[0]->v4l2_planes[0].length;
2370                 fileio->q_count += 1;
2371
2372                 /*
2373                  * Switch to the next buffer
2374                  */
2375                 fileio->index = (index + 1) % q->num_buffers;
2376
2377                 /*
2378                  * Start streaming if required.
2379                  */
2380                 if (!read && !q->streaming) {
2381                         ret = vb2_streamon(q, q->type);
2382                         if (ret)
2383                                 goto end;
2384                 }
2385         }
2386
2387         /*
2388          * Return proper number of bytes processed.
2389          */
2390         if (ret == 0)
2391                 ret = count;
2392 end:
2393         /*
2394          * Restore the fileio context and block vb2 ioctl interface.
2395          */
2396         q->fileio = fileio;
2397         return ret;
2398 }
2399
2400 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2401                 loff_t *ppos, int nonblocking)
2402 {
2403         return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2404 }
2405 EXPORT_SYMBOL_GPL(vb2_read);
2406
2407 size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
2408                 loff_t *ppos, int nonblocking)
2409 {
2410         return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
2411 }
2412 EXPORT_SYMBOL_GPL(vb2_write);
2413
2414
2415 /*
2416  * The following functions are not part of the vb2 core API, but are helper
2417  * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2418  * and struct vb2_ops.
2419  * They contain boilerplate code that most if not all drivers have to do
2420  * and so they simplify the driver code.
2421  */
2422
2423 /* The queue is busy if there is a owner and you are not that owner. */
2424 static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
2425 {
2426         return vdev->queue->owner && vdev->queue->owner != file->private_data;
2427 }
2428
2429 /* vb2 ioctl helpers */
2430
2431 int vb2_ioctl_reqbufs(struct file *file, void *priv,
2432                           struct v4l2_requestbuffers *p)
2433 {
2434         struct video_device *vdev = video_devdata(file);
2435         int res = __verify_memory_type(vdev->queue, p->memory, p->type);
2436
2437         if (res)
2438                 return res;
2439         if (vb2_queue_is_busy(vdev, file))
2440                 return -EBUSY;
2441         res = __reqbufs(vdev->queue, p);
2442         /* If count == 0, then the owner has released all buffers and he
2443            is no longer owner of the queue. Otherwise we have a new owner. */
2444         if (res == 0)
2445                 vdev->queue->owner = p->count ? file->private_data : NULL;
2446         return res;
2447 }
2448 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
2449
2450 int vb2_ioctl_create_bufs(struct file *file, void *priv,
2451                           struct v4l2_create_buffers *p)
2452 {
2453         struct video_device *vdev = video_devdata(file);
2454         int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
2455
2456         p->index = vdev->queue->num_buffers;
2457         /* If count == 0, then just check if memory and type are valid.
2458            Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2459         if (p->count == 0)
2460                 return res != -EBUSY ? res : 0;
2461         if (res)
2462                 return res;
2463         if (vb2_queue_is_busy(vdev, file))
2464                 return -EBUSY;
2465         res = __create_bufs(vdev->queue, p);
2466         if (res == 0)
2467                 vdev->queue->owner = file->private_data;
2468         return res;
2469 }
2470 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
2471
2472 int vb2_ioctl_prepare_buf(struct file *file, void *priv,
2473                           struct v4l2_buffer *p)
2474 {
2475         struct video_device *vdev = video_devdata(file);
2476
2477         if (vb2_queue_is_busy(vdev, file))
2478                 return -EBUSY;
2479         return vb2_prepare_buf(vdev->queue, p);
2480 }
2481 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
2482
2483 int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
2484 {
2485         struct video_device *vdev = video_devdata(file);
2486
2487         /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2488         return vb2_querybuf(vdev->queue, p);
2489 }
2490 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
2491
2492 int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2493 {
2494         struct video_device *vdev = video_devdata(file);
2495
2496         if (vb2_queue_is_busy(vdev, file))
2497                 return -EBUSY;
2498         return vb2_qbuf(vdev->queue, p);
2499 }
2500 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
2501
2502 int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2503 {
2504         struct video_device *vdev = video_devdata(file);
2505
2506         if (vb2_queue_is_busy(vdev, file))
2507                 return -EBUSY;
2508         return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
2509 }
2510 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
2511
2512 int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
2513 {
2514         struct video_device *vdev = video_devdata(file);
2515
2516         if (vb2_queue_is_busy(vdev, file))
2517                 return -EBUSY;
2518         return vb2_streamon(vdev->queue, i);
2519 }
2520 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
2521
2522 int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
2523 {
2524         struct video_device *vdev = video_devdata(file);
2525
2526         if (vb2_queue_is_busy(vdev, file))
2527                 return -EBUSY;
2528         return vb2_streamoff(vdev->queue, i);
2529 }
2530 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
2531
2532 int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
2533 {
2534         struct video_device *vdev = video_devdata(file);
2535
2536         if (vb2_queue_is_busy(vdev, file))
2537                 return -EBUSY;
2538         return vb2_expbuf(vdev->queue, p);
2539 }
2540 EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
2541
2542 /* v4l2_file_operations helpers */
2543
2544 int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
2545 {
2546         struct video_device *vdev = video_devdata(file);
2547
2548         return vb2_mmap(vdev->queue, vma);
2549 }
2550 EXPORT_SYMBOL_GPL(vb2_fop_mmap);
2551
2552 int vb2_fop_release(struct file *file)
2553 {
2554         struct video_device *vdev = video_devdata(file);
2555
2556         if (file->private_data == vdev->queue->owner) {
2557                 vb2_queue_release(vdev->queue);
2558                 vdev->queue->owner = NULL;
2559         }
2560         return v4l2_fh_release(file);
2561 }
2562 EXPORT_SYMBOL_GPL(vb2_fop_release);
2563
2564 ssize_t vb2_fop_write(struct file *file, char __user *buf,
2565                 size_t count, loff_t *ppos)
2566 {
2567         struct video_device *vdev = video_devdata(file);
2568         struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2569         int err = -EBUSY;
2570
2571         if (lock && mutex_lock_interruptible(lock))
2572                 return -ERESTARTSYS;
2573         if (vb2_queue_is_busy(vdev, file))
2574                 goto exit;
2575         err = vb2_write(vdev->queue, buf, count, ppos,
2576                        file->f_flags & O_NONBLOCK);
2577         if (vdev->queue->fileio)
2578                 vdev->queue->owner = file->private_data;
2579 exit:
2580         if (lock)
2581                 mutex_unlock(lock);
2582         return err;
2583 }
2584 EXPORT_SYMBOL_GPL(vb2_fop_write);
2585
2586 ssize_t vb2_fop_read(struct file *file, char __user *buf,
2587                 size_t count, loff_t *ppos)
2588 {
2589         struct video_device *vdev = video_devdata(file);
2590         struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2591         int err = -EBUSY;
2592
2593         if (lock && mutex_lock_interruptible(lock))
2594                 return -ERESTARTSYS;
2595         if (vb2_queue_is_busy(vdev, file))
2596                 goto exit;
2597         err = vb2_read(vdev->queue, buf, count, ppos,
2598                        file->f_flags & O_NONBLOCK);
2599         if (vdev->queue->fileio)
2600                 vdev->queue->owner = file->private_data;
2601 exit:
2602         if (lock)
2603                 mutex_unlock(lock);
2604         return err;
2605 }
2606 EXPORT_SYMBOL_GPL(vb2_fop_read);
2607
2608 unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
2609 {
2610         struct video_device *vdev = video_devdata(file);
2611         struct vb2_queue *q = vdev->queue;
2612         struct mutex *lock = q->lock ? q->lock : vdev->lock;
2613         unsigned long req_events = poll_requested_events(wait);
2614         unsigned res;
2615         void *fileio;
2616         bool must_lock = false;
2617
2618         /* Try to be smart: only lock if polling might start fileio,
2619            otherwise locking will only introduce unwanted delays. */
2620         if (q->num_buffers == 0 && q->fileio == NULL) {
2621                 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2622                                 (req_events & (POLLIN | POLLRDNORM)))
2623                         must_lock = true;
2624                 else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2625                                 (req_events & (POLLOUT | POLLWRNORM)))
2626                         must_lock = true;
2627         }
2628
2629         /* If locking is needed, but this helper doesn't know how, then you
2630            shouldn't be using this helper but you should write your own. */
2631         WARN_ON(must_lock && !lock);
2632
2633         if (must_lock && lock && mutex_lock_interruptible(lock))
2634                 return POLLERR;
2635
2636         fileio = q->fileio;
2637
2638         res = vb2_poll(vdev->queue, file, wait);
2639
2640         /* If fileio was started, then we have a new queue owner. */
2641         if (must_lock && !fileio && q->fileio)
2642                 q->owner = file->private_data;
2643         if (must_lock && lock)
2644                 mutex_unlock(lock);
2645         return res;
2646 }
2647 EXPORT_SYMBOL_GPL(vb2_fop_poll);
2648
2649 #ifndef CONFIG_MMU
2650 unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
2651                 unsigned long len, unsigned long pgoff, unsigned long flags)
2652 {
2653         struct video_device *vdev = video_devdata(file);
2654
2655         return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
2656 }
2657 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
2658 #endif
2659
2660 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
2661
2662 void vb2_ops_wait_prepare(struct vb2_queue *vq)
2663 {
2664         mutex_unlock(vq->lock);
2665 }
2666 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
2667
2668 void vb2_ops_wait_finish(struct vb2_queue *vq)
2669 {
2670         mutex_lock(vq->lock);
2671 }
2672 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
2673
2674 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
2675 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2676 MODULE_LICENSE("GPL");