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/videobuf2-vmalloc.h>
33 #define MEM2MEM_TEST_MODULE_NAME "mem2mem-testdev"
35 MODULE_DESCRIPTION("Virtual device for mem2mem framework testing");
36 MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
37 MODULE_LICENSE("GPL");
38 MODULE_VERSION("0.1.1");
44 #define DIM_ALIGN_MASK 7 /* 8-byte alignment for line length */
46 /* Flags that indicate a format can be used for capture/output */
47 #define MEM2MEM_CAPTURE (1 << 0)
48 #define MEM2MEM_OUTPUT (1 << 1)
50 #define MEM2MEM_NAME "m2m-testdev"
53 #define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME
54 /* In bytes, per queue */
55 #define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024)
57 /* Default transaction time in msec */
58 #define MEM2MEM_DEF_TRANSTIME 1000
59 /* Default number of buffers per transaction */
60 #define MEM2MEM_DEF_TRANSLEN 1
61 #define MEM2MEM_COLOR_STEP (0xff >> 4)
62 #define MEM2MEM_NUM_TILES 8
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, 1, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
72 void m2mtest_dev_release(struct device *dev)
75 static struct platform_device m2mtest_pdev = {
77 .dev.release = m2mtest_dev_release,
84 /* Types the format can be used for */
88 static struct m2mtest_fmt formats[] = {
90 .name = "RGB565 (BE)",
91 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
93 /* Both capture and output format */
94 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
97 .name = "4:2:2, packed, YUYV",
98 .fourcc = V4L2_PIX_FMT_YUYV,
100 /* Output-only format */
101 .types = MEM2MEM_OUTPUT,
105 #define NUM_FORMATS ARRAY_SIZE(formats)
107 /* Per-queue, driver-specific private data */
108 struct m2mtest_q_data {
111 unsigned int sizeimage;
112 struct m2mtest_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 m2mtest_fmt *find_format(struct v4l2_format *f)
125 struct m2mtest_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;
145 struct mutex dev_mutex;
148 struct timer_list timer;
150 struct v4l2_m2m_dev *m2m_dev;
155 struct m2mtest_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 /* Abort requested by m2m */
170 /* Processing mode */
173 struct v4l2_m2m_ctx *m2m_ctx;
175 /* Source and destination queue data */
176 struct m2mtest_q_data q_data[2];
179 static inline struct m2mtest_ctx *file2ctx(struct file *file)
181 return container_of(file->private_data, struct m2mtest_ctx, fh);
184 static struct m2mtest_q_data *get_q_data(struct m2mtest_ctx *ctx,
185 enum v4l2_buf_type type)
188 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
189 return &ctx->q_data[V4L2_M2M_SRC];
190 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
191 return &ctx->q_data[V4L2_M2M_DST];
199 static int device_process(struct m2mtest_ctx *ctx,
200 struct vb2_buffer *in_vb,
201 struct vb2_buffer *out_vb)
203 struct m2mtest_dev *dev = ctx->dev;
204 struct m2mtest_q_data *q_data;
207 int tile_w, bytes_left;
208 int width, height, bytesperline;
210 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
212 width = q_data->width;
213 height = q_data->height;
214 bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
216 p_in = vb2_plane_vaddr(in_vb, 0);
217 p_out = vb2_plane_vaddr(out_vb, 0);
218 if (!p_in || !p_out) {
219 v4l2_err(&dev->v4l2_dev,
220 "Acquiring kernel pointers to buffers failed\n");
224 if (vb2_plane_size(in_vb, 0) > vb2_plane_size(out_vb, 0)) {
225 v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n");
229 tile_w = (width * (q_data[V4L2_M2M_DST].fmt->depth >> 3))
231 bytes_left = bytesperline - tile_w * MEM2MEM_NUM_TILES;
235 case MEM2MEM_HFLIP | MEM2MEM_VFLIP:
236 p_out += bytesperline * height - bytes_left;
237 for (y = 0; y < height; ++y) {
238 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
240 for (x = 0; x < tile_w; ++x)
244 for (x = 0; x < tile_w; ++x)
256 for (y = 0; y < height; ++y) {
257 p_out += MEM2MEM_NUM_TILES * tile_w;
258 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
260 for (x = 0; x < tile_w; ++x)
264 for (x = 0; x < tile_w; ++x)
271 p_out += bytesperline;
276 p_out += bytesperline * (height - 1);
277 for (y = 0; y < height; ++y) {
278 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
280 for (x = 0; x < tile_w; ++x)
284 for (x = 0; x < tile_w; ++x)
291 p_out += bytes_left - 2 * bytesperline;
296 for (y = 0; y < height; ++y) {
297 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
299 for (x = 0; x < tile_w; ++x)
303 for (x = 0; x < tile_w; ++x)
317 static void schedule_irq(struct m2mtest_dev *dev, int msec_timeout)
319 dprintk(dev, "Scheduling a simulated irq\n");
320 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(msec_timeout));
328 * job_ready() - check whether an instance is ready to be scheduled to run
330 static int job_ready(void *priv)
332 struct m2mtest_ctx *ctx = priv;
334 if (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) < ctx->translen
335 || v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx) < ctx->translen) {
336 dprintk(ctx->dev, "Not enough buffers available\n");
343 static void job_abort(void *priv)
345 struct m2mtest_ctx *ctx = priv;
347 /* Will cancel the transaction in the next interrupt handler */
351 static void m2mtest_lock(void *priv)
353 struct m2mtest_ctx *ctx = priv;
354 struct m2mtest_dev *dev = ctx->dev;
355 mutex_lock(&dev->dev_mutex);
358 static void m2mtest_unlock(void *priv)
360 struct m2mtest_ctx *ctx = priv;
361 struct m2mtest_dev *dev = ctx->dev;
362 mutex_unlock(&dev->dev_mutex);
366 /* device_run() - prepares and starts the device
368 * This simulates all the immediate preparations required before starting
369 * a device. This will be called by the framework when it decides to schedule
370 * a particular instance.
372 static void device_run(void *priv)
374 struct m2mtest_ctx *ctx = priv;
375 struct m2mtest_dev *dev = ctx->dev;
376 struct vb2_buffer *src_buf, *dst_buf;
378 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
379 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
381 device_process(ctx, src_buf, dst_buf);
383 /* Run a timer, which simulates a hardware irq */
384 schedule_irq(dev, ctx->transtime);
387 static void device_isr(unsigned long priv)
389 struct m2mtest_dev *m2mtest_dev = (struct m2mtest_dev *)priv;
390 struct m2mtest_ctx *curr_ctx;
391 struct vb2_buffer *src_vb, *dst_vb;
394 curr_ctx = v4l2_m2m_get_curr_priv(m2mtest_dev->m2m_dev);
396 if (NULL == curr_ctx) {
398 "Instance released before the end of transaction\n");
402 src_vb = v4l2_m2m_src_buf_remove(curr_ctx->m2m_ctx);
403 dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->m2m_ctx);
405 curr_ctx->num_processed++;
407 spin_lock_irqsave(&m2mtest_dev->irqlock, flags);
408 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
409 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
410 spin_unlock_irqrestore(&m2mtest_dev->irqlock, flags);
412 if (curr_ctx->num_processed == curr_ctx->translen
413 || curr_ctx->aborting) {
414 dprintk(curr_ctx->dev, "Finishing transaction\n");
415 curr_ctx->num_processed = 0;
416 v4l2_m2m_job_finish(m2mtest_dev->m2m_dev, curr_ctx->m2m_ctx);
418 device_run(curr_ctx);
425 static int vidioc_querycap(struct file *file, void *priv,
426 struct v4l2_capability *cap)
428 strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
429 strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
430 strlcpy(cap->bus_info, MEM2MEM_NAME, sizeof(cap->bus_info));
431 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT
432 | V4L2_CAP_STREAMING;
433 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
437 static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
440 struct m2mtest_fmt *fmt;
444 for (i = 0; i < NUM_FORMATS; ++i) {
445 if (formats[i].types & type) {
446 /* index-th format of type type found ? */
449 /* Correct type but haven't reached our index yet,
450 * just increment per-type index */
455 if (i < NUM_FORMATS) {
458 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
459 f->pixelformat = fmt->fourcc;
463 /* Format not found */
467 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
468 struct v4l2_fmtdesc *f)
470 return enum_fmt(f, MEM2MEM_CAPTURE);
473 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
474 struct v4l2_fmtdesc *f)
476 return enum_fmt(f, MEM2MEM_OUTPUT);
479 static int vidioc_g_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
481 struct vb2_queue *vq;
482 struct m2mtest_q_data *q_data;
484 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
488 q_data = get_q_data(ctx, f->type);
490 f->fmt.pix.width = q_data->width;
491 f->fmt.pix.height = q_data->height;
492 f->fmt.pix.field = V4L2_FIELD_NONE;
493 f->fmt.pix.pixelformat = q_data->fmt->fourcc;
494 f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
495 f->fmt.pix.sizeimage = q_data->sizeimage;
500 static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
501 struct v4l2_format *f)
503 return vidioc_g_fmt(file2ctx(file), f);
506 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
507 struct v4l2_format *f)
509 return vidioc_g_fmt(file2ctx(file), f);
512 static int vidioc_try_fmt(struct v4l2_format *f, struct m2mtest_fmt *fmt)
514 enum v4l2_field field;
516 field = f->fmt.pix.field;
518 if (field == V4L2_FIELD_ANY)
519 field = V4L2_FIELD_NONE;
520 else if (V4L2_FIELD_NONE != field)
523 /* V4L2 specification suggests the driver corrects the format struct
524 * if any of the dimensions is unsupported */
525 f->fmt.pix.field = field;
527 if (f->fmt.pix.height < MIN_H)
528 f->fmt.pix.height = MIN_H;
529 else if (f->fmt.pix.height > MAX_H)
530 f->fmt.pix.height = MAX_H;
532 if (f->fmt.pix.width < MIN_W)
533 f->fmt.pix.width = MIN_W;
534 else if (f->fmt.pix.width > MAX_W)
535 f->fmt.pix.width = MAX_W;
537 f->fmt.pix.width &= ~DIM_ALIGN_MASK;
538 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
539 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
544 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
545 struct v4l2_format *f)
547 struct m2mtest_fmt *fmt;
548 struct m2mtest_ctx *ctx = file2ctx(file);
550 fmt = find_format(f);
551 if (!fmt || !(fmt->types & MEM2MEM_CAPTURE)) {
552 v4l2_err(&ctx->dev->v4l2_dev,
553 "Fourcc format (0x%08x) invalid.\n",
554 f->fmt.pix.pixelformat);
558 return vidioc_try_fmt(f, fmt);
561 static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
562 struct v4l2_format *f)
564 struct m2mtest_fmt *fmt;
565 struct m2mtest_ctx *ctx = file2ctx(file);
567 fmt = find_format(f);
568 if (!fmt || !(fmt->types & MEM2MEM_OUTPUT)) {
569 v4l2_err(&ctx->dev->v4l2_dev,
570 "Fourcc format (0x%08x) invalid.\n",
571 f->fmt.pix.pixelformat);
575 return vidioc_try_fmt(f, fmt);
578 static int vidioc_s_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
580 struct m2mtest_q_data *q_data;
581 struct vb2_queue *vq;
583 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
587 q_data = get_q_data(ctx, f->type);
591 if (vb2_is_busy(vq)) {
592 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
596 q_data->fmt = find_format(f);
597 q_data->width = f->fmt.pix.width;
598 q_data->height = f->fmt.pix.height;
599 q_data->sizeimage = q_data->width * q_data->height
600 * q_data->fmt->depth >> 3;
603 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
604 f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
609 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
610 struct v4l2_format *f)
614 ret = vidioc_try_fmt_vid_cap(file, priv, f);
618 return vidioc_s_fmt(file2ctx(file), f);
621 static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
622 struct v4l2_format *f)
626 ret = vidioc_try_fmt_vid_out(file, priv, f);
630 return vidioc_s_fmt(file2ctx(file), f);
633 static int vidioc_reqbufs(struct file *file, void *priv,
634 struct v4l2_requestbuffers *reqbufs)
636 struct m2mtest_ctx *ctx = file2ctx(file);
638 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
641 static int vidioc_querybuf(struct file *file, void *priv,
642 struct v4l2_buffer *buf)
644 struct m2mtest_ctx *ctx = file2ctx(file);
646 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
649 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
651 struct m2mtest_ctx *ctx = file2ctx(file);
653 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
656 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
658 struct m2mtest_ctx *ctx = file2ctx(file);
660 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
663 static int vidioc_streamon(struct file *file, void *priv,
664 enum v4l2_buf_type type)
666 struct m2mtest_ctx *ctx = file2ctx(file);
668 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
671 static int vidioc_streamoff(struct file *file, void *priv,
672 enum v4l2_buf_type type)
674 struct m2mtest_ctx *ctx = file2ctx(file);
676 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
679 static int m2mtest_s_ctrl(struct v4l2_ctrl *ctrl)
681 struct m2mtest_ctx *ctx =
682 container_of(ctrl->handler, struct m2mtest_ctx, hdl);
687 ctx->mode |= MEM2MEM_HFLIP;
689 ctx->mode &= ~MEM2MEM_HFLIP;
694 ctx->mode |= MEM2MEM_VFLIP;
696 ctx->mode &= ~MEM2MEM_VFLIP;
699 case V4L2_CID_TRANS_TIME_MSEC:
700 ctx->transtime = ctrl->val;
703 case V4L2_CID_TRANS_NUM_BUFS:
704 ctx->translen = ctrl->val;
708 v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
715 static const struct v4l2_ctrl_ops m2mtest_ctrl_ops = {
716 .s_ctrl = m2mtest_s_ctrl,
720 static const struct v4l2_ioctl_ops m2mtest_ioctl_ops = {
721 .vidioc_querycap = vidioc_querycap,
723 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
724 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
725 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
726 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
728 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
729 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
730 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
731 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
733 .vidioc_reqbufs = vidioc_reqbufs,
734 .vidioc_querybuf = vidioc_querybuf,
736 .vidioc_qbuf = vidioc_qbuf,
737 .vidioc_dqbuf = vidioc_dqbuf,
739 .vidioc_streamon = vidioc_streamon,
740 .vidioc_streamoff = vidioc_streamoff,
748 static int m2mtest_queue_setup(struct vb2_queue *vq,
749 const struct v4l2_format *fmt,
750 unsigned int *nbuffers, unsigned int *nplanes,
751 unsigned int sizes[], void *alloc_ctxs[])
753 struct m2mtest_ctx *ctx = vb2_get_drv_priv(vq);
754 struct m2mtest_q_data *q_data;
755 unsigned int size, count = *nbuffers;
757 q_data = get_q_data(ctx, vq->type);
759 size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
761 while (size * count > MEM2MEM_VID_MEM_LIMIT)
769 * videobuf2-vmalloc allocator is context-less so no need to set
773 dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
778 static int m2mtest_buf_prepare(struct vb2_buffer *vb)
780 struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
781 struct m2mtest_q_data *q_data;
783 dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
785 q_data = get_q_data(ctx, vb->vb2_queue->type);
787 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
788 dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n",
789 __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage);
793 vb2_set_plane_payload(vb, 0, q_data->sizeimage);
798 static void m2mtest_buf_queue(struct vb2_buffer *vb)
800 struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
801 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
804 static void m2mtest_wait_prepare(struct vb2_queue *q)
806 struct m2mtest_ctx *ctx = vb2_get_drv_priv(q);
810 static void m2mtest_wait_finish(struct vb2_queue *q)
812 struct m2mtest_ctx *ctx = vb2_get_drv_priv(q);
816 static struct vb2_ops m2mtest_qops = {
817 .queue_setup = m2mtest_queue_setup,
818 .buf_prepare = m2mtest_buf_prepare,
819 .buf_queue = m2mtest_buf_queue,
820 .wait_prepare = m2mtest_wait_prepare,
821 .wait_finish = m2mtest_wait_finish,
824 static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
826 struct m2mtest_ctx *ctx = priv;
829 memset(src_vq, 0, sizeof(*src_vq));
830 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
831 src_vq->io_modes = VB2_MMAP;
832 src_vq->drv_priv = ctx;
833 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
834 src_vq->ops = &m2mtest_qops;
835 src_vq->mem_ops = &vb2_vmalloc_memops;
837 ret = vb2_queue_init(src_vq);
841 memset(dst_vq, 0, sizeof(*dst_vq));
842 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
843 dst_vq->io_modes = VB2_MMAP;
844 dst_vq->drv_priv = ctx;
845 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
846 dst_vq->ops = &m2mtest_qops;
847 dst_vq->mem_ops = &vb2_vmalloc_memops;
849 return vb2_queue_init(dst_vq);
852 static const struct v4l2_ctrl_config m2mtest_ctrl_trans_time_msec = {
853 .ops = &m2mtest_ctrl_ops,
854 .id = V4L2_CID_TRANS_TIME_MSEC,
855 .name = "Transaction Time (msec)",
856 .type = V4L2_CTRL_TYPE_INTEGER,
863 static const struct v4l2_ctrl_config m2mtest_ctrl_trans_num_bufs = {
864 .ops = &m2mtest_ctrl_ops,
865 .id = V4L2_CID_TRANS_NUM_BUFS,
866 .name = "Buffers Per Transaction",
867 .type = V4L2_CTRL_TYPE_INTEGER,
870 .max = MEM2MEM_DEF_NUM_BUFS,
877 static int m2mtest_open(struct file *file)
879 struct m2mtest_dev *dev = video_drvdata(file);
880 struct m2mtest_ctx *ctx = NULL;
881 struct v4l2_ctrl_handler *hdl;
883 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
887 v4l2_fh_init(&ctx->fh, video_devdata(file));
888 file->private_data = &ctx->fh;
891 v4l2_ctrl_handler_init(hdl, 4);
892 v4l2_ctrl_new_std(hdl, &m2mtest_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
893 v4l2_ctrl_new_std(hdl, &m2mtest_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
894 v4l2_ctrl_new_custom(hdl, &m2mtest_ctrl_trans_time_msec, NULL);
895 v4l2_ctrl_new_custom(hdl, &m2mtest_ctrl_trans_num_bufs, NULL);
897 int err = hdl->error;
899 v4l2_ctrl_handler_free(hdl);
902 ctx->fh.ctrl_handler = hdl;
903 v4l2_ctrl_handler_setup(hdl);
905 ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
906 ctx->q_data[V4L2_M2M_DST].fmt = &formats[0];
908 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
910 if (IS_ERR(ctx->m2m_ctx)) {
911 int ret = PTR_ERR(ctx->m2m_ctx);
913 v4l2_ctrl_handler_free(hdl);
918 v4l2_fh_add(&ctx->fh);
919 atomic_inc(&dev->num_inst);
921 dprintk(dev, "Created instance %p, m2m_ctx: %p\n", ctx, ctx->m2m_ctx);
926 static int m2mtest_release(struct file *file)
928 struct m2mtest_dev *dev = video_drvdata(file);
929 struct m2mtest_ctx *ctx = file2ctx(file);
931 dprintk(dev, "Releasing instance %p\n", ctx);
933 v4l2_fh_del(&ctx->fh);
934 v4l2_fh_exit(&ctx->fh);
935 v4l2_ctrl_handler_free(&ctx->hdl);
936 v4l2_m2m_ctx_release(ctx->m2m_ctx);
939 atomic_dec(&dev->num_inst);
944 static unsigned int m2mtest_poll(struct file *file,
945 struct poll_table_struct *wait)
947 struct m2mtest_ctx *ctx = file2ctx(file);
949 return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
952 static int m2mtest_mmap(struct file *file, struct vm_area_struct *vma)
954 struct m2mtest_ctx *ctx = file2ctx(file);
956 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
959 static const struct v4l2_file_operations m2mtest_fops = {
960 .owner = THIS_MODULE,
961 .open = m2mtest_open,
962 .release = m2mtest_release,
963 .poll = m2mtest_poll,
964 .unlocked_ioctl = video_ioctl2,
965 .mmap = m2mtest_mmap,
968 static struct video_device m2mtest_videodev = {
969 .name = MEM2MEM_NAME,
970 .fops = &m2mtest_fops,
971 .ioctl_ops = &m2mtest_ioctl_ops,
973 .release = video_device_release,
976 static struct v4l2_m2m_ops m2m_ops = {
977 .device_run = device_run,
978 .job_ready = job_ready,
979 .job_abort = job_abort,
980 .lock = m2mtest_lock,
981 .unlock = m2mtest_unlock,
984 static int m2mtest_probe(struct platform_device *pdev)
986 struct m2mtest_dev *dev;
987 struct video_device *vfd;
990 dev = kzalloc(sizeof *dev, GFP_KERNEL);
994 spin_lock_init(&dev->irqlock);
996 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1000 atomic_set(&dev->num_inst, 0);
1001 mutex_init(&dev->dev_mutex);
1003 vfd = video_device_alloc();
1005 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
1010 *vfd = m2mtest_videodev;
1011 /* Locking in file operations other than ioctl should be done
1012 by the driver, not the V4L2 core.
1013 This driver needs auditing so that this flag can be removed. */
1014 set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
1015 vfd->lock = &dev->dev_mutex;
1017 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1019 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1023 video_set_drvdata(vfd, dev);
1024 snprintf(vfd->name, sizeof(vfd->name), "%s", m2mtest_videodev.name);
1026 v4l2_info(&dev->v4l2_dev, MEM2MEM_TEST_MODULE_NAME
1027 "Device registered as /dev/video%d\n", vfd->num);
1029 setup_timer(&dev->timer, device_isr, (long)dev);
1030 platform_set_drvdata(pdev, dev);
1032 dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
1033 if (IS_ERR(dev->m2m_dev)) {
1034 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1035 ret = PTR_ERR(dev->m2m_dev);
1041 v4l2_m2m_release(dev->m2m_dev);
1043 video_unregister_device(dev->vfd);
1045 video_device_release(vfd);
1047 v4l2_device_unregister(&dev->v4l2_dev);
1054 static int m2mtest_remove(struct platform_device *pdev)
1056 struct m2mtest_dev *dev =
1057 (struct m2mtest_dev *)platform_get_drvdata(pdev);
1059 v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_TEST_MODULE_NAME);
1060 v4l2_m2m_release(dev->m2m_dev);
1061 del_timer_sync(&dev->timer);
1062 video_unregister_device(dev->vfd);
1063 v4l2_device_unregister(&dev->v4l2_dev);
1069 static struct platform_driver m2mtest_pdrv = {
1070 .probe = m2mtest_probe,
1071 .remove = m2mtest_remove,
1073 .name = MEM2MEM_NAME,
1074 .owner = THIS_MODULE,
1078 static void __exit m2mtest_exit(void)
1080 platform_driver_unregister(&m2mtest_pdrv);
1081 platform_device_unregister(&m2mtest_pdev);
1084 static int __init m2mtest_init(void)
1088 ret = platform_device_register(&m2mtest_pdev);
1092 ret = platform_driver_register(&m2mtest_pdrv);
1094 platform_device_unregister(&m2mtest_pdev);
1099 module_init(m2mtest_init);
1100 module_exit(m2mtest_exit);