blob: 6f155713ee0251ea7e693ef21a15929efb674089 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Support for the mpeg transport stream transfers
4 * PCI function #2 of the cx2388x.
5 *
6 * (c) 2004 Jelle Foks <jelle@foks.8m.com>
7 * (c) 2004 Chris Pascoe <c.pascoe@itee.uq.edu.au>
8 * (c) 2004 Gerd Knorr <kraxel@bytesex.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/init.h>
28#include <linux/device.h>
29#include <linux/interrupt.h>
30#include <asm/delay.h>
31
32#include "cx88.h"
33
34/* ------------------------------------------------------------------ */
35
36MODULE_DESCRIPTION("mpeg driver for cx2388x based TV cards");
37MODULE_AUTHOR("Jelle Foks <jelle@foks.8m.com>");
38MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
39MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
40MODULE_LICENSE("GPL");
41
42static unsigned int debug = 0;
43module_param(debug,int,0644);
44MODULE_PARM_DESC(debug,"enable debug messages [mpeg]");
45
46#define dprintk(level,fmt, arg...) if (debug >= level) \
Steven Toth6c5be742006-12-02 21:15:51 -020047 printk(KERN_DEBUG "%s/2-mpeg: " fmt, dev->core->name, ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Steven Toth6c5be742006-12-02 21:15:51 -020049#define mpeg_dbg(level,fmt, arg...) if (debug >= level) \
50 printk(KERN_DEBUG "%s/2-mpeg: " fmt, core->name, ## arg)
51
52static LIST_HEAD(cx8802_devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/* ------------------------------------------------------------------ */
54
55static int cx8802_start_dma(struct cx8802_dev *dev,
56 struct cx88_dmaqueue *q,
57 struct cx88_buffer *buf)
58{
59 struct cx88_core *core = dev->core;
60
Trent Piepho76d313b2006-04-10 09:27:08 -030061 dprintk(1, "cx8802_start_dma w: %d, h: %d, f: %d\n", dev->width, dev->height, buf->vb.field);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 /* setup fifo + format */
64 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH28],
65 dev->ts_packet_size, buf->risc.dma);
66
67 /* write TS length to chip */
68 cx_write(MO_TS_LNGTH, buf->vb.width);
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 /* FIXME: this needs a review.
71 * also: move to cx88-blackbird + cx88-dvb source files? */
72
Steven Toth6c5be742006-12-02 21:15:51 -020073 dprintk( 1, "core->active_type_id = 0x%08x\n", core->active_type_id);
74
75 if ( (core->active_type_id == CX88_MPEG_DVB) &&
76 (cx88_boards[core->board].mpeg & CX88_MPEG_DVB) ) {
77
78 dprintk( 1, "cx8802_start_dma doing .dvb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 /* negedge driven & software reset */
Michael Krufkyf1798492005-07-07 17:58:39 -070080 cx_write(TS_GEN_CNTRL, 0x0040 | dev->ts_gen_cntrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 udelay(100);
82 cx_write(MO_PINMUX_IO, 0x00);
Michael Krufky0d723c02005-07-07 17:58:42 -070083 cx_write(TS_HW_SOP_CNTRL,0x47<<16|188<<4|0x01);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -070084 switch (core->board) {
85 case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q:
86 case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T:
87 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
Rusty Scottda215d22006-04-07 02:21:31 -030088 case CX88_BOARD_PCHDTV_HD5500:
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -070089 cx_write(TS_SOP_STAT, 1<<13);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -070090 break;
Steven Toth0fa14aa2006-01-09 15:25:02 -020091 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
92 case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
93 cx_write(MO_PINMUX_IO, 0x88); /* Enable MPEG parallel IO and video signal pins */
94 udelay(100);
95 break;
Steven Toth6c5be742006-12-02 21:15:51 -020096 case CX88_BOARD_HAUPPAUGE_HVR1300:
97 break;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -070098 default:
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -070099 cx_write(TS_SOP_STAT, 0x00);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700100 break;
Michael Krufkyf1798492005-07-07 17:58:39 -0700101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 cx_write(TS_GEN_CNTRL, dev->ts_gen_cntrl);
103 udelay(100);
Steven Toth6c5be742006-12-02 21:15:51 -0200104 } else if ( (core->active_type_id == CX88_MPEG_BLACKBIRD) &&
105 (cx88_boards[core->board].mpeg & CX88_MPEG_BLACKBIRD) ) {
106 dprintk( 1, "cx8802_start_dma doing .blackbird\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 cx_write(MO_PINMUX_IO, 0x88); /* enable MPEG parallel IO */
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 cx_write(TS_GEN_CNTRL, 0x46); /* punctured clock TS & posedge driven & software reset */
110 udelay(100);
111
112 cx_write(TS_HW_SOP_CNTRL, 0x408); /* mpeg start byte */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 cx_write(TS_VALERR_CNTRL, 0x2000);
114
115 cx_write(TS_GEN_CNTRL, 0x06); /* punctured clock TS & posedge driven */
116 udelay(100);
Steven Toth6c5be742006-12-02 21:15:51 -0200117 } else {
118 printk( "%s() Failed. Unsupported value in .mpeg (0x%08x)\n", __FUNCTION__,
119 cx88_boards[core->board].mpeg );
120 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 /* reset counter */
124 cx_write(MO_TS_GPCNTRL, GP_COUNT_CONTROL_RESET);
125 q->count = 1;
126
127 /* enable irqs */
Trent Piepho76d313b2006-04-10 09:27:08 -0300128 dprintk( 1, "setting the interrupt mask\n" );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x04);
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700130 cx_set(MO_TS_INTMSK, 0x1f0011);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 /* start dma */
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700133 cx_set(MO_DEV_CNTRL2, (1<<5));
134 cx_set(MO_TS_DMACNTRL, 0x11);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return 0;
136}
137
138static int cx8802_stop_dma(struct cx8802_dev *dev)
139{
140 struct cx88_core *core = dev->core;
Trent Piepho76d313b2006-04-10 09:27:08 -0300141 dprintk( 1, "cx8802_stop_dma\n" );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 /* stop dma */
144 cx_clear(MO_TS_DMACNTRL, 0x11);
145
146 /* disable irqs */
147 cx_clear(MO_PCI_INTMSK, 0x000004);
148 cx_clear(MO_TS_INTMSK, 0x1f0011);
149
150 /* Reset the controller */
151 cx_write(TS_GEN_CNTRL, 0xcd);
152 return 0;
153}
154
155static int cx8802_restart_queue(struct cx8802_dev *dev,
156 struct cx88_dmaqueue *q)
157{
158 struct cx88_buffer *buf;
159 struct list_head *item;
160
Jelle Foksf14ac0b2006-03-09 17:44:07 -0300161 dprintk( 1, "cx8802_restart_queue\n" );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (list_empty(&q->active))
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700163 {
Jelle Foksf14ac0b2006-03-09 17:44:07 -0300164 struct cx88_buffer *prev;
165 prev = NULL;
166
167 dprintk(1, "cx8802_restart_queue: queue is empty\n" );
168
169 for (;;) {
170 if (list_empty(&q->queued))
171 return 0;
172 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
173 if (NULL == prev) {
174 list_del(&buf->vb.queue);
175 list_add_tail(&buf->vb.queue,&q->active);
176 cx8802_start_dma(dev, q, buf);
177 buf->vb.state = STATE_ACTIVE;
178 buf->count = q->count++;
179 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
180 dprintk(1,"[%p/%d] restart_queue - first active\n",
181 buf,buf->vb.i);
182
183 } else if (prev->vb.width == buf->vb.width &&
184 prev->vb.height == buf->vb.height &&
185 prev->fmt == buf->fmt) {
186 list_del(&buf->vb.queue);
187 list_add_tail(&buf->vb.queue,&q->active);
188 buf->vb.state = STATE_ACTIVE;
189 buf->count = q->count++;
190 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
191 dprintk(1,"[%p/%d] restart_queue - move to active\n",
192 buf,buf->vb.i);
193 } else {
194 return 0;
195 }
196 prev = buf;
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return 0;
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
202 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
203 buf, buf->vb.i);
204 cx8802_start_dma(dev, q, buf);
205 list_for_each(item,&q->active) {
206 buf = list_entry(item, struct cx88_buffer, vb.queue);
207 buf->count = q->count++;
208 }
209 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
210 return 0;
211}
212
213/* ------------------------------------------------------------------ */
214
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300215int cx8802_buf_prepare(struct videobuf_queue *q, struct cx8802_dev *dev,
216 struct cx88_buffer *buf, enum v4l2_field field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 int size = dev->ts_packet_size * dev->ts_packet_count;
219 int rc;
220
221 dprintk(1, "%s: %p\n", __FUNCTION__, buf);
222 if (0 != buf->vb.baddr && buf->vb.bsize < size)
223 return -EINVAL;
224
225 if (STATE_NEEDS_INIT == buf->vb.state) {
226 buf->vb.width = dev->ts_packet_size;
227 buf->vb.height = dev->ts_packet_count;
228 buf->vb.size = size;
Catalin Climov31629422005-11-08 21:36:17 -0800229 buf->vb.field = field /*V4L2_FIELD_TOP*/;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300231 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 goto fail;
233 cx88_risc_databuffer(dev->pci, &buf->risc,
234 buf->vb.dma.sglist,
235 buf->vb.width, buf->vb.height);
236 }
237 buf->vb.state = STATE_PREPARED;
238 return 0;
239
240 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300241 cx88_free_buffer(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return rc;
243}
244
245void cx8802_buf_queue(struct cx8802_dev *dev, struct cx88_buffer *buf)
246{
247 struct cx88_buffer *prev;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300248 struct cx88_dmaqueue *cx88q = &dev->mpegq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700250 dprintk( 1, "cx8802_buf_queue\n" );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /* add jump to stopper */
252 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300253 buf->risc.jmp[1] = cpu_to_le32(cx88q->stopper.dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300255 if (list_empty(&cx88q->active)) {
Trent Piepho76d313b2006-04-10 09:27:08 -0300256 dprintk( 1, "queue is empty - first active\n" );
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300257 list_add_tail(&buf->vb.queue,&cx88q->active);
258 cx8802_start_dma(dev, cx88q, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 buf->vb.state = STATE_ACTIVE;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300260 buf->count = cx88q->count++;
261 mod_timer(&cx88q->timeout, jiffies+BUFFER_TIMEOUT);
Trent Piepho76d313b2006-04-10 09:27:08 -0300262 dprintk(1,"[%p/%d] %s - first active\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 buf, buf->vb.i, __FUNCTION__);
264
265 } else {
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700266 dprintk( 1, "queue is not empty - append to active\n" );
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300267 prev = list_entry(cx88q->active.prev, struct cx88_buffer, vb.queue);
268 list_add_tail(&buf->vb.queue,&cx88q->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 buf->vb.state = STATE_ACTIVE;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300270 buf->count = cx88q->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700272 dprintk( 1, "[%p/%d] %s - append to active\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 buf, buf->vb.i, __FUNCTION__);
274 }
275}
276
277/* ----------------------------------------------------------- */
278
279static void do_cancel_buffers(struct cx8802_dev *dev, char *reason, int restart)
280{
281 struct cx88_dmaqueue *q = &dev->mpegq;
282 struct cx88_buffer *buf;
283 unsigned long flags;
284
285 spin_lock_irqsave(&dev->slock,flags);
286 while (!list_empty(&q->active)) {
287 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
288 list_del(&buf->vb.queue);
289 buf->vb.state = STATE_ERROR;
290 wake_up(&buf->vb.done);
291 dprintk(1,"[%p/%d] %s - dma=0x%08lx\n",
292 buf, buf->vb.i, reason, (unsigned long)buf->risc.dma);
293 }
294 if (restart)
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700295 {
Trent Piepho76d313b2006-04-10 09:27:08 -0300296 dprintk(1, "restarting queue\n" );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 cx8802_restart_queue(dev,q);
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 spin_unlock_irqrestore(&dev->slock,flags);
300}
301
302void cx8802_cancel_buffers(struct cx8802_dev *dev)
303{
304 struct cx88_dmaqueue *q = &dev->mpegq;
305
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700306 dprintk( 1, "cx8802_cancel_buffers" );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 del_timer_sync(&q->timeout);
308 cx8802_stop_dma(dev);
309 do_cancel_buffers(dev,"cancel",0);
310}
311
312static void cx8802_timeout(unsigned long data)
313{
314 struct cx8802_dev *dev = (struct cx8802_dev*)data;
315
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700316 dprintk(0, "%s\n",__FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 if (debug)
319 cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH28]);
320 cx8802_stop_dma(dev);
321 do_cancel_buffers(dev,"timeout",1);
322}
323
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -0700324static char *cx88_mpeg_irqs[32] = {
325 "ts_risci1", NULL, NULL, NULL,
326 "ts_risci2", NULL, NULL, NULL,
327 "ts_oflow", NULL, NULL, NULL,
328 "ts_sync", NULL, NULL, NULL,
329 "opc_err", "par_err", "rip_err", "pci_abort",
330 "ts_err?",
331};
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333static void cx8802_mpeg_irq(struct cx8802_dev *dev)
334{
335 struct cx88_core *core = dev->core;
336 u32 status, mask, count;
337
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700338 dprintk( 1, "cx8802_mpeg_irq\n" );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 status = cx_read(MO_TS_INTSTAT);
340 mask = cx_read(MO_TS_INTMSK);
341 if (0 == (status & mask))
342 return;
343
344 cx_write(MO_TS_INTSTAT, status);
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -0700345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (debug || (status & mask & ~0xff))
347 cx88_print_irqbits(core->name, "irq mpeg ",
348 cx88_mpeg_irqs, status, mask);
349
350 /* risc op code error */
351 if (status & (1 << 16)) {
352 printk(KERN_WARNING "%s: mpeg risc op code error\n",core->name);
353 cx_clear(MO_TS_DMACNTRL, 0x11);
354 cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH28]);
355 }
356
357 /* risc1 y */
358 if (status & 0x01) {
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700359 dprintk( 1, "wake up\n" );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 spin_lock(&dev->slock);
361 count = cx_read(MO_TS_GPCNT);
362 cx88_wakeup(dev->core, &dev->mpegq, count);
363 spin_unlock(&dev->slock);
364 }
365
366 /* risc2 y */
367 if (status & 0x10) {
368 spin_lock(&dev->slock);
369 cx8802_restart_queue(dev,&dev->mpegq);
370 spin_unlock(&dev->slock);
371 }
372
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800373 /* other general errors */
374 if (status & 0x1f0100) {
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700375 dprintk( 0, "general errors: 0x%08x\n", status & 0x1f0100 );
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800376 spin_lock(&dev->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 cx8802_stop_dma(dev);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800378 cx8802_restart_queue(dev,&dev->mpegq);
379 spin_unlock(&dev->slock);
380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700383#define MAX_IRQ_LOOP 10
384
David Howells7d12e782006-10-05 14:55:46 +0100385static irqreturn_t cx8802_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 struct cx8802_dev *dev = dev_id;
388 struct cx88_core *core = dev->core;
389 u32 status;
390 int loop, handled = 0;
391
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700392 for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x04);
394 if (0 == status)
395 goto out;
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700396 dprintk( 1, "cx8802_irq\n" );
397 dprintk( 1, " loop: %d/%d\n", loop, MAX_IRQ_LOOP );
398 dprintk( 1, " status: %d\n", status );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 handled = 1;
400 cx_write(MO_PCI_INTSTAT, status);
401
402 if (status & core->pci_irqmask)
403 cx88_core_irq(core,status);
404 if (status & 0x04)
405 cx8802_mpeg_irq(dev);
406 };
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700407 if (MAX_IRQ_LOOP == loop) {
408 dprintk( 0, "clearing mask\n" );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
410 core->name);
411 cx_write(MO_PCI_INTMSK,0);
412 }
413
414 out:
415 return IRQ_RETVAL(handled);
416}
417
418/* ----------------------------------------------------------- */
419/* exported stuff */
420
421int cx8802_init_common(struct cx8802_dev *dev)
422{
423 struct cx88_core *core = dev->core;
424 int err;
425
426 /* pci init */
427 if (pci_enable_device(dev->pci))
428 return -EIO;
429 pci_set_master(dev->pci);
430 if (!pci_dma_supported(dev->pci,0xffffffff)) {
431 printk("%s/2: Oops: no 32bit PCI DMA ???\n",dev->core->name);
432 return -EIO;
433 }
434
435 pci_read_config_byte(dev->pci, PCI_CLASS_REVISION, &dev->pci_rev);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800436 pci_read_config_byte(dev->pci, PCI_LATENCY_TIMER, &dev->pci_lat);
437 printk(KERN_INFO "%s/2: found at %s, rev: %d, irq: %d, "
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -0700438 "latency: %d, mmio: 0x%llx\n", dev->core->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 pci_name(dev->pci), dev->pci_rev, dev->pci->irq,
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -0700440 dev->pci_lat,(unsigned long long)pci_resource_start(dev->pci,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 /* initialize driver struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 spin_lock_init(&dev->slock);
444
445 /* init dma queue */
446 INIT_LIST_HEAD(&dev->mpegq.active);
447 INIT_LIST_HEAD(&dev->mpegq.queued);
448 dev->mpegq.timeout.function = cx8802_timeout;
449 dev->mpegq.timeout.data = (unsigned long)dev;
450 init_timer(&dev->mpegq.timeout);
451 cx88_risc_stopper(dev->pci,&dev->mpegq.stopper,
452 MO_TS_DMACNTRL,0x11,0x00);
453
454 /* get irq */
455 err = request_irq(dev->pci->irq, cx8802_irq,
Thomas Gleixner8076fe32006-07-01 19:29:37 -0700456 IRQF_SHARED | IRQF_DISABLED, dev->core->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (err < 0) {
458 printk(KERN_ERR "%s: can't get IRQ %d\n",
459 dev->core->name, dev->pci->irq);
460 return err;
461 }
462 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
463
464 /* everything worked */
465 pci_set_drvdata(dev->pci,dev);
466 return 0;
467}
468
469void cx8802_fini_common(struct cx8802_dev *dev)
470{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700471 dprintk( 2, "cx8802_fini_common\n" );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 cx8802_stop_dma(dev);
473 pci_disable_device(dev->pci);
474
475 /* unregister stuff */
476 free_irq(dev->pci->irq, dev);
477 pci_set_drvdata(dev->pci, NULL);
478
479 /* free memory */
480 btcx_riscmem_free(dev->pci,&dev->mpegq.stopper);
481}
482
483/* ----------------------------------------------------------- */
484
485int cx8802_suspend_common(struct pci_dev *pci_dev, pm_message_t state)
486{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800487 struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 struct cx88_core *core = dev->core;
489
490 /* stop mpeg dma */
491 spin_lock(&dev->slock);
492 if (!list_empty(&dev->mpegq.active)) {
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700493 dprintk( 2, "suspend\n" );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 printk("%s: suspend mpeg\n", core->name);
495 cx8802_stop_dma(dev);
496 del_timer(&dev->mpegq.timeout);
497 }
498 spin_unlock(&dev->slock);
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /* FIXME -- shutdown device */
501 cx88_shutdown(dev->core);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 pci_save_state(pci_dev);
504 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
505 pci_disable_device(pci_dev);
506 dev->state.disabled = 1;
507 }
508 return 0;
509}
510
511int cx8802_resume_common(struct pci_dev *pci_dev)
512{
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -0700513 struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -0700515 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 if (dev->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -0700518 err=pci_enable_device(pci_dev);
519 if (err) {
520 printk(KERN_ERR "%s: can't enable device\n",
521 dev->core->name);
522 return err;
523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 dev->state.disabled = 0;
525 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -0700526 err=pci_set_power_state(pci_dev, PCI_D0);
527 if (err) {
528 printk(KERN_ERR "%s: can't enable device\n",
529 dev->core->name);
530 pci_disable_device(pci_dev);
531 dev->state.disabled = 1;
532
533 return err;
534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 pci_restore_state(pci_dev);
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* FIXME: re-initialize hardware */
538 cx88_reset(dev->core);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 /* restart video+vbi capture */
541 spin_lock(&dev->slock);
542 if (!list_empty(&dev->mpegq.active)) {
543 printk("%s: resume mpeg\n", core->name);
544 cx8802_restart_queue(dev,&dev->mpegq);
545 }
546 spin_unlock(&dev->slock);
547
548 return 0;
549}
550
Steven Toth6c5be742006-12-02 21:15:51 -0200551struct cx8802_dev * cx8802_get_device(struct inode *inode)
552{
553 int minor = iminor(inode);
554 struct cx8802_dev *h = NULL;
555 struct list_head *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Steven Toth6c5be742006-12-02 21:15:51 -0200557 list_for_each(list,&cx8802_devlist) {
558 h = list_entry(list, struct cx8802_dev, devlist);
559 if (h->mpeg_dev->minor == minor)
560 return h;
561 }
562
563 return NULL;
564}
565
566struct cx8802_driver * cx8802_get_driver(struct cx8802_dev *dev, enum cx88_board_type btype)
567{
568 struct cx8802_dev *h = NULL;
569 struct cx8802_driver *d = NULL;
570 struct list_head *list;
571 struct list_head *list2;
572
573 list_for_each(list,&cx8802_devlist) {
574 h = list_entry(list, struct cx8802_dev, devlist);
575
576 list_for_each(list2, &h->drvlist.devlist) {
577 d = list_entry(list2, struct cx8802_driver, devlist);
578
579 /* only unregister the correct driver type */
580 if (d->type_id == btype) {
581 return d;
582 }
583 }
584 }
585
586 return NULL;
587}
588
589/* Driver asked for hardware access. */
590int cx8802_request_acquire(struct cx8802_driver *drv)
591{
592 struct cx88_core *core = drv->core;
593
594 /* Fail a request for hardware if the device is busy. */
595 if (core->active_type_id != CX88_BOARD_NONE)
596 return -EBUSY;
597
598 if (drv->advise_acquire)
599 {
600 core->active_type_id = drv->type_id;
601 drv->advise_acquire(drv);
602
603 mpeg_dbg(1,"%s() Post acquire GPIO=%x\n", __FUNCTION__, cx_read(MO_GP0_IO));
604 }
605
606 return 0;
607}
608
609/* Driver asked to release hardware. */
610int cx8802_request_release(struct cx8802_driver *drv)
611{
612 struct cx88_core *core = drv->core;
613
614 if (drv->advise_release)
615 {
616 drv->advise_release(drv);
617 core->active_type_id = CX88_BOARD_NONE;
618 mpeg_dbg(1,"%s() Post release GPIO=%x\n", __FUNCTION__, cx_read(MO_GP0_IO));
619 }
620
621 return 0;
622}
623
624static int cx8802_check_driver(struct cx8802_driver *drv)
625{
626 if (drv == NULL)
627 return -ENODEV;
628
629 if ((drv->type_id != CX88_MPEG_DVB) &&
630 (drv->type_id != CX88_MPEG_BLACKBIRD))
631 return -EINVAL;
632
633 if ((drv->hw_access != CX8802_DRVCTL_SHARED) &&
634 (drv->hw_access != CX8802_DRVCTL_EXCLUSIVE))
635 return -EINVAL;
636
637 if ((drv->probe == NULL) ||
638 (drv->remove == NULL) ||
639 (drv->advise_acquire == NULL) ||
640 (drv->advise_release == NULL))
641 return -EINVAL;
642
643 return 0;
644}
645
646int cx8802_register_driver(struct cx8802_driver *drv)
647{
648 struct cx8802_dev *h;
649 struct cx8802_driver *driver;
650 struct list_head *list;
651 int err = 0, i = 0;
652
653 printk(KERN_INFO "%s() ->registering driver type=%s access=%s\n", __FUNCTION__ ,
654 drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird",
655 drv->hw_access == CX8802_DRVCTL_SHARED ? "shared" : "exclusive");
656
657 if ((err = cx8802_check_driver(drv)) != 0) {
658 printk(KERN_INFO "%s() cx8802_driver is invalid\n", __FUNCTION__ );
659 return err;
660 }
661
662 list_for_each(list,&cx8802_devlist) {
663 i++;
664 h = list_entry(list, struct cx8802_dev, devlist);
665
666 printk(KERN_INFO "CORE %s: subsystem: %04x:%04x, board: %s [card=%d]\n",
667 h->core->name,h->pci->subsystem_vendor,
668 h->pci->subsystem_device,cx88_boards[h->core->board].name,
669 h->core->board);
670
671 /* Bring up a new struct for each driver instance */
672 driver = kzalloc(sizeof(*drv),GFP_KERNEL);
673 if (driver == NULL)
674 return -ENOMEM;
675
676 /* Snapshot of the driver registration data */
677 drv->core = h->core;
678 drv->suspend = cx8802_suspend_common;
679 drv->resume = cx8802_resume_common;
680 drv->request_acquire = cx8802_request_acquire;
681 drv->request_release = cx8802_request_release;
682 memcpy(driver, drv, sizeof(*driver));
683
684 err = drv->probe(driver);
685 if (err == 0) {
686 mutex_lock(&drv->core->lock);
687 list_add_tail(&driver->devlist,&h->drvlist.devlist);
688 mutex_unlock(&drv->core->lock);
689 } else {
690 printk(KERN_ERR "%s() ->probe failed err = %d\n", __FUNCTION__, err);
691 }
692
693 }
694 if (i == 0)
695 err = -ENODEV;
696
697 return err;
698}
699
700int cx8802_unregister_driver(struct cx8802_driver *drv)
701{
702 struct cx8802_dev *h;
703 struct cx8802_driver *d;
704 struct list_head *list;
705 struct list_head *list2, *q;
706 int err = 0, i = 0;
707
708 printk(KERN_INFO "%s() ->unregistering driver type=%s\n", __FUNCTION__ ,
709 drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird");
710
711 list_for_each(list,&cx8802_devlist) {
712 i++;
713 h = list_entry(list, struct cx8802_dev, devlist);
714
715 printk(KERN_INFO "CORE %s: subsystem: %04x:%04x, board: %s [card=%d]\n",
716 h->core->name,h->pci->subsystem_vendor,
717 h->pci->subsystem_device,cx88_boards[h->core->board].name,
718 h->core->board);
719
720 list_for_each_safe(list2, q, &h->drvlist.devlist) {
721 d = list_entry(list2, struct cx8802_driver, devlist);
722
723 /* only unregister the correct driver type */
724 if (d->type_id != drv->type_id)
725 continue;
726
727 err = d->remove(d);
728 if (err == 0) {
729 mutex_lock(&drv->core->lock);
730 list_del(list2);
731 mutex_unlock(&drv->core->lock);
732 } else
733 printk(KERN_ERR "%s() ->remove failed err = %d\n", __FUNCTION__, err);
734
735 }
736
737 }
738
739 return err;
740}
741
742/* ----------------------------------------------------------- */
743static int __devinit cx8802_probe(struct pci_dev *pci_dev,
744 const struct pci_device_id *pci_id)
745{
746 struct cx8802_dev *dev;
747 struct cx88_core *core;
748 int err;
749
750 /* general setup */
751 core = cx88_core_get(pci_dev);
752 if (NULL == core)
753 return -EINVAL;
754
755 printk("%s/2: cx2388x 8802 Driver Manager\n", core->name);
756
757 err = -ENODEV;
758 if (!cx88_boards[core->board].mpeg)
759 goto fail_core;
760
761 err = -ENOMEM;
762 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
763 if (NULL == dev)
764 goto fail_core;
765 dev->pci = pci_dev;
766 dev->core = core;
767
768 err = cx8802_init_common(dev);
769 if (err != 0)
770 goto fail_free;
771
772 INIT_LIST_HEAD(&dev->drvlist.devlist);
773 list_add_tail(&dev->devlist,&cx8802_devlist);
774
775 /* Maintain a reference so cx88-video can query the 8802 device. */
776 core->dvbdev = dev;
777 return 0;
778
779 fail_free:
780 kfree(dev);
781 fail_core:
782 cx88_core_put(core,pci_dev);
783 return err;
784}
785
786static void __devexit cx8802_remove(struct pci_dev *pci_dev)
787{
788 struct cx8802_dev *dev;
789 struct cx8802_driver *h;
790 struct list_head *list;
791
792 dev = pci_get_drvdata(pci_dev);
793
794 dprintk( 1, "%s\n", __FUNCTION__);
795
796 list_for_each(list,&dev->drvlist.devlist) {
797 h = list_entry(list, struct cx8802_driver, devlist);
798 dprintk( 1, " ->driver\n");
799 if (h->remove == NULL) {
800 printk(KERN_ERR "%s .. skipping driver, no probe function\n", __FUNCTION__);
801 continue;
802 }
803 printk(KERN_INFO "%s .. Removing driver type %d\n", __FUNCTION__, h->type_id);
804 cx8802_unregister_driver(h);
805 list_del(&dev->drvlist.devlist);
806 }
807
808 /* Destroy any 8802 reference. */
809 dev->core->dvbdev = NULL;
810
811 /* common */
812 cx8802_fini_common(dev);
813 cx88_core_put(dev->core,dev->pci);
814 kfree(dev);
815}
816
817static struct pci_device_id cx8802_pci_tbl[] = {
818 {
819 .vendor = 0x14f1,
820 .device = 0x8802,
821 .subvendor = PCI_ANY_ID,
822 .subdevice = PCI_ANY_ID,
823 },{
824 /* --- end of list --- */
825 }
826};
827MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
828
829static struct pci_driver cx8802_pci_driver = {
830 .name = "cx88-mpeg driver manager",
831 .id_table = cx8802_pci_tbl,
832 .probe = cx8802_probe,
833 .remove = __devexit_p(cx8802_remove),
834};
835
836static int cx8802_init(void)
837{
838 printk(KERN_INFO "cx2388x cx88-mpeg Driver Manager version %d.%d.%d loaded\n",
839 (CX88_VERSION_CODE >> 16) & 0xff,
840 (CX88_VERSION_CODE >> 8) & 0xff,
841 CX88_VERSION_CODE & 0xff);
842#ifdef SNAPSHOT
843 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
844 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
845#endif
846 return pci_register_driver(&cx8802_pci_driver);
847}
848
849static void cx8802_fini(void)
850{
851 pci_unregister_driver(&cx8802_pci_driver);
852}
853
854module_init(cx8802_init);
855module_exit(cx8802_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856EXPORT_SYMBOL(cx8802_buf_prepare);
857EXPORT_SYMBOL(cx8802_buf_queue);
858EXPORT_SYMBOL(cx8802_cancel_buffers);
859
860EXPORT_SYMBOL(cx8802_init_common);
861EXPORT_SYMBOL(cx8802_fini_common);
862
Steven Toth6c5be742006-12-02 21:15:51 -0200863EXPORT_SYMBOL(cx8802_register_driver);
864EXPORT_SYMBOL(cx8802_unregister_driver);
865EXPORT_SYMBOL(cx8802_get_device);
866EXPORT_SYMBOL(cx8802_get_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867/* ----------------------------------------------------------- */
868/*
869 * Local variables:
870 * c-basic-offset: 8
871 * End:
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700872 * kate: eol "unix"; indent-width 3; remove-trailing-space on; replace-trailing-space-save on; tab-width 8; replace-tabs off; space-indent off; mixed-indent off
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 */