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