3 bttv - Bt848 frame grabber driver
6 (c) 2002 Gerd Knorr <kraxel@bytesex.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/interrupt.h>
29 #include <linux/kdev_t.h>
33 /* Offset from line sync pulse leading edge (0H) to start of VBI capture,
34 in fCLKx2 pixels. According to the datasheet, VBI capture starts
35 VBI_HDELAY fCLKx1 pixels from the tailing edgeof /HRESET, and /HRESET
36 is 64 fCLKx1 pixels wide. VBI_HDELAY is set to 0, so this should be
37 (64 + 0) * 2 = 128 fCLKx2 pixels. But it's not! The datasheet is
38 Just Plain Wrong. The real value appears to be different for
39 different revisions of the bt8x8 chips, and to be affected by the
40 horizontal scaling factor. Experimentally, the value is measured
42 #define VBI_OFFSET 244
44 #define VBI_DEFLINES 16
45 #define VBI_MAXLINES 32
47 static unsigned int vbibufs = 4;
48 static unsigned int vbi_debug = 0;
50 module_param(vbibufs, int, 0444);
51 module_param(vbi_debug, int, 0644);
52 MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32, default 4");
53 MODULE_PARM_DESC(vbi_debug,"vbi code debug messages, default is 0 (no)");
58 #define dprintk(fmt, arg...) if (vbi_debug) \
59 printk(KERN_DEBUG "bttv%d/vbi: " fmt, btv->c.nr , ## arg)
61 /* ----------------------------------------------------------------------- */
62 /* vbi risc code + mm */
65 vbi_buffer_risc(struct bttv *btv, struct bttv_buffer *buf, int lines)
69 bttv_risc_packed(btv, &buf->top, buf->vb.dma.sglist,
71 bttv_risc_packed(btv, &buf->bottom, buf->vb.dma.sglist,
72 lines * bpl, bpl-4, 4, lines);
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;
84 *size = fh->lines * 2 * 2048;
85 dprintk("setup: lines=%d\n",fh->lines);
89 static int vbi_buffer_prepare(struct videobuf_queue *q,
90 struct videobuf_buffer *vb,
91 enum v4l2_field field)
93 struct bttv_fh *fh = q->priv_data;
94 struct bttv *btv = fh->btv;
95 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
98 buf->vb.size = fh->lines * 2 * 2048;
99 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
102 if (STATE_NEEDS_INIT == buf->vb.state) {
103 if (0 != (rc = videobuf_iolock(q, &buf->vb, NULL)))
105 if (0 != (rc = vbi_buffer_risc(btv,buf,fh->lines)))
108 buf->vb.state = STATE_PREPARED;
109 buf->vb.field = field;
110 dprintk("buf prepare %p: top=%p bottom=%p field=%s\n",
111 vb, &buf->top, &buf->bottom,
112 v4l2_field_names[buf->vb.field]);
116 bttv_dma_free(q,btv,buf);
121 vbi_buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
123 struct bttv_fh *fh = q->priv_data;
124 struct bttv *btv = fh->btv;
125 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
127 dprintk("queue %p\n",vb);
128 buf->vb.state = STATE_QUEUED;
129 list_add_tail(&buf->vb.queue,&btv->vcapture);
130 if (NULL == btv->cvbi) {
131 fh->btv->loop_irq |= 4;
132 bttv_set_dma(btv,0x0c);
136 static void vbi_buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
138 struct bttv_fh *fh = q->priv_data;
139 struct bttv *btv = fh->btv;
140 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
142 dprintk("free %p\n",vb);
143 bttv_dma_free(&fh->cap,fh->btv,buf);
146 struct videobuf_queue_ops bttv_vbi_qops = {
147 .buf_setup = vbi_buffer_setup,
148 .buf_prepare = vbi_buffer_prepare,
149 .buf_queue = vbi_buffer_queue,
150 .buf_release = vbi_buffer_release,
153 /* ----------------------------------------------------------------------- */
155 void bttv_vbi_setlines(struct bttv_fh *fh, struct bttv *btv, int lines)
161 if (lines > VBI_MAXLINES)
162 lines = VBI_MAXLINES;
165 vdelay = btread(BT848_E_VDELAY_LO);
166 if (vdelay < lines*2) {
168 btwrite(vdelay,BT848_E_VDELAY_LO);
169 btwrite(vdelay,BT848_O_VDELAY_LO);
173 void bttv_vbi_try_fmt(struct bttv_fh *fh, struct v4l2_format *f)
175 const struct bttv_tvnorm *tvnorm;
176 s64 count0,count1,count;
178 tvnorm = &bttv_tvnorms[fh->btv->tvnorm];
179 f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
180 f->fmt.vbi.sampling_rate = tvnorm->Fsc;
181 f->fmt.vbi.samples_per_line = 2048;
182 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
183 f->fmt.vbi.offset = VBI_OFFSET;
184 f->fmt.vbi.flags = 0;
186 /* s64 to prevent overflow. */
187 count0 = (s64) f->fmt.vbi.start[0] + f->fmt.vbi.count[0]
188 - tvnorm->vbistart[0];
189 count1 = (s64) f->fmt.vbi.start[1] + f->fmt.vbi.count[1]
190 - tvnorm->vbistart[1];
191 count = clamp (max (count0, count1), (s64) 1, (s64) VBI_MAXLINES);
193 f->fmt.vbi.start[0] = tvnorm->vbistart[0];
194 f->fmt.vbi.start[1] = tvnorm->vbistart[1];
195 f->fmt.vbi.count[0] = count;
196 f->fmt.vbi.count[1] = count;
198 f->fmt.vbi.reserved[0] = 0;
199 f->fmt.vbi.reserved[1] = 0;
202 void bttv_vbi_get_fmt(struct bttv_fh *fh, struct v4l2_format *f)
204 const struct bttv_tvnorm *tvnorm;
206 tvnorm = &bttv_tvnorms[fh->btv->tvnorm];
207 memset(f,0,sizeof(*f));
208 f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
209 f->fmt.vbi.sampling_rate = tvnorm->Fsc;
210 f->fmt.vbi.samples_per_line = 2048;
211 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
212 f->fmt.vbi.offset = VBI_OFFSET;
213 f->fmt.vbi.start[0] = tvnorm->vbistart[0];
214 f->fmt.vbi.start[1] = tvnorm->vbistart[1];
215 f->fmt.vbi.count[0] = fh->lines;
216 f->fmt.vbi.count[1] = fh->lines;
217 f->fmt.vbi.flags = 0;
220 /* ----------------------------------------------------------------------- */