2 * videobuf2-core.c - V4L2 driver helper framework
4 * Copyright (C) 2010 Samsung Electronics
6 * Author: Pawel Osciak <p.osciak@samsung.com>
7 * Marek Szyprowski <m.szyprowski@samsung.com>
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.
14 #include <linux/err.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/sched.h>
22 #include <media/videobuf2-core.h>
25 module_param(debug
, int, 0644);
27 #define dprintk(level, fmt, arg...) \
30 printk(KERN_DEBUG "vb2: " fmt, ## arg); \
33 #define call_memop(q, plane, op, args...) \
34 (((q)->mem_ops->op) ? \
35 ((q)->mem_ops->op(args)) : 0)
37 #define call_qop(q, op, args...) \
38 (((q)->ops->op) ? ((q)->ops->op(args)) : 0)
41 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
43 static int __vb2_buf_mem_alloc(struct vb2_buffer
*vb
,
44 unsigned long *plane_sizes
)
46 struct vb2_queue
*q
= vb
->vb2_queue
;
50 /* Allocate memory for all planes in this buffer */
51 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
52 mem_priv
= call_memop(q
, plane
, alloc
, q
->alloc_ctx
[plane
],
57 /* Associate allocator private data with this plane */
58 vb
->planes
[plane
].mem_priv
= mem_priv
;
59 vb
->v4l2_planes
[plane
].length
= plane_sizes
[plane
];
64 /* Free already allocated memory if one of the allocations failed */
65 for (; plane
> 0; --plane
)
66 call_memop(q
, plane
, put
, vb
->planes
[plane
- 1].mem_priv
);
72 * __vb2_buf_mem_free() - free memory of the given buffer
74 static void __vb2_buf_mem_free(struct vb2_buffer
*vb
)
76 struct vb2_queue
*q
= vb
->vb2_queue
;
79 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
80 call_memop(q
, plane
, put
, vb
->planes
[plane
].mem_priv
);
81 vb
->planes
[plane
].mem_priv
= NULL
;
82 dprintk(3, "Freed plane %d of buffer %d\n",
83 plane
, vb
->v4l2_buf
.index
);
88 * __vb2_buf_userptr_put() - release userspace memory associated with
91 static void __vb2_buf_userptr_put(struct vb2_buffer
*vb
)
93 struct vb2_queue
*q
= vb
->vb2_queue
;
96 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
97 void *mem_priv
= vb
->planes
[plane
].mem_priv
;
100 call_memop(q
, plane
, put_userptr
, mem_priv
);
101 vb
->planes
[plane
].mem_priv
= NULL
;
107 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
108 * every buffer on the queue
110 static void __setup_offsets(struct vb2_queue
*q
)
112 unsigned int buffer
, plane
;
113 struct vb2_buffer
*vb
;
114 unsigned long off
= 0;
116 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
117 vb
= q
->bufs
[buffer
];
121 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
122 vb
->v4l2_planes
[plane
].m
.mem_offset
= off
;
124 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
127 off
+= vb
->v4l2_planes
[plane
].length
;
128 off
= PAGE_ALIGN(off
);
134 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
135 * video buffer memory for all buffers/planes on the queue and initializes the
138 * Returns the number of buffers successfully allocated.
140 static int __vb2_queue_alloc(struct vb2_queue
*q
, enum v4l2_memory memory
,
141 unsigned int num_buffers
, unsigned int num_planes
,
142 unsigned long plane_sizes
[])
145 struct vb2_buffer
*vb
;
148 for (buffer
= 0; buffer
< num_buffers
; ++buffer
) {
149 /* Allocate videobuf buffer structures */
150 vb
= kzalloc(q
->buf_struct_size
, GFP_KERNEL
);
152 dprintk(1, "Memory alloc for buffer struct failed\n");
156 /* Length stores number of planes for multiplanar buffers */
157 if (V4L2_TYPE_IS_MULTIPLANAR(q
->type
))
158 vb
->v4l2_buf
.length
= num_planes
;
160 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
162 vb
->num_planes
= num_planes
;
163 vb
->v4l2_buf
.index
= buffer
;
164 vb
->v4l2_buf
.type
= q
->type
;
165 vb
->v4l2_buf
.memory
= memory
;
167 /* Allocate video buffer memory for the MMAP type */
168 if (memory
== V4L2_MEMORY_MMAP
) {
169 ret
= __vb2_buf_mem_alloc(vb
, plane_sizes
);
171 dprintk(1, "Failed allocating memory for "
172 "buffer %d\n", buffer
);
177 * Call the driver-provided buffer initialization
178 * callback, if given. An error in initialization
179 * results in queue setup failure.
181 ret
= call_qop(q
, buf_init
, vb
);
183 dprintk(1, "Buffer %d %p initialization"
184 " failed\n", buffer
, vb
);
185 __vb2_buf_mem_free(vb
);
191 q
->bufs
[buffer
] = vb
;
194 q
->num_buffers
= buffer
;
198 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
199 q
->num_buffers
, num_planes
);
205 * __vb2_free_mem() - release all video buffer memory for a given queue
207 static void __vb2_free_mem(struct vb2_queue
*q
)
210 struct vb2_buffer
*vb
;
212 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
213 vb
= q
->bufs
[buffer
];
217 /* Free MMAP buffers or release USERPTR buffers */
218 if (q
->memory
== V4L2_MEMORY_MMAP
)
219 __vb2_buf_mem_free(vb
);
221 __vb2_buf_userptr_put(vb
);
226 * __vb2_queue_free() - free the queue - video memory and related information
227 * and return the queue to an uninitialized state. Might be called even if the
228 * queue has already been freed.
230 static int __vb2_queue_free(struct vb2_queue
*q
)
234 /* Call driver-provided cleanup function for each buffer, if provided */
235 if (q
->ops
->buf_cleanup
) {
236 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
237 if (NULL
== q
->bufs
[buffer
])
239 q
->ops
->buf_cleanup(q
->bufs
[buffer
]);
243 /* Release video buffer memory */
246 /* Free videobuf buffers */
247 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
248 kfree(q
->bufs
[buffer
]);
249 q
->bufs
[buffer
] = NULL
;
259 * __verify_planes_array() - verify that the planes array passed in struct
260 * v4l2_buffer from userspace can be safely used
262 static int __verify_planes_array(struct vb2_buffer
*vb
, struct v4l2_buffer
*b
)
264 /* Is memory for copying plane information present? */
265 if (NULL
== b
->m
.planes
) {
266 dprintk(1, "Multi-planar buffer passed but "
267 "planes array not provided\n");
271 if (b
->length
< vb
->num_planes
|| b
->length
> VIDEO_MAX_PLANES
) {
272 dprintk(1, "Incorrect planes array length, "
273 "expected %d, got %d\n", vb
->num_planes
, b
->length
);
281 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
282 * returned to userspace
284 static int __fill_v4l2_buffer(struct vb2_buffer
*vb
, struct v4l2_buffer
*b
)
286 struct vb2_queue
*q
= vb
->vb2_queue
;
289 /* Copy back data such as timestamp, input, etc. */
290 memcpy(b
, &vb
->v4l2_buf
, offsetof(struct v4l2_buffer
, m
));
291 b
->input
= vb
->v4l2_buf
.input
;
292 b
->reserved
= vb
->v4l2_buf
.reserved
;
294 if (V4L2_TYPE_IS_MULTIPLANAR(q
->type
)) {
295 ret
= __verify_planes_array(vb
, b
);
300 * Fill in plane-related data if userspace provided an array
301 * for it. The memory and size is verified above.
303 memcpy(b
->m
.planes
, vb
->v4l2_planes
,
304 b
->length
* sizeof(struct v4l2_plane
));
307 * We use length and offset in v4l2_planes array even for
308 * single-planar buffers, but userspace does not.
310 b
->length
= vb
->v4l2_planes
[0].length
;
311 b
->bytesused
= vb
->v4l2_planes
[0].bytesused
;
312 if (q
->memory
== V4L2_MEMORY_MMAP
)
313 b
->m
.offset
= vb
->v4l2_planes
[0].m
.mem_offset
;
314 else if (q
->memory
== V4L2_MEMORY_USERPTR
)
315 b
->m
.userptr
= vb
->v4l2_planes
[0].m
.userptr
;
321 case VB2_BUF_STATE_QUEUED
:
322 case VB2_BUF_STATE_ACTIVE
:
323 b
->flags
|= V4L2_BUF_FLAG_QUEUED
;
325 case VB2_BUF_STATE_ERROR
:
326 b
->flags
|= V4L2_BUF_FLAG_ERROR
;
328 case VB2_BUF_STATE_DONE
:
329 b
->flags
|= V4L2_BUF_FLAG_DONE
;
331 case VB2_BUF_STATE_DEQUEUED
:
336 if (vb
->num_planes_mapped
== vb
->num_planes
)
337 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
343 * vb2_querybuf() - query video buffer information
345 * @b: buffer struct passed from userspace to vidioc_querybuf handler
348 * Should be called from vidioc_querybuf ioctl handler in driver.
349 * This function will verify the passed v4l2_buffer structure and fill the
350 * relevant information for the userspace.
352 * The return values from this function are intended to be directly returned
353 * from vidioc_querybuf handler in driver.
355 int vb2_querybuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
357 struct vb2_buffer
*vb
;
359 if (b
->type
!= q
->type
) {
360 dprintk(1, "querybuf: wrong buffer type\n");
364 if (b
->index
>= q
->num_buffers
) {
365 dprintk(1, "querybuf: buffer index out of range\n");
368 vb
= q
->bufs
[b
->index
];
370 return __fill_v4l2_buffer(vb
, b
);
372 EXPORT_SYMBOL(vb2_querybuf
);
375 * __verify_userptr_ops() - verify that all memory operations required for
376 * USERPTR queue type have been provided
378 static int __verify_userptr_ops(struct vb2_queue
*q
)
380 if (!(q
->io_modes
& VB2_USERPTR
) || !q
->mem_ops
->get_userptr
||
381 !q
->mem_ops
->put_userptr
)
388 * __verify_mmap_ops() - verify that all memory operations required for
389 * MMAP queue type have been provided
391 static int __verify_mmap_ops(struct vb2_queue
*q
)
393 if (!(q
->io_modes
& VB2_MMAP
) || !q
->mem_ops
->alloc
||
394 !q
->mem_ops
->put
|| !q
->mem_ops
->mmap
)
401 * __buffers_in_use() - return true if any buffers on the queue are in use and
402 * the queue cannot be freed (by the means of REQBUFS(0)) call
404 static bool __buffers_in_use(struct vb2_queue
*q
)
406 unsigned int buffer
, plane
;
407 struct vb2_buffer
*vb
;
409 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
410 vb
= q
->bufs
[buffer
];
411 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
413 * If num_users() has not been provided, call_memop
414 * will return 0, apparently nobody cares about this
415 * case anyway. If num_users() returns more than 1,
416 * we are not the only user of the plane's memory.
418 if (call_memop(q
, plane
, num_users
,
419 vb
->planes
[plane
].mem_priv
) > 1)
428 * vb2_reqbufs() - Initiate streaming
429 * @q: videobuf2 queue
430 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
432 * Should be called from vidioc_reqbufs ioctl handler of a driver.
434 * 1) verifies streaming parameters passed from the userspace,
435 * 2) sets up the queue,
436 * 3) negotiates number of buffers and planes per buffer with the driver
437 * to be used during streaming,
438 * 4) allocates internal buffer structures (struct vb2_buffer), according to
439 * the agreed parameters,
440 * 5) for MMAP memory type, allocates actual video memory, using the
441 * memory handling/allocation routines provided during queue initialization
443 * If req->count is 0, all the memory will be freed instead.
444 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
445 * and the queue is not busy, memory will be reallocated.
447 * The return values from this function are intended to be directly returned
448 * from vidioc_reqbufs handler in driver.
450 int vb2_reqbufs(struct vb2_queue
*q
, struct v4l2_requestbuffers
*req
)
452 unsigned int num_buffers
, num_planes
;
453 unsigned long plane_sizes
[VIDEO_MAX_PLANES
];
457 dprintk(1, "reqbufs: file io in progress\n");
461 if (req
->memory
!= V4L2_MEMORY_MMAP
462 && req
->memory
!= V4L2_MEMORY_USERPTR
) {
463 dprintk(1, "reqbufs: unsupported memory type\n");
467 if (req
->type
!= q
->type
) {
468 dprintk(1, "reqbufs: requested type is incorrect\n");
473 dprintk(1, "reqbufs: streaming active\n");
478 * Make sure all the required memory ops for given memory type
481 if (req
->memory
== V4L2_MEMORY_MMAP
&& __verify_mmap_ops(q
)) {
482 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
486 if (req
->memory
== V4L2_MEMORY_USERPTR
&& __verify_userptr_ops(q
)) {
487 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
491 if (req
->count
== 0 || q
->num_buffers
!= 0) {
493 * We already have buffers allocated, so first check if they
494 * are not in use and can be freed.
496 if (q
->memory
== V4L2_MEMORY_MMAP
&& __buffers_in_use(q
)) {
497 dprintk(1, "reqbufs: memory in use, cannot free\n");
501 ret
= __vb2_queue_free(q
);
507 * Make sure the requested values and current defaults are sane.
509 num_buffers
= min_t(unsigned int, req
->count
, VIDEO_MAX_FRAME
);
510 memset(plane_sizes
, 0, sizeof(plane_sizes
));
511 memset(q
->alloc_ctx
, 0, sizeof(q
->alloc_ctx
));
514 * Ask the driver how many buffers and planes per buffer it requires.
515 * Driver also sets the size and allocator context for each plane.
517 ret
= call_qop(q
, queue_setup
, q
, &num_buffers
, &num_planes
,
518 plane_sizes
, q
->alloc_ctx
);
522 /* Finally, allocate buffers and video memory */
523 ret
= __vb2_queue_alloc(q
, req
->memory
, num_buffers
, num_planes
,
526 dprintk(1, "Memory allocation failed with error: %d\n", ret
);
531 * Check if driver can handle the allocated number of buffers.
533 if (ret
< num_buffers
) {
534 unsigned int orig_num_buffers
;
536 orig_num_buffers
= num_buffers
= ret
;
537 ret
= call_qop(q
, queue_setup
, q
, &num_buffers
, &num_planes
,
538 plane_sizes
, q
->alloc_ctx
);
542 if (orig_num_buffers
< num_buffers
) {
548 * Ok, driver accepted smaller number of buffers.
553 q
->memory
= req
->memory
;
556 * Return the number of successfully allocated buffers
567 EXPORT_SYMBOL_GPL(vb2_reqbufs
);
570 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
571 * @vb: vb2_buffer to which the plane in question belongs to
572 * @plane_no: plane number for which the address is to be returned
574 * This function returns a kernel virtual address of a given plane if
575 * such a mapping exist, NULL otherwise.
577 void *vb2_plane_vaddr(struct vb2_buffer
*vb
, unsigned int plane_no
)
579 struct vb2_queue
*q
= vb
->vb2_queue
;
581 if (plane_no
> vb
->num_planes
)
584 return call_memop(q
, plane_no
, vaddr
, vb
->planes
[plane_no
].mem_priv
);
587 EXPORT_SYMBOL_GPL(vb2_plane_vaddr
);
590 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
591 * @vb: vb2_buffer to which the plane in question belongs to
592 * @plane_no: plane number for which the cookie is to be returned
594 * This function returns an allocator specific cookie for a given plane if
595 * available, NULL otherwise. The allocator should provide some simple static
596 * inline function, which would convert this cookie to the allocator specific
597 * type that can be used directly by the driver to access the buffer. This can
598 * be for example physical address, pointer to scatter list or IOMMU mapping.
600 void *vb2_plane_cookie(struct vb2_buffer
*vb
, unsigned int plane_no
)
602 struct vb2_queue
*q
= vb
->vb2_queue
;
604 if (plane_no
> vb
->num_planes
)
607 return call_memop(q
, plane_no
, cookie
, vb
->planes
[plane_no
].mem_priv
);
609 EXPORT_SYMBOL_GPL(vb2_plane_cookie
);
612 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
613 * @vb: vb2_buffer returned from the driver
614 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
615 * or VB2_BUF_STATE_ERROR if the operation finished with an error
617 * This function should be called by the driver after a hardware operation on
618 * a buffer is finished and the buffer may be returned to userspace. The driver
619 * cannot use this buffer anymore until it is queued back to it by videobuf
620 * by the means of buf_queue callback. Only buffers previously queued to the
621 * driver by buf_queue can be passed to this function.
623 void vb2_buffer_done(struct vb2_buffer
*vb
, enum vb2_buffer_state state
)
625 struct vb2_queue
*q
= vb
->vb2_queue
;
628 if (vb
->state
!= VB2_BUF_STATE_ACTIVE
)
631 if (state
!= VB2_BUF_STATE_DONE
&& state
!= VB2_BUF_STATE_ERROR
)
634 dprintk(4, "Done processing on buffer %d, state: %d\n",
635 vb
->v4l2_buf
.index
, vb
->state
);
637 /* Add the buffer to the done buffers list */
638 spin_lock_irqsave(&q
->done_lock
, flags
);
640 list_add_tail(&vb
->done_entry
, &q
->done_list
);
641 atomic_dec(&q
->queued_count
);
642 spin_unlock_irqrestore(&q
->done_lock
, flags
);
644 /* Inform any processes that may be waiting for buffers */
645 wake_up(&q
->done_wq
);
647 EXPORT_SYMBOL_GPL(vb2_buffer_done
);
650 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in
651 * a v4l2_buffer by the userspace
653 static int __fill_vb2_buffer(struct vb2_buffer
*vb
, struct v4l2_buffer
*b
,
654 struct v4l2_plane
*v4l2_planes
)
659 if (V4L2_TYPE_IS_MULTIPLANAR(b
->type
)) {
661 * Verify that the userspace gave us a valid array for
664 ret
= __verify_planes_array(vb
, b
);
668 /* Fill in driver-provided information for OUTPUT types */
669 if (V4L2_TYPE_IS_OUTPUT(b
->type
)) {
671 * Will have to go up to b->length when API starts
672 * accepting variable number of planes.
674 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
675 v4l2_planes
[plane
].bytesused
=
676 b
->m
.planes
[plane
].bytesused
;
677 v4l2_planes
[plane
].data_offset
=
678 b
->m
.planes
[plane
].data_offset
;
682 if (b
->memory
== V4L2_MEMORY_USERPTR
) {
683 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
684 v4l2_planes
[plane
].m
.userptr
=
685 b
->m
.planes
[plane
].m
.userptr
;
686 v4l2_planes
[plane
].length
=
687 b
->m
.planes
[plane
].length
;
692 * Single-planar buffers do not use planes array,
693 * so fill in relevant v4l2_buffer struct fields instead.
694 * In videobuf we use our internal V4l2_planes struct for
695 * single-planar buffers as well, for simplicity.
697 if (V4L2_TYPE_IS_OUTPUT(b
->type
))
698 v4l2_planes
[0].bytesused
= b
->bytesused
;
700 if (b
->memory
== V4L2_MEMORY_USERPTR
) {
701 v4l2_planes
[0].m
.userptr
= b
->m
.userptr
;
702 v4l2_planes
[0].length
= b
->length
;
706 vb
->v4l2_buf
.field
= b
->field
;
707 vb
->v4l2_buf
.timestamp
= b
->timestamp
;
713 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
715 static int __qbuf_userptr(struct vb2_buffer
*vb
, struct v4l2_buffer
*b
)
717 struct v4l2_plane planes
[VIDEO_MAX_PLANES
];
718 struct vb2_queue
*q
= vb
->vb2_queue
;
722 int write
= !V4L2_TYPE_IS_OUTPUT(q
->type
);
724 /* Verify and copy relevant information provided by the userspace */
725 ret
= __fill_vb2_buffer(vb
, b
, planes
);
729 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
730 /* Skip the plane if already verified */
731 if (vb
->v4l2_planes
[plane
].m
.userptr
== planes
[plane
].m
.userptr
732 && vb
->v4l2_planes
[plane
].length
== planes
[plane
].length
)
735 dprintk(3, "qbuf: userspace address for plane %d changed, "
736 "reacquiring memory\n", plane
);
738 /* Release previously acquired memory if present */
739 if (vb
->planes
[plane
].mem_priv
)
740 call_memop(q
, plane
, put_userptr
,
741 vb
->planes
[plane
].mem_priv
);
743 vb
->planes
[plane
].mem_priv
= NULL
;
745 /* Acquire each plane's memory */
746 if (q
->mem_ops
->get_userptr
) {
747 mem_priv
= q
->mem_ops
->get_userptr(q
->alloc_ctx
[plane
],
748 planes
[plane
].m
.userptr
,
749 planes
[plane
].length
,
751 if (IS_ERR(mem_priv
)) {
752 dprintk(1, "qbuf: failed acquiring userspace "
753 "memory for plane %d\n", plane
);
754 ret
= PTR_ERR(mem_priv
);
757 vb
->planes
[plane
].mem_priv
= mem_priv
;
762 * Call driver-specific initialization on the newly acquired buffer,
765 ret
= call_qop(q
, buf_init
, vb
);
767 dprintk(1, "qbuf: buffer initialization failed\n");
772 * Now that everything is in order, copy relevant information
773 * provided by userspace.
775 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
776 vb
->v4l2_planes
[plane
] = planes
[plane
];
780 /* In case of errors, release planes that were already acquired */
781 for (; plane
> 0; --plane
) {
782 call_memop(q
, plane
, put_userptr
,
783 vb
->planes
[plane
- 1].mem_priv
);
784 vb
->planes
[plane
- 1].mem_priv
= NULL
;
791 * __qbuf_mmap() - handle qbuf of an MMAP buffer
793 static int __qbuf_mmap(struct vb2_buffer
*vb
, struct v4l2_buffer
*b
)
795 return __fill_vb2_buffer(vb
, b
, vb
->v4l2_planes
);
799 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
801 static void __enqueue_in_driver(struct vb2_buffer
*vb
)
803 struct vb2_queue
*q
= vb
->vb2_queue
;
805 vb
->state
= VB2_BUF_STATE_ACTIVE
;
806 atomic_inc(&q
->queued_count
);
807 q
->ops
->buf_queue(vb
);
811 * vb2_qbuf() - Queue a buffer from userspace
812 * @q: videobuf2 queue
813 * @b: buffer structure passed from userspace to vidioc_qbuf handler
816 * Should be called from vidioc_qbuf ioctl handler of a driver.
818 * 1) verifies the passed buffer,
819 * 2) calls buf_prepare callback in the driver (if provided), in which
820 * driver-specific buffer initialization can be performed,
821 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
822 * callback for processing.
824 * The return values from this function are intended to be directly returned
825 * from vidioc_qbuf handler in driver.
827 int vb2_qbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
829 struct vb2_buffer
*vb
;
833 dprintk(1, "qbuf: file io in progress\n");
837 if (b
->type
!= q
->type
) {
838 dprintk(1, "qbuf: invalid buffer type\n");
842 if (b
->index
>= q
->num_buffers
) {
843 dprintk(1, "qbuf: buffer index out of range\n");
847 vb
= q
->bufs
[b
->index
];
849 /* Should never happen */
850 dprintk(1, "qbuf: buffer is NULL\n");
854 if (b
->memory
!= q
->memory
) {
855 dprintk(1, "qbuf: invalid memory type\n");
859 if (vb
->state
!= VB2_BUF_STATE_DEQUEUED
) {
860 dprintk(1, "qbuf: buffer already in use\n");
864 if (q
->memory
== V4L2_MEMORY_MMAP
)
865 ret
= __qbuf_mmap(vb
, b
);
866 else if (q
->memory
== V4L2_MEMORY_USERPTR
)
867 ret
= __qbuf_userptr(vb
, b
);
869 WARN(1, "Invalid queue type\n");
876 ret
= call_qop(q
, buf_prepare
, vb
);
878 dprintk(1, "qbuf: buffer preparation failed\n");
883 * Add to the queued buffers list, a buffer will stay on it until
886 list_add_tail(&vb
->queued_entry
, &q
->queued_list
);
887 vb
->state
= VB2_BUF_STATE_QUEUED
;
890 * If already streaming, give the buffer to driver for processing.
891 * If not, the buffer will be given to driver on next streamon.
894 __enqueue_in_driver(vb
);
896 dprintk(1, "qbuf of buffer %d succeeded\n", vb
->v4l2_buf
.index
);
899 EXPORT_SYMBOL_GPL(vb2_qbuf
);
902 * __vb2_wait_for_done_vb() - wait for a buffer to become available
905 * Will sleep if required for nonblocking == false.
907 static int __vb2_wait_for_done_vb(struct vb2_queue
*q
, int nonblocking
)
910 * All operations on vb_done_list are performed under done_lock
911 * spinlock protection. However, buffers may be removed from
912 * it and returned to userspace only while holding both driver's
913 * lock and the done_lock spinlock. Thus we can be sure that as
914 * long as we hold the driver's lock, the list will remain not
915 * empty if list_empty() check succeeds.
922 dprintk(1, "Streaming off, will not wait for buffers\n");
926 if (!list_empty(&q
->done_list
)) {
928 * Found a buffer that we were waiting for.
934 dprintk(1, "Nonblocking and no buffers to dequeue, "
940 * We are streaming and blocking, wait for another buffer to
941 * become ready or for streamoff. Driver's lock is released to
942 * allow streamoff or qbuf to be called while waiting.
944 call_qop(q
, wait_prepare
, q
);
947 * All locks have been released, it is safe to sleep now.
949 dprintk(3, "Will sleep waiting for buffers\n");
950 ret
= wait_event_interruptible(q
->done_wq
,
951 !list_empty(&q
->done_list
) || !q
->streaming
);
954 * We need to reevaluate both conditions again after reacquiring
955 * the locks or return an error if one occurred.
957 call_qop(q
, wait_finish
, q
);
965 * __vb2_get_done_vb() - get a buffer ready for dequeuing
967 * Will sleep if required for nonblocking == false.
969 static int __vb2_get_done_vb(struct vb2_queue
*q
, struct vb2_buffer
**vb
,
976 * Wait for at least one buffer to become available on the done_list.
978 ret
= __vb2_wait_for_done_vb(q
, nonblocking
);
983 * Driver's lock has been held since we last verified that done_list
984 * is not empty, so no need for another list_empty(done_list) check.
986 spin_lock_irqsave(&q
->done_lock
, flags
);
987 *vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
, done_entry
);
988 list_del(&(*vb
)->done_entry
);
989 spin_unlock_irqrestore(&q
->done_lock
, flags
);
995 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
996 * @q: videobuf2 queue
998 * This function will wait until all buffers that have been given to the driver
999 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1000 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1001 * taken, for example from stop_streaming() callback.
1003 int vb2_wait_for_all_buffers(struct vb2_queue
*q
)
1005 if (!q
->streaming
) {
1006 dprintk(1, "Streaming off, will not wait for buffers\n");
1010 wait_event(q
->done_wq
, !atomic_read(&q
->queued_count
));
1013 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers
);
1016 * vb2_dqbuf() - Dequeue a buffer to the userspace
1017 * @q: videobuf2 queue
1018 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1020 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1021 * buffers ready for dequeuing are present. Normally the driver
1022 * would be passing (file->f_flags & O_NONBLOCK) here
1024 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1026 * 1) verifies the passed buffer,
1027 * 2) calls buf_finish callback in the driver (if provided), in which
1028 * driver can perform any additional operations that may be required before
1029 * returning the buffer to userspace, such as cache sync,
1030 * 3) the buffer struct members are filled with relevant information for
1033 * The return values from this function are intended to be directly returned
1034 * from vidioc_dqbuf handler in driver.
1036 int vb2_dqbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
, bool nonblocking
)
1038 struct vb2_buffer
*vb
= NULL
;
1042 dprintk(1, "dqbuf: file io in progress\n");
1046 if (b
->type
!= q
->type
) {
1047 dprintk(1, "dqbuf: invalid buffer type\n");
1051 ret
= __vb2_get_done_vb(q
, &vb
, nonblocking
);
1053 dprintk(1, "dqbuf: error getting next done buffer\n");
1057 ret
= call_qop(q
, buf_finish
, vb
);
1059 dprintk(1, "dqbuf: buffer finish failed\n");
1063 switch (vb
->state
) {
1064 case VB2_BUF_STATE_DONE
:
1065 dprintk(3, "dqbuf: Returning done buffer\n");
1067 case VB2_BUF_STATE_ERROR
:
1068 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1071 dprintk(1, "dqbuf: Invalid buffer state\n");
1075 /* Fill buffer information for the userspace */
1076 __fill_v4l2_buffer(vb
, b
);
1077 /* Remove from videobuf queue */
1078 list_del(&vb
->queued_entry
);
1080 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1081 vb
->v4l2_buf
.index
, vb
->state
);
1083 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
1086 EXPORT_SYMBOL_GPL(vb2_dqbuf
);
1089 * vb2_streamon - start streaming
1090 * @q: videobuf2 queue
1091 * @type: type argument passed from userspace to vidioc_streamon handler
1093 * Should be called from vidioc_streamon handler of a driver.
1095 * 1) verifies current state
1096 * 2) starts streaming and passes any previously queued buffers to the driver
1098 * The return values from this function are intended to be directly returned
1099 * from vidioc_streamon handler in the driver.
1101 int vb2_streamon(struct vb2_queue
*q
, enum v4l2_buf_type type
)
1103 struct vb2_buffer
*vb
;
1106 dprintk(1, "streamon: file io in progress\n");
1110 if (type
!= q
->type
) {
1111 dprintk(1, "streamon: invalid stream type\n");
1116 dprintk(1, "streamon: already streaming\n");
1121 * Cannot start streaming on an OUTPUT device if no buffers have
1124 if (V4L2_TYPE_IS_OUTPUT(q
->type
)) {
1125 if (list_empty(&q
->queued_list
)) {
1126 dprintk(1, "streamon: no output buffers queued\n");
1134 * Let driver notice that streaming state has been enabled.
1136 call_qop(q
, start_streaming
, q
);
1139 * If any buffers were queued before streamon,
1140 * we can now pass them to driver for processing.
1142 list_for_each_entry(vb
, &q
->queued_list
, queued_entry
)
1143 __enqueue_in_driver(vb
);
1145 dprintk(3, "Streamon successful\n");
1148 EXPORT_SYMBOL_GPL(vb2_streamon
);
1151 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1153 * Removes all queued buffers from driver's queue and all buffers queued by
1154 * userspace from videobuf's queue. Returns to state after reqbufs.
1156 static void __vb2_queue_cancel(struct vb2_queue
*q
)
1161 * Tell driver to stop all transactions and release all queued
1165 call_qop(q
, stop_streaming
, q
);
1169 * Remove all buffers from videobuf's list...
1171 INIT_LIST_HEAD(&q
->queued_list
);
1173 * ...and done list; userspace will not receive any buffers it
1174 * has not already dequeued before initiating cancel.
1176 INIT_LIST_HEAD(&q
->done_list
);
1177 wake_up_all(&q
->done_wq
);
1180 * Reinitialize all buffers for next use.
1182 for (i
= 0; i
< q
->num_buffers
; ++i
)
1183 q
->bufs
[i
]->state
= VB2_BUF_STATE_DEQUEUED
;
1187 * vb2_streamoff - stop streaming
1188 * @q: videobuf2 queue
1189 * @type: type argument passed from userspace to vidioc_streamoff handler
1191 * Should be called from vidioc_streamoff handler of a driver.
1193 * 1) verifies current state,
1194 * 2) stop streaming and dequeues any queued buffers, including those previously
1195 * passed to the driver (after waiting for the driver to finish).
1197 * This call can be used for pausing playback.
1198 * The return values from this function are intended to be directly returned
1199 * from vidioc_streamoff handler in the driver
1201 int vb2_streamoff(struct vb2_queue
*q
, enum v4l2_buf_type type
)
1204 dprintk(1, "streamoff: file io in progress\n");
1208 if (type
!= q
->type
) {
1209 dprintk(1, "streamoff: invalid stream type\n");
1213 if (!q
->streaming
) {
1214 dprintk(1, "streamoff: not streaming\n");
1219 * Cancel will pause streaming and remove all buffers from the driver
1220 * and videobuf, effectively returning control over them to userspace.
1222 __vb2_queue_cancel(q
);
1224 dprintk(3, "Streamoff successful\n");
1227 EXPORT_SYMBOL_GPL(vb2_streamoff
);
1230 * __find_plane_by_offset() - find plane associated with the given offset off
1232 static int __find_plane_by_offset(struct vb2_queue
*q
, unsigned long off
,
1233 unsigned int *_buffer
, unsigned int *_plane
)
1235 struct vb2_buffer
*vb
;
1236 unsigned int buffer
, plane
;
1239 * Go over all buffers and their planes, comparing the given offset
1240 * with an offset assigned to each plane. If a match is found,
1241 * return its buffer and plane numbers.
1243 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
1244 vb
= q
->bufs
[buffer
];
1246 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1247 if (vb
->v4l2_planes
[plane
].m
.mem_offset
== off
) {
1259 * vb2_mmap() - map video buffers into application address space
1260 * @q: videobuf2 queue
1261 * @vma: vma passed to the mmap file operation handler in the driver
1263 * Should be called from mmap file operation handler of a driver.
1264 * This function maps one plane of one of the available video buffers to
1265 * userspace. To map whole video memory allocated on reqbufs, this function
1266 * has to be called once per each plane per each buffer previously allocated.
1268 * When the userspace application calls mmap, it passes to it an offset returned
1269 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1270 * a "cookie", which is then used to identify the plane to be mapped.
1271 * This function finds a plane with a matching offset and a mapping is performed
1272 * by the means of a provided memory operation.
1274 * The return values from this function are intended to be directly returned
1275 * from the mmap handler in driver.
1277 int vb2_mmap(struct vb2_queue
*q
, struct vm_area_struct
*vma
)
1279 unsigned long off
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1280 struct vb2_plane
*vb_plane
;
1281 struct vb2_buffer
*vb
;
1282 unsigned int buffer
, plane
;
1285 if (q
->memory
!= V4L2_MEMORY_MMAP
) {
1286 dprintk(1, "Queue is not currently set up for mmap\n");
1291 * Check memory area access mode.
1293 if (!(vma
->vm_flags
& VM_SHARED
)) {
1294 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1297 if (V4L2_TYPE_IS_OUTPUT(q
->type
)) {
1298 if (!(vma
->vm_flags
& VM_WRITE
)) {
1299 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1303 if (!(vma
->vm_flags
& VM_READ
)) {
1304 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1310 * Find the plane corresponding to the offset passed by userspace.
1312 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
1316 vb
= q
->bufs
[buffer
];
1317 vb_plane
= &vb
->planes
[plane
];
1319 ret
= q
->mem_ops
->mmap(vb_plane
->mem_priv
, vma
);
1323 vb_plane
->mapped
= 1;
1324 vb
->num_planes_mapped
++;
1326 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer
, plane
);
1329 EXPORT_SYMBOL_GPL(vb2_mmap
);
1331 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
);
1332 static int __vb2_cleanup_fileio(struct vb2_queue
*q
);
1335 * vb2_poll() - implements poll userspace operation
1336 * @q: videobuf2 queue
1337 * @file: file argument passed to the poll file operation handler
1338 * @wait: wait argument passed to the poll file operation handler
1340 * This function implements poll file operation handler for a driver.
1341 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1342 * be informed that the file descriptor of a video device is available for
1344 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1345 * will be reported as available for writing.
1347 * The return values from this function are intended to be directly returned
1348 * from poll handler in driver.
1350 unsigned int vb2_poll(struct vb2_queue
*q
, struct file
*file
, poll_table
*wait
)
1352 unsigned long flags
;
1354 struct vb2_buffer
*vb
= NULL
;
1357 * Start file io emulator if streaming api has not been used yet.
1359 if (q
->num_buffers
== 0 && q
->fileio
== NULL
) {
1360 if (!V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_READ
)) {
1361 ret
= __vb2_init_fileio(q
, 1);
1365 if (V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_WRITE
)) {
1366 ret
= __vb2_init_fileio(q
, 0);
1370 * Write to OUTPUT queue can be done immediately.
1372 return POLLOUT
| POLLWRNORM
;
1377 * There is nothing to wait for if no buffers have already been queued.
1379 if (list_empty(&q
->queued_list
))
1382 poll_wait(file
, &q
->done_wq
, wait
);
1385 * Take first buffer available for dequeuing.
1387 spin_lock_irqsave(&q
->done_lock
, flags
);
1388 if (!list_empty(&q
->done_list
))
1389 vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
,
1391 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1393 if (vb
&& (vb
->state
== VB2_BUF_STATE_DONE
1394 || vb
->state
== VB2_BUF_STATE_ERROR
)) {
1395 return (V4L2_TYPE_IS_OUTPUT(q
->type
)) ? POLLOUT
| POLLWRNORM
:
1396 POLLIN
| POLLRDNORM
;
1400 EXPORT_SYMBOL_GPL(vb2_poll
);
1403 * vb2_queue_init() - initialize a videobuf2 queue
1404 * @q: videobuf2 queue; this structure should be allocated in driver
1406 * The vb2_queue structure should be allocated by the driver. The driver is
1407 * responsible of clearing it's content and setting initial values for some
1408 * required entries before calling this function.
1409 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
1410 * to the struct vb2_queue description in include/media/videobuf2-core.h
1411 * for more information.
1413 int vb2_queue_init(struct vb2_queue
*q
)
1417 BUG_ON(!q
->mem_ops
);
1419 BUG_ON(!q
->io_modes
);
1421 BUG_ON(!q
->ops
->queue_setup
);
1422 BUG_ON(!q
->ops
->buf_queue
);
1424 INIT_LIST_HEAD(&q
->queued_list
);
1425 INIT_LIST_HEAD(&q
->done_list
);
1426 spin_lock_init(&q
->done_lock
);
1427 init_waitqueue_head(&q
->done_wq
);
1429 if (q
->buf_struct_size
== 0)
1430 q
->buf_struct_size
= sizeof(struct vb2_buffer
);
1434 EXPORT_SYMBOL_GPL(vb2_queue_init
);
1437 * vb2_queue_release() - stop streaming, release the queue and free memory
1438 * @q: videobuf2 queue
1440 * This function stops streaming and performs necessary clean ups, including
1441 * freeing video buffer memory. The driver is responsible for freeing
1442 * the vb2_queue structure itself.
1444 void vb2_queue_release(struct vb2_queue
*q
)
1446 __vb2_cleanup_fileio(q
);
1447 __vb2_queue_cancel(q
);
1448 __vb2_queue_free(q
);
1450 EXPORT_SYMBOL_GPL(vb2_queue_release
);
1453 * struct vb2_fileio_buf - buffer context used by file io emulator
1455 * vb2 provides a compatibility layer and emulator of file io (read and
1456 * write) calls on top of streaming API. This structure is used for
1457 * tracking context related to the buffers.
1459 struct vb2_fileio_buf
{
1463 unsigned int queued
:1;
1467 * struct vb2_fileio_data - queue context used by file io emulator
1469 * vb2 provides a compatibility layer and emulator of file io (read and
1470 * write) calls on top of streaming API. For proper operation it required
1471 * this structure to save the driver state between each call of the read
1472 * or write function.
1474 struct vb2_fileio_data
{
1475 struct v4l2_requestbuffers req
;
1476 struct v4l2_buffer b
;
1477 struct vb2_fileio_buf bufs
[VIDEO_MAX_FRAME
];
1479 unsigned int q_count
;
1480 unsigned int dq_count
;
1485 * __vb2_init_fileio() - initialize file io emulator
1486 * @q: videobuf2 queue
1487 * @read: mode selector (1 means read, 0 means write)
1489 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
)
1491 struct vb2_fileio_data
*fileio
;
1493 unsigned int count
= 0;
1498 if ((read
&& !(q
->io_modes
& VB2_READ
)) ||
1499 (!read
&& !(q
->io_modes
& VB2_WRITE
)))
1503 * Check if device supports mapping buffers to kernel virtual space.
1505 if (!q
->mem_ops
->vaddr
)
1509 * Check if streaming api has not been already activated.
1511 if (q
->streaming
|| q
->num_buffers
> 0)
1515 * Start with count 1, driver can increase it in queue_setup()
1519 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
1520 (read
) ? "read" : "write", count
, q
->io_flags
);
1522 fileio
= kzalloc(sizeof(struct vb2_fileio_data
), GFP_KERNEL
);
1526 fileio
->flags
= q
->io_flags
;
1529 * Request buffers and use MMAP type to force driver
1530 * to allocate buffers by itself.
1532 fileio
->req
.count
= count
;
1533 fileio
->req
.memory
= V4L2_MEMORY_MMAP
;
1534 fileio
->req
.type
= q
->type
;
1535 ret
= vb2_reqbufs(q
, &fileio
->req
);
1540 * Check if plane_count is correct
1541 * (multiplane buffers are not supported).
1543 if (q
->bufs
[0]->num_planes
!= 1) {
1544 fileio
->req
.count
= 0;
1550 * Get kernel address of each buffer.
1552 for (i
= 0; i
< q
->num_buffers
; i
++) {
1553 fileio
->bufs
[i
].vaddr
= vb2_plane_vaddr(q
->bufs
[i
], 0);
1554 if (fileio
->bufs
[i
].vaddr
== NULL
)
1556 fileio
->bufs
[i
].size
= vb2_plane_size(q
->bufs
[i
], 0);
1560 * Read mode requires pre queuing of all buffers.
1564 * Queue all buffers.
1566 for (i
= 0; i
< q
->num_buffers
; i
++) {
1567 struct v4l2_buffer
*b
= &fileio
->b
;
1568 memset(b
, 0, sizeof(*b
));
1570 b
->memory
= q
->memory
;
1572 ret
= vb2_qbuf(q
, b
);
1575 fileio
->bufs
[i
].queued
= 1;
1581 ret
= vb2_streamon(q
, q
->type
);
1591 vb2_reqbufs(q
, &fileio
->req
);
1599 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1600 * @q: videobuf2 queue
1602 static int __vb2_cleanup_fileio(struct vb2_queue
*q
)
1604 struct vb2_fileio_data
*fileio
= q
->fileio
;
1608 * Hack fileio context to enable direct calls to vb2 ioctl
1613 vb2_streamoff(q
, q
->type
);
1614 fileio
->req
.count
= 0;
1615 vb2_reqbufs(q
, &fileio
->req
);
1617 dprintk(3, "file io emulator closed\n");
1623 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1624 * @q: videobuf2 queue
1625 * @data: pointed to target userspace buffer
1626 * @count: number of bytes to read or write
1627 * @ppos: file handle position tracking pointer
1628 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1629 * @read: access mode selector (1 means read, 0 means write)
1631 static size_t __vb2_perform_fileio(struct vb2_queue
*q
, char __user
*data
, size_t count
,
1632 loff_t
*ppos
, int nonblock
, int read
)
1634 struct vb2_fileio_data
*fileio
;
1635 struct vb2_fileio_buf
*buf
;
1638 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
1639 read
? "read" : "write", (long)*ppos
, count
,
1640 nonblock
? "non" : "");
1646 * Initialize emulator on first call.
1649 ret
= __vb2_init_fileio(q
, read
);
1650 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret
);
1657 * Hack fileio context to enable direct calls to vb2 ioctl interface.
1658 * The pointer will be restored before returning from this function.
1662 index
= fileio
->index
;
1663 buf
= &fileio
->bufs
[index
];
1666 * Check if we need to dequeue the buffer.
1669 struct vb2_buffer
*vb
;
1672 * Call vb2_dqbuf to get buffer back.
1674 memset(&fileio
->b
, 0, sizeof(fileio
->b
));
1675 fileio
->b
.type
= q
->type
;
1676 fileio
->b
.memory
= q
->memory
;
1677 fileio
->b
.index
= index
;
1678 ret
= vb2_dqbuf(q
, &fileio
->b
, nonblock
);
1679 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret
);
1682 fileio
->dq_count
+= 1;
1685 * Get number of bytes filled by the driver
1687 vb
= q
->bufs
[index
];
1688 buf
->size
= vb2_get_plane_payload(vb
, 0);
1693 * Limit count on last few bytes of the buffer.
1695 if (buf
->pos
+ count
> buf
->size
) {
1696 count
= buf
->size
- buf
->pos
;
1697 dprintk(5, "reducing read count: %zd\n", count
);
1701 * Transfer data to userspace.
1703 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
1704 count
, index
, buf
->pos
);
1706 ret
= copy_to_user(data
, buf
->vaddr
+ buf
->pos
, count
);
1708 ret
= copy_from_user(buf
->vaddr
+ buf
->pos
, data
, count
);
1710 dprintk(3, "file io: error copying data\n");
1722 * Queue next buffer if required.
1724 if (buf
->pos
== buf
->size
||
1725 (!read
&& (fileio
->flags
& VB2_FILEIO_WRITE_IMMEDIATELY
))) {
1727 * Check if this is the last buffer to read.
1729 if (read
&& (fileio
->flags
& VB2_FILEIO_READ_ONCE
) &&
1730 fileio
->dq_count
== 1) {
1731 dprintk(3, "file io: read limit reached\n");
1733 * Restore fileio pointer and release the context.
1736 return __vb2_cleanup_fileio(q
);
1740 * Call vb2_qbuf and give buffer to the driver.
1742 memset(&fileio
->b
, 0, sizeof(fileio
->b
));
1743 fileio
->b
.type
= q
->type
;
1744 fileio
->b
.memory
= q
->memory
;
1745 fileio
->b
.index
= index
;
1746 fileio
->b
.bytesused
= buf
->pos
;
1747 ret
= vb2_qbuf(q
, &fileio
->b
);
1748 dprintk(5, "file io: vb2_dbuf result: %d\n", ret
);
1753 * Buffer has been queued, update the status
1757 buf
->size
= q
->bufs
[0]->v4l2_planes
[0].length
;
1758 fileio
->q_count
+= 1;
1761 * Switch to the next buffer
1763 fileio
->index
= (index
+ 1) % q
->num_buffers
;
1766 * Start streaming if required.
1768 if (!read
&& !q
->streaming
) {
1769 ret
= vb2_streamon(q
, q
->type
);
1776 * Return proper number of bytes processed.
1782 * Restore the fileio context and block vb2 ioctl interface.
1788 size_t vb2_read(struct vb2_queue
*q
, char __user
*data
, size_t count
,
1789 loff_t
*ppos
, int nonblocking
)
1791 return __vb2_perform_fileio(q
, data
, count
, ppos
, nonblocking
, 1);
1793 EXPORT_SYMBOL_GPL(vb2_read
);
1795 size_t vb2_write(struct vb2_queue
*q
, char __user
*data
, size_t count
,
1796 loff_t
*ppos
, int nonblocking
)
1798 return __vb2_perform_fileio(q
, data
, count
, ppos
, nonblocking
, 0);
1800 EXPORT_SYMBOL_GPL(vb2_write
);
1802 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
1803 MODULE_AUTHOR("Pawel Osciak, Marek Szyprowski");
1804 MODULE_LICENSE("GPL");