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