xref: /linux/drivers/media/test-drivers/vivid/vivid-vid-out.c (revision fcc79e1714e8c2b8e216dc3149812edd37884eef)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vivid-vid-out.c - video output support functions.
4  *
5  * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6  */
7 
8 #include <linux/errno.h>
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/videodev2.h>
12 #include <linux/v4l2-dv-timings.h>
13 #include <media/v4l2-common.h>
14 #include <media/v4l2-event.h>
15 #include <media/v4l2-dv-timings.h>
16 #include <media/v4l2-rect.h>
17 
18 #include "vivid-core.h"
19 #include "vivid-vid-common.h"
20 #include "vivid-kthread-out.h"
21 #include "vivid-vid-out.h"
22 
23 static int vid_out_queue_setup(struct vb2_queue *vq,
24 		       unsigned *nbuffers, unsigned *nplanes,
25 		       unsigned sizes[], struct device *alloc_devs[])
26 {
27 	struct vivid_dev *dev = vb2_get_drv_priv(vq);
28 	const struct vivid_fmt *vfmt = dev->fmt_out;
29 	unsigned planes = vfmt->buffers;
30 	unsigned h = dev->fmt_out_rect.height;
31 	unsigned int size = dev->bytesperline_out[0] * h + vfmt->data_offset[0];
32 	unsigned p;
33 
34 	for (p = vfmt->buffers; p < vfmt->planes; p++)
35 		size += dev->bytesperline_out[p] * h / vfmt->vdownsampling[p] +
36 			vfmt->data_offset[p];
37 
38 	if (dev->field_out == V4L2_FIELD_ALTERNATE) {
39 		/*
40 		 * You cannot use write() with FIELD_ALTERNATE since the field
41 		 * information (TOP/BOTTOM) cannot be passed to the kernel.
42 		 */
43 		if (vb2_fileio_is_active(vq))
44 			return -EINVAL;
45 	}
46 
47 	if (dev->queue_setup_error) {
48 		/*
49 		 * Error injection: test what happens if queue_setup() returns
50 		 * an error.
51 		 */
52 		dev->queue_setup_error = false;
53 		return -EINVAL;
54 	}
55 
56 	if (*nplanes) {
57 		/*
58 		 * Check if the number of requested planes match
59 		 * the number of planes in the current format. You can't mix that.
60 		 */
61 		if (*nplanes != planes)
62 			return -EINVAL;
63 		if (sizes[0] < size)
64 			return -EINVAL;
65 		for (p = 1; p < planes; p++) {
66 			if (sizes[p] < dev->bytesperline_out[p] * h /
67 					vfmt->vdownsampling[p] +
68 					vfmt->data_offset[p])
69 				return -EINVAL;
70 		}
71 	} else {
72 		for (p = 0; p < planes; p++)
73 			sizes[p] = p ? dev->bytesperline_out[p] * h /
74 					vfmt->vdownsampling[p] +
75 					vfmt->data_offset[p] : size;
76 	}
77 
78 	*nplanes = planes;
79 
80 	dprintk(dev, 1, "%s: count=%u\n", __func__, *nbuffers);
81 	for (p = 0; p < planes; p++)
82 		dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
83 	return 0;
84 }
85 
86 static int vid_out_buf_out_validate(struct vb2_buffer *vb)
87 {
88 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
89 	struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
90 
91 	dprintk(dev, 1, "%s\n", __func__);
92 
93 	if (dev->field_out != V4L2_FIELD_ALTERNATE)
94 		vbuf->field = dev->field_out;
95 	else if (vbuf->field != V4L2_FIELD_TOP &&
96 		 vbuf->field != V4L2_FIELD_BOTTOM)
97 		return -EINVAL;
98 	return 0;
99 }
100 
101 static int vid_out_buf_prepare(struct vb2_buffer *vb)
102 {
103 	struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
104 	const struct vivid_fmt *vfmt = dev->fmt_out;
105 	unsigned int planes = vfmt->buffers;
106 	unsigned int h = dev->fmt_out_rect.height;
107 	unsigned int size = dev->bytesperline_out[0] * h;
108 	unsigned p;
109 
110 	for (p = vfmt->buffers; p < vfmt->planes; p++)
111 		size += dev->bytesperline_out[p] * h / vfmt->vdownsampling[p];
112 
113 	dprintk(dev, 1, "%s\n", __func__);
114 
115 	if (WARN_ON(NULL == dev->fmt_out))
116 		return -EINVAL;
117 
118 	if (dev->buf_prepare_error) {
119 		/*
120 		 * Error injection: test what happens if buf_prepare() returns
121 		 * an error.
122 		 */
123 		dev->buf_prepare_error = false;
124 		return -EINVAL;
125 	}
126 
127 	for (p = 0; p < planes; p++) {
128 		if (p)
129 			size = dev->bytesperline_out[p] * h / vfmt->vdownsampling[p];
130 		size += vb->planes[p].data_offset;
131 
132 		if (vb2_get_plane_payload(vb, p) < size) {
133 			dprintk(dev, 1, "%s the payload is too small for plane %u (%lu < %u)\n",
134 					__func__, p, vb2_get_plane_payload(vb, p), size);
135 			return -EINVAL;
136 		}
137 	}
138 
139 	return 0;
140 }
141 
142 static void vid_out_buf_queue(struct vb2_buffer *vb)
143 {
144 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
145 	struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
146 	struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
147 
148 	dprintk(dev, 1, "%s\n", __func__);
149 
150 	spin_lock(&dev->slock);
151 	list_add_tail(&buf->list, &dev->vid_out_active);
152 	spin_unlock(&dev->slock);
153 }
154 
155 static int vid_out_start_streaming(struct vb2_queue *vq, unsigned count)
156 {
157 	struct vivid_dev *dev = vb2_get_drv_priv(vq);
158 	int err;
159 
160 	dev->vid_out_seq_count = 0;
161 	dprintk(dev, 1, "%s\n", __func__);
162 	if (dev->start_streaming_error) {
163 		dev->start_streaming_error = false;
164 		err = -EINVAL;
165 	} else {
166 		err = vivid_start_generating_vid_out(dev, &dev->vid_out_streaming);
167 	}
168 	if (err) {
169 		struct vivid_buffer *buf, *tmp;
170 
171 		list_for_each_entry_safe(buf, tmp, &dev->vid_out_active, list) {
172 			list_del(&buf->list);
173 			vb2_buffer_done(&buf->vb.vb2_buf,
174 					VB2_BUF_STATE_QUEUED);
175 		}
176 	}
177 	return err;
178 }
179 
180 /* abort streaming and wait for last buffer */
181 static void vid_out_stop_streaming(struct vb2_queue *vq)
182 {
183 	struct vivid_dev *dev = vb2_get_drv_priv(vq);
184 
185 	dprintk(dev, 1, "%s\n", __func__);
186 	vivid_stop_generating_vid_out(dev, &dev->vid_out_streaming);
187 }
188 
189 static void vid_out_buf_request_complete(struct vb2_buffer *vb)
190 {
191 	struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
192 
193 	v4l2_ctrl_request_complete(vb->req_obj.req, &dev->ctrl_hdl_vid_out);
194 }
195 
196 const struct vb2_ops vivid_vid_out_qops = {
197 	.queue_setup		= vid_out_queue_setup,
198 	.buf_out_validate		= vid_out_buf_out_validate,
199 	.buf_prepare		= vid_out_buf_prepare,
200 	.buf_queue		= vid_out_buf_queue,
201 	.start_streaming	= vid_out_start_streaming,
202 	.stop_streaming		= vid_out_stop_streaming,
203 	.buf_request_complete	= vid_out_buf_request_complete,
204 };
205 
206 /*
207  * Called whenever the format has to be reset which can occur when
208  * changing outputs, standard, timings, etc.
209  */
210 void vivid_update_format_out(struct vivid_dev *dev)
211 {
212 	struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
213 	unsigned size, p;
214 	u64 pixelclock;
215 
216 	switch (dev->output_type[dev->output]) {
217 	case SVID:
218 	default:
219 		dev->field_out = dev->tv_field_out;
220 		dev->sink_rect.width = 720;
221 		if (dev->std_out & V4L2_STD_525_60) {
222 			dev->sink_rect.height = 480;
223 			dev->timeperframe_vid_out = (struct v4l2_fract) { 1001, 30000 };
224 			dev->service_set_out = V4L2_SLICED_CAPTION_525;
225 		} else {
226 			dev->sink_rect.height = 576;
227 			dev->timeperframe_vid_out = (struct v4l2_fract) { 1000, 25000 };
228 			dev->service_set_out = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
229 		}
230 		dev->colorspace_out = V4L2_COLORSPACE_SMPTE170M;
231 		break;
232 	case HDMI:
233 		dev->sink_rect.width = bt->width;
234 		dev->sink_rect.height = bt->height;
235 		size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
236 
237 		if (can_reduce_fps(bt) && (bt->flags & V4L2_DV_FL_REDUCED_FPS))
238 			pixelclock = div_u64(bt->pixelclock * 1000, 1001);
239 		else
240 			pixelclock = bt->pixelclock;
241 
242 		dev->timeperframe_vid_out = (struct v4l2_fract) {
243 			size / 100, (u32)pixelclock / 100
244 		};
245 		if (bt->interlaced)
246 			dev->field_out = V4L2_FIELD_ALTERNATE;
247 		else
248 			dev->field_out = V4L2_FIELD_NONE;
249 		if (!dev->dvi_d_out && (bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
250 			if (bt->width == 720 && bt->height <= 576)
251 				dev->colorspace_out = V4L2_COLORSPACE_SMPTE170M;
252 			else
253 				dev->colorspace_out = V4L2_COLORSPACE_REC709;
254 		} else {
255 			dev->colorspace_out = V4L2_COLORSPACE_SRGB;
256 		}
257 		break;
258 	}
259 	dev->xfer_func_out = V4L2_XFER_FUNC_DEFAULT;
260 	dev->ycbcr_enc_out = V4L2_YCBCR_ENC_DEFAULT;
261 	dev->hsv_enc_out = V4L2_HSV_ENC_180;
262 	dev->quantization_out = V4L2_QUANTIZATION_DEFAULT;
263 	dev->compose_out = dev->sink_rect;
264 	dev->compose_bounds_out = dev->sink_rect;
265 	dev->crop_out = dev->compose_out;
266 	if (V4L2_FIELD_HAS_T_OR_B(dev->field_out))
267 		dev->crop_out.height /= 2;
268 	dev->fmt_out_rect = dev->crop_out;
269 	for (p = 0; p < dev->fmt_out->planes; p++)
270 		dev->bytesperline_out[p] =
271 			(dev->sink_rect.width * dev->fmt_out->bit_depth[p]) / 8;
272 }
273 
274 /* Map the field to something that is valid for the current output */
275 static enum v4l2_field vivid_field_out(struct vivid_dev *dev, enum v4l2_field field)
276 {
277 	if (vivid_is_svid_out(dev)) {
278 		switch (field) {
279 		case V4L2_FIELD_INTERLACED_TB:
280 		case V4L2_FIELD_INTERLACED_BT:
281 		case V4L2_FIELD_SEQ_TB:
282 		case V4L2_FIELD_SEQ_BT:
283 		case V4L2_FIELD_ALTERNATE:
284 			return field;
285 		case V4L2_FIELD_INTERLACED:
286 		default:
287 			return V4L2_FIELD_INTERLACED;
288 		}
289 	}
290 	if (vivid_is_hdmi_out(dev))
291 		return dev->dv_timings_out.bt.interlaced ? V4L2_FIELD_ALTERNATE :
292 						       V4L2_FIELD_NONE;
293 	return V4L2_FIELD_NONE;
294 }
295 
296 static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
297 {
298 	if (vivid_is_svid_out(dev))
299 		return (dev->std_out & V4L2_STD_525_60) ?
300 			TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
301 
302 	if (vivid_is_hdmi_out(dev) &&
303 	    dev->sink_rect.width == 720 && dev->sink_rect.height <= 576)
304 		return dev->sink_rect.height == 480 ?
305 			TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
306 
307 	return TPG_PIXEL_ASPECT_SQUARE;
308 }
309 
310 int vivid_g_fmt_vid_out(struct file *file, void *priv,
311 					struct v4l2_format *f)
312 {
313 	struct vivid_dev *dev = video_drvdata(file);
314 	struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
315 	const struct vivid_fmt *fmt = dev->fmt_out;
316 	unsigned p;
317 
318 	mp->width        = dev->fmt_out_rect.width;
319 	mp->height       = dev->fmt_out_rect.height;
320 	mp->field        = dev->field_out;
321 	mp->pixelformat  = fmt->fourcc;
322 	mp->colorspace   = dev->colorspace_out;
323 	mp->xfer_func    = dev->xfer_func_out;
324 	mp->ycbcr_enc    = dev->ycbcr_enc_out;
325 	mp->quantization = dev->quantization_out;
326 	mp->num_planes = fmt->buffers;
327 	for (p = 0; p < mp->num_planes; p++) {
328 		mp->plane_fmt[p].bytesperline = dev->bytesperline_out[p];
329 		mp->plane_fmt[p].sizeimage =
330 			mp->plane_fmt[p].bytesperline * mp->height /
331 			fmt->vdownsampling[p] + fmt->data_offset[p];
332 	}
333 	for (p = fmt->buffers; p < fmt->planes; p++) {
334 		unsigned stride = dev->bytesperline_out[p];
335 
336 		mp->plane_fmt[0].sizeimage +=
337 			(stride * mp->height) / fmt->vdownsampling[p];
338 	}
339 	return 0;
340 }
341 
342 int vivid_try_fmt_vid_out(struct file *file, void *priv,
343 			struct v4l2_format *f)
344 {
345 	struct vivid_dev *dev = video_drvdata(file);
346 	struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
347 	struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
348 	struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
349 	const struct vivid_fmt *fmt;
350 	unsigned bytesperline, max_bpl;
351 	unsigned factor = 1;
352 	unsigned w, h;
353 	unsigned p;
354 
355 	fmt = vivid_get_format(dev, mp->pixelformat);
356 	if (!fmt) {
357 		dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
358 			mp->pixelformat);
359 		mp->pixelformat = V4L2_PIX_FMT_YUYV;
360 		fmt = vivid_get_format(dev, mp->pixelformat);
361 	}
362 
363 	mp->field = vivid_field_out(dev, mp->field);
364 	if (vivid_is_svid_out(dev)) {
365 		w = 720;
366 		h = (dev->std_out & V4L2_STD_525_60) ? 480 : 576;
367 	} else {
368 		w = dev->sink_rect.width;
369 		h = dev->sink_rect.height;
370 	}
371 	if (V4L2_FIELD_HAS_T_OR_B(mp->field))
372 		factor = 2;
373 	if (!dev->has_scaler_out && !dev->has_crop_out && !dev->has_compose_out) {
374 		mp->width = w;
375 		mp->height = h / factor;
376 	} else {
377 		struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
378 
379 		v4l2_rect_set_min_size(&r, &vivid_min_rect);
380 		v4l2_rect_set_max_size(&r, &vivid_max_rect);
381 		if (dev->has_scaler_out && !dev->has_crop_out) {
382 			struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
383 
384 			v4l2_rect_set_max_size(&r, &max_r);
385 		} else if (!dev->has_scaler_out && dev->has_compose_out && !dev->has_crop_out) {
386 			v4l2_rect_set_max_size(&r, &dev->sink_rect);
387 		} else if (!dev->has_scaler_out && !dev->has_compose_out) {
388 			v4l2_rect_set_min_size(&r, &dev->sink_rect);
389 		}
390 		mp->width = r.width;
391 		mp->height = r.height / factor;
392 	}
393 
394 	/* This driver supports custom bytesperline values */
395 
396 	mp->num_planes = fmt->buffers;
397 	for (p = 0; p < fmt->buffers; p++) {
398 		/* Calculate the minimum supported bytesperline value */
399 		bytesperline = (mp->width * fmt->bit_depth[p]) >> 3;
400 		/* Calculate the maximum supported bytesperline value */
401 		max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[p]) >> 3;
402 
403 		if (pfmt[p].bytesperline > max_bpl)
404 			pfmt[p].bytesperline = max_bpl;
405 		if (pfmt[p].bytesperline < bytesperline)
406 			pfmt[p].bytesperline = bytesperline;
407 
408 		pfmt[p].sizeimage = (pfmt[p].bytesperline * mp->height) /
409 				fmt->vdownsampling[p] + fmt->data_offset[p];
410 
411 		memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
412 	}
413 	for (p = fmt->buffers; p < fmt->planes; p++)
414 		pfmt[0].sizeimage += (pfmt[0].bytesperline * mp->height *
415 			(fmt->bit_depth[p] / fmt->vdownsampling[p])) /
416 			(fmt->bit_depth[0] / fmt->vdownsampling[0]);
417 
418 	mp->xfer_func = V4L2_XFER_FUNC_DEFAULT;
419 	mp->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
420 	mp->quantization = V4L2_QUANTIZATION_DEFAULT;
421 	if (vivid_is_svid_out(dev)) {
422 		mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
423 	} else if (dev->dvi_d_out || !(bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
424 		mp->colorspace = V4L2_COLORSPACE_SRGB;
425 		if (dev->dvi_d_out)
426 			mp->quantization = V4L2_QUANTIZATION_LIM_RANGE;
427 	} else if (bt->width == 720 && bt->height <= 576) {
428 		mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
429 	} else if (mp->colorspace != V4L2_COLORSPACE_SMPTE170M &&
430 		   mp->colorspace != V4L2_COLORSPACE_REC709 &&
431 		   mp->colorspace != V4L2_COLORSPACE_OPRGB &&
432 		   mp->colorspace != V4L2_COLORSPACE_BT2020 &&
433 		   mp->colorspace != V4L2_COLORSPACE_SRGB) {
434 		mp->colorspace = V4L2_COLORSPACE_REC709;
435 	}
436 	memset(mp->reserved, 0, sizeof(mp->reserved));
437 	return 0;
438 }
439 
440 int vivid_s_fmt_vid_out(struct file *file, void *priv,
441 					struct v4l2_format *f)
442 {
443 	struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
444 	struct vivid_dev *dev = video_drvdata(file);
445 	struct v4l2_rect *crop = &dev->crop_out;
446 	struct v4l2_rect *compose = &dev->compose_out;
447 	struct vb2_queue *q = &dev->vb_vid_out_q;
448 	int ret = vivid_try_fmt_vid_out(file, priv, f);
449 	unsigned factor = 1;
450 	unsigned p;
451 
452 	if (ret < 0)
453 		return ret;
454 
455 	if (vb2_is_busy(q) &&
456 	    (vivid_is_svid_out(dev) ||
457 	     mp->width != dev->fmt_out_rect.width ||
458 	     mp->height != dev->fmt_out_rect.height ||
459 	     mp->pixelformat != dev->fmt_out->fourcc ||
460 	     mp->field != dev->field_out)) {
461 		dprintk(dev, 1, "%s device busy\n", __func__);
462 		return -EBUSY;
463 	}
464 
465 	/*
466 	 * Allow for changing the colorspace on the fly. Useful for testing
467 	 * purposes, and it is something that HDMI transmitters are able
468 	 * to do.
469 	 */
470 	if (vb2_is_busy(q))
471 		goto set_colorspace;
472 
473 	dev->fmt_out = vivid_get_format(dev, mp->pixelformat);
474 	if (V4L2_FIELD_HAS_T_OR_B(mp->field))
475 		factor = 2;
476 
477 	if (dev->has_scaler_out || dev->has_crop_out || dev->has_compose_out) {
478 		struct v4l2_rect r = { 0, 0, mp->width, mp->height };
479 
480 		if (dev->has_scaler_out) {
481 			if (dev->has_crop_out)
482 				v4l2_rect_map_inside(crop, &r);
483 			else
484 				*crop = r;
485 			if (dev->has_compose_out && !dev->has_crop_out) {
486 				struct v4l2_rect min_r = {
487 					0, 0,
488 					r.width / MAX_ZOOM,
489 					factor * r.height / MAX_ZOOM
490 				};
491 				struct v4l2_rect max_r = {
492 					0, 0,
493 					r.width * MAX_ZOOM,
494 					factor * r.height * MAX_ZOOM
495 				};
496 
497 				v4l2_rect_set_min_size(compose, &min_r);
498 				v4l2_rect_set_max_size(compose, &max_r);
499 				v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
500 			} else if (dev->has_compose_out) {
501 				struct v4l2_rect min_r = {
502 					0, 0,
503 					crop->width / MAX_ZOOM,
504 					factor * crop->height / MAX_ZOOM
505 				};
506 				struct v4l2_rect max_r = {
507 					0, 0,
508 					crop->width * MAX_ZOOM,
509 					factor * crop->height * MAX_ZOOM
510 				};
511 
512 				v4l2_rect_set_min_size(compose, &min_r);
513 				v4l2_rect_set_max_size(compose, &max_r);
514 				v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
515 			}
516 		} else if (dev->has_compose_out && !dev->has_crop_out) {
517 			v4l2_rect_set_size_to(crop, &r);
518 			r.height *= factor;
519 			v4l2_rect_set_size_to(compose, &r);
520 			v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
521 		} else if (!dev->has_compose_out) {
522 			v4l2_rect_map_inside(crop, &r);
523 			r.height /= factor;
524 			v4l2_rect_set_size_to(compose, &r);
525 		} else {
526 			r.height *= factor;
527 			v4l2_rect_set_max_size(compose, &r);
528 			v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
529 			crop->top *= factor;
530 			crop->height *= factor;
531 			v4l2_rect_set_size_to(crop, compose);
532 			v4l2_rect_map_inside(crop, &r);
533 			crop->top /= factor;
534 			crop->height /= factor;
535 		}
536 	} else {
537 		struct v4l2_rect r = { 0, 0, mp->width, mp->height };
538 
539 		v4l2_rect_set_size_to(crop, &r);
540 		r.height /= factor;
541 		v4l2_rect_set_size_to(compose, &r);
542 	}
543 
544 	dev->fmt_out_rect.width = mp->width;
545 	dev->fmt_out_rect.height = mp->height;
546 	for (p = 0; p < mp->num_planes; p++)
547 		dev->bytesperline_out[p] = mp->plane_fmt[p].bytesperline;
548 	for (p = dev->fmt_out->buffers; p < dev->fmt_out->planes; p++)
549 		dev->bytesperline_out[p] =
550 			(dev->bytesperline_out[0] * dev->fmt_out->bit_depth[p]) /
551 			dev->fmt_out->bit_depth[0];
552 	dev->field_out = mp->field;
553 	if (vivid_is_svid_out(dev))
554 		dev->tv_field_out = mp->field;
555 
556 set_colorspace:
557 	dev->colorspace_out = mp->colorspace;
558 	dev->xfer_func_out = mp->xfer_func;
559 	dev->ycbcr_enc_out = mp->ycbcr_enc;
560 	dev->quantization_out = mp->quantization;
561 	struct vivid_dev *in_dev = vivid_output_is_connected_to(dev);
562 
563 	if (in_dev) {
564 		vivid_send_source_change(in_dev, SVID);
565 		vivid_send_source_change(in_dev, HDMI);
566 	}
567 	return 0;
568 }
569 
570 int vidioc_g_fmt_vid_out_mplane(struct file *file, void *priv,
571 					struct v4l2_format *f)
572 {
573 	struct vivid_dev *dev = video_drvdata(file);
574 
575 	if (!dev->multiplanar)
576 		return -ENOTTY;
577 	return vivid_g_fmt_vid_out(file, priv, f);
578 }
579 
580 int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
581 			struct v4l2_format *f)
582 {
583 	struct vivid_dev *dev = video_drvdata(file);
584 
585 	if (!dev->multiplanar)
586 		return -ENOTTY;
587 	return vivid_try_fmt_vid_out(file, priv, f);
588 }
589 
590 int vidioc_s_fmt_vid_out_mplane(struct file *file, void *priv,
591 			struct v4l2_format *f)
592 {
593 	struct vivid_dev *dev = video_drvdata(file);
594 
595 	if (!dev->multiplanar)
596 		return -ENOTTY;
597 	return vivid_s_fmt_vid_out(file, priv, f);
598 }
599 
600 int vidioc_g_fmt_vid_out(struct file *file, void *priv,
601 					struct v4l2_format *f)
602 {
603 	struct vivid_dev *dev = video_drvdata(file);
604 
605 	if (dev->multiplanar)
606 		return -ENOTTY;
607 	return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_out);
608 }
609 
610 int vidioc_try_fmt_vid_out(struct file *file, void *priv,
611 			struct v4l2_format *f)
612 {
613 	struct vivid_dev *dev = video_drvdata(file);
614 
615 	if (dev->multiplanar)
616 		return -ENOTTY;
617 	return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_out);
618 }
619 
620 int vidioc_s_fmt_vid_out(struct file *file, void *priv,
621 			struct v4l2_format *f)
622 {
623 	struct vivid_dev *dev = video_drvdata(file);
624 
625 	if (dev->multiplanar)
626 		return -ENOTTY;
627 	return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_out);
628 }
629 
630 int vivid_vid_out_g_selection(struct file *file, void *priv,
631 			      struct v4l2_selection *sel)
632 {
633 	struct vivid_dev *dev = video_drvdata(file);
634 
635 	if (!dev->has_crop_out && !dev->has_compose_out)
636 		return -ENOTTY;
637 	if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
638 		return -EINVAL;
639 
640 	sel->r.left = sel->r.top = 0;
641 	switch (sel->target) {
642 	case V4L2_SEL_TGT_CROP:
643 		if (!dev->has_crop_out)
644 			return -EINVAL;
645 		sel->r = dev->crop_out;
646 		break;
647 	case V4L2_SEL_TGT_CROP_DEFAULT:
648 		if (!dev->has_crop_out)
649 			return -EINVAL;
650 		sel->r = dev->fmt_out_rect;
651 		break;
652 	case V4L2_SEL_TGT_CROP_BOUNDS:
653 		if (!dev->has_crop_out)
654 			return -EINVAL;
655 		sel->r = vivid_max_rect;
656 		break;
657 	case V4L2_SEL_TGT_COMPOSE:
658 		if (!dev->has_compose_out)
659 			return -EINVAL;
660 		sel->r = dev->compose_out;
661 		break;
662 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
663 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
664 		if (!dev->has_compose_out)
665 			return -EINVAL;
666 		sel->r = dev->sink_rect;
667 		break;
668 	default:
669 		return -EINVAL;
670 	}
671 	return 0;
672 }
673 
674 int vivid_vid_out_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
675 {
676 	struct vivid_dev *dev = video_drvdata(file);
677 	struct v4l2_rect *crop = &dev->crop_out;
678 	struct v4l2_rect *compose = &dev->compose_out;
679 	unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_out) ? 2 : 1;
680 	int ret;
681 
682 	if (!dev->has_crop_out && !dev->has_compose_out)
683 		return -ENOTTY;
684 	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
685 		return -EINVAL;
686 
687 	switch (s->target) {
688 	case V4L2_SEL_TGT_CROP:
689 		if (!dev->has_crop_out)
690 			return -EINVAL;
691 		ret = vivid_vid_adjust_sel(s->flags, &s->r);
692 		if (ret)
693 			return ret;
694 		v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
695 		v4l2_rect_set_max_size(&s->r, &dev->fmt_out_rect);
696 		if (dev->has_scaler_out) {
697 			struct v4l2_rect max_rect = {
698 				0, 0,
699 				dev->sink_rect.width * MAX_ZOOM,
700 				(dev->sink_rect.height / factor) * MAX_ZOOM
701 			};
702 
703 			v4l2_rect_set_max_size(&s->r, &max_rect);
704 			if (dev->has_compose_out) {
705 				struct v4l2_rect min_rect = {
706 					0, 0,
707 					s->r.width / MAX_ZOOM,
708 					(s->r.height * factor) / MAX_ZOOM
709 				};
710 				struct v4l2_rect max_rect = {
711 					0, 0,
712 					s->r.width * MAX_ZOOM,
713 					(s->r.height * factor) * MAX_ZOOM
714 				};
715 
716 				v4l2_rect_set_min_size(compose, &min_rect);
717 				v4l2_rect_set_max_size(compose, &max_rect);
718 				v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
719 			}
720 		} else if (dev->has_compose_out) {
721 			s->r.top *= factor;
722 			s->r.height *= factor;
723 			v4l2_rect_set_max_size(&s->r, &dev->sink_rect);
724 			v4l2_rect_set_size_to(compose, &s->r);
725 			v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
726 			s->r.top /= factor;
727 			s->r.height /= factor;
728 		} else {
729 			v4l2_rect_set_size_to(&s->r, &dev->sink_rect);
730 			s->r.height /= factor;
731 		}
732 		v4l2_rect_map_inside(&s->r, &dev->fmt_out_rect);
733 		*crop = s->r;
734 		break;
735 	case V4L2_SEL_TGT_COMPOSE:
736 		if (!dev->has_compose_out)
737 			return -EINVAL;
738 		ret = vivid_vid_adjust_sel(s->flags, &s->r);
739 		if (ret)
740 			return ret;
741 		v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
742 		v4l2_rect_set_max_size(&s->r, &dev->sink_rect);
743 		v4l2_rect_map_inside(&s->r, &dev->compose_bounds_out);
744 		s->r.top /= factor;
745 		s->r.height /= factor;
746 		if (dev->has_scaler_out) {
747 			struct v4l2_rect fmt = dev->fmt_out_rect;
748 			struct v4l2_rect max_rect = {
749 				0, 0,
750 				s->r.width * MAX_ZOOM,
751 				s->r.height * MAX_ZOOM
752 			};
753 			struct v4l2_rect min_rect = {
754 				0, 0,
755 				s->r.width / MAX_ZOOM,
756 				s->r.height / MAX_ZOOM
757 			};
758 
759 			v4l2_rect_set_min_size(&fmt, &min_rect);
760 			if (!dev->has_crop_out)
761 				v4l2_rect_set_max_size(&fmt, &max_rect);
762 			if (!v4l2_rect_same_size(&dev->fmt_out_rect, &fmt) &&
763 			    vb2_is_busy(&dev->vb_vid_out_q))
764 				return -EBUSY;
765 			if (dev->has_crop_out) {
766 				v4l2_rect_set_min_size(crop, &min_rect);
767 				v4l2_rect_set_max_size(crop, &max_rect);
768 			}
769 			dev->fmt_out_rect = fmt;
770 		} else if (dev->has_crop_out) {
771 			struct v4l2_rect fmt = dev->fmt_out_rect;
772 
773 			v4l2_rect_set_min_size(&fmt, &s->r);
774 			if (!v4l2_rect_same_size(&dev->fmt_out_rect, &fmt) &&
775 			    vb2_is_busy(&dev->vb_vid_out_q))
776 				return -EBUSY;
777 			dev->fmt_out_rect = fmt;
778 			v4l2_rect_set_size_to(crop, &s->r);
779 			v4l2_rect_map_inside(crop, &dev->fmt_out_rect);
780 		} else {
781 			if (!v4l2_rect_same_size(&s->r, &dev->fmt_out_rect) &&
782 			    vb2_is_busy(&dev->vb_vid_out_q))
783 				return -EBUSY;
784 			v4l2_rect_set_size_to(&dev->fmt_out_rect, &s->r);
785 			v4l2_rect_set_size_to(crop, &s->r);
786 			crop->height /= factor;
787 			v4l2_rect_map_inside(crop, &dev->fmt_out_rect);
788 		}
789 		s->r.top *= factor;
790 		s->r.height *= factor;
791 		*compose = s->r;
792 		break;
793 	default:
794 		return -EINVAL;
795 	}
796 
797 	return 0;
798 }
799 
800 int vivid_vid_out_g_pixelaspect(struct file *file, void *priv,
801 				int type, struct v4l2_fract *f)
802 {
803 	struct vivid_dev *dev = video_drvdata(file);
804 
805 	if (type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
806 		return -EINVAL;
807 
808 	switch (vivid_get_pixel_aspect(dev)) {
809 	case TPG_PIXEL_ASPECT_NTSC:
810 		f->numerator = 11;
811 		f->denominator = 10;
812 		break;
813 	case TPG_PIXEL_ASPECT_PAL:
814 		f->numerator = 54;
815 		f->denominator = 59;
816 		break;
817 	default:
818 		break;
819 	}
820 	return 0;
821 }
822 
823 int vidioc_g_fmt_vid_out_overlay(struct file *file, void *priv,
824 					struct v4l2_format *f)
825 {
826 	struct vivid_dev *dev = video_drvdata(file);
827 	const struct v4l2_rect *compose = &dev->compose_out;
828 	struct v4l2_window *win = &f->fmt.win;
829 
830 	if (!dev->has_fb)
831 		return -EINVAL;
832 	win->w.top = dev->overlay_out_top;
833 	win->w.left = dev->overlay_out_left;
834 	win->w.width = compose->width;
835 	win->w.height = compose->height;
836 	win->field = V4L2_FIELD_ANY;
837 	win->chromakey = dev->chromakey_out;
838 	win->global_alpha = dev->global_alpha_out;
839 	return 0;
840 }
841 
842 int vidioc_try_fmt_vid_out_overlay(struct file *file, void *priv,
843 					struct v4l2_format *f)
844 {
845 	struct vivid_dev *dev = video_drvdata(file);
846 	const struct v4l2_rect *compose = &dev->compose_out;
847 	struct v4l2_window *win = &f->fmt.win;
848 
849 	if (!dev->has_fb)
850 		return -EINVAL;
851 	win->w.left = clamp_t(int, win->w.left,
852 			      -dev->display_width, dev->display_width);
853 	win->w.top = clamp_t(int, win->w.top,
854 			     -dev->display_height, dev->display_height);
855 	win->w.width = compose->width;
856 	win->w.height = compose->height;
857 	/*
858 	 * It makes no sense for an OSD to overlay only top or bottom fields,
859 	 * so always set this to ANY.
860 	 */
861 	win->field = V4L2_FIELD_ANY;
862 	return 0;
863 }
864 
865 int vidioc_s_fmt_vid_out_overlay(struct file *file, void *priv,
866 					struct v4l2_format *f)
867 {
868 	struct vivid_dev *dev = video_drvdata(file);
869 	struct v4l2_window *win = &f->fmt.win;
870 	int ret = vidioc_try_fmt_vid_out_overlay(file, priv, f);
871 
872 	if (ret)
873 		return ret;
874 
875 	dev->overlay_out_top = win->w.top;
876 	dev->overlay_out_left = win->w.left;
877 	dev->chromakey_out = win->chromakey;
878 	dev->global_alpha_out = win->global_alpha;
879 	return ret;
880 }
881 
882 int vivid_vid_out_overlay(struct file *file, void *fh, unsigned i)
883 {
884 	struct vivid_dev *dev = video_drvdata(file);
885 
886 	if (i && !dev->fmt_out->can_do_overlay) {
887 		dprintk(dev, 1, "unsupported output format for output overlay\n");
888 		return -EINVAL;
889 	}
890 
891 	dev->overlay_out_enabled = i;
892 	return 0;
893 }
894 
895 int vivid_vid_out_g_fbuf(struct file *file, void *fh,
896 				struct v4l2_framebuffer *a)
897 {
898 	struct vivid_dev *dev = video_drvdata(file);
899 
900 	a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
901 			V4L2_FBUF_CAP_CHROMAKEY |
902 			V4L2_FBUF_CAP_SRC_CHROMAKEY |
903 			V4L2_FBUF_CAP_GLOBAL_ALPHA |
904 			V4L2_FBUF_CAP_LOCAL_ALPHA |
905 			V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
906 	a->flags = V4L2_FBUF_FLAG_OVERLAY | dev->fbuf_out_flags;
907 	a->base = (void *)dev->video_pbase;
908 	a->fmt.width = dev->display_width;
909 	a->fmt.height = dev->display_height;
910 	if (dev->fb_defined.green.length == 5)
911 		a->fmt.pixelformat = V4L2_PIX_FMT_ARGB555;
912 	else
913 		a->fmt.pixelformat = V4L2_PIX_FMT_RGB565;
914 	a->fmt.bytesperline = dev->display_byte_stride;
915 	a->fmt.sizeimage = a->fmt.height * a->fmt.bytesperline;
916 	a->fmt.field = V4L2_FIELD_NONE;
917 	a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
918 	a->fmt.priv = 0;
919 	return 0;
920 }
921 
922 int vivid_vid_out_s_fbuf(struct file *file, void *fh,
923 				const struct v4l2_framebuffer *a)
924 {
925 	struct vivid_dev *dev = video_drvdata(file);
926 	const unsigned chroma_flags = V4L2_FBUF_FLAG_CHROMAKEY |
927 				      V4L2_FBUF_FLAG_SRC_CHROMAKEY;
928 	const unsigned alpha_flags = V4L2_FBUF_FLAG_GLOBAL_ALPHA |
929 				     V4L2_FBUF_FLAG_LOCAL_ALPHA |
930 				     V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
931 
932 
933 	if ((a->flags & chroma_flags) == chroma_flags)
934 		return -EINVAL;
935 	switch (a->flags & alpha_flags) {
936 	case 0:
937 	case V4L2_FBUF_FLAG_GLOBAL_ALPHA:
938 	case V4L2_FBUF_FLAG_LOCAL_ALPHA:
939 	case V4L2_FBUF_FLAG_LOCAL_INV_ALPHA:
940 		break;
941 	default:
942 		return -EINVAL;
943 	}
944 	dev->fbuf_out_flags &= ~(chroma_flags | alpha_flags);
945 	dev->fbuf_out_flags |= a->flags & (chroma_flags | alpha_flags);
946 	return 0;
947 }
948 
949 static const struct v4l2_audioout vivid_audio_outputs[] = {
950 	{ 0, "Line-Out 1" },
951 	{ 1, "Line-Out 2" },
952 };
953 
954 int vidioc_enum_output(struct file *file, void *priv,
955 				struct v4l2_output *out)
956 {
957 	struct vivid_dev *dev = video_drvdata(file);
958 
959 	if (out->index >= dev->num_outputs)
960 		return -EINVAL;
961 
962 	out->type = V4L2_OUTPUT_TYPE_ANALOG;
963 	switch (dev->output_type[out->index]) {
964 	case SVID:
965 		snprintf(out->name, sizeof(out->name), "S-Video %03u-%u",
966 			 dev->inst, dev->output_name_counter[out->index]);
967 		out->std = V4L2_STD_ALL;
968 		if (dev->has_audio_outputs)
969 			out->audioset = (1 << ARRAY_SIZE(vivid_audio_outputs)) - 1;
970 		out->capabilities = V4L2_OUT_CAP_STD;
971 		break;
972 	case HDMI:
973 		snprintf(out->name, sizeof(out->name), "HDMI %03u-%u",
974 			 dev->inst, dev->output_name_counter[out->index]);
975 		out->capabilities = V4L2_OUT_CAP_DV_TIMINGS;
976 		break;
977 	}
978 	return 0;
979 }
980 
981 int vidioc_g_output(struct file *file, void *priv, unsigned *o)
982 {
983 	struct vivid_dev *dev = video_drvdata(file);
984 
985 	*o = dev->output;
986 	return 0;
987 }
988 
989 int vidioc_s_output(struct file *file, void *priv, unsigned o)
990 {
991 	struct vivid_dev *dev = video_drvdata(file);
992 
993 	if (o >= dev->num_outputs)
994 		return -EINVAL;
995 
996 	if (o == dev->output)
997 		return 0;
998 
999 	if (vb2_is_busy(&dev->vb_vid_out_q) ||
1000 	    vb2_is_busy(&dev->vb_vbi_out_q) ||
1001 	    vb2_is_busy(&dev->vb_meta_out_q))
1002 		return -EBUSY;
1003 
1004 	dev->output = o;
1005 	dev->tv_audio_output = 0;
1006 	if (dev->output_type[o] == SVID)
1007 		dev->vid_out_dev.tvnorms = V4L2_STD_ALL;
1008 	else
1009 		dev->vid_out_dev.tvnorms = 0;
1010 
1011 	dev->vbi_out_dev.tvnorms = dev->vid_out_dev.tvnorms;
1012 	dev->meta_out_dev.tvnorms = dev->vid_out_dev.tvnorms;
1013 	vivid_update_format_out(dev);
1014 
1015 	return 0;
1016 }
1017 
1018 int vidioc_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vout)
1019 {
1020 	if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1021 		return -EINVAL;
1022 	*vout = vivid_audio_outputs[vout->index];
1023 	return 0;
1024 }
1025 
1026 int vidioc_g_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
1027 {
1028 	struct vivid_dev *dev = video_drvdata(file);
1029 
1030 	if (!vivid_is_svid_out(dev))
1031 		return -EINVAL;
1032 	*vout = vivid_audio_outputs[dev->tv_audio_output];
1033 	return 0;
1034 }
1035 
1036 int vidioc_s_audout(struct file *file, void *fh, const struct v4l2_audioout *vout)
1037 {
1038 	struct vivid_dev *dev = video_drvdata(file);
1039 
1040 	if (!vivid_is_svid_out(dev))
1041 		return -EINVAL;
1042 	if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1043 		return -EINVAL;
1044 	dev->tv_audio_output = vout->index;
1045 	return 0;
1046 }
1047 
1048 int vivid_vid_out_s_std(struct file *file, void *priv, v4l2_std_id id)
1049 {
1050 	struct vivid_dev *dev = video_drvdata(file);
1051 
1052 	if (!vivid_is_svid_out(dev))
1053 		return -ENODATA;
1054 	if (dev->std_out == id)
1055 		return 0;
1056 	if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
1057 		return -EBUSY;
1058 	dev->std_out = id;
1059 	vivid_update_format_out(dev);
1060 	return 0;
1061 }
1062 
1063 static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
1064 {
1065 	struct v4l2_bt_timings *bt = &timings->bt;
1066 
1067 	if ((bt->standards & (V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF)) &&
1068 	    v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap, NULL, NULL))
1069 		return true;
1070 
1071 	return false;
1072 }
1073 
1074 int vivid_vid_out_s_dv_timings(struct file *file, void *_fh,
1075 				    struct v4l2_dv_timings *timings)
1076 {
1077 	struct vivid_dev *dev = video_drvdata(file);
1078 	if (!vivid_is_hdmi_out(dev))
1079 		return -ENODATA;
1080 	if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
1081 				0, NULL, NULL) &&
1082 	    !valid_cvt_gtf_timings(timings))
1083 		return -EINVAL;
1084 	if (v4l2_match_dv_timings(timings, &dev->dv_timings_out, 0, true))
1085 		return 0;
1086 	if (vb2_is_busy(&dev->vb_vid_out_q))
1087 		return -EBUSY;
1088 	dev->dv_timings_out = *timings;
1089 	vivid_update_format_out(dev);
1090 	return 0;
1091 }
1092 
1093 int vivid_vid_out_g_parm(struct file *file, void *priv,
1094 			  struct v4l2_streamparm *parm)
1095 {
1096 	struct vivid_dev *dev = video_drvdata(file);
1097 
1098 	if (parm->type != (dev->multiplanar ?
1099 			   V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
1100 			   V4L2_BUF_TYPE_VIDEO_OUTPUT))
1101 		return -EINVAL;
1102 
1103 	parm->parm.output.capability   = V4L2_CAP_TIMEPERFRAME;
1104 	parm->parm.output.timeperframe = dev->timeperframe_vid_out;
1105 	parm->parm.output.writebuffers  = 1;
1106 
1107 	return 0;
1108 }
1109 
1110 int vidioc_subscribe_event(struct v4l2_fh *fh,
1111 			const struct v4l2_event_subscription *sub)
1112 {
1113 	switch (sub->type) {
1114 	case V4L2_EVENT_SOURCE_CHANGE:
1115 		if (fh->vdev->vfl_dir == VFL_DIR_RX)
1116 			return v4l2_src_change_event_subscribe(fh, sub);
1117 		break;
1118 	default:
1119 		return v4l2_ctrl_subscribe_event(fh, sub);
1120 	}
1121 	return -EINVAL;
1122 }
1123