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