m2m-deinterlace.c 26 KB

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