m2m-deinterlace.c 28 KB

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