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