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 timer).
7 * The device is capable of multi-instance, multi-buffer-per-transaction
8 * operation (via the mem2mem framework).
10 * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
11 * Pawel Osciak, <pawel@osciak.com>
12 * Marek Szyprowski, <m.szyprowski@samsung.com>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version
19 #include <linux/module.h>
20 #include <linux/delay.h>
22 #include <linux/timer.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 #define MEM2MEM_TEST_MODULE_NAME "mem2mem-testdev"
36 MODULE_DESCRIPTION("Virtual device for mem2mem framework testing");
37 MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
38 MODULE_LICENSE("GPL");
39 MODULE_VERSION("0.1.1");
45 #define DIM_ALIGN_MASK 7 /* 8-byte alignment for line length */
47 /* Flags that indicate a format can be used for capture/output */
48 #define MEM2MEM_CAPTURE (1 << 0)
49 #define MEM2MEM_OUTPUT (1 << 1)
51 #define MEM2MEM_NAME "m2m-testdev"
54 #define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME
55 /* In bytes, per queue */
56 #define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024)
58 /* Default transaction time in msec */
59 #define MEM2MEM_DEF_TRANSTIME 1000
60 /* Default number of buffers per transaction */
61 #define MEM2MEM_DEF_TRANSLEN 1
62 #define MEM2MEM_COLOR_STEP (0xff >> 4)
63 #define MEM2MEM_NUM_TILES 8
65 /* Flags that indicate processing mode */
66 #define MEM2MEM_HFLIP (1 << 0)
67 #define MEM2MEM_VFLIP (1 << 1)
69 #define dprintk(dev, fmt, arg...) \
70 v4l2_dbg(1, 1, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
73 void m2mtest_dev_release(struct device *dev)
76 static struct platform_device m2mtest_pdev = {
78 .dev.release = m2mtest_dev_release,
85 /* Types the format can be used for */
89 static struct m2mtest_fmt formats[] = {
91 .name = "RGB565 (BE)",
92 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
94 /* Both capture and output format */
95 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
98 .name = "4:2:2, packed, YUYV",
99 .fourcc = V4L2_PIX_FMT_YUYV,
101 /* Output-only format */
102 .types = MEM2MEM_OUTPUT,
106 #define NUM_FORMATS ARRAY_SIZE(formats)
108 /* Per-queue, driver-specific private data */
109 struct m2mtest_q_data {
112 unsigned int sizeimage;
113 struct m2mtest_fmt *fmt;
121 #define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000)
122 #define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001)
124 static struct m2mtest_fmt *find_format(struct v4l2_format *f)
126 struct m2mtest_fmt *fmt;
129 for (k = 0; k < NUM_FORMATS; k++) {
131 if (fmt->fourcc == f->fmt.pix.pixelformat)
135 if (k == NUM_FORMATS)
142 struct v4l2_device v4l2_dev;
143 struct video_device *vfd;
146 struct mutex dev_mutex;
149 struct timer_list timer;
151 struct v4l2_m2m_dev *m2m_dev;
156 struct m2mtest_dev *dev;
158 struct v4l2_ctrl_handler hdl;
160 /* Processed buffers in this transaction */
163 /* Transaction length (i.e. how many buffers per transaction) */
165 /* Transaction time (i.e. simulated processing time) in milliseconds */
168 /* Abort requested by m2m */
171 /* Processing mode */
174 enum v4l2_colorspace colorspace;
176 struct v4l2_m2m_ctx *m2m_ctx;
178 /* Source and destination queue data */
179 struct m2mtest_q_data q_data[2];
182 static inline struct m2mtest_ctx *file2ctx(struct file *file)
184 return container_of(file->private_data, struct m2mtest_ctx, fh);
187 static struct m2mtest_q_data *get_q_data(struct m2mtest_ctx *ctx,
188 enum v4l2_buf_type type)
191 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
192 return &ctx->q_data[V4L2_M2M_SRC];
193 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
194 return &ctx->q_data[V4L2_M2M_DST];
202 static int device_process(struct m2mtest_ctx *ctx,
203 struct vb2_buffer *in_vb,
204 struct vb2_buffer *out_vb)
206 struct m2mtest_dev *dev = ctx->dev;
207 struct m2mtest_q_data *q_data;
210 int tile_w, bytes_left;
211 int width, height, bytesperline;
213 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
215 width = q_data->width;
216 height = q_data->height;
217 bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
219 p_in = vb2_plane_vaddr(in_vb, 0);
220 p_out = vb2_plane_vaddr(out_vb, 0);
221 if (!p_in || !p_out) {
222 v4l2_err(&dev->v4l2_dev,
223 "Acquiring kernel pointers to buffers failed\n");
227 if (vb2_plane_size(in_vb, 0) > vb2_plane_size(out_vb, 0)) {
228 v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n");
232 tile_w = (width * (q_data[V4L2_M2M_DST].fmt->depth >> 3))
234 bytes_left = bytesperline - tile_w * MEM2MEM_NUM_TILES;
238 case MEM2MEM_HFLIP | MEM2MEM_VFLIP:
239 p_out += bytesperline * height - bytes_left;
240 for (y = 0; y < height; ++y) {
241 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
243 for (x = 0; x < tile_w; ++x)
247 for (x = 0; x < tile_w; ++x)
259 for (y = 0; y < height; ++y) {
260 p_out += MEM2MEM_NUM_TILES * tile_w;
261 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
263 for (x = 0; x < tile_w; ++x)
267 for (x = 0; x < tile_w; ++x)
274 p_out += bytesperline;
279 p_out += bytesperline * (height - 1);
280 for (y = 0; y < height; ++y) {
281 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
283 for (x = 0; x < tile_w; ++x)
287 for (x = 0; x < tile_w; ++x)
294 p_out += bytes_left - 2 * bytesperline;
299 for (y = 0; y < height; ++y) {
300 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
302 for (x = 0; x < tile_w; ++x)
306 for (x = 0; x < tile_w; ++x)
320 static void schedule_irq(struct m2mtest_dev *dev, int msec_timeout)
322 dprintk(dev, "Scheduling a simulated irq\n");
323 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(msec_timeout));
331 * job_ready() - check whether an instance is ready to be scheduled to run
333 static int job_ready(void *priv)
335 struct m2mtest_ctx *ctx = priv;
337 if (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) < ctx->translen
338 || v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx) < ctx->translen) {
339 dprintk(ctx->dev, "Not enough buffers available\n");
346 static void job_abort(void *priv)
348 struct m2mtest_ctx *ctx = priv;
350 /* Will cancel the transaction in the next interrupt handler */
354 static void m2mtest_lock(void *priv)
356 struct m2mtest_ctx *ctx = priv;
357 struct m2mtest_dev *dev = ctx->dev;
358 mutex_lock(&dev->dev_mutex);
361 static void m2mtest_unlock(void *priv)
363 struct m2mtest_ctx *ctx = priv;
364 struct m2mtest_dev *dev = ctx->dev;
365 mutex_unlock(&dev->dev_mutex);
369 /* device_run() - prepares and starts the device
371 * This simulates all the immediate preparations required before starting
372 * a device. This will be called by the framework when it decides to schedule
373 * a particular instance.
375 static void device_run(void *priv)
377 struct m2mtest_ctx *ctx = priv;
378 struct m2mtest_dev *dev = ctx->dev;
379 struct vb2_buffer *src_buf, *dst_buf;
381 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
382 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
384 device_process(ctx, src_buf, dst_buf);
386 /* Run a timer, which simulates a hardware irq */
387 schedule_irq(dev, ctx->transtime);
390 static void device_isr(unsigned long priv)
392 struct m2mtest_dev *m2mtest_dev = (struct m2mtest_dev *)priv;
393 struct m2mtest_ctx *curr_ctx;
394 struct vb2_buffer *src_vb, *dst_vb;
397 curr_ctx = v4l2_m2m_get_curr_priv(m2mtest_dev->m2m_dev);
399 if (NULL == curr_ctx) {
401 "Instance released before the end of transaction\n");
405 src_vb = v4l2_m2m_src_buf_remove(curr_ctx->m2m_ctx);
406 dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->m2m_ctx);
408 curr_ctx->num_processed++;
410 spin_lock_irqsave(&m2mtest_dev->irqlock, flags);
411 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
412 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
413 spin_unlock_irqrestore(&m2mtest_dev->irqlock, flags);
415 if (curr_ctx->num_processed == curr_ctx->translen
416 || curr_ctx->aborting) {
417 dprintk(curr_ctx->dev, "Finishing transaction\n");
418 curr_ctx->num_processed = 0;
419 v4l2_m2m_job_finish(m2mtest_dev->m2m_dev, curr_ctx->m2m_ctx);
421 device_run(curr_ctx);
428 static int vidioc_querycap(struct file *file, void *priv,
429 struct v4l2_capability *cap)
431 strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
432 strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
433 strlcpy(cap->bus_info, MEM2MEM_NAME, sizeof(cap->bus_info));
434 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT
435 | V4L2_CAP_STREAMING;
436 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
440 static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
443 struct m2mtest_fmt *fmt;
447 for (i = 0; i < NUM_FORMATS; ++i) {
448 if (formats[i].types & type) {
449 /* index-th format of type type found ? */
452 /* Correct type but haven't reached our index yet,
453 * just increment per-type index */
458 if (i < NUM_FORMATS) {
461 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
462 f->pixelformat = fmt->fourcc;
466 /* Format not found */
470 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
471 struct v4l2_fmtdesc *f)
473 return enum_fmt(f, MEM2MEM_CAPTURE);
476 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
477 struct v4l2_fmtdesc *f)
479 return enum_fmt(f, MEM2MEM_OUTPUT);
482 static int vidioc_g_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
484 struct vb2_queue *vq;
485 struct m2mtest_q_data *q_data;
487 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
491 q_data = get_q_data(ctx, f->type);
493 f->fmt.pix.width = q_data->width;
494 f->fmt.pix.height = q_data->height;
495 f->fmt.pix.field = V4L2_FIELD_NONE;
496 f->fmt.pix.pixelformat = q_data->fmt->fourcc;
497 f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
498 f->fmt.pix.sizeimage = q_data->sizeimage;
499 f->fmt.pix.colorspace = ctx->colorspace;
504 static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
505 struct v4l2_format *f)
507 return vidioc_g_fmt(file2ctx(file), f);
510 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
511 struct v4l2_format *f)
513 return vidioc_g_fmt(file2ctx(file), f);
516 static int vidioc_try_fmt(struct v4l2_format *f, struct m2mtest_fmt *fmt)
518 enum v4l2_field field;
520 field = f->fmt.pix.field;
522 if (field == V4L2_FIELD_ANY)
523 field = V4L2_FIELD_NONE;
524 else if (V4L2_FIELD_NONE != field)
527 /* V4L2 specification suggests the driver corrects the format struct
528 * if any of the dimensions is unsupported */
529 f->fmt.pix.field = field;
531 if (f->fmt.pix.height < MIN_H)
532 f->fmt.pix.height = MIN_H;
533 else if (f->fmt.pix.height > MAX_H)
534 f->fmt.pix.height = MAX_H;
536 if (f->fmt.pix.width < MIN_W)
537 f->fmt.pix.width = MIN_W;
538 else if (f->fmt.pix.width > MAX_W)
539 f->fmt.pix.width = MAX_W;
541 f->fmt.pix.width &= ~DIM_ALIGN_MASK;
542 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
543 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
548 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
549 struct v4l2_format *f)
551 struct m2mtest_fmt *fmt;
552 struct m2mtest_ctx *ctx = file2ctx(file);
554 fmt = find_format(f);
555 if (!fmt || !(fmt->types & MEM2MEM_CAPTURE)) {
556 v4l2_err(&ctx->dev->v4l2_dev,
557 "Fourcc format (0x%08x) invalid.\n",
558 f->fmt.pix.pixelformat);
561 f->fmt.pix.colorspace = ctx->colorspace;
563 return vidioc_try_fmt(f, fmt);
566 static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
567 struct v4l2_format *f)
569 struct m2mtest_fmt *fmt;
570 struct m2mtest_ctx *ctx = file2ctx(file);
572 fmt = find_format(f);
573 if (!fmt || !(fmt->types & MEM2MEM_OUTPUT)) {
574 v4l2_err(&ctx->dev->v4l2_dev,
575 "Fourcc format (0x%08x) invalid.\n",
576 f->fmt.pix.pixelformat);
579 if (!f->fmt.pix.colorspace)
580 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
582 return vidioc_try_fmt(f, fmt);
585 static int vidioc_s_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
587 struct m2mtest_q_data *q_data;
588 struct vb2_queue *vq;
590 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
594 q_data = get_q_data(ctx, f->type);
598 if (vb2_is_busy(vq)) {
599 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
603 q_data->fmt = find_format(f);
604 q_data->width = f->fmt.pix.width;
605 q_data->height = f->fmt.pix.height;
606 q_data->sizeimage = q_data->width * q_data->height
607 * q_data->fmt->depth >> 3;
610 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
611 f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
616 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
617 struct v4l2_format *f)
621 ret = vidioc_try_fmt_vid_cap(file, priv, f);
625 return vidioc_s_fmt(file2ctx(file), f);
628 static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
629 struct v4l2_format *f)
631 struct m2mtest_ctx *ctx = file2ctx(file);
634 ret = vidioc_try_fmt_vid_out(file, priv, f);
638 ret = vidioc_s_fmt(file2ctx(file), f);
640 ctx->colorspace = f->fmt.pix.colorspace;
644 static int vidioc_reqbufs(struct file *file, void *priv,
645 struct v4l2_requestbuffers *reqbufs)
647 struct m2mtest_ctx *ctx = file2ctx(file);
649 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
652 static int vidioc_querybuf(struct file *file, void *priv,
653 struct v4l2_buffer *buf)
655 struct m2mtest_ctx *ctx = file2ctx(file);
657 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
660 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
662 struct m2mtest_ctx *ctx = file2ctx(file);
664 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
667 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
669 struct m2mtest_ctx *ctx = file2ctx(file);
671 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
674 static int vidioc_streamon(struct file *file, void *priv,
675 enum v4l2_buf_type type)
677 struct m2mtest_ctx *ctx = file2ctx(file);
679 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
682 static int vidioc_streamoff(struct file *file, void *priv,
683 enum v4l2_buf_type type)
685 struct m2mtest_ctx *ctx = file2ctx(file);
687 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
690 static int m2mtest_s_ctrl(struct v4l2_ctrl *ctrl)
692 struct m2mtest_ctx *ctx =
693 container_of(ctrl->handler, struct m2mtest_ctx, hdl);
698 ctx->mode |= MEM2MEM_HFLIP;
700 ctx->mode &= ~MEM2MEM_HFLIP;
705 ctx->mode |= MEM2MEM_VFLIP;
707 ctx->mode &= ~MEM2MEM_VFLIP;
710 case V4L2_CID_TRANS_TIME_MSEC:
711 ctx->transtime = ctrl->val;
714 case V4L2_CID_TRANS_NUM_BUFS:
715 ctx->translen = ctrl->val;
719 v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
726 static const struct v4l2_ctrl_ops m2mtest_ctrl_ops = {
727 .s_ctrl = m2mtest_s_ctrl,
731 static const struct v4l2_ioctl_ops m2mtest_ioctl_ops = {
732 .vidioc_querycap = vidioc_querycap,
734 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
735 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
736 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
737 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
739 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
740 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
741 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
742 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
744 .vidioc_reqbufs = vidioc_reqbufs,
745 .vidioc_querybuf = vidioc_querybuf,
747 .vidioc_qbuf = vidioc_qbuf,
748 .vidioc_dqbuf = vidioc_dqbuf,
750 .vidioc_streamon = vidioc_streamon,
751 .vidioc_streamoff = vidioc_streamoff,
752 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
753 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
761 static int m2mtest_queue_setup(struct vb2_queue *vq,
762 const struct v4l2_format *fmt,
763 unsigned int *nbuffers, unsigned int *nplanes,
764 unsigned int sizes[], void *alloc_ctxs[])
766 struct m2mtest_ctx *ctx = vb2_get_drv_priv(vq);
767 struct m2mtest_q_data *q_data;
768 unsigned int size, count = *nbuffers;
770 q_data = get_q_data(ctx, vq->type);
772 size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
774 while (size * count > MEM2MEM_VID_MEM_LIMIT)
782 * videobuf2-vmalloc allocator is context-less so no need to set
786 dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
791 static int m2mtest_buf_prepare(struct vb2_buffer *vb)
793 struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
794 struct m2mtest_q_data *q_data;
796 dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
798 q_data = get_q_data(ctx, vb->vb2_queue->type);
800 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
801 dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n",
802 __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage);
806 vb2_set_plane_payload(vb, 0, q_data->sizeimage);
811 static void m2mtest_buf_queue(struct vb2_buffer *vb)
813 struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
814 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
817 static void m2mtest_wait_prepare(struct vb2_queue *q)
819 struct m2mtest_ctx *ctx = vb2_get_drv_priv(q);
823 static void m2mtest_wait_finish(struct vb2_queue *q)
825 struct m2mtest_ctx *ctx = vb2_get_drv_priv(q);
829 static struct vb2_ops m2mtest_qops = {
830 .queue_setup = m2mtest_queue_setup,
831 .buf_prepare = m2mtest_buf_prepare,
832 .buf_queue = m2mtest_buf_queue,
833 .wait_prepare = m2mtest_wait_prepare,
834 .wait_finish = m2mtest_wait_finish,
837 static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
839 struct m2mtest_ctx *ctx = priv;
842 memset(src_vq, 0, sizeof(*src_vq));
843 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
844 src_vq->io_modes = VB2_MMAP;
845 src_vq->drv_priv = ctx;
846 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
847 src_vq->ops = &m2mtest_qops;
848 src_vq->mem_ops = &vb2_vmalloc_memops;
850 ret = vb2_queue_init(src_vq);
854 memset(dst_vq, 0, sizeof(*dst_vq));
855 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
856 dst_vq->io_modes = VB2_MMAP;
857 dst_vq->drv_priv = ctx;
858 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
859 dst_vq->ops = &m2mtest_qops;
860 dst_vq->mem_ops = &vb2_vmalloc_memops;
862 return vb2_queue_init(dst_vq);
865 static const struct v4l2_ctrl_config m2mtest_ctrl_trans_time_msec = {
866 .ops = &m2mtest_ctrl_ops,
867 .id = V4L2_CID_TRANS_TIME_MSEC,
868 .name = "Transaction Time (msec)",
869 .type = V4L2_CTRL_TYPE_INTEGER,
876 static const struct v4l2_ctrl_config m2mtest_ctrl_trans_num_bufs = {
877 .ops = &m2mtest_ctrl_ops,
878 .id = V4L2_CID_TRANS_NUM_BUFS,
879 .name = "Buffers Per Transaction",
880 .type = V4L2_CTRL_TYPE_INTEGER,
883 .max = MEM2MEM_DEF_NUM_BUFS,
890 static int m2mtest_open(struct file *file)
892 struct m2mtest_dev *dev = video_drvdata(file);
893 struct m2mtest_ctx *ctx = NULL;
894 struct v4l2_ctrl_handler *hdl;
896 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
900 v4l2_fh_init(&ctx->fh, video_devdata(file));
901 file->private_data = &ctx->fh;
904 v4l2_ctrl_handler_init(hdl, 4);
905 v4l2_ctrl_new_std(hdl, &m2mtest_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
906 v4l2_ctrl_new_std(hdl, &m2mtest_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
907 v4l2_ctrl_new_custom(hdl, &m2mtest_ctrl_trans_time_msec, NULL);
908 v4l2_ctrl_new_custom(hdl, &m2mtest_ctrl_trans_num_bufs, NULL);
910 int err = hdl->error;
912 v4l2_ctrl_handler_free(hdl);
915 ctx->fh.ctrl_handler = hdl;
916 v4l2_ctrl_handler_setup(hdl);
918 ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
919 ctx->q_data[V4L2_M2M_SRC].width = 640;
920 ctx->q_data[V4L2_M2M_SRC].height = 480;
921 ctx->q_data[V4L2_M2M_SRC].sizeimage =
922 ctx->q_data[V4L2_M2M_SRC].width *
923 ctx->q_data[V4L2_M2M_SRC].height *
924 (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3);
925 ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
926 ctx->colorspace = V4L2_COLORSPACE_REC709;
928 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
930 if (IS_ERR(ctx->m2m_ctx)) {
931 int ret = PTR_ERR(ctx->m2m_ctx);
933 v4l2_ctrl_handler_free(hdl);
938 v4l2_fh_add(&ctx->fh);
939 atomic_inc(&dev->num_inst);
941 dprintk(dev, "Created instance %p, m2m_ctx: %p\n", ctx, ctx->m2m_ctx);
946 static int m2mtest_release(struct file *file)
948 struct m2mtest_dev *dev = video_drvdata(file);
949 struct m2mtest_ctx *ctx = file2ctx(file);
951 dprintk(dev, "Releasing instance %p\n", ctx);
953 v4l2_fh_del(&ctx->fh);
954 v4l2_fh_exit(&ctx->fh);
955 v4l2_ctrl_handler_free(&ctx->hdl);
956 v4l2_m2m_ctx_release(ctx->m2m_ctx);
959 atomic_dec(&dev->num_inst);
964 static unsigned int m2mtest_poll(struct file *file,
965 struct poll_table_struct *wait)
967 struct m2mtest_ctx *ctx = file2ctx(file);
969 return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
972 static int m2mtest_mmap(struct file *file, struct vm_area_struct *vma)
974 struct m2mtest_ctx *ctx = file2ctx(file);
976 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
979 static const struct v4l2_file_operations m2mtest_fops = {
980 .owner = THIS_MODULE,
981 .open = m2mtest_open,
982 .release = m2mtest_release,
983 .poll = m2mtest_poll,
984 .unlocked_ioctl = video_ioctl2,
985 .mmap = m2mtest_mmap,
988 static struct video_device m2mtest_videodev = {
989 .name = MEM2MEM_NAME,
990 .fops = &m2mtest_fops,
991 .ioctl_ops = &m2mtest_ioctl_ops,
993 .release = video_device_release,
996 static struct v4l2_m2m_ops m2m_ops = {
997 .device_run = device_run,
998 .job_ready = job_ready,
999 .job_abort = job_abort,
1000 .lock = m2mtest_lock,
1001 .unlock = m2mtest_unlock,
1004 static int m2mtest_probe(struct platform_device *pdev)
1006 struct m2mtest_dev *dev;
1007 struct video_device *vfd;
1010 dev = kzalloc(sizeof *dev, GFP_KERNEL);
1014 spin_lock_init(&dev->irqlock);
1016 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1020 atomic_set(&dev->num_inst, 0);
1021 mutex_init(&dev->dev_mutex);
1023 vfd = video_device_alloc();
1025 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
1030 *vfd = m2mtest_videodev;
1031 /* Locking in file operations other than ioctl should be done
1032 by the driver, not the V4L2 core.
1033 This driver needs auditing so that this flag can be removed. */
1034 set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
1035 vfd->lock = &dev->dev_mutex;
1037 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1039 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1043 video_set_drvdata(vfd, dev);
1044 snprintf(vfd->name, sizeof(vfd->name), "%s", m2mtest_videodev.name);
1046 v4l2_info(&dev->v4l2_dev, MEM2MEM_TEST_MODULE_NAME
1047 "Device registered as /dev/video%d\n", vfd->num);
1049 setup_timer(&dev->timer, device_isr, (long)dev);
1050 platform_set_drvdata(pdev, dev);
1052 dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
1053 if (IS_ERR(dev->m2m_dev)) {
1054 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1055 ret = PTR_ERR(dev->m2m_dev);
1061 v4l2_m2m_release(dev->m2m_dev);
1063 video_unregister_device(dev->vfd);
1065 video_device_release(vfd);
1067 v4l2_device_unregister(&dev->v4l2_dev);
1074 static int m2mtest_remove(struct platform_device *pdev)
1076 struct m2mtest_dev *dev =
1077 (struct m2mtest_dev *)platform_get_drvdata(pdev);
1079 v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_TEST_MODULE_NAME);
1080 v4l2_m2m_release(dev->m2m_dev);
1081 del_timer_sync(&dev->timer);
1082 video_unregister_device(dev->vfd);
1083 v4l2_device_unregister(&dev->v4l2_dev);
1089 static struct platform_driver m2mtest_pdrv = {
1090 .probe = m2mtest_probe,
1091 .remove = m2mtest_remove,
1093 .name = MEM2MEM_NAME,
1094 .owner = THIS_MODULE,
1098 static void __exit m2mtest_exit(void)
1100 platform_driver_unregister(&m2mtest_pdrv);
1101 platform_device_unregister(&m2mtest_pdev);
1104 static int __init m2mtest_init(void)
1108 ret = platform_device_register(&m2mtest_pdev);
1112 ret = platform_driver_register(&m2mtest_pdrv);
1114 platform_device_unregister(&m2mtest_pdev);
1119 module_init(m2mtest_init);
1120 module_exit(m2mtest_exit);