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