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/errno.h>
29 #include <linux/kernel.h>
30 #include <linux/interrupt.h>
31 #include <linux/kdev_t.h>
35 /* Offset from line sync pulse leading edge (0H) to start of VBI capture,
36 in fCLKx2 pixels. According to the datasheet, VBI capture starts
37 VBI_HDELAY fCLKx1 pixels from the tailing edgeof /HRESET, and /HRESET
38 is 64 fCLKx1 pixels wide. VBI_HDELAY is set to 0, so this should be
39 (64 + 0) * 2 = 128 fCLKx2 pixels. But it's not! The datasheet is
40 Just Plain Wrong. The real value appears to be different for
41 different revisions of the bt8x8 chips, and to be affected by the
42 horizontal scaling factor. Experimentally, the value is measured
44 #define VBI_OFFSET 244
46 /* 2048 for compatibility with earlier driver versions. The driver
47 really stores 1024 + tvnorm->vbipack * 4 samples per line in the
48 buffer. Note tvnorm->vbipack is <= 0xFF (limit of VBIPACK_LO + HI
49 is 0x1FF DWORDs) and VBI read()s store a frame counter in the last
50 four bytes of the VBI image. */
54 #define VBI_DEFLINES 16
56 static unsigned int vbibufs = 4;
57 static unsigned int vbi_debug = 0;
59 module_param(vbibufs, int, 0444);
60 module_param(vbi_debug, int, 0644);
61 MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32, default 4");
62 MODULE_PARM_DESC(vbi_debug,"vbi code debug messages, default is 0 (no)");
67 #define dprintk(fmt, arg...) if (vbi_debug) \
68 printk(KERN_DEBUG "bttv%d/vbi: " fmt, btv->c.nr , ## arg)
70 #define IMAGE_SIZE(fmt) \
71 (((fmt)->count[0] + (fmt)->count[1]) * (fmt)->samples_per_line)
73 /* ----------------------------------------------------------------------- */
74 /* vbi risc code + mm */
76 static int vbi_buffer_setup(struct videobuf_queue *q,
77 unsigned int *count, unsigned int *size)
79 struct bttv_fh *fh = q->priv_data;
80 struct bttv *btv = fh->btv;
85 *size = IMAGE_SIZE(&fh->vbi_fmt.fmt);
87 dprintk("setup: samples=%u start=%d,%d count=%u,%u\n",
88 fh->vbi_fmt.fmt.samples_per_line,
89 fh->vbi_fmt.fmt.start[0],
90 fh->vbi_fmt.fmt.start[1],
91 fh->vbi_fmt.fmt.count[0],
92 fh->vbi_fmt.fmt.count[1]);
97 static int vbi_buffer_prepare(struct videobuf_queue *q,
98 struct videobuf_buffer *vb,
99 enum v4l2_field field)
101 struct bttv_fh *fh = q->priv_data;
102 struct bttv *btv = fh->btv;
103 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
104 const struct bttv_tvnorm *tvnorm;
105 unsigned int skip_lines0, skip_lines1, min_vdelay;
109 buf->vb.size = IMAGE_SIZE(&fh->vbi_fmt.fmt);
110 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
113 tvnorm = fh->vbi_fmt.tvnorm;
115 /* There's no VBI_VDELAY register, RISC must skip the lines
116 we don't want. With default parameters we skip zero lines
117 as earlier driver versions did. The driver permits video
118 standard changes while capturing, so we use vbi_fmt.tvnorm
119 instead of btv->tvnorm to skip zero lines after video
120 standard changes as well. */
125 if (fh->vbi_fmt.fmt.count[0] > 0)
126 skip_lines0 = max(0, (fh->vbi_fmt.fmt.start[0]
127 - tvnorm->vbistart[0]));
128 if (fh->vbi_fmt.fmt.count[1] > 0)
129 skip_lines1 = max(0, (fh->vbi_fmt.fmt.start[1]
130 - tvnorm->vbistart[1]));
134 if (buf->vbi_skip[0] != skip_lines0 ||
135 buf->vbi_skip[1] != skip_lines1 ||
136 buf->vbi_count[0] != fh->vbi_fmt.fmt.count[0] ||
137 buf->vbi_count[1] != fh->vbi_fmt.fmt.count[1]) {
138 buf->vbi_skip[0] = skip_lines0;
139 buf->vbi_skip[1] = skip_lines1;
140 buf->vbi_count[0] = fh->vbi_fmt.fmt.count[0];
141 buf->vbi_count[1] = fh->vbi_fmt.fmt.count[1];
145 if (STATE_NEEDS_INIT == buf->vb.state) {
147 if (0 != (rc = videobuf_iolock(q, &buf->vb, NULL)))
152 unsigned int bpl, padding, offset;
154 bpl = 2044; /* max. vbipack */
155 padding = VBI_BPL - bpl;
157 if (fh->vbi_fmt.fmt.count[0] > 0) {
158 rc = bttv_risc_packed(btv, &buf->top,
161 padding, skip_lines0,
162 fh->vbi_fmt.fmt.count[0]);
167 if (fh->vbi_fmt.fmt.count[1] > 0) {
168 offset = fh->vbi_fmt.fmt.count[0] * VBI_BPL;
170 rc = bttv_risc_packed(btv, &buf->bottom,
173 padding, skip_lines1,
174 fh->vbi_fmt.fmt.count[1]);
180 /* VBI capturing ends at VDELAY, start of video capturing,
181 no matter where the RISC program ends. VDELAY minimum is 2,
182 bounds.top is the corresponding first field line number
183 times two. VDELAY counts half field lines. */
184 min_vdelay = MIN_VDELAY;
185 if (fh->vbi_fmt.end >= tvnorm->cropcap.bounds.top)
186 min_vdelay += fh->vbi_fmt.end - tvnorm->cropcap.bounds.top;
188 /* For bttv_buffer_activate_vbi(). */
189 buf->geo.vdelay = min_vdelay;
191 buf->vb.state = STATE_PREPARED;
192 buf->vb.field = field;
193 dprintk("buf prepare %p: top=%p bottom=%p field=%s\n",
194 vb, &buf->top, &buf->bottom,
195 v4l2_field_names[buf->vb.field]);
199 bttv_dma_free(q,btv,buf);
204 vbi_buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
206 struct bttv_fh *fh = q->priv_data;
207 struct bttv *btv = fh->btv;
208 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
210 dprintk("queue %p\n",vb);
211 buf->vb.state = STATE_QUEUED;
212 list_add_tail(&buf->vb.queue,&btv->vcapture);
213 if (NULL == btv->cvbi) {
214 fh->btv->loop_irq |= 4;
215 bttv_set_dma(btv,0x0c);
219 static void vbi_buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
221 struct bttv_fh *fh = q->priv_data;
222 struct bttv *btv = fh->btv;
223 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
225 dprintk("free %p\n",vb);
226 bttv_dma_free(q,fh->btv,buf);
229 struct videobuf_queue_ops bttv_vbi_qops = {
230 .buf_setup = vbi_buffer_setup,
231 .buf_prepare = vbi_buffer_prepare,
232 .buf_queue = vbi_buffer_queue,
233 .buf_release = vbi_buffer_release,
236 /* ----------------------------------------------------------------------- */
239 try_fmt (struct v4l2_vbi_format * f,
240 const struct bttv_tvnorm * tvnorm,
243 __s32 min_start, max_start, max_end, f2_offset;
246 /* For compatibility with earlier driver versions we must pretend
247 the VBI and video capture window may overlap. In reality RISC
248 magic aborts VBI capturing at the first line of video capturing,
249 leaving the rest of the buffer unchanged, usually all zero.
250 VBI capturing must always start before video capturing. >> 1
251 because cropping counts field lines times two. */
252 min_start = tvnorm->vbistart[0];
253 max_start = (crop_start >> 1) - 1;
254 max_end = (tvnorm->cropcap.bounds.top
255 + tvnorm->cropcap.bounds.height) >> 1;
257 if (min_start > max_start)
260 BUG_ON(max_start >= max_end);
262 f->sampling_rate = tvnorm->Fsc;
263 f->samples_per_line = VBI_BPL;
264 f->sample_format = V4L2_PIX_FMT_GREY;
265 f->offset = VBI_OFFSET;
267 f2_offset = tvnorm->vbistart[1] - tvnorm->vbistart[0];
269 for (i = 0; i < 2; ++i) {
270 if (0 == f->count[i]) {
271 /* No data from this field. We leave f->start[i]
272 alone because VIDIOCSVBIFMT is w/o and EINVALs
273 when a driver does not support exactly the
274 requested parameters. */
278 start = clamp(f->start[i], min_start, max_start);
279 /* s64 to prevent overflow. */
280 count = (s64) f->start[i] + f->count[i] - start;
282 f->count[i] = clamp(count, (s64) 1,
286 min_start += f2_offset;
287 max_start += f2_offset;
288 max_end += f2_offset;
291 if (0 == (f->count[0] | f->count[1])) {
292 /* As in earlier driver versions. */
293 f->start[0] = tvnorm->vbistart[0];
294 f->start[1] = tvnorm->vbistart[1];
308 bttv_vbi_try_fmt (struct bttv_fh * fh,
309 struct v4l2_vbi_format * f)
311 struct bttv *btv = fh->btv;
312 const struct bttv_tvnorm *tvnorm;
315 mutex_lock(&btv->lock);
317 tvnorm = &bttv_tvnorms[btv->tvnorm];
318 crop_start = btv->crop_start;
320 mutex_unlock(&btv->lock);
322 return try_fmt(f, tvnorm, crop_start);
326 bttv_vbi_set_fmt (struct bttv_fh * fh,
327 struct v4l2_vbi_format * f)
329 struct bttv *btv = fh->btv;
330 const struct bttv_tvnorm *tvnorm;
334 mutex_lock(&btv->lock);
337 if (fh->resources & RESOURCE_VBI)
340 tvnorm = &bttv_tvnorms[btv->tvnorm];
342 rc = try_fmt(f, tvnorm, btv->crop_start);
346 start1 = f->start[1] - tvnorm->vbistart[1] + tvnorm->vbistart[0];
348 /* First possible line of video capturing. Should be
349 max(f->start[0] + f->count[0], start1 + f->count[1]) * 2
350 when capturing both fields. But for compatibility we must
351 pretend the VBI and video capture window may overlap,
352 so end = start + 1, the lowest possible value, times two
353 because vbi_fmt.end counts field lines times two. */
354 end = max(f->start[0], start1) * 2 + 2;
356 mutex_lock(&fh->vbi.lock);
358 fh->vbi_fmt.fmt = *f;
359 fh->vbi_fmt.tvnorm = tvnorm;
360 fh->vbi_fmt.end = end;
362 mutex_unlock(&fh->vbi.lock);
367 mutex_unlock(&btv->lock);
373 bttv_vbi_get_fmt (struct bttv_fh * fh,
374 struct v4l2_vbi_format * f)
376 const struct bttv_tvnorm *tvnorm;
378 *f = fh->vbi_fmt.fmt;
380 tvnorm = &bttv_tvnorms[fh->btv->tvnorm];
382 if (tvnorm != fh->vbi_fmt.tvnorm) {
386 /* As in vbi_buffer_prepare() this imitates the
387 behaviour of earlier driver versions after video
388 standard changes, with default parameters anyway. */
390 max_end = (tvnorm->cropcap.bounds.top
391 + tvnorm->cropcap.bounds.height) >> 1;
393 f->sampling_rate = tvnorm->Fsc;
395 for (i = 0; i < 2; ++i) {
398 new_start = f->start[i]
399 + tvnorm->vbistart[i]
400 - fh->vbi_fmt.tvnorm->vbistart[i];
402 f->start[i] = min(new_start, max_end - 1);
403 f->count[i] = min((__s32) f->count[i],
404 max_end - f->start[i]);
406 max_end += tvnorm->vbistart[1]
407 - tvnorm->vbistart[0];
413 bttv_vbi_fmt_reset (struct bttv_vbi_fmt * f,
416 const struct bttv_tvnorm *tvnorm;
417 unsigned int real_samples_per_line;
418 unsigned int real_count;
420 tvnorm = &bttv_tvnorms[norm];
422 f->fmt.sampling_rate = tvnorm->Fsc;
423 f->fmt.samples_per_line = VBI_BPL;
424 f->fmt.sample_format = V4L2_PIX_FMT_GREY;
425 f->fmt.offset = VBI_OFFSET;
426 f->fmt.start[0] = tvnorm->vbistart[0];
427 f->fmt.start[1] = tvnorm->vbistart[1];
428 f->fmt.count[0] = VBI_DEFLINES;
429 f->fmt.count[1] = VBI_DEFLINES;
431 f->fmt.reserved[0] = 0;
432 f->fmt.reserved[1] = 0;
434 /* For compatibility the buffer size must be 2 * VBI_DEFLINES *
435 VBI_BPL regardless of the current video standard. */
436 real_samples_per_line = 1024 + tvnorm->vbipack * 4;
437 real_count = ((tvnorm->cropcap.defrect.top >> 1)
438 - tvnorm->vbistart[0]);
440 BUG_ON(real_samples_per_line > VBI_BPL);
441 BUG_ON(real_count > VBI_DEFLINES);
445 /* See bttv_vbi_fmt_set(). */
446 f->end = tvnorm->vbistart[0] * 2 + 2;
449 /* ----------------------------------------------------------------------- */