v4l2-mem2mem.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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-v4l2.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. struct vb2_queue *v4l2_m2m_get_vq(struct v4l2_m2m_ctx *m2m_ctx,
  66. enum v4l2_buf_type type)
  67. {
  68. struct v4l2_m2m_queue_ctx *q_ctx;
  69. q_ctx = get_queue_ctx(m2m_ctx, type);
  70. if (!q_ctx)
  71. return NULL;
  72. return &q_ctx->q;
  73. }
  74. EXPORT_SYMBOL(v4l2_m2m_get_vq);
  75. void *v4l2_m2m_next_buf(struct v4l2_m2m_queue_ctx *q_ctx)
  76. {
  77. struct v4l2_m2m_buffer *b;
  78. unsigned long flags;
  79. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  80. if (list_empty(&q_ctx->rdy_queue)) {
  81. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  82. return NULL;
  83. }
  84. b = list_first_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list);
  85. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  86. return &b->vb;
  87. }
  88. EXPORT_SYMBOL_GPL(v4l2_m2m_next_buf);
  89. void *v4l2_m2m_buf_remove(struct v4l2_m2m_queue_ctx *q_ctx)
  90. {
  91. struct v4l2_m2m_buffer *b;
  92. unsigned long flags;
  93. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  94. if (list_empty(&q_ctx->rdy_queue)) {
  95. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  96. return NULL;
  97. }
  98. b = list_first_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list);
  99. list_del(&b->list);
  100. q_ctx->num_rdy--;
  101. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  102. return &b->vb;
  103. }
  104. EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove);
  105. void v4l2_m2m_buf_remove_by_buf(struct v4l2_m2m_queue_ctx *q_ctx,
  106. struct vb2_v4l2_buffer *vbuf)
  107. {
  108. struct v4l2_m2m_buffer *b;
  109. unsigned long flags;
  110. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  111. b = container_of(vbuf, struct v4l2_m2m_buffer, vb);
  112. list_del(&b->list);
  113. q_ctx->num_rdy--;
  114. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  115. }
  116. EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove_by_buf);
  117. struct vb2_v4l2_buffer *
  118. v4l2_m2m_buf_remove_by_idx(struct v4l2_m2m_queue_ctx *q_ctx, unsigned int idx)
  119. {
  120. struct v4l2_m2m_buffer *b, *tmp;
  121. struct vb2_v4l2_buffer *ret = NULL;
  122. unsigned long flags;
  123. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  124. list_for_each_entry_safe(b, tmp, &q_ctx->rdy_queue, list) {
  125. if (b->vb.vb2_buf.index == idx) {
  126. list_del(&b->list);
  127. q_ctx->num_rdy--;
  128. ret = &b->vb;
  129. break;
  130. }
  131. }
  132. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  133. return ret;
  134. }
  135. EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove_by_idx);
  136. /*
  137. * Scheduling handlers
  138. */
  139. void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev *m2m_dev)
  140. {
  141. unsigned long flags;
  142. void *ret = NULL;
  143. spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
  144. if (m2m_dev->curr_ctx)
  145. ret = m2m_dev->curr_ctx->priv;
  146. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  147. return ret;
  148. }
  149. EXPORT_SYMBOL(v4l2_m2m_get_curr_priv);
  150. /**
  151. * v4l2_m2m_try_run() - select next job to perform and run it if possible
  152. * @m2m_dev: per-device context
  153. *
  154. * Get next transaction (if present) from the waiting jobs list and run it.
  155. */
  156. static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev)
  157. {
  158. unsigned long flags;
  159. spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
  160. if (NULL != m2m_dev->curr_ctx) {
  161. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  162. dprintk("Another instance is running, won't run now\n");
  163. return;
  164. }
  165. if (list_empty(&m2m_dev->job_queue)) {
  166. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  167. dprintk("No job pending\n");
  168. return;
  169. }
  170. m2m_dev->curr_ctx = list_first_entry(&m2m_dev->job_queue,
  171. struct v4l2_m2m_ctx, queue);
  172. m2m_dev->curr_ctx->job_flags |= TRANS_RUNNING;
  173. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  174. m2m_dev->m2m_ops->device_run(m2m_dev->curr_ctx->priv);
  175. }
  176. void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx)
  177. {
  178. struct v4l2_m2m_dev *m2m_dev;
  179. unsigned long flags_job, flags_out, flags_cap;
  180. m2m_dev = m2m_ctx->m2m_dev;
  181. dprintk("Trying to schedule a job for m2m_ctx: %p\n", m2m_ctx);
  182. if (!m2m_ctx->out_q_ctx.q.streaming
  183. || !m2m_ctx->cap_q_ctx.q.streaming) {
  184. dprintk("Streaming needs to be on for both queues\n");
  185. return;
  186. }
  187. spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job);
  188. /* If the context is aborted then don't schedule it */
  189. if (m2m_ctx->job_flags & TRANS_ABORT) {
  190. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  191. dprintk("Aborted context\n");
  192. return;
  193. }
  194. if (m2m_ctx->job_flags & TRANS_QUEUED) {
  195. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  196. dprintk("On job queue already\n");
  197. return;
  198. }
  199. spin_lock_irqsave(&m2m_ctx->out_q_ctx.rdy_spinlock, flags_out);
  200. if (list_empty(&m2m_ctx->out_q_ctx.rdy_queue)
  201. && !m2m_ctx->out_q_ctx.buffered) {
  202. spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock,
  203. flags_out);
  204. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  205. dprintk("No input buffers available\n");
  206. return;
  207. }
  208. spin_lock_irqsave(&m2m_ctx->cap_q_ctx.rdy_spinlock, flags_cap);
  209. if (list_empty(&m2m_ctx->cap_q_ctx.rdy_queue)
  210. && !m2m_ctx->cap_q_ctx.buffered) {
  211. spin_unlock_irqrestore(&m2m_ctx->cap_q_ctx.rdy_spinlock,
  212. flags_cap);
  213. spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock,
  214. flags_out);
  215. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  216. dprintk("No output buffers available\n");
  217. return;
  218. }
  219. spin_unlock_irqrestore(&m2m_ctx->cap_q_ctx.rdy_spinlock, flags_cap);
  220. spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock, flags_out);
  221. if (m2m_dev->m2m_ops->job_ready
  222. && (!m2m_dev->m2m_ops->job_ready(m2m_ctx->priv))) {
  223. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  224. dprintk("Driver not ready\n");
  225. return;
  226. }
  227. list_add_tail(&m2m_ctx->queue, &m2m_dev->job_queue);
  228. m2m_ctx->job_flags |= TRANS_QUEUED;
  229. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  230. v4l2_m2m_try_run(m2m_dev);
  231. }
  232. EXPORT_SYMBOL_GPL(v4l2_m2m_try_schedule);
  233. /**
  234. * v4l2_m2m_cancel_job() - cancel pending jobs for the context
  235. * @m2m_ctx: m2m context with jobs to be canceled
  236. *
  237. * In case of streamoff or release called on any context,
  238. * 1] If the context is currently running, then abort job will be called
  239. * 2] If the context is queued, then the context will be removed from
  240. * the job_queue
  241. */
  242. static void v4l2_m2m_cancel_job(struct v4l2_m2m_ctx *m2m_ctx)
  243. {
  244. struct v4l2_m2m_dev *m2m_dev;
  245. unsigned long flags;
  246. m2m_dev = m2m_ctx->m2m_dev;
  247. spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
  248. m2m_ctx->job_flags |= TRANS_ABORT;
  249. if (m2m_ctx->job_flags & TRANS_RUNNING) {
  250. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  251. m2m_dev->m2m_ops->job_abort(m2m_ctx->priv);
  252. dprintk("m2m_ctx %p running, will wait to complete", m2m_ctx);
  253. wait_event(m2m_ctx->finished,
  254. !(m2m_ctx->job_flags & TRANS_RUNNING));
  255. } else if (m2m_ctx->job_flags & TRANS_QUEUED) {
  256. list_del(&m2m_ctx->queue);
  257. m2m_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING);
  258. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  259. dprintk("m2m_ctx: %p had been on queue and was removed\n",
  260. m2m_ctx);
  261. } else {
  262. /* Do nothing, was not on queue/running */
  263. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  264. }
  265. }
  266. void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev,
  267. struct v4l2_m2m_ctx *m2m_ctx)
  268. {
  269. unsigned long flags;
  270. spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
  271. if (!m2m_dev->curr_ctx || m2m_dev->curr_ctx != m2m_ctx) {
  272. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  273. dprintk("Called by an instance not currently running\n");
  274. return;
  275. }
  276. list_del(&m2m_dev->curr_ctx->queue);
  277. m2m_dev->curr_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING);
  278. wake_up(&m2m_dev->curr_ctx->finished);
  279. m2m_dev->curr_ctx = NULL;
  280. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  281. /* This instance might have more buffers ready, but since we do not
  282. * allow more than one job on the job_queue per instance, each has
  283. * to be scheduled separately after the previous one finishes. */
  284. v4l2_m2m_try_schedule(m2m_ctx);
  285. }
  286. EXPORT_SYMBOL(v4l2_m2m_job_finish);
  287. int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  288. struct v4l2_requestbuffers *reqbufs)
  289. {
  290. struct vb2_queue *vq;
  291. int ret;
  292. vq = v4l2_m2m_get_vq(m2m_ctx, reqbufs->type);
  293. ret = vb2_reqbufs(vq, reqbufs);
  294. /* If count == 0, then the owner has released all buffers and he
  295. is no longer owner of the queue. Otherwise we have an owner. */
  296. if (ret == 0)
  297. vq->owner = reqbufs->count ? file->private_data : NULL;
  298. return ret;
  299. }
  300. EXPORT_SYMBOL_GPL(v4l2_m2m_reqbufs);
  301. int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  302. struct v4l2_buffer *buf)
  303. {
  304. struct vb2_queue *vq;
  305. int ret = 0;
  306. unsigned int i;
  307. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  308. ret = vb2_querybuf(vq, buf);
  309. /* Adjust MMAP memory offsets for the CAPTURE queue */
  310. if (buf->memory == V4L2_MEMORY_MMAP && !V4L2_TYPE_IS_OUTPUT(vq->type)) {
  311. if (V4L2_TYPE_IS_MULTIPLANAR(vq->type)) {
  312. for (i = 0; i < buf->length; ++i)
  313. buf->m.planes[i].m.mem_offset
  314. += DST_QUEUE_OFF_BASE;
  315. } else {
  316. buf->m.offset += DST_QUEUE_OFF_BASE;
  317. }
  318. }
  319. return ret;
  320. }
  321. EXPORT_SYMBOL_GPL(v4l2_m2m_querybuf);
  322. int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  323. struct v4l2_buffer *buf)
  324. {
  325. struct vb2_queue *vq;
  326. int ret;
  327. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  328. ret = vb2_qbuf(vq, buf);
  329. if (!ret)
  330. v4l2_m2m_try_schedule(m2m_ctx);
  331. return ret;
  332. }
  333. EXPORT_SYMBOL_GPL(v4l2_m2m_qbuf);
  334. int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  335. struct v4l2_buffer *buf)
  336. {
  337. struct vb2_queue *vq;
  338. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  339. return vb2_dqbuf(vq, buf, file->f_flags & O_NONBLOCK);
  340. }
  341. EXPORT_SYMBOL_GPL(v4l2_m2m_dqbuf);
  342. int v4l2_m2m_prepare_buf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  343. struct v4l2_buffer *buf)
  344. {
  345. struct vb2_queue *vq;
  346. int ret;
  347. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  348. ret = vb2_prepare_buf(vq, buf);
  349. if (!ret)
  350. v4l2_m2m_try_schedule(m2m_ctx);
  351. return ret;
  352. }
  353. EXPORT_SYMBOL_GPL(v4l2_m2m_prepare_buf);
  354. int v4l2_m2m_create_bufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  355. struct v4l2_create_buffers *create)
  356. {
  357. struct vb2_queue *vq;
  358. vq = v4l2_m2m_get_vq(m2m_ctx, create->format.type);
  359. return vb2_create_bufs(vq, create);
  360. }
  361. EXPORT_SYMBOL_GPL(v4l2_m2m_create_bufs);
  362. int v4l2_m2m_expbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  363. struct v4l2_exportbuffer *eb)
  364. {
  365. struct vb2_queue *vq;
  366. vq = v4l2_m2m_get_vq(m2m_ctx, eb->type);
  367. return vb2_expbuf(vq, eb);
  368. }
  369. EXPORT_SYMBOL_GPL(v4l2_m2m_expbuf);
  370. int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  371. enum v4l2_buf_type type)
  372. {
  373. struct vb2_queue *vq;
  374. int ret;
  375. vq = v4l2_m2m_get_vq(m2m_ctx, type);
  376. ret = vb2_streamon(vq, type);
  377. if (!ret)
  378. v4l2_m2m_try_schedule(m2m_ctx);
  379. return ret;
  380. }
  381. EXPORT_SYMBOL_GPL(v4l2_m2m_streamon);
  382. int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  383. enum v4l2_buf_type type)
  384. {
  385. struct v4l2_m2m_dev *m2m_dev;
  386. struct v4l2_m2m_queue_ctx *q_ctx;
  387. unsigned long flags_job, flags;
  388. int ret;
  389. /* wait until the current context is dequeued from job_queue */
  390. v4l2_m2m_cancel_job(m2m_ctx);
  391. q_ctx = get_queue_ctx(m2m_ctx, type);
  392. ret = vb2_streamoff(&q_ctx->q, type);
  393. if (ret)
  394. return ret;
  395. m2m_dev = m2m_ctx->m2m_dev;
  396. spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job);
  397. /* We should not be scheduled anymore, since we're dropping a queue. */
  398. if (m2m_ctx->job_flags & TRANS_QUEUED)
  399. list_del(&m2m_ctx->queue);
  400. m2m_ctx->job_flags = 0;
  401. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  402. /* Drop queue, since streamoff returns device to the same state as after
  403. * calling reqbufs. */
  404. INIT_LIST_HEAD(&q_ctx->rdy_queue);
  405. q_ctx->num_rdy = 0;
  406. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  407. if (m2m_dev->curr_ctx == m2m_ctx) {
  408. m2m_dev->curr_ctx = NULL;
  409. wake_up(&m2m_ctx->finished);
  410. }
  411. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  412. return 0;
  413. }
  414. EXPORT_SYMBOL_GPL(v4l2_m2m_streamoff);
  415. __poll_t v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  416. struct poll_table_struct *wait)
  417. {
  418. struct video_device *vfd = video_devdata(file);
  419. __poll_t req_events = poll_requested_events(wait);
  420. struct vb2_queue *src_q, *dst_q;
  421. struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
  422. __poll_t rc = 0;
  423. unsigned long flags;
  424. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
  425. struct v4l2_fh *fh = file->private_data;
  426. if (v4l2_event_pending(fh))
  427. rc = EPOLLPRI;
  428. else if (req_events & EPOLLPRI)
  429. poll_wait(file, &fh->wait, wait);
  430. if (!(req_events & (EPOLLOUT | EPOLLWRNORM | EPOLLIN | EPOLLRDNORM)))
  431. return rc;
  432. }
  433. src_q = v4l2_m2m_get_src_vq(m2m_ctx);
  434. dst_q = v4l2_m2m_get_dst_vq(m2m_ctx);
  435. /*
  436. * There has to be at least one buffer queued on each queued_list, which
  437. * means either in driver already or waiting for driver to claim it
  438. * and start processing.
  439. */
  440. if ((!src_q->streaming || list_empty(&src_q->queued_list))
  441. && (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
  442. rc |= EPOLLERR;
  443. goto end;
  444. }
  445. spin_lock_irqsave(&src_q->done_lock, flags);
  446. if (list_empty(&src_q->done_list))
  447. poll_wait(file, &src_q->done_wq, wait);
  448. spin_unlock_irqrestore(&src_q->done_lock, flags);
  449. spin_lock_irqsave(&dst_q->done_lock, flags);
  450. if (list_empty(&dst_q->done_list)) {
  451. /*
  452. * If the last buffer was dequeued from the capture queue,
  453. * return immediately. DQBUF will return -EPIPE.
  454. */
  455. if (dst_q->last_buffer_dequeued) {
  456. spin_unlock_irqrestore(&dst_q->done_lock, flags);
  457. return rc | EPOLLIN | EPOLLRDNORM;
  458. }
  459. poll_wait(file, &dst_q->done_wq, wait);
  460. }
  461. spin_unlock_irqrestore(&dst_q->done_lock, flags);
  462. spin_lock_irqsave(&src_q->done_lock, flags);
  463. if (!list_empty(&src_q->done_list))
  464. src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
  465. done_entry);
  466. if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
  467. || src_vb->state == VB2_BUF_STATE_ERROR))
  468. rc |= EPOLLOUT | EPOLLWRNORM;
  469. spin_unlock_irqrestore(&src_q->done_lock, flags);
  470. spin_lock_irqsave(&dst_q->done_lock, flags);
  471. if (!list_empty(&dst_q->done_list))
  472. dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
  473. done_entry);
  474. if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
  475. || dst_vb->state == VB2_BUF_STATE_ERROR))
  476. rc |= EPOLLIN | EPOLLRDNORM;
  477. spin_unlock_irqrestore(&dst_q->done_lock, flags);
  478. end:
  479. return rc;
  480. }
  481. EXPORT_SYMBOL_GPL(v4l2_m2m_poll);
  482. int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  483. struct vm_area_struct *vma)
  484. {
  485. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  486. struct vb2_queue *vq;
  487. if (offset < DST_QUEUE_OFF_BASE) {
  488. vq = v4l2_m2m_get_src_vq(m2m_ctx);
  489. } else {
  490. vq = v4l2_m2m_get_dst_vq(m2m_ctx);
  491. vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
  492. }
  493. return vb2_mmap(vq, vma);
  494. }
  495. EXPORT_SYMBOL(v4l2_m2m_mmap);
  496. struct v4l2_m2m_dev *v4l2_m2m_init(const struct v4l2_m2m_ops *m2m_ops)
  497. {
  498. struct v4l2_m2m_dev *m2m_dev;
  499. if (!m2m_ops || WARN_ON(!m2m_ops->device_run) ||
  500. WARN_ON(!m2m_ops->job_abort))
  501. return ERR_PTR(-EINVAL);
  502. m2m_dev = kzalloc(sizeof *m2m_dev, GFP_KERNEL);
  503. if (!m2m_dev)
  504. return ERR_PTR(-ENOMEM);
  505. m2m_dev->curr_ctx = NULL;
  506. m2m_dev->m2m_ops = m2m_ops;
  507. INIT_LIST_HEAD(&m2m_dev->job_queue);
  508. spin_lock_init(&m2m_dev->job_spinlock);
  509. return m2m_dev;
  510. }
  511. EXPORT_SYMBOL_GPL(v4l2_m2m_init);
  512. void v4l2_m2m_release(struct v4l2_m2m_dev *m2m_dev)
  513. {
  514. kfree(m2m_dev);
  515. }
  516. EXPORT_SYMBOL_GPL(v4l2_m2m_release);
  517. struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev,
  518. void *drv_priv,
  519. int (*queue_init)(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq))
  520. {
  521. struct v4l2_m2m_ctx *m2m_ctx;
  522. struct v4l2_m2m_queue_ctx *out_q_ctx, *cap_q_ctx;
  523. int ret;
  524. m2m_ctx = kzalloc(sizeof *m2m_ctx, GFP_KERNEL);
  525. if (!m2m_ctx)
  526. return ERR_PTR(-ENOMEM);
  527. m2m_ctx->priv = drv_priv;
  528. m2m_ctx->m2m_dev = m2m_dev;
  529. init_waitqueue_head(&m2m_ctx->finished);
  530. out_q_ctx = &m2m_ctx->out_q_ctx;
  531. cap_q_ctx = &m2m_ctx->cap_q_ctx;
  532. INIT_LIST_HEAD(&out_q_ctx->rdy_queue);
  533. INIT_LIST_HEAD(&cap_q_ctx->rdy_queue);
  534. spin_lock_init(&out_q_ctx->rdy_spinlock);
  535. spin_lock_init(&cap_q_ctx->rdy_spinlock);
  536. INIT_LIST_HEAD(&m2m_ctx->queue);
  537. ret = queue_init(drv_priv, &out_q_ctx->q, &cap_q_ctx->q);
  538. if (ret)
  539. goto err;
  540. /*
  541. * If both queues use same mutex assign it as the common buffer
  542. * queues lock to the m2m context. This lock is used in the
  543. * v4l2_m2m_ioctl_* helpers.
  544. */
  545. if (out_q_ctx->q.lock == cap_q_ctx->q.lock)
  546. m2m_ctx->q_lock = out_q_ctx->q.lock;
  547. return m2m_ctx;
  548. err:
  549. kfree(m2m_ctx);
  550. return ERR_PTR(ret);
  551. }
  552. EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_init);
  553. void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx *m2m_ctx)
  554. {
  555. /* wait until the current context is dequeued from job_queue */
  556. v4l2_m2m_cancel_job(m2m_ctx);
  557. vb2_queue_release(&m2m_ctx->cap_q_ctx.q);
  558. vb2_queue_release(&m2m_ctx->out_q_ctx.q);
  559. kfree(m2m_ctx);
  560. }
  561. EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_release);
  562. void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx *m2m_ctx,
  563. struct vb2_v4l2_buffer *vbuf)
  564. {
  565. struct v4l2_m2m_buffer *b = container_of(vbuf,
  566. struct v4l2_m2m_buffer, vb);
  567. struct v4l2_m2m_queue_ctx *q_ctx;
  568. unsigned long flags;
  569. q_ctx = get_queue_ctx(m2m_ctx, vbuf->vb2_buf.vb2_queue->type);
  570. if (!q_ctx)
  571. return;
  572. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  573. list_add_tail(&b->list, &q_ctx->rdy_queue);
  574. q_ctx->num_rdy++;
  575. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  576. }
  577. EXPORT_SYMBOL_GPL(v4l2_m2m_buf_queue);
  578. /* Videobuf2 ioctl helpers */
  579. int v4l2_m2m_ioctl_reqbufs(struct file *file, void *priv,
  580. struct v4l2_requestbuffers *rb)
  581. {
  582. struct v4l2_fh *fh = file->private_data;
  583. return v4l2_m2m_reqbufs(file, fh->m2m_ctx, rb);
  584. }
  585. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_reqbufs);
  586. int v4l2_m2m_ioctl_create_bufs(struct file *file, void *priv,
  587. struct v4l2_create_buffers *create)
  588. {
  589. struct v4l2_fh *fh = file->private_data;
  590. return v4l2_m2m_create_bufs(file, fh->m2m_ctx, create);
  591. }
  592. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_create_bufs);
  593. int v4l2_m2m_ioctl_querybuf(struct file *file, void *priv,
  594. struct v4l2_buffer *buf)
  595. {
  596. struct v4l2_fh *fh = file->private_data;
  597. return v4l2_m2m_querybuf(file, fh->m2m_ctx, buf);
  598. }
  599. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_querybuf);
  600. int v4l2_m2m_ioctl_qbuf(struct file *file, void *priv,
  601. struct v4l2_buffer *buf)
  602. {
  603. struct v4l2_fh *fh = file->private_data;
  604. return v4l2_m2m_qbuf(file, fh->m2m_ctx, buf);
  605. }
  606. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_qbuf);
  607. int v4l2_m2m_ioctl_dqbuf(struct file *file, void *priv,
  608. struct v4l2_buffer *buf)
  609. {
  610. struct v4l2_fh *fh = file->private_data;
  611. return v4l2_m2m_dqbuf(file, fh->m2m_ctx, buf);
  612. }
  613. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_dqbuf);
  614. int v4l2_m2m_ioctl_prepare_buf(struct file *file, void *priv,
  615. struct v4l2_buffer *buf)
  616. {
  617. struct v4l2_fh *fh = file->private_data;
  618. return v4l2_m2m_prepare_buf(file, fh->m2m_ctx, buf);
  619. }
  620. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_prepare_buf);
  621. int v4l2_m2m_ioctl_expbuf(struct file *file, void *priv,
  622. struct v4l2_exportbuffer *eb)
  623. {
  624. struct v4l2_fh *fh = file->private_data;
  625. return v4l2_m2m_expbuf(file, fh->m2m_ctx, eb);
  626. }
  627. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_expbuf);
  628. int v4l2_m2m_ioctl_streamon(struct file *file, void *priv,
  629. enum v4l2_buf_type type)
  630. {
  631. struct v4l2_fh *fh = file->private_data;
  632. return v4l2_m2m_streamon(file, fh->m2m_ctx, type);
  633. }
  634. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamon);
  635. int v4l2_m2m_ioctl_streamoff(struct file *file, void *priv,
  636. enum v4l2_buf_type type)
  637. {
  638. struct v4l2_fh *fh = file->private_data;
  639. return v4l2_m2m_streamoff(file, fh->m2m_ctx, type);
  640. }
  641. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamoff);
  642. /*
  643. * v4l2_file_operations helpers. It is assumed here same lock is used
  644. * for the output and the capture buffer queue.
  645. */
  646. int v4l2_m2m_fop_mmap(struct file *file, struct vm_area_struct *vma)
  647. {
  648. struct v4l2_fh *fh = file->private_data;
  649. return v4l2_m2m_mmap(file, fh->m2m_ctx, vma);
  650. }
  651. EXPORT_SYMBOL_GPL(v4l2_m2m_fop_mmap);
  652. __poll_t v4l2_m2m_fop_poll(struct file *file, poll_table *wait)
  653. {
  654. struct v4l2_fh *fh = file->private_data;
  655. struct v4l2_m2m_ctx *m2m_ctx = fh->m2m_ctx;
  656. __poll_t ret;
  657. if (m2m_ctx->q_lock)
  658. mutex_lock(m2m_ctx->q_lock);
  659. ret = v4l2_m2m_poll(file, m2m_ctx, wait);
  660. if (m2m_ctx->q_lock)
  661. mutex_unlock(m2m_ctx->q_lock);
  662. return ret;
  663. }
  664. EXPORT_SYMBOL_GPL(v4l2_m2m_fop_poll);