at v5.0 1145 lines 28 kB view raw
1/* 2 * A virtual v4l2-mem2mem example device. 3 * 4 * This is a virtual device driver for testing mem-to-mem videobuf framework. 5 * It simulates a device that uses memory buffers for both source and 6 * destination, processes the data and issues an "irq" (simulated by a delayed 7 * workqueue). 8 * The device is capable of multi-instance, multi-buffer-per-transaction 9 * operation (via the mem2mem framework). 10 * 11 * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd. 12 * Pawel Osciak, <pawel@osciak.com> 13 * Marek Szyprowski, <m.szyprowski@samsung.com> 14 * 15 * This program is free software; you can redistribute it and/or modify 16 * it under the terms of the GNU General Public License as published by the 17 * Free Software Foundation; either version 2 of the 18 * License, or (at your option) any later version 19 */ 20#include <linux/module.h> 21#include <linux/delay.h> 22#include <linux/fs.h> 23#include <linux/sched.h> 24#include <linux/slab.h> 25 26#include <linux/platform_device.h> 27#include <media/v4l2-mem2mem.h> 28#include <media/v4l2-device.h> 29#include <media/v4l2-ioctl.h> 30#include <media/v4l2-ctrls.h> 31#include <media/v4l2-event.h> 32#include <media/videobuf2-vmalloc.h> 33 34MODULE_DESCRIPTION("Virtual device for mem2mem framework testing"); 35MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>"); 36MODULE_LICENSE("GPL"); 37MODULE_VERSION("0.1.1"); 38MODULE_ALIAS("mem2mem_testdev"); 39 40static unsigned debug; 41module_param(debug, uint, 0644); 42MODULE_PARM_DESC(debug, "activates debug info"); 43 44#define MIN_W 32 45#define MIN_H 32 46#define MAX_W 640 47#define MAX_H 480 48#define DIM_ALIGN_MASK 7 /* 8-byte alignment for line length */ 49 50/* Flags that indicate a format can be used for capture/output */ 51#define MEM2MEM_CAPTURE (1 << 0) 52#define MEM2MEM_OUTPUT (1 << 1) 53 54#define MEM2MEM_NAME "vim2m" 55 56/* Per queue */ 57#define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME 58/* In bytes, per queue */ 59#define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024) 60 61/* Default transaction time in msec */ 62#define MEM2MEM_DEF_TRANSTIME 40 63#define MEM2MEM_COLOR_STEP (0xff >> 4) 64#define MEM2MEM_NUM_TILES 8 65 66/* Flags that indicate processing mode */ 67#define MEM2MEM_HFLIP (1 << 0) 68#define MEM2MEM_VFLIP (1 << 1) 69 70#define dprintk(dev, fmt, arg...) \ 71 v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg) 72 73 74static void vim2m_dev_release(struct device *dev) 75{} 76 77static struct platform_device vim2m_pdev = { 78 .name = MEM2MEM_NAME, 79 .dev.release = vim2m_dev_release, 80}; 81 82struct vim2m_fmt { 83 u32 fourcc; 84 int depth; 85 /* Types the format can be used for */ 86 u32 types; 87}; 88 89static struct vim2m_fmt formats[] = { 90 { 91 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */ 92 .depth = 16, 93 /* Both capture and output format */ 94 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT, 95 }, 96 { 97 .fourcc = V4L2_PIX_FMT_YUYV, 98 .depth = 16, 99 /* Output-only format */ 100 .types = MEM2MEM_OUTPUT, 101 }, 102}; 103 104#define NUM_FORMATS ARRAY_SIZE(formats) 105 106/* Per-queue, driver-specific private data */ 107struct vim2m_q_data { 108 unsigned int width; 109 unsigned int height; 110 unsigned int sizeimage; 111 unsigned int sequence; 112 struct vim2m_fmt *fmt; 113}; 114 115enum { 116 V4L2_M2M_SRC = 0, 117 V4L2_M2M_DST = 1, 118}; 119 120#define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000) 121#define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001) 122 123static struct vim2m_fmt *find_format(struct v4l2_format *f) 124{ 125 struct vim2m_fmt *fmt; 126 unsigned int k; 127 128 for (k = 0; k < NUM_FORMATS; k++) { 129 fmt = &formats[k]; 130 if (fmt->fourcc == f->fmt.pix.pixelformat) 131 break; 132 } 133 134 if (k == NUM_FORMATS) 135 return NULL; 136 137 return &formats[k]; 138} 139 140struct vim2m_dev { 141 struct v4l2_device v4l2_dev; 142 struct video_device vfd; 143#ifdef CONFIG_MEDIA_CONTROLLER 144 struct media_device mdev; 145#endif 146 147 atomic_t num_inst; 148 struct mutex dev_mutex; 149 spinlock_t irqlock; 150 151 struct delayed_work work_run; 152 153 struct v4l2_m2m_dev *m2m_dev; 154}; 155 156struct vim2m_ctx { 157 struct v4l2_fh fh; 158 struct vim2m_dev *dev; 159 160 struct v4l2_ctrl_handler hdl; 161 162 /* Processed buffers in this transaction */ 163 u8 num_processed; 164 165 /* Transaction length (i.e. how many buffers per transaction) */ 166 u32 translen; 167 /* Transaction time (i.e. simulated processing time) in milliseconds */ 168 u32 transtime; 169 170 /* Abort requested by m2m */ 171 int aborting; 172 173 /* Processing mode */ 174 int mode; 175 176 enum v4l2_colorspace colorspace; 177 enum v4l2_ycbcr_encoding ycbcr_enc; 178 enum v4l2_xfer_func xfer_func; 179 enum v4l2_quantization quant; 180 181 /* Source and destination queue data */ 182 struct vim2m_q_data q_data[2]; 183}; 184 185static inline struct vim2m_ctx *file2ctx(struct file *file) 186{ 187 return container_of(file->private_data, struct vim2m_ctx, fh); 188} 189 190static struct vim2m_q_data *get_q_data(struct vim2m_ctx *ctx, 191 enum v4l2_buf_type type) 192{ 193 switch (type) { 194 case V4L2_BUF_TYPE_VIDEO_OUTPUT: 195 return &ctx->q_data[V4L2_M2M_SRC]; 196 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 197 return &ctx->q_data[V4L2_M2M_DST]; 198 default: 199 BUG(); 200 } 201 return NULL; 202} 203 204 205static int device_process(struct vim2m_ctx *ctx, 206 struct vb2_v4l2_buffer *in_vb, 207 struct vb2_v4l2_buffer *out_vb) 208{ 209 struct vim2m_dev *dev = ctx->dev; 210 struct vim2m_q_data *q_data; 211 u8 *p_in, *p_out; 212 int x, y, t, w; 213 int tile_w, bytes_left; 214 int width, height, bytesperline; 215 216 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); 217 218 width = q_data->width; 219 height = q_data->height; 220 bytesperline = (q_data->width * q_data->fmt->depth) >> 3; 221 222 p_in = vb2_plane_vaddr(&in_vb->vb2_buf, 0); 223 p_out = vb2_plane_vaddr(&out_vb->vb2_buf, 0); 224 if (!p_in || !p_out) { 225 v4l2_err(&dev->v4l2_dev, 226 "Acquiring kernel pointers to buffers failed\n"); 227 return -EFAULT; 228 } 229 230 if (vb2_plane_size(&in_vb->vb2_buf, 0) > 231 vb2_plane_size(&out_vb->vb2_buf, 0)) { 232 v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n"); 233 return -EINVAL; 234 } 235 236 tile_w = (width * (q_data[V4L2_M2M_DST].fmt->depth >> 3)) 237 / MEM2MEM_NUM_TILES; 238 bytes_left = bytesperline - tile_w * MEM2MEM_NUM_TILES; 239 w = 0; 240 241 out_vb->sequence = 242 get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE)->sequence++; 243 in_vb->sequence = q_data->sequence++; 244 out_vb->vb2_buf.timestamp = in_vb->vb2_buf.timestamp; 245 246 if (in_vb->flags & V4L2_BUF_FLAG_TIMECODE) 247 out_vb->timecode = in_vb->timecode; 248 out_vb->field = in_vb->field; 249 out_vb->flags = in_vb->flags & 250 (V4L2_BUF_FLAG_TIMECODE | 251 V4L2_BUF_FLAG_KEYFRAME | 252 V4L2_BUF_FLAG_PFRAME | 253 V4L2_BUF_FLAG_BFRAME | 254 V4L2_BUF_FLAG_TSTAMP_SRC_MASK); 255 256 switch (ctx->mode) { 257 case MEM2MEM_HFLIP | MEM2MEM_VFLIP: 258 p_out += bytesperline * height - bytes_left; 259 for (y = 0; y < height; ++y) { 260 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) { 261 if (w & 0x1) { 262 for (x = 0; x < tile_w; ++x) 263 *--p_out = *p_in++ + 264 MEM2MEM_COLOR_STEP; 265 } else { 266 for (x = 0; x < tile_w; ++x) 267 *--p_out = *p_in++ - 268 MEM2MEM_COLOR_STEP; 269 } 270 ++w; 271 } 272 p_in += bytes_left; 273 p_out -= bytes_left; 274 } 275 break; 276 277 case MEM2MEM_HFLIP: 278 for (y = 0; y < height; ++y) { 279 p_out += MEM2MEM_NUM_TILES * tile_w; 280 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) { 281 if (w & 0x01) { 282 for (x = 0; x < tile_w; ++x) 283 *--p_out = *p_in++ + 284 MEM2MEM_COLOR_STEP; 285 } else { 286 for (x = 0; x < tile_w; ++x) 287 *--p_out = *p_in++ - 288 MEM2MEM_COLOR_STEP; 289 } 290 ++w; 291 } 292 p_in += bytes_left; 293 p_out += bytesperline; 294 } 295 break; 296 297 case MEM2MEM_VFLIP: 298 p_out += bytesperline * (height - 1); 299 for (y = 0; y < height; ++y) { 300 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) { 301 if (w & 0x1) { 302 for (x = 0; x < tile_w; ++x) 303 *p_out++ = *p_in++ + 304 MEM2MEM_COLOR_STEP; 305 } else { 306 for (x = 0; x < tile_w; ++x) 307 *p_out++ = *p_in++ - 308 MEM2MEM_COLOR_STEP; 309 } 310 ++w; 311 } 312 p_in += bytes_left; 313 p_out += bytes_left - 2 * bytesperline; 314 } 315 break; 316 317 default: 318 for (y = 0; y < height; ++y) { 319 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) { 320 if (w & 0x1) { 321 for (x = 0; x < tile_w; ++x) 322 *p_out++ = *p_in++ + 323 MEM2MEM_COLOR_STEP; 324 } else { 325 for (x = 0; x < tile_w; ++x) 326 *p_out++ = *p_in++ - 327 MEM2MEM_COLOR_STEP; 328 } 329 ++w; 330 } 331 p_in += bytes_left; 332 p_out += bytes_left; 333 } 334 } 335 336 return 0; 337} 338 339/* 340 * mem2mem callbacks 341 */ 342 343/* 344 * job_ready() - check whether an instance is ready to be scheduled to run 345 */ 346static int job_ready(void *priv) 347{ 348 struct vim2m_ctx *ctx = priv; 349 350 if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen 351 || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen) { 352 dprintk(ctx->dev, "Not enough buffers available\n"); 353 return 0; 354 } 355 356 return 1; 357} 358 359static void job_abort(void *priv) 360{ 361 struct vim2m_ctx *ctx = priv; 362 363 /* Will cancel the transaction in the next interrupt handler */ 364 ctx->aborting = 1; 365} 366 367/* device_run() - prepares and starts the device 368 * 369 * This simulates all the immediate preparations required before starting 370 * a device. This will be called by the framework when it decides to schedule 371 * a particular instance. 372 */ 373static void device_run(void *priv) 374{ 375 struct vim2m_ctx *ctx = priv; 376 struct vim2m_dev *dev = ctx->dev; 377 struct vb2_v4l2_buffer *src_buf, *dst_buf; 378 379 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); 380 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); 381 382 /* Apply request controls if any */ 383 v4l2_ctrl_request_setup(src_buf->vb2_buf.req_obj.req, 384 &ctx->hdl); 385 386 device_process(ctx, src_buf, dst_buf); 387 388 /* Complete request controls if any */ 389 v4l2_ctrl_request_complete(src_buf->vb2_buf.req_obj.req, 390 &ctx->hdl); 391 392 /* Run delayed work, which simulates a hardware irq */ 393 schedule_delayed_work(&dev->work_run, msecs_to_jiffies(ctx->transtime)); 394} 395 396static void device_work(struct work_struct *w) 397{ 398 struct vim2m_dev *vim2m_dev = 399 container_of(w, struct vim2m_dev, work_run.work); 400 struct vim2m_ctx *curr_ctx; 401 struct vb2_v4l2_buffer *src_vb, *dst_vb; 402 unsigned long flags; 403 404 curr_ctx = v4l2_m2m_get_curr_priv(vim2m_dev->m2m_dev); 405 406 if (NULL == curr_ctx) { 407 pr_err("Instance released before the end of transaction\n"); 408 return; 409 } 410 411 src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx); 412 dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx); 413 414 curr_ctx->num_processed++; 415 416 spin_lock_irqsave(&vim2m_dev->irqlock, flags); 417 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE); 418 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE); 419 spin_unlock_irqrestore(&vim2m_dev->irqlock, flags); 420 421 if (curr_ctx->num_processed == curr_ctx->translen 422 || curr_ctx->aborting) { 423 dprintk(curr_ctx->dev, "Finishing transaction\n"); 424 curr_ctx->num_processed = 0; 425 v4l2_m2m_job_finish(vim2m_dev->m2m_dev, curr_ctx->fh.m2m_ctx); 426 } else { 427 device_run(curr_ctx); 428 } 429} 430 431/* 432 * video ioctls 433 */ 434static int vidioc_querycap(struct file *file, void *priv, 435 struct v4l2_capability *cap) 436{ 437 strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1); 438 strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1); 439 snprintf(cap->bus_info, sizeof(cap->bus_info), 440 "platform:%s", MEM2MEM_NAME); 441 return 0; 442} 443 444static int enum_fmt(struct v4l2_fmtdesc *f, u32 type) 445{ 446 int i, num; 447 struct vim2m_fmt *fmt; 448 449 num = 0; 450 451 for (i = 0; i < NUM_FORMATS; ++i) { 452 if (formats[i].types & type) { 453 /* index-th format of type type found ? */ 454 if (num == f->index) 455 break; 456 /* Correct type but haven't reached our index yet, 457 * just increment per-type index */ 458 ++num; 459 } 460 } 461 462 if (i < NUM_FORMATS) { 463 /* Format found */ 464 fmt = &formats[i]; 465 f->pixelformat = fmt->fourcc; 466 return 0; 467 } 468 469 /* Format not found */ 470 return -EINVAL; 471} 472 473static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, 474 struct v4l2_fmtdesc *f) 475{ 476 return enum_fmt(f, MEM2MEM_CAPTURE); 477} 478 479static int vidioc_enum_fmt_vid_out(struct file *file, void *priv, 480 struct v4l2_fmtdesc *f) 481{ 482 return enum_fmt(f, MEM2MEM_OUTPUT); 483} 484 485static int vidioc_g_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f) 486{ 487 struct vb2_queue *vq; 488 struct vim2m_q_data *q_data; 489 490 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); 491 if (!vq) 492 return -EINVAL; 493 494 q_data = get_q_data(ctx, f->type); 495 496 f->fmt.pix.width = q_data->width; 497 f->fmt.pix.height = q_data->height; 498 f->fmt.pix.field = V4L2_FIELD_NONE; 499 f->fmt.pix.pixelformat = q_data->fmt->fourcc; 500 f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3; 501 f->fmt.pix.sizeimage = q_data->sizeimage; 502 f->fmt.pix.colorspace = ctx->colorspace; 503 f->fmt.pix.xfer_func = ctx->xfer_func; 504 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc; 505 f->fmt.pix.quantization = ctx->quant; 506 507 return 0; 508} 509 510static int vidioc_g_fmt_vid_out(struct file *file, void *priv, 511 struct v4l2_format *f) 512{ 513 return vidioc_g_fmt(file2ctx(file), f); 514} 515 516static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, 517 struct v4l2_format *f) 518{ 519 return vidioc_g_fmt(file2ctx(file), f); 520} 521 522static int vidioc_try_fmt(struct v4l2_format *f, struct vim2m_fmt *fmt) 523{ 524 /* V4L2 specification suggests the driver corrects the format struct 525 * if any of the dimensions is unsupported */ 526 if (f->fmt.pix.height < MIN_H) 527 f->fmt.pix.height = MIN_H; 528 else if (f->fmt.pix.height > MAX_H) 529 f->fmt.pix.height = MAX_H; 530 531 if (f->fmt.pix.width < MIN_W) 532 f->fmt.pix.width = MIN_W; 533 else if (f->fmt.pix.width > MAX_W) 534 f->fmt.pix.width = MAX_W; 535 536 f->fmt.pix.width &= ~DIM_ALIGN_MASK; 537 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3; 538 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline; 539 f->fmt.pix.field = V4L2_FIELD_NONE; 540 541 return 0; 542} 543 544static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, 545 struct v4l2_format *f) 546{ 547 struct vim2m_fmt *fmt; 548 struct vim2m_ctx *ctx = file2ctx(file); 549 550 fmt = find_format(f); 551 if (!fmt) { 552 f->fmt.pix.pixelformat = formats[0].fourcc; 553 fmt = find_format(f); 554 } 555 if (!(fmt->types & MEM2MEM_CAPTURE)) { 556 v4l2_err(&ctx->dev->v4l2_dev, 557 "Fourcc format (0x%08x) invalid.\n", 558 f->fmt.pix.pixelformat); 559 return -EINVAL; 560 } 561 f->fmt.pix.colorspace = ctx->colorspace; 562 f->fmt.pix.xfer_func = ctx->xfer_func; 563 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc; 564 f->fmt.pix.quantization = ctx->quant; 565 566 return vidioc_try_fmt(f, fmt); 567} 568 569static int vidioc_try_fmt_vid_out(struct file *file, void *priv, 570 struct v4l2_format *f) 571{ 572 struct vim2m_fmt *fmt; 573 struct vim2m_ctx *ctx = file2ctx(file); 574 575 fmt = find_format(f); 576 if (!fmt) { 577 f->fmt.pix.pixelformat = formats[0].fourcc; 578 fmt = find_format(f); 579 } 580 if (!(fmt->types & MEM2MEM_OUTPUT)) { 581 v4l2_err(&ctx->dev->v4l2_dev, 582 "Fourcc format (0x%08x) invalid.\n", 583 f->fmt.pix.pixelformat); 584 return -EINVAL; 585 } 586 if (!f->fmt.pix.colorspace) 587 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709; 588 589 return vidioc_try_fmt(f, fmt); 590} 591 592static int vidioc_s_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f) 593{ 594 struct vim2m_q_data *q_data; 595 struct vb2_queue *vq; 596 597 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); 598 if (!vq) 599 return -EINVAL; 600 601 q_data = get_q_data(ctx, f->type); 602 if (!q_data) 603 return -EINVAL; 604 605 if (vb2_is_busy(vq)) { 606 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__); 607 return -EBUSY; 608 } 609 610 q_data->fmt = find_format(f); 611 q_data->width = f->fmt.pix.width; 612 q_data->height = f->fmt.pix.height; 613 q_data->sizeimage = q_data->width * q_data->height 614 * q_data->fmt->depth >> 3; 615 616 dprintk(ctx->dev, 617 "Setting format for type %d, wxh: %dx%d, fmt: %d\n", 618 f->type, q_data->width, q_data->height, q_data->fmt->fourcc); 619 620 return 0; 621} 622 623static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, 624 struct v4l2_format *f) 625{ 626 int ret; 627 628 ret = vidioc_try_fmt_vid_cap(file, priv, f); 629 if (ret) 630 return ret; 631 632 return vidioc_s_fmt(file2ctx(file), f); 633} 634 635static int vidioc_s_fmt_vid_out(struct file *file, void *priv, 636 struct v4l2_format *f) 637{ 638 struct vim2m_ctx *ctx = file2ctx(file); 639 int ret; 640 641 ret = vidioc_try_fmt_vid_out(file, priv, f); 642 if (ret) 643 return ret; 644 645 ret = vidioc_s_fmt(file2ctx(file), f); 646 if (!ret) { 647 ctx->colorspace = f->fmt.pix.colorspace; 648 ctx->xfer_func = f->fmt.pix.xfer_func; 649 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc; 650 ctx->quant = f->fmt.pix.quantization; 651 } 652 return ret; 653} 654 655static int vim2m_s_ctrl(struct v4l2_ctrl *ctrl) 656{ 657 struct vim2m_ctx *ctx = 658 container_of(ctrl->handler, struct vim2m_ctx, hdl); 659 660 switch (ctrl->id) { 661 case V4L2_CID_HFLIP: 662 if (ctrl->val) 663 ctx->mode |= MEM2MEM_HFLIP; 664 else 665 ctx->mode &= ~MEM2MEM_HFLIP; 666 break; 667 668 case V4L2_CID_VFLIP: 669 if (ctrl->val) 670 ctx->mode |= MEM2MEM_VFLIP; 671 else 672 ctx->mode &= ~MEM2MEM_VFLIP; 673 break; 674 675 case V4L2_CID_TRANS_TIME_MSEC: 676 ctx->transtime = ctrl->val; 677 break; 678 679 case V4L2_CID_TRANS_NUM_BUFS: 680 ctx->translen = ctrl->val; 681 break; 682 683 default: 684 v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n"); 685 return -EINVAL; 686 } 687 688 return 0; 689} 690 691static const struct v4l2_ctrl_ops vim2m_ctrl_ops = { 692 .s_ctrl = vim2m_s_ctrl, 693}; 694 695 696static const struct v4l2_ioctl_ops vim2m_ioctl_ops = { 697 .vidioc_querycap = vidioc_querycap, 698 699 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, 700 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, 701 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, 702 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, 703 704 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out, 705 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out, 706 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out, 707 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out, 708 709 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, 710 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, 711 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, 712 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, 713 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, 714 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, 715 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, 716 717 .vidioc_streamon = v4l2_m2m_ioctl_streamon, 718 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, 719 720 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 721 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 722}; 723 724 725/* 726 * Queue operations 727 */ 728 729static int vim2m_queue_setup(struct vb2_queue *vq, 730 unsigned int *nbuffers, unsigned int *nplanes, 731 unsigned int sizes[], struct device *alloc_devs[]) 732{ 733 struct vim2m_ctx *ctx = vb2_get_drv_priv(vq); 734 struct vim2m_q_data *q_data; 735 unsigned int size, count = *nbuffers; 736 737 q_data = get_q_data(ctx, vq->type); 738 739 size = q_data->width * q_data->height * q_data->fmt->depth >> 3; 740 741 while (size * count > MEM2MEM_VID_MEM_LIMIT) 742 (count)--; 743 *nbuffers = count; 744 745 if (*nplanes) 746 return sizes[0] < size ? -EINVAL : 0; 747 748 *nplanes = 1; 749 sizes[0] = size; 750 751 dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size); 752 753 return 0; 754} 755 756static int vim2m_buf_prepare(struct vb2_buffer *vb) 757{ 758 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 759 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 760 struct vim2m_q_data *q_data; 761 762 dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type); 763 764 q_data = get_q_data(ctx, vb->vb2_queue->type); 765 if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) { 766 if (vbuf->field == V4L2_FIELD_ANY) 767 vbuf->field = V4L2_FIELD_NONE; 768 if (vbuf->field != V4L2_FIELD_NONE) { 769 dprintk(ctx->dev, "%s field isn't supported\n", 770 __func__); 771 return -EINVAL; 772 } 773 } 774 775 if (vb2_plane_size(vb, 0) < q_data->sizeimage) { 776 dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n", 777 __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage); 778 return -EINVAL; 779 } 780 781 vb2_set_plane_payload(vb, 0, q_data->sizeimage); 782 783 return 0; 784} 785 786static void vim2m_buf_queue(struct vb2_buffer *vb) 787{ 788 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 789 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 790 791 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf); 792} 793 794static int vim2m_start_streaming(struct vb2_queue *q, unsigned count) 795{ 796 struct vim2m_ctx *ctx = vb2_get_drv_priv(q); 797 struct vim2m_q_data *q_data = get_q_data(ctx, q->type); 798 799 q_data->sequence = 0; 800 return 0; 801} 802 803static void vim2m_stop_streaming(struct vb2_queue *q) 804{ 805 struct vim2m_ctx *ctx = vb2_get_drv_priv(q); 806 struct vim2m_dev *dev = ctx->dev; 807 struct vb2_v4l2_buffer *vbuf; 808 unsigned long flags; 809 810 if (v4l2_m2m_get_curr_priv(dev->m2m_dev) == ctx) 811 cancel_delayed_work_sync(&dev->work_run); 812 813 for (;;) { 814 if (V4L2_TYPE_IS_OUTPUT(q->type)) 815 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 816 else 817 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); 818 if (vbuf == NULL) 819 return; 820 v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req, 821 &ctx->hdl); 822 spin_lock_irqsave(&ctx->dev->irqlock, flags); 823 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR); 824 spin_unlock_irqrestore(&ctx->dev->irqlock, flags); 825 } 826} 827 828static void vim2m_buf_request_complete(struct vb2_buffer *vb) 829{ 830 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 831 832 v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl); 833} 834 835static const struct vb2_ops vim2m_qops = { 836 .queue_setup = vim2m_queue_setup, 837 .buf_prepare = vim2m_buf_prepare, 838 .buf_queue = vim2m_buf_queue, 839 .start_streaming = vim2m_start_streaming, 840 .stop_streaming = vim2m_stop_streaming, 841 .wait_prepare = vb2_ops_wait_prepare, 842 .wait_finish = vb2_ops_wait_finish, 843 .buf_request_complete = vim2m_buf_request_complete, 844}; 845 846static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq) 847{ 848 struct vim2m_ctx *ctx = priv; 849 int ret; 850 851 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; 852 src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; 853 src_vq->drv_priv = ctx; 854 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); 855 src_vq->ops = &vim2m_qops; 856 src_vq->mem_ops = &vb2_vmalloc_memops; 857 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 858 src_vq->lock = &ctx->dev->dev_mutex; 859 src_vq->supports_requests = true; 860 861 ret = vb2_queue_init(src_vq); 862 if (ret) 863 return ret; 864 865 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 866 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; 867 dst_vq->drv_priv = ctx; 868 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); 869 dst_vq->ops = &vim2m_qops; 870 dst_vq->mem_ops = &vb2_vmalloc_memops; 871 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 872 dst_vq->lock = &ctx->dev->dev_mutex; 873 874 return vb2_queue_init(dst_vq); 875} 876 877static const struct v4l2_ctrl_config vim2m_ctrl_trans_time_msec = { 878 .ops = &vim2m_ctrl_ops, 879 .id = V4L2_CID_TRANS_TIME_MSEC, 880 .name = "Transaction Time (msec)", 881 .type = V4L2_CTRL_TYPE_INTEGER, 882 .def = MEM2MEM_DEF_TRANSTIME, 883 .min = 1, 884 .max = 10001, 885 .step = 1, 886}; 887 888static const struct v4l2_ctrl_config vim2m_ctrl_trans_num_bufs = { 889 .ops = &vim2m_ctrl_ops, 890 .id = V4L2_CID_TRANS_NUM_BUFS, 891 .name = "Buffers Per Transaction", 892 .type = V4L2_CTRL_TYPE_INTEGER, 893 .def = 1, 894 .min = 1, 895 .max = MEM2MEM_DEF_NUM_BUFS, 896 .step = 1, 897}; 898 899/* 900 * File operations 901 */ 902static int vim2m_open(struct file *file) 903{ 904 struct vim2m_dev *dev = video_drvdata(file); 905 struct vim2m_ctx *ctx = NULL; 906 struct v4l2_ctrl_handler *hdl; 907 int rc = 0; 908 909 if (mutex_lock_interruptible(&dev->dev_mutex)) 910 return -ERESTARTSYS; 911 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 912 if (!ctx) { 913 rc = -ENOMEM; 914 goto open_unlock; 915 } 916 917 v4l2_fh_init(&ctx->fh, video_devdata(file)); 918 file->private_data = &ctx->fh; 919 ctx->dev = dev; 920 hdl = &ctx->hdl; 921 v4l2_ctrl_handler_init(hdl, 4); 922 v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0); 923 v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0); 924 v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_time_msec, NULL); 925 v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_num_bufs, NULL); 926 if (hdl->error) { 927 rc = hdl->error; 928 v4l2_ctrl_handler_free(hdl); 929 kfree(ctx); 930 goto open_unlock; 931 } 932 ctx->fh.ctrl_handler = hdl; 933 v4l2_ctrl_handler_setup(hdl); 934 935 ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0]; 936 ctx->q_data[V4L2_M2M_SRC].width = 640; 937 ctx->q_data[V4L2_M2M_SRC].height = 480; 938 ctx->q_data[V4L2_M2M_SRC].sizeimage = 939 ctx->q_data[V4L2_M2M_SRC].width * 940 ctx->q_data[V4L2_M2M_SRC].height * 941 (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3); 942 ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC]; 943 ctx->colorspace = V4L2_COLORSPACE_REC709; 944 945 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init); 946 947 if (IS_ERR(ctx->fh.m2m_ctx)) { 948 rc = PTR_ERR(ctx->fh.m2m_ctx); 949 950 v4l2_ctrl_handler_free(hdl); 951 v4l2_fh_exit(&ctx->fh); 952 kfree(ctx); 953 goto open_unlock; 954 } 955 956 v4l2_fh_add(&ctx->fh); 957 atomic_inc(&dev->num_inst); 958 959 dprintk(dev, "Created instance: %p, m2m_ctx: %p\n", 960 ctx, ctx->fh.m2m_ctx); 961 962open_unlock: 963 mutex_unlock(&dev->dev_mutex); 964 return rc; 965} 966 967static int vim2m_release(struct file *file) 968{ 969 struct vim2m_dev *dev = video_drvdata(file); 970 struct vim2m_ctx *ctx = file2ctx(file); 971 972 dprintk(dev, "Releasing instance %p\n", ctx); 973 974 v4l2_fh_del(&ctx->fh); 975 v4l2_fh_exit(&ctx->fh); 976 v4l2_ctrl_handler_free(&ctx->hdl); 977 mutex_lock(&dev->dev_mutex); 978 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); 979 mutex_unlock(&dev->dev_mutex); 980 kfree(ctx); 981 982 atomic_dec(&dev->num_inst); 983 984 return 0; 985} 986 987static const struct v4l2_file_operations vim2m_fops = { 988 .owner = THIS_MODULE, 989 .open = vim2m_open, 990 .release = vim2m_release, 991 .poll = v4l2_m2m_fop_poll, 992 .unlocked_ioctl = video_ioctl2, 993 .mmap = v4l2_m2m_fop_mmap, 994}; 995 996static const struct video_device vim2m_videodev = { 997 .name = MEM2MEM_NAME, 998 .vfl_dir = VFL_DIR_M2M, 999 .fops = &vim2m_fops, 1000 .ioctl_ops = &vim2m_ioctl_ops, 1001 .minor = -1, 1002 .release = video_device_release_empty, 1003 .device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING, 1004}; 1005 1006static const struct v4l2_m2m_ops m2m_ops = { 1007 .device_run = device_run, 1008 .job_ready = job_ready, 1009 .job_abort = job_abort, 1010}; 1011 1012static const struct media_device_ops m2m_media_ops = { 1013 .req_validate = vb2_request_validate, 1014 .req_queue = v4l2_m2m_request_queue, 1015}; 1016 1017static int vim2m_probe(struct platform_device *pdev) 1018{ 1019 struct vim2m_dev *dev; 1020 struct video_device *vfd; 1021 int ret; 1022 1023 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); 1024 if (!dev) 1025 return -ENOMEM; 1026 1027 spin_lock_init(&dev->irqlock); 1028 1029 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); 1030 if (ret) 1031 return ret; 1032 1033 atomic_set(&dev->num_inst, 0); 1034 mutex_init(&dev->dev_mutex); 1035 1036 dev->vfd = vim2m_videodev; 1037 vfd = &dev->vfd; 1038 vfd->lock = &dev->dev_mutex; 1039 vfd->v4l2_dev = &dev->v4l2_dev; 1040 INIT_DELAYED_WORK(&dev->work_run, device_work); 1041 1042 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0); 1043 if (ret) { 1044 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n"); 1045 goto unreg_v4l2; 1046 } 1047 1048 video_set_drvdata(vfd, dev); 1049 v4l2_info(&dev->v4l2_dev, 1050 "Device registered as /dev/video%d\n", vfd->num); 1051 1052 platform_set_drvdata(pdev, dev); 1053 1054 dev->m2m_dev = v4l2_m2m_init(&m2m_ops); 1055 if (IS_ERR(dev->m2m_dev)) { 1056 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n"); 1057 ret = PTR_ERR(dev->m2m_dev); 1058 goto unreg_dev; 1059 } 1060 1061#ifdef CONFIG_MEDIA_CONTROLLER 1062 dev->mdev.dev = &pdev->dev; 1063 strscpy(dev->mdev.model, "vim2m", sizeof(dev->mdev.model)); 1064 media_device_init(&dev->mdev); 1065 dev->mdev.ops = &m2m_media_ops; 1066 dev->v4l2_dev.mdev = &dev->mdev; 1067 1068 ret = v4l2_m2m_register_media_controller(dev->m2m_dev, 1069 vfd, MEDIA_ENT_F_PROC_VIDEO_SCALER); 1070 if (ret) { 1071 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller\n"); 1072 goto unreg_m2m; 1073 } 1074 1075 ret = media_device_register(&dev->mdev); 1076 if (ret) { 1077 v4l2_err(&dev->v4l2_dev, "Failed to register mem2mem media device\n"); 1078 goto unreg_m2m_mc; 1079 } 1080#endif 1081 return 0; 1082 1083#ifdef CONFIG_MEDIA_CONTROLLER 1084unreg_m2m_mc: 1085 v4l2_m2m_unregister_media_controller(dev->m2m_dev); 1086unreg_m2m: 1087 v4l2_m2m_release(dev->m2m_dev); 1088#endif 1089unreg_dev: 1090 video_unregister_device(&dev->vfd); 1091unreg_v4l2: 1092 v4l2_device_unregister(&dev->v4l2_dev); 1093 1094 return ret; 1095} 1096 1097static int vim2m_remove(struct platform_device *pdev) 1098{ 1099 struct vim2m_dev *dev = platform_get_drvdata(pdev); 1100 1101 v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME); 1102 1103#ifdef CONFIG_MEDIA_CONTROLLER 1104 media_device_unregister(&dev->mdev); 1105 v4l2_m2m_unregister_media_controller(dev->m2m_dev); 1106 media_device_cleanup(&dev->mdev); 1107#endif 1108 v4l2_m2m_release(dev->m2m_dev); 1109 video_unregister_device(&dev->vfd); 1110 v4l2_device_unregister(&dev->v4l2_dev); 1111 1112 return 0; 1113} 1114 1115static struct platform_driver vim2m_pdrv = { 1116 .probe = vim2m_probe, 1117 .remove = vim2m_remove, 1118 .driver = { 1119 .name = MEM2MEM_NAME, 1120 }, 1121}; 1122 1123static void __exit vim2m_exit(void) 1124{ 1125 platform_driver_unregister(&vim2m_pdrv); 1126 platform_device_unregister(&vim2m_pdev); 1127} 1128 1129static int __init vim2m_init(void) 1130{ 1131 int ret; 1132 1133 ret = platform_device_register(&vim2m_pdev); 1134 if (ret) 1135 return ret; 1136 1137 ret = platform_driver_register(&vim2m_pdrv); 1138 if (ret) 1139 platform_device_unregister(&vim2m_pdev); 1140 1141 return ret; 1142} 1143 1144module_init(vim2m_init); 1145module_exit(vim2m_exit);