Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * vivid-kthread-out.h - video/vbi output thread support functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/font.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/videodev2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/freezer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/v4l2-dv-timings.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <asm/div64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <media/videobuf2-vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <media/v4l2-dv-timings.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <media/v4l2-ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <media/v4l2-fh.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <media/v4l2-event.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include "vivid-core.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include "vivid-vid-common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include "vivid-vid-cap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include "vivid-vid-out.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include "vivid-radio-common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include "vivid-radio-rx.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include "vivid-radio-tx.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include "vivid-sdr-cap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include "vivid-vbi-cap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include "vivid-vbi-out.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include "vivid-osd.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #include "vivid-ctrls.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #include "vivid-kthread-out.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #include "vivid-meta-out.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static void vivid_thread_vid_out_tick(struct vivid_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct vivid_buffer *vid_out_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct vivid_buffer *vbi_out_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct vivid_buffer *meta_out_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	dprintk(dev, 1, "Video Output Thread Tick\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	/* Drop a certain percentage of buffers. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	if (dev->perc_dropped_buffers &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	    prandom_u32_max(100) < dev->perc_dropped_buffers)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	spin_lock(&dev->slock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	 * Only dequeue buffer if there is at least one more pending.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	 * This makes video loopback possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (!list_empty(&dev->vid_out_active) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	    !list_is_singular(&dev->vid_out_active)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		vid_out_buf = list_entry(dev->vid_out_active.next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 					 struct vivid_buffer, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		list_del(&vid_out_buf->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	if (!list_empty(&dev->vbi_out_active) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	    (dev->field_out != V4L2_FIELD_ALTERNATE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	     (dev->vbi_out_seq_count & 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		vbi_out_buf = list_entry(dev->vbi_out_active.next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 					 struct vivid_buffer, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		list_del(&vbi_out_buf->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (!list_empty(&dev->meta_out_active)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		meta_out_buf = list_entry(dev->meta_out_active.next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 					  struct vivid_buffer, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		list_del(&meta_out_buf->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	spin_unlock(&dev->slock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (!vid_out_buf && !vbi_out_buf && !meta_out_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (vid_out_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		v4l2_ctrl_request_setup(vid_out_buf->vb.vb2_buf.req_obj.req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 					&dev->ctrl_hdl_vid_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		v4l2_ctrl_request_complete(vid_out_buf->vb.vb2_buf.req_obj.req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 					   &dev->ctrl_hdl_vid_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		vid_out_buf->vb.sequence = dev->vid_out_seq_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		if (dev->field_out == V4L2_FIELD_ALTERNATE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			 * The sequence counter counts frames, not fields.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			 * So divide by two.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			vid_out_buf->vb.sequence /= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		vid_out_buf->vb.vb2_buf.timestamp =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			ktime_get_ns() + dev->time_wrap_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		vb2_buffer_done(&vid_out_buf->vb.vb2_buf, dev->dqbuf_error ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 				VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		dprintk(dev, 2, "vid_out buffer %d done\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			vid_out_buf->vb.vb2_buf.index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (vbi_out_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		v4l2_ctrl_request_setup(vbi_out_buf->vb.vb2_buf.req_obj.req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 					&dev->ctrl_hdl_vbi_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		v4l2_ctrl_request_complete(vbi_out_buf->vb.vb2_buf.req_obj.req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 					   &dev->ctrl_hdl_vbi_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		if (dev->stream_sliced_vbi_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			vivid_sliced_vbi_out_process(dev, vbi_out_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		vbi_out_buf->vb.sequence = dev->vbi_out_seq_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		vbi_out_buf->vb.vb2_buf.timestamp =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			ktime_get_ns() + dev->time_wrap_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		vb2_buffer_done(&vbi_out_buf->vb.vb2_buf, dev->dqbuf_error ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 				VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		dprintk(dev, 2, "vbi_out buffer %d done\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 			vbi_out_buf->vb.vb2_buf.index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (meta_out_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		v4l2_ctrl_request_setup(meta_out_buf->vb.vb2_buf.req_obj.req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 					&dev->ctrl_hdl_meta_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		v4l2_ctrl_request_complete(meta_out_buf->vb.vb2_buf.req_obj.req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 					   &dev->ctrl_hdl_meta_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		vivid_meta_out_process(dev, meta_out_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		meta_out_buf->vb.sequence = dev->meta_out_seq_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		meta_out_buf->vb.vb2_buf.timestamp =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			ktime_get_ns() + dev->time_wrap_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		vb2_buffer_done(&meta_out_buf->vb.vb2_buf, dev->dqbuf_error ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 				VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		dprintk(dev, 2, "meta_out buffer %d done\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			meta_out_buf->vb.vb2_buf.index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	dev->dqbuf_error = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static int vivid_thread_vid_out(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	struct vivid_dev *dev = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	u64 numerators_since_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	u64 buffers_since_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	u64 next_jiffies_since_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	unsigned long jiffies_since_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	unsigned long cur_jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	unsigned wait_jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	unsigned numerator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	unsigned denominator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	dprintk(dev, 1, "Video Output Thread Start\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	set_freezable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	/* Resets frame counters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	dev->out_seq_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	if (dev->seq_wrap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		dev->out_seq_count = 0xffffff80U;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	dev->jiffies_vid_out = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	dev->vid_out_seq_start = dev->vbi_out_seq_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	dev->meta_out_seq_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	dev->out_seq_resync = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		try_to_freeze();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		if (kthread_should_stop())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		if (!mutex_trylock(&dev->mutex)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			schedule_timeout_uninterruptible(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		cur_jiffies = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		if (dev->out_seq_resync) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			dev->jiffies_vid_out = cur_jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			dev->out_seq_offset = dev->out_seq_count + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 			dev->out_seq_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			dev->out_seq_resync = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		numerator = dev->timeperframe_vid_out.numerator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		denominator = dev->timeperframe_vid_out.denominator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		if (dev->field_out == V4L2_FIELD_ALTERNATE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			denominator *= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		/* Calculate the number of jiffies since we started streaming */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		jiffies_since_start = cur_jiffies - dev->jiffies_vid_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		/* Get the number of buffers streamed since the start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		buffers_since_start = (u64)jiffies_since_start * denominator +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 				      (HZ * numerator) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		do_div(buffers_since_start, HZ * numerator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		 * After more than 0xf0000000 (rounded down to a multiple of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		 * 'jiffies-per-day' to ease jiffies_to_msecs calculation)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		 * jiffies have passed since we started streaming reset the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		 * counters and keep track of the sequence offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		if (jiffies_since_start > JIFFIES_RESYNC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			dev->jiffies_vid_out = cur_jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			dev->out_seq_offset = buffers_since_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			buffers_since_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		dev->out_seq_count = buffers_since_start + dev->out_seq_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		dev->vid_out_seq_count = dev->out_seq_count - dev->vid_out_seq_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		dev->vbi_out_seq_count = dev->out_seq_count - dev->vbi_out_seq_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		dev->meta_out_seq_count = dev->out_seq_count - dev->meta_out_seq_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		vivid_thread_vid_out_tick(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		mutex_unlock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		 * Calculate the number of 'numerators' streamed since we started,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		 * not including the current buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		numerators_since_start = buffers_since_start * numerator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		/* And the number of jiffies since we started */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		jiffies_since_start = jiffies - dev->jiffies_vid_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		/* Increase by the 'numerator' of one buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		numerators_since_start += numerator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		 * Calculate when that next buffer is supposed to start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		 * in jiffies since we started streaming.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		next_jiffies_since_start = numerators_since_start * HZ +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 					   denominator / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		do_div(next_jiffies_since_start, denominator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		/* If it is in the past, then just schedule asap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		if (next_jiffies_since_start < jiffies_since_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			next_jiffies_since_start = jiffies_since_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		wait_jiffies = next_jiffies_since_start - jiffies_since_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		schedule_timeout_interruptible(wait_jiffies ? wait_jiffies : 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	dprintk(dev, 1, "Video Output Thread End\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static void vivid_grab_controls(struct vivid_dev *dev, bool grab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	v4l2_ctrl_grab(dev->ctrl_has_crop_out, grab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	v4l2_ctrl_grab(dev->ctrl_has_compose_out, grab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	v4l2_ctrl_grab(dev->ctrl_has_scaler_out, grab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	v4l2_ctrl_grab(dev->ctrl_tx_mode, grab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	v4l2_ctrl_grab(dev->ctrl_tx_rgb_range, grab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) int vivid_start_generating_vid_out(struct vivid_dev *dev, bool *pstreaming)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	dprintk(dev, 1, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	if (dev->kthread_vid_out) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		u32 seq_count = dev->out_seq_count + dev->seq_wrap * 128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		if (pstreaming == &dev->vid_out_streaming)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			dev->vid_out_seq_start = seq_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		else if (pstreaming == &dev->vbi_out_streaming)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 			dev->vbi_out_seq_start = seq_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			dev->meta_out_seq_start = seq_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		*pstreaming = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	/* Resets frame counters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	dev->jiffies_vid_out = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	dev->vid_out_seq_start = dev->seq_wrap * 128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	dev->vbi_out_seq_start = dev->seq_wrap * 128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	dev->meta_out_seq_start = dev->seq_wrap * 128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	dev->kthread_vid_out = kthread_run(vivid_thread_vid_out, dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			"%s-vid-out", dev->v4l2_dev.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	if (IS_ERR(dev->kthread_vid_out)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		int err = PTR_ERR(dev->kthread_vid_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		dev->kthread_vid_out = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	*pstreaming = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	vivid_grab_controls(dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	dprintk(dev, 1, "returning from %s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) void vivid_stop_generating_vid_out(struct vivid_dev *dev, bool *pstreaming)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	dprintk(dev, 1, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	if (dev->kthread_vid_out == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	*pstreaming = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (pstreaming == &dev->vid_out_streaming) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		/* Release all active buffers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		while (!list_empty(&dev->vid_out_active)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			struct vivid_buffer *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			buf = list_entry(dev->vid_out_active.next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 					 struct vivid_buffer, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			list_del(&buf->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 						   &dev->ctrl_hdl_vid_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			dprintk(dev, 2, "vid_out buffer %d done\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 				buf->vb.vb2_buf.index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	if (pstreaming == &dev->vbi_out_streaming) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		while (!list_empty(&dev->vbi_out_active)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			struct vivid_buffer *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			buf = list_entry(dev->vbi_out_active.next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 					 struct vivid_buffer, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 			list_del(&buf->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 						   &dev->ctrl_hdl_vbi_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			dprintk(dev, 2, "vbi_out buffer %d done\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 				buf->vb.vb2_buf.index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (pstreaming == &dev->meta_out_streaming) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		while (!list_empty(&dev->meta_out_active)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			struct vivid_buffer *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			buf = list_entry(dev->meta_out_active.next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 					 struct vivid_buffer, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 			list_del(&buf->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 						   &dev->ctrl_hdl_meta_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 			dprintk(dev, 2, "meta_out buffer %d done\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 				buf->vb.vb2_buf.index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	if (dev->vid_out_streaming || dev->vbi_out_streaming ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	    dev->meta_out_streaming)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	/* shutdown control thread */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	vivid_grab_controls(dev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	kthread_stop(dev->kthread_vid_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	dev->kthread_vid_out = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }