vivid-kthread-out.c 9.1 KB

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