v4l2-mem2mem.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. v4l2_m2m_try_run(m2m_dev);
  286. }
  287. EXPORT_SYMBOL(v4l2_m2m_job_finish);
  288. int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  289. struct v4l2_requestbuffers *reqbufs)
  290. {
  291. struct vb2_queue *vq;
  292. int ret;
  293. vq = v4l2_m2m_get_vq(m2m_ctx, reqbufs->type);
  294. ret = vb2_reqbufs(vq, reqbufs);
  295. /* If count == 0, then the owner has released all buffers and he
  296. is no longer owner of the queue. Otherwise we have an owner. */
  297. if (ret == 0)
  298. vq->owner = reqbufs->count ? file->private_data : NULL;
  299. return ret;
  300. }
  301. EXPORT_SYMBOL_GPL(v4l2_m2m_reqbufs);
  302. int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  303. struct v4l2_buffer *buf)
  304. {
  305. struct vb2_queue *vq;
  306. int ret = 0;
  307. unsigned int i;
  308. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  309. ret = vb2_querybuf(vq, buf);
  310. /* Adjust MMAP memory offsets for the CAPTURE queue */
  311. if (buf->memory == V4L2_MEMORY_MMAP && !V4L2_TYPE_IS_OUTPUT(vq->type)) {
  312. if (V4L2_TYPE_IS_MULTIPLANAR(vq->type)) {
  313. for (i = 0; i < buf->length; ++i)
  314. buf->m.planes[i].m.mem_offset
  315. += DST_QUEUE_OFF_BASE;
  316. } else {
  317. buf->m.offset += DST_QUEUE_OFF_BASE;
  318. }
  319. }
  320. return ret;
  321. }
  322. EXPORT_SYMBOL_GPL(v4l2_m2m_querybuf);
  323. int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  324. struct v4l2_buffer *buf)
  325. {
  326. struct vb2_queue *vq;
  327. int ret;
  328. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  329. ret = vb2_qbuf(vq, buf);
  330. if (!ret)
  331. v4l2_m2m_try_schedule(m2m_ctx);
  332. return ret;
  333. }
  334. EXPORT_SYMBOL_GPL(v4l2_m2m_qbuf);
  335. int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  336. struct v4l2_buffer *buf)
  337. {
  338. struct vb2_queue *vq;
  339. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  340. return vb2_dqbuf(vq, buf, file->f_flags & O_NONBLOCK);
  341. }
  342. EXPORT_SYMBOL_GPL(v4l2_m2m_dqbuf);
  343. int v4l2_m2m_prepare_buf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  344. struct v4l2_buffer *buf)
  345. {
  346. struct vb2_queue *vq;
  347. int ret;
  348. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  349. ret = vb2_prepare_buf(vq, buf);
  350. if (!ret)
  351. v4l2_m2m_try_schedule(m2m_ctx);
  352. return ret;
  353. }
  354. EXPORT_SYMBOL_GPL(v4l2_m2m_prepare_buf);
  355. int v4l2_m2m_create_bufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  356. struct v4l2_create_buffers *create)
  357. {
  358. struct vb2_queue *vq;
  359. vq = v4l2_m2m_get_vq(m2m_ctx, create->format.type);
  360. return vb2_create_bufs(vq, create);
  361. }
  362. EXPORT_SYMBOL_GPL(v4l2_m2m_create_bufs);
  363. int v4l2_m2m_expbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  364. struct v4l2_exportbuffer *eb)
  365. {
  366. struct vb2_queue *vq;
  367. vq = v4l2_m2m_get_vq(m2m_ctx, eb->type);
  368. return vb2_expbuf(vq, eb);
  369. }
  370. EXPORT_SYMBOL_GPL(v4l2_m2m_expbuf);
  371. int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  372. enum v4l2_buf_type type)
  373. {
  374. struct vb2_queue *vq;
  375. int ret;
  376. vq = v4l2_m2m_get_vq(m2m_ctx, type);
  377. ret = vb2_streamon(vq, type);
  378. if (!ret)
  379. v4l2_m2m_try_schedule(m2m_ctx);
  380. return ret;
  381. }
  382. EXPORT_SYMBOL_GPL(v4l2_m2m_streamon);
  383. int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  384. enum v4l2_buf_type type)
  385. {
  386. struct v4l2_m2m_dev *m2m_dev;
  387. struct v4l2_m2m_queue_ctx *q_ctx;
  388. unsigned long flags_job, flags;
  389. int ret;
  390. /* wait until the current context is dequeued from job_queue */
  391. v4l2_m2m_cancel_job(m2m_ctx);
  392. q_ctx = get_queue_ctx(m2m_ctx, type);
  393. ret = vb2_streamoff(&q_ctx->q, type);
  394. if (ret)
  395. return ret;
  396. m2m_dev = m2m_ctx->m2m_dev;
  397. spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job);
  398. /* We should not be scheduled anymore, since we're dropping a queue. */
  399. if (m2m_ctx->job_flags & TRANS_QUEUED)
  400. list_del(&m2m_ctx->queue);
  401. m2m_ctx->job_flags = 0;
  402. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  403. /* Drop queue, since streamoff returns device to the same state as after
  404. * calling reqbufs. */
  405. INIT_LIST_HEAD(&q_ctx->rdy_queue);
  406. q_ctx->num_rdy = 0;
  407. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  408. if (m2m_dev->curr_ctx == m2m_ctx) {
  409. m2m_dev->curr_ctx = NULL;
  410. wake_up(&m2m_ctx->finished);
  411. }
  412. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  413. return 0;
  414. }
  415. EXPORT_SYMBOL_GPL(v4l2_m2m_streamoff);
  416. __poll_t v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  417. struct poll_table_struct *wait)
  418. {
  419. struct video_device *vfd = video_devdata(file);
  420. __poll_t req_events = poll_requested_events(wait);
  421. struct vb2_queue *src_q, *dst_q;
  422. struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
  423. __poll_t rc = 0;
  424. unsigned long flags;
  425. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
  426. struct v4l2_fh *fh = file->private_data;
  427. if (v4l2_event_pending(fh))
  428. rc = EPOLLPRI;
  429. else if (req_events & EPOLLPRI)
  430. poll_wait(file, &fh->wait, wait);
  431. if (!(req_events & (EPOLLOUT | EPOLLWRNORM | EPOLLIN | EPOLLRDNORM)))
  432. return rc;
  433. }
  434. src_q = v4l2_m2m_get_src_vq(m2m_ctx);
  435. dst_q = v4l2_m2m_get_dst_vq(m2m_ctx);
  436. /*
  437. * There has to be at least one buffer queued on each queued_list, which
  438. * means either in driver already or waiting for driver to claim it
  439. * and start processing.
  440. */
  441. if ((!src_q->streaming || list_empty(&src_q->queued_list))
  442. && (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
  443. rc |= EPOLLERR;
  444. goto end;
  445. }
  446. spin_lock_irqsave(&src_q->done_lock, flags);
  447. if (list_empty(&src_q->done_list))
  448. poll_wait(file, &src_q->done_wq, wait);
  449. spin_unlock_irqrestore(&src_q->done_lock, flags);
  450. spin_lock_irqsave(&dst_q->done_lock, flags);
  451. if (list_empty(&dst_q->done_list)) {
  452. /*
  453. * If the last buffer was dequeued from the capture queue,
  454. * return immediately. DQBUF will return -EPIPE.
  455. */
  456. if (dst_q->last_buffer_dequeued) {
  457. spin_unlock_irqrestore(&dst_q->done_lock, flags);
  458. return rc | EPOLLIN | EPOLLRDNORM;
  459. }
  460. poll_wait(file, &dst_q->done_wq, wait);
  461. }
  462. spin_unlock_irqrestore(&dst_q->done_lock, flags);
  463. spin_lock_irqsave(&src_q->done_lock, flags);
  464. if (!list_empty(&src_q->done_list))
  465. src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
  466. done_entry);
  467. if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
  468. || src_vb->state == VB2_BUF_STATE_ERROR))
  469. rc |= EPOLLOUT | EPOLLWRNORM;
  470. spin_unlock_irqrestore(&src_q->done_lock, flags);
  471. spin_lock_irqsave(&dst_q->done_lock, flags);
  472. if (!list_empty(&dst_q->done_list))
  473. dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
  474. done_entry);
  475. if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
  476. || dst_vb->state == VB2_BUF_STATE_ERROR))
  477. rc |= EPOLLIN | EPOLLRDNORM;
  478. spin_unlock_irqrestore(&dst_q->done_lock, flags);
  479. end:
  480. return rc;
  481. }
  482. EXPORT_SYMBOL_GPL(v4l2_m2m_poll);
  483. int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  484. struct vm_area_struct *vma)
  485. {
  486. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  487. struct vb2_queue *vq;
  488. if (offset < DST_QUEUE_OFF_BASE) {
  489. vq = v4l2_m2m_get_src_vq(m2m_ctx);
  490. } else {
  491. vq = v4l2_m2m_get_dst_vq(m2m_ctx);
  492. vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
  493. }
  494. return vb2_mmap(vq, vma);
  495. }
  496. EXPORT_SYMBOL(v4l2_m2m_mmap);
  497. struct v4l2_m2m_dev *v4l2_m2m_init(const struct v4l2_m2m_ops *m2m_ops)
  498. {
  499. struct v4l2_m2m_dev *m2m_dev;
  500. if (!m2m_ops || WARN_ON(!m2m_ops->device_run) ||
  501. WARN_ON(!m2m_ops->job_abort))
  502. return ERR_PTR(-EINVAL);
  503. m2m_dev = kzalloc(sizeof *m2m_dev, GFP_KERNEL);
  504. if (!m2m_dev)
  505. return ERR_PTR(-ENOMEM);
  506. m2m_dev->curr_ctx = NULL;
  507. m2m_dev->m2m_ops = m2m_ops;
  508. INIT_LIST_HEAD(&m2m_dev->job_queue);
  509. spin_lock_init(&m2m_dev->job_spinlock);
  510. return m2m_dev;
  511. }
  512. EXPORT_SYMBOL_GPL(v4l2_m2m_init);
  513. void v4l2_m2m_release(struct v4l2_m2m_dev *m2m_dev)
  514. {
  515. kfree(m2m_dev);
  516. }
  517. EXPORT_SYMBOL_GPL(v4l2_m2m_release);
  518. struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev,
  519. void *drv_priv,
  520. int (*queue_init)(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq))
  521. {
  522. struct v4l2_m2m_ctx *m2m_ctx;
  523. struct v4l2_m2m_queue_ctx *out_q_ctx, *cap_q_ctx;
  524. int ret;
  525. m2m_ctx = kzalloc(sizeof *m2m_ctx, GFP_KERNEL);
  526. if (!m2m_ctx)
  527. return ERR_PTR(-ENOMEM);
  528. m2m_ctx->priv = drv_priv;
  529. m2m_ctx->m2m_dev = m2m_dev;
  530. init_waitqueue_head(&m2m_ctx->finished);
  531. out_q_ctx = &m2m_ctx->out_q_ctx;
  532. cap_q_ctx = &m2m_ctx->cap_q_ctx;
  533. INIT_LIST_HEAD(&out_q_ctx->rdy_queue);
  534. INIT_LIST_HEAD(&cap_q_ctx->rdy_queue);
  535. spin_lock_init(&out_q_ctx->rdy_spinlock);
  536. spin_lock_init(&cap_q_ctx->rdy_spinlock);
  537. INIT_LIST_HEAD(&m2m_ctx->queue);
  538. ret = queue_init(drv_priv, &out_q_ctx->q, &cap_q_ctx->q);
  539. if (ret)
  540. goto err;
  541. /*
  542. * If both queues use same mutex assign it as the common buffer
  543. * queues lock to the m2m context. This lock is used in the
  544. * v4l2_m2m_ioctl_* helpers.
  545. */
  546. if (out_q_ctx->q.lock == cap_q_ctx->q.lock)
  547. m2m_ctx->q_lock = out_q_ctx->q.lock;
  548. return m2m_ctx;
  549. err:
  550. kfree(m2m_ctx);
  551. return ERR_PTR(ret);
  552. }
  553. EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_init);
  554. void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx *m2m_ctx)
  555. {
  556. /* wait until the current context is dequeued from job_queue */
  557. v4l2_m2m_cancel_job(m2m_ctx);
  558. vb2_queue_release(&m2m_ctx->cap_q_ctx.q);
  559. vb2_queue_release(&m2m_ctx->out_q_ctx.q);
  560. kfree(m2m_ctx);
  561. }
  562. EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_release);
  563. void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx *m2m_ctx,
  564. struct vb2_v4l2_buffer *vbuf)
  565. {
  566. struct v4l2_m2m_buffer *b = container_of(vbuf,
  567. struct v4l2_m2m_buffer, vb);
  568. struct v4l2_m2m_queue_ctx *q_ctx;
  569. unsigned long flags;
  570. q_ctx = get_queue_ctx(m2m_ctx, vbuf->vb2_buf.vb2_queue->type);
  571. if (!q_ctx)
  572. return;
  573. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  574. list_add_tail(&b->list, &q_ctx->rdy_queue);
  575. q_ctx->num_rdy++;
  576. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  577. }
  578. EXPORT_SYMBOL_GPL(v4l2_m2m_buf_queue);
  579. /* Videobuf2 ioctl helpers */
  580. int v4l2_m2m_ioctl_reqbufs(struct file *file, void *priv,
  581. struct v4l2_requestbuffers *rb)
  582. {
  583. struct v4l2_fh *fh = file->private_data;
  584. return v4l2_m2m_reqbufs(file, fh->m2m_ctx, rb);
  585. }
  586. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_reqbufs);
  587. int v4l2_m2m_ioctl_create_bufs(struct file *file, void *priv,
  588. struct v4l2_create_buffers *create)
  589. {
  590. struct v4l2_fh *fh = file->private_data;
  591. return v4l2_m2m_create_bufs(file, fh->m2m_ctx, create);
  592. }
  593. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_create_bufs);
  594. int v4l2_m2m_ioctl_querybuf(struct file *file, void *priv,
  595. struct v4l2_buffer *buf)
  596. {
  597. struct v4l2_fh *fh = file->private_data;
  598. return v4l2_m2m_querybuf(file, fh->m2m_ctx, buf);
  599. }
  600. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_querybuf);
  601. int v4l2_m2m_ioctl_qbuf(struct file *file, void *priv,
  602. struct v4l2_buffer *buf)
  603. {
  604. struct v4l2_fh *fh = file->private_data;
  605. return v4l2_m2m_qbuf(file, fh->m2m_ctx, buf);
  606. }
  607. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_qbuf);
  608. int v4l2_m2m_ioctl_dqbuf(struct file *file, void *priv,
  609. struct v4l2_buffer *buf)
  610. {
  611. struct v4l2_fh *fh = file->private_data;
  612. return v4l2_m2m_dqbuf(file, fh->m2m_ctx, buf);
  613. }
  614. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_dqbuf);
  615. int v4l2_m2m_ioctl_prepare_buf(struct file *file, void *priv,
  616. struct v4l2_buffer *buf)
  617. {
  618. struct v4l2_fh *fh = file->private_data;
  619. return v4l2_m2m_prepare_buf(file, fh->m2m_ctx, buf);
  620. }
  621. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_prepare_buf);
  622. int v4l2_m2m_ioctl_expbuf(struct file *file, void *priv,
  623. struct v4l2_exportbuffer *eb)
  624. {
  625. struct v4l2_fh *fh = file->private_data;
  626. return v4l2_m2m_expbuf(file, fh->m2m_ctx, eb);
  627. }
  628. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_expbuf);
  629. int v4l2_m2m_ioctl_streamon(struct file *file, void *priv,
  630. enum v4l2_buf_type type)
  631. {
  632. struct v4l2_fh *fh = file->private_data;
  633. return v4l2_m2m_streamon(file, fh->m2m_ctx, type);
  634. }
  635. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamon);
  636. int v4l2_m2m_ioctl_streamoff(struct file *file, void *priv,
  637. enum v4l2_buf_type type)
  638. {
  639. struct v4l2_fh *fh = file->private_data;
  640. return v4l2_m2m_streamoff(file, fh->m2m_ctx, type);
  641. }
  642. EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamoff);
  643. /*
  644. * v4l2_file_operations helpers. It is assumed here same lock is used
  645. * for the output and the capture buffer queue.
  646. */
  647. int v4l2_m2m_fop_mmap(struct file *file, struct vm_area_struct *vma)
  648. {
  649. struct v4l2_fh *fh = file->private_data;
  650. return v4l2_m2m_mmap(file, fh->m2m_ctx, vma);
  651. }
  652. EXPORT_SYMBOL_GPL(v4l2_m2m_fop_mmap);
  653. __poll_t v4l2_m2m_fop_poll(struct file *file, poll_table *wait)
  654. {
  655. struct v4l2_fh *fh = file->private_data;
  656. struct v4l2_m2m_ctx *m2m_ctx = fh->m2m_ctx;
  657. __poll_t ret;
  658. if (m2m_ctx->q_lock)
  659. mutex_lock(m2m_ctx->q_lock);
  660. ret = v4l2_m2m_poll(file, m2m_ctx, wait);
  661. if (m2m_ctx->q_lock)
  662. mutex_unlock(m2m_ctx->q_lock);
  663. return ret;
  664. }
  665. EXPORT_SYMBOL_GPL(v4l2_m2m_fop_poll);