Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1 | /* |
| 2 | * A virtual v4l2-mem2mem example device. |
| 3 | * |
| 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). |
| 9 | * |
| 10 | * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd. |
Pawel Osciak | 9507208 | 2011-03-13 15:23:32 -0300 | [diff] [blame] | 11 | * Pawel Osciak, <pawel@osciak.com> |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 12 | * Marek Szyprowski, <m.szyprowski@samsung.com> |
| 13 | * |
| 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 |
| 18 | */ |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/fs.h> |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 22 | #include <linux/timer.h> |
| 23 | #include <linux/sched.h> |
Randy Dunlap | 6b46c39 | 2010-05-07 15:22:26 -0300 | [diff] [blame] | 24 | #include <linux/slab.h> |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 25 | |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <media/v4l2-mem2mem.h> |
| 28 | #include <media/v4l2-device.h> |
| 29 | #include <media/v4l2-ioctl.h> |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 30 | #include <media/v4l2-ctrls.h> |
Hans Verkuil | 97a3c90 | 2012-07-18 10:54:59 -0300 | [diff] [blame] | 31 | #include <media/v4l2-event.h> |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 32 | #include <media/videobuf2-vmalloc.h> |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 33 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 34 | MODULE_DESCRIPTION("Virtual device for mem2mem framework testing"); |
Pawel Osciak | 9507208 | 2011-03-13 15:23:32 -0300 | [diff] [blame] | 35 | MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>"); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 36 | MODULE_LICENSE("GPL"); |
Mauro Carvalho Chehab | 1990d50 | 2011-06-24 14:45:49 -0300 | [diff] [blame] | 37 | MODULE_VERSION("0.1.1"); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 38 | MODULE_ALIAS("mem2mem_testdev"); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 39 | |
Hans Verkuil | d95d7c6 | 2013-04-17 03:04:10 -0300 | [diff] [blame] | 40 | static unsigned debug; |
| 41 | module_param(debug, uint, 0644); |
| 42 | MODULE_PARM_DESC(debug, "activates debug info"); |
| 43 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 44 | #define MIN_W 32 |
| 45 | #define MIN_H 32 |
| 46 | #define MAX_W 640 |
| 47 | #define MAX_H 480 |
Guennadi Liakhovetski | 9ce3ce4 | 2012-04-27 04:16:46 -0300 | [diff] [blame] | 48 | #define DIM_ALIGN_MASK 7 /* 8-byte alignment for line length */ |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 49 | |
| 50 | /* Flags that indicate a format can be used for capture/output */ |
| 51 | #define MEM2MEM_CAPTURE (1 << 0) |
| 52 | #define MEM2MEM_OUTPUT (1 << 1) |
| 53 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 54 | #define MEM2MEM_NAME "vim2m" |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 55 | |
| 56 | /* Per queue */ |
| 57 | #define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME |
| 58 | /* In bytes, per queue */ |
| 59 | #define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024) |
| 60 | |
| 61 | /* Default transaction time in msec */ |
Hans Verkuil | 4340583 | 2014-03-10 10:58:23 -0300 | [diff] [blame] | 62 | #define MEM2MEM_DEF_TRANSTIME 40 |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 63 | #define MEM2MEM_COLOR_STEP (0xff >> 4) |
| 64 | #define MEM2MEM_NUM_TILES 8 |
| 65 | |
Tomasz Moń | 8007287 | 2012-06-12 06:43:49 -0300 | [diff] [blame] | 66 | /* Flags that indicate processing mode */ |
| 67 | #define MEM2MEM_HFLIP (1 << 0) |
| 68 | #define MEM2MEM_VFLIP (1 << 1) |
| 69 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 70 | #define dprintk(dev, fmt, arg...) \ |
Hans Verkuil | d95d7c6 | 2013-04-17 03:04:10 -0300 | [diff] [blame] | 71 | v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 72 | |
| 73 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 74 | static void vim2m_dev_release(struct device *dev) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 75 | {} |
| 76 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 77 | static struct platform_device vim2m_pdev = { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 78 | .name = MEM2MEM_NAME, |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 79 | .dev.release = vim2m_dev_release, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 80 | }; |
| 81 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 82 | struct vim2m_fmt { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 83 | u32 fourcc; |
| 84 | int depth; |
| 85 | /* Types the format can be used for */ |
| 86 | u32 types; |
| 87 | }; |
| 88 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 89 | static struct vim2m_fmt formats[] = { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 90 | { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 91 | .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */ |
| 92 | .depth = 16, |
| 93 | /* Both capture and output format */ |
| 94 | .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT, |
| 95 | }, |
| 96 | { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 97 | .fourcc = V4L2_PIX_FMT_YUYV, |
| 98 | .depth = 16, |
| 99 | /* Output-only format */ |
| 100 | .types = MEM2MEM_OUTPUT, |
| 101 | }, |
| 102 | }; |
| 103 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 104 | #define NUM_FORMATS ARRAY_SIZE(formats) |
| 105 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 106 | /* Per-queue, driver-specific private data */ |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 107 | struct vim2m_q_data { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 108 | unsigned int width; |
| 109 | unsigned int height; |
| 110 | unsigned int sizeimage; |
Hans Verkuil | ca5f5fd | 2014-03-10 10:58:28 -0300 | [diff] [blame] | 111 | unsigned int sequence; |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 112 | struct vim2m_fmt *fmt; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | enum { |
| 116 | V4L2_M2M_SRC = 0, |
| 117 | V4L2_M2M_DST = 1, |
| 118 | }; |
| 119 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 120 | #define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000) |
| 121 | #define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 122 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 123 | static struct vim2m_fmt *find_format(struct v4l2_format *f) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 124 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 125 | struct vim2m_fmt *fmt; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 126 | unsigned int k; |
| 127 | |
| 128 | for (k = 0; k < NUM_FORMATS; k++) { |
| 129 | fmt = &formats[k]; |
| 130 | if (fmt->fourcc == f->fmt.pix.pixelformat) |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | if (k == NUM_FORMATS) |
| 135 | return NULL; |
| 136 | |
| 137 | return &formats[k]; |
| 138 | } |
| 139 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 140 | struct vim2m_dev { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 141 | struct v4l2_device v4l2_dev; |
Hans Verkuil | 0936542 | 2015-03-09 13:33:56 -0300 | [diff] [blame] | 142 | struct video_device vfd; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 143 | |
| 144 | atomic_t num_inst; |
| 145 | struct mutex dev_mutex; |
| 146 | spinlock_t irqlock; |
| 147 | |
| 148 | struct timer_list timer; |
| 149 | |
| 150 | struct v4l2_m2m_dev *m2m_dev; |
| 151 | }; |
| 152 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 153 | struct vim2m_ctx { |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 154 | struct v4l2_fh fh; |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 155 | struct vim2m_dev *dev; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 156 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 157 | struct v4l2_ctrl_handler hdl; |
| 158 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 159 | /* Processed buffers in this transaction */ |
| 160 | u8 num_processed; |
| 161 | |
| 162 | /* Transaction length (i.e. how many buffers per transaction) */ |
| 163 | u32 translen; |
| 164 | /* Transaction time (i.e. simulated processing time) in milliseconds */ |
| 165 | u32 transtime; |
| 166 | |
| 167 | /* Abort requested by m2m */ |
| 168 | int aborting; |
| 169 | |
Tomasz Moń | 8007287 | 2012-06-12 06:43:49 -0300 | [diff] [blame] | 170 | /* Processing mode */ |
| 171 | int mode; |
| 172 | |
Hans Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 173 | enum v4l2_colorspace colorspace; |
| 174 | |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 175 | /* Source and destination queue data */ |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 176 | struct vim2m_q_data q_data[2]; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 177 | }; |
| 178 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 179 | static inline struct vim2m_ctx *file2ctx(struct file *file) |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 180 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 181 | return container_of(file->private_data, struct vim2m_ctx, fh); |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 182 | } |
| 183 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 184 | static struct vim2m_q_data *get_q_data(struct vim2m_ctx *ctx, |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 185 | enum v4l2_buf_type type) |
| 186 | { |
| 187 | switch (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]; |
| 192 | default: |
| 193 | BUG(); |
| 194 | } |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 199 | static int device_process(struct vim2m_ctx *ctx, |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 200 | struct vb2_v4l2_buffer *in_vb, |
| 201 | struct vb2_v4l2_buffer *out_vb) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 202 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 203 | struct vim2m_dev *dev = ctx->dev; |
| 204 | struct vim2m_q_data *q_data; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 205 | u8 *p_in, *p_out; |
| 206 | int x, y, t, w; |
| 207 | int tile_w, bytes_left; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 208 | int width, height, bytesperline; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 209 | |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 210 | q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 211 | |
| 212 | width = q_data->width; |
| 213 | height = q_data->height; |
| 214 | bytesperline = (q_data->width * q_data->fmt->depth) >> 3; |
| 215 | |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 216 | p_in = vb2_plane_vaddr(&in_vb->vb2_buf, 0); |
| 217 | p_out = vb2_plane_vaddr(&out_vb->vb2_buf, 0); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 218 | if (!p_in || !p_out) { |
| 219 | v4l2_err(&dev->v4l2_dev, |
| 220 | "Acquiring kernel pointers to buffers failed\n"); |
| 221 | return -EFAULT; |
| 222 | } |
| 223 | |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 224 | if (vb2_plane_size(&in_vb->vb2_buf, 0) > |
| 225 | vb2_plane_size(&out_vb->vb2_buf, 0)) { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 226 | v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n"); |
| 227 | return -EINVAL; |
| 228 | } |
| 229 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 230 | tile_w = (width * (q_data[V4L2_M2M_DST].fmt->depth >> 3)) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 231 | / MEM2MEM_NUM_TILES; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 232 | bytes_left = bytesperline - tile_w * MEM2MEM_NUM_TILES; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 233 | w = 0; |
| 234 | |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 235 | out_vb->sequence = |
| 236 | get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE)->sequence++; |
| 237 | in_vb->sequence = q_data->sequence++; |
Junghak Sung | d6dd645 | 2015-11-03 08:16:37 -0200 | [diff] [blame] | 238 | out_vb->vb2_buf.timestamp = in_vb->vb2_buf.timestamp; |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 239 | |
| 240 | if (in_vb->flags & V4L2_BUF_FLAG_TIMECODE) |
| 241 | out_vb->timecode = in_vb->timecode; |
| 242 | out_vb->field = in_vb->field; |
| 243 | out_vb->flags = in_vb->flags & |
Hans Verkuil | ca5f5fd | 2014-03-10 10:58:28 -0300 | [diff] [blame] | 244 | (V4L2_BUF_FLAG_TIMECODE | |
| 245 | V4L2_BUF_FLAG_KEYFRAME | |
| 246 | V4L2_BUF_FLAG_PFRAME | |
| 247 | V4L2_BUF_FLAG_BFRAME | |
| 248 | V4L2_BUF_FLAG_TSTAMP_SRC_MASK); |
Hans Verkuil | d95d7c6 | 2013-04-17 03:04:10 -0300 | [diff] [blame] | 249 | |
Tomasz Moń | 8007287 | 2012-06-12 06:43:49 -0300 | [diff] [blame] | 250 | switch (ctx->mode) { |
| 251 | case MEM2MEM_HFLIP | MEM2MEM_VFLIP: |
| 252 | p_out += bytesperline * height - bytes_left; |
| 253 | for (y = 0; y < height; ++y) { |
| 254 | for (t = 0; t < MEM2MEM_NUM_TILES; ++t) { |
| 255 | if (w & 0x1) { |
| 256 | for (x = 0; x < tile_w; ++x) |
| 257 | *--p_out = *p_in++ + |
| 258 | MEM2MEM_COLOR_STEP; |
| 259 | } else { |
| 260 | for (x = 0; x < tile_w; ++x) |
| 261 | *--p_out = *p_in++ - |
| 262 | MEM2MEM_COLOR_STEP; |
| 263 | } |
| 264 | ++w; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 265 | } |
Tomasz Moń | 8007287 | 2012-06-12 06:43:49 -0300 | [diff] [blame] | 266 | p_in += bytes_left; |
| 267 | p_out -= bytes_left; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 268 | } |
Tomasz Moń | 8007287 | 2012-06-12 06:43:49 -0300 | [diff] [blame] | 269 | break; |
| 270 | |
| 271 | case MEM2MEM_HFLIP: |
| 272 | for (y = 0; y < height; ++y) { |
| 273 | p_out += MEM2MEM_NUM_TILES * tile_w; |
| 274 | for (t = 0; t < MEM2MEM_NUM_TILES; ++t) { |
| 275 | if (w & 0x01) { |
| 276 | for (x = 0; x < tile_w; ++x) |
| 277 | *--p_out = *p_in++ + |
| 278 | MEM2MEM_COLOR_STEP; |
| 279 | } else { |
| 280 | for (x = 0; x < tile_w; ++x) |
| 281 | *--p_out = *p_in++ - |
| 282 | MEM2MEM_COLOR_STEP; |
| 283 | } |
| 284 | ++w; |
| 285 | } |
| 286 | p_in += bytes_left; |
| 287 | p_out += bytesperline; |
| 288 | } |
| 289 | break; |
| 290 | |
| 291 | case MEM2MEM_VFLIP: |
| 292 | p_out += bytesperline * (height - 1); |
| 293 | for (y = 0; y < height; ++y) { |
| 294 | for (t = 0; t < MEM2MEM_NUM_TILES; ++t) { |
| 295 | if (w & 0x1) { |
| 296 | for (x = 0; x < tile_w; ++x) |
| 297 | *p_out++ = *p_in++ + |
| 298 | MEM2MEM_COLOR_STEP; |
| 299 | } else { |
| 300 | for (x = 0; x < tile_w; ++x) |
| 301 | *p_out++ = *p_in++ - |
| 302 | MEM2MEM_COLOR_STEP; |
| 303 | } |
| 304 | ++w; |
| 305 | } |
| 306 | p_in += bytes_left; |
| 307 | p_out += bytes_left - 2 * bytesperline; |
| 308 | } |
| 309 | break; |
| 310 | |
| 311 | default: |
| 312 | for (y = 0; y < height; ++y) { |
| 313 | for (t = 0; t < MEM2MEM_NUM_TILES; ++t) { |
| 314 | if (w & 0x1) { |
| 315 | for (x = 0; x < tile_w; ++x) |
| 316 | *p_out++ = *p_in++ + |
| 317 | MEM2MEM_COLOR_STEP; |
| 318 | } else { |
| 319 | for (x = 0; x < tile_w; ++x) |
| 320 | *p_out++ = *p_in++ - |
| 321 | MEM2MEM_COLOR_STEP; |
| 322 | } |
| 323 | ++w; |
| 324 | } |
| 325 | p_in += bytes_left; |
| 326 | p_out += bytes_left; |
| 327 | } |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 333 | static void schedule_irq(struct vim2m_dev *dev, int msec_timeout) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 334 | { |
| 335 | dprintk(dev, "Scheduling a simulated irq\n"); |
| 336 | mod_timer(&dev->timer, jiffies + msecs_to_jiffies(msec_timeout)); |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * mem2mem callbacks |
| 341 | */ |
| 342 | |
| 343 | /** |
| 344 | * job_ready() - check whether an instance is ready to be scheduled to run |
| 345 | */ |
| 346 | static int job_ready(void *priv) |
| 347 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 348 | struct vim2m_ctx *ctx = priv; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 349 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 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) { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 352 | dprintk(ctx->dev, "Not enough buffers available\n"); |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | return 1; |
| 357 | } |
| 358 | |
| 359 | static void job_abort(void *priv) |
| 360 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 361 | struct vim2m_ctx *ctx = priv; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 362 | |
| 363 | /* Will cancel the transaction in the next interrupt handler */ |
| 364 | ctx->aborting = 1; |
| 365 | } |
| 366 | |
| 367 | /* device_run() - prepares and starts the device |
| 368 | * |
| 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. |
| 372 | */ |
| 373 | static void device_run(void *priv) |
| 374 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 375 | struct vim2m_ctx *ctx = priv; |
| 376 | struct vim2m_dev *dev = ctx->dev; |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 377 | struct vb2_v4l2_buffer *src_buf, *dst_buf; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 378 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 379 | src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); |
| 380 | dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 381 | |
| 382 | device_process(ctx, src_buf, dst_buf); |
| 383 | |
| 384 | /* Run a timer, which simulates a hardware irq */ |
| 385 | schedule_irq(dev, ctx->transtime); |
| 386 | } |
| 387 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 388 | static void device_isr(unsigned long priv) |
| 389 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 390 | struct vim2m_dev *vim2m_dev = (struct vim2m_dev *)priv; |
| 391 | struct vim2m_ctx *curr_ctx; |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 392 | struct vb2_v4l2_buffer *src_vb, *dst_vb; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 393 | unsigned long flags; |
| 394 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 395 | curr_ctx = v4l2_m2m_get_curr_priv(vim2m_dev->m2m_dev); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 396 | |
| 397 | if (NULL == curr_ctx) { |
Sachin Kamat | 26fdcf0 | 2012-09-24 02:17:47 -0300 | [diff] [blame] | 398 | pr_err("Instance released before the end of transaction\n"); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 399 | return; |
| 400 | } |
| 401 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 402 | src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx); |
| 403 | dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx); |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 404 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 405 | curr_ctx->num_processed++; |
| 406 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 407 | spin_lock_irqsave(&vim2m_dev->irqlock, flags); |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 408 | v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE); |
| 409 | v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 410 | spin_unlock_irqrestore(&vim2m_dev->irqlock, flags); |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 411 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 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; |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 416 | v4l2_m2m_job_finish(vim2m_dev->m2m_dev, curr_ctx->fh.m2m_ctx); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 417 | } else { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 418 | device_run(curr_ctx); |
| 419 | } |
| 420 | } |
| 421 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 422 | /* |
| 423 | * video ioctls |
| 424 | */ |
| 425 | static int vidioc_querycap(struct file *file, void *priv, |
| 426 | struct v4l2_capability *cap) |
| 427 | { |
| 428 | strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1); |
| 429 | strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1); |
Hans Verkuil | 72c2af6 | 2012-09-14 06:23:12 -0300 | [diff] [blame] | 430 | snprintf(cap->bus_info, sizeof(cap->bus_info), |
| 431 | "platform:%s", MEM2MEM_NAME); |
Mauro Carvalho Chehab | 79e8c7b | 2012-08-24 11:25:10 -0300 | [diff] [blame] | 432 | cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING; |
Hans Verkuil | b0d1499 | 2012-07-18 10:46:01 -0300 | [diff] [blame] | 433 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | static int enum_fmt(struct v4l2_fmtdesc *f, u32 type) |
| 438 | { |
| 439 | int i, num; |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 440 | struct vim2m_fmt *fmt; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 441 | |
| 442 | num = 0; |
| 443 | |
| 444 | for (i = 0; i < NUM_FORMATS; ++i) { |
| 445 | if (formats[i].types & type) { |
| 446 | /* index-th format of type type found ? */ |
| 447 | if (num == f->index) |
| 448 | break; |
| 449 | /* Correct type but haven't reached our index yet, |
| 450 | * just increment per-type index */ |
| 451 | ++num; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | if (i < NUM_FORMATS) { |
| 456 | /* Format found */ |
| 457 | fmt = &formats[i]; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 458 | f->pixelformat = fmt->fourcc; |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | /* Format not found */ |
| 463 | return -EINVAL; |
| 464 | } |
| 465 | |
| 466 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, |
| 467 | struct v4l2_fmtdesc *f) |
| 468 | { |
| 469 | return enum_fmt(f, MEM2MEM_CAPTURE); |
| 470 | } |
| 471 | |
| 472 | static int vidioc_enum_fmt_vid_out(struct file *file, void *priv, |
| 473 | struct v4l2_fmtdesc *f) |
| 474 | { |
| 475 | return enum_fmt(f, MEM2MEM_OUTPUT); |
| 476 | } |
| 477 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 478 | static int vidioc_g_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 479 | { |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 480 | struct vb2_queue *vq; |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 481 | struct vim2m_q_data *q_data; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 482 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 483 | vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 484 | if (!vq) |
| 485 | return -EINVAL; |
| 486 | |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 487 | q_data = get_q_data(ctx, f->type); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 488 | |
| 489 | f->fmt.pix.width = q_data->width; |
| 490 | f->fmt.pix.height = q_data->height; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 491 | f->fmt.pix.field = V4L2_FIELD_NONE; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 492 | f->fmt.pix.pixelformat = q_data->fmt->fourcc; |
| 493 | f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3; |
| 494 | f->fmt.pix.sizeimage = q_data->sizeimage; |
Hans Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 495 | f->fmt.pix.colorspace = ctx->colorspace; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | static int vidioc_g_fmt_vid_out(struct file *file, void *priv, |
| 501 | struct v4l2_format *f) |
| 502 | { |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 503 | return vidioc_g_fmt(file2ctx(file), f); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, |
| 507 | struct v4l2_format *f) |
| 508 | { |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 509 | return vidioc_g_fmt(file2ctx(file), f); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 510 | } |
| 511 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 512 | static int vidioc_try_fmt(struct v4l2_format *f, struct vim2m_fmt *fmt) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 513 | { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 514 | /* V4L2 specification suggests the driver corrects the format struct |
| 515 | * if any of the dimensions is unsupported */ |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 516 | if (f->fmt.pix.height < MIN_H) |
| 517 | f->fmt.pix.height = MIN_H; |
| 518 | else if (f->fmt.pix.height > MAX_H) |
| 519 | f->fmt.pix.height = MAX_H; |
| 520 | |
| 521 | if (f->fmt.pix.width < MIN_W) |
| 522 | f->fmt.pix.width = MIN_W; |
| 523 | else if (f->fmt.pix.width > MAX_W) |
| 524 | f->fmt.pix.width = MAX_W; |
| 525 | |
| 526 | f->fmt.pix.width &= ~DIM_ALIGN_MASK; |
| 527 | f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3; |
| 528 | f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline; |
Hans Verkuil | 5c3112b | 2014-03-10 10:58:29 -0300 | [diff] [blame] | 529 | f->fmt.pix.field = V4L2_FIELD_NONE; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, |
| 535 | struct v4l2_format *f) |
| 536 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 537 | struct vim2m_fmt *fmt; |
| 538 | struct vim2m_ctx *ctx = file2ctx(file); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 539 | |
| 540 | fmt = find_format(f); |
Hans Verkuil | 4e8ec0a | 2014-03-10 10:58:24 -0300 | [diff] [blame] | 541 | if (!fmt) { |
| 542 | f->fmt.pix.pixelformat = formats[0].fourcc; |
| 543 | fmt = find_format(f); |
| 544 | } |
| 545 | if (!(fmt->types & MEM2MEM_CAPTURE)) { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 546 | v4l2_err(&ctx->dev->v4l2_dev, |
| 547 | "Fourcc format (0x%08x) invalid.\n", |
| 548 | f->fmt.pix.pixelformat); |
| 549 | return -EINVAL; |
| 550 | } |
Hans Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 551 | f->fmt.pix.colorspace = ctx->colorspace; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 552 | |
| 553 | return vidioc_try_fmt(f, fmt); |
| 554 | } |
| 555 | |
| 556 | static int vidioc_try_fmt_vid_out(struct file *file, void *priv, |
| 557 | struct v4l2_format *f) |
| 558 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 559 | struct vim2m_fmt *fmt; |
| 560 | struct vim2m_ctx *ctx = file2ctx(file); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 561 | |
| 562 | fmt = find_format(f); |
Hans Verkuil | 4e8ec0a | 2014-03-10 10:58:24 -0300 | [diff] [blame] | 563 | if (!fmt) { |
| 564 | f->fmt.pix.pixelformat = formats[0].fourcc; |
| 565 | fmt = find_format(f); |
| 566 | } |
| 567 | if (!(fmt->types & MEM2MEM_OUTPUT)) { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 568 | v4l2_err(&ctx->dev->v4l2_dev, |
| 569 | "Fourcc format (0x%08x) invalid.\n", |
| 570 | f->fmt.pix.pixelformat); |
| 571 | return -EINVAL; |
| 572 | } |
Hans Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 573 | if (!f->fmt.pix.colorspace) |
| 574 | f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 575 | |
| 576 | return vidioc_try_fmt(f, fmt); |
| 577 | } |
| 578 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 579 | static int vidioc_s_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 580 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 581 | struct vim2m_q_data *q_data; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 582 | struct vb2_queue *vq; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 583 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 584 | vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 585 | if (!vq) |
| 586 | return -EINVAL; |
| 587 | |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 588 | q_data = get_q_data(ctx, f->type); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 589 | if (!q_data) |
| 590 | return -EINVAL; |
| 591 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 592 | if (vb2_is_busy(vq)) { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 593 | v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__); |
Marek Szyprowski | 07e8030 | 2010-12-20 14:39:25 -0300 | [diff] [blame] | 594 | return -EBUSY; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | q_data->fmt = find_format(f); |
| 598 | q_data->width = f->fmt.pix.width; |
| 599 | q_data->height = f->fmt.pix.height; |
| 600 | q_data->sizeimage = q_data->width * q_data->height |
| 601 | * q_data->fmt->depth >> 3; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 602 | |
| 603 | dprintk(ctx->dev, |
| 604 | "Setting format for type %d, wxh: %dx%d, fmt: %d\n", |
| 605 | f->type, q_data->width, q_data->height, q_data->fmt->fourcc); |
| 606 | |
Marek Szyprowski | 07e8030 | 2010-12-20 14:39:25 -0300 | [diff] [blame] | 607 | return 0; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, |
| 611 | struct v4l2_format *f) |
| 612 | { |
| 613 | int ret; |
| 614 | |
| 615 | ret = vidioc_try_fmt_vid_cap(file, priv, f); |
| 616 | if (ret) |
| 617 | return ret; |
| 618 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 619 | return vidioc_s_fmt(file2ctx(file), f); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | static int vidioc_s_fmt_vid_out(struct file *file, void *priv, |
| 623 | struct v4l2_format *f) |
| 624 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 625 | struct vim2m_ctx *ctx = file2ctx(file); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 626 | int ret; |
| 627 | |
| 628 | ret = vidioc_try_fmt_vid_out(file, priv, f); |
| 629 | if (ret) |
| 630 | return ret; |
| 631 | |
Hans Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 632 | ret = vidioc_s_fmt(file2ctx(file), f); |
| 633 | if (!ret) |
| 634 | ctx->colorspace = f->fmt.pix.colorspace; |
| 635 | return ret; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 636 | } |
| 637 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 638 | static int vim2m_s_ctrl(struct v4l2_ctrl *ctrl) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 639 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 640 | struct vim2m_ctx *ctx = |
| 641 | container_of(ctrl->handler, struct vim2m_ctx, hdl); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 642 | |
| 643 | switch (ctrl->id) { |
Tomasz Moń | 8007287 | 2012-06-12 06:43:49 -0300 | [diff] [blame] | 644 | case V4L2_CID_HFLIP: |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 645 | if (ctrl->val) |
Tomasz Moń | 8007287 | 2012-06-12 06:43:49 -0300 | [diff] [blame] | 646 | ctx->mode |= MEM2MEM_HFLIP; |
| 647 | else |
| 648 | ctx->mode &= ~MEM2MEM_HFLIP; |
| 649 | break; |
| 650 | |
| 651 | case V4L2_CID_VFLIP: |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 652 | if (ctrl->val) |
Tomasz Moń | 8007287 | 2012-06-12 06:43:49 -0300 | [diff] [blame] | 653 | ctx->mode |= MEM2MEM_VFLIP; |
| 654 | else |
| 655 | ctx->mode &= ~MEM2MEM_VFLIP; |
| 656 | break; |
| 657 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 658 | case V4L2_CID_TRANS_TIME_MSEC: |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 659 | ctx->transtime = ctrl->val; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 660 | break; |
| 661 | |
| 662 | case V4L2_CID_TRANS_NUM_BUFS: |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 663 | ctx->translen = ctrl->val; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 664 | break; |
| 665 | |
| 666 | default: |
| 667 | v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n"); |
| 668 | return -EINVAL; |
| 669 | } |
| 670 | |
| 671 | return 0; |
| 672 | } |
| 673 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 674 | static const struct v4l2_ctrl_ops vim2m_ctrl_ops = { |
| 675 | .s_ctrl = vim2m_s_ctrl, |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 676 | }; |
| 677 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 678 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 679 | static const struct v4l2_ioctl_ops vim2m_ioctl_ops = { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 680 | .vidioc_querycap = vidioc_querycap, |
| 681 | |
| 682 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, |
| 683 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, |
| 684 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, |
| 685 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, |
| 686 | |
| 687 | .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out, |
| 688 | .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out, |
| 689 | .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out, |
| 690 | .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out, |
| 691 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 692 | .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, |
| 693 | .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, |
| 694 | .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, |
| 695 | .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, |
Hans Verkuil | 7bcff1c | 2015-06-05 11:28:52 -0300 | [diff] [blame] | 696 | .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, |
| 697 | .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, |
Hans Verkuil | f5294f4 | 2014-11-18 09:51:07 -0300 | [diff] [blame] | 698 | .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 699 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 700 | .vidioc_streamon = v4l2_m2m_ioctl_streamon, |
| 701 | .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 702 | |
Hans Verkuil | 97a3c90 | 2012-07-18 10:54:59 -0300 | [diff] [blame] | 703 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
| 704 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 705 | }; |
| 706 | |
| 707 | |
| 708 | /* |
| 709 | * Queue operations |
| 710 | */ |
| 711 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 712 | static int vim2m_queue_setup(struct vb2_queue *vq, |
Guennadi Liakhovetski | fc714e70 | 2011-08-24 10:30:21 -0300 | [diff] [blame] | 713 | unsigned int *nbuffers, unsigned int *nplanes, |
| 714 | unsigned int sizes[], void *alloc_ctxs[]) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 715 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 716 | struct vim2m_ctx *ctx = vb2_get_drv_priv(vq); |
| 717 | struct vim2m_q_data *q_data; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 718 | unsigned int size, count = *nbuffers; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 719 | |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 720 | q_data = get_q_data(ctx, vq->type); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 721 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 722 | size = q_data->width * q_data->height * q_data->fmt->depth >> 3; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 723 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 724 | while (size * count > MEM2MEM_VID_MEM_LIMIT) |
| 725 | (count)--; |
Hans Verkuil | df9ecb0c | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 726 | *nbuffers = count; |
| 727 | |
| 728 | if (*nplanes) |
| 729 | return sizes[0] < size ? -EINVAL : 0; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 730 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 731 | *nplanes = 1; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 732 | sizes[0] = size; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 733 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 734 | /* |
| 735 | * videobuf2-vmalloc allocator is context-less so no need to set |
| 736 | * alloc_ctxs array. |
| 737 | */ |
| 738 | |
| 739 | dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 744 | static int vim2m_buf_prepare(struct vb2_buffer *vb) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 745 | { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 746 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 747 | struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
| 748 | struct vim2m_q_data *q_data; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 749 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 750 | dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 751 | |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 752 | q_data = get_q_data(ctx, vb->vb2_queue->type); |
Hans Verkuil | 5c3112b | 2014-03-10 10:58:29 -0300 | [diff] [blame] | 753 | if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 754 | if (vbuf->field == V4L2_FIELD_ANY) |
| 755 | vbuf->field = V4L2_FIELD_NONE; |
| 756 | if (vbuf->field != V4L2_FIELD_NONE) { |
Hans Verkuil | 5c3112b | 2014-03-10 10:58:29 -0300 | [diff] [blame] | 757 | dprintk(ctx->dev, "%s field isn't supported\n", |
| 758 | __func__); |
| 759 | return -EINVAL; |
| 760 | } |
| 761 | } |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 762 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 763 | if (vb2_plane_size(vb, 0) < q_data->sizeimage) { |
| 764 | dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n", |
| 765 | __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 766 | return -EINVAL; |
| 767 | } |
| 768 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 769 | vb2_set_plane_payload(vb, 0, q_data->sizeimage); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 770 | |
| 771 | return 0; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 772 | } |
| 773 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 774 | static void vim2m_buf_queue(struct vb2_buffer *vb) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 775 | { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 776 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 777 | struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
Hans Verkuil | ca5f5fd | 2014-03-10 10:58:28 -0300 | [diff] [blame] | 778 | |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 779 | v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf); |
Michael Olbrich | 0f910bf | 2011-07-12 09:46:44 -0300 | [diff] [blame] | 780 | } |
| 781 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 782 | static int vim2m_start_streaming(struct vb2_queue *q, unsigned count) |
Hans Verkuil | ca5f5fd | 2014-03-10 10:58:28 -0300 | [diff] [blame] | 783 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 784 | struct vim2m_ctx *ctx = vb2_get_drv_priv(q); |
| 785 | struct vim2m_q_data *q_data = get_q_data(ctx, q->type); |
Hans Verkuil | ca5f5fd | 2014-03-10 10:58:28 -0300 | [diff] [blame] | 786 | |
| 787 | q_data->sequence = 0; |
| 788 | return 0; |
| 789 | } |
| 790 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 791 | static void vim2m_stop_streaming(struct vb2_queue *q) |
Hans Verkuil | a773632 | 2014-03-10 10:58:27 -0300 | [diff] [blame] | 792 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 793 | struct vim2m_ctx *ctx = vb2_get_drv_priv(q); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 794 | struct vb2_v4l2_buffer *vbuf; |
Hans Verkuil | a773632 | 2014-03-10 10:58:27 -0300 | [diff] [blame] | 795 | unsigned long flags; |
| 796 | |
| 797 | for (;;) { |
| 798 | if (V4L2_TYPE_IS_OUTPUT(q->type)) |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 799 | vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); |
Hans Verkuil | a773632 | 2014-03-10 10:58:27 -0300 | [diff] [blame] | 800 | else |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 801 | vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); |
| 802 | if (vbuf == NULL) |
Hans Verkuil | e37559b | 2014-04-17 02:47:21 -0300 | [diff] [blame] | 803 | return; |
Hans Verkuil | a773632 | 2014-03-10 10:58:27 -0300 | [diff] [blame] | 804 | spin_lock_irqsave(&ctx->dev->irqlock, flags); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 805 | v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR); |
Hans Verkuil | a773632 | 2014-03-10 10:58:27 -0300 | [diff] [blame] | 806 | spin_unlock_irqrestore(&ctx->dev->irqlock, flags); |
| 807 | } |
Hans Verkuil | a773632 | 2014-03-10 10:58:27 -0300 | [diff] [blame] | 808 | } |
| 809 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 810 | static struct vb2_ops vim2m_qops = { |
| 811 | .queue_setup = vim2m_queue_setup, |
| 812 | .buf_prepare = vim2m_buf_prepare, |
| 813 | .buf_queue = vim2m_buf_queue, |
| 814 | .start_streaming = vim2m_start_streaming, |
| 815 | .stop_streaming = vim2m_stop_streaming, |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 816 | .wait_prepare = vb2_ops_wait_prepare, |
| 817 | .wait_finish = vb2_ops_wait_finish, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 818 | }; |
| 819 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 820 | static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 821 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 822 | struct vim2m_ctx *ctx = priv; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 823 | int ret; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 824 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 825 | src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; |
Hans Verkuil | 782f36c | 2014-03-10 10:58:26 -0300 | [diff] [blame] | 826 | src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 827 | src_vq->drv_priv = ctx; |
| 828 | src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 829 | src_vq->ops = &vim2m_qops; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 830 | src_vq->mem_ops = &vb2_vmalloc_memops; |
Sakari Ailus | ade4868 | 2014-02-25 19:12:19 -0300 | [diff] [blame] | 831 | src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 832 | src_vq->lock = &ctx->dev->dev_mutex; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 833 | |
| 834 | ret = vb2_queue_init(src_vq); |
| 835 | if (ret) |
| 836 | return ret; |
| 837 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 838 | dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Hans Verkuil | 782f36c | 2014-03-10 10:58:26 -0300 | [diff] [blame] | 839 | dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 840 | dst_vq->drv_priv = ctx; |
| 841 | dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 842 | dst_vq->ops = &vim2m_qops; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 843 | dst_vq->mem_ops = &vb2_vmalloc_memops; |
Sakari Ailus | ade4868 | 2014-02-25 19:12:19 -0300 | [diff] [blame] | 844 | dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 845 | dst_vq->lock = &ctx->dev->dev_mutex; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 846 | |
| 847 | return vb2_queue_init(dst_vq); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 848 | } |
| 849 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 850 | static const struct v4l2_ctrl_config vim2m_ctrl_trans_time_msec = { |
| 851 | .ops = &vim2m_ctrl_ops, |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 852 | .id = V4L2_CID_TRANS_TIME_MSEC, |
| 853 | .name = "Transaction Time (msec)", |
| 854 | .type = V4L2_CTRL_TYPE_INTEGER, |
Hans Verkuil | 4340583 | 2014-03-10 10:58:23 -0300 | [diff] [blame] | 855 | .def = MEM2MEM_DEF_TRANSTIME, |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 856 | .min = 1, |
| 857 | .max = 10001, |
Hans Verkuil | 4340583 | 2014-03-10 10:58:23 -0300 | [diff] [blame] | 858 | .step = 1, |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 859 | }; |
| 860 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 861 | static const struct v4l2_ctrl_config vim2m_ctrl_trans_num_bufs = { |
| 862 | .ops = &vim2m_ctrl_ops, |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 863 | .id = V4L2_CID_TRANS_NUM_BUFS, |
| 864 | .name = "Buffers Per Transaction", |
| 865 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 866 | .def = 1, |
| 867 | .min = 1, |
| 868 | .max = MEM2MEM_DEF_NUM_BUFS, |
| 869 | .step = 1, |
| 870 | }; |
| 871 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 872 | /* |
| 873 | * File operations |
| 874 | */ |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 875 | static int vim2m_open(struct file *file) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 876 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 877 | struct vim2m_dev *dev = video_drvdata(file); |
| 878 | struct vim2m_ctx *ctx = NULL; |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 879 | struct v4l2_ctrl_handler *hdl; |
Hans Verkuil | f469403 | 2012-07-31 03:51:25 -0300 | [diff] [blame] | 880 | int rc = 0; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 881 | |
Hans Verkuil | f469403 | 2012-07-31 03:51:25 -0300 | [diff] [blame] | 882 | if (mutex_lock_interruptible(&dev->dev_mutex)) |
| 883 | return -ERESTARTSYS; |
Sachin Kamat | 56ed221 | 2012-09-24 02:17:46 -0300 | [diff] [blame] | 884 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
Hans Verkuil | f469403 | 2012-07-31 03:51:25 -0300 | [diff] [blame] | 885 | if (!ctx) { |
| 886 | rc = -ENOMEM; |
| 887 | goto open_unlock; |
| 888 | } |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 889 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 890 | v4l2_fh_init(&ctx->fh, video_devdata(file)); |
| 891 | file->private_data = &ctx->fh; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 892 | ctx->dev = dev; |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 893 | hdl = &ctx->hdl; |
| 894 | v4l2_ctrl_handler_init(hdl, 4); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 895 | v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0); |
| 896 | v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0); |
| 897 | v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_time_msec, NULL); |
| 898 | v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_num_bufs, NULL); |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 899 | if (hdl->error) { |
Dan Carpenter | a7bd775 | 2012-08-14 02:58:56 -0300 | [diff] [blame] | 900 | rc = hdl->error; |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 901 | v4l2_ctrl_handler_free(hdl); |
Dan Carpenter | a7bd775 | 2012-08-14 02:58:56 -0300 | [diff] [blame] | 902 | goto open_unlock; |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 903 | } |
| 904 | ctx->fh.ctrl_handler = hdl; |
| 905 | v4l2_ctrl_handler_setup(hdl); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 906 | |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 907 | ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0]; |
Hans Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 908 | ctx->q_data[V4L2_M2M_SRC].width = 640; |
| 909 | ctx->q_data[V4L2_M2M_SRC].height = 480; |
| 910 | ctx->q_data[V4L2_M2M_SRC].sizeimage = |
| 911 | ctx->q_data[V4L2_M2M_SRC].width * |
| 912 | ctx->q_data[V4L2_M2M_SRC].height * |
| 913 | (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3); |
| 914 | ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC]; |
| 915 | ctx->colorspace = V4L2_COLORSPACE_REC709; |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 916 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 917 | ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init); |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 918 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 919 | if (IS_ERR(ctx->fh.m2m_ctx)) { |
| 920 | rc = PTR_ERR(ctx->fh.m2m_ctx); |
Dan Carpenter | 16ee9bb | 2010-05-05 02:58:57 -0300 | [diff] [blame] | 921 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 922 | v4l2_ctrl_handler_free(hdl); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 923 | kfree(ctx); |
Hans Verkuil | f469403 | 2012-07-31 03:51:25 -0300 | [diff] [blame] | 924 | goto open_unlock; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 925 | } |
| 926 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 927 | v4l2_fh_add(&ctx->fh); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 928 | atomic_inc(&dev->num_inst); |
| 929 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 930 | dprintk(dev, "Created instance: %p, m2m_ctx: %p\n", |
| 931 | ctx, ctx->fh.m2m_ctx); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 932 | |
Hans Verkuil | f469403 | 2012-07-31 03:51:25 -0300 | [diff] [blame] | 933 | open_unlock: |
| 934 | mutex_unlock(&dev->dev_mutex); |
Dan Carpenter | a7bd775 | 2012-08-14 02:58:56 -0300 | [diff] [blame] | 935 | return rc; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 936 | } |
| 937 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 938 | static int vim2m_release(struct file *file) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 939 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 940 | struct vim2m_dev *dev = video_drvdata(file); |
| 941 | struct vim2m_ctx *ctx = file2ctx(file); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 942 | |
| 943 | dprintk(dev, "Releasing instance %p\n", ctx); |
| 944 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 945 | v4l2_fh_del(&ctx->fh); |
| 946 | v4l2_fh_exit(&ctx->fh); |
| 947 | v4l2_ctrl_handler_free(&ctx->hdl); |
Hans Verkuil | f469403 | 2012-07-31 03:51:25 -0300 | [diff] [blame] | 948 | mutex_lock(&dev->dev_mutex); |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 949 | v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); |
Hans Verkuil | f469403 | 2012-07-31 03:51:25 -0300 | [diff] [blame] | 950 | mutex_unlock(&dev->dev_mutex); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 951 | kfree(ctx); |
| 952 | |
| 953 | atomic_dec(&dev->num_inst); |
| 954 | |
| 955 | return 0; |
| 956 | } |
| 957 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 958 | static const struct v4l2_file_operations vim2m_fops = { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 959 | .owner = THIS_MODULE, |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 960 | .open = vim2m_open, |
| 961 | .release = vim2m_release, |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 962 | .poll = v4l2_m2m_fop_poll, |
Marek Szyprowski | 07e8030 | 2010-12-20 14:39:25 -0300 | [diff] [blame] | 963 | .unlocked_ioctl = video_ioctl2, |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 964 | .mmap = v4l2_m2m_fop_mmap, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 965 | }; |
| 966 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 967 | static struct video_device vim2m_videodev = { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 968 | .name = MEM2MEM_NAME, |
Hans Verkuil | 954f340 | 2012-09-05 06:05:50 -0300 | [diff] [blame] | 969 | .vfl_dir = VFL_DIR_M2M, |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 970 | .fops = &vim2m_fops, |
| 971 | .ioctl_ops = &vim2m_ioctl_ops, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 972 | .minor = -1, |
Hans Verkuil | 0936542 | 2015-03-09 13:33:56 -0300 | [diff] [blame] | 973 | .release = video_device_release_empty, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 974 | }; |
| 975 | |
| 976 | static struct v4l2_m2m_ops m2m_ops = { |
| 977 | .device_run = device_run, |
| 978 | .job_ready = job_ready, |
| 979 | .job_abort = job_abort, |
| 980 | }; |
| 981 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 982 | static int vim2m_probe(struct platform_device *pdev) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 983 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 984 | struct vim2m_dev *dev; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 985 | struct video_device *vfd; |
| 986 | int ret; |
| 987 | |
Sachin Kamat | 38a7996 | 2012-09-24 02:17:48 -0300 | [diff] [blame] | 988 | dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 989 | if (!dev) |
| 990 | return -ENOMEM; |
| 991 | |
| 992 | spin_lock_init(&dev->irqlock); |
| 993 | |
| 994 | ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); |
| 995 | if (ret) |
Sachin Kamat | 38a7996 | 2012-09-24 02:17:48 -0300 | [diff] [blame] | 996 | return ret; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 997 | |
| 998 | atomic_set(&dev->num_inst, 0); |
| 999 | mutex_init(&dev->dev_mutex); |
| 1000 | |
Hans Verkuil | 0936542 | 2015-03-09 13:33:56 -0300 | [diff] [blame] | 1001 | dev->vfd = vim2m_videodev; |
| 1002 | vfd = &dev->vfd; |
Marek Szyprowski | 07e8030 | 2010-12-20 14:39:25 -0300 | [diff] [blame] | 1003 | vfd->lock = &dev->dev_mutex; |
Hans Verkuil | 8f484d8 | 2013-06-27 02:44:04 -0300 | [diff] [blame] | 1004 | vfd->v4l2_dev = &dev->v4l2_dev; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1005 | |
| 1006 | ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0); |
| 1007 | if (ret) { |
| 1008 | v4l2_err(&dev->v4l2_dev, "Failed to register video device\n"); |
Hans Verkuil | 0936542 | 2015-03-09 13:33:56 -0300 | [diff] [blame] | 1009 | goto unreg_dev; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | video_set_drvdata(vfd, dev); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1013 | snprintf(vfd->name, sizeof(vfd->name), "%s", vim2m_videodev.name); |
Hans Verkuil | 8f484d8 | 2013-06-27 02:44:04 -0300 | [diff] [blame] | 1014 | v4l2_info(&dev->v4l2_dev, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1015 | "Device registered as /dev/video%d\n", vfd->num); |
| 1016 | |
| 1017 | setup_timer(&dev->timer, device_isr, (long)dev); |
| 1018 | platform_set_drvdata(pdev, dev); |
| 1019 | |
| 1020 | dev->m2m_dev = v4l2_m2m_init(&m2m_ops); |
| 1021 | if (IS_ERR(dev->m2m_dev)) { |
| 1022 | v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n"); |
| 1023 | ret = PTR_ERR(dev->m2m_dev); |
| 1024 | goto err_m2m; |
| 1025 | } |
| 1026 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1027 | return 0; |
| 1028 | |
| 1029 | err_m2m: |
Sachin Kamat | b7e13ef | 2012-09-24 02:17:45 -0300 | [diff] [blame] | 1030 | v4l2_m2m_release(dev->m2m_dev); |
Hans Verkuil | 0936542 | 2015-03-09 13:33:56 -0300 | [diff] [blame] | 1031 | video_unregister_device(&dev->vfd); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1032 | unreg_dev: |
| 1033 | v4l2_device_unregister(&dev->v4l2_dev); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1034 | |
| 1035 | return ret; |
| 1036 | } |
| 1037 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1038 | static int vim2m_remove(struct platform_device *pdev) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1039 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1040 | struct vim2m_dev *dev = platform_get_drvdata(pdev); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1041 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1042 | v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1043 | v4l2_m2m_release(dev->m2m_dev); |
| 1044 | del_timer_sync(&dev->timer); |
Hans Verkuil | 0936542 | 2015-03-09 13:33:56 -0300 | [diff] [blame] | 1045 | video_unregister_device(&dev->vfd); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1046 | v4l2_device_unregister(&dev->v4l2_dev); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1047 | |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1051 | static struct platform_driver vim2m_pdrv = { |
| 1052 | .probe = vim2m_probe, |
| 1053 | .remove = vim2m_remove, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1054 | .driver = { |
| 1055 | .name = MEM2MEM_NAME, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1056 | }, |
| 1057 | }; |
| 1058 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1059 | static void __exit vim2m_exit(void) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1060 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1061 | platform_driver_unregister(&vim2m_pdrv); |
| 1062 | platform_device_unregister(&vim2m_pdev); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1063 | } |
| 1064 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1065 | static int __init vim2m_init(void) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1066 | { |
| 1067 | int ret; |
| 1068 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1069 | ret = platform_device_register(&vim2m_pdev); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1070 | if (ret) |
| 1071 | return ret; |
| 1072 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1073 | ret = platform_driver_register(&vim2m_pdrv); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1074 | if (ret) |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1075 | platform_device_unregister(&vim2m_pdev); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1076 | |
| 1077 | return 0; |
| 1078 | } |
| 1079 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1080 | module_init(vim2m_init); |
| 1081 | module_exit(vim2m_exit); |