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