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