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