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>
25 #include <linux/gfp.h>
27 #include <asm/uaccess.h>
28 #include <linux/netdevice.h>
29 #include <linux/isdn/capicmd.h>
30 #include <linux/isdn/capiutil.h>
31 #include <linux/isdn/capilli.h>
35 #undef AVM_C4_POLLDEBUG
37 /* ------------------------------------------------------------- */
39 static char *revision
= "$Revision: 1.1.2.2 $";
41 /* ------------------------------------------------------------- */
43 static bool suppress_pollack
;
45 static struct pci_device_id c4_pci_tbl
[] = {
46 { PCI_VENDOR_ID_DEC
, PCI_DEVICE_ID_DEC_21285
, PCI_VENDOR_ID_AVM
, PCI_DEVICE_ID_AVM_C4
, 0, 0, (unsigned long)4 },
47 { PCI_VENDOR_ID_DEC
, PCI_DEVICE_ID_DEC_21285
, PCI_VENDOR_ID_AVM
, PCI_DEVICE_ID_AVM_C2
, 0, 0, (unsigned long)2 },
48 { } /* Terminating entry */
51 MODULE_DEVICE_TABLE(pci
, c4_pci_tbl
);
52 MODULE_DESCRIPTION("CAPI4Linux: Driver for AVM C2/C4 cards");
53 MODULE_AUTHOR("Carsten Paeth");
54 MODULE_LICENSE("GPL");
55 module_param(suppress_pollack
, bool, 0);
57 /* ------------------------------------------------------------- */
59 static void c4_dispatch_tx(avmcard
*card
);
61 /* ------------------------------------------------------------- */
63 #define DC21285_DRAM_A0MR 0x40000000
64 #define DC21285_DRAM_A1MR 0x40004000
65 #define DC21285_DRAM_A2MR 0x40008000
66 #define DC21285_DRAM_A3MR 0x4000C000
68 #define CAS_OFFSET 0x88
70 #define DC21285_ARMCSR_BASE 0x42000000
72 #define PCI_OUT_INT_STATUS 0x30
73 #define PCI_OUT_INT_MASK 0x34
74 #define MAILBOX_0 0x50
75 #define MAILBOX_1 0x54
76 #define MAILBOX_2 0x58
77 #define MAILBOX_3 0x5C
79 #define DOORBELL_SETUP 0x64
81 #define CHAN_1_CONTROL 0x90
82 #define CHAN_2_CONTROL 0xB0
83 #define DRAM_TIMING 0x10C
84 #define DRAM_ADDR_SIZE_0 0x110
85 #define DRAM_ADDR_SIZE_1 0x114
86 #define DRAM_ADDR_SIZE_2 0x118
87 #define DRAM_ADDR_SIZE_3 0x11C
88 #define SA_CONTROL 0x13C
89 #define XBUS_CYCLE 0x148
90 #define XBUS_STROBE 0x14C
91 #define DBELL_PCI_MASK 0x150
92 #define DBELL_SA_MASK 0x154
94 #define SDRAM_SIZE 0x1000000
96 /* ------------------------------------------------------------- */
98 #define MBOX_PEEK_POKE MAILBOX_0
100 #define DBELL_ADDR 0x01
101 #define DBELL_DATA 0x02
102 #define DBELL_RNWR 0x40
103 #define DBELL_INIT 0x80
105 /* ------------------------------------------------------------- */
107 #define MBOX_UP_ADDR MAILBOX_0
108 #define MBOX_UP_LEN MAILBOX_1
109 #define MBOX_DOWN_ADDR MAILBOX_2
110 #define MBOX_DOWN_LEN MAILBOX_3
112 #define DBELL_UP_HOST 0x00000100
113 #define DBELL_UP_ARM 0x00000200
114 #define DBELL_DOWN_HOST 0x00000400
115 #define DBELL_DOWN_ARM 0x00000800
116 #define DBELL_RESET_HOST 0x40000000
117 #define DBELL_RESET_ARM 0x80000000
119 /* ------------------------------------------------------------- */
121 #define DRAM_TIMING_DEF 0x001A01A5
122 #define DRAM_AD_SZ_DEF0 0x00000045
123 #define DRAM_AD_SZ_NULL 0x00000000
125 #define SA_CTL_ALLRIGHT 0x64AA0271
127 #define INIT_XBUS_CYCLE 0x100016DB
128 #define INIT_XBUS_STROBE 0xF1F1F1F1
130 /* ------------------------------------------------------------- */
132 #define RESET_TIMEOUT (15*HZ) /* 15 sec */
133 #define PEEK_POKE_TIMEOUT (HZ/10) /* 0.1 sec */
135 /* ------------------------------------------------------------- */
137 #define c4outmeml(addr, value) writel(value, addr)
138 #define c4inmeml(addr) readl(addr)
139 #define c4outmemw(addr, value) writew(value, addr)
140 #define c4inmemw(addr) readw(addr)
141 #define c4outmemb(addr, value) writeb(value, addr)
142 #define c4inmemb(addr) readb(addr)
144 /* ------------------------------------------------------------- */
146 static inline int wait_for_doorbell(avmcard
*card
, unsigned long t
)
151 while (c4inmeml(card
->mbase
+DOORBELL
) != 0xffffffff) {
152 if (!time_before(jiffies
, stop
))
159 static int c4_poke(avmcard
*card
, unsigned long off
, unsigned long value
)
162 if (wait_for_doorbell(card
, HZ
/10) < 0)
165 c4outmeml(card
->mbase
+MBOX_PEEK_POKE
, off
);
166 c4outmeml(card
->mbase
+DOORBELL
, DBELL_ADDR
);
168 if (wait_for_doorbell(card
, HZ
/10) < 0)
171 c4outmeml(card
->mbase
+MBOX_PEEK_POKE
, value
);
172 c4outmeml(card
->mbase
+DOORBELL
, DBELL_DATA
| DBELL_ADDR
);
177 static int c4_peek(avmcard
*card
, unsigned long off
, unsigned long *valuep
)
179 if (wait_for_doorbell(card
, HZ
/10) < 0)
182 c4outmeml(card
->mbase
+MBOX_PEEK_POKE
, off
);
183 c4outmeml(card
->mbase
+DOORBELL
, DBELL_RNWR
| DBELL_ADDR
);
185 if (wait_for_doorbell(card
, HZ
/10) < 0)
188 *valuep
= c4inmeml(card
->mbase
+MBOX_PEEK_POKE
);
193 /* ------------------------------------------------------------- */
195 static int c4_load_t4file(avmcard
*card
, capiloaddatapart
* t4file
)
204 while (left
>= sizeof(u32
)) {
206 if (copy_from_user(&val
, dp
, sizeof(val
)))
209 memcpy(&val
, dp
, sizeof(val
));
211 if (c4_poke(card
, loadoff
, val
)) {
212 printk(KERN_ERR
"%s: corrupted firmware file ?\n",
218 loadoff
+= sizeof(u32
);
223 if (copy_from_user(&val
, dp
, left
))
226 memcpy(&val
, dp
, left
);
228 if (c4_poke(card
, loadoff
, val
)) {
229 printk(KERN_ERR
"%s: corrupted firmware file ?\n",
237 /* ------------------------------------------------------------- */
239 static inline void _put_byte(void **pp
, u8 val
)
246 static inline void _put_word(void **pp
, u32 val
)
250 *s
++ = (val
>> 8) & 0xff;
251 *s
++ = (val
>> 16) & 0xff;
252 *s
++ = (val
>> 24) & 0xff;
256 static inline void _put_slice(void **pp
, unsigned char *dp
, unsigned int len
)
261 _put_byte(pp
, *dp
++);
264 static inline u8
_get_byte(void **pp
)
273 static inline u32
_get_word(void **pp
)
285 static inline u32
_get_slice(void **pp
, unsigned char *dp
)
289 len
= i
= _get_word(pp
);
290 while (i
-- > 0) *dp
++ = _get_byte(pp
);
294 /* ------------------------------------------------------------- */
296 static void c4_reset(avmcard
*card
)
300 c4outmeml(card
->mbase
+DOORBELL
, DBELL_RESET_ARM
);
302 stop
= jiffies
+ HZ
*10;
303 while (c4inmeml(card
->mbase
+DOORBELL
) != 0xffffffff) {
304 if (!time_before(jiffies
, stop
))
306 c4outmeml(card
->mbase
+DOORBELL
, DBELL_ADDR
);
310 c4_poke(card
, DC21285_ARMCSR_BASE
+ CHAN_1_CONTROL
, 0);
311 c4_poke(card
, DC21285_ARMCSR_BASE
+ CHAN_2_CONTROL
, 0);
314 /* ------------------------------------------------------------- */
316 static int c4_detect(avmcard
*card
)
318 unsigned long stop
, dummy
;
320 c4outmeml(card
->mbase
+PCI_OUT_INT_MASK
, 0x0c);
321 if (c4inmeml(card
->mbase
+PCI_OUT_INT_MASK
) != 0x0c)
324 c4outmeml(card
->mbase
+DOORBELL
, DBELL_RESET_ARM
);
326 stop
= jiffies
+ HZ
*10;
327 while (c4inmeml(card
->mbase
+DOORBELL
) != 0xffffffff) {
328 if (!time_before(jiffies
, stop
))
330 c4outmeml(card
->mbase
+DOORBELL
, DBELL_ADDR
);
334 c4_poke(card
, DC21285_ARMCSR_BASE
+ CHAN_1_CONTROL
, 0);
335 c4_poke(card
, DC21285_ARMCSR_BASE
+ CHAN_2_CONTROL
, 0);
337 c4outmeml(card
->mbase
+MAILBOX_0
, 0x55aa55aa);
338 if (c4inmeml(card
->mbase
+MAILBOX_0
) != 0x55aa55aa) return 3;
340 c4outmeml(card
->mbase
+MAILBOX_0
, 0xaa55aa55);
341 if (c4inmeml(card
->mbase
+MAILBOX_0
) != 0xaa55aa55) return 4;
343 if (c4_poke(card
, DC21285_ARMCSR_BASE
+DBELL_SA_MASK
, 0)) return 5;
344 if (c4_poke(card
, DC21285_ARMCSR_BASE
+DBELL_PCI_MASK
, 0)) return 6;
345 if (c4_poke(card
, DC21285_ARMCSR_BASE
+SA_CONTROL
, SA_CTL_ALLRIGHT
))
347 if (c4_poke(card
, DC21285_ARMCSR_BASE
+XBUS_CYCLE
, INIT_XBUS_CYCLE
))
349 if (c4_poke(card
, DC21285_ARMCSR_BASE
+XBUS_STROBE
, INIT_XBUS_STROBE
))
351 if (c4_poke(card
, DC21285_ARMCSR_BASE
+DRAM_TIMING
, 0)) return 9;
355 if (c4_peek(card
, DC21285_DRAM_A0MR
, &dummy
)) return 10;
356 if (c4_peek(card
, DC21285_DRAM_A1MR
, &dummy
)) return 11;
357 if (c4_peek(card
, DC21285_DRAM_A2MR
, &dummy
)) return 12;
358 if (c4_peek(card
, DC21285_DRAM_A3MR
, &dummy
)) return 13;
360 if (c4_poke(card
, DC21285_DRAM_A0MR
+CAS_OFFSET
, 0)) return 14;
361 if (c4_poke(card
, DC21285_DRAM_A1MR
+CAS_OFFSET
, 0)) return 15;
362 if (c4_poke(card
, DC21285_DRAM_A2MR
+CAS_OFFSET
, 0)) return 16;
363 if (c4_poke(card
, DC21285_DRAM_A3MR
+CAS_OFFSET
, 0)) return 17;
367 if (c4_poke(card
, DC21285_ARMCSR_BASE
+DRAM_TIMING
, DRAM_TIMING_DEF
))
370 if (c4_poke(card
, DC21285_ARMCSR_BASE
+DRAM_ADDR_SIZE_0
,DRAM_AD_SZ_DEF0
))
372 if (c4_poke(card
, DC21285_ARMCSR_BASE
+DRAM_ADDR_SIZE_1
,DRAM_AD_SZ_NULL
))
374 if (c4_poke(card
, DC21285_ARMCSR_BASE
+DRAM_ADDR_SIZE_2
,DRAM_AD_SZ_NULL
))
376 if (c4_poke(card
, DC21285_ARMCSR_BASE
+DRAM_ADDR_SIZE_3
,DRAM_AD_SZ_NULL
))
379 /* Transputer test */
381 if ( c4_poke(card
, 0x000000, 0x11111111)
382 || c4_poke(card
, 0x400000, 0x22222222)
383 || c4_poke(card
, 0x800000, 0x33333333)
384 || c4_poke(card
, 0xC00000, 0x44444444))
387 if ( c4_peek(card
, 0x000000, &dummy
) || dummy
!= 0x11111111
388 || c4_peek(card
, 0x400000, &dummy
) || dummy
!= 0x22222222
389 || c4_peek(card
, 0x800000, &dummy
) || dummy
!= 0x33333333
390 || c4_peek(card
, 0xC00000, &dummy
) || dummy
!= 0x44444444)
393 if ( c4_poke(card
, 0x000000, 0x55555555)
394 || c4_poke(card
, 0x400000, 0x66666666)
395 || c4_poke(card
, 0x800000, 0x77777777)
396 || c4_poke(card
, 0xC00000, 0x88888888))
399 if ( c4_peek(card
, 0x000000, &dummy
) || dummy
!= 0x55555555
400 || c4_peek(card
, 0x400000, &dummy
) || dummy
!= 0x66666666
401 || c4_peek(card
, 0x800000, &dummy
) || dummy
!= 0x77777777
402 || c4_peek(card
, 0xC00000, &dummy
) || dummy
!= 0x88888888)
408 /* ------------------------------------------------------------- */
410 static void c4_dispatch_tx(avmcard
*card
)
412 avmcard_dmainfo
*dma
= card
->dma
;
420 if (card
->csr
& DBELL_DOWN_ARM
) { /* tx busy */
424 skb
= skb_dequeue(&dma
->send_queue
);
427 printk(KERN_DEBUG
"%s: tx underrun\n", card
->name
);
432 len
= CAPIMSG_LEN(skb
->data
);
435 cmd
= CAPIMSG_COMMAND(skb
->data
);
436 subcmd
= CAPIMSG_SUBCOMMAND(skb
->data
);
438 p
= dma
->sendbuf
.dmabuf
;
440 if (CAPICMD(cmd
, subcmd
) == CAPI_DATA_B3_REQ
) {
441 u16 dlen
= CAPIMSG_DATALEN(skb
->data
);
442 _put_byte(&p
, SEND_DATA_B3_REQ
);
443 _put_slice(&p
, skb
->data
, len
);
444 _put_slice(&p
, skb
->data
+ len
, dlen
);
446 _put_byte(&p
, SEND_MESSAGE
);
447 _put_slice(&p
, skb
->data
, len
);
449 txlen
= (u8
*)p
- (u8
*)dma
->sendbuf
.dmabuf
;
451 printk(KERN_DEBUG
"%s: tx put msg len=%d\n", card
->name
, txlen
);
455 #ifdef AVM_C4_POLLDEBUG
456 if (skb
->data
[2] == SEND_POLLACK
)
457 printk(KERN_INFO
"%s: ack to c4\n", card
->name
);
460 printk(KERN_DEBUG
"%s: tx put 0x%x len=%d\n",
461 card
->name
, skb
->data
[2], txlen
);
463 skb_copy_from_linear_data_offset(skb
, 2, dma
->sendbuf
.dmabuf
,
466 txlen
= (txlen
+ 3) & ~3;
468 c4outmeml(card
->mbase
+MBOX_DOWN_ADDR
, dma
->sendbuf
.dmaaddr
);
469 c4outmeml(card
->mbase
+MBOX_DOWN_LEN
, txlen
);
471 card
->csr
|= DBELL_DOWN_ARM
;
473 c4outmeml(card
->mbase
+DOORBELL
, DBELL_DOWN_ARM
);
475 dev_kfree_skb_any(skb
);
478 /* ------------------------------------------------------------- */
480 static void queue_pollack(avmcard
*card
)
485 skb
= alloc_skb(3, GFP_ATOMIC
);
487 printk(KERN_CRIT
"%s: no memory, lost poll ack\n",
494 _put_byte(&p
, SEND_POLLACK
);
495 skb_put(skb
, (u8
*)p
- (u8
*)skb
->data
);
497 skb_queue_tail(&card
->dma
->send_queue
, skb
);
498 c4_dispatch_tx(card
);
501 /* ------------------------------------------------------------- */
503 static void c4_handle_rx(avmcard
*card
)
505 avmcard_dmainfo
*dma
= card
->dma
;
506 struct capi_ctr
*ctrl
;
509 void *p
= dma
->recvbuf
.dmabuf
;
510 u32 ApplId
, MsgLen
, DataB3Len
, NCCI
, WindowSize
;
511 u8 b1cmd
= _get_byte(&p
);
516 printk(KERN_DEBUG
"%s: rx 0x%x len=%lu\n", card
->name
,
517 b1cmd
, (unsigned long)dma
->recvlen
);
521 case RECEIVE_DATA_B3_IND
:
523 ApplId
= (unsigned) _get_word(&p
);
524 MsgLen
= _get_slice(&p
, card
->msgbuf
);
525 DataB3Len
= _get_slice(&p
, card
->databuf
);
526 cidx
= CAPIMSG_CONTROLLER(card
->msgbuf
)-card
->cardnr
;
527 if (cidx
>= card
->nlogcontr
) cidx
= 0;
528 ctrl
= &card
->ctrlinfo
[cidx
].capi_ctrl
;
530 if (MsgLen
< 30) { /* not CAPI 64Bit */
531 memset(card
->msgbuf
+MsgLen
, 0, 30-MsgLen
);
533 CAPIMSG_SETLEN(card
->msgbuf
, 30);
535 if (!(skb
= alloc_skb(DataB3Len
+MsgLen
, GFP_ATOMIC
))) {
536 printk(KERN_ERR
"%s: incoming packet dropped\n",
539 memcpy(skb_put(skb
, MsgLen
), card
->msgbuf
, MsgLen
);
540 memcpy(skb_put(skb
, DataB3Len
), card
->databuf
, DataB3Len
);
541 capi_ctr_handle_message(ctrl
, ApplId
, skb
);
545 case RECEIVE_MESSAGE
:
547 ApplId
= (unsigned) _get_word(&p
);
548 MsgLen
= _get_slice(&p
, card
->msgbuf
);
549 cidx
= CAPIMSG_CONTROLLER(card
->msgbuf
)-card
->cardnr
;
550 if (cidx
>= card
->nlogcontr
) cidx
= 0;
551 cinfo
= &card
->ctrlinfo
[cidx
];
552 ctrl
= &card
->ctrlinfo
[cidx
].capi_ctrl
;
554 if (!(skb
= alloc_skb(MsgLen
, GFP_ATOMIC
))) {
555 printk(KERN_ERR
"%s: incoming packet dropped\n",
558 memcpy(skb_put(skb
, MsgLen
), card
->msgbuf
, MsgLen
);
559 if (CAPIMSG_CMD(skb
->data
) == CAPI_DATA_B3_CONF
)
560 capilib_data_b3_conf(&cinfo
->ncci_head
, ApplId
,
561 CAPIMSG_NCCI(skb
->data
),
562 CAPIMSG_MSGID(skb
->data
));
564 capi_ctr_handle_message(ctrl
, ApplId
, skb
);
568 case RECEIVE_NEW_NCCI
:
570 ApplId
= _get_word(&p
);
571 NCCI
= _get_word(&p
);
572 WindowSize
= _get_word(&p
);
573 cidx
= (NCCI
&0x7f) - card
->cardnr
;
574 if (cidx
>= card
->nlogcontr
) cidx
= 0;
576 capilib_new_ncci(&card
->ctrlinfo
[cidx
].ncci_head
, ApplId
, NCCI
, WindowSize
);
580 case RECEIVE_FREE_NCCI
:
582 ApplId
= _get_word(&p
);
583 NCCI
= _get_word(&p
);
585 if (NCCI
!= 0xffffffff) {
586 cidx
= (NCCI
&0x7f) - card
->cardnr
;
587 if (cidx
>= card
->nlogcontr
) cidx
= 0;
588 capilib_free_ncci(&card
->ctrlinfo
[cidx
].ncci_head
, ApplId
, NCCI
);
593 #ifdef AVM_C4_POLLDEBUG
594 printk(KERN_INFO
"%s: poll from c4\n", card
->name
);
596 if (!suppress_pollack
)
598 for (cidx
=0; cidx
< card
->nr_controllers
; cidx
++) {
599 ctrl
= &card
->ctrlinfo
[cidx
].capi_ctrl
;
600 capi_ctr_resume_output(ctrl
);
605 for (cidx
=0; cidx
< card
->nr_controllers
; cidx
++) {
606 ctrl
= &card
->ctrlinfo
[cidx
].capi_ctrl
;
607 capi_ctr_suspend_output(ctrl
);
613 cidx
= card
->nlogcontr
;
614 if (cidx
>= card
->nr_controllers
) {
615 printk(KERN_ERR
"%s: card with %d controllers ??\n",
620 cinfo
= &card
->ctrlinfo
[cidx
];
621 ctrl
= &cinfo
->capi_ctrl
;
622 cinfo
->versionlen
= _get_slice(&p
, cinfo
->versionbuf
);
623 b1_parse_version(cinfo
);
624 printk(KERN_INFO
"%s: %s-card (%s) now active\n",
626 cinfo
->version
[VER_CARDTYPE
],
627 cinfo
->version
[VER_DRIVER
]);
628 capi_ctr_ready(&cinfo
->capi_ctrl
);
631 case RECEIVE_TASK_READY
:
632 ApplId
= (unsigned) _get_word(&p
);
633 MsgLen
= _get_slice(&p
, card
->msgbuf
);
634 card
->msgbuf
[MsgLen
] = 0;
636 && ( card
->msgbuf
[MsgLen
-1] == '\n'
637 || card
->msgbuf
[MsgLen
-1] == '\r')) {
638 card
->msgbuf
[MsgLen
-1] = 0;
641 printk(KERN_INFO
"%s: task %d \"%s\" ready.\n",
642 card
->name
, ApplId
, card
->msgbuf
);
645 case RECEIVE_DEBUGMSG
:
646 MsgLen
= _get_slice(&p
, card
->msgbuf
);
647 card
->msgbuf
[MsgLen
] = 0;
649 && ( card
->msgbuf
[MsgLen
-1] == '\n'
650 || card
->msgbuf
[MsgLen
-1] == '\r')) {
651 card
->msgbuf
[MsgLen
-1] = 0;
654 printk(KERN_INFO
"%s: DEBUG: %s\n", card
->name
, card
->msgbuf
);
658 printk(KERN_ERR
"%s: c4_interrupt: 0x%x ???\n",
664 /* ------------------------------------------------------------- */
666 static irqreturn_t
c4_handle_interrupt(avmcard
*card
)
671 spin_lock_irqsave(&card
->lock
, flags
);
672 status
= c4inmeml(card
->mbase
+DOORBELL
);
674 if (status
& DBELL_RESET_HOST
) {
676 c4outmeml(card
->mbase
+PCI_OUT_INT_MASK
, 0x0c);
677 spin_unlock_irqrestore(&card
->lock
, flags
);
678 if (card
->nlogcontr
== 0)
680 printk(KERN_ERR
"%s: unexpected reset\n", card
->name
);
681 for (i
=0; i
< card
->nr_controllers
; i
++) {
682 avmctrl_info
*cinfo
= &card
->ctrlinfo
[i
];
683 memset(cinfo
->version
, 0, sizeof(cinfo
->version
));
684 spin_lock_irqsave(&card
->lock
, flags
);
685 capilib_release(&cinfo
->ncci_head
);
686 spin_unlock_irqrestore(&card
->lock
, flags
);
687 capi_ctr_down(&cinfo
->capi_ctrl
);
693 status
&= (DBELL_UP_HOST
| DBELL_DOWN_HOST
);
695 spin_unlock_irqrestore(&card
->lock
, flags
);
698 c4outmeml(card
->mbase
+DOORBELL
, status
);
700 if ((status
& DBELL_UP_HOST
) != 0) {
701 card
->dma
->recvlen
= c4inmeml(card
->mbase
+MBOX_UP_LEN
);
702 c4outmeml(card
->mbase
+MBOX_UP_LEN
, 0);
704 card
->dma
->recvlen
= 0;
705 c4outmeml(card
->mbase
+MBOX_UP_LEN
, card
->dma
->recvbuf
.size
);
706 c4outmeml(card
->mbase
+DOORBELL
, DBELL_UP_ARM
);
709 if ((status
& DBELL_DOWN_HOST
) != 0) {
710 card
->csr
&= ~DBELL_DOWN_ARM
;
711 c4_dispatch_tx(card
);
712 } else if (card
->csr
& DBELL_DOWN_HOST
) {
713 if (c4inmeml(card
->mbase
+MBOX_DOWN_LEN
) == 0) {
714 card
->csr
&= ~DBELL_DOWN_ARM
;
715 c4_dispatch_tx(card
);
718 spin_unlock_irqrestore(&card
->lock
, flags
);
722 static irqreturn_t
c4_interrupt(int interrupt
, void *devptr
)
724 avmcard
*card
= devptr
;
726 return c4_handle_interrupt(card
);
729 /* ------------------------------------------------------------- */
731 static void c4_send_init(avmcard
*card
)
737 skb
= alloc_skb(15, GFP_ATOMIC
);
739 printk(KERN_CRIT
"%s: no memory, lost register appl.\n",
746 _put_byte(&p
, SEND_INIT
);
747 _put_word(&p
, CAPI_MAXAPPL
);
748 _put_word(&p
, AVM_NCCI_PER_CHANNEL
*30);
749 _put_word(&p
, card
->cardnr
- 1);
750 skb_put(skb
, (u8
*)p
- (u8
*)skb
->data
);
752 skb_queue_tail(&card
->dma
->send_queue
, skb
);
753 spin_lock_irqsave(&card
->lock
, flags
);
754 c4_dispatch_tx(card
);
755 spin_unlock_irqrestore(&card
->lock
, flags
);
758 static int queue_sendconfigword(avmcard
*card
, u32 val
)
764 skb
= alloc_skb(3+4, GFP_ATOMIC
);
766 printk(KERN_CRIT
"%s: no memory, send config\n",
773 _put_byte(&p
, SEND_CONFIG
);
775 skb_put(skb
, (u8
*)p
- (u8
*)skb
->data
);
777 skb_queue_tail(&card
->dma
->send_queue
, skb
);
778 spin_lock_irqsave(&card
->lock
, flags
);
779 c4_dispatch_tx(card
);
780 spin_unlock_irqrestore(&card
->lock
, flags
);
784 static int queue_sendconfig(avmcard
*card
, char cval
[4])
790 skb
= alloc_skb(3+4, GFP_ATOMIC
);
792 printk(KERN_CRIT
"%s: no memory, send config\n",
799 _put_byte(&p
, SEND_CONFIG
);
800 _put_byte(&p
, cval
[0]);
801 _put_byte(&p
, cval
[1]);
802 _put_byte(&p
, cval
[2]);
803 _put_byte(&p
, cval
[3]);
804 skb_put(skb
, (u8
*)p
- (u8
*)skb
->data
);
806 skb_queue_tail(&card
->dma
->send_queue
, skb
);
808 spin_lock_irqsave(&card
->lock
, flags
);
809 c4_dispatch_tx(card
);
810 spin_unlock_irqrestore(&card
->lock
, flags
);
814 static int c4_send_config(avmcard
*card
, capiloaddatapart
* config
)
821 if ((retval
= queue_sendconfigword(card
, 1)) != 0)
823 if ((retval
= queue_sendconfigword(card
, config
->len
)) != 0)
828 while (left
>= sizeof(u32
)) {
830 if (copy_from_user(val
, dp
, sizeof(val
)))
833 memcpy(val
, dp
, sizeof(val
));
835 if ((retval
= queue_sendconfig(card
, val
)) != 0)
841 memset(val
, 0, sizeof(val
));
843 if (copy_from_user(&val
, dp
, left
))
846 memcpy(&val
, dp
, left
);
848 if ((retval
= queue_sendconfig(card
, val
)) != 0)
855 static int c4_load_firmware(struct capi_ctr
*ctrl
, capiloaddata
*data
)
857 avmctrl_info
*cinfo
= (avmctrl_info
*)(ctrl
->driverdata
);
858 avmcard
*card
= cinfo
->card
;
861 if ((retval
= c4_load_t4file(card
, &data
->firmware
))) {
862 printk(KERN_ERR
"%s: failed to load t4file!!\n",
869 c4outmeml(card
->mbase
+MBOX_UP_LEN
, 0);
870 c4outmeml(card
->mbase
+MBOX_DOWN_LEN
, 0);
871 c4outmeml(card
->mbase
+DOORBELL
, DBELL_INIT
);
873 c4outmeml(card
->mbase
+DOORBELL
,
874 DBELL_UP_HOST
| DBELL_DOWN_HOST
| DBELL_RESET_HOST
);
876 c4outmeml(card
->mbase
+PCI_OUT_INT_MASK
, 0x08);
878 card
->dma
->recvlen
= 0;
879 c4outmeml(card
->mbase
+MBOX_UP_ADDR
, card
->dma
->recvbuf
.dmaaddr
);
880 c4outmeml(card
->mbase
+MBOX_UP_LEN
, card
->dma
->recvbuf
.size
);
881 c4outmeml(card
->mbase
+DOORBELL
, DBELL_UP_ARM
);
883 if (data
->configuration
.len
> 0 && data
->configuration
.data
) {
884 retval
= c4_send_config(card
, &data
->configuration
);
886 printk(KERN_ERR
"%s: failed to set config!!\n",
899 static void c4_reset_ctr(struct capi_ctr
*ctrl
)
901 avmcard
*card
= ((avmctrl_info
*)(ctrl
->driverdata
))->card
;
906 spin_lock_irqsave(&card
->lock
, flags
);
910 spin_unlock_irqrestore(&card
->lock
, flags
);
912 for (i
=0; i
< card
->nr_controllers
; i
++) {
913 cinfo
= &card
->ctrlinfo
[i
];
914 memset(cinfo
->version
, 0, sizeof(cinfo
->version
));
915 capi_ctr_down(&cinfo
->capi_ctrl
);
920 static void c4_remove(struct pci_dev
*pdev
)
922 avmcard
*card
= pci_get_drvdata(pdev
);
931 for (i
=0; i
< card
->nr_controllers
; i
++) {
932 cinfo
= &card
->ctrlinfo
[i
];
933 detach_capi_ctr(&cinfo
->capi_ctrl
);
936 free_irq(card
->irq
, card
);
937 iounmap(card
->mbase
);
938 release_region(card
->port
, AVMB1_PORTLEN
);
939 avmcard_dma_free(card
->dma
);
940 pci_set_drvdata(pdev
, NULL
);
944 /* ------------------------------------------------------------- */
947 static void c4_register_appl(struct capi_ctr
*ctrl
,
949 capi_register_params
*rp
)
951 avmctrl_info
*cinfo
= (avmctrl_info
*)(ctrl
->driverdata
);
952 avmcard
*card
= cinfo
->card
;
954 int want
= rp
->level3cnt
;
959 if (ctrl
->cnr
== card
->cardnr
) {
961 if (want
> 0) nconn
= want
;
962 else nconn
= ctrl
->profile
.nbchannel
* 4 * -want
;
963 if (nconn
== 0) nconn
= ctrl
->profile
.nbchannel
* 4;
965 skb
= alloc_skb(23, GFP_ATOMIC
);
967 printk(KERN_CRIT
"%s: no memory, lost register appl.\n",
974 _put_byte(&p
, SEND_REGISTER
);
976 _put_word(&p
, 1024 * (nconn
+1));
977 _put_word(&p
, nconn
);
978 _put_word(&p
, rp
->datablkcnt
);
979 _put_word(&p
, rp
->datablklen
);
980 skb_put(skb
, (u8
*)p
- (u8
*)skb
->data
);
982 skb_queue_tail(&card
->dma
->send_queue
, skb
);
984 spin_lock_irqsave(&card
->lock
, flags
);
985 c4_dispatch_tx(card
);
986 spin_unlock_irqrestore(&card
->lock
, flags
);
990 /* ------------------------------------------------------------- */
992 static void c4_release_appl(struct capi_ctr
*ctrl
, u16 appl
)
994 avmctrl_info
*cinfo
= (avmctrl_info
*)(ctrl
->driverdata
);
995 avmcard
*card
= cinfo
->card
;
1000 spin_lock_irqsave(&card
->lock
, flags
);
1001 capilib_release_appl(&cinfo
->ncci_head
, appl
);
1002 spin_unlock_irqrestore(&card
->lock
, flags
);
1004 if (ctrl
->cnr
== card
->cardnr
) {
1005 skb
= alloc_skb(7, GFP_ATOMIC
);
1007 printk(KERN_CRIT
"%s: no memory, lost release appl.\n",
1014 _put_byte(&p
, SEND_RELEASE
);
1015 _put_word(&p
, appl
);
1017 skb_put(skb
, (u8
*)p
- (u8
*)skb
->data
);
1018 skb_queue_tail(&card
->dma
->send_queue
, skb
);
1019 spin_lock_irqsave(&card
->lock
, flags
);
1020 c4_dispatch_tx(card
);
1021 spin_unlock_irqrestore(&card
->lock
, flags
);
1025 /* ------------------------------------------------------------- */
1028 static u16
c4_send_message(struct capi_ctr
*ctrl
, struct sk_buff
*skb
)
1030 avmctrl_info
*cinfo
= (avmctrl_info
*)(ctrl
->driverdata
);
1031 avmcard
*card
= cinfo
->card
;
1032 u16 retval
= CAPI_NOERROR
;
1033 unsigned long flags
;
1035 spin_lock_irqsave(&card
->lock
, flags
);
1036 if (CAPIMSG_CMD(skb
->data
) == CAPI_DATA_B3_REQ
) {
1037 retval
= capilib_data_b3_req(&cinfo
->ncci_head
,
1038 CAPIMSG_APPID(skb
->data
),
1039 CAPIMSG_NCCI(skb
->data
),
1040 CAPIMSG_MSGID(skb
->data
));
1042 if (retval
== CAPI_NOERROR
) {
1043 skb_queue_tail(&card
->dma
->send_queue
, skb
);
1044 c4_dispatch_tx(card
);
1046 spin_unlock_irqrestore(&card
->lock
, flags
);
1050 /* ------------------------------------------------------------- */
1052 static char *c4_procinfo(struct capi_ctr
*ctrl
)
1054 avmctrl_info
*cinfo
= (avmctrl_info
*)(ctrl
->driverdata
);
1058 sprintf(cinfo
->infobuf
, "%s %s 0x%x %d 0x%lx",
1059 cinfo
->cardname
[0] ? cinfo
->cardname
: "-",
1060 cinfo
->version
[VER_DRIVER
] ? cinfo
->version
[VER_DRIVER
] : "-",
1061 cinfo
->card
? cinfo
->card
->port
: 0x0,
1062 cinfo
->card
? cinfo
->card
->irq
: 0,
1063 cinfo
->card
? cinfo
->card
->membase
: 0
1065 return cinfo
->infobuf
;
1068 static int c4_proc_show(struct seq_file
*m
, void *v
)
1070 struct capi_ctr
*ctrl
= m
->private;
1071 avmctrl_info
*cinfo
= (avmctrl_info
*)(ctrl
->driverdata
);
1072 avmcard
*card
= cinfo
->card
;
1076 seq_printf(m
, "%-16s %s\n", "name", card
->name
);
1077 seq_printf(m
, "%-16s 0x%x\n", "io", card
->port
);
1078 seq_printf(m
, "%-16s %d\n", "irq", card
->irq
);
1079 seq_printf(m
, "%-16s 0x%lx\n", "membase", card
->membase
);
1080 switch (card
->cardtype
) {
1081 case avm_b1isa
: s
= "B1 ISA"; break;
1082 case avm_b1pci
: s
= "B1 PCI"; break;
1083 case avm_b1pcmcia
: s
= "B1 PCMCIA"; break;
1084 case avm_m1
: s
= "M1"; break;
1085 case avm_m2
: s
= "M2"; break;
1086 case avm_t1isa
: s
= "T1 ISA (HEMA)"; break;
1087 case avm_t1pci
: s
= "T1 PCI"; break;
1088 case avm_c4
: s
= "C4"; break;
1089 case avm_c2
: s
= "C2"; break;
1090 default: s
= "???"; break;
1092 seq_printf(m
, "%-16s %s\n", "type", s
);
1093 if ((s
= cinfo
->version
[VER_DRIVER
]) != NULL
)
1094 seq_printf(m
, "%-16s %s\n", "ver_driver", s
);
1095 if ((s
= cinfo
->version
[VER_CARDTYPE
]) != NULL
)
1096 seq_printf(m
, "%-16s %s\n", "ver_cardtype", s
);
1097 if ((s
= cinfo
->version
[VER_SERIAL
]) != NULL
)
1098 seq_printf(m
, "%-16s %s\n", "ver_serial", s
);
1100 if (card
->cardtype
!= avm_m1
) {
1101 flag
= ((u8
*)(ctrl
->profile
.manu
))[3];
1103 seq_printf(m
, "%-16s%s%s%s%s%s%s%s\n",
1105 (flag
& 0x01) ? " DSS1" : "",
1106 (flag
& 0x02) ? " CT1" : "",
1107 (flag
& 0x04) ? " VN3" : "",
1108 (flag
& 0x08) ? " NI1" : "",
1109 (flag
& 0x10) ? " AUSTEL" : "",
1110 (flag
& 0x20) ? " ESS" : "",
1111 (flag
& 0x40) ? " 1TR6" : ""
1114 if (card
->cardtype
!= avm_m1
) {
1115 flag
= ((u8
*)(ctrl
->profile
.manu
))[5];
1117 seq_printf(m
, "%-16s%s%s%s%s\n",
1119 (flag
& 0x01) ? " point to point" : "",
1120 (flag
& 0x02) ? " point to multipoint" : "",
1121 (flag
& 0x08) ? " leased line without D-channel" : "",
1122 (flag
& 0x04) ? " leased line with D-channel" : ""
1125 seq_printf(m
, "%-16s %s\n", "cardname", cinfo
->cardname
);
1130 static int c4_proc_open(struct inode
*inode
, struct file
*file
)
1132 return single_open(file
, c4_proc_show
, PDE(inode
)->data
);
1135 static const struct file_operations c4_proc_fops
= {
1136 .owner
= THIS_MODULE
,
1137 .open
= c4_proc_open
,
1139 .llseek
= seq_lseek
,
1140 .release
= single_release
,
1143 /* ------------------------------------------------------------- */
1145 static int c4_add_card(struct capicardparams
*p
, struct pci_dev
*dev
,
1149 avmctrl_info
*cinfo
;
1153 card
= b1_alloc_card(nr_controllers
);
1155 printk(KERN_WARNING
"c4: no memory.\n");
1159 card
->dma
= avmcard_dma_alloc("c4", dev
, 2048+128, 2048+128);
1161 printk(KERN_WARNING
"c4: no memory.\n");
1166 sprintf(card
->name
, "c%d-%x", nr_controllers
, p
->port
);
1167 card
->port
= p
->port
;
1169 card
->membase
= p
->membase
;
1170 card
->cardtype
= (nr_controllers
== 4) ? avm_c4
: avm_c2
;
1172 if (!request_region(card
->port
, AVMB1_PORTLEN
, card
->name
)) {
1173 printk(KERN_WARNING
"c4: ports 0x%03x-0x%03x in use.\n",
1174 card
->port
, card
->port
+ AVMB1_PORTLEN
);
1179 card
->mbase
= ioremap(card
->membase
, 128);
1180 if (card
->mbase
== NULL
) {
1181 printk(KERN_NOTICE
"c4: can't remap memory at 0x%lx\n",
1184 goto err_release_region
;
1187 retval
= c4_detect(card
);
1189 printk(KERN_NOTICE
"c4: NO card at 0x%x error(%d)\n",
1190 card
->port
, retval
);
1196 retval
= request_irq(card
->irq
, c4_interrupt
, IRQF_SHARED
, card
->name
, card
);
1198 printk(KERN_ERR
"c4: unable to get IRQ %d.\n",card
->irq
);
1203 for (i
=0; i
< nr_controllers
; i
++) {
1204 cinfo
= &card
->ctrlinfo
[i
];
1205 cinfo
->capi_ctrl
.owner
= THIS_MODULE
;
1206 cinfo
->capi_ctrl
.driver_name
= "c4";
1207 cinfo
->capi_ctrl
.driverdata
= cinfo
;
1208 cinfo
->capi_ctrl
.register_appl
= c4_register_appl
;
1209 cinfo
->capi_ctrl
.release_appl
= c4_release_appl
;
1210 cinfo
->capi_ctrl
.send_message
= c4_send_message
;
1211 cinfo
->capi_ctrl
.load_firmware
= c4_load_firmware
;
1212 cinfo
->capi_ctrl
.reset_ctr
= c4_reset_ctr
;
1213 cinfo
->capi_ctrl
.procinfo
= c4_procinfo
;
1214 cinfo
->capi_ctrl
.proc_fops
= &c4_proc_fops
;
1215 strcpy(cinfo
->capi_ctrl
.name
, card
->name
);
1217 retval
= attach_capi_ctr(&cinfo
->capi_ctrl
);
1219 printk(KERN_ERR
"c4: attach controller failed (%d).\n", i
);
1220 for (i
--; i
>= 0; i
--) {
1221 cinfo
= &card
->ctrlinfo
[i
];
1222 detach_capi_ctr(&cinfo
->capi_ctrl
);
1227 card
->cardnr
= cinfo
->capi_ctrl
.cnr
;
1230 printk(KERN_INFO
"c4: AVM C%d at i/o %#x, irq %d, mem %#lx\n",
1231 nr_controllers
, card
->port
, card
->irq
,
1233 pci_set_drvdata(dev
, card
);
1237 free_irq(card
->irq
, card
);
1239 iounmap(card
->mbase
);
1241 release_region(card
->port
, AVMB1_PORTLEN
);
1243 avmcard_dma_free(card
->dma
);
1250 /* ------------------------------------------------------------- */
1252 static int __devinit
c4_probe(struct pci_dev
*dev
,
1253 const struct pci_device_id
*ent
)
1255 int nr
= ent
->driver_data
;
1257 struct capicardparams param
;
1259 if (pci_enable_device(dev
) < 0) {
1260 printk(KERN_ERR
"c4: failed to enable AVM-C%d\n", nr
);
1263 pci_set_master(dev
);
1265 param
.port
= pci_resource_start(dev
, 1);
1266 param
.irq
= dev
->irq
;
1267 param
.membase
= pci_resource_start(dev
, 0);
1269 printk(KERN_INFO
"c4: PCI BIOS reports AVM-C%d at i/o %#x, irq %d, mem %#x\n",
1270 nr
, param
.port
, param
.irq
, param
.membase
);
1272 retval
= c4_add_card(¶m
, dev
, nr
);
1274 printk(KERN_ERR
"c4: no AVM-C%d at i/o %#x, irq %d detected, mem %#x\n",
1275 nr
, param
.port
, param
.irq
, param
.membase
);
1276 pci_disable_device(dev
);
1282 static struct pci_driver c4_pci_driver
= {
1284 .id_table
= c4_pci_tbl
,
1286 .remove
= c4_remove
,
1289 static struct capi_driver capi_driver_c2
= {
1294 static struct capi_driver capi_driver_c4
= {
1299 static int __init
c4_init(void)
1305 if ((p
= strchr(revision
, ':')) != NULL
&& p
[1]) {
1306 strlcpy(rev
, p
+ 2, 32);
1307 if ((p
= strchr(rev
, '$')) != NULL
&& p
> rev
)
1312 err
= pci_register_driver(&c4_pci_driver
);
1314 strlcpy(capi_driver_c2
.revision
, rev
, 32);
1315 register_capi_driver(&capi_driver_c2
);
1316 strlcpy(capi_driver_c4
.revision
, rev
, 32);
1317 register_capi_driver(&capi_driver_c4
);
1318 printk(KERN_INFO
"c4: revision %s\n", rev
);
1323 static void __exit
c4_exit(void)
1325 unregister_capi_driver(&capi_driver_c2
);
1326 unregister_capi_driver(&capi_driver_c4
);
1327 pci_unregister_driver(&c4_pci_driver
);
1330 module_init(c4_init
);
1331 module_exit(c4_exit
);