v4l2-mem2mem.c 21 KB

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