blob: 144caf12fb6cc3fa20a4acaaf28ed7f12f0b0cfc [file] [log] [blame]
Pawel Osciake23ccc02010-10-11 10:56:41 -03001/*
2 * videobuf2-core.c - V4L2 driver helper framework
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 *
Pawel Osciak95072082011-03-13 15:23:32 -03006 * Author: Pawel Osciak <pawel@osciak.com>
Pawel Osciake23ccc02010-10-11 10:56:41 -03007 * Marek Szyprowski <m.szyprowski@samsung.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation.
12 */
13
14#include <linux/err.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/mm.h>
18#include <linux/poll.h>
19#include <linux/slab.h>
20#include <linux/sched.h>
21
Hans Verkuil95213ce2011-07-13 04:26:52 -030022#include <media/v4l2-dev.h>
23#include <media/v4l2-fh.h>
24#include <media/v4l2-event.h>
Pawel Osciake23ccc02010-10-11 10:56:41 -030025#include <media/videobuf2-core.h>
26
27static int debug;
28module_param(debug, int, 0644);
29
30#define dprintk(level, fmt, arg...) \
31 do { \
32 if (debug >= level) \
33 printk(KERN_DEBUG "vb2: " fmt, ## arg); \
34 } while (0)
35
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030036#define call_memop(q, op, args...) \
Pawel Osciake23ccc02010-10-11 10:56:41 -030037 (((q)->mem_ops->op) ? \
38 ((q)->mem_ops->op(args)) : 0)
39
40#define call_qop(q, op, args...) \
41 (((q)->ops->op) ? ((q)->ops->op(args)) : 0)
42
Sakari Ailus1b18e7a2012-10-22 17:10:16 -030043#define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -030044 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
Sakari Ailus1b18e7a2012-10-22 17:10:16 -030045 V4L2_BUF_FLAG_PREPARED | \
46 V4L2_BUF_FLAG_TIMESTAMP_MASK)
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -030047
Pawel Osciake23ccc02010-10-11 10:56:41 -030048/**
49 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
50 */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030051static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
Pawel Osciake23ccc02010-10-11 10:56:41 -030052{
53 struct vb2_queue *q = vb->vb2_queue;
54 void *mem_priv;
55 int plane;
56
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -030057 /*
58 * Allocate memory for all planes in this buffer
59 * NOTE: mmapped areas should be page aligned
60 */
Pawel Osciake23ccc02010-10-11 10:56:41 -030061 for (plane = 0; plane < vb->num_planes; ++plane) {
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -030062 unsigned long size = PAGE_ALIGN(q->plane_sizes[plane]);
63
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030064 mem_priv = call_memop(q, alloc, q->alloc_ctx[plane],
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -030065 size, q->gfp_flags);
Guennadi Liakhovetski62a79432011-03-22 09:24:58 -030066 if (IS_ERR_OR_NULL(mem_priv))
Pawel Osciake23ccc02010-10-11 10:56:41 -030067 goto free;
68
69 /* Associate allocator private data with this plane */
70 vb->planes[plane].mem_priv = mem_priv;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030071 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
Pawel Osciake23ccc02010-10-11 10:56:41 -030072 }
73
74 return 0;
75free:
76 /* Free already allocated memory if one of the allocations failed */
Marek Szyprowskia00d0262011-12-15 05:53:06 -030077 for (; plane > 0; --plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030078 call_memop(q, put, vb->planes[plane - 1].mem_priv);
Marek Szyprowskia00d0262011-12-15 05:53:06 -030079 vb->planes[plane - 1].mem_priv = NULL;
80 }
Pawel Osciake23ccc02010-10-11 10:56:41 -030081
82 return -ENOMEM;
83}
84
85/**
86 * __vb2_buf_mem_free() - free memory of the given buffer
87 */
88static void __vb2_buf_mem_free(struct vb2_buffer *vb)
89{
90 struct vb2_queue *q = vb->vb2_queue;
91 unsigned int plane;
92
93 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030094 call_memop(q, put, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -030095 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskia00d0262011-12-15 05:53:06 -030096 dprintk(3, "Freed plane %d of buffer %d\n", plane,
97 vb->v4l2_buf.index);
Pawel Osciake23ccc02010-10-11 10:56:41 -030098 }
99}
100
101/**
102 * __vb2_buf_userptr_put() - release userspace memory associated with
103 * a USERPTR buffer
104 */
105static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
106{
107 struct vb2_queue *q = vb->vb2_queue;
108 unsigned int plane;
109
110 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300111 if (vb->planes[plane].mem_priv)
112 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
113 vb->planes[plane].mem_priv = NULL;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300114 }
115}
116
117/**
Sumit Semwalc5384042012-06-14 10:37:37 -0300118 * __vb2_plane_dmabuf_put() - release memory associated with
119 * a DMABUF shared plane
120 */
121static void __vb2_plane_dmabuf_put(struct vb2_queue *q, struct vb2_plane *p)
122{
123 if (!p->mem_priv)
124 return;
125
126 if (p->dbuf_mapped)
127 call_memop(q, unmap_dmabuf, p->mem_priv);
128
129 call_memop(q, detach_dmabuf, p->mem_priv);
130 dma_buf_put(p->dbuf);
131 memset(p, 0, sizeof(*p));
132}
133
134/**
135 * __vb2_buf_dmabuf_put() - release memory associated with
136 * a DMABUF shared buffer
137 */
138static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
139{
140 struct vb2_queue *q = vb->vb2_queue;
141 unsigned int plane;
142
143 for (plane = 0; plane < vb->num_planes; ++plane)
144 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
145}
146
147/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300148 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
149 * every buffer on the queue
150 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300151static void __setup_offsets(struct vb2_queue *q, unsigned int n)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300152{
153 unsigned int buffer, plane;
154 struct vb2_buffer *vb;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300155 unsigned long off;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300156
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300157 if (q->num_buffers) {
158 struct v4l2_plane *p;
159 vb = q->bufs[q->num_buffers - 1];
160 p = &vb->v4l2_planes[vb->num_planes - 1];
161 off = PAGE_ALIGN(p->m.mem_offset + p->length);
162 } else {
163 off = 0;
164 }
165
166 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300167 vb = q->bufs[buffer];
168 if (!vb)
169 continue;
170
171 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski49076022011-10-13 07:07:24 -0300172 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
Pawel Osciake23ccc02010-10-11 10:56:41 -0300173 vb->v4l2_planes[plane].m.mem_offset = off;
174
175 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
176 buffer, plane, off);
177
178 off += vb->v4l2_planes[plane].length;
179 off = PAGE_ALIGN(off);
180 }
181 }
182}
183
184/**
185 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
186 * video buffer memory for all buffers/planes on the queue and initializes the
187 * queue
188 *
189 * Returns the number of buffers successfully allocated.
190 */
191static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300192 unsigned int num_buffers, unsigned int num_planes)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300193{
194 unsigned int buffer;
195 struct vb2_buffer *vb;
196 int ret;
197
198 for (buffer = 0; buffer < num_buffers; ++buffer) {
199 /* Allocate videobuf buffer structures */
200 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
201 if (!vb) {
202 dprintk(1, "Memory alloc for buffer struct failed\n");
203 break;
204 }
205
206 /* Length stores number of planes for multiplanar buffers */
207 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
208 vb->v4l2_buf.length = num_planes;
209
210 vb->state = VB2_BUF_STATE_DEQUEUED;
211 vb->vb2_queue = q;
212 vb->num_planes = num_planes;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300213 vb->v4l2_buf.index = q->num_buffers + buffer;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300214 vb->v4l2_buf.type = q->type;
215 vb->v4l2_buf.memory = memory;
216
217 /* Allocate video buffer memory for the MMAP type */
218 if (memory == V4L2_MEMORY_MMAP) {
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300219 ret = __vb2_buf_mem_alloc(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300220 if (ret) {
221 dprintk(1, "Failed allocating memory for "
222 "buffer %d\n", buffer);
223 kfree(vb);
224 break;
225 }
226 /*
227 * Call the driver-provided buffer initialization
228 * callback, if given. An error in initialization
229 * results in queue setup failure.
230 */
231 ret = call_qop(q, buf_init, vb);
232 if (ret) {
233 dprintk(1, "Buffer %d %p initialization"
234 " failed\n", buffer, vb);
235 __vb2_buf_mem_free(vb);
236 kfree(vb);
237 break;
238 }
239 }
240
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300241 q->bufs[q->num_buffers + buffer] = vb;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300242 }
243
Philipp Zabeldc775232013-09-19 04:37:29 -0300244 if (memory == V4L2_MEMORY_MMAP)
245 __setup_offsets(q, buffer);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300246
247 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300248 buffer, num_planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300249
250 return buffer;
251}
252
253/**
254 * __vb2_free_mem() - release all video buffer memory for a given queue
255 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300256static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300257{
258 unsigned int buffer;
259 struct vb2_buffer *vb;
260
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300261 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
262 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300263 vb = q->bufs[buffer];
264 if (!vb)
265 continue;
266
267 /* Free MMAP buffers or release USERPTR buffers */
268 if (q->memory == V4L2_MEMORY_MMAP)
269 __vb2_buf_mem_free(vb);
Sumit Semwalc5384042012-06-14 10:37:37 -0300270 else if (q->memory == V4L2_MEMORY_DMABUF)
271 __vb2_buf_dmabuf_put(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300272 else
273 __vb2_buf_userptr_put(vb);
274 }
275}
276
277/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300278 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
279 * related information, if no buffers are left return the queue to an
280 * uninitialized state. Might be called even if the queue has already been freed.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300281 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300282static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300283{
284 unsigned int buffer;
285
286 /* Call driver-provided cleanup function for each buffer, if provided */
287 if (q->ops->buf_cleanup) {
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300288 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
289 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300290 if (NULL == q->bufs[buffer])
291 continue;
292 q->ops->buf_cleanup(q->bufs[buffer]);
293 }
294 }
295
296 /* Release video buffer memory */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300297 __vb2_free_mem(q, buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300298
299 /* Free videobuf buffers */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300300 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
301 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300302 kfree(q->bufs[buffer]);
303 q->bufs[buffer] = NULL;
304 }
305
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300306 q->num_buffers -= buffers;
307 if (!q->num_buffers)
308 q->memory = 0;
Marek Szyprowskibd50d992011-10-25 03:07:59 -0300309 INIT_LIST_HEAD(&q->queued_list);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300310}
311
312/**
313 * __verify_planes_array() - verify that the planes array passed in struct
314 * v4l2_buffer from userspace can be safely used
315 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300316static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300317{
Hans Verkuil32a77262012-09-28 06:12:53 -0300318 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
319 return 0;
320
Pawel Osciake23ccc02010-10-11 10:56:41 -0300321 /* Is memory for copying plane information present? */
322 if (NULL == b->m.planes) {
323 dprintk(1, "Multi-planar buffer passed but "
324 "planes array not provided\n");
325 return -EINVAL;
326 }
327
328 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
329 dprintk(1, "Incorrect planes array length, "
330 "expected %d, got %d\n", vb->num_planes, b->length);
331 return -EINVAL;
332 }
333
334 return 0;
335}
336
337/**
Laurent Pinchart8023ed02012-07-10 10:41:40 -0300338 * __verify_length() - Verify that the bytesused value for each plane fits in
339 * the plane length and that the data offset doesn't exceed the bytesused value.
340 */
341static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
342{
343 unsigned int length;
344 unsigned int plane;
345
346 if (!V4L2_TYPE_IS_OUTPUT(b->type))
347 return 0;
348
349 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
350 for (plane = 0; plane < vb->num_planes; ++plane) {
351 length = (b->memory == V4L2_MEMORY_USERPTR)
352 ? b->m.planes[plane].length
353 : vb->v4l2_planes[plane].length;
354
355 if (b->m.planes[plane].bytesused > length)
356 return -EINVAL;
Sylwester Nawrocki3c5c23c2013-08-26 11:47:09 -0300357
358 if (b->m.planes[plane].data_offset > 0 &&
359 b->m.planes[plane].data_offset >=
Laurent Pinchart8023ed02012-07-10 10:41:40 -0300360 b->m.planes[plane].bytesused)
361 return -EINVAL;
362 }
363 } else {
364 length = (b->memory == V4L2_MEMORY_USERPTR)
365 ? b->length : vb->v4l2_planes[0].length;
366
367 if (b->bytesused > length)
368 return -EINVAL;
369 }
370
371 return 0;
372}
373
374/**
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300375 * __buffer_in_use() - return true if the buffer is in use and
376 * the queue cannot be freed (by the means of REQBUFS(0)) call
377 */
378static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
379{
380 unsigned int plane;
381 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski2c2dd6ac2011-10-12 13:09:53 -0300382 void *mem_priv = vb->planes[plane].mem_priv;
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300383 /*
384 * If num_users() has not been provided, call_memop
385 * will return 0, apparently nobody cares about this
386 * case anyway. If num_users() returns more than 1,
387 * we are not the only user of the plane's memory.
388 */
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300389 if (mem_priv && call_memop(q, num_users, mem_priv) > 1)
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300390 return true;
391 }
392 return false;
393}
394
395/**
396 * __buffers_in_use() - return true if any buffers on the queue are in use and
397 * the queue cannot be freed (by the means of REQBUFS(0)) call
398 */
399static bool __buffers_in_use(struct vb2_queue *q)
400{
401 unsigned int buffer;
402 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
403 if (__buffer_in_use(q, q->bufs[buffer]))
404 return true;
405 }
406 return false;
407}
408
409/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300410 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
411 * returned to userspace
412 */
Hans Verkuil32a77262012-09-28 06:12:53 -0300413static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300414{
415 struct vb2_queue *q = vb->vb2_queue;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300416
Sakari Ailus2b719d72012-05-02 09:40:03 -0300417 /* Copy back data such as timestamp, flags, etc. */
Pawel Osciake23ccc02010-10-11 10:56:41 -0300418 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
Sakari Ailus2b719d72012-05-02 09:40:03 -0300419 b->reserved2 = vb->v4l2_buf.reserved2;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300420 b->reserved = vb->v4l2_buf.reserved;
421
422 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300423 /*
424 * Fill in plane-related data if userspace provided an array
Hans Verkuil32a77262012-09-28 06:12:53 -0300425 * for it. The caller has already verified memory and size.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300426 */
Hans Verkuil3c0b6062012-09-28 06:24:18 -0300427 b->length = vb->num_planes;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300428 memcpy(b->m.planes, vb->v4l2_planes,
429 b->length * sizeof(struct v4l2_plane));
430 } else {
431 /*
432 * We use length and offset in v4l2_planes array even for
433 * single-planar buffers, but userspace does not.
434 */
435 b->length = vb->v4l2_planes[0].length;
436 b->bytesused = vb->v4l2_planes[0].bytesused;
437 if (q->memory == V4L2_MEMORY_MMAP)
438 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
439 else if (q->memory == V4L2_MEMORY_USERPTR)
440 b->m.userptr = vb->v4l2_planes[0].m.userptr;
Sumit Semwalc5384042012-06-14 10:37:37 -0300441 else if (q->memory == V4L2_MEMORY_DMABUF)
442 b->m.fd = vb->v4l2_planes[0].m.fd;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300443 }
444
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300445 /*
446 * Clear any buffer state related flags.
447 */
Sakari Ailus1b18e7a2012-10-22 17:10:16 -0300448 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
Kamil Debski6aa69f92013-01-25 06:29:57 -0300449 b->flags |= q->timestamp_type;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300450
451 switch (vb->state) {
452 case VB2_BUF_STATE_QUEUED:
453 case VB2_BUF_STATE_ACTIVE:
454 b->flags |= V4L2_BUF_FLAG_QUEUED;
455 break;
456 case VB2_BUF_STATE_ERROR:
457 b->flags |= V4L2_BUF_FLAG_ERROR;
458 /* fall through */
459 case VB2_BUF_STATE_DONE:
460 b->flags |= V4L2_BUF_FLAG_DONE;
461 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300462 case VB2_BUF_STATE_PREPARED:
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300463 b->flags |= V4L2_BUF_FLAG_PREPARED;
464 break;
465 case VB2_BUF_STATE_DEQUEUED:
Pawel Osciake23ccc02010-10-11 10:56:41 -0300466 /* nothing */
467 break;
468 }
469
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300470 if (__buffer_in_use(q, vb))
Pawel Osciake23ccc02010-10-11 10:56:41 -0300471 b->flags |= V4L2_BUF_FLAG_MAPPED;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300472}
473
474/**
475 * vb2_querybuf() - query video buffer information
476 * @q: videobuf queue
477 * @b: buffer struct passed from userspace to vidioc_querybuf handler
478 * in driver
479 *
480 * Should be called from vidioc_querybuf ioctl handler in driver.
481 * This function will verify the passed v4l2_buffer structure and fill the
482 * relevant information for the userspace.
483 *
484 * The return values from this function are intended to be directly returned
485 * from vidioc_querybuf handler in driver.
486 */
487int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
488{
489 struct vb2_buffer *vb;
Hans Verkuil32a77262012-09-28 06:12:53 -0300490 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300491
492 if (b->type != q->type) {
493 dprintk(1, "querybuf: wrong buffer type\n");
494 return -EINVAL;
495 }
496
497 if (b->index >= q->num_buffers) {
498 dprintk(1, "querybuf: buffer index out of range\n");
499 return -EINVAL;
500 }
501 vb = q->bufs[b->index];
Hans Verkuil32a77262012-09-28 06:12:53 -0300502 ret = __verify_planes_array(vb, b);
503 if (!ret)
504 __fill_v4l2_buffer(vb, b);
505 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300506}
507EXPORT_SYMBOL(vb2_querybuf);
508
509/**
510 * __verify_userptr_ops() - verify that all memory operations required for
511 * USERPTR queue type have been provided
512 */
513static int __verify_userptr_ops(struct vb2_queue *q)
514{
515 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
516 !q->mem_ops->put_userptr)
517 return -EINVAL;
518
519 return 0;
520}
521
522/**
523 * __verify_mmap_ops() - verify that all memory operations required for
524 * MMAP queue type have been provided
525 */
526static int __verify_mmap_ops(struct vb2_queue *q)
527{
528 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
529 !q->mem_ops->put || !q->mem_ops->mmap)
530 return -EINVAL;
531
532 return 0;
533}
534
535/**
Sumit Semwalc5384042012-06-14 10:37:37 -0300536 * __verify_dmabuf_ops() - verify that all memory operations required for
537 * DMABUF queue type have been provided
538 */
539static int __verify_dmabuf_ops(struct vb2_queue *q)
540{
541 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
542 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
543 !q->mem_ops->unmap_dmabuf)
544 return -EINVAL;
545
546 return 0;
547}
548
549/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300550 * __verify_memory_type() - Check whether the memory type and buffer type
551 * passed to a buffer operation are compatible with the queue.
552 */
553static int __verify_memory_type(struct vb2_queue *q,
554 enum v4l2_memory memory, enum v4l2_buf_type type)
555{
Sumit Semwalc5384042012-06-14 10:37:37 -0300556 if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR &&
557 memory != V4L2_MEMORY_DMABUF) {
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300558 dprintk(1, "reqbufs: unsupported memory type\n");
559 return -EINVAL;
560 }
561
562 if (type != q->type) {
563 dprintk(1, "reqbufs: requested type is incorrect\n");
564 return -EINVAL;
565 }
566
567 /*
568 * Make sure all the required memory ops for given memory type
569 * are available.
570 */
571 if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
572 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
573 return -EINVAL;
574 }
575
576 if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
577 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
578 return -EINVAL;
579 }
580
Sumit Semwalc5384042012-06-14 10:37:37 -0300581 if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
582 dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
583 return -EINVAL;
584 }
585
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300586 /*
587 * Place the busy tests at the end: -EBUSY can be ignored when
588 * create_bufs is called with count == 0, but count == 0 should still
589 * do the memory and type validation.
590 */
591 if (q->fileio) {
592 dprintk(1, "reqbufs: file io in progress\n");
593 return -EBUSY;
594 }
595 return 0;
596}
597
598/**
599 * __reqbufs() - Initiate streaming
Pawel Osciake23ccc02010-10-11 10:56:41 -0300600 * @q: videobuf2 queue
601 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
602 *
603 * Should be called from vidioc_reqbufs ioctl handler of a driver.
604 * This function:
605 * 1) verifies streaming parameters passed from the userspace,
606 * 2) sets up the queue,
607 * 3) negotiates number of buffers and planes per buffer with the driver
608 * to be used during streaming,
609 * 4) allocates internal buffer structures (struct vb2_buffer), according to
610 * the agreed parameters,
611 * 5) for MMAP memory type, allocates actual video memory, using the
612 * memory handling/allocation routines provided during queue initialization
613 *
614 * If req->count is 0, all the memory will be freed instead.
615 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
616 * and the queue is not busy, memory will be reallocated.
617 *
618 * The return values from this function are intended to be directly returned
619 * from vidioc_reqbufs handler in driver.
620 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300621static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300622{
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300623 unsigned int num_buffers, allocated_buffers, num_planes = 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300624 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300625
626 if (q->streaming) {
627 dprintk(1, "reqbufs: streaming active\n");
628 return -EBUSY;
629 }
630
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300631 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300632 /*
633 * We already have buffers allocated, so first check if they
634 * are not in use and can be freed.
635 */
636 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
637 dprintk(1, "reqbufs: memory in use, cannot free\n");
638 return -EBUSY;
639 }
640
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300641 __vb2_queue_free(q, q->num_buffers);
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300642
643 /*
644 * In case of REQBUFS(0) return immediately without calling
645 * driver's queue_setup() callback and allocating resources.
646 */
647 if (req->count == 0)
648 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300649 }
650
651 /*
652 * Make sure the requested values and current defaults are sane.
653 */
654 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300655 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
Pawel Osciake23ccc02010-10-11 10:56:41 -0300656 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
Marek Szyprowski13b14092011-04-14 07:17:44 -0300657 q->memory = req->memory;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300658
659 /*
660 * Ask the driver how many buffers and planes per buffer it requires.
661 * Driver also sets the size and allocator context for each plane.
662 */
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300663 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300664 q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300665 if (ret)
666 return ret;
667
668 /* Finally, allocate buffers and video memory */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300669 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
Marek Szyprowski66072d42011-06-28 08:29:02 -0300670 if (ret == 0) {
671 dprintk(1, "Memory allocation failed\n");
672 return -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300673 }
674
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300675 allocated_buffers = ret;
676
Pawel Osciake23ccc02010-10-11 10:56:41 -0300677 /*
678 * Check if driver can handle the allocated number of buffers.
679 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300680 if (allocated_buffers < num_buffers) {
681 num_buffers = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300682
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300683 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
684 &num_planes, q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300685
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300686 if (!ret && allocated_buffers < num_buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300687 ret = -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300688
689 /*
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300690 * Either the driver has accepted a smaller number of buffers,
691 * or .queue_setup() returned an error
Pawel Osciake23ccc02010-10-11 10:56:41 -0300692 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300693 }
694
695 q->num_buffers = allocated_buffers;
696
697 if (ret < 0) {
698 __vb2_queue_free(q, allocated_buffers);
699 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300700 }
701
Pawel Osciake23ccc02010-10-11 10:56:41 -0300702 /*
703 * Return the number of successfully allocated buffers
704 * to the userspace.
705 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300706 req->count = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300707
708 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300709}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300710
711/**
712 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
713 * type values.
714 * @q: videobuf2 queue
715 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
716 */
717int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
718{
719 int ret = __verify_memory_type(q, req->memory, req->type);
720
721 return ret ? ret : __reqbufs(q, req);
722}
Pawel Osciake23ccc02010-10-11 10:56:41 -0300723EXPORT_SYMBOL_GPL(vb2_reqbufs);
724
725/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300726 * __create_bufs() - Allocate buffers and any required auxiliary structs
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300727 * @q: videobuf2 queue
728 * @create: creation parameters, passed from userspace to vidioc_create_bufs
729 * handler in driver
730 *
731 * Should be called from vidioc_create_bufs ioctl handler of a driver.
732 * This function:
733 * 1) verifies parameter sanity
734 * 2) calls the .queue_setup() queue operation
735 * 3) performs any necessary memory allocations
736 *
737 * The return values from this function are intended to be directly returned
738 * from vidioc_create_bufs handler in driver.
739 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300740static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300741{
742 unsigned int num_planes = 0, num_buffers, allocated_buffers;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300743 int ret;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300744
745 if (q->num_buffers == VIDEO_MAX_FRAME) {
746 dprintk(1, "%s(): maximum number of buffers already allocated\n",
747 __func__);
748 return -ENOBUFS;
749 }
750
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300751 if (!q->num_buffers) {
752 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
753 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
754 q->memory = create->memory;
755 }
756
757 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
758
759 /*
760 * Ask the driver, whether the requested number of buffers, planes per
761 * buffer and their sizes are acceptable
762 */
763 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
764 &num_planes, q->plane_sizes, q->alloc_ctx);
765 if (ret)
766 return ret;
767
768 /* Finally, allocate buffers and video memory */
769 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
770 num_planes);
Hans Verkuilf05393d22012-06-22 05:44:14 -0300771 if (ret == 0) {
772 dprintk(1, "Memory allocation failed\n");
773 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300774 }
775
776 allocated_buffers = ret;
777
778 /*
779 * Check if driver can handle the so far allocated number of buffers.
780 */
781 if (ret < num_buffers) {
782 num_buffers = ret;
783
784 /*
785 * q->num_buffers contains the total number of buffers, that the
786 * queue driver has set up
787 */
788 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
789 &num_planes, q->plane_sizes, q->alloc_ctx);
790
791 if (!ret && allocated_buffers < num_buffers)
792 ret = -ENOMEM;
793
794 /*
795 * Either the driver has accepted a smaller number of buffers,
796 * or .queue_setup() returned an error
797 */
798 }
799
800 q->num_buffers += allocated_buffers;
801
802 if (ret < 0) {
803 __vb2_queue_free(q, allocated_buffers);
Hans Verkuilf05393d22012-06-22 05:44:14 -0300804 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300805 }
806
807 /*
808 * Return the number of successfully allocated buffers
809 * to the userspace.
810 */
811 create->count = allocated_buffers;
812
813 return 0;
814}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300815
816/**
Nicolas THERY53aa3b12012-07-20 09:25:37 -0300817 * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
818 * memory and type values.
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300819 * @q: videobuf2 queue
820 * @create: creation parameters, passed from userspace to vidioc_create_bufs
821 * handler in driver
822 */
823int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
824{
825 int ret = __verify_memory_type(q, create->memory, create->format.type);
826
827 create->index = q->num_buffers;
Hans Verkuilf05393d22012-06-22 05:44:14 -0300828 if (create->count == 0)
829 return ret != -EBUSY ? ret : 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300830 return ret ? ret : __create_bufs(q, create);
831}
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300832EXPORT_SYMBOL_GPL(vb2_create_bufs);
833
834/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300835 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
836 * @vb: vb2_buffer to which the plane in question belongs to
837 * @plane_no: plane number for which the address is to be returned
838 *
839 * This function returns a kernel virtual address of a given plane if
840 * such a mapping exist, NULL otherwise.
841 */
842void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
843{
844 struct vb2_queue *q = vb->vb2_queue;
845
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300846 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300847 return NULL;
848
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300849 return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300850
851}
852EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
853
854/**
855 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
856 * @vb: vb2_buffer to which the plane in question belongs to
857 * @plane_no: plane number for which the cookie is to be returned
858 *
859 * This function returns an allocator specific cookie for a given plane if
860 * available, NULL otherwise. The allocator should provide some simple static
861 * inline function, which would convert this cookie to the allocator specific
862 * type that can be used directly by the driver to access the buffer. This can
863 * be for example physical address, pointer to scatter list or IOMMU mapping.
864 */
865void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
866{
867 struct vb2_queue *q = vb->vb2_queue;
868
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300869 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300870 return NULL;
871
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300872 return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300873}
874EXPORT_SYMBOL_GPL(vb2_plane_cookie);
875
876/**
877 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
878 * @vb: vb2_buffer returned from the driver
879 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
880 * or VB2_BUF_STATE_ERROR if the operation finished with an error
881 *
882 * This function should be called by the driver after a hardware operation on
883 * a buffer is finished and the buffer may be returned to userspace. The driver
884 * cannot use this buffer anymore until it is queued back to it by videobuf
885 * by the means of buf_queue callback. Only buffers previously queued to the
886 * driver by buf_queue can be passed to this function.
887 */
888void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
889{
890 struct vb2_queue *q = vb->vb2_queue;
891 unsigned long flags;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -0300892 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300893
894 if (vb->state != VB2_BUF_STATE_ACTIVE)
895 return;
896
897 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
898 return;
899
900 dprintk(4, "Done processing on buffer %d, state: %d\n",
Tushar Behera9b6f5dc2012-11-12 04:01:29 -0300901 vb->v4l2_buf.index, state);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300902
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -0300903 /* sync buffers */
904 for (plane = 0; plane < vb->num_planes; ++plane)
905 call_memop(q, finish, vb->planes[plane].mem_priv);
906
Pawel Osciake23ccc02010-10-11 10:56:41 -0300907 /* Add the buffer to the done buffers list */
908 spin_lock_irqsave(&q->done_lock, flags);
909 vb->state = state;
910 list_add_tail(&vb->done_entry, &q->done_list);
911 atomic_dec(&q->queued_count);
912 spin_unlock_irqrestore(&q->done_lock, flags);
913
914 /* Inform any processes that may be waiting for buffers */
915 wake_up(&q->done_wq);
916}
917EXPORT_SYMBOL_GPL(vb2_buffer_done);
918
919/**
Hans Verkuil32a77262012-09-28 06:12:53 -0300920 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
921 * v4l2_buffer by the userspace. The caller has already verified that struct
922 * v4l2_buffer has a valid number of planes.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300923 */
Hans Verkuil32a77262012-09-28 06:12:53 -0300924static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
Pawel Osciake23ccc02010-10-11 10:56:41 -0300925 struct v4l2_plane *v4l2_planes)
926{
927 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300928
929 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300930 /* Fill in driver-provided information for OUTPUT types */
931 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
932 /*
933 * Will have to go up to b->length when API starts
934 * accepting variable number of planes.
935 */
936 for (plane = 0; plane < vb->num_planes; ++plane) {
937 v4l2_planes[plane].bytesused =
938 b->m.planes[plane].bytesused;
939 v4l2_planes[plane].data_offset =
940 b->m.planes[plane].data_offset;
941 }
942 }
943
944 if (b->memory == V4L2_MEMORY_USERPTR) {
945 for (plane = 0; plane < vb->num_planes; ++plane) {
946 v4l2_planes[plane].m.userptr =
947 b->m.planes[plane].m.userptr;
948 v4l2_planes[plane].length =
949 b->m.planes[plane].length;
950 }
951 }
Sumit Semwalc5384042012-06-14 10:37:37 -0300952 if (b->memory == V4L2_MEMORY_DMABUF) {
953 for (plane = 0; plane < vb->num_planes; ++plane) {
954 v4l2_planes[plane].m.fd =
955 b->m.planes[plane].m.fd;
956 v4l2_planes[plane].length =
957 b->m.planes[plane].length;
958 v4l2_planes[plane].data_offset =
959 b->m.planes[plane].data_offset;
960 }
961 }
Pawel Osciake23ccc02010-10-11 10:56:41 -0300962 } else {
963 /*
964 * Single-planar buffers do not use planes array,
965 * so fill in relevant v4l2_buffer struct fields instead.
966 * In videobuf we use our internal V4l2_planes struct for
967 * single-planar buffers as well, for simplicity.
968 */
Laurent Pinchartac706bf2012-12-17 07:38:16 -0300969 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300970 v4l2_planes[0].bytesused = b->bytesused;
Laurent Pinchartac706bf2012-12-17 07:38:16 -0300971 v4l2_planes[0].data_offset = 0;
972 }
Pawel Osciake23ccc02010-10-11 10:56:41 -0300973
974 if (b->memory == V4L2_MEMORY_USERPTR) {
975 v4l2_planes[0].m.userptr = b->m.userptr;
976 v4l2_planes[0].length = b->length;
977 }
Sumit Semwalc5384042012-06-14 10:37:37 -0300978
979 if (b->memory == V4L2_MEMORY_DMABUF) {
980 v4l2_planes[0].m.fd = b->m.fd;
981 v4l2_planes[0].length = b->length;
982 v4l2_planes[0].data_offset = 0;
983 }
984
Pawel Osciake23ccc02010-10-11 10:56:41 -0300985 }
986
987 vb->v4l2_buf.field = b->field;
988 vb->v4l2_buf.timestamp = b->timestamp;
Sakari Ailus1b18e7a2012-10-22 17:10:16 -0300989 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300990}
991
992/**
993 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
994 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300995static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300996{
997 struct v4l2_plane planes[VIDEO_MAX_PLANES];
998 struct vb2_queue *q = vb->vb2_queue;
999 void *mem_priv;
1000 unsigned int plane;
1001 int ret;
1002 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1003
Hans Verkuil32a77262012-09-28 06:12:53 -03001004 /* Copy relevant information provided by the userspace */
1005 __fill_vb2_buffer(vb, b, planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001006
1007 for (plane = 0; plane < vb->num_planes; ++plane) {
1008 /* Skip the plane if already verified */
Marek Szyprowskif0b7c7f2011-11-16 15:09:40 -03001009 if (vb->v4l2_planes[plane].m.userptr &&
1010 vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
Pawel Osciake23ccc02010-10-11 10:56:41 -03001011 && vb->v4l2_planes[plane].length == planes[plane].length)
1012 continue;
1013
1014 dprintk(3, "qbuf: userspace address for plane %d changed, "
1015 "reacquiring memory\n", plane);
1016
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001017 /* Check if the provided plane buffer is large enough */
1018 if (planes[plane].length < q->plane_sizes[plane]) {
Seung-Woo Kim2484a7e2013-08-20 04:48:06 -03001019 dprintk(1, "qbuf: provided buffer size %u is less than "
1020 "setup size %u for plane %d\n",
1021 planes[plane].length,
1022 q->plane_sizes[plane], plane);
Marek Szyprowski4c2625d2011-10-03 03:21:45 -03001023 ret = -EINVAL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001024 goto err;
1025 }
1026
Pawel Osciake23ccc02010-10-11 10:56:41 -03001027 /* Release previously acquired memory if present */
1028 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -03001029 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001030
1031 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001032 vb->v4l2_planes[plane].m.userptr = 0;
1033 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001034
1035 /* Acquire each plane's memory */
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001036 mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
1037 planes[plane].m.userptr,
1038 planes[plane].length, write);
1039 if (IS_ERR_OR_NULL(mem_priv)) {
1040 dprintk(1, "qbuf: failed acquiring userspace "
Pawel Osciake23ccc02010-10-11 10:56:41 -03001041 "memory for plane %d\n", plane);
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001042 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
1043 goto err;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001044 }
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001045 vb->planes[plane].mem_priv = mem_priv;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001046 }
1047
1048 /*
1049 * Call driver-specific initialization on the newly acquired buffer,
1050 * if provided.
1051 */
1052 ret = call_qop(q, buf_init, vb);
1053 if (ret) {
1054 dprintk(1, "qbuf: buffer initialization failed\n");
1055 goto err;
1056 }
1057
1058 /*
1059 * Now that everything is in order, copy relevant information
1060 * provided by userspace.
1061 */
1062 for (plane = 0; plane < vb->num_planes; ++plane)
1063 vb->v4l2_planes[plane] = planes[plane];
1064
1065 return 0;
1066err:
1067 /* In case of errors, release planes that were already acquired */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001068 for (plane = 0; plane < vb->num_planes; ++plane) {
1069 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -03001070 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001071 vb->planes[plane].mem_priv = NULL;
1072 vb->v4l2_planes[plane].m.userptr = 0;
1073 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001074 }
1075
1076 return ret;
1077}
1078
1079/**
1080 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1081 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001082static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001083{
Hans Verkuil32a77262012-09-28 06:12:53 -03001084 __fill_vb2_buffer(vb, b, vb->v4l2_planes);
1085 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001086}
1087
1088/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001089 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1090 */
1091static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1092{
1093 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1094 struct vb2_queue *q = vb->vb2_queue;
1095 void *mem_priv;
1096 unsigned int plane;
1097 int ret;
1098 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1099
1100 /* Verify and copy relevant information provided by the userspace */
1101 __fill_vb2_buffer(vb, b, planes);
1102
1103 for (plane = 0; plane < vb->num_planes; ++plane) {
1104 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1105
1106 if (IS_ERR_OR_NULL(dbuf)) {
1107 dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
1108 plane);
1109 ret = -EINVAL;
1110 goto err;
1111 }
1112
1113 /* use DMABUF size if length is not provided */
1114 if (planes[plane].length == 0)
1115 planes[plane].length = dbuf->size;
1116
1117 if (planes[plane].length < planes[plane].data_offset +
1118 q->plane_sizes[plane]) {
Seung-Woo Kim77c07822013-11-29 04:50:29 -03001119 dprintk(1, "qbuf: invalid dmabuf length for plane %d\n",
1120 plane);
Sumit Semwalc5384042012-06-14 10:37:37 -03001121 ret = -EINVAL;
1122 goto err;
1123 }
1124
1125 /* Skip the plane if already verified */
1126 if (dbuf == vb->planes[plane].dbuf &&
1127 vb->v4l2_planes[plane].length == planes[plane].length) {
1128 dma_buf_put(dbuf);
1129 continue;
1130 }
1131
1132 dprintk(1, "qbuf: buffer for plane %d changed\n", plane);
1133
1134 /* Release previously acquired memory if present */
1135 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
1136 memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
1137
1138 /* Acquire each plane's memory */
1139 mem_priv = call_memop(q, attach_dmabuf, q->alloc_ctx[plane],
1140 dbuf, planes[plane].length, write);
1141 if (IS_ERR(mem_priv)) {
1142 dprintk(1, "qbuf: failed to attach dmabuf\n");
1143 ret = PTR_ERR(mem_priv);
1144 dma_buf_put(dbuf);
1145 goto err;
1146 }
1147
1148 vb->planes[plane].dbuf = dbuf;
1149 vb->planes[plane].mem_priv = mem_priv;
1150 }
1151
1152 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1153 * really we want to do this just before the DMA, not while queueing
1154 * the buffer(s)..
1155 */
1156 for (plane = 0; plane < vb->num_planes; ++plane) {
1157 ret = call_memop(q, map_dmabuf, vb->planes[plane].mem_priv);
1158 if (ret) {
1159 dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
1160 plane);
1161 goto err;
1162 }
1163 vb->planes[plane].dbuf_mapped = 1;
1164 }
1165
1166 /*
1167 * Call driver-specific initialization on the newly acquired buffer,
1168 * if provided.
1169 */
1170 ret = call_qop(q, buf_init, vb);
1171 if (ret) {
1172 dprintk(1, "qbuf: buffer initialization failed\n");
1173 goto err;
1174 }
1175
1176 /*
1177 * Now that everything is in order, copy relevant information
1178 * provided by userspace.
1179 */
1180 for (plane = 0; plane < vb->num_planes; ++plane)
1181 vb->v4l2_planes[plane] = planes[plane];
1182
1183 return 0;
1184err:
1185 /* In case of errors, release planes that were already acquired */
1186 __vb2_buf_dmabuf_put(vb);
1187
1188 return ret;
1189}
1190
1191/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001192 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1193 */
1194static void __enqueue_in_driver(struct vb2_buffer *vb)
1195{
1196 struct vb2_queue *q = vb->vb2_queue;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001197 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001198
1199 vb->state = VB2_BUF_STATE_ACTIVE;
1200 atomic_inc(&q->queued_count);
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001201
1202 /* sync buffers */
1203 for (plane = 0; plane < vb->num_planes; ++plane)
1204 call_memop(q, prepare, vb->planes[plane].mem_priv);
1205
Pawel Osciake23ccc02010-10-11 10:56:41 -03001206 q->ops->buf_queue(vb);
1207}
1208
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001209static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001210{
1211 struct vb2_queue *q = vb->vb2_queue;
1212 int ret;
1213
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001214 ret = __verify_length(vb, b);
Sylwester Nawrocki3a9621b2013-08-26 11:47:53 -03001215 if (ret < 0) {
1216 dprintk(1, "%s(): plane parameters verification failed: %d\n",
1217 __func__, ret);
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001218 return ret;
Sylwester Nawrocki3a9621b2013-08-26 11:47:53 -03001219 }
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001220
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001221 switch (q->memory) {
1222 case V4L2_MEMORY_MMAP:
1223 ret = __qbuf_mmap(vb, b);
1224 break;
1225 case V4L2_MEMORY_USERPTR:
1226 ret = __qbuf_userptr(vb, b);
1227 break;
Sumit Semwalc5384042012-06-14 10:37:37 -03001228 case V4L2_MEMORY_DMABUF:
1229 ret = __qbuf_dmabuf(vb, b);
1230 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001231 default:
1232 WARN(1, "Invalid queue type\n");
1233 ret = -EINVAL;
1234 }
1235
1236 if (!ret)
1237 ret = call_qop(q, buf_prepare, vb);
1238 if (ret)
1239 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
1240 else
1241 vb->state = VB2_BUF_STATE_PREPARED;
1242
1243 return ret;
1244}
1245
Laurent Pinchart012043b2013-08-09 08:11:26 -03001246static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
1247 const char *opname,
1248 int (*handler)(struct vb2_queue *,
1249 struct v4l2_buffer *,
1250 struct vb2_buffer *))
1251{
1252 struct rw_semaphore *mmap_sem = NULL;
1253 struct vb2_buffer *vb;
1254 int ret;
1255
1256 /*
1257 * In case of user pointer buffers vb2 allocators need to get direct
1258 * access to userspace pages. This requires getting the mmap semaphore
1259 * for read access in the current process structure. The same semaphore
1260 * is taken before calling mmap operation, while both qbuf/prepare_buf
1261 * and mmap are called by the driver or v4l2 core with the driver's lock
1262 * held. To avoid an AB-BA deadlock (mmap_sem then driver's lock in mmap
1263 * and driver's lock then mmap_sem in qbuf/prepare_buf) the videobuf2
1264 * core releases the driver's lock, takes mmap_sem and then takes the
1265 * driver's lock again.
1266 *
1267 * To avoid racing with other vb2 calls, which might be called after
1268 * releasing the driver's lock, this operation is performed at the
1269 * beginning of qbuf/prepare_buf processing. This way the queue status
1270 * is consistent after getting the driver's lock back.
1271 */
1272 if (q->memory == V4L2_MEMORY_USERPTR) {
1273 mmap_sem = &current->mm->mmap_sem;
1274 call_qop(q, wait_prepare, q);
1275 down_read(mmap_sem);
1276 call_qop(q, wait_finish, q);
1277 }
1278
1279 if (q->fileio) {
1280 dprintk(1, "%s(): file io in progress\n", opname);
1281 ret = -EBUSY;
1282 goto unlock;
1283 }
1284
1285 if (b->type != q->type) {
1286 dprintk(1, "%s(): invalid buffer type\n", opname);
1287 ret = -EINVAL;
1288 goto unlock;
1289 }
1290
1291 if (b->index >= q->num_buffers) {
1292 dprintk(1, "%s(): buffer index out of range\n", opname);
1293 ret = -EINVAL;
1294 goto unlock;
1295 }
1296
1297 vb = q->bufs[b->index];
1298 if (NULL == vb) {
1299 /* Should never happen */
1300 dprintk(1, "%s(): buffer is NULL\n", opname);
1301 ret = -EINVAL;
1302 goto unlock;
1303 }
1304
1305 if (b->memory != q->memory) {
1306 dprintk(1, "%s(): invalid memory type\n", opname);
1307 ret = -EINVAL;
1308 goto unlock;
1309 }
1310
1311 ret = __verify_planes_array(vb, b);
1312 if (ret)
1313 goto unlock;
1314
1315 ret = handler(q, b, vb);
1316 if (ret)
1317 goto unlock;
1318
1319 /* Fill buffer information for the userspace */
1320 __fill_v4l2_buffer(vb, b);
1321
1322 dprintk(1, "%s() of buffer %d succeeded\n", opname, vb->v4l2_buf.index);
1323unlock:
1324 if (mmap_sem)
1325 up_read(mmap_sem);
1326 return ret;
1327}
1328
1329static int __vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
1330 struct vb2_buffer *vb)
1331{
1332 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1333 dprintk(1, "%s(): invalid buffer state %d\n", __func__,
1334 vb->state);
1335 return -EINVAL;
1336 }
1337
1338 return __buf_prepare(vb, b);
1339}
1340
Pawel Osciake23ccc02010-10-11 10:56:41 -03001341/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001342 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1343 * @q: videobuf2 queue
1344 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1345 * handler in driver
1346 *
1347 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1348 * This function:
1349 * 1) verifies the passed buffer,
1350 * 2) calls buf_prepare callback in the driver (if provided), in which
1351 * driver-specific buffer initialization can be performed,
1352 *
1353 * The return values from this function are intended to be directly returned
1354 * from vidioc_prepare_buf handler in driver.
1355 */
1356int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1357{
Laurent Pinchart012043b2013-08-09 08:11:26 -03001358 return vb2_queue_or_prepare_buf(q, b, "prepare_buf", __vb2_prepare_buf);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001359}
1360EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1361
Laurent Pinchart012043b2013-08-09 08:11:26 -03001362static int __vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b,
1363 struct vb2_buffer *vb)
1364{
1365 int ret;
1366
1367 switch (vb->state) {
1368 case VB2_BUF_STATE_DEQUEUED:
1369 ret = __buf_prepare(vb, b);
1370 if (ret)
1371 return ret;
1372 case VB2_BUF_STATE_PREPARED:
1373 break;
1374 default:
1375 dprintk(1, "qbuf: buffer already in use\n");
1376 return -EINVAL;
1377 }
1378
1379 /*
1380 * Add to the queued buffers list, a buffer will stay on it until
1381 * dequeued in dqbuf.
1382 */
1383 list_add_tail(&vb->queued_entry, &q->queued_list);
1384 vb->state = VB2_BUF_STATE_QUEUED;
1385
1386 /*
1387 * If already streaming, give the buffer to driver for processing.
1388 * If not, the buffer will be given to driver on next streamon.
1389 */
1390 if (q->streaming)
1391 __enqueue_in_driver(vb);
1392
1393 return 0;
1394}
1395
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001396/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001397 * vb2_qbuf() - Queue a buffer from userspace
1398 * @q: videobuf2 queue
1399 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1400 * in driver
1401 *
1402 * Should be called from vidioc_qbuf ioctl handler of a driver.
1403 * This function:
1404 * 1) verifies the passed buffer,
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001405 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1406 * which driver-specific buffer initialization can be performed,
Pawel Osciake23ccc02010-10-11 10:56:41 -03001407 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1408 * callback for processing.
1409 *
1410 * The return values from this function are intended to be directly returned
1411 * from vidioc_qbuf handler in driver.
1412 */
1413int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1414{
Laurent Pinchart012043b2013-08-09 08:11:26 -03001415 return vb2_queue_or_prepare_buf(q, b, "qbuf", __vb2_qbuf);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001416}
1417EXPORT_SYMBOL_GPL(vb2_qbuf);
1418
1419/**
1420 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1421 * for dequeuing
1422 *
1423 * Will sleep if required for nonblocking == false.
1424 */
1425static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1426{
1427 /*
1428 * All operations on vb_done_list are performed under done_lock
1429 * spinlock protection. However, buffers may be removed from
1430 * it and returned to userspace only while holding both driver's
1431 * lock and the done_lock spinlock. Thus we can be sure that as
1432 * long as we hold the driver's lock, the list will remain not
1433 * empty if list_empty() check succeeds.
1434 */
1435
1436 for (;;) {
1437 int ret;
1438
1439 if (!q->streaming) {
1440 dprintk(1, "Streaming off, will not wait for buffers\n");
1441 return -EINVAL;
1442 }
1443
1444 if (!list_empty(&q->done_list)) {
1445 /*
1446 * Found a buffer that we were waiting for.
1447 */
1448 break;
1449 }
1450
1451 if (nonblocking) {
1452 dprintk(1, "Nonblocking and no buffers to dequeue, "
1453 "will not wait\n");
1454 return -EAGAIN;
1455 }
1456
1457 /*
1458 * We are streaming and blocking, wait for another buffer to
1459 * become ready or for streamoff. Driver's lock is released to
1460 * allow streamoff or qbuf to be called while waiting.
1461 */
1462 call_qop(q, wait_prepare, q);
1463
1464 /*
1465 * All locks have been released, it is safe to sleep now.
1466 */
1467 dprintk(3, "Will sleep waiting for buffers\n");
1468 ret = wait_event_interruptible(q->done_wq,
1469 !list_empty(&q->done_list) || !q->streaming);
1470
1471 /*
1472 * We need to reevaluate both conditions again after reacquiring
1473 * the locks or return an error if one occurred.
1474 */
1475 call_qop(q, wait_finish, q);
Hans Verkuil32a77262012-09-28 06:12:53 -03001476 if (ret) {
1477 dprintk(1, "Sleep was interrupted\n");
Pawel Osciake23ccc02010-10-11 10:56:41 -03001478 return ret;
Hans Verkuil32a77262012-09-28 06:12:53 -03001479 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001480 }
1481 return 0;
1482}
1483
1484/**
1485 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1486 *
1487 * Will sleep if required for nonblocking == false.
1488 */
1489static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
Hans Verkuil32a77262012-09-28 06:12:53 -03001490 struct v4l2_buffer *b, int nonblocking)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001491{
1492 unsigned long flags;
1493 int ret;
1494
1495 /*
1496 * Wait for at least one buffer to become available on the done_list.
1497 */
1498 ret = __vb2_wait_for_done_vb(q, nonblocking);
1499 if (ret)
1500 return ret;
1501
1502 /*
1503 * Driver's lock has been held since we last verified that done_list
1504 * is not empty, so no need for another list_empty(done_list) check.
1505 */
1506 spin_lock_irqsave(&q->done_lock, flags);
1507 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
Hans Verkuil32a77262012-09-28 06:12:53 -03001508 /*
1509 * Only remove the buffer from done_list if v4l2_buffer can handle all
1510 * the planes.
1511 */
1512 ret = __verify_planes_array(*vb, b);
1513 if (!ret)
1514 list_del(&(*vb)->done_entry);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001515 spin_unlock_irqrestore(&q->done_lock, flags);
1516
Hans Verkuil32a77262012-09-28 06:12:53 -03001517 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001518}
1519
1520/**
1521 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1522 * @q: videobuf2 queue
1523 *
1524 * This function will wait until all buffers that have been given to the driver
1525 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1526 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1527 * taken, for example from stop_streaming() callback.
1528 */
1529int vb2_wait_for_all_buffers(struct vb2_queue *q)
1530{
1531 if (!q->streaming) {
1532 dprintk(1, "Streaming off, will not wait for buffers\n");
1533 return -EINVAL;
1534 }
1535
1536 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1537 return 0;
1538}
1539EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1540
1541/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001542 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1543 */
1544static void __vb2_dqbuf(struct vb2_buffer *vb)
1545{
1546 struct vb2_queue *q = vb->vb2_queue;
1547 unsigned int i;
1548
1549 /* nothing to do if the buffer is already dequeued */
1550 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1551 return;
1552
1553 vb->state = VB2_BUF_STATE_DEQUEUED;
1554
1555 /* unmap DMABUF buffer */
1556 if (q->memory == V4L2_MEMORY_DMABUF)
1557 for (i = 0; i < vb->num_planes; ++i) {
1558 if (!vb->planes[i].dbuf_mapped)
1559 continue;
1560 call_memop(q, unmap_dmabuf, vb->planes[i].mem_priv);
1561 vb->planes[i].dbuf_mapped = 0;
1562 }
1563}
1564
1565/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001566 * vb2_dqbuf() - Dequeue a buffer to the userspace
1567 * @q: videobuf2 queue
1568 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1569 * in driver
1570 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1571 * buffers ready for dequeuing are present. Normally the driver
1572 * would be passing (file->f_flags & O_NONBLOCK) here
1573 *
1574 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1575 * This function:
1576 * 1) verifies the passed buffer,
1577 * 2) calls buf_finish callback in the driver (if provided), in which
1578 * driver can perform any additional operations that may be required before
1579 * returning the buffer to userspace, such as cache sync,
1580 * 3) the buffer struct members are filled with relevant information for
1581 * the userspace.
1582 *
1583 * The return values from this function are intended to be directly returned
1584 * from vidioc_dqbuf handler in driver.
1585 */
1586int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1587{
1588 struct vb2_buffer *vb = NULL;
1589 int ret;
1590
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001591 if (q->fileio) {
1592 dprintk(1, "dqbuf: file io in progress\n");
1593 return -EBUSY;
1594 }
1595
Pawel Osciake23ccc02010-10-11 10:56:41 -03001596 if (b->type != q->type) {
1597 dprintk(1, "dqbuf: invalid buffer type\n");
1598 return -EINVAL;
1599 }
Hans Verkuil32a77262012-09-28 06:12:53 -03001600 ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
1601 if (ret < 0)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001602 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001603
1604 ret = call_qop(q, buf_finish, vb);
1605 if (ret) {
1606 dprintk(1, "dqbuf: buffer finish failed\n");
1607 return ret;
1608 }
1609
1610 switch (vb->state) {
1611 case VB2_BUF_STATE_DONE:
1612 dprintk(3, "dqbuf: Returning done buffer\n");
1613 break;
1614 case VB2_BUF_STATE_ERROR:
1615 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1616 break;
1617 default:
1618 dprintk(1, "dqbuf: Invalid buffer state\n");
1619 return -EINVAL;
1620 }
1621
1622 /* Fill buffer information for the userspace */
1623 __fill_v4l2_buffer(vb, b);
1624 /* Remove from videobuf queue */
1625 list_del(&vb->queued_entry);
Sumit Semwalc5384042012-06-14 10:37:37 -03001626 /* go back to dequeued state */
1627 __vb2_dqbuf(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001628
1629 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1630 vb->v4l2_buf.index, vb->state);
1631
Pawel Osciake23ccc02010-10-11 10:56:41 -03001632 return 0;
1633}
1634EXPORT_SYMBOL_GPL(vb2_dqbuf);
1635
1636/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001637 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1638 *
1639 * Removes all queued buffers from driver's queue and all buffers queued by
1640 * userspace from videobuf's queue. Returns to state after reqbufs.
1641 */
1642static void __vb2_queue_cancel(struct vb2_queue *q)
1643{
1644 unsigned int i;
1645
1646 /*
1647 * Tell driver to stop all transactions and release all queued
1648 * buffers.
1649 */
1650 if (q->streaming)
1651 call_qop(q, stop_streaming, q);
1652 q->streaming = 0;
1653
1654 /*
1655 * Remove all buffers from videobuf's list...
1656 */
1657 INIT_LIST_HEAD(&q->queued_list);
1658 /*
1659 * ...and done list; userspace will not receive any buffers it
1660 * has not already dequeued before initiating cancel.
1661 */
1662 INIT_LIST_HEAD(&q->done_list);
Marek Szyprowskiafdea8b2011-06-10 08:58:42 -03001663 atomic_set(&q->queued_count, 0);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001664 wake_up_all(&q->done_wq);
1665
1666 /*
1667 * Reinitialize all buffers for next use.
1668 */
1669 for (i = 0; i < q->num_buffers; ++i)
Sumit Semwalc5384042012-06-14 10:37:37 -03001670 __vb2_dqbuf(q->bufs[i]);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001671}
1672
1673/**
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001674 * vb2_streamon - start streaming
1675 * @q: videobuf2 queue
1676 * @type: type argument passed from userspace to vidioc_streamon handler
1677 *
1678 * Should be called from vidioc_streamon handler of a driver.
1679 * This function:
1680 * 1) verifies current state
1681 * 2) passes any previously queued buffers to the driver and starts streaming
1682 *
1683 * The return values from this function are intended to be directly returned
1684 * from vidioc_streamon handler in the driver.
1685 */
1686int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1687{
1688 struct vb2_buffer *vb;
1689 int ret;
1690
1691 if (q->fileio) {
1692 dprintk(1, "streamon: file io in progress\n");
1693 return -EBUSY;
1694 }
1695
1696 if (type != q->type) {
1697 dprintk(1, "streamon: invalid stream type\n");
1698 return -EINVAL;
1699 }
1700
1701 if (q->streaming) {
Ricardo Ribaldaf9560352013-11-08 07:08:45 -03001702 dprintk(3, "streamon successful: already streaming\n");
1703 return 0;
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001704 }
1705
1706 /*
1707 * If any buffers were queued before streamon,
1708 * we can now pass them to driver for processing.
1709 */
1710 list_for_each_entry(vb, &q->queued_list, queued_entry)
1711 __enqueue_in_driver(vb);
1712
1713 /*
1714 * Let driver notice that streaming state has been enabled.
1715 */
1716 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1717 if (ret) {
1718 dprintk(1, "streamon: driver refused to start streaming\n");
1719 __vb2_queue_cancel(q);
1720 return ret;
1721 }
1722
1723 q->streaming = 1;
1724
1725 dprintk(3, "Streamon successful\n");
1726 return 0;
1727}
1728EXPORT_SYMBOL_GPL(vb2_streamon);
1729
1730
1731/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001732 * vb2_streamoff - stop streaming
1733 * @q: videobuf2 queue
1734 * @type: type argument passed from userspace to vidioc_streamoff handler
1735 *
1736 * Should be called from vidioc_streamoff handler of a driver.
1737 * This function:
1738 * 1) verifies current state,
1739 * 2) stop streaming and dequeues any queued buffers, including those previously
1740 * passed to the driver (after waiting for the driver to finish).
1741 *
1742 * This call can be used for pausing playback.
1743 * The return values from this function are intended to be directly returned
1744 * from vidioc_streamoff handler in the driver
1745 */
1746int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1747{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001748 if (q->fileio) {
1749 dprintk(1, "streamoff: file io in progress\n");
1750 return -EBUSY;
1751 }
1752
Pawel Osciake23ccc02010-10-11 10:56:41 -03001753 if (type != q->type) {
1754 dprintk(1, "streamoff: invalid stream type\n");
1755 return -EINVAL;
1756 }
1757
1758 if (!q->streaming) {
Ricardo Ribaldaf9560352013-11-08 07:08:45 -03001759 dprintk(3, "streamoff successful: not streaming\n");
1760 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001761 }
1762
1763 /*
1764 * Cancel will pause streaming and remove all buffers from the driver
1765 * and videobuf, effectively returning control over them to userspace.
1766 */
1767 __vb2_queue_cancel(q);
1768
1769 dprintk(3, "Streamoff successful\n");
1770 return 0;
1771}
1772EXPORT_SYMBOL_GPL(vb2_streamoff);
1773
1774/**
1775 * __find_plane_by_offset() - find plane associated with the given offset off
1776 */
1777static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1778 unsigned int *_buffer, unsigned int *_plane)
1779{
1780 struct vb2_buffer *vb;
1781 unsigned int buffer, plane;
1782
1783 /*
1784 * Go over all buffers and their planes, comparing the given offset
1785 * with an offset assigned to each plane. If a match is found,
1786 * return its buffer and plane numbers.
1787 */
1788 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1789 vb = q->bufs[buffer];
1790
1791 for (plane = 0; plane < vb->num_planes; ++plane) {
1792 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1793 *_buffer = buffer;
1794 *_plane = plane;
1795 return 0;
1796 }
1797 }
1798 }
1799
1800 return -EINVAL;
1801}
1802
1803/**
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03001804 * vb2_expbuf() - Export a buffer as a file descriptor
1805 * @q: videobuf2 queue
1806 * @eb: export buffer structure passed from userspace to vidioc_expbuf
1807 * handler in driver
1808 *
1809 * The return values from this function are intended to be directly returned
1810 * from vidioc_expbuf handler in driver.
1811 */
1812int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
1813{
1814 struct vb2_buffer *vb = NULL;
1815 struct vb2_plane *vb_plane;
1816 int ret;
1817 struct dma_buf *dbuf;
1818
1819 if (q->memory != V4L2_MEMORY_MMAP) {
1820 dprintk(1, "Queue is not currently set up for mmap\n");
1821 return -EINVAL;
1822 }
1823
1824 if (!q->mem_ops->get_dmabuf) {
1825 dprintk(1, "Queue does not support DMA buffer exporting\n");
1826 return -EINVAL;
1827 }
1828
Philipp Zabelea3aba82013-05-21 05:11:35 -03001829 if (eb->flags & ~(O_CLOEXEC | O_ACCMODE)) {
1830 dprintk(1, "Queue does support only O_CLOEXEC and access mode flags\n");
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03001831 return -EINVAL;
1832 }
1833
1834 if (eb->type != q->type) {
1835 dprintk(1, "qbuf: invalid buffer type\n");
1836 return -EINVAL;
1837 }
1838
1839 if (eb->index >= q->num_buffers) {
1840 dprintk(1, "buffer index out of range\n");
1841 return -EINVAL;
1842 }
1843
1844 vb = q->bufs[eb->index];
1845
1846 if (eb->plane >= vb->num_planes) {
1847 dprintk(1, "buffer plane out of range\n");
1848 return -EINVAL;
1849 }
1850
1851 vb_plane = &vb->planes[eb->plane];
1852
Philipp Zabelea3aba82013-05-21 05:11:35 -03001853 dbuf = call_memop(q, get_dmabuf, vb_plane->mem_priv, eb->flags & O_ACCMODE);
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03001854 if (IS_ERR_OR_NULL(dbuf)) {
1855 dprintk(1, "Failed to export buffer %d, plane %d\n",
1856 eb->index, eb->plane);
1857 return -EINVAL;
1858 }
1859
Philipp Zabelea3aba82013-05-21 05:11:35 -03001860 ret = dma_buf_fd(dbuf, eb->flags & ~O_ACCMODE);
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03001861 if (ret < 0) {
1862 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
1863 eb->index, eb->plane, ret);
1864 dma_buf_put(dbuf);
1865 return ret;
1866 }
1867
1868 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
1869 eb->index, eb->plane, ret);
1870 eb->fd = ret;
1871
1872 return 0;
1873}
1874EXPORT_SYMBOL_GPL(vb2_expbuf);
1875
1876/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001877 * vb2_mmap() - map video buffers into application address space
1878 * @q: videobuf2 queue
1879 * @vma: vma passed to the mmap file operation handler in the driver
1880 *
1881 * Should be called from mmap file operation handler of a driver.
1882 * This function maps one plane of one of the available video buffers to
1883 * userspace. To map whole video memory allocated on reqbufs, this function
1884 * has to be called once per each plane per each buffer previously allocated.
1885 *
1886 * When the userspace application calls mmap, it passes to it an offset returned
1887 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1888 * a "cookie", which is then used to identify the plane to be mapped.
1889 * This function finds a plane with a matching offset and a mapping is performed
1890 * by the means of a provided memory operation.
1891 *
1892 * The return values from this function are intended to be directly returned
1893 * from the mmap handler in driver.
1894 */
1895int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1896{
1897 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001898 struct vb2_buffer *vb;
1899 unsigned int buffer, plane;
1900 int ret;
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -03001901 unsigned long length;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001902
1903 if (q->memory != V4L2_MEMORY_MMAP) {
1904 dprintk(1, "Queue is not currently set up for mmap\n");
1905 return -EINVAL;
1906 }
1907
1908 /*
1909 * Check memory area access mode.
1910 */
1911 if (!(vma->vm_flags & VM_SHARED)) {
1912 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1913 return -EINVAL;
1914 }
1915 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1916 if (!(vma->vm_flags & VM_WRITE)) {
1917 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1918 return -EINVAL;
1919 }
1920 } else {
1921 if (!(vma->vm_flags & VM_READ)) {
1922 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1923 return -EINVAL;
1924 }
1925 }
1926
1927 /*
1928 * Find the plane corresponding to the offset passed by userspace.
1929 */
1930 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1931 if (ret)
1932 return ret;
1933
1934 vb = q->bufs[buffer];
Pawel Osciake23ccc02010-10-11 10:56:41 -03001935
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -03001936 /*
1937 * MMAP requires page_aligned buffers.
1938 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
1939 * so, we need to do the same here.
1940 */
1941 length = PAGE_ALIGN(vb->v4l2_planes[plane].length);
1942 if (length < (vma->vm_end - vma->vm_start)) {
1943 dprintk(1,
1944 "MMAP invalid, as it would overflow buffer length\n");
Seung-Woo Kim068a0df2013-04-11 23:57:57 -03001945 return -EINVAL;
1946 }
1947
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001948 ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001949 if (ret)
1950 return ret;
1951
Pawel Osciake23ccc02010-10-11 10:56:41 -03001952 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1953 return 0;
1954}
1955EXPORT_SYMBOL_GPL(vb2_mmap);
1956
Scott Jiang6f524ec2011-09-21 09:25:23 -03001957#ifndef CONFIG_MMU
1958unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1959 unsigned long addr,
1960 unsigned long len,
1961 unsigned long pgoff,
1962 unsigned long flags)
1963{
1964 unsigned long off = pgoff << PAGE_SHIFT;
1965 struct vb2_buffer *vb;
1966 unsigned int buffer, plane;
1967 int ret;
1968
1969 if (q->memory != V4L2_MEMORY_MMAP) {
1970 dprintk(1, "Queue is not currently set up for mmap\n");
1971 return -EINVAL;
1972 }
1973
1974 /*
1975 * Find the plane corresponding to the offset passed by userspace.
1976 */
1977 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1978 if (ret)
1979 return ret;
1980
1981 vb = q->bufs[buffer];
1982
1983 return (unsigned long)vb2_plane_vaddr(vb, plane);
1984}
1985EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
1986#endif
1987
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001988static int __vb2_init_fileio(struct vb2_queue *q, int read);
1989static int __vb2_cleanup_fileio(struct vb2_queue *q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001990
1991/**
1992 * vb2_poll() - implements poll userspace operation
1993 * @q: videobuf2 queue
1994 * @file: file argument passed to the poll file operation handler
1995 * @wait: wait argument passed to the poll file operation handler
1996 *
1997 * This function implements poll file operation handler for a driver.
1998 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1999 * be informed that the file descriptor of a video device is available for
2000 * reading.
2001 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
2002 * will be reported as available for writing.
2003 *
Hans Verkuil95213ce2011-07-13 04:26:52 -03002004 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
2005 * pending events.
2006 *
Pawel Osciake23ccc02010-10-11 10:56:41 -03002007 * The return values from this function are intended to be directly returned
2008 * from poll handler in driver.
2009 */
2010unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
2011{
Hans Verkuil95213ce2011-07-13 04:26:52 -03002012 struct video_device *vfd = video_devdata(file);
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002013 unsigned long req_events = poll_requested_events(wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002014 struct vb2_buffer *vb = NULL;
Hans Verkuil95213ce2011-07-13 04:26:52 -03002015 unsigned int res = 0;
2016 unsigned long flags;
2017
2018 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
2019 struct v4l2_fh *fh = file->private_data;
2020
2021 if (v4l2_event_pending(fh))
2022 res = POLLPRI;
2023 else if (req_events & POLLPRI)
2024 poll_wait(file, &fh->wait, wait);
2025 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03002026
Hans Verkuilcd138232013-01-30 13:29:02 -03002027 if (!V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLIN | POLLRDNORM)))
2028 return res;
2029 if (V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLOUT | POLLWRNORM)))
2030 return res;
2031
Pawel Osciake23ccc02010-10-11 10:56:41 -03002032 /*
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03002033 * Start file I/O emulator only if streaming API has not been used yet.
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002034 */
2035 if (q->num_buffers == 0 && q->fileio == NULL) {
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002036 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2037 (req_events & (POLLIN | POLLRDNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002038 if (__vb2_init_fileio(q, 1))
2039 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002040 }
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002041 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2042 (req_events & (POLLOUT | POLLWRNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002043 if (__vb2_init_fileio(q, 0))
2044 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002045 /*
2046 * Write to OUTPUT queue can be done immediately.
2047 */
Hans Verkuil95213ce2011-07-13 04:26:52 -03002048 return res | POLLOUT | POLLWRNORM;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002049 }
2050 }
2051
2052 /*
Pawel Osciake23ccc02010-10-11 10:56:41 -03002053 * There is nothing to wait for if no buffers have already been queued.
2054 */
2055 if (list_empty(&q->queued_list))
Hans Verkuil95213ce2011-07-13 04:26:52 -03002056 return res | POLLERR;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002057
Seung-Woo Kim412cb872013-05-20 23:47:29 -03002058 if (list_empty(&q->done_list))
2059 poll_wait(file, &q->done_wq, wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002060
2061 /*
2062 * Take first buffer available for dequeuing.
2063 */
2064 spin_lock_irqsave(&q->done_lock, flags);
2065 if (!list_empty(&q->done_list))
2066 vb = list_first_entry(&q->done_list, struct vb2_buffer,
2067 done_entry);
2068 spin_unlock_irqrestore(&q->done_lock, flags);
2069
2070 if (vb && (vb->state == VB2_BUF_STATE_DONE
2071 || vb->state == VB2_BUF_STATE_ERROR)) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002072 return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
2073 res | POLLOUT | POLLWRNORM :
2074 res | POLLIN | POLLRDNORM;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002075 }
Hans Verkuil95213ce2011-07-13 04:26:52 -03002076 return res;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002077}
2078EXPORT_SYMBOL_GPL(vb2_poll);
2079
2080/**
2081 * vb2_queue_init() - initialize a videobuf2 queue
2082 * @q: videobuf2 queue; this structure should be allocated in driver
2083 *
2084 * The vb2_queue structure should be allocated by the driver. The driver is
2085 * responsible of clearing it's content and setting initial values for some
2086 * required entries before calling this function.
2087 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2088 * to the struct vb2_queue description in include/media/videobuf2-core.h
2089 * for more information.
2090 */
2091int vb2_queue_init(struct vb2_queue *q)
2092{
Ezequiel Garcia896f38f2012-09-17 14:59:30 -03002093 /*
2094 * Sanity check
2095 */
2096 if (WARN_ON(!q) ||
2097 WARN_ON(!q->ops) ||
2098 WARN_ON(!q->mem_ops) ||
2099 WARN_ON(!q->type) ||
2100 WARN_ON(!q->io_modes) ||
2101 WARN_ON(!q->ops->queue_setup) ||
Kamil Debski6aa69f92013-01-25 06:29:57 -03002102 WARN_ON(!q->ops->buf_queue) ||
2103 WARN_ON(q->timestamp_type & ~V4L2_BUF_FLAG_TIMESTAMP_MASK))
Ezequiel Garcia896f38f2012-09-17 14:59:30 -03002104 return -EINVAL;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002105
Kamil Debski6aa69f92013-01-25 06:29:57 -03002106 /* Warn that the driver should choose an appropriate timestamp type */
2107 WARN_ON(q->timestamp_type == V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
2108
Pawel Osciake23ccc02010-10-11 10:56:41 -03002109 INIT_LIST_HEAD(&q->queued_list);
2110 INIT_LIST_HEAD(&q->done_list);
2111 spin_lock_init(&q->done_lock);
2112 init_waitqueue_head(&q->done_wq);
2113
2114 if (q->buf_struct_size == 0)
2115 q->buf_struct_size = sizeof(struct vb2_buffer);
2116
2117 return 0;
2118}
2119EXPORT_SYMBOL_GPL(vb2_queue_init);
2120
2121/**
2122 * vb2_queue_release() - stop streaming, release the queue and free memory
2123 * @q: videobuf2 queue
2124 *
2125 * This function stops streaming and performs necessary clean ups, including
2126 * freeing video buffer memory. The driver is responsible for freeing
2127 * the vb2_queue structure itself.
2128 */
2129void vb2_queue_release(struct vb2_queue *q)
2130{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002131 __vb2_cleanup_fileio(q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002132 __vb2_queue_cancel(q);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03002133 __vb2_queue_free(q, q->num_buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002134}
2135EXPORT_SYMBOL_GPL(vb2_queue_release);
2136
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002137/**
2138 * struct vb2_fileio_buf - buffer context used by file io emulator
2139 *
2140 * vb2 provides a compatibility layer and emulator of file io (read and
2141 * write) calls on top of streaming API. This structure is used for
2142 * tracking context related to the buffers.
2143 */
2144struct vb2_fileio_buf {
2145 void *vaddr;
2146 unsigned int size;
2147 unsigned int pos;
2148 unsigned int queued:1;
2149};
2150
2151/**
2152 * struct vb2_fileio_data - queue context used by file io emulator
2153 *
2154 * vb2 provides a compatibility layer and emulator of file io (read and
2155 * write) calls on top of streaming API. For proper operation it required
2156 * this structure to save the driver state between each call of the read
2157 * or write function.
2158 */
2159struct vb2_fileio_data {
2160 struct v4l2_requestbuffers req;
2161 struct v4l2_buffer b;
2162 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
2163 unsigned int index;
2164 unsigned int q_count;
2165 unsigned int dq_count;
2166 unsigned int flags;
2167};
2168
2169/**
2170 * __vb2_init_fileio() - initialize file io emulator
2171 * @q: videobuf2 queue
2172 * @read: mode selector (1 means read, 0 means write)
2173 */
2174static int __vb2_init_fileio(struct vb2_queue *q, int read)
2175{
2176 struct vb2_fileio_data *fileio;
2177 int i, ret;
2178 unsigned int count = 0;
2179
2180 /*
2181 * Sanity check
2182 */
2183 if ((read && !(q->io_modes & VB2_READ)) ||
2184 (!read && !(q->io_modes & VB2_WRITE)))
2185 BUG();
2186
2187 /*
2188 * Check if device supports mapping buffers to kernel virtual space.
2189 */
2190 if (!q->mem_ops->vaddr)
2191 return -EBUSY;
2192
2193 /*
2194 * Check if streaming api has not been already activated.
2195 */
2196 if (q->streaming || q->num_buffers > 0)
2197 return -EBUSY;
2198
2199 /*
2200 * Start with count 1, driver can increase it in queue_setup()
2201 */
2202 count = 1;
2203
2204 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
2205 (read) ? "read" : "write", count, q->io_flags);
2206
2207 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
2208 if (fileio == NULL)
2209 return -ENOMEM;
2210
2211 fileio->flags = q->io_flags;
2212
2213 /*
2214 * Request buffers and use MMAP type to force driver
2215 * to allocate buffers by itself.
2216 */
2217 fileio->req.count = count;
2218 fileio->req.memory = V4L2_MEMORY_MMAP;
2219 fileio->req.type = q->type;
2220 ret = vb2_reqbufs(q, &fileio->req);
2221 if (ret)
2222 goto err_kfree;
2223
2224 /*
2225 * Check if plane_count is correct
2226 * (multiplane buffers are not supported).
2227 */
2228 if (q->bufs[0]->num_planes != 1) {
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002229 ret = -EBUSY;
2230 goto err_reqbufs;
2231 }
2232
2233 /*
2234 * Get kernel address of each buffer.
2235 */
2236 for (i = 0; i < q->num_buffers; i++) {
2237 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
Wei Yongjun5dd69462013-05-13 01:48:45 -03002238 if (fileio->bufs[i].vaddr == NULL) {
2239 ret = -EINVAL;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002240 goto err_reqbufs;
Wei Yongjun5dd69462013-05-13 01:48:45 -03002241 }
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002242 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
2243 }
2244
2245 /*
2246 * Read mode requires pre queuing of all buffers.
2247 */
2248 if (read) {
2249 /*
2250 * Queue all buffers.
2251 */
2252 for (i = 0; i < q->num_buffers; i++) {
2253 struct v4l2_buffer *b = &fileio->b;
2254 memset(b, 0, sizeof(*b));
2255 b->type = q->type;
2256 b->memory = q->memory;
2257 b->index = i;
2258 ret = vb2_qbuf(q, b);
2259 if (ret)
2260 goto err_reqbufs;
2261 fileio->bufs[i].queued = 1;
2262 }
2263
2264 /*
2265 * Start streaming.
2266 */
2267 ret = vb2_streamon(q, q->type);
2268 if (ret)
2269 goto err_reqbufs;
2270 }
2271
2272 q->fileio = fileio;
2273
2274 return ret;
2275
2276err_reqbufs:
Hans de Goedea67e1722012-05-08 14:47:39 -03002277 fileio->req.count = 0;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002278 vb2_reqbufs(q, &fileio->req);
2279
2280err_kfree:
2281 kfree(fileio);
2282 return ret;
2283}
2284
2285/**
2286 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2287 * @q: videobuf2 queue
2288 */
2289static int __vb2_cleanup_fileio(struct vb2_queue *q)
2290{
2291 struct vb2_fileio_data *fileio = q->fileio;
2292
2293 if (fileio) {
2294 /*
2295 * Hack fileio context to enable direct calls to vb2 ioctl
2296 * interface.
2297 */
2298 q->fileio = NULL;
2299
2300 vb2_streamoff(q, q->type);
2301 fileio->req.count = 0;
2302 vb2_reqbufs(q, &fileio->req);
2303 kfree(fileio);
2304 dprintk(3, "file io emulator closed\n");
2305 }
2306 return 0;
2307}
2308
2309/**
2310 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2311 * @q: videobuf2 queue
2312 * @data: pointed to target userspace buffer
2313 * @count: number of bytes to read or write
2314 * @ppos: file handle position tracking pointer
2315 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2316 * @read: access mode selector (1 means read, 0 means write)
2317 */
2318static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
2319 loff_t *ppos, int nonblock, int read)
2320{
2321 struct vb2_fileio_data *fileio;
2322 struct vb2_fileio_buf *buf;
2323 int ret, index;
2324
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002325 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002326 read ? "read" : "write", (long)*ppos, count,
2327 nonblock ? "non" : "");
2328
2329 if (!data)
2330 return -EINVAL;
2331
2332 /*
2333 * Initialize emulator on first call.
2334 */
2335 if (!q->fileio) {
2336 ret = __vb2_init_fileio(q, read);
2337 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
2338 if (ret)
2339 return ret;
2340 }
2341 fileio = q->fileio;
2342
2343 /*
2344 * Hack fileio context to enable direct calls to vb2 ioctl interface.
2345 * The pointer will be restored before returning from this function.
2346 */
2347 q->fileio = NULL;
2348
2349 index = fileio->index;
2350 buf = &fileio->bufs[index];
2351
2352 /*
2353 * Check if we need to dequeue the buffer.
2354 */
2355 if (buf->queued) {
2356 struct vb2_buffer *vb;
2357
2358 /*
2359 * Call vb2_dqbuf to get buffer back.
2360 */
2361 memset(&fileio->b, 0, sizeof(fileio->b));
2362 fileio->b.type = q->type;
2363 fileio->b.memory = q->memory;
2364 fileio->b.index = index;
2365 ret = vb2_dqbuf(q, &fileio->b, nonblock);
2366 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2367 if (ret)
2368 goto end;
2369 fileio->dq_count += 1;
2370
2371 /*
2372 * Get number of bytes filled by the driver
2373 */
2374 vb = q->bufs[index];
2375 buf->size = vb2_get_plane_payload(vb, 0);
2376 buf->queued = 0;
2377 }
2378
2379 /*
2380 * Limit count on last few bytes of the buffer.
2381 */
2382 if (buf->pos + count > buf->size) {
2383 count = buf->size - buf->pos;
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002384 dprintk(5, "reducing read count: %zd\n", count);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002385 }
2386
2387 /*
2388 * Transfer data to userspace.
2389 */
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002390 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002391 count, index, buf->pos);
2392 if (read)
2393 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2394 else
2395 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2396 if (ret) {
2397 dprintk(3, "file io: error copying data\n");
2398 ret = -EFAULT;
2399 goto end;
2400 }
2401
2402 /*
2403 * Update counters.
2404 */
2405 buf->pos += count;
2406 *ppos += count;
2407
2408 /*
2409 * Queue next buffer if required.
2410 */
2411 if (buf->pos == buf->size ||
2412 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2413 /*
2414 * Check if this is the last buffer to read.
2415 */
2416 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2417 fileio->dq_count == 1) {
2418 dprintk(3, "file io: read limit reached\n");
2419 /*
2420 * Restore fileio pointer and release the context.
2421 */
2422 q->fileio = fileio;
2423 return __vb2_cleanup_fileio(q);
2424 }
2425
2426 /*
2427 * Call vb2_qbuf and give buffer to the driver.
2428 */
2429 memset(&fileio->b, 0, sizeof(fileio->b));
2430 fileio->b.type = q->type;
2431 fileio->b.memory = q->memory;
2432 fileio->b.index = index;
2433 fileio->b.bytesused = buf->pos;
2434 ret = vb2_qbuf(q, &fileio->b);
2435 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2436 if (ret)
2437 goto end;
2438
2439 /*
2440 * Buffer has been queued, update the status
2441 */
2442 buf->pos = 0;
2443 buf->queued = 1;
2444 buf->size = q->bufs[0]->v4l2_planes[0].length;
2445 fileio->q_count += 1;
2446
2447 /*
2448 * Switch to the next buffer
2449 */
2450 fileio->index = (index + 1) % q->num_buffers;
2451
2452 /*
2453 * Start streaming if required.
2454 */
2455 if (!read && !q->streaming) {
2456 ret = vb2_streamon(q, q->type);
2457 if (ret)
2458 goto end;
2459 }
2460 }
2461
2462 /*
2463 * Return proper number of bytes processed.
2464 */
2465 if (ret == 0)
2466 ret = count;
2467end:
2468 /*
2469 * Restore the fileio context and block vb2 ioctl interface.
2470 */
2471 q->fileio = fileio;
2472 return ret;
2473}
2474
2475size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2476 loff_t *ppos, int nonblocking)
2477{
2478 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2479}
2480EXPORT_SYMBOL_GPL(vb2_read);
2481
Ricardo Ribalda819585b2013-08-28 04:39:29 -03002482size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002483 loff_t *ppos, int nonblocking)
2484{
Ricardo Ribalda819585b2013-08-28 04:39:29 -03002485 return __vb2_perform_fileio(q, (char __user *) data, count,
2486 ppos, nonblocking, 0);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002487}
2488EXPORT_SYMBOL_GPL(vb2_write);
2489
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002490
2491/*
2492 * The following functions are not part of the vb2 core API, but are helper
2493 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2494 * and struct vb2_ops.
2495 * They contain boilerplate code that most if not all drivers have to do
2496 * and so they simplify the driver code.
2497 */
2498
2499/* The queue is busy if there is a owner and you are not that owner. */
2500static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
2501{
2502 return vdev->queue->owner && vdev->queue->owner != file->private_data;
2503}
2504
2505/* vb2 ioctl helpers */
2506
2507int vb2_ioctl_reqbufs(struct file *file, void *priv,
2508 struct v4l2_requestbuffers *p)
2509{
2510 struct video_device *vdev = video_devdata(file);
2511 int res = __verify_memory_type(vdev->queue, p->memory, p->type);
2512
2513 if (res)
2514 return res;
2515 if (vb2_queue_is_busy(vdev, file))
2516 return -EBUSY;
2517 res = __reqbufs(vdev->queue, p);
2518 /* If count == 0, then the owner has released all buffers and he
2519 is no longer owner of the queue. Otherwise we have a new owner. */
2520 if (res == 0)
2521 vdev->queue->owner = p->count ? file->private_data : NULL;
2522 return res;
2523}
2524EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
2525
2526int vb2_ioctl_create_bufs(struct file *file, void *priv,
2527 struct v4l2_create_buffers *p)
2528{
2529 struct video_device *vdev = video_devdata(file);
2530 int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
2531
2532 p->index = vdev->queue->num_buffers;
2533 /* If count == 0, then just check if memory and type are valid.
2534 Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2535 if (p->count == 0)
2536 return res != -EBUSY ? res : 0;
2537 if (res)
2538 return res;
2539 if (vb2_queue_is_busy(vdev, file))
2540 return -EBUSY;
2541 res = __create_bufs(vdev->queue, p);
2542 if (res == 0)
2543 vdev->queue->owner = file->private_data;
2544 return res;
2545}
2546EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
2547
2548int vb2_ioctl_prepare_buf(struct file *file, void *priv,
2549 struct v4l2_buffer *p)
2550{
2551 struct video_device *vdev = video_devdata(file);
2552
2553 if (vb2_queue_is_busy(vdev, file))
2554 return -EBUSY;
2555 return vb2_prepare_buf(vdev->queue, p);
2556}
2557EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
2558
2559int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
2560{
2561 struct video_device *vdev = video_devdata(file);
2562
2563 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2564 return vb2_querybuf(vdev->queue, p);
2565}
2566EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
2567
2568int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2569{
2570 struct video_device *vdev = video_devdata(file);
2571
2572 if (vb2_queue_is_busy(vdev, file))
2573 return -EBUSY;
2574 return vb2_qbuf(vdev->queue, p);
2575}
2576EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
2577
2578int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2579{
2580 struct video_device *vdev = video_devdata(file);
2581
2582 if (vb2_queue_is_busy(vdev, file))
2583 return -EBUSY;
2584 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
2585}
2586EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
2587
2588int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
2589{
2590 struct video_device *vdev = video_devdata(file);
2591
2592 if (vb2_queue_is_busy(vdev, file))
2593 return -EBUSY;
2594 return vb2_streamon(vdev->queue, i);
2595}
2596EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
2597
2598int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
2599{
2600 struct video_device *vdev = video_devdata(file);
2601
2602 if (vb2_queue_is_busy(vdev, file))
2603 return -EBUSY;
2604 return vb2_streamoff(vdev->queue, i);
2605}
2606EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
2607
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002608int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
2609{
2610 struct video_device *vdev = video_devdata(file);
2611
2612 if (vb2_queue_is_busy(vdev, file))
2613 return -EBUSY;
2614 return vb2_expbuf(vdev->queue, p);
2615}
2616EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
2617
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002618/* v4l2_file_operations helpers */
2619
2620int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
2621{
2622 struct video_device *vdev = video_devdata(file);
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002623 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2624 int err;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002625
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002626 if (lock && mutex_lock_interruptible(lock))
2627 return -ERESTARTSYS;
2628 err = vb2_mmap(vdev->queue, vma);
2629 if (lock)
2630 mutex_unlock(lock);
2631 return err;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002632}
2633EXPORT_SYMBOL_GPL(vb2_fop_mmap);
2634
2635int vb2_fop_release(struct file *file)
2636{
2637 struct video_device *vdev = video_devdata(file);
2638
2639 if (file->private_data == vdev->queue->owner) {
2640 vb2_queue_release(vdev->queue);
2641 vdev->queue->owner = NULL;
2642 }
2643 return v4l2_fh_release(file);
2644}
2645EXPORT_SYMBOL_GPL(vb2_fop_release);
2646
Ricardo Ribalda819585b2013-08-28 04:39:29 -03002647ssize_t vb2_fop_write(struct file *file, const char __user *buf,
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002648 size_t count, loff_t *ppos)
2649{
2650 struct video_device *vdev = video_devdata(file);
2651 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002652 int err = -EBUSY;
2653
Hans Verkuilcf533732012-07-31 04:02:25 -03002654 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002655 return -ERESTARTSYS;
2656 if (vb2_queue_is_busy(vdev, file))
2657 goto exit;
2658 err = vb2_write(vdev->queue, buf, count, ppos,
2659 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03002660 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002661 vdev->queue->owner = file->private_data;
2662exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03002663 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002664 mutex_unlock(lock);
2665 return err;
2666}
2667EXPORT_SYMBOL_GPL(vb2_fop_write);
2668
2669ssize_t vb2_fop_read(struct file *file, char __user *buf,
2670 size_t count, loff_t *ppos)
2671{
2672 struct video_device *vdev = video_devdata(file);
2673 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002674 int err = -EBUSY;
2675
Hans Verkuilcf533732012-07-31 04:02:25 -03002676 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002677 return -ERESTARTSYS;
2678 if (vb2_queue_is_busy(vdev, file))
2679 goto exit;
2680 err = vb2_read(vdev->queue, buf, count, ppos,
2681 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03002682 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002683 vdev->queue->owner = file->private_data;
2684exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03002685 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002686 mutex_unlock(lock);
2687 return err;
2688}
2689EXPORT_SYMBOL_GPL(vb2_fop_read);
2690
2691unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
2692{
2693 struct video_device *vdev = video_devdata(file);
2694 struct vb2_queue *q = vdev->queue;
2695 struct mutex *lock = q->lock ? q->lock : vdev->lock;
2696 unsigned long req_events = poll_requested_events(wait);
2697 unsigned res;
2698 void *fileio;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002699 bool must_lock = false;
2700
2701 /* Try to be smart: only lock if polling might start fileio,
2702 otherwise locking will only introduce unwanted delays. */
2703 if (q->num_buffers == 0 && q->fileio == NULL) {
2704 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2705 (req_events & (POLLIN | POLLRDNORM)))
2706 must_lock = true;
2707 else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2708 (req_events & (POLLOUT | POLLWRNORM)))
2709 must_lock = true;
2710 }
2711
2712 /* If locking is needed, but this helper doesn't know how, then you
2713 shouldn't be using this helper but you should write your own. */
Hans Verkuilcf533732012-07-31 04:02:25 -03002714 WARN_ON(must_lock && !lock);
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002715
Hans Verkuilcf533732012-07-31 04:02:25 -03002716 if (must_lock && lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002717 return POLLERR;
2718
2719 fileio = q->fileio;
2720
2721 res = vb2_poll(vdev->queue, file, wait);
2722
2723 /* If fileio was started, then we have a new queue owner. */
2724 if (must_lock && !fileio && q->fileio)
2725 q->owner = file->private_data;
Hans Verkuilcf533732012-07-31 04:02:25 -03002726 if (must_lock && lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002727 mutex_unlock(lock);
2728 return res;
2729}
2730EXPORT_SYMBOL_GPL(vb2_fop_poll);
2731
2732#ifndef CONFIG_MMU
2733unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
2734 unsigned long len, unsigned long pgoff, unsigned long flags)
2735{
2736 struct video_device *vdev = video_devdata(file);
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002737 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2738 int ret;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002739
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002740 if (lock && mutex_lock_interruptible(lock))
2741 return -ERESTARTSYS;
2742 ret = vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
2743 if (lock)
2744 mutex_unlock(lock);
2745 return ret;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002746}
2747EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
2748#endif
2749
2750/* vb2_ops helpers. Only use if vq->lock is non-NULL. */
2751
2752void vb2_ops_wait_prepare(struct vb2_queue *vq)
2753{
2754 mutex_unlock(vq->lock);
2755}
2756EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
2757
2758void vb2_ops_wait_finish(struct vb2_queue *vq)
2759{
2760 mutex_lock(vq->lock);
2761}
2762EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
2763
Pawel Osciake23ccc02010-10-11 10:56:41 -03002764MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
Pawel Osciak95072082011-03-13 15:23:32 -03002765MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
Pawel Osciake23ccc02010-10-11 10:56:41 -03002766MODULE_LICENSE("GPL");