videobuf2-v4l2.h 9.8 KB

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