blob: 91412d463ba0e1eba3a311514f42dbe086a587a6 [file] [log] [blame]
Pawel Osciake23ccc02010-10-11 10:56:41 -03001/*
2 * videobuf2-core.c - V4L2 driver helper framework
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 *
Pawel Osciak95072082011-03-13 15:23:32 -03006 * Author: Pawel Osciak <pawel@osciak.com>
Pawel Osciake23ccc02010-10-11 10:56:41 -03007 * 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
Hans Verkuil95213ce2011-07-13 04:26:52 -030022#include <media/v4l2-dev.h>
23#include <media/v4l2-fh.h>
24#include <media/v4l2-event.h>
Pawel Osciake23ccc02010-10-11 10:56:41 -030025#include <media/videobuf2-core.h>
26
27static int debug;
28module_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
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030036#define call_memop(q, op, args...) \
Pawel Osciake23ccc02010-10-11 10:56:41 -030037 (((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
Sakari Ailus1b18e7a2012-10-22 17:10:16 -030043#define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -030044 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
Sakari Ailus1b18e7a2012-10-22 17:10:16 -030045 V4L2_BUF_FLAG_PREPARED | \
46 V4L2_BUF_FLAG_TIMESTAMP_MASK)
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -030047
Pawel Osciake23ccc02010-10-11 10:56:41 -030048/**
49 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
50 */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030051static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
Pawel Osciake23ccc02010-10-11 10:56:41 -030052{
53 struct vb2_queue *q = vb->vb2_queue;
54 void *mem_priv;
55 int plane;
56
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -030057 /*
58 * Allocate memory for all planes in this buffer
59 * NOTE: mmapped areas should be page aligned
60 */
Pawel Osciake23ccc02010-10-11 10:56:41 -030061 for (plane = 0; plane < vb->num_planes; ++plane) {
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -030062 unsigned long size = PAGE_ALIGN(q->plane_sizes[plane]);
63
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030064 mem_priv = call_memop(q, alloc, q->alloc_ctx[plane],
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -030065 size, q->gfp_flags);
Guennadi Liakhovetski62a79432011-03-22 09:24:58 -030066 if (IS_ERR_OR_NULL(mem_priv))
Pawel Osciake23ccc02010-10-11 10:56:41 -030067 goto free;
68
69 /* Associate allocator private data with this plane */
70 vb->planes[plane].mem_priv = mem_priv;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030071 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
Pawel Osciake23ccc02010-10-11 10:56:41 -030072 }
73
74 return 0;
75free:
76 /* Free already allocated memory if one of the allocations failed */
Marek Szyprowskia00d0262011-12-15 05:53:06 -030077 for (; plane > 0; --plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030078 call_memop(q, put, vb->planes[plane - 1].mem_priv);
Marek Szyprowskia00d0262011-12-15 05:53:06 -030079 vb->planes[plane - 1].mem_priv = NULL;
80 }
Pawel Osciake23ccc02010-10-11 10:56:41 -030081
82 return -ENOMEM;
83}
84
85/**
86 * __vb2_buf_mem_free() - free memory of the given buffer
87 */
88static void __vb2_buf_mem_free(struct vb2_buffer *vb)
89{
90 struct vb2_queue *q = vb->vb2_queue;
91 unsigned int plane;
92
93 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030094 call_memop(q, put, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -030095 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskia00d0262011-12-15 05:53:06 -030096 dprintk(3, "Freed plane %d of buffer %d\n", plane,
97 vb->v4l2_buf.index);
Pawel Osciake23ccc02010-10-11 10:56:41 -030098 }
99}
100
101/**
102 * __vb2_buf_userptr_put() - release userspace memory associated with
103 * a USERPTR buffer
104 */
105static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
106{
107 struct vb2_queue *q = vb->vb2_queue;
108 unsigned int plane;
109
110 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300111 if (vb->planes[plane].mem_priv)
112 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
113 vb->planes[plane].mem_priv = NULL;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300114 }
115}
116
117/**
Sumit Semwalc5384042012-06-14 10:37:37 -0300118 * __vb2_plane_dmabuf_put() - release memory associated with
119 * a DMABUF shared plane
120 */
121static void __vb2_plane_dmabuf_put(struct vb2_queue *q, struct vb2_plane *p)
122{
123 if (!p->mem_priv)
124 return;
125
126 if (p->dbuf_mapped)
127 call_memop(q, unmap_dmabuf, p->mem_priv);
128
129 call_memop(q, detach_dmabuf, p->mem_priv);
130 dma_buf_put(p->dbuf);
131 memset(p, 0, sizeof(*p));
132}
133
134/**
135 * __vb2_buf_dmabuf_put() - release memory associated with
136 * a DMABUF shared buffer
137 */
138static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
139{
140 struct vb2_queue *q = vb->vb2_queue;
141 unsigned int plane;
142
143 for (plane = 0; plane < vb->num_planes; ++plane)
144 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
145}
146
147/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300148 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
149 * every buffer on the queue
150 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300151static void __setup_offsets(struct vb2_queue *q, unsigned int n)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300152{
153 unsigned int buffer, plane;
154 struct vb2_buffer *vb;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300155 unsigned long off;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300156
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300157 if (q->num_buffers) {
158 struct v4l2_plane *p;
159 vb = q->bufs[q->num_buffers - 1];
160 p = &vb->v4l2_planes[vb->num_planes - 1];
161 off = PAGE_ALIGN(p->m.mem_offset + p->length);
162 } else {
163 off = 0;
164 }
165
166 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300167 vb = q->bufs[buffer];
168 if (!vb)
169 continue;
170
171 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski49076022011-10-13 07:07:24 -0300172 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
Pawel Osciake23ccc02010-10-11 10:56:41 -0300173 vb->v4l2_planes[plane].m.mem_offset = off;
174
175 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
176 buffer, plane, off);
177
178 off += vb->v4l2_planes[plane].length;
179 off = PAGE_ALIGN(off);
180 }
181 }
182}
183
184/**
185 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
186 * video buffer memory for all buffers/planes on the queue and initializes the
187 * queue
188 *
189 * Returns the number of buffers successfully allocated.
190 */
191static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300192 unsigned int num_buffers, unsigned int num_planes)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300193{
194 unsigned int buffer;
195 struct vb2_buffer *vb;
196 int ret;
197
198 for (buffer = 0; buffer < num_buffers; ++buffer) {
199 /* Allocate videobuf buffer structures */
200 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
201 if (!vb) {
202 dprintk(1, "Memory alloc for buffer struct failed\n");
203 break;
204 }
205
206 /* Length stores number of planes for multiplanar buffers */
207 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
208 vb->v4l2_buf.length = num_planes;
209
210 vb->state = VB2_BUF_STATE_DEQUEUED;
211 vb->vb2_queue = q;
212 vb->num_planes = num_planes;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300213 vb->v4l2_buf.index = q->num_buffers + buffer;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300214 vb->v4l2_buf.type = q->type;
215 vb->v4l2_buf.memory = memory;
216
217 /* Allocate video buffer memory for the MMAP type */
218 if (memory == V4L2_MEMORY_MMAP) {
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300219 ret = __vb2_buf_mem_alloc(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300220 if (ret) {
221 dprintk(1, "Failed allocating memory for "
222 "buffer %d\n", buffer);
223 kfree(vb);
224 break;
225 }
226 /*
227 * Call the driver-provided buffer initialization
228 * callback, if given. An error in initialization
229 * results in queue setup failure.
230 */
231 ret = call_qop(q, buf_init, vb);
232 if (ret) {
233 dprintk(1, "Buffer %d %p initialization"
234 " failed\n", buffer, vb);
235 __vb2_buf_mem_free(vb);
236 kfree(vb);
237 break;
238 }
239 }
240
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300241 q->bufs[q->num_buffers + buffer] = vb;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300242 }
243
Philipp Zabeldc775232013-09-19 04:37:29 -0300244 if (memory == V4L2_MEMORY_MMAP)
245 __setup_offsets(q, buffer);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300246
247 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300248 buffer, num_planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300249
250 return buffer;
251}
252
253/**
254 * __vb2_free_mem() - release all video buffer memory for a given queue
255 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300256static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300257{
258 unsigned int buffer;
259 struct vb2_buffer *vb;
260
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300261 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
262 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300263 vb = q->bufs[buffer];
264 if (!vb)
265 continue;
266
267 /* Free MMAP buffers or release USERPTR buffers */
268 if (q->memory == V4L2_MEMORY_MMAP)
269 __vb2_buf_mem_free(vb);
Sumit Semwalc5384042012-06-14 10:37:37 -0300270 else if (q->memory == V4L2_MEMORY_DMABUF)
271 __vb2_buf_dmabuf_put(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300272 else
273 __vb2_buf_userptr_put(vb);
274 }
275}
276
277/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300278 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
279 * related information, if no buffers are left return the queue to an
280 * uninitialized state. Might be called even if the queue has already been freed.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300281 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300282static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300283{
284 unsigned int buffer;
285
286 /* Call driver-provided cleanup function for each buffer, if provided */
287 if (q->ops->buf_cleanup) {
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300288 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
289 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300290 if (NULL == q->bufs[buffer])
291 continue;
292 q->ops->buf_cleanup(q->bufs[buffer]);
293 }
294 }
295
296 /* Release video buffer memory */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300297 __vb2_free_mem(q, buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300298
299 /* Free videobuf buffers */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300300 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
301 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300302 kfree(q->bufs[buffer]);
303 q->bufs[buffer] = NULL;
304 }
305
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300306 q->num_buffers -= buffers;
307 if (!q->num_buffers)
308 q->memory = 0;
Marek Szyprowskibd50d992011-10-25 03:07:59 -0300309 INIT_LIST_HEAD(&q->queued_list);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300310}
311
312/**
313 * __verify_planes_array() - verify that the planes array passed in struct
314 * v4l2_buffer from userspace can be safely used
315 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300316static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300317{
Hans Verkuil32a77262012-09-28 06:12:53 -0300318 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
319 return 0;
320
Pawel Osciake23ccc02010-10-11 10:56:41 -0300321 /* Is memory for copying plane information present? */
322 if (NULL == b->m.planes) {
323 dprintk(1, "Multi-planar buffer passed but "
324 "planes array not provided\n");
325 return -EINVAL;
326 }
327
328 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
329 dprintk(1, "Incorrect planes array length, "
330 "expected %d, got %d\n", vb->num_planes, b->length);
331 return -EINVAL;
332 }
333
334 return 0;
335}
336
337/**
Laurent Pinchart8023ed02012-07-10 10:41:40 -0300338 * __verify_length() - Verify that the bytesused value for each plane fits in
339 * the plane length and that the data offset doesn't exceed the bytesused value.
340 */
341static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
342{
343 unsigned int length;
344 unsigned int plane;
345
346 if (!V4L2_TYPE_IS_OUTPUT(b->type))
347 return 0;
348
349 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
350 for (plane = 0; plane < vb->num_planes; ++plane) {
351 length = (b->memory == V4L2_MEMORY_USERPTR)
352 ? b->m.planes[plane].length
353 : vb->v4l2_planes[plane].length;
354
355 if (b->m.planes[plane].bytesused > length)
356 return -EINVAL;
357 if (b->m.planes[plane].data_offset >=
358 b->m.planes[plane].bytesused)
359 return -EINVAL;
360 }
361 } else {
362 length = (b->memory == V4L2_MEMORY_USERPTR)
363 ? b->length : vb->v4l2_planes[0].length;
364
365 if (b->bytesused > length)
366 return -EINVAL;
367 }
368
369 return 0;
370}
371
372/**
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300373 * __buffer_in_use() - return true if the buffer is in use and
374 * the queue cannot be freed (by the means of REQBUFS(0)) call
375 */
376static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
377{
378 unsigned int plane;
379 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski2c2dd6ac2011-10-12 13:09:53 -0300380 void *mem_priv = vb->planes[plane].mem_priv;
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300381 /*
382 * If num_users() has not been provided, call_memop
383 * will return 0, apparently nobody cares about this
384 * case anyway. If num_users() returns more than 1,
385 * we are not the only user of the plane's memory.
386 */
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300387 if (mem_priv && call_memop(q, num_users, mem_priv) > 1)
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300388 return true;
389 }
390 return false;
391}
392
393/**
394 * __buffers_in_use() - return true if any buffers on the queue are in use and
395 * the queue cannot be freed (by the means of REQBUFS(0)) call
396 */
397static bool __buffers_in_use(struct vb2_queue *q)
398{
399 unsigned int buffer;
400 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
401 if (__buffer_in_use(q, q->bufs[buffer]))
402 return true;
403 }
404 return false;
405}
406
407/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300408 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
409 * returned to userspace
410 */
Hans Verkuil32a77262012-09-28 06:12:53 -0300411static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300412{
413 struct vb2_queue *q = vb->vb2_queue;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300414
Sakari Ailus2b719d72012-05-02 09:40:03 -0300415 /* Copy back data such as timestamp, flags, etc. */
Pawel Osciake23ccc02010-10-11 10:56:41 -0300416 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
Sakari Ailus2b719d72012-05-02 09:40:03 -0300417 b->reserved2 = vb->v4l2_buf.reserved2;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300418 b->reserved = vb->v4l2_buf.reserved;
419
420 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300421 /*
422 * Fill in plane-related data if userspace provided an array
Hans Verkuil32a77262012-09-28 06:12:53 -0300423 * for it. The caller has already verified memory and size.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300424 */
Hans Verkuil3c0b6062012-09-28 06:24:18 -0300425 b->length = vb->num_planes;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300426 memcpy(b->m.planes, vb->v4l2_planes,
427 b->length * sizeof(struct v4l2_plane));
428 } else {
429 /*
430 * We use length and offset in v4l2_planes array even for
431 * single-planar buffers, but userspace does not.
432 */
433 b->length = vb->v4l2_planes[0].length;
434 b->bytesused = vb->v4l2_planes[0].bytesused;
435 if (q->memory == V4L2_MEMORY_MMAP)
436 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
437 else if (q->memory == V4L2_MEMORY_USERPTR)
438 b->m.userptr = vb->v4l2_planes[0].m.userptr;
Sumit Semwalc5384042012-06-14 10:37:37 -0300439 else if (q->memory == V4L2_MEMORY_DMABUF)
440 b->m.fd = vb->v4l2_planes[0].m.fd;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300441 }
442
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300443 /*
444 * Clear any buffer state related flags.
445 */
Sakari Ailus1b18e7a2012-10-22 17:10:16 -0300446 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
Kamil Debski6aa69f92013-01-25 06:29:57 -0300447 b->flags |= q->timestamp_type;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300448
449 switch (vb->state) {
450 case VB2_BUF_STATE_QUEUED:
451 case VB2_BUF_STATE_ACTIVE:
452 b->flags |= V4L2_BUF_FLAG_QUEUED;
453 break;
454 case VB2_BUF_STATE_ERROR:
455 b->flags |= V4L2_BUF_FLAG_ERROR;
456 /* fall through */
457 case VB2_BUF_STATE_DONE:
458 b->flags |= V4L2_BUF_FLAG_DONE;
459 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300460 case VB2_BUF_STATE_PREPARED:
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300461 b->flags |= V4L2_BUF_FLAG_PREPARED;
462 break;
463 case VB2_BUF_STATE_DEQUEUED:
Pawel Osciake23ccc02010-10-11 10:56:41 -0300464 /* nothing */
465 break;
466 }
467
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300468 if (__buffer_in_use(q, vb))
Pawel Osciake23ccc02010-10-11 10:56:41 -0300469 b->flags |= V4L2_BUF_FLAG_MAPPED;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300470}
471
472/**
473 * vb2_querybuf() - query video buffer information
474 * @q: videobuf queue
475 * @b: buffer struct passed from userspace to vidioc_querybuf handler
476 * in driver
477 *
478 * Should be called from vidioc_querybuf ioctl handler in driver.
479 * This function will verify the passed v4l2_buffer structure and fill the
480 * relevant information for the userspace.
481 *
482 * The return values from this function are intended to be directly returned
483 * from vidioc_querybuf handler in driver.
484 */
485int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
486{
487 struct vb2_buffer *vb;
Hans Verkuil32a77262012-09-28 06:12:53 -0300488 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300489
490 if (b->type != q->type) {
491 dprintk(1, "querybuf: wrong buffer type\n");
492 return -EINVAL;
493 }
494
495 if (b->index >= q->num_buffers) {
496 dprintk(1, "querybuf: buffer index out of range\n");
497 return -EINVAL;
498 }
499 vb = q->bufs[b->index];
Hans Verkuil32a77262012-09-28 06:12:53 -0300500 ret = __verify_planes_array(vb, b);
501 if (!ret)
502 __fill_v4l2_buffer(vb, b);
503 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300504}
505EXPORT_SYMBOL(vb2_querybuf);
506
507/**
508 * __verify_userptr_ops() - verify that all memory operations required for
509 * USERPTR queue type have been provided
510 */
511static int __verify_userptr_ops(struct vb2_queue *q)
512{
513 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
514 !q->mem_ops->put_userptr)
515 return -EINVAL;
516
517 return 0;
518}
519
520/**
521 * __verify_mmap_ops() - verify that all memory operations required for
522 * MMAP queue type have been provided
523 */
524static int __verify_mmap_ops(struct vb2_queue *q)
525{
526 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
527 !q->mem_ops->put || !q->mem_ops->mmap)
528 return -EINVAL;
529
530 return 0;
531}
532
533/**
Sumit Semwalc5384042012-06-14 10:37:37 -0300534 * __verify_dmabuf_ops() - verify that all memory operations required for
535 * DMABUF queue type have been provided
536 */
537static int __verify_dmabuf_ops(struct vb2_queue *q)
538{
539 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
540 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
541 !q->mem_ops->unmap_dmabuf)
542 return -EINVAL;
543
544 return 0;
545}
546
547/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300548 * __verify_memory_type() - Check whether the memory type and buffer type
549 * passed to a buffer operation are compatible with the queue.
550 */
551static int __verify_memory_type(struct vb2_queue *q,
552 enum v4l2_memory memory, enum v4l2_buf_type type)
553{
Sumit Semwalc5384042012-06-14 10:37:37 -0300554 if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR &&
555 memory != V4L2_MEMORY_DMABUF) {
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300556 dprintk(1, "reqbufs: unsupported memory type\n");
557 return -EINVAL;
558 }
559
560 if (type != q->type) {
561 dprintk(1, "reqbufs: requested type is incorrect\n");
562 return -EINVAL;
563 }
564
565 /*
566 * Make sure all the required memory ops for given memory type
567 * are available.
568 */
569 if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
570 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
571 return -EINVAL;
572 }
573
574 if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
575 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
576 return -EINVAL;
577 }
578
Sumit Semwalc5384042012-06-14 10:37:37 -0300579 if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
580 dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
581 return -EINVAL;
582 }
583
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300584 /*
585 * Place the busy tests at the end: -EBUSY can be ignored when
586 * create_bufs is called with count == 0, but count == 0 should still
587 * do the memory and type validation.
588 */
589 if (q->fileio) {
590 dprintk(1, "reqbufs: file io in progress\n");
591 return -EBUSY;
592 }
593 return 0;
594}
595
596/**
597 * __reqbufs() - Initiate streaming
Pawel Osciake23ccc02010-10-11 10:56:41 -0300598 * @q: videobuf2 queue
599 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
600 *
601 * Should be called from vidioc_reqbufs ioctl handler of a driver.
602 * This function:
603 * 1) verifies streaming parameters passed from the userspace,
604 * 2) sets up the queue,
605 * 3) negotiates number of buffers and planes per buffer with the driver
606 * to be used during streaming,
607 * 4) allocates internal buffer structures (struct vb2_buffer), according to
608 * the agreed parameters,
609 * 5) for MMAP memory type, allocates actual video memory, using the
610 * memory handling/allocation routines provided during queue initialization
611 *
612 * If req->count is 0, all the memory will be freed instead.
613 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
614 * and the queue is not busy, memory will be reallocated.
615 *
616 * The return values from this function are intended to be directly returned
617 * from vidioc_reqbufs handler in driver.
618 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300619static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300620{
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300621 unsigned int num_buffers, allocated_buffers, num_planes = 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300622 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300623
624 if (q->streaming) {
625 dprintk(1, "reqbufs: streaming active\n");
626 return -EBUSY;
627 }
628
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300629 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300630 /*
631 * We already have buffers allocated, so first check if they
632 * are not in use and can be freed.
633 */
634 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
635 dprintk(1, "reqbufs: memory in use, cannot free\n");
636 return -EBUSY;
637 }
638
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300639 __vb2_queue_free(q, q->num_buffers);
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300640
641 /*
642 * In case of REQBUFS(0) return immediately without calling
643 * driver's queue_setup() callback and allocating resources.
644 */
645 if (req->count == 0)
646 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300647 }
648
649 /*
650 * Make sure the requested values and current defaults are sane.
651 */
652 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300653 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
Pawel Osciake23ccc02010-10-11 10:56:41 -0300654 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
Marek Szyprowski13b14092011-04-14 07:17:44 -0300655 q->memory = req->memory;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300656
657 /*
658 * Ask the driver how many buffers and planes per buffer it requires.
659 * Driver also sets the size and allocator context for each plane.
660 */
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300661 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300662 q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300663 if (ret)
664 return ret;
665
666 /* Finally, allocate buffers and video memory */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300667 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
Marek Szyprowski66072d42011-06-28 08:29:02 -0300668 if (ret == 0) {
669 dprintk(1, "Memory allocation failed\n");
670 return -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300671 }
672
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300673 allocated_buffers = ret;
674
Pawel Osciake23ccc02010-10-11 10:56:41 -0300675 /*
676 * Check if driver can handle the allocated number of buffers.
677 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300678 if (allocated_buffers < num_buffers) {
679 num_buffers = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300680
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300681 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
682 &num_planes, q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300683
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300684 if (!ret && allocated_buffers < num_buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300685 ret = -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300686
687 /*
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300688 * Either the driver has accepted a smaller number of buffers,
689 * or .queue_setup() returned an error
Pawel Osciake23ccc02010-10-11 10:56:41 -0300690 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300691 }
692
693 q->num_buffers = allocated_buffers;
694
695 if (ret < 0) {
696 __vb2_queue_free(q, allocated_buffers);
697 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300698 }
699
Pawel Osciake23ccc02010-10-11 10:56:41 -0300700 /*
701 * Return the number of successfully allocated buffers
702 * to the userspace.
703 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300704 req->count = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300705
706 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300707}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300708
709/**
710 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
711 * type values.
712 * @q: videobuf2 queue
713 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
714 */
715int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
716{
717 int ret = __verify_memory_type(q, req->memory, req->type);
718
719 return ret ? ret : __reqbufs(q, req);
720}
Pawel Osciake23ccc02010-10-11 10:56:41 -0300721EXPORT_SYMBOL_GPL(vb2_reqbufs);
722
723/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300724 * __create_bufs() - Allocate buffers and any required auxiliary structs
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300725 * @q: videobuf2 queue
726 * @create: creation parameters, passed from userspace to vidioc_create_bufs
727 * handler in driver
728 *
729 * Should be called from vidioc_create_bufs ioctl handler of a driver.
730 * This function:
731 * 1) verifies parameter sanity
732 * 2) calls the .queue_setup() queue operation
733 * 3) performs any necessary memory allocations
734 *
735 * The return values from this function are intended to be directly returned
736 * from vidioc_create_bufs handler in driver.
737 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300738static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300739{
740 unsigned int num_planes = 0, num_buffers, allocated_buffers;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300741 int ret;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300742
743 if (q->num_buffers == VIDEO_MAX_FRAME) {
744 dprintk(1, "%s(): maximum number of buffers already allocated\n",
745 __func__);
746 return -ENOBUFS;
747 }
748
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300749 if (!q->num_buffers) {
750 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
751 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
752 q->memory = create->memory;
753 }
754
755 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
756
757 /*
758 * Ask the driver, whether the requested number of buffers, planes per
759 * buffer and their sizes are acceptable
760 */
761 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
762 &num_planes, q->plane_sizes, q->alloc_ctx);
763 if (ret)
764 return ret;
765
766 /* Finally, allocate buffers and video memory */
767 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
768 num_planes);
Hans Verkuilf05393d22012-06-22 05:44:14 -0300769 if (ret == 0) {
770 dprintk(1, "Memory allocation failed\n");
771 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300772 }
773
774 allocated_buffers = ret;
775
776 /*
777 * Check if driver can handle the so far allocated number of buffers.
778 */
779 if (ret < num_buffers) {
780 num_buffers = ret;
781
782 /*
783 * q->num_buffers contains the total number of buffers, that the
784 * queue driver has set up
785 */
786 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
787 &num_planes, q->plane_sizes, q->alloc_ctx);
788
789 if (!ret && allocated_buffers < num_buffers)
790 ret = -ENOMEM;
791
792 /*
793 * Either the driver has accepted a smaller number of buffers,
794 * or .queue_setup() returned an error
795 */
796 }
797
798 q->num_buffers += allocated_buffers;
799
800 if (ret < 0) {
801 __vb2_queue_free(q, allocated_buffers);
Hans Verkuilf05393d22012-06-22 05:44:14 -0300802 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300803 }
804
805 /*
806 * Return the number of successfully allocated buffers
807 * to the userspace.
808 */
809 create->count = allocated_buffers;
810
811 return 0;
812}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300813
814/**
Nicolas THERY53aa3b12012-07-20 09:25:37 -0300815 * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
816 * memory and type values.
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300817 * @q: videobuf2 queue
818 * @create: creation parameters, passed from userspace to vidioc_create_bufs
819 * handler in driver
820 */
821int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
822{
823 int ret = __verify_memory_type(q, create->memory, create->format.type);
824
825 create->index = q->num_buffers;
Hans Verkuilf05393d22012-06-22 05:44:14 -0300826 if (create->count == 0)
827 return ret != -EBUSY ? ret : 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300828 return ret ? ret : __create_bufs(q, create);
829}
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300830EXPORT_SYMBOL_GPL(vb2_create_bufs);
831
832/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300833 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
834 * @vb: vb2_buffer to which the plane in question belongs to
835 * @plane_no: plane number for which the address is to be returned
836 *
837 * This function returns a kernel virtual address of a given plane if
838 * such a mapping exist, NULL otherwise.
839 */
840void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
841{
842 struct vb2_queue *q = vb->vb2_queue;
843
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300844 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300845 return NULL;
846
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300847 return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300848
849}
850EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
851
852/**
853 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
854 * @vb: vb2_buffer to which the plane in question belongs to
855 * @plane_no: plane number for which the cookie is to be returned
856 *
857 * This function returns an allocator specific cookie for a given plane if
858 * available, NULL otherwise. The allocator should provide some simple static
859 * inline function, which would convert this cookie to the allocator specific
860 * type that can be used directly by the driver to access the buffer. This can
861 * be for example physical address, pointer to scatter list or IOMMU mapping.
862 */
863void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
864{
865 struct vb2_queue *q = vb->vb2_queue;
866
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300867 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300868 return NULL;
869
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300870 return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300871}
872EXPORT_SYMBOL_GPL(vb2_plane_cookie);
873
874/**
875 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
876 * @vb: vb2_buffer returned from the driver
877 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
878 * or VB2_BUF_STATE_ERROR if the operation finished with an error
879 *
880 * This function should be called by the driver after a hardware operation on
881 * a buffer is finished and the buffer may be returned to userspace. The driver
882 * cannot use this buffer anymore until it is queued back to it by videobuf
883 * by the means of buf_queue callback. Only buffers previously queued to the
884 * driver by buf_queue can be passed to this function.
885 */
886void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
887{
888 struct vb2_queue *q = vb->vb2_queue;
889 unsigned long flags;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -0300890 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300891
892 if (vb->state != VB2_BUF_STATE_ACTIVE)
893 return;
894
895 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
896 return;
897
898 dprintk(4, "Done processing on buffer %d, state: %d\n",
Tushar Behera9b6f5dc2012-11-12 04:01:29 -0300899 vb->v4l2_buf.index, state);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300900
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -0300901 /* sync buffers */
902 for (plane = 0; plane < vb->num_planes; ++plane)
903 call_memop(q, finish, vb->planes[plane].mem_priv);
904
Pawel Osciake23ccc02010-10-11 10:56:41 -0300905 /* Add the buffer to the done buffers list */
906 spin_lock_irqsave(&q->done_lock, flags);
907 vb->state = state;
908 list_add_tail(&vb->done_entry, &q->done_list);
909 atomic_dec(&q->queued_count);
910 spin_unlock_irqrestore(&q->done_lock, flags);
911
912 /* Inform any processes that may be waiting for buffers */
913 wake_up(&q->done_wq);
914}
915EXPORT_SYMBOL_GPL(vb2_buffer_done);
916
917/**
Hans Verkuil32a77262012-09-28 06:12:53 -0300918 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
919 * v4l2_buffer by the userspace. The caller has already verified that struct
920 * v4l2_buffer has a valid number of planes.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300921 */
Hans Verkuil32a77262012-09-28 06:12:53 -0300922static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
Pawel Osciake23ccc02010-10-11 10:56:41 -0300923 struct v4l2_plane *v4l2_planes)
924{
925 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300926
927 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300928 /* Fill in driver-provided information for OUTPUT types */
929 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
930 /*
931 * Will have to go up to b->length when API starts
932 * accepting variable number of planes.
933 */
934 for (plane = 0; plane < vb->num_planes; ++plane) {
935 v4l2_planes[plane].bytesused =
936 b->m.planes[plane].bytesused;
937 v4l2_planes[plane].data_offset =
938 b->m.planes[plane].data_offset;
939 }
940 }
941
942 if (b->memory == V4L2_MEMORY_USERPTR) {
943 for (plane = 0; plane < vb->num_planes; ++plane) {
944 v4l2_planes[plane].m.userptr =
945 b->m.planes[plane].m.userptr;
946 v4l2_planes[plane].length =
947 b->m.planes[plane].length;
948 }
949 }
Sumit Semwalc5384042012-06-14 10:37:37 -0300950 if (b->memory == V4L2_MEMORY_DMABUF) {
951 for (plane = 0; plane < vb->num_planes; ++plane) {
952 v4l2_planes[plane].m.fd =
953 b->m.planes[plane].m.fd;
954 v4l2_planes[plane].length =
955 b->m.planes[plane].length;
956 v4l2_planes[plane].data_offset =
957 b->m.planes[plane].data_offset;
958 }
959 }
Pawel Osciake23ccc02010-10-11 10:56:41 -0300960 } else {
961 /*
962 * Single-planar buffers do not use planes array,
963 * so fill in relevant v4l2_buffer struct fields instead.
964 * In videobuf we use our internal V4l2_planes struct for
965 * single-planar buffers as well, for simplicity.
966 */
Laurent Pinchartac706bf2012-12-17 07:38:16 -0300967 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300968 v4l2_planes[0].bytesused = b->bytesused;
Laurent Pinchartac706bf2012-12-17 07:38:16 -0300969 v4l2_planes[0].data_offset = 0;
970 }
Pawel Osciake23ccc02010-10-11 10:56:41 -0300971
972 if (b->memory == V4L2_MEMORY_USERPTR) {
973 v4l2_planes[0].m.userptr = b->m.userptr;
974 v4l2_planes[0].length = b->length;
975 }
Sumit Semwalc5384042012-06-14 10:37:37 -0300976
977 if (b->memory == V4L2_MEMORY_DMABUF) {
978 v4l2_planes[0].m.fd = b->m.fd;
979 v4l2_planes[0].length = b->length;
980 v4l2_planes[0].data_offset = 0;
981 }
982
Pawel Osciake23ccc02010-10-11 10:56:41 -0300983 }
984
985 vb->v4l2_buf.field = b->field;
986 vb->v4l2_buf.timestamp = b->timestamp;
Sakari Ailus1b18e7a2012-10-22 17:10:16 -0300987 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300988}
989
990/**
991 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
992 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300993static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300994{
995 struct v4l2_plane planes[VIDEO_MAX_PLANES];
996 struct vb2_queue *q = vb->vb2_queue;
997 void *mem_priv;
998 unsigned int plane;
999 int ret;
1000 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1001
Hans Verkuil32a77262012-09-28 06:12:53 -03001002 /* Copy relevant information provided by the userspace */
1003 __fill_vb2_buffer(vb, b, planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001004
1005 for (plane = 0; plane < vb->num_planes; ++plane) {
1006 /* Skip the plane if already verified */
Marek Szyprowskif0b7c7f2011-11-16 15:09:40 -03001007 if (vb->v4l2_planes[plane].m.userptr &&
1008 vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
Pawel Osciake23ccc02010-10-11 10:56:41 -03001009 && vb->v4l2_planes[plane].length == planes[plane].length)
1010 continue;
1011
1012 dprintk(3, "qbuf: userspace address for plane %d changed, "
1013 "reacquiring memory\n", plane);
1014
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001015 /* Check if the provided plane buffer is large enough */
1016 if (planes[plane].length < q->plane_sizes[plane]) {
Seung-Woo Kim2484a7e2013-08-20 04:48:06 -03001017 dprintk(1, "qbuf: provided buffer size %u is less than "
1018 "setup size %u for plane %d\n",
1019 planes[plane].length,
1020 q->plane_sizes[plane], plane);
Marek Szyprowski4c2625d2011-10-03 03:21:45 -03001021 ret = -EINVAL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001022 goto err;
1023 }
1024
Pawel Osciake23ccc02010-10-11 10:56:41 -03001025 /* Release previously acquired memory if present */
1026 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -03001027 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001028
1029 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001030 vb->v4l2_planes[plane].m.userptr = 0;
1031 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001032
1033 /* Acquire each plane's memory */
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001034 mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
1035 planes[plane].m.userptr,
1036 planes[plane].length, write);
1037 if (IS_ERR_OR_NULL(mem_priv)) {
1038 dprintk(1, "qbuf: failed acquiring userspace "
Pawel Osciake23ccc02010-10-11 10:56:41 -03001039 "memory for plane %d\n", plane);
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001040 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
1041 goto err;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001042 }
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001043 vb->planes[plane].mem_priv = mem_priv;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001044 }
1045
1046 /*
1047 * Call driver-specific initialization on the newly acquired buffer,
1048 * if provided.
1049 */
1050 ret = call_qop(q, buf_init, vb);
1051 if (ret) {
1052 dprintk(1, "qbuf: buffer initialization failed\n");
1053 goto err;
1054 }
1055
1056 /*
1057 * Now that everything is in order, copy relevant information
1058 * provided by userspace.
1059 */
1060 for (plane = 0; plane < vb->num_planes; ++plane)
1061 vb->v4l2_planes[plane] = planes[plane];
1062
1063 return 0;
1064err:
1065 /* In case of errors, release planes that were already acquired */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001066 for (plane = 0; plane < vb->num_planes; ++plane) {
1067 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -03001068 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001069 vb->planes[plane].mem_priv = NULL;
1070 vb->v4l2_planes[plane].m.userptr = 0;
1071 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001072 }
1073
1074 return ret;
1075}
1076
1077/**
1078 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1079 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001080static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001081{
Hans Verkuil32a77262012-09-28 06:12:53 -03001082 __fill_vb2_buffer(vb, b, vb->v4l2_planes);
1083 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001084}
1085
1086/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001087 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1088 */
1089static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1090{
1091 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1092 struct vb2_queue *q = vb->vb2_queue;
1093 void *mem_priv;
1094 unsigned int plane;
1095 int ret;
1096 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1097
1098 /* Verify and copy relevant information provided by the userspace */
1099 __fill_vb2_buffer(vb, b, planes);
1100
1101 for (plane = 0; plane < vb->num_planes; ++plane) {
1102 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1103
1104 if (IS_ERR_OR_NULL(dbuf)) {
1105 dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
1106 plane);
1107 ret = -EINVAL;
1108 goto err;
1109 }
1110
1111 /* use DMABUF size if length is not provided */
1112 if (planes[plane].length == 0)
1113 planes[plane].length = dbuf->size;
1114
1115 if (planes[plane].length < planes[plane].data_offset +
1116 q->plane_sizes[plane]) {
1117 ret = -EINVAL;
1118 goto err;
1119 }
1120
1121 /* Skip the plane if already verified */
1122 if (dbuf == vb->planes[plane].dbuf &&
1123 vb->v4l2_planes[plane].length == planes[plane].length) {
1124 dma_buf_put(dbuf);
1125 continue;
1126 }
1127
1128 dprintk(1, "qbuf: buffer for plane %d changed\n", plane);
1129
1130 /* Release previously acquired memory if present */
1131 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
1132 memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
1133
1134 /* Acquire each plane's memory */
1135 mem_priv = call_memop(q, attach_dmabuf, q->alloc_ctx[plane],
1136 dbuf, planes[plane].length, write);
1137 if (IS_ERR(mem_priv)) {
1138 dprintk(1, "qbuf: failed to attach dmabuf\n");
1139 ret = PTR_ERR(mem_priv);
1140 dma_buf_put(dbuf);
1141 goto err;
1142 }
1143
1144 vb->planes[plane].dbuf = dbuf;
1145 vb->planes[plane].mem_priv = mem_priv;
1146 }
1147
1148 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1149 * really we want to do this just before the DMA, not while queueing
1150 * the buffer(s)..
1151 */
1152 for (plane = 0; plane < vb->num_planes; ++plane) {
1153 ret = call_memop(q, map_dmabuf, vb->planes[plane].mem_priv);
1154 if (ret) {
1155 dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
1156 plane);
1157 goto err;
1158 }
1159 vb->planes[plane].dbuf_mapped = 1;
1160 }
1161
1162 /*
1163 * Call driver-specific initialization on the newly acquired buffer,
1164 * if provided.
1165 */
1166 ret = call_qop(q, buf_init, vb);
1167 if (ret) {
1168 dprintk(1, "qbuf: buffer initialization failed\n");
1169 goto err;
1170 }
1171
1172 /*
1173 * Now that everything is in order, copy relevant information
1174 * provided by userspace.
1175 */
1176 for (plane = 0; plane < vb->num_planes; ++plane)
1177 vb->v4l2_planes[plane] = planes[plane];
1178
1179 return 0;
1180err:
1181 /* In case of errors, release planes that were already acquired */
1182 __vb2_buf_dmabuf_put(vb);
1183
1184 return ret;
1185}
1186
1187/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001188 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1189 */
1190static void __enqueue_in_driver(struct vb2_buffer *vb)
1191{
1192 struct vb2_queue *q = vb->vb2_queue;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001193 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001194
1195 vb->state = VB2_BUF_STATE_ACTIVE;
1196 atomic_inc(&q->queued_count);
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001197
1198 /* sync buffers */
1199 for (plane = 0; plane < vb->num_planes; ++plane)
1200 call_memop(q, prepare, vb->planes[plane].mem_priv);
1201
Pawel Osciake23ccc02010-10-11 10:56:41 -03001202 q->ops->buf_queue(vb);
1203}
1204
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001205static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001206{
1207 struct vb2_queue *q = vb->vb2_queue;
1208 int ret;
1209
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001210 ret = __verify_length(vb, b);
Sylwester Nawrocki3a9621b2013-08-26 11:47:53 -03001211 if (ret < 0) {
1212 dprintk(1, "%s(): plane parameters verification failed: %d\n",
1213 __func__, ret);
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001214 return ret;
Sylwester Nawrocki3a9621b2013-08-26 11:47:53 -03001215 }
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001216
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001217 switch (q->memory) {
1218 case V4L2_MEMORY_MMAP:
1219 ret = __qbuf_mmap(vb, b);
1220 break;
1221 case V4L2_MEMORY_USERPTR:
1222 ret = __qbuf_userptr(vb, b);
1223 break;
Sumit Semwalc5384042012-06-14 10:37:37 -03001224 case V4L2_MEMORY_DMABUF:
1225 ret = __qbuf_dmabuf(vb, b);
1226 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001227 default:
1228 WARN(1, "Invalid queue type\n");
1229 ret = -EINVAL;
1230 }
1231
1232 if (!ret)
1233 ret = call_qop(q, buf_prepare, vb);
1234 if (ret)
1235 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
1236 else
1237 vb->state = VB2_BUF_STATE_PREPARED;
1238
1239 return ret;
1240}
1241
Laurent Pinchart012043b2013-08-09 08:11:26 -03001242static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
1243 const char *opname,
1244 int (*handler)(struct vb2_queue *,
1245 struct v4l2_buffer *,
1246 struct vb2_buffer *))
1247{
1248 struct rw_semaphore *mmap_sem = NULL;
1249 struct vb2_buffer *vb;
1250 int ret;
1251
1252 /*
1253 * In case of user pointer buffers vb2 allocators need to get direct
1254 * access to userspace pages. This requires getting the mmap semaphore
1255 * for read access in the current process structure. The same semaphore
1256 * is taken before calling mmap operation, while both qbuf/prepare_buf
1257 * and mmap are called by the driver or v4l2 core with the driver's lock
1258 * held. To avoid an AB-BA deadlock (mmap_sem then driver's lock in mmap
1259 * and driver's lock then mmap_sem in qbuf/prepare_buf) the videobuf2
1260 * core releases the driver's lock, takes mmap_sem and then takes the
1261 * driver's lock again.
1262 *
1263 * To avoid racing with other vb2 calls, which might be called after
1264 * releasing the driver's lock, this operation is performed at the
1265 * beginning of qbuf/prepare_buf processing. This way the queue status
1266 * is consistent after getting the driver's lock back.
1267 */
1268 if (q->memory == V4L2_MEMORY_USERPTR) {
1269 mmap_sem = &current->mm->mmap_sem;
1270 call_qop(q, wait_prepare, q);
1271 down_read(mmap_sem);
1272 call_qop(q, wait_finish, q);
1273 }
1274
1275 if (q->fileio) {
1276 dprintk(1, "%s(): file io in progress\n", opname);
1277 ret = -EBUSY;
1278 goto unlock;
1279 }
1280
1281 if (b->type != q->type) {
1282 dprintk(1, "%s(): invalid buffer type\n", opname);
1283 ret = -EINVAL;
1284 goto unlock;
1285 }
1286
1287 if (b->index >= q->num_buffers) {
1288 dprintk(1, "%s(): buffer index out of range\n", opname);
1289 ret = -EINVAL;
1290 goto unlock;
1291 }
1292
1293 vb = q->bufs[b->index];
1294 if (NULL == vb) {
1295 /* Should never happen */
1296 dprintk(1, "%s(): buffer is NULL\n", opname);
1297 ret = -EINVAL;
1298 goto unlock;
1299 }
1300
1301 if (b->memory != q->memory) {
1302 dprintk(1, "%s(): invalid memory type\n", opname);
1303 ret = -EINVAL;
1304 goto unlock;
1305 }
1306
1307 ret = __verify_planes_array(vb, b);
1308 if (ret)
1309 goto unlock;
1310
1311 ret = handler(q, b, vb);
1312 if (ret)
1313 goto unlock;
1314
1315 /* Fill buffer information for the userspace */
1316 __fill_v4l2_buffer(vb, b);
1317
1318 dprintk(1, "%s() of buffer %d succeeded\n", opname, vb->v4l2_buf.index);
1319unlock:
1320 if (mmap_sem)
1321 up_read(mmap_sem);
1322 return ret;
1323}
1324
1325static int __vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
1326 struct vb2_buffer *vb)
1327{
1328 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1329 dprintk(1, "%s(): invalid buffer state %d\n", __func__,
1330 vb->state);
1331 return -EINVAL;
1332 }
1333
1334 return __buf_prepare(vb, b);
1335}
1336
Pawel Osciake23ccc02010-10-11 10:56:41 -03001337/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001338 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1339 * @q: videobuf2 queue
1340 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1341 * handler in driver
1342 *
1343 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1344 * This function:
1345 * 1) verifies the passed buffer,
1346 * 2) calls buf_prepare callback in the driver (if provided), in which
1347 * driver-specific buffer initialization can be performed,
1348 *
1349 * The return values from this function are intended to be directly returned
1350 * from vidioc_prepare_buf handler in driver.
1351 */
1352int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1353{
Laurent Pinchart012043b2013-08-09 08:11:26 -03001354 return vb2_queue_or_prepare_buf(q, b, "prepare_buf", __vb2_prepare_buf);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001355}
1356EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1357
Laurent Pinchart012043b2013-08-09 08:11:26 -03001358static int __vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b,
1359 struct vb2_buffer *vb)
1360{
1361 int ret;
1362
1363 switch (vb->state) {
1364 case VB2_BUF_STATE_DEQUEUED:
1365 ret = __buf_prepare(vb, b);
1366 if (ret)
1367 return ret;
1368 case VB2_BUF_STATE_PREPARED:
1369 break;
1370 default:
1371 dprintk(1, "qbuf: buffer already in use\n");
1372 return -EINVAL;
1373 }
1374
1375 /*
1376 * Add to the queued buffers list, a buffer will stay on it until
1377 * dequeued in dqbuf.
1378 */
1379 list_add_tail(&vb->queued_entry, &q->queued_list);
1380 vb->state = VB2_BUF_STATE_QUEUED;
1381
1382 /*
1383 * If already streaming, give the buffer to driver for processing.
1384 * If not, the buffer will be given to driver on next streamon.
1385 */
1386 if (q->streaming)
1387 __enqueue_in_driver(vb);
1388
1389 return 0;
1390}
1391
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001392/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001393 * vb2_qbuf() - Queue a buffer from userspace
1394 * @q: videobuf2 queue
1395 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1396 * in driver
1397 *
1398 * Should be called from vidioc_qbuf ioctl handler of a driver.
1399 * This function:
1400 * 1) verifies the passed buffer,
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001401 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1402 * which driver-specific buffer initialization can be performed,
Pawel Osciake23ccc02010-10-11 10:56:41 -03001403 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1404 * callback for processing.
1405 *
1406 * The return values from this function are intended to be directly returned
1407 * from vidioc_qbuf handler in driver.
1408 */
1409int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1410{
Laurent Pinchart012043b2013-08-09 08:11:26 -03001411 return vb2_queue_or_prepare_buf(q, b, "qbuf", __vb2_qbuf);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001412}
1413EXPORT_SYMBOL_GPL(vb2_qbuf);
1414
1415/**
1416 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1417 * for dequeuing
1418 *
1419 * Will sleep if required for nonblocking == false.
1420 */
1421static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1422{
1423 /*
1424 * All operations on vb_done_list are performed under done_lock
1425 * spinlock protection. However, buffers may be removed from
1426 * it and returned to userspace only while holding both driver's
1427 * lock and the done_lock spinlock. Thus we can be sure that as
1428 * long as we hold the driver's lock, the list will remain not
1429 * empty if list_empty() check succeeds.
1430 */
1431
1432 for (;;) {
1433 int ret;
1434
1435 if (!q->streaming) {
1436 dprintk(1, "Streaming off, will not wait for buffers\n");
1437 return -EINVAL;
1438 }
1439
1440 if (!list_empty(&q->done_list)) {
1441 /*
1442 * Found a buffer that we were waiting for.
1443 */
1444 break;
1445 }
1446
1447 if (nonblocking) {
1448 dprintk(1, "Nonblocking and no buffers to dequeue, "
1449 "will not wait\n");
1450 return -EAGAIN;
1451 }
1452
1453 /*
1454 * We are streaming and blocking, wait for another buffer to
1455 * become ready or for streamoff. Driver's lock is released to
1456 * allow streamoff or qbuf to be called while waiting.
1457 */
1458 call_qop(q, wait_prepare, q);
1459
1460 /*
1461 * All locks have been released, it is safe to sleep now.
1462 */
1463 dprintk(3, "Will sleep waiting for buffers\n");
1464 ret = wait_event_interruptible(q->done_wq,
1465 !list_empty(&q->done_list) || !q->streaming);
1466
1467 /*
1468 * We need to reevaluate both conditions again after reacquiring
1469 * the locks or return an error if one occurred.
1470 */
1471 call_qop(q, wait_finish, q);
Hans Verkuil32a77262012-09-28 06:12:53 -03001472 if (ret) {
1473 dprintk(1, "Sleep was interrupted\n");
Pawel Osciake23ccc02010-10-11 10:56:41 -03001474 return ret;
Hans Verkuil32a77262012-09-28 06:12:53 -03001475 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001476 }
1477 return 0;
1478}
1479
1480/**
1481 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1482 *
1483 * Will sleep if required for nonblocking == false.
1484 */
1485static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
Hans Verkuil32a77262012-09-28 06:12:53 -03001486 struct v4l2_buffer *b, int nonblocking)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001487{
1488 unsigned long flags;
1489 int ret;
1490
1491 /*
1492 * Wait for at least one buffer to become available on the done_list.
1493 */
1494 ret = __vb2_wait_for_done_vb(q, nonblocking);
1495 if (ret)
1496 return ret;
1497
1498 /*
1499 * Driver's lock has been held since we last verified that done_list
1500 * is not empty, so no need for another list_empty(done_list) check.
1501 */
1502 spin_lock_irqsave(&q->done_lock, flags);
1503 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
Hans Verkuil32a77262012-09-28 06:12:53 -03001504 /*
1505 * Only remove the buffer from done_list if v4l2_buffer can handle all
1506 * the planes.
1507 */
1508 ret = __verify_planes_array(*vb, b);
1509 if (!ret)
1510 list_del(&(*vb)->done_entry);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001511 spin_unlock_irqrestore(&q->done_lock, flags);
1512
Hans Verkuil32a77262012-09-28 06:12:53 -03001513 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001514}
1515
1516/**
1517 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1518 * @q: videobuf2 queue
1519 *
1520 * This function will wait until all buffers that have been given to the driver
1521 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1522 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1523 * taken, for example from stop_streaming() callback.
1524 */
1525int vb2_wait_for_all_buffers(struct vb2_queue *q)
1526{
1527 if (!q->streaming) {
1528 dprintk(1, "Streaming off, will not wait for buffers\n");
1529 return -EINVAL;
1530 }
1531
1532 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1533 return 0;
1534}
1535EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1536
1537/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001538 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1539 */
1540static void __vb2_dqbuf(struct vb2_buffer *vb)
1541{
1542 struct vb2_queue *q = vb->vb2_queue;
1543 unsigned int i;
1544
1545 /* nothing to do if the buffer is already dequeued */
1546 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1547 return;
1548
1549 vb->state = VB2_BUF_STATE_DEQUEUED;
1550
1551 /* unmap DMABUF buffer */
1552 if (q->memory == V4L2_MEMORY_DMABUF)
1553 for (i = 0; i < vb->num_planes; ++i) {
1554 if (!vb->planes[i].dbuf_mapped)
1555 continue;
1556 call_memop(q, unmap_dmabuf, vb->planes[i].mem_priv);
1557 vb->planes[i].dbuf_mapped = 0;
1558 }
1559}
1560
1561/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001562 * vb2_dqbuf() - Dequeue a buffer to the userspace
1563 * @q: videobuf2 queue
1564 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1565 * in driver
1566 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1567 * buffers ready for dequeuing are present. Normally the driver
1568 * would be passing (file->f_flags & O_NONBLOCK) here
1569 *
1570 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1571 * This function:
1572 * 1) verifies the passed buffer,
1573 * 2) calls buf_finish callback in the driver (if provided), in which
1574 * driver can perform any additional operations that may be required before
1575 * returning the buffer to userspace, such as cache sync,
1576 * 3) the buffer struct members are filled with relevant information for
1577 * the userspace.
1578 *
1579 * The return values from this function are intended to be directly returned
1580 * from vidioc_dqbuf handler in driver.
1581 */
1582int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1583{
1584 struct vb2_buffer *vb = NULL;
1585 int ret;
1586
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001587 if (q->fileio) {
1588 dprintk(1, "dqbuf: file io in progress\n");
1589 return -EBUSY;
1590 }
1591
Pawel Osciake23ccc02010-10-11 10:56:41 -03001592 if (b->type != q->type) {
1593 dprintk(1, "dqbuf: invalid buffer type\n");
1594 return -EINVAL;
1595 }
Hans Verkuil32a77262012-09-28 06:12:53 -03001596 ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
1597 if (ret < 0)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001598 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001599
1600 ret = call_qop(q, buf_finish, vb);
1601 if (ret) {
1602 dprintk(1, "dqbuf: buffer finish failed\n");
1603 return ret;
1604 }
1605
1606 switch (vb->state) {
1607 case VB2_BUF_STATE_DONE:
1608 dprintk(3, "dqbuf: Returning done buffer\n");
1609 break;
1610 case VB2_BUF_STATE_ERROR:
1611 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1612 break;
1613 default:
1614 dprintk(1, "dqbuf: Invalid buffer state\n");
1615 return -EINVAL;
1616 }
1617
1618 /* Fill buffer information for the userspace */
1619 __fill_v4l2_buffer(vb, b);
1620 /* Remove from videobuf queue */
1621 list_del(&vb->queued_entry);
Sumit Semwalc5384042012-06-14 10:37:37 -03001622 /* go back to dequeued state */
1623 __vb2_dqbuf(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001624
1625 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1626 vb->v4l2_buf.index, vb->state);
1627
Pawel Osciake23ccc02010-10-11 10:56:41 -03001628 return 0;
1629}
1630EXPORT_SYMBOL_GPL(vb2_dqbuf);
1631
1632/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001633 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1634 *
1635 * Removes all queued buffers from driver's queue and all buffers queued by
1636 * userspace from videobuf's queue. Returns to state after reqbufs.
1637 */
1638static void __vb2_queue_cancel(struct vb2_queue *q)
1639{
1640 unsigned int i;
1641
1642 /*
1643 * Tell driver to stop all transactions and release all queued
1644 * buffers.
1645 */
1646 if (q->streaming)
1647 call_qop(q, stop_streaming, q);
1648 q->streaming = 0;
1649
1650 /*
1651 * Remove all buffers from videobuf's list...
1652 */
1653 INIT_LIST_HEAD(&q->queued_list);
1654 /*
1655 * ...and done list; userspace will not receive any buffers it
1656 * has not already dequeued before initiating cancel.
1657 */
1658 INIT_LIST_HEAD(&q->done_list);
Marek Szyprowskiafdea8b2011-06-10 08:58:42 -03001659 atomic_set(&q->queued_count, 0);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001660 wake_up_all(&q->done_wq);
1661
1662 /*
1663 * Reinitialize all buffers for next use.
1664 */
1665 for (i = 0; i < q->num_buffers; ++i)
Sumit Semwalc5384042012-06-14 10:37:37 -03001666 __vb2_dqbuf(q->bufs[i]);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001667}
1668
1669/**
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001670 * vb2_streamon - start streaming
1671 * @q: videobuf2 queue
1672 * @type: type argument passed from userspace to vidioc_streamon handler
1673 *
1674 * Should be called from vidioc_streamon handler of a driver.
1675 * This function:
1676 * 1) verifies current state
1677 * 2) passes any previously queued buffers to the driver and starts streaming
1678 *
1679 * The return values from this function are intended to be directly returned
1680 * from vidioc_streamon handler in the driver.
1681 */
1682int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1683{
1684 struct vb2_buffer *vb;
1685 int ret;
1686
1687 if (q->fileio) {
1688 dprintk(1, "streamon: file io in progress\n");
1689 return -EBUSY;
1690 }
1691
1692 if (type != q->type) {
1693 dprintk(1, "streamon: invalid stream type\n");
1694 return -EINVAL;
1695 }
1696
1697 if (q->streaming) {
1698 dprintk(1, "streamon: already streaming\n");
1699 return -EBUSY;
1700 }
1701
1702 /*
1703 * If any buffers were queued before streamon,
1704 * we can now pass them to driver for processing.
1705 */
1706 list_for_each_entry(vb, &q->queued_list, queued_entry)
1707 __enqueue_in_driver(vb);
1708
1709 /*
1710 * Let driver notice that streaming state has been enabled.
1711 */
1712 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1713 if (ret) {
1714 dprintk(1, "streamon: driver refused to start streaming\n");
1715 __vb2_queue_cancel(q);
1716 return ret;
1717 }
1718
1719 q->streaming = 1;
1720
1721 dprintk(3, "Streamon successful\n");
1722 return 0;
1723}
1724EXPORT_SYMBOL_GPL(vb2_streamon);
1725
1726
1727/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001728 * vb2_streamoff - stop streaming
1729 * @q: videobuf2 queue
1730 * @type: type argument passed from userspace to vidioc_streamoff handler
1731 *
1732 * Should be called from vidioc_streamoff handler of a driver.
1733 * This function:
1734 * 1) verifies current state,
1735 * 2) stop streaming and dequeues any queued buffers, including those previously
1736 * passed to the driver (after waiting for the driver to finish).
1737 *
1738 * This call can be used for pausing playback.
1739 * The return values from this function are intended to be directly returned
1740 * from vidioc_streamoff handler in the driver
1741 */
1742int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1743{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001744 if (q->fileio) {
1745 dprintk(1, "streamoff: file io in progress\n");
1746 return -EBUSY;
1747 }
1748
Pawel Osciake23ccc02010-10-11 10:56:41 -03001749 if (type != q->type) {
1750 dprintk(1, "streamoff: invalid stream type\n");
1751 return -EINVAL;
1752 }
1753
1754 if (!q->streaming) {
1755 dprintk(1, "streamoff: not streaming\n");
1756 return -EINVAL;
1757 }
1758
1759 /*
1760 * Cancel will pause streaming and remove all buffers from the driver
1761 * and videobuf, effectively returning control over them to userspace.
1762 */
1763 __vb2_queue_cancel(q);
1764
1765 dprintk(3, "Streamoff successful\n");
1766 return 0;
1767}
1768EXPORT_SYMBOL_GPL(vb2_streamoff);
1769
1770/**
1771 * __find_plane_by_offset() - find plane associated with the given offset off
1772 */
1773static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1774 unsigned int *_buffer, unsigned int *_plane)
1775{
1776 struct vb2_buffer *vb;
1777 unsigned int buffer, plane;
1778
1779 /*
1780 * Go over all buffers and their planes, comparing the given offset
1781 * with an offset assigned to each plane. If a match is found,
1782 * return its buffer and plane numbers.
1783 */
1784 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1785 vb = q->bufs[buffer];
1786
1787 for (plane = 0; plane < vb->num_planes; ++plane) {
1788 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1789 *_buffer = buffer;
1790 *_plane = plane;
1791 return 0;
1792 }
1793 }
1794 }
1795
1796 return -EINVAL;
1797}
1798
1799/**
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03001800 * vb2_expbuf() - Export a buffer as a file descriptor
1801 * @q: videobuf2 queue
1802 * @eb: export buffer structure passed from userspace to vidioc_expbuf
1803 * handler in driver
1804 *
1805 * The return values from this function are intended to be directly returned
1806 * from vidioc_expbuf handler in driver.
1807 */
1808int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
1809{
1810 struct vb2_buffer *vb = NULL;
1811 struct vb2_plane *vb_plane;
1812 int ret;
1813 struct dma_buf *dbuf;
1814
1815 if (q->memory != V4L2_MEMORY_MMAP) {
1816 dprintk(1, "Queue is not currently set up for mmap\n");
1817 return -EINVAL;
1818 }
1819
1820 if (!q->mem_ops->get_dmabuf) {
1821 dprintk(1, "Queue does not support DMA buffer exporting\n");
1822 return -EINVAL;
1823 }
1824
1825 if (eb->flags & ~O_CLOEXEC) {
1826 dprintk(1, "Queue does support only O_CLOEXEC flag\n");
1827 return -EINVAL;
1828 }
1829
1830 if (eb->type != q->type) {
1831 dprintk(1, "qbuf: invalid buffer type\n");
1832 return -EINVAL;
1833 }
1834
1835 if (eb->index >= q->num_buffers) {
1836 dprintk(1, "buffer index out of range\n");
1837 return -EINVAL;
1838 }
1839
1840 vb = q->bufs[eb->index];
1841
1842 if (eb->plane >= vb->num_planes) {
1843 dprintk(1, "buffer plane out of range\n");
1844 return -EINVAL;
1845 }
1846
1847 vb_plane = &vb->planes[eb->plane];
1848
1849 dbuf = call_memop(q, get_dmabuf, vb_plane->mem_priv);
1850 if (IS_ERR_OR_NULL(dbuf)) {
1851 dprintk(1, "Failed to export buffer %d, plane %d\n",
1852 eb->index, eb->plane);
1853 return -EINVAL;
1854 }
1855
1856 ret = dma_buf_fd(dbuf, eb->flags);
1857 if (ret < 0) {
1858 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
1859 eb->index, eb->plane, ret);
1860 dma_buf_put(dbuf);
1861 return ret;
1862 }
1863
1864 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
1865 eb->index, eb->plane, ret);
1866 eb->fd = ret;
1867
1868 return 0;
1869}
1870EXPORT_SYMBOL_GPL(vb2_expbuf);
1871
1872/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001873 * vb2_mmap() - map video buffers into application address space
1874 * @q: videobuf2 queue
1875 * @vma: vma passed to the mmap file operation handler in the driver
1876 *
1877 * Should be called from mmap file operation handler of a driver.
1878 * This function maps one plane of one of the available video buffers to
1879 * userspace. To map whole video memory allocated on reqbufs, this function
1880 * has to be called once per each plane per each buffer previously allocated.
1881 *
1882 * When the userspace application calls mmap, it passes to it an offset returned
1883 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1884 * a "cookie", which is then used to identify the plane to be mapped.
1885 * This function finds a plane with a matching offset and a mapping is performed
1886 * by the means of a provided memory operation.
1887 *
1888 * The return values from this function are intended to be directly returned
1889 * from the mmap handler in driver.
1890 */
1891int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1892{
1893 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001894 struct vb2_buffer *vb;
1895 unsigned int buffer, plane;
1896 int ret;
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -03001897 unsigned long length;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001898
1899 if (q->memory != V4L2_MEMORY_MMAP) {
1900 dprintk(1, "Queue is not currently set up for mmap\n");
1901 return -EINVAL;
1902 }
1903
1904 /*
1905 * Check memory area access mode.
1906 */
1907 if (!(vma->vm_flags & VM_SHARED)) {
1908 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1909 return -EINVAL;
1910 }
1911 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1912 if (!(vma->vm_flags & VM_WRITE)) {
1913 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1914 return -EINVAL;
1915 }
1916 } else {
1917 if (!(vma->vm_flags & VM_READ)) {
1918 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1919 return -EINVAL;
1920 }
1921 }
1922
1923 /*
1924 * Find the plane corresponding to the offset passed by userspace.
1925 */
1926 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1927 if (ret)
1928 return ret;
1929
1930 vb = q->bufs[buffer];
Pawel Osciake23ccc02010-10-11 10:56:41 -03001931
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -03001932 /*
1933 * MMAP requires page_aligned buffers.
1934 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
1935 * so, we need to do the same here.
1936 */
1937 length = PAGE_ALIGN(vb->v4l2_planes[plane].length);
1938 if (length < (vma->vm_end - vma->vm_start)) {
1939 dprintk(1,
1940 "MMAP invalid, as it would overflow buffer length\n");
Seung-Woo Kim068a0df2013-04-11 23:57:57 -03001941 return -EINVAL;
1942 }
1943
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001944 ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001945 if (ret)
1946 return ret;
1947
Pawel Osciake23ccc02010-10-11 10:56:41 -03001948 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1949 return 0;
1950}
1951EXPORT_SYMBOL_GPL(vb2_mmap);
1952
Scott Jiang6f524ec2011-09-21 09:25:23 -03001953#ifndef CONFIG_MMU
1954unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1955 unsigned long addr,
1956 unsigned long len,
1957 unsigned long pgoff,
1958 unsigned long flags)
1959{
1960 unsigned long off = pgoff << PAGE_SHIFT;
1961 struct vb2_buffer *vb;
1962 unsigned int buffer, plane;
1963 int ret;
1964
1965 if (q->memory != V4L2_MEMORY_MMAP) {
1966 dprintk(1, "Queue is not currently set up for mmap\n");
1967 return -EINVAL;
1968 }
1969
1970 /*
1971 * Find the plane corresponding to the offset passed by userspace.
1972 */
1973 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1974 if (ret)
1975 return ret;
1976
1977 vb = q->bufs[buffer];
1978
1979 return (unsigned long)vb2_plane_vaddr(vb, plane);
1980}
1981EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
1982#endif
1983
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001984static int __vb2_init_fileio(struct vb2_queue *q, int read);
1985static int __vb2_cleanup_fileio(struct vb2_queue *q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001986
1987/**
1988 * vb2_poll() - implements poll userspace operation
1989 * @q: videobuf2 queue
1990 * @file: file argument passed to the poll file operation handler
1991 * @wait: wait argument passed to the poll file operation handler
1992 *
1993 * This function implements poll file operation handler for a driver.
1994 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1995 * be informed that the file descriptor of a video device is available for
1996 * reading.
1997 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1998 * will be reported as available for writing.
1999 *
Hans Verkuil95213ce2011-07-13 04:26:52 -03002000 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
2001 * pending events.
2002 *
Pawel Osciake23ccc02010-10-11 10:56:41 -03002003 * The return values from this function are intended to be directly returned
2004 * from poll handler in driver.
2005 */
2006unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
2007{
Hans Verkuil95213ce2011-07-13 04:26:52 -03002008 struct video_device *vfd = video_devdata(file);
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002009 unsigned long req_events = poll_requested_events(wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002010 struct vb2_buffer *vb = NULL;
Hans Verkuil95213ce2011-07-13 04:26:52 -03002011 unsigned int res = 0;
2012 unsigned long flags;
2013
2014 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
2015 struct v4l2_fh *fh = file->private_data;
2016
2017 if (v4l2_event_pending(fh))
2018 res = POLLPRI;
2019 else if (req_events & POLLPRI)
2020 poll_wait(file, &fh->wait, wait);
2021 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03002022
Hans Verkuilcd138232013-01-30 13:29:02 -03002023 if (!V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLIN | POLLRDNORM)))
2024 return res;
2025 if (V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLOUT | POLLWRNORM)))
2026 return res;
2027
Pawel Osciake23ccc02010-10-11 10:56:41 -03002028 /*
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03002029 * Start file I/O emulator only if streaming API has not been used yet.
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002030 */
2031 if (q->num_buffers == 0 && q->fileio == NULL) {
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002032 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2033 (req_events & (POLLIN | POLLRDNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002034 if (__vb2_init_fileio(q, 1))
2035 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002036 }
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002037 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2038 (req_events & (POLLOUT | POLLWRNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002039 if (__vb2_init_fileio(q, 0))
2040 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002041 /*
2042 * Write to OUTPUT queue can be done immediately.
2043 */
Hans Verkuil95213ce2011-07-13 04:26:52 -03002044 return res | POLLOUT | POLLWRNORM;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002045 }
2046 }
2047
2048 /*
Pawel Osciake23ccc02010-10-11 10:56:41 -03002049 * There is nothing to wait for if no buffers have already been queued.
2050 */
2051 if (list_empty(&q->queued_list))
Hans Verkuil95213ce2011-07-13 04:26:52 -03002052 return res | POLLERR;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002053
Seung-Woo Kim412cb872013-05-20 23:47:29 -03002054 if (list_empty(&q->done_list))
2055 poll_wait(file, &q->done_wq, wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002056
2057 /*
2058 * Take first buffer available for dequeuing.
2059 */
2060 spin_lock_irqsave(&q->done_lock, flags);
2061 if (!list_empty(&q->done_list))
2062 vb = list_first_entry(&q->done_list, struct vb2_buffer,
2063 done_entry);
2064 spin_unlock_irqrestore(&q->done_lock, flags);
2065
2066 if (vb && (vb->state == VB2_BUF_STATE_DONE
2067 || vb->state == VB2_BUF_STATE_ERROR)) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002068 return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
2069 res | POLLOUT | POLLWRNORM :
2070 res | POLLIN | POLLRDNORM;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002071 }
Hans Verkuil95213ce2011-07-13 04:26:52 -03002072 return res;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002073}
2074EXPORT_SYMBOL_GPL(vb2_poll);
2075
2076/**
2077 * vb2_queue_init() - initialize a videobuf2 queue
2078 * @q: videobuf2 queue; this structure should be allocated in driver
2079 *
2080 * The vb2_queue structure should be allocated by the driver. The driver is
2081 * responsible of clearing it's content and setting initial values for some
2082 * required entries before calling this function.
2083 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2084 * to the struct vb2_queue description in include/media/videobuf2-core.h
2085 * for more information.
2086 */
2087int vb2_queue_init(struct vb2_queue *q)
2088{
Ezequiel Garcia896f38f2012-09-17 14:59:30 -03002089 /*
2090 * Sanity check
2091 */
2092 if (WARN_ON(!q) ||
2093 WARN_ON(!q->ops) ||
2094 WARN_ON(!q->mem_ops) ||
2095 WARN_ON(!q->type) ||
2096 WARN_ON(!q->io_modes) ||
2097 WARN_ON(!q->ops->queue_setup) ||
Kamil Debski6aa69f92013-01-25 06:29:57 -03002098 WARN_ON(!q->ops->buf_queue) ||
2099 WARN_ON(q->timestamp_type & ~V4L2_BUF_FLAG_TIMESTAMP_MASK))
Ezequiel Garcia896f38f2012-09-17 14:59:30 -03002100 return -EINVAL;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002101
Kamil Debski6aa69f92013-01-25 06:29:57 -03002102 /* Warn that the driver should choose an appropriate timestamp type */
2103 WARN_ON(q->timestamp_type == V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
2104
Pawel Osciake23ccc02010-10-11 10:56:41 -03002105 INIT_LIST_HEAD(&q->queued_list);
2106 INIT_LIST_HEAD(&q->done_list);
2107 spin_lock_init(&q->done_lock);
2108 init_waitqueue_head(&q->done_wq);
2109
2110 if (q->buf_struct_size == 0)
2111 q->buf_struct_size = sizeof(struct vb2_buffer);
2112
2113 return 0;
2114}
2115EXPORT_SYMBOL_GPL(vb2_queue_init);
2116
2117/**
2118 * vb2_queue_release() - stop streaming, release the queue and free memory
2119 * @q: videobuf2 queue
2120 *
2121 * This function stops streaming and performs necessary clean ups, including
2122 * freeing video buffer memory. The driver is responsible for freeing
2123 * the vb2_queue structure itself.
2124 */
2125void vb2_queue_release(struct vb2_queue *q)
2126{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002127 __vb2_cleanup_fileio(q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002128 __vb2_queue_cancel(q);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03002129 __vb2_queue_free(q, q->num_buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002130}
2131EXPORT_SYMBOL_GPL(vb2_queue_release);
2132
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002133/**
2134 * struct vb2_fileio_buf - buffer context used by file io emulator
2135 *
2136 * vb2 provides a compatibility layer and emulator of file io (read and
2137 * write) calls on top of streaming API. This structure is used for
2138 * tracking context related to the buffers.
2139 */
2140struct vb2_fileio_buf {
2141 void *vaddr;
2142 unsigned int size;
2143 unsigned int pos;
2144 unsigned int queued:1;
2145};
2146
2147/**
2148 * struct vb2_fileio_data - queue context used by file io emulator
2149 *
2150 * vb2 provides a compatibility layer and emulator of file io (read and
2151 * write) calls on top of streaming API. For proper operation it required
2152 * this structure to save the driver state between each call of the read
2153 * or write function.
2154 */
2155struct vb2_fileio_data {
2156 struct v4l2_requestbuffers req;
2157 struct v4l2_buffer b;
2158 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
2159 unsigned int index;
2160 unsigned int q_count;
2161 unsigned int dq_count;
2162 unsigned int flags;
2163};
2164
2165/**
2166 * __vb2_init_fileio() - initialize file io emulator
2167 * @q: videobuf2 queue
2168 * @read: mode selector (1 means read, 0 means write)
2169 */
2170static int __vb2_init_fileio(struct vb2_queue *q, int read)
2171{
2172 struct vb2_fileio_data *fileio;
2173 int i, ret;
2174 unsigned int count = 0;
2175
2176 /*
2177 * Sanity check
2178 */
2179 if ((read && !(q->io_modes & VB2_READ)) ||
2180 (!read && !(q->io_modes & VB2_WRITE)))
2181 BUG();
2182
2183 /*
2184 * Check if device supports mapping buffers to kernel virtual space.
2185 */
2186 if (!q->mem_ops->vaddr)
2187 return -EBUSY;
2188
2189 /*
2190 * Check if streaming api has not been already activated.
2191 */
2192 if (q->streaming || q->num_buffers > 0)
2193 return -EBUSY;
2194
2195 /*
2196 * Start with count 1, driver can increase it in queue_setup()
2197 */
2198 count = 1;
2199
2200 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
2201 (read) ? "read" : "write", count, q->io_flags);
2202
2203 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
2204 if (fileio == NULL)
2205 return -ENOMEM;
2206
2207 fileio->flags = q->io_flags;
2208
2209 /*
2210 * Request buffers and use MMAP type to force driver
2211 * to allocate buffers by itself.
2212 */
2213 fileio->req.count = count;
2214 fileio->req.memory = V4L2_MEMORY_MMAP;
2215 fileio->req.type = q->type;
2216 ret = vb2_reqbufs(q, &fileio->req);
2217 if (ret)
2218 goto err_kfree;
2219
2220 /*
2221 * Check if plane_count is correct
2222 * (multiplane buffers are not supported).
2223 */
2224 if (q->bufs[0]->num_planes != 1) {
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002225 ret = -EBUSY;
2226 goto err_reqbufs;
2227 }
2228
2229 /*
2230 * Get kernel address of each buffer.
2231 */
2232 for (i = 0; i < q->num_buffers; i++) {
2233 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
Wei Yongjun5dd69462013-05-13 01:48:45 -03002234 if (fileio->bufs[i].vaddr == NULL) {
2235 ret = -EINVAL;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002236 goto err_reqbufs;
Wei Yongjun5dd69462013-05-13 01:48:45 -03002237 }
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002238 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
2239 }
2240
2241 /*
2242 * Read mode requires pre queuing of all buffers.
2243 */
2244 if (read) {
2245 /*
2246 * Queue all buffers.
2247 */
2248 for (i = 0; i < q->num_buffers; i++) {
2249 struct v4l2_buffer *b = &fileio->b;
2250 memset(b, 0, sizeof(*b));
2251 b->type = q->type;
2252 b->memory = q->memory;
2253 b->index = i;
2254 ret = vb2_qbuf(q, b);
2255 if (ret)
2256 goto err_reqbufs;
2257 fileio->bufs[i].queued = 1;
2258 }
2259
2260 /*
2261 * Start streaming.
2262 */
2263 ret = vb2_streamon(q, q->type);
2264 if (ret)
2265 goto err_reqbufs;
2266 }
2267
2268 q->fileio = fileio;
2269
2270 return ret;
2271
2272err_reqbufs:
Hans de Goedea67e1722012-05-08 14:47:39 -03002273 fileio->req.count = 0;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002274 vb2_reqbufs(q, &fileio->req);
2275
2276err_kfree:
2277 kfree(fileio);
2278 return ret;
2279}
2280
2281/**
2282 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2283 * @q: videobuf2 queue
2284 */
2285static int __vb2_cleanup_fileio(struct vb2_queue *q)
2286{
2287 struct vb2_fileio_data *fileio = q->fileio;
2288
2289 if (fileio) {
2290 /*
2291 * Hack fileio context to enable direct calls to vb2 ioctl
2292 * interface.
2293 */
2294 q->fileio = NULL;
2295
2296 vb2_streamoff(q, q->type);
2297 fileio->req.count = 0;
2298 vb2_reqbufs(q, &fileio->req);
2299 kfree(fileio);
2300 dprintk(3, "file io emulator closed\n");
2301 }
2302 return 0;
2303}
2304
2305/**
2306 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2307 * @q: videobuf2 queue
2308 * @data: pointed to target userspace buffer
2309 * @count: number of bytes to read or write
2310 * @ppos: file handle position tracking pointer
2311 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2312 * @read: access mode selector (1 means read, 0 means write)
2313 */
2314static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
2315 loff_t *ppos, int nonblock, int read)
2316{
2317 struct vb2_fileio_data *fileio;
2318 struct vb2_fileio_buf *buf;
2319 int ret, index;
2320
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002321 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002322 read ? "read" : "write", (long)*ppos, count,
2323 nonblock ? "non" : "");
2324
2325 if (!data)
2326 return -EINVAL;
2327
2328 /*
2329 * Initialize emulator on first call.
2330 */
2331 if (!q->fileio) {
2332 ret = __vb2_init_fileio(q, read);
2333 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
2334 if (ret)
2335 return ret;
2336 }
2337 fileio = q->fileio;
2338
2339 /*
2340 * Hack fileio context to enable direct calls to vb2 ioctl interface.
2341 * The pointer will be restored before returning from this function.
2342 */
2343 q->fileio = NULL;
2344
2345 index = fileio->index;
2346 buf = &fileio->bufs[index];
2347
2348 /*
2349 * Check if we need to dequeue the buffer.
2350 */
2351 if (buf->queued) {
2352 struct vb2_buffer *vb;
2353
2354 /*
2355 * Call vb2_dqbuf to get buffer back.
2356 */
2357 memset(&fileio->b, 0, sizeof(fileio->b));
2358 fileio->b.type = q->type;
2359 fileio->b.memory = q->memory;
2360 fileio->b.index = index;
2361 ret = vb2_dqbuf(q, &fileio->b, nonblock);
2362 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2363 if (ret)
2364 goto end;
2365 fileio->dq_count += 1;
2366
2367 /*
2368 * Get number of bytes filled by the driver
2369 */
2370 vb = q->bufs[index];
2371 buf->size = vb2_get_plane_payload(vb, 0);
2372 buf->queued = 0;
2373 }
2374
2375 /*
2376 * Limit count on last few bytes of the buffer.
2377 */
2378 if (buf->pos + count > buf->size) {
2379 count = buf->size - buf->pos;
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002380 dprintk(5, "reducing read count: %zd\n", count);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002381 }
2382
2383 /*
2384 * Transfer data to userspace.
2385 */
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002386 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002387 count, index, buf->pos);
2388 if (read)
2389 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2390 else
2391 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2392 if (ret) {
2393 dprintk(3, "file io: error copying data\n");
2394 ret = -EFAULT;
2395 goto end;
2396 }
2397
2398 /*
2399 * Update counters.
2400 */
2401 buf->pos += count;
2402 *ppos += count;
2403
2404 /*
2405 * Queue next buffer if required.
2406 */
2407 if (buf->pos == buf->size ||
2408 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2409 /*
2410 * Check if this is the last buffer to read.
2411 */
2412 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2413 fileio->dq_count == 1) {
2414 dprintk(3, "file io: read limit reached\n");
2415 /*
2416 * Restore fileio pointer and release the context.
2417 */
2418 q->fileio = fileio;
2419 return __vb2_cleanup_fileio(q);
2420 }
2421
2422 /*
2423 * Call vb2_qbuf and give buffer to the driver.
2424 */
2425 memset(&fileio->b, 0, sizeof(fileio->b));
2426 fileio->b.type = q->type;
2427 fileio->b.memory = q->memory;
2428 fileio->b.index = index;
2429 fileio->b.bytesused = buf->pos;
2430 ret = vb2_qbuf(q, &fileio->b);
2431 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2432 if (ret)
2433 goto end;
2434
2435 /*
2436 * Buffer has been queued, update the status
2437 */
2438 buf->pos = 0;
2439 buf->queued = 1;
2440 buf->size = q->bufs[0]->v4l2_planes[0].length;
2441 fileio->q_count += 1;
2442
2443 /*
2444 * Switch to the next buffer
2445 */
2446 fileio->index = (index + 1) % q->num_buffers;
2447
2448 /*
2449 * Start streaming if required.
2450 */
2451 if (!read && !q->streaming) {
2452 ret = vb2_streamon(q, q->type);
2453 if (ret)
2454 goto end;
2455 }
2456 }
2457
2458 /*
2459 * Return proper number of bytes processed.
2460 */
2461 if (ret == 0)
2462 ret = count;
2463end:
2464 /*
2465 * Restore the fileio context and block vb2 ioctl interface.
2466 */
2467 q->fileio = fileio;
2468 return ret;
2469}
2470
2471size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2472 loff_t *ppos, int nonblocking)
2473{
2474 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2475}
2476EXPORT_SYMBOL_GPL(vb2_read);
2477
Ricardo Ribalda819585b2013-08-28 04:39:29 -03002478size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002479 loff_t *ppos, int nonblocking)
2480{
Ricardo Ribalda819585b2013-08-28 04:39:29 -03002481 return __vb2_perform_fileio(q, (char __user *) data, count,
2482 ppos, nonblocking, 0);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002483}
2484EXPORT_SYMBOL_GPL(vb2_write);
2485
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002486
2487/*
2488 * The following functions are not part of the vb2 core API, but are helper
2489 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2490 * and struct vb2_ops.
2491 * They contain boilerplate code that most if not all drivers have to do
2492 * and so they simplify the driver code.
2493 */
2494
2495/* The queue is busy if there is a owner and you are not that owner. */
2496static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
2497{
2498 return vdev->queue->owner && vdev->queue->owner != file->private_data;
2499}
2500
2501/* vb2 ioctl helpers */
2502
2503int vb2_ioctl_reqbufs(struct file *file, void *priv,
2504 struct v4l2_requestbuffers *p)
2505{
2506 struct video_device *vdev = video_devdata(file);
2507 int res = __verify_memory_type(vdev->queue, p->memory, p->type);
2508
2509 if (res)
2510 return res;
2511 if (vb2_queue_is_busy(vdev, file))
2512 return -EBUSY;
2513 res = __reqbufs(vdev->queue, p);
2514 /* If count == 0, then the owner has released all buffers and he
2515 is no longer owner of the queue. Otherwise we have a new owner. */
2516 if (res == 0)
2517 vdev->queue->owner = p->count ? file->private_data : NULL;
2518 return res;
2519}
2520EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
2521
2522int vb2_ioctl_create_bufs(struct file *file, void *priv,
2523 struct v4l2_create_buffers *p)
2524{
2525 struct video_device *vdev = video_devdata(file);
2526 int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
2527
2528 p->index = vdev->queue->num_buffers;
2529 /* If count == 0, then just check if memory and type are valid.
2530 Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2531 if (p->count == 0)
2532 return res != -EBUSY ? res : 0;
2533 if (res)
2534 return res;
2535 if (vb2_queue_is_busy(vdev, file))
2536 return -EBUSY;
2537 res = __create_bufs(vdev->queue, p);
2538 if (res == 0)
2539 vdev->queue->owner = file->private_data;
2540 return res;
2541}
2542EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
2543
2544int vb2_ioctl_prepare_buf(struct file *file, void *priv,
2545 struct v4l2_buffer *p)
2546{
2547 struct video_device *vdev = video_devdata(file);
2548
2549 if (vb2_queue_is_busy(vdev, file))
2550 return -EBUSY;
2551 return vb2_prepare_buf(vdev->queue, p);
2552}
2553EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
2554
2555int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
2556{
2557 struct video_device *vdev = video_devdata(file);
2558
2559 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2560 return vb2_querybuf(vdev->queue, p);
2561}
2562EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
2563
2564int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2565{
2566 struct video_device *vdev = video_devdata(file);
2567
2568 if (vb2_queue_is_busy(vdev, file))
2569 return -EBUSY;
2570 return vb2_qbuf(vdev->queue, p);
2571}
2572EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
2573
2574int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2575{
2576 struct video_device *vdev = video_devdata(file);
2577
2578 if (vb2_queue_is_busy(vdev, file))
2579 return -EBUSY;
2580 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
2581}
2582EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
2583
2584int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
2585{
2586 struct video_device *vdev = video_devdata(file);
2587
2588 if (vb2_queue_is_busy(vdev, file))
2589 return -EBUSY;
2590 return vb2_streamon(vdev->queue, i);
2591}
2592EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
2593
2594int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
2595{
2596 struct video_device *vdev = video_devdata(file);
2597
2598 if (vb2_queue_is_busy(vdev, file))
2599 return -EBUSY;
2600 return vb2_streamoff(vdev->queue, i);
2601}
2602EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
2603
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002604int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
2605{
2606 struct video_device *vdev = video_devdata(file);
2607
2608 if (vb2_queue_is_busy(vdev, file))
2609 return -EBUSY;
2610 return vb2_expbuf(vdev->queue, p);
2611}
2612EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
2613
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002614/* v4l2_file_operations helpers */
2615
2616int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
2617{
2618 struct video_device *vdev = video_devdata(file);
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002619 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2620 int err;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002621
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002622 if (lock && mutex_lock_interruptible(lock))
2623 return -ERESTARTSYS;
2624 err = vb2_mmap(vdev->queue, vma);
2625 if (lock)
2626 mutex_unlock(lock);
2627 return err;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002628}
2629EXPORT_SYMBOL_GPL(vb2_fop_mmap);
2630
2631int vb2_fop_release(struct file *file)
2632{
2633 struct video_device *vdev = video_devdata(file);
2634
2635 if (file->private_data == vdev->queue->owner) {
2636 vb2_queue_release(vdev->queue);
2637 vdev->queue->owner = NULL;
2638 }
2639 return v4l2_fh_release(file);
2640}
2641EXPORT_SYMBOL_GPL(vb2_fop_release);
2642
Ricardo Ribalda819585b2013-08-28 04:39:29 -03002643ssize_t vb2_fop_write(struct file *file, const char __user *buf,
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002644 size_t count, loff_t *ppos)
2645{
2646 struct video_device *vdev = video_devdata(file);
2647 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002648 int err = -EBUSY;
2649
Hans Verkuilcf533732012-07-31 04:02:25 -03002650 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002651 return -ERESTARTSYS;
2652 if (vb2_queue_is_busy(vdev, file))
2653 goto exit;
2654 err = vb2_write(vdev->queue, buf, count, ppos,
2655 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03002656 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002657 vdev->queue->owner = file->private_data;
2658exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03002659 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002660 mutex_unlock(lock);
2661 return err;
2662}
2663EXPORT_SYMBOL_GPL(vb2_fop_write);
2664
2665ssize_t vb2_fop_read(struct file *file, char __user *buf,
2666 size_t count, loff_t *ppos)
2667{
2668 struct video_device *vdev = video_devdata(file);
2669 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002670 int err = -EBUSY;
2671
Hans Verkuilcf533732012-07-31 04:02:25 -03002672 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002673 return -ERESTARTSYS;
2674 if (vb2_queue_is_busy(vdev, file))
2675 goto exit;
2676 err = vb2_read(vdev->queue, buf, count, ppos,
2677 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03002678 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002679 vdev->queue->owner = file->private_data;
2680exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03002681 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002682 mutex_unlock(lock);
2683 return err;
2684}
2685EXPORT_SYMBOL_GPL(vb2_fop_read);
2686
2687unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
2688{
2689 struct video_device *vdev = video_devdata(file);
2690 struct vb2_queue *q = vdev->queue;
2691 struct mutex *lock = q->lock ? q->lock : vdev->lock;
2692 unsigned long req_events = poll_requested_events(wait);
2693 unsigned res;
2694 void *fileio;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002695 bool must_lock = false;
2696
2697 /* Try to be smart: only lock if polling might start fileio,
2698 otherwise locking will only introduce unwanted delays. */
2699 if (q->num_buffers == 0 && q->fileio == NULL) {
2700 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2701 (req_events & (POLLIN | POLLRDNORM)))
2702 must_lock = true;
2703 else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2704 (req_events & (POLLOUT | POLLWRNORM)))
2705 must_lock = true;
2706 }
2707
2708 /* If locking is needed, but this helper doesn't know how, then you
2709 shouldn't be using this helper but you should write your own. */
Hans Verkuilcf533732012-07-31 04:02:25 -03002710 WARN_ON(must_lock && !lock);
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002711
Hans Verkuilcf533732012-07-31 04:02:25 -03002712 if (must_lock && lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002713 return POLLERR;
2714
2715 fileio = q->fileio;
2716
2717 res = vb2_poll(vdev->queue, file, wait);
2718
2719 /* If fileio was started, then we have a new queue owner. */
2720 if (must_lock && !fileio && q->fileio)
2721 q->owner = file->private_data;
Hans Verkuilcf533732012-07-31 04:02:25 -03002722 if (must_lock && lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002723 mutex_unlock(lock);
2724 return res;
2725}
2726EXPORT_SYMBOL_GPL(vb2_fop_poll);
2727
2728#ifndef CONFIG_MMU
2729unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
2730 unsigned long len, unsigned long pgoff, unsigned long flags)
2731{
2732 struct video_device *vdev = video_devdata(file);
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002733 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2734 int ret;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002735
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002736 if (lock && mutex_lock_interruptible(lock))
2737 return -ERESTARTSYS;
2738 ret = vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
2739 if (lock)
2740 mutex_unlock(lock);
2741 return ret;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002742}
2743EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
2744#endif
2745
2746/* vb2_ops helpers. Only use if vq->lock is non-NULL. */
2747
2748void vb2_ops_wait_prepare(struct vb2_queue *vq)
2749{
2750 mutex_unlock(vq->lock);
2751}
2752EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
2753
2754void vb2_ops_wait_finish(struct vb2_queue *vq)
2755{
2756 mutex_lock(vq->lock);
2757}
2758EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
2759
Pawel Osciake23ccc02010-10-11 10:56:41 -03002760MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
Pawel Osciak95072082011-03-13 15:23:32 -03002761MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
Pawel Osciake23ccc02010-10-11 10:56:41 -03002762MODULE_LICENSE("GPL");