Linux 2.6.34-rc3
[pohmelfs.git] / drivers / media / video / cx23885 / cx23885-vbi.c
blob5b297f0323b6485a5099a255d9bf8511414db848
1 /*
2 * Driver for the Conexant CX23885 PCIe bridge
4 * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
28 #include "cx23885.h"
30 static unsigned int vbibufs = 4;
31 module_param(vbibufs, int, 0644);
32 MODULE_PARM_DESC(vbibufs, "number of vbi buffers, range 2-32");
34 static unsigned int vbi_debug;
35 module_param(vbi_debug, int, 0644);
36 MODULE_PARM_DESC(vbi_debug, "enable debug messages [vbi]");
38 #define dprintk(level, fmt, arg...)\
39 do { if (vbi_debug >= level)\
40 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
41 } while (0)
43 /* ------------------------------------------------------------------ */
45 int cx23885_vbi_fmt(struct file *file, void *priv,
46 struct v4l2_format *f)
48 struct cx23885_fh *fh = priv;
49 struct cx23885_dev *dev = fh->dev;
51 if (dev->tvnorm & V4L2_STD_525_60) {
52 /* ntsc */
53 f->fmt.vbi.sampling_rate = 28636363;
54 f->fmt.vbi.start[0] = 10;
55 f->fmt.vbi.start[1] = 273;
57 } else if (dev->tvnorm & V4L2_STD_625_50) {
58 /* pal */
59 f->fmt.vbi.sampling_rate = 35468950;
60 f->fmt.vbi.start[0] = 7 - 1;
61 f->fmt.vbi.start[1] = 319 - 1;
63 return 0;
66 static int cx23885_start_vbi_dma(struct cx23885_dev *dev,
67 struct cx23885_dmaqueue *q,
68 struct cx23885_buffer *buf)
70 /* setup fifo + format */
71 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH02],
72 buf->vb.width, buf->risc.dma);
74 /* reset counter */
75 q->count = 1;
77 /* enable irqs */
78 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
79 cx_set(VID_A_INT_MSK, 0x000022);
81 /* start dma */
82 cx_set(DEV_CNTRL2, (1<<5));
83 cx_set(VID_A_DMA_CTL, 0x00000022);
85 return 0;
89 static int cx23885_restart_vbi_queue(struct cx23885_dev *dev,
90 struct cx23885_dmaqueue *q)
92 struct cx23885_buffer *buf;
93 struct list_head *item;
95 if (list_empty(&q->active))
96 return 0;
98 buf = list_entry(q->active.next, struct cx23885_buffer, vb.queue);
99 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
100 buf, buf->vb.i);
101 cx23885_start_vbi_dma(dev, q, buf);
102 list_for_each(item, &q->active) {
103 buf = list_entry(item, struct cx23885_buffer, vb.queue);
104 buf->count = q->count++;
106 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
107 return 0;
110 void cx23885_vbi_timeout(unsigned long data)
112 struct cx23885_dev *dev = (struct cx23885_dev *)data;
113 struct cx23885_dmaqueue *q = &dev->vbiq;
114 struct cx23885_buffer *buf;
115 unsigned long flags;
117 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH02]);
119 cx_clear(VID_A_DMA_CTL, 0x22);
121 spin_lock_irqsave(&dev->slock, flags);
122 while (!list_empty(&q->active)) {
123 buf = list_entry(q->active.next, struct cx23885_buffer,
124 vb.queue);
125 list_del(&buf->vb.queue);
126 buf->vb.state = VIDEOBUF_ERROR;
127 wake_up(&buf->vb.done);
128 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", dev->name,
129 buf, buf->vb.i, (unsigned long)buf->risc.dma);
131 cx23885_restart_vbi_queue(dev, q);
132 spin_unlock_irqrestore(&dev->slock, flags);
135 /* ------------------------------------------------------------------ */
136 #define VBI_LINE_LENGTH 2048
137 #define VBI_LINE_COUNT 17
139 static int
140 vbi_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
142 *size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
143 if (0 == *count)
144 *count = vbibufs;
145 if (*count < 2)
146 *count = 2;
147 if (*count > 32)
148 *count = 32;
149 return 0;
152 static int
153 vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
154 enum v4l2_field field)
156 struct cx23885_fh *fh = q->priv_data;
157 struct cx23885_dev *dev = fh->dev;
158 struct cx23885_buffer *buf = container_of(vb,
159 struct cx23885_buffer, vb);
160 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
161 unsigned int size;
162 int rc;
164 size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
165 if (0 != buf->vb.baddr && buf->vb.bsize < size)
166 return -EINVAL;
168 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
169 buf->vb.width = VBI_LINE_LENGTH;
170 buf->vb.height = VBI_LINE_COUNT;
171 buf->vb.size = size;
172 buf->vb.field = V4L2_FIELD_SEQ_TB;
174 rc = videobuf_iolock(q, &buf->vb, NULL);
175 if (0 != rc)
176 goto fail;
177 cx23885_risc_buffer(dev->pci, &buf->risc,
178 dma->sglist,
179 0, buf->vb.width * buf->vb.height,
180 buf->vb.width, 0,
181 buf->vb.height);
183 buf->vb.state = VIDEOBUF_PREPARED;
184 return 0;
186 fail:
187 cx23885_free_buffer(q, buf);
188 return rc;
191 static void
192 vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
194 struct cx23885_buffer *buf =
195 container_of(vb, struct cx23885_buffer, vb);
196 struct cx23885_buffer *prev;
197 struct cx23885_fh *fh = vq->priv_data;
198 struct cx23885_dev *dev = fh->dev;
199 struct cx23885_dmaqueue *q = &dev->vbiq;
201 /* add jump to stopper */
202 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
203 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
204 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
206 if (list_empty(&q->active)) {
207 list_add_tail(&buf->vb.queue, &q->active);
208 cx23885_start_vbi_dma(dev, q, buf);
209 buf->vb.state = VIDEOBUF_ACTIVE;
210 buf->count = q->count++;
211 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
212 dprintk(2, "[%p/%d] vbi_queue - first active\n",
213 buf, buf->vb.i);
215 } else {
216 prev = list_entry(q->active.prev, struct cx23885_buffer,
217 vb.queue);
218 list_add_tail(&buf->vb.queue, &q->active);
219 buf->vb.state = VIDEOBUF_ACTIVE;
220 buf->count = q->count++;
221 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
222 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63-32 */
223 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
224 buf, buf->vb.i);
228 static void vbi_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
230 struct cx23885_buffer *buf =
231 container_of(vb, struct cx23885_buffer, vb);
233 cx23885_free_buffer(q, buf);
236 struct videobuf_queue_ops cx23885_vbi_qops = {
237 .buf_setup = vbi_setup,
238 .buf_prepare = vbi_prepare,
239 .buf_queue = vbi_queue,
240 .buf_release = vbi_release,
243 /* ------------------------------------------------------------------ */
245 * Local variables:
246 * c-basic-offset: 8
247 * End: