Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/penberg...
[linux-2.6] / drivers / media / video / cx23885 / cx23885-vbi.c
1 /*
2  *  Driver for the Conexant CX23885 PCIe bridge
3  *
4  *  Copyright (c) 2007 Steven Toth <stoth@hauppauge.com>
5  *
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.
10  *
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
14  *
15  *  GNU General Public License for more details.
16  *
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.
20  */
21
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>
27
28 #include "cx23885.h"
29
30 static unsigned int vbibufs = 4;
31 module_param(vbibufs, int, 0644);
32 MODULE_PARM_DESC(vbibufs, "number of vbi buffers, range 2-32");
33
34 static unsigned int vbi_debug;
35 module_param(vbi_debug, int, 0644);
36 MODULE_PARM_DESC(vbi_debug, "enable debug messages [vbi]");
37
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)
42
43 /* ------------------------------------------------------------------ */
44
45 int cx23885_vbi_fmt(struct file *file, void *priv,
46         struct v4l2_format *f)
47 {
48         struct cx23885_fh *fh = priv;
49         struct cx23885_dev *dev = fh->dev;
50
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;
56
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;
62         }
63         return 0;
64 }
65
66 static int cx23885_start_vbi_dma(struct cx23885_dev    *dev,
67                          struct cx23885_dmaqueue *q,
68                          struct cx23885_buffer   *buf)
69 {
70         /* setup fifo + format */
71         cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH02],
72                                 buf->vb.width, buf->risc.dma);
73
74         /* reset counter */
75         q->count = 1;
76
77         /* enable irqs */
78         cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
79         cx_set(VID_A_INT_MSK, 0x000022);
80
81         /* start dma */
82         cx_set(DEV_CNTRL2, (1<<5));
83         cx_set(VID_A_DMA_CTL, 0x00000022);
84
85         return 0;
86 }
87
88 int cx23885_stop_vbi_dma(struct cx23885_dev *dev)
89 {
90         /* stop dma */
91         cx_clear(VID_A_DMA_CTL, 0x00000022);
92
93         /* disable irqs */
94         cx_clear(PCI_INT_MSK, 0x000001);
95         cx_clear(VID_A_INT_MSK, 0x00000022);
96         return 0;
97 }
98
99 int cx23885_restart_vbi_queue(struct cx23885_dev    *dev,
100                              struct cx23885_dmaqueue *q)
101 {
102         struct cx23885_buffer *buf;
103         struct list_head *item;
104
105         if (list_empty(&q->active))
106                 return 0;
107
108         buf = list_entry(q->active.next, struct cx23885_buffer, vb.queue);
109         dprintk(2, "restart_queue [%p/%d]: restart dma\n",
110                 buf, buf->vb.i);
111         cx23885_start_vbi_dma(dev, q, buf);
112         list_for_each(item, &q->active) {
113                 buf = list_entry(item, struct cx23885_buffer, vb.queue);
114                 buf->count = q->count++;
115         }
116         mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
117         return 0;
118 }
119
120 void cx23885_vbi_timeout(unsigned long data)
121 {
122         struct cx23885_dev *dev = (struct cx23885_dev *)data;
123         struct cx23885_dmaqueue *q = &dev->vbiq;
124         struct cx23885_buffer *buf;
125         unsigned long flags;
126
127         cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH02]);
128
129         cx_clear(VID_A_DMA_CTL, 0x22);
130
131         spin_lock_irqsave(&dev->slock, flags);
132         while (!list_empty(&q->active)) {
133                 buf = list_entry(q->active.next, struct cx23885_buffer,
134                         vb.queue);
135                 list_del(&buf->vb.queue);
136                 buf->vb.state = VIDEOBUF_ERROR;
137                 wake_up(&buf->vb.done);
138                 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", dev->name,
139                        buf, buf->vb.i, (unsigned long)buf->risc.dma);
140         }
141         cx23885_restart_vbi_queue(dev, q);
142         spin_unlock_irqrestore(&dev->slock, flags);
143 }
144
145 /* ------------------------------------------------------------------ */
146 #define VBI_LINE_LENGTH 2048
147 #define VBI_LINE_COUNT 17
148
149 static int
150 vbi_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
151 {
152         *size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
153         if (0 == *count)
154                 *count = vbibufs;
155         if (*count < 2)
156                 *count = 2;
157         if (*count > 32)
158                 *count = 32;
159         return 0;
160 }
161
162 static int
163 vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
164             enum v4l2_field field)
165 {
166         struct cx23885_fh *fh  = q->priv_data;
167         struct cx23885_dev *dev = fh->dev;
168         struct cx23885_buffer *buf = container_of(vb,
169                 struct cx23885_buffer, vb);
170         struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
171         unsigned int size;
172         int rc;
173
174         size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
175         if (0 != buf->vb.baddr  &&  buf->vb.bsize < size)
176                 return -EINVAL;
177
178         if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
179                 buf->vb.width  = VBI_LINE_LENGTH;
180                 buf->vb.height = VBI_LINE_COUNT;
181                 buf->vb.size   = size;
182                 buf->vb.field  = V4L2_FIELD_SEQ_TB;
183
184                 rc = videobuf_iolock(q, &buf->vb, NULL);
185                 if (0 != rc)
186                         goto fail;
187                 cx23885_risc_buffer(dev->pci, &buf->risc,
188                                  dma->sglist,
189                                  0, buf->vb.width * buf->vb.height,
190                                  buf->vb.width, 0,
191                                  buf->vb.height);
192         }
193         buf->vb.state = VIDEOBUF_PREPARED;
194         return 0;
195
196  fail:
197         cx23885_free_buffer(q, buf);
198         return rc;
199 }
200
201 static void
202 vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
203 {
204         struct cx23885_buffer   *buf =
205                 container_of(vb, struct cx23885_buffer, vb);
206         struct cx23885_buffer   *prev;
207         struct cx23885_fh       *fh   = vq->priv_data;
208         struct cx23885_dev      *dev  = fh->dev;
209         struct cx23885_dmaqueue *q    = &dev->vbiq;
210
211         /* add jump to stopper */
212         buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
213         buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
214         buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
215
216         if (list_empty(&q->active)) {
217                 list_add_tail(&buf->vb.queue, &q->active);
218                 cx23885_start_vbi_dma(dev, q, buf);
219                 buf->vb.state = VIDEOBUF_ACTIVE;
220                 buf->count    = q->count++;
221                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
222                 dprintk(2, "[%p/%d] vbi_queue - first active\n",
223                         buf, buf->vb.i);
224
225         } else {
226                 prev = list_entry(q->active.prev, struct cx23885_buffer,
227                         vb.queue);
228                 list_add_tail(&buf->vb.queue, &q->active);
229                 buf->vb.state = VIDEOBUF_ACTIVE;
230                 buf->count    = q->count++;
231                 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
232                 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63-32 */
233                 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
234                         buf, buf->vb.i);
235         }
236 }
237
238 static void vbi_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
239 {
240         struct cx23885_buffer *buf =
241                 container_of(vb, struct cx23885_buffer, vb);
242
243         cx23885_free_buffer(q, buf);
244 }
245
246 struct videobuf_queue_ops cx23885_vbi_qops = {
247         .buf_setup    = vbi_setup,
248         .buf_prepare  = vbi_prepare,
249         .buf_queue    = vbi_queue,
250         .buf_release  = vbi_release,
251 };
252
253 /* ------------------------------------------------------------------ */
254 /*
255  * Local variables:
256  * c-basic-offset: 8
257  * End:
258  */