ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / media / video / saa7134 / saa7134-vbi.c
1 /*
2  * device driver for philips saa7134 based TV cards
3  * video4linux video interface
4  *
5  * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
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/init.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27
28 #include "saa7134-reg.h"
29 #include "saa7134.h"
30
31 /* ------------------------------------------------------------------ */
32
33 static unsigned int vbi_debug  = 0;
34 MODULE_PARM(vbi_debug,"i");
35 MODULE_PARM_DESC(vbi_debug,"enable debug messages [vbi]");
36
37 static unsigned int vbibufs = 4;
38 MODULE_PARM(vbibufs,"i");
39 MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32");
40
41 #define dprintk(fmt, arg...)    if (vbi_debug) \
42         printk(KERN_DEBUG "%s/vbi: " fmt, dev->name , ## arg)
43
44 /* ------------------------------------------------------------------ */
45
46 #define VBI_LINE_COUNT     16
47 #define VBI_LINE_LENGTH  2048
48 #define VBI_SCALE       0x200
49
50 static void task_init(struct saa7134_dev *dev, struct saa7134_buf *buf,
51                       int task)
52 {
53         struct saa7134_tvnorm *norm = dev->tvnorm;
54         
55         /* setup video scaler */
56         saa_writeb(SAA7134_VBI_H_START1(task), norm->h_start     &  0xff);
57         saa_writeb(SAA7134_VBI_H_START2(task), norm->h_start     >> 8);
58         saa_writeb(SAA7134_VBI_H_STOP1(task),  norm->h_stop      &  0xff);
59         saa_writeb(SAA7134_VBI_H_STOP2(task),  norm->h_stop      >> 8);
60         saa_writeb(SAA7134_VBI_V_START1(task), norm->vbi_v_start &  0xff);
61         saa_writeb(SAA7134_VBI_V_START2(task), norm->vbi_v_start >> 8);
62         saa_writeb(SAA7134_VBI_V_STOP1(task),  norm->vbi_v_stop  &  0xff);
63         saa_writeb(SAA7134_VBI_V_STOP2(task),  norm->vbi_v_stop  >> 8);
64
65         saa_writeb(SAA7134_VBI_H_SCALE_INC1(task),        VBI_SCALE & 0xff);
66         saa_writeb(SAA7134_VBI_H_SCALE_INC2(task),        VBI_SCALE >> 8);
67         saa_writeb(SAA7134_VBI_PHASE_OFFSET_LUMA(task),   0x00);
68         saa_writeb(SAA7134_VBI_PHASE_OFFSET_CHROMA(task), 0x00);
69
70         saa_writeb(SAA7134_VBI_H_LEN1(task), buf->vb.width   & 0xff);
71         saa_writeb(SAA7134_VBI_H_LEN2(task), buf->vb.width   >> 8);
72         saa_writeb(SAA7134_VBI_V_LEN1(task), buf->vb.height  & 0xff);
73         saa_writeb(SAA7134_VBI_V_LEN2(task), buf->vb.height  >> 8);
74
75         saa_andorb(SAA7134_DATA_PATH(task), 0xc0, 0x00);
76 }
77
78 /* ------------------------------------------------------------------ */
79
80 static int buffer_activate(struct saa7134_dev *dev,
81                            struct saa7134_buf *buf,
82                            struct saa7134_buf *next)
83 {
84         unsigned long control,base;
85
86         dprintk("buffer_activate [%p]\n",buf);
87         buf->vb.state = STATE_ACTIVE;
88         buf->top_seen = 0;
89
90         task_init(dev,buf,TASK_A);
91         task_init(dev,buf,TASK_B);
92         saa_writeb(SAA7134_OFMT_DATA_A, 0x06);
93         saa_writeb(SAA7134_OFMT_DATA_B, 0x06);
94
95         /* DMA: setup channel 2+3 (= VBI Task A+B) */
96         base    = saa7134_buffer_base(buf);
97         control = SAA7134_RS_CONTROL_BURST_16 |
98                 SAA7134_RS_CONTROL_ME |
99                 (buf->pt->dma >> 12);
100         saa_writel(SAA7134_RS_BA1(2),base);
101         saa_writel(SAA7134_RS_BA2(2),base + buf->vb.size/2);
102         saa_writel(SAA7134_RS_PITCH(2),buf->vb.width);
103         saa_writel(SAA7134_RS_CONTROL(2),control);
104         saa_writel(SAA7134_RS_BA1(3),base);
105         saa_writel(SAA7134_RS_BA2(3),base + buf->vb.size/2);
106         saa_writel(SAA7134_RS_PITCH(3),buf->vb.width);
107         saa_writel(SAA7134_RS_CONTROL(3),control);
108
109         /* start DMA */
110         saa7134_set_dmabits(dev);
111         mod_timer(&dev->vbi_q.timeout, jiffies+BUFFER_TIMEOUT);
112
113         return 0;
114 }
115
116 static int buffer_prepare(struct file *file, struct videobuf_buffer *vb,
117                           enum v4l2_field field)
118 {
119         struct saa7134_fh *fh   = file->private_data;
120         struct saa7134_dev *dev = fh->dev;
121         struct saa7134_buf *buf = (struct saa7134_buf *)vb;
122         struct saa7134_tvnorm *norm = dev->tvnorm;
123         unsigned int lines, llength, size;
124         int err;
125
126         lines   = norm->vbi_v_stop - norm->vbi_v_start +1;
127         if (lines > VBI_LINE_COUNT)
128                 lines = VBI_LINE_COUNT;
129 #if 1
130         llength = VBI_LINE_LENGTH;
131 #else
132         llength = (norm->h_stop - norm->h_start +1) * 2;
133         if (llength > VBI_LINE_LENGTH)
134                 llength = VBI_LINE_LENGTH;
135 #endif
136         size = lines * llength * 2;
137         if (0 != buf->vb.baddr  &&  buf->vb.bsize < size)
138                 return -EINVAL;
139
140         if (buf->vb.size != size)
141                 saa7134_dma_free(dev,buf);
142
143         if (STATE_NEEDS_INIT == buf->vb.state) {
144                 buf->vb.width  = llength;
145                 buf->vb.height = lines;
146                 buf->vb.size   = size;
147                 buf->pt        = &fh->pt_vbi;
148
149                 err = videobuf_iolock(dev->pci,&buf->vb,NULL);
150                 if (err)
151                         goto oops;
152                 err = saa7134_pgtable_build(dev->pci,buf->pt,
153                                             buf->vb.dma.sglist,
154                                             buf->vb.dma.sglen,
155                                             saa7134_buffer_startpage(buf));
156                 if (err)
157                         goto oops;
158         }
159         buf->vb.state = STATE_PREPARED;
160         buf->activate = buffer_activate;
161         buf->vb.field = field;
162         return 0;
163
164  oops:
165         saa7134_dma_free(dev,buf);
166         return err;
167 }
168
169 static int
170 buffer_setup(struct file *file, unsigned int *count, unsigned int *size)
171 {
172         struct saa7134_fh *fh   = file->private_data;
173         struct saa7134_dev *dev = fh->dev;
174         int llength,lines;
175         
176         lines   = dev->tvnorm->vbi_v_stop - dev->tvnorm->vbi_v_start +1;
177 #if 1
178         llength = VBI_LINE_LENGTH;
179 #else
180         llength = (norm->h_stop - norm->h_start +1) * 2;
181         if (llength > VBI_LINE_LENGTH)
182                 llength = VBI_LINE_LENGTH;
183 #endif
184         *size = lines * llength * 2;
185         if (0 == *count)
186                 *count = vbibufs;
187         *count = saa7134_buffer_count(*size,*count);
188         return 0;
189 }
190
191 static void buffer_queue(struct file *file, struct videobuf_buffer *vb)
192 {
193         struct saa7134_fh *fh = file->private_data;
194         struct saa7134_dev *dev = fh->dev;
195         struct saa7134_buf *buf = (struct saa7134_buf *)vb;
196         
197         saa7134_buffer_queue(dev,&dev->vbi_q,buf);
198 }
199
200 static void buffer_release(struct file *file, struct videobuf_buffer *vb)
201 {
202         struct saa7134_fh *fh   = file->private_data;
203         struct saa7134_dev *dev = fh->dev;
204         struct saa7134_buf *buf = (struct saa7134_buf *)vb;
205         
206         saa7134_dma_free(dev,buf);
207 }
208
209 struct videobuf_queue_ops saa7134_vbi_qops = {
210         .buf_setup    = buffer_setup,
211         .buf_prepare  = buffer_prepare,
212         .buf_queue    = buffer_queue,
213         .buf_release  = buffer_release,
214 };
215
216 /* ------------------------------------------------------------------ */
217
218 int saa7134_vbi_init1(struct saa7134_dev *dev)
219 {
220         INIT_LIST_HEAD(&dev->vbi_q.queue);
221         init_timer(&dev->vbi_q.timeout);
222         dev->vbi_q.timeout.function = saa7134_buffer_timeout;
223         dev->vbi_q.timeout.data     = (unsigned long)(&dev->vbi_q);
224         dev->vbi_q.dev              = dev;
225
226         if (vbibufs < 2)
227                 vbibufs = 2;
228         if (vbibufs > VIDEO_MAX_FRAME)
229                 vbibufs = VIDEO_MAX_FRAME;
230         return 0;
231 }
232
233 int saa7134_vbi_fini(struct saa7134_dev *dev)
234 {
235         /* nothing */
236         return 0;
237 }
238
239 void saa7134_irq_vbi_done(struct saa7134_dev *dev, unsigned long status)
240 {
241         spin_lock(&dev->slock);
242         if (dev->vbi_q.curr) {
243                 dev->vbi_fieldcount++;
244                 /* make sure we have seen both fields */
245                 if ((status & 0x10) == 0x00) {
246                         dev->vbi_q.curr->top_seen = 1;
247                         goto done;
248                 }
249                 if (!dev->vbi_q.curr->top_seen)
250                         goto done;
251
252                 dev->vbi_q.curr->vb.field_count = dev->vbi_fieldcount;
253                 saa7134_buffer_finish(dev,&dev->vbi_q,STATE_DONE);
254         }
255         saa7134_buffer_next(dev,&dev->vbi_q);
256
257  done:
258         spin_unlock(&dev->slock);
259 }
260
261 /* ----------------------------------------------------------- */
262 /*
263  * Local variables:
264  * c-basic-offset: 8
265  * End:
266  */