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