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