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 #define dprintk(dev, fmt, arg...) \
64 v4l2_dbg(1, 1, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
67 void m2mtest_dev_release(struct device *dev)
70 static struct platform_device m2mtest_pdev = {
72 .dev.release = m2mtest_dev_release,
79 /* Types the format can be used for */
83 static struct m2mtest_fmt formats[] = {
85 .name = "RGB565 (BE)",
86 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
88 /* Both capture and output format */
89 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
92 .name = "4:2:2, packed, YUYV",
93 .fourcc = V4L2_PIX_FMT_YUYV,
95 /* Output-only format */
96 .types = MEM2MEM_OUTPUT,
100 /* Per-queue, driver-specific private data */
101 struct m2mtest_q_data {
104 unsigned int sizeimage;
105 struct m2mtest_fmt *fmt;
113 #define V4L2_CID_TRANS_TIME_MSEC V4L2_CID_PRIVATE_BASE
114 #define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_PRIVATE_BASE + 1)
116 static struct v4l2_queryctrl m2mtest_ctrls[] = {
118 .id = V4L2_CID_TRANS_TIME_MSEC,
119 .type = V4L2_CTRL_TYPE_INTEGER,
120 .name = "Transaction time (msec)",
124 .default_value = 1000,
127 .id = V4L2_CID_TRANS_NUM_BUFS,
128 .type = V4L2_CTRL_TYPE_INTEGER,
129 .name = "Buffers per transaction",
131 .maximum = MEM2MEM_DEF_NUM_BUFS,
138 #define NUM_FORMATS ARRAY_SIZE(formats)
140 static struct m2mtest_fmt *find_format(struct v4l2_format *f)
142 struct m2mtest_fmt *fmt;
145 for (k = 0; k < NUM_FORMATS; k++) {
147 if (fmt->fourcc == f->fmt.pix.pixelformat)
151 if (k == NUM_FORMATS)
158 struct v4l2_device v4l2_dev;
159 struct video_device *vfd;
162 struct mutex dev_mutex;
165 struct timer_list timer;
167 struct v4l2_m2m_dev *m2m_dev;
171 struct m2mtest_dev *dev;
173 /* Processed buffers in this transaction */
176 /* Transaction length (i.e. how many buffers per transaction) */
178 /* Transaction time (i.e. simulated processing time) in milliseconds */
181 /* Abort requested by m2m */
184 struct v4l2_m2m_ctx *m2m_ctx;
186 /* Source and destination queue data */
187 struct m2mtest_q_data q_data[2];
190 static struct m2mtest_q_data *get_q_data(struct m2mtest_ctx *ctx,
191 enum v4l2_buf_type type)
194 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
195 return &ctx->q_data[V4L2_M2M_SRC];
196 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
197 return &ctx->q_data[V4L2_M2M_DST];
205 static struct v4l2_queryctrl *get_ctrl(int id)
209 for (i = 0; i < ARRAY_SIZE(m2mtest_ctrls); ++i) {
210 if (id == m2mtest_ctrls[i].id)
211 return &m2mtest_ctrls[i];
217 static int device_process(struct m2mtest_ctx *ctx,
218 struct vb2_buffer *in_vb,
219 struct vb2_buffer *out_vb)
221 struct m2mtest_dev *dev = ctx->dev;
222 struct m2mtest_q_data *q_data;
225 int tile_w, bytes_left;
226 int width, height, bytesperline;
228 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
230 width = q_data->width;
231 height = q_data->height;
232 bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
234 p_in = vb2_plane_vaddr(in_vb, 0);
235 p_out = vb2_plane_vaddr(out_vb, 0);
236 if (!p_in || !p_out) {
237 v4l2_err(&dev->v4l2_dev,
238 "Acquiring kernel pointers to buffers failed\n");
242 if (vb2_plane_size(in_vb, 0) > vb2_plane_size(out_vb, 0)) {
243 v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n");
247 tile_w = (width * (q_data[V4L2_M2M_DST].fmt->depth >> 3))
249 bytes_left = bytesperline - tile_w * MEM2MEM_NUM_TILES;
252 for (y = 0; y < height; ++y) {
253 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
255 for (x = 0; x < tile_w; ++x)
256 *p_out++ = *p_in++ + MEM2MEM_COLOR_STEP;
258 for (x = 0; x < tile_w; ++x)
259 *p_out++ = *p_in++ - MEM2MEM_COLOR_STEP;
270 static void schedule_irq(struct m2mtest_dev *dev, int msec_timeout)
272 dprintk(dev, "Scheduling a simulated irq\n");
273 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(msec_timeout));
281 * job_ready() - check whether an instance is ready to be scheduled to run
283 static int job_ready(void *priv)
285 struct m2mtest_ctx *ctx = priv;
287 if (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) < ctx->translen
288 || v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx) < ctx->translen) {
289 dprintk(ctx->dev, "Not enough buffers available\n");
296 static void job_abort(void *priv)
298 struct m2mtest_ctx *ctx = priv;
300 /* Will cancel the transaction in the next interrupt handler */
304 static void m2mtest_lock(void *priv)
306 struct m2mtest_ctx *ctx = priv;
307 struct m2mtest_dev *dev = ctx->dev;
308 mutex_lock(&dev->dev_mutex);
311 static void m2mtest_unlock(void *priv)
313 struct m2mtest_ctx *ctx = priv;
314 struct m2mtest_dev *dev = ctx->dev;
315 mutex_unlock(&dev->dev_mutex);
319 /* device_run() - prepares and starts the device
321 * This simulates all the immediate preparations required before starting
322 * a device. This will be called by the framework when it decides to schedule
323 * a particular instance.
325 static void device_run(void *priv)
327 struct m2mtest_ctx *ctx = priv;
328 struct m2mtest_dev *dev = ctx->dev;
329 struct vb2_buffer *src_buf, *dst_buf;
331 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
332 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
334 device_process(ctx, src_buf, dst_buf);
336 /* Run a timer, which simulates a hardware irq */
337 schedule_irq(dev, ctx->transtime);
340 static void device_isr(unsigned long priv)
342 struct m2mtest_dev *m2mtest_dev = (struct m2mtest_dev *)priv;
343 struct m2mtest_ctx *curr_ctx;
344 struct vb2_buffer *src_vb, *dst_vb;
347 curr_ctx = v4l2_m2m_get_curr_priv(m2mtest_dev->m2m_dev);
349 if (NULL == curr_ctx) {
351 "Instance released before the end of transaction\n");
355 src_vb = v4l2_m2m_src_buf_remove(curr_ctx->m2m_ctx);
356 dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->m2m_ctx);
358 curr_ctx->num_processed++;
360 spin_lock_irqsave(&m2mtest_dev->irqlock, flags);
361 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
362 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
363 spin_unlock_irqrestore(&m2mtest_dev->irqlock, flags);
365 if (curr_ctx->num_processed == curr_ctx->translen
366 || curr_ctx->aborting) {
367 dprintk(curr_ctx->dev, "Finishing transaction\n");
368 curr_ctx->num_processed = 0;
369 v4l2_m2m_job_finish(m2mtest_dev->m2m_dev, curr_ctx->m2m_ctx);
371 device_run(curr_ctx);
378 static int vidioc_querycap(struct file *file, void *priv,
379 struct v4l2_capability *cap)
381 strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
382 strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
383 cap->bus_info[0] = 0;
384 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT
385 | V4L2_CAP_STREAMING;
390 static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
393 struct m2mtest_fmt *fmt;
397 for (i = 0; i < NUM_FORMATS; ++i) {
398 if (formats[i].types & type) {
399 /* index-th format of type type found ? */
402 /* Correct type but haven't reached our index yet,
403 * just increment per-type index */
408 if (i < NUM_FORMATS) {
411 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
412 f->pixelformat = fmt->fourcc;
416 /* Format not found */
420 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
421 struct v4l2_fmtdesc *f)
423 return enum_fmt(f, MEM2MEM_CAPTURE);
426 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
427 struct v4l2_fmtdesc *f)
429 return enum_fmt(f, MEM2MEM_OUTPUT);
432 static int vidioc_g_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
434 struct vb2_queue *vq;
435 struct m2mtest_q_data *q_data;
437 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
441 q_data = get_q_data(ctx, f->type);
443 f->fmt.pix.width = q_data->width;
444 f->fmt.pix.height = q_data->height;
445 f->fmt.pix.field = V4L2_FIELD_NONE;
446 f->fmt.pix.pixelformat = q_data->fmt->fourcc;
447 f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
448 f->fmt.pix.sizeimage = q_data->sizeimage;
453 static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
454 struct v4l2_format *f)
456 return vidioc_g_fmt(priv, f);
459 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
460 struct v4l2_format *f)
462 return vidioc_g_fmt(priv, f);
465 static int vidioc_try_fmt(struct v4l2_format *f, struct m2mtest_fmt *fmt)
467 enum v4l2_field field;
469 field = f->fmt.pix.field;
471 if (field == V4L2_FIELD_ANY)
472 field = V4L2_FIELD_NONE;
473 else if (V4L2_FIELD_NONE != field)
476 /* V4L2 specification suggests the driver corrects the format struct
477 * if any of the dimensions is unsupported */
478 f->fmt.pix.field = field;
480 if (f->fmt.pix.height < MIN_H)
481 f->fmt.pix.height = MIN_H;
482 else if (f->fmt.pix.height > MAX_H)
483 f->fmt.pix.height = MAX_H;
485 if (f->fmt.pix.width < MIN_W)
486 f->fmt.pix.width = MIN_W;
487 else if (f->fmt.pix.width > MAX_W)
488 f->fmt.pix.width = MAX_W;
490 f->fmt.pix.width &= ~DIM_ALIGN_MASK;
491 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
492 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
497 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
498 struct v4l2_format *f)
500 struct m2mtest_fmt *fmt;
501 struct m2mtest_ctx *ctx = priv;
503 fmt = find_format(f);
504 if (!fmt || !(fmt->types & MEM2MEM_CAPTURE)) {
505 v4l2_err(&ctx->dev->v4l2_dev,
506 "Fourcc format (0x%08x) invalid.\n",
507 f->fmt.pix.pixelformat);
511 return vidioc_try_fmt(f, fmt);
514 static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
515 struct v4l2_format *f)
517 struct m2mtest_fmt *fmt;
518 struct m2mtest_ctx *ctx = priv;
520 fmt = find_format(f);
521 if (!fmt || !(fmt->types & MEM2MEM_OUTPUT)) {
522 v4l2_err(&ctx->dev->v4l2_dev,
523 "Fourcc format (0x%08x) invalid.\n",
524 f->fmt.pix.pixelformat);
528 return vidioc_try_fmt(f, fmt);
531 static int vidioc_s_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
533 struct m2mtest_q_data *q_data;
534 struct vb2_queue *vq;
536 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
540 q_data = get_q_data(ctx, f->type);
544 if (vb2_is_busy(vq)) {
545 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
549 q_data->fmt = find_format(f);
550 q_data->width = f->fmt.pix.width;
551 q_data->height = f->fmt.pix.height;
552 q_data->sizeimage = q_data->width * q_data->height
553 * q_data->fmt->depth >> 3;
556 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
557 f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
562 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
563 struct v4l2_format *f)
567 ret = vidioc_try_fmt_vid_cap(file, priv, f);
571 return vidioc_s_fmt(priv, f);
574 static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
575 struct v4l2_format *f)
579 ret = vidioc_try_fmt_vid_out(file, priv, f);
583 return vidioc_s_fmt(priv, f);
586 static int vidioc_reqbufs(struct file *file, void *priv,
587 struct v4l2_requestbuffers *reqbufs)
589 struct m2mtest_ctx *ctx = priv;
591 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
594 static int vidioc_querybuf(struct file *file, void *priv,
595 struct v4l2_buffer *buf)
597 struct m2mtest_ctx *ctx = priv;
599 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
602 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
604 struct m2mtest_ctx *ctx = priv;
606 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
609 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
611 struct m2mtest_ctx *ctx = priv;
613 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
616 static int vidioc_streamon(struct file *file, void *priv,
617 enum v4l2_buf_type type)
619 struct m2mtest_ctx *ctx = priv;
621 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
624 static int vidioc_streamoff(struct file *file, void *priv,
625 enum v4l2_buf_type type)
627 struct m2mtest_ctx *ctx = priv;
629 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
632 static int vidioc_queryctrl(struct file *file, void *priv,
633 struct v4l2_queryctrl *qc)
635 struct v4l2_queryctrl *c;
637 c = get_ctrl(qc->id);
645 static int vidioc_g_ctrl(struct file *file, void *priv,
646 struct v4l2_control *ctrl)
648 struct m2mtest_ctx *ctx = priv;
651 case V4L2_CID_TRANS_TIME_MSEC:
652 ctrl->value = ctx->transtime;
655 case V4L2_CID_TRANS_NUM_BUFS:
656 ctrl->value = ctx->translen;
660 v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
667 static int check_ctrl_val(struct m2mtest_ctx *ctx, struct v4l2_control *ctrl)
669 struct v4l2_queryctrl *c;
671 c = get_ctrl(ctrl->id);
675 if (ctrl->value < c->minimum || ctrl->value > c->maximum) {
676 v4l2_err(&ctx->dev->v4l2_dev, "Value out of range\n");
683 static int vidioc_s_ctrl(struct file *file, void *priv,
684 struct v4l2_control *ctrl)
686 struct m2mtest_ctx *ctx = priv;
689 ret = check_ctrl_val(ctx, ctrl);
694 case V4L2_CID_TRANS_TIME_MSEC:
695 ctx->transtime = ctrl->value;
698 case V4L2_CID_TRANS_NUM_BUFS:
699 ctx->translen = ctrl->value;
703 v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
711 static const struct v4l2_ioctl_ops m2mtest_ioctl_ops = {
712 .vidioc_querycap = vidioc_querycap,
714 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
715 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
716 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
717 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
719 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
720 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
721 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
722 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
724 .vidioc_reqbufs = vidioc_reqbufs,
725 .vidioc_querybuf = vidioc_querybuf,
727 .vidioc_qbuf = vidioc_qbuf,
728 .vidioc_dqbuf = vidioc_dqbuf,
730 .vidioc_streamon = vidioc_streamon,
731 .vidioc_streamoff = vidioc_streamoff,
733 .vidioc_queryctrl = vidioc_queryctrl,
734 .vidioc_g_ctrl = vidioc_g_ctrl,
735 .vidioc_s_ctrl = vidioc_s_ctrl,
743 static int m2mtest_queue_setup(struct vb2_queue *vq,
744 const struct v4l2_format *fmt,
745 unsigned int *nbuffers, unsigned int *nplanes,
746 unsigned int sizes[], void *alloc_ctxs[])
748 struct m2mtest_ctx *ctx = vb2_get_drv_priv(vq);
749 struct m2mtest_q_data *q_data;
750 unsigned int size, count = *nbuffers;
752 q_data = get_q_data(ctx, vq->type);
754 size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
756 while (size * count > MEM2MEM_VID_MEM_LIMIT)
764 * videobuf2-vmalloc allocator is context-less so no need to set
768 dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
773 static int m2mtest_buf_prepare(struct vb2_buffer *vb)
775 struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
776 struct m2mtest_q_data *q_data;
778 dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
780 q_data = get_q_data(ctx, vb->vb2_queue->type);
782 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
783 dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n",
784 __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage);
788 vb2_set_plane_payload(vb, 0, q_data->sizeimage);
793 static void m2mtest_buf_queue(struct vb2_buffer *vb)
795 struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
796 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
799 static void m2mtest_wait_prepare(struct vb2_queue *q)
801 struct m2mtest_ctx *ctx = vb2_get_drv_priv(q);
805 static void m2mtest_wait_finish(struct vb2_queue *q)
807 struct m2mtest_ctx *ctx = vb2_get_drv_priv(q);
811 static struct vb2_ops m2mtest_qops = {
812 .queue_setup = m2mtest_queue_setup,
813 .buf_prepare = m2mtest_buf_prepare,
814 .buf_queue = m2mtest_buf_queue,
815 .wait_prepare = m2mtest_wait_prepare,
816 .wait_finish = m2mtest_wait_finish,
819 static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
821 struct m2mtest_ctx *ctx = priv;
824 memset(src_vq, 0, sizeof(*src_vq));
825 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
826 src_vq->io_modes = VB2_MMAP;
827 src_vq->drv_priv = ctx;
828 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
829 src_vq->ops = &m2mtest_qops;
830 src_vq->mem_ops = &vb2_vmalloc_memops;
832 ret = vb2_queue_init(src_vq);
836 memset(dst_vq, 0, sizeof(*dst_vq));
837 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
838 dst_vq->io_modes = VB2_MMAP;
839 dst_vq->drv_priv = ctx;
840 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
841 dst_vq->ops = &m2mtest_qops;
842 dst_vq->mem_ops = &vb2_vmalloc_memops;
844 return vb2_queue_init(dst_vq);
850 static int m2mtest_open(struct file *file)
852 struct m2mtest_dev *dev = video_drvdata(file);
853 struct m2mtest_ctx *ctx = NULL;
855 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
859 file->private_data = ctx;
861 ctx->translen = MEM2MEM_DEF_TRANSLEN;
862 ctx->transtime = MEM2MEM_DEF_TRANSTIME;
863 ctx->num_processed = 0;
865 ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
866 ctx->q_data[V4L2_M2M_DST].fmt = &formats[0];
868 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
870 if (IS_ERR(ctx->m2m_ctx)) {
871 int ret = PTR_ERR(ctx->m2m_ctx);
877 atomic_inc(&dev->num_inst);
879 dprintk(dev, "Created instance %p, m2m_ctx: %p\n", ctx, ctx->m2m_ctx);
884 static int m2mtest_release(struct file *file)
886 struct m2mtest_dev *dev = video_drvdata(file);
887 struct m2mtest_ctx *ctx = file->private_data;
889 dprintk(dev, "Releasing instance %p\n", ctx);
891 v4l2_m2m_ctx_release(ctx->m2m_ctx);
894 atomic_dec(&dev->num_inst);
899 static unsigned int m2mtest_poll(struct file *file,
900 struct poll_table_struct *wait)
902 struct m2mtest_ctx *ctx = file->private_data;
904 return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
907 static int m2mtest_mmap(struct file *file, struct vm_area_struct *vma)
909 struct m2mtest_ctx *ctx = file->private_data;
911 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
914 static const struct v4l2_file_operations m2mtest_fops = {
915 .owner = THIS_MODULE,
916 .open = m2mtest_open,
917 .release = m2mtest_release,
918 .poll = m2mtest_poll,
919 .unlocked_ioctl = video_ioctl2,
920 .mmap = m2mtest_mmap,
923 static struct video_device m2mtest_videodev = {
924 .name = MEM2MEM_NAME,
925 .fops = &m2mtest_fops,
926 .ioctl_ops = &m2mtest_ioctl_ops,
928 .release = video_device_release,
931 static struct v4l2_m2m_ops m2m_ops = {
932 .device_run = device_run,
933 .job_ready = job_ready,
934 .job_abort = job_abort,
935 .lock = m2mtest_lock,
936 .unlock = m2mtest_unlock,
939 static int m2mtest_probe(struct platform_device *pdev)
941 struct m2mtest_dev *dev;
942 struct video_device *vfd;
945 dev = kzalloc(sizeof *dev, GFP_KERNEL);
949 spin_lock_init(&dev->irqlock);
951 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
955 atomic_set(&dev->num_inst, 0);
956 mutex_init(&dev->dev_mutex);
958 vfd = video_device_alloc();
960 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
965 *vfd = m2mtest_videodev;
966 /* Locking in file operations other than ioctl should be done
967 by the driver, not the V4L2 core.
968 This driver needs auditing so that this flag can be removed. */
969 set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
970 vfd->lock = &dev->dev_mutex;
972 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
974 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
978 video_set_drvdata(vfd, dev);
979 snprintf(vfd->name, sizeof(vfd->name), "%s", m2mtest_videodev.name);
981 v4l2_info(&dev->v4l2_dev, MEM2MEM_TEST_MODULE_NAME
982 "Device registered as /dev/video%d\n", vfd->num);
984 setup_timer(&dev->timer, device_isr, (long)dev);
985 platform_set_drvdata(pdev, dev);
987 dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
988 if (IS_ERR(dev->m2m_dev)) {
989 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
990 ret = PTR_ERR(dev->m2m_dev);
996 v4l2_m2m_release(dev->m2m_dev);
998 video_unregister_device(dev->vfd);
1000 video_device_release(vfd);
1002 v4l2_device_unregister(&dev->v4l2_dev);
1009 static int m2mtest_remove(struct platform_device *pdev)
1011 struct m2mtest_dev *dev =
1012 (struct m2mtest_dev *)platform_get_drvdata(pdev);
1014 v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_TEST_MODULE_NAME);
1015 v4l2_m2m_release(dev->m2m_dev);
1016 del_timer_sync(&dev->timer);
1017 video_unregister_device(dev->vfd);
1018 v4l2_device_unregister(&dev->v4l2_dev);
1024 static struct platform_driver m2mtest_pdrv = {
1025 .probe = m2mtest_probe,
1026 .remove = m2mtest_remove,
1028 .name = MEM2MEM_NAME,
1029 .owner = THIS_MODULE,
1033 static void __exit m2mtest_exit(void)
1035 platform_driver_unregister(&m2mtest_pdrv);
1036 platform_device_unregister(&m2mtest_pdev);
1039 static int __init m2mtest_init(void)
1043 ret = platform_device_register(&m2mtest_pdev);
1047 ret = platform_driver_register(&m2mtest_pdrv);
1049 platform_device_unregister(&m2mtest_pdev);
1054 module_init(m2mtest_init);
1055 module_exit(m2mtest_exit);