V4L/DVB (6006): tuner: move last_div to tuner-simple private data
[wrt350n-kernel.git] / drivers / media / video / saa7134 / saa7134-ts.c
blob60a90a2617aea615849e0550efa7757e5296f0c5
1 /*
3 * device driver for philips saa7134 based TV cards
4 * video4linux video interface
6 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
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.
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.
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.
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>
31 #include "saa7134-reg.h"
32 #include "saa7134.h"
34 /* ------------------------------------------------------------------ */
36 static unsigned int ts_debug = 0;
37 module_param(ts_debug, int, 0644);
38 MODULE_PARM_DESC(ts_debug,"enable debug messages [ts]");
40 #define dprintk(fmt, arg...) if (ts_debug) \
41 printk(KERN_DEBUG "%s/ts: " fmt, dev->name , ## arg)
43 /* ------------------------------------------------------------------ */
45 static int buffer_activate(struct saa7134_dev *dev,
46 struct saa7134_buf *buf,
47 struct saa7134_buf *next)
50 dprintk("buffer_activate [%p]",buf);
51 buf->vb.state = STATE_ACTIVE;
52 buf->top_seen = 0;
54 if (NULL == next)
55 next = buf;
56 if (V4L2_FIELD_TOP == buf->vb.field) {
57 dprintk("- [top] buf=%p next=%p\n",buf,next);
58 saa_writel(SAA7134_RS_BA1(5),saa7134_buffer_base(buf));
59 saa_writel(SAA7134_RS_BA2(5),saa7134_buffer_base(next));
60 } else {
61 dprintk("- [bottom] buf=%p next=%p\n",buf,next);
62 saa_writel(SAA7134_RS_BA1(5),saa7134_buffer_base(next));
63 saa_writel(SAA7134_RS_BA2(5),saa7134_buffer_base(buf));
66 /* start DMA */
67 saa7134_set_dmabits(dev);
69 mod_timer(&dev->ts_q.timeout, jiffies+BUFFER_TIMEOUT);
70 return 0;
73 static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
74 enum v4l2_field field)
76 struct saa7134_dev *dev = q->priv_data;
77 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
78 unsigned int lines, llength, size;
79 u32 control;
80 int err;
82 dprintk("buffer_prepare [%p,%s]\n",buf,v4l2_field_names[field]);
84 llength = TS_PACKET_SIZE;
85 lines = dev->ts.nr_packets;
87 size = lines * llength;
88 if (0 != buf->vb.baddr && buf->vb.bsize < size)
89 return -EINVAL;
91 if (buf->vb.size != size) {
92 saa7134_dma_free(q,buf);
95 if (STATE_NEEDS_INIT == buf->vb.state) {
96 buf->vb.width = llength;
97 buf->vb.height = lines;
98 buf->vb.size = size;
99 buf->pt = &dev->ts.pt_ts;
101 err = videobuf_iolock(q,&buf->vb,NULL);
102 if (err)
103 goto oops;
104 err = saa7134_pgtable_build(dev->pci,buf->pt,
105 buf->vb.dma.sglist,
106 buf->vb.dma.sglen,
107 saa7134_buffer_startpage(buf));
108 if (err)
109 goto oops;
112 /* dma: setup channel 5 (= TS) */
113 control = SAA7134_RS_CONTROL_BURST_16 |
114 SAA7134_RS_CONTROL_ME |
115 (buf->pt->dma >> 12);
117 saa_writeb(SAA7134_TS_DMA0, ((lines-1)&0xff));
118 saa_writeb(SAA7134_TS_DMA1, (((lines-1)>>8)&0xff));
119 saa_writeb(SAA7134_TS_DMA2, ((((lines-1)>>16)&0x3f) | 0x00)); /* TSNOPIT=0, TSCOLAP=0 */
120 saa_writel(SAA7134_RS_PITCH(5),TS_PACKET_SIZE);
121 saa_writel(SAA7134_RS_CONTROL(5),control);
123 buf->vb.state = STATE_PREPARED;
124 buf->activate = buffer_activate;
125 buf->vb.field = field;
126 return 0;
128 oops:
129 saa7134_dma_free(q,buf);
130 return err;
133 static int
134 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
136 struct saa7134_dev *dev = q->priv_data;
138 *size = TS_PACKET_SIZE * dev->ts.nr_packets;
139 if (0 == *count)
140 *count = dev->ts.nr_bufs;
141 *count = saa7134_buffer_count(*size,*count);
142 return 0;
145 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
147 struct saa7134_dev *dev = q->priv_data;
148 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
150 saa7134_buffer_queue(dev,&dev->ts_q,buf);
153 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
155 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
157 saa7134_dma_free(q,buf);
160 struct videobuf_queue_ops saa7134_ts_qops = {
161 .buf_setup = buffer_setup,
162 .buf_prepare = buffer_prepare,
163 .buf_queue = buffer_queue,
164 .buf_release = buffer_release,
166 EXPORT_SYMBOL_GPL(saa7134_ts_qops);
168 /* ----------------------------------------------------------- */
169 /* exported stuff */
171 static unsigned int tsbufs = 8;
172 module_param(tsbufs, int, 0444);
173 MODULE_PARM_DESC(tsbufs,"number of ts buffers, range 2-32");
175 static unsigned int ts_nr_packets = 64;
176 module_param(ts_nr_packets, int, 0444);
177 MODULE_PARM_DESC(ts_nr_packets,"size of a ts buffers (in ts packets)");
179 int saa7134_ts_init1(struct saa7134_dev *dev)
181 /* sanitycheck insmod options */
182 if (tsbufs < 2)
183 tsbufs = 2;
184 if (tsbufs > VIDEO_MAX_FRAME)
185 tsbufs = VIDEO_MAX_FRAME;
186 if (ts_nr_packets < 4)
187 ts_nr_packets = 4;
188 if (ts_nr_packets > 312)
189 ts_nr_packets = 312;
190 dev->ts.nr_bufs = tsbufs;
191 dev->ts.nr_packets = ts_nr_packets;
193 INIT_LIST_HEAD(&dev->ts_q.queue);
194 init_timer(&dev->ts_q.timeout);
195 dev->ts_q.timeout.function = saa7134_buffer_timeout;
196 dev->ts_q.timeout.data = (unsigned long)(&dev->ts_q);
197 dev->ts_q.dev = dev;
198 dev->ts_q.need_two = 1;
199 saa7134_pgtable_alloc(dev->pci,&dev->ts.pt_ts);
201 /* init TS hw */
202 saa_writeb(SAA7134_TS_SERIAL1, 0x00); /* deactivate TS softreset */
203 saa_writeb(SAA7134_TS_PARALLEL, 0xec); /* TSSOP high active, TSVAL high active, TSLOCK ignored */
204 saa_writeb(SAA7134_TS_PARALLEL_SERIAL, (TS_PACKET_SIZE-1));
205 saa_writeb(SAA7134_TS_DMA0, ((dev->ts.nr_packets-1)&0xff));
206 saa_writeb(SAA7134_TS_DMA1, (((dev->ts.nr_packets-1)>>8)&0xff));
207 saa_writeb(SAA7134_TS_DMA2, ((((dev->ts.nr_packets-1)>>16)&0x3f) | 0x00)); /* TSNOPIT=0, TSCOLAP=0 */
209 return 0;
212 int saa7134_ts_fini(struct saa7134_dev *dev)
214 saa7134_pgtable_free(dev->pci,&dev->ts.pt_ts);
215 return 0;
219 void saa7134_irq_ts_done(struct saa7134_dev *dev, unsigned long status)
221 enum v4l2_field field;
223 spin_lock(&dev->slock);
224 if (dev->ts_q.curr) {
225 field = dev->ts_q.curr->vb.field;
226 if (field == V4L2_FIELD_TOP) {
227 if ((status & 0x100000) != 0x000000)
228 goto done;
229 } else {
230 if ((status & 0x100000) != 0x100000)
231 goto done;
233 saa7134_buffer_finish(dev,&dev->ts_q,STATE_DONE);
235 saa7134_buffer_next(dev,&dev->ts_q);
237 done:
238 spin_unlock(&dev->slock);
241 /* ----------------------------------------------------------- */
243 * Local variables:
244 * c-basic-offset: 8
245 * End: