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/videobuf2-vmalloc.h>
32 #define MEM2MEM_TEST_MODULE_NAME "mem2mem-testdev"
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");
43 #define DIM_ALIGN_MASK 7 /* 8-byte alignment for line length */
45 /* Flags that indicate a format can be used for capture/output */
46 #define MEM2MEM_CAPTURE (1 << 0)
47 #define MEM2MEM_OUTPUT (1 << 1)
49 #define MEM2MEM_NAME "m2m-testdev"
52 #define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME
53 /* In bytes, per queue */
54 #define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024)
56 /* Default transaction time in msec */
57 #define MEM2MEM_DEF_TRANSTIME 1000
58 /* Default number of buffers per transaction */
59 #define MEM2MEM_DEF_TRANSLEN 1
60 #define MEM2MEM_COLOR_STEP (0xff >> 4)
61 #define MEM2MEM_NUM_TILES 8
63 /* Flags that indicate processing mode */
64 #define MEM2MEM_HFLIP (1 << 0)
65 #define MEM2MEM_VFLIP (1 << 1)
67 #define dprintk(dev, fmt, arg...) \
68 v4l2_dbg(1, 1, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
71 void m2mtest_dev_release(struct device *dev)
74 static struct platform_device m2mtest_pdev = {
76 .dev.release = m2mtest_dev_release,
83 /* Types the format can be used for */
87 static struct m2mtest_fmt formats[] = {
89 .name = "RGB565 (BE)",
90 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
92 /* Both capture and output format */
93 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
96 .name = "4:2:2, packed, YUYV",
97 .fourcc = V4L2_PIX_FMT_YUYV,
99 /* Output-only format */
100 .types = MEM2MEM_OUTPUT,
104 /* Per-queue, driver-specific private data */
105 struct m2mtest_q_data {
108 unsigned int sizeimage;
109 struct m2mtest_fmt *fmt;
117 #define V4L2_CID_TRANS_TIME_MSEC V4L2_CID_PRIVATE_BASE
118 #define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_PRIVATE_BASE + 1)
120 static struct v4l2_queryctrl m2mtest_ctrls[] = {
122 .id = V4L2_CID_HFLIP,
123 .type = V4L2_CTRL_TYPE_BOOLEAN,
131 .id = V4L2_CID_VFLIP,
132 .type = V4L2_CTRL_TYPE_BOOLEAN,
133 .name = "Vertical Mirror",
140 .id = V4L2_CID_TRANS_TIME_MSEC,
141 .type = V4L2_CTRL_TYPE_INTEGER,
142 .name = "Transaction time (msec)",
146 .default_value = 1000,
149 .id = V4L2_CID_TRANS_NUM_BUFS,
150 .type = V4L2_CTRL_TYPE_INTEGER,
151 .name = "Buffers per transaction",
153 .maximum = MEM2MEM_DEF_NUM_BUFS,
160 #define NUM_FORMATS ARRAY_SIZE(formats)
162 static struct m2mtest_fmt *find_format(struct v4l2_format *f)
164 struct m2mtest_fmt *fmt;
167 for (k = 0; k < NUM_FORMATS; k++) {
169 if (fmt->fourcc == f->fmt.pix.pixelformat)
173 if (k == NUM_FORMATS)
180 struct v4l2_device v4l2_dev;
181 struct video_device *vfd;
184 struct mutex dev_mutex;
187 struct timer_list timer;
189 struct v4l2_m2m_dev *m2m_dev;
193 struct m2mtest_dev *dev;
195 /* Processed buffers in this transaction */
198 /* Transaction length (i.e. how many buffers per transaction) */
200 /* Transaction time (i.e. simulated processing time) in milliseconds */
203 /* Abort requested by m2m */
206 /* Processing mode */
209 struct v4l2_m2m_ctx *m2m_ctx;
211 /* Source and destination queue data */
212 struct m2mtest_q_data q_data[2];
215 static struct m2mtest_q_data *get_q_data(struct m2mtest_ctx *ctx,
216 enum v4l2_buf_type type)
219 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
220 return &ctx->q_data[V4L2_M2M_SRC];
221 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
222 return &ctx->q_data[V4L2_M2M_DST];
230 static struct v4l2_queryctrl *get_ctrl(int id)
234 for (i = 0; i < ARRAY_SIZE(m2mtest_ctrls); ++i) {
235 if (id == m2mtest_ctrls[i].id)
236 return &m2mtest_ctrls[i];
242 static int device_process(struct m2mtest_ctx *ctx,
243 struct vb2_buffer *in_vb,
244 struct vb2_buffer *out_vb)
246 struct m2mtest_dev *dev = ctx->dev;
247 struct m2mtest_q_data *q_data;
250 int tile_w, bytes_left;
251 int width, height, bytesperline;
253 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
255 width = q_data->width;
256 height = q_data->height;
257 bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
259 p_in = vb2_plane_vaddr(in_vb, 0);
260 p_out = vb2_plane_vaddr(out_vb, 0);
261 if (!p_in || !p_out) {
262 v4l2_err(&dev->v4l2_dev,
263 "Acquiring kernel pointers to buffers failed\n");
267 if (vb2_plane_size(in_vb, 0) > vb2_plane_size(out_vb, 0)) {
268 v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n");
272 tile_w = (width * (q_data[V4L2_M2M_DST].fmt->depth >> 3))
274 bytes_left = bytesperline - tile_w * MEM2MEM_NUM_TILES;
278 case MEM2MEM_HFLIP | MEM2MEM_VFLIP:
279 p_out += bytesperline * height - bytes_left;
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)
299 for (y = 0; y < height; ++y) {
300 p_out += MEM2MEM_NUM_TILES * tile_w;
301 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
303 for (x = 0; x < tile_w; ++x)
307 for (x = 0; x < tile_w; ++x)
314 p_out += bytesperline;
319 p_out += bytesperline * (height - 1);
320 for (y = 0; y < height; ++y) {
321 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
323 for (x = 0; x < tile_w; ++x)
327 for (x = 0; x < tile_w; ++x)
334 p_out += bytes_left - 2 * bytesperline;
339 for (y = 0; y < height; ++y) {
340 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
342 for (x = 0; x < tile_w; ++x)
346 for (x = 0; x < tile_w; ++x)
360 static void schedule_irq(struct m2mtest_dev *dev, int msec_timeout)
362 dprintk(dev, "Scheduling a simulated irq\n");
363 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(msec_timeout));
371 * job_ready() - check whether an instance is ready to be scheduled to run
373 static int job_ready(void *priv)
375 struct m2mtest_ctx *ctx = priv;
377 if (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) < ctx->translen
378 || v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx) < ctx->translen) {
379 dprintk(ctx->dev, "Not enough buffers available\n");
386 static void job_abort(void *priv)
388 struct m2mtest_ctx *ctx = priv;
390 /* Will cancel the transaction in the next interrupt handler */
394 static void m2mtest_lock(void *priv)
396 struct m2mtest_ctx *ctx = priv;
397 struct m2mtest_dev *dev = ctx->dev;
398 mutex_lock(&dev->dev_mutex);
401 static void m2mtest_unlock(void *priv)
403 struct m2mtest_ctx *ctx = priv;
404 struct m2mtest_dev *dev = ctx->dev;
405 mutex_unlock(&dev->dev_mutex);
409 /* device_run() - prepares and starts the device
411 * This simulates all the immediate preparations required before starting
412 * a device. This will be called by the framework when it decides to schedule
413 * a particular instance.
415 static void device_run(void *priv)
417 struct m2mtest_ctx *ctx = priv;
418 struct m2mtest_dev *dev = ctx->dev;
419 struct vb2_buffer *src_buf, *dst_buf;
421 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
422 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
424 device_process(ctx, src_buf, dst_buf);
426 /* Run a timer, which simulates a hardware irq */
427 schedule_irq(dev, ctx->transtime);
430 static void device_isr(unsigned long priv)
432 struct m2mtest_dev *m2mtest_dev = (struct m2mtest_dev *)priv;
433 struct m2mtest_ctx *curr_ctx;
434 struct vb2_buffer *src_vb, *dst_vb;
437 curr_ctx = v4l2_m2m_get_curr_priv(m2mtest_dev->m2m_dev);
439 if (NULL == curr_ctx) {
441 "Instance released before the end of transaction\n");
445 src_vb = v4l2_m2m_src_buf_remove(curr_ctx->m2m_ctx);
446 dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->m2m_ctx);
448 curr_ctx->num_processed++;
450 spin_lock_irqsave(&m2mtest_dev->irqlock, flags);
451 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
452 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
453 spin_unlock_irqrestore(&m2mtest_dev->irqlock, flags);
455 if (curr_ctx->num_processed == curr_ctx->translen
456 || curr_ctx->aborting) {
457 dprintk(curr_ctx->dev, "Finishing transaction\n");
458 curr_ctx->num_processed = 0;
459 v4l2_m2m_job_finish(m2mtest_dev->m2m_dev, curr_ctx->m2m_ctx);
461 device_run(curr_ctx);
468 static int vidioc_querycap(struct file *file, void *priv,
469 struct v4l2_capability *cap)
471 strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
472 strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
473 cap->bus_info[0] = 0;
474 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT
475 | V4L2_CAP_STREAMING;
480 static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
483 struct m2mtest_fmt *fmt;
487 for (i = 0; i < NUM_FORMATS; ++i) {
488 if (formats[i].types & type) {
489 /* index-th format of type type found ? */
492 /* Correct type but haven't reached our index yet,
493 * just increment per-type index */
498 if (i < NUM_FORMATS) {
501 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
502 f->pixelformat = fmt->fourcc;
506 /* Format not found */
510 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
511 struct v4l2_fmtdesc *f)
513 return enum_fmt(f, MEM2MEM_CAPTURE);
516 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
517 struct v4l2_fmtdesc *f)
519 return enum_fmt(f, MEM2MEM_OUTPUT);
522 static int vidioc_g_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
524 struct vb2_queue *vq;
525 struct m2mtest_q_data *q_data;
527 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
531 q_data = get_q_data(ctx, f->type);
533 f->fmt.pix.width = q_data->width;
534 f->fmt.pix.height = q_data->height;
535 f->fmt.pix.field = V4L2_FIELD_NONE;
536 f->fmt.pix.pixelformat = q_data->fmt->fourcc;
537 f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
538 f->fmt.pix.sizeimage = q_data->sizeimage;
543 static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
544 struct v4l2_format *f)
546 return vidioc_g_fmt(priv, f);
549 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
550 struct v4l2_format *f)
552 return vidioc_g_fmt(priv, f);
555 static int vidioc_try_fmt(struct v4l2_format *f, struct m2mtest_fmt *fmt)
557 enum v4l2_field field;
559 field = f->fmt.pix.field;
561 if (field == V4L2_FIELD_ANY)
562 field = V4L2_FIELD_NONE;
563 else if (V4L2_FIELD_NONE != field)
566 /* V4L2 specification suggests the driver corrects the format struct
567 * if any of the dimensions is unsupported */
568 f->fmt.pix.field = field;
570 if (f->fmt.pix.height < MIN_H)
571 f->fmt.pix.height = MIN_H;
572 else if (f->fmt.pix.height > MAX_H)
573 f->fmt.pix.height = MAX_H;
575 if (f->fmt.pix.width < MIN_W)
576 f->fmt.pix.width = MIN_W;
577 else if (f->fmt.pix.width > MAX_W)
578 f->fmt.pix.width = MAX_W;
580 f->fmt.pix.width &= ~DIM_ALIGN_MASK;
581 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
582 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
587 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
588 struct v4l2_format *f)
590 struct m2mtest_fmt *fmt;
591 struct m2mtest_ctx *ctx = priv;
593 fmt = find_format(f);
594 if (!fmt || !(fmt->types & MEM2MEM_CAPTURE)) {
595 v4l2_err(&ctx->dev->v4l2_dev,
596 "Fourcc format (0x%08x) invalid.\n",
597 f->fmt.pix.pixelformat);
601 return vidioc_try_fmt(f, fmt);
604 static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
605 struct v4l2_format *f)
607 struct m2mtest_fmt *fmt;
608 struct m2mtest_ctx *ctx = priv;
610 fmt = find_format(f);
611 if (!fmt || !(fmt->types & MEM2MEM_OUTPUT)) {
612 v4l2_err(&ctx->dev->v4l2_dev,
613 "Fourcc format (0x%08x) invalid.\n",
614 f->fmt.pix.pixelformat);
618 return vidioc_try_fmt(f, fmt);
621 static int vidioc_s_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
623 struct m2mtest_q_data *q_data;
624 struct vb2_queue *vq;
626 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
630 q_data = get_q_data(ctx, f->type);
634 if (vb2_is_busy(vq)) {
635 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
639 q_data->fmt = find_format(f);
640 q_data->width = f->fmt.pix.width;
641 q_data->height = f->fmt.pix.height;
642 q_data->sizeimage = q_data->width * q_data->height
643 * q_data->fmt->depth >> 3;
646 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
647 f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
652 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
653 struct v4l2_format *f)
657 ret = vidioc_try_fmt_vid_cap(file, priv, f);
661 return vidioc_s_fmt(priv, f);
664 static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
665 struct v4l2_format *f)
669 ret = vidioc_try_fmt_vid_out(file, priv, f);
673 return vidioc_s_fmt(priv, f);
676 static int vidioc_reqbufs(struct file *file, void *priv,
677 struct v4l2_requestbuffers *reqbufs)
679 struct m2mtest_ctx *ctx = priv;
681 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
684 static int vidioc_querybuf(struct file *file, void *priv,
685 struct v4l2_buffer *buf)
687 struct m2mtest_ctx *ctx = priv;
689 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
692 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
694 struct m2mtest_ctx *ctx = priv;
696 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
699 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
701 struct m2mtest_ctx *ctx = priv;
703 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
706 static int vidioc_streamon(struct file *file, void *priv,
707 enum v4l2_buf_type type)
709 struct m2mtest_ctx *ctx = priv;
711 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
714 static int vidioc_streamoff(struct file *file, void *priv,
715 enum v4l2_buf_type type)
717 struct m2mtest_ctx *ctx = priv;
719 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
722 static int vidioc_queryctrl(struct file *file, void *priv,
723 struct v4l2_queryctrl *qc)
725 struct v4l2_queryctrl *c;
727 c = get_ctrl(qc->id);
735 static int vidioc_g_ctrl(struct file *file, void *priv,
736 struct v4l2_control *ctrl)
738 struct m2mtest_ctx *ctx = priv;
742 ctrl->value = (ctx->mode & MEM2MEM_HFLIP) ? 1 : 0;
746 ctrl->value = (ctx->mode & MEM2MEM_VFLIP) ? 1 : 0;
749 case V4L2_CID_TRANS_TIME_MSEC:
750 ctrl->value = ctx->transtime;
753 case V4L2_CID_TRANS_NUM_BUFS:
754 ctrl->value = ctx->translen;
758 v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
765 static int check_ctrl_val(struct m2mtest_ctx *ctx, struct v4l2_control *ctrl)
767 struct v4l2_queryctrl *c;
769 c = get_ctrl(ctrl->id);
773 if (ctrl->value < c->minimum || ctrl->value > c->maximum) {
774 v4l2_err(&ctx->dev->v4l2_dev, "Value out of range\n");
781 static int vidioc_s_ctrl(struct file *file, void *priv,
782 struct v4l2_control *ctrl)
784 struct m2mtest_ctx *ctx = priv;
787 ret = check_ctrl_val(ctx, ctrl);
794 ctx->mode |= MEM2MEM_HFLIP;
796 ctx->mode &= ~MEM2MEM_HFLIP;
801 ctx->mode |= MEM2MEM_VFLIP;
803 ctx->mode &= ~MEM2MEM_VFLIP;
806 case V4L2_CID_TRANS_TIME_MSEC:
807 ctx->transtime = ctrl->value;
810 case V4L2_CID_TRANS_NUM_BUFS:
811 ctx->translen = ctrl->value;
815 v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
823 static const struct v4l2_ioctl_ops m2mtest_ioctl_ops = {
824 .vidioc_querycap = vidioc_querycap,
826 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
827 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
828 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
829 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
831 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
832 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
833 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
834 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
836 .vidioc_reqbufs = vidioc_reqbufs,
837 .vidioc_querybuf = vidioc_querybuf,
839 .vidioc_qbuf = vidioc_qbuf,
840 .vidioc_dqbuf = vidioc_dqbuf,
842 .vidioc_streamon = vidioc_streamon,
843 .vidioc_streamoff = vidioc_streamoff,
845 .vidioc_queryctrl = vidioc_queryctrl,
846 .vidioc_g_ctrl = vidioc_g_ctrl,
847 .vidioc_s_ctrl = vidioc_s_ctrl,
855 static int m2mtest_queue_setup(struct vb2_queue *vq,
856 const struct v4l2_format *fmt,
857 unsigned int *nbuffers, unsigned int *nplanes,
858 unsigned int sizes[], void *alloc_ctxs[])
860 struct m2mtest_ctx *ctx = vb2_get_drv_priv(vq);
861 struct m2mtest_q_data *q_data;
862 unsigned int size, count = *nbuffers;
864 q_data = get_q_data(ctx, vq->type);
866 size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
868 while (size * count > MEM2MEM_VID_MEM_LIMIT)
876 * videobuf2-vmalloc allocator is context-less so no need to set
880 dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
885 static int m2mtest_buf_prepare(struct vb2_buffer *vb)
887 struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
888 struct m2mtest_q_data *q_data;
890 dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
892 q_data = get_q_data(ctx, vb->vb2_queue->type);
894 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
895 dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n",
896 __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage);
900 vb2_set_plane_payload(vb, 0, q_data->sizeimage);
905 static void m2mtest_buf_queue(struct vb2_buffer *vb)
907 struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
908 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
911 static void m2mtest_wait_prepare(struct vb2_queue *q)
913 struct m2mtest_ctx *ctx = vb2_get_drv_priv(q);
917 static void m2mtest_wait_finish(struct vb2_queue *q)
919 struct m2mtest_ctx *ctx = vb2_get_drv_priv(q);
923 static struct vb2_ops m2mtest_qops = {
924 .queue_setup = m2mtest_queue_setup,
925 .buf_prepare = m2mtest_buf_prepare,
926 .buf_queue = m2mtest_buf_queue,
927 .wait_prepare = m2mtest_wait_prepare,
928 .wait_finish = m2mtest_wait_finish,
931 static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
933 struct m2mtest_ctx *ctx = priv;
936 memset(src_vq, 0, sizeof(*src_vq));
937 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
938 src_vq->io_modes = VB2_MMAP;
939 src_vq->drv_priv = ctx;
940 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
941 src_vq->ops = &m2mtest_qops;
942 src_vq->mem_ops = &vb2_vmalloc_memops;
944 ret = vb2_queue_init(src_vq);
948 memset(dst_vq, 0, sizeof(*dst_vq));
949 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
950 dst_vq->io_modes = VB2_MMAP;
951 dst_vq->drv_priv = ctx;
952 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
953 dst_vq->ops = &m2mtest_qops;
954 dst_vq->mem_ops = &vb2_vmalloc_memops;
956 return vb2_queue_init(dst_vq);
962 static int m2mtest_open(struct file *file)
964 struct m2mtest_dev *dev = video_drvdata(file);
965 struct m2mtest_ctx *ctx = NULL;
967 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
971 file->private_data = ctx;
973 ctx->translen = MEM2MEM_DEF_TRANSLEN;
974 ctx->transtime = MEM2MEM_DEF_TRANSTIME;
975 ctx->num_processed = 0;
978 ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
979 ctx->q_data[V4L2_M2M_DST].fmt = &formats[0];
981 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
983 if (IS_ERR(ctx->m2m_ctx)) {
984 int ret = PTR_ERR(ctx->m2m_ctx);
990 atomic_inc(&dev->num_inst);
992 dprintk(dev, "Created instance %p, m2m_ctx: %p\n", ctx, ctx->m2m_ctx);
997 static int m2mtest_release(struct file *file)
999 struct m2mtest_dev *dev = video_drvdata(file);
1000 struct m2mtest_ctx *ctx = file->private_data;
1002 dprintk(dev, "Releasing instance %p\n", ctx);
1004 v4l2_m2m_ctx_release(ctx->m2m_ctx);
1007 atomic_dec(&dev->num_inst);
1012 static unsigned int m2mtest_poll(struct file *file,
1013 struct poll_table_struct *wait)
1015 struct m2mtest_ctx *ctx = file->private_data;
1017 return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
1020 static int m2mtest_mmap(struct file *file, struct vm_area_struct *vma)
1022 struct m2mtest_ctx *ctx = file->private_data;
1024 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
1027 static const struct v4l2_file_operations m2mtest_fops = {
1028 .owner = THIS_MODULE,
1029 .open = m2mtest_open,
1030 .release = m2mtest_release,
1031 .poll = m2mtest_poll,
1032 .unlocked_ioctl = video_ioctl2,
1033 .mmap = m2mtest_mmap,
1036 static struct video_device m2mtest_videodev = {
1037 .name = MEM2MEM_NAME,
1038 .fops = &m2mtest_fops,
1039 .ioctl_ops = &m2mtest_ioctl_ops,
1041 .release = video_device_release,
1044 static struct v4l2_m2m_ops m2m_ops = {
1045 .device_run = device_run,
1046 .job_ready = job_ready,
1047 .job_abort = job_abort,
1048 .lock = m2mtest_lock,
1049 .unlock = m2mtest_unlock,
1052 static int m2mtest_probe(struct platform_device *pdev)
1054 struct m2mtest_dev *dev;
1055 struct video_device *vfd;
1058 dev = kzalloc(sizeof *dev, GFP_KERNEL);
1062 spin_lock_init(&dev->irqlock);
1064 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1068 atomic_set(&dev->num_inst, 0);
1069 mutex_init(&dev->dev_mutex);
1071 vfd = video_device_alloc();
1073 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
1078 *vfd = m2mtest_videodev;
1079 /* Locking in file operations other than ioctl should be done
1080 by the driver, not the V4L2 core.
1081 This driver needs auditing so that this flag can be removed. */
1082 set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
1083 vfd->lock = &dev->dev_mutex;
1085 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1087 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1091 video_set_drvdata(vfd, dev);
1092 snprintf(vfd->name, sizeof(vfd->name), "%s", m2mtest_videodev.name);
1094 v4l2_info(&dev->v4l2_dev, MEM2MEM_TEST_MODULE_NAME
1095 "Device registered as /dev/video%d\n", vfd->num);
1097 setup_timer(&dev->timer, device_isr, (long)dev);
1098 platform_set_drvdata(pdev, dev);
1100 dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
1101 if (IS_ERR(dev->m2m_dev)) {
1102 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1103 ret = PTR_ERR(dev->m2m_dev);
1109 v4l2_m2m_release(dev->m2m_dev);
1111 video_unregister_device(dev->vfd);
1113 video_device_release(vfd);
1115 v4l2_device_unregister(&dev->v4l2_dev);
1122 static int m2mtest_remove(struct platform_device *pdev)
1124 struct m2mtest_dev *dev =
1125 (struct m2mtest_dev *)platform_get_drvdata(pdev);
1127 v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_TEST_MODULE_NAME);
1128 v4l2_m2m_release(dev->m2m_dev);
1129 del_timer_sync(&dev->timer);
1130 video_unregister_device(dev->vfd);
1131 v4l2_device_unregister(&dev->v4l2_dev);
1137 static struct platform_driver m2mtest_pdrv = {
1138 .probe = m2mtest_probe,
1139 .remove = m2mtest_remove,
1141 .name = MEM2MEM_NAME,
1142 .owner = THIS_MODULE,
1146 static void __exit m2mtest_exit(void)
1148 platform_driver_unregister(&m2mtest_pdrv);
1149 platform_device_unregister(&m2mtest_pdev);
1152 static int __init m2mtest_init(void)
1156 ret = platform_device_register(&m2mtest_pdev);
1160 ret = platform_driver_register(&m2mtest_pdrv);
1162 platform_device_unregister(&m2mtest_pdev);
1167 module_init(m2mtest_init);
1168 module_exit(m2mtest_exit);