videobuf2-v4l2.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #include <linux/videodev2.h>
  15. #include <media/videobuf2-core.h>
  16. #if VB2_MAX_FRAME != VIDEO_MAX_FRAME
  17. #error VB2_MAX_FRAME != VIDEO_MAX_FRAME
  18. #endif
  19. #if VB2_MAX_PLANES != VIDEO_MAX_PLANES
  20. #error VB2_MAX_PLANES != VIDEO_MAX_PLANES
  21. #endif
  22. /**
  23. * struct vb2_v4l2_buffer - video buffer information for v4l2.
  24. *
  25. * @vb2_buf: embedded struct &vb2_buffer.
  26. * @flags: buffer informational flags.
  27. * @field: field order of the image in the buffer, as defined by
  28. * &enum v4l2_field.
  29. * @timecode: frame timecode.
  30. * @sequence: sequence count of this frame.
  31. * @request_fd: the request_fd associated with this buffer
  32. * @planes: plane information (userptr/fd, length, bytesused, data_offset).
  33. *
  34. * Should contain enough information to be able to cover all the fields
  35. * of &struct v4l2_buffer at ``videodev2.h``.
  36. */
  37. struct vb2_v4l2_buffer {
  38. struct vb2_buffer vb2_buf;
  39. __u32 flags;
  40. __u32 field;
  41. struct v4l2_timecode timecode;
  42. __u32 sequence;
  43. __s32 request_fd;
  44. struct vb2_plane planes[VB2_MAX_PLANES];
  45. };
  46. /*
  47. * to_vb2_v4l2_buffer() - cast struct vb2_buffer * to struct vb2_v4l2_buffer *
  48. */
  49. #define to_vb2_v4l2_buffer(vb) \
  50. container_of(vb, struct vb2_v4l2_buffer, vb2_buf)
  51. int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b);
  52. /**
  53. * vb2_reqbufs() - Wrapper for vb2_core_reqbufs() that also verifies
  54. * the memory and type values.
  55. *
  56. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  57. * @req: &struct v4l2_requestbuffers passed from userspace to
  58. * &v4l2_ioctl_ops->vidioc_reqbufs handler in driver.
  59. */
  60. int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req);
  61. /**
  62. * vb2_create_bufs() - Wrapper for vb2_core_create_bufs() that also verifies
  63. * the memory and type values.
  64. *
  65. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  66. * @create: creation parameters, passed from userspace to
  67. * &v4l2_ioctl_ops->vidioc_create_bufs handler in driver
  68. */
  69. int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create);
  70. /**
  71. * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
  72. *
  73. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  74. * @mdev: pointer to &struct media_device, may be NULL.
  75. * @b: buffer structure passed from userspace to
  76. * &v4l2_ioctl_ops->vidioc_prepare_buf handler in driver
  77. *
  78. * Should be called from &v4l2_ioctl_ops->vidioc_prepare_buf ioctl handler
  79. * of a driver.
  80. *
  81. * This function:
  82. *
  83. * #) verifies the passed buffer,
  84. * #) calls &vb2_ops->buf_prepare callback in the driver (if provided),
  85. * in which driver-specific buffer initialization can be performed.
  86. * #) if @b->request_fd is non-zero and @mdev->ops->req_queue is set,
  87. * then bind the prepared buffer to the request.
  88. *
  89. * The return values from this function are intended to be directly returned
  90. * from &v4l2_ioctl_ops->vidioc_prepare_buf handler in driver.
  91. */
  92. int vb2_prepare_buf(struct vb2_queue *q, struct media_device *mdev,
  93. struct v4l2_buffer *b);
  94. /**
  95. * vb2_qbuf() - Queue a buffer from userspace
  96. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  97. * @mdev: pointer to &struct media_device, may be NULL.
  98. * @b: buffer structure passed from userspace to
  99. * &v4l2_ioctl_ops->vidioc_qbuf handler in driver
  100. *
  101. * Should be called from &v4l2_ioctl_ops->vidioc_qbuf handler of a driver.
  102. *
  103. * This function:
  104. *
  105. * #) verifies the passed buffer;
  106. * #) if @b->request_fd is non-zero and @mdev->ops->req_queue is set,
  107. * then bind the buffer to the request.
  108. * #) if necessary, calls &vb2_ops->buf_prepare callback in the driver
  109. * (if provided), in which driver-specific buffer initialization can
  110. * be performed;
  111. * #) if streaming is on, queues the buffer in driver by the means of
  112. * &vb2_ops->buf_queue callback for processing.
  113. *
  114. * The return values from this function are intended to be directly returned
  115. * from &v4l2_ioctl_ops->vidioc_qbuf handler in driver.
  116. */
  117. int vb2_qbuf(struct vb2_queue *q, struct media_device *mdev,
  118. struct v4l2_buffer *b);
  119. /**
  120. * vb2_expbuf() - Export a buffer as a file descriptor
  121. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  122. * @eb: export buffer structure passed from userspace to
  123. * &v4l2_ioctl_ops->vidioc_expbuf handler in driver
  124. *
  125. * The return values from this function are intended to be directly returned
  126. * from &v4l2_ioctl_ops->vidioc_expbuf handler in driver.
  127. */
  128. int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb);
  129. /**
  130. * vb2_dqbuf() - Dequeue a buffer to the userspace
  131. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  132. * @b: buffer structure passed from userspace to
  133. * &v4l2_ioctl_ops->vidioc_dqbuf handler in driver
  134. * @nonblocking: if true, this call will not sleep waiting for a buffer if no
  135. * buffers ready for dequeuing are present. Normally the driver
  136. * would be passing (&file->f_flags & %O_NONBLOCK) here
  137. *
  138. * Should be called from &v4l2_ioctl_ops->vidioc_dqbuf ioctl handler
  139. * of a driver.
  140. *
  141. * This function:
  142. *
  143. * #) verifies the passed buffer;
  144. * #) calls &vb2_ops->buf_finish callback in the driver (if provided), in which
  145. * driver can perform any additional operations that may be required before
  146. * returning the buffer to userspace, such as cache sync;
  147. * #) the buffer struct members are filled with relevant information for
  148. * the userspace.
  149. *
  150. * The return values from this function are intended to be directly returned
  151. * from &v4l2_ioctl_ops->vidioc_dqbuf handler in driver.
  152. */
  153. int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking);
  154. /**
  155. * vb2_streamon - start streaming
  156. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  157. * @type: type argument passed from userspace to vidioc_streamon handler,
  158. * as defined by &enum v4l2_buf_type.
  159. *
  160. * Should be called from &v4l2_ioctl_ops->vidioc_streamon handler of a driver.
  161. *
  162. * This function:
  163. *
  164. * 1) verifies current state
  165. * 2) passes any previously queued buffers to the driver and starts streaming
  166. *
  167. * The return values from this function are intended to be directly returned
  168. * from &v4l2_ioctl_ops->vidioc_streamon handler in the driver.
  169. */
  170. int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type);
  171. /**
  172. * vb2_streamoff - stop streaming
  173. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  174. * @type: type argument passed from userspace to vidioc_streamoff handler
  175. *
  176. * Should be called from vidioc_streamoff handler of a driver.
  177. *
  178. * This function:
  179. *
  180. * #) verifies current state,
  181. * #) stop streaming and dequeues any queued buffers, including those previously
  182. * passed to the driver (after waiting for the driver to finish).
  183. *
  184. * This call can be used for pausing playback.
  185. * The return values from this function are intended to be directly returned
  186. * from vidioc_streamoff handler in the driver
  187. */
  188. int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type);
  189. /**
  190. * vb2_queue_init() - initialize a videobuf2 queue
  191. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  192. *
  193. * The vb2_queue structure should be allocated by the driver. The driver is
  194. * responsible of clearing it's content and setting initial values for some
  195. * required entries before calling this function.
  196. * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
  197. * to the struct vb2_queue description in include/media/videobuf2-core.h
  198. * for more information.
  199. */
  200. int __must_check vb2_queue_init(struct vb2_queue *q);
  201. /**
  202. * vb2_queue_release() - stop streaming, release the queue and free memory
  203. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  204. *
  205. * This function stops streaming and performs necessary clean ups, including
  206. * freeing video buffer memory. The driver is responsible for freeing
  207. * the vb2_queue structure itself.
  208. */
  209. void vb2_queue_release(struct vb2_queue *q);
  210. /**
  211. * vb2_poll() - implements poll userspace operation
  212. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  213. * @file: file argument passed to the poll file operation handler
  214. * @wait: wait argument passed to the poll file operation handler
  215. *
  216. * This function implements poll file operation handler for a driver.
  217. * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
  218. * be informed that the file descriptor of a video device is available for
  219. * reading.
  220. * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
  221. * will be reported as available for writing.
  222. *
  223. * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
  224. * pending events.
  225. *
  226. * The return values from this function are intended to be directly returned
  227. * from poll handler in driver.
  228. */
  229. __poll_t vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait);
  230. /*
  231. * The following functions are not part of the vb2 core API, but are simple
  232. * helper functions that you can use in your struct v4l2_file_operations,
  233. * struct v4l2_ioctl_ops and struct vb2_ops. They will serialize if vb2_queue->lock
  234. * or video_device->lock is set, and they will set and test vb2_queue->owner
  235. * to check if the calling filehandle is permitted to do the queuing operation.
  236. */
  237. /* struct v4l2_ioctl_ops helpers */
  238. int vb2_ioctl_reqbufs(struct file *file, void *priv,
  239. struct v4l2_requestbuffers *p);
  240. int vb2_ioctl_create_bufs(struct file *file, void *priv,
  241. struct v4l2_create_buffers *p);
  242. int vb2_ioctl_prepare_buf(struct file *file, void *priv,
  243. struct v4l2_buffer *p);
  244. int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p);
  245. int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p);
  246. int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p);
  247. int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i);
  248. int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i);
  249. int vb2_ioctl_expbuf(struct file *file, void *priv,
  250. struct v4l2_exportbuffer *p);
  251. /* struct v4l2_file_operations helpers */
  252. int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma);
  253. int vb2_fop_release(struct file *file);
  254. int _vb2_fop_release(struct file *file, struct mutex *lock);
  255. ssize_t vb2_fop_write(struct file *file, const char __user *buf,
  256. size_t count, loff_t *ppos);
  257. ssize_t vb2_fop_read(struct file *file, char __user *buf,
  258. size_t count, loff_t *ppos);
  259. __poll_t vb2_fop_poll(struct file *file, poll_table *wait);
  260. #ifndef CONFIG_MMU
  261. unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
  262. unsigned long len, unsigned long pgoff, unsigned long flags);
  263. #endif
  264. /**
  265. * vb2_ops_wait_prepare - helper function to lock a struct &vb2_queue
  266. *
  267. * @vq: pointer to &struct vb2_queue
  268. *
  269. * ..note:: only use if vq->lock is non-NULL.
  270. */
  271. void vb2_ops_wait_prepare(struct vb2_queue *vq);
  272. /**
  273. * vb2_ops_wait_finish - helper function to unlock a struct &vb2_queue
  274. *
  275. * @vq: pointer to &struct vb2_queue
  276. *
  277. * ..note:: only use if vq->lock is non-NULL.
  278. */
  279. void vb2_ops_wait_finish(struct vb2_queue *vq);
  280. struct media_request;
  281. int vb2_request_validate(struct media_request *req);
  282. void vb2_request_queue(struct media_request *req);
  283. #endif /* _MEDIA_VIDEOBUF2_V4L2_H */