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