v4l2-mem2mem.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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/media-device.h>
  20. #include <media/videobuf2-v4l2.h>
  21. #include <media/v4l2-mem2mem.h>
  22. #include <media/v4l2-dev.h>
  23. #include <media/v4l2-device.h>
  24. #include <media/v4l2-fh.h>
  25. #include <media/v4l2-event.h>
  26. MODULE_DESCRIPTION("Mem to mem device framework for videobuf");
  27. MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
  28. MODULE_LICENSE("GPL");
  29. static bool debug;
  30. module_param(debug, bool, 0644);
  31. #define dprintk(fmt, arg...) \
  32. do { \
  33. if (debug) \
  34. printk(KERN_DEBUG "%s: " fmt, __func__, ## arg);\
  35. } while (0)
  36. /* Instance is already queued on the job_queue */
  37. #define TRANS_QUEUED (1 << 0)
  38. /* Instance is currently running in hardware */
  39. #define TRANS_RUNNING (1 << 1)
  40. /* Instance is currently aborting */
  41. #define TRANS_ABORT (1 << 2)
  42. /* Offset base for buffers on the destination queue - used to distinguish
  43. * between source and destination buffers when mmapping - they receive the same
  44. * offsets but for different queues */
  45. #define DST_QUEUE_OFF_BASE (1 << 30)
  46. enum v4l2_m2m_entity_type {
  47. MEM2MEM_ENT_TYPE_SOURCE,
  48. MEM2MEM_ENT_TYPE_SINK,
  49. MEM2MEM_ENT_TYPE_PROC
  50. };
  51. static const char * const m2m_entity_name[] = {
  52. "source",
  53. "sink",
  54. "proc"
  55. };
  56. /**
  57. * struct v4l2_m2m_dev - per-device context
  58. * @source: &struct media_entity pointer with the source entity
  59. * Used only when the M2M device is registered via
  60. * v4l2_m2m_unregister_media_controller().
  61. * @source_pad: &struct media_pad with the source pad.
  62. * Used only when the M2M device is registered via
  63. * v4l2_m2m_unregister_media_controller().
  64. * @sink: &struct media_entity pointer with the sink entity
  65. * Used only when the M2M device is registered via
  66. * v4l2_m2m_unregister_media_controller().
  67. * @sink_pad: &struct media_pad with the sink pad.
  68. * Used only when the M2M device is registered via
  69. * v4l2_m2m_unregister_media_controller().
  70. * @proc: &struct media_entity pointer with the M2M device itself.
  71. * @proc_pads: &struct media_pad with the @proc pads.
  72. * Used only when the M2M device is registered via
  73. * v4l2_m2m_unregister_media_controller().
  74. * @intf_devnode: &struct media_intf devnode pointer with the interface
  75. * with controls the M2M device.
  76. * @curr_ctx: currently running instance
  77. * @job_queue: instances queued to run
  78. * @job_spinlock: protects job_queue
  79. * @m2m_ops: driver callbacks
  80. */
  81. struct v4l2_m2m_dev {
  82. struct v4l2_m2m_ctx *curr_ctx;
  83. #ifdef CONFIG_MEDIA_CONTROLLER
  84. struct media_entity *source;
  85. struct media_pad source_pad;
  86. struct media_entity sink;
  87. struct media_pad sink_pad;
  88. struct media_entity proc;
  89. struct media_pad proc_pads[2];
  90. struct media_intf_devnode *intf_devnode;
  91. #endif
  92. struct list_head job_queue;
  93. spinlock_t job_spinlock;
  94. const struct v4l2_m2m_ops *m2m_ops;
  95. };
  96. static struct v4l2_m2m_queue_ctx *get_queue_ctx(struct v4l2_m2m_ctx *m2m_ctx,
  97. enum v4l2_buf_type type)
  98. {
  99. if (V4L2_TYPE_IS_OUTPUT(type))
  100. return &m2m_ctx->out_q_ctx;
  101. else
  102. return &m2m_ctx->cap_q_ctx;
  103. }
  104. struct vb2_queue *v4l2_m2m_get_vq(struct v4l2_m2m_ctx *m2m_ctx,
  105. enum v4l2_buf_type type)
  106. {
  107. struct v4l2_m2m_queue_ctx *q_ctx;
  108. q_ctx = get_queue_ctx(m2m_ctx, type);
  109. if (!q_ctx)
  110. return NULL;
  111. return &q_ctx->q;
  112. }
  113. EXPORT_SYMBOL(v4l2_m2m_get_vq);
  114. void *v4l2_m2m_next_buf(struct v4l2_m2m_queue_ctx *q_ctx)
  115. {
  116. struct v4l2_m2m_buffer *b;
  117. unsigned long flags;
  118. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  119. if (list_empty(&q_ctx->rdy_queue)) {
  120. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  121. return NULL;
  122. }
  123. b = list_first_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list);
  124. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  125. return &b->vb;
  126. }
  127. EXPORT_SYMBOL_GPL(v4l2_m2m_next_buf);
  128. void *v4l2_m2m_last_buf(struct v4l2_m2m_queue_ctx *q_ctx)
  129. {
  130. struct v4l2_m2m_buffer *b;
  131. unsigned long flags;
  132. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  133. if (list_empty(&q_ctx->rdy_queue)) {
  134. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  135. return NULL;
  136. }
  137. b = list_last_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list);
  138. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  139. return &b->vb;
  140. }
  141. EXPORT_SYMBOL_GPL(v4l2_m2m_last_buf);
  142. void *v4l2_m2m_buf_remove(struct v4l2_m2m_queue_ctx *q_ctx)
  143. {
  144. struct v4l2_m2m_buffer *b;
  145. unsigned long flags;
  146. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  147. if (list_empty(&q_ctx->rdy_queue)) {
  148. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  149. return NULL;
  150. }
  151. b = list_first_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list);
  152. list_del(&b->list);
  153. q_ctx->num_rdy--;
  154. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  155. return &b->vb;
  156. }
  157. EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove);
  158. void v4l2_m2m_buf_remove_by_buf(struct v4l2_m2m_queue_ctx *q_ctx,
  159. struct vb2_v4l2_buffer *vbuf)
  160. {
  161. struct v4l2_m2m_buffer *b;
  162. unsigned long flags;
  163. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  164. b = container_of(vbuf, struct v4l2_m2m_buffer, vb);
  165. list_del(&b->list);
  166. q_ctx->num_rdy--;
  167. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  168. }
  169. EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove_by_buf);
  170. struct vb2_v4l2_buffer *
  171. v4l2_m2m_buf_remove_by_idx(struct v4l2_m2m_queue_ctx *q_ctx, unsigned int idx)
  172. {
  173. struct v4l2_m2m_buffer *b, *tmp;
  174. struct vb2_v4l2_buffer *ret = NULL;
  175. unsigned long flags;
  176. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  177. list_for_each_entry_safe(b, tmp, &q_ctx->rdy_queue, list) {
  178. if (b->vb.vb2_buf.index == idx) {
  179. list_del(&b->list);
  180. q_ctx->num_rdy--;
  181. ret = &b->vb;
  182. break;
  183. }
  184. }
  185. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  186. return ret;
  187. }
  188. EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove_by_idx);
  189. /*
  190. * Scheduling handlers
  191. */
  192. void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev *m2m_dev)
  193. {
  194. unsigned long flags;
  195. void *ret = NULL;
  196. spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
  197. if (m2m_dev->curr_ctx)
  198. ret = m2m_dev->curr_ctx->priv;
  199. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  200. return ret;
  201. }
  202. EXPORT_SYMBOL(v4l2_m2m_get_curr_priv);
  203. /**
  204. * v4l2_m2m_try_run() - select next job to perform and run it if possible
  205. * @m2m_dev: per-device context
  206. *
  207. * Get next transaction (if present) from the waiting jobs list and run it.
  208. */
  209. static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev)
  210. {
  211. unsigned long flags;
  212. spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
  213. if (NULL != m2m_dev->curr_ctx) {
  214. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  215. dprintk("Another instance is running, won't run now\n");
  216. return;
  217. }
  218. if (list_empty(&m2m_dev->job_queue)) {
  219. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  220. dprintk("No job pending\n");
  221. return;
  222. }
  223. m2m_dev->curr_ctx = list_first_entry(&m2m_dev->job_queue,
  224. struct v4l2_m2m_ctx, queue);
  225. m2m_dev->curr_ctx->job_flags |= TRANS_RUNNING;
  226. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  227. dprintk("Running job on m2m_ctx: %p\n", m2m_dev->curr_ctx);
  228. m2m_dev->m2m_ops->device_run(m2m_dev->curr_ctx->priv);
  229. }
  230. /*
  231. * __v4l2_m2m_try_queue() - queue a job
  232. * @m2m_dev: m2m device
  233. * @m2m_ctx: m2m context
  234. *
  235. * Check if this context is ready to queue a job.
  236. *
  237. * This function can run in interrupt context.
  238. */
  239. static void __v4l2_m2m_try_queue(struct v4l2_m2m_dev *m2m_dev,
  240. struct v4l2_m2m_ctx *m2m_ctx)
  241. {
  242. unsigned long flags_job, flags_out, flags_cap;
  243. dprintk("Trying to schedule a job for m2m_ctx: %p\n", m2m_ctx);
  244. if (!m2m_ctx->out_q_ctx.q.streaming
  245. || !m2m_ctx->cap_q_ctx.q.streaming) {
  246. dprintk("Streaming needs to be on for both queues\n");
  247. return;
  248. }
  249. spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job);
  250. /* If the context is aborted then don't schedule it */
  251. if (m2m_ctx->job_flags & TRANS_ABORT) {
  252. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  253. dprintk("Aborted context\n");
  254. return;
  255. }
  256. if (m2m_ctx->job_flags & TRANS_QUEUED) {
  257. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  258. dprintk("On job queue already\n");
  259. return;
  260. }
  261. spin_lock_irqsave(&m2m_ctx->out_q_ctx.rdy_spinlock, flags_out);
  262. if (list_empty(&m2m_ctx->out_q_ctx.rdy_queue)
  263. && !m2m_ctx->out_q_ctx.buffered) {
  264. spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock,
  265. flags_out);
  266. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  267. dprintk("No input buffers available\n");
  268. return;
  269. }
  270. spin_lock_irqsave(&m2m_ctx->cap_q_ctx.rdy_spinlock, flags_cap);
  271. if (list_empty(&m2m_ctx->cap_q_ctx.rdy_queue)
  272. && !m2m_ctx->cap_q_ctx.buffered) {
  273. spin_unlock_irqrestore(&m2m_ctx->cap_q_ctx.rdy_spinlock,
  274. flags_cap);
  275. spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock,
  276. flags_out);
  277. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  278. dprintk("No output buffers available\n");
  279. return;
  280. }
  281. spin_unlock_irqrestore(&m2m_ctx->cap_q_ctx.rdy_spinlock, flags_cap);
  282. spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock, flags_out);
  283. if (m2m_dev->m2m_ops->job_ready
  284. && (!m2m_dev->m2m_ops->job_ready(m2m_ctx->priv))) {
  285. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  286. dprintk("Driver not ready\n");
  287. return;
  288. }
  289. list_add_tail(&m2m_ctx->queue, &m2m_dev->job_queue);
  290. m2m_ctx->job_flags |= TRANS_QUEUED;
  291. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  292. }
  293. /**
  294. * v4l2_m2m_try_schedule() - schedule and possibly run a job for any context
  295. * @m2m_ctx: m2m context
  296. *
  297. * Check if this context is ready to queue a job. If suitable,
  298. * run the next queued job on the mem2mem device.
  299. *
  300. * This function shouldn't run in interrupt context.
  301. *
  302. * Note that v4l2_m2m_try_schedule() can schedule one job for this context,
  303. * and then run another job for another context.
  304. */
  305. void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx)
  306. {
  307. struct v4l2_m2m_dev *m2m_dev = m2m_ctx->m2m_dev;
  308. __v4l2_m2m_try_queue(m2m_dev, m2m_ctx);
  309. v4l2_m2m_try_run(m2m_dev);
  310. }
  311. EXPORT_SYMBOL_GPL(v4l2_m2m_try_schedule);
  312. /**
  313. * v4l2_m2m_cancel_job() - cancel pending jobs for the context
  314. * @m2m_ctx: m2m context with jobs to be canceled
  315. *
  316. * In case of streamoff or release called on any context,
  317. * 1] If the context is currently running, then abort job will be called
  318. * 2] If the context is queued, then the context will be removed from
  319. * the job_queue
  320. */
  321. static void v4l2_m2m_cancel_job(struct v4l2_m2m_ctx *m2m_ctx)
  322. {
  323. struct v4l2_m2m_dev *m2m_dev;
  324. unsigned long flags;
  325. m2m_dev = m2m_ctx->m2m_dev;
  326. spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
  327. m2m_ctx->job_flags |= TRANS_ABORT;
  328. if (m2m_ctx->job_flags & TRANS_RUNNING) {
  329. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  330. if (m2m_dev->m2m_ops->job_abort)
  331. m2m_dev->m2m_ops->job_abort(m2m_ctx->priv);
  332. dprintk("m2m_ctx %p running, will wait to complete", m2m_ctx);
  333. wait_event(m2m_ctx->finished,
  334. !(m2m_ctx->job_flags & TRANS_RUNNING));
  335. } else if (m2m_ctx->job_flags & TRANS_QUEUED) {
  336. list_del(&m2m_ctx->queue);
  337. m2m_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING);
  338. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  339. dprintk("m2m_ctx: %p had been on queue and was removed\n",
  340. m2m_ctx);
  341. } else {
  342. /* Do nothing, was not on queue/running */
  343. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  344. }
  345. }
  346. void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev,
  347. struct v4l2_m2m_ctx *m2m_ctx)
  348. {
  349. unsigned long flags;
  350. spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
  351. if (!m2m_dev->curr_ctx || m2m_dev->curr_ctx != m2m_ctx) {
  352. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  353. dprintk("Called by an instance not currently running\n");
  354. return;
  355. }
  356. list_del(&m2m_dev->curr_ctx->queue);
  357. m2m_dev->curr_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING);
  358. wake_up(&m2m_dev->curr_ctx->finished);
  359. m2m_dev->curr_ctx = NULL;
  360. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  361. /* This instance might have more buffers ready, but since we do not
  362. * allow more than one job on the job_queue per instance, each has
  363. * to be scheduled separately after the previous one finishes. */
  364. v4l2_m2m_try_schedule(m2m_ctx);
  365. }
  366. EXPORT_SYMBOL(v4l2_m2m_job_finish);
  367. int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  368. struct v4l2_requestbuffers *reqbufs)
  369. {
  370. struct vb2_queue *vq;
  371. int ret;
  372. vq = v4l2_m2m_get_vq(m2m_ctx, reqbufs->type);
  373. ret = vb2_reqbufs(vq, reqbufs);
  374. /* If count == 0, then the owner has released all buffers and he
  375. is no longer owner of the queue. Otherwise we have an owner. */
  376. if (ret == 0)
  377. vq->owner = reqbufs->count ? file->private_data : NULL;
  378. return ret;
  379. }
  380. EXPORT_SYMBOL_GPL(v4l2_m2m_reqbufs);
  381. int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  382. struct v4l2_buffer *buf)
  383. {
  384. struct vb2_queue *vq;
  385. int ret = 0;
  386. unsigned int i;
  387. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  388. ret = vb2_querybuf(vq, buf);
  389. /* Adjust MMAP memory offsets for the CAPTURE queue */
  390. if (buf->memory == V4L2_MEMORY_MMAP && !V4L2_TYPE_IS_OUTPUT(vq->type)) {
  391. if (V4L2_TYPE_IS_MULTIPLANAR(vq->type)) {
  392. for (i = 0; i < buf->length; ++i)
  393. buf->m.planes[i].m.mem_offset
  394. += DST_QUEUE_OFF_BASE;
  395. } else {
  396. buf->m.offset += DST_QUEUE_OFF_BASE;
  397. }
  398. }
  399. return ret;
  400. }
  401. EXPORT_SYMBOL_GPL(v4l2_m2m_querybuf);
  402. int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  403. struct v4l2_buffer *buf)
  404. {
  405. struct vb2_queue *vq;
  406. int ret;
  407. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  408. ret = vb2_qbuf(vq, buf);
  409. if (!ret)
  410. v4l2_m2m_try_schedule(m2m_ctx);
  411. return ret;
  412. }
  413. EXPORT_SYMBOL_GPL(v4l2_m2m_qbuf);
  414. int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  415. struct v4l2_buffer *buf)
  416. {
  417. struct vb2_queue *vq;
  418. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  419. return vb2_dqbuf(vq, buf, file->f_flags & O_NONBLOCK);
  420. }
  421. EXPORT_SYMBOL_GPL(v4l2_m2m_dqbuf);
  422. int v4l2_m2m_prepare_buf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  423. struct v4l2_buffer *buf)
  424. {
  425. struct vb2_queue *vq;
  426. int ret;
  427. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  428. ret = vb2_prepare_buf(vq, buf);
  429. if (!ret)
  430. v4l2_m2m_try_schedule(m2m_ctx);
  431. return ret;
  432. }
  433. EXPORT_SYMBOL_GPL(v4l2_m2m_prepare_buf);
  434. int v4l2_m2m_create_bufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  435. struct v4l2_create_buffers *create)
  436. {
  437. struct vb2_queue *vq;
  438. vq = v4l2_m2m_get_vq(m2m_ctx, create->format.type);
  439. return vb2_create_bufs(vq, create);
  440. }
  441. EXPORT_SYMBOL_GPL(v4l2_m2m_create_bufs);
  442. int v4l2_m2m_expbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  443. struct v4l2_exportbuffer *eb)
  444. {
  445. struct vb2_queue *vq;
  446. vq = v4l2_m2m_get_vq(m2m_ctx, eb->type);
  447. return vb2_expbuf(vq, eb);
  448. }
  449. EXPORT_SYMBOL_GPL(v4l2_m2m_expbuf);
  450. int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  451. enum v4l2_buf_type type)
  452. {
  453. struct vb2_queue *vq;
  454. int ret;
  455. vq = v4l2_m2m_get_vq(m2m_ctx, type);
  456. ret = vb2_streamon(vq, type);
  457. if (!ret)
  458. v4l2_m2m_try_schedule(m2m_ctx);
  459. return ret;
  460. }
  461. EXPORT_SYMBOL_GPL(v4l2_m2m_streamon);
  462. int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  463. enum v4l2_buf_type type)
  464. {
  465. struct v4l2_m2m_dev *m2m_dev;
  466. struct v4l2_m2m_queue_ctx *q_ctx;
  467. unsigned long flags_job, flags;
  468. int ret;
  469. /* wait until the current context is dequeued from job_queue */
  470. v4l2_m2m_cancel_job(m2m_ctx);
  471. q_ctx = get_queue_ctx(m2m_ctx, type);
  472. ret = vb2_streamoff(&q_ctx->q, type);
  473. if (ret)
  474. return ret;
  475. m2m_dev = m2m_ctx->m2m_dev;
  476. spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job);
  477. /* We should not be scheduled anymore, since we're dropping a queue. */
  478. if (m2m_ctx->job_flags & TRANS_QUEUED)
  479. list_del(&m2m_ctx->queue);
  480. m2m_ctx->job_flags = 0;
  481. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  482. /* Drop queue, since streamoff returns device to the same state as after
  483. * calling reqbufs. */
  484. INIT_LIST_HEAD(&q_ctx->rdy_queue);
  485. q_ctx->num_rdy = 0;
  486. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  487. if (m2m_dev->curr_ctx == m2m_ctx) {
  488. m2m_dev->curr_ctx = NULL;
  489. wake_up(&m2m_ctx->finished);
  490. }
  491. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  492. return 0;
  493. }
  494. EXPORT_SYMBOL_GPL(v4l2_m2m_streamoff);
  495. __poll_t v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  496. struct poll_table_struct *wait)
  497. {
  498. struct video_device *vfd = video_devdata(file);
  499. __poll_t req_events = poll_requested_events(wait);
  500. struct vb2_queue *src_q, *dst_q;
  501. struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
  502. __poll_t rc = 0;
  503. unsigned long flags;
  504. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
  505. struct v4l2_fh *fh = file->private_data;
  506. if (v4l2_event_pending(fh))
  507. rc = EPOLLPRI;
  508. else if (req_events & EPOLLPRI)
  509. poll_wait(file, &fh->wait, wait);
  510. if (!(req_events & (EPOLLOUT | EPOLLWRNORM | EPOLLIN | EPOLLRDNORM)))
  511. return rc;
  512. }
  513. src_q = v4l2_m2m_get_src_vq(m2m_ctx);
  514. dst_q = v4l2_m2m_get_dst_vq(m2m_ctx);
  515. /*
  516. * There has to be at least one buffer queued on each queued_list, which
  517. * means either in driver already or waiting for driver to claim it
  518. * and start processing.
  519. */
  520. if ((!src_q->streaming || list_empty(&src_q->queued_list))
  521. && (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
  522. rc |= EPOLLERR;
  523. goto end;
  524. }
  525. spin_lock_irqsave(&src_q->done_lock, flags);
  526. if (list_empty(&src_q->done_list))
  527. poll_wait(file, &src_q->done_wq, wait);
  528. spin_unlock_irqrestore(&src_q->done_lock, flags);
  529. spin_lock_irqsave(&dst_q->done_lock, flags);
  530. if (list_empty(&dst_q->done_list)) {
  531. /*
  532. * If the last buffer was dequeued from the capture queue,
  533. * return immediately. DQBUF will return -EPIPE.
  534. */
  535. if (dst_q->last_buffer_dequeued) {
  536. spin_unlock_irqrestore(&dst_q->done_lock, flags);
  537. return rc | EPOLLIN | EPOLLRDNORM;
  538. }
  539. poll_wait(file, &dst_q->done_wq, wait);
  540. }
  541. spin_unlock_irqrestore(&dst_q->done_lock, flags);
  542. spin_lock_irqsave(&src_q->done_lock, flags);
  543. if (!list_empty(&src_q->done_list))
  544. src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
  545. done_entry);
  546. if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
  547. || src_vb->state == VB2_BUF_STATE_ERROR))
  548. rc |= EPOLLOUT | EPOLLWRNORM;
  549. spin_unlock_irqrestore(&src_q->done_lock, flags);
  550. spin_lock_irqsave(&dst_q->done_lock, flags);
  551. if (!list_empty(&dst_q->done_list))
  552. dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
  553. done_entry);
  554. if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
  555. || dst_vb->state == VB2_BUF_STATE_ERROR))
  556. rc |= EPOLLIN | EPOLLRDNORM;
  557. spin_unlock_irqrestore(&dst_q->done_lock, flags);
  558. end:
  559. return rc;
  560. }
  561. EXPORT_SYMBOL_GPL(v4l2_m2m_poll);
  562. int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  563. struct vm_area_struct *vma)
  564. {
  565. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  566. struct vb2_queue *vq;
  567. if (offset < DST_QUEUE_OFF_BASE) {
  568. vq = v4l2_m2m_get_src_vq(m2m_ctx);
  569. } else {
  570. vq = v4l2_m2m_get_dst_vq(m2m_ctx);
  571. vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
  572. }
  573. return vb2_mmap(vq, vma);
  574. }
  575. EXPORT_SYMBOL(v4l2_m2m_mmap);
  576. #if defined(CONFIG_MEDIA_CONTROLLER)
  577. void v4l2_m2m_unregister_media_controller(struct v4l2_m2m_dev *m2m_dev)
  578. {
  579. media_remove_intf_links(&m2m_dev->intf_devnode->intf);
  580. media_devnode_remove(m2m_dev->intf_devnode);
  581. media_entity_remove_links(m2m_dev->source);
  582. media_entity_remove_links(&m2m_dev->sink);
  583. media_entity_remove_links(&m2m_dev->proc);
  584. media_device_unregister_entity(m2m_dev->source);
  585. media_device_unregister_entity(&m2m_dev->sink);
  586. media_device_unregister_entity(&m2m_dev->proc);
  587. kfree(m2m_dev->source->name);
  588. kfree(m2m_dev->sink.name);
  589. kfree(m2m_dev->proc.name);
  590. }
  591. EXPORT_SYMBOL_GPL(v4l2_m2m_unregister_media_controller);
  592. static int v4l2_m2m_register_entity(struct media_device *mdev,
  593. struct v4l2_m2m_dev *m2m_dev, enum v4l2_m2m_entity_type type,
  594. struct video_device *vdev, int function)
  595. {
  596. struct media_entity *entity;
  597. struct media_pad *pads;
  598. char *name;
  599. unsigned int len;
  600. int num_pads;
  601. int ret;
  602. switch (type) {
  603. case MEM2MEM_ENT_TYPE_SOURCE:
  604. entity = m2m_dev->source;
  605. pads = &m2m_dev->source_pad;
  606. pads[0].flags = MEDIA_PAD_FL_SOURCE;
  607. num_pads = 1;
  608. break;
  609. case MEM2MEM_ENT_TYPE_SINK:
  610. entity = &m2m_dev->sink;
  611. pads = &m2m_dev->sink_pad;
  612. pads[0].flags = MEDIA_PAD_FL_SINK;
  613. num_pads = 1;
  614. break;
  615. case MEM2MEM_ENT_TYPE_PROC:
  616. entity = &m2m_dev->proc;
  617. pads = m2m_dev->proc_pads;
  618. pads[0].flags = MEDIA_PAD_FL_SINK;
  619. pads[1].flags = MEDIA_PAD_FL_SOURCE;
  620. num_pads = 2;
  621. break;
  622. default:
  623. return -EINVAL;
  624. }
  625. entity->obj_type = MEDIA_ENTITY_TYPE_BASE;
  626. if (type != MEM2MEM_ENT_TYPE_PROC) {
  627. entity->info.dev.major = VIDEO_MAJOR;
  628. entity->info.dev.minor = vdev->minor;
  629. }
  630. len = strlen(vdev->name) + 2 + strlen(m2m_entity_name[type]);
  631. name = kmalloc(len, GFP_KERNEL);
  632. if (!name)
  633. return -ENOMEM;
  634. snprintf(name, len, "%s-%s", vdev->name, m2m_entity_name[type]);
  635. entity->name = name;
  636. entity->function = function;
  637. ret = media_entity_pads_init(entity, num_pads, pads);
  638. if (ret)
  639. return ret;
  640. ret = media_device_register_entity(mdev, entity);
  641. if (ret)
  642. return ret;
  643. return 0;
  644. }
  645. int v4l2_m2m_register_media_controller(struct v4l2_m2m_dev *m2m_dev,
  646. struct video_device *vdev, int function)
  647. {
  648. struct media_device *mdev = vdev->v4l2_dev->mdev;
  649. struct media_link *link;
  650. int ret;
  651. if (!mdev)
  652. return 0;
  653. /* A memory-to-memory device consists in two
  654. * DMA engine and one video processing entities.
  655. * The DMA engine entities are linked to a V4L interface
  656. */
  657. /* Create the three entities with their pads */
  658. m2m_dev->source = &vdev->entity;
  659. ret = v4l2_m2m_register_entity(mdev, m2m_dev,
  660. MEM2MEM_ENT_TYPE_SOURCE, vdev, MEDIA_ENT_F_IO_V4L);
  661. if (ret)
  662. return ret;
  663. ret = v4l2_m2m_register_entity(mdev, m2m_dev,
  664. MEM2MEM_ENT_TYPE_PROC, vdev, function);
  665. if (ret)
  666. goto err_rel_entity0;
  667. ret = v4l2_m2m_register_entity(mdev, m2m_dev,
  668. MEM2MEM_ENT_TYPE_SINK, vdev, MEDIA_ENT_F_IO_V4L);
  669. if (ret)
  670. goto err_rel_entity1;
  671. /* Connect the three entities */
  672. ret = media_create_pad_link(m2m_dev->source, 0, &m2m_dev->proc, 0,
  673. MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
  674. if (ret)
  675. goto err_rel_entity2;
  676. ret = media_create_pad_link(&m2m_dev->proc, 1, &m2m_dev->sink, 0,
  677. MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
  678. if (ret)
  679. goto err_rm_links0;
  680. /* Create video interface */
  681. m2m_dev->intf_devnode = media_devnode_create(mdev,
  682. MEDIA_INTF_T_V4L_VIDEO, 0,
  683. VIDEO_MAJOR, vdev->minor);
  684. if (!m2m_dev->intf_devnode) {
  685. ret = -ENOMEM;
  686. goto err_rm_links1;
  687. }
  688. /* Connect the two DMA engines to the interface */
  689. link = media_create_intf_link(m2m_dev->source,
  690. &m2m_dev->intf_devnode->intf,
  691. MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
  692. if (!link) {
  693. ret = -ENOMEM;
  694. goto err_rm_devnode;
  695. }
  696. link = media_create_intf_link(&m2m_dev->sink,
  697. &m2m_dev->intf_devnode->intf,
  698. MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
  699. if (!link) {
  700. ret = -ENOMEM;
  701. goto err_rm_intf_link;
  702. }
  703. return 0;
  704. err_rm_intf_link:
  705. media_remove_intf_links(&m2m_dev->intf_devnode->intf);
  706. err_rm_devnode:
  707. media_devnode_remove(m2m_dev->intf_devnode);
  708. err_rm_links1:
  709. media_entity_remove_links(&m2m_dev->sink);
  710. err_rm_links0:
  711. media_entity_remove_links(&m2m_dev->proc);
  712. media_entity_remove_links(m2m_dev->source);
  713. err_rel_entity2:
  714. media_device_unregister_entity(&m2m_dev->proc);
  715. kfree(m2m_dev->proc.name);
  716. err_rel_entity1:
  717. media_device_unregister_entity(&m2m_dev->sink);
  718. kfree(m2m_dev->sink.name);
  719. err_rel_entity0:
  720. media_device_unregister_entity(m2m_dev->source);
  721. kfree(m2m_dev->source->name);
  722. return ret;
  723. return 0;
  724. }
  725. EXPORT_SYMBOL_GPL(v4l2_m2m_register_media_controller);
  726. #endif
  727. struct v4l2_m2m_dev *v4l2_m2m_init(const struct v4l2_m2m_ops *m2m_ops)
  728. {
  729. struct v4l2_m2m_dev *m2m_dev;
  730. if (!m2m_ops || WARN_ON(!m2m_ops->device_run))
  731. return ERR_PTR(-EINVAL);
  732. m2m_dev = kzalloc(sizeof *m2m_dev, GFP_KERNEL);
  733. if (!m2m_dev)
  734. return ERR_PTR(-ENOMEM);
  735. m2m_dev->curr_ctx = NULL;
  736. m2m_dev->m2m_ops = m2m_ops;
  737. INIT_LIST_HEAD(&m2m_dev->job_queue);
  738. spin_lock_init(&m2m_dev->job_spinlock);
  739. return m2m_dev;
  740. }
  741. EXPORT_SYMBOL_GPL(v4l2_m2m_init);
  742. void v4l2_m2m_release(struct v4l2_m2m_dev *m2m_dev)
  743. {
  744. kfree(m2m_dev);
  745. }
  746. EXPORT_SYMBOL_GPL(v4l2_m2m_release);
  747. struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev,
  748. void *drv_priv,
  749. int (*queue_init)(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq))
  750. {
  751. struct v4l2_m2m_ctx *m2m_ctx;
  752. struct v4l2_m2m_queue_ctx *out_q_ctx, *cap_q_ctx;
  753. int ret;
  754. m2m_ctx = kzalloc(sizeof *m2m_ctx, GFP_KERNEL);
  755. if (!m2m_ctx)
  756. return ERR_PTR(-ENOMEM);
  757. m2m_ctx->priv = drv_priv;
  758. m2m_ctx->m2m_dev = m2m_dev;
  759. init_waitqueue_head(&m2m_ctx->finished);
  760. out_q_ctx = &m2m_ctx->out_q_ctx;
  761. cap_q_ctx = &m2m_ctx->cap_q_ctx;
  762. INIT_LIST_HEAD(&out_q_ctx->rdy_queue);
  763. INIT_LIST_HEAD(&cap_q_ctx->rdy_queue);
  764. spin_lock_init(&out_q_ctx->rdy_spinlock);
  765. spin_lock_init(&cap_q_ctx->rdy_spinlock);
  766. INIT_LIST_HEAD(&m2m_ctx->queue);
  767. ret = queue_init(drv_priv, &out_q_ctx->q, &cap_q_ctx->q);
  768. if (ret)
  769. goto err;
  770. /*
  771. * If both queues use same mutex assign it as the common buffer
  772. * queues lock to the m2m context. This lock is used in the
  773. * v4l2_m2m_ioctl_* helpers.
  774. */
  775. if (out_q_ctx->q.lock == cap_q_ctx->q.lock)
  776. m2m_ctx->q_lock = out_q_ctx->q.lock;
  777. return m2m_ctx;
  778. err:
  779. kfree(m2m_ctx);
  780. return ERR_PTR(ret);
  781. }
  782. EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_init);
  783. void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx *m2m_ctx)
  784. {
  785. /* wait until the current context is dequeued from job_queue */
  786. v4l2_m2m_cancel_job(m2m_ctx);
  787. vb2_queue_release(&m2m_ctx->cap_q_ctx.q);
  788. vb2_queue_release(&m2m_ctx->out_q_ctx.q);
  789. kfree(m2m_ctx);
  790. }
  791. EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_release);
  792. void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx *m2m_ctx,
  793. struct vb2_v4l2_buffer *vbuf)
  794. {
  795. struct v4l2_m2m_buffer *b = container_of(vbuf,
  796. struct v4l2_m2m_buffer, vb);
  797. struct v4l2_m2m_queue_ctx *q_ctx;
  798. unsigned long flags;
  799. q_ctx = get_queue_ctx(m2m_ctx, vbuf->vb2_buf.vb2_queue->type);
  800. if (!q_ctx)
  801. return;
  802. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  803. list_add_tail(&b->list, &q_ctx->rdy_queue);
  804. q_ctx->num_rdy++;
  805. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  806. }
  807. EXPORT_SYMBOL_GPL(v4l2_m2m_buf_queue);
  808. /* Videobuf2 ioctl helpers */
  809. int v4l2_m2m_ioctl_reqbufs(struct file *file, void *priv,
  810. struct v4l2_requestbuffers *rb)
  811. {
  812. struct v4l2_fh *fh = file->private_data;
  813. return v4l2_m2m_reqbufs(file, fh->m2m_ctx, rb);
  814. }
  815. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_reqbufs);
  816. int v4l2_m2m_ioctl_create_bufs(struct file *file, void *priv,
  817. struct v4l2_create_buffers *create)
  818. {
  819. struct v4l2_fh *fh = file->private_data;
  820. return v4l2_m2m_create_bufs(file, fh->m2m_ctx, create);
  821. }
  822. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_create_bufs);
  823. int v4l2_m2m_ioctl_querybuf(struct file *file, void *priv,
  824. struct v4l2_buffer *buf)
  825. {
  826. struct v4l2_fh *fh = file->private_data;
  827. return v4l2_m2m_querybuf(file, fh->m2m_ctx, buf);
  828. }
  829. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_querybuf);
  830. int v4l2_m2m_ioctl_qbuf(struct file *file, void *priv,
  831. struct v4l2_buffer *buf)
  832. {
  833. struct v4l2_fh *fh = file->private_data;
  834. return v4l2_m2m_qbuf(file, fh->m2m_ctx, buf);
  835. }
  836. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_qbuf);
  837. int v4l2_m2m_ioctl_dqbuf(struct file *file, void *priv,
  838. struct v4l2_buffer *buf)
  839. {
  840. struct v4l2_fh *fh = file->private_data;
  841. return v4l2_m2m_dqbuf(file, fh->m2m_ctx, buf);
  842. }
  843. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_dqbuf);
  844. int v4l2_m2m_ioctl_prepare_buf(struct file *file, void *priv,
  845. struct v4l2_buffer *buf)
  846. {
  847. struct v4l2_fh *fh = file->private_data;
  848. return v4l2_m2m_prepare_buf(file, fh->m2m_ctx, buf);
  849. }
  850. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_prepare_buf);
  851. int v4l2_m2m_ioctl_expbuf(struct file *file, void *priv,
  852. struct v4l2_exportbuffer *eb)
  853. {
  854. struct v4l2_fh *fh = file->private_data;
  855. return v4l2_m2m_expbuf(file, fh->m2m_ctx, eb);
  856. }
  857. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_expbuf);
  858. int v4l2_m2m_ioctl_streamon(struct file *file, void *priv,
  859. enum v4l2_buf_type type)
  860. {
  861. struct v4l2_fh *fh = file->private_data;
  862. return v4l2_m2m_streamon(file, fh->m2m_ctx, type);
  863. }
  864. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamon);
  865. int v4l2_m2m_ioctl_streamoff(struct file *file, void *priv,
  866. enum v4l2_buf_type type)
  867. {
  868. struct v4l2_fh *fh = file->private_data;
  869. return v4l2_m2m_streamoff(file, fh->m2m_ctx, type);
  870. }
  871. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamoff);
  872. /*
  873. * v4l2_file_operations helpers. It is assumed here same lock is used
  874. * for the output and the capture buffer queue.
  875. */
  876. int v4l2_m2m_fop_mmap(struct file *file, struct vm_area_struct *vma)
  877. {
  878. struct v4l2_fh *fh = file->private_data;
  879. return v4l2_m2m_mmap(file, fh->m2m_ctx, vma);
  880. }
  881. EXPORT_SYMBOL_GPL(v4l2_m2m_fop_mmap);
  882. __poll_t v4l2_m2m_fop_poll(struct file *file, poll_table *wait)
  883. {
  884. struct v4l2_fh *fh = file->private_data;
  885. struct v4l2_m2m_ctx *m2m_ctx = fh->m2m_ctx;
  886. __poll_t ret;
  887. if (m2m_ctx->q_lock)
  888. mutex_lock(m2m_ctx->q_lock);
  889. ret = v4l2_m2m_poll(file, m2m_ctx, wait);
  890. if (m2m_ctx->q_lock)
  891. mutex_unlock(m2m_ctx->q_lock);
  892. return ret;
  893. }
  894. EXPORT_SYMBOL_GPL(v4l2_m2m_fop_poll);