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