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/skbuff.h>
15 #include <linux/delay.h>
17 #include <linux/interrupt.h>
18 #include <linux/ioport.h>
19 #include <linux/pci.h>
20 #include <linux/capi.h>
21 #include <linux/kernelcapi.h>
22 #include <linux/init.h>
24 #include <asm/uaccess.h>
25 #include <linux/netdevice.h>
26 #include <linux/isdn/capicmd.h>
27 #include <linux/isdn/capiutil.h>
28 #include <linux/isdn/capilli.h>
32 #undef AVM_C4_POLLDEBUG
34 /* ------------------------------------------------------------- */
36 static char *revision = "$Revision: 1.1.2.2 $";
38 /* ------------------------------------------------------------- */
40 static int suppress_pollack;
42 static struct pci_device_id c4_pci_tbl[] = {
43 { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21285, PCI_VENDOR_ID_AVM, PCI_DEVICE_ID_AVM_C4, 0, 0, (unsigned long)4 },
44 { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21285, PCI_VENDOR_ID_AVM, PCI_DEVICE_ID_AVM_C2, 0, 0, (unsigned long)2 },
45 { } /* Terminating entry */
48 MODULE_DEVICE_TABLE(pci, c4_pci_tbl);
49 MODULE_DESCRIPTION("CAPI4Linux: Driver for AVM C2/C4 cards");
50 MODULE_AUTHOR("Carsten Paeth");
51 MODULE_LICENSE("GPL");
52 module_param(suppress_pollack, bool, 0);
54 /* ------------------------------------------------------------- */
56 static void c4_dispatch_tx(avmcard *card);
58 /* ------------------------------------------------------------- */
60 #define DC21285_DRAM_A0MR 0x40000000
61 #define DC21285_DRAM_A1MR 0x40004000
62 #define DC21285_DRAM_A2MR 0x40008000
63 #define DC21285_DRAM_A3MR 0x4000C000
65 #define CAS_OFFSET 0x88
67 #define DC21285_ARMCSR_BASE 0x42000000
69 #define PCI_OUT_INT_STATUS 0x30
70 #define PCI_OUT_INT_MASK 0x34
71 #define MAILBOX_0 0x50
72 #define MAILBOX_1 0x54
73 #define MAILBOX_2 0x58
74 #define MAILBOX_3 0x5C
76 #define DOORBELL_SETUP 0x64
78 #define CHAN_1_CONTROL 0x90
79 #define CHAN_2_CONTROL 0xB0
80 #define DRAM_TIMING 0x10C
81 #define DRAM_ADDR_SIZE_0 0x110
82 #define DRAM_ADDR_SIZE_1 0x114
83 #define DRAM_ADDR_SIZE_2 0x118
84 #define DRAM_ADDR_SIZE_3 0x11C
85 #define SA_CONTROL 0x13C
86 #define XBUS_CYCLE 0x148
87 #define XBUS_STROBE 0x14C
88 #define DBELL_PCI_MASK 0x150
89 #define DBELL_SA_MASK 0x154
91 #define SDRAM_SIZE 0x1000000
93 /* ------------------------------------------------------------- */
95 #define MBOX_PEEK_POKE MAILBOX_0
97 #define DBELL_ADDR 0x01
98 #define DBELL_DATA 0x02
99 #define DBELL_RNWR 0x40
100 #define DBELL_INIT 0x80
102 /* ------------------------------------------------------------- */
104 #define MBOX_UP_ADDR MAILBOX_0
105 #define MBOX_UP_LEN MAILBOX_1
106 #define MBOX_DOWN_ADDR MAILBOX_2
107 #define MBOX_DOWN_LEN MAILBOX_3
109 #define DBELL_UP_HOST 0x00000100
110 #define DBELL_UP_ARM 0x00000200
111 #define DBELL_DOWN_HOST 0x00000400
112 #define DBELL_DOWN_ARM 0x00000800
113 #define DBELL_RESET_HOST 0x40000000
114 #define DBELL_RESET_ARM 0x80000000
116 /* ------------------------------------------------------------- */
118 #define DRAM_TIMING_DEF 0x001A01A5
119 #define DRAM_AD_SZ_DEF0 0x00000045
120 #define DRAM_AD_SZ_NULL 0x00000000
122 #define SA_CTL_ALLRIGHT 0x64AA0271
124 #define INIT_XBUS_CYCLE 0x100016DB
125 #define INIT_XBUS_STROBE 0xF1F1F1F1
127 /* ------------------------------------------------------------- */
129 #define RESET_TIMEOUT (15*HZ) /* 15 sec */
130 #define PEEK_POKE_TIMEOUT (HZ/10) /* 0.1 sec */
132 /* ------------------------------------------------------------- */
134 #define c4outmeml(addr, value) writel(value, addr)
135 #define c4inmeml(addr) readl(addr)
136 #define c4outmemw(addr, value) writew(value, addr)
137 #define c4inmemw(addr) readw(addr)
138 #define c4outmemb(addr, value) writeb(value, addr)
139 #define c4inmemb(addr) readb(addr)
141 /* ------------------------------------------------------------- */
143 static inline int wait_for_doorbell(avmcard *card, unsigned long t)
148 while (c4inmeml(card->mbase+DOORBELL) != 0xffffffff) {
149 if (!time_before(jiffies, stop))
156 static int c4_poke(avmcard *card, unsigned long off, unsigned long value)
159 if (wait_for_doorbell(card, HZ/10) < 0)
162 c4outmeml(card->mbase+MBOX_PEEK_POKE, off);
163 c4outmeml(card->mbase+DOORBELL, DBELL_ADDR);
165 if (wait_for_doorbell(card, HZ/10) < 0)
168 c4outmeml(card->mbase+MBOX_PEEK_POKE, value);
169 c4outmeml(card->mbase+DOORBELL, DBELL_DATA | DBELL_ADDR);
174 static int c4_peek(avmcard *card, unsigned long off, unsigned long *valuep)
176 if (wait_for_doorbell(card, HZ/10) < 0)
179 c4outmeml(card->mbase+MBOX_PEEK_POKE, off);
180 c4outmeml(card->mbase+DOORBELL, DBELL_RNWR | DBELL_ADDR);
182 if (wait_for_doorbell(card, HZ/10) < 0)
185 *valuep = c4inmeml(card->mbase+MBOX_PEEK_POKE);
190 /* ------------------------------------------------------------- */
192 static int c4_load_t4file(avmcard *card, capiloaddatapart * t4file)
201 while (left >= sizeof(u32)) {
203 if (copy_from_user(&val, dp, sizeof(val)))
206 memcpy(&val, dp, sizeof(val));
208 if (c4_poke(card, loadoff, val)) {
209 printk(KERN_ERR "%s: corrupted firmware file ?\n",
215 loadoff += sizeof(u32);
220 if (copy_from_user(&val, dp, left))
223 memcpy(&val, dp, left);
225 if (c4_poke(card, loadoff, val)) {
226 printk(KERN_ERR "%s: corrupted firmware file ?\n",
234 /* ------------------------------------------------------------- */
236 static inline void _put_byte(void **pp, u8 val)
243 static inline void _put_word(void **pp, u32 val)
247 *s++ = (val >> 8) & 0xff;
248 *s++ = (val >> 16) & 0xff;
249 *s++ = (val >> 24) & 0xff;
253 static inline void _put_slice(void **pp, unsigned char *dp, unsigned int len)
258 _put_byte(pp, *dp++);
261 static inline u8 _get_byte(void **pp)
270 static inline u32 _get_word(void **pp)
282 static inline u32 _get_slice(void **pp, unsigned char *dp)
286 len = i = _get_word(pp);
287 while (i-- > 0) *dp++ = _get_byte(pp);
291 /* ------------------------------------------------------------- */
293 static void c4_reset(avmcard *card)
297 c4outmeml(card->mbase+DOORBELL, DBELL_RESET_ARM);
299 stop = jiffies + HZ*10;
300 while (c4inmeml(card->mbase+DOORBELL) != 0xffffffff) {
301 if (!time_before(jiffies, stop))
303 c4outmeml(card->mbase+DOORBELL, DBELL_ADDR);
307 c4_poke(card, DC21285_ARMCSR_BASE + CHAN_1_CONTROL, 0);
308 c4_poke(card, DC21285_ARMCSR_BASE + CHAN_2_CONTROL, 0);
311 /* ------------------------------------------------------------- */
313 static int c4_detect(avmcard *card)
315 unsigned long stop, dummy;
317 c4outmeml(card->mbase+PCI_OUT_INT_MASK, 0x0c);
318 if (c4inmeml(card->mbase+PCI_OUT_INT_MASK) != 0x0c)
321 c4outmeml(card->mbase+DOORBELL, DBELL_RESET_ARM);
323 stop = jiffies + HZ*10;
324 while (c4inmeml(card->mbase+DOORBELL) != 0xffffffff) {
325 if (!time_before(jiffies, stop))
327 c4outmeml(card->mbase+DOORBELL, DBELL_ADDR);
331 c4_poke(card, DC21285_ARMCSR_BASE + CHAN_1_CONTROL, 0);
332 c4_poke(card, DC21285_ARMCSR_BASE + CHAN_2_CONTROL, 0);
334 c4outmeml(card->mbase+MAILBOX_0, 0x55aa55aa);
335 if (c4inmeml(card->mbase+MAILBOX_0) != 0x55aa55aa) return 3;
337 c4outmeml(card->mbase+MAILBOX_0, 0xaa55aa55);
338 if (c4inmeml(card->mbase+MAILBOX_0) != 0xaa55aa55) return 4;
340 if (c4_poke(card, DC21285_ARMCSR_BASE+DBELL_SA_MASK, 0)) return 5;
341 if (c4_poke(card, DC21285_ARMCSR_BASE+DBELL_PCI_MASK, 0)) return 6;
342 if (c4_poke(card, DC21285_ARMCSR_BASE+SA_CONTROL, SA_CTL_ALLRIGHT))
344 if (c4_poke(card, DC21285_ARMCSR_BASE+XBUS_CYCLE, INIT_XBUS_CYCLE))
346 if (c4_poke(card, DC21285_ARMCSR_BASE+XBUS_STROBE, INIT_XBUS_STROBE))
348 if (c4_poke(card, DC21285_ARMCSR_BASE+DRAM_TIMING, 0)) return 9;
352 if (c4_peek(card, DC21285_DRAM_A0MR, &dummy)) return 10;
353 if (c4_peek(card, DC21285_DRAM_A1MR, &dummy)) return 11;
354 if (c4_peek(card, DC21285_DRAM_A2MR, &dummy)) return 12;
355 if (c4_peek(card, DC21285_DRAM_A3MR, &dummy)) return 13;
357 if (c4_poke(card, DC21285_DRAM_A0MR+CAS_OFFSET, 0)) return 14;
358 if (c4_poke(card, DC21285_DRAM_A1MR+CAS_OFFSET, 0)) return 15;
359 if (c4_poke(card, DC21285_DRAM_A2MR+CAS_OFFSET, 0)) return 16;
360 if (c4_poke(card, DC21285_DRAM_A3MR+CAS_OFFSET, 0)) return 17;
364 if (c4_poke(card, DC21285_ARMCSR_BASE+DRAM_TIMING, DRAM_TIMING_DEF))
367 if (c4_poke(card, DC21285_ARMCSR_BASE+DRAM_ADDR_SIZE_0,DRAM_AD_SZ_DEF0))
369 if (c4_poke(card, DC21285_ARMCSR_BASE+DRAM_ADDR_SIZE_1,DRAM_AD_SZ_NULL))
371 if (c4_poke(card, DC21285_ARMCSR_BASE+DRAM_ADDR_SIZE_2,DRAM_AD_SZ_NULL))
373 if (c4_poke(card, DC21285_ARMCSR_BASE+DRAM_ADDR_SIZE_3,DRAM_AD_SZ_NULL))
376 /* Transputer test */
378 if ( c4_poke(card, 0x000000, 0x11111111)
379 || c4_poke(card, 0x400000, 0x22222222)
380 || c4_poke(card, 0x800000, 0x33333333)
381 || c4_poke(card, 0xC00000, 0x44444444))
384 if ( c4_peek(card, 0x000000, &dummy) || dummy != 0x11111111
385 || c4_peek(card, 0x400000, &dummy) || dummy != 0x22222222
386 || c4_peek(card, 0x800000, &dummy) || dummy != 0x33333333
387 || c4_peek(card, 0xC00000, &dummy) || dummy != 0x44444444)
390 if ( c4_poke(card, 0x000000, 0x55555555)
391 || c4_poke(card, 0x400000, 0x66666666)
392 || c4_poke(card, 0x800000, 0x77777777)
393 || c4_poke(card, 0xC00000, 0x88888888))
396 if ( c4_peek(card, 0x000000, &dummy) || dummy != 0x55555555
397 || c4_peek(card, 0x400000, &dummy) || dummy != 0x66666666
398 || c4_peek(card, 0x800000, &dummy) || dummy != 0x77777777
399 || c4_peek(card, 0xC00000, &dummy) || dummy != 0x88888888)
405 /* ------------------------------------------------------------- */
407 static void c4_dispatch_tx(avmcard *card)
409 avmcard_dmainfo *dma = card->dma;
417 if (card->csr & DBELL_DOWN_ARM) { /* tx busy */
421 skb = skb_dequeue(&dma->send_queue);
424 printk(KERN_DEBUG "%s: tx underrun\n", card->name);
429 len = CAPIMSG_LEN(skb->data);
432 cmd = CAPIMSG_COMMAND(skb->data);
433 subcmd = CAPIMSG_SUBCOMMAND(skb->data);
435 p = dma->sendbuf.dmabuf;
437 if (CAPICMD(cmd, subcmd) == CAPI_DATA_B3_REQ) {
438 u16 dlen = CAPIMSG_DATALEN(skb->data);
439 _put_byte(&p, SEND_DATA_B3_REQ);
440 _put_slice(&p, skb->data, len);
441 _put_slice(&p, skb->data + len, dlen);
443 _put_byte(&p, SEND_MESSAGE);
444 _put_slice(&p, skb->data, len);
446 txlen = (u8 *)p - (u8 *)dma->sendbuf.dmabuf;
448 printk(KERN_DEBUG "%s: tx put msg len=%d\n", card->name, txlen);
452 #ifdef AVM_C4_POLLDEBUG
453 if (skb->data[2] == SEND_POLLACK)
454 printk(KERN_INFO "%s: ack to c4\n", card->name);
457 printk(KERN_DEBUG "%s: tx put 0x%x len=%d\n",
458 card->name, skb->data[2], txlen);
460 skb_copy_from_linear_data_offset(skb, 2, dma->sendbuf.dmabuf,
463 txlen = (txlen + 3) & ~3;
465 c4outmeml(card->mbase+MBOX_DOWN_ADDR, dma->sendbuf.dmaaddr);
466 c4outmeml(card->mbase+MBOX_DOWN_LEN, txlen);
468 card->csr |= DBELL_DOWN_ARM;
470 c4outmeml(card->mbase+DOORBELL, DBELL_DOWN_ARM);
472 dev_kfree_skb_any(skb);
475 /* ------------------------------------------------------------- */
477 static void queue_pollack(avmcard *card)
482 skb = alloc_skb(3, GFP_ATOMIC);
484 printk(KERN_CRIT "%s: no memory, lost poll ack\n",
491 _put_byte(&p, SEND_POLLACK);
492 skb_put(skb, (u8 *)p - (u8 *)skb->data);
494 skb_queue_tail(&card->dma->send_queue, skb);
495 c4_dispatch_tx(card);
498 /* ------------------------------------------------------------- */
500 static void c4_handle_rx(avmcard *card)
502 avmcard_dmainfo *dma = card->dma;
503 struct capi_ctr *ctrl;
506 void *p = dma->recvbuf.dmabuf;
507 u32 ApplId, MsgLen, DataB3Len, NCCI, WindowSize;
508 u8 b1cmd = _get_byte(&p);
513 printk(KERN_DEBUG "%s: rx 0x%x len=%lu\n", card->name,
514 b1cmd, (unsigned long)dma->recvlen);
518 case RECEIVE_DATA_B3_IND:
520 ApplId = (unsigned) _get_word(&p);
521 MsgLen = _get_slice(&p, card->msgbuf);
522 DataB3Len = _get_slice(&p, card->databuf);
523 cidx = CAPIMSG_CONTROLLER(card->msgbuf)-card->cardnr;
524 if (cidx >= card->nlogcontr) cidx = 0;
525 ctrl = &card->ctrlinfo[cidx].capi_ctrl;
527 if (MsgLen < 30) { /* not CAPI 64Bit */
528 memset(card->msgbuf+MsgLen, 0, 30-MsgLen);
530 CAPIMSG_SETLEN(card->msgbuf, 30);
532 if (!(skb = alloc_skb(DataB3Len+MsgLen, GFP_ATOMIC))) {
533 printk(KERN_ERR "%s: incoming packet dropped\n",
536 memcpy(skb_put(skb, MsgLen), card->msgbuf, MsgLen);
537 memcpy(skb_put(skb, DataB3Len), card->databuf, DataB3Len);
538 capi_ctr_handle_message(ctrl, ApplId, skb);
542 case RECEIVE_MESSAGE:
544 ApplId = (unsigned) _get_word(&p);
545 MsgLen = _get_slice(&p, card->msgbuf);
546 cidx = CAPIMSG_CONTROLLER(card->msgbuf)-card->cardnr;
547 if (cidx >= card->nlogcontr) cidx = 0;
548 cinfo = &card->ctrlinfo[cidx];
549 ctrl = &card->ctrlinfo[cidx].capi_ctrl;
551 if (!(skb = alloc_skb(MsgLen, GFP_ATOMIC))) {
552 printk(KERN_ERR "%s: incoming packet dropped\n",
555 memcpy(skb_put(skb, MsgLen), card->msgbuf, MsgLen);
556 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_CONF)
557 capilib_data_b3_conf(&cinfo->ncci_head, ApplId,
558 CAPIMSG_NCCI(skb->data),
559 CAPIMSG_MSGID(skb->data));
561 capi_ctr_handle_message(ctrl, ApplId, skb);
565 case RECEIVE_NEW_NCCI:
567 ApplId = _get_word(&p);
568 NCCI = _get_word(&p);
569 WindowSize = _get_word(&p);
570 cidx = (NCCI&0x7f) - card->cardnr;
571 if (cidx >= card->nlogcontr) cidx = 0;
573 capilib_new_ncci(&card->ctrlinfo[cidx].ncci_head, ApplId, NCCI, WindowSize);
577 case RECEIVE_FREE_NCCI:
579 ApplId = _get_word(&p);
580 NCCI = _get_word(&p);
582 if (NCCI != 0xffffffff) {
583 cidx = (NCCI&0x7f) - card->cardnr;
584 if (cidx >= card->nlogcontr) cidx = 0;
585 capilib_free_ncci(&card->ctrlinfo[cidx].ncci_head, ApplId, NCCI);
590 #ifdef AVM_C4_POLLDEBUG
591 printk(KERN_INFO "%s: poll from c4\n", card->name);
593 if (!suppress_pollack)
595 for (cidx=0; cidx < card->nr_controllers; cidx++) {
596 ctrl = &card->ctrlinfo[cidx].capi_ctrl;
597 capi_ctr_resume_output(ctrl);
602 for (cidx=0; cidx < card->nr_controllers; cidx++) {
603 ctrl = &card->ctrlinfo[cidx].capi_ctrl;
604 capi_ctr_suspend_output(ctrl);
610 cidx = card->nlogcontr;
611 if (cidx >= card->nr_controllers) {
612 printk(KERN_ERR "%s: card with %d controllers ??\n",
617 cinfo = &card->ctrlinfo[cidx];
618 ctrl = &cinfo->capi_ctrl;
619 cinfo->versionlen = _get_slice(&p, cinfo->versionbuf);
620 b1_parse_version(cinfo);
621 printk(KERN_INFO "%s: %s-card (%s) now active\n",
623 cinfo->version[VER_CARDTYPE],
624 cinfo->version[VER_DRIVER]);
625 capi_ctr_ready(&cinfo->capi_ctrl);
628 case RECEIVE_TASK_READY:
629 ApplId = (unsigned) _get_word(&p);
630 MsgLen = _get_slice(&p, card->msgbuf);
631 card->msgbuf[MsgLen] = 0;
633 && ( card->msgbuf[MsgLen-1] == '\n'
634 || card->msgbuf[MsgLen-1] == '\r')) {
635 card->msgbuf[MsgLen-1] = 0;
638 printk(KERN_INFO "%s: task %d \"%s\" ready.\n",
639 card->name, ApplId, card->msgbuf);
642 case RECEIVE_DEBUGMSG:
643 MsgLen = _get_slice(&p, card->msgbuf);
644 card->msgbuf[MsgLen] = 0;
646 && ( card->msgbuf[MsgLen-1] == '\n'
647 || card->msgbuf[MsgLen-1] == '\r')) {
648 card->msgbuf[MsgLen-1] = 0;
651 printk(KERN_INFO "%s: DEBUG: %s\n", card->name, card->msgbuf);
655 printk(KERN_ERR "%s: c4_interrupt: 0x%x ???\n",
661 /* ------------------------------------------------------------- */
663 static irqreturn_t c4_handle_interrupt(avmcard *card)
668 spin_lock_irqsave(&card->lock, flags);
669 status = c4inmeml(card->mbase+DOORBELL);
671 if (status & DBELL_RESET_HOST) {
673 c4outmeml(card->mbase+PCI_OUT_INT_MASK, 0x0c);
674 spin_unlock_irqrestore(&card->lock, flags);
675 if (card->nlogcontr == 0)
677 printk(KERN_ERR "%s: unexpected reset\n", card->name);
678 for (i=0; i < card->nr_controllers; i++) {
679 avmctrl_info *cinfo = &card->ctrlinfo[i];
680 memset(cinfo->version, 0, sizeof(cinfo->version));
681 spin_lock_irqsave(&card->lock, flags);
682 capilib_release(&cinfo->ncci_head);
683 spin_unlock_irqrestore(&card->lock, flags);
684 capi_ctr_reseted(&cinfo->capi_ctrl);
690 status &= (DBELL_UP_HOST | DBELL_DOWN_HOST);
692 spin_unlock_irqrestore(&card->lock, flags);
695 c4outmeml(card->mbase+DOORBELL, status);
697 if ((status & DBELL_UP_HOST) != 0) {
698 card->dma->recvlen = c4inmeml(card->mbase+MBOX_UP_LEN);
699 c4outmeml(card->mbase+MBOX_UP_LEN, 0);
701 card->dma->recvlen = 0;
702 c4outmeml(card->mbase+MBOX_UP_LEN, card->dma->recvbuf.size);
703 c4outmeml(card->mbase+DOORBELL, DBELL_UP_ARM);
706 if ((status & DBELL_DOWN_HOST) != 0) {
707 card->csr &= ~DBELL_DOWN_ARM;
708 c4_dispatch_tx(card);
709 } else if (card->csr & DBELL_DOWN_HOST) {
710 if (c4inmeml(card->mbase+MBOX_DOWN_LEN) == 0) {
711 card->csr &= ~DBELL_DOWN_ARM;
712 c4_dispatch_tx(card);
715 spin_unlock_irqrestore(&card->lock, flags);
719 static irqreturn_t c4_interrupt(int interrupt, void *devptr)
721 avmcard *card = devptr;
723 return c4_handle_interrupt(card);
726 /* ------------------------------------------------------------- */
728 static void c4_send_init(avmcard *card)
734 skb = alloc_skb(15, GFP_ATOMIC);
736 printk(KERN_CRIT "%s: no memory, lost register appl.\n",
743 _put_byte(&p, SEND_INIT);
744 _put_word(&p, CAPI_MAXAPPL);
745 _put_word(&p, AVM_NCCI_PER_CHANNEL*30);
746 _put_word(&p, card->cardnr - 1);
747 skb_put(skb, (u8 *)p - (u8 *)skb->data);
749 skb_queue_tail(&card->dma->send_queue, skb);
750 spin_lock_irqsave(&card->lock, flags);
751 c4_dispatch_tx(card);
752 spin_unlock_irqrestore(&card->lock, flags);
755 static int queue_sendconfigword(avmcard *card, u32 val)
761 skb = alloc_skb(3+4, GFP_ATOMIC);
763 printk(KERN_CRIT "%s: no memory, send config\n",
770 _put_byte(&p, SEND_CONFIG);
772 skb_put(skb, (u8 *)p - (u8 *)skb->data);
774 skb_queue_tail(&card->dma->send_queue, skb);
775 spin_lock_irqsave(&card->lock, flags);
776 c4_dispatch_tx(card);
777 spin_unlock_irqrestore(&card->lock, flags);
781 static int queue_sendconfig(avmcard *card, char cval[4])
787 skb = alloc_skb(3+4, GFP_ATOMIC);
789 printk(KERN_CRIT "%s: no memory, send config\n",
796 _put_byte(&p, SEND_CONFIG);
797 _put_byte(&p, cval[0]);
798 _put_byte(&p, cval[1]);
799 _put_byte(&p, cval[2]);
800 _put_byte(&p, cval[3]);
801 skb_put(skb, (u8 *)p - (u8 *)skb->data);
803 skb_queue_tail(&card->dma->send_queue, skb);
805 spin_lock_irqsave(&card->lock, flags);
806 c4_dispatch_tx(card);
807 spin_unlock_irqrestore(&card->lock, flags);
811 static int c4_send_config(avmcard *card, capiloaddatapart * config)
818 if ((retval = queue_sendconfigword(card, 1)) != 0)
820 if ((retval = queue_sendconfigword(card, config->len)) != 0)
825 while (left >= sizeof(u32)) {
827 if (copy_from_user(val, dp, sizeof(val)))
830 memcpy(val, dp, sizeof(val));
832 if ((retval = queue_sendconfig(card, val)) != 0)
838 memset(val, 0, sizeof(val));
840 if (copy_from_user(&val, dp, left))
843 memcpy(&val, dp, left);
845 if ((retval = queue_sendconfig(card, val)) != 0)
852 static int c4_load_firmware(struct capi_ctr *ctrl, capiloaddata *data)
854 avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
855 avmcard *card = cinfo->card;
858 if ((retval = c4_load_t4file(card, &data->firmware))) {
859 printk(KERN_ERR "%s: failed to load t4file!!\n",
866 c4outmeml(card->mbase+MBOX_UP_LEN, 0);
867 c4outmeml(card->mbase+MBOX_DOWN_LEN, 0);
868 c4outmeml(card->mbase+DOORBELL, DBELL_INIT);
870 c4outmeml(card->mbase+DOORBELL,
871 DBELL_UP_HOST | DBELL_DOWN_HOST | DBELL_RESET_HOST);
873 c4outmeml(card->mbase+PCI_OUT_INT_MASK, 0x08);
875 card->dma->recvlen = 0;
876 c4outmeml(card->mbase+MBOX_UP_ADDR, card->dma->recvbuf.dmaaddr);
877 c4outmeml(card->mbase+MBOX_UP_LEN, card->dma->recvbuf.size);
878 c4outmeml(card->mbase+DOORBELL, DBELL_UP_ARM);
880 if (data->configuration.len > 0 && data->configuration.data) {
881 retval = c4_send_config(card, &data->configuration);
883 printk(KERN_ERR "%s: failed to set config!!\n",
896 static void c4_reset_ctr(struct capi_ctr *ctrl)
898 avmcard *card = ((avmctrl_info *)(ctrl->driverdata))->card;
903 spin_lock_irqsave(&card->lock, flags);
907 spin_unlock_irqrestore(&card->lock, flags);
909 for (i=0; i < card->nr_controllers; i++) {
910 cinfo = &card->ctrlinfo[i];
911 memset(cinfo->version, 0, sizeof(cinfo->version));
912 capi_ctr_reseted(&cinfo->capi_ctrl);
917 static void c4_remove(struct pci_dev *pdev)
919 avmcard *card = pci_get_drvdata(pdev);
928 for (i=0; i < card->nr_controllers; i++) {
929 cinfo = &card->ctrlinfo[i];
930 detach_capi_ctr(&cinfo->capi_ctrl);
933 free_irq(card->irq, card);
934 iounmap(card->mbase);
935 release_region(card->port, AVMB1_PORTLEN);
936 avmcard_dma_free(card->dma);
937 pci_set_drvdata(pdev, NULL);
941 /* ------------------------------------------------------------- */
944 static void c4_register_appl(struct capi_ctr *ctrl,
946 capi_register_params *rp)
948 avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
949 avmcard *card = cinfo->card;
951 int want = rp->level3cnt;
956 if (ctrl->cnr == card->cardnr) {
958 if (want > 0) nconn = want;
959 else nconn = ctrl->profile.nbchannel * 4 * -want;
960 if (nconn == 0) nconn = ctrl->profile.nbchannel * 4;
962 skb = alloc_skb(23, GFP_ATOMIC);
964 printk(KERN_CRIT "%s: no memory, lost register appl.\n",
971 _put_byte(&p, SEND_REGISTER);
973 _put_word(&p, 1024 * (nconn+1));
974 _put_word(&p, nconn);
975 _put_word(&p, rp->datablkcnt);
976 _put_word(&p, rp->datablklen);
977 skb_put(skb, (u8 *)p - (u8 *)skb->data);
979 skb_queue_tail(&card->dma->send_queue, skb);
981 spin_lock_irqsave(&card->lock, flags);
982 c4_dispatch_tx(card);
983 spin_unlock_irqrestore(&card->lock, flags);
987 /* ------------------------------------------------------------- */
989 static void c4_release_appl(struct capi_ctr *ctrl, u16 appl)
991 avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
992 avmcard *card = cinfo->card;
997 spin_lock_irqsave(&card->lock, flags);
998 capilib_release_appl(&cinfo->ncci_head, appl);
999 spin_unlock_irqrestore(&card->lock, flags);
1001 if (ctrl->cnr == card->cardnr) {
1002 skb = alloc_skb(7, GFP_ATOMIC);
1004 printk(KERN_CRIT "%s: no memory, lost release appl.\n",
1011 _put_byte(&p, SEND_RELEASE);
1012 _put_word(&p, appl);
1014 skb_put(skb, (u8 *)p - (u8 *)skb->data);
1015 skb_queue_tail(&card->dma->send_queue, skb);
1016 spin_lock_irqsave(&card->lock, flags);
1017 c4_dispatch_tx(card);
1018 spin_unlock_irqrestore(&card->lock, flags);
1022 /* ------------------------------------------------------------- */
1025 static u16 c4_send_message(struct capi_ctr *ctrl, struct sk_buff *skb)
1027 avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
1028 avmcard *card = cinfo->card;
1029 u16 retval = CAPI_NOERROR;
1030 unsigned long flags;
1032 spin_lock_irqsave(&card->lock, flags);
1033 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
1034 retval = capilib_data_b3_req(&cinfo->ncci_head,
1035 CAPIMSG_APPID(skb->data),
1036 CAPIMSG_NCCI(skb->data),
1037 CAPIMSG_MSGID(skb->data));
1039 if (retval == CAPI_NOERROR) {
1040 skb_queue_tail(&card->dma->send_queue, skb);
1041 c4_dispatch_tx(card);
1043 spin_unlock_irqrestore(&card->lock, flags);
1047 /* ------------------------------------------------------------- */
1049 static char *c4_procinfo(struct capi_ctr *ctrl)
1051 avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
1055 sprintf(cinfo->infobuf, "%s %s 0x%x %d 0x%lx",
1056 cinfo->cardname[0] ? cinfo->cardname : "-",
1057 cinfo->version[VER_DRIVER] ? cinfo->version[VER_DRIVER] : "-",
1058 cinfo->card ? cinfo->card->port : 0x0,
1059 cinfo->card ? cinfo->card->irq : 0,
1060 cinfo->card ? cinfo->card->membase : 0
1062 return cinfo->infobuf;
1065 static int c4_read_proc(char *page, char **start, off_t off,
1066 int count, int *eof, struct capi_ctr *ctrl)
1068 avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
1069 avmcard *card = cinfo->card;
1074 len += sprintf(page+len, "%-16s %s\n", "name", card->name);
1075 len += sprintf(page+len, "%-16s 0x%x\n", "io", card->port);
1076 len += sprintf(page+len, "%-16s %d\n", "irq", card->irq);
1077 len += sprintf(page+len, "%-16s 0x%lx\n", "membase", card->membase);
1078 switch (card->cardtype) {
1079 case avm_b1isa: s = "B1 ISA"; break;
1080 case avm_b1pci: s = "B1 PCI"; break;
1081 case avm_b1pcmcia: s = "B1 PCMCIA"; break;
1082 case avm_m1: s = "M1"; break;
1083 case avm_m2: s = "M2"; break;
1084 case avm_t1isa: s = "T1 ISA (HEMA)"; break;
1085 case avm_t1pci: s = "T1 PCI"; break;
1086 case avm_c4: s = "C4"; break;
1087 case avm_c2: s = "C2"; break;
1088 default: s = "???"; break;
1090 len += sprintf(page+len, "%-16s %s\n", "type", s);
1091 if ((s = cinfo->version[VER_DRIVER]) != NULL)
1092 len += sprintf(page+len, "%-16s %s\n", "ver_driver", s);
1093 if ((s = cinfo->version[VER_CARDTYPE]) != NULL)
1094 len += sprintf(page+len, "%-16s %s\n", "ver_cardtype", s);
1095 if ((s = cinfo->version[VER_SERIAL]) != NULL)
1096 len += sprintf(page+len, "%-16s %s\n", "ver_serial", s);
1098 if (card->cardtype != avm_m1) {
1099 flag = ((u8 *)(ctrl->profile.manu))[3];
1101 len += sprintf(page+len, "%-16s%s%s%s%s%s%s%s\n",
1103 (flag & 0x01) ? " DSS1" : "",
1104 (flag & 0x02) ? " CT1" : "",
1105 (flag & 0x04) ? " VN3" : "",
1106 (flag & 0x08) ? " NI1" : "",
1107 (flag & 0x10) ? " AUSTEL" : "",
1108 (flag & 0x20) ? " ESS" : "",
1109 (flag & 0x40) ? " 1TR6" : ""
1112 if (card->cardtype != avm_m1) {
1113 flag = ((u8 *)(ctrl->profile.manu))[5];
1115 len += sprintf(page+len, "%-16s%s%s%s%s\n",
1117 (flag & 0x01) ? " point to point" : "",
1118 (flag & 0x02) ? " point to multipoint" : "",
1119 (flag & 0x08) ? " leased line without D-channel" : "",
1120 (flag & 0x04) ? " leased line with D-channel" : ""
1123 len += sprintf(page+len, "%-16s %s\n", "cardname", cinfo->cardname);
1125 if (off+count >= len)
1129 *start = page + off;
1130 return ((count < len-off) ? count : len-off);
1133 /* ------------------------------------------------------------- */
1135 static int c4_add_card(struct capicardparams *p, struct pci_dev *dev,
1139 avmctrl_info *cinfo;
1143 card = b1_alloc_card(nr_controllers);
1145 printk(KERN_WARNING "c4: no memory.\n");
1149 card->dma = avmcard_dma_alloc("c4", dev, 2048+128, 2048+128);
1151 printk(KERN_WARNING "c4: no memory.\n");
1156 sprintf(card->name, "c%d-%x", nr_controllers, p->port);
1157 card->port = p->port;
1159 card->membase = p->membase;
1160 card->cardtype = (nr_controllers == 4) ? avm_c4 : avm_c2;
1162 if (!request_region(card->port, AVMB1_PORTLEN, card->name)) {
1163 printk(KERN_WARNING "c4: ports 0x%03x-0x%03x in use.\n",
1164 card->port, card->port + AVMB1_PORTLEN);
1169 card->mbase = ioremap(card->membase, 128);
1170 if (card->mbase == NULL) {
1171 printk(KERN_NOTICE "c4: can't remap memory at 0x%lx\n",
1174 goto err_release_region;
1177 retval = c4_detect(card);
1179 printk(KERN_NOTICE "c4: NO card at 0x%x error(%d)\n",
1180 card->port, retval);
1186 retval = request_irq(card->irq, c4_interrupt, IRQF_SHARED, card->name, card);
1188 printk(KERN_ERR "c4: unable to get IRQ %d.\n",card->irq);
1193 for (i=0; i < nr_controllers ; i++) {
1194 cinfo = &card->ctrlinfo[i];
1195 cinfo->capi_ctrl.owner = THIS_MODULE;
1196 cinfo->capi_ctrl.driver_name = "c4";
1197 cinfo->capi_ctrl.driverdata = cinfo;
1198 cinfo->capi_ctrl.register_appl = c4_register_appl;
1199 cinfo->capi_ctrl.release_appl = c4_release_appl;
1200 cinfo->capi_ctrl.send_message = c4_send_message;
1201 cinfo->capi_ctrl.load_firmware = c4_load_firmware;
1202 cinfo->capi_ctrl.reset_ctr = c4_reset_ctr;
1203 cinfo->capi_ctrl.procinfo = c4_procinfo;
1204 cinfo->capi_ctrl.ctr_read_proc = c4_read_proc;
1205 strcpy(cinfo->capi_ctrl.name, card->name);
1207 retval = attach_capi_ctr(&cinfo->capi_ctrl);
1209 printk(KERN_ERR "c4: attach controller failed (%d).\n", i);
1210 for (i--; i >= 0; i--) {
1211 cinfo = &card->ctrlinfo[i];
1212 detach_capi_ctr(&cinfo->capi_ctrl);
1217 card->cardnr = cinfo->capi_ctrl.cnr;
1220 printk(KERN_INFO "c4: AVM C%d at i/o %#x, irq %d, mem %#lx\n",
1221 nr_controllers, card->port, card->irq,
1223 pci_set_drvdata(dev, card);
1227 free_irq(card->irq, card);
1229 iounmap(card->mbase);
1231 release_region(card->port, AVMB1_PORTLEN);
1233 avmcard_dma_free(card->dma);
1240 /* ------------------------------------------------------------- */
1242 static int __devinit c4_probe(struct pci_dev *dev,
1243 const struct pci_device_id *ent)
1245 int nr = ent->driver_data;
1247 struct capicardparams param;
1249 if (pci_enable_device(dev) < 0) {
1250 printk(KERN_ERR "c4: failed to enable AVM-C%d\n", nr);
1253 pci_set_master(dev);
1255 param.port = pci_resource_start(dev, 1);
1256 param.irq = dev->irq;
1257 param.membase = pci_resource_start(dev, 0);
1259 printk(KERN_INFO "c4: PCI BIOS reports AVM-C%d at i/o %#x, irq %d, mem %#x\n",
1260 nr, param.port, param.irq, param.membase);
1262 retval = c4_add_card(¶m, dev, nr);
1264 printk(KERN_ERR "c4: no AVM-C%d at i/o %#x, irq %d detected, mem %#x\n",
1265 nr, param.port, param.irq, param.membase);
1271 static struct pci_driver c4_pci_driver = {
1273 .id_table = c4_pci_tbl,
1275 .remove = c4_remove,
1278 static struct capi_driver capi_driver_c2 = {
1283 static struct capi_driver capi_driver_c4 = {
1288 static int __init c4_init(void)
1294 if ((p = strchr(revision, ':')) != NULL && p[1]) {
1295 strlcpy(rev, p + 2, 32);
1296 if ((p = strchr(rev, '$')) != NULL && p > rev)
1301 err = pci_register_driver(&c4_pci_driver);
1303 strlcpy(capi_driver_c2.revision, rev, 32);
1304 register_capi_driver(&capi_driver_c2);
1305 strlcpy(capi_driver_c4.revision, rev, 32);
1306 register_capi_driver(&capi_driver_c4);
1307 printk(KERN_INFO "c4: revision %s\n", rev);
1312 static void __exit c4_exit(void)
1314 unregister_capi_driver(&capi_driver_c2);
1315 unregister_capi_driver(&capi_driver_c4);
1316 pci_unregister_driver(&c4_pci_driver);
1319 module_init(c4_init);
1320 module_exit(c4_exit);