m2m-deinterlace.c 28 KB

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