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