1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * A virtual v4l2-mem2mem example device. 4 * 5 * This is a virtual device driver for testing mem-to-mem vb2 framework. 6 * It simulates a device that uses memory buffers for both source and 7 * destination, processes the data and issues an "irq" (simulated by a delayed 8 * workqueue). 9 * The device is capable of multi-instance, multi-buffer-per-transaction 10 * operation (via the mem2mem framework). 11 * 12 * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd. 13 * Pawel Osciak, <pawel@osciak.com> 14 * Marek Szyprowski, <m.szyprowski@samsung.com> 15 */ 16 #include <linux/module.h> 17 #include <linux/delay.h> 18 #include <linux/fs.h> 19 #include <linux/sched.h> 20 #include <linux/slab.h> 21 22 #include <linux/platform_device.h> 23 #include <media/v4l2-mem2mem.h> 24 #include <media/v4l2-device.h> 25 #include <media/v4l2-ioctl.h> 26 #include <media/v4l2-ctrls.h> 27 #include <media/v4l2-event.h> 28 #include <media/videobuf2-vmalloc.h> 29 #include <media/v4l2-common.h> 30 31 MODULE_DESCRIPTION("Virtual device for mem2mem framework testing"); 32 MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>"); 33 MODULE_LICENSE("GPL"); 34 MODULE_VERSION("0.2"); 35 MODULE_ALIAS("mem2mem_testdev"); 36 37 static unsigned int debug; 38 module_param(debug, uint, 0644); 39 MODULE_PARM_DESC(debug, "debug level"); 40 41 /* Default transaction time in msec */ 42 static unsigned int default_transtime = 40; /* Max 25 fps */ 43 module_param(default_transtime, uint, 0644); 44 MODULE_PARM_DESC(default_transtime, "default transaction time in ms"); 45 46 static unsigned int multiplanar = 1; 47 module_param(multiplanar, uint, 0644); 48 MODULE_PARM_DESC(multiplanar, "1 (default) creates a single planar device, 2 creates multiplanar device."); 49 50 #define MIN_W 32 51 #define MIN_H 32 52 #define MAX_W 640 53 #define MAX_H 480 54 55 /* Pixel alignment for non-bayer formats */ 56 #define WIDTH_ALIGN 2 57 #define HEIGHT_ALIGN 1 58 59 /* Pixel alignment for bayer formats */ 60 #define BAYER_WIDTH_ALIGN 2 61 #define BAYER_HEIGHT_ALIGN 2 62 63 /* Flags that indicate a format can be used for capture/output */ 64 #define MEM2MEM_CAPTURE BIT(0) 65 #define MEM2MEM_OUTPUT BIT(1) 66 67 #define MEM2MEM_NAME "vim2m" 68 69 /* Per queue */ 70 #define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME 71 /* In bytes, per queue */ 72 #define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024) 73 74 /* Flags that indicate processing mode */ 75 #define MEM2MEM_HFLIP BIT(0) 76 #define MEM2MEM_VFLIP BIT(1) 77 78 #define dprintk(dev, lvl, fmt, arg...) \ 79 v4l2_dbg(lvl, debug, &(dev)->v4l2_dev, "%s: " fmt, __func__, ## arg) 80 81 static void vim2m_dev_release(struct device *dev) 82 {} 83 84 static struct platform_device vim2m_pdev = { 85 .name = MEM2MEM_NAME, 86 .dev.release = vim2m_dev_release, 87 }; 88 89 struct vim2m_fmt { 90 u32 fourcc; 91 int depth; 92 /* Types the format can be used for */ 93 u32 types; 94 }; 95 96 static struct vim2m_fmt formats[] = { 97 { 98 .fourcc = V4L2_PIX_FMT_RGB565, /* rrrrrggg gggbbbbb */ 99 .depth = 16, 100 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT, 101 }, { 102 .fourcc = V4L2_PIX_FMT_RGB565X, /* gggbbbbb rrrrrggg */ 103 .depth = 16, 104 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT, 105 }, { 106 .fourcc = V4L2_PIX_FMT_RGB24, 107 .depth = 24, 108 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT, 109 }, { 110 .fourcc = V4L2_PIX_FMT_BGR24, 111 .depth = 24, 112 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT, 113 }, { 114 .fourcc = V4L2_PIX_FMT_YUYV, 115 .depth = 16, 116 .types = MEM2MEM_CAPTURE, 117 }, { 118 .fourcc = V4L2_PIX_FMT_SBGGR8, 119 .depth = 8, 120 .types = MEM2MEM_CAPTURE, 121 }, { 122 .fourcc = V4L2_PIX_FMT_SGBRG8, 123 .depth = 8, 124 .types = MEM2MEM_CAPTURE, 125 }, { 126 .fourcc = V4L2_PIX_FMT_SGRBG8, 127 .depth = 8, 128 .types = MEM2MEM_CAPTURE, 129 }, { 130 .fourcc = V4L2_PIX_FMT_SRGGB8, 131 .depth = 8, 132 .types = MEM2MEM_CAPTURE, 133 }, 134 }; 135 136 #define NUM_FORMATS ARRAY_SIZE(formats) 137 138 /* Per-queue, driver-specific private data */ 139 struct vim2m_q_data { 140 unsigned int width; 141 unsigned int height; 142 unsigned int num_mem_planes; 143 unsigned int sizeimage[VIDEO_MAX_PLANES]; 144 unsigned int sequence; 145 struct vim2m_fmt *fmt; 146 }; 147 148 enum { 149 V4L2_M2M_SRC = 0, 150 V4L2_M2M_DST = 1, 151 }; 152 153 #define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000) 154 #define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001) 155 156 static struct vim2m_fmt *find_format(u32 fourcc) 157 { 158 struct vim2m_fmt *fmt; 159 unsigned int k; 160 161 for (k = 0; k < NUM_FORMATS; k++) { 162 fmt = &formats[k]; 163 if (fmt->fourcc == fourcc) 164 break; 165 } 166 167 if (k == NUM_FORMATS) 168 return NULL; 169 170 return &formats[k]; 171 } 172 173 static void get_alignment(u32 fourcc, 174 unsigned int *walign, unsigned int *halign) 175 { 176 switch (fourcc) { 177 case V4L2_PIX_FMT_SBGGR8: 178 case V4L2_PIX_FMT_SGBRG8: 179 case V4L2_PIX_FMT_SGRBG8: 180 case V4L2_PIX_FMT_SRGGB8: 181 *walign = BAYER_WIDTH_ALIGN; 182 *halign = BAYER_HEIGHT_ALIGN; 183 return; 184 default: 185 *walign = WIDTH_ALIGN; 186 *halign = HEIGHT_ALIGN; 187 return; 188 } 189 } 190 191 struct vim2m_dev { 192 struct v4l2_device v4l2_dev; 193 struct video_device vfd; 194 #ifdef CONFIG_MEDIA_CONTROLLER 195 struct media_device mdev; 196 #endif 197 198 atomic_t num_inst; 199 struct mutex dev_mutex; 200 201 struct v4l2_m2m_dev *m2m_dev; 202 bool multiplanar; 203 }; 204 205 struct vim2m_ctx { 206 struct v4l2_fh fh; 207 struct vim2m_dev *dev; 208 209 struct v4l2_ctrl_handler hdl; 210 211 /* Processed buffers in this transaction */ 212 u8 num_processed; 213 214 /* Transaction length (i.e. how many buffers per transaction) */ 215 u32 translen; 216 /* Transaction time (i.e. simulated processing time) in milliseconds */ 217 u32 transtime; 218 219 struct mutex vb_mutex; 220 struct delayed_work work_run; 221 222 /* Abort requested by m2m */ 223 int aborting; 224 225 /* Processing mode */ 226 int mode; 227 228 enum v4l2_colorspace colorspace; 229 enum v4l2_ycbcr_encoding ycbcr_enc; 230 enum v4l2_xfer_func xfer_func; 231 enum v4l2_quantization quant; 232 233 /* Source and destination queue data */ 234 struct vim2m_q_data q_data[2]; 235 }; 236 237 static inline struct vim2m_ctx *file2ctx(struct file *file) 238 { 239 return container_of(file->private_data, struct vim2m_ctx, fh); 240 } 241 242 static struct vim2m_q_data *get_q_data(struct vim2m_ctx *ctx, 243 enum v4l2_buf_type type) 244 { 245 switch (type) { 246 case V4L2_BUF_TYPE_VIDEO_OUTPUT: 247 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: 248 return &ctx->q_data[V4L2_M2M_SRC]; 249 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 250 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: 251 return &ctx->q_data[V4L2_M2M_DST]; 252 default: 253 return NULL; 254 } 255 } 256 257 static const char *type_name(enum v4l2_buf_type type) 258 { 259 switch (type) { 260 case V4L2_BUF_TYPE_VIDEO_OUTPUT: 261 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: 262 return "Output"; 263 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 264 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: 265 return "Capture"; 266 default: 267 return "Invalid"; 268 } 269 } 270 271 #define CLIP(__color) \ 272 (u8)(((__color) > 0xff) ? 0xff : (((__color) < 0) ? 0 : (__color))) 273 274 static void copy_line(struct vim2m_q_data *q_data_out, 275 u8 *src, u8 *dst, bool reverse) 276 { 277 int x, depth = q_data_out->fmt->depth >> 3; 278 279 if (!reverse) { 280 memcpy(dst, src, q_data_out->width * depth); 281 } else { 282 for (x = 0; x < q_data_out->width >> 1; x++) { 283 memcpy(dst, src, depth); 284 memcpy(dst + depth, src - depth, depth); 285 src -= depth << 1; 286 dst += depth << 1; 287 } 288 return; 289 } 290 } 291 292 static void copy_two_pixels(struct vim2m_q_data *q_data_in, 293 struct vim2m_q_data *q_data_out, 294 u8 *src[2], u8 **dst, int ypos, bool reverse) 295 { 296 struct vim2m_fmt *out = q_data_out->fmt; 297 struct vim2m_fmt *in = q_data_in->fmt; 298 u8 _r[2], _g[2], _b[2], *r, *g, *b; 299 int i; 300 301 /* Step 1: read two consecutive pixels from src pointer */ 302 303 r = _r; 304 g = _g; 305 b = _b; 306 307 switch (in->fourcc) { 308 case V4L2_PIX_FMT_RGB565: /* rrrrrggg gggbbbbb */ 309 for (i = 0; i < 2; i++) { 310 u16 pix = le16_to_cpu(*(__le16 *)(src[i])); 311 312 *r++ = (u8)(((pix & 0xf800) >> 11) << 3) | 0x07; 313 *g++ = (u8)((((pix & 0x07e0) >> 5)) << 2) | 0x03; 314 *b++ = (u8)((pix & 0x1f) << 3) | 0x07; 315 } 316 break; 317 case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */ 318 for (i = 0; i < 2; i++) { 319 u16 pix = be16_to_cpu(*(__be16 *)(src[i])); 320 321 *r++ = (u8)(((pix & 0xf800) >> 11) << 3) | 0x07; 322 *g++ = (u8)((((pix & 0x07e0) >> 5)) << 2) | 0x03; 323 *b++ = (u8)((pix & 0x1f) << 3) | 0x07; 324 } 325 break; 326 default: 327 case V4L2_PIX_FMT_RGB24: 328 for (i = 0; i < 2; i++) { 329 *r++ = src[i][0]; 330 *g++ = src[i][1]; 331 *b++ = src[i][2]; 332 } 333 break; 334 case V4L2_PIX_FMT_BGR24: 335 for (i = 0; i < 2; i++) { 336 *b++ = src[i][0]; 337 *g++ = src[i][1]; 338 *r++ = src[i][2]; 339 } 340 break; 341 } 342 343 /* Step 2: store two consecutive points, reversing them if needed */ 344 345 r = _r; 346 g = _g; 347 b = _b; 348 349 switch (out->fourcc) { 350 case V4L2_PIX_FMT_RGB565: /* rrrrrggg gggbbbbb */ 351 for (i = 0; i < 2; i++) { 352 u16 pix; 353 __le16 *dst_pix = (__le16 *)*dst; 354 355 pix = ((*r << 8) & 0xf800) | ((*g << 3) & 0x07e0) | 356 (*b >> 3); 357 358 *dst_pix = cpu_to_le16(pix); 359 360 *dst += 2; 361 } 362 return; 363 case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */ 364 for (i = 0; i < 2; i++) { 365 u16 pix; 366 __be16 *dst_pix = (__be16 *)*dst; 367 368 pix = ((*r << 8) & 0xf800) | ((*g << 3) & 0x07e0) | 369 (*b >> 3); 370 371 *dst_pix = cpu_to_be16(pix); 372 373 *dst += 2; 374 } 375 return; 376 case V4L2_PIX_FMT_RGB24: 377 for (i = 0; i < 2; i++) { 378 *(*dst)++ = *r++; 379 *(*dst)++ = *g++; 380 *(*dst)++ = *b++; 381 } 382 return; 383 case V4L2_PIX_FMT_BGR24: 384 for (i = 0; i < 2; i++) { 385 *(*dst)++ = *b++; 386 *(*dst)++ = *g++; 387 *(*dst)++ = *r++; 388 } 389 return; 390 case V4L2_PIX_FMT_YUYV: 391 default: 392 { 393 u8 y, y1, u, v; 394 395 y = ((8453 * (*r) + 16594 * (*g) + 3223 * (*b) 396 + 524288) >> 15); 397 u = ((-4878 * (*r) - 9578 * (*g) + 14456 * (*b) 398 + 4210688) >> 15); 399 v = ((14456 * (*r++) - 12105 * (*g++) - 2351 * (*b++) 400 + 4210688) >> 15); 401 y1 = ((8453 * (*r) + 16594 * (*g) + 3223 * (*b) 402 + 524288) >> 15); 403 404 *(*dst)++ = y; 405 *(*dst)++ = u; 406 407 *(*dst)++ = y1; 408 *(*dst)++ = v; 409 return; 410 } 411 case V4L2_PIX_FMT_SBGGR8: 412 if (!(ypos & 1)) { 413 *(*dst)++ = *b; 414 *(*dst)++ = *++g; 415 } else { 416 *(*dst)++ = *g; 417 *(*dst)++ = *++r; 418 } 419 return; 420 case V4L2_PIX_FMT_SGBRG8: 421 if (!(ypos & 1)) { 422 *(*dst)++ = *g; 423 *(*dst)++ = *++b; 424 } else { 425 *(*dst)++ = *r; 426 *(*dst)++ = *++g; 427 } 428 return; 429 case V4L2_PIX_FMT_SGRBG8: 430 if (!(ypos & 1)) { 431 *(*dst)++ = *g; 432 *(*dst)++ = *++r; 433 } else { 434 *(*dst)++ = *b; 435 *(*dst)++ = *++g; 436 } 437 return; 438 case V4L2_PIX_FMT_SRGGB8: 439 if (!(ypos & 1)) { 440 *(*dst)++ = *r; 441 *(*dst)++ = *++g; 442 } else { 443 *(*dst)++ = *g; 444 *(*dst)++ = *++b; 445 } 446 return; 447 } 448 } 449 450 static int device_process(struct vim2m_ctx *ctx, 451 struct vb2_v4l2_buffer *in_vb, 452 struct vb2_v4l2_buffer *out_vb) 453 { 454 struct vim2m_dev *dev = ctx->dev; 455 struct vim2m_q_data *q_data_in, *q_data_out; 456 u8 *p_in, *p_line, *p_in_x[2], *p, *p_out; 457 unsigned int width, height, bytesperline, bytes_per_pixel; 458 unsigned int x, y, y_in, y_out, x_int, x_fract, x_err, x_offset; 459 int start, end, step; 460 461 q_data_in = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); 462 if (!q_data_in) 463 return 0; 464 bytesperline = (q_data_in->width * q_data_in->fmt->depth) >> 3; 465 bytes_per_pixel = q_data_in->fmt->depth >> 3; 466 467 q_data_out = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); 468 if (!q_data_out) 469 return 0; 470 471 /* As we're doing scaling, use the output dimensions here */ 472 height = q_data_out->height; 473 width = q_data_out->width; 474 475 p_in = vb2_plane_vaddr(&in_vb->vb2_buf, 0); 476 p_out = vb2_plane_vaddr(&out_vb->vb2_buf, 0); 477 if (!p_in || !p_out) { 478 v4l2_err(&dev->v4l2_dev, 479 "Acquiring kernel pointers to buffers failed\n"); 480 return -EFAULT; 481 } 482 483 out_vb->sequence = q_data_out->sequence++; 484 in_vb->sequence = q_data_in->sequence++; 485 v4l2_m2m_buf_copy_metadata(in_vb, out_vb, true); 486 487 if (ctx->mode & MEM2MEM_VFLIP) { 488 start = height - 1; 489 end = -1; 490 step = -1; 491 } else { 492 start = 0; 493 end = height; 494 step = 1; 495 } 496 y_out = 0; 497 498 /* 499 * When format and resolution are identical, 500 * we can use a faster copy logic 501 */ 502 if (q_data_in->fmt->fourcc == q_data_out->fmt->fourcc && 503 q_data_in->width == q_data_out->width && 504 q_data_in->height == q_data_out->height) { 505 for (y = start; y != end; y += step, y_out++) { 506 p = p_in + (y * bytesperline); 507 if (ctx->mode & MEM2MEM_HFLIP) 508 p += bytesperline - (q_data_in->fmt->depth >> 3); 509 510 copy_line(q_data_out, p, p_out, 511 ctx->mode & MEM2MEM_HFLIP); 512 513 p_out += bytesperline; 514 } 515 return 0; 516 } 517 518 /* Slower algorithm with format conversion, hflip, vflip and scaler */ 519 520 /* To speed scaler up, use Bresenham for X dimension */ 521 x_int = q_data_in->width / q_data_out->width; 522 x_fract = q_data_in->width % q_data_out->width; 523 524 for (y = start; y != end; y += step, y_out++) { 525 y_in = (y * q_data_in->height) / q_data_out->height; 526 x_offset = 0; 527 x_err = 0; 528 529 p_line = p_in + (y_in * bytesperline); 530 if (ctx->mode & MEM2MEM_HFLIP) 531 p_line += bytesperline - (q_data_in->fmt->depth >> 3); 532 p_in_x[0] = p_line; 533 534 for (x = 0; x < width >> 1; x++) { 535 x_offset += x_int; 536 x_err += x_fract; 537 if (x_err > width) { 538 x_offset++; 539 x_err -= width; 540 } 541 542 if (ctx->mode & MEM2MEM_HFLIP) 543 p_in_x[1] = p_line - x_offset * bytes_per_pixel; 544 else 545 p_in_x[1] = p_line + x_offset * bytes_per_pixel; 546 547 copy_two_pixels(q_data_in, q_data_out, 548 p_in_x, &p_out, y_out, 549 ctx->mode & MEM2MEM_HFLIP); 550 551 /* Calculate the next p_in_x0 */ 552 x_offset += x_int; 553 x_err += x_fract; 554 if (x_err > width) { 555 x_offset++; 556 x_err -= width; 557 } 558 559 if (ctx->mode & MEM2MEM_HFLIP) 560 p_in_x[0] = p_line - x_offset * bytes_per_pixel; 561 else 562 p_in_x[0] = p_line + x_offset * bytes_per_pixel; 563 } 564 } 565 566 return 0; 567 } 568 569 /* 570 * mem2mem callbacks 571 */ 572 573 /* 574 * job_ready() - check whether an instance is ready to be scheduled to run 575 */ 576 static int job_ready(void *priv) 577 { 578 struct vim2m_ctx *ctx = priv; 579 580 if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen 581 || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen) { 582 dprintk(ctx->dev, 1, "Not enough buffers available\n"); 583 return 0; 584 } 585 586 return 1; 587 } 588 589 static void job_abort(void *priv) 590 { 591 struct vim2m_ctx *ctx = priv; 592 593 /* Will cancel the transaction in the next interrupt handler */ 594 ctx->aborting = 1; 595 } 596 597 /* device_run() - prepares and starts the device 598 * 599 * This simulates all the immediate preparations required before starting 600 * a device. This will be called by the framework when it decides to schedule 601 * a particular instance. 602 */ 603 static void device_run(void *priv) 604 { 605 struct vim2m_ctx *ctx = priv; 606 struct vb2_v4l2_buffer *src_buf, *dst_buf; 607 608 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); 609 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); 610 611 /* Apply request controls if any */ 612 v4l2_ctrl_request_setup(src_buf->vb2_buf.req_obj.req, 613 &ctx->hdl); 614 615 device_process(ctx, src_buf, dst_buf); 616 617 /* Complete request controls if any */ 618 v4l2_ctrl_request_complete(src_buf->vb2_buf.req_obj.req, 619 &ctx->hdl); 620 621 /* Run delayed work, which simulates a hardware irq */ 622 schedule_delayed_work(&ctx->work_run, msecs_to_jiffies(ctx->transtime)); 623 } 624 625 static void device_work(struct work_struct *w) 626 { 627 struct vim2m_ctx *curr_ctx; 628 struct vim2m_dev *vim2m_dev; 629 struct vb2_v4l2_buffer *src_vb, *dst_vb; 630 631 curr_ctx = container_of(w, struct vim2m_ctx, work_run.work); 632 633 vim2m_dev = curr_ctx->dev; 634 635 src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx); 636 dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx); 637 638 curr_ctx->num_processed++; 639 640 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE); 641 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE); 642 643 if (curr_ctx->num_processed == curr_ctx->translen 644 || curr_ctx->aborting) { 645 dprintk(curr_ctx->dev, 2, "Finishing capture buffer fill\n"); 646 curr_ctx->num_processed = 0; 647 v4l2_m2m_job_finish(vim2m_dev->m2m_dev, curr_ctx->fh.m2m_ctx); 648 } else { 649 device_run(curr_ctx); 650 } 651 } 652 653 /* 654 * video ioctls 655 */ 656 static int vidioc_querycap(struct file *file, void *priv, 657 struct v4l2_capability *cap) 658 { 659 strscpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver)); 660 strscpy(cap->card, MEM2MEM_NAME, sizeof(cap->card)); 661 snprintf(cap->bus_info, sizeof(cap->bus_info), 662 "platform:%s", MEM2MEM_NAME); 663 return 0; 664 } 665 666 static int enum_fmt(struct v4l2_fmtdesc *f, u32 type) 667 { 668 int i, num; 669 struct vim2m_fmt *fmt; 670 671 num = 0; 672 673 for (i = 0; i < NUM_FORMATS; ++i) { 674 if (formats[i].types & type) { 675 /* index-th format of type type found ? */ 676 if (num == f->index) 677 break; 678 /* 679 * Correct type but haven't reached our index yet, 680 * just increment per-type index 681 */ 682 ++num; 683 } 684 } 685 686 if (i < NUM_FORMATS) { 687 /* Format found */ 688 fmt = &formats[i]; 689 f->pixelformat = fmt->fourcc; 690 return 0; 691 } 692 693 /* Format not found */ 694 return -EINVAL; 695 } 696 697 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, 698 struct v4l2_fmtdesc *f) 699 { 700 return enum_fmt(f, MEM2MEM_CAPTURE); 701 } 702 703 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv, 704 struct v4l2_fmtdesc *f) 705 { 706 return enum_fmt(f, MEM2MEM_OUTPUT); 707 } 708 709 static int vidioc_enum_framesizes(struct file *file, void *priv, 710 struct v4l2_frmsizeenum *fsize) 711 { 712 if (fsize->index != 0) 713 return -EINVAL; 714 715 if (!find_format(fsize->pixel_format)) 716 return -EINVAL; 717 718 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE; 719 fsize->stepwise.min_width = MIN_W; 720 fsize->stepwise.min_height = MIN_H; 721 fsize->stepwise.max_width = MAX_W; 722 fsize->stepwise.max_height = MAX_H; 723 724 get_alignment(fsize->pixel_format, 725 &fsize->stepwise.step_width, 726 &fsize->stepwise.step_height); 727 return 0; 728 } 729 730 static int vidioc_g_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f) 731 { 732 struct vb2_queue *vq; 733 struct vim2m_q_data *q_data; 734 int ret; 735 736 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); 737 if (!vq) 738 return -EINVAL; 739 740 q_data = get_q_data(ctx, f->type); 741 if (!q_data) 742 return -EINVAL; 743 744 ret = v4l2_fill_pixfmt(&f->fmt.pix, q_data->fmt->fourcc, 745 q_data->width, q_data->height); 746 if (ret) 747 return ret; 748 749 f->fmt.pix.field = V4L2_FIELD_NONE; 750 f->fmt.pix.colorspace = ctx->colorspace; 751 f->fmt.pix.xfer_func = ctx->xfer_func; 752 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc; 753 f->fmt.pix.quantization = ctx->quant; 754 755 return 0; 756 } 757 758 static int vidioc_g_fmt_mplane(struct vim2m_ctx *ctx, struct v4l2_format *f) 759 { 760 struct vb2_queue *vq; 761 struct vim2m_q_data *q_data; 762 int ret; 763 764 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); 765 if (!vq) 766 return -EINVAL; 767 768 q_data = get_q_data(ctx, f->type); 769 if (!q_data) 770 return -EINVAL; 771 772 ret = v4l2_fill_pixfmt_mp(&f->fmt.pix_mp, q_data->fmt->fourcc, 773 q_data->width, q_data->height); 774 if (ret) 775 return ret; 776 777 f->fmt.pix_mp.field = V4L2_FIELD_NONE; 778 f->fmt.pix_mp.colorspace = ctx->colorspace; 779 f->fmt.pix_mp.xfer_func = ctx->xfer_func; 780 f->fmt.pix_mp.ycbcr_enc = ctx->ycbcr_enc; 781 f->fmt.pix_mp.quantization = ctx->quant; 782 783 return 0; 784 } 785 786 static int vidioc_g_fmt_vid_out(struct file *file, void *priv, 787 struct v4l2_format *f) 788 { 789 struct vim2m_dev *dev = video_drvdata(file); 790 791 if (dev->multiplanar) 792 return -ENOTTY; 793 794 return vidioc_g_fmt(file2ctx(file), f); 795 } 796 797 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, 798 struct v4l2_format *f) 799 { 800 struct vim2m_dev *dev = video_drvdata(file); 801 802 if (dev->multiplanar) 803 return -ENOTTY; 804 805 return vidioc_g_fmt(file2ctx(file), f); 806 } 807 808 static int vidioc_g_fmt_vid_out_mplane(struct file *file, void *priv, 809 struct v4l2_format *f) 810 { 811 struct vim2m_dev *dev = video_drvdata(file); 812 813 if (!dev->multiplanar) 814 return -ENOTTY; 815 816 return vidioc_g_fmt_mplane(file2ctx(file), f); 817 } 818 819 static int vidioc_g_fmt_vid_cap_mplane(struct file *file, void *priv, 820 struct v4l2_format *f) 821 { 822 struct vim2m_dev *dev = video_drvdata(file); 823 824 if (!dev->multiplanar) 825 return -ENOTTY; 826 827 return vidioc_g_fmt_mplane(file2ctx(file), f); 828 } 829 830 static int vidioc_try_fmt(struct v4l2_format *f, bool is_mplane) 831 { 832 int walign, halign, ret; 833 int width = (is_mplane) ? f->fmt.pix_mp.width : f->fmt.pix.width; 834 int height = (is_mplane) ? f->fmt.pix_mp.height : f->fmt.pix.height; 835 u32 pixfmt = (is_mplane) ? f->fmt.pix_mp.pixelformat : 836 f->fmt.pix.pixelformat; 837 838 width = clamp(width, MIN_W, MAX_W); 839 height = clamp(height, MIN_H, MAX_H); 840 841 get_alignment(pixfmt, &walign, &halign); 842 width = ALIGN(width, walign); 843 height = ALIGN(height, halign); 844 845 f->fmt.pix.field = V4L2_FIELD_NONE; 846 847 if (is_mplane) { 848 ret = v4l2_fill_pixfmt_mp(&f->fmt.pix_mp, pixfmt, width, 849 height); 850 } else { 851 ret = v4l2_fill_pixfmt(&f->fmt.pix, pixfmt, width, height); 852 } 853 return ret; 854 } 855 856 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, 857 struct v4l2_format *f) 858 { 859 struct vim2m_fmt *fmt; 860 struct vim2m_ctx *ctx = file2ctx(file); 861 struct vim2m_dev *dev = video_drvdata(file); 862 863 if (dev->multiplanar) 864 return -ENOTTY; 865 866 fmt = find_format(f->fmt.pix.pixelformat); 867 if (!fmt) { 868 f->fmt.pix.pixelformat = formats[0].fourcc; 869 fmt = find_format(f->fmt.pix.pixelformat); 870 } 871 if (!(fmt->types & MEM2MEM_CAPTURE)) { 872 v4l2_err(&ctx->dev->v4l2_dev, 873 "Fourcc format (0x%08x) invalid.\n", 874 f->fmt.pix.pixelformat); 875 return -EINVAL; 876 } 877 f->fmt.pix.colorspace = ctx->colorspace; 878 f->fmt.pix.xfer_func = ctx->xfer_func; 879 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc; 880 f->fmt.pix.quantization = ctx->quant; 881 882 return vidioc_try_fmt(f, false); 883 } 884 885 static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv, 886 struct v4l2_format *f) 887 { 888 struct vim2m_fmt *fmt; 889 struct vim2m_ctx *ctx = file2ctx(file); 890 struct vim2m_dev *dev = video_drvdata(file); 891 892 if (!dev->multiplanar) 893 return -ENOTTY; 894 895 fmt = find_format(f->fmt.pix_mp.pixelformat); 896 if (!fmt) { 897 f->fmt.pix_mp.pixelformat = formats[0].fourcc; 898 fmt = find_format(f->fmt.pix_mp.pixelformat); 899 } 900 if (!(fmt->types & MEM2MEM_CAPTURE)) { 901 v4l2_err(&ctx->dev->v4l2_dev, 902 "Fourcc format (0x%08x) invalid.\n", 903 f->fmt.pix.pixelformat); 904 return -EINVAL; 905 } 906 f->fmt.pix_mp.colorspace = ctx->colorspace; 907 f->fmt.pix_mp.xfer_func = ctx->xfer_func; 908 f->fmt.pix_mp.ycbcr_enc = ctx->ycbcr_enc; 909 f->fmt.pix_mp.quantization = ctx->quant; 910 911 return vidioc_try_fmt(f, true); 912 } 913 914 static int vidioc_try_fmt_vid_out(struct file *file, void *priv, 915 struct v4l2_format *f) 916 { 917 struct vim2m_fmt *fmt; 918 struct vim2m_ctx *ctx = file2ctx(file); 919 struct vim2m_dev *dev = video_drvdata(file); 920 921 if (dev->multiplanar) 922 return -ENOTTY; 923 924 fmt = find_format(f->fmt.pix.pixelformat); 925 if (!fmt) { 926 f->fmt.pix.pixelformat = formats[0].fourcc; 927 fmt = find_format(f->fmt.pix.pixelformat); 928 } 929 if (!(fmt->types & MEM2MEM_OUTPUT)) { 930 v4l2_err(&ctx->dev->v4l2_dev, 931 "Fourcc format (0x%08x) invalid.\n", 932 f->fmt.pix.pixelformat); 933 return -EINVAL; 934 } 935 if (!f->fmt.pix.colorspace) 936 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709; 937 938 return vidioc_try_fmt(f, false); 939 } 940 941 static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv, 942 struct v4l2_format *f) 943 { 944 struct vim2m_fmt *fmt; 945 struct vim2m_ctx *ctx = file2ctx(file); 946 struct vim2m_dev *dev = video_drvdata(file); 947 948 if (!dev->multiplanar) 949 return -ENOTTY; 950 951 fmt = find_format(f->fmt.pix_mp.pixelformat); 952 if (!fmt) { 953 f->fmt.pix_mp.pixelformat = formats[0].fourcc; 954 fmt = find_format(f->fmt.pix_mp.pixelformat); 955 } 956 if (!(fmt->types & MEM2MEM_OUTPUT)) { 957 v4l2_err(&ctx->dev->v4l2_dev, 958 "Fourcc format (0x%08x) invalid.\n", 959 f->fmt.pix_mp.pixelformat); 960 return -EINVAL; 961 } 962 if (!f->fmt.pix_mp.colorspace) 963 f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_REC709; 964 965 return vidioc_try_fmt(f, true); 966 } 967 968 static int vidioc_s_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f) 969 { 970 struct vim2m_q_data *q_data; 971 struct vb2_queue *vq; 972 unsigned int i; 973 bool is_mplane = ctx->dev->multiplanar; 974 u32 pixfmt = (is_mplane) ? f->fmt.pix_mp.pixelformat : f->fmt.pix.pixelformat; 975 u32 width = (is_mplane) ? f->fmt.pix_mp.width : f->fmt.pix.width; 976 u32 height = (is_mplane) ? f->fmt.pix_mp.height : f->fmt.pix.height; 977 978 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); 979 if (!vq) 980 return -EINVAL; 981 982 q_data = get_q_data(ctx, f->type); 983 if (!q_data) 984 return -EINVAL; 985 986 if (vb2_is_busy(vq)) { 987 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__); 988 return -EBUSY; 989 } 990 991 q_data->fmt = find_format(pixfmt); 992 q_data->width = width; 993 q_data->height = height; 994 if (is_mplane) { 995 q_data->num_mem_planes = f->fmt.pix_mp.num_planes; 996 for (i = 0; i < f->fmt.pix_mp.num_planes; i++) 997 q_data->sizeimage[i] = f->fmt.pix_mp.plane_fmt[i].sizeimage; 998 } else { 999 q_data->sizeimage[0] = f->fmt.pix.sizeimage; 1000 q_data->num_mem_planes = 1; 1001 } 1002 1003 dprintk(ctx->dev, 1, 1004 "Format for type %s: %dx%d (%d bpp), fmt: %c%c%c%c\n", 1005 type_name(f->type), q_data->width, q_data->height, 1006 q_data->fmt->depth, 1007 (q_data->fmt->fourcc & 0xff), 1008 (q_data->fmt->fourcc >> 8) & 0xff, 1009 (q_data->fmt->fourcc >> 16) & 0xff, 1010 (q_data->fmt->fourcc >> 24) & 0xff); 1011 1012 return 0; 1013 } 1014 1015 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, 1016 struct v4l2_format *f) 1017 { 1018 int ret; 1019 struct vim2m_dev *dev = video_drvdata(file); 1020 1021 if (dev->multiplanar) 1022 return -ENOTTY; 1023 1024 ret = vidioc_try_fmt_vid_cap(file, priv, f); 1025 if (ret) 1026 return ret; 1027 1028 return vidioc_s_fmt(file2ctx(file), f); 1029 } 1030 1031 static int vidioc_s_fmt_vid_cap_mplane(struct file *file, void *priv, 1032 struct v4l2_format *f) 1033 { 1034 int ret; 1035 struct vim2m_dev *dev = video_drvdata(file); 1036 1037 if (!dev->multiplanar) 1038 return -ENOTTY; 1039 1040 ret = vidioc_try_fmt_vid_cap_mplane(file, priv, f); 1041 if (ret) 1042 return ret; 1043 1044 return vidioc_s_fmt(file2ctx(file), f); 1045 } 1046 1047 static int vidioc_s_fmt_vid_out(struct file *file, void *priv, 1048 struct v4l2_format *f) 1049 { 1050 struct vim2m_ctx *ctx = file2ctx(file); 1051 struct vim2m_dev *dev = video_drvdata(file); 1052 int ret; 1053 1054 if (dev->multiplanar) 1055 return -ENOTTY; 1056 1057 ret = vidioc_try_fmt_vid_out(file, priv, f); 1058 if (ret) 1059 return ret; 1060 1061 ret = vidioc_s_fmt(file2ctx(file), f); 1062 if (!ret) { 1063 ctx->colorspace = f->fmt.pix.colorspace; 1064 ctx->xfer_func = f->fmt.pix.xfer_func; 1065 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc; 1066 ctx->quant = f->fmt.pix.quantization; 1067 } 1068 return ret; 1069 } 1070 1071 static int vidioc_s_fmt_vid_out_mplane(struct file *file, void *priv, 1072 struct v4l2_format *f) 1073 { 1074 struct vim2m_ctx *ctx = file2ctx(file); 1075 struct vim2m_dev *dev = video_drvdata(file); 1076 int ret; 1077 1078 if (!dev->multiplanar) 1079 return -ENOTTY; 1080 1081 ret = vidioc_try_fmt_vid_out_mplane(file, priv, f); 1082 if (ret) 1083 return ret; 1084 1085 ret = vidioc_s_fmt(file2ctx(file), f); 1086 if (!ret) { 1087 ctx->colorspace = f->fmt.pix_mp.colorspace; 1088 ctx->xfer_func = f->fmt.pix_mp.xfer_func; 1089 ctx->ycbcr_enc = f->fmt.pix_mp.ycbcr_enc; 1090 ctx->quant = f->fmt.pix_mp.quantization; 1091 } 1092 return ret; 1093 } 1094 1095 static int vim2m_s_ctrl(struct v4l2_ctrl *ctrl) 1096 { 1097 struct vim2m_ctx *ctx = 1098 container_of(ctrl->handler, struct vim2m_ctx, hdl); 1099 1100 switch (ctrl->id) { 1101 case V4L2_CID_HFLIP: 1102 if (ctrl->val) 1103 ctx->mode |= MEM2MEM_HFLIP; 1104 else 1105 ctx->mode &= ~MEM2MEM_HFLIP; 1106 break; 1107 1108 case V4L2_CID_VFLIP: 1109 if (ctrl->val) 1110 ctx->mode |= MEM2MEM_VFLIP; 1111 else 1112 ctx->mode &= ~MEM2MEM_VFLIP; 1113 break; 1114 1115 case V4L2_CID_TRANS_TIME_MSEC: 1116 ctx->transtime = ctrl->val; 1117 if (ctx->transtime < 1) 1118 ctx->transtime = 1; 1119 break; 1120 1121 case V4L2_CID_TRANS_NUM_BUFS: 1122 ctx->translen = ctrl->val; 1123 break; 1124 1125 default: 1126 v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n"); 1127 return -EINVAL; 1128 } 1129 1130 return 0; 1131 } 1132 1133 static const struct v4l2_ctrl_ops vim2m_ctrl_ops = { 1134 .s_ctrl = vim2m_s_ctrl, 1135 }; 1136 1137 static const struct v4l2_ioctl_ops vim2m_ioctl_ops = { 1138 .vidioc_querycap = vidioc_querycap, 1139 1140 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, 1141 .vidioc_enum_framesizes = vidioc_enum_framesizes, 1142 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, 1143 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, 1144 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, 1145 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt_vid_cap_mplane, 1146 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_vid_cap_mplane, 1147 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt_vid_cap_mplane, 1148 1149 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out, 1150 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out, 1151 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out, 1152 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out, 1153 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt_vid_out_mplane, 1154 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_vid_out_mplane, 1155 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt_vid_out_mplane, 1156 1157 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, 1158 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, 1159 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, 1160 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, 1161 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, 1162 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, 1163 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, 1164 1165 .vidioc_streamon = v4l2_m2m_ioctl_streamon, 1166 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, 1167 1168 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 1169 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1170 }; 1171 1172 /* 1173 * Queue operations 1174 */ 1175 1176 static int vim2m_queue_setup(struct vb2_queue *vq, 1177 unsigned int *nbuffers, 1178 unsigned int *nplanes, 1179 unsigned int sizes[], 1180 struct device *alloc_devs[]) 1181 { 1182 struct vim2m_ctx *ctx = vb2_get_drv_priv(vq); 1183 struct vim2m_q_data *q_data; 1184 unsigned int size, p, count = *nbuffers; 1185 1186 q_data = get_q_data(ctx, vq->type); 1187 if (!q_data) 1188 return -EINVAL; 1189 1190 size = 0; 1191 for (p = 0; p < q_data->num_mem_planes; p++) 1192 size += q_data->sizeimage[p]; 1193 1194 while (size * count > MEM2MEM_VID_MEM_LIMIT) 1195 (count)--; 1196 *nbuffers = count; 1197 1198 if (*nplanes) { 1199 if (*nplanes != q_data->num_mem_planes) 1200 return -EINVAL; 1201 for (p = 0; p < q_data->num_mem_planes; p++) { 1202 if (sizes[p] < q_data->sizeimage[p]) 1203 return -EINVAL; 1204 } 1205 } else { 1206 *nplanes = q_data->num_mem_planes; 1207 for (p = 0; p < q_data->num_mem_planes; p++) 1208 sizes[p] = q_data->sizeimage[p]; 1209 } 1210 1211 dprintk(ctx->dev, 1, "%s: get %d buffer(s) of size %d each.\n", 1212 type_name(vq->type), count, size); 1213 1214 return 0; 1215 } 1216 1217 static int vim2m_buf_out_validate(struct vb2_buffer *vb) 1218 { 1219 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1220 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 1221 1222 if (vbuf->field == V4L2_FIELD_ANY) 1223 vbuf->field = V4L2_FIELD_NONE; 1224 if (vbuf->field != V4L2_FIELD_NONE) { 1225 dprintk(ctx->dev, 1, "%s field isn't supported\n", __func__); 1226 return -EINVAL; 1227 } 1228 1229 return 0; 1230 } 1231 1232 static int vim2m_buf_prepare(struct vb2_buffer *vb) 1233 { 1234 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 1235 struct vim2m_q_data *q_data; 1236 unsigned int p; 1237 1238 dprintk(ctx->dev, 2, "type: %s\n", type_name(vb->vb2_queue->type)); 1239 1240 q_data = get_q_data(ctx, vb->vb2_queue->type); 1241 if (!q_data) 1242 return -EINVAL; 1243 1244 for (p = 0; p < q_data->num_mem_planes; p++) { 1245 if (vb2_plane_size(vb, p) < q_data->sizeimage[p]) { 1246 dprintk(ctx->dev, 1, 1247 "%s data will not fit into plane (%lu < %lu)\n", 1248 __func__, vb2_plane_size(vb, p), 1249 (long)q_data->sizeimage[p]); 1250 return -EINVAL; 1251 } 1252 vb2_set_plane_payload(vb, p, q_data->sizeimage[p]); 1253 } 1254 1255 return 0; 1256 } 1257 1258 static void vim2m_buf_queue(struct vb2_buffer *vb) 1259 { 1260 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1261 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 1262 1263 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf); 1264 } 1265 1266 static int vim2m_start_streaming(struct vb2_queue *q, unsigned int count) 1267 { 1268 struct vim2m_ctx *ctx = vb2_get_drv_priv(q); 1269 struct vim2m_q_data *q_data = get_q_data(ctx, q->type); 1270 1271 if (!q_data) 1272 return -EINVAL; 1273 1274 if (V4L2_TYPE_IS_OUTPUT(q->type)) 1275 ctx->aborting = 0; 1276 1277 q_data->sequence = 0; 1278 return 0; 1279 } 1280 1281 static void vim2m_stop_streaming(struct vb2_queue *q) 1282 { 1283 struct vim2m_ctx *ctx = vb2_get_drv_priv(q); 1284 struct vb2_v4l2_buffer *vbuf; 1285 1286 cancel_delayed_work_sync(&ctx->work_run); 1287 1288 for (;;) { 1289 if (V4L2_TYPE_IS_OUTPUT(q->type)) 1290 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 1291 else 1292 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); 1293 if (!vbuf) 1294 return; 1295 v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req, 1296 &ctx->hdl); 1297 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR); 1298 } 1299 } 1300 1301 static void vim2m_buf_request_complete(struct vb2_buffer *vb) 1302 { 1303 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 1304 1305 v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl); 1306 } 1307 1308 static const struct vb2_ops vim2m_qops = { 1309 .queue_setup = vim2m_queue_setup, 1310 .buf_out_validate = vim2m_buf_out_validate, 1311 .buf_prepare = vim2m_buf_prepare, 1312 .buf_queue = vim2m_buf_queue, 1313 .start_streaming = vim2m_start_streaming, 1314 .stop_streaming = vim2m_stop_streaming, 1315 .buf_request_complete = vim2m_buf_request_complete, 1316 }; 1317 1318 static int queue_init(void *priv, struct vb2_queue *src_vq, 1319 struct vb2_queue *dst_vq) 1320 { 1321 struct vim2m_ctx *ctx = priv; 1322 int ret; 1323 1324 src_vq->type = (ctx->dev->multiplanar) ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : 1325 V4L2_BUF_TYPE_VIDEO_OUTPUT; 1326 src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; 1327 src_vq->drv_priv = ctx; 1328 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); 1329 src_vq->ops = &vim2m_qops; 1330 src_vq->mem_ops = &vb2_vmalloc_memops; 1331 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1332 src_vq->lock = &ctx->vb_mutex; 1333 src_vq->supports_requests = true; 1334 1335 ret = vb2_queue_init(src_vq); 1336 if (ret) 1337 return ret; 1338 1339 dst_vq->type = (ctx->dev->multiplanar) ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : 1340 V4L2_BUF_TYPE_VIDEO_CAPTURE; 1341 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; 1342 dst_vq->drv_priv = ctx; 1343 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); 1344 dst_vq->ops = &vim2m_qops; 1345 dst_vq->mem_ops = &vb2_vmalloc_memops; 1346 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1347 dst_vq->lock = &ctx->vb_mutex; 1348 1349 return vb2_queue_init(dst_vq); 1350 } 1351 1352 static struct v4l2_ctrl_config vim2m_ctrl_trans_time_msec = { 1353 .ops = &vim2m_ctrl_ops, 1354 .id = V4L2_CID_TRANS_TIME_MSEC, 1355 .name = "Transaction Time (msec)", 1356 .type = V4L2_CTRL_TYPE_INTEGER, 1357 .min = 1, 1358 .max = 10001, 1359 .step = 1, 1360 }; 1361 1362 static const struct v4l2_ctrl_config vim2m_ctrl_trans_num_bufs = { 1363 .ops = &vim2m_ctrl_ops, 1364 .id = V4L2_CID_TRANS_NUM_BUFS, 1365 .name = "Buffers Per Transaction", 1366 .type = V4L2_CTRL_TYPE_INTEGER, 1367 .def = 1, 1368 .min = 1, 1369 .max = MEM2MEM_DEF_NUM_BUFS, 1370 .step = 1, 1371 }; 1372 1373 /* 1374 * File operations 1375 */ 1376 static int vim2m_open(struct file *file) 1377 { 1378 struct vim2m_dev *dev = video_drvdata(file); 1379 struct vim2m_ctx *ctx = NULL; 1380 struct v4l2_ctrl_handler *hdl; 1381 int rc = 0; 1382 1383 if (mutex_lock_interruptible(&dev->dev_mutex)) 1384 return -ERESTARTSYS; 1385 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 1386 if (!ctx) { 1387 rc = -ENOMEM; 1388 goto open_unlock; 1389 } 1390 1391 v4l2_fh_init(&ctx->fh, video_devdata(file)); 1392 file->private_data = &ctx->fh; 1393 ctx->dev = dev; 1394 hdl = &ctx->hdl; 1395 v4l2_ctrl_handler_init(hdl, 4); 1396 v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0); 1397 v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0); 1398 1399 vim2m_ctrl_trans_time_msec.def = default_transtime; 1400 v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_time_msec, NULL); 1401 v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_num_bufs, NULL); 1402 if (hdl->error) { 1403 rc = hdl->error; 1404 v4l2_ctrl_handler_free(hdl); 1405 kfree(ctx); 1406 goto open_unlock; 1407 } 1408 ctx->fh.ctrl_handler = hdl; 1409 v4l2_ctrl_handler_setup(hdl); 1410 1411 ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0]; 1412 ctx->q_data[V4L2_M2M_SRC].width = 640; 1413 ctx->q_data[V4L2_M2M_SRC].height = 480; 1414 ctx->q_data[V4L2_M2M_SRC].sizeimage[0] = 1415 ctx->q_data[V4L2_M2M_SRC].width * 1416 ctx->q_data[V4L2_M2M_SRC].height * 1417 (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3); 1418 ctx->q_data[V4L2_M2M_SRC].num_mem_planes = 1; 1419 ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC]; 1420 ctx->colorspace = V4L2_COLORSPACE_REC709; 1421 1422 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init); 1423 1424 mutex_init(&ctx->vb_mutex); 1425 INIT_DELAYED_WORK(&ctx->work_run, device_work); 1426 1427 if (IS_ERR(ctx->fh.m2m_ctx)) { 1428 rc = PTR_ERR(ctx->fh.m2m_ctx); 1429 1430 v4l2_ctrl_handler_free(hdl); 1431 v4l2_fh_exit(&ctx->fh); 1432 kfree(ctx); 1433 goto open_unlock; 1434 } 1435 1436 v4l2_fh_add(&ctx->fh); 1437 atomic_inc(&dev->num_inst); 1438 1439 dprintk(dev, 1, "Created instance: %p, m2m_ctx: %p\n", 1440 ctx, ctx->fh.m2m_ctx); 1441 1442 open_unlock: 1443 mutex_unlock(&dev->dev_mutex); 1444 return rc; 1445 } 1446 1447 static int vim2m_release(struct file *file) 1448 { 1449 struct vim2m_dev *dev = video_drvdata(file); 1450 struct vim2m_ctx *ctx = file2ctx(file); 1451 1452 dprintk(dev, 1, "Releasing instance %p\n", ctx); 1453 1454 v4l2_fh_del(&ctx->fh); 1455 v4l2_fh_exit(&ctx->fh); 1456 v4l2_ctrl_handler_free(&ctx->hdl); 1457 mutex_lock(&dev->dev_mutex); 1458 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); 1459 mutex_unlock(&dev->dev_mutex); 1460 kfree(ctx); 1461 1462 atomic_dec(&dev->num_inst); 1463 1464 return 0; 1465 } 1466 1467 static void vim2m_device_release(struct video_device *vdev) 1468 { 1469 struct vim2m_dev *dev = container_of(vdev, struct vim2m_dev, vfd); 1470 1471 v4l2_device_unregister(&dev->v4l2_dev); 1472 v4l2_m2m_release(dev->m2m_dev); 1473 #ifdef CONFIG_MEDIA_CONTROLLER 1474 media_device_cleanup(&dev->mdev); 1475 #endif 1476 kfree(dev); 1477 } 1478 1479 static const struct v4l2_file_operations vim2m_fops = { 1480 .owner = THIS_MODULE, 1481 .open = vim2m_open, 1482 .release = vim2m_release, 1483 .poll = v4l2_m2m_fop_poll, 1484 .unlocked_ioctl = video_ioctl2, 1485 .mmap = v4l2_m2m_fop_mmap, 1486 }; 1487 1488 static const struct video_device vim2m_videodev = { 1489 .name = MEM2MEM_NAME, 1490 .vfl_dir = VFL_DIR_M2M, 1491 .fops = &vim2m_fops, 1492 .ioctl_ops = &vim2m_ioctl_ops, 1493 .minor = -1, 1494 .release = vim2m_device_release, 1495 .device_caps = V4L2_CAP_STREAMING, 1496 }; 1497 1498 static const struct v4l2_m2m_ops m2m_ops = { 1499 .device_run = device_run, 1500 .job_ready = job_ready, 1501 .job_abort = job_abort, 1502 }; 1503 1504 static const struct media_device_ops m2m_media_ops = { 1505 .req_validate = vb2_request_validate, 1506 .req_queue = v4l2_m2m_request_queue, 1507 }; 1508 1509 static int vim2m_probe(struct platform_device *pdev) 1510 { 1511 struct vim2m_dev *dev; 1512 struct video_device *vfd; 1513 int ret; 1514 1515 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1516 if (!dev) 1517 return -ENOMEM; 1518 1519 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); 1520 if (ret) 1521 goto error_free; 1522 1523 atomic_set(&dev->num_inst, 0); 1524 mutex_init(&dev->dev_mutex); 1525 1526 dev->multiplanar = (multiplanar == 2); 1527 1528 dev->vfd = vim2m_videodev; 1529 vfd = &dev->vfd; 1530 vfd->lock = &dev->dev_mutex; 1531 vfd->v4l2_dev = &dev->v4l2_dev; 1532 vfd->device_caps |= (dev->multiplanar) ? V4L2_CAP_VIDEO_M2M_MPLANE : 1533 V4L2_CAP_VIDEO_M2M; 1534 1535 video_set_drvdata(vfd, dev); 1536 platform_set_drvdata(pdev, dev); 1537 1538 dev->m2m_dev = v4l2_m2m_init(&m2m_ops); 1539 if (IS_ERR(dev->m2m_dev)) { 1540 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n"); 1541 ret = PTR_ERR(dev->m2m_dev); 1542 dev->m2m_dev = NULL; 1543 goto error_dev; 1544 } 1545 1546 #ifdef CONFIG_MEDIA_CONTROLLER 1547 dev->mdev.dev = &pdev->dev; 1548 strscpy(dev->mdev.model, "vim2m", sizeof(dev->mdev.model)); 1549 strscpy(dev->mdev.bus_info, "platform:vim2m", 1550 sizeof(dev->mdev.bus_info)); 1551 media_device_init(&dev->mdev); 1552 dev->mdev.ops = &m2m_media_ops; 1553 dev->v4l2_dev.mdev = &dev->mdev; 1554 #endif 1555 1556 ret = video_register_device(vfd, VFL_TYPE_VIDEO, 0); 1557 if (ret) { 1558 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n"); 1559 goto error_m2m; 1560 } 1561 1562 v4l2_info(&dev->v4l2_dev, 1563 "Device registered as /dev/video%d\n", vfd->num); 1564 1565 #ifdef CONFIG_MEDIA_CONTROLLER 1566 ret = v4l2_m2m_register_media_controller(dev->m2m_dev, vfd, 1567 MEDIA_ENT_F_PROC_VIDEO_SCALER); 1568 if (ret) { 1569 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller\n"); 1570 goto error_v4l2; 1571 } 1572 1573 ret = media_device_register(&dev->mdev); 1574 if (ret) { 1575 v4l2_err(&dev->v4l2_dev, "Failed to register mem2mem media device\n"); 1576 goto error_m2m_mc; 1577 } 1578 #endif 1579 return 0; 1580 1581 #ifdef CONFIG_MEDIA_CONTROLLER 1582 error_m2m_mc: 1583 v4l2_m2m_unregister_media_controller(dev->m2m_dev); 1584 #endif 1585 error_v4l2: 1586 video_unregister_device(&dev->vfd); 1587 /* vim2m_device_release called by video_unregister_device to release various objects */ 1588 return ret; 1589 error_m2m: 1590 v4l2_m2m_release(dev->m2m_dev); 1591 error_dev: 1592 v4l2_device_unregister(&dev->v4l2_dev); 1593 error_free: 1594 kfree(dev); 1595 1596 return ret; 1597 } 1598 1599 static void vim2m_remove(struct platform_device *pdev) 1600 { 1601 struct vim2m_dev *dev = platform_get_drvdata(pdev); 1602 1603 v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME); 1604 1605 #ifdef CONFIG_MEDIA_CONTROLLER 1606 media_device_unregister(&dev->mdev); 1607 v4l2_m2m_unregister_media_controller(dev->m2m_dev); 1608 #endif 1609 video_unregister_device(&dev->vfd); 1610 } 1611 1612 static struct platform_driver vim2m_pdrv = { 1613 .probe = vim2m_probe, 1614 .remove = vim2m_remove, 1615 .driver = { 1616 .name = MEM2MEM_NAME, 1617 }, 1618 }; 1619 1620 static void __exit vim2m_exit(void) 1621 { 1622 platform_driver_unregister(&vim2m_pdrv); 1623 platform_device_unregister(&vim2m_pdev); 1624 } 1625 1626 static int __init vim2m_init(void) 1627 { 1628 int ret; 1629 1630 ret = platform_device_register(&vim2m_pdev); 1631 if (ret) 1632 return ret; 1633 1634 ret = platform_driver_register(&vim2m_pdrv); 1635 if (ret) 1636 platform_device_unregister(&vim2m_pdev); 1637 1638 return ret; 1639 } 1640 1641 module_init(vim2m_init); 1642 module_exit(vim2m_exit); 1643