blob: 35a5b8ff6a0968a18f60b4af145001fbd69c31bd [file] [log] [blame]
Pawel Osciake23ccc02010-10-11 10:56:41 -03001/*
2 * videobuf2-core.c - V4L2 driver helper framework
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 *
Pawel Osciak95072082011-03-13 15:23:32 -03006 * Author: Pawel Osciak <pawel@osciak.com>
Pawel Osciake23ccc02010-10-11 10:56:41 -03007 * Marek Szyprowski <m.szyprowski@samsung.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation.
12 */
13
14#include <linux/err.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/mm.h>
18#include <linux/poll.h>
19#include <linux/slab.h>
20#include <linux/sched.h>
21
Hans Verkuil95213ce2011-07-13 04:26:52 -030022#include <media/v4l2-dev.h>
23#include <media/v4l2-fh.h>
24#include <media/v4l2-event.h>
Pawel Osciake23ccc02010-10-11 10:56:41 -030025#include <media/videobuf2-core.h>
26
27static int debug;
28module_param(debug, int, 0644);
29
30#define dprintk(level, fmt, arg...) \
31 do { \
32 if (debug >= level) \
33 printk(KERN_DEBUG "vb2: " fmt, ## arg); \
34 } while (0)
35
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030036#define call_memop(q, op, args...) \
Pawel Osciake23ccc02010-10-11 10:56:41 -030037 (((q)->mem_ops->op) ? \
38 ((q)->mem_ops->op(args)) : 0)
39
40#define call_qop(q, op, args...) \
41 (((q)->ops->op) ? ((q)->ops->op(args)) : 0)
42
Sakari Ailus1b18e7a2012-10-22 17:10:16 -030043#define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -030044 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
Sakari Ailus1b18e7a2012-10-22 17:10:16 -030045 V4L2_BUF_FLAG_PREPARED | \
46 V4L2_BUF_FLAG_TIMESTAMP_MASK)
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -030047
Pawel Osciake23ccc02010-10-11 10:56:41 -030048/**
49 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
50 */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030051static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
Pawel Osciake23ccc02010-10-11 10:56:41 -030052{
53 struct vb2_queue *q = vb->vb2_queue;
54 void *mem_priv;
55 int plane;
56
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -030057 /*
58 * Allocate memory for all planes in this buffer
59 * NOTE: mmapped areas should be page aligned
60 */
Pawel Osciake23ccc02010-10-11 10:56:41 -030061 for (plane = 0; plane < vb->num_planes; ++plane) {
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -030062 unsigned long size = PAGE_ALIGN(q->plane_sizes[plane]);
63
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030064 mem_priv = call_memop(q, alloc, q->alloc_ctx[plane],
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -030065 size, q->gfp_flags);
Guennadi Liakhovetski62a79432011-03-22 09:24:58 -030066 if (IS_ERR_OR_NULL(mem_priv))
Pawel Osciake23ccc02010-10-11 10:56:41 -030067 goto free;
68
69 /* Associate allocator private data with this plane */
70 vb->planes[plane].mem_priv = mem_priv;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030071 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
Pawel Osciake23ccc02010-10-11 10:56:41 -030072 }
73
74 return 0;
75free:
76 /* Free already allocated memory if one of the allocations failed */
Marek Szyprowskia00d0262011-12-15 05:53:06 -030077 for (; plane > 0; --plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030078 call_memop(q, put, vb->planes[plane - 1].mem_priv);
Marek Szyprowskia00d0262011-12-15 05:53:06 -030079 vb->planes[plane - 1].mem_priv = NULL;
80 }
Pawel Osciake23ccc02010-10-11 10:56:41 -030081
82 return -ENOMEM;
83}
84
85/**
86 * __vb2_buf_mem_free() - free memory of the given buffer
87 */
88static void __vb2_buf_mem_free(struct vb2_buffer *vb)
89{
90 struct vb2_queue *q = vb->vb2_queue;
91 unsigned int plane;
92
93 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030094 call_memop(q, put, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -030095 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskia00d0262011-12-15 05:53:06 -030096 dprintk(3, "Freed plane %d of buffer %d\n", plane,
97 vb->v4l2_buf.index);
Pawel Osciake23ccc02010-10-11 10:56:41 -030098 }
99}
100
101/**
102 * __vb2_buf_userptr_put() - release userspace memory associated with
103 * a USERPTR buffer
104 */
105static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
106{
107 struct vb2_queue *q = vb->vb2_queue;
108 unsigned int plane;
109
110 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300111 if (vb->planes[plane].mem_priv)
112 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
113 vb->planes[plane].mem_priv = NULL;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300114 }
115}
116
117/**
Sumit Semwalc5384042012-06-14 10:37:37 -0300118 * __vb2_plane_dmabuf_put() - release memory associated with
119 * a DMABUF shared plane
120 */
121static void __vb2_plane_dmabuf_put(struct vb2_queue *q, struct vb2_plane *p)
122{
123 if (!p->mem_priv)
124 return;
125
126 if (p->dbuf_mapped)
127 call_memop(q, unmap_dmabuf, p->mem_priv);
128
129 call_memop(q, detach_dmabuf, p->mem_priv);
130 dma_buf_put(p->dbuf);
131 memset(p, 0, sizeof(*p));
132}
133
134/**
135 * __vb2_buf_dmabuf_put() - release memory associated with
136 * a DMABUF shared buffer
137 */
138static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
139{
140 struct vb2_queue *q = vb->vb2_queue;
141 unsigned int plane;
142
143 for (plane = 0; plane < vb->num_planes; ++plane)
144 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
145}
146
147/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300148 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
149 * every buffer on the queue
150 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300151static void __setup_offsets(struct vb2_queue *q, unsigned int n)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300152{
153 unsigned int buffer, plane;
154 struct vb2_buffer *vb;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300155 unsigned long off;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300156
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300157 if (q->num_buffers) {
158 struct v4l2_plane *p;
159 vb = q->bufs[q->num_buffers - 1];
160 p = &vb->v4l2_planes[vb->num_planes - 1];
161 off = PAGE_ALIGN(p->m.mem_offset + p->length);
162 } else {
163 off = 0;
164 }
165
166 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300167 vb = q->bufs[buffer];
168 if (!vb)
169 continue;
170
171 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski49076022011-10-13 07:07:24 -0300172 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
Pawel Osciake23ccc02010-10-11 10:56:41 -0300173 vb->v4l2_planes[plane].m.mem_offset = off;
174
175 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
176 buffer, plane, off);
177
178 off += vb->v4l2_planes[plane].length;
179 off = PAGE_ALIGN(off);
180 }
181 }
182}
183
184/**
185 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
186 * video buffer memory for all buffers/planes on the queue and initializes the
187 * queue
188 *
189 * Returns the number of buffers successfully allocated.
190 */
191static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300192 unsigned int num_buffers, unsigned int num_planes)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300193{
194 unsigned int buffer;
195 struct vb2_buffer *vb;
196 int ret;
197
198 for (buffer = 0; buffer < num_buffers; ++buffer) {
199 /* Allocate videobuf buffer structures */
200 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
201 if (!vb) {
202 dprintk(1, "Memory alloc for buffer struct failed\n");
203 break;
204 }
205
206 /* Length stores number of planes for multiplanar buffers */
207 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
208 vb->v4l2_buf.length = num_planes;
209
210 vb->state = VB2_BUF_STATE_DEQUEUED;
211 vb->vb2_queue = q;
212 vb->num_planes = num_planes;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300213 vb->v4l2_buf.index = q->num_buffers + buffer;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300214 vb->v4l2_buf.type = q->type;
215 vb->v4l2_buf.memory = memory;
216
217 /* Allocate video buffer memory for the MMAP type */
218 if (memory == V4L2_MEMORY_MMAP) {
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300219 ret = __vb2_buf_mem_alloc(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300220 if (ret) {
221 dprintk(1, "Failed allocating memory for "
222 "buffer %d\n", buffer);
223 kfree(vb);
224 break;
225 }
226 /*
227 * Call the driver-provided buffer initialization
228 * callback, if given. An error in initialization
229 * results in queue setup failure.
230 */
231 ret = call_qop(q, buf_init, vb);
232 if (ret) {
233 dprintk(1, "Buffer %d %p initialization"
234 " failed\n", buffer, vb);
235 __vb2_buf_mem_free(vb);
236 kfree(vb);
237 break;
238 }
239 }
240
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300241 q->bufs[q->num_buffers + buffer] = vb;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300242 }
243
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300244 __setup_offsets(q, buffer);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300245
246 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300247 buffer, num_planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300248
249 return buffer;
250}
251
252/**
253 * __vb2_free_mem() - release all video buffer memory for a given queue
254 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300255static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300256{
257 unsigned int buffer;
258 struct vb2_buffer *vb;
259
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300260 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
261 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300262 vb = q->bufs[buffer];
263 if (!vb)
264 continue;
265
266 /* Free MMAP buffers or release USERPTR buffers */
267 if (q->memory == V4L2_MEMORY_MMAP)
268 __vb2_buf_mem_free(vb);
Sumit Semwalc5384042012-06-14 10:37:37 -0300269 else if (q->memory == V4L2_MEMORY_DMABUF)
270 __vb2_buf_dmabuf_put(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300271 else
272 __vb2_buf_userptr_put(vb);
273 }
274}
275
276/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300277 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
278 * related information, if no buffers are left return the queue to an
279 * uninitialized state. Might be called even if the queue has already been freed.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300280 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300281static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300282{
283 unsigned int buffer;
284
285 /* Call driver-provided cleanup function for each buffer, if provided */
286 if (q->ops->buf_cleanup) {
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300287 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
288 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300289 if (NULL == q->bufs[buffer])
290 continue;
291 q->ops->buf_cleanup(q->bufs[buffer]);
292 }
293 }
294
295 /* Release video buffer memory */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300296 __vb2_free_mem(q, buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300297
298 /* Free videobuf buffers */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300299 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
300 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300301 kfree(q->bufs[buffer]);
302 q->bufs[buffer] = NULL;
303 }
304
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300305 q->num_buffers -= buffers;
306 if (!q->num_buffers)
307 q->memory = 0;
Marek Szyprowskibd50d992011-10-25 03:07:59 -0300308 INIT_LIST_HEAD(&q->queued_list);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300309}
310
311/**
312 * __verify_planes_array() - verify that the planes array passed in struct
313 * v4l2_buffer from userspace can be safely used
314 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300315static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300316{
Hans Verkuil32a77262012-09-28 06:12:53 -0300317 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
318 return 0;
319
Pawel Osciake23ccc02010-10-11 10:56:41 -0300320 /* Is memory for copying plane information present? */
321 if (NULL == b->m.planes) {
322 dprintk(1, "Multi-planar buffer passed but "
323 "planes array not provided\n");
324 return -EINVAL;
325 }
326
327 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
328 dprintk(1, "Incorrect planes array length, "
329 "expected %d, got %d\n", vb->num_planes, b->length);
330 return -EINVAL;
331 }
332
333 return 0;
334}
335
336/**
Laurent Pinchart8023ed02012-07-10 10:41:40 -0300337 * __verify_length() - Verify that the bytesused value for each plane fits in
338 * the plane length and that the data offset doesn't exceed the bytesused value.
339 */
340static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
341{
342 unsigned int length;
343 unsigned int plane;
344
345 if (!V4L2_TYPE_IS_OUTPUT(b->type))
346 return 0;
347
348 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
349 for (plane = 0; plane < vb->num_planes; ++plane) {
350 length = (b->memory == V4L2_MEMORY_USERPTR)
351 ? b->m.planes[plane].length
352 : vb->v4l2_planes[plane].length;
353
354 if (b->m.planes[plane].bytesused > length)
355 return -EINVAL;
356 if (b->m.planes[plane].data_offset >=
357 b->m.planes[plane].bytesused)
358 return -EINVAL;
359 }
360 } else {
361 length = (b->memory == V4L2_MEMORY_USERPTR)
362 ? b->length : vb->v4l2_planes[0].length;
363
364 if (b->bytesused > length)
365 return -EINVAL;
366 }
367
368 return 0;
369}
370
371/**
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300372 * __buffer_in_use() - return true if the buffer is in use and
373 * the queue cannot be freed (by the means of REQBUFS(0)) call
374 */
375static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
376{
377 unsigned int plane;
378 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski2c2dd6ac2011-10-12 13:09:53 -0300379 void *mem_priv = vb->planes[plane].mem_priv;
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300380 /*
381 * If num_users() has not been provided, call_memop
382 * will return 0, apparently nobody cares about this
383 * case anyway. If num_users() returns more than 1,
384 * we are not the only user of the plane's memory.
385 */
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300386 if (mem_priv && call_memop(q, num_users, mem_priv) > 1)
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300387 return true;
388 }
389 return false;
390}
391
392/**
393 * __buffers_in_use() - return true if any buffers on the queue are in use and
394 * the queue cannot be freed (by the means of REQBUFS(0)) call
395 */
396static bool __buffers_in_use(struct vb2_queue *q)
397{
398 unsigned int buffer;
399 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
400 if (__buffer_in_use(q, q->bufs[buffer]))
401 return true;
402 }
403 return false;
404}
405
406/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300407 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
408 * returned to userspace
409 */
Hans Verkuil32a77262012-09-28 06:12:53 -0300410static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300411{
412 struct vb2_queue *q = vb->vb2_queue;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300413
Sakari Ailus2b719d72012-05-02 09:40:03 -0300414 /* Copy back data such as timestamp, flags, etc. */
Pawel Osciake23ccc02010-10-11 10:56:41 -0300415 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
Sakari Ailus2b719d72012-05-02 09:40:03 -0300416 b->reserved2 = vb->v4l2_buf.reserved2;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300417 b->reserved = vb->v4l2_buf.reserved;
418
419 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300420 /*
421 * Fill in plane-related data if userspace provided an array
Hans Verkuil32a77262012-09-28 06:12:53 -0300422 * for it. The caller has already verified memory and size.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300423 */
Hans Verkuil3c0b6062012-09-28 06:24:18 -0300424 b->length = vb->num_planes;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300425 memcpy(b->m.planes, vb->v4l2_planes,
426 b->length * sizeof(struct v4l2_plane));
427 } else {
428 /*
429 * We use length and offset in v4l2_planes array even for
430 * single-planar buffers, but userspace does not.
431 */
432 b->length = vb->v4l2_planes[0].length;
433 b->bytesused = vb->v4l2_planes[0].bytesused;
434 if (q->memory == V4L2_MEMORY_MMAP)
435 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
436 else if (q->memory == V4L2_MEMORY_USERPTR)
437 b->m.userptr = vb->v4l2_planes[0].m.userptr;
Sumit Semwalc5384042012-06-14 10:37:37 -0300438 else if (q->memory == V4L2_MEMORY_DMABUF)
439 b->m.fd = vb->v4l2_planes[0].m.fd;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300440 }
441
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300442 /*
443 * Clear any buffer state related flags.
444 */
Sakari Ailus1b18e7a2012-10-22 17:10:16 -0300445 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
Kamil Debski6aa69f92013-01-25 06:29:57 -0300446 b->flags |= q->timestamp_type;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300447
448 switch (vb->state) {
449 case VB2_BUF_STATE_QUEUED:
450 case VB2_BUF_STATE_ACTIVE:
451 b->flags |= V4L2_BUF_FLAG_QUEUED;
452 break;
453 case VB2_BUF_STATE_ERROR:
454 b->flags |= V4L2_BUF_FLAG_ERROR;
455 /* fall through */
456 case VB2_BUF_STATE_DONE:
457 b->flags |= V4L2_BUF_FLAG_DONE;
458 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300459 case VB2_BUF_STATE_PREPARED:
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300460 b->flags |= V4L2_BUF_FLAG_PREPARED;
461 break;
462 case VB2_BUF_STATE_DEQUEUED:
Pawel Osciake23ccc02010-10-11 10:56:41 -0300463 /* nothing */
464 break;
465 }
466
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300467 if (__buffer_in_use(q, vb))
Pawel Osciake23ccc02010-10-11 10:56:41 -0300468 b->flags |= V4L2_BUF_FLAG_MAPPED;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300469}
470
471/**
472 * vb2_querybuf() - query video buffer information
473 * @q: videobuf queue
474 * @b: buffer struct passed from userspace to vidioc_querybuf handler
475 * in driver
476 *
477 * Should be called from vidioc_querybuf ioctl handler in driver.
478 * This function will verify the passed v4l2_buffer structure and fill the
479 * relevant information for the userspace.
480 *
481 * The return values from this function are intended to be directly returned
482 * from vidioc_querybuf handler in driver.
483 */
484int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
485{
486 struct vb2_buffer *vb;
Hans Verkuil32a77262012-09-28 06:12:53 -0300487 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300488
489 if (b->type != q->type) {
490 dprintk(1, "querybuf: wrong buffer type\n");
491 return -EINVAL;
492 }
493
494 if (b->index >= q->num_buffers) {
495 dprintk(1, "querybuf: buffer index out of range\n");
496 return -EINVAL;
497 }
498 vb = q->bufs[b->index];
Hans Verkuil32a77262012-09-28 06:12:53 -0300499 ret = __verify_planes_array(vb, b);
500 if (!ret)
501 __fill_v4l2_buffer(vb, b);
502 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300503}
504EXPORT_SYMBOL(vb2_querybuf);
505
506/**
507 * __verify_userptr_ops() - verify that all memory operations required for
508 * USERPTR queue type have been provided
509 */
510static int __verify_userptr_ops(struct vb2_queue *q)
511{
512 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
513 !q->mem_ops->put_userptr)
514 return -EINVAL;
515
516 return 0;
517}
518
519/**
520 * __verify_mmap_ops() - verify that all memory operations required for
521 * MMAP queue type have been provided
522 */
523static int __verify_mmap_ops(struct vb2_queue *q)
524{
525 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
526 !q->mem_ops->put || !q->mem_ops->mmap)
527 return -EINVAL;
528
529 return 0;
530}
531
532/**
Sumit Semwalc5384042012-06-14 10:37:37 -0300533 * __verify_dmabuf_ops() - verify that all memory operations required for
534 * DMABUF queue type have been provided
535 */
536static int __verify_dmabuf_ops(struct vb2_queue *q)
537{
538 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
539 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
540 !q->mem_ops->unmap_dmabuf)
541 return -EINVAL;
542
543 return 0;
544}
545
546/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300547 * __verify_memory_type() - Check whether the memory type and buffer type
548 * passed to a buffer operation are compatible with the queue.
549 */
550static int __verify_memory_type(struct vb2_queue *q,
551 enum v4l2_memory memory, enum v4l2_buf_type type)
552{
Sumit Semwalc5384042012-06-14 10:37:37 -0300553 if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR &&
554 memory != V4L2_MEMORY_DMABUF) {
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300555 dprintk(1, "reqbufs: unsupported memory type\n");
556 return -EINVAL;
557 }
558
559 if (type != q->type) {
560 dprintk(1, "reqbufs: requested type is incorrect\n");
561 return -EINVAL;
562 }
563
564 /*
565 * Make sure all the required memory ops for given memory type
566 * are available.
567 */
568 if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
569 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
570 return -EINVAL;
571 }
572
573 if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
574 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
575 return -EINVAL;
576 }
577
Sumit Semwalc5384042012-06-14 10:37:37 -0300578 if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
579 dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
580 return -EINVAL;
581 }
582
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300583 /*
584 * Place the busy tests at the end: -EBUSY can be ignored when
585 * create_bufs is called with count == 0, but count == 0 should still
586 * do the memory and type validation.
587 */
588 if (q->fileio) {
589 dprintk(1, "reqbufs: file io in progress\n");
590 return -EBUSY;
591 }
592 return 0;
593}
594
595/**
596 * __reqbufs() - Initiate streaming
Pawel Osciake23ccc02010-10-11 10:56:41 -0300597 * @q: videobuf2 queue
598 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
599 *
600 * Should be called from vidioc_reqbufs ioctl handler of a driver.
601 * This function:
602 * 1) verifies streaming parameters passed from the userspace,
603 * 2) sets up the queue,
604 * 3) negotiates number of buffers and planes per buffer with the driver
605 * to be used during streaming,
606 * 4) allocates internal buffer structures (struct vb2_buffer), according to
607 * the agreed parameters,
608 * 5) for MMAP memory type, allocates actual video memory, using the
609 * memory handling/allocation routines provided during queue initialization
610 *
611 * If req->count is 0, all the memory will be freed instead.
612 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
613 * and the queue is not busy, memory will be reallocated.
614 *
615 * The return values from this function are intended to be directly returned
616 * from vidioc_reqbufs handler in driver.
617 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300618static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300619{
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300620 unsigned int num_buffers, allocated_buffers, num_planes = 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300621 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300622
623 if (q->streaming) {
624 dprintk(1, "reqbufs: streaming active\n");
625 return -EBUSY;
626 }
627
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300628 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300629 /*
630 * We already have buffers allocated, so first check if they
631 * are not in use and can be freed.
632 */
633 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
634 dprintk(1, "reqbufs: memory in use, cannot free\n");
635 return -EBUSY;
636 }
637
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300638 __vb2_queue_free(q, q->num_buffers);
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300639
640 /*
641 * In case of REQBUFS(0) return immediately without calling
642 * driver's queue_setup() callback and allocating resources.
643 */
644 if (req->count == 0)
645 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300646 }
647
648 /*
649 * Make sure the requested values and current defaults are sane.
650 */
651 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300652 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
Pawel Osciake23ccc02010-10-11 10:56:41 -0300653 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
Marek Szyprowski13b14092011-04-14 07:17:44 -0300654 q->memory = req->memory;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300655
656 /*
657 * Ask the driver how many buffers and planes per buffer it requires.
658 * Driver also sets the size and allocator context for each plane.
659 */
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300660 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300661 q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300662 if (ret)
663 return ret;
664
665 /* Finally, allocate buffers and video memory */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300666 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
Marek Szyprowski66072d42011-06-28 08:29:02 -0300667 if (ret == 0) {
668 dprintk(1, "Memory allocation failed\n");
669 return -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300670 }
671
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300672 allocated_buffers = ret;
673
Pawel Osciake23ccc02010-10-11 10:56:41 -0300674 /*
675 * Check if driver can handle the allocated number of buffers.
676 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300677 if (allocated_buffers < num_buffers) {
678 num_buffers = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300679
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300680 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
681 &num_planes, q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300682
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300683 if (!ret && allocated_buffers < num_buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300684 ret = -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300685
686 /*
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300687 * Either the driver has accepted a smaller number of buffers,
688 * or .queue_setup() returned an error
Pawel Osciake23ccc02010-10-11 10:56:41 -0300689 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300690 }
691
692 q->num_buffers = allocated_buffers;
693
694 if (ret < 0) {
695 __vb2_queue_free(q, allocated_buffers);
696 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300697 }
698
Pawel Osciake23ccc02010-10-11 10:56:41 -0300699 /*
700 * Return the number of successfully allocated buffers
701 * to the userspace.
702 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300703 req->count = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300704
705 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300706}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300707
708/**
709 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
710 * type values.
711 * @q: videobuf2 queue
712 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
713 */
714int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
715{
716 int ret = __verify_memory_type(q, req->memory, req->type);
717
718 return ret ? ret : __reqbufs(q, req);
719}
Pawel Osciake23ccc02010-10-11 10:56:41 -0300720EXPORT_SYMBOL_GPL(vb2_reqbufs);
721
722/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300723 * __create_bufs() - Allocate buffers and any required auxiliary structs
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300724 * @q: videobuf2 queue
725 * @create: creation parameters, passed from userspace to vidioc_create_bufs
726 * handler in driver
727 *
728 * Should be called from vidioc_create_bufs ioctl handler of a driver.
729 * This function:
730 * 1) verifies parameter sanity
731 * 2) calls the .queue_setup() queue operation
732 * 3) performs any necessary memory allocations
733 *
734 * The return values from this function are intended to be directly returned
735 * from vidioc_create_bufs handler in driver.
736 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300737static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300738{
739 unsigned int num_planes = 0, num_buffers, allocated_buffers;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300740 int ret;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300741
742 if (q->num_buffers == VIDEO_MAX_FRAME) {
743 dprintk(1, "%s(): maximum number of buffers already allocated\n",
744 __func__);
745 return -ENOBUFS;
746 }
747
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300748 if (!q->num_buffers) {
749 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
750 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
751 q->memory = create->memory;
752 }
753
754 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
755
756 /*
757 * Ask the driver, whether the requested number of buffers, planes per
758 * buffer and their sizes are acceptable
759 */
760 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
761 &num_planes, q->plane_sizes, q->alloc_ctx);
762 if (ret)
763 return ret;
764
765 /* Finally, allocate buffers and video memory */
766 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
767 num_planes);
Hans Verkuilf05393d22012-06-22 05:44:14 -0300768 if (ret == 0) {
769 dprintk(1, "Memory allocation failed\n");
770 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300771 }
772
773 allocated_buffers = ret;
774
775 /*
776 * Check if driver can handle the so far allocated number of buffers.
777 */
778 if (ret < num_buffers) {
779 num_buffers = ret;
780
781 /*
782 * q->num_buffers contains the total number of buffers, that the
783 * queue driver has set up
784 */
785 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
786 &num_planes, q->plane_sizes, q->alloc_ctx);
787
788 if (!ret && allocated_buffers < num_buffers)
789 ret = -ENOMEM;
790
791 /*
792 * Either the driver has accepted a smaller number of buffers,
793 * or .queue_setup() returned an error
794 */
795 }
796
797 q->num_buffers += allocated_buffers;
798
799 if (ret < 0) {
800 __vb2_queue_free(q, allocated_buffers);
Hans Verkuilf05393d22012-06-22 05:44:14 -0300801 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300802 }
803
804 /*
805 * Return the number of successfully allocated buffers
806 * to the userspace.
807 */
808 create->count = allocated_buffers;
809
810 return 0;
811}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300812
813/**
Nicolas THERY53aa3b12012-07-20 09:25:37 -0300814 * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
815 * memory and type values.
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300816 * @q: videobuf2 queue
817 * @create: creation parameters, passed from userspace to vidioc_create_bufs
818 * handler in driver
819 */
820int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
821{
822 int ret = __verify_memory_type(q, create->memory, create->format.type);
823
824 create->index = q->num_buffers;
Hans Verkuilf05393d22012-06-22 05:44:14 -0300825 if (create->count == 0)
826 return ret != -EBUSY ? ret : 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300827 return ret ? ret : __create_bufs(q, create);
828}
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300829EXPORT_SYMBOL_GPL(vb2_create_bufs);
830
831/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300832 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
833 * @vb: vb2_buffer to which the plane in question belongs to
834 * @plane_no: plane number for which the address is to be returned
835 *
836 * This function returns a kernel virtual address of a given plane if
837 * such a mapping exist, NULL otherwise.
838 */
839void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
840{
841 struct vb2_queue *q = vb->vb2_queue;
842
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300843 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300844 return NULL;
845
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300846 return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300847
848}
849EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
850
851/**
852 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
853 * @vb: vb2_buffer to which the plane in question belongs to
854 * @plane_no: plane number for which the cookie is to be returned
855 *
856 * This function returns an allocator specific cookie for a given plane if
857 * available, NULL otherwise. The allocator should provide some simple static
858 * inline function, which would convert this cookie to the allocator specific
859 * type that can be used directly by the driver to access the buffer. This can
860 * be for example physical address, pointer to scatter list or IOMMU mapping.
861 */
862void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
863{
864 struct vb2_queue *q = vb->vb2_queue;
865
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300866 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300867 return NULL;
868
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300869 return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300870}
871EXPORT_SYMBOL_GPL(vb2_plane_cookie);
872
873/**
874 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
875 * @vb: vb2_buffer returned from the driver
876 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
877 * or VB2_BUF_STATE_ERROR if the operation finished with an error
878 *
879 * This function should be called by the driver after a hardware operation on
880 * a buffer is finished and the buffer may be returned to userspace. The driver
881 * cannot use this buffer anymore until it is queued back to it by videobuf
882 * by the means of buf_queue callback. Only buffers previously queued to the
883 * driver by buf_queue can be passed to this function.
884 */
885void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
886{
887 struct vb2_queue *q = vb->vb2_queue;
888 unsigned long flags;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -0300889 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300890
891 if (vb->state != VB2_BUF_STATE_ACTIVE)
892 return;
893
894 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
895 return;
896
897 dprintk(4, "Done processing on buffer %d, state: %d\n",
Tushar Behera9b6f5dc2012-11-12 04:01:29 -0300898 vb->v4l2_buf.index, state);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300899
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -0300900 /* sync buffers */
901 for (plane = 0; plane < vb->num_planes; ++plane)
902 call_memop(q, finish, vb->planes[plane].mem_priv);
903
Pawel Osciake23ccc02010-10-11 10:56:41 -0300904 /* Add the buffer to the done buffers list */
905 spin_lock_irqsave(&q->done_lock, flags);
906 vb->state = state;
907 list_add_tail(&vb->done_entry, &q->done_list);
908 atomic_dec(&q->queued_count);
909 spin_unlock_irqrestore(&q->done_lock, flags);
910
911 /* Inform any processes that may be waiting for buffers */
912 wake_up(&q->done_wq);
913}
914EXPORT_SYMBOL_GPL(vb2_buffer_done);
915
916/**
Hans Verkuil32a77262012-09-28 06:12:53 -0300917 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
918 * v4l2_buffer by the userspace. The caller has already verified that struct
919 * v4l2_buffer has a valid number of planes.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300920 */
Hans Verkuil32a77262012-09-28 06:12:53 -0300921static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
Pawel Osciake23ccc02010-10-11 10:56:41 -0300922 struct v4l2_plane *v4l2_planes)
923{
924 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300925
926 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300927 /* Fill in driver-provided information for OUTPUT types */
928 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
929 /*
930 * Will have to go up to b->length when API starts
931 * accepting variable number of planes.
932 */
933 for (plane = 0; plane < vb->num_planes; ++plane) {
934 v4l2_planes[plane].bytesused =
935 b->m.planes[plane].bytesused;
936 v4l2_planes[plane].data_offset =
937 b->m.planes[plane].data_offset;
938 }
939 }
940
941 if (b->memory == V4L2_MEMORY_USERPTR) {
942 for (plane = 0; plane < vb->num_planes; ++plane) {
943 v4l2_planes[plane].m.userptr =
944 b->m.planes[plane].m.userptr;
945 v4l2_planes[plane].length =
946 b->m.planes[plane].length;
947 }
948 }
Sumit Semwalc5384042012-06-14 10:37:37 -0300949 if (b->memory == V4L2_MEMORY_DMABUF) {
950 for (plane = 0; plane < vb->num_planes; ++plane) {
951 v4l2_planes[plane].m.fd =
952 b->m.planes[plane].m.fd;
953 v4l2_planes[plane].length =
954 b->m.planes[plane].length;
955 v4l2_planes[plane].data_offset =
956 b->m.planes[plane].data_offset;
957 }
958 }
Pawel Osciake23ccc02010-10-11 10:56:41 -0300959 } else {
960 /*
961 * Single-planar buffers do not use planes array,
962 * so fill in relevant v4l2_buffer struct fields instead.
963 * In videobuf we use our internal V4l2_planes struct for
964 * single-planar buffers as well, for simplicity.
965 */
Laurent Pinchartac706bf2012-12-17 07:38:16 -0300966 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300967 v4l2_planes[0].bytesused = b->bytesused;
Laurent Pinchartac706bf2012-12-17 07:38:16 -0300968 v4l2_planes[0].data_offset = 0;
969 }
Pawel Osciake23ccc02010-10-11 10:56:41 -0300970
971 if (b->memory == V4L2_MEMORY_USERPTR) {
972 v4l2_planes[0].m.userptr = b->m.userptr;
973 v4l2_planes[0].length = b->length;
974 }
Sumit Semwalc5384042012-06-14 10:37:37 -0300975
976 if (b->memory == V4L2_MEMORY_DMABUF) {
977 v4l2_planes[0].m.fd = b->m.fd;
978 v4l2_planes[0].length = b->length;
979 v4l2_planes[0].data_offset = 0;
980 }
981
Pawel Osciake23ccc02010-10-11 10:56:41 -0300982 }
983
984 vb->v4l2_buf.field = b->field;
985 vb->v4l2_buf.timestamp = b->timestamp;
Sakari Ailus1b18e7a2012-10-22 17:10:16 -0300986 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300987}
988
989/**
990 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
991 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300992static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300993{
994 struct v4l2_plane planes[VIDEO_MAX_PLANES];
995 struct vb2_queue *q = vb->vb2_queue;
996 void *mem_priv;
997 unsigned int plane;
998 int ret;
999 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1000
Hans Verkuil32a77262012-09-28 06:12:53 -03001001 /* Copy relevant information provided by the userspace */
1002 __fill_vb2_buffer(vb, b, planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001003
1004 for (plane = 0; plane < vb->num_planes; ++plane) {
1005 /* Skip the plane if already verified */
Marek Szyprowskif0b7c7f2011-11-16 15:09:40 -03001006 if (vb->v4l2_planes[plane].m.userptr &&
1007 vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
Pawel Osciake23ccc02010-10-11 10:56:41 -03001008 && vb->v4l2_planes[plane].length == planes[plane].length)
1009 continue;
1010
1011 dprintk(3, "qbuf: userspace address for plane %d changed, "
1012 "reacquiring memory\n", plane);
1013
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001014 /* Check if the provided plane buffer is large enough */
1015 if (planes[plane].length < q->plane_sizes[plane]) {
Marek Szyprowski4c2625d2011-10-03 03:21:45 -03001016 ret = -EINVAL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001017 goto err;
1018 }
1019
Pawel Osciake23ccc02010-10-11 10:56:41 -03001020 /* Release previously acquired memory if present */
1021 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -03001022 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001023
1024 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001025 vb->v4l2_planes[plane].m.userptr = 0;
1026 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001027
1028 /* Acquire each plane's memory */
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001029 mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
1030 planes[plane].m.userptr,
1031 planes[plane].length, write);
1032 if (IS_ERR_OR_NULL(mem_priv)) {
1033 dprintk(1, "qbuf: failed acquiring userspace "
Pawel Osciake23ccc02010-10-11 10:56:41 -03001034 "memory for plane %d\n", plane);
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001035 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
1036 goto err;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001037 }
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001038 vb->planes[plane].mem_priv = mem_priv;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001039 }
1040
1041 /*
1042 * Call driver-specific initialization on the newly acquired buffer,
1043 * if provided.
1044 */
1045 ret = call_qop(q, buf_init, vb);
1046 if (ret) {
1047 dprintk(1, "qbuf: buffer initialization failed\n");
1048 goto err;
1049 }
1050
1051 /*
1052 * Now that everything is in order, copy relevant information
1053 * provided by userspace.
1054 */
1055 for (plane = 0; plane < vb->num_planes; ++plane)
1056 vb->v4l2_planes[plane] = planes[plane];
1057
1058 return 0;
1059err:
1060 /* In case of errors, release planes that were already acquired */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001061 for (plane = 0; plane < vb->num_planes; ++plane) {
1062 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -03001063 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001064 vb->planes[plane].mem_priv = NULL;
1065 vb->v4l2_planes[plane].m.userptr = 0;
1066 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001067 }
1068
1069 return ret;
1070}
1071
1072/**
1073 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1074 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001075static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001076{
Hans Verkuil32a77262012-09-28 06:12:53 -03001077 __fill_vb2_buffer(vb, b, vb->v4l2_planes);
1078 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001079}
1080
1081/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001082 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1083 */
1084static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1085{
1086 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1087 struct vb2_queue *q = vb->vb2_queue;
1088 void *mem_priv;
1089 unsigned int plane;
1090 int ret;
1091 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1092
1093 /* Verify and copy relevant information provided by the userspace */
1094 __fill_vb2_buffer(vb, b, planes);
1095
1096 for (plane = 0; plane < vb->num_planes; ++plane) {
1097 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1098
1099 if (IS_ERR_OR_NULL(dbuf)) {
1100 dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
1101 plane);
1102 ret = -EINVAL;
1103 goto err;
1104 }
1105
1106 /* use DMABUF size if length is not provided */
1107 if (planes[plane].length == 0)
1108 planes[plane].length = dbuf->size;
1109
1110 if (planes[plane].length < planes[plane].data_offset +
1111 q->plane_sizes[plane]) {
1112 ret = -EINVAL;
1113 goto err;
1114 }
1115
1116 /* Skip the plane if already verified */
1117 if (dbuf == vb->planes[plane].dbuf &&
1118 vb->v4l2_planes[plane].length == planes[plane].length) {
1119 dma_buf_put(dbuf);
1120 continue;
1121 }
1122
1123 dprintk(1, "qbuf: buffer for plane %d changed\n", plane);
1124
1125 /* Release previously acquired memory if present */
1126 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
1127 memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
1128
1129 /* Acquire each plane's memory */
1130 mem_priv = call_memop(q, attach_dmabuf, q->alloc_ctx[plane],
1131 dbuf, planes[plane].length, write);
1132 if (IS_ERR(mem_priv)) {
1133 dprintk(1, "qbuf: failed to attach dmabuf\n");
1134 ret = PTR_ERR(mem_priv);
1135 dma_buf_put(dbuf);
1136 goto err;
1137 }
1138
1139 vb->planes[plane].dbuf = dbuf;
1140 vb->planes[plane].mem_priv = mem_priv;
1141 }
1142
1143 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1144 * really we want to do this just before the DMA, not while queueing
1145 * the buffer(s)..
1146 */
1147 for (plane = 0; plane < vb->num_planes; ++plane) {
1148 ret = call_memop(q, map_dmabuf, vb->planes[plane].mem_priv);
1149 if (ret) {
1150 dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
1151 plane);
1152 goto err;
1153 }
1154 vb->planes[plane].dbuf_mapped = 1;
1155 }
1156
1157 /*
1158 * Call driver-specific initialization on the newly acquired buffer,
1159 * if provided.
1160 */
1161 ret = call_qop(q, buf_init, vb);
1162 if (ret) {
1163 dprintk(1, "qbuf: buffer initialization failed\n");
1164 goto err;
1165 }
1166
1167 /*
1168 * Now that everything is in order, copy relevant information
1169 * provided by userspace.
1170 */
1171 for (plane = 0; plane < vb->num_planes; ++plane)
1172 vb->v4l2_planes[plane] = planes[plane];
1173
1174 return 0;
1175err:
1176 /* In case of errors, release planes that were already acquired */
1177 __vb2_buf_dmabuf_put(vb);
1178
1179 return ret;
1180}
1181
1182/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001183 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1184 */
1185static void __enqueue_in_driver(struct vb2_buffer *vb)
1186{
1187 struct vb2_queue *q = vb->vb2_queue;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001188 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001189
1190 vb->state = VB2_BUF_STATE_ACTIVE;
1191 atomic_inc(&q->queued_count);
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001192
1193 /* sync buffers */
1194 for (plane = 0; plane < vb->num_planes; ++plane)
1195 call_memop(q, prepare, vb->planes[plane].mem_priv);
1196
Pawel Osciake23ccc02010-10-11 10:56:41 -03001197 q->ops->buf_queue(vb);
1198}
1199
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001200static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001201{
1202 struct vb2_queue *q = vb->vb2_queue;
1203 int ret;
1204
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001205 ret = __verify_length(vb, b);
1206 if (ret < 0)
1207 return ret;
1208
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001209 switch (q->memory) {
1210 case V4L2_MEMORY_MMAP:
1211 ret = __qbuf_mmap(vb, b);
1212 break;
1213 case V4L2_MEMORY_USERPTR:
1214 ret = __qbuf_userptr(vb, b);
1215 break;
Sumit Semwalc5384042012-06-14 10:37:37 -03001216 case V4L2_MEMORY_DMABUF:
1217 ret = __qbuf_dmabuf(vb, b);
1218 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001219 default:
1220 WARN(1, "Invalid queue type\n");
1221 ret = -EINVAL;
1222 }
1223
1224 if (!ret)
1225 ret = call_qop(q, buf_prepare, vb);
1226 if (ret)
1227 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
1228 else
1229 vb->state = VB2_BUF_STATE_PREPARED;
1230
1231 return ret;
1232}
1233
Pawel Osciake23ccc02010-10-11 10:56:41 -03001234/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001235 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1236 * @q: videobuf2 queue
1237 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1238 * handler in driver
1239 *
1240 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1241 * This function:
1242 * 1) verifies the passed buffer,
1243 * 2) calls buf_prepare callback in the driver (if provided), in which
1244 * driver-specific buffer initialization can be performed,
1245 *
1246 * The return values from this function are intended to be directly returned
1247 * from vidioc_prepare_buf handler in driver.
1248 */
1249int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1250{
1251 struct vb2_buffer *vb;
1252 int ret;
1253
1254 if (q->fileio) {
1255 dprintk(1, "%s(): file io in progress\n", __func__);
1256 return -EBUSY;
1257 }
1258
1259 if (b->type != q->type) {
1260 dprintk(1, "%s(): invalid buffer type\n", __func__);
1261 return -EINVAL;
1262 }
1263
1264 if (b->index >= q->num_buffers) {
1265 dprintk(1, "%s(): buffer index out of range\n", __func__);
1266 return -EINVAL;
1267 }
1268
1269 vb = q->bufs[b->index];
1270 if (NULL == vb) {
1271 /* Should never happen */
1272 dprintk(1, "%s(): buffer is NULL\n", __func__);
1273 return -EINVAL;
1274 }
1275
1276 if (b->memory != q->memory) {
1277 dprintk(1, "%s(): invalid memory type\n", __func__);
1278 return -EINVAL;
1279 }
1280
1281 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1282 dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state);
1283 return -EINVAL;
1284 }
Hans Verkuil32a77262012-09-28 06:12:53 -03001285 ret = __verify_planes_array(vb, b);
1286 if (ret < 0)
1287 return ret;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001288 ret = __buf_prepare(vb, b);
1289 if (ret < 0)
1290 return ret;
1291
1292 __fill_v4l2_buffer(vb, b);
1293
1294 return 0;
1295}
1296EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1297
1298/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001299 * vb2_qbuf() - Queue a buffer from userspace
1300 * @q: videobuf2 queue
1301 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1302 * in driver
1303 *
1304 * Should be called from vidioc_qbuf ioctl handler of a driver.
1305 * This function:
1306 * 1) verifies the passed buffer,
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001307 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1308 * which driver-specific buffer initialization can be performed,
Pawel Osciake23ccc02010-10-11 10:56:41 -03001309 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1310 * callback for processing.
1311 *
1312 * The return values from this function are intended to be directly returned
1313 * from vidioc_qbuf handler in driver.
1314 */
1315int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1316{
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001317 struct rw_semaphore *mmap_sem = NULL;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001318 struct vb2_buffer *vb;
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001319 int ret = 0;
1320
1321 /*
1322 * In case of user pointer buffers vb2 allocator needs to get direct
1323 * access to userspace pages. This requires getting read access on
1324 * mmap semaphore in the current process structure. The same
1325 * semaphore is taken before calling mmap operation, while both mmap
1326 * and qbuf are called by the driver or v4l2 core with driver's lock
1327 * held. To avoid a AB-BA deadlock (mmap_sem then driver's lock in
1328 * mmap and driver's lock then mmap_sem in qbuf) the videobuf2 core
1329 * release driver's lock, takes mmap_sem and then takes again driver's
1330 * lock.
1331 *
1332 * To avoid race with other vb2 calls, which might be called after
1333 * releasing driver's lock, this operation is performed at the
1334 * beggining of qbuf processing. This way the queue status is
1335 * consistent after getting driver's lock back.
1336 */
Mauro Carvalho Chehab57e43cf2011-12-30 16:10:44 -02001337 if (q->memory == V4L2_MEMORY_USERPTR) {
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001338 mmap_sem = &current->mm->mmap_sem;
1339 call_qop(q, wait_prepare, q);
1340 down_read(mmap_sem);
1341 call_qop(q, wait_finish, q);
1342 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001343
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001344 if (q->fileio) {
1345 dprintk(1, "qbuf: file io in progress\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001346 ret = -EBUSY;
1347 goto unlock;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001348 }
1349
Pawel Osciake23ccc02010-10-11 10:56:41 -03001350 if (b->type != q->type) {
1351 dprintk(1, "qbuf: invalid buffer type\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001352 ret = -EINVAL;
1353 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001354 }
1355
1356 if (b->index >= q->num_buffers) {
1357 dprintk(1, "qbuf: buffer index out of range\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001358 ret = -EINVAL;
1359 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001360 }
1361
1362 vb = q->bufs[b->index];
1363 if (NULL == vb) {
1364 /* Should never happen */
1365 dprintk(1, "qbuf: buffer is NULL\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001366 ret = -EINVAL;
1367 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001368 }
1369
1370 if (b->memory != q->memory) {
1371 dprintk(1, "qbuf: invalid memory type\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001372 ret = -EINVAL;
1373 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001374 }
Hans Verkuil32a77262012-09-28 06:12:53 -03001375 ret = __verify_planes_array(vb, b);
1376 if (ret)
1377 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001378
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001379 switch (vb->state) {
1380 case VB2_BUF_STATE_DEQUEUED:
1381 ret = __buf_prepare(vb, b);
1382 if (ret)
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001383 goto unlock;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001384 case VB2_BUF_STATE_PREPARED:
1385 break;
1386 default:
Pawel Osciake23ccc02010-10-11 10:56:41 -03001387 dprintk(1, "qbuf: buffer already in use\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001388 ret = -EINVAL;
1389 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001390 }
1391
Pawel Osciake23ccc02010-10-11 10:56:41 -03001392 /*
1393 * Add to the queued buffers list, a buffer will stay on it until
1394 * dequeued in dqbuf.
1395 */
1396 list_add_tail(&vb->queued_entry, &q->queued_list);
1397 vb->state = VB2_BUF_STATE_QUEUED;
1398
1399 /*
1400 * If already streaming, give the buffer to driver for processing.
1401 * If not, the buffer will be given to driver on next streamon.
1402 */
1403 if (q->streaming)
1404 __enqueue_in_driver(vb);
1405
Guennadi Liakhovetski21db3e02011-09-28 07:23:27 -03001406 /* Fill buffer information for the userspace */
1407 __fill_v4l2_buffer(vb, b);
1408
Pawel Osciake23ccc02010-10-11 10:56:41 -03001409 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001410unlock:
1411 if (mmap_sem)
1412 up_read(mmap_sem);
1413 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001414}
1415EXPORT_SYMBOL_GPL(vb2_qbuf);
1416
1417/**
1418 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1419 * for dequeuing
1420 *
1421 * Will sleep if required for nonblocking == false.
1422 */
1423static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1424{
1425 /*
1426 * All operations on vb_done_list are performed under done_lock
1427 * spinlock protection. However, buffers may be removed from
1428 * it and returned to userspace only while holding both driver's
1429 * lock and the done_lock spinlock. Thus we can be sure that as
1430 * long as we hold the driver's lock, the list will remain not
1431 * empty if list_empty() check succeeds.
1432 */
1433
1434 for (;;) {
1435 int ret;
1436
1437 if (!q->streaming) {
1438 dprintk(1, "Streaming off, will not wait for buffers\n");
1439 return -EINVAL;
1440 }
1441
1442 if (!list_empty(&q->done_list)) {
1443 /*
1444 * Found a buffer that we were waiting for.
1445 */
1446 break;
1447 }
1448
1449 if (nonblocking) {
1450 dprintk(1, "Nonblocking and no buffers to dequeue, "
1451 "will not wait\n");
1452 return -EAGAIN;
1453 }
1454
1455 /*
1456 * We are streaming and blocking, wait for another buffer to
1457 * become ready or for streamoff. Driver's lock is released to
1458 * allow streamoff or qbuf to be called while waiting.
1459 */
1460 call_qop(q, wait_prepare, q);
1461
1462 /*
1463 * All locks have been released, it is safe to sleep now.
1464 */
1465 dprintk(3, "Will sleep waiting for buffers\n");
1466 ret = wait_event_interruptible(q->done_wq,
1467 !list_empty(&q->done_list) || !q->streaming);
1468
1469 /*
1470 * We need to reevaluate both conditions again after reacquiring
1471 * the locks or return an error if one occurred.
1472 */
1473 call_qop(q, wait_finish, q);
Hans Verkuil32a77262012-09-28 06:12:53 -03001474 if (ret) {
1475 dprintk(1, "Sleep was interrupted\n");
Pawel Osciake23ccc02010-10-11 10:56:41 -03001476 return ret;
Hans Verkuil32a77262012-09-28 06:12:53 -03001477 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001478 }
1479 return 0;
1480}
1481
1482/**
1483 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1484 *
1485 * Will sleep if required for nonblocking == false.
1486 */
1487static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
Hans Verkuil32a77262012-09-28 06:12:53 -03001488 struct v4l2_buffer *b, int nonblocking)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001489{
1490 unsigned long flags;
1491 int ret;
1492
1493 /*
1494 * Wait for at least one buffer to become available on the done_list.
1495 */
1496 ret = __vb2_wait_for_done_vb(q, nonblocking);
1497 if (ret)
1498 return ret;
1499
1500 /*
1501 * Driver's lock has been held since we last verified that done_list
1502 * is not empty, so no need for another list_empty(done_list) check.
1503 */
1504 spin_lock_irqsave(&q->done_lock, flags);
1505 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
Hans Verkuil32a77262012-09-28 06:12:53 -03001506 /*
1507 * Only remove the buffer from done_list if v4l2_buffer can handle all
1508 * the planes.
1509 */
1510 ret = __verify_planes_array(*vb, b);
1511 if (!ret)
1512 list_del(&(*vb)->done_entry);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001513 spin_unlock_irqrestore(&q->done_lock, flags);
1514
Hans Verkuil32a77262012-09-28 06:12:53 -03001515 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001516}
1517
1518/**
1519 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1520 * @q: videobuf2 queue
1521 *
1522 * This function will wait until all buffers that have been given to the driver
1523 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1524 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1525 * taken, for example from stop_streaming() callback.
1526 */
1527int vb2_wait_for_all_buffers(struct vb2_queue *q)
1528{
1529 if (!q->streaming) {
1530 dprintk(1, "Streaming off, will not wait for buffers\n");
1531 return -EINVAL;
1532 }
1533
1534 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1535 return 0;
1536}
1537EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1538
1539/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001540 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1541 */
1542static void __vb2_dqbuf(struct vb2_buffer *vb)
1543{
1544 struct vb2_queue *q = vb->vb2_queue;
1545 unsigned int i;
1546
1547 /* nothing to do if the buffer is already dequeued */
1548 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1549 return;
1550
1551 vb->state = VB2_BUF_STATE_DEQUEUED;
1552
1553 /* unmap DMABUF buffer */
1554 if (q->memory == V4L2_MEMORY_DMABUF)
1555 for (i = 0; i < vb->num_planes; ++i) {
1556 if (!vb->planes[i].dbuf_mapped)
1557 continue;
1558 call_memop(q, unmap_dmabuf, vb->planes[i].mem_priv);
1559 vb->planes[i].dbuf_mapped = 0;
1560 }
1561}
1562
1563/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001564 * vb2_dqbuf() - Dequeue a buffer to the userspace
1565 * @q: videobuf2 queue
1566 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1567 * in driver
1568 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1569 * buffers ready for dequeuing are present. Normally the driver
1570 * would be passing (file->f_flags & O_NONBLOCK) here
1571 *
1572 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1573 * This function:
1574 * 1) verifies the passed buffer,
1575 * 2) calls buf_finish callback in the driver (if provided), in which
1576 * driver can perform any additional operations that may be required before
1577 * returning the buffer to userspace, such as cache sync,
1578 * 3) the buffer struct members are filled with relevant information for
1579 * the userspace.
1580 *
1581 * The return values from this function are intended to be directly returned
1582 * from vidioc_dqbuf handler in driver.
1583 */
1584int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1585{
1586 struct vb2_buffer *vb = NULL;
1587 int ret;
1588
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001589 if (q->fileio) {
1590 dprintk(1, "dqbuf: file io in progress\n");
1591 return -EBUSY;
1592 }
1593
Pawel Osciake23ccc02010-10-11 10:56:41 -03001594 if (b->type != q->type) {
1595 dprintk(1, "dqbuf: invalid buffer type\n");
1596 return -EINVAL;
1597 }
Hans Verkuil32a77262012-09-28 06:12:53 -03001598 ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
1599 if (ret < 0)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001600 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001601
1602 ret = call_qop(q, buf_finish, vb);
1603 if (ret) {
1604 dprintk(1, "dqbuf: buffer finish failed\n");
1605 return ret;
1606 }
1607
1608 switch (vb->state) {
1609 case VB2_BUF_STATE_DONE:
1610 dprintk(3, "dqbuf: Returning done buffer\n");
1611 break;
1612 case VB2_BUF_STATE_ERROR:
1613 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1614 break;
1615 default:
1616 dprintk(1, "dqbuf: Invalid buffer state\n");
1617 return -EINVAL;
1618 }
1619
1620 /* Fill buffer information for the userspace */
1621 __fill_v4l2_buffer(vb, b);
1622 /* Remove from videobuf queue */
1623 list_del(&vb->queued_entry);
Sumit Semwalc5384042012-06-14 10:37:37 -03001624 /* go back to dequeued state */
1625 __vb2_dqbuf(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001626
1627 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1628 vb->v4l2_buf.index, vb->state);
1629
Pawel Osciake23ccc02010-10-11 10:56:41 -03001630 return 0;
1631}
1632EXPORT_SYMBOL_GPL(vb2_dqbuf);
1633
1634/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001635 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1636 *
1637 * Removes all queued buffers from driver's queue and all buffers queued by
1638 * userspace from videobuf's queue. Returns to state after reqbufs.
1639 */
1640static void __vb2_queue_cancel(struct vb2_queue *q)
1641{
1642 unsigned int i;
1643
1644 /*
1645 * Tell driver to stop all transactions and release all queued
1646 * buffers.
1647 */
1648 if (q->streaming)
1649 call_qop(q, stop_streaming, q);
1650 q->streaming = 0;
1651
1652 /*
1653 * Remove all buffers from videobuf's list...
1654 */
1655 INIT_LIST_HEAD(&q->queued_list);
1656 /*
1657 * ...and done list; userspace will not receive any buffers it
1658 * has not already dequeued before initiating cancel.
1659 */
1660 INIT_LIST_HEAD(&q->done_list);
Marek Szyprowskiafdea8b2011-06-10 08:58:42 -03001661 atomic_set(&q->queued_count, 0);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001662 wake_up_all(&q->done_wq);
1663
1664 /*
1665 * Reinitialize all buffers for next use.
1666 */
1667 for (i = 0; i < q->num_buffers; ++i)
Sumit Semwalc5384042012-06-14 10:37:37 -03001668 __vb2_dqbuf(q->bufs[i]);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001669}
1670
1671/**
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001672 * vb2_streamon - start streaming
1673 * @q: videobuf2 queue
1674 * @type: type argument passed from userspace to vidioc_streamon handler
1675 *
1676 * Should be called from vidioc_streamon handler of a driver.
1677 * This function:
1678 * 1) verifies current state
1679 * 2) passes any previously queued buffers to the driver and starts streaming
1680 *
1681 * The return values from this function are intended to be directly returned
1682 * from vidioc_streamon handler in the driver.
1683 */
1684int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1685{
1686 struct vb2_buffer *vb;
1687 int ret;
1688
1689 if (q->fileio) {
1690 dprintk(1, "streamon: file io in progress\n");
1691 return -EBUSY;
1692 }
1693
1694 if (type != q->type) {
1695 dprintk(1, "streamon: invalid stream type\n");
1696 return -EINVAL;
1697 }
1698
1699 if (q->streaming) {
1700 dprintk(1, "streamon: already streaming\n");
1701 return -EBUSY;
1702 }
1703
1704 /*
1705 * If any buffers were queued before streamon,
1706 * we can now pass them to driver for processing.
1707 */
1708 list_for_each_entry(vb, &q->queued_list, queued_entry)
1709 __enqueue_in_driver(vb);
1710
1711 /*
1712 * Let driver notice that streaming state has been enabled.
1713 */
1714 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1715 if (ret) {
1716 dprintk(1, "streamon: driver refused to start streaming\n");
1717 __vb2_queue_cancel(q);
1718 return ret;
1719 }
1720
1721 q->streaming = 1;
1722
1723 dprintk(3, "Streamon successful\n");
1724 return 0;
1725}
1726EXPORT_SYMBOL_GPL(vb2_streamon);
1727
1728
1729/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001730 * vb2_streamoff - stop streaming
1731 * @q: videobuf2 queue
1732 * @type: type argument passed from userspace to vidioc_streamoff handler
1733 *
1734 * Should be called from vidioc_streamoff handler of a driver.
1735 * This function:
1736 * 1) verifies current state,
1737 * 2) stop streaming and dequeues any queued buffers, including those previously
1738 * passed to the driver (after waiting for the driver to finish).
1739 *
1740 * This call can be used for pausing playback.
1741 * The return values from this function are intended to be directly returned
1742 * from vidioc_streamoff handler in the driver
1743 */
1744int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1745{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001746 if (q->fileio) {
1747 dprintk(1, "streamoff: file io in progress\n");
1748 return -EBUSY;
1749 }
1750
Pawel Osciake23ccc02010-10-11 10:56:41 -03001751 if (type != q->type) {
1752 dprintk(1, "streamoff: invalid stream type\n");
1753 return -EINVAL;
1754 }
1755
1756 if (!q->streaming) {
1757 dprintk(1, "streamoff: not streaming\n");
1758 return -EINVAL;
1759 }
1760
1761 /*
1762 * Cancel will pause streaming and remove all buffers from the driver
1763 * and videobuf, effectively returning control over them to userspace.
1764 */
1765 __vb2_queue_cancel(q);
1766
1767 dprintk(3, "Streamoff successful\n");
1768 return 0;
1769}
1770EXPORT_SYMBOL_GPL(vb2_streamoff);
1771
1772/**
1773 * __find_plane_by_offset() - find plane associated with the given offset off
1774 */
1775static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1776 unsigned int *_buffer, unsigned int *_plane)
1777{
1778 struct vb2_buffer *vb;
1779 unsigned int buffer, plane;
1780
1781 /*
1782 * Go over all buffers and their planes, comparing the given offset
1783 * with an offset assigned to each plane. If a match is found,
1784 * return its buffer and plane numbers.
1785 */
1786 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1787 vb = q->bufs[buffer];
1788
1789 for (plane = 0; plane < vb->num_planes; ++plane) {
1790 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1791 *_buffer = buffer;
1792 *_plane = plane;
1793 return 0;
1794 }
1795 }
1796 }
1797
1798 return -EINVAL;
1799}
1800
1801/**
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03001802 * vb2_expbuf() - Export a buffer as a file descriptor
1803 * @q: videobuf2 queue
1804 * @eb: export buffer structure passed from userspace to vidioc_expbuf
1805 * handler in driver
1806 *
1807 * The return values from this function are intended to be directly returned
1808 * from vidioc_expbuf handler in driver.
1809 */
1810int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
1811{
1812 struct vb2_buffer *vb = NULL;
1813 struct vb2_plane *vb_plane;
1814 int ret;
1815 struct dma_buf *dbuf;
1816
1817 if (q->memory != V4L2_MEMORY_MMAP) {
1818 dprintk(1, "Queue is not currently set up for mmap\n");
1819 return -EINVAL;
1820 }
1821
1822 if (!q->mem_ops->get_dmabuf) {
1823 dprintk(1, "Queue does not support DMA buffer exporting\n");
1824 return -EINVAL;
1825 }
1826
1827 if (eb->flags & ~O_CLOEXEC) {
1828 dprintk(1, "Queue does support only O_CLOEXEC flag\n");
1829 return -EINVAL;
1830 }
1831
1832 if (eb->type != q->type) {
1833 dprintk(1, "qbuf: invalid buffer type\n");
1834 return -EINVAL;
1835 }
1836
1837 if (eb->index >= q->num_buffers) {
1838 dprintk(1, "buffer index out of range\n");
1839 return -EINVAL;
1840 }
1841
1842 vb = q->bufs[eb->index];
1843
1844 if (eb->plane >= vb->num_planes) {
1845 dprintk(1, "buffer plane out of range\n");
1846 return -EINVAL;
1847 }
1848
1849 vb_plane = &vb->planes[eb->plane];
1850
1851 dbuf = call_memop(q, get_dmabuf, vb_plane->mem_priv);
1852 if (IS_ERR_OR_NULL(dbuf)) {
1853 dprintk(1, "Failed to export buffer %d, plane %d\n",
1854 eb->index, eb->plane);
1855 return -EINVAL;
1856 }
1857
1858 ret = dma_buf_fd(dbuf, eb->flags);
1859 if (ret < 0) {
1860 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
1861 eb->index, eb->plane, ret);
1862 dma_buf_put(dbuf);
1863 return ret;
1864 }
1865
1866 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
1867 eb->index, eb->plane, ret);
1868 eb->fd = ret;
1869
1870 return 0;
1871}
1872EXPORT_SYMBOL_GPL(vb2_expbuf);
1873
1874/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001875 * vb2_mmap() - map video buffers into application address space
1876 * @q: videobuf2 queue
1877 * @vma: vma passed to the mmap file operation handler in the driver
1878 *
1879 * Should be called from mmap file operation handler of a driver.
1880 * This function maps one plane of one of the available video buffers to
1881 * userspace. To map whole video memory allocated on reqbufs, this function
1882 * has to be called once per each plane per each buffer previously allocated.
1883 *
1884 * When the userspace application calls mmap, it passes to it an offset returned
1885 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1886 * a "cookie", which is then used to identify the plane to be mapped.
1887 * This function finds a plane with a matching offset and a mapping is performed
1888 * by the means of a provided memory operation.
1889 *
1890 * The return values from this function are intended to be directly returned
1891 * from the mmap handler in driver.
1892 */
1893int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1894{
1895 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001896 struct vb2_buffer *vb;
1897 unsigned int buffer, plane;
1898 int ret;
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -03001899 unsigned long length;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001900
1901 if (q->memory != V4L2_MEMORY_MMAP) {
1902 dprintk(1, "Queue is not currently set up for mmap\n");
1903 return -EINVAL;
1904 }
1905
1906 /*
1907 * Check memory area access mode.
1908 */
1909 if (!(vma->vm_flags & VM_SHARED)) {
1910 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1911 return -EINVAL;
1912 }
1913 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1914 if (!(vma->vm_flags & VM_WRITE)) {
1915 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1916 return -EINVAL;
1917 }
1918 } else {
1919 if (!(vma->vm_flags & VM_READ)) {
1920 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1921 return -EINVAL;
1922 }
1923 }
1924
1925 /*
1926 * Find the plane corresponding to the offset passed by userspace.
1927 */
1928 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1929 if (ret)
1930 return ret;
1931
1932 vb = q->bufs[buffer];
Pawel Osciake23ccc02010-10-11 10:56:41 -03001933
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -03001934 /*
1935 * MMAP requires page_aligned buffers.
1936 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
1937 * so, we need to do the same here.
1938 */
1939 length = PAGE_ALIGN(vb->v4l2_planes[plane].length);
1940 if (length < (vma->vm_end - vma->vm_start)) {
1941 dprintk(1,
1942 "MMAP invalid, as it would overflow buffer length\n");
Seung-Woo Kim068a0df2013-04-11 23:57:57 -03001943 return -EINVAL;
1944 }
1945
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001946 ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001947 if (ret)
1948 return ret;
1949
Pawel Osciake23ccc02010-10-11 10:56:41 -03001950 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1951 return 0;
1952}
1953EXPORT_SYMBOL_GPL(vb2_mmap);
1954
Scott Jiang6f524ec2011-09-21 09:25:23 -03001955#ifndef CONFIG_MMU
1956unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1957 unsigned long addr,
1958 unsigned long len,
1959 unsigned long pgoff,
1960 unsigned long flags)
1961{
1962 unsigned long off = pgoff << PAGE_SHIFT;
1963 struct vb2_buffer *vb;
1964 unsigned int buffer, plane;
1965 int ret;
1966
1967 if (q->memory != V4L2_MEMORY_MMAP) {
1968 dprintk(1, "Queue is not currently set up for mmap\n");
1969 return -EINVAL;
1970 }
1971
1972 /*
1973 * Find the plane corresponding to the offset passed by userspace.
1974 */
1975 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1976 if (ret)
1977 return ret;
1978
1979 vb = q->bufs[buffer];
1980
1981 return (unsigned long)vb2_plane_vaddr(vb, plane);
1982}
1983EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
1984#endif
1985
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001986static int __vb2_init_fileio(struct vb2_queue *q, int read);
1987static int __vb2_cleanup_fileio(struct vb2_queue *q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001988
1989/**
1990 * vb2_poll() - implements poll userspace operation
1991 * @q: videobuf2 queue
1992 * @file: file argument passed to the poll file operation handler
1993 * @wait: wait argument passed to the poll file operation handler
1994 *
1995 * This function implements poll file operation handler for a driver.
1996 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1997 * be informed that the file descriptor of a video device is available for
1998 * reading.
1999 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
2000 * will be reported as available for writing.
2001 *
Hans Verkuil95213ce2011-07-13 04:26:52 -03002002 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
2003 * pending events.
2004 *
Pawel Osciake23ccc02010-10-11 10:56:41 -03002005 * The return values from this function are intended to be directly returned
2006 * from poll handler in driver.
2007 */
2008unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
2009{
Hans Verkuil95213ce2011-07-13 04:26:52 -03002010 struct video_device *vfd = video_devdata(file);
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002011 unsigned long req_events = poll_requested_events(wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002012 struct vb2_buffer *vb = NULL;
Hans Verkuil95213ce2011-07-13 04:26:52 -03002013 unsigned int res = 0;
2014 unsigned long flags;
2015
2016 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
2017 struct v4l2_fh *fh = file->private_data;
2018
2019 if (v4l2_event_pending(fh))
2020 res = POLLPRI;
2021 else if (req_events & POLLPRI)
2022 poll_wait(file, &fh->wait, wait);
2023 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03002024
Hans Verkuilcd138232013-01-30 13:29:02 -03002025 if (!V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLIN | POLLRDNORM)))
2026 return res;
2027 if (V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLOUT | POLLWRNORM)))
2028 return res;
2029
Pawel Osciake23ccc02010-10-11 10:56:41 -03002030 /*
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03002031 * Start file I/O emulator only if streaming API has not been used yet.
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002032 */
2033 if (q->num_buffers == 0 && q->fileio == NULL) {
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002034 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2035 (req_events & (POLLIN | POLLRDNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002036 if (__vb2_init_fileio(q, 1))
2037 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002038 }
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002039 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2040 (req_events & (POLLOUT | POLLWRNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002041 if (__vb2_init_fileio(q, 0))
2042 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002043 /*
2044 * Write to OUTPUT queue can be done immediately.
2045 */
Hans Verkuil95213ce2011-07-13 04:26:52 -03002046 return res | POLLOUT | POLLWRNORM;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002047 }
2048 }
2049
2050 /*
Pawel Osciake23ccc02010-10-11 10:56:41 -03002051 * There is nothing to wait for if no buffers have already been queued.
2052 */
2053 if (list_empty(&q->queued_list))
Hans Verkuil95213ce2011-07-13 04:26:52 -03002054 return res | POLLERR;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002055
Seung-Woo Kim412cb872013-05-20 23:47:29 -03002056 if (list_empty(&q->done_list))
2057 poll_wait(file, &q->done_wq, wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002058
2059 /*
2060 * Take first buffer available for dequeuing.
2061 */
2062 spin_lock_irqsave(&q->done_lock, flags);
2063 if (!list_empty(&q->done_list))
2064 vb = list_first_entry(&q->done_list, struct vb2_buffer,
2065 done_entry);
2066 spin_unlock_irqrestore(&q->done_lock, flags);
2067
2068 if (vb && (vb->state == VB2_BUF_STATE_DONE
2069 || vb->state == VB2_BUF_STATE_ERROR)) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002070 return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
2071 res | POLLOUT | POLLWRNORM :
2072 res | POLLIN | POLLRDNORM;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002073 }
Hans Verkuil95213ce2011-07-13 04:26:52 -03002074 return res;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002075}
2076EXPORT_SYMBOL_GPL(vb2_poll);
2077
2078/**
2079 * vb2_queue_init() - initialize a videobuf2 queue
2080 * @q: videobuf2 queue; this structure should be allocated in driver
2081 *
2082 * The vb2_queue structure should be allocated by the driver. The driver is
2083 * responsible of clearing it's content and setting initial values for some
2084 * required entries before calling this function.
2085 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2086 * to the struct vb2_queue description in include/media/videobuf2-core.h
2087 * for more information.
2088 */
2089int vb2_queue_init(struct vb2_queue *q)
2090{
Ezequiel Garcia896f38f2012-09-17 14:59:30 -03002091 /*
2092 * Sanity check
2093 */
2094 if (WARN_ON(!q) ||
2095 WARN_ON(!q->ops) ||
2096 WARN_ON(!q->mem_ops) ||
2097 WARN_ON(!q->type) ||
2098 WARN_ON(!q->io_modes) ||
2099 WARN_ON(!q->ops->queue_setup) ||
Kamil Debski6aa69f92013-01-25 06:29:57 -03002100 WARN_ON(!q->ops->buf_queue) ||
2101 WARN_ON(q->timestamp_type & ~V4L2_BUF_FLAG_TIMESTAMP_MASK))
Ezequiel Garcia896f38f2012-09-17 14:59:30 -03002102 return -EINVAL;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002103
Kamil Debski6aa69f92013-01-25 06:29:57 -03002104 /* Warn that the driver should choose an appropriate timestamp type */
2105 WARN_ON(q->timestamp_type == V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
2106
Pawel Osciake23ccc02010-10-11 10:56:41 -03002107 INIT_LIST_HEAD(&q->queued_list);
2108 INIT_LIST_HEAD(&q->done_list);
2109 spin_lock_init(&q->done_lock);
2110 init_waitqueue_head(&q->done_wq);
2111
2112 if (q->buf_struct_size == 0)
2113 q->buf_struct_size = sizeof(struct vb2_buffer);
2114
2115 return 0;
2116}
2117EXPORT_SYMBOL_GPL(vb2_queue_init);
2118
2119/**
2120 * vb2_queue_release() - stop streaming, release the queue and free memory
2121 * @q: videobuf2 queue
2122 *
2123 * This function stops streaming and performs necessary clean ups, including
2124 * freeing video buffer memory. The driver is responsible for freeing
2125 * the vb2_queue structure itself.
2126 */
2127void vb2_queue_release(struct vb2_queue *q)
2128{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002129 __vb2_cleanup_fileio(q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002130 __vb2_queue_cancel(q);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03002131 __vb2_queue_free(q, q->num_buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002132}
2133EXPORT_SYMBOL_GPL(vb2_queue_release);
2134
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002135/**
2136 * struct vb2_fileio_buf - buffer context used by file io emulator
2137 *
2138 * vb2 provides a compatibility layer and emulator of file io (read and
2139 * write) calls on top of streaming API. This structure is used for
2140 * tracking context related to the buffers.
2141 */
2142struct vb2_fileio_buf {
2143 void *vaddr;
2144 unsigned int size;
2145 unsigned int pos;
2146 unsigned int queued:1;
2147};
2148
2149/**
2150 * struct vb2_fileio_data - queue context used by file io emulator
2151 *
2152 * vb2 provides a compatibility layer and emulator of file io (read and
2153 * write) calls on top of streaming API. For proper operation it required
2154 * this structure to save the driver state between each call of the read
2155 * or write function.
2156 */
2157struct vb2_fileio_data {
2158 struct v4l2_requestbuffers req;
2159 struct v4l2_buffer b;
2160 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
2161 unsigned int index;
2162 unsigned int q_count;
2163 unsigned int dq_count;
2164 unsigned int flags;
2165};
2166
2167/**
2168 * __vb2_init_fileio() - initialize file io emulator
2169 * @q: videobuf2 queue
2170 * @read: mode selector (1 means read, 0 means write)
2171 */
2172static int __vb2_init_fileio(struct vb2_queue *q, int read)
2173{
2174 struct vb2_fileio_data *fileio;
2175 int i, ret;
2176 unsigned int count = 0;
2177
2178 /*
2179 * Sanity check
2180 */
2181 if ((read && !(q->io_modes & VB2_READ)) ||
2182 (!read && !(q->io_modes & VB2_WRITE)))
2183 BUG();
2184
2185 /*
2186 * Check if device supports mapping buffers to kernel virtual space.
2187 */
2188 if (!q->mem_ops->vaddr)
2189 return -EBUSY;
2190
2191 /*
2192 * Check if streaming api has not been already activated.
2193 */
2194 if (q->streaming || q->num_buffers > 0)
2195 return -EBUSY;
2196
2197 /*
2198 * Start with count 1, driver can increase it in queue_setup()
2199 */
2200 count = 1;
2201
2202 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
2203 (read) ? "read" : "write", count, q->io_flags);
2204
2205 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
2206 if (fileio == NULL)
2207 return -ENOMEM;
2208
2209 fileio->flags = q->io_flags;
2210
2211 /*
2212 * Request buffers and use MMAP type to force driver
2213 * to allocate buffers by itself.
2214 */
2215 fileio->req.count = count;
2216 fileio->req.memory = V4L2_MEMORY_MMAP;
2217 fileio->req.type = q->type;
2218 ret = vb2_reqbufs(q, &fileio->req);
2219 if (ret)
2220 goto err_kfree;
2221
2222 /*
2223 * Check if plane_count is correct
2224 * (multiplane buffers are not supported).
2225 */
2226 if (q->bufs[0]->num_planes != 1) {
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002227 ret = -EBUSY;
2228 goto err_reqbufs;
2229 }
2230
2231 /*
2232 * Get kernel address of each buffer.
2233 */
2234 for (i = 0; i < q->num_buffers; i++) {
2235 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
Wei Yongjun5dd69462013-05-13 01:48:45 -03002236 if (fileio->bufs[i].vaddr == NULL) {
2237 ret = -EINVAL;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002238 goto err_reqbufs;
Wei Yongjun5dd69462013-05-13 01:48:45 -03002239 }
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002240 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
2241 }
2242
2243 /*
2244 * Read mode requires pre queuing of all buffers.
2245 */
2246 if (read) {
2247 /*
2248 * Queue all buffers.
2249 */
2250 for (i = 0; i < q->num_buffers; i++) {
2251 struct v4l2_buffer *b = &fileio->b;
2252 memset(b, 0, sizeof(*b));
2253 b->type = q->type;
2254 b->memory = q->memory;
2255 b->index = i;
2256 ret = vb2_qbuf(q, b);
2257 if (ret)
2258 goto err_reqbufs;
2259 fileio->bufs[i].queued = 1;
2260 }
2261
2262 /*
2263 * Start streaming.
2264 */
2265 ret = vb2_streamon(q, q->type);
2266 if (ret)
2267 goto err_reqbufs;
2268 }
2269
2270 q->fileio = fileio;
2271
2272 return ret;
2273
2274err_reqbufs:
Hans de Goedea67e1722012-05-08 14:47:39 -03002275 fileio->req.count = 0;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002276 vb2_reqbufs(q, &fileio->req);
2277
2278err_kfree:
2279 kfree(fileio);
2280 return ret;
2281}
2282
2283/**
2284 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2285 * @q: videobuf2 queue
2286 */
2287static int __vb2_cleanup_fileio(struct vb2_queue *q)
2288{
2289 struct vb2_fileio_data *fileio = q->fileio;
2290
2291 if (fileio) {
2292 /*
2293 * Hack fileio context to enable direct calls to vb2 ioctl
2294 * interface.
2295 */
2296 q->fileio = NULL;
2297
2298 vb2_streamoff(q, q->type);
2299 fileio->req.count = 0;
2300 vb2_reqbufs(q, &fileio->req);
2301 kfree(fileio);
2302 dprintk(3, "file io emulator closed\n");
2303 }
2304 return 0;
2305}
2306
2307/**
2308 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2309 * @q: videobuf2 queue
2310 * @data: pointed to target userspace buffer
2311 * @count: number of bytes to read or write
2312 * @ppos: file handle position tracking pointer
2313 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2314 * @read: access mode selector (1 means read, 0 means write)
2315 */
2316static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
2317 loff_t *ppos, int nonblock, int read)
2318{
2319 struct vb2_fileio_data *fileio;
2320 struct vb2_fileio_buf *buf;
2321 int ret, index;
2322
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002323 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002324 read ? "read" : "write", (long)*ppos, count,
2325 nonblock ? "non" : "");
2326
2327 if (!data)
2328 return -EINVAL;
2329
2330 /*
2331 * Initialize emulator on first call.
2332 */
2333 if (!q->fileio) {
2334 ret = __vb2_init_fileio(q, read);
2335 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
2336 if (ret)
2337 return ret;
2338 }
2339 fileio = q->fileio;
2340
2341 /*
2342 * Hack fileio context to enable direct calls to vb2 ioctl interface.
2343 * The pointer will be restored before returning from this function.
2344 */
2345 q->fileio = NULL;
2346
2347 index = fileio->index;
2348 buf = &fileio->bufs[index];
2349
2350 /*
2351 * Check if we need to dequeue the buffer.
2352 */
2353 if (buf->queued) {
2354 struct vb2_buffer *vb;
2355
2356 /*
2357 * Call vb2_dqbuf to get buffer back.
2358 */
2359 memset(&fileio->b, 0, sizeof(fileio->b));
2360 fileio->b.type = q->type;
2361 fileio->b.memory = q->memory;
2362 fileio->b.index = index;
2363 ret = vb2_dqbuf(q, &fileio->b, nonblock);
2364 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2365 if (ret)
2366 goto end;
2367 fileio->dq_count += 1;
2368
2369 /*
2370 * Get number of bytes filled by the driver
2371 */
2372 vb = q->bufs[index];
2373 buf->size = vb2_get_plane_payload(vb, 0);
2374 buf->queued = 0;
2375 }
2376
2377 /*
2378 * Limit count on last few bytes of the buffer.
2379 */
2380 if (buf->pos + count > buf->size) {
2381 count = buf->size - buf->pos;
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002382 dprintk(5, "reducing read count: %zd\n", count);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002383 }
2384
2385 /*
2386 * Transfer data to userspace.
2387 */
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002388 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002389 count, index, buf->pos);
2390 if (read)
2391 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2392 else
2393 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2394 if (ret) {
2395 dprintk(3, "file io: error copying data\n");
2396 ret = -EFAULT;
2397 goto end;
2398 }
2399
2400 /*
2401 * Update counters.
2402 */
2403 buf->pos += count;
2404 *ppos += count;
2405
2406 /*
2407 * Queue next buffer if required.
2408 */
2409 if (buf->pos == buf->size ||
2410 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2411 /*
2412 * Check if this is the last buffer to read.
2413 */
2414 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2415 fileio->dq_count == 1) {
2416 dprintk(3, "file io: read limit reached\n");
2417 /*
2418 * Restore fileio pointer and release the context.
2419 */
2420 q->fileio = fileio;
2421 return __vb2_cleanup_fileio(q);
2422 }
2423
2424 /*
2425 * Call vb2_qbuf and give buffer to the driver.
2426 */
2427 memset(&fileio->b, 0, sizeof(fileio->b));
2428 fileio->b.type = q->type;
2429 fileio->b.memory = q->memory;
2430 fileio->b.index = index;
2431 fileio->b.bytesused = buf->pos;
2432 ret = vb2_qbuf(q, &fileio->b);
2433 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2434 if (ret)
2435 goto end;
2436
2437 /*
2438 * Buffer has been queued, update the status
2439 */
2440 buf->pos = 0;
2441 buf->queued = 1;
2442 buf->size = q->bufs[0]->v4l2_planes[0].length;
2443 fileio->q_count += 1;
2444
2445 /*
2446 * Switch to the next buffer
2447 */
2448 fileio->index = (index + 1) % q->num_buffers;
2449
2450 /*
2451 * Start streaming if required.
2452 */
2453 if (!read && !q->streaming) {
2454 ret = vb2_streamon(q, q->type);
2455 if (ret)
2456 goto end;
2457 }
2458 }
2459
2460 /*
2461 * Return proper number of bytes processed.
2462 */
2463 if (ret == 0)
2464 ret = count;
2465end:
2466 /*
2467 * Restore the fileio context and block vb2 ioctl interface.
2468 */
2469 q->fileio = fileio;
2470 return ret;
2471}
2472
2473size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2474 loff_t *ppos, int nonblocking)
2475{
2476 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2477}
2478EXPORT_SYMBOL_GPL(vb2_read);
2479
2480size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
2481 loff_t *ppos, int nonblocking)
2482{
2483 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
2484}
2485EXPORT_SYMBOL_GPL(vb2_write);
2486
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002487
2488/*
2489 * The following functions are not part of the vb2 core API, but are helper
2490 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2491 * and struct vb2_ops.
2492 * They contain boilerplate code that most if not all drivers have to do
2493 * and so they simplify the driver code.
2494 */
2495
2496/* The queue is busy if there is a owner and you are not that owner. */
2497static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
2498{
2499 return vdev->queue->owner && vdev->queue->owner != file->private_data;
2500}
2501
2502/* vb2 ioctl helpers */
2503
2504int vb2_ioctl_reqbufs(struct file *file, void *priv,
2505 struct v4l2_requestbuffers *p)
2506{
2507 struct video_device *vdev = video_devdata(file);
2508 int res = __verify_memory_type(vdev->queue, p->memory, p->type);
2509
2510 if (res)
2511 return res;
2512 if (vb2_queue_is_busy(vdev, file))
2513 return -EBUSY;
2514 res = __reqbufs(vdev->queue, p);
2515 /* If count == 0, then the owner has released all buffers and he
2516 is no longer owner of the queue. Otherwise we have a new owner. */
2517 if (res == 0)
2518 vdev->queue->owner = p->count ? file->private_data : NULL;
2519 return res;
2520}
2521EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
2522
2523int vb2_ioctl_create_bufs(struct file *file, void *priv,
2524 struct v4l2_create_buffers *p)
2525{
2526 struct video_device *vdev = video_devdata(file);
2527 int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
2528
2529 p->index = vdev->queue->num_buffers;
2530 /* If count == 0, then just check if memory and type are valid.
2531 Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2532 if (p->count == 0)
2533 return res != -EBUSY ? res : 0;
2534 if (res)
2535 return res;
2536 if (vb2_queue_is_busy(vdev, file))
2537 return -EBUSY;
2538 res = __create_bufs(vdev->queue, p);
2539 if (res == 0)
2540 vdev->queue->owner = file->private_data;
2541 return res;
2542}
2543EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
2544
2545int vb2_ioctl_prepare_buf(struct file *file, void *priv,
2546 struct v4l2_buffer *p)
2547{
2548 struct video_device *vdev = video_devdata(file);
2549
2550 if (vb2_queue_is_busy(vdev, file))
2551 return -EBUSY;
2552 return vb2_prepare_buf(vdev->queue, p);
2553}
2554EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
2555
2556int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
2557{
2558 struct video_device *vdev = video_devdata(file);
2559
2560 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2561 return vb2_querybuf(vdev->queue, p);
2562}
2563EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
2564
2565int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2566{
2567 struct video_device *vdev = video_devdata(file);
2568
2569 if (vb2_queue_is_busy(vdev, file))
2570 return -EBUSY;
2571 return vb2_qbuf(vdev->queue, p);
2572}
2573EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
2574
2575int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2576{
2577 struct video_device *vdev = video_devdata(file);
2578
2579 if (vb2_queue_is_busy(vdev, file))
2580 return -EBUSY;
2581 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
2582}
2583EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
2584
2585int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
2586{
2587 struct video_device *vdev = video_devdata(file);
2588
2589 if (vb2_queue_is_busy(vdev, file))
2590 return -EBUSY;
2591 return vb2_streamon(vdev->queue, i);
2592}
2593EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
2594
2595int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
2596{
2597 struct video_device *vdev = video_devdata(file);
2598
2599 if (vb2_queue_is_busy(vdev, file))
2600 return -EBUSY;
2601 return vb2_streamoff(vdev->queue, i);
2602}
2603EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
2604
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002605int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
2606{
2607 struct video_device *vdev = video_devdata(file);
2608
2609 if (vb2_queue_is_busy(vdev, file))
2610 return -EBUSY;
2611 return vb2_expbuf(vdev->queue, p);
2612}
2613EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
2614
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002615/* v4l2_file_operations helpers */
2616
2617int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
2618{
2619 struct video_device *vdev = video_devdata(file);
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002620 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2621 int err;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002622
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002623 if (lock && mutex_lock_interruptible(lock))
2624 return -ERESTARTSYS;
2625 err = vb2_mmap(vdev->queue, vma);
2626 if (lock)
2627 mutex_unlock(lock);
2628 return err;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002629}
2630EXPORT_SYMBOL_GPL(vb2_fop_mmap);
2631
2632int vb2_fop_release(struct file *file)
2633{
2634 struct video_device *vdev = video_devdata(file);
2635
2636 if (file->private_data == vdev->queue->owner) {
2637 vb2_queue_release(vdev->queue);
2638 vdev->queue->owner = NULL;
2639 }
2640 return v4l2_fh_release(file);
2641}
2642EXPORT_SYMBOL_GPL(vb2_fop_release);
2643
2644ssize_t vb2_fop_write(struct file *file, char __user *buf,
2645 size_t count, loff_t *ppos)
2646{
2647 struct video_device *vdev = video_devdata(file);
2648 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002649 int err = -EBUSY;
2650
Hans Verkuilcf533732012-07-31 04:02:25 -03002651 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002652 return -ERESTARTSYS;
2653 if (vb2_queue_is_busy(vdev, file))
2654 goto exit;
2655 err = vb2_write(vdev->queue, buf, count, ppos,
2656 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03002657 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002658 vdev->queue->owner = file->private_data;
2659exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03002660 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002661 mutex_unlock(lock);
2662 return err;
2663}
2664EXPORT_SYMBOL_GPL(vb2_fop_write);
2665
2666ssize_t vb2_fop_read(struct file *file, char __user *buf,
2667 size_t count, loff_t *ppos)
2668{
2669 struct video_device *vdev = video_devdata(file);
2670 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002671 int err = -EBUSY;
2672
Hans Verkuilcf533732012-07-31 04:02:25 -03002673 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002674 return -ERESTARTSYS;
2675 if (vb2_queue_is_busy(vdev, file))
2676 goto exit;
2677 err = vb2_read(vdev->queue, buf, count, ppos,
2678 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03002679 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002680 vdev->queue->owner = file->private_data;
2681exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03002682 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002683 mutex_unlock(lock);
2684 return err;
2685}
2686EXPORT_SYMBOL_GPL(vb2_fop_read);
2687
2688unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
2689{
2690 struct video_device *vdev = video_devdata(file);
2691 struct vb2_queue *q = vdev->queue;
2692 struct mutex *lock = q->lock ? q->lock : vdev->lock;
2693 unsigned long req_events = poll_requested_events(wait);
2694 unsigned res;
2695 void *fileio;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002696 bool must_lock = false;
2697
2698 /* Try to be smart: only lock if polling might start fileio,
2699 otherwise locking will only introduce unwanted delays. */
2700 if (q->num_buffers == 0 && q->fileio == NULL) {
2701 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2702 (req_events & (POLLIN | POLLRDNORM)))
2703 must_lock = true;
2704 else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2705 (req_events & (POLLOUT | POLLWRNORM)))
2706 must_lock = true;
2707 }
2708
2709 /* If locking is needed, but this helper doesn't know how, then you
2710 shouldn't be using this helper but you should write your own. */
Hans Verkuilcf533732012-07-31 04:02:25 -03002711 WARN_ON(must_lock && !lock);
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002712
Hans Verkuilcf533732012-07-31 04:02:25 -03002713 if (must_lock && lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002714 return POLLERR;
2715
2716 fileio = q->fileio;
2717
2718 res = vb2_poll(vdev->queue, file, wait);
2719
2720 /* If fileio was started, then we have a new queue owner. */
2721 if (must_lock && !fileio && q->fileio)
2722 q->owner = file->private_data;
Hans Verkuilcf533732012-07-31 04:02:25 -03002723 if (must_lock && lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002724 mutex_unlock(lock);
2725 return res;
2726}
2727EXPORT_SYMBOL_GPL(vb2_fop_poll);
2728
2729#ifndef CONFIG_MMU
2730unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
2731 unsigned long len, unsigned long pgoff, unsigned long flags)
2732{
2733 struct video_device *vdev = video_devdata(file);
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002734 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2735 int ret;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002736
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002737 if (lock && mutex_lock_interruptible(lock))
2738 return -ERESTARTSYS;
2739 ret = vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
2740 if (lock)
2741 mutex_unlock(lock);
2742 return ret;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002743}
2744EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
2745#endif
2746
2747/* vb2_ops helpers. Only use if vq->lock is non-NULL. */
2748
2749void vb2_ops_wait_prepare(struct vb2_queue *vq)
2750{
2751 mutex_unlock(vq->lock);
2752}
2753EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
2754
2755void vb2_ops_wait_finish(struct vb2_queue *vq)
2756{
2757 mutex_lock(vq->lock);
2758}
2759EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
2760
Pawel Osciake23ccc02010-10-11 10:56:41 -03002761MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
Pawel Osciak95072082011-03-13 15:23:32 -03002762MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
Pawel Osciake23ccc02010-10-11 10:56:41 -03002763MODULE_LICENSE("GPL");