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;
150 struct v4l2_m2m_dev *m2m_dev;
155 struct vim2m_dev *dev;
157 struct v4l2_ctrl_handler hdl;
159 /* Processed buffers in this transaction */
162 /* Transaction length (i.e. how many buffers per transaction) */
164 /* Transaction time (i.e. simulated processing time) in milliseconds */
167 struct mutex vb_mutex;
168 struct delayed_work work_run;
171 /* Abort requested by m2m */
174 /* Processing mode */
177 enum v4l2_colorspace colorspace;
178 enum v4l2_ycbcr_encoding ycbcr_enc;
179 enum v4l2_xfer_func xfer_func;
180 enum v4l2_quantization quant;
182 /* Source and destination queue data */
183 struct vim2m_q_data q_data[2];
186 static inline struct vim2m_ctx *file2ctx(struct file *file)
188 return container_of(file->private_data, struct vim2m_ctx, fh);
191 static struct vim2m_q_data *get_q_data(struct vim2m_ctx *ctx,
192 enum v4l2_buf_type type)
195 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
196 return &ctx->q_data[V4L2_M2M_SRC];
197 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
198 return &ctx->q_data[V4L2_M2M_DST];
205 #define CLIP(__color) \
206 (u8)(((__color) > 0xff) ? 0xff : (((__color) < 0) ? 0 : (__color)))
208 static void copy_two_pixels(struct vim2m_fmt *in, struct vim2m_fmt *out,
209 u8 **src, u8 **dst, bool reverse)
211 u8 _r[2], _g[2], _b[2], *r, *g, *b;
214 // If format is the same just copy the data, respecting the width
215 if (in->fourcc == out->fourcc) {
216 int depth = out->depth >> 3;
219 if (in->fourcc == V4L2_PIX_FMT_YUYV) {
224 y1 = (*src)[0]; /* copy as second point */
226 y = (*src)[2]; /* copy as first point */
238 memcpy(*dst, *src, depth);
239 memcpy(*dst + depth, *src - depth, depth);
242 memcpy(*dst, *src, depth << 1);
249 /* Step 1: read two consecutive pixels from src pointer */
260 switch (in->fourcc) {
261 case V4L2_PIX_FMT_RGB565: /* rrrrrggg gggbbbbb */
262 for (i = 0; i < 2; i++) {
263 u16 pix = *(u16 *)*src;
265 *r++ = (u8)(((pix & 0xf800) >> 11) << 3) | 0x07;
266 *g++ = (u8)((((pix & 0x07e0) >> 5)) << 2) | 0x03;
267 *b++ = (u8)((pix & 0x1f) << 3) | 0x07;
272 case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */
273 for (i = 0; i < 2; i++) {
274 u16 pix = *(u16 *)*src;
276 *r++ = (u8)(((0x00f8 & pix) >> 3) << 3) | 0x07;
277 *g++ = (u8)(((pix & 0x7) << 2) |
278 ((pix & 0xe000) >> 5)) | 0x03;
279 *b++ = (u8)(((pix & 0x1f00) >> 8) << 3) | 0x07;
284 case V4L2_PIX_FMT_RGB24:
285 for (i = 0; i < 2; i++) {
293 case V4L2_PIX_FMT_BGR24:
294 for (i = 0; i < 2; i++) {
302 default: /* V4L2_PIX_FMT_YUYV */
304 int u, v, y, y1, u1, v1, tmp;
309 y1 = (*src)[0]; /* copy as second point */
311 y = (*src)[2]; /* copy as first point */
322 u1 = (((u - 128) << 7) + (u - 128)) >> 6;
323 tmp = (((u - 128) << 1) + (u - 128) +
324 ((v - 128) << 2) + ((v - 128) << 1)) >> 3;
325 v1 = (((v - 128) << 1) + (v - 128)) >> 1;
328 *g++ = CLIP(y - tmp);
338 /* Step 2: store two consecutive points, reversing them if needed */
344 switch (out->fourcc) {
345 case V4L2_PIX_FMT_RGB565: /* rrrrrggg gggbbbbb */
346 for (i = 0; i < 2; i++) {
347 u16 *pix = (u16 *)*dst;
349 *pix = ((*r << 8) & 0xf800) | ((*g << 3) & 0x07e0) |
355 case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */
356 for (i = 0; i < 2; i++) {
357 u16 *pix = (u16 *)*dst;
358 u8 green = *g++ >> 2;
360 *pix = ((green << 8) & 0xe000) | (green & 0x07) |
361 ((*b++ << 5) & 0x1f00) | ((*r++ & 0xf8));
366 case V4L2_PIX_FMT_RGB24:
367 for (i = 0; i < 2; i++) {
373 case V4L2_PIX_FMT_BGR24:
374 for (i = 0; i < 2; i++) {
380 default: /* V4L2_PIX_FMT_YUYV */
384 y = ((8453 * (*r) + 16594 * (*g) + 3223 * (*b)
386 u = ((-4878 * (*r) - 9578 * (*g) + 14456 * (*b)
388 v = ((14456 * (*r++) - 12105 * (*g++) - 2351 * (*b++)
390 y1 = ((8453 * (*r) + 16594 * (*g) + 3223 * (*b)
403 static int device_process(struct vim2m_ctx *ctx,
404 struct vb2_v4l2_buffer *in_vb,
405 struct vb2_v4l2_buffer *out_vb)
407 struct vim2m_dev *dev = ctx->dev;
408 struct vim2m_q_data *q_data_in, *q_data_out;
409 u8 *p_in, *p, *p_out;
410 int width, height, bytesperline, x, y, start, end, step;
411 struct vim2m_fmt *in, *out;
413 q_data_in = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
415 width = q_data_in->width;
416 height = q_data_in->height;
417 bytesperline = (q_data_in->width * q_data_in->fmt->depth) >> 3;
419 q_data_out = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
420 out = q_data_out->fmt;
422 p_in = vb2_plane_vaddr(&in_vb->vb2_buf, 0);
423 p_out = vb2_plane_vaddr(&out_vb->vb2_buf, 0);
424 if (!p_in || !p_out) {
425 v4l2_err(&dev->v4l2_dev,
426 "Acquiring kernel pointers to buffers failed\n");
430 out_vb->sequence = get_q_data(ctx,
431 V4L2_BUF_TYPE_VIDEO_CAPTURE)->sequence++;
432 in_vb->sequence = q_data_in->sequence++;
433 v4l2_m2m_buf_copy_data(in_vb, out_vb, true);
435 if (ctx->mode & MEM2MEM_VFLIP) {
444 for (y = start; y != end; y += step) {
445 p = p_in + (y * bytesperline);
446 if (ctx->mode & MEM2MEM_HFLIP)
447 p += bytesperline - (q_data_in->fmt->depth >> 3);
449 for (x = 0; x < width >> 1; x++)
450 copy_two_pixels(in, out, &p, &p_out,
451 ctx->mode & MEM2MEM_HFLIP);
462 * job_ready() - check whether an instance is ready to be scheduled to run
464 static int job_ready(void *priv)
466 struct vim2m_ctx *ctx = priv;
468 if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen
469 || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen) {
470 dprintk(ctx->dev, "Not enough buffers available\n");
477 static void job_abort(void *priv)
479 struct vim2m_ctx *ctx = priv;
481 /* Will cancel the transaction in the next interrupt handler */
485 /* device_run() - prepares and starts the device
487 * This simulates all the immediate preparations required before starting
488 * a device. This will be called by the framework when it decides to schedule
489 * a particular instance.
491 static void device_run(void *priv)
493 struct vim2m_ctx *ctx = priv;
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(&ctx->work_run, msecs_to_jiffies(ctx->transtime));
513 static void device_work(struct work_struct *w)
515 struct vim2m_ctx *curr_ctx;
516 struct vim2m_dev *vim2m_dev;
517 struct vb2_v4l2_buffer *src_vb, *dst_vb;
520 curr_ctx = container_of(w, struct vim2m_ctx, work_run.work);
521 vim2m_dev = curr_ctx->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(&curr_ctx->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(&curr_ctx->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 vb2_v4l2_buffer *vbuf;
903 cancel_delayed_work_sync(&ctx->work_run);
905 if (V4L2_TYPE_IS_OUTPUT(q->type))
906 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
908 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
911 v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
913 spin_lock_irqsave(&ctx->irqlock, flags);
914 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
915 spin_unlock_irqrestore(&ctx->irqlock, flags);
919 static void vim2m_buf_request_complete(struct vb2_buffer *vb)
921 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
923 v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl);
926 static const struct vb2_ops vim2m_qops = {
927 .queue_setup = vim2m_queue_setup,
928 .buf_out_validate = vim2m_buf_out_validate,
929 .buf_prepare = vim2m_buf_prepare,
930 .buf_queue = vim2m_buf_queue,
931 .start_streaming = vim2m_start_streaming,
932 .stop_streaming = vim2m_stop_streaming,
933 .wait_prepare = vb2_ops_wait_prepare,
934 .wait_finish = vb2_ops_wait_finish,
935 .buf_request_complete = vim2m_buf_request_complete,
938 static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
940 struct vim2m_ctx *ctx = priv;
943 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
944 src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
945 src_vq->drv_priv = ctx;
946 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
947 src_vq->ops = &vim2m_qops;
948 src_vq->mem_ops = &vb2_vmalloc_memops;
949 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
950 src_vq->lock = &ctx->vb_mutex;
951 src_vq->supports_requests = true;
953 ret = vb2_queue_init(src_vq);
957 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
958 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
959 dst_vq->drv_priv = ctx;
960 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
961 dst_vq->ops = &vim2m_qops;
962 dst_vq->mem_ops = &vb2_vmalloc_memops;
963 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
964 dst_vq->lock = &ctx->vb_mutex;
966 return vb2_queue_init(dst_vq);
969 static const struct v4l2_ctrl_config vim2m_ctrl_trans_time_msec = {
970 .ops = &vim2m_ctrl_ops,
971 .id = V4L2_CID_TRANS_TIME_MSEC,
972 .name = "Transaction Time (msec)",
973 .type = V4L2_CTRL_TYPE_INTEGER,
974 .def = MEM2MEM_DEF_TRANSTIME,
980 static const struct v4l2_ctrl_config vim2m_ctrl_trans_num_bufs = {
981 .ops = &vim2m_ctrl_ops,
982 .id = V4L2_CID_TRANS_NUM_BUFS,
983 .name = "Buffers Per Transaction",
984 .type = V4L2_CTRL_TYPE_INTEGER,
987 .max = MEM2MEM_DEF_NUM_BUFS,
994 static int vim2m_open(struct file *file)
996 struct vim2m_dev *dev = video_drvdata(file);
997 struct vim2m_ctx *ctx = NULL;
998 struct v4l2_ctrl_handler *hdl;
1001 if (mutex_lock_interruptible(&dev->dev_mutex))
1002 return -ERESTARTSYS;
1003 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1009 v4l2_fh_init(&ctx->fh, video_devdata(file));
1010 file->private_data = &ctx->fh;
1013 v4l2_ctrl_handler_init(hdl, 4);
1014 v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
1015 v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
1016 v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_time_msec, NULL);
1017 v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_num_bufs, NULL);
1020 v4l2_ctrl_handler_free(hdl);
1024 ctx->fh.ctrl_handler = hdl;
1025 v4l2_ctrl_handler_setup(hdl);
1027 ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
1028 ctx->q_data[V4L2_M2M_SRC].width = 640;
1029 ctx->q_data[V4L2_M2M_SRC].height = 480;
1030 ctx->q_data[V4L2_M2M_SRC].sizeimage =
1031 ctx->q_data[V4L2_M2M_SRC].width *
1032 ctx->q_data[V4L2_M2M_SRC].height *
1033 (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3);
1034 ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
1035 ctx->colorspace = V4L2_COLORSPACE_REC709;
1037 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
1039 mutex_init(&ctx->vb_mutex);
1040 spin_lock_init(&ctx->irqlock);
1041 INIT_DELAYED_WORK(&ctx->work_run, device_work);
1043 if (IS_ERR(ctx->fh.m2m_ctx)) {
1044 rc = PTR_ERR(ctx->fh.m2m_ctx);
1046 v4l2_ctrl_handler_free(hdl);
1047 v4l2_fh_exit(&ctx->fh);
1052 v4l2_fh_add(&ctx->fh);
1053 atomic_inc(&dev->num_inst);
1055 dprintk(dev, "Created instance: %p, m2m_ctx: %p\n",
1056 ctx, ctx->fh.m2m_ctx);
1059 mutex_unlock(&dev->dev_mutex);
1063 static int vim2m_release(struct file *file)
1065 struct vim2m_dev *dev = video_drvdata(file);
1066 struct vim2m_ctx *ctx = file2ctx(file);
1068 dprintk(dev, "Releasing instance %p\n", ctx);
1070 v4l2_fh_del(&ctx->fh);
1071 v4l2_fh_exit(&ctx->fh);
1072 v4l2_ctrl_handler_free(&ctx->hdl);
1073 mutex_lock(&dev->dev_mutex);
1074 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1075 mutex_unlock(&dev->dev_mutex);
1078 atomic_dec(&dev->num_inst);
1083 static const struct v4l2_file_operations vim2m_fops = {
1084 .owner = THIS_MODULE,
1086 .release = vim2m_release,
1087 .poll = v4l2_m2m_fop_poll,
1088 .unlocked_ioctl = video_ioctl2,
1089 .mmap = v4l2_m2m_fop_mmap,
1092 static const struct video_device vim2m_videodev = {
1093 .name = MEM2MEM_NAME,
1094 .vfl_dir = VFL_DIR_M2M,
1095 .fops = &vim2m_fops,
1096 .ioctl_ops = &vim2m_ioctl_ops,
1098 .release = video_device_release_empty,
1099 .device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING,
1102 static const struct v4l2_m2m_ops m2m_ops = {
1103 .device_run = device_run,
1104 .job_ready = job_ready,
1105 .job_abort = job_abort,
1108 static const struct media_device_ops m2m_media_ops = {
1109 .req_validate = vb2_request_validate,
1110 .req_queue = v4l2_m2m_request_queue,
1113 static int vim2m_probe(struct platform_device *pdev)
1115 struct vim2m_dev *dev;
1116 struct video_device *vfd;
1119 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
1123 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1127 atomic_set(&dev->num_inst, 0);
1128 mutex_init(&dev->dev_mutex);
1130 dev->vfd = vim2m_videodev;
1132 vfd->lock = &dev->dev_mutex;
1133 vfd->v4l2_dev = &dev->v4l2_dev;
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);