vivid-vbi-out.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * vivid-vbi-out.c - vbi output 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-out.h"
  25. #include "vivid-vbi-out.h"
  26. #include "vivid-vbi-cap.h"
  27. static int vbi_out_queue_setup(struct vb2_queue *vq, const void *parg,
  28. unsigned *nbuffers, unsigned *nplanes,
  29. unsigned sizes[], void *alloc_ctxs[])
  30. {
  31. struct vivid_dev *dev = vb2_get_drv_priv(vq);
  32. bool is_60hz = dev->std_out & V4L2_STD_525_60;
  33. unsigned size = vq->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT ?
  34. 36 * sizeof(struct v4l2_sliced_vbi_data) :
  35. 1440 * 2 * (is_60hz ? 12 : 18);
  36. if (!vivid_is_svid_out(dev))
  37. return -EINVAL;
  38. sizes[0] = size;
  39. if (vq->num_buffers + *nbuffers < 2)
  40. *nbuffers = 2 - vq->num_buffers;
  41. *nplanes = 1;
  42. return 0;
  43. }
  44. static int vbi_out_buf_prepare(struct vb2_buffer *vb)
  45. {
  46. struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  47. bool is_60hz = dev->std_out & V4L2_STD_525_60;
  48. unsigned size = vb->vb2_queue->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT ?
  49. 36 * sizeof(struct v4l2_sliced_vbi_data) :
  50. 1440 * 2 * (is_60hz ? 12 : 18);
  51. dprintk(dev, 1, "%s\n", __func__);
  52. if (dev->buf_prepare_error) {
  53. /*
  54. * Error injection: test what happens if buf_prepare() returns
  55. * an error.
  56. */
  57. dev->buf_prepare_error = false;
  58. return -EINVAL;
  59. }
  60. if (vb2_plane_size(vb, 0) < size) {
  61. dprintk(dev, 1, "%s data will not fit into plane (%lu < %u)\n",
  62. __func__, vb2_plane_size(vb, 0), size);
  63. return -EINVAL;
  64. }
  65. vb2_set_plane_payload(vb, 0, size);
  66. return 0;
  67. }
  68. static void vbi_out_buf_queue(struct vb2_buffer *vb)
  69. {
  70. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  71. struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  72. struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
  73. dprintk(dev, 1, "%s\n", __func__);
  74. spin_lock(&dev->slock);
  75. list_add_tail(&buf->list, &dev->vbi_out_active);
  76. spin_unlock(&dev->slock);
  77. }
  78. static int vbi_out_start_streaming(struct vb2_queue *vq, unsigned count)
  79. {
  80. struct vivid_dev *dev = vb2_get_drv_priv(vq);
  81. int err;
  82. dprintk(dev, 1, "%s\n", __func__);
  83. dev->vbi_out_seq_count = 0;
  84. if (dev->start_streaming_error) {
  85. dev->start_streaming_error = false;
  86. err = -EINVAL;
  87. } else {
  88. err = vivid_start_generating_vid_out(dev, &dev->vbi_out_streaming);
  89. }
  90. if (err) {
  91. struct vivid_buffer *buf, *tmp;
  92. list_for_each_entry_safe(buf, tmp, &dev->vbi_out_active, list) {
  93. list_del(&buf->list);
  94. vb2_buffer_done(&buf->vb.vb2_buf,
  95. VB2_BUF_STATE_QUEUED);
  96. }
  97. }
  98. return err;
  99. }
  100. /* abort streaming and wait for last buffer */
  101. static void vbi_out_stop_streaming(struct vb2_queue *vq)
  102. {
  103. struct vivid_dev *dev = vb2_get_drv_priv(vq);
  104. dprintk(dev, 1, "%s\n", __func__);
  105. vivid_stop_generating_vid_out(dev, &dev->vbi_out_streaming);
  106. dev->vbi_out_have_wss = false;
  107. dev->vbi_out_have_cc[0] = false;
  108. dev->vbi_out_have_cc[1] = false;
  109. }
  110. const struct vb2_ops vivid_vbi_out_qops = {
  111. .queue_setup = vbi_out_queue_setup,
  112. .buf_prepare = vbi_out_buf_prepare,
  113. .buf_queue = vbi_out_buf_queue,
  114. .start_streaming = vbi_out_start_streaming,
  115. .stop_streaming = vbi_out_stop_streaming,
  116. .wait_prepare = vb2_ops_wait_prepare,
  117. .wait_finish = vb2_ops_wait_finish,
  118. };
  119. int vidioc_g_fmt_vbi_out(struct file *file, void *priv,
  120. struct v4l2_format *f)
  121. {
  122. struct vivid_dev *dev = video_drvdata(file);
  123. struct v4l2_vbi_format *vbi = &f->fmt.vbi;
  124. bool is_60hz = dev->std_out & V4L2_STD_525_60;
  125. if (!vivid_is_svid_out(dev) || !dev->has_raw_vbi_out)
  126. return -EINVAL;
  127. vbi->sampling_rate = 25000000;
  128. vbi->offset = 24;
  129. vbi->samples_per_line = 1440;
  130. vbi->sample_format = V4L2_PIX_FMT_GREY;
  131. vbi->start[0] = is_60hz ? V4L2_VBI_ITU_525_F1_START + 9 : V4L2_VBI_ITU_625_F1_START + 5;
  132. vbi->start[1] = is_60hz ? V4L2_VBI_ITU_525_F2_START + 9 : V4L2_VBI_ITU_625_F2_START + 5;
  133. vbi->count[0] = vbi->count[1] = is_60hz ? 12 : 18;
  134. vbi->flags = dev->vbi_cap_interlaced ? V4L2_VBI_INTERLACED : 0;
  135. vbi->reserved[0] = 0;
  136. vbi->reserved[1] = 0;
  137. return 0;
  138. }
  139. int vidioc_s_fmt_vbi_out(struct file *file, void *priv,
  140. struct v4l2_format *f)
  141. {
  142. struct vivid_dev *dev = video_drvdata(file);
  143. int ret = vidioc_g_fmt_vbi_out(file, priv, f);
  144. if (ret)
  145. return ret;
  146. if (vb2_is_busy(&dev->vb_vbi_out_q))
  147. return -EBUSY;
  148. dev->stream_sliced_vbi_out = false;
  149. dev->vbi_out_dev.queue->type = V4L2_BUF_TYPE_VBI_OUTPUT;
  150. return 0;
  151. }
  152. int vidioc_g_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
  153. {
  154. struct vivid_dev *dev = video_drvdata(file);
  155. struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
  156. if (!vivid_is_svid_out(dev) || !dev->has_sliced_vbi_out)
  157. return -EINVAL;
  158. vivid_fill_service_lines(vbi, dev->service_set_out);
  159. return 0;
  160. }
  161. int vidioc_try_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
  162. {
  163. struct vivid_dev *dev = video_drvdata(file);
  164. struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
  165. bool is_60hz = dev->std_out & V4L2_STD_525_60;
  166. u32 service_set = vbi->service_set;
  167. if (!vivid_is_svid_out(dev) || !dev->has_sliced_vbi_out)
  168. return -EINVAL;
  169. service_set &= is_60hz ? V4L2_SLICED_CAPTION_525 :
  170. V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
  171. vivid_fill_service_lines(vbi, service_set);
  172. return 0;
  173. }
  174. int vidioc_s_fmt_sliced_vbi_out(struct file *file, void *fh,
  175. struct v4l2_format *fmt)
  176. {
  177. struct vivid_dev *dev = video_drvdata(file);
  178. struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
  179. int ret = vidioc_try_fmt_sliced_vbi_out(file, fh, fmt);
  180. if (ret)
  181. return ret;
  182. if (vb2_is_busy(&dev->vb_vbi_out_q))
  183. return -EBUSY;
  184. dev->service_set_out = vbi->service_set;
  185. dev->stream_sliced_vbi_out = true;
  186. dev->vbi_out_dev.queue->type = V4L2_BUF_TYPE_SLICED_VBI_OUTPUT;
  187. return 0;
  188. }
  189. void vivid_sliced_vbi_out_process(struct vivid_dev *dev,
  190. struct vivid_buffer *buf)
  191. {
  192. struct v4l2_sliced_vbi_data *vbi =
  193. vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
  194. unsigned elems =
  195. vb2_get_plane_payload(&buf->vb.vb2_buf, 0) / sizeof(*vbi);
  196. dev->vbi_out_have_cc[0] = false;
  197. dev->vbi_out_have_cc[1] = false;
  198. dev->vbi_out_have_wss = false;
  199. while (elems--) {
  200. switch (vbi->id) {
  201. case V4L2_SLICED_CAPTION_525:
  202. if ((dev->std_out & V4L2_STD_525_60) && vbi->line == 21) {
  203. dev->vbi_out_have_cc[!!vbi->field] = true;
  204. dev->vbi_out_cc[!!vbi->field][0] = vbi->data[0];
  205. dev->vbi_out_cc[!!vbi->field][1] = vbi->data[1];
  206. }
  207. break;
  208. case V4L2_SLICED_WSS_625:
  209. if ((dev->std_out & V4L2_STD_625_50) &&
  210. vbi->field == 0 && vbi->line == 23) {
  211. dev->vbi_out_have_wss = true;
  212. dev->vbi_out_wss[0] = vbi->data[0];
  213. dev->vbi_out_wss[1] = vbi->data[1];
  214. }
  215. break;
  216. }
  217. vbi++;
  218. }
  219. }