1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vivid-kthread-cap.h - video/vbi capture thread support functions.
4  *
5  * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6  */
7 
8 #include <linux/module.h>
9 #include <linux/errno.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/font.h>
15 #include <linux/mutex.h>
16 #include <linux/videodev2.h>
17 #include <linux/kthread.h>
18 #include <linux/freezer.h>
19 #include <linux/random.h>
20 #include <linux/v4l2-dv-timings.h>
21 #include <asm/div64.h>
22 #include <media/videobuf2-vmalloc.h>
23 #include <media/v4l2-dv-timings.h>
24 #include <media/v4l2-ioctl.h>
25 #include <media/v4l2-fh.h>
26 #include <media/v4l2-event.h>
27 #include <media/v4l2-rect.h>
28 
29 #include "vivid-core.h"
30 #include "vivid-vid-common.h"
31 #include "vivid-vid-cap.h"
32 #include "vivid-vid-out.h"
33 #include "vivid-radio-common.h"
34 #include "vivid-radio-rx.h"
35 #include "vivid-radio-tx.h"
36 #include "vivid-sdr-cap.h"
37 #include "vivid-vbi-cap.h"
38 #include "vivid-vbi-out.h"
39 #include "vivid-osd.h"
40 #include "vivid-ctrls.h"
41 #include "vivid-kthread-cap.h"
42 #include "vivid-meta-cap.h"
43 
44 static inline v4l2_std_id vivid_get_std_cap(const struct vivid_dev *dev)
45 {
46 	if (vivid_is_sdtv_cap(dev))
47 		return dev->std_cap[dev->input];
48 	return 0;
49 }
50 
51 static void copy_pix(struct vivid_dev *dev, int win_y, int win_x,
52 			u16 *cap, const u16 *osd)
53 {
54 	u16 out;
55 	int left = dev->overlay_out_left;
56 	int top = dev->overlay_out_top;
57 	int fb_x = win_x + left;
58 	int fb_y = win_y + top;
59 	int i;
60 
61 	out = *cap;
62 	*cap = *osd;
63 	if (dev->bitmap_out) {
64 		const u8 *p = dev->bitmap_out;
65 		unsigned stride = (dev->compose_out.width + 7) / 8;
66 
67 		win_x -= dev->compose_out.left;
68 		win_y -= dev->compose_out.top;
69 		if (!(p[stride * win_y + win_x / 8] & (1 << (win_x & 7))))
70 			return;
71 	}
72 
73 	for (i = 0; i < dev->clipcount_out; i++) {
74 		struct v4l2_rect *r = &dev->clips_out[i].c;
75 
76 		if (fb_y >= r->top && fb_y < r->top + r->height &&
77 		    fb_x >= r->left && fb_x < r->left + r->width)
78 			return;
79 	}
80 	if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
81 	    *osd != dev->chromakey_out)
82 		return;
83 	if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
84 	    out == dev->chromakey_out)
85 		return;
86 	if (dev->fmt_cap->alpha_mask) {
87 		if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) &&
88 		    dev->global_alpha_out)
89 			return;
90 		if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) &&
91 		    *cap & dev->fmt_cap->alpha_mask)
92 			return;
93 		if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_LOCAL_INV_ALPHA) &&
94 		    !(*cap & dev->fmt_cap->alpha_mask))
95 			return;
96 	}
97 	*cap = out;
98 }
99 
100 static void blend_line(struct vivid_dev *dev, unsigned y_offset, unsigned x_offset,
101 		u8 *vcapbuf, const u8 *vosdbuf,
102 		unsigned width, unsigned pixsize)
103 {
104 	unsigned x;
105 
106 	for (x = 0; x < width; x++, vcapbuf += pixsize, vosdbuf += pixsize) {
107 		copy_pix(dev, y_offset, x_offset + x,
108 			 (u16 *)vcapbuf, (const u16 *)vosdbuf);
109 	}
110 }
111 
112 static void scale_line(const u8 *src, u8 *dst, unsigned srcw, unsigned dstw, unsigned twopixsize)
113 {
114 	/* Coarse scaling with Bresenham */
115 	unsigned int_part;
116 	unsigned fract_part;
117 	unsigned src_x = 0;
118 	unsigned error = 0;
119 	unsigned x;
120 
121 	/*
122 	 * We always combine two pixels to prevent color bleed in the packed
123 	 * yuv case.
124 	 */
125 	srcw /= 2;
126 	dstw /= 2;
127 	int_part = srcw / dstw;
128 	fract_part = srcw % dstw;
129 	for (x = 0; x < dstw; x++, dst += twopixsize) {
130 		memcpy(dst, src + src_x * twopixsize, twopixsize);
131 		src_x += int_part;
132 		error += fract_part;
133 		if (error >= dstw) {
134 			error -= dstw;
135 			src_x++;
136 		}
137 	}
138 }
139 
140 /*
141  * Precalculate the rectangles needed to perform video looping:
142  *
143  * The nominal pipeline is that the video output buffer is cropped by
144  * crop_out, scaled to compose_out, overlaid with the output overlay,
145  * cropped on the capture side by crop_cap and scaled again to the video
146  * capture buffer using compose_cap.
147  *
148  * To keep things efficient we calculate the intersection of compose_out
149  * and crop_cap (since that's the only part of the video that will
150  * actually end up in the capture buffer), determine which part of the
151  * video output buffer that is and which part of the video capture buffer
152  * so we can scale the video straight from the output buffer to the capture
153  * buffer without any intermediate steps.
154  *
155  * If we need to deal with an output overlay, then there is no choice and
156  * that intermediate step still has to be taken. For the output overlay
157  * support we calculate the intersection of the framebuffer and the overlay
158  * window (which may be partially or wholly outside of the framebuffer
159  * itself) and the intersection of that with loop_vid_copy (i.e. the part of
160  * the actual looped video that will be overlaid). The result is calculated
161  * both in framebuffer coordinates (loop_fb_copy) and compose_out coordinates
162  * (loop_vid_overlay). Finally calculate the part of the capture buffer that
163  * will receive that overlaid video.
164  */
165 static void vivid_precalc_copy_rects(struct vivid_dev *dev)
166 {
167 	/* Framebuffer rectangle */
168 	struct v4l2_rect r_fb = {
169 		0, 0, dev->display_width, dev->display_height
170 	};
171 	/* Overlay window rectangle in framebuffer coordinates */
172 	struct v4l2_rect r_overlay = {
173 		dev->overlay_out_left, dev->overlay_out_top,
174 		dev->compose_out.width, dev->compose_out.height
175 	};
176 
177 	v4l2_rect_intersect(&dev->loop_vid_copy, &dev->crop_cap, &dev->compose_out);
178 
179 	dev->loop_vid_out = dev->loop_vid_copy;
180 	v4l2_rect_scale(&dev->loop_vid_out, &dev->compose_out, &dev->crop_out);
181 	dev->loop_vid_out.left += dev->crop_out.left;
182 	dev->loop_vid_out.top += dev->crop_out.top;
183 
184 	dev->loop_vid_cap = dev->loop_vid_copy;
185 	v4l2_rect_scale(&dev->loop_vid_cap, &dev->crop_cap, &dev->compose_cap);
186 
187 	dprintk(dev, 1,
188 		"loop_vid_copy: %dx%d@%dx%d loop_vid_out: %dx%d@%dx%d loop_vid_cap: %dx%d@%dx%d\n",
189 		dev->loop_vid_copy.width, dev->loop_vid_copy.height,
190 		dev->loop_vid_copy.left, dev->loop_vid_copy.top,
191 		dev->loop_vid_out.width, dev->loop_vid_out.height,
192 		dev->loop_vid_out.left, dev->loop_vid_out.top,
193 		dev->loop_vid_cap.width, dev->loop_vid_cap.height,
194 		dev->loop_vid_cap.left, dev->loop_vid_cap.top);
195 
196 	v4l2_rect_intersect(&r_overlay, &r_fb, &r_overlay);
197 
198 	/* shift r_overlay to the same origin as compose_out */
199 	r_overlay.left += dev->compose_out.left - dev->overlay_out_left;
200 	r_overlay.top += dev->compose_out.top - dev->overlay_out_top;
201 
202 	v4l2_rect_intersect(&dev->loop_vid_overlay, &r_overlay, &dev->loop_vid_copy);
203 	dev->loop_fb_copy = dev->loop_vid_overlay;
204 
205 	/* shift dev->loop_fb_copy back again to the fb origin */
206 	dev->loop_fb_copy.left -= dev->compose_out.left - dev->overlay_out_left;
207 	dev->loop_fb_copy.top -= dev->compose_out.top - dev->overlay_out_top;
208 
209 	dev->loop_vid_overlay_cap = dev->loop_vid_overlay;
210 	v4l2_rect_scale(&dev->loop_vid_overlay_cap, &dev->crop_cap, &dev->compose_cap);
211 
212 	dprintk(dev, 1,
213 		"loop_fb_copy: %dx%d@%dx%d loop_vid_overlay: %dx%d@%dx%d loop_vid_overlay_cap: %dx%d@%dx%d\n",
214 		dev->loop_fb_copy.width, dev->loop_fb_copy.height,
215 		dev->loop_fb_copy.left, dev->loop_fb_copy.top,
216 		dev->loop_vid_overlay.width, dev->loop_vid_overlay.height,
217 		dev->loop_vid_overlay.left, dev->loop_vid_overlay.top,
218 		dev->loop_vid_overlay_cap.width, dev->loop_vid_overlay_cap.height,
219 		dev->loop_vid_overlay_cap.left, dev->loop_vid_overlay_cap.top);
220 }
221 
222 static void *plane_vaddr(struct tpg_data *tpg, struct vivid_buffer *buf,
223 			 unsigned p, unsigned bpl[TPG_MAX_PLANES], unsigned h)
224 {
225 	unsigned i;
226 	void *vbuf;
227 
228 	if (p == 0 || tpg_g_buffers(tpg) > 1)
229 		return vb2_plane_vaddr(&buf->vb.vb2_buf, p);
230 	vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
231 	for (i = 0; i < p; i++)
232 		vbuf += bpl[i] * h / tpg->vdownsampling[i];
233 	return vbuf;
234 }
235 
236 static noinline_for_stack int vivid_copy_buffer(struct vivid_dev *dev, unsigned p,
237 		u8 *vcapbuf, struct vivid_buffer *vid_cap_buf)
238 {
239 	bool blank = dev->must_blank[vid_cap_buf->vb.vb2_buf.index];
240 	struct tpg_data *tpg = &dev->tpg;
241 	struct vivid_buffer *vid_out_buf = NULL;
242 	unsigned vdiv = dev->fmt_out->vdownsampling[p];
243 	unsigned twopixsize = tpg_g_twopixelsize(tpg, p);
244 	unsigned img_width = tpg_hdiv(tpg, p, dev->compose_cap.width);
245 	unsigned img_height = dev->compose_cap.height;
246 	unsigned stride_cap = tpg->bytesperline[p];
247 	unsigned stride_out = dev->bytesperline_out[p];
248 	unsigned stride_osd = dev->display_byte_stride;
249 	unsigned hmax = (img_height * tpg->perc_fill) / 100;
250 	u8 *voutbuf;
251 	u8 *vosdbuf = NULL;
252 	unsigned y;
253 	bool blend = dev->bitmap_out || dev->clipcount_out || dev->fbuf_out_flags;
254 	/* Coarse scaling with Bresenham */
255 	unsigned vid_out_int_part;
256 	unsigned vid_out_fract_part;
257 	unsigned vid_out_y = 0;
258 	unsigned vid_out_error = 0;
259 	unsigned vid_overlay_int_part = 0;
260 	unsigned vid_overlay_fract_part = 0;
261 	unsigned vid_overlay_y = 0;
262 	unsigned vid_overlay_error = 0;
263 	unsigned vid_cap_left = tpg_hdiv(tpg, p, dev->loop_vid_cap.left);
264 	unsigned vid_cap_right;
265 	bool quick;
266 
267 	vid_out_int_part = dev->loop_vid_out.height / dev->loop_vid_cap.height;
268 	vid_out_fract_part = dev->loop_vid_out.height % dev->loop_vid_cap.height;
269 
270 	if (!list_empty(&dev->vid_out_active))
271 		vid_out_buf = list_entry(dev->vid_out_active.next,
272 					 struct vivid_buffer, list);
273 	if (vid_out_buf == NULL)
274 		return -ENODATA;
275 
276 	vid_cap_buf->vb.field = vid_out_buf->vb.field;
277 
278 	voutbuf = plane_vaddr(tpg, vid_out_buf, p,
279 			      dev->bytesperline_out, dev->fmt_out_rect.height);
280 	if (p < dev->fmt_out->buffers)
281 		voutbuf += vid_out_buf->vb.vb2_buf.planes[p].data_offset;
282 	voutbuf += tpg_hdiv(tpg, p, dev->loop_vid_out.left) +
283 		(dev->loop_vid_out.top / vdiv) * stride_out;
284 	vcapbuf += tpg_hdiv(tpg, p, dev->compose_cap.left) +
285 		(dev->compose_cap.top / vdiv) * stride_cap;
286 
287 	if (dev->loop_vid_copy.width == 0 || dev->loop_vid_copy.height == 0) {
288 		/*
289 		 * If there is nothing to copy, then just fill the capture window
290 		 * with black.
291 		 */
292 		for (y = 0; y < hmax / vdiv; y++, vcapbuf += stride_cap)
293 			memcpy(vcapbuf, tpg->black_line[p], img_width);
294 		return 0;
295 	}
296 
297 	if (dev->overlay_out_enabled &&
298 	    dev->loop_vid_overlay.width && dev->loop_vid_overlay.height) {
299 		vosdbuf = dev->video_vbase;
300 		vosdbuf += (dev->loop_fb_copy.left * twopixsize) / 2 +
301 			   dev->loop_fb_copy.top * stride_osd;
302 		vid_overlay_int_part = dev->loop_vid_overlay.height /
303 				       dev->loop_vid_overlay_cap.height;
304 		vid_overlay_fract_part = dev->loop_vid_overlay.height %
305 					 dev->loop_vid_overlay_cap.height;
306 	}
307 
308 	vid_cap_right = tpg_hdiv(tpg, p, dev->loop_vid_cap.left + dev->loop_vid_cap.width);
309 	/* quick is true if no video scaling is needed */
310 	quick = dev->loop_vid_out.width == dev->loop_vid_cap.width;
311 
312 	dev->cur_scaled_line = dev->loop_vid_out.height;
313 	for (y = 0; y < hmax; y += vdiv, vcapbuf += stride_cap) {
314 		/* osdline is true if this line requires overlay blending */
315 		bool osdline = vosdbuf && y >= dev->loop_vid_overlay_cap.top &&
316 			  y < dev->loop_vid_overlay_cap.top + dev->loop_vid_overlay_cap.height;
317 
318 		/*
319 		 * If this line of the capture buffer doesn't get any video, then
320 		 * just fill with black.
321 		 */
322 		if (y < dev->loop_vid_cap.top ||
323 		    y >= dev->loop_vid_cap.top + dev->loop_vid_cap.height) {
324 			memcpy(vcapbuf, tpg->black_line[p], img_width);
325 			continue;
326 		}
327 
328 		/* fill the left border with black */
329 		if (dev->loop_vid_cap.left)
330 			memcpy(vcapbuf, tpg->black_line[p], vid_cap_left);
331 
332 		/* fill the right border with black */
333 		if (vid_cap_right < img_width)
334 			memcpy(vcapbuf + vid_cap_right, tpg->black_line[p],
335 				img_width - vid_cap_right);
336 
337 		if (quick && !osdline) {
338 			memcpy(vcapbuf + vid_cap_left,
339 			       voutbuf + vid_out_y * stride_out,
340 			       tpg_hdiv(tpg, p, dev->loop_vid_cap.width));
341 			goto update_vid_out_y;
342 		}
343 		if (dev->cur_scaled_line == vid_out_y) {
344 			memcpy(vcapbuf + vid_cap_left, dev->scaled_line,
345 			       tpg_hdiv(tpg, p, dev->loop_vid_cap.width));
346 			goto update_vid_out_y;
347 		}
348 		if (!osdline) {
349 			scale_line(voutbuf + vid_out_y * stride_out, dev->scaled_line,
350 				tpg_hdiv(tpg, p, dev->loop_vid_out.width),
351 				tpg_hdiv(tpg, p, dev->loop_vid_cap.width),
352 				tpg_g_twopixelsize(tpg, p));
353 		} else {
354 			/*
355 			 * Offset in bytes within loop_vid_copy to the start of the
356 			 * loop_vid_overlay rectangle.
357 			 */
358 			unsigned offset =
359 				((dev->loop_vid_overlay.left - dev->loop_vid_copy.left) *
360 				 twopixsize) / 2;
361 			u8 *osd = vosdbuf + vid_overlay_y * stride_osd;
362 
363 			scale_line(voutbuf + vid_out_y * stride_out, dev->blended_line,
364 				dev->loop_vid_out.width, dev->loop_vid_copy.width,
365 				tpg_g_twopixelsize(tpg, p));
366 			if (blend)
367 				blend_line(dev, vid_overlay_y + dev->loop_vid_overlay.top,
368 					   dev->loop_vid_overlay.left,
369 					   dev->blended_line + offset, osd,
370 					   dev->loop_vid_overlay.width, twopixsize / 2);
371 			else
372 				memcpy(dev->blended_line + offset,
373 				       osd, (dev->loop_vid_overlay.width * twopixsize) / 2);
374 			scale_line(dev->blended_line, dev->scaled_line,
375 					dev->loop_vid_copy.width, dev->loop_vid_cap.width,
376 					tpg_g_twopixelsize(tpg, p));
377 		}
378 		dev->cur_scaled_line = vid_out_y;
379 		memcpy(vcapbuf + vid_cap_left, dev->scaled_line,
380 		       tpg_hdiv(tpg, p, dev->loop_vid_cap.width));
381 
382 update_vid_out_y:
383 		if (osdline) {
384 			vid_overlay_y += vid_overlay_int_part;
385 			vid_overlay_error += vid_overlay_fract_part;
386 			if (vid_overlay_error >= dev->loop_vid_overlay_cap.height) {
387 				vid_overlay_error -= dev->loop_vid_overlay_cap.height;
388 				vid_overlay_y++;
389 			}
390 		}
391 		vid_out_y += vid_out_int_part;
392 		vid_out_error += vid_out_fract_part;
393 		if (vid_out_error >= dev->loop_vid_cap.height / vdiv) {
394 			vid_out_error -= dev->loop_vid_cap.height / vdiv;
395 			vid_out_y++;
396 		}
397 	}
398 
399 	if (!blank)
400 		return 0;
401 	for (; y < img_height; y += vdiv, vcapbuf += stride_cap)
402 		memcpy(vcapbuf, tpg->contrast_line[p], img_width);
403 	return 0;
404 }
405 
406 static void vivid_fillbuff(struct vivid_dev *dev, struct vivid_buffer *buf)
407 {
408 	struct tpg_data *tpg = &dev->tpg;
409 	unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1;
410 	unsigned line_height = 16 / factor;
411 	bool is_tv = vivid_is_sdtv_cap(dev);
412 	bool is_60hz = is_tv && (dev->std_cap[dev->input] & V4L2_STD_525_60);
413 	unsigned p;
414 	int line = 1;
415 	u8 *basep[TPG_MAX_PLANES][2];
416 	unsigned ms;
417 	char str[100];
418 	s32 gain;
419 	bool is_loop = false;
420 
421 	if (dev->loop_video && dev->can_loop_video &&
422 		((vivid_is_svid_cap(dev) &&
423 		!VIVID_INVALID_SIGNAL(dev->std_signal_mode[dev->input])) ||
424 		(vivid_is_hdmi_cap(dev) &&
425 		!VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode[dev->input]))))
426 		is_loop = true;
427 
428 	buf->vb.sequence = dev->vid_cap_seq_count;
429 	if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
430 		/*
431 		 * 60 Hz standards start with the bottom field, 50 Hz standards
432 		 * with the top field. So if the 0-based seq_count is even,
433 		 * then the field is TOP for 50 Hz and BOTTOM for 60 Hz
434 		 * standards.
435 		 */
436 		buf->vb.field = ((dev->vid_cap_seq_count & 1) ^ is_60hz) ?
437 			V4L2_FIELD_BOTTOM : V4L2_FIELD_TOP;
438 		/*
439 		 * The sequence counter counts frames, not fields. So divide
440 		 * by two.
441 		 */
442 		buf->vb.sequence /= 2;
443 	} else {
444 		buf->vb.field = dev->field_cap;
445 	}
446 	tpg_s_field(tpg, buf->vb.field,
447 		    dev->field_cap == V4L2_FIELD_ALTERNATE);
448 	tpg_s_perc_fill_blank(tpg, dev->must_blank[buf->vb.vb2_buf.index]);
449 
450 	vivid_precalc_copy_rects(dev);
451 
452 	for (p = 0; p < tpg_g_planes(tpg); p++) {
453 		void *vbuf = plane_vaddr(tpg, buf, p,
454 					 tpg->bytesperline, tpg->buf_height);
455 
456 		/*
457 		 * The first plane of a multiplanar format has a non-zero
458 		 * data_offset. This helps testing whether the application
459 		 * correctly supports non-zero data offsets.
460 		 */
461 		if (p < tpg_g_buffers(tpg) && dev->fmt_cap->data_offset[p]) {
462 			memset(vbuf, dev->fmt_cap->data_offset[p] & 0xff,
463 			       dev->fmt_cap->data_offset[p]);
464 			vbuf += dev->fmt_cap->data_offset[p];
465 		}
466 		tpg_calc_text_basep(tpg, basep, p, vbuf);
467 		if (!is_loop || vivid_copy_buffer(dev, p, vbuf, buf))
468 			tpg_fill_plane_buffer(tpg, vivid_get_std_cap(dev),
469 					p, vbuf);
470 	}
471 	dev->must_blank[buf->vb.vb2_buf.index] = false;
472 
473 	/* Updates stream time, only update at the start of a new frame. */
474 	if (dev->field_cap != V4L2_FIELD_ALTERNATE ||
475 			(dev->vid_cap_seq_count & 1) == 0)
476 		dev->ms_vid_cap =
477 			jiffies_to_msecs(jiffies - dev->jiffies_vid_cap);
478 
479 	ms = dev->ms_vid_cap;
480 	if (dev->osd_mode <= 1) {
481 		snprintf(str, sizeof(str), " %02d:%02d:%02d:%03d %u%s",
482 				(ms / (60 * 60 * 1000)) % 24,
483 				(ms / (60 * 1000)) % 60,
484 				(ms / 1000) % 60,
485 				ms % 1000,
486 				buf->vb.sequence,
487 				(dev->field_cap == V4L2_FIELD_ALTERNATE) ?
488 					(buf->vb.field == V4L2_FIELD_TOP ?
489 					 " top" : " bottom") : "");
490 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
491 	}
492 	if (dev->osd_mode == 0) {
493 		snprintf(str, sizeof(str), " %dx%d, input %d ",
494 				dev->src_rect.width, dev->src_rect.height, dev->input);
495 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
496 
497 		gain = v4l2_ctrl_g_ctrl(dev->gain);
498 		mutex_lock(dev->ctrl_hdl_user_vid.lock);
499 		snprintf(str, sizeof(str),
500 			" brightness %3d, contrast %3d, saturation %3d, hue %d ",
501 			dev->brightness->cur.val,
502 			dev->contrast->cur.val,
503 			dev->saturation->cur.val,
504 			dev->hue->cur.val);
505 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
506 		snprintf(str, sizeof(str),
507 			" autogain %d, gain %3d, alpha 0x%02x ",
508 			dev->autogain->cur.val, gain, dev->alpha->cur.val);
509 		mutex_unlock(dev->ctrl_hdl_user_vid.lock);
510 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
511 		mutex_lock(dev->ctrl_hdl_user_aud.lock);
512 		snprintf(str, sizeof(str),
513 			" volume %3d, mute %d ",
514 			dev->volume->cur.val, dev->mute->cur.val);
515 		mutex_unlock(dev->ctrl_hdl_user_aud.lock);
516 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
517 		mutex_lock(dev->ctrl_hdl_user_gen.lock);
518 		snprintf(str, sizeof(str), " int32 %d, int64 %lld, bitmask %08x ",
519 			dev->int32->cur.val,
520 			*dev->int64->p_cur.p_s64,
521 			dev->bitmask->cur.val);
522 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
523 		snprintf(str, sizeof(str), " boolean %d, menu %s, string \"%s\" ",
524 			dev->boolean->cur.val,
525 			dev->menu->qmenu[dev->menu->cur.val],
526 			dev->string->p_cur.p_char);
527 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
528 		snprintf(str, sizeof(str), " integer_menu %lld, value %d ",
529 			dev->int_menu->qmenu_int[dev->int_menu->cur.val],
530 			dev->int_menu->cur.val);
531 		mutex_unlock(dev->ctrl_hdl_user_gen.lock);
532 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
533 		if (dev->button_pressed) {
534 			dev->button_pressed--;
535 			snprintf(str, sizeof(str), " button pressed!");
536 			tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
537 		}
538 		if (dev->osd[0]) {
539 			if (vivid_is_hdmi_cap(dev)) {
540 				snprintf(str, sizeof(str),
541 					 " OSD \"%s\"", dev->osd);
542 				tpg_gen_text(tpg, basep, line++ * line_height,
543 					     16, str);
544 			}
545 			if (dev->osd_jiffies &&
546 			    time_is_before_jiffies(dev->osd_jiffies + 5 * HZ)) {
547 				dev->osd[0] = 0;
548 				dev->osd_jiffies = 0;
549 			}
550 		}
551 	}
552 }
553 
554 /*
555  * Return true if this pixel coordinate is a valid video pixel.
556  */
557 static bool valid_pix(struct vivid_dev *dev, int win_y, int win_x, int fb_y, int fb_x)
558 {
559 	int i;
560 
561 	if (dev->bitmap_cap) {
562 		/*
563 		 * Only if the corresponding bit in the bitmap is set can
564 		 * the video pixel be shown. Coordinates are relative to
565 		 * the overlay window set by VIDIOC_S_FMT.
566 		 */
567 		const u8 *p = dev->bitmap_cap;
568 		unsigned stride = (dev->compose_cap.width + 7) / 8;
569 
570 		if (!(p[stride * win_y + win_x / 8] & (1 << (win_x & 7))))
571 			return false;
572 	}
573 
574 	for (i = 0; i < dev->clipcount_cap; i++) {
575 		/*
576 		 * Only if the framebuffer coordinate is not in any of the
577 		 * clip rectangles will be video pixel be shown.
578 		 */
579 		struct v4l2_rect *r = &dev->clips_cap[i].c;
580 
581 		if (fb_y >= r->top && fb_y < r->top + r->height &&
582 		    fb_x >= r->left && fb_x < r->left + r->width)
583 			return false;
584 	}
585 	return true;
586 }
587 
588 /*
589  * Draw the image into the overlay buffer.
590  * Note that the combination of overlay and multiplanar is not supported.
591  */
592 static void vivid_overlay(struct vivid_dev *dev, struct vivid_buffer *buf)
593 {
594 	struct tpg_data *tpg = &dev->tpg;
595 	unsigned pixsize = tpg_g_twopixelsize(tpg, 0) / 2;
596 	void *vbase = dev->fb_vbase_cap;
597 	void *vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
598 	unsigned img_width = dev->compose_cap.width;
599 	unsigned img_height = dev->compose_cap.height;
600 	unsigned stride = tpg->bytesperline[0];
601 	/* if quick is true, then valid_pix() doesn't have to be called */
602 	bool quick = dev->bitmap_cap == NULL && dev->clipcount_cap == 0;
603 	int x, y, w, out_x = 0;
604 
605 	/*
606 	 * Overlay support is only supported for formats that have a twopixelsize
607 	 * that's >= 2. Warn and bail out if that's not the case.
608 	 */
609 	if (WARN_ON(pixsize == 0))
610 		return;
611 	if ((dev->overlay_cap_field == V4L2_FIELD_TOP ||
612 	     dev->overlay_cap_field == V4L2_FIELD_BOTTOM) &&
613 	    dev->overlay_cap_field != buf->vb.field)
614 		return;
615 
616 	vbuf += dev->compose_cap.left * pixsize + dev->compose_cap.top * stride;
617 	x = dev->overlay_cap_left;
618 	w = img_width;
619 	if (x < 0) {
620 		out_x = -x;
621 		w = w - out_x;
622 		x = 0;
623 	} else {
624 		w = dev->fb_cap.fmt.width - x;
625 		if (w > img_width)
626 			w = img_width;
627 	}
628 	if (w <= 0)
629 		return;
630 	if (dev->overlay_cap_top >= 0)
631 		vbase += dev->overlay_cap_top * dev->fb_cap.fmt.bytesperline;
632 	for (y = dev->overlay_cap_top;
633 	     y < dev->overlay_cap_top + (int)img_height;
634 	     y++, vbuf += stride) {
635 		int px;
636 
637 		if (y < 0 || y > dev->fb_cap.fmt.height)
638 			continue;
639 		if (quick) {
640 			memcpy(vbase + x * pixsize,
641 			       vbuf + out_x * pixsize, w * pixsize);
642 			vbase += dev->fb_cap.fmt.bytesperline;
643 			continue;
644 		}
645 		for (px = 0; px < w; px++) {
646 			if (!valid_pix(dev, y - dev->overlay_cap_top,
647 				       px + out_x, y, px + x))
648 				continue;
649 			memcpy(vbase + (px + x) * pixsize,
650 			       vbuf + (px + out_x) * pixsize,
651 			       pixsize);
652 		}
653 		vbase += dev->fb_cap.fmt.bytesperline;
654 	}
655 }
656 
657 static void vivid_cap_update_frame_period(struct vivid_dev *dev)
658 {
659 	u64 f_period;
660 
661 	f_period = (u64)dev->timeperframe_vid_cap.numerator * 1000000000;
662 	if (WARN_ON(dev->timeperframe_vid_cap.denominator == 0))
663 		dev->timeperframe_vid_cap.denominator = 1;
664 	do_div(f_period, dev->timeperframe_vid_cap.denominator);
665 	if (dev->field_cap == V4L2_FIELD_ALTERNATE)
666 		f_period >>= 1;
667 	/*
668 	 * If "End of Frame", then offset the exposure time by 0.9
669 	 * of the frame period.
670 	 */
671 	dev->cap_frame_eof_offset = f_period * 9;
672 	do_div(dev->cap_frame_eof_offset, 10);
673 	dev->cap_frame_period = f_period;
674 }
675 
676 static noinline_for_stack void vivid_thread_vid_cap_tick(struct vivid_dev *dev,
677 							 int dropped_bufs)
678 {
679 	struct vivid_buffer *vid_cap_buf = NULL;
680 	struct vivid_buffer *vbi_cap_buf = NULL;
681 	struct vivid_buffer *meta_cap_buf = NULL;
682 	u64 f_time = 0;
683 
684 	dprintk(dev, 1, "Video Capture Thread Tick\n");
685 
686 	while (dropped_bufs-- > 1)
687 		tpg_update_mv_count(&dev->tpg,
688 				dev->field_cap == V4L2_FIELD_NONE ||
689 				dev->field_cap == V4L2_FIELD_ALTERNATE);
690 
691 	/* Drop a certain percentage of buffers. */
692 	if (dev->perc_dropped_buffers &&
693 	    prandom_u32_max(100) < dev->perc_dropped_buffers)
694 		goto update_mv;
695 
696 	spin_lock(&dev->slock);
697 	if (!list_empty(&dev->vid_cap_active)) {
698 		vid_cap_buf = list_entry(dev->vid_cap_active.next, struct vivid_buffer, list);
699 		list_del(&vid_cap_buf->list);
700 	}
701 	if (!list_empty(&dev->vbi_cap_active)) {
702 		if (dev->field_cap != V4L2_FIELD_ALTERNATE ||
703 		    (dev->vbi_cap_seq_count & 1)) {
704 			vbi_cap_buf = list_entry(dev->vbi_cap_active.next,
705 						 struct vivid_buffer, list);
706 			list_del(&vbi_cap_buf->list);
707 		}
708 	}
709 	if (!list_empty(&dev->meta_cap_active)) {
710 		meta_cap_buf = list_entry(dev->meta_cap_active.next,
711 					  struct vivid_buffer, list);
712 		list_del(&meta_cap_buf->list);
713 	}
714 
715 	spin_unlock(&dev->slock);
716 
717 	if (!vid_cap_buf && !vbi_cap_buf && !meta_cap_buf)
718 		goto update_mv;
719 
720 	f_time = dev->cap_frame_period * dev->vid_cap_seq_count +
721 		 dev->cap_stream_start + dev->time_wrap_offset;
722 
723 	if (vid_cap_buf) {
724 		v4l2_ctrl_request_setup(vid_cap_buf->vb.vb2_buf.req_obj.req,
725 					&dev->ctrl_hdl_vid_cap);
726 		/* Fill buffer */
727 		vivid_fillbuff(dev, vid_cap_buf);
728 		dprintk(dev, 1, "filled buffer %d\n",
729 			vid_cap_buf->vb.vb2_buf.index);
730 
731 		/* Handle overlay */
732 		if (dev->overlay_cap_owner && dev->fb_cap.base &&
733 			dev->fb_cap.fmt.pixelformat == dev->fmt_cap->fourcc)
734 			vivid_overlay(dev, vid_cap_buf);
735 
736 		v4l2_ctrl_request_complete(vid_cap_buf->vb.vb2_buf.req_obj.req,
737 					   &dev->ctrl_hdl_vid_cap);
738 		vb2_buffer_done(&vid_cap_buf->vb.vb2_buf, dev->dqbuf_error ?
739 				VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
740 		dprintk(dev, 2, "vid_cap buffer %d done\n",
741 				vid_cap_buf->vb.vb2_buf.index);
742 
743 		vid_cap_buf->vb.vb2_buf.timestamp = f_time;
744 		if (!dev->tstamp_src_is_soe)
745 			vid_cap_buf->vb.vb2_buf.timestamp += dev->cap_frame_eof_offset;
746 	}
747 
748 	if (vbi_cap_buf) {
749 		u64 vbi_period;
750 
751 		v4l2_ctrl_request_setup(vbi_cap_buf->vb.vb2_buf.req_obj.req,
752 					&dev->ctrl_hdl_vbi_cap);
753 		if (dev->stream_sliced_vbi_cap)
754 			vivid_sliced_vbi_cap_process(dev, vbi_cap_buf);
755 		else
756 			vivid_raw_vbi_cap_process(dev, vbi_cap_buf);
757 		v4l2_ctrl_request_complete(vbi_cap_buf->vb.vb2_buf.req_obj.req,
758 					   &dev->ctrl_hdl_vbi_cap);
759 		vb2_buffer_done(&vbi_cap_buf->vb.vb2_buf, dev->dqbuf_error ?
760 				VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
761 		dprintk(dev, 2, "vbi_cap %d done\n",
762 				vbi_cap_buf->vb.vb2_buf.index);
763 
764 		/* If capturing a VBI, offset by 0.05 */
765 		vbi_period = dev->cap_frame_period * 5;
766 		do_div(vbi_period, 100);
767 		vbi_cap_buf->vb.vb2_buf.timestamp = f_time + dev->cap_frame_eof_offset + vbi_period;
768 	}
769 
770 	if (meta_cap_buf) {
771 		v4l2_ctrl_request_setup(meta_cap_buf->vb.vb2_buf.req_obj.req,
772 					&dev->ctrl_hdl_meta_cap);
773 		vivid_meta_cap_fillbuff(dev, meta_cap_buf, f_time);
774 		v4l2_ctrl_request_complete(meta_cap_buf->vb.vb2_buf.req_obj.req,
775 					   &dev->ctrl_hdl_meta_cap);
776 		vb2_buffer_done(&meta_cap_buf->vb.vb2_buf, dev->dqbuf_error ?
777 				VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
778 		dprintk(dev, 2, "meta_cap %d done\n",
779 			meta_cap_buf->vb.vb2_buf.index);
780 		meta_cap_buf->vb.vb2_buf.timestamp = f_time + dev->cap_frame_eof_offset;
781 	}
782 
783 	dev->dqbuf_error = false;
784 
785 update_mv:
786 	/* Update the test pattern movement counters */
787 	tpg_update_mv_count(&dev->tpg, dev->field_cap == V4L2_FIELD_NONE ||
788 				       dev->field_cap == V4L2_FIELD_ALTERNATE);
789 }
790 
791 static int vivid_thread_vid_cap(void *data)
792 {
793 	struct vivid_dev *dev = data;
794 	u64 numerators_since_start;
795 	u64 buffers_since_start;
796 	u64 next_jiffies_since_start;
797 	unsigned long jiffies_since_start;
798 	unsigned long cur_jiffies;
799 	unsigned wait_jiffies;
800 	unsigned numerator;
801 	unsigned denominator;
802 	int dropped_bufs;
803 
804 	dprintk(dev, 1, "Video Capture Thread Start\n");
805 
806 	set_freezable();
807 
808 	/* Resets frame counters */
809 	dev->cap_seq_offset = 0;
810 	dev->cap_seq_count = 0;
811 	dev->cap_seq_resync = false;
812 	dev->jiffies_vid_cap = jiffies;
813 	dev->cap_stream_start = ktime_get_ns();
814 	vivid_cap_update_frame_period(dev);
815 
816 	for (;;) {
817 		try_to_freeze();
818 		if (kthread_should_stop())
819 			break;
820 
821 		if (!mutex_trylock(&dev->mutex)) {
822 			schedule();
823 			continue;
824 		}
825 
826 		cur_jiffies = jiffies;
827 		if (dev->cap_seq_resync) {
828 			dev->jiffies_vid_cap = cur_jiffies;
829 			dev->cap_seq_offset = dev->cap_seq_count + 1;
830 			dev->cap_seq_count = 0;
831 			dev->cap_stream_start += dev->cap_frame_period *
832 						 dev->cap_seq_offset;
833 			vivid_cap_update_frame_period(dev);
834 			dev->cap_seq_resync = false;
835 		}
836 		numerator = dev->timeperframe_vid_cap.numerator;
837 		denominator = dev->timeperframe_vid_cap.denominator;
838 
839 		if (dev->field_cap == V4L2_FIELD_ALTERNATE)
840 			denominator *= 2;
841 
842 		/* Calculate the number of jiffies since we started streaming */
843 		jiffies_since_start = cur_jiffies - dev->jiffies_vid_cap;
844 		/* Get the number of buffers streamed since the start */
845 		buffers_since_start = (u64)jiffies_since_start * denominator +
846 				      (HZ * numerator) / 2;
847 		do_div(buffers_since_start, HZ * numerator);
848 
849 		/*
850 		 * After more than 0xf0000000 (rounded down to a multiple of
851 		 * 'jiffies-per-day' to ease jiffies_to_msecs calculation)
852 		 * jiffies have passed since we started streaming reset the
853 		 * counters and keep track of the sequence offset.
854 		 */
855 		if (jiffies_since_start > JIFFIES_RESYNC) {
856 			dev->jiffies_vid_cap = cur_jiffies;
857 			dev->cap_seq_offset = buffers_since_start;
858 			buffers_since_start = 0;
859 		}
860 		dropped_bufs = buffers_since_start + dev->cap_seq_offset - dev->cap_seq_count;
861 		dev->cap_seq_count = buffers_since_start + dev->cap_seq_offset;
862 		dev->vid_cap_seq_count = dev->cap_seq_count - dev->vid_cap_seq_start;
863 		dev->vbi_cap_seq_count = dev->cap_seq_count - dev->vbi_cap_seq_start;
864 		dev->meta_cap_seq_count = dev->cap_seq_count - dev->meta_cap_seq_start;
865 
866 		vivid_thread_vid_cap_tick(dev, dropped_bufs);
867 
868 		/*
869 		 * Calculate the number of 'numerators' streamed since we started,
870 		 * including the current buffer.
871 		 */
872 		numerators_since_start = ++buffers_since_start * numerator;
873 
874 		/* And the number of jiffies since we started */
875 		jiffies_since_start = jiffies - dev->jiffies_vid_cap;
876 
877 		mutex_unlock(&dev->mutex);
878 
879 		/*
880 		 * Calculate when that next buffer is supposed to start
881 		 * in jiffies since we started streaming.
882 		 */
883 		next_jiffies_since_start = numerators_since_start * HZ +
884 					   denominator / 2;
885 		do_div(next_jiffies_since_start, denominator);
886 		/* If it is in the past, then just schedule asap */
887 		if (next_jiffies_since_start < jiffies_since_start)
888 			next_jiffies_since_start = jiffies_since_start;
889 
890 		wait_jiffies = next_jiffies_since_start - jiffies_since_start;
891 		while (jiffies - cur_jiffies < wait_jiffies &&
892 		       !kthread_should_stop())
893 			schedule();
894 	}
895 	dprintk(dev, 1, "Video Capture Thread End\n");
896 	return 0;
897 }
898 
899 static void vivid_grab_controls(struct vivid_dev *dev, bool grab)
900 {
901 	v4l2_ctrl_grab(dev->ctrl_has_crop_cap, grab);
902 	v4l2_ctrl_grab(dev->ctrl_has_compose_cap, grab);
903 	v4l2_ctrl_grab(dev->ctrl_has_scaler_cap, grab);
904 }
905 
906 int vivid_start_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
907 {
908 	dprintk(dev, 1, "%s\n", __func__);
909 
910 	if (dev->kthread_vid_cap) {
911 		u32 seq_count = dev->cap_seq_count + dev->seq_wrap * 128;
912 
913 		if (pstreaming == &dev->vid_cap_streaming)
914 			dev->vid_cap_seq_start = seq_count;
915 		else if (pstreaming == &dev->vbi_cap_streaming)
916 			dev->vbi_cap_seq_start = seq_count;
917 		else
918 			dev->meta_cap_seq_start = seq_count;
919 		*pstreaming = true;
920 		return 0;
921 	}
922 
923 	/* Resets frame counters */
924 	tpg_init_mv_count(&dev->tpg);
925 
926 	dev->vid_cap_seq_start = dev->seq_wrap * 128;
927 	dev->vbi_cap_seq_start = dev->seq_wrap * 128;
928 	dev->meta_cap_seq_start = dev->seq_wrap * 128;
929 
930 	dev->kthread_vid_cap = kthread_run(vivid_thread_vid_cap, dev,
931 			"%s-vid-cap", dev->v4l2_dev.name);
932 
933 	if (IS_ERR(dev->kthread_vid_cap)) {
934 		int err = PTR_ERR(dev->kthread_vid_cap);
935 
936 		dev->kthread_vid_cap = NULL;
937 		v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
938 		return err;
939 	}
940 	*pstreaming = true;
941 	vivid_grab_controls(dev, true);
942 
943 	dprintk(dev, 1, "returning from %s\n", __func__);
944 	return 0;
945 }
946 
947 void vivid_stop_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
948 {
949 	dprintk(dev, 1, "%s\n", __func__);
950 
951 	if (dev->kthread_vid_cap == NULL)
952 		return;
953 
954 	*pstreaming = false;
955 	if (pstreaming == &dev->vid_cap_streaming) {
956 		/* Release all active buffers */
957 		while (!list_empty(&dev->vid_cap_active)) {
958 			struct vivid_buffer *buf;
959 
960 			buf = list_entry(dev->vid_cap_active.next,
961 					 struct vivid_buffer, list);
962 			list_del(&buf->list);
963 			v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
964 						   &dev->ctrl_hdl_vid_cap);
965 			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
966 			dprintk(dev, 2, "vid_cap buffer %d done\n",
967 				buf->vb.vb2_buf.index);
968 		}
969 	}
970 
971 	if (pstreaming == &dev->vbi_cap_streaming) {
972 		while (!list_empty(&dev->vbi_cap_active)) {
973 			struct vivid_buffer *buf;
974 
975 			buf = list_entry(dev->vbi_cap_active.next,
976 					 struct vivid_buffer, list);
977 			list_del(&buf->list);
978 			v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
979 						   &dev->ctrl_hdl_vbi_cap);
980 			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
981 			dprintk(dev, 2, "vbi_cap buffer %d done\n",
982 				buf->vb.vb2_buf.index);
983 		}
984 	}
985 
986 	if (pstreaming == &dev->meta_cap_streaming) {
987 		while (!list_empty(&dev->meta_cap_active)) {
988 			struct vivid_buffer *buf;
989 
990 			buf = list_entry(dev->meta_cap_active.next,
991 					 struct vivid_buffer, list);
992 			list_del(&buf->list);
993 			v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
994 						   &dev->ctrl_hdl_meta_cap);
995 			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
996 			dprintk(dev, 2, "meta_cap buffer %d done\n",
997 				buf->vb.vb2_buf.index);
998 		}
999 	}
1000 
1001 	if (dev->vid_cap_streaming || dev->vbi_cap_streaming ||
1002 	    dev->meta_cap_streaming)
1003 		return;
1004 
1005 	/* shutdown control thread */
1006 	vivid_grab_controls(dev, false);
1007 	kthread_stop(dev->kthread_vid_cap);
1008 	dev->kthread_vid_cap = NULL;
1009 }
1010