blob: dbc2b8ab8cdb1e50b7aa6752d424dc0db80b3067 [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
Hans Verkuilf1343282014-02-24 14:44:50 -030043/* Flags that are set by the vb2 core */
Sakari Ailus1b18e7a2012-10-22 17:10:16 -030044#define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -030045 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
Sakari Ailus1b18e7a2012-10-22 17:10:16 -030046 V4L2_BUF_FLAG_PREPARED | \
47 V4L2_BUF_FLAG_TIMESTAMP_MASK)
Hans Verkuilf1343282014-02-24 14:44:50 -030048/* Output buffer flags that should be passed on to the driver */
49#define V4L2_BUFFER_OUT_FLAGS (V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_BFRAME | \
50 V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_TIMECODE)
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -030051
Pawel Osciake23ccc02010-10-11 10:56:41 -030052/**
53 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
54 */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030055static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
Pawel Osciake23ccc02010-10-11 10:56:41 -030056{
57 struct vb2_queue *q = vb->vb2_queue;
58 void *mem_priv;
59 int plane;
60
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -030061 /*
62 * Allocate memory for all planes in this buffer
63 * NOTE: mmapped areas should be page aligned
64 */
Pawel Osciake23ccc02010-10-11 10:56:41 -030065 for (plane = 0; plane < vb->num_planes; ++plane) {
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -030066 unsigned long size = PAGE_ALIGN(q->plane_sizes[plane]);
67
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030068 mem_priv = call_memop(q, alloc, q->alloc_ctx[plane],
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -030069 size, q->gfp_flags);
Guennadi Liakhovetski62a79432011-03-22 09:24:58 -030070 if (IS_ERR_OR_NULL(mem_priv))
Pawel Osciake23ccc02010-10-11 10:56:41 -030071 goto free;
72
73 /* Associate allocator private data with this plane */
74 vb->planes[plane].mem_priv = mem_priv;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030075 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
Pawel Osciake23ccc02010-10-11 10:56:41 -030076 }
77
78 return 0;
79free:
80 /* Free already allocated memory if one of the allocations failed */
Marek Szyprowskia00d0262011-12-15 05:53:06 -030081 for (; plane > 0; --plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030082 call_memop(q, put, vb->planes[plane - 1].mem_priv);
Marek Szyprowskia00d0262011-12-15 05:53:06 -030083 vb->planes[plane - 1].mem_priv = NULL;
84 }
Pawel Osciake23ccc02010-10-11 10:56:41 -030085
86 return -ENOMEM;
87}
88
89/**
90 * __vb2_buf_mem_free() - free memory of the given buffer
91 */
92static void __vb2_buf_mem_free(struct vb2_buffer *vb)
93{
94 struct vb2_queue *q = vb->vb2_queue;
95 unsigned int plane;
96
97 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030098 call_memop(q, put, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -030099 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300100 dprintk(3, "Freed plane %d of buffer %d\n", plane,
101 vb->v4l2_buf.index);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300102 }
103}
104
105/**
106 * __vb2_buf_userptr_put() - release userspace memory associated with
107 * a USERPTR buffer
108 */
109static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
110{
111 struct vb2_queue *q = vb->vb2_queue;
112 unsigned int plane;
113
114 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300115 if (vb->planes[plane].mem_priv)
116 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
117 vb->planes[plane].mem_priv = NULL;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300118 }
119}
120
121/**
Sumit Semwalc5384042012-06-14 10:37:37 -0300122 * __vb2_plane_dmabuf_put() - release memory associated with
123 * a DMABUF shared plane
124 */
125static void __vb2_plane_dmabuf_put(struct vb2_queue *q, struct vb2_plane *p)
126{
127 if (!p->mem_priv)
128 return;
129
130 if (p->dbuf_mapped)
131 call_memop(q, unmap_dmabuf, p->mem_priv);
132
133 call_memop(q, detach_dmabuf, p->mem_priv);
134 dma_buf_put(p->dbuf);
135 memset(p, 0, sizeof(*p));
136}
137
138/**
139 * __vb2_buf_dmabuf_put() - release memory associated with
140 * a DMABUF shared buffer
141 */
142static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
143{
144 struct vb2_queue *q = vb->vb2_queue;
145 unsigned int plane;
146
147 for (plane = 0; plane < vb->num_planes; ++plane)
148 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
149}
150
151/**
Hans Verkuila5e3d742013-12-04 15:14:05 +0100152 * __setup_lengths() - setup initial lengths for every plane in
153 * every buffer on the queue
154 */
155static void __setup_lengths(struct vb2_queue *q, unsigned int n)
156{
157 unsigned int buffer, plane;
158 struct vb2_buffer *vb;
159
160 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
161 vb = q->bufs[buffer];
162 if (!vb)
163 continue;
164
165 for (plane = 0; plane < vb->num_planes; ++plane)
166 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
167 }
168}
169
170/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300171 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
172 * every buffer on the queue
173 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300174static void __setup_offsets(struct vb2_queue *q, unsigned int n)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300175{
176 unsigned int buffer, plane;
177 struct vb2_buffer *vb;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300178 unsigned long off;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300179
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300180 if (q->num_buffers) {
181 struct v4l2_plane *p;
182 vb = q->bufs[q->num_buffers - 1];
183 p = &vb->v4l2_planes[vb->num_planes - 1];
184 off = PAGE_ALIGN(p->m.mem_offset + p->length);
185 } else {
186 off = 0;
187 }
188
189 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300190 vb = q->bufs[buffer];
191 if (!vb)
192 continue;
193
194 for (plane = 0; plane < vb->num_planes; ++plane) {
195 vb->v4l2_planes[plane].m.mem_offset = off;
196
197 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
198 buffer, plane, off);
199
200 off += vb->v4l2_planes[plane].length;
201 off = PAGE_ALIGN(off);
202 }
203 }
204}
205
206/**
207 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
208 * video buffer memory for all buffers/planes on the queue and initializes the
209 * queue
210 *
211 * Returns the number of buffers successfully allocated.
212 */
213static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300214 unsigned int num_buffers, unsigned int num_planes)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300215{
216 unsigned int buffer;
217 struct vb2_buffer *vb;
218 int ret;
219
220 for (buffer = 0; buffer < num_buffers; ++buffer) {
221 /* Allocate videobuf buffer structures */
222 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
223 if (!vb) {
224 dprintk(1, "Memory alloc for buffer struct failed\n");
225 break;
226 }
227
228 /* Length stores number of planes for multiplanar buffers */
229 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
230 vb->v4l2_buf.length = num_planes;
231
232 vb->state = VB2_BUF_STATE_DEQUEUED;
233 vb->vb2_queue = q;
234 vb->num_planes = num_planes;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300235 vb->v4l2_buf.index = q->num_buffers + buffer;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300236 vb->v4l2_buf.type = q->type;
237 vb->v4l2_buf.memory = memory;
238
239 /* Allocate video buffer memory for the MMAP type */
240 if (memory == V4L2_MEMORY_MMAP) {
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300241 ret = __vb2_buf_mem_alloc(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300242 if (ret) {
243 dprintk(1, "Failed allocating memory for "
244 "buffer %d\n", buffer);
245 kfree(vb);
246 break;
247 }
248 /*
249 * Call the driver-provided buffer initialization
250 * callback, if given. An error in initialization
251 * results in queue setup failure.
252 */
253 ret = call_qop(q, buf_init, vb);
254 if (ret) {
255 dprintk(1, "Buffer %d %p initialization"
256 " failed\n", buffer, vb);
257 __vb2_buf_mem_free(vb);
258 kfree(vb);
259 break;
260 }
261 }
262
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300263 q->bufs[q->num_buffers + buffer] = vb;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300264 }
265
Hans Verkuila5e3d742013-12-04 15:14:05 +0100266 __setup_lengths(q, buffer);
Philipp Zabeldc775232013-09-19 04:37:29 -0300267 if (memory == V4L2_MEMORY_MMAP)
268 __setup_offsets(q, buffer);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300269
270 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300271 buffer, num_planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300272
273 return buffer;
274}
275
276/**
277 * __vb2_free_mem() - release all video buffer memory for a given queue
278 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300279static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300280{
281 unsigned int buffer;
282 struct vb2_buffer *vb;
283
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300284 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
285 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300286 vb = q->bufs[buffer];
287 if (!vb)
288 continue;
289
290 /* Free MMAP buffers or release USERPTR buffers */
291 if (q->memory == V4L2_MEMORY_MMAP)
292 __vb2_buf_mem_free(vb);
Sumit Semwalc5384042012-06-14 10:37:37 -0300293 else if (q->memory == V4L2_MEMORY_DMABUF)
294 __vb2_buf_dmabuf_put(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300295 else
296 __vb2_buf_userptr_put(vb);
297 }
298}
299
300/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300301 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
302 * related information, if no buffers are left return the queue to an
303 * uninitialized state. Might be called even if the queue has already been freed.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300304 */
Hans Verkuil63faabf2013-12-13 13:13:40 -0300305static int __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300306{
307 unsigned int buffer;
308
Hans Verkuil63faabf2013-12-13 13:13:40 -0300309 /*
310 * Sanity check: when preparing a buffer the queue lock is released for
311 * a short while (see __buf_prepare for the details), which would allow
312 * a race with a reqbufs which can call this function. Removing the
313 * buffers from underneath __buf_prepare is obviously a bad idea, so we
314 * check if any of the buffers is in the state PREPARING, and if so we
315 * just return -EAGAIN.
316 */
317 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
318 ++buffer) {
319 if (q->bufs[buffer] == NULL)
320 continue;
321 if (q->bufs[buffer]->state == VB2_BUF_STATE_PREPARING) {
322 dprintk(1, "reqbufs: preparing buffers, cannot free\n");
323 return -EAGAIN;
324 }
325 }
326
Pawel Osciake23ccc02010-10-11 10:56:41 -0300327 /* Call driver-provided cleanup function for each buffer, if provided */
328 if (q->ops->buf_cleanup) {
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300329 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
330 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300331 if (NULL == q->bufs[buffer])
332 continue;
333 q->ops->buf_cleanup(q->bufs[buffer]);
334 }
335 }
336
337 /* Release video buffer memory */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300338 __vb2_free_mem(q, buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300339
340 /* Free videobuf buffers */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300341 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
342 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300343 kfree(q->bufs[buffer]);
344 q->bufs[buffer] = NULL;
345 }
346
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300347 q->num_buffers -= buffers;
348 if (!q->num_buffers)
349 q->memory = 0;
Marek Szyprowskibd50d992011-10-25 03:07:59 -0300350 INIT_LIST_HEAD(&q->queued_list);
Hans Verkuil63faabf2013-12-13 13:13:40 -0300351 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300352}
353
354/**
355 * __verify_planes_array() - verify that the planes array passed in struct
356 * v4l2_buffer from userspace can be safely used
357 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300358static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300359{
Hans Verkuil32a77262012-09-28 06:12:53 -0300360 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
361 return 0;
362
Pawel Osciake23ccc02010-10-11 10:56:41 -0300363 /* Is memory for copying plane information present? */
364 if (NULL == b->m.planes) {
365 dprintk(1, "Multi-planar buffer passed but "
366 "planes array not provided\n");
367 return -EINVAL;
368 }
369
370 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
371 dprintk(1, "Incorrect planes array length, "
372 "expected %d, got %d\n", vb->num_planes, b->length);
373 return -EINVAL;
374 }
375
376 return 0;
377}
378
379/**
Laurent Pinchart8023ed02012-07-10 10:41:40 -0300380 * __verify_length() - Verify that the bytesused value for each plane fits in
381 * the plane length and that the data offset doesn't exceed the bytesused value.
382 */
383static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
384{
385 unsigned int length;
386 unsigned int plane;
387
388 if (!V4L2_TYPE_IS_OUTPUT(b->type))
389 return 0;
390
391 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
392 for (plane = 0; plane < vb->num_planes; ++plane) {
393 length = (b->memory == V4L2_MEMORY_USERPTR)
394 ? b->m.planes[plane].length
395 : vb->v4l2_planes[plane].length;
396
397 if (b->m.planes[plane].bytesused > length)
398 return -EINVAL;
Sylwester Nawrocki3c5c23c2013-08-26 11:47:09 -0300399
400 if (b->m.planes[plane].data_offset > 0 &&
401 b->m.planes[plane].data_offset >=
Laurent Pinchart8023ed02012-07-10 10:41:40 -0300402 b->m.planes[plane].bytesused)
403 return -EINVAL;
404 }
405 } else {
406 length = (b->memory == V4L2_MEMORY_USERPTR)
407 ? b->length : vb->v4l2_planes[0].length;
408
409 if (b->bytesused > length)
410 return -EINVAL;
411 }
412
413 return 0;
414}
415
416/**
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300417 * __buffer_in_use() - return true if the buffer is in use and
418 * the queue cannot be freed (by the means of REQBUFS(0)) call
419 */
420static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
421{
422 unsigned int plane;
423 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski2c2dd6ac2011-10-12 13:09:53 -0300424 void *mem_priv = vb->planes[plane].mem_priv;
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300425 /*
426 * If num_users() has not been provided, call_memop
427 * will return 0, apparently nobody cares about this
428 * case anyway. If num_users() returns more than 1,
429 * we are not the only user of the plane's memory.
430 */
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300431 if (mem_priv && call_memop(q, num_users, mem_priv) > 1)
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300432 return true;
433 }
434 return false;
435}
436
437/**
438 * __buffers_in_use() - return true if any buffers on the queue are in use and
439 * the queue cannot be freed (by the means of REQBUFS(0)) call
440 */
441static bool __buffers_in_use(struct vb2_queue *q)
442{
443 unsigned int buffer;
444 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
445 if (__buffer_in_use(q, q->bufs[buffer]))
446 return true;
447 }
448 return false;
449}
450
451/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300452 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
453 * returned to userspace
454 */
Hans Verkuil32a77262012-09-28 06:12:53 -0300455static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300456{
457 struct vb2_queue *q = vb->vb2_queue;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300458
Sakari Ailus2b719d72012-05-02 09:40:03 -0300459 /* Copy back data such as timestamp, flags, etc. */
Pawel Osciake23ccc02010-10-11 10:56:41 -0300460 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
Sakari Ailus2b719d72012-05-02 09:40:03 -0300461 b->reserved2 = vb->v4l2_buf.reserved2;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300462 b->reserved = vb->v4l2_buf.reserved;
463
464 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300465 /*
466 * Fill in plane-related data if userspace provided an array
Hans Verkuil32a77262012-09-28 06:12:53 -0300467 * for it. The caller has already verified memory and size.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300468 */
Hans Verkuil3c0b6062012-09-28 06:24:18 -0300469 b->length = vb->num_planes;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300470 memcpy(b->m.planes, vb->v4l2_planes,
471 b->length * sizeof(struct v4l2_plane));
472 } else {
473 /*
474 * We use length and offset in v4l2_planes array even for
475 * single-planar buffers, but userspace does not.
476 */
477 b->length = vb->v4l2_planes[0].length;
478 b->bytesused = vb->v4l2_planes[0].bytesused;
479 if (q->memory == V4L2_MEMORY_MMAP)
480 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
481 else if (q->memory == V4L2_MEMORY_USERPTR)
482 b->m.userptr = vb->v4l2_planes[0].m.userptr;
Sumit Semwalc5384042012-06-14 10:37:37 -0300483 else if (q->memory == V4L2_MEMORY_DMABUF)
484 b->m.fd = vb->v4l2_planes[0].m.fd;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300485 }
486
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300487 /*
488 * Clear any buffer state related flags.
489 */
Sakari Ailus1b18e7a2012-10-22 17:10:16 -0300490 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
Sakari Ailus7ce6fd82014-02-25 19:08:52 -0300491 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
492 if ((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) !=
493 V4L2_BUF_FLAG_TIMESTAMP_COPY) {
494 /*
495 * For non-COPY timestamps, drop timestamp source bits
496 * and obtain the timestamp source from the queue.
497 */
498 b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
499 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
500 }
Pawel Osciake23ccc02010-10-11 10:56:41 -0300501
502 switch (vb->state) {
503 case VB2_BUF_STATE_QUEUED:
504 case VB2_BUF_STATE_ACTIVE:
505 b->flags |= V4L2_BUF_FLAG_QUEUED;
506 break;
507 case VB2_BUF_STATE_ERROR:
508 b->flags |= V4L2_BUF_FLAG_ERROR;
509 /* fall through */
510 case VB2_BUF_STATE_DONE:
511 b->flags |= V4L2_BUF_FLAG_DONE;
512 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300513 case VB2_BUF_STATE_PREPARED:
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300514 b->flags |= V4L2_BUF_FLAG_PREPARED;
515 break;
Hans Verkuilb18a8ff2013-12-13 13:13:38 -0300516 case VB2_BUF_STATE_PREPARING:
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300517 case VB2_BUF_STATE_DEQUEUED:
Pawel Osciake23ccc02010-10-11 10:56:41 -0300518 /* nothing */
519 break;
520 }
521
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300522 if (__buffer_in_use(q, vb))
Pawel Osciake23ccc02010-10-11 10:56:41 -0300523 b->flags |= V4L2_BUF_FLAG_MAPPED;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300524}
525
526/**
527 * vb2_querybuf() - query video buffer information
528 * @q: videobuf queue
529 * @b: buffer struct passed from userspace to vidioc_querybuf handler
530 * in driver
531 *
532 * Should be called from vidioc_querybuf ioctl handler in driver.
533 * This function will verify the passed v4l2_buffer structure and fill the
534 * relevant information for the userspace.
535 *
536 * The return values from this function are intended to be directly returned
537 * from vidioc_querybuf handler in driver.
538 */
539int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
540{
541 struct vb2_buffer *vb;
Hans Verkuil32a77262012-09-28 06:12:53 -0300542 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300543
544 if (b->type != q->type) {
545 dprintk(1, "querybuf: wrong buffer type\n");
546 return -EINVAL;
547 }
548
549 if (b->index >= q->num_buffers) {
550 dprintk(1, "querybuf: buffer index out of range\n");
551 return -EINVAL;
552 }
553 vb = q->bufs[b->index];
Hans Verkuil32a77262012-09-28 06:12:53 -0300554 ret = __verify_planes_array(vb, b);
555 if (!ret)
556 __fill_v4l2_buffer(vb, b);
557 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300558}
559EXPORT_SYMBOL(vb2_querybuf);
560
561/**
562 * __verify_userptr_ops() - verify that all memory operations required for
563 * USERPTR queue type have been provided
564 */
565static int __verify_userptr_ops(struct vb2_queue *q)
566{
567 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
568 !q->mem_ops->put_userptr)
569 return -EINVAL;
570
571 return 0;
572}
573
574/**
575 * __verify_mmap_ops() - verify that all memory operations required for
576 * MMAP queue type have been provided
577 */
578static int __verify_mmap_ops(struct vb2_queue *q)
579{
580 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
581 !q->mem_ops->put || !q->mem_ops->mmap)
582 return -EINVAL;
583
584 return 0;
585}
586
587/**
Sumit Semwalc5384042012-06-14 10:37:37 -0300588 * __verify_dmabuf_ops() - verify that all memory operations required for
589 * DMABUF queue type have been provided
590 */
591static int __verify_dmabuf_ops(struct vb2_queue *q)
592{
593 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
594 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
595 !q->mem_ops->unmap_dmabuf)
596 return -EINVAL;
597
598 return 0;
599}
600
601/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300602 * __verify_memory_type() - Check whether the memory type and buffer type
603 * passed to a buffer operation are compatible with the queue.
604 */
605static int __verify_memory_type(struct vb2_queue *q,
606 enum v4l2_memory memory, enum v4l2_buf_type type)
607{
Sumit Semwalc5384042012-06-14 10:37:37 -0300608 if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR &&
609 memory != V4L2_MEMORY_DMABUF) {
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300610 dprintk(1, "reqbufs: unsupported memory type\n");
611 return -EINVAL;
612 }
613
614 if (type != q->type) {
615 dprintk(1, "reqbufs: requested type is incorrect\n");
616 return -EINVAL;
617 }
618
619 /*
620 * Make sure all the required memory ops for given memory type
621 * are available.
622 */
623 if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
624 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
625 return -EINVAL;
626 }
627
628 if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
629 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
630 return -EINVAL;
631 }
632
Sumit Semwalc5384042012-06-14 10:37:37 -0300633 if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
634 dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
635 return -EINVAL;
636 }
637
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300638 /*
639 * Place the busy tests at the end: -EBUSY can be ignored when
640 * create_bufs is called with count == 0, but count == 0 should still
641 * do the memory and type validation.
642 */
643 if (q->fileio) {
644 dprintk(1, "reqbufs: file io in progress\n");
645 return -EBUSY;
646 }
647 return 0;
648}
649
650/**
651 * __reqbufs() - Initiate streaming
Pawel Osciake23ccc02010-10-11 10:56:41 -0300652 * @q: videobuf2 queue
653 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
654 *
655 * Should be called from vidioc_reqbufs ioctl handler of a driver.
656 * This function:
657 * 1) verifies streaming parameters passed from the userspace,
658 * 2) sets up the queue,
659 * 3) negotiates number of buffers and planes per buffer with the driver
660 * to be used during streaming,
661 * 4) allocates internal buffer structures (struct vb2_buffer), according to
662 * the agreed parameters,
663 * 5) for MMAP memory type, allocates actual video memory, using the
664 * memory handling/allocation routines provided during queue initialization
665 *
666 * If req->count is 0, all the memory will be freed instead.
667 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
668 * and the queue is not busy, memory will be reallocated.
669 *
670 * The return values from this function are intended to be directly returned
671 * from vidioc_reqbufs handler in driver.
672 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300673static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300674{
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300675 unsigned int num_buffers, allocated_buffers, num_planes = 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300676 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300677
678 if (q->streaming) {
679 dprintk(1, "reqbufs: streaming active\n");
680 return -EBUSY;
681 }
682
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300683 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300684 /*
685 * We already have buffers allocated, so first check if they
686 * are not in use and can be freed.
687 */
688 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
689 dprintk(1, "reqbufs: memory in use, cannot free\n");
690 return -EBUSY;
691 }
692
Hans Verkuil63faabf2013-12-13 13:13:40 -0300693 ret = __vb2_queue_free(q, q->num_buffers);
694 if (ret)
695 return ret;
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300696
697 /*
698 * In case of REQBUFS(0) return immediately without calling
699 * driver's queue_setup() callback and allocating resources.
700 */
701 if (req->count == 0)
702 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300703 }
704
705 /*
706 * Make sure the requested values and current defaults are sane.
707 */
708 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300709 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
Pawel Osciake23ccc02010-10-11 10:56:41 -0300710 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
Marek Szyprowski13b14092011-04-14 07:17:44 -0300711 q->memory = req->memory;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300712
713 /*
714 * Ask the driver how many buffers and planes per buffer it requires.
715 * Driver also sets the size and allocator context for each plane.
716 */
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300717 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300718 q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300719 if (ret)
720 return ret;
721
722 /* Finally, allocate buffers and video memory */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300723 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
Marek Szyprowski66072d42011-06-28 08:29:02 -0300724 if (ret == 0) {
725 dprintk(1, "Memory allocation failed\n");
726 return -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300727 }
728
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300729 allocated_buffers = ret;
730
Pawel Osciake23ccc02010-10-11 10:56:41 -0300731 /*
732 * Check if driver can handle the allocated number of buffers.
733 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300734 if (allocated_buffers < num_buffers) {
735 num_buffers = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300736
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300737 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
738 &num_planes, q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300739
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300740 if (!ret && allocated_buffers < num_buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300741 ret = -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300742
743 /*
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300744 * Either the driver has accepted a smaller number of buffers,
745 * or .queue_setup() returned an error
Pawel Osciake23ccc02010-10-11 10:56:41 -0300746 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300747 }
748
749 q->num_buffers = allocated_buffers;
750
751 if (ret < 0) {
752 __vb2_queue_free(q, allocated_buffers);
753 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300754 }
755
Pawel Osciake23ccc02010-10-11 10:56:41 -0300756 /*
757 * Return the number of successfully allocated buffers
758 * to the userspace.
759 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300760 req->count = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300761
762 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300763}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300764
765/**
766 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
767 * type values.
768 * @q: videobuf2 queue
769 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
770 */
771int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
772{
773 int ret = __verify_memory_type(q, req->memory, req->type);
774
775 return ret ? ret : __reqbufs(q, req);
776}
Pawel Osciake23ccc02010-10-11 10:56:41 -0300777EXPORT_SYMBOL_GPL(vb2_reqbufs);
778
779/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300780 * __create_bufs() - Allocate buffers and any required auxiliary structs
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300781 * @q: videobuf2 queue
782 * @create: creation parameters, passed from userspace to vidioc_create_bufs
783 * handler in driver
784 *
785 * Should be called from vidioc_create_bufs ioctl handler of a driver.
786 * This function:
787 * 1) verifies parameter sanity
788 * 2) calls the .queue_setup() queue operation
789 * 3) performs any necessary memory allocations
790 *
791 * The return values from this function are intended to be directly returned
792 * from vidioc_create_bufs handler in driver.
793 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300794static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300795{
796 unsigned int num_planes = 0, num_buffers, allocated_buffers;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300797 int ret;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300798
799 if (q->num_buffers == VIDEO_MAX_FRAME) {
800 dprintk(1, "%s(): maximum number of buffers already allocated\n",
801 __func__);
802 return -ENOBUFS;
803 }
804
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300805 if (!q->num_buffers) {
806 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
807 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
808 q->memory = create->memory;
809 }
810
811 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
812
813 /*
814 * Ask the driver, whether the requested number of buffers, planes per
815 * buffer and their sizes are acceptable
816 */
817 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
818 &num_planes, q->plane_sizes, q->alloc_ctx);
819 if (ret)
820 return ret;
821
822 /* Finally, allocate buffers and video memory */
823 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
824 num_planes);
Hans Verkuilf05393d22012-06-22 05:44:14 -0300825 if (ret == 0) {
826 dprintk(1, "Memory allocation failed\n");
827 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300828 }
829
830 allocated_buffers = ret;
831
832 /*
833 * Check if driver can handle the so far allocated number of buffers.
834 */
835 if (ret < num_buffers) {
836 num_buffers = ret;
837
838 /*
839 * q->num_buffers contains the total number of buffers, that the
840 * queue driver has set up
841 */
842 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
843 &num_planes, q->plane_sizes, q->alloc_ctx);
844
845 if (!ret && allocated_buffers < num_buffers)
846 ret = -ENOMEM;
847
848 /*
849 * Either the driver has accepted a smaller number of buffers,
850 * or .queue_setup() returned an error
851 */
852 }
853
854 q->num_buffers += allocated_buffers;
855
856 if (ret < 0) {
857 __vb2_queue_free(q, allocated_buffers);
Hans Verkuilf05393d22012-06-22 05:44:14 -0300858 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300859 }
860
861 /*
862 * Return the number of successfully allocated buffers
863 * to the userspace.
864 */
865 create->count = allocated_buffers;
866
867 return 0;
868}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300869
870/**
Nicolas THERY53aa3b12012-07-20 09:25:37 -0300871 * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
872 * memory and type values.
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300873 * @q: videobuf2 queue
874 * @create: creation parameters, passed from userspace to vidioc_create_bufs
875 * handler in driver
876 */
877int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
878{
879 int ret = __verify_memory_type(q, create->memory, create->format.type);
880
881 create->index = q->num_buffers;
Hans Verkuilf05393d22012-06-22 05:44:14 -0300882 if (create->count == 0)
883 return ret != -EBUSY ? ret : 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300884 return ret ? ret : __create_bufs(q, create);
885}
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300886EXPORT_SYMBOL_GPL(vb2_create_bufs);
887
888/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300889 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
890 * @vb: vb2_buffer to which the plane in question belongs to
891 * @plane_no: plane number for which the address is to be returned
892 *
893 * This function returns a kernel virtual address of a given plane if
894 * such a mapping exist, NULL otherwise.
895 */
896void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
897{
898 struct vb2_queue *q = vb->vb2_queue;
899
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300900 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300901 return NULL;
902
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300903 return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300904
905}
906EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
907
908/**
909 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
910 * @vb: vb2_buffer to which the plane in question belongs to
911 * @plane_no: plane number for which the cookie is to be returned
912 *
913 * This function returns an allocator specific cookie for a given plane if
914 * available, NULL otherwise. The allocator should provide some simple static
915 * inline function, which would convert this cookie to the allocator specific
916 * type that can be used directly by the driver to access the buffer. This can
917 * be for example physical address, pointer to scatter list or IOMMU mapping.
918 */
919void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
920{
921 struct vb2_queue *q = vb->vb2_queue;
922
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300923 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300924 return NULL;
925
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300926 return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300927}
928EXPORT_SYMBOL_GPL(vb2_plane_cookie);
929
930/**
931 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
932 * @vb: vb2_buffer returned from the driver
933 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
934 * or VB2_BUF_STATE_ERROR if the operation finished with an error
935 *
936 * This function should be called by the driver after a hardware operation on
937 * a buffer is finished and the buffer may be returned to userspace. The driver
938 * cannot use this buffer anymore until it is queued back to it by videobuf
939 * by the means of buf_queue callback. Only buffers previously queued to the
940 * driver by buf_queue can be passed to this function.
941 */
942void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
943{
944 struct vb2_queue *q = vb->vb2_queue;
945 unsigned long flags;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -0300946 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300947
948 if (vb->state != VB2_BUF_STATE_ACTIVE)
949 return;
950
951 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
952 return;
953
954 dprintk(4, "Done processing on buffer %d, state: %d\n",
Tushar Behera9b6f5dc2012-11-12 04:01:29 -0300955 vb->v4l2_buf.index, state);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300956
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -0300957 /* sync buffers */
958 for (plane = 0; plane < vb->num_planes; ++plane)
959 call_memop(q, finish, vb->planes[plane].mem_priv);
960
Pawel Osciake23ccc02010-10-11 10:56:41 -0300961 /* Add the buffer to the done buffers list */
962 spin_lock_irqsave(&q->done_lock, flags);
963 vb->state = state;
964 list_add_tail(&vb->done_entry, &q->done_list);
965 atomic_dec(&q->queued_count);
966 spin_unlock_irqrestore(&q->done_lock, flags);
967
968 /* Inform any processes that may be waiting for buffers */
969 wake_up(&q->done_wq);
970}
971EXPORT_SYMBOL_GPL(vb2_buffer_done);
972
973/**
Hans Verkuil32a77262012-09-28 06:12:53 -0300974 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
975 * v4l2_buffer by the userspace. The caller has already verified that struct
976 * v4l2_buffer has a valid number of planes.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300977 */
Hans Verkuil32a77262012-09-28 06:12:53 -0300978static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
Pawel Osciake23ccc02010-10-11 10:56:41 -0300979 struct v4l2_plane *v4l2_planes)
980{
981 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300982
983 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300984 /* Fill in driver-provided information for OUTPUT types */
985 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
986 /*
987 * Will have to go up to b->length when API starts
988 * accepting variable number of planes.
989 */
990 for (plane = 0; plane < vb->num_planes; ++plane) {
991 v4l2_planes[plane].bytesused =
992 b->m.planes[plane].bytesused;
993 v4l2_planes[plane].data_offset =
994 b->m.planes[plane].data_offset;
995 }
996 }
997
998 if (b->memory == V4L2_MEMORY_USERPTR) {
999 for (plane = 0; plane < vb->num_planes; ++plane) {
1000 v4l2_planes[plane].m.userptr =
1001 b->m.planes[plane].m.userptr;
1002 v4l2_planes[plane].length =
1003 b->m.planes[plane].length;
1004 }
1005 }
Sumit Semwalc5384042012-06-14 10:37:37 -03001006 if (b->memory == V4L2_MEMORY_DMABUF) {
1007 for (plane = 0; plane < vb->num_planes; ++plane) {
1008 v4l2_planes[plane].m.fd =
1009 b->m.planes[plane].m.fd;
1010 v4l2_planes[plane].length =
1011 b->m.planes[plane].length;
1012 v4l2_planes[plane].data_offset =
1013 b->m.planes[plane].data_offset;
1014 }
1015 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001016 } else {
1017 /*
1018 * Single-planar buffers do not use planes array,
1019 * so fill in relevant v4l2_buffer struct fields instead.
1020 * In videobuf we use our internal V4l2_planes struct for
1021 * single-planar buffers as well, for simplicity.
1022 */
Laurent Pinchartac706bf2012-12-17 07:38:16 -03001023 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -03001024 v4l2_planes[0].bytesused = b->bytesused;
Laurent Pinchartac706bf2012-12-17 07:38:16 -03001025 v4l2_planes[0].data_offset = 0;
1026 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001027
1028 if (b->memory == V4L2_MEMORY_USERPTR) {
1029 v4l2_planes[0].m.userptr = b->m.userptr;
1030 v4l2_planes[0].length = b->length;
1031 }
Sumit Semwalc5384042012-06-14 10:37:37 -03001032
1033 if (b->memory == V4L2_MEMORY_DMABUF) {
1034 v4l2_planes[0].m.fd = b->m.fd;
1035 v4l2_planes[0].length = b->length;
1036 v4l2_planes[0].data_offset = 0;
1037 }
1038
Pawel Osciake23ccc02010-10-11 10:56:41 -03001039 }
1040
Hans Verkuilf1343282014-02-24 14:44:50 -03001041 /* Zero flags that the vb2 core handles */
Sakari Ailus1b18e7a2012-10-22 17:10:16 -03001042 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
Sakari Ailus7ce6fd82014-02-25 19:08:52 -03001043 if ((vb->vb2_queue->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) !=
1044 V4L2_BUF_FLAG_TIMESTAMP_COPY || !V4L2_TYPE_IS_OUTPUT(b->type)) {
1045 /*
1046 * Non-COPY timestamps and non-OUTPUT queues will get
1047 * their timestamp and timestamp source flags from the
1048 * queue.
1049 */
1050 vb->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
1051 }
1052
Hans Verkuilf1343282014-02-24 14:44:50 -03001053 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
1054 /*
1055 * For output buffers mask out the timecode flag:
1056 * this will be handled later in vb2_internal_qbuf().
1057 * The 'field' is valid metadata for this output buffer
1058 * and so that needs to be copied here.
1059 */
1060 vb->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TIMECODE;
1061 vb->v4l2_buf.field = b->field;
1062 } else {
1063 /* Zero any output buffer flags as this is a capture buffer */
1064 vb->v4l2_buf.flags &= ~V4L2_BUFFER_OUT_FLAGS;
1065 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001066}
1067
1068/**
1069 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
1070 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001071static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001072{
1073 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1074 struct vb2_queue *q = vb->vb2_queue;
1075 void *mem_priv;
1076 unsigned int plane;
1077 int ret;
1078 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1079
Hans Verkuil32a77262012-09-28 06:12:53 -03001080 /* Copy relevant information provided by the userspace */
1081 __fill_vb2_buffer(vb, b, planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001082
1083 for (plane = 0; plane < vb->num_planes; ++plane) {
1084 /* Skip the plane if already verified */
Marek Szyprowskif0b7c7f2011-11-16 15:09:40 -03001085 if (vb->v4l2_planes[plane].m.userptr &&
1086 vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
Pawel Osciake23ccc02010-10-11 10:56:41 -03001087 && vb->v4l2_planes[plane].length == planes[plane].length)
1088 continue;
1089
1090 dprintk(3, "qbuf: userspace address for plane %d changed, "
1091 "reacquiring memory\n", plane);
1092
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001093 /* Check if the provided plane buffer is large enough */
1094 if (planes[plane].length < q->plane_sizes[plane]) {
Seung-Woo Kim2484a7e2013-08-20 04:48:06 -03001095 dprintk(1, "qbuf: provided buffer size %u is less than "
1096 "setup size %u for plane %d\n",
1097 planes[plane].length,
1098 q->plane_sizes[plane], plane);
Marek Szyprowski4c2625d2011-10-03 03:21:45 -03001099 ret = -EINVAL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001100 goto err;
1101 }
1102
Pawel Osciake23ccc02010-10-11 10:56:41 -03001103 /* Release previously acquired memory if present */
1104 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -03001105 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001106
1107 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001108 vb->v4l2_planes[plane].m.userptr = 0;
1109 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001110
1111 /* Acquire each plane's memory */
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001112 mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
1113 planes[plane].m.userptr,
1114 planes[plane].length, write);
1115 if (IS_ERR_OR_NULL(mem_priv)) {
1116 dprintk(1, "qbuf: failed acquiring userspace "
Pawel Osciake23ccc02010-10-11 10:56:41 -03001117 "memory for plane %d\n", plane);
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001118 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
1119 goto err;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001120 }
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001121 vb->planes[plane].mem_priv = mem_priv;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001122 }
1123
1124 /*
1125 * Call driver-specific initialization on the newly acquired buffer,
1126 * if provided.
1127 */
1128 ret = call_qop(q, buf_init, vb);
1129 if (ret) {
1130 dprintk(1, "qbuf: buffer initialization failed\n");
1131 goto err;
1132 }
1133
1134 /*
1135 * Now that everything is in order, copy relevant information
1136 * provided by userspace.
1137 */
1138 for (plane = 0; plane < vb->num_planes; ++plane)
1139 vb->v4l2_planes[plane] = planes[plane];
1140
1141 return 0;
1142err:
1143 /* In case of errors, release planes that were already acquired */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001144 for (plane = 0; plane < vb->num_planes; ++plane) {
1145 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -03001146 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001147 vb->planes[plane].mem_priv = NULL;
1148 vb->v4l2_planes[plane].m.userptr = 0;
1149 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001150 }
1151
1152 return ret;
1153}
1154
1155/**
1156 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1157 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001158static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001159{
Hans Verkuil32a77262012-09-28 06:12:53 -03001160 __fill_vb2_buffer(vb, b, vb->v4l2_planes);
1161 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001162}
1163
1164/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001165 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1166 */
1167static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1168{
1169 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1170 struct vb2_queue *q = vb->vb2_queue;
1171 void *mem_priv;
1172 unsigned int plane;
1173 int ret;
1174 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1175
Laurent Pinchart6f546c52014-01-01 09:10:48 -03001176 /* Copy relevant information provided by the userspace */
Sumit Semwalc5384042012-06-14 10:37:37 -03001177 __fill_vb2_buffer(vb, b, planes);
1178
1179 for (plane = 0; plane < vb->num_planes; ++plane) {
1180 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1181
1182 if (IS_ERR_OR_NULL(dbuf)) {
1183 dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
1184 plane);
1185 ret = -EINVAL;
1186 goto err;
1187 }
1188
1189 /* use DMABUF size if length is not provided */
1190 if (planes[plane].length == 0)
1191 planes[plane].length = dbuf->size;
1192
1193 if (planes[plane].length < planes[plane].data_offset +
1194 q->plane_sizes[plane]) {
Seung-Woo Kim77c07822013-11-29 04:50:29 -03001195 dprintk(1, "qbuf: invalid dmabuf length for plane %d\n",
1196 plane);
Sumit Semwalc5384042012-06-14 10:37:37 -03001197 ret = -EINVAL;
1198 goto err;
1199 }
1200
1201 /* Skip the plane if already verified */
1202 if (dbuf == vb->planes[plane].dbuf &&
1203 vb->v4l2_planes[plane].length == planes[plane].length) {
1204 dma_buf_put(dbuf);
1205 continue;
1206 }
1207
1208 dprintk(1, "qbuf: buffer for plane %d changed\n", plane);
1209
1210 /* Release previously acquired memory if present */
1211 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
1212 memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
1213
1214 /* Acquire each plane's memory */
1215 mem_priv = call_memop(q, attach_dmabuf, q->alloc_ctx[plane],
1216 dbuf, planes[plane].length, write);
1217 if (IS_ERR(mem_priv)) {
1218 dprintk(1, "qbuf: failed to attach dmabuf\n");
1219 ret = PTR_ERR(mem_priv);
1220 dma_buf_put(dbuf);
1221 goto err;
1222 }
1223
1224 vb->planes[plane].dbuf = dbuf;
1225 vb->planes[plane].mem_priv = mem_priv;
1226 }
1227
1228 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1229 * really we want to do this just before the DMA, not while queueing
1230 * the buffer(s)..
1231 */
1232 for (plane = 0; plane < vb->num_planes; ++plane) {
1233 ret = call_memop(q, map_dmabuf, vb->planes[plane].mem_priv);
1234 if (ret) {
1235 dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
1236 plane);
1237 goto err;
1238 }
1239 vb->planes[plane].dbuf_mapped = 1;
1240 }
1241
1242 /*
1243 * Call driver-specific initialization on the newly acquired buffer,
1244 * if provided.
1245 */
1246 ret = call_qop(q, buf_init, vb);
1247 if (ret) {
1248 dprintk(1, "qbuf: buffer initialization failed\n");
1249 goto err;
1250 }
1251
1252 /*
1253 * Now that everything is in order, copy relevant information
1254 * provided by userspace.
1255 */
1256 for (plane = 0; plane < vb->num_planes; ++plane)
1257 vb->v4l2_planes[plane] = planes[plane];
1258
1259 return 0;
1260err:
1261 /* In case of errors, release planes that were already acquired */
1262 __vb2_buf_dmabuf_put(vb);
1263
1264 return ret;
1265}
1266
1267/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001268 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1269 */
1270static void __enqueue_in_driver(struct vb2_buffer *vb)
1271{
1272 struct vb2_queue *q = vb->vb2_queue;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001273 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001274
1275 vb->state = VB2_BUF_STATE_ACTIVE;
1276 atomic_inc(&q->queued_count);
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001277
1278 /* sync buffers */
1279 for (plane = 0; plane < vb->num_planes; ++plane)
1280 call_memop(q, prepare, vb->planes[plane].mem_priv);
1281
Pawel Osciake23ccc02010-10-11 10:56:41 -03001282 q->ops->buf_queue(vb);
1283}
1284
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001285static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001286{
1287 struct vb2_queue *q = vb->vb2_queue;
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001288 struct rw_semaphore *mmap_sem;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001289 int ret;
1290
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001291 ret = __verify_length(vb, b);
Sylwester Nawrocki3a9621b2013-08-26 11:47:53 -03001292 if (ret < 0) {
1293 dprintk(1, "%s(): plane parameters verification failed: %d\n",
1294 __func__, ret);
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001295 return ret;
Sylwester Nawrocki3a9621b2013-08-26 11:47:53 -03001296 }
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001297
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001298 vb->state = VB2_BUF_STATE_PREPARING;
Hans Verkuilf1343282014-02-24 14:44:50 -03001299 vb->v4l2_buf.timestamp.tv_sec = 0;
1300 vb->v4l2_buf.timestamp.tv_usec = 0;
1301 vb->v4l2_buf.sequence = 0;
1302
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001303 switch (q->memory) {
1304 case V4L2_MEMORY_MMAP:
1305 ret = __qbuf_mmap(vb, b);
1306 break;
1307 case V4L2_MEMORY_USERPTR:
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001308 /*
Mauro Carvalho Chehabf103b5d2014-01-07 07:03:09 -02001309 * In case of user pointer buffers vb2 allocators need to get
1310 * direct access to userspace pages. This requires getting
1311 * the mmap semaphore for read access in the current process
1312 * structure. The same semaphore is taken before calling mmap
1313 * operation, while both qbuf/prepare_buf and mmap are called
1314 * by the driver or v4l2 core with the driver's lock held.
1315 * To avoid an AB-BA deadlock (mmap_sem then driver's lock in
1316 * mmap and driver's lock then mmap_sem in qbuf/prepare_buf),
1317 * the videobuf2 core releases the driver's lock, takes
1318 * mmap_sem and then takes the driver's lock again.
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001319 */
1320 mmap_sem = &current->mm->mmap_sem;
1321 call_qop(q, wait_prepare, q);
1322 down_read(mmap_sem);
1323 call_qop(q, wait_finish, q);
1324
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001325 ret = __qbuf_userptr(vb, b);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001326
1327 up_read(mmap_sem);
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001328 break;
Sumit Semwalc5384042012-06-14 10:37:37 -03001329 case V4L2_MEMORY_DMABUF:
1330 ret = __qbuf_dmabuf(vb, b);
1331 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001332 default:
1333 WARN(1, "Invalid queue type\n");
1334 ret = -EINVAL;
1335 }
1336
1337 if (!ret)
1338 ret = call_qop(q, buf_prepare, vb);
1339 if (ret)
1340 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001341 vb->state = ret ? VB2_BUF_STATE_DEQUEUED : VB2_BUF_STATE_PREPARED;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001342
1343 return ret;
1344}
1345
Laurent Pinchart012043b2013-08-09 08:11:26 -03001346static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
Hans Verkuil41381112013-12-13 13:13:39 -03001347 const char *opname)
Laurent Pinchart012043b2013-08-09 08:11:26 -03001348{
Laurent Pinchart012043b2013-08-09 08:11:26 -03001349 if (b->type != q->type) {
1350 dprintk(1, "%s(): invalid buffer type\n", opname);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001351 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001352 }
1353
1354 if (b->index >= q->num_buffers) {
1355 dprintk(1, "%s(): buffer index out of range\n", opname);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001356 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001357 }
1358
Hans Verkuil41381112013-12-13 13:13:39 -03001359 if (q->bufs[b->index] == NULL) {
Laurent Pinchart012043b2013-08-09 08:11:26 -03001360 /* Should never happen */
1361 dprintk(1, "%s(): buffer is NULL\n", opname);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001362 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001363 }
1364
1365 if (b->memory != q->memory) {
1366 dprintk(1, "%s(): invalid memory type\n", opname);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001367 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001368 }
1369
Hans Verkuil41381112013-12-13 13:13:39 -03001370 return __verify_planes_array(q->bufs[b->index], b);
Laurent Pinchart012043b2013-08-09 08:11:26 -03001371}
1372
Pawel Osciake23ccc02010-10-11 10:56:41 -03001373/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001374 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1375 * @q: videobuf2 queue
1376 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1377 * handler in driver
1378 *
1379 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1380 * This function:
1381 * 1) verifies the passed buffer,
1382 * 2) calls buf_prepare callback in the driver (if provided), in which
1383 * driver-specific buffer initialization can be performed,
1384 *
1385 * The return values from this function are intended to be directly returned
1386 * from vidioc_prepare_buf handler in driver.
1387 */
1388int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1389{
Hans Verkuil41381112013-12-13 13:13:39 -03001390 struct vb2_buffer *vb;
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001391 int ret;
Hans Verkuil41381112013-12-13 13:13:39 -03001392
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001393 if (q->fileio) {
1394 dprintk(1, "%s(): file io in progress\n", __func__);
1395 return -EBUSY;
1396 }
1397
1398 ret = vb2_queue_or_prepare_buf(q, b, "prepare_buf");
Hans Verkuil41381112013-12-13 13:13:39 -03001399 if (ret)
1400 return ret;
1401
1402 vb = q->bufs[b->index];
1403 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1404 dprintk(1, "%s(): invalid buffer state %d\n", __func__,
1405 vb->state);
1406 return -EINVAL;
1407 }
1408
1409 ret = __buf_prepare(vb, b);
1410 if (!ret) {
1411 /* Fill buffer information for the userspace */
1412 __fill_v4l2_buffer(vb, b);
1413
1414 dprintk(1, "%s() of buffer %d succeeded\n", __func__, vb->v4l2_buf.index);
1415 }
1416 return ret;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001417}
1418EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1419
Hans Verkuil02f142e2013-12-13 13:13:42 -03001420/**
1421 * vb2_start_streaming() - Attempt to start streaming.
1422 * @q: videobuf2 queue
1423 *
1424 * If there are not enough buffers, then retry_start_streaming is set to
1425 * 1 and 0 is returned. The next time a buffer is queued and
1426 * retry_start_streaming is 1, this function will be called again to
1427 * retry starting the DMA engine.
1428 */
1429static int vb2_start_streaming(struct vb2_queue *q)
1430{
1431 int ret;
1432
1433 /* Tell the driver to start streaming */
1434 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1435
1436 /*
1437 * If there are not enough buffers queued to start streaming, then
1438 * the start_streaming operation will return -ENOBUFS and you have to
1439 * retry when the next buffer is queued.
1440 */
1441 if (ret == -ENOBUFS) {
1442 dprintk(1, "qbuf: not enough buffers, retry when more buffers are queued.\n");
1443 q->retry_start_streaming = 1;
1444 return 0;
1445 }
1446 if (ret)
1447 dprintk(1, "qbuf: driver refused to start streaming\n");
1448 else
1449 q->retry_start_streaming = 0;
1450 return ret;
1451}
1452
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001453static int vb2_internal_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
Laurent Pinchart012043b2013-08-09 08:11:26 -03001454{
Hans Verkuil41381112013-12-13 13:13:39 -03001455 int ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
1456 struct vb2_buffer *vb;
1457
1458 if (ret)
1459 return ret;
1460
1461 vb = q->bufs[b->index];
1462 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1463 dprintk(1, "%s(): invalid buffer state %d\n", __func__,
1464 vb->state);
1465 return -EINVAL;
1466 }
Laurent Pinchart012043b2013-08-09 08:11:26 -03001467
1468 switch (vb->state) {
1469 case VB2_BUF_STATE_DEQUEUED:
1470 ret = __buf_prepare(vb, b);
1471 if (ret)
1472 return ret;
Hans Verkuil41381112013-12-13 13:13:39 -03001473 break;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001474 case VB2_BUF_STATE_PREPARED:
1475 break;
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001476 case VB2_BUF_STATE_PREPARING:
1477 dprintk(1, "qbuf: buffer still being prepared\n");
1478 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001479 default:
1480 dprintk(1, "qbuf: buffer already in use\n");
1481 return -EINVAL;
1482 }
1483
1484 /*
1485 * Add to the queued buffers list, a buffer will stay on it until
1486 * dequeued in dqbuf.
1487 */
1488 list_add_tail(&vb->queued_entry, &q->queued_list);
1489 vb->state = VB2_BUF_STATE_QUEUED;
Hans Verkuilf1343282014-02-24 14:44:50 -03001490 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1491 /*
1492 * For output buffers copy the timestamp if needed,
1493 * and the timecode field and flag if needed.
1494 */
Sakari Ailusc57ff792014-03-01 10:28:02 -03001495 if ((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
1496 V4L2_BUF_FLAG_TIMESTAMP_COPY)
Hans Verkuilf1343282014-02-24 14:44:50 -03001497 vb->v4l2_buf.timestamp = b->timestamp;
1498 vb->v4l2_buf.flags |= b->flags & V4L2_BUF_FLAG_TIMECODE;
1499 if (b->flags & V4L2_BUF_FLAG_TIMECODE)
1500 vb->v4l2_buf.timecode = b->timecode;
1501 }
Laurent Pinchart012043b2013-08-09 08:11:26 -03001502
1503 /*
1504 * If already streaming, give the buffer to driver for processing.
1505 * If not, the buffer will be given to driver on next streamon.
1506 */
1507 if (q->streaming)
1508 __enqueue_in_driver(vb);
1509
Hans Verkuil41381112013-12-13 13:13:39 -03001510 /* Fill buffer information for the userspace */
1511 __fill_v4l2_buffer(vb, b);
Laurent Pinchart012043b2013-08-09 08:11:26 -03001512
Hans Verkuil02f142e2013-12-13 13:13:42 -03001513 if (q->retry_start_streaming) {
1514 ret = vb2_start_streaming(q);
1515 if (ret)
1516 return ret;
1517 }
1518
Hans Verkuil41381112013-12-13 13:13:39 -03001519 dprintk(1, "%s() of buffer %d succeeded\n", __func__, vb->v4l2_buf.index);
1520 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001521}
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001522
1523/**
1524 * vb2_qbuf() - Queue a buffer from userspace
1525 * @q: videobuf2 queue
1526 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1527 * in driver
1528 *
1529 * Should be called from vidioc_qbuf ioctl handler of a driver.
1530 * This function:
1531 * 1) verifies the passed buffer,
1532 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1533 * which driver-specific buffer initialization can be performed,
1534 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1535 * callback for processing.
1536 *
1537 * The return values from this function are intended to be directly returned
1538 * from vidioc_qbuf handler in driver.
1539 */
1540int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1541{
1542 if (q->fileio) {
1543 dprintk(1, "%s(): file io in progress\n", __func__);
1544 return -EBUSY;
1545 }
1546
1547 return vb2_internal_qbuf(q, b);
1548}
Pawel Osciake23ccc02010-10-11 10:56:41 -03001549EXPORT_SYMBOL_GPL(vb2_qbuf);
1550
1551/**
1552 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1553 * for dequeuing
1554 *
1555 * Will sleep if required for nonblocking == false.
1556 */
1557static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1558{
1559 /*
1560 * All operations on vb_done_list are performed under done_lock
1561 * spinlock protection. However, buffers may be removed from
1562 * it and returned to userspace only while holding both driver's
1563 * lock and the done_lock spinlock. Thus we can be sure that as
1564 * long as we hold the driver's lock, the list will remain not
1565 * empty if list_empty() check succeeds.
1566 */
1567
1568 for (;;) {
1569 int ret;
1570
1571 if (!q->streaming) {
1572 dprintk(1, "Streaming off, will not wait for buffers\n");
1573 return -EINVAL;
1574 }
1575
1576 if (!list_empty(&q->done_list)) {
1577 /*
1578 * Found a buffer that we were waiting for.
1579 */
1580 break;
1581 }
1582
1583 if (nonblocking) {
1584 dprintk(1, "Nonblocking and no buffers to dequeue, "
1585 "will not wait\n");
1586 return -EAGAIN;
1587 }
1588
1589 /*
1590 * We are streaming and blocking, wait for another buffer to
1591 * become ready or for streamoff. Driver's lock is released to
1592 * allow streamoff or qbuf to be called while waiting.
1593 */
1594 call_qop(q, wait_prepare, q);
1595
1596 /*
1597 * All locks have been released, it is safe to sleep now.
1598 */
1599 dprintk(3, "Will sleep waiting for buffers\n");
1600 ret = wait_event_interruptible(q->done_wq,
1601 !list_empty(&q->done_list) || !q->streaming);
1602
1603 /*
1604 * We need to reevaluate both conditions again after reacquiring
1605 * the locks or return an error if one occurred.
1606 */
1607 call_qop(q, wait_finish, q);
Hans Verkuil32a77262012-09-28 06:12:53 -03001608 if (ret) {
1609 dprintk(1, "Sleep was interrupted\n");
Pawel Osciake23ccc02010-10-11 10:56:41 -03001610 return ret;
Hans Verkuil32a77262012-09-28 06:12:53 -03001611 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001612 }
1613 return 0;
1614}
1615
1616/**
1617 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1618 *
1619 * Will sleep if required for nonblocking == false.
1620 */
1621static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
Hans Verkuil32a77262012-09-28 06:12:53 -03001622 struct v4l2_buffer *b, int nonblocking)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001623{
1624 unsigned long flags;
1625 int ret;
1626
1627 /*
1628 * Wait for at least one buffer to become available on the done_list.
1629 */
1630 ret = __vb2_wait_for_done_vb(q, nonblocking);
1631 if (ret)
1632 return ret;
1633
1634 /*
1635 * Driver's lock has been held since we last verified that done_list
1636 * is not empty, so no need for another list_empty(done_list) check.
1637 */
1638 spin_lock_irqsave(&q->done_lock, flags);
1639 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
Hans Verkuil32a77262012-09-28 06:12:53 -03001640 /*
1641 * Only remove the buffer from done_list if v4l2_buffer can handle all
1642 * the planes.
1643 */
1644 ret = __verify_planes_array(*vb, b);
1645 if (!ret)
1646 list_del(&(*vb)->done_entry);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001647 spin_unlock_irqrestore(&q->done_lock, flags);
1648
Hans Verkuil32a77262012-09-28 06:12:53 -03001649 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001650}
1651
1652/**
1653 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1654 * @q: videobuf2 queue
1655 *
1656 * This function will wait until all buffers that have been given to the driver
1657 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1658 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1659 * taken, for example from stop_streaming() callback.
1660 */
1661int vb2_wait_for_all_buffers(struct vb2_queue *q)
1662{
1663 if (!q->streaming) {
1664 dprintk(1, "Streaming off, will not wait for buffers\n");
1665 return -EINVAL;
1666 }
1667
Hans Verkuil02f142e2013-12-13 13:13:42 -03001668 if (!q->retry_start_streaming)
1669 wait_event(q->done_wq, !atomic_read(&q->queued_count));
Pawel Osciake23ccc02010-10-11 10:56:41 -03001670 return 0;
1671}
1672EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1673
1674/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001675 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1676 */
1677static void __vb2_dqbuf(struct vb2_buffer *vb)
1678{
1679 struct vb2_queue *q = vb->vb2_queue;
1680 unsigned int i;
1681
1682 /* nothing to do if the buffer is already dequeued */
1683 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1684 return;
1685
1686 vb->state = VB2_BUF_STATE_DEQUEUED;
1687
1688 /* unmap DMABUF buffer */
1689 if (q->memory == V4L2_MEMORY_DMABUF)
1690 for (i = 0; i < vb->num_planes; ++i) {
1691 if (!vb->planes[i].dbuf_mapped)
1692 continue;
1693 call_memop(q, unmap_dmabuf, vb->planes[i].mem_priv);
1694 vb->planes[i].dbuf_mapped = 0;
1695 }
1696}
1697
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001698static int vb2_internal_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001699{
1700 struct vb2_buffer *vb = NULL;
1701 int ret;
1702
1703 if (b->type != q->type) {
1704 dprintk(1, "dqbuf: invalid buffer type\n");
1705 return -EINVAL;
1706 }
Hans Verkuil32a77262012-09-28 06:12:53 -03001707 ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
1708 if (ret < 0)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001709 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001710
1711 ret = call_qop(q, buf_finish, vb);
1712 if (ret) {
1713 dprintk(1, "dqbuf: buffer finish failed\n");
1714 return ret;
1715 }
1716
1717 switch (vb->state) {
1718 case VB2_BUF_STATE_DONE:
1719 dprintk(3, "dqbuf: Returning done buffer\n");
1720 break;
1721 case VB2_BUF_STATE_ERROR:
1722 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1723 break;
1724 default:
1725 dprintk(1, "dqbuf: Invalid buffer state\n");
1726 return -EINVAL;
1727 }
1728
1729 /* Fill buffer information for the userspace */
1730 __fill_v4l2_buffer(vb, b);
1731 /* Remove from videobuf queue */
1732 list_del(&vb->queued_entry);
Sumit Semwalc5384042012-06-14 10:37:37 -03001733 /* go back to dequeued state */
1734 __vb2_dqbuf(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001735
1736 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1737 vb->v4l2_buf.index, vb->state);
1738
Pawel Osciake23ccc02010-10-11 10:56:41 -03001739 return 0;
1740}
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001741
1742/**
1743 * vb2_dqbuf() - Dequeue a buffer to the userspace
1744 * @q: videobuf2 queue
1745 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1746 * in driver
1747 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1748 * buffers ready for dequeuing are present. Normally the driver
1749 * would be passing (file->f_flags & O_NONBLOCK) here
1750 *
1751 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1752 * This function:
1753 * 1) verifies the passed buffer,
1754 * 2) calls buf_finish callback in the driver (if provided), in which
1755 * driver can perform any additional operations that may be required before
1756 * returning the buffer to userspace, such as cache sync,
1757 * 3) the buffer struct members are filled with relevant information for
1758 * the userspace.
1759 *
1760 * The return values from this function are intended to be directly returned
1761 * from vidioc_dqbuf handler in driver.
1762 */
1763int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1764{
1765 if (q->fileio) {
1766 dprintk(1, "dqbuf: file io in progress\n");
1767 return -EBUSY;
1768 }
1769 return vb2_internal_dqbuf(q, b, nonblocking);
1770}
Pawel Osciake23ccc02010-10-11 10:56:41 -03001771EXPORT_SYMBOL_GPL(vb2_dqbuf);
1772
1773/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001774 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1775 *
1776 * Removes all queued buffers from driver's queue and all buffers queued by
1777 * userspace from videobuf's queue. Returns to state after reqbufs.
1778 */
1779static void __vb2_queue_cancel(struct vb2_queue *q)
1780{
1781 unsigned int i;
1782
Hans Verkuil02f142e2013-12-13 13:13:42 -03001783 if (q->retry_start_streaming) {
1784 q->retry_start_streaming = 0;
1785 q->streaming = 0;
1786 }
1787
Pawel Osciake23ccc02010-10-11 10:56:41 -03001788 /*
1789 * Tell driver to stop all transactions and release all queued
1790 * buffers.
1791 */
1792 if (q->streaming)
1793 call_qop(q, stop_streaming, q);
1794 q->streaming = 0;
1795
1796 /*
1797 * Remove all buffers from videobuf's list...
1798 */
1799 INIT_LIST_HEAD(&q->queued_list);
1800 /*
1801 * ...and done list; userspace will not receive any buffers it
1802 * has not already dequeued before initiating cancel.
1803 */
1804 INIT_LIST_HEAD(&q->done_list);
Marek Szyprowskiafdea8b2011-06-10 08:58:42 -03001805 atomic_set(&q->queued_count, 0);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001806 wake_up_all(&q->done_wq);
1807
1808 /*
1809 * Reinitialize all buffers for next use.
1810 */
1811 for (i = 0; i < q->num_buffers; ++i)
Sumit Semwalc5384042012-06-14 10:37:37 -03001812 __vb2_dqbuf(q->bufs[i]);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001813}
1814
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001815static int vb2_internal_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001816{
1817 struct vb2_buffer *vb;
1818 int ret;
1819
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001820 if (type != q->type) {
1821 dprintk(1, "streamon: invalid stream type\n");
1822 return -EINVAL;
1823 }
1824
1825 if (q->streaming) {
Ricardo Ribaldaf9560352013-11-08 07:08:45 -03001826 dprintk(3, "streamon successful: already streaming\n");
1827 return 0;
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001828 }
1829
Ricardo Ribalda548df782014-01-08 05:01:33 -03001830 if (!q->num_buffers) {
1831 dprintk(1, "streamon: no buffers have been allocated\n");
1832 return -EINVAL;
1833 }
1834
Ricardo Ribalda Delgado249f5a52014-01-08 05:01:33 -03001835 if (!q->num_buffers) {
1836 dprintk(1, "streamon: no buffers have been allocated\n");
1837 return -EINVAL;
1838 }
1839
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001840 /*
1841 * If any buffers were queued before streamon,
1842 * we can now pass them to driver for processing.
1843 */
1844 list_for_each_entry(vb, &q->queued_list, queued_entry)
1845 __enqueue_in_driver(vb);
1846
Hans Verkuil02f142e2013-12-13 13:13:42 -03001847 /* Tell driver to start streaming. */
1848 ret = vb2_start_streaming(q);
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001849 if (ret) {
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001850 __vb2_queue_cancel(q);
1851 return ret;
1852 }
1853
1854 q->streaming = 1;
1855
1856 dprintk(3, "Streamon successful\n");
1857 return 0;
1858}
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001859
1860/**
1861 * vb2_streamon - start streaming
1862 * @q: videobuf2 queue
1863 * @type: type argument passed from userspace to vidioc_streamon handler
1864 *
1865 * Should be called from vidioc_streamon handler of a driver.
1866 * This function:
1867 * 1) verifies current state
1868 * 2) passes any previously queued buffers to the driver and starts streaming
1869 *
1870 * The return values from this function are intended to be directly returned
1871 * from vidioc_streamon handler in the driver.
1872 */
1873int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1874{
1875 if (q->fileio) {
1876 dprintk(1, "streamon: file io in progress\n");
1877 return -EBUSY;
1878 }
1879 return vb2_internal_streamon(q, type);
1880}
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001881EXPORT_SYMBOL_GPL(vb2_streamon);
1882
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001883static int vb2_internal_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1884{
1885 if (type != q->type) {
1886 dprintk(1, "streamoff: invalid stream type\n");
1887 return -EINVAL;
1888 }
1889
1890 if (!q->streaming) {
1891 dprintk(3, "streamoff successful: not streaming\n");
1892 return 0;
1893 }
1894
1895 /*
1896 * Cancel will pause streaming and remove all buffers from the driver
1897 * and videobuf, effectively returning control over them to userspace.
1898 */
1899 __vb2_queue_cancel(q);
1900
1901 dprintk(3, "Streamoff successful\n");
1902 return 0;
1903}
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001904
1905/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001906 * vb2_streamoff - stop streaming
1907 * @q: videobuf2 queue
1908 * @type: type argument passed from userspace to vidioc_streamoff handler
1909 *
1910 * Should be called from vidioc_streamoff handler of a driver.
1911 * This function:
1912 * 1) verifies current state,
1913 * 2) stop streaming and dequeues any queued buffers, including those previously
1914 * passed to the driver (after waiting for the driver to finish).
1915 *
1916 * This call can be used for pausing playback.
1917 * The return values from this function are intended to be directly returned
1918 * from vidioc_streamoff handler in the driver
1919 */
1920int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1921{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001922 if (q->fileio) {
1923 dprintk(1, "streamoff: file io in progress\n");
1924 return -EBUSY;
1925 }
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001926 return vb2_internal_streamoff(q, type);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001927}
1928EXPORT_SYMBOL_GPL(vb2_streamoff);
1929
1930/**
1931 * __find_plane_by_offset() - find plane associated with the given offset off
1932 */
1933static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1934 unsigned int *_buffer, unsigned int *_plane)
1935{
1936 struct vb2_buffer *vb;
1937 unsigned int buffer, plane;
1938
1939 /*
1940 * Go over all buffers and their planes, comparing the given offset
1941 * with an offset assigned to each plane. If a match is found,
1942 * return its buffer and plane numbers.
1943 */
1944 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1945 vb = q->bufs[buffer];
1946
1947 for (plane = 0; plane < vb->num_planes; ++plane) {
1948 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1949 *_buffer = buffer;
1950 *_plane = plane;
1951 return 0;
1952 }
1953 }
1954 }
1955
1956 return -EINVAL;
1957}
1958
1959/**
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03001960 * vb2_expbuf() - Export a buffer as a file descriptor
1961 * @q: videobuf2 queue
1962 * @eb: export buffer structure passed from userspace to vidioc_expbuf
1963 * handler in driver
1964 *
1965 * The return values from this function are intended to be directly returned
1966 * from vidioc_expbuf handler in driver.
1967 */
1968int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
1969{
1970 struct vb2_buffer *vb = NULL;
1971 struct vb2_plane *vb_plane;
1972 int ret;
1973 struct dma_buf *dbuf;
1974
1975 if (q->memory != V4L2_MEMORY_MMAP) {
1976 dprintk(1, "Queue is not currently set up for mmap\n");
1977 return -EINVAL;
1978 }
1979
1980 if (!q->mem_ops->get_dmabuf) {
1981 dprintk(1, "Queue does not support DMA buffer exporting\n");
1982 return -EINVAL;
1983 }
1984
Philipp Zabelea3aba82013-05-21 05:11:35 -03001985 if (eb->flags & ~(O_CLOEXEC | O_ACCMODE)) {
1986 dprintk(1, "Queue does support only O_CLOEXEC and access mode flags\n");
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03001987 return -EINVAL;
1988 }
1989
1990 if (eb->type != q->type) {
1991 dprintk(1, "qbuf: invalid buffer type\n");
1992 return -EINVAL;
1993 }
1994
1995 if (eb->index >= q->num_buffers) {
1996 dprintk(1, "buffer index out of range\n");
1997 return -EINVAL;
1998 }
1999
2000 vb = q->bufs[eb->index];
2001
2002 if (eb->plane >= vb->num_planes) {
2003 dprintk(1, "buffer plane out of range\n");
2004 return -EINVAL;
2005 }
2006
2007 vb_plane = &vb->planes[eb->plane];
2008
Philipp Zabelea3aba82013-05-21 05:11:35 -03002009 dbuf = call_memop(q, get_dmabuf, vb_plane->mem_priv, eb->flags & O_ACCMODE);
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002010 if (IS_ERR_OR_NULL(dbuf)) {
2011 dprintk(1, "Failed to export buffer %d, plane %d\n",
2012 eb->index, eb->plane);
2013 return -EINVAL;
2014 }
2015
Philipp Zabelea3aba82013-05-21 05:11:35 -03002016 ret = dma_buf_fd(dbuf, eb->flags & ~O_ACCMODE);
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002017 if (ret < 0) {
2018 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
2019 eb->index, eb->plane, ret);
2020 dma_buf_put(dbuf);
2021 return ret;
2022 }
2023
2024 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
2025 eb->index, eb->plane, ret);
2026 eb->fd = ret;
2027
2028 return 0;
2029}
2030EXPORT_SYMBOL_GPL(vb2_expbuf);
2031
2032/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03002033 * vb2_mmap() - map video buffers into application address space
2034 * @q: videobuf2 queue
2035 * @vma: vma passed to the mmap file operation handler in the driver
2036 *
2037 * Should be called from mmap file operation handler of a driver.
2038 * This function maps one plane of one of the available video buffers to
2039 * userspace. To map whole video memory allocated on reqbufs, this function
2040 * has to be called once per each plane per each buffer previously allocated.
2041 *
2042 * When the userspace application calls mmap, it passes to it an offset returned
2043 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
2044 * a "cookie", which is then used to identify the plane to be mapped.
2045 * This function finds a plane with a matching offset and a mapping is performed
2046 * by the means of a provided memory operation.
2047 *
2048 * The return values from this function are intended to be directly returned
2049 * from the mmap handler in driver.
2050 */
2051int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
2052{
2053 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002054 struct vb2_buffer *vb;
2055 unsigned int buffer, plane;
2056 int ret;
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -03002057 unsigned long length;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002058
2059 if (q->memory != V4L2_MEMORY_MMAP) {
2060 dprintk(1, "Queue is not currently set up for mmap\n");
2061 return -EINVAL;
2062 }
2063
2064 /*
2065 * Check memory area access mode.
2066 */
2067 if (!(vma->vm_flags & VM_SHARED)) {
2068 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
2069 return -EINVAL;
2070 }
2071 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
2072 if (!(vma->vm_flags & VM_WRITE)) {
2073 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
2074 return -EINVAL;
2075 }
2076 } else {
2077 if (!(vma->vm_flags & VM_READ)) {
2078 dprintk(1, "Invalid vma flags, VM_READ needed\n");
2079 return -EINVAL;
2080 }
2081 }
2082
2083 /*
2084 * Find the plane corresponding to the offset passed by userspace.
2085 */
2086 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2087 if (ret)
2088 return ret;
2089
2090 vb = q->bufs[buffer];
Pawel Osciake23ccc02010-10-11 10:56:41 -03002091
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -03002092 /*
2093 * MMAP requires page_aligned buffers.
2094 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
2095 * so, we need to do the same here.
2096 */
2097 length = PAGE_ALIGN(vb->v4l2_planes[plane].length);
2098 if (length < (vma->vm_end - vma->vm_start)) {
2099 dprintk(1,
2100 "MMAP invalid, as it would overflow buffer length\n");
Seung-Woo Kim068a0df2013-04-11 23:57:57 -03002101 return -EINVAL;
2102 }
2103
Marek Szyprowskia00d0262011-12-15 05:53:06 -03002104 ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002105 if (ret)
2106 return ret;
2107
Pawel Osciake23ccc02010-10-11 10:56:41 -03002108 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
2109 return 0;
2110}
2111EXPORT_SYMBOL_GPL(vb2_mmap);
2112
Scott Jiang6f524ec2011-09-21 09:25:23 -03002113#ifndef CONFIG_MMU
2114unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
2115 unsigned long addr,
2116 unsigned long len,
2117 unsigned long pgoff,
2118 unsigned long flags)
2119{
2120 unsigned long off = pgoff << PAGE_SHIFT;
2121 struct vb2_buffer *vb;
2122 unsigned int buffer, plane;
2123 int ret;
2124
2125 if (q->memory != V4L2_MEMORY_MMAP) {
2126 dprintk(1, "Queue is not currently set up for mmap\n");
2127 return -EINVAL;
2128 }
2129
2130 /*
2131 * Find the plane corresponding to the offset passed by userspace.
2132 */
2133 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2134 if (ret)
2135 return ret;
2136
2137 vb = q->bufs[buffer];
2138
2139 return (unsigned long)vb2_plane_vaddr(vb, plane);
2140}
2141EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
2142#endif
2143
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002144static int __vb2_init_fileio(struct vb2_queue *q, int read);
2145static int __vb2_cleanup_fileio(struct vb2_queue *q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002146
2147/**
2148 * vb2_poll() - implements poll userspace operation
2149 * @q: videobuf2 queue
2150 * @file: file argument passed to the poll file operation handler
2151 * @wait: wait argument passed to the poll file operation handler
2152 *
2153 * This function implements poll file operation handler for a driver.
2154 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
2155 * be informed that the file descriptor of a video device is available for
2156 * reading.
2157 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
2158 * will be reported as available for writing.
2159 *
Hans Verkuil95213ce2011-07-13 04:26:52 -03002160 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
2161 * pending events.
2162 *
Pawel Osciake23ccc02010-10-11 10:56:41 -03002163 * The return values from this function are intended to be directly returned
2164 * from poll handler in driver.
2165 */
2166unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
2167{
Hans Verkuil95213ce2011-07-13 04:26:52 -03002168 struct video_device *vfd = video_devdata(file);
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002169 unsigned long req_events = poll_requested_events(wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002170 struct vb2_buffer *vb = NULL;
Hans Verkuil95213ce2011-07-13 04:26:52 -03002171 unsigned int res = 0;
2172 unsigned long flags;
2173
2174 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
2175 struct v4l2_fh *fh = file->private_data;
2176
2177 if (v4l2_event_pending(fh))
2178 res = POLLPRI;
2179 else if (req_events & POLLPRI)
2180 poll_wait(file, &fh->wait, wait);
2181 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03002182
Hans Verkuilcd138232013-01-30 13:29:02 -03002183 if (!V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLIN | POLLRDNORM)))
2184 return res;
2185 if (V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLOUT | POLLWRNORM)))
2186 return res;
2187
Pawel Osciake23ccc02010-10-11 10:56:41 -03002188 /*
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03002189 * Start file I/O emulator only if streaming API has not been used yet.
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002190 */
2191 if (q->num_buffers == 0 && q->fileio == NULL) {
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002192 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2193 (req_events & (POLLIN | POLLRDNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002194 if (__vb2_init_fileio(q, 1))
2195 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002196 }
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002197 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2198 (req_events & (POLLOUT | POLLWRNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002199 if (__vb2_init_fileio(q, 0))
2200 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002201 /*
2202 * Write to OUTPUT queue can be done immediately.
2203 */
Hans Verkuil95213ce2011-07-13 04:26:52 -03002204 return res | POLLOUT | POLLWRNORM;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002205 }
2206 }
2207
2208 /*
Pawel Osciake23ccc02010-10-11 10:56:41 -03002209 * There is nothing to wait for if no buffers have already been queued.
2210 */
2211 if (list_empty(&q->queued_list))
Hans Verkuil95213ce2011-07-13 04:26:52 -03002212 return res | POLLERR;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002213
Seung-Woo Kim412cb872013-05-20 23:47:29 -03002214 if (list_empty(&q->done_list))
2215 poll_wait(file, &q->done_wq, wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002216
2217 /*
2218 * Take first buffer available for dequeuing.
2219 */
2220 spin_lock_irqsave(&q->done_lock, flags);
2221 if (!list_empty(&q->done_list))
2222 vb = list_first_entry(&q->done_list, struct vb2_buffer,
2223 done_entry);
2224 spin_unlock_irqrestore(&q->done_lock, flags);
2225
2226 if (vb && (vb->state == VB2_BUF_STATE_DONE
2227 || vb->state == VB2_BUF_STATE_ERROR)) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002228 return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
2229 res | POLLOUT | POLLWRNORM :
2230 res | POLLIN | POLLRDNORM;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002231 }
Hans Verkuil95213ce2011-07-13 04:26:52 -03002232 return res;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002233}
2234EXPORT_SYMBOL_GPL(vb2_poll);
2235
2236/**
2237 * vb2_queue_init() - initialize a videobuf2 queue
2238 * @q: videobuf2 queue; this structure should be allocated in driver
2239 *
2240 * The vb2_queue structure should be allocated by the driver. The driver is
2241 * responsible of clearing it's content and setting initial values for some
2242 * required entries before calling this function.
2243 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2244 * to the struct vb2_queue description in include/media/videobuf2-core.h
2245 * for more information.
2246 */
2247int vb2_queue_init(struct vb2_queue *q)
2248{
Ezequiel Garcia896f38f2012-09-17 14:59:30 -03002249 /*
2250 * Sanity check
2251 */
2252 if (WARN_ON(!q) ||
2253 WARN_ON(!q->ops) ||
2254 WARN_ON(!q->mem_ops) ||
2255 WARN_ON(!q->type) ||
2256 WARN_ON(!q->io_modes) ||
2257 WARN_ON(!q->ops->queue_setup) ||
Kamil Debski6aa69f92013-01-25 06:29:57 -03002258 WARN_ON(!q->ops->buf_queue) ||
Sakari Ailus872484c2013-08-25 17:57:03 -03002259 WARN_ON(q->timestamp_flags &
2260 ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
2261 V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
Ezequiel Garcia896f38f2012-09-17 14:59:30 -03002262 return -EINVAL;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002263
Kamil Debski6aa69f92013-01-25 06:29:57 -03002264 /* Warn that the driver should choose an appropriate timestamp type */
Sakari Ailusc57ff792014-03-01 10:28:02 -03002265 WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
2266 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
Kamil Debski6aa69f92013-01-25 06:29:57 -03002267
Pawel Osciake23ccc02010-10-11 10:56:41 -03002268 INIT_LIST_HEAD(&q->queued_list);
2269 INIT_LIST_HEAD(&q->done_list);
2270 spin_lock_init(&q->done_lock);
2271 init_waitqueue_head(&q->done_wq);
2272
2273 if (q->buf_struct_size == 0)
2274 q->buf_struct_size = sizeof(struct vb2_buffer);
2275
2276 return 0;
2277}
2278EXPORT_SYMBOL_GPL(vb2_queue_init);
2279
2280/**
2281 * vb2_queue_release() - stop streaming, release the queue and free memory
2282 * @q: videobuf2 queue
2283 *
2284 * This function stops streaming and performs necessary clean ups, including
2285 * freeing video buffer memory. The driver is responsible for freeing
2286 * the vb2_queue structure itself.
2287 */
2288void vb2_queue_release(struct vb2_queue *q)
2289{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002290 __vb2_cleanup_fileio(q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002291 __vb2_queue_cancel(q);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03002292 __vb2_queue_free(q, q->num_buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002293}
2294EXPORT_SYMBOL_GPL(vb2_queue_release);
2295
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002296/**
2297 * struct vb2_fileio_buf - buffer context used by file io emulator
2298 *
2299 * vb2 provides a compatibility layer and emulator of file io (read and
2300 * write) calls on top of streaming API. This structure is used for
2301 * tracking context related to the buffers.
2302 */
2303struct vb2_fileio_buf {
2304 void *vaddr;
2305 unsigned int size;
2306 unsigned int pos;
2307 unsigned int queued:1;
2308};
2309
2310/**
2311 * struct vb2_fileio_data - queue context used by file io emulator
2312 *
Hans Verkuil4e5a4d82014-02-14 06:46:50 -03002313 * @cur_index: the index of the buffer currently being read from or
2314 * written to. If equal to q->num_buffers then a new buffer
2315 * must be dequeued.
2316 * @initial_index: in the read() case all buffers are queued up immediately
2317 * in __vb2_init_fileio() and __vb2_perform_fileio() just cycles
2318 * buffers. However, in the write() case no buffers are initially
2319 * queued, instead whenever a buffer is full it is queued up by
2320 * __vb2_perform_fileio(). Only once all available buffers have
2321 * been queued up will __vb2_perform_fileio() start to dequeue
2322 * buffers. This means that initially __vb2_perform_fileio()
2323 * needs to know what buffer index to use when it is queuing up
2324 * the buffers for the first time. That initial index is stored
2325 * in this field. Once it is equal to q->num_buffers all
2326 * available buffers have been queued and __vb2_perform_fileio()
2327 * should start the normal dequeue/queue cycle.
2328 *
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002329 * vb2 provides a compatibility layer and emulator of file io (read and
2330 * write) calls on top of streaming API. For proper operation it required
2331 * this structure to save the driver state between each call of the read
2332 * or write function.
2333 */
2334struct vb2_fileio_data {
2335 struct v4l2_requestbuffers req;
2336 struct v4l2_buffer b;
2337 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
Hans Verkuil4e5a4d82014-02-14 06:46:50 -03002338 unsigned int cur_index;
2339 unsigned int initial_index;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002340 unsigned int q_count;
2341 unsigned int dq_count;
2342 unsigned int flags;
2343};
2344
2345/**
2346 * __vb2_init_fileio() - initialize file io emulator
2347 * @q: videobuf2 queue
2348 * @read: mode selector (1 means read, 0 means write)
2349 */
2350static int __vb2_init_fileio(struct vb2_queue *q, int read)
2351{
2352 struct vb2_fileio_data *fileio;
2353 int i, ret;
2354 unsigned int count = 0;
2355
2356 /*
2357 * Sanity check
2358 */
2359 if ((read && !(q->io_modes & VB2_READ)) ||
2360 (!read && !(q->io_modes & VB2_WRITE)))
2361 BUG();
2362
2363 /*
2364 * Check if device supports mapping buffers to kernel virtual space.
2365 */
2366 if (!q->mem_ops->vaddr)
2367 return -EBUSY;
2368
2369 /*
2370 * Check if streaming api has not been already activated.
2371 */
2372 if (q->streaming || q->num_buffers > 0)
2373 return -EBUSY;
2374
2375 /*
2376 * Start with count 1, driver can increase it in queue_setup()
2377 */
2378 count = 1;
2379
2380 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
2381 (read) ? "read" : "write", count, q->io_flags);
2382
2383 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
2384 if (fileio == NULL)
2385 return -ENOMEM;
2386
2387 fileio->flags = q->io_flags;
2388
2389 /*
2390 * Request buffers and use MMAP type to force driver
2391 * to allocate buffers by itself.
2392 */
2393 fileio->req.count = count;
2394 fileio->req.memory = V4L2_MEMORY_MMAP;
2395 fileio->req.type = q->type;
2396 ret = vb2_reqbufs(q, &fileio->req);
2397 if (ret)
2398 goto err_kfree;
2399
2400 /*
2401 * Check if plane_count is correct
2402 * (multiplane buffers are not supported).
2403 */
2404 if (q->bufs[0]->num_planes != 1) {
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002405 ret = -EBUSY;
2406 goto err_reqbufs;
2407 }
2408
2409 /*
2410 * Get kernel address of each buffer.
2411 */
2412 for (i = 0; i < q->num_buffers; i++) {
2413 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
Wei Yongjun5dd69462013-05-13 01:48:45 -03002414 if (fileio->bufs[i].vaddr == NULL) {
2415 ret = -EINVAL;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002416 goto err_reqbufs;
Wei Yongjun5dd69462013-05-13 01:48:45 -03002417 }
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002418 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
2419 }
2420
2421 /*
2422 * Read mode requires pre queuing of all buffers.
2423 */
2424 if (read) {
2425 /*
2426 * Queue all buffers.
2427 */
2428 for (i = 0; i < q->num_buffers; i++) {
2429 struct v4l2_buffer *b = &fileio->b;
2430 memset(b, 0, sizeof(*b));
2431 b->type = q->type;
2432 b->memory = q->memory;
2433 b->index = i;
2434 ret = vb2_qbuf(q, b);
2435 if (ret)
2436 goto err_reqbufs;
2437 fileio->bufs[i].queued = 1;
2438 }
Hans Verkuil4e5a4d82014-02-14 06:46:50 -03002439 /*
2440 * All buffers have been queued, so mark that by setting
2441 * initial_index to q->num_buffers
2442 */
2443 fileio->initial_index = q->num_buffers;
2444 fileio->cur_index = q->num_buffers;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002445 }
2446
Hans Verkuil02f142e2013-12-13 13:13:42 -03002447 /*
2448 * Start streaming.
2449 */
2450 ret = vb2_streamon(q, q->type);
2451 if (ret)
2452 goto err_reqbufs;
2453
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002454 q->fileio = fileio;
2455
2456 return ret;
2457
2458err_reqbufs:
Hans de Goedea67e1722012-05-08 14:47:39 -03002459 fileio->req.count = 0;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002460 vb2_reqbufs(q, &fileio->req);
2461
2462err_kfree:
2463 kfree(fileio);
2464 return ret;
2465}
2466
2467/**
2468 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2469 * @q: videobuf2 queue
2470 */
2471static int __vb2_cleanup_fileio(struct vb2_queue *q)
2472{
2473 struct vb2_fileio_data *fileio = q->fileio;
2474
2475 if (fileio) {
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002476 vb2_internal_streamoff(q, q->type);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002477 q->fileio = NULL;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002478 fileio->req.count = 0;
2479 vb2_reqbufs(q, &fileio->req);
2480 kfree(fileio);
2481 dprintk(3, "file io emulator closed\n");
2482 }
2483 return 0;
2484}
2485
2486/**
2487 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2488 * @q: videobuf2 queue
2489 * @data: pointed to target userspace buffer
2490 * @count: number of bytes to read or write
2491 * @ppos: file handle position tracking pointer
2492 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2493 * @read: access mode selector (1 means read, 0 means write)
2494 */
2495static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
2496 loff_t *ppos, int nonblock, int read)
2497{
2498 struct vb2_fileio_data *fileio;
2499 struct vb2_fileio_buf *buf;
2500 int ret, index;
2501
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002502 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002503 read ? "read" : "write", (long)*ppos, count,
2504 nonblock ? "non" : "");
2505
2506 if (!data)
2507 return -EINVAL;
2508
2509 /*
2510 * Initialize emulator on first call.
2511 */
2512 if (!q->fileio) {
2513 ret = __vb2_init_fileio(q, read);
2514 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
2515 if (ret)
2516 return ret;
2517 }
2518 fileio = q->fileio;
2519
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002520 /*
2521 * Check if we need to dequeue the buffer.
2522 */
Hans Verkuil4e5a4d82014-02-14 06:46:50 -03002523 index = fileio->cur_index;
Hans Verkuil88e26872013-12-13 13:13:45 -03002524 if (index >= q->num_buffers) {
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002525 /*
2526 * Call vb2_dqbuf to get buffer back.
2527 */
2528 memset(&fileio->b, 0, sizeof(fileio->b));
2529 fileio->b.type = q->type;
2530 fileio->b.memory = q->memory;
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002531 ret = vb2_internal_dqbuf(q, &fileio->b, nonblock);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002532 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2533 if (ret)
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002534 return ret;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002535 fileio->dq_count += 1;
2536
Hans Verkuil4e5a4d82014-02-14 06:46:50 -03002537 fileio->cur_index = index = fileio->b.index;
Hans Verkuil88e26872013-12-13 13:13:45 -03002538 buf = &fileio->bufs[index];
2539
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002540 /*
2541 * Get number of bytes filled by the driver
2542 */
Hans Verkuil88e26872013-12-13 13:13:45 -03002543 buf->pos = 0;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002544 buf->queued = 0;
Hans Verkuil88e26872013-12-13 13:13:45 -03002545 buf->size = read ? vb2_get_plane_payload(q->bufs[index], 0)
2546 : vb2_plane_size(q->bufs[index], 0);
2547 } else {
2548 buf = &fileio->bufs[index];
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002549 }
2550
2551 /*
2552 * Limit count on last few bytes of the buffer.
2553 */
2554 if (buf->pos + count > buf->size) {
2555 count = buf->size - buf->pos;
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002556 dprintk(5, "reducing read count: %zd\n", count);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002557 }
2558
2559 /*
2560 * Transfer data to userspace.
2561 */
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002562 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002563 count, index, buf->pos);
2564 if (read)
2565 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2566 else
2567 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2568 if (ret) {
2569 dprintk(3, "file io: error copying data\n");
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002570 return -EFAULT;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002571 }
2572
2573 /*
2574 * Update counters.
2575 */
2576 buf->pos += count;
2577 *ppos += count;
2578
2579 /*
2580 * Queue next buffer if required.
2581 */
2582 if (buf->pos == buf->size ||
2583 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2584 /*
2585 * Check if this is the last buffer to read.
2586 */
2587 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2588 fileio->dq_count == 1) {
2589 dprintk(3, "file io: read limit reached\n");
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002590 return __vb2_cleanup_fileio(q);
2591 }
2592
2593 /*
2594 * Call vb2_qbuf and give buffer to the driver.
2595 */
2596 memset(&fileio->b, 0, sizeof(fileio->b));
2597 fileio->b.type = q->type;
2598 fileio->b.memory = q->memory;
2599 fileio->b.index = index;
2600 fileio->b.bytesused = buf->pos;
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002601 ret = vb2_internal_qbuf(q, &fileio->b);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002602 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2603 if (ret)
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002604 return ret;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002605
2606 /*
2607 * Buffer has been queued, update the status
2608 */
2609 buf->pos = 0;
2610 buf->queued = 1;
Hans Verkuil88e26872013-12-13 13:13:45 -03002611 buf->size = vb2_plane_size(q->bufs[index], 0);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002612 fileio->q_count += 1;
Hans Verkuil4e5a4d82014-02-14 06:46:50 -03002613 /*
2614 * If we are queuing up buffers for the first time, then
2615 * increase initial_index by one.
2616 */
2617 if (fileio->initial_index < q->num_buffers)
2618 fileio->initial_index++;
2619 /*
2620 * The next buffer to use is either a buffer that's going to be
2621 * queued for the first time (initial_index < q->num_buffers)
2622 * or it is equal to q->num_buffers, meaning that the next
2623 * time we need to dequeue a buffer since we've now queued up
2624 * all the 'first time' buffers.
2625 */
2626 fileio->cur_index = fileio->initial_index;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002627 }
2628
2629 /*
2630 * Return proper number of bytes processed.
2631 */
2632 if (ret == 0)
2633 ret = count;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002634 return ret;
2635}
2636
2637size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2638 loff_t *ppos, int nonblocking)
2639{
2640 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2641}
2642EXPORT_SYMBOL_GPL(vb2_read);
2643
Ricardo Ribalda819585b2013-08-28 04:39:29 -03002644size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002645 loff_t *ppos, int nonblocking)
2646{
Ricardo Ribalda819585b2013-08-28 04:39:29 -03002647 return __vb2_perform_fileio(q, (char __user *) data, count,
2648 ppos, nonblocking, 0);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002649}
2650EXPORT_SYMBOL_GPL(vb2_write);
2651
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002652
2653/*
2654 * The following functions are not part of the vb2 core API, but are helper
2655 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2656 * and struct vb2_ops.
2657 * They contain boilerplate code that most if not all drivers have to do
2658 * and so they simplify the driver code.
2659 */
2660
2661/* The queue is busy if there is a owner and you are not that owner. */
2662static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
2663{
2664 return vdev->queue->owner && vdev->queue->owner != file->private_data;
2665}
2666
2667/* vb2 ioctl helpers */
2668
2669int vb2_ioctl_reqbufs(struct file *file, void *priv,
2670 struct v4l2_requestbuffers *p)
2671{
2672 struct video_device *vdev = video_devdata(file);
2673 int res = __verify_memory_type(vdev->queue, p->memory, p->type);
2674
2675 if (res)
2676 return res;
2677 if (vb2_queue_is_busy(vdev, file))
2678 return -EBUSY;
2679 res = __reqbufs(vdev->queue, p);
2680 /* If count == 0, then the owner has released all buffers and he
2681 is no longer owner of the queue. Otherwise we have a new owner. */
2682 if (res == 0)
2683 vdev->queue->owner = p->count ? file->private_data : NULL;
2684 return res;
2685}
2686EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
2687
2688int vb2_ioctl_create_bufs(struct file *file, void *priv,
2689 struct v4l2_create_buffers *p)
2690{
2691 struct video_device *vdev = video_devdata(file);
2692 int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
2693
2694 p->index = vdev->queue->num_buffers;
2695 /* If count == 0, then just check if memory and type are valid.
2696 Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2697 if (p->count == 0)
2698 return res != -EBUSY ? res : 0;
2699 if (res)
2700 return res;
2701 if (vb2_queue_is_busy(vdev, file))
2702 return -EBUSY;
2703 res = __create_bufs(vdev->queue, p);
2704 if (res == 0)
2705 vdev->queue->owner = file->private_data;
2706 return res;
2707}
2708EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
2709
2710int vb2_ioctl_prepare_buf(struct file *file, void *priv,
2711 struct v4l2_buffer *p)
2712{
2713 struct video_device *vdev = video_devdata(file);
2714
2715 if (vb2_queue_is_busy(vdev, file))
2716 return -EBUSY;
2717 return vb2_prepare_buf(vdev->queue, p);
2718}
2719EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
2720
2721int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
2722{
2723 struct video_device *vdev = video_devdata(file);
2724
2725 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2726 return vb2_querybuf(vdev->queue, p);
2727}
2728EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
2729
2730int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2731{
2732 struct video_device *vdev = video_devdata(file);
2733
2734 if (vb2_queue_is_busy(vdev, file))
2735 return -EBUSY;
2736 return vb2_qbuf(vdev->queue, p);
2737}
2738EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
2739
2740int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2741{
2742 struct video_device *vdev = video_devdata(file);
2743
2744 if (vb2_queue_is_busy(vdev, file))
2745 return -EBUSY;
2746 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
2747}
2748EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
2749
2750int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
2751{
2752 struct video_device *vdev = video_devdata(file);
2753
2754 if (vb2_queue_is_busy(vdev, file))
2755 return -EBUSY;
2756 return vb2_streamon(vdev->queue, i);
2757}
2758EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
2759
2760int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
2761{
2762 struct video_device *vdev = video_devdata(file);
2763
2764 if (vb2_queue_is_busy(vdev, file))
2765 return -EBUSY;
2766 return vb2_streamoff(vdev->queue, i);
2767}
2768EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
2769
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002770int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
2771{
2772 struct video_device *vdev = video_devdata(file);
2773
2774 if (vb2_queue_is_busy(vdev, file))
2775 return -EBUSY;
2776 return vb2_expbuf(vdev->queue, p);
2777}
2778EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
2779
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002780/* v4l2_file_operations helpers */
2781
2782int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
2783{
2784 struct video_device *vdev = video_devdata(file);
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002785 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2786 int err;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002787
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002788 if (lock && mutex_lock_interruptible(lock))
2789 return -ERESTARTSYS;
2790 err = vb2_mmap(vdev->queue, vma);
2791 if (lock)
2792 mutex_unlock(lock);
2793 return err;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002794}
2795EXPORT_SYMBOL_GPL(vb2_fop_mmap);
2796
Ricardo Ribalda1380f572013-11-25 05:49:02 -03002797int _vb2_fop_release(struct file *file, struct mutex *lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002798{
2799 struct video_device *vdev = video_devdata(file);
2800
2801 if (file->private_data == vdev->queue->owner) {
Ricardo Ribalda1380f572013-11-25 05:49:02 -03002802 if (lock)
2803 mutex_lock(lock);
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002804 vb2_queue_release(vdev->queue);
2805 vdev->queue->owner = NULL;
Ricardo Ribalda1380f572013-11-25 05:49:02 -03002806 if (lock)
2807 mutex_unlock(lock);
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002808 }
2809 return v4l2_fh_release(file);
2810}
Ricardo Ribalda1380f572013-11-25 05:49:02 -03002811EXPORT_SYMBOL_GPL(_vb2_fop_release);
2812
2813int vb2_fop_release(struct file *file)
2814{
2815 struct video_device *vdev = video_devdata(file);
2816 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2817
2818 return _vb2_fop_release(file, lock);
2819}
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002820EXPORT_SYMBOL_GPL(vb2_fop_release);
2821
Ricardo Ribalda819585b2013-08-28 04:39:29 -03002822ssize_t vb2_fop_write(struct file *file, const char __user *buf,
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002823 size_t count, loff_t *ppos)
2824{
2825 struct video_device *vdev = video_devdata(file);
2826 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002827 int err = -EBUSY;
2828
Hans Verkuilcf533732012-07-31 04:02:25 -03002829 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002830 return -ERESTARTSYS;
2831 if (vb2_queue_is_busy(vdev, file))
2832 goto exit;
2833 err = vb2_write(vdev->queue, buf, count, ppos,
2834 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03002835 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002836 vdev->queue->owner = file->private_data;
2837exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03002838 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002839 mutex_unlock(lock);
2840 return err;
2841}
2842EXPORT_SYMBOL_GPL(vb2_fop_write);
2843
2844ssize_t vb2_fop_read(struct file *file, char __user *buf,
2845 size_t count, loff_t *ppos)
2846{
2847 struct video_device *vdev = video_devdata(file);
2848 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002849 int err = -EBUSY;
2850
Hans Verkuilcf533732012-07-31 04:02:25 -03002851 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002852 return -ERESTARTSYS;
2853 if (vb2_queue_is_busy(vdev, file))
2854 goto exit;
2855 err = vb2_read(vdev->queue, buf, count, ppos,
2856 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03002857 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002858 vdev->queue->owner = file->private_data;
2859exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03002860 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002861 mutex_unlock(lock);
2862 return err;
2863}
2864EXPORT_SYMBOL_GPL(vb2_fop_read);
2865
2866unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
2867{
2868 struct video_device *vdev = video_devdata(file);
2869 struct vb2_queue *q = vdev->queue;
2870 struct mutex *lock = q->lock ? q->lock : vdev->lock;
2871 unsigned long req_events = poll_requested_events(wait);
2872 unsigned res;
2873 void *fileio;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002874 bool must_lock = false;
2875
2876 /* Try to be smart: only lock if polling might start fileio,
2877 otherwise locking will only introduce unwanted delays. */
2878 if (q->num_buffers == 0 && q->fileio == NULL) {
2879 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2880 (req_events & (POLLIN | POLLRDNORM)))
2881 must_lock = true;
2882 else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2883 (req_events & (POLLOUT | POLLWRNORM)))
2884 must_lock = true;
2885 }
2886
2887 /* If locking is needed, but this helper doesn't know how, then you
2888 shouldn't be using this helper but you should write your own. */
Hans Verkuilcf533732012-07-31 04:02:25 -03002889 WARN_ON(must_lock && !lock);
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002890
Hans Verkuilcf533732012-07-31 04:02:25 -03002891 if (must_lock && lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002892 return POLLERR;
2893
2894 fileio = q->fileio;
2895
2896 res = vb2_poll(vdev->queue, file, wait);
2897
2898 /* If fileio was started, then we have a new queue owner. */
2899 if (must_lock && !fileio && q->fileio)
2900 q->owner = file->private_data;
Hans Verkuilcf533732012-07-31 04:02:25 -03002901 if (must_lock && lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002902 mutex_unlock(lock);
2903 return res;
2904}
2905EXPORT_SYMBOL_GPL(vb2_fop_poll);
2906
2907#ifndef CONFIG_MMU
2908unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
2909 unsigned long len, unsigned long pgoff, unsigned long flags)
2910{
2911 struct video_device *vdev = video_devdata(file);
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002912 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2913 int ret;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002914
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002915 if (lock && mutex_lock_interruptible(lock))
2916 return -ERESTARTSYS;
2917 ret = vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
2918 if (lock)
2919 mutex_unlock(lock);
2920 return ret;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002921}
2922EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
2923#endif
2924
2925/* vb2_ops helpers. Only use if vq->lock is non-NULL. */
2926
2927void vb2_ops_wait_prepare(struct vb2_queue *vq)
2928{
2929 mutex_unlock(vq->lock);
2930}
2931EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
2932
2933void vb2_ops_wait_finish(struct vb2_queue *vq)
2934{
2935 mutex_lock(vq->lock);
2936}
2937EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
2938
Pawel Osciake23ccc02010-10-11 10:56:41 -03002939MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
Pawel Osciak95072082011-03-13 15:23:32 -03002940MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
Pawel Osciake23ccc02010-10-11 10:56:41 -03002941MODULE_LICENSE("GPL");