v4l2-mem2mem.c 17 KB

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