blob: f8006a30c12fb7c2234b00ed70d2b02d1f4008aa [file] [log] [blame]
Hans Verkuil67a51352018-02-07 09:12:45 -05001// SPDX-License-Identifier: GPL-2.0-only
Hans Verkuil3f682ff2014-08-25 08:00:20 -03002/*
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.
Hans Verkuil3f682ff2014-08-25 08:00:20 -03006 */
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>
Hans Verkuild1e5d8b2016-04-03 16:42:43 -030027#include <media/v4l2-rect.h>
Hans Verkuil3f682ff2014-08-25 08:00:20 -030028
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"
Mauro Carvalho Chehab96ec7d22014-09-02 17:57:34 -030041#include "vivid-kthread-cap.h"
Hans Verkuil3f682ff2014-08-25 08:00:20 -030042
43static inline v4l2_std_id vivid_get_std_cap(const struct vivid_dev *dev)
44{
45 if (vivid_is_sdtv_cap(dev))
46 return dev->std_cap;
47 return 0;
48}
49
50static void copy_pix(struct vivid_dev *dev, int win_y, int win_x,
51 u16 *cap, const u16 *osd)
52{
53 u16 out;
54 int left = dev->overlay_out_left;
55 int top = dev->overlay_out_top;
56 int fb_x = win_x + left;
57 int fb_y = win_y + top;
58 int i;
59
60 out = *cap;
61 *cap = *osd;
62 if (dev->bitmap_out) {
63 const u8 *p = dev->bitmap_out;
64 unsigned stride = (dev->compose_out.width + 7) / 8;
65
66 win_x -= dev->compose_out.left;
67 win_y -= dev->compose_out.top;
68 if (!(p[stride * win_y + win_x / 8] & (1 << (win_x & 7))))
69 return;
70 }
71
72 for (i = 0; i < dev->clipcount_out; i++) {
73 struct v4l2_rect *r = &dev->clips_out[i].c;
74
75 if (fb_y >= r->top && fb_y < r->top + r->height &&
76 fb_x >= r->left && fb_x < r->left + r->width)
77 return;
78 }
79 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
80 *osd != dev->chromakey_out)
81 return;
82 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
83 out == dev->chromakey_out)
84 return;
85 if (dev->fmt_cap->alpha_mask) {
86 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) &&
87 dev->global_alpha_out)
88 return;
89 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) &&
90 *cap & dev->fmt_cap->alpha_mask)
91 return;
92 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_LOCAL_INV_ALPHA) &&
93 !(*cap & dev->fmt_cap->alpha_mask))
94 return;
95 }
96 *cap = out;
97}
98
99static void blend_line(struct vivid_dev *dev, unsigned y_offset, unsigned x_offset,
100 u8 *vcapbuf, const u8 *vosdbuf,
101 unsigned width, unsigned pixsize)
102{
103 unsigned x;
104
105 for (x = 0; x < width; x++, vcapbuf += pixsize, vosdbuf += pixsize) {
106 copy_pix(dev, y_offset, x_offset + x,
107 (u16 *)vcapbuf, (const u16 *)vosdbuf);
108 }
109}
110
111static void scale_line(const u8 *src, u8 *dst, unsigned srcw, unsigned dstw, unsigned twopixsize)
112{
113 /* Coarse scaling with Bresenham */
114 unsigned int_part;
115 unsigned fract_part;
116 unsigned src_x = 0;
117 unsigned error = 0;
118 unsigned x;
119
120 /*
121 * We always combine two pixels to prevent color bleed in the packed
122 * yuv case.
123 */
124 srcw /= 2;
125 dstw /= 2;
126 int_part = srcw / dstw;
127 fract_part = srcw % dstw;
128 for (x = 0; x < dstw; x++, dst += twopixsize) {
129 memcpy(dst, src + src_x * twopixsize, twopixsize);
130 src_x += int_part;
131 error += fract_part;
132 if (error >= dstw) {
133 error -= dstw;
134 src_x++;
135 }
136 }
137}
138
139/*
140 * Precalculate the rectangles needed to perform video looping:
141 *
142 * The nominal pipeline is that the video output buffer is cropped by
143 * crop_out, scaled to compose_out, overlaid with the output overlay,
144 * cropped on the capture side by crop_cap and scaled again to the video
145 * capture buffer using compose_cap.
146 *
147 * To keep things efficient we calculate the intersection of compose_out
148 * and crop_cap (since that's the only part of the video that will
149 * actually end up in the capture buffer), determine which part of the
150 * video output buffer that is and which part of the video capture buffer
151 * so we can scale the video straight from the output buffer to the capture
152 * buffer without any intermediate steps.
153 *
154 * If we need to deal with an output overlay, then there is no choice and
155 * that intermediate step still has to be taken. For the output overlay
156 * support we calculate the intersection of the framebuffer and the overlay
157 * window (which may be partially or wholly outside of the framebuffer
158 * itself) and the intersection of that with loop_vid_copy (i.e. the part of
159 * the actual looped video that will be overlaid). The result is calculated
160 * both in framebuffer coordinates (loop_fb_copy) and compose_out coordinates
161 * (loop_vid_overlay). Finally calculate the part of the capture buffer that
162 * will receive that overlaid video.
163 */
164static void vivid_precalc_copy_rects(struct vivid_dev *dev)
165{
166 /* Framebuffer rectangle */
167 struct v4l2_rect r_fb = {
168 0, 0, dev->display_width, dev->display_height
169 };
170 /* Overlay window rectangle in framebuffer coordinates */
171 struct v4l2_rect r_overlay = {
172 dev->overlay_out_left, dev->overlay_out_top,
173 dev->compose_out.width, dev->compose_out.height
174 };
175
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300176 v4l2_rect_intersect(&dev->loop_vid_copy, &dev->crop_cap, &dev->compose_out);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300177
178 dev->loop_vid_out = dev->loop_vid_copy;
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300179 v4l2_rect_scale(&dev->loop_vid_out, &dev->compose_out, &dev->crop_out);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300180 dev->loop_vid_out.left += dev->crop_out.left;
181 dev->loop_vid_out.top += dev->crop_out.top;
182
183 dev->loop_vid_cap = dev->loop_vid_copy;
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300184 v4l2_rect_scale(&dev->loop_vid_cap, &dev->crop_cap, &dev->compose_cap);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300185
186 dprintk(dev, 1,
187 "loop_vid_copy: %dx%d@%dx%d loop_vid_out: %dx%d@%dx%d loop_vid_cap: %dx%d@%dx%d\n",
188 dev->loop_vid_copy.width, dev->loop_vid_copy.height,
189 dev->loop_vid_copy.left, dev->loop_vid_copy.top,
190 dev->loop_vid_out.width, dev->loop_vid_out.height,
191 dev->loop_vid_out.left, dev->loop_vid_out.top,
192 dev->loop_vid_cap.width, dev->loop_vid_cap.height,
193 dev->loop_vid_cap.left, dev->loop_vid_cap.top);
194
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300195 v4l2_rect_intersect(&r_overlay, &r_fb, &r_overlay);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300196
197 /* shift r_overlay to the same origin as compose_out */
198 r_overlay.left += dev->compose_out.left - dev->overlay_out_left;
199 r_overlay.top += dev->compose_out.top - dev->overlay_out_top;
200
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300201 v4l2_rect_intersect(&dev->loop_vid_overlay, &r_overlay, &dev->loop_vid_copy);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300202 dev->loop_fb_copy = dev->loop_vid_overlay;
203
204 /* shift dev->loop_fb_copy back again to the fb origin */
205 dev->loop_fb_copy.left -= dev->compose_out.left - dev->overlay_out_left;
206 dev->loop_fb_copy.top -= dev->compose_out.top - dev->overlay_out_top;
207
208 dev->loop_vid_overlay_cap = dev->loop_vid_overlay;
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300209 v4l2_rect_scale(&dev->loop_vid_overlay_cap, &dev->crop_cap, &dev->compose_cap);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300210
211 dprintk(dev, 1,
212 "loop_fb_copy: %dx%d@%dx%d loop_vid_overlay: %dx%d@%dx%d loop_vid_overlay_cap: %dx%d@%dx%d\n",
213 dev->loop_fb_copy.width, dev->loop_fb_copy.height,
214 dev->loop_fb_copy.left, dev->loop_fb_copy.top,
215 dev->loop_vid_overlay.width, dev->loop_vid_overlay.height,
216 dev->loop_vid_overlay.left, dev->loop_vid_overlay.top,
217 dev->loop_vid_overlay_cap.width, dev->loop_vid_overlay_cap.height,
218 dev->loop_vid_overlay_cap.left, dev->loop_vid_overlay_cap.top);
219}
220
Hans Verkuilddcaee92015-03-09 12:03:52 -0300221static void *plane_vaddr(struct tpg_data *tpg, struct vivid_buffer *buf,
222 unsigned p, unsigned bpl[TPG_MAX_PLANES], unsigned h)
223{
224 unsigned i;
225 void *vbuf;
226
227 if (p == 0 || tpg_g_buffers(tpg) > 1)
Junghak Sung2d700712015-09-22 10:30:30 -0300228 return vb2_plane_vaddr(&buf->vb.vb2_buf, p);
229 vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
Hans Verkuilddcaee92015-03-09 12:03:52 -0300230 for (i = 0; i < p; i++)
231 vbuf += bpl[i] * h / tpg->vdownsampling[i];
232 return vbuf;
233}
234
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300235static int vivid_copy_buffer(struct vivid_dev *dev, unsigned p, u8 *vcapbuf,
236 struct vivid_buffer *vid_cap_buf)
237{
Junghak Sung2d700712015-09-22 10:30:30 -0300238 bool blank = dev->must_blank[vid_cap_buf->vb.vb2_buf.index];
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300239 struct tpg_data *tpg = &dev->tpg;
240 struct vivid_buffer *vid_out_buf = NULL;
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300241 unsigned vdiv = dev->fmt_out->vdownsampling[p];
242 unsigned twopixsize = tpg_g_twopixelsize(tpg, p);
243 unsigned img_width = tpg_hdiv(tpg, p, dev->compose_cap.width);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300244 unsigned img_height = dev->compose_cap.height;
245 unsigned stride_cap = tpg->bytesperline[p];
246 unsigned stride_out = dev->bytesperline_out[p];
247 unsigned stride_osd = dev->display_byte_stride;
248 unsigned hmax = (img_height * tpg->perc_fill) / 100;
249 u8 *voutbuf;
250 u8 *vosdbuf = NULL;
251 unsigned y;
252 bool blend = dev->bitmap_out || dev->clipcount_out || dev->fbuf_out_flags;
253 /* Coarse scaling with Bresenham */
254 unsigned vid_out_int_part;
255 unsigned vid_out_fract_part;
256 unsigned vid_out_y = 0;
257 unsigned vid_out_error = 0;
258 unsigned vid_overlay_int_part = 0;
259 unsigned vid_overlay_fract_part = 0;
260 unsigned vid_overlay_y = 0;
261 unsigned vid_overlay_error = 0;
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300262 unsigned vid_cap_left = tpg_hdiv(tpg, p, dev->loop_vid_cap.left);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300263 unsigned vid_cap_right;
264 bool quick;
265
266 vid_out_int_part = dev->loop_vid_out.height / dev->loop_vid_cap.height;
267 vid_out_fract_part = dev->loop_vid_out.height % dev->loop_vid_cap.height;
268
269 if (!list_empty(&dev->vid_out_active))
270 vid_out_buf = list_entry(dev->vid_out_active.next,
271 struct vivid_buffer, list);
272 if (vid_out_buf == NULL)
273 return -ENODATA;
274
Junghak Sung2d700712015-09-22 10:30:30 -0300275 vid_cap_buf->vb.field = vid_out_buf->vb.field;
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300276
Hans Verkuilddcaee92015-03-09 12:03:52 -0300277 voutbuf = plane_vaddr(tpg, vid_out_buf, p,
278 dev->bytesperline_out, dev->fmt_out_rect.height);
279 if (p < dev->fmt_out->buffers)
Junghak Sung2d700712015-09-22 10:30:30 -0300280 voutbuf += vid_out_buf->vb.vb2_buf.planes[p].data_offset;
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300281 voutbuf += tpg_hdiv(tpg, p, dev->loop_vid_out.left) +
282 (dev->loop_vid_out.top / vdiv) * stride_out;
283 vcapbuf += tpg_hdiv(tpg, p, dev->compose_cap.left) +
284 (dev->compose_cap.top / vdiv) * stride_cap;
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300285
286 if (dev->loop_vid_copy.width == 0 || dev->loop_vid_copy.height == 0) {
287 /*
288 * If there is nothing to copy, then just fill the capture window
289 * with black.
290 */
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300291 for (y = 0; y < hmax / vdiv; y++, vcapbuf += stride_cap)
292 memcpy(vcapbuf, tpg->black_line[p], img_width);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300293 return 0;
294 }
295
296 if (dev->overlay_out_enabled &&
297 dev->loop_vid_overlay.width && dev->loop_vid_overlay.height) {
298 vosdbuf = dev->video_vbase;
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300299 vosdbuf += (dev->loop_fb_copy.left * twopixsize) / 2 +
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300300 dev->loop_fb_copy.top * stride_osd;
301 vid_overlay_int_part = dev->loop_vid_overlay.height /
302 dev->loop_vid_overlay_cap.height;
303 vid_overlay_fract_part = dev->loop_vid_overlay.height %
304 dev->loop_vid_overlay_cap.height;
305 }
306
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300307 vid_cap_right = tpg_hdiv(tpg, p, dev->loop_vid_cap.left + dev->loop_vid_cap.width);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300308 /* quick is true if no video scaling is needed */
309 quick = dev->loop_vid_out.width == dev->loop_vid_cap.width;
310
311 dev->cur_scaled_line = dev->loop_vid_out.height;
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300312 for (y = 0; y < hmax; y += vdiv, vcapbuf += stride_cap) {
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300313 /* osdline is true if this line requires overlay blending */
314 bool osdline = vosdbuf && y >= dev->loop_vid_overlay_cap.top &&
315 y < dev->loop_vid_overlay_cap.top + dev->loop_vid_overlay_cap.height;
316
317 /*
318 * If this line of the capture buffer doesn't get any video, then
319 * just fill with black.
320 */
321 if (y < dev->loop_vid_cap.top ||
322 y >= dev->loop_vid_cap.top + dev->loop_vid_cap.height) {
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300323 memcpy(vcapbuf, tpg->black_line[p], img_width);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300324 continue;
325 }
326
327 /* fill the left border with black */
328 if (dev->loop_vid_cap.left)
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300329 memcpy(vcapbuf, tpg->black_line[p], vid_cap_left);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300330
331 /* fill the right border with black */
332 if (vid_cap_right < img_width)
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300333 memcpy(vcapbuf + vid_cap_right, tpg->black_line[p],
334 img_width - vid_cap_right);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300335
336 if (quick && !osdline) {
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300337 memcpy(vcapbuf + vid_cap_left,
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300338 voutbuf + vid_out_y * stride_out,
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300339 tpg_hdiv(tpg, p, dev->loop_vid_cap.width));
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300340 goto update_vid_out_y;
341 }
342 if (dev->cur_scaled_line == vid_out_y) {
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300343 memcpy(vcapbuf + vid_cap_left, dev->scaled_line,
344 tpg_hdiv(tpg, p, dev->loop_vid_cap.width));
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300345 goto update_vid_out_y;
346 }
347 if (!osdline) {
348 scale_line(voutbuf + vid_out_y * stride_out, dev->scaled_line,
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300349 tpg_hdiv(tpg, p, dev->loop_vid_out.width),
350 tpg_hdiv(tpg, p, dev->loop_vid_cap.width),
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300351 tpg_g_twopixelsize(tpg, p));
352 } else {
353 /*
354 * Offset in bytes within loop_vid_copy to the start of the
355 * loop_vid_overlay rectangle.
356 */
357 unsigned offset =
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300358 ((dev->loop_vid_overlay.left - dev->loop_vid_copy.left) *
359 twopixsize) / 2;
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300360 u8 *osd = vosdbuf + vid_overlay_y * stride_osd;
361
362 scale_line(voutbuf + vid_out_y * stride_out, dev->blended_line,
363 dev->loop_vid_out.width, dev->loop_vid_copy.width,
364 tpg_g_twopixelsize(tpg, p));
365 if (blend)
366 blend_line(dev, vid_overlay_y + dev->loop_vid_overlay.top,
367 dev->loop_vid_overlay.left,
368 dev->blended_line + offset, osd,
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300369 dev->loop_vid_overlay.width, twopixsize / 2);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300370 else
371 memcpy(dev->blended_line + offset,
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300372 osd, (dev->loop_vid_overlay.width * twopixsize) / 2);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300373 scale_line(dev->blended_line, dev->scaled_line,
374 dev->loop_vid_copy.width, dev->loop_vid_cap.width,
375 tpg_g_twopixelsize(tpg, p));
376 }
377 dev->cur_scaled_line = vid_out_y;
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300378 memcpy(vcapbuf + vid_cap_left, dev->scaled_line,
379 tpg_hdiv(tpg, p, dev->loop_vid_cap.width));
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300380
381update_vid_out_y:
382 if (osdline) {
383 vid_overlay_y += vid_overlay_int_part;
384 vid_overlay_error += vid_overlay_fract_part;
385 if (vid_overlay_error >= dev->loop_vid_overlay_cap.height) {
386 vid_overlay_error -= dev->loop_vid_overlay_cap.height;
387 vid_overlay_y++;
388 }
389 }
390 vid_out_y += vid_out_int_part;
391 vid_out_error += vid_out_fract_part;
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300392 if (vid_out_error >= dev->loop_vid_cap.height / vdiv) {
393 vid_out_error -= dev->loop_vid_cap.height / vdiv;
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300394 vid_out_y++;
395 }
396 }
397
398 if (!blank)
399 return 0;
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300400 for (; y < img_height; y += vdiv, vcapbuf += stride_cap)
401 memcpy(vcapbuf, tpg->contrast_line[p], img_width);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300402 return 0;
403}
404
405static void vivid_fillbuff(struct vivid_dev *dev, struct vivid_buffer *buf)
406{
Hans Verkuilddcaee92015-03-09 12:03:52 -0300407 struct tpg_data *tpg = &dev->tpg;
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300408 unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1;
409 unsigned line_height = 16 / factor;
410 bool is_tv = vivid_is_sdtv_cap(dev);
411 bool is_60hz = is_tv && (dev->std_cap & V4L2_STD_525_60);
412 unsigned p;
413 int line = 1;
414 u8 *basep[TPG_MAX_PLANES][2];
415 unsigned ms;
416 char str[100];
417 s32 gain;
418 bool is_loop = false;
419
420 if (dev->loop_video && dev->can_loop_video &&
Junghak Sung2d700712015-09-22 10:30:30 -0300421 ((vivid_is_svid_cap(dev) &&
422 !VIVID_INVALID_SIGNAL(dev->std_signal_mode)) ||
423 (vivid_is_hdmi_cap(dev) &&
424 !VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode))))
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300425 is_loop = true;
426
Junghak Sung2d700712015-09-22 10:30:30 -0300427 buf->vb.sequence = dev->vid_cap_seq_count;
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300428 if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
429 /*
430 * 60 Hz standards start with the bottom field, 50 Hz standards
431 * with the top field. So if the 0-based seq_count is even,
432 * then the field is TOP for 50 Hz and BOTTOM for 60 Hz
433 * standards.
434 */
Junghak Sung2d700712015-09-22 10:30:30 -0300435 buf->vb.field = ((dev->vid_cap_seq_count & 1) ^ is_60hz) ?
Hans Verkuila94e7d62015-03-06 11:24:04 -0300436 V4L2_FIELD_BOTTOM : V4L2_FIELD_TOP;
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300437 /*
438 * The sequence counter counts frames, not fields. So divide
439 * by two.
440 */
Junghak Sung2d700712015-09-22 10:30:30 -0300441 buf->vb.sequence /= 2;
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300442 } else {
Junghak Sung2d700712015-09-22 10:30:30 -0300443 buf->vb.field = dev->field_cap;
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300444 }
Junghak Sung2d700712015-09-22 10:30:30 -0300445 tpg_s_field(tpg, buf->vb.field,
Hans Verkuil43047f62015-03-07 12:38:42 -0300446 dev->field_cap == V4L2_FIELD_ALTERNATE);
Junghak Sung2d700712015-09-22 10:30:30 -0300447 tpg_s_perc_fill_blank(tpg, dev->must_blank[buf->vb.vb2_buf.index]);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300448
449 vivid_precalc_copy_rects(dev);
450
Hans Verkuilddcaee92015-03-09 12:03:52 -0300451 for (p = 0; p < tpg_g_planes(tpg); p++) {
452 void *vbuf = plane_vaddr(tpg, buf, p,
453 tpg->bytesperline, tpg->buf_height);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300454
455 /*
456 * The first plane of a multiplanar format has a non-zero
457 * data_offset. This helps testing whether the application
458 * correctly supports non-zero data offsets.
459 */
Hans Verkuilddcaee92015-03-09 12:03:52 -0300460 if (p < tpg_g_buffers(tpg) && dev->fmt_cap->data_offset[p]) {
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300461 memset(vbuf, dev->fmt_cap->data_offset[p] & 0xff,
462 dev->fmt_cap->data_offset[p]);
463 vbuf += dev->fmt_cap->data_offset[p];
464 }
Hans Verkuilddcaee92015-03-09 12:03:52 -0300465 tpg_calc_text_basep(tpg, basep, p, vbuf);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300466 if (!is_loop || vivid_copy_buffer(dev, p, vbuf, buf))
Junghak Sung2d700712015-09-22 10:30:30 -0300467 tpg_fill_plane_buffer(tpg, vivid_get_std_cap(dev),
468 p, vbuf);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300469 }
Junghak Sung2d700712015-09-22 10:30:30 -0300470 dev->must_blank[buf->vb.vb2_buf.index] = false;
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300471
472 /* Updates stream time, only update at the start of a new frame. */
Junghak Sung2d700712015-09-22 10:30:30 -0300473 if (dev->field_cap != V4L2_FIELD_ALTERNATE ||
Nicolas Dufresne43bd1dc2018-07-20 10:09:16 -0400474 (dev->vid_cap_seq_count & 1) == 0)
Junghak Sung2d700712015-09-22 10:30:30 -0300475 dev->ms_vid_cap =
476 jiffies_to_msecs(jiffies - dev->jiffies_vid_cap);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300477
478 ms = dev->ms_vid_cap;
479 if (dev->osd_mode <= 1) {
480 snprintf(str, sizeof(str), " %02d:%02d:%02d:%03d %u%s",
481 (ms / (60 * 60 * 1000)) % 24,
482 (ms / (60 * 1000)) % 60,
483 (ms / 1000) % 60,
484 ms % 1000,
Junghak Sung2d700712015-09-22 10:30:30 -0300485 buf->vb.sequence,
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300486 (dev->field_cap == V4L2_FIELD_ALTERNATE) ?
Junghak Sung2d700712015-09-22 10:30:30 -0300487 (buf->vb.field == V4L2_FIELD_TOP ?
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300488 " top" : " bottom") : "");
Hans Verkuilddcaee92015-03-09 12:03:52 -0300489 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300490 }
491 if (dev->osd_mode == 0) {
492 snprintf(str, sizeof(str), " %dx%d, input %d ",
493 dev->src_rect.width, dev->src_rect.height, dev->input);
Hans Verkuilddcaee92015-03-09 12:03:52 -0300494 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300495
496 gain = v4l2_ctrl_g_ctrl(dev->gain);
497 mutex_lock(dev->ctrl_hdl_user_vid.lock);
498 snprintf(str, sizeof(str),
499 " brightness %3d, contrast %3d, saturation %3d, hue %d ",
500 dev->brightness->cur.val,
501 dev->contrast->cur.val,
502 dev->saturation->cur.val,
503 dev->hue->cur.val);
Hans Verkuilddcaee92015-03-09 12:03:52 -0300504 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300505 snprintf(str, sizeof(str),
506 " autogain %d, gain %3d, alpha 0x%02x ",
507 dev->autogain->cur.val, gain, dev->alpha->cur.val);
508 mutex_unlock(dev->ctrl_hdl_user_vid.lock);
Hans Verkuilddcaee92015-03-09 12:03:52 -0300509 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300510 mutex_lock(dev->ctrl_hdl_user_aud.lock);
511 snprintf(str, sizeof(str),
512 " volume %3d, mute %d ",
513 dev->volume->cur.val, dev->mute->cur.val);
514 mutex_unlock(dev->ctrl_hdl_user_aud.lock);
Hans Verkuilddcaee92015-03-09 12:03:52 -0300515 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300516 mutex_lock(dev->ctrl_hdl_user_gen.lock);
517 snprintf(str, sizeof(str), " int32 %d, int64 %lld, bitmask %08x ",
518 dev->int32->cur.val,
519 *dev->int64->p_cur.p_s64,
520 dev->bitmask->cur.val);
Hans Verkuilddcaee92015-03-09 12:03:52 -0300521 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300522 snprintf(str, sizeof(str), " boolean %d, menu %s, string \"%s\" ",
523 dev->boolean->cur.val,
524 dev->menu->qmenu[dev->menu->cur.val],
525 dev->string->p_cur.p_char);
Hans Verkuilddcaee92015-03-09 12:03:52 -0300526 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300527 snprintf(str, sizeof(str), " integer_menu %lld, value %d ",
528 dev->int_menu->qmenu_int[dev->int_menu->cur.val],
529 dev->int_menu->cur.val);
530 mutex_unlock(dev->ctrl_hdl_user_gen.lock);
Hans Verkuilddcaee92015-03-09 12:03:52 -0300531 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300532 if (dev->button_pressed) {
533 dev->button_pressed--;
534 snprintf(str, sizeof(str), " button pressed!");
Hans Verkuilddcaee92015-03-09 12:03:52 -0300535 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300536 }
Hans Verkuil6f8adea2016-02-05 12:44:21 -0200537 if (dev->osd[0]) {
538 if (vivid_is_hdmi_cap(dev)) {
539 snprintf(str, sizeof(str),
540 " OSD \"%s\"", dev->osd);
541 tpg_gen_text(tpg, basep, line++ * line_height,
542 16, str);
543 }
544 if (dev->osd_jiffies &&
545 time_is_before_jiffies(dev->osd_jiffies + 5 * HZ)) {
546 dev->osd[0] = 0;
547 dev->osd_jiffies = 0;
548 }
549 }
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300550 }
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300551}
552
553/*
554 * Return true if this pixel coordinate is a valid video pixel.
555 */
556static bool valid_pix(struct vivid_dev *dev, int win_y, int win_x, int fb_y, int fb_x)
557{
558 int i;
559
560 if (dev->bitmap_cap) {
561 /*
562 * Only if the corresponding bit in the bitmap is set can
563 * the video pixel be shown. Coordinates are relative to
564 * the overlay window set by VIDIOC_S_FMT.
565 */
566 const u8 *p = dev->bitmap_cap;
567 unsigned stride = (dev->compose_cap.width + 7) / 8;
568
569 if (!(p[stride * win_y + win_x / 8] & (1 << (win_x & 7))))
570 return false;
571 }
572
573 for (i = 0; i < dev->clipcount_cap; i++) {
574 /*
575 * Only if the framebuffer coordinate is not in any of the
576 * clip rectangles will be video pixel be shown.
577 */
578 struct v4l2_rect *r = &dev->clips_cap[i].c;
579
580 if (fb_y >= r->top && fb_y < r->top + r->height &&
581 fb_x >= r->left && fb_x < r->left + r->width)
582 return false;
583 }
584 return true;
585}
586
587/*
588 * Draw the image into the overlay buffer.
589 * Note that the combination of overlay and multiplanar is not supported.
590 */
591static void vivid_overlay(struct vivid_dev *dev, struct vivid_buffer *buf)
592{
593 struct tpg_data *tpg = &dev->tpg;
594 unsigned pixsize = tpg_g_twopixelsize(tpg, 0) / 2;
595 void *vbase = dev->fb_vbase_cap;
Junghak Sung2d700712015-09-22 10:30:30 -0300596 void *vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300597 unsigned img_width = dev->compose_cap.width;
598 unsigned img_height = dev->compose_cap.height;
599 unsigned stride = tpg->bytesperline[0];
600 /* if quick is true, then valid_pix() doesn't have to be called */
601 bool quick = dev->bitmap_cap == NULL && dev->clipcount_cap == 0;
602 int x, y, w, out_x = 0;
603
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300604 /*
605 * Overlay support is only supported for formats that have a twopixelsize
606 * that's >= 2. Warn and bail out if that's not the case.
607 */
608 if (WARN_ON(pixsize == 0))
609 return;
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300610 if ((dev->overlay_cap_field == V4L2_FIELD_TOP ||
611 dev->overlay_cap_field == V4L2_FIELD_BOTTOM) &&
Junghak Sung2d700712015-09-22 10:30:30 -0300612 dev->overlay_cap_field != buf->vb.field)
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300613 return;
614
615 vbuf += dev->compose_cap.left * pixsize + dev->compose_cap.top * stride;
616 x = dev->overlay_cap_left;
617 w = img_width;
618 if (x < 0) {
619 out_x = -x;
620 w = w - out_x;
621 x = 0;
622 } else {
623 w = dev->fb_cap.fmt.width - x;
624 if (w > img_width)
625 w = img_width;
626 }
627 if (w <= 0)
628 return;
629 if (dev->overlay_cap_top >= 0)
630 vbase += dev->overlay_cap_top * dev->fb_cap.fmt.bytesperline;
631 for (y = dev->overlay_cap_top;
632 y < dev->overlay_cap_top + (int)img_height;
633 y++, vbuf += stride) {
634 int px;
635
636 if (y < 0 || y > dev->fb_cap.fmt.height)
637 continue;
638 if (quick) {
639 memcpy(vbase + x * pixsize,
640 vbuf + out_x * pixsize, w * pixsize);
641 vbase += dev->fb_cap.fmt.bytesperline;
642 continue;
643 }
644 for (px = 0; px < w; px++) {
645 if (!valid_pix(dev, y - dev->overlay_cap_top,
646 px + out_x, y, px + x))
647 continue;
648 memcpy(vbase + (px + x) * pixsize,
649 vbuf + (px + out_x) * pixsize,
650 pixsize);
651 }
652 vbase += dev->fb_cap.fmt.bytesperline;
653 }
654}
655
Gabriel Francisco Mandajibe7e3c32018-12-02 08:45:38 -0500656static void vivid_cap_update_frame_period(struct vivid_dev *dev)
657{
658 u64 f_period;
659
660 f_period = (u64)dev->timeperframe_vid_cap.numerator * 1000000000;
661 do_div(f_period, dev->timeperframe_vid_cap.denominator);
662 if (dev->field_cap == V4L2_FIELD_ALTERNATE)
663 f_period >>= 1;
664 /*
665 * If "End of Frame", then offset the exposure time by 0.9
666 * of the frame period.
667 */
668 dev->cap_frame_eof_offset = f_period * 9;
669 do_div(dev->cap_frame_eof_offset, 10);
670 dev->cap_frame_period = f_period;
671}
672
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300673static void vivid_thread_vid_cap_tick(struct vivid_dev *dev, int dropped_bufs)
674{
675 struct vivid_buffer *vid_cap_buf = NULL;
676 struct vivid_buffer *vbi_cap_buf = NULL;
Gabriel Francisco Mandajibe7e3c32018-12-02 08:45:38 -0500677 u64 f_time = 0;
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300678
679 dprintk(dev, 1, "Video Capture Thread Tick\n");
680
681 while (dropped_bufs-- > 1)
682 tpg_update_mv_count(&dev->tpg,
683 dev->field_cap == V4L2_FIELD_NONE ||
684 dev->field_cap == V4L2_FIELD_ALTERNATE);
685
686 /* Drop a certain percentage of buffers. */
687 if (dev->perc_dropped_buffers &&
688 prandom_u32_max(100) < dev->perc_dropped_buffers)
689 goto update_mv;
690
691 spin_lock(&dev->slock);
692 if (!list_empty(&dev->vid_cap_active)) {
693 vid_cap_buf = list_entry(dev->vid_cap_active.next, struct vivid_buffer, list);
694 list_del(&vid_cap_buf->list);
695 }
696 if (!list_empty(&dev->vbi_cap_active)) {
697 if (dev->field_cap != V4L2_FIELD_ALTERNATE ||
698 (dev->vbi_cap_seq_count & 1)) {
699 vbi_cap_buf = list_entry(dev->vbi_cap_active.next,
700 struct vivid_buffer, list);
701 list_del(&vbi_cap_buf->list);
702 }
703 }
704 spin_unlock(&dev->slock);
705
706 if (!vid_cap_buf && !vbi_cap_buf)
707 goto update_mv;
708
Gabriel Francisco Mandajibe7e3c32018-12-02 08:45:38 -0500709 f_time = dev->cap_frame_period * dev->vid_cap_seq_count +
710 dev->cap_stream_start + dev->time_wrap_offset;
711 if (!dev->tstamp_src_is_soe)
712 f_time += dev->cap_frame_eof_offset;
713
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300714 if (vid_cap_buf) {
Hans Verkuil757fdb52018-05-21 04:54:59 -0400715 v4l2_ctrl_request_setup(vid_cap_buf->vb.vb2_buf.req_obj.req,
716 &dev->ctrl_hdl_vid_cap);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300717 /* Fill buffer */
718 vivid_fillbuff(dev, vid_cap_buf);
719 dprintk(dev, 1, "filled buffer %d\n",
Junghak Sung2d700712015-09-22 10:30:30 -0300720 vid_cap_buf->vb.vb2_buf.index);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300721
722 /* Handle overlay */
723 if (dev->overlay_cap_owner && dev->fb_cap.base &&
Junghak Sung2d700712015-09-22 10:30:30 -0300724 dev->fb_cap.fmt.pixelformat == dev->fmt_cap->fourcc)
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300725 vivid_overlay(dev, vid_cap_buf);
726
Hans Verkuil757fdb52018-05-21 04:54:59 -0400727 v4l2_ctrl_request_complete(vid_cap_buf->vb.vb2_buf.req_obj.req,
728 &dev->ctrl_hdl_vid_cap);
Junghak Sung2d700712015-09-22 10:30:30 -0300729 vb2_buffer_done(&vid_cap_buf->vb.vb2_buf, dev->dqbuf_error ?
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300730 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
731 dprintk(dev, 2, "vid_cap buffer %d done\n",
Junghak Sung2d700712015-09-22 10:30:30 -0300732 vid_cap_buf->vb.vb2_buf.index);
Gabriel Francisco Mandajibe7e3c32018-12-02 08:45:38 -0500733
734 vid_cap_buf->vb.vb2_buf.timestamp = f_time;
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300735 }
736
737 if (vbi_cap_buf) {
Gabriel Francisco Mandajibe7e3c32018-12-02 08:45:38 -0500738 u64 vbi_period;
739
Hans Verkuil757fdb52018-05-21 04:54:59 -0400740 v4l2_ctrl_request_setup(vbi_cap_buf->vb.vb2_buf.req_obj.req,
741 &dev->ctrl_hdl_vbi_cap);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300742 if (dev->stream_sliced_vbi_cap)
743 vivid_sliced_vbi_cap_process(dev, vbi_cap_buf);
744 else
745 vivid_raw_vbi_cap_process(dev, vbi_cap_buf);
Hans Verkuil757fdb52018-05-21 04:54:59 -0400746 v4l2_ctrl_request_complete(vbi_cap_buf->vb.vb2_buf.req_obj.req,
747 &dev->ctrl_hdl_vbi_cap);
Junghak Sung2d700712015-09-22 10:30:30 -0300748 vb2_buffer_done(&vbi_cap_buf->vb.vb2_buf, dev->dqbuf_error ?
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300749 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
750 dprintk(dev, 2, "vbi_cap %d done\n",
Junghak Sung2d700712015-09-22 10:30:30 -0300751 vbi_cap_buf->vb.vb2_buf.index);
Gabriel Francisco Mandajibe7e3c32018-12-02 08:45:38 -0500752
753 /* If capturing a VBI, offset by 0.05 */
754 vbi_period = dev->cap_frame_period * 5;
755 do_div(vbi_period, 100);
756 vbi_cap_buf->vb.vb2_buf.timestamp = f_time + vbi_period;
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300757 }
758 dev->dqbuf_error = false;
759
760update_mv:
761 /* Update the test pattern movement counters */
762 tpg_update_mv_count(&dev->tpg, dev->field_cap == V4L2_FIELD_NONE ||
763 dev->field_cap == V4L2_FIELD_ALTERNATE);
764}
765
766static int vivid_thread_vid_cap(void *data)
767{
768 struct vivid_dev *dev = data;
769 u64 numerators_since_start;
770 u64 buffers_since_start;
771 u64 next_jiffies_since_start;
772 unsigned long jiffies_since_start;
773 unsigned long cur_jiffies;
774 unsigned wait_jiffies;
775 unsigned numerator;
776 unsigned denominator;
777 int dropped_bufs;
778
779 dprintk(dev, 1, "Video Capture Thread Start\n");
780
781 set_freezable();
782
783 /* Resets frame counters */
784 dev->cap_seq_offset = 0;
785 dev->cap_seq_count = 0;
786 dev->cap_seq_resync = false;
787 dev->jiffies_vid_cap = jiffies;
Gabriel Francisco Mandajibe7e3c32018-12-02 08:45:38 -0500788 dev->cap_stream_start = ktime_get_ns();
789 vivid_cap_update_frame_period(dev);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300790
791 for (;;) {
792 try_to_freeze();
793 if (kthread_should_stop())
794 break;
795
796 mutex_lock(&dev->mutex);
797 cur_jiffies = jiffies;
798 if (dev->cap_seq_resync) {
799 dev->jiffies_vid_cap = cur_jiffies;
800 dev->cap_seq_offset = dev->cap_seq_count + 1;
801 dev->cap_seq_count = 0;
Gabriel Francisco Mandajibe7e3c32018-12-02 08:45:38 -0500802 dev->cap_stream_start += dev->cap_frame_period *
803 dev->cap_seq_offset;
804 vivid_cap_update_frame_period(dev);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300805 dev->cap_seq_resync = false;
806 }
807 numerator = dev->timeperframe_vid_cap.numerator;
808 denominator = dev->timeperframe_vid_cap.denominator;
809
810 if (dev->field_cap == V4L2_FIELD_ALTERNATE)
811 denominator *= 2;
812
813 /* Calculate the number of jiffies since we started streaming */
814 jiffies_since_start = cur_jiffies - dev->jiffies_vid_cap;
815 /* Get the number of buffers streamed since the start */
816 buffers_since_start = (u64)jiffies_since_start * denominator +
817 (HZ * numerator) / 2;
818 do_div(buffers_since_start, HZ * numerator);
819
820 /*
821 * After more than 0xf0000000 (rounded down to a multiple of
822 * 'jiffies-per-day' to ease jiffies_to_msecs calculation)
823 * jiffies have passed since we started streaming reset the
824 * counters and keep track of the sequence offset.
825 */
826 if (jiffies_since_start > JIFFIES_RESYNC) {
827 dev->jiffies_vid_cap = cur_jiffies;
828 dev->cap_seq_offset = buffers_since_start;
829 buffers_since_start = 0;
830 }
831 dropped_bufs = buffers_since_start + dev->cap_seq_offset - dev->cap_seq_count;
832 dev->cap_seq_count = buffers_since_start + dev->cap_seq_offset;
833 dev->vid_cap_seq_count = dev->cap_seq_count - dev->vid_cap_seq_start;
834 dev->vbi_cap_seq_count = dev->cap_seq_count - dev->vbi_cap_seq_start;
835
836 vivid_thread_vid_cap_tick(dev, dropped_bufs);
837
838 /*
839 * Calculate the number of 'numerators' streamed since we started,
840 * including the current buffer.
841 */
842 numerators_since_start = ++buffers_since_start * numerator;
843
844 /* And the number of jiffies since we started */
845 jiffies_since_start = jiffies - dev->jiffies_vid_cap;
846
847 mutex_unlock(&dev->mutex);
848
849 /*
850 * Calculate when that next buffer is supposed to start
851 * in jiffies since we started streaming.
852 */
853 next_jiffies_since_start = numerators_since_start * HZ +
854 denominator / 2;
855 do_div(next_jiffies_since_start, denominator);
856 /* If it is in the past, then just schedule asap */
857 if (next_jiffies_since_start < jiffies_since_start)
858 next_jiffies_since_start = jiffies_since_start;
859
860 wait_jiffies = next_jiffies_since_start - jiffies_since_start;
861 schedule_timeout_interruptible(wait_jiffies ? wait_jiffies : 1);
862 }
863 dprintk(dev, 1, "Video Capture Thread End\n");
864 return 0;
865}
866
867static void vivid_grab_controls(struct vivid_dev *dev, bool grab)
868{
869 v4l2_ctrl_grab(dev->ctrl_has_crop_cap, grab);
870 v4l2_ctrl_grab(dev->ctrl_has_compose_cap, grab);
871 v4l2_ctrl_grab(dev->ctrl_has_scaler_cap, grab);
872}
873
874int vivid_start_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
875{
876 dprintk(dev, 1, "%s\n", __func__);
877
878 if (dev->kthread_vid_cap) {
879 u32 seq_count = dev->cap_seq_count + dev->seq_wrap * 128;
880
881 if (pstreaming == &dev->vid_cap_streaming)
882 dev->vid_cap_seq_start = seq_count;
883 else
884 dev->vbi_cap_seq_start = seq_count;
885 *pstreaming = true;
886 return 0;
887 }
888
889 /* Resets frame counters */
890 tpg_init_mv_count(&dev->tpg);
891
892 dev->vid_cap_seq_start = dev->seq_wrap * 128;
893 dev->vbi_cap_seq_start = dev->seq_wrap * 128;
894
895 dev->kthread_vid_cap = kthread_run(vivid_thread_vid_cap, dev,
896 "%s-vid-cap", dev->v4l2_dev.name);
897
898 if (IS_ERR(dev->kthread_vid_cap)) {
Hans Verkuil701f49b2018-10-29 06:15:31 -0400899 int err = PTR_ERR(dev->kthread_vid_cap);
900
901 dev->kthread_vid_cap = NULL;
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300902 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
Hans Verkuil701f49b2018-10-29 06:15:31 -0400903 return err;
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300904 }
905 *pstreaming = true;
906 vivid_grab_controls(dev, true);
907
908 dprintk(dev, 1, "returning from %s\n", __func__);
909 return 0;
910}
911
912void vivid_stop_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
913{
914 dprintk(dev, 1, "%s\n", __func__);
915
916 if (dev->kthread_vid_cap == NULL)
917 return;
918
919 *pstreaming = false;
920 if (pstreaming == &dev->vid_cap_streaming) {
921 /* Release all active buffers */
922 while (!list_empty(&dev->vid_cap_active)) {
923 struct vivid_buffer *buf;
924
925 buf = list_entry(dev->vid_cap_active.next,
926 struct vivid_buffer, list);
927 list_del(&buf->list);
Hans Verkuil757fdb52018-05-21 04:54:59 -0400928 v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
929 &dev->ctrl_hdl_vid_cap);
Junghak Sung2d700712015-09-22 10:30:30 -0300930 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300931 dprintk(dev, 2, "vid_cap buffer %d done\n",
Junghak Sung2d700712015-09-22 10:30:30 -0300932 buf->vb.vb2_buf.index);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300933 }
934 }
935
936 if (pstreaming == &dev->vbi_cap_streaming) {
937 while (!list_empty(&dev->vbi_cap_active)) {
938 struct vivid_buffer *buf;
939
940 buf = list_entry(dev->vbi_cap_active.next,
941 struct vivid_buffer, list);
942 list_del(&buf->list);
Hans Verkuil757fdb52018-05-21 04:54:59 -0400943 v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
944 &dev->ctrl_hdl_vbi_cap);
Junghak Sung2d700712015-09-22 10:30:30 -0300945 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300946 dprintk(dev, 2, "vbi_cap buffer %d done\n",
Junghak Sung2d700712015-09-22 10:30:30 -0300947 buf->vb.vb2_buf.index);
Hans Verkuil3f682ff2014-08-25 08:00:20 -0300948 }
949 }
950
951 if (dev->vid_cap_streaming || dev->vbi_cap_streaming)
952 return;
953
954 /* shutdown control thread */
955 vivid_grab_controls(dev, false);
956 mutex_unlock(&dev->mutex);
957 kthread_stop(dev->kthread_vid_cap);
958 dev->kthread_vid_cap = NULL;
959 mutex_lock(&dev->mutex);
960}