vivid-kthread-out.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. #include <linux/module.h>
  8. #include <linux/errno.h>
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/sched.h>
  12. #include <linux/slab.h>
  13. #include <linux/font.h>
  14. #include <linux/mutex.h>
  15. #include <linux/videodev2.h>
  16. #include <linux/kthread.h>
  17. #include <linux/freezer.h>
  18. #include <linux/random.h>
  19. #include <linux/v4l2-dv-timings.h>
  20. #include <asm/div64.h>
  21. #include <media/videobuf2-vmalloc.h>
  22. #include <media/v4l2-dv-timings.h>
  23. #include <media/v4l2-ioctl.h>
  24. #include <media/v4l2-fh.h>
  25. #include <media/v4l2-event.h>
  26. #include "vivid-core.h"
  27. #include "vivid-vid-common.h"
  28. #include "vivid-vid-cap.h"
  29. #include "vivid-vid-out.h"
  30. #include "vivid-radio-common.h"
  31. #include "vivid-radio-rx.h"
  32. #include "vivid-radio-tx.h"
  33. #include "vivid-sdr-cap.h"
  34. #include "vivid-vbi-cap.h"
  35. #include "vivid-vbi-out.h"
  36. #include "vivid-osd.h"
  37. #include "vivid-ctrls.h"
  38. #include "vivid-kthread-out.h"
  39. static void vivid_thread_vid_out_tick(struct vivid_dev *dev)
  40. {
  41. struct vivid_buffer *vid_out_buf = NULL;
  42. struct vivid_buffer *vbi_out_buf = NULL;
  43. dprintk(dev, 1, "Video Output Thread Tick\n");
  44. /* Drop a certain percentage of buffers. */
  45. if (dev->perc_dropped_buffers &&
  46. prandom_u32_max(100) < dev->perc_dropped_buffers)
  47. return;
  48. spin_lock(&dev->slock);
  49. /*
  50. * Only dequeue buffer if there is at least one more pending.
  51. * This makes video loopback possible.
  52. */
  53. if (!list_empty(&dev->vid_out_active) &&
  54. !list_is_singular(&dev->vid_out_active)) {
  55. vid_out_buf = list_entry(dev->vid_out_active.next,
  56. struct vivid_buffer, list);
  57. list_del(&vid_out_buf->list);
  58. }
  59. if (!list_empty(&dev->vbi_out_active) &&
  60. (dev->field_out != V4L2_FIELD_ALTERNATE ||
  61. (dev->vbi_out_seq_count & 1))) {
  62. vbi_out_buf = list_entry(dev->vbi_out_active.next,
  63. struct vivid_buffer, list);
  64. list_del(&vbi_out_buf->list);
  65. }
  66. spin_unlock(&dev->slock);
  67. if (!vid_out_buf && !vbi_out_buf)
  68. return;
  69. if (vid_out_buf) {
  70. vid_out_buf->vb.sequence = dev->vid_out_seq_count;
  71. if (dev->field_out == V4L2_FIELD_ALTERNATE) {
  72. /*
  73. * The sequence counter counts frames, not fields.
  74. * So divide by two.
  75. */
  76. vid_out_buf->vb.sequence /= 2;
  77. }
  78. vid_out_buf->vb.vb2_buf.timestamp =
  79. ktime_get_ns() + dev->time_wrap_offset;
  80. vb2_buffer_done(&vid_out_buf->vb.vb2_buf, dev->dqbuf_error ?
  81. VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
  82. dprintk(dev, 2, "vid_out buffer %d done\n",
  83. vid_out_buf->vb.vb2_buf.index);
  84. }
  85. if (vbi_out_buf) {
  86. if (dev->stream_sliced_vbi_out)
  87. vivid_sliced_vbi_out_process(dev, vbi_out_buf);
  88. vbi_out_buf->vb.sequence = dev->vbi_out_seq_count;
  89. vbi_out_buf->vb.vb2_buf.timestamp =
  90. ktime_get_ns() + dev->time_wrap_offset;
  91. vb2_buffer_done(&vbi_out_buf->vb.vb2_buf, dev->dqbuf_error ?
  92. VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
  93. dprintk(dev, 2, "vbi_out buffer %d done\n",
  94. vbi_out_buf->vb.vb2_buf.index);
  95. }
  96. dev->dqbuf_error = false;
  97. }
  98. static int vivid_thread_vid_out(void *data)
  99. {
  100. struct vivid_dev *dev = data;
  101. u64 numerators_since_start;
  102. u64 buffers_since_start;
  103. u64 next_jiffies_since_start;
  104. unsigned long jiffies_since_start;
  105. unsigned long cur_jiffies;
  106. unsigned wait_jiffies;
  107. unsigned numerator;
  108. unsigned denominator;
  109. dprintk(dev, 1, "Video Output Thread Start\n");
  110. set_freezable();
  111. /* Resets frame counters */
  112. dev->out_seq_offset = 0;
  113. if (dev->seq_wrap)
  114. dev->out_seq_count = 0xffffff80U;
  115. dev->jiffies_vid_out = jiffies;
  116. dev->vid_out_seq_start = dev->vbi_out_seq_start = 0;
  117. dev->out_seq_resync = false;
  118. for (;;) {
  119. try_to_freeze();
  120. if (kthread_should_stop())
  121. break;
  122. if (!mutex_trylock(&dev->mutex)) {
  123. schedule_timeout_uninterruptible(1);
  124. continue;
  125. }
  126. cur_jiffies = jiffies;
  127. if (dev->out_seq_resync) {
  128. dev->jiffies_vid_out = cur_jiffies;
  129. dev->out_seq_offset = dev->out_seq_count + 1;
  130. dev->out_seq_count = 0;
  131. dev->out_seq_resync = false;
  132. }
  133. numerator = dev->timeperframe_vid_out.numerator;
  134. denominator = dev->timeperframe_vid_out.denominator;
  135. if (dev->field_out == V4L2_FIELD_ALTERNATE)
  136. denominator *= 2;
  137. /* Calculate the number of jiffies since we started streaming */
  138. jiffies_since_start = cur_jiffies - dev->jiffies_vid_out;
  139. /* Get the number of buffers streamed since the start */
  140. buffers_since_start = (u64)jiffies_since_start * denominator +
  141. (HZ * numerator) / 2;
  142. do_div(buffers_since_start, HZ * numerator);
  143. /*
  144. * After more than 0xf0000000 (rounded down to a multiple of
  145. * 'jiffies-per-day' to ease jiffies_to_msecs calculation)
  146. * jiffies have passed since we started streaming reset the
  147. * counters and keep track of the sequence offset.
  148. */
  149. if (jiffies_since_start > JIFFIES_RESYNC) {
  150. dev->jiffies_vid_out = cur_jiffies;
  151. dev->out_seq_offset = buffers_since_start;
  152. buffers_since_start = 0;
  153. }
  154. dev->out_seq_count = buffers_since_start + dev->out_seq_offset;
  155. dev->vid_out_seq_count = dev->out_seq_count - dev->vid_out_seq_start;
  156. dev->vbi_out_seq_count = dev->out_seq_count - dev->vbi_out_seq_start;
  157. vivid_thread_vid_out_tick(dev);
  158. mutex_unlock(&dev->mutex);
  159. /*
  160. * Calculate the number of 'numerators' streamed since we started,
  161. * not including the current buffer.
  162. */
  163. numerators_since_start = buffers_since_start * numerator;
  164. /* And the number of jiffies since we started */
  165. jiffies_since_start = jiffies - dev->jiffies_vid_out;
  166. /* Increase by the 'numerator' of one buffer */
  167. numerators_since_start += numerator;
  168. /*
  169. * Calculate when that next buffer is supposed to start
  170. * in jiffies since we started streaming.
  171. */
  172. next_jiffies_since_start = numerators_since_start * HZ +
  173. denominator / 2;
  174. do_div(next_jiffies_since_start, denominator);
  175. /* If it is in the past, then just schedule asap */
  176. if (next_jiffies_since_start < jiffies_since_start)
  177. next_jiffies_since_start = jiffies_since_start;
  178. wait_jiffies = next_jiffies_since_start - jiffies_since_start;
  179. schedule_timeout_interruptible(wait_jiffies ? wait_jiffies : 1);
  180. }
  181. dprintk(dev, 1, "Video Output Thread End\n");
  182. return 0;
  183. }
  184. static void vivid_grab_controls(struct vivid_dev *dev, bool grab)
  185. {
  186. v4l2_ctrl_grab(dev->ctrl_has_crop_out, grab);
  187. v4l2_ctrl_grab(dev->ctrl_has_compose_out, grab);
  188. v4l2_ctrl_grab(dev->ctrl_has_scaler_out, grab);
  189. v4l2_ctrl_grab(dev->ctrl_tx_mode, grab);
  190. v4l2_ctrl_grab(dev->ctrl_tx_rgb_range, grab);
  191. }
  192. int vivid_start_generating_vid_out(struct vivid_dev *dev, bool *pstreaming)
  193. {
  194. dprintk(dev, 1, "%s\n", __func__);
  195. if (dev->kthread_vid_out) {
  196. u32 seq_count = dev->out_seq_count + dev->seq_wrap * 128;
  197. if (pstreaming == &dev->vid_out_streaming)
  198. dev->vid_out_seq_start = seq_count;
  199. else
  200. dev->vbi_out_seq_start = seq_count;
  201. *pstreaming = true;
  202. return 0;
  203. }
  204. /* Resets frame counters */
  205. dev->jiffies_vid_out = jiffies;
  206. dev->vid_out_seq_start = dev->seq_wrap * 128;
  207. dev->vbi_out_seq_start = dev->seq_wrap * 128;
  208. dev->kthread_vid_out = kthread_run(vivid_thread_vid_out, dev,
  209. "%s-vid-out", dev->v4l2_dev.name);
  210. if (IS_ERR(dev->kthread_vid_out)) {
  211. int err = PTR_ERR(dev->kthread_vid_out);
  212. dev->kthread_vid_out = NULL;
  213. v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
  214. return err;
  215. }
  216. *pstreaming = true;
  217. vivid_grab_controls(dev, true);
  218. dprintk(dev, 1, "returning from %s\n", __func__);
  219. return 0;
  220. }
  221. void vivid_stop_generating_vid_out(struct vivid_dev *dev, bool *pstreaming)
  222. {
  223. dprintk(dev, 1, "%s\n", __func__);
  224. if (dev->kthread_vid_out == NULL)
  225. return;
  226. *pstreaming = false;
  227. if (pstreaming == &dev->vid_out_streaming) {
  228. /* Release all active buffers */
  229. while (!list_empty(&dev->vid_out_active)) {
  230. struct vivid_buffer *buf;
  231. buf = list_entry(dev->vid_out_active.next,
  232. struct vivid_buffer, list);
  233. list_del(&buf->list);
  234. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  235. dprintk(dev, 2, "vid_out buffer %d done\n",
  236. buf->vb.vb2_buf.index);
  237. }
  238. }
  239. if (pstreaming == &dev->vbi_out_streaming) {
  240. while (!list_empty(&dev->vbi_out_active)) {
  241. struct vivid_buffer *buf;
  242. buf = list_entry(dev->vbi_out_active.next,
  243. struct vivid_buffer, list);
  244. list_del(&buf->list);
  245. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  246. dprintk(dev, 2, "vbi_out buffer %d done\n",
  247. buf->vb.vb2_buf.index);
  248. }
  249. }
  250. if (dev->vid_out_streaming || dev->vbi_out_streaming)
  251. return;
  252. /* shutdown control thread */
  253. vivid_grab_controls(dev, false);
  254. kthread_stop(dev->kthread_vid_out);
  255. dev->kthread_vid_out = NULL;
  256. }