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