2 * A virtual v4l2-mem2mem example device.
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
8 * The device is capable of multi-instance, multi-buffer-per-transaction
9 * operation (via the mem2mem framework).
11 * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
12 * Pawel Osciak, <pawel@osciak.com>
13 * Marek Szyprowski, <m.szyprowski@samsung.com>
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
20 #include <linux/module.h>
21 #include <linux/delay.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
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>
34 MODULE_DESCRIPTION("Virtual device for mem2mem framework testing");
35 MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
36 MODULE_LICENSE("GPL");
37 MODULE_VERSION("0.1.1");
38 MODULE_ALIAS("mem2mem_testdev");
40 static unsigned debug;
41 module_param(debug, uint, 0644);
42 MODULE_PARM_DESC(debug, "activates debug info");
48 #define DIM_ALIGN_MASK 7 /* 8-byte alignment for line length */
50 /* Flags that indicate a format can be used for capture/output */
51 #define MEM2MEM_CAPTURE (1 << 0)
52 #define MEM2MEM_OUTPUT (1 << 1)
54 #define MEM2MEM_NAME "vim2m"
57 #define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME
58 /* In bytes, per queue */
59 #define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024)
61 /* Default transaction time in msec */
62 #define MEM2MEM_DEF_TRANSTIME 40
64 /* Flags that indicate processing mode */
65 #define MEM2MEM_HFLIP (1 << 0)
66 #define MEM2MEM_VFLIP (1 << 1)
68 #define dprintk(dev, fmt, arg...) \
69 v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
72 static void vim2m_dev_release(struct device *dev)
75 static struct platform_device vim2m_pdev = {
77 .dev.release = vim2m_dev_release,
85 static struct vim2m_fmt formats[] = {
87 .fourcc = V4L2_PIX_FMT_RGB565, /* rrrrrggg gggbbbbb */
90 .fourcc = V4L2_PIX_FMT_RGB565X, /* gggbbbbb rrrrrggg */
93 .fourcc = V4L2_PIX_FMT_RGB24,
96 .fourcc = V4L2_PIX_FMT_BGR24,
99 .fourcc = V4L2_PIX_FMT_YUYV,
104 #define NUM_FORMATS ARRAY_SIZE(formats)
106 /* Per-queue, driver-specific private data */
107 struct vim2m_q_data {
110 unsigned int sizeimage;
111 unsigned int sequence;
112 struct vim2m_fmt *fmt;
120 #define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000)
121 #define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001)
123 static struct vim2m_fmt *find_format(struct v4l2_format *f)
125 struct vim2m_fmt *fmt;
128 for (k = 0; k < NUM_FORMATS; k++) {
130 if (fmt->fourcc == f->fmt.pix.pixelformat)
134 if (k == NUM_FORMATS)
141 struct v4l2_device v4l2_dev;
142 struct video_device vfd;
143 #ifdef CONFIG_MEDIA_CONTROLLER
144 struct media_device mdev;
148 struct mutex dev_mutex;
151 struct delayed_work work_run;
153 struct v4l2_m2m_dev *m2m_dev;
158 struct vim2m_dev *dev;
160 struct v4l2_ctrl_handler hdl;
162 /* Processed buffers in this transaction */
165 /* Transaction length (i.e. how many buffers per transaction) */
167 /* Transaction time (i.e. simulated processing time) in milliseconds */
170 /* Abort requested by m2m */
173 /* Processing mode */
176 enum v4l2_colorspace colorspace;
177 enum v4l2_ycbcr_encoding ycbcr_enc;
178 enum v4l2_xfer_func xfer_func;
179 enum v4l2_quantization quant;
181 /* Source and destination queue data */
182 struct vim2m_q_data q_data[2];
185 static inline struct vim2m_ctx *file2ctx(struct file *file)
187 return container_of(file->private_data, struct vim2m_ctx, fh);
190 static struct vim2m_q_data *get_q_data(struct vim2m_ctx *ctx,
191 enum v4l2_buf_type 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];
204 #define CLIP(__color) \
205 (u8)(((__color) > 0xff) ? 0xff : (((__color) < 0) ? 0 : (__color)))
207 static void copy_two_pixels(struct vim2m_fmt *in, struct vim2m_fmt *out,
208 u8 **src, u8 **dst, bool reverse)
210 u8 _r[2], _g[2], _b[2], *r, *g, *b;
213 // If format is the same just copy the data, respecting the width
214 if (in->fourcc == out->fourcc) {
215 int depth = out->depth >> 3;
218 if (in->fourcc == V4L2_PIX_FMT_YUYV) {
223 y1 = (*src)[0]; /* copy as second point */
225 y = (*src)[2]; /* copy as first point */
237 memcpy(*dst, *src, depth);
238 memcpy(*dst + depth, *src - depth, depth);
241 memcpy(*dst, *src, depth << 1);
248 /* Step 1: read two consecutive pixels from src pointer */
259 switch (in->fourcc) {
260 case V4L2_PIX_FMT_RGB565: /* rrrrrggg gggbbbbb */
261 for (i = 0; i < 2; i++) {
262 u16 pix = *(u16 *)*src;
264 *r++ = (u8)(((pix & 0xf800) >> 11) << 3) | 0x07;
265 *g++ = (u8)((((pix & 0x07e0) >> 5)) << 2) | 0x03;
266 *b++ = (u8)((pix & 0x1f) << 3) | 0x07;
271 case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */
272 for (i = 0; i < 2; i++) {
273 u16 pix = *(u16 *)*src;
275 *r++ = (u8)(((0x00f8 & pix) >> 3) << 3) | 0x07;
276 *g++ = (u8)(((pix & 0x7) << 2) |
277 ((pix & 0xe000) >> 5)) | 0x03;
278 *b++ = (u8)(((pix & 0x1f00) >> 8) << 3) | 0x07;
283 case V4L2_PIX_FMT_RGB24:
284 for (i = 0; i < 2; i++) {
292 case V4L2_PIX_FMT_BGR24:
293 for (i = 0; i < 2; i++) {
301 default: /* V4L2_PIX_FMT_YUYV */
303 int u, v, y, y1, u1, v1, tmp;
308 y1 = (*src)[0]; /* copy as second point */
310 y = (*src)[2]; /* copy as first point */
321 u1 = (((u - 128) << 7) + (u - 128)) >> 6;
322 tmp = (((u - 128) << 1) + (u - 128) +
323 ((v - 128) << 2) + ((v - 128) << 1)) >> 3;
324 v1 = (((v - 128) << 1) + (v - 128)) >> 1;
327 *g++ = CLIP(y - tmp);
337 /* Step 2: store two consecutive points, reversing them if needed */
343 switch (out->fourcc) {
344 case V4L2_PIX_FMT_RGB565: /* rrrrrggg gggbbbbb */
345 for (i = 0; i < 2; i++) {
346 u16 *pix = (u16 *)*dst;
348 *pix = ((*r << 8) & 0xf800) | ((*g << 3) & 0x07e0) |
354 case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */
355 for (i = 0; i < 2; i++) {
356 u16 *pix = (u16 *)*dst;
357 u8 green = *g++ >> 2;
359 *pix = ((green << 8) & 0xe000) | (green & 0x07) |
360 ((*b++ << 5) & 0x1f00) | ((*r++ & 0xf8));
365 case V4L2_PIX_FMT_RGB24:
366 for (i = 0; i < 2; i++) {
372 case V4L2_PIX_FMT_BGR24:
373 for (i = 0; i < 2; i++) {
379 default: /* V4L2_PIX_FMT_YUYV */
383 y = ((8453 * (*r) + 16594 * (*g) + 3223 * (*b)
385 u = ((-4878 * (*r) - 9578 * (*g) + 14456 * (*b)
387 v = ((14456 * (*r++) - 12105 * (*g++) - 2351 * (*b++)
389 y1 = ((8453 * (*r) + 16594 * (*g) + 3223 * (*b)
402 static int device_process(struct vim2m_ctx *ctx,
403 struct vb2_v4l2_buffer *in_vb,
404 struct vb2_v4l2_buffer *out_vb)
406 struct vim2m_dev *dev = ctx->dev;
407 struct vim2m_q_data *q_data_in, *q_data_out;
408 u8 *p_in, *p, *p_out;
409 int width, height, bytesperline, x, y, start, end, step;
410 struct vim2m_fmt *in, *out;
412 q_data_in = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
414 width = q_data_in->width;
415 height = q_data_in->height;
416 bytesperline = (q_data_in->width * q_data_in->fmt->depth) >> 3;
418 q_data_out = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
419 out = q_data_out->fmt;
421 p_in = vb2_plane_vaddr(&in_vb->vb2_buf, 0);
422 p_out = vb2_plane_vaddr(&out_vb->vb2_buf, 0);
423 if (!p_in || !p_out) {
424 v4l2_err(&dev->v4l2_dev,
425 "Acquiring kernel pointers to buffers failed\n");
429 out_vb->sequence = get_q_data(ctx,
430 V4L2_BUF_TYPE_VIDEO_CAPTURE)->sequence++;
431 in_vb->sequence = q_data_in->sequence++;
432 v4l2_m2m_buf_copy_data(in_vb, out_vb, true);
434 if (ctx->mode & MEM2MEM_VFLIP) {
443 for (y = start; y != end; y += step) {
444 p = p_in + (y * bytesperline);
445 if (ctx->mode & MEM2MEM_HFLIP)
446 p += bytesperline - (q_data_in->fmt->depth >> 3);
448 for (x = 0; x < width >> 1; x++)
449 copy_two_pixels(in, out, &p, &p_out,
450 ctx->mode & MEM2MEM_HFLIP);
461 * job_ready() - check whether an instance is ready to be scheduled to run
463 static int job_ready(void *priv)
465 struct vim2m_ctx *ctx = priv;
467 if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen
468 || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen) {
469 dprintk(ctx->dev, "Not enough buffers available\n");
476 static void job_abort(void *priv)
478 struct vim2m_ctx *ctx = priv;
480 /* Will cancel the transaction in the next interrupt handler */
484 /* device_run() - prepares and starts the device
486 * This simulates all the immediate preparations required before starting
487 * a device. This will be called by the framework when it decides to schedule
488 * a particular instance.
490 static void device_run(void *priv)
492 struct vim2m_ctx *ctx = priv;
493 struct vim2m_dev *dev = ctx->dev;
494 struct vb2_v4l2_buffer *src_buf, *dst_buf;
496 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
497 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
499 /* Apply request controls if any */
500 v4l2_ctrl_request_setup(src_buf->vb2_buf.req_obj.req,
503 device_process(ctx, src_buf, dst_buf);
505 /* Complete request controls if any */
506 v4l2_ctrl_request_complete(src_buf->vb2_buf.req_obj.req,
509 /* Run delayed work, which simulates a hardware irq */
510 schedule_delayed_work(&dev->work_run, msecs_to_jiffies(ctx->transtime));
513 static void device_work(struct work_struct *w)
515 struct vim2m_dev *vim2m_dev =
516 container_of(w, struct vim2m_dev, work_run.work);
517 struct vim2m_ctx *curr_ctx;
518 struct vb2_v4l2_buffer *src_vb, *dst_vb;
521 curr_ctx = v4l2_m2m_get_curr_priv(vim2m_dev->m2m_dev);
523 if (NULL == curr_ctx) {
524 pr_err("Instance released before the end of transaction\n");
528 src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
529 dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
531 curr_ctx->num_processed++;
533 spin_lock_irqsave(&vim2m_dev->irqlock, flags);
534 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
535 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
536 spin_unlock_irqrestore(&vim2m_dev->irqlock, flags);
538 if (curr_ctx->num_processed == curr_ctx->translen
539 || curr_ctx->aborting) {
540 dprintk(curr_ctx->dev, "Finishing transaction\n");
541 curr_ctx->num_processed = 0;
542 v4l2_m2m_job_finish(vim2m_dev->m2m_dev, curr_ctx->fh.m2m_ctx);
544 device_run(curr_ctx);
551 static int vidioc_querycap(struct file *file, void *priv,
552 struct v4l2_capability *cap)
554 strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
555 strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
556 snprintf(cap->bus_info, sizeof(cap->bus_info),
557 "platform:%s", MEM2MEM_NAME);
561 static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
563 struct vim2m_fmt *fmt;
565 if (f->index < NUM_FORMATS) {
567 fmt = &formats[f->index];
568 f->pixelformat = fmt->fourcc;
572 /* Format not found */
576 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
577 struct v4l2_fmtdesc *f)
579 return enum_fmt(f, MEM2MEM_CAPTURE);
582 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
583 struct v4l2_fmtdesc *f)
585 return enum_fmt(f, MEM2MEM_OUTPUT);
588 static int vidioc_g_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
590 struct vb2_queue *vq;
591 struct vim2m_q_data *q_data;
593 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
597 q_data = get_q_data(ctx, f->type);
599 f->fmt.pix.width = q_data->width;
600 f->fmt.pix.height = q_data->height;
601 f->fmt.pix.field = V4L2_FIELD_NONE;
602 f->fmt.pix.pixelformat = q_data->fmt->fourcc;
603 f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
604 f->fmt.pix.sizeimage = q_data->sizeimage;
605 f->fmt.pix.colorspace = ctx->colorspace;
606 f->fmt.pix.xfer_func = ctx->xfer_func;
607 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
608 f->fmt.pix.quantization = ctx->quant;
613 static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
614 struct v4l2_format *f)
616 return vidioc_g_fmt(file2ctx(file), f);
619 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
620 struct v4l2_format *f)
622 return vidioc_g_fmt(file2ctx(file), f);
625 static int vidioc_try_fmt(struct v4l2_format *f, struct vim2m_fmt *fmt)
627 /* V4L2 specification suggests the driver corrects the format struct
628 * if any of the dimensions is unsupported */
629 if (f->fmt.pix.height < MIN_H)
630 f->fmt.pix.height = MIN_H;
631 else if (f->fmt.pix.height > MAX_H)
632 f->fmt.pix.height = MAX_H;
634 if (f->fmt.pix.width < MIN_W)
635 f->fmt.pix.width = MIN_W;
636 else if (f->fmt.pix.width > MAX_W)
637 f->fmt.pix.width = MAX_W;
639 f->fmt.pix.width &= ~DIM_ALIGN_MASK;
640 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
641 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
642 f->fmt.pix.field = V4L2_FIELD_NONE;
647 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
648 struct v4l2_format *f)
650 struct vim2m_fmt *fmt;
651 struct vim2m_ctx *ctx = file2ctx(file);
653 fmt = find_format(f);
655 f->fmt.pix.pixelformat = formats[0].fourcc;
656 fmt = find_format(f);
658 f->fmt.pix.colorspace = ctx->colorspace;
659 f->fmt.pix.xfer_func = ctx->xfer_func;
660 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
661 f->fmt.pix.quantization = ctx->quant;
663 return vidioc_try_fmt(f, fmt);
666 static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
667 struct v4l2_format *f)
669 struct vim2m_fmt *fmt;
671 fmt = find_format(f);
673 f->fmt.pix.pixelformat = formats[0].fourcc;
674 fmt = find_format(f);
676 if (!f->fmt.pix.colorspace)
677 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
679 return vidioc_try_fmt(f, fmt);
682 static int vidioc_s_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
684 struct vim2m_q_data *q_data;
685 struct vb2_queue *vq;
687 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
691 q_data = get_q_data(ctx, f->type);
695 if (vb2_is_busy(vq)) {
696 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
700 q_data->fmt = find_format(f);
701 q_data->width = f->fmt.pix.width;
702 q_data->height = f->fmt.pix.height;
703 q_data->sizeimage = q_data->width * q_data->height
704 * q_data->fmt->depth >> 3;
707 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
708 f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
713 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
714 struct v4l2_format *f)
718 ret = vidioc_try_fmt_vid_cap(file, priv, f);
722 return vidioc_s_fmt(file2ctx(file), f);
725 static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
726 struct v4l2_format *f)
728 struct vim2m_ctx *ctx = file2ctx(file);
731 ret = vidioc_try_fmt_vid_out(file, priv, f);
735 ret = vidioc_s_fmt(file2ctx(file), f);
737 ctx->colorspace = f->fmt.pix.colorspace;
738 ctx->xfer_func = f->fmt.pix.xfer_func;
739 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
740 ctx->quant = f->fmt.pix.quantization;
745 static int vim2m_s_ctrl(struct v4l2_ctrl *ctrl)
747 struct vim2m_ctx *ctx =
748 container_of(ctrl->handler, struct vim2m_ctx, hdl);
753 ctx->mode |= MEM2MEM_HFLIP;
755 ctx->mode &= ~MEM2MEM_HFLIP;
760 ctx->mode |= MEM2MEM_VFLIP;
762 ctx->mode &= ~MEM2MEM_VFLIP;
765 case V4L2_CID_TRANS_TIME_MSEC:
766 ctx->transtime = ctrl->val;
769 case V4L2_CID_TRANS_NUM_BUFS:
770 ctx->translen = ctrl->val;
774 v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
781 static const struct v4l2_ctrl_ops vim2m_ctrl_ops = {
782 .s_ctrl = vim2m_s_ctrl,
786 static const struct v4l2_ioctl_ops vim2m_ioctl_ops = {
787 .vidioc_querycap = vidioc_querycap,
789 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
790 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
791 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
792 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
794 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
795 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
796 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
797 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
799 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
800 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
801 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
802 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
803 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
804 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
805 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
807 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
808 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
810 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
811 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
819 static int vim2m_queue_setup(struct vb2_queue *vq,
820 unsigned int *nbuffers, unsigned int *nplanes,
821 unsigned int sizes[], struct device *alloc_devs[])
823 struct vim2m_ctx *ctx = vb2_get_drv_priv(vq);
824 struct vim2m_q_data *q_data;
825 unsigned int size, count = *nbuffers;
827 q_data = get_q_data(ctx, vq->type);
829 size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
831 while (size * count > MEM2MEM_VID_MEM_LIMIT)
836 return sizes[0] < size ? -EINVAL : 0;
841 dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
846 static int vim2m_buf_out_validate(struct vb2_buffer *vb)
848 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
849 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
851 if (vbuf->field == V4L2_FIELD_ANY)
852 vbuf->field = V4L2_FIELD_NONE;
853 if (vbuf->field != V4L2_FIELD_NONE) {
854 dprintk(ctx->dev, "%s field isn't supported\n", __func__);
861 static int vim2m_buf_prepare(struct vb2_buffer *vb)
863 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
864 struct vim2m_q_data *q_data;
866 dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
868 q_data = get_q_data(ctx, vb->vb2_queue->type);
869 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
870 dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n",
871 __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage);
875 vb2_set_plane_payload(vb, 0, q_data->sizeimage);
880 static void vim2m_buf_queue(struct vb2_buffer *vb)
882 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
883 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
885 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
888 static int vim2m_start_streaming(struct vb2_queue *q, unsigned count)
890 struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
891 struct vim2m_q_data *q_data = get_q_data(ctx, q->type);
893 q_data->sequence = 0;
897 static void vim2m_stop_streaming(struct vb2_queue *q)
899 struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
900 struct vim2m_dev *dev = ctx->dev;
901 struct vb2_v4l2_buffer *vbuf;
904 cancel_delayed_work_sync(&dev->work_run);
906 if (V4L2_TYPE_IS_OUTPUT(q->type))
907 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
909 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
912 v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
914 spin_lock_irqsave(&ctx->dev->irqlock, flags);
915 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
916 spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
920 static void vim2m_buf_request_complete(struct vb2_buffer *vb)
922 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
924 v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl);
927 static const struct vb2_ops vim2m_qops = {
928 .queue_setup = vim2m_queue_setup,
929 .buf_out_validate = vim2m_buf_out_validate,
930 .buf_prepare = vim2m_buf_prepare,
931 .buf_queue = vim2m_buf_queue,
932 .start_streaming = vim2m_start_streaming,
933 .stop_streaming = vim2m_stop_streaming,
934 .wait_prepare = vb2_ops_wait_prepare,
935 .wait_finish = vb2_ops_wait_finish,
936 .buf_request_complete = vim2m_buf_request_complete,
939 static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
941 struct vim2m_ctx *ctx = priv;
944 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
945 src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
946 src_vq->drv_priv = ctx;
947 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
948 src_vq->ops = &vim2m_qops;
949 src_vq->mem_ops = &vb2_vmalloc_memops;
950 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
951 src_vq->lock = &ctx->dev->dev_mutex;
952 src_vq->supports_requests = true;
954 ret = vb2_queue_init(src_vq);
958 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
959 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
960 dst_vq->drv_priv = ctx;
961 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
962 dst_vq->ops = &vim2m_qops;
963 dst_vq->mem_ops = &vb2_vmalloc_memops;
964 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
965 dst_vq->lock = &ctx->dev->dev_mutex;
967 return vb2_queue_init(dst_vq);
970 static const struct v4l2_ctrl_config vim2m_ctrl_trans_time_msec = {
971 .ops = &vim2m_ctrl_ops,
972 .id = V4L2_CID_TRANS_TIME_MSEC,
973 .name = "Transaction Time (msec)",
974 .type = V4L2_CTRL_TYPE_INTEGER,
975 .def = MEM2MEM_DEF_TRANSTIME,
981 static const struct v4l2_ctrl_config vim2m_ctrl_trans_num_bufs = {
982 .ops = &vim2m_ctrl_ops,
983 .id = V4L2_CID_TRANS_NUM_BUFS,
984 .name = "Buffers Per Transaction",
985 .type = V4L2_CTRL_TYPE_INTEGER,
988 .max = MEM2MEM_DEF_NUM_BUFS,
995 static int vim2m_open(struct file *file)
997 struct vim2m_dev *dev = video_drvdata(file);
998 struct vim2m_ctx *ctx = NULL;
999 struct v4l2_ctrl_handler *hdl;
1002 if (mutex_lock_interruptible(&dev->dev_mutex))
1003 return -ERESTARTSYS;
1004 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1010 v4l2_fh_init(&ctx->fh, video_devdata(file));
1011 file->private_data = &ctx->fh;
1014 v4l2_ctrl_handler_init(hdl, 4);
1015 v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
1016 v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
1017 v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_time_msec, NULL);
1018 v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_num_bufs, NULL);
1021 v4l2_ctrl_handler_free(hdl);
1025 ctx->fh.ctrl_handler = hdl;
1026 v4l2_ctrl_handler_setup(hdl);
1028 ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
1029 ctx->q_data[V4L2_M2M_SRC].width = 640;
1030 ctx->q_data[V4L2_M2M_SRC].height = 480;
1031 ctx->q_data[V4L2_M2M_SRC].sizeimage =
1032 ctx->q_data[V4L2_M2M_SRC].width *
1033 ctx->q_data[V4L2_M2M_SRC].height *
1034 (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3);
1035 ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
1036 ctx->colorspace = V4L2_COLORSPACE_REC709;
1038 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
1040 if (IS_ERR(ctx->fh.m2m_ctx)) {
1041 rc = PTR_ERR(ctx->fh.m2m_ctx);
1043 v4l2_ctrl_handler_free(hdl);
1044 v4l2_fh_exit(&ctx->fh);
1049 v4l2_fh_add(&ctx->fh);
1050 atomic_inc(&dev->num_inst);
1052 dprintk(dev, "Created instance: %p, m2m_ctx: %p\n",
1053 ctx, ctx->fh.m2m_ctx);
1056 mutex_unlock(&dev->dev_mutex);
1060 static int vim2m_release(struct file *file)
1062 struct vim2m_dev *dev = video_drvdata(file);
1063 struct vim2m_ctx *ctx = file2ctx(file);
1065 dprintk(dev, "Releasing instance %p\n", ctx);
1067 v4l2_fh_del(&ctx->fh);
1068 v4l2_fh_exit(&ctx->fh);
1069 v4l2_ctrl_handler_free(&ctx->hdl);
1070 mutex_lock(&dev->dev_mutex);
1071 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1072 mutex_unlock(&dev->dev_mutex);
1075 atomic_dec(&dev->num_inst);
1080 static const struct v4l2_file_operations vim2m_fops = {
1081 .owner = THIS_MODULE,
1083 .release = vim2m_release,
1084 .poll = v4l2_m2m_fop_poll,
1085 .unlocked_ioctl = video_ioctl2,
1086 .mmap = v4l2_m2m_fop_mmap,
1089 static const struct video_device vim2m_videodev = {
1090 .name = MEM2MEM_NAME,
1091 .vfl_dir = VFL_DIR_M2M,
1092 .fops = &vim2m_fops,
1093 .ioctl_ops = &vim2m_ioctl_ops,
1095 .release = video_device_release_empty,
1096 .device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING,
1099 static const struct v4l2_m2m_ops m2m_ops = {
1100 .device_run = device_run,
1101 .job_ready = job_ready,
1102 .job_abort = job_abort,
1105 static const struct media_device_ops m2m_media_ops = {
1106 .req_validate = vb2_request_validate,
1107 .req_queue = v4l2_m2m_request_queue,
1110 static int vim2m_probe(struct platform_device *pdev)
1112 struct vim2m_dev *dev;
1113 struct video_device *vfd;
1116 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
1120 spin_lock_init(&dev->irqlock);
1122 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1126 atomic_set(&dev->num_inst, 0);
1127 mutex_init(&dev->dev_mutex);
1129 dev->vfd = vim2m_videodev;
1131 vfd->lock = &dev->dev_mutex;
1132 vfd->v4l2_dev = &dev->v4l2_dev;
1133 INIT_DELAYED_WORK(&dev->work_run, device_work);
1135 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1137 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1141 video_set_drvdata(vfd, dev);
1142 v4l2_info(&dev->v4l2_dev,
1143 "Device registered as /dev/video%d\n", vfd->num);
1145 platform_set_drvdata(pdev, dev);
1147 dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
1148 if (IS_ERR(dev->m2m_dev)) {
1149 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1150 ret = PTR_ERR(dev->m2m_dev);
1154 #ifdef CONFIG_MEDIA_CONTROLLER
1155 dev->mdev.dev = &pdev->dev;
1156 strscpy(dev->mdev.model, "vim2m", sizeof(dev->mdev.model));
1157 media_device_init(&dev->mdev);
1158 dev->mdev.ops = &m2m_media_ops;
1159 dev->v4l2_dev.mdev = &dev->mdev;
1161 ret = v4l2_m2m_register_media_controller(dev->m2m_dev,
1162 vfd, MEDIA_ENT_F_PROC_VIDEO_SCALER);
1164 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller\n");
1168 ret = media_device_register(&dev->mdev);
1170 v4l2_err(&dev->v4l2_dev, "Failed to register mem2mem media device\n");
1176 #ifdef CONFIG_MEDIA_CONTROLLER
1178 v4l2_m2m_unregister_media_controller(dev->m2m_dev);
1180 v4l2_m2m_release(dev->m2m_dev);
1183 video_unregister_device(&dev->vfd);
1185 v4l2_device_unregister(&dev->v4l2_dev);
1190 static int vim2m_remove(struct platform_device *pdev)
1192 struct vim2m_dev *dev = platform_get_drvdata(pdev);
1194 v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME);
1196 #ifdef CONFIG_MEDIA_CONTROLLER
1197 media_device_unregister(&dev->mdev);
1198 v4l2_m2m_unregister_media_controller(dev->m2m_dev);
1199 media_device_cleanup(&dev->mdev);
1201 v4l2_m2m_release(dev->m2m_dev);
1202 video_unregister_device(&dev->vfd);
1203 v4l2_device_unregister(&dev->v4l2_dev);
1208 static struct platform_driver vim2m_pdrv = {
1209 .probe = vim2m_probe,
1210 .remove = vim2m_remove,
1212 .name = MEM2MEM_NAME,
1216 static void __exit vim2m_exit(void)
1218 platform_driver_unregister(&vim2m_pdrv);
1219 platform_device_unregister(&vim2m_pdev);
1222 static int __init vim2m_init(void)
1226 ret = platform_device_register(&vim2m_pdev);
1230 ret = platform_driver_register(&vim2m_pdrv);
1232 platform_device_unregister(&vim2m_pdev);
1237 module_init(vim2m_init);
1238 module_exit(vim2m_exit);