3 bttv - Bt848 frame grabber driver
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 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/errno.h>
30 #include <linux/kernel.h>
31 #include <linux/interrupt.h>
32 #include <linux/kdev_t.h>
36 /* Offset from line sync pulse leading edge (0H) to start of VBI capture,
37 in fCLKx2 pixels. According to the datasheet, VBI capture starts
38 VBI_HDELAY fCLKx1 pixels from the tailing edgeof /HRESET, and /HRESET
39 is 64 fCLKx1 pixels wide. VBI_HDELAY is set to 0, so this should be
40 (64 + 0) * 2 = 128 fCLKx2 pixels. But it's not! The datasheet is
41 Just Plain Wrong. The real value appears to be different for
42 different revisions of the bt8x8 chips, and to be affected by the
43 horizontal scaling factor. Experimentally, the value is measured
45 #define VBI_OFFSET 244
47 /* 2048 for compatibility with earlier driver versions. The driver
48 really stores 1024 + tvnorm->vbipack * 4 samples per line in the
49 buffer. Note tvnorm->vbipack is <= 0xFF (limit of VBIPACK_LO + HI
50 is 0x1FF DWORDs) and VBI read()s store a frame counter in the last
51 four bytes of the VBI image. */
55 #define VBI_DEFLINES 16
57 static unsigned int vbibufs = 4;
58 static unsigned int vbi_debug = 0;
60 module_param(vbibufs, int, 0444);
61 module_param(vbi_debug, int, 0644);
62 MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32, default 4");
63 MODULE_PARM_DESC(vbi_debug,"vbi code debug messages, default is 0 (no)");
68 #define dprintk(fmt, arg...) if (vbi_debug) \
69 printk(KERN_DEBUG "bttv%d/vbi: " fmt, btv->c.nr , ## arg)
71 #define IMAGE_SIZE(fmt) \
72 (((fmt)->count[0] + (fmt)->count[1]) * (fmt)->samples_per_line)
74 /* ----------------------------------------------------------------------- */
75 /* vbi risc code + mm */
77 static int vbi_buffer_setup(struct videobuf_queue *q,
78 unsigned int *count, unsigned int *size)
80 struct bttv_fh *fh = q->priv_data;
81 struct bttv *btv = fh->btv;
86 *size = IMAGE_SIZE(&fh->vbi_fmt.fmt);
88 dprintk("setup: samples=%u start=%d,%d count=%u,%u\n",
89 fh->vbi_fmt.fmt.samples_per_line,
90 fh->vbi_fmt.fmt.start[0],
91 fh->vbi_fmt.fmt.start[1],
92 fh->vbi_fmt.fmt.count[0],
93 fh->vbi_fmt.fmt.count[1]);
98 static int vbi_buffer_prepare(struct videobuf_queue *q,
99 struct videobuf_buffer *vb,
100 enum v4l2_field field)
102 struct bttv_fh *fh = q->priv_data;
103 struct bttv *btv = fh->btv;
104 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
105 const struct bttv_tvnorm *tvnorm;
106 unsigned int skip_lines0, skip_lines1, min_vdelay;
110 buf->vb.size = IMAGE_SIZE(&fh->vbi_fmt.fmt);
111 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
114 tvnorm = fh->vbi_fmt.tvnorm;
116 /* There's no VBI_VDELAY register, RISC must skip the lines
117 we don't want. With default parameters we skip zero lines
118 as earlier driver versions did. The driver permits video
119 standard changes while capturing, so we use vbi_fmt.tvnorm
120 instead of btv->tvnorm to skip zero lines after video
121 standard changes as well. */
126 if (fh->vbi_fmt.fmt.count[0] > 0)
127 skip_lines0 = max(0, (fh->vbi_fmt.fmt.start[0]
128 - tvnorm->vbistart[0]));
129 if (fh->vbi_fmt.fmt.count[1] > 0)
130 skip_lines1 = max(0, (fh->vbi_fmt.fmt.start[1]
131 - tvnorm->vbistart[1]));
135 if (buf->vbi_skip[0] != skip_lines0 ||
136 buf->vbi_skip[1] != skip_lines1 ||
137 buf->vbi_count[0] != fh->vbi_fmt.fmt.count[0] ||
138 buf->vbi_count[1] != fh->vbi_fmt.fmt.count[1]) {
139 buf->vbi_skip[0] = skip_lines0;
140 buf->vbi_skip[1] = skip_lines1;
141 buf->vbi_count[0] = fh->vbi_fmt.fmt.count[0];
142 buf->vbi_count[1] = fh->vbi_fmt.fmt.count[1];
146 if (STATE_NEEDS_INIT == buf->vb.state) {
148 if (0 != (rc = videobuf_iolock(q, &buf->vb, NULL)))
153 unsigned int bpl, padding, offset;
155 bpl = 2044; /* max. vbipack */
156 padding = VBI_BPL - bpl;
158 if (fh->vbi_fmt.fmt.count[0] > 0) {
159 rc = bttv_risc_packed(btv, &buf->top,
162 padding, skip_lines0,
163 fh->vbi_fmt.fmt.count[0]);
168 if (fh->vbi_fmt.fmt.count[1] > 0) {
169 offset = fh->vbi_fmt.fmt.count[0] * VBI_BPL;
171 rc = bttv_risc_packed(btv, &buf->bottom,
174 padding, skip_lines1,
175 fh->vbi_fmt.fmt.count[1]);
181 /* VBI capturing ends at VDELAY, start of video capturing,
182 no matter where the RISC program ends. VDELAY minimum is 2,
183 bounds.top is the corresponding first field line number
184 times two. VDELAY counts half field lines. */
185 min_vdelay = MIN_VDELAY;
186 if (fh->vbi_fmt.end >= tvnorm->cropcap.bounds.top)
187 min_vdelay += fh->vbi_fmt.end - tvnorm->cropcap.bounds.top;
189 /* For bttv_buffer_activate_vbi(). */
190 buf->geo.vdelay = min_vdelay;
192 buf->vb.state = STATE_PREPARED;
193 buf->vb.field = field;
194 dprintk("buf prepare %p: top=%p bottom=%p field=%s\n",
195 vb, &buf->top, &buf->bottom,
196 v4l2_field_names[buf->vb.field]);
200 bttv_dma_free(q,btv,buf);
205 vbi_buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
207 struct bttv_fh *fh = q->priv_data;
208 struct bttv *btv = fh->btv;
209 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
211 dprintk("queue %p\n",vb);
212 buf->vb.state = STATE_QUEUED;
213 list_add_tail(&buf->vb.queue,&btv->vcapture);
214 if (NULL == btv->cvbi) {
215 fh->btv->loop_irq |= 4;
216 bttv_set_dma(btv,0x0c);
220 static void vbi_buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
222 struct bttv_fh *fh = q->priv_data;
223 struct bttv *btv = fh->btv;
224 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
226 dprintk("free %p\n",vb);
227 bttv_dma_free(q,fh->btv,buf);
230 struct videobuf_queue_ops bttv_vbi_qops = {
231 .buf_setup = vbi_buffer_setup,
232 .buf_prepare = vbi_buffer_prepare,
233 .buf_queue = vbi_buffer_queue,
234 .buf_release = vbi_buffer_release,
237 /* ----------------------------------------------------------------------- */
240 try_fmt (struct v4l2_vbi_format * f,
241 const struct bttv_tvnorm * tvnorm,
244 __s32 min_start, max_start, max_end, f2_offset;
247 /* For compatibility with earlier driver versions we must pretend
248 the VBI and video capture window may overlap. In reality RISC
249 magic aborts VBI capturing at the first line of video capturing,
250 leaving the rest of the buffer unchanged, usually all zero.
251 VBI capturing must always start before video capturing. >> 1
252 because cropping counts field lines times two. */
253 min_start = tvnorm->vbistart[0];
254 max_start = (crop_start >> 1) - 1;
255 max_end = (tvnorm->cropcap.bounds.top
256 + tvnorm->cropcap.bounds.height) >> 1;
258 if (min_start > max_start)
261 BUG_ON(max_start >= max_end);
263 f->sampling_rate = tvnorm->Fsc;
264 f->samples_per_line = VBI_BPL;
265 f->sample_format = V4L2_PIX_FMT_GREY;
266 f->offset = VBI_OFFSET;
268 f2_offset = tvnorm->vbistart[1] - tvnorm->vbistart[0];
270 for (i = 0; i < 2; ++i) {
271 if (0 == f->count[i]) {
272 /* No data from this field. We leave f->start[i]
273 alone because VIDIOCSVBIFMT is w/o and EINVALs
274 when a driver does not support exactly the
275 requested parameters. */
279 start = clamp(f->start[i], min_start, max_start);
280 /* s64 to prevent overflow. */
281 count = (s64) f->start[i] + f->count[i] - start;
283 f->count[i] = clamp(count, (s64) 1,
287 min_start += f2_offset;
288 max_start += f2_offset;
289 max_end += f2_offset;
292 if (0 == (f->count[0] | f->count[1])) {
293 /* As in earlier driver versions. */
294 f->start[0] = tvnorm->vbistart[0];
295 f->start[1] = tvnorm->vbistart[1];
309 bttv_vbi_try_fmt (struct bttv_fh * fh,
310 struct v4l2_vbi_format * f)
312 struct bttv *btv = fh->btv;
313 const struct bttv_tvnorm *tvnorm;
316 mutex_lock(&btv->lock);
318 tvnorm = &bttv_tvnorms[btv->tvnorm];
319 crop_start = btv->crop_start;
321 mutex_unlock(&btv->lock);
323 return try_fmt(f, tvnorm, crop_start);
327 bttv_vbi_set_fmt (struct bttv_fh * fh,
328 struct v4l2_vbi_format * f)
330 struct bttv *btv = fh->btv;
331 const struct bttv_tvnorm *tvnorm;
335 mutex_lock(&btv->lock);
338 if (fh->resources & RESOURCE_VBI)
341 tvnorm = &bttv_tvnorms[btv->tvnorm];
343 rc = try_fmt(f, tvnorm, btv->crop_start);
347 start1 = f->start[1] - tvnorm->vbistart[1] + tvnorm->vbistart[0];
349 /* First possible line of video capturing. Should be
350 max(f->start[0] + f->count[0], start1 + f->count[1]) * 2
351 when capturing both fields. But for compatibility we must
352 pretend the VBI and video capture window may overlap,
353 so end = start + 1, the lowest possible value, times two
354 because vbi_fmt.end counts field lines times two. */
355 end = max(f->start[0], start1) * 2 + 2;
357 mutex_lock(&fh->vbi.lock);
359 fh->vbi_fmt.fmt = *f;
360 fh->vbi_fmt.tvnorm = tvnorm;
361 fh->vbi_fmt.end = end;
363 mutex_unlock(&fh->vbi.lock);
368 mutex_unlock(&btv->lock);
374 bttv_vbi_get_fmt (struct bttv_fh * fh,
375 struct v4l2_vbi_format * f)
377 const struct bttv_tvnorm *tvnorm;
379 *f = fh->vbi_fmt.fmt;
381 tvnorm = &bttv_tvnorms[fh->btv->tvnorm];
383 if (tvnorm != fh->vbi_fmt.tvnorm) {
387 /* As in vbi_buffer_prepare() this imitates the
388 behaviour of earlier driver versions after video
389 standard changes, with default parameters anyway. */
391 max_end = (tvnorm->cropcap.bounds.top
392 + tvnorm->cropcap.bounds.height) >> 1;
394 f->sampling_rate = tvnorm->Fsc;
396 for (i = 0; i < 2; ++i) {
399 new_start = f->start[i]
400 + tvnorm->vbistart[i]
401 - fh->vbi_fmt.tvnorm->vbistart[i];
403 f->start[i] = min(new_start, max_end - 1);
404 f->count[i] = min((__s32) f->count[i],
405 max_end - f->start[i]);
407 max_end += tvnorm->vbistart[1]
408 - tvnorm->vbistart[0];
414 bttv_vbi_fmt_reset (struct bttv_vbi_fmt * f,
417 const struct bttv_tvnorm *tvnorm;
418 unsigned int real_samples_per_line;
419 unsigned int real_count;
421 tvnorm = &bttv_tvnorms[norm];
423 f->fmt.sampling_rate = tvnorm->Fsc;
424 f->fmt.samples_per_line = VBI_BPL;
425 f->fmt.sample_format = V4L2_PIX_FMT_GREY;
426 f->fmt.offset = VBI_OFFSET;
427 f->fmt.start[0] = tvnorm->vbistart[0];
428 f->fmt.start[1] = tvnorm->vbistart[1];
429 f->fmt.count[0] = VBI_DEFLINES;
430 f->fmt.count[1] = VBI_DEFLINES;
432 f->fmt.reserved[0] = 0;
433 f->fmt.reserved[1] = 0;
435 /* For compatibility the buffer size must be 2 * VBI_DEFLINES *
436 VBI_BPL regardless of the current video standard. */
437 real_samples_per_line = 1024 + tvnorm->vbipack * 4;
438 real_count = ((tvnorm->cropcap.defrect.top >> 1)
439 - tvnorm->vbistart[0]);
441 BUG_ON(real_samples_per_line > VBI_BPL);
442 BUG_ON(real_count > VBI_DEFLINES);
446 /* See bttv_vbi_fmt_set(). */
447 f->end = tvnorm->vbistart[0] * 2 + 2;
450 /* ----------------------------------------------------------------------- */