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