1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vivid-vbi-cap.c - vbi capture 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-cap.h"
15 #include "vivid-vbi-cap.h"
16 #include "vivid-vbi-gen.h"
17 
18 static void vivid_sliced_vbi_cap_fill(struct vivid_dev *dev, unsigned seqnr)
19 {
20 	struct vivid_vbi_gen_data *vbi_gen = &dev->vbi_gen;
21 	bool is_60hz = dev->std_cap[dev->input] & V4L2_STD_525_60;
22 
23 	vivid_vbi_gen_sliced(vbi_gen, is_60hz, seqnr);
24 
25 	if (!is_60hz) {
26 		if (dev->loop_video) {
27 			if (dev->vbi_out_have_wss) {
28 				vbi_gen->data[12].data[0] = dev->vbi_out_wss[0];
29 				vbi_gen->data[12].data[1] = dev->vbi_out_wss[1];
30 			} else {
31 				vbi_gen->data[12].id = 0;
32 			}
33 		} else {
34 			switch (tpg_g_video_aspect(&dev->tpg)) {
35 			case TPG_VIDEO_ASPECT_14X9_CENTRE:
36 				vbi_gen->data[12].data[0] = 0x01;
37 				break;
38 			case TPG_VIDEO_ASPECT_16X9_CENTRE:
39 				vbi_gen->data[12].data[0] = 0x0b;
40 				break;
41 			case TPG_VIDEO_ASPECT_16X9_ANAMORPHIC:
42 				vbi_gen->data[12].data[0] = 0x07;
43 				break;
44 			case TPG_VIDEO_ASPECT_4X3:
45 			default:
46 				vbi_gen->data[12].data[0] = 0x08;
47 				break;
48 			}
49 		}
50 	} else if (dev->loop_video && is_60hz) {
51 		if (dev->vbi_out_have_cc[0]) {
52 			vbi_gen->data[0].data[0] = dev->vbi_out_cc[0][0];
53 			vbi_gen->data[0].data[1] = dev->vbi_out_cc[0][1];
54 		} else {
55 			vbi_gen->data[0].id = 0;
56 		}
57 		if (dev->vbi_out_have_cc[1]) {
58 			vbi_gen->data[1].data[0] = dev->vbi_out_cc[1][0];
59 			vbi_gen->data[1].data[1] = dev->vbi_out_cc[1][1];
60 		} else {
61 			vbi_gen->data[1].id = 0;
62 		}
63 	}
64 }
65 
66 static void vivid_g_fmt_vbi_cap(struct vivid_dev *dev, struct v4l2_vbi_format *vbi)
67 {
68 	bool is_60hz = dev->std_cap[dev->input] & V4L2_STD_525_60;
69 
70 	vbi->sampling_rate = 27000000;
71 	vbi->offset = 24;
72 	vbi->samples_per_line = 1440;
73 	vbi->sample_format = V4L2_PIX_FMT_GREY;
74 	vbi->start[0] = is_60hz ? V4L2_VBI_ITU_525_F1_START + 9 : V4L2_VBI_ITU_625_F1_START + 5;
75 	vbi->start[1] = is_60hz ? V4L2_VBI_ITU_525_F2_START + 9 : V4L2_VBI_ITU_625_F2_START + 5;
76 	vbi->count[0] = vbi->count[1] = is_60hz ? 12 : 18;
77 	vbi->flags = dev->vbi_cap_interlaced ? V4L2_VBI_INTERLACED : 0;
78 	vbi->reserved[0] = 0;
79 	vbi->reserved[1] = 0;
80 }
81 
82 void vivid_raw_vbi_cap_process(struct vivid_dev *dev, struct vivid_buffer *buf)
83 {
84 	struct v4l2_vbi_format vbi;
85 	u8 *vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
86 
87 	vivid_g_fmt_vbi_cap(dev, &vbi);
88 	buf->vb.sequence = dev->vbi_cap_seq_count;
89 	if (dev->field_cap == V4L2_FIELD_ALTERNATE)
90 		buf->vb.sequence /= 2;
91 
92 	vivid_sliced_vbi_cap_fill(dev, buf->vb.sequence);
93 
94 	memset(vbuf, 0x10, vb2_plane_size(&buf->vb.vb2_buf, 0));
95 
96 	if (!VIVID_INVALID_SIGNAL(dev->std_signal_mode[dev->input]))
97 		vivid_vbi_gen_raw(&dev->vbi_gen, &vbi, vbuf);
98 }
99 
100 
101 void vivid_sliced_vbi_cap_process(struct vivid_dev *dev,
102 			struct vivid_buffer *buf)
103 {
104 	struct v4l2_sliced_vbi_data *vbuf =
105 			vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
106 
107 	buf->vb.sequence = dev->vbi_cap_seq_count;
108 	if (dev->field_cap == V4L2_FIELD_ALTERNATE)
109 		buf->vb.sequence /= 2;
110 
111 	vivid_sliced_vbi_cap_fill(dev, buf->vb.sequence);
112 
113 	memset(vbuf, 0, vb2_plane_size(&buf->vb.vb2_buf, 0));
114 	if (!VIVID_INVALID_SIGNAL(dev->std_signal_mode[dev->input])) {
115 		unsigned i;
116 
117 		for (i = 0; i < 25; i++)
118 			vbuf[i] = dev->vbi_gen.data[i];
119 	}
120 }
121 
122 static int vbi_cap_queue_setup(struct vb2_queue *vq,
123 		       unsigned *nbuffers, unsigned *nplanes,
124 		       unsigned sizes[], struct device *alloc_devs[])
125 {
126 	struct vivid_dev *dev = vb2_get_drv_priv(vq);
127 	bool is_60hz = dev->std_cap[dev->input] & V4L2_STD_525_60;
128 	unsigned size = vq->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE ?
129 		36 * sizeof(struct v4l2_sliced_vbi_data) :
130 		1440 * 2 * (is_60hz ? 12 : 18);
131 
132 	if (!vivid_is_sdtv_cap(dev))
133 		return -EINVAL;
134 
135 	sizes[0] = size;
136 
137 	*nplanes = 1;
138 	return 0;
139 }
140 
141 static int vbi_cap_buf_prepare(struct vb2_buffer *vb)
142 {
143 	struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
144 	bool is_60hz = dev->std_cap[dev->input] & V4L2_STD_525_60;
145 	unsigned size = vb->vb2_queue->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE ?
146 		36 * sizeof(struct v4l2_sliced_vbi_data) :
147 		1440 * 2 * (is_60hz ? 12 : 18);
148 
149 	dprintk(dev, 1, "%s\n", __func__);
150 
151 	if (dev->buf_prepare_error) {
152 		/*
153 		 * Error injection: test what happens if buf_prepare() returns
154 		 * an error.
155 		 */
156 		dev->buf_prepare_error = false;
157 		return -EINVAL;
158 	}
159 	if (vb2_plane_size(vb, 0) < size) {
160 		dprintk(dev, 1, "%s data will not fit into plane (%lu < %u)\n",
161 				__func__, vb2_plane_size(vb, 0), size);
162 		return -EINVAL;
163 	}
164 	vb2_set_plane_payload(vb, 0, size);
165 
166 	return 0;
167 }
168 
169 static void vbi_cap_buf_queue(struct vb2_buffer *vb)
170 {
171 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
172 	struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
173 	struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
174 
175 	dprintk(dev, 1, "%s\n", __func__);
176 
177 	spin_lock(&dev->slock);
178 	list_add_tail(&buf->list, &dev->vbi_cap_active);
179 	spin_unlock(&dev->slock);
180 }
181 
182 static int vbi_cap_start_streaming(struct vb2_queue *vq, unsigned count)
183 {
184 	struct vivid_dev *dev = vb2_get_drv_priv(vq);
185 	int err;
186 
187 	dprintk(dev, 1, "%s\n", __func__);
188 	dev->vbi_cap_seq_count = 0;
189 	if (dev->start_streaming_error) {
190 		dev->start_streaming_error = false;
191 		err = -EINVAL;
192 	} else {
193 		err = vivid_start_generating_vid_cap(dev, &dev->vbi_cap_streaming);
194 	}
195 	if (err) {
196 		struct vivid_buffer *buf, *tmp;
197 
198 		list_for_each_entry_safe(buf, tmp, &dev->vbi_cap_active, list) {
199 			list_del(&buf->list);
200 			vb2_buffer_done(&buf->vb.vb2_buf,
201 					VB2_BUF_STATE_QUEUED);
202 		}
203 	}
204 	return err;
205 }
206 
207 /* abort streaming and wait for last buffer */
208 static void vbi_cap_stop_streaming(struct vb2_queue *vq)
209 {
210 	struct vivid_dev *dev = vb2_get_drv_priv(vq);
211 
212 	dprintk(dev, 1, "%s\n", __func__);
213 	vivid_stop_generating_vid_cap(dev, &dev->vbi_cap_streaming);
214 }
215 
216 static void vbi_cap_buf_request_complete(struct vb2_buffer *vb)
217 {
218 	struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
219 
220 	v4l2_ctrl_request_complete(vb->req_obj.req, &dev->ctrl_hdl_vbi_cap);
221 }
222 
223 const struct vb2_ops vivid_vbi_cap_qops = {
224 	.queue_setup		= vbi_cap_queue_setup,
225 	.buf_prepare		= vbi_cap_buf_prepare,
226 	.buf_queue		= vbi_cap_buf_queue,
227 	.start_streaming	= vbi_cap_start_streaming,
228 	.stop_streaming		= vbi_cap_stop_streaming,
229 	.buf_request_complete	= vbi_cap_buf_request_complete,
230 	.wait_prepare		= vb2_ops_wait_prepare,
231 	.wait_finish		= vb2_ops_wait_finish,
232 };
233 
234 int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
235 					struct v4l2_format *f)
236 {
237 	struct vivid_dev *dev = video_drvdata(file);
238 	struct v4l2_vbi_format *vbi = &f->fmt.vbi;
239 
240 	if (!vivid_is_sdtv_cap(dev) || !dev->has_raw_vbi_cap)
241 		return -EINVAL;
242 
243 	vivid_g_fmt_vbi_cap(dev, vbi);
244 	return 0;
245 }
246 
247 int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
248 					struct v4l2_format *f)
249 {
250 	struct vivid_dev *dev = video_drvdata(file);
251 	int ret = vidioc_g_fmt_vbi_cap(file, priv, f);
252 
253 	if (ret)
254 		return ret;
255 	if (f->type != V4L2_BUF_TYPE_VBI_CAPTURE && vb2_is_busy(&dev->vb_vbi_cap_q))
256 		return -EBUSY;
257 	return 0;
258 }
259 
260 void vivid_fill_service_lines(struct v4l2_sliced_vbi_format *vbi, u32 service_set)
261 {
262 	vbi->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
263 	vbi->service_set = service_set;
264 	memset(vbi->service_lines, 0, sizeof(vbi->service_lines));
265 	memset(vbi->reserved, 0, sizeof(vbi->reserved));
266 
267 	if (vbi->service_set == 0)
268 		return;
269 
270 	if (vbi->service_set & V4L2_SLICED_CAPTION_525) {
271 		vbi->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
272 		vbi->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
273 	}
274 	if (vbi->service_set & V4L2_SLICED_WSS_625) {
275 		unsigned i;
276 
277 		for (i = 7; i <= 18; i++)
278 			vbi->service_lines[0][i] =
279 			vbi->service_lines[1][i] = V4L2_SLICED_TELETEXT_B;
280 		vbi->service_lines[0][23] = V4L2_SLICED_WSS_625;
281 	}
282 }
283 
284 int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
285 {
286 	struct vivid_dev *dev = video_drvdata(file);
287 	struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
288 
289 	if (!vivid_is_sdtv_cap(dev) || !dev->has_sliced_vbi_cap)
290 		return -EINVAL;
291 
292 	vivid_fill_service_lines(vbi, dev->service_set_cap);
293 	return 0;
294 }
295 
296 int vidioc_try_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
297 {
298 	struct vivid_dev *dev = video_drvdata(file);
299 	struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
300 	bool is_60hz = dev->std_cap[dev->input] & V4L2_STD_525_60;
301 	u32 service_set = vbi->service_set;
302 
303 	if (!vivid_is_sdtv_cap(dev) || !dev->has_sliced_vbi_cap)
304 		return -EINVAL;
305 
306 	service_set &= is_60hz ? V4L2_SLICED_CAPTION_525 :
307 				 V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
308 	vivid_fill_service_lines(vbi, service_set);
309 	return 0;
310 }
311 
312 int vidioc_s_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
313 {
314 	struct vivid_dev *dev = video_drvdata(file);
315 	struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
316 	int ret = vidioc_try_fmt_sliced_vbi_cap(file, fh, fmt);
317 
318 	if (ret)
319 		return ret;
320 	if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE && vb2_is_busy(&dev->vb_vbi_cap_q))
321 		return -EBUSY;
322 	dev->service_set_cap = vbi->service_set;
323 	return 0;
324 }
325 
326 int vidioc_g_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_sliced_vbi_cap *cap)
327 {
328 	struct vivid_dev *dev = video_drvdata(file);
329 	struct video_device *vdev = video_devdata(file);
330 	bool is_60hz;
331 
332 	if (vdev->vfl_dir == VFL_DIR_RX) {
333 		is_60hz = dev->std_cap[dev->input] & V4L2_STD_525_60;
334 		if (!vivid_is_sdtv_cap(dev) || !dev->has_sliced_vbi_cap ||
335 		    cap->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
336 			return -EINVAL;
337 	} else {
338 		is_60hz = dev->std_out & V4L2_STD_525_60;
339 		if (!vivid_is_svid_out(dev) || !dev->has_sliced_vbi_out ||
340 		    cap->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT)
341 			return -EINVAL;
342 	}
343 
344 	cap->service_set = is_60hz ? V4L2_SLICED_CAPTION_525 :
345 				     V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
346 	if (is_60hz) {
347 		cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
348 		cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
349 	} else {
350 		unsigned i;
351 
352 		for (i = 7; i <= 18; i++)
353 			cap->service_lines[0][i] =
354 			cap->service_lines[1][i] = V4L2_SLICED_TELETEXT_B;
355 		cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
356 	}
357 	return 0;
358 }
359