Merge branch 'sched-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[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+TS_BUFFER_TIMEOUT);
69
70         if (!dev->ts_started)
71                 saa7134_ts_start(dev);
72
73         return 0;
74 }
75
76 static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
77                 enum v4l2_field field)
78 {
79         struct saa7134_dev *dev = q->priv_data;
80         struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
81         unsigned int lines, llength, size;
82         int err;
83
84         dprintk("buffer_prepare [%p,%s]\n",buf,v4l2_field_names[field]);
85
86         llength = TS_PACKET_SIZE;
87         lines = dev->ts.nr_packets;
88
89         size = lines * llength;
90         if (0 != buf->vb.baddr  &&  buf->vb.bsize < size)
91                 return -EINVAL;
92
93         if (buf->vb.size != size) {
94                 saa7134_dma_free(q,buf);
95         }
96
97         if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
98
99                 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
100
101                 dprintk("buffer_prepare: needs_init\n");
102
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(q,&buf->vb,NULL);
109                 if (err)
110                         goto oops;
111                 err = saa7134_pgtable_build(dev->pci,buf->pt,
112                                             dma->sglist,
113                                             dma->sglen,
114                                             saa7134_buffer_startpage(buf));
115                 if (err)
116                         goto oops;
117         }
118
119         buf->vb.state = VIDEOBUF_PREPARED;
120         buf->activate = buffer_activate;
121         buf->vb.field = field;
122         return 0;
123
124  oops:
125         saa7134_dma_free(q,buf);
126         return err;
127 }
128
129 static int
130 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
131 {
132         struct saa7134_dev *dev = q->priv_data;
133
134         *size = TS_PACKET_SIZE * dev->ts.nr_packets;
135         if (0 == *count)
136                 *count = dev->ts.nr_bufs;
137         *count = saa7134_buffer_count(*size,*count);
138
139         return 0;
140 }
141
142 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
143 {
144         struct saa7134_dev *dev = q->priv_data;
145         struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
146
147         saa7134_buffer_queue(dev,&dev->ts_q,buf);
148 }
149
150 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
151 {
152         struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
153         struct saa7134_dev *dev = q->priv_data;
154
155         if (dev->ts_started)
156                 saa7134_ts_stop(dev);
157
158         saa7134_dma_free(q,buf);
159 }
160
161 struct videobuf_queue_ops saa7134_ts_qops = {
162         .buf_setup    = buffer_setup,
163         .buf_prepare  = buffer_prepare,
164         .buf_queue    = buffer_queue,
165         .buf_release  = buffer_release,
166 };
167 EXPORT_SYMBOL_GPL(saa7134_ts_qops);
168
169 /* ----------------------------------------------------------- */
170 /* exported stuff                                              */
171
172 static unsigned int tsbufs = 8;
173 module_param(tsbufs, int, 0444);
174 MODULE_PARM_DESC(tsbufs, "number of ts buffers for read/write IO, range 2-32");
175
176 static unsigned int ts_nr_packets = 64;
177 module_param(ts_nr_packets, int, 0444);
178 MODULE_PARM_DESC(ts_nr_packets,"size of a ts buffers (in ts packets)");
179
180 int saa7134_ts_init_hw(struct saa7134_dev *dev)
181 {
182         /* deactivate TS softreset */
183         saa_writeb(SAA7134_TS_SERIAL1, 0x00);
184         /* TSSOP high active, TSVAL high active, TSLOCK ignored */
185         saa_writeb(SAA7134_TS_PARALLEL, 0x6c);
186         saa_writeb(SAA7134_TS_PARALLEL_SERIAL, (TS_PACKET_SIZE-1));
187         saa_writeb(SAA7134_TS_DMA0, ((dev->ts.nr_packets-1)&0xff));
188         saa_writeb(SAA7134_TS_DMA1, (((dev->ts.nr_packets-1)>>8)&0xff));
189         /* TSNOPIT=0, TSCOLAP=0 */
190         saa_writeb(SAA7134_TS_DMA2,
191                 ((((dev->ts.nr_packets-1)>>16)&0x3f) | 0x00));
192
193         return 0;
194 }
195
196 int saa7134_ts_init1(struct saa7134_dev *dev)
197 {
198         /* sanitycheck insmod options */
199         if (tsbufs < 2)
200                 tsbufs = 2;
201         if (tsbufs > VIDEO_MAX_FRAME)
202                 tsbufs = VIDEO_MAX_FRAME;
203         if (ts_nr_packets < 4)
204                 ts_nr_packets = 4;
205         if (ts_nr_packets > 312)
206                 ts_nr_packets = 312;
207         dev->ts.nr_bufs    = tsbufs;
208         dev->ts.nr_packets = ts_nr_packets;
209
210         INIT_LIST_HEAD(&dev->ts_q.queue);
211         init_timer(&dev->ts_q.timeout);
212         dev->ts_q.timeout.function = saa7134_buffer_timeout;
213         dev->ts_q.timeout.data     = (unsigned long)(&dev->ts_q);
214         dev->ts_q.dev              = dev;
215         dev->ts_q.need_two         = 1;
216         dev->ts_started            = 0;
217         saa7134_pgtable_alloc(dev->pci,&dev->ts.pt_ts);
218
219         /* init TS hw */
220         saa7134_ts_init_hw(dev);
221
222         return 0;
223 }
224
225 /* Function for stop TS */
226 int saa7134_ts_stop(struct saa7134_dev *dev)
227 {
228         dprintk("TS stop\n");
229
230         BUG_ON(!dev->ts_started);
231
232         /* Stop TS stream */
233         switch (saa7134_boards[dev->board].ts_type) {
234         case SAA7134_MPEG_TS_PARALLEL:
235                 saa_writeb(SAA7134_TS_PARALLEL, 0x6c);
236                 dev->ts_started = 0;
237                 break;
238         case SAA7134_MPEG_TS_SERIAL:
239                 saa_writeb(SAA7134_TS_SERIAL0, 0x40);
240                 dev->ts_started = 0;
241                 break;
242         }
243         return 0;
244 }
245
246 /* Function for start TS */
247 int saa7134_ts_start(struct saa7134_dev *dev)
248 {
249         dprintk("TS start\n");
250
251         BUG_ON(dev->ts_started);
252
253         saa_writeb(SAA7134_TS_SERIAL1, 0x00);
254         saa_writeb(SAA7134_TS_SERIAL1, 0x03);
255         saa_writeb(SAA7134_TS_SERIAL1, 0x00);
256         saa_writeb(SAA7134_TS_SERIAL1, 0x01);
257
258         /* TS clock non-inverted */
259         saa_writeb(SAA7134_TS_SERIAL1, 0x00);
260
261         /* Start TS stream */
262         switch (saa7134_boards[dev->board].ts_type) {
263         case SAA7134_MPEG_TS_PARALLEL:
264                 saa_writeb(SAA7134_TS_SERIAL0, 0x40);
265                 saa_writeb(SAA7134_TS_PARALLEL, 0xec);
266                 break;
267         case SAA7134_MPEG_TS_SERIAL:
268                 saa_writeb(SAA7134_TS_SERIAL0, 0xd8);
269                 saa_writeb(SAA7134_TS_PARALLEL, 0x6c);
270                 saa_writeb(SAA7134_TS_PARALLEL_SERIAL, 0xbc);
271                 saa_writeb(SAA7134_TS_SERIAL1, 0x02);
272                 break;
273         }
274
275         dev->ts_started = 1;
276
277         return 0;
278 }
279
280 int saa7134_ts_fini(struct saa7134_dev *dev)
281 {
282         saa7134_pgtable_free(dev->pci,&dev->ts.pt_ts);
283         return 0;
284 }
285
286 void saa7134_irq_ts_done(struct saa7134_dev *dev, unsigned long status)
287 {
288         enum v4l2_field field;
289
290         spin_lock(&dev->slock);
291         if (dev->ts_q.curr) {
292                 field = dev->ts_q.curr->vb.field;
293                 if (field == V4L2_FIELD_TOP) {
294                         if ((status & 0x100000) != 0x000000)
295                                 goto done;
296                 } else {
297                         if ((status & 0x100000) != 0x100000)
298                                 goto done;
299                 }
300                 saa7134_buffer_finish(dev,&dev->ts_q,VIDEOBUF_DONE);
301         }
302         saa7134_buffer_next(dev,&dev->ts_q);
303
304  done:
305         spin_unlock(&dev->slock);
306 }
307
308 /* ----------------------------------------------------------- */
309 /*
310  * Local variables:
311  * c-basic-offset: 8
312  * End:
313  */