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