[media] mem2mem_testdev: convert to the control framework and v4l2_fh
[deliverable/linux.git] / drivers / media / video / mem2mem_testdev.c
CommitLineData
96d8eab5
PO
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.
95072084 11 * Pawel Osciak, <pawel@osciak.com>
96d8eab5
PO
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>
96d8eab5
PO
22#include <linux/timer.h>
23#include <linux/sched.h>
6b46c397 24#include <linux/slab.h>
96d8eab5
PO
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>
d3dd59c2 30#include <media/v4l2-ctrls.h>
d80ee38c 31#include <media/videobuf2-vmalloc.h>
96d8eab5
PO
32
33#define MEM2MEM_TEST_MODULE_NAME "mem2mem-testdev"
34
35MODULE_DESCRIPTION("Virtual device for mem2mem framework testing");
95072084 36MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
96d8eab5 37MODULE_LICENSE("GPL");
1990d50b 38MODULE_VERSION("0.1.1");
96d8eab5
PO
39
40#define MIN_W 32
41#define MIN_H 32
42#define MAX_W 640
43#define MAX_H 480
9ce3ce4d 44#define DIM_ALIGN_MASK 7 /* 8-byte alignment for line length */
96d8eab5
PO
45
46/* Flags that indicate a format can be used for capture/output */
47#define MEM2MEM_CAPTURE (1 << 0)
48#define MEM2MEM_OUTPUT (1 << 1)
49
50#define MEM2MEM_NAME "m2m-testdev"
51
52/* Per queue */
53#define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME
54/* In bytes, per queue */
55#define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024)
56
57/* Default transaction time in msec */
58#define MEM2MEM_DEF_TRANSTIME 1000
59/* Default number of buffers per transaction */
60#define MEM2MEM_DEF_TRANSLEN 1
61#define MEM2MEM_COLOR_STEP (0xff >> 4)
62#define MEM2MEM_NUM_TILES 8
63
80072871
TM
64/* Flags that indicate processing mode */
65#define MEM2MEM_HFLIP (1 << 0)
66#define MEM2MEM_VFLIP (1 << 1)
67
96d8eab5
PO
68#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
d3dd59c2
HV
105#define NUM_FORMATS ARRAY_SIZE(formats)
106
96d8eab5
PO
107/* 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
d3dd59c2
HV
120#define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000)
121#define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001)
96d8eab5
PO
122
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 {
d3dd59c2 154 struct v4l2_fh fh;
96d8eab5
PO
155 struct m2mtest_dev *dev;
156
d3dd59c2
HV
157 struct v4l2_ctrl_handler hdl;
158
96d8eab5
PO
159 /* Processed buffers in this transaction */
160 u8 num_processed;
161
162 /* Transaction length (i.e. how many buffers per transaction) */
163 u32 translen;
164 /* Transaction time (i.e. simulated processing time) in milliseconds */
165 u32 transtime;
166
167 /* Abort requested by m2m */
168 int aborting;
169
80072871
TM
170 /* Processing mode */
171 int mode;
172
96d8eab5 173 struct v4l2_m2m_ctx *m2m_ctx;
9f4161a6
TM
174
175 /* Source and destination queue data */
176 struct m2mtest_q_data q_data[2];
96d8eab5
PO
177};
178
d3dd59c2
HV
179static inline struct m2mtest_ctx *file2ctx(struct file *file)
180{
181 return container_of(file->private_data, struct m2mtest_ctx, fh);
182}
183
9f4161a6
TM
184static 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
96d8eab5 199static int device_process(struct m2mtest_ctx *ctx,
d80ee38c
MS
200 struct vb2_buffer *in_vb,
201 struct vb2_buffer *out_vb)
96d8eab5
PO
202{
203 struct m2mtest_dev *dev = ctx->dev;
d80ee38c 204 struct m2mtest_q_data *q_data;
96d8eab5
PO
205 u8 *p_in, *p_out;
206 int x, y, t, w;
207 int tile_w, bytes_left;
d80ee38c 208 int width, height, bytesperline;
96d8eab5 209
9f4161a6 210 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
d80ee38c
MS
211
212 width = q_data->width;
213 height = q_data->height;
214 bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
215
216 p_in = vb2_plane_vaddr(in_vb, 0);
217 p_out = vb2_plane_vaddr(out_vb, 0);
96d8eab5
PO
218 if (!p_in || !p_out) {
219 v4l2_err(&dev->v4l2_dev,
220 "Acquiring kernel pointers to buffers failed\n");
221 return -EFAULT;
222 }
223
d80ee38c 224 if (vb2_plane_size(in_vb, 0) > vb2_plane_size(out_vb, 0)) {
96d8eab5
PO
225 v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n");
226 return -EINVAL;
227 }
228
d80ee38c 229 tile_w = (width * (q_data[V4L2_M2M_DST].fmt->depth >> 3))
96d8eab5 230 / MEM2MEM_NUM_TILES;
d80ee38c 231 bytes_left = bytesperline - tile_w * MEM2MEM_NUM_TILES;
96d8eab5
PO
232 w = 0;
233
80072871
TM
234 switch (ctx->mode) {
235 case MEM2MEM_HFLIP | MEM2MEM_VFLIP:
236 p_out += bytesperline * height - bytes_left;
237 for (y = 0; y < height; ++y) {
238 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
239 if (w & 0x1) {
240 for (x = 0; x < tile_w; ++x)
241 *--p_out = *p_in++ +
242 MEM2MEM_COLOR_STEP;
243 } else {
244 for (x = 0; x < tile_w; ++x)
245 *--p_out = *p_in++ -
246 MEM2MEM_COLOR_STEP;
247 }
248 ++w;
96d8eab5 249 }
80072871
TM
250 p_in += bytes_left;
251 p_out -= bytes_left;
252 }
253 break;
254
255 case MEM2MEM_HFLIP:
256 for (y = 0; y < height; ++y) {
257 p_out += MEM2MEM_NUM_TILES * tile_w;
258 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
259 if (w & 0x01) {
260 for (x = 0; x < tile_w; ++x)
261 *--p_out = *p_in++ +
262 MEM2MEM_COLOR_STEP;
263 } else {
264 for (x = 0; x < tile_w; ++x)
265 *--p_out = *p_in++ -
266 MEM2MEM_COLOR_STEP;
267 }
268 ++w;
269 }
270 p_in += bytes_left;
271 p_out += bytesperline;
272 }
273 break;
274
275 case MEM2MEM_VFLIP:
276 p_out += bytesperline * (height - 1);
277 for (y = 0; y < height; ++y) {
278 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
279 if (w & 0x1) {
280 for (x = 0; x < tile_w; ++x)
281 *p_out++ = *p_in++ +
282 MEM2MEM_COLOR_STEP;
283 } else {
284 for (x = 0; x < tile_w; ++x)
285 *p_out++ = *p_in++ -
286 MEM2MEM_COLOR_STEP;
287 }
288 ++w;
289 }
290 p_in += bytes_left;
291 p_out += bytes_left - 2 * bytesperline;
292 }
293 break;
294
295 default:
296 for (y = 0; y < height; ++y) {
297 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
298 if (w & 0x1) {
299 for (x = 0; x < tile_w; ++x)
300 *p_out++ = *p_in++ +
301 MEM2MEM_COLOR_STEP;
302 } else {
303 for (x = 0; x < tile_w; ++x)
304 *p_out++ = *p_in++ -
305 MEM2MEM_COLOR_STEP;
306 }
307 ++w;
308 }
309 p_in += bytes_left;
310 p_out += bytes_left;
96d8eab5 311 }
96d8eab5
PO
312 }
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
d80ee38c
MS
351static 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
96d8eab5
PO
366/* device_run() - prepares and starts the device
367 *
368 * This simulates all the immediate preparations required before starting
369 * a device. This will be called by the framework when it decides to schedule
370 * a particular instance.
371 */
372static void device_run(void *priv)
373{
374 struct m2mtest_ctx *ctx = priv;
375 struct m2mtest_dev *dev = ctx->dev;
d80ee38c 376 struct vb2_buffer *src_buf, *dst_buf;
96d8eab5
PO
377
378 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
379 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
380
381 device_process(ctx, src_buf, dst_buf);
382
383 /* Run a timer, which simulates a hardware irq */
384 schedule_irq(dev, ctx->transtime);
385}
386
96d8eab5
PO
387static void device_isr(unsigned long priv)
388{
389 struct m2mtest_dev *m2mtest_dev = (struct m2mtest_dev *)priv;
390 struct m2mtest_ctx *curr_ctx;
d80ee38c 391 struct vb2_buffer *src_vb, *dst_vb;
96d8eab5
PO
392 unsigned long flags;
393
394 curr_ctx = v4l2_m2m_get_curr_priv(m2mtest_dev->m2m_dev);
395
396 if (NULL == curr_ctx) {
397 printk(KERN_ERR
398 "Instance released before the end of transaction\n");
399 return;
400 }
401
d80ee38c
MS
402 src_vb = v4l2_m2m_src_buf_remove(curr_ctx->m2m_ctx);
403 dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->m2m_ctx);
404
96d8eab5
PO
405 curr_ctx->num_processed++;
406
d80ee38c
MS
407 spin_lock_irqsave(&m2mtest_dev->irqlock, flags);
408 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
409 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
410 spin_unlock_irqrestore(&m2mtest_dev->irqlock, flags);
411
96d8eab5
PO
412 if (curr_ctx->num_processed == curr_ctx->translen
413 || curr_ctx->aborting) {
414 dprintk(curr_ctx->dev, "Finishing transaction\n");
415 curr_ctx->num_processed = 0;
96d8eab5
PO
416 v4l2_m2m_job_finish(m2mtest_dev->m2m_dev, curr_ctx->m2m_ctx);
417 } else {
96d8eab5
PO
418 device_run(curr_ctx);
419 }
420}
421
96d8eab5
PO
422/*
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;
96d8eab5
PO
431 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{
d80ee38c 481 struct vb2_queue *vq;
96d8eab5
PO
482 struct m2mtest_q_data *q_data;
483
484 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
485 if (!vq)
486 return -EINVAL;
487
9f4161a6 488 q_data = get_q_data(ctx, f->type);
96d8eab5
PO
489
490 f->fmt.pix.width = q_data->width;
491 f->fmt.pix.height = q_data->height;
d80ee38c 492 f->fmt.pix.field = V4L2_FIELD_NONE;
96d8eab5
PO
493 f->fmt.pix.pixelformat = q_data->fmt->fourcc;
494 f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
495 f->fmt.pix.sizeimage = q_data->sizeimage;
496
497 return 0;
498}
499
500static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
501 struct v4l2_format *f)
502{
d3dd59c2 503 return vidioc_g_fmt(file2ctx(file), f);
96d8eab5
PO
504}
505
506static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
507 struct v4l2_format *f)
508{
d3dd59c2 509 return vidioc_g_fmt(file2ctx(file), f);
96d8eab5
PO
510}
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;
d3dd59c2 548 struct m2mtest_ctx *ctx = file2ctx(file);
96d8eab5
PO
549
550 fmt = find_format(f);
551 if (!fmt || !(fmt->types & MEM2MEM_CAPTURE)) {
552 v4l2_err(&ctx->dev->v4l2_dev,
553 "Fourcc format (0x%08x) invalid.\n",
554 f->fmt.pix.pixelformat);
555 return -EINVAL;
556 }
557
558 return vidioc_try_fmt(f, fmt);
559}
560
561static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
562 struct v4l2_format *f)
563{
564 struct m2mtest_fmt *fmt;
d3dd59c2 565 struct m2mtest_ctx *ctx = file2ctx(file);
96d8eab5
PO
566
567 fmt = find_format(f);
568 if (!fmt || !(fmt->types & MEM2MEM_OUTPUT)) {
569 v4l2_err(&ctx->dev->v4l2_dev,
570 "Fourcc format (0x%08x) invalid.\n",
571 f->fmt.pix.pixelformat);
572 return -EINVAL;
573 }
574
575 return vidioc_try_fmt(f, fmt);
576}
577
578static int vidioc_s_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
579{
580 struct m2mtest_q_data *q_data;
d80ee38c 581 struct vb2_queue *vq;
96d8eab5
PO
582
583 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
584 if (!vq)
585 return -EINVAL;
586
9f4161a6 587 q_data = get_q_data(ctx, f->type);
96d8eab5
PO
588 if (!q_data)
589 return -EINVAL;
590
d80ee38c 591 if (vb2_is_busy(vq)) {
96d8eab5 592 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
07e80305 593 return -EBUSY;
96d8eab5
PO
594 }
595
596 q_data->fmt = find_format(f);
597 q_data->width = f->fmt.pix.width;
598 q_data->height = f->fmt.pix.height;
599 q_data->sizeimage = q_data->width * q_data->height
600 * q_data->fmt->depth >> 3;
96d8eab5
PO
601
602 dprintk(ctx->dev,
603 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
604 f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
605
07e80305 606 return 0;
96d8eab5
PO
607}
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
d3dd59c2 618 return vidioc_s_fmt(file2ctx(file), f);
96d8eab5
PO
619}
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
d3dd59c2 630 return vidioc_s_fmt(file2ctx(file), f);
96d8eab5
PO
631}
632
633static int vidioc_reqbufs(struct file *file, void *priv,
634 struct v4l2_requestbuffers *reqbufs)
635{
d3dd59c2 636 struct m2mtest_ctx *ctx = file2ctx(file);
96d8eab5
PO
637
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{
d3dd59c2 644 struct m2mtest_ctx *ctx = file2ctx(file);
96d8eab5
PO
645
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{
d3dd59c2 651 struct m2mtest_ctx *ctx = file2ctx(file);
96d8eab5
PO
652
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{
d3dd59c2 658 struct m2mtest_ctx *ctx = file2ctx(file);
96d8eab5
PO
659
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{
d3dd59c2 666 struct m2mtest_ctx *ctx = file2ctx(file);
96d8eab5
PO
667
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{
d3dd59c2 674 struct m2mtest_ctx *ctx = file2ctx(file);
96d8eab5
PO
675
676 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
677}
678
d3dd59c2 679static int m2mtest_s_ctrl(struct v4l2_ctrl *ctrl)
96d8eab5 680{
d3dd59c2
HV
681 struct m2mtest_ctx *ctx =
682 container_of(ctrl->handler, struct m2mtest_ctx, hdl);
96d8eab5
PO
683
684 switch (ctrl->id) {
80072871 685 case V4L2_CID_HFLIP:
d3dd59c2 686 if (ctrl->val)
80072871
TM
687 ctx->mode |= MEM2MEM_HFLIP;
688 else
689 ctx->mode &= ~MEM2MEM_HFLIP;
690 break;
691
692 case V4L2_CID_VFLIP:
d3dd59c2 693 if (ctrl->val)
80072871
TM
694 ctx->mode |= MEM2MEM_VFLIP;
695 else
696 ctx->mode &= ~MEM2MEM_VFLIP;
697 break;
698
96d8eab5 699 case V4L2_CID_TRANS_TIME_MSEC:
d3dd59c2 700 ctx->transtime = ctrl->val;
96d8eab5
PO
701 break;
702
703 case V4L2_CID_TRANS_NUM_BUFS:
d3dd59c2 704 ctx->translen = ctrl->val;
96d8eab5
PO
705 break;
706
707 default:
708 v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
709 return -EINVAL;
710 }
711
712 return 0;
713}
714
d3dd59c2
HV
715static const struct v4l2_ctrl_ops m2mtest_ctrl_ops = {
716 .s_ctrl = m2mtest_s_ctrl,
717};
718
96d8eab5
PO
719
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,
96d8eab5
PO
741};
742
743
744/*
745 * Queue operations
746 */
747
fc714e70
GL
748static 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[])
96d8eab5 752{
d80ee38c 753 struct m2mtest_ctx *ctx = vb2_get_drv_priv(vq);
96d8eab5 754 struct m2mtest_q_data *q_data;
d80ee38c 755 unsigned int size, count = *nbuffers;
96d8eab5 756
9f4161a6 757 q_data = get_q_data(ctx, vq->type);
96d8eab5 758
d80ee38c
MS
759 size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
760
761 while (size * count > MEM2MEM_VID_MEM_LIMIT)
762 (count)--;
96d8eab5 763
d80ee38c
MS
764 *nplanes = 1;
765 *nbuffers = count;
766 sizes[0] = size;
96d8eab5 767
d80ee38c
MS
768 /*
769 * videobuf2-vmalloc allocator is context-less so no need to set
770 * alloc_ctxs array.
771 */
96d8eab5 772
d80ee38c 773 dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
96d8eab5
PO
774
775 return 0;
776}
777
d80ee38c 778static int m2mtest_buf_prepare(struct vb2_buffer *vb)
96d8eab5 779{
d80ee38c 780 struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
96d8eab5 781 struct m2mtest_q_data *q_data;
96d8eab5 782
d80ee38c 783 dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
96d8eab5 784
9f4161a6 785 q_data = get_q_data(ctx, vb->vb2_queue->type);
96d8eab5 786
d80ee38c
MS
787 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
788 dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n",
789 __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage);
96d8eab5
PO
790 return -EINVAL;
791 }
792
d80ee38c 793 vb2_set_plane_payload(vb, 0, q_data->sizeimage);
96d8eab5
PO
794
795 return 0;
96d8eab5
PO
796}
797
d80ee38c 798static void m2mtest_buf_queue(struct vb2_buffer *vb)
96d8eab5 799{
d80ee38c
MS
800 struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
801 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
96d8eab5
PO
802}
803
0f910bf0
MO
804static 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
d80ee38c
MS
816static struct vb2_ops m2mtest_qops = {
817 .queue_setup = m2mtest_queue_setup,
818 .buf_prepare = m2mtest_buf_prepare,
819 .buf_queue = m2mtest_buf_queue,
0f910bf0
MO
820 .wait_prepare = m2mtest_wait_prepare,
821 .wait_finish = m2mtest_wait_finish,
96d8eab5
PO
822};
823
d80ee38c 824static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
96d8eab5
PO
825{
826 struct m2mtest_ctx *ctx = priv;
d80ee38c 827 int ret;
96d8eab5 828
d80ee38c
MS
829 memset(src_vq, 0, sizeof(*src_vq));
830 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
831 src_vq->io_modes = VB2_MMAP;
832 src_vq->drv_priv = ctx;
833 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
834 src_vq->ops = &m2mtest_qops;
835 src_vq->mem_ops = &vb2_vmalloc_memops;
836
837 ret = vb2_queue_init(src_vq);
838 if (ret)
839 return ret;
96d8eab5 840
d80ee38c
MS
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);
850}
96d8eab5 851
d3dd59c2
HV
852static 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
96d8eab5
PO
874/*
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;
d3dd59c2 881 struct v4l2_ctrl_handler *hdl;
96d8eab5
PO
882
883 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
884 if (!ctx)
885 return -ENOMEM;
886
d3dd59c2
HV
887 v4l2_fh_init(&ctx->fh, video_devdata(file));
888 file->private_data = &ctx->fh;
96d8eab5 889 ctx->dev = dev;
d3dd59c2
HV
890 hdl = &ctx->hdl;
891 v4l2_ctrl_handler_init(hdl, 4);
892 v4l2_ctrl_new_std(hdl, &m2mtest_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
893 v4l2_ctrl_new_std(hdl, &m2mtest_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
894 v4l2_ctrl_new_custom(hdl, &m2mtest_ctrl_trans_time_msec, NULL);
895 v4l2_ctrl_new_custom(hdl, &m2mtest_ctrl_trans_num_bufs, NULL);
896 if (hdl->error) {
897 int err = hdl->error;
898
899 v4l2_ctrl_handler_free(hdl);
900 return err;
901 }
902 ctx->fh.ctrl_handler = hdl;
903 v4l2_ctrl_handler_setup(hdl);
96d8eab5 904
9f4161a6
TM
905 ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
906 ctx->q_data[V4L2_M2M_DST].fmt = &formats[0];
907
d80ee38c
MS
908 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
909
96d8eab5 910 if (IS_ERR(ctx->m2m_ctx)) {
16ee9bb1
DC
911 int ret = PTR_ERR(ctx->m2m_ctx);
912
d3dd59c2 913 v4l2_ctrl_handler_free(hdl);
96d8eab5 914 kfree(ctx);
16ee9bb1 915 return ret;
96d8eab5
PO
916 }
917
d3dd59c2 918 v4l2_fh_add(&ctx->fh);
96d8eab5
PO
919 atomic_inc(&dev->num_inst);
920
921 dprintk(dev, "Created instance %p, m2m_ctx: %p\n", ctx, ctx->m2m_ctx);
922
923 return 0;
924}
925
926static int m2mtest_release(struct file *file)
927{
928 struct m2mtest_dev *dev = video_drvdata(file);
d3dd59c2 929 struct m2mtest_ctx *ctx = file2ctx(file);
96d8eab5
PO
930
931 dprintk(dev, "Releasing instance %p\n", ctx);
932
d3dd59c2
HV
933 v4l2_fh_del(&ctx->fh);
934 v4l2_fh_exit(&ctx->fh);
935 v4l2_ctrl_handler_free(&ctx->hdl);
96d8eab5
PO
936 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{
d3dd59c2 947 struct m2mtest_ctx *ctx = file2ctx(file);
96d8eab5
PO
948
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{
d3dd59c2 954 struct m2mtest_ctx *ctx = file2ctx(file);
96d8eab5
PO
955
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,
07e80305 964 .unlocked_ioctl = video_ioctl2,
96d8eab5
PO
965 .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,
d80ee38c
MS
980 .lock = m2mtest_lock,
981 .unlock = m2mtest_unlock,
96d8eab5
PO
982};
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;
5126f259
HV
1011 /* Locking in file operations other than ioctl should be done
1012 by the driver, not the V4L2 core.
1013 This driver needs auditing so that this flag can be removed. */
1014 set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
07e80305 1015 vfd->lock = &dev->dev_mutex;
96d8eab5
PO
1016
1017 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1018 if (ret) {
1019 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1020 goto rel_vdev;
1021 }
1022
1023 video_set_drvdata(vfd, dev);
1024 snprintf(vfd->name, sizeof(vfd->name), "%s", m2mtest_videodev.name);
1025 dev->vfd = vfd;
1026 v4l2_info(&dev->v4l2_dev, MEM2MEM_TEST_MODULE_NAME
1027 "Device registered as /dev/video%d\n", vfd->num);
1028
1029 setup_timer(&dev->timer, device_isr, (long)dev);
1030 platform_set_drvdata(pdev, dev);
1031
1032 dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
1033 if (IS_ERR(dev->m2m_dev)) {
1034 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1035 ret = PTR_ERR(dev->m2m_dev);
1036 goto err_m2m;
1037 }
1038
1039 return 0;
1040
d80ee38c 1041 v4l2_m2m_release(dev->m2m_dev);
96d8eab5
PO
1042err_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);
1063 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
This page took 1.227569 seconds and 5 git commands to generate.