GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / media / platform / vivid / vivid-kthread-out.c
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
42 static void vivid_thread_vid_out_tick(struct vivid_dev *dev)
43 {
44         struct vivid_buffer *vid_out_buf = NULL;
45         struct vivid_buffer *vbi_out_buf = NULL;
46
47         dprintk(dev, 1, "Video Output Thread Tick\n");
48
49         /* Drop a certain percentage of buffers. */
50         if (dev->perc_dropped_buffers &&
51             prandom_u32_max(100) < dev->perc_dropped_buffers)
52                 return;
53
54         spin_lock(&dev->slock);
55         /*
56          * Only dequeue buffer if there is at least one more pending.
57          * This makes video loopback possible.
58          */
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);
64         }
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);
71         }
72         spin_unlock(&dev->slock);
73
74         if (!vid_out_buf && !vbi_out_buf)
75                 return;
76
77         if (vid_out_buf) {
78                 vid_out_buf->vb.sequence = dev->vid_out_seq_count;
79                 if (dev->field_out == V4L2_FIELD_ALTERNATE) {
80                         /*
81                          * The sequence counter counts frames, not fields.
82                          * So divide by two.
83                          */
84                         vid_out_buf->vb.sequence /= 2;
85                 }
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);
92         }
93
94         if (vbi_out_buf) {
95                 if (dev->stream_sliced_vbi_out)
96                         vivid_sliced_vbi_out_process(dev, vbi_out_buf);
97
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);
105         }
106         dev->dqbuf_error = false;
107 }
108
109 static int vivid_thread_vid_out(void *data)
110 {
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;
118         unsigned numerator;
119         unsigned denominator;
120
121         dprintk(dev, 1, "Video Output Thread Start\n");
122
123         set_freezable();
124
125         /* Resets frame counters */
126         dev->out_seq_offset = 0;
127         if (dev->seq_wrap)
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;
132
133         for (;;) {
134                 try_to_freeze();
135                 if (kthread_should_stop())
136                         break;
137
138                 if (!mutex_trylock(&dev->mutex)) {
139                         schedule_timeout_uninterruptible(1);
140                         continue;
141                 }
142
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;
149                 }
150                 numerator = dev->timeperframe_vid_out.numerator;
151                 denominator = dev->timeperframe_vid_out.denominator;
152
153                 if (dev->field_out == V4L2_FIELD_ALTERNATE)
154                         denominator *= 2;
155
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);
162
163                 /*
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.
168                  */
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;
173                 }
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;
177
178                 vivid_thread_vid_out_tick(dev);
179                 mutex_unlock(&dev->mutex);
180
181                 /*
182                  * Calculate the number of 'numerators' streamed since we started,
183                  * not including the current buffer.
184                  */
185                 numerators_since_start = buffers_since_start * numerator;
186
187                 /* And the number of jiffies since we started */
188                 jiffies_since_start = jiffies - dev->jiffies_vid_out;
189
190                 /* Increase by the 'numerator' of one buffer */
191                 numerators_since_start += numerator;
192                 /*
193                  * Calculate when that next buffer is supposed to start
194                  * in jiffies since we started streaming.
195                  */
196                 next_jiffies_since_start = numerators_since_start * HZ +
197                                            denominator / 2;
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;
202
203                 wait_jiffies = next_jiffies_since_start - jiffies_since_start;
204                 schedule_timeout_interruptible(wait_jiffies ? wait_jiffies : 1);
205         }
206         dprintk(dev, 1, "Video Output Thread End\n");
207         return 0;
208 }
209
210 static void vivid_grab_controls(struct vivid_dev *dev, bool grab)
211 {
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);
217 }
218
219 int vivid_start_generating_vid_out(struct vivid_dev *dev, bool *pstreaming)
220 {
221         dprintk(dev, 1, "%s\n", __func__);
222
223         if (dev->kthread_vid_out) {
224                 u32 seq_count = dev->out_seq_count + dev->seq_wrap * 128;
225
226                 if (pstreaming == &dev->vid_out_streaming)
227                         dev->vid_out_seq_start = seq_count;
228                 else
229                         dev->vbi_out_seq_start = seq_count;
230                 *pstreaming = true;
231                 return 0;
232         }
233
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;
238
239         dev->kthread_vid_out = kthread_run(vivid_thread_vid_out, dev,
240                         "%s-vid-out", dev->v4l2_dev.name);
241
242         if (IS_ERR(dev->kthread_vid_out)) {
243                 int err = PTR_ERR(dev->kthread_vid_out);
244
245                 dev->kthread_vid_out = NULL;
246                 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
247                 return err;
248         }
249         *pstreaming = true;
250         vivid_grab_controls(dev, true);
251
252         dprintk(dev, 1, "returning from %s\n", __func__);
253         return 0;
254 }
255
256 void vivid_stop_generating_vid_out(struct vivid_dev *dev, bool *pstreaming)
257 {
258         dprintk(dev, 1, "%s\n", __func__);
259
260         if (dev->kthread_vid_out == NULL)
261                 return;
262
263         *pstreaming = false;
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;
268
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);
275                 }
276         }
277
278         if (pstreaming == &dev->vbi_out_streaming) {
279                 while (!list_empty(&dev->vbi_out_active)) {
280                         struct vivid_buffer *buf;
281
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);
288                 }
289         }
290
291         if (dev->vid_out_streaming || dev->vbi_out_streaming)
292                 return;
293
294         /* shutdown control thread */
295         vivid_grab_controls(dev, false);
296         kthread_stop(dev->kthread_vid_out);
297         dev->kthread_vid_out = NULL;
298 }