Linux 4.16.11
[linux/fpc-iii.git] / drivers / media / pci / bt8xx / bttv-vbi.c
blob67c6583f1d797a63e27e54f3592f12f9b4bb75b9
1 /*
3 bttv - Bt848 frame grabber driver
4 vbi interface
6 (c) 2002 Gerd Knorr <kraxel@bytesex.org>
8 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
9 Sponsored by OPQ Systems AB
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/module.h>
29 #include <linux/errno.h>
30 #include <linux/fs.h>
31 #include <linux/kernel.h>
32 #include <linux/interrupt.h>
33 #include <linux/kdev_t.h>
34 #include <media/v4l2-ioctl.h>
35 #include <asm/io.h>
36 #include "bttvp.h"
38 /* Offset from line sync pulse leading edge (0H) to start of VBI capture,
39 in fCLKx2 pixels. According to the datasheet, VBI capture starts
40 VBI_HDELAY fCLKx1 pixels from the tailing edgeof /HRESET, and /HRESET
41 is 64 fCLKx1 pixels wide. VBI_HDELAY is set to 0, so this should be
42 (64 + 0) * 2 = 128 fCLKx2 pixels. But it's not! The datasheet is
43 Just Plain Wrong. The real value appears to be different for
44 different revisions of the bt8x8 chips, and to be affected by the
45 horizontal scaling factor. Experimentally, the value is measured
46 to be about 244. */
47 #define VBI_OFFSET 244
49 /* 2048 for compatibility with earlier driver versions. The driver
50 really stores 1024 + tvnorm->vbipack * 4 samples per line in the
51 buffer. Note tvnorm->vbipack is <= 0xFF (limit of VBIPACK_LO + HI
52 is 0x1FF DWORDs) and VBI read()s store a frame counter in the last
53 four bytes of the VBI image. */
54 #define VBI_BPL 2048
56 /* Compatibility. */
57 #define VBI_DEFLINES 16
59 static unsigned int vbibufs = 4;
60 static unsigned int vbi_debug;
62 module_param(vbibufs, int, 0444);
63 module_param(vbi_debug, int, 0644);
64 MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32, default 4");
65 MODULE_PARM_DESC(vbi_debug,"vbi code debug messages, default is 0 (no)");
67 #ifdef dprintk
68 # undef dprintk
69 #endif
70 #define dprintk(fmt, ...) \
71 do { \
72 if (vbi_debug) \
73 pr_debug("%d: " fmt, btv->c.nr, ##__VA_ARGS__); \
74 } while (0)
76 #define IMAGE_SIZE(fmt) \
77 (((fmt)->count[0] + (fmt)->count[1]) * (fmt)->samples_per_line)
79 /* ----------------------------------------------------------------------- */
80 /* vbi risc code + mm */
82 static int vbi_buffer_setup(struct videobuf_queue *q,
83 unsigned int *count, unsigned int *size)
85 struct bttv_fh *fh = q->priv_data;
86 struct bttv *btv = fh->btv;
88 if (0 == *count)
89 *count = vbibufs;
91 *size = IMAGE_SIZE(&fh->vbi_fmt.fmt);
93 dprintk("setup: samples=%u start=%d,%d count=%u,%u\n",
94 fh->vbi_fmt.fmt.samples_per_line,
95 fh->vbi_fmt.fmt.start[0],
96 fh->vbi_fmt.fmt.start[1],
97 fh->vbi_fmt.fmt.count[0],
98 fh->vbi_fmt.fmt.count[1]);
100 return 0;
103 static int vbi_buffer_prepare(struct videobuf_queue *q,
104 struct videobuf_buffer *vb,
105 enum v4l2_field field)
107 struct bttv_fh *fh = q->priv_data;
108 struct bttv *btv = fh->btv;
109 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
110 const struct bttv_tvnorm *tvnorm;
111 unsigned int skip_lines0, skip_lines1, min_vdelay;
112 int redo_dma_risc;
113 int rc;
115 buf->vb.size = IMAGE_SIZE(&fh->vbi_fmt.fmt);
116 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
117 return -EINVAL;
119 tvnorm = fh->vbi_fmt.tvnorm;
121 /* There's no VBI_VDELAY register, RISC must skip the lines
122 we don't want. With default parameters we skip zero lines
123 as earlier driver versions did. The driver permits video
124 standard changes while capturing, so we use vbi_fmt.tvnorm
125 instead of btv->tvnorm to skip zero lines after video
126 standard changes as well. */
128 skip_lines0 = 0;
129 skip_lines1 = 0;
131 if (fh->vbi_fmt.fmt.count[0] > 0)
132 skip_lines0 = max(0, (fh->vbi_fmt.fmt.start[0]
133 - tvnorm->vbistart[0]));
134 if (fh->vbi_fmt.fmt.count[1] > 0)
135 skip_lines1 = max(0, (fh->vbi_fmt.fmt.start[1]
136 - tvnorm->vbistart[1]));
138 redo_dma_risc = 0;
140 if (buf->vbi_skip[0] != skip_lines0 ||
141 buf->vbi_skip[1] != skip_lines1 ||
142 buf->vbi_count[0] != fh->vbi_fmt.fmt.count[0] ||
143 buf->vbi_count[1] != fh->vbi_fmt.fmt.count[1]) {
144 buf->vbi_skip[0] = skip_lines0;
145 buf->vbi_skip[1] = skip_lines1;
146 buf->vbi_count[0] = fh->vbi_fmt.fmt.count[0];
147 buf->vbi_count[1] = fh->vbi_fmt.fmt.count[1];
148 redo_dma_risc = 1;
151 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
152 redo_dma_risc = 1;
153 if (0 != (rc = videobuf_iolock(q, &buf->vb, NULL)))
154 goto fail;
157 if (redo_dma_risc) {
158 unsigned int bpl, padding, offset;
159 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
161 bpl = 2044; /* max. vbipack */
162 padding = VBI_BPL - bpl;
164 if (fh->vbi_fmt.fmt.count[0] > 0) {
165 rc = bttv_risc_packed(btv, &buf->top,
166 dma->sglist,
167 /* offset */ 0, bpl,
168 padding, skip_lines0,
169 fh->vbi_fmt.fmt.count[0]);
170 if (0 != rc)
171 goto fail;
174 if (fh->vbi_fmt.fmt.count[1] > 0) {
175 offset = fh->vbi_fmt.fmt.count[0] * VBI_BPL;
177 rc = bttv_risc_packed(btv, &buf->bottom,
178 dma->sglist,
179 offset, bpl,
180 padding, skip_lines1,
181 fh->vbi_fmt.fmt.count[1]);
182 if (0 != rc)
183 goto fail;
187 /* VBI capturing ends at VDELAY, start of video capturing,
188 no matter where the RISC program ends. VDELAY minimum is 2,
189 bounds.top is the corresponding first field line number
190 times two. VDELAY counts half field lines. */
191 min_vdelay = MIN_VDELAY;
192 if (fh->vbi_fmt.end >= tvnorm->cropcap.bounds.top)
193 min_vdelay += fh->vbi_fmt.end - tvnorm->cropcap.bounds.top;
195 /* For bttv_buffer_activate_vbi(). */
196 buf->geo.vdelay = min_vdelay;
198 buf->vb.state = VIDEOBUF_PREPARED;
199 buf->vb.field = field;
200 dprintk("buf prepare %p: top=%p bottom=%p field=%s\n",
201 vb, &buf->top, &buf->bottom,
202 v4l2_field_names[buf->vb.field]);
203 return 0;
205 fail:
206 bttv_dma_free(q,btv,buf);
207 return rc;
210 static void
211 vbi_buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
213 struct bttv_fh *fh = q->priv_data;
214 struct bttv *btv = fh->btv;
215 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
217 dprintk("queue %p\n",vb);
218 buf->vb.state = VIDEOBUF_QUEUED;
219 list_add_tail(&buf->vb.queue,&btv->vcapture);
220 if (NULL == btv->cvbi) {
221 fh->btv->loop_irq |= 4;
222 bttv_set_dma(btv,0x0c);
226 static void vbi_buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
228 struct bttv_fh *fh = q->priv_data;
229 struct bttv *btv = fh->btv;
230 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
232 dprintk("free %p\n",vb);
233 bttv_dma_free(q,fh->btv,buf);
236 const struct videobuf_queue_ops bttv_vbi_qops = {
237 .buf_setup = vbi_buffer_setup,
238 .buf_prepare = vbi_buffer_prepare,
239 .buf_queue = vbi_buffer_queue,
240 .buf_release = vbi_buffer_release,
243 /* ----------------------------------------------------------------------- */
245 static int try_fmt(struct v4l2_vbi_format *f, const struct bttv_tvnorm *tvnorm,
246 __s32 crop_start)
248 __s32 min_start, max_start, max_end, f2_offset;
249 unsigned int i;
251 /* For compatibility with earlier driver versions we must pretend
252 the VBI and video capture window may overlap. In reality RISC
253 magic aborts VBI capturing at the first line of video capturing,
254 leaving the rest of the buffer unchanged, usually all zero.
255 VBI capturing must always start before video capturing. >> 1
256 because cropping counts field lines times two. */
257 min_start = tvnorm->vbistart[0];
258 max_start = (crop_start >> 1) - 1;
259 max_end = (tvnorm->cropcap.bounds.top
260 + tvnorm->cropcap.bounds.height) >> 1;
262 if (min_start > max_start)
263 return -EBUSY;
265 BUG_ON(max_start >= max_end);
267 f->sampling_rate = tvnorm->Fsc;
268 f->samples_per_line = VBI_BPL;
269 f->sample_format = V4L2_PIX_FMT_GREY;
270 f->offset = VBI_OFFSET;
272 f2_offset = tvnorm->vbistart[1] - tvnorm->vbistart[0];
274 for (i = 0; i < 2; ++i) {
275 if (0 == f->count[i]) {
276 /* No data from this field. We leave f->start[i]
277 alone because VIDIOCSVBIFMT is w/o and EINVALs
278 when a driver does not support exactly the
279 requested parameters. */
280 } else {
281 s64 start, count;
283 start = clamp(f->start[i], min_start, max_start);
284 /* s64 to prevent overflow. */
285 count = (s64) f->start[i] + f->count[i] - start;
286 f->start[i] = start;
287 f->count[i] = clamp(count, (s64) 1,
288 max_end - start);
291 min_start += f2_offset;
292 max_start += f2_offset;
293 max_end += f2_offset;
296 if (0 == (f->count[0] | f->count[1])) {
297 /* As in earlier driver versions. */
298 f->start[0] = tvnorm->vbistart[0];
299 f->start[1] = tvnorm->vbistart[1];
300 f->count[0] = 1;
301 f->count[1] = 1;
304 f->flags = 0;
306 f->reserved[0] = 0;
307 f->reserved[1] = 0;
309 return 0;
312 int bttv_try_fmt_vbi_cap(struct file *file, void *f, struct v4l2_format *frt)
314 struct bttv_fh *fh = f;
315 struct bttv *btv = fh->btv;
316 const struct bttv_tvnorm *tvnorm;
317 __s32 crop_start;
319 mutex_lock(&btv->lock);
321 tvnorm = &bttv_tvnorms[btv->tvnorm];
322 crop_start = btv->crop_start;
324 mutex_unlock(&btv->lock);
326 return try_fmt(&frt->fmt.vbi, tvnorm, crop_start);
330 int bttv_s_fmt_vbi_cap(struct file *file, void *f, struct v4l2_format *frt)
332 struct bttv_fh *fh = f;
333 struct bttv *btv = fh->btv;
334 const struct bttv_tvnorm *tvnorm;
335 __s32 start1, end;
336 int rc;
338 mutex_lock(&btv->lock);
340 rc = -EBUSY;
341 if (fh->resources & RESOURCE_VBI)
342 goto fail;
344 tvnorm = &bttv_tvnorms[btv->tvnorm];
346 rc = try_fmt(&frt->fmt.vbi, tvnorm, btv->crop_start);
347 if (0 != rc)
348 goto fail;
350 start1 = frt->fmt.vbi.start[1] - tvnorm->vbistart[1] +
351 tvnorm->vbistart[0];
353 /* First possible line of video capturing. Should be
354 max(f->start[0] + f->count[0], start1 + f->count[1]) * 2
355 when capturing both fields. But for compatibility we must
356 pretend the VBI and video capture window may overlap,
357 so end = start + 1, the lowest possible value, times two
358 because vbi_fmt.end counts field lines times two. */
359 end = max(frt->fmt.vbi.start[0], start1) * 2 + 2;
361 mutex_lock(&fh->vbi.vb_lock);
363 fh->vbi_fmt.fmt = frt->fmt.vbi;
364 fh->vbi_fmt.tvnorm = tvnorm;
365 fh->vbi_fmt.end = end;
367 mutex_unlock(&fh->vbi.vb_lock);
369 rc = 0;
371 fail:
372 mutex_unlock(&btv->lock);
374 return rc;
378 int bttv_g_fmt_vbi_cap(struct file *file, void *f, struct v4l2_format *frt)
380 struct bttv_fh *fh = f;
381 const struct bttv_tvnorm *tvnorm;
383 frt->fmt.vbi = fh->vbi_fmt.fmt;
385 tvnorm = &bttv_tvnorms[fh->btv->tvnorm];
387 if (tvnorm != fh->vbi_fmt.tvnorm) {
388 __s32 max_end;
389 unsigned int i;
391 /* As in vbi_buffer_prepare() this imitates the
392 behaviour of earlier driver versions after video
393 standard changes, with default parameters anyway. */
395 max_end = (tvnorm->cropcap.bounds.top
396 + tvnorm->cropcap.bounds.height) >> 1;
398 frt->fmt.vbi.sampling_rate = tvnorm->Fsc;
400 for (i = 0; i < 2; ++i) {
401 __s32 new_start;
403 new_start = frt->fmt.vbi.start[i]
404 + tvnorm->vbistart[i]
405 - fh->vbi_fmt.tvnorm->vbistart[i];
407 frt->fmt.vbi.start[i] = min(new_start, max_end - 1);
408 frt->fmt.vbi.count[i] =
409 min((__s32) frt->fmt.vbi.count[i],
410 max_end - frt->fmt.vbi.start[i]);
412 max_end += tvnorm->vbistart[1]
413 - tvnorm->vbistart[0];
416 return 0;
419 void bttv_vbi_fmt_reset(struct bttv_vbi_fmt *f, unsigned int norm)
421 const struct bttv_tvnorm *tvnorm;
422 unsigned int real_samples_per_line;
423 unsigned int real_count;
425 tvnorm = &bttv_tvnorms[norm];
427 f->fmt.sampling_rate = tvnorm->Fsc;
428 f->fmt.samples_per_line = VBI_BPL;
429 f->fmt.sample_format = V4L2_PIX_FMT_GREY;
430 f->fmt.offset = VBI_OFFSET;
431 f->fmt.start[0] = tvnorm->vbistart[0];
432 f->fmt.start[1] = tvnorm->vbistart[1];
433 f->fmt.count[0] = VBI_DEFLINES;
434 f->fmt.count[1] = VBI_DEFLINES;
435 f->fmt.flags = 0;
436 f->fmt.reserved[0] = 0;
437 f->fmt.reserved[1] = 0;
439 /* For compatibility the buffer size must be 2 * VBI_DEFLINES *
440 VBI_BPL regardless of the current video standard. */
441 real_samples_per_line = 1024 + tvnorm->vbipack * 4;
442 real_count = ((tvnorm->cropcap.defrect.top >> 1)
443 - tvnorm->vbistart[0]);
445 BUG_ON(real_samples_per_line > VBI_BPL);
446 BUG_ON(real_count > VBI_DEFLINES);
448 f->tvnorm = tvnorm;
450 /* See bttv_vbi_fmt_set(). */
451 f->end = tvnorm->vbistart[0] * 2 + 2;