xref: /OK3568_Linux_fs/kernel/drivers/media/test-drivers/vivid/vivid-vid-cap.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vivid-vid-cap.c - video capture 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/vmalloc.h>
12 #include <linux/videodev2.h>
13 #include <linux/v4l2-dv-timings.h>
14 #include <media/v4l2-common.h>
15 #include <media/v4l2-event.h>
16 #include <media/v4l2-dv-timings.h>
17 #include <media/v4l2-rect.h>
18 
19 #include "vivid-core.h"
20 #include "vivid-vid-common.h"
21 #include "vivid-kthread-cap.h"
22 #include "vivid-vid-cap.h"
23 
24 static const struct vivid_fmt formats_ovl[] = {
25 	{
26 		.fourcc   = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
27 		.vdownsampling = { 1 },
28 		.bit_depth = { 16 },
29 		.planes   = 1,
30 		.buffers = 1,
31 	},
32 	{
33 		.fourcc   = V4L2_PIX_FMT_XRGB555, /* gggbbbbb arrrrrgg */
34 		.vdownsampling = { 1 },
35 		.bit_depth = { 16 },
36 		.planes   = 1,
37 		.buffers = 1,
38 	},
39 	{
40 		.fourcc   = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */
41 		.vdownsampling = { 1 },
42 		.bit_depth = { 16 },
43 		.planes   = 1,
44 		.buffers = 1,
45 	},
46 };
47 
48 /* The number of discrete webcam framesizes */
49 #define VIVID_WEBCAM_SIZES 6
50 /* The number of discrete webcam frameintervals */
51 #define VIVID_WEBCAM_IVALS (VIVID_WEBCAM_SIZES * 2)
52 
53 /* Sizes must be in increasing order */
54 static const struct v4l2_frmsize_discrete webcam_sizes[VIVID_WEBCAM_SIZES] = {
55 	{  320, 180 },
56 	{  640, 360 },
57 	{  640, 480 },
58 	{ 1280, 720 },
59 	{ 1920, 1080 },
60 	{ 3840, 2160 },
61 };
62 
63 /*
64  * Intervals must be in increasing order and there must be twice as many
65  * elements in this array as there are in webcam_sizes.
66  */
67 static const struct v4l2_fract webcam_intervals[VIVID_WEBCAM_IVALS] = {
68 	{  1, 1 },
69 	{  1, 2 },
70 	{  1, 4 },
71 	{  1, 5 },
72 	{  1, 10 },
73 	{  2, 25 },
74 	{  1, 15 },
75 	{  1, 25 },
76 	{  1, 30 },
77 	{  1, 40 },
78 	{  1, 50 },
79 	{  1, 60 },
80 };
81 
vid_cap_queue_setup(struct vb2_queue * vq,unsigned * nbuffers,unsigned * nplanes,unsigned sizes[],struct device * alloc_devs[])82 static int vid_cap_queue_setup(struct vb2_queue *vq,
83 		       unsigned *nbuffers, unsigned *nplanes,
84 		       unsigned sizes[], struct device *alloc_devs[])
85 {
86 	struct vivid_dev *dev = vb2_get_drv_priv(vq);
87 	unsigned buffers = tpg_g_buffers(&dev->tpg);
88 	unsigned h = dev->fmt_cap_rect.height;
89 	unsigned p;
90 
91 	if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
92 		/*
93 		 * You cannot use read() with FIELD_ALTERNATE since the field
94 		 * information (TOP/BOTTOM) cannot be passed back to the user.
95 		 */
96 		if (vb2_fileio_is_active(vq))
97 			return -EINVAL;
98 	}
99 
100 	if (dev->queue_setup_error) {
101 		/*
102 		 * Error injection: test what happens if queue_setup() returns
103 		 * an error.
104 		 */
105 		dev->queue_setup_error = false;
106 		return -EINVAL;
107 	}
108 	if (*nplanes) {
109 		/*
110 		 * Check if the number of requested planes match
111 		 * the number of buffers in the current format. You can't mix that.
112 		 */
113 		if (*nplanes != buffers)
114 			return -EINVAL;
115 		for (p = 0; p < buffers; p++) {
116 			if (sizes[p] < tpg_g_line_width(&dev->tpg, p) * h +
117 						dev->fmt_cap->data_offset[p])
118 				return -EINVAL;
119 		}
120 	} else {
121 		for (p = 0; p < buffers; p++)
122 			sizes[p] = (tpg_g_line_width(&dev->tpg, p) * h) /
123 					dev->fmt_cap->vdownsampling[p] +
124 					dev->fmt_cap->data_offset[p];
125 	}
126 
127 	if (vq->num_buffers + *nbuffers < 2)
128 		*nbuffers = 2 - vq->num_buffers;
129 
130 	*nplanes = buffers;
131 
132 	dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers);
133 	for (p = 0; p < buffers; p++)
134 		dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
135 
136 	return 0;
137 }
138 
vid_cap_buf_prepare(struct vb2_buffer * vb)139 static int vid_cap_buf_prepare(struct vb2_buffer *vb)
140 {
141 	struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
142 	unsigned long size;
143 	unsigned buffers = tpg_g_buffers(&dev->tpg);
144 	unsigned p;
145 
146 	dprintk(dev, 1, "%s\n", __func__);
147 
148 	if (WARN_ON(NULL == dev->fmt_cap))
149 		return -EINVAL;
150 
151 	if (dev->buf_prepare_error) {
152 		/*
153 		 * Error injection: test what happens if buf_prepare() returns
154 		 * an error.
155 		 */
156 		dev->buf_prepare_error = false;
157 		return -EINVAL;
158 	}
159 	for (p = 0; p < buffers; p++) {
160 		size = (tpg_g_line_width(&dev->tpg, p) *
161 			dev->fmt_cap_rect.height) /
162 			dev->fmt_cap->vdownsampling[p] +
163 			dev->fmt_cap->data_offset[p];
164 
165 		if (vb2_plane_size(vb, p) < size) {
166 			dprintk(dev, 1, "%s data will not fit into plane %u (%lu < %lu)\n",
167 					__func__, p, vb2_plane_size(vb, p), size);
168 			return -EINVAL;
169 		}
170 
171 		vb2_set_plane_payload(vb, p, size);
172 		vb->planes[p].data_offset = dev->fmt_cap->data_offset[p];
173 	}
174 
175 	return 0;
176 }
177 
vid_cap_buf_finish(struct vb2_buffer * vb)178 static void vid_cap_buf_finish(struct vb2_buffer *vb)
179 {
180 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
181 	struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
182 	struct v4l2_timecode *tc = &vbuf->timecode;
183 	unsigned fps = 25;
184 	unsigned seq = vbuf->sequence;
185 
186 	if (!vivid_is_sdtv_cap(dev))
187 		return;
188 
189 	/*
190 	 * Set the timecode. Rarely used, so it is interesting to
191 	 * test this.
192 	 */
193 	vbuf->flags |= V4L2_BUF_FLAG_TIMECODE;
194 	if (dev->std_cap[dev->input] & V4L2_STD_525_60)
195 		fps = 30;
196 	tc->type = (fps == 30) ? V4L2_TC_TYPE_30FPS : V4L2_TC_TYPE_25FPS;
197 	tc->flags = 0;
198 	tc->frames = seq % fps;
199 	tc->seconds = (seq / fps) % 60;
200 	tc->minutes = (seq / (60 * fps)) % 60;
201 	tc->hours = (seq / (60 * 60 * fps)) % 24;
202 }
203 
vid_cap_buf_queue(struct vb2_buffer * vb)204 static void vid_cap_buf_queue(struct vb2_buffer *vb)
205 {
206 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
207 	struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
208 	struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
209 
210 	dprintk(dev, 1, "%s\n", __func__);
211 
212 	spin_lock(&dev->slock);
213 	list_add_tail(&buf->list, &dev->vid_cap_active);
214 	spin_unlock(&dev->slock);
215 }
216 
vid_cap_start_streaming(struct vb2_queue * vq,unsigned count)217 static int vid_cap_start_streaming(struct vb2_queue *vq, unsigned count)
218 {
219 	struct vivid_dev *dev = vb2_get_drv_priv(vq);
220 	unsigned i;
221 	int err;
222 
223 	if (vb2_is_streaming(&dev->vb_vid_out_q))
224 		dev->can_loop_video = vivid_vid_can_loop(dev);
225 
226 	dev->vid_cap_seq_count = 0;
227 	dprintk(dev, 1, "%s\n", __func__);
228 	for (i = 0; i < VIDEO_MAX_FRAME; i++)
229 		dev->must_blank[i] = tpg_g_perc_fill(&dev->tpg) < 100;
230 	if (dev->start_streaming_error) {
231 		dev->start_streaming_error = false;
232 		err = -EINVAL;
233 	} else {
234 		err = vivid_start_generating_vid_cap(dev, &dev->vid_cap_streaming);
235 	}
236 	if (err) {
237 		struct vivid_buffer *buf, *tmp;
238 
239 		list_for_each_entry_safe(buf, tmp, &dev->vid_cap_active, list) {
240 			list_del(&buf->list);
241 			vb2_buffer_done(&buf->vb.vb2_buf,
242 					VB2_BUF_STATE_QUEUED);
243 		}
244 	}
245 	return err;
246 }
247 
248 /* abort streaming and wait for last buffer */
vid_cap_stop_streaming(struct vb2_queue * vq)249 static void vid_cap_stop_streaming(struct vb2_queue *vq)
250 {
251 	struct vivid_dev *dev = vb2_get_drv_priv(vq);
252 
253 	dprintk(dev, 1, "%s\n", __func__);
254 	vivid_stop_generating_vid_cap(dev, &dev->vid_cap_streaming);
255 	dev->can_loop_video = false;
256 }
257 
vid_cap_buf_request_complete(struct vb2_buffer * vb)258 static void vid_cap_buf_request_complete(struct vb2_buffer *vb)
259 {
260 	struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
261 
262 	v4l2_ctrl_request_complete(vb->req_obj.req, &dev->ctrl_hdl_vid_cap);
263 }
264 
265 const struct vb2_ops vivid_vid_cap_qops = {
266 	.queue_setup		= vid_cap_queue_setup,
267 	.buf_prepare		= vid_cap_buf_prepare,
268 	.buf_finish		= vid_cap_buf_finish,
269 	.buf_queue		= vid_cap_buf_queue,
270 	.start_streaming	= vid_cap_start_streaming,
271 	.stop_streaming		= vid_cap_stop_streaming,
272 	.buf_request_complete	= vid_cap_buf_request_complete,
273 	.wait_prepare		= vb2_ops_wait_prepare,
274 	.wait_finish		= vb2_ops_wait_finish,
275 };
276 
277 /*
278  * Determine the 'picture' quality based on the current TV frequency: either
279  * COLOR for a good 'signal', GRAY (grayscale picture) for a slightly off
280  * signal or NOISE for no signal.
281  */
vivid_update_quality(struct vivid_dev * dev)282 void vivid_update_quality(struct vivid_dev *dev)
283 {
284 	unsigned freq_modulus;
285 
286 	if (dev->loop_video && (vivid_is_svid_cap(dev) || vivid_is_hdmi_cap(dev))) {
287 		/*
288 		 * The 'noise' will only be replaced by the actual video
289 		 * if the output video matches the input video settings.
290 		 */
291 		tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
292 		return;
293 	}
294 	if (vivid_is_hdmi_cap(dev) &&
295 	    VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode[dev->input])) {
296 		tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
297 		return;
298 	}
299 	if (vivid_is_sdtv_cap(dev) &&
300 	    VIVID_INVALID_SIGNAL(dev->std_signal_mode[dev->input])) {
301 		tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
302 		return;
303 	}
304 	if (!vivid_is_tv_cap(dev)) {
305 		tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
306 		return;
307 	}
308 
309 	/*
310 	 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
311 	 * From +/- 0.25 MHz around the channel there is color, and from
312 	 * +/- 1 MHz there is grayscale (chroma is lost).
313 	 * Everywhere else it is just noise.
314 	 */
315 	freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
316 	if (freq_modulus > 2 * 16) {
317 		tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE,
318 			next_pseudo_random32(dev->tv_freq ^ 0x55) & 0x3f);
319 		return;
320 	}
321 	if (freq_modulus < 12 /*0.75 * 16*/ || freq_modulus > 20 /*1.25 * 16*/)
322 		tpg_s_quality(&dev->tpg, TPG_QUAL_GRAY, 0);
323 	else
324 		tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
325 }
326 
327 /*
328  * Get the current picture quality and the associated afc value.
329  */
vivid_get_quality(struct vivid_dev * dev,s32 * afc)330 static enum tpg_quality vivid_get_quality(struct vivid_dev *dev, s32 *afc)
331 {
332 	unsigned freq_modulus;
333 
334 	if (afc)
335 		*afc = 0;
336 	if (tpg_g_quality(&dev->tpg) == TPG_QUAL_COLOR ||
337 	    tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE)
338 		return tpg_g_quality(&dev->tpg);
339 
340 	/*
341 	 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
342 	 * From +/- 0.25 MHz around the channel there is color, and from
343 	 * +/- 1 MHz there is grayscale (chroma is lost).
344 	 * Everywhere else it is just gray.
345 	 */
346 	freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
347 	if (afc)
348 		*afc = freq_modulus - 1 * 16;
349 	return TPG_QUAL_GRAY;
350 }
351 
vivid_get_video_aspect(const struct vivid_dev * dev)352 enum tpg_video_aspect vivid_get_video_aspect(const struct vivid_dev *dev)
353 {
354 	if (vivid_is_sdtv_cap(dev))
355 		return dev->std_aspect_ratio[dev->input];
356 
357 	if (vivid_is_hdmi_cap(dev))
358 		return dev->dv_timings_aspect_ratio[dev->input];
359 
360 	return TPG_VIDEO_ASPECT_IMAGE;
361 }
362 
vivid_get_pixel_aspect(const struct vivid_dev * dev)363 static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
364 {
365 	if (vivid_is_sdtv_cap(dev))
366 		return (dev->std_cap[dev->input] & V4L2_STD_525_60) ?
367 			TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
368 
369 	if (vivid_is_hdmi_cap(dev) &&
370 	    dev->src_rect.width == 720 && dev->src_rect.height <= 576)
371 		return dev->src_rect.height == 480 ?
372 			TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
373 
374 	return TPG_PIXEL_ASPECT_SQUARE;
375 }
376 
377 /*
378  * Called whenever the format has to be reset which can occur when
379  * changing inputs, standard, timings, etc.
380  */
vivid_update_format_cap(struct vivid_dev * dev,bool keep_controls)381 void vivid_update_format_cap(struct vivid_dev *dev, bool keep_controls)
382 {
383 	struct v4l2_bt_timings *bt = &dev->dv_timings_cap[dev->input].bt;
384 	unsigned size;
385 	u64 pixelclock;
386 
387 	switch (dev->input_type[dev->input]) {
388 	case WEBCAM:
389 	default:
390 		dev->src_rect.width = webcam_sizes[dev->webcam_size_idx].width;
391 		dev->src_rect.height = webcam_sizes[dev->webcam_size_idx].height;
392 		dev->timeperframe_vid_cap = webcam_intervals[dev->webcam_ival_idx];
393 		dev->field_cap = V4L2_FIELD_NONE;
394 		tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
395 		break;
396 	case TV:
397 	case SVID:
398 		dev->field_cap = dev->tv_field_cap;
399 		dev->src_rect.width = 720;
400 		if (dev->std_cap[dev->input] & V4L2_STD_525_60) {
401 			dev->src_rect.height = 480;
402 			dev->timeperframe_vid_cap = (struct v4l2_fract) { 1001, 30000 };
403 			dev->service_set_cap = V4L2_SLICED_CAPTION_525;
404 		} else {
405 			dev->src_rect.height = 576;
406 			dev->timeperframe_vid_cap = (struct v4l2_fract) { 1000, 25000 };
407 			dev->service_set_cap = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
408 		}
409 		tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
410 		break;
411 	case HDMI:
412 		dev->src_rect.width = bt->width;
413 		dev->src_rect.height = bt->height;
414 		size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
415 		if (dev->reduced_fps && can_reduce_fps(bt)) {
416 			pixelclock = div_u64(bt->pixelclock * 1000, 1001);
417 			bt->flags |= V4L2_DV_FL_REDUCED_FPS;
418 		} else {
419 			pixelclock = bt->pixelclock;
420 			bt->flags &= ~V4L2_DV_FL_REDUCED_FPS;
421 		}
422 		dev->timeperframe_vid_cap = (struct v4l2_fract) {
423 			size / 100, (u32)pixelclock / 100
424 		};
425 		if (bt->interlaced)
426 			dev->field_cap = V4L2_FIELD_ALTERNATE;
427 		else
428 			dev->field_cap = V4L2_FIELD_NONE;
429 
430 		/*
431 		 * We can be called from within s_ctrl, in that case we can't
432 		 * set/get controls. Luckily we don't need to in that case.
433 		 */
434 		if (keep_controls || !dev->colorspace)
435 			break;
436 		if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
437 			if (bt->width == 720 && bt->height <= 576)
438 				v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
439 			else
440 				v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
441 			v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 1);
442 		} else {
443 			v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
444 			v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 0);
445 		}
446 		tpg_s_rgb_range(&dev->tpg, v4l2_ctrl_g_ctrl(dev->rgb_range_cap));
447 		break;
448 	}
449 	vfree(dev->bitmap_cap);
450 	dev->bitmap_cap = NULL;
451 	vivid_update_quality(dev);
452 	tpg_reset_source(&dev->tpg, dev->src_rect.width, dev->src_rect.height, dev->field_cap);
453 	dev->crop_cap = dev->src_rect;
454 	dev->crop_bounds_cap = dev->src_rect;
455 	if (dev->bitmap_cap &&
456 	    (dev->compose_cap.width != dev->crop_cap.width ||
457 	     dev->compose_cap.height != dev->crop_cap.height)) {
458 		vfree(dev->bitmap_cap);
459 		dev->bitmap_cap = NULL;
460 	}
461 	dev->compose_cap = dev->crop_cap;
462 	if (V4L2_FIELD_HAS_T_OR_B(dev->field_cap))
463 		dev->compose_cap.height /= 2;
464 	dev->fmt_cap_rect = dev->compose_cap;
465 	tpg_s_video_aspect(&dev->tpg, vivid_get_video_aspect(dev));
466 	tpg_s_pixel_aspect(&dev->tpg, vivid_get_pixel_aspect(dev));
467 	tpg_update_mv_step(&dev->tpg);
468 }
469 
470 /* Map the field to something that is valid for the current input */
vivid_field_cap(struct vivid_dev * dev,enum v4l2_field field)471 static enum v4l2_field vivid_field_cap(struct vivid_dev *dev, enum v4l2_field field)
472 {
473 	if (vivid_is_sdtv_cap(dev)) {
474 		switch (field) {
475 		case V4L2_FIELD_INTERLACED_TB:
476 		case V4L2_FIELD_INTERLACED_BT:
477 		case V4L2_FIELD_SEQ_TB:
478 		case V4L2_FIELD_SEQ_BT:
479 		case V4L2_FIELD_TOP:
480 		case V4L2_FIELD_BOTTOM:
481 		case V4L2_FIELD_ALTERNATE:
482 			return field;
483 		case V4L2_FIELD_INTERLACED:
484 		default:
485 			return V4L2_FIELD_INTERLACED;
486 		}
487 	}
488 	if (vivid_is_hdmi_cap(dev))
489 		return dev->dv_timings_cap[dev->input].bt.interlaced ?
490 			V4L2_FIELD_ALTERNATE : V4L2_FIELD_NONE;
491 	return V4L2_FIELD_NONE;
492 }
493 
vivid_colorspace_cap(struct vivid_dev * dev)494 static unsigned vivid_colorspace_cap(struct vivid_dev *dev)
495 {
496 	if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
497 		return tpg_g_colorspace(&dev->tpg);
498 	return dev->colorspace_out;
499 }
500 
vivid_xfer_func_cap(struct vivid_dev * dev)501 static unsigned vivid_xfer_func_cap(struct vivid_dev *dev)
502 {
503 	if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
504 		return tpg_g_xfer_func(&dev->tpg);
505 	return dev->xfer_func_out;
506 }
507 
vivid_ycbcr_enc_cap(struct vivid_dev * dev)508 static unsigned vivid_ycbcr_enc_cap(struct vivid_dev *dev)
509 {
510 	if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
511 		return tpg_g_ycbcr_enc(&dev->tpg);
512 	return dev->ycbcr_enc_out;
513 }
514 
vivid_hsv_enc_cap(struct vivid_dev * dev)515 static unsigned int vivid_hsv_enc_cap(struct vivid_dev *dev)
516 {
517 	if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
518 		return tpg_g_hsv_enc(&dev->tpg);
519 	return dev->hsv_enc_out;
520 }
521 
vivid_quantization_cap(struct vivid_dev * dev)522 static unsigned vivid_quantization_cap(struct vivid_dev *dev)
523 {
524 	if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
525 		return tpg_g_quantization(&dev->tpg);
526 	return dev->quantization_out;
527 }
528 
vivid_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)529 int vivid_g_fmt_vid_cap(struct file *file, void *priv,
530 					struct v4l2_format *f)
531 {
532 	struct vivid_dev *dev = video_drvdata(file);
533 	struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
534 	unsigned p;
535 
536 	mp->width        = dev->fmt_cap_rect.width;
537 	mp->height       = dev->fmt_cap_rect.height;
538 	mp->field        = dev->field_cap;
539 	mp->pixelformat  = dev->fmt_cap->fourcc;
540 	mp->colorspace   = vivid_colorspace_cap(dev);
541 	mp->xfer_func    = vivid_xfer_func_cap(dev);
542 	if (dev->fmt_cap->color_enc == TGP_COLOR_ENC_HSV)
543 		mp->hsv_enc    = vivid_hsv_enc_cap(dev);
544 	else
545 		mp->ycbcr_enc    = vivid_ycbcr_enc_cap(dev);
546 	mp->quantization = vivid_quantization_cap(dev);
547 	mp->num_planes = dev->fmt_cap->buffers;
548 	for (p = 0; p < mp->num_planes; p++) {
549 		mp->plane_fmt[p].bytesperline = tpg_g_bytesperline(&dev->tpg, p);
550 		mp->plane_fmt[p].sizeimage =
551 			(tpg_g_line_width(&dev->tpg, p) * mp->height) /
552 			dev->fmt_cap->vdownsampling[p] +
553 			dev->fmt_cap->data_offset[p];
554 	}
555 	return 0;
556 }
557 
vivid_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)558 int vivid_try_fmt_vid_cap(struct file *file, void *priv,
559 			struct v4l2_format *f)
560 {
561 	struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
562 	struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
563 	struct vivid_dev *dev = video_drvdata(file);
564 	const struct vivid_fmt *fmt;
565 	unsigned bytesperline, max_bpl;
566 	unsigned factor = 1;
567 	unsigned w, h;
568 	unsigned p;
569 	bool user_set_csc = !!(mp->flags & V4L2_PIX_FMT_FLAG_SET_CSC);
570 
571 	fmt = vivid_get_format(dev, mp->pixelformat);
572 	if (!fmt) {
573 		dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
574 			mp->pixelformat);
575 		mp->pixelformat = V4L2_PIX_FMT_YUYV;
576 		fmt = vivid_get_format(dev, mp->pixelformat);
577 	}
578 
579 	mp->field = vivid_field_cap(dev, mp->field);
580 	if (vivid_is_webcam(dev)) {
581 		const struct v4l2_frmsize_discrete *sz =
582 			v4l2_find_nearest_size(webcam_sizes,
583 					       VIVID_WEBCAM_SIZES, width,
584 					       height, mp->width, mp->height);
585 
586 		w = sz->width;
587 		h = sz->height;
588 	} else if (vivid_is_sdtv_cap(dev)) {
589 		w = 720;
590 		h = (dev->std_cap[dev->input] & V4L2_STD_525_60) ? 480 : 576;
591 	} else {
592 		w = dev->src_rect.width;
593 		h = dev->src_rect.height;
594 	}
595 	if (V4L2_FIELD_HAS_T_OR_B(mp->field))
596 		factor = 2;
597 	if (vivid_is_webcam(dev) ||
598 	    (!dev->has_scaler_cap && !dev->has_crop_cap && !dev->has_compose_cap)) {
599 		mp->width = w;
600 		mp->height = h / factor;
601 	} else {
602 		struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
603 
604 		v4l2_rect_set_min_size(&r, &vivid_min_rect);
605 		v4l2_rect_set_max_size(&r, &vivid_max_rect);
606 		if (dev->has_scaler_cap && !dev->has_compose_cap) {
607 			struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
608 
609 			v4l2_rect_set_max_size(&r, &max_r);
610 		} else if (!dev->has_scaler_cap && dev->has_crop_cap && !dev->has_compose_cap) {
611 			v4l2_rect_set_max_size(&r, &dev->src_rect);
612 		} else if (!dev->has_scaler_cap && !dev->has_crop_cap) {
613 			v4l2_rect_set_min_size(&r, &dev->src_rect);
614 		}
615 		mp->width = r.width;
616 		mp->height = r.height / factor;
617 	}
618 
619 	/* This driver supports custom bytesperline values */
620 
621 	mp->num_planes = fmt->buffers;
622 	for (p = 0; p < fmt->buffers; p++) {
623 		/* Calculate the minimum supported bytesperline value */
624 		bytesperline = (mp->width * fmt->bit_depth[p]) >> 3;
625 		/* Calculate the maximum supported bytesperline value */
626 		max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[p]) >> 3;
627 
628 		if (pfmt[p].bytesperline > max_bpl)
629 			pfmt[p].bytesperline = max_bpl;
630 		if (pfmt[p].bytesperline < bytesperline)
631 			pfmt[p].bytesperline = bytesperline;
632 
633 		pfmt[p].sizeimage = (pfmt[p].bytesperline * mp->height) /
634 				fmt->vdownsampling[p] + fmt->data_offset[p];
635 
636 		memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
637 	}
638 	for (p = fmt->buffers; p < fmt->planes; p++)
639 		pfmt[0].sizeimage += (pfmt[0].bytesperline * mp->height *
640 			(fmt->bit_depth[p] / fmt->vdownsampling[p])) /
641 			(fmt->bit_depth[0] / fmt->vdownsampling[0]);
642 
643 	if (!user_set_csc || !v4l2_is_colorspace_valid(mp->colorspace))
644 		mp->colorspace = vivid_colorspace_cap(dev);
645 
646 	if (!user_set_csc || !v4l2_is_xfer_func_valid(mp->xfer_func))
647 		mp->xfer_func = vivid_xfer_func_cap(dev);
648 
649 	if (fmt->color_enc == TGP_COLOR_ENC_HSV) {
650 		if (!user_set_csc || !v4l2_is_hsv_enc_valid(mp->hsv_enc))
651 			mp->hsv_enc = vivid_hsv_enc_cap(dev);
652 	} else if (fmt->color_enc == TGP_COLOR_ENC_YCBCR) {
653 		if (!user_set_csc || !v4l2_is_ycbcr_enc_valid(mp->ycbcr_enc))
654 			mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
655 	} else {
656 		mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
657 	}
658 
659 	if (fmt->color_enc == TGP_COLOR_ENC_YCBCR ||
660 	    fmt->color_enc == TGP_COLOR_ENC_RGB) {
661 		if (!user_set_csc || !v4l2_is_quant_valid(mp->quantization))
662 			mp->quantization = vivid_quantization_cap(dev);
663 	} else {
664 		mp->quantization = vivid_quantization_cap(dev);
665 	}
666 
667 	memset(mp->reserved, 0, sizeof(mp->reserved));
668 	return 0;
669 }
670 
vivid_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)671 int vivid_s_fmt_vid_cap(struct file *file, void *priv,
672 					struct v4l2_format *f)
673 {
674 	struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
675 	struct vivid_dev *dev = video_drvdata(file);
676 	struct v4l2_rect *crop = &dev->crop_cap;
677 	struct v4l2_rect *compose = &dev->compose_cap;
678 	struct vb2_queue *q = &dev->vb_vid_cap_q;
679 	int ret = vivid_try_fmt_vid_cap(file, priv, f);
680 	unsigned factor = 1;
681 	unsigned p;
682 	unsigned i;
683 
684 	if (ret < 0)
685 		return ret;
686 
687 	if (vb2_is_busy(q)) {
688 		dprintk(dev, 1, "%s device busy\n", __func__);
689 		return -EBUSY;
690 	}
691 
692 	if (dev->overlay_cap_owner && dev->fb_cap.fmt.pixelformat != mp->pixelformat) {
693 		dprintk(dev, 1, "overlay is active, can't change pixelformat\n");
694 		return -EBUSY;
695 	}
696 
697 	dev->fmt_cap = vivid_get_format(dev, mp->pixelformat);
698 	if (V4L2_FIELD_HAS_T_OR_B(mp->field))
699 		factor = 2;
700 
701 	/* Note: the webcam input doesn't support scaling, cropping or composing */
702 
703 	if (!vivid_is_webcam(dev) &&
704 	    (dev->has_scaler_cap || dev->has_crop_cap || dev->has_compose_cap)) {
705 		struct v4l2_rect r = { 0, 0, mp->width, mp->height };
706 
707 		if (dev->has_scaler_cap) {
708 			if (dev->has_compose_cap)
709 				v4l2_rect_map_inside(compose, &r);
710 			else
711 				*compose = r;
712 			if (dev->has_crop_cap && !dev->has_compose_cap) {
713 				struct v4l2_rect min_r = {
714 					0, 0,
715 					r.width / MAX_ZOOM,
716 					factor * r.height / MAX_ZOOM
717 				};
718 				struct v4l2_rect max_r = {
719 					0, 0,
720 					r.width * MAX_ZOOM,
721 					factor * r.height * MAX_ZOOM
722 				};
723 
724 				v4l2_rect_set_min_size(crop, &min_r);
725 				v4l2_rect_set_max_size(crop, &max_r);
726 				v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
727 			} else if (dev->has_crop_cap) {
728 				struct v4l2_rect min_r = {
729 					0, 0,
730 					compose->width / MAX_ZOOM,
731 					factor * compose->height / MAX_ZOOM
732 				};
733 				struct v4l2_rect max_r = {
734 					0, 0,
735 					compose->width * MAX_ZOOM,
736 					factor * compose->height * MAX_ZOOM
737 				};
738 
739 				v4l2_rect_set_min_size(crop, &min_r);
740 				v4l2_rect_set_max_size(crop, &max_r);
741 				v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
742 			}
743 		} else if (dev->has_crop_cap && !dev->has_compose_cap) {
744 			r.height *= factor;
745 			v4l2_rect_set_size_to(crop, &r);
746 			v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
747 			r = *crop;
748 			r.height /= factor;
749 			v4l2_rect_set_size_to(compose, &r);
750 		} else if (!dev->has_crop_cap) {
751 			v4l2_rect_map_inside(compose, &r);
752 		} else {
753 			r.height *= factor;
754 			v4l2_rect_set_max_size(crop, &r);
755 			v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
756 			compose->top *= factor;
757 			compose->height *= factor;
758 			v4l2_rect_set_size_to(compose, crop);
759 			v4l2_rect_map_inside(compose, &r);
760 			compose->top /= factor;
761 			compose->height /= factor;
762 		}
763 	} else if (vivid_is_webcam(dev)) {
764 		/* Guaranteed to be a match */
765 		for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
766 			if (webcam_sizes[i].width == mp->width &&
767 					webcam_sizes[i].height == mp->height)
768 				break;
769 		dev->webcam_size_idx = i;
770 		if (dev->webcam_ival_idx >= 2 * (VIVID_WEBCAM_SIZES - i))
771 			dev->webcam_ival_idx = 2 * (VIVID_WEBCAM_SIZES - i) - 1;
772 		vivid_update_format_cap(dev, false);
773 	} else {
774 		struct v4l2_rect r = { 0, 0, mp->width, mp->height };
775 
776 		v4l2_rect_set_size_to(compose, &r);
777 		r.height *= factor;
778 		v4l2_rect_set_size_to(crop, &r);
779 	}
780 
781 	dev->fmt_cap_rect.width = mp->width;
782 	dev->fmt_cap_rect.height = mp->height;
783 	tpg_s_buf_height(&dev->tpg, mp->height);
784 	tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
785 	for (p = 0; p < tpg_g_buffers(&dev->tpg); p++)
786 		tpg_s_bytesperline(&dev->tpg, p, mp->plane_fmt[p].bytesperline);
787 	dev->field_cap = mp->field;
788 	if (dev->field_cap == V4L2_FIELD_ALTERNATE)
789 		tpg_s_field(&dev->tpg, V4L2_FIELD_TOP, true);
790 	else
791 		tpg_s_field(&dev->tpg, dev->field_cap, false);
792 	tpg_s_crop_compose(&dev->tpg, &dev->crop_cap, &dev->compose_cap);
793 	if (vivid_is_sdtv_cap(dev))
794 		dev->tv_field_cap = mp->field;
795 	tpg_update_mv_step(&dev->tpg);
796 	dev->tpg.colorspace = mp->colorspace;
797 	dev->tpg.xfer_func = mp->xfer_func;
798 	if (dev->fmt_cap->color_enc == TGP_COLOR_ENC_YCBCR)
799 		dev->tpg.ycbcr_enc = mp->ycbcr_enc;
800 	else
801 		dev->tpg.hsv_enc = mp->hsv_enc;
802 	dev->tpg.quantization = mp->quantization;
803 
804 	return 0;
805 }
806 
vidioc_g_fmt_vid_cap_mplane(struct file * file,void * priv,struct v4l2_format * f)807 int vidioc_g_fmt_vid_cap_mplane(struct file *file, void *priv,
808 					struct v4l2_format *f)
809 {
810 	struct vivid_dev *dev = video_drvdata(file);
811 
812 	if (!dev->multiplanar)
813 		return -ENOTTY;
814 	return vivid_g_fmt_vid_cap(file, priv, f);
815 }
816 
vidioc_try_fmt_vid_cap_mplane(struct file * file,void * priv,struct v4l2_format * f)817 int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
818 			struct v4l2_format *f)
819 {
820 	struct vivid_dev *dev = video_drvdata(file);
821 
822 	if (!dev->multiplanar)
823 		return -ENOTTY;
824 	return vivid_try_fmt_vid_cap(file, priv, f);
825 }
826 
vidioc_s_fmt_vid_cap_mplane(struct file * file,void * priv,struct v4l2_format * f)827 int vidioc_s_fmt_vid_cap_mplane(struct file *file, void *priv,
828 			struct v4l2_format *f)
829 {
830 	struct vivid_dev *dev = video_drvdata(file);
831 
832 	if (!dev->multiplanar)
833 		return -ENOTTY;
834 	return vivid_s_fmt_vid_cap(file, priv, f);
835 }
836 
vidioc_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)837 int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
838 					struct v4l2_format *f)
839 {
840 	struct vivid_dev *dev = video_drvdata(file);
841 
842 	if (dev->multiplanar)
843 		return -ENOTTY;
844 	return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_cap);
845 }
846 
vidioc_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)847 int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
848 			struct v4l2_format *f)
849 {
850 	struct vivid_dev *dev = video_drvdata(file);
851 
852 	if (dev->multiplanar)
853 		return -ENOTTY;
854 	return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_cap);
855 }
856 
vidioc_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)857 int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
858 			struct v4l2_format *f)
859 {
860 	struct vivid_dev *dev = video_drvdata(file);
861 
862 	if (dev->multiplanar)
863 		return -ENOTTY;
864 	return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_cap);
865 }
866 
vivid_vid_cap_g_selection(struct file * file,void * priv,struct v4l2_selection * sel)867 int vivid_vid_cap_g_selection(struct file *file, void *priv,
868 			      struct v4l2_selection *sel)
869 {
870 	struct vivid_dev *dev = video_drvdata(file);
871 
872 	if (!dev->has_crop_cap && !dev->has_compose_cap)
873 		return -ENOTTY;
874 	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
875 		return -EINVAL;
876 	if (vivid_is_webcam(dev))
877 		return -ENODATA;
878 
879 	sel->r.left = sel->r.top = 0;
880 	switch (sel->target) {
881 	case V4L2_SEL_TGT_CROP:
882 		if (!dev->has_crop_cap)
883 			return -EINVAL;
884 		sel->r = dev->crop_cap;
885 		break;
886 	case V4L2_SEL_TGT_CROP_DEFAULT:
887 	case V4L2_SEL_TGT_CROP_BOUNDS:
888 		if (!dev->has_crop_cap)
889 			return -EINVAL;
890 		sel->r = dev->src_rect;
891 		break;
892 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
893 		if (!dev->has_compose_cap)
894 			return -EINVAL;
895 		sel->r = vivid_max_rect;
896 		break;
897 	case V4L2_SEL_TGT_COMPOSE:
898 		if (!dev->has_compose_cap)
899 			return -EINVAL;
900 		sel->r = dev->compose_cap;
901 		break;
902 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
903 		if (!dev->has_compose_cap)
904 			return -EINVAL;
905 		sel->r = dev->fmt_cap_rect;
906 		break;
907 	default:
908 		return -EINVAL;
909 	}
910 	return 0;
911 }
912 
vivid_vid_cap_s_selection(struct file * file,void * fh,struct v4l2_selection * s)913 int vivid_vid_cap_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
914 {
915 	struct vivid_dev *dev = video_drvdata(file);
916 	struct v4l2_rect *crop = &dev->crop_cap;
917 	struct v4l2_rect *compose = &dev->compose_cap;
918 	unsigned orig_compose_w = compose->width;
919 	unsigned orig_compose_h = compose->height;
920 	unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1;
921 	int ret;
922 
923 	if (!dev->has_crop_cap && !dev->has_compose_cap)
924 		return -ENOTTY;
925 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
926 		return -EINVAL;
927 	if (vivid_is_webcam(dev))
928 		return -ENODATA;
929 
930 	switch (s->target) {
931 	case V4L2_SEL_TGT_CROP:
932 		if (!dev->has_crop_cap)
933 			return -EINVAL;
934 		ret = vivid_vid_adjust_sel(s->flags, &s->r);
935 		if (ret)
936 			return ret;
937 		v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
938 		v4l2_rect_set_max_size(&s->r, &dev->src_rect);
939 		v4l2_rect_map_inside(&s->r, &dev->crop_bounds_cap);
940 		s->r.top /= factor;
941 		s->r.height /= factor;
942 		if (dev->has_scaler_cap) {
943 			struct v4l2_rect fmt = dev->fmt_cap_rect;
944 			struct v4l2_rect max_rect = {
945 				0, 0,
946 				s->r.width * MAX_ZOOM,
947 				s->r.height * MAX_ZOOM
948 			};
949 			struct v4l2_rect min_rect = {
950 				0, 0,
951 				s->r.width / MAX_ZOOM,
952 				s->r.height / MAX_ZOOM
953 			};
954 
955 			v4l2_rect_set_min_size(&fmt, &min_rect);
956 			if (!dev->has_compose_cap)
957 				v4l2_rect_set_max_size(&fmt, &max_rect);
958 			if (!v4l2_rect_same_size(&dev->fmt_cap_rect, &fmt) &&
959 			    vb2_is_busy(&dev->vb_vid_cap_q))
960 				return -EBUSY;
961 			if (dev->has_compose_cap) {
962 				v4l2_rect_set_min_size(compose, &min_rect);
963 				v4l2_rect_set_max_size(compose, &max_rect);
964 			}
965 			dev->fmt_cap_rect = fmt;
966 			tpg_s_buf_height(&dev->tpg, fmt.height);
967 		} else if (dev->has_compose_cap) {
968 			struct v4l2_rect fmt = dev->fmt_cap_rect;
969 
970 			v4l2_rect_set_min_size(&fmt, &s->r);
971 			if (!v4l2_rect_same_size(&dev->fmt_cap_rect, &fmt) &&
972 			    vb2_is_busy(&dev->vb_vid_cap_q))
973 				return -EBUSY;
974 			dev->fmt_cap_rect = fmt;
975 			tpg_s_buf_height(&dev->tpg, fmt.height);
976 			v4l2_rect_set_size_to(compose, &s->r);
977 			v4l2_rect_map_inside(compose, &dev->fmt_cap_rect);
978 		} else {
979 			if (!v4l2_rect_same_size(&s->r, &dev->fmt_cap_rect) &&
980 			    vb2_is_busy(&dev->vb_vid_cap_q))
981 				return -EBUSY;
982 			v4l2_rect_set_size_to(&dev->fmt_cap_rect, &s->r);
983 			v4l2_rect_set_size_to(compose, &s->r);
984 			v4l2_rect_map_inside(compose, &dev->fmt_cap_rect);
985 			tpg_s_buf_height(&dev->tpg, dev->fmt_cap_rect.height);
986 		}
987 		s->r.top *= factor;
988 		s->r.height *= factor;
989 		*crop = s->r;
990 		break;
991 	case V4L2_SEL_TGT_COMPOSE:
992 		if (!dev->has_compose_cap)
993 			return -EINVAL;
994 		ret = vivid_vid_adjust_sel(s->flags, &s->r);
995 		if (ret)
996 			return ret;
997 		v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
998 		v4l2_rect_set_max_size(&s->r, &dev->fmt_cap_rect);
999 		if (dev->has_scaler_cap) {
1000 			struct v4l2_rect max_rect = {
1001 				0, 0,
1002 				dev->src_rect.width * MAX_ZOOM,
1003 				(dev->src_rect.height / factor) * MAX_ZOOM
1004 			};
1005 
1006 			v4l2_rect_set_max_size(&s->r, &max_rect);
1007 			if (dev->has_crop_cap) {
1008 				struct v4l2_rect min_rect = {
1009 					0, 0,
1010 					s->r.width / MAX_ZOOM,
1011 					(s->r.height * factor) / MAX_ZOOM
1012 				};
1013 				struct v4l2_rect max_rect = {
1014 					0, 0,
1015 					s->r.width * MAX_ZOOM,
1016 					(s->r.height * factor) * MAX_ZOOM
1017 				};
1018 
1019 				v4l2_rect_set_min_size(crop, &min_rect);
1020 				v4l2_rect_set_max_size(crop, &max_rect);
1021 				v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
1022 			}
1023 		} else if (dev->has_crop_cap) {
1024 			s->r.top *= factor;
1025 			s->r.height *= factor;
1026 			v4l2_rect_set_max_size(&s->r, &dev->src_rect);
1027 			v4l2_rect_set_size_to(crop, &s->r);
1028 			v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
1029 			s->r.top /= factor;
1030 			s->r.height /= factor;
1031 		} else {
1032 			v4l2_rect_set_size_to(&s->r, &dev->src_rect);
1033 			s->r.height /= factor;
1034 		}
1035 		v4l2_rect_map_inside(&s->r, &dev->fmt_cap_rect);
1036 		*compose = s->r;
1037 		break;
1038 	default:
1039 		return -EINVAL;
1040 	}
1041 
1042 	if (dev->bitmap_cap && (compose->width != orig_compose_w ||
1043 				compose->height != orig_compose_h)) {
1044 		vfree(dev->bitmap_cap);
1045 		dev->bitmap_cap = NULL;
1046 	}
1047 	tpg_s_crop_compose(&dev->tpg, crop, compose);
1048 	return 0;
1049 }
1050 
vivid_vid_cap_g_pixelaspect(struct file * file,void * priv,int type,struct v4l2_fract * f)1051 int vivid_vid_cap_g_pixelaspect(struct file *file, void *priv,
1052 				int type, struct v4l2_fract *f)
1053 {
1054 	struct vivid_dev *dev = video_drvdata(file);
1055 
1056 	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1057 		return -EINVAL;
1058 
1059 	switch (vivid_get_pixel_aspect(dev)) {
1060 	case TPG_PIXEL_ASPECT_NTSC:
1061 		f->numerator = 11;
1062 		f->denominator = 10;
1063 		break;
1064 	case TPG_PIXEL_ASPECT_PAL:
1065 		f->numerator = 54;
1066 		f->denominator = 59;
1067 		break;
1068 	default:
1069 		break;
1070 	}
1071 	return 0;
1072 }
1073 
vidioc_enum_fmt_vid_overlay(struct file * file,void * priv,struct v4l2_fmtdesc * f)1074 int vidioc_enum_fmt_vid_overlay(struct file *file, void  *priv,
1075 					struct v4l2_fmtdesc *f)
1076 {
1077 	struct vivid_dev *dev = video_drvdata(file);
1078 	const struct vivid_fmt *fmt;
1079 
1080 	if (dev->multiplanar)
1081 		return -ENOTTY;
1082 
1083 	if (f->index >= ARRAY_SIZE(formats_ovl))
1084 		return -EINVAL;
1085 
1086 	fmt = &formats_ovl[f->index];
1087 
1088 	f->pixelformat = fmt->fourcc;
1089 	return 0;
1090 }
1091 
vidioc_g_fmt_vid_overlay(struct file * file,void * priv,struct v4l2_format * f)1092 int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
1093 					struct v4l2_format *f)
1094 {
1095 	struct vivid_dev *dev = video_drvdata(file);
1096 	const struct v4l2_rect *compose = &dev->compose_cap;
1097 	struct v4l2_window *win = &f->fmt.win;
1098 	unsigned clipcount = win->clipcount;
1099 
1100 	if (dev->multiplanar)
1101 		return -ENOTTY;
1102 
1103 	win->w.top = dev->overlay_cap_top;
1104 	win->w.left = dev->overlay_cap_left;
1105 	win->w.width = compose->width;
1106 	win->w.height = compose->height;
1107 	win->field = dev->overlay_cap_field;
1108 	win->clipcount = dev->clipcount_cap;
1109 	if (clipcount > dev->clipcount_cap)
1110 		clipcount = dev->clipcount_cap;
1111 	if (dev->bitmap_cap == NULL)
1112 		win->bitmap = NULL;
1113 	else if (win->bitmap) {
1114 		if (copy_to_user(win->bitmap, dev->bitmap_cap,
1115 		    ((compose->width + 7) / 8) * compose->height))
1116 			return -EFAULT;
1117 	}
1118 	if (clipcount && win->clips) {
1119 		if (copy_to_user(win->clips, dev->clips_cap,
1120 				 clipcount * sizeof(dev->clips_cap[0])))
1121 			return -EFAULT;
1122 	}
1123 	return 0;
1124 }
1125 
vidioc_try_fmt_vid_overlay(struct file * file,void * priv,struct v4l2_format * f)1126 int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
1127 					struct v4l2_format *f)
1128 {
1129 	struct vivid_dev *dev = video_drvdata(file);
1130 	const struct v4l2_rect *compose = &dev->compose_cap;
1131 	struct v4l2_window *win = &f->fmt.win;
1132 	int i, j;
1133 
1134 	if (dev->multiplanar)
1135 		return -ENOTTY;
1136 
1137 	win->w.left = clamp_t(int, win->w.left,
1138 			      -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
1139 	win->w.top = clamp_t(int, win->w.top,
1140 			     -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
1141 	win->w.width = compose->width;
1142 	win->w.height = compose->height;
1143 	if (win->field != V4L2_FIELD_BOTTOM && win->field != V4L2_FIELD_TOP)
1144 		win->field = V4L2_FIELD_ANY;
1145 	win->chromakey = 0;
1146 	win->global_alpha = 0;
1147 	if (win->clipcount && !win->clips)
1148 		win->clipcount = 0;
1149 	if (win->clipcount > MAX_CLIPS)
1150 		win->clipcount = MAX_CLIPS;
1151 	if (win->clipcount) {
1152 		if (copy_from_user(dev->try_clips_cap, win->clips,
1153 				   win->clipcount * sizeof(dev->clips_cap[0])))
1154 			return -EFAULT;
1155 		for (i = 0; i < win->clipcount; i++) {
1156 			struct v4l2_rect *r = &dev->try_clips_cap[i].c;
1157 
1158 			r->top = clamp_t(s32, r->top, 0, dev->fb_cap.fmt.height - 1);
1159 			r->height = clamp_t(s32, r->height, 1, dev->fb_cap.fmt.height - r->top);
1160 			r->left = clamp_t(u32, r->left, 0, dev->fb_cap.fmt.width - 1);
1161 			r->width = clamp_t(u32, r->width, 1, dev->fb_cap.fmt.width - r->left);
1162 		}
1163 		/*
1164 		 * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
1165 		 * number and it's typically a one-time deal.
1166 		 */
1167 		for (i = 0; i < win->clipcount - 1; i++) {
1168 			struct v4l2_rect *r1 = &dev->try_clips_cap[i].c;
1169 
1170 			for (j = i + 1; j < win->clipcount; j++) {
1171 				struct v4l2_rect *r2 = &dev->try_clips_cap[j].c;
1172 
1173 				if (v4l2_rect_overlap(r1, r2))
1174 					return -EINVAL;
1175 			}
1176 		}
1177 		if (copy_to_user(win->clips, dev->try_clips_cap,
1178 				 win->clipcount * sizeof(dev->clips_cap[0])))
1179 			return -EFAULT;
1180 	}
1181 	return 0;
1182 }
1183 
vidioc_s_fmt_vid_overlay(struct file * file,void * priv,struct v4l2_format * f)1184 int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
1185 					struct v4l2_format *f)
1186 {
1187 	struct vivid_dev *dev = video_drvdata(file);
1188 	const struct v4l2_rect *compose = &dev->compose_cap;
1189 	struct v4l2_window *win = &f->fmt.win;
1190 	int ret = vidioc_try_fmt_vid_overlay(file, priv, f);
1191 	unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
1192 	unsigned clips_size = win->clipcount * sizeof(dev->clips_cap[0]);
1193 	void *new_bitmap = NULL;
1194 
1195 	if (ret)
1196 		return ret;
1197 
1198 	if (win->bitmap) {
1199 		new_bitmap = vzalloc(bitmap_size);
1200 
1201 		if (new_bitmap == NULL)
1202 			return -ENOMEM;
1203 		if (copy_from_user(new_bitmap, win->bitmap, bitmap_size)) {
1204 			vfree(new_bitmap);
1205 			return -EFAULT;
1206 		}
1207 	}
1208 
1209 	dev->overlay_cap_top = win->w.top;
1210 	dev->overlay_cap_left = win->w.left;
1211 	dev->overlay_cap_field = win->field;
1212 	vfree(dev->bitmap_cap);
1213 	dev->bitmap_cap = new_bitmap;
1214 	dev->clipcount_cap = win->clipcount;
1215 	if (dev->clipcount_cap)
1216 		memcpy(dev->clips_cap, dev->try_clips_cap, clips_size);
1217 	return 0;
1218 }
1219 
vivid_vid_cap_overlay(struct file * file,void * fh,unsigned i)1220 int vivid_vid_cap_overlay(struct file *file, void *fh, unsigned i)
1221 {
1222 	struct vivid_dev *dev = video_drvdata(file);
1223 
1224 	if (dev->multiplanar)
1225 		return -ENOTTY;
1226 
1227 	if (i && dev->fb_vbase_cap == NULL)
1228 		return -EINVAL;
1229 
1230 	if (i && dev->fb_cap.fmt.pixelformat != dev->fmt_cap->fourcc) {
1231 		dprintk(dev, 1, "mismatch between overlay and video capture pixelformats\n");
1232 		return -EINVAL;
1233 	}
1234 
1235 	if (dev->overlay_cap_owner && dev->overlay_cap_owner != fh)
1236 		return -EBUSY;
1237 	dev->overlay_cap_owner = i ? fh : NULL;
1238 	return 0;
1239 }
1240 
vivid_vid_cap_g_fbuf(struct file * file,void * fh,struct v4l2_framebuffer * a)1241 int vivid_vid_cap_g_fbuf(struct file *file, void *fh,
1242 				struct v4l2_framebuffer *a)
1243 {
1244 	struct vivid_dev *dev = video_drvdata(file);
1245 
1246 	if (dev->multiplanar)
1247 		return -ENOTTY;
1248 
1249 	*a = dev->fb_cap;
1250 	a->capability = V4L2_FBUF_CAP_BITMAP_CLIPPING |
1251 			V4L2_FBUF_CAP_LIST_CLIPPING;
1252 	a->flags = V4L2_FBUF_FLAG_PRIMARY;
1253 	a->fmt.field = V4L2_FIELD_NONE;
1254 	a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
1255 	a->fmt.priv = 0;
1256 	return 0;
1257 }
1258 
vivid_vid_cap_s_fbuf(struct file * file,void * fh,const struct v4l2_framebuffer * a)1259 int vivid_vid_cap_s_fbuf(struct file *file, void *fh,
1260 				const struct v4l2_framebuffer *a)
1261 {
1262 	struct vivid_dev *dev = video_drvdata(file);
1263 	const struct vivid_fmt *fmt;
1264 
1265 	if (dev->multiplanar)
1266 		return -ENOTTY;
1267 
1268 	if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1269 		return -EPERM;
1270 
1271 	if (dev->overlay_cap_owner)
1272 		return -EBUSY;
1273 
1274 	if (a->base == NULL) {
1275 		dev->fb_cap.base = NULL;
1276 		dev->fb_vbase_cap = NULL;
1277 		return 0;
1278 	}
1279 
1280 	if (a->fmt.width < 48 || a->fmt.height < 32)
1281 		return -EINVAL;
1282 	fmt = vivid_get_format(dev, a->fmt.pixelformat);
1283 	if (!fmt || !fmt->can_do_overlay)
1284 		return -EINVAL;
1285 	if (a->fmt.bytesperline < (a->fmt.width * fmt->bit_depth[0]) / 8)
1286 		return -EINVAL;
1287 	if (a->fmt.bytesperline > a->fmt.sizeimage / a->fmt.height)
1288 		return -EINVAL;
1289 
1290 	/*
1291 	 * Only support the framebuffer of one of the vivid instances.
1292 	 * Anything else is rejected.
1293 	 */
1294 	if (!vivid_validate_fb(a))
1295 		return -EINVAL;
1296 
1297 	dev->fb_vbase_cap = phys_to_virt((unsigned long)a->base);
1298 	dev->fb_cap = *a;
1299 	dev->overlay_cap_left = clamp_t(int, dev->overlay_cap_left,
1300 				    -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
1301 	dev->overlay_cap_top = clamp_t(int, dev->overlay_cap_top,
1302 				   -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
1303 	return 0;
1304 }
1305 
1306 static const struct v4l2_audio vivid_audio_inputs[] = {
1307 	{ 0, "TV", V4L2_AUDCAP_STEREO },
1308 	{ 1, "Line-In", V4L2_AUDCAP_STEREO },
1309 };
1310 
vidioc_enum_input(struct file * file,void * priv,struct v4l2_input * inp)1311 int vidioc_enum_input(struct file *file, void *priv,
1312 				struct v4l2_input *inp)
1313 {
1314 	struct vivid_dev *dev = video_drvdata(file);
1315 
1316 	if (inp->index >= dev->num_inputs)
1317 		return -EINVAL;
1318 
1319 	inp->type = V4L2_INPUT_TYPE_CAMERA;
1320 	switch (dev->input_type[inp->index]) {
1321 	case WEBCAM:
1322 		snprintf(inp->name, sizeof(inp->name), "Webcam %u",
1323 				dev->input_name_counter[inp->index]);
1324 		inp->capabilities = 0;
1325 		break;
1326 	case TV:
1327 		snprintf(inp->name, sizeof(inp->name), "TV %u",
1328 				dev->input_name_counter[inp->index]);
1329 		inp->type = V4L2_INPUT_TYPE_TUNER;
1330 		inp->std = V4L2_STD_ALL;
1331 		if (dev->has_audio_inputs)
1332 			inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
1333 		inp->capabilities = V4L2_IN_CAP_STD;
1334 		break;
1335 	case SVID:
1336 		snprintf(inp->name, sizeof(inp->name), "S-Video %u",
1337 				dev->input_name_counter[inp->index]);
1338 		inp->std = V4L2_STD_ALL;
1339 		if (dev->has_audio_inputs)
1340 			inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
1341 		inp->capabilities = V4L2_IN_CAP_STD;
1342 		break;
1343 	case HDMI:
1344 		snprintf(inp->name, sizeof(inp->name), "HDMI %u",
1345 				dev->input_name_counter[inp->index]);
1346 		inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
1347 		if (dev->edid_blocks == 0 ||
1348 		    dev->dv_timings_signal_mode[dev->input] == NO_SIGNAL)
1349 			inp->status |= V4L2_IN_ST_NO_SIGNAL;
1350 		else if (dev->dv_timings_signal_mode[dev->input] == NO_LOCK ||
1351 			 dev->dv_timings_signal_mode[dev->input] == OUT_OF_RANGE)
1352 			inp->status |= V4L2_IN_ST_NO_H_LOCK;
1353 		break;
1354 	}
1355 	if (dev->sensor_hflip)
1356 		inp->status |= V4L2_IN_ST_HFLIP;
1357 	if (dev->sensor_vflip)
1358 		inp->status |= V4L2_IN_ST_VFLIP;
1359 	if (dev->input == inp->index && vivid_is_sdtv_cap(dev)) {
1360 		if (dev->std_signal_mode[dev->input] == NO_SIGNAL) {
1361 			inp->status |= V4L2_IN_ST_NO_SIGNAL;
1362 		} else if (dev->std_signal_mode[dev->input] == NO_LOCK) {
1363 			inp->status |= V4L2_IN_ST_NO_H_LOCK;
1364 		} else if (vivid_is_tv_cap(dev)) {
1365 			switch (tpg_g_quality(&dev->tpg)) {
1366 			case TPG_QUAL_GRAY:
1367 				inp->status |= V4L2_IN_ST_COLOR_KILL;
1368 				break;
1369 			case TPG_QUAL_NOISE:
1370 				inp->status |= V4L2_IN_ST_NO_H_LOCK;
1371 				break;
1372 			default:
1373 				break;
1374 			}
1375 		}
1376 	}
1377 	return 0;
1378 }
1379 
vidioc_g_input(struct file * file,void * priv,unsigned * i)1380 int vidioc_g_input(struct file *file, void *priv, unsigned *i)
1381 {
1382 	struct vivid_dev *dev = video_drvdata(file);
1383 
1384 	*i = dev->input;
1385 	return 0;
1386 }
1387 
vidioc_s_input(struct file * file,void * priv,unsigned i)1388 int vidioc_s_input(struct file *file, void *priv, unsigned i)
1389 {
1390 	struct vivid_dev *dev = video_drvdata(file);
1391 	struct v4l2_bt_timings *bt = &dev->dv_timings_cap[dev->input].bt;
1392 	unsigned brightness;
1393 
1394 	if (i >= dev->num_inputs)
1395 		return -EINVAL;
1396 
1397 	if (i == dev->input)
1398 		return 0;
1399 
1400 	if (vb2_is_busy(&dev->vb_vid_cap_q) ||
1401 	    vb2_is_busy(&dev->vb_vbi_cap_q) ||
1402 	    vb2_is_busy(&dev->vb_meta_cap_q))
1403 		return -EBUSY;
1404 
1405 	dev->input = i;
1406 	dev->vid_cap_dev.tvnorms = 0;
1407 	if (dev->input_type[i] == TV || dev->input_type[i] == SVID) {
1408 		dev->tv_audio_input = (dev->input_type[i] == TV) ? 0 : 1;
1409 		dev->vid_cap_dev.tvnorms = V4L2_STD_ALL;
1410 	}
1411 	dev->vbi_cap_dev.tvnorms = dev->vid_cap_dev.tvnorms;
1412 	dev->meta_cap_dev.tvnorms = dev->vid_cap_dev.tvnorms;
1413 	vivid_update_format_cap(dev, false);
1414 
1415 	if (dev->colorspace) {
1416 		switch (dev->input_type[i]) {
1417 		case WEBCAM:
1418 			v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
1419 			break;
1420 		case TV:
1421 		case SVID:
1422 			v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
1423 			break;
1424 		case HDMI:
1425 			if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
1426 				if (dev->src_rect.width == 720 && dev->src_rect.height <= 576)
1427 					v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
1428 				else
1429 					v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
1430 			} else {
1431 				v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
1432 			}
1433 			break;
1434 		}
1435 	}
1436 
1437 	/*
1438 	 * Modify the brightness range depending on the input.
1439 	 * This makes it easy to use vivid to test if applications can
1440 	 * handle control range modifications and is also how this is
1441 	 * typically used in practice as different inputs may be hooked
1442 	 * up to different receivers with different control ranges.
1443 	 */
1444 	brightness = 128 * i + dev->input_brightness[i];
1445 	v4l2_ctrl_modify_range(dev->brightness,
1446 			128 * i, 255 + 128 * i, 1, 128 + 128 * i);
1447 	v4l2_ctrl_s_ctrl(dev->brightness, brightness);
1448 
1449 	/* Restore per-input states. */
1450 	v4l2_ctrl_activate(dev->ctrl_dv_timings_signal_mode,
1451 			   vivid_is_hdmi_cap(dev));
1452 	v4l2_ctrl_activate(dev->ctrl_dv_timings, vivid_is_hdmi_cap(dev) &&
1453 			   dev->dv_timings_signal_mode[dev->input] ==
1454 			   SELECTED_DV_TIMINGS);
1455 	v4l2_ctrl_activate(dev->ctrl_std_signal_mode, vivid_is_sdtv_cap(dev));
1456 	v4l2_ctrl_activate(dev->ctrl_standard, vivid_is_sdtv_cap(dev) &&
1457 			   dev->std_signal_mode[dev->input]);
1458 
1459 	if (vivid_is_hdmi_cap(dev)) {
1460 		v4l2_ctrl_s_ctrl(dev->ctrl_dv_timings_signal_mode,
1461 				 dev->dv_timings_signal_mode[dev->input]);
1462 		v4l2_ctrl_s_ctrl(dev->ctrl_dv_timings,
1463 				 dev->query_dv_timings[dev->input]);
1464 	} else if (vivid_is_sdtv_cap(dev)) {
1465 		v4l2_ctrl_s_ctrl(dev->ctrl_std_signal_mode,
1466 				 dev->std_signal_mode[dev->input]);
1467 		v4l2_ctrl_s_ctrl(dev->ctrl_standard,
1468 				 dev->std_signal_mode[dev->input]);
1469 	}
1470 
1471 	return 0;
1472 }
1473 
vidioc_enumaudio(struct file * file,void * fh,struct v4l2_audio * vin)1474 int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
1475 {
1476 	if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1477 		return -EINVAL;
1478 	*vin = vivid_audio_inputs[vin->index];
1479 	return 0;
1480 }
1481 
vidioc_g_audio(struct file * file,void * fh,struct v4l2_audio * vin)1482 int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
1483 {
1484 	struct vivid_dev *dev = video_drvdata(file);
1485 
1486 	if (!vivid_is_sdtv_cap(dev))
1487 		return -EINVAL;
1488 	*vin = vivid_audio_inputs[dev->tv_audio_input];
1489 	return 0;
1490 }
1491 
vidioc_s_audio(struct file * file,void * fh,const struct v4l2_audio * vin)1492 int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *vin)
1493 {
1494 	struct vivid_dev *dev = video_drvdata(file);
1495 
1496 	if (!vivid_is_sdtv_cap(dev))
1497 		return -EINVAL;
1498 	if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1499 		return -EINVAL;
1500 	dev->tv_audio_input = vin->index;
1501 	return 0;
1502 }
1503 
vivid_video_g_frequency(struct file * file,void * fh,struct v4l2_frequency * vf)1504 int vivid_video_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1505 {
1506 	struct vivid_dev *dev = video_drvdata(file);
1507 
1508 	if (vf->tuner != 0)
1509 		return -EINVAL;
1510 	vf->frequency = dev->tv_freq;
1511 	return 0;
1512 }
1513 
vivid_video_s_frequency(struct file * file,void * fh,const struct v4l2_frequency * vf)1514 int vivid_video_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
1515 {
1516 	struct vivid_dev *dev = video_drvdata(file);
1517 
1518 	if (vf->tuner != 0)
1519 		return -EINVAL;
1520 	dev->tv_freq = clamp_t(unsigned, vf->frequency, MIN_TV_FREQ, MAX_TV_FREQ);
1521 	if (vivid_is_tv_cap(dev))
1522 		vivid_update_quality(dev);
1523 	return 0;
1524 }
1525 
vivid_video_s_tuner(struct file * file,void * fh,const struct v4l2_tuner * vt)1526 int vivid_video_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
1527 {
1528 	struct vivid_dev *dev = video_drvdata(file);
1529 
1530 	if (vt->index != 0)
1531 		return -EINVAL;
1532 	if (vt->audmode > V4L2_TUNER_MODE_LANG1_LANG2)
1533 		return -EINVAL;
1534 	dev->tv_audmode = vt->audmode;
1535 	return 0;
1536 }
1537 
vivid_video_g_tuner(struct file * file,void * fh,struct v4l2_tuner * vt)1538 int vivid_video_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1539 {
1540 	struct vivid_dev *dev = video_drvdata(file);
1541 	enum tpg_quality qual;
1542 
1543 	if (vt->index != 0)
1544 		return -EINVAL;
1545 
1546 	vt->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
1547 			 V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
1548 	vt->audmode = dev->tv_audmode;
1549 	vt->rangelow = MIN_TV_FREQ;
1550 	vt->rangehigh = MAX_TV_FREQ;
1551 	qual = vivid_get_quality(dev, &vt->afc);
1552 	if (qual == TPG_QUAL_COLOR)
1553 		vt->signal = 0xffff;
1554 	else if (qual == TPG_QUAL_GRAY)
1555 		vt->signal = 0x8000;
1556 	else
1557 		vt->signal = 0;
1558 	if (qual == TPG_QUAL_NOISE) {
1559 		vt->rxsubchans = 0;
1560 	} else if (qual == TPG_QUAL_GRAY) {
1561 		vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1562 	} else {
1563 		unsigned int channel_nr = dev->tv_freq / (6 * 16);
1564 		unsigned int options =
1565 			(dev->std_cap[dev->input] & V4L2_STD_NTSC_M) ? 4 : 3;
1566 
1567 		switch (channel_nr % options) {
1568 		case 0:
1569 			vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1570 			break;
1571 		case 1:
1572 			vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
1573 			break;
1574 		case 2:
1575 			if (dev->std_cap[dev->input] & V4L2_STD_NTSC_M)
1576 				vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_SAP;
1577 			else
1578 				vt->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
1579 			break;
1580 		case 3:
1581 			vt->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_SAP;
1582 			break;
1583 		}
1584 	}
1585 	strscpy(vt->name, "TV Tuner", sizeof(vt->name));
1586 	return 0;
1587 }
1588 
1589 /* Must remain in sync with the vivid_ctrl_standard_strings array */
1590 const v4l2_std_id vivid_standard[] = {
1591 	V4L2_STD_NTSC_M,
1592 	V4L2_STD_NTSC_M_JP,
1593 	V4L2_STD_NTSC_M_KR,
1594 	V4L2_STD_NTSC_443,
1595 	V4L2_STD_PAL_BG | V4L2_STD_PAL_H,
1596 	V4L2_STD_PAL_I,
1597 	V4L2_STD_PAL_DK,
1598 	V4L2_STD_PAL_M,
1599 	V4L2_STD_PAL_N,
1600 	V4L2_STD_PAL_Nc,
1601 	V4L2_STD_PAL_60,
1602 	V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H,
1603 	V4L2_STD_SECAM_DK,
1604 	V4L2_STD_SECAM_L,
1605 	V4L2_STD_SECAM_LC,
1606 	V4L2_STD_UNKNOWN
1607 };
1608 
1609 /* Must remain in sync with the vivid_standard array */
1610 const char * const vivid_ctrl_standard_strings[] = {
1611 	"NTSC-M",
1612 	"NTSC-M-JP",
1613 	"NTSC-M-KR",
1614 	"NTSC-443",
1615 	"PAL-BGH",
1616 	"PAL-I",
1617 	"PAL-DK",
1618 	"PAL-M",
1619 	"PAL-N",
1620 	"PAL-Nc",
1621 	"PAL-60",
1622 	"SECAM-BGH",
1623 	"SECAM-DK",
1624 	"SECAM-L",
1625 	"SECAM-Lc",
1626 	NULL,
1627 };
1628 
vidioc_querystd(struct file * file,void * priv,v4l2_std_id * id)1629 int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *id)
1630 {
1631 	struct vivid_dev *dev = video_drvdata(file);
1632 	unsigned int last = dev->query_std_last[dev->input];
1633 
1634 	if (!vivid_is_sdtv_cap(dev))
1635 		return -ENODATA;
1636 	if (dev->std_signal_mode[dev->input] == NO_SIGNAL ||
1637 	    dev->std_signal_mode[dev->input] == NO_LOCK) {
1638 		*id = V4L2_STD_UNKNOWN;
1639 		return 0;
1640 	}
1641 	if (vivid_is_tv_cap(dev) && tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE) {
1642 		*id = V4L2_STD_UNKNOWN;
1643 	} else if (dev->std_signal_mode[dev->input] == CURRENT_STD) {
1644 		*id = dev->std_cap[dev->input];
1645 	} else if (dev->std_signal_mode[dev->input] == SELECTED_STD) {
1646 		*id = dev->query_std[dev->input];
1647 	} else {
1648 		*id = vivid_standard[last];
1649 		dev->query_std_last[dev->input] =
1650 			(last + 1) % ARRAY_SIZE(vivid_standard);
1651 	}
1652 
1653 	return 0;
1654 }
1655 
vivid_vid_cap_s_std(struct file * file,void * priv,v4l2_std_id id)1656 int vivid_vid_cap_s_std(struct file *file, void *priv, v4l2_std_id id)
1657 {
1658 	struct vivid_dev *dev = video_drvdata(file);
1659 
1660 	if (!vivid_is_sdtv_cap(dev))
1661 		return -ENODATA;
1662 	if (dev->std_cap[dev->input] == id)
1663 		return 0;
1664 	if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
1665 		return -EBUSY;
1666 	dev->std_cap[dev->input] = id;
1667 	vivid_update_format_cap(dev, false);
1668 	return 0;
1669 }
1670 
find_aspect_ratio(u32 width,u32 height,u32 * num,u32 * denom)1671 static void find_aspect_ratio(u32 width, u32 height,
1672 			       u32 *num, u32 *denom)
1673 {
1674 	if (!(height % 3) && ((height * 4 / 3) == width)) {
1675 		*num = 4;
1676 		*denom = 3;
1677 	} else if (!(height % 9) && ((height * 16 / 9) == width)) {
1678 		*num = 16;
1679 		*denom = 9;
1680 	} else if (!(height % 10) && ((height * 16 / 10) == width)) {
1681 		*num = 16;
1682 		*denom = 10;
1683 	} else if (!(height % 4) && ((height * 5 / 4) == width)) {
1684 		*num = 5;
1685 		*denom = 4;
1686 	} else if (!(height % 9) && ((height * 15 / 9) == width)) {
1687 		*num = 15;
1688 		*denom = 9;
1689 	} else { /* default to 16:9 */
1690 		*num = 16;
1691 		*denom = 9;
1692 	}
1693 }
1694 
valid_cvt_gtf_timings(struct v4l2_dv_timings * timings)1695 static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
1696 {
1697 	struct v4l2_bt_timings *bt = &timings->bt;
1698 	u32 total_h_pixel;
1699 	u32 total_v_lines;
1700 	u32 h_freq;
1701 
1702 	if (!v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap,
1703 				NULL, NULL))
1704 		return false;
1705 
1706 	total_h_pixel = V4L2_DV_BT_FRAME_WIDTH(bt);
1707 	total_v_lines = V4L2_DV_BT_FRAME_HEIGHT(bt);
1708 
1709 	h_freq = (u32)bt->pixelclock / total_h_pixel;
1710 
1711 	if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_CVT)) {
1712 		if (v4l2_detect_cvt(total_v_lines, h_freq, bt->vsync, bt->width,
1713 				    bt->polarities, bt->interlaced, timings))
1714 			return true;
1715 	}
1716 
1717 	if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_GTF)) {
1718 		struct v4l2_fract aspect_ratio;
1719 
1720 		find_aspect_ratio(bt->width, bt->height,
1721 				  &aspect_ratio.numerator,
1722 				  &aspect_ratio.denominator);
1723 		if (v4l2_detect_gtf(total_v_lines, h_freq, bt->vsync,
1724 				    bt->polarities, bt->interlaced,
1725 				    aspect_ratio, timings))
1726 			return true;
1727 	}
1728 	return false;
1729 }
1730 
vivid_vid_cap_s_dv_timings(struct file * file,void * _fh,struct v4l2_dv_timings * timings)1731 int vivid_vid_cap_s_dv_timings(struct file *file, void *_fh,
1732 				    struct v4l2_dv_timings *timings)
1733 {
1734 	struct vivid_dev *dev = video_drvdata(file);
1735 
1736 	if (!vivid_is_hdmi_cap(dev))
1737 		return -ENODATA;
1738 	if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
1739 				      0, NULL, NULL) &&
1740 	    !valid_cvt_gtf_timings(timings))
1741 		return -EINVAL;
1742 
1743 	if (v4l2_match_dv_timings(timings, &dev->dv_timings_cap[dev->input],
1744 				  0, false))
1745 		return 0;
1746 	if (vb2_is_busy(&dev->vb_vid_cap_q))
1747 		return -EBUSY;
1748 
1749 	dev->dv_timings_cap[dev->input] = *timings;
1750 	vivid_update_format_cap(dev, false);
1751 	return 0;
1752 }
1753 
vidioc_query_dv_timings(struct file * file,void * _fh,struct v4l2_dv_timings * timings)1754 int vidioc_query_dv_timings(struct file *file, void *_fh,
1755 				    struct v4l2_dv_timings *timings)
1756 {
1757 	struct vivid_dev *dev = video_drvdata(file);
1758 	unsigned int input = dev->input;
1759 	unsigned int last = dev->query_dv_timings_last[input];
1760 
1761 	if (!vivid_is_hdmi_cap(dev))
1762 		return -ENODATA;
1763 	if (dev->dv_timings_signal_mode[input] == NO_SIGNAL ||
1764 	    dev->edid_blocks == 0)
1765 		return -ENOLINK;
1766 	if (dev->dv_timings_signal_mode[input] == NO_LOCK)
1767 		return -ENOLCK;
1768 	if (dev->dv_timings_signal_mode[input] == OUT_OF_RANGE) {
1769 		timings->bt.pixelclock = vivid_dv_timings_cap.bt.max_pixelclock * 2;
1770 		return -ERANGE;
1771 	}
1772 	if (dev->dv_timings_signal_mode[input] == CURRENT_DV_TIMINGS) {
1773 		*timings = dev->dv_timings_cap[input];
1774 	} else if (dev->dv_timings_signal_mode[input] ==
1775 		   SELECTED_DV_TIMINGS) {
1776 		*timings =
1777 			v4l2_dv_timings_presets[dev->query_dv_timings[input]];
1778 	} else {
1779 		*timings =
1780 			v4l2_dv_timings_presets[last];
1781 		dev->query_dv_timings_last[input] =
1782 			(last + 1) % dev->query_dv_timings_size;
1783 	}
1784 	return 0;
1785 }
1786 
vidioc_s_edid(struct file * file,void * _fh,struct v4l2_edid * edid)1787 int vidioc_s_edid(struct file *file, void *_fh,
1788 			 struct v4l2_edid *edid)
1789 {
1790 	struct vivid_dev *dev = video_drvdata(file);
1791 	u16 phys_addr;
1792 	u32 display_present = 0;
1793 	unsigned int i, j;
1794 	int ret;
1795 
1796 	memset(edid->reserved, 0, sizeof(edid->reserved));
1797 	if (edid->pad >= dev->num_inputs)
1798 		return -EINVAL;
1799 	if (dev->input_type[edid->pad] != HDMI || edid->start_block)
1800 		return -EINVAL;
1801 	if (edid->blocks == 0) {
1802 		dev->edid_blocks = 0;
1803 		v4l2_ctrl_s_ctrl(dev->ctrl_tx_edid_present, 0);
1804 		v4l2_ctrl_s_ctrl(dev->ctrl_tx_hotplug, 0);
1805 		phys_addr = CEC_PHYS_ADDR_INVALID;
1806 		goto set_phys_addr;
1807 	}
1808 	if (edid->blocks > dev->edid_max_blocks) {
1809 		edid->blocks = dev->edid_max_blocks;
1810 		return -E2BIG;
1811 	}
1812 	phys_addr = cec_get_edid_phys_addr(edid->edid, edid->blocks * 128, NULL);
1813 	ret = v4l2_phys_addr_validate(phys_addr, &phys_addr, NULL);
1814 	if (ret)
1815 		return ret;
1816 
1817 	if (vb2_is_busy(&dev->vb_vid_cap_q))
1818 		return -EBUSY;
1819 
1820 	dev->edid_blocks = edid->blocks;
1821 	memcpy(dev->edid, edid->edid, edid->blocks * 128);
1822 
1823 	for (i = 0, j = 0; i < dev->num_outputs; i++)
1824 		if (dev->output_type[i] == HDMI)
1825 			display_present |=
1826 				dev->display_present[i] << j++;
1827 
1828 	v4l2_ctrl_s_ctrl(dev->ctrl_tx_edid_present, display_present);
1829 	v4l2_ctrl_s_ctrl(dev->ctrl_tx_hotplug, display_present);
1830 
1831 set_phys_addr:
1832 	/* TODO: a proper hotplug detect cycle should be emulated here */
1833 	cec_s_phys_addr(dev->cec_rx_adap, phys_addr, false);
1834 
1835 	for (i = 0; i < MAX_OUTPUTS && dev->cec_tx_adap[i]; i++)
1836 		cec_s_phys_addr(dev->cec_tx_adap[i],
1837 				dev->display_present[i] ?
1838 				v4l2_phys_addr_for_input(phys_addr, i + 1) :
1839 				CEC_PHYS_ADDR_INVALID,
1840 				false);
1841 	return 0;
1842 }
1843 
vidioc_enum_framesizes(struct file * file,void * fh,struct v4l2_frmsizeenum * fsize)1844 int vidioc_enum_framesizes(struct file *file, void *fh,
1845 					 struct v4l2_frmsizeenum *fsize)
1846 {
1847 	struct vivid_dev *dev = video_drvdata(file);
1848 
1849 	if (!vivid_is_webcam(dev) && !dev->has_scaler_cap)
1850 		return -EINVAL;
1851 	if (vivid_get_format(dev, fsize->pixel_format) == NULL)
1852 		return -EINVAL;
1853 	if (vivid_is_webcam(dev)) {
1854 		if (fsize->index >= ARRAY_SIZE(webcam_sizes))
1855 			return -EINVAL;
1856 		fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1857 		fsize->discrete = webcam_sizes[fsize->index];
1858 		return 0;
1859 	}
1860 	if (fsize->index)
1861 		return -EINVAL;
1862 	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1863 	fsize->stepwise.min_width = MIN_WIDTH;
1864 	fsize->stepwise.max_width = MAX_WIDTH * MAX_ZOOM;
1865 	fsize->stepwise.step_width = 2;
1866 	fsize->stepwise.min_height = MIN_HEIGHT;
1867 	fsize->stepwise.max_height = MAX_HEIGHT * MAX_ZOOM;
1868 	fsize->stepwise.step_height = 2;
1869 	return 0;
1870 }
1871 
1872 /* timeperframe is arbitrary and continuous */
vidioc_enum_frameintervals(struct file * file,void * priv,struct v4l2_frmivalenum * fival)1873 int vidioc_enum_frameintervals(struct file *file, void *priv,
1874 					     struct v4l2_frmivalenum *fival)
1875 {
1876 	struct vivid_dev *dev = video_drvdata(file);
1877 	const struct vivid_fmt *fmt;
1878 	int i;
1879 
1880 	fmt = vivid_get_format(dev, fival->pixel_format);
1881 	if (!fmt)
1882 		return -EINVAL;
1883 
1884 	if (!vivid_is_webcam(dev)) {
1885 		if (fival->index)
1886 			return -EINVAL;
1887 		if (fival->width < MIN_WIDTH || fival->width > MAX_WIDTH * MAX_ZOOM)
1888 			return -EINVAL;
1889 		if (fival->height < MIN_HEIGHT || fival->height > MAX_HEIGHT * MAX_ZOOM)
1890 			return -EINVAL;
1891 		fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1892 		fival->discrete = dev->timeperframe_vid_cap;
1893 		return 0;
1894 	}
1895 
1896 	for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
1897 		if (fival->width == webcam_sizes[i].width &&
1898 		    fival->height == webcam_sizes[i].height)
1899 			break;
1900 	if (i == ARRAY_SIZE(webcam_sizes))
1901 		return -EINVAL;
1902 	if (fival->index >= 2 * (VIVID_WEBCAM_SIZES - i))
1903 		return -EINVAL;
1904 	fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1905 	fival->discrete = webcam_intervals[fival->index];
1906 	return 0;
1907 }
1908 
vivid_vid_cap_g_parm(struct file * file,void * priv,struct v4l2_streamparm * parm)1909 int vivid_vid_cap_g_parm(struct file *file, void *priv,
1910 			  struct v4l2_streamparm *parm)
1911 {
1912 	struct vivid_dev *dev = video_drvdata(file);
1913 
1914 	if (parm->type != (dev->multiplanar ?
1915 			   V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1916 			   V4L2_BUF_TYPE_VIDEO_CAPTURE))
1917 		return -EINVAL;
1918 
1919 	parm->parm.capture.capability   = V4L2_CAP_TIMEPERFRAME;
1920 	parm->parm.capture.timeperframe = dev->timeperframe_vid_cap;
1921 	parm->parm.capture.readbuffers  = 1;
1922 	return 0;
1923 }
1924 
vivid_vid_cap_s_parm(struct file * file,void * priv,struct v4l2_streamparm * parm)1925 int vivid_vid_cap_s_parm(struct file *file, void *priv,
1926 			  struct v4l2_streamparm *parm)
1927 {
1928 	struct vivid_dev *dev = video_drvdata(file);
1929 	unsigned ival_sz = 2 * (VIVID_WEBCAM_SIZES - dev->webcam_size_idx);
1930 	struct v4l2_fract tpf;
1931 	unsigned i;
1932 
1933 	if (parm->type != (dev->multiplanar ?
1934 			   V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1935 			   V4L2_BUF_TYPE_VIDEO_CAPTURE))
1936 		return -EINVAL;
1937 	if (!vivid_is_webcam(dev))
1938 		return vivid_vid_cap_g_parm(file, priv, parm);
1939 
1940 	tpf = parm->parm.capture.timeperframe;
1941 
1942 	if (tpf.denominator == 0)
1943 		tpf = webcam_intervals[ival_sz - 1];
1944 	for (i = 0; i < ival_sz; i++)
1945 		if (V4L2_FRACT_COMPARE(tpf, >=, webcam_intervals[i]))
1946 			break;
1947 	if (i == ival_sz)
1948 		i = ival_sz - 1;
1949 	dev->webcam_ival_idx = i;
1950 	tpf = webcam_intervals[dev->webcam_ival_idx];
1951 
1952 	/* resync the thread's timings */
1953 	dev->cap_seq_resync = true;
1954 	dev->timeperframe_vid_cap = tpf;
1955 	parm->parm.capture.capability   = V4L2_CAP_TIMEPERFRAME;
1956 	parm->parm.capture.timeperframe = tpf;
1957 	parm->parm.capture.readbuffers  = 1;
1958 	return 0;
1959 }
1960