1 // SPDX-License-Identifier: GPL-2.0-only
3 * vivid-vbi-cap.c - vbi capture support functions.
5 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
8 #include <linux/errno.h>
9 #include <linux/kernel.h>
10 #include <linux/videodev2.h>
11 #include <media/v4l2-common.h>
13 #include "vivid-core.h"
14 #include "vivid-kthread-cap.h"
15 #include "vivid-vbi-cap.h"
16 #include "vivid-vbi-gen.h"
18 static void vivid_sliced_vbi_cap_fill(struct vivid_dev
*dev
, unsigned seqnr
)
20 struct vivid_vbi_gen_data
*vbi_gen
= &dev
->vbi_gen
;
21 bool is_60hz
= dev
->std_cap
& V4L2_STD_525_60
;
23 vivid_vbi_gen_sliced(vbi_gen
, is_60hz
, seqnr
);
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];
31 vbi_gen
->data
[12].id
= 0;
34 switch (tpg_g_video_aspect(&dev
->tpg
)) {
35 case TPG_VIDEO_ASPECT_14X9_CENTRE
:
36 vbi_gen
->data
[12].data
[0] = 0x01;
38 case TPG_VIDEO_ASPECT_16X9_CENTRE
:
39 vbi_gen
->data
[12].data
[0] = 0x0b;
41 case TPG_VIDEO_ASPECT_16X9_ANAMORPHIC
:
42 vbi_gen
->data
[12].data
[0] = 0x07;
44 case TPG_VIDEO_ASPECT_4X3
:
46 vbi_gen
->data
[12].data
[0] = 0x08;
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];
55 vbi_gen
->data
[0].id
= 0;
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];
61 vbi_gen
->data
[1].id
= 0;
66 static void vivid_g_fmt_vbi_cap(struct vivid_dev
*dev
, struct v4l2_vbi_format
*vbi
)
68 bool is_60hz
= dev
->std_cap
& V4L2_STD_525_60
;
70 vbi
->sampling_rate
= 27000000;
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;
82 void vivid_raw_vbi_cap_process(struct vivid_dev
*dev
, struct vivid_buffer
*buf
)
84 struct v4l2_vbi_format vbi
;
85 u8
*vbuf
= vb2_plane_vaddr(&buf
->vb
.vb2_buf
, 0);
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;
92 vivid_sliced_vbi_cap_fill(dev
, buf
->vb
.sequence
);
94 memset(vbuf
, 0x10, vb2_plane_size(&buf
->vb
.vb2_buf
, 0));
96 if (!VIVID_INVALID_SIGNAL(dev
->std_signal_mode
))
97 vivid_vbi_gen_raw(&dev
->vbi_gen
, &vbi
, vbuf
);
99 buf
->vb
.vb2_buf
.timestamp
= ktime_get_ns() + dev
->time_wrap_offset
;
103 void vivid_sliced_vbi_cap_process(struct vivid_dev
*dev
,
104 struct vivid_buffer
*buf
)
106 struct v4l2_sliced_vbi_data
*vbuf
=
107 vb2_plane_vaddr(&buf
->vb
.vb2_buf
, 0);
109 buf
->vb
.sequence
= dev
->vbi_cap_seq_count
;
110 if (dev
->field_cap
== V4L2_FIELD_ALTERNATE
)
111 buf
->vb
.sequence
/= 2;
113 vivid_sliced_vbi_cap_fill(dev
, buf
->vb
.sequence
);
115 memset(vbuf
, 0, vb2_plane_size(&buf
->vb
.vb2_buf
, 0));
116 if (!VIVID_INVALID_SIGNAL(dev
->std_signal_mode
)) {
119 for (i
= 0; i
< 25; i
++)
120 vbuf
[i
] = dev
->vbi_gen
.data
[i
];
123 buf
->vb
.vb2_buf
.timestamp
= ktime_get_ns() + dev
->time_wrap_offset
;
126 static int vbi_cap_queue_setup(struct vb2_queue
*vq
,
127 unsigned *nbuffers
, unsigned *nplanes
,
128 unsigned sizes
[], struct device
*alloc_devs
[])
130 struct vivid_dev
*dev
= vb2_get_drv_priv(vq
);
131 bool is_60hz
= dev
->std_cap
& V4L2_STD_525_60
;
132 unsigned size
= vq
->type
== V4L2_BUF_TYPE_SLICED_VBI_CAPTURE
?
133 36 * sizeof(struct v4l2_sliced_vbi_data
) :
134 1440 * 2 * (is_60hz
? 12 : 18);
136 if (!vivid_is_sdtv_cap(dev
))
141 if (vq
->num_buffers
+ *nbuffers
< 2)
142 *nbuffers
= 2 - vq
->num_buffers
;
148 static int vbi_cap_buf_prepare(struct vb2_buffer
*vb
)
150 struct vivid_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
151 bool is_60hz
= dev
->std_cap
& V4L2_STD_525_60
;
152 unsigned size
= vb
->vb2_queue
->type
== V4L2_BUF_TYPE_SLICED_VBI_CAPTURE
?
153 36 * sizeof(struct v4l2_sliced_vbi_data
) :
154 1440 * 2 * (is_60hz
? 12 : 18);
156 dprintk(dev
, 1, "%s\n", __func__
);
158 if (dev
->buf_prepare_error
) {
160 * Error injection: test what happens if buf_prepare() returns
163 dev
->buf_prepare_error
= false;
166 if (vb2_plane_size(vb
, 0) < size
) {
167 dprintk(dev
, 1, "%s data will not fit into plane (%lu < %u)\n",
168 __func__
, vb2_plane_size(vb
, 0), size
);
171 vb2_set_plane_payload(vb
, 0, size
);
176 static void vbi_cap_buf_queue(struct vb2_buffer
*vb
)
178 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
179 struct vivid_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
180 struct vivid_buffer
*buf
= container_of(vbuf
, struct vivid_buffer
, vb
);
182 dprintk(dev
, 1, "%s\n", __func__
);
184 spin_lock(&dev
->slock
);
185 list_add_tail(&buf
->list
, &dev
->vbi_cap_active
);
186 spin_unlock(&dev
->slock
);
189 static int vbi_cap_start_streaming(struct vb2_queue
*vq
, unsigned count
)
191 struct vivid_dev
*dev
= vb2_get_drv_priv(vq
);
194 dprintk(dev
, 1, "%s\n", __func__
);
195 dev
->vbi_cap_seq_count
= 0;
196 if (dev
->start_streaming_error
) {
197 dev
->start_streaming_error
= false;
200 err
= vivid_start_generating_vid_cap(dev
, &dev
->vbi_cap_streaming
);
203 struct vivid_buffer
*buf
, *tmp
;
205 list_for_each_entry_safe(buf
, tmp
, &dev
->vbi_cap_active
, list
) {
206 list_del(&buf
->list
);
207 vb2_buffer_done(&buf
->vb
.vb2_buf
,
208 VB2_BUF_STATE_QUEUED
);
214 /* abort streaming and wait for last buffer */
215 static void vbi_cap_stop_streaming(struct vb2_queue
*vq
)
217 struct vivid_dev
*dev
= vb2_get_drv_priv(vq
);
219 dprintk(dev
, 1, "%s\n", __func__
);
220 vivid_stop_generating_vid_cap(dev
, &dev
->vbi_cap_streaming
);
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 .wait_prepare
= vb2_ops_wait_prepare
,
230 .wait_finish
= vb2_ops_wait_finish
,
233 int vidioc_g_fmt_vbi_cap(struct file
*file
, void *priv
,
234 struct v4l2_format
*f
)
236 struct vivid_dev
*dev
= video_drvdata(file
);
237 struct v4l2_vbi_format
*vbi
= &f
->fmt
.vbi
;
239 if (!vivid_is_sdtv_cap(dev
) || !dev
->has_raw_vbi_cap
)
242 vivid_g_fmt_vbi_cap(dev
, vbi
);
246 int vidioc_s_fmt_vbi_cap(struct file
*file
, void *priv
,
247 struct v4l2_format
*f
)
249 struct vivid_dev
*dev
= video_drvdata(file
);
250 int ret
= vidioc_g_fmt_vbi_cap(file
, priv
, f
);
254 if (dev
->stream_sliced_vbi_cap
&& vb2_is_busy(&dev
->vb_vbi_cap_q
))
256 dev
->stream_sliced_vbi_cap
= false;
257 dev
->vbi_cap_dev
.queue
->type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
261 void vivid_fill_service_lines(struct v4l2_sliced_vbi_format
*vbi
, u32 service_set
)
263 vbi
->io_size
= sizeof(struct v4l2_sliced_vbi_data
) * 36;
264 vbi
->service_set
= service_set
;
265 memset(vbi
->service_lines
, 0, sizeof(vbi
->service_lines
));
266 memset(vbi
->reserved
, 0, sizeof(vbi
->reserved
));
268 if (vbi
->service_set
== 0)
271 if (vbi
->service_set
& V4L2_SLICED_CAPTION_525
) {
272 vbi
->service_lines
[0][21] = V4L2_SLICED_CAPTION_525
;
273 vbi
->service_lines
[1][21] = V4L2_SLICED_CAPTION_525
;
275 if (vbi
->service_set
& V4L2_SLICED_WSS_625
) {
278 for (i
= 7; i
<= 18; i
++)
279 vbi
->service_lines
[0][i
] =
280 vbi
->service_lines
[1][i
] = V4L2_SLICED_TELETEXT_B
;
281 vbi
->service_lines
[0][23] = V4L2_SLICED_WSS_625
;
285 int vidioc_g_fmt_sliced_vbi_cap(struct file
*file
, void *fh
, struct v4l2_format
*fmt
)
287 struct vivid_dev
*dev
= video_drvdata(file
);
288 struct v4l2_sliced_vbi_format
*vbi
= &fmt
->fmt
.sliced
;
290 if (!vivid_is_sdtv_cap(dev
) || !dev
->has_sliced_vbi_cap
)
293 vivid_fill_service_lines(vbi
, dev
->service_set_cap
);
297 int vidioc_try_fmt_sliced_vbi_cap(struct file
*file
, void *fh
, struct v4l2_format
*fmt
)
299 struct vivid_dev
*dev
= video_drvdata(file
);
300 struct v4l2_sliced_vbi_format
*vbi
= &fmt
->fmt
.sliced
;
301 bool is_60hz
= dev
->std_cap
& V4L2_STD_525_60
;
302 u32 service_set
= vbi
->service_set
;
304 if (!vivid_is_sdtv_cap(dev
) || !dev
->has_sliced_vbi_cap
)
307 service_set
&= is_60hz
? V4L2_SLICED_CAPTION_525
:
308 V4L2_SLICED_WSS_625
| V4L2_SLICED_TELETEXT_B
;
309 vivid_fill_service_lines(vbi
, service_set
);
313 int vidioc_s_fmt_sliced_vbi_cap(struct file
*file
, void *fh
, struct v4l2_format
*fmt
)
315 struct vivid_dev
*dev
= video_drvdata(file
);
316 struct v4l2_sliced_vbi_format
*vbi
= &fmt
->fmt
.sliced
;
317 int ret
= vidioc_try_fmt_sliced_vbi_cap(file
, fh
, fmt
);
321 if (!dev
->stream_sliced_vbi_cap
&& vb2_is_busy(&dev
->vb_vbi_cap_q
))
323 dev
->service_set_cap
= vbi
->service_set
;
324 dev
->stream_sliced_vbi_cap
= true;
325 dev
->vbi_cap_dev
.queue
->type
= V4L2_BUF_TYPE_SLICED_VBI_CAPTURE
;
329 int vidioc_g_sliced_vbi_cap(struct file
*file
, void *fh
, struct v4l2_sliced_vbi_cap
*cap
)
331 struct vivid_dev
*dev
= video_drvdata(file
);
332 struct video_device
*vdev
= video_devdata(file
);
335 if (vdev
->vfl_dir
== VFL_DIR_RX
) {
336 is_60hz
= dev
->std_cap
& V4L2_STD_525_60
;
337 if (!vivid_is_sdtv_cap(dev
) || !dev
->has_sliced_vbi_cap
||
338 cap
->type
!= V4L2_BUF_TYPE_SLICED_VBI_CAPTURE
)
341 is_60hz
= dev
->std_out
& V4L2_STD_525_60
;
342 if (!vivid_is_svid_out(dev
) || !dev
->has_sliced_vbi_out
||
343 cap
->type
!= V4L2_BUF_TYPE_SLICED_VBI_OUTPUT
)
347 cap
->service_set
= is_60hz
? V4L2_SLICED_CAPTION_525
:
348 V4L2_SLICED_WSS_625
| V4L2_SLICED_TELETEXT_B
;
350 cap
->service_lines
[0][21] = V4L2_SLICED_CAPTION_525
;
351 cap
->service_lines
[1][21] = V4L2_SLICED_CAPTION_525
;
355 for (i
= 7; i
<= 18; i
++)
356 cap
->service_lines
[0][i
] =
357 cap
->service_lines
[1][i
] = V4L2_SLICED_TELETEXT_B
;
358 cap
->service_lines
[0][23] = V4L2_SLICED_WSS_625
;