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