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 dev->out_seq_count = 0; 158 dev->jiffies_vid_out = jiffies; 159 dev->out_seq_resync = false; 160 if (dev->time_wrap) 161 dev->time_wrap_offset = dev->time_wrap - ktime_get_ns(); 162 else 163 dev->time_wrap_offset = 0; 164 165 for (;;) { 166 try_to_freeze(); 167 if (kthread_should_stop()) 168 break; 169 170 if (!mutex_trylock(&dev->mutex)) { 171 schedule(); 172 continue; 173 } 174 175 cur_jiffies = jiffies; 176 if (dev->out_seq_resync) { 177 dev->jiffies_vid_out = cur_jiffies; 178 dev->out_seq_offset = dev->out_seq_count + 1; 179 dev->out_seq_count = 0; 180 dev->out_seq_resync = false; 181 } 182 numerator = dev->timeperframe_vid_out.numerator; 183 denominator = dev->timeperframe_vid_out.denominator; 184 185 if (dev->field_out == V4L2_FIELD_ALTERNATE) 186 denominator *= 2; 187 188 /* Calculate the number of jiffies since we started streaming */ 189 jiffies_since_start = cur_jiffies - dev->jiffies_vid_out; 190 /* Get the number of buffers streamed since the start */ 191 buffers_since_start = (u64)jiffies_since_start * denominator + 192 (HZ * numerator) / 2; 193 do_div(buffers_since_start, HZ * numerator); 194 195 /* 196 * After more than 0xf0000000 (rounded down to a multiple of 197 * 'jiffies-per-day' to ease jiffies_to_msecs calculation) 198 * jiffies have passed since we started streaming reset the 199 * counters and keep track of the sequence offset. 200 */ 201 if (jiffies_since_start > JIFFIES_RESYNC) { 202 dev->jiffies_vid_out = cur_jiffies; 203 dev->out_seq_offset = buffers_since_start; 204 buffers_since_start = 0; 205 } 206 dev->out_seq_count = buffers_since_start + dev->out_seq_offset; 207 dev->vid_out_seq_count = dev->out_seq_count - dev->vid_out_seq_start; 208 dev->vbi_out_seq_count = dev->out_seq_count - dev->vbi_out_seq_start; 209 dev->meta_out_seq_count = dev->out_seq_count - dev->meta_out_seq_start; 210 211 vivid_thread_vid_out_tick(dev); 212 mutex_unlock(&dev->mutex); 213 214 /* 215 * Calculate the number of 'numerators' streamed since we started, 216 * not including the current buffer. 217 */ 218 numerators_since_start = buffers_since_start * numerator; 219 220 /* And the number of jiffies since we started */ 221 jiffies_since_start = jiffies - dev->jiffies_vid_out; 222 223 /* Increase by the 'numerator' of one buffer */ 224 numerators_since_start += numerator; 225 /* 226 * Calculate when that next buffer is supposed to start 227 * in jiffies since we started streaming. 228 */ 229 next_jiffies_since_start = numerators_since_start * HZ + 230 denominator / 2; 231 do_div(next_jiffies_since_start, denominator); 232 /* If it is in the past, then just schedule asap */ 233 if (next_jiffies_since_start < jiffies_since_start) 234 next_jiffies_since_start = jiffies_since_start; 235 236 wait_jiffies = next_jiffies_since_start - jiffies_since_start; 237 while (jiffies - cur_jiffies < wait_jiffies && 238 !kthread_should_stop()) 239 schedule(); 240 } 241 dprintk(dev, 1, "Video Output Thread End\n"); 242 return 0; 243 } 244 245 static void vivid_grab_controls(struct vivid_dev *dev, bool grab) 246 { 247 v4l2_ctrl_grab(dev->ctrl_has_crop_out, grab); 248 v4l2_ctrl_grab(dev->ctrl_has_compose_out, grab); 249 v4l2_ctrl_grab(dev->ctrl_has_scaler_out, grab); 250 v4l2_ctrl_grab(dev->ctrl_tx_mode, grab); 251 v4l2_ctrl_grab(dev->ctrl_tx_rgb_range, grab); 252 } 253 254 int vivid_start_generating_vid_out(struct vivid_dev *dev, bool *pstreaming) 255 { 256 dprintk(dev, 1, "%s\n", __func__); 257 258 if (dev->kthread_vid_out) { 259 u32 seq_count = dev->out_seq_count + dev->seq_wrap * 128; 260 261 if (pstreaming == &dev->vid_out_streaming) 262 dev->vid_out_seq_start = seq_count; 263 else if (pstreaming == &dev->vbi_out_streaming) 264 dev->vbi_out_seq_start = seq_count; 265 else 266 dev->meta_out_seq_start = seq_count; 267 *pstreaming = true; 268 return 0; 269 } 270 271 /* Resets frame counters */ 272 dev->jiffies_vid_out = jiffies; 273 dev->vid_out_seq_start = dev->seq_wrap * 128; 274 dev->vbi_out_seq_start = dev->seq_wrap * 128; 275 dev->meta_out_seq_start = dev->seq_wrap * 128; 276 277 dev->kthread_vid_out = kthread_run(vivid_thread_vid_out, dev, 278 "%s-vid-out", dev->v4l2_dev.name); 279 280 if (IS_ERR(dev->kthread_vid_out)) { 281 int err = PTR_ERR(dev->kthread_vid_out); 282 283 dev->kthread_vid_out = NULL; 284 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n"); 285 return err; 286 } 287 *pstreaming = true; 288 vivid_grab_controls(dev, true); 289 290 dprintk(dev, 1, "returning from %s\n", __func__); 291 return 0; 292 } 293 294 void vivid_stop_generating_vid_out(struct vivid_dev *dev, bool *pstreaming) 295 { 296 dprintk(dev, 1, "%s\n", __func__); 297 298 if (dev->kthread_vid_out == NULL) 299 return; 300 301 *pstreaming = false; 302 if (pstreaming == &dev->vid_out_streaming) { 303 /* Release all active buffers */ 304 while (!list_empty(&dev->vid_out_active)) { 305 struct vivid_buffer *buf; 306 307 buf = list_entry(dev->vid_out_active.next, 308 struct vivid_buffer, list); 309 list_del(&buf->list); 310 v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req, 311 &dev->ctrl_hdl_vid_out); 312 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 313 dprintk(dev, 2, "vid_out buffer %d done\n", 314 buf->vb.vb2_buf.index); 315 } 316 } 317 318 if (pstreaming == &dev->vbi_out_streaming) { 319 while (!list_empty(&dev->vbi_out_active)) { 320 struct vivid_buffer *buf; 321 322 buf = list_entry(dev->vbi_out_active.next, 323 struct vivid_buffer, list); 324 list_del(&buf->list); 325 v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req, 326 &dev->ctrl_hdl_vbi_out); 327 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 328 dprintk(dev, 2, "vbi_out buffer %d done\n", 329 buf->vb.vb2_buf.index); 330 } 331 } 332 333 if (pstreaming == &dev->meta_out_streaming) { 334 while (!list_empty(&dev->meta_out_active)) { 335 struct vivid_buffer *buf; 336 337 buf = list_entry(dev->meta_out_active.next, 338 struct vivid_buffer, list); 339 list_del(&buf->list); 340 v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req, 341 &dev->ctrl_hdl_meta_out); 342 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 343 dprintk(dev, 2, "meta_out buffer %d done\n", 344 buf->vb.vb2_buf.index); 345 } 346 } 347 348 if (dev->vid_out_streaming || dev->vbi_out_streaming || 349 dev->meta_out_streaming) 350 return; 351 352 /* shutdown control thread */ 353 vivid_grab_controls(dev, false); 354 kthread_stop(dev->kthread_vid_out); 355 dev->kthread_vid_out = NULL; 356 } 357