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