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