v4l2-mem2mem.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /*
  2. * Memory-to-memory device framework for Video for Linux 2 and videobuf.
  3. *
  4. * Helper functions for devices that use videobuf buffers for both their
  5. * source and destination.
  6. *
  7. * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
  8. * Pawel Osciak, <pawel@osciak.com>
  9. * Marek Szyprowski, <m.szyprowski@samsung.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/sched.h>
  18. #include <linux/slab.h>
  19. #include <media/videobuf2-core.h>
  20. #include <media/v4l2-mem2mem.h>
  21. MODULE_DESCRIPTION("Mem to mem device framework for videobuf");
  22. MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
  23. MODULE_LICENSE("GPL");
  24. static bool debug;
  25. module_param(debug, bool, 0644);
  26. #define dprintk(fmt, arg...) \
  27. do { \
  28. if (debug) \
  29. printk(KERN_DEBUG "%s: " fmt, __func__, ## arg);\
  30. } while (0)
  31. /* Instance is already queued on the job_queue */
  32. #define TRANS_QUEUED (1 << 0)
  33. /* Instance is currently running in hardware */
  34. #define TRANS_RUNNING (1 << 1)
  35. /* Offset base for buffers on the destination queue - used to distinguish
  36. * between source and destination buffers when mmapping - they receive the same
  37. * offsets but for different queues */
  38. #define DST_QUEUE_OFF_BASE (1 << 30)
  39. /**
  40. * struct v4l2_m2m_dev - per-device context
  41. * @curr_ctx: currently running instance
  42. * @job_queue: instances queued to run
  43. * @job_spinlock: protects job_queue
  44. * @m2m_ops: driver callbacks
  45. */
  46. struct v4l2_m2m_dev {
  47. struct v4l2_m2m_ctx *curr_ctx;
  48. struct list_head job_queue;
  49. spinlock_t job_spinlock;
  50. struct v4l2_m2m_ops *m2m_ops;
  51. };
  52. static struct v4l2_m2m_queue_ctx *get_queue_ctx(struct v4l2_m2m_ctx *m2m_ctx,
  53. enum v4l2_buf_type type)
  54. {
  55. if (V4L2_TYPE_IS_OUTPUT(type))
  56. return &m2m_ctx->out_q_ctx;
  57. else
  58. return &m2m_ctx->cap_q_ctx;
  59. }
  60. /**
  61. * v4l2_m2m_get_vq() - return vb2_queue for the given type
  62. */
  63. struct vb2_queue *v4l2_m2m_get_vq(struct v4l2_m2m_ctx *m2m_ctx,
  64. enum v4l2_buf_type type)
  65. {
  66. struct v4l2_m2m_queue_ctx *q_ctx;
  67. q_ctx = get_queue_ctx(m2m_ctx, type);
  68. if (!q_ctx)
  69. return NULL;
  70. return &q_ctx->q;
  71. }
  72. EXPORT_SYMBOL(v4l2_m2m_get_vq);
  73. /**
  74. * v4l2_m2m_next_buf() - return next buffer from the list of ready buffers
  75. */
  76. void *v4l2_m2m_next_buf(struct v4l2_m2m_queue_ctx *q_ctx)
  77. {
  78. struct v4l2_m2m_buffer *b = NULL;
  79. unsigned long flags;
  80. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  81. if (list_empty(&q_ctx->rdy_queue))
  82. goto end;
  83. b = list_entry(q_ctx->rdy_queue.next, struct v4l2_m2m_buffer, list);
  84. end:
  85. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  86. return &b->vb;
  87. }
  88. EXPORT_SYMBOL_GPL(v4l2_m2m_next_buf);
  89. /**
  90. * v4l2_m2m_buf_remove() - take off a buffer from the list of ready buffers and
  91. * return it
  92. */
  93. void *v4l2_m2m_buf_remove(struct v4l2_m2m_queue_ctx *q_ctx)
  94. {
  95. struct v4l2_m2m_buffer *b = NULL;
  96. unsigned long flags;
  97. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  98. if (!list_empty(&q_ctx->rdy_queue)) {
  99. b = list_entry(q_ctx->rdy_queue.next, struct v4l2_m2m_buffer,
  100. list);
  101. list_del(&b->list);
  102. q_ctx->num_rdy--;
  103. }
  104. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  105. return &b->vb;
  106. }
  107. EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove);
  108. /*
  109. * Scheduling handlers
  110. */
  111. /**
  112. * v4l2_m2m_get_curr_priv() - return driver private data for the currently
  113. * running instance or NULL if no instance is running
  114. */
  115. void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev *m2m_dev)
  116. {
  117. unsigned long flags;
  118. void *ret = NULL;
  119. spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
  120. if (m2m_dev->curr_ctx)
  121. ret = m2m_dev->curr_ctx->priv;
  122. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  123. return ret;
  124. }
  125. EXPORT_SYMBOL(v4l2_m2m_get_curr_priv);
  126. /**
  127. * v4l2_m2m_try_run() - select next job to perform and run it if possible
  128. *
  129. * Get next transaction (if present) from the waiting jobs list and run it.
  130. */
  131. static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev)
  132. {
  133. unsigned long flags;
  134. spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
  135. if (NULL != m2m_dev->curr_ctx) {
  136. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  137. dprintk("Another instance is running, won't run now\n");
  138. return;
  139. }
  140. if (list_empty(&m2m_dev->job_queue)) {
  141. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  142. dprintk("No job pending\n");
  143. return;
  144. }
  145. m2m_dev->curr_ctx = list_entry(m2m_dev->job_queue.next,
  146. struct v4l2_m2m_ctx, queue);
  147. m2m_dev->curr_ctx->job_flags |= TRANS_RUNNING;
  148. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  149. m2m_dev->m2m_ops->device_run(m2m_dev->curr_ctx->priv);
  150. }
  151. /**
  152. * v4l2_m2m_try_schedule() - check whether an instance is ready to be added to
  153. * the pending job queue and add it if so.
  154. * @m2m_ctx: m2m context assigned to the instance to be checked
  155. *
  156. * There are three basic requirements an instance has to meet to be able to run:
  157. * 1) at least one source buffer has to be queued,
  158. * 2) at least one destination buffer has to be queued,
  159. * 3) streaming has to be on.
  160. *
  161. * There may also be additional, custom requirements. In such case the driver
  162. * should supply a custom callback (job_ready in v4l2_m2m_ops) that should
  163. * return 1 if the instance is ready.
  164. * An example of the above could be an instance that requires more than one
  165. * src/dst buffer per transaction.
  166. */
  167. static void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx)
  168. {
  169. struct v4l2_m2m_dev *m2m_dev;
  170. unsigned long flags_job, flags;
  171. m2m_dev = m2m_ctx->m2m_dev;
  172. dprintk("Trying to schedule a job for m2m_ctx: %p\n", m2m_ctx);
  173. if (!m2m_ctx->out_q_ctx.q.streaming
  174. || !m2m_ctx->cap_q_ctx.q.streaming) {
  175. dprintk("Streaming needs to be on for both queues\n");
  176. return;
  177. }
  178. spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job);
  179. if (m2m_ctx->job_flags & TRANS_QUEUED) {
  180. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  181. dprintk("On job queue already\n");
  182. return;
  183. }
  184. spin_lock_irqsave(&m2m_ctx->out_q_ctx.rdy_spinlock, flags);
  185. if (list_empty(&m2m_ctx->out_q_ctx.rdy_queue)) {
  186. spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock, flags);
  187. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  188. dprintk("No input buffers available\n");
  189. return;
  190. }
  191. if (list_empty(&m2m_ctx->cap_q_ctx.rdy_queue)) {
  192. spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock, flags);
  193. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  194. dprintk("No output buffers available\n");
  195. return;
  196. }
  197. spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock, flags);
  198. if (m2m_dev->m2m_ops->job_ready
  199. && (!m2m_dev->m2m_ops->job_ready(m2m_ctx->priv))) {
  200. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  201. dprintk("Driver not ready\n");
  202. return;
  203. }
  204. list_add_tail(&m2m_ctx->queue, &m2m_dev->job_queue);
  205. m2m_ctx->job_flags |= TRANS_QUEUED;
  206. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  207. v4l2_m2m_try_run(m2m_dev);
  208. }
  209. /**
  210. * v4l2_m2m_job_finish() - inform the framework that a job has been finished
  211. * and have it clean up
  212. *
  213. * Called by a driver to yield back the device after it has finished with it.
  214. * Should be called as soon as possible after reaching a state which allows
  215. * other instances to take control of the device.
  216. *
  217. * This function has to be called only after device_run() callback has been
  218. * called on the driver. To prevent recursion, it should not be called directly
  219. * from the device_run() callback though.
  220. */
  221. void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev,
  222. struct v4l2_m2m_ctx *m2m_ctx)
  223. {
  224. unsigned long flags;
  225. spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
  226. if (!m2m_dev->curr_ctx || m2m_dev->curr_ctx != m2m_ctx) {
  227. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  228. dprintk("Called by an instance not currently running\n");
  229. return;
  230. }
  231. list_del(&m2m_dev->curr_ctx->queue);
  232. m2m_dev->curr_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING);
  233. wake_up(&m2m_dev->curr_ctx->finished);
  234. m2m_dev->curr_ctx = NULL;
  235. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  236. /* This instance might have more buffers ready, but since we do not
  237. * allow more than one job on the job_queue per instance, each has
  238. * to be scheduled separately after the previous one finishes. */
  239. v4l2_m2m_try_schedule(m2m_ctx);
  240. v4l2_m2m_try_run(m2m_dev);
  241. }
  242. EXPORT_SYMBOL(v4l2_m2m_job_finish);
  243. /**
  244. * v4l2_m2m_reqbufs() - multi-queue-aware REQBUFS multiplexer
  245. */
  246. int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  247. struct v4l2_requestbuffers *reqbufs)
  248. {
  249. struct vb2_queue *vq;
  250. vq = v4l2_m2m_get_vq(m2m_ctx, reqbufs->type);
  251. return vb2_reqbufs(vq, reqbufs);
  252. }
  253. EXPORT_SYMBOL_GPL(v4l2_m2m_reqbufs);
  254. /**
  255. * v4l2_m2m_querybuf() - multi-queue-aware QUERYBUF multiplexer
  256. *
  257. * See v4l2_m2m_mmap() documentation for details.
  258. */
  259. int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  260. struct v4l2_buffer *buf)
  261. {
  262. struct vb2_queue *vq;
  263. int ret = 0;
  264. unsigned int i;
  265. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  266. ret = vb2_querybuf(vq, buf);
  267. /* Adjust MMAP memory offsets for the CAPTURE queue */
  268. if (buf->memory == V4L2_MEMORY_MMAP && !V4L2_TYPE_IS_OUTPUT(vq->type)) {
  269. if (V4L2_TYPE_IS_MULTIPLANAR(vq->type)) {
  270. for (i = 0; i < buf->length; ++i)
  271. buf->m.planes[i].m.mem_offset
  272. += DST_QUEUE_OFF_BASE;
  273. } else {
  274. buf->m.offset += DST_QUEUE_OFF_BASE;
  275. }
  276. }
  277. return ret;
  278. }
  279. EXPORT_SYMBOL_GPL(v4l2_m2m_querybuf);
  280. /**
  281. * v4l2_m2m_qbuf() - enqueue a source or destination buffer, depending on
  282. * the type
  283. */
  284. int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  285. struct v4l2_buffer *buf)
  286. {
  287. struct vb2_queue *vq;
  288. int ret;
  289. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  290. ret = vb2_qbuf(vq, buf);
  291. if (!ret)
  292. v4l2_m2m_try_schedule(m2m_ctx);
  293. return ret;
  294. }
  295. EXPORT_SYMBOL_GPL(v4l2_m2m_qbuf);
  296. /**
  297. * v4l2_m2m_dqbuf() - dequeue a source or destination buffer, depending on
  298. * the type
  299. */
  300. int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  301. struct v4l2_buffer *buf)
  302. {
  303. struct vb2_queue *vq;
  304. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  305. return vb2_dqbuf(vq, buf, file->f_flags & O_NONBLOCK);
  306. }
  307. EXPORT_SYMBOL_GPL(v4l2_m2m_dqbuf);
  308. /**
  309. * v4l2_m2m_streamon() - turn on streaming for a video queue
  310. */
  311. int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  312. enum v4l2_buf_type type)
  313. {
  314. struct vb2_queue *vq;
  315. int ret;
  316. vq = v4l2_m2m_get_vq(m2m_ctx, type);
  317. ret = vb2_streamon(vq, type);
  318. if (!ret)
  319. v4l2_m2m_try_schedule(m2m_ctx);
  320. return ret;
  321. }
  322. EXPORT_SYMBOL_GPL(v4l2_m2m_streamon);
  323. /**
  324. * v4l2_m2m_streamoff() - turn off streaming for a video queue
  325. */
  326. int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  327. enum v4l2_buf_type type)
  328. {
  329. struct vb2_queue *vq;
  330. vq = v4l2_m2m_get_vq(m2m_ctx, type);
  331. return vb2_streamoff(vq, type);
  332. }
  333. EXPORT_SYMBOL_GPL(v4l2_m2m_streamoff);
  334. /**
  335. * v4l2_m2m_poll() - poll replacement, for destination buffers only
  336. *
  337. * Call from the driver's poll() function. Will poll both queues. If a buffer
  338. * is available to dequeue (with dqbuf) from the source queue, this will
  339. * indicate that a non-blocking write can be performed, while read will be
  340. * returned in case of the destination queue.
  341. */
  342. unsigned int v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  343. struct poll_table_struct *wait)
  344. {
  345. struct vb2_queue *src_q, *dst_q;
  346. struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
  347. unsigned int rc = 0;
  348. unsigned long flags;
  349. src_q = v4l2_m2m_get_src_vq(m2m_ctx);
  350. dst_q = v4l2_m2m_get_dst_vq(m2m_ctx);
  351. /*
  352. * There has to be at least one buffer queued on each queued_list, which
  353. * means either in driver already or waiting for driver to claim it
  354. * and start processing.
  355. */
  356. if ((!src_q->streaming || list_empty(&src_q->queued_list))
  357. && (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
  358. rc = POLLERR;
  359. goto end;
  360. }
  361. if (m2m_ctx->m2m_dev->m2m_ops->unlock)
  362. m2m_ctx->m2m_dev->m2m_ops->unlock(m2m_ctx->priv);
  363. poll_wait(file, &src_q->done_wq, wait);
  364. poll_wait(file, &dst_q->done_wq, wait);
  365. if (m2m_ctx->m2m_dev->m2m_ops->lock)
  366. m2m_ctx->m2m_dev->m2m_ops->lock(m2m_ctx->priv);
  367. spin_lock_irqsave(&src_q->done_lock, flags);
  368. if (!list_empty(&src_q->done_list))
  369. src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
  370. done_entry);
  371. if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
  372. || src_vb->state == VB2_BUF_STATE_ERROR))
  373. rc |= POLLOUT | POLLWRNORM;
  374. spin_unlock_irqrestore(&src_q->done_lock, flags);
  375. spin_lock_irqsave(&dst_q->done_lock, flags);
  376. if (!list_empty(&dst_q->done_list))
  377. dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
  378. done_entry);
  379. if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
  380. || dst_vb->state == VB2_BUF_STATE_ERROR))
  381. rc |= POLLIN | POLLRDNORM;
  382. spin_unlock_irqrestore(&dst_q->done_lock, flags);
  383. end:
  384. return rc;
  385. }
  386. EXPORT_SYMBOL_GPL(v4l2_m2m_poll);
  387. /**
  388. * v4l2_m2m_mmap() - source and destination queues-aware mmap multiplexer
  389. *
  390. * Call from driver's mmap() function. Will handle mmap() for both queues
  391. * seamlessly for videobuffer, which will receive normal per-queue offsets and
  392. * proper videobuf queue pointers. The differentiation is made outside videobuf
  393. * by adding a predefined offset to buffers from one of the queues and
  394. * subtracting it before passing it back to videobuf. Only drivers (and
  395. * thus applications) receive modified offsets.
  396. */
  397. int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  398. struct vm_area_struct *vma)
  399. {
  400. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  401. struct vb2_queue *vq;
  402. if (offset < DST_QUEUE_OFF_BASE) {
  403. vq = v4l2_m2m_get_src_vq(m2m_ctx);
  404. } else {
  405. vq = v4l2_m2m_get_dst_vq(m2m_ctx);
  406. vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
  407. }
  408. return vb2_mmap(vq, vma);
  409. }
  410. EXPORT_SYMBOL(v4l2_m2m_mmap);
  411. /**
  412. * v4l2_m2m_init() - initialize per-driver m2m data
  413. *
  414. * Usually called from driver's probe() function.
  415. */
  416. struct v4l2_m2m_dev *v4l2_m2m_init(struct v4l2_m2m_ops *m2m_ops)
  417. {
  418. struct v4l2_m2m_dev *m2m_dev;
  419. if (!m2m_ops)
  420. return ERR_PTR(-EINVAL);
  421. BUG_ON(!m2m_ops->device_run);
  422. BUG_ON(!m2m_ops->job_abort);
  423. m2m_dev = kzalloc(sizeof *m2m_dev, GFP_KERNEL);
  424. if (!m2m_dev)
  425. return ERR_PTR(-ENOMEM);
  426. m2m_dev->curr_ctx = NULL;
  427. m2m_dev->m2m_ops = m2m_ops;
  428. INIT_LIST_HEAD(&m2m_dev->job_queue);
  429. spin_lock_init(&m2m_dev->job_spinlock);
  430. return m2m_dev;
  431. }
  432. EXPORT_SYMBOL_GPL(v4l2_m2m_init);
  433. /**
  434. * v4l2_m2m_release() - cleans up and frees a m2m_dev structure
  435. *
  436. * Usually called from driver's remove() function.
  437. */
  438. void v4l2_m2m_release(struct v4l2_m2m_dev *m2m_dev)
  439. {
  440. kfree(m2m_dev);
  441. }
  442. EXPORT_SYMBOL_GPL(v4l2_m2m_release);
  443. /**
  444. * v4l2_m2m_ctx_init() - allocate and initialize a m2m context
  445. * @priv - driver's instance private data
  446. * @m2m_dev - a previously initialized m2m_dev struct
  447. * @vq_init - a callback for queue type-specific initialization function to be
  448. * used for initializing videobuf_queues
  449. *
  450. * Usually called from driver's open() function.
  451. */
  452. struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev,
  453. void *drv_priv,
  454. int (*queue_init)(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq))
  455. {
  456. struct v4l2_m2m_ctx *m2m_ctx;
  457. struct v4l2_m2m_queue_ctx *out_q_ctx, *cap_q_ctx;
  458. int ret;
  459. m2m_ctx = kzalloc(sizeof *m2m_ctx, GFP_KERNEL);
  460. if (!m2m_ctx)
  461. return ERR_PTR(-ENOMEM);
  462. m2m_ctx->priv = drv_priv;
  463. m2m_ctx->m2m_dev = m2m_dev;
  464. init_waitqueue_head(&m2m_ctx->finished);
  465. out_q_ctx = &m2m_ctx->out_q_ctx;
  466. cap_q_ctx = &m2m_ctx->cap_q_ctx;
  467. INIT_LIST_HEAD(&out_q_ctx->rdy_queue);
  468. INIT_LIST_HEAD(&cap_q_ctx->rdy_queue);
  469. spin_lock_init(&out_q_ctx->rdy_spinlock);
  470. spin_lock_init(&cap_q_ctx->rdy_spinlock);
  471. INIT_LIST_HEAD(&m2m_ctx->queue);
  472. ret = queue_init(drv_priv, &out_q_ctx->q, &cap_q_ctx->q);
  473. if (ret)
  474. goto err;
  475. return m2m_ctx;
  476. err:
  477. kfree(m2m_ctx);
  478. return ERR_PTR(ret);
  479. }
  480. EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_init);
  481. /**
  482. * v4l2_m2m_ctx_release() - release m2m context
  483. *
  484. * Usually called from driver's release() function.
  485. */
  486. void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx *m2m_ctx)
  487. {
  488. struct v4l2_m2m_dev *m2m_dev;
  489. unsigned long flags;
  490. m2m_dev = m2m_ctx->m2m_dev;
  491. spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
  492. if (m2m_ctx->job_flags & TRANS_RUNNING) {
  493. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  494. m2m_dev->m2m_ops->job_abort(m2m_ctx->priv);
  495. dprintk("m2m_ctx %p running, will wait to complete", m2m_ctx);
  496. wait_event(m2m_ctx->finished, !(m2m_ctx->job_flags & TRANS_RUNNING));
  497. } else if (m2m_ctx->job_flags & TRANS_QUEUED) {
  498. list_del(&m2m_ctx->queue);
  499. m2m_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING);
  500. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  501. dprintk("m2m_ctx: %p had been on queue and was removed\n",
  502. m2m_ctx);
  503. } else {
  504. /* Do nothing, was not on queue/running */
  505. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  506. }
  507. vb2_queue_release(&m2m_ctx->cap_q_ctx.q);
  508. vb2_queue_release(&m2m_ctx->out_q_ctx.q);
  509. kfree(m2m_ctx);
  510. }
  511. EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_release);
  512. /**
  513. * v4l2_m2m_buf_queue() - add a buffer to the proper ready buffers list.
  514. *
  515. * Call from buf_queue(), videobuf_queue_ops callback.
  516. */
  517. void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx *m2m_ctx, struct vb2_buffer *vb)
  518. {
  519. struct v4l2_m2m_buffer *b = container_of(vb, struct v4l2_m2m_buffer, vb);
  520. struct v4l2_m2m_queue_ctx *q_ctx;
  521. unsigned long flags;
  522. q_ctx = get_queue_ctx(m2m_ctx, vb->vb2_queue->type);
  523. if (!q_ctx)
  524. return;
  525. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  526. list_add_tail(&b->list, &q_ctx->rdy_queue);
  527. q_ctx->num_rdy++;
  528. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  529. }
  530. EXPORT_SYMBOL_GPL(v4l2_m2m_buf_queue);