]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/media/platform/vivid/vivid-vbi-cap.c
[media] media: videobuf2: Restructure vb2_buffer
[mirror_ubuntu-hirsute-kernel.git] / drivers / media / platform / vivid / vivid-vbi-cap.c
CommitLineData
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
30static 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) {
62f28725
HV
40 vbi_gen->data[12].data[0] = dev->vbi_out_wss[0];
41 vbi_gen->data[12].data[1] = dev->vbi_out_wss[1];
7bb70caa 42 } else {
62f28725 43 vbi_gen->data[12].id = 0;
7bb70caa
HV
44 }
45 } else {
46 switch (tpg_g_video_aspect(&dev->tpg)) {
47 case TPG_VIDEO_ASPECT_14X9_CENTRE:
62f28725 48 vbi_gen->data[12].data[0] = 0x01;
7bb70caa
HV
49 break;
50 case TPG_VIDEO_ASPECT_16X9_CENTRE:
62f28725 51 vbi_gen->data[12].data[0] = 0x0b;
7bb70caa
HV
52 break;
53 case TPG_VIDEO_ASPECT_16X9_ANAMORPHIC:
62f28725 54 vbi_gen->data[12].data[0] = 0x07;
7bb70caa
HV
55 break;
56 case TPG_VIDEO_ASPECT_4X3:
57 default:
62f28725 58 vbi_gen->data[12].data[0] = 0x08;
7bb70caa
HV
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
78static 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;
62f28725
HV
86 vbi->start[0] = is_60hz ? V4L2_VBI_ITU_525_F1_START + 9 : V4L2_VBI_ITU_625_F1_START + 5;
87 vbi->start[1] = is_60hz ? V4L2_VBI_ITU_525_F2_START + 9 : V4L2_VBI_ITU_625_F2_START + 5;
7bb70caa
HV
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
94void vivid_raw_vbi_cap_process(struct vivid_dev *dev, struct vivid_buffer *buf)
95{
96 struct v4l2_vbi_format vbi;
2d700715 97 u8 *vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
7bb70caa
HV
98
99 vivid_g_fmt_vbi_cap(dev, &vbi);
2d700715 100 buf->vb.sequence = dev->vbi_cap_seq_count;
7bb70caa 101 if (dev->field_cap == V4L2_FIELD_ALTERNATE)
2d700715 102 buf->vb.sequence /= 2;
7bb70caa 103
2d700715 104 vivid_sliced_vbi_cap_fill(dev, buf->vb.sequence);
7bb70caa 105
2d700715 106 memset(vbuf, 0x10, vb2_plane_size(&buf->vb.vb2_buf, 0));
7bb70caa
HV
107
108 if (!VIVID_INVALID_SIGNAL(dev->std_signal_mode))
109 vivid_vbi_gen_raw(&dev->vbi_gen, &vbi, vbuf);
110
2d700715
JS
111 v4l2_get_timestamp(&buf->vb.timestamp);
112 buf->vb.timestamp.tv_sec += dev->time_wrap_offset;
7bb70caa
HV
113}
114
115
2d700715
JS
116void vivid_sliced_vbi_cap_process(struct vivid_dev *dev,
117 struct vivid_buffer *buf)
7bb70caa 118{
2d700715
JS
119 struct v4l2_sliced_vbi_data *vbuf =
120 vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
7bb70caa 121
2d700715 122 buf->vb.sequence = dev->vbi_cap_seq_count;
7bb70caa 123 if (dev->field_cap == V4L2_FIELD_ALTERNATE)
2d700715 124 buf->vb.sequence /= 2;
7bb70caa 125
2d700715 126 vivid_sliced_vbi_cap_fill(dev, buf->vb.sequence);
7bb70caa 127
2d700715 128 memset(vbuf, 0, vb2_plane_size(&buf->vb.vb2_buf, 0));
7bb70caa 129 if (!VIVID_INVALID_SIGNAL(dev->std_signal_mode)) {
62f28725
HV
130 unsigned i;
131
132 for (i = 0; i < 25; i++)
133 vbuf[i] = dev->vbi_gen.data[i];
7bb70caa
HV
134 }
135
2d700715
JS
136 v4l2_get_timestamp(&buf->vb.timestamp);
137 buf->vb.timestamp.tv_sec += dev->time_wrap_offset;
7bb70caa
HV
138}
139
2d700715
JS
140static int vbi_cap_queue_setup(struct vb2_queue *vq,
141 const struct v4l2_format *fmt,
142 unsigned *nbuffers, unsigned *nplanes,
143 unsigned sizes[], void *alloc_ctxs[])
7bb70caa
HV
144{
145 struct vivid_dev *dev = vb2_get_drv_priv(vq);
146 bool is_60hz = dev->std_cap & V4L2_STD_525_60;
147 unsigned size = vq->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE ?
148 36 * sizeof(struct v4l2_sliced_vbi_data) :
149 1440 * 2 * (is_60hz ? 12 : 18);
150
151 if (!vivid_is_sdtv_cap(dev))
152 return -EINVAL;
153
154 sizes[0] = size;
155
156 if (vq->num_buffers + *nbuffers < 2)
157 *nbuffers = 2 - vq->num_buffers;
158
159 *nplanes = 1;
160 return 0;
161}
162
163static int vbi_cap_buf_prepare(struct vb2_buffer *vb)
164{
165 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
166 bool is_60hz = dev->std_cap & V4L2_STD_525_60;
167 unsigned size = vb->vb2_queue->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE ?
168 36 * sizeof(struct v4l2_sliced_vbi_data) :
169 1440 * 2 * (is_60hz ? 12 : 18);
170
171 dprintk(dev, 1, "%s\n", __func__);
172
173 if (dev->buf_prepare_error) {
174 /*
175 * Error injection: test what happens if buf_prepare() returns
176 * an error.
177 */
178 dev->buf_prepare_error = false;
179 return -EINVAL;
180 }
181 if (vb2_plane_size(vb, 0) < size) {
182 dprintk(dev, 1, "%s data will not fit into plane (%lu < %u)\n",
183 __func__, vb2_plane_size(vb, 0), size);
184 return -EINVAL;
185 }
186 vb2_set_plane_payload(vb, 0, size);
187
188 return 0;
189}
190
191static void vbi_cap_buf_queue(struct vb2_buffer *vb)
192{
2d700715 193 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
7bb70caa 194 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
2d700715 195 struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
7bb70caa
HV
196
197 dprintk(dev, 1, "%s\n", __func__);
198
199 spin_lock(&dev->slock);
200 list_add_tail(&buf->list, &dev->vbi_cap_active);
201 spin_unlock(&dev->slock);
202}
203
204static int vbi_cap_start_streaming(struct vb2_queue *vq, unsigned count)
205{
206 struct vivid_dev *dev = vb2_get_drv_priv(vq);
207 int err;
208
209 dprintk(dev, 1, "%s\n", __func__);
210 dev->vbi_cap_seq_count = 0;
211 if (dev->start_streaming_error) {
212 dev->start_streaming_error = false;
213 err = -EINVAL;
214 } else {
215 err = vivid_start_generating_vid_cap(dev, &dev->vbi_cap_streaming);
216 }
217 if (err) {
218 struct vivid_buffer *buf, *tmp;
219
220 list_for_each_entry_safe(buf, tmp, &dev->vbi_cap_active, list) {
221 list_del(&buf->list);
2d700715
JS
222 vb2_buffer_done(&buf->vb.vb2_buf,
223 VB2_BUF_STATE_QUEUED);
7bb70caa
HV
224 }
225 }
226 return err;
227}
228
229/* abort streaming and wait for last buffer */
230static void vbi_cap_stop_streaming(struct vb2_queue *vq)
231{
232 struct vivid_dev *dev = vb2_get_drv_priv(vq);
233
234 dprintk(dev, 1, "%s\n", __func__);
235 vivid_stop_generating_vid_cap(dev, &dev->vbi_cap_streaming);
236}
237
238const struct vb2_ops vivid_vbi_cap_qops = {
239 .queue_setup = vbi_cap_queue_setup,
240 .buf_prepare = vbi_cap_buf_prepare,
241 .buf_queue = vbi_cap_buf_queue,
242 .start_streaming = vbi_cap_start_streaming,
243 .stop_streaming = vbi_cap_stop_streaming,
6dfa5131
PL
244 .wait_prepare = vb2_ops_wait_prepare,
245 .wait_finish = vb2_ops_wait_finish,
7bb70caa
HV
246};
247
248int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
249 struct v4l2_format *f)
250{
251 struct vivid_dev *dev = video_drvdata(file);
252 struct v4l2_vbi_format *vbi = &f->fmt.vbi;
253
254 if (!vivid_is_sdtv_cap(dev) || !dev->has_raw_vbi_cap)
255 return -EINVAL;
256
257 vivid_g_fmt_vbi_cap(dev, vbi);
258 return 0;
259}
260
261int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
262 struct v4l2_format *f)
263{
264 struct vivid_dev *dev = video_drvdata(file);
265 int ret = vidioc_g_fmt_vbi_cap(file, priv, f);
266
267 if (ret)
268 return ret;
269 if (dev->stream_sliced_vbi_cap && vb2_is_busy(&dev->vb_vbi_cap_q))
270 return -EBUSY;
271 dev->stream_sliced_vbi_cap = false;
272 dev->vbi_cap_dev.queue->type = V4L2_BUF_TYPE_VBI_CAPTURE;
273 return 0;
274}
275
276void vivid_fill_service_lines(struct v4l2_sliced_vbi_format *vbi, u32 service_set)
277{
278 vbi->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
279 vbi->service_set = service_set;
280 memset(vbi->service_lines, 0, sizeof(vbi->service_lines));
281 memset(vbi->reserved, 0, sizeof(vbi->reserved));
282
283 if (vbi->service_set == 0)
284 return;
285
286 if (vbi->service_set & V4L2_SLICED_CAPTION_525) {
287 vbi->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
288 vbi->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
289 }
62f28725
HV
290 if (vbi->service_set & V4L2_SLICED_WSS_625) {
291 unsigned i;
292
293 for (i = 7; i <= 18; i++)
294 vbi->service_lines[0][i] =
295 vbi->service_lines[1][i] = V4L2_SLICED_TELETEXT_B;
7bb70caa 296 vbi->service_lines[0][23] = V4L2_SLICED_WSS_625;
62f28725 297 }
7bb70caa
HV
298}
299
300int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
301{
302 struct vivid_dev *dev = video_drvdata(file);
303 struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
304
305 if (!vivid_is_sdtv_cap(dev) || !dev->has_sliced_vbi_cap)
306 return -EINVAL;
307
308 vivid_fill_service_lines(vbi, dev->service_set_cap);
309 return 0;
310}
311
312int vidioc_try_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 bool is_60hz = dev->std_cap & V4L2_STD_525_60;
317 u32 service_set = vbi->service_set;
318
319 if (!vivid_is_sdtv_cap(dev) || !dev->has_sliced_vbi_cap)
320 return -EINVAL;
321
62f28725
HV
322 service_set &= is_60hz ? V4L2_SLICED_CAPTION_525 :
323 V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
7bb70caa
HV
324 vivid_fill_service_lines(vbi, service_set);
325 return 0;
326}
327
328int vidioc_s_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
329{
330 struct vivid_dev *dev = video_drvdata(file);
331 struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
332 int ret = vidioc_try_fmt_sliced_vbi_cap(file, fh, fmt);
333
334 if (ret)
335 return ret;
336 if (!dev->stream_sliced_vbi_cap && vb2_is_busy(&dev->vb_vbi_cap_q))
337 return -EBUSY;
338 dev->service_set_cap = vbi->service_set;
339 dev->stream_sliced_vbi_cap = true;
340 dev->vbi_cap_dev.queue->type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
341 return 0;
342}
343
344int vidioc_g_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_sliced_vbi_cap *cap)
345{
346 struct vivid_dev *dev = video_drvdata(file);
347 struct video_device *vdev = video_devdata(file);
348 bool is_60hz;
349
350 if (vdev->vfl_dir == VFL_DIR_RX) {
351 is_60hz = dev->std_cap & V4L2_STD_525_60;
352 if (!vivid_is_sdtv_cap(dev) || !dev->has_sliced_vbi_cap ||
353 cap->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
354 return -EINVAL;
355 } else {
356 is_60hz = dev->std_out & V4L2_STD_525_60;
357 if (!vivid_is_svid_out(dev) || !dev->has_sliced_vbi_out ||
358 cap->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT)
359 return -EINVAL;
360 }
361
62f28725
HV
362 cap->service_set = is_60hz ? V4L2_SLICED_CAPTION_525 :
363 V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
7bb70caa
HV
364 if (is_60hz) {
365 cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
366 cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
367 } else {
62f28725
HV
368 unsigned i;
369
370 for (i = 7; i <= 18; i++)
371 cap->service_lines[0][i] =
372 cap->service_lines[1][i] = V4L2_SLICED_TELETEXT_B;
7bb70caa
HV
373 cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
374 }
375 return 0;
376}