m2m-deinterlace.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. /*
  2. * V4L2 deinterlacing support.
  3. *
  4. * Copyright (c) 2012 Vista Silicon S.L.
  5. * Javier Martin <javier.martin@vista-silicon.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the
  10. * License, or (at your option) any later version
  11. */
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/dmaengine.h>
  16. #include <linux/platform_device.h>
  17. #include <media/v4l2-mem2mem.h>
  18. #include <media/v4l2-device.h>
  19. #include <media/v4l2-ioctl.h>
  20. #include <media/videobuf2-dma-contig.h>
  21. #define MEM2MEM_TEST_MODULE_NAME "mem2mem-deinterlace"
  22. MODULE_DESCRIPTION("mem2mem device which supports deinterlacing using dmaengine");
  23. MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com");
  24. MODULE_LICENSE("GPL");
  25. MODULE_VERSION("0.0.1");
  26. static bool debug;
  27. module_param(debug, bool, 0644);
  28. /* Flags that indicate a format can be used for capture/output */
  29. #define MEM2MEM_CAPTURE (1 << 0)
  30. #define MEM2MEM_OUTPUT (1 << 1)
  31. #define MEM2MEM_NAME "m2m-deinterlace"
  32. #define dprintk(dev, fmt, arg...) \
  33. v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
  34. struct deinterlace_fmt {
  35. char *name;
  36. u32 fourcc;
  37. /* Types the format can be used for */
  38. u32 types;
  39. };
  40. static struct deinterlace_fmt formats[] = {
  41. {
  42. .name = "YUV 4:2:0 Planar",
  43. .fourcc = V4L2_PIX_FMT_YUV420,
  44. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  45. },
  46. {
  47. .name = "YUYV 4:2:2",
  48. .fourcc = V4L2_PIX_FMT_YUYV,
  49. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  50. },
  51. };
  52. #define NUM_FORMATS ARRAY_SIZE(formats)
  53. /* Per-queue, driver-specific private data */
  54. struct deinterlace_q_data {
  55. unsigned int width;
  56. unsigned int height;
  57. unsigned int sizeimage;
  58. struct deinterlace_fmt *fmt;
  59. enum v4l2_field field;
  60. };
  61. enum {
  62. V4L2_M2M_SRC = 0,
  63. V4L2_M2M_DST = 1,
  64. };
  65. enum {
  66. YUV420_DMA_Y_ODD,
  67. YUV420_DMA_Y_EVEN,
  68. YUV420_DMA_U_ODD,
  69. YUV420_DMA_U_EVEN,
  70. YUV420_DMA_V_ODD,
  71. YUV420_DMA_V_EVEN,
  72. YUV420_DMA_Y_ODD_DOUBLING,
  73. YUV420_DMA_U_ODD_DOUBLING,
  74. YUV420_DMA_V_ODD_DOUBLING,
  75. YUYV_DMA_ODD,
  76. YUYV_DMA_EVEN,
  77. YUYV_DMA_EVEN_DOUBLING,
  78. };
  79. /* Source and destination queue data */
  80. static struct deinterlace_q_data q_data[2];
  81. static struct deinterlace_q_data *get_q_data(enum v4l2_buf_type type)
  82. {
  83. switch (type) {
  84. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  85. return &q_data[V4L2_M2M_SRC];
  86. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  87. return &q_data[V4L2_M2M_DST];
  88. default:
  89. BUG();
  90. }
  91. return NULL;
  92. }
  93. static struct deinterlace_fmt *find_format(struct v4l2_format *f)
  94. {
  95. struct deinterlace_fmt *fmt;
  96. unsigned int k;
  97. for (k = 0; k < NUM_FORMATS; k++) {
  98. fmt = &formats[k];
  99. if ((fmt->types & f->type) &&
  100. (fmt->fourcc == f->fmt.pix.pixelformat))
  101. break;
  102. }
  103. if (k == NUM_FORMATS)
  104. return NULL;
  105. return &formats[k];
  106. }
  107. struct deinterlace_dev {
  108. struct v4l2_device v4l2_dev;
  109. struct video_device vfd;
  110. atomic_t busy;
  111. struct mutex dev_mutex;
  112. spinlock_t irqlock;
  113. struct dma_chan *dma_chan;
  114. struct v4l2_m2m_dev *m2m_dev;
  115. };
  116. struct deinterlace_ctx {
  117. struct deinterlace_dev *dev;
  118. /* Abort requested by m2m */
  119. int aborting;
  120. enum v4l2_colorspace colorspace;
  121. dma_cookie_t cookie;
  122. struct v4l2_m2m_ctx *m2m_ctx;
  123. struct dma_interleaved_template *xt;
  124. };
  125. /*
  126. * mem2mem callbacks
  127. */
  128. static int deinterlace_job_ready(void *priv)
  129. {
  130. struct deinterlace_ctx *ctx = priv;
  131. struct deinterlace_dev *pcdev = ctx->dev;
  132. if ((v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) > 0)
  133. && (v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx) > 0)
  134. && (atomic_read(&ctx->dev->busy) == 0)) {
  135. dprintk(pcdev, "Task ready\n");
  136. return 1;
  137. }
  138. dprintk(pcdev, "Task not ready to run\n");
  139. return 0;
  140. }
  141. static void deinterlace_job_abort(void *priv)
  142. {
  143. struct deinterlace_ctx *ctx = priv;
  144. struct deinterlace_dev *pcdev = ctx->dev;
  145. ctx->aborting = 1;
  146. dprintk(pcdev, "Aborting task\n");
  147. v4l2_m2m_job_finish(pcdev->m2m_dev, ctx->m2m_ctx);
  148. }
  149. static void deinterlace_lock(void *priv)
  150. {
  151. struct deinterlace_ctx *ctx = priv;
  152. struct deinterlace_dev *pcdev = ctx->dev;
  153. mutex_lock(&pcdev->dev_mutex);
  154. }
  155. static void deinterlace_unlock(void *priv)
  156. {
  157. struct deinterlace_ctx *ctx = priv;
  158. struct deinterlace_dev *pcdev = ctx->dev;
  159. mutex_unlock(&pcdev->dev_mutex);
  160. }
  161. static void dma_callback(void *data)
  162. {
  163. struct deinterlace_ctx *curr_ctx = data;
  164. struct deinterlace_dev *pcdev = curr_ctx->dev;
  165. struct vb2_v4l2_buffer *src_vb, *dst_vb;
  166. atomic_set(&pcdev->busy, 0);
  167. src_vb = v4l2_m2m_src_buf_remove(curr_ctx->m2m_ctx);
  168. dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->m2m_ctx);
  169. dst_vb->vb2_buf.timestamp = src_vb->vb2_buf.timestamp;
  170. dst_vb->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  171. dst_vb->flags |=
  172. src_vb->flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  173. dst_vb->timecode = src_vb->timecode;
  174. v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
  175. v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
  176. v4l2_m2m_job_finish(pcdev->m2m_dev, curr_ctx->m2m_ctx);
  177. dprintk(pcdev, "dma transfers completed.\n");
  178. }
  179. static void deinterlace_issue_dma(struct deinterlace_ctx *ctx, int op,
  180. int do_callback)
  181. {
  182. struct deinterlace_q_data *s_q_data;
  183. struct vb2_v4l2_buffer *src_buf, *dst_buf;
  184. struct deinterlace_dev *pcdev = ctx->dev;
  185. struct dma_chan *chan = pcdev->dma_chan;
  186. struct dma_device *dmadev = chan->device;
  187. struct dma_async_tx_descriptor *tx;
  188. unsigned int s_width, s_height;
  189. unsigned int s_size;
  190. dma_addr_t p_in, p_out;
  191. enum dma_ctrl_flags flags;
  192. src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  193. dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  194. s_q_data = get_q_data(V4L2_BUF_TYPE_VIDEO_OUTPUT);
  195. s_width = s_q_data->width;
  196. s_height = s_q_data->height;
  197. s_size = s_width * s_height;
  198. p_in = (dma_addr_t)vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
  199. p_out = (dma_addr_t)vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf,
  200. 0);
  201. if (!p_in || !p_out) {
  202. v4l2_err(&pcdev->v4l2_dev,
  203. "Acquiring kernel pointers to buffers failed\n");
  204. return;
  205. }
  206. switch (op) {
  207. case YUV420_DMA_Y_ODD:
  208. ctx->xt->numf = s_height / 2;
  209. ctx->xt->sgl[0].size = s_width;
  210. ctx->xt->sgl[0].icg = s_width;
  211. ctx->xt->src_start = p_in;
  212. ctx->xt->dst_start = p_out;
  213. break;
  214. case YUV420_DMA_Y_EVEN:
  215. ctx->xt->numf = s_height / 2;
  216. ctx->xt->sgl[0].size = s_width;
  217. ctx->xt->sgl[0].icg = s_width;
  218. ctx->xt->src_start = p_in + s_size / 2;
  219. ctx->xt->dst_start = p_out + s_width;
  220. break;
  221. case YUV420_DMA_U_ODD:
  222. ctx->xt->numf = s_height / 4;
  223. ctx->xt->sgl[0].size = s_width / 2;
  224. ctx->xt->sgl[0].icg = s_width / 2;
  225. ctx->xt->src_start = p_in + s_size;
  226. ctx->xt->dst_start = p_out + s_size;
  227. break;
  228. case YUV420_DMA_U_EVEN:
  229. ctx->xt->numf = s_height / 4;
  230. ctx->xt->sgl[0].size = s_width / 2;
  231. ctx->xt->sgl[0].icg = s_width / 2;
  232. ctx->xt->src_start = p_in + (9 * s_size) / 8;
  233. ctx->xt->dst_start = p_out + s_size + s_width / 2;
  234. break;
  235. case YUV420_DMA_V_ODD:
  236. ctx->xt->numf = s_height / 4;
  237. ctx->xt->sgl[0].size = s_width / 2;
  238. ctx->xt->sgl[0].icg = s_width / 2;
  239. ctx->xt->src_start = p_in + (5 * s_size) / 4;
  240. ctx->xt->dst_start = p_out + (5 * s_size) / 4;
  241. break;
  242. case YUV420_DMA_V_EVEN:
  243. ctx->xt->numf = s_height / 4;
  244. ctx->xt->sgl[0].size = s_width / 2;
  245. ctx->xt->sgl[0].icg = s_width / 2;
  246. ctx->xt->src_start = p_in + (11 * s_size) / 8;
  247. ctx->xt->dst_start = p_out + (5 * s_size) / 4 + s_width / 2;
  248. break;
  249. case YUV420_DMA_Y_ODD_DOUBLING:
  250. ctx->xt->numf = s_height / 2;
  251. ctx->xt->sgl[0].size = s_width;
  252. ctx->xt->sgl[0].icg = s_width;
  253. ctx->xt->src_start = p_in;
  254. ctx->xt->dst_start = p_out + s_width;
  255. break;
  256. case YUV420_DMA_U_ODD_DOUBLING:
  257. ctx->xt->numf = s_height / 4;
  258. ctx->xt->sgl[0].size = s_width / 2;
  259. ctx->xt->sgl[0].icg = s_width / 2;
  260. ctx->xt->src_start = p_in + s_size;
  261. ctx->xt->dst_start = p_out + s_size + s_width / 2;
  262. break;
  263. case YUV420_DMA_V_ODD_DOUBLING:
  264. ctx->xt->numf = s_height / 4;
  265. ctx->xt->sgl[0].size = s_width / 2;
  266. ctx->xt->sgl[0].icg = s_width / 2;
  267. ctx->xt->src_start = p_in + (5 * s_size) / 4;
  268. ctx->xt->dst_start = p_out + (5 * s_size) / 4 + s_width / 2;
  269. break;
  270. case YUYV_DMA_ODD:
  271. ctx->xt->numf = s_height / 2;
  272. ctx->xt->sgl[0].size = s_width * 2;
  273. ctx->xt->sgl[0].icg = s_width * 2;
  274. ctx->xt->src_start = p_in;
  275. ctx->xt->dst_start = p_out;
  276. break;
  277. case YUYV_DMA_EVEN:
  278. ctx->xt->numf = s_height / 2;
  279. ctx->xt->sgl[0].size = s_width * 2;
  280. ctx->xt->sgl[0].icg = s_width * 2;
  281. ctx->xt->src_start = p_in + s_size;
  282. ctx->xt->dst_start = p_out + s_width * 2;
  283. break;
  284. case YUYV_DMA_EVEN_DOUBLING:
  285. default:
  286. ctx->xt->numf = s_height / 2;
  287. ctx->xt->sgl[0].size = s_width * 2;
  288. ctx->xt->sgl[0].icg = s_width * 2;
  289. ctx->xt->src_start = p_in;
  290. ctx->xt->dst_start = p_out + s_width * 2;
  291. break;
  292. }
  293. /* Common parameters for al transfers */
  294. ctx->xt->frame_size = 1;
  295. ctx->xt->dir = DMA_MEM_TO_MEM;
  296. ctx->xt->src_sgl = false;
  297. ctx->xt->dst_sgl = true;
  298. flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
  299. tx = dmadev->device_prep_interleaved_dma(chan, ctx->xt, flags);
  300. if (tx == NULL) {
  301. v4l2_warn(&pcdev->v4l2_dev, "DMA interleaved prep error\n");
  302. return;
  303. }
  304. if (do_callback) {
  305. tx->callback = dma_callback;
  306. tx->callback_param = ctx;
  307. }
  308. ctx->cookie = dmaengine_submit(tx);
  309. if (dma_submit_error(ctx->cookie)) {
  310. v4l2_warn(&pcdev->v4l2_dev,
  311. "DMA submit error %d with src=0x%x dst=0x%x len=0x%x\n",
  312. ctx->cookie, (unsigned)p_in, (unsigned)p_out,
  313. s_size * 3/2);
  314. return;
  315. }
  316. dma_async_issue_pending(chan);
  317. }
  318. static void deinterlace_device_run(void *priv)
  319. {
  320. struct deinterlace_ctx *ctx = priv;
  321. struct deinterlace_q_data *dst_q_data;
  322. atomic_set(&ctx->dev->busy, 1);
  323. dprintk(ctx->dev, "%s: DMA try issue.\n", __func__);
  324. dst_q_data = get_q_data(V4L2_BUF_TYPE_VIDEO_CAPTURE);
  325. /*
  326. * 4 possible field conversions are possible at the moment:
  327. * V4L2_FIELD_SEQ_TB --> V4L2_FIELD_INTERLACED_TB:
  328. * two separate fields in the same input buffer are interlaced
  329. * in the output buffer using weaving. Top field comes first.
  330. * V4L2_FIELD_SEQ_TB --> V4L2_FIELD_NONE:
  331. * top field from the input buffer is copied to the output buffer
  332. * using line doubling. Bottom field from the input buffer is discarded.
  333. * V4L2_FIELD_SEQ_BT --> V4L2_FIELD_INTERLACED_BT:
  334. * two separate fields in the same input buffer are interlaced
  335. * in the output buffer using weaving. Bottom field comes first.
  336. * V4L2_FIELD_SEQ_BT --> V4L2_FIELD_NONE:
  337. * bottom field from the input buffer is copied to the output buffer
  338. * using line doubling. Top field from the input buffer is discarded.
  339. */
  340. switch (dst_q_data->fmt->fourcc) {
  341. case V4L2_PIX_FMT_YUV420:
  342. switch (dst_q_data->field) {
  343. case V4L2_FIELD_INTERLACED_TB:
  344. case V4L2_FIELD_INTERLACED_BT:
  345. dprintk(ctx->dev, "%s: yuv420 interlaced tb.\n",
  346. __func__);
  347. deinterlace_issue_dma(ctx, YUV420_DMA_Y_ODD, 0);
  348. deinterlace_issue_dma(ctx, YUV420_DMA_Y_EVEN, 0);
  349. deinterlace_issue_dma(ctx, YUV420_DMA_U_ODD, 0);
  350. deinterlace_issue_dma(ctx, YUV420_DMA_U_EVEN, 0);
  351. deinterlace_issue_dma(ctx, YUV420_DMA_V_ODD, 0);
  352. deinterlace_issue_dma(ctx, YUV420_DMA_V_EVEN, 1);
  353. break;
  354. case V4L2_FIELD_NONE:
  355. default:
  356. dprintk(ctx->dev, "%s: yuv420 interlaced line doubling.\n",
  357. __func__);
  358. deinterlace_issue_dma(ctx, YUV420_DMA_Y_ODD, 0);
  359. deinterlace_issue_dma(ctx, YUV420_DMA_Y_ODD_DOUBLING, 0);
  360. deinterlace_issue_dma(ctx, YUV420_DMA_U_ODD, 0);
  361. deinterlace_issue_dma(ctx, YUV420_DMA_U_ODD_DOUBLING, 0);
  362. deinterlace_issue_dma(ctx, YUV420_DMA_V_ODD, 0);
  363. deinterlace_issue_dma(ctx, YUV420_DMA_V_ODD_DOUBLING, 1);
  364. break;
  365. }
  366. break;
  367. case V4L2_PIX_FMT_YUYV:
  368. default:
  369. switch (dst_q_data->field) {
  370. case V4L2_FIELD_INTERLACED_TB:
  371. case V4L2_FIELD_INTERLACED_BT:
  372. dprintk(ctx->dev, "%s: yuyv interlaced_tb.\n",
  373. __func__);
  374. deinterlace_issue_dma(ctx, YUYV_DMA_ODD, 0);
  375. deinterlace_issue_dma(ctx, YUYV_DMA_EVEN, 1);
  376. break;
  377. case V4L2_FIELD_NONE:
  378. default:
  379. dprintk(ctx->dev, "%s: yuyv interlaced line doubling.\n",
  380. __func__);
  381. deinterlace_issue_dma(ctx, YUYV_DMA_ODD, 0);
  382. deinterlace_issue_dma(ctx, YUYV_DMA_EVEN_DOUBLING, 1);
  383. break;
  384. }
  385. break;
  386. }
  387. dprintk(ctx->dev, "%s: DMA issue done.\n", __func__);
  388. }
  389. /*
  390. * video ioctls
  391. */
  392. static int vidioc_querycap(struct file *file, void *priv,
  393. struct v4l2_capability *cap)
  394. {
  395. strlcpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver));
  396. strlcpy(cap->card, MEM2MEM_NAME, sizeof(cap->card));
  397. strlcpy(cap->bus_info, MEM2MEM_NAME, sizeof(cap->card));
  398. /*
  399. * This is only a mem-to-mem video device. The capture and output
  400. * device capability flags are left only for backward compatibility
  401. * and are scheduled for removal.
  402. */
  403. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
  404. V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
  405. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  406. return 0;
  407. }
  408. static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
  409. {
  410. int i, num;
  411. struct deinterlace_fmt *fmt;
  412. num = 0;
  413. for (i = 0; i < NUM_FORMATS; ++i) {
  414. if (formats[i].types & type) {
  415. /* index-th format of type type found ? */
  416. if (num == f->index)
  417. break;
  418. /* Correct type but haven't reached our index yet,
  419. * just increment per-type index */
  420. ++num;
  421. }
  422. }
  423. if (i < NUM_FORMATS) {
  424. /* Format found */
  425. fmt = &formats[i];
  426. strlcpy(f->description, fmt->name, sizeof(f->description));
  427. f->pixelformat = fmt->fourcc;
  428. return 0;
  429. }
  430. /* Format not found */
  431. return -EINVAL;
  432. }
  433. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  434. struct v4l2_fmtdesc *f)
  435. {
  436. return enum_fmt(f, MEM2MEM_CAPTURE);
  437. }
  438. static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
  439. struct v4l2_fmtdesc *f)
  440. {
  441. return enum_fmt(f, MEM2MEM_OUTPUT);
  442. }
  443. static int vidioc_g_fmt(struct deinterlace_ctx *ctx, struct v4l2_format *f)
  444. {
  445. struct vb2_queue *vq;
  446. struct deinterlace_q_data *q_data;
  447. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  448. if (!vq)
  449. return -EINVAL;
  450. q_data = get_q_data(f->type);
  451. f->fmt.pix.width = q_data->width;
  452. f->fmt.pix.height = q_data->height;
  453. f->fmt.pix.field = q_data->field;
  454. f->fmt.pix.pixelformat = q_data->fmt->fourcc;
  455. switch (q_data->fmt->fourcc) {
  456. case V4L2_PIX_FMT_YUV420:
  457. f->fmt.pix.bytesperline = q_data->width * 3 / 2;
  458. break;
  459. case V4L2_PIX_FMT_YUYV:
  460. default:
  461. f->fmt.pix.bytesperline = q_data->width * 2;
  462. }
  463. f->fmt.pix.sizeimage = q_data->sizeimage;
  464. f->fmt.pix.colorspace = ctx->colorspace;
  465. return 0;
  466. }
  467. static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
  468. struct v4l2_format *f)
  469. {
  470. return vidioc_g_fmt(priv, f);
  471. }
  472. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  473. struct v4l2_format *f)
  474. {
  475. return vidioc_g_fmt(priv, f);
  476. }
  477. static int vidioc_try_fmt(struct v4l2_format *f, struct deinterlace_fmt *fmt)
  478. {
  479. switch (f->fmt.pix.pixelformat) {
  480. case V4L2_PIX_FMT_YUV420:
  481. f->fmt.pix.bytesperline = f->fmt.pix.width * 3 / 2;
  482. break;
  483. case V4L2_PIX_FMT_YUYV:
  484. default:
  485. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  486. }
  487. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  488. return 0;
  489. }
  490. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  491. struct v4l2_format *f)
  492. {
  493. struct deinterlace_fmt *fmt;
  494. struct deinterlace_ctx *ctx = priv;
  495. fmt = find_format(f);
  496. if (!fmt || !(fmt->types & MEM2MEM_CAPTURE))
  497. f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
  498. f->fmt.pix.colorspace = ctx->colorspace;
  499. if (f->fmt.pix.field != V4L2_FIELD_INTERLACED_TB &&
  500. f->fmt.pix.field != V4L2_FIELD_INTERLACED_BT &&
  501. f->fmt.pix.field != V4L2_FIELD_NONE)
  502. f->fmt.pix.field = V4L2_FIELD_INTERLACED_TB;
  503. return vidioc_try_fmt(f, fmt);
  504. }
  505. static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
  506. struct v4l2_format *f)
  507. {
  508. struct deinterlace_fmt *fmt;
  509. fmt = find_format(f);
  510. if (!fmt || !(fmt->types & MEM2MEM_OUTPUT))
  511. f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
  512. if (!f->fmt.pix.colorspace)
  513. f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
  514. if (f->fmt.pix.field != V4L2_FIELD_SEQ_TB &&
  515. f->fmt.pix.field != V4L2_FIELD_SEQ_BT)
  516. f->fmt.pix.field = V4L2_FIELD_SEQ_TB;
  517. return vidioc_try_fmt(f, fmt);
  518. }
  519. static int vidioc_s_fmt(struct deinterlace_ctx *ctx, struct v4l2_format *f)
  520. {
  521. struct deinterlace_q_data *q_data;
  522. struct vb2_queue *vq;
  523. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  524. if (!vq)
  525. return -EINVAL;
  526. q_data = get_q_data(f->type);
  527. if (!q_data)
  528. return -EINVAL;
  529. if (vb2_is_busy(vq)) {
  530. v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
  531. return -EBUSY;
  532. }
  533. q_data->fmt = find_format(f);
  534. if (!q_data->fmt) {
  535. v4l2_err(&ctx->dev->v4l2_dev,
  536. "Couldn't set format type %d, wxh: %dx%d. fmt: %d, field: %d\n",
  537. f->type, f->fmt.pix.width, f->fmt.pix.height,
  538. f->fmt.pix.pixelformat, f->fmt.pix.field);
  539. return -EINVAL;
  540. }
  541. q_data->width = f->fmt.pix.width;
  542. q_data->height = f->fmt.pix.height;
  543. q_data->field = f->fmt.pix.field;
  544. switch (f->fmt.pix.pixelformat) {
  545. case V4L2_PIX_FMT_YUV420:
  546. f->fmt.pix.bytesperline = f->fmt.pix.width * 3 / 2;
  547. q_data->sizeimage = (q_data->width * q_data->height * 3) / 2;
  548. break;
  549. case V4L2_PIX_FMT_YUYV:
  550. default:
  551. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  552. q_data->sizeimage = q_data->width * q_data->height * 2;
  553. }
  554. dprintk(ctx->dev,
  555. "Setting format for type %d, wxh: %dx%d, fmt: %d, field: %d\n",
  556. f->type, q_data->width, q_data->height, q_data->fmt->fourcc,
  557. q_data->field);
  558. return 0;
  559. }
  560. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  561. struct v4l2_format *f)
  562. {
  563. int ret;
  564. ret = vidioc_try_fmt_vid_cap(file, priv, f);
  565. if (ret)
  566. return ret;
  567. return vidioc_s_fmt(priv, f);
  568. }
  569. static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
  570. struct v4l2_format *f)
  571. {
  572. struct deinterlace_ctx *ctx = priv;
  573. int ret;
  574. ret = vidioc_try_fmt_vid_out(file, priv, f);
  575. if (ret)
  576. return ret;
  577. ret = vidioc_s_fmt(priv, f);
  578. if (!ret)
  579. ctx->colorspace = f->fmt.pix.colorspace;
  580. return ret;
  581. }
  582. static int vidioc_reqbufs(struct file *file, void *priv,
  583. struct v4l2_requestbuffers *reqbufs)
  584. {
  585. struct deinterlace_ctx *ctx = priv;
  586. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  587. }
  588. static int vidioc_querybuf(struct file *file, void *priv,
  589. struct v4l2_buffer *buf)
  590. {
  591. struct deinterlace_ctx *ctx = priv;
  592. return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
  593. }
  594. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  595. {
  596. struct deinterlace_ctx *ctx = priv;
  597. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  598. }
  599. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  600. {
  601. struct deinterlace_ctx *ctx = priv;
  602. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  603. }
  604. static int vidioc_streamon(struct file *file, void *priv,
  605. enum v4l2_buf_type type)
  606. {
  607. struct deinterlace_q_data *s_q_data, *d_q_data;
  608. struct deinterlace_ctx *ctx = priv;
  609. s_q_data = get_q_data(V4L2_BUF_TYPE_VIDEO_OUTPUT);
  610. d_q_data = get_q_data(V4L2_BUF_TYPE_VIDEO_CAPTURE);
  611. /* Check that src and dst queues have the same pix format */
  612. if (s_q_data->fmt->fourcc != d_q_data->fmt->fourcc) {
  613. v4l2_err(&ctx->dev->v4l2_dev,
  614. "src and dst formats don't match.\n");
  615. return -EINVAL;
  616. }
  617. /* Check that input and output deinterlacing types are compatible */
  618. switch (s_q_data->field) {
  619. case V4L2_FIELD_SEQ_BT:
  620. if (d_q_data->field != V4L2_FIELD_NONE &&
  621. d_q_data->field != V4L2_FIELD_INTERLACED_BT) {
  622. v4l2_err(&ctx->dev->v4l2_dev,
  623. "src and dst field conversion [(%d)->(%d)] not supported.\n",
  624. s_q_data->field, d_q_data->field);
  625. return -EINVAL;
  626. }
  627. break;
  628. case V4L2_FIELD_SEQ_TB:
  629. if (d_q_data->field != V4L2_FIELD_NONE &&
  630. d_q_data->field != V4L2_FIELD_INTERLACED_TB) {
  631. v4l2_err(&ctx->dev->v4l2_dev,
  632. "src and dst field conversion [(%d)->(%d)] not supported.\n",
  633. s_q_data->field, d_q_data->field);
  634. return -EINVAL;
  635. }
  636. break;
  637. default:
  638. return -EINVAL;
  639. }
  640. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  641. }
  642. static int vidioc_streamoff(struct file *file, void *priv,
  643. enum v4l2_buf_type type)
  644. {
  645. struct deinterlace_ctx *ctx = priv;
  646. return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
  647. }
  648. static const struct v4l2_ioctl_ops deinterlace_ioctl_ops = {
  649. .vidioc_querycap = vidioc_querycap,
  650. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  651. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  652. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  653. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  654. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  655. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  656. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  657. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  658. .vidioc_reqbufs = vidioc_reqbufs,
  659. .vidioc_querybuf = vidioc_querybuf,
  660. .vidioc_qbuf = vidioc_qbuf,
  661. .vidioc_dqbuf = vidioc_dqbuf,
  662. .vidioc_streamon = vidioc_streamon,
  663. .vidioc_streamoff = vidioc_streamoff,
  664. };
  665. /*
  666. * Queue operations
  667. */
  668. struct vb2_dc_conf {
  669. struct device *dev;
  670. };
  671. static int deinterlace_queue_setup(struct vb2_queue *vq,
  672. unsigned int *nbuffers, unsigned int *nplanes,
  673. unsigned int sizes[], struct device *alloc_devs[])
  674. {
  675. struct deinterlace_ctx *ctx = vb2_get_drv_priv(vq);
  676. struct deinterlace_q_data *q_data;
  677. unsigned int size, count = *nbuffers;
  678. q_data = get_q_data(vq->type);
  679. switch (q_data->fmt->fourcc) {
  680. case V4L2_PIX_FMT_YUV420:
  681. size = q_data->width * q_data->height * 3 / 2;
  682. break;
  683. case V4L2_PIX_FMT_YUYV:
  684. default:
  685. size = q_data->width * q_data->height * 2;
  686. }
  687. *nplanes = 1;
  688. *nbuffers = count;
  689. sizes[0] = size;
  690. dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
  691. return 0;
  692. }
  693. static int deinterlace_buf_prepare(struct vb2_buffer *vb)
  694. {
  695. struct deinterlace_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  696. struct deinterlace_q_data *q_data;
  697. dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
  698. q_data = get_q_data(vb->vb2_queue->type);
  699. if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
  700. dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n",
  701. __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage);
  702. return -EINVAL;
  703. }
  704. vb2_set_plane_payload(vb, 0, q_data->sizeimage);
  705. return 0;
  706. }
  707. static void deinterlace_buf_queue(struct vb2_buffer *vb)
  708. {
  709. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  710. struct deinterlace_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  711. v4l2_m2m_buf_queue(ctx->m2m_ctx, vbuf);
  712. }
  713. static const struct vb2_ops deinterlace_qops = {
  714. .queue_setup = deinterlace_queue_setup,
  715. .buf_prepare = deinterlace_buf_prepare,
  716. .buf_queue = deinterlace_buf_queue,
  717. };
  718. static int queue_init(void *priv, struct vb2_queue *src_vq,
  719. struct vb2_queue *dst_vq)
  720. {
  721. struct deinterlace_ctx *ctx = priv;
  722. int ret;
  723. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  724. src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  725. src_vq->drv_priv = ctx;
  726. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  727. src_vq->ops = &deinterlace_qops;
  728. src_vq->mem_ops = &vb2_dma_contig_memops;
  729. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  730. src_vq->dev = ctx->dev->v4l2_dev.dev;
  731. q_data[V4L2_M2M_SRC].fmt = &formats[0];
  732. q_data[V4L2_M2M_SRC].width = 640;
  733. q_data[V4L2_M2M_SRC].height = 480;
  734. q_data[V4L2_M2M_SRC].sizeimage = (640 * 480 * 3) / 2;
  735. q_data[V4L2_M2M_SRC].field = V4L2_FIELD_SEQ_TB;
  736. ret = vb2_queue_init(src_vq);
  737. if (ret)
  738. return ret;
  739. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  740. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  741. dst_vq->drv_priv = ctx;
  742. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  743. dst_vq->ops = &deinterlace_qops;
  744. dst_vq->mem_ops = &vb2_dma_contig_memops;
  745. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  746. dst_vq->dev = ctx->dev->v4l2_dev.dev;
  747. q_data[V4L2_M2M_DST].fmt = &formats[0];
  748. q_data[V4L2_M2M_DST].width = 640;
  749. q_data[V4L2_M2M_DST].height = 480;
  750. q_data[V4L2_M2M_DST].sizeimage = (640 * 480 * 3) / 2;
  751. q_data[V4L2_M2M_SRC].field = V4L2_FIELD_INTERLACED_TB;
  752. return vb2_queue_init(dst_vq);
  753. }
  754. /*
  755. * File operations
  756. */
  757. static int deinterlace_open(struct file *file)
  758. {
  759. struct deinterlace_dev *pcdev = video_drvdata(file);
  760. struct deinterlace_ctx *ctx = NULL;
  761. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  762. if (!ctx)
  763. return -ENOMEM;
  764. file->private_data = ctx;
  765. ctx->dev = pcdev;
  766. ctx->m2m_ctx = v4l2_m2m_ctx_init(pcdev->m2m_dev, ctx, &queue_init);
  767. if (IS_ERR(ctx->m2m_ctx)) {
  768. int ret = PTR_ERR(ctx->m2m_ctx);
  769. kfree(ctx);
  770. return ret;
  771. }
  772. ctx->xt = kzalloc(sizeof(struct dma_interleaved_template) +
  773. sizeof(struct data_chunk), GFP_KERNEL);
  774. if (!ctx->xt) {
  775. kfree(ctx);
  776. return -ENOMEM;
  777. }
  778. ctx->colorspace = V4L2_COLORSPACE_REC709;
  779. dprintk(pcdev, "Created instance %p, m2m_ctx: %p\n", ctx, ctx->m2m_ctx);
  780. return 0;
  781. }
  782. static int deinterlace_release(struct file *file)
  783. {
  784. struct deinterlace_dev *pcdev = video_drvdata(file);
  785. struct deinterlace_ctx *ctx = file->private_data;
  786. dprintk(pcdev, "Releasing instance %p\n", ctx);
  787. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  788. kfree(ctx->xt);
  789. kfree(ctx);
  790. return 0;
  791. }
  792. static unsigned int deinterlace_poll(struct file *file,
  793. struct poll_table_struct *wait)
  794. {
  795. struct deinterlace_ctx *ctx = file->private_data;
  796. int ret;
  797. deinterlace_lock(ctx);
  798. ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
  799. deinterlace_unlock(ctx);
  800. return ret;
  801. }
  802. static int deinterlace_mmap(struct file *file, struct vm_area_struct *vma)
  803. {
  804. struct deinterlace_ctx *ctx = file->private_data;
  805. return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
  806. }
  807. static const struct v4l2_file_operations deinterlace_fops = {
  808. .owner = THIS_MODULE,
  809. .open = deinterlace_open,
  810. .release = deinterlace_release,
  811. .poll = deinterlace_poll,
  812. .unlocked_ioctl = video_ioctl2,
  813. .mmap = deinterlace_mmap,
  814. };
  815. static const struct video_device deinterlace_videodev = {
  816. .name = MEM2MEM_NAME,
  817. .fops = &deinterlace_fops,
  818. .ioctl_ops = &deinterlace_ioctl_ops,
  819. .minor = -1,
  820. .release = video_device_release_empty,
  821. .vfl_dir = VFL_DIR_M2M,
  822. };
  823. static const struct v4l2_m2m_ops m2m_ops = {
  824. .device_run = deinterlace_device_run,
  825. .job_ready = deinterlace_job_ready,
  826. .job_abort = deinterlace_job_abort,
  827. .lock = deinterlace_lock,
  828. .unlock = deinterlace_unlock,
  829. };
  830. static int deinterlace_probe(struct platform_device *pdev)
  831. {
  832. struct deinterlace_dev *pcdev;
  833. struct video_device *vfd;
  834. dma_cap_mask_t mask;
  835. int ret = 0;
  836. pcdev = devm_kzalloc(&pdev->dev, sizeof(*pcdev), GFP_KERNEL);
  837. if (!pcdev)
  838. return -ENOMEM;
  839. spin_lock_init(&pcdev->irqlock);
  840. dma_cap_zero(mask);
  841. dma_cap_set(DMA_INTERLEAVE, mask);
  842. pcdev->dma_chan = dma_request_channel(mask, NULL, pcdev);
  843. if (!pcdev->dma_chan)
  844. return -ENODEV;
  845. if (!dma_has_cap(DMA_INTERLEAVE, pcdev->dma_chan->device->cap_mask)) {
  846. dev_err(&pdev->dev, "DMA does not support INTERLEAVE\n");
  847. ret = -ENODEV;
  848. goto rel_dma;
  849. }
  850. ret = v4l2_device_register(&pdev->dev, &pcdev->v4l2_dev);
  851. if (ret)
  852. goto rel_dma;
  853. atomic_set(&pcdev->busy, 0);
  854. mutex_init(&pcdev->dev_mutex);
  855. vfd = &pcdev->vfd;
  856. *vfd = deinterlace_videodev;
  857. vfd->lock = &pcdev->dev_mutex;
  858. vfd->v4l2_dev = &pcdev->v4l2_dev;
  859. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  860. if (ret) {
  861. v4l2_err(&pcdev->v4l2_dev, "Failed to register video device\n");
  862. goto unreg_dev;
  863. }
  864. video_set_drvdata(vfd, pcdev);
  865. snprintf(vfd->name, sizeof(vfd->name), "%s", deinterlace_videodev.name);
  866. v4l2_info(&pcdev->v4l2_dev, MEM2MEM_TEST_MODULE_NAME
  867. " Device registered as /dev/video%d\n", vfd->num);
  868. platform_set_drvdata(pdev, pcdev);
  869. pcdev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  870. if (IS_ERR(pcdev->m2m_dev)) {
  871. v4l2_err(&pcdev->v4l2_dev, "Failed to init mem2mem device\n");
  872. ret = PTR_ERR(pcdev->m2m_dev);
  873. goto err_m2m;
  874. }
  875. return 0;
  876. err_m2m:
  877. video_unregister_device(&pcdev->vfd);
  878. unreg_dev:
  879. v4l2_device_unregister(&pcdev->v4l2_dev);
  880. rel_dma:
  881. dma_release_channel(pcdev->dma_chan);
  882. return ret;
  883. }
  884. static int deinterlace_remove(struct platform_device *pdev)
  885. {
  886. struct deinterlace_dev *pcdev = platform_get_drvdata(pdev);
  887. v4l2_info(&pcdev->v4l2_dev, "Removing " MEM2MEM_TEST_MODULE_NAME);
  888. v4l2_m2m_release(pcdev->m2m_dev);
  889. video_unregister_device(&pcdev->vfd);
  890. v4l2_device_unregister(&pcdev->v4l2_dev);
  891. dma_release_channel(pcdev->dma_chan);
  892. return 0;
  893. }
  894. static struct platform_driver deinterlace_pdrv = {
  895. .probe = deinterlace_probe,
  896. .remove = deinterlace_remove,
  897. .driver = {
  898. .name = MEM2MEM_NAME,
  899. },
  900. };
  901. module_platform_driver(deinterlace_pdrv);