v4l2-mem2mem.h 30 KB

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