Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/cpufreq
[linux-2.6] / drivers / media / video / cx88 / cx88-vbi.c
1 /*
2  */
3 #include <linux/kernel.h>
4 #include <linux/module.h>
5 #include <linux/moduleparam.h>
6 #include <linux/init.h>
7 #include <linux/slab.h>
8
9 #include "cx88.h"
10
11 static unsigned int vbibufs = 4;
12 module_param(vbibufs,int,0644);
13 MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32");
14
15 static unsigned int vbi_debug = 0;
16 module_param(vbi_debug,int,0644);
17 MODULE_PARM_DESC(vbi_debug,"enable debug messages [vbi]");
18
19 #define dprintk(level,fmt, arg...)      if (vbi_debug >= level) \
20         printk(KERN_DEBUG "%s: " fmt, dev->core->name , ## arg)
21
22 /* ------------------------------------------------------------------ */
23
24 int cx8800_vbi_fmt (struct file *file, void *priv,
25                                         struct v4l2_format *f)
26 {
27         struct cx8800_fh  *fh   = priv;
28         struct cx8800_dev *dev  = fh->dev;
29
30         f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
31         f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
32         f->fmt.vbi.offset = 244;
33         f->fmt.vbi.count[0] = VBI_LINE_COUNT;
34         f->fmt.vbi.count[1] = VBI_LINE_COUNT;
35
36         if (dev->core->tvnorm & V4L2_STD_525_60) {
37                 /* ntsc */
38                 f->fmt.vbi.sampling_rate = 28636363;
39                 f->fmt.vbi.start[0] = 10;
40                 f->fmt.vbi.start[1] = 273;
41
42         } else if (dev->core->tvnorm & V4L2_STD_625_50) {
43                 /* pal */
44                 f->fmt.vbi.sampling_rate = 35468950;
45                 f->fmt.vbi.start[0] = 7 -1;
46                 f->fmt.vbi.start[1] = 319 -1;
47         }
48         return 0;
49 }
50
51 static int cx8800_start_vbi_dma(struct cx8800_dev    *dev,
52                          struct cx88_dmaqueue *q,
53                          struct cx88_buffer   *buf)
54 {
55         struct cx88_core *core = dev->core;
56
57         /* setup fifo + format */
58         cx88_sram_channel_setup(dev->core, &cx88_sram_channels[SRAM_CH24],
59                                 buf->vb.width, buf->risc.dma);
60
61         cx_write(MO_VBOS_CONTROL, ( (1 << 18) |  // comb filter delay fixup
62                                     (1 << 15) |  // enable vbi capture
63                                     (1 << 11) ));
64
65         /* reset counter */
66         cx_write(MO_VBI_GPCNTRL, GP_COUNT_CONTROL_RESET);
67         q->count = 1;
68
69         /* enable irqs */
70         cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x01);
71         cx_set(MO_VID_INTMSK, 0x0f0088);
72
73         /* enable capture */
74         cx_set(VID_CAPTURE_CONTROL,0x18);
75
76         /* start dma */
77         cx_set(MO_DEV_CNTRL2, (1<<5));
78         cx_set(MO_VID_DMACNTRL, 0x88);
79
80         return 0;
81 }
82
83 int cx8800_stop_vbi_dma(struct cx8800_dev *dev)
84 {
85         struct cx88_core *core = dev->core;
86
87         /* stop dma */
88         cx_clear(MO_VID_DMACNTRL, 0x88);
89
90         /* disable capture */
91         cx_clear(VID_CAPTURE_CONTROL,0x18);
92
93         /* disable irqs */
94         cx_clear(MO_PCI_INTMSK, 0x000001);
95         cx_clear(MO_VID_INTMSK, 0x0f0088);
96         return 0;
97 }
98
99 int cx8800_restart_vbi_queue(struct cx8800_dev    *dev,
100                              struct cx88_dmaqueue *q)
101 {
102         struct cx88_buffer *buf;
103         struct list_head *item;
104
105         if (list_empty(&q->active))
106                 return 0;
107
108         buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
109         dprintk(2,"restart_queue [%p/%d]: restart dma\n",
110                 buf, buf->vb.i);
111         cx8800_start_vbi_dma(dev, q, buf);
112         list_for_each(item,&q->active) {
113                 buf = list_entry(item, struct cx88_buffer, vb.queue);
114                 buf->count = q->count++;
115         }
116         mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
117         return 0;
118 }
119
120 void cx8800_vbi_timeout(unsigned long data)
121 {
122         struct cx8800_dev *dev = (struct cx8800_dev*)data;
123         struct cx88_core *core = dev->core;
124         struct cx88_dmaqueue *q = &dev->vbiq;
125         struct cx88_buffer *buf;
126         unsigned long flags;
127
128         cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH24]);
129
130         cx_clear(MO_VID_DMACNTRL, 0x88);
131         cx_clear(VID_CAPTURE_CONTROL, 0x18);
132
133         spin_lock_irqsave(&dev->slock,flags);
134         while (!list_empty(&q->active)) {
135                 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
136                 list_del(&buf->vb.queue);
137                 buf->vb.state = STATE_ERROR;
138                 wake_up(&buf->vb.done);
139                 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", dev->core->name,
140                        buf, buf->vb.i, (unsigned long)buf->risc.dma);
141         }
142         cx8800_restart_vbi_queue(dev,q);
143         spin_unlock_irqrestore(&dev->slock,flags);
144 }
145
146 /* ------------------------------------------------------------------ */
147
148 static int
149 vbi_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
150 {
151         *size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
152         if (0 == *count)
153                 *count = vbibufs;
154         if (*count < 2)
155                 *count = 2;
156         if (*count > 32)
157                 *count = 32;
158         return 0;
159 }
160
161 static int
162 vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
163             enum v4l2_field field)
164 {
165         struct cx8800_fh   *fh  = q->priv_data;
166         struct cx8800_dev  *dev = fh->dev;
167         struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
168         unsigned int size;
169         int rc;
170
171         size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
172         if (0 != buf->vb.baddr  &&  buf->vb.bsize < size)
173                 return -EINVAL;
174
175         if (STATE_NEEDS_INIT == buf->vb.state) {
176                 buf->vb.width  = VBI_LINE_LENGTH;
177                 buf->vb.height = VBI_LINE_COUNT;
178                 buf->vb.size   = size;
179                 buf->vb.field  = V4L2_FIELD_SEQ_TB;
180
181                 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
182                         goto fail;
183                 cx88_risc_buffer(dev->pci, &buf->risc,
184                                  buf->vb.dma.sglist,
185                                  0, buf->vb.width * buf->vb.height,
186                                  buf->vb.width, 0,
187                                  buf->vb.height);
188         }
189         buf->vb.state = STATE_PREPARED;
190         return 0;
191
192  fail:
193         cx88_free_buffer(q,buf);
194         return rc;
195 }
196
197 static void
198 vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
199 {
200         struct cx88_buffer    *buf = container_of(vb,struct cx88_buffer,vb);
201         struct cx88_buffer    *prev;
202         struct cx8800_fh      *fh   = vq->priv_data;
203         struct cx8800_dev     *dev  = fh->dev;
204         struct cx88_dmaqueue  *q    = &dev->vbiq;
205
206         /* add jump to stopper */
207         buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
208         buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
209
210         if (list_empty(&q->active)) {
211                 list_add_tail(&buf->vb.queue,&q->active);
212                 cx8800_start_vbi_dma(dev, q, buf);
213                 buf->vb.state = STATE_ACTIVE;
214                 buf->count    = q->count++;
215                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
216                 dprintk(2,"[%p/%d] vbi_queue - first active\n",
217                         buf, buf->vb.i);
218
219         } else {
220                 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
221                 list_add_tail(&buf->vb.queue,&q->active);
222                 buf->vb.state = STATE_ACTIVE;
223                 buf->count    = q->count++;
224                 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
225                 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
226                         buf, buf->vb.i);
227         }
228 }
229
230 static void vbi_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
231 {
232         struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
233
234         cx88_free_buffer(q,buf);
235 }
236
237 struct videobuf_queue_ops cx8800_vbi_qops = {
238         .buf_setup    = vbi_setup,
239         .buf_prepare  = vbi_prepare,
240         .buf_queue    = vbi_queue,
241         .buf_release  = vbi_release,
242 };
243
244 /* ------------------------------------------------------------------ */
245 /*
246  * Local variables:
247  * c-basic-offset: 8
248  * End:
249  */