1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * TI Camera Access Layer (CAL) - Video Device
4  *
5  * Copyright (c) 2015-2020 Texas Instruments Inc.
6  *
7  * Authors:
8  *	Benoit Parrot <bparrot@ti.com>
9  *	Laurent Pinchart <laurent.pinchart@ideasonboard.com>
10  */
11 
12 #include <linux/ioctl.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/videodev2.h>
15 
16 #include <media/media-device.h>
17 #include <media/v4l2-common.h>
18 #include <media/v4l2-ctrls.h>
19 #include <media/v4l2-device.h>
20 #include <media/v4l2-event.h>
21 #include <media/v4l2-fh.h>
22 #include <media/v4l2-ioctl.h>
23 #include <media/videobuf2-core.h>
24 #include <media/videobuf2-dma-contig.h>
25 
26 #include "cal.h"
27 
28 /*  Print Four-character-code (FOURCC) */
29 static char *fourcc_to_str(u32 fmt)
30 {
31 	static char code[5];
32 
33 	code[0] = (unsigned char)(fmt & 0xff);
34 	code[1] = (unsigned char)((fmt >> 8) & 0xff);
35 	code[2] = (unsigned char)((fmt >> 16) & 0xff);
36 	code[3] = (unsigned char)((fmt >> 24) & 0xff);
37 	code[4] = '\0';
38 
39 	return code;
40 }
41 
42 /* ------------------------------------------------------------------
43  *	V4L2 Common IOCTLs
44  * ------------------------------------------------------------------
45  */
46 
47 static int cal_querycap(struct file *file, void *priv,
48 			struct v4l2_capability *cap)
49 {
50 	strscpy(cap->driver, CAL_MODULE_NAME, sizeof(cap->driver));
51 	strscpy(cap->card, CAL_MODULE_NAME, sizeof(cap->card));
52 
53 	return 0;
54 }
55 
56 static int cal_g_fmt_vid_cap(struct file *file, void *priv,
57 			     struct v4l2_format *f)
58 {
59 	struct cal_ctx *ctx = video_drvdata(file);
60 
61 	*f = ctx->v_fmt;
62 
63 	return 0;
64 }
65 
66 /* ------------------------------------------------------------------
67  *	V4L2 Video Node Centric IOCTLs
68  * ------------------------------------------------------------------
69  */
70 
71 static const struct cal_format_info *find_format_by_pix(struct cal_ctx *ctx,
72 							u32 pixelformat)
73 {
74 	const struct cal_format_info *fmtinfo;
75 	unsigned int k;
76 
77 	for (k = 0; k < ctx->num_active_fmt; k++) {
78 		fmtinfo = ctx->active_fmt[k];
79 		if (fmtinfo->fourcc == pixelformat)
80 			return fmtinfo;
81 	}
82 
83 	return NULL;
84 }
85 
86 static const struct cal_format_info *find_format_by_code(struct cal_ctx *ctx,
87 							 u32 code)
88 {
89 	const struct cal_format_info *fmtinfo;
90 	unsigned int k;
91 
92 	for (k = 0; k < ctx->num_active_fmt; k++) {
93 		fmtinfo = ctx->active_fmt[k];
94 		if (fmtinfo->code == code)
95 			return fmtinfo;
96 	}
97 
98 	return NULL;
99 }
100 
101 static int cal_legacy_enum_fmt_vid_cap(struct file *file, void *priv,
102 				       struct v4l2_fmtdesc *f)
103 {
104 	struct cal_ctx *ctx = video_drvdata(file);
105 	const struct cal_format_info *fmtinfo;
106 
107 	if (f->index >= ctx->num_active_fmt)
108 		return -EINVAL;
109 
110 	fmtinfo = ctx->active_fmt[f->index];
111 
112 	f->pixelformat = fmtinfo->fourcc;
113 	f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
114 	return 0;
115 }
116 
117 static int __subdev_get_format(struct cal_ctx *ctx,
118 			       struct v4l2_mbus_framefmt *fmt)
119 {
120 	struct v4l2_subdev_format sd_fmt;
121 	struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
122 	int ret;
123 
124 	sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
125 	sd_fmt.pad = 0;
126 
127 	ret = v4l2_subdev_call(ctx->phy->source, pad, get_fmt, NULL, &sd_fmt);
128 	if (ret)
129 		return ret;
130 
131 	*fmt = *mbus_fmt;
132 
133 	ctx_dbg(1, ctx, "%s %dx%d code:%04X\n", __func__,
134 		fmt->width, fmt->height, fmt->code);
135 
136 	return 0;
137 }
138 
139 static int __subdev_set_format(struct cal_ctx *ctx,
140 			       struct v4l2_mbus_framefmt *fmt)
141 {
142 	struct v4l2_subdev_format sd_fmt;
143 	struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
144 	int ret;
145 
146 	sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
147 	sd_fmt.pad = 0;
148 	*mbus_fmt = *fmt;
149 
150 	ret = v4l2_subdev_call(ctx->phy->source, pad, set_fmt, NULL, &sd_fmt);
151 	if (ret)
152 		return ret;
153 
154 	ctx_dbg(1, ctx, "%s %dx%d code:%04X\n", __func__,
155 		fmt->width, fmt->height, fmt->code);
156 
157 	return 0;
158 }
159 
160 static void cal_calc_format_size(struct cal_ctx *ctx,
161 				 const struct cal_format_info *fmtinfo,
162 				 struct v4l2_format *f)
163 {
164 	u32 bpl, max_width;
165 
166 	/*
167 	 * Maximum width is bound by the DMA max width in bytes.
168 	 * We need to recalculate the actual maxi width depending on the
169 	 * number of bytes per pixels required.
170 	 */
171 	max_width = CAL_MAX_WIDTH_BYTES / (ALIGN(fmtinfo->bpp, 8) >> 3);
172 	v4l_bound_align_image(&f->fmt.pix.width, 48, max_width, 2,
173 			      &f->fmt.pix.height, 32, CAL_MAX_HEIGHT_LINES,
174 			      0, 0);
175 
176 	bpl = (f->fmt.pix.width * ALIGN(fmtinfo->bpp, 8)) >> 3;
177 	f->fmt.pix.bytesperline = ALIGN(bpl, 16);
178 
179 	f->fmt.pix.sizeimage = f->fmt.pix.height *
180 			       f->fmt.pix.bytesperline;
181 
182 	ctx_dbg(3, ctx, "%s: fourcc: %s size: %dx%d bpl:%d img_size:%d\n",
183 		__func__, fourcc_to_str(f->fmt.pix.pixelformat),
184 		f->fmt.pix.width, f->fmt.pix.height,
185 		f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
186 }
187 
188 static int cal_legacy_try_fmt_vid_cap(struct file *file, void *priv,
189 				      struct v4l2_format *f)
190 {
191 	struct cal_ctx *ctx = video_drvdata(file);
192 	const struct cal_format_info *fmtinfo;
193 	struct v4l2_subdev_frame_size_enum fse;
194 	int found;
195 
196 	fmtinfo = find_format_by_pix(ctx, f->fmt.pix.pixelformat);
197 	if (!fmtinfo) {
198 		ctx_dbg(3, ctx, "Fourcc format (0x%08x) not found.\n",
199 			f->fmt.pix.pixelformat);
200 
201 		/* Just get the first one enumerated */
202 		fmtinfo = ctx->active_fmt[0];
203 		f->fmt.pix.pixelformat = fmtinfo->fourcc;
204 	}
205 
206 	f->fmt.pix.field = ctx->v_fmt.fmt.pix.field;
207 
208 	/* check for/find a valid width/height */
209 	found = false;
210 	fse.pad = 0;
211 	fse.code = fmtinfo->code;
212 	fse.which = V4L2_SUBDEV_FORMAT_ACTIVE;
213 	for (fse.index = 0; ; fse.index++) {
214 		int ret;
215 
216 		ret = v4l2_subdev_call(ctx->phy->source, pad, enum_frame_size,
217 				       NULL, &fse);
218 		if (ret)
219 			break;
220 
221 		if ((f->fmt.pix.width == fse.max_width) &&
222 		    (f->fmt.pix.height == fse.max_height)) {
223 			found = true;
224 			break;
225 		} else if ((f->fmt.pix.width >= fse.min_width) &&
226 			 (f->fmt.pix.width <= fse.max_width) &&
227 			 (f->fmt.pix.height >= fse.min_height) &&
228 			 (f->fmt.pix.height <= fse.max_height)) {
229 			found = true;
230 			break;
231 		}
232 	}
233 
234 	if (!found) {
235 		/* use existing values as default */
236 		f->fmt.pix.width = ctx->v_fmt.fmt.pix.width;
237 		f->fmt.pix.height =  ctx->v_fmt.fmt.pix.height;
238 	}
239 
240 	/*
241 	 * Use current colorspace for now, it will get
242 	 * updated properly during s_fmt
243 	 */
244 	f->fmt.pix.colorspace = ctx->v_fmt.fmt.pix.colorspace;
245 	cal_calc_format_size(ctx, fmtinfo, f);
246 	return 0;
247 }
248 
249 static int cal_legacy_s_fmt_vid_cap(struct file *file, void *priv,
250 				    struct v4l2_format *f)
251 {
252 	struct cal_ctx *ctx = video_drvdata(file);
253 	struct vb2_queue *q = &ctx->vb_vidq;
254 	struct v4l2_subdev_format sd_fmt = {
255 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
256 		.pad = CAL_CAMERARX_PAD_SINK,
257 	};
258 	const struct cal_format_info *fmtinfo;
259 	int ret;
260 
261 	if (vb2_is_busy(q)) {
262 		ctx_dbg(3, ctx, "%s device busy\n", __func__);
263 		return -EBUSY;
264 	}
265 
266 	ret = cal_legacy_try_fmt_vid_cap(file, priv, f);
267 	if (ret < 0)
268 		return ret;
269 
270 	fmtinfo = find_format_by_pix(ctx, f->fmt.pix.pixelformat);
271 
272 	v4l2_fill_mbus_format(&sd_fmt.format, &f->fmt.pix, fmtinfo->code);
273 
274 	ret = __subdev_set_format(ctx, &sd_fmt.format);
275 	if (ret)
276 		return ret;
277 
278 	/* Just double check nothing has gone wrong */
279 	if (sd_fmt.format.code != fmtinfo->code) {
280 		ctx_dbg(3, ctx,
281 			"%s subdev changed format on us, this should not happen\n",
282 			__func__);
283 		return -EINVAL;
284 	}
285 
286 	v4l2_fill_pix_format(&ctx->v_fmt.fmt.pix, &sd_fmt.format);
287 	ctx->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
288 	ctx->v_fmt.fmt.pix.pixelformat = fmtinfo->fourcc;
289 	ctx->v_fmt.fmt.pix.field = sd_fmt.format.field;
290 	cal_calc_format_size(ctx, fmtinfo, &ctx->v_fmt);
291 
292 	v4l2_subdev_call(&ctx->phy->subdev, pad, set_fmt, NULL, &sd_fmt);
293 
294 	ctx->fmtinfo = fmtinfo;
295 	*f = ctx->v_fmt;
296 
297 	return 0;
298 }
299 
300 static int cal_legacy_enum_framesizes(struct file *file, void *fh,
301 				      struct v4l2_frmsizeenum *fsize)
302 {
303 	struct cal_ctx *ctx = video_drvdata(file);
304 	const struct cal_format_info *fmtinfo;
305 	struct v4l2_subdev_frame_size_enum fse;
306 	int ret;
307 
308 	/* check for valid format */
309 	fmtinfo = find_format_by_pix(ctx, fsize->pixel_format);
310 	if (!fmtinfo) {
311 		ctx_dbg(3, ctx, "Invalid pixel code: %x\n",
312 			fsize->pixel_format);
313 		return -EINVAL;
314 	}
315 
316 	fse.index = fsize->index;
317 	fse.pad = 0;
318 	fse.code = fmtinfo->code;
319 	fse.which = V4L2_SUBDEV_FORMAT_ACTIVE;
320 
321 	ret = v4l2_subdev_call(ctx->phy->source, pad, enum_frame_size, NULL,
322 			       &fse);
323 	if (ret)
324 		return ret;
325 
326 	ctx_dbg(1, ctx, "%s: index: %d code: %x W:[%d,%d] H:[%d,%d]\n",
327 		__func__, fse.index, fse.code, fse.min_width, fse.max_width,
328 		fse.min_height, fse.max_height);
329 
330 	fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
331 	fsize->discrete.width = fse.max_width;
332 	fsize->discrete.height = fse.max_height;
333 
334 	return 0;
335 }
336 
337 static int cal_legacy_enum_input(struct file *file, void *priv,
338 				 struct v4l2_input *inp)
339 {
340 	if (inp->index > 0)
341 		return -EINVAL;
342 
343 	inp->type = V4L2_INPUT_TYPE_CAMERA;
344 	sprintf(inp->name, "Camera %u", inp->index);
345 	return 0;
346 }
347 
348 static int cal_legacy_g_input(struct file *file, void *priv, unsigned int *i)
349 {
350 	*i = 0;
351 	return 0;
352 }
353 
354 static int cal_legacy_s_input(struct file *file, void *priv, unsigned int i)
355 {
356 	return i > 0 ? -EINVAL : 0;
357 }
358 
359 /* timeperframe is arbitrary and continuous */
360 static int cal_legacy_enum_frameintervals(struct file *file, void *priv,
361 					  struct v4l2_frmivalenum *fival)
362 {
363 	struct cal_ctx *ctx = video_drvdata(file);
364 	const struct cal_format_info *fmtinfo;
365 	struct v4l2_subdev_frame_interval_enum fie = {
366 		.index = fival->index,
367 		.width = fival->width,
368 		.height = fival->height,
369 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
370 	};
371 	int ret;
372 
373 	fmtinfo = find_format_by_pix(ctx, fival->pixel_format);
374 	if (!fmtinfo)
375 		return -EINVAL;
376 
377 	fie.code = fmtinfo->code;
378 	ret = v4l2_subdev_call(ctx->phy->source, pad, enum_frame_interval,
379 			       NULL, &fie);
380 	if (ret)
381 		return ret;
382 	fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
383 	fival->discrete = fie.interval;
384 
385 	return 0;
386 }
387 
388 static int cal_legacy_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
389 {
390 	struct cal_ctx *ctx = video_drvdata(file);
391 
392 	return v4l2_g_parm_cap(video_devdata(file), ctx->phy->source, a);
393 }
394 
395 static int cal_legacy_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
396 {
397 	struct cal_ctx *ctx = video_drvdata(file);
398 
399 	return v4l2_s_parm_cap(video_devdata(file), ctx->phy->source, a);
400 }
401 
402 static const struct v4l2_ioctl_ops cal_ioctl_legacy_ops = {
403 	.vidioc_querycap      = cal_querycap,
404 	.vidioc_enum_fmt_vid_cap  = cal_legacy_enum_fmt_vid_cap,
405 	.vidioc_g_fmt_vid_cap     = cal_g_fmt_vid_cap,
406 	.vidioc_try_fmt_vid_cap   = cal_legacy_try_fmt_vid_cap,
407 	.vidioc_s_fmt_vid_cap     = cal_legacy_s_fmt_vid_cap,
408 	.vidioc_enum_framesizes   = cal_legacy_enum_framesizes,
409 	.vidioc_reqbufs       = vb2_ioctl_reqbufs,
410 	.vidioc_create_bufs   = vb2_ioctl_create_bufs,
411 	.vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
412 	.vidioc_querybuf      = vb2_ioctl_querybuf,
413 	.vidioc_qbuf          = vb2_ioctl_qbuf,
414 	.vidioc_dqbuf         = vb2_ioctl_dqbuf,
415 	.vidioc_expbuf        = vb2_ioctl_expbuf,
416 	.vidioc_enum_input    = cal_legacy_enum_input,
417 	.vidioc_g_input       = cal_legacy_g_input,
418 	.vidioc_s_input       = cal_legacy_s_input,
419 	.vidioc_enum_frameintervals = cal_legacy_enum_frameintervals,
420 	.vidioc_streamon      = vb2_ioctl_streamon,
421 	.vidioc_streamoff     = vb2_ioctl_streamoff,
422 	.vidioc_log_status    = v4l2_ctrl_log_status,
423 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
424 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
425 	.vidioc_g_parm		= cal_legacy_g_parm,
426 	.vidioc_s_parm		= cal_legacy_s_parm,
427 };
428 
429 /* ------------------------------------------------------------------
430  *	V4L2 Media Controller Centric IOCTLs
431  * ------------------------------------------------------------------
432  */
433 
434 static int cal_mc_enum_fmt_vid_cap(struct file *file, void  *priv,
435 				   struct v4l2_fmtdesc *f)
436 {
437 	unsigned int i;
438 	unsigned int idx;
439 
440 	if (f->index >= cal_num_formats)
441 		return -EINVAL;
442 
443 	idx = 0;
444 
445 	for (i = 0; i < cal_num_formats; ++i) {
446 		if (f->mbus_code && cal_formats[i].code != f->mbus_code)
447 			continue;
448 
449 		if (idx == f->index) {
450 			f->pixelformat = cal_formats[i].fourcc;
451 			f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
452 			return 0;
453 		}
454 
455 		idx++;
456 	}
457 
458 	return -EINVAL;
459 }
460 
461 static void cal_mc_try_fmt(struct cal_ctx *ctx, struct v4l2_format *f,
462 			   const struct cal_format_info **info)
463 {
464 	struct v4l2_pix_format *format = &f->fmt.pix;
465 	const struct cal_format_info *fmtinfo;
466 	unsigned int bpp;
467 
468 	/*
469 	 * Default to the first format if the requested pixel format code isn't
470 	 * supported.
471 	 */
472 	fmtinfo = cal_format_by_fourcc(f->fmt.pix.pixelformat);
473 	if (!fmtinfo)
474 		fmtinfo = &cal_formats[0];
475 
476 	/*
477 	 * Clamp the size, update the pixel format. The field and colorspace are
478 	 * accepted as-is, except for V4L2_FIELD_ANY that is turned into
479 	 * V4L2_FIELD_NONE.
480 	 */
481 	bpp = ALIGN(fmtinfo->bpp, 8);
482 
483 	format->width = clamp_t(unsigned int, format->width,
484 				CAL_MIN_WIDTH_BYTES * 8 / bpp,
485 				CAL_MAX_WIDTH_BYTES * 8 / bpp);
486 	format->height = clamp_t(unsigned int, format->height,
487 				 CAL_MIN_HEIGHT_LINES, CAL_MAX_HEIGHT_LINES);
488 	format->pixelformat = fmtinfo->fourcc;
489 
490 	if (format->field == V4L2_FIELD_ANY)
491 		format->field = V4L2_FIELD_NONE;
492 
493 	/*
494 	 * Calculate the number of bytes per line and the image size. The
495 	 * hardware stores the stride as a number of 16 bytes words, in a
496 	 * signed 15-bit value. Only 14 bits are thus usable.
497 	 */
498 	format->bytesperline = ALIGN(clamp(format->bytesperline,
499 					   format->width * bpp / 8,
500 					   ((1U << 14) - 1) * 16), 16);
501 
502 	format->sizeimage = format->height * format->bytesperline;
503 
504 	format->colorspace = ctx->v_fmt.fmt.pix.colorspace;
505 
506 	if (info)
507 		*info = fmtinfo;
508 
509 	ctx_dbg(3, ctx, "%s: %s %ux%u (bytesperline %u sizeimage %u)\n",
510 		__func__, fourcc_to_str(format->pixelformat),
511 		format->width, format->height,
512 		format->bytesperline, format->sizeimage);
513 }
514 
515 static int cal_mc_try_fmt_vid_cap(struct file *file, void *priv,
516 				  struct v4l2_format *f)
517 {
518 	struct cal_ctx *ctx = video_drvdata(file);
519 
520 	cal_mc_try_fmt(ctx, f, NULL);
521 	return 0;
522 }
523 
524 static int cal_mc_s_fmt_vid_cap(struct file *file, void *priv,
525 				struct v4l2_format *f)
526 {
527 	struct cal_ctx *ctx = video_drvdata(file);
528 	const struct cal_format_info *fmtinfo;
529 
530 	if (vb2_is_busy(&ctx->vb_vidq)) {
531 		ctx_dbg(3, ctx, "%s device busy\n", __func__);
532 		return -EBUSY;
533 	}
534 
535 	cal_mc_try_fmt(ctx, f, &fmtinfo);
536 
537 	ctx->v_fmt = *f;
538 	ctx->fmtinfo = fmtinfo;
539 
540 	return 0;
541 }
542 
543 static int cal_mc_enum_framesizes(struct file *file, void *fh,
544 				  struct v4l2_frmsizeenum *fsize)
545 {
546 	struct cal_ctx *ctx = video_drvdata(file);
547 	const struct cal_format_info *fmtinfo;
548 	unsigned int bpp;
549 
550 	if (fsize->index > 0)
551 		return -EINVAL;
552 
553 	fmtinfo = cal_format_by_fourcc(fsize->pixel_format);
554 	if (!fmtinfo) {
555 		ctx_dbg(3, ctx, "Invalid pixel format 0x%08x\n",
556 			fsize->pixel_format);
557 		return -EINVAL;
558 	}
559 
560 	bpp = ALIGN(fmtinfo->bpp, 8);
561 
562 	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
563 	fsize->stepwise.min_width = CAL_MIN_WIDTH_BYTES * 8 / bpp;
564 	fsize->stepwise.max_width = CAL_MAX_WIDTH_BYTES * 8 / bpp;
565 	fsize->stepwise.step_width = 64 / bpp;
566 	fsize->stepwise.min_height = CAL_MIN_HEIGHT_LINES;
567 	fsize->stepwise.max_height = CAL_MAX_HEIGHT_LINES;
568 	fsize->stepwise.step_height = 1;
569 
570 	return 0;
571 }
572 
573 static const struct v4l2_ioctl_ops cal_ioctl_mc_ops = {
574 	.vidioc_querycap      = cal_querycap,
575 	.vidioc_enum_fmt_vid_cap  = cal_mc_enum_fmt_vid_cap,
576 	.vidioc_g_fmt_vid_cap     = cal_g_fmt_vid_cap,
577 	.vidioc_try_fmt_vid_cap   = cal_mc_try_fmt_vid_cap,
578 	.vidioc_s_fmt_vid_cap     = cal_mc_s_fmt_vid_cap,
579 	.vidioc_enum_framesizes   = cal_mc_enum_framesizes,
580 	.vidioc_reqbufs       = vb2_ioctl_reqbufs,
581 	.vidioc_create_bufs   = vb2_ioctl_create_bufs,
582 	.vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
583 	.vidioc_querybuf      = vb2_ioctl_querybuf,
584 	.vidioc_qbuf          = vb2_ioctl_qbuf,
585 	.vidioc_dqbuf         = vb2_ioctl_dqbuf,
586 	.vidioc_expbuf        = vb2_ioctl_expbuf,
587 	.vidioc_streamon      = vb2_ioctl_streamon,
588 	.vidioc_streamoff     = vb2_ioctl_streamoff,
589 	.vidioc_log_status    = v4l2_ctrl_log_status,
590 };
591 
592 /* ------------------------------------------------------------------
593  *	videobuf2 Common Operations
594  * ------------------------------------------------------------------
595  */
596 
597 static int cal_queue_setup(struct vb2_queue *vq,
598 			   unsigned int *nbuffers, unsigned int *nplanes,
599 			   unsigned int sizes[], struct device *alloc_devs[])
600 {
601 	struct cal_ctx *ctx = vb2_get_drv_priv(vq);
602 	unsigned int size = ctx->v_fmt.fmt.pix.sizeimage;
603 
604 	if (vq->num_buffers + *nbuffers < 3)
605 		*nbuffers = 3 - vq->num_buffers;
606 
607 	if (*nplanes) {
608 		if (sizes[0] < size)
609 			return -EINVAL;
610 		size = sizes[0];
611 	}
612 
613 	*nplanes = 1;
614 	sizes[0] = size;
615 
616 	ctx_dbg(3, ctx, "nbuffers=%d, size=%d\n", *nbuffers, sizes[0]);
617 
618 	return 0;
619 }
620 
621 static int cal_buffer_prepare(struct vb2_buffer *vb)
622 {
623 	struct cal_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
624 	struct cal_buffer *buf = container_of(vb, struct cal_buffer,
625 					      vb.vb2_buf);
626 	unsigned long size;
627 
628 	size = ctx->v_fmt.fmt.pix.sizeimage;
629 	if (vb2_plane_size(vb, 0) < size) {
630 		ctx_err(ctx,
631 			"data will not fit into plane (%lu < %lu)\n",
632 			vb2_plane_size(vb, 0), size);
633 		return -EINVAL;
634 	}
635 
636 	vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
637 	return 0;
638 }
639 
640 static void cal_buffer_queue(struct vb2_buffer *vb)
641 {
642 	struct cal_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
643 	struct cal_buffer *buf = container_of(vb, struct cal_buffer,
644 					      vb.vb2_buf);
645 	unsigned long flags;
646 
647 	/* recheck locking */
648 	spin_lock_irqsave(&ctx->dma.lock, flags);
649 	list_add_tail(&buf->list, &ctx->dma.queue);
650 	spin_unlock_irqrestore(&ctx->dma.lock, flags);
651 }
652 
653 static void cal_release_buffers(struct cal_ctx *ctx,
654 				enum vb2_buffer_state state)
655 {
656 	struct cal_buffer *buf, *tmp;
657 
658 	/* Release all queued buffers. */
659 	spin_lock_irq(&ctx->dma.lock);
660 
661 	list_for_each_entry_safe(buf, tmp, &ctx->dma.queue, list) {
662 		list_del(&buf->list);
663 		vb2_buffer_done(&buf->vb.vb2_buf, state);
664 	}
665 
666 	if (ctx->dma.pending) {
667 		vb2_buffer_done(&ctx->dma.pending->vb.vb2_buf, state);
668 		ctx->dma.pending = NULL;
669 	}
670 
671 	if (ctx->dma.active) {
672 		vb2_buffer_done(&ctx->dma.active->vb.vb2_buf, state);
673 		ctx->dma.active = NULL;
674 	}
675 
676 	spin_unlock_irq(&ctx->dma.lock);
677 }
678 
679 /* ------------------------------------------------------------------
680  *	videobuf2 Operations
681  * ------------------------------------------------------------------
682  */
683 
684 static int cal_video_check_format(struct cal_ctx *ctx)
685 {
686 	const struct v4l2_mbus_framefmt *format;
687 	struct media_pad *remote_pad;
688 
689 	remote_pad = media_pad_remote_pad_first(&ctx->pad);
690 	if (!remote_pad)
691 		return -ENODEV;
692 
693 	format = &ctx->phy->formats[remote_pad->index];
694 
695 	if (ctx->fmtinfo->code != format->code ||
696 	    ctx->v_fmt.fmt.pix.height != format->height ||
697 	    ctx->v_fmt.fmt.pix.width != format->width ||
698 	    ctx->v_fmt.fmt.pix.field != format->field)
699 		return -EPIPE;
700 
701 	return 0;
702 }
703 
704 static int cal_start_streaming(struct vb2_queue *vq, unsigned int count)
705 {
706 	struct cal_ctx *ctx = vb2_get_drv_priv(vq);
707 	struct cal_buffer *buf;
708 	dma_addr_t addr;
709 	int ret;
710 
711 	ret = video_device_pipeline_alloc_start(&ctx->vdev);
712 	if (ret < 0) {
713 		ctx_err(ctx, "Failed to start media pipeline: %d\n", ret);
714 		goto error_release_buffers;
715 	}
716 
717 	/*
718 	 * Verify that the currently configured format matches the output of
719 	 * the connected CAMERARX.
720 	 */
721 	ret = cal_video_check_format(ctx);
722 	if (ret < 0) {
723 		ctx_dbg(3, ctx,
724 			"Format mismatch between CAMERARX and video node\n");
725 		goto error_pipeline;
726 	}
727 
728 	ret = cal_ctx_prepare(ctx);
729 	if (ret) {
730 		ctx_err(ctx, "Failed to prepare context: %d\n", ret);
731 		goto error_pipeline;
732 	}
733 
734 	spin_lock_irq(&ctx->dma.lock);
735 	buf = list_first_entry(&ctx->dma.queue, struct cal_buffer, list);
736 	ctx->dma.active = buf;
737 	list_del(&buf->list);
738 	spin_unlock_irq(&ctx->dma.lock);
739 
740 	addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
741 
742 	ret = pm_runtime_resume_and_get(ctx->cal->dev);
743 	if (ret < 0)
744 		goto error_pipeline;
745 
746 	cal_ctx_set_dma_addr(ctx, addr);
747 	cal_ctx_start(ctx);
748 
749 	ret = v4l2_subdev_call(&ctx->phy->subdev, video, s_stream, 1);
750 	if (ret)
751 		goto error_stop;
752 
753 	if (cal_debug >= 4)
754 		cal_quickdump_regs(ctx->cal);
755 
756 	return 0;
757 
758 error_stop:
759 	cal_ctx_stop(ctx);
760 	pm_runtime_put_sync(ctx->cal->dev);
761 	cal_ctx_unprepare(ctx);
762 
763 error_pipeline:
764 	video_device_pipeline_stop(&ctx->vdev);
765 error_release_buffers:
766 	cal_release_buffers(ctx, VB2_BUF_STATE_QUEUED);
767 
768 	return ret;
769 }
770 
771 static void cal_stop_streaming(struct vb2_queue *vq)
772 {
773 	struct cal_ctx *ctx = vb2_get_drv_priv(vq);
774 
775 	cal_ctx_stop(ctx);
776 
777 	v4l2_subdev_call(&ctx->phy->subdev, video, s_stream, 0);
778 
779 	pm_runtime_put_sync(ctx->cal->dev);
780 
781 	cal_ctx_unprepare(ctx);
782 
783 	cal_release_buffers(ctx, VB2_BUF_STATE_ERROR);
784 
785 	video_device_pipeline_stop(&ctx->vdev);
786 }
787 
788 static const struct vb2_ops cal_video_qops = {
789 	.queue_setup		= cal_queue_setup,
790 	.buf_prepare		= cal_buffer_prepare,
791 	.buf_queue		= cal_buffer_queue,
792 	.start_streaming	= cal_start_streaming,
793 	.stop_streaming		= cal_stop_streaming,
794 	.wait_prepare		= vb2_ops_wait_prepare,
795 	.wait_finish		= vb2_ops_wait_finish,
796 };
797 
798 /* ------------------------------------------------------------------
799  *	V4L2 Initialization and Registration
800  * ------------------------------------------------------------------
801  */
802 
803 static const struct v4l2_file_operations cal_fops = {
804 	.owner		= THIS_MODULE,
805 	.open           = v4l2_fh_open,
806 	.release        = vb2_fop_release,
807 	.poll		= vb2_fop_poll,
808 	.unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
809 	.mmap           = vb2_fop_mmap,
810 };
811 
812 static int cal_ctx_v4l2_init_formats(struct cal_ctx *ctx)
813 {
814 	struct v4l2_subdev_mbus_code_enum mbus_code;
815 	struct v4l2_mbus_framefmt mbus_fmt;
816 	const struct cal_format_info *fmtinfo;
817 	unsigned int i, j, k;
818 	int ret = 0;
819 
820 	/* Enumerate sub device formats and enable all matching local formats */
821 	ctx->active_fmt = devm_kcalloc(ctx->cal->dev, cal_num_formats,
822 				       sizeof(*ctx->active_fmt), GFP_KERNEL);
823 	if (!ctx->active_fmt)
824 		return -ENOMEM;
825 
826 	ctx->num_active_fmt = 0;
827 
828 	for (j = 0, i = 0; ; ++j) {
829 
830 		memset(&mbus_code, 0, sizeof(mbus_code));
831 		mbus_code.index = j;
832 		mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
833 		ret = v4l2_subdev_call(ctx->phy->source, pad, enum_mbus_code,
834 				       NULL, &mbus_code);
835 		if (ret == -EINVAL)
836 			break;
837 
838 		if (ret) {
839 			ctx_err(ctx, "Error enumerating mbus codes in subdev %s: %d\n",
840 				ctx->phy->source->name, ret);
841 			return ret;
842 		}
843 
844 		ctx_dbg(2, ctx,
845 			"subdev %s: code: %04x idx: %u\n",
846 			ctx->phy->source->name, mbus_code.code, j);
847 
848 		for (k = 0; k < cal_num_formats; k++) {
849 			fmtinfo = &cal_formats[k];
850 
851 			if (mbus_code.code == fmtinfo->code) {
852 				ctx->active_fmt[i] = fmtinfo;
853 				ctx_dbg(2, ctx,
854 					"matched fourcc: %s: code: %04x idx: %u\n",
855 					fourcc_to_str(fmtinfo->fourcc),
856 					fmtinfo->code, i);
857 				ctx->num_active_fmt = ++i;
858 			}
859 		}
860 	}
861 
862 	if (i == 0) {
863 		ctx_err(ctx, "No suitable format reported by subdev %s\n",
864 			ctx->phy->source->name);
865 		return -EINVAL;
866 	}
867 
868 	ret = __subdev_get_format(ctx, &mbus_fmt);
869 	if (ret)
870 		return ret;
871 
872 	fmtinfo = find_format_by_code(ctx, mbus_fmt.code);
873 	if (!fmtinfo) {
874 		ctx_dbg(3, ctx, "mbus code format (0x%08x) not found.\n",
875 			mbus_fmt.code);
876 		return -EINVAL;
877 	}
878 
879 	/* Save current format */
880 	v4l2_fill_pix_format(&ctx->v_fmt.fmt.pix, &mbus_fmt);
881 	ctx->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
882 	ctx->v_fmt.fmt.pix.pixelformat = fmtinfo->fourcc;
883 	cal_calc_format_size(ctx, fmtinfo, &ctx->v_fmt);
884 	ctx->fmtinfo = fmtinfo;
885 
886 	return 0;
887 }
888 
889 static int cal_ctx_v4l2_init_mc_format(struct cal_ctx *ctx)
890 {
891 	const struct cal_format_info *fmtinfo;
892 	struct v4l2_pix_format *pix_fmt = &ctx->v_fmt.fmt.pix;
893 
894 	fmtinfo = cal_format_by_code(MEDIA_BUS_FMT_UYVY8_2X8);
895 	if (!fmtinfo)
896 		return -EINVAL;
897 
898 	pix_fmt->width = 640;
899 	pix_fmt->height = 480;
900 	pix_fmt->field = V4L2_FIELD_NONE;
901 	pix_fmt->colorspace = V4L2_COLORSPACE_SRGB;
902 	pix_fmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
903 	pix_fmt->quantization = V4L2_QUANTIZATION_LIM_RANGE;
904 	pix_fmt->xfer_func = V4L2_XFER_FUNC_SRGB;
905 	pix_fmt->pixelformat = fmtinfo->fourcc;
906 
907 	ctx->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
908 
909 	/* Save current format */
910 	cal_calc_format_size(ctx, fmtinfo, &ctx->v_fmt);
911 	ctx->fmtinfo = fmtinfo;
912 
913 	return 0;
914 }
915 
916 int cal_ctx_v4l2_register(struct cal_ctx *ctx)
917 {
918 	struct video_device *vfd = &ctx->vdev;
919 	int ret;
920 
921 	if (!cal_mc_api) {
922 		struct v4l2_ctrl_handler *hdl = &ctx->ctrl_handler;
923 
924 		ret = cal_ctx_v4l2_init_formats(ctx);
925 		if (ret) {
926 			ctx_err(ctx, "Failed to init formats: %d\n", ret);
927 			return ret;
928 		}
929 
930 		ret = v4l2_ctrl_add_handler(hdl, ctx->phy->source->ctrl_handler,
931 					    NULL, true);
932 		if (ret < 0) {
933 			ctx_err(ctx, "Failed to add source ctrl handler\n");
934 			return ret;
935 		}
936 	} else {
937 		ret = cal_ctx_v4l2_init_mc_format(ctx);
938 		if (ret) {
939 			ctx_err(ctx, "Failed to init format: %d\n", ret);
940 			return ret;
941 		}
942 	}
943 
944 	ret = video_register_device(vfd, VFL_TYPE_VIDEO, cal_video_nr);
945 	if (ret < 0) {
946 		ctx_err(ctx, "Failed to register video device\n");
947 		return ret;
948 	}
949 
950 	ret = media_create_pad_link(&ctx->phy->subdev.entity,
951 				    CAL_CAMERARX_PAD_FIRST_SOURCE,
952 				    &vfd->entity, 0,
953 				    MEDIA_LNK_FL_IMMUTABLE |
954 				    MEDIA_LNK_FL_ENABLED);
955 	if (ret) {
956 		ctx_err(ctx, "Failed to create media link for context %u\n",
957 			ctx->dma_ctx);
958 		video_unregister_device(vfd);
959 		return ret;
960 	}
961 
962 	ctx_info(ctx, "V4L2 device registered as %s\n",
963 		 video_device_node_name(vfd));
964 
965 	return 0;
966 }
967 
968 void cal_ctx_v4l2_unregister(struct cal_ctx *ctx)
969 {
970 	ctx_dbg(1, ctx, "unregistering %s\n",
971 		video_device_node_name(&ctx->vdev));
972 
973 	video_unregister_device(&ctx->vdev);
974 }
975 
976 int cal_ctx_v4l2_init(struct cal_ctx *ctx)
977 {
978 	struct video_device *vfd = &ctx->vdev;
979 	struct vb2_queue *q = &ctx->vb_vidq;
980 	int ret;
981 
982 	INIT_LIST_HEAD(&ctx->dma.queue);
983 	spin_lock_init(&ctx->dma.lock);
984 	mutex_init(&ctx->mutex);
985 	init_waitqueue_head(&ctx->dma.wait);
986 
987 	/* Initialize the vb2 queue. */
988 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
989 	q->io_modes = VB2_MMAP | VB2_DMABUF;
990 	q->drv_priv = ctx;
991 	q->buf_struct_size = sizeof(struct cal_buffer);
992 	q->ops = &cal_video_qops;
993 	q->mem_ops = &vb2_dma_contig_memops;
994 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
995 	q->lock = &ctx->mutex;
996 	q->min_buffers_needed = 3;
997 	q->dev = ctx->cal->dev;
998 
999 	ret = vb2_queue_init(q);
1000 	if (ret)
1001 		return ret;
1002 
1003 	/* Initialize the video device and media entity. */
1004 	vfd->fops = &cal_fops;
1005 	vfd->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING
1006 			 | (cal_mc_api ? V4L2_CAP_IO_MC : 0);
1007 	vfd->v4l2_dev = &ctx->cal->v4l2_dev;
1008 	vfd->queue = q;
1009 	snprintf(vfd->name, sizeof(vfd->name), "CAL output %u", ctx->dma_ctx);
1010 	vfd->release = video_device_release_empty;
1011 	vfd->ioctl_ops = cal_mc_api ? &cal_ioctl_mc_ops : &cal_ioctl_legacy_ops;
1012 	vfd->lock = &ctx->mutex;
1013 	video_set_drvdata(vfd, ctx);
1014 
1015 	ctx->pad.flags = MEDIA_PAD_FL_SINK;
1016 	ret = media_entity_pads_init(&vfd->entity, 1, &ctx->pad);
1017 	if (ret < 0)
1018 		return ret;
1019 
1020 	if (!cal_mc_api) {
1021 		/* Initialize the control handler. */
1022 		struct v4l2_ctrl_handler *hdl = &ctx->ctrl_handler;
1023 
1024 		ret = v4l2_ctrl_handler_init(hdl, 11);
1025 		if (ret < 0) {
1026 			ctx_err(ctx, "Failed to init ctrl handler\n");
1027 			goto error;
1028 		}
1029 
1030 		vfd->ctrl_handler = hdl;
1031 	}
1032 
1033 	return 0;
1034 
1035 error:
1036 	media_entity_cleanup(&vfd->entity);
1037 	return ret;
1038 }
1039 
1040 void cal_ctx_v4l2_cleanup(struct cal_ctx *ctx)
1041 {
1042 	if (!cal_mc_api)
1043 		v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1044 
1045 	media_entity_cleanup(&ctx->vdev.entity);
1046 }
1047