1 /* $Id: c4.c,v 1.1.2.2 2004/01/16 21:09:27 keil Exp $
3 * Module for AVM C4 & C2 card.
5 * Copyright 1999 by Carsten Paeth <calle@calle.de>
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
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>
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>
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>
34 #undef AVM_C4_POLLDEBUG
36 /* ------------------------------------------------------------- */
38 static char *revision
= "$Revision: 1.1.2.2 $";
40 /* ------------------------------------------------------------- */
42 static int suppress_pollack
;
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 */
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);
56 /* ------------------------------------------------------------- */
58 static void c4_dispatch_tx(avmcard
*card
);
60 /* ------------------------------------------------------------- */
62 #define DC21285_DRAM_A0MR 0x40000000
63 #define DC21285_DRAM_A1MR 0x40004000
64 #define DC21285_DRAM_A2MR 0x40008000
65 #define DC21285_DRAM_A3MR 0x4000C000
67 #define CAS_OFFSET 0x88
69 #define DC21285_ARMCSR_BASE 0x42000000
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
78 #define DOORBELL_SETUP 0x64
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
93 #define SDRAM_SIZE 0x1000000
95 /* ------------------------------------------------------------- */
97 #define MBOX_PEEK_POKE MAILBOX_0
99 #define DBELL_ADDR 0x01
100 #define DBELL_DATA 0x02
101 #define DBELL_RNWR 0x40
102 #define DBELL_INIT 0x80
104 /* ------------------------------------------------------------- */
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
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
118 /* ------------------------------------------------------------- */
120 #define DRAM_TIMING_DEF 0x001A01A5
121 #define DRAM_AD_SZ_DEF0 0x00000045
122 #define DRAM_AD_SZ_NULL 0x00000000
124 #define SA_CTL_ALLRIGHT 0x64AA0271
126 #define INIT_XBUS_CYCLE 0x100016DB
127 #define INIT_XBUS_STROBE 0xF1F1F1F1
129 /* ------------------------------------------------------------- */
131 #define RESET_TIMEOUT (15*HZ) /* 15 sec */
132 #define PEEK_POKE_TIMEOUT (HZ/10) /* 0.1 sec */
134 /* ------------------------------------------------------------- */
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)
143 /* ------------------------------------------------------------- */
145 static inline int wait_for_doorbell(avmcard
*card
, unsigned long t
)
150 while (c4inmeml(card
->mbase
+DOORBELL
) != 0xffffffff) {
151 if (!time_before(jiffies
, stop
))
158 static int c4_poke(avmcard
*card
, unsigned long off
, unsigned long value
)
161 if (wait_for_doorbell(card
, HZ
/10) < 0)
164 c4outmeml(card
->mbase
+MBOX_PEEK_POKE
, off
);
165 c4outmeml(card
->mbase
+DOORBELL
, DBELL_ADDR
);
167 if (wait_for_doorbell(card
, HZ
/10) < 0)
170 c4outmeml(card
->mbase
+MBOX_PEEK_POKE
, value
);
171 c4outmeml(card
->mbase
+DOORBELL
, DBELL_DATA
| DBELL_ADDR
);
176 static int c4_peek(avmcard
*card
, unsigned long off
, unsigned long *valuep
)
178 if (wait_for_doorbell(card
, HZ
/10) < 0)
181 c4outmeml(card
->mbase
+MBOX_PEEK_POKE
, off
);
182 c4outmeml(card
->mbase
+DOORBELL
, DBELL_RNWR
| DBELL_ADDR
);
184 if (wait_for_doorbell(card
, HZ
/10) < 0)
187 *valuep
= c4inmeml(card
->mbase
+MBOX_PEEK_POKE
);
192 /* ------------------------------------------------------------- */
194 static int c4_load_t4file(avmcard
*card
, capiloaddatapart
* t4file
)
203 while (left
>= sizeof(u32
)) {
205 if (copy_from_user(&val
, dp
, sizeof(val
)))
208 memcpy(&val
, dp
, sizeof(val
));
210 if (c4_poke(card
, loadoff
, val
)) {
211 printk(KERN_ERR
"%s: corrupted firmware file ?\n",
217 loadoff
+= sizeof(u32
);
222 if (copy_from_user(&val
, dp
, left
))
225 memcpy(&val
, dp
, left
);
227 if (c4_poke(card
, loadoff
, val
)) {
228 printk(KERN_ERR
"%s: corrupted firmware file ?\n",
236 /* ------------------------------------------------------------- */
238 static inline void _put_byte(void **pp
, u8 val
)
245 static inline void _put_word(void **pp
, u32 val
)
249 *s
++ = (val
>> 8) & 0xff;
250 *s
++ = (val
>> 16) & 0xff;
251 *s
++ = (val
>> 24) & 0xff;
255 static inline void _put_slice(void **pp
, unsigned char *dp
, unsigned int len
)
260 _put_byte(pp
, *dp
++);
263 static inline u8
_get_byte(void **pp
)
272 static inline u32
_get_word(void **pp
)
284 static inline u32
_get_slice(void **pp
, unsigned char *dp
)
288 len
= i
= _get_word(pp
);
289 while (i
-- > 0) *dp
++ = _get_byte(pp
);
293 /* ------------------------------------------------------------- */
295 static void c4_reset(avmcard
*card
)
299 c4outmeml(card
->mbase
+DOORBELL
, DBELL_RESET_ARM
);
301 stop
= jiffies
+ HZ
*10;
302 while (c4inmeml(card
->mbase
+DOORBELL
) != 0xffffffff) {
303 if (!time_before(jiffies
, stop
))
305 c4outmeml(card
->mbase
+DOORBELL
, DBELL_ADDR
);
309 c4_poke(card
, DC21285_ARMCSR_BASE
+ CHAN_1_CONTROL
, 0);
310 c4_poke(card
, DC21285_ARMCSR_BASE
+ CHAN_2_CONTROL
, 0);
313 /* ------------------------------------------------------------- */
315 static int c4_detect(avmcard
*card
)
317 unsigned long stop
, dummy
;
319 c4outmeml(card
->mbase
+PCI_OUT_INT_MASK
, 0x0c);
320 if (c4inmeml(card
->mbase
+PCI_OUT_INT_MASK
) != 0x0c)
323 c4outmeml(card
->mbase
+DOORBELL
, DBELL_RESET_ARM
);
325 stop
= jiffies
+ HZ
*10;
326 while (c4inmeml(card
->mbase
+DOORBELL
) != 0xffffffff) {
327 if (!time_before(jiffies
, stop
))
329 c4outmeml(card
->mbase
+DOORBELL
, DBELL_ADDR
);
333 c4_poke(card
, DC21285_ARMCSR_BASE
+ CHAN_1_CONTROL
, 0);
334 c4_poke(card
, DC21285_ARMCSR_BASE
+ CHAN_2_CONTROL
, 0);
336 c4outmeml(card
->mbase
+MAILBOX_0
, 0x55aa55aa);
337 if (c4inmeml(card
->mbase
+MAILBOX_0
) != 0x55aa55aa) return 3;
339 c4outmeml(card
->mbase
+MAILBOX_0
, 0xaa55aa55);
340 if (c4inmeml(card
->mbase
+MAILBOX_0
) != 0xaa55aa55) return 4;
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
))
346 if (c4_poke(card
, DC21285_ARMCSR_BASE
+XBUS_CYCLE
, INIT_XBUS_CYCLE
))
348 if (c4_poke(card
, DC21285_ARMCSR_BASE
+XBUS_STROBE
, INIT_XBUS_STROBE
))
350 if (c4_poke(card
, DC21285_ARMCSR_BASE
+DRAM_TIMING
, 0)) return 9;
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;
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;
366 if (c4_poke(card
, DC21285_ARMCSR_BASE
+DRAM_TIMING
, DRAM_TIMING_DEF
))
369 if (c4_poke(card
, DC21285_ARMCSR_BASE
+DRAM_ADDR_SIZE_0
,DRAM_AD_SZ_DEF0
))
371 if (c4_poke(card
, DC21285_ARMCSR_BASE
+DRAM_ADDR_SIZE_1
,DRAM_AD_SZ_NULL
))
373 if (c4_poke(card
, DC21285_ARMCSR_BASE
+DRAM_ADDR_SIZE_2
,DRAM_AD_SZ_NULL
))
375 if (c4_poke(card
, DC21285_ARMCSR_BASE
+DRAM_ADDR_SIZE_3
,DRAM_AD_SZ_NULL
))
378 /* Transputer test */
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))
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)
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))
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)
407 /* ------------------------------------------------------------- */
409 static void c4_dispatch_tx(avmcard
*card
)
411 avmcard_dmainfo
*dma
= card
->dma
;
419 if (card
->csr
& DBELL_DOWN_ARM
) { /* tx busy */
423 skb
= skb_dequeue(&dma
->send_queue
);
426 printk(KERN_DEBUG
"%s: tx underrun\n", card
->name
);
431 len
= CAPIMSG_LEN(skb
->data
);
434 cmd
= CAPIMSG_COMMAND(skb
->data
);
435 subcmd
= CAPIMSG_SUBCOMMAND(skb
->data
);
437 p
= dma
->sendbuf
.dmabuf
;
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
);
445 _put_byte(&p
, SEND_MESSAGE
);
446 _put_slice(&p
, skb
->data
, len
);
448 txlen
= (u8
*)p
- (u8
*)dma
->sendbuf
.dmabuf
;
450 printk(KERN_DEBUG
"%s: tx put msg len=%d\n", card
->name
, txlen
);
454 #ifdef AVM_C4_POLLDEBUG
455 if (skb
->data
[2] == SEND_POLLACK
)
456 printk(KERN_INFO
"%s: ack to c4\n", card
->name
);
459 printk(KERN_DEBUG
"%s: tx put 0x%x len=%d\n",
460 card
->name
, skb
->data
[2], txlen
);
462 skb_copy_from_linear_data_offset(skb
, 2, dma
->sendbuf
.dmabuf
,
465 txlen
= (txlen
+ 3) & ~3;
467 c4outmeml(card
->mbase
+MBOX_DOWN_ADDR
, dma
->sendbuf
.dmaaddr
);
468 c4outmeml(card
->mbase
+MBOX_DOWN_LEN
, txlen
);
470 card
->csr
|= DBELL_DOWN_ARM
;
472 c4outmeml(card
->mbase
+DOORBELL
, DBELL_DOWN_ARM
);
474 dev_kfree_skb_any(skb
);
477 /* ------------------------------------------------------------- */
479 static void queue_pollack(avmcard
*card
)
484 skb
= alloc_skb(3, GFP_ATOMIC
);
486 printk(KERN_CRIT
"%s: no memory, lost poll ack\n",
493 _put_byte(&p
, SEND_POLLACK
);
494 skb_put(skb
, (u8
*)p
- (u8
*)skb
->data
);
496 skb_queue_tail(&card
->dma
->send_queue
, skb
);
497 c4_dispatch_tx(card
);
500 /* ------------------------------------------------------------- */
502 static void c4_handle_rx(avmcard
*card
)
504 avmcard_dmainfo
*dma
= card
->dma
;
505 struct capi_ctr
*ctrl
;
508 void *p
= dma
->recvbuf
.dmabuf
;
509 u32 ApplId
, MsgLen
, DataB3Len
, NCCI
, WindowSize
;
510 u8 b1cmd
= _get_byte(&p
);
515 printk(KERN_DEBUG
"%s: rx 0x%x len=%lu\n", card
->name
,
516 b1cmd
, (unsigned long)dma
->recvlen
);
520 case RECEIVE_DATA_B3_IND
:
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
;
529 if (MsgLen
< 30) { /* not CAPI 64Bit */
530 memset(card
->msgbuf
+MsgLen
, 0, 30-MsgLen
);
532 CAPIMSG_SETLEN(card
->msgbuf
, 30);
534 if (!(skb
= alloc_skb(DataB3Len
+MsgLen
, GFP_ATOMIC
))) {
535 printk(KERN_ERR
"%s: incoming packet dropped\n",
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
);
544 case RECEIVE_MESSAGE
:
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
;
553 if (!(skb
= alloc_skb(MsgLen
, GFP_ATOMIC
))) {
554 printk(KERN_ERR
"%s: incoming packet dropped\n",
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
));
563 capi_ctr_handle_message(ctrl
, ApplId
, skb
);
567 case RECEIVE_NEW_NCCI
:
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;
575 capilib_new_ncci(&card
->ctrlinfo
[cidx
].ncci_head
, ApplId
, NCCI
, WindowSize
);
579 case RECEIVE_FREE_NCCI
:
581 ApplId
= _get_word(&p
);
582 NCCI
= _get_word(&p
);
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
);
592 #ifdef AVM_C4_POLLDEBUG
593 printk(KERN_INFO
"%s: poll from c4\n", card
->name
);
595 if (!suppress_pollack
)
597 for (cidx
=0; cidx
< card
->nr_controllers
; cidx
++) {
598 ctrl
= &card
->ctrlinfo
[cidx
].capi_ctrl
;
599 capi_ctr_resume_output(ctrl
);
604 for (cidx
=0; cidx
< card
->nr_controllers
; cidx
++) {
605 ctrl
= &card
->ctrlinfo
[cidx
].capi_ctrl
;
606 capi_ctr_suspend_output(ctrl
);
612 cidx
= card
->nlogcontr
;
613 if (cidx
>= card
->nr_controllers
) {
614 printk(KERN_ERR
"%s: card with %d controllers ??\n",
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",
625 cinfo
->version
[VER_CARDTYPE
],
626 cinfo
->version
[VER_DRIVER
]);
627 capi_ctr_ready(&cinfo
->capi_ctrl
);
630 case RECEIVE_TASK_READY
:
631 ApplId
= (unsigned) _get_word(&p
);
632 MsgLen
= _get_slice(&p
, card
->msgbuf
);
633 card
->msgbuf
[MsgLen
] = 0;
635 && ( card
->msgbuf
[MsgLen
-1] == '\n'
636 || card
->msgbuf
[MsgLen
-1] == '\r')) {
637 card
->msgbuf
[MsgLen
-1] = 0;
640 printk(KERN_INFO
"%s: task %d \"%s\" ready.\n",
641 card
->name
, ApplId
, card
->msgbuf
);
644 case RECEIVE_DEBUGMSG
:
645 MsgLen
= _get_slice(&p
, card
->msgbuf
);
646 card
->msgbuf
[MsgLen
] = 0;
648 && ( card
->msgbuf
[MsgLen
-1] == '\n'
649 || card
->msgbuf
[MsgLen
-1] == '\r')) {
650 card
->msgbuf
[MsgLen
-1] = 0;
653 printk(KERN_INFO
"%s: DEBUG: %s\n", card
->name
, card
->msgbuf
);
657 printk(KERN_ERR
"%s: c4_interrupt: 0x%x ???\n",
663 /* ------------------------------------------------------------- */
665 static irqreturn_t
c4_handle_interrupt(avmcard
*card
)
670 spin_lock_irqsave(&card
->lock
, flags
);
671 status
= c4inmeml(card
->mbase
+DOORBELL
);
673 if (status
& DBELL_RESET_HOST
) {
675 c4outmeml(card
->mbase
+PCI_OUT_INT_MASK
, 0x0c);
676 spin_unlock_irqrestore(&card
->lock
, flags
);
677 if (card
->nlogcontr
== 0)
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
);
692 status
&= (DBELL_UP_HOST
| DBELL_DOWN_HOST
);
694 spin_unlock_irqrestore(&card
->lock
, flags
);
697 c4outmeml(card
->mbase
+DOORBELL
, status
);
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);
703 card
->dma
->recvlen
= 0;
704 c4outmeml(card
->mbase
+MBOX_UP_LEN
, card
->dma
->recvbuf
.size
);
705 c4outmeml(card
->mbase
+DOORBELL
, DBELL_UP_ARM
);
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
);
717 spin_unlock_irqrestore(&card
->lock
, flags
);
721 static irqreturn_t
c4_interrupt(int interrupt
, void *devptr
)
723 avmcard
*card
= devptr
;
725 return c4_handle_interrupt(card
);
728 /* ------------------------------------------------------------- */
730 static void c4_send_init(avmcard
*card
)
736 skb
= alloc_skb(15, GFP_ATOMIC
);
738 printk(KERN_CRIT
"%s: no memory, lost register appl.\n",
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
);
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
);
757 static int queue_sendconfigword(avmcard
*card
, u32 val
)
763 skb
= alloc_skb(3+4, GFP_ATOMIC
);
765 printk(KERN_CRIT
"%s: no memory, send config\n",
772 _put_byte(&p
, SEND_CONFIG
);
774 skb_put(skb
, (u8
*)p
- (u8
*)skb
->data
);
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
);
783 static int queue_sendconfig(avmcard
*card
, char cval
[4])
789 skb
= alloc_skb(3+4, GFP_ATOMIC
);
791 printk(KERN_CRIT
"%s: no memory, send config\n",
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
);
805 skb_queue_tail(&card
->dma
->send_queue
, skb
);
807 spin_lock_irqsave(&card
->lock
, flags
);
808 c4_dispatch_tx(card
);
809 spin_unlock_irqrestore(&card
->lock
, flags
);
813 static int c4_send_config(avmcard
*card
, capiloaddatapart
* config
)
820 if ((retval
= queue_sendconfigword(card
, 1)) != 0)
822 if ((retval
= queue_sendconfigword(card
, config
->len
)) != 0)
827 while (left
>= sizeof(u32
)) {
829 if (copy_from_user(val
, dp
, sizeof(val
)))
832 memcpy(val
, dp
, sizeof(val
));
834 if ((retval
= queue_sendconfig(card
, val
)) != 0)
840 memset(val
, 0, sizeof(val
));
842 if (copy_from_user(&val
, dp
, left
))
845 memcpy(&val
, dp
, left
);
847 if ((retval
= queue_sendconfig(card
, val
)) != 0)
854 static int c4_load_firmware(struct capi_ctr
*ctrl
, capiloaddata
*data
)
856 avmctrl_info
*cinfo
= (avmctrl_info
*)(ctrl
->driverdata
);
857 avmcard
*card
= cinfo
->card
;
860 if ((retval
= c4_load_t4file(card
, &data
->firmware
))) {
861 printk(KERN_ERR
"%s: failed to load t4file!!\n",
868 c4outmeml(card
->mbase
+MBOX_UP_LEN
, 0);
869 c4outmeml(card
->mbase
+MBOX_DOWN_LEN
, 0);
870 c4outmeml(card
->mbase
+DOORBELL
, DBELL_INIT
);
872 c4outmeml(card
->mbase
+DOORBELL
,
873 DBELL_UP_HOST
| DBELL_DOWN_HOST
| DBELL_RESET_HOST
);
875 c4outmeml(card
->mbase
+PCI_OUT_INT_MASK
, 0x08);
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
);
882 if (data
->configuration
.len
> 0 && data
->configuration
.data
) {
883 retval
= c4_send_config(card
, &data
->configuration
);
885 printk(KERN_ERR
"%s: failed to set config!!\n",
898 static void c4_reset_ctr(struct capi_ctr
*ctrl
)
900 avmcard
*card
= ((avmctrl_info
*)(ctrl
->driverdata
))->card
;
905 spin_lock_irqsave(&card
->lock
, flags
);
909 spin_unlock_irqrestore(&card
->lock
, flags
);
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
);
919 static void c4_remove(struct pci_dev
*pdev
)
921 avmcard
*card
= pci_get_drvdata(pdev
);
930 for (i
=0; i
< card
->nr_controllers
; i
++) {
931 cinfo
= &card
->ctrlinfo
[i
];
932 detach_capi_ctr(&cinfo
->capi_ctrl
);
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
);
943 /* ------------------------------------------------------------- */
946 static void c4_register_appl(struct capi_ctr
*ctrl
,
948 capi_register_params
*rp
)
950 avmctrl_info
*cinfo
= (avmctrl_info
*)(ctrl
->driverdata
);
951 avmcard
*card
= cinfo
->card
;
953 int want
= rp
->level3cnt
;
958 if (ctrl
->cnr
== card
->cardnr
) {
960 if (want
> 0) nconn
= want
;
961 else nconn
= ctrl
->profile
.nbchannel
* 4 * -want
;
962 if (nconn
== 0) nconn
= ctrl
->profile
.nbchannel
* 4;
964 skb
= alloc_skb(23, GFP_ATOMIC
);
966 printk(KERN_CRIT
"%s: no memory, lost register appl.\n",
973 _put_byte(&p
, SEND_REGISTER
);
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
);
981 skb_queue_tail(&card
->dma
->send_queue
, skb
);
983 spin_lock_irqsave(&card
->lock
, flags
);
984 c4_dispatch_tx(card
);
985 spin_unlock_irqrestore(&card
->lock
, flags
);
989 /* ------------------------------------------------------------- */
991 static void c4_release_appl(struct capi_ctr
*ctrl
, u16 appl
)
993 avmctrl_info
*cinfo
= (avmctrl_info
*)(ctrl
->driverdata
);
994 avmcard
*card
= cinfo
->card
;
999 spin_lock_irqsave(&card
->lock
, flags
);
1000 capilib_release_appl(&cinfo
->ncci_head
, appl
);
1001 spin_unlock_irqrestore(&card
->lock
, flags
);
1003 if (ctrl
->cnr
== card
->cardnr
) {
1004 skb
= alloc_skb(7, GFP_ATOMIC
);
1006 printk(KERN_CRIT
"%s: no memory, lost release appl.\n",
1013 _put_byte(&p
, SEND_RELEASE
);
1014 _put_word(&p
, appl
);
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
);
1024 /* ------------------------------------------------------------- */
1027 static u16
c4_send_message(struct capi_ctr
*ctrl
, struct sk_buff
*skb
)
1029 avmctrl_info
*cinfo
= (avmctrl_info
*)(ctrl
->driverdata
);
1030 avmcard
*card
= cinfo
->card
;
1031 u16 retval
= CAPI_NOERROR
;
1032 unsigned long flags
;
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
));
1041 if (retval
== CAPI_NOERROR
) {
1042 skb_queue_tail(&card
->dma
->send_queue
, skb
);
1043 c4_dispatch_tx(card
);
1045 spin_unlock_irqrestore(&card
->lock
, flags
);
1049 /* ------------------------------------------------------------- */
1051 static char *c4_procinfo(struct capi_ctr
*ctrl
)
1053 avmctrl_info
*cinfo
= (avmctrl_info
*)(ctrl
->driverdata
);
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
1064 return cinfo
->infobuf
;
1067 static int c4_proc_show(struct seq_file
*m
, void *v
)
1069 struct capi_ctr
*ctrl
= m
->private;
1070 avmctrl_info
*cinfo
= (avmctrl_info
*)(ctrl
->driverdata
);
1071 avmcard
*card
= cinfo
->card
;
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;
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
);
1099 if (card
->cardtype
!= avm_m1
) {
1100 flag
= ((u8
*)(ctrl
->profile
.manu
))[3];
1102 seq_printf(m
, "%-16s%s%s%s%s%s%s%s\n",
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" : ""
1113 if (card
->cardtype
!= avm_m1
) {
1114 flag
= ((u8
*)(ctrl
->profile
.manu
))[5];
1116 seq_printf(m
, "%-16s%s%s%s%s\n",
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" : ""
1124 seq_printf(m
, "%-16s %s\n", "cardname", cinfo
->cardname
);
1129 static int c4_proc_open(struct inode
*inode
, struct file
*file
)
1131 return single_open(file
, c4_proc_show
, PDE(inode
)->data
);
1134 static const struct file_operations c4_proc_fops
= {
1135 .owner
= THIS_MODULE
,
1136 .open
= c4_proc_open
,
1138 .llseek
= seq_lseek
,
1139 .release
= single_release
,
1142 /* ------------------------------------------------------------- */
1144 static int c4_add_card(struct capicardparams
*p
, struct pci_dev
*dev
,
1148 avmctrl_info
*cinfo
;
1152 card
= b1_alloc_card(nr_controllers
);
1154 printk(KERN_WARNING
"c4: no memory.\n");
1158 card
->dma
= avmcard_dma_alloc("c4", dev
, 2048+128, 2048+128);
1160 printk(KERN_WARNING
"c4: no memory.\n");
1165 sprintf(card
->name
, "c%d-%x", nr_controllers
, p
->port
);
1166 card
->port
= p
->port
;
1168 card
->membase
= p
->membase
;
1169 card
->cardtype
= (nr_controllers
== 4) ? avm_c4
: avm_c2
;
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
);
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",
1183 goto err_release_region
;
1186 retval
= c4_detect(card
);
1188 printk(KERN_NOTICE
"c4: NO card at 0x%x error(%d)\n",
1189 card
->port
, retval
);
1195 retval
= request_irq(card
->irq
, c4_interrupt
, IRQF_SHARED
, card
->name
, card
);
1197 printk(KERN_ERR
"c4: unable to get IRQ %d.\n",card
->irq
);
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
);
1216 retval
= attach_capi_ctr(&cinfo
->capi_ctrl
);
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
);
1226 card
->cardnr
= cinfo
->capi_ctrl
.cnr
;
1229 printk(KERN_INFO
"c4: AVM C%d at i/o %#x, irq %d, mem %#lx\n",
1230 nr_controllers
, card
->port
, card
->irq
,
1232 pci_set_drvdata(dev
, card
);
1236 free_irq(card
->irq
, card
);
1238 iounmap(card
->mbase
);
1240 release_region(card
->port
, AVMB1_PORTLEN
);
1242 avmcard_dma_free(card
->dma
);
1249 /* ------------------------------------------------------------- */
1251 static int __devinit
c4_probe(struct pci_dev
*dev
,
1252 const struct pci_device_id
*ent
)
1254 int nr
= ent
->driver_data
;
1256 struct capicardparams param
;
1258 if (pci_enable_device(dev
) < 0) {
1259 printk(KERN_ERR
"c4: failed to enable AVM-C%d\n", nr
);
1262 pci_set_master(dev
);
1264 param
.port
= pci_resource_start(dev
, 1);
1265 param
.irq
= dev
->irq
;
1266 param
.membase
= pci_resource_start(dev
, 0);
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
);
1271 retval
= c4_add_card(¶m
, dev
, nr
);
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
);
1280 static struct pci_driver c4_pci_driver
= {
1282 .id_table
= c4_pci_tbl
,
1284 .remove
= c4_remove
,
1287 static struct capi_driver capi_driver_c2
= {
1292 static struct capi_driver capi_driver_c4
= {
1297 static int __init
c4_init(void)
1303 if ((p
= strchr(revision
, ':')) != NULL
&& p
[1]) {
1304 strlcpy(rev
, p
+ 2, 32);
1305 if ((p
= strchr(rev
, '$')) != NULL
&& p
> rev
)
1310 err
= pci_register_driver(&c4_pci_driver
);
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
);
1321 static void __exit
c4_exit(void)
1323 unregister_capi_driver(&capi_driver_c2
);
1324 unregister_capi_driver(&capi_driver_c4
);
1325 pci_unregister_driver(&c4_pci_driver
);
1328 module_init(c4_init
);
1329 module_exit(c4_exit
);