2 * A virtual v4l2-mem2mem example device.
4 * This is a virtual device driver for testing mem-to-mem videobuf framework.
5 * It simulates a device that uses memory buffers for both source and
6 * destination, processes the data and issues an "irq" (simulated by a delayed
8 * The device is capable of multi-instance, multi-buffer-per-transaction
9 * operation (via the mem2mem framework).
11 * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
12 * Pawel Osciak, <pawel@osciak.com>
13 * Marek Szyprowski, <m.szyprowski@samsung.com>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the
18 * License, or (at your option) any later version
20 #include <linux/module.h>
21 #include <linux/delay.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
26 #include <linux/platform_device.h>
27 #include <media/v4l2-mem2mem.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ioctl.h>
30 #include <media/v4l2-ctrls.h>
31 #include <media/v4l2-event.h>
32 #include <media/videobuf2-vmalloc.h>
34 MODULE_DESCRIPTION("Virtual device for mem2mem framework testing");
35 MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
36 MODULE_LICENSE("GPL");
37 MODULE_VERSION("0.1.1");
38 MODULE_ALIAS("mem2mem_testdev");
40 static unsigned debug
;
41 module_param(debug
, uint
, 0644);
42 MODULE_PARM_DESC(debug
, "activates debug info");
48 #define DIM_ALIGN_MASK 7 /* 8-byte alignment for line length */
50 /* Flags that indicate a format can be used for capture/output */
51 #define MEM2MEM_CAPTURE (1 << 0)
52 #define MEM2MEM_OUTPUT (1 << 1)
54 #define MEM2MEM_NAME "vim2m"
57 #define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME
58 /* In bytes, per queue */
59 #define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024)
61 /* Default transaction time in msec */
62 #define MEM2MEM_DEF_TRANSTIME 40
63 #define MEM2MEM_COLOR_STEP (0xff >> 4)
64 #define MEM2MEM_NUM_TILES 8
66 /* Flags that indicate processing mode */
67 #define MEM2MEM_HFLIP (1 << 0)
68 #define MEM2MEM_VFLIP (1 << 1)
70 #define dprintk(dev, fmt, arg...) \
71 v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
74 static void vim2m_dev_release(struct device
*dev
)
77 static struct platform_device vim2m_pdev
= {
79 .dev
.release
= vim2m_dev_release
,
85 /* Types the format can be used for */
89 static struct vim2m_fmt formats
[] = {
91 .fourcc
= V4L2_PIX_FMT_RGB565X
, /* rrrrrggg gggbbbbb */
93 /* Both capture and output format */
94 .types
= MEM2MEM_CAPTURE
| MEM2MEM_OUTPUT
,
97 .fourcc
= V4L2_PIX_FMT_YUYV
,
99 /* Output-only format */
100 .types
= MEM2MEM_OUTPUT
,
104 #define NUM_FORMATS ARRAY_SIZE(formats)
106 /* Per-queue, driver-specific private data */
107 struct vim2m_q_data
{
110 unsigned int sizeimage
;
111 unsigned int sequence
;
112 struct vim2m_fmt
*fmt
;
120 #define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000)
121 #define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001)
123 static struct vim2m_fmt
*find_format(struct v4l2_format
*f
)
125 struct vim2m_fmt
*fmt
;
128 for (k
= 0; k
< NUM_FORMATS
; k
++) {
130 if (fmt
->fourcc
== f
->fmt
.pix
.pixelformat
)
134 if (k
== NUM_FORMATS
)
141 struct v4l2_device v4l2_dev
;
142 struct video_device vfd
;
143 #ifdef CONFIG_MEDIA_CONTROLLER
144 struct media_device mdev
;
148 struct mutex dev_mutex
;
151 struct delayed_work work_run
;
153 struct v4l2_m2m_dev
*m2m_dev
;
158 struct vim2m_dev
*dev
;
160 struct v4l2_ctrl_handler hdl
;
162 /* Processed buffers in this transaction */
165 /* Transaction length (i.e. how many buffers per transaction) */
167 /* Transaction time (i.e. simulated processing time) in milliseconds */
170 /* Abort requested by m2m */
173 /* Processing mode */
176 enum v4l2_colorspace colorspace
;
177 enum v4l2_ycbcr_encoding ycbcr_enc
;
178 enum v4l2_xfer_func xfer_func
;
179 enum v4l2_quantization quant
;
181 /* Source and destination queue data */
182 struct vim2m_q_data q_data
[2];
185 static inline struct vim2m_ctx
*file2ctx(struct file
*file
)
187 return container_of(file
->private_data
, struct vim2m_ctx
, fh
);
190 static struct vim2m_q_data
*get_q_data(struct vim2m_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 int device_process(struct vim2m_ctx
*ctx
,
206 struct vb2_v4l2_buffer
*in_vb
,
207 struct vb2_v4l2_buffer
*out_vb
)
209 struct vim2m_dev
*dev
= ctx
->dev
;
210 struct vim2m_q_data
*q_data
;
213 int tile_w
, bytes_left
;
214 int width
, height
, bytesperline
;
216 q_data
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
218 width
= q_data
->width
;
219 height
= q_data
->height
;
220 bytesperline
= (q_data
->width
* q_data
->fmt
->depth
) >> 3;
222 p_in
= vb2_plane_vaddr(&in_vb
->vb2_buf
, 0);
223 p_out
= vb2_plane_vaddr(&out_vb
->vb2_buf
, 0);
224 if (!p_in
|| !p_out
) {
225 v4l2_err(&dev
->v4l2_dev
,
226 "Acquiring kernel pointers to buffers failed\n");
230 if (vb2_plane_size(&in_vb
->vb2_buf
, 0) >
231 vb2_plane_size(&out_vb
->vb2_buf
, 0)) {
232 v4l2_err(&dev
->v4l2_dev
, "Output buffer is too small\n");
236 tile_w
= (width
* (q_data
[V4L2_M2M_DST
].fmt
->depth
>> 3))
238 bytes_left
= bytesperline
- tile_w
* MEM2MEM_NUM_TILES
;
242 get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
)->sequence
++;
243 in_vb
->sequence
= q_data
->sequence
++;
244 out_vb
->vb2_buf
.timestamp
= in_vb
->vb2_buf
.timestamp
;
246 if (in_vb
->flags
& V4L2_BUF_FLAG_TIMECODE
)
247 out_vb
->timecode
= in_vb
->timecode
;
248 out_vb
->field
= in_vb
->field
;
249 out_vb
->flags
= in_vb
->flags
&
250 (V4L2_BUF_FLAG_TIMECODE
|
251 V4L2_BUF_FLAG_KEYFRAME
|
252 V4L2_BUF_FLAG_PFRAME
|
253 V4L2_BUF_FLAG_BFRAME
|
254 V4L2_BUF_FLAG_TSTAMP_SRC_MASK
);
257 case MEM2MEM_HFLIP
| MEM2MEM_VFLIP
:
258 p_out
+= bytesperline
* height
- bytes_left
;
259 for (y
= 0; y
< height
; ++y
) {
260 for (t
= 0; t
< MEM2MEM_NUM_TILES
; ++t
) {
262 for (x
= 0; x
< tile_w
; ++x
)
266 for (x
= 0; x
< tile_w
; ++x
)
278 for (y
= 0; y
< height
; ++y
) {
279 p_out
+= MEM2MEM_NUM_TILES
* tile_w
;
280 for (t
= 0; t
< MEM2MEM_NUM_TILES
; ++t
) {
282 for (x
= 0; x
< tile_w
; ++x
)
286 for (x
= 0; x
< tile_w
; ++x
)
293 p_out
+= bytesperline
;
298 p_out
+= bytesperline
* (height
- 1);
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
)
313 p_out
+= bytes_left
- 2 * bytesperline
;
318 for (y
= 0; y
< height
; ++y
) {
319 for (t
= 0; t
< MEM2MEM_NUM_TILES
; ++t
) {
321 for (x
= 0; x
< tile_w
; ++x
)
325 for (x
= 0; x
< tile_w
; ++x
)
344 * job_ready() - check whether an instance is ready to be scheduled to run
346 static int job_ready(void *priv
)
348 struct vim2m_ctx
*ctx
= priv
;
350 if (v4l2_m2m_num_src_bufs_ready(ctx
->fh
.m2m_ctx
) < ctx
->translen
351 || v4l2_m2m_num_dst_bufs_ready(ctx
->fh
.m2m_ctx
) < ctx
->translen
) {
352 dprintk(ctx
->dev
, "Not enough buffers available\n");
359 static void job_abort(void *priv
)
361 struct vim2m_ctx
*ctx
= priv
;
363 /* Will cancel the transaction in the next interrupt handler */
367 /* device_run() - prepares and starts the device
369 * This simulates all the immediate preparations required before starting
370 * a device. This will be called by the framework when it decides to schedule
371 * a particular instance.
373 static void device_run(void *priv
)
375 struct vim2m_ctx
*ctx
= priv
;
376 struct vim2m_dev
*dev
= ctx
->dev
;
377 struct vb2_v4l2_buffer
*src_buf
, *dst_buf
;
379 src_buf
= v4l2_m2m_next_src_buf(ctx
->fh
.m2m_ctx
);
380 dst_buf
= v4l2_m2m_next_dst_buf(ctx
->fh
.m2m_ctx
);
382 device_process(ctx
, src_buf
, dst_buf
);
384 /* Run delayed work, which simulates a hardware irq */
385 schedule_delayed_work(&dev
->work_run
, msecs_to_jiffies(ctx
->transtime
));
388 static void device_work(struct work_struct
*w
)
390 struct vim2m_dev
*vim2m_dev
=
391 container_of(w
, struct vim2m_dev
, work_run
.work
);
392 struct vim2m_ctx
*curr_ctx
;
393 struct vb2_v4l2_buffer
*src_vb
, *dst_vb
;
396 curr_ctx
= v4l2_m2m_get_curr_priv(vim2m_dev
->m2m_dev
);
398 if (NULL
== curr_ctx
) {
399 pr_err("Instance released before the end of transaction\n");
403 src_vb
= v4l2_m2m_src_buf_remove(curr_ctx
->fh
.m2m_ctx
);
404 dst_vb
= v4l2_m2m_dst_buf_remove(curr_ctx
->fh
.m2m_ctx
);
406 curr_ctx
->num_processed
++;
408 spin_lock_irqsave(&vim2m_dev
->irqlock
, flags
);
409 v4l2_m2m_buf_done(src_vb
, VB2_BUF_STATE_DONE
);
410 v4l2_m2m_buf_done(dst_vb
, VB2_BUF_STATE_DONE
);
411 spin_unlock_irqrestore(&vim2m_dev
->irqlock
, flags
);
413 if (curr_ctx
->num_processed
== curr_ctx
->translen
414 || curr_ctx
->aborting
) {
415 dprintk(curr_ctx
->dev
, "Finishing transaction\n");
416 curr_ctx
->num_processed
= 0;
417 v4l2_m2m_job_finish(vim2m_dev
->m2m_dev
, curr_ctx
->fh
.m2m_ctx
);
419 device_run(curr_ctx
);
426 static int vidioc_querycap(struct file
*file
, void *priv
,
427 struct v4l2_capability
*cap
)
429 strncpy(cap
->driver
, MEM2MEM_NAME
, sizeof(cap
->driver
) - 1);
430 strncpy(cap
->card
, MEM2MEM_NAME
, sizeof(cap
->card
) - 1);
431 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
),
432 "platform:%s", MEM2MEM_NAME
);
433 cap
->device_caps
= V4L2_CAP_VIDEO_M2M
| V4L2_CAP_STREAMING
;
434 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
438 static int enum_fmt(struct v4l2_fmtdesc
*f
, u32 type
)
441 struct vim2m_fmt
*fmt
;
445 for (i
= 0; i
< NUM_FORMATS
; ++i
) {
446 if (formats
[i
].types
& type
) {
447 /* index-th format of type type found ? */
450 /* Correct type but haven't reached our index yet,
451 * just increment per-type index */
456 if (i
< NUM_FORMATS
) {
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 vim2m_ctx
*ctx
, struct v4l2_format
*f
)
481 struct vb2_queue
*vq
;
482 struct vim2m_q_data
*q_data
;
484 vq
= v4l2_m2m_get_vq(ctx
->fh
.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
;
496 f
->fmt
.pix
.colorspace
= ctx
->colorspace
;
497 f
->fmt
.pix
.xfer_func
= ctx
->xfer_func
;
498 f
->fmt
.pix
.ycbcr_enc
= ctx
->ycbcr_enc
;
499 f
->fmt
.pix
.quantization
= ctx
->quant
;
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 vim2m_fmt
*fmt
)
518 /* V4L2 specification suggests the driver corrects the format struct
519 * if any of the dimensions is unsupported */
520 if (f
->fmt
.pix
.height
< MIN_H
)
521 f
->fmt
.pix
.height
= MIN_H
;
522 else if (f
->fmt
.pix
.height
> MAX_H
)
523 f
->fmt
.pix
.height
= MAX_H
;
525 if (f
->fmt
.pix
.width
< MIN_W
)
526 f
->fmt
.pix
.width
= MIN_W
;
527 else if (f
->fmt
.pix
.width
> MAX_W
)
528 f
->fmt
.pix
.width
= MAX_W
;
530 f
->fmt
.pix
.width
&= ~DIM_ALIGN_MASK
;
531 f
->fmt
.pix
.bytesperline
= (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
532 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
533 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
538 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
539 struct v4l2_format
*f
)
541 struct vim2m_fmt
*fmt
;
542 struct vim2m_ctx
*ctx
= file2ctx(file
);
544 fmt
= find_format(f
);
546 f
->fmt
.pix
.pixelformat
= formats
[0].fourcc
;
547 fmt
= find_format(f
);
549 if (!(fmt
->types
& MEM2MEM_CAPTURE
)) {
550 v4l2_err(&ctx
->dev
->v4l2_dev
,
551 "Fourcc format (0x%08x) invalid.\n",
552 f
->fmt
.pix
.pixelformat
);
555 f
->fmt
.pix
.colorspace
= ctx
->colorspace
;
556 f
->fmt
.pix
.xfer_func
= ctx
->xfer_func
;
557 f
->fmt
.pix
.ycbcr_enc
= ctx
->ycbcr_enc
;
558 f
->fmt
.pix
.quantization
= ctx
->quant
;
560 return vidioc_try_fmt(f
, fmt
);
563 static int vidioc_try_fmt_vid_out(struct file
*file
, void *priv
,
564 struct v4l2_format
*f
)
566 struct vim2m_fmt
*fmt
;
567 struct vim2m_ctx
*ctx
= file2ctx(file
);
569 fmt
= find_format(f
);
571 f
->fmt
.pix
.pixelformat
= formats
[0].fourcc
;
572 fmt
= find_format(f
);
574 if (!(fmt
->types
& MEM2MEM_OUTPUT
)) {
575 v4l2_err(&ctx
->dev
->v4l2_dev
,
576 "Fourcc format (0x%08x) invalid.\n",
577 f
->fmt
.pix
.pixelformat
);
580 if (!f
->fmt
.pix
.colorspace
)
581 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_REC709
;
583 return vidioc_try_fmt(f
, fmt
);
586 static int vidioc_s_fmt(struct vim2m_ctx
*ctx
, struct v4l2_format
*f
)
588 struct vim2m_q_data
*q_data
;
589 struct vb2_queue
*vq
;
591 vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, f
->type
);
595 q_data
= get_q_data(ctx
, f
->type
);
599 if (vb2_is_busy(vq
)) {
600 v4l2_err(&ctx
->dev
->v4l2_dev
, "%s queue busy\n", __func__
);
604 q_data
->fmt
= find_format(f
);
605 q_data
->width
= f
->fmt
.pix
.width
;
606 q_data
->height
= f
->fmt
.pix
.height
;
607 q_data
->sizeimage
= q_data
->width
* q_data
->height
608 * q_data
->fmt
->depth
>> 3;
611 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
612 f
->type
, q_data
->width
, q_data
->height
, q_data
->fmt
->fourcc
);
617 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
618 struct v4l2_format
*f
)
622 ret
= vidioc_try_fmt_vid_cap(file
, priv
, f
);
626 return vidioc_s_fmt(file2ctx(file
), f
);
629 static int vidioc_s_fmt_vid_out(struct file
*file
, void *priv
,
630 struct v4l2_format
*f
)
632 struct vim2m_ctx
*ctx
= file2ctx(file
);
635 ret
= vidioc_try_fmt_vid_out(file
, priv
, f
);
639 ret
= vidioc_s_fmt(file2ctx(file
), f
);
641 ctx
->colorspace
= f
->fmt
.pix
.colorspace
;
642 ctx
->xfer_func
= f
->fmt
.pix
.xfer_func
;
643 ctx
->ycbcr_enc
= f
->fmt
.pix
.ycbcr_enc
;
644 ctx
->quant
= f
->fmt
.pix
.quantization
;
649 static int vim2m_s_ctrl(struct v4l2_ctrl
*ctrl
)
651 struct vim2m_ctx
*ctx
=
652 container_of(ctrl
->handler
, struct vim2m_ctx
, hdl
);
657 ctx
->mode
|= MEM2MEM_HFLIP
;
659 ctx
->mode
&= ~MEM2MEM_HFLIP
;
664 ctx
->mode
|= MEM2MEM_VFLIP
;
666 ctx
->mode
&= ~MEM2MEM_VFLIP
;
669 case V4L2_CID_TRANS_TIME_MSEC
:
670 ctx
->transtime
= ctrl
->val
;
673 case V4L2_CID_TRANS_NUM_BUFS
:
674 ctx
->translen
= ctrl
->val
;
678 v4l2_err(&ctx
->dev
->v4l2_dev
, "Invalid control\n");
685 static const struct v4l2_ctrl_ops vim2m_ctrl_ops
= {
686 .s_ctrl
= vim2m_s_ctrl
,
690 static const struct v4l2_ioctl_ops vim2m_ioctl_ops
= {
691 .vidioc_querycap
= vidioc_querycap
,
693 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
694 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
695 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
696 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
698 .vidioc_enum_fmt_vid_out
= vidioc_enum_fmt_vid_out
,
699 .vidioc_g_fmt_vid_out
= vidioc_g_fmt_vid_out
,
700 .vidioc_try_fmt_vid_out
= vidioc_try_fmt_vid_out
,
701 .vidioc_s_fmt_vid_out
= vidioc_s_fmt_vid_out
,
703 .vidioc_reqbufs
= v4l2_m2m_ioctl_reqbufs
,
704 .vidioc_querybuf
= v4l2_m2m_ioctl_querybuf
,
705 .vidioc_qbuf
= v4l2_m2m_ioctl_qbuf
,
706 .vidioc_dqbuf
= v4l2_m2m_ioctl_dqbuf
,
707 .vidioc_prepare_buf
= v4l2_m2m_ioctl_prepare_buf
,
708 .vidioc_create_bufs
= v4l2_m2m_ioctl_create_bufs
,
709 .vidioc_expbuf
= v4l2_m2m_ioctl_expbuf
,
711 .vidioc_streamon
= v4l2_m2m_ioctl_streamon
,
712 .vidioc_streamoff
= v4l2_m2m_ioctl_streamoff
,
714 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
715 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
723 static int vim2m_queue_setup(struct vb2_queue
*vq
,
724 unsigned int *nbuffers
, unsigned int *nplanes
,
725 unsigned int sizes
[], struct device
*alloc_devs
[])
727 struct vim2m_ctx
*ctx
= vb2_get_drv_priv(vq
);
728 struct vim2m_q_data
*q_data
;
729 unsigned int size
, count
= *nbuffers
;
731 q_data
= get_q_data(ctx
, vq
->type
);
733 size
= q_data
->width
* q_data
->height
* q_data
->fmt
->depth
>> 3;
735 while (size
* count
> MEM2MEM_VID_MEM_LIMIT
)
740 return sizes
[0] < size
? -EINVAL
: 0;
745 dprintk(ctx
->dev
, "get %d buffer(s) of size %d each.\n", count
, size
);
750 static int vim2m_buf_prepare(struct vb2_buffer
*vb
)
752 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
753 struct vim2m_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
754 struct vim2m_q_data
*q_data
;
756 dprintk(ctx
->dev
, "type: %d\n", vb
->vb2_queue
->type
);
758 q_data
= get_q_data(ctx
, vb
->vb2_queue
->type
);
759 if (V4L2_TYPE_IS_OUTPUT(vb
->vb2_queue
->type
)) {
760 if (vbuf
->field
== V4L2_FIELD_ANY
)
761 vbuf
->field
= V4L2_FIELD_NONE
;
762 if (vbuf
->field
!= V4L2_FIELD_NONE
) {
763 dprintk(ctx
->dev
, "%s field isn't supported\n",
769 if (vb2_plane_size(vb
, 0) < q_data
->sizeimage
) {
770 dprintk(ctx
->dev
, "%s data will not fit into plane (%lu < %lu)\n",
771 __func__
, vb2_plane_size(vb
, 0), (long)q_data
->sizeimage
);
775 vb2_set_plane_payload(vb
, 0, q_data
->sizeimage
);
780 static void vim2m_buf_queue(struct vb2_buffer
*vb
)
782 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
783 struct vim2m_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
785 v4l2_m2m_buf_queue(ctx
->fh
.m2m_ctx
, vbuf
);
788 static int vim2m_start_streaming(struct vb2_queue
*q
, unsigned count
)
790 struct vim2m_ctx
*ctx
= vb2_get_drv_priv(q
);
791 struct vim2m_q_data
*q_data
= get_q_data(ctx
, q
->type
);
793 q_data
->sequence
= 0;
797 static void vim2m_stop_streaming(struct vb2_queue
*q
)
799 struct vim2m_ctx
*ctx
= vb2_get_drv_priv(q
);
800 struct vim2m_dev
*dev
= ctx
->dev
;
801 struct vb2_v4l2_buffer
*vbuf
;
804 if (v4l2_m2m_get_curr_priv(dev
->m2m_dev
) == ctx
)
805 cancel_delayed_work_sync(&dev
->work_run
);
808 if (V4L2_TYPE_IS_OUTPUT(q
->type
))
809 vbuf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
);
811 vbuf
= v4l2_m2m_dst_buf_remove(ctx
->fh
.m2m_ctx
);
814 spin_lock_irqsave(&ctx
->dev
->irqlock
, flags
);
815 v4l2_m2m_buf_done(vbuf
, VB2_BUF_STATE_ERROR
);
816 spin_unlock_irqrestore(&ctx
->dev
->irqlock
, flags
);
820 static const struct vb2_ops vim2m_qops
= {
821 .queue_setup
= vim2m_queue_setup
,
822 .buf_prepare
= vim2m_buf_prepare
,
823 .buf_queue
= vim2m_buf_queue
,
824 .start_streaming
= vim2m_start_streaming
,
825 .stop_streaming
= vim2m_stop_streaming
,
826 .wait_prepare
= vb2_ops_wait_prepare
,
827 .wait_finish
= vb2_ops_wait_finish
,
830 static int queue_init(void *priv
, struct vb2_queue
*src_vq
, struct vb2_queue
*dst_vq
)
832 struct vim2m_ctx
*ctx
= priv
;
835 src_vq
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
836 src_vq
->io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
;
837 src_vq
->drv_priv
= ctx
;
838 src_vq
->buf_struct_size
= sizeof(struct v4l2_m2m_buffer
);
839 src_vq
->ops
= &vim2m_qops
;
840 src_vq
->mem_ops
= &vb2_vmalloc_memops
;
841 src_vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
842 src_vq
->lock
= &ctx
->dev
->dev_mutex
;
844 ret
= vb2_queue_init(src_vq
);
848 dst_vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
849 dst_vq
->io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
;
850 dst_vq
->drv_priv
= ctx
;
851 dst_vq
->buf_struct_size
= sizeof(struct v4l2_m2m_buffer
);
852 dst_vq
->ops
= &vim2m_qops
;
853 dst_vq
->mem_ops
= &vb2_vmalloc_memops
;
854 dst_vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
855 dst_vq
->lock
= &ctx
->dev
->dev_mutex
;
857 return vb2_queue_init(dst_vq
);
860 static const struct v4l2_ctrl_config vim2m_ctrl_trans_time_msec
= {
861 .ops
= &vim2m_ctrl_ops
,
862 .id
= V4L2_CID_TRANS_TIME_MSEC
,
863 .name
= "Transaction Time (msec)",
864 .type
= V4L2_CTRL_TYPE_INTEGER
,
865 .def
= MEM2MEM_DEF_TRANSTIME
,
871 static const struct v4l2_ctrl_config vim2m_ctrl_trans_num_bufs
= {
872 .ops
= &vim2m_ctrl_ops
,
873 .id
= V4L2_CID_TRANS_NUM_BUFS
,
874 .name
= "Buffers Per Transaction",
875 .type
= V4L2_CTRL_TYPE_INTEGER
,
878 .max
= MEM2MEM_DEF_NUM_BUFS
,
885 static int vim2m_open(struct file
*file
)
887 struct vim2m_dev
*dev
= video_drvdata(file
);
888 struct vim2m_ctx
*ctx
= NULL
;
889 struct v4l2_ctrl_handler
*hdl
;
892 if (mutex_lock_interruptible(&dev
->dev_mutex
))
894 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
, &vim2m_ctrl_ops
, V4L2_CID_HFLIP
, 0, 1, 1, 0);
906 v4l2_ctrl_new_std(hdl
, &vim2m_ctrl_ops
, V4L2_CID_VFLIP
, 0, 1, 1, 0);
907 v4l2_ctrl_new_custom(hdl
, &vim2m_ctrl_trans_time_msec
, NULL
);
908 v4l2_ctrl_new_custom(hdl
, &vim2m_ctrl_trans_num_bufs
, NULL
);
911 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
->fh
.m2m_ctx
= v4l2_m2m_ctx_init(dev
->m2m_dev
, ctx
, &queue_init
);
930 if (IS_ERR(ctx
->fh
.m2m_ctx
)) {
931 rc
= PTR_ERR(ctx
->fh
.m2m_ctx
);
933 v4l2_ctrl_handler_free(hdl
);
934 v4l2_fh_exit(&ctx
->fh
);
939 v4l2_fh_add(&ctx
->fh
);
940 atomic_inc(&dev
->num_inst
);
942 dprintk(dev
, "Created instance: %p, m2m_ctx: %p\n",
943 ctx
, ctx
->fh
.m2m_ctx
);
946 mutex_unlock(&dev
->dev_mutex
);
950 static int vim2m_release(struct file
*file
)
952 struct vim2m_dev
*dev
= video_drvdata(file
);
953 struct vim2m_ctx
*ctx
= file2ctx(file
);
955 dprintk(dev
, "Releasing instance %p\n", ctx
);
957 v4l2_fh_del(&ctx
->fh
);
958 v4l2_fh_exit(&ctx
->fh
);
959 v4l2_ctrl_handler_free(&ctx
->hdl
);
960 mutex_lock(&dev
->dev_mutex
);
961 v4l2_m2m_ctx_release(ctx
->fh
.m2m_ctx
);
962 mutex_unlock(&dev
->dev_mutex
);
965 atomic_dec(&dev
->num_inst
);
970 static const struct v4l2_file_operations vim2m_fops
= {
971 .owner
= THIS_MODULE
,
973 .release
= vim2m_release
,
974 .poll
= v4l2_m2m_fop_poll
,
975 .unlocked_ioctl
= video_ioctl2
,
976 .mmap
= v4l2_m2m_fop_mmap
,
979 static const struct video_device vim2m_videodev
= {
980 .name
= MEM2MEM_NAME
,
981 .vfl_dir
= VFL_DIR_M2M
,
983 .ioctl_ops
= &vim2m_ioctl_ops
,
985 .release
= video_device_release_empty
,
988 static const struct v4l2_m2m_ops m2m_ops
= {
989 .device_run
= device_run
,
990 .job_ready
= job_ready
,
991 .job_abort
= job_abort
,
994 static int vim2m_probe(struct platform_device
*pdev
)
996 struct vim2m_dev
*dev
;
997 struct video_device
*vfd
;
1000 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
1004 spin_lock_init(&dev
->irqlock
);
1006 ret
= v4l2_device_register(&pdev
->dev
, &dev
->v4l2_dev
);
1010 atomic_set(&dev
->num_inst
, 0);
1011 mutex_init(&dev
->dev_mutex
);
1013 dev
->vfd
= vim2m_videodev
;
1015 vfd
->lock
= &dev
->dev_mutex
;
1016 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
1017 INIT_DELAYED_WORK(&dev
->work_run
, device_work
);
1019 ret
= video_register_device(vfd
, VFL_TYPE_GRABBER
, 0);
1021 v4l2_err(&dev
->v4l2_dev
, "Failed to register video device\n");
1025 video_set_drvdata(vfd
, dev
);
1026 v4l2_info(&dev
->v4l2_dev
,
1027 "Device registered as /dev/video%d\n", vfd
->num
);
1029 platform_set_drvdata(pdev
, dev
);
1031 dev
->m2m_dev
= v4l2_m2m_init(&m2m_ops
);
1032 if (IS_ERR(dev
->m2m_dev
)) {
1033 v4l2_err(&dev
->v4l2_dev
, "Failed to init mem2mem device\n");
1034 ret
= PTR_ERR(dev
->m2m_dev
);
1038 #ifdef CONFIG_MEDIA_CONTROLLER
1039 dev
->mdev
.dev
= &pdev
->dev
;
1040 strlcpy(dev
->mdev
.model
, "vim2m", sizeof(dev
->mdev
.model
));
1041 media_device_init(&dev
->mdev
);
1042 dev
->v4l2_dev
.mdev
= &dev
->mdev
;
1044 ret
= v4l2_m2m_register_media_controller(dev
->m2m_dev
,
1045 vfd
, MEDIA_ENT_F_PROC_VIDEO_SCALER
);
1047 v4l2_err(&dev
->v4l2_dev
, "Failed to init mem2mem media controller\n");
1051 ret
= media_device_register(&dev
->mdev
);
1053 v4l2_err(&dev
->v4l2_dev
, "Failed to register mem2mem media device\n");
1059 #ifdef CONFIG_MEDIA_CONTROLLER
1061 v4l2_m2m_unregister_media_controller(dev
->m2m_dev
);
1063 v4l2_m2m_release(dev
->m2m_dev
);
1066 video_unregister_device(&dev
->vfd
);
1068 v4l2_device_unregister(&dev
->v4l2_dev
);
1073 static int vim2m_remove(struct platform_device
*pdev
)
1075 struct vim2m_dev
*dev
= platform_get_drvdata(pdev
);
1077 v4l2_info(&dev
->v4l2_dev
, "Removing " MEM2MEM_NAME
);
1079 #ifdef CONFIG_MEDIA_CONTROLLER
1080 media_device_unregister(&dev
->mdev
);
1081 v4l2_m2m_unregister_media_controller(dev
->m2m_dev
);
1082 media_device_cleanup(&dev
->mdev
);
1084 v4l2_m2m_release(dev
->m2m_dev
);
1085 video_unregister_device(&dev
->vfd
);
1086 v4l2_device_unregister(&dev
->v4l2_dev
);
1091 static struct platform_driver vim2m_pdrv
= {
1092 .probe
= vim2m_probe
,
1093 .remove
= vim2m_remove
,
1095 .name
= MEM2MEM_NAME
,
1099 static void __exit
vim2m_exit(void)
1101 platform_driver_unregister(&vim2m_pdrv
);
1102 platform_device_unregister(&vim2m_pdev
);
1105 static int __init
vim2m_init(void)
1109 ret
= platform_device_register(&vim2m_pdev
);
1113 ret
= platform_driver_register(&vim2m_pdrv
);
1115 platform_device_unregister(&vim2m_pdev
);
1120 module_init(vim2m_init
);
1121 module_exit(vim2m_exit
);