v4l2-mem2mem.h 29 KB

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