v4l2-mem2mem.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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. #include <media/v4l2-dev.h>
  22. #include <media/v4l2-fh.h>
  23. #include <media/v4l2-event.h>
  24. MODULE_DESCRIPTION("Mem to mem device framework for videobuf");
  25. MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
  26. MODULE_LICENSE("GPL");
  27. static bool debug;
  28. module_param(debug, bool, 0644);
  29. #define dprintk(fmt, arg...) \
  30. do { \
  31. if (debug) \
  32. printk(KERN_DEBUG "%s: " fmt, __func__, ## arg);\
  33. } while (0)
  34. /* Instance is already queued on the job_queue */
  35. #define TRANS_QUEUED (1 << 0)
  36. /* Instance is currently running in hardware */
  37. #define TRANS_RUNNING (1 << 1)
  38. /* Instance is currently aborting */
  39. #define TRANS_ABORT (1 << 2)
  40. /* Offset base for buffers on the destination queue - used to distinguish
  41. * between source and destination buffers when mmapping - they receive the same
  42. * offsets but for different queues */
  43. #define DST_QUEUE_OFF_BASE (1 << 30)
  44. /**
  45. * struct v4l2_m2m_dev - per-device context
  46. * @curr_ctx: currently running instance
  47. * @job_queue: instances queued to run
  48. * @job_spinlock: protects job_queue
  49. * @m2m_ops: driver callbacks
  50. */
  51. struct v4l2_m2m_dev {
  52. struct v4l2_m2m_ctx *curr_ctx;
  53. struct list_head job_queue;
  54. spinlock_t job_spinlock;
  55. const struct v4l2_m2m_ops *m2m_ops;
  56. };
  57. static struct v4l2_m2m_queue_ctx *get_queue_ctx(struct v4l2_m2m_ctx *m2m_ctx,
  58. enum v4l2_buf_type type)
  59. {
  60. if (V4L2_TYPE_IS_OUTPUT(type))
  61. return &m2m_ctx->out_q_ctx;
  62. else
  63. return &m2m_ctx->cap_q_ctx;
  64. }
  65. /**
  66. * v4l2_m2m_get_vq() - return vb2_queue for the given type
  67. */
  68. struct vb2_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_queue_ctx *q_ctx)
  82. {
  83. struct v4l2_m2m_buffer *b;
  84. unsigned long flags;
  85. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  86. if (list_empty(&q_ctx->rdy_queue)) {
  87. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  88. return NULL;
  89. }
  90. b = list_first_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list);
  91. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  92. return &b->vb;
  93. }
  94. EXPORT_SYMBOL_GPL(v4l2_m2m_next_buf);
  95. /**
  96. * v4l2_m2m_buf_remove() - take off a buffer from the list of ready buffers and
  97. * return it
  98. */
  99. void *v4l2_m2m_buf_remove(struct v4l2_m2m_queue_ctx *q_ctx)
  100. {
  101. struct v4l2_m2m_buffer *b;
  102. unsigned long flags;
  103. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  104. if (list_empty(&q_ctx->rdy_queue)) {
  105. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  106. return NULL;
  107. }
  108. b = list_first_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list);
  109. list_del(&b->list);
  110. q_ctx->num_rdy--;
  111. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  112. return &b->vb;
  113. }
  114. EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove);
  115. /*
  116. * Scheduling handlers
  117. */
  118. /**
  119. * v4l2_m2m_get_curr_priv() - return driver private data for the currently
  120. * running instance or NULL if no instance is running
  121. */
  122. void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev *m2m_dev)
  123. {
  124. unsigned long flags;
  125. void *ret = NULL;
  126. spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
  127. if (m2m_dev->curr_ctx)
  128. ret = m2m_dev->curr_ctx->priv;
  129. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  130. return ret;
  131. }
  132. EXPORT_SYMBOL(v4l2_m2m_get_curr_priv);
  133. /**
  134. * v4l2_m2m_try_run() - select next job to perform and run it if possible
  135. *
  136. * Get next transaction (if present) from the waiting jobs list and run it.
  137. */
  138. static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev)
  139. {
  140. unsigned long flags;
  141. spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
  142. if (NULL != m2m_dev->curr_ctx) {
  143. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  144. dprintk("Another instance is running, won't run now\n");
  145. return;
  146. }
  147. if (list_empty(&m2m_dev->job_queue)) {
  148. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  149. dprintk("No job pending\n");
  150. return;
  151. }
  152. m2m_dev->curr_ctx = list_first_entry(&m2m_dev->job_queue,
  153. struct v4l2_m2m_ctx, queue);
  154. m2m_dev->curr_ctx->job_flags |= TRANS_RUNNING;
  155. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  156. m2m_dev->m2m_ops->device_run(m2m_dev->curr_ctx->priv);
  157. }
  158. /**
  159. * v4l2_m2m_try_schedule() - check whether an instance is ready to be added to
  160. * the pending job queue and add it if so.
  161. * @m2m_ctx: m2m context assigned to the instance to be checked
  162. *
  163. * There are three basic requirements an instance has to meet to be able to run:
  164. * 1) at least one source buffer has to be queued,
  165. * 2) at least one destination buffer has to be queued,
  166. * 3) streaming has to be on.
  167. *
  168. * If a queue is buffered (for example a decoder hardware ringbuffer that has
  169. * to be drained before doing streamoff), allow scheduling without v4l2 buffers
  170. * on that queue.
  171. *
  172. * There may also be additional, custom requirements. In such case the driver
  173. * should supply a custom callback (job_ready in v4l2_m2m_ops) that should
  174. * return 1 if the instance is ready.
  175. * An example of the above could be an instance that requires more than one
  176. * src/dst buffer per transaction.
  177. */
  178. void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx)
  179. {
  180. struct v4l2_m2m_dev *m2m_dev;
  181. unsigned long flags_job, flags_out, flags_cap;
  182. m2m_dev = m2m_ctx->m2m_dev;
  183. dprintk("Trying to schedule a job for m2m_ctx: %p\n", m2m_ctx);
  184. if (!m2m_ctx->out_q_ctx.q.streaming
  185. || !m2m_ctx->cap_q_ctx.q.streaming) {
  186. dprintk("Streaming needs to be on for both queues\n");
  187. return;
  188. }
  189. spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job);
  190. /* If the context is aborted then don't schedule it */
  191. if (m2m_ctx->job_flags & TRANS_ABORT) {
  192. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  193. dprintk("Aborted context\n");
  194. return;
  195. }
  196. if (m2m_ctx->job_flags & TRANS_QUEUED) {
  197. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  198. dprintk("On job queue already\n");
  199. return;
  200. }
  201. spin_lock_irqsave(&m2m_ctx->out_q_ctx.rdy_spinlock, flags_out);
  202. if (list_empty(&m2m_ctx->out_q_ctx.rdy_queue)
  203. && !m2m_ctx->out_q_ctx.buffered) {
  204. spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock,
  205. flags_out);
  206. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  207. dprintk("No input buffers available\n");
  208. return;
  209. }
  210. spin_lock_irqsave(&m2m_ctx->cap_q_ctx.rdy_spinlock, flags_cap);
  211. if (list_empty(&m2m_ctx->cap_q_ctx.rdy_queue)
  212. && !m2m_ctx->cap_q_ctx.buffered) {
  213. spin_unlock_irqrestore(&m2m_ctx->cap_q_ctx.rdy_spinlock,
  214. flags_cap);
  215. spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock,
  216. flags_out);
  217. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  218. dprintk("No output buffers available\n");
  219. return;
  220. }
  221. spin_unlock_irqrestore(&m2m_ctx->cap_q_ctx.rdy_spinlock, flags_cap);
  222. spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock, flags_out);
  223. if (m2m_dev->m2m_ops->job_ready
  224. && (!m2m_dev->m2m_ops->job_ready(m2m_ctx->priv))) {
  225. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  226. dprintk("Driver not ready\n");
  227. return;
  228. }
  229. list_add_tail(&m2m_ctx->queue, &m2m_dev->job_queue);
  230. m2m_ctx->job_flags |= TRANS_QUEUED;
  231. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  232. v4l2_m2m_try_run(m2m_dev);
  233. }
  234. EXPORT_SYMBOL_GPL(v4l2_m2m_try_schedule);
  235. /**
  236. * v4l2_m2m_cancel_job() - cancel pending jobs for the context
  237. *
  238. * In case of streamoff or release called on any context,
  239. * 1] If the context is currently running, then abort job will be called
  240. * 2] If the context is queued, then the context will be removed from
  241. * the job_queue
  242. */
  243. static void v4l2_m2m_cancel_job(struct v4l2_m2m_ctx *m2m_ctx)
  244. {
  245. struct v4l2_m2m_dev *m2m_dev;
  246. unsigned long flags;
  247. m2m_dev = m2m_ctx->m2m_dev;
  248. spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
  249. m2m_ctx->job_flags |= TRANS_ABORT;
  250. if (m2m_ctx->job_flags & TRANS_RUNNING) {
  251. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  252. m2m_dev->m2m_ops->job_abort(m2m_ctx->priv);
  253. dprintk("m2m_ctx %p running, will wait to complete", m2m_ctx);
  254. wait_event(m2m_ctx->finished,
  255. !(m2m_ctx->job_flags & TRANS_RUNNING));
  256. } else if (m2m_ctx->job_flags & TRANS_QUEUED) {
  257. list_del(&m2m_ctx->queue);
  258. m2m_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING);
  259. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  260. dprintk("m2m_ctx: %p had been on queue and was removed\n",
  261. m2m_ctx);
  262. } else {
  263. /* Do nothing, was not on queue/running */
  264. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  265. }
  266. }
  267. /**
  268. * v4l2_m2m_job_finish() - inform the framework that a job has been finished
  269. * and have it clean up
  270. *
  271. * Called by a driver to yield back the device after it has finished with it.
  272. * Should be called as soon as possible after reaching a state which allows
  273. * other instances to take control of the device.
  274. *
  275. * This function has to be called only after device_run() callback has been
  276. * called on the driver. To prevent recursion, it should not be called directly
  277. * from the device_run() callback though.
  278. */
  279. void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev,
  280. struct v4l2_m2m_ctx *m2m_ctx)
  281. {
  282. unsigned long flags;
  283. spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
  284. if (!m2m_dev->curr_ctx || m2m_dev->curr_ctx != m2m_ctx) {
  285. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  286. dprintk("Called by an instance not currently running\n");
  287. return;
  288. }
  289. list_del(&m2m_dev->curr_ctx->queue);
  290. m2m_dev->curr_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING);
  291. wake_up(&m2m_dev->curr_ctx->finished);
  292. m2m_dev->curr_ctx = NULL;
  293. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  294. /* This instance might have more buffers ready, but since we do not
  295. * allow more than one job on the job_queue per instance, each has
  296. * to be scheduled separately after the previous one finishes. */
  297. v4l2_m2m_try_schedule(m2m_ctx);
  298. v4l2_m2m_try_run(m2m_dev);
  299. }
  300. EXPORT_SYMBOL(v4l2_m2m_job_finish);
  301. /**
  302. * v4l2_m2m_reqbufs() - multi-queue-aware REQBUFS multiplexer
  303. */
  304. int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  305. struct v4l2_requestbuffers *reqbufs)
  306. {
  307. struct vb2_queue *vq;
  308. int ret;
  309. vq = v4l2_m2m_get_vq(m2m_ctx, reqbufs->type);
  310. ret = vb2_reqbufs(vq, reqbufs);
  311. /* If count == 0, then the owner has released all buffers and he
  312. is no longer owner of the queue. Otherwise we have an owner. */
  313. if (ret == 0)
  314. vq->owner = reqbufs->count ? file->private_data : NULL;
  315. return ret;
  316. }
  317. EXPORT_SYMBOL_GPL(v4l2_m2m_reqbufs);
  318. /**
  319. * v4l2_m2m_querybuf() - multi-queue-aware QUERYBUF multiplexer
  320. *
  321. * See v4l2_m2m_mmap() documentation for details.
  322. */
  323. int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  324. struct v4l2_buffer *buf)
  325. {
  326. struct vb2_queue *vq;
  327. int ret = 0;
  328. unsigned int i;
  329. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  330. ret = vb2_querybuf(vq, buf);
  331. /* Adjust MMAP memory offsets for the CAPTURE queue */
  332. if (buf->memory == V4L2_MEMORY_MMAP && !V4L2_TYPE_IS_OUTPUT(vq->type)) {
  333. if (V4L2_TYPE_IS_MULTIPLANAR(vq->type)) {
  334. for (i = 0; i < buf->length; ++i)
  335. buf->m.planes[i].m.mem_offset
  336. += DST_QUEUE_OFF_BASE;
  337. } else {
  338. buf->m.offset += DST_QUEUE_OFF_BASE;
  339. }
  340. }
  341. return ret;
  342. }
  343. EXPORT_SYMBOL_GPL(v4l2_m2m_querybuf);
  344. /**
  345. * v4l2_m2m_qbuf() - enqueue a source or destination buffer, depending on
  346. * the type
  347. */
  348. int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  349. struct v4l2_buffer *buf)
  350. {
  351. struct vb2_queue *vq;
  352. int ret;
  353. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  354. ret = vb2_qbuf(vq, buf);
  355. if (!ret)
  356. v4l2_m2m_try_schedule(m2m_ctx);
  357. return ret;
  358. }
  359. EXPORT_SYMBOL_GPL(v4l2_m2m_qbuf);
  360. /**
  361. * v4l2_m2m_dqbuf() - dequeue a source or destination buffer, depending on
  362. * the type
  363. */
  364. int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  365. struct v4l2_buffer *buf)
  366. {
  367. struct vb2_queue *vq;
  368. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  369. return vb2_dqbuf(vq, buf, file->f_flags & O_NONBLOCK);
  370. }
  371. EXPORT_SYMBOL_GPL(v4l2_m2m_dqbuf);
  372. /**
  373. * v4l2_m2m_prepare_buf() - prepare a source or destination buffer, depending on
  374. * the type
  375. */
  376. int v4l2_m2m_prepare_buf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  377. struct v4l2_buffer *buf)
  378. {
  379. struct vb2_queue *vq;
  380. int ret;
  381. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  382. ret = vb2_prepare_buf(vq, buf);
  383. if (!ret)
  384. v4l2_m2m_try_schedule(m2m_ctx);
  385. return ret;
  386. }
  387. EXPORT_SYMBOL_GPL(v4l2_m2m_prepare_buf);
  388. /**
  389. * v4l2_m2m_create_bufs() - create a source or destination buffer, depending
  390. * on the type
  391. */
  392. int v4l2_m2m_create_bufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  393. struct v4l2_create_buffers *create)
  394. {
  395. struct vb2_queue *vq;
  396. vq = v4l2_m2m_get_vq(m2m_ctx, create->format.type);
  397. return vb2_create_bufs(vq, create);
  398. }
  399. EXPORT_SYMBOL_GPL(v4l2_m2m_create_bufs);
  400. /**
  401. * v4l2_m2m_expbuf() - export a source or destination buffer, depending on
  402. * the type
  403. */
  404. int v4l2_m2m_expbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  405. struct v4l2_exportbuffer *eb)
  406. {
  407. struct vb2_queue *vq;
  408. vq = v4l2_m2m_get_vq(m2m_ctx, eb->type);
  409. return vb2_expbuf(vq, eb);
  410. }
  411. EXPORT_SYMBOL_GPL(v4l2_m2m_expbuf);
  412. /**
  413. * v4l2_m2m_streamon() - turn on streaming for a video queue
  414. */
  415. int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  416. enum v4l2_buf_type type)
  417. {
  418. struct vb2_queue *vq;
  419. int ret;
  420. vq = v4l2_m2m_get_vq(m2m_ctx, type);
  421. ret = vb2_streamon(vq, type);
  422. if (!ret)
  423. v4l2_m2m_try_schedule(m2m_ctx);
  424. return ret;
  425. }
  426. EXPORT_SYMBOL_GPL(v4l2_m2m_streamon);
  427. /**
  428. * v4l2_m2m_streamoff() - turn off streaming for a video queue
  429. */
  430. int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  431. enum v4l2_buf_type type)
  432. {
  433. struct v4l2_m2m_dev *m2m_dev;
  434. struct v4l2_m2m_queue_ctx *q_ctx;
  435. unsigned long flags_job, flags;
  436. int ret;
  437. /* wait until the current context is dequeued from job_queue */
  438. v4l2_m2m_cancel_job(m2m_ctx);
  439. q_ctx = get_queue_ctx(m2m_ctx, type);
  440. ret = vb2_streamoff(&q_ctx->q, type);
  441. if (ret)
  442. return ret;
  443. m2m_dev = m2m_ctx->m2m_dev;
  444. spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job);
  445. /* We should not be scheduled anymore, since we're dropping a queue. */
  446. if (m2m_ctx->job_flags & TRANS_QUEUED)
  447. list_del(&m2m_ctx->queue);
  448. m2m_ctx->job_flags = 0;
  449. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  450. /* Drop queue, since streamoff returns device to the same state as after
  451. * calling reqbufs. */
  452. INIT_LIST_HEAD(&q_ctx->rdy_queue);
  453. q_ctx->num_rdy = 0;
  454. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  455. if (m2m_dev->curr_ctx == m2m_ctx) {
  456. m2m_dev->curr_ctx = NULL;
  457. wake_up(&m2m_ctx->finished);
  458. }
  459. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  460. return 0;
  461. }
  462. EXPORT_SYMBOL_GPL(v4l2_m2m_streamoff);
  463. /**
  464. * v4l2_m2m_poll() - poll replacement, for destination buffers only
  465. *
  466. * Call from the driver's poll() function. Will poll both queues. If a buffer
  467. * is available to dequeue (with dqbuf) from the source queue, this will
  468. * indicate that a non-blocking write can be performed, while read will be
  469. * returned in case of the destination queue.
  470. */
  471. unsigned int v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  472. struct poll_table_struct *wait)
  473. {
  474. struct video_device *vfd = video_devdata(file);
  475. unsigned long req_events = poll_requested_events(wait);
  476. struct vb2_queue *src_q, *dst_q;
  477. struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
  478. unsigned int rc = 0;
  479. unsigned long flags;
  480. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
  481. struct v4l2_fh *fh = file->private_data;
  482. if (v4l2_event_pending(fh))
  483. rc = POLLPRI;
  484. else if (req_events & POLLPRI)
  485. poll_wait(file, &fh->wait, wait);
  486. if (!(req_events & (POLLOUT | POLLWRNORM | POLLIN | POLLRDNORM)))
  487. return rc;
  488. }
  489. src_q = v4l2_m2m_get_src_vq(m2m_ctx);
  490. dst_q = v4l2_m2m_get_dst_vq(m2m_ctx);
  491. /*
  492. * There has to be at least one buffer queued on each queued_list, which
  493. * means either in driver already or waiting for driver to claim it
  494. * and start processing.
  495. */
  496. if ((!src_q->streaming || list_empty(&src_q->queued_list))
  497. && (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
  498. rc |= POLLERR;
  499. goto end;
  500. }
  501. if (m2m_ctx->m2m_dev->m2m_ops->unlock)
  502. m2m_ctx->m2m_dev->m2m_ops->unlock(m2m_ctx->priv);
  503. else if (m2m_ctx->q_lock)
  504. mutex_unlock(m2m_ctx->q_lock);
  505. if (list_empty(&src_q->done_list))
  506. poll_wait(file, &src_q->done_wq, wait);
  507. if (list_empty(&dst_q->done_list)) {
  508. /*
  509. * If the last buffer was dequeued from the capture queue,
  510. * return immediately. DQBUF will return -EPIPE.
  511. */
  512. if (dst_q->last_buffer_dequeued)
  513. return rc | POLLIN | POLLRDNORM;
  514. poll_wait(file, &dst_q->done_wq, wait);
  515. }
  516. if (m2m_ctx->m2m_dev->m2m_ops->lock)
  517. m2m_ctx->m2m_dev->m2m_ops->lock(m2m_ctx->priv);
  518. else if (m2m_ctx->q_lock) {
  519. if (mutex_lock_interruptible(m2m_ctx->q_lock)) {
  520. rc |= POLLERR;
  521. goto end;
  522. }
  523. }
  524. spin_lock_irqsave(&src_q->done_lock, flags);
  525. if (!list_empty(&src_q->done_list))
  526. src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
  527. done_entry);
  528. if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
  529. || src_vb->state == VB2_BUF_STATE_ERROR))
  530. rc |= POLLOUT | POLLWRNORM;
  531. spin_unlock_irqrestore(&src_q->done_lock, flags);
  532. spin_lock_irqsave(&dst_q->done_lock, flags);
  533. if (!list_empty(&dst_q->done_list))
  534. dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
  535. done_entry);
  536. if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
  537. || dst_vb->state == VB2_BUF_STATE_ERROR))
  538. rc |= POLLIN | POLLRDNORM;
  539. spin_unlock_irqrestore(&dst_q->done_lock, flags);
  540. end:
  541. return rc;
  542. }
  543. EXPORT_SYMBOL_GPL(v4l2_m2m_poll);
  544. /**
  545. * v4l2_m2m_mmap() - source and destination queues-aware mmap multiplexer
  546. *
  547. * Call from driver's mmap() function. Will handle mmap() for both queues
  548. * seamlessly for videobuffer, which will receive normal per-queue offsets and
  549. * proper videobuf queue pointers. The differentiation is made outside videobuf
  550. * by adding a predefined offset to buffers from one of the queues and
  551. * subtracting it before passing it back to videobuf. Only drivers (and
  552. * thus applications) receive modified offsets.
  553. */
  554. int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  555. struct vm_area_struct *vma)
  556. {
  557. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  558. struct vb2_queue *vq;
  559. if (offset < DST_QUEUE_OFF_BASE) {
  560. vq = v4l2_m2m_get_src_vq(m2m_ctx);
  561. } else {
  562. vq = v4l2_m2m_get_dst_vq(m2m_ctx);
  563. vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
  564. }
  565. return vb2_mmap(vq, vma);
  566. }
  567. EXPORT_SYMBOL(v4l2_m2m_mmap);
  568. /**
  569. * v4l2_m2m_init() - initialize per-driver m2m data
  570. *
  571. * Usually called from driver's probe() function.
  572. */
  573. struct v4l2_m2m_dev *v4l2_m2m_init(const struct v4l2_m2m_ops *m2m_ops)
  574. {
  575. struct v4l2_m2m_dev *m2m_dev;
  576. if (!m2m_ops || WARN_ON(!m2m_ops->device_run) ||
  577. WARN_ON(!m2m_ops->job_abort))
  578. return ERR_PTR(-EINVAL);
  579. m2m_dev = kzalloc(sizeof *m2m_dev, GFP_KERNEL);
  580. if (!m2m_dev)
  581. return ERR_PTR(-ENOMEM);
  582. m2m_dev->curr_ctx = NULL;
  583. m2m_dev->m2m_ops = m2m_ops;
  584. INIT_LIST_HEAD(&m2m_dev->job_queue);
  585. spin_lock_init(&m2m_dev->job_spinlock);
  586. return m2m_dev;
  587. }
  588. EXPORT_SYMBOL_GPL(v4l2_m2m_init);
  589. /**
  590. * v4l2_m2m_release() - cleans up and frees a m2m_dev structure
  591. *
  592. * Usually called from driver's remove() function.
  593. */
  594. void v4l2_m2m_release(struct v4l2_m2m_dev *m2m_dev)
  595. {
  596. kfree(m2m_dev);
  597. }
  598. EXPORT_SYMBOL_GPL(v4l2_m2m_release);
  599. /**
  600. * v4l2_m2m_ctx_init() - allocate and initialize a m2m context
  601. * @priv - driver's instance private data
  602. * @m2m_dev - a previously initialized m2m_dev struct
  603. * @vq_init - a callback for queue type-specific initialization function to be
  604. * used for initializing videobuf_queues
  605. *
  606. * Usually called from driver's open() function.
  607. */
  608. struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev,
  609. void *drv_priv,
  610. int (*queue_init)(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq))
  611. {
  612. struct v4l2_m2m_ctx *m2m_ctx;
  613. struct v4l2_m2m_queue_ctx *out_q_ctx, *cap_q_ctx;
  614. int ret;
  615. m2m_ctx = kzalloc(sizeof *m2m_ctx, GFP_KERNEL);
  616. if (!m2m_ctx)
  617. return ERR_PTR(-ENOMEM);
  618. m2m_ctx->priv = drv_priv;
  619. m2m_ctx->m2m_dev = m2m_dev;
  620. init_waitqueue_head(&m2m_ctx->finished);
  621. out_q_ctx = &m2m_ctx->out_q_ctx;
  622. cap_q_ctx = &m2m_ctx->cap_q_ctx;
  623. INIT_LIST_HEAD(&out_q_ctx->rdy_queue);
  624. INIT_LIST_HEAD(&cap_q_ctx->rdy_queue);
  625. spin_lock_init(&out_q_ctx->rdy_spinlock);
  626. spin_lock_init(&cap_q_ctx->rdy_spinlock);
  627. INIT_LIST_HEAD(&m2m_ctx->queue);
  628. ret = queue_init(drv_priv, &out_q_ctx->q, &cap_q_ctx->q);
  629. if (ret)
  630. goto err;
  631. /*
  632. * If both queues use same mutex assign it as the common buffer
  633. * queues lock to the m2m context. This lock is used in the
  634. * v4l2_m2m_ioctl_* helpers.
  635. */
  636. if (out_q_ctx->q.lock == cap_q_ctx->q.lock)
  637. m2m_ctx->q_lock = out_q_ctx->q.lock;
  638. return m2m_ctx;
  639. err:
  640. kfree(m2m_ctx);
  641. return ERR_PTR(ret);
  642. }
  643. EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_init);
  644. /**
  645. * v4l2_m2m_ctx_release() - release m2m context
  646. *
  647. * Usually called from driver's release() function.
  648. */
  649. void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx *m2m_ctx)
  650. {
  651. /* wait until the current context is dequeued from job_queue */
  652. v4l2_m2m_cancel_job(m2m_ctx);
  653. vb2_queue_release(&m2m_ctx->cap_q_ctx.q);
  654. vb2_queue_release(&m2m_ctx->out_q_ctx.q);
  655. kfree(m2m_ctx);
  656. }
  657. EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_release);
  658. /**
  659. * v4l2_m2m_buf_queue() - add a buffer to the proper ready buffers list.
  660. *
  661. * Call from buf_queue(), videobuf_queue_ops callback.
  662. */
  663. void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx *m2m_ctx, struct vb2_buffer *vb)
  664. {
  665. struct v4l2_m2m_buffer *b = container_of(vb, struct v4l2_m2m_buffer, vb);
  666. struct v4l2_m2m_queue_ctx *q_ctx;
  667. unsigned long flags;
  668. q_ctx = get_queue_ctx(m2m_ctx, vb->vb2_queue->type);
  669. if (!q_ctx)
  670. return;
  671. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  672. list_add_tail(&b->list, &q_ctx->rdy_queue);
  673. q_ctx->num_rdy++;
  674. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  675. }
  676. EXPORT_SYMBOL_GPL(v4l2_m2m_buf_queue);
  677. /* Videobuf2 ioctl helpers */
  678. int v4l2_m2m_ioctl_reqbufs(struct file *file, void *priv,
  679. struct v4l2_requestbuffers *rb)
  680. {
  681. struct v4l2_fh *fh = file->private_data;
  682. return v4l2_m2m_reqbufs(file, fh->m2m_ctx, rb);
  683. }
  684. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_reqbufs);
  685. int v4l2_m2m_ioctl_create_bufs(struct file *file, void *priv,
  686. struct v4l2_create_buffers *create)
  687. {
  688. struct v4l2_fh *fh = file->private_data;
  689. return v4l2_m2m_create_bufs(file, fh->m2m_ctx, create);
  690. }
  691. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_create_bufs);
  692. int v4l2_m2m_ioctl_querybuf(struct file *file, void *priv,
  693. struct v4l2_buffer *buf)
  694. {
  695. struct v4l2_fh *fh = file->private_data;
  696. return v4l2_m2m_querybuf(file, fh->m2m_ctx, buf);
  697. }
  698. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_querybuf);
  699. int v4l2_m2m_ioctl_qbuf(struct file *file, void *priv,
  700. struct v4l2_buffer *buf)
  701. {
  702. struct v4l2_fh *fh = file->private_data;
  703. return v4l2_m2m_qbuf(file, fh->m2m_ctx, buf);
  704. }
  705. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_qbuf);
  706. int v4l2_m2m_ioctl_dqbuf(struct file *file, void *priv,
  707. struct v4l2_buffer *buf)
  708. {
  709. struct v4l2_fh *fh = file->private_data;
  710. return v4l2_m2m_dqbuf(file, fh->m2m_ctx, buf);
  711. }
  712. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_dqbuf);
  713. int v4l2_m2m_ioctl_prepare_buf(struct file *file, void *priv,
  714. struct v4l2_buffer *buf)
  715. {
  716. struct v4l2_fh *fh = file->private_data;
  717. return v4l2_m2m_prepare_buf(file, fh->m2m_ctx, buf);
  718. }
  719. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_prepare_buf);
  720. int v4l2_m2m_ioctl_expbuf(struct file *file, void *priv,
  721. struct v4l2_exportbuffer *eb)
  722. {
  723. struct v4l2_fh *fh = file->private_data;
  724. return v4l2_m2m_expbuf(file, fh->m2m_ctx, eb);
  725. }
  726. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_expbuf);
  727. int v4l2_m2m_ioctl_streamon(struct file *file, void *priv,
  728. enum v4l2_buf_type type)
  729. {
  730. struct v4l2_fh *fh = file->private_data;
  731. return v4l2_m2m_streamon(file, fh->m2m_ctx, type);
  732. }
  733. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamon);
  734. int v4l2_m2m_ioctl_streamoff(struct file *file, void *priv,
  735. enum v4l2_buf_type type)
  736. {
  737. struct v4l2_fh *fh = file->private_data;
  738. return v4l2_m2m_streamoff(file, fh->m2m_ctx, type);
  739. }
  740. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamoff);
  741. /*
  742. * v4l2_file_operations helpers. It is assumed here same lock is used
  743. * for the output and the capture buffer queue.
  744. */
  745. int v4l2_m2m_fop_mmap(struct file *file, struct vm_area_struct *vma)
  746. {
  747. struct v4l2_fh *fh = file->private_data;
  748. return v4l2_m2m_mmap(file, fh->m2m_ctx, vma);
  749. }
  750. EXPORT_SYMBOL_GPL(v4l2_m2m_fop_mmap);
  751. unsigned int v4l2_m2m_fop_poll(struct file *file, poll_table *wait)
  752. {
  753. struct v4l2_fh *fh = file->private_data;
  754. struct v4l2_m2m_ctx *m2m_ctx = fh->m2m_ctx;
  755. unsigned int ret;
  756. if (m2m_ctx->q_lock)
  757. mutex_lock(m2m_ctx->q_lock);
  758. ret = v4l2_m2m_poll(file, m2m_ctx, wait);
  759. if (m2m_ctx->q_lock)
  760. mutex_unlock(m2m_ctx->q_lock);
  761. return ret;
  762. }
  763. EXPORT_SYMBOL_GPL(v4l2_m2m_fop_poll);