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