videobuf2-v4l2.h 10 KB

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