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