1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * generic helper functions for handling video4linux capture buffers
5 * (c) 2007 Mauro Carvalho Chehab, <mchehab@kernel.org>
7 * Highly based on video-buf written originally by:
8 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
9 * (c) 2006 Mauro Carvalho Chehab, <mchehab@kernel.org>
10 * (c) 2006 Ted Walther and John Sokol
13 #ifndef _VIDEOBUF_CORE_H
14 #define _VIDEOBUF_CORE_H
16 #include <linux/poll.h>
17 #include <linux/videodev2.h>
22 struct videobuf_buffer
;
23 struct videobuf_queue
;
25 /* --------------------------------------------------------------------- */
28 * A small set of helper functions to manage video4linux buffers.
30 * struct videobuf_buffer holds the data structures used by the helper
31 * functions, additionally some commonly used fields for v4l buffers
32 * (width, height, lists, waitqueue) are in there. That struct should
33 * be used as first element in the drivers buffer struct.
35 * about the mmap helpers (videobuf_mmap_*):
37 * The mmaper function allows to map any subset of contiguous buffers.
38 * This includes one mmap() call for all buffers (which the original
39 * video4linux API uses) as well as one mmap() for every single buffer
42 * If there is a valid mapping for a buffer, buffer->baddr/bsize holds
43 * userspace address + size which can be fed into the
44 * videobuf_dma_init_user function listed above.
48 struct videobuf_mapping
{
50 struct videobuf_queue
*q
;
54 VIDEOBUF_NEEDS_INIT
= 0,
55 VIDEOBUF_PREPARED
= 1,
63 struct videobuf_buffer
{
67 /* info about the buffer */
70 unsigned int bytesperline
; /* use only if != 0 */
72 enum v4l2_field field
;
73 enum videobuf_state state
;
74 struct list_head stream
; /* QBUF/DQBUF list */
76 /* touched by irq handler */
77 struct list_head queue
;
78 wait_queue_head_t done
;
79 unsigned int field_count
;
83 enum v4l2_memory memory
;
88 /* buffer offset (mmap + overlay) */
91 /* buffer addr (userland ptr!) */
94 /* for mmap'ed buffers */
95 struct videobuf_mapping
*map
;
97 /* Private pointer to allow specific methods to store their data */
102 struct videobuf_queue_ops
{
103 int (*buf_setup
)(struct videobuf_queue
*q
,
104 unsigned int *count
, unsigned int *size
);
105 int (*buf_prepare
)(struct videobuf_queue
*q
,
106 struct videobuf_buffer
*vb
,
107 enum v4l2_field field
);
108 void (*buf_queue
)(struct videobuf_queue
*q
,
109 struct videobuf_buffer
*vb
);
110 void (*buf_release
)(struct videobuf_queue
*q
,
111 struct videobuf_buffer
*vb
);
114 #define MAGIC_QTYPE_OPS 0x12261003
116 /* Helper operations - device type dependent */
117 struct videobuf_qtype_ops
{
120 struct videobuf_buffer
*(*alloc_vb
)(size_t size
);
121 void *(*vaddr
) (struct videobuf_buffer
*buf
);
122 int (*iolock
) (struct videobuf_queue
*q
,
123 struct videobuf_buffer
*vb
,
124 struct v4l2_framebuffer
*fbuf
);
125 int (*sync
) (struct videobuf_queue
*q
,
126 struct videobuf_buffer
*buf
);
127 int (*mmap_mapper
) (struct videobuf_queue
*q
,
128 struct videobuf_buffer
*buf
,
129 struct vm_area_struct
*vma
);
132 struct videobuf_queue
{
133 struct mutex vb_lock
;
134 struct mutex
*ext_lock
;
138 wait_queue_head_t wait
; /* wait if queue is empty */
140 enum v4l2_buf_type type
;
142 enum v4l2_field field
;
143 enum v4l2_field last
; /* for field=V4L2_FIELD_ALTERNATE */
144 struct videobuf_buffer
*bufs
[VIDEO_MAX_FRAME
];
145 const struct videobuf_queue_ops
*ops
;
146 struct videobuf_qtype_ops
*int_ops
;
148 unsigned int streaming
:1;
149 unsigned int reading
:1;
151 /* capture via mmap() + ioctl(QBUF/DQBUF) */
152 struct list_head stream
;
154 /* capture via read() */
155 unsigned int read_off
;
156 struct videobuf_buffer
*read_buf
;
158 /* driver private data */
162 static inline void videobuf_queue_lock(struct videobuf_queue
*q
)
165 mutex_lock(&q
->vb_lock
);
168 static inline void videobuf_queue_unlock(struct videobuf_queue
*q
)
171 mutex_unlock(&q
->vb_lock
);
174 int videobuf_waiton(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
175 int non_blocking
, int intr
);
176 int videobuf_iolock(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
177 struct v4l2_framebuffer
*fbuf
);
179 struct videobuf_buffer
*videobuf_alloc_vb(struct videobuf_queue
*q
);
181 /* Used on videobuf-dvb */
182 void *videobuf_queue_to_vaddr(struct videobuf_queue
*q
,
183 struct videobuf_buffer
*buf
);
185 void videobuf_queue_core_init(struct videobuf_queue
*q
,
186 const struct videobuf_queue_ops
*ops
,
189 enum v4l2_buf_type type
,
190 enum v4l2_field field
,
193 struct videobuf_qtype_ops
*int_ops
,
194 struct mutex
*ext_lock
);
195 int videobuf_queue_is_busy(struct videobuf_queue
*q
);
196 void videobuf_queue_cancel(struct videobuf_queue
*q
);
198 enum v4l2_field
videobuf_next_field(struct videobuf_queue
*q
);
199 int videobuf_reqbufs(struct videobuf_queue
*q
,
200 struct v4l2_requestbuffers
*req
);
201 int videobuf_querybuf(struct videobuf_queue
*q
, struct v4l2_buffer
*b
);
202 int videobuf_qbuf(struct videobuf_queue
*q
,
203 struct v4l2_buffer
*b
);
204 int videobuf_dqbuf(struct videobuf_queue
*q
,
205 struct v4l2_buffer
*b
, int nonblocking
);
206 int videobuf_streamon(struct videobuf_queue
*q
);
207 int videobuf_streamoff(struct videobuf_queue
*q
);
209 void videobuf_stop(struct videobuf_queue
*q
);
211 int videobuf_read_start(struct videobuf_queue
*q
);
212 void videobuf_read_stop(struct videobuf_queue
*q
);
213 ssize_t
videobuf_read_stream(struct videobuf_queue
*q
,
214 char __user
*data
, size_t count
, loff_t
*ppos
,
215 int vbihack
, int nonblocking
);
216 ssize_t
videobuf_read_one(struct videobuf_queue
*q
,
217 char __user
*data
, size_t count
, loff_t
*ppos
,
219 __poll_t
videobuf_poll_stream(struct file
*file
,
220 struct videobuf_queue
*q
,
223 int videobuf_mmap_setup(struct videobuf_queue
*q
,
224 unsigned int bcount
, unsigned int bsize
,
225 enum v4l2_memory memory
);
226 int __videobuf_mmap_setup(struct videobuf_queue
*q
,
227 unsigned int bcount
, unsigned int bsize
,
228 enum v4l2_memory memory
);
229 int videobuf_mmap_free(struct videobuf_queue
*q
);
230 int videobuf_mmap_mapper(struct videobuf_queue
*q
,
231 struct vm_area_struct
*vma
);