Merge branch 'upstream-jgarzik' of git://git.tuxdriver.com/git/netdev-jwl
[linux-2.6] / drivers / media / video / bttv-vbi.c
1 /*
2
3     bttv - Bt848 frame grabber driver
4     vbi interface
5
6     (c) 2002 Gerd Knorr <kraxel@bytesex.org>
7
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.
12
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.
17
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.
21 */
22
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"
33
34 #define VBI_DEFLINES 16
35 #define VBI_MAXLINES 32
36
37 static unsigned int vbibufs = 4;
38 static unsigned int vbi_debug = 0;
39
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)");
44
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)
50
51 /* ----------------------------------------------------------------------- */
52 /* vbi risc code + mm                                                      */
53
54 static int
55 vbi_buffer_risc(struct bttv *btv, struct bttv_buffer *buf, int lines)
56 {
57         int bpl = 2048;
58
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;
64 }
65
66 static int vbi_buffer_setup(struct videobuf_queue *q,
67                             unsigned int *count, unsigned int *size)
68 {
69         struct bttv_fh *fh = q->priv_data;
70         struct bttv *btv = fh->btv;
71
72         if (0 == *count)
73                 *count = vbibufs;
74         *size = fh->lines * 2 * 2048;
75         dprintk("setup: lines=%d\n",fh->lines);
76         return 0;
77 }
78
79 static int vbi_buffer_prepare(struct videobuf_queue *q,
80                               struct videobuf_buffer *vb,
81                               enum v4l2_field field)
82 {
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;
87
88         buf->vb.size = fh->lines * 2 * 2048;
89         if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
90                 return -EINVAL;
91
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;
97         }
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;
104
105  fail:
106         bttv_dma_free(btv,buf);
107         return rc;
108 }
109
110 static void
111 vbi_buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
112 {
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);
116
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);
123         }
124 }
125
126 static void vbi_buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
127 {
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);
131
132         dprintk("free %p\n",vb);
133         bttv_dma_free(fh->btv,buf);
134 }
135
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,
141 };
142
143 /* ----------------------------------------------------------------------- */
144
145 void bttv_vbi_setlines(struct bttv_fh *fh, struct bttv *btv, int lines)
146 {
147         int vdelay;
148
149         if (lines < 1)
150                 lines = 1;
151         if (lines > VBI_MAXLINES)
152                 lines = VBI_MAXLINES;
153         fh->lines = lines;
154
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);
160         }
161 }
162
163 void bttv_vbi_try_fmt(struct bttv_fh *fh, struct v4l2_format *f)
164 {
165         const struct bttv_tvnorm *tvnorm;
166         u32 start0,start1;
167         s32 count0,count1,count;
168
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;
186         }
187
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;
195
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;
200 }
201
202 void bttv_vbi_get_fmt(struct bttv_fh *fh, struct v4l2_format *f)
203 {
204         const struct bttv_tvnorm *tvnorm;
205
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;
226         }
227 }
228
229 /* ----------------------------------------------------------------------- */
230 /*
231  * Local variables:
232  * c-basic-offset: 8
233  * End:
234  */