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