blob: aa40505c4b3a2300a62fee0cd62d90a4d1cf0b85 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 */
3#include <linux/kernel.h>
4#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/init.h>
6#include <linux/slab.h>
7
8#include "cx88.h"
9
10static unsigned int vbibufs = 4;
11module_param(vbibufs,int,0644);
12MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32");
13
14static unsigned int vbi_debug = 0;
15module_param(vbi_debug,int,0644);
16MODULE_PARM_DESC(vbi_debug,"enable debug messages [vbi]");
17
18#define dprintk(level,fmt, arg...) if (vbi_debug >= level) \
19 printk(KERN_DEBUG "%s: " fmt, dev->core->name , ## arg)
20
21/* ------------------------------------------------------------------ */
22
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -030023int cx8800_vbi_fmt (struct file *file, void *priv,
24 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -030026 struct cx8800_fh *fh = priv;
27 struct cx8800_dev *dev = fh->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
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
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -030035 if (dev->core->tvnorm & V4L2_STD_525_60) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 /* ntsc */
37 f->fmt.vbi.sampling_rate = 28636363;
Michael Schimek419ac5d2006-05-22 10:32:11 -030038 f->fmt.vbi.start[0] = 10;
39 f->fmt.vbi.start[1] = 273;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -030041 } else if (dev->core->tvnorm & V4L2_STD_625_50) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 /* 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 }
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -030047 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
Adrian Bunk408b6642005-05-01 08:59:29 -070050static int cx8800_start_vbi_dma(struct cx8800_dev *dev,
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -070051 struct cx88_dmaqueue *q,
52 struct cx88_buffer *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 struct cx88_core *core = dev->core;
55
56 /* setup fifo + format */
57 cx88_sram_channel_setup(dev->core, &cx88_sram_channels[SRAM_CH24],
58 buf->vb.width, buf->risc.dma);
59
60 cx_write(MO_VBOS_CONTROL, ( (1 << 18) | // comb filter delay fixup
61 (1 << 15) | // enable vbi capture
62 (1 << 11) ));
63
64 /* reset counter */
65 cx_write(MO_VBI_GPCNTRL, GP_COUNT_CONTROL_RESET);
66 q->count = 1;
67
68 /* enable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -030069 cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 cx_set(MO_VID_INTMSK, 0x0f0088);
71
72 /* enable capture */
73 cx_set(VID_CAPTURE_CONTROL,0x18);
74
75 /* start dma */
76 cx_set(MO_DEV_CNTRL2, (1<<5));
77 cx_set(MO_VID_DMACNTRL, 0x88);
78
79 return 0;
80}
81
82int cx8800_stop_vbi_dma(struct cx8800_dev *dev)
83{
84 struct cx88_core *core = dev->core;
85
86 /* stop dma */
87 cx_clear(MO_VID_DMACNTRL, 0x88);
88
89 /* disable capture */
90 cx_clear(VID_CAPTURE_CONTROL,0x18);
91
92 /* disable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -030093 cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 cx_clear(MO_VID_INTMSK, 0x0f0088);
95 return 0;
96}
97
98int cx8800_restart_vbi_queue(struct cx8800_dev *dev,
99 struct cx88_dmaqueue *q)
100{
101 struct cx88_buffer *buf;
102 struct list_head *item;
103
104 if (list_empty(&q->active))
105 return 0;
106
107 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
108 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
109 buf, buf->vb.i);
110 cx8800_start_vbi_dma(dev, q, buf);
111 list_for_each(item,&q->active) {
112 buf = list_entry(item, struct cx88_buffer, vb.queue);
113 buf->count = q->count++;
114 }
115 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
116 return 0;
117}
118
119void cx8800_vbi_timeout(unsigned long data)
120{
121 struct cx8800_dev *dev = (struct cx8800_dev*)data;
122 struct cx88_core *core = dev->core;
123 struct cx88_dmaqueue *q = &dev->vbiq;
124 struct cx88_buffer *buf;
125 unsigned long flags;
126
127 cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH24]);
128
129 cx_clear(MO_VID_DMACNTRL, 0x88);
130 cx_clear(VID_CAPTURE_CONTROL, 0x18);
131
132 spin_lock_irqsave(&dev->slock,flags);
133 while (!list_empty(&q->active)) {
134 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
135 list_del(&buf->vb.queue);
136 buf->vb.state = STATE_ERROR;
137 wake_up(&buf->vb.done);
138 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", dev->core->name,
139 buf, buf->vb.i, (unsigned long)buf->risc.dma);
140 }
141 cx8800_restart_vbi_queue(dev,q);
142 spin_unlock_irqrestore(&dev->slock,flags);
143}
144
145/* ------------------------------------------------------------------ */
146
147static int
148vbi_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
149{
150 *size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
151 if (0 == *count)
152 *count = vbibufs;
153 if (*count < 2)
154 *count = 2;
155 if (*count > 32)
156 *count = 32;
157 return 0;
158}
159
160static int
161vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
162 enum v4l2_field field)
163{
164 struct cx8800_fh *fh = q->priv_data;
165 struct cx8800_dev *dev = fh->dev;
166 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
167 unsigned int size;
168 int rc;
169
170 size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
171 if (0 != buf->vb.baddr && buf->vb.bsize < size)
172 return -EINVAL;
173
174 if (STATE_NEEDS_INIT == buf->vb.state) {
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300175 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 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
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300181 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 goto fail;
183 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300184 dma->sglist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 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:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300193 cx88_free_buffer(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return rc;
195}
196
197static void
198vbi_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
230static void vbi_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
231{
232 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300234 cx88_free_buffer(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237struct 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 */