1 /* $Id: b1.c,v 1.1.2.2 2004/01/16 21:09:27 keil Exp $
3 * Common module for AVM B1 cards.
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/pci.h>
15 #include <linux/skbuff.h>
16 #include <linux/delay.h>
18 #include <linux/interrupt.h>
19 #include <linux/ioport.h>
20 #include <linux/capi.h>
21 #include <linux/kernelcapi.h>
23 #include <linux/init.h>
24 #include <asm/uaccess.h>
25 #include <linux/netdevice.h>
26 #include <linux/isdn/capilli.h>
28 #include <linux/isdn/capicmd.h>
29 #include <linux/isdn/capiutil.h>
31 static char *revision = "$Revision: 1.1.2.2 $";
33 /* ------------------------------------------------------------- */
35 MODULE_DESCRIPTION("CAPI4Linux: Common support for active AVM cards");
36 MODULE_AUTHOR("Carsten Paeth");
37 MODULE_LICENSE("GPL");
39 /* ------------------------------------------------------------- */
41 int b1_irq_table[16] =
60 /* ------------------------------------------------------------- */
62 avmcard *b1_alloc_card(int nr_controllers)
68 card = kzalloc(sizeof(*card), GFP_KERNEL);
72 cinfo = kzalloc(sizeof(*cinfo) * nr_controllers, GFP_KERNEL);
78 card->ctrlinfo = cinfo;
79 for (i = 0; i < nr_controllers; i++) {
80 INIT_LIST_HEAD(&cinfo[i].ncci_head);
83 spin_lock_init(&card->lock);
84 card->nr_controllers = nr_controllers;
89 /* ------------------------------------------------------------- */
91 void b1_free_card(avmcard *card)
93 kfree(card->ctrlinfo);
97 /* ------------------------------------------------------------- */
99 int b1_detect(unsigned int base, enum avmcardtype cardtype)
104 * Statusregister 0000 00xx
106 if ((inb(base + B1_INSTAT) & 0xfc)
107 || (inb(base + B1_OUTSTAT) & 0xfc))
110 * Statusregister 0000 001x
112 b1outp(base, B1_INSTAT, 0x2); /* enable irq */
113 /* b1outp(base, B1_OUTSTAT, 0x2); */
114 if ((inb(base + B1_INSTAT) & 0xfe) != 0x2
115 /* || (inb(base + B1_OUTSTAT) & 0xfe) != 0x2 */)
118 * Statusregister 0000 000x
120 b1outp(base, B1_INSTAT, 0x0); /* disable irq */
121 b1outp(base, B1_OUTSTAT, 0x0);
122 if ((inb(base + B1_INSTAT) & 0xfe)
123 || (inb(base + B1_OUTSTAT) & 0xfe))
126 for (onoff = !0, i= 0; i < 10 ; i++) {
127 b1_set_test_bit(base, cardtype, onoff);
128 if (b1_get_test_bit(base, cardtype) != onoff)
133 if (cardtype == avm_m1)
136 if ((b1_rd_reg(base, B1_STAT1(cardtype)) & 0x0f) != 0x01)
142 void b1_getrevision(avmcard *card)
144 card->class = inb(card->port + B1_ANALYSE);
145 card->revision = inb(card->port + B1_REVISION);
148 #define FWBUF_SIZE 256
149 int b1_load_t4file(avmcard *card, capiloaddatapart * t4file)
151 unsigned char buf[FWBUF_SIZE];
154 unsigned int base = card->port;
158 while (left > FWBUF_SIZE) {
160 if (copy_from_user(buf, dp, FWBUF_SIZE))
163 memcpy(buf, dp, FWBUF_SIZE);
165 for (i = 0; i < FWBUF_SIZE; i++)
166 if (b1_save_put_byte(base, buf[i]) < 0) {
167 printk(KERN_ERR "%s: corrupted firmware file ?\n",
176 if (copy_from_user(buf, dp, left))
179 memcpy(buf, dp, left);
181 for (i = 0; i < left; i++)
182 if (b1_save_put_byte(base, buf[i]) < 0) {
183 printk(KERN_ERR "%s: corrupted firmware file ?\n",
191 int b1_load_config(avmcard *card, capiloaddatapart * config)
193 unsigned char buf[FWBUF_SIZE];
195 unsigned int base = card->port;
201 b1_put_byte(base, SEND_CONFIG);
202 b1_put_word(base, 1);
203 b1_put_byte(base, SEND_CONFIG);
204 b1_put_word(base, left);
206 while (left > FWBUF_SIZE) {
208 if (copy_from_user(buf, dp, FWBUF_SIZE))
211 memcpy(buf, dp, FWBUF_SIZE);
213 for (i = 0; i < FWBUF_SIZE; ) {
214 b1_put_byte(base, SEND_CONFIG);
215 for (j=0; j < 4; j++) {
216 b1_put_byte(base, buf[i++]);
224 if (copy_from_user(buf, dp, left))
227 memcpy(buf, dp, left);
229 for (i = 0; i < left; ) {
230 b1_put_byte(base, SEND_CONFIG);
231 for (j=0; j < 4; j++) {
233 b1_put_byte(base, buf[i++]);
235 b1_put_byte(base, 0);
242 int b1_loaded(avmcard *card)
244 unsigned int base = card->port;
247 unsigned long tout = 2;
249 for (stop = jiffies + tout * HZ; time_before(jiffies, stop);) {
250 if (b1_tx_empty(base))
253 if (!b1_tx_empty(base)) {
254 printk(KERN_ERR "%s: b1_loaded: tx err, corrupted t4 file ?\n",
258 b1_put_byte(base, SEND_POLL);
259 for (stop = jiffies + tout * HZ; time_before(jiffies, stop);) {
260 if (b1_rx_full(base)) {
261 if ((ans = b1_get_byte(base)) == RECEIVE_POLL) {
264 printk(KERN_ERR "%s: b1_loaded: got 0x%x, firmware not running\n",
269 printk(KERN_ERR "%s: b1_loaded: firmware not running\n", card->name);
273 /* ------------------------------------------------------------- */
275 int b1_load_firmware(struct capi_ctr *ctrl, capiloaddata *data)
277 avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
278 avmcard *card = cinfo->card;
279 unsigned int port = card->port;
285 if ((retval = b1_load_t4file(card, &data->firmware))) {
287 printk(KERN_ERR "%s: failed to load t4file!!\n",
292 b1_disable_irq(port);
294 if (data->configuration.len > 0 && data->configuration.data) {
295 if ((retval = b1_load_config(card, &data->configuration))) {
297 printk(KERN_ERR "%s: failed to load config!!\n",
303 if (!b1_loaded(card)) {
304 printk(KERN_ERR "%s: failed to load t4file.\n", card->name);
308 spin_lock_irqsave(&card->lock, flags);
309 b1_setinterrupt(port, card->irq, card->cardtype);
310 b1_put_byte(port, SEND_INIT);
311 b1_put_word(port, CAPI_MAXAPPL);
312 b1_put_word(port, AVM_NCCI_PER_CHANNEL*2);
313 b1_put_word(port, ctrl->cnr - 1);
314 spin_unlock_irqrestore(&card->lock, flags);
319 void b1_reset_ctr(struct capi_ctr *ctrl)
321 avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
322 avmcard *card = cinfo->card;
323 unsigned int port = card->port;
328 memset(cinfo->version, 0, sizeof(cinfo->version));
329 capilib_release(&cinfo->ncci_head);
330 capi_ctr_reseted(ctrl);
333 void b1_register_appl(struct capi_ctr *ctrl,
335 capi_register_params *rp)
337 avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
338 avmcard *card = cinfo->card;
339 unsigned int port = card->port;
341 int nconn, want = rp->level3cnt;
343 if (want > 0) nconn = want;
344 else nconn = ctrl->profile.nbchannel * -want;
345 if (nconn == 0) nconn = ctrl->profile.nbchannel;
347 spin_lock_irqsave(&card->lock, flags);
348 b1_put_byte(port, SEND_REGISTER);
349 b1_put_word(port, appl);
350 b1_put_word(port, 1024 * (nconn+1));
351 b1_put_word(port, nconn);
352 b1_put_word(port, rp->datablkcnt);
353 b1_put_word(port, rp->datablklen);
354 spin_unlock_irqrestore(&card->lock, flags);
357 void b1_release_appl(struct capi_ctr *ctrl, u16 appl)
359 avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
360 avmcard *card = cinfo->card;
361 unsigned int port = card->port;
364 capilib_release_appl(&cinfo->ncci_head, appl);
366 spin_lock_irqsave(&card->lock, flags);
367 b1_put_byte(port, SEND_RELEASE);
368 b1_put_word(port, appl);
369 spin_unlock_irqrestore(&card->lock, flags);
372 u16 b1_send_message(struct capi_ctr *ctrl, struct sk_buff *skb)
374 avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
375 avmcard *card = cinfo->card;
376 unsigned int port = card->port;
378 u16 len = CAPIMSG_LEN(skb->data);
379 u8 cmd = CAPIMSG_COMMAND(skb->data);
380 u8 subcmd = CAPIMSG_SUBCOMMAND(skb->data);
383 if (CAPICMD(cmd, subcmd) == CAPI_DATA_B3_REQ) {
384 retval = capilib_data_b3_req(&cinfo->ncci_head,
385 CAPIMSG_APPID(skb->data),
386 CAPIMSG_NCCI(skb->data),
387 CAPIMSG_MSGID(skb->data));
388 if (retval != CAPI_NOERROR)
391 dlen = CAPIMSG_DATALEN(skb->data);
393 spin_lock_irqsave(&card->lock, flags);
394 b1_put_byte(port, SEND_DATA_B3_REQ);
395 b1_put_slice(port, skb->data, len);
396 b1_put_slice(port, skb->data + len, dlen);
397 spin_unlock_irqrestore(&card->lock, flags);
399 spin_lock_irqsave(&card->lock, flags);
400 b1_put_byte(port, SEND_MESSAGE);
401 b1_put_slice(port, skb->data, len);
402 spin_unlock_irqrestore(&card->lock, flags);
405 dev_kfree_skb_any(skb);
409 /* ------------------------------------------------------------- */
411 void b1_parse_version(avmctrl_info *cinfo)
413 struct capi_ctr *ctrl = &cinfo->capi_ctrl;
414 avmcard *card = cinfo->card;
420 for (j = 0; j < AVM_MAXVERSION; j++)
421 cinfo->version[j] = "\0\0" + 1;
423 j < AVM_MAXVERSION && i < cinfo->versionlen;
424 j++, i += cinfo->versionbuf[i] + 1)
425 cinfo->version[j] = &cinfo->versionbuf[i + 1];
427 strlcpy(ctrl->serial, cinfo->version[VER_SERIAL], sizeof(ctrl->serial));
428 memcpy(&ctrl->profile, cinfo->version[VER_PROFILE],sizeof(capi_profile));
429 strlcpy(ctrl->manu, "AVM GmbH", sizeof(ctrl->manu));
430 dversion = cinfo->version[VER_DRIVER];
431 ctrl->version.majorversion = 2;
432 ctrl->version.minorversion = 0;
433 ctrl->version.majormanuversion = (((dversion[0] - '0') & 0xf) << 4);
434 ctrl->version.majormanuversion |= ((dversion[2] - '0') & 0xf);
435 ctrl->version.minormanuversion = (dversion[3] - '0') << 4;
436 ctrl->version.minormanuversion |=
437 (dversion[5] - '0') * 10 + ((dversion[6] - '0') & 0xf);
439 profp = &ctrl->profile;
441 flag = ((u8 *)(profp->manu))[1];
443 case 0: if (cinfo->version[VER_CARDTYPE])
444 strcpy(cinfo->cardname, cinfo->version[VER_CARDTYPE]);
445 else strcpy(cinfo->cardname, "B1");
447 case 3: strcpy(cinfo->cardname,"PCMCIA B"); break;
448 case 4: strcpy(cinfo->cardname,"PCMCIA M1"); break;
449 case 5: strcpy(cinfo->cardname,"PCMCIA M2"); break;
450 case 6: strcpy(cinfo->cardname,"B1 V3.0"); break;
451 case 7: strcpy(cinfo->cardname,"B1 PCI"); break;
452 default: sprintf(cinfo->cardname, "AVM?%u", (unsigned int)flag); break;
454 printk(KERN_NOTICE "%s: card %d \"%s\" ready.\n",
455 card->name, ctrl->cnr, cinfo->cardname);
457 flag = ((u8 *)(profp->manu))[3];
459 printk(KERN_NOTICE "%s: card %d Protocol:%s%s%s%s%s%s%s\n",
462 (flag & 0x01) ? " DSS1" : "",
463 (flag & 0x02) ? " CT1" : "",
464 (flag & 0x04) ? " VN3" : "",
465 (flag & 0x08) ? " NI1" : "",
466 (flag & 0x10) ? " AUSTEL" : "",
467 (flag & 0x20) ? " ESS" : "",
468 (flag & 0x40) ? " 1TR6" : ""
471 flag = ((u8 *)(profp->manu))[5];
473 printk(KERN_NOTICE "%s: card %d Linetype:%s%s%s%s\n",
476 (flag & 0x01) ? " point to point" : "",
477 (flag & 0x02) ? " point to multipoint" : "",
478 (flag & 0x08) ? " leased line without D-channel" : "",
479 (flag & 0x04) ? " leased line with D-channel" : ""
483 /* ------------------------------------------------------------- */
485 irqreturn_t b1_interrupt(int interrupt, void *devptr)
487 avmcard *card = devptr;
488 avmctrl_info *cinfo = &card->ctrlinfo[0];
489 struct capi_ctr *ctrl = &cinfo->capi_ctrl;
500 spin_lock_irqsave(&card->lock, flags);
502 if (!b1_rx_full(card->port)) {
503 spin_unlock_irqrestore(&card->lock, flags);
507 b1cmd = b1_get_byte(card->port);
511 case RECEIVE_DATA_B3_IND:
513 ApplId = (unsigned) b1_get_word(card->port);
514 MsgLen = b1_get_slice(card->port, card->msgbuf);
515 DataB3Len = b1_get_slice(card->port, card->databuf);
516 spin_unlock_irqrestore(&card->lock, flags);
518 if (MsgLen < 30) { /* not CAPI 64Bit */
519 memset(card->msgbuf+MsgLen, 0, 30-MsgLen);
521 CAPIMSG_SETLEN(card->msgbuf, 30);
523 if (!(skb = alloc_skb(DataB3Len + MsgLen, GFP_ATOMIC))) {
524 printk(KERN_ERR "%s: incoming packet dropped\n",
527 memcpy(skb_put(skb, MsgLen), card->msgbuf, MsgLen);
528 memcpy(skb_put(skb, DataB3Len), card->databuf, DataB3Len);
529 capi_ctr_handle_message(ctrl, ApplId, skb);
533 case RECEIVE_MESSAGE:
535 ApplId = (unsigned) b1_get_word(card->port);
536 MsgLen = b1_get_slice(card->port, card->msgbuf);
537 spin_unlock_irqrestore(&card->lock, flags);
538 if (!(skb = alloc_skb(MsgLen, GFP_ATOMIC))) {
539 printk(KERN_ERR "%s: incoming packet dropped\n",
542 memcpy(skb_put(skb, MsgLen), card->msgbuf, MsgLen);
543 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_CONF)
544 capilib_data_b3_conf(&cinfo->ncci_head, ApplId,
545 CAPIMSG_NCCI(skb->data),
546 CAPIMSG_MSGID(skb->data));
548 capi_ctr_handle_message(ctrl, ApplId, skb);
552 case RECEIVE_NEW_NCCI:
554 ApplId = b1_get_word(card->port);
555 NCCI = b1_get_word(card->port);
556 WindowSize = b1_get_word(card->port);
557 spin_unlock_irqrestore(&card->lock, flags);
559 capilib_new_ncci(&cinfo->ncci_head, ApplId, NCCI, WindowSize);
563 case RECEIVE_FREE_NCCI:
565 ApplId = b1_get_word(card->port);
566 NCCI = b1_get_word(card->port);
567 spin_unlock_irqrestore(&card->lock, flags);
569 if (NCCI != 0xffffffff)
570 capilib_free_ncci(&cinfo->ncci_head, ApplId, NCCI);
575 /* b1_put_byte(card->port, SEND_POLLACK); */
576 spin_unlock_irqrestore(&card->lock, flags);
577 capi_ctr_resume_output(ctrl);
581 spin_unlock_irqrestore(&card->lock, flags);
582 capi_ctr_suspend_output(ctrl);
587 cinfo->versionlen = b1_get_slice(card->port, cinfo->versionbuf);
588 spin_unlock_irqrestore(&card->lock, flags);
589 b1_parse_version(cinfo);
590 printk(KERN_INFO "%s: %s-card (%s) now active\n",
592 cinfo->version[VER_CARDTYPE],
593 cinfo->version[VER_DRIVER]);
594 capi_ctr_ready(ctrl);
597 case RECEIVE_TASK_READY:
598 ApplId = (unsigned) b1_get_word(card->port);
599 MsgLen = b1_get_slice(card->port, card->msgbuf);
600 spin_unlock_irqrestore(&card->lock, flags);
601 card->msgbuf[MsgLen] = 0;
603 && ( card->msgbuf[MsgLen-1] == '\n'
604 || card->msgbuf[MsgLen-1] == '\r')) {
605 card->msgbuf[MsgLen-1] = 0;
608 printk(KERN_INFO "%s: task %d \"%s\" ready.\n",
609 card->name, ApplId, card->msgbuf);
612 case RECEIVE_DEBUGMSG:
613 MsgLen = b1_get_slice(card->port, card->msgbuf);
614 spin_unlock_irqrestore(&card->lock, flags);
615 card->msgbuf[MsgLen] = 0;
617 && ( card->msgbuf[MsgLen-1] == '\n'
618 || card->msgbuf[MsgLen-1] == '\r')) {
619 card->msgbuf[MsgLen-1] = 0;
622 printk(KERN_INFO "%s: DEBUG: %s\n", card->name, card->msgbuf);
626 spin_unlock_irqrestore(&card->lock, flags);
627 printk(KERN_ERR "%s: card removed ?\n", card->name);
630 spin_unlock_irqrestore(&card->lock, flags);
631 printk(KERN_ERR "%s: b1_interrupt: 0x%x ???\n",
638 /* ------------------------------------------------------------- */
639 int b1ctl_read_proc(char *page, char **start, off_t off,
640 int count, int *eof, struct capi_ctr *ctrl)
642 avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
643 avmcard *card = cinfo->card;
648 len += sprintf(page+len, "%-16s %s\n", "name", card->name);
649 len += sprintf(page+len, "%-16s 0x%x\n", "io", card->port);
650 len += sprintf(page+len, "%-16s %d\n", "irq", card->irq);
651 switch (card->cardtype) {
652 case avm_b1isa: s = "B1 ISA"; break;
653 case avm_b1pci: s = "B1 PCI"; break;
654 case avm_b1pcmcia: s = "B1 PCMCIA"; break;
655 case avm_m1: s = "M1"; break;
656 case avm_m2: s = "M2"; break;
657 case avm_t1isa: s = "T1 ISA (HEMA)"; break;
658 case avm_t1pci: s = "T1 PCI"; break;
659 case avm_c4: s = "C4"; break;
660 case avm_c2: s = "C2"; break;
661 default: s = "???"; break;
663 len += sprintf(page+len, "%-16s %s\n", "type", s);
664 if (card->cardtype == avm_t1isa)
665 len += sprintf(page+len, "%-16s %d\n", "cardnr", card->cardnr);
666 if ((s = cinfo->version[VER_DRIVER]) != 0)
667 len += sprintf(page+len, "%-16s %s\n", "ver_driver", s);
668 if ((s = cinfo->version[VER_CARDTYPE]) != 0)
669 len += sprintf(page+len, "%-16s %s\n", "ver_cardtype", s);
670 if ((s = cinfo->version[VER_SERIAL]) != 0)
671 len += sprintf(page+len, "%-16s %s\n", "ver_serial", s);
673 if (card->cardtype != avm_m1) {
674 flag = ((u8 *)(ctrl->profile.manu))[3];
676 len += sprintf(page+len, "%-16s%s%s%s%s%s%s%s\n",
678 (flag & 0x01) ? " DSS1" : "",
679 (flag & 0x02) ? " CT1" : "",
680 (flag & 0x04) ? " VN3" : "",
681 (flag & 0x08) ? " NI1" : "",
682 (flag & 0x10) ? " AUSTEL" : "",
683 (flag & 0x20) ? " ESS" : "",
684 (flag & 0x40) ? " 1TR6" : ""
687 if (card->cardtype != avm_m1) {
688 flag = ((u8 *)(ctrl->profile.manu))[5];
690 len += sprintf(page+len, "%-16s%s%s%s%s\n",
692 (flag & 0x01) ? " point to point" : "",
693 (flag & 0x02) ? " point to multipoint" : "",
694 (flag & 0x08) ? " leased line without D-channel" : "",
695 (flag & 0x04) ? " leased line with D-channel" : ""
698 len += sprintf(page+len, "%-16s %s\n", "cardname", cinfo->cardname);
700 if (off+count >= len)
705 return ((count < len-off) ? count : len-off);
708 /* ------------------------------------------------------------- */
713 avmcard_dma_alloc(char *name, struct pci_dev *pdev, long rsize, long ssize)
718 p = kzalloc(sizeof(avmcard_dmainfo), GFP_KERNEL);
720 printk(KERN_WARNING "%s: no memory.\n", name);
724 p->recvbuf.size = rsize;
725 buf = pci_alloc_consistent(pdev, rsize, &p->recvbuf.dmaaddr);
727 printk(KERN_WARNING "%s: allocation of receive dma buffer failed.\n", name);
730 p->recvbuf.dmabuf = buf;
732 p->sendbuf.size = ssize;
733 buf = pci_alloc_consistent(pdev, ssize, &p->sendbuf.dmaaddr);
735 printk(KERN_WARNING "%s: allocation of send dma buffer failed.\n", name);
736 goto err_free_consistent;
739 p->sendbuf.dmabuf = buf;
740 skb_queue_head_init(&p->send_queue);
745 pci_free_consistent(p->pcidev, p->recvbuf.size,
746 p->recvbuf.dmabuf, p->recvbuf.dmaaddr);
753 void avmcard_dma_free(avmcard_dmainfo *p)
755 pci_free_consistent(p->pcidev, p->recvbuf.size,
756 p->recvbuf.dmabuf, p->recvbuf.dmaaddr);
757 pci_free_consistent(p->pcidev, p->sendbuf.size,
758 p->sendbuf.dmabuf, p->sendbuf.dmaaddr);
759 skb_queue_purge(&p->send_queue);
763 EXPORT_SYMBOL(avmcard_dma_alloc);
764 EXPORT_SYMBOL(avmcard_dma_free);
768 EXPORT_SYMBOL(b1_irq_table);
770 EXPORT_SYMBOL(b1_alloc_card);
771 EXPORT_SYMBOL(b1_free_card);
772 EXPORT_SYMBOL(b1_detect);
773 EXPORT_SYMBOL(b1_getrevision);
774 EXPORT_SYMBOL(b1_load_t4file);
775 EXPORT_SYMBOL(b1_load_config);
776 EXPORT_SYMBOL(b1_loaded);
777 EXPORT_SYMBOL(b1_load_firmware);
778 EXPORT_SYMBOL(b1_reset_ctr);
779 EXPORT_SYMBOL(b1_register_appl);
780 EXPORT_SYMBOL(b1_release_appl);
781 EXPORT_SYMBOL(b1_send_message);
783 EXPORT_SYMBOL(b1_parse_version);
784 EXPORT_SYMBOL(b1_interrupt);
786 EXPORT_SYMBOL(b1ctl_read_proc);
788 static int __init b1_init(void)
793 if ((p = strchr(revision, ':')) != 0 && p[1]) {
794 strlcpy(rev, p + 2, 32);
795 if ((p = strchr(rev, '$')) != 0 && p > rev)
800 printk(KERN_INFO "b1: revision %s\n", rev);
805 static void __exit b1_exit(void)
809 module_init(b1_init);
810 module_exit(b1_exit);