blob: 521350a74c4668b5cec1f44dbf6629e6dc4ee9fc [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 Ailusade48682014-02-25 19:12:19 -0300491 b->flags |= q->timestamp_flags;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300492
493 switch (vb->state) {
494 case VB2_BUF_STATE_QUEUED:
495 case VB2_BUF_STATE_ACTIVE:
496 b->flags |= V4L2_BUF_FLAG_QUEUED;
497 break;
498 case VB2_BUF_STATE_ERROR:
499 b->flags |= V4L2_BUF_FLAG_ERROR;
500 /* fall through */
501 case VB2_BUF_STATE_DONE:
502 b->flags |= V4L2_BUF_FLAG_DONE;
503 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300504 case VB2_BUF_STATE_PREPARED:
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300505 b->flags |= V4L2_BUF_FLAG_PREPARED;
506 break;
Hans Verkuilb18a8ff2013-12-13 13:13:38 -0300507 case VB2_BUF_STATE_PREPARING:
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300508 case VB2_BUF_STATE_DEQUEUED:
Pawel Osciake23ccc02010-10-11 10:56:41 -0300509 /* nothing */
510 break;
511 }
512
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300513 if (__buffer_in_use(q, vb))
Pawel Osciake23ccc02010-10-11 10:56:41 -0300514 b->flags |= V4L2_BUF_FLAG_MAPPED;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300515}
516
517/**
518 * vb2_querybuf() - query video buffer information
519 * @q: videobuf queue
520 * @b: buffer struct passed from userspace to vidioc_querybuf handler
521 * in driver
522 *
523 * Should be called from vidioc_querybuf ioctl handler in driver.
524 * This function will verify the passed v4l2_buffer structure and fill the
525 * relevant information for the userspace.
526 *
527 * The return values from this function are intended to be directly returned
528 * from vidioc_querybuf handler in driver.
529 */
530int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
531{
532 struct vb2_buffer *vb;
Hans Verkuil32a77262012-09-28 06:12:53 -0300533 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300534
535 if (b->type != q->type) {
536 dprintk(1, "querybuf: wrong buffer type\n");
537 return -EINVAL;
538 }
539
540 if (b->index >= q->num_buffers) {
541 dprintk(1, "querybuf: buffer index out of range\n");
542 return -EINVAL;
543 }
544 vb = q->bufs[b->index];
Hans Verkuil32a77262012-09-28 06:12:53 -0300545 ret = __verify_planes_array(vb, b);
546 if (!ret)
547 __fill_v4l2_buffer(vb, b);
548 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300549}
550EXPORT_SYMBOL(vb2_querybuf);
551
552/**
553 * __verify_userptr_ops() - verify that all memory operations required for
554 * USERPTR queue type have been provided
555 */
556static int __verify_userptr_ops(struct vb2_queue *q)
557{
558 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
559 !q->mem_ops->put_userptr)
560 return -EINVAL;
561
562 return 0;
563}
564
565/**
566 * __verify_mmap_ops() - verify that all memory operations required for
567 * MMAP queue type have been provided
568 */
569static int __verify_mmap_ops(struct vb2_queue *q)
570{
571 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
572 !q->mem_ops->put || !q->mem_ops->mmap)
573 return -EINVAL;
574
575 return 0;
576}
577
578/**
Sumit Semwalc5384042012-06-14 10:37:37 -0300579 * __verify_dmabuf_ops() - verify that all memory operations required for
580 * DMABUF queue type have been provided
581 */
582static int __verify_dmabuf_ops(struct vb2_queue *q)
583{
584 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
585 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
586 !q->mem_ops->unmap_dmabuf)
587 return -EINVAL;
588
589 return 0;
590}
591
592/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300593 * __verify_memory_type() - Check whether the memory type and buffer type
594 * passed to a buffer operation are compatible with the queue.
595 */
596static int __verify_memory_type(struct vb2_queue *q,
597 enum v4l2_memory memory, enum v4l2_buf_type type)
598{
Sumit Semwalc5384042012-06-14 10:37:37 -0300599 if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR &&
600 memory != V4L2_MEMORY_DMABUF) {
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300601 dprintk(1, "reqbufs: unsupported memory type\n");
602 return -EINVAL;
603 }
604
605 if (type != q->type) {
606 dprintk(1, "reqbufs: requested type is incorrect\n");
607 return -EINVAL;
608 }
609
610 /*
611 * Make sure all the required memory ops for given memory type
612 * are available.
613 */
614 if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
615 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
616 return -EINVAL;
617 }
618
619 if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
620 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
621 return -EINVAL;
622 }
623
Sumit Semwalc5384042012-06-14 10:37:37 -0300624 if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
625 dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
626 return -EINVAL;
627 }
628
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300629 /*
630 * Place the busy tests at the end: -EBUSY can be ignored when
631 * create_bufs is called with count == 0, but count == 0 should still
632 * do the memory and type validation.
633 */
634 if (q->fileio) {
635 dprintk(1, "reqbufs: file io in progress\n");
636 return -EBUSY;
637 }
638 return 0;
639}
640
641/**
642 * __reqbufs() - Initiate streaming
Pawel Osciake23ccc02010-10-11 10:56:41 -0300643 * @q: videobuf2 queue
644 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
645 *
646 * Should be called from vidioc_reqbufs ioctl handler of a driver.
647 * This function:
648 * 1) verifies streaming parameters passed from the userspace,
649 * 2) sets up the queue,
650 * 3) negotiates number of buffers and planes per buffer with the driver
651 * to be used during streaming,
652 * 4) allocates internal buffer structures (struct vb2_buffer), according to
653 * the agreed parameters,
654 * 5) for MMAP memory type, allocates actual video memory, using the
655 * memory handling/allocation routines provided during queue initialization
656 *
657 * If req->count is 0, all the memory will be freed instead.
658 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
659 * and the queue is not busy, memory will be reallocated.
660 *
661 * The return values from this function are intended to be directly returned
662 * from vidioc_reqbufs handler in driver.
663 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300664static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300665{
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300666 unsigned int num_buffers, allocated_buffers, num_planes = 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300667 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300668
669 if (q->streaming) {
670 dprintk(1, "reqbufs: streaming active\n");
671 return -EBUSY;
672 }
673
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300674 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300675 /*
676 * We already have buffers allocated, so first check if they
677 * are not in use and can be freed.
678 */
679 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
680 dprintk(1, "reqbufs: memory in use, cannot free\n");
681 return -EBUSY;
682 }
683
Hans Verkuil63faabf2013-12-13 13:13:40 -0300684 ret = __vb2_queue_free(q, q->num_buffers);
685 if (ret)
686 return ret;
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300687
688 /*
689 * In case of REQBUFS(0) return immediately without calling
690 * driver's queue_setup() callback and allocating resources.
691 */
692 if (req->count == 0)
693 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300694 }
695
696 /*
697 * Make sure the requested values and current defaults are sane.
698 */
699 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300700 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
Pawel Osciake23ccc02010-10-11 10:56:41 -0300701 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
Marek Szyprowski13b14092011-04-14 07:17:44 -0300702 q->memory = req->memory;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300703
704 /*
705 * Ask the driver how many buffers and planes per buffer it requires.
706 * Driver also sets the size and allocator context for each plane.
707 */
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300708 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300709 q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300710 if (ret)
711 return ret;
712
713 /* Finally, allocate buffers and video memory */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300714 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
Marek Szyprowski66072d42011-06-28 08:29:02 -0300715 if (ret == 0) {
716 dprintk(1, "Memory allocation failed\n");
717 return -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300718 }
719
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300720 allocated_buffers = ret;
721
Pawel Osciake23ccc02010-10-11 10:56:41 -0300722 /*
723 * Check if driver can handle the allocated number of buffers.
724 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300725 if (allocated_buffers < num_buffers) {
726 num_buffers = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300727
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300728 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
729 &num_planes, q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300730
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300731 if (!ret && allocated_buffers < num_buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300732 ret = -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300733
734 /*
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300735 * Either the driver has accepted a smaller number of buffers,
736 * or .queue_setup() returned an error
Pawel Osciake23ccc02010-10-11 10:56:41 -0300737 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300738 }
739
740 q->num_buffers = allocated_buffers;
741
742 if (ret < 0) {
743 __vb2_queue_free(q, allocated_buffers);
744 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300745 }
746
Pawel Osciake23ccc02010-10-11 10:56:41 -0300747 /*
748 * Return the number of successfully allocated buffers
749 * to the userspace.
750 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300751 req->count = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300752
753 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300754}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300755
756/**
757 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
758 * type values.
759 * @q: videobuf2 queue
760 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
761 */
762int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
763{
764 int ret = __verify_memory_type(q, req->memory, req->type);
765
766 return ret ? ret : __reqbufs(q, req);
767}
Pawel Osciake23ccc02010-10-11 10:56:41 -0300768EXPORT_SYMBOL_GPL(vb2_reqbufs);
769
770/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300771 * __create_bufs() - Allocate buffers and any required auxiliary structs
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300772 * @q: videobuf2 queue
773 * @create: creation parameters, passed from userspace to vidioc_create_bufs
774 * handler in driver
775 *
776 * Should be called from vidioc_create_bufs ioctl handler of a driver.
777 * This function:
778 * 1) verifies parameter sanity
779 * 2) calls the .queue_setup() queue operation
780 * 3) performs any necessary memory allocations
781 *
782 * The return values from this function are intended to be directly returned
783 * from vidioc_create_bufs handler in driver.
784 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300785static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300786{
787 unsigned int num_planes = 0, num_buffers, allocated_buffers;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300788 int ret;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300789
790 if (q->num_buffers == VIDEO_MAX_FRAME) {
791 dprintk(1, "%s(): maximum number of buffers already allocated\n",
792 __func__);
793 return -ENOBUFS;
794 }
795
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300796 if (!q->num_buffers) {
797 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
798 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
799 q->memory = create->memory;
800 }
801
802 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
803
804 /*
805 * Ask the driver, whether the requested number of buffers, planes per
806 * buffer and their sizes are acceptable
807 */
808 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
809 &num_planes, q->plane_sizes, q->alloc_ctx);
810 if (ret)
811 return ret;
812
813 /* Finally, allocate buffers and video memory */
814 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
815 num_planes);
Hans Verkuilf05393d22012-06-22 05:44:14 -0300816 if (ret == 0) {
817 dprintk(1, "Memory allocation failed\n");
818 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300819 }
820
821 allocated_buffers = ret;
822
823 /*
824 * Check if driver can handle the so far allocated number of buffers.
825 */
826 if (ret < num_buffers) {
827 num_buffers = ret;
828
829 /*
830 * q->num_buffers contains the total number of buffers, that the
831 * queue driver has set up
832 */
833 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
834 &num_planes, q->plane_sizes, q->alloc_ctx);
835
836 if (!ret && allocated_buffers < num_buffers)
837 ret = -ENOMEM;
838
839 /*
840 * Either the driver has accepted a smaller number of buffers,
841 * or .queue_setup() returned an error
842 */
843 }
844
845 q->num_buffers += allocated_buffers;
846
847 if (ret < 0) {
848 __vb2_queue_free(q, allocated_buffers);
Hans Verkuilf05393d22012-06-22 05:44:14 -0300849 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300850 }
851
852 /*
853 * Return the number of successfully allocated buffers
854 * to the userspace.
855 */
856 create->count = allocated_buffers;
857
858 return 0;
859}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300860
861/**
Nicolas THERY53aa3b12012-07-20 09:25:37 -0300862 * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
863 * memory and type values.
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300864 * @q: videobuf2 queue
865 * @create: creation parameters, passed from userspace to vidioc_create_bufs
866 * handler in driver
867 */
868int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
869{
870 int ret = __verify_memory_type(q, create->memory, create->format.type);
871
872 create->index = q->num_buffers;
Hans Verkuilf05393d22012-06-22 05:44:14 -0300873 if (create->count == 0)
874 return ret != -EBUSY ? ret : 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300875 return ret ? ret : __create_bufs(q, create);
876}
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300877EXPORT_SYMBOL_GPL(vb2_create_bufs);
878
879/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300880 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
881 * @vb: vb2_buffer to which the plane in question belongs to
882 * @plane_no: plane number for which the address is to be returned
883 *
884 * This function returns a kernel virtual address of a given plane if
885 * such a mapping exist, NULL otherwise.
886 */
887void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
888{
889 struct vb2_queue *q = vb->vb2_queue;
890
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300891 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300892 return NULL;
893
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300894 return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300895
896}
897EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
898
899/**
900 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
901 * @vb: vb2_buffer to which the plane in question belongs to
902 * @plane_no: plane number for which the cookie is to be returned
903 *
904 * This function returns an allocator specific cookie for a given plane if
905 * available, NULL otherwise. The allocator should provide some simple static
906 * inline function, which would convert this cookie to the allocator specific
907 * type that can be used directly by the driver to access the buffer. This can
908 * be for example physical address, pointer to scatter list or IOMMU mapping.
909 */
910void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
911{
912 struct vb2_queue *q = vb->vb2_queue;
913
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300914 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300915 return NULL;
916
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300917 return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300918}
919EXPORT_SYMBOL_GPL(vb2_plane_cookie);
920
921/**
922 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
923 * @vb: vb2_buffer returned from the driver
924 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
925 * or VB2_BUF_STATE_ERROR if the operation finished with an error
926 *
927 * This function should be called by the driver after a hardware operation on
928 * a buffer is finished and the buffer may be returned to userspace. The driver
929 * cannot use this buffer anymore until it is queued back to it by videobuf
930 * by the means of buf_queue callback. Only buffers previously queued to the
931 * driver by buf_queue can be passed to this function.
932 */
933void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
934{
935 struct vb2_queue *q = vb->vb2_queue;
936 unsigned long flags;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -0300937 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300938
939 if (vb->state != VB2_BUF_STATE_ACTIVE)
940 return;
941
942 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
943 return;
944
945 dprintk(4, "Done processing on buffer %d, state: %d\n",
Tushar Behera9b6f5dc2012-11-12 04:01:29 -0300946 vb->v4l2_buf.index, state);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300947
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -0300948 /* sync buffers */
949 for (plane = 0; plane < vb->num_planes; ++plane)
950 call_memop(q, finish, vb->planes[plane].mem_priv);
951
Pawel Osciake23ccc02010-10-11 10:56:41 -0300952 /* Add the buffer to the done buffers list */
953 spin_lock_irqsave(&q->done_lock, flags);
954 vb->state = state;
955 list_add_tail(&vb->done_entry, &q->done_list);
956 atomic_dec(&q->queued_count);
957 spin_unlock_irqrestore(&q->done_lock, flags);
958
959 /* Inform any processes that may be waiting for buffers */
960 wake_up(&q->done_wq);
961}
962EXPORT_SYMBOL_GPL(vb2_buffer_done);
963
964/**
Hans Verkuil32a77262012-09-28 06:12:53 -0300965 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
966 * v4l2_buffer by the userspace. The caller has already verified that struct
967 * v4l2_buffer has a valid number of planes.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300968 */
Hans Verkuil32a77262012-09-28 06:12:53 -0300969static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
Pawel Osciake23ccc02010-10-11 10:56:41 -0300970 struct v4l2_plane *v4l2_planes)
971{
972 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300973
974 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300975 /* Fill in driver-provided information for OUTPUT types */
976 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
977 /*
978 * Will have to go up to b->length when API starts
979 * accepting variable number of planes.
980 */
981 for (plane = 0; plane < vb->num_planes; ++plane) {
982 v4l2_planes[plane].bytesused =
983 b->m.planes[plane].bytesused;
984 v4l2_planes[plane].data_offset =
985 b->m.planes[plane].data_offset;
986 }
987 }
988
989 if (b->memory == V4L2_MEMORY_USERPTR) {
990 for (plane = 0; plane < vb->num_planes; ++plane) {
991 v4l2_planes[plane].m.userptr =
992 b->m.planes[plane].m.userptr;
993 v4l2_planes[plane].length =
994 b->m.planes[plane].length;
995 }
996 }
Sumit Semwalc5384042012-06-14 10:37:37 -0300997 if (b->memory == V4L2_MEMORY_DMABUF) {
998 for (plane = 0; plane < vb->num_planes; ++plane) {
999 v4l2_planes[plane].m.fd =
1000 b->m.planes[plane].m.fd;
1001 v4l2_planes[plane].length =
1002 b->m.planes[plane].length;
1003 v4l2_planes[plane].data_offset =
1004 b->m.planes[plane].data_offset;
1005 }
1006 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001007 } else {
1008 /*
1009 * Single-planar buffers do not use planes array,
1010 * so fill in relevant v4l2_buffer struct fields instead.
1011 * In videobuf we use our internal V4l2_planes struct for
1012 * single-planar buffers as well, for simplicity.
1013 */
Laurent Pinchartac706bf2012-12-17 07:38:16 -03001014 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -03001015 v4l2_planes[0].bytesused = b->bytesused;
Laurent Pinchartac706bf2012-12-17 07:38:16 -03001016 v4l2_planes[0].data_offset = 0;
1017 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001018
1019 if (b->memory == V4L2_MEMORY_USERPTR) {
1020 v4l2_planes[0].m.userptr = b->m.userptr;
1021 v4l2_planes[0].length = b->length;
1022 }
Sumit Semwalc5384042012-06-14 10:37:37 -03001023
1024 if (b->memory == V4L2_MEMORY_DMABUF) {
1025 v4l2_planes[0].m.fd = b->m.fd;
1026 v4l2_planes[0].length = b->length;
1027 v4l2_planes[0].data_offset = 0;
1028 }
1029
Pawel Osciake23ccc02010-10-11 10:56:41 -03001030 }
1031
Hans Verkuilf1343282014-02-24 14:44:50 -03001032 /* Zero flags that the vb2 core handles */
Sakari Ailus1b18e7a2012-10-22 17:10:16 -03001033 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
Hans Verkuilf1343282014-02-24 14:44:50 -03001034 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
1035 /*
1036 * For output buffers mask out the timecode flag:
1037 * this will be handled later in vb2_internal_qbuf().
1038 * The 'field' is valid metadata for this output buffer
1039 * and so that needs to be copied here.
1040 */
1041 vb->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TIMECODE;
1042 vb->v4l2_buf.field = b->field;
1043 } else {
1044 /* Zero any output buffer flags as this is a capture buffer */
1045 vb->v4l2_buf.flags &= ~V4L2_BUFFER_OUT_FLAGS;
1046 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001047}
1048
1049/**
1050 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
1051 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001052static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001053{
1054 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1055 struct vb2_queue *q = vb->vb2_queue;
1056 void *mem_priv;
1057 unsigned int plane;
1058 int ret;
1059 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1060
Hans Verkuil32a77262012-09-28 06:12:53 -03001061 /* Copy relevant information provided by the userspace */
1062 __fill_vb2_buffer(vb, b, planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001063
1064 for (plane = 0; plane < vb->num_planes; ++plane) {
1065 /* Skip the plane if already verified */
Marek Szyprowskif0b7c7f2011-11-16 15:09:40 -03001066 if (vb->v4l2_planes[plane].m.userptr &&
1067 vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
Pawel Osciake23ccc02010-10-11 10:56:41 -03001068 && vb->v4l2_planes[plane].length == planes[plane].length)
1069 continue;
1070
1071 dprintk(3, "qbuf: userspace address for plane %d changed, "
1072 "reacquiring memory\n", plane);
1073
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001074 /* Check if the provided plane buffer is large enough */
1075 if (planes[plane].length < q->plane_sizes[plane]) {
Seung-Woo Kim2484a7e2013-08-20 04:48:06 -03001076 dprintk(1, "qbuf: provided buffer size %u is less than "
1077 "setup size %u for plane %d\n",
1078 planes[plane].length,
1079 q->plane_sizes[plane], plane);
Marek Szyprowski4c2625d2011-10-03 03:21:45 -03001080 ret = -EINVAL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001081 goto err;
1082 }
1083
Pawel Osciake23ccc02010-10-11 10:56:41 -03001084 /* Release previously acquired memory if present */
1085 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -03001086 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001087
1088 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001089 vb->v4l2_planes[plane].m.userptr = 0;
1090 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001091
1092 /* Acquire each plane's memory */
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001093 mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
1094 planes[plane].m.userptr,
1095 planes[plane].length, write);
1096 if (IS_ERR_OR_NULL(mem_priv)) {
1097 dprintk(1, "qbuf: failed acquiring userspace "
Pawel Osciake23ccc02010-10-11 10:56:41 -03001098 "memory for plane %d\n", plane);
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001099 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
1100 goto err;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001101 }
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001102 vb->planes[plane].mem_priv = mem_priv;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001103 }
1104
1105 /*
1106 * Call driver-specific initialization on the newly acquired buffer,
1107 * if provided.
1108 */
1109 ret = call_qop(q, buf_init, vb);
1110 if (ret) {
1111 dprintk(1, "qbuf: buffer initialization failed\n");
1112 goto err;
1113 }
1114
1115 /*
1116 * Now that everything is in order, copy relevant information
1117 * provided by userspace.
1118 */
1119 for (plane = 0; plane < vb->num_planes; ++plane)
1120 vb->v4l2_planes[plane] = planes[plane];
1121
1122 return 0;
1123err:
1124 /* In case of errors, release planes that were already acquired */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001125 for (plane = 0; plane < vb->num_planes; ++plane) {
1126 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -03001127 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001128 vb->planes[plane].mem_priv = NULL;
1129 vb->v4l2_planes[plane].m.userptr = 0;
1130 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001131 }
1132
1133 return ret;
1134}
1135
1136/**
1137 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1138 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001139static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001140{
Hans Verkuil32a77262012-09-28 06:12:53 -03001141 __fill_vb2_buffer(vb, b, vb->v4l2_planes);
1142 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001143}
1144
1145/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001146 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1147 */
1148static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1149{
1150 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1151 struct vb2_queue *q = vb->vb2_queue;
1152 void *mem_priv;
1153 unsigned int plane;
1154 int ret;
1155 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1156
Laurent Pinchart6f546c52014-01-01 09:10:48 -03001157 /* Copy relevant information provided by the userspace */
Sumit Semwalc5384042012-06-14 10:37:37 -03001158 __fill_vb2_buffer(vb, b, planes);
1159
1160 for (plane = 0; plane < vb->num_planes; ++plane) {
1161 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1162
1163 if (IS_ERR_OR_NULL(dbuf)) {
1164 dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
1165 plane);
1166 ret = -EINVAL;
1167 goto err;
1168 }
1169
1170 /* use DMABUF size if length is not provided */
1171 if (planes[plane].length == 0)
1172 planes[plane].length = dbuf->size;
1173
1174 if (planes[plane].length < planes[plane].data_offset +
1175 q->plane_sizes[plane]) {
Seung-Woo Kim77c07822013-11-29 04:50:29 -03001176 dprintk(1, "qbuf: invalid dmabuf length for plane %d\n",
1177 plane);
Sumit Semwalc5384042012-06-14 10:37:37 -03001178 ret = -EINVAL;
1179 goto err;
1180 }
1181
1182 /* Skip the plane if already verified */
1183 if (dbuf == vb->planes[plane].dbuf &&
1184 vb->v4l2_planes[plane].length == planes[plane].length) {
1185 dma_buf_put(dbuf);
1186 continue;
1187 }
1188
1189 dprintk(1, "qbuf: buffer for plane %d changed\n", plane);
1190
1191 /* Release previously acquired memory if present */
1192 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
1193 memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
1194
1195 /* Acquire each plane's memory */
1196 mem_priv = call_memop(q, attach_dmabuf, q->alloc_ctx[plane],
1197 dbuf, planes[plane].length, write);
1198 if (IS_ERR(mem_priv)) {
1199 dprintk(1, "qbuf: failed to attach dmabuf\n");
1200 ret = PTR_ERR(mem_priv);
1201 dma_buf_put(dbuf);
1202 goto err;
1203 }
1204
1205 vb->planes[plane].dbuf = dbuf;
1206 vb->planes[plane].mem_priv = mem_priv;
1207 }
1208
1209 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1210 * really we want to do this just before the DMA, not while queueing
1211 * the buffer(s)..
1212 */
1213 for (plane = 0; plane < vb->num_planes; ++plane) {
1214 ret = call_memop(q, map_dmabuf, vb->planes[plane].mem_priv);
1215 if (ret) {
1216 dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
1217 plane);
1218 goto err;
1219 }
1220 vb->planes[plane].dbuf_mapped = 1;
1221 }
1222
1223 /*
1224 * Call driver-specific initialization on the newly acquired buffer,
1225 * if provided.
1226 */
1227 ret = call_qop(q, buf_init, vb);
1228 if (ret) {
1229 dprintk(1, "qbuf: buffer initialization failed\n");
1230 goto err;
1231 }
1232
1233 /*
1234 * Now that everything is in order, copy relevant information
1235 * provided by userspace.
1236 */
1237 for (plane = 0; plane < vb->num_planes; ++plane)
1238 vb->v4l2_planes[plane] = planes[plane];
1239
1240 return 0;
1241err:
1242 /* In case of errors, release planes that were already acquired */
1243 __vb2_buf_dmabuf_put(vb);
1244
1245 return ret;
1246}
1247
1248/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001249 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1250 */
1251static void __enqueue_in_driver(struct vb2_buffer *vb)
1252{
1253 struct vb2_queue *q = vb->vb2_queue;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001254 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001255
1256 vb->state = VB2_BUF_STATE_ACTIVE;
1257 atomic_inc(&q->queued_count);
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001258
1259 /* sync buffers */
1260 for (plane = 0; plane < vb->num_planes; ++plane)
1261 call_memop(q, prepare, vb->planes[plane].mem_priv);
1262
Pawel Osciake23ccc02010-10-11 10:56:41 -03001263 q->ops->buf_queue(vb);
1264}
1265
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001266static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001267{
1268 struct vb2_queue *q = vb->vb2_queue;
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001269 struct rw_semaphore *mmap_sem;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001270 int ret;
1271
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001272 ret = __verify_length(vb, b);
Sylwester Nawrocki3a9621b2013-08-26 11:47:53 -03001273 if (ret < 0) {
1274 dprintk(1, "%s(): plane parameters verification failed: %d\n",
1275 __func__, ret);
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001276 return ret;
Sylwester Nawrocki3a9621b2013-08-26 11:47:53 -03001277 }
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001278
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001279 vb->state = VB2_BUF_STATE_PREPARING;
Hans Verkuilf1343282014-02-24 14:44:50 -03001280 vb->v4l2_buf.timestamp.tv_sec = 0;
1281 vb->v4l2_buf.timestamp.tv_usec = 0;
1282 vb->v4l2_buf.sequence = 0;
1283
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001284 switch (q->memory) {
1285 case V4L2_MEMORY_MMAP:
1286 ret = __qbuf_mmap(vb, b);
1287 break;
1288 case V4L2_MEMORY_USERPTR:
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001289 /*
Mauro Carvalho Chehabf103b5d2014-01-07 07:03:09 -02001290 * In case of user pointer buffers vb2 allocators need to get
1291 * direct access to userspace pages. This requires getting
1292 * the mmap semaphore for read access in the current process
1293 * structure. The same semaphore is taken before calling mmap
1294 * operation, while both qbuf/prepare_buf and mmap are called
1295 * by the driver or v4l2 core with the driver's lock held.
1296 * To avoid an AB-BA deadlock (mmap_sem then driver's lock in
1297 * mmap and driver's lock then mmap_sem in qbuf/prepare_buf),
1298 * the videobuf2 core releases the driver's lock, takes
1299 * mmap_sem and then takes the driver's lock again.
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001300 */
1301 mmap_sem = &current->mm->mmap_sem;
1302 call_qop(q, wait_prepare, q);
1303 down_read(mmap_sem);
1304 call_qop(q, wait_finish, q);
1305
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001306 ret = __qbuf_userptr(vb, b);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001307
1308 up_read(mmap_sem);
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001309 break;
Sumit Semwalc5384042012-06-14 10:37:37 -03001310 case V4L2_MEMORY_DMABUF:
1311 ret = __qbuf_dmabuf(vb, b);
1312 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001313 default:
1314 WARN(1, "Invalid queue type\n");
1315 ret = -EINVAL;
1316 }
1317
1318 if (!ret)
1319 ret = call_qop(q, buf_prepare, vb);
1320 if (ret)
1321 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001322 vb->state = ret ? VB2_BUF_STATE_DEQUEUED : VB2_BUF_STATE_PREPARED;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001323
1324 return ret;
1325}
1326
Laurent Pinchart012043b2013-08-09 08:11:26 -03001327static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
Hans Verkuil41381112013-12-13 13:13:39 -03001328 const char *opname)
Laurent Pinchart012043b2013-08-09 08:11:26 -03001329{
Laurent Pinchart012043b2013-08-09 08:11:26 -03001330 if (b->type != q->type) {
1331 dprintk(1, "%s(): invalid buffer type\n", opname);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001332 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001333 }
1334
1335 if (b->index >= q->num_buffers) {
1336 dprintk(1, "%s(): buffer index out of range\n", opname);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001337 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001338 }
1339
Hans Verkuil41381112013-12-13 13:13:39 -03001340 if (q->bufs[b->index] == NULL) {
Laurent Pinchart012043b2013-08-09 08:11:26 -03001341 /* Should never happen */
1342 dprintk(1, "%s(): buffer is NULL\n", opname);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001343 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001344 }
1345
1346 if (b->memory != q->memory) {
1347 dprintk(1, "%s(): invalid memory type\n", opname);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001348 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001349 }
1350
Hans Verkuil41381112013-12-13 13:13:39 -03001351 return __verify_planes_array(q->bufs[b->index], b);
Laurent Pinchart012043b2013-08-09 08:11:26 -03001352}
1353
Pawel Osciake23ccc02010-10-11 10:56:41 -03001354/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001355 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1356 * @q: videobuf2 queue
1357 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1358 * handler in driver
1359 *
1360 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1361 * This function:
1362 * 1) verifies the passed buffer,
1363 * 2) calls buf_prepare callback in the driver (if provided), in which
1364 * driver-specific buffer initialization can be performed,
1365 *
1366 * The return values from this function are intended to be directly returned
1367 * from vidioc_prepare_buf handler in driver.
1368 */
1369int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1370{
Hans Verkuil41381112013-12-13 13:13:39 -03001371 struct vb2_buffer *vb;
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001372 int ret;
Hans Verkuil41381112013-12-13 13:13:39 -03001373
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001374 if (q->fileio) {
1375 dprintk(1, "%s(): file io in progress\n", __func__);
1376 return -EBUSY;
1377 }
1378
1379 ret = vb2_queue_or_prepare_buf(q, b, "prepare_buf");
Hans Verkuil41381112013-12-13 13:13:39 -03001380 if (ret)
1381 return ret;
1382
1383 vb = q->bufs[b->index];
1384 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1385 dprintk(1, "%s(): invalid buffer state %d\n", __func__,
1386 vb->state);
1387 return -EINVAL;
1388 }
1389
1390 ret = __buf_prepare(vb, b);
1391 if (!ret) {
1392 /* Fill buffer information for the userspace */
1393 __fill_v4l2_buffer(vb, b);
1394
1395 dprintk(1, "%s() of buffer %d succeeded\n", __func__, vb->v4l2_buf.index);
1396 }
1397 return ret;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001398}
1399EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1400
Hans Verkuil02f142e2013-12-13 13:13:42 -03001401/**
1402 * vb2_start_streaming() - Attempt to start streaming.
1403 * @q: videobuf2 queue
1404 *
1405 * If there are not enough buffers, then retry_start_streaming is set to
1406 * 1 and 0 is returned. The next time a buffer is queued and
1407 * retry_start_streaming is 1, this function will be called again to
1408 * retry starting the DMA engine.
1409 */
1410static int vb2_start_streaming(struct vb2_queue *q)
1411{
1412 int ret;
1413
1414 /* Tell the driver to start streaming */
1415 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1416
1417 /*
1418 * If there are not enough buffers queued to start streaming, then
1419 * the start_streaming operation will return -ENOBUFS and you have to
1420 * retry when the next buffer is queued.
1421 */
1422 if (ret == -ENOBUFS) {
1423 dprintk(1, "qbuf: not enough buffers, retry when more buffers are queued.\n");
1424 q->retry_start_streaming = 1;
1425 return 0;
1426 }
1427 if (ret)
1428 dprintk(1, "qbuf: driver refused to start streaming\n");
1429 else
1430 q->retry_start_streaming = 0;
1431 return ret;
1432}
1433
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001434static int vb2_internal_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
Laurent Pinchart012043b2013-08-09 08:11:26 -03001435{
Hans Verkuil41381112013-12-13 13:13:39 -03001436 int ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
1437 struct vb2_buffer *vb;
1438
1439 if (ret)
1440 return ret;
1441
1442 vb = q->bufs[b->index];
1443 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1444 dprintk(1, "%s(): invalid buffer state %d\n", __func__,
1445 vb->state);
1446 return -EINVAL;
1447 }
Laurent Pinchart012043b2013-08-09 08:11:26 -03001448
1449 switch (vb->state) {
1450 case VB2_BUF_STATE_DEQUEUED:
1451 ret = __buf_prepare(vb, b);
1452 if (ret)
1453 return ret;
Hans Verkuil41381112013-12-13 13:13:39 -03001454 break;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001455 case VB2_BUF_STATE_PREPARED:
1456 break;
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001457 case VB2_BUF_STATE_PREPARING:
1458 dprintk(1, "qbuf: buffer still being prepared\n");
1459 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001460 default:
1461 dprintk(1, "qbuf: buffer already in use\n");
1462 return -EINVAL;
1463 }
1464
1465 /*
1466 * Add to the queued buffers list, a buffer will stay on it until
1467 * dequeued in dqbuf.
1468 */
1469 list_add_tail(&vb->queued_entry, &q->queued_list);
1470 vb->state = VB2_BUF_STATE_QUEUED;
Hans Verkuilf1343282014-02-24 14:44:50 -03001471 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1472 /*
1473 * For output buffers copy the timestamp if needed,
1474 * and the timecode field and flag if needed.
1475 */
Sakari Ailusc57ff792014-03-01 10:28:02 -03001476 if ((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
1477 V4L2_BUF_FLAG_TIMESTAMP_COPY)
Hans Verkuilf1343282014-02-24 14:44:50 -03001478 vb->v4l2_buf.timestamp = b->timestamp;
1479 vb->v4l2_buf.flags |= b->flags & V4L2_BUF_FLAG_TIMECODE;
1480 if (b->flags & V4L2_BUF_FLAG_TIMECODE)
1481 vb->v4l2_buf.timecode = b->timecode;
1482 }
Laurent Pinchart012043b2013-08-09 08:11:26 -03001483
1484 /*
1485 * If already streaming, give the buffer to driver for processing.
1486 * If not, the buffer will be given to driver on next streamon.
1487 */
1488 if (q->streaming)
1489 __enqueue_in_driver(vb);
1490
Hans Verkuil41381112013-12-13 13:13:39 -03001491 /* Fill buffer information for the userspace */
1492 __fill_v4l2_buffer(vb, b);
Laurent Pinchart012043b2013-08-09 08:11:26 -03001493
Hans Verkuil02f142e2013-12-13 13:13:42 -03001494 if (q->retry_start_streaming) {
1495 ret = vb2_start_streaming(q);
1496 if (ret)
1497 return ret;
1498 }
1499
Hans Verkuil41381112013-12-13 13:13:39 -03001500 dprintk(1, "%s() of buffer %d succeeded\n", __func__, vb->v4l2_buf.index);
1501 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001502}
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001503
1504/**
1505 * vb2_qbuf() - Queue a buffer from userspace
1506 * @q: videobuf2 queue
1507 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1508 * in driver
1509 *
1510 * Should be called from vidioc_qbuf ioctl handler of a driver.
1511 * This function:
1512 * 1) verifies the passed buffer,
1513 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1514 * which driver-specific buffer initialization can be performed,
1515 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1516 * callback for processing.
1517 *
1518 * The return values from this function are intended to be directly returned
1519 * from vidioc_qbuf handler in driver.
1520 */
1521int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1522{
1523 if (q->fileio) {
1524 dprintk(1, "%s(): file io in progress\n", __func__);
1525 return -EBUSY;
1526 }
1527
1528 return vb2_internal_qbuf(q, b);
1529}
Pawel Osciake23ccc02010-10-11 10:56:41 -03001530EXPORT_SYMBOL_GPL(vb2_qbuf);
1531
1532/**
1533 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1534 * for dequeuing
1535 *
1536 * Will sleep if required for nonblocking == false.
1537 */
1538static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1539{
1540 /*
1541 * All operations on vb_done_list are performed under done_lock
1542 * spinlock protection. However, buffers may be removed from
1543 * it and returned to userspace only while holding both driver's
1544 * lock and the done_lock spinlock. Thus we can be sure that as
1545 * long as we hold the driver's lock, the list will remain not
1546 * empty if list_empty() check succeeds.
1547 */
1548
1549 for (;;) {
1550 int ret;
1551
1552 if (!q->streaming) {
1553 dprintk(1, "Streaming off, will not wait for buffers\n");
1554 return -EINVAL;
1555 }
1556
1557 if (!list_empty(&q->done_list)) {
1558 /*
1559 * Found a buffer that we were waiting for.
1560 */
1561 break;
1562 }
1563
1564 if (nonblocking) {
1565 dprintk(1, "Nonblocking and no buffers to dequeue, "
1566 "will not wait\n");
1567 return -EAGAIN;
1568 }
1569
1570 /*
1571 * We are streaming and blocking, wait for another buffer to
1572 * become ready or for streamoff. Driver's lock is released to
1573 * allow streamoff or qbuf to be called while waiting.
1574 */
1575 call_qop(q, wait_prepare, q);
1576
1577 /*
1578 * All locks have been released, it is safe to sleep now.
1579 */
1580 dprintk(3, "Will sleep waiting for buffers\n");
1581 ret = wait_event_interruptible(q->done_wq,
1582 !list_empty(&q->done_list) || !q->streaming);
1583
1584 /*
1585 * We need to reevaluate both conditions again after reacquiring
1586 * the locks or return an error if one occurred.
1587 */
1588 call_qop(q, wait_finish, q);
Hans Verkuil32a77262012-09-28 06:12:53 -03001589 if (ret) {
1590 dprintk(1, "Sleep was interrupted\n");
Pawel Osciake23ccc02010-10-11 10:56:41 -03001591 return ret;
Hans Verkuil32a77262012-09-28 06:12:53 -03001592 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001593 }
1594 return 0;
1595}
1596
1597/**
1598 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1599 *
1600 * Will sleep if required for nonblocking == false.
1601 */
1602static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
Hans Verkuil32a77262012-09-28 06:12:53 -03001603 struct v4l2_buffer *b, int nonblocking)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001604{
1605 unsigned long flags;
1606 int ret;
1607
1608 /*
1609 * Wait for at least one buffer to become available on the done_list.
1610 */
1611 ret = __vb2_wait_for_done_vb(q, nonblocking);
1612 if (ret)
1613 return ret;
1614
1615 /*
1616 * Driver's lock has been held since we last verified that done_list
1617 * is not empty, so no need for another list_empty(done_list) check.
1618 */
1619 spin_lock_irqsave(&q->done_lock, flags);
1620 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
Hans Verkuil32a77262012-09-28 06:12:53 -03001621 /*
1622 * Only remove the buffer from done_list if v4l2_buffer can handle all
1623 * the planes.
1624 */
1625 ret = __verify_planes_array(*vb, b);
1626 if (!ret)
1627 list_del(&(*vb)->done_entry);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001628 spin_unlock_irqrestore(&q->done_lock, flags);
1629
Hans Verkuil32a77262012-09-28 06:12:53 -03001630 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001631}
1632
1633/**
1634 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1635 * @q: videobuf2 queue
1636 *
1637 * This function will wait until all buffers that have been given to the driver
1638 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1639 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1640 * taken, for example from stop_streaming() callback.
1641 */
1642int vb2_wait_for_all_buffers(struct vb2_queue *q)
1643{
1644 if (!q->streaming) {
1645 dprintk(1, "Streaming off, will not wait for buffers\n");
1646 return -EINVAL;
1647 }
1648
Hans Verkuil02f142e2013-12-13 13:13:42 -03001649 if (!q->retry_start_streaming)
1650 wait_event(q->done_wq, !atomic_read(&q->queued_count));
Pawel Osciake23ccc02010-10-11 10:56:41 -03001651 return 0;
1652}
1653EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1654
1655/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001656 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1657 */
1658static void __vb2_dqbuf(struct vb2_buffer *vb)
1659{
1660 struct vb2_queue *q = vb->vb2_queue;
1661 unsigned int i;
1662
1663 /* nothing to do if the buffer is already dequeued */
1664 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1665 return;
1666
1667 vb->state = VB2_BUF_STATE_DEQUEUED;
1668
1669 /* unmap DMABUF buffer */
1670 if (q->memory == V4L2_MEMORY_DMABUF)
1671 for (i = 0; i < vb->num_planes; ++i) {
1672 if (!vb->planes[i].dbuf_mapped)
1673 continue;
1674 call_memop(q, unmap_dmabuf, vb->planes[i].mem_priv);
1675 vb->planes[i].dbuf_mapped = 0;
1676 }
1677}
1678
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001679static int vb2_internal_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001680{
1681 struct vb2_buffer *vb = NULL;
1682 int ret;
1683
1684 if (b->type != q->type) {
1685 dprintk(1, "dqbuf: invalid buffer type\n");
1686 return -EINVAL;
1687 }
Hans Verkuil32a77262012-09-28 06:12:53 -03001688 ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
1689 if (ret < 0)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001690 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001691
1692 ret = call_qop(q, buf_finish, vb);
1693 if (ret) {
1694 dprintk(1, "dqbuf: buffer finish failed\n");
1695 return ret;
1696 }
1697
1698 switch (vb->state) {
1699 case VB2_BUF_STATE_DONE:
1700 dprintk(3, "dqbuf: Returning done buffer\n");
1701 break;
1702 case VB2_BUF_STATE_ERROR:
1703 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1704 break;
1705 default:
1706 dprintk(1, "dqbuf: Invalid buffer state\n");
1707 return -EINVAL;
1708 }
1709
1710 /* Fill buffer information for the userspace */
1711 __fill_v4l2_buffer(vb, b);
1712 /* Remove from videobuf queue */
1713 list_del(&vb->queued_entry);
Sumit Semwalc5384042012-06-14 10:37:37 -03001714 /* go back to dequeued state */
1715 __vb2_dqbuf(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001716
1717 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1718 vb->v4l2_buf.index, vb->state);
1719
Pawel Osciake23ccc02010-10-11 10:56:41 -03001720 return 0;
1721}
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001722
1723/**
1724 * vb2_dqbuf() - Dequeue a buffer to the userspace
1725 * @q: videobuf2 queue
1726 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1727 * in driver
1728 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1729 * buffers ready for dequeuing are present. Normally the driver
1730 * would be passing (file->f_flags & O_NONBLOCK) here
1731 *
1732 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1733 * This function:
1734 * 1) verifies the passed buffer,
1735 * 2) calls buf_finish callback in the driver (if provided), in which
1736 * driver can perform any additional operations that may be required before
1737 * returning the buffer to userspace, such as cache sync,
1738 * 3) the buffer struct members are filled with relevant information for
1739 * the userspace.
1740 *
1741 * The return values from this function are intended to be directly returned
1742 * from vidioc_dqbuf handler in driver.
1743 */
1744int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1745{
1746 if (q->fileio) {
1747 dprintk(1, "dqbuf: file io in progress\n");
1748 return -EBUSY;
1749 }
1750 return vb2_internal_dqbuf(q, b, nonblocking);
1751}
Pawel Osciake23ccc02010-10-11 10:56:41 -03001752EXPORT_SYMBOL_GPL(vb2_dqbuf);
1753
1754/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001755 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1756 *
1757 * Removes all queued buffers from driver's queue and all buffers queued by
1758 * userspace from videobuf's queue. Returns to state after reqbufs.
1759 */
1760static void __vb2_queue_cancel(struct vb2_queue *q)
1761{
1762 unsigned int i;
1763
Hans Verkuil02f142e2013-12-13 13:13:42 -03001764 if (q->retry_start_streaming) {
1765 q->retry_start_streaming = 0;
1766 q->streaming = 0;
1767 }
1768
Pawel Osciake23ccc02010-10-11 10:56:41 -03001769 /*
1770 * Tell driver to stop all transactions and release all queued
1771 * buffers.
1772 */
1773 if (q->streaming)
1774 call_qop(q, stop_streaming, q);
1775 q->streaming = 0;
1776
1777 /*
1778 * Remove all buffers from videobuf's list...
1779 */
1780 INIT_LIST_HEAD(&q->queued_list);
1781 /*
1782 * ...and done list; userspace will not receive any buffers it
1783 * has not already dequeued before initiating cancel.
1784 */
1785 INIT_LIST_HEAD(&q->done_list);
Marek Szyprowskiafdea8b2011-06-10 08:58:42 -03001786 atomic_set(&q->queued_count, 0);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001787 wake_up_all(&q->done_wq);
1788
1789 /*
1790 * Reinitialize all buffers for next use.
1791 */
1792 for (i = 0; i < q->num_buffers; ++i)
Sumit Semwalc5384042012-06-14 10:37:37 -03001793 __vb2_dqbuf(q->bufs[i]);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001794}
1795
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001796static int vb2_internal_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001797{
1798 struct vb2_buffer *vb;
1799 int ret;
1800
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001801 if (type != q->type) {
1802 dprintk(1, "streamon: invalid stream type\n");
1803 return -EINVAL;
1804 }
1805
1806 if (q->streaming) {
Ricardo Ribaldaf9560352013-11-08 07:08:45 -03001807 dprintk(3, "streamon successful: already streaming\n");
1808 return 0;
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001809 }
1810
1811 /*
1812 * If any buffers were queued before streamon,
1813 * we can now pass them to driver for processing.
1814 */
1815 list_for_each_entry(vb, &q->queued_list, queued_entry)
1816 __enqueue_in_driver(vb);
1817
Hans Verkuil02f142e2013-12-13 13:13:42 -03001818 /* Tell driver to start streaming. */
1819 ret = vb2_start_streaming(q);
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001820 if (ret) {
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001821 __vb2_queue_cancel(q);
1822 return ret;
1823 }
1824
1825 q->streaming = 1;
1826
1827 dprintk(3, "Streamon successful\n");
1828 return 0;
1829}
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001830
1831/**
1832 * vb2_streamon - start streaming
1833 * @q: videobuf2 queue
1834 * @type: type argument passed from userspace to vidioc_streamon handler
1835 *
1836 * Should be called from vidioc_streamon handler of a driver.
1837 * This function:
1838 * 1) verifies current state
1839 * 2) passes any previously queued buffers to the driver and starts streaming
1840 *
1841 * The return values from this function are intended to be directly returned
1842 * from vidioc_streamon handler in the driver.
1843 */
1844int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1845{
1846 if (q->fileio) {
1847 dprintk(1, "streamon: file io in progress\n");
1848 return -EBUSY;
1849 }
1850 return vb2_internal_streamon(q, type);
1851}
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001852EXPORT_SYMBOL_GPL(vb2_streamon);
1853
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001854static int vb2_internal_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1855{
1856 if (type != q->type) {
1857 dprintk(1, "streamoff: invalid stream type\n");
1858 return -EINVAL;
1859 }
1860
1861 if (!q->streaming) {
1862 dprintk(3, "streamoff successful: not streaming\n");
1863 return 0;
1864 }
1865
1866 /*
1867 * Cancel will pause streaming and remove all buffers from the driver
1868 * and videobuf, effectively returning control over them to userspace.
1869 */
1870 __vb2_queue_cancel(q);
1871
1872 dprintk(3, "Streamoff successful\n");
1873 return 0;
1874}
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001875
1876/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001877 * vb2_streamoff - stop streaming
1878 * @q: videobuf2 queue
1879 * @type: type argument passed from userspace to vidioc_streamoff handler
1880 *
1881 * Should be called from vidioc_streamoff handler of a driver.
1882 * This function:
1883 * 1) verifies current state,
1884 * 2) stop streaming and dequeues any queued buffers, including those previously
1885 * passed to the driver (after waiting for the driver to finish).
1886 *
1887 * This call can be used for pausing playback.
1888 * The return values from this function are intended to be directly returned
1889 * from vidioc_streamoff handler in the driver
1890 */
1891int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1892{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001893 if (q->fileio) {
1894 dprintk(1, "streamoff: file io in progress\n");
1895 return -EBUSY;
1896 }
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001897 return vb2_internal_streamoff(q, type);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001898}
1899EXPORT_SYMBOL_GPL(vb2_streamoff);
1900
1901/**
1902 * __find_plane_by_offset() - find plane associated with the given offset off
1903 */
1904static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1905 unsigned int *_buffer, unsigned int *_plane)
1906{
1907 struct vb2_buffer *vb;
1908 unsigned int buffer, plane;
1909
1910 /*
1911 * Go over all buffers and their planes, comparing the given offset
1912 * with an offset assigned to each plane. If a match is found,
1913 * return its buffer and plane numbers.
1914 */
1915 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1916 vb = q->bufs[buffer];
1917
1918 for (plane = 0; plane < vb->num_planes; ++plane) {
1919 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1920 *_buffer = buffer;
1921 *_plane = plane;
1922 return 0;
1923 }
1924 }
1925 }
1926
1927 return -EINVAL;
1928}
1929
1930/**
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03001931 * vb2_expbuf() - Export a buffer as a file descriptor
1932 * @q: videobuf2 queue
1933 * @eb: export buffer structure passed from userspace to vidioc_expbuf
1934 * handler in driver
1935 *
1936 * The return values from this function are intended to be directly returned
1937 * from vidioc_expbuf handler in driver.
1938 */
1939int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
1940{
1941 struct vb2_buffer *vb = NULL;
1942 struct vb2_plane *vb_plane;
1943 int ret;
1944 struct dma_buf *dbuf;
1945
1946 if (q->memory != V4L2_MEMORY_MMAP) {
1947 dprintk(1, "Queue is not currently set up for mmap\n");
1948 return -EINVAL;
1949 }
1950
1951 if (!q->mem_ops->get_dmabuf) {
1952 dprintk(1, "Queue does not support DMA buffer exporting\n");
1953 return -EINVAL;
1954 }
1955
Philipp Zabelea3aba82013-05-21 05:11:35 -03001956 if (eb->flags & ~(O_CLOEXEC | O_ACCMODE)) {
1957 dprintk(1, "Queue does support only O_CLOEXEC and access mode flags\n");
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03001958 return -EINVAL;
1959 }
1960
1961 if (eb->type != q->type) {
1962 dprintk(1, "qbuf: invalid buffer type\n");
1963 return -EINVAL;
1964 }
1965
1966 if (eb->index >= q->num_buffers) {
1967 dprintk(1, "buffer index out of range\n");
1968 return -EINVAL;
1969 }
1970
1971 vb = q->bufs[eb->index];
1972
1973 if (eb->plane >= vb->num_planes) {
1974 dprintk(1, "buffer plane out of range\n");
1975 return -EINVAL;
1976 }
1977
1978 vb_plane = &vb->planes[eb->plane];
1979
Philipp Zabelea3aba82013-05-21 05:11:35 -03001980 dbuf = call_memop(q, get_dmabuf, vb_plane->mem_priv, eb->flags & O_ACCMODE);
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03001981 if (IS_ERR_OR_NULL(dbuf)) {
1982 dprintk(1, "Failed to export buffer %d, plane %d\n",
1983 eb->index, eb->plane);
1984 return -EINVAL;
1985 }
1986
Philipp Zabelea3aba82013-05-21 05:11:35 -03001987 ret = dma_buf_fd(dbuf, eb->flags & ~O_ACCMODE);
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03001988 if (ret < 0) {
1989 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
1990 eb->index, eb->plane, ret);
1991 dma_buf_put(dbuf);
1992 return ret;
1993 }
1994
1995 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
1996 eb->index, eb->plane, ret);
1997 eb->fd = ret;
1998
1999 return 0;
2000}
2001EXPORT_SYMBOL_GPL(vb2_expbuf);
2002
2003/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03002004 * vb2_mmap() - map video buffers into application address space
2005 * @q: videobuf2 queue
2006 * @vma: vma passed to the mmap file operation handler in the driver
2007 *
2008 * Should be called from mmap file operation handler of a driver.
2009 * This function maps one plane of one of the available video buffers to
2010 * userspace. To map whole video memory allocated on reqbufs, this function
2011 * has to be called once per each plane per each buffer previously allocated.
2012 *
2013 * When the userspace application calls mmap, it passes to it an offset returned
2014 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
2015 * a "cookie", which is then used to identify the plane to be mapped.
2016 * This function finds a plane with a matching offset and a mapping is performed
2017 * by the means of a provided memory operation.
2018 *
2019 * The return values from this function are intended to be directly returned
2020 * from the mmap handler in driver.
2021 */
2022int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
2023{
2024 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002025 struct vb2_buffer *vb;
2026 unsigned int buffer, plane;
2027 int ret;
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -03002028 unsigned long length;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002029
2030 if (q->memory != V4L2_MEMORY_MMAP) {
2031 dprintk(1, "Queue is not currently set up for mmap\n");
2032 return -EINVAL;
2033 }
2034
2035 /*
2036 * Check memory area access mode.
2037 */
2038 if (!(vma->vm_flags & VM_SHARED)) {
2039 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
2040 return -EINVAL;
2041 }
2042 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
2043 if (!(vma->vm_flags & VM_WRITE)) {
2044 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
2045 return -EINVAL;
2046 }
2047 } else {
2048 if (!(vma->vm_flags & VM_READ)) {
2049 dprintk(1, "Invalid vma flags, VM_READ needed\n");
2050 return -EINVAL;
2051 }
2052 }
2053
2054 /*
2055 * Find the plane corresponding to the offset passed by userspace.
2056 */
2057 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2058 if (ret)
2059 return ret;
2060
2061 vb = q->bufs[buffer];
Pawel Osciake23ccc02010-10-11 10:56:41 -03002062
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -03002063 /*
2064 * MMAP requires page_aligned buffers.
2065 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
2066 * so, we need to do the same here.
2067 */
2068 length = PAGE_ALIGN(vb->v4l2_planes[plane].length);
2069 if (length < (vma->vm_end - vma->vm_start)) {
2070 dprintk(1,
2071 "MMAP invalid, as it would overflow buffer length\n");
Seung-Woo Kim068a0df2013-04-11 23:57:57 -03002072 return -EINVAL;
2073 }
2074
Marek Szyprowskia00d0262011-12-15 05:53:06 -03002075 ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002076 if (ret)
2077 return ret;
2078
Pawel Osciake23ccc02010-10-11 10:56:41 -03002079 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
2080 return 0;
2081}
2082EXPORT_SYMBOL_GPL(vb2_mmap);
2083
Scott Jiang6f524ec2011-09-21 09:25:23 -03002084#ifndef CONFIG_MMU
2085unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
2086 unsigned long addr,
2087 unsigned long len,
2088 unsigned long pgoff,
2089 unsigned long flags)
2090{
2091 unsigned long off = pgoff << PAGE_SHIFT;
2092 struct vb2_buffer *vb;
2093 unsigned int buffer, plane;
2094 int ret;
2095
2096 if (q->memory != V4L2_MEMORY_MMAP) {
2097 dprintk(1, "Queue is not currently set up for mmap\n");
2098 return -EINVAL;
2099 }
2100
2101 /*
2102 * Find the plane corresponding to the offset passed by userspace.
2103 */
2104 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2105 if (ret)
2106 return ret;
2107
2108 vb = q->bufs[buffer];
2109
2110 return (unsigned long)vb2_plane_vaddr(vb, plane);
2111}
2112EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
2113#endif
2114
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002115static int __vb2_init_fileio(struct vb2_queue *q, int read);
2116static int __vb2_cleanup_fileio(struct vb2_queue *q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002117
2118/**
2119 * vb2_poll() - implements poll userspace operation
2120 * @q: videobuf2 queue
2121 * @file: file argument passed to the poll file operation handler
2122 * @wait: wait argument passed to the poll file operation handler
2123 *
2124 * This function implements poll file operation handler for a driver.
2125 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
2126 * be informed that the file descriptor of a video device is available for
2127 * reading.
2128 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
2129 * will be reported as available for writing.
2130 *
Hans Verkuil95213ce2011-07-13 04:26:52 -03002131 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
2132 * pending events.
2133 *
Pawel Osciake23ccc02010-10-11 10:56:41 -03002134 * The return values from this function are intended to be directly returned
2135 * from poll handler in driver.
2136 */
2137unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
2138{
Hans Verkuil95213ce2011-07-13 04:26:52 -03002139 struct video_device *vfd = video_devdata(file);
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002140 unsigned long req_events = poll_requested_events(wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002141 struct vb2_buffer *vb = NULL;
Hans Verkuil95213ce2011-07-13 04:26:52 -03002142 unsigned int res = 0;
2143 unsigned long flags;
2144
2145 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
2146 struct v4l2_fh *fh = file->private_data;
2147
2148 if (v4l2_event_pending(fh))
2149 res = POLLPRI;
2150 else if (req_events & POLLPRI)
2151 poll_wait(file, &fh->wait, wait);
2152 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03002153
Hans Verkuilcd138232013-01-30 13:29:02 -03002154 if (!V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLIN | POLLRDNORM)))
2155 return res;
2156 if (V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLOUT | POLLWRNORM)))
2157 return res;
2158
Pawel Osciake23ccc02010-10-11 10:56:41 -03002159 /*
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03002160 * Start file I/O emulator only if streaming API has not been used yet.
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002161 */
2162 if (q->num_buffers == 0 && q->fileio == NULL) {
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002163 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2164 (req_events & (POLLIN | POLLRDNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002165 if (__vb2_init_fileio(q, 1))
2166 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002167 }
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002168 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2169 (req_events & (POLLOUT | POLLWRNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002170 if (__vb2_init_fileio(q, 0))
2171 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002172 /*
2173 * Write to OUTPUT queue can be done immediately.
2174 */
Hans Verkuil95213ce2011-07-13 04:26:52 -03002175 return res | POLLOUT | POLLWRNORM;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002176 }
2177 }
2178
2179 /*
Pawel Osciake23ccc02010-10-11 10:56:41 -03002180 * There is nothing to wait for if no buffers have already been queued.
2181 */
2182 if (list_empty(&q->queued_list))
Hans Verkuil95213ce2011-07-13 04:26:52 -03002183 return res | POLLERR;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002184
Seung-Woo Kim412cb872013-05-20 23:47:29 -03002185 if (list_empty(&q->done_list))
2186 poll_wait(file, &q->done_wq, wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002187
2188 /*
2189 * Take first buffer available for dequeuing.
2190 */
2191 spin_lock_irqsave(&q->done_lock, flags);
2192 if (!list_empty(&q->done_list))
2193 vb = list_first_entry(&q->done_list, struct vb2_buffer,
2194 done_entry);
2195 spin_unlock_irqrestore(&q->done_lock, flags);
2196
2197 if (vb && (vb->state == VB2_BUF_STATE_DONE
2198 || vb->state == VB2_BUF_STATE_ERROR)) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002199 return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
2200 res | POLLOUT | POLLWRNORM :
2201 res | POLLIN | POLLRDNORM;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002202 }
Hans Verkuil95213ce2011-07-13 04:26:52 -03002203 return res;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002204}
2205EXPORT_SYMBOL_GPL(vb2_poll);
2206
2207/**
2208 * vb2_queue_init() - initialize a videobuf2 queue
2209 * @q: videobuf2 queue; this structure should be allocated in driver
2210 *
2211 * The vb2_queue structure should be allocated by the driver. The driver is
2212 * responsible of clearing it's content and setting initial values for some
2213 * required entries before calling this function.
2214 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2215 * to the struct vb2_queue description in include/media/videobuf2-core.h
2216 * for more information.
2217 */
2218int vb2_queue_init(struct vb2_queue *q)
2219{
Ezequiel Garcia896f38f2012-09-17 14:59:30 -03002220 /*
2221 * Sanity check
2222 */
2223 if (WARN_ON(!q) ||
2224 WARN_ON(!q->ops) ||
2225 WARN_ON(!q->mem_ops) ||
2226 WARN_ON(!q->type) ||
2227 WARN_ON(!q->io_modes) ||
2228 WARN_ON(!q->ops->queue_setup) ||
Kamil Debski6aa69f92013-01-25 06:29:57 -03002229 WARN_ON(!q->ops->buf_queue) ||
Sakari Ailusade48682014-02-25 19:12:19 -03002230 WARN_ON(q->timestamp_flags & ~V4L2_BUF_FLAG_TIMESTAMP_MASK))
Ezequiel Garcia896f38f2012-09-17 14:59:30 -03002231 return -EINVAL;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002232
Kamil Debski6aa69f92013-01-25 06:29:57 -03002233 /* Warn that the driver should choose an appropriate timestamp type */
Sakari Ailusc57ff792014-03-01 10:28:02 -03002234 WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
2235 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
Kamil Debski6aa69f92013-01-25 06:29:57 -03002236
Pawel Osciake23ccc02010-10-11 10:56:41 -03002237 INIT_LIST_HEAD(&q->queued_list);
2238 INIT_LIST_HEAD(&q->done_list);
2239 spin_lock_init(&q->done_lock);
2240 init_waitqueue_head(&q->done_wq);
2241
2242 if (q->buf_struct_size == 0)
2243 q->buf_struct_size = sizeof(struct vb2_buffer);
2244
2245 return 0;
2246}
2247EXPORT_SYMBOL_GPL(vb2_queue_init);
2248
2249/**
2250 * vb2_queue_release() - stop streaming, release the queue and free memory
2251 * @q: videobuf2 queue
2252 *
2253 * This function stops streaming and performs necessary clean ups, including
2254 * freeing video buffer memory. The driver is responsible for freeing
2255 * the vb2_queue structure itself.
2256 */
2257void vb2_queue_release(struct vb2_queue *q)
2258{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002259 __vb2_cleanup_fileio(q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002260 __vb2_queue_cancel(q);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03002261 __vb2_queue_free(q, q->num_buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002262}
2263EXPORT_SYMBOL_GPL(vb2_queue_release);
2264
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002265/**
2266 * struct vb2_fileio_buf - buffer context used by file io emulator
2267 *
2268 * vb2 provides a compatibility layer and emulator of file io (read and
2269 * write) calls on top of streaming API. This structure is used for
2270 * tracking context related to the buffers.
2271 */
2272struct vb2_fileio_buf {
2273 void *vaddr;
2274 unsigned int size;
2275 unsigned int pos;
2276 unsigned int queued:1;
2277};
2278
2279/**
2280 * struct vb2_fileio_data - queue context used by file io emulator
2281 *
2282 * vb2 provides a compatibility layer and emulator of file io (read and
2283 * write) calls on top of streaming API. For proper operation it required
2284 * this structure to save the driver state between each call of the read
2285 * or write function.
2286 */
2287struct vb2_fileio_data {
2288 struct v4l2_requestbuffers req;
2289 struct v4l2_buffer b;
2290 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
2291 unsigned int index;
2292 unsigned int q_count;
2293 unsigned int dq_count;
2294 unsigned int flags;
2295};
2296
2297/**
2298 * __vb2_init_fileio() - initialize file io emulator
2299 * @q: videobuf2 queue
2300 * @read: mode selector (1 means read, 0 means write)
2301 */
2302static int __vb2_init_fileio(struct vb2_queue *q, int read)
2303{
2304 struct vb2_fileio_data *fileio;
2305 int i, ret;
2306 unsigned int count = 0;
2307
2308 /*
2309 * Sanity check
2310 */
2311 if ((read && !(q->io_modes & VB2_READ)) ||
2312 (!read && !(q->io_modes & VB2_WRITE)))
2313 BUG();
2314
2315 /*
2316 * Check if device supports mapping buffers to kernel virtual space.
2317 */
2318 if (!q->mem_ops->vaddr)
2319 return -EBUSY;
2320
2321 /*
2322 * Check if streaming api has not been already activated.
2323 */
2324 if (q->streaming || q->num_buffers > 0)
2325 return -EBUSY;
2326
2327 /*
2328 * Start with count 1, driver can increase it in queue_setup()
2329 */
2330 count = 1;
2331
2332 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
2333 (read) ? "read" : "write", count, q->io_flags);
2334
2335 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
2336 if (fileio == NULL)
2337 return -ENOMEM;
2338
2339 fileio->flags = q->io_flags;
2340
2341 /*
2342 * Request buffers and use MMAP type to force driver
2343 * to allocate buffers by itself.
2344 */
2345 fileio->req.count = count;
2346 fileio->req.memory = V4L2_MEMORY_MMAP;
2347 fileio->req.type = q->type;
2348 ret = vb2_reqbufs(q, &fileio->req);
2349 if (ret)
2350 goto err_kfree;
2351
2352 /*
2353 * Check if plane_count is correct
2354 * (multiplane buffers are not supported).
2355 */
2356 if (q->bufs[0]->num_planes != 1) {
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002357 ret = -EBUSY;
2358 goto err_reqbufs;
2359 }
2360
2361 /*
2362 * Get kernel address of each buffer.
2363 */
2364 for (i = 0; i < q->num_buffers; i++) {
2365 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
Wei Yongjun5dd69462013-05-13 01:48:45 -03002366 if (fileio->bufs[i].vaddr == NULL) {
2367 ret = -EINVAL;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002368 goto err_reqbufs;
Wei Yongjun5dd69462013-05-13 01:48:45 -03002369 }
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002370 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
2371 }
2372
2373 /*
2374 * Read mode requires pre queuing of all buffers.
2375 */
2376 if (read) {
2377 /*
2378 * Queue all buffers.
2379 */
2380 for (i = 0; i < q->num_buffers; i++) {
2381 struct v4l2_buffer *b = &fileio->b;
2382 memset(b, 0, sizeof(*b));
2383 b->type = q->type;
2384 b->memory = q->memory;
2385 b->index = i;
2386 ret = vb2_qbuf(q, b);
2387 if (ret)
2388 goto err_reqbufs;
2389 fileio->bufs[i].queued = 1;
2390 }
Hans Verkuil88e26872013-12-13 13:13:45 -03002391 fileio->index = q->num_buffers;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002392 }
2393
Hans Verkuil02f142e2013-12-13 13:13:42 -03002394 /*
2395 * Start streaming.
2396 */
2397 ret = vb2_streamon(q, q->type);
2398 if (ret)
2399 goto err_reqbufs;
2400
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002401 q->fileio = fileio;
2402
2403 return ret;
2404
2405err_reqbufs:
Hans de Goedea67e1722012-05-08 14:47:39 -03002406 fileio->req.count = 0;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002407 vb2_reqbufs(q, &fileio->req);
2408
2409err_kfree:
2410 kfree(fileio);
2411 return ret;
2412}
2413
2414/**
2415 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2416 * @q: videobuf2 queue
2417 */
2418static int __vb2_cleanup_fileio(struct vb2_queue *q)
2419{
2420 struct vb2_fileio_data *fileio = q->fileio;
2421
2422 if (fileio) {
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002423 vb2_internal_streamoff(q, q->type);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002424 q->fileio = NULL;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002425 fileio->req.count = 0;
2426 vb2_reqbufs(q, &fileio->req);
2427 kfree(fileio);
2428 dprintk(3, "file io emulator closed\n");
2429 }
2430 return 0;
2431}
2432
2433/**
2434 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2435 * @q: videobuf2 queue
2436 * @data: pointed to target userspace buffer
2437 * @count: number of bytes to read or write
2438 * @ppos: file handle position tracking pointer
2439 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2440 * @read: access mode selector (1 means read, 0 means write)
2441 */
2442static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
2443 loff_t *ppos, int nonblock, int read)
2444{
2445 struct vb2_fileio_data *fileio;
2446 struct vb2_fileio_buf *buf;
2447 int ret, index;
2448
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002449 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002450 read ? "read" : "write", (long)*ppos, count,
2451 nonblock ? "non" : "");
2452
2453 if (!data)
2454 return -EINVAL;
2455
2456 /*
2457 * Initialize emulator on first call.
2458 */
2459 if (!q->fileio) {
2460 ret = __vb2_init_fileio(q, read);
2461 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
2462 if (ret)
2463 return ret;
2464 }
2465 fileio = q->fileio;
2466
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002467 /*
2468 * Check if we need to dequeue the buffer.
2469 */
Hans Verkuil88e26872013-12-13 13:13:45 -03002470 index = fileio->index;
2471 if (index >= q->num_buffers) {
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002472 /*
2473 * Call vb2_dqbuf to get buffer back.
2474 */
2475 memset(&fileio->b, 0, sizeof(fileio->b));
2476 fileio->b.type = q->type;
2477 fileio->b.memory = q->memory;
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002478 ret = vb2_internal_dqbuf(q, &fileio->b, nonblock);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002479 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2480 if (ret)
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002481 return ret;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002482 fileio->dq_count += 1;
2483
Hans Verkuil88e26872013-12-13 13:13:45 -03002484 index = fileio->b.index;
2485 buf = &fileio->bufs[index];
2486
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002487 /*
2488 * Get number of bytes filled by the driver
2489 */
Hans Verkuil88e26872013-12-13 13:13:45 -03002490 buf->pos = 0;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002491 buf->queued = 0;
Hans Verkuil88e26872013-12-13 13:13:45 -03002492 buf->size = read ? vb2_get_plane_payload(q->bufs[index], 0)
2493 : vb2_plane_size(q->bufs[index], 0);
2494 } else {
2495 buf = &fileio->bufs[index];
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002496 }
2497
2498 /*
2499 * Limit count on last few bytes of the buffer.
2500 */
2501 if (buf->pos + count > buf->size) {
2502 count = buf->size - buf->pos;
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002503 dprintk(5, "reducing read count: %zd\n", count);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002504 }
2505
2506 /*
2507 * Transfer data to userspace.
2508 */
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002509 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002510 count, index, buf->pos);
2511 if (read)
2512 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2513 else
2514 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2515 if (ret) {
2516 dprintk(3, "file io: error copying data\n");
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002517 return -EFAULT;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002518 }
2519
2520 /*
2521 * Update counters.
2522 */
2523 buf->pos += count;
2524 *ppos += count;
2525
2526 /*
2527 * Queue next buffer if required.
2528 */
2529 if (buf->pos == buf->size ||
2530 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2531 /*
2532 * Check if this is the last buffer to read.
2533 */
2534 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2535 fileio->dq_count == 1) {
2536 dprintk(3, "file io: read limit reached\n");
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002537 return __vb2_cleanup_fileio(q);
2538 }
2539
2540 /*
2541 * Call vb2_qbuf and give buffer to the driver.
2542 */
2543 memset(&fileio->b, 0, sizeof(fileio->b));
2544 fileio->b.type = q->type;
2545 fileio->b.memory = q->memory;
2546 fileio->b.index = index;
2547 fileio->b.bytesused = buf->pos;
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002548 ret = vb2_internal_qbuf(q, &fileio->b);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002549 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2550 if (ret)
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002551 return ret;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002552
2553 /*
2554 * Buffer has been queued, update the status
2555 */
2556 buf->pos = 0;
2557 buf->queued = 1;
Hans Verkuil88e26872013-12-13 13:13:45 -03002558 buf->size = vb2_plane_size(q->bufs[index], 0);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002559 fileio->q_count += 1;
Hans Verkuil88e26872013-12-13 13:13:45 -03002560 if (fileio->index < q->num_buffers)
2561 fileio->index++;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002562 }
2563
2564 /*
2565 * Return proper number of bytes processed.
2566 */
2567 if (ret == 0)
2568 ret = count;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002569 return ret;
2570}
2571
2572size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2573 loff_t *ppos, int nonblocking)
2574{
2575 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2576}
2577EXPORT_SYMBOL_GPL(vb2_read);
2578
Ricardo Ribalda819585b2013-08-28 04:39:29 -03002579size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002580 loff_t *ppos, int nonblocking)
2581{
Ricardo Ribalda819585b2013-08-28 04:39:29 -03002582 return __vb2_perform_fileio(q, (char __user *) data, count,
2583 ppos, nonblocking, 0);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002584}
2585EXPORT_SYMBOL_GPL(vb2_write);
2586
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002587
2588/*
2589 * The following functions are not part of the vb2 core API, but are helper
2590 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2591 * and struct vb2_ops.
2592 * They contain boilerplate code that most if not all drivers have to do
2593 * and so they simplify the driver code.
2594 */
2595
2596/* The queue is busy if there is a owner and you are not that owner. */
2597static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
2598{
2599 return vdev->queue->owner && vdev->queue->owner != file->private_data;
2600}
2601
2602/* vb2 ioctl helpers */
2603
2604int vb2_ioctl_reqbufs(struct file *file, void *priv,
2605 struct v4l2_requestbuffers *p)
2606{
2607 struct video_device *vdev = video_devdata(file);
2608 int res = __verify_memory_type(vdev->queue, p->memory, p->type);
2609
2610 if (res)
2611 return res;
2612 if (vb2_queue_is_busy(vdev, file))
2613 return -EBUSY;
2614 res = __reqbufs(vdev->queue, p);
2615 /* If count == 0, then the owner has released all buffers and he
2616 is no longer owner of the queue. Otherwise we have a new owner. */
2617 if (res == 0)
2618 vdev->queue->owner = p->count ? file->private_data : NULL;
2619 return res;
2620}
2621EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
2622
2623int vb2_ioctl_create_bufs(struct file *file, void *priv,
2624 struct v4l2_create_buffers *p)
2625{
2626 struct video_device *vdev = video_devdata(file);
2627 int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
2628
2629 p->index = vdev->queue->num_buffers;
2630 /* If count == 0, then just check if memory and type are valid.
2631 Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2632 if (p->count == 0)
2633 return res != -EBUSY ? res : 0;
2634 if (res)
2635 return res;
2636 if (vb2_queue_is_busy(vdev, file))
2637 return -EBUSY;
2638 res = __create_bufs(vdev->queue, p);
2639 if (res == 0)
2640 vdev->queue->owner = file->private_data;
2641 return res;
2642}
2643EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
2644
2645int vb2_ioctl_prepare_buf(struct file *file, void *priv,
2646 struct v4l2_buffer *p)
2647{
2648 struct video_device *vdev = video_devdata(file);
2649
2650 if (vb2_queue_is_busy(vdev, file))
2651 return -EBUSY;
2652 return vb2_prepare_buf(vdev->queue, p);
2653}
2654EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
2655
2656int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
2657{
2658 struct video_device *vdev = video_devdata(file);
2659
2660 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2661 return vb2_querybuf(vdev->queue, p);
2662}
2663EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
2664
2665int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2666{
2667 struct video_device *vdev = video_devdata(file);
2668
2669 if (vb2_queue_is_busy(vdev, file))
2670 return -EBUSY;
2671 return vb2_qbuf(vdev->queue, p);
2672}
2673EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
2674
2675int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2676{
2677 struct video_device *vdev = video_devdata(file);
2678
2679 if (vb2_queue_is_busy(vdev, file))
2680 return -EBUSY;
2681 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
2682}
2683EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
2684
2685int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
2686{
2687 struct video_device *vdev = video_devdata(file);
2688
2689 if (vb2_queue_is_busy(vdev, file))
2690 return -EBUSY;
2691 return vb2_streamon(vdev->queue, i);
2692}
2693EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
2694
2695int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
2696{
2697 struct video_device *vdev = video_devdata(file);
2698
2699 if (vb2_queue_is_busy(vdev, file))
2700 return -EBUSY;
2701 return vb2_streamoff(vdev->queue, i);
2702}
2703EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
2704
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002705int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
2706{
2707 struct video_device *vdev = video_devdata(file);
2708
2709 if (vb2_queue_is_busy(vdev, file))
2710 return -EBUSY;
2711 return vb2_expbuf(vdev->queue, p);
2712}
2713EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
2714
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002715/* v4l2_file_operations helpers */
2716
2717int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
2718{
2719 struct video_device *vdev = video_devdata(file);
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002720 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2721 int err;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002722
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002723 if (lock && mutex_lock_interruptible(lock))
2724 return -ERESTARTSYS;
2725 err = vb2_mmap(vdev->queue, vma);
2726 if (lock)
2727 mutex_unlock(lock);
2728 return err;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002729}
2730EXPORT_SYMBOL_GPL(vb2_fop_mmap);
2731
Ricardo Ribalda1380f572013-11-25 05:49:02 -03002732int _vb2_fop_release(struct file *file, struct mutex *lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002733{
2734 struct video_device *vdev = video_devdata(file);
2735
2736 if (file->private_data == vdev->queue->owner) {
Ricardo Ribalda1380f572013-11-25 05:49:02 -03002737 if (lock)
2738 mutex_lock(lock);
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002739 vb2_queue_release(vdev->queue);
2740 vdev->queue->owner = NULL;
Ricardo Ribalda1380f572013-11-25 05:49:02 -03002741 if (lock)
2742 mutex_unlock(lock);
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002743 }
2744 return v4l2_fh_release(file);
2745}
Ricardo Ribalda1380f572013-11-25 05:49:02 -03002746EXPORT_SYMBOL_GPL(_vb2_fop_release);
2747
2748int vb2_fop_release(struct file *file)
2749{
2750 struct video_device *vdev = video_devdata(file);
2751 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2752
2753 return _vb2_fop_release(file, lock);
2754}
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002755EXPORT_SYMBOL_GPL(vb2_fop_release);
2756
Ricardo Ribalda819585b2013-08-28 04:39:29 -03002757ssize_t vb2_fop_write(struct file *file, const char __user *buf,
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002758 size_t count, loff_t *ppos)
2759{
2760 struct video_device *vdev = video_devdata(file);
2761 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002762 int err = -EBUSY;
2763
Hans Verkuilcf533732012-07-31 04:02:25 -03002764 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002765 return -ERESTARTSYS;
2766 if (vb2_queue_is_busy(vdev, file))
2767 goto exit;
2768 err = vb2_write(vdev->queue, buf, count, ppos,
2769 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03002770 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002771 vdev->queue->owner = file->private_data;
2772exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03002773 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002774 mutex_unlock(lock);
2775 return err;
2776}
2777EXPORT_SYMBOL_GPL(vb2_fop_write);
2778
2779ssize_t vb2_fop_read(struct file *file, char __user *buf,
2780 size_t count, loff_t *ppos)
2781{
2782 struct video_device *vdev = video_devdata(file);
2783 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002784 int err = -EBUSY;
2785
Hans Verkuilcf533732012-07-31 04:02:25 -03002786 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002787 return -ERESTARTSYS;
2788 if (vb2_queue_is_busy(vdev, file))
2789 goto exit;
2790 err = vb2_read(vdev->queue, buf, count, ppos,
2791 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03002792 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002793 vdev->queue->owner = file->private_data;
2794exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03002795 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002796 mutex_unlock(lock);
2797 return err;
2798}
2799EXPORT_SYMBOL_GPL(vb2_fop_read);
2800
2801unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
2802{
2803 struct video_device *vdev = video_devdata(file);
2804 struct vb2_queue *q = vdev->queue;
2805 struct mutex *lock = q->lock ? q->lock : vdev->lock;
2806 unsigned long req_events = poll_requested_events(wait);
2807 unsigned res;
2808 void *fileio;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002809 bool must_lock = false;
2810
2811 /* Try to be smart: only lock if polling might start fileio,
2812 otherwise locking will only introduce unwanted delays. */
2813 if (q->num_buffers == 0 && q->fileio == NULL) {
2814 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2815 (req_events & (POLLIN | POLLRDNORM)))
2816 must_lock = true;
2817 else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2818 (req_events & (POLLOUT | POLLWRNORM)))
2819 must_lock = true;
2820 }
2821
2822 /* If locking is needed, but this helper doesn't know how, then you
2823 shouldn't be using this helper but you should write your own. */
Hans Verkuilcf533732012-07-31 04:02:25 -03002824 WARN_ON(must_lock && !lock);
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002825
Hans Verkuilcf533732012-07-31 04:02:25 -03002826 if (must_lock && lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002827 return POLLERR;
2828
2829 fileio = q->fileio;
2830
2831 res = vb2_poll(vdev->queue, file, wait);
2832
2833 /* If fileio was started, then we have a new queue owner. */
2834 if (must_lock && !fileio && q->fileio)
2835 q->owner = file->private_data;
Hans Verkuilcf533732012-07-31 04:02:25 -03002836 if (must_lock && lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002837 mutex_unlock(lock);
2838 return res;
2839}
2840EXPORT_SYMBOL_GPL(vb2_fop_poll);
2841
2842#ifndef CONFIG_MMU
2843unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
2844 unsigned long len, unsigned long pgoff, unsigned long flags)
2845{
2846 struct video_device *vdev = video_devdata(file);
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002847 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2848 int ret;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002849
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002850 if (lock && mutex_lock_interruptible(lock))
2851 return -ERESTARTSYS;
2852 ret = vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
2853 if (lock)
2854 mutex_unlock(lock);
2855 return ret;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002856}
2857EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
2858#endif
2859
2860/* vb2_ops helpers. Only use if vq->lock is non-NULL. */
2861
2862void vb2_ops_wait_prepare(struct vb2_queue *vq)
2863{
2864 mutex_unlock(vq->lock);
2865}
2866EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
2867
2868void vb2_ops_wait_finish(struct vb2_queue *vq)
2869{
2870 mutex_lock(vq->lock);
2871}
2872EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
2873
Pawel Osciake23ccc02010-10-11 10:56:41 -03002874MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
Pawel Osciak95072082011-03-13 15:23:32 -03002875MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
Pawel Osciake23ccc02010-10-11 10:56:41 -03002876MODULE_LICENSE("GPL");