blob: de0e87f0b2c3a833f113121709b9a496d29ef1f3 [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
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300244 __setup_offsets(q, buffer);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300245
246 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300247 buffer, num_planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300248
249 return buffer;
250}
251
252/**
253 * __vb2_free_mem() - release all video buffer memory for a given queue
254 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300255static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300256{
257 unsigned int buffer;
258 struct vb2_buffer *vb;
259
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300260 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
261 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300262 vb = q->bufs[buffer];
263 if (!vb)
264 continue;
265
266 /* Free MMAP buffers or release USERPTR buffers */
267 if (q->memory == V4L2_MEMORY_MMAP)
268 __vb2_buf_mem_free(vb);
Sumit Semwalc5384042012-06-14 10:37:37 -0300269 else if (q->memory == V4L2_MEMORY_DMABUF)
270 __vb2_buf_dmabuf_put(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300271 else
272 __vb2_buf_userptr_put(vb);
273 }
274}
275
276/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300277 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
278 * related information, if no buffers are left return the queue to an
279 * uninitialized state. Might be called even if the queue has already been freed.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300280 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300281static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300282{
283 unsigned int buffer;
284
285 /* Call driver-provided cleanup function for each buffer, if provided */
286 if (q->ops->buf_cleanup) {
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300287 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
288 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300289 if (NULL == q->bufs[buffer])
290 continue;
291 q->ops->buf_cleanup(q->bufs[buffer]);
292 }
293 }
294
295 /* Release video buffer memory */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300296 __vb2_free_mem(q, buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300297
298 /* Free videobuf buffers */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300299 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
300 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300301 kfree(q->bufs[buffer]);
302 q->bufs[buffer] = NULL;
303 }
304
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300305 q->num_buffers -= buffers;
306 if (!q->num_buffers)
307 q->memory = 0;
Marek Szyprowskibd50d992011-10-25 03:07:59 -0300308 INIT_LIST_HEAD(&q->queued_list);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300309}
310
311/**
312 * __verify_planes_array() - verify that the planes array passed in struct
313 * v4l2_buffer from userspace can be safely used
314 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300315static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300316{
Hans Verkuil32a77262012-09-28 06:12:53 -0300317 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
318 return 0;
319
Pawel Osciake23ccc02010-10-11 10:56:41 -0300320 /* Is memory for copying plane information present? */
321 if (NULL == b->m.planes) {
322 dprintk(1, "Multi-planar buffer passed but "
323 "planes array not provided\n");
324 return -EINVAL;
325 }
326
327 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
328 dprintk(1, "Incorrect planes array length, "
329 "expected %d, got %d\n", vb->num_planes, b->length);
330 return -EINVAL;
331 }
332
333 return 0;
334}
335
336/**
Laurent Pinchart8023ed02012-07-10 10:41:40 -0300337 * __verify_length() - Verify that the bytesused value for each plane fits in
338 * the plane length and that the data offset doesn't exceed the bytesused value.
339 */
340static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
341{
342 unsigned int length;
343 unsigned int plane;
344
345 if (!V4L2_TYPE_IS_OUTPUT(b->type))
346 return 0;
347
348 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
349 for (plane = 0; plane < vb->num_planes; ++plane) {
350 length = (b->memory == V4L2_MEMORY_USERPTR)
351 ? b->m.planes[plane].length
352 : vb->v4l2_planes[plane].length;
353
354 if (b->m.planes[plane].bytesused > length)
355 return -EINVAL;
Sylwester Nawrocki3c5c23c2013-08-26 11:47:09 -0300356
357 if (b->m.planes[plane].data_offset > 0 &&
358 b->m.planes[plane].data_offset >=
Laurent Pinchart8023ed02012-07-10 10:41:40 -0300359 b->m.planes[plane].bytesused)
360 return -EINVAL;
361 }
362 } else {
363 length = (b->memory == V4L2_MEMORY_USERPTR)
364 ? b->length : vb->v4l2_planes[0].length;
365
366 if (b->bytesused > length)
367 return -EINVAL;
368 }
369
370 return 0;
371}
372
373/**
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300374 * __buffer_in_use() - return true if the buffer is in use and
375 * the queue cannot be freed (by the means of REQBUFS(0)) call
376 */
377static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
378{
379 unsigned int plane;
380 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski2c2dd6ac2011-10-12 13:09:53 -0300381 void *mem_priv = vb->planes[plane].mem_priv;
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300382 /*
383 * If num_users() has not been provided, call_memop
384 * will return 0, apparently nobody cares about this
385 * case anyway. If num_users() returns more than 1,
386 * we are not the only user of the plane's memory.
387 */
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300388 if (mem_priv && call_memop(q, num_users, mem_priv) > 1)
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300389 return true;
390 }
391 return false;
392}
393
394/**
395 * __buffers_in_use() - return true if any buffers on the queue are in use and
396 * the queue cannot be freed (by the means of REQBUFS(0)) call
397 */
398static bool __buffers_in_use(struct vb2_queue *q)
399{
400 unsigned int buffer;
401 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
402 if (__buffer_in_use(q, q->bufs[buffer]))
403 return true;
404 }
405 return false;
406}
407
408/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300409 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
410 * returned to userspace
411 */
Hans Verkuil32a77262012-09-28 06:12:53 -0300412static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300413{
414 struct vb2_queue *q = vb->vb2_queue;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300415
Sakari Ailus2b719d72012-05-02 09:40:03 -0300416 /* Copy back data such as timestamp, flags, etc. */
Pawel Osciake23ccc02010-10-11 10:56:41 -0300417 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
Sakari Ailus2b719d72012-05-02 09:40:03 -0300418 b->reserved2 = vb->v4l2_buf.reserved2;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300419 b->reserved = vb->v4l2_buf.reserved;
420
421 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300422 /*
423 * Fill in plane-related data if userspace provided an array
Hans Verkuil32a77262012-09-28 06:12:53 -0300424 * for it. The caller has already verified memory and size.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300425 */
Hans Verkuil3c0b6062012-09-28 06:24:18 -0300426 b->length = vb->num_planes;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300427 memcpy(b->m.planes, vb->v4l2_planes,
428 b->length * sizeof(struct v4l2_plane));
429 } else {
430 /*
431 * We use length and offset in v4l2_planes array even for
432 * single-planar buffers, but userspace does not.
433 */
434 b->length = vb->v4l2_planes[0].length;
435 b->bytesused = vb->v4l2_planes[0].bytesused;
436 if (q->memory == V4L2_MEMORY_MMAP)
437 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
438 else if (q->memory == V4L2_MEMORY_USERPTR)
439 b->m.userptr = vb->v4l2_planes[0].m.userptr;
Sumit Semwalc5384042012-06-14 10:37:37 -0300440 else if (q->memory == V4L2_MEMORY_DMABUF)
441 b->m.fd = vb->v4l2_planes[0].m.fd;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300442 }
443
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300444 /*
445 * Clear any buffer state related flags.
446 */
Sakari Ailus1b18e7a2012-10-22 17:10:16 -0300447 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
Kamil Debski6aa69f92013-01-25 06:29:57 -0300448 b->flags |= q->timestamp_type;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300449
450 switch (vb->state) {
451 case VB2_BUF_STATE_QUEUED:
452 case VB2_BUF_STATE_ACTIVE:
453 b->flags |= V4L2_BUF_FLAG_QUEUED;
454 break;
455 case VB2_BUF_STATE_ERROR:
456 b->flags |= V4L2_BUF_FLAG_ERROR;
457 /* fall through */
458 case VB2_BUF_STATE_DONE:
459 b->flags |= V4L2_BUF_FLAG_DONE;
460 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300461 case VB2_BUF_STATE_PREPARED:
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300462 b->flags |= V4L2_BUF_FLAG_PREPARED;
463 break;
464 case VB2_BUF_STATE_DEQUEUED:
Pawel Osciake23ccc02010-10-11 10:56:41 -0300465 /* nothing */
466 break;
467 }
468
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300469 if (__buffer_in_use(q, vb))
Pawel Osciake23ccc02010-10-11 10:56:41 -0300470 b->flags |= V4L2_BUF_FLAG_MAPPED;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300471}
472
473/**
474 * vb2_querybuf() - query video buffer information
475 * @q: videobuf queue
476 * @b: buffer struct passed from userspace to vidioc_querybuf handler
477 * in driver
478 *
479 * Should be called from vidioc_querybuf ioctl handler in driver.
480 * This function will verify the passed v4l2_buffer structure and fill the
481 * relevant information for the userspace.
482 *
483 * The return values from this function are intended to be directly returned
484 * from vidioc_querybuf handler in driver.
485 */
486int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
487{
488 struct vb2_buffer *vb;
Hans Verkuil32a77262012-09-28 06:12:53 -0300489 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300490
491 if (b->type != q->type) {
492 dprintk(1, "querybuf: wrong buffer type\n");
493 return -EINVAL;
494 }
495
496 if (b->index >= q->num_buffers) {
497 dprintk(1, "querybuf: buffer index out of range\n");
498 return -EINVAL;
499 }
500 vb = q->bufs[b->index];
Hans Verkuil32a77262012-09-28 06:12:53 -0300501 ret = __verify_planes_array(vb, b);
502 if (!ret)
503 __fill_v4l2_buffer(vb, b);
504 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300505}
506EXPORT_SYMBOL(vb2_querybuf);
507
508/**
509 * __verify_userptr_ops() - verify that all memory operations required for
510 * USERPTR queue type have been provided
511 */
512static int __verify_userptr_ops(struct vb2_queue *q)
513{
514 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
515 !q->mem_ops->put_userptr)
516 return -EINVAL;
517
518 return 0;
519}
520
521/**
522 * __verify_mmap_ops() - verify that all memory operations required for
523 * MMAP queue type have been provided
524 */
525static int __verify_mmap_ops(struct vb2_queue *q)
526{
527 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
528 !q->mem_ops->put || !q->mem_ops->mmap)
529 return -EINVAL;
530
531 return 0;
532}
533
534/**
Sumit Semwalc5384042012-06-14 10:37:37 -0300535 * __verify_dmabuf_ops() - verify that all memory operations required for
536 * DMABUF queue type have been provided
537 */
538static int __verify_dmabuf_ops(struct vb2_queue *q)
539{
540 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
541 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
542 !q->mem_ops->unmap_dmabuf)
543 return -EINVAL;
544
545 return 0;
546}
547
548/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300549 * __verify_memory_type() - Check whether the memory type and buffer type
550 * passed to a buffer operation are compatible with the queue.
551 */
552static int __verify_memory_type(struct vb2_queue *q,
553 enum v4l2_memory memory, enum v4l2_buf_type type)
554{
Sumit Semwalc5384042012-06-14 10:37:37 -0300555 if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR &&
556 memory != V4L2_MEMORY_DMABUF) {
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300557 dprintk(1, "reqbufs: unsupported memory type\n");
558 return -EINVAL;
559 }
560
561 if (type != q->type) {
562 dprintk(1, "reqbufs: requested type is incorrect\n");
563 return -EINVAL;
564 }
565
566 /*
567 * Make sure all the required memory ops for given memory type
568 * are available.
569 */
570 if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
571 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
572 return -EINVAL;
573 }
574
575 if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
576 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
577 return -EINVAL;
578 }
579
Sumit Semwalc5384042012-06-14 10:37:37 -0300580 if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
581 dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
582 return -EINVAL;
583 }
584
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300585 /*
586 * Place the busy tests at the end: -EBUSY can be ignored when
587 * create_bufs is called with count == 0, but count == 0 should still
588 * do the memory and type validation.
589 */
590 if (q->fileio) {
591 dprintk(1, "reqbufs: file io in progress\n");
592 return -EBUSY;
593 }
594 return 0;
595}
596
597/**
598 * __reqbufs() - Initiate streaming
Pawel Osciake23ccc02010-10-11 10:56:41 -0300599 * @q: videobuf2 queue
600 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
601 *
602 * Should be called from vidioc_reqbufs ioctl handler of a driver.
603 * This function:
604 * 1) verifies streaming parameters passed from the userspace,
605 * 2) sets up the queue,
606 * 3) negotiates number of buffers and planes per buffer with the driver
607 * to be used during streaming,
608 * 4) allocates internal buffer structures (struct vb2_buffer), according to
609 * the agreed parameters,
610 * 5) for MMAP memory type, allocates actual video memory, using the
611 * memory handling/allocation routines provided during queue initialization
612 *
613 * If req->count is 0, all the memory will be freed instead.
614 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
615 * and the queue is not busy, memory will be reallocated.
616 *
617 * The return values from this function are intended to be directly returned
618 * from vidioc_reqbufs handler in driver.
619 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300620static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300621{
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300622 unsigned int num_buffers, allocated_buffers, num_planes = 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300623 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300624
625 if (q->streaming) {
626 dprintk(1, "reqbufs: streaming active\n");
627 return -EBUSY;
628 }
629
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300630 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300631 /*
632 * We already have buffers allocated, so first check if they
633 * are not in use and can be freed.
634 */
635 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
636 dprintk(1, "reqbufs: memory in use, cannot free\n");
637 return -EBUSY;
638 }
639
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300640 __vb2_queue_free(q, q->num_buffers);
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300641
642 /*
643 * In case of REQBUFS(0) return immediately without calling
644 * driver's queue_setup() callback and allocating resources.
645 */
646 if (req->count == 0)
647 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300648 }
649
650 /*
651 * Make sure the requested values and current defaults are sane.
652 */
653 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300654 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
Pawel Osciake23ccc02010-10-11 10:56:41 -0300655 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
Marek Szyprowski13b14092011-04-14 07:17:44 -0300656 q->memory = req->memory;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300657
658 /*
659 * Ask the driver how many buffers and planes per buffer it requires.
660 * Driver also sets the size and allocator context for each plane.
661 */
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300662 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300663 q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300664 if (ret)
665 return ret;
666
667 /* Finally, allocate buffers and video memory */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300668 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
Marek Szyprowski66072d42011-06-28 08:29:02 -0300669 if (ret == 0) {
670 dprintk(1, "Memory allocation failed\n");
671 return -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300672 }
673
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300674 allocated_buffers = ret;
675
Pawel Osciake23ccc02010-10-11 10:56:41 -0300676 /*
677 * Check if driver can handle the allocated number of buffers.
678 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300679 if (allocated_buffers < num_buffers) {
680 num_buffers = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300681
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300682 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
683 &num_planes, q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300684
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300685 if (!ret && allocated_buffers < num_buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300686 ret = -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300687
688 /*
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300689 * Either the driver has accepted a smaller number of buffers,
690 * or .queue_setup() returned an error
Pawel Osciake23ccc02010-10-11 10:56:41 -0300691 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300692 }
693
694 q->num_buffers = allocated_buffers;
695
696 if (ret < 0) {
697 __vb2_queue_free(q, allocated_buffers);
698 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300699 }
700
Pawel Osciake23ccc02010-10-11 10:56:41 -0300701 /*
702 * Return the number of successfully allocated buffers
703 * to the userspace.
704 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300705 req->count = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300706
707 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300708}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300709
710/**
711 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
712 * type values.
713 * @q: videobuf2 queue
714 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
715 */
716int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
717{
718 int ret = __verify_memory_type(q, req->memory, req->type);
719
720 return ret ? ret : __reqbufs(q, req);
721}
Pawel Osciake23ccc02010-10-11 10:56:41 -0300722EXPORT_SYMBOL_GPL(vb2_reqbufs);
723
724/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300725 * __create_bufs() - Allocate buffers and any required auxiliary structs
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300726 * @q: videobuf2 queue
727 * @create: creation parameters, passed from userspace to vidioc_create_bufs
728 * handler in driver
729 *
730 * Should be called from vidioc_create_bufs ioctl handler of a driver.
731 * This function:
732 * 1) verifies parameter sanity
733 * 2) calls the .queue_setup() queue operation
734 * 3) performs any necessary memory allocations
735 *
736 * The return values from this function are intended to be directly returned
737 * from vidioc_create_bufs handler in driver.
738 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300739static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300740{
741 unsigned int num_planes = 0, num_buffers, allocated_buffers;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300742 int ret;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300743
744 if (q->num_buffers == VIDEO_MAX_FRAME) {
745 dprintk(1, "%s(): maximum number of buffers already allocated\n",
746 __func__);
747 return -ENOBUFS;
748 }
749
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300750 if (!q->num_buffers) {
751 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
752 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
753 q->memory = create->memory;
754 }
755
756 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
757
758 /*
759 * Ask the driver, whether the requested number of buffers, planes per
760 * buffer and their sizes are acceptable
761 */
762 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
763 &num_planes, q->plane_sizes, q->alloc_ctx);
764 if (ret)
765 return ret;
766
767 /* Finally, allocate buffers and video memory */
768 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
769 num_planes);
Hans Verkuilf05393d22012-06-22 05:44:14 -0300770 if (ret == 0) {
771 dprintk(1, "Memory allocation failed\n");
772 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300773 }
774
775 allocated_buffers = ret;
776
777 /*
778 * Check if driver can handle the so far allocated number of buffers.
779 */
780 if (ret < num_buffers) {
781 num_buffers = ret;
782
783 /*
784 * q->num_buffers contains the total number of buffers, that the
785 * queue driver has set up
786 */
787 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
788 &num_planes, q->plane_sizes, q->alloc_ctx);
789
790 if (!ret && allocated_buffers < num_buffers)
791 ret = -ENOMEM;
792
793 /*
794 * Either the driver has accepted a smaller number of buffers,
795 * or .queue_setup() returned an error
796 */
797 }
798
799 q->num_buffers += allocated_buffers;
800
801 if (ret < 0) {
802 __vb2_queue_free(q, allocated_buffers);
Hans Verkuilf05393d22012-06-22 05:44:14 -0300803 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300804 }
805
806 /*
807 * Return the number of successfully allocated buffers
808 * to the userspace.
809 */
810 create->count = allocated_buffers;
811
812 return 0;
813}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300814
815/**
Nicolas THERY53aa3b12012-07-20 09:25:37 -0300816 * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
817 * memory and type values.
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300818 * @q: videobuf2 queue
819 * @create: creation parameters, passed from userspace to vidioc_create_bufs
820 * handler in driver
821 */
822int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
823{
824 int ret = __verify_memory_type(q, create->memory, create->format.type);
825
826 create->index = q->num_buffers;
Hans Verkuilf05393d22012-06-22 05:44:14 -0300827 if (create->count == 0)
828 return ret != -EBUSY ? ret : 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300829 return ret ? ret : __create_bufs(q, create);
830}
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300831EXPORT_SYMBOL_GPL(vb2_create_bufs);
832
833/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300834 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
835 * @vb: vb2_buffer to which the plane in question belongs to
836 * @plane_no: plane number for which the address is to be returned
837 *
838 * This function returns a kernel virtual address of a given plane if
839 * such a mapping exist, NULL otherwise.
840 */
841void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
842{
843 struct vb2_queue *q = vb->vb2_queue;
844
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300845 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300846 return NULL;
847
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300848 return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300849
850}
851EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
852
853/**
854 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
855 * @vb: vb2_buffer to which the plane in question belongs to
856 * @plane_no: plane number for which the cookie is to be returned
857 *
858 * This function returns an allocator specific cookie for a given plane if
859 * available, NULL otherwise. The allocator should provide some simple static
860 * inline function, which would convert this cookie to the allocator specific
861 * type that can be used directly by the driver to access the buffer. This can
862 * be for example physical address, pointer to scatter list or IOMMU mapping.
863 */
864void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
865{
866 struct vb2_queue *q = vb->vb2_queue;
867
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300868 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300869 return NULL;
870
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300871 return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300872}
873EXPORT_SYMBOL_GPL(vb2_plane_cookie);
874
875/**
876 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
877 * @vb: vb2_buffer returned from the driver
878 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
879 * or VB2_BUF_STATE_ERROR if the operation finished with an error
880 *
881 * This function should be called by the driver after a hardware operation on
882 * a buffer is finished and the buffer may be returned to userspace. The driver
883 * cannot use this buffer anymore until it is queued back to it by videobuf
884 * by the means of buf_queue callback. Only buffers previously queued to the
885 * driver by buf_queue can be passed to this function.
886 */
887void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
888{
889 struct vb2_queue *q = vb->vb2_queue;
890 unsigned long flags;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -0300891 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300892
893 if (vb->state != VB2_BUF_STATE_ACTIVE)
894 return;
895
896 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
897 return;
898
899 dprintk(4, "Done processing on buffer %d, state: %d\n",
Tushar Behera9b6f5dc2012-11-12 04:01:29 -0300900 vb->v4l2_buf.index, state);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300901
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -0300902 /* sync buffers */
903 for (plane = 0; plane < vb->num_planes; ++plane)
904 call_memop(q, finish, vb->planes[plane].mem_priv);
905
Pawel Osciake23ccc02010-10-11 10:56:41 -0300906 /* Add the buffer to the done buffers list */
907 spin_lock_irqsave(&q->done_lock, flags);
908 vb->state = state;
909 list_add_tail(&vb->done_entry, &q->done_list);
910 atomic_dec(&q->queued_count);
911 spin_unlock_irqrestore(&q->done_lock, flags);
912
913 /* Inform any processes that may be waiting for buffers */
914 wake_up(&q->done_wq);
915}
916EXPORT_SYMBOL_GPL(vb2_buffer_done);
917
918/**
Hans Verkuil32a77262012-09-28 06:12:53 -0300919 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
920 * v4l2_buffer by the userspace. The caller has already verified that struct
921 * v4l2_buffer has a valid number of planes.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300922 */
Hans Verkuil32a77262012-09-28 06:12:53 -0300923static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
Pawel Osciake23ccc02010-10-11 10:56:41 -0300924 struct v4l2_plane *v4l2_planes)
925{
926 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300927
928 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300929 /* Fill in driver-provided information for OUTPUT types */
930 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
931 /*
932 * Will have to go up to b->length when API starts
933 * accepting variable number of planes.
934 */
935 for (plane = 0; plane < vb->num_planes; ++plane) {
936 v4l2_planes[plane].bytesused =
937 b->m.planes[plane].bytesused;
938 v4l2_planes[plane].data_offset =
939 b->m.planes[plane].data_offset;
940 }
941 }
942
943 if (b->memory == V4L2_MEMORY_USERPTR) {
944 for (plane = 0; plane < vb->num_planes; ++plane) {
945 v4l2_planes[plane].m.userptr =
946 b->m.planes[plane].m.userptr;
947 v4l2_planes[plane].length =
948 b->m.planes[plane].length;
949 }
950 }
Sumit Semwalc5384042012-06-14 10:37:37 -0300951 if (b->memory == V4L2_MEMORY_DMABUF) {
952 for (plane = 0; plane < vb->num_planes; ++plane) {
953 v4l2_planes[plane].m.fd =
954 b->m.planes[plane].m.fd;
955 v4l2_planes[plane].length =
956 b->m.planes[plane].length;
957 v4l2_planes[plane].data_offset =
958 b->m.planes[plane].data_offset;
959 }
960 }
Pawel Osciake23ccc02010-10-11 10:56:41 -0300961 } else {
962 /*
963 * Single-planar buffers do not use planes array,
964 * so fill in relevant v4l2_buffer struct fields instead.
965 * In videobuf we use our internal V4l2_planes struct for
966 * single-planar buffers as well, for simplicity.
967 */
Laurent Pinchartac706bf2012-12-17 07:38:16 -0300968 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300969 v4l2_planes[0].bytesused = b->bytesused;
Laurent Pinchartac706bf2012-12-17 07:38:16 -0300970 v4l2_planes[0].data_offset = 0;
971 }
Pawel Osciake23ccc02010-10-11 10:56:41 -0300972
973 if (b->memory == V4L2_MEMORY_USERPTR) {
974 v4l2_planes[0].m.userptr = b->m.userptr;
975 v4l2_planes[0].length = b->length;
976 }
Sumit Semwalc5384042012-06-14 10:37:37 -0300977
978 if (b->memory == V4L2_MEMORY_DMABUF) {
979 v4l2_planes[0].m.fd = b->m.fd;
980 v4l2_planes[0].length = b->length;
981 v4l2_planes[0].data_offset = 0;
982 }
983
Pawel Osciake23ccc02010-10-11 10:56:41 -0300984 }
985
986 vb->v4l2_buf.field = b->field;
987 vb->v4l2_buf.timestamp = b->timestamp;
Sakari Ailus1b18e7a2012-10-22 17:10:16 -0300988 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300989}
990
991/**
992 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
993 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300994static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300995{
996 struct v4l2_plane planes[VIDEO_MAX_PLANES];
997 struct vb2_queue *q = vb->vb2_queue;
998 void *mem_priv;
999 unsigned int plane;
1000 int ret;
1001 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1002
Hans Verkuil32a77262012-09-28 06:12:53 -03001003 /* Copy relevant information provided by the userspace */
1004 __fill_vb2_buffer(vb, b, planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001005
1006 for (plane = 0; plane < vb->num_planes; ++plane) {
1007 /* Skip the plane if already verified */
Marek Szyprowskif0b7c7f2011-11-16 15:09:40 -03001008 if (vb->v4l2_planes[plane].m.userptr &&
1009 vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
Pawel Osciake23ccc02010-10-11 10:56:41 -03001010 && vb->v4l2_planes[plane].length == planes[plane].length)
1011 continue;
1012
1013 dprintk(3, "qbuf: userspace address for plane %d changed, "
1014 "reacquiring memory\n", plane);
1015
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001016 /* Check if the provided plane buffer is large enough */
1017 if (planes[plane].length < q->plane_sizes[plane]) {
Marek Szyprowski4c2625d2011-10-03 03:21:45 -03001018 ret = -EINVAL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001019 goto err;
1020 }
1021
Pawel Osciake23ccc02010-10-11 10:56:41 -03001022 /* Release previously acquired memory if present */
1023 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -03001024 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001025
1026 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001027 vb->v4l2_planes[plane].m.userptr = 0;
1028 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001029
1030 /* Acquire each plane's memory */
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001031 mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
1032 planes[plane].m.userptr,
1033 planes[plane].length, write);
1034 if (IS_ERR_OR_NULL(mem_priv)) {
1035 dprintk(1, "qbuf: failed acquiring userspace "
Pawel Osciake23ccc02010-10-11 10:56:41 -03001036 "memory for plane %d\n", plane);
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001037 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
1038 goto err;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001039 }
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001040 vb->planes[plane].mem_priv = mem_priv;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001041 }
1042
1043 /*
1044 * Call driver-specific initialization on the newly acquired buffer,
1045 * if provided.
1046 */
1047 ret = call_qop(q, buf_init, vb);
1048 if (ret) {
1049 dprintk(1, "qbuf: buffer initialization failed\n");
1050 goto err;
1051 }
1052
1053 /*
1054 * Now that everything is in order, copy relevant information
1055 * provided by userspace.
1056 */
1057 for (plane = 0; plane < vb->num_planes; ++plane)
1058 vb->v4l2_planes[plane] = planes[plane];
1059
1060 return 0;
1061err:
1062 /* In case of errors, release planes that were already acquired */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001063 for (plane = 0; plane < vb->num_planes; ++plane) {
1064 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -03001065 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001066 vb->planes[plane].mem_priv = NULL;
1067 vb->v4l2_planes[plane].m.userptr = 0;
1068 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001069 }
1070
1071 return ret;
1072}
1073
1074/**
1075 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1076 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001077static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001078{
Hans Verkuil32a77262012-09-28 06:12:53 -03001079 __fill_vb2_buffer(vb, b, vb->v4l2_planes);
1080 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001081}
1082
1083/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001084 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1085 */
1086static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1087{
1088 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1089 struct vb2_queue *q = vb->vb2_queue;
1090 void *mem_priv;
1091 unsigned int plane;
1092 int ret;
1093 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1094
1095 /* Verify and copy relevant information provided by the userspace */
1096 __fill_vb2_buffer(vb, b, planes);
1097
1098 for (plane = 0; plane < vb->num_planes; ++plane) {
1099 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1100
1101 if (IS_ERR_OR_NULL(dbuf)) {
1102 dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
1103 plane);
1104 ret = -EINVAL;
1105 goto err;
1106 }
1107
1108 /* use DMABUF size if length is not provided */
1109 if (planes[plane].length == 0)
1110 planes[plane].length = dbuf->size;
1111
1112 if (planes[plane].length < planes[plane].data_offset +
1113 q->plane_sizes[plane]) {
1114 ret = -EINVAL;
1115 goto err;
1116 }
1117
1118 /* Skip the plane if already verified */
1119 if (dbuf == vb->planes[plane].dbuf &&
1120 vb->v4l2_planes[plane].length == planes[plane].length) {
1121 dma_buf_put(dbuf);
1122 continue;
1123 }
1124
1125 dprintk(1, "qbuf: buffer for plane %d changed\n", plane);
1126
1127 /* Release previously acquired memory if present */
1128 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
1129 memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
1130
1131 /* Acquire each plane's memory */
1132 mem_priv = call_memop(q, attach_dmabuf, q->alloc_ctx[plane],
1133 dbuf, planes[plane].length, write);
1134 if (IS_ERR(mem_priv)) {
1135 dprintk(1, "qbuf: failed to attach dmabuf\n");
1136 ret = PTR_ERR(mem_priv);
1137 dma_buf_put(dbuf);
1138 goto err;
1139 }
1140
1141 vb->planes[plane].dbuf = dbuf;
1142 vb->planes[plane].mem_priv = mem_priv;
1143 }
1144
1145 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1146 * really we want to do this just before the DMA, not while queueing
1147 * the buffer(s)..
1148 */
1149 for (plane = 0; plane < vb->num_planes; ++plane) {
1150 ret = call_memop(q, map_dmabuf, vb->planes[plane].mem_priv);
1151 if (ret) {
1152 dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
1153 plane);
1154 goto err;
1155 }
1156 vb->planes[plane].dbuf_mapped = 1;
1157 }
1158
1159 /*
1160 * Call driver-specific initialization on the newly acquired buffer,
1161 * if provided.
1162 */
1163 ret = call_qop(q, buf_init, vb);
1164 if (ret) {
1165 dprintk(1, "qbuf: buffer initialization failed\n");
1166 goto err;
1167 }
1168
1169 /*
1170 * Now that everything is in order, copy relevant information
1171 * provided by userspace.
1172 */
1173 for (plane = 0; plane < vb->num_planes; ++plane)
1174 vb->v4l2_planes[plane] = planes[plane];
1175
1176 return 0;
1177err:
1178 /* In case of errors, release planes that were already acquired */
1179 __vb2_buf_dmabuf_put(vb);
1180
1181 return ret;
1182}
1183
1184/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001185 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1186 */
1187static void __enqueue_in_driver(struct vb2_buffer *vb)
1188{
1189 struct vb2_queue *q = vb->vb2_queue;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001190 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001191
1192 vb->state = VB2_BUF_STATE_ACTIVE;
1193 atomic_inc(&q->queued_count);
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001194
1195 /* sync buffers */
1196 for (plane = 0; plane < vb->num_planes; ++plane)
1197 call_memop(q, prepare, vb->planes[plane].mem_priv);
1198
Pawel Osciake23ccc02010-10-11 10:56:41 -03001199 q->ops->buf_queue(vb);
1200}
1201
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001202static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001203{
1204 struct vb2_queue *q = vb->vb2_queue;
1205 int ret;
1206
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001207 ret = __verify_length(vb, b);
1208 if (ret < 0)
1209 return ret;
1210
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001211 switch (q->memory) {
1212 case V4L2_MEMORY_MMAP:
1213 ret = __qbuf_mmap(vb, b);
1214 break;
1215 case V4L2_MEMORY_USERPTR:
1216 ret = __qbuf_userptr(vb, b);
1217 break;
Sumit Semwalc5384042012-06-14 10:37:37 -03001218 case V4L2_MEMORY_DMABUF:
1219 ret = __qbuf_dmabuf(vb, b);
1220 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001221 default:
1222 WARN(1, "Invalid queue type\n");
1223 ret = -EINVAL;
1224 }
1225
1226 if (!ret)
1227 ret = call_qop(q, buf_prepare, vb);
1228 if (ret)
1229 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
1230 else
1231 vb->state = VB2_BUF_STATE_PREPARED;
1232
1233 return ret;
1234}
1235
Laurent Pinchart012043b2013-08-09 08:11:26 -03001236static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
1237 const char *opname,
1238 int (*handler)(struct vb2_queue *,
1239 struct v4l2_buffer *,
1240 struct vb2_buffer *))
1241{
1242 struct rw_semaphore *mmap_sem = NULL;
1243 struct vb2_buffer *vb;
1244 int ret;
1245
1246 /*
1247 * In case of user pointer buffers vb2 allocators need to get direct
1248 * access to userspace pages. This requires getting the mmap semaphore
1249 * for read access in the current process structure. The same semaphore
1250 * is taken before calling mmap operation, while both qbuf/prepare_buf
1251 * and mmap are called by the driver or v4l2 core with the driver's lock
1252 * held. To avoid an AB-BA deadlock (mmap_sem then driver's lock in mmap
1253 * and driver's lock then mmap_sem in qbuf/prepare_buf) the videobuf2
1254 * core releases the driver's lock, takes mmap_sem and then takes the
1255 * driver's lock again.
1256 *
1257 * To avoid racing with other vb2 calls, which might be called after
1258 * releasing the driver's lock, this operation is performed at the
1259 * beginning of qbuf/prepare_buf processing. This way the queue status
1260 * is consistent after getting the driver's lock back.
1261 */
1262 if (q->memory == V4L2_MEMORY_USERPTR) {
1263 mmap_sem = &current->mm->mmap_sem;
1264 call_qop(q, wait_prepare, q);
1265 down_read(mmap_sem);
1266 call_qop(q, wait_finish, q);
1267 }
1268
1269 if (q->fileio) {
1270 dprintk(1, "%s(): file io in progress\n", opname);
1271 ret = -EBUSY;
1272 goto unlock;
1273 }
1274
1275 if (b->type != q->type) {
1276 dprintk(1, "%s(): invalid buffer type\n", opname);
1277 ret = -EINVAL;
1278 goto unlock;
1279 }
1280
1281 if (b->index >= q->num_buffers) {
1282 dprintk(1, "%s(): buffer index out of range\n", opname);
1283 ret = -EINVAL;
1284 goto unlock;
1285 }
1286
1287 vb = q->bufs[b->index];
1288 if (NULL == vb) {
1289 /* Should never happen */
1290 dprintk(1, "%s(): buffer is NULL\n", opname);
1291 ret = -EINVAL;
1292 goto unlock;
1293 }
1294
1295 if (b->memory != q->memory) {
1296 dprintk(1, "%s(): invalid memory type\n", opname);
1297 ret = -EINVAL;
1298 goto unlock;
1299 }
1300
1301 ret = __verify_planes_array(vb, b);
1302 if (ret)
1303 goto unlock;
1304
1305 ret = handler(q, b, vb);
1306 if (ret)
1307 goto unlock;
1308
1309 /* Fill buffer information for the userspace */
1310 __fill_v4l2_buffer(vb, b);
1311
1312 dprintk(1, "%s() of buffer %d succeeded\n", opname, vb->v4l2_buf.index);
1313unlock:
1314 if (mmap_sem)
1315 up_read(mmap_sem);
1316 return ret;
1317}
1318
1319static int __vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
1320 struct vb2_buffer *vb)
1321{
1322 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1323 dprintk(1, "%s(): invalid buffer state %d\n", __func__,
1324 vb->state);
1325 return -EINVAL;
1326 }
1327
1328 return __buf_prepare(vb, b);
1329}
1330
Pawel Osciake23ccc02010-10-11 10:56:41 -03001331/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001332 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1333 * @q: videobuf2 queue
1334 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1335 * handler in driver
1336 *
1337 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1338 * This function:
1339 * 1) verifies the passed buffer,
1340 * 2) calls buf_prepare callback in the driver (if provided), in which
1341 * driver-specific buffer initialization can be performed,
1342 *
1343 * The return values from this function are intended to be directly returned
1344 * from vidioc_prepare_buf handler in driver.
1345 */
1346int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1347{
Laurent Pinchart012043b2013-08-09 08:11:26 -03001348 return vb2_queue_or_prepare_buf(q, b, "prepare_buf", __vb2_prepare_buf);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001349}
1350EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1351
Laurent Pinchart012043b2013-08-09 08:11:26 -03001352static int __vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b,
1353 struct vb2_buffer *vb)
1354{
1355 int ret;
1356
1357 switch (vb->state) {
1358 case VB2_BUF_STATE_DEQUEUED:
1359 ret = __buf_prepare(vb, b);
1360 if (ret)
1361 return ret;
1362 case VB2_BUF_STATE_PREPARED:
1363 break;
1364 default:
1365 dprintk(1, "qbuf: buffer already in use\n");
1366 return -EINVAL;
1367 }
1368
1369 /*
1370 * Add to the queued buffers list, a buffer will stay on it until
1371 * dequeued in dqbuf.
1372 */
1373 list_add_tail(&vb->queued_entry, &q->queued_list);
1374 vb->state = VB2_BUF_STATE_QUEUED;
1375
1376 /*
1377 * If already streaming, give the buffer to driver for processing.
1378 * If not, the buffer will be given to driver on next streamon.
1379 */
1380 if (q->streaming)
1381 __enqueue_in_driver(vb);
1382
1383 return 0;
1384}
1385
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001386/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001387 * vb2_qbuf() - Queue a buffer from userspace
1388 * @q: videobuf2 queue
1389 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1390 * in driver
1391 *
1392 * Should be called from vidioc_qbuf ioctl handler of a driver.
1393 * This function:
1394 * 1) verifies the passed buffer,
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001395 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1396 * which driver-specific buffer initialization can be performed,
Pawel Osciake23ccc02010-10-11 10:56:41 -03001397 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1398 * callback for processing.
1399 *
1400 * The return values from this function are intended to be directly returned
1401 * from vidioc_qbuf handler in driver.
1402 */
1403int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1404{
Laurent Pinchart012043b2013-08-09 08:11:26 -03001405 return vb2_queue_or_prepare_buf(q, b, "qbuf", __vb2_qbuf);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001406}
1407EXPORT_SYMBOL_GPL(vb2_qbuf);
1408
1409/**
1410 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1411 * for dequeuing
1412 *
1413 * Will sleep if required for nonblocking == false.
1414 */
1415static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1416{
1417 /*
1418 * All operations on vb_done_list are performed under done_lock
1419 * spinlock protection. However, buffers may be removed from
1420 * it and returned to userspace only while holding both driver's
1421 * lock and the done_lock spinlock. Thus we can be sure that as
1422 * long as we hold the driver's lock, the list will remain not
1423 * empty if list_empty() check succeeds.
1424 */
1425
1426 for (;;) {
1427 int ret;
1428
1429 if (!q->streaming) {
1430 dprintk(1, "Streaming off, will not wait for buffers\n");
1431 return -EINVAL;
1432 }
1433
1434 if (!list_empty(&q->done_list)) {
1435 /*
1436 * Found a buffer that we were waiting for.
1437 */
1438 break;
1439 }
1440
1441 if (nonblocking) {
1442 dprintk(1, "Nonblocking and no buffers to dequeue, "
1443 "will not wait\n");
1444 return -EAGAIN;
1445 }
1446
1447 /*
1448 * We are streaming and blocking, wait for another buffer to
1449 * become ready or for streamoff. Driver's lock is released to
1450 * allow streamoff or qbuf to be called while waiting.
1451 */
1452 call_qop(q, wait_prepare, q);
1453
1454 /*
1455 * All locks have been released, it is safe to sleep now.
1456 */
1457 dprintk(3, "Will sleep waiting for buffers\n");
1458 ret = wait_event_interruptible(q->done_wq,
1459 !list_empty(&q->done_list) || !q->streaming);
1460
1461 /*
1462 * We need to reevaluate both conditions again after reacquiring
1463 * the locks or return an error if one occurred.
1464 */
1465 call_qop(q, wait_finish, q);
Hans Verkuil32a77262012-09-28 06:12:53 -03001466 if (ret) {
1467 dprintk(1, "Sleep was interrupted\n");
Pawel Osciake23ccc02010-10-11 10:56:41 -03001468 return ret;
Hans Verkuil32a77262012-09-28 06:12:53 -03001469 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001470 }
1471 return 0;
1472}
1473
1474/**
1475 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1476 *
1477 * Will sleep if required for nonblocking == false.
1478 */
1479static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
Hans Verkuil32a77262012-09-28 06:12:53 -03001480 struct v4l2_buffer *b, int nonblocking)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001481{
1482 unsigned long flags;
1483 int ret;
1484
1485 /*
1486 * Wait for at least one buffer to become available on the done_list.
1487 */
1488 ret = __vb2_wait_for_done_vb(q, nonblocking);
1489 if (ret)
1490 return ret;
1491
1492 /*
1493 * Driver's lock has been held since we last verified that done_list
1494 * is not empty, so no need for another list_empty(done_list) check.
1495 */
1496 spin_lock_irqsave(&q->done_lock, flags);
1497 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
Hans Verkuil32a77262012-09-28 06:12:53 -03001498 /*
1499 * Only remove the buffer from done_list if v4l2_buffer can handle all
1500 * the planes.
1501 */
1502 ret = __verify_planes_array(*vb, b);
1503 if (!ret)
1504 list_del(&(*vb)->done_entry);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001505 spin_unlock_irqrestore(&q->done_lock, flags);
1506
Hans Verkuil32a77262012-09-28 06:12:53 -03001507 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001508}
1509
1510/**
1511 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1512 * @q: videobuf2 queue
1513 *
1514 * This function will wait until all buffers that have been given to the driver
1515 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1516 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1517 * taken, for example from stop_streaming() callback.
1518 */
1519int vb2_wait_for_all_buffers(struct vb2_queue *q)
1520{
1521 if (!q->streaming) {
1522 dprintk(1, "Streaming off, will not wait for buffers\n");
1523 return -EINVAL;
1524 }
1525
1526 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1527 return 0;
1528}
1529EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1530
1531/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001532 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1533 */
1534static void __vb2_dqbuf(struct vb2_buffer *vb)
1535{
1536 struct vb2_queue *q = vb->vb2_queue;
1537 unsigned int i;
1538
1539 /* nothing to do if the buffer is already dequeued */
1540 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1541 return;
1542
1543 vb->state = VB2_BUF_STATE_DEQUEUED;
1544
1545 /* unmap DMABUF buffer */
1546 if (q->memory == V4L2_MEMORY_DMABUF)
1547 for (i = 0; i < vb->num_planes; ++i) {
1548 if (!vb->planes[i].dbuf_mapped)
1549 continue;
1550 call_memop(q, unmap_dmabuf, vb->planes[i].mem_priv);
1551 vb->planes[i].dbuf_mapped = 0;
1552 }
1553}
1554
1555/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001556 * vb2_dqbuf() - Dequeue a buffer to the userspace
1557 * @q: videobuf2 queue
1558 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1559 * in driver
1560 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1561 * buffers ready for dequeuing are present. Normally the driver
1562 * would be passing (file->f_flags & O_NONBLOCK) here
1563 *
1564 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1565 * This function:
1566 * 1) verifies the passed buffer,
1567 * 2) calls buf_finish callback in the driver (if provided), in which
1568 * driver can perform any additional operations that may be required before
1569 * returning the buffer to userspace, such as cache sync,
1570 * 3) the buffer struct members are filled with relevant information for
1571 * the userspace.
1572 *
1573 * The return values from this function are intended to be directly returned
1574 * from vidioc_dqbuf handler in driver.
1575 */
1576int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1577{
1578 struct vb2_buffer *vb = NULL;
1579 int ret;
1580
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001581 if (q->fileio) {
1582 dprintk(1, "dqbuf: file io in progress\n");
1583 return -EBUSY;
1584 }
1585
Pawel Osciake23ccc02010-10-11 10:56:41 -03001586 if (b->type != q->type) {
1587 dprintk(1, "dqbuf: invalid buffer type\n");
1588 return -EINVAL;
1589 }
Hans Verkuil32a77262012-09-28 06:12:53 -03001590 ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
1591 if (ret < 0)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001592 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001593
1594 ret = call_qop(q, buf_finish, vb);
1595 if (ret) {
1596 dprintk(1, "dqbuf: buffer finish failed\n");
1597 return ret;
1598 }
1599
1600 switch (vb->state) {
1601 case VB2_BUF_STATE_DONE:
1602 dprintk(3, "dqbuf: Returning done buffer\n");
1603 break;
1604 case VB2_BUF_STATE_ERROR:
1605 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1606 break;
1607 default:
1608 dprintk(1, "dqbuf: Invalid buffer state\n");
1609 return -EINVAL;
1610 }
1611
1612 /* Fill buffer information for the userspace */
1613 __fill_v4l2_buffer(vb, b);
1614 /* Remove from videobuf queue */
1615 list_del(&vb->queued_entry);
Sumit Semwalc5384042012-06-14 10:37:37 -03001616 /* go back to dequeued state */
1617 __vb2_dqbuf(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001618
1619 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1620 vb->v4l2_buf.index, vb->state);
1621
Pawel Osciake23ccc02010-10-11 10:56:41 -03001622 return 0;
1623}
1624EXPORT_SYMBOL_GPL(vb2_dqbuf);
1625
1626/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001627 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1628 *
1629 * Removes all queued buffers from driver's queue and all buffers queued by
1630 * userspace from videobuf's queue. Returns to state after reqbufs.
1631 */
1632static void __vb2_queue_cancel(struct vb2_queue *q)
1633{
1634 unsigned int i;
1635
1636 /*
1637 * Tell driver to stop all transactions and release all queued
1638 * buffers.
1639 */
1640 if (q->streaming)
1641 call_qop(q, stop_streaming, q);
1642 q->streaming = 0;
1643
1644 /*
1645 * Remove all buffers from videobuf's list...
1646 */
1647 INIT_LIST_HEAD(&q->queued_list);
1648 /*
1649 * ...and done list; userspace will not receive any buffers it
1650 * has not already dequeued before initiating cancel.
1651 */
1652 INIT_LIST_HEAD(&q->done_list);
Marek Szyprowskiafdea8b2011-06-10 08:58:42 -03001653 atomic_set(&q->queued_count, 0);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001654 wake_up_all(&q->done_wq);
1655
1656 /*
1657 * Reinitialize all buffers for next use.
1658 */
1659 for (i = 0; i < q->num_buffers; ++i)
Sumit Semwalc5384042012-06-14 10:37:37 -03001660 __vb2_dqbuf(q->bufs[i]);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001661}
1662
1663/**
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001664 * vb2_streamon - start streaming
1665 * @q: videobuf2 queue
1666 * @type: type argument passed from userspace to vidioc_streamon handler
1667 *
1668 * Should be called from vidioc_streamon handler of a driver.
1669 * This function:
1670 * 1) verifies current state
1671 * 2) passes any previously queued buffers to the driver and starts streaming
1672 *
1673 * The return values from this function are intended to be directly returned
1674 * from vidioc_streamon handler in the driver.
1675 */
1676int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1677{
1678 struct vb2_buffer *vb;
1679 int ret;
1680
1681 if (q->fileio) {
1682 dprintk(1, "streamon: file io in progress\n");
1683 return -EBUSY;
1684 }
1685
1686 if (type != q->type) {
1687 dprintk(1, "streamon: invalid stream type\n");
1688 return -EINVAL;
1689 }
1690
1691 if (q->streaming) {
1692 dprintk(1, "streamon: already streaming\n");
1693 return -EBUSY;
1694 }
1695
1696 /*
1697 * If any buffers were queued before streamon,
1698 * we can now pass them to driver for processing.
1699 */
1700 list_for_each_entry(vb, &q->queued_list, queued_entry)
1701 __enqueue_in_driver(vb);
1702
1703 /*
1704 * Let driver notice that streaming state has been enabled.
1705 */
1706 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1707 if (ret) {
1708 dprintk(1, "streamon: driver refused to start streaming\n");
1709 __vb2_queue_cancel(q);
1710 return ret;
1711 }
1712
1713 q->streaming = 1;
1714
1715 dprintk(3, "Streamon successful\n");
1716 return 0;
1717}
1718EXPORT_SYMBOL_GPL(vb2_streamon);
1719
1720
1721/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001722 * vb2_streamoff - stop streaming
1723 * @q: videobuf2 queue
1724 * @type: type argument passed from userspace to vidioc_streamoff handler
1725 *
1726 * Should be called from vidioc_streamoff handler of a driver.
1727 * This function:
1728 * 1) verifies current state,
1729 * 2) stop streaming and dequeues any queued buffers, including those previously
1730 * passed to the driver (after waiting for the driver to finish).
1731 *
1732 * This call can be used for pausing playback.
1733 * The return values from this function are intended to be directly returned
1734 * from vidioc_streamoff handler in the driver
1735 */
1736int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1737{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001738 if (q->fileio) {
1739 dprintk(1, "streamoff: file io in progress\n");
1740 return -EBUSY;
1741 }
1742
Pawel Osciake23ccc02010-10-11 10:56:41 -03001743 if (type != q->type) {
1744 dprintk(1, "streamoff: invalid stream type\n");
1745 return -EINVAL;
1746 }
1747
1748 if (!q->streaming) {
1749 dprintk(1, "streamoff: not streaming\n");
1750 return -EINVAL;
1751 }
1752
1753 /*
1754 * Cancel will pause streaming and remove all buffers from the driver
1755 * and videobuf, effectively returning control over them to userspace.
1756 */
1757 __vb2_queue_cancel(q);
1758
1759 dprintk(3, "Streamoff successful\n");
1760 return 0;
1761}
1762EXPORT_SYMBOL_GPL(vb2_streamoff);
1763
1764/**
1765 * __find_plane_by_offset() - find plane associated with the given offset off
1766 */
1767static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1768 unsigned int *_buffer, unsigned int *_plane)
1769{
1770 struct vb2_buffer *vb;
1771 unsigned int buffer, plane;
1772
1773 /*
1774 * Go over all buffers and their planes, comparing the given offset
1775 * with an offset assigned to each plane. If a match is found,
1776 * return its buffer and plane numbers.
1777 */
1778 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1779 vb = q->bufs[buffer];
1780
1781 for (plane = 0; plane < vb->num_planes; ++plane) {
1782 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1783 *_buffer = buffer;
1784 *_plane = plane;
1785 return 0;
1786 }
1787 }
1788 }
1789
1790 return -EINVAL;
1791}
1792
1793/**
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03001794 * vb2_expbuf() - Export a buffer as a file descriptor
1795 * @q: videobuf2 queue
1796 * @eb: export buffer structure passed from userspace to vidioc_expbuf
1797 * handler in driver
1798 *
1799 * The return values from this function are intended to be directly returned
1800 * from vidioc_expbuf handler in driver.
1801 */
1802int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
1803{
1804 struct vb2_buffer *vb = NULL;
1805 struct vb2_plane *vb_plane;
1806 int ret;
1807 struct dma_buf *dbuf;
1808
1809 if (q->memory != V4L2_MEMORY_MMAP) {
1810 dprintk(1, "Queue is not currently set up for mmap\n");
1811 return -EINVAL;
1812 }
1813
1814 if (!q->mem_ops->get_dmabuf) {
1815 dprintk(1, "Queue does not support DMA buffer exporting\n");
1816 return -EINVAL;
1817 }
1818
1819 if (eb->flags & ~O_CLOEXEC) {
1820 dprintk(1, "Queue does support only O_CLOEXEC flag\n");
1821 return -EINVAL;
1822 }
1823
1824 if (eb->type != q->type) {
1825 dprintk(1, "qbuf: invalid buffer type\n");
1826 return -EINVAL;
1827 }
1828
1829 if (eb->index >= q->num_buffers) {
1830 dprintk(1, "buffer index out of range\n");
1831 return -EINVAL;
1832 }
1833
1834 vb = q->bufs[eb->index];
1835
1836 if (eb->plane >= vb->num_planes) {
1837 dprintk(1, "buffer plane out of range\n");
1838 return -EINVAL;
1839 }
1840
1841 vb_plane = &vb->planes[eb->plane];
1842
1843 dbuf = call_memop(q, get_dmabuf, vb_plane->mem_priv);
1844 if (IS_ERR_OR_NULL(dbuf)) {
1845 dprintk(1, "Failed to export buffer %d, plane %d\n",
1846 eb->index, eb->plane);
1847 return -EINVAL;
1848 }
1849
1850 ret = dma_buf_fd(dbuf, eb->flags);
1851 if (ret < 0) {
1852 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
1853 eb->index, eb->plane, ret);
1854 dma_buf_put(dbuf);
1855 return ret;
1856 }
1857
1858 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
1859 eb->index, eb->plane, ret);
1860 eb->fd = ret;
1861
1862 return 0;
1863}
1864EXPORT_SYMBOL_GPL(vb2_expbuf);
1865
1866/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001867 * vb2_mmap() - map video buffers into application address space
1868 * @q: videobuf2 queue
1869 * @vma: vma passed to the mmap file operation handler in the driver
1870 *
1871 * Should be called from mmap file operation handler of a driver.
1872 * This function maps one plane of one of the available video buffers to
1873 * userspace. To map whole video memory allocated on reqbufs, this function
1874 * has to be called once per each plane per each buffer previously allocated.
1875 *
1876 * When the userspace application calls mmap, it passes to it an offset returned
1877 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1878 * a "cookie", which is then used to identify the plane to be mapped.
1879 * This function finds a plane with a matching offset and a mapping is performed
1880 * by the means of a provided memory operation.
1881 *
1882 * The return values from this function are intended to be directly returned
1883 * from the mmap handler in driver.
1884 */
1885int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1886{
1887 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001888 struct vb2_buffer *vb;
1889 unsigned int buffer, plane;
1890 int ret;
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -03001891 unsigned long length;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001892
1893 if (q->memory != V4L2_MEMORY_MMAP) {
1894 dprintk(1, "Queue is not currently set up for mmap\n");
1895 return -EINVAL;
1896 }
1897
1898 /*
1899 * Check memory area access mode.
1900 */
1901 if (!(vma->vm_flags & VM_SHARED)) {
1902 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1903 return -EINVAL;
1904 }
1905 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1906 if (!(vma->vm_flags & VM_WRITE)) {
1907 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1908 return -EINVAL;
1909 }
1910 } else {
1911 if (!(vma->vm_flags & VM_READ)) {
1912 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1913 return -EINVAL;
1914 }
1915 }
1916
1917 /*
1918 * Find the plane corresponding to the offset passed by userspace.
1919 */
1920 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1921 if (ret)
1922 return ret;
1923
1924 vb = q->bufs[buffer];
Pawel Osciake23ccc02010-10-11 10:56:41 -03001925
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -03001926 /*
1927 * MMAP requires page_aligned buffers.
1928 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
1929 * so, we need to do the same here.
1930 */
1931 length = PAGE_ALIGN(vb->v4l2_planes[plane].length);
1932 if (length < (vma->vm_end - vma->vm_start)) {
1933 dprintk(1,
1934 "MMAP invalid, as it would overflow buffer length\n");
Seung-Woo Kim068a0df2013-04-11 23:57:57 -03001935 return -EINVAL;
1936 }
1937
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001938 ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001939 if (ret)
1940 return ret;
1941
Pawel Osciake23ccc02010-10-11 10:56:41 -03001942 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1943 return 0;
1944}
1945EXPORT_SYMBOL_GPL(vb2_mmap);
1946
Scott Jiang6f524ec2011-09-21 09:25:23 -03001947#ifndef CONFIG_MMU
1948unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1949 unsigned long addr,
1950 unsigned long len,
1951 unsigned long pgoff,
1952 unsigned long flags)
1953{
1954 unsigned long off = pgoff << PAGE_SHIFT;
1955 struct vb2_buffer *vb;
1956 unsigned int buffer, plane;
1957 int ret;
1958
1959 if (q->memory != V4L2_MEMORY_MMAP) {
1960 dprintk(1, "Queue is not currently set up for mmap\n");
1961 return -EINVAL;
1962 }
1963
1964 /*
1965 * Find the plane corresponding to the offset passed by userspace.
1966 */
1967 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1968 if (ret)
1969 return ret;
1970
1971 vb = q->bufs[buffer];
1972
1973 return (unsigned long)vb2_plane_vaddr(vb, plane);
1974}
1975EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
1976#endif
1977
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001978static int __vb2_init_fileio(struct vb2_queue *q, int read);
1979static int __vb2_cleanup_fileio(struct vb2_queue *q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001980
1981/**
1982 * vb2_poll() - implements poll userspace operation
1983 * @q: videobuf2 queue
1984 * @file: file argument passed to the poll file operation handler
1985 * @wait: wait argument passed to the poll file operation handler
1986 *
1987 * This function implements poll file operation handler for a driver.
1988 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1989 * be informed that the file descriptor of a video device is available for
1990 * reading.
1991 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1992 * will be reported as available for writing.
1993 *
Hans Verkuil95213ce2011-07-13 04:26:52 -03001994 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
1995 * pending events.
1996 *
Pawel Osciake23ccc02010-10-11 10:56:41 -03001997 * The return values from this function are intended to be directly returned
1998 * from poll handler in driver.
1999 */
2000unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
2001{
Hans Verkuil95213ce2011-07-13 04:26:52 -03002002 struct video_device *vfd = video_devdata(file);
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002003 unsigned long req_events = poll_requested_events(wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002004 struct vb2_buffer *vb = NULL;
Hans Verkuil95213ce2011-07-13 04:26:52 -03002005 unsigned int res = 0;
2006 unsigned long flags;
2007
2008 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
2009 struct v4l2_fh *fh = file->private_data;
2010
2011 if (v4l2_event_pending(fh))
2012 res = POLLPRI;
2013 else if (req_events & POLLPRI)
2014 poll_wait(file, &fh->wait, wait);
2015 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03002016
Hans Verkuilcd138232013-01-30 13:29:02 -03002017 if (!V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLIN | POLLRDNORM)))
2018 return res;
2019 if (V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLOUT | POLLWRNORM)))
2020 return res;
2021
Pawel Osciake23ccc02010-10-11 10:56:41 -03002022 /*
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03002023 * Start file I/O emulator only if streaming API has not been used yet.
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002024 */
2025 if (q->num_buffers == 0 && q->fileio == NULL) {
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002026 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2027 (req_events & (POLLIN | POLLRDNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002028 if (__vb2_init_fileio(q, 1))
2029 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002030 }
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002031 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2032 (req_events & (POLLOUT | POLLWRNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002033 if (__vb2_init_fileio(q, 0))
2034 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002035 /*
2036 * Write to OUTPUT queue can be done immediately.
2037 */
Hans Verkuil95213ce2011-07-13 04:26:52 -03002038 return res | POLLOUT | POLLWRNORM;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002039 }
2040 }
2041
2042 /*
Pawel Osciake23ccc02010-10-11 10:56:41 -03002043 * There is nothing to wait for if no buffers have already been queued.
2044 */
2045 if (list_empty(&q->queued_list))
Hans Verkuil95213ce2011-07-13 04:26:52 -03002046 return res | POLLERR;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002047
Seung-Woo Kim412cb872013-05-20 23:47:29 -03002048 if (list_empty(&q->done_list))
2049 poll_wait(file, &q->done_wq, wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002050
2051 /*
2052 * Take first buffer available for dequeuing.
2053 */
2054 spin_lock_irqsave(&q->done_lock, flags);
2055 if (!list_empty(&q->done_list))
2056 vb = list_first_entry(&q->done_list, struct vb2_buffer,
2057 done_entry);
2058 spin_unlock_irqrestore(&q->done_lock, flags);
2059
2060 if (vb && (vb->state == VB2_BUF_STATE_DONE
2061 || vb->state == VB2_BUF_STATE_ERROR)) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002062 return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
2063 res | POLLOUT | POLLWRNORM :
2064 res | POLLIN | POLLRDNORM;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002065 }
Hans Verkuil95213ce2011-07-13 04:26:52 -03002066 return res;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002067}
2068EXPORT_SYMBOL_GPL(vb2_poll);
2069
2070/**
2071 * vb2_queue_init() - initialize a videobuf2 queue
2072 * @q: videobuf2 queue; this structure should be allocated in driver
2073 *
2074 * The vb2_queue structure should be allocated by the driver. The driver is
2075 * responsible of clearing it's content and setting initial values for some
2076 * required entries before calling this function.
2077 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2078 * to the struct vb2_queue description in include/media/videobuf2-core.h
2079 * for more information.
2080 */
2081int vb2_queue_init(struct vb2_queue *q)
2082{
Ezequiel Garcia896f38f2012-09-17 14:59:30 -03002083 /*
2084 * Sanity check
2085 */
2086 if (WARN_ON(!q) ||
2087 WARN_ON(!q->ops) ||
2088 WARN_ON(!q->mem_ops) ||
2089 WARN_ON(!q->type) ||
2090 WARN_ON(!q->io_modes) ||
2091 WARN_ON(!q->ops->queue_setup) ||
Kamil Debski6aa69f92013-01-25 06:29:57 -03002092 WARN_ON(!q->ops->buf_queue) ||
2093 WARN_ON(q->timestamp_type & ~V4L2_BUF_FLAG_TIMESTAMP_MASK))
Ezequiel Garcia896f38f2012-09-17 14:59:30 -03002094 return -EINVAL;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002095
Kamil Debski6aa69f92013-01-25 06:29:57 -03002096 /* Warn that the driver should choose an appropriate timestamp type */
2097 WARN_ON(q->timestamp_type == V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
2098
Pawel Osciake23ccc02010-10-11 10:56:41 -03002099 INIT_LIST_HEAD(&q->queued_list);
2100 INIT_LIST_HEAD(&q->done_list);
2101 spin_lock_init(&q->done_lock);
2102 init_waitqueue_head(&q->done_wq);
2103
2104 if (q->buf_struct_size == 0)
2105 q->buf_struct_size = sizeof(struct vb2_buffer);
2106
2107 return 0;
2108}
2109EXPORT_SYMBOL_GPL(vb2_queue_init);
2110
2111/**
2112 * vb2_queue_release() - stop streaming, release the queue and free memory
2113 * @q: videobuf2 queue
2114 *
2115 * This function stops streaming and performs necessary clean ups, including
2116 * freeing video buffer memory. The driver is responsible for freeing
2117 * the vb2_queue structure itself.
2118 */
2119void vb2_queue_release(struct vb2_queue *q)
2120{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002121 __vb2_cleanup_fileio(q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002122 __vb2_queue_cancel(q);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03002123 __vb2_queue_free(q, q->num_buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002124}
2125EXPORT_SYMBOL_GPL(vb2_queue_release);
2126
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002127/**
2128 * struct vb2_fileio_buf - buffer context used by file io emulator
2129 *
2130 * vb2 provides a compatibility layer and emulator of file io (read and
2131 * write) calls on top of streaming API. This structure is used for
2132 * tracking context related to the buffers.
2133 */
2134struct vb2_fileio_buf {
2135 void *vaddr;
2136 unsigned int size;
2137 unsigned int pos;
2138 unsigned int queued:1;
2139};
2140
2141/**
2142 * struct vb2_fileio_data - queue context used by file io emulator
2143 *
2144 * vb2 provides a compatibility layer and emulator of file io (read and
2145 * write) calls on top of streaming API. For proper operation it required
2146 * this structure to save the driver state between each call of the read
2147 * or write function.
2148 */
2149struct vb2_fileio_data {
2150 struct v4l2_requestbuffers req;
2151 struct v4l2_buffer b;
2152 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
2153 unsigned int index;
2154 unsigned int q_count;
2155 unsigned int dq_count;
2156 unsigned int flags;
2157};
2158
2159/**
2160 * __vb2_init_fileio() - initialize file io emulator
2161 * @q: videobuf2 queue
2162 * @read: mode selector (1 means read, 0 means write)
2163 */
2164static int __vb2_init_fileio(struct vb2_queue *q, int read)
2165{
2166 struct vb2_fileio_data *fileio;
2167 int i, ret;
2168 unsigned int count = 0;
2169
2170 /*
2171 * Sanity check
2172 */
2173 if ((read && !(q->io_modes & VB2_READ)) ||
2174 (!read && !(q->io_modes & VB2_WRITE)))
2175 BUG();
2176
2177 /*
2178 * Check if device supports mapping buffers to kernel virtual space.
2179 */
2180 if (!q->mem_ops->vaddr)
2181 return -EBUSY;
2182
2183 /*
2184 * Check if streaming api has not been already activated.
2185 */
2186 if (q->streaming || q->num_buffers > 0)
2187 return -EBUSY;
2188
2189 /*
2190 * Start with count 1, driver can increase it in queue_setup()
2191 */
2192 count = 1;
2193
2194 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
2195 (read) ? "read" : "write", count, q->io_flags);
2196
2197 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
2198 if (fileio == NULL)
2199 return -ENOMEM;
2200
2201 fileio->flags = q->io_flags;
2202
2203 /*
2204 * Request buffers and use MMAP type to force driver
2205 * to allocate buffers by itself.
2206 */
2207 fileio->req.count = count;
2208 fileio->req.memory = V4L2_MEMORY_MMAP;
2209 fileio->req.type = q->type;
2210 ret = vb2_reqbufs(q, &fileio->req);
2211 if (ret)
2212 goto err_kfree;
2213
2214 /*
2215 * Check if plane_count is correct
2216 * (multiplane buffers are not supported).
2217 */
2218 if (q->bufs[0]->num_planes != 1) {
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002219 ret = -EBUSY;
2220 goto err_reqbufs;
2221 }
2222
2223 /*
2224 * Get kernel address of each buffer.
2225 */
2226 for (i = 0; i < q->num_buffers; i++) {
2227 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
Wei Yongjun5dd69462013-05-13 01:48:45 -03002228 if (fileio->bufs[i].vaddr == NULL) {
2229 ret = -EINVAL;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002230 goto err_reqbufs;
Wei Yongjun5dd69462013-05-13 01:48:45 -03002231 }
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002232 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
2233 }
2234
2235 /*
2236 * Read mode requires pre queuing of all buffers.
2237 */
2238 if (read) {
2239 /*
2240 * Queue all buffers.
2241 */
2242 for (i = 0; i < q->num_buffers; i++) {
2243 struct v4l2_buffer *b = &fileio->b;
2244 memset(b, 0, sizeof(*b));
2245 b->type = q->type;
2246 b->memory = q->memory;
2247 b->index = i;
2248 ret = vb2_qbuf(q, b);
2249 if (ret)
2250 goto err_reqbufs;
2251 fileio->bufs[i].queued = 1;
2252 }
2253
2254 /*
2255 * Start streaming.
2256 */
2257 ret = vb2_streamon(q, q->type);
2258 if (ret)
2259 goto err_reqbufs;
2260 }
2261
2262 q->fileio = fileio;
2263
2264 return ret;
2265
2266err_reqbufs:
Hans de Goedea67e1722012-05-08 14:47:39 -03002267 fileio->req.count = 0;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002268 vb2_reqbufs(q, &fileio->req);
2269
2270err_kfree:
2271 kfree(fileio);
2272 return ret;
2273}
2274
2275/**
2276 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2277 * @q: videobuf2 queue
2278 */
2279static int __vb2_cleanup_fileio(struct vb2_queue *q)
2280{
2281 struct vb2_fileio_data *fileio = q->fileio;
2282
2283 if (fileio) {
2284 /*
2285 * Hack fileio context to enable direct calls to vb2 ioctl
2286 * interface.
2287 */
2288 q->fileio = NULL;
2289
2290 vb2_streamoff(q, q->type);
2291 fileio->req.count = 0;
2292 vb2_reqbufs(q, &fileio->req);
2293 kfree(fileio);
2294 dprintk(3, "file io emulator closed\n");
2295 }
2296 return 0;
2297}
2298
2299/**
2300 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2301 * @q: videobuf2 queue
2302 * @data: pointed to target userspace buffer
2303 * @count: number of bytes to read or write
2304 * @ppos: file handle position tracking pointer
2305 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2306 * @read: access mode selector (1 means read, 0 means write)
2307 */
2308static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
2309 loff_t *ppos, int nonblock, int read)
2310{
2311 struct vb2_fileio_data *fileio;
2312 struct vb2_fileio_buf *buf;
2313 int ret, index;
2314
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002315 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002316 read ? "read" : "write", (long)*ppos, count,
2317 nonblock ? "non" : "");
2318
2319 if (!data)
2320 return -EINVAL;
2321
2322 /*
2323 * Initialize emulator on first call.
2324 */
2325 if (!q->fileio) {
2326 ret = __vb2_init_fileio(q, read);
2327 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
2328 if (ret)
2329 return ret;
2330 }
2331 fileio = q->fileio;
2332
2333 /*
2334 * Hack fileio context to enable direct calls to vb2 ioctl interface.
2335 * The pointer will be restored before returning from this function.
2336 */
2337 q->fileio = NULL;
2338
2339 index = fileio->index;
2340 buf = &fileio->bufs[index];
2341
2342 /*
2343 * Check if we need to dequeue the buffer.
2344 */
2345 if (buf->queued) {
2346 struct vb2_buffer *vb;
2347
2348 /*
2349 * Call vb2_dqbuf to get buffer back.
2350 */
2351 memset(&fileio->b, 0, sizeof(fileio->b));
2352 fileio->b.type = q->type;
2353 fileio->b.memory = q->memory;
2354 fileio->b.index = index;
2355 ret = vb2_dqbuf(q, &fileio->b, nonblock);
2356 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2357 if (ret)
2358 goto end;
2359 fileio->dq_count += 1;
2360
2361 /*
2362 * Get number of bytes filled by the driver
2363 */
2364 vb = q->bufs[index];
2365 buf->size = vb2_get_plane_payload(vb, 0);
2366 buf->queued = 0;
2367 }
2368
2369 /*
2370 * Limit count on last few bytes of the buffer.
2371 */
2372 if (buf->pos + count > buf->size) {
2373 count = buf->size - buf->pos;
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002374 dprintk(5, "reducing read count: %zd\n", count);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002375 }
2376
2377 /*
2378 * Transfer data to userspace.
2379 */
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002380 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002381 count, index, buf->pos);
2382 if (read)
2383 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2384 else
2385 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2386 if (ret) {
2387 dprintk(3, "file io: error copying data\n");
2388 ret = -EFAULT;
2389 goto end;
2390 }
2391
2392 /*
2393 * Update counters.
2394 */
2395 buf->pos += count;
2396 *ppos += count;
2397
2398 /*
2399 * Queue next buffer if required.
2400 */
2401 if (buf->pos == buf->size ||
2402 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2403 /*
2404 * Check if this is the last buffer to read.
2405 */
2406 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2407 fileio->dq_count == 1) {
2408 dprintk(3, "file io: read limit reached\n");
2409 /*
2410 * Restore fileio pointer and release the context.
2411 */
2412 q->fileio = fileio;
2413 return __vb2_cleanup_fileio(q);
2414 }
2415
2416 /*
2417 * Call vb2_qbuf and give buffer to the driver.
2418 */
2419 memset(&fileio->b, 0, sizeof(fileio->b));
2420 fileio->b.type = q->type;
2421 fileio->b.memory = q->memory;
2422 fileio->b.index = index;
2423 fileio->b.bytesused = buf->pos;
2424 ret = vb2_qbuf(q, &fileio->b);
2425 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2426 if (ret)
2427 goto end;
2428
2429 /*
2430 * Buffer has been queued, update the status
2431 */
2432 buf->pos = 0;
2433 buf->queued = 1;
2434 buf->size = q->bufs[0]->v4l2_planes[0].length;
2435 fileio->q_count += 1;
2436
2437 /*
2438 * Switch to the next buffer
2439 */
2440 fileio->index = (index + 1) % q->num_buffers;
2441
2442 /*
2443 * Start streaming if required.
2444 */
2445 if (!read && !q->streaming) {
2446 ret = vb2_streamon(q, q->type);
2447 if (ret)
2448 goto end;
2449 }
2450 }
2451
2452 /*
2453 * Return proper number of bytes processed.
2454 */
2455 if (ret == 0)
2456 ret = count;
2457end:
2458 /*
2459 * Restore the fileio context and block vb2 ioctl interface.
2460 */
2461 q->fileio = fileio;
2462 return ret;
2463}
2464
2465size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2466 loff_t *ppos, int nonblocking)
2467{
2468 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2469}
2470EXPORT_SYMBOL_GPL(vb2_read);
2471
2472size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
2473 loff_t *ppos, int nonblocking)
2474{
2475 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
2476}
2477EXPORT_SYMBOL_GPL(vb2_write);
2478
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002479
2480/*
2481 * The following functions are not part of the vb2 core API, but are helper
2482 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2483 * and struct vb2_ops.
2484 * They contain boilerplate code that most if not all drivers have to do
2485 * and so they simplify the driver code.
2486 */
2487
2488/* The queue is busy if there is a owner and you are not that owner. */
2489static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
2490{
2491 return vdev->queue->owner && vdev->queue->owner != file->private_data;
2492}
2493
2494/* vb2 ioctl helpers */
2495
2496int vb2_ioctl_reqbufs(struct file *file, void *priv,
2497 struct v4l2_requestbuffers *p)
2498{
2499 struct video_device *vdev = video_devdata(file);
2500 int res = __verify_memory_type(vdev->queue, p->memory, p->type);
2501
2502 if (res)
2503 return res;
2504 if (vb2_queue_is_busy(vdev, file))
2505 return -EBUSY;
2506 res = __reqbufs(vdev->queue, p);
2507 /* If count == 0, then the owner has released all buffers and he
2508 is no longer owner of the queue. Otherwise we have a new owner. */
2509 if (res == 0)
2510 vdev->queue->owner = p->count ? file->private_data : NULL;
2511 return res;
2512}
2513EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
2514
2515int vb2_ioctl_create_bufs(struct file *file, void *priv,
2516 struct v4l2_create_buffers *p)
2517{
2518 struct video_device *vdev = video_devdata(file);
2519 int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
2520
2521 p->index = vdev->queue->num_buffers;
2522 /* If count == 0, then just check if memory and type are valid.
2523 Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2524 if (p->count == 0)
2525 return res != -EBUSY ? res : 0;
2526 if (res)
2527 return res;
2528 if (vb2_queue_is_busy(vdev, file))
2529 return -EBUSY;
2530 res = __create_bufs(vdev->queue, p);
2531 if (res == 0)
2532 vdev->queue->owner = file->private_data;
2533 return res;
2534}
2535EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
2536
2537int vb2_ioctl_prepare_buf(struct file *file, void *priv,
2538 struct v4l2_buffer *p)
2539{
2540 struct video_device *vdev = video_devdata(file);
2541
2542 if (vb2_queue_is_busy(vdev, file))
2543 return -EBUSY;
2544 return vb2_prepare_buf(vdev->queue, p);
2545}
2546EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
2547
2548int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
2549{
2550 struct video_device *vdev = video_devdata(file);
2551
2552 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2553 return vb2_querybuf(vdev->queue, p);
2554}
2555EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
2556
2557int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2558{
2559 struct video_device *vdev = video_devdata(file);
2560
2561 if (vb2_queue_is_busy(vdev, file))
2562 return -EBUSY;
2563 return vb2_qbuf(vdev->queue, p);
2564}
2565EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
2566
2567int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2568{
2569 struct video_device *vdev = video_devdata(file);
2570
2571 if (vb2_queue_is_busy(vdev, file))
2572 return -EBUSY;
2573 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
2574}
2575EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
2576
2577int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
2578{
2579 struct video_device *vdev = video_devdata(file);
2580
2581 if (vb2_queue_is_busy(vdev, file))
2582 return -EBUSY;
2583 return vb2_streamon(vdev->queue, i);
2584}
2585EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
2586
2587int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
2588{
2589 struct video_device *vdev = video_devdata(file);
2590
2591 if (vb2_queue_is_busy(vdev, file))
2592 return -EBUSY;
2593 return vb2_streamoff(vdev->queue, i);
2594}
2595EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
2596
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002597int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
2598{
2599 struct video_device *vdev = video_devdata(file);
2600
2601 if (vb2_queue_is_busy(vdev, file))
2602 return -EBUSY;
2603 return vb2_expbuf(vdev->queue, p);
2604}
2605EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
2606
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002607/* v4l2_file_operations helpers */
2608
2609int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
2610{
2611 struct video_device *vdev = video_devdata(file);
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002612 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2613 int err;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002614
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002615 if (lock && mutex_lock_interruptible(lock))
2616 return -ERESTARTSYS;
2617 err = vb2_mmap(vdev->queue, vma);
2618 if (lock)
2619 mutex_unlock(lock);
2620 return err;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002621}
2622EXPORT_SYMBOL_GPL(vb2_fop_mmap);
2623
2624int vb2_fop_release(struct file *file)
2625{
2626 struct video_device *vdev = video_devdata(file);
2627
2628 if (file->private_data == vdev->queue->owner) {
2629 vb2_queue_release(vdev->queue);
2630 vdev->queue->owner = NULL;
2631 }
2632 return v4l2_fh_release(file);
2633}
2634EXPORT_SYMBOL_GPL(vb2_fop_release);
2635
2636ssize_t vb2_fop_write(struct file *file, char __user *buf,
2637 size_t count, loff_t *ppos)
2638{
2639 struct video_device *vdev = video_devdata(file);
2640 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002641 int err = -EBUSY;
2642
Hans Verkuilcf533732012-07-31 04:02:25 -03002643 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002644 return -ERESTARTSYS;
2645 if (vb2_queue_is_busy(vdev, file))
2646 goto exit;
2647 err = vb2_write(vdev->queue, buf, count, ppos,
2648 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03002649 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002650 vdev->queue->owner = file->private_data;
2651exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03002652 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002653 mutex_unlock(lock);
2654 return err;
2655}
2656EXPORT_SYMBOL_GPL(vb2_fop_write);
2657
2658ssize_t vb2_fop_read(struct file *file, char __user *buf,
2659 size_t count, loff_t *ppos)
2660{
2661 struct video_device *vdev = video_devdata(file);
2662 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002663 int err = -EBUSY;
2664
Hans Verkuilcf533732012-07-31 04:02:25 -03002665 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002666 return -ERESTARTSYS;
2667 if (vb2_queue_is_busy(vdev, file))
2668 goto exit;
2669 err = vb2_read(vdev->queue, buf, count, ppos,
2670 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03002671 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002672 vdev->queue->owner = file->private_data;
2673exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03002674 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002675 mutex_unlock(lock);
2676 return err;
2677}
2678EXPORT_SYMBOL_GPL(vb2_fop_read);
2679
2680unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
2681{
2682 struct video_device *vdev = video_devdata(file);
2683 struct vb2_queue *q = vdev->queue;
2684 struct mutex *lock = q->lock ? q->lock : vdev->lock;
2685 unsigned long req_events = poll_requested_events(wait);
2686 unsigned res;
2687 void *fileio;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002688 bool must_lock = false;
2689
2690 /* Try to be smart: only lock if polling might start fileio,
2691 otherwise locking will only introduce unwanted delays. */
2692 if (q->num_buffers == 0 && q->fileio == NULL) {
2693 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2694 (req_events & (POLLIN | POLLRDNORM)))
2695 must_lock = true;
2696 else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2697 (req_events & (POLLOUT | POLLWRNORM)))
2698 must_lock = true;
2699 }
2700
2701 /* If locking is needed, but this helper doesn't know how, then you
2702 shouldn't be using this helper but you should write your own. */
Hans Verkuilcf533732012-07-31 04:02:25 -03002703 WARN_ON(must_lock && !lock);
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002704
Hans Verkuilcf533732012-07-31 04:02:25 -03002705 if (must_lock && lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002706 return POLLERR;
2707
2708 fileio = q->fileio;
2709
2710 res = vb2_poll(vdev->queue, file, wait);
2711
2712 /* If fileio was started, then we have a new queue owner. */
2713 if (must_lock && !fileio && q->fileio)
2714 q->owner = file->private_data;
Hans Verkuilcf533732012-07-31 04:02:25 -03002715 if (must_lock && lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002716 mutex_unlock(lock);
2717 return res;
2718}
2719EXPORT_SYMBOL_GPL(vb2_fop_poll);
2720
2721#ifndef CONFIG_MMU
2722unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
2723 unsigned long len, unsigned long pgoff, unsigned long flags)
2724{
2725 struct video_device *vdev = video_devdata(file);
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002726 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2727 int ret;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002728
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002729 if (lock && mutex_lock_interruptible(lock))
2730 return -ERESTARTSYS;
2731 ret = vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
2732 if (lock)
2733 mutex_unlock(lock);
2734 return ret;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002735}
2736EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
2737#endif
2738
2739/* vb2_ops helpers. Only use if vq->lock is non-NULL. */
2740
2741void vb2_ops_wait_prepare(struct vb2_queue *vq)
2742{
2743 mutex_unlock(vq->lock);
2744}
2745EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
2746
2747void vb2_ops_wait_finish(struct vb2_queue *vq)
2748{
2749 mutex_lock(vq->lock);
2750}
2751EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
2752
Pawel Osciake23ccc02010-10-11 10:56:41 -03002753MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
Pawel Osciak95072082011-03-13 15:23:32 -03002754MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
Pawel Osciake23ccc02010-10-11 10:56:41 -03002755MODULE_LICENSE("GPL");