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 	v4l2_ctrl_s_ctrl(dev->ro_int32, buf->vb.sequence & 0xff);
430 	if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
431 		/*
432 		 * 60 Hz standards start with the bottom field, 50 Hz standards
433 		 * with the top field. So if the 0-based seq_count is even,
434 		 * then the field is TOP for 50 Hz and BOTTOM for 60 Hz
435 		 * standards.
436 		 */
437 		buf->vb.field = ((dev->vid_cap_seq_count & 1) ^ is_60hz) ?
438 			V4L2_FIELD_BOTTOM : V4L2_FIELD_TOP;
439 		/*
440 		 * The sequence counter counts frames, not fields. So divide
441 		 * by two.
442 		 */
443 		buf->vb.sequence /= 2;
444 	} else {
445 		buf->vb.field = dev->field_cap;
446 	}
447 	tpg_s_field(tpg, buf->vb.field,
448 		    dev->field_cap == V4L2_FIELD_ALTERNATE);
449 	tpg_s_perc_fill_blank(tpg, dev->must_blank[buf->vb.vb2_buf.index]);
450 
451 	vivid_precalc_copy_rects(dev);
452 
453 	for (p = 0; p < tpg_g_planes(tpg); p++) {
454 		void *vbuf = plane_vaddr(tpg, buf, p,
455 					 tpg->bytesperline, tpg->buf_height);
456 
457 		/*
458 		 * The first plane of a multiplanar format has a non-zero
459 		 * data_offset. This helps testing whether the application
460 		 * correctly supports non-zero data offsets.
461 		 */
462 		if (p < tpg_g_buffers(tpg) && dev->fmt_cap->data_offset[p]) {
463 			memset(vbuf, dev->fmt_cap->data_offset[p] & 0xff,
464 			       dev->fmt_cap->data_offset[p]);
465 			vbuf += dev->fmt_cap->data_offset[p];
466 		}
467 		tpg_calc_text_basep(tpg, basep, p, vbuf);
468 		if (!is_loop || vivid_copy_buffer(dev, p, vbuf, buf))
469 			tpg_fill_plane_buffer(tpg, vivid_get_std_cap(dev),
470 					p, vbuf);
471 	}
472 	dev->must_blank[buf->vb.vb2_buf.index] = false;
473 
474 	/* Updates stream time, only update at the start of a new frame. */
475 	if (dev->field_cap != V4L2_FIELD_ALTERNATE ||
476 			(dev->vid_cap_seq_count & 1) == 0)
477 		dev->ms_vid_cap =
478 			jiffies_to_msecs(jiffies - dev->jiffies_vid_cap);
479 
480 	ms = dev->ms_vid_cap;
481 	if (dev->osd_mode <= 1) {
482 		snprintf(str, sizeof(str), " %02d:%02d:%02d:%03d %u%s",
483 				(ms / (60 * 60 * 1000)) % 24,
484 				(ms / (60 * 1000)) % 60,
485 				(ms / 1000) % 60,
486 				ms % 1000,
487 				buf->vb.sequence,
488 				(dev->field_cap == V4L2_FIELD_ALTERNATE) ?
489 					(buf->vb.field == V4L2_FIELD_TOP ?
490 					 " top" : " bottom") : "");
491 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
492 	}
493 	if (dev->osd_mode == 0) {
494 		snprintf(str, sizeof(str), " %dx%d, input %d ",
495 				dev->src_rect.width, dev->src_rect.height, dev->input);
496 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
497 
498 		gain = v4l2_ctrl_g_ctrl(dev->gain);
499 		mutex_lock(dev->ctrl_hdl_user_vid.lock);
500 		snprintf(str, sizeof(str),
501 			" brightness %3d, contrast %3d, saturation %3d, hue %d ",
502 			dev->brightness->cur.val,
503 			dev->contrast->cur.val,
504 			dev->saturation->cur.val,
505 			dev->hue->cur.val);
506 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
507 		snprintf(str, sizeof(str),
508 			" autogain %d, gain %3d, alpha 0x%02x ",
509 			dev->autogain->cur.val, gain, dev->alpha->cur.val);
510 		mutex_unlock(dev->ctrl_hdl_user_vid.lock);
511 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
512 		mutex_lock(dev->ctrl_hdl_user_aud.lock);
513 		snprintf(str, sizeof(str),
514 			" volume %3d, mute %d ",
515 			dev->volume->cur.val, dev->mute->cur.val);
516 		mutex_unlock(dev->ctrl_hdl_user_aud.lock);
517 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
518 		mutex_lock(dev->ctrl_hdl_user_gen.lock);
519 		snprintf(str, sizeof(str), " int32 %d, ro_int32 %d, int64 %lld, bitmask %08x ",
520 			 dev->int32->cur.val,
521 			 dev->ro_int32->cur.val,
522 			 *dev->int64->p_cur.p_s64,
523 			 dev->bitmask->cur.val);
524 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
525 		snprintf(str, sizeof(str), " boolean %d, menu %s, string \"%s\" ",
526 			dev->boolean->cur.val,
527 			dev->menu->qmenu[dev->menu->cur.val],
528 			dev->string->p_cur.p_char);
529 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
530 		snprintf(str, sizeof(str), " integer_menu %lld, value %d ",
531 			dev->int_menu->qmenu_int[dev->int_menu->cur.val],
532 			dev->int_menu->cur.val);
533 		mutex_unlock(dev->ctrl_hdl_user_gen.lock);
534 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
535 		if (dev->button_pressed) {
536 			dev->button_pressed--;
537 			snprintf(str, sizeof(str), " button pressed!");
538 			tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
539 		}
540 		if (dev->osd[0]) {
541 			if (vivid_is_hdmi_cap(dev)) {
542 				snprintf(str, sizeof(str),
543 					 " OSD \"%s\"", dev->osd);
544 				tpg_gen_text(tpg, basep, line++ * line_height,
545 					     16, str);
546 			}
547 			if (dev->osd_jiffies &&
548 			    time_is_before_jiffies(dev->osd_jiffies + 5 * HZ)) {
549 				dev->osd[0] = 0;
550 				dev->osd_jiffies = 0;
551 			}
552 		}
553 	}
554 }
555 
556 /*
557  * Return true if this pixel coordinate is a valid video pixel.
558  */
559 static bool valid_pix(struct vivid_dev *dev, int win_y, int win_x, int fb_y, int fb_x)
560 {
561 	int i;
562 
563 	if (dev->bitmap_cap) {
564 		/*
565 		 * Only if the corresponding bit in the bitmap is set can
566 		 * the video pixel be shown. Coordinates are relative to
567 		 * the overlay window set by VIDIOC_S_FMT.
568 		 */
569 		const u8 *p = dev->bitmap_cap;
570 		unsigned stride = (dev->compose_cap.width + 7) / 8;
571 
572 		if (!(p[stride * win_y + win_x / 8] & (1 << (win_x & 7))))
573 			return false;
574 	}
575 
576 	for (i = 0; i < dev->clipcount_cap; i++) {
577 		/*
578 		 * Only if the framebuffer coordinate is not in any of the
579 		 * clip rectangles will be video pixel be shown.
580 		 */
581 		struct v4l2_rect *r = &dev->clips_cap[i].c;
582 
583 		if (fb_y >= r->top && fb_y < r->top + r->height &&
584 		    fb_x >= r->left && fb_x < r->left + r->width)
585 			return false;
586 	}
587 	return true;
588 }
589 
590 /*
591  * Draw the image into the overlay buffer.
592  * Note that the combination of overlay and multiplanar is not supported.
593  */
594 static void vivid_overlay(struct vivid_dev *dev, struct vivid_buffer *buf)
595 {
596 	struct tpg_data *tpg = &dev->tpg;
597 	unsigned pixsize = tpg_g_twopixelsize(tpg, 0) / 2;
598 	void *vbase = dev->fb_vbase_cap;
599 	void *vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
600 	unsigned img_width = dev->compose_cap.width;
601 	unsigned img_height = dev->compose_cap.height;
602 	unsigned stride = tpg->bytesperline[0];
603 	/* if quick is true, then valid_pix() doesn't have to be called */
604 	bool quick = dev->bitmap_cap == NULL && dev->clipcount_cap == 0;
605 	int x, y, w, out_x = 0;
606 
607 	/*
608 	 * Overlay support is only supported for formats that have a twopixelsize
609 	 * that's >= 2. Warn and bail out if that's not the case.
610 	 */
611 	if (WARN_ON(pixsize == 0))
612 		return;
613 	if ((dev->overlay_cap_field == V4L2_FIELD_TOP ||
614 	     dev->overlay_cap_field == V4L2_FIELD_BOTTOM) &&
615 	    dev->overlay_cap_field != buf->vb.field)
616 		return;
617 
618 	vbuf += dev->compose_cap.left * pixsize + dev->compose_cap.top * stride;
619 	x = dev->overlay_cap_left;
620 	w = img_width;
621 	if (x < 0) {
622 		out_x = -x;
623 		w = w - out_x;
624 		x = 0;
625 	} else {
626 		w = dev->fb_cap.fmt.width - x;
627 		if (w > img_width)
628 			w = img_width;
629 	}
630 	if (w <= 0)
631 		return;
632 	if (dev->overlay_cap_top >= 0)
633 		vbase += dev->overlay_cap_top * dev->fb_cap.fmt.bytesperline;
634 	for (y = dev->overlay_cap_top;
635 	     y < dev->overlay_cap_top + (int)img_height;
636 	     y++, vbuf += stride) {
637 		int px;
638 
639 		if (y < 0 || y > dev->fb_cap.fmt.height)
640 			continue;
641 		if (quick) {
642 			memcpy(vbase + x * pixsize,
643 			       vbuf + out_x * pixsize, w * pixsize);
644 			vbase += dev->fb_cap.fmt.bytesperline;
645 			continue;
646 		}
647 		for (px = 0; px < w; px++) {
648 			if (!valid_pix(dev, y - dev->overlay_cap_top,
649 				       px + out_x, y, px + x))
650 				continue;
651 			memcpy(vbase + (px + x) * pixsize,
652 			       vbuf + (px + out_x) * pixsize,
653 			       pixsize);
654 		}
655 		vbase += dev->fb_cap.fmt.bytesperline;
656 	}
657 }
658 
659 static void vivid_cap_update_frame_period(struct vivid_dev *dev)
660 {
661 	u64 f_period;
662 
663 	f_period = (u64)dev->timeperframe_vid_cap.numerator * 1000000000;
664 	if (WARN_ON(dev->timeperframe_vid_cap.denominator == 0))
665 		dev->timeperframe_vid_cap.denominator = 1;
666 	do_div(f_period, dev->timeperframe_vid_cap.denominator);
667 	if (dev->field_cap == V4L2_FIELD_ALTERNATE)
668 		f_period >>= 1;
669 	/*
670 	 * If "End of Frame", then offset the exposure time by 0.9
671 	 * of the frame period.
672 	 */
673 	dev->cap_frame_eof_offset = f_period * 9;
674 	do_div(dev->cap_frame_eof_offset, 10);
675 	dev->cap_frame_period = f_period;
676 }
677 
678 static noinline_for_stack void vivid_thread_vid_cap_tick(struct vivid_dev *dev,
679 							 int dropped_bufs)
680 {
681 	struct vivid_buffer *vid_cap_buf = NULL;
682 	struct vivid_buffer *vbi_cap_buf = NULL;
683 	struct vivid_buffer *meta_cap_buf = NULL;
684 	u64 f_time = 0;
685 
686 	dprintk(dev, 1, "Video Capture Thread Tick\n");
687 
688 	while (dropped_bufs-- > 1)
689 		tpg_update_mv_count(&dev->tpg,
690 				dev->field_cap == V4L2_FIELD_NONE ||
691 				dev->field_cap == V4L2_FIELD_ALTERNATE);
692 
693 	/* Drop a certain percentage of buffers. */
694 	if (dev->perc_dropped_buffers &&
695 	    prandom_u32_max(100) < dev->perc_dropped_buffers)
696 		goto update_mv;
697 
698 	spin_lock(&dev->slock);
699 	if (!list_empty(&dev->vid_cap_active)) {
700 		vid_cap_buf = list_entry(dev->vid_cap_active.next, struct vivid_buffer, list);
701 		list_del(&vid_cap_buf->list);
702 	}
703 	if (!list_empty(&dev->vbi_cap_active)) {
704 		if (dev->field_cap != V4L2_FIELD_ALTERNATE ||
705 		    (dev->vbi_cap_seq_count & 1)) {
706 			vbi_cap_buf = list_entry(dev->vbi_cap_active.next,
707 						 struct vivid_buffer, list);
708 			list_del(&vbi_cap_buf->list);
709 		}
710 	}
711 	if (!list_empty(&dev->meta_cap_active)) {
712 		meta_cap_buf = list_entry(dev->meta_cap_active.next,
713 					  struct vivid_buffer, list);
714 		list_del(&meta_cap_buf->list);
715 	}
716 
717 	spin_unlock(&dev->slock);
718 
719 	if (!vid_cap_buf && !vbi_cap_buf && !meta_cap_buf)
720 		goto update_mv;
721 
722 	f_time = dev->cap_frame_period * dev->vid_cap_seq_count +
723 		 dev->cap_stream_start + dev->time_wrap_offset;
724 
725 	if (vid_cap_buf) {
726 		v4l2_ctrl_request_setup(vid_cap_buf->vb.vb2_buf.req_obj.req,
727 					&dev->ctrl_hdl_vid_cap);
728 		/* Fill buffer */
729 		vivid_fillbuff(dev, vid_cap_buf);
730 		dprintk(dev, 1, "filled buffer %d\n",
731 			vid_cap_buf->vb.vb2_buf.index);
732 
733 		/* Handle overlay */
734 		if (dev->overlay_cap_owner && dev->fb_cap.base &&
735 			dev->fb_cap.fmt.pixelformat == dev->fmt_cap->fourcc)
736 			vivid_overlay(dev, vid_cap_buf);
737 
738 		v4l2_ctrl_request_complete(vid_cap_buf->vb.vb2_buf.req_obj.req,
739 					   &dev->ctrl_hdl_vid_cap);
740 		vb2_buffer_done(&vid_cap_buf->vb.vb2_buf, dev->dqbuf_error ?
741 				VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
742 		dprintk(dev, 2, "vid_cap buffer %d done\n",
743 				vid_cap_buf->vb.vb2_buf.index);
744 
745 		vid_cap_buf->vb.vb2_buf.timestamp = f_time;
746 		if (!dev->tstamp_src_is_soe)
747 			vid_cap_buf->vb.vb2_buf.timestamp += dev->cap_frame_eof_offset;
748 	}
749 
750 	if (vbi_cap_buf) {
751 		u64 vbi_period;
752 
753 		v4l2_ctrl_request_setup(vbi_cap_buf->vb.vb2_buf.req_obj.req,
754 					&dev->ctrl_hdl_vbi_cap);
755 		if (vbi_cap_buf->vb.vb2_buf.type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
756 			vivid_sliced_vbi_cap_process(dev, vbi_cap_buf);
757 		else
758 			vivid_raw_vbi_cap_process(dev, vbi_cap_buf);
759 		v4l2_ctrl_request_complete(vbi_cap_buf->vb.vb2_buf.req_obj.req,
760 					   &dev->ctrl_hdl_vbi_cap);
761 		vb2_buffer_done(&vbi_cap_buf->vb.vb2_buf, dev->dqbuf_error ?
762 				VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
763 		dprintk(dev, 2, "vbi_cap %d done\n",
764 				vbi_cap_buf->vb.vb2_buf.index);
765 
766 		/* If capturing a VBI, offset by 0.05 */
767 		vbi_period = dev->cap_frame_period * 5;
768 		do_div(vbi_period, 100);
769 		vbi_cap_buf->vb.vb2_buf.timestamp = f_time + dev->cap_frame_eof_offset + vbi_period;
770 	}
771 
772 	if (meta_cap_buf) {
773 		v4l2_ctrl_request_setup(meta_cap_buf->vb.vb2_buf.req_obj.req,
774 					&dev->ctrl_hdl_meta_cap);
775 		vivid_meta_cap_fillbuff(dev, meta_cap_buf, f_time);
776 		v4l2_ctrl_request_complete(meta_cap_buf->vb.vb2_buf.req_obj.req,
777 					   &dev->ctrl_hdl_meta_cap);
778 		vb2_buffer_done(&meta_cap_buf->vb.vb2_buf, dev->dqbuf_error ?
779 				VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
780 		dprintk(dev, 2, "meta_cap %d done\n",
781 			meta_cap_buf->vb.vb2_buf.index);
782 		meta_cap_buf->vb.vb2_buf.timestamp = f_time + dev->cap_frame_eof_offset;
783 	}
784 
785 	dev->dqbuf_error = false;
786 
787 update_mv:
788 	/* Update the test pattern movement counters */
789 	tpg_update_mv_count(&dev->tpg, dev->field_cap == V4L2_FIELD_NONE ||
790 				       dev->field_cap == V4L2_FIELD_ALTERNATE);
791 }
792 
793 static int vivid_thread_vid_cap(void *data)
794 {
795 	struct vivid_dev *dev = data;
796 	u64 numerators_since_start;
797 	u64 buffers_since_start;
798 	u64 next_jiffies_since_start;
799 	unsigned long jiffies_since_start;
800 	unsigned long cur_jiffies;
801 	unsigned wait_jiffies;
802 	unsigned numerator;
803 	unsigned denominator;
804 	int dropped_bufs;
805 
806 	dprintk(dev, 1, "Video Capture Thread Start\n");
807 
808 	set_freezable();
809 
810 	/* Resets frame counters */
811 	dev->cap_seq_offset = 0;
812 	dev->cap_seq_count = 0;
813 	dev->cap_seq_resync = false;
814 	dev->jiffies_vid_cap = jiffies;
815 	dev->cap_stream_start = ktime_get_ns();
816 	vivid_cap_update_frame_period(dev);
817 
818 	for (;;) {
819 		try_to_freeze();
820 		if (kthread_should_stop())
821 			break;
822 
823 		if (!mutex_trylock(&dev->mutex)) {
824 			schedule();
825 			continue;
826 		}
827 
828 		cur_jiffies = jiffies;
829 		if (dev->cap_seq_resync) {
830 			dev->jiffies_vid_cap = cur_jiffies;
831 			dev->cap_seq_offset = dev->cap_seq_count + 1;
832 			dev->cap_seq_count = 0;
833 			dev->cap_stream_start += dev->cap_frame_period *
834 						 dev->cap_seq_offset;
835 			vivid_cap_update_frame_period(dev);
836 			dev->cap_seq_resync = false;
837 		}
838 		numerator = dev->timeperframe_vid_cap.numerator;
839 		denominator = dev->timeperframe_vid_cap.denominator;
840 
841 		if (dev->field_cap == V4L2_FIELD_ALTERNATE)
842 			denominator *= 2;
843 
844 		/* Calculate the number of jiffies since we started streaming */
845 		jiffies_since_start = cur_jiffies - dev->jiffies_vid_cap;
846 		/* Get the number of buffers streamed since the start */
847 		buffers_since_start = (u64)jiffies_since_start * denominator +
848 				      (HZ * numerator) / 2;
849 		do_div(buffers_since_start, HZ * numerator);
850 
851 		/*
852 		 * After more than 0xf0000000 (rounded down to a multiple of
853 		 * 'jiffies-per-day' to ease jiffies_to_msecs calculation)
854 		 * jiffies have passed since we started streaming reset the
855 		 * counters and keep track of the sequence offset.
856 		 */
857 		if (jiffies_since_start > JIFFIES_RESYNC) {
858 			dev->jiffies_vid_cap = cur_jiffies;
859 			dev->cap_seq_offset = buffers_since_start;
860 			buffers_since_start = 0;
861 		}
862 		dropped_bufs = buffers_since_start + dev->cap_seq_offset - dev->cap_seq_count;
863 		dev->cap_seq_count = buffers_since_start + dev->cap_seq_offset;
864 		dev->vid_cap_seq_count = dev->cap_seq_count - dev->vid_cap_seq_start;
865 		dev->vbi_cap_seq_count = dev->cap_seq_count - dev->vbi_cap_seq_start;
866 		dev->meta_cap_seq_count = dev->cap_seq_count - dev->meta_cap_seq_start;
867 
868 		vivid_thread_vid_cap_tick(dev, dropped_bufs);
869 
870 		/*
871 		 * Calculate the number of 'numerators' streamed since we started,
872 		 * including the current buffer.
873 		 */
874 		numerators_since_start = ++buffers_since_start * numerator;
875 
876 		/* And the number of jiffies since we started */
877 		jiffies_since_start = jiffies - dev->jiffies_vid_cap;
878 
879 		mutex_unlock(&dev->mutex);
880 
881 		/*
882 		 * Calculate when that next buffer is supposed to start
883 		 * in jiffies since we started streaming.
884 		 */
885 		next_jiffies_since_start = numerators_since_start * HZ +
886 					   denominator / 2;
887 		do_div(next_jiffies_since_start, denominator);
888 		/* If it is in the past, then just schedule asap */
889 		if (next_jiffies_since_start < jiffies_since_start)
890 			next_jiffies_since_start = jiffies_since_start;
891 
892 		wait_jiffies = next_jiffies_since_start - jiffies_since_start;
893 		while (jiffies - cur_jiffies < wait_jiffies &&
894 		       !kthread_should_stop())
895 			schedule();
896 	}
897 	dprintk(dev, 1, "Video Capture Thread End\n");
898 	return 0;
899 }
900 
901 static void vivid_grab_controls(struct vivid_dev *dev, bool grab)
902 {
903 	v4l2_ctrl_grab(dev->ctrl_has_crop_cap, grab);
904 	v4l2_ctrl_grab(dev->ctrl_has_compose_cap, grab);
905 	v4l2_ctrl_grab(dev->ctrl_has_scaler_cap, grab);
906 }
907 
908 int vivid_start_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
909 {
910 	dprintk(dev, 1, "%s\n", __func__);
911 
912 	if (dev->kthread_vid_cap) {
913 		u32 seq_count = dev->cap_seq_count + dev->seq_wrap * 128;
914 
915 		if (pstreaming == &dev->vid_cap_streaming)
916 			dev->vid_cap_seq_start = seq_count;
917 		else if (pstreaming == &dev->vbi_cap_streaming)
918 			dev->vbi_cap_seq_start = seq_count;
919 		else
920 			dev->meta_cap_seq_start = seq_count;
921 		*pstreaming = true;
922 		return 0;
923 	}
924 
925 	/* Resets frame counters */
926 	tpg_init_mv_count(&dev->tpg);
927 
928 	dev->vid_cap_seq_start = dev->seq_wrap * 128;
929 	dev->vbi_cap_seq_start = dev->seq_wrap * 128;
930 	dev->meta_cap_seq_start = dev->seq_wrap * 128;
931 
932 	dev->kthread_vid_cap = kthread_run(vivid_thread_vid_cap, dev,
933 			"%s-vid-cap", dev->v4l2_dev.name);
934 
935 	if (IS_ERR(dev->kthread_vid_cap)) {
936 		int err = PTR_ERR(dev->kthread_vid_cap);
937 
938 		dev->kthread_vid_cap = NULL;
939 		v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
940 		return err;
941 	}
942 	*pstreaming = true;
943 	vivid_grab_controls(dev, true);
944 
945 	dprintk(dev, 1, "returning from %s\n", __func__);
946 	return 0;
947 }
948 
949 void vivid_stop_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
950 {
951 	dprintk(dev, 1, "%s\n", __func__);
952 
953 	if (dev->kthread_vid_cap == NULL)
954 		return;
955 
956 	*pstreaming = false;
957 	if (pstreaming == &dev->vid_cap_streaming) {
958 		/* Release all active buffers */
959 		while (!list_empty(&dev->vid_cap_active)) {
960 			struct vivid_buffer *buf;
961 
962 			buf = list_entry(dev->vid_cap_active.next,
963 					 struct vivid_buffer, list);
964 			list_del(&buf->list);
965 			v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
966 						   &dev->ctrl_hdl_vid_cap);
967 			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
968 			dprintk(dev, 2, "vid_cap buffer %d done\n",
969 				buf->vb.vb2_buf.index);
970 		}
971 	}
972 
973 	if (pstreaming == &dev->vbi_cap_streaming) {
974 		while (!list_empty(&dev->vbi_cap_active)) {
975 			struct vivid_buffer *buf;
976 
977 			buf = list_entry(dev->vbi_cap_active.next,
978 					 struct vivid_buffer, list);
979 			list_del(&buf->list);
980 			v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
981 						   &dev->ctrl_hdl_vbi_cap);
982 			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
983 			dprintk(dev, 2, "vbi_cap buffer %d done\n",
984 				buf->vb.vb2_buf.index);
985 		}
986 	}
987 
988 	if (pstreaming == &dev->meta_cap_streaming) {
989 		while (!list_empty(&dev->meta_cap_active)) {
990 			struct vivid_buffer *buf;
991 
992 			buf = list_entry(dev->meta_cap_active.next,
993 					 struct vivid_buffer, list);
994 			list_del(&buf->list);
995 			v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
996 						   &dev->ctrl_hdl_meta_cap);
997 			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
998 			dprintk(dev, 2, "meta_cap buffer %d done\n",
999 				buf->vb.vb2_buf.index);
1000 		}
1001 	}
1002 
1003 	if (dev->vid_cap_streaming || dev->vbi_cap_streaming ||
1004 	    dev->meta_cap_streaming)
1005 		return;
1006 
1007 	/* shutdown control thread */
1008 	vivid_grab_controls(dev, false);
1009 	kthread_stop(dev->kthread_vid_cap);
1010 	dev->kthread_vid_cap = NULL;
1011 }
1012