vivid-kthread-out.c 9.1 KB

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