videobuf2-v4l2.h 14 KB

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