Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/roland/infiniband
[linux-2.6] / drivers / isdn / hardware / avm / c4.c
1 /* $Id: c4.c,v 1.1.2.2 2004/01/16 21:09:27 keil Exp $
2  * 
3  * Module for AVM C4 & C2 card.
4  * 
5  * Copyright 1999 by Carsten Paeth <calle@calle.de>
6  *
7  * This software may be used and distributed according to the terms
8  * of the GNU General Public License, incorporated herein by reference.
9  *
10  */
11
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/skbuff.h>
16 #include <linux/delay.h>
17 #include <linux/mm.h>
18 #include <linux/interrupt.h>
19 #include <linux/ioport.h>
20 #include <linux/pci.h>
21 #include <linux/capi.h>
22 #include <linux/kernelcapi.h>
23 #include <linux/init.h>
24 #include <asm/io.h>
25 #include <asm/uaccess.h>
26 #include <linux/netdevice.h>
27 #include <linux/isdn/capicmd.h>
28 #include <linux/isdn/capiutil.h>
29 #include <linux/isdn/capilli.h>
30 #include "avmcard.h"
31
32 #undef CONFIG_C4_DEBUG
33 #undef CONFIG_C4_POLLDEBUG
34
35 /* ------------------------------------------------------------- */
36
37 static char *revision = "$Revision: 1.1.2.2 $";
38
39 /* ------------------------------------------------------------- */
40
41 static int suppress_pollack;
42
43 static struct pci_device_id c4_pci_tbl[] = {
44         { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21285, PCI_VENDOR_ID_AVM, PCI_DEVICE_ID_AVM_C4, 0, 0, (unsigned long)4 },
45         { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21285, PCI_VENDOR_ID_AVM, PCI_DEVICE_ID_AVM_C2, 0, 0, (unsigned long)2 },
46         { }                     /* Terminating entry */
47 };
48
49 MODULE_DEVICE_TABLE(pci, c4_pci_tbl);
50 MODULE_DESCRIPTION("CAPI4Linux: Driver for AVM C2/C4 cards");
51 MODULE_AUTHOR("Carsten Paeth");
52 MODULE_LICENSE("GPL");
53 module_param(suppress_pollack, bool, 0);
54
55 /* ------------------------------------------------------------- */
56
57 static void c4_dispatch_tx(avmcard *card);
58
59 /* ------------------------------------------------------------- */
60
61 #define DC21285_DRAM_A0MR       0x40000000
62 #define DC21285_DRAM_A1MR       0x40004000
63 #define DC21285_DRAM_A2MR       0x40008000
64 #define DC21285_DRAM_A3MR       0x4000C000
65
66 #define CAS_OFFSET      0x88
67
68 #define DC21285_ARMCSR_BASE     0x42000000
69
70 #define PCI_OUT_INT_STATUS      0x30
71 #define PCI_OUT_INT_MASK        0x34
72 #define MAILBOX_0               0x50
73 #define MAILBOX_1               0x54
74 #define MAILBOX_2               0x58
75 #define MAILBOX_3               0x5C
76 #define DOORBELL                0x60
77 #define DOORBELL_SETUP          0x64
78
79 #define CHAN_1_CONTROL          0x90
80 #define CHAN_2_CONTROL          0xB0
81 #define DRAM_TIMING             0x10C
82 #define DRAM_ADDR_SIZE_0        0x110
83 #define DRAM_ADDR_SIZE_1        0x114
84 #define DRAM_ADDR_SIZE_2        0x118
85 #define DRAM_ADDR_SIZE_3        0x11C
86 #define SA_CONTROL              0x13C
87 #define XBUS_CYCLE              0x148
88 #define XBUS_STROBE             0x14C
89 #define DBELL_PCI_MASK          0x150
90 #define DBELL_SA_MASK           0x154
91
92 #define SDRAM_SIZE              0x1000000
93
94 /* ------------------------------------------------------------- */
95
96 #define MBOX_PEEK_POKE          MAILBOX_0
97
98 #define DBELL_ADDR              0x01
99 #define DBELL_DATA              0x02
100 #define DBELL_RNWR              0x40
101 #define DBELL_INIT              0x80
102
103 /* ------------------------------------------------------------- */
104
105 #define MBOX_UP_ADDR            MAILBOX_0
106 #define MBOX_UP_LEN             MAILBOX_1
107 #define MBOX_DOWN_ADDR          MAILBOX_2
108 #define MBOX_DOWN_LEN           MAILBOX_3
109
110 #define DBELL_UP_HOST           0x00000100
111 #define DBELL_UP_ARM            0x00000200
112 #define DBELL_DOWN_HOST         0x00000400
113 #define DBELL_DOWN_ARM          0x00000800
114 #define DBELL_RESET_HOST        0x40000000
115 #define DBELL_RESET_ARM         0x80000000
116
117 /* ------------------------------------------------------------- */
118
119 #define DRAM_TIMING_DEF         0x001A01A5
120 #define DRAM_AD_SZ_DEF0         0x00000045
121 #define DRAM_AD_SZ_NULL         0x00000000
122
123 #define SA_CTL_ALLRIGHT         0x64AA0271
124
125 #define INIT_XBUS_CYCLE         0x100016DB
126 #define INIT_XBUS_STROBE        0xF1F1F1F1
127
128 /* ------------------------------------------------------------- */
129
130 #define RESET_TIMEOUT           (15*HZ) /* 15 sec */
131 #define PEEK_POKE_TIMEOUT       (HZ/10) /* 0.1 sec */
132
133 /* ------------------------------------------------------------- */
134
135 #define c4outmeml(addr, value)  writel(value, addr)
136 #define c4inmeml(addr)  readl(addr)
137 #define c4outmemw(addr, value)  writew(value, addr)
138 #define c4inmemw(addr)  readw(addr)
139 #define c4outmemb(addr, value)  writeb(value, addr)
140 #define c4inmemb(addr)  readb(addr)
141
142 /* ------------------------------------------------------------- */
143
144 static inline int wait_for_doorbell(avmcard *card, unsigned long t)
145 {
146         unsigned long stop;
147
148         stop = jiffies + t;
149         while (c4inmeml(card->mbase+DOORBELL) != 0xffffffff) {
150                 if (!time_before(jiffies, stop))
151                         return -1;
152                 mb();
153         }
154         return 0;
155 }
156
157 static int c4_poke(avmcard *card,  unsigned long off, unsigned long value)
158 {
159
160         if (wait_for_doorbell(card, HZ/10) < 0)
161                 return -1;
162         
163         c4outmeml(card->mbase+MBOX_PEEK_POKE, off);
164         c4outmeml(card->mbase+DOORBELL, DBELL_ADDR);
165
166         if (wait_for_doorbell(card, HZ/10) < 0)
167                 return -1;
168
169         c4outmeml(card->mbase+MBOX_PEEK_POKE, value);
170         c4outmeml(card->mbase+DOORBELL, DBELL_DATA | DBELL_ADDR);
171
172         return 0;
173 }
174
175 static int c4_peek(avmcard *card,  unsigned long off, unsigned long *valuep)
176 {
177         if (wait_for_doorbell(card, HZ/10) < 0)
178                 return -1;
179
180         c4outmeml(card->mbase+MBOX_PEEK_POKE, off);
181         c4outmeml(card->mbase+DOORBELL, DBELL_RNWR | DBELL_ADDR);
182
183         if (wait_for_doorbell(card, HZ/10) < 0)
184                 return -1;
185
186         *valuep = c4inmeml(card->mbase+MBOX_PEEK_POKE);
187
188         return 0;
189 }
190
191 /* ------------------------------------------------------------- */
192
193 static int c4_load_t4file(avmcard *card, capiloaddatapart * t4file)
194 {
195         u32 val;
196         unsigned char *dp;
197         u_int left;
198         u32 loadoff = 0;
199
200         dp = t4file->data;
201         left = t4file->len;
202         while (left >= sizeof(u32)) {
203                 if (t4file->user) {
204                         if (copy_from_user(&val, dp, sizeof(val)))
205                                 return -EFAULT;
206                 } else {
207                         memcpy(&val, dp, sizeof(val));
208                 }
209                 if (c4_poke(card, loadoff, val)) {
210                         printk(KERN_ERR "%s: corrupted firmware file ?\n",
211                                         card->name);
212                         return -EIO;
213                 }
214                 left -= sizeof(u32);
215                 dp += sizeof(u32);
216                 loadoff += sizeof(u32);
217         }
218         if (left) {
219                 val = 0;
220                 if (t4file->user) {
221                         if (copy_from_user(&val, dp, left))
222                                 return -EFAULT;
223                 } else {
224                         memcpy(&val, dp, left);
225                 }
226                 if (c4_poke(card, loadoff, val)) {
227                         printk(KERN_ERR "%s: corrupted firmware file ?\n",
228                                         card->name);
229                         return -EIO;
230                 }
231         }
232         return 0;
233 }
234
235 /* ------------------------------------------------------------- */
236
237 static inline void _put_byte(void **pp, u8 val)
238 {
239         u8 *s = *pp;
240         *s++ = val;
241         *pp = s;
242 }
243
244 static inline void _put_word(void **pp, u32 val)
245 {
246         u8 *s = *pp;
247         *s++ = val & 0xff;
248         *s++ = (val >> 8) & 0xff;
249         *s++ = (val >> 16) & 0xff;
250         *s++ = (val >> 24) & 0xff;
251         *pp = s;
252 }
253
254 static inline void _put_slice(void **pp, unsigned char *dp, unsigned int len)
255 {
256         unsigned i = len;
257         _put_word(pp, i);
258         while (i-- > 0)
259                 _put_byte(pp, *dp++);
260 }
261
262 static inline u8 _get_byte(void **pp)
263 {
264         u8 *s = *pp;
265         u8 val;
266         val = *s++;
267         *pp = s;
268         return val;
269 }
270
271 static inline u32 _get_word(void **pp)
272 {
273         u8 *s = *pp;
274         u32 val;
275         val = *s++;
276         val |= (*s++ << 8);
277         val |= (*s++ << 16);
278         val |= (*s++ << 24);
279         *pp = s;
280         return val;
281 }
282
283 static inline u32 _get_slice(void **pp, unsigned char *dp)
284 {
285         unsigned int len, i;
286
287         len = i = _get_word(pp);
288         while (i-- > 0) *dp++ = _get_byte(pp);
289         return len;
290 }
291
292 /* ------------------------------------------------------------- */
293
294 static void c4_reset(avmcard *card)
295 {
296         unsigned long stop;
297
298         c4outmeml(card->mbase+DOORBELL, DBELL_RESET_ARM);
299
300         stop = jiffies + HZ*10;
301         while (c4inmeml(card->mbase+DOORBELL) != 0xffffffff) {
302                 if (!time_before(jiffies, stop))
303                         return;
304                 c4outmeml(card->mbase+DOORBELL, DBELL_ADDR);
305                 mb();
306         }
307
308         c4_poke(card, DC21285_ARMCSR_BASE + CHAN_1_CONTROL, 0);
309         c4_poke(card, DC21285_ARMCSR_BASE + CHAN_2_CONTROL, 0);
310 }
311
312 /* ------------------------------------------------------------- */
313
314 static int c4_detect(avmcard *card)
315 {
316         unsigned long stop, dummy;
317
318         c4outmeml(card->mbase+PCI_OUT_INT_MASK, 0x0c);
319         if (c4inmeml(card->mbase+PCI_OUT_INT_MASK) != 0x0c)
320                 return  1;
321
322         c4outmeml(card->mbase+DOORBELL, DBELL_RESET_ARM);
323
324         stop = jiffies + HZ*10;
325         while (c4inmeml(card->mbase+DOORBELL) != 0xffffffff) {
326                 if (!time_before(jiffies, stop))
327                         return 2;
328                 c4outmeml(card->mbase+DOORBELL, DBELL_ADDR);
329                 mb();
330         }
331
332         c4_poke(card, DC21285_ARMCSR_BASE + CHAN_1_CONTROL, 0);
333         c4_poke(card, DC21285_ARMCSR_BASE + CHAN_2_CONTROL, 0);
334
335         c4outmeml(card->mbase+MAILBOX_0, 0x55aa55aa);
336         if (c4inmeml(card->mbase+MAILBOX_0) != 0x55aa55aa) return 3;
337
338         c4outmeml(card->mbase+MAILBOX_0, 0xaa55aa55);
339         if (c4inmeml(card->mbase+MAILBOX_0) != 0xaa55aa55) return 4;
340
341         if (c4_poke(card, DC21285_ARMCSR_BASE+DBELL_SA_MASK, 0)) return 5;
342         if (c4_poke(card, DC21285_ARMCSR_BASE+DBELL_PCI_MASK, 0)) return 6;
343         if (c4_poke(card, DC21285_ARMCSR_BASE+SA_CONTROL, SA_CTL_ALLRIGHT))
344                 return 7;
345         if (c4_poke(card, DC21285_ARMCSR_BASE+XBUS_CYCLE, INIT_XBUS_CYCLE))
346                 return 8;
347         if (c4_poke(card, DC21285_ARMCSR_BASE+XBUS_STROBE, INIT_XBUS_STROBE))
348                 return 8;
349         if (c4_poke(card, DC21285_ARMCSR_BASE+DRAM_TIMING, 0)) return 9;
350
351         mdelay(1);
352
353         if (c4_peek(card, DC21285_DRAM_A0MR, &dummy)) return 10;
354         if (c4_peek(card, DC21285_DRAM_A1MR, &dummy)) return 11;
355         if (c4_peek(card, DC21285_DRAM_A2MR, &dummy)) return 12;
356         if (c4_peek(card, DC21285_DRAM_A3MR, &dummy)) return 13;
357
358         if (c4_poke(card, DC21285_DRAM_A0MR+CAS_OFFSET, 0)) return 14;
359         if (c4_poke(card, DC21285_DRAM_A1MR+CAS_OFFSET, 0)) return 15;
360         if (c4_poke(card, DC21285_DRAM_A2MR+CAS_OFFSET, 0)) return 16;
361         if (c4_poke(card, DC21285_DRAM_A3MR+CAS_OFFSET, 0)) return 17;
362
363         mdelay(1);
364
365         if (c4_poke(card, DC21285_ARMCSR_BASE+DRAM_TIMING, DRAM_TIMING_DEF))
366                 return 18;
367
368         if (c4_poke(card, DC21285_ARMCSR_BASE+DRAM_ADDR_SIZE_0,DRAM_AD_SZ_DEF0))
369                 return 19;
370         if (c4_poke(card, DC21285_ARMCSR_BASE+DRAM_ADDR_SIZE_1,DRAM_AD_SZ_NULL))
371                 return 20;
372         if (c4_poke(card, DC21285_ARMCSR_BASE+DRAM_ADDR_SIZE_2,DRAM_AD_SZ_NULL))
373                 return 21;
374         if (c4_poke(card, DC21285_ARMCSR_BASE+DRAM_ADDR_SIZE_3,DRAM_AD_SZ_NULL))
375                 return 22;
376
377         /* Transputer test */
378         
379         if (   c4_poke(card, 0x000000, 0x11111111)
380             || c4_poke(card, 0x400000, 0x22222222)
381             || c4_poke(card, 0x800000, 0x33333333)
382             || c4_poke(card, 0xC00000, 0x44444444))
383                 return 23;
384
385         if (   c4_peek(card, 0x000000, &dummy) || dummy != 0x11111111
386             || c4_peek(card, 0x400000, &dummy) || dummy != 0x22222222
387             || c4_peek(card, 0x800000, &dummy) || dummy != 0x33333333
388             || c4_peek(card, 0xC00000, &dummy) || dummy != 0x44444444)
389                 return 24;
390
391         if (   c4_poke(card, 0x000000, 0x55555555)
392             || c4_poke(card, 0x400000, 0x66666666)
393             || c4_poke(card, 0x800000, 0x77777777)
394             || c4_poke(card, 0xC00000, 0x88888888))
395                 return 25;
396
397         if (   c4_peek(card, 0x000000, &dummy) || dummy != 0x55555555
398             || c4_peek(card, 0x400000, &dummy) || dummy != 0x66666666
399             || c4_peek(card, 0x800000, &dummy) || dummy != 0x77777777
400             || c4_peek(card, 0xC00000, &dummy) || dummy != 0x88888888)
401                 return 26;
402
403         return 0;
404 }
405
406 /* ------------------------------------------------------------- */
407
408 static void c4_dispatch_tx(avmcard *card)
409 {
410         avmcard_dmainfo *dma = card->dma;
411         struct sk_buff *skb;
412         u8 cmd, subcmd;
413         u16 len;
414         u32 txlen;
415         void *p;
416
417
418         if (card->csr & DBELL_DOWN_ARM) { /* tx busy */
419                 return;
420         }
421
422         skb = skb_dequeue(&dma->send_queue);
423         if (!skb) {
424 #ifdef CONFIG_C4_DEBUG
425                 printk(KERN_DEBUG "%s: tx underrun\n", card->name);
426 #endif
427                 return;
428         }
429
430         len = CAPIMSG_LEN(skb->data);
431
432         if (len) {
433                 cmd = CAPIMSG_COMMAND(skb->data);
434                 subcmd = CAPIMSG_SUBCOMMAND(skb->data);
435
436                 p = dma->sendbuf.dmabuf;
437
438                 if (CAPICMD(cmd, subcmd) == CAPI_DATA_B3_REQ) {
439                         u16 dlen = CAPIMSG_DATALEN(skb->data);
440                         _put_byte(&p, SEND_DATA_B3_REQ);
441                         _put_slice(&p, skb->data, len);
442                         _put_slice(&p, skb->data + len, dlen);
443                 } else {
444                         _put_byte(&p, SEND_MESSAGE);
445                         _put_slice(&p, skb->data, len);
446                 }
447                 txlen = (u8 *)p - (u8 *)dma->sendbuf.dmabuf;
448 #ifdef CONFIG_C4_DEBUG
449                 printk(KERN_DEBUG "%s: tx put msg len=%d\n", card->name, txlen);
450 #endif
451         } else {
452                 txlen = skb->len-2;
453 #ifdef CONFIG_C4_POLLDEBUG
454                 if (skb->data[2] == SEND_POLLACK)
455                         printk(KERN_INFO "%s: ack to c4\n", card->name);
456 #endif
457 #ifdef CONFIG_C4_DEBUG
458                 printk(KERN_DEBUG "%s: tx put 0x%x len=%d\n",
459                                 card->name, skb->data[2], txlen);
460 #endif
461                 memcpy(dma->sendbuf.dmabuf, skb->data+2, skb->len-2);
462         }
463         txlen = (txlen + 3) & ~3;
464
465         c4outmeml(card->mbase+MBOX_DOWN_ADDR, dma->sendbuf.dmaaddr);
466         c4outmeml(card->mbase+MBOX_DOWN_LEN, txlen);
467
468         card->csr |= DBELL_DOWN_ARM;
469
470         c4outmeml(card->mbase+DOORBELL, DBELL_DOWN_ARM);
471
472         dev_kfree_skb_any(skb);
473 }
474
475 /* ------------------------------------------------------------- */
476
477 static void queue_pollack(avmcard *card)
478 {
479         struct sk_buff *skb;
480         void *p;
481
482         skb = alloc_skb(3, GFP_ATOMIC);
483         if (!skb) {
484                 printk(KERN_CRIT "%s: no memory, lost poll ack\n",
485                                         card->name);
486                 return;
487         }
488         p = skb->data;
489         _put_byte(&p, 0);
490         _put_byte(&p, 0);
491         _put_byte(&p, SEND_POLLACK);
492         skb_put(skb, (u8 *)p - (u8 *)skb->data);
493
494         skb_queue_tail(&card->dma->send_queue, skb);
495         c4_dispatch_tx(card);
496 }
497
498 /* ------------------------------------------------------------- */
499
500 static void c4_handle_rx(avmcard *card)
501 {
502         avmcard_dmainfo *dma = card->dma;
503         struct capi_ctr *ctrl;
504         avmctrl_info *cinfo;
505         struct sk_buff *skb;
506         void *p = dma->recvbuf.dmabuf;
507         u32 ApplId, MsgLen, DataB3Len, NCCI, WindowSize;
508         u8 b1cmd =  _get_byte(&p);
509         u32 cidx;
510
511
512 #ifdef CONFIG_C4_DEBUG
513         printk(KERN_DEBUG "%s: rx 0x%x len=%lu\n", card->name,
514                                 b1cmd, (unsigned long)dma->recvlen);
515 #endif
516         
517         switch (b1cmd) {
518         case RECEIVE_DATA_B3_IND:
519
520                 ApplId = (unsigned) _get_word(&p);
521                 MsgLen = _get_slice(&p, card->msgbuf);
522                 DataB3Len = _get_slice(&p, card->databuf);
523                 cidx = CAPIMSG_CONTROLLER(card->msgbuf)-card->cardnr;
524                 if (cidx >= card->nlogcontr) cidx = 0;
525                 ctrl = &card->ctrlinfo[cidx].capi_ctrl;
526
527                 if (MsgLen < 30) { /* not CAPI 64Bit */
528                         memset(card->msgbuf+MsgLen, 0, 30-MsgLen);
529                         MsgLen = 30;
530                         CAPIMSG_SETLEN(card->msgbuf, 30);
531                 }
532                 if (!(skb = alloc_skb(DataB3Len+MsgLen, GFP_ATOMIC))) {
533                         printk(KERN_ERR "%s: incoming packet dropped\n",
534                                         card->name);
535                 } else {
536                         memcpy(skb_put(skb, MsgLen), card->msgbuf, MsgLen);
537                         memcpy(skb_put(skb, DataB3Len), card->databuf, DataB3Len);
538                         capi_ctr_handle_message(ctrl, ApplId, skb);
539                 }
540                 break;
541
542         case RECEIVE_MESSAGE:
543
544                 ApplId = (unsigned) _get_word(&p);
545                 MsgLen = _get_slice(&p, card->msgbuf);
546                 cidx = CAPIMSG_CONTROLLER(card->msgbuf)-card->cardnr;
547                 if (cidx >= card->nlogcontr) cidx = 0;
548                 cinfo = &card->ctrlinfo[cidx];
549                 ctrl = &card->ctrlinfo[cidx].capi_ctrl;
550
551                 if (!(skb = alloc_skb(MsgLen, GFP_ATOMIC))) {
552                         printk(KERN_ERR "%s: incoming packet dropped\n",
553                                         card->name);
554                 } else {
555                         memcpy(skb_put(skb, MsgLen), card->msgbuf, MsgLen);
556                         if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_CONF)
557                                 capilib_data_b3_conf(&cinfo->ncci_head, ApplId,
558                                                      CAPIMSG_NCCI(skb->data),
559                                                      CAPIMSG_MSGID(skb->data));
560
561                         capi_ctr_handle_message(ctrl, ApplId, skb);
562                 }
563                 break;
564
565         case RECEIVE_NEW_NCCI:
566
567                 ApplId = _get_word(&p);
568                 NCCI = _get_word(&p);
569                 WindowSize = _get_word(&p);
570                 cidx = (NCCI&0x7f) - card->cardnr;
571                 if (cidx >= card->nlogcontr) cidx = 0;
572
573                 capilib_new_ncci(&card->ctrlinfo[cidx].ncci_head, ApplId, NCCI, WindowSize);
574
575                 break;
576
577         case RECEIVE_FREE_NCCI:
578
579                 ApplId = _get_word(&p);
580                 NCCI = _get_word(&p);
581
582                 if (NCCI != 0xffffffff) {
583                         cidx = (NCCI&0x7f) - card->cardnr;
584                         if (cidx >= card->nlogcontr) cidx = 0;
585                         capilib_free_ncci(&card->ctrlinfo[cidx].ncci_head, ApplId, NCCI);
586                 }
587                 break;
588
589         case RECEIVE_START:
590 #ifdef CONFIG_C4_POLLDEBUG
591                 printk(KERN_INFO "%s: poll from c4\n", card->name);
592 #endif
593                 if (!suppress_pollack)
594                         queue_pollack(card);
595                 for (cidx=0; cidx < card->nr_controllers; cidx++) {
596                         ctrl = &card->ctrlinfo[cidx].capi_ctrl;
597                         capi_ctr_resume_output(ctrl);
598                 }
599                 break;
600
601         case RECEIVE_STOP:
602                 for (cidx=0; cidx < card->nr_controllers; cidx++) {
603                         ctrl = &card->ctrlinfo[cidx].capi_ctrl;
604                         capi_ctr_suspend_output(ctrl);
605                 }
606                 break;
607
608         case RECEIVE_INIT:
609
610                 cidx = card->nlogcontr;
611                 if (cidx >= card->nr_controllers) {
612                         printk(KERN_ERR "%s: card with %d controllers ??\n",
613                                         card->name, cidx+1);
614                         break;
615                 }
616                 card->nlogcontr++;
617                 cinfo = &card->ctrlinfo[cidx];
618                 ctrl = &cinfo->capi_ctrl;
619                 cinfo->versionlen = _get_slice(&p, cinfo->versionbuf);
620                 b1_parse_version(cinfo);
621                 printk(KERN_INFO "%s: %s-card (%s) now active\n",
622                        card->name,
623                        cinfo->version[VER_CARDTYPE],
624                        cinfo->version[VER_DRIVER]);
625                 capi_ctr_ready(&cinfo->capi_ctrl);
626                 break;
627
628         case RECEIVE_TASK_READY:
629                 ApplId = (unsigned) _get_word(&p);
630                 MsgLen = _get_slice(&p, card->msgbuf);
631                 card->msgbuf[MsgLen] = 0;
632                 while (    MsgLen > 0
633                        && (   card->msgbuf[MsgLen-1] == '\n'
634                            || card->msgbuf[MsgLen-1] == '\r')) {
635                         card->msgbuf[MsgLen-1] = 0;
636                         MsgLen--;
637                 }
638                 printk(KERN_INFO "%s: task %d \"%s\" ready.\n",
639                                 card->name, ApplId, card->msgbuf);
640                 break;
641
642         case RECEIVE_DEBUGMSG:
643                 MsgLen = _get_slice(&p, card->msgbuf);
644                 card->msgbuf[MsgLen] = 0;
645                 while (    MsgLen > 0
646                        && (   card->msgbuf[MsgLen-1] == '\n'
647                            || card->msgbuf[MsgLen-1] == '\r')) {
648                         card->msgbuf[MsgLen-1] = 0;
649                         MsgLen--;
650                 }
651                 printk(KERN_INFO "%s: DEBUG: %s\n", card->name, card->msgbuf);
652                 break;
653
654         default:
655                 printk(KERN_ERR "%s: c4_interrupt: 0x%x ???\n",
656                                 card->name, b1cmd);
657                 return;
658         }
659 }
660
661 /* ------------------------------------------------------------- */
662
663 static irqreturn_t c4_handle_interrupt(avmcard *card)
664 {
665         unsigned long flags;
666         u32 status;
667
668         spin_lock_irqsave(&card->lock, flags);
669         status = c4inmeml(card->mbase+DOORBELL);
670
671         if (status & DBELL_RESET_HOST) {
672                 u_int i;
673                 c4outmeml(card->mbase+PCI_OUT_INT_MASK, 0x0c);
674                 spin_unlock_irqrestore(&card->lock, flags);
675                 if (card->nlogcontr == 0)
676                         return IRQ_HANDLED;
677                 printk(KERN_ERR "%s: unexpected reset\n", card->name);
678                 for (i=0; i < card->nr_controllers; i++) {
679                         avmctrl_info *cinfo = &card->ctrlinfo[i];
680                         memset(cinfo->version, 0, sizeof(cinfo->version));
681                         capilib_release(&cinfo->ncci_head);
682                         capi_ctr_reseted(&cinfo->capi_ctrl);
683                 }
684                 card->nlogcontr = 0;
685                 return IRQ_HANDLED;
686         }
687
688         status &= (DBELL_UP_HOST | DBELL_DOWN_HOST);
689         if (!status) {
690                 spin_unlock_irqrestore(&card->lock, flags);
691                 return IRQ_HANDLED;
692         }
693         c4outmeml(card->mbase+DOORBELL, status);
694
695         if ((status & DBELL_UP_HOST) != 0) {
696                 card->dma->recvlen = c4inmeml(card->mbase+MBOX_UP_LEN);
697                 c4outmeml(card->mbase+MBOX_UP_LEN, 0);
698                 c4_handle_rx(card);
699                 card->dma->recvlen = 0;
700                 c4outmeml(card->mbase+MBOX_UP_LEN, card->dma->recvbuf.size);
701                 c4outmeml(card->mbase+DOORBELL, DBELL_UP_ARM);
702         }
703
704         if ((status & DBELL_DOWN_HOST) != 0) {
705                 card->csr &= ~DBELL_DOWN_ARM;
706                 c4_dispatch_tx(card);
707         } else if (card->csr & DBELL_DOWN_HOST) {
708                 if (c4inmeml(card->mbase+MBOX_DOWN_LEN) == 0) {
709                         card->csr &= ~DBELL_DOWN_ARM;
710                         c4_dispatch_tx(card);
711                 }
712         }
713         spin_unlock_irqrestore(&card->lock, flags);
714         return IRQ_HANDLED;
715 }
716
717 static irqreturn_t c4_interrupt(int interrupt, void *devptr, struct pt_regs *regs)
718 {
719         avmcard *card = devptr;
720
721         return c4_handle_interrupt(card);
722 }
723
724 /* ------------------------------------------------------------- */
725
726 static void c4_send_init(avmcard *card)
727 {
728         struct sk_buff *skb;
729         void *p;
730
731         skb = alloc_skb(15, GFP_ATOMIC);
732         if (!skb) {
733                 printk(KERN_CRIT "%s: no memory, lost register appl.\n",
734                                         card->name);
735                 return;
736         }
737         p = skb->data;
738         _put_byte(&p, 0);
739         _put_byte(&p, 0);
740         _put_byte(&p, SEND_INIT);
741         _put_word(&p, CAPI_MAXAPPL);
742         _put_word(&p, AVM_NCCI_PER_CHANNEL*30);
743         _put_word(&p, card->cardnr - 1);
744         skb_put(skb, (u8 *)p - (u8 *)skb->data);
745
746         skb_queue_tail(&card->dma->send_queue, skb);
747         c4_dispatch_tx(card);
748 }
749
750 static int queue_sendconfigword(avmcard *card, u32 val)
751 {
752         struct sk_buff *skb;
753         void *p;
754
755         skb = alloc_skb(3+4, GFP_ATOMIC);
756         if (!skb) {
757                 printk(KERN_CRIT "%s: no memory, send config\n",
758                                         card->name);
759                 return -ENOMEM;
760         }
761         p = skb->data;
762         _put_byte(&p, 0);
763         _put_byte(&p, 0);
764         _put_byte(&p, SEND_CONFIG);
765         _put_word(&p, val);
766         skb_put(skb, (u8 *)p - (u8 *)skb->data);
767
768         skb_queue_tail(&card->dma->send_queue, skb);
769         c4_dispatch_tx(card);
770         return 0;
771 }
772
773 static int queue_sendconfig(avmcard *card, char cval[4])
774 {
775         struct sk_buff *skb;
776         unsigned long flags;
777         void *p;
778
779         skb = alloc_skb(3+4, GFP_ATOMIC);
780         if (!skb) {
781                 printk(KERN_CRIT "%s: no memory, send config\n",
782                                         card->name);
783                 return -ENOMEM;
784         }
785         p = skb->data;
786         _put_byte(&p, 0);
787         _put_byte(&p, 0);
788         _put_byte(&p, SEND_CONFIG);
789         _put_byte(&p, cval[0]);
790         _put_byte(&p, cval[1]);
791         _put_byte(&p, cval[2]);
792         _put_byte(&p, cval[3]);
793         skb_put(skb, (u8 *)p - (u8 *)skb->data);
794
795         skb_queue_tail(&card->dma->send_queue, skb);
796         
797         spin_lock_irqsave(&card->lock, flags);
798         c4_dispatch_tx(card);
799         spin_unlock_irqrestore(&card->lock, flags);
800         return 0;
801 }
802
803 static int c4_send_config(avmcard *card, capiloaddatapart * config)
804 {
805         u8 val[4];
806         unsigned char *dp;
807         u_int left;
808         int retval;
809         
810         if ((retval = queue_sendconfigword(card, 1)) != 0)
811                 return retval;
812         if ((retval = queue_sendconfigword(card, config->len)) != 0)
813                 return retval;
814
815         dp = config->data;
816         left = config->len;
817         while (left >= sizeof(u32)) {
818                 if (config->user) {
819                         if (copy_from_user(val, dp, sizeof(val)))
820                                 return -EFAULT;
821                 } else {
822                         memcpy(val, dp, sizeof(val));
823                 }
824                 if ((retval = queue_sendconfig(card, val)) != 0)
825                         return retval;
826                 left -= sizeof(val);
827                 dp += sizeof(val);
828         }
829         if (left) {
830                 memset(val, 0, sizeof(val));
831                 if (config->user) {
832                         if (copy_from_user(&val, dp, left))
833                                 return -EFAULT;
834                 } else {
835                         memcpy(&val, dp, left);
836                 }
837                 if ((retval = queue_sendconfig(card, val)) != 0)
838                         return retval;
839         }
840
841         return 0;
842 }
843
844 static int c4_load_firmware(struct capi_ctr *ctrl, capiloaddata *data)
845 {
846         avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
847         avmcard *card = cinfo->card;
848         int retval;
849
850         if ((retval = c4_load_t4file(card, &data->firmware))) {
851                 printk(KERN_ERR "%s: failed to load t4file!!\n",
852                                         card->name);
853                 c4_reset(card);
854                 return retval;
855         }
856
857         card->csr = 0;
858         c4outmeml(card->mbase+MBOX_UP_LEN, 0);
859         c4outmeml(card->mbase+MBOX_DOWN_LEN, 0);
860         c4outmeml(card->mbase+DOORBELL, DBELL_INIT);
861         mdelay(1);
862         c4outmeml(card->mbase+DOORBELL,
863                         DBELL_UP_HOST | DBELL_DOWN_HOST | DBELL_RESET_HOST);
864
865         c4outmeml(card->mbase+PCI_OUT_INT_MASK, 0x08);
866
867         card->dma->recvlen = 0;
868         c4outmeml(card->mbase+MBOX_UP_ADDR, card->dma->recvbuf.dmaaddr);
869         c4outmeml(card->mbase+MBOX_UP_LEN, card->dma->recvbuf.size);
870         c4outmeml(card->mbase+DOORBELL, DBELL_UP_ARM);
871
872         if (data->configuration.len > 0 && data->configuration.data) {
873                 retval = c4_send_config(card, &data->configuration);
874                 if (retval) {
875                         printk(KERN_ERR "%s: failed to set config!!\n",
876                                         card->name);
877                         c4_reset(card);
878                         return retval;
879                 }
880         }
881
882         c4_send_init(card);
883
884         return 0;
885 }
886
887
888 static void c4_reset_ctr(struct capi_ctr *ctrl)
889 {
890         avmcard *card = ((avmctrl_info *)(ctrl->driverdata))->card;
891         avmctrl_info *cinfo;
892         u_int i;
893         unsigned long flags;
894
895         spin_lock_irqsave(&card->lock, flags);
896
897         c4_reset(card);
898
899         spin_unlock_irqrestore(&card->lock, flags);
900
901         for (i=0; i < card->nr_controllers; i++) {
902                 cinfo = &card->ctrlinfo[i];
903                 memset(cinfo->version, 0, sizeof(cinfo->version));
904                 capi_ctr_reseted(&cinfo->capi_ctrl);
905         }
906         card->nlogcontr = 0;
907 }
908
909 static void c4_remove(struct pci_dev *pdev)
910 {
911         avmcard *card = pci_get_drvdata(pdev);
912         avmctrl_info *cinfo;
913         u_int i;
914
915         if (!card)
916                 return;
917
918         c4_reset(card);
919
920         for (i=0; i < card->nr_controllers; i++) {
921                 cinfo = &card->ctrlinfo[i];
922                 detach_capi_ctr(&cinfo->capi_ctrl);
923         }
924
925         free_irq(card->irq, card);
926         iounmap(card->mbase);
927         release_region(card->port, AVMB1_PORTLEN);
928         avmcard_dma_free(card->dma);
929         pci_set_drvdata(pdev, NULL);
930         b1_free_card(card);
931 }
932
933 /* ------------------------------------------------------------- */
934
935
936 static void c4_register_appl(struct capi_ctr *ctrl,
937                                 u16 appl,
938                                 capi_register_params *rp)
939 {
940         avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
941         avmcard *card = cinfo->card;
942         struct sk_buff *skb;
943         int want = rp->level3cnt;
944         unsigned long flags;
945         int nconn;
946         void *p;
947
948         if (ctrl->cnr == card->cardnr) {
949
950                 if (want > 0) nconn = want;
951                 else nconn = ctrl->profile.nbchannel * 4 * -want;
952                 if (nconn == 0) nconn = ctrl->profile.nbchannel * 4;
953
954                 skb = alloc_skb(23, GFP_ATOMIC);
955                 if (!skb) {
956                         printk(KERN_CRIT "%s: no memory, lost register appl.\n",
957                                                 card->name);
958                         return;
959                 }
960                 p = skb->data;
961                 _put_byte(&p, 0);
962                 _put_byte(&p, 0);
963                 _put_byte(&p, SEND_REGISTER);
964                 _put_word(&p, appl);
965                 _put_word(&p, 1024 * (nconn+1));
966                 _put_word(&p, nconn);
967                 _put_word(&p, rp->datablkcnt);
968                 _put_word(&p, rp->datablklen);
969                 skb_put(skb, (u8 *)p - (u8 *)skb->data);
970
971                 skb_queue_tail(&card->dma->send_queue, skb);
972         
973                 spin_lock_irqsave(&card->lock, flags);
974                 c4_dispatch_tx(card);
975                 spin_unlock_irqrestore(&card->lock, flags);
976         }
977 }
978
979 /* ------------------------------------------------------------- */
980
981 static void c4_release_appl(struct capi_ctr *ctrl, u16 appl)
982 {
983         avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
984         avmcard *card = cinfo->card;
985         unsigned long flags;
986         struct sk_buff *skb;
987         void *p;
988
989         capilib_release_appl(&cinfo->ncci_head, appl);
990
991         if (ctrl->cnr == card->cardnr) {
992                 skb = alloc_skb(7, GFP_ATOMIC);
993                 if (!skb) {
994                         printk(KERN_CRIT "%s: no memory, lost release appl.\n",
995                                                 card->name);
996                         return;
997                 }
998                 p = skb->data;
999                 _put_byte(&p, 0);
1000                 _put_byte(&p, 0);
1001                 _put_byte(&p, SEND_RELEASE);
1002                 _put_word(&p, appl);
1003
1004                 skb_put(skb, (u8 *)p - (u8 *)skb->data);
1005                 skb_queue_tail(&card->dma->send_queue, skb);
1006                 spin_lock_irqsave(&card->lock, flags);
1007                 c4_dispatch_tx(card);
1008                 spin_unlock_irqrestore(&card->lock, flags);
1009         }
1010 }
1011
1012 /* ------------------------------------------------------------- */
1013
1014
1015 static u16 c4_send_message(struct capi_ctr *ctrl, struct sk_buff *skb)
1016 {
1017         avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
1018         avmcard *card = cinfo->card;
1019         u16 retval = CAPI_NOERROR;
1020         unsigned long flags;
1021
1022         if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
1023                 retval = capilib_data_b3_req(&cinfo->ncci_head,
1024                                              CAPIMSG_APPID(skb->data),
1025                                              CAPIMSG_NCCI(skb->data),
1026                                              CAPIMSG_MSGID(skb->data));
1027         }
1028         if (retval == CAPI_NOERROR) {
1029                 skb_queue_tail(&card->dma->send_queue, skb);
1030                 spin_lock_irqsave(&card->lock, flags);
1031                 c4_dispatch_tx(card);
1032                 spin_unlock_irqrestore(&card->lock, flags);
1033         }
1034         return retval;
1035 }
1036
1037 /* ------------------------------------------------------------- */
1038
1039 static char *c4_procinfo(struct capi_ctr *ctrl)
1040 {
1041         avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
1042
1043         if (!cinfo)
1044                 return "";
1045         sprintf(cinfo->infobuf, "%s %s 0x%x %d 0x%lx",
1046                 cinfo->cardname[0] ? cinfo->cardname : "-",
1047                 cinfo->version[VER_DRIVER] ? cinfo->version[VER_DRIVER] : "-",
1048                 cinfo->card ? cinfo->card->port : 0x0,
1049                 cinfo->card ? cinfo->card->irq : 0,
1050                 cinfo->card ? cinfo->card->membase : 0
1051                 );
1052         return cinfo->infobuf;
1053 }
1054
1055 static int c4_read_proc(char *page, char **start, off_t off,
1056                         int count, int *eof, struct capi_ctr *ctrl)
1057 {
1058         avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
1059         avmcard *card = cinfo->card;
1060         u8 flag;
1061         int len = 0;
1062         char *s;
1063
1064         len += sprintf(page+len, "%-16s %s\n", "name", card->name);
1065         len += sprintf(page+len, "%-16s 0x%x\n", "io", card->port);
1066         len += sprintf(page+len, "%-16s %d\n", "irq", card->irq);
1067         len += sprintf(page+len, "%-16s 0x%lx\n", "membase", card->membase);
1068         switch (card->cardtype) {
1069         case avm_b1isa: s = "B1 ISA"; break;
1070         case avm_b1pci: s = "B1 PCI"; break;
1071         case avm_b1pcmcia: s = "B1 PCMCIA"; break;
1072         case avm_m1: s = "M1"; break;
1073         case avm_m2: s = "M2"; break;
1074         case avm_t1isa: s = "T1 ISA (HEMA)"; break;
1075         case avm_t1pci: s = "T1 PCI"; break;
1076         case avm_c4: s = "C4"; break;
1077         case avm_c2: s = "C2"; break;
1078         default: s = "???"; break;
1079         }
1080         len += sprintf(page+len, "%-16s %s\n", "type", s);
1081         if ((s = cinfo->version[VER_DRIVER]) != 0)
1082            len += sprintf(page+len, "%-16s %s\n", "ver_driver", s);
1083         if ((s = cinfo->version[VER_CARDTYPE]) != 0)
1084            len += sprintf(page+len, "%-16s %s\n", "ver_cardtype", s);
1085         if ((s = cinfo->version[VER_SERIAL]) != 0)
1086            len += sprintf(page+len, "%-16s %s\n", "ver_serial", s);
1087
1088         if (card->cardtype != avm_m1) {
1089                 flag = ((u8 *)(ctrl->profile.manu))[3];
1090                 if (flag)
1091                         len += sprintf(page+len, "%-16s%s%s%s%s%s%s%s\n",
1092                         "protocol",
1093                         (flag & 0x01) ? " DSS1" : "",
1094                         (flag & 0x02) ? " CT1" : "",
1095                         (flag & 0x04) ? " VN3" : "",
1096                         (flag & 0x08) ? " NI1" : "",
1097                         (flag & 0x10) ? " AUSTEL" : "",
1098                         (flag & 0x20) ? " ESS" : "",
1099                         (flag & 0x40) ? " 1TR6" : ""
1100                         );
1101         }
1102         if (card->cardtype != avm_m1) {
1103                 flag = ((u8 *)(ctrl->profile.manu))[5];
1104                 if (flag)
1105                         len += sprintf(page+len, "%-16s%s%s%s%s\n",
1106                         "linetype",
1107                         (flag & 0x01) ? " point to point" : "",
1108                         (flag & 0x02) ? " point to multipoint" : "",
1109                         (flag & 0x08) ? " leased line without D-channel" : "",
1110                         (flag & 0x04) ? " leased line with D-channel" : ""
1111                         );
1112         }
1113         len += sprintf(page+len, "%-16s %s\n", "cardname", cinfo->cardname);
1114
1115         if (off+count >= len)
1116            *eof = 1;
1117         if (len < off)
1118            return 0;
1119         *start = page + off;
1120         return ((count < len-off) ? count : len-off);
1121 }
1122
1123 /* ------------------------------------------------------------- */
1124
1125 static int c4_add_card(struct capicardparams *p, struct pci_dev *dev,
1126                        int nr_controllers)
1127 {
1128         avmcard *card;
1129         avmctrl_info *cinfo;
1130         int retval;
1131         int i;
1132
1133         card = b1_alloc_card(nr_controllers);
1134         if (!card) {
1135                 printk(KERN_WARNING "c4: no memory.\n");
1136                 retval = -ENOMEM;
1137                 goto err;
1138         }
1139         card->dma = avmcard_dma_alloc("c4", dev, 2048+128, 2048+128);
1140         if (!card->dma) {
1141                 printk(KERN_WARNING "c4: no memory.\n");
1142                 retval = -ENOMEM;
1143                 goto err_free;
1144         }
1145
1146         sprintf(card->name, "c%d-%x", nr_controllers, p->port);
1147         card->port = p->port;
1148         card->irq = p->irq;
1149         card->membase = p->membase;
1150         card->cardtype = (nr_controllers == 4) ? avm_c4 : avm_c2;
1151
1152         if (!request_region(card->port, AVMB1_PORTLEN, card->name)) {
1153                 printk(KERN_WARNING "c4: ports 0x%03x-0x%03x in use.\n",
1154                        card->port, card->port + AVMB1_PORTLEN);
1155                 retval = -EBUSY;
1156                 goto err_free_dma;
1157         }
1158
1159         card->mbase = ioremap(card->membase, 128);
1160         if (card->mbase == 0) {
1161                 printk(KERN_NOTICE "c4: can't remap memory at 0x%lx\n",
1162                        card->membase);
1163                 retval = -EIO;
1164                 goto err_release_region;
1165         }
1166
1167         retval = c4_detect(card);
1168         if (retval != 0) {
1169                 printk(KERN_NOTICE "c4: NO card at 0x%x error(%d)\n",
1170                        card->port, retval);
1171                 retval = -EIO;
1172                 goto err_unmap;
1173         }
1174         c4_reset(card);
1175
1176         retval = request_irq(card->irq, c4_interrupt, SA_SHIRQ, card->name, card);
1177         if (retval) {
1178                 printk(KERN_ERR "c4: unable to get IRQ %d.\n",card->irq);
1179                 retval = -EBUSY;
1180                 goto err_unmap;
1181         }
1182
1183         for (i=0; i < nr_controllers ; i++) {
1184                 cinfo = &card->ctrlinfo[i];
1185                 cinfo->capi_ctrl.owner = THIS_MODULE;
1186                 cinfo->capi_ctrl.driver_name   = "c4";
1187                 cinfo->capi_ctrl.driverdata    = cinfo;
1188                 cinfo->capi_ctrl.register_appl = c4_register_appl;
1189                 cinfo->capi_ctrl.release_appl  = c4_release_appl;
1190                 cinfo->capi_ctrl.send_message  = c4_send_message;
1191                 cinfo->capi_ctrl.load_firmware = c4_load_firmware;
1192                 cinfo->capi_ctrl.reset_ctr     = c4_reset_ctr;
1193                 cinfo->capi_ctrl.procinfo      = c4_procinfo;
1194                 cinfo->capi_ctrl.ctr_read_proc = c4_read_proc;
1195                 strcpy(cinfo->capi_ctrl.name, card->name);
1196
1197                 retval = attach_capi_ctr(&cinfo->capi_ctrl);
1198                 if (retval) {
1199                         printk(KERN_ERR "c4: attach controller failed (%d).\n", i);
1200                         for (i--; i >= 0; i--) {
1201                                 cinfo = &card->ctrlinfo[i];
1202                                 detach_capi_ctr(&cinfo->capi_ctrl);
1203                         }
1204                         goto err_free_irq;
1205                 }
1206                 if (i == 0)
1207                         card->cardnr = cinfo->capi_ctrl.cnr;
1208         }
1209
1210         printk(KERN_INFO "c4: AVM C%d at i/o %#x, irq %d, mem %#lx\n",
1211                nr_controllers, card->port, card->irq,
1212                card->membase);
1213         pci_set_drvdata(dev, card);
1214         return 0;
1215
1216  err_free_irq:
1217         free_irq(card->irq, card);
1218  err_unmap:
1219         iounmap(card->mbase);
1220  err_release_region:
1221         release_region(card->port, AVMB1_PORTLEN);
1222  err_free_dma:
1223         avmcard_dma_free(card->dma);
1224  err_free:
1225         b1_free_card(card);
1226  err:
1227         return retval;
1228 }
1229
1230 /* ------------------------------------------------------------- */
1231
1232 static int __devinit c4_probe(struct pci_dev *dev,
1233                               const struct pci_device_id *ent)
1234 {
1235         int nr = ent->driver_data;
1236         int retval = 0;
1237         struct capicardparams param;
1238
1239         if (pci_enable_device(dev) < 0) {
1240                 printk(KERN_ERR "c4: failed to enable AVM-C%d\n", nr);
1241                 return -ENODEV;
1242         }
1243         pci_set_master(dev);
1244
1245         param.port = pci_resource_start(dev, 1);
1246         param.irq = dev->irq;
1247         param.membase = pci_resource_start(dev, 0);
1248         
1249         printk(KERN_INFO "c4: PCI BIOS reports AVM-C%d at i/o %#x, irq %d, mem %#x\n",
1250                nr, param.port, param.irq, param.membase);
1251         
1252         retval = c4_add_card(&param, dev, nr);
1253         if (retval != 0) {
1254                 printk(KERN_ERR "c4: no AVM-C%d at i/o %#x, irq %d detected, mem %#x\n",
1255                        nr, param.port, param.irq, param.membase);
1256                 return -ENODEV;
1257         }
1258         return 0;
1259 }
1260
1261 static struct pci_driver c4_pci_driver = {
1262        .name           = "c4",
1263        .id_table       = c4_pci_tbl,
1264        .probe          = c4_probe,
1265        .remove         = c4_remove,
1266 };
1267
1268 static struct capi_driver capi_driver_c2 = {
1269         .name           = "c2",
1270         .revision       = "1.0",
1271 };
1272
1273 static struct capi_driver capi_driver_c4 = {
1274         .name           = "c4",
1275         .revision       = "1.0",
1276 };
1277
1278 static int __init c4_init(void)
1279 {
1280         char *p;
1281         char rev[32];
1282         int err;
1283
1284         if ((p = strchr(revision, ':')) != 0 && p[1]) {
1285                 strlcpy(rev, p + 2, 32);
1286                 if ((p = strchr(rev, '$')) != 0 && p > rev)
1287                    *(p-1) = 0;
1288         } else
1289                 strcpy(rev, "1.0");
1290
1291         err = pci_register_driver(&c4_pci_driver);
1292         if (!err) {
1293                 strlcpy(capi_driver_c2.revision, rev, 32);
1294                 register_capi_driver(&capi_driver_c2);
1295                 strlcpy(capi_driver_c4.revision, rev, 32);
1296                 register_capi_driver(&capi_driver_c4);
1297                 printk(KERN_INFO "c4: revision %s\n", rev);
1298         }
1299         return err;
1300 }
1301
1302 static void __exit c4_exit(void)
1303 {
1304         unregister_capi_driver(&capi_driver_c2);
1305         unregister_capi_driver(&capi_driver_c4);
1306         pci_unregister_driver(&c4_pci_driver);
1307 }
1308
1309 module_init(c4_init);
1310 module_exit(c4_exit);