vivid-vbi-cap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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, 0);
  90. vivid_g_fmt_vbi_cap(dev, &vbi);
  91. buf->vb.v4l2_buf.sequence = dev->vbi_cap_seq_count;
  92. if (dev->field_cap == V4L2_FIELD_ALTERNATE)
  93. buf->vb.v4l2_buf.sequence /= 2;
  94. vivid_sliced_vbi_cap_fill(dev, buf->vb.v4l2_buf.sequence);
  95. memset(vbuf, 0x10, vb2_plane_size(&buf->vb, 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.v4l2_buf.timestamp);
  99. buf->vb.v4l2_buf.timestamp.tv_sec += dev->time_wrap_offset;
  100. }
  101. void vivid_sliced_vbi_cap_process(struct vivid_dev *dev, struct vivid_buffer *buf)
  102. {
  103. struct v4l2_sliced_vbi_data *vbuf = vb2_plane_vaddr(&buf->vb, 0);
  104. buf->vb.v4l2_buf.sequence = dev->vbi_cap_seq_count;
  105. if (dev->field_cap == V4L2_FIELD_ALTERNATE)
  106. buf->vb.v4l2_buf.sequence /= 2;
  107. vivid_sliced_vbi_cap_fill(dev, buf->vb.v4l2_buf.sequence);
  108. memset(vbuf, 0, vb2_plane_size(&buf->vb, 0));
  109. if (!VIVID_INVALID_SIGNAL(dev->std_signal_mode)) {
  110. unsigned i;
  111. for (i = 0; i < 25; i++)
  112. vbuf[i] = dev->vbi_gen.data[i];
  113. }
  114. v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
  115. buf->vb.v4l2_buf.timestamp.tv_sec += dev->time_wrap_offset;
  116. }
  117. static int vbi_cap_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  118. unsigned *nbuffers, unsigned *nplanes,
  119. unsigned sizes[], void *alloc_ctxs[])
  120. {
  121. struct vivid_dev *dev = vb2_get_drv_priv(vq);
  122. bool is_60hz = dev->std_cap & V4L2_STD_525_60;
  123. unsigned size = vq->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE ?
  124. 36 * sizeof(struct v4l2_sliced_vbi_data) :
  125. 1440 * 2 * (is_60hz ? 12 : 18);
  126. if (!vivid_is_sdtv_cap(dev))
  127. return -EINVAL;
  128. sizes[0] = size;
  129. if (vq->num_buffers + *nbuffers < 2)
  130. *nbuffers = 2 - vq->num_buffers;
  131. *nplanes = 1;
  132. return 0;
  133. }
  134. static int vbi_cap_buf_prepare(struct vb2_buffer *vb)
  135. {
  136. struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  137. bool is_60hz = dev->std_cap & V4L2_STD_525_60;
  138. unsigned size = vb->vb2_queue->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE ?
  139. 36 * sizeof(struct v4l2_sliced_vbi_data) :
  140. 1440 * 2 * (is_60hz ? 12 : 18);
  141. dprintk(dev, 1, "%s\n", __func__);
  142. if (dev->buf_prepare_error) {
  143. /*
  144. * Error injection: test what happens if buf_prepare() returns
  145. * an error.
  146. */
  147. dev->buf_prepare_error = false;
  148. return -EINVAL;
  149. }
  150. if (vb2_plane_size(vb, 0) < size) {
  151. dprintk(dev, 1, "%s data will not fit into plane (%lu < %u)\n",
  152. __func__, vb2_plane_size(vb, 0), size);
  153. return -EINVAL;
  154. }
  155. vb2_set_plane_payload(vb, 0, size);
  156. return 0;
  157. }
  158. static void vbi_cap_buf_queue(struct vb2_buffer *vb)
  159. {
  160. struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  161. struct vivid_buffer *buf = container_of(vb, struct vivid_buffer, vb);
  162. dprintk(dev, 1, "%s\n", __func__);
  163. spin_lock(&dev->slock);
  164. list_add_tail(&buf->list, &dev->vbi_cap_active);
  165. spin_unlock(&dev->slock);
  166. }
  167. static int vbi_cap_start_streaming(struct vb2_queue *vq, unsigned count)
  168. {
  169. struct vivid_dev *dev = vb2_get_drv_priv(vq);
  170. int err;
  171. dprintk(dev, 1, "%s\n", __func__);
  172. dev->vbi_cap_seq_count = 0;
  173. if (dev->start_streaming_error) {
  174. dev->start_streaming_error = false;
  175. err = -EINVAL;
  176. } else {
  177. err = vivid_start_generating_vid_cap(dev, &dev->vbi_cap_streaming);
  178. }
  179. if (err) {
  180. struct vivid_buffer *buf, *tmp;
  181. list_for_each_entry_safe(buf, tmp, &dev->vbi_cap_active, list) {
  182. list_del(&buf->list);
  183. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
  184. }
  185. }
  186. return err;
  187. }
  188. /* abort streaming and wait for last buffer */
  189. static void vbi_cap_stop_streaming(struct vb2_queue *vq)
  190. {
  191. struct vivid_dev *dev = vb2_get_drv_priv(vq);
  192. dprintk(dev, 1, "%s\n", __func__);
  193. vivid_stop_generating_vid_cap(dev, &dev->vbi_cap_streaming);
  194. }
  195. const struct vb2_ops vivid_vbi_cap_qops = {
  196. .queue_setup = vbi_cap_queue_setup,
  197. .buf_prepare = vbi_cap_buf_prepare,
  198. .buf_queue = vbi_cap_buf_queue,
  199. .start_streaming = vbi_cap_start_streaming,
  200. .stop_streaming = vbi_cap_stop_streaming,
  201. .wait_prepare = vb2_ops_wait_prepare,
  202. .wait_finish = vb2_ops_wait_finish,
  203. };
  204. int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  205. struct v4l2_format *f)
  206. {
  207. struct vivid_dev *dev = video_drvdata(file);
  208. struct v4l2_vbi_format *vbi = &f->fmt.vbi;
  209. if (!vivid_is_sdtv_cap(dev) || !dev->has_raw_vbi_cap)
  210. return -EINVAL;
  211. vivid_g_fmt_vbi_cap(dev, vbi);
  212. return 0;
  213. }
  214. int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
  215. struct v4l2_format *f)
  216. {
  217. struct vivid_dev *dev = video_drvdata(file);
  218. int ret = vidioc_g_fmt_vbi_cap(file, priv, f);
  219. if (ret)
  220. return ret;
  221. if (dev->stream_sliced_vbi_cap && vb2_is_busy(&dev->vb_vbi_cap_q))
  222. return -EBUSY;
  223. dev->stream_sliced_vbi_cap = false;
  224. dev->vbi_cap_dev.queue->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  225. return 0;
  226. }
  227. void vivid_fill_service_lines(struct v4l2_sliced_vbi_format *vbi, u32 service_set)
  228. {
  229. vbi->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
  230. vbi->service_set = service_set;
  231. memset(vbi->service_lines, 0, sizeof(vbi->service_lines));
  232. memset(vbi->reserved, 0, sizeof(vbi->reserved));
  233. if (vbi->service_set == 0)
  234. return;
  235. if (vbi->service_set & V4L2_SLICED_CAPTION_525) {
  236. vbi->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
  237. vbi->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
  238. }
  239. if (vbi->service_set & V4L2_SLICED_WSS_625) {
  240. unsigned i;
  241. for (i = 7; i <= 18; i++)
  242. vbi->service_lines[0][i] =
  243. vbi->service_lines[1][i] = V4L2_SLICED_TELETEXT_B;
  244. vbi->service_lines[0][23] = V4L2_SLICED_WSS_625;
  245. }
  246. }
  247. int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  248. {
  249. struct vivid_dev *dev = video_drvdata(file);
  250. struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
  251. if (!vivid_is_sdtv_cap(dev) || !dev->has_sliced_vbi_cap)
  252. return -EINVAL;
  253. vivid_fill_service_lines(vbi, dev->service_set_cap);
  254. return 0;
  255. }
  256. int vidioc_try_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  257. {
  258. struct vivid_dev *dev = video_drvdata(file);
  259. struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
  260. bool is_60hz = dev->std_cap & V4L2_STD_525_60;
  261. u32 service_set = vbi->service_set;
  262. if (!vivid_is_sdtv_cap(dev) || !dev->has_sliced_vbi_cap)
  263. return -EINVAL;
  264. service_set &= is_60hz ? V4L2_SLICED_CAPTION_525 :
  265. V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
  266. vivid_fill_service_lines(vbi, service_set);
  267. return 0;
  268. }
  269. int vidioc_s_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  270. {
  271. struct vivid_dev *dev = video_drvdata(file);
  272. struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
  273. int ret = vidioc_try_fmt_sliced_vbi_cap(file, fh, fmt);
  274. if (ret)
  275. return ret;
  276. if (!dev->stream_sliced_vbi_cap && vb2_is_busy(&dev->vb_vbi_cap_q))
  277. return -EBUSY;
  278. dev->service_set_cap = vbi->service_set;
  279. dev->stream_sliced_vbi_cap = true;
  280. dev->vbi_cap_dev.queue->type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
  281. return 0;
  282. }
  283. int vidioc_g_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_sliced_vbi_cap *cap)
  284. {
  285. struct vivid_dev *dev = video_drvdata(file);
  286. struct video_device *vdev = video_devdata(file);
  287. bool is_60hz;
  288. if (vdev->vfl_dir == VFL_DIR_RX) {
  289. is_60hz = dev->std_cap & V4L2_STD_525_60;
  290. if (!vivid_is_sdtv_cap(dev) || !dev->has_sliced_vbi_cap ||
  291. cap->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
  292. return -EINVAL;
  293. } else {
  294. is_60hz = dev->std_out & V4L2_STD_525_60;
  295. if (!vivid_is_svid_out(dev) || !dev->has_sliced_vbi_out ||
  296. cap->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT)
  297. return -EINVAL;
  298. }
  299. cap->service_set = is_60hz ? V4L2_SLICED_CAPTION_525 :
  300. V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
  301. if (is_60hz) {
  302. cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
  303. cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
  304. } else {
  305. unsigned i;
  306. for (i = 7; i <= 18; i++)
  307. cap->service_lines[0][i] =
  308. cap->service_lines[1][i] = V4L2_SLICED_TELETEXT_B;
  309. cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
  310. }
  311. return 0;
  312. }