vivid-kthread-out.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. mutex_lock(&dev->mutex);
  123. cur_jiffies = jiffies;
  124. if (dev->out_seq_resync) {
  125. dev->jiffies_vid_out = cur_jiffies;
  126. dev->out_seq_offset = dev->out_seq_count + 1;
  127. dev->out_seq_count = 0;
  128. dev->out_seq_resync = false;
  129. }
  130. numerator = dev->timeperframe_vid_out.numerator;
  131. denominator = dev->timeperframe_vid_out.denominator;
  132. if (dev->field_out == V4L2_FIELD_ALTERNATE)
  133. denominator *= 2;
  134. /* Calculate the number of jiffies since we started streaming */
  135. jiffies_since_start = cur_jiffies - dev->jiffies_vid_out;
  136. /* Get the number of buffers streamed since the start */
  137. buffers_since_start = (u64)jiffies_since_start * denominator +
  138. (HZ * numerator) / 2;
  139. do_div(buffers_since_start, HZ * numerator);
  140. /*
  141. * After more than 0xf0000000 (rounded down to a multiple of
  142. * 'jiffies-per-day' to ease jiffies_to_msecs calculation)
  143. * jiffies have passed since we started streaming reset the
  144. * counters and keep track of the sequence offset.
  145. */
  146. if (jiffies_since_start > JIFFIES_RESYNC) {
  147. dev->jiffies_vid_out = cur_jiffies;
  148. dev->out_seq_offset = buffers_since_start;
  149. buffers_since_start = 0;
  150. }
  151. dev->out_seq_count = buffers_since_start + dev->out_seq_offset;
  152. dev->vid_out_seq_count = dev->out_seq_count - dev->vid_out_seq_start;
  153. dev->vbi_out_seq_count = dev->out_seq_count - dev->vbi_out_seq_start;
  154. vivid_thread_vid_out_tick(dev);
  155. mutex_unlock(&dev->mutex);
  156. /*
  157. * Calculate the number of 'numerators' streamed since we started,
  158. * not including the current buffer.
  159. */
  160. numerators_since_start = buffers_since_start * numerator;
  161. /* And the number of jiffies since we started */
  162. jiffies_since_start = jiffies - dev->jiffies_vid_out;
  163. /* Increase by the 'numerator' of one buffer */
  164. numerators_since_start += numerator;
  165. /*
  166. * Calculate when that next buffer is supposed to start
  167. * in jiffies since we started streaming.
  168. */
  169. next_jiffies_since_start = numerators_since_start * HZ +
  170. denominator / 2;
  171. do_div(next_jiffies_since_start, denominator);
  172. /* If it is in the past, then just schedule asap */
  173. if (next_jiffies_since_start < jiffies_since_start)
  174. next_jiffies_since_start = jiffies_since_start;
  175. wait_jiffies = next_jiffies_since_start - jiffies_since_start;
  176. schedule_timeout_interruptible(wait_jiffies ? wait_jiffies : 1);
  177. }
  178. dprintk(dev, 1, "Video Output Thread End\n");
  179. return 0;
  180. }
  181. static void vivid_grab_controls(struct vivid_dev *dev, bool grab)
  182. {
  183. v4l2_ctrl_grab(dev->ctrl_has_crop_out, grab);
  184. v4l2_ctrl_grab(dev->ctrl_has_compose_out, grab);
  185. v4l2_ctrl_grab(dev->ctrl_has_scaler_out, grab);
  186. v4l2_ctrl_grab(dev->ctrl_tx_mode, grab);
  187. v4l2_ctrl_grab(dev->ctrl_tx_rgb_range, grab);
  188. }
  189. int vivid_start_generating_vid_out(struct vivid_dev *dev, bool *pstreaming)
  190. {
  191. dprintk(dev, 1, "%s\n", __func__);
  192. if (dev->kthread_vid_out) {
  193. u32 seq_count = dev->out_seq_count + dev->seq_wrap * 128;
  194. if (pstreaming == &dev->vid_out_streaming)
  195. dev->vid_out_seq_start = seq_count;
  196. else
  197. dev->vbi_out_seq_start = seq_count;
  198. *pstreaming = true;
  199. return 0;
  200. }
  201. /* Resets frame counters */
  202. dev->jiffies_vid_out = jiffies;
  203. dev->vid_out_seq_start = dev->seq_wrap * 128;
  204. dev->vbi_out_seq_start = dev->seq_wrap * 128;
  205. dev->kthread_vid_out = kthread_run(vivid_thread_vid_out, dev,
  206. "%s-vid-out", dev->v4l2_dev.name);
  207. if (IS_ERR(dev->kthread_vid_out)) {
  208. v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
  209. return PTR_ERR(dev->kthread_vid_out);
  210. }
  211. *pstreaming = true;
  212. vivid_grab_controls(dev, true);
  213. dprintk(dev, 1, "returning from %s\n", __func__);
  214. return 0;
  215. }
  216. void vivid_stop_generating_vid_out(struct vivid_dev *dev, bool *pstreaming)
  217. {
  218. dprintk(dev, 1, "%s\n", __func__);
  219. if (dev->kthread_vid_out == NULL)
  220. return;
  221. *pstreaming = false;
  222. if (pstreaming == &dev->vid_out_streaming) {
  223. /* Release all active buffers */
  224. while (!list_empty(&dev->vid_out_active)) {
  225. struct vivid_buffer *buf;
  226. buf = list_entry(dev->vid_out_active.next,
  227. struct vivid_buffer, list);
  228. list_del(&buf->list);
  229. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  230. dprintk(dev, 2, "vid_out buffer %d done\n",
  231. buf->vb.vb2_buf.index);
  232. }
  233. }
  234. if (pstreaming == &dev->vbi_out_streaming) {
  235. while (!list_empty(&dev->vbi_out_active)) {
  236. struct vivid_buffer *buf;
  237. buf = list_entry(dev->vbi_out_active.next,
  238. struct vivid_buffer, list);
  239. list_del(&buf->list);
  240. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  241. dprintk(dev, 2, "vbi_out buffer %d done\n",
  242. buf->vb.vb2_buf.index);
  243. }
  244. }
  245. if (dev->vid_out_streaming || dev->vbi_out_streaming)
  246. return;
  247. /* shutdown control thread */
  248. vivid_grab_controls(dev, false);
  249. mutex_unlock(&dev->mutex);
  250. kthread_stop(dev->kthread_vid_out);
  251. dev->kthread_vid_out = NULL;
  252. mutex_lock(&dev->mutex);
  253. }