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