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