aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/v4l2-core/videobuf2-core.c
diff options
context:
space:
mode:
authorJunghak Sung <jh1009.sung@samsung.com>2015-10-06 05:37:49 -0400
committerMauro Carvalho Chehab <mchehab@osg.samsung.com>2015-10-20 13:14:28 -0400
commit3c5be988e0b6a2f368e1659083b39e1f7ac909aa (patch)
tree255e4d1813b7b8024b75d9627db303811f8c649e /drivers/media/v4l2-core/videobuf2-core.c
parentb0e0e1f83de31aa0428c38b692c590cc0ecd3f03 (diff)
[media] media: videobuf2: Move v4l2-specific stuff to videobuf2-v4l2
Move v4l2-specific stuff from videobu2-core to videobuf2-v4l2 without doing any functional changes. Signed-off-by: Junghak Sung <jh1009.sung@samsung.com> Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com> Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com> Acked-by: Inki Dae <inki.dae@samsung.com> Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com> Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
Diffstat (limited to 'drivers/media/v4l2-core/videobuf2-core.c')
-rw-r--r--drivers/media/v4l2-core/videobuf2-core.c1827
1 files changed, 34 insertions, 1793 deletions
diff --git a/drivers/media/v4l2-core/videobuf2-core.c b/drivers/media/v4l2-core/videobuf2-core.c
index 2c057054555d..33bdd81065e8 100644
--- a/drivers/media/v4l2-core/videobuf2-core.c
+++ b/drivers/media/v4l2-core/videobuf2-core.c
@@ -24,172 +24,15 @@
24#include <linux/freezer.h> 24#include <linux/freezer.h>
25#include <linux/kthread.h> 25#include <linux/kthread.h>
26 26
27#include <media/v4l2-dev.h> 27#include <media/videobuf2-core.h>
28#include <media/v4l2-fh.h>
29#include <media/v4l2-event.h>
30#include <media/v4l2-common.h>
31#include <media/videobuf2-v4l2.h>
32 28
33#include <trace/events/vb2.h> 29#include <trace/events/vb2.h>
34 30
35static int debug; 31#include "videobuf2-internal.h"
36module_param(debug, int, 0644);
37 32
38#define dprintk(level, fmt, arg...) \ 33int vb2_debug;
39 do { \ 34EXPORT_SYMBOL_GPL(vb2_debug);
40 if (debug >= level) \ 35module_param_named(debug, vb2_debug, int, 0644);
41 pr_info("vb2: %s: " fmt, __func__, ## arg); \
42 } while (0)
43
44#ifdef CONFIG_VIDEO_ADV_DEBUG
45
46/*
47 * If advanced debugging is on, then count how often each op is called
48 * successfully, which can either be per-buffer or per-queue.
49 *
50 * This makes it easy to check that the 'init' and 'cleanup'
51 * (and variations thereof) stay balanced.
52 */
53
54#define log_memop(vb, op) \
55 dprintk(2, "call_memop(%p, %d, %s)%s\n", \
56 (vb)->vb2_queue, (vb)->index, #op, \
57 (vb)->vb2_queue->mem_ops->op ? "" : " (nop)")
58
59#define call_memop(vb, op, args...) \
60({ \
61 struct vb2_queue *_q = (vb)->vb2_queue; \
62 int err; \
63 \
64 log_memop(vb, op); \
65 err = _q->mem_ops->op ? _q->mem_ops->op(args) : 0; \
66 if (!err) \
67 (vb)->cnt_mem_ ## op++; \
68 err; \
69})
70
71#define call_ptr_memop(vb, op, args...) \
72({ \
73 struct vb2_queue *_q = (vb)->vb2_queue; \
74 void *ptr; \
75 \
76 log_memop(vb, op); \
77 ptr = _q->mem_ops->op ? _q->mem_ops->op(args) : NULL; \
78 if (!IS_ERR_OR_NULL(ptr)) \
79 (vb)->cnt_mem_ ## op++; \
80 ptr; \
81})
82
83#define call_void_memop(vb, op, args...) \
84({ \
85 struct vb2_queue *_q = (vb)->vb2_queue; \
86 \
87 log_memop(vb, op); \
88 if (_q->mem_ops->op) \
89 _q->mem_ops->op(args); \
90 (vb)->cnt_mem_ ## op++; \
91})
92
93#define log_qop(q, op) \
94 dprintk(2, "call_qop(%p, %s)%s\n", q, #op, \
95 (q)->ops->op ? "" : " (nop)")
96
97#define call_qop(q, op, args...) \
98({ \
99 int err; \
100 \
101 log_qop(q, op); \
102 err = (q)->ops->op ? (q)->ops->op(args) : 0; \
103 if (!err) \
104 (q)->cnt_ ## op++; \
105 err; \
106})
107
108#define call_void_qop(q, op, args...) \
109({ \
110 log_qop(q, op); \
111 if ((q)->ops->op) \
112 (q)->ops->op(args); \
113 (q)->cnt_ ## op++; \
114})
115
116#define log_vb_qop(vb, op, args...) \
117 dprintk(2, "call_vb_qop(%p, %d, %s)%s\n", \
118 (vb)->vb2_queue, (vb)->index, #op, \
119 (vb)->vb2_queue->ops->op ? "" : " (nop)")
120
121#define call_vb_qop(vb, op, args...) \
122({ \
123 int err; \
124 \
125 log_vb_qop(vb, op); \
126 err = (vb)->vb2_queue->ops->op ? \
127 (vb)->vb2_queue->ops->op(args) : 0; \
128 if (!err) \
129 (vb)->cnt_ ## op++; \
130 err; \
131})
132
133#define call_void_vb_qop(vb, op, args...) \
134({ \
135 log_vb_qop(vb, op); \
136 if ((vb)->vb2_queue->ops->op) \
137 (vb)->vb2_queue->ops->op(args); \
138 (vb)->cnt_ ## op++; \
139})
140
141#else
142
143#define call_memop(vb, op, args...) \
144 ((vb)->vb2_queue->mem_ops->op ? \
145 (vb)->vb2_queue->mem_ops->op(args) : 0)
146
147#define call_ptr_memop(vb, op, args...) \
148 ((vb)->vb2_queue->mem_ops->op ? \
149 (vb)->vb2_queue->mem_ops->op(args) : NULL)
150
151#define call_void_memop(vb, op, args...) \
152 do { \
153 if ((vb)->vb2_queue->mem_ops->op) \
154 (vb)->vb2_queue->mem_ops->op(args); \
155 } while (0)
156
157#define call_qop(q, op, args...) \
158 ((q)->ops->op ? (q)->ops->op(args) : 0)
159
160#define call_void_qop(q, op, args...) \
161 do { \
162 if ((q)->ops->op) \
163 (q)->ops->op(args); \
164 } while (0)
165
166#define call_vb_qop(vb, op, args...) \
167 ((vb)->vb2_queue->ops->op ? (vb)->vb2_queue->ops->op(args) : 0)
168
169#define call_void_vb_qop(vb, op, args...) \
170 do { \
171 if ((vb)->vb2_queue->ops->op) \
172 (vb)->vb2_queue->ops->op(args); \
173 } while (0)
174
175#endif
176
177#define call_bufop(q, op, args...) \
178({ \
179 int ret = 0; \
180 if (q && q->buf_ops && q->buf_ops->op) \
181 ret = q->buf_ops->op(args); \
182 ret; \
183})
184
185/* Flags that are set by the vb2 core */
186#define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
187 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
188 V4L2_BUF_FLAG_PREPARED | \
189 V4L2_BUF_FLAG_TIMESTAMP_MASK)
190/* Output buffer flags that should be passed on to the driver */
191#define V4L2_BUFFER_OUT_FLAGS (V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_BFRAME | \
192 V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_TIMECODE)
193 36
194static void __vb2_queue_cancel(struct vb2_queue *q); 37static void __vb2_queue_cancel(struct vb2_queue *q);
195static void __enqueue_in_driver(struct vb2_buffer *vb); 38static void __enqueue_in_driver(struct vb2_buffer *vb);
@@ -487,7 +330,7 @@ static int __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
487 bool unbalanced = q->cnt_start_streaming != q->cnt_stop_streaming || 330 bool unbalanced = q->cnt_start_streaming != q->cnt_stop_streaming ||
488 q->cnt_wait_prepare != q->cnt_wait_finish; 331 q->cnt_wait_prepare != q->cnt_wait_finish;
489 332
490 if (unbalanced || debug) { 333 if (unbalanced || vb2_debug) {
491 pr_info("vb2: counters for queue %p:%s\n", q, 334 pr_info("vb2: counters for queue %p:%s\n", q,
492 unbalanced ? " UNBALANCED!" : ""); 335 unbalanced ? " UNBALANCED!" : "");
493 pr_info("vb2: setup: %u start_streaming: %u stop_streaming: %u\n", 336 pr_info("vb2: setup: %u start_streaming: %u stop_streaming: %u\n",
@@ -513,7 +356,7 @@ static int __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
513 vb->cnt_buf_prepare != vb->cnt_buf_finish || 356 vb->cnt_buf_prepare != vb->cnt_buf_finish ||
514 vb->cnt_buf_init != vb->cnt_buf_cleanup; 357 vb->cnt_buf_init != vb->cnt_buf_cleanup;
515 358
516 if (unbalanced || debug) { 359 if (unbalanced || vb2_debug) {
517 pr_info("vb2: counters for queue %p, buffer %d:%s\n", 360 pr_info("vb2: counters for queue %p, buffer %d:%s\n",
518 q, buffer, unbalanced ? " UNBALANCED!" : ""); 361 q, buffer, unbalanced ? " UNBALANCED!" : "");
519 pr_info("vb2: buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n", 362 pr_info("vb2: buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n",
@@ -555,75 +398,10 @@ static int __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
555} 398}
556 399
557/** 400/**
558 * __verify_planes_array() - verify that the planes array passed in struct
559 * v4l2_buffer from userspace can be safely used
560 */
561static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
562{
563 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
564 return 0;
565
566 /* Is memory for copying plane information present? */
567 if (NULL == b->m.planes) {
568 dprintk(1, "multi-planar buffer passed but "
569 "planes array not provided\n");
570 return -EINVAL;
571 }
572
573 if (b->length < vb->num_planes || b->length > VB2_MAX_PLANES) {
574 dprintk(1, "incorrect planes array length, "
575 "expected %d, got %d\n", vb->num_planes, b->length);
576 return -EINVAL;
577 }
578
579 return 0;
580}
581
582/**
583 * __verify_length() - Verify that the bytesused value for each plane fits in
584 * the plane length and that the data offset doesn't exceed the bytesused value.
585 */
586static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
587{
588 unsigned int length;
589 unsigned int bytesused;
590 unsigned int plane;
591
592 if (!V4L2_TYPE_IS_OUTPUT(b->type))
593 return 0;
594
595 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
596 for (plane = 0; plane < vb->num_planes; ++plane) {
597 length = (b->memory == VB2_MEMORY_USERPTR ||
598 b->memory == VB2_MEMORY_DMABUF)
599 ? b->m.planes[plane].length
600 : vb->planes[plane].length;
601 bytesused = b->m.planes[plane].bytesused
602 ? b->m.planes[plane].bytesused : length;
603
604 if (b->m.planes[plane].bytesused > length)
605 return -EINVAL;
606
607 if (b->m.planes[plane].data_offset > 0 &&
608 b->m.planes[plane].data_offset >= bytesused)
609 return -EINVAL;
610 }
611 } else {
612 length = (b->memory == VB2_MEMORY_USERPTR)
613 ? b->length : vb->planes[0].length;
614
615 if (b->bytesused > length)
616 return -EINVAL;
617 }
618
619 return 0;
620}
621
622/**
623 * vb2_buffer_in_use() - return true if the buffer is in use and 401 * vb2_buffer_in_use() - return true if the buffer is in use and
624 * the queue cannot be freed (by the means of REQBUFS(0)) call 402 * the queue cannot be freed (by the means of REQBUFS(0)) call
625 */ 403 */
626static bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb) 404bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
627{ 405{
628 unsigned int plane; 406 unsigned int plane;
629 for (plane = 0; plane < vb->num_planes; ++plane) { 407 for (plane = 0; plane < vb->num_planes; ++plane) {
@@ -639,6 +417,7 @@ static bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
639 } 417 }
640 return false; 418 return false;
641} 419}
420EXPORT_SYMBOL(vb2_buffer_in_use);
642 421
643/** 422/**
644 * __buffers_in_use() - return true if any buffers on the queue are in use and 423 * __buffers_in_use() - return true if any buffers on the queue are in use and
@@ -655,109 +434,6 @@ static bool __buffers_in_use(struct vb2_queue *q)
655} 434}
656 435
657/** 436/**
658 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
659 * returned to userspace
660 */
661static int __fill_v4l2_buffer(struct vb2_buffer *vb, void *pb)
662{
663 struct v4l2_buffer *b = pb;
664 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
665 struct vb2_queue *q = vb->vb2_queue;
666 unsigned int plane;
667
668 /* Copy back data such as timestamp, flags, etc. */
669 b->index = vb->index;
670 b->type = vb->type;
671 b->memory = vb->memory;
672 b->bytesused = 0;
673
674 b->flags = vbuf->flags;
675 b->field = vbuf->field;
676 b->timestamp = vbuf->timestamp;
677 b->timecode = vbuf->timecode;
678 b->sequence = vbuf->sequence;
679 b->reserved2 = 0;
680 b->reserved = 0;
681
682 if (q->is_multiplanar) {
683 /*
684 * Fill in plane-related data if userspace provided an array
685 * for it. The caller has already verified memory and size.
686 */
687 b->length = vb->num_planes;
688 for (plane = 0; plane < vb->num_planes; ++plane) {
689 struct v4l2_plane *pdst = &b->m.planes[plane];
690 struct vb2_plane *psrc = &vb->planes[plane];
691
692 pdst->bytesused = psrc->bytesused;
693 pdst->length = psrc->length;
694 if (q->memory == VB2_MEMORY_MMAP)
695 pdst->m.mem_offset = psrc->m.offset;
696 else if (q->memory == VB2_MEMORY_USERPTR)
697 pdst->m.userptr = psrc->m.userptr;
698 else if (q->memory == VB2_MEMORY_DMABUF)
699 pdst->m.fd = psrc->m.fd;
700 pdst->data_offset = psrc->data_offset;
701 memset(pdst->reserved, 0, sizeof(pdst->reserved));
702 }
703 } else {
704 /*
705 * We use length and offset in v4l2_planes array even for
706 * single-planar buffers, but userspace does not.
707 */
708 b->length = vb->planes[0].length;
709 b->bytesused = vb->planes[0].bytesused;
710 if (q->memory == VB2_MEMORY_MMAP)
711 b->m.offset = vb->planes[0].m.offset;
712 else if (q->memory == VB2_MEMORY_USERPTR)
713 b->m.userptr = vb->planes[0].m.userptr;
714 else if (q->memory == VB2_MEMORY_DMABUF)
715 b->m.fd = vb->planes[0].m.fd;
716 }
717
718 /*
719 * Clear any buffer state related flags.
720 */
721 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
722 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
723 if ((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) !=
724 V4L2_BUF_FLAG_TIMESTAMP_COPY) {
725 /*
726 * For non-COPY timestamps, drop timestamp source bits
727 * and obtain the timestamp source from the queue.
728 */
729 b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
730 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
731 }
732
733 switch (vb->state) {
734 case VB2_BUF_STATE_QUEUED:
735 case VB2_BUF_STATE_ACTIVE:
736 b->flags |= V4L2_BUF_FLAG_QUEUED;
737 break;
738 case VB2_BUF_STATE_ERROR:
739 b->flags |= V4L2_BUF_FLAG_ERROR;
740 /* fall through */
741 case VB2_BUF_STATE_DONE:
742 b->flags |= V4L2_BUF_FLAG_DONE;
743 break;
744 case VB2_BUF_STATE_PREPARED:
745 b->flags |= V4L2_BUF_FLAG_PREPARED;
746 break;
747 case VB2_BUF_STATE_PREPARING:
748 case VB2_BUF_STATE_DEQUEUED:
749 case VB2_BUF_STATE_REQUEUEING:
750 /* nothing */
751 break;
752 }
753
754 if (vb2_buffer_in_use(q, vb))
755 b->flags |= V4L2_BUF_FLAG_MAPPED;
756
757 return 0;
758}
759
760/**
761 * vb2_core_querybuf() - query video buffer information 437 * vb2_core_querybuf() - query video buffer information
762 * @q: videobuf queue 438 * @q: videobuf queue
763 * @index: id number of the buffer 439 * @index: id number of the buffer
@@ -770,44 +446,11 @@ static int __fill_v4l2_buffer(struct vb2_buffer *vb, void *pb)
770 * The return values from this function are intended to be directly returned 446 * The return values from this function are intended to be directly returned
771 * from vidioc_querybuf handler in driver. 447 * from vidioc_querybuf handler in driver.
772 */ 448 */
773static int vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb) 449int vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb)
774{ 450{
775 return call_bufop(q, fill_user_buffer, q->bufs[index], pb); 451 return call_bufop(q, fill_user_buffer, q->bufs[index], pb);
776} 452}
777 453EXPORT_SYMBOL_GPL(vb2_core_querybuf);
778/**
779 * vb2_querybuf() - query video buffer information
780 * @q: videobuf queue
781 * @b: buffer struct passed from userspace to vidioc_querybuf handler
782 * in driver
783 *
784 * Should be called from vidioc_querybuf ioctl handler in driver.
785 * This function will verify the passed v4l2_buffer structure and fill the
786 * relevant information for the userspace.
787 *
788 * The return values from this function are intended to be directly returned
789 * from vidioc_querybuf handler in driver.
790 */
791int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
792{
793 struct vb2_buffer *vb;
794 int ret;
795
796 if (b->type != q->type) {
797 dprintk(1, "wrong buffer type\n");
798 return -EINVAL;
799 }
800
801 if (b->index >= q->num_buffers) {
802 dprintk(1, "buffer index out of range\n");
803 return -EINVAL;
804 }
805 vb = q->bufs[b->index];
806 ret = __verify_planes_array(vb, b);
807
808 return ret ? ret : vb2_core_querybuf(q, b->index, b);
809}
810EXPORT_SYMBOL(vb2_querybuf);
811 454
812/** 455/**
813 * __verify_userptr_ops() - verify that all memory operations required for 456 * __verify_userptr_ops() - verify that all memory operations required for
@@ -853,7 +496,7 @@ static int __verify_dmabuf_ops(struct vb2_queue *q)
853 * vb2_verify_memory_type() - Check whether the memory type and buffer type 496 * vb2_verify_memory_type() - Check whether the memory type and buffer type
854 * passed to a buffer operation are compatible with the queue. 497 * passed to a buffer operation are compatible with the queue.
855 */ 498 */
856static int vb2_verify_memory_type(struct vb2_queue *q, 499int vb2_verify_memory_type(struct vb2_queue *q,
857 enum vb2_memory memory, unsigned int type) 500 enum vb2_memory memory, unsigned int type)
858{ 501{
859 if (memory != VB2_MEMORY_MMAP && memory != VB2_MEMORY_USERPTR && 502 if (memory != VB2_MEMORY_MMAP && memory != VB2_MEMORY_USERPTR &&
@@ -897,6 +540,7 @@ static int vb2_verify_memory_type(struct vb2_queue *q,
897 } 540 }
898 return 0; 541 return 0;
899} 542}
543EXPORT_SYMBOL(vb2_verify_memory_type);
900 544
901/** 545/**
902 * vb2_core_reqbufs() - Initiate streaming 546 * vb2_core_reqbufs() - Initiate streaming
@@ -922,7 +566,7 @@ static int vb2_verify_memory_type(struct vb2_queue *q,
922 * The return values from this function are intended to be directly returned 566 * The return values from this function are intended to be directly returned
923 * from vidioc_reqbufs handler in driver. 567 * from vidioc_reqbufs handler in driver.
924 */ 568 */
925static int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, 569int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
926 unsigned int *count) 570 unsigned int *count)
927{ 571{
928 unsigned int num_buffers, allocated_buffers, num_planes = 0; 572 unsigned int num_buffers, allocated_buffers, num_planes = 0;
@@ -1038,21 +682,7 @@ static int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
1038 682
1039 return 0; 683 return 0;
1040} 684}
1041 685EXPORT_SYMBOL_GPL(vb2_core_reqbufs);
1042/**
1043 * vb2_reqbufs() - Wrapper for vb2_core_reqbufs() that also verifies
1044 * the memory and type values.
1045 * @q: videobuf2 queue
1046 * @req: struct passed from userspace to vidioc_reqbufs handler
1047 * in driver
1048 */
1049int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
1050{
1051 int ret = vb2_verify_memory_type(q, req->memory, req->type);
1052
1053 return ret ? ret : vb2_core_reqbufs(q, req->memory, &req->count);
1054}
1055EXPORT_SYMBOL_GPL(vb2_reqbufs);
1056 686
1057/** 687/**
1058 * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs 688 * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs
@@ -1070,7 +700,7 @@ EXPORT_SYMBOL_GPL(vb2_reqbufs);
1070 * The return values from this function are intended to be directly returned 700 * The return values from this function are intended to be directly returned
1071 * from vidioc_create_bufs handler in driver. 701 * from vidioc_create_bufs handler in driver.
1072 */ 702 */
1073static int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, 703int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
1074 unsigned int *count, const void *parg) 704 unsigned int *count, const void *parg)
1075{ 705{
1076 unsigned int num_planes = 0, num_buffers, allocated_buffers; 706 unsigned int num_planes = 0, num_buffers, allocated_buffers;
@@ -1151,26 +781,7 @@ static int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
1151 781
1152 return 0; 782 return 0;
1153} 783}
1154 784EXPORT_SYMBOL_GPL(vb2_core_create_bufs);
1155/**
1156 * vb2_create_bufs() - Wrapper for vb2_core_create_bufs() that also verifies
1157 * the memory and type values.
1158 * @q: videobuf2 queue
1159 * @create: creation parameters, passed from userspace to vidioc_create_bufs
1160 * handler in driver
1161 */
1162int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
1163{
1164 int ret = vb2_verify_memory_type(q, create->memory,
1165 create->format.type);
1166
1167 create->index = q->num_buffers;
1168 if (create->count == 0)
1169 return ret != -EBUSY ? ret : 0;
1170 return ret ? ret : vb2_core_create_bufs(q, create->memory,
1171 &create->count, &create->format);
1172}
1173EXPORT_SYMBOL_GPL(vb2_create_bufs);
1174 785
1175/** 786/**
1176 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane 787 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
@@ -1312,201 +923,6 @@ void vb2_discard_done(struct vb2_queue *q)
1312} 923}
1313EXPORT_SYMBOL_GPL(vb2_discard_done); 924EXPORT_SYMBOL_GPL(vb2_discard_done);
1314 925
1315static void vb2_warn_zero_bytesused(struct vb2_buffer *vb)
1316{
1317 static bool check_once;
1318
1319 if (check_once)
1320 return;
1321
1322 check_once = true;
1323 WARN_ON(1);
1324
1325 pr_warn("use of bytesused == 0 is deprecated and will be removed in the future,\n");
1326 if (vb->vb2_queue->allow_zero_bytesused)
1327 pr_warn("use VIDIOC_DECODER_CMD(V4L2_DEC_CMD_STOP) instead.\n");
1328 else
1329 pr_warn("use the actual size instead.\n");
1330}
1331
1332static int __set_timestamp(struct vb2_buffer *vb, const void *pb)
1333{
1334 const struct v4l2_buffer *b = pb;
1335 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1336 struct vb2_queue *q = vb->vb2_queue;
1337
1338 if (q->is_output) {
1339 /*
1340 * For output buffers copy the timestamp if needed,
1341 * and the timecode field and flag if needed.
1342 */
1343 if ((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
1344 V4L2_BUF_FLAG_TIMESTAMP_COPY)
1345 vbuf->timestamp = b->timestamp;
1346 vbuf->flags |= b->flags & V4L2_BUF_FLAG_TIMECODE;
1347 if (b->flags & V4L2_BUF_FLAG_TIMECODE)
1348 vbuf->timecode = b->timecode;
1349 }
1350
1351 return 0;
1352};
1353
1354/**
1355 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
1356 * v4l2_buffer by the userspace. It also verifies that struct
1357 * v4l2_buffer has a valid number of planes.
1358 */
1359static int __fill_vb2_buffer(struct vb2_buffer *vb,
1360 const void *pb, struct vb2_plane *planes)
1361{
1362 struct vb2_queue *q = vb->vb2_queue;
1363 const struct v4l2_buffer *b = pb;
1364 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1365 unsigned int plane;
1366 int ret;
1367
1368 ret = __verify_length(vb, b);
1369 if (ret < 0) {
1370 dprintk(1, "plane parameters verification failed: %d\n", ret);
1371 return ret;
1372 }
1373 if (b->field == V4L2_FIELD_ALTERNATE && q->is_output) {
1374 /*
1375 * If the format's field is ALTERNATE, then the buffer's field
1376 * should be either TOP or BOTTOM, not ALTERNATE since that
1377 * makes no sense. The driver has to know whether the
1378 * buffer represents a top or a bottom field in order to
1379 * program any DMA correctly. Using ALTERNATE is wrong, since
1380 * that just says that it is either a top or a bottom field,
1381 * but not which of the two it is.
1382 */
1383 dprintk(1, "the field is incorrectly set to ALTERNATE "
1384 "for an output buffer\n");
1385 return -EINVAL;
1386 }
1387 vbuf->timestamp.tv_sec = 0;
1388 vbuf->timestamp.tv_usec = 0;
1389 vbuf->sequence = 0;
1390
1391 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
1392 if (b->memory == VB2_MEMORY_USERPTR) {
1393 for (plane = 0; plane < vb->num_planes; ++plane) {
1394 planes[plane].m.userptr =
1395 b->m.planes[plane].m.userptr;
1396 planes[plane].length =
1397 b->m.planes[plane].length;
1398 }
1399 }
1400 if (b->memory == VB2_MEMORY_DMABUF) {
1401 for (plane = 0; plane < vb->num_planes; ++plane) {
1402 planes[plane].m.fd =
1403 b->m.planes[plane].m.fd;
1404 planes[plane].length =
1405 b->m.planes[plane].length;
1406 }
1407 }
1408
1409 /* Fill in driver-provided information for OUTPUT types */
1410 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
1411 /*
1412 * Will have to go up to b->length when API starts
1413 * accepting variable number of planes.
1414 *
1415 * If bytesused == 0 for the output buffer, then fall
1416 * back to the full buffer size. In that case
1417 * userspace clearly never bothered to set it and
1418 * it's a safe assumption that they really meant to
1419 * use the full plane sizes.
1420 *
1421 * Some drivers, e.g. old codec drivers, use bytesused == 0
1422 * as a way to indicate that streaming is finished.
1423 * In that case, the driver should use the
1424 * allow_zero_bytesused flag to keep old userspace
1425 * applications working.
1426 */
1427 for (plane = 0; plane < vb->num_planes; ++plane) {
1428 struct vb2_plane *pdst = &planes[plane];
1429 struct v4l2_plane *psrc = &b->m.planes[plane];
1430
1431 if (psrc->bytesused == 0)
1432 vb2_warn_zero_bytesused(vb);
1433
1434 if (vb->vb2_queue->allow_zero_bytesused)
1435 pdst->bytesused = psrc->bytesused;
1436 else
1437 pdst->bytesused = psrc->bytesused ?
1438 psrc->bytesused : pdst->length;
1439 pdst->data_offset = psrc->data_offset;
1440 }
1441 }
1442 } else {
1443 /*
1444 * Single-planar buffers do not use planes array,
1445 * so fill in relevant v4l2_buffer struct fields instead.
1446 * In videobuf we use our internal V4l2_planes struct for
1447 * single-planar buffers as well, for simplicity.
1448 *
1449 * If bytesused == 0 for the output buffer, then fall back
1450 * to the full buffer size as that's a sensible default.
1451 *
1452 * Some drivers, e.g. old codec drivers, use bytesused == 0 as
1453 * a way to indicate that streaming is finished. In that case,
1454 * the driver should use the allow_zero_bytesused flag to keep
1455 * old userspace applications working.
1456 */
1457 if (b->memory == VB2_MEMORY_USERPTR) {
1458 planes[0].m.userptr = b->m.userptr;
1459 planes[0].length = b->length;
1460 }
1461
1462 if (b->memory == VB2_MEMORY_DMABUF) {
1463 planes[0].m.fd = b->m.fd;
1464 planes[0].length = b->length;
1465 }
1466
1467 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
1468 if (b->bytesused == 0)
1469 vb2_warn_zero_bytesused(vb);
1470
1471 if (vb->vb2_queue->allow_zero_bytesused)
1472 planes[0].bytesused = b->bytesused;
1473 else
1474 planes[0].bytesused = b->bytesused ?
1475 b->bytesused : planes[0].length;
1476 } else
1477 planes[0].bytesused = 0;
1478
1479 }
1480
1481 /* Zero flags that the vb2 core handles */
1482 vbuf->flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
1483 if ((vb->vb2_queue->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) !=
1484 V4L2_BUF_FLAG_TIMESTAMP_COPY || !V4L2_TYPE_IS_OUTPUT(b->type)) {
1485 /*
1486 * Non-COPY timestamps and non-OUTPUT queues will get
1487 * their timestamp and timestamp source flags from the
1488 * queue.
1489 */
1490 vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
1491 }
1492
1493 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
1494 /*
1495 * For output buffers mask out the timecode flag:
1496 * this will be handled later in vb2_internal_qbuf().
1497 * The 'field' is valid metadata for this output buffer
1498 * and so that needs to be copied here.
1499 */
1500 vbuf->flags &= ~V4L2_BUF_FLAG_TIMECODE;
1501 vbuf->field = b->field;
1502 } else {
1503 /* Zero any output buffer flags as this is a capture buffer */
1504 vbuf->flags &= ~V4L2_BUFFER_OUT_FLAGS;
1505 }
1506
1507 return 0;
1508}
1509
1510/** 926/**
1511 * __qbuf_mmap() - handle qbuf of an MMAP buffer 927 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1512 */ 928 */
@@ -1814,33 +1230,6 @@ static int __buf_prepare(struct vb2_buffer *vb, const void *pb)
1814 return ret; 1230 return ret;
1815} 1231}
1816 1232
1817static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
1818 const char *opname)
1819{
1820 if (b->type != q->type) {
1821 dprintk(1, "%s: invalid buffer type\n", opname);
1822 return -EINVAL;
1823 }
1824
1825 if (b->index >= q->num_buffers) {
1826 dprintk(1, "%s: buffer index out of range\n", opname);
1827 return -EINVAL;
1828 }
1829
1830 if (q->bufs[b->index] == NULL) {
1831 /* Should never happen */
1832 dprintk(1, "%s: buffer is NULL\n", opname);
1833 return -EINVAL;
1834 }
1835
1836 if (b->memory != q->memory) {
1837 dprintk(1, "%s: invalid memory type\n", opname);
1838 return -EINVAL;
1839 }
1840
1841 return __verify_planes_array(q->bufs[b->index], b);
1842}
1843
1844/** 1233/**
1845 * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace 1234 * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace
1846 * to the kernel 1235 * to the kernel
@@ -1857,7 +1246,7 @@ static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
1857 * The return values from this function are intended to be directly returned 1246 * The return values from this function are intended to be directly returned
1858 * from vidioc_prepare_buf handler in driver. 1247 * from vidioc_prepare_buf handler in driver.
1859 */ 1248 */
1860static int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb) 1249int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb)
1861{ 1250{
1862 struct vb2_buffer *vb; 1251 struct vb2_buffer *vb;
1863 int ret; 1252 int ret;
@@ -1882,36 +1271,7 @@ static int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *p
1882 1271
1883 return ret; 1272 return ret;
1884} 1273}
1885 1274EXPORT_SYMBOL_GPL(vb2_core_prepare_buf);
1886/**
1887 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1888 * @q: videobuf2 queue
1889 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1890 * handler in driver
1891 *
1892 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1893 * This function:
1894 * 1) verifies the passed buffer,
1895 * 2) calls buf_prepare callback in the driver (if provided), in which
1896 * driver-specific buffer initialization can be performed,
1897 *
1898 * The return values from this function are intended to be directly returned
1899 * from vidioc_prepare_buf handler in driver.
1900 */
1901int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1902{
1903 int ret;
1904
1905 if (vb2_fileio_is_active(q)) {
1906 dprintk(1, "file io in progress\n");
1907 return -EBUSY;
1908 }
1909
1910 ret = vb2_queue_or_prepare_buf(q, b, "prepare_buf");
1911
1912 return ret ? ret : vb2_core_prepare_buf(q, b->index, b);
1913}
1914EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1915 1275
1916/** 1276/**
1917 * vb2_start_streaming() - Attempt to start streaming. 1277 * vb2_start_streaming() - Attempt to start streaming.
@@ -1994,7 +1354,7 @@ static int vb2_start_streaming(struct vb2_queue *q)
1994 * The return values from this function are intended to be directly returned 1354 * The return values from this function are intended to be directly returned
1995 * from vidioc_qbuf handler in driver. 1355 * from vidioc_qbuf handler in driver.
1996 */ 1356 */
1997static int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb) 1357int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb)
1998{ 1358{
1999 struct vb2_buffer *vb; 1359 struct vb2_buffer *vb;
2000 int ret; 1360 int ret;
@@ -2026,7 +1386,7 @@ static int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb)
2026 q->waiting_for_buffers = false; 1386 q->waiting_for_buffers = false;
2027 vb->state = VB2_BUF_STATE_QUEUED; 1387 vb->state = VB2_BUF_STATE_QUEUED;
2028 1388
2029 __set_timestamp(vb, pb); 1389 call_bufop(q, set_timestamp, vb, pb);
2030 1390
2031 trace_vb2_qbuf(q, vb); 1391 trace_vb2_qbuf(q, vb);
2032 1392
@@ -2058,41 +1418,7 @@ static int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb)
2058 dprintk(1, "qbuf of buffer %d succeeded\n", vb->index); 1418 dprintk(1, "qbuf of buffer %d succeeded\n", vb->index);
2059 return 0; 1419 return 0;
2060} 1420}
2061 1421EXPORT_SYMBOL_GPL(vb2_core_qbuf);
2062static int vb2_internal_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
2063{
2064 int ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
2065
2066 return ret ? ret : vb2_core_qbuf(q, b->index, b);
2067}
2068
2069/**
2070 * vb2_qbuf() - Queue a buffer from userspace
2071 * @q: videobuf2 queue
2072 * @b: buffer structure passed from userspace to vidioc_qbuf handler
2073 * in driver
2074 *
2075 * Should be called from vidioc_qbuf ioctl handler of a driver.
2076 * This function:
2077 * 1) verifies the passed buffer,
2078 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
2079 * which driver-specific buffer initialization can be performed,
2080 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
2081 * callback for processing.
2082 *
2083 * The return values from this function are intended to be directly returned
2084 * from vidioc_qbuf handler in driver.
2085 */
2086int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
2087{
2088 if (vb2_fileio_is_active(q)) {
2089 dprintk(1, "file io in progress\n");
2090 return -EBUSY;
2091 }
2092
2093 return vb2_internal_qbuf(q, b);
2094}
2095EXPORT_SYMBOL_GPL(vb2_qbuf);
2096 1422
2097/** 1423/**
2098 * __vb2_wait_for_done_vb() - wait for a buffer to become available 1424 * __vb2_wait_for_done_vb() - wait for a buffer to become available
@@ -2273,7 +1599,7 @@ static void __vb2_dqbuf(struct vb2_buffer *vb)
2273 * The return values from this function are intended to be directly returned 1599 * The return values from this function are intended to be directly returned
2274 * from vidioc_dqbuf handler in driver. 1600 * from vidioc_dqbuf handler in driver.
2275 */ 1601 */
2276static int vb2_core_dqbuf(struct vb2_queue *q, void *pb, bool nonblocking) 1602int vb2_core_dqbuf(struct vb2_queue *q, void *pb, bool nonblocking)
2277{ 1603{
2278 struct vb2_buffer *vb = NULL; 1604 struct vb2_buffer *vb = NULL;
2279 int ret; 1605 int ret;
@@ -2316,56 +1642,7 @@ static int vb2_core_dqbuf(struct vb2_queue *q, void *pb, bool nonblocking)
2316 return 0; 1642 return 0;
2317 1643
2318} 1644}
2319 1645EXPORT_SYMBOL_GPL(vb2_core_dqbuf);
2320static int vb2_internal_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b,
2321 bool nonblocking)
2322{
2323 int ret;
2324
2325 if (b->type != q->type) {
2326 dprintk(1, "invalid buffer type\n");
2327 return -EINVAL;
2328 }
2329
2330 ret = vb2_core_dqbuf(q, b, nonblocking);
2331
2332 if (!ret && !q->is_output &&
2333 b->flags & V4L2_BUF_FLAG_LAST)
2334 q->last_buffer_dequeued = true;
2335
2336 return ret;
2337}
2338
2339/**
2340 * vb2_dqbuf() - Dequeue a buffer to the userspace
2341 * @q: videobuf2 queue
2342 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
2343 * in driver
2344 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
2345 * buffers ready for dequeuing are present. Normally the driver
2346 * would be passing (file->f_flags & O_NONBLOCK) here
2347 *
2348 * Should be called from vidioc_dqbuf ioctl handler of a driver.
2349 * This function:
2350 * 1) verifies the passed buffer,
2351 * 2) calls buf_finish callback in the driver (if provided), in which
2352 * driver can perform any additional operations that may be required before
2353 * returning the buffer to userspace, such as cache sync,
2354 * 3) the buffer struct members are filled with relevant information for
2355 * the userspace.
2356 *
2357 * The return values from this function are intended to be directly returned
2358 * from vidioc_dqbuf handler in driver.
2359 */
2360int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
2361{
2362 if (vb2_fileio_is_active(q)) {
2363 dprintk(1, "file io in progress\n");
2364 return -EBUSY;
2365 }
2366 return vb2_internal_dqbuf(q, b, nonblocking);
2367}
2368EXPORT_SYMBOL_GPL(vb2_dqbuf);
2369 1646
2370/** 1647/**
2371 * __vb2_queue_cancel() - cancel and stop (pause) streaming 1648 * __vb2_queue_cancel() - cancel and stop (pause) streaming
@@ -2435,7 +1712,7 @@ static void __vb2_queue_cancel(struct vb2_queue *q)
2435 } 1712 }
2436} 1713}
2437 1714
2438static int vb2_core_streamon(struct vb2_queue *q, unsigned int type) 1715int vb2_core_streamon(struct vb2_queue *q, unsigned int type)
2439{ 1716{
2440 int ret; 1717 int ret;
2441 1718
@@ -2477,6 +1754,7 @@ static int vb2_core_streamon(struct vb2_queue *q, unsigned int type)
2477 dprintk(3, "successful\n"); 1754 dprintk(3, "successful\n");
2478 return 0; 1755 return 0;
2479} 1756}
1757EXPORT_SYMBOL_GPL(vb2_core_streamon);
2480 1758
2481/** 1759/**
2482 * vb2_queue_error() - signal a fatal error on the queue 1760 * vb2_queue_error() - signal a fatal error on the queue
@@ -2499,30 +1777,7 @@ void vb2_queue_error(struct vb2_queue *q)
2499} 1777}
2500EXPORT_SYMBOL_GPL(vb2_queue_error); 1778EXPORT_SYMBOL_GPL(vb2_queue_error);
2501 1779
2502/** 1780int vb2_core_streamoff(struct vb2_queue *q, unsigned int type)
2503 * vb2_streamon - start streaming
2504 * @q: videobuf2 queue
2505 * @type: type argument passed from userspace to vidioc_streamon handler
2506 *
2507 * Should be called from vidioc_streamon handler of a driver.
2508 * This function:
2509 * 1) verifies current state
2510 * 2) passes any previously queued buffers to the driver and starts streaming
2511 *
2512 * The return values from this function are intended to be directly returned
2513 * from vidioc_streamon handler in the driver.
2514 */
2515int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
2516{
2517 if (vb2_fileio_is_active(q)) {
2518 dprintk(1, "file io in progress\n");
2519 return -EBUSY;
2520 }
2521 return vb2_core_streamon(q, type);
2522}
2523EXPORT_SYMBOL_GPL(vb2_streamon);
2524
2525static int vb2_core_streamoff(struct vb2_queue *q, unsigned int type)
2526{ 1781{
2527 if (type != q->type) { 1782 if (type != q->type) {
2528 dprintk(1, "invalid stream type\n"); 1783 dprintk(1, "invalid stream type\n");
@@ -2545,31 +1800,7 @@ static int vb2_core_streamoff(struct vb2_queue *q, unsigned int type)
2545 dprintk(3, "successful\n"); 1800 dprintk(3, "successful\n");
2546 return 0; 1801 return 0;
2547} 1802}
2548 1803EXPORT_SYMBOL_GPL(vb2_core_streamoff);
2549/**
2550 * vb2_streamoff - stop streaming
2551 * @q: videobuf2 queue
2552 * @type: type argument passed from userspace to vidioc_streamoff handler
2553 *
2554 * Should be called from vidioc_streamoff handler of a driver.
2555 * This function:
2556 * 1) verifies current state,
2557 * 2) stop streaming and dequeues any queued buffers, including those previously
2558 * passed to the driver (after waiting for the driver to finish).
2559 *
2560 * This call can be used for pausing playback.
2561 * The return values from this function are intended to be directly returned
2562 * from vidioc_streamoff handler in the driver
2563 */
2564int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
2565{
2566 if (vb2_fileio_is_active(q)) {
2567 dprintk(1, "file io in progress\n");
2568 return -EBUSY;
2569 }
2570 return vb2_core_streamoff(q, type);
2571}
2572EXPORT_SYMBOL_GPL(vb2_streamoff);
2573 1804
2574/** 1805/**
2575 * __find_plane_by_offset() - find plane associated with the given offset off 1806 * __find_plane_by_offset() - find plane associated with the given offset off
@@ -2613,7 +1844,7 @@ static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
2613 * The return values from this function are intended to be directly returned 1844 * The return values from this function are intended to be directly returned
2614 * from vidioc_expbuf handler in driver. 1845 * from vidioc_expbuf handler in driver.
2615 */ 1846 */
2616static int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type, 1847int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
2617 unsigned int index, unsigned int plane, unsigned int flags) 1848 unsigned int index, unsigned int plane, unsigned int flags)
2618{ 1849{
2619 struct vb2_buffer *vb = NULL; 1850 struct vb2_buffer *vb = NULL;
@@ -2682,22 +1913,7 @@ static int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
2682 1913
2683 return 0; 1914 return 0;
2684} 1915}
2685 1916EXPORT_SYMBOL_GPL(vb2_core_expbuf);
2686/**
2687 * vb2_expbuf() - Export a buffer as a file descriptor
2688 * @q: videobuf2 queue
2689 * @eb: export buffer structure passed from userspace to vidioc_expbuf
2690 * handler in driver
2691 *
2692 * The return values from this function are intended to be directly returned
2693 * from vidioc_expbuf handler in driver.
2694 */
2695int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
2696{
2697 return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index,
2698 eb->plane, eb->flags);
2699}
2700EXPORT_SYMBOL_GPL(vb2_expbuf);
2701 1917
2702/** 1918/**
2703 * vb2_mmap() - map video buffers into application address space 1919 * vb2_mmap() - map video buffers into application address space
@@ -2819,121 +2035,6 @@ unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
2819EXPORT_SYMBOL_GPL(vb2_get_unmapped_area); 2035EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
2820#endif 2036#endif
2821 2037
2822static int __vb2_init_fileio(struct vb2_queue *q, int read);
2823static int __vb2_cleanup_fileio(struct vb2_queue *q);
2824
2825/**
2826 * vb2_poll() - implements poll userspace operation
2827 * @q: videobuf2 queue
2828 * @file: file argument passed to the poll file operation handler
2829 * @wait: wait argument passed to the poll file operation handler
2830 *
2831 * This function implements poll file operation handler for a driver.
2832 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
2833 * be informed that the file descriptor of a video device is available for
2834 * reading.
2835 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
2836 * will be reported as available for writing.
2837 *
2838 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
2839 * pending events.
2840 *
2841 * The return values from this function are intended to be directly returned
2842 * from poll handler in driver.
2843 */
2844unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
2845{
2846 struct video_device *vfd = video_devdata(file);
2847 unsigned long req_events = poll_requested_events(wait);
2848 struct vb2_buffer *vb = NULL;
2849 unsigned int res = 0;
2850 unsigned long flags;
2851
2852 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
2853 struct v4l2_fh *fh = file->private_data;
2854
2855 if (v4l2_event_pending(fh))
2856 res = POLLPRI;
2857 else if (req_events & POLLPRI)
2858 poll_wait(file, &fh->wait, wait);
2859 }
2860
2861 if (!q->is_output && !(req_events & (POLLIN | POLLRDNORM)))
2862 return res;
2863 if (q->is_output && !(req_events & (POLLOUT | POLLWRNORM)))
2864 return res;
2865
2866 /*
2867 * Start file I/O emulator only if streaming API has not been used yet.
2868 */
2869 if (q->num_buffers == 0 && !vb2_fileio_is_active(q)) {
2870 if (!q->is_output && (q->io_modes & VB2_READ) &&
2871 (req_events & (POLLIN | POLLRDNORM))) {
2872 if (__vb2_init_fileio(q, 1))
2873 return res | POLLERR;
2874 }
2875 if (q->is_output && (q->io_modes & VB2_WRITE) &&
2876 (req_events & (POLLOUT | POLLWRNORM))) {
2877 if (__vb2_init_fileio(q, 0))
2878 return res | POLLERR;
2879 /*
2880 * Write to OUTPUT queue can be done immediately.
2881 */
2882 return res | POLLOUT | POLLWRNORM;
2883 }
2884 }
2885
2886 /*
2887 * There is nothing to wait for if the queue isn't streaming, or if the
2888 * error flag is set.
2889 */
2890 if (!vb2_is_streaming(q) || q->error)
2891 return res | POLLERR;
2892 /*
2893 * For compatibility with vb1: if QBUF hasn't been called yet, then
2894 * return POLLERR as well. This only affects capture queues, output
2895 * queues will always initialize waiting_for_buffers to false.
2896 */
2897 if (q->waiting_for_buffers)
2898 return res | POLLERR;
2899
2900 /*
2901 * For output streams you can write as long as there are fewer buffers
2902 * queued than there are buffers available.
2903 */
2904 if (q->is_output && q->queued_count < q->num_buffers)
2905 return res | POLLOUT | POLLWRNORM;
2906
2907 if (list_empty(&q->done_list)) {
2908 /*
2909 * If the last buffer was dequeued from a capture queue,
2910 * return immediately. DQBUF will return -EPIPE.
2911 */
2912 if (q->last_buffer_dequeued)
2913 return res | POLLIN | POLLRDNORM;
2914
2915 poll_wait(file, &q->done_wq, wait);
2916 }
2917
2918 /*
2919 * Take first buffer available for dequeuing.
2920 */
2921 spin_lock_irqsave(&q->done_lock, flags);
2922 if (!list_empty(&q->done_list))
2923 vb = list_first_entry(&q->done_list, struct vb2_buffer,
2924 done_entry);
2925 spin_unlock_irqrestore(&q->done_lock, flags);
2926
2927 if (vb && (vb->state == VB2_BUF_STATE_DONE
2928 || vb->state == VB2_BUF_STATE_ERROR)) {
2929 return (q->is_output) ?
2930 res | POLLOUT | POLLWRNORM :
2931 res | POLLIN | POLLRDNORM;
2932 }
2933 return res;
2934}
2935EXPORT_SYMBOL_GPL(vb2_poll);
2936
2937/** 2038/**
2938 * vb2_core_queue_init() - initialize a videobuf2 queue 2039 * vb2_core_queue_init() - initialize a videobuf2 queue
2939 * @q: videobuf2 queue; this structure should be allocated in driver 2040 * @q: videobuf2 queue; this structure should be allocated in driver
@@ -2945,7 +2046,7 @@ EXPORT_SYMBOL_GPL(vb2_poll);
2945 * to the struct vb2_queue description in include/media/videobuf2-core.h 2046 * to the struct vb2_queue description in include/media/videobuf2-core.h
2946 * for more information. 2047 * for more information.
2947 */ 2048 */
2948static int vb2_core_queue_init(struct vb2_queue *q) 2049int vb2_core_queue_init(struct vb2_queue *q)
2949{ 2050{
2950 /* 2051 /*
2951 * Sanity check 2052 * Sanity check
@@ -2970,55 +2071,7 @@ static int vb2_core_queue_init(struct vb2_queue *q)
2970 2071
2971 return 0; 2072 return 0;
2972} 2073}
2973 2074EXPORT_SYMBOL_GPL(vb2_core_queue_init);
2974static const struct vb2_buf_ops v4l2_buf_ops = {
2975 .fill_user_buffer = __fill_v4l2_buffer,
2976 .fill_vb2_buffer = __fill_vb2_buffer,
2977 .set_timestamp = __set_timestamp,
2978};
2979
2980/**
2981 * vb2_queue_init() - initialize a videobuf2 queue
2982 * @q: videobuf2 queue; this structure should be allocated in driver
2983 *
2984 * The vb2_queue structure should be allocated by the driver. The driver is
2985 * responsible of clearing it's content and setting initial values for some
2986 * required entries before calling this function.
2987 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2988 * to the struct vb2_queue description in include/media/videobuf2-core.h
2989 * for more information.
2990 */
2991int vb2_queue_init(struct vb2_queue *q)
2992{
2993 /*
2994 * Sanity check
2995 */
2996 if (WARN_ON(!q) ||
2997 WARN_ON(q->timestamp_flags &
2998 ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
2999 V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
3000 return -EINVAL;
3001
3002 /* Warn that the driver should choose an appropriate timestamp type */
3003 WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
3004 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
3005
3006 /* Warn that vb2_memory should match with v4l2_memory */
3007 if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP)
3008 || WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR)
3009 || WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF))
3010 return -EINVAL;
3011
3012 if (q->buf_struct_size == 0)
3013 q->buf_struct_size = sizeof(struct vb2_v4l2_buffer);
3014
3015 q->buf_ops = &v4l2_buf_ops;
3016 q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
3017 q->is_output = V4L2_TYPE_IS_OUTPUT(q->type);
3018
3019 return vb2_core_queue_init(q);
3020}
3021EXPORT_SYMBOL_GPL(vb2_queue_init);
3022 2075
3023/** 2076/**
3024 * vb2_core_queue_release() - stop streaming, release the queue and free memory 2077 * vb2_core_queue_release() - stop streaming, release the queue and free memory
@@ -3028,826 +2081,14 @@ EXPORT_SYMBOL_GPL(vb2_queue_init);
3028 * freeing video buffer memory. The driver is responsible for freeing 2081 * freeing video buffer memory. The driver is responsible for freeing
3029 * the vb2_queue structure itself. 2082 * the vb2_queue structure itself.
3030 */ 2083 */
3031static void vb2_core_queue_release(struct vb2_queue *q) 2084void vb2_core_queue_release(struct vb2_queue *q)
3032{ 2085{
3033 __vb2_queue_cancel(q); 2086 __vb2_queue_cancel(q);
3034 mutex_lock(&q->mmap_lock); 2087 mutex_lock(&q->mmap_lock);
3035 __vb2_queue_free(q, q->num_buffers); 2088 __vb2_queue_free(q, q->num_buffers);
3036 mutex_unlock(&q->mmap_lock); 2089 mutex_unlock(&q->mmap_lock);
3037} 2090}
3038 2091EXPORT_SYMBOL_GPL(vb2_core_queue_release);
3039/**
3040 * vb2_queue_release() - stop streaming, release the queue and free memory
3041 * @q: videobuf2 queue
3042 *
3043 * This function stops streaming and performs necessary clean ups, including
3044 * freeing video buffer memory. The driver is responsible for freeing
3045 * the vb2_queue structure itself.
3046 */
3047void vb2_queue_release(struct vb2_queue *q)
3048{
3049 __vb2_cleanup_fileio(q);
3050 vb2_core_queue_release(q);
3051}
3052EXPORT_SYMBOL_GPL(vb2_queue_release);
3053
3054/**
3055 * struct vb2_fileio_buf - buffer context used by file io emulator
3056 *
3057 * vb2 provides a compatibility layer and emulator of file io (read and
3058 * write) calls on top of streaming API. This structure is used for
3059 * tracking context related to the buffers.
3060 */
3061struct vb2_fileio_buf {
3062 void *vaddr;
3063 unsigned int size;
3064 unsigned int pos;
3065 unsigned int queued:1;
3066};
3067
3068/**
3069 * struct vb2_fileio_data - queue context used by file io emulator
3070 *
3071 * @cur_index: the index of the buffer currently being read from or
3072 * written to. If equal to q->num_buffers then a new buffer
3073 * must be dequeued.
3074 * @initial_index: in the read() case all buffers are queued up immediately
3075 * in __vb2_init_fileio() and __vb2_perform_fileio() just cycles
3076 * buffers. However, in the write() case no buffers are initially
3077 * queued, instead whenever a buffer is full it is queued up by
3078 * __vb2_perform_fileio(). Only once all available buffers have
3079 * been queued up will __vb2_perform_fileio() start to dequeue
3080 * buffers. This means that initially __vb2_perform_fileio()
3081 * needs to know what buffer index to use when it is queuing up
3082 * the buffers for the first time. That initial index is stored
3083 * in this field. Once it is equal to q->num_buffers all
3084 * available buffers have been queued and __vb2_perform_fileio()
3085 * should start the normal dequeue/queue cycle.
3086 *
3087 * vb2 provides a compatibility layer and emulator of file io (read and
3088 * write) calls on top of streaming API. For proper operation it required
3089 * this structure to save the driver state between each call of the read
3090 * or write function.
3091 */
3092struct vb2_fileio_data {
3093 struct v4l2_requestbuffers req;
3094 struct v4l2_plane p;
3095 struct v4l2_buffer b;
3096 struct vb2_fileio_buf bufs[VB2_MAX_FRAME];
3097 unsigned int cur_index;
3098 unsigned int initial_index;
3099 unsigned int q_count;
3100 unsigned int dq_count;
3101 unsigned read_once:1;
3102 unsigned write_immediately:1;
3103};
3104
3105/**
3106 * __vb2_init_fileio() - initialize file io emulator
3107 * @q: videobuf2 queue
3108 * @read: mode selector (1 means read, 0 means write)
3109 */
3110static int __vb2_init_fileio(struct vb2_queue *q, int read)
3111{
3112 struct vb2_fileio_data *fileio;
3113 int i, ret;
3114 unsigned int count = 0;
3115
3116 /*
3117 * Sanity check
3118 */
3119 if (WARN_ON((read && !(q->io_modes & VB2_READ)) ||
3120 (!read && !(q->io_modes & VB2_WRITE))))
3121 return -EINVAL;
3122
3123 /*
3124 * Check if device supports mapping buffers to kernel virtual space.
3125 */
3126 if (!q->mem_ops->vaddr)
3127 return -EBUSY;
3128
3129 /*
3130 * Check if streaming api has not been already activated.
3131 */
3132 if (q->streaming || q->num_buffers > 0)
3133 return -EBUSY;
3134
3135 /*
3136 * Start with count 1, driver can increase it in queue_setup()
3137 */
3138 count = 1;
3139
3140 dprintk(3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n",
3141 (read) ? "read" : "write", count, q->fileio_read_once,
3142 q->fileio_write_immediately);
3143
3144 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
3145 if (fileio == NULL)
3146 return -ENOMEM;
3147
3148 fileio->read_once = q->fileio_read_once;
3149 fileio->write_immediately = q->fileio_write_immediately;
3150
3151 /*
3152 * Request buffers and use MMAP type to force driver
3153 * to allocate buffers by itself.
3154 */
3155 fileio->req.count = count;
3156 fileio->req.memory = VB2_MEMORY_MMAP;
3157 fileio->req.type = q->type;
3158 q->fileio = fileio;
3159 ret = vb2_core_reqbufs(q, fileio->req.memory, &fileio->req.count);
3160 if (ret)
3161 goto err_kfree;
3162
3163 /*
3164 * Check if plane_count is correct
3165 * (multiplane buffers are not supported).
3166 */
3167 if (q->bufs[0]->num_planes != 1) {
3168 ret = -EBUSY;
3169 goto err_reqbufs;
3170 }
3171
3172 /*
3173 * Get kernel address of each buffer.
3174 */
3175 for (i = 0; i < q->num_buffers; i++) {
3176 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
3177 if (fileio->bufs[i].vaddr == NULL) {
3178 ret = -EINVAL;
3179 goto err_reqbufs;
3180 }
3181 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
3182 }
3183
3184 /*
3185 * Read mode requires pre queuing of all buffers.
3186 */
3187 if (read) {
3188 bool is_multiplanar = q->is_multiplanar;
3189
3190 /*
3191 * Queue all buffers.
3192 */
3193 for (i = 0; i < q->num_buffers; i++) {
3194 struct v4l2_buffer *b = &fileio->b;
3195
3196 memset(b, 0, sizeof(*b));
3197 b->type = q->type;
3198 if (is_multiplanar) {
3199 memset(&fileio->p, 0, sizeof(fileio->p));
3200 b->m.planes = &fileio->p;
3201 b->length = 1;
3202 }
3203 b->memory = q->memory;
3204 b->index = i;
3205 ret = vb2_internal_qbuf(q, b);
3206 if (ret)
3207 goto err_reqbufs;
3208 fileio->bufs[i].queued = 1;
3209 }
3210 /*
3211 * All buffers have been queued, so mark that by setting
3212 * initial_index to q->num_buffers
3213 */
3214 fileio->initial_index = q->num_buffers;
3215 fileio->cur_index = q->num_buffers;
3216 }
3217
3218 /*
3219 * Start streaming.
3220 */
3221 ret = vb2_core_streamon(q, q->type);
3222 if (ret)
3223 goto err_reqbufs;
3224
3225 return ret;
3226
3227err_reqbufs:
3228 fileio->req.count = 0;
3229 vb2_core_reqbufs(q, fileio->req.memory, &fileio->req.count);
3230
3231err_kfree:
3232 q->fileio = NULL;
3233 kfree(fileio);
3234 return ret;
3235}
3236
3237/**
3238 * __vb2_cleanup_fileio() - free resourced used by file io emulator
3239 * @q: videobuf2 queue
3240 */
3241static int __vb2_cleanup_fileio(struct vb2_queue *q)
3242{
3243 struct vb2_fileio_data *fileio = q->fileio;
3244
3245 if (fileio) {
3246 vb2_core_streamoff(q, q->type);
3247 q->fileio = NULL;
3248 fileio->req.count = 0;
3249 vb2_reqbufs(q, &fileio->req);
3250 kfree(fileio);
3251 dprintk(3, "file io emulator closed\n");
3252 }
3253 return 0;
3254}
3255
3256/**
3257 * __vb2_perform_fileio() - perform a single file io (read or write) operation
3258 * @q: videobuf2 queue
3259 * @data: pointed to target userspace buffer
3260 * @count: number of bytes to read or write
3261 * @ppos: file handle position tracking pointer
3262 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
3263 * @read: access mode selector (1 means read, 0 means write)
3264 */
3265static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
3266 loff_t *ppos, int nonblock, int read)
3267{
3268 struct vb2_fileio_data *fileio;
3269 struct vb2_fileio_buf *buf;
3270 bool is_multiplanar = q->is_multiplanar;
3271 /*
3272 * When using write() to write data to an output video node the vb2 core
3273 * should set timestamps if V4L2_BUF_FLAG_TIMESTAMP_COPY is set. Nobody
3274 * else is able to provide this information with the write() operation.
3275 */
3276 bool set_timestamp = !read &&
3277 (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
3278 V4L2_BUF_FLAG_TIMESTAMP_COPY;
3279 int ret, index;
3280
3281 dprintk(3, "mode %s, offset %ld, count %zd, %sblocking\n",
3282 read ? "read" : "write", (long)*ppos, count,
3283 nonblock ? "non" : "");
3284
3285 if (!data)
3286 return -EINVAL;
3287
3288 /*
3289 * Initialize emulator on first call.
3290 */
3291 if (!vb2_fileio_is_active(q)) {
3292 ret = __vb2_init_fileio(q, read);
3293 dprintk(3, "vb2_init_fileio result: %d\n", ret);
3294 if (ret)
3295 return ret;
3296 }
3297 fileio = q->fileio;
3298
3299 /*
3300 * Check if we need to dequeue the buffer.
3301 */
3302 index = fileio->cur_index;
3303 if (index >= q->num_buffers) {
3304 /*
3305 * Call vb2_dqbuf to get buffer back.
3306 */
3307 memset(&fileio->b, 0, sizeof(fileio->b));
3308 fileio->b.type = q->type;
3309 fileio->b.memory = q->memory;
3310 if (is_multiplanar) {
3311 memset(&fileio->p, 0, sizeof(fileio->p));
3312 fileio->b.m.planes = &fileio->p;
3313 fileio->b.length = 1;
3314 }
3315 ret = vb2_internal_dqbuf(q, &fileio->b, nonblock);
3316 dprintk(5, "vb2_dqbuf result: %d\n", ret);
3317 if (ret)
3318 return ret;
3319 fileio->dq_count += 1;
3320
3321 fileio->cur_index = index = fileio->b.index;
3322 buf = &fileio->bufs[index];
3323
3324 /*
3325 * Get number of bytes filled by the driver
3326 */
3327 buf->pos = 0;
3328 buf->queued = 0;
3329 buf->size = read ? vb2_get_plane_payload(q->bufs[index], 0)
3330 : vb2_plane_size(q->bufs[index], 0);
3331 /* Compensate for data_offset on read in the multiplanar case. */
3332 if (is_multiplanar && read &&
3333 fileio->b.m.planes[0].data_offset < buf->size) {
3334 buf->pos = fileio->b.m.planes[0].data_offset;
3335 buf->size -= buf->pos;
3336 }
3337 } else {
3338 buf = &fileio->bufs[index];
3339 }
3340
3341 /*
3342 * Limit count on last few bytes of the buffer.
3343 */
3344 if (buf->pos + count > buf->size) {
3345 count = buf->size - buf->pos;
3346 dprintk(5, "reducing read count: %zd\n", count);
3347 }
3348
3349 /*
3350 * Transfer data to userspace.
3351 */
3352 dprintk(3, "copying %zd bytes - buffer %d, offset %u\n",
3353 count, index, buf->pos);
3354 if (read)
3355 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
3356 else
3357 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
3358 if (ret) {
3359 dprintk(3, "error copying data\n");
3360 return -EFAULT;
3361 }
3362
3363 /*
3364 * Update counters.
3365 */
3366 buf->pos += count;
3367 *ppos += count;
3368
3369 /*
3370 * Queue next buffer if required.
3371 */
3372 if (buf->pos == buf->size || (!read && fileio->write_immediately)) {
3373 /*
3374 * Check if this is the last buffer to read.
3375 */
3376 if (read && fileio->read_once && fileio->dq_count == 1) {
3377 dprintk(3, "read limit reached\n");
3378 return __vb2_cleanup_fileio(q);
3379 }
3380
3381 /*
3382 * Call vb2_qbuf and give buffer to the driver.
3383 */
3384 memset(&fileio->b, 0, sizeof(fileio->b));
3385 fileio->b.type = q->type;
3386 fileio->b.memory = q->memory;
3387 fileio->b.index = index;
3388 fileio->b.bytesused = buf->pos;
3389 if (is_multiplanar) {
3390 memset(&fileio->p, 0, sizeof(fileio->p));
3391 fileio->p.bytesused = buf->pos;
3392 fileio->b.m.planes = &fileio->p;
3393 fileio->b.length = 1;
3394 }
3395 if (set_timestamp)
3396 v4l2_get_timestamp(&fileio->b.timestamp);
3397 ret = vb2_internal_qbuf(q, &fileio->b);
3398 dprintk(5, "vb2_dbuf result: %d\n", ret);
3399 if (ret)
3400 return ret;
3401
3402 /*
3403 * Buffer has been queued, update the status
3404 */
3405 buf->pos = 0;
3406 buf->queued = 1;
3407 buf->size = vb2_plane_size(q->bufs[index], 0);
3408 fileio->q_count += 1;
3409 /*
3410 * If we are queuing up buffers for the first time, then
3411 * increase initial_index by one.
3412 */
3413 if (fileio->initial_index < q->num_buffers)
3414 fileio->initial_index++;
3415 /*
3416 * The next buffer to use is either a buffer that's going to be
3417 * queued for the first time (initial_index < q->num_buffers)
3418 * or it is equal to q->num_buffers, meaning that the next
3419 * time we need to dequeue a buffer since we've now queued up
3420 * all the 'first time' buffers.
3421 */
3422 fileio->cur_index = fileio->initial_index;
3423 }
3424
3425 /*
3426 * Return proper number of bytes processed.
3427 */
3428 if (ret == 0)
3429 ret = count;
3430 return ret;
3431}
3432
3433size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
3434 loff_t *ppos, int nonblocking)
3435{
3436 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
3437}
3438EXPORT_SYMBOL_GPL(vb2_read);
3439
3440size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
3441 loff_t *ppos, int nonblocking)
3442{
3443 return __vb2_perform_fileio(q, (char __user *) data, count,
3444 ppos, nonblocking, 0);
3445}
3446EXPORT_SYMBOL_GPL(vb2_write);
3447
3448struct vb2_threadio_data {
3449 struct task_struct *thread;
3450 vb2_thread_fnc fnc;
3451 void *priv;
3452 bool stop;
3453};
3454
3455static int vb2_thread(void *data)
3456{
3457 struct vb2_queue *q = data;
3458 struct vb2_threadio_data *threadio = q->threadio;
3459 struct vb2_fileio_data *fileio = q->fileio;
3460 bool set_timestamp = false;
3461 int prequeue = 0;
3462 int index = 0;
3463 int ret = 0;
3464
3465 if (q->is_output) {
3466 prequeue = q->num_buffers;
3467 set_timestamp =
3468 (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
3469 V4L2_BUF_FLAG_TIMESTAMP_COPY;
3470 }
3471
3472 set_freezable();
3473
3474 for (;;) {
3475 struct vb2_buffer *vb;
3476
3477 /*
3478 * Call vb2_dqbuf to get buffer back.
3479 */
3480 memset(&fileio->b, 0, sizeof(fileio->b));
3481 fileio->b.type = q->type;
3482 fileio->b.memory = q->memory;
3483 if (prequeue) {
3484 fileio->b.index = index++;
3485 prequeue--;
3486 } else {
3487 call_void_qop(q, wait_finish, q);
3488 if (!threadio->stop)
3489 ret = vb2_internal_dqbuf(q, &fileio->b, 0);
3490 call_void_qop(q, wait_prepare, q);
3491 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
3492 }
3493 if (ret || threadio->stop)
3494 break;
3495 try_to_freeze();
3496
3497 vb = q->bufs[fileio->b.index];
3498 if (!(fileio->b.flags & V4L2_BUF_FLAG_ERROR))
3499 if (threadio->fnc(vb, threadio->priv))
3500 break;
3501 call_void_qop(q, wait_finish, q);
3502 if (set_timestamp)
3503 v4l2_get_timestamp(&fileio->b.timestamp);
3504 if (!threadio->stop)
3505 ret = vb2_internal_qbuf(q, &fileio->b);
3506 call_void_qop(q, wait_prepare, q);
3507 if (ret || threadio->stop)
3508 break;
3509 }
3510
3511 /* Hmm, linux becomes *very* unhappy without this ... */
3512 while (!kthread_should_stop()) {
3513 set_current_state(TASK_INTERRUPTIBLE);
3514 schedule();
3515 }
3516 return 0;
3517}
3518
3519/*
3520 * This function should not be used for anything else but the videobuf2-dvb
3521 * support. If you think you have another good use-case for this, then please
3522 * contact the linux-media mailinglist first.
3523 */
3524int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv,
3525 const char *thread_name)
3526{
3527 struct vb2_threadio_data *threadio;
3528 int ret = 0;
3529
3530 if (q->threadio)
3531 return -EBUSY;
3532 if (vb2_is_busy(q))
3533 return -EBUSY;
3534 if (WARN_ON(q->fileio))
3535 return -EBUSY;
3536
3537 threadio = kzalloc(sizeof(*threadio), GFP_KERNEL);
3538 if (threadio == NULL)
3539 return -ENOMEM;
3540 threadio->fnc = fnc;
3541 threadio->priv = priv;
3542
3543 ret = __vb2_init_fileio(q, !q->is_output);
3544 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
3545 if (ret)
3546 goto nomem;
3547 q->threadio = threadio;
3548 threadio->thread = kthread_run(vb2_thread, q, "vb2-%s", thread_name);
3549 if (IS_ERR(threadio->thread)) {
3550 ret = PTR_ERR(threadio->thread);
3551 threadio->thread = NULL;
3552 goto nothread;
3553 }
3554 return 0;
3555
3556nothread:
3557 __vb2_cleanup_fileio(q);
3558nomem:
3559 kfree(threadio);
3560 return ret;
3561}
3562EXPORT_SYMBOL_GPL(vb2_thread_start);
3563
3564int vb2_thread_stop(struct vb2_queue *q)
3565{
3566 struct vb2_threadio_data *threadio = q->threadio;
3567 int err;
3568
3569 if (threadio == NULL)
3570 return 0;
3571 threadio->stop = true;
3572 /* Wake up all pending sleeps in the thread */
3573 vb2_queue_error(q);
3574 err = kthread_stop(threadio->thread);
3575 __vb2_cleanup_fileio(q);
3576 threadio->thread = NULL;
3577 kfree(threadio);
3578 q->threadio = NULL;
3579 return err;
3580}
3581EXPORT_SYMBOL_GPL(vb2_thread_stop);
3582
3583/*
3584 * The following functions are not part of the vb2 core API, but are helper
3585 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
3586 * and struct vb2_ops.
3587 * They contain boilerplate code that most if not all drivers have to do
3588 * and so they simplify the driver code.
3589 */
3590
3591/* The queue is busy if there is a owner and you are not that owner. */
3592static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
3593{
3594 return vdev->queue->owner && vdev->queue->owner != file->private_data;
3595}
3596
3597/* vb2 ioctl helpers */
3598
3599int vb2_ioctl_reqbufs(struct file *file, void *priv,
3600 struct v4l2_requestbuffers *p)
3601{
3602 struct video_device *vdev = video_devdata(file);
3603 int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type);
3604
3605 if (res)
3606 return res;
3607 if (vb2_queue_is_busy(vdev, file))
3608 return -EBUSY;
3609 res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count);
3610 /* If count == 0, then the owner has released all buffers and he
3611 is no longer owner of the queue. Otherwise we have a new owner. */
3612 if (res == 0)
3613 vdev->queue->owner = p->count ? file->private_data : NULL;
3614 return res;
3615}
3616EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
3617
3618int vb2_ioctl_create_bufs(struct file *file, void *priv,
3619 struct v4l2_create_buffers *p)
3620{
3621 struct video_device *vdev = video_devdata(file);
3622 int res = vb2_verify_memory_type(vdev->queue, p->memory,
3623 p->format.type);
3624
3625 p->index = vdev->queue->num_buffers;
3626 /*
3627 * If count == 0, then just check if memory and type are valid.
3628 * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
3629 */
3630 if (p->count == 0)
3631 return res != -EBUSY ? res : 0;
3632 if (res)
3633 return res;
3634 if (vb2_queue_is_busy(vdev, file))
3635 return -EBUSY;
3636 res = vb2_core_create_bufs(vdev->queue, p->memory,
3637 &p->count, &p->format);
3638 if (res == 0)
3639 vdev->queue->owner = file->private_data;
3640 return res;
3641}
3642EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
3643
3644int vb2_ioctl_prepare_buf(struct file *file, void *priv,
3645 struct v4l2_buffer *p)
3646{
3647 struct video_device *vdev = video_devdata(file);
3648
3649 if (vb2_queue_is_busy(vdev, file))
3650 return -EBUSY;
3651 return vb2_prepare_buf(vdev->queue, p);
3652}
3653EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
3654
3655int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
3656{
3657 struct video_device *vdev = video_devdata(file);
3658
3659 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
3660 return vb2_querybuf(vdev->queue, p);
3661}
3662EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
3663
3664int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
3665{
3666 struct video_device *vdev = video_devdata(file);
3667
3668 if (vb2_queue_is_busy(vdev, file))
3669 return -EBUSY;
3670 return vb2_qbuf(vdev->queue, p);
3671}
3672EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
3673
3674int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
3675{
3676 struct video_device *vdev = video_devdata(file);
3677
3678 if (vb2_queue_is_busy(vdev, file))
3679 return -EBUSY;
3680 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
3681}
3682EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
3683
3684int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
3685{
3686 struct video_device *vdev = video_devdata(file);
3687
3688 if (vb2_queue_is_busy(vdev, file))
3689 return -EBUSY;
3690 return vb2_streamon(vdev->queue, i);
3691}
3692EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
3693
3694int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
3695{
3696 struct video_device *vdev = video_devdata(file);
3697
3698 if (vb2_queue_is_busy(vdev, file))
3699 return -EBUSY;
3700 return vb2_streamoff(vdev->queue, i);
3701}
3702EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
3703
3704int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
3705{
3706 struct video_device *vdev = video_devdata(file);
3707
3708 if (vb2_queue_is_busy(vdev, file))
3709 return -EBUSY;
3710 return vb2_expbuf(vdev->queue, p);
3711}
3712EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
3713
3714/* v4l2_file_operations helpers */
3715
3716int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
3717{
3718 struct video_device *vdev = video_devdata(file);
3719
3720 return vb2_mmap(vdev->queue, vma);
3721}
3722EXPORT_SYMBOL_GPL(vb2_fop_mmap);
3723
3724int _vb2_fop_release(struct file *file, struct mutex *lock)
3725{
3726 struct video_device *vdev = video_devdata(file);
3727
3728 if (lock)
3729 mutex_lock(lock);
3730 if (file->private_data == vdev->queue->owner) {
3731 vb2_queue_release(vdev->queue);
3732 vdev->queue->owner = NULL;
3733 }
3734 if (lock)
3735 mutex_unlock(lock);
3736 return v4l2_fh_release(file);
3737}
3738EXPORT_SYMBOL_GPL(_vb2_fop_release);
3739
3740int vb2_fop_release(struct file *file)
3741{
3742 struct video_device *vdev = video_devdata(file);
3743 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
3744
3745 return _vb2_fop_release(file, lock);
3746}
3747EXPORT_SYMBOL_GPL(vb2_fop_release);
3748
3749ssize_t vb2_fop_write(struct file *file, const char __user *buf,
3750 size_t count, loff_t *ppos)
3751{
3752 struct video_device *vdev = video_devdata(file);
3753 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
3754 int err = -EBUSY;
3755
3756 if (!(vdev->queue->io_modes & VB2_WRITE))
3757 return -EINVAL;
3758 if (lock && mutex_lock_interruptible(lock))
3759 return -ERESTARTSYS;
3760 if (vb2_queue_is_busy(vdev, file))
3761 goto exit;
3762 err = vb2_write(vdev->queue, buf, count, ppos,
3763 file->f_flags & O_NONBLOCK);
3764 if (vdev->queue->fileio)
3765 vdev->queue->owner = file->private_data;
3766exit:
3767 if (lock)
3768 mutex_unlock(lock);
3769 return err;
3770}
3771EXPORT_SYMBOL_GPL(vb2_fop_write);
3772
3773ssize_t vb2_fop_read(struct file *file, char __user *buf,
3774 size_t count, loff_t *ppos)
3775{
3776 struct video_device *vdev = video_devdata(file);
3777 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
3778 int err = -EBUSY;
3779
3780 if (!(vdev->queue->io_modes & VB2_READ))
3781 return -EINVAL;
3782 if (lock && mutex_lock_interruptible(lock))
3783 return -ERESTARTSYS;
3784 if (vb2_queue_is_busy(vdev, file))
3785 goto exit;
3786 err = vb2_read(vdev->queue, buf, count, ppos,
3787 file->f_flags & O_NONBLOCK);
3788 if (vdev->queue->fileio)
3789 vdev->queue->owner = file->private_data;
3790exit:
3791 if (lock)
3792 mutex_unlock(lock);
3793 return err;
3794}
3795EXPORT_SYMBOL_GPL(vb2_fop_read);
3796
3797unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
3798{
3799 struct video_device *vdev = video_devdata(file);
3800 struct vb2_queue *q = vdev->queue;
3801 struct mutex *lock = q->lock ? q->lock : vdev->lock;
3802 unsigned res;
3803 void *fileio;
3804
3805 /*
3806 * If this helper doesn't know how to lock, then you shouldn't be using
3807 * it but you should write your own.
3808 */
3809 WARN_ON(!lock);
3810
3811 if (lock && mutex_lock_interruptible(lock))
3812 return POLLERR;
3813
3814 fileio = q->fileio;
3815
3816 res = vb2_poll(vdev->queue, file, wait);
3817
3818 /* If fileio was started, then we have a new queue owner. */
3819 if (!fileio && q->fileio)
3820 q->owner = file->private_data;
3821 if (lock)
3822 mutex_unlock(lock);
3823 return res;
3824}
3825EXPORT_SYMBOL_GPL(vb2_fop_poll);
3826
3827#ifndef CONFIG_MMU
3828unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
3829 unsigned long len, unsigned long pgoff, unsigned long flags)
3830{
3831 struct video_device *vdev = video_devdata(file);
3832
3833 return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
3834}
3835EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
3836#endif
3837
3838/* vb2_ops helpers. Only use if vq->lock is non-NULL. */
3839
3840void vb2_ops_wait_prepare(struct vb2_queue *vq)
3841{
3842 mutex_unlock(vq->lock);
3843}
3844EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
3845
3846void vb2_ops_wait_finish(struct vb2_queue *vq)
3847{
3848 mutex_lock(vq->lock);
3849}
3850EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
3851 2092
3852MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2"); 2093MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
3853MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski"); 2094MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");