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