1
2
3
4
5
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
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
114 unsigned int_part;
115 unsigned fract_part;
116 unsigned src_x = 0;
117 unsigned error = 0;
118 unsigned x;
119
120
121
122
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164static void vivid_precalc_copy_rects(struct vivid_dev *dev)
165{
166
167 struct v4l2_rect r_fb = {
168 0, 0, dev->display_width, dev->display_height
169 };
170
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
176 v4l2_rect_intersect(&dev->loop_vid_copy, &dev->crop_cap, &dev->compose_out);
177
178 dev->loop_vid_out = dev->loop_vid_copy;
179 v4l2_rect_scale(&dev->loop_vid_out, &dev->compose_out, &dev->crop_out);
180 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;
184 v4l2_rect_scale(&dev->loop_vid_cap, &dev->crop_cap, &dev->compose_cap);
185
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
195 v4l2_rect_intersect(&r_overlay, &r_fb, &r_overlay);
196
197
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
201 v4l2_rect_intersect(&dev->loop_vid_overlay, &r_overlay, &dev->loop_vid_copy);
202 dev->loop_fb_copy = dev->loop_vid_overlay;
203
204
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;
209 v4l2_rect_scale(&dev->loop_vid_overlay_cap, &dev->crop_cap, &dev->compose_cap);
210
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
221static 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)
228 return vb2_plane_vaddr(&buf->vb.vb2_buf, p);
229 vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
230 for (i = 0; i < p; i++)
231 vbuf += bpl[i] * h / tpg->vdownsampling[i];
232 return vbuf;
233}
234
235static int vivid_copy_buffer(struct vivid_dev *dev, unsigned p, u8 *vcapbuf,
236 struct vivid_buffer *vid_cap_buf)
237{
238 bool blank = dev->must_blank[vid_cap_buf->vb.vb2_buf.index];
239 struct tpg_data *tpg = &dev->tpg;
240 struct vivid_buffer *vid_out_buf = NULL;
241 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);
244 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
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;
262 unsigned vid_cap_left = tpg_hdiv(tpg, p, dev->loop_vid_cap.left);
263 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
275 vid_cap_buf->vb.field = vid_out_buf->vb.field;
276
277 voutbuf = plane_vaddr(tpg, vid_out_buf, p,
278 dev->bytesperline_out, dev->fmt_out_rect.height);
279 if (p < dev->fmt_out->buffers)
280 voutbuf += vid_out_buf->vb.vb2_buf.planes[p].data_offset;
281 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;
285
286 if (dev->loop_vid_copy.width == 0 || dev->loop_vid_copy.height == 0) {
287
288
289
290
291 for (y = 0; y < hmax / vdiv; y++, vcapbuf += stride_cap)
292 memcpy(vcapbuf, tpg->black_line[p], img_width);
293 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;
299 vosdbuf += (dev->loop_fb_copy.left * twopixsize) / 2 +
300 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
307 vid_cap_right = tpg_hdiv(tpg, p, dev->loop_vid_cap.left + dev->loop_vid_cap.width);
308
309 quick = dev->loop_vid_out.width == dev->loop_vid_cap.width;
310
311 dev->cur_scaled_line = dev->loop_vid_out.height;
312 for (y = 0; y < hmax; y += vdiv, vcapbuf += stride_cap) {
313
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
319
320
321 if (y < dev->loop_vid_cap.top ||
322 y >= dev->loop_vid_cap.top + dev->loop_vid_cap.height) {
323 memcpy(vcapbuf, tpg->black_line[p], img_width);
324 continue;
325 }
326
327
328 if (dev->loop_vid_cap.left)
329 memcpy(vcapbuf, tpg->black_line[p], vid_cap_left);
330
331
332 if (vid_cap_right < img_width)
333 memcpy(vcapbuf + vid_cap_right, tpg->black_line[p],
334 img_width - vid_cap_right);
335
336 if (quick && !osdline) {
337 memcpy(vcapbuf + vid_cap_left,
338 voutbuf + vid_out_y * stride_out,
339 tpg_hdiv(tpg, p, dev->loop_vid_cap.width));
340 goto update_vid_out_y;
341 }
342 if (dev->cur_scaled_line == vid_out_y) {
343 memcpy(vcapbuf + vid_cap_left, dev->scaled_line,
344 tpg_hdiv(tpg, p, dev->loop_vid_cap.width));
345 goto update_vid_out_y;
346 }
347 if (!osdline) {
348 scale_line(voutbuf + vid_out_y * stride_out, dev->scaled_line,
349 tpg_hdiv(tpg, p, dev->loop_vid_out.width),
350 tpg_hdiv(tpg, p, dev->loop_vid_cap.width),
351 tpg_g_twopixelsize(tpg, p));
352 } else {
353
354
355
356
357 unsigned offset =
358 ((dev->loop_vid_overlay.left - dev->loop_vid_copy.left) *
359 twopixsize) / 2;
360 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,
369 dev->loop_vid_overlay.width, twopixsize / 2);
370 else
371 memcpy(dev->blended_line + offset,
372 osd, (dev->loop_vid_overlay.width * twopixsize) / 2);
373 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;
378 memcpy(vcapbuf + vid_cap_left, dev->scaled_line,
379 tpg_hdiv(tpg, p, dev->loop_vid_cap.width));
380
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;
392 if (vid_out_error >= dev->loop_vid_cap.height / vdiv) {
393 vid_out_error -= dev->loop_vid_cap.height / vdiv;
394 vid_out_y++;
395 }
396 }
397
398 if (!blank)
399 return 0;
400 for (; y < img_height; y += vdiv, vcapbuf += stride_cap)
401 memcpy(vcapbuf, tpg->contrast_line[p], img_width);
402 return 0;
403}
404
405static void vivid_fillbuff(struct vivid_dev *dev, struct vivid_buffer *buf)
406{
407 struct tpg_data *tpg = &dev->tpg;
408 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 &&
421 ((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))))
425 is_loop = true;
426
427 buf->vb.sequence = dev->vid_cap_seq_count;
428
429
430
431
432 if (dev->tstamp_src_is_soe)
433 buf->vb.vb2_buf.timestamp = ktime_get_ns();
434 if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
435
436
437
438
439
440
441 buf->vb.field = ((dev->vid_cap_seq_count & 1) ^ is_60hz) ?
442 V4L2_FIELD_BOTTOM : V4L2_FIELD_TOP;
443
444
445
446
447 buf->vb.sequence /= 2;
448 } else {
449 buf->vb.field = dev->field_cap;
450 }
451 tpg_s_field(tpg, buf->vb.field,
452 dev->field_cap == V4L2_FIELD_ALTERNATE);
453 tpg_s_perc_fill_blank(tpg, dev->must_blank[buf->vb.vb2_buf.index]);
454
455 vivid_precalc_copy_rects(dev);
456
457 for (p = 0; p < tpg_g_planes(tpg); p++) {
458 void *vbuf = plane_vaddr(tpg, buf, p,
459 tpg->bytesperline, tpg->buf_height);
460
461
462
463
464
465
466 if (p < tpg_g_buffers(tpg) && dev->fmt_cap->data_offset[p]) {
467 memset(vbuf, dev->fmt_cap->data_offset[p] & 0xff,
468 dev->fmt_cap->data_offset[p]);
469 vbuf += dev->fmt_cap->data_offset[p];
470 }
471 tpg_calc_text_basep(tpg, basep, p, vbuf);
472 if (!is_loop || vivid_copy_buffer(dev, p, vbuf, buf))
473 tpg_fill_plane_buffer(tpg, vivid_get_std_cap(dev),
474 p, vbuf);
475 }
476 dev->must_blank[buf->vb.vb2_buf.index] = false;
477
478
479 if (dev->field_cap != V4L2_FIELD_ALTERNATE ||
480 (dev->vid_cap_seq_count & 1) == 0)
481 dev->ms_vid_cap =
482 jiffies_to_msecs(jiffies - dev->jiffies_vid_cap);
483
484 ms = dev->ms_vid_cap;
485 if (dev->osd_mode <= 1) {
486 snprintf(str, sizeof(str), " %02d:%02d:%02d:%03d %u%s",
487 (ms / (60 * 60 * 1000)) % 24,
488 (ms / (60 * 1000)) % 60,
489 (ms / 1000) % 60,
490 ms % 1000,
491 buf->vb.sequence,
492 (dev->field_cap == V4L2_FIELD_ALTERNATE) ?
493 (buf->vb.field == V4L2_FIELD_TOP ?
494 " top" : " bottom") : "");
495 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
496 }
497 if (dev->osd_mode == 0) {
498 snprintf(str, sizeof(str), " %dx%d, input %d ",
499 dev->src_rect.width, dev->src_rect.height, dev->input);
500 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
501
502 gain = v4l2_ctrl_g_ctrl(dev->gain);
503 mutex_lock(dev->ctrl_hdl_user_vid.lock);
504 snprintf(str, sizeof(str),
505 " brightness %3d, contrast %3d, saturation %3d, hue %d ",
506 dev->brightness->cur.val,
507 dev->contrast->cur.val,
508 dev->saturation->cur.val,
509 dev->hue->cur.val);
510 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
511 snprintf(str, sizeof(str),
512 " autogain %d, gain %3d, alpha 0x%02x ",
513 dev->autogain->cur.val, gain, dev->alpha->cur.val);
514 mutex_unlock(dev->ctrl_hdl_user_vid.lock);
515 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
516 mutex_lock(dev->ctrl_hdl_user_aud.lock);
517 snprintf(str, sizeof(str),
518 " volume %3d, mute %d ",
519 dev->volume->cur.val, dev->mute->cur.val);
520 mutex_unlock(dev->ctrl_hdl_user_aud.lock);
521 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
522 mutex_lock(dev->ctrl_hdl_user_gen.lock);
523 snprintf(str, sizeof(str), " int32 %d, int64 %lld, bitmask %08x ",
524 dev->int32->cur.val,
525 *dev->int64->p_cur.p_s64,
526 dev->bitmask->cur.val);
527 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
528 snprintf(str, sizeof(str), " boolean %d, menu %s, string \"%s\" ",
529 dev->boolean->cur.val,
530 dev->menu->qmenu[dev->menu->cur.val],
531 dev->string->p_cur.p_char);
532 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
533 snprintf(str, sizeof(str), " integer_menu %lld, value %d ",
534 dev->int_menu->qmenu_int[dev->int_menu->cur.val],
535 dev->int_menu->cur.val);
536 mutex_unlock(dev->ctrl_hdl_user_gen.lock);
537 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
538 if (dev->button_pressed) {
539 dev->button_pressed--;
540 snprintf(str, sizeof(str), " button pressed!");
541 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
542 }
543 if (dev->osd[0]) {
544 if (vivid_is_hdmi_cap(dev)) {
545 snprintf(str, sizeof(str),
546 " OSD \"%s\"", dev->osd);
547 tpg_gen_text(tpg, basep, line++ * line_height,
548 16, str);
549 }
550 if (dev->osd_jiffies &&
551 time_is_before_jiffies(dev->osd_jiffies + 5 * HZ)) {
552 dev->osd[0] = 0;
553 dev->osd_jiffies = 0;
554 }
555 }
556 }
557
558
559
560
561
562 if (!dev->tstamp_src_is_soe)
563 buf->vb.vb2_buf.timestamp = ktime_get_ns();
564 buf->vb.vb2_buf.timestamp += dev->time_wrap_offset;
565}
566
567
568
569
570static bool valid_pix(struct vivid_dev *dev, int win_y, int win_x, int fb_y, int fb_x)
571{
572 int i;
573
574 if (dev->bitmap_cap) {
575
576
577
578
579
580 const u8 *p = dev->bitmap_cap;
581 unsigned stride = (dev->compose_cap.width + 7) / 8;
582
583 if (!(p[stride * win_y + win_x / 8] & (1 << (win_x & 7))))
584 return false;
585 }
586
587 for (i = 0; i < dev->clipcount_cap; i++) {
588
589
590
591
592 struct v4l2_rect *r = &dev->clips_cap[i].c;
593
594 if (fb_y >= r->top && fb_y < r->top + r->height &&
595 fb_x >= r->left && fb_x < r->left + r->width)
596 return false;
597 }
598 return true;
599}
600
601
602
603
604
605static void vivid_overlay(struct vivid_dev *dev, struct vivid_buffer *buf)
606{
607 struct tpg_data *tpg = &dev->tpg;
608 unsigned pixsize = tpg_g_twopixelsize(tpg, 0) / 2;
609 void *vbase = dev->fb_vbase_cap;
610 void *vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
611 unsigned img_width = dev->compose_cap.width;
612 unsigned img_height = dev->compose_cap.height;
613 unsigned stride = tpg->bytesperline[0];
614
615 bool quick = dev->bitmap_cap == NULL && dev->clipcount_cap == 0;
616 int x, y, w, out_x = 0;
617
618
619
620
621
622 if (WARN_ON(pixsize == 0))
623 return;
624 if ((dev->overlay_cap_field == V4L2_FIELD_TOP ||
625 dev->overlay_cap_field == V4L2_FIELD_BOTTOM) &&
626 dev->overlay_cap_field != buf->vb.field)
627 return;
628
629 vbuf += dev->compose_cap.left * pixsize + dev->compose_cap.top * stride;
630 x = dev->overlay_cap_left;
631 w = img_width;
632 if (x < 0) {
633 out_x = -x;
634 w = w - out_x;
635 x = 0;
636 } else {
637 w = dev->fb_cap.fmt.width - x;
638 if (w > img_width)
639 w = img_width;
640 }
641 if (w <= 0)
642 return;
643 if (dev->overlay_cap_top >= 0)
644 vbase += dev->overlay_cap_top * dev->fb_cap.fmt.bytesperline;
645 for (y = dev->overlay_cap_top;
646 y < dev->overlay_cap_top + (int)img_height;
647 y++, vbuf += stride) {
648 int px;
649
650 if (y < 0 || y > dev->fb_cap.fmt.height)
651 continue;
652 if (quick) {
653 memcpy(vbase + x * pixsize,
654 vbuf + out_x * pixsize, w * pixsize);
655 vbase += dev->fb_cap.fmt.bytesperline;
656 continue;
657 }
658 for (px = 0; px < w; px++) {
659 if (!valid_pix(dev, y - dev->overlay_cap_top,
660 px + out_x, y, px + x))
661 continue;
662 memcpy(vbase + (px + x) * pixsize,
663 vbuf + (px + out_x) * pixsize,
664 pixsize);
665 }
666 vbase += dev->fb_cap.fmt.bytesperline;
667 }
668}
669
670static void vivid_thread_vid_cap_tick(struct vivid_dev *dev, int dropped_bufs)
671{
672 struct vivid_buffer *vid_cap_buf = NULL;
673 struct vivid_buffer *vbi_cap_buf = NULL;
674
675 dprintk(dev, 1, "Video Capture Thread Tick\n");
676
677 while (dropped_bufs-- > 1)
678 tpg_update_mv_count(&dev->tpg,
679 dev->field_cap == V4L2_FIELD_NONE ||
680 dev->field_cap == V4L2_FIELD_ALTERNATE);
681
682
683 if (dev->perc_dropped_buffers &&
684 prandom_u32_max(100) < dev->perc_dropped_buffers)
685 goto update_mv;
686
687 spin_lock(&dev->slock);
688 if (!list_empty(&dev->vid_cap_active)) {
689 vid_cap_buf = list_entry(dev->vid_cap_active.next, struct vivid_buffer, list);
690 list_del(&vid_cap_buf->list);
691 }
692 if (!list_empty(&dev->vbi_cap_active)) {
693 if (dev->field_cap != V4L2_FIELD_ALTERNATE ||
694 (dev->vbi_cap_seq_count & 1)) {
695 vbi_cap_buf = list_entry(dev->vbi_cap_active.next,
696 struct vivid_buffer, list);
697 list_del(&vbi_cap_buf->list);
698 }
699 }
700 spin_unlock(&dev->slock);
701
702 if (!vid_cap_buf && !vbi_cap_buf)
703 goto update_mv;
704
705 if (vid_cap_buf) {
706 v4l2_ctrl_request_setup(vid_cap_buf->vb.vb2_buf.req_obj.req,
707 &dev->ctrl_hdl_vid_cap);
708
709 vivid_fillbuff(dev, vid_cap_buf);
710 dprintk(dev, 1, "filled buffer %d\n",
711 vid_cap_buf->vb.vb2_buf.index);
712
713
714 if (dev->overlay_cap_owner && dev->fb_cap.base &&
715 dev->fb_cap.fmt.pixelformat == dev->fmt_cap->fourcc)
716 vivid_overlay(dev, vid_cap_buf);
717
718 v4l2_ctrl_request_complete(vid_cap_buf->vb.vb2_buf.req_obj.req,
719 &dev->ctrl_hdl_vid_cap);
720 vb2_buffer_done(&vid_cap_buf->vb.vb2_buf, dev->dqbuf_error ?
721 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
722 dprintk(dev, 2, "vid_cap buffer %d done\n",
723 vid_cap_buf->vb.vb2_buf.index);
724 }
725
726 if (vbi_cap_buf) {
727 v4l2_ctrl_request_setup(vbi_cap_buf->vb.vb2_buf.req_obj.req,
728 &dev->ctrl_hdl_vbi_cap);
729 if (dev->stream_sliced_vbi_cap)
730 vivid_sliced_vbi_cap_process(dev, vbi_cap_buf);
731 else
732 vivid_raw_vbi_cap_process(dev, vbi_cap_buf);
733 v4l2_ctrl_request_complete(vbi_cap_buf->vb.vb2_buf.req_obj.req,
734 &dev->ctrl_hdl_vbi_cap);
735 vb2_buffer_done(&vbi_cap_buf->vb.vb2_buf, dev->dqbuf_error ?
736 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
737 dprintk(dev, 2, "vbi_cap %d done\n",
738 vbi_cap_buf->vb.vb2_buf.index);
739 }
740 dev->dqbuf_error = false;
741
742update_mv:
743
744 tpg_update_mv_count(&dev->tpg, dev->field_cap == V4L2_FIELD_NONE ||
745 dev->field_cap == V4L2_FIELD_ALTERNATE);
746}
747
748static int vivid_thread_vid_cap(void *data)
749{
750 struct vivid_dev *dev = data;
751 u64 numerators_since_start;
752 u64 buffers_since_start;
753 u64 next_jiffies_since_start;
754 unsigned long jiffies_since_start;
755 unsigned long cur_jiffies;
756 unsigned wait_jiffies;
757 unsigned numerator;
758 unsigned denominator;
759 int dropped_bufs;
760
761 dprintk(dev, 1, "Video Capture Thread Start\n");
762
763 set_freezable();
764
765
766 dev->cap_seq_offset = 0;
767 dev->cap_seq_count = 0;
768 dev->cap_seq_resync = false;
769 dev->jiffies_vid_cap = jiffies;
770
771 for (;;) {
772 try_to_freeze();
773 if (kthread_should_stop())
774 break;
775
776 mutex_lock(&dev->mutex);
777 cur_jiffies = jiffies;
778 if (dev->cap_seq_resync) {
779 dev->jiffies_vid_cap = cur_jiffies;
780 dev->cap_seq_offset = dev->cap_seq_count + 1;
781 dev->cap_seq_count = 0;
782 dev->cap_seq_resync = false;
783 }
784 numerator = dev->timeperframe_vid_cap.numerator;
785 denominator = dev->timeperframe_vid_cap.denominator;
786
787 if (dev->field_cap == V4L2_FIELD_ALTERNATE)
788 denominator *= 2;
789
790
791 jiffies_since_start = cur_jiffies - dev->jiffies_vid_cap;
792
793 buffers_since_start = (u64)jiffies_since_start * denominator +
794 (HZ * numerator) / 2;
795 do_div(buffers_since_start, HZ * numerator);
796
797
798
799
800
801
802
803 if (jiffies_since_start > JIFFIES_RESYNC) {
804 dev->jiffies_vid_cap = cur_jiffies;
805 dev->cap_seq_offset = buffers_since_start;
806 buffers_since_start = 0;
807 }
808 dropped_bufs = buffers_since_start + dev->cap_seq_offset - dev->cap_seq_count;
809 dev->cap_seq_count = buffers_since_start + dev->cap_seq_offset;
810 dev->vid_cap_seq_count = dev->cap_seq_count - dev->vid_cap_seq_start;
811 dev->vbi_cap_seq_count = dev->cap_seq_count - dev->vbi_cap_seq_start;
812
813 vivid_thread_vid_cap_tick(dev, dropped_bufs);
814
815
816
817
818
819 numerators_since_start = ++buffers_since_start * numerator;
820
821
822 jiffies_since_start = jiffies - dev->jiffies_vid_cap;
823
824 mutex_unlock(&dev->mutex);
825
826
827
828
829
830 next_jiffies_since_start = numerators_since_start * HZ +
831 denominator / 2;
832 do_div(next_jiffies_since_start, denominator);
833
834 if (next_jiffies_since_start < jiffies_since_start)
835 next_jiffies_since_start = jiffies_since_start;
836
837 wait_jiffies = next_jiffies_since_start - jiffies_since_start;
838 schedule_timeout_interruptible(wait_jiffies ? wait_jiffies : 1);
839 }
840 dprintk(dev, 1, "Video Capture Thread End\n");
841 return 0;
842}
843
844static void vivid_grab_controls(struct vivid_dev *dev, bool grab)
845{
846 v4l2_ctrl_grab(dev->ctrl_has_crop_cap, grab);
847 v4l2_ctrl_grab(dev->ctrl_has_compose_cap, grab);
848 v4l2_ctrl_grab(dev->ctrl_has_scaler_cap, grab);
849}
850
851int vivid_start_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
852{
853 dprintk(dev, 1, "%s\n", __func__);
854
855 if (dev->kthread_vid_cap) {
856 u32 seq_count = dev->cap_seq_count + dev->seq_wrap * 128;
857
858 if (pstreaming == &dev->vid_cap_streaming)
859 dev->vid_cap_seq_start = seq_count;
860 else
861 dev->vbi_cap_seq_start = seq_count;
862 *pstreaming = true;
863 return 0;
864 }
865
866
867 tpg_init_mv_count(&dev->tpg);
868
869 dev->vid_cap_seq_start = dev->seq_wrap * 128;
870 dev->vbi_cap_seq_start = dev->seq_wrap * 128;
871
872 dev->kthread_vid_cap = kthread_run(vivid_thread_vid_cap, dev,
873 "%s-vid-cap", dev->v4l2_dev.name);
874
875 if (IS_ERR(dev->kthread_vid_cap)) {
876 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
877 return PTR_ERR(dev->kthread_vid_cap);
878 }
879 *pstreaming = true;
880 vivid_grab_controls(dev, true);
881
882 dprintk(dev, 1, "returning from %s\n", __func__);
883 return 0;
884}
885
886void vivid_stop_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
887{
888 dprintk(dev, 1, "%s\n", __func__);
889
890 if (dev->kthread_vid_cap == NULL)
891 return;
892
893 *pstreaming = false;
894 if (pstreaming == &dev->vid_cap_streaming) {
895
896 while (!list_empty(&dev->vid_cap_active)) {
897 struct vivid_buffer *buf;
898
899 buf = list_entry(dev->vid_cap_active.next,
900 struct vivid_buffer, list);
901 list_del(&buf->list);
902 v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
903 &dev->ctrl_hdl_vid_cap);
904 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
905 dprintk(dev, 2, "vid_cap buffer %d done\n",
906 buf->vb.vb2_buf.index);
907 }
908 }
909
910 if (pstreaming == &dev->vbi_cap_streaming) {
911 while (!list_empty(&dev->vbi_cap_active)) {
912 struct vivid_buffer *buf;
913
914 buf = list_entry(dev->vbi_cap_active.next,
915 struct vivid_buffer, list);
916 list_del(&buf->list);
917 v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
918 &dev->ctrl_hdl_vbi_cap);
919 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
920 dprintk(dev, 2, "vbi_cap buffer %d done\n",
921 buf->vb.vb2_buf.index);
922 }
923 }
924
925 if (dev->vid_cap_streaming || dev->vbi_cap_streaming)
926 return;
927
928
929 vivid_grab_controls(dev, false);
930 mutex_unlock(&dev->mutex);
931 kthread_stop(dev->kthread_vid_cap);
932 dev->kthread_vid_cap = NULL;
933 mutex_lock(&dev->mutex);
934}
935