v4l2-mem2mem.c 17 KB

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