vivid-vbi-out.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 struct v4l2_format *fmt,
  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 vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  71. struct vivid_buffer *buf = container_of(vb, struct vivid_buffer, vb);
  72. dprintk(dev, 1, "%s\n", __func__);
  73. spin_lock(&dev->slock);
  74. list_add_tail(&buf->list, &dev->vbi_out_active);
  75. spin_unlock(&dev->slock);
  76. }
  77. static int vbi_out_start_streaming(struct vb2_queue *vq, unsigned count)
  78. {
  79. struct vivid_dev *dev = vb2_get_drv_priv(vq);
  80. int err;
  81. dprintk(dev, 1, "%s\n", __func__);
  82. dev->vbi_out_seq_count = 0;
  83. if (dev->start_streaming_error) {
  84. dev->start_streaming_error = false;
  85. err = -EINVAL;
  86. } else {
  87. err = vivid_start_generating_vid_out(dev, &dev->vbi_out_streaming);
  88. }
  89. if (err) {
  90. struct vivid_buffer *buf, *tmp;
  91. list_for_each_entry_safe(buf, tmp, &dev->vbi_out_active, list) {
  92. list_del(&buf->list);
  93. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
  94. }
  95. }
  96. return err;
  97. }
  98. /* abort streaming and wait for last buffer */
  99. static void vbi_out_stop_streaming(struct vb2_queue *vq)
  100. {
  101. struct vivid_dev *dev = vb2_get_drv_priv(vq);
  102. dprintk(dev, 1, "%s\n", __func__);
  103. vivid_stop_generating_vid_out(dev, &dev->vbi_out_streaming);
  104. dev->vbi_out_have_wss = false;
  105. dev->vbi_out_have_cc[0] = false;
  106. dev->vbi_out_have_cc[1] = false;
  107. }
  108. const struct vb2_ops vivid_vbi_out_qops = {
  109. .queue_setup = vbi_out_queue_setup,
  110. .buf_prepare = vbi_out_buf_prepare,
  111. .buf_queue = vbi_out_buf_queue,
  112. .start_streaming = vbi_out_start_streaming,
  113. .stop_streaming = vbi_out_stop_streaming,
  114. .wait_prepare = vb2_ops_wait_prepare,
  115. .wait_finish = vb2_ops_wait_finish,
  116. };
  117. int vidioc_g_fmt_vbi_out(struct file *file, void *priv,
  118. struct v4l2_format *f)
  119. {
  120. struct vivid_dev *dev = video_drvdata(file);
  121. struct v4l2_vbi_format *vbi = &f->fmt.vbi;
  122. bool is_60hz = dev->std_out & V4L2_STD_525_60;
  123. if (!vivid_is_svid_out(dev) || !dev->has_raw_vbi_out)
  124. return -EINVAL;
  125. vbi->sampling_rate = 25000000;
  126. vbi->offset = 24;
  127. vbi->samples_per_line = 1440;
  128. vbi->sample_format = V4L2_PIX_FMT_GREY;
  129. vbi->start[0] = is_60hz ? V4L2_VBI_ITU_525_F1_START + 9 : V4L2_VBI_ITU_625_F1_START + 5;
  130. vbi->start[1] = is_60hz ? V4L2_VBI_ITU_525_F2_START + 9 : V4L2_VBI_ITU_625_F2_START + 5;
  131. vbi->count[0] = vbi->count[1] = is_60hz ? 12 : 18;
  132. vbi->flags = dev->vbi_cap_interlaced ? V4L2_VBI_INTERLACED : 0;
  133. vbi->reserved[0] = 0;
  134. vbi->reserved[1] = 0;
  135. return 0;
  136. }
  137. int vidioc_s_fmt_vbi_out(struct file *file, void *priv,
  138. struct v4l2_format *f)
  139. {
  140. struct vivid_dev *dev = video_drvdata(file);
  141. int ret = vidioc_g_fmt_vbi_out(file, priv, f);
  142. if (ret)
  143. return ret;
  144. if (vb2_is_busy(&dev->vb_vbi_out_q))
  145. return -EBUSY;
  146. dev->stream_sliced_vbi_out = false;
  147. dev->vbi_out_dev.queue->type = V4L2_BUF_TYPE_VBI_OUTPUT;
  148. return 0;
  149. }
  150. int vidioc_g_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
  151. {
  152. struct vivid_dev *dev = video_drvdata(file);
  153. struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
  154. if (!vivid_is_svid_out(dev) || !dev->has_sliced_vbi_out)
  155. return -EINVAL;
  156. vivid_fill_service_lines(vbi, dev->service_set_out);
  157. return 0;
  158. }
  159. int vidioc_try_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
  160. {
  161. struct vivid_dev *dev = video_drvdata(file);
  162. struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
  163. bool is_60hz = dev->std_out & V4L2_STD_525_60;
  164. u32 service_set = vbi->service_set;
  165. if (!vivid_is_svid_out(dev) || !dev->has_sliced_vbi_out)
  166. return -EINVAL;
  167. service_set &= is_60hz ? V4L2_SLICED_CAPTION_525 :
  168. V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
  169. vivid_fill_service_lines(vbi, service_set);
  170. return 0;
  171. }
  172. int vidioc_s_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
  173. {
  174. struct vivid_dev *dev = video_drvdata(file);
  175. struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
  176. int ret = vidioc_try_fmt_sliced_vbi_out(file, fh, fmt);
  177. if (ret)
  178. return ret;
  179. if (vb2_is_busy(&dev->vb_vbi_out_q))
  180. return -EBUSY;
  181. dev->service_set_out = vbi->service_set;
  182. dev->stream_sliced_vbi_out = true;
  183. dev->vbi_out_dev.queue->type = V4L2_BUF_TYPE_SLICED_VBI_OUTPUT;
  184. return 0;
  185. }
  186. void vivid_sliced_vbi_out_process(struct vivid_dev *dev, struct vivid_buffer *buf)
  187. {
  188. struct v4l2_sliced_vbi_data *vbi = vb2_plane_vaddr(&buf->vb, 0);
  189. unsigned elems = vb2_get_plane_payload(&buf->vb, 0) / sizeof(*vbi);
  190. dev->vbi_out_have_cc[0] = false;
  191. dev->vbi_out_have_cc[1] = false;
  192. dev->vbi_out_have_wss = false;
  193. while (elems--) {
  194. switch (vbi->id) {
  195. case V4L2_SLICED_CAPTION_525:
  196. if ((dev->std_out & V4L2_STD_525_60) && vbi->line == 21) {
  197. dev->vbi_out_have_cc[!!vbi->field] = true;
  198. dev->vbi_out_cc[!!vbi->field][0] = vbi->data[0];
  199. dev->vbi_out_cc[!!vbi->field][1] = vbi->data[1];
  200. }
  201. break;
  202. case V4L2_SLICED_WSS_625:
  203. if ((dev->std_out & V4L2_STD_625_50) &&
  204. vbi->field == 0 && vbi->line == 23) {
  205. dev->vbi_out_have_wss = true;
  206. dev->vbi_out_wss[0] = vbi->data[0];
  207. dev->vbi_out_wss[1] = vbi->data[1];
  208. }
  209. break;
  210. }
  211. vbi++;
  212. }
  213. }