ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / media / video / bttv-vbi.c
1 /*
2     bttv - Bt848 frame grabber driver
3     vbi interface
4     
5     (c) 2002 Gerd Knorr <kraxel@bytesex.org>
6     
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11     
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16     
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include <linux/module.h>
23 #include <linux/errno.h>
24 #include <linux/fs.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/interrupt.h>
28 #include <linux/kdev_t.h>
29 #include <asm/io.h>
30 #include "bttvp.h"
31
32 #define VBI_DEFLINES 16
33 #define VBI_MAXLINES 32
34
35 static unsigned int vbibufs = 4;
36 static unsigned int vbi_debug = 0;
37
38 MODULE_PARM(vbibufs,"i");
39 MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32, default 4");
40 MODULE_PARM(vbi_debug,"i");
41 MODULE_PARM_DESC(vbi_debug,"vbi code debug messages, default is 0 (no)");
42
43 #ifdef dprintk
44 # undef dprintk
45 #endif
46 #define dprintk(fmt, arg...)    if (vbi_debug) \
47         printk(KERN_DEBUG "bttv%d/vbi: " fmt, btv->c.nr , ## arg)
48
49 /* ----------------------------------------------------------------------- */
50 /* vbi risc code + mm                                                      */
51
52 static int
53 vbi_buffer_risc(struct bttv *btv, struct bttv_buffer *buf, int lines)
54 {
55         int bpl = 2048;
56
57         bttv_risc_packed(btv, &buf->top, buf->vb.dma.sglist,
58                          0, bpl-4, 4, lines);
59         bttv_risc_packed(btv, &buf->bottom, buf->vb.dma.sglist,
60                          lines * bpl, bpl-4, 4, lines);
61         return 0;
62 }
63
64 static int vbi_buffer_setup(struct file *file,
65                             unsigned int *count, unsigned int *size)
66 {
67         struct bttv_fh *fh = file->private_data;
68         struct bttv *btv = fh->btv;
69
70         if (0 == *count)
71                 *count = vbibufs;
72         *size = fh->lines * 2 * 2048;
73         dprintk("setup: lines=%d\n",fh->lines);
74         return 0;
75 }
76
77 static int vbi_buffer_prepare(struct file *file, struct videobuf_buffer *vb,
78                               enum v4l2_field field)
79 {
80         struct bttv_fh *fh = file->private_data;
81         struct bttv *btv = fh->btv;
82         struct bttv_buffer *buf = (struct bttv_buffer*)vb;
83         int rc;
84         
85         buf->vb.size = fh->lines * 2 * 2048;
86         if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
87                 return -EINVAL;
88
89         if (STATE_NEEDS_INIT == buf->vb.state) {
90                 if (0 != (rc = videobuf_iolock(btv->c.pci, &buf->vb, NULL)))
91                         goto fail;
92                 if (0 != (rc = vbi_buffer_risc(btv,buf,fh->lines)))
93                         goto fail;
94         }
95         buf->vb.state = STATE_PREPARED;
96         buf->vb.field = field;
97         dprintk("buf prepare %p: top=%p bottom=%p field=%s\n",
98                 vb, &buf->top, &buf->bottom,
99                 v4l2_field_names[buf->vb.field]);
100         return 0;
101
102  fail:
103         bttv_dma_free(btv,buf);
104         return rc;
105 }
106
107 static void
108 vbi_buffer_queue(struct file *file, struct videobuf_buffer *vb)
109 {
110         struct bttv_fh *fh = file->private_data;
111         struct bttv *btv = fh->btv;
112         struct bttv_buffer *buf = (struct bttv_buffer*)vb;
113         
114         dprintk("queue %p\n",vb);
115         buf->vb.state = STATE_QUEUED;
116         list_add_tail(&buf->vb.queue,&btv->vcapture);
117         bttv_set_dma(btv,0x0c,1);
118 }
119
120 static void vbi_buffer_release(struct file *file, struct videobuf_buffer *vb)
121 {
122         struct bttv_fh *fh = file->private_data;
123         struct bttv *btv = fh->btv;
124         struct bttv_buffer *buf = (struct bttv_buffer*)vb;
125         
126         dprintk("free %p\n",vb);
127         bttv_dma_free(fh->btv,buf);
128 }
129
130 struct videobuf_queue_ops bttv_vbi_qops = {
131         .buf_setup    = vbi_buffer_setup,
132         .buf_prepare  = vbi_buffer_prepare,
133         .buf_queue    = vbi_buffer_queue,
134         .buf_release  = vbi_buffer_release,
135 };
136
137 /* ----------------------------------------------------------------------- */
138
139 void bttv_vbi_setlines(struct bttv_fh *fh, struct bttv *btv, int lines)
140 {
141         int vdelay;
142
143         if (lines < 1)
144                 lines = 1;
145         if (lines > VBI_MAXLINES)
146                 lines = VBI_MAXLINES;
147         fh->lines = lines;
148
149         vdelay = btread(BT848_E_VDELAY_LO);
150         if (vdelay < lines*2) {
151                 vdelay = lines*2;
152                 btwrite(vdelay,BT848_E_VDELAY_LO);
153                 btwrite(vdelay,BT848_O_VDELAY_LO);
154         }
155 }
156
157 void bttv_vbi_try_fmt(struct bttv_fh *fh, struct v4l2_format *f)
158 {
159         const struct bttv_tvnorm *tvnorm;
160         u32 start0,start1;
161         s32 count0,count1,count;
162
163         tvnorm = &bttv_tvnorms[fh->btv->tvnorm];
164         f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
165         f->fmt.vbi.sampling_rate    = tvnorm->Fsc;
166         f->fmt.vbi.samples_per_line = 2048;
167         f->fmt.vbi.sample_format    = V4L2_PIX_FMT_GREY;
168         f->fmt.vbi.offset           = 244;
169         f->fmt.vbi.flags            = 0;
170         switch (fh->btv->tvnorm) {
171         case 1: /* NTSC */
172                 start0 = 10;
173                 start1 = 273;
174                 break;
175         case 0: /* PAL */
176         case 2: /* SECAM */
177         default:
178                 start0 = 7;
179                 start1 = 319;
180         }
181
182         count0 = (f->fmt.vbi.start[0] + f->fmt.vbi.count[0]) - start0;
183         count1 = (f->fmt.vbi.start[1] + f->fmt.vbi.count[1]) - start1;
184         count  = max(count0,count1);
185         if (count > VBI_MAXLINES)
186                 count = VBI_MAXLINES;
187         if (count < 1)
188                 count = 1;
189
190         f->fmt.vbi.start[0] = start0;
191         f->fmt.vbi.start[1] = start1;
192         f->fmt.vbi.count[0] = count;
193         f->fmt.vbi.count[1] = count;
194 }
195
196 void bttv_vbi_get_fmt(struct bttv_fh *fh, struct v4l2_format *f)
197 {
198         const struct bttv_tvnorm *tvnorm;
199
200         tvnorm = &bttv_tvnorms[fh->btv->tvnorm];
201         memset(f,0,sizeof(*f));
202         f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
203         f->fmt.vbi.sampling_rate    = tvnorm->Fsc;
204         f->fmt.vbi.samples_per_line = 2048;
205         f->fmt.vbi.sample_format    = V4L2_PIX_FMT_GREY;
206         f->fmt.vbi.offset           = 244;
207         f->fmt.vbi.count[0]         = fh->lines;
208         f->fmt.vbi.count[1]         = fh->lines;
209         f->fmt.vbi.flags            = 0;
210         switch (fh->btv->tvnorm) {
211         case 1: /* NTSC */
212                 f->fmt.vbi.start[0] = 10;
213                 f->fmt.vbi.start[1] = 273;
214                 break;
215         case 0: /* PAL */
216         case 2: /* SECAM */
217         default:
218                 f->fmt.vbi.start[0] = 7;
219                 f->fmt.vbi.start[1] = 319;
220         }
221 }
222
223 /* ----------------------------------------------------------------------- */
224 /*
225  * Local variables:
226  * c-basic-offset: 8
227  * End:
228  */