V4L/DVB (7010): cx23885: Small cleanup
[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 = 0;
35 module_param(vbi_debug, int, 0644);
36 MODULE_PARM_DESC(vbi_debug, "enable debug messages [vbi]");
37
38 #define dprintk(level, fmt, arg...)     if (vbi_debug >= level) \
39         printk(KERN_DEBUG "%s: " fmt, dev->name , ## arg)
40
41 /* ------------------------------------------------------------------ */
42
43 int cx23885_vbi_fmt(struct file *file, void *priv,
44         struct v4l2_format *f)
45 {
46         struct cx23885_fh *fh = priv;
47         struct cx23885_dev *dev = fh->dev;
48
49         if (dev->tvnorm & V4L2_STD_525_60) {
50                 /* ntsc */
51                 f->fmt.vbi.sampling_rate = 28636363;
52                 f->fmt.vbi.start[0] = 10;
53                 f->fmt.vbi.start[1] = 273;
54
55         } else if (dev->tvnorm & V4L2_STD_625_50) {
56                 /* pal */
57                 f->fmt.vbi.sampling_rate = 35468950;
58                 f->fmt.vbi.start[0] = 7 - 1;
59                 f->fmt.vbi.start[1] = 319 - 1;
60         }
61         return 0;
62 }
63
64 static int cx23885_start_vbi_dma(struct cx23885_dev    *dev,
65                          struct cx23885_dmaqueue *q,
66                          struct cx23885_buffer   *buf)
67 {
68         /* setup fifo + format */
69         cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH02],
70                                 buf->vb.width, buf->risc.dma);
71
72         /* reset counter */
73         q->count = 1;
74
75         /* enable irqs */
76         cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
77         cx_set(VID_A_INT_MSK, 0x000022);
78
79         /* start dma */
80         cx_set(DEV_CNTRL2, (1<<5));
81         cx_set(VID_A_DMA_CTL, 0x00000022);
82
83         return 0;
84 }
85
86 int cx23885_stop_vbi_dma(struct cx23885_dev *dev)
87 {
88         /* stop dma */
89         cx_clear(VID_A_DMA_CTL, 0x00000022);
90
91         /* disable irqs */
92         cx_clear(PCI_INT_MSK, 0x000001);
93         cx_clear(VID_A_INT_MSK, 0x00000022);
94         return 0;
95 }
96
97 int cx23885_restart_vbi_queue(struct cx23885_dev    *dev,
98                              struct cx23885_dmaqueue *q)
99 {
100         struct cx23885_buffer *buf;
101         struct list_head *item;
102
103         if (list_empty(&q->active))
104                 return 0;
105
106         buf = list_entry(q->active.next, struct cx23885_buffer, vb.queue);
107         dprintk(2, "restart_queue [%p/%d]: restart dma\n",
108                 buf, buf->vb.i);
109         cx23885_start_vbi_dma(dev, q, buf);
110         list_for_each(item, &q->active) {
111                 buf = list_entry(item, struct cx23885_buffer, vb.queue);
112                 buf->count = q->count++;
113         }
114         mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
115         return 0;
116 }
117
118 void cx23885_vbi_timeout(unsigned long data)
119 {
120         struct cx23885_dev *dev = (struct cx23885_dev *)data;
121         struct cx23885_dmaqueue *q = &dev->vbiq;
122         struct cx23885_buffer *buf;
123         unsigned long flags;
124
125         cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH02]);
126
127         cx_clear(VID_A_DMA_CTL, 0x22);
128
129         spin_lock_irqsave(&dev->slock, flags);
130         while (!list_empty(&q->active)) {
131                 buf = list_entry(q->active.next, struct cx23885_buffer,
132                         vb.queue);
133                 list_del(&buf->vb.queue);
134                 buf->vb.state = VIDEOBUF_ERROR;
135                 wake_up(&buf->vb.done);
136                 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", dev->name,
137                        buf, buf->vb.i, (unsigned long)buf->risc.dma);
138         }
139         cx23885_restart_vbi_queue(dev, q);
140         spin_unlock_irqrestore(&dev->slock, flags);
141 }
142
143 /* ------------------------------------------------------------------ */
144 #define VBI_LINE_LENGTH 2048
145 #define VBI_LINE_COUNT 17
146
147 static int
148 vbi_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
160 static int
161 vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
162             enum v4l2_field field)
163 {
164         struct cx23885_fh *fh  = q->priv_data;
165         struct cx23885_dev *dev = fh->dev;
166         struct cx23885_buffer *buf = container_of(vb,
167                 struct cx23885_buffer, vb);
168         struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
169         unsigned int size;
170         int rc;
171
172         size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
173         if (0 != buf->vb.baddr  &&  buf->vb.bsize < size)
174                 return -EINVAL;
175
176         if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
177                 buf->vb.width  = VBI_LINE_LENGTH;
178                 buf->vb.height = VBI_LINE_COUNT;
179                 buf->vb.size   = size;
180                 buf->vb.field  = V4L2_FIELD_SEQ_TB;
181
182                 rc = videobuf_iolock(q, &buf->vb, NULL);
183                 if (0 != rc)
184                         goto fail;
185                 cx23885_risc_buffer(dev->pci, &buf->risc,
186                                  dma->sglist,
187                                  0, buf->vb.width * buf->vb.height,
188                                  buf->vb.width, 0,
189                                  buf->vb.height);
190         }
191         buf->vb.state = VIDEOBUF_PREPARED;
192         return 0;
193
194  fail:
195         cx23885_free_buffer(q, buf);
196         return rc;
197 }
198
199 static void
200 vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
201 {
202         struct cx23885_buffer   *buf =
203                 container_of(vb, struct cx23885_buffer, vb);
204         struct cx23885_buffer   *prev;
205         struct cx23885_fh       *fh   = vq->priv_data;
206         struct cx23885_dev      *dev  = fh->dev;
207         struct cx23885_dmaqueue *q    = &dev->vbiq;
208
209         /* add jump to stopper */
210         buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
211         buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
212         buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
213
214         if (list_empty(&q->active)) {
215                 list_add_tail(&buf->vb.queue, &q->active);
216                 cx23885_start_vbi_dma(dev, q, buf);
217                 buf->vb.state = VIDEOBUF_ACTIVE;
218                 buf->count    = q->count++;
219                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
220                 dprintk(2, "[%p/%d] vbi_queue - first active\n",
221                         buf, buf->vb.i);
222
223         } else {
224                 prev = list_entry(q->active.prev, struct cx23885_buffer,
225                         vb.queue);
226                 list_add_tail(&buf->vb.queue, &q->active);
227                 buf->vb.state = VIDEOBUF_ACTIVE;
228                 buf->count    = q->count++;
229                 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
230                 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63-32 */
231                 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
232                         buf, buf->vb.i);
233         }
234 }
235
236 static void vbi_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
237 {
238         struct cx23885_buffer *buf =
239                 container_of(vb, struct cx23885_buffer, vb);
240
241         cx23885_free_buffer(q, buf);
242 }
243
244 struct videobuf_queue_ops cx23885_vbi_qops = {
245         .buf_setup    = vbi_setup,
246         .buf_prepare  = vbi_prepare,
247         .buf_queue    = vbi_queue,
248         .buf_release  = vbi_release,
249 };
250
251 /* ------------------------------------------------------------------ */
252 /*
253  * Local variables:
254  * c-basic-offset: 8
255  * End:
256  */