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