m2m-deinterlace.c 28 KB

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