]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/media/platform/vim2m.c
Merge tag 'topic/drm-misc-2016-10-24' of git://anongit.freedesktop.org/drm-intel...
[mirror_ubuntu-artful-kernel.git] / drivers / media / platform / vim2m.c
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, <pawel@osciak.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/timer.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
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>
30 #include <media/v4l2-ctrls.h>
31 #include <media/v4l2-event.h>
32 #include <media/videobuf2-vmalloc.h>
33
34 MODULE_DESCRIPTION("Virtual device for mem2mem framework testing");
35 MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
36 MODULE_LICENSE("GPL");
37 MODULE_VERSION("0.1.1");
38 MODULE_ALIAS("mem2mem_testdev");
39
40 static unsigned debug;
41 module_param(debug, uint, 0644);
42 MODULE_PARM_DESC(debug, "activates debug info");
43
44 #define MIN_W 32
45 #define MIN_H 32
46 #define MAX_W 640
47 #define MAX_H 480
48 #define DIM_ALIGN_MASK 7 /* 8-byte alignment for line length */
49
50 /* Flags that indicate a format can be used for capture/output */
51 #define MEM2MEM_CAPTURE (1 << 0)
52 #define MEM2MEM_OUTPUT (1 << 1)
53
54 #define MEM2MEM_NAME "vim2m"
55
56 /* Per queue */
57 #define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME
58 /* In bytes, per queue */
59 #define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024)
60
61 /* Default transaction time in msec */
62 #define MEM2MEM_DEF_TRANSTIME 40
63 #define MEM2MEM_COLOR_STEP (0xff >> 4)
64 #define MEM2MEM_NUM_TILES 8
65
66 /* Flags that indicate processing mode */
67 #define MEM2MEM_HFLIP (1 << 0)
68 #define MEM2MEM_VFLIP (1 << 1)
69
70 #define dprintk(dev, fmt, arg...) \
71 v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
72
73
74 static void vim2m_dev_release(struct device *dev)
75 {}
76
77 static struct platform_device vim2m_pdev = {
78 .name = MEM2MEM_NAME,
79 .dev.release = vim2m_dev_release,
80 };
81
82 struct vim2m_fmt {
83 u32 fourcc;
84 int depth;
85 /* Types the format can be used for */
86 u32 types;
87 };
88
89 static struct vim2m_fmt formats[] = {
90 {
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 .fourcc = V4L2_PIX_FMT_YUYV,
98 .depth = 16,
99 /* Output-only format */
100 .types = MEM2MEM_OUTPUT,
101 },
102 };
103
104 #define NUM_FORMATS ARRAY_SIZE(formats)
105
106 /* Per-queue, driver-specific private data */
107 struct vim2m_q_data {
108 unsigned int width;
109 unsigned int height;
110 unsigned int sizeimage;
111 unsigned int sequence;
112 struct vim2m_fmt *fmt;
113 };
114
115 enum {
116 V4L2_M2M_SRC = 0,
117 V4L2_M2M_DST = 1,
118 };
119
120 #define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000)
121 #define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001)
122
123 static struct vim2m_fmt *find_format(struct v4l2_format *f)
124 {
125 struct vim2m_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
140 struct vim2m_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
153 struct vim2m_ctx {
154 struct v4l2_fh fh;
155 struct vim2m_dev *dev;
156
157 struct v4l2_ctrl_handler hdl;
158
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
170 /* Processing mode */
171 int mode;
172
173 enum v4l2_colorspace colorspace;
174 enum v4l2_ycbcr_encoding ycbcr_enc;
175 enum v4l2_xfer_func xfer_func;
176 enum v4l2_quantization quant;
177
178 /* Source and destination queue data */
179 struct vim2m_q_data q_data[2];
180 };
181
182 static inline struct vim2m_ctx *file2ctx(struct file *file)
183 {
184 return container_of(file->private_data, struct vim2m_ctx, fh);
185 }
186
187 static struct vim2m_q_data *get_q_data(struct vim2m_ctx *ctx,
188 enum v4l2_buf_type type)
189 {
190 switch (type) {
191 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
192 return &ctx->q_data[V4L2_M2M_SRC];
193 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
194 return &ctx->q_data[V4L2_M2M_DST];
195 default:
196 BUG();
197 }
198 return NULL;
199 }
200
201
202 static int device_process(struct vim2m_ctx *ctx,
203 struct vb2_v4l2_buffer *in_vb,
204 struct vb2_v4l2_buffer *out_vb)
205 {
206 struct vim2m_dev *dev = ctx->dev;
207 struct vim2m_q_data *q_data;
208 u8 *p_in, *p_out;
209 int x, y, t, w;
210 int tile_w, bytes_left;
211 int width, height, bytesperline;
212
213 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
214
215 width = q_data->width;
216 height = q_data->height;
217 bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
218
219 p_in = vb2_plane_vaddr(&in_vb->vb2_buf, 0);
220 p_out = vb2_plane_vaddr(&out_vb->vb2_buf, 0);
221 if (!p_in || !p_out) {
222 v4l2_err(&dev->v4l2_dev,
223 "Acquiring kernel pointers to buffers failed\n");
224 return -EFAULT;
225 }
226
227 if (vb2_plane_size(&in_vb->vb2_buf, 0) >
228 vb2_plane_size(&out_vb->vb2_buf, 0)) {
229 v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n");
230 return -EINVAL;
231 }
232
233 tile_w = (width * (q_data[V4L2_M2M_DST].fmt->depth >> 3))
234 / MEM2MEM_NUM_TILES;
235 bytes_left = bytesperline - tile_w * MEM2MEM_NUM_TILES;
236 w = 0;
237
238 out_vb->sequence =
239 get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE)->sequence++;
240 in_vb->sequence = q_data->sequence++;
241 out_vb->vb2_buf.timestamp = in_vb->vb2_buf.timestamp;
242
243 if (in_vb->flags & V4L2_BUF_FLAG_TIMECODE)
244 out_vb->timecode = in_vb->timecode;
245 out_vb->field = in_vb->field;
246 out_vb->flags = in_vb->flags &
247 (V4L2_BUF_FLAG_TIMECODE |
248 V4L2_BUF_FLAG_KEYFRAME |
249 V4L2_BUF_FLAG_PFRAME |
250 V4L2_BUF_FLAG_BFRAME |
251 V4L2_BUF_FLAG_TSTAMP_SRC_MASK);
252
253 switch (ctx->mode) {
254 case MEM2MEM_HFLIP | MEM2MEM_VFLIP:
255 p_out += bytesperline * height - bytes_left;
256 for (y = 0; y < height; ++y) {
257 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
258 if (w & 0x1) {
259 for (x = 0; x < tile_w; ++x)
260 *--p_out = *p_in++ +
261 MEM2MEM_COLOR_STEP;
262 } else {
263 for (x = 0; x < tile_w; ++x)
264 *--p_out = *p_in++ -
265 MEM2MEM_COLOR_STEP;
266 }
267 ++w;
268 }
269 p_in += bytes_left;
270 p_out -= bytes_left;
271 }
272 break;
273
274 case MEM2MEM_HFLIP:
275 for (y = 0; y < height; ++y) {
276 p_out += MEM2MEM_NUM_TILES * tile_w;
277 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
278 if (w & 0x01) {
279 for (x = 0; x < tile_w; ++x)
280 *--p_out = *p_in++ +
281 MEM2MEM_COLOR_STEP;
282 } else {
283 for (x = 0; x < tile_w; ++x)
284 *--p_out = *p_in++ -
285 MEM2MEM_COLOR_STEP;
286 }
287 ++w;
288 }
289 p_in += bytes_left;
290 p_out += bytesperline;
291 }
292 break;
293
294 case MEM2MEM_VFLIP:
295 p_out += bytesperline * (height - 1);
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 - 2 * bytesperline;
311 }
312 break;
313
314 default:
315 for (y = 0; y < height; ++y) {
316 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
317 if (w & 0x1) {
318 for (x = 0; x < tile_w; ++x)
319 *p_out++ = *p_in++ +
320 MEM2MEM_COLOR_STEP;
321 } else {
322 for (x = 0; x < tile_w; ++x)
323 *p_out++ = *p_in++ -
324 MEM2MEM_COLOR_STEP;
325 }
326 ++w;
327 }
328 p_in += bytes_left;
329 p_out += bytes_left;
330 }
331 }
332
333 return 0;
334 }
335
336 static void schedule_irq(struct vim2m_dev *dev, int msec_timeout)
337 {
338 dprintk(dev, "Scheduling a simulated irq\n");
339 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(msec_timeout));
340 }
341
342 /*
343 * mem2mem callbacks
344 */
345
346 /**
347 * job_ready() - check whether an instance is ready to be scheduled to run
348 */
349 static int job_ready(void *priv)
350 {
351 struct vim2m_ctx *ctx = priv;
352
353 if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen
354 || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen) {
355 dprintk(ctx->dev, "Not enough buffers available\n");
356 return 0;
357 }
358
359 return 1;
360 }
361
362 static void job_abort(void *priv)
363 {
364 struct vim2m_ctx *ctx = priv;
365
366 /* Will cancel the transaction in the next interrupt handler */
367 ctx->aborting = 1;
368 }
369
370 /* device_run() - prepares and starts the device
371 *
372 * This simulates all the immediate preparations required before starting
373 * a device. This will be called by the framework when it decides to schedule
374 * a particular instance.
375 */
376 static void device_run(void *priv)
377 {
378 struct vim2m_ctx *ctx = priv;
379 struct vim2m_dev *dev = ctx->dev;
380 struct vb2_v4l2_buffer *src_buf, *dst_buf;
381
382 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
383 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
384
385 device_process(ctx, src_buf, dst_buf);
386
387 /* Run a timer, which simulates a hardware irq */
388 schedule_irq(dev, ctx->transtime);
389 }
390
391 static void device_isr(unsigned long priv)
392 {
393 struct vim2m_dev *vim2m_dev = (struct vim2m_dev *)priv;
394 struct vim2m_ctx *curr_ctx;
395 struct vb2_v4l2_buffer *src_vb, *dst_vb;
396 unsigned long flags;
397
398 curr_ctx = v4l2_m2m_get_curr_priv(vim2m_dev->m2m_dev);
399
400 if (NULL == curr_ctx) {
401 pr_err("Instance released before the end of transaction\n");
402 return;
403 }
404
405 src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
406 dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
407
408 curr_ctx->num_processed++;
409
410 spin_lock_irqsave(&vim2m_dev->irqlock, flags);
411 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
412 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
413 spin_unlock_irqrestore(&vim2m_dev->irqlock, flags);
414
415 if (curr_ctx->num_processed == curr_ctx->translen
416 || curr_ctx->aborting) {
417 dprintk(curr_ctx->dev, "Finishing transaction\n");
418 curr_ctx->num_processed = 0;
419 v4l2_m2m_job_finish(vim2m_dev->m2m_dev, curr_ctx->fh.m2m_ctx);
420 } else {
421 device_run(curr_ctx);
422 }
423 }
424
425 /*
426 * video ioctls
427 */
428 static int vidioc_querycap(struct file *file, void *priv,
429 struct v4l2_capability *cap)
430 {
431 strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
432 strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
433 snprintf(cap->bus_info, sizeof(cap->bus_info),
434 "platform:%s", MEM2MEM_NAME);
435 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
436 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
437 return 0;
438 }
439
440 static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
441 {
442 int i, num;
443 struct vim2m_fmt *fmt;
444
445 num = 0;
446
447 for (i = 0; i < NUM_FORMATS; ++i) {
448 if (formats[i].types & type) {
449 /* index-th format of type type found ? */
450 if (num == f->index)
451 break;
452 /* Correct type but haven't reached our index yet,
453 * just increment per-type index */
454 ++num;
455 }
456 }
457
458 if (i < NUM_FORMATS) {
459 /* Format found */
460 fmt = &formats[i];
461 f->pixelformat = fmt->fourcc;
462 return 0;
463 }
464
465 /* Format not found */
466 return -EINVAL;
467 }
468
469 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
470 struct v4l2_fmtdesc *f)
471 {
472 return enum_fmt(f, MEM2MEM_CAPTURE);
473 }
474
475 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
476 struct v4l2_fmtdesc *f)
477 {
478 return enum_fmt(f, MEM2MEM_OUTPUT);
479 }
480
481 static int vidioc_g_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
482 {
483 struct vb2_queue *vq;
484 struct vim2m_q_data *q_data;
485
486 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
487 if (!vq)
488 return -EINVAL;
489
490 q_data = get_q_data(ctx, f->type);
491
492 f->fmt.pix.width = q_data->width;
493 f->fmt.pix.height = q_data->height;
494 f->fmt.pix.field = V4L2_FIELD_NONE;
495 f->fmt.pix.pixelformat = q_data->fmt->fourcc;
496 f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
497 f->fmt.pix.sizeimage = q_data->sizeimage;
498 f->fmt.pix.colorspace = ctx->colorspace;
499 f->fmt.pix.xfer_func = ctx->xfer_func;
500 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
501 f->fmt.pix.quantization = ctx->quant;
502
503 return 0;
504 }
505
506 static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
507 struct v4l2_format *f)
508 {
509 return vidioc_g_fmt(file2ctx(file), f);
510 }
511
512 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
513 struct v4l2_format *f)
514 {
515 return vidioc_g_fmt(file2ctx(file), f);
516 }
517
518 static int vidioc_try_fmt(struct v4l2_format *f, struct vim2m_fmt *fmt)
519 {
520 /* V4L2 specification suggests the driver corrects the format struct
521 * if any of the dimensions is unsupported */
522 if (f->fmt.pix.height < MIN_H)
523 f->fmt.pix.height = MIN_H;
524 else if (f->fmt.pix.height > MAX_H)
525 f->fmt.pix.height = MAX_H;
526
527 if (f->fmt.pix.width < MIN_W)
528 f->fmt.pix.width = MIN_W;
529 else if (f->fmt.pix.width > MAX_W)
530 f->fmt.pix.width = MAX_W;
531
532 f->fmt.pix.width &= ~DIM_ALIGN_MASK;
533 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
534 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
535 f->fmt.pix.field = V4L2_FIELD_NONE;
536
537 return 0;
538 }
539
540 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
541 struct v4l2_format *f)
542 {
543 struct vim2m_fmt *fmt;
544 struct vim2m_ctx *ctx = file2ctx(file);
545
546 fmt = find_format(f);
547 if (!fmt) {
548 f->fmt.pix.pixelformat = formats[0].fourcc;
549 fmt = find_format(f);
550 }
551 if (!(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 f->fmt.pix.colorspace = ctx->colorspace;
558 f->fmt.pix.xfer_func = ctx->xfer_func;
559 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
560 f->fmt.pix.quantization = ctx->quant;
561
562 return vidioc_try_fmt(f, fmt);
563 }
564
565 static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
566 struct v4l2_format *f)
567 {
568 struct vim2m_fmt *fmt;
569 struct vim2m_ctx *ctx = file2ctx(file);
570
571 fmt = find_format(f);
572 if (!fmt) {
573 f->fmt.pix.pixelformat = formats[0].fourcc;
574 fmt = find_format(f);
575 }
576 if (!(fmt->types & MEM2MEM_OUTPUT)) {
577 v4l2_err(&ctx->dev->v4l2_dev,
578 "Fourcc format (0x%08x) invalid.\n",
579 f->fmt.pix.pixelformat);
580 return -EINVAL;
581 }
582 if (!f->fmt.pix.colorspace)
583 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
584
585 return vidioc_try_fmt(f, fmt);
586 }
587
588 static int vidioc_s_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
589 {
590 struct vim2m_q_data *q_data;
591 struct vb2_queue *vq;
592
593 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
594 if (!vq)
595 return -EINVAL;
596
597 q_data = get_q_data(ctx, f->type);
598 if (!q_data)
599 return -EINVAL;
600
601 if (vb2_is_busy(vq)) {
602 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
603 return -EBUSY;
604 }
605
606 q_data->fmt = find_format(f);
607 q_data->width = f->fmt.pix.width;
608 q_data->height = f->fmt.pix.height;
609 q_data->sizeimage = q_data->width * q_data->height
610 * q_data->fmt->depth >> 3;
611
612 dprintk(ctx->dev,
613 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
614 f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
615
616 return 0;
617 }
618
619 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
620 struct v4l2_format *f)
621 {
622 int ret;
623
624 ret = vidioc_try_fmt_vid_cap(file, priv, f);
625 if (ret)
626 return ret;
627
628 return vidioc_s_fmt(file2ctx(file), f);
629 }
630
631 static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
632 struct v4l2_format *f)
633 {
634 struct vim2m_ctx *ctx = file2ctx(file);
635 int ret;
636
637 ret = vidioc_try_fmt_vid_out(file, priv, f);
638 if (ret)
639 return ret;
640
641 ret = vidioc_s_fmt(file2ctx(file), f);
642 if (!ret) {
643 ctx->colorspace = f->fmt.pix.colorspace;
644 ctx->xfer_func = f->fmt.pix.xfer_func;
645 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
646 ctx->quant = f->fmt.pix.quantization;
647 }
648 return ret;
649 }
650
651 static int vim2m_s_ctrl(struct v4l2_ctrl *ctrl)
652 {
653 struct vim2m_ctx *ctx =
654 container_of(ctrl->handler, struct vim2m_ctx, hdl);
655
656 switch (ctrl->id) {
657 case V4L2_CID_HFLIP:
658 if (ctrl->val)
659 ctx->mode |= MEM2MEM_HFLIP;
660 else
661 ctx->mode &= ~MEM2MEM_HFLIP;
662 break;
663
664 case V4L2_CID_VFLIP:
665 if (ctrl->val)
666 ctx->mode |= MEM2MEM_VFLIP;
667 else
668 ctx->mode &= ~MEM2MEM_VFLIP;
669 break;
670
671 case V4L2_CID_TRANS_TIME_MSEC:
672 ctx->transtime = ctrl->val;
673 break;
674
675 case V4L2_CID_TRANS_NUM_BUFS:
676 ctx->translen = ctrl->val;
677 break;
678
679 default:
680 v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
681 return -EINVAL;
682 }
683
684 return 0;
685 }
686
687 static const struct v4l2_ctrl_ops vim2m_ctrl_ops = {
688 .s_ctrl = vim2m_s_ctrl,
689 };
690
691
692 static const struct v4l2_ioctl_ops vim2m_ioctl_ops = {
693 .vidioc_querycap = vidioc_querycap,
694
695 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
696 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
697 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
698 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
699
700 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
701 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
702 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
703 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
704
705 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
706 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
707 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
708 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
709 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
710 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
711 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
712
713 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
714 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
715
716 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
717 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
718 };
719
720
721 /*
722 * Queue operations
723 */
724
725 static int vim2m_queue_setup(struct vb2_queue *vq,
726 unsigned int *nbuffers, unsigned int *nplanes,
727 unsigned int sizes[], struct device *alloc_devs[])
728 {
729 struct vim2m_ctx *ctx = vb2_get_drv_priv(vq);
730 struct vim2m_q_data *q_data;
731 unsigned int size, count = *nbuffers;
732
733 q_data = get_q_data(ctx, vq->type);
734
735 size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
736
737 while (size * count > MEM2MEM_VID_MEM_LIMIT)
738 (count)--;
739 *nbuffers = count;
740
741 if (*nplanes)
742 return sizes[0] < size ? -EINVAL : 0;
743
744 *nplanes = 1;
745 sizes[0] = size;
746
747 dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
748
749 return 0;
750 }
751
752 static int vim2m_buf_prepare(struct vb2_buffer *vb)
753 {
754 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
755 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
756 struct vim2m_q_data *q_data;
757
758 dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
759
760 q_data = get_q_data(ctx, vb->vb2_queue->type);
761 if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
762 if (vbuf->field == V4L2_FIELD_ANY)
763 vbuf->field = V4L2_FIELD_NONE;
764 if (vbuf->field != V4L2_FIELD_NONE) {
765 dprintk(ctx->dev, "%s field isn't supported\n",
766 __func__);
767 return -EINVAL;
768 }
769 }
770
771 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
772 dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n",
773 __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage);
774 return -EINVAL;
775 }
776
777 vb2_set_plane_payload(vb, 0, q_data->sizeimage);
778
779 return 0;
780 }
781
782 static void vim2m_buf_queue(struct vb2_buffer *vb)
783 {
784 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
785 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
786
787 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
788 }
789
790 static int vim2m_start_streaming(struct vb2_queue *q, unsigned count)
791 {
792 struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
793 struct vim2m_q_data *q_data = get_q_data(ctx, q->type);
794
795 q_data->sequence = 0;
796 return 0;
797 }
798
799 static void vim2m_stop_streaming(struct vb2_queue *q)
800 {
801 struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
802 struct vb2_v4l2_buffer *vbuf;
803 unsigned long flags;
804
805 for (;;) {
806 if (V4L2_TYPE_IS_OUTPUT(q->type))
807 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
808 else
809 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
810 if (vbuf == NULL)
811 return;
812 spin_lock_irqsave(&ctx->dev->irqlock, flags);
813 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
814 spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
815 }
816 }
817
818 static const struct vb2_ops vim2m_qops = {
819 .queue_setup = vim2m_queue_setup,
820 .buf_prepare = vim2m_buf_prepare,
821 .buf_queue = vim2m_buf_queue,
822 .start_streaming = vim2m_start_streaming,
823 .stop_streaming = vim2m_stop_streaming,
824 .wait_prepare = vb2_ops_wait_prepare,
825 .wait_finish = vb2_ops_wait_finish,
826 };
827
828 static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
829 {
830 struct vim2m_ctx *ctx = priv;
831 int ret;
832
833 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
834 src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
835 src_vq->drv_priv = ctx;
836 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
837 src_vq->ops = &vim2m_qops;
838 src_vq->mem_ops = &vb2_vmalloc_memops;
839 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
840 src_vq->lock = &ctx->dev->dev_mutex;
841
842 ret = vb2_queue_init(src_vq);
843 if (ret)
844 return ret;
845
846 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
847 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
848 dst_vq->drv_priv = ctx;
849 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
850 dst_vq->ops = &vim2m_qops;
851 dst_vq->mem_ops = &vb2_vmalloc_memops;
852 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
853 dst_vq->lock = &ctx->dev->dev_mutex;
854
855 return vb2_queue_init(dst_vq);
856 }
857
858 static const struct v4l2_ctrl_config vim2m_ctrl_trans_time_msec = {
859 .ops = &vim2m_ctrl_ops,
860 .id = V4L2_CID_TRANS_TIME_MSEC,
861 .name = "Transaction Time (msec)",
862 .type = V4L2_CTRL_TYPE_INTEGER,
863 .def = MEM2MEM_DEF_TRANSTIME,
864 .min = 1,
865 .max = 10001,
866 .step = 1,
867 };
868
869 static const struct v4l2_ctrl_config vim2m_ctrl_trans_num_bufs = {
870 .ops = &vim2m_ctrl_ops,
871 .id = V4L2_CID_TRANS_NUM_BUFS,
872 .name = "Buffers Per Transaction",
873 .type = V4L2_CTRL_TYPE_INTEGER,
874 .def = 1,
875 .min = 1,
876 .max = MEM2MEM_DEF_NUM_BUFS,
877 .step = 1,
878 };
879
880 /*
881 * File operations
882 */
883 static int vim2m_open(struct file *file)
884 {
885 struct vim2m_dev *dev = video_drvdata(file);
886 struct vim2m_ctx *ctx = NULL;
887 struct v4l2_ctrl_handler *hdl;
888 int rc = 0;
889
890 if (mutex_lock_interruptible(&dev->dev_mutex))
891 return -ERESTARTSYS;
892 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
893 if (!ctx) {
894 rc = -ENOMEM;
895 goto open_unlock;
896 }
897
898 v4l2_fh_init(&ctx->fh, video_devdata(file));
899 file->private_data = &ctx->fh;
900 ctx->dev = dev;
901 hdl = &ctx->hdl;
902 v4l2_ctrl_handler_init(hdl, 4);
903 v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
904 v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
905 v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_time_msec, NULL);
906 v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_num_bufs, NULL);
907 if (hdl->error) {
908 rc = hdl->error;
909 v4l2_ctrl_handler_free(hdl);
910 goto open_unlock;
911 }
912 ctx->fh.ctrl_handler = hdl;
913 v4l2_ctrl_handler_setup(hdl);
914
915 ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
916 ctx->q_data[V4L2_M2M_SRC].width = 640;
917 ctx->q_data[V4L2_M2M_SRC].height = 480;
918 ctx->q_data[V4L2_M2M_SRC].sizeimage =
919 ctx->q_data[V4L2_M2M_SRC].width *
920 ctx->q_data[V4L2_M2M_SRC].height *
921 (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3);
922 ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
923 ctx->colorspace = V4L2_COLORSPACE_REC709;
924
925 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
926
927 if (IS_ERR(ctx->fh.m2m_ctx)) {
928 rc = PTR_ERR(ctx->fh.m2m_ctx);
929
930 v4l2_ctrl_handler_free(hdl);
931 kfree(ctx);
932 goto open_unlock;
933 }
934
935 v4l2_fh_add(&ctx->fh);
936 atomic_inc(&dev->num_inst);
937
938 dprintk(dev, "Created instance: %p, m2m_ctx: %p\n",
939 ctx, ctx->fh.m2m_ctx);
940
941 open_unlock:
942 mutex_unlock(&dev->dev_mutex);
943 return rc;
944 }
945
946 static int vim2m_release(struct file *file)
947 {
948 struct vim2m_dev *dev = video_drvdata(file);
949 struct vim2m_ctx *ctx = file2ctx(file);
950
951 dprintk(dev, "Releasing instance %p\n", ctx);
952
953 v4l2_fh_del(&ctx->fh);
954 v4l2_fh_exit(&ctx->fh);
955 v4l2_ctrl_handler_free(&ctx->hdl);
956 mutex_lock(&dev->dev_mutex);
957 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
958 mutex_unlock(&dev->dev_mutex);
959 kfree(ctx);
960
961 atomic_dec(&dev->num_inst);
962
963 return 0;
964 }
965
966 static const struct v4l2_file_operations vim2m_fops = {
967 .owner = THIS_MODULE,
968 .open = vim2m_open,
969 .release = vim2m_release,
970 .poll = v4l2_m2m_fop_poll,
971 .unlocked_ioctl = video_ioctl2,
972 .mmap = v4l2_m2m_fop_mmap,
973 };
974
975 static struct video_device vim2m_videodev = {
976 .name = MEM2MEM_NAME,
977 .vfl_dir = VFL_DIR_M2M,
978 .fops = &vim2m_fops,
979 .ioctl_ops = &vim2m_ioctl_ops,
980 .minor = -1,
981 .release = video_device_release_empty,
982 };
983
984 static struct v4l2_m2m_ops m2m_ops = {
985 .device_run = device_run,
986 .job_ready = job_ready,
987 .job_abort = job_abort,
988 };
989
990 static int vim2m_probe(struct platform_device *pdev)
991 {
992 struct vim2m_dev *dev;
993 struct video_device *vfd;
994 int ret;
995
996 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
997 if (!dev)
998 return -ENOMEM;
999
1000 spin_lock_init(&dev->irqlock);
1001
1002 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1003 if (ret)
1004 return ret;
1005
1006 atomic_set(&dev->num_inst, 0);
1007 mutex_init(&dev->dev_mutex);
1008
1009 dev->vfd = vim2m_videodev;
1010 vfd = &dev->vfd;
1011 vfd->lock = &dev->dev_mutex;
1012 vfd->v4l2_dev = &dev->v4l2_dev;
1013
1014 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1015 if (ret) {
1016 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1017 goto unreg_dev;
1018 }
1019
1020 video_set_drvdata(vfd, dev);
1021 snprintf(vfd->name, sizeof(vfd->name), "%s", vim2m_videodev.name);
1022 v4l2_info(&dev->v4l2_dev,
1023 "Device registered as /dev/video%d\n", vfd->num);
1024
1025 setup_timer(&dev->timer, device_isr, (long)dev);
1026 platform_set_drvdata(pdev, dev);
1027
1028 dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
1029 if (IS_ERR(dev->m2m_dev)) {
1030 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1031 ret = PTR_ERR(dev->m2m_dev);
1032 goto err_m2m;
1033 }
1034
1035 return 0;
1036
1037 err_m2m:
1038 v4l2_m2m_release(dev->m2m_dev);
1039 video_unregister_device(&dev->vfd);
1040 unreg_dev:
1041 v4l2_device_unregister(&dev->v4l2_dev);
1042
1043 return ret;
1044 }
1045
1046 static int vim2m_remove(struct platform_device *pdev)
1047 {
1048 struct vim2m_dev *dev = platform_get_drvdata(pdev);
1049
1050 v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME);
1051 v4l2_m2m_release(dev->m2m_dev);
1052 del_timer_sync(&dev->timer);
1053 video_unregister_device(&dev->vfd);
1054 v4l2_device_unregister(&dev->v4l2_dev);
1055
1056 return 0;
1057 }
1058
1059 static struct platform_driver vim2m_pdrv = {
1060 .probe = vim2m_probe,
1061 .remove = vim2m_remove,
1062 .driver = {
1063 .name = MEM2MEM_NAME,
1064 },
1065 };
1066
1067 static void __exit vim2m_exit(void)
1068 {
1069 platform_driver_unregister(&vim2m_pdrv);
1070 platform_device_unregister(&vim2m_pdev);
1071 }
1072
1073 static int __init vim2m_init(void)
1074 {
1075 int ret;
1076
1077 ret = platform_device_register(&vim2m_pdev);
1078 if (ret)
1079 return ret;
1080
1081 ret = platform_driver_register(&vim2m_pdrv);
1082 if (ret)
1083 platform_device_unregister(&vim2m_pdev);
1084
1085 return ret;
1086 }
1087
1088 module_init(vim2m_init);
1089 module_exit(vim2m_exit);