blob: a9a9c6a1c15800f190d99a4f0bbb2e13d0cc5145 [file] [log] [blame]
Pawel Osciake23ccc02010-10-11 10:56:41 -03001/*
2 * videobuf2-core.c - V4L2 driver helper framework
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 *
Pawel Osciak95072082011-03-13 15:23:32 -03006 * Author: Pawel Osciak <pawel@osciak.com>
Pawel Osciake23ccc02010-10-11 10:56:41 -03007 * Marek Szyprowski <m.szyprowski@samsung.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation.
12 */
13
14#include <linux/err.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/mm.h>
18#include <linux/poll.h>
19#include <linux/slab.h>
20#include <linux/sched.h>
21
Hans Verkuil95213ce2011-07-13 04:26:52 -030022#include <media/v4l2-dev.h>
23#include <media/v4l2-fh.h>
24#include <media/v4l2-event.h>
Pawel Osciake23ccc02010-10-11 10:56:41 -030025#include <media/videobuf2-core.h>
26
27static int debug;
28module_param(debug, int, 0644);
29
30#define dprintk(level, fmt, arg...) \
31 do { \
32 if (debug >= level) \
33 printk(KERN_DEBUG "vb2: " fmt, ## arg); \
34 } while (0)
35
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030036#define call_memop(q, op, args...) \
Pawel Osciake23ccc02010-10-11 10:56:41 -030037 (((q)->mem_ops->op) ? \
38 ((q)->mem_ops->op(args)) : 0)
39
40#define call_qop(q, op, args...) \
41 (((q)->ops->op) ? ((q)->ops->op(args)) : 0)
42
Sakari Ailus1b18e7a2012-10-22 17:10:16 -030043#define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -030044 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
Sakari Ailus1b18e7a2012-10-22 17:10:16 -030045 V4L2_BUF_FLAG_PREPARED | \
46 V4L2_BUF_FLAG_TIMESTAMP_MASK)
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -030047
Pawel Osciake23ccc02010-10-11 10:56:41 -030048/**
49 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
50 */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030051static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
Pawel Osciake23ccc02010-10-11 10:56:41 -030052{
53 struct vb2_queue *q = vb->vb2_queue;
54 void *mem_priv;
55 int plane;
56
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -030057 /*
58 * Allocate memory for all planes in this buffer
59 * NOTE: mmapped areas should be page aligned
60 */
Pawel Osciake23ccc02010-10-11 10:56:41 -030061 for (plane = 0; plane < vb->num_planes; ++plane) {
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -030062 unsigned long size = PAGE_ALIGN(q->plane_sizes[plane]);
63
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030064 mem_priv = call_memop(q, alloc, q->alloc_ctx[plane],
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -030065 size, q->gfp_flags);
Guennadi Liakhovetski62a79432011-03-22 09:24:58 -030066 if (IS_ERR_OR_NULL(mem_priv))
Pawel Osciake23ccc02010-10-11 10:56:41 -030067 goto free;
68
69 /* Associate allocator private data with this plane */
70 vb->planes[plane].mem_priv = mem_priv;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030071 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
Pawel Osciake23ccc02010-10-11 10:56:41 -030072 }
73
74 return 0;
75free:
76 /* Free already allocated memory if one of the allocations failed */
Marek Szyprowskia00d0262011-12-15 05:53:06 -030077 for (; plane > 0; --plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030078 call_memop(q, put, vb->planes[plane - 1].mem_priv);
Marek Szyprowskia00d0262011-12-15 05:53:06 -030079 vb->planes[plane - 1].mem_priv = NULL;
80 }
Pawel Osciake23ccc02010-10-11 10:56:41 -030081
82 return -ENOMEM;
83}
84
85/**
86 * __vb2_buf_mem_free() - free memory of the given buffer
87 */
88static void __vb2_buf_mem_free(struct vb2_buffer *vb)
89{
90 struct vb2_queue *q = vb->vb2_queue;
91 unsigned int plane;
92
93 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030094 call_memop(q, put, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -030095 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskia00d0262011-12-15 05:53:06 -030096 dprintk(3, "Freed plane %d of buffer %d\n", plane,
97 vb->v4l2_buf.index);
Pawel Osciake23ccc02010-10-11 10:56:41 -030098 }
99}
100
101/**
102 * __vb2_buf_userptr_put() - release userspace memory associated with
103 * a USERPTR buffer
104 */
105static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
106{
107 struct vb2_queue *q = vb->vb2_queue;
108 unsigned int plane;
109
110 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300111 if (vb->planes[plane].mem_priv)
112 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
113 vb->planes[plane].mem_priv = NULL;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300114 }
115}
116
117/**
Sumit Semwalc5384042012-06-14 10:37:37 -0300118 * __vb2_plane_dmabuf_put() - release memory associated with
119 * a DMABUF shared plane
120 */
121static void __vb2_plane_dmabuf_put(struct vb2_queue *q, struct vb2_plane *p)
122{
123 if (!p->mem_priv)
124 return;
125
126 if (p->dbuf_mapped)
127 call_memop(q, unmap_dmabuf, p->mem_priv);
128
129 call_memop(q, detach_dmabuf, p->mem_priv);
130 dma_buf_put(p->dbuf);
131 memset(p, 0, sizeof(*p));
132}
133
134/**
135 * __vb2_buf_dmabuf_put() - release memory associated with
136 * a DMABUF shared buffer
137 */
138static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
139{
140 struct vb2_queue *q = vb->vb2_queue;
141 unsigned int plane;
142
143 for (plane = 0; plane < vb->num_planes; ++plane)
144 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
145}
146
147/**
Hans Verkuila5e3d742013-12-04 15:14:05 +0100148 * __setup_lengths() - setup initial lengths for every plane in
149 * every buffer on the queue
150 */
151static void __setup_lengths(struct vb2_queue *q, unsigned int n)
152{
153 unsigned int buffer, plane;
154 struct vb2_buffer *vb;
155
156 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
157 vb = q->bufs[buffer];
158 if (!vb)
159 continue;
160
161 for (plane = 0; plane < vb->num_planes; ++plane)
162 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
163 }
164}
165
166/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300167 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
168 * every buffer on the queue
169 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300170static void __setup_offsets(struct vb2_queue *q, unsigned int n)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300171{
172 unsigned int buffer, plane;
173 struct vb2_buffer *vb;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300174 unsigned long off;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300175
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300176 if (q->num_buffers) {
177 struct v4l2_plane *p;
178 vb = q->bufs[q->num_buffers - 1];
179 p = &vb->v4l2_planes[vb->num_planes - 1];
180 off = PAGE_ALIGN(p->m.mem_offset + p->length);
181 } else {
182 off = 0;
183 }
184
185 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300186 vb = q->bufs[buffer];
187 if (!vb)
188 continue;
189
190 for (plane = 0; plane < vb->num_planes; ++plane) {
191 vb->v4l2_planes[plane].m.mem_offset = off;
192
193 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
194 buffer, plane, off);
195
196 off += vb->v4l2_planes[plane].length;
197 off = PAGE_ALIGN(off);
198 }
199 }
200}
201
202/**
203 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
204 * video buffer memory for all buffers/planes on the queue and initializes the
205 * queue
206 *
207 * Returns the number of buffers successfully allocated.
208 */
209static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300210 unsigned int num_buffers, unsigned int num_planes)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300211{
212 unsigned int buffer;
213 struct vb2_buffer *vb;
214 int ret;
215
216 for (buffer = 0; buffer < num_buffers; ++buffer) {
217 /* Allocate videobuf buffer structures */
218 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
219 if (!vb) {
220 dprintk(1, "Memory alloc for buffer struct failed\n");
221 break;
222 }
223
224 /* Length stores number of planes for multiplanar buffers */
225 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
226 vb->v4l2_buf.length = num_planes;
227
228 vb->state = VB2_BUF_STATE_DEQUEUED;
229 vb->vb2_queue = q;
230 vb->num_planes = num_planes;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300231 vb->v4l2_buf.index = q->num_buffers + buffer;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300232 vb->v4l2_buf.type = q->type;
233 vb->v4l2_buf.memory = memory;
234
235 /* Allocate video buffer memory for the MMAP type */
236 if (memory == V4L2_MEMORY_MMAP) {
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300237 ret = __vb2_buf_mem_alloc(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300238 if (ret) {
239 dprintk(1, "Failed allocating memory for "
240 "buffer %d\n", buffer);
241 kfree(vb);
242 break;
243 }
244 /*
245 * Call the driver-provided buffer initialization
246 * callback, if given. An error in initialization
247 * results in queue setup failure.
248 */
249 ret = call_qop(q, buf_init, vb);
250 if (ret) {
251 dprintk(1, "Buffer %d %p initialization"
252 " failed\n", buffer, vb);
253 __vb2_buf_mem_free(vb);
254 kfree(vb);
255 break;
256 }
257 }
258
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300259 q->bufs[q->num_buffers + buffer] = vb;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300260 }
261
Hans Verkuila5e3d742013-12-04 15:14:05 +0100262 __setup_lengths(q, buffer);
Philipp Zabeldc775232013-09-19 04:37:29 -0300263 if (memory == V4L2_MEMORY_MMAP)
264 __setup_offsets(q, buffer);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300265
266 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300267 buffer, num_planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300268
269 return buffer;
270}
271
272/**
273 * __vb2_free_mem() - release all video buffer memory for a given queue
274 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300275static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300276{
277 unsigned int buffer;
278 struct vb2_buffer *vb;
279
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300280 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
281 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300282 vb = q->bufs[buffer];
283 if (!vb)
284 continue;
285
286 /* Free MMAP buffers or release USERPTR buffers */
287 if (q->memory == V4L2_MEMORY_MMAP)
288 __vb2_buf_mem_free(vb);
Sumit Semwalc5384042012-06-14 10:37:37 -0300289 else if (q->memory == V4L2_MEMORY_DMABUF)
290 __vb2_buf_dmabuf_put(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300291 else
292 __vb2_buf_userptr_put(vb);
293 }
294}
295
296/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300297 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
298 * related information, if no buffers are left return the queue to an
299 * uninitialized state. Might be called even if the queue has already been freed.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300300 */
Hans Verkuil63faabf2013-12-13 13:13:40 -0300301static int __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300302{
303 unsigned int buffer;
304
Hans Verkuil63faabf2013-12-13 13:13:40 -0300305 /*
306 * Sanity check: when preparing a buffer the queue lock is released for
307 * a short while (see __buf_prepare for the details), which would allow
308 * a race with a reqbufs which can call this function. Removing the
309 * buffers from underneath __buf_prepare is obviously a bad idea, so we
310 * check if any of the buffers is in the state PREPARING, and if so we
311 * just return -EAGAIN.
312 */
313 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
314 ++buffer) {
315 if (q->bufs[buffer] == NULL)
316 continue;
317 if (q->bufs[buffer]->state == VB2_BUF_STATE_PREPARING) {
318 dprintk(1, "reqbufs: preparing buffers, cannot free\n");
319 return -EAGAIN;
320 }
321 }
322
Pawel Osciake23ccc02010-10-11 10:56:41 -0300323 /* Call driver-provided cleanup function for each buffer, if provided */
324 if (q->ops->buf_cleanup) {
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300325 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
326 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300327 if (NULL == q->bufs[buffer])
328 continue;
329 q->ops->buf_cleanup(q->bufs[buffer]);
330 }
331 }
332
333 /* Release video buffer memory */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300334 __vb2_free_mem(q, buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300335
336 /* Free videobuf buffers */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300337 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
338 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300339 kfree(q->bufs[buffer]);
340 q->bufs[buffer] = NULL;
341 }
342
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300343 q->num_buffers -= buffers;
344 if (!q->num_buffers)
345 q->memory = 0;
Marek Szyprowskibd50d992011-10-25 03:07:59 -0300346 INIT_LIST_HEAD(&q->queued_list);
Hans Verkuil63faabf2013-12-13 13:13:40 -0300347 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300348}
349
350/**
351 * __verify_planes_array() - verify that the planes array passed in struct
352 * v4l2_buffer from userspace can be safely used
353 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300354static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300355{
Hans Verkuil32a77262012-09-28 06:12:53 -0300356 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
357 return 0;
358
Pawel Osciake23ccc02010-10-11 10:56:41 -0300359 /* Is memory for copying plane information present? */
360 if (NULL == b->m.planes) {
361 dprintk(1, "Multi-planar buffer passed but "
362 "planes array not provided\n");
363 return -EINVAL;
364 }
365
366 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
367 dprintk(1, "Incorrect planes array length, "
368 "expected %d, got %d\n", vb->num_planes, b->length);
369 return -EINVAL;
370 }
371
372 return 0;
373}
374
375/**
Laurent Pinchart8023ed02012-07-10 10:41:40 -0300376 * __verify_length() - Verify that the bytesused value for each plane fits in
377 * the plane length and that the data offset doesn't exceed the bytesused value.
378 */
379static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
380{
381 unsigned int length;
382 unsigned int plane;
383
384 if (!V4L2_TYPE_IS_OUTPUT(b->type))
385 return 0;
386
387 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
388 for (plane = 0; plane < vb->num_planes; ++plane) {
389 length = (b->memory == V4L2_MEMORY_USERPTR)
390 ? b->m.planes[plane].length
391 : vb->v4l2_planes[plane].length;
392
393 if (b->m.planes[plane].bytesused > length)
394 return -EINVAL;
Sylwester Nawrocki3c5c23c2013-08-26 11:47:09 -0300395
396 if (b->m.planes[plane].data_offset > 0 &&
397 b->m.planes[plane].data_offset >=
Laurent Pinchart8023ed02012-07-10 10:41:40 -0300398 b->m.planes[plane].bytesused)
399 return -EINVAL;
400 }
401 } else {
402 length = (b->memory == V4L2_MEMORY_USERPTR)
403 ? b->length : vb->v4l2_planes[0].length;
404
405 if (b->bytesused > length)
406 return -EINVAL;
407 }
408
409 return 0;
410}
411
412/**
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300413 * __buffer_in_use() - return true if the buffer is in use and
414 * the queue cannot be freed (by the means of REQBUFS(0)) call
415 */
416static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
417{
418 unsigned int plane;
419 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski2c2dd6ac2011-10-12 13:09:53 -0300420 void *mem_priv = vb->planes[plane].mem_priv;
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300421 /*
422 * If num_users() has not been provided, call_memop
423 * will return 0, apparently nobody cares about this
424 * case anyway. If num_users() returns more than 1,
425 * we are not the only user of the plane's memory.
426 */
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300427 if (mem_priv && call_memop(q, num_users, mem_priv) > 1)
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300428 return true;
429 }
430 return false;
431}
432
433/**
434 * __buffers_in_use() - return true if any buffers on the queue are in use and
435 * the queue cannot be freed (by the means of REQBUFS(0)) call
436 */
437static bool __buffers_in_use(struct vb2_queue *q)
438{
439 unsigned int buffer;
440 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
441 if (__buffer_in_use(q, q->bufs[buffer]))
442 return true;
443 }
444 return false;
445}
446
447/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300448 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
449 * returned to userspace
450 */
Hans Verkuil32a77262012-09-28 06:12:53 -0300451static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300452{
453 struct vb2_queue *q = vb->vb2_queue;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300454
Sakari Ailus2b719d72012-05-02 09:40:03 -0300455 /* Copy back data such as timestamp, flags, etc. */
Pawel Osciake23ccc02010-10-11 10:56:41 -0300456 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
Sakari Ailus2b719d72012-05-02 09:40:03 -0300457 b->reserved2 = vb->v4l2_buf.reserved2;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300458 b->reserved = vb->v4l2_buf.reserved;
459
460 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300461 /*
462 * Fill in plane-related data if userspace provided an array
Hans Verkuil32a77262012-09-28 06:12:53 -0300463 * for it. The caller has already verified memory and size.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300464 */
Hans Verkuil3c0b6062012-09-28 06:24:18 -0300465 b->length = vb->num_planes;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300466 memcpy(b->m.planes, vb->v4l2_planes,
467 b->length * sizeof(struct v4l2_plane));
468 } else {
469 /*
470 * We use length and offset in v4l2_planes array even for
471 * single-planar buffers, but userspace does not.
472 */
473 b->length = vb->v4l2_planes[0].length;
474 b->bytesused = vb->v4l2_planes[0].bytesused;
475 if (q->memory == V4L2_MEMORY_MMAP)
476 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
477 else if (q->memory == V4L2_MEMORY_USERPTR)
478 b->m.userptr = vb->v4l2_planes[0].m.userptr;
Sumit Semwalc5384042012-06-14 10:37:37 -0300479 else if (q->memory == V4L2_MEMORY_DMABUF)
480 b->m.fd = vb->v4l2_planes[0].m.fd;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300481 }
482
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300483 /*
484 * Clear any buffer state related flags.
485 */
Sakari Ailus1b18e7a2012-10-22 17:10:16 -0300486 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
Kamil Debski6aa69f92013-01-25 06:29:57 -0300487 b->flags |= q->timestamp_type;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300488
489 switch (vb->state) {
490 case VB2_BUF_STATE_QUEUED:
491 case VB2_BUF_STATE_ACTIVE:
492 b->flags |= V4L2_BUF_FLAG_QUEUED;
493 break;
494 case VB2_BUF_STATE_ERROR:
495 b->flags |= V4L2_BUF_FLAG_ERROR;
496 /* fall through */
497 case VB2_BUF_STATE_DONE:
498 b->flags |= V4L2_BUF_FLAG_DONE;
499 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300500 case VB2_BUF_STATE_PREPARED:
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300501 b->flags |= V4L2_BUF_FLAG_PREPARED;
502 break;
Hans Verkuilb18a8ff2013-12-13 13:13:38 -0300503 case VB2_BUF_STATE_PREPARING:
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300504 case VB2_BUF_STATE_DEQUEUED:
Pawel Osciake23ccc02010-10-11 10:56:41 -0300505 /* nothing */
506 break;
507 }
508
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300509 if (__buffer_in_use(q, vb))
Pawel Osciake23ccc02010-10-11 10:56:41 -0300510 b->flags |= V4L2_BUF_FLAG_MAPPED;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300511}
512
513/**
514 * vb2_querybuf() - query video buffer information
515 * @q: videobuf queue
516 * @b: buffer struct passed from userspace to vidioc_querybuf handler
517 * in driver
518 *
519 * Should be called from vidioc_querybuf ioctl handler in driver.
520 * This function will verify the passed v4l2_buffer structure and fill the
521 * relevant information for the userspace.
522 *
523 * The return values from this function are intended to be directly returned
524 * from vidioc_querybuf handler in driver.
525 */
526int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
527{
528 struct vb2_buffer *vb;
Hans Verkuil32a77262012-09-28 06:12:53 -0300529 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300530
531 if (b->type != q->type) {
532 dprintk(1, "querybuf: wrong buffer type\n");
533 return -EINVAL;
534 }
535
536 if (b->index >= q->num_buffers) {
537 dprintk(1, "querybuf: buffer index out of range\n");
538 return -EINVAL;
539 }
540 vb = q->bufs[b->index];
Hans Verkuil32a77262012-09-28 06:12:53 -0300541 ret = __verify_planes_array(vb, b);
542 if (!ret)
543 __fill_v4l2_buffer(vb, b);
544 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300545}
546EXPORT_SYMBOL(vb2_querybuf);
547
548/**
549 * __verify_userptr_ops() - verify that all memory operations required for
550 * USERPTR queue type have been provided
551 */
552static int __verify_userptr_ops(struct vb2_queue *q)
553{
554 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
555 !q->mem_ops->put_userptr)
556 return -EINVAL;
557
558 return 0;
559}
560
561/**
562 * __verify_mmap_ops() - verify that all memory operations required for
563 * MMAP queue type have been provided
564 */
565static int __verify_mmap_ops(struct vb2_queue *q)
566{
567 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
568 !q->mem_ops->put || !q->mem_ops->mmap)
569 return -EINVAL;
570
571 return 0;
572}
573
574/**
Sumit Semwalc5384042012-06-14 10:37:37 -0300575 * __verify_dmabuf_ops() - verify that all memory operations required for
576 * DMABUF queue type have been provided
577 */
578static int __verify_dmabuf_ops(struct vb2_queue *q)
579{
580 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
581 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
582 !q->mem_ops->unmap_dmabuf)
583 return -EINVAL;
584
585 return 0;
586}
587
588/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300589 * __verify_memory_type() - Check whether the memory type and buffer type
590 * passed to a buffer operation are compatible with the queue.
591 */
592static int __verify_memory_type(struct vb2_queue *q,
593 enum v4l2_memory memory, enum v4l2_buf_type type)
594{
Sumit Semwalc5384042012-06-14 10:37:37 -0300595 if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR &&
596 memory != V4L2_MEMORY_DMABUF) {
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300597 dprintk(1, "reqbufs: unsupported memory type\n");
598 return -EINVAL;
599 }
600
601 if (type != q->type) {
602 dprintk(1, "reqbufs: requested type is incorrect\n");
603 return -EINVAL;
604 }
605
606 /*
607 * Make sure all the required memory ops for given memory type
608 * are available.
609 */
610 if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
611 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
612 return -EINVAL;
613 }
614
615 if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
616 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
617 return -EINVAL;
618 }
619
Sumit Semwalc5384042012-06-14 10:37:37 -0300620 if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
621 dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
622 return -EINVAL;
623 }
624
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300625 /*
626 * Place the busy tests at the end: -EBUSY can be ignored when
627 * create_bufs is called with count == 0, but count == 0 should still
628 * do the memory and type validation.
629 */
630 if (q->fileio) {
631 dprintk(1, "reqbufs: file io in progress\n");
632 return -EBUSY;
633 }
634 return 0;
635}
636
637/**
638 * __reqbufs() - Initiate streaming
Pawel Osciake23ccc02010-10-11 10:56:41 -0300639 * @q: videobuf2 queue
640 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
641 *
642 * Should be called from vidioc_reqbufs ioctl handler of a driver.
643 * This function:
644 * 1) verifies streaming parameters passed from the userspace,
645 * 2) sets up the queue,
646 * 3) negotiates number of buffers and planes per buffer with the driver
647 * to be used during streaming,
648 * 4) allocates internal buffer structures (struct vb2_buffer), according to
649 * the agreed parameters,
650 * 5) for MMAP memory type, allocates actual video memory, using the
651 * memory handling/allocation routines provided during queue initialization
652 *
653 * If req->count is 0, all the memory will be freed instead.
654 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
655 * and the queue is not busy, memory will be reallocated.
656 *
657 * The return values from this function are intended to be directly returned
658 * from vidioc_reqbufs handler in driver.
659 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300660static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300661{
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300662 unsigned int num_buffers, allocated_buffers, num_planes = 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300663 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300664
665 if (q->streaming) {
666 dprintk(1, "reqbufs: streaming active\n");
667 return -EBUSY;
668 }
669
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300670 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300671 /*
672 * We already have buffers allocated, so first check if they
673 * are not in use and can be freed.
674 */
675 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
676 dprintk(1, "reqbufs: memory in use, cannot free\n");
677 return -EBUSY;
678 }
679
Hans Verkuil63faabf2013-12-13 13:13:40 -0300680 ret = __vb2_queue_free(q, q->num_buffers);
681 if (ret)
682 return ret;
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300683
684 /*
685 * In case of REQBUFS(0) return immediately without calling
686 * driver's queue_setup() callback and allocating resources.
687 */
688 if (req->count == 0)
689 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300690 }
691
692 /*
693 * Make sure the requested values and current defaults are sane.
694 */
695 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300696 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
Pawel Osciake23ccc02010-10-11 10:56:41 -0300697 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
Marek Szyprowski13b14092011-04-14 07:17:44 -0300698 q->memory = req->memory;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300699
700 /*
701 * Ask the driver how many buffers and planes per buffer it requires.
702 * Driver also sets the size and allocator context for each plane.
703 */
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300704 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300705 q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300706 if (ret)
707 return ret;
708
709 /* Finally, allocate buffers and video memory */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300710 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
Marek Szyprowski66072d42011-06-28 08:29:02 -0300711 if (ret == 0) {
712 dprintk(1, "Memory allocation failed\n");
713 return -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300714 }
715
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300716 allocated_buffers = ret;
717
Pawel Osciake23ccc02010-10-11 10:56:41 -0300718 /*
719 * Check if driver can handle the allocated number of buffers.
720 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300721 if (allocated_buffers < num_buffers) {
722 num_buffers = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300723
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300724 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
725 &num_planes, q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300726
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300727 if (!ret && allocated_buffers < num_buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300728 ret = -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300729
730 /*
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300731 * Either the driver has accepted a smaller number of buffers,
732 * or .queue_setup() returned an error
Pawel Osciake23ccc02010-10-11 10:56:41 -0300733 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300734 }
735
736 q->num_buffers = allocated_buffers;
737
738 if (ret < 0) {
739 __vb2_queue_free(q, allocated_buffers);
740 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300741 }
742
Pawel Osciake23ccc02010-10-11 10:56:41 -0300743 /*
744 * Return the number of successfully allocated buffers
745 * to the userspace.
746 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300747 req->count = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300748
749 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300750}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300751
752/**
753 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
754 * type values.
755 * @q: videobuf2 queue
756 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
757 */
758int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
759{
760 int ret = __verify_memory_type(q, req->memory, req->type);
761
762 return ret ? ret : __reqbufs(q, req);
763}
Pawel Osciake23ccc02010-10-11 10:56:41 -0300764EXPORT_SYMBOL_GPL(vb2_reqbufs);
765
766/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300767 * __create_bufs() - Allocate buffers and any required auxiliary structs
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300768 * @q: videobuf2 queue
769 * @create: creation parameters, passed from userspace to vidioc_create_bufs
770 * handler in driver
771 *
772 * Should be called from vidioc_create_bufs ioctl handler of a driver.
773 * This function:
774 * 1) verifies parameter sanity
775 * 2) calls the .queue_setup() queue operation
776 * 3) performs any necessary memory allocations
777 *
778 * The return values from this function are intended to be directly returned
779 * from vidioc_create_bufs handler in driver.
780 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300781static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300782{
783 unsigned int num_planes = 0, num_buffers, allocated_buffers;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300784 int ret;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300785
786 if (q->num_buffers == VIDEO_MAX_FRAME) {
787 dprintk(1, "%s(): maximum number of buffers already allocated\n",
788 __func__);
789 return -ENOBUFS;
790 }
791
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300792 if (!q->num_buffers) {
793 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
794 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
795 q->memory = create->memory;
796 }
797
798 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
799
800 /*
801 * Ask the driver, whether the requested number of buffers, planes per
802 * buffer and their sizes are acceptable
803 */
804 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
805 &num_planes, q->plane_sizes, q->alloc_ctx);
806 if (ret)
807 return ret;
808
809 /* Finally, allocate buffers and video memory */
810 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
811 num_planes);
Hans Verkuilf05393d22012-06-22 05:44:14 -0300812 if (ret == 0) {
813 dprintk(1, "Memory allocation failed\n");
814 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300815 }
816
817 allocated_buffers = ret;
818
819 /*
820 * Check if driver can handle the so far allocated number of buffers.
821 */
822 if (ret < num_buffers) {
823 num_buffers = ret;
824
825 /*
826 * q->num_buffers contains the total number of buffers, that the
827 * queue driver has set up
828 */
829 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
830 &num_planes, q->plane_sizes, q->alloc_ctx);
831
832 if (!ret && allocated_buffers < num_buffers)
833 ret = -ENOMEM;
834
835 /*
836 * Either the driver has accepted a smaller number of buffers,
837 * or .queue_setup() returned an error
838 */
839 }
840
841 q->num_buffers += allocated_buffers;
842
843 if (ret < 0) {
844 __vb2_queue_free(q, allocated_buffers);
Hans Verkuilf05393d22012-06-22 05:44:14 -0300845 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300846 }
847
848 /*
849 * Return the number of successfully allocated buffers
850 * to the userspace.
851 */
852 create->count = allocated_buffers;
853
854 return 0;
855}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300856
857/**
Nicolas THERY53aa3b12012-07-20 09:25:37 -0300858 * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
859 * memory and type values.
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300860 * @q: videobuf2 queue
861 * @create: creation parameters, passed from userspace to vidioc_create_bufs
862 * handler in driver
863 */
864int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
865{
866 int ret = __verify_memory_type(q, create->memory, create->format.type);
867
868 create->index = q->num_buffers;
Hans Verkuilf05393d22012-06-22 05:44:14 -0300869 if (create->count == 0)
870 return ret != -EBUSY ? ret : 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300871 return ret ? ret : __create_bufs(q, create);
872}
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300873EXPORT_SYMBOL_GPL(vb2_create_bufs);
874
875/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300876 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
877 * @vb: vb2_buffer to which the plane in question belongs to
878 * @plane_no: plane number for which the address is to be returned
879 *
880 * This function returns a kernel virtual address of a given plane if
881 * such a mapping exist, NULL otherwise.
882 */
883void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
884{
885 struct vb2_queue *q = vb->vb2_queue;
886
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300887 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300888 return NULL;
889
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300890 return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300891
892}
893EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
894
895/**
896 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
897 * @vb: vb2_buffer to which the plane in question belongs to
898 * @plane_no: plane number for which the cookie is to be returned
899 *
900 * This function returns an allocator specific cookie for a given plane if
901 * available, NULL otherwise. The allocator should provide some simple static
902 * inline function, which would convert this cookie to the allocator specific
903 * type that can be used directly by the driver to access the buffer. This can
904 * be for example physical address, pointer to scatter list or IOMMU mapping.
905 */
906void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
907{
908 struct vb2_queue *q = vb->vb2_queue;
909
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300910 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300911 return NULL;
912
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300913 return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300914}
915EXPORT_SYMBOL_GPL(vb2_plane_cookie);
916
917/**
918 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
919 * @vb: vb2_buffer returned from the driver
920 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
921 * or VB2_BUF_STATE_ERROR if the operation finished with an error
922 *
923 * This function should be called by the driver after a hardware operation on
924 * a buffer is finished and the buffer may be returned to userspace. The driver
925 * cannot use this buffer anymore until it is queued back to it by videobuf
926 * by the means of buf_queue callback. Only buffers previously queued to the
927 * driver by buf_queue can be passed to this function.
928 */
929void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
930{
931 struct vb2_queue *q = vb->vb2_queue;
932 unsigned long flags;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -0300933 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300934
935 if (vb->state != VB2_BUF_STATE_ACTIVE)
936 return;
937
938 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
939 return;
940
941 dprintk(4, "Done processing on buffer %d, state: %d\n",
Tushar Behera9b6f5dc2012-11-12 04:01:29 -0300942 vb->v4l2_buf.index, state);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300943
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -0300944 /* sync buffers */
945 for (plane = 0; plane < vb->num_planes; ++plane)
946 call_memop(q, finish, vb->planes[plane].mem_priv);
947
Pawel Osciake23ccc02010-10-11 10:56:41 -0300948 /* Add the buffer to the done buffers list */
949 spin_lock_irqsave(&q->done_lock, flags);
950 vb->state = state;
951 list_add_tail(&vb->done_entry, &q->done_list);
952 atomic_dec(&q->queued_count);
953 spin_unlock_irqrestore(&q->done_lock, flags);
954
955 /* Inform any processes that may be waiting for buffers */
956 wake_up(&q->done_wq);
957}
958EXPORT_SYMBOL_GPL(vb2_buffer_done);
959
960/**
Hans Verkuil32a77262012-09-28 06:12:53 -0300961 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
962 * v4l2_buffer by the userspace. The caller has already verified that struct
963 * v4l2_buffer has a valid number of planes.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300964 */
Hans Verkuil32a77262012-09-28 06:12:53 -0300965static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
Pawel Osciake23ccc02010-10-11 10:56:41 -0300966 struct v4l2_plane *v4l2_planes)
967{
968 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300969
970 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300971 /* Fill in driver-provided information for OUTPUT types */
972 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
973 /*
974 * Will have to go up to b->length when API starts
975 * accepting variable number of planes.
976 */
977 for (plane = 0; plane < vb->num_planes; ++plane) {
978 v4l2_planes[plane].bytesused =
979 b->m.planes[plane].bytesused;
980 v4l2_planes[plane].data_offset =
981 b->m.planes[plane].data_offset;
982 }
983 }
984
985 if (b->memory == V4L2_MEMORY_USERPTR) {
986 for (plane = 0; plane < vb->num_planes; ++plane) {
987 v4l2_planes[plane].m.userptr =
988 b->m.planes[plane].m.userptr;
989 v4l2_planes[plane].length =
990 b->m.planes[plane].length;
991 }
992 }
Sumit Semwalc5384042012-06-14 10:37:37 -0300993 if (b->memory == V4L2_MEMORY_DMABUF) {
994 for (plane = 0; plane < vb->num_planes; ++plane) {
995 v4l2_planes[plane].m.fd =
996 b->m.planes[plane].m.fd;
997 v4l2_planes[plane].length =
998 b->m.planes[plane].length;
999 v4l2_planes[plane].data_offset =
1000 b->m.planes[plane].data_offset;
1001 }
1002 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001003 } else {
1004 /*
1005 * Single-planar buffers do not use planes array,
1006 * so fill in relevant v4l2_buffer struct fields instead.
1007 * In videobuf we use our internal V4l2_planes struct for
1008 * single-planar buffers as well, for simplicity.
1009 */
Laurent Pinchartac706bf2012-12-17 07:38:16 -03001010 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -03001011 v4l2_planes[0].bytesused = b->bytesused;
Laurent Pinchartac706bf2012-12-17 07:38:16 -03001012 v4l2_planes[0].data_offset = 0;
1013 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001014
1015 if (b->memory == V4L2_MEMORY_USERPTR) {
1016 v4l2_planes[0].m.userptr = b->m.userptr;
1017 v4l2_planes[0].length = b->length;
1018 }
Sumit Semwalc5384042012-06-14 10:37:37 -03001019
1020 if (b->memory == V4L2_MEMORY_DMABUF) {
1021 v4l2_planes[0].m.fd = b->m.fd;
1022 v4l2_planes[0].length = b->length;
1023 v4l2_planes[0].data_offset = 0;
1024 }
1025
Pawel Osciake23ccc02010-10-11 10:56:41 -03001026 }
1027
1028 vb->v4l2_buf.field = b->field;
1029 vb->v4l2_buf.timestamp = b->timestamp;
Sakari Ailus1b18e7a2012-10-22 17:10:16 -03001030 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001031}
1032
1033/**
1034 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
1035 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001036static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001037{
1038 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1039 struct vb2_queue *q = vb->vb2_queue;
1040 void *mem_priv;
1041 unsigned int plane;
1042 int ret;
1043 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1044
Hans Verkuil32a77262012-09-28 06:12:53 -03001045 /* Copy relevant information provided by the userspace */
1046 __fill_vb2_buffer(vb, b, planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001047
1048 for (plane = 0; plane < vb->num_planes; ++plane) {
1049 /* Skip the plane if already verified */
Marek Szyprowskif0b7c7f2011-11-16 15:09:40 -03001050 if (vb->v4l2_planes[plane].m.userptr &&
1051 vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
Pawel Osciake23ccc02010-10-11 10:56:41 -03001052 && vb->v4l2_planes[plane].length == planes[plane].length)
1053 continue;
1054
1055 dprintk(3, "qbuf: userspace address for plane %d changed, "
1056 "reacquiring memory\n", plane);
1057
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001058 /* Check if the provided plane buffer is large enough */
1059 if (planes[plane].length < q->plane_sizes[plane]) {
Seung-Woo Kim2484a7e2013-08-20 04:48:06 -03001060 dprintk(1, "qbuf: provided buffer size %u is less than "
1061 "setup size %u for plane %d\n",
1062 planes[plane].length,
1063 q->plane_sizes[plane], plane);
Marek Szyprowski4c2625d2011-10-03 03:21:45 -03001064 ret = -EINVAL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001065 goto err;
1066 }
1067
Pawel Osciake23ccc02010-10-11 10:56:41 -03001068 /* Release previously acquired memory if present */
1069 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -03001070 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001071
1072 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001073 vb->v4l2_planes[plane].m.userptr = 0;
1074 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001075
1076 /* Acquire each plane's memory */
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001077 mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
1078 planes[plane].m.userptr,
1079 planes[plane].length, write);
1080 if (IS_ERR_OR_NULL(mem_priv)) {
1081 dprintk(1, "qbuf: failed acquiring userspace "
Pawel Osciake23ccc02010-10-11 10:56:41 -03001082 "memory for plane %d\n", plane);
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001083 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
1084 goto err;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001085 }
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001086 vb->planes[plane].mem_priv = mem_priv;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001087 }
1088
1089 /*
1090 * Call driver-specific initialization on the newly acquired buffer,
1091 * if provided.
1092 */
1093 ret = call_qop(q, buf_init, vb);
1094 if (ret) {
1095 dprintk(1, "qbuf: buffer initialization failed\n");
1096 goto err;
1097 }
1098
1099 /*
1100 * Now that everything is in order, copy relevant information
1101 * provided by userspace.
1102 */
1103 for (plane = 0; plane < vb->num_planes; ++plane)
1104 vb->v4l2_planes[plane] = planes[plane];
1105
1106 return 0;
1107err:
1108 /* In case of errors, release planes that were already acquired */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001109 for (plane = 0; plane < vb->num_planes; ++plane) {
1110 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -03001111 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001112 vb->planes[plane].mem_priv = NULL;
1113 vb->v4l2_planes[plane].m.userptr = 0;
1114 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001115 }
1116
1117 return ret;
1118}
1119
1120/**
1121 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1122 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001123static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001124{
Hans Verkuil32a77262012-09-28 06:12:53 -03001125 __fill_vb2_buffer(vb, b, vb->v4l2_planes);
1126 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001127}
1128
1129/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001130 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1131 */
1132static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1133{
1134 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1135 struct vb2_queue *q = vb->vb2_queue;
1136 void *mem_priv;
1137 unsigned int plane;
1138 int ret;
1139 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1140
1141 /* Verify and copy relevant information provided by the userspace */
1142 __fill_vb2_buffer(vb, b, planes);
1143
1144 for (plane = 0; plane < vb->num_planes; ++plane) {
1145 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1146
1147 if (IS_ERR_OR_NULL(dbuf)) {
1148 dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
1149 plane);
1150 ret = -EINVAL;
1151 goto err;
1152 }
1153
1154 /* use DMABUF size if length is not provided */
1155 if (planes[plane].length == 0)
1156 planes[plane].length = dbuf->size;
1157
1158 if (planes[plane].length < planes[plane].data_offset +
1159 q->plane_sizes[plane]) {
Seung-Woo Kim77c07822013-11-29 04:50:29 -03001160 dprintk(1, "qbuf: invalid dmabuf length for plane %d\n",
1161 plane);
Sumit Semwalc5384042012-06-14 10:37:37 -03001162 ret = -EINVAL;
1163 goto err;
1164 }
1165
1166 /* Skip the plane if already verified */
1167 if (dbuf == vb->planes[plane].dbuf &&
1168 vb->v4l2_planes[plane].length == planes[plane].length) {
1169 dma_buf_put(dbuf);
1170 continue;
1171 }
1172
1173 dprintk(1, "qbuf: buffer for plane %d changed\n", plane);
1174
1175 /* Release previously acquired memory if present */
1176 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
1177 memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
1178
1179 /* Acquire each plane's memory */
1180 mem_priv = call_memop(q, attach_dmabuf, q->alloc_ctx[plane],
1181 dbuf, planes[plane].length, write);
1182 if (IS_ERR(mem_priv)) {
1183 dprintk(1, "qbuf: failed to attach dmabuf\n");
1184 ret = PTR_ERR(mem_priv);
1185 dma_buf_put(dbuf);
1186 goto err;
1187 }
1188
1189 vb->planes[plane].dbuf = dbuf;
1190 vb->planes[plane].mem_priv = mem_priv;
1191 }
1192
1193 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1194 * really we want to do this just before the DMA, not while queueing
1195 * the buffer(s)..
1196 */
1197 for (plane = 0; plane < vb->num_planes; ++plane) {
1198 ret = call_memop(q, map_dmabuf, vb->planes[plane].mem_priv);
1199 if (ret) {
1200 dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
1201 plane);
1202 goto err;
1203 }
1204 vb->planes[plane].dbuf_mapped = 1;
1205 }
1206
1207 /*
1208 * Call driver-specific initialization on the newly acquired buffer,
1209 * if provided.
1210 */
1211 ret = call_qop(q, buf_init, vb);
1212 if (ret) {
1213 dprintk(1, "qbuf: buffer initialization failed\n");
1214 goto err;
1215 }
1216
1217 /*
1218 * Now that everything is in order, copy relevant information
1219 * provided by userspace.
1220 */
1221 for (plane = 0; plane < vb->num_planes; ++plane)
1222 vb->v4l2_planes[plane] = planes[plane];
1223
1224 return 0;
1225err:
1226 /* In case of errors, release planes that were already acquired */
1227 __vb2_buf_dmabuf_put(vb);
1228
1229 return ret;
1230}
1231
1232/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001233 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1234 */
1235static void __enqueue_in_driver(struct vb2_buffer *vb)
1236{
1237 struct vb2_queue *q = vb->vb2_queue;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001238 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001239
1240 vb->state = VB2_BUF_STATE_ACTIVE;
1241 atomic_inc(&q->queued_count);
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001242
1243 /* sync buffers */
1244 for (plane = 0; plane < vb->num_planes; ++plane)
1245 call_memop(q, prepare, vb->planes[plane].mem_priv);
1246
Pawel Osciake23ccc02010-10-11 10:56:41 -03001247 q->ops->buf_queue(vb);
1248}
1249
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001250static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001251{
1252 struct vb2_queue *q = vb->vb2_queue;
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001253 struct rw_semaphore *mmap_sem;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001254 int ret;
1255
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001256 ret = __verify_length(vb, b);
Sylwester Nawrocki3a9621b2013-08-26 11:47:53 -03001257 if (ret < 0) {
1258 dprintk(1, "%s(): plane parameters verification failed: %d\n",
1259 __func__, ret);
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001260 return ret;
Sylwester Nawrocki3a9621b2013-08-26 11:47:53 -03001261 }
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001262
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001263 vb->state = VB2_BUF_STATE_PREPARING;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001264 switch (q->memory) {
1265 case V4L2_MEMORY_MMAP:
1266 ret = __qbuf_mmap(vb, b);
1267 break;
1268 case V4L2_MEMORY_USERPTR:
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001269 /*
Mauro Carvalho Chehabf103b5d2014-01-07 07:03:09 -02001270 * In case of user pointer buffers vb2 allocators need to get
1271 * direct access to userspace pages. This requires getting
1272 * the mmap semaphore for read access in the current process
1273 * structure. The same semaphore is taken before calling mmap
1274 * operation, while both qbuf/prepare_buf and mmap are called
1275 * by the driver or v4l2 core with the driver's lock held.
1276 * To avoid an AB-BA deadlock (mmap_sem then driver's lock in
1277 * mmap and driver's lock then mmap_sem in qbuf/prepare_buf),
1278 * the videobuf2 core releases the driver's lock, takes
1279 * mmap_sem and then takes the driver's lock again.
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001280 */
1281 mmap_sem = &current->mm->mmap_sem;
1282 call_qop(q, wait_prepare, q);
1283 down_read(mmap_sem);
1284 call_qop(q, wait_finish, q);
1285
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001286 ret = __qbuf_userptr(vb, b);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001287
1288 up_read(mmap_sem);
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001289 break;
Sumit Semwalc5384042012-06-14 10:37:37 -03001290 case V4L2_MEMORY_DMABUF:
1291 ret = __qbuf_dmabuf(vb, b);
1292 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001293 default:
1294 WARN(1, "Invalid queue type\n");
1295 ret = -EINVAL;
1296 }
1297
1298 if (!ret)
1299 ret = call_qop(q, buf_prepare, vb);
1300 if (ret)
1301 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001302 vb->state = ret ? VB2_BUF_STATE_DEQUEUED : VB2_BUF_STATE_PREPARED;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001303
1304 return ret;
1305}
1306
Laurent Pinchart012043b2013-08-09 08:11:26 -03001307static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
Hans Verkuil41381112013-12-13 13:13:39 -03001308 const char *opname)
Laurent Pinchart012043b2013-08-09 08:11:26 -03001309{
Laurent Pinchart012043b2013-08-09 08:11:26 -03001310 if (q->fileio) {
1311 dprintk(1, "%s(): file io in progress\n", opname);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001312 return -EBUSY;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001313 }
1314
1315 if (b->type != q->type) {
1316 dprintk(1, "%s(): invalid buffer type\n", opname);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001317 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001318 }
1319
1320 if (b->index >= q->num_buffers) {
1321 dprintk(1, "%s(): buffer index out of range\n", opname);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001322 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001323 }
1324
Hans Verkuil41381112013-12-13 13:13:39 -03001325 if (q->bufs[b->index] == NULL) {
Laurent Pinchart012043b2013-08-09 08:11:26 -03001326 /* Should never happen */
1327 dprintk(1, "%s(): buffer is NULL\n", opname);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001328 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001329 }
1330
1331 if (b->memory != q->memory) {
1332 dprintk(1, "%s(): invalid memory type\n", opname);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001333 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001334 }
1335
Hans Verkuil41381112013-12-13 13:13:39 -03001336 return __verify_planes_array(q->bufs[b->index], b);
Laurent Pinchart012043b2013-08-09 08:11:26 -03001337}
1338
Pawel Osciake23ccc02010-10-11 10:56:41 -03001339/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001340 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1341 * @q: videobuf2 queue
1342 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1343 * handler in driver
1344 *
1345 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1346 * This function:
1347 * 1) verifies the passed buffer,
1348 * 2) calls buf_prepare callback in the driver (if provided), in which
1349 * driver-specific buffer initialization can be performed,
1350 *
1351 * The return values from this function are intended to be directly returned
1352 * from vidioc_prepare_buf handler in driver.
1353 */
1354int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1355{
Hans Verkuil41381112013-12-13 13:13:39 -03001356 int ret = vb2_queue_or_prepare_buf(q, b, "prepare_buf");
1357 struct vb2_buffer *vb;
1358
1359 if (ret)
1360 return ret;
1361
1362 vb = q->bufs[b->index];
1363 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1364 dprintk(1, "%s(): invalid buffer state %d\n", __func__,
1365 vb->state);
1366 return -EINVAL;
1367 }
1368
1369 ret = __buf_prepare(vb, b);
1370 if (!ret) {
1371 /* Fill buffer information for the userspace */
1372 __fill_v4l2_buffer(vb, b);
1373
1374 dprintk(1, "%s() of buffer %d succeeded\n", __func__, vb->v4l2_buf.index);
1375 }
1376 return ret;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001377}
1378EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1379
Hans Verkuil41381112013-12-13 13:13:39 -03001380/**
1381 * vb2_qbuf() - Queue a buffer from userspace
1382 * @q: videobuf2 queue
1383 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1384 * in driver
1385 *
1386 * Should be called from vidioc_qbuf ioctl handler of a driver.
1387 * This function:
1388 * 1) verifies the passed buffer,
1389 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1390 * which driver-specific buffer initialization can be performed,
1391 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1392 * callback for processing.
1393 *
1394 * The return values from this function are intended to be directly returned
1395 * from vidioc_qbuf handler in driver.
1396 */
1397int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
Laurent Pinchart012043b2013-08-09 08:11:26 -03001398{
Hans Verkuil41381112013-12-13 13:13:39 -03001399 int ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
1400 struct vb2_buffer *vb;
1401
1402 if (ret)
1403 return ret;
1404
1405 vb = q->bufs[b->index];
1406 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1407 dprintk(1, "%s(): invalid buffer state %d\n", __func__,
1408 vb->state);
1409 return -EINVAL;
1410 }
Laurent Pinchart012043b2013-08-09 08:11:26 -03001411
1412 switch (vb->state) {
1413 case VB2_BUF_STATE_DEQUEUED:
1414 ret = __buf_prepare(vb, b);
1415 if (ret)
1416 return ret;
Hans Verkuil41381112013-12-13 13:13:39 -03001417 break;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001418 case VB2_BUF_STATE_PREPARED:
1419 break;
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001420 case VB2_BUF_STATE_PREPARING:
1421 dprintk(1, "qbuf: buffer still being prepared\n");
1422 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001423 default:
1424 dprintk(1, "qbuf: buffer already in use\n");
1425 return -EINVAL;
1426 }
1427
1428 /*
1429 * Add to the queued buffers list, a buffer will stay on it until
1430 * dequeued in dqbuf.
1431 */
1432 list_add_tail(&vb->queued_entry, &q->queued_list);
1433 vb->state = VB2_BUF_STATE_QUEUED;
1434
1435 /*
1436 * If already streaming, give the buffer to driver for processing.
1437 * If not, the buffer will be given to driver on next streamon.
1438 */
1439 if (q->streaming)
1440 __enqueue_in_driver(vb);
1441
Hans Verkuil41381112013-12-13 13:13:39 -03001442 /* Fill buffer information for the userspace */
1443 __fill_v4l2_buffer(vb, b);
Laurent Pinchart012043b2013-08-09 08:11:26 -03001444
Hans Verkuil41381112013-12-13 13:13:39 -03001445 dprintk(1, "%s() of buffer %d succeeded\n", __func__, vb->v4l2_buf.index);
1446 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001447}
1448EXPORT_SYMBOL_GPL(vb2_qbuf);
1449
1450/**
1451 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1452 * for dequeuing
1453 *
1454 * Will sleep if required for nonblocking == false.
1455 */
1456static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1457{
1458 /*
1459 * All operations on vb_done_list are performed under done_lock
1460 * spinlock protection. However, buffers may be removed from
1461 * it and returned to userspace only while holding both driver's
1462 * lock and the done_lock spinlock. Thus we can be sure that as
1463 * long as we hold the driver's lock, the list will remain not
1464 * empty if list_empty() check succeeds.
1465 */
1466
1467 for (;;) {
1468 int ret;
1469
1470 if (!q->streaming) {
1471 dprintk(1, "Streaming off, will not wait for buffers\n");
1472 return -EINVAL;
1473 }
1474
1475 if (!list_empty(&q->done_list)) {
1476 /*
1477 * Found a buffer that we were waiting for.
1478 */
1479 break;
1480 }
1481
1482 if (nonblocking) {
1483 dprintk(1, "Nonblocking and no buffers to dequeue, "
1484 "will not wait\n");
1485 return -EAGAIN;
1486 }
1487
1488 /*
1489 * We are streaming and blocking, wait for another buffer to
1490 * become ready or for streamoff. Driver's lock is released to
1491 * allow streamoff or qbuf to be called while waiting.
1492 */
1493 call_qop(q, wait_prepare, q);
1494
1495 /*
1496 * All locks have been released, it is safe to sleep now.
1497 */
1498 dprintk(3, "Will sleep waiting for buffers\n");
1499 ret = wait_event_interruptible(q->done_wq,
1500 !list_empty(&q->done_list) || !q->streaming);
1501
1502 /*
1503 * We need to reevaluate both conditions again after reacquiring
1504 * the locks or return an error if one occurred.
1505 */
1506 call_qop(q, wait_finish, q);
Hans Verkuil32a77262012-09-28 06:12:53 -03001507 if (ret) {
1508 dprintk(1, "Sleep was interrupted\n");
Pawel Osciake23ccc02010-10-11 10:56:41 -03001509 return ret;
Hans Verkuil32a77262012-09-28 06:12:53 -03001510 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001511 }
1512 return 0;
1513}
1514
1515/**
1516 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1517 *
1518 * Will sleep if required for nonblocking == false.
1519 */
1520static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
Hans Verkuil32a77262012-09-28 06:12:53 -03001521 struct v4l2_buffer *b, int nonblocking)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001522{
1523 unsigned long flags;
1524 int ret;
1525
1526 /*
1527 * Wait for at least one buffer to become available on the done_list.
1528 */
1529 ret = __vb2_wait_for_done_vb(q, nonblocking);
1530 if (ret)
1531 return ret;
1532
1533 /*
1534 * Driver's lock has been held since we last verified that done_list
1535 * is not empty, so no need for another list_empty(done_list) check.
1536 */
1537 spin_lock_irqsave(&q->done_lock, flags);
1538 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
Hans Verkuil32a77262012-09-28 06:12:53 -03001539 /*
1540 * Only remove the buffer from done_list if v4l2_buffer can handle all
1541 * the planes.
1542 */
1543 ret = __verify_planes_array(*vb, b);
1544 if (!ret)
1545 list_del(&(*vb)->done_entry);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001546 spin_unlock_irqrestore(&q->done_lock, flags);
1547
Hans Verkuil32a77262012-09-28 06:12:53 -03001548 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001549}
1550
1551/**
1552 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1553 * @q: videobuf2 queue
1554 *
1555 * This function will wait until all buffers that have been given to the driver
1556 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1557 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1558 * taken, for example from stop_streaming() callback.
1559 */
1560int vb2_wait_for_all_buffers(struct vb2_queue *q)
1561{
1562 if (!q->streaming) {
1563 dprintk(1, "Streaming off, will not wait for buffers\n");
1564 return -EINVAL;
1565 }
1566
1567 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1568 return 0;
1569}
1570EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1571
1572/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001573 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1574 */
1575static void __vb2_dqbuf(struct vb2_buffer *vb)
1576{
1577 struct vb2_queue *q = vb->vb2_queue;
1578 unsigned int i;
1579
1580 /* nothing to do if the buffer is already dequeued */
1581 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1582 return;
1583
1584 vb->state = VB2_BUF_STATE_DEQUEUED;
1585
1586 /* unmap DMABUF buffer */
1587 if (q->memory == V4L2_MEMORY_DMABUF)
1588 for (i = 0; i < vb->num_planes; ++i) {
1589 if (!vb->planes[i].dbuf_mapped)
1590 continue;
1591 call_memop(q, unmap_dmabuf, vb->planes[i].mem_priv);
1592 vb->planes[i].dbuf_mapped = 0;
1593 }
1594}
1595
1596/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001597 * vb2_dqbuf() - Dequeue a buffer to the userspace
1598 * @q: videobuf2 queue
1599 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1600 * in driver
1601 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1602 * buffers ready for dequeuing are present. Normally the driver
1603 * would be passing (file->f_flags & O_NONBLOCK) here
1604 *
1605 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1606 * This function:
1607 * 1) verifies the passed buffer,
1608 * 2) calls buf_finish callback in the driver (if provided), in which
1609 * driver can perform any additional operations that may be required before
1610 * returning the buffer to userspace, such as cache sync,
1611 * 3) the buffer struct members are filled with relevant information for
1612 * the userspace.
1613 *
1614 * The return values from this function are intended to be directly returned
1615 * from vidioc_dqbuf handler in driver.
1616 */
1617int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1618{
1619 struct vb2_buffer *vb = NULL;
1620 int ret;
1621
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001622 if (q->fileio) {
1623 dprintk(1, "dqbuf: file io in progress\n");
1624 return -EBUSY;
1625 }
1626
Pawel Osciake23ccc02010-10-11 10:56:41 -03001627 if (b->type != q->type) {
1628 dprintk(1, "dqbuf: invalid buffer type\n");
1629 return -EINVAL;
1630 }
Hans Verkuil32a77262012-09-28 06:12:53 -03001631 ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
1632 if (ret < 0)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001633 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001634
1635 ret = call_qop(q, buf_finish, vb);
1636 if (ret) {
1637 dprintk(1, "dqbuf: buffer finish failed\n");
1638 return ret;
1639 }
1640
1641 switch (vb->state) {
1642 case VB2_BUF_STATE_DONE:
1643 dprintk(3, "dqbuf: Returning done buffer\n");
1644 break;
1645 case VB2_BUF_STATE_ERROR:
1646 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1647 break;
1648 default:
1649 dprintk(1, "dqbuf: Invalid buffer state\n");
1650 return -EINVAL;
1651 }
1652
1653 /* Fill buffer information for the userspace */
1654 __fill_v4l2_buffer(vb, b);
1655 /* Remove from videobuf queue */
1656 list_del(&vb->queued_entry);
Sumit Semwalc5384042012-06-14 10:37:37 -03001657 /* go back to dequeued state */
1658 __vb2_dqbuf(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001659
1660 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1661 vb->v4l2_buf.index, vb->state);
1662
Pawel Osciake23ccc02010-10-11 10:56:41 -03001663 return 0;
1664}
1665EXPORT_SYMBOL_GPL(vb2_dqbuf);
1666
1667/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001668 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1669 *
1670 * Removes all queued buffers from driver's queue and all buffers queued by
1671 * userspace from videobuf's queue. Returns to state after reqbufs.
1672 */
1673static void __vb2_queue_cancel(struct vb2_queue *q)
1674{
1675 unsigned int i;
1676
1677 /*
1678 * Tell driver to stop all transactions and release all queued
1679 * buffers.
1680 */
1681 if (q->streaming)
1682 call_qop(q, stop_streaming, q);
1683 q->streaming = 0;
1684
1685 /*
1686 * Remove all buffers from videobuf's list...
1687 */
1688 INIT_LIST_HEAD(&q->queued_list);
1689 /*
1690 * ...and done list; userspace will not receive any buffers it
1691 * has not already dequeued before initiating cancel.
1692 */
1693 INIT_LIST_HEAD(&q->done_list);
Marek Szyprowskiafdea8b2011-06-10 08:58:42 -03001694 atomic_set(&q->queued_count, 0);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001695 wake_up_all(&q->done_wq);
1696
1697 /*
1698 * Reinitialize all buffers for next use.
1699 */
1700 for (i = 0; i < q->num_buffers; ++i)
Sumit Semwalc5384042012-06-14 10:37:37 -03001701 __vb2_dqbuf(q->bufs[i]);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001702}
1703
1704/**
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001705 * vb2_streamon - start streaming
1706 * @q: videobuf2 queue
1707 * @type: type argument passed from userspace to vidioc_streamon handler
1708 *
1709 * Should be called from vidioc_streamon handler of a driver.
1710 * This function:
1711 * 1) verifies current state
1712 * 2) passes any previously queued buffers to the driver and starts streaming
1713 *
1714 * The return values from this function are intended to be directly returned
1715 * from vidioc_streamon handler in the driver.
1716 */
1717int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1718{
1719 struct vb2_buffer *vb;
1720 int ret;
1721
1722 if (q->fileio) {
1723 dprintk(1, "streamon: file io in progress\n");
1724 return -EBUSY;
1725 }
1726
1727 if (type != q->type) {
1728 dprintk(1, "streamon: invalid stream type\n");
1729 return -EINVAL;
1730 }
1731
1732 if (q->streaming) {
Ricardo Ribaldaf9560352013-11-08 07:08:45 -03001733 dprintk(3, "streamon successful: already streaming\n");
1734 return 0;
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001735 }
1736
1737 /*
1738 * If any buffers were queued before streamon,
1739 * we can now pass them to driver for processing.
1740 */
1741 list_for_each_entry(vb, &q->queued_list, queued_entry)
1742 __enqueue_in_driver(vb);
1743
1744 /*
1745 * Let driver notice that streaming state has been enabled.
1746 */
1747 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1748 if (ret) {
1749 dprintk(1, "streamon: driver refused to start streaming\n");
1750 __vb2_queue_cancel(q);
1751 return ret;
1752 }
1753
1754 q->streaming = 1;
1755
1756 dprintk(3, "Streamon successful\n");
1757 return 0;
1758}
1759EXPORT_SYMBOL_GPL(vb2_streamon);
1760
1761
1762/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001763 * vb2_streamoff - stop streaming
1764 * @q: videobuf2 queue
1765 * @type: type argument passed from userspace to vidioc_streamoff handler
1766 *
1767 * Should be called from vidioc_streamoff handler of a driver.
1768 * This function:
1769 * 1) verifies current state,
1770 * 2) stop streaming and dequeues any queued buffers, including those previously
1771 * passed to the driver (after waiting for the driver to finish).
1772 *
1773 * This call can be used for pausing playback.
1774 * The return values from this function are intended to be directly returned
1775 * from vidioc_streamoff handler in the driver
1776 */
1777int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1778{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001779 if (q->fileio) {
1780 dprintk(1, "streamoff: file io in progress\n");
1781 return -EBUSY;
1782 }
1783
Pawel Osciake23ccc02010-10-11 10:56:41 -03001784 if (type != q->type) {
1785 dprintk(1, "streamoff: invalid stream type\n");
1786 return -EINVAL;
1787 }
1788
1789 if (!q->streaming) {
Ricardo Ribaldaf9560352013-11-08 07:08:45 -03001790 dprintk(3, "streamoff successful: not streaming\n");
1791 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001792 }
1793
1794 /*
1795 * Cancel will pause streaming and remove all buffers from the driver
1796 * and videobuf, effectively returning control over them to userspace.
1797 */
1798 __vb2_queue_cancel(q);
1799
1800 dprintk(3, "Streamoff successful\n");
1801 return 0;
1802}
1803EXPORT_SYMBOL_GPL(vb2_streamoff);
1804
1805/**
1806 * __find_plane_by_offset() - find plane associated with the given offset off
1807 */
1808static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1809 unsigned int *_buffer, unsigned int *_plane)
1810{
1811 struct vb2_buffer *vb;
1812 unsigned int buffer, plane;
1813
1814 /*
1815 * Go over all buffers and their planes, comparing the given offset
1816 * with an offset assigned to each plane. If a match is found,
1817 * return its buffer and plane numbers.
1818 */
1819 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1820 vb = q->bufs[buffer];
1821
1822 for (plane = 0; plane < vb->num_planes; ++plane) {
1823 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1824 *_buffer = buffer;
1825 *_plane = plane;
1826 return 0;
1827 }
1828 }
1829 }
1830
1831 return -EINVAL;
1832}
1833
1834/**
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03001835 * vb2_expbuf() - Export a buffer as a file descriptor
1836 * @q: videobuf2 queue
1837 * @eb: export buffer structure passed from userspace to vidioc_expbuf
1838 * handler in driver
1839 *
1840 * The return values from this function are intended to be directly returned
1841 * from vidioc_expbuf handler in driver.
1842 */
1843int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
1844{
1845 struct vb2_buffer *vb = NULL;
1846 struct vb2_plane *vb_plane;
1847 int ret;
1848 struct dma_buf *dbuf;
1849
1850 if (q->memory != V4L2_MEMORY_MMAP) {
1851 dprintk(1, "Queue is not currently set up for mmap\n");
1852 return -EINVAL;
1853 }
1854
1855 if (!q->mem_ops->get_dmabuf) {
1856 dprintk(1, "Queue does not support DMA buffer exporting\n");
1857 return -EINVAL;
1858 }
1859
Philipp Zabelea3aba82013-05-21 05:11:35 -03001860 if (eb->flags & ~(O_CLOEXEC | O_ACCMODE)) {
1861 dprintk(1, "Queue does support only O_CLOEXEC and access mode flags\n");
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03001862 return -EINVAL;
1863 }
1864
1865 if (eb->type != q->type) {
1866 dprintk(1, "qbuf: invalid buffer type\n");
1867 return -EINVAL;
1868 }
1869
1870 if (eb->index >= q->num_buffers) {
1871 dprintk(1, "buffer index out of range\n");
1872 return -EINVAL;
1873 }
1874
1875 vb = q->bufs[eb->index];
1876
1877 if (eb->plane >= vb->num_planes) {
1878 dprintk(1, "buffer plane out of range\n");
1879 return -EINVAL;
1880 }
1881
1882 vb_plane = &vb->planes[eb->plane];
1883
Philipp Zabelea3aba82013-05-21 05:11:35 -03001884 dbuf = call_memop(q, get_dmabuf, vb_plane->mem_priv, eb->flags & O_ACCMODE);
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03001885 if (IS_ERR_OR_NULL(dbuf)) {
1886 dprintk(1, "Failed to export buffer %d, plane %d\n",
1887 eb->index, eb->plane);
1888 return -EINVAL;
1889 }
1890
Philipp Zabelea3aba82013-05-21 05:11:35 -03001891 ret = dma_buf_fd(dbuf, eb->flags & ~O_ACCMODE);
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03001892 if (ret < 0) {
1893 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
1894 eb->index, eb->plane, ret);
1895 dma_buf_put(dbuf);
1896 return ret;
1897 }
1898
1899 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
1900 eb->index, eb->plane, ret);
1901 eb->fd = ret;
1902
1903 return 0;
1904}
1905EXPORT_SYMBOL_GPL(vb2_expbuf);
1906
1907/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001908 * vb2_mmap() - map video buffers into application address space
1909 * @q: videobuf2 queue
1910 * @vma: vma passed to the mmap file operation handler in the driver
1911 *
1912 * Should be called from mmap file operation handler of a driver.
1913 * This function maps one plane of one of the available video buffers to
1914 * userspace. To map whole video memory allocated on reqbufs, this function
1915 * has to be called once per each plane per each buffer previously allocated.
1916 *
1917 * When the userspace application calls mmap, it passes to it an offset returned
1918 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1919 * a "cookie", which is then used to identify the plane to be mapped.
1920 * This function finds a plane with a matching offset and a mapping is performed
1921 * by the means of a provided memory operation.
1922 *
1923 * The return values from this function are intended to be directly returned
1924 * from the mmap handler in driver.
1925 */
1926int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1927{
1928 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001929 struct vb2_buffer *vb;
1930 unsigned int buffer, plane;
1931 int ret;
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -03001932 unsigned long length;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001933
1934 if (q->memory != V4L2_MEMORY_MMAP) {
1935 dprintk(1, "Queue is not currently set up for mmap\n");
1936 return -EINVAL;
1937 }
1938
1939 /*
1940 * Check memory area access mode.
1941 */
1942 if (!(vma->vm_flags & VM_SHARED)) {
1943 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1944 return -EINVAL;
1945 }
1946 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1947 if (!(vma->vm_flags & VM_WRITE)) {
1948 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1949 return -EINVAL;
1950 }
1951 } else {
1952 if (!(vma->vm_flags & VM_READ)) {
1953 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1954 return -EINVAL;
1955 }
1956 }
1957
1958 /*
1959 * Find the plane corresponding to the offset passed by userspace.
1960 */
1961 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1962 if (ret)
1963 return ret;
1964
1965 vb = q->bufs[buffer];
Pawel Osciake23ccc02010-10-11 10:56:41 -03001966
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -03001967 /*
1968 * MMAP requires page_aligned buffers.
1969 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
1970 * so, we need to do the same here.
1971 */
1972 length = PAGE_ALIGN(vb->v4l2_planes[plane].length);
1973 if (length < (vma->vm_end - vma->vm_start)) {
1974 dprintk(1,
1975 "MMAP invalid, as it would overflow buffer length\n");
Seung-Woo Kim068a0df2013-04-11 23:57:57 -03001976 return -EINVAL;
1977 }
1978
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001979 ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001980 if (ret)
1981 return ret;
1982
Pawel Osciake23ccc02010-10-11 10:56:41 -03001983 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1984 return 0;
1985}
1986EXPORT_SYMBOL_GPL(vb2_mmap);
1987
Scott Jiang6f524ec2011-09-21 09:25:23 -03001988#ifndef CONFIG_MMU
1989unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1990 unsigned long addr,
1991 unsigned long len,
1992 unsigned long pgoff,
1993 unsigned long flags)
1994{
1995 unsigned long off = pgoff << PAGE_SHIFT;
1996 struct vb2_buffer *vb;
1997 unsigned int buffer, plane;
1998 int ret;
1999
2000 if (q->memory != V4L2_MEMORY_MMAP) {
2001 dprintk(1, "Queue is not currently set up for mmap\n");
2002 return -EINVAL;
2003 }
2004
2005 /*
2006 * Find the plane corresponding to the offset passed by userspace.
2007 */
2008 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2009 if (ret)
2010 return ret;
2011
2012 vb = q->bufs[buffer];
2013
2014 return (unsigned long)vb2_plane_vaddr(vb, plane);
2015}
2016EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
2017#endif
2018
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002019static int __vb2_init_fileio(struct vb2_queue *q, int read);
2020static int __vb2_cleanup_fileio(struct vb2_queue *q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002021
2022/**
2023 * vb2_poll() - implements poll userspace operation
2024 * @q: videobuf2 queue
2025 * @file: file argument passed to the poll file operation handler
2026 * @wait: wait argument passed to the poll file operation handler
2027 *
2028 * This function implements poll file operation handler for a driver.
2029 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
2030 * be informed that the file descriptor of a video device is available for
2031 * reading.
2032 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
2033 * will be reported as available for writing.
2034 *
Hans Verkuil95213ce2011-07-13 04:26:52 -03002035 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
2036 * pending events.
2037 *
Pawel Osciake23ccc02010-10-11 10:56:41 -03002038 * The return values from this function are intended to be directly returned
2039 * from poll handler in driver.
2040 */
2041unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
2042{
Hans Verkuil95213ce2011-07-13 04:26:52 -03002043 struct video_device *vfd = video_devdata(file);
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002044 unsigned long req_events = poll_requested_events(wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002045 struct vb2_buffer *vb = NULL;
Hans Verkuil95213ce2011-07-13 04:26:52 -03002046 unsigned int res = 0;
2047 unsigned long flags;
2048
2049 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
2050 struct v4l2_fh *fh = file->private_data;
2051
2052 if (v4l2_event_pending(fh))
2053 res = POLLPRI;
2054 else if (req_events & POLLPRI)
2055 poll_wait(file, &fh->wait, wait);
2056 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03002057
Hans Verkuilcd138232013-01-30 13:29:02 -03002058 if (!V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLIN | POLLRDNORM)))
2059 return res;
2060 if (V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLOUT | POLLWRNORM)))
2061 return res;
2062
Pawel Osciake23ccc02010-10-11 10:56:41 -03002063 /*
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03002064 * Start file I/O emulator only if streaming API has not been used yet.
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002065 */
2066 if (q->num_buffers == 0 && q->fileio == NULL) {
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002067 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2068 (req_events & (POLLIN | POLLRDNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002069 if (__vb2_init_fileio(q, 1))
2070 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002071 }
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002072 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2073 (req_events & (POLLOUT | POLLWRNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002074 if (__vb2_init_fileio(q, 0))
2075 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002076 /*
2077 * Write to OUTPUT queue can be done immediately.
2078 */
Hans Verkuil95213ce2011-07-13 04:26:52 -03002079 return res | POLLOUT | POLLWRNORM;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002080 }
2081 }
2082
2083 /*
Pawel Osciake23ccc02010-10-11 10:56:41 -03002084 * There is nothing to wait for if no buffers have already been queued.
2085 */
2086 if (list_empty(&q->queued_list))
Hans Verkuil95213ce2011-07-13 04:26:52 -03002087 return res | POLLERR;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002088
Seung-Woo Kim412cb872013-05-20 23:47:29 -03002089 if (list_empty(&q->done_list))
2090 poll_wait(file, &q->done_wq, wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002091
2092 /*
2093 * Take first buffer available for dequeuing.
2094 */
2095 spin_lock_irqsave(&q->done_lock, flags);
2096 if (!list_empty(&q->done_list))
2097 vb = list_first_entry(&q->done_list, struct vb2_buffer,
2098 done_entry);
2099 spin_unlock_irqrestore(&q->done_lock, flags);
2100
2101 if (vb && (vb->state == VB2_BUF_STATE_DONE
2102 || vb->state == VB2_BUF_STATE_ERROR)) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002103 return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
2104 res | POLLOUT | POLLWRNORM :
2105 res | POLLIN | POLLRDNORM;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002106 }
Hans Verkuil95213ce2011-07-13 04:26:52 -03002107 return res;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002108}
2109EXPORT_SYMBOL_GPL(vb2_poll);
2110
2111/**
2112 * vb2_queue_init() - initialize a videobuf2 queue
2113 * @q: videobuf2 queue; this structure should be allocated in driver
2114 *
2115 * The vb2_queue structure should be allocated by the driver. The driver is
2116 * responsible of clearing it's content and setting initial values for some
2117 * required entries before calling this function.
2118 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2119 * to the struct vb2_queue description in include/media/videobuf2-core.h
2120 * for more information.
2121 */
2122int vb2_queue_init(struct vb2_queue *q)
2123{
Ezequiel Garcia896f38f2012-09-17 14:59:30 -03002124 /*
2125 * Sanity check
2126 */
2127 if (WARN_ON(!q) ||
2128 WARN_ON(!q->ops) ||
2129 WARN_ON(!q->mem_ops) ||
2130 WARN_ON(!q->type) ||
2131 WARN_ON(!q->io_modes) ||
2132 WARN_ON(!q->ops->queue_setup) ||
Kamil Debski6aa69f92013-01-25 06:29:57 -03002133 WARN_ON(!q->ops->buf_queue) ||
2134 WARN_ON(q->timestamp_type & ~V4L2_BUF_FLAG_TIMESTAMP_MASK))
Ezequiel Garcia896f38f2012-09-17 14:59:30 -03002135 return -EINVAL;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002136
Kamil Debski6aa69f92013-01-25 06:29:57 -03002137 /* Warn that the driver should choose an appropriate timestamp type */
2138 WARN_ON(q->timestamp_type == V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
2139
Pawel Osciake23ccc02010-10-11 10:56:41 -03002140 INIT_LIST_HEAD(&q->queued_list);
2141 INIT_LIST_HEAD(&q->done_list);
2142 spin_lock_init(&q->done_lock);
2143 init_waitqueue_head(&q->done_wq);
2144
2145 if (q->buf_struct_size == 0)
2146 q->buf_struct_size = sizeof(struct vb2_buffer);
2147
2148 return 0;
2149}
2150EXPORT_SYMBOL_GPL(vb2_queue_init);
2151
2152/**
2153 * vb2_queue_release() - stop streaming, release the queue and free memory
2154 * @q: videobuf2 queue
2155 *
2156 * This function stops streaming and performs necessary clean ups, including
2157 * freeing video buffer memory. The driver is responsible for freeing
2158 * the vb2_queue structure itself.
2159 */
2160void vb2_queue_release(struct vb2_queue *q)
2161{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002162 __vb2_cleanup_fileio(q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002163 __vb2_queue_cancel(q);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03002164 __vb2_queue_free(q, q->num_buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002165}
2166EXPORT_SYMBOL_GPL(vb2_queue_release);
2167
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002168/**
2169 * struct vb2_fileio_buf - buffer context used by file io emulator
2170 *
2171 * vb2 provides a compatibility layer and emulator of file io (read and
2172 * write) calls on top of streaming API. This structure is used for
2173 * tracking context related to the buffers.
2174 */
2175struct vb2_fileio_buf {
2176 void *vaddr;
2177 unsigned int size;
2178 unsigned int pos;
2179 unsigned int queued:1;
2180};
2181
2182/**
2183 * struct vb2_fileio_data - queue context used by file io emulator
2184 *
2185 * vb2 provides a compatibility layer and emulator of file io (read and
2186 * write) calls on top of streaming API. For proper operation it required
2187 * this structure to save the driver state between each call of the read
2188 * or write function.
2189 */
2190struct vb2_fileio_data {
2191 struct v4l2_requestbuffers req;
2192 struct v4l2_buffer b;
2193 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
2194 unsigned int index;
2195 unsigned int q_count;
2196 unsigned int dq_count;
2197 unsigned int flags;
2198};
2199
2200/**
2201 * __vb2_init_fileio() - initialize file io emulator
2202 * @q: videobuf2 queue
2203 * @read: mode selector (1 means read, 0 means write)
2204 */
2205static int __vb2_init_fileio(struct vb2_queue *q, int read)
2206{
2207 struct vb2_fileio_data *fileio;
2208 int i, ret;
2209 unsigned int count = 0;
2210
2211 /*
2212 * Sanity check
2213 */
2214 if ((read && !(q->io_modes & VB2_READ)) ||
2215 (!read && !(q->io_modes & VB2_WRITE)))
2216 BUG();
2217
2218 /*
2219 * Check if device supports mapping buffers to kernel virtual space.
2220 */
2221 if (!q->mem_ops->vaddr)
2222 return -EBUSY;
2223
2224 /*
2225 * Check if streaming api has not been already activated.
2226 */
2227 if (q->streaming || q->num_buffers > 0)
2228 return -EBUSY;
2229
2230 /*
2231 * Start with count 1, driver can increase it in queue_setup()
2232 */
2233 count = 1;
2234
2235 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
2236 (read) ? "read" : "write", count, q->io_flags);
2237
2238 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
2239 if (fileio == NULL)
2240 return -ENOMEM;
2241
2242 fileio->flags = q->io_flags;
2243
2244 /*
2245 * Request buffers and use MMAP type to force driver
2246 * to allocate buffers by itself.
2247 */
2248 fileio->req.count = count;
2249 fileio->req.memory = V4L2_MEMORY_MMAP;
2250 fileio->req.type = q->type;
2251 ret = vb2_reqbufs(q, &fileio->req);
2252 if (ret)
2253 goto err_kfree;
2254
2255 /*
2256 * Check if plane_count is correct
2257 * (multiplane buffers are not supported).
2258 */
2259 if (q->bufs[0]->num_planes != 1) {
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002260 ret = -EBUSY;
2261 goto err_reqbufs;
2262 }
2263
2264 /*
2265 * Get kernel address of each buffer.
2266 */
2267 for (i = 0; i < q->num_buffers; i++) {
2268 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
Wei Yongjun5dd69462013-05-13 01:48:45 -03002269 if (fileio->bufs[i].vaddr == NULL) {
2270 ret = -EINVAL;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002271 goto err_reqbufs;
Wei Yongjun5dd69462013-05-13 01:48:45 -03002272 }
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002273 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
2274 }
2275
2276 /*
2277 * Read mode requires pre queuing of all buffers.
2278 */
2279 if (read) {
2280 /*
2281 * Queue all buffers.
2282 */
2283 for (i = 0; i < q->num_buffers; i++) {
2284 struct v4l2_buffer *b = &fileio->b;
2285 memset(b, 0, sizeof(*b));
2286 b->type = q->type;
2287 b->memory = q->memory;
2288 b->index = i;
2289 ret = vb2_qbuf(q, b);
2290 if (ret)
2291 goto err_reqbufs;
2292 fileio->bufs[i].queued = 1;
2293 }
2294
2295 /*
2296 * Start streaming.
2297 */
2298 ret = vb2_streamon(q, q->type);
2299 if (ret)
2300 goto err_reqbufs;
2301 }
2302
2303 q->fileio = fileio;
2304
2305 return ret;
2306
2307err_reqbufs:
Hans de Goedea67e1722012-05-08 14:47:39 -03002308 fileio->req.count = 0;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002309 vb2_reqbufs(q, &fileio->req);
2310
2311err_kfree:
2312 kfree(fileio);
2313 return ret;
2314}
2315
2316/**
2317 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2318 * @q: videobuf2 queue
2319 */
2320static int __vb2_cleanup_fileio(struct vb2_queue *q)
2321{
2322 struct vb2_fileio_data *fileio = q->fileio;
2323
2324 if (fileio) {
2325 /*
2326 * Hack fileio context to enable direct calls to vb2 ioctl
2327 * interface.
2328 */
2329 q->fileio = NULL;
2330
2331 vb2_streamoff(q, q->type);
2332 fileio->req.count = 0;
2333 vb2_reqbufs(q, &fileio->req);
2334 kfree(fileio);
2335 dprintk(3, "file io emulator closed\n");
2336 }
2337 return 0;
2338}
2339
2340/**
2341 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2342 * @q: videobuf2 queue
2343 * @data: pointed to target userspace buffer
2344 * @count: number of bytes to read or write
2345 * @ppos: file handle position tracking pointer
2346 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2347 * @read: access mode selector (1 means read, 0 means write)
2348 */
2349static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
2350 loff_t *ppos, int nonblock, int read)
2351{
2352 struct vb2_fileio_data *fileio;
2353 struct vb2_fileio_buf *buf;
2354 int ret, index;
2355
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002356 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002357 read ? "read" : "write", (long)*ppos, count,
2358 nonblock ? "non" : "");
2359
2360 if (!data)
2361 return -EINVAL;
2362
2363 /*
2364 * Initialize emulator on first call.
2365 */
2366 if (!q->fileio) {
2367 ret = __vb2_init_fileio(q, read);
2368 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
2369 if (ret)
2370 return ret;
2371 }
2372 fileio = q->fileio;
2373
2374 /*
2375 * Hack fileio context to enable direct calls to vb2 ioctl interface.
2376 * The pointer will be restored before returning from this function.
2377 */
2378 q->fileio = NULL;
2379
2380 index = fileio->index;
2381 buf = &fileio->bufs[index];
2382
2383 /*
2384 * Check if we need to dequeue the buffer.
2385 */
2386 if (buf->queued) {
2387 struct vb2_buffer *vb;
2388
2389 /*
2390 * Call vb2_dqbuf to get buffer back.
2391 */
2392 memset(&fileio->b, 0, sizeof(fileio->b));
2393 fileio->b.type = q->type;
2394 fileio->b.memory = q->memory;
2395 fileio->b.index = index;
2396 ret = vb2_dqbuf(q, &fileio->b, nonblock);
2397 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2398 if (ret)
2399 goto end;
2400 fileio->dq_count += 1;
2401
2402 /*
2403 * Get number of bytes filled by the driver
2404 */
2405 vb = q->bufs[index];
2406 buf->size = vb2_get_plane_payload(vb, 0);
2407 buf->queued = 0;
2408 }
2409
2410 /*
2411 * Limit count on last few bytes of the buffer.
2412 */
2413 if (buf->pos + count > buf->size) {
2414 count = buf->size - buf->pos;
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002415 dprintk(5, "reducing read count: %zd\n", count);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002416 }
2417
2418 /*
2419 * Transfer data to userspace.
2420 */
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002421 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002422 count, index, buf->pos);
2423 if (read)
2424 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2425 else
2426 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2427 if (ret) {
2428 dprintk(3, "file io: error copying data\n");
2429 ret = -EFAULT;
2430 goto end;
2431 }
2432
2433 /*
2434 * Update counters.
2435 */
2436 buf->pos += count;
2437 *ppos += count;
2438
2439 /*
2440 * Queue next buffer if required.
2441 */
2442 if (buf->pos == buf->size ||
2443 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2444 /*
2445 * Check if this is the last buffer to read.
2446 */
2447 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2448 fileio->dq_count == 1) {
2449 dprintk(3, "file io: read limit reached\n");
2450 /*
2451 * Restore fileio pointer and release the context.
2452 */
2453 q->fileio = fileio;
2454 return __vb2_cleanup_fileio(q);
2455 }
2456
2457 /*
2458 * Call vb2_qbuf and give buffer to the driver.
2459 */
2460 memset(&fileio->b, 0, sizeof(fileio->b));
2461 fileio->b.type = q->type;
2462 fileio->b.memory = q->memory;
2463 fileio->b.index = index;
2464 fileio->b.bytesused = buf->pos;
2465 ret = vb2_qbuf(q, &fileio->b);
2466 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2467 if (ret)
2468 goto end;
2469
2470 /*
2471 * Buffer has been queued, update the status
2472 */
2473 buf->pos = 0;
2474 buf->queued = 1;
2475 buf->size = q->bufs[0]->v4l2_planes[0].length;
2476 fileio->q_count += 1;
2477
2478 /*
2479 * Switch to the next buffer
2480 */
2481 fileio->index = (index + 1) % q->num_buffers;
2482
2483 /*
2484 * Start streaming if required.
2485 */
2486 if (!read && !q->streaming) {
2487 ret = vb2_streamon(q, q->type);
2488 if (ret)
2489 goto end;
2490 }
2491 }
2492
2493 /*
2494 * Return proper number of bytes processed.
2495 */
2496 if (ret == 0)
2497 ret = count;
2498end:
2499 /*
2500 * Restore the fileio context and block vb2 ioctl interface.
2501 */
2502 q->fileio = fileio;
2503 return ret;
2504}
2505
2506size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2507 loff_t *ppos, int nonblocking)
2508{
2509 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2510}
2511EXPORT_SYMBOL_GPL(vb2_read);
2512
Ricardo Ribalda819585b2013-08-28 04:39:29 -03002513size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002514 loff_t *ppos, int nonblocking)
2515{
Ricardo Ribalda819585b2013-08-28 04:39:29 -03002516 return __vb2_perform_fileio(q, (char __user *) data, count,
2517 ppos, nonblocking, 0);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002518}
2519EXPORT_SYMBOL_GPL(vb2_write);
2520
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002521
2522/*
2523 * The following functions are not part of the vb2 core API, but are helper
2524 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2525 * and struct vb2_ops.
2526 * They contain boilerplate code that most if not all drivers have to do
2527 * and so they simplify the driver code.
2528 */
2529
2530/* The queue is busy if there is a owner and you are not that owner. */
2531static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
2532{
2533 return vdev->queue->owner && vdev->queue->owner != file->private_data;
2534}
2535
2536/* vb2 ioctl helpers */
2537
2538int vb2_ioctl_reqbufs(struct file *file, void *priv,
2539 struct v4l2_requestbuffers *p)
2540{
2541 struct video_device *vdev = video_devdata(file);
2542 int res = __verify_memory_type(vdev->queue, p->memory, p->type);
2543
2544 if (res)
2545 return res;
2546 if (vb2_queue_is_busy(vdev, file))
2547 return -EBUSY;
2548 res = __reqbufs(vdev->queue, p);
2549 /* If count == 0, then the owner has released all buffers and he
2550 is no longer owner of the queue. Otherwise we have a new owner. */
2551 if (res == 0)
2552 vdev->queue->owner = p->count ? file->private_data : NULL;
2553 return res;
2554}
2555EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
2556
2557int vb2_ioctl_create_bufs(struct file *file, void *priv,
2558 struct v4l2_create_buffers *p)
2559{
2560 struct video_device *vdev = video_devdata(file);
2561 int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
2562
2563 p->index = vdev->queue->num_buffers;
2564 /* If count == 0, then just check if memory and type are valid.
2565 Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2566 if (p->count == 0)
2567 return res != -EBUSY ? res : 0;
2568 if (res)
2569 return res;
2570 if (vb2_queue_is_busy(vdev, file))
2571 return -EBUSY;
2572 res = __create_bufs(vdev->queue, p);
2573 if (res == 0)
2574 vdev->queue->owner = file->private_data;
2575 return res;
2576}
2577EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
2578
2579int vb2_ioctl_prepare_buf(struct file *file, void *priv,
2580 struct v4l2_buffer *p)
2581{
2582 struct video_device *vdev = video_devdata(file);
2583
2584 if (vb2_queue_is_busy(vdev, file))
2585 return -EBUSY;
2586 return vb2_prepare_buf(vdev->queue, p);
2587}
2588EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
2589
2590int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
2591{
2592 struct video_device *vdev = video_devdata(file);
2593
2594 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2595 return vb2_querybuf(vdev->queue, p);
2596}
2597EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
2598
2599int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2600{
2601 struct video_device *vdev = video_devdata(file);
2602
2603 if (vb2_queue_is_busy(vdev, file))
2604 return -EBUSY;
2605 return vb2_qbuf(vdev->queue, p);
2606}
2607EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
2608
2609int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2610{
2611 struct video_device *vdev = video_devdata(file);
2612
2613 if (vb2_queue_is_busy(vdev, file))
2614 return -EBUSY;
2615 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
2616}
2617EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
2618
2619int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
2620{
2621 struct video_device *vdev = video_devdata(file);
2622
2623 if (vb2_queue_is_busy(vdev, file))
2624 return -EBUSY;
2625 return vb2_streamon(vdev->queue, i);
2626}
2627EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
2628
2629int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
2630{
2631 struct video_device *vdev = video_devdata(file);
2632
2633 if (vb2_queue_is_busy(vdev, file))
2634 return -EBUSY;
2635 return vb2_streamoff(vdev->queue, i);
2636}
2637EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
2638
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002639int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
2640{
2641 struct video_device *vdev = video_devdata(file);
2642
2643 if (vb2_queue_is_busy(vdev, file))
2644 return -EBUSY;
2645 return vb2_expbuf(vdev->queue, p);
2646}
2647EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
2648
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002649/* v4l2_file_operations helpers */
2650
2651int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
2652{
2653 struct video_device *vdev = video_devdata(file);
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002654 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2655 int err;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002656
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002657 if (lock && mutex_lock_interruptible(lock))
2658 return -ERESTARTSYS;
2659 err = vb2_mmap(vdev->queue, vma);
2660 if (lock)
2661 mutex_unlock(lock);
2662 return err;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002663}
2664EXPORT_SYMBOL_GPL(vb2_fop_mmap);
2665
Ricardo Ribalda1380f572013-11-25 05:49:02 -03002666int _vb2_fop_release(struct file *file, struct mutex *lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002667{
2668 struct video_device *vdev = video_devdata(file);
2669
2670 if (file->private_data == vdev->queue->owner) {
Ricardo Ribalda1380f572013-11-25 05:49:02 -03002671 if (lock)
2672 mutex_lock(lock);
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002673 vb2_queue_release(vdev->queue);
2674 vdev->queue->owner = NULL;
Ricardo Ribalda1380f572013-11-25 05:49:02 -03002675 if (lock)
2676 mutex_unlock(lock);
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002677 }
2678 return v4l2_fh_release(file);
2679}
Ricardo Ribalda1380f572013-11-25 05:49:02 -03002680EXPORT_SYMBOL_GPL(_vb2_fop_release);
2681
2682int vb2_fop_release(struct file *file)
2683{
2684 struct video_device *vdev = video_devdata(file);
2685 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2686
2687 return _vb2_fop_release(file, lock);
2688}
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002689EXPORT_SYMBOL_GPL(vb2_fop_release);
2690
Ricardo Ribalda819585b2013-08-28 04:39:29 -03002691ssize_t vb2_fop_write(struct file *file, const char __user *buf,
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002692 size_t count, loff_t *ppos)
2693{
2694 struct video_device *vdev = video_devdata(file);
2695 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002696 int err = -EBUSY;
2697
Hans Verkuilcf533732012-07-31 04:02:25 -03002698 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002699 return -ERESTARTSYS;
2700 if (vb2_queue_is_busy(vdev, file))
2701 goto exit;
2702 err = vb2_write(vdev->queue, buf, count, ppos,
2703 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03002704 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002705 vdev->queue->owner = file->private_data;
2706exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03002707 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002708 mutex_unlock(lock);
2709 return err;
2710}
2711EXPORT_SYMBOL_GPL(vb2_fop_write);
2712
2713ssize_t vb2_fop_read(struct file *file, char __user *buf,
2714 size_t count, loff_t *ppos)
2715{
2716 struct video_device *vdev = video_devdata(file);
2717 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002718 int err = -EBUSY;
2719
Hans Verkuilcf533732012-07-31 04:02:25 -03002720 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002721 return -ERESTARTSYS;
2722 if (vb2_queue_is_busy(vdev, file))
2723 goto exit;
2724 err = vb2_read(vdev->queue, buf, count, ppos,
2725 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03002726 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002727 vdev->queue->owner = file->private_data;
2728exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03002729 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002730 mutex_unlock(lock);
2731 return err;
2732}
2733EXPORT_SYMBOL_GPL(vb2_fop_read);
2734
2735unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
2736{
2737 struct video_device *vdev = video_devdata(file);
2738 struct vb2_queue *q = vdev->queue;
2739 struct mutex *lock = q->lock ? q->lock : vdev->lock;
2740 unsigned long req_events = poll_requested_events(wait);
2741 unsigned res;
2742 void *fileio;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002743 bool must_lock = false;
2744
2745 /* Try to be smart: only lock if polling might start fileio,
2746 otherwise locking will only introduce unwanted delays. */
2747 if (q->num_buffers == 0 && q->fileio == NULL) {
2748 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2749 (req_events & (POLLIN | POLLRDNORM)))
2750 must_lock = true;
2751 else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2752 (req_events & (POLLOUT | POLLWRNORM)))
2753 must_lock = true;
2754 }
2755
2756 /* If locking is needed, but this helper doesn't know how, then you
2757 shouldn't be using this helper but you should write your own. */
Hans Verkuilcf533732012-07-31 04:02:25 -03002758 WARN_ON(must_lock && !lock);
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002759
Hans Verkuilcf533732012-07-31 04:02:25 -03002760 if (must_lock && lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002761 return POLLERR;
2762
2763 fileio = q->fileio;
2764
2765 res = vb2_poll(vdev->queue, file, wait);
2766
2767 /* If fileio was started, then we have a new queue owner. */
2768 if (must_lock && !fileio && q->fileio)
2769 q->owner = file->private_data;
Hans Verkuilcf533732012-07-31 04:02:25 -03002770 if (must_lock && lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002771 mutex_unlock(lock);
2772 return res;
2773}
2774EXPORT_SYMBOL_GPL(vb2_fop_poll);
2775
2776#ifndef CONFIG_MMU
2777unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
2778 unsigned long len, unsigned long pgoff, unsigned long flags)
2779{
2780 struct video_device *vdev = video_devdata(file);
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002781 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2782 int ret;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002783
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002784 if (lock && mutex_lock_interruptible(lock))
2785 return -ERESTARTSYS;
2786 ret = vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
2787 if (lock)
2788 mutex_unlock(lock);
2789 return ret;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002790}
2791EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
2792#endif
2793
2794/* vb2_ops helpers. Only use if vq->lock is non-NULL. */
2795
2796void vb2_ops_wait_prepare(struct vb2_queue *vq)
2797{
2798 mutex_unlock(vq->lock);
2799}
2800EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
2801
2802void vb2_ops_wait_finish(struct vb2_queue *vq)
2803{
2804 mutex_lock(vq->lock);
2805}
2806EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
2807
Pawel Osciake23ccc02010-10-11 10:56:41 -03002808MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
Pawel Osciak95072082011-03-13 15:23:32 -03002809MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
Pawel Osciake23ccc02010-10-11 10:56:41 -03002810MODULE_LICENSE("GPL");