Commit | Line | Data |
---|---|---|
c139990e JS |
1 | /* |
2 | * videobuf2-v4l2.h - V4L2 driver helper framework | |
3 | * | |
4 | * Copyright (C) 2010 Samsung Electronics | |
5 | * | |
6 | * Author: Pawel Osciak <pawel@osciak.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation. | |
11 | */ | |
12 | #ifndef _MEDIA_VIDEOBUF2_V4L2_H | |
13 | #define _MEDIA_VIDEOBUF2_V4L2_H | |
14 | ||
2d700715 | 15 | #include <linux/videodev2.h> |
c139990e JS |
16 | #include <media/videobuf2-core.h> |
17 | ||
bed04f93 JS |
18 | #if VB2_MAX_FRAME != VIDEO_MAX_FRAME |
19 | #error VB2_MAX_FRAME != VIDEO_MAX_FRAME | |
20 | #endif | |
21 | ||
22 | #if VB2_MAX_PLANES != VIDEO_MAX_PLANES | |
23 | #error VB2_MAX_PLANES != VIDEO_MAX_PLANES | |
24 | #endif | |
25 | ||
f729ef57 HV |
26 | struct video_device; |
27 | ||
2d700715 | 28 | /** |
9fbe71b4 | 29 | * struct vb2_v4l2_buffer - video buffer information for v4l2. |
bf4404b4 | 30 | * |
9fbe71b4 MCC |
31 | * @vb2_buf: embedded struct &vb2_buffer. |
32 | * @flags: buffer informational flags. | |
33 | * @field: field order of the image in the buffer, as defined by | |
34 | * &enum v4l2_field. | |
35 | * @timecode: frame timecode. | |
36 | * @sequence: sequence count of this frame. | |
394dc588 | 37 | * @request_fd: the request_fd associated with this buffer |
137272cd | 38 | * @is_held: if true, then this capture buffer was held |
db6e8d57 | 39 | * @planes: plane information (userptr/fd, length, bytesused, data_offset). |
bf4404b4 | 40 | * |
2d700715 | 41 | * Should contain enough information to be able to cover all the fields |
9fbe71b4 | 42 | * of &struct v4l2_buffer at ``videodev2.h``. |
2d700715 JS |
43 | */ |
44 | struct vb2_v4l2_buffer { | |
45 | struct vb2_buffer vb2_buf; | |
46 | ||
47 | __u32 flags; | |
48 | __u32 field; | |
2d700715 JS |
49 | struct v4l2_timecode timecode; |
50 | __u32 sequence; | |
394dc588 | 51 | __s32 request_fd; |
137272cd | 52 | bool is_held; |
db6e8d57 | 53 | struct vb2_plane planes[VB2_MAX_PLANES]; |
2d700715 JS |
54 | }; |
55 | ||
137272cd HV |
56 | /* VB2 V4L2 flags as set in vb2_queue.subsystem_flags */ |
57 | #define VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF (1 << 0) | |
58 | ||
d383b579 | 59 | /* |
2d700715 JS |
60 | * to_vb2_v4l2_buffer() - cast struct vb2_buffer * to struct vb2_v4l2_buffer * |
61 | */ | |
62 | #define to_vb2_v4l2_buffer(vb) \ | |
d383b579 | 63 | container_of(vb, struct vb2_v4l2_buffer, vb2_buf) |
2d700715 | 64 | |
9d042e45 EG |
65 | /** |
66 | * vb2_find_buffer() - Find a buffer with given timestamp | |
67 | * | |
68 | * @q: pointer to &struct vb2_queue with videobuf2 queue. | |
69 | * @timestamp: the timestamp to find. | |
70 | * | |
71 | * Returns the buffer with the given @timestamp, or NULL if not found. | |
72 | */ | |
73 | struct vb2_buffer *vb2_find_buffer(struct vb2_queue *q, u64 timestamp); | |
74 | ||
3c5be988 | 75 | int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b); |
24ade5b6 MCC |
76 | |
77 | /** | |
78 | * vb2_reqbufs() - Wrapper for vb2_core_reqbufs() that also verifies | |
79 | * the memory and type values. | |
bf4404b4 | 80 | * |
9fbe71b4 MCC |
81 | * @q: pointer to &struct vb2_queue with videobuf2 queue. |
82 | * @req: &struct v4l2_requestbuffers passed from userspace to | |
83 | * &v4l2_ioctl_ops->vidioc_reqbufs handler in driver. | |
24ade5b6 | 84 | */ |
3c5be988 JS |
85 | int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req); |
86 | ||
24ade5b6 MCC |
87 | /** |
88 | * vb2_create_bufs() - Wrapper for vb2_core_create_bufs() that also verifies | |
89 | * the memory and type values. | |
bf4404b4 | 90 | * |
9fbe71b4 MCC |
91 | * @q: pointer to &struct vb2_queue with videobuf2 queue. |
92 | * @create: creation parameters, passed from userspace to | |
93 | * &v4l2_ioctl_ops->vidioc_create_bufs handler in driver | |
24ade5b6 | 94 | */ |
3c5be988 | 95 | int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create); |
24ade5b6 MCC |
96 | |
97 | /** | |
98 | * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel | |
bf4404b4 | 99 | * |
9fbe71b4 | 100 | * @q: pointer to &struct vb2_queue with videobuf2 queue. |
394dc588 | 101 | * @mdev: pointer to &struct media_device, may be NULL. |
9fbe71b4 MCC |
102 | * @b: buffer structure passed from userspace to |
103 | * &v4l2_ioctl_ops->vidioc_prepare_buf handler in driver | |
104 | * | |
105 | * Should be called from &v4l2_ioctl_ops->vidioc_prepare_buf ioctl handler | |
106 | * of a driver. | |
24ade5b6 | 107 | * |
24ade5b6 | 108 | * This function: |
bf4404b4 MCC |
109 | * |
110 | * #) verifies the passed buffer, | |
9fbe71b4 MCC |
111 | * #) calls &vb2_ops->buf_prepare callback in the driver (if provided), |
112 | * in which driver-specific buffer initialization can be performed. | |
394dc588 HV |
113 | * #) if @b->request_fd is non-zero and @mdev->ops->req_queue is set, |
114 | * then bind the prepared buffer to the request. | |
24ade5b6 MCC |
115 | * |
116 | * The return values from this function are intended to be directly returned | |
9fbe71b4 | 117 | * from &v4l2_ioctl_ops->vidioc_prepare_buf handler in driver. |
24ade5b6 | 118 | */ |
394dc588 HV |
119 | int vb2_prepare_buf(struct vb2_queue *q, struct media_device *mdev, |
120 | struct v4l2_buffer *b); | |
3c5be988 | 121 | |
24ade5b6 MCC |
122 | /** |
123 | * vb2_qbuf() - Queue a buffer from userspace | |
9fbe71b4 | 124 | * @q: pointer to &struct vb2_queue with videobuf2 queue. |
394dc588 | 125 | * @mdev: pointer to &struct media_device, may be NULL. |
9fbe71b4 MCC |
126 | * @b: buffer structure passed from userspace to |
127 | * &v4l2_ioctl_ops->vidioc_qbuf handler in driver | |
24ade5b6 | 128 | * |
9fbe71b4 | 129 | * Should be called from &v4l2_ioctl_ops->vidioc_qbuf handler of a driver. |
bf4404b4 | 130 | * |
24ade5b6 | 131 | * This function: |
bf4404b4 | 132 | * |
9fbe71b4 | 133 | * #) verifies the passed buffer; |
394dc588 HV |
134 | * #) if @b->request_fd is non-zero and @mdev->ops->req_queue is set, |
135 | * then bind the buffer to the request. | |
9fbe71b4 MCC |
136 | * #) if necessary, calls &vb2_ops->buf_prepare callback in the driver |
137 | * (if provided), in which driver-specific buffer initialization can | |
138 | * be performed; | |
139 | * #) if streaming is on, queues the buffer in driver by the means of | |
140 | * &vb2_ops->buf_queue callback for processing. | |
24ade5b6 MCC |
141 | * |
142 | * The return values from this function are intended to be directly returned | |
9fbe71b4 | 143 | * from &v4l2_ioctl_ops->vidioc_qbuf handler in driver. |
24ade5b6 | 144 | */ |
394dc588 HV |
145 | int vb2_qbuf(struct vb2_queue *q, struct media_device *mdev, |
146 | struct v4l2_buffer *b); | |
24ade5b6 MCC |
147 | |
148 | /** | |
149 | * vb2_expbuf() - Export a buffer as a file descriptor | |
9fbe71b4 MCC |
150 | * @q: pointer to &struct vb2_queue with videobuf2 queue. |
151 | * @eb: export buffer structure passed from userspace to | |
152 | * &v4l2_ioctl_ops->vidioc_expbuf handler in driver | |
24ade5b6 MCC |
153 | * |
154 | * The return values from this function are intended to be directly returned | |
9fbe71b4 | 155 | * from &v4l2_ioctl_ops->vidioc_expbuf handler in driver. |
24ade5b6 | 156 | */ |
3c5be988 | 157 | int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb); |
24ade5b6 MCC |
158 | |
159 | /** | |
160 | * vb2_dqbuf() - Dequeue a buffer to the userspace | |
9fbe71b4 MCC |
161 | * @q: pointer to &struct vb2_queue with videobuf2 queue. |
162 | * @b: buffer structure passed from userspace to | |
163 | * &v4l2_ioctl_ops->vidioc_dqbuf handler in driver | |
24ade5b6 MCC |
164 | * @nonblocking: if true, this call will not sleep waiting for a buffer if no |
165 | * buffers ready for dequeuing are present. Normally the driver | |
9fbe71b4 | 166 | * would be passing (&file->f_flags & %O_NONBLOCK) here |
24ade5b6 | 167 | * |
9fbe71b4 MCC |
168 | * Should be called from &v4l2_ioctl_ops->vidioc_dqbuf ioctl handler |
169 | * of a driver. | |
bf4404b4 | 170 | * |
24ade5b6 | 171 | * This function: |
bf4404b4 | 172 | * |
9fbe71b4 MCC |
173 | * #) verifies the passed buffer; |
174 | * #) calls &vb2_ops->buf_finish callback in the driver (if provided), in which | |
24ade5b6 | 175 | * driver can perform any additional operations that may be required before |
9fbe71b4 | 176 | * returning the buffer to userspace, such as cache sync; |
bf4404b4 | 177 | * #) the buffer struct members are filled with relevant information for |
24ade5b6 MCC |
178 | * the userspace. |
179 | * | |
180 | * The return values from this function are intended to be directly returned | |
9fbe71b4 | 181 | * from &v4l2_ioctl_ops->vidioc_dqbuf handler in driver. |
24ade5b6 | 182 | */ |
3c5be988 JS |
183 | int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking); |
184 | ||
24ade5b6 MCC |
185 | /** |
186 | * vb2_streamon - start streaming | |
9fbe71b4 MCC |
187 | * @q: pointer to &struct vb2_queue with videobuf2 queue. |
188 | * @type: type argument passed from userspace to vidioc_streamon handler, | |
189 | * as defined by &enum v4l2_buf_type. | |
24ade5b6 | 190 | * |
9fbe71b4 | 191 | * Should be called from &v4l2_ioctl_ops->vidioc_streamon handler of a driver. |
bf4404b4 | 192 | * |
24ade5b6 | 193 | * This function: |
bf4404b4 | 194 | * |
24ade5b6 MCC |
195 | * 1) verifies current state |
196 | * 2) passes any previously queued buffers to the driver and starts streaming | |
197 | * | |
198 | * The return values from this function are intended to be directly returned | |
9fbe71b4 | 199 | * from &v4l2_ioctl_ops->vidioc_streamon handler in the driver. |
24ade5b6 | 200 | */ |
3c5be988 | 201 | int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type); |
24ade5b6 MCC |
202 | |
203 | /** | |
204 | * vb2_streamoff - stop streaming | |
9fbe71b4 | 205 | * @q: pointer to &struct vb2_queue with videobuf2 queue. |
24ade5b6 MCC |
206 | * @type: type argument passed from userspace to vidioc_streamoff handler |
207 | * | |
208 | * Should be called from vidioc_streamoff handler of a driver. | |
bf4404b4 | 209 | * |
24ade5b6 | 210 | * This function: |
bf4404b4 MCC |
211 | * |
212 | * #) verifies current state, | |
213 | * #) stop streaming and dequeues any queued buffers, including those previously | |
24ade5b6 MCC |
214 | * passed to the driver (after waiting for the driver to finish). |
215 | * | |
216 | * This call can be used for pausing playback. | |
217 | * The return values from this function are intended to be directly returned | |
218 | * from vidioc_streamoff handler in the driver | |
219 | */ | |
3c5be988 JS |
220 | int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type); |
221 | ||
24ade5b6 MCC |
222 | /** |
223 | * vb2_queue_init() - initialize a videobuf2 queue | |
9fbe71b4 | 224 | * @q: pointer to &struct vb2_queue with videobuf2 queue. |
24ade5b6 MCC |
225 | * |
226 | * The vb2_queue structure should be allocated by the driver. The driver is | |
227 | * responsible of clearing it's content and setting initial values for some | |
228 | * required entries before calling this function. | |
229 | * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer | |
230 | * to the struct vb2_queue description in include/media/videobuf2-core.h | |
231 | * for more information. | |
232 | */ | |
3c5be988 | 233 | int __must_check vb2_queue_init(struct vb2_queue *q); |
24ade5b6 | 234 | |
b820935b LP |
235 | /** |
236 | * vb2_queue_init_name() - initialize a videobuf2 queue with a name | |
237 | * @q: pointer to &struct vb2_queue with videobuf2 queue. | |
238 | * @name: the queue name | |
239 | * | |
240 | * This function initializes the vb2_queue exactly like vb2_queue_init(), | |
241 | * and additionally sets the queue name. The queue name is used for logging | |
242 | * purpose, and should uniquely identify the queue within the context of the | |
243 | * device it belongs to. This is useful to attribute kernel log messages to the | |
244 | * right queue for m2m devices or other devices that handle multiple queues. | |
245 | */ | |
246 | int __must_check vb2_queue_init_name(struct vb2_queue *q, const char *name); | |
247 | ||
24ade5b6 MCC |
248 | /** |
249 | * vb2_queue_release() - stop streaming, release the queue and free memory | |
9fbe71b4 | 250 | * @q: pointer to &struct vb2_queue with videobuf2 queue. |
24ade5b6 MCC |
251 | * |
252 | * This function stops streaming and performs necessary clean ups, including | |
253 | * freeing video buffer memory. The driver is responsible for freeing | |
254 | * the vb2_queue structure itself. | |
255 | */ | |
3c5be988 | 256 | void vb2_queue_release(struct vb2_queue *q); |
24ade5b6 | 257 | |
a810ed0b TV |
258 | /** |
259 | * vb2_queue_change_type() - change the type of an inactive vb2_queue | |
260 | * @q: pointer to &struct vb2_queue with videobuf2 queue. | |
261 | * @type: the type to change to (V4L2_BUF_TYPE_VIDEO_*) | |
262 | * | |
263 | * This function changes the type of the vb2_queue. This is only possible | |
264 | * if the queue is not busy (i.e. no buffers have been allocated). | |
265 | * | |
266 | * vb2_queue_change_type() can be used to support multiple buffer types using | |
267 | * the same queue. The driver can implement v4l2_ioctl_ops.vidioc_reqbufs and | |
268 | * v4l2_ioctl_ops.vidioc_create_bufs functions and call vb2_queue_change_type() | |
269 | * before calling vb2_ioctl_reqbufs() or vb2_ioctl_create_bufs(), and thus | |
270 | * "lock" the buffer type until the buffers have been released. | |
271 | */ | |
272 | int vb2_queue_change_type(struct vb2_queue *q, unsigned int type); | |
273 | ||
24ade5b6 MCC |
274 | /** |
275 | * vb2_poll() - implements poll userspace operation | |
9fbe71b4 | 276 | * @q: pointer to &struct vb2_queue with videobuf2 queue. |
24ade5b6 MCC |
277 | * @file: file argument passed to the poll file operation handler |
278 | * @wait: wait argument passed to the poll file operation handler | |
279 | * | |
280 | * This function implements poll file operation handler for a driver. | |
281 | * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will | |
282 | * be informed that the file descriptor of a video device is available for | |
283 | * reading. | |
284 | * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor | |
285 | * will be reported as available for writing. | |
286 | * | |
287 | * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any | |
288 | * pending events. | |
289 | * | |
290 | * The return values from this function are intended to be directly returned | |
291 | * from poll handler in driver. | |
292 | */ | |
c23e0cb8 | 293 | __poll_t vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait); |
3c5be988 JS |
294 | |
295 | /* | |
296 | * The following functions are not part of the vb2 core API, but are simple | |
297 | * helper functions that you can use in your struct v4l2_file_operations, | |
298 | * struct v4l2_ioctl_ops and struct vb2_ops. They will serialize if vb2_queue->lock | |
7101d127 LP |
299 | * or video_device->lock is set, and they will set and test the queue owner |
300 | * (vb2_queue->owner) to check if the calling filehandle is permitted to do the | |
301 | * queuing operation. | |
3c5be988 JS |
302 | */ |
303 | ||
7101d127 LP |
304 | /** |
305 | * vb2_queue_is_busy() - check if the queue is busy | |
306 | * @q: pointer to &struct vb2_queue with videobuf2 queue. | |
307 | * @file: file through which the vb2 queue access is performed | |
308 | * | |
309 | * The queue is considered busy if it has an owner and the owner is not the | |
310 | * @file. | |
311 | * | |
312 | * Queue ownership is acquired and checked by some of the v4l2_ioctl_ops helpers | |
313 | * below. Drivers can also use this function directly when they need to | |
314 | * open-code ioctl handlers, for instance to add additional checks between the | |
315 | * queue ownership test and the call to the corresponding vb2 operation. | |
316 | */ | |
317 | static inline bool vb2_queue_is_busy(struct vb2_queue *q, struct file *file) | |
318 | { | |
319 | return q->owner && q->owner != file->private_data; | |
320 | } | |
321 | ||
3c5be988 JS |
322 | /* struct v4l2_ioctl_ops helpers */ |
323 | ||
324 | int vb2_ioctl_reqbufs(struct file *file, void *priv, | |
325 | struct v4l2_requestbuffers *p); | |
326 | int vb2_ioctl_create_bufs(struct file *file, void *priv, | |
327 | struct v4l2_create_buffers *p); | |
328 | int vb2_ioctl_prepare_buf(struct file *file, void *priv, | |
329 | struct v4l2_buffer *p); | |
330 | int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p); | |
331 | int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p); | |
332 | int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p); | |
333 | int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i); | |
334 | int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i); | |
335 | int vb2_ioctl_expbuf(struct file *file, void *priv, | |
336 | struct v4l2_exportbuffer *p); | |
337 | ||
338 | /* struct v4l2_file_operations helpers */ | |
339 | ||
340 | int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma); | |
341 | int vb2_fop_release(struct file *file); | |
342 | int _vb2_fop_release(struct file *file, struct mutex *lock); | |
343 | ssize_t vb2_fop_write(struct file *file, const char __user *buf, | |
344 | size_t count, loff_t *ppos); | |
345 | ssize_t vb2_fop_read(struct file *file, char __user *buf, | |
346 | size_t count, loff_t *ppos); | |
c23e0cb8 | 347 | __poll_t vb2_fop_poll(struct file *file, poll_table *wait); |
3c5be988 JS |
348 | #ifndef CONFIG_MMU |
349 | unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr, | |
350 | unsigned long len, unsigned long pgoff, unsigned long flags); | |
351 | #endif | |
352 | ||
f729ef57 HV |
353 | /** |
354 | * vb2_video_unregister_device - unregister the video device and release queue | |
355 | * | |
356 | * @vdev: pointer to &struct video_device | |
357 | * | |
358 | * If the driver uses vb2_fop_release()/_vb2_fop_release(), then it should use | |
359 | * vb2_video_unregister_device() instead of video_unregister_device(). | |
360 | * | |
361 | * This function will call video_unregister_device() and then release the | |
362 | * vb2_queue if streaming is in progress. This will stop streaming and | |
363 | * this will simplify the unbind sequence since after this call all subdevs | |
364 | * will have stopped streaming as well. | |
365 | */ | |
366 | void vb2_video_unregister_device(struct video_device *vdev); | |
367 | ||
dba2d12a MCC |
368 | /** |
369 | * vb2_ops_wait_prepare - helper function to lock a struct &vb2_queue | |
370 | * | |
9fbe71b4 | 371 | * @vq: pointer to &struct vb2_queue |
dba2d12a MCC |
372 | * |
373 | * ..note:: only use if vq->lock is non-NULL. | |
374 | */ | |
3c5be988 | 375 | void vb2_ops_wait_prepare(struct vb2_queue *vq); |
dba2d12a MCC |
376 | |
377 | /** | |
378 | * vb2_ops_wait_finish - helper function to unlock a struct &vb2_queue | |
379 | * | |
9fbe71b4 | 380 | * @vq: pointer to &struct vb2_queue |
dba2d12a MCC |
381 | * |
382 | * ..note:: only use if vq->lock is non-NULL. | |
383 | */ | |
3c5be988 JS |
384 | void vb2_ops_wait_finish(struct vb2_queue *vq); |
385 | ||
86f6bd3c HV |
386 | struct media_request; |
387 | int vb2_request_validate(struct media_request *req); | |
388 | void vb2_request_queue(struct media_request *req); | |
389 | ||
c139990e | 390 | #endif /* _MEDIA_VIDEOBUF2_V4L2_H */ |