m2m-deinterlace.c 27 KB

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