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