vivid-vbi-cap.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * vivid-vbi-cap.c - vbi capture support functions.
  4. *
  5. * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/kernel.h>
  9. #include <linux/videodev2.h>
  10. #include <media/v4l2-common.h>
  11. #include "vivid-core.h"
  12. #include "vivid-kthread-cap.h"
  13. #include "vivid-vbi-cap.h"
  14. #include "vivid-vbi-gen.h"
  15. static void vivid_sliced_vbi_cap_fill(struct vivid_dev *dev, unsigned seqnr)
  16. {
  17. struct vivid_vbi_gen_data *vbi_gen = &dev->vbi_gen;
  18. bool is_60hz = dev->std_cap & V4L2_STD_525_60;
  19. vivid_vbi_gen_sliced(vbi_gen, is_60hz, seqnr);
  20. if (!is_60hz) {
  21. if (dev->loop_video) {
  22. if (dev->vbi_out_have_wss) {
  23. vbi_gen->data[12].data[0] = dev->vbi_out_wss[0];
  24. vbi_gen->data[12].data[1] = dev->vbi_out_wss[1];
  25. } else {
  26. vbi_gen->data[12].id = 0;
  27. }
  28. } else {
  29. switch (tpg_g_video_aspect(&dev->tpg)) {
  30. case TPG_VIDEO_ASPECT_14X9_CENTRE:
  31. vbi_gen->data[12].data[0] = 0x01;
  32. break;
  33. case TPG_VIDEO_ASPECT_16X9_CENTRE:
  34. vbi_gen->data[12].data[0] = 0x0b;
  35. break;
  36. case TPG_VIDEO_ASPECT_16X9_ANAMORPHIC:
  37. vbi_gen->data[12].data[0] = 0x07;
  38. break;
  39. case TPG_VIDEO_ASPECT_4X3:
  40. default:
  41. vbi_gen->data[12].data[0] = 0x08;
  42. break;
  43. }
  44. }
  45. } else if (dev->loop_video && is_60hz) {
  46. if (dev->vbi_out_have_cc[0]) {
  47. vbi_gen->data[0].data[0] = dev->vbi_out_cc[0][0];
  48. vbi_gen->data[0].data[1] = dev->vbi_out_cc[0][1];
  49. } else {
  50. vbi_gen->data[0].id = 0;
  51. }
  52. if (dev->vbi_out_have_cc[1]) {
  53. vbi_gen->data[1].data[0] = dev->vbi_out_cc[1][0];
  54. vbi_gen->data[1].data[1] = dev->vbi_out_cc[1][1];
  55. } else {
  56. vbi_gen->data[1].id = 0;
  57. }
  58. }
  59. }
  60. static void vivid_g_fmt_vbi_cap(struct vivid_dev *dev, struct v4l2_vbi_format *vbi)
  61. {
  62. bool is_60hz = dev->std_cap & V4L2_STD_525_60;
  63. vbi->sampling_rate = 27000000;
  64. vbi->offset = 24;
  65. vbi->samples_per_line = 1440;
  66. vbi->sample_format = V4L2_PIX_FMT_GREY;
  67. vbi->start[0] = is_60hz ? V4L2_VBI_ITU_525_F1_START + 9 : V4L2_VBI_ITU_625_F1_START + 5;
  68. vbi->start[1] = is_60hz ? V4L2_VBI_ITU_525_F2_START + 9 : V4L2_VBI_ITU_625_F2_START + 5;
  69. vbi->count[0] = vbi->count[1] = is_60hz ? 12 : 18;
  70. vbi->flags = dev->vbi_cap_interlaced ? V4L2_VBI_INTERLACED : 0;
  71. vbi->reserved[0] = 0;
  72. vbi->reserved[1] = 0;
  73. }
  74. void vivid_raw_vbi_cap_process(struct vivid_dev *dev, struct vivid_buffer *buf)
  75. {
  76. struct v4l2_vbi_format vbi;
  77. u8 *vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
  78. vivid_g_fmt_vbi_cap(dev, &vbi);
  79. buf->vb.sequence = dev->vbi_cap_seq_count;
  80. if (dev->field_cap == V4L2_FIELD_ALTERNATE)
  81. buf->vb.sequence /= 2;
  82. vivid_sliced_vbi_cap_fill(dev, buf->vb.sequence);
  83. memset(vbuf, 0x10, vb2_plane_size(&buf->vb.vb2_buf, 0));
  84. if (!VIVID_INVALID_SIGNAL(dev->std_signal_mode))
  85. vivid_vbi_gen_raw(&dev->vbi_gen, &vbi, vbuf);
  86. buf->vb.vb2_buf.timestamp = ktime_get_ns() + dev->time_wrap_offset;
  87. }
  88. void vivid_sliced_vbi_cap_process(struct vivid_dev *dev,
  89. struct vivid_buffer *buf)
  90. {
  91. struct v4l2_sliced_vbi_data *vbuf =
  92. vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
  93. buf->vb.sequence = dev->vbi_cap_seq_count;
  94. if (dev->field_cap == V4L2_FIELD_ALTERNATE)
  95. buf->vb.sequence /= 2;
  96. vivid_sliced_vbi_cap_fill(dev, buf->vb.sequence);
  97. memset(vbuf, 0, vb2_plane_size(&buf->vb.vb2_buf, 0));
  98. if (!VIVID_INVALID_SIGNAL(dev->std_signal_mode)) {
  99. unsigned i;
  100. for (i = 0; i < 25; i++)
  101. vbuf[i] = dev->vbi_gen.data[i];
  102. }
  103. buf->vb.vb2_buf.timestamp = ktime_get_ns() + dev->time_wrap_offset;
  104. }
  105. static int vbi_cap_queue_setup(struct vb2_queue *vq,
  106. unsigned *nbuffers, unsigned *nplanes,
  107. unsigned sizes[], struct device *alloc_devs[])
  108. {
  109. struct vivid_dev *dev = vb2_get_drv_priv(vq);
  110. bool is_60hz = dev->std_cap & V4L2_STD_525_60;
  111. unsigned size = vq->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE ?
  112. 36 * sizeof(struct v4l2_sliced_vbi_data) :
  113. 1440 * 2 * (is_60hz ? 12 : 18);
  114. if (!vivid_is_sdtv_cap(dev))
  115. return -EINVAL;
  116. sizes[0] = size;
  117. if (vq->num_buffers + *nbuffers < 2)
  118. *nbuffers = 2 - vq->num_buffers;
  119. *nplanes = 1;
  120. return 0;
  121. }
  122. static int vbi_cap_buf_prepare(struct vb2_buffer *vb)
  123. {
  124. struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  125. bool is_60hz = dev->std_cap & V4L2_STD_525_60;
  126. unsigned size = vb->vb2_queue->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE ?
  127. 36 * sizeof(struct v4l2_sliced_vbi_data) :
  128. 1440 * 2 * (is_60hz ? 12 : 18);
  129. dprintk(dev, 1, "%s\n", __func__);
  130. if (dev->buf_prepare_error) {
  131. /*
  132. * Error injection: test what happens if buf_prepare() returns
  133. * an error.
  134. */
  135. dev->buf_prepare_error = false;
  136. return -EINVAL;
  137. }
  138. if (vb2_plane_size(vb, 0) < size) {
  139. dprintk(dev, 1, "%s data will not fit into plane (%lu < %u)\n",
  140. __func__, vb2_plane_size(vb, 0), size);
  141. return -EINVAL;
  142. }
  143. vb2_set_plane_payload(vb, 0, size);
  144. return 0;
  145. }
  146. static void vbi_cap_buf_queue(struct vb2_buffer *vb)
  147. {
  148. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  149. struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  150. struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
  151. dprintk(dev, 1, "%s\n", __func__);
  152. spin_lock(&dev->slock);
  153. list_add_tail(&buf->list, &dev->vbi_cap_active);
  154. spin_unlock(&dev->slock);
  155. }
  156. static int vbi_cap_start_streaming(struct vb2_queue *vq, unsigned count)
  157. {
  158. struct vivid_dev *dev = vb2_get_drv_priv(vq);
  159. int err;
  160. dprintk(dev, 1, "%s\n", __func__);
  161. dev->vbi_cap_seq_count = 0;
  162. if (dev->start_streaming_error) {
  163. dev->start_streaming_error = false;
  164. err = -EINVAL;
  165. } else {
  166. err = vivid_start_generating_vid_cap(dev, &dev->vbi_cap_streaming);
  167. }
  168. if (err) {
  169. struct vivid_buffer *buf, *tmp;
  170. list_for_each_entry_safe(buf, tmp, &dev->vbi_cap_active, list) {
  171. list_del(&buf->list);
  172. vb2_buffer_done(&buf->vb.vb2_buf,
  173. VB2_BUF_STATE_QUEUED);
  174. }
  175. }
  176. return err;
  177. }
  178. /* abort streaming and wait for last buffer */
  179. static void vbi_cap_stop_streaming(struct vb2_queue *vq)
  180. {
  181. struct vivid_dev *dev = vb2_get_drv_priv(vq);
  182. dprintk(dev, 1, "%s\n", __func__);
  183. vivid_stop_generating_vid_cap(dev, &dev->vbi_cap_streaming);
  184. }
  185. const struct vb2_ops vivid_vbi_cap_qops = {
  186. .queue_setup = vbi_cap_queue_setup,
  187. .buf_prepare = vbi_cap_buf_prepare,
  188. .buf_queue = vbi_cap_buf_queue,
  189. .start_streaming = vbi_cap_start_streaming,
  190. .stop_streaming = vbi_cap_stop_streaming,
  191. .wait_prepare = vb2_ops_wait_prepare,
  192. .wait_finish = vb2_ops_wait_finish,
  193. };
  194. int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  195. struct v4l2_format *f)
  196. {
  197. struct vivid_dev *dev = video_drvdata(file);
  198. struct v4l2_vbi_format *vbi = &f->fmt.vbi;
  199. if (!vivid_is_sdtv_cap(dev) || !dev->has_raw_vbi_cap)
  200. return -EINVAL;
  201. vivid_g_fmt_vbi_cap(dev, vbi);
  202. return 0;
  203. }
  204. int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
  205. struct v4l2_format *f)
  206. {
  207. struct vivid_dev *dev = video_drvdata(file);
  208. int ret = vidioc_g_fmt_vbi_cap(file, priv, f);
  209. if (ret)
  210. return ret;
  211. if (dev->stream_sliced_vbi_cap && vb2_is_busy(&dev->vb_vbi_cap_q))
  212. return -EBUSY;
  213. dev->stream_sliced_vbi_cap = false;
  214. dev->vbi_cap_dev.queue->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  215. return 0;
  216. }
  217. void vivid_fill_service_lines(struct v4l2_sliced_vbi_format *vbi, u32 service_set)
  218. {
  219. vbi->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
  220. vbi->service_set = service_set;
  221. memset(vbi->service_lines, 0, sizeof(vbi->service_lines));
  222. memset(vbi->reserved, 0, sizeof(vbi->reserved));
  223. if (vbi->service_set == 0)
  224. return;
  225. if (vbi->service_set & V4L2_SLICED_CAPTION_525) {
  226. vbi->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
  227. vbi->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
  228. }
  229. if (vbi->service_set & V4L2_SLICED_WSS_625) {
  230. unsigned i;
  231. for (i = 7; i <= 18; i++)
  232. vbi->service_lines[0][i] =
  233. vbi->service_lines[1][i] = V4L2_SLICED_TELETEXT_B;
  234. vbi->service_lines[0][23] = V4L2_SLICED_WSS_625;
  235. }
  236. }
  237. int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  238. {
  239. struct vivid_dev *dev = video_drvdata(file);
  240. struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
  241. if (!vivid_is_sdtv_cap(dev) || !dev->has_sliced_vbi_cap)
  242. return -EINVAL;
  243. vivid_fill_service_lines(vbi, dev->service_set_cap);
  244. return 0;
  245. }
  246. int vidioc_try_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  247. {
  248. struct vivid_dev *dev = video_drvdata(file);
  249. struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
  250. bool is_60hz = dev->std_cap & V4L2_STD_525_60;
  251. u32 service_set = vbi->service_set;
  252. if (!vivid_is_sdtv_cap(dev) || !dev->has_sliced_vbi_cap)
  253. return -EINVAL;
  254. service_set &= is_60hz ? V4L2_SLICED_CAPTION_525 :
  255. V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
  256. vivid_fill_service_lines(vbi, service_set);
  257. return 0;
  258. }
  259. int vidioc_s_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  260. {
  261. struct vivid_dev *dev = video_drvdata(file);
  262. struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
  263. int ret = vidioc_try_fmt_sliced_vbi_cap(file, fh, fmt);
  264. if (ret)
  265. return ret;
  266. if (!dev->stream_sliced_vbi_cap && vb2_is_busy(&dev->vb_vbi_cap_q))
  267. return -EBUSY;
  268. dev->service_set_cap = vbi->service_set;
  269. dev->stream_sliced_vbi_cap = true;
  270. dev->vbi_cap_dev.queue->type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
  271. return 0;
  272. }
  273. int vidioc_g_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_sliced_vbi_cap *cap)
  274. {
  275. struct vivid_dev *dev = video_drvdata(file);
  276. struct video_device *vdev = video_devdata(file);
  277. bool is_60hz;
  278. if (vdev->vfl_dir == VFL_DIR_RX) {
  279. is_60hz = dev->std_cap & V4L2_STD_525_60;
  280. if (!vivid_is_sdtv_cap(dev) || !dev->has_sliced_vbi_cap ||
  281. cap->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
  282. return -EINVAL;
  283. } else {
  284. is_60hz = dev->std_out & V4L2_STD_525_60;
  285. if (!vivid_is_svid_out(dev) || !dev->has_sliced_vbi_out ||
  286. cap->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT)
  287. return -EINVAL;
  288. }
  289. cap->service_set = is_60hz ? V4L2_SLICED_CAPTION_525 :
  290. V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
  291. if (is_60hz) {
  292. cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
  293. cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
  294. } else {
  295. unsigned i;
  296. for (i = 7; i <= 18; i++)
  297. cap->service_lines[0][i] =
  298. cap->service_lines[1][i] = V4L2_SLICED_TELETEXT_B;
  299. cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
  300. }
  301. return 0;
  302. }