Linux v2.6.15-rc7
[pohmelfs.git] / drivers / media / video / bttv-vbi.c
blobf4f58c60f152b2d178156c211ef13aad1a493bd5
1 /*
3 bttv - Bt848 frame grabber driver
4 vbi interface
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>
26 #include <linux/fs.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/interrupt.h>
30 #include <linux/kdev_t.h>
31 #include <asm/io.h>
32 #include "bttvp.h"
34 #define VBI_DEFLINES 16
35 #define VBI_MAXLINES 32
37 static unsigned int vbibufs = 4;
38 static unsigned int vbi_debug = 0;
40 module_param(vbibufs, int, 0444);
41 module_param(vbi_debug, int, 0644);
42 MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32, default 4");
43 MODULE_PARM_DESC(vbi_debug,"vbi code debug messages, default is 0 (no)");
45 #ifdef dprintk
46 # undef dprintk
47 #endif
48 #define dprintk(fmt, arg...) if (vbi_debug) \
49 printk(KERN_DEBUG "bttv%d/vbi: " fmt, btv->c.nr , ## arg)
51 /* ----------------------------------------------------------------------- */
52 /* vbi risc code + mm */
54 static int
55 vbi_buffer_risc(struct bttv *btv, struct bttv_buffer *buf, int lines)
57 int bpl = 2048;
59 bttv_risc_packed(btv, &buf->top, buf->vb.dma.sglist,
60 0, bpl-4, 4, lines);
61 bttv_risc_packed(btv, &buf->bottom, buf->vb.dma.sglist,
62 lines * bpl, bpl-4, 4, lines);
63 return 0;
66 static int vbi_buffer_setup(struct videobuf_queue *q,
67 unsigned int *count, unsigned int *size)
69 struct bttv_fh *fh = q->priv_data;
70 struct bttv *btv = fh->btv;
72 if (0 == *count)
73 *count = vbibufs;
74 *size = fh->lines * 2 * 2048;
75 dprintk("setup: lines=%d\n",fh->lines);
76 return 0;
79 static int vbi_buffer_prepare(struct videobuf_queue *q,
80 struct videobuf_buffer *vb,
81 enum v4l2_field field)
83 struct bttv_fh *fh = q->priv_data;
84 struct bttv *btv = fh->btv;
85 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
86 int rc;
88 buf->vb.size = fh->lines * 2 * 2048;
89 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
90 return -EINVAL;
92 if (STATE_NEEDS_INIT == buf->vb.state) {
93 if (0 != (rc = videobuf_iolock(btv->c.pci, &buf->vb, NULL)))
94 goto fail;
95 if (0 != (rc = vbi_buffer_risc(btv,buf,fh->lines)))
96 goto fail;
98 buf->vb.state = STATE_PREPARED;
99 buf->vb.field = field;
100 dprintk("buf prepare %p: top=%p bottom=%p field=%s\n",
101 vb, &buf->top, &buf->bottom,
102 v4l2_field_names[buf->vb.field]);
103 return 0;
105 fail:
106 bttv_dma_free(btv,buf);
107 return rc;
110 static void
111 vbi_buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
113 struct bttv_fh *fh = q->priv_data;
114 struct bttv *btv = fh->btv;
115 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
117 dprintk("queue %p\n",vb);
118 buf->vb.state = STATE_QUEUED;
119 list_add_tail(&buf->vb.queue,&btv->vcapture);
120 if (NULL == btv->cvbi) {
121 fh->btv->loop_irq |= 4;
122 bttv_set_dma(btv,0x0c);
126 static void vbi_buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
128 struct bttv_fh *fh = q->priv_data;
129 struct bttv *btv = fh->btv;
130 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
132 dprintk("free %p\n",vb);
133 bttv_dma_free(fh->btv,buf);
136 struct videobuf_queue_ops bttv_vbi_qops = {
137 .buf_setup = vbi_buffer_setup,
138 .buf_prepare = vbi_buffer_prepare,
139 .buf_queue = vbi_buffer_queue,
140 .buf_release = vbi_buffer_release,
143 /* ----------------------------------------------------------------------- */
145 void bttv_vbi_setlines(struct bttv_fh *fh, struct bttv *btv, int lines)
147 int vdelay;
149 if (lines < 1)
150 lines = 1;
151 if (lines > VBI_MAXLINES)
152 lines = VBI_MAXLINES;
153 fh->lines = lines;
155 vdelay = btread(BT848_E_VDELAY_LO);
156 if (vdelay < lines*2) {
157 vdelay = lines*2;
158 btwrite(vdelay,BT848_E_VDELAY_LO);
159 btwrite(vdelay,BT848_O_VDELAY_LO);
163 void bttv_vbi_try_fmt(struct bttv_fh *fh, struct v4l2_format *f)
165 const struct bttv_tvnorm *tvnorm;
166 u32 start0,start1;
167 s32 count0,count1,count;
169 tvnorm = &bttv_tvnorms[fh->btv->tvnorm];
170 f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
171 f->fmt.vbi.sampling_rate = tvnorm->Fsc;
172 f->fmt.vbi.samples_per_line = 2048;
173 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
174 f->fmt.vbi.offset = 244;
175 f->fmt.vbi.flags = 0;
176 switch (fh->btv->tvnorm) {
177 case 1: /* NTSC */
178 start0 = 10;
179 start1 = 273;
180 break;
181 case 0: /* PAL */
182 case 2: /* SECAM */
183 default:
184 start0 = 7;
185 start1 = 320;
188 count0 = (f->fmt.vbi.start[0] + f->fmt.vbi.count[0]) - start0;
189 count1 = (f->fmt.vbi.start[1] + f->fmt.vbi.count[1]) - start1;
190 count = max(count0,count1);
191 if (count > VBI_MAXLINES)
192 count = VBI_MAXLINES;
193 if (count < 1)
194 count = 1;
196 f->fmt.vbi.start[0] = start0;
197 f->fmt.vbi.start[1] = start1;
198 f->fmt.vbi.count[0] = count;
199 f->fmt.vbi.count[1] = count;
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 = 244;
213 f->fmt.vbi.count[0] = fh->lines;
214 f->fmt.vbi.count[1] = fh->lines;
215 f->fmt.vbi.flags = 0;
216 switch (fh->btv->tvnorm) {
217 case 1: /* NTSC */
218 f->fmt.vbi.start[0] = 10;
219 f->fmt.vbi.start[1] = 273;
220 break;
221 case 0: /* PAL */
222 case 2: /* SECAM */
223 default:
224 f->fmt.vbi.start[0] = 7;
225 f->fmt.vbi.start[1] = 319;
229 /* ----------------------------------------------------------------------- */
231 * Local variables:
232 * c-basic-offset: 8
233 * End: