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