vivid-vbi-cap.c 11 KB

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