v4l2-mem2mem.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Memory-to-memory device framework for Video for Linux 2.
  4. *
  5. * Helper functions for devices that use memory buffers for both source
  6. * and destination.
  7. *
  8. * Copyright (c) 2009 Samsung Electronics Co., Ltd.
  9. * Pawel Osciak, <[email protected]>
  10. * Marek Szyprowski, <[email protected]>
  11. */
  12. #ifndef _MEDIA_V4L2_MEM2MEM_H
  13. #define _MEDIA_V4L2_MEM2MEM_H
  14. #include <media/videobuf2-v4l2.h>
  15. /**
  16. * struct v4l2_m2m_ops - mem-to-mem device driver callbacks
  17. * @device_run: required. Begin the actual job (transaction) inside this
  18. * callback.
  19. * The job does NOT have to end before this callback returns
  20. * (and it will be the usual case). When the job finishes,
  21. * v4l2_m2m_job_finish() or v4l2_m2m_buf_done_and_job_finish()
  22. * has to be called.
  23. * @job_ready: optional. Should return 0 if the driver does not have a job
  24. * fully prepared to run yet (i.e. it will not be able to finish a
  25. * transaction without sleeping). If not provided, it will be
  26. * assumed that one source and one destination buffer are all
  27. * that is required for the driver to perform one full transaction.
  28. * This method may not sleep.
  29. * @job_abort: optional. Informs the driver that it has to abort the currently
  30. * running transaction as soon as possible (i.e. as soon as it can
  31. * stop the device safely; e.g. in the next interrupt handler),
  32. * even if the transaction would not have been finished by then.
  33. * After the driver performs the necessary steps, it has to call
  34. * v4l2_m2m_job_finish() or v4l2_m2m_buf_done_and_job_finish() as
  35. * if the transaction ended normally.
  36. * This function does not have to (and will usually not) wait
  37. * until the device enters a state when it can be stopped.
  38. */
  39. struct v4l2_m2m_ops {
  40. void (*device_run)(void *priv);
  41. int (*job_ready)(void *priv);
  42. void (*job_abort)(void *priv);
  43. };
  44. struct video_device;
  45. struct v4l2_m2m_dev;
  46. /**
  47. * struct v4l2_m2m_queue_ctx - represents a queue for buffers ready to be
  48. * processed
  49. *
  50. * @q: pointer to struct &vb2_queue
  51. * @rdy_queue: List of V4L2 mem-to-mem queues
  52. * @rdy_spinlock: spin lock to protect the struct usage
  53. * @num_rdy: number of buffers ready to be processed
  54. * @buffered: is the queue buffered?
  55. *
  56. * Queue for buffers ready to be processed as soon as this
  57. * instance receives access to the device.
  58. */
  59. struct v4l2_m2m_queue_ctx {
  60. struct vb2_queue q;
  61. struct list_head rdy_queue;
  62. spinlock_t rdy_spinlock;
  63. u8 num_rdy;
  64. bool buffered;
  65. };
  66. /**
  67. * struct v4l2_m2m_ctx - Memory to memory context structure
  68. *
  69. * @q_lock: struct &mutex lock
  70. * @new_frame: valid in the device_run callback: if true, then this
  71. * starts a new frame; if false, then this is a new slice
  72. * for an existing frame. This is always true unless
  73. * V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF is set, which
  74. * indicates slicing support.
  75. * @is_draining: indicates device is in draining phase
  76. * @last_src_buf: indicate the last source buffer for draining
  77. * @next_buf_last: next capture queud buffer will be tagged as last
  78. * @has_stopped: indicate the device has been stopped
  79. * @m2m_dev: opaque pointer to the internal data to handle M2M context
  80. * @cap_q_ctx: Capture (output to memory) queue context
  81. * @out_q_ctx: Output (input from memory) queue context
  82. * @queue: List of memory to memory contexts
  83. * @job_flags: Job queue flags, used internally by v4l2-mem2mem.c:
  84. * %TRANS_QUEUED, %TRANS_RUNNING and %TRANS_ABORT.
  85. * @finished: Wait queue used to signalize when a job queue finished.
  86. * @priv: Instance private data
  87. *
  88. * The memory to memory context is specific to a file handle, NOT to e.g.
  89. * a device.
  90. */
  91. struct v4l2_m2m_ctx {
  92. /* optional cap/out vb2 queues lock */
  93. struct mutex *q_lock;
  94. bool new_frame;
  95. bool is_draining;
  96. struct vb2_v4l2_buffer *last_src_buf;
  97. bool next_buf_last;
  98. bool has_stopped;
  99. /* internal use only */
  100. struct v4l2_m2m_dev *m2m_dev;
  101. struct v4l2_m2m_queue_ctx cap_q_ctx;
  102. struct v4l2_m2m_queue_ctx out_q_ctx;
  103. /* For device job queue */
  104. struct list_head queue;
  105. unsigned long job_flags;
  106. wait_queue_head_t finished;
  107. void *priv;
  108. };
  109. /**
  110. * struct v4l2_m2m_buffer - Memory to memory buffer
  111. *
  112. * @vb: pointer to struct &vb2_v4l2_buffer
  113. * @list: list of m2m buffers
  114. */
  115. struct v4l2_m2m_buffer {
  116. struct vb2_v4l2_buffer vb;
  117. struct list_head list;
  118. };
  119. /**
  120. * v4l2_m2m_get_curr_priv() - return driver private data for the currently
  121. * running instance or NULL if no instance is running
  122. *
  123. * @m2m_dev: opaque pointer to the internal data to handle M2M context
  124. */
  125. void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev *m2m_dev);
  126. /**
  127. * v4l2_m2m_get_vq() - return vb2_queue for the given type
  128. *
  129. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  130. * @type: type of the V4L2 buffer, as defined by enum &v4l2_buf_type
  131. */
  132. struct vb2_queue *v4l2_m2m_get_vq(struct v4l2_m2m_ctx *m2m_ctx,
  133. enum v4l2_buf_type type);
  134. /**
  135. * v4l2_m2m_try_schedule() - check whether an instance is ready to be added to
  136. * the pending job queue and add it if so.
  137. *
  138. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  139. *
  140. * There are three basic requirements an instance has to meet to be able to run:
  141. * 1) at least one source buffer has to be queued,
  142. * 2) at least one destination buffer has to be queued,
  143. * 3) streaming has to be on.
  144. *
  145. * If a queue is buffered (for example a decoder hardware ringbuffer that has
  146. * to be drained before doing streamoff), allow scheduling without v4l2 buffers
  147. * on that queue.
  148. *
  149. * There may also be additional, custom requirements. In such case the driver
  150. * should supply a custom callback (job_ready in v4l2_m2m_ops) that should
  151. * return 1 if the instance is ready.
  152. * An example of the above could be an instance that requires more than one
  153. * src/dst buffer per transaction.
  154. */
  155. void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx);
  156. /**
  157. * v4l2_m2m_job_finish() - inform the framework that a job has been finished
  158. * and have it clean up
  159. *
  160. * @m2m_dev: opaque pointer to the internal data to handle M2M context
  161. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  162. *
  163. * Called by a driver to yield back the device after it has finished with it.
  164. * Should be called as soon as possible after reaching a state which allows
  165. * other instances to take control of the device.
  166. *
  167. * This function has to be called only after &v4l2_m2m_ops->device_run
  168. * callback has been called on the driver. To prevent recursion, it should
  169. * not be called directly from the &v4l2_m2m_ops->device_run callback though.
  170. */
  171. void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev,
  172. struct v4l2_m2m_ctx *m2m_ctx);
  173. /**
  174. * v4l2_m2m_buf_done_and_job_finish() - return source/destination buffers with
  175. * state and inform the framework that a job has been finished and have it
  176. * clean up
  177. *
  178. * @m2m_dev: opaque pointer to the internal data to handle M2M context
  179. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  180. * @state: vb2 buffer state passed to v4l2_m2m_buf_done().
  181. *
  182. * Drivers that set V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF must use this
  183. * function instead of job_finish() to take held buffers into account. It is
  184. * optional for other drivers.
  185. *
  186. * This function removes the source buffer from the ready list and returns
  187. * it with the given state. The same is done for the destination buffer, unless
  188. * it is marked 'held'. In that case the buffer is kept on the ready list.
  189. *
  190. * After that the job is finished (see job_finish()).
  191. *
  192. * This allows for multiple output buffers to be used to fill in a single
  193. * capture buffer. This is typically used by stateless decoders where
  194. * multiple e.g. H.264 slices contribute to a single decoded frame.
  195. */
  196. void v4l2_m2m_buf_done_and_job_finish(struct v4l2_m2m_dev *m2m_dev,
  197. struct v4l2_m2m_ctx *m2m_ctx,
  198. enum vb2_buffer_state state);
  199. static inline void
  200. v4l2_m2m_buf_done(struct vb2_v4l2_buffer *buf, enum vb2_buffer_state state)
  201. {
  202. vb2_buffer_done(&buf->vb2_buf, state);
  203. }
  204. /**
  205. * v4l2_m2m_clear_state() - clear encoding/decoding state
  206. *
  207. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  208. */
  209. static inline void
  210. v4l2_m2m_clear_state(struct v4l2_m2m_ctx *m2m_ctx)
  211. {
  212. m2m_ctx->next_buf_last = false;
  213. m2m_ctx->is_draining = false;
  214. m2m_ctx->has_stopped = false;
  215. }
  216. /**
  217. * v4l2_m2m_mark_stopped() - set current encoding/decoding state as stopped
  218. *
  219. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  220. */
  221. static inline void
  222. v4l2_m2m_mark_stopped(struct v4l2_m2m_ctx *m2m_ctx)
  223. {
  224. m2m_ctx->next_buf_last = false;
  225. m2m_ctx->is_draining = false;
  226. m2m_ctx->has_stopped = true;
  227. }
  228. /**
  229. * v4l2_m2m_dst_buf_is_last() - return the current encoding/decoding session
  230. * draining management state of next queued capture buffer
  231. *
  232. * This last capture buffer should be tagged with V4L2_BUF_FLAG_LAST to notify
  233. * the end of the capture session.
  234. *
  235. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  236. */
  237. static inline bool
  238. v4l2_m2m_dst_buf_is_last(struct v4l2_m2m_ctx *m2m_ctx)
  239. {
  240. return m2m_ctx->is_draining && m2m_ctx->next_buf_last;
  241. }
  242. /**
  243. * v4l2_m2m_has_stopped() - return the current encoding/decoding session
  244. * stopped state
  245. *
  246. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  247. */
  248. static inline bool
  249. v4l2_m2m_has_stopped(struct v4l2_m2m_ctx *m2m_ctx)
  250. {
  251. return m2m_ctx->has_stopped;
  252. }
  253. /**
  254. * v4l2_m2m_is_last_draining_src_buf() - return the output buffer draining
  255. * state in the current encoding/decoding session
  256. *
  257. * This will identify the last output buffer queued before a session stop
  258. * was required, leading to an actual encoding/decoding session stop state
  259. * in the encoding/decoding process after being processed.
  260. *
  261. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  262. * @vbuf: pointer to struct &v4l2_buffer
  263. */
  264. static inline bool
  265. v4l2_m2m_is_last_draining_src_buf(struct v4l2_m2m_ctx *m2m_ctx,
  266. struct vb2_v4l2_buffer *vbuf)
  267. {
  268. return m2m_ctx->is_draining && vbuf == m2m_ctx->last_src_buf;
  269. }
  270. /**
  271. * v4l2_m2m_last_buffer_done() - marks the buffer with LAST flag and DONE
  272. *
  273. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  274. * @vbuf: pointer to struct &v4l2_buffer
  275. */
  276. void v4l2_m2m_last_buffer_done(struct v4l2_m2m_ctx *m2m_ctx,
  277. struct vb2_v4l2_buffer *vbuf);
  278. /**
  279. * v4l2_m2m_suspend() - stop new jobs from being run and wait for current job
  280. * to finish
  281. *
  282. * @m2m_dev: opaque pointer to the internal data to handle M2M context
  283. *
  284. * Called by a driver in the suspend hook. Stop new jobs from being run, and
  285. * wait for current running job to finish.
  286. */
  287. void v4l2_m2m_suspend(struct v4l2_m2m_dev *m2m_dev);
  288. /**
  289. * v4l2_m2m_resume() - resume job running and try to run a queued job
  290. *
  291. * @m2m_dev: opaque pointer to the internal data to handle M2M context
  292. *
  293. * Called by a driver in the resume hook. This reverts the operation of
  294. * v4l2_m2m_suspend() and allows job to be run. Also try to run a queued job if
  295. * there is any.
  296. */
  297. void v4l2_m2m_resume(struct v4l2_m2m_dev *m2m_dev);
  298. /**
  299. * v4l2_m2m_reqbufs() - multi-queue-aware REQBUFS multiplexer
  300. *
  301. * @file: pointer to struct &file
  302. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  303. * @reqbufs: pointer to struct &v4l2_requestbuffers
  304. */
  305. int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  306. struct v4l2_requestbuffers *reqbufs);
  307. /**
  308. * v4l2_m2m_querybuf() - multi-queue-aware QUERYBUF multiplexer
  309. *
  310. * @file: pointer to struct &file
  311. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  312. * @buf: pointer to struct &v4l2_buffer
  313. *
  314. * See v4l2_m2m_mmap() documentation for details.
  315. */
  316. int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  317. struct v4l2_buffer *buf);
  318. /**
  319. * v4l2_m2m_qbuf() - enqueue a source or destination buffer, depending on
  320. * the type
  321. *
  322. * @file: pointer to struct &file
  323. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  324. * @buf: pointer to struct &v4l2_buffer
  325. */
  326. int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  327. struct v4l2_buffer *buf);
  328. /**
  329. * v4l2_m2m_dqbuf() - dequeue a source or destination buffer, depending on
  330. * the type
  331. *
  332. * @file: pointer to struct &file
  333. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  334. * @buf: pointer to struct &v4l2_buffer
  335. */
  336. int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  337. struct v4l2_buffer *buf);
  338. /**
  339. * v4l2_m2m_prepare_buf() - prepare a source or destination buffer, depending on
  340. * the type
  341. *
  342. * @file: pointer to struct &file
  343. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  344. * @buf: pointer to struct &v4l2_buffer
  345. */
  346. int v4l2_m2m_prepare_buf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  347. struct v4l2_buffer *buf);
  348. /**
  349. * v4l2_m2m_create_bufs() - create a source or destination buffer, depending
  350. * on the type
  351. *
  352. * @file: pointer to struct &file
  353. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  354. * @create: pointer to struct &v4l2_create_buffers
  355. */
  356. int v4l2_m2m_create_bufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  357. struct v4l2_create_buffers *create);
  358. /**
  359. * v4l2_m2m_expbuf() - export a source or destination buffer, depending on
  360. * the type
  361. *
  362. * @file: pointer to struct &file
  363. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  364. * @eb: pointer to struct &v4l2_exportbuffer
  365. */
  366. int v4l2_m2m_expbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  367. struct v4l2_exportbuffer *eb);
  368. /**
  369. * v4l2_m2m_streamon() - turn on streaming for a video queue
  370. *
  371. * @file: pointer to struct &file
  372. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  373. * @type: type of the V4L2 buffer, as defined by enum &v4l2_buf_type
  374. */
  375. int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  376. enum v4l2_buf_type type);
  377. /**
  378. * v4l2_m2m_streamoff() - turn off streaming for a video queue
  379. *
  380. * @file: pointer to struct &file
  381. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  382. * @type: type of the V4L2 buffer, as defined by enum &v4l2_buf_type
  383. */
  384. int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  385. enum v4l2_buf_type type);
  386. /**
  387. * v4l2_m2m_update_start_streaming_state() - update the encoding/decoding
  388. * session state when a start of streaming of a video queue is requested
  389. *
  390. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  391. * @q: queue
  392. */
  393. void v4l2_m2m_update_start_streaming_state(struct v4l2_m2m_ctx *m2m_ctx,
  394. struct vb2_queue *q);
  395. /**
  396. * v4l2_m2m_update_stop_streaming_state() - update the encoding/decoding
  397. * session state when a stop of streaming of a video queue is requested
  398. *
  399. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  400. * @q: queue
  401. */
  402. void v4l2_m2m_update_stop_streaming_state(struct v4l2_m2m_ctx *m2m_ctx,
  403. struct vb2_queue *q);
  404. /**
  405. * v4l2_m2m_encoder_cmd() - execute an encoder command
  406. *
  407. * @file: pointer to struct &file
  408. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  409. * @ec: pointer to the encoder command
  410. */
  411. int v4l2_m2m_encoder_cmd(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  412. struct v4l2_encoder_cmd *ec);
  413. /**
  414. * v4l2_m2m_decoder_cmd() - execute a decoder command
  415. *
  416. * @file: pointer to struct &file
  417. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  418. * @dc: pointer to the decoder command
  419. */
  420. int v4l2_m2m_decoder_cmd(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  421. struct v4l2_decoder_cmd *dc);
  422. /**
  423. * v4l2_m2m_poll() - poll replacement, for destination buffers only
  424. *
  425. * @file: pointer to struct &file
  426. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  427. * @wait: pointer to struct &poll_table_struct
  428. *
  429. * Call from the driver's poll() function. Will poll both queues. If a buffer
  430. * is available to dequeue (with dqbuf) from the source queue, this will
  431. * indicate that a non-blocking write can be performed, while read will be
  432. * returned in case of the destination queue.
  433. */
  434. __poll_t v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  435. struct poll_table_struct *wait);
  436. /**
  437. * v4l2_m2m_mmap() - source and destination queues-aware mmap multiplexer
  438. *
  439. * @file: pointer to struct &file
  440. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  441. * @vma: pointer to struct &vm_area_struct
  442. *
  443. * Call from driver's mmap() function. Will handle mmap() for both queues
  444. * seamlessly for the video buffer, which will receive normal per-queue offsets
  445. * and proper vb2 queue pointers. The differentiation is made outside
  446. * vb2 by adding a predefined offset to buffers from one of the queues
  447. * and subtracting it before passing it back to vb2. Only drivers (and
  448. * thus applications) receive modified offsets.
  449. */
  450. int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  451. struct vm_area_struct *vma);
  452. #ifndef CONFIG_MMU
  453. unsigned long v4l2_m2m_get_unmapped_area(struct file *file, unsigned long addr,
  454. unsigned long len, unsigned long pgoff,
  455. unsigned long flags);
  456. #endif
  457. /**
  458. * v4l2_m2m_init() - initialize per-driver m2m data
  459. *
  460. * @m2m_ops: pointer to struct v4l2_m2m_ops
  461. *
  462. * Usually called from driver's ``probe()`` function.
  463. *
  464. * Return: returns an opaque pointer to the internal data to handle M2M context
  465. */
  466. struct v4l2_m2m_dev *v4l2_m2m_init(const struct v4l2_m2m_ops *m2m_ops);
  467. #if defined(CONFIG_MEDIA_CONTROLLER)
  468. void v4l2_m2m_unregister_media_controller(struct v4l2_m2m_dev *m2m_dev);
  469. int v4l2_m2m_register_media_controller(struct v4l2_m2m_dev *m2m_dev,
  470. struct video_device *vdev, int function);
  471. #else
  472. static inline void
  473. v4l2_m2m_unregister_media_controller(struct v4l2_m2m_dev *m2m_dev)
  474. {
  475. }
  476. static inline int
  477. v4l2_m2m_register_media_controller(struct v4l2_m2m_dev *m2m_dev,
  478. struct video_device *vdev, int function)
  479. {
  480. return 0;
  481. }
  482. #endif
  483. /**
  484. * v4l2_m2m_release() - cleans up and frees a m2m_dev structure
  485. *
  486. * @m2m_dev: opaque pointer to the internal data to handle M2M context
  487. *
  488. * Usually called from driver's ``remove()`` function.
  489. */
  490. void v4l2_m2m_release(struct v4l2_m2m_dev *m2m_dev);
  491. /**
  492. * v4l2_m2m_ctx_init() - allocate and initialize a m2m context
  493. *
  494. * @m2m_dev: opaque pointer to the internal data to handle M2M context
  495. * @drv_priv: driver's instance private data
  496. * @queue_init: a callback for queue type-specific initialization function
  497. * to be used for initializing vb2_queues
  498. *
  499. * Usually called from driver's ``open()`` function.
  500. */
  501. struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev,
  502. void *drv_priv,
  503. int (*queue_init)(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq));
  504. static inline void v4l2_m2m_set_src_buffered(struct v4l2_m2m_ctx *m2m_ctx,
  505. bool buffered)
  506. {
  507. m2m_ctx->out_q_ctx.buffered = buffered;
  508. }
  509. static inline void v4l2_m2m_set_dst_buffered(struct v4l2_m2m_ctx *m2m_ctx,
  510. bool buffered)
  511. {
  512. m2m_ctx->cap_q_ctx.buffered = buffered;
  513. }
  514. /**
  515. * v4l2_m2m_ctx_release() - release m2m context
  516. *
  517. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  518. *
  519. * Usually called from driver's release() function.
  520. */
  521. void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx *m2m_ctx);
  522. /**
  523. * v4l2_m2m_buf_queue() - add a buffer to the proper ready buffers list.
  524. *
  525. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  526. * @vbuf: pointer to struct &vb2_v4l2_buffer
  527. *
  528. * Call from vb2_queue_ops->ops->buf_queue, vb2_queue_ops callback.
  529. */
  530. void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx *m2m_ctx,
  531. struct vb2_v4l2_buffer *vbuf);
  532. /**
  533. * v4l2_m2m_num_src_bufs_ready() - return the number of source buffers ready for
  534. * use
  535. *
  536. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  537. */
  538. static inline
  539. unsigned int v4l2_m2m_num_src_bufs_ready(struct v4l2_m2m_ctx *m2m_ctx)
  540. {
  541. unsigned int num_buf_rdy;
  542. unsigned long flags;
  543. spin_lock_irqsave(&m2m_ctx->out_q_ctx.rdy_spinlock, flags);
  544. num_buf_rdy = m2m_ctx->out_q_ctx.num_rdy;
  545. spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock, flags);
  546. return num_buf_rdy;
  547. }
  548. /**
  549. * v4l2_m2m_num_dst_bufs_ready() - return the number of destination buffers
  550. * ready for use
  551. *
  552. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  553. */
  554. static inline
  555. unsigned int v4l2_m2m_num_dst_bufs_ready(struct v4l2_m2m_ctx *m2m_ctx)
  556. {
  557. unsigned int num_buf_rdy;
  558. unsigned long flags;
  559. spin_lock_irqsave(&m2m_ctx->cap_q_ctx.rdy_spinlock, flags);
  560. num_buf_rdy = m2m_ctx->cap_q_ctx.num_rdy;
  561. spin_unlock_irqrestore(&m2m_ctx->cap_q_ctx.rdy_spinlock, flags);
  562. return num_buf_rdy;
  563. }
  564. /**
  565. * v4l2_m2m_next_buf() - return next buffer from the list of ready buffers
  566. *
  567. * @q_ctx: pointer to struct @v4l2_m2m_queue_ctx
  568. */
  569. struct vb2_v4l2_buffer *v4l2_m2m_next_buf(struct v4l2_m2m_queue_ctx *q_ctx);
  570. /**
  571. * v4l2_m2m_next_src_buf() - return next source buffer from the list of ready
  572. * buffers
  573. *
  574. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  575. */
  576. static inline struct vb2_v4l2_buffer *
  577. v4l2_m2m_next_src_buf(struct v4l2_m2m_ctx *m2m_ctx)
  578. {
  579. return v4l2_m2m_next_buf(&m2m_ctx->out_q_ctx);
  580. }
  581. /**
  582. * v4l2_m2m_next_dst_buf() - return next destination buffer from the list of
  583. * ready buffers
  584. *
  585. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  586. */
  587. static inline struct vb2_v4l2_buffer *
  588. v4l2_m2m_next_dst_buf(struct v4l2_m2m_ctx *m2m_ctx)
  589. {
  590. return v4l2_m2m_next_buf(&m2m_ctx->cap_q_ctx);
  591. }
  592. /**
  593. * v4l2_m2m_last_buf() - return last buffer from the list of ready buffers
  594. *
  595. * @q_ctx: pointer to struct @v4l2_m2m_queue_ctx
  596. */
  597. struct vb2_v4l2_buffer *v4l2_m2m_last_buf(struct v4l2_m2m_queue_ctx *q_ctx);
  598. /**
  599. * v4l2_m2m_last_src_buf() - return last destination buffer from the list of
  600. * ready buffers
  601. *
  602. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  603. */
  604. static inline struct vb2_v4l2_buffer *
  605. v4l2_m2m_last_src_buf(struct v4l2_m2m_ctx *m2m_ctx)
  606. {
  607. return v4l2_m2m_last_buf(&m2m_ctx->out_q_ctx);
  608. }
  609. /**
  610. * v4l2_m2m_last_dst_buf() - return last destination buffer from the list of
  611. * ready buffers
  612. *
  613. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  614. */
  615. static inline struct vb2_v4l2_buffer *
  616. v4l2_m2m_last_dst_buf(struct v4l2_m2m_ctx *m2m_ctx)
  617. {
  618. return v4l2_m2m_last_buf(&m2m_ctx->cap_q_ctx);
  619. }
  620. /**
  621. * v4l2_m2m_for_each_dst_buf() - iterate over a list of destination ready
  622. * buffers
  623. *
  624. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  625. * @b: current buffer of type struct v4l2_m2m_buffer
  626. */
  627. #define v4l2_m2m_for_each_dst_buf(m2m_ctx, b) \
  628. list_for_each_entry(b, &m2m_ctx->cap_q_ctx.rdy_queue, list)
  629. /**
  630. * v4l2_m2m_for_each_src_buf() - iterate over a list of source ready buffers
  631. *
  632. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  633. * @b: current buffer of type struct v4l2_m2m_buffer
  634. */
  635. #define v4l2_m2m_for_each_src_buf(m2m_ctx, b) \
  636. list_for_each_entry(b, &m2m_ctx->out_q_ctx.rdy_queue, list)
  637. /**
  638. * v4l2_m2m_for_each_dst_buf_safe() - iterate over a list of destination ready
  639. * buffers safely
  640. *
  641. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  642. * @b: current buffer of type struct v4l2_m2m_buffer
  643. * @n: used as temporary storage
  644. */
  645. #define v4l2_m2m_for_each_dst_buf_safe(m2m_ctx, b, n) \
  646. list_for_each_entry_safe(b, n, &m2m_ctx->cap_q_ctx.rdy_queue, list)
  647. /**
  648. * v4l2_m2m_for_each_src_buf_safe() - iterate over a list of source ready
  649. * buffers safely
  650. *
  651. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  652. * @b: current buffer of type struct v4l2_m2m_buffer
  653. * @n: used as temporary storage
  654. */
  655. #define v4l2_m2m_for_each_src_buf_safe(m2m_ctx, b, n) \
  656. list_for_each_entry_safe(b, n, &m2m_ctx->out_q_ctx.rdy_queue, list)
  657. /**
  658. * v4l2_m2m_get_src_vq() - return vb2_queue for source buffers
  659. *
  660. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  661. */
  662. static inline
  663. struct vb2_queue *v4l2_m2m_get_src_vq(struct v4l2_m2m_ctx *m2m_ctx)
  664. {
  665. return &m2m_ctx->out_q_ctx.q;
  666. }
  667. /**
  668. * v4l2_m2m_get_dst_vq() - return vb2_queue for destination buffers
  669. *
  670. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  671. */
  672. static inline
  673. struct vb2_queue *v4l2_m2m_get_dst_vq(struct v4l2_m2m_ctx *m2m_ctx)
  674. {
  675. return &m2m_ctx->cap_q_ctx.q;
  676. }
  677. /**
  678. * v4l2_m2m_buf_remove() - take off a buffer from the list of ready buffers and
  679. * return it
  680. *
  681. * @q_ctx: pointer to struct @v4l2_m2m_queue_ctx
  682. */
  683. struct vb2_v4l2_buffer *v4l2_m2m_buf_remove(struct v4l2_m2m_queue_ctx *q_ctx);
  684. /**
  685. * v4l2_m2m_src_buf_remove() - take off a source buffer from the list of ready
  686. * buffers and return it
  687. *
  688. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  689. */
  690. static inline struct vb2_v4l2_buffer *
  691. v4l2_m2m_src_buf_remove(struct v4l2_m2m_ctx *m2m_ctx)
  692. {
  693. return v4l2_m2m_buf_remove(&m2m_ctx->out_q_ctx);
  694. }
  695. /**
  696. * v4l2_m2m_dst_buf_remove() - take off a destination buffer from the list of
  697. * ready buffers and return it
  698. *
  699. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  700. */
  701. static inline struct vb2_v4l2_buffer *
  702. v4l2_m2m_dst_buf_remove(struct v4l2_m2m_ctx *m2m_ctx)
  703. {
  704. return v4l2_m2m_buf_remove(&m2m_ctx->cap_q_ctx);
  705. }
  706. /**
  707. * v4l2_m2m_buf_remove_by_buf() - take off exact buffer from the list of ready
  708. * buffers
  709. *
  710. * @q_ctx: pointer to struct @v4l2_m2m_queue_ctx
  711. * @vbuf: the buffer to be removed
  712. */
  713. void v4l2_m2m_buf_remove_by_buf(struct v4l2_m2m_queue_ctx *q_ctx,
  714. struct vb2_v4l2_buffer *vbuf);
  715. /**
  716. * v4l2_m2m_src_buf_remove_by_buf() - take off exact source buffer from the list
  717. * of ready buffers
  718. *
  719. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  720. * @vbuf: the buffer to be removed
  721. */
  722. static inline void v4l2_m2m_src_buf_remove_by_buf(struct v4l2_m2m_ctx *m2m_ctx,
  723. struct vb2_v4l2_buffer *vbuf)
  724. {
  725. v4l2_m2m_buf_remove_by_buf(&m2m_ctx->out_q_ctx, vbuf);
  726. }
  727. /**
  728. * v4l2_m2m_dst_buf_remove_by_buf() - take off exact destination buffer from the
  729. * list of ready buffers
  730. *
  731. * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
  732. * @vbuf: the buffer to be removed
  733. */
  734. static inline void v4l2_m2m_dst_buf_remove_by_buf(struct v4l2_m2m_ctx *m2m_ctx,
  735. struct vb2_v4l2_buffer *vbuf)
  736. {
  737. v4l2_m2m_buf_remove_by_buf(&m2m_ctx->cap_q_ctx, vbuf);
  738. }
  739. struct vb2_v4l2_buffer *
  740. v4l2_m2m_buf_remove_by_idx(struct v4l2_m2m_queue_ctx *q_ctx, unsigned int idx);
  741. static inline struct vb2_v4l2_buffer *
  742. v4l2_m2m_src_buf_remove_by_idx(struct v4l2_m2m_ctx *m2m_ctx, unsigned int idx)
  743. {
  744. return v4l2_m2m_buf_remove_by_idx(&m2m_ctx->out_q_ctx, idx);
  745. }
  746. static inline struct vb2_v4l2_buffer *
  747. v4l2_m2m_dst_buf_remove_by_idx(struct v4l2_m2m_ctx *m2m_ctx, unsigned int idx)
  748. {
  749. return v4l2_m2m_buf_remove_by_idx(&m2m_ctx->cap_q_ctx, idx);
  750. }
  751. /**
  752. * v4l2_m2m_buf_copy_metadata() - copy buffer metadata from
  753. * the output buffer to the capture buffer
  754. *
  755. * @out_vb: the output buffer that is the source of the metadata.
  756. * @cap_vb: the capture buffer that will receive the metadata.
  757. * @copy_frame_flags: copy the KEY/B/PFRAME flags as well.
  758. *
  759. * This helper function copies the timestamp, timecode (if the TIMECODE
  760. * buffer flag was set), field and the TIMECODE, KEYFRAME, BFRAME, PFRAME
  761. * and TSTAMP_SRC_MASK flags from @out_vb to @cap_vb.
  762. *
  763. * If @copy_frame_flags is false, then the KEYFRAME, BFRAME and PFRAME
  764. * flags are not copied. This is typically needed for encoders that
  765. * set this bits explicitly.
  766. */
  767. void v4l2_m2m_buf_copy_metadata(const struct vb2_v4l2_buffer *out_vb,
  768. struct vb2_v4l2_buffer *cap_vb,
  769. bool copy_frame_flags);
  770. /* v4l2 request helper */
  771. void v4l2_m2m_request_queue(struct media_request *req);
  772. /* v4l2 ioctl helpers */
  773. int v4l2_m2m_ioctl_reqbufs(struct file *file, void *priv,
  774. struct v4l2_requestbuffers *rb);
  775. int v4l2_m2m_ioctl_create_bufs(struct file *file, void *fh,
  776. struct v4l2_create_buffers *create);
  777. int v4l2_m2m_ioctl_querybuf(struct file *file, void *fh,
  778. struct v4l2_buffer *buf);
  779. int v4l2_m2m_ioctl_expbuf(struct file *file, void *fh,
  780. struct v4l2_exportbuffer *eb);
  781. int v4l2_m2m_ioctl_qbuf(struct file *file, void *fh,
  782. struct v4l2_buffer *buf);
  783. int v4l2_m2m_ioctl_dqbuf(struct file *file, void *fh,
  784. struct v4l2_buffer *buf);
  785. int v4l2_m2m_ioctl_prepare_buf(struct file *file, void *fh,
  786. struct v4l2_buffer *buf);
  787. int v4l2_m2m_ioctl_streamon(struct file *file, void *fh,
  788. enum v4l2_buf_type type);
  789. int v4l2_m2m_ioctl_streamoff(struct file *file, void *fh,
  790. enum v4l2_buf_type type);
  791. int v4l2_m2m_ioctl_encoder_cmd(struct file *file, void *fh,
  792. struct v4l2_encoder_cmd *ec);
  793. int v4l2_m2m_ioctl_decoder_cmd(struct file *file, void *fh,
  794. struct v4l2_decoder_cmd *dc);
  795. int v4l2_m2m_ioctl_try_encoder_cmd(struct file *file, void *fh,
  796. struct v4l2_encoder_cmd *ec);
  797. int v4l2_m2m_ioctl_try_decoder_cmd(struct file *file, void *fh,
  798. struct v4l2_decoder_cmd *dc);
  799. int v4l2_m2m_ioctl_stateless_try_decoder_cmd(struct file *file, void *fh,
  800. struct v4l2_decoder_cmd *dc);
  801. int v4l2_m2m_ioctl_stateless_decoder_cmd(struct file *file, void *priv,
  802. struct v4l2_decoder_cmd *dc);
  803. int v4l2_m2m_fop_mmap(struct file *file, struct vm_area_struct *vma);
  804. __poll_t v4l2_m2m_fop_poll(struct file *file, poll_table *wait);
  805. #endif /* _MEDIA_V4L2_MEM2MEM_H */