v4l2-mem2mem.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Memory-to-memory device framework for Video for Linux 2.
  3. *
  4. * Helper functions for devices that use memory buffers for both source
  5. * and destination.
  6. *
  7. * Copyright (c) 2009 Samsung Electronics Co., Ltd.
  8. * Pawel Osciak, <pawel@osciak.com>
  9. * Marek Szyprowski, <m.szyprowski@samsung.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the
  14. * License, or (at your option) any later version
  15. */
  16. #ifndef _MEDIA_V4L2_MEM2MEM_H
  17. #define _MEDIA_V4L2_MEM2MEM_H
  18. #include <media/videobuf2-v4l2.h>
  19. /**
  20. * struct v4l2_m2m_ops - mem-to-mem device driver callbacks
  21. * @device_run: required. Begin the actual job (transaction) inside this
  22. * callback.
  23. * The job does NOT have to end before this callback returns
  24. * (and it will be the usual case). When the job finishes,
  25. * v4l2_m2m_job_finish() has to be called.
  26. * @job_ready: optional. Should return 0 if the driver does not have a job
  27. * fully prepared to run yet (i.e. it will not be able to finish a
  28. * transaction without sleeping). If not provided, it will be
  29. * assumed that one source and one destination buffer are all
  30. * that is required for the driver to perform one full transaction.
  31. * This method may not sleep.
  32. * @job_abort: required. Informs the driver that it has to abort the currently
  33. * running transaction as soon as possible (i.e. as soon as it can
  34. * stop the device safely; e.g. in the next interrupt handler),
  35. * even if the transaction would not have been finished by then.
  36. * After the driver performs the necessary steps, it has to call
  37. * v4l2_m2m_job_finish() (as if the transaction ended normally).
  38. * This function does not have to (and will usually not) wait
  39. * until the device enters a state when it can be stopped.
  40. * @lock: optional. Define a driver's own lock callback, instead of using
  41. * m2m_ctx->q_lock.
  42. * @unlock: optional. Define a driver's own unlock callback, instead of
  43. * using m2m_ctx->q_lock.
  44. */
  45. struct v4l2_m2m_ops {
  46. void (*device_run)(void *priv);
  47. int (*job_ready)(void *priv);
  48. void (*job_abort)(void *priv);
  49. void (*lock)(void *priv);
  50. void (*unlock)(void *priv);
  51. };
  52. struct v4l2_m2m_dev;
  53. struct v4l2_m2m_queue_ctx {
  54. /* private: internal use only */
  55. struct vb2_queue q;
  56. /* Queue for buffers ready to be processed as soon as this
  57. * instance receives access to the device */
  58. struct list_head rdy_queue;
  59. spinlock_t rdy_spinlock;
  60. u8 num_rdy;
  61. bool buffered;
  62. };
  63. struct v4l2_m2m_ctx {
  64. /* optional cap/out vb2 queues lock */
  65. struct mutex *q_lock;
  66. /* private: internal use only */
  67. struct v4l2_m2m_dev *m2m_dev;
  68. /* Capture (output to memory) queue context */
  69. struct v4l2_m2m_queue_ctx cap_q_ctx;
  70. /* Output (input from memory) queue context */
  71. struct v4l2_m2m_queue_ctx out_q_ctx;
  72. /* For device job queue */
  73. struct list_head queue;
  74. unsigned long job_flags;
  75. wait_queue_head_t finished;
  76. /* Instance private data */
  77. void *priv;
  78. };
  79. struct v4l2_m2m_buffer {
  80. struct vb2_v4l2_buffer vb;
  81. struct list_head list;
  82. };
  83. void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev *m2m_dev);
  84. struct vb2_queue *v4l2_m2m_get_vq(struct v4l2_m2m_ctx *m2m_ctx,
  85. enum v4l2_buf_type type);
  86. void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx);
  87. void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev,
  88. struct v4l2_m2m_ctx *m2m_ctx);
  89. static inline void
  90. v4l2_m2m_buf_done(struct vb2_v4l2_buffer *buf, enum vb2_buffer_state state)
  91. {
  92. vb2_buffer_done(&buf->vb2_buf, state);
  93. }
  94. int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  95. struct v4l2_requestbuffers *reqbufs);
  96. int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  97. struct v4l2_buffer *buf);
  98. int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  99. struct v4l2_buffer *buf);
  100. int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  101. struct v4l2_buffer *buf);
  102. int v4l2_m2m_prepare_buf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  103. struct v4l2_buffer *buf);
  104. int v4l2_m2m_create_bufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  105. struct v4l2_create_buffers *create);
  106. int v4l2_m2m_expbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  107. struct v4l2_exportbuffer *eb);
  108. int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  109. enum v4l2_buf_type type);
  110. int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  111. enum v4l2_buf_type type);
  112. unsigned int v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  113. struct poll_table_struct *wait);
  114. int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  115. struct vm_area_struct *vma);
  116. struct v4l2_m2m_dev *v4l2_m2m_init(const struct v4l2_m2m_ops *m2m_ops);
  117. void v4l2_m2m_release(struct v4l2_m2m_dev *m2m_dev);
  118. struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev,
  119. void *drv_priv,
  120. int (*queue_init)(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq));
  121. static inline void v4l2_m2m_set_src_buffered(struct v4l2_m2m_ctx *m2m_ctx,
  122. bool buffered)
  123. {
  124. m2m_ctx->out_q_ctx.buffered = buffered;
  125. }
  126. static inline void v4l2_m2m_set_dst_buffered(struct v4l2_m2m_ctx *m2m_ctx,
  127. bool buffered)
  128. {
  129. m2m_ctx->cap_q_ctx.buffered = buffered;
  130. }
  131. void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx *m2m_ctx);
  132. void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx *m2m_ctx,
  133. struct vb2_v4l2_buffer *vbuf);
  134. /**
  135. * v4l2_m2m_num_src_bufs_ready() - return the number of source buffers ready for
  136. * use
  137. *
  138. * @m2m_ctx: pointer to struct v4l2_m2m_ctx
  139. */
  140. static inline
  141. unsigned int v4l2_m2m_num_src_bufs_ready(struct v4l2_m2m_ctx *m2m_ctx)
  142. {
  143. return m2m_ctx->out_q_ctx.num_rdy;
  144. }
  145. /**
  146. * v4l2_m2m_num_src_bufs_ready() - return the number of destination buffers
  147. * ready for use
  148. *
  149. * @m2m_ctx: pointer to struct v4l2_m2m_ctx
  150. */
  151. static inline
  152. unsigned int v4l2_m2m_num_dst_bufs_ready(struct v4l2_m2m_ctx *m2m_ctx)
  153. {
  154. return m2m_ctx->cap_q_ctx.num_rdy;
  155. }
  156. void *v4l2_m2m_next_buf(struct v4l2_m2m_queue_ctx *q_ctx);
  157. /**
  158. * v4l2_m2m_next_src_buf() - return next source buffer from the list of ready
  159. * buffers
  160. *
  161. * @m2m_ctx: pointer to struct v4l2_m2m_ctx
  162. */
  163. static inline void *v4l2_m2m_next_src_buf(struct v4l2_m2m_ctx *m2m_ctx)
  164. {
  165. return v4l2_m2m_next_buf(&m2m_ctx->out_q_ctx);
  166. }
  167. /**
  168. * v4l2_m2m_next_dst_buf() - return next destination buffer from the list of
  169. * ready buffers
  170. *
  171. * @m2m_ctx: pointer to struct v4l2_m2m_ctx
  172. */
  173. static inline void *v4l2_m2m_next_dst_buf(struct v4l2_m2m_ctx *m2m_ctx)
  174. {
  175. return v4l2_m2m_next_buf(&m2m_ctx->cap_q_ctx);
  176. }
  177. /**
  178. * v4l2_m2m_get_src_vq() - return vb2_queue for source buffers
  179. *
  180. * @m2m_ctx: pointer to struct v4l2_m2m_ctx
  181. */
  182. static inline
  183. struct vb2_queue *v4l2_m2m_get_src_vq(struct v4l2_m2m_ctx *m2m_ctx)
  184. {
  185. return &m2m_ctx->out_q_ctx.q;
  186. }
  187. /**
  188. * v4l2_m2m_get_dst_vq() - return vb2_queue for destination buffers
  189. *
  190. * @m2m_ctx: pointer to struct v4l2_m2m_ctx
  191. */
  192. static inline
  193. struct vb2_queue *v4l2_m2m_get_dst_vq(struct v4l2_m2m_ctx *m2m_ctx)
  194. {
  195. return &m2m_ctx->cap_q_ctx.q;
  196. }
  197. void *v4l2_m2m_buf_remove(struct v4l2_m2m_queue_ctx *q_ctx);
  198. /**
  199. * v4l2_m2m_src_buf_remove() - take off a source buffer from the list of ready
  200. * buffers and return it
  201. *
  202. * @m2m_ctx: pointer to struct v4l2_m2m_ctx
  203. */
  204. static inline void *v4l2_m2m_src_buf_remove(struct v4l2_m2m_ctx *m2m_ctx)
  205. {
  206. return v4l2_m2m_buf_remove(&m2m_ctx->out_q_ctx);
  207. }
  208. /**
  209. * v4l2_m2m_dst_buf_remove() - take off a destination buffer from the list of
  210. * ready buffers and return it
  211. *
  212. * @m2m_ctx: pointer to struct v4l2_m2m_ctx
  213. */
  214. static inline void *v4l2_m2m_dst_buf_remove(struct v4l2_m2m_ctx *m2m_ctx)
  215. {
  216. return v4l2_m2m_buf_remove(&m2m_ctx->cap_q_ctx);
  217. }
  218. /* v4l2 ioctl helpers */
  219. int v4l2_m2m_ioctl_reqbufs(struct file *file, void *priv,
  220. struct v4l2_requestbuffers *rb);
  221. int v4l2_m2m_ioctl_create_bufs(struct file *file, void *fh,
  222. struct v4l2_create_buffers *create);
  223. int v4l2_m2m_ioctl_querybuf(struct file *file, void *fh,
  224. struct v4l2_buffer *buf);
  225. int v4l2_m2m_ioctl_expbuf(struct file *file, void *fh,
  226. struct v4l2_exportbuffer *eb);
  227. int v4l2_m2m_ioctl_qbuf(struct file *file, void *fh,
  228. struct v4l2_buffer *buf);
  229. int v4l2_m2m_ioctl_dqbuf(struct file *file, void *fh,
  230. struct v4l2_buffer *buf);
  231. int v4l2_m2m_ioctl_prepare_buf(struct file *file, void *fh,
  232. struct v4l2_buffer *buf);
  233. int v4l2_m2m_ioctl_streamon(struct file *file, void *fh,
  234. enum v4l2_buf_type type);
  235. int v4l2_m2m_ioctl_streamoff(struct file *file, void *fh,
  236. enum v4l2_buf_type type);
  237. int v4l2_m2m_fop_mmap(struct file *file, struct vm_area_struct *vma);
  238. unsigned int v4l2_m2m_fop_poll(struct file *file, poll_table *wait);
  239. #endif /* _MEDIA_V4L2_MEM2MEM_H */