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