Merge /spare/repo/linux-2.6/
[linux-2.6] / drivers / media / video / saa7134 / saa7134-ts.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 #include <linux/delay.h>
30
31 #include "saa7134-reg.h"
32 #include "saa7134.h"
33
34 /* ------------------------------------------------------------------ */
35
36 static unsigned int ts_debug  = 0;
37 module_param(ts_debug, int, 0644);
38 MODULE_PARM_DESC(ts_debug,"enable debug messages [ts]");
39
40 #define dprintk(fmt, arg...)    if (ts_debug) \
41         printk(KERN_DEBUG "%s/ts: " fmt, dev->name , ## arg)
42
43 /* ------------------------------------------------------------------ */
44
45 static int buffer_activate(struct saa7134_dev *dev,
46                            struct saa7134_buf *buf,
47                            struct saa7134_buf *next)
48 {
49         u32 control;
50
51         dprintk("buffer_activate [%p]",buf);
52         buf->vb.state = STATE_ACTIVE;
53         buf->top_seen = 0;
54
55         /* dma: setup channel 5 (= TS) */
56         control = SAA7134_RS_CONTROL_BURST_16 |
57                 SAA7134_RS_CONTROL_ME |
58                 (buf->pt->dma >> 12);
59
60         if (NULL == next)
61                 next = buf;
62         if (V4L2_FIELD_TOP == buf->vb.field) {
63                 dprintk("- [top]     buf=%p next=%p\n",buf,next);
64                 saa_writel(SAA7134_RS_BA1(5),saa7134_buffer_base(buf));
65                 saa_writel(SAA7134_RS_BA2(5),saa7134_buffer_base(next));
66         } else {
67                 dprintk("- [bottom]  buf=%p next=%p\n",buf,next);
68                 saa_writel(SAA7134_RS_BA1(5),saa7134_buffer_base(next));
69                 saa_writel(SAA7134_RS_BA2(5),saa7134_buffer_base(buf));
70         }
71         saa_writel(SAA7134_RS_PITCH(5),TS_PACKET_SIZE);
72         saa_writel(SAA7134_RS_CONTROL(5),control);
73
74         /* start DMA */
75         saa7134_set_dmabits(dev);
76
77         mod_timer(&dev->ts_q.timeout, jiffies+BUFFER_TIMEOUT);
78         return 0;
79 }
80
81 static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
82                           enum v4l2_field field)
83 {
84         struct saa7134_dev *dev = q->priv_data;
85         struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
86         unsigned int lines, llength, size;
87         int err;
88
89         dprintk("buffer_prepare [%p,%s]\n",buf,v4l2_field_names[field]);
90
91         llength = TS_PACKET_SIZE;
92         lines = dev->ts.nr_packets;
93
94         size = lines * llength;
95         if (0 != buf->vb.baddr  &&  buf->vb.bsize < size)
96                 return -EINVAL;
97
98         if (buf->vb.size != size) {
99                 saa7134_dma_free(dev,buf);
100         }
101
102         if (STATE_NEEDS_INIT == buf->vb.state) {
103                 buf->vb.width  = llength;
104                 buf->vb.height = lines;
105                 buf->vb.size   = size;
106                 buf->pt        = &dev->ts.pt_ts;
107
108                 err = videobuf_iolock(dev->pci,&buf->vb,NULL);
109                 if (err)
110                         goto oops;
111                 err = saa7134_pgtable_build(dev->pci,buf->pt,
112                                             buf->vb.dma.sglist,
113                                             buf->vb.dma.sglen,
114                                             saa7134_buffer_startpage(buf));
115                 if (err)
116                         goto oops;
117         }
118         buf->vb.state = STATE_PREPARED;
119         buf->activate = buffer_activate;
120         buf->vb.field = field;
121         return 0;
122
123  oops:
124         saa7134_dma_free(dev,buf);
125         return err;
126 }
127
128 static int
129 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
130 {
131         struct saa7134_dev *dev = q->priv_data;
132
133         *size = TS_PACKET_SIZE * dev->ts.nr_packets;
134         if (0 == *count)
135                 *count = dev->ts.nr_bufs;
136         *count = saa7134_buffer_count(*size,*count);
137         return 0;
138 }
139
140 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
141 {
142         struct saa7134_dev *dev = q->priv_data;
143         struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
144
145         saa7134_buffer_queue(dev,&dev->ts_q,buf);
146 }
147
148 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
149 {
150         struct saa7134_dev *dev = q->priv_data;
151         struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
152
153         saa7134_dma_free(dev,buf);
154 }
155
156 struct videobuf_queue_ops saa7134_ts_qops = {
157         .buf_setup    = buffer_setup,
158         .buf_prepare  = buffer_prepare,
159         .buf_queue    = buffer_queue,
160         .buf_release  = buffer_release,
161 };
162 EXPORT_SYMBOL_GPL(saa7134_ts_qops);
163
164 /* ----------------------------------------------------------- */
165 /* exported stuff                                              */
166
167 static unsigned int tsbufs = 4;
168 module_param(tsbufs, int, 0444);
169 MODULE_PARM_DESC(tsbufs,"number of ts buffers, range 2-32");
170
171 static unsigned int ts_nr_packets = 30;
172 module_param(ts_nr_packets, int, 0444);
173 MODULE_PARM_DESC(ts_nr_packets,"size of a ts buffers (in ts packets)");
174
175 int saa7134_ts_init1(struct saa7134_dev *dev)
176 {
177         /* sanitycheck insmod options */
178         if (tsbufs < 2)
179                 tsbufs = 2;
180         if (tsbufs > VIDEO_MAX_FRAME)
181                 tsbufs = VIDEO_MAX_FRAME;
182         if (ts_nr_packets < 4)
183                 ts_nr_packets = 4;
184         if (ts_nr_packets > 312)
185                 ts_nr_packets = 312;
186         dev->ts.nr_bufs    = tsbufs;
187         dev->ts.nr_packets = ts_nr_packets;
188
189         INIT_LIST_HEAD(&dev->ts_q.queue);
190         init_timer(&dev->ts_q.timeout);
191         dev->ts_q.timeout.function = saa7134_buffer_timeout;
192         dev->ts_q.timeout.data     = (unsigned long)(&dev->ts_q);
193         dev->ts_q.dev              = dev;
194         dev->ts_q.need_two         = 1;
195         saa7134_pgtable_alloc(dev->pci,&dev->ts.pt_ts);
196
197         /* init TS hw */
198         saa_writeb(SAA7134_TS_SERIAL1, 0x00);  /* deactivate TS softreset */
199         saa_writeb(SAA7134_TS_PARALLEL, 0xec); /* TSSOP high active, TSVAL high active, TSLOCK ignored */
200         saa_writeb(SAA7134_TS_PARALLEL_SERIAL, (TS_PACKET_SIZE-1));
201         saa_writeb(SAA7134_TS_DMA0, ((dev->ts.nr_packets-1)&0xff));
202         saa_writeb(SAA7134_TS_DMA1, (((dev->ts.nr_packets-1)>>8)&0xff));
203         saa_writeb(SAA7134_TS_DMA2, ((((dev->ts.nr_packets-1)>>16)&0x3f) | 0x00)); /* TSNOPIT=0, TSCOLAP=0 */
204
205         return 0;
206 }
207
208 int saa7134_ts_fini(struct saa7134_dev *dev)
209 {
210         saa7134_pgtable_free(dev->pci,&dev->ts.pt_ts);
211         return 0;
212 }
213
214
215 void saa7134_irq_ts_done(struct saa7134_dev *dev, unsigned long status)
216 {
217         enum v4l2_field field;
218
219         spin_lock(&dev->slock);
220         if (dev->ts_q.curr) {
221                 field = dev->ts_q.curr->vb.field;
222                 if (field == V4L2_FIELD_TOP) {
223                         if ((status & 0x100000) != 0x100000)
224                                 goto done;
225                 } else {
226                         if ((status & 0x100000) != 0x000000)
227                                 goto done;
228                 }
229                 saa7134_buffer_finish(dev,&dev->ts_q,STATE_DONE);
230         }
231         saa7134_buffer_next(dev,&dev->ts_q);
232
233  done:
234         spin_unlock(&dev->slock);
235 }
236
237 /* ----------------------------------------------------------- */
238 /*
239  * Local variables:
240  * c-basic-offset: 8
241  * End:
242  */