blob: a1d5c15eff40aa9d83d029c9bfd3f8209cbad5c4 [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>
Marek Szyprowskid80ee382011-01-12 06:50:55 -030031#include <media/videobuf2-vmalloc.h>
Pawel Osciak96d8eab2010-04-23 05:38:38 -030032
33#define MEM2MEM_TEST_MODULE_NAME "mem2mem-testdev"
34
35MODULE_DESCRIPTION("Virtual device for mem2mem framework testing");
Pawel Osciak95072082011-03-13 15:23:32 -030036MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
Pawel Osciak96d8eab2010-04-23 05:38:38 -030037MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030038MODULE_VERSION("0.1.1");
Pawel Osciak96d8eab2010-04-23 05:38:38 -030039
40#define MIN_W 32
41#define MIN_H 32
42#define MAX_W 640
43#define MAX_H 480
Guennadi Liakhovetski9ce3ce42012-04-27 04:16:46 -030044#define DIM_ALIGN_MASK 7 /* 8-byte alignment for line length */
Pawel Osciak96d8eab2010-04-23 05:38:38 -030045
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ń80072872012-06-12 06:43:49 -030064/* Flags that indicate processing mode */
65#define MEM2MEM_HFLIP (1 << 0)
66#define MEM2MEM_VFLIP (1 << 1)
67
Pawel Osciak96d8eab2010-04-23 05:38:38 -030068#define dprintk(dev, fmt, arg...) \
69 v4l2_dbg(1, 1, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
70
71
72void m2mtest_dev_release(struct device *dev)
73{}
74
75static struct platform_device m2mtest_pdev = {
76 .name = MEM2MEM_NAME,
77 .dev.release = m2mtest_dev_release,
78};
79
80struct m2mtest_fmt {
81 char *name;
82 u32 fourcc;
83 int depth;
84 /* Types the format can be used for */
85 u32 types;
86};
87
88static 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 Verkuild3dd59c2012-07-18 10:35:37 -0300105#define NUM_FORMATS ARRAY_SIZE(formats)
106
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300107/* Per-queue, driver-specific private data */
108struct m2mtest_q_data {
109 unsigned int width;
110 unsigned int height;
111 unsigned int sizeimage;
112 struct m2mtest_fmt *fmt;
113};
114
115enum {
116 V4L2_M2M_SRC = 0,
117 V4L2_M2M_DST = 1,
118};
119
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300120#define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000)
121#define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300122
123static 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
140struct 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
153struct m2mtest_ctx {
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300154 struct v4l2_fh fh;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300155 struct m2mtest_dev *dev;
156
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300157 struct v4l2_ctrl_handler hdl;
158
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300159 /* 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ń80072872012-06-12 06:43:49 -0300170 /* Processing mode */
171 int mode;
172
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300173 struct v4l2_m2m_ctx *m2m_ctx;
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300174
175 /* Source and destination queue data */
176 struct m2mtest_q_data q_data[2];
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300177};
178
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300179static inline struct m2mtest_ctx *file2ctx(struct file *file)
180{
181 return container_of(file->private_data, struct m2mtest_ctx, fh);
182}
183
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300184static 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 Osciak96d8eab2010-04-23 05:38:38 -0300199static int device_process(struct m2mtest_ctx *ctx,
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300200 struct vb2_buffer *in_vb,
201 struct vb2_buffer *out_vb)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300202{
203 struct m2mtest_dev *dev = ctx->dev;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300204 struct m2mtest_q_data *q_data;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300205 u8 *p_in, *p_out;
206 int x, y, t, w;
207 int tile_w, bytes_left;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300208 int width, height, bytesperline;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300209
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300210 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300211
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 Osciak96d8eab2010-04-23 05:38:38 -0300218 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 Szyprowskid80ee382011-01-12 06:50:55 -0300224 if (vb2_plane_size(in_vb, 0) > vb2_plane_size(out_vb, 0)) {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300225 v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n");
226 return -EINVAL;
227 }
228
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300229 tile_w = (width * (q_data[V4L2_M2M_DST].fmt->depth >> 3))
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300230 / MEM2MEM_NUM_TILES;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300231 bytes_left = bytesperline - tile_w * MEM2MEM_NUM_TILES;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300232 w = 0;
233
Tomasz Moń80072872012-06-12 06:43:49 -0300234 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 Osciak96d8eab2010-04-23 05:38:38 -0300249 }
Tomasz Moń80072872012-06-12 06:43:49 -0300250 p_in += bytes_left;
251 p_out -= bytes_left;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300252 }
Tomasz Moń80072872012-06-12 06:43:49 -0300253 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 Osciak96d8eab2010-04-23 05:38:38 -0300312 }
313
314 return 0;
315}
316
317static 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 */
330static 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
343static 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 Szyprowskid80ee382011-01-12 06:50:55 -0300351static 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
358static 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 Osciak96d8eab2010-04-23 05:38:38 -0300366/* 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 */
372static void device_run(void *priv)
373{
374 struct m2mtest_ctx *ctx = priv;
375 struct m2mtest_dev *dev = ctx->dev;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300376 struct vb2_buffer *src_buf, *dst_buf;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300377
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 Osciak96d8eab2010-04-23 05:38:38 -0300387static void device_isr(unsigned long priv)
388{
389 struct m2mtest_dev *m2mtest_dev = (struct m2mtest_dev *)priv;
390 struct m2mtest_ctx *curr_ctx;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300391 struct vb2_buffer *src_vb, *dst_vb;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300392 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 Szyprowskid80ee382011-01-12 06:50:55 -0300402 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 Osciak96d8eab2010-04-23 05:38:38 -0300405 curr_ctx->num_processed++;
406
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300407 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 Osciak96d8eab2010-04-23 05:38:38 -0300412 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 Osciak96d8eab2010-04-23 05:38:38 -0300416 v4l2_m2m_job_finish(m2mtest_dev->m2m_dev, curr_ctx->m2m_ctx);
417 } else {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300418 device_run(curr_ctx);
419 }
420}
421
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300422/*
423 * video ioctls
424 */
425static 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 Osciak96d8eab2010-04-23 05:38:38 -0300431 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT
432 | V4L2_CAP_STREAMING;
433
434 return 0;
435}
436
437static 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
467static 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
473static 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
479static int vidioc_g_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
480{
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300481 struct vb2_queue *vq;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300482 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ń9f4161a2012-06-08 04:47:34 -0300488 q_data = get_q_data(ctx, f->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300489
490 f->fmt.pix.width = q_data->width;
491 f->fmt.pix.height = q_data->height;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300492 f->fmt.pix.field = V4L2_FIELD_NONE;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300493 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
500static int vidioc_g_fmt_vid_out(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_g_fmt_vid_cap(struct file *file, void *priv,
507 struct v4l2_format *f)
508{
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300509 return vidioc_g_fmt(file2ctx(file), f);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300510}
511
512static 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
544static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
545 struct v4l2_format *f)
546{
547 struct m2mtest_fmt *fmt;
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300548 struct m2mtest_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300549
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
561static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
562 struct v4l2_format *f)
563{
564 struct m2mtest_fmt *fmt;
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300565 struct m2mtest_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300566
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
578static int vidioc_s_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
579{
580 struct m2mtest_q_data *q_data;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300581 struct vb2_queue *vq;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300582
583 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
584 if (!vq)
585 return -EINVAL;
586
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300587 q_data = get_q_data(ctx, f->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300588 if (!q_data)
589 return -EINVAL;
590
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300591 if (vb2_is_busy(vq)) {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300592 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
Marek Szyprowski07e80302010-12-20 14:39:25 -0300593 return -EBUSY;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300594 }
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 Osciak96d8eab2010-04-23 05:38:38 -0300601
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 Szyprowski07e80302010-12-20 14:39:25 -0300606 return 0;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300607}
608
609static 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 Verkuild3dd59c2012-07-18 10:35:37 -0300618 return vidioc_s_fmt(file2ctx(file), f);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300619}
620
621static 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 Verkuild3dd59c2012-07-18 10:35:37 -0300630 return vidioc_s_fmt(file2ctx(file), f);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300631}
632
633static int vidioc_reqbufs(struct file *file, void *priv,
634 struct v4l2_requestbuffers *reqbufs)
635{
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300636 struct m2mtest_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300637
638 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
639}
640
641static int vidioc_querybuf(struct file *file, void *priv,
642 struct v4l2_buffer *buf)
643{
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300644 struct m2mtest_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300645
646 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
647}
648
649static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
650{
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300651 struct m2mtest_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300652
653 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
654}
655
656static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
657{
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300658 struct m2mtest_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300659
660 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
661}
662
663static int vidioc_streamon(struct file *file, void *priv,
664 enum v4l2_buf_type type)
665{
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300666 struct m2mtest_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300667
668 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
669}
670
671static int vidioc_streamoff(struct file *file, void *priv,
672 enum v4l2_buf_type type)
673{
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300674 struct m2mtest_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300675
676 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
677}
678
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300679static int m2mtest_s_ctrl(struct v4l2_ctrl *ctrl)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300680{
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300681 struct m2mtest_ctx *ctx =
682 container_of(ctrl->handler, struct m2mtest_ctx, hdl);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300683
684 switch (ctrl->id) {
Tomasz Moń80072872012-06-12 06:43:49 -0300685 case V4L2_CID_HFLIP:
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300686 if (ctrl->val)
Tomasz Moń80072872012-06-12 06:43:49 -0300687 ctx->mode |= MEM2MEM_HFLIP;
688 else
689 ctx->mode &= ~MEM2MEM_HFLIP;
690 break;
691
692 case V4L2_CID_VFLIP:
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300693 if (ctrl->val)
Tomasz Moń80072872012-06-12 06:43:49 -0300694 ctx->mode |= MEM2MEM_VFLIP;
695 else
696 ctx->mode &= ~MEM2MEM_VFLIP;
697 break;
698
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300699 case V4L2_CID_TRANS_TIME_MSEC:
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300700 ctx->transtime = ctrl->val;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300701 break;
702
703 case V4L2_CID_TRANS_NUM_BUFS:
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300704 ctx->translen = ctrl->val;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300705 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 Verkuild3dd59c2012-07-18 10:35:37 -0300715static const struct v4l2_ctrl_ops m2mtest_ctrl_ops = {
716 .s_ctrl = m2mtest_s_ctrl,
717};
718
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300719
720static 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 Osciak96d8eab2010-04-23 05:38:38 -0300741};
742
743
744/*
745 * Queue operations
746 */
747
Guennadi Liakhovetskifc714e702011-08-24 10:30:21 -0300748static 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 Osciak96d8eab2010-04-23 05:38:38 -0300752{
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300753 struct m2mtest_ctx *ctx = vb2_get_drv_priv(vq);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300754 struct m2mtest_q_data *q_data;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300755 unsigned int size, count = *nbuffers;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300756
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300757 q_data = get_q_data(ctx, vq->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300758
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300759 size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300760
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300761 while (size * count > MEM2MEM_VID_MEM_LIMIT)
762 (count)--;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300763
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300764 *nplanes = 1;
765 *nbuffers = count;
766 sizes[0] = size;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300767
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300768 /*
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 Osciak96d8eab2010-04-23 05:38:38 -0300774
775 return 0;
776}
777
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300778static int m2mtest_buf_prepare(struct vb2_buffer *vb)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300779{
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300780 struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300781 struct m2mtest_q_data *q_data;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300782
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300783 dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300784
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300785 q_data = get_q_data(ctx, vb->vb2_queue->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300786
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300787 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 Osciak96d8eab2010-04-23 05:38:38 -0300790 return -EINVAL;
791 }
792
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300793 vb2_set_plane_payload(vb, 0, q_data->sizeimage);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300794
795 return 0;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300796}
797
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300798static void m2mtest_buf_queue(struct vb2_buffer *vb)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300799{
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300800 struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
801 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300802}
803
Michael Olbrich0f910bf2011-07-12 09:46:44 -0300804static 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
810static 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 Szyprowskid80ee382011-01-12 06:50:55 -0300816static struct vb2_ops m2mtest_qops = {
817 .queue_setup = m2mtest_queue_setup,
818 .buf_prepare = m2mtest_buf_prepare,
819 .buf_queue = m2mtest_buf_queue,
Michael Olbrich0f910bf2011-07-12 09:46:44 -0300820 .wait_prepare = m2mtest_wait_prepare,
821 .wait_finish = m2mtest_wait_finish,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300822};
823
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300824static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300825{
826 struct m2mtest_ctx *ctx = priv;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300827 int ret;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300828
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300829 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 Osciak96d8eab2010-04-23 05:38:38 -0300850}
851
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300852static 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
863static 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 Osciak96d8eab2010-04-23 05:38:38 -0300874/*
875 * File operations
876 */
877static int m2mtest_open(struct file *file)
878{
879 struct m2mtest_dev *dev = video_drvdata(file);
880 struct m2mtest_ctx *ctx = NULL;
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300881 struct v4l2_ctrl_handler *hdl;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300882
883 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
884 if (!ctx)
885 return -ENOMEM;
886
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300887 v4l2_fh_init(&ctx->fh, video_devdata(file));
888 file->private_data = &ctx->fh;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300889 ctx->dev = dev;
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300890 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 Osciak96d8eab2010-04-23 05:38:38 -0300904
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300905 ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
906 ctx->q_data[V4L2_M2M_DST].fmt = &formats[0];
907
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300908 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
909
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300910 if (IS_ERR(ctx->m2m_ctx)) {
Dan Carpenter16ee9bb2010-05-05 02:58:57 -0300911 int ret = PTR_ERR(ctx->m2m_ctx);
912
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300913 v4l2_ctrl_handler_free(hdl);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300914 kfree(ctx);
Dan Carpenter16ee9bb2010-05-05 02:58:57 -0300915 return ret;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300916 }
917
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300918 v4l2_fh_add(&ctx->fh);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300919 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
926static int m2mtest_release(struct file *file)
927{
928 struct m2mtest_dev *dev = video_drvdata(file);
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300929 struct m2mtest_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300930
931 dprintk(dev, "Releasing instance %p\n", ctx);
932
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300933 v4l2_fh_del(&ctx->fh);
934 v4l2_fh_exit(&ctx->fh);
935 v4l2_ctrl_handler_free(&ctx->hdl);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300936 v4l2_m2m_ctx_release(ctx->m2m_ctx);
937 kfree(ctx);
938
939 atomic_dec(&dev->num_inst);
940
941 return 0;
942}
943
944static unsigned int m2mtest_poll(struct file *file,
945 struct poll_table_struct *wait)
946{
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300947 struct m2mtest_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300948
949 return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
950}
951
952static int m2mtest_mmap(struct file *file, struct vm_area_struct *vma)
953{
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300954 struct m2mtest_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300955
956 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
957}
958
959static const struct v4l2_file_operations m2mtest_fops = {
960 .owner = THIS_MODULE,
961 .open = m2mtest_open,
962 .release = m2mtest_release,
963 .poll = m2mtest_poll,
Marek Szyprowski07e80302010-12-20 14:39:25 -0300964 .unlocked_ioctl = video_ioctl2,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300965 .mmap = m2mtest_mmap,
966};
967
968static 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
976static struct v4l2_m2m_ops m2m_ops = {
977 .device_run = device_run,
978 .job_ready = job_ready,
979 .job_abort = job_abort,
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300980 .lock = m2mtest_lock,
981 .unlock = m2mtest_unlock,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300982};
983
984static 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 Verkuil5126f252012-05-10 04:57:22 -03001011 /* 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 Szyprowski07e80302010-12-20 14:39:25 -03001015 vfd->lock = &dev->dev_mutex;
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001016
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 Osciak96d8eab2010-04-23 05:38:38 -03001039 return 0;
1040
Marek Szyprowskid80ee382011-01-12 06:50:55 -03001041 v4l2_m2m_release(dev->m2m_dev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001042err_m2m:
1043 video_unregister_device(dev->vfd);
1044rel_vdev:
1045 video_device_release(vfd);
1046unreg_dev:
1047 v4l2_device_unregister(&dev->v4l2_dev);
1048free_dev:
1049 kfree(dev);
1050
1051 return ret;
1052}
1053
1054static 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 Osciak96d8eab2010-04-23 05:38:38 -03001063 v4l2_device_unregister(&dev->v4l2_dev);
1064 kfree(dev);
1065
1066 return 0;
1067}
1068
1069static 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
1078static void __exit m2mtest_exit(void)
1079{
1080 platform_driver_unregister(&m2mtest_pdrv);
1081 platform_device_unregister(&m2mtest_pdev);
1082}
1083
1084static 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
1099module_init(m2mtest_init);
1100module_exit(m2mtest_exit);
1101