v4l2-mem2mem.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. * &v4l2_m2m_ctx->q_lock.
  42. * @unlock: optional. Define a driver's own unlock callback, instead of
  43. * using &v4l2_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. /**
  54. * struct v4l2_m2m_queue_ctx - represents a queue for buffers ready to be
  55. * processed
  56. *
  57. * @q: pointer to struct &vb2_queue
  58. * @rdy_queue: List of V4L2 mem-to-mem queues
  59. * @rdy_spinlock: spin lock to protect the struct usage
  60. * @num_rdy: number of buffers ready to be processed
  61. * @buffered: is the queue buffered?
  62. *
  63. * Queue for buffers ready to be processed as soon as this
  64. * instance receives access to the device.
  65. */
  66. struct v4l2_m2m_queue_ctx {
  67. struct vb2_queue q;
  68. struct list_head rdy_queue;
  69. spinlock_t rdy_spinlock;
  70. u8 num_rdy;
  71. bool buffered;
  72. };
  73. /**
  74. * struct v4l2_m2m_ctx - Memory to memory context structure
  75. *
  76. * @q_lock: struct &mutex lock
  77. * @m2m_dev: opaque pointer to the internal data to handle M2M context
  78. * @cap_q_ctx: Capture (output to memory) queue context
  79. * @out_q_ctx: Output (input from memory) queue context
  80. * @queue: List of memory to memory contexts
  81. * @job_flags: Job queue flags, used internally by v4l2-mem2mem.c:
  82. * %TRANS_QUEUED, %TRANS_RUNNING and %TRANS_ABORT.
  83. * @finished: Wait queue used to signalize when a job queue finished.
  84. * @priv: Instance private data
  85. *
  86. * The memory to memory context is specific to a file handle, NOT to e.g.
  87. * a device.
  88. */
  89. struct v4l2_m2m_ctx {
  90. /* optional cap/out vb2 queues lock */
  91. struct mutex *q_lock;
  92. /* internal use only */
  93. struct v4l2_m2m_dev *m2m_dev;
  94. struct v4l2_m2m_queue_ctx cap_q_ctx;
  95. struct v4l2_m2m_queue_ctx out_q_ctx;
  96. /* For device job queue */
  97. struct list_head queue;
  98. unsigned long job_flags;
  99. wait_queue_head_t finished;
  100. void *priv;
  101. };
  102. /**
  103. * struct v4l2_m2m_buffer - Memory to memory buffer
  104. *
  105. * @vb: pointer to struct &vb2_v4l2_buffer
  106. * @list: list of m2m buffers
  107. */
  108. struct v4l2_m2m_buffer {
  109. struct vb2_v4l2_buffer vb;
  110. struct list_head list;
  111. };
  112. /**
  113. * v4l2_m2m_get_curr_priv() - return driver private data for the currently
  114. * running instance or NULL if no instance is running
  115. *
  116. * @m2m_dev: opaque pointer to the internal data to handle M2M context
  117. */
  118. void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev *m2m_dev);
  119. /**
  120. * v4l2_m2m_get_vq() - return vb2_queue for the given type
  121. *
  122. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  123. * @type: type of the V4L2 buffer, as defined by enum &v4l2_buf_type
  124. */
  125. struct vb2_queue *v4l2_m2m_get_vq(struct v4l2_m2m_ctx *m2m_ctx,
  126. enum v4l2_buf_type type);
  127. /**
  128. * v4l2_m2m_try_schedule() - check whether an instance is ready to be added to
  129. * the pending job queue and add it if so.
  130. *
  131. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  132. *
  133. * There are three basic requirements an instance has to meet to be able to run:
  134. * 1) at least one source buffer has to be queued,
  135. * 2) at least one destination buffer has to be queued,
  136. * 3) streaming has to be on.
  137. *
  138. * If a queue is buffered (for example a decoder hardware ringbuffer that has
  139. * to be drained before doing streamoff), allow scheduling without v4l2 buffers
  140. * on that queue.
  141. *
  142. * There may also be additional, custom requirements. In such case the driver
  143. * should supply a custom callback (job_ready in v4l2_m2m_ops) that should
  144. * return 1 if the instance is ready.
  145. * An example of the above could be an instance that requires more than one
  146. * src/dst buffer per transaction.
  147. */
  148. void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx);
  149. /**
  150. * v4l2_m2m_job_finish() - inform the framework that a job has been finished
  151. * and have it clean up
  152. *
  153. * @m2m_dev: opaque pointer to the internal data to handle M2M context
  154. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  155. *
  156. * Called by a driver to yield back the device after it has finished with it.
  157. * Should be called as soon as possible after reaching a state which allows
  158. * other instances to take control of the device.
  159. *
  160. * This function has to be called only after &v4l2_m2m_ops->device_run
  161. * callback has been called on the driver. To prevent recursion, it should
  162. * not be called directly from the &v4l2_m2m_ops->device_run callback though.
  163. */
  164. void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev,
  165. struct v4l2_m2m_ctx *m2m_ctx);
  166. static inline void
  167. v4l2_m2m_buf_done(struct vb2_v4l2_buffer *buf, enum vb2_buffer_state state)
  168. {
  169. vb2_buffer_done(&buf->vb2_buf, state);
  170. }
  171. /**
  172. * v4l2_m2m_reqbufs() - multi-queue-aware REQBUFS multiplexer
  173. *
  174. * @file: pointer to struct &file
  175. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  176. * @reqbufs: pointer to struct &v4l2_requestbuffers
  177. */
  178. int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  179. struct v4l2_requestbuffers *reqbufs);
  180. /**
  181. * v4l2_m2m_querybuf() - multi-queue-aware QUERYBUF multiplexer
  182. *
  183. * @file: pointer to struct &file
  184. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  185. * @buf: pointer to struct &v4l2_buffer
  186. *
  187. * See v4l2_m2m_mmap() documentation for details.
  188. */
  189. int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  190. struct v4l2_buffer *buf);
  191. /**
  192. * v4l2_m2m_qbuf() - enqueue a source or destination buffer, depending on
  193. * the type
  194. *
  195. * @file: pointer to struct &file
  196. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  197. * @buf: pointer to struct &v4l2_buffer
  198. */
  199. int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  200. struct v4l2_buffer *buf);
  201. /**
  202. * v4l2_m2m_dqbuf() - dequeue a source or destination buffer, depending on
  203. * the type
  204. *
  205. * @file: pointer to struct &file
  206. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  207. * @buf: pointer to struct &v4l2_buffer
  208. */
  209. int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  210. struct v4l2_buffer *buf);
  211. /**
  212. * v4l2_m2m_prepare_buf() - prepare a source or destination buffer, depending on
  213. * the type
  214. *
  215. * @file: pointer to struct &file
  216. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  217. * @buf: pointer to struct &v4l2_buffer
  218. */
  219. int v4l2_m2m_prepare_buf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  220. struct v4l2_buffer *buf);
  221. /**
  222. * v4l2_m2m_create_bufs() - create a source or destination buffer, depending
  223. * on the type
  224. *
  225. * @file: pointer to struct &file
  226. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  227. * @create: pointer to struct &v4l2_create_buffers
  228. */
  229. int v4l2_m2m_create_bufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  230. struct v4l2_create_buffers *create);
  231. /**
  232. * v4l2_m2m_expbuf() - export a source or destination buffer, depending on
  233. * the type
  234. *
  235. * @file: pointer to struct &file
  236. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  237. * @eb: pointer to struct &v4l2_exportbuffer
  238. */
  239. int v4l2_m2m_expbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  240. struct v4l2_exportbuffer *eb);
  241. /**
  242. * v4l2_m2m_streamon() - turn on streaming for a video queue
  243. *
  244. * @file: pointer to struct &file
  245. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  246. * @type: type of the V4L2 buffer, as defined by enum &v4l2_buf_type
  247. */
  248. int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  249. enum v4l2_buf_type type);
  250. /**
  251. * v4l2_m2m_streamoff() - turn off streaming for a video queue
  252. *
  253. * @file: pointer to struct &file
  254. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  255. * @type: type of the V4L2 buffer, as defined by enum &v4l2_buf_type
  256. */
  257. int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  258. enum v4l2_buf_type type);
  259. /**
  260. * v4l2_m2m_poll() - poll replacement, for destination buffers only
  261. *
  262. * @file: pointer to struct &file
  263. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  264. * @wait: pointer to struct &poll_table_struct
  265. *
  266. * Call from the driver's poll() function. Will poll both queues. If a buffer
  267. * is available to dequeue (with dqbuf) from the source queue, this will
  268. * indicate that a non-blocking write can be performed, while read will be
  269. * returned in case of the destination queue.
  270. */
  271. unsigned int v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  272. struct poll_table_struct *wait);
  273. /**
  274. * v4l2_m2m_mmap() - source and destination queues-aware mmap multiplexer
  275. *
  276. * @file: pointer to struct &file
  277. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  278. * @vma: pointer to struct &vm_area_struct
  279. *
  280. * Call from driver's mmap() function. Will handle mmap() for both queues
  281. * seamlessly for videobuffer, which will receive normal per-queue offsets and
  282. * proper videobuf queue pointers. The differentiation is made outside videobuf
  283. * by adding a predefined offset to buffers from one of the queues and
  284. * subtracting it before passing it back to videobuf. Only drivers (and
  285. * thus applications) receive modified offsets.
  286. */
  287. int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  288. struct vm_area_struct *vma);
  289. /**
  290. * v4l2_m2m_init() - initialize per-driver m2m data
  291. *
  292. * @m2m_ops: pointer to struct v4l2_m2m_ops
  293. *
  294. * Usually called from driver's ``probe()`` function.
  295. *
  296. * Return: returns an opaque pointer to the internal data to handle M2M context
  297. */
  298. struct v4l2_m2m_dev *v4l2_m2m_init(const struct v4l2_m2m_ops *m2m_ops);
  299. /**
  300. * v4l2_m2m_release() - cleans up and frees a m2m_dev structure
  301. *
  302. * @m2m_dev: opaque pointer to the internal data to handle M2M context
  303. *
  304. * Usually called from driver's ``remove()`` function.
  305. */
  306. void v4l2_m2m_release(struct v4l2_m2m_dev *m2m_dev);
  307. /**
  308. * v4l2_m2m_ctx_init() - allocate and initialize a m2m context
  309. *
  310. * @m2m_dev: opaque pointer to the internal data to handle M2M context
  311. * @drv_priv: driver's instance private data
  312. * @queue_init: a callback for queue type-specific initialization function
  313. * to be used for initializing videobuf_queues
  314. *
  315. * Usually called from driver's ``open()`` function.
  316. */
  317. struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev,
  318. void *drv_priv,
  319. int (*queue_init)(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq));
  320. static inline void v4l2_m2m_set_src_buffered(struct v4l2_m2m_ctx *m2m_ctx,
  321. bool buffered)
  322. {
  323. m2m_ctx->out_q_ctx.buffered = buffered;
  324. }
  325. static inline void v4l2_m2m_set_dst_buffered(struct v4l2_m2m_ctx *m2m_ctx,
  326. bool buffered)
  327. {
  328. m2m_ctx->cap_q_ctx.buffered = buffered;
  329. }
  330. /**
  331. * v4l2_m2m_ctx_release() - release m2m context
  332. *
  333. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  334. *
  335. * Usually called from driver's release() function.
  336. */
  337. void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx *m2m_ctx);
  338. /**
  339. * v4l2_m2m_buf_queue() - add a buffer to the proper ready buffers list.
  340. *
  341. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  342. * @vbuf: pointer to struct &vb2_v4l2_buffer
  343. *
  344. * Call from videobuf_queue_ops->ops->buf_queue, videobuf_queue_ops callback.
  345. */
  346. void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx *m2m_ctx,
  347. struct vb2_v4l2_buffer *vbuf);
  348. /**
  349. * v4l2_m2m_num_src_bufs_ready() - return the number of source buffers ready for
  350. * use
  351. *
  352. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  353. */
  354. static inline
  355. unsigned int v4l2_m2m_num_src_bufs_ready(struct v4l2_m2m_ctx *m2m_ctx)
  356. {
  357. return m2m_ctx->out_q_ctx.num_rdy;
  358. }
  359. /**
  360. * v4l2_m2m_num_dst_bufs_ready() - return the number of destination buffers
  361. * ready for use
  362. *
  363. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  364. */
  365. static inline
  366. unsigned int v4l2_m2m_num_dst_bufs_ready(struct v4l2_m2m_ctx *m2m_ctx)
  367. {
  368. return m2m_ctx->cap_q_ctx.num_rdy;
  369. }
  370. /**
  371. * v4l2_m2m_next_buf() - return next buffer from the list of ready buffers
  372. *
  373. * @q_ctx: pointer to struct @v4l2_m2m_queue_ctx
  374. */
  375. void *v4l2_m2m_next_buf(struct v4l2_m2m_queue_ctx *q_ctx);
  376. /**
  377. * v4l2_m2m_next_src_buf() - return next source buffer from the list of ready
  378. * buffers
  379. *
  380. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  381. */
  382. static inline void *v4l2_m2m_next_src_buf(struct v4l2_m2m_ctx *m2m_ctx)
  383. {
  384. return v4l2_m2m_next_buf(&m2m_ctx->out_q_ctx);
  385. }
  386. /**
  387. * v4l2_m2m_next_dst_buf() - return next destination buffer from the list of
  388. * ready buffers
  389. *
  390. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  391. */
  392. static inline void *v4l2_m2m_next_dst_buf(struct v4l2_m2m_ctx *m2m_ctx)
  393. {
  394. return v4l2_m2m_next_buf(&m2m_ctx->cap_q_ctx);
  395. }
  396. /**
  397. * v4l2_m2m_get_src_vq() - return vb2_queue for source buffers
  398. *
  399. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  400. */
  401. static inline
  402. struct vb2_queue *v4l2_m2m_get_src_vq(struct v4l2_m2m_ctx *m2m_ctx)
  403. {
  404. return &m2m_ctx->out_q_ctx.q;
  405. }
  406. /**
  407. * v4l2_m2m_get_dst_vq() - return vb2_queue for destination buffers
  408. *
  409. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  410. */
  411. static inline
  412. struct vb2_queue *v4l2_m2m_get_dst_vq(struct v4l2_m2m_ctx *m2m_ctx)
  413. {
  414. return &m2m_ctx->cap_q_ctx.q;
  415. }
  416. /**
  417. * v4l2_m2m_buf_remove() - take off a buffer from the list of ready buffers and
  418. * return it
  419. *
  420. * @q_ctx: pointer to struct @v4l2_m2m_queue_ctx
  421. */
  422. void *v4l2_m2m_buf_remove(struct v4l2_m2m_queue_ctx *q_ctx);
  423. /**
  424. * v4l2_m2m_src_buf_remove() - take off a source buffer from the list of ready
  425. * buffers and return it
  426. *
  427. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  428. */
  429. static inline void *v4l2_m2m_src_buf_remove(struct v4l2_m2m_ctx *m2m_ctx)
  430. {
  431. return v4l2_m2m_buf_remove(&m2m_ctx->out_q_ctx);
  432. }
  433. /**
  434. * v4l2_m2m_dst_buf_remove() - take off a destination buffer from the list of
  435. * ready buffers and return it
  436. *
  437. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  438. */
  439. static inline void *v4l2_m2m_dst_buf_remove(struct v4l2_m2m_ctx *m2m_ctx)
  440. {
  441. return v4l2_m2m_buf_remove(&m2m_ctx->cap_q_ctx);
  442. }
  443. /* v4l2 ioctl helpers */
  444. int v4l2_m2m_ioctl_reqbufs(struct file *file, void *priv,
  445. struct v4l2_requestbuffers *rb);
  446. int v4l2_m2m_ioctl_create_bufs(struct file *file, void *fh,
  447. struct v4l2_create_buffers *create);
  448. int v4l2_m2m_ioctl_querybuf(struct file *file, void *fh,
  449. struct v4l2_buffer *buf);
  450. int v4l2_m2m_ioctl_expbuf(struct file *file, void *fh,
  451. struct v4l2_exportbuffer *eb);
  452. int v4l2_m2m_ioctl_qbuf(struct file *file, void *fh,
  453. struct v4l2_buffer *buf);
  454. int v4l2_m2m_ioctl_dqbuf(struct file *file, void *fh,
  455. struct v4l2_buffer *buf);
  456. int v4l2_m2m_ioctl_prepare_buf(struct file *file, void *fh,
  457. struct v4l2_buffer *buf);
  458. int v4l2_m2m_ioctl_streamon(struct file *file, void *fh,
  459. enum v4l2_buf_type type);
  460. int v4l2_m2m_ioctl_streamoff(struct file *file, void *fh,
  461. enum v4l2_buf_type type);
  462. int v4l2_m2m_fop_mmap(struct file *file, struct vm_area_struct *vma);
  463. unsigned int v4l2_m2m_fop_poll(struct file *file, poll_table *wait);
  464. #endif /* _MEDIA_V4L2_MEM2MEM_H */