v4l2-mem2mem.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /*
  2. * Memory-to-memory device framework for Video for Linux 2 and videobuf.
  3. *
  4. * Helper functions for devices that use videobuf buffers for both their
  5. * source and destination.
  6. *
  7. * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
  8. * Pawel Osciak, <pawel@osciak.com>
  9. * Marek Szyprowski, <m.szyprowski@samsung.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/sched.h>
  18. #include <linux/slab.h>
  19. #include <media/videobuf2-core.h>
  20. #include <media/v4l2-mem2mem.h>
  21. #include <media/v4l2-dev.h>
  22. #include <media/v4l2-fh.h>
  23. #include <media/v4l2-event.h>
  24. MODULE_DESCRIPTION("Mem to mem device framework for videobuf");
  25. MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
  26. MODULE_LICENSE("GPL");
  27. static bool debug;
  28. module_param(debug, bool, 0644);
  29. #define dprintk(fmt, arg...) \
  30. do { \
  31. if (debug) \
  32. printk(KERN_DEBUG "%s: " fmt, __func__, ## arg);\
  33. } while (0)
  34. /* Instance is already queued on the job_queue */
  35. #define TRANS_QUEUED (1 << 0)
  36. /* Instance is currently running in hardware */
  37. #define TRANS_RUNNING (1 << 1)
  38. /* Offset base for buffers on the destination queue - used to distinguish
  39. * between source and destination buffers when mmapping - they receive the same
  40. * offsets but for different queues */
  41. #define DST_QUEUE_OFF_BASE (1 << 30)
  42. /**
  43. * struct v4l2_m2m_dev - per-device context
  44. * @curr_ctx: currently running instance
  45. * @job_queue: instances queued to run
  46. * @job_spinlock: protects job_queue
  47. * @m2m_ops: driver callbacks
  48. */
  49. struct v4l2_m2m_dev {
  50. struct v4l2_m2m_ctx *curr_ctx;
  51. struct list_head job_queue;
  52. spinlock_t job_spinlock;
  53. const struct v4l2_m2m_ops *m2m_ops;
  54. };
  55. static struct v4l2_m2m_queue_ctx *get_queue_ctx(struct v4l2_m2m_ctx *m2m_ctx,
  56. enum v4l2_buf_type type)
  57. {
  58. if (V4L2_TYPE_IS_OUTPUT(type))
  59. return &m2m_ctx->out_q_ctx;
  60. else
  61. return &m2m_ctx->cap_q_ctx;
  62. }
  63. /**
  64. * v4l2_m2m_get_vq() - return vb2_queue for the given type
  65. */
  66. struct vb2_queue *v4l2_m2m_get_vq(struct v4l2_m2m_ctx *m2m_ctx,
  67. enum v4l2_buf_type type)
  68. {
  69. struct v4l2_m2m_queue_ctx *q_ctx;
  70. q_ctx = get_queue_ctx(m2m_ctx, type);
  71. if (!q_ctx)
  72. return NULL;
  73. return &q_ctx->q;
  74. }
  75. EXPORT_SYMBOL(v4l2_m2m_get_vq);
  76. /**
  77. * v4l2_m2m_next_buf() - return next buffer from the list of ready buffers
  78. */
  79. void *v4l2_m2m_next_buf(struct v4l2_m2m_queue_ctx *q_ctx)
  80. {
  81. struct v4l2_m2m_buffer *b = NULL;
  82. unsigned long flags;
  83. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  84. if (list_empty(&q_ctx->rdy_queue)) {
  85. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  86. return NULL;
  87. }
  88. b = list_first_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list);
  89. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  90. return &b->vb;
  91. }
  92. EXPORT_SYMBOL_GPL(v4l2_m2m_next_buf);
  93. /**
  94. * v4l2_m2m_buf_remove() - take off a buffer from the list of ready buffers and
  95. * return it
  96. */
  97. void *v4l2_m2m_buf_remove(struct v4l2_m2m_queue_ctx *q_ctx)
  98. {
  99. struct v4l2_m2m_buffer *b = NULL;
  100. unsigned long flags;
  101. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  102. if (list_empty(&q_ctx->rdy_queue)) {
  103. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  104. return NULL;
  105. }
  106. b = list_first_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list);
  107. list_del(&b->list);
  108. q_ctx->num_rdy--;
  109. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  110. return &b->vb;
  111. }
  112. EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove);
  113. /*
  114. * Scheduling handlers
  115. */
  116. /**
  117. * v4l2_m2m_get_curr_priv() - return driver private data for the currently
  118. * running instance or NULL if no instance is running
  119. */
  120. void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev *m2m_dev)
  121. {
  122. unsigned long flags;
  123. void *ret = NULL;
  124. spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
  125. if (m2m_dev->curr_ctx)
  126. ret = m2m_dev->curr_ctx->priv;
  127. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  128. return ret;
  129. }
  130. EXPORT_SYMBOL(v4l2_m2m_get_curr_priv);
  131. /**
  132. * v4l2_m2m_try_run() - select next job to perform and run it if possible
  133. *
  134. * Get next transaction (if present) from the waiting jobs list and run it.
  135. */
  136. static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev)
  137. {
  138. unsigned long flags;
  139. spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
  140. if (NULL != m2m_dev->curr_ctx) {
  141. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  142. dprintk("Another instance is running, won't run now\n");
  143. return;
  144. }
  145. if (list_empty(&m2m_dev->job_queue)) {
  146. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  147. dprintk("No job pending\n");
  148. return;
  149. }
  150. m2m_dev->curr_ctx = list_first_entry(&m2m_dev->job_queue,
  151. struct v4l2_m2m_ctx, queue);
  152. m2m_dev->curr_ctx->job_flags |= TRANS_RUNNING;
  153. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  154. m2m_dev->m2m_ops->device_run(m2m_dev->curr_ctx->priv);
  155. }
  156. /**
  157. * v4l2_m2m_try_schedule() - check whether an instance is ready to be added to
  158. * the pending job queue and add it if so.
  159. * @m2m_ctx: m2m context assigned to the instance to be checked
  160. *
  161. * There are three basic requirements an instance has to meet to be able to run:
  162. * 1) at least one source buffer has to be queued,
  163. * 2) at least one destination buffer has to be queued,
  164. * 3) streaming has to be on.
  165. *
  166. * If a queue is buffered (for example a decoder hardware ringbuffer that has
  167. * to be drained before doing streamoff), allow scheduling without v4l2 buffers
  168. * on that queue.
  169. *
  170. * There may also be additional, custom requirements. In such case the driver
  171. * should supply a custom callback (job_ready in v4l2_m2m_ops) that should
  172. * return 1 if the instance is ready.
  173. * An example of the above could be an instance that requires more than one
  174. * src/dst buffer per transaction.
  175. */
  176. static 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 (m2m_ctx->job_flags & TRANS_QUEUED) {
  189. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  190. dprintk("On job queue already\n");
  191. return;
  192. }
  193. spin_lock_irqsave(&m2m_ctx->out_q_ctx.rdy_spinlock, flags_out);
  194. if (list_empty(&m2m_ctx->out_q_ctx.rdy_queue)
  195. && !m2m_ctx->out_q_ctx.buffered) {
  196. spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock,
  197. flags_out);
  198. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  199. dprintk("No input buffers available\n");
  200. return;
  201. }
  202. spin_lock_irqsave(&m2m_ctx->cap_q_ctx.rdy_spinlock, flags_cap);
  203. if (list_empty(&m2m_ctx->cap_q_ctx.rdy_queue)
  204. && !m2m_ctx->cap_q_ctx.buffered) {
  205. spin_unlock_irqrestore(&m2m_ctx->cap_q_ctx.rdy_spinlock,
  206. flags_cap);
  207. spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock,
  208. flags_out);
  209. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  210. dprintk("No output buffers available\n");
  211. return;
  212. }
  213. spin_unlock_irqrestore(&m2m_ctx->cap_q_ctx.rdy_spinlock, flags_cap);
  214. spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock, flags_out);
  215. if (m2m_dev->m2m_ops->job_ready
  216. && (!m2m_dev->m2m_ops->job_ready(m2m_ctx->priv))) {
  217. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  218. dprintk("Driver not ready\n");
  219. return;
  220. }
  221. list_add_tail(&m2m_ctx->queue, &m2m_dev->job_queue);
  222. m2m_ctx->job_flags |= TRANS_QUEUED;
  223. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  224. v4l2_m2m_try_run(m2m_dev);
  225. }
  226. /**
  227. * v4l2_m2m_cancel_job() - cancel pending jobs for the context
  228. *
  229. * In case of streamoff or release called on any context,
  230. * 1] If the context is currently running, then abort job will be called
  231. * 2] If the context is queued, then the context will be removed from
  232. * the job_queue
  233. */
  234. static void v4l2_m2m_cancel_job(struct v4l2_m2m_ctx *m2m_ctx)
  235. {
  236. struct v4l2_m2m_dev *m2m_dev;
  237. unsigned long flags;
  238. m2m_dev = m2m_ctx->m2m_dev;
  239. spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
  240. if (m2m_ctx->job_flags & TRANS_RUNNING) {
  241. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  242. m2m_dev->m2m_ops->job_abort(m2m_ctx->priv);
  243. dprintk("m2m_ctx %p running, will wait to complete", m2m_ctx);
  244. wait_event(m2m_ctx->finished,
  245. !(m2m_ctx->job_flags & TRANS_RUNNING));
  246. } else if (m2m_ctx->job_flags & TRANS_QUEUED) {
  247. list_del(&m2m_ctx->queue);
  248. m2m_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING);
  249. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  250. dprintk("m2m_ctx: %p had been on queue and was removed\n",
  251. m2m_ctx);
  252. } else {
  253. /* Do nothing, was not on queue/running */
  254. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  255. }
  256. }
  257. /**
  258. * v4l2_m2m_job_finish() - inform the framework that a job has been finished
  259. * and have it clean up
  260. *
  261. * Called by a driver to yield back the device after it has finished with it.
  262. * Should be called as soon as possible after reaching a state which allows
  263. * other instances to take control of the device.
  264. *
  265. * This function has to be called only after device_run() callback has been
  266. * called on the driver. To prevent recursion, it should not be called directly
  267. * from the device_run() callback though.
  268. */
  269. void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev,
  270. struct v4l2_m2m_ctx *m2m_ctx)
  271. {
  272. unsigned long flags;
  273. spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
  274. if (!m2m_dev->curr_ctx || m2m_dev->curr_ctx != m2m_ctx) {
  275. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  276. dprintk("Called by an instance not currently running\n");
  277. return;
  278. }
  279. list_del(&m2m_dev->curr_ctx->queue);
  280. m2m_dev->curr_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING);
  281. wake_up(&m2m_dev->curr_ctx->finished);
  282. m2m_dev->curr_ctx = NULL;
  283. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
  284. /* This instance might have more buffers ready, but since we do not
  285. * allow more than one job on the job_queue per instance, each has
  286. * to be scheduled separately after the previous one finishes. */
  287. v4l2_m2m_try_schedule(m2m_ctx);
  288. v4l2_m2m_try_run(m2m_dev);
  289. }
  290. EXPORT_SYMBOL(v4l2_m2m_job_finish);
  291. /**
  292. * v4l2_m2m_reqbufs() - multi-queue-aware REQBUFS multiplexer
  293. */
  294. int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  295. struct v4l2_requestbuffers *reqbufs)
  296. {
  297. struct vb2_queue *vq;
  298. vq = v4l2_m2m_get_vq(m2m_ctx, reqbufs->type);
  299. return vb2_reqbufs(vq, reqbufs);
  300. }
  301. EXPORT_SYMBOL_GPL(v4l2_m2m_reqbufs);
  302. /**
  303. * v4l2_m2m_querybuf() - multi-queue-aware QUERYBUF multiplexer
  304. *
  305. * See v4l2_m2m_mmap() documentation for details.
  306. */
  307. int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  308. struct v4l2_buffer *buf)
  309. {
  310. struct vb2_queue *vq;
  311. int ret = 0;
  312. unsigned int i;
  313. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  314. ret = vb2_querybuf(vq, buf);
  315. /* Adjust MMAP memory offsets for the CAPTURE queue */
  316. if (buf->memory == V4L2_MEMORY_MMAP && !V4L2_TYPE_IS_OUTPUT(vq->type)) {
  317. if (V4L2_TYPE_IS_MULTIPLANAR(vq->type)) {
  318. for (i = 0; i < buf->length; ++i)
  319. buf->m.planes[i].m.mem_offset
  320. += DST_QUEUE_OFF_BASE;
  321. } else {
  322. buf->m.offset += DST_QUEUE_OFF_BASE;
  323. }
  324. }
  325. return ret;
  326. }
  327. EXPORT_SYMBOL_GPL(v4l2_m2m_querybuf);
  328. /**
  329. * v4l2_m2m_qbuf() - enqueue a source or destination buffer, depending on
  330. * the type
  331. */
  332. int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  333. struct v4l2_buffer *buf)
  334. {
  335. struct vb2_queue *vq;
  336. int ret;
  337. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  338. ret = vb2_qbuf(vq, buf);
  339. if (!ret)
  340. v4l2_m2m_try_schedule(m2m_ctx);
  341. return ret;
  342. }
  343. EXPORT_SYMBOL_GPL(v4l2_m2m_qbuf);
  344. /**
  345. * v4l2_m2m_dqbuf() - dequeue a source or destination buffer, depending on
  346. * the type
  347. */
  348. int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  349. struct v4l2_buffer *buf)
  350. {
  351. struct vb2_queue *vq;
  352. vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
  353. return vb2_dqbuf(vq, buf, file->f_flags & O_NONBLOCK);
  354. }
  355. EXPORT_SYMBOL_GPL(v4l2_m2m_dqbuf);
  356. /**
  357. * v4l2_m2m_create_bufs() - create a source or destination buffer, depending
  358. * on the type
  359. */
  360. int v4l2_m2m_create_bufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  361. struct v4l2_create_buffers *create)
  362. {
  363. struct vb2_queue *vq;
  364. vq = v4l2_m2m_get_vq(m2m_ctx, create->format.type);
  365. return vb2_create_bufs(vq, create);
  366. }
  367. EXPORT_SYMBOL_GPL(v4l2_m2m_create_bufs);
  368. /**
  369. * v4l2_m2m_expbuf() - export a source or destination buffer, depending on
  370. * the type
  371. */
  372. int v4l2_m2m_expbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  373. struct v4l2_exportbuffer *eb)
  374. {
  375. struct vb2_queue *vq;
  376. vq = v4l2_m2m_get_vq(m2m_ctx, eb->type);
  377. return vb2_expbuf(vq, eb);
  378. }
  379. EXPORT_SYMBOL_GPL(v4l2_m2m_expbuf);
  380. /**
  381. * v4l2_m2m_streamon() - turn on streaming for a video queue
  382. */
  383. int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  384. enum v4l2_buf_type type)
  385. {
  386. struct vb2_queue *vq;
  387. int ret;
  388. vq = v4l2_m2m_get_vq(m2m_ctx, type);
  389. ret = vb2_streamon(vq, type);
  390. if (!ret)
  391. v4l2_m2m_try_schedule(m2m_ctx);
  392. return ret;
  393. }
  394. EXPORT_SYMBOL_GPL(v4l2_m2m_streamon);
  395. /**
  396. * v4l2_m2m_streamoff() - turn off streaming for a video queue
  397. */
  398. int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  399. enum v4l2_buf_type type)
  400. {
  401. struct v4l2_m2m_dev *m2m_dev;
  402. struct v4l2_m2m_queue_ctx *q_ctx;
  403. unsigned long flags_job, flags;
  404. int ret;
  405. /* wait until the current context is dequeued from job_queue */
  406. v4l2_m2m_cancel_job(m2m_ctx);
  407. q_ctx = get_queue_ctx(m2m_ctx, type);
  408. ret = vb2_streamoff(&q_ctx->q, type);
  409. if (ret)
  410. return ret;
  411. m2m_dev = m2m_ctx->m2m_dev;
  412. spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job);
  413. /* We should not be scheduled anymore, since we're dropping a queue. */
  414. INIT_LIST_HEAD(&m2m_ctx->queue);
  415. m2m_ctx->job_flags = 0;
  416. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  417. /* Drop queue, since streamoff returns device to the same state as after
  418. * calling reqbufs. */
  419. INIT_LIST_HEAD(&q_ctx->rdy_queue);
  420. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  421. if (m2m_dev->curr_ctx == m2m_ctx) {
  422. m2m_dev->curr_ctx = NULL;
  423. wake_up(&m2m_ctx->finished);
  424. }
  425. spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
  426. return 0;
  427. }
  428. EXPORT_SYMBOL_GPL(v4l2_m2m_streamoff);
  429. /**
  430. * v4l2_m2m_poll() - poll replacement, for destination buffers only
  431. *
  432. * Call from the driver's poll() function. Will poll both queues. If a buffer
  433. * is available to dequeue (with dqbuf) from the source queue, this will
  434. * indicate that a non-blocking write can be performed, while read will be
  435. * returned in case of the destination queue.
  436. */
  437. unsigned int v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  438. struct poll_table_struct *wait)
  439. {
  440. struct video_device *vfd = video_devdata(file);
  441. unsigned long req_events = poll_requested_events(wait);
  442. struct vb2_queue *src_q, *dst_q;
  443. struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
  444. unsigned int rc = 0;
  445. unsigned long flags;
  446. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
  447. struct v4l2_fh *fh = file->private_data;
  448. if (v4l2_event_pending(fh))
  449. rc = POLLPRI;
  450. else if (req_events & POLLPRI)
  451. poll_wait(file, &fh->wait, wait);
  452. if (!(req_events & (POLLOUT | POLLWRNORM | POLLIN | POLLRDNORM)))
  453. return rc;
  454. }
  455. src_q = v4l2_m2m_get_src_vq(m2m_ctx);
  456. dst_q = v4l2_m2m_get_dst_vq(m2m_ctx);
  457. /*
  458. * There has to be at least one buffer queued on each queued_list, which
  459. * means either in driver already or waiting for driver to claim it
  460. * and start processing.
  461. */
  462. if ((!src_q->streaming || list_empty(&src_q->queued_list))
  463. && (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
  464. rc |= POLLERR;
  465. goto end;
  466. }
  467. if (m2m_ctx->m2m_dev->m2m_ops->unlock)
  468. m2m_ctx->m2m_dev->m2m_ops->unlock(m2m_ctx->priv);
  469. if (list_empty(&src_q->done_list))
  470. poll_wait(file, &src_q->done_wq, wait);
  471. if (list_empty(&dst_q->done_list))
  472. poll_wait(file, &dst_q->done_wq, wait);
  473. if (m2m_ctx->m2m_dev->m2m_ops->lock)
  474. m2m_ctx->m2m_dev->m2m_ops->lock(m2m_ctx->priv);
  475. spin_lock_irqsave(&src_q->done_lock, flags);
  476. if (!list_empty(&src_q->done_list))
  477. src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
  478. done_entry);
  479. if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
  480. || src_vb->state == VB2_BUF_STATE_ERROR))
  481. rc |= POLLOUT | POLLWRNORM;
  482. spin_unlock_irqrestore(&src_q->done_lock, flags);
  483. spin_lock_irqsave(&dst_q->done_lock, flags);
  484. if (!list_empty(&dst_q->done_list))
  485. dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
  486. done_entry);
  487. if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
  488. || dst_vb->state == VB2_BUF_STATE_ERROR))
  489. rc |= POLLIN | POLLRDNORM;
  490. spin_unlock_irqrestore(&dst_q->done_lock, flags);
  491. end:
  492. return rc;
  493. }
  494. EXPORT_SYMBOL_GPL(v4l2_m2m_poll);
  495. /**
  496. * v4l2_m2m_mmap() - source and destination queues-aware mmap multiplexer
  497. *
  498. * Call from driver's mmap() function. Will handle mmap() for both queues
  499. * seamlessly for videobuffer, which will receive normal per-queue offsets and
  500. * proper videobuf queue pointers. The differentiation is made outside videobuf
  501. * by adding a predefined offset to buffers from one of the queues and
  502. * subtracting it before passing it back to videobuf. Only drivers (and
  503. * thus applications) receive modified offsets.
  504. */
  505. int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
  506. struct vm_area_struct *vma)
  507. {
  508. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  509. struct vb2_queue *vq;
  510. if (offset < DST_QUEUE_OFF_BASE) {
  511. vq = v4l2_m2m_get_src_vq(m2m_ctx);
  512. } else {
  513. vq = v4l2_m2m_get_dst_vq(m2m_ctx);
  514. vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
  515. }
  516. return vb2_mmap(vq, vma);
  517. }
  518. EXPORT_SYMBOL(v4l2_m2m_mmap);
  519. /**
  520. * v4l2_m2m_init() - initialize per-driver m2m data
  521. *
  522. * Usually called from driver's probe() function.
  523. */
  524. struct v4l2_m2m_dev *v4l2_m2m_init(const struct v4l2_m2m_ops *m2m_ops)
  525. {
  526. struct v4l2_m2m_dev *m2m_dev;
  527. if (!m2m_ops || WARN_ON(!m2m_ops->device_run) ||
  528. WARN_ON(!m2m_ops->job_abort))
  529. return ERR_PTR(-EINVAL);
  530. m2m_dev = kzalloc(sizeof *m2m_dev, GFP_KERNEL);
  531. if (!m2m_dev)
  532. return ERR_PTR(-ENOMEM);
  533. m2m_dev->curr_ctx = NULL;
  534. m2m_dev->m2m_ops = m2m_ops;
  535. INIT_LIST_HEAD(&m2m_dev->job_queue);
  536. spin_lock_init(&m2m_dev->job_spinlock);
  537. return m2m_dev;
  538. }
  539. EXPORT_SYMBOL_GPL(v4l2_m2m_init);
  540. /**
  541. * v4l2_m2m_release() - cleans up and frees a m2m_dev structure
  542. *
  543. * Usually called from driver's remove() function.
  544. */
  545. void v4l2_m2m_release(struct v4l2_m2m_dev *m2m_dev)
  546. {
  547. kfree(m2m_dev);
  548. }
  549. EXPORT_SYMBOL_GPL(v4l2_m2m_release);
  550. /**
  551. * v4l2_m2m_ctx_init() - allocate and initialize a m2m context
  552. * @priv - driver's instance private data
  553. * @m2m_dev - a previously initialized m2m_dev struct
  554. * @vq_init - a callback for queue type-specific initialization function to be
  555. * used for initializing videobuf_queues
  556. *
  557. * Usually called from driver's open() function.
  558. */
  559. struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev,
  560. void *drv_priv,
  561. int (*queue_init)(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq))
  562. {
  563. struct v4l2_m2m_ctx *m2m_ctx;
  564. struct v4l2_m2m_queue_ctx *out_q_ctx, *cap_q_ctx;
  565. int ret;
  566. m2m_ctx = kzalloc(sizeof *m2m_ctx, GFP_KERNEL);
  567. if (!m2m_ctx)
  568. return ERR_PTR(-ENOMEM);
  569. m2m_ctx->priv = drv_priv;
  570. m2m_ctx->m2m_dev = m2m_dev;
  571. init_waitqueue_head(&m2m_ctx->finished);
  572. out_q_ctx = &m2m_ctx->out_q_ctx;
  573. cap_q_ctx = &m2m_ctx->cap_q_ctx;
  574. INIT_LIST_HEAD(&out_q_ctx->rdy_queue);
  575. INIT_LIST_HEAD(&cap_q_ctx->rdy_queue);
  576. spin_lock_init(&out_q_ctx->rdy_spinlock);
  577. spin_lock_init(&cap_q_ctx->rdy_spinlock);
  578. INIT_LIST_HEAD(&m2m_ctx->queue);
  579. ret = queue_init(drv_priv, &out_q_ctx->q, &cap_q_ctx->q);
  580. if (ret)
  581. goto err;
  582. return m2m_ctx;
  583. err:
  584. kfree(m2m_ctx);
  585. return ERR_PTR(ret);
  586. }
  587. EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_init);
  588. /**
  589. * v4l2_m2m_ctx_release() - release m2m context
  590. *
  591. * Usually called from driver's release() function.
  592. */
  593. void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx *m2m_ctx)
  594. {
  595. /* wait until the current context is dequeued from job_queue */
  596. v4l2_m2m_cancel_job(m2m_ctx);
  597. vb2_queue_release(&m2m_ctx->cap_q_ctx.q);
  598. vb2_queue_release(&m2m_ctx->out_q_ctx.q);
  599. kfree(m2m_ctx);
  600. }
  601. EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_release);
  602. /**
  603. * v4l2_m2m_buf_queue() - add a buffer to the proper ready buffers list.
  604. *
  605. * Call from buf_queue(), videobuf_queue_ops callback.
  606. */
  607. void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx *m2m_ctx, struct vb2_buffer *vb)
  608. {
  609. struct v4l2_m2m_buffer *b = container_of(vb, struct v4l2_m2m_buffer, vb);
  610. struct v4l2_m2m_queue_ctx *q_ctx;
  611. unsigned long flags;
  612. q_ctx = get_queue_ctx(m2m_ctx, vb->vb2_queue->type);
  613. if (!q_ctx)
  614. return;
  615. spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
  616. list_add_tail(&b->list, &q_ctx->rdy_queue);
  617. q_ctx->num_rdy++;
  618. spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
  619. }
  620. EXPORT_SYMBOL_GPL(v4l2_m2m_buf_queue);