1 // SPDX-License-Identifier: GPL-2.0-only
3 * vivid-kthread-out.h - video/vbi output thread support functions.
5 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
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>
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"
42 static void vivid_thread_vid_out_tick(struct vivid_dev *dev)
44 struct vivid_buffer *vid_out_buf = NULL;
45 struct vivid_buffer *vbi_out_buf = NULL;
47 dprintk(dev, 1, "Video Output Thread Tick\n");
49 /* Drop a certain percentage of buffers. */
50 if (dev->perc_dropped_buffers &&
51 prandom_u32_max(100) < dev->perc_dropped_buffers)
54 spin_lock(&dev->slock);
56 * Only dequeue buffer if there is at least one more pending.
57 * This makes video loopback possible.
59 if (!list_empty(&dev->vid_out_active) &&
60 !list_is_singular(&dev->vid_out_active)) {
61 vid_out_buf = list_entry(dev->vid_out_active.next,
62 struct vivid_buffer, list);
63 list_del(&vid_out_buf->list);
65 if (!list_empty(&dev->vbi_out_active) &&
66 (dev->field_out != V4L2_FIELD_ALTERNATE ||
67 (dev->vbi_out_seq_count & 1))) {
68 vbi_out_buf = list_entry(dev->vbi_out_active.next,
69 struct vivid_buffer, list);
70 list_del(&vbi_out_buf->list);
72 spin_unlock(&dev->slock);
74 if (!vid_out_buf && !vbi_out_buf)
78 vid_out_buf->vb.sequence = dev->vid_out_seq_count;
79 if (dev->field_out == V4L2_FIELD_ALTERNATE) {
81 * The sequence counter counts frames, not fields.
84 vid_out_buf->vb.sequence /= 2;
86 vid_out_buf->vb.vb2_buf.timestamp =
87 ktime_get_ns() + dev->time_wrap_offset;
88 vb2_buffer_done(&vid_out_buf->vb.vb2_buf, dev->dqbuf_error ?
89 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
90 dprintk(dev, 2, "vid_out buffer %d done\n",
91 vid_out_buf->vb.vb2_buf.index);
95 if (dev->stream_sliced_vbi_out)
96 vivid_sliced_vbi_out_process(dev, vbi_out_buf);
98 vbi_out_buf->vb.sequence = dev->vbi_out_seq_count;
99 vbi_out_buf->vb.vb2_buf.timestamp =
100 ktime_get_ns() + dev->time_wrap_offset;
101 vb2_buffer_done(&vbi_out_buf->vb.vb2_buf, dev->dqbuf_error ?
102 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
103 dprintk(dev, 2, "vbi_out buffer %d done\n",
104 vbi_out_buf->vb.vb2_buf.index);
106 dev->dqbuf_error = false;
109 static int vivid_thread_vid_out(void *data)
111 struct vivid_dev *dev = data;
112 u64 numerators_since_start;
113 u64 buffers_since_start;
114 u64 next_jiffies_since_start;
115 unsigned long jiffies_since_start;
116 unsigned long cur_jiffies;
117 unsigned wait_jiffies;
119 unsigned denominator;
121 dprintk(dev, 1, "Video Output Thread Start\n");
125 /* Resets frame counters */
126 dev->out_seq_offset = 0;
128 dev->out_seq_count = 0xffffff80U;
129 dev->jiffies_vid_out = jiffies;
130 dev->vid_out_seq_start = dev->vbi_out_seq_start = 0;
131 dev->out_seq_resync = false;
135 if (kthread_should_stop())
138 if (!mutex_trylock(&dev->mutex)) {
139 schedule_timeout_uninterruptible(1);
143 cur_jiffies = jiffies;
144 if (dev->out_seq_resync) {
145 dev->jiffies_vid_out = cur_jiffies;
146 dev->out_seq_offset = dev->out_seq_count + 1;
147 dev->out_seq_count = 0;
148 dev->out_seq_resync = false;
150 numerator = dev->timeperframe_vid_out.numerator;
151 denominator = dev->timeperframe_vid_out.denominator;
153 if (dev->field_out == V4L2_FIELD_ALTERNATE)
156 /* Calculate the number of jiffies since we started streaming */
157 jiffies_since_start = cur_jiffies - dev->jiffies_vid_out;
158 /* Get the number of buffers streamed since the start */
159 buffers_since_start = (u64)jiffies_since_start * denominator +
160 (HZ * numerator) / 2;
161 do_div(buffers_since_start, HZ * numerator);
164 * After more than 0xf0000000 (rounded down to a multiple of
165 * 'jiffies-per-day' to ease jiffies_to_msecs calculation)
166 * jiffies have passed since we started streaming reset the
167 * counters and keep track of the sequence offset.
169 if (jiffies_since_start > JIFFIES_RESYNC) {
170 dev->jiffies_vid_out = cur_jiffies;
171 dev->out_seq_offset = buffers_since_start;
172 buffers_since_start = 0;
174 dev->out_seq_count = buffers_since_start + dev->out_seq_offset;
175 dev->vid_out_seq_count = dev->out_seq_count - dev->vid_out_seq_start;
176 dev->vbi_out_seq_count = dev->out_seq_count - dev->vbi_out_seq_start;
178 vivid_thread_vid_out_tick(dev);
179 mutex_unlock(&dev->mutex);
182 * Calculate the number of 'numerators' streamed since we started,
183 * not including the current buffer.
185 numerators_since_start = buffers_since_start * numerator;
187 /* And the number of jiffies since we started */
188 jiffies_since_start = jiffies - dev->jiffies_vid_out;
190 /* Increase by the 'numerator' of one buffer */
191 numerators_since_start += numerator;
193 * Calculate when that next buffer is supposed to start
194 * in jiffies since we started streaming.
196 next_jiffies_since_start = numerators_since_start * HZ +
198 do_div(next_jiffies_since_start, denominator);
199 /* If it is in the past, then just schedule asap */
200 if (next_jiffies_since_start < jiffies_since_start)
201 next_jiffies_since_start = jiffies_since_start;
203 wait_jiffies = next_jiffies_since_start - jiffies_since_start;
204 schedule_timeout_interruptible(wait_jiffies ? wait_jiffies : 1);
206 dprintk(dev, 1, "Video Output Thread End\n");
210 static void vivid_grab_controls(struct vivid_dev *dev, bool grab)
212 v4l2_ctrl_grab(dev->ctrl_has_crop_out, grab);
213 v4l2_ctrl_grab(dev->ctrl_has_compose_out, grab);
214 v4l2_ctrl_grab(dev->ctrl_has_scaler_out, grab);
215 v4l2_ctrl_grab(dev->ctrl_tx_mode, grab);
216 v4l2_ctrl_grab(dev->ctrl_tx_rgb_range, grab);
219 int vivid_start_generating_vid_out(struct vivid_dev *dev, bool *pstreaming)
221 dprintk(dev, 1, "%s\n", __func__);
223 if (dev->kthread_vid_out) {
224 u32 seq_count = dev->out_seq_count + dev->seq_wrap * 128;
226 if (pstreaming == &dev->vid_out_streaming)
227 dev->vid_out_seq_start = seq_count;
229 dev->vbi_out_seq_start = seq_count;
234 /* Resets frame counters */
235 dev->jiffies_vid_out = jiffies;
236 dev->vid_out_seq_start = dev->seq_wrap * 128;
237 dev->vbi_out_seq_start = dev->seq_wrap * 128;
239 dev->kthread_vid_out = kthread_run(vivid_thread_vid_out, dev,
240 "%s-vid-out", dev->v4l2_dev.name);
242 if (IS_ERR(dev->kthread_vid_out)) {
243 int err = PTR_ERR(dev->kthread_vid_out);
245 dev->kthread_vid_out = NULL;
246 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
250 vivid_grab_controls(dev, true);
252 dprintk(dev, 1, "returning from %s\n", __func__);
256 void vivid_stop_generating_vid_out(struct vivid_dev *dev, bool *pstreaming)
258 dprintk(dev, 1, "%s\n", __func__);
260 if (dev->kthread_vid_out == NULL)
264 if (pstreaming == &dev->vid_out_streaming) {
265 /* Release all active buffers */
266 while (!list_empty(&dev->vid_out_active)) {
267 struct vivid_buffer *buf;
269 buf = list_entry(dev->vid_out_active.next,
270 struct vivid_buffer, list);
271 list_del(&buf->list);
272 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
273 dprintk(dev, 2, "vid_out buffer %d done\n",
274 buf->vb.vb2_buf.index);
278 if (pstreaming == &dev->vbi_out_streaming) {
279 while (!list_empty(&dev->vbi_out_active)) {
280 struct vivid_buffer *buf;
282 buf = list_entry(dev->vbi_out_active.next,
283 struct vivid_buffer, list);
284 list_del(&buf->list);
285 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
286 dprintk(dev, 2, "vbi_out buffer %d done\n",
287 buf->vb.vb2_buf.index);
291 if (dev->vid_out_streaming || dev->vbi_out_streaming)
294 /* shutdown control thread */
295 vivid_grab_controls(dev, false);
296 kthread_stop(dev->kthread_vid_out);
297 dev->kthread_vid_out = NULL;