videobuf2-v4l2.h 14 KB

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