xref: /linux/drivers/media/test-drivers/vim2m.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * A virtual v4l2-mem2mem example device.
4  *
5  * This is a virtual device driver for testing mem-to-mem vb2 framework.
6  * It simulates a device that uses memory buffers for both source and
7  * destination, processes the data and issues an "irq" (simulated by a delayed
8  * workqueue).
9  * The device is capable of multi-instance, multi-buffer-per-transaction
10  * operation (via the mem2mem framework).
11  *
12  * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
13  * Pawel Osciak, <pawel@osciak.com>
14  * Marek Szyprowski, <m.szyprowski@samsung.com>
15  */
16 #include <linux/module.h>
17 #include <linux/delay.h>
18 #include <linux/fs.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 
22 #include <linux/platform_device.h>
23 #include <media/v4l2-mem2mem.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-event.h>
28 #include <media/videobuf2-vmalloc.h>
29 #include <media/v4l2-common.h>
30 
31 MODULE_DESCRIPTION("Virtual device for mem2mem framework testing");
32 MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
33 MODULE_LICENSE("GPL");
34 MODULE_VERSION("0.2");
35 MODULE_ALIAS("mem2mem_testdev");
36 
37 static unsigned int debug;
38 module_param(debug, uint, 0644);
39 MODULE_PARM_DESC(debug, "debug level");
40 
41 /* Default transaction time in msec */
42 static unsigned int default_transtime = 40; /* Max 25 fps */
43 module_param(default_transtime, uint, 0644);
44 MODULE_PARM_DESC(default_transtime, "default transaction time in ms");
45 
46 static unsigned int multiplanar = 1;
47 module_param(multiplanar, uint, 0644);
48 MODULE_PARM_DESC(multiplanar, "1 (default) creates a single planar device, 2 creates multiplanar device.");
49 
50 #define MIN_W 32
51 #define MIN_H 32
52 #define MAX_W 640
53 #define MAX_H 480
54 
55 /* Pixel alignment for non-bayer formats */
56 #define WIDTH_ALIGN 2
57 #define HEIGHT_ALIGN 1
58 
59 /* Pixel alignment for bayer formats */
60 #define BAYER_WIDTH_ALIGN  2
61 #define BAYER_HEIGHT_ALIGN 2
62 
63 /* Flags that indicate a format can be used for capture/output */
64 #define MEM2MEM_CAPTURE	BIT(0)
65 #define MEM2MEM_OUTPUT	BIT(1)
66 
67 #define MEM2MEM_NAME		"vim2m"
68 
69 /* Per queue */
70 #define MEM2MEM_DEF_NUM_BUFS	VIDEO_MAX_FRAME
71 /* In bytes, per queue */
72 #define MEM2MEM_VID_MEM_LIMIT	(16 * 1024 * 1024)
73 
74 /* Flags that indicate processing mode */
75 #define MEM2MEM_HFLIP	BIT(0)
76 #define MEM2MEM_VFLIP	BIT(1)
77 
78 #define dprintk(dev, lvl, fmt, arg...) \
79 	v4l2_dbg(lvl, debug, &(dev)->v4l2_dev, "%s: " fmt, __func__, ## arg)
80 
81 static void vim2m_dev_release(struct device *dev)
82 {}
83 
84 static struct platform_device vim2m_pdev = {
85 	.name		= MEM2MEM_NAME,
86 	.dev.release	= vim2m_dev_release,
87 };
88 
89 struct vim2m_fmt {
90 	u32	fourcc;
91 	int	depth;
92 	/* Types the format can be used for */
93 	u32     types;
94 };
95 
96 static struct vim2m_fmt formats[] = {
97 	{
98 		.fourcc	= V4L2_PIX_FMT_RGB565,  /* rrrrrggg gggbbbbb */
99 		.depth	= 16,
100 		.types  = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
101 	}, {
102 		.fourcc	= V4L2_PIX_FMT_RGB565X, /* gggbbbbb rrrrrggg */
103 		.depth	= 16,
104 		.types  = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
105 	}, {
106 		.fourcc	= V4L2_PIX_FMT_RGB24,
107 		.depth	= 24,
108 		.types  = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
109 	}, {
110 		.fourcc	= V4L2_PIX_FMT_BGR24,
111 		.depth	= 24,
112 		.types  = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
113 	}, {
114 		.fourcc	= V4L2_PIX_FMT_YUYV,
115 		.depth	= 16,
116 		.types  = MEM2MEM_CAPTURE,
117 	}, {
118 		.fourcc	= V4L2_PIX_FMT_SBGGR8,
119 		.depth	= 8,
120 		.types  = MEM2MEM_CAPTURE,
121 	}, {
122 		.fourcc	= V4L2_PIX_FMT_SGBRG8,
123 		.depth	= 8,
124 		.types  = MEM2MEM_CAPTURE,
125 	}, {
126 		.fourcc	= V4L2_PIX_FMT_SGRBG8,
127 		.depth	= 8,
128 		.types  = MEM2MEM_CAPTURE,
129 	}, {
130 		.fourcc	= V4L2_PIX_FMT_SRGGB8,
131 		.depth	= 8,
132 		.types  = MEM2MEM_CAPTURE,
133 	},
134 };
135 
136 #define NUM_FORMATS ARRAY_SIZE(formats)
137 
138 /* Per-queue, driver-specific private data */
139 struct vim2m_q_data {
140 	unsigned int		width;
141 	unsigned int		height;
142 	unsigned int            num_mem_planes;
143 	unsigned int		sizeimage[VIDEO_MAX_PLANES];
144 	unsigned int		sequence;
145 	struct vim2m_fmt	*fmt;
146 };
147 
148 enum {
149 	V4L2_M2M_SRC = 0,
150 	V4L2_M2M_DST = 1,
151 };
152 
153 #define V4L2_CID_TRANS_TIME_MSEC	(V4L2_CID_USER_BASE + 0x1000)
154 #define V4L2_CID_TRANS_NUM_BUFS		(V4L2_CID_USER_BASE + 0x1001)
155 
156 static struct vim2m_fmt *find_format(u32 fourcc)
157 {
158 	struct vim2m_fmt *fmt;
159 	unsigned int k;
160 
161 	for (k = 0; k < NUM_FORMATS; k++) {
162 		fmt = &formats[k];
163 		if (fmt->fourcc == fourcc)
164 			break;
165 	}
166 
167 	if (k == NUM_FORMATS)
168 		return NULL;
169 
170 	return &formats[k];
171 }
172 
173 static void get_alignment(u32 fourcc,
174 			  unsigned int *walign, unsigned int *halign)
175 {
176 	switch (fourcc) {
177 	case V4L2_PIX_FMT_SBGGR8:
178 	case V4L2_PIX_FMT_SGBRG8:
179 	case V4L2_PIX_FMT_SGRBG8:
180 	case V4L2_PIX_FMT_SRGGB8:
181 		*walign = BAYER_WIDTH_ALIGN;
182 		*halign = BAYER_HEIGHT_ALIGN;
183 		return;
184 	default:
185 		*walign = WIDTH_ALIGN;
186 		*halign = HEIGHT_ALIGN;
187 		return;
188 	}
189 }
190 
191 struct vim2m_dev {
192 	struct v4l2_device	v4l2_dev;
193 	struct video_device	vfd;
194 	struct media_device	mdev;
195 
196 	atomic_t		num_inst;
197 	struct mutex		dev_mutex;
198 
199 	struct v4l2_m2m_dev	*m2m_dev;
200 	bool			multiplanar;
201 };
202 
203 struct vim2m_ctx {
204 	struct v4l2_fh		fh;
205 	struct vim2m_dev	*dev;
206 
207 	struct v4l2_ctrl_handler hdl;
208 
209 	/* Processed buffers in this transaction */
210 	u8			num_processed;
211 
212 	/* Transaction length (i.e. how many buffers per transaction) */
213 	u32			translen;
214 	/* Transaction time (i.e. simulated processing time) in milliseconds */
215 	u32			transtime;
216 
217 	struct mutex		vb_mutex;
218 	struct delayed_work	work_run;
219 
220 	/* Abort requested by m2m */
221 	int			aborting;
222 
223 	/* Processing mode */
224 	int			mode;
225 
226 	enum v4l2_colorspace	colorspace;
227 	enum v4l2_ycbcr_encoding ycbcr_enc;
228 	enum v4l2_xfer_func	xfer_func;
229 	enum v4l2_quantization	quant;
230 
231 	/* Source and destination queue data */
232 	struct vim2m_q_data   q_data[2];
233 };
234 
235 static inline struct vim2m_ctx *file2ctx(struct file *file)
236 {
237 	return container_of(file_to_v4l2_fh(file), struct vim2m_ctx, fh);
238 }
239 
240 static struct vim2m_q_data *get_q_data(struct vim2m_ctx *ctx,
241 				       enum v4l2_buf_type type)
242 {
243 	switch (type) {
244 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
245 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
246 		return &ctx->q_data[V4L2_M2M_SRC];
247 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
248 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
249 		return &ctx->q_data[V4L2_M2M_DST];
250 	default:
251 		return NULL;
252 	}
253 }
254 
255 static const char *type_name(enum v4l2_buf_type type)
256 {
257 	switch (type) {
258 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
259 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
260 		return "Output";
261 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
262 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
263 		return "Capture";
264 	default:
265 		return "Invalid";
266 	}
267 }
268 
269 static void copy_line(struct vim2m_q_data *q_data_out,
270 		      u8 *src, u8 *dst, bool reverse)
271 {
272 	int x, depth = q_data_out->fmt->depth >> 3;
273 
274 	if (!reverse) {
275 		memcpy(dst, src, q_data_out->width * depth);
276 	} else {
277 		for (x = 0; x < q_data_out->width >> 1; x++) {
278 			memcpy(dst, src, depth);
279 			memcpy(dst + depth, src - depth, depth);
280 			src -= depth << 1;
281 			dst += depth << 1;
282 		}
283 		return;
284 	}
285 }
286 
287 static void copy_two_pixels(struct vim2m_q_data *q_data_in,
288 			    struct vim2m_q_data *q_data_out,
289 			    u8 *src[2], u8 **dst, int ypos, bool reverse)
290 {
291 	struct vim2m_fmt *out = q_data_out->fmt;
292 	struct vim2m_fmt *in = q_data_in->fmt;
293 	u8 _r[2], _g[2], _b[2], *r, *g, *b;
294 	int i;
295 
296 	/* Step 1: read two consecutive pixels from src pointer */
297 
298 	r = _r;
299 	g = _g;
300 	b = _b;
301 
302 	switch (in->fourcc) {
303 	case V4L2_PIX_FMT_RGB565: /* rrrrrggg gggbbbbb */
304 		for (i = 0; i < 2; i++) {
305 			u16 pix = le16_to_cpu(*(__le16 *)(src[i]));
306 
307 			*r++ = (u8)(((pix & 0xf800) >> 11) << 3) | 0x07;
308 			*g++ = (u8)((((pix & 0x07e0) >> 5)) << 2) | 0x03;
309 			*b++ = (u8)((pix & 0x1f) << 3) | 0x07;
310 		}
311 		break;
312 	case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */
313 		for (i = 0; i < 2; i++) {
314 			u16 pix = be16_to_cpu(*(__be16 *)(src[i]));
315 
316 			*r++ = (u8)(((pix & 0xf800) >> 11) << 3) | 0x07;
317 			*g++ = (u8)((((pix & 0x07e0) >> 5)) << 2) | 0x03;
318 			*b++ = (u8)((pix & 0x1f) << 3) | 0x07;
319 		}
320 		break;
321 	default:
322 	case V4L2_PIX_FMT_RGB24:
323 		for (i = 0; i < 2; i++) {
324 			*r++ = src[i][0];
325 			*g++ = src[i][1];
326 			*b++ = src[i][2];
327 		}
328 		break;
329 	case V4L2_PIX_FMT_BGR24:
330 		for (i = 0; i < 2; i++) {
331 			*b++ = src[i][0];
332 			*g++ = src[i][1];
333 			*r++ = src[i][2];
334 		}
335 		break;
336 	}
337 
338 	/* Step 2: store two consecutive points, reversing them if needed */
339 
340 	r = _r;
341 	g = _g;
342 	b = _b;
343 
344 	switch (out->fourcc) {
345 	case V4L2_PIX_FMT_RGB565: /* rrrrrggg gggbbbbb */
346 		for (i = 0; i < 2; i++) {
347 			u16 pix;
348 			__le16 *dst_pix = (__le16 *)*dst;
349 
350 			pix = ((*r << 8) & 0xf800) | ((*g << 3) & 0x07e0) |
351 			      (*b >> 3);
352 
353 			*dst_pix = cpu_to_le16(pix);
354 
355 			*dst += 2;
356 		}
357 		return;
358 	case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */
359 		for (i = 0; i < 2; i++) {
360 			u16 pix;
361 			__be16 *dst_pix = (__be16 *)*dst;
362 
363 			pix = ((*r << 8) & 0xf800) | ((*g << 3) & 0x07e0) |
364 			      (*b >> 3);
365 
366 			*dst_pix = cpu_to_be16(pix);
367 
368 			*dst += 2;
369 		}
370 		return;
371 	case V4L2_PIX_FMT_RGB24:
372 		for (i = 0; i < 2; i++) {
373 			*(*dst)++ = *r++;
374 			*(*dst)++ = *g++;
375 			*(*dst)++ = *b++;
376 		}
377 		return;
378 	case V4L2_PIX_FMT_BGR24:
379 		for (i = 0; i < 2; i++) {
380 			*(*dst)++ = *b++;
381 			*(*dst)++ = *g++;
382 			*(*dst)++ = *r++;
383 		}
384 		return;
385 	case V4L2_PIX_FMT_YUYV:
386 	default:
387 	{
388 		u8 y, y1, u, v;
389 
390 		y = ((8453  * (*r) + 16594 * (*g) +  3223 * (*b)
391 		     + 524288) >> 15);
392 		u = ((-4878 * (*r) - 9578  * (*g) + 14456 * (*b)
393 		     + 4210688) >> 15);
394 		v = ((14456 * (*r++) - 12105 * (*g++) - 2351 * (*b++)
395 		     + 4210688) >> 15);
396 		y1 = ((8453 * (*r) + 16594 * (*g) +  3223 * (*b)
397 		     + 524288) >> 15);
398 
399 		*(*dst)++ = y;
400 		*(*dst)++ = u;
401 
402 		*(*dst)++ = y1;
403 		*(*dst)++ = v;
404 		return;
405 	}
406 	case V4L2_PIX_FMT_SBGGR8:
407 		if (!(ypos & 1)) {
408 			*(*dst)++ = *b;
409 			*(*dst)++ = *++g;
410 		} else {
411 			*(*dst)++ = *g;
412 			*(*dst)++ = *++r;
413 		}
414 		return;
415 	case V4L2_PIX_FMT_SGBRG8:
416 		if (!(ypos & 1)) {
417 			*(*dst)++ = *g;
418 			*(*dst)++ = *++b;
419 		} else {
420 			*(*dst)++ = *r;
421 			*(*dst)++ = *++g;
422 		}
423 		return;
424 	case V4L2_PIX_FMT_SGRBG8:
425 		if (!(ypos & 1)) {
426 			*(*dst)++ = *g;
427 			*(*dst)++ = *++r;
428 		} else {
429 			*(*dst)++ = *b;
430 			*(*dst)++ = *++g;
431 		}
432 		return;
433 	case V4L2_PIX_FMT_SRGGB8:
434 		if (!(ypos & 1)) {
435 			*(*dst)++ = *r;
436 			*(*dst)++ = *++g;
437 		} else {
438 			*(*dst)++ = *g;
439 			*(*dst)++ = *++b;
440 		}
441 		return;
442 	}
443 }
444 
445 static int device_process(struct vim2m_ctx *ctx,
446 			  struct vb2_v4l2_buffer *in_vb,
447 			  struct vb2_v4l2_buffer *out_vb)
448 {
449 	struct vim2m_dev *dev = ctx->dev;
450 	struct vim2m_q_data *q_data_in, *q_data_out;
451 	u8 *p_in, *p_line, *p_in_x[2], *p, *p_out;
452 	unsigned int width, height, bytesperline, bytes_per_pixel;
453 	unsigned int x, y, y_in, y_out, x_int, x_fract, x_err, x_offset;
454 	int start, end, step;
455 
456 	q_data_in = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
457 	if (!q_data_in)
458 		return 0;
459 	bytesperline = (q_data_in->width * q_data_in->fmt->depth) >> 3;
460 	bytes_per_pixel = q_data_in->fmt->depth >> 3;
461 
462 	q_data_out = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
463 	if (!q_data_out)
464 		return 0;
465 
466 	/* As we're doing scaling, use the output dimensions here */
467 	height = q_data_out->height;
468 	width = q_data_out->width;
469 
470 	p_in = vb2_plane_vaddr(&in_vb->vb2_buf, 0);
471 	p_out = vb2_plane_vaddr(&out_vb->vb2_buf, 0);
472 	if (!p_in || !p_out) {
473 		v4l2_err(&dev->v4l2_dev,
474 			 "Acquiring kernel pointers to buffers failed\n");
475 		return -EFAULT;
476 	}
477 
478 	out_vb->sequence = q_data_out->sequence++;
479 	in_vb->sequence = q_data_in->sequence++;
480 	v4l2_m2m_buf_copy_metadata(in_vb, out_vb, true);
481 
482 	if (ctx->mode & MEM2MEM_VFLIP) {
483 		start = height - 1;
484 		end = -1;
485 		step = -1;
486 	} else {
487 		start = 0;
488 		end = height;
489 		step = 1;
490 	}
491 	y_out = 0;
492 
493 	/*
494 	 * When format and resolution are identical,
495 	 * we can use a faster copy logic
496 	 */
497 	if (q_data_in->fmt->fourcc == q_data_out->fmt->fourcc &&
498 	    q_data_in->width == q_data_out->width &&
499 	    q_data_in->height == q_data_out->height) {
500 		for (y = start; y != end; y += step, y_out++) {
501 			p = p_in + (y * bytesperline);
502 			if (ctx->mode & MEM2MEM_HFLIP)
503 				p += bytesperline - (q_data_in->fmt->depth >> 3);
504 
505 			copy_line(q_data_out, p, p_out,
506 				  ctx->mode & MEM2MEM_HFLIP);
507 
508 			p_out += bytesperline;
509 		}
510 		return 0;
511 	}
512 
513 	/* Slower algorithm with format conversion, hflip, vflip and scaler */
514 
515 	/* To speed scaler up, use Bresenham for X dimension */
516 	x_int = q_data_in->width / q_data_out->width;
517 	x_fract = q_data_in->width % q_data_out->width;
518 
519 	for (y = start; y != end; y += step, y_out++) {
520 		y_in = (y * q_data_in->height) / q_data_out->height;
521 		x_offset = 0;
522 		x_err = 0;
523 
524 		p_line = p_in + (y_in * bytesperline);
525 		if (ctx->mode & MEM2MEM_HFLIP)
526 			p_line += bytesperline - (q_data_in->fmt->depth >> 3);
527 		p_in_x[0] = p_line;
528 
529 		for (x = 0; x < width >> 1; x++) {
530 			x_offset += x_int;
531 			x_err += x_fract;
532 			if (x_err > width) {
533 				x_offset++;
534 				x_err -= width;
535 			}
536 
537 			if (ctx->mode & MEM2MEM_HFLIP)
538 				p_in_x[1] = p_line - x_offset * bytes_per_pixel;
539 			else
540 				p_in_x[1] = p_line + x_offset * bytes_per_pixel;
541 
542 			copy_two_pixels(q_data_in, q_data_out,
543 					p_in_x, &p_out, y_out,
544 					ctx->mode & MEM2MEM_HFLIP);
545 
546 			/* Calculate the next p_in_x0 */
547 			x_offset += x_int;
548 			x_err += x_fract;
549 			if (x_err > width) {
550 				x_offset++;
551 				x_err -= width;
552 			}
553 
554 			if (ctx->mode & MEM2MEM_HFLIP)
555 				p_in_x[0] = p_line - x_offset * bytes_per_pixel;
556 			else
557 				p_in_x[0] = p_line + x_offset * bytes_per_pixel;
558 		}
559 	}
560 
561 	return 0;
562 }
563 
564 /*
565  * mem2mem callbacks
566  */
567 
568 /*
569  * job_ready() - check whether an instance is ready to be scheduled to run
570  */
571 static int job_ready(void *priv)
572 {
573 	struct vim2m_ctx *ctx = priv;
574 
575 	if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen
576 	    || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen) {
577 		dprintk(ctx->dev, 1, "Not enough buffers available\n");
578 		return 0;
579 	}
580 
581 	return 1;
582 }
583 
584 static void job_abort(void *priv)
585 {
586 	struct vim2m_ctx *ctx = priv;
587 
588 	/* Will cancel the transaction in the next interrupt handler */
589 	ctx->aborting = 1;
590 }
591 
592 /* device_run() - prepares and starts the device
593  *
594  * This simulates all the immediate preparations required before starting
595  * a device. This will be called by the framework when it decides to schedule
596  * a particular instance.
597  */
598 static void device_run(void *priv)
599 {
600 	struct vim2m_ctx *ctx = priv;
601 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
602 
603 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
604 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
605 
606 	/* Apply request controls if any */
607 	v4l2_ctrl_request_setup(src_buf->vb2_buf.req_obj.req,
608 				&ctx->hdl);
609 
610 	device_process(ctx, src_buf, dst_buf);
611 
612 	/* Complete request controls if any */
613 	v4l2_ctrl_request_complete(src_buf->vb2_buf.req_obj.req,
614 				   &ctx->hdl);
615 
616 	/* Run delayed work, which simulates a hardware irq  */
617 	schedule_delayed_work(&ctx->work_run, msecs_to_jiffies(ctx->transtime));
618 }
619 
620 static void device_work(struct work_struct *w)
621 {
622 	struct vim2m_ctx *curr_ctx;
623 	struct vim2m_dev *vim2m_dev;
624 	struct vb2_v4l2_buffer *src_vb, *dst_vb;
625 
626 	curr_ctx = container_of(w, struct vim2m_ctx, work_run.work);
627 
628 	vim2m_dev = curr_ctx->dev;
629 
630 	src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
631 	dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
632 
633 	curr_ctx->num_processed++;
634 
635 	v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
636 	v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
637 
638 	if (curr_ctx->num_processed == curr_ctx->translen
639 	    || curr_ctx->aborting) {
640 		dprintk(curr_ctx->dev, 2, "Finishing capture buffer fill\n");
641 		curr_ctx->num_processed = 0;
642 		v4l2_m2m_job_finish(vim2m_dev->m2m_dev, curr_ctx->fh.m2m_ctx);
643 	} else {
644 		device_run(curr_ctx);
645 	}
646 }
647 
648 /*
649  * video ioctls
650  */
651 static int vidioc_querycap(struct file *file, void *priv,
652 			   struct v4l2_capability *cap)
653 {
654 	strscpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver));
655 	strscpy(cap->card, MEM2MEM_NAME, sizeof(cap->card));
656 	snprintf(cap->bus_info, sizeof(cap->bus_info),
657 		 "platform:%s", MEM2MEM_NAME);
658 	return 0;
659 }
660 
661 static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
662 {
663 	int i, num;
664 	struct vim2m_fmt *fmt;
665 
666 	num = 0;
667 
668 	for (i = 0; i < NUM_FORMATS; ++i) {
669 		if (formats[i].types & type) {
670 			/* index-th format of type type found ? */
671 			if (num == f->index)
672 				break;
673 			/*
674 			 * Correct type but haven't reached our index yet,
675 			 * just increment per-type index
676 			 */
677 			++num;
678 		}
679 	}
680 
681 	if (i < NUM_FORMATS) {
682 		/* Format found */
683 		fmt = &formats[i];
684 		f->pixelformat = fmt->fourcc;
685 		return 0;
686 	}
687 
688 	/* Format not found */
689 	return -EINVAL;
690 }
691 
692 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
693 				   struct v4l2_fmtdesc *f)
694 {
695 	return enum_fmt(f, MEM2MEM_CAPTURE);
696 }
697 
698 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
699 				   struct v4l2_fmtdesc *f)
700 {
701 	return enum_fmt(f, MEM2MEM_OUTPUT);
702 }
703 
704 static int vidioc_enum_framesizes(struct file *file, void *priv,
705 				  struct v4l2_frmsizeenum *fsize)
706 {
707 	if (fsize->index != 0)
708 		return -EINVAL;
709 
710 	if (!find_format(fsize->pixel_format))
711 		return -EINVAL;
712 
713 	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
714 	fsize->stepwise.min_width = MIN_W;
715 	fsize->stepwise.min_height = MIN_H;
716 	fsize->stepwise.max_width = MAX_W;
717 	fsize->stepwise.max_height = MAX_H;
718 
719 	get_alignment(fsize->pixel_format,
720 		      &fsize->stepwise.step_width,
721 		      &fsize->stepwise.step_height);
722 	return 0;
723 }
724 
725 static int vidioc_g_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
726 {
727 	struct vb2_queue *vq;
728 	struct vim2m_q_data *q_data;
729 	int ret;
730 
731 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
732 	if (!vq)
733 		return -EINVAL;
734 
735 	q_data = get_q_data(ctx, f->type);
736 	if (!q_data)
737 		return -EINVAL;
738 
739 	ret = v4l2_fill_pixfmt(&f->fmt.pix, q_data->fmt->fourcc,
740 			       q_data->width, q_data->height);
741 	if (ret)
742 		return ret;
743 
744 	f->fmt.pix.field	= V4L2_FIELD_NONE;
745 	f->fmt.pix.colorspace	= ctx->colorspace;
746 	f->fmt.pix.xfer_func	= ctx->xfer_func;
747 	f->fmt.pix.ycbcr_enc	= ctx->ycbcr_enc;
748 	f->fmt.pix.quantization	= ctx->quant;
749 
750 	return 0;
751 }
752 
753 static int vidioc_g_fmt_mplane(struct vim2m_ctx *ctx, struct v4l2_format *f)
754 {
755 	struct vb2_queue *vq;
756 	struct vim2m_q_data *q_data;
757 	int ret;
758 
759 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
760 	if (!vq)
761 		return -EINVAL;
762 
763 	q_data = get_q_data(ctx, f->type);
764 	if (!q_data)
765 		return -EINVAL;
766 
767 	ret = v4l2_fill_pixfmt_mp(&f->fmt.pix_mp, q_data->fmt->fourcc,
768 				  q_data->width, q_data->height);
769 	if (ret)
770 		return ret;
771 
772 	f->fmt.pix_mp.field	   = V4L2_FIELD_NONE;
773 	f->fmt.pix_mp.colorspace   = ctx->colorspace;
774 	f->fmt.pix_mp.xfer_func	   = ctx->xfer_func;
775 	f->fmt.pix_mp.ycbcr_enc	   = ctx->ycbcr_enc;
776 	f->fmt.pix_mp.quantization = ctx->quant;
777 
778 	return 0;
779 }
780 
781 static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
782 				struct v4l2_format *f)
783 {
784 	struct vim2m_dev *dev = video_drvdata(file);
785 
786 	if (dev->multiplanar)
787 		return -ENOTTY;
788 
789 	return vidioc_g_fmt(file2ctx(file), f);
790 }
791 
792 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
793 				struct v4l2_format *f)
794 {
795 	struct vim2m_dev *dev = video_drvdata(file);
796 
797 	if (dev->multiplanar)
798 		return -ENOTTY;
799 
800 	return vidioc_g_fmt(file2ctx(file), f);
801 }
802 
803 static int vidioc_g_fmt_vid_out_mplane(struct file *file, void *priv,
804 				       struct v4l2_format *f)
805 {
806 	struct vim2m_dev *dev = video_drvdata(file);
807 
808 	if (!dev->multiplanar)
809 		return -ENOTTY;
810 
811 	return vidioc_g_fmt_mplane(file2ctx(file), f);
812 }
813 
814 static int vidioc_g_fmt_vid_cap_mplane(struct file *file, void *priv,
815 				       struct v4l2_format *f)
816 {
817 	struct vim2m_dev *dev = video_drvdata(file);
818 
819 	if (!dev->multiplanar)
820 		return -ENOTTY;
821 
822 	return vidioc_g_fmt_mplane(file2ctx(file), f);
823 }
824 
825 static int vidioc_try_fmt(struct v4l2_format *f, bool is_mplane)
826 {
827 	int walign, halign, ret;
828 	int width = (is_mplane) ? f->fmt.pix_mp.width : f->fmt.pix.width;
829 	int height = (is_mplane) ? f->fmt.pix_mp.height : f->fmt.pix.height;
830 	u32 pixfmt = (is_mplane) ? f->fmt.pix_mp.pixelformat :
831 		f->fmt.pix.pixelformat;
832 
833 	width = clamp(width, MIN_W, MAX_W);
834 	height = clamp(height, MIN_H, MAX_H);
835 
836 	get_alignment(pixfmt, &walign, &halign);
837 	width = ALIGN(width, walign);
838 	height = ALIGN(height, halign);
839 
840 	f->fmt.pix.field = V4L2_FIELD_NONE;
841 
842 	if (is_mplane) {
843 		ret = v4l2_fill_pixfmt_mp(&f->fmt.pix_mp, pixfmt, width,
844 					  height);
845 	} else {
846 		ret = v4l2_fill_pixfmt(&f->fmt.pix, pixfmt,  width, height);
847 	}
848 	return ret;
849 }
850 
851 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
852 				  struct v4l2_format *f)
853 {
854 	struct vim2m_fmt *fmt;
855 	struct vim2m_ctx *ctx = file2ctx(file);
856 	struct vim2m_dev *dev = video_drvdata(file);
857 
858 	if (dev->multiplanar)
859 		return -ENOTTY;
860 
861 	fmt = find_format(f->fmt.pix.pixelformat);
862 	if (!fmt) {
863 		f->fmt.pix.pixelformat = formats[0].fourcc;
864 		fmt = find_format(f->fmt.pix.pixelformat);
865 	}
866 	if (!(fmt->types & MEM2MEM_CAPTURE)) {
867 		v4l2_err(&ctx->dev->v4l2_dev,
868 			 "Fourcc format (0x%08x) invalid.\n",
869 			 f->fmt.pix.pixelformat);
870 		return -EINVAL;
871 	}
872 	f->fmt.pix.colorspace = ctx->colorspace;
873 	f->fmt.pix.xfer_func = ctx->xfer_func;
874 	f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
875 	f->fmt.pix.quantization = ctx->quant;
876 
877 	return vidioc_try_fmt(f, false);
878 }
879 
880 static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
881 					 struct v4l2_format *f)
882 {
883 	struct vim2m_fmt *fmt;
884 	struct vim2m_ctx *ctx = file2ctx(file);
885 	struct vim2m_dev *dev = video_drvdata(file);
886 
887 	if (!dev->multiplanar)
888 		return -ENOTTY;
889 
890 	fmt = find_format(f->fmt.pix_mp.pixelformat);
891 	if (!fmt) {
892 		f->fmt.pix_mp.pixelformat = formats[0].fourcc;
893 		fmt = find_format(f->fmt.pix_mp.pixelformat);
894 	}
895 	if (!(fmt->types & MEM2MEM_CAPTURE)) {
896 		v4l2_err(&ctx->dev->v4l2_dev,
897 			 "Fourcc format (0x%08x) invalid.\n",
898 			 f->fmt.pix.pixelformat);
899 		return -EINVAL;
900 	}
901 	f->fmt.pix_mp.colorspace = ctx->colorspace;
902 	f->fmt.pix_mp.xfer_func = ctx->xfer_func;
903 	f->fmt.pix_mp.ycbcr_enc = ctx->ycbcr_enc;
904 	f->fmt.pix_mp.quantization = ctx->quant;
905 
906 	return vidioc_try_fmt(f, true);
907 }
908 
909 static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
910 				  struct v4l2_format *f)
911 {
912 	struct vim2m_fmt *fmt;
913 	struct vim2m_ctx *ctx = file2ctx(file);
914 	struct vim2m_dev *dev = video_drvdata(file);
915 
916 	if (dev->multiplanar)
917 		return -ENOTTY;
918 
919 	fmt = find_format(f->fmt.pix.pixelformat);
920 	if (!fmt) {
921 		f->fmt.pix.pixelformat = formats[0].fourcc;
922 		fmt = find_format(f->fmt.pix.pixelformat);
923 	}
924 	if (!(fmt->types & MEM2MEM_OUTPUT)) {
925 		v4l2_err(&ctx->dev->v4l2_dev,
926 			 "Fourcc format (0x%08x) invalid.\n",
927 			 f->fmt.pix.pixelformat);
928 		return -EINVAL;
929 	}
930 	if (!f->fmt.pix.colorspace)
931 		f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
932 
933 	return vidioc_try_fmt(f, false);
934 }
935 
936 static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
937 					 struct v4l2_format *f)
938 {
939 	struct vim2m_fmt *fmt;
940 	struct vim2m_ctx *ctx = file2ctx(file);
941 	struct vim2m_dev *dev = video_drvdata(file);
942 
943 	if (!dev->multiplanar)
944 		return -ENOTTY;
945 
946 	fmt = find_format(f->fmt.pix_mp.pixelformat);
947 	if (!fmt) {
948 		f->fmt.pix_mp.pixelformat = formats[0].fourcc;
949 		fmt = find_format(f->fmt.pix_mp.pixelformat);
950 	}
951 	if (!(fmt->types & MEM2MEM_OUTPUT)) {
952 		v4l2_err(&ctx->dev->v4l2_dev,
953 			 "Fourcc format (0x%08x) invalid.\n",
954 			 f->fmt.pix_mp.pixelformat);
955 		return -EINVAL;
956 	}
957 	if (!f->fmt.pix_mp.colorspace)
958 		f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_REC709;
959 
960 	return vidioc_try_fmt(f, true);
961 }
962 
963 static int vidioc_s_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
964 {
965 	struct vim2m_q_data *q_data;
966 	struct vb2_queue *vq;
967 	unsigned int i;
968 	bool is_mplane = ctx->dev->multiplanar;
969 	u32 pixfmt = (is_mplane) ? f->fmt.pix_mp.pixelformat : f->fmt.pix.pixelformat;
970 	u32 width = (is_mplane) ? f->fmt.pix_mp.width : f->fmt.pix.width;
971 	u32 height = (is_mplane) ? f->fmt.pix_mp.height : f->fmt.pix.height;
972 
973 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
974 	if (!vq)
975 		return -EINVAL;
976 
977 	q_data = get_q_data(ctx, f->type);
978 	if (!q_data)
979 		return -EINVAL;
980 
981 	if (vb2_is_busy(vq)) {
982 		v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
983 		return -EBUSY;
984 	}
985 
986 	q_data->fmt		= find_format(pixfmt);
987 	q_data->width		= width;
988 	q_data->height		= height;
989 	if (is_mplane) {
990 		q_data->num_mem_planes = f->fmt.pix_mp.num_planes;
991 		for (i = 0; i < f->fmt.pix_mp.num_planes; i++)
992 			q_data->sizeimage[i] = f->fmt.pix_mp.plane_fmt[i].sizeimage;
993 	} else {
994 		q_data->sizeimage[0] = f->fmt.pix.sizeimage;
995 		q_data->num_mem_planes = 1;
996 	}
997 
998 	dprintk(ctx->dev, 1,
999 		"Format for type %s: %dx%d (%d bpp), fmt: %c%c%c%c\n",
1000 		type_name(f->type), q_data->width, q_data->height,
1001 		q_data->fmt->depth,
1002 		(q_data->fmt->fourcc & 0xff),
1003 		(q_data->fmt->fourcc >>  8) & 0xff,
1004 		(q_data->fmt->fourcc >> 16) & 0xff,
1005 		(q_data->fmt->fourcc >> 24) & 0xff);
1006 
1007 	return 0;
1008 }
1009 
1010 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1011 				struct v4l2_format *f)
1012 {
1013 	int ret;
1014 	struct vim2m_dev *dev = video_drvdata(file);
1015 
1016 	if (dev->multiplanar)
1017 		return -ENOTTY;
1018 
1019 	ret = vidioc_try_fmt_vid_cap(file, priv, f);
1020 	if (ret)
1021 		return ret;
1022 
1023 	return vidioc_s_fmt(file2ctx(file), f);
1024 }
1025 
1026 static int vidioc_s_fmt_vid_cap_mplane(struct file *file, void *priv,
1027 				       struct v4l2_format *f)
1028 {
1029 	int ret;
1030 	struct vim2m_dev *dev = video_drvdata(file);
1031 
1032 	if (!dev->multiplanar)
1033 		return -ENOTTY;
1034 
1035 	ret = vidioc_try_fmt_vid_cap_mplane(file, priv, f);
1036 	if (ret)
1037 		return ret;
1038 
1039 	return vidioc_s_fmt(file2ctx(file), f);
1040 }
1041 
1042 static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
1043 				struct v4l2_format *f)
1044 {
1045 	struct vim2m_ctx *ctx = file2ctx(file);
1046 	struct vim2m_dev *dev = video_drvdata(file);
1047 	int ret;
1048 
1049 	if (dev->multiplanar)
1050 		return -ENOTTY;
1051 
1052 	ret = vidioc_try_fmt_vid_out(file, priv, f);
1053 	if (ret)
1054 		return ret;
1055 
1056 	ret = vidioc_s_fmt(file2ctx(file), f);
1057 	if (!ret) {
1058 		ctx->colorspace = f->fmt.pix.colorspace;
1059 		ctx->xfer_func = f->fmt.pix.xfer_func;
1060 		ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
1061 		ctx->quant = f->fmt.pix.quantization;
1062 	}
1063 	return ret;
1064 }
1065 
1066 static int vidioc_s_fmt_vid_out_mplane(struct file *file, void *priv,
1067 				       struct v4l2_format *f)
1068 {
1069 	struct vim2m_ctx *ctx = file2ctx(file);
1070 	struct vim2m_dev *dev = video_drvdata(file);
1071 	int ret;
1072 
1073 	if (!dev->multiplanar)
1074 		return -ENOTTY;
1075 
1076 	ret = vidioc_try_fmt_vid_out_mplane(file, priv, f);
1077 	if (ret)
1078 		return ret;
1079 
1080 	ret = vidioc_s_fmt(file2ctx(file), f);
1081 	if (!ret) {
1082 		ctx->colorspace = f->fmt.pix_mp.colorspace;
1083 		ctx->xfer_func = f->fmt.pix_mp.xfer_func;
1084 		ctx->ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
1085 		ctx->quant = f->fmt.pix_mp.quantization;
1086 	}
1087 	return ret;
1088 }
1089 
1090 static int vim2m_s_ctrl(struct v4l2_ctrl *ctrl)
1091 {
1092 	struct vim2m_ctx *ctx =
1093 		container_of(ctrl->handler, struct vim2m_ctx, hdl);
1094 
1095 	switch (ctrl->id) {
1096 	case V4L2_CID_HFLIP:
1097 		if (ctrl->val)
1098 			ctx->mode |= MEM2MEM_HFLIP;
1099 		else
1100 			ctx->mode &= ~MEM2MEM_HFLIP;
1101 		break;
1102 
1103 	case V4L2_CID_VFLIP:
1104 		if (ctrl->val)
1105 			ctx->mode |= MEM2MEM_VFLIP;
1106 		else
1107 			ctx->mode &= ~MEM2MEM_VFLIP;
1108 		break;
1109 
1110 	case V4L2_CID_TRANS_TIME_MSEC:
1111 		ctx->transtime = ctrl->val;
1112 		if (ctx->transtime < 1)
1113 			ctx->transtime = 1;
1114 		break;
1115 
1116 	case V4L2_CID_TRANS_NUM_BUFS:
1117 		ctx->translen = ctrl->val;
1118 		break;
1119 
1120 	default:
1121 		v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
1122 		return -EINVAL;
1123 	}
1124 
1125 	return 0;
1126 }
1127 
1128 static const struct v4l2_ctrl_ops vim2m_ctrl_ops = {
1129 	.s_ctrl = vim2m_s_ctrl,
1130 };
1131 
1132 static const struct v4l2_ioctl_ops vim2m_ioctl_ops = {
1133 	.vidioc_querycap	= vidioc_querycap,
1134 
1135 	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1136 	.vidioc_enum_framesizes = vidioc_enum_framesizes,
1137 	.vidioc_g_fmt_vid_cap	= vidioc_g_fmt_vid_cap,
1138 	.vidioc_try_fmt_vid_cap	= vidioc_try_fmt_vid_cap,
1139 	.vidioc_s_fmt_vid_cap	= vidioc_s_fmt_vid_cap,
1140 	.vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt_vid_cap_mplane,
1141 	.vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_vid_cap_mplane,
1142 	.vidioc_s_fmt_vid_cap_mplane	= vidioc_s_fmt_vid_cap_mplane,
1143 
1144 	.vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
1145 	.vidioc_g_fmt_vid_out	= vidioc_g_fmt_vid_out,
1146 	.vidioc_try_fmt_vid_out	= vidioc_try_fmt_vid_out,
1147 	.vidioc_s_fmt_vid_out	= vidioc_s_fmt_vid_out,
1148 	.vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt_vid_out_mplane,
1149 	.vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_vid_out_mplane,
1150 	.vidioc_s_fmt_vid_out_mplane	= vidioc_s_fmt_vid_out_mplane,
1151 
1152 	.vidioc_reqbufs		= v4l2_m2m_ioctl_reqbufs,
1153 	.vidioc_querybuf	= v4l2_m2m_ioctl_querybuf,
1154 	.vidioc_qbuf		= v4l2_m2m_ioctl_qbuf,
1155 	.vidioc_dqbuf		= v4l2_m2m_ioctl_dqbuf,
1156 	.vidioc_prepare_buf	= v4l2_m2m_ioctl_prepare_buf,
1157 	.vidioc_create_bufs	= v4l2_m2m_ioctl_create_bufs,
1158 	.vidioc_expbuf		= v4l2_m2m_ioctl_expbuf,
1159 
1160 	.vidioc_streamon	= v4l2_m2m_ioctl_streamon,
1161 	.vidioc_streamoff	= v4l2_m2m_ioctl_streamoff,
1162 
1163 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1164 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1165 };
1166 
1167 /*
1168  * Queue operations
1169  */
1170 
1171 static int vim2m_queue_setup(struct vb2_queue *vq,
1172 			     unsigned int *nbuffers,
1173 			     unsigned int *nplanes,
1174 			     unsigned int sizes[],
1175 			     struct device *alloc_devs[])
1176 {
1177 	struct vim2m_ctx *ctx = vb2_get_drv_priv(vq);
1178 	struct vim2m_q_data *q_data;
1179 	unsigned int size, p, count = *nbuffers;
1180 
1181 	q_data = get_q_data(ctx, vq->type);
1182 	if (!q_data)
1183 		return -EINVAL;
1184 
1185 	size = 0;
1186 	for (p = 0; p < q_data->num_mem_planes; p++)
1187 		size += q_data->sizeimage[p];
1188 
1189 	while (size * count > MEM2MEM_VID_MEM_LIMIT)
1190 		(count)--;
1191 	*nbuffers = count;
1192 
1193 	if (*nplanes) {
1194 		if (*nplanes != q_data->num_mem_planes)
1195 			return -EINVAL;
1196 		for (p = 0; p < q_data->num_mem_planes; p++) {
1197 			if (sizes[p] < q_data->sizeimage[p])
1198 				return -EINVAL;
1199 		}
1200 	} else {
1201 		*nplanes = q_data->num_mem_planes;
1202 		for (p = 0; p < q_data->num_mem_planes; p++)
1203 			sizes[p] = q_data->sizeimage[p];
1204 	}
1205 
1206 	dprintk(ctx->dev, 1, "%s: get %d buffer(s) of size %d each.\n",
1207 		type_name(vq->type), count, size);
1208 
1209 	return 0;
1210 }
1211 
1212 static int vim2m_buf_out_validate(struct vb2_buffer *vb)
1213 {
1214 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1215 	struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1216 
1217 	if (vbuf->field == V4L2_FIELD_ANY)
1218 		vbuf->field = V4L2_FIELD_NONE;
1219 	if (vbuf->field != V4L2_FIELD_NONE) {
1220 		dprintk(ctx->dev, 1, "%s field isn't supported\n", __func__);
1221 		return -EINVAL;
1222 	}
1223 
1224 	return 0;
1225 }
1226 
1227 static int vim2m_buf_prepare(struct vb2_buffer *vb)
1228 {
1229 	struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1230 	struct vim2m_q_data *q_data;
1231 	unsigned int p;
1232 
1233 	dprintk(ctx->dev, 2, "type: %s\n", type_name(vb->vb2_queue->type));
1234 
1235 	q_data = get_q_data(ctx, vb->vb2_queue->type);
1236 	if (!q_data)
1237 		return -EINVAL;
1238 
1239 	for (p = 0; p < q_data->num_mem_planes; p++) {
1240 		if (vb2_plane_size(vb, p) < q_data->sizeimage[p]) {
1241 			dprintk(ctx->dev, 1,
1242 				"%s data will not fit into plane (%lu < %lu)\n",
1243 				__func__, vb2_plane_size(vb, p),
1244 				(long)q_data->sizeimage[p]);
1245 			return -EINVAL;
1246 		}
1247 		vb2_set_plane_payload(vb, p, q_data->sizeimage[p]);
1248 	}
1249 
1250 	return 0;
1251 }
1252 
1253 static void vim2m_buf_queue(struct vb2_buffer *vb)
1254 {
1255 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1256 	struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1257 
1258 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1259 }
1260 
1261 static int vim2m_start_streaming(struct vb2_queue *q, unsigned int count)
1262 {
1263 	struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
1264 	struct vim2m_q_data *q_data = get_q_data(ctx, q->type);
1265 
1266 	if (!q_data)
1267 		return -EINVAL;
1268 
1269 	if (V4L2_TYPE_IS_OUTPUT(q->type))
1270 		ctx->aborting = 0;
1271 
1272 	q_data->sequence = 0;
1273 	return 0;
1274 }
1275 
1276 static void vim2m_stop_streaming(struct vb2_queue *q)
1277 {
1278 	struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
1279 	struct vb2_v4l2_buffer *vbuf;
1280 
1281 	cancel_delayed_work_sync(&ctx->work_run);
1282 
1283 	for (;;) {
1284 		if (V4L2_TYPE_IS_OUTPUT(q->type))
1285 			vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1286 		else
1287 			vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1288 		if (!vbuf)
1289 			return;
1290 		v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
1291 					   &ctx->hdl);
1292 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
1293 	}
1294 }
1295 
1296 static void vim2m_buf_request_complete(struct vb2_buffer *vb)
1297 {
1298 	struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1299 
1300 	v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl);
1301 }
1302 
1303 static const struct vb2_ops vim2m_qops = {
1304 	.queue_setup	 = vim2m_queue_setup,
1305 	.buf_out_validate	 = vim2m_buf_out_validate,
1306 	.buf_prepare	 = vim2m_buf_prepare,
1307 	.buf_queue	 = vim2m_buf_queue,
1308 	.start_streaming = vim2m_start_streaming,
1309 	.stop_streaming  = vim2m_stop_streaming,
1310 	.buf_request_complete = vim2m_buf_request_complete,
1311 };
1312 
1313 static int queue_init(void *priv, struct vb2_queue *src_vq,
1314 		      struct vb2_queue *dst_vq)
1315 {
1316 	struct vim2m_ctx *ctx = priv;
1317 	int ret;
1318 
1319 	src_vq->type = (ctx->dev->multiplanar) ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
1320 		V4L2_BUF_TYPE_VIDEO_OUTPUT;
1321 	src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1322 	src_vq->drv_priv = ctx;
1323 	src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1324 	src_vq->ops = &vim2m_qops;
1325 	src_vq->mem_ops = &vb2_vmalloc_memops;
1326 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1327 	src_vq->lock = &ctx->vb_mutex;
1328 	src_vq->supports_requests = true;
1329 
1330 	ret = vb2_queue_init(src_vq);
1331 	if (ret)
1332 		return ret;
1333 
1334 	dst_vq->type = (ctx->dev->multiplanar) ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1335 		V4L2_BUF_TYPE_VIDEO_CAPTURE;
1336 	dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1337 	dst_vq->drv_priv = ctx;
1338 	dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1339 	dst_vq->ops = &vim2m_qops;
1340 	dst_vq->mem_ops = &vb2_vmalloc_memops;
1341 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1342 	dst_vq->lock = &ctx->vb_mutex;
1343 
1344 	return vb2_queue_init(dst_vq);
1345 }
1346 
1347 static struct v4l2_ctrl_config vim2m_ctrl_trans_time_msec = {
1348 	.ops = &vim2m_ctrl_ops,
1349 	.id = V4L2_CID_TRANS_TIME_MSEC,
1350 	.name = "Transaction Time (msec)",
1351 	.type = V4L2_CTRL_TYPE_INTEGER,
1352 	.min = 1,
1353 	.max = 10001,
1354 	.step = 1,
1355 };
1356 
1357 static const struct v4l2_ctrl_config vim2m_ctrl_trans_num_bufs = {
1358 	.ops = &vim2m_ctrl_ops,
1359 	.id = V4L2_CID_TRANS_NUM_BUFS,
1360 	.name = "Buffers Per Transaction",
1361 	.type = V4L2_CTRL_TYPE_INTEGER,
1362 	.def = 1,
1363 	.min = 1,
1364 	.max = MEM2MEM_DEF_NUM_BUFS,
1365 	.step = 1,
1366 };
1367 
1368 /*
1369  * File operations
1370  */
1371 static int vim2m_open(struct file *file)
1372 {
1373 	struct vim2m_dev *dev = video_drvdata(file);
1374 	struct vim2m_ctx *ctx = NULL;
1375 	struct v4l2_ctrl_handler *hdl;
1376 	int rc = 0;
1377 
1378 	if (mutex_lock_interruptible(&dev->dev_mutex))
1379 		return -ERESTARTSYS;
1380 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1381 	if (!ctx) {
1382 		rc = -ENOMEM;
1383 		goto open_unlock;
1384 	}
1385 
1386 	v4l2_fh_init(&ctx->fh, video_devdata(file));
1387 	ctx->dev = dev;
1388 	hdl = &ctx->hdl;
1389 	v4l2_ctrl_handler_init(hdl, 4);
1390 	v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
1391 	v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
1392 
1393 	vim2m_ctrl_trans_time_msec.def = default_transtime;
1394 	v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_time_msec, NULL);
1395 	v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_num_bufs, NULL);
1396 	if (hdl->error) {
1397 		rc = hdl->error;
1398 		v4l2_ctrl_handler_free(hdl);
1399 		kfree(ctx);
1400 		goto open_unlock;
1401 	}
1402 	ctx->fh.ctrl_handler = hdl;
1403 	v4l2_ctrl_handler_setup(hdl);
1404 
1405 	ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
1406 	ctx->q_data[V4L2_M2M_SRC].width = 640;
1407 	ctx->q_data[V4L2_M2M_SRC].height = 480;
1408 	ctx->q_data[V4L2_M2M_SRC].sizeimage[0] =
1409 		ctx->q_data[V4L2_M2M_SRC].width *
1410 		ctx->q_data[V4L2_M2M_SRC].height *
1411 		(ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3);
1412 	ctx->q_data[V4L2_M2M_SRC].num_mem_planes = 1;
1413 	ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
1414 	ctx->colorspace = V4L2_COLORSPACE_REC709;
1415 
1416 	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
1417 
1418 	mutex_init(&ctx->vb_mutex);
1419 	INIT_DELAYED_WORK(&ctx->work_run, device_work);
1420 
1421 	if (IS_ERR(ctx->fh.m2m_ctx)) {
1422 		rc = PTR_ERR(ctx->fh.m2m_ctx);
1423 
1424 		v4l2_ctrl_handler_free(hdl);
1425 		v4l2_fh_exit(&ctx->fh);
1426 		kfree(ctx);
1427 		goto open_unlock;
1428 	}
1429 
1430 	v4l2_fh_add(&ctx->fh, file);
1431 	atomic_inc(&dev->num_inst);
1432 
1433 	dprintk(dev, 1, "Created instance: %p, m2m_ctx: %p\n",
1434 		ctx, ctx->fh.m2m_ctx);
1435 
1436 open_unlock:
1437 	mutex_unlock(&dev->dev_mutex);
1438 	return rc;
1439 }
1440 
1441 static int vim2m_release(struct file *file)
1442 {
1443 	struct vim2m_dev *dev = video_drvdata(file);
1444 	struct vim2m_ctx *ctx = file2ctx(file);
1445 
1446 	dprintk(dev, 1, "Releasing instance %p\n", ctx);
1447 
1448 	v4l2_fh_del(&ctx->fh, file);
1449 	v4l2_fh_exit(&ctx->fh);
1450 	v4l2_ctrl_handler_free(&ctx->hdl);
1451 	mutex_lock(&dev->dev_mutex);
1452 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1453 	mutex_unlock(&dev->dev_mutex);
1454 	kfree(ctx);
1455 
1456 	atomic_dec(&dev->num_inst);
1457 
1458 	return 0;
1459 }
1460 
1461 static void vim2m_device_release(struct video_device *vdev)
1462 {
1463 	struct vim2m_dev *dev = container_of(vdev, struct vim2m_dev, vfd);
1464 
1465 	v4l2_device_unregister(&dev->v4l2_dev);
1466 	v4l2_m2m_release(dev->m2m_dev);
1467 	media_device_cleanup(&dev->mdev);
1468 	kfree(dev);
1469 }
1470 
1471 static const struct v4l2_file_operations vim2m_fops = {
1472 	.owner		= THIS_MODULE,
1473 	.open		= vim2m_open,
1474 	.release	= vim2m_release,
1475 	.poll		= v4l2_m2m_fop_poll,
1476 	.unlocked_ioctl	= video_ioctl2,
1477 	.mmap		= v4l2_m2m_fop_mmap,
1478 };
1479 
1480 static const struct video_device vim2m_videodev = {
1481 	.name		= MEM2MEM_NAME,
1482 	.vfl_dir	= VFL_DIR_M2M,
1483 	.fops		= &vim2m_fops,
1484 	.ioctl_ops	= &vim2m_ioctl_ops,
1485 	.minor		= -1,
1486 	.release	= vim2m_device_release,
1487 	.device_caps	= V4L2_CAP_STREAMING,
1488 };
1489 
1490 static const struct v4l2_m2m_ops m2m_ops = {
1491 	.device_run	= device_run,
1492 	.job_ready	= job_ready,
1493 	.job_abort	= job_abort,
1494 };
1495 
1496 static const struct media_device_ops m2m_media_ops = {
1497 	.req_validate = vb2_request_validate,
1498 	.req_queue = v4l2_m2m_request_queue,
1499 };
1500 
1501 static int vim2m_probe(struct platform_device *pdev)
1502 {
1503 	struct vim2m_dev *dev;
1504 	struct video_device *vfd;
1505 	int ret;
1506 
1507 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1508 	if (!dev)
1509 		return -ENOMEM;
1510 
1511 	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1512 	if (ret)
1513 		goto error_free;
1514 
1515 	atomic_set(&dev->num_inst, 0);
1516 	mutex_init(&dev->dev_mutex);
1517 
1518 	dev->multiplanar = (multiplanar == 2);
1519 
1520 	dev->vfd = vim2m_videodev;
1521 	vfd = &dev->vfd;
1522 	vfd->lock = &dev->dev_mutex;
1523 	vfd->v4l2_dev = &dev->v4l2_dev;
1524 	vfd->device_caps |= (dev->multiplanar) ? V4L2_CAP_VIDEO_M2M_MPLANE :
1525 		V4L2_CAP_VIDEO_M2M;
1526 
1527 	video_set_drvdata(vfd, dev);
1528 	platform_set_drvdata(pdev, dev);
1529 
1530 	dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
1531 	if (IS_ERR(dev->m2m_dev)) {
1532 		v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1533 		ret = PTR_ERR(dev->m2m_dev);
1534 		dev->m2m_dev = NULL;
1535 		goto error_dev;
1536 	}
1537 
1538 	dev->mdev.dev = &pdev->dev;
1539 	strscpy(dev->mdev.model, "vim2m", sizeof(dev->mdev.model));
1540 	strscpy(dev->mdev.bus_info, "platform:vim2m",
1541 		sizeof(dev->mdev.bus_info));
1542 	media_device_init(&dev->mdev);
1543 	dev->mdev.ops = &m2m_media_ops;
1544 	dev->v4l2_dev.mdev = &dev->mdev;
1545 
1546 	ret = video_register_device(vfd, VFL_TYPE_VIDEO, 0);
1547 	if (ret) {
1548 		v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1549 		goto error_m2m;
1550 	}
1551 
1552 	v4l2_info(&dev->v4l2_dev,
1553 		  "Device registered as /dev/video%d\n", vfd->num);
1554 
1555 	ret = v4l2_m2m_register_media_controller(dev->m2m_dev, vfd,
1556 						 MEDIA_ENT_F_PROC_VIDEO_SCALER);
1557 	if (ret) {
1558 		v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller\n");
1559 		goto error_v4l2;
1560 	}
1561 
1562 	ret = media_device_register(&dev->mdev);
1563 	if (ret) {
1564 		v4l2_err(&dev->v4l2_dev, "Failed to register mem2mem media device\n");
1565 		goto error_m2m_mc;
1566 	}
1567 
1568 	return 0;
1569 
1570 error_m2m_mc:
1571 	v4l2_m2m_unregister_media_controller(dev->m2m_dev);
1572 error_v4l2:
1573 	video_unregister_device(&dev->vfd);
1574 	/* vim2m_device_release called by video_unregister_device to release various objects */
1575 	return ret;
1576 error_m2m:
1577 	v4l2_m2m_release(dev->m2m_dev);
1578 error_dev:
1579 	v4l2_device_unregister(&dev->v4l2_dev);
1580 error_free:
1581 	kfree(dev);
1582 
1583 	return ret;
1584 }
1585 
1586 static void vim2m_remove(struct platform_device *pdev)
1587 {
1588 	struct vim2m_dev *dev = platform_get_drvdata(pdev);
1589 
1590 	v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME);
1591 
1592 	media_device_unregister(&dev->mdev);
1593 	v4l2_m2m_unregister_media_controller(dev->m2m_dev);
1594 	video_unregister_device(&dev->vfd);
1595 }
1596 
1597 static struct platform_driver vim2m_pdrv = {
1598 	.probe		= vim2m_probe,
1599 	.remove		= vim2m_remove,
1600 	.driver		= {
1601 		.name	= MEM2MEM_NAME,
1602 	},
1603 };
1604 
1605 static void __exit vim2m_exit(void)
1606 {
1607 	platform_driver_unregister(&vim2m_pdrv);
1608 	platform_device_unregister(&vim2m_pdev);
1609 }
1610 
1611 static int __init vim2m_init(void)
1612 {
1613 	int ret;
1614 
1615 	ret = platform_device_register(&vim2m_pdev);
1616 	if (ret)
1617 		return ret;
1618 
1619 	ret = platform_driver_register(&vim2m_pdrv);
1620 	if (ret)
1621 		platform_device_unregister(&vim2m_pdev);
1622 
1623 	return ret;
1624 }
1625 
1626 module_init(vim2m_init);
1627 module_exit(vim2m_exit);
1628