blob: 08e24379b7949af3a71f67123dd8749ce831c7aa [file] [log] [blame]
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001/*
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 Osciak95072082011-03-13 15:23:32 -030011 * Pawel Osciak, <pawel@osciak.com>
Pawel Osciak96d8eab2010-04-23 05:38:38 -030012 * 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 Osciak96d8eab2010-04-23 05:38:38 -030022#include <linux/timer.h>
23#include <linux/sched.h>
Randy Dunlap6b46c392010-05-07 15:22:26 -030024#include <linux/slab.h>
Pawel Osciak96d8eab2010-04-23 05:38:38 -030025
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 Verkuild3dd59c2012-07-18 10:35:37 -030030#include <media/v4l2-ctrls.h>
Hans Verkuil97a3c902012-07-18 10:54:59 -030031#include <media/v4l2-event.h>
Marek Szyprowskid80ee382011-01-12 06:50:55 -030032#include <media/videobuf2-vmalloc.h>
Pawel Osciak96d8eab2010-04-23 05:38:38 -030033
34#define MEM2MEM_TEST_MODULE_NAME "mem2mem-testdev"
35
36MODULE_DESCRIPTION("Virtual device for mem2mem framework testing");
Pawel Osciak95072082011-03-13 15:23:32 -030037MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
Pawel Osciak96d8eab2010-04-23 05:38:38 -030038MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030039MODULE_VERSION("0.1.1");
Pawel Osciak96d8eab2010-04-23 05:38:38 -030040
Hans Verkuild95d7c62013-04-17 03:04:10 -030041static unsigned debug;
42module_param(debug, uint, 0644);
43MODULE_PARM_DESC(debug, "activates debug info");
44
Pawel Osciak96d8eab2010-04-23 05:38:38 -030045#define MIN_W 32
46#define MIN_H 32
47#define MAX_W 640
48#define MAX_H 480
Guennadi Liakhovetski9ce3ce42012-04-27 04:16:46 -030049#define DIM_ALIGN_MASK 7 /* 8-byte alignment for line length */
Pawel Osciak96d8eab2010-04-23 05:38:38 -030050
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ń80072872012-06-12 06:43:49 -030069/* Flags that indicate processing mode */
70#define MEM2MEM_HFLIP (1 << 0)
71#define MEM2MEM_VFLIP (1 << 1)
72
Pawel Osciak96d8eab2010-04-23 05:38:38 -030073#define dprintk(dev, fmt, arg...) \
Hans Verkuild95d7c62013-04-17 03:04:10 -030074 v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
Pawel Osciak96d8eab2010-04-23 05:38:38 -030075
76
Sachin Kamat32872832012-08-24 08:06:15 -030077static void m2mtest_dev_release(struct device *dev)
Pawel Osciak96d8eab2010-04-23 05:38:38 -030078{}
79
80static struct platform_device m2mtest_pdev = {
81 .name = MEM2MEM_NAME,
82 .dev.release = m2mtest_dev_release,
83};
84
85struct m2mtest_fmt {
86 char *name;
87 u32 fourcc;
88 int depth;
89 /* Types the format can be used for */
90 u32 types;
91};
92
93static 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 Verkuild3dd59c2012-07-18 10:35:37 -0300110#define NUM_FORMATS ARRAY_SIZE(formats)
111
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300112/* Per-queue, driver-specific private data */
113struct m2mtest_q_data {
114 unsigned int width;
115 unsigned int height;
116 unsigned int sizeimage;
117 struct m2mtest_fmt *fmt;
118};
119
120enum {
121 V4L2_M2M_SRC = 0,
122 V4L2_M2M_DST = 1,
123};
124
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300125#define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000)
126#define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300127
128static 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
145struct 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
158struct m2mtest_ctx {
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300159 struct v4l2_fh fh;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300160 struct m2mtest_dev *dev;
161
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300162 struct v4l2_ctrl_handler hdl;
163
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300164 /* 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ń80072872012-06-12 06:43:49 -0300175 /* Processing mode */
176 int mode;
177
Hans Verkuil47556ff2012-07-18 11:33:22 -0300178 enum v4l2_colorspace colorspace;
179
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300180 /* Source and destination queue data */
181 struct m2mtest_q_data q_data[2];
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300182};
183
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300184static inline struct m2mtest_ctx *file2ctx(struct file *file)
185{
186 return container_of(file->private_data, struct m2mtest_ctx, fh);
187}
188
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300189static struct m2mtest_q_data *get_q_data(struct m2mtest_ctx *ctx,
190 enum v4l2_buf_type type)
191{
192 switch (type) {
193 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
194 return &ctx->q_data[V4L2_M2M_SRC];
195 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
196 return &ctx->q_data[V4L2_M2M_DST];
197 default:
198 BUG();
199 }
200 return NULL;
201}
202
203
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300204static int device_process(struct m2mtest_ctx *ctx,
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300205 struct vb2_buffer *in_vb,
206 struct vb2_buffer *out_vb)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300207{
208 struct m2mtest_dev *dev = ctx->dev;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300209 struct m2mtest_q_data *q_data;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300210 u8 *p_in, *p_out;
211 int x, y, t, w;
212 int tile_w, bytes_left;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300213 int width, height, bytesperline;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300214
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300215 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300216
217 width = q_data->width;
218 height = q_data->height;
219 bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
220
221 p_in = vb2_plane_vaddr(in_vb, 0);
222 p_out = vb2_plane_vaddr(out_vb, 0);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300223 if (!p_in || !p_out) {
224 v4l2_err(&dev->v4l2_dev,
225 "Acquiring kernel pointers to buffers failed\n");
226 return -EFAULT;
227 }
228
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300229 if (vb2_plane_size(in_vb, 0) > vb2_plane_size(out_vb, 0)) {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300230 v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n");
231 return -EINVAL;
232 }
233
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300234 tile_w = (width * (q_data[V4L2_M2M_DST].fmt->depth >> 3))
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300235 / MEM2MEM_NUM_TILES;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300236 bytes_left = bytesperline - tile_w * MEM2MEM_NUM_TILES;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300237 w = 0;
238
Hans Verkuild95d7c62013-04-17 03:04:10 -0300239 memcpy(&out_vb->v4l2_buf.timestamp,
240 &in_vb->v4l2_buf.timestamp,
241 sizeof(struct timeval));
242
Tomasz Moń80072872012-06-12 06:43:49 -0300243 switch (ctx->mode) {
244 case MEM2MEM_HFLIP | MEM2MEM_VFLIP:
245 p_out += bytesperline * height - bytes_left;
246 for (y = 0; y < height; ++y) {
247 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
248 if (w & 0x1) {
249 for (x = 0; x < tile_w; ++x)
250 *--p_out = *p_in++ +
251 MEM2MEM_COLOR_STEP;
252 } else {
253 for (x = 0; x < tile_w; ++x)
254 *--p_out = *p_in++ -
255 MEM2MEM_COLOR_STEP;
256 }
257 ++w;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300258 }
Tomasz Moń80072872012-06-12 06:43:49 -0300259 p_in += bytes_left;
260 p_out -= bytes_left;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300261 }
Tomasz Moń80072872012-06-12 06:43:49 -0300262 break;
263
264 case MEM2MEM_HFLIP:
265 for (y = 0; y < height; ++y) {
266 p_out += MEM2MEM_NUM_TILES * tile_w;
267 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
268 if (w & 0x01) {
269 for (x = 0; x < tile_w; ++x)
270 *--p_out = *p_in++ +
271 MEM2MEM_COLOR_STEP;
272 } else {
273 for (x = 0; x < tile_w; ++x)
274 *--p_out = *p_in++ -
275 MEM2MEM_COLOR_STEP;
276 }
277 ++w;
278 }
279 p_in += bytes_left;
280 p_out += bytesperline;
281 }
282 break;
283
284 case MEM2MEM_VFLIP:
285 p_out += bytesperline * (height - 1);
286 for (y = 0; y < height; ++y) {
287 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
288 if (w & 0x1) {
289 for (x = 0; x < tile_w; ++x)
290 *p_out++ = *p_in++ +
291 MEM2MEM_COLOR_STEP;
292 } else {
293 for (x = 0; x < tile_w; ++x)
294 *p_out++ = *p_in++ -
295 MEM2MEM_COLOR_STEP;
296 }
297 ++w;
298 }
299 p_in += bytes_left;
300 p_out += bytes_left - 2 * bytesperline;
301 }
302 break;
303
304 default:
305 for (y = 0; y < height; ++y) {
306 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
307 if (w & 0x1) {
308 for (x = 0; x < tile_w; ++x)
309 *p_out++ = *p_in++ +
310 MEM2MEM_COLOR_STEP;
311 } else {
312 for (x = 0; x < tile_w; ++x)
313 *p_out++ = *p_in++ -
314 MEM2MEM_COLOR_STEP;
315 }
316 ++w;
317 }
318 p_in += bytes_left;
319 p_out += bytes_left;
320 }
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300321 }
322
323 return 0;
324}
325
326static void schedule_irq(struct m2mtest_dev *dev, int msec_timeout)
327{
328 dprintk(dev, "Scheduling a simulated irq\n");
329 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(msec_timeout));
330}
331
332/*
333 * mem2mem callbacks
334 */
335
336/**
337 * job_ready() - check whether an instance is ready to be scheduled to run
338 */
339static int job_ready(void *priv)
340{
341 struct m2mtest_ctx *ctx = priv;
342
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300343 if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen
344 || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen) {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300345 dprintk(ctx->dev, "Not enough buffers available\n");
346 return 0;
347 }
348
349 return 1;
350}
351
352static void job_abort(void *priv)
353{
354 struct m2mtest_ctx *ctx = priv;
355
356 /* Will cancel the transaction in the next interrupt handler */
357 ctx->aborting = 1;
358}
359
360/* device_run() - prepares and starts the device
361 *
362 * This simulates all the immediate preparations required before starting
363 * a device. This will be called by the framework when it decides to schedule
364 * a particular instance.
365 */
366static void device_run(void *priv)
367{
368 struct m2mtest_ctx *ctx = priv;
369 struct m2mtest_dev *dev = ctx->dev;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300370 struct vb2_buffer *src_buf, *dst_buf;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300371
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300372 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
373 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300374
375 device_process(ctx, src_buf, dst_buf);
376
377 /* Run a timer, which simulates a hardware irq */
378 schedule_irq(dev, ctx->transtime);
379}
380
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300381static void device_isr(unsigned long priv)
382{
383 struct m2mtest_dev *m2mtest_dev = (struct m2mtest_dev *)priv;
384 struct m2mtest_ctx *curr_ctx;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300385 struct vb2_buffer *src_vb, *dst_vb;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300386 unsigned long flags;
387
388 curr_ctx = v4l2_m2m_get_curr_priv(m2mtest_dev->m2m_dev);
389
390 if (NULL == curr_ctx) {
Sachin Kamat26fdcf02012-09-24 02:17:47 -0300391 pr_err("Instance released before the end of transaction\n");
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300392 return;
393 }
394
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300395 src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
396 dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300397
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300398 curr_ctx->num_processed++;
399
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300400 spin_lock_irqsave(&m2mtest_dev->irqlock, flags);
401 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
402 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
403 spin_unlock_irqrestore(&m2mtest_dev->irqlock, flags);
404
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300405 if (curr_ctx->num_processed == curr_ctx->translen
406 || curr_ctx->aborting) {
407 dprintk(curr_ctx->dev, "Finishing transaction\n");
408 curr_ctx->num_processed = 0;
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300409 v4l2_m2m_job_finish(m2mtest_dev->m2m_dev, curr_ctx->fh.m2m_ctx);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300410 } else {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300411 device_run(curr_ctx);
412 }
413}
414
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300415/*
416 * video ioctls
417 */
418static int vidioc_querycap(struct file *file, void *priv,
419 struct v4l2_capability *cap)
420{
421 strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
422 strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
Hans Verkuil72c2af62012-09-14 06:23:12 -0300423 snprintf(cap->bus_info, sizeof(cap->bus_info),
424 "platform:%s", MEM2MEM_NAME);
Mauro Carvalho Chehab79e8c7b2012-08-24 11:25:10 -0300425 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Hans Verkuilb0d14992012-07-18 10:46:01 -0300426 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300427 return 0;
428}
429
430static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
431{
432 int i, num;
433 struct m2mtest_fmt *fmt;
434
435 num = 0;
436
437 for (i = 0; i < NUM_FORMATS; ++i) {
438 if (formats[i].types & type) {
439 /* index-th format of type type found ? */
440 if (num == f->index)
441 break;
442 /* Correct type but haven't reached our index yet,
443 * just increment per-type index */
444 ++num;
445 }
446 }
447
448 if (i < NUM_FORMATS) {
449 /* Format found */
450 fmt = &formats[i];
451 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
452 f->pixelformat = fmt->fourcc;
453 return 0;
454 }
455
456 /* Format not found */
457 return -EINVAL;
458}
459
460static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
461 struct v4l2_fmtdesc *f)
462{
463 return enum_fmt(f, MEM2MEM_CAPTURE);
464}
465
466static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
467 struct v4l2_fmtdesc *f)
468{
469 return enum_fmt(f, MEM2MEM_OUTPUT);
470}
471
472static int vidioc_g_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
473{
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300474 struct vb2_queue *vq;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300475 struct m2mtest_q_data *q_data;
476
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300477 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300478 if (!vq)
479 return -EINVAL;
480
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300481 q_data = get_q_data(ctx, f->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300482
483 f->fmt.pix.width = q_data->width;
484 f->fmt.pix.height = q_data->height;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300485 f->fmt.pix.field = V4L2_FIELD_NONE;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300486 f->fmt.pix.pixelformat = q_data->fmt->fourcc;
487 f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
488 f->fmt.pix.sizeimage = q_data->sizeimage;
Hans Verkuil47556ff2012-07-18 11:33:22 -0300489 f->fmt.pix.colorspace = ctx->colorspace;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300490
491 return 0;
492}
493
494static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
495 struct v4l2_format *f)
496{
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300497 return vidioc_g_fmt(file2ctx(file), f);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300498}
499
500static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
501 struct v4l2_format *f)
502{
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300503 return vidioc_g_fmt(file2ctx(file), f);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300504}
505
506static int vidioc_try_fmt(struct v4l2_format *f, struct m2mtest_fmt *fmt)
507{
508 enum v4l2_field field;
509
510 field = f->fmt.pix.field;
511
512 if (field == V4L2_FIELD_ANY)
513 field = V4L2_FIELD_NONE;
514 else if (V4L2_FIELD_NONE != field)
515 return -EINVAL;
516
517 /* V4L2 specification suggests the driver corrects the format struct
518 * if any of the dimensions is unsupported */
519 f->fmt.pix.field = field;
520
521 if (f->fmt.pix.height < MIN_H)
522 f->fmt.pix.height = MIN_H;
523 else if (f->fmt.pix.height > MAX_H)
524 f->fmt.pix.height = MAX_H;
525
526 if (f->fmt.pix.width < MIN_W)
527 f->fmt.pix.width = MIN_W;
528 else if (f->fmt.pix.width > MAX_W)
529 f->fmt.pix.width = MAX_W;
530
531 f->fmt.pix.width &= ~DIM_ALIGN_MASK;
532 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
533 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
534
535 return 0;
536}
537
538static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
539 struct v4l2_format *f)
540{
541 struct m2mtest_fmt *fmt;
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300542 struct m2mtest_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300543
544 fmt = find_format(f);
545 if (!fmt || !(fmt->types & MEM2MEM_CAPTURE)) {
546 v4l2_err(&ctx->dev->v4l2_dev,
547 "Fourcc format (0x%08x) invalid.\n",
548 f->fmt.pix.pixelformat);
549 return -EINVAL;
550 }
Hans Verkuil47556ff2012-07-18 11:33:22 -0300551 f->fmt.pix.colorspace = ctx->colorspace;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300552
553 return vidioc_try_fmt(f, fmt);
554}
555
556static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
557 struct v4l2_format *f)
558{
559 struct m2mtest_fmt *fmt;
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300560 struct m2mtest_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300561
562 fmt = find_format(f);
563 if (!fmt || !(fmt->types & MEM2MEM_OUTPUT)) {
564 v4l2_err(&ctx->dev->v4l2_dev,
565 "Fourcc format (0x%08x) invalid.\n",
566 f->fmt.pix.pixelformat);
567 return -EINVAL;
568 }
Hans Verkuil47556ff2012-07-18 11:33:22 -0300569 if (!f->fmt.pix.colorspace)
570 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300571
572 return vidioc_try_fmt(f, fmt);
573}
574
575static int vidioc_s_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
576{
577 struct m2mtest_q_data *q_data;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300578 struct vb2_queue *vq;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300579
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300580 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300581 if (!vq)
582 return -EINVAL;
583
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300584 q_data = get_q_data(ctx, f->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300585 if (!q_data)
586 return -EINVAL;
587
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300588 if (vb2_is_busy(vq)) {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300589 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
Marek Szyprowski07e80302010-12-20 14:39:25 -0300590 return -EBUSY;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300591 }
592
593 q_data->fmt = find_format(f);
594 q_data->width = f->fmt.pix.width;
595 q_data->height = f->fmt.pix.height;
596 q_data->sizeimage = q_data->width * q_data->height
597 * q_data->fmt->depth >> 3;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300598
599 dprintk(ctx->dev,
600 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
601 f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
602
Marek Szyprowski07e80302010-12-20 14:39:25 -0300603 return 0;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300604}
605
606static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
607 struct v4l2_format *f)
608{
609 int ret;
610
611 ret = vidioc_try_fmt_vid_cap(file, priv, f);
612 if (ret)
613 return ret;
614
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300615 return vidioc_s_fmt(file2ctx(file), f);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300616}
617
618static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
619 struct v4l2_format *f)
620{
Hans Verkuil47556ff2012-07-18 11:33:22 -0300621 struct m2mtest_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300622 int ret;
623
624 ret = vidioc_try_fmt_vid_out(file, priv, f);
625 if (ret)
626 return ret;
627
Hans Verkuil47556ff2012-07-18 11:33:22 -0300628 ret = vidioc_s_fmt(file2ctx(file), f);
629 if (!ret)
630 ctx->colorspace = f->fmt.pix.colorspace;
631 return ret;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300632}
633
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300634static int m2mtest_s_ctrl(struct v4l2_ctrl *ctrl)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300635{
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300636 struct m2mtest_ctx *ctx =
637 container_of(ctrl->handler, struct m2mtest_ctx, hdl);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300638
639 switch (ctrl->id) {
Tomasz Moń80072872012-06-12 06:43:49 -0300640 case V4L2_CID_HFLIP:
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300641 if (ctrl->val)
Tomasz Moń80072872012-06-12 06:43:49 -0300642 ctx->mode |= MEM2MEM_HFLIP;
643 else
644 ctx->mode &= ~MEM2MEM_HFLIP;
645 break;
646
647 case V4L2_CID_VFLIP:
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300648 if (ctrl->val)
Tomasz Moń80072872012-06-12 06:43:49 -0300649 ctx->mode |= MEM2MEM_VFLIP;
650 else
651 ctx->mode &= ~MEM2MEM_VFLIP;
652 break;
653
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300654 case V4L2_CID_TRANS_TIME_MSEC:
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300655 ctx->transtime = ctrl->val;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300656 break;
657
658 case V4L2_CID_TRANS_NUM_BUFS:
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300659 ctx->translen = ctrl->val;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300660 break;
661
662 default:
663 v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
664 return -EINVAL;
665 }
666
667 return 0;
668}
669
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300670static const struct v4l2_ctrl_ops m2mtest_ctrl_ops = {
671 .s_ctrl = m2mtest_s_ctrl,
672};
673
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300674
675static const struct v4l2_ioctl_ops m2mtest_ioctl_ops = {
676 .vidioc_querycap = vidioc_querycap,
677
678 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
679 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
680 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
681 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
682
683 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
684 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
685 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
686 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
687
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300688 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
689 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
690 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
691 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300692
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300693 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
694 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300695
Hans Verkuil97a3c902012-07-18 10:54:59 -0300696 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
697 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300698};
699
700
701/*
702 * Queue operations
703 */
704
Guennadi Liakhovetskifc714e702011-08-24 10:30:21 -0300705static int m2mtest_queue_setup(struct vb2_queue *vq,
706 const struct v4l2_format *fmt,
707 unsigned int *nbuffers, unsigned int *nplanes,
708 unsigned int sizes[], void *alloc_ctxs[])
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300709{
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300710 struct m2mtest_ctx *ctx = vb2_get_drv_priv(vq);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300711 struct m2mtest_q_data *q_data;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300712 unsigned int size, count = *nbuffers;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300713
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300714 q_data = get_q_data(ctx, vq->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300715
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300716 size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300717
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300718 while (size * count > MEM2MEM_VID_MEM_LIMIT)
719 (count)--;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300720
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300721 *nplanes = 1;
722 *nbuffers = count;
723 sizes[0] = size;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300724
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300725 /*
726 * videobuf2-vmalloc allocator is context-less so no need to set
727 * alloc_ctxs array.
728 */
729
730 dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300731
732 return 0;
733}
734
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300735static int m2mtest_buf_prepare(struct vb2_buffer *vb)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300736{
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300737 struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300738 struct m2mtest_q_data *q_data;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300739
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300740 dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300741
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300742 q_data = get_q_data(ctx, vb->vb2_queue->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300743
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300744 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
745 dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n",
746 __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300747 return -EINVAL;
748 }
749
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300750 vb2_set_plane_payload(vb, 0, q_data->sizeimage);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300751
752 return 0;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300753}
754
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300755static void m2mtest_buf_queue(struct vb2_buffer *vb)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300756{
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300757 struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300758 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Michael Olbrich0f910bf2011-07-12 09:46:44 -0300759}
760
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300761static struct vb2_ops m2mtest_qops = {
762 .queue_setup = m2mtest_queue_setup,
763 .buf_prepare = m2mtest_buf_prepare,
764 .buf_queue = m2mtest_buf_queue,
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300765 .wait_prepare = vb2_ops_wait_prepare,
766 .wait_finish = vb2_ops_wait_finish,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300767};
768
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300769static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300770{
771 struct m2mtest_ctx *ctx = priv;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300772 int ret;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300773
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300774 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Tomasz Stanislawskib0a550332012-06-14 10:37:49 -0300775 src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300776 src_vq->drv_priv = ctx;
777 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
778 src_vq->ops = &m2mtest_qops;
779 src_vq->mem_ops = &vb2_vmalloc_memops;
Hans Verkuild95d7c62013-04-17 03:04:10 -0300780 src_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300781 src_vq->lock = &ctx->dev->dev_mutex;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300782
783 ret = vb2_queue_init(src_vq);
784 if (ret)
785 return ret;
786
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300787 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Tomasz Stanislawskib0a550332012-06-14 10:37:49 -0300788 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300789 dst_vq->drv_priv = ctx;
790 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
791 dst_vq->ops = &m2mtest_qops;
792 dst_vq->mem_ops = &vb2_vmalloc_memops;
Hans Verkuild95d7c62013-04-17 03:04:10 -0300793 dst_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300794 dst_vq->lock = &ctx->dev->dev_mutex;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300795
796 return vb2_queue_init(dst_vq);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300797}
798
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300799static const struct v4l2_ctrl_config m2mtest_ctrl_trans_time_msec = {
800 .ops = &m2mtest_ctrl_ops,
801 .id = V4L2_CID_TRANS_TIME_MSEC,
802 .name = "Transaction Time (msec)",
803 .type = V4L2_CTRL_TYPE_INTEGER,
804 .def = 1001,
805 .min = 1,
806 .max = 10001,
807 .step = 100,
808};
809
810static const struct v4l2_ctrl_config m2mtest_ctrl_trans_num_bufs = {
811 .ops = &m2mtest_ctrl_ops,
812 .id = V4L2_CID_TRANS_NUM_BUFS,
813 .name = "Buffers Per Transaction",
814 .type = V4L2_CTRL_TYPE_INTEGER,
815 .def = 1,
816 .min = 1,
817 .max = MEM2MEM_DEF_NUM_BUFS,
818 .step = 1,
819};
820
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300821/*
822 * File operations
823 */
824static int m2mtest_open(struct file *file)
825{
826 struct m2mtest_dev *dev = video_drvdata(file);
827 struct m2mtest_ctx *ctx = NULL;
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300828 struct v4l2_ctrl_handler *hdl;
Hans Verkuilf4694032012-07-31 03:51:25 -0300829 int rc = 0;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300830
Hans Verkuilf4694032012-07-31 03:51:25 -0300831 if (mutex_lock_interruptible(&dev->dev_mutex))
832 return -ERESTARTSYS;
Sachin Kamat56ed2212012-09-24 02:17:46 -0300833 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Hans Verkuilf4694032012-07-31 03:51:25 -0300834 if (!ctx) {
835 rc = -ENOMEM;
836 goto open_unlock;
837 }
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300838
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300839 v4l2_fh_init(&ctx->fh, video_devdata(file));
840 file->private_data = &ctx->fh;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300841 ctx->dev = dev;
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300842 hdl = &ctx->hdl;
843 v4l2_ctrl_handler_init(hdl, 4);
844 v4l2_ctrl_new_std(hdl, &m2mtest_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
845 v4l2_ctrl_new_std(hdl, &m2mtest_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
846 v4l2_ctrl_new_custom(hdl, &m2mtest_ctrl_trans_time_msec, NULL);
847 v4l2_ctrl_new_custom(hdl, &m2mtest_ctrl_trans_num_bufs, NULL);
848 if (hdl->error) {
Dan Carpentera7bd7752012-08-14 02:58:56 -0300849 rc = hdl->error;
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300850 v4l2_ctrl_handler_free(hdl);
Dan Carpentera7bd7752012-08-14 02:58:56 -0300851 goto open_unlock;
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300852 }
853 ctx->fh.ctrl_handler = hdl;
854 v4l2_ctrl_handler_setup(hdl);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300855
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300856 ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
Hans Verkuil47556ff2012-07-18 11:33:22 -0300857 ctx->q_data[V4L2_M2M_SRC].width = 640;
858 ctx->q_data[V4L2_M2M_SRC].height = 480;
859 ctx->q_data[V4L2_M2M_SRC].sizeimage =
860 ctx->q_data[V4L2_M2M_SRC].width *
861 ctx->q_data[V4L2_M2M_SRC].height *
862 (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3);
863 ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
864 ctx->colorspace = V4L2_COLORSPACE_REC709;
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300865
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300866 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300867
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300868 if (IS_ERR(ctx->fh.m2m_ctx)) {
869 rc = PTR_ERR(ctx->fh.m2m_ctx);
Dan Carpenter16ee9bb2010-05-05 02:58:57 -0300870
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300871 v4l2_ctrl_handler_free(hdl);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300872 kfree(ctx);
Hans Verkuilf4694032012-07-31 03:51:25 -0300873 goto open_unlock;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300874 }
875
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300876 v4l2_fh_add(&ctx->fh);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300877 atomic_inc(&dev->num_inst);
878
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300879 dprintk(dev, "Created instance: %p, m2m_ctx: %p\n",
880 ctx, ctx->fh.m2m_ctx);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300881
Hans Verkuilf4694032012-07-31 03:51:25 -0300882open_unlock:
883 mutex_unlock(&dev->dev_mutex);
Dan Carpentera7bd7752012-08-14 02:58:56 -0300884 return rc;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300885}
886
887static int m2mtest_release(struct file *file)
888{
889 struct m2mtest_dev *dev = video_drvdata(file);
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300890 struct m2mtest_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300891
892 dprintk(dev, "Releasing instance %p\n", ctx);
893
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300894 v4l2_fh_del(&ctx->fh);
895 v4l2_fh_exit(&ctx->fh);
896 v4l2_ctrl_handler_free(&ctx->hdl);
Hans Verkuilf4694032012-07-31 03:51:25 -0300897 mutex_lock(&dev->dev_mutex);
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300898 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Hans Verkuilf4694032012-07-31 03:51:25 -0300899 mutex_unlock(&dev->dev_mutex);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300900 kfree(ctx);
901
902 atomic_dec(&dev->num_inst);
903
904 return 0;
905}
906
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300907static const struct v4l2_file_operations m2mtest_fops = {
908 .owner = THIS_MODULE,
909 .open = m2mtest_open,
910 .release = m2mtest_release,
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300911 .poll = v4l2_m2m_fop_poll,
Marek Szyprowski07e80302010-12-20 14:39:25 -0300912 .unlocked_ioctl = video_ioctl2,
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300913 .mmap = v4l2_m2m_fop_mmap,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300914};
915
916static struct video_device m2mtest_videodev = {
917 .name = MEM2MEM_NAME,
Hans Verkuil954f3402012-09-05 06:05:50 -0300918 .vfl_dir = VFL_DIR_M2M,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300919 .fops = &m2mtest_fops,
920 .ioctl_ops = &m2mtest_ioctl_ops,
921 .minor = -1,
922 .release = video_device_release,
923};
924
925static struct v4l2_m2m_ops m2m_ops = {
926 .device_run = device_run,
927 .job_ready = job_ready,
928 .job_abort = job_abort,
929};
930
931static int m2mtest_probe(struct platform_device *pdev)
932{
933 struct m2mtest_dev *dev;
934 struct video_device *vfd;
935 int ret;
936
Sachin Kamat38a79962012-09-24 02:17:48 -0300937 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300938 if (!dev)
939 return -ENOMEM;
940
941 spin_lock_init(&dev->irqlock);
942
943 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
944 if (ret)
Sachin Kamat38a79962012-09-24 02:17:48 -0300945 return ret;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300946
947 atomic_set(&dev->num_inst, 0);
948 mutex_init(&dev->dev_mutex);
949
950 vfd = video_device_alloc();
951 if (!vfd) {
952 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
953 ret = -ENOMEM;
954 goto unreg_dev;
955 }
956
957 *vfd = m2mtest_videodev;
Marek Szyprowski07e80302010-12-20 14:39:25 -0300958 vfd->lock = &dev->dev_mutex;
Hans Verkuil8f484d82013-06-27 02:44:04 -0300959 vfd->v4l2_dev = &dev->v4l2_dev;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300960
961 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
962 if (ret) {
963 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
964 goto rel_vdev;
965 }
966
967 video_set_drvdata(vfd, dev);
968 snprintf(vfd->name, sizeof(vfd->name), "%s", m2mtest_videodev.name);
969 dev->vfd = vfd;
Hans Verkuil8f484d82013-06-27 02:44:04 -0300970 v4l2_info(&dev->v4l2_dev,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300971 "Device registered as /dev/video%d\n", vfd->num);
972
973 setup_timer(&dev->timer, device_isr, (long)dev);
974 platform_set_drvdata(pdev, dev);
975
976 dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
977 if (IS_ERR(dev->m2m_dev)) {
978 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
979 ret = PTR_ERR(dev->m2m_dev);
980 goto err_m2m;
981 }
982
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300983 return 0;
984
985err_m2m:
Sachin Kamatb7e13ef2012-09-24 02:17:45 -0300986 v4l2_m2m_release(dev->m2m_dev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300987 video_unregister_device(dev->vfd);
988rel_vdev:
989 video_device_release(vfd);
990unreg_dev:
991 v4l2_device_unregister(&dev->v4l2_dev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300992
993 return ret;
994}
995
996static int m2mtest_remove(struct platform_device *pdev)
997{
Jingoo Hanede79412013-09-09 02:54:27 -0300998 struct m2mtest_dev *dev = platform_get_drvdata(pdev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300999
1000 v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_TEST_MODULE_NAME);
1001 v4l2_m2m_release(dev->m2m_dev);
1002 del_timer_sync(&dev->timer);
1003 video_unregister_device(dev->vfd);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001004 v4l2_device_unregister(&dev->v4l2_dev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001005
1006 return 0;
1007}
1008
1009static struct platform_driver m2mtest_pdrv = {
1010 .probe = m2mtest_probe,
1011 .remove = m2mtest_remove,
1012 .driver = {
1013 .name = MEM2MEM_NAME,
1014 .owner = THIS_MODULE,
1015 },
1016};
1017
1018static void __exit m2mtest_exit(void)
1019{
1020 platform_driver_unregister(&m2mtest_pdrv);
1021 platform_device_unregister(&m2mtest_pdev);
1022}
1023
1024static int __init m2mtest_init(void)
1025{
1026 int ret;
1027
1028 ret = platform_device_register(&m2mtest_pdev);
1029 if (ret)
1030 return ret;
1031
1032 ret = platform_driver_register(&m2mtest_pdrv);
1033 if (ret)
1034 platform_device_unregister(&m2mtest_pdev);
1035
1036 return 0;
1037}
1038
1039module_init(m2mtest_init);
1040module_exit(m2mtest_exit);