VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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         if (NULL == btv->cvbi) {
118                 fh->btv->curr.irqflags |= 4;
119                 bttv_set_dma(btv,0x0c,fh->btv->curr.irqflags);
120         }
121 }
122
123 static void vbi_buffer_release(struct file *file, struct videobuf_buffer *vb)
124 {
125         struct bttv_fh *fh = file->private_data;
126         struct bttv *btv = fh->btv;
127         struct bttv_buffer *buf = (struct bttv_buffer*)vb;
128         
129         dprintk("free %p\n",vb);
130         bttv_dma_free(fh->btv,buf);
131 }
132
133 struct videobuf_queue_ops bttv_vbi_qops = {
134         .buf_setup    = vbi_buffer_setup,
135         .buf_prepare  = vbi_buffer_prepare,
136         .buf_queue    = vbi_buffer_queue,
137         .buf_release  = vbi_buffer_release,
138 };
139
140 /* ----------------------------------------------------------------------- */
141
142 void bttv_vbi_setlines(struct bttv_fh *fh, struct bttv *btv, int lines)
143 {
144         int vdelay;
145
146         if (lines < 1)
147                 lines = 1;
148         if (lines > VBI_MAXLINES)
149                 lines = VBI_MAXLINES;
150         fh->lines = lines;
151
152         vdelay = btread(BT848_E_VDELAY_LO);
153         if (vdelay < lines*2) {
154                 vdelay = lines*2;
155                 btwrite(vdelay,BT848_E_VDELAY_LO);
156                 btwrite(vdelay,BT848_O_VDELAY_LO);
157         }
158 }
159
160 void bttv_vbi_try_fmt(struct bttv_fh *fh, struct v4l2_format *f)
161 {
162         const struct bttv_tvnorm *tvnorm;
163         u32 start0,start1;
164         s32 count0,count1,count;
165
166         tvnorm = &bttv_tvnorms[fh->btv->tvnorm];
167         f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
168         f->fmt.vbi.sampling_rate    = tvnorm->Fsc;
169         f->fmt.vbi.samples_per_line = 2048;
170         f->fmt.vbi.sample_format    = V4L2_PIX_FMT_GREY;
171         f->fmt.vbi.offset           = 244;
172         f->fmt.vbi.flags            = 0;
173         switch (fh->btv->tvnorm) {
174         case 1: /* NTSC */
175                 start0 = 10;
176                 start1 = 273;
177                 break;
178         case 0: /* PAL */
179         case 2: /* SECAM */
180         default:
181                 start0 = 7;
182                 start1 = 319;
183         }
184
185         count0 = (f->fmt.vbi.start[0] + f->fmt.vbi.count[0]) - start0;
186         count1 = (f->fmt.vbi.start[1] + f->fmt.vbi.count[1]) - start1;
187         count  = max(count0,count1);
188         if (count > VBI_MAXLINES)
189                 count = VBI_MAXLINES;
190         if (count < 1)
191                 count = 1;
192
193         f->fmt.vbi.start[0] = start0;
194         f->fmt.vbi.start[1] = start1;
195         f->fmt.vbi.count[0] = count;
196         f->fmt.vbi.count[1] = count;
197 }
198
199 void bttv_vbi_get_fmt(struct bttv_fh *fh, struct v4l2_format *f)
200 {
201         const struct bttv_tvnorm *tvnorm;
202
203         tvnorm = &bttv_tvnorms[fh->btv->tvnorm];
204         memset(f,0,sizeof(*f));
205         f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
206         f->fmt.vbi.sampling_rate    = tvnorm->Fsc;
207         f->fmt.vbi.samples_per_line = 2048;
208         f->fmt.vbi.sample_format    = V4L2_PIX_FMT_GREY;
209         f->fmt.vbi.offset           = 244;
210         f->fmt.vbi.count[0]         = fh->lines;
211         f->fmt.vbi.count[1]         = fh->lines;
212         f->fmt.vbi.flags            = 0;
213         switch (fh->btv->tvnorm) {
214         case 1: /* NTSC */
215                 f->fmt.vbi.start[0] = 10;
216                 f->fmt.vbi.start[1] = 273;
217                 break;
218         case 0: /* PAL */
219         case 2: /* SECAM */
220         default:
221                 f->fmt.vbi.start[0] = 7;
222                 f->fmt.vbi.start[1] = 319;
223         }
224 }
225
226 /* ----------------------------------------------------------------------- */
227 /*
228  * Local variables:
229  * c-basic-offset: 8
230  * End:
231  */