blob: 668cc733ee234ae780f843ad3d219f78f4fd2b11 [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 Ramasubramanian705d2152012-07-24 18:04:40 -07001107 mmap_sem = &current->active_mm->mmap_sem;
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001108 call_qop(q, wait_prepare, q);
1109 down_read(mmap_sem);
1110 call_qop(q, wait_finish, q);
1111 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001112
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001113 if (q->fileio) {
1114 dprintk(1, "qbuf: file io in progress\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001115 ret = -EBUSY;
1116 goto unlock;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001117 }
1118
Pawel Osciake23ccc02010-10-11 10:56:41 -03001119 if (b->type != q->type) {
1120 dprintk(1, "qbuf: invalid buffer type\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001121 ret = -EINVAL;
1122 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001123 }
1124
1125 if (b->index >= q->num_buffers) {
1126 dprintk(1, "qbuf: buffer index out of range\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001127 ret = -EINVAL;
1128 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001129 }
1130
1131 vb = q->bufs[b->index];
1132 if (NULL == vb) {
1133 /* Should never happen */
1134 dprintk(1, "qbuf: buffer is NULL\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001135 ret = -EINVAL;
1136 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001137 }
1138
1139 if (b->memory != q->memory) {
1140 dprintk(1, "qbuf: invalid memory type\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001141 ret = -EINVAL;
1142 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001143 }
1144
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001145 switch (vb->state) {
1146 case VB2_BUF_STATE_DEQUEUED:
1147 ret = __buf_prepare(vb, b);
1148 if (ret)
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001149 goto unlock;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001150 case VB2_BUF_STATE_PREPARED:
1151 break;
1152 default:
Pawel Osciake23ccc02010-10-11 10:56:41 -03001153 dprintk(1, "qbuf: buffer already in use\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001154 ret = -EINVAL;
1155 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001156 }
1157
Pawel Osciake23ccc02010-10-11 10:56:41 -03001158 /*
1159 * Add to the queued buffers list, a buffer will stay on it until
1160 * dequeued in dqbuf.
1161 */
1162 list_add_tail(&vb->queued_entry, &q->queued_list);
1163 vb->state = VB2_BUF_STATE_QUEUED;
1164
1165 /*
1166 * If already streaming, give the buffer to driver for processing.
1167 * If not, the buffer will be given to driver on next streamon.
1168 */
1169 if (q->streaming)
1170 __enqueue_in_driver(vb);
1171
Guennadi Liakhovetski21db3e02011-09-28 07:23:27 -03001172 /* Fill buffer information for the userspace */
1173 __fill_v4l2_buffer(vb, b);
1174
Pawel Osciake23ccc02010-10-11 10:56:41 -03001175 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001176unlock:
1177 if (mmap_sem)
1178 up_read(mmap_sem);
1179 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001180}
1181EXPORT_SYMBOL_GPL(vb2_qbuf);
1182
1183/**
1184 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1185 * for dequeuing
1186 *
1187 * Will sleep if required for nonblocking == false.
1188 */
1189static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1190{
1191 /*
1192 * All operations on vb_done_list are performed under done_lock
1193 * spinlock protection. However, buffers may be removed from
1194 * it and returned to userspace only while holding both driver's
1195 * lock and the done_lock spinlock. Thus we can be sure that as
1196 * long as we hold the driver's lock, the list will remain not
1197 * empty if list_empty() check succeeds.
1198 */
1199
1200 for (;;) {
1201 int ret;
1202
1203 if (!q->streaming) {
1204 dprintk(1, "Streaming off, will not wait for buffers\n");
1205 return -EINVAL;
1206 }
1207
1208 if (!list_empty(&q->done_list)) {
1209 /*
1210 * Found a buffer that we were waiting for.
1211 */
1212 break;
1213 }
1214
1215 if (nonblocking) {
1216 dprintk(1, "Nonblocking and no buffers to dequeue, "
1217 "will not wait\n");
1218 return -EAGAIN;
1219 }
1220
1221 /*
1222 * We are streaming and blocking, wait for another buffer to
1223 * become ready or for streamoff. Driver's lock is released to
1224 * allow streamoff or qbuf to be called while waiting.
1225 */
1226 call_qop(q, wait_prepare, q);
1227
1228 /*
1229 * All locks have been released, it is safe to sleep now.
1230 */
1231 dprintk(3, "Will sleep waiting for buffers\n");
1232 ret = wait_event_interruptible(q->done_wq,
1233 !list_empty(&q->done_list) || !q->streaming);
1234
1235 /*
1236 * We need to reevaluate both conditions again after reacquiring
1237 * the locks or return an error if one occurred.
1238 */
1239 call_qop(q, wait_finish, q);
1240 if (ret)
1241 return ret;
1242 }
1243 return 0;
1244}
1245
1246/**
1247 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1248 *
1249 * Will sleep if required for nonblocking == false.
1250 */
1251static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1252 int nonblocking)
1253{
1254 unsigned long flags;
1255 int ret;
1256
1257 /*
1258 * Wait for at least one buffer to become available on the done_list.
1259 */
1260 ret = __vb2_wait_for_done_vb(q, nonblocking);
1261 if (ret)
1262 return ret;
1263
1264 /*
1265 * Driver's lock has been held since we last verified that done_list
1266 * is not empty, so no need for another list_empty(done_list) check.
1267 */
1268 spin_lock_irqsave(&q->done_lock, flags);
1269 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1270 list_del(&(*vb)->done_entry);
1271 spin_unlock_irqrestore(&q->done_lock, flags);
1272
1273 return 0;
1274}
1275
1276/**
1277 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1278 * @q: videobuf2 queue
1279 *
1280 * This function will wait until all buffers that have been given to the driver
1281 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1282 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1283 * taken, for example from stop_streaming() callback.
1284 */
1285int vb2_wait_for_all_buffers(struct vb2_queue *q)
1286{
1287 if (!q->streaming) {
1288 dprintk(1, "Streaming off, will not wait for buffers\n");
1289 return -EINVAL;
1290 }
1291
1292 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1293 return 0;
1294}
1295EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1296
1297/**
1298 * vb2_dqbuf() - Dequeue a buffer to the userspace
1299 * @q: videobuf2 queue
1300 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1301 * in driver
1302 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1303 * buffers ready for dequeuing are present. Normally the driver
1304 * would be passing (file->f_flags & O_NONBLOCK) here
1305 *
1306 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1307 * This function:
1308 * 1) verifies the passed buffer,
1309 * 2) calls buf_finish callback in the driver (if provided), in which
1310 * driver can perform any additional operations that may be required before
1311 * returning the buffer to userspace, such as cache sync,
1312 * 3) the buffer struct members are filled with relevant information for
1313 * the userspace.
1314 *
1315 * The return values from this function are intended to be directly returned
1316 * from vidioc_dqbuf handler in driver.
1317 */
1318int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1319{
1320 struct vb2_buffer *vb = NULL;
1321 int ret;
1322
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001323 if (q->fileio) {
1324 dprintk(1, "dqbuf: file io in progress\n");
1325 return -EBUSY;
1326 }
1327
Pawel Osciake23ccc02010-10-11 10:56:41 -03001328 if (b->type != q->type) {
1329 dprintk(1, "dqbuf: invalid buffer type\n");
1330 return -EINVAL;
1331 }
1332
1333 ret = __vb2_get_done_vb(q, &vb, nonblocking);
1334 if (ret < 0) {
1335 dprintk(1, "dqbuf: error getting next done buffer\n");
1336 return ret;
1337 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001338 ret = call_qop(q, buf_finish, vb);
1339 if (ret) {
1340 dprintk(1, "dqbuf: buffer finish failed\n");
1341 return ret;
1342 }
1343
1344 switch (vb->state) {
1345 case VB2_BUF_STATE_DONE:
1346 dprintk(3, "dqbuf: Returning done buffer\n");
1347 break;
1348 case VB2_BUF_STATE_ERROR:
1349 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1350 break;
1351 default:
1352 dprintk(1, "dqbuf: Invalid buffer state\n");
1353 return -EINVAL;
1354 }
1355
1356 /* Fill buffer information for the userspace */
1357 __fill_v4l2_buffer(vb, b);
1358 /* Remove from videobuf queue */
1359 list_del(&vb->queued_entry);
1360
1361 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1362 vb->v4l2_buf.index, vb->state);
1363
1364 vb->state = VB2_BUF_STATE_DEQUEUED;
1365 return 0;
1366}
1367EXPORT_SYMBOL_GPL(vb2_dqbuf);
1368
1369/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001370 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1371 *
1372 * Removes all queued buffers from driver's queue and all buffers queued by
1373 * userspace from videobuf's queue. Returns to state after reqbufs.
1374 */
1375static void __vb2_queue_cancel(struct vb2_queue *q)
1376{
1377 unsigned int i;
1378
1379 /*
1380 * Tell driver to stop all transactions and release all queued
1381 * buffers.
1382 */
1383 if (q->streaming)
1384 call_qop(q, stop_streaming, q);
1385 q->streaming = 0;
1386
1387 /*
1388 * Remove all buffers from videobuf's list...
1389 */
1390 INIT_LIST_HEAD(&q->queued_list);
1391 /*
1392 * ...and done list; userspace will not receive any buffers it
1393 * has not already dequeued before initiating cancel.
1394 */
1395 INIT_LIST_HEAD(&q->done_list);
Marek Szyprowskiafdea8b2011-06-10 08:58:42 -03001396 atomic_set(&q->queued_count, 0);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001397 wake_up_all(&q->done_wq);
1398
1399 /*
1400 * Reinitialize all buffers for next use.
1401 */
1402 for (i = 0; i < q->num_buffers; ++i)
1403 q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED;
1404}
1405
1406/**
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001407 * vb2_streamon - start streaming
1408 * @q: videobuf2 queue
1409 * @type: type argument passed from userspace to vidioc_streamon handler
1410 *
1411 * Should be called from vidioc_streamon handler of a driver.
1412 * This function:
1413 * 1) verifies current state
1414 * 2) passes any previously queued buffers to the driver and starts streaming
1415 *
1416 * The return values from this function are intended to be directly returned
1417 * from vidioc_streamon handler in the driver.
1418 */
1419int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1420{
1421 struct vb2_buffer *vb;
1422 int ret;
1423
1424 if (q->fileio) {
1425 dprintk(1, "streamon: file io in progress\n");
1426 return -EBUSY;
1427 }
1428
1429 if (type != q->type) {
1430 dprintk(1, "streamon: invalid stream type\n");
1431 return -EINVAL;
1432 }
1433
1434 if (q->streaming) {
1435 dprintk(1, "streamon: already streaming\n");
1436 return -EBUSY;
1437 }
1438
1439 /*
1440 * If any buffers were queued before streamon,
1441 * we can now pass them to driver for processing.
1442 */
1443 list_for_each_entry(vb, &q->queued_list, queued_entry)
1444 __enqueue_in_driver(vb);
1445
1446 /*
1447 * Let driver notice that streaming state has been enabled.
1448 */
1449 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1450 if (ret) {
1451 dprintk(1, "streamon: driver refused to start streaming\n");
1452 __vb2_queue_cancel(q);
1453 return ret;
1454 }
1455
1456 q->streaming = 1;
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001457 dprintk(3, "Streamon successful\n");
1458 return 0;
1459}
1460EXPORT_SYMBOL_GPL(vb2_streamon);
1461
1462
1463/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001464 * vb2_streamoff - stop streaming
1465 * @q: videobuf2 queue
1466 * @type: type argument passed from userspace to vidioc_streamoff handler
1467 *
1468 * Should be called from vidioc_streamoff handler of a driver.
1469 * This function:
1470 * 1) verifies current state,
1471 * 2) stop streaming and dequeues any queued buffers, including those previously
1472 * passed to the driver (after waiting for the driver to finish).
1473 *
1474 * This call can be used for pausing playback.
1475 * The return values from this function are intended to be directly returned
1476 * from vidioc_streamoff handler in the driver
1477 */
1478int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1479{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001480 if (q->fileio) {
1481 dprintk(1, "streamoff: file io in progress\n");
1482 return -EBUSY;
1483 }
1484
Pawel Osciake23ccc02010-10-11 10:56:41 -03001485 if (type != q->type) {
1486 dprintk(1, "streamoff: invalid stream type\n");
1487 return -EINVAL;
1488 }
1489
1490 if (!q->streaming) {
1491 dprintk(1, "streamoff: not streaming\n");
1492 return -EINVAL;
1493 }
1494
1495 /*
1496 * Cancel will pause streaming and remove all buffers from the driver
1497 * and videobuf, effectively returning control over them to userspace.
1498 */
1499 __vb2_queue_cancel(q);
1500
1501 dprintk(3, "Streamoff successful\n");
1502 return 0;
1503}
1504EXPORT_SYMBOL_GPL(vb2_streamoff);
1505
1506/**
1507 * __find_plane_by_offset() - find plane associated with the given offset off
1508 */
1509static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1510 unsigned int *_buffer, unsigned int *_plane)
1511{
1512 struct vb2_buffer *vb;
1513 unsigned int buffer, plane;
1514
1515 /*
1516 * Go over all buffers and their planes, comparing the given offset
1517 * with an offset assigned to each plane. If a match is found,
1518 * return its buffer and plane numbers.
1519 */
1520 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1521 vb = q->bufs[buffer];
1522
1523 for (plane = 0; plane < vb->num_planes; ++plane) {
1524 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1525 *_buffer = buffer;
1526 *_plane = plane;
1527 return 0;
1528 }
1529 }
1530 }
1531
1532 return -EINVAL;
1533}
1534
1535/**
1536 * vb2_mmap() - map video buffers into application address space
1537 * @q: videobuf2 queue
1538 * @vma: vma passed to the mmap file operation handler in the driver
1539 *
1540 * Should be called from mmap file operation handler of a driver.
1541 * This function maps one plane of one of the available video buffers to
1542 * userspace. To map whole video memory allocated on reqbufs, this function
1543 * has to be called once per each plane per each buffer previously allocated.
1544 *
1545 * When the userspace application calls mmap, it passes to it an offset returned
1546 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1547 * a "cookie", which is then used to identify the plane to be mapped.
1548 * This function finds a plane with a matching offset and a mapping is performed
1549 * by the means of a provided memory operation.
1550 *
1551 * The return values from this function are intended to be directly returned
1552 * from the mmap handler in driver.
1553 */
1554int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1555{
1556 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001557 struct vb2_buffer *vb;
1558 unsigned int buffer, plane;
1559 int ret;
1560
1561 if (q->memory != V4L2_MEMORY_MMAP) {
1562 dprintk(1, "Queue is not currently set up for mmap\n");
1563 return -EINVAL;
1564 }
1565
1566 /*
1567 * Check memory area access mode.
1568 */
1569 if (!(vma->vm_flags & VM_SHARED)) {
1570 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1571 return -EINVAL;
1572 }
1573 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1574 if (!(vma->vm_flags & VM_WRITE)) {
1575 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1576 return -EINVAL;
1577 }
1578 } else {
1579 if (!(vma->vm_flags & VM_READ)) {
1580 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1581 return -EINVAL;
1582 }
1583 }
1584
1585 /*
1586 * Find the plane corresponding to the offset passed by userspace.
1587 */
1588 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1589 if (ret)
1590 return ret;
1591
1592 vb = q->bufs[buffer];
Pawel Osciake23ccc02010-10-11 10:56:41 -03001593
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001594 ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001595 if (ret)
1596 return ret;
1597
Pawel Osciake23ccc02010-10-11 10:56:41 -03001598 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1599 return 0;
1600}
1601EXPORT_SYMBOL_GPL(vb2_mmap);
1602
Scott Jiang6f524ec2011-09-21 09:25:23 -03001603#ifndef CONFIG_MMU
1604unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1605 unsigned long addr,
1606 unsigned long len,
1607 unsigned long pgoff,
1608 unsigned long flags)
1609{
1610 unsigned long off = pgoff << PAGE_SHIFT;
1611 struct vb2_buffer *vb;
1612 unsigned int buffer, plane;
1613 int ret;
1614
1615 if (q->memory != V4L2_MEMORY_MMAP) {
1616 dprintk(1, "Queue is not currently set up for mmap\n");
1617 return -EINVAL;
1618 }
1619
1620 /*
1621 * Find the plane corresponding to the offset passed by userspace.
1622 */
1623 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1624 if (ret)
1625 return ret;
1626
1627 vb = q->bufs[buffer];
1628
1629 return (unsigned long)vb2_plane_vaddr(vb, plane);
1630}
1631EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
1632#endif
1633
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001634static int __vb2_init_fileio(struct vb2_queue *q, int read);
1635static int __vb2_cleanup_fileio(struct vb2_queue *q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001636
1637/**
1638 * vb2_poll() - implements poll userspace operation
1639 * @q: videobuf2 queue
1640 * @file: file argument passed to the poll file operation handler
1641 * @wait: wait argument passed to the poll file operation handler
1642 *
1643 * This function implements poll file operation handler for a driver.
1644 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1645 * be informed that the file descriptor of a video device is available for
1646 * reading.
1647 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1648 * will be reported as available for writing.
1649 *
1650 * The return values from this function are intended to be directly returned
1651 * from poll handler in driver.
1652 */
1653unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
1654{
1655 unsigned long flags;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001656 unsigned int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001657 struct vb2_buffer *vb = NULL;
1658
1659 /*
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001660 * Start file I/O emulator only if streaming API has not been used yet.
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001661 */
1662 if (q->num_buffers == 0 && q->fileio == NULL) {
1663 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ)) {
1664 ret = __vb2_init_fileio(q, 1);
1665 if (ret)
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001666 return POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001667 }
1668 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE)) {
1669 ret = __vb2_init_fileio(q, 0);
1670 if (ret)
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001671 return POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001672 /*
1673 * Write to OUTPUT queue can be done immediately.
1674 */
1675 return POLLOUT | POLLWRNORM;
1676 }
1677 }
1678
1679 /*
Pawel Osciake23ccc02010-10-11 10:56:41 -03001680 * There is nothing to wait for if no buffers have already been queued.
1681 */
1682 if (list_empty(&q->queued_list))
1683 return POLLERR;
1684
1685 poll_wait(file, &q->done_wq, wait);
1686
1687 /*
1688 * Take first buffer available for dequeuing.
1689 */
1690 spin_lock_irqsave(&q->done_lock, flags);
1691 if (!list_empty(&q->done_list))
1692 vb = list_first_entry(&q->done_list, struct vb2_buffer,
1693 done_entry);
1694 spin_unlock_irqrestore(&q->done_lock, flags);
1695
1696 if (vb && (vb->state == VB2_BUF_STATE_DONE
1697 || vb->state == VB2_BUF_STATE_ERROR)) {
1698 return (V4L2_TYPE_IS_OUTPUT(q->type)) ? POLLOUT | POLLWRNORM :
1699 POLLIN | POLLRDNORM;
1700 }
1701 return 0;
1702}
1703EXPORT_SYMBOL_GPL(vb2_poll);
1704
1705/**
1706 * vb2_queue_init() - initialize a videobuf2 queue
1707 * @q: videobuf2 queue; this structure should be allocated in driver
1708 *
1709 * The vb2_queue structure should be allocated by the driver. The driver is
1710 * responsible of clearing it's content and setting initial values for some
1711 * required entries before calling this function.
1712 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
1713 * to the struct vb2_queue description in include/media/videobuf2-core.h
1714 * for more information.
1715 */
1716int vb2_queue_init(struct vb2_queue *q)
1717{
1718 BUG_ON(!q);
1719 BUG_ON(!q->ops);
1720 BUG_ON(!q->mem_ops);
1721 BUG_ON(!q->type);
1722 BUG_ON(!q->io_modes);
1723
1724 BUG_ON(!q->ops->queue_setup);
1725 BUG_ON(!q->ops->buf_queue);
1726
1727 INIT_LIST_HEAD(&q->queued_list);
1728 INIT_LIST_HEAD(&q->done_list);
1729 spin_lock_init(&q->done_lock);
1730 init_waitqueue_head(&q->done_wq);
1731
1732 if (q->buf_struct_size == 0)
1733 q->buf_struct_size = sizeof(struct vb2_buffer);
1734
1735 return 0;
1736}
1737EXPORT_SYMBOL_GPL(vb2_queue_init);
1738
1739/**
1740 * vb2_queue_release() - stop streaming, release the queue and free memory
1741 * @q: videobuf2 queue
1742 *
1743 * This function stops streaming and performs necessary clean ups, including
1744 * freeing video buffer memory. The driver is responsible for freeing
1745 * the vb2_queue structure itself.
1746 */
1747void vb2_queue_release(struct vb2_queue *q)
1748{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001749 __vb2_cleanup_fileio(q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001750 __vb2_queue_cancel(q);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001751 __vb2_queue_free(q, q->num_buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001752}
1753EXPORT_SYMBOL_GPL(vb2_queue_release);
1754
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001755/**
1756 * struct vb2_fileio_buf - buffer context used by file io emulator
1757 *
1758 * vb2 provides a compatibility layer and emulator of file io (read and
1759 * write) calls on top of streaming API. This structure is used for
1760 * tracking context related to the buffers.
1761 */
1762struct vb2_fileio_buf {
1763 void *vaddr;
1764 unsigned int size;
1765 unsigned int pos;
1766 unsigned int queued:1;
1767};
1768
1769/**
1770 * struct vb2_fileio_data - queue context used by file io emulator
1771 *
1772 * vb2 provides a compatibility layer and emulator of file io (read and
1773 * write) calls on top of streaming API. For proper operation it required
1774 * this structure to save the driver state between each call of the read
1775 * or write function.
1776 */
1777struct vb2_fileio_data {
1778 struct v4l2_requestbuffers req;
1779 struct v4l2_buffer b;
1780 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
1781 unsigned int index;
1782 unsigned int q_count;
1783 unsigned int dq_count;
1784 unsigned int flags;
1785};
1786
1787/**
1788 * __vb2_init_fileio() - initialize file io emulator
1789 * @q: videobuf2 queue
1790 * @read: mode selector (1 means read, 0 means write)
1791 */
1792static int __vb2_init_fileio(struct vb2_queue *q, int read)
1793{
1794 struct vb2_fileio_data *fileio;
1795 int i, ret;
1796 unsigned int count = 0;
1797
1798 /*
1799 * Sanity check
1800 */
1801 if ((read && !(q->io_modes & VB2_READ)) ||
1802 (!read && !(q->io_modes & VB2_WRITE)))
1803 BUG();
1804
1805 /*
1806 * Check if device supports mapping buffers to kernel virtual space.
1807 */
1808 if (!q->mem_ops->vaddr)
1809 return -EBUSY;
1810
1811 /*
1812 * Check if streaming api has not been already activated.
1813 */
1814 if (q->streaming || q->num_buffers > 0)
1815 return -EBUSY;
1816
1817 /*
1818 * Start with count 1, driver can increase it in queue_setup()
1819 */
1820 count = 1;
1821
1822 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
1823 (read) ? "read" : "write", count, q->io_flags);
1824
1825 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
1826 if (fileio == NULL)
1827 return -ENOMEM;
1828
1829 fileio->flags = q->io_flags;
1830
1831 /*
1832 * Request buffers and use MMAP type to force driver
1833 * to allocate buffers by itself.
1834 */
1835 fileio->req.count = count;
1836 fileio->req.memory = V4L2_MEMORY_MMAP;
1837 fileio->req.type = q->type;
1838 ret = vb2_reqbufs(q, &fileio->req);
1839 if (ret)
1840 goto err_kfree;
1841
1842 /*
1843 * Check if plane_count is correct
1844 * (multiplane buffers are not supported).
1845 */
1846 if (q->bufs[0]->num_planes != 1) {
1847 fileio->req.count = 0;
1848 ret = -EBUSY;
1849 goto err_reqbufs;
1850 }
1851
1852 /*
1853 * Get kernel address of each buffer.
1854 */
1855 for (i = 0; i < q->num_buffers; i++) {
1856 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
1857 if (fileio->bufs[i].vaddr == NULL)
1858 goto err_reqbufs;
1859 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
1860 }
1861
1862 /*
1863 * Read mode requires pre queuing of all buffers.
1864 */
1865 if (read) {
1866 /*
1867 * Queue all buffers.
1868 */
1869 for (i = 0; i < q->num_buffers; i++) {
1870 struct v4l2_buffer *b = &fileio->b;
1871 memset(b, 0, sizeof(*b));
1872 b->type = q->type;
1873 b->memory = q->memory;
1874 b->index = i;
1875 ret = vb2_qbuf(q, b);
1876 if (ret)
1877 goto err_reqbufs;
1878 fileio->bufs[i].queued = 1;
1879 }
1880
1881 /*
1882 * Start streaming.
1883 */
1884 ret = vb2_streamon(q, q->type);
1885 if (ret)
1886 goto err_reqbufs;
1887 }
1888
1889 q->fileio = fileio;
1890
1891 return ret;
1892
1893err_reqbufs:
1894 vb2_reqbufs(q, &fileio->req);
1895
1896err_kfree:
1897 kfree(fileio);
1898 return ret;
1899}
1900
1901/**
1902 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1903 * @q: videobuf2 queue
1904 */
1905static int __vb2_cleanup_fileio(struct vb2_queue *q)
1906{
1907 struct vb2_fileio_data *fileio = q->fileio;
1908
1909 if (fileio) {
1910 /*
1911 * Hack fileio context to enable direct calls to vb2 ioctl
1912 * interface.
1913 */
1914 q->fileio = NULL;
1915
1916 vb2_streamoff(q, q->type);
1917 fileio->req.count = 0;
1918 vb2_reqbufs(q, &fileio->req);
1919 kfree(fileio);
1920 dprintk(3, "file io emulator closed\n");
1921 }
1922 return 0;
1923}
1924
1925/**
1926 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1927 * @q: videobuf2 queue
1928 * @data: pointed to target userspace buffer
1929 * @count: number of bytes to read or write
1930 * @ppos: file handle position tracking pointer
1931 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1932 * @read: access mode selector (1 means read, 0 means write)
1933 */
1934static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
1935 loff_t *ppos, int nonblock, int read)
1936{
1937 struct vb2_fileio_data *fileio;
1938 struct vb2_fileio_buf *buf;
1939 int ret, index;
1940
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03001941 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001942 read ? "read" : "write", (long)*ppos, count,
1943 nonblock ? "non" : "");
1944
1945 if (!data)
1946 return -EINVAL;
1947
1948 /*
1949 * Initialize emulator on first call.
1950 */
1951 if (!q->fileio) {
1952 ret = __vb2_init_fileio(q, read);
1953 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
1954 if (ret)
1955 return ret;
1956 }
1957 fileio = q->fileio;
1958
1959 /*
1960 * Hack fileio context to enable direct calls to vb2 ioctl interface.
1961 * The pointer will be restored before returning from this function.
1962 */
1963 q->fileio = NULL;
1964
1965 index = fileio->index;
1966 buf = &fileio->bufs[index];
1967
1968 /*
1969 * Check if we need to dequeue the buffer.
1970 */
1971 if (buf->queued) {
1972 struct vb2_buffer *vb;
1973
1974 /*
1975 * Call vb2_dqbuf to get buffer back.
1976 */
1977 memset(&fileio->b, 0, sizeof(fileio->b));
1978 fileio->b.type = q->type;
1979 fileio->b.memory = q->memory;
1980 fileio->b.index = index;
1981 ret = vb2_dqbuf(q, &fileio->b, nonblock);
1982 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
1983 if (ret)
1984 goto end;
1985 fileio->dq_count += 1;
1986
1987 /*
1988 * Get number of bytes filled by the driver
1989 */
1990 vb = q->bufs[index];
1991 buf->size = vb2_get_plane_payload(vb, 0);
1992 buf->queued = 0;
1993 }
1994
1995 /*
1996 * Limit count on last few bytes of the buffer.
1997 */
1998 if (buf->pos + count > buf->size) {
1999 count = buf->size - buf->pos;
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002000 dprintk(5, "reducing read count: %zd\n", count);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002001 }
2002
2003 /*
2004 * Transfer data to userspace.
2005 */
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002006 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002007 count, index, buf->pos);
2008 if (read)
2009 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2010 else
2011 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2012 if (ret) {
2013 dprintk(3, "file io: error copying data\n");
2014 ret = -EFAULT;
2015 goto end;
2016 }
2017
2018 /*
2019 * Update counters.
2020 */
2021 buf->pos += count;
2022 *ppos += count;
2023
2024 /*
2025 * Queue next buffer if required.
2026 */
2027 if (buf->pos == buf->size ||
2028 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2029 /*
2030 * Check if this is the last buffer to read.
2031 */
2032 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2033 fileio->dq_count == 1) {
2034 dprintk(3, "file io: read limit reached\n");
2035 /*
2036 * Restore fileio pointer and release the context.
2037 */
2038 q->fileio = fileio;
2039 return __vb2_cleanup_fileio(q);
2040 }
2041
2042 /*
2043 * Call vb2_qbuf and give buffer to the driver.
2044 */
2045 memset(&fileio->b, 0, sizeof(fileio->b));
2046 fileio->b.type = q->type;
2047 fileio->b.memory = q->memory;
2048 fileio->b.index = index;
2049 fileio->b.bytesused = buf->pos;
2050 ret = vb2_qbuf(q, &fileio->b);
2051 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2052 if (ret)
2053 goto end;
2054
2055 /*
2056 * Buffer has been queued, update the status
2057 */
2058 buf->pos = 0;
2059 buf->queued = 1;
2060 buf->size = q->bufs[0]->v4l2_planes[0].length;
2061 fileio->q_count += 1;
2062
2063 /*
2064 * Switch to the next buffer
2065 */
2066 fileio->index = (index + 1) % q->num_buffers;
2067
2068 /*
2069 * Start streaming if required.
2070 */
2071 if (!read && !q->streaming) {
2072 ret = vb2_streamon(q, q->type);
2073 if (ret)
2074 goto end;
2075 }
2076 }
2077
2078 /*
2079 * Return proper number of bytes processed.
2080 */
2081 if (ret == 0)
2082 ret = count;
2083end:
2084 /*
2085 * Restore the fileio context and block vb2 ioctl interface.
2086 */
2087 q->fileio = fileio;
2088 return ret;
2089}
2090
2091size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2092 loff_t *ppos, int nonblocking)
2093{
2094 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2095}
2096EXPORT_SYMBOL_GPL(vb2_read);
2097
2098size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
2099 loff_t *ppos, int nonblocking)
2100{
2101 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
2102}
2103EXPORT_SYMBOL_GPL(vb2_write);
2104
Pawel Osciake23ccc02010-10-11 10:56:41 -03002105MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
Pawel Osciak95072082011-03-13 15:23:32 -03002106MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
Pawel Osciake23ccc02010-10-11 10:56:41 -03002107MODULE_LICENSE("GPL");