blob: e5311cebdbb90b8a6539ff14027f5ecf2ac0f178 [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
22#include <media/videobuf2-core.h>
23
24static int debug;
25module_param(debug, int, 0644);
26
27#define dprintk(level, fmt, arg...) \
28 do { \
29 if (debug >= level) \
30 printk(KERN_DEBUG "vb2: " fmt, ## arg); \
31 } while (0)
32
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030033#define call_memop(q, op, args...) \
Pawel Osciake23ccc02010-10-11 10:56:41 -030034 (((q)->mem_ops->op) ? \
35 ((q)->mem_ops->op(args)) : 0)
36
37#define call_qop(q, op, args...) \
38 (((q)->ops->op) ? ((q)->ops->op(args)) : 0)
39
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -030040#define V4L2_BUFFER_STATE_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -030041 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
42 V4L2_BUF_FLAG_PREPARED)
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -030043
Pawel Osciake23ccc02010-10-11 10:56:41 -030044/**
45 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
46 */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030047static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
Pawel Osciake23ccc02010-10-11 10:56:41 -030048{
49 struct vb2_queue *q = vb->vb2_queue;
50 void *mem_priv;
51 int plane;
52
53 /* Allocate memory for all planes in this buffer */
54 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030055 mem_priv = call_memop(q, alloc, q->alloc_ctx[plane],
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030056 q->plane_sizes[plane]);
Guennadi Liakhovetski62a79432011-03-22 09:24:58 -030057 if (IS_ERR_OR_NULL(mem_priv))
Pawel Osciake23ccc02010-10-11 10:56:41 -030058 goto free;
59
60 /* Associate allocator private data with this plane */
61 vb->planes[plane].mem_priv = mem_priv;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030062 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
Pawel Osciake23ccc02010-10-11 10:56:41 -030063 }
64
65 return 0;
66free:
67 /* Free already allocated memory if one of the allocations failed */
Marek Szyprowskia00d0262011-12-15 05:53:06 -030068 for (; plane > 0; --plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030069 call_memop(q, put, vb->planes[plane - 1].mem_priv);
Marek Szyprowskia00d0262011-12-15 05:53:06 -030070 vb->planes[plane - 1].mem_priv = NULL;
71 }
Pawel Osciake23ccc02010-10-11 10:56:41 -030072
73 return -ENOMEM;
74}
75
76/**
77 * __vb2_buf_mem_free() - free memory of the given buffer
78 */
79static void __vb2_buf_mem_free(struct vb2_buffer *vb)
80{
81 struct vb2_queue *q = vb->vb2_queue;
82 unsigned int plane;
83
84 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030085 call_memop(q, put, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -030086 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskia00d0262011-12-15 05:53:06 -030087 dprintk(3, "Freed plane %d of buffer %d\n", plane,
88 vb->v4l2_buf.index);
Pawel Osciake23ccc02010-10-11 10:56:41 -030089 }
90}
91
92/**
93 * __vb2_buf_userptr_put() - release userspace memory associated with
94 * a USERPTR buffer
95 */
96static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
97{
98 struct vb2_queue *q = vb->vb2_queue;
99 unsigned int plane;
100
101 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300102 if (vb->planes[plane].mem_priv)
103 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
104 vb->planes[plane].mem_priv = NULL;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300105 }
106}
107
108/**
109 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
110 * every buffer on the queue
111 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300112static void __setup_offsets(struct vb2_queue *q, unsigned int n)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300113{
114 unsigned int buffer, plane;
115 struct vb2_buffer *vb;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300116 unsigned long off;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300117
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300118 if (q->num_buffers) {
119 struct v4l2_plane *p;
120 vb = q->bufs[q->num_buffers - 1];
121 p = &vb->v4l2_planes[vb->num_planes - 1];
122 off = PAGE_ALIGN(p->m.mem_offset + p->length);
123 } else {
124 off = 0;
125 }
126
127 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300128 vb = q->bufs[buffer];
129 if (!vb)
130 continue;
131
132 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski49076022011-10-13 07:07:24 -0300133 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
Pawel Osciake23ccc02010-10-11 10:56:41 -0300134 vb->v4l2_planes[plane].m.mem_offset = off;
135
136 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
137 buffer, plane, off);
138
139 off += vb->v4l2_planes[plane].length;
140 off = PAGE_ALIGN(off);
141 }
142 }
143}
144
145/**
146 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
147 * video buffer memory for all buffers/planes on the queue and initializes the
148 * queue
149 *
150 * Returns the number of buffers successfully allocated.
151 */
152static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300153 unsigned int num_buffers, unsigned int num_planes)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300154{
155 unsigned int buffer;
156 struct vb2_buffer *vb;
157 int ret;
158
159 for (buffer = 0; buffer < num_buffers; ++buffer) {
160 /* Allocate videobuf buffer structures */
161 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
162 if (!vb) {
163 dprintk(1, "Memory alloc for buffer struct failed\n");
164 break;
165 }
166
167 /* Length stores number of planes for multiplanar buffers */
168 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
169 vb->v4l2_buf.length = num_planes;
170
171 vb->state = VB2_BUF_STATE_DEQUEUED;
172 vb->vb2_queue = q;
173 vb->num_planes = num_planes;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300174 vb->v4l2_buf.index = q->num_buffers + buffer;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300175 vb->v4l2_buf.type = q->type;
176 vb->v4l2_buf.memory = memory;
177
178 /* Allocate video buffer memory for the MMAP type */
179 if (memory == V4L2_MEMORY_MMAP) {
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300180 ret = __vb2_buf_mem_alloc(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300181 if (ret) {
182 dprintk(1, "Failed allocating memory for "
183 "buffer %d\n", buffer);
184 kfree(vb);
185 break;
186 }
187 /*
188 * Call the driver-provided buffer initialization
189 * callback, if given. An error in initialization
190 * results in queue setup failure.
191 */
192 ret = call_qop(q, buf_init, vb);
193 if (ret) {
194 dprintk(1, "Buffer %d %p initialization"
195 " failed\n", buffer, vb);
196 __vb2_buf_mem_free(vb);
197 kfree(vb);
198 break;
199 }
200 }
201
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300202 q->bufs[q->num_buffers + buffer] = vb;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300203 }
204
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300205 __setup_offsets(q, buffer);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300206
207 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300208 buffer, num_planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300209
210 return buffer;
211}
212
213/**
214 * __vb2_free_mem() - release all video buffer memory for a given queue
215 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300216static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300217{
218 unsigned int buffer;
219 struct vb2_buffer *vb;
220
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300221 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
222 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300223 vb = q->bufs[buffer];
224 if (!vb)
225 continue;
226
227 /* Free MMAP buffers or release USERPTR buffers */
228 if (q->memory == V4L2_MEMORY_MMAP)
229 __vb2_buf_mem_free(vb);
230 else
231 __vb2_buf_userptr_put(vb);
232 }
233}
234
235/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300236 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
237 * related information, if no buffers are left return the queue to an
238 * uninitialized state. Might be called even if the queue has already been freed.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300239 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300240static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300241{
242 unsigned int buffer;
243
244 /* Call driver-provided cleanup function for each buffer, if provided */
245 if (q->ops->buf_cleanup) {
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300246 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
247 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300248 if (NULL == q->bufs[buffer])
249 continue;
250 q->ops->buf_cleanup(q->bufs[buffer]);
251 }
252 }
253
254 /* Release video buffer memory */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300255 __vb2_free_mem(q, buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300256
257 /* Free videobuf buffers */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300258 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
259 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300260 kfree(q->bufs[buffer]);
261 q->bufs[buffer] = NULL;
262 }
263
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300264 q->num_buffers -= buffers;
265 if (!q->num_buffers)
266 q->memory = 0;
Marek Szyprowskibd50d992011-10-25 03:07:59 -0300267 INIT_LIST_HEAD(&q->queued_list);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300268}
269
270/**
271 * __verify_planes_array() - verify that the planes array passed in struct
272 * v4l2_buffer from userspace can be safely used
273 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300274static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300275{
276 /* Is memory for copying plane information present? */
277 if (NULL == b->m.planes) {
278 dprintk(1, "Multi-planar buffer passed but "
279 "planes array not provided\n");
280 return -EINVAL;
281 }
282
283 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
284 dprintk(1, "Incorrect planes array length, "
285 "expected %d, got %d\n", vb->num_planes, b->length);
286 return -EINVAL;
287 }
288
289 return 0;
290}
291
292/**
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300293 * __buffer_in_use() - return true if the buffer is in use and
294 * the queue cannot be freed (by the means of REQBUFS(0)) call
295 */
296static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
297{
298 unsigned int plane;
299 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski2c2dd6a2011-10-12 13:09:53 -0300300 void *mem_priv = vb->planes[plane].mem_priv;
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300301 /*
302 * If num_users() has not been provided, call_memop
303 * will return 0, apparently nobody cares about this
304 * case anyway. If num_users() returns more than 1,
305 * we are not the only user of the plane's memory.
306 */
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300307 if (mem_priv && call_memop(q, num_users, mem_priv) > 1)
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300308 return true;
309 }
310 return false;
311}
312
313/**
314 * __buffers_in_use() - return true if any buffers on the queue are in use and
315 * the queue cannot be freed (by the means of REQBUFS(0)) call
316 */
317static bool __buffers_in_use(struct vb2_queue *q)
318{
319 unsigned int buffer;
320 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
321 if (__buffer_in_use(q, q->bufs[buffer]))
322 return true;
323 }
324 return false;
325}
326
327/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300328 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
329 * returned to userspace
330 */
331static int __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
332{
333 struct vb2_queue *q = vb->vb2_queue;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300334 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300335
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300336 /* Copy back data such as timestamp, flags, input, etc. */
Pawel Osciake23ccc02010-10-11 10:56:41 -0300337 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
338 b->input = vb->v4l2_buf.input;
339 b->reserved = vb->v4l2_buf.reserved;
340
341 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
342 ret = __verify_planes_array(vb, b);
343 if (ret)
344 return ret;
345
346 /*
347 * Fill in plane-related data if userspace provided an array
348 * for it. The memory and size is verified above.
349 */
350 memcpy(b->m.planes, vb->v4l2_planes,
351 b->length * sizeof(struct v4l2_plane));
352 } else {
353 /*
354 * We use length and offset in v4l2_planes array even for
355 * single-planar buffers, but userspace does not.
356 */
357 b->length = vb->v4l2_planes[0].length;
358 b->bytesused = vb->v4l2_planes[0].bytesused;
359 if (q->memory == V4L2_MEMORY_MMAP)
360 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
361 else if (q->memory == V4L2_MEMORY_USERPTR)
362 b->m.userptr = vb->v4l2_planes[0].m.userptr;
363 }
364
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300365 /*
366 * Clear any buffer state related flags.
367 */
368 b->flags &= ~V4L2_BUFFER_STATE_FLAGS;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300369
370 switch (vb->state) {
371 case VB2_BUF_STATE_QUEUED:
372 case VB2_BUF_STATE_ACTIVE:
373 b->flags |= V4L2_BUF_FLAG_QUEUED;
374 break;
375 case VB2_BUF_STATE_ERROR:
376 b->flags |= V4L2_BUF_FLAG_ERROR;
377 /* fall through */
378 case VB2_BUF_STATE_DONE:
379 b->flags |= V4L2_BUF_FLAG_DONE;
380 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300381 case VB2_BUF_STATE_PREPARED:
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300382 b->flags |= V4L2_BUF_FLAG_PREPARED;
383 break;
384 case VB2_BUF_STATE_DEQUEUED:
Pawel Osciake23ccc02010-10-11 10:56:41 -0300385 /* nothing */
386 break;
387 }
388
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300389 if (__buffer_in_use(q, vb))
Pawel Osciake23ccc02010-10-11 10:56:41 -0300390 b->flags |= V4L2_BUF_FLAG_MAPPED;
391
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300392 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300393}
394
395/**
396 * vb2_querybuf() - query video buffer information
397 * @q: videobuf queue
398 * @b: buffer struct passed from userspace to vidioc_querybuf handler
399 * in driver
400 *
401 * Should be called from vidioc_querybuf ioctl handler in driver.
402 * This function will verify the passed v4l2_buffer structure and fill the
403 * relevant information for the userspace.
404 *
405 * The return values from this function are intended to be directly returned
406 * from vidioc_querybuf handler in driver.
407 */
408int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
409{
410 struct vb2_buffer *vb;
411
412 if (b->type != q->type) {
413 dprintk(1, "querybuf: wrong buffer type\n");
414 return -EINVAL;
415 }
416
417 if (b->index >= q->num_buffers) {
418 dprintk(1, "querybuf: buffer index out of range\n");
419 return -EINVAL;
420 }
421 vb = q->bufs[b->index];
422
423 return __fill_v4l2_buffer(vb, b);
424}
425EXPORT_SYMBOL(vb2_querybuf);
426
427/**
428 * __verify_userptr_ops() - verify that all memory operations required for
429 * USERPTR queue type have been provided
430 */
431static int __verify_userptr_ops(struct vb2_queue *q)
432{
433 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
434 !q->mem_ops->put_userptr)
435 return -EINVAL;
436
437 return 0;
438}
439
440/**
441 * __verify_mmap_ops() - verify that all memory operations required for
442 * MMAP queue type have been provided
443 */
444static int __verify_mmap_ops(struct vb2_queue *q)
445{
446 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
447 !q->mem_ops->put || !q->mem_ops->mmap)
448 return -EINVAL;
449
450 return 0;
451}
452
453/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300454 * vb2_reqbufs() - Initiate streaming
455 * @q: videobuf2 queue
456 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
457 *
458 * Should be called from vidioc_reqbufs ioctl handler of a driver.
459 * This function:
460 * 1) verifies streaming parameters passed from the userspace,
461 * 2) sets up the queue,
462 * 3) negotiates number of buffers and planes per buffer with the driver
463 * to be used during streaming,
464 * 4) allocates internal buffer structures (struct vb2_buffer), according to
465 * the agreed parameters,
466 * 5) for MMAP memory type, allocates actual video memory, using the
467 * memory handling/allocation routines provided during queue initialization
468 *
469 * If req->count is 0, all the memory will be freed instead.
470 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
471 * and the queue is not busy, memory will be reallocated.
472 *
473 * The return values from this function are intended to be directly returned
474 * from vidioc_reqbufs handler in driver.
475 */
476int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
477{
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300478 unsigned int num_buffers, allocated_buffers, num_planes = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300479 int ret = 0;
480
Marek Szyprowskib25748f2010-12-06 05:56:55 -0300481 if (q->fileio) {
482 dprintk(1, "reqbufs: file io in progress\n");
483 return -EBUSY;
484 }
485
Pawel Osciake23ccc02010-10-11 10:56:41 -0300486 if (req->memory != V4L2_MEMORY_MMAP
487 && req->memory != V4L2_MEMORY_USERPTR) {
488 dprintk(1, "reqbufs: unsupported memory type\n");
489 return -EINVAL;
490 }
491
492 if (req->type != q->type) {
493 dprintk(1, "reqbufs: requested type is incorrect\n");
494 return -EINVAL;
495 }
496
497 if (q->streaming) {
498 dprintk(1, "reqbufs: streaming active\n");
499 return -EBUSY;
500 }
501
502 /*
503 * Make sure all the required memory ops for given memory type
504 * are available.
505 */
506 if (req->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
507 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
508 return -EINVAL;
509 }
510
511 if (req->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
512 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
513 return -EINVAL;
514 }
515
Marek Szyprowski4eb02932011-03-11 06:07:27 -0300516 /*
517 * If the same number of buffers and memory access method is requested
518 * then return immediately.
519 */
520 if (q->memory == req->memory && req->count == q->num_buffers)
521 return 0;
522
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300523 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300524 /*
525 * We already have buffers allocated, so first check if they
526 * are not in use and can be freed.
527 */
528 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
529 dprintk(1, "reqbufs: memory in use, cannot free\n");
530 return -EBUSY;
531 }
532
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300533 __vb2_queue_free(q, q->num_buffers);
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300534
535 /*
536 * In case of REQBUFS(0) return immediately without calling
537 * driver's queue_setup() callback and allocating resources.
538 */
539 if (req->count == 0)
540 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300541 }
542
543 /*
544 * Make sure the requested values and current defaults are sane.
545 */
546 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300547 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
Pawel Osciake23ccc02010-10-11 10:56:41 -0300548 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
Marek Szyprowski13b14092011-04-14 07:17:44 -0300549 q->memory = req->memory;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300550
551 /*
552 * Ask the driver how many buffers and planes per buffer it requires.
553 * Driver also sets the size and allocator context for each plane.
554 */
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300555 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300556 q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300557 if (ret)
558 return ret;
559
560 /* Finally, allocate buffers and video memory */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300561 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
Marek Szyprowski66072d42011-06-28 08:29:02 -0300562 if (ret == 0) {
563 dprintk(1, "Memory allocation failed\n");
564 return -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300565 }
566
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300567 allocated_buffers = ret;
568
Pawel Osciake23ccc02010-10-11 10:56:41 -0300569 /*
570 * Check if driver can handle the allocated number of buffers.
571 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300572 if (allocated_buffers < num_buffers) {
573 num_buffers = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300574
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300575 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
576 &num_planes, q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300577
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300578 if (!ret && allocated_buffers < num_buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300579 ret = -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300580
581 /*
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300582 * Either the driver has accepted a smaller number of buffers,
583 * or .queue_setup() returned an error
Pawel Osciake23ccc02010-10-11 10:56:41 -0300584 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300585 }
586
587 q->num_buffers = allocated_buffers;
588
589 if (ret < 0) {
590 __vb2_queue_free(q, allocated_buffers);
591 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300592 }
593
Pawel Osciake23ccc02010-10-11 10:56:41 -0300594 /*
595 * Return the number of successfully allocated buffers
596 * to the userspace.
597 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300598 req->count = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300599
600 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300601}
602EXPORT_SYMBOL_GPL(vb2_reqbufs);
603
604/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300605 * vb2_create_bufs() - Allocate buffers and any required auxiliary structs
606 * @q: videobuf2 queue
607 * @create: creation parameters, passed from userspace to vidioc_create_bufs
608 * handler in driver
609 *
610 * Should be called from vidioc_create_bufs ioctl handler of a driver.
611 * This function:
612 * 1) verifies parameter sanity
613 * 2) calls the .queue_setup() queue operation
614 * 3) performs any necessary memory allocations
615 *
616 * The return values from this function are intended to be directly returned
617 * from vidioc_create_bufs handler in driver.
618 */
619int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
620{
621 unsigned int num_planes = 0, num_buffers, allocated_buffers;
622 int ret = 0;
623
624 if (q->fileio) {
625 dprintk(1, "%s(): file io in progress\n", __func__);
626 return -EBUSY;
627 }
628
629 if (create->memory != V4L2_MEMORY_MMAP
630 && create->memory != V4L2_MEMORY_USERPTR) {
631 dprintk(1, "%s(): unsupported memory type\n", __func__);
632 return -EINVAL;
633 }
634
635 if (create->format.type != q->type) {
636 dprintk(1, "%s(): requested type is incorrect\n", __func__);
637 return -EINVAL;
638 }
639
640 /*
641 * Make sure all the required memory ops for given memory type
642 * are available.
643 */
644 if (create->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
645 dprintk(1, "%s(): MMAP for current setup unsupported\n", __func__);
646 return -EINVAL;
647 }
648
649 if (create->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
650 dprintk(1, "%s(): USERPTR for current setup unsupported\n", __func__);
651 return -EINVAL;
652 }
653
654 if (q->num_buffers == VIDEO_MAX_FRAME) {
655 dprintk(1, "%s(): maximum number of buffers already allocated\n",
656 __func__);
657 return -ENOBUFS;
658 }
659
660 create->index = q->num_buffers;
661
662 if (!q->num_buffers) {
663 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
664 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
665 q->memory = create->memory;
666 }
667
668 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
669
670 /*
671 * Ask the driver, whether the requested number of buffers, planes per
672 * buffer and their sizes are acceptable
673 */
674 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
675 &num_planes, q->plane_sizes, q->alloc_ctx);
676 if (ret)
677 return ret;
678
679 /* Finally, allocate buffers and video memory */
680 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
681 num_planes);
682 if (ret < 0) {
683 dprintk(1, "Memory allocation failed with error: %d\n", ret);
684 return ret;
685 }
686
687 allocated_buffers = ret;
688
689 /*
690 * Check if driver can handle the so far allocated number of buffers.
691 */
692 if (ret < num_buffers) {
693 num_buffers = ret;
694
695 /*
696 * q->num_buffers contains the total number of buffers, that the
697 * queue driver has set up
698 */
699 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
700 &num_planes, q->plane_sizes, q->alloc_ctx);
701
702 if (!ret && allocated_buffers < num_buffers)
703 ret = -ENOMEM;
704
705 /*
706 * Either the driver has accepted a smaller number of buffers,
707 * or .queue_setup() returned an error
708 */
709 }
710
711 q->num_buffers += allocated_buffers;
712
713 if (ret < 0) {
714 __vb2_queue_free(q, allocated_buffers);
715 return ret;
716 }
717
718 /*
719 * Return the number of successfully allocated buffers
720 * to the userspace.
721 */
722 create->count = allocated_buffers;
723
724 return 0;
725}
726EXPORT_SYMBOL_GPL(vb2_create_bufs);
727
728/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300729 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
730 * @vb: vb2_buffer to which the plane in question belongs to
731 * @plane_no: plane number for which the address is to be returned
732 *
733 * This function returns a kernel virtual address of a given plane if
734 * such a mapping exist, NULL otherwise.
735 */
736void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
737{
738 struct vb2_queue *q = vb->vb2_queue;
739
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300740 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300741 return NULL;
742
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300743 return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300744
745}
746EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
747
748/**
749 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
750 * @vb: vb2_buffer to which the plane in question belongs to
751 * @plane_no: plane number for which the cookie is to be returned
752 *
753 * This function returns an allocator specific cookie for a given plane if
754 * available, NULL otherwise. The allocator should provide some simple static
755 * inline function, which would convert this cookie to the allocator specific
756 * type that can be used directly by the driver to access the buffer. This can
757 * be for example physical address, pointer to scatter list or IOMMU mapping.
758 */
759void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
760{
761 struct vb2_queue *q = vb->vb2_queue;
762
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300763 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300764 return NULL;
765
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300766 return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300767}
768EXPORT_SYMBOL_GPL(vb2_plane_cookie);
769
770/**
771 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
772 * @vb: vb2_buffer returned from the driver
773 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
774 * or VB2_BUF_STATE_ERROR if the operation finished with an error
775 *
776 * This function should be called by the driver after a hardware operation on
777 * a buffer is finished and the buffer may be returned to userspace. The driver
778 * cannot use this buffer anymore until it is queued back to it by videobuf
779 * by the means of buf_queue callback. Only buffers previously queued to the
780 * driver by buf_queue can be passed to this function.
781 */
782void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
783{
784 struct vb2_queue *q = vb->vb2_queue;
785 unsigned long flags;
786
787 if (vb->state != VB2_BUF_STATE_ACTIVE)
788 return;
789
790 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
791 return;
792
793 dprintk(4, "Done processing on buffer %d, state: %d\n",
794 vb->v4l2_buf.index, vb->state);
795
796 /* Add the buffer to the done buffers list */
797 spin_lock_irqsave(&q->done_lock, flags);
798 vb->state = state;
799 list_add_tail(&vb->done_entry, &q->done_list);
800 atomic_dec(&q->queued_count);
801 spin_unlock_irqrestore(&q->done_lock, flags);
802
803 /* Inform any processes that may be waiting for buffers */
804 wake_up(&q->done_wq);
805}
806EXPORT_SYMBOL_GPL(vb2_buffer_done);
807
808/**
809 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in
810 * a v4l2_buffer by the userspace
811 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300812static int __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
Pawel Osciake23ccc02010-10-11 10:56:41 -0300813 struct v4l2_plane *v4l2_planes)
814{
815 unsigned int plane;
816 int ret;
817
818 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
819 /*
820 * Verify that the userspace gave us a valid array for
821 * plane information.
822 */
823 ret = __verify_planes_array(vb, b);
824 if (ret)
825 return ret;
826
827 /* Fill in driver-provided information for OUTPUT types */
828 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
829 /*
830 * Will have to go up to b->length when API starts
831 * accepting variable number of planes.
832 */
833 for (plane = 0; plane < vb->num_planes; ++plane) {
834 v4l2_planes[plane].bytesused =
835 b->m.planes[plane].bytesused;
836 v4l2_planes[plane].data_offset =
837 b->m.planes[plane].data_offset;
838 }
839 }
840
841 if (b->memory == V4L2_MEMORY_USERPTR) {
842 for (plane = 0; plane < vb->num_planes; ++plane) {
843 v4l2_planes[plane].m.userptr =
844 b->m.planes[plane].m.userptr;
845 v4l2_planes[plane].length =
846 b->m.planes[plane].length;
847 }
848 }
849 } else {
850 /*
851 * Single-planar buffers do not use planes array,
852 * so fill in relevant v4l2_buffer struct fields instead.
853 * In videobuf we use our internal V4l2_planes struct for
854 * single-planar buffers as well, for simplicity.
855 */
856 if (V4L2_TYPE_IS_OUTPUT(b->type))
857 v4l2_planes[0].bytesused = b->bytesused;
858
859 if (b->memory == V4L2_MEMORY_USERPTR) {
860 v4l2_planes[0].m.userptr = b->m.userptr;
861 v4l2_planes[0].length = b->length;
862 }
863 }
864
865 vb->v4l2_buf.field = b->field;
866 vb->v4l2_buf.timestamp = b->timestamp;
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300867 vb->v4l2_buf.input = b->input;
868 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300869
870 return 0;
871}
872
873/**
874 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
875 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300876static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300877{
878 struct v4l2_plane planes[VIDEO_MAX_PLANES];
879 struct vb2_queue *q = vb->vb2_queue;
880 void *mem_priv;
881 unsigned int plane;
882 int ret;
883 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
884
885 /* Verify and copy relevant information provided by the userspace */
886 ret = __fill_vb2_buffer(vb, b, planes);
887 if (ret)
888 return ret;
889
890 for (plane = 0; plane < vb->num_planes; ++plane) {
891 /* Skip the plane if already verified */
Marek Szyprowskif0b7c7f2011-11-16 15:09:40 -0300892 if (vb->v4l2_planes[plane].m.userptr &&
893 vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
Pawel Osciake23ccc02010-10-11 10:56:41 -0300894 && vb->v4l2_planes[plane].length == planes[plane].length)
895 continue;
896
897 dprintk(3, "qbuf: userspace address for plane %d changed, "
898 "reacquiring memory\n", plane);
899
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300900 /* Check if the provided plane buffer is large enough */
901 if (planes[plane].length < q->plane_sizes[plane]) {
Marek Szyprowski4c2625d2011-10-03 03:21:45 -0300902 ret = -EINVAL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300903 goto err;
904 }
905
Pawel Osciake23ccc02010-10-11 10:56:41 -0300906 /* Release previously acquired memory if present */
907 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300908 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300909
910 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300911 vb->v4l2_planes[plane].m.userptr = 0;
912 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300913
914 /* Acquire each plane's memory */
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300915 mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
916 planes[plane].m.userptr,
917 planes[plane].length, write);
918 if (IS_ERR_OR_NULL(mem_priv)) {
919 dprintk(1, "qbuf: failed acquiring userspace "
Pawel Osciake23ccc02010-10-11 10:56:41 -0300920 "memory for plane %d\n", plane);
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300921 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
922 goto err;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300923 }
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300924 vb->planes[plane].mem_priv = mem_priv;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300925 }
926
927 /*
928 * Call driver-specific initialization on the newly acquired buffer,
929 * if provided.
930 */
931 ret = call_qop(q, buf_init, vb);
932 if (ret) {
933 dprintk(1, "qbuf: buffer initialization failed\n");
934 goto err;
935 }
936
937 /*
938 * Now that everything is in order, copy relevant information
939 * provided by userspace.
940 */
941 for (plane = 0; plane < vb->num_planes; ++plane)
942 vb->v4l2_planes[plane] = planes[plane];
943
944 return 0;
945err:
946 /* In case of errors, release planes that were already acquired */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300947 for (plane = 0; plane < vb->num_planes; ++plane) {
948 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300949 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300950 vb->planes[plane].mem_priv = NULL;
951 vb->v4l2_planes[plane].m.userptr = 0;
952 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300953 }
954
955 return ret;
956}
957
958/**
959 * __qbuf_mmap() - handle qbuf of an MMAP buffer
960 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300961static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300962{
963 return __fill_vb2_buffer(vb, b, vb->v4l2_planes);
964}
965
966/**
967 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
968 */
969static void __enqueue_in_driver(struct vb2_buffer *vb)
970{
971 struct vb2_queue *q = vb->vb2_queue;
972
973 vb->state = VB2_BUF_STATE_ACTIVE;
974 atomic_inc(&q->queued_count);
975 q->ops->buf_queue(vb);
976}
977
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300978static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300979{
980 struct vb2_queue *q = vb->vb2_queue;
981 int ret;
982
983 switch (q->memory) {
984 case V4L2_MEMORY_MMAP:
985 ret = __qbuf_mmap(vb, b);
986 break;
987 case V4L2_MEMORY_USERPTR:
988 ret = __qbuf_userptr(vb, b);
989 break;
990 default:
991 WARN(1, "Invalid queue type\n");
992 ret = -EINVAL;
993 }
994
995 if (!ret)
996 ret = call_qop(q, buf_prepare, vb);
997 if (ret)
998 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
999 else
1000 vb->state = VB2_BUF_STATE_PREPARED;
1001
1002 return ret;
1003}
1004
Pawel Osciake23ccc02010-10-11 10:56:41 -03001005/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001006 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1007 * @q: videobuf2 queue
1008 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1009 * handler in driver
1010 *
1011 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1012 * This function:
1013 * 1) verifies the passed buffer,
1014 * 2) calls buf_prepare callback in the driver (if provided), in which
1015 * driver-specific buffer initialization can be performed,
1016 *
1017 * The return values from this function are intended to be directly returned
1018 * from vidioc_prepare_buf handler in driver.
1019 */
1020int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1021{
1022 struct vb2_buffer *vb;
1023 int ret;
1024
1025 if (q->fileio) {
1026 dprintk(1, "%s(): file io in progress\n", __func__);
1027 return -EBUSY;
1028 }
1029
1030 if (b->type != q->type) {
1031 dprintk(1, "%s(): invalid buffer type\n", __func__);
1032 return -EINVAL;
1033 }
1034
1035 if (b->index >= q->num_buffers) {
1036 dprintk(1, "%s(): buffer index out of range\n", __func__);
1037 return -EINVAL;
1038 }
1039
1040 vb = q->bufs[b->index];
1041 if (NULL == vb) {
1042 /* Should never happen */
1043 dprintk(1, "%s(): buffer is NULL\n", __func__);
1044 return -EINVAL;
1045 }
1046
1047 if (b->memory != q->memory) {
1048 dprintk(1, "%s(): invalid memory type\n", __func__);
1049 return -EINVAL;
1050 }
1051
1052 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1053 dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state);
1054 return -EINVAL;
1055 }
1056
1057 ret = __buf_prepare(vb, b);
1058 if (ret < 0)
1059 return ret;
1060
1061 __fill_v4l2_buffer(vb, b);
1062
1063 return 0;
1064}
1065EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1066
1067/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001068 * vb2_qbuf() - Queue a buffer from userspace
1069 * @q: videobuf2 queue
1070 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1071 * in driver
1072 *
1073 * Should be called from vidioc_qbuf ioctl handler of a driver.
1074 * This function:
1075 * 1) verifies the passed buffer,
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001076 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1077 * which driver-specific buffer initialization can be performed,
Pawel Osciake23ccc02010-10-11 10:56:41 -03001078 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1079 * callback for processing.
1080 *
1081 * The return values from this function are intended to be directly returned
1082 * from vidioc_qbuf handler in driver.
1083 */
1084int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1085{
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001086 struct rw_semaphore *mmap_sem = NULL;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001087 struct vb2_buffer *vb;
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001088 int ret = 0;
1089
1090 /*
1091 * In case of user pointer buffers vb2 allocator needs to get direct
1092 * access to userspace pages. This requires getting read access on
1093 * mmap semaphore in the current process structure. The same
1094 * semaphore is taken before calling mmap operation, while both mmap
1095 * and qbuf are called by the driver or v4l2 core with driver's lock
1096 * held. To avoid a AB-BA deadlock (mmap_sem then driver's lock in
1097 * mmap and driver's lock then mmap_sem in qbuf) the videobuf2 core
1098 * release driver's lock, takes mmap_sem and then takes again driver's
1099 * lock.
1100 *
1101 * To avoid race with other vb2 calls, which might be called after
1102 * releasing driver's lock, this operation is performed at the
1103 * beggining of qbuf processing. This way the queue status is
1104 * consistent after getting driver's lock back.
1105 */
Mauro Carvalho Chehab57e43cf2011-12-30 16:10:44 -02001106 if (q->memory == V4L2_MEMORY_USERPTR) {
Deva Ramasubramaniana80e7c82014-03-04 13:16:29 -08001107 bool mm_exists = !!current->mm;
1108
1109 mmap_sem = mm_exists ? &current->mm->mmap_sem : NULL;
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001110 call_qop(q, wait_prepare, q);
Deva Ramasubramaniana80e7c82014-03-04 13:16:29 -08001111 /* kthreads have no userspace, hence no pages to lock */
1112 if (mmap_sem)
1113 down_read(mmap_sem);
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001114 call_qop(q, wait_finish, q);
1115 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001116
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001117 if (q->fileio) {
1118 dprintk(1, "qbuf: file io in progress\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001119 ret = -EBUSY;
1120 goto unlock;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001121 }
1122
Pawel Osciake23ccc02010-10-11 10:56:41 -03001123 if (b->type != q->type) {
1124 dprintk(1, "qbuf: invalid buffer type\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001125 ret = -EINVAL;
1126 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001127 }
1128
1129 if (b->index >= q->num_buffers) {
1130 dprintk(1, "qbuf: buffer index out of range\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001131 ret = -EINVAL;
1132 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001133 }
1134
1135 vb = q->bufs[b->index];
1136 if (NULL == vb) {
1137 /* Should never happen */
1138 dprintk(1, "qbuf: buffer is NULL\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001139 ret = -EINVAL;
1140 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001141 }
1142
1143 if (b->memory != q->memory) {
1144 dprintk(1, "qbuf: invalid memory type\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001145 ret = -EINVAL;
1146 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001147 }
1148
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001149 switch (vb->state) {
1150 case VB2_BUF_STATE_DEQUEUED:
1151 ret = __buf_prepare(vb, b);
1152 if (ret)
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001153 goto unlock;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001154 case VB2_BUF_STATE_PREPARED:
1155 break;
1156 default:
Pawel Osciake23ccc02010-10-11 10:56:41 -03001157 dprintk(1, "qbuf: buffer already in use\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001158 ret = -EINVAL;
1159 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001160 }
1161
Pawel Osciake23ccc02010-10-11 10:56:41 -03001162 /*
1163 * Add to the queued buffers list, a buffer will stay on it until
1164 * dequeued in dqbuf.
1165 */
1166 list_add_tail(&vb->queued_entry, &q->queued_list);
1167 vb->state = VB2_BUF_STATE_QUEUED;
1168
1169 /*
1170 * If already streaming, give the buffer to driver for processing.
1171 * If not, the buffer will be given to driver on next streamon.
1172 */
1173 if (q->streaming)
1174 __enqueue_in_driver(vb);
1175
Guennadi Liakhovetski21db3e02011-09-28 07:23:27 -03001176 /* Fill buffer information for the userspace */
1177 __fill_v4l2_buffer(vb, b);
1178
Pawel Osciake23ccc02010-10-11 10:56:41 -03001179 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001180unlock:
1181 if (mmap_sem)
1182 up_read(mmap_sem);
1183 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001184}
1185EXPORT_SYMBOL_GPL(vb2_qbuf);
1186
1187/**
1188 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1189 * for dequeuing
1190 *
1191 * Will sleep if required for nonblocking == false.
1192 */
1193static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1194{
1195 /*
1196 * All operations on vb_done_list are performed under done_lock
1197 * spinlock protection. However, buffers may be removed from
1198 * it and returned to userspace only while holding both driver's
1199 * lock and the done_lock spinlock. Thus we can be sure that as
1200 * long as we hold the driver's lock, the list will remain not
1201 * empty if list_empty() check succeeds.
1202 */
1203
1204 for (;;) {
1205 int ret;
1206
1207 if (!q->streaming) {
1208 dprintk(1, "Streaming off, will not wait for buffers\n");
1209 return -EINVAL;
1210 }
1211
1212 if (!list_empty(&q->done_list)) {
1213 /*
1214 * Found a buffer that we were waiting for.
1215 */
1216 break;
1217 }
1218
1219 if (nonblocking) {
1220 dprintk(1, "Nonblocking and no buffers to dequeue, "
1221 "will not wait\n");
1222 return -EAGAIN;
1223 }
1224
1225 /*
1226 * We are streaming and blocking, wait for another buffer to
1227 * become ready or for streamoff. Driver's lock is released to
1228 * allow streamoff or qbuf to be called while waiting.
1229 */
1230 call_qop(q, wait_prepare, q);
1231
1232 /*
1233 * All locks have been released, it is safe to sleep now.
1234 */
1235 dprintk(3, "Will sleep waiting for buffers\n");
1236 ret = wait_event_interruptible(q->done_wq,
1237 !list_empty(&q->done_list) || !q->streaming);
1238
1239 /*
1240 * We need to reevaluate both conditions again after reacquiring
1241 * the locks or return an error if one occurred.
1242 */
1243 call_qop(q, wait_finish, q);
1244 if (ret)
1245 return ret;
1246 }
1247 return 0;
1248}
1249
1250/**
1251 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1252 *
1253 * Will sleep if required for nonblocking == false.
1254 */
1255static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1256 int nonblocking)
1257{
1258 unsigned long flags;
1259 int ret;
1260
1261 /*
1262 * Wait for at least one buffer to become available on the done_list.
1263 */
1264 ret = __vb2_wait_for_done_vb(q, nonblocking);
1265 if (ret)
1266 return ret;
1267
1268 /*
1269 * Driver's lock has been held since we last verified that done_list
1270 * is not empty, so no need for another list_empty(done_list) check.
1271 */
1272 spin_lock_irqsave(&q->done_lock, flags);
1273 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1274 list_del(&(*vb)->done_entry);
1275 spin_unlock_irqrestore(&q->done_lock, flags);
1276
1277 return 0;
1278}
1279
1280/**
1281 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1282 * @q: videobuf2 queue
1283 *
1284 * This function will wait until all buffers that have been given to the driver
1285 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1286 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1287 * taken, for example from stop_streaming() callback.
1288 */
1289int vb2_wait_for_all_buffers(struct vb2_queue *q)
1290{
1291 if (!q->streaming) {
1292 dprintk(1, "Streaming off, will not wait for buffers\n");
1293 return -EINVAL;
1294 }
1295
1296 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1297 return 0;
1298}
1299EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1300
1301/**
1302 * vb2_dqbuf() - Dequeue a buffer to the userspace
1303 * @q: videobuf2 queue
1304 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1305 * in driver
1306 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1307 * buffers ready for dequeuing are present. Normally the driver
1308 * would be passing (file->f_flags & O_NONBLOCK) here
1309 *
1310 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1311 * This function:
1312 * 1) verifies the passed buffer,
1313 * 2) calls buf_finish callback in the driver (if provided), in which
1314 * driver can perform any additional operations that may be required before
1315 * returning the buffer to userspace, such as cache sync,
1316 * 3) the buffer struct members are filled with relevant information for
1317 * the userspace.
1318 *
1319 * The return values from this function are intended to be directly returned
1320 * from vidioc_dqbuf handler in driver.
1321 */
1322int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1323{
1324 struct vb2_buffer *vb = NULL;
1325 int ret;
1326
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001327 if (q->fileio) {
1328 dprintk(1, "dqbuf: file io in progress\n");
1329 return -EBUSY;
1330 }
1331
Pawel Osciake23ccc02010-10-11 10:56:41 -03001332 if (b->type != q->type) {
1333 dprintk(1, "dqbuf: invalid buffer type\n");
1334 return -EINVAL;
1335 }
1336
1337 ret = __vb2_get_done_vb(q, &vb, nonblocking);
1338 if (ret < 0) {
1339 dprintk(1, "dqbuf: error getting next done buffer\n");
1340 return ret;
1341 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001342 ret = call_qop(q, buf_finish, vb);
1343 if (ret) {
1344 dprintk(1, "dqbuf: buffer finish failed\n");
1345 return ret;
1346 }
1347
1348 switch (vb->state) {
1349 case VB2_BUF_STATE_DONE:
1350 dprintk(3, "dqbuf: Returning done buffer\n");
1351 break;
1352 case VB2_BUF_STATE_ERROR:
1353 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1354 break;
1355 default:
1356 dprintk(1, "dqbuf: Invalid buffer state\n");
1357 return -EINVAL;
1358 }
1359
1360 /* Fill buffer information for the userspace */
1361 __fill_v4l2_buffer(vb, b);
1362 /* Remove from videobuf queue */
1363 list_del(&vb->queued_entry);
1364
1365 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1366 vb->v4l2_buf.index, vb->state);
1367
1368 vb->state = VB2_BUF_STATE_DEQUEUED;
1369 return 0;
1370}
1371EXPORT_SYMBOL_GPL(vb2_dqbuf);
1372
1373/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001374 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1375 *
1376 * Removes all queued buffers from driver's queue and all buffers queued by
1377 * userspace from videobuf's queue. Returns to state after reqbufs.
1378 */
1379static void __vb2_queue_cancel(struct vb2_queue *q)
1380{
1381 unsigned int i;
1382
1383 /*
1384 * Tell driver to stop all transactions and release all queued
1385 * buffers.
1386 */
1387 if (q->streaming)
1388 call_qop(q, stop_streaming, q);
1389 q->streaming = 0;
1390
1391 /*
1392 * Remove all buffers from videobuf's list...
1393 */
1394 INIT_LIST_HEAD(&q->queued_list);
1395 /*
1396 * ...and done list; userspace will not receive any buffers it
1397 * has not already dequeued before initiating cancel.
1398 */
1399 INIT_LIST_HEAD(&q->done_list);
Marek Szyprowskiafdea8b2011-06-10 08:58:42 -03001400 atomic_set(&q->queued_count, 0);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001401 wake_up_all(&q->done_wq);
1402
1403 /*
1404 * Reinitialize all buffers for next use.
1405 */
1406 for (i = 0; i < q->num_buffers; ++i)
1407 q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED;
1408}
1409
1410/**
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001411 * vb2_streamon - start streaming
1412 * @q: videobuf2 queue
1413 * @type: type argument passed from userspace to vidioc_streamon handler
1414 *
1415 * Should be called from vidioc_streamon handler of a driver.
1416 * This function:
1417 * 1) verifies current state
1418 * 2) passes any previously queued buffers to the driver and starts streaming
1419 *
1420 * The return values from this function are intended to be directly returned
1421 * from vidioc_streamon handler in the driver.
1422 */
1423int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1424{
1425 struct vb2_buffer *vb;
1426 int ret;
1427
1428 if (q->fileio) {
1429 dprintk(1, "streamon: file io in progress\n");
1430 return -EBUSY;
1431 }
1432
1433 if (type != q->type) {
1434 dprintk(1, "streamon: invalid stream type\n");
1435 return -EINVAL;
1436 }
1437
1438 if (q->streaming) {
1439 dprintk(1, "streamon: already streaming\n");
1440 return -EBUSY;
1441 }
1442
1443 /*
1444 * If any buffers were queued before streamon,
1445 * we can now pass them to driver for processing.
1446 */
1447 list_for_each_entry(vb, &q->queued_list, queued_entry)
1448 __enqueue_in_driver(vb);
1449
1450 /*
1451 * Let driver notice that streaming state has been enabled.
1452 */
1453 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1454 if (ret) {
1455 dprintk(1, "streamon: driver refused to start streaming\n");
1456 __vb2_queue_cancel(q);
1457 return ret;
1458 }
1459
1460 q->streaming = 1;
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001461 dprintk(3, "Streamon successful\n");
1462 return 0;
1463}
1464EXPORT_SYMBOL_GPL(vb2_streamon);
1465
1466
1467/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001468 * vb2_streamoff - stop streaming
1469 * @q: videobuf2 queue
1470 * @type: type argument passed from userspace to vidioc_streamoff handler
1471 *
1472 * Should be called from vidioc_streamoff handler of a driver.
1473 * This function:
1474 * 1) verifies current state,
1475 * 2) stop streaming and dequeues any queued buffers, including those previously
1476 * passed to the driver (after waiting for the driver to finish).
1477 *
1478 * This call can be used for pausing playback.
1479 * The return values from this function are intended to be directly returned
1480 * from vidioc_streamoff handler in the driver
1481 */
1482int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1483{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001484 if (q->fileio) {
1485 dprintk(1, "streamoff: file io in progress\n");
1486 return -EBUSY;
1487 }
1488
Pawel Osciake23ccc02010-10-11 10:56:41 -03001489 if (type != q->type) {
1490 dprintk(1, "streamoff: invalid stream type\n");
1491 return -EINVAL;
1492 }
1493
1494 if (!q->streaming) {
1495 dprintk(1, "streamoff: not streaming\n");
1496 return -EINVAL;
1497 }
1498
1499 /*
1500 * Cancel will pause streaming and remove all buffers from the driver
1501 * and videobuf, effectively returning control over them to userspace.
1502 */
1503 __vb2_queue_cancel(q);
1504
1505 dprintk(3, "Streamoff successful\n");
1506 return 0;
1507}
1508EXPORT_SYMBOL_GPL(vb2_streamoff);
1509
1510/**
1511 * __find_plane_by_offset() - find plane associated with the given offset off
1512 */
1513static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1514 unsigned int *_buffer, unsigned int *_plane)
1515{
1516 struct vb2_buffer *vb;
1517 unsigned int buffer, plane;
1518
1519 /*
1520 * Go over all buffers and their planes, comparing the given offset
1521 * with an offset assigned to each plane. If a match is found,
1522 * return its buffer and plane numbers.
1523 */
1524 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1525 vb = q->bufs[buffer];
1526
1527 for (plane = 0; plane < vb->num_planes; ++plane) {
1528 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1529 *_buffer = buffer;
1530 *_plane = plane;
1531 return 0;
1532 }
1533 }
1534 }
1535
1536 return -EINVAL;
1537}
1538
1539/**
1540 * vb2_mmap() - map video buffers into application address space
1541 * @q: videobuf2 queue
1542 * @vma: vma passed to the mmap file operation handler in the driver
1543 *
1544 * Should be called from mmap file operation handler of a driver.
1545 * This function maps one plane of one of the available video buffers to
1546 * userspace. To map whole video memory allocated on reqbufs, this function
1547 * has to be called once per each plane per each buffer previously allocated.
1548 *
1549 * When the userspace application calls mmap, it passes to it an offset returned
1550 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1551 * a "cookie", which is then used to identify the plane to be mapped.
1552 * This function finds a plane with a matching offset and a mapping is performed
1553 * by the means of a provided memory operation.
1554 *
1555 * The return values from this function are intended to be directly returned
1556 * from the mmap handler in driver.
1557 */
1558int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1559{
1560 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001561 struct vb2_buffer *vb;
1562 unsigned int buffer, plane;
1563 int ret;
1564
1565 if (q->memory != V4L2_MEMORY_MMAP) {
1566 dprintk(1, "Queue is not currently set up for mmap\n");
1567 return -EINVAL;
1568 }
1569
1570 /*
1571 * Check memory area access mode.
1572 */
1573 if (!(vma->vm_flags & VM_SHARED)) {
1574 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1575 return -EINVAL;
1576 }
1577 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1578 if (!(vma->vm_flags & VM_WRITE)) {
1579 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1580 return -EINVAL;
1581 }
1582 } else {
1583 if (!(vma->vm_flags & VM_READ)) {
1584 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1585 return -EINVAL;
1586 }
1587 }
1588
1589 /*
1590 * Find the plane corresponding to the offset passed by userspace.
1591 */
1592 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1593 if (ret)
1594 return ret;
1595
1596 vb = q->bufs[buffer];
Pawel Osciake23ccc02010-10-11 10:56:41 -03001597
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001598 ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001599 if (ret)
1600 return ret;
1601
Pawel Osciake23ccc02010-10-11 10:56:41 -03001602 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1603 return 0;
1604}
1605EXPORT_SYMBOL_GPL(vb2_mmap);
1606
Scott Jiang6f524ec2011-09-21 09:25:23 -03001607#ifndef CONFIG_MMU
1608unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1609 unsigned long addr,
1610 unsigned long len,
1611 unsigned long pgoff,
1612 unsigned long flags)
1613{
1614 unsigned long off = pgoff << PAGE_SHIFT;
1615 struct vb2_buffer *vb;
1616 unsigned int buffer, plane;
1617 int ret;
1618
1619 if (q->memory != V4L2_MEMORY_MMAP) {
1620 dprintk(1, "Queue is not currently set up for mmap\n");
1621 return -EINVAL;
1622 }
1623
1624 /*
1625 * Find the plane corresponding to the offset passed by userspace.
1626 */
1627 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1628 if (ret)
1629 return ret;
1630
1631 vb = q->bufs[buffer];
1632
1633 return (unsigned long)vb2_plane_vaddr(vb, plane);
1634}
1635EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
1636#endif
1637
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001638static int __vb2_init_fileio(struct vb2_queue *q, int read);
1639static int __vb2_cleanup_fileio(struct vb2_queue *q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001640
1641/**
1642 * vb2_poll() - implements poll userspace operation
1643 * @q: videobuf2 queue
1644 * @file: file argument passed to the poll file operation handler
1645 * @wait: wait argument passed to the poll file operation handler
1646 *
1647 * This function implements poll file operation handler for a driver.
1648 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1649 * be informed that the file descriptor of a video device is available for
1650 * reading.
1651 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1652 * will be reported as available for writing.
1653 *
1654 * The return values from this function are intended to be directly returned
1655 * from poll handler in driver.
1656 */
1657unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
1658{
1659 unsigned long flags;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001660 unsigned int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001661 struct vb2_buffer *vb = NULL;
1662
1663 /*
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001664 * Start file I/O emulator only if streaming API has not been used yet.
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001665 */
1666 if (q->num_buffers == 0 && q->fileio == NULL) {
1667 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ)) {
1668 ret = __vb2_init_fileio(q, 1);
1669 if (ret)
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001670 return POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001671 }
1672 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE)) {
1673 ret = __vb2_init_fileio(q, 0);
1674 if (ret)
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001675 return POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001676 /*
1677 * Write to OUTPUT queue can be done immediately.
1678 */
1679 return POLLOUT | POLLWRNORM;
1680 }
1681 }
1682
1683 /*
Pawel Osciake23ccc02010-10-11 10:56:41 -03001684 * There is nothing to wait for if no buffers have already been queued.
1685 */
1686 if (list_empty(&q->queued_list))
1687 return POLLERR;
1688
1689 poll_wait(file, &q->done_wq, wait);
1690
1691 /*
1692 * Take first buffer available for dequeuing.
1693 */
1694 spin_lock_irqsave(&q->done_lock, flags);
1695 if (!list_empty(&q->done_list))
1696 vb = list_first_entry(&q->done_list, struct vb2_buffer,
1697 done_entry);
1698 spin_unlock_irqrestore(&q->done_lock, flags);
1699
1700 if (vb && (vb->state == VB2_BUF_STATE_DONE
1701 || vb->state == VB2_BUF_STATE_ERROR)) {
1702 return (V4L2_TYPE_IS_OUTPUT(q->type)) ? POLLOUT | POLLWRNORM :
1703 POLLIN | POLLRDNORM;
1704 }
1705 return 0;
1706}
1707EXPORT_SYMBOL_GPL(vb2_poll);
1708
1709/**
1710 * vb2_queue_init() - initialize a videobuf2 queue
1711 * @q: videobuf2 queue; this structure should be allocated in driver
1712 *
1713 * The vb2_queue structure should be allocated by the driver. The driver is
1714 * responsible of clearing it's content and setting initial values for some
1715 * required entries before calling this function.
1716 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
1717 * to the struct vb2_queue description in include/media/videobuf2-core.h
1718 * for more information.
1719 */
1720int vb2_queue_init(struct vb2_queue *q)
1721{
1722 BUG_ON(!q);
1723 BUG_ON(!q->ops);
1724 BUG_ON(!q->mem_ops);
1725 BUG_ON(!q->type);
1726 BUG_ON(!q->io_modes);
1727
1728 BUG_ON(!q->ops->queue_setup);
1729 BUG_ON(!q->ops->buf_queue);
1730
1731 INIT_LIST_HEAD(&q->queued_list);
1732 INIT_LIST_HEAD(&q->done_list);
1733 spin_lock_init(&q->done_lock);
1734 init_waitqueue_head(&q->done_wq);
1735
1736 if (q->buf_struct_size == 0)
1737 q->buf_struct_size = sizeof(struct vb2_buffer);
1738
1739 return 0;
1740}
1741EXPORT_SYMBOL_GPL(vb2_queue_init);
1742
1743/**
1744 * vb2_queue_release() - stop streaming, release the queue and free memory
1745 * @q: videobuf2 queue
1746 *
1747 * This function stops streaming and performs necessary clean ups, including
1748 * freeing video buffer memory. The driver is responsible for freeing
1749 * the vb2_queue structure itself.
1750 */
1751void vb2_queue_release(struct vb2_queue *q)
1752{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001753 __vb2_cleanup_fileio(q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001754 __vb2_queue_cancel(q);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001755 __vb2_queue_free(q, q->num_buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001756}
1757EXPORT_SYMBOL_GPL(vb2_queue_release);
1758
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001759/**
1760 * struct vb2_fileio_buf - buffer context used by file io emulator
1761 *
1762 * vb2 provides a compatibility layer and emulator of file io (read and
1763 * write) calls on top of streaming API. This structure is used for
1764 * tracking context related to the buffers.
1765 */
1766struct vb2_fileio_buf {
1767 void *vaddr;
1768 unsigned int size;
1769 unsigned int pos;
1770 unsigned int queued:1;
1771};
1772
1773/**
1774 * struct vb2_fileio_data - queue context used by file io emulator
1775 *
1776 * vb2 provides a compatibility layer and emulator of file io (read and
1777 * write) calls on top of streaming API. For proper operation it required
1778 * this structure to save the driver state between each call of the read
1779 * or write function.
1780 */
1781struct vb2_fileio_data {
1782 struct v4l2_requestbuffers req;
1783 struct v4l2_buffer b;
1784 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
1785 unsigned int index;
1786 unsigned int q_count;
1787 unsigned int dq_count;
1788 unsigned int flags;
1789};
1790
1791/**
1792 * __vb2_init_fileio() - initialize file io emulator
1793 * @q: videobuf2 queue
1794 * @read: mode selector (1 means read, 0 means write)
1795 */
1796static int __vb2_init_fileio(struct vb2_queue *q, int read)
1797{
1798 struct vb2_fileio_data *fileio;
1799 int i, ret;
1800 unsigned int count = 0;
1801
1802 /*
1803 * Sanity check
1804 */
1805 if ((read && !(q->io_modes & VB2_READ)) ||
1806 (!read && !(q->io_modes & VB2_WRITE)))
1807 BUG();
1808
1809 /*
1810 * Check if device supports mapping buffers to kernel virtual space.
1811 */
1812 if (!q->mem_ops->vaddr)
1813 return -EBUSY;
1814
1815 /*
1816 * Check if streaming api has not been already activated.
1817 */
1818 if (q->streaming || q->num_buffers > 0)
1819 return -EBUSY;
1820
1821 /*
1822 * Start with count 1, driver can increase it in queue_setup()
1823 */
1824 count = 1;
1825
1826 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
1827 (read) ? "read" : "write", count, q->io_flags);
1828
1829 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
1830 if (fileio == NULL)
1831 return -ENOMEM;
1832
1833 fileio->flags = q->io_flags;
1834
1835 /*
1836 * Request buffers and use MMAP type to force driver
1837 * to allocate buffers by itself.
1838 */
1839 fileio->req.count = count;
1840 fileio->req.memory = V4L2_MEMORY_MMAP;
1841 fileio->req.type = q->type;
1842 ret = vb2_reqbufs(q, &fileio->req);
1843 if (ret)
1844 goto err_kfree;
1845
1846 /*
1847 * Check if plane_count is correct
1848 * (multiplane buffers are not supported).
1849 */
1850 if (q->bufs[0]->num_planes != 1) {
1851 fileio->req.count = 0;
1852 ret = -EBUSY;
1853 goto err_reqbufs;
1854 }
1855
1856 /*
1857 * Get kernel address of each buffer.
1858 */
1859 for (i = 0; i < q->num_buffers; i++) {
1860 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
1861 if (fileio->bufs[i].vaddr == NULL)
1862 goto err_reqbufs;
1863 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
1864 }
1865
1866 /*
1867 * Read mode requires pre queuing of all buffers.
1868 */
1869 if (read) {
1870 /*
1871 * Queue all buffers.
1872 */
1873 for (i = 0; i < q->num_buffers; i++) {
1874 struct v4l2_buffer *b = &fileio->b;
1875 memset(b, 0, sizeof(*b));
1876 b->type = q->type;
1877 b->memory = q->memory;
1878 b->index = i;
1879 ret = vb2_qbuf(q, b);
1880 if (ret)
1881 goto err_reqbufs;
1882 fileio->bufs[i].queued = 1;
1883 }
1884
1885 /*
1886 * Start streaming.
1887 */
1888 ret = vb2_streamon(q, q->type);
1889 if (ret)
1890 goto err_reqbufs;
1891 }
1892
1893 q->fileio = fileio;
1894
1895 return ret;
1896
1897err_reqbufs:
1898 vb2_reqbufs(q, &fileio->req);
1899
1900err_kfree:
1901 kfree(fileio);
1902 return ret;
1903}
1904
1905/**
1906 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1907 * @q: videobuf2 queue
1908 */
1909static int __vb2_cleanup_fileio(struct vb2_queue *q)
1910{
1911 struct vb2_fileio_data *fileio = q->fileio;
1912
1913 if (fileio) {
1914 /*
1915 * Hack fileio context to enable direct calls to vb2 ioctl
1916 * interface.
1917 */
1918 q->fileio = NULL;
1919
1920 vb2_streamoff(q, q->type);
1921 fileio->req.count = 0;
1922 vb2_reqbufs(q, &fileio->req);
1923 kfree(fileio);
1924 dprintk(3, "file io emulator closed\n");
1925 }
1926 return 0;
1927}
1928
1929/**
1930 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1931 * @q: videobuf2 queue
1932 * @data: pointed to target userspace buffer
1933 * @count: number of bytes to read or write
1934 * @ppos: file handle position tracking pointer
1935 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1936 * @read: access mode selector (1 means read, 0 means write)
1937 */
1938static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
1939 loff_t *ppos, int nonblock, int read)
1940{
1941 struct vb2_fileio_data *fileio;
1942 struct vb2_fileio_buf *buf;
1943 int ret, index;
1944
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03001945 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001946 read ? "read" : "write", (long)*ppos, count,
1947 nonblock ? "non" : "");
1948
1949 if (!data)
1950 return -EINVAL;
1951
1952 /*
1953 * Initialize emulator on first call.
1954 */
1955 if (!q->fileio) {
1956 ret = __vb2_init_fileio(q, read);
1957 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
1958 if (ret)
1959 return ret;
1960 }
1961 fileio = q->fileio;
1962
1963 /*
1964 * Hack fileio context to enable direct calls to vb2 ioctl interface.
1965 * The pointer will be restored before returning from this function.
1966 */
1967 q->fileio = NULL;
1968
1969 index = fileio->index;
1970 buf = &fileio->bufs[index];
1971
1972 /*
1973 * Check if we need to dequeue the buffer.
1974 */
1975 if (buf->queued) {
1976 struct vb2_buffer *vb;
1977
1978 /*
1979 * Call vb2_dqbuf to get buffer back.
1980 */
1981 memset(&fileio->b, 0, sizeof(fileio->b));
1982 fileio->b.type = q->type;
1983 fileio->b.memory = q->memory;
1984 fileio->b.index = index;
1985 ret = vb2_dqbuf(q, &fileio->b, nonblock);
1986 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
1987 if (ret)
1988 goto end;
1989 fileio->dq_count += 1;
1990
1991 /*
1992 * Get number of bytes filled by the driver
1993 */
1994 vb = q->bufs[index];
1995 buf->size = vb2_get_plane_payload(vb, 0);
1996 buf->queued = 0;
1997 }
1998
1999 /*
2000 * Limit count on last few bytes of the buffer.
2001 */
2002 if (buf->pos + count > buf->size) {
2003 count = buf->size - buf->pos;
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002004 dprintk(5, "reducing read count: %zd\n", count);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002005 }
2006
2007 /*
2008 * Transfer data to userspace.
2009 */
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002010 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002011 count, index, buf->pos);
2012 if (read)
2013 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2014 else
2015 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2016 if (ret) {
2017 dprintk(3, "file io: error copying data\n");
2018 ret = -EFAULT;
2019 goto end;
2020 }
2021
2022 /*
2023 * Update counters.
2024 */
2025 buf->pos += count;
2026 *ppos += count;
2027
2028 /*
2029 * Queue next buffer if required.
2030 */
2031 if (buf->pos == buf->size ||
2032 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2033 /*
2034 * Check if this is the last buffer to read.
2035 */
2036 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2037 fileio->dq_count == 1) {
2038 dprintk(3, "file io: read limit reached\n");
2039 /*
2040 * Restore fileio pointer and release the context.
2041 */
2042 q->fileio = fileio;
2043 return __vb2_cleanup_fileio(q);
2044 }
2045
2046 /*
2047 * Call vb2_qbuf and give buffer to the driver.
2048 */
2049 memset(&fileio->b, 0, sizeof(fileio->b));
2050 fileio->b.type = q->type;
2051 fileio->b.memory = q->memory;
2052 fileio->b.index = index;
2053 fileio->b.bytesused = buf->pos;
2054 ret = vb2_qbuf(q, &fileio->b);
2055 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2056 if (ret)
2057 goto end;
2058
2059 /*
2060 * Buffer has been queued, update the status
2061 */
2062 buf->pos = 0;
2063 buf->queued = 1;
2064 buf->size = q->bufs[0]->v4l2_planes[0].length;
2065 fileio->q_count += 1;
2066
2067 /*
2068 * Switch to the next buffer
2069 */
2070 fileio->index = (index + 1) % q->num_buffers;
2071
2072 /*
2073 * Start streaming if required.
2074 */
2075 if (!read && !q->streaming) {
2076 ret = vb2_streamon(q, q->type);
2077 if (ret)
2078 goto end;
2079 }
2080 }
2081
2082 /*
2083 * Return proper number of bytes processed.
2084 */
2085 if (ret == 0)
2086 ret = count;
2087end:
2088 /*
2089 * Restore the fileio context and block vb2 ioctl interface.
2090 */
2091 q->fileio = fileio;
2092 return ret;
2093}
2094
2095size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2096 loff_t *ppos, int nonblocking)
2097{
2098 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2099}
2100EXPORT_SYMBOL_GPL(vb2_read);
2101
2102size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
2103 loff_t *ppos, int nonblocking)
2104{
2105 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
2106}
2107EXPORT_SYMBOL_GPL(vb2_write);
2108
Pawel Osciake23ccc02010-10-11 10:56:41 -03002109MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
Pawel Osciak95072082011-03-13 15:23:32 -03002110MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
Pawel Osciake23ccc02010-10-11 10:56:41 -03002111MODULE_LICENSE("GPL");