media: vim2m: ensure that width is multiple of two
[platform/kernel/linux-starfive.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 delayed
7  * workqueue).
8  * The device is capable of multi-instance, multi-buffer-per-transaction
9  * operation (via the mem2mem framework).
10  *
11  * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
12  * Pawel Osciak, <pawel@osciak.com>
13  * Marek Szyprowski, <m.szyprowski@samsung.com>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by the
17  * Free Software Foundation; either version 2 of the
18  * License, or (at your option) any later version
19  */
20 #include <linux/module.h>
21 #include <linux/delay.h>
22 #include <linux/fs.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, "debug level");
43
44 /* Default transaction time in msec */
45 static unsigned int default_transtime = 40; /* Max 25 fps */
46 module_param(default_transtime, uint, 0644);
47 MODULE_PARM_DESC(default_transtime, "default transaction time in ms");
48
49 #define MIN_W 32
50 #define MIN_H 32
51 #define MAX_W 640
52 #define MAX_H 480
53 #define DIM_ALIGN_MASK 1 /* 2-byte alignment */
54
55 /* Flags that indicate a format can be used for capture/output */
56 #define MEM2MEM_CAPTURE (1 << 0)
57 #define MEM2MEM_OUTPUT  (1 << 1)
58
59 #define MEM2MEM_NAME            "vim2m"
60
61 /* Per queue */
62 #define MEM2MEM_DEF_NUM_BUFS    VIDEO_MAX_FRAME
63 /* In bytes, per queue */
64 #define MEM2MEM_VID_MEM_LIMIT   (16 * 1024 * 1024)
65
66 /* Flags that indicate processing mode */
67 #define MEM2MEM_HFLIP   (1 << 0)
68 #define MEM2MEM_VFLIP   (1 << 1)
69
70 #define dprintk(dev, lvl, fmt, arg...) \
71         v4l2_dbg(lvl, 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_RGB565,  /* rrrrrggg gggbbbbb */
92                 .depth  = 16,
93                 .types  = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
94         }, {
95                 .fourcc = V4L2_PIX_FMT_RGB565X, /* gggbbbbb rrrrrggg */
96                 .depth  = 16,
97                 .types  = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
98         }, {
99                 .fourcc = V4L2_PIX_FMT_RGB24,
100                 .depth  = 24,
101                 .types  = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
102         }, {
103                 .fourcc = V4L2_PIX_FMT_BGR24,
104                 .depth  = 24,
105                 .types  = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
106         }, {
107                 .fourcc = V4L2_PIX_FMT_YUYV,
108                 .depth  = 16,
109                 .types  = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
110         }, {
111                 .fourcc = V4L2_PIX_FMT_SBGGR8,
112                 .depth  = 8,
113                 .types  = MEM2MEM_CAPTURE,
114         }, {
115                 .fourcc = V4L2_PIX_FMT_SGBRG8,
116                 .depth  = 8,
117                 .types  = MEM2MEM_CAPTURE,
118         }, {
119                 .fourcc = V4L2_PIX_FMT_SGRBG8,
120                 .depth  = 8,
121                 .types  = MEM2MEM_CAPTURE,
122         }, {
123                 .fourcc = V4L2_PIX_FMT_SRGGB8,
124                 .depth  = 8,
125                 .types  = MEM2MEM_CAPTURE,
126         },
127 };
128
129 #define NUM_FORMATS ARRAY_SIZE(formats)
130
131 /* Per-queue, driver-specific private data */
132 struct vim2m_q_data {
133         unsigned int            width;
134         unsigned int            height;
135         unsigned int            sizeimage;
136         unsigned int            sequence;
137         struct vim2m_fmt        *fmt;
138 };
139
140 enum {
141         V4L2_M2M_SRC = 0,
142         V4L2_M2M_DST = 1,
143 };
144
145 #define V4L2_CID_TRANS_TIME_MSEC        (V4L2_CID_USER_BASE + 0x1000)
146 #define V4L2_CID_TRANS_NUM_BUFS         (V4L2_CID_USER_BASE + 0x1001)
147
148 static struct vim2m_fmt *find_format(struct v4l2_format *f)
149 {
150         struct vim2m_fmt *fmt;
151         unsigned int k;
152
153         for (k = 0; k < NUM_FORMATS; k++) {
154                 fmt = &formats[k];
155                 if (fmt->fourcc == f->fmt.pix.pixelformat)
156                         break;
157         }
158
159         if (k == NUM_FORMATS)
160                 return NULL;
161
162         return &formats[k];
163 }
164
165 struct vim2m_dev {
166         struct v4l2_device      v4l2_dev;
167         struct video_device     vfd;
168 #ifdef CONFIG_MEDIA_CONTROLLER
169         struct media_device     mdev;
170 #endif
171
172         atomic_t                num_inst;
173         struct mutex            dev_mutex;
174
175         struct v4l2_m2m_dev     *m2m_dev;
176 };
177
178 struct vim2m_ctx {
179         struct v4l2_fh          fh;
180         struct vim2m_dev        *dev;
181
182         struct v4l2_ctrl_handler hdl;
183
184         /* Processed buffers in this transaction */
185         u8                      num_processed;
186
187         /* Transaction length (i.e. how many buffers per transaction) */
188         u32                     translen;
189         /* Transaction time (i.e. simulated processing time) in milliseconds */
190         u32                     transtime;
191
192         struct mutex            vb_mutex;
193         struct delayed_work     work_run;
194         spinlock_t              irqlock;
195
196         /* Abort requested by m2m */
197         int                     aborting;
198
199         /* Processing mode */
200         int                     mode;
201
202         enum v4l2_colorspace    colorspace;
203         enum v4l2_ycbcr_encoding ycbcr_enc;
204         enum v4l2_xfer_func     xfer_func;
205         enum v4l2_quantization  quant;
206
207         /* Source and destination queue data */
208         struct vim2m_q_data   q_data[2];
209 };
210
211 static inline struct vim2m_ctx *file2ctx(struct file *file)
212 {
213         return container_of(file->private_data, struct vim2m_ctx, fh);
214 }
215
216 static struct vim2m_q_data *get_q_data(struct vim2m_ctx *ctx,
217                                          enum v4l2_buf_type type)
218 {
219         switch (type) {
220         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
221                 return &ctx->q_data[V4L2_M2M_SRC];
222         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
223                 return &ctx->q_data[V4L2_M2M_DST];
224         default:
225                 BUG();
226         }
227         return NULL;
228 }
229
230 static const char *type_name(enum v4l2_buf_type type)
231 {
232         switch (type) {
233         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
234                 return "Output";
235         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
236                 return "Capture";
237         default:
238                 return "Invalid";
239         }
240 }
241
242 #define CLIP(__color) \
243         (u8)(((__color) > 0xff) ? 0xff : (((__color) < 0) ? 0 : (__color)))
244
245 static void copy_two_pixels(struct vim2m_fmt *in, struct vim2m_fmt *out,
246                             u8 **src, u8 **dst, int ypos, bool reverse)
247 {
248         u8 _r[2], _g[2], _b[2], *r, *g, *b;
249         int i, step;
250
251         // If format is the same just copy the data, respecting the width
252         if (in->fourcc == out->fourcc) {
253                 int depth = out->depth >> 3;
254
255                 if (reverse) {
256                         if (in->fourcc == V4L2_PIX_FMT_YUYV) {
257                                 int u, v, y, y1;
258
259                                 *src -= 2;
260
261                                 y1 = (*src)[0]; /* copy as second point */
262                                 u  = (*src)[1];
263                                 y  = (*src)[2]; /* copy as first point */
264                                 v  = (*src)[3];
265
266                                 *src -= 2;
267
268                                 *(*dst)++ = y;
269                                 *(*dst)++ = u;
270                                 *(*dst)++ = y1;
271                                 *(*dst)++ = v;
272                                 return;
273                         }
274
275                         memcpy(*dst, *src, depth);
276                         memcpy(*dst + depth, *src - depth, depth);
277                         *src -= depth << 1;
278                 } else {
279                         memcpy(*dst, *src, depth << 1);
280                         *src += depth << 1;
281                 }
282                 *dst += depth << 1;
283                 return;
284         }
285
286         /* Step 1: read two consecutive pixels from src pointer */
287
288         r = _r;
289         g = _g;
290         b = _b;
291
292         if (reverse)
293                 step = -1;
294         else
295                 step = 1;
296
297         switch (in->fourcc) {
298         case V4L2_PIX_FMT_RGB565: /* rrrrrggg gggbbbbb */
299                 for (i = 0; i < 2; i++) {
300                         u16 pix = *(u16 *)*src;
301
302                         *r++ = (u8)(((pix & 0xf800) >> 11) << 3) | 0x07;
303                         *g++ = (u8)((((pix & 0x07e0) >> 5)) << 2) | 0x03;
304                         *b++ = (u8)((pix & 0x1f) << 3) | 0x07;
305
306                         *src += step << 1;
307                 }
308                 break;
309         case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */
310                 for (i = 0; i < 2; i++) {
311                         u16 pix = *(u16 *)*src;
312
313                         *r++ = (u8)(((0x00f8 & pix) >> 3) << 3) | 0x07;
314                         *g++ = (u8)(((pix & 0x7) << 2) |
315                                     ((pix & 0xe000) >> 5)) | 0x03;
316                         *b++ = (u8)(((pix & 0x1f00) >> 8) << 3) | 0x07;
317
318                         *src += step << 1;
319                 }
320                 break;
321         case V4L2_PIX_FMT_RGB24:
322                 for (i = 0; i < 2; i++) {
323                         *r++ = (*src)[0];
324                         *g++ = (*src)[1];
325                         *b++ = (*src)[2];
326
327                         *src += step * 3;
328                 }
329                 break;
330         case V4L2_PIX_FMT_BGR24:
331                 for (i = 0; i < 2; i++) {
332                         *b++ = (*src)[0];
333                         *g++ = (*src)[1];
334                         *r++ = (*src)[2];
335
336                         *src += step * 3;
337                 }
338                 break;
339         default: /* V4L2_PIX_FMT_YUYV */
340         {
341                 int u, v, y, y1, u1, v1, tmp;
342
343                 if (reverse) {
344                         *src -= 2;
345
346                         y1 = (*src)[0]; /* copy as second point */
347                         u  = (*src)[1];
348                         y  = (*src)[2]; /* copy as first point */
349                         v  = (*src)[3];
350
351                         *src -= 2;
352                 } else {
353                         y  = *(*src)++;
354                         u  = *(*src)++;
355                         y1 = *(*src)++;
356                         v  = *(*src)++;
357                 }
358
359                 u1 = (((u - 128) << 7) +  (u - 128)) >> 6;
360                 tmp = (((u - 128) << 1) + (u - 128) +
361                        ((v - 128) << 2) + ((v - 128) << 1)) >> 3;
362                 v1 = (((v - 128) << 1) +  (v - 128)) >> 1;
363
364                 *r++ = CLIP(y + v1);
365                 *g++ = CLIP(y - tmp);
366                 *b++ = CLIP(y + u1);
367
368                 *r = CLIP(y1 + v1);
369                 *g = CLIP(y1 - tmp);
370                 *b = CLIP(y1 + u1);
371                 break;
372         }
373         }
374
375         /* Step 2: store two consecutive points, reversing them if needed */
376
377         r = _r;
378         g = _g;
379         b = _b;
380
381         switch (out->fourcc) {
382         case V4L2_PIX_FMT_RGB565: /* rrrrrggg gggbbbbb */
383                 for (i = 0; i < 2; i++) {
384                         u16 *pix = (u16 *)*dst;
385
386                         *pix = ((*r << 8) & 0xf800) | ((*g << 3) & 0x07e0) |
387                                (*b >> 3);
388
389                         *dst += 2;
390                 }
391                 return;
392         case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */
393                 for (i = 0; i < 2; i++) {
394                         u16 *pix = (u16 *)*dst;
395                         u8 green = *g++ >> 2;
396
397                         *pix = ((green << 8) & 0xe000) | (green & 0x07) |
398                                ((*b++ << 5) & 0x1f00) | ((*r++ & 0xf8));
399
400                         *dst += 2;
401                 }
402                 return;
403         case V4L2_PIX_FMT_RGB24:
404                 for (i = 0; i < 2; i++) {
405                         *(*dst)++ = *r++;
406                         *(*dst)++ = *g++;
407                         *(*dst)++ = *b++;
408                 }
409                 return;
410         case V4L2_PIX_FMT_BGR24:
411                 for (i = 0; i < 2; i++) {
412                         *(*dst)++ = *b++;
413                         *(*dst)++ = *g++;
414                         *(*dst)++ = *r++;
415                 }
416                 return;
417         case V4L2_PIX_FMT_YUYV:
418         default:
419         {
420                 u8 y, y1, u, v;
421
422                 y = ((8453  * (*r) + 16594 * (*g) +  3223 * (*b)
423                      + 524288) >> 15);
424                 u = ((-4878 * (*r) - 9578  * (*g) + 14456 * (*b)
425                      + 4210688) >> 15);
426                 v = ((14456 * (*r++) - 12105 * (*g++) - 2351 * (*b++)
427                      + 4210688) >> 15);
428                 y1 = ((8453 * (*r) + 16594 * (*g) +  3223 * (*b)
429                      + 524288) >> 15);
430
431                 *(*dst)++ = y;
432                 *(*dst)++ = u;
433
434                 *(*dst)++ = y1;
435                 *(*dst)++ = v;
436                 return;
437         }
438         case V4L2_PIX_FMT_SBGGR8:
439                 if (!(ypos & 1)) {
440                         *(*dst)++ = *b;
441                         *(*dst)++ = *++g;
442                 } else {
443                         *(*dst)++ = *g;
444                         *(*dst)++ = *++r;
445                 }
446                 return;
447         case V4L2_PIX_FMT_SGBRG8:
448                 if (!(ypos & 1)) {
449                         *(*dst)++ = *g;
450                         *(*dst)++ = *++b;
451                 } else {
452                         *(*dst)++ = *r;
453                         *(*dst)++ = *++g;
454                 }
455                 return;
456         case V4L2_PIX_FMT_SGRBG8:
457                 if (!(ypos & 1)) {
458                         *(*dst)++ = *g;
459                         *(*dst)++ = *++r;
460                 } else {
461                         *(*dst)++ = *b;
462                         *(*dst)++ = *++g;
463                 }
464                 return;
465         case V4L2_PIX_FMT_SRGGB8:
466                 if (!(ypos & 1)) {
467                         *(*dst)++ = *r;
468                         *(*dst)++ = *++g;
469                 } else {
470                         *(*dst)++ = *g;
471                         *(*dst)++ = *++b;
472                 }
473                 return;
474         }
475 }
476
477 static int device_process(struct vim2m_ctx *ctx,
478                           struct vb2_v4l2_buffer *in_vb,
479                           struct vb2_v4l2_buffer *out_vb)
480 {
481         struct vim2m_dev *dev = ctx->dev;
482         struct vim2m_q_data *q_data_in, *q_data_out;
483         u8 *p_in, *p, *p_out;
484         int width, height, bytesperline, x, y, y_out, start, end, step;
485         struct vim2m_fmt *in, *out;
486
487         q_data_in = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
488         in = q_data_in->fmt;
489         width = q_data_in->width;
490         height = q_data_in->height;
491         bytesperline = (q_data_in->width * q_data_in->fmt->depth) >> 3;
492
493         q_data_out = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
494         out = q_data_out->fmt;
495
496         p_in = vb2_plane_vaddr(&in_vb->vb2_buf, 0);
497         p_out = vb2_plane_vaddr(&out_vb->vb2_buf, 0);
498         if (!p_in || !p_out) {
499                 v4l2_err(&dev->v4l2_dev,
500                          "Acquiring kernel pointers to buffers failed\n");
501                 return -EFAULT;
502         }
503
504         out_vb->sequence = get_q_data(ctx,
505                                       V4L2_BUF_TYPE_VIDEO_CAPTURE)->sequence++;
506         in_vb->sequence = q_data_in->sequence++;
507         v4l2_m2m_buf_copy_metadata(in_vb, out_vb, true);
508
509         if (ctx->mode & MEM2MEM_VFLIP) {
510                 start = height - 1;
511                 end = -1;
512                 step = -1;
513         } else {
514                 start = 0;
515                 end = height;
516                 step = 1;
517         }
518         y_out = 0;
519         for (y = start; y != end; y += step, y_out++) {
520                 p = p_in + (y * bytesperline);
521                 if (ctx->mode & MEM2MEM_HFLIP)
522                         p += bytesperline - (q_data_in->fmt->depth >> 3);
523
524                 for (x = 0; x < width >> 1; x++)
525                         copy_two_pixels(in, out, &p, &p_out, y_out,
526                                         ctx->mode & MEM2MEM_HFLIP);
527         }
528
529         return 0;
530 }
531
532 /*
533  * mem2mem callbacks
534  */
535
536 /*
537  * job_ready() - check whether an instance is ready to be scheduled to run
538  */
539 static int job_ready(void *priv)
540 {
541         struct vim2m_ctx *ctx = priv;
542
543         if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen
544             || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen) {
545                 dprintk(ctx->dev, 1, "Not enough buffers available\n");
546                 return 0;
547         }
548
549         return 1;
550 }
551
552 static void job_abort(void *priv)
553 {
554         struct vim2m_ctx *ctx = priv;
555
556         /* Will cancel the transaction in the next interrupt handler */
557         ctx->aborting = 1;
558 }
559
560 /* device_run() - prepares and starts the device
561  *
562  * This simulates all the immediate preparations required before starting
563  * a device. This will be called by the framework when it decides to schedule
564  * a particular instance.
565  */
566 static void device_run(void *priv)
567 {
568         struct vim2m_ctx *ctx = priv;
569         struct vb2_v4l2_buffer *src_buf, *dst_buf;
570
571         src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
572         dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
573
574         /* Apply request controls if any */
575         v4l2_ctrl_request_setup(src_buf->vb2_buf.req_obj.req,
576                                 &ctx->hdl);
577
578         device_process(ctx, src_buf, dst_buf);
579
580         /* Complete request controls if any */
581         v4l2_ctrl_request_complete(src_buf->vb2_buf.req_obj.req,
582                                    &ctx->hdl);
583
584         /* Run delayed work, which simulates a hardware irq  */
585         schedule_delayed_work(&ctx->work_run, msecs_to_jiffies(ctx->transtime));
586 }
587
588 static void device_work(struct work_struct *w)
589 {
590         struct vim2m_ctx *curr_ctx;
591         struct vim2m_dev *vim2m_dev;
592         struct vb2_v4l2_buffer *src_vb, *dst_vb;
593         unsigned long flags;
594
595         curr_ctx = container_of(w, struct vim2m_ctx, work_run.work);
596
597         if (NULL == curr_ctx) {
598                 pr_err("Instance released before the end of transaction\n");
599                 return;
600         }
601
602         vim2m_dev = curr_ctx->dev;
603
604         src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
605         dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
606
607         curr_ctx->num_processed++;
608
609         spin_lock_irqsave(&curr_ctx->irqlock, flags);
610         v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
611         v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
612         spin_unlock_irqrestore(&curr_ctx->irqlock, flags);
613
614         if (curr_ctx->num_processed == curr_ctx->translen
615             || curr_ctx->aborting) {
616                 dprintk(curr_ctx->dev, 2, "Finishing capture buffer fill\n");
617                 curr_ctx->num_processed = 0;
618                 v4l2_m2m_job_finish(vim2m_dev->m2m_dev, curr_ctx->fh.m2m_ctx);
619         } else {
620                 device_run(curr_ctx);
621         }
622 }
623
624 /*
625  * video ioctls
626  */
627 static int vidioc_querycap(struct file *file, void *priv,
628                            struct v4l2_capability *cap)
629 {
630         strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
631         strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
632         snprintf(cap->bus_info, sizeof(cap->bus_info),
633                         "platform:%s", MEM2MEM_NAME);
634         return 0;
635 }
636
637 static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
638 {
639         int i, num;
640         struct vim2m_fmt *fmt;
641
642         num = 0;
643
644         for (i = 0; i < NUM_FORMATS; ++i) {
645                 if (formats[i].types & type) {
646                         /* index-th format of type type found ? */
647                         if (num == f->index)
648                                 break;
649                         /*
650                          * Correct type but haven't reached our index yet,
651                          * just increment per-type index
652                          */
653                         ++num;
654                 }
655         }
656
657         if (i < NUM_FORMATS) {
658                 /* Format found */
659                 fmt = &formats[i];
660                 f->pixelformat = fmt->fourcc;
661                 return 0;
662         }
663
664         /* Format not found */
665         return -EINVAL;
666 }
667
668 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
669                                    struct v4l2_fmtdesc *f)
670 {
671         return enum_fmt(f, MEM2MEM_CAPTURE);
672 }
673
674 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
675                                    struct v4l2_fmtdesc *f)
676 {
677         return enum_fmt(f, MEM2MEM_OUTPUT);
678 }
679
680 static int vidioc_g_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
681 {
682         struct vb2_queue *vq;
683         struct vim2m_q_data *q_data;
684
685         vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
686         if (!vq)
687                 return -EINVAL;
688
689         q_data = get_q_data(ctx, f->type);
690
691         f->fmt.pix.width        = q_data->width;
692         f->fmt.pix.height       = q_data->height;
693         f->fmt.pix.field        = V4L2_FIELD_NONE;
694         f->fmt.pix.pixelformat  = q_data->fmt->fourcc;
695         f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
696         f->fmt.pix.sizeimage    = q_data->sizeimage;
697         f->fmt.pix.colorspace   = ctx->colorspace;
698         f->fmt.pix.xfer_func    = ctx->xfer_func;
699         f->fmt.pix.ycbcr_enc    = ctx->ycbcr_enc;
700         f->fmt.pix.quantization = ctx->quant;
701
702         return 0;
703 }
704
705 static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
706                                 struct v4l2_format *f)
707 {
708         return vidioc_g_fmt(file2ctx(file), f);
709 }
710
711 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
712                                 struct v4l2_format *f)
713 {
714         return vidioc_g_fmt(file2ctx(file), f);
715 }
716
717 static int vidioc_try_fmt(struct v4l2_format *f, struct vim2m_fmt *fmt)
718 {
719         /* V4L2 specification suggests the driver corrects the format struct
720          * if any of the dimensions is unsupported */
721         if (f->fmt.pix.height < MIN_H)
722                 f->fmt.pix.height = MIN_H;
723         else if (f->fmt.pix.height > MAX_H)
724                 f->fmt.pix.height = MAX_H;
725
726         if (f->fmt.pix.width < MIN_W)
727                 f->fmt.pix.width = MIN_W;
728         else if (f->fmt.pix.width > MAX_W)
729                 f->fmt.pix.width = MAX_W;
730
731         f->fmt.pix.width &= ~DIM_ALIGN_MASK;
732         f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
733         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
734         f->fmt.pix.field = V4L2_FIELD_NONE;
735
736         return 0;
737 }
738
739 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
740                                   struct v4l2_format *f)
741 {
742         struct vim2m_fmt *fmt;
743         struct vim2m_ctx *ctx = file2ctx(file);
744
745         fmt = find_format(f);
746         if (!fmt) {
747                 f->fmt.pix.pixelformat = formats[0].fourcc;
748                 fmt = find_format(f);
749         }
750         if (!(fmt->types & MEM2MEM_CAPTURE)) {
751                 v4l2_err(&ctx->dev->v4l2_dev,
752                          "Fourcc format (0x%08x) invalid.\n",
753                          f->fmt.pix.pixelformat);
754                 return -EINVAL;
755         }
756         f->fmt.pix.colorspace = ctx->colorspace;
757         f->fmt.pix.xfer_func = ctx->xfer_func;
758         f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
759         f->fmt.pix.quantization = ctx->quant;
760
761         return vidioc_try_fmt(f, fmt);
762 }
763
764 static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
765                                   struct v4l2_format *f)
766 {
767         struct vim2m_fmt *fmt;
768         struct vim2m_ctx *ctx = file2ctx(file);
769
770         fmt = find_format(f);
771         if (!fmt) {
772                 f->fmt.pix.pixelformat = formats[0].fourcc;
773                 fmt = find_format(f);
774         }
775         if (!(fmt->types & MEM2MEM_OUTPUT)) {
776                 v4l2_err(&ctx->dev->v4l2_dev,
777                          "Fourcc format (0x%08x) invalid.\n",
778                          f->fmt.pix.pixelformat);
779                 return -EINVAL;
780         }
781         if (!f->fmt.pix.colorspace)
782                 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
783
784         return vidioc_try_fmt(f, fmt);
785 }
786
787 static int vidioc_s_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
788 {
789         struct vim2m_q_data *q_data;
790         struct vb2_queue *vq;
791
792         vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
793         if (!vq)
794                 return -EINVAL;
795
796         q_data = get_q_data(ctx, f->type);
797         if (!q_data)
798                 return -EINVAL;
799
800         if (vb2_is_busy(vq)) {
801                 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
802                 return -EBUSY;
803         }
804
805         q_data->fmt             = find_format(f);
806         q_data->width           = f->fmt.pix.width;
807         q_data->height          = f->fmt.pix.height;
808         q_data->sizeimage       = q_data->width * q_data->height
809                                 * q_data->fmt->depth >> 3;
810
811         dprintk(ctx->dev, 1,
812                 "Format for type %s: %dx%d (%d bpp), fmt: %c%c%c%c\n",
813                 type_name(f->type), q_data->width, q_data->height,
814                 q_data->fmt->depth,
815                 (q_data->fmt->fourcc & 0xff),
816                 (q_data->fmt->fourcc >>  8) & 0xff,
817                 (q_data->fmt->fourcc >> 16) & 0xff,
818                 (q_data->fmt->fourcc >> 24) & 0xff);
819
820         return 0;
821 }
822
823 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
824                                 struct v4l2_format *f)
825 {
826         int ret;
827
828         ret = vidioc_try_fmt_vid_cap(file, priv, f);
829         if (ret)
830                 return ret;
831
832         return vidioc_s_fmt(file2ctx(file), f);
833 }
834
835 static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
836                                 struct v4l2_format *f)
837 {
838         struct vim2m_ctx *ctx = file2ctx(file);
839         int ret;
840
841         ret = vidioc_try_fmt_vid_out(file, priv, f);
842         if (ret)
843                 return ret;
844
845         ret = vidioc_s_fmt(file2ctx(file), f);
846         if (!ret) {
847                 ctx->colorspace = f->fmt.pix.colorspace;
848                 ctx->xfer_func = f->fmt.pix.xfer_func;
849                 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
850                 ctx->quant = f->fmt.pix.quantization;
851         }
852         return ret;
853 }
854
855 static int vim2m_s_ctrl(struct v4l2_ctrl *ctrl)
856 {
857         struct vim2m_ctx *ctx =
858                 container_of(ctrl->handler, struct vim2m_ctx, hdl);
859
860         switch (ctrl->id) {
861         case V4L2_CID_HFLIP:
862                 if (ctrl->val)
863                         ctx->mode |= MEM2MEM_HFLIP;
864                 else
865                         ctx->mode &= ~MEM2MEM_HFLIP;
866                 break;
867
868         case V4L2_CID_VFLIP:
869                 if (ctrl->val)
870                         ctx->mode |= MEM2MEM_VFLIP;
871                 else
872                         ctx->mode &= ~MEM2MEM_VFLIP;
873                 break;
874
875         case V4L2_CID_TRANS_TIME_MSEC:
876                 ctx->transtime = ctrl->val;
877                 if (ctx->transtime < 1)
878                         ctx->transtime = 1;
879                 break;
880
881         case V4L2_CID_TRANS_NUM_BUFS:
882                 ctx->translen = ctrl->val;
883                 break;
884
885         default:
886                 v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
887                 return -EINVAL;
888         }
889
890         return 0;
891 }
892
893 static const struct v4l2_ctrl_ops vim2m_ctrl_ops = {
894         .s_ctrl = vim2m_s_ctrl,
895 };
896
897
898 static const struct v4l2_ioctl_ops vim2m_ioctl_ops = {
899         .vidioc_querycap        = vidioc_querycap,
900
901         .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
902         .vidioc_g_fmt_vid_cap   = vidioc_g_fmt_vid_cap,
903         .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
904         .vidioc_s_fmt_vid_cap   = vidioc_s_fmt_vid_cap,
905
906         .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
907         .vidioc_g_fmt_vid_out   = vidioc_g_fmt_vid_out,
908         .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
909         .vidioc_s_fmt_vid_out   = vidioc_s_fmt_vid_out,
910
911         .vidioc_reqbufs         = v4l2_m2m_ioctl_reqbufs,
912         .vidioc_querybuf        = v4l2_m2m_ioctl_querybuf,
913         .vidioc_qbuf            = v4l2_m2m_ioctl_qbuf,
914         .vidioc_dqbuf           = v4l2_m2m_ioctl_dqbuf,
915         .vidioc_prepare_buf     = v4l2_m2m_ioctl_prepare_buf,
916         .vidioc_create_bufs     = v4l2_m2m_ioctl_create_bufs,
917         .vidioc_expbuf          = v4l2_m2m_ioctl_expbuf,
918
919         .vidioc_streamon        = v4l2_m2m_ioctl_streamon,
920         .vidioc_streamoff       = v4l2_m2m_ioctl_streamoff,
921
922         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
923         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
924 };
925
926
927 /*
928  * Queue operations
929  */
930
931 static int vim2m_queue_setup(struct vb2_queue *vq,
932                                 unsigned int *nbuffers, unsigned int *nplanes,
933                                 unsigned int sizes[], struct device *alloc_devs[])
934 {
935         struct vim2m_ctx *ctx = vb2_get_drv_priv(vq);
936         struct vim2m_q_data *q_data;
937         unsigned int size, count = *nbuffers;
938
939         q_data = get_q_data(ctx, vq->type);
940
941         size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
942
943         while (size * count > MEM2MEM_VID_MEM_LIMIT)
944                 (count)--;
945         *nbuffers = count;
946
947         if (*nplanes)
948                 return sizes[0] < size ? -EINVAL : 0;
949
950         *nplanes = 1;
951         sizes[0] = size;
952
953         dprintk(ctx->dev, 1, "%s: get %d buffer(s) of size %d each.\n",
954                 type_name(vq->type), count, size);
955
956         return 0;
957 }
958
959 static int vim2m_buf_out_validate(struct vb2_buffer *vb)
960 {
961         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
962         struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
963
964         if (vbuf->field == V4L2_FIELD_ANY)
965                 vbuf->field = V4L2_FIELD_NONE;
966         if (vbuf->field != V4L2_FIELD_NONE) {
967                 dprintk(ctx->dev, 1, "%s field isn't supported\n", __func__);
968                 return -EINVAL;
969         }
970
971         return 0;
972 }
973
974 static int vim2m_buf_prepare(struct vb2_buffer *vb)
975 {
976         struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
977         struct vim2m_q_data *q_data;
978
979         dprintk(ctx->dev, 2, "type: %s\n", type_name(vb->vb2_queue->type));
980
981         q_data = get_q_data(ctx, vb->vb2_queue->type);
982         if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
983                 dprintk(ctx->dev, 1,
984                         "%s data will not fit into plane (%lu < %lu)\n",
985                         __func__, vb2_plane_size(vb, 0),
986                         (long)q_data->sizeimage);
987                 return -EINVAL;
988         }
989
990         vb2_set_plane_payload(vb, 0, q_data->sizeimage);
991
992         return 0;
993 }
994
995 static void vim2m_buf_queue(struct vb2_buffer *vb)
996 {
997         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
998         struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
999
1000         v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1001 }
1002
1003 static int vim2m_start_streaming(struct vb2_queue *q, unsigned count)
1004 {
1005         struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
1006         struct vim2m_q_data *q_data = get_q_data(ctx, q->type);
1007
1008         q_data->sequence = 0;
1009         return 0;
1010 }
1011
1012 static void vim2m_stop_streaming(struct vb2_queue *q)
1013 {
1014         struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
1015         struct vb2_v4l2_buffer *vbuf;
1016         unsigned long flags;
1017
1018         cancel_delayed_work_sync(&ctx->work_run);
1019
1020         for (;;) {
1021                 if (V4L2_TYPE_IS_OUTPUT(q->type))
1022                         vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1023                 else
1024                         vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1025                 if (vbuf == NULL)
1026                         return;
1027                 v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
1028                                            &ctx->hdl);
1029                 spin_lock_irqsave(&ctx->irqlock, flags);
1030                 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
1031                 spin_unlock_irqrestore(&ctx->irqlock, flags);
1032         }
1033 }
1034
1035 static void vim2m_buf_request_complete(struct vb2_buffer *vb)
1036 {
1037         struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1038
1039         v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl);
1040 }
1041
1042 static const struct vb2_ops vim2m_qops = {
1043         .queue_setup     = vim2m_queue_setup,
1044         .buf_out_validate        = vim2m_buf_out_validate,
1045         .buf_prepare     = vim2m_buf_prepare,
1046         .buf_queue       = vim2m_buf_queue,
1047         .start_streaming = vim2m_start_streaming,
1048         .stop_streaming  = vim2m_stop_streaming,
1049         .wait_prepare    = vb2_ops_wait_prepare,
1050         .wait_finish     = vb2_ops_wait_finish,
1051         .buf_request_complete = vim2m_buf_request_complete,
1052 };
1053
1054 static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
1055 {
1056         struct vim2m_ctx *ctx = priv;
1057         int ret;
1058
1059         src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1060         src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1061         src_vq->drv_priv = ctx;
1062         src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1063         src_vq->ops = &vim2m_qops;
1064         src_vq->mem_ops = &vb2_vmalloc_memops;
1065         src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1066         src_vq->lock = &ctx->vb_mutex;
1067         src_vq->supports_requests = true;
1068
1069         ret = vb2_queue_init(src_vq);
1070         if (ret)
1071                 return ret;
1072
1073         dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1074         dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1075         dst_vq->drv_priv = ctx;
1076         dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1077         dst_vq->ops = &vim2m_qops;
1078         dst_vq->mem_ops = &vb2_vmalloc_memops;
1079         dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1080         dst_vq->lock = &ctx->vb_mutex;
1081
1082         return vb2_queue_init(dst_vq);
1083 }
1084
1085 static struct v4l2_ctrl_config vim2m_ctrl_trans_time_msec = {
1086         .ops = &vim2m_ctrl_ops,
1087         .id = V4L2_CID_TRANS_TIME_MSEC,
1088         .name = "Transaction Time (msec)",
1089         .type = V4L2_CTRL_TYPE_INTEGER,
1090         .min = 1,
1091         .max = 10001,
1092         .step = 1,
1093 };
1094
1095 static const struct v4l2_ctrl_config vim2m_ctrl_trans_num_bufs = {
1096         .ops = &vim2m_ctrl_ops,
1097         .id = V4L2_CID_TRANS_NUM_BUFS,
1098         .name = "Buffers Per Transaction",
1099         .type = V4L2_CTRL_TYPE_INTEGER,
1100         .def = 1,
1101         .min = 1,
1102         .max = MEM2MEM_DEF_NUM_BUFS,
1103         .step = 1,
1104 };
1105
1106 /*
1107  * File operations
1108  */
1109 static int vim2m_open(struct file *file)
1110 {
1111         struct vim2m_dev *dev = video_drvdata(file);
1112         struct vim2m_ctx *ctx = NULL;
1113         struct v4l2_ctrl_handler *hdl;
1114         int rc = 0;
1115
1116         if (mutex_lock_interruptible(&dev->dev_mutex))
1117                 return -ERESTARTSYS;
1118         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1119         if (!ctx) {
1120                 rc = -ENOMEM;
1121                 goto open_unlock;
1122         }
1123
1124         v4l2_fh_init(&ctx->fh, video_devdata(file));
1125         file->private_data = &ctx->fh;
1126         ctx->dev = dev;
1127         hdl = &ctx->hdl;
1128         v4l2_ctrl_handler_init(hdl, 4);
1129         v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
1130         v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
1131
1132         vim2m_ctrl_trans_time_msec.def = default_transtime;
1133         v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_time_msec, NULL);
1134         v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_num_bufs, NULL);
1135         if (hdl->error) {
1136                 rc = hdl->error;
1137                 v4l2_ctrl_handler_free(hdl);
1138                 kfree(ctx);
1139                 goto open_unlock;
1140         }
1141         ctx->fh.ctrl_handler = hdl;
1142         v4l2_ctrl_handler_setup(hdl);
1143
1144         ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
1145         ctx->q_data[V4L2_M2M_SRC].width = 640;
1146         ctx->q_data[V4L2_M2M_SRC].height = 480;
1147         ctx->q_data[V4L2_M2M_SRC].sizeimage =
1148                 ctx->q_data[V4L2_M2M_SRC].width *
1149                 ctx->q_data[V4L2_M2M_SRC].height *
1150                 (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3);
1151         ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
1152         ctx->colorspace = V4L2_COLORSPACE_REC709;
1153
1154         ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
1155
1156         mutex_init(&ctx->vb_mutex);
1157         spin_lock_init(&ctx->irqlock);
1158         INIT_DELAYED_WORK(&ctx->work_run, device_work);
1159
1160         if (IS_ERR(ctx->fh.m2m_ctx)) {
1161                 rc = PTR_ERR(ctx->fh.m2m_ctx);
1162
1163                 v4l2_ctrl_handler_free(hdl);
1164                 v4l2_fh_exit(&ctx->fh);
1165                 kfree(ctx);
1166                 goto open_unlock;
1167         }
1168
1169         v4l2_fh_add(&ctx->fh);
1170         atomic_inc(&dev->num_inst);
1171
1172         dprintk(dev, 1, "Created instance: %p, m2m_ctx: %p\n",
1173                 ctx, ctx->fh.m2m_ctx);
1174
1175 open_unlock:
1176         mutex_unlock(&dev->dev_mutex);
1177         return rc;
1178 }
1179
1180 static int vim2m_release(struct file *file)
1181 {
1182         struct vim2m_dev *dev = video_drvdata(file);
1183         struct vim2m_ctx *ctx = file2ctx(file);
1184
1185         dprintk(dev, 1, "Releasing instance %p\n", ctx);
1186
1187         v4l2_fh_del(&ctx->fh);
1188         v4l2_fh_exit(&ctx->fh);
1189         v4l2_ctrl_handler_free(&ctx->hdl);
1190         mutex_lock(&dev->dev_mutex);
1191         v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1192         mutex_unlock(&dev->dev_mutex);
1193         kfree(ctx);
1194
1195         atomic_dec(&dev->num_inst);
1196
1197         return 0;
1198 }
1199
1200 static const struct v4l2_file_operations vim2m_fops = {
1201         .owner          = THIS_MODULE,
1202         .open           = vim2m_open,
1203         .release        = vim2m_release,
1204         .poll           = v4l2_m2m_fop_poll,
1205         .unlocked_ioctl = video_ioctl2,
1206         .mmap           = v4l2_m2m_fop_mmap,
1207 };
1208
1209 static const struct video_device vim2m_videodev = {
1210         .name           = MEM2MEM_NAME,
1211         .vfl_dir        = VFL_DIR_M2M,
1212         .fops           = &vim2m_fops,
1213         .ioctl_ops      = &vim2m_ioctl_ops,
1214         .minor          = -1,
1215         .release        = video_device_release_empty,
1216         .device_caps    = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING,
1217 };
1218
1219 static const struct v4l2_m2m_ops m2m_ops = {
1220         .device_run     = device_run,
1221         .job_ready      = job_ready,
1222         .job_abort      = job_abort,
1223 };
1224
1225 static const struct media_device_ops m2m_media_ops = {
1226         .req_validate = vb2_request_validate,
1227         .req_queue = v4l2_m2m_request_queue,
1228 };
1229
1230 static int vim2m_probe(struct platform_device *pdev)
1231 {
1232         struct vim2m_dev *dev;
1233         struct video_device *vfd;
1234         int ret;
1235
1236         dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
1237         if (!dev)
1238                 return -ENOMEM;
1239
1240         ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1241         if (ret)
1242                 return ret;
1243
1244         atomic_set(&dev->num_inst, 0);
1245         mutex_init(&dev->dev_mutex);
1246
1247         dev->vfd = vim2m_videodev;
1248         vfd = &dev->vfd;
1249         vfd->lock = &dev->dev_mutex;
1250         vfd->v4l2_dev = &dev->v4l2_dev;
1251
1252         ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1253         if (ret) {
1254                 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1255                 goto unreg_v4l2;
1256         }
1257
1258         video_set_drvdata(vfd, dev);
1259         v4l2_info(&dev->v4l2_dev,
1260                         "Device registered as /dev/video%d\n", vfd->num);
1261
1262         platform_set_drvdata(pdev, dev);
1263
1264         dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
1265         if (IS_ERR(dev->m2m_dev)) {
1266                 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1267                 ret = PTR_ERR(dev->m2m_dev);
1268                 goto unreg_dev;
1269         }
1270
1271 #ifdef CONFIG_MEDIA_CONTROLLER
1272         dev->mdev.dev = &pdev->dev;
1273         strscpy(dev->mdev.model, "vim2m", sizeof(dev->mdev.model));
1274         strscpy(dev->mdev.bus_info, "platform:vim2m",
1275                 sizeof(dev->mdev.bus_info));
1276         media_device_init(&dev->mdev);
1277         dev->mdev.ops = &m2m_media_ops;
1278         dev->v4l2_dev.mdev = &dev->mdev;
1279
1280         ret = v4l2_m2m_register_media_controller(dev->m2m_dev,
1281                         vfd, MEDIA_ENT_F_PROC_VIDEO_SCALER);
1282         if (ret) {
1283                 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller\n");
1284                 goto unreg_m2m;
1285         }
1286
1287         ret = media_device_register(&dev->mdev);
1288         if (ret) {
1289                 v4l2_err(&dev->v4l2_dev, "Failed to register mem2mem media device\n");
1290                 goto unreg_m2m_mc;
1291         }
1292 #endif
1293         return 0;
1294
1295 #ifdef CONFIG_MEDIA_CONTROLLER
1296 unreg_m2m_mc:
1297         v4l2_m2m_unregister_media_controller(dev->m2m_dev);
1298 unreg_m2m:
1299         v4l2_m2m_release(dev->m2m_dev);
1300 #endif
1301 unreg_dev:
1302         video_unregister_device(&dev->vfd);
1303 unreg_v4l2:
1304         v4l2_device_unregister(&dev->v4l2_dev);
1305
1306         return ret;
1307 }
1308
1309 static int vim2m_remove(struct platform_device *pdev)
1310 {
1311         struct vim2m_dev *dev = platform_get_drvdata(pdev);
1312
1313         v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME);
1314
1315 #ifdef CONFIG_MEDIA_CONTROLLER
1316         media_device_unregister(&dev->mdev);
1317         v4l2_m2m_unregister_media_controller(dev->m2m_dev);
1318         media_device_cleanup(&dev->mdev);
1319 #endif
1320         v4l2_m2m_release(dev->m2m_dev);
1321         video_unregister_device(&dev->vfd);
1322         v4l2_device_unregister(&dev->v4l2_dev);
1323
1324         return 0;
1325 }
1326
1327 static struct platform_driver vim2m_pdrv = {
1328         .probe          = vim2m_probe,
1329         .remove         = vim2m_remove,
1330         .driver         = {
1331                 .name   = MEM2MEM_NAME,
1332         },
1333 };
1334
1335 static void __exit vim2m_exit(void)
1336 {
1337         platform_driver_unregister(&vim2m_pdrv);
1338         platform_device_unregister(&vim2m_pdev);
1339 }
1340
1341 static int __init vim2m_init(void)
1342 {
1343         int ret;
1344
1345         ret = platform_device_register(&vim2m_pdev);
1346         if (ret)
1347                 return ret;
1348
1349         ret = platform_driver_register(&vim2m_pdrv);
1350         if (ret)
1351                 platform_device_unregister(&vim2m_pdev);
1352
1353         return ret;
1354 }
1355
1356 module_init(vim2m_init);
1357 module_exit(vim2m_exit);