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, "debug level");
44 /* Default transaction time in msec */
45 static unsigned int default_transtime = 40; /* Max 25 fps */
46 module_param(default_transtime, uint, 0644);
47 MODULE_PARM_DESC(default_transtime, "default transaction time in ms");
53 #define DIM_ALIGN_MASK 1 /* 2-byte alignment */
55 /* Flags that indicate a format can be used for capture/output */
56 #define MEM2MEM_CAPTURE (1 << 0)
57 #define MEM2MEM_OUTPUT (1 << 1)
59 #define MEM2MEM_NAME "vim2m"
62 #define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME
63 /* In bytes, per queue */
64 #define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024)
66 /* Flags that indicate processing mode */
67 #define MEM2MEM_HFLIP (1 << 0)
68 #define MEM2MEM_VFLIP (1 << 1)
70 #define dprintk(dev, lvl, fmt, arg...) \
71 v4l2_dbg(lvl, debug, &(dev)->v4l2_dev, "%s: " fmt, __func__, ## arg)
74 static void vim2m_dev_release(struct device *dev)
77 static struct platform_device vim2m_pdev = {
79 .dev.release = vim2m_dev_release,
85 /* Types the format can be used for */
89 static struct vim2m_fmt formats[] = {
91 .fourcc = V4L2_PIX_FMT_RGB565, /* rrrrrggg gggbbbbb */
93 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
95 .fourcc = V4L2_PIX_FMT_RGB565X, /* gggbbbbb rrrrrggg */
97 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
99 .fourcc = V4L2_PIX_FMT_RGB24,
101 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
103 .fourcc = V4L2_PIX_FMT_BGR24,
105 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
107 .fourcc = V4L2_PIX_FMT_YUYV,
109 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
111 .fourcc = V4L2_PIX_FMT_SBGGR8,
113 .types = MEM2MEM_CAPTURE,
115 .fourcc = V4L2_PIX_FMT_SGBRG8,
117 .types = MEM2MEM_CAPTURE,
119 .fourcc = V4L2_PIX_FMT_SGRBG8,
121 .types = MEM2MEM_CAPTURE,
123 .fourcc = V4L2_PIX_FMT_SRGGB8,
125 .types = MEM2MEM_CAPTURE,
129 #define NUM_FORMATS ARRAY_SIZE(formats)
131 /* Per-queue, driver-specific private data */
132 struct vim2m_q_data {
135 unsigned int sizeimage;
136 unsigned int sequence;
137 struct vim2m_fmt *fmt;
145 #define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000)
146 #define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001)
148 static struct vim2m_fmt *find_format(struct v4l2_format *f)
150 struct vim2m_fmt *fmt;
153 for (k = 0; k < NUM_FORMATS; k++) {
155 if (fmt->fourcc == f->fmt.pix.pixelformat)
159 if (k == NUM_FORMATS)
166 struct v4l2_device v4l2_dev;
167 struct video_device vfd;
168 #ifdef CONFIG_MEDIA_CONTROLLER
169 struct media_device mdev;
173 struct mutex dev_mutex;
175 struct v4l2_m2m_dev *m2m_dev;
180 struct vim2m_dev *dev;
182 struct v4l2_ctrl_handler hdl;
184 /* Processed buffers in this transaction */
187 /* Transaction length (i.e. how many buffers per transaction) */
189 /* Transaction time (i.e. simulated processing time) in milliseconds */
192 struct mutex vb_mutex;
193 struct delayed_work work_run;
196 /* Abort requested by m2m */
199 /* Processing mode */
202 enum v4l2_colorspace colorspace;
203 enum v4l2_ycbcr_encoding ycbcr_enc;
204 enum v4l2_xfer_func xfer_func;
205 enum v4l2_quantization quant;
207 /* Source and destination queue data */
208 struct vim2m_q_data q_data[2];
211 static inline struct vim2m_ctx *file2ctx(struct file *file)
213 return container_of(file->private_data, struct vim2m_ctx, fh);
216 static struct vim2m_q_data *get_q_data(struct vim2m_ctx *ctx,
217 enum v4l2_buf_type type)
220 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
221 return &ctx->q_data[V4L2_M2M_SRC];
222 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
223 return &ctx->q_data[V4L2_M2M_DST];
230 static const char *type_name(enum v4l2_buf_type type)
233 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
235 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
242 #define CLIP(__color) \
243 (u8)(((__color) > 0xff) ? 0xff : (((__color) < 0) ? 0 : (__color)))
245 static void copy_two_pixels(struct vim2m_fmt *in, struct vim2m_fmt *out,
246 u8 **src, u8 **dst, int ypos, bool reverse)
248 u8 _r[2], _g[2], _b[2], *r, *g, *b;
251 // If format is the same just copy the data, respecting the width
252 if (in->fourcc == out->fourcc) {
253 int depth = out->depth >> 3;
256 if (in->fourcc == V4L2_PIX_FMT_YUYV) {
261 y1 = (*src)[0]; /* copy as second point */
263 y = (*src)[2]; /* copy as first point */
275 memcpy(*dst, *src, depth);
276 memcpy(*dst + depth, *src - depth, depth);
279 memcpy(*dst, *src, depth << 1);
286 /* Step 1: read two consecutive pixels from src pointer */
297 switch (in->fourcc) {
298 case V4L2_PIX_FMT_RGB565: /* rrrrrggg gggbbbbb */
299 for (i = 0; i < 2; i++) {
300 u16 pix = *(u16 *)*src;
302 *r++ = (u8)(((pix & 0xf800) >> 11) << 3) | 0x07;
303 *g++ = (u8)((((pix & 0x07e0) >> 5)) << 2) | 0x03;
304 *b++ = (u8)((pix & 0x1f) << 3) | 0x07;
309 case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */
310 for (i = 0; i < 2; i++) {
311 u16 pix = *(u16 *)*src;
313 *r++ = (u8)(((0x00f8 & pix) >> 3) << 3) | 0x07;
314 *g++ = (u8)(((pix & 0x7) << 2) |
315 ((pix & 0xe000) >> 5)) | 0x03;
316 *b++ = (u8)(((pix & 0x1f00) >> 8) << 3) | 0x07;
321 case V4L2_PIX_FMT_RGB24:
322 for (i = 0; i < 2; i++) {
330 case V4L2_PIX_FMT_BGR24:
331 for (i = 0; i < 2; i++) {
339 default: /* V4L2_PIX_FMT_YUYV */
341 int u, v, y, y1, u1, v1, tmp;
346 y1 = (*src)[0]; /* copy as second point */
348 y = (*src)[2]; /* copy as first point */
359 u1 = (((u - 128) << 7) + (u - 128)) >> 6;
360 tmp = (((u - 128) << 1) + (u - 128) +
361 ((v - 128) << 2) + ((v - 128) << 1)) >> 3;
362 v1 = (((v - 128) << 1) + (v - 128)) >> 1;
365 *g++ = CLIP(y - tmp);
375 /* Step 2: store two consecutive points, reversing them if needed */
381 switch (out->fourcc) {
382 case V4L2_PIX_FMT_RGB565: /* rrrrrggg gggbbbbb */
383 for (i = 0; i < 2; i++) {
384 u16 *pix = (u16 *)*dst;
386 *pix = ((*r << 8) & 0xf800) | ((*g << 3) & 0x07e0) |
392 case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */
393 for (i = 0; i < 2; i++) {
394 u16 *pix = (u16 *)*dst;
395 u8 green = *g++ >> 2;
397 *pix = ((green << 8) & 0xe000) | (green & 0x07) |
398 ((*b++ << 5) & 0x1f00) | ((*r++ & 0xf8));
403 case V4L2_PIX_FMT_RGB24:
404 for (i = 0; i < 2; i++) {
410 case V4L2_PIX_FMT_BGR24:
411 for (i = 0; i < 2; i++) {
417 case V4L2_PIX_FMT_YUYV:
422 y = ((8453 * (*r) + 16594 * (*g) + 3223 * (*b)
424 u = ((-4878 * (*r) - 9578 * (*g) + 14456 * (*b)
426 v = ((14456 * (*r++) - 12105 * (*g++) - 2351 * (*b++)
428 y1 = ((8453 * (*r) + 16594 * (*g) + 3223 * (*b)
438 case V4L2_PIX_FMT_SBGGR8:
447 case V4L2_PIX_FMT_SGBRG8:
456 case V4L2_PIX_FMT_SGRBG8:
465 case V4L2_PIX_FMT_SRGGB8:
477 static int device_process(struct vim2m_ctx *ctx,
478 struct vb2_v4l2_buffer *in_vb,
479 struct vb2_v4l2_buffer *out_vb)
481 struct vim2m_dev *dev = ctx->dev;
482 struct vim2m_q_data *q_data_in, *q_data_out;
483 u8 *p_in, *p, *p_out;
484 int width, height, bytesperline, x, y, y_out, start, end, step;
485 struct vim2m_fmt *in, *out;
487 q_data_in = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
489 width = q_data_in->width;
490 height = q_data_in->height;
491 bytesperline = (q_data_in->width * q_data_in->fmt->depth) >> 3;
493 q_data_out = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
494 out = q_data_out->fmt;
496 p_in = vb2_plane_vaddr(&in_vb->vb2_buf, 0);
497 p_out = vb2_plane_vaddr(&out_vb->vb2_buf, 0);
498 if (!p_in || !p_out) {
499 v4l2_err(&dev->v4l2_dev,
500 "Acquiring kernel pointers to buffers failed\n");
504 out_vb->sequence = get_q_data(ctx,
505 V4L2_BUF_TYPE_VIDEO_CAPTURE)->sequence++;
506 in_vb->sequence = q_data_in->sequence++;
507 v4l2_m2m_buf_copy_metadata(in_vb, out_vb, true);
509 if (ctx->mode & MEM2MEM_VFLIP) {
519 for (y = start; y != end; y += step, y_out++) {
520 p = p_in + (y * bytesperline);
521 if (ctx->mode & MEM2MEM_HFLIP)
522 p += bytesperline - (q_data_in->fmt->depth >> 3);
524 for (x = 0; x < width >> 1; x++)
525 copy_two_pixels(in, out, &p, &p_out, y_out,
526 ctx->mode & MEM2MEM_HFLIP);
537 * job_ready() - check whether an instance is ready to be scheduled to run
539 static int job_ready(void *priv)
541 struct vim2m_ctx *ctx = priv;
543 if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen
544 || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen) {
545 dprintk(ctx->dev, 1, "Not enough buffers available\n");
552 static void job_abort(void *priv)
554 struct vim2m_ctx *ctx = priv;
556 /* Will cancel the transaction in the next interrupt handler */
560 /* device_run() - prepares and starts the device
562 * This simulates all the immediate preparations required before starting
563 * a device. This will be called by the framework when it decides to schedule
564 * a particular instance.
566 static void device_run(void *priv)
568 struct vim2m_ctx *ctx = priv;
569 struct vb2_v4l2_buffer *src_buf, *dst_buf;
571 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
572 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
574 /* Apply request controls if any */
575 v4l2_ctrl_request_setup(src_buf->vb2_buf.req_obj.req,
578 device_process(ctx, src_buf, dst_buf);
580 /* Complete request controls if any */
581 v4l2_ctrl_request_complete(src_buf->vb2_buf.req_obj.req,
584 /* Run delayed work, which simulates a hardware irq */
585 schedule_delayed_work(&ctx->work_run, msecs_to_jiffies(ctx->transtime));
588 static void device_work(struct work_struct *w)
590 struct vim2m_ctx *curr_ctx;
591 struct vim2m_dev *vim2m_dev;
592 struct vb2_v4l2_buffer *src_vb, *dst_vb;
595 curr_ctx = container_of(w, struct vim2m_ctx, work_run.work);
597 if (NULL == curr_ctx) {
598 pr_err("Instance released before the end of transaction\n");
602 vim2m_dev = curr_ctx->dev;
604 src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
605 dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
607 curr_ctx->num_processed++;
609 spin_lock_irqsave(&curr_ctx->irqlock, flags);
610 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
611 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
612 spin_unlock_irqrestore(&curr_ctx->irqlock, flags);
614 if (curr_ctx->num_processed == curr_ctx->translen
615 || curr_ctx->aborting) {
616 dprintk(curr_ctx->dev, 2, "Finishing capture buffer fill\n");
617 curr_ctx->num_processed = 0;
618 v4l2_m2m_job_finish(vim2m_dev->m2m_dev, curr_ctx->fh.m2m_ctx);
620 device_run(curr_ctx);
627 static int vidioc_querycap(struct file *file, void *priv,
628 struct v4l2_capability *cap)
630 strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
631 strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
632 snprintf(cap->bus_info, sizeof(cap->bus_info),
633 "platform:%s", MEM2MEM_NAME);
637 static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
640 struct vim2m_fmt *fmt;
644 for (i = 0; i < NUM_FORMATS; ++i) {
645 if (formats[i].types & type) {
646 /* index-th format of type type found ? */
650 * Correct type but haven't reached our index yet,
651 * just increment per-type index
657 if (i < NUM_FORMATS) {
660 f->pixelformat = fmt->fourcc;
664 /* Format not found */
668 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
669 struct v4l2_fmtdesc *f)
671 return enum_fmt(f, MEM2MEM_CAPTURE);
674 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
675 struct v4l2_fmtdesc *f)
677 return enum_fmt(f, MEM2MEM_OUTPUT);
680 static int vidioc_g_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
682 struct vb2_queue *vq;
683 struct vim2m_q_data *q_data;
685 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
689 q_data = get_q_data(ctx, f->type);
691 f->fmt.pix.width = q_data->width;
692 f->fmt.pix.height = q_data->height;
693 f->fmt.pix.field = V4L2_FIELD_NONE;
694 f->fmt.pix.pixelformat = q_data->fmt->fourcc;
695 f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
696 f->fmt.pix.sizeimage = q_data->sizeimage;
697 f->fmt.pix.colorspace = ctx->colorspace;
698 f->fmt.pix.xfer_func = ctx->xfer_func;
699 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
700 f->fmt.pix.quantization = ctx->quant;
705 static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
706 struct v4l2_format *f)
708 return vidioc_g_fmt(file2ctx(file), f);
711 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
712 struct v4l2_format *f)
714 return vidioc_g_fmt(file2ctx(file), f);
717 static int vidioc_try_fmt(struct v4l2_format *f, struct vim2m_fmt *fmt)
719 /* V4L2 specification suggests the driver corrects the format struct
720 * if any of the dimensions is unsupported */
721 if (f->fmt.pix.height < MIN_H)
722 f->fmt.pix.height = MIN_H;
723 else if (f->fmt.pix.height > MAX_H)
724 f->fmt.pix.height = MAX_H;
726 if (f->fmt.pix.width < MIN_W)
727 f->fmt.pix.width = MIN_W;
728 else if (f->fmt.pix.width > MAX_W)
729 f->fmt.pix.width = MAX_W;
731 f->fmt.pix.width &= ~DIM_ALIGN_MASK;
732 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
733 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
734 f->fmt.pix.field = V4L2_FIELD_NONE;
739 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
740 struct v4l2_format *f)
742 struct vim2m_fmt *fmt;
743 struct vim2m_ctx *ctx = file2ctx(file);
745 fmt = find_format(f);
747 f->fmt.pix.pixelformat = formats[0].fourcc;
748 fmt = find_format(f);
750 if (!(fmt->types & MEM2MEM_CAPTURE)) {
751 v4l2_err(&ctx->dev->v4l2_dev,
752 "Fourcc format (0x%08x) invalid.\n",
753 f->fmt.pix.pixelformat);
756 f->fmt.pix.colorspace = ctx->colorspace;
757 f->fmt.pix.xfer_func = ctx->xfer_func;
758 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
759 f->fmt.pix.quantization = ctx->quant;
761 return vidioc_try_fmt(f, fmt);
764 static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
765 struct v4l2_format *f)
767 struct vim2m_fmt *fmt;
768 struct vim2m_ctx *ctx = file2ctx(file);
770 fmt = find_format(f);
772 f->fmt.pix.pixelformat = formats[0].fourcc;
773 fmt = find_format(f);
775 if (!(fmt->types & MEM2MEM_OUTPUT)) {
776 v4l2_err(&ctx->dev->v4l2_dev,
777 "Fourcc format (0x%08x) invalid.\n",
778 f->fmt.pix.pixelformat);
781 if (!f->fmt.pix.colorspace)
782 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
784 return vidioc_try_fmt(f, fmt);
787 static int vidioc_s_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
789 struct vim2m_q_data *q_data;
790 struct vb2_queue *vq;
792 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
796 q_data = get_q_data(ctx, f->type);
800 if (vb2_is_busy(vq)) {
801 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
805 q_data->fmt = find_format(f);
806 q_data->width = f->fmt.pix.width;
807 q_data->height = f->fmt.pix.height;
808 q_data->sizeimage = q_data->width * q_data->height
809 * q_data->fmt->depth >> 3;
812 "Format for type %s: %dx%d (%d bpp), fmt: %c%c%c%c\n",
813 type_name(f->type), q_data->width, q_data->height,
815 (q_data->fmt->fourcc & 0xff),
816 (q_data->fmt->fourcc >> 8) & 0xff,
817 (q_data->fmt->fourcc >> 16) & 0xff,
818 (q_data->fmt->fourcc >> 24) & 0xff);
823 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
824 struct v4l2_format *f)
828 ret = vidioc_try_fmt_vid_cap(file, priv, f);
832 return vidioc_s_fmt(file2ctx(file), f);
835 static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
836 struct v4l2_format *f)
838 struct vim2m_ctx *ctx = file2ctx(file);
841 ret = vidioc_try_fmt_vid_out(file, priv, f);
845 ret = vidioc_s_fmt(file2ctx(file), f);
847 ctx->colorspace = f->fmt.pix.colorspace;
848 ctx->xfer_func = f->fmt.pix.xfer_func;
849 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
850 ctx->quant = f->fmt.pix.quantization;
855 static int vim2m_s_ctrl(struct v4l2_ctrl *ctrl)
857 struct vim2m_ctx *ctx =
858 container_of(ctrl->handler, struct vim2m_ctx, hdl);
863 ctx->mode |= MEM2MEM_HFLIP;
865 ctx->mode &= ~MEM2MEM_HFLIP;
870 ctx->mode |= MEM2MEM_VFLIP;
872 ctx->mode &= ~MEM2MEM_VFLIP;
875 case V4L2_CID_TRANS_TIME_MSEC:
876 ctx->transtime = ctrl->val;
877 if (ctx->transtime < 1)
881 case V4L2_CID_TRANS_NUM_BUFS:
882 ctx->translen = ctrl->val;
886 v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
893 static const struct v4l2_ctrl_ops vim2m_ctrl_ops = {
894 .s_ctrl = vim2m_s_ctrl,
898 static const struct v4l2_ioctl_ops vim2m_ioctl_ops = {
899 .vidioc_querycap = vidioc_querycap,
901 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
902 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
903 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
904 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
906 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
907 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
908 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
909 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
911 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
912 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
913 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
914 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
915 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
916 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
917 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
919 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
920 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
922 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
923 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
931 static int vim2m_queue_setup(struct vb2_queue *vq,
932 unsigned int *nbuffers, unsigned int *nplanes,
933 unsigned int sizes[], struct device *alloc_devs[])
935 struct vim2m_ctx *ctx = vb2_get_drv_priv(vq);
936 struct vim2m_q_data *q_data;
937 unsigned int size, count = *nbuffers;
939 q_data = get_q_data(ctx, vq->type);
941 size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
943 while (size * count > MEM2MEM_VID_MEM_LIMIT)
948 return sizes[0] < size ? -EINVAL : 0;
953 dprintk(ctx->dev, 1, "%s: get %d buffer(s) of size %d each.\n",
954 type_name(vq->type), count, size);
959 static int vim2m_buf_out_validate(struct vb2_buffer *vb)
961 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
962 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
964 if (vbuf->field == V4L2_FIELD_ANY)
965 vbuf->field = V4L2_FIELD_NONE;
966 if (vbuf->field != V4L2_FIELD_NONE) {
967 dprintk(ctx->dev, 1, "%s field isn't supported\n", __func__);
974 static int vim2m_buf_prepare(struct vb2_buffer *vb)
976 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
977 struct vim2m_q_data *q_data;
979 dprintk(ctx->dev, 2, "type: %s\n", type_name(vb->vb2_queue->type));
981 q_data = get_q_data(ctx, vb->vb2_queue->type);
982 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
984 "%s data will not fit into plane (%lu < %lu)\n",
985 __func__, vb2_plane_size(vb, 0),
986 (long)q_data->sizeimage);
990 vb2_set_plane_payload(vb, 0, q_data->sizeimage);
995 static void vim2m_buf_queue(struct vb2_buffer *vb)
997 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
998 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1000 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1003 static int vim2m_start_streaming(struct vb2_queue *q, unsigned count)
1005 struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
1006 struct vim2m_q_data *q_data = get_q_data(ctx, q->type);
1008 q_data->sequence = 0;
1012 static void vim2m_stop_streaming(struct vb2_queue *q)
1014 struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
1015 struct vb2_v4l2_buffer *vbuf;
1016 unsigned long flags;
1018 cancel_delayed_work_sync(&ctx->work_run);
1021 if (V4L2_TYPE_IS_OUTPUT(q->type))
1022 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1024 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1027 v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
1029 spin_lock_irqsave(&ctx->irqlock, flags);
1030 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
1031 spin_unlock_irqrestore(&ctx->irqlock, flags);
1035 static void vim2m_buf_request_complete(struct vb2_buffer *vb)
1037 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1039 v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl);
1042 static const struct vb2_ops vim2m_qops = {
1043 .queue_setup = vim2m_queue_setup,
1044 .buf_out_validate = vim2m_buf_out_validate,
1045 .buf_prepare = vim2m_buf_prepare,
1046 .buf_queue = vim2m_buf_queue,
1047 .start_streaming = vim2m_start_streaming,
1048 .stop_streaming = vim2m_stop_streaming,
1049 .wait_prepare = vb2_ops_wait_prepare,
1050 .wait_finish = vb2_ops_wait_finish,
1051 .buf_request_complete = vim2m_buf_request_complete,
1054 static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
1056 struct vim2m_ctx *ctx = priv;
1059 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1060 src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1061 src_vq->drv_priv = ctx;
1062 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1063 src_vq->ops = &vim2m_qops;
1064 src_vq->mem_ops = &vb2_vmalloc_memops;
1065 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1066 src_vq->lock = &ctx->vb_mutex;
1067 src_vq->supports_requests = true;
1069 ret = vb2_queue_init(src_vq);
1073 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1074 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1075 dst_vq->drv_priv = ctx;
1076 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1077 dst_vq->ops = &vim2m_qops;
1078 dst_vq->mem_ops = &vb2_vmalloc_memops;
1079 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1080 dst_vq->lock = &ctx->vb_mutex;
1082 return vb2_queue_init(dst_vq);
1085 static struct v4l2_ctrl_config vim2m_ctrl_trans_time_msec = {
1086 .ops = &vim2m_ctrl_ops,
1087 .id = V4L2_CID_TRANS_TIME_MSEC,
1088 .name = "Transaction Time (msec)",
1089 .type = V4L2_CTRL_TYPE_INTEGER,
1095 static const struct v4l2_ctrl_config vim2m_ctrl_trans_num_bufs = {
1096 .ops = &vim2m_ctrl_ops,
1097 .id = V4L2_CID_TRANS_NUM_BUFS,
1098 .name = "Buffers Per Transaction",
1099 .type = V4L2_CTRL_TYPE_INTEGER,
1102 .max = MEM2MEM_DEF_NUM_BUFS,
1109 static int vim2m_open(struct file *file)
1111 struct vim2m_dev *dev = video_drvdata(file);
1112 struct vim2m_ctx *ctx = NULL;
1113 struct v4l2_ctrl_handler *hdl;
1116 if (mutex_lock_interruptible(&dev->dev_mutex))
1117 return -ERESTARTSYS;
1118 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1124 v4l2_fh_init(&ctx->fh, video_devdata(file));
1125 file->private_data = &ctx->fh;
1128 v4l2_ctrl_handler_init(hdl, 4);
1129 v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
1130 v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
1132 vim2m_ctrl_trans_time_msec.def = default_transtime;
1133 v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_time_msec, NULL);
1134 v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_num_bufs, NULL);
1137 v4l2_ctrl_handler_free(hdl);
1141 ctx->fh.ctrl_handler = hdl;
1142 v4l2_ctrl_handler_setup(hdl);
1144 ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
1145 ctx->q_data[V4L2_M2M_SRC].width = 640;
1146 ctx->q_data[V4L2_M2M_SRC].height = 480;
1147 ctx->q_data[V4L2_M2M_SRC].sizeimage =
1148 ctx->q_data[V4L2_M2M_SRC].width *
1149 ctx->q_data[V4L2_M2M_SRC].height *
1150 (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3);
1151 ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
1152 ctx->colorspace = V4L2_COLORSPACE_REC709;
1154 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
1156 mutex_init(&ctx->vb_mutex);
1157 spin_lock_init(&ctx->irqlock);
1158 INIT_DELAYED_WORK(&ctx->work_run, device_work);
1160 if (IS_ERR(ctx->fh.m2m_ctx)) {
1161 rc = PTR_ERR(ctx->fh.m2m_ctx);
1163 v4l2_ctrl_handler_free(hdl);
1164 v4l2_fh_exit(&ctx->fh);
1169 v4l2_fh_add(&ctx->fh);
1170 atomic_inc(&dev->num_inst);
1172 dprintk(dev, 1, "Created instance: %p, m2m_ctx: %p\n",
1173 ctx, ctx->fh.m2m_ctx);
1176 mutex_unlock(&dev->dev_mutex);
1180 static int vim2m_release(struct file *file)
1182 struct vim2m_dev *dev = video_drvdata(file);
1183 struct vim2m_ctx *ctx = file2ctx(file);
1185 dprintk(dev, 1, "Releasing instance %p\n", ctx);
1187 v4l2_fh_del(&ctx->fh);
1188 v4l2_fh_exit(&ctx->fh);
1189 v4l2_ctrl_handler_free(&ctx->hdl);
1190 mutex_lock(&dev->dev_mutex);
1191 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1192 mutex_unlock(&dev->dev_mutex);
1195 atomic_dec(&dev->num_inst);
1200 static const struct v4l2_file_operations vim2m_fops = {
1201 .owner = THIS_MODULE,
1203 .release = vim2m_release,
1204 .poll = v4l2_m2m_fop_poll,
1205 .unlocked_ioctl = video_ioctl2,
1206 .mmap = v4l2_m2m_fop_mmap,
1209 static const struct video_device vim2m_videodev = {
1210 .name = MEM2MEM_NAME,
1211 .vfl_dir = VFL_DIR_M2M,
1212 .fops = &vim2m_fops,
1213 .ioctl_ops = &vim2m_ioctl_ops,
1215 .release = video_device_release_empty,
1216 .device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING,
1219 static const struct v4l2_m2m_ops m2m_ops = {
1220 .device_run = device_run,
1221 .job_ready = job_ready,
1222 .job_abort = job_abort,
1225 static const struct media_device_ops m2m_media_ops = {
1226 .req_validate = vb2_request_validate,
1227 .req_queue = v4l2_m2m_request_queue,
1230 static int vim2m_probe(struct platform_device *pdev)
1232 struct vim2m_dev *dev;
1233 struct video_device *vfd;
1236 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
1240 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1244 atomic_set(&dev->num_inst, 0);
1245 mutex_init(&dev->dev_mutex);
1247 dev->vfd = vim2m_videodev;
1249 vfd->lock = &dev->dev_mutex;
1250 vfd->v4l2_dev = &dev->v4l2_dev;
1252 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1254 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1258 video_set_drvdata(vfd, dev);
1259 v4l2_info(&dev->v4l2_dev,
1260 "Device registered as /dev/video%d\n", vfd->num);
1262 platform_set_drvdata(pdev, dev);
1264 dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
1265 if (IS_ERR(dev->m2m_dev)) {
1266 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1267 ret = PTR_ERR(dev->m2m_dev);
1271 #ifdef CONFIG_MEDIA_CONTROLLER
1272 dev->mdev.dev = &pdev->dev;
1273 strscpy(dev->mdev.model, "vim2m", sizeof(dev->mdev.model));
1274 strscpy(dev->mdev.bus_info, "platform:vim2m",
1275 sizeof(dev->mdev.bus_info));
1276 media_device_init(&dev->mdev);
1277 dev->mdev.ops = &m2m_media_ops;
1278 dev->v4l2_dev.mdev = &dev->mdev;
1280 ret = v4l2_m2m_register_media_controller(dev->m2m_dev,
1281 vfd, MEDIA_ENT_F_PROC_VIDEO_SCALER);
1283 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller\n");
1287 ret = media_device_register(&dev->mdev);
1289 v4l2_err(&dev->v4l2_dev, "Failed to register mem2mem media device\n");
1295 #ifdef CONFIG_MEDIA_CONTROLLER
1297 v4l2_m2m_unregister_media_controller(dev->m2m_dev);
1299 v4l2_m2m_release(dev->m2m_dev);
1302 video_unregister_device(&dev->vfd);
1304 v4l2_device_unregister(&dev->v4l2_dev);
1309 static int vim2m_remove(struct platform_device *pdev)
1311 struct vim2m_dev *dev = platform_get_drvdata(pdev);
1313 v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME);
1315 #ifdef CONFIG_MEDIA_CONTROLLER
1316 media_device_unregister(&dev->mdev);
1317 v4l2_m2m_unregister_media_controller(dev->m2m_dev);
1318 media_device_cleanup(&dev->mdev);
1320 v4l2_m2m_release(dev->m2m_dev);
1321 video_unregister_device(&dev->vfd);
1322 v4l2_device_unregister(&dev->v4l2_dev);
1327 static struct platform_driver vim2m_pdrv = {
1328 .probe = vim2m_probe,
1329 .remove = vim2m_remove,
1331 .name = MEM2MEM_NAME,
1335 static void __exit vim2m_exit(void)
1337 platform_driver_unregister(&vim2m_pdrv);
1338 platform_device_unregister(&vim2m_pdev);
1341 static int __init vim2m_init(void)
1345 ret = platform_device_register(&vim2m_pdev);
1349 ret = platform_driver_register(&vim2m_pdrv);
1351 platform_device_unregister(&vim2m_pdev);
1356 module_init(vim2m_init);
1357 module_exit(vim2m_exit);