GNU Linux-libre 4.9.309-gnu1
[releases.git] / drivers / media / platform / vivid / vivid-kthread-out.c
1 /*
2  * vivid-kthread-out.h - video/vbi output thread support functions.
3  *
4  * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5  *
6  * This program is free software; you may redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17  * SOFTWARE.
18  */
19
20 #include <linux/module.h>
21 #include <linux/errno.h>
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/sched.h>
25 #include <linux/slab.h>
26 #include <linux/font.h>
27 #include <linux/mutex.h>
28 #include <linux/videodev2.h>
29 #include <linux/kthread.h>
30 #include <linux/freezer.h>
31 #include <linux/random.h>
32 #include <linux/v4l2-dv-timings.h>
33 #include <asm/div64.h>
34 #include <media/videobuf2-vmalloc.h>
35 #include <media/v4l2-dv-timings.h>
36 #include <media/v4l2-ioctl.h>
37 #include <media/v4l2-fh.h>
38 #include <media/v4l2-event.h>
39
40 #include "vivid-core.h"
41 #include "vivid-vid-common.h"
42 #include "vivid-vid-cap.h"
43 #include "vivid-vid-out.h"
44 #include "vivid-radio-common.h"
45 #include "vivid-radio-rx.h"
46 #include "vivid-radio-tx.h"
47 #include "vivid-sdr-cap.h"
48 #include "vivid-vbi-cap.h"
49 #include "vivid-vbi-out.h"
50 #include "vivid-osd.h"
51 #include "vivid-ctrls.h"
52 #include "vivid-kthread-out.h"
53
54 static void vivid_thread_vid_out_tick(struct vivid_dev *dev)
55 {
56         struct vivid_buffer *vid_out_buf = NULL;
57         struct vivid_buffer *vbi_out_buf = NULL;
58
59         dprintk(dev, 1, "Video Output Thread Tick\n");
60
61         /* Drop a certain percentage of buffers. */
62         if (dev->perc_dropped_buffers &&
63             prandom_u32_max(100) < dev->perc_dropped_buffers)
64                 return;
65
66         spin_lock(&dev->slock);
67         /*
68          * Only dequeue buffer if there is at least one more pending.
69          * This makes video loopback possible.
70          */
71         if (!list_empty(&dev->vid_out_active) &&
72             !list_is_singular(&dev->vid_out_active)) {
73                 vid_out_buf = list_entry(dev->vid_out_active.next,
74                                          struct vivid_buffer, list);
75                 list_del(&vid_out_buf->list);
76         }
77         if (!list_empty(&dev->vbi_out_active) &&
78             (dev->field_out != V4L2_FIELD_ALTERNATE ||
79              (dev->vbi_out_seq_count & 1))) {
80                 vbi_out_buf = list_entry(dev->vbi_out_active.next,
81                                          struct vivid_buffer, list);
82                 list_del(&vbi_out_buf->list);
83         }
84         spin_unlock(&dev->slock);
85
86         if (!vid_out_buf && !vbi_out_buf)
87                 return;
88
89         if (vid_out_buf) {
90                 vid_out_buf->vb.sequence = dev->vid_out_seq_count;
91                 if (dev->field_out == V4L2_FIELD_ALTERNATE) {
92                         /*
93                          * The sequence counter counts frames, not fields.
94                          * So divide by two.
95                          */
96                         vid_out_buf->vb.sequence /= 2;
97                 }
98                 vid_out_buf->vb.vb2_buf.timestamp =
99                         ktime_get_ns() + dev->time_wrap_offset;
100                 vb2_buffer_done(&vid_out_buf->vb.vb2_buf, dev->dqbuf_error ?
101                                 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
102                 dprintk(dev, 2, "vid_out buffer %d done\n",
103                         vid_out_buf->vb.vb2_buf.index);
104         }
105
106         if (vbi_out_buf) {
107                 if (dev->stream_sliced_vbi_out)
108                         vivid_sliced_vbi_out_process(dev, vbi_out_buf);
109
110                 vbi_out_buf->vb.sequence = dev->vbi_out_seq_count;
111                 vbi_out_buf->vb.vb2_buf.timestamp =
112                         ktime_get_ns() + dev->time_wrap_offset;
113                 vb2_buffer_done(&vbi_out_buf->vb.vb2_buf, dev->dqbuf_error ?
114                                 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
115                 dprintk(dev, 2, "vbi_out buffer %d done\n",
116                         vbi_out_buf->vb.vb2_buf.index);
117         }
118         dev->dqbuf_error = false;
119 }
120
121 static int vivid_thread_vid_out(void *data)
122 {
123         struct vivid_dev *dev = data;
124         u64 numerators_since_start;
125         u64 buffers_since_start;
126         u64 next_jiffies_since_start;
127         unsigned long jiffies_since_start;
128         unsigned long cur_jiffies;
129         unsigned wait_jiffies;
130         unsigned numerator;
131         unsigned denominator;
132
133         dprintk(dev, 1, "Video Output Thread Start\n");
134
135         set_freezable();
136
137         /* Resets frame counters */
138         dev->out_seq_offset = 0;
139         if (dev->seq_wrap)
140                 dev->out_seq_count = 0xffffff80U;
141         dev->jiffies_vid_out = jiffies;
142         dev->vid_out_seq_start = dev->vbi_out_seq_start = 0;
143         dev->out_seq_resync = false;
144
145         for (;;) {
146                 try_to_freeze();
147                 if (kthread_should_stop())
148                         break;
149
150                 if (!mutex_trylock(&dev->mutex)) {
151                         schedule_timeout_uninterruptible(1);
152                         continue;
153                 }
154
155                 cur_jiffies = jiffies;
156                 if (dev->out_seq_resync) {
157                         dev->jiffies_vid_out = cur_jiffies;
158                         dev->out_seq_offset = dev->out_seq_count + 1;
159                         dev->out_seq_count = 0;
160                         dev->out_seq_resync = false;
161                 }
162                 numerator = dev->timeperframe_vid_out.numerator;
163                 denominator = dev->timeperframe_vid_out.denominator;
164
165                 if (dev->field_out == V4L2_FIELD_ALTERNATE)
166                         denominator *= 2;
167
168                 /* Calculate the number of jiffies since we started streaming */
169                 jiffies_since_start = cur_jiffies - dev->jiffies_vid_out;
170                 /* Get the number of buffers streamed since the start */
171                 buffers_since_start = (u64)jiffies_since_start * denominator +
172                                       (HZ * numerator) / 2;
173                 do_div(buffers_since_start, HZ * numerator);
174
175                 /*
176                  * After more than 0xf0000000 (rounded down to a multiple of
177                  * 'jiffies-per-day' to ease jiffies_to_msecs calculation)
178                  * jiffies have passed since we started streaming reset the
179                  * counters and keep track of the sequence offset.
180                  */
181                 if (jiffies_since_start > JIFFIES_RESYNC) {
182                         dev->jiffies_vid_out = cur_jiffies;
183                         dev->out_seq_offset = buffers_since_start;
184                         buffers_since_start = 0;
185                 }
186                 dev->out_seq_count = buffers_since_start + dev->out_seq_offset;
187                 dev->vid_out_seq_count = dev->out_seq_count - dev->vid_out_seq_start;
188                 dev->vbi_out_seq_count = dev->out_seq_count - dev->vbi_out_seq_start;
189
190                 vivid_thread_vid_out_tick(dev);
191                 mutex_unlock(&dev->mutex);
192
193                 /*
194                  * Calculate the number of 'numerators' streamed since we started,
195                  * not including the current buffer.
196                  */
197                 numerators_since_start = buffers_since_start * numerator;
198
199                 /* And the number of jiffies since we started */
200                 jiffies_since_start = jiffies - dev->jiffies_vid_out;
201
202                 /* Increase by the 'numerator' of one buffer */
203                 numerators_since_start += numerator;
204                 /*
205                  * Calculate when that next buffer is supposed to start
206                  * in jiffies since we started streaming.
207                  */
208                 next_jiffies_since_start = numerators_since_start * HZ +
209                                            denominator / 2;
210                 do_div(next_jiffies_since_start, denominator);
211                 /* If it is in the past, then just schedule asap */
212                 if (next_jiffies_since_start < jiffies_since_start)
213                         next_jiffies_since_start = jiffies_since_start;
214
215                 wait_jiffies = next_jiffies_since_start - jiffies_since_start;
216                 schedule_timeout_interruptible(wait_jiffies ? wait_jiffies : 1);
217         }
218         dprintk(dev, 1, "Video Output Thread End\n");
219         return 0;
220 }
221
222 static void vivid_grab_controls(struct vivid_dev *dev, bool grab)
223 {
224         v4l2_ctrl_grab(dev->ctrl_has_crop_out, grab);
225         v4l2_ctrl_grab(dev->ctrl_has_compose_out, grab);
226         v4l2_ctrl_grab(dev->ctrl_has_scaler_out, grab);
227         v4l2_ctrl_grab(dev->ctrl_tx_mode, grab);
228         v4l2_ctrl_grab(dev->ctrl_tx_rgb_range, grab);
229 }
230
231 int vivid_start_generating_vid_out(struct vivid_dev *dev, bool *pstreaming)
232 {
233         dprintk(dev, 1, "%s\n", __func__);
234
235         if (dev->kthread_vid_out) {
236                 u32 seq_count = dev->out_seq_count + dev->seq_wrap * 128;
237
238                 if (pstreaming == &dev->vid_out_streaming)
239                         dev->vid_out_seq_start = seq_count;
240                 else
241                         dev->vbi_out_seq_start = seq_count;
242                 *pstreaming = true;
243                 return 0;
244         }
245
246         /* Resets frame counters */
247         dev->jiffies_vid_out = jiffies;
248         dev->vid_out_seq_start = dev->seq_wrap * 128;
249         dev->vbi_out_seq_start = dev->seq_wrap * 128;
250
251         dev->kthread_vid_out = kthread_run(vivid_thread_vid_out, dev,
252                         "%s-vid-out", dev->v4l2_dev.name);
253
254         if (IS_ERR(dev->kthread_vid_out)) {
255                 int err = PTR_ERR(dev->kthread_vid_out);
256
257                 dev->kthread_vid_out = NULL;
258                 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
259                 return err;
260         }
261         *pstreaming = true;
262         vivid_grab_controls(dev, true);
263
264         dprintk(dev, 1, "returning from %s\n", __func__);
265         return 0;
266 }
267
268 void vivid_stop_generating_vid_out(struct vivid_dev *dev, bool *pstreaming)
269 {
270         dprintk(dev, 1, "%s\n", __func__);
271
272         if (dev->kthread_vid_out == NULL)
273                 return;
274
275         *pstreaming = false;
276         if (pstreaming == &dev->vid_out_streaming) {
277                 /* Release all active buffers */
278                 while (!list_empty(&dev->vid_out_active)) {
279                         struct vivid_buffer *buf;
280
281                         buf = list_entry(dev->vid_out_active.next,
282                                          struct vivid_buffer, list);
283                         list_del(&buf->list);
284                         vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
285                         dprintk(dev, 2, "vid_out buffer %d done\n",
286                                 buf->vb.vb2_buf.index);
287                 }
288         }
289
290         if (pstreaming == &dev->vbi_out_streaming) {
291                 while (!list_empty(&dev->vbi_out_active)) {
292                         struct vivid_buffer *buf;
293
294                         buf = list_entry(dev->vbi_out_active.next,
295                                          struct vivid_buffer, list);
296                         list_del(&buf->list);
297                         vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
298                         dprintk(dev, 2, "vbi_out buffer %d done\n",
299                                 buf->vb.vb2_buf.index);
300                 }
301         }
302
303         if (dev->vid_out_streaming || dev->vbi_out_streaming)
304                 return;
305
306         /* shutdown control thread */
307         vivid_grab_controls(dev, false);
308         kthread_stop(dev->kthread_vid_out);
309         dev->kthread_vid_out = NULL;
310 }