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