v4l2-mem2mem.c 30 KB

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