1 /* $Id: icn.c,v 1.65.6.8 2001/09/23 22:24:55 kai Exp $
3 * ISDN low-level module for the ICN active ISDN-Card.
5 * Copyright 1994,95,96 by Fritz Elfert (fritz@isdn4linux.de)
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
13 #include <linux/module.h>
14 #include <linux/init.h>
16 static int portbase = ICN_BASEADDR;
17 static unsigned long membase = ICN_MEMADDR;
18 static char *icn_id = "\0";
19 static char *icn_id2 = "\0";
21 MODULE_DESCRIPTION("ISDN4Linux: Driver for ICN active ISDN card");
22 MODULE_AUTHOR("Fritz Elfert");
23 MODULE_LICENSE("GPL");
24 module_param(portbase, int, 0);
25 MODULE_PARM_DESC(portbase, "Port address of first card");
26 module_param(membase, ulong, 0);
27 MODULE_PARM_DESC(membase, "Shared memory address of all cards");
28 module_param(icn_id, charp, 0);
29 MODULE_PARM_DESC(icn_id, "ID-String of first card");
30 module_param(icn_id2, charp, 0);
31 MODULE_PARM_DESC(icn_id2, "ID-String of first card, second S0 (4B only)");
34 * Verbose bootcode- and protocol-downloading.
39 * Verbose Shmem-Mapping.
44 *revision = "$Revision: 1.65.6.8 $";
46 static int icn_addcard(int, char *, char *);
49 * Free send-queue completely.
51 * card = pointer to card struct
52 * channel = channel number
55 icn_free_queue(icn_card * card, int channel)
57 struct sk_buff_head *queue = &card->spqueue[channel];
60 skb_queue_purge(queue);
61 card->xlen[channel] = 0;
62 card->sndcount[channel] = 0;
63 if ((skb = card->xskb[channel])) {
64 card->xskb[channel] = NULL;
69 /* Put a value into a shift-register, highest bit first.
71 * port = port for output (bit 0 is significant)
72 * val = value to be output
73 * firstbit = Bit-Number of highest bit
74 * bitcount = Number of bits to output
77 icn_shiftout(unsigned short port,
86 for (s = firstbit, c = bitcount; c > 0; s--, c--)
87 OUTB_P((u_char) ((val >> s) & 1) ? 0xff : 0, port);
91 * disable a cards shared memory
94 icn_disable_ram(icn_card * card)
96 OUTB_P(0, ICN_MAPRAM);
100 * enable a cards shared memory
103 icn_enable_ram(icn_card * card)
105 OUTB_P(0xff, ICN_MAPRAM);
109 * Map a cards channel0 (Bank0/Bank8) or channel1 (Bank4/Bank12)
111 * must called with holding the devlock
114 icn_map_channel(icn_card * card, int channel)
117 printk(KERN_DEBUG "icn_map_channel %d %d\n", dev.channel, channel);
119 if ((channel == dev.channel) && (card == dev.mcard))
122 icn_disable_ram(dev.mcard);
123 icn_shiftout(ICN_BANK, chan2bank[channel], 3, 4); /* Select Bank */
124 icn_enable_ram(card);
126 dev.channel = channel;
128 printk(KERN_DEBUG "icn_map_channel done\n");
133 * Lock a cards channel.
134 * Return 0 if requested card/channel is unmapped (failure).
135 * Return 1 on success.
137 * must called with holding the devlock
140 icn_lock_channel(icn_card * card, int channel)
145 printk(KERN_DEBUG "icn_lock_channel %d\n", channel);
147 if ((dev.channel == channel) && (card == dev.mcard)) {
151 printk(KERN_DEBUG "icn_lock_channel %d OK\n", channel);
156 printk(KERN_DEBUG "icn_lock_channel %d FAILED, dc=%d\n", channel, dev.channel);
163 * Release current card/channel lock
165 * must called with holding the devlock
168 __icn_release_channel(void)
171 printk(KERN_DEBUG "icn_release_channel l=%d\n", dev.chanlock);
173 if (dev.chanlock > 0)
178 * Release current card/channel lock
181 icn_release_channel(void)
185 spin_lock_irqsave(&dev.devlock, flags);
186 __icn_release_channel();
187 spin_unlock_irqrestore(&dev.devlock, flags);
191 * Try to map and lock a cards channel.
192 * Return 1 on success, 0 on failure.
195 icn_trymaplock_channel(icn_card * card, int channel)
200 printk(KERN_DEBUG "trymaplock c=%d dc=%d l=%d\n", channel, dev.channel,
203 spin_lock_irqsave(&dev.devlock, flags);
204 if ((!dev.chanlock) ||
205 ((dev.channel == channel) && (dev.mcard == card))) {
207 icn_map_channel(card, channel);
208 spin_unlock_irqrestore(&dev.devlock, flags);
210 printk(KERN_DEBUG "trymaplock %d OK\n", channel);
214 spin_unlock_irqrestore(&dev.devlock, flags);
216 printk(KERN_DEBUG "trymaplock %d FAILED\n", channel);
222 * Release current card/channel lock,
223 * then map same or other channel without locking.
226 icn_maprelease_channel(icn_card * card, int channel)
231 printk(KERN_DEBUG "map_release c=%d l=%d\n", channel, dev.chanlock);
233 spin_lock_irqsave(&dev.devlock, flags);
234 if (dev.chanlock > 0)
237 icn_map_channel(card, channel);
238 spin_unlock_irqrestore(&dev.devlock, flags);
241 /* Get Data from the B-Channel, assemble fragmented packets and put them
242 * into receive-queue. Wake up any B-Channel-reading processes.
243 * This routine is called via timer-callback from icn_pollbchan().
247 icn_pollbchan_receive(int channel, icn_card * card)
249 int mch = channel + ((card->secondhalf) ? 2 : 0);
254 if (icn_trymaplock_channel(card, mch)) {
256 cnt = readb(&rbuf_l);
257 if ((card->rcvidx[channel] + cnt) > 4000) {
259 "icn: (%s) bogus packet on ch%d, dropping.\n",
262 card->rcvidx[channel] = 0;
265 memcpy_fromio(&card->rcvbuf[channel][card->rcvidx[channel]],
267 card->rcvidx[channel] += cnt;
268 eflag = readb(&rbuf_f);
271 icn_maprelease_channel(card, mch & 2);
273 if ((cnt = card->rcvidx[channel])) {
274 if (!(skb = dev_alloc_skb(cnt))) {
275 printk(KERN_WARNING "icn: receive out of memory\n");
278 memcpy(skb_put(skb, cnt), card->rcvbuf[channel], cnt);
279 card->rcvidx[channel] = 0;
280 card->interface.rcvcallb_skb(card->myid, channel, skb);
283 if (!icn_trymaplock_channel(card, mch))
286 icn_maprelease_channel(card, mch & 2);
290 /* Send data-packet to B-Channel, split it up into fragments of
291 * ICN_FRAGSIZE length. If last fragment is sent out, signal
292 * success to upper layers via statcallb with ISDN_STAT_BSENT argument.
293 * This routine is called via timer-callback from icn_pollbchan() or
294 * directly from icn_sendbuf().
298 icn_pollbchan_send(int channel, icn_card * card)
300 int mch = channel + ((card->secondhalf) ? 2 : 0);
306 if (!(card->sndcount[channel] || card->xskb[channel] ||
307 !skb_queue_empty(&card->spqueue[channel])))
309 if (icn_trymaplock_channel(card, mch)) {
311 (card->sndcount[channel] ||
312 !skb_queue_empty(&card->spqueue[channel]) ||
313 card->xskb[channel])) {
314 spin_lock_irqsave(&card->lock, flags);
315 if (card->xmit_lock[channel]) {
316 spin_unlock_irqrestore(&card->lock, flags);
319 card->xmit_lock[channel]++;
320 spin_unlock_irqrestore(&card->lock, flags);
321 skb = card->xskb[channel];
323 skb = skb_dequeue(&card->spqueue[channel]);
325 /* Pop ACK-flag off skb.
326 * Store length to xlen.
328 if (*(skb_pull(skb,1)))
329 card->xlen[channel] = skb->len;
331 card->xlen[channel] = 0;
336 if (skb->len > ICN_FRAGSIZE) {
337 writeb(0xff, &sbuf_f);
340 writeb(0x0, &sbuf_f);
343 writeb(cnt, &sbuf_l);
344 memcpy_toio(&sbuf_d, skb->data, cnt);
346 sbnext; /* switch to next buffer */
347 icn_maprelease_channel(card, mch & 2);
348 spin_lock_irqsave(&card->lock, flags);
349 card->sndcount[channel] -= cnt;
351 if (card->xskb[channel])
352 card->xskb[channel] = NULL;
353 card->xmit_lock[channel] = 0;
354 spin_unlock_irqrestore(&card->lock, flags);
356 if (card->xlen[channel]) {
357 cmd.command = ISDN_STAT_BSENT;
358 cmd.driver = card->myid;
360 cmd.parm.length = card->xlen[channel];
361 card->interface.statcallb(&cmd);
364 card->xskb[channel] = skb;
365 card->xmit_lock[channel] = 0;
366 spin_unlock_irqrestore(&card->lock, flags);
368 if (!icn_trymaplock_channel(card, mch))
371 icn_maprelease_channel(card, mch & 2);
375 /* Send/Receive Data to/from the B-Channel.
376 * This routine is called via timer-callback.
377 * It schedules itself while any B-Channel is open.
381 icn_pollbchan(unsigned long data)
383 icn_card *card = (icn_card *) data;
386 if (card->flags & ICN_FLAGS_B1ACTIVE) {
387 icn_pollbchan_receive(0, card);
388 icn_pollbchan_send(0, card);
390 if (card->flags & ICN_FLAGS_B2ACTIVE) {
391 icn_pollbchan_receive(1, card);
392 icn_pollbchan_send(1, card);
394 if (card->flags & (ICN_FLAGS_B1ACTIVE | ICN_FLAGS_B2ACTIVE)) {
395 /* schedule b-channel polling again */
396 spin_lock_irqsave(&card->lock, flags);
397 mod_timer(&card->rb_timer, jiffies+ICN_TIMER_BCREAD);
398 card->flags |= ICN_FLAGS_RBTIMER;
399 spin_unlock_irqrestore(&card->lock, flags);
401 card->flags &= ~ICN_FLAGS_RBTIMER;
404 typedef struct icn_stat {
410 static icn_stat icn_stat_table[] =
412 {"BCON_", ISDN_STAT_BCONN, 1}, /* B-Channel connected */
413 {"BDIS_", ISDN_STAT_BHUP, 2}, /* B-Channel disconnected */
415 ** add d-channel connect and disconnect support to link-level
417 {"DCON_", ISDN_STAT_DCONN, 10}, /* D-Channel connected */
418 {"DDIS_", ISDN_STAT_DHUP, 11}, /* D-Channel disconnected */
419 {"DCAL_I", ISDN_STAT_ICALL, 3}, /* Incoming call dialup-line */
420 {"DSCA_I", ISDN_STAT_ICALL, 3}, /* Incoming call 1TR6-SPV */
421 {"FCALL", ISDN_STAT_ICALL, 4}, /* Leased line connection up */
422 {"CIF", ISDN_STAT_CINF, 5}, /* Charge-info, 1TR6-type */
423 {"AOC", ISDN_STAT_CINF, 6}, /* Charge-info, DSS1-type */
424 {"CAU", ISDN_STAT_CAUSE, 7}, /* Cause code */
425 {"TEI OK", ISDN_STAT_RUN, 0}, /* Card connected to wallplug */
426 {"E_L1: ACT FAIL", ISDN_STAT_BHUP, 8}, /* Layer-1 activation failed */
427 {"E_L2: DATA LIN", ISDN_STAT_BHUP, 8}, /* Layer-2 data link lost */
428 {"E_L1: ACTIVATION FAILED",
429 ISDN_STAT_BHUP, 8}, /* Layer-1 activation failed */
436 * Check Statusqueue-Pointer from isdn-cards.
437 * If there are new status-replies from the interface, check
438 * them against B-Channel-connects/disconnects and set flags accordingly.
439 * Wake-Up any processes, who are reading the status-device.
440 * If there are B-Channels open, initiate a timer-callback to
442 * This routine is called periodically via timer.
446 icn_parse_status(u_char * status, int channel, icn_card * card)
448 icn_stat *s = icn_stat_table;
454 if (!strncmp(status, s->statstr, strlen(s->statstr))) {
455 cmd.command = s->command;
463 cmd.driver = card->myid;
467 spin_lock_irqsave(&card->lock, flags);
468 icn_free_queue(card,channel);
469 card->rcvidx[channel] = 0;
472 ((channel)?ICN_FLAGS_B2ACTIVE:ICN_FLAGS_B1ACTIVE)) {
476 card->flags &= ~((channel)?
477 ICN_FLAGS_B2ACTIVE:ICN_FLAGS_B1ACTIVE);
479 memset(&ncmd, 0, sizeof(ncmd));
481 ncmd.driver = card->myid;
483 ncmd.command = ISDN_STAT_BHUP;
484 spin_unlock_irqrestore(&card->lock, flags);
485 card->interface.statcallb(&cmd);
487 spin_unlock_irqrestore(&card->lock, flags);
490 spin_lock_irqsave(&card->lock, flags);
491 icn_free_queue(card,channel);
492 card->flags |= (channel) ?
493 ICN_FLAGS_B2ACTIVE : ICN_FLAGS_B1ACTIVE;
494 spin_unlock_irqrestore(&card->lock, flags);
497 spin_lock_irqsave(&card->lock, flags);
498 card->flags &= ~((channel) ?
499 ICN_FLAGS_B2ACTIVE : ICN_FLAGS_B1ACTIVE);
500 icn_free_queue(card, channel);
501 card->rcvidx[channel] = 0;
502 spin_unlock_irqrestore(&card->lock, flags);
506 char *t = status + 6;
507 char *s = strchr(t, ',');
510 strlcpy(cmd.parm.setup.phone, t,
511 sizeof(cmd.parm.setup.phone));
512 s = strchr(t = s, ',');
515 cmd.parm.setup.si1 = 0;
518 simple_strtoul(t, NULL, 10);
519 s = strchr(t = s, ',');
522 cmd.parm.setup.si2 = 0;
525 simple_strtoul(t, NULL, 10);
526 strlcpy(cmd.parm.setup.eazmsn, s,
527 sizeof(cmd.parm.setup.eazmsn));
529 cmd.parm.setup.plan = 0;
530 cmd.parm.setup.screen = 0;
533 sprintf(cmd.parm.setup.phone, "LEASED%d", card->myid);
534 sprintf(cmd.parm.setup.eazmsn, "%d", channel + 1);
535 cmd.parm.setup.si1 = 7;
536 cmd.parm.setup.si2 = 0;
537 cmd.parm.setup.plan = 0;
538 cmd.parm.setup.screen = 0;
541 strlcpy(cmd.parm.num, status + 3, sizeof(cmd.parm.num));
544 snprintf(cmd.parm.num, sizeof(cmd.parm.num), "%d",
545 (int) simple_strtoul(status + 7, NULL, 16));
549 if (strlen(status) == 4)
550 snprintf(cmd.parm.num, sizeof(cmd.parm.num), "%s%c%c",
551 status + 2, *status, *(status + 1));
553 strlcpy(cmd.parm.num, status + 1, sizeof(cmd.parm.num));
556 spin_lock_irqsave(&card->lock, flags);
557 card->flags &= ~ICN_FLAGS_B1ACTIVE;
558 icn_free_queue(card, 0);
560 spin_unlock_irqrestore(&card->lock, flags);
562 cmd.driver = card->myid;
563 card->interface.statcallb(&cmd);
564 cmd.command = ISDN_STAT_DHUP;
566 cmd.driver = card->myid;
567 card->interface.statcallb(&cmd);
568 cmd.command = ISDN_STAT_BHUP;
569 spin_lock_irqsave(&card->lock, flags);
570 card->flags &= ~ICN_FLAGS_B2ACTIVE;
571 icn_free_queue(card, 1);
573 spin_unlock_irqrestore(&card->lock, flags);
575 cmd.driver = card->myid;
576 card->interface.statcallb(&cmd);
577 cmd.command = ISDN_STAT_DHUP;
579 cmd.driver = card->myid;
582 card->interface.statcallb(&cmd);
587 icn_putmsg(icn_card * card, unsigned char c)
591 spin_lock_irqsave(&card->lock, flags);
592 *card->msg_buf_write++ = (c == 0xff) ? '\n' : c;
593 if (card->msg_buf_write == card->msg_buf_read) {
594 if (++card->msg_buf_read > card->msg_buf_end)
595 card->msg_buf_read = card->msg_buf;
597 if (card->msg_buf_write > card->msg_buf_end)
598 card->msg_buf_write = card->msg_buf;
599 spin_unlock_irqrestore(&card->lock, flags);
603 icn_polldchan(unsigned long data)
605 icn_card *card = (icn_card *) data;
606 int mch = card->secondhalf ? 2 : 0;
616 if (icn_trymaplock_channel(card, mch)) {
618 for (left = avail, i = readb(&msg_o); left > 0; i++, left--) {
619 c = readb(&dev.shmem->comm_buffers.iopc_buf[i & 0xff]);
622 card->imsg[card->iptr] = 0;
624 if (card->imsg[0] == '0' && card->imsg[1] >= '0' &&
625 card->imsg[1] <= '2' && card->imsg[2] == ';') {
626 ch = (card->imsg[1] - '0') - 1;
628 icn_parse_status(p, ch, card);
631 if (!strncmp(p, "DRV1.", 5)) {
635 printk(KERN_INFO "icn: (%s) %s\n", CID, p);
636 if (!strncmp(p + 7, "TC", 2)) {
637 card->ptype = ISDN_PTYPE_1TR6;
638 card->interface.features |= ISDN_FEATURE_P_1TR6;
640 "icn: (%s) 1TR6-Protocol loaded and running\n", CID);
642 if (!strncmp(p + 7, "EC", 2)) {
643 card->ptype = ISDN_PTYPE_EURO;
644 card->interface.features |= ISDN_FEATURE_P_EURO;
646 "icn: (%s) Euro-Protocol loaded and running\n", CID);
648 p = strstr(card->imsg, "BRV") + 3;
650 if (*p >= '0' && *p <= '9')
657 card->fw_rev = (int) simple_strtoul(vstr, NULL, 10);
663 card->imsg[card->iptr] = c;
668 writeb((readb(&msg_o) + avail) & 0xff, &msg_o);
669 icn_release_channel();
672 cmd.command = ISDN_STAT_STAVAIL;
673 cmd.driver = card->myid;
675 card->interface.statcallb(&cmd);
677 spin_lock_irqsave(&card->lock, flags);
678 if (card->flags & (ICN_FLAGS_B1ACTIVE | ICN_FLAGS_B2ACTIVE))
679 if (!(card->flags & ICN_FLAGS_RBTIMER)) {
680 /* schedule b-channel polling */
681 card->flags |= ICN_FLAGS_RBTIMER;
682 del_timer(&card->rb_timer);
683 card->rb_timer.function = icn_pollbchan;
684 card->rb_timer.data = (unsigned long) card;
685 card->rb_timer.expires = jiffies + ICN_TIMER_BCREAD;
686 add_timer(&card->rb_timer);
689 mod_timer(&card->st_timer, jiffies+ICN_TIMER_DCREAD);
690 spin_unlock_irqrestore(&card->lock, flags);
693 /* Append a packet to the transmit buffer-queue.
695 * channel = Number of B-channel
696 * skb = pointer to sk_buff
697 * card = pointer to card-struct
699 * Number of bytes transferred, -E??? on error
703 icn_sendbuf(int channel, int ack, struct sk_buff *skb, icn_card * card)
707 struct sk_buff *nskb;
711 "icn: Send packet too large\n");
715 if (!(card->flags & (channel) ? ICN_FLAGS_B2ACTIVE : ICN_FLAGS_B1ACTIVE))
717 if (card->sndcount[channel] > ICN_MAX_SQUEUE)
719 #warning TODO test headroom or use skb->nb to flag ACK
720 nskb = skb_clone(skb, GFP_ATOMIC);
722 /* Push ACK flag as one
723 * byte in front of data.
725 *(skb_push(nskb, 1)) = ack?1:0;
726 skb_queue_tail(&card->spqueue[channel], nskb);
730 spin_lock_irqsave(&card->lock, flags);
731 card->sndcount[channel] += len;
732 spin_unlock_irqrestore(&card->lock, flags);
738 * Check card's status after starting the bootstrap loader.
739 * On entry, the card's shared memory has already to be mapped.
741 * 0 on success (Boot loader ready)
742 * -EIO on failure (timeout)
745 icn_check_loader(int cardnumber)
751 printk(KERN_DEBUG "Loader %d ?\n", cardnumber);
753 if (readb(&dev.shmem->data_control.scns) ||
754 readb(&dev.shmem->data_control.scnr)) {
757 "icn: Boot-Loader %d timed out.\n",
759 icn_release_channel();
763 printk(KERN_DEBUG "Loader %d TO?\n", cardnumber);
765 msleep_interruptible(ICN_BOOT_TIMEOUT1);
768 printk(KERN_DEBUG "Loader %d OK\n", cardnumber);
770 icn_release_channel();
776 /* Load the boot-code into the interface-card's memory and start it.
777 * Always called from user-process.
780 * buffer = pointer to packet
782 * 0 if successfully loaded
786 #define SLEEP(sec) { \
788 printk(KERN_DEBUG "SLEEP(%d)\n",slsec); \
790 msleep_interruptible(1000); \
799 icn_loadboot(u_char __user * buffer, icn_card * card)
806 printk(KERN_DEBUG "icn_loadboot called, buffaddr=%08lx\n", (ulong) buffer);
808 if (!(codebuf = kmalloc(ICN_CODE_STAGE1, GFP_KERNEL))) {
809 printk(KERN_WARNING "icn: Could not allocate code buffer\n");
813 if (copy_from_user(codebuf, buffer, ICN_CODE_STAGE1)) {
818 if (!request_region(card->port, ICN_PORTLEN, card->regname)) {
820 "icn: (%s) ports 0x%03x-0x%03x in use.\n",
823 card->port + ICN_PORTLEN);
829 card->other->rvalid = 1;
832 if (!request_mem_region(dev.memaddr, 0x4000, "icn-isdn (all cards)")) {
834 "icn: memory at 0x%08lx in use.\n", dev.memaddr);
838 dev.shmem = ioremap(dev.memaddr, 0x4000);
841 OUTB_P(0, ICN_RUN); /* Reset Controller */
842 OUTB_P(0, ICN_MAPRAM); /* Disable RAM */
843 icn_shiftout(ICN_CFG, 0x0f, 3, 4); /* Windowsize= 16k */
844 icn_shiftout(ICN_CFG, dev.memaddr, 23, 10); /* Set RAM-Addr. */
846 printk(KERN_DEBUG "shmem=%08lx\n", dev.memaddr);
850 printk(KERN_DEBUG "Map Bank 0\n");
852 spin_lock_irqsave(&dev.devlock, flags);
853 icn_map_channel(card, 0); /* Select Bank 0 */
854 icn_lock_channel(card, 0); /* Lock Bank 0 */
855 spin_unlock_irqrestore(&dev.devlock, flags);
857 memcpy_toio(dev.shmem, codebuf, ICN_CODE_STAGE1); /* Copy code */
859 printk(KERN_DEBUG "Bootloader transferred\n");
861 if (card->doubleS0) {
864 printk(KERN_DEBUG "Map Bank 8\n");
866 spin_lock_irqsave(&dev.devlock, flags);
867 __icn_release_channel();
868 icn_map_channel(card, 2); /* Select Bank 8 */
869 icn_lock_channel(card, 2); /* Lock Bank 8 */
870 spin_unlock_irqrestore(&dev.devlock, flags);
872 memcpy_toio(dev.shmem, codebuf, ICN_CODE_STAGE1); /* Copy code */
874 printk(KERN_DEBUG "Bootloader transferred\n");
878 OUTB_P(0xff, ICN_RUN); /* Start Boot-Code */
879 if ((ret = icn_check_loader(card->doubleS0 ? 2 : 1))) {
882 if (!card->doubleS0) {
886 /* reached only, if we have a Double-S0-Card */
888 printk(KERN_DEBUG "Map Bank 0\n");
890 spin_lock_irqsave(&dev.devlock, flags);
891 icn_map_channel(card, 0); /* Select Bank 0 */
892 icn_lock_channel(card, 0); /* Lock Bank 0 */
893 spin_unlock_irqrestore(&dev.devlock, flags);
895 ret = (icn_check_loader(1));
904 icn_loadproto(u_char __user * buffer, icn_card * card)
906 register u_char __user *p = buffer;
908 uint left = ICN_CODE_STAGE2;
914 printk(KERN_DEBUG "icn_loadproto called\n");
916 if (!access_ok(VERIFY_READ, buffer, ICN_CODE_STAGE2))
919 spin_lock_irqsave(&dev.devlock, flags);
920 if (card->secondhalf) {
921 icn_map_channel(card, 2);
922 icn_lock_channel(card, 2);
924 icn_map_channel(card, 0);
925 icn_lock_channel(card, 0);
927 spin_unlock_irqrestore(&dev.devlock, flags);
929 if (sbfree) { /* If there is a free buffer... */
933 if (copy_from_user(codebuf, p, cnt)) {
934 icn_maprelease_channel(card, 0);
937 memcpy_toio(&sbuf_l, codebuf, cnt); /* copy data */
938 sbnext; /* switch to next buffer */
944 printk(KERN_DEBUG "boot 2 !sbfree\n");
947 icn_maprelease_channel(card, 0);
950 schedule_timeout_interruptible(10);
953 writeb(0x20, &sbuf_n);
956 if (readb(&cmd_o) || readb(&cmd_i)) {
958 printk(KERN_DEBUG "Proto?\n");
962 "icn: (%s) Protocol timed out.\n",
965 printk(KERN_DEBUG "Proto TO!\n");
967 icn_maprelease_channel(card, 0);
971 printk(KERN_DEBUG "Proto TO?\n");
973 msleep_interruptible(ICN_BOOT_TIMEOUT1);
975 if ((card->secondhalf) || (!card->doubleS0)) {
977 printk(KERN_DEBUG "Proto loaded, install poll-timer %d\n",
980 spin_lock_irqsave(&card->lock, flags);
981 init_timer(&card->st_timer);
982 card->st_timer.expires = jiffies + ICN_TIMER_DCREAD;
983 card->st_timer.function = icn_polldchan;
984 card->st_timer.data = (unsigned long) card;
985 add_timer(&card->st_timer);
986 card->flags |= ICN_FLAGS_RUNNING;
987 if (card->doubleS0) {
988 init_timer(&card->other->st_timer);
989 card->other->st_timer.expires = jiffies + ICN_TIMER_DCREAD;
990 card->other->st_timer.function = icn_polldchan;
991 card->other->st_timer.data = (unsigned long) card->other;
992 add_timer(&card->other->st_timer);
993 card->other->flags |= ICN_FLAGS_RUNNING;
995 spin_unlock_irqrestore(&card->lock, flags);
997 icn_maprelease_channel(card, 0);
1003 /* Read the Status-replies from the Interface */
1005 icn_readstatus(u_char __user *buf, int len, icn_card * card)
1010 for (p = buf, count = 0; count < len; p++, count++) {
1011 if (card->msg_buf_read == card->msg_buf_write)
1013 put_user(*card->msg_buf_read++, p);
1014 if (card->msg_buf_read > card->msg_buf_end)
1015 card->msg_buf_read = card->msg_buf;
1020 /* Put command-strings into the command-queue of the Interface */
1022 icn_writecmd(const u_char * buf, int len, int user, icn_card * card)
1024 int mch = card->secondhalf ? 2 : 0;
1031 unsigned long flags;
1032 int lastmap_channel;
1033 struct icn_card *lastmap_card;
1045 if (copy_from_user(msg, buf, count))
1048 memcpy(msg, buf, count);
1050 spin_lock_irqsave(&dev.devlock, flags);
1051 lastmap_card = dev.mcard;
1052 lastmap_channel = dev.channel;
1053 icn_map_channel(card, mch);
1055 icn_putmsg(card, '>');
1056 for (p = msg, pp = readb(&cmd_i), i = count; i > 0; i--, p++, pp
1058 writeb((*p == '\n') ? 0xff : *p,
1059 &dev.shmem->comm_buffers.pcio_buf[pp & 0xff]);
1062 icn_putmsg(card, *p);
1063 if ((*p == '\n') && (i > 1)) {
1064 icn_putmsg(card, '>');
1069 writeb((readb(&cmd_i) + count) & 0xff, &cmd_i);
1071 icn_map_channel(lastmap_card, lastmap_channel);
1072 spin_unlock_irqrestore(&dev.devlock, flags);
1081 printk(KERN_WARNING "icn: writemsg incomplete!\n");
1082 cmd.command = ISDN_STAT_STAVAIL;
1083 cmd.driver = card->myid;
1085 card->interface.statcallb(&cmd);
1090 * Delete card's pending timers, send STOP to linklevel
1093 icn_stopcard(icn_card * card)
1095 unsigned long flags;
1098 spin_lock_irqsave(&card->lock, flags);
1099 if (card->flags & ICN_FLAGS_RUNNING) {
1100 card->flags &= ~ICN_FLAGS_RUNNING;
1101 del_timer(&card->st_timer);
1102 del_timer(&card->rb_timer);
1103 spin_unlock_irqrestore(&card->lock, flags);
1104 cmd.command = ISDN_STAT_STOP;
1105 cmd.driver = card->myid;
1106 card->interface.statcallb(&cmd);
1108 icn_stopcard(card->other);
1110 spin_unlock_irqrestore(&card->lock, flags);
1114 icn_stopallcards(void)
1116 icn_card *p = cards;
1125 * Unmap all cards, because some of them may be mapped accidetly during
1126 * autoprobing of some network drivers (SMC-driver?)
1129 icn_disable_cards(void)
1131 icn_card *card = cards;
1134 if (!request_region(card->port, ICN_PORTLEN, "icn-isdn")) {
1136 "icn: (%s) ports 0x%03x-0x%03x in use.\n",
1139 card->port + ICN_PORTLEN);
1141 OUTB_P(0, ICN_RUN); /* Reset Controller */
1142 OUTB_P(0, ICN_MAPRAM); /* Disable RAM */
1143 release_region(card->port, ICN_PORTLEN);
1150 icn_command(isdn_ctrl * c, icn_card * card)
1160 switch (c->command) {
1161 case ISDN_CMD_IOCTL:
1162 memcpy(&a, c->parm.num, sizeof(ulong));
1163 arg = (char __user *)a;
1165 case ICN_IOCTL_SETMMIO:
1166 if (dev.memaddr != (a & 0x0ffc000)) {
1167 if (!request_mem_region(a & 0x0ffc000, 0x4000, "icn-isdn (all cards)")) {
1169 "icn: memory at 0x%08lx in use.\n",
1173 release_mem_region(a & 0x0ffc000, 0x4000);
1175 spin_lock_irqsave(&card->lock, flags);
1178 release_mem_region(dev.memaddr, 0x4000);
1181 dev.memaddr = a & 0x0ffc000;
1182 spin_unlock_irqrestore(&card->lock, flags);
1184 "icn: (%s) mmio set to 0x%08lx\n",
1189 case ICN_IOCTL_GETMMIO:
1190 return (long) dev.memaddr;
1191 case ICN_IOCTL_SETPORT:
1192 if (a == 0x300 || a == 0x310 || a == 0x320 || a == 0x330
1193 || a == 0x340 || a == 0x350 || a == 0x360 ||
1194 a == 0x308 || a == 0x318 || a == 0x328 || a == 0x338
1195 || a == 0x348 || a == 0x358 || a == 0x368) {
1196 if (card->port != (unsigned short) a) {
1197 if (!request_region((unsigned short) a, ICN_PORTLEN, "icn-isdn")) {
1199 "icn: (%s) ports 0x%03x-0x%03x in use.\n",
1200 CID, (int) a, (int) a + ICN_PORTLEN);
1203 release_region((unsigned short) a, ICN_PORTLEN);
1205 spin_lock_irqsave(&card->lock, flags);
1207 release_region(card->port, ICN_PORTLEN);
1208 card->port = (unsigned short) a;
1210 if (card->doubleS0) {
1211 card->other->port = (unsigned short) a;
1212 card->other->rvalid = 0;
1214 spin_unlock_irqrestore(&card->lock, flags);
1216 "icn: (%s) port set to 0x%03x\n",
1222 case ICN_IOCTL_GETPORT:
1223 return (int) card->port;
1224 case ICN_IOCTL_GETDOUBLE:
1225 return (int) card->doubleS0;
1226 case ICN_IOCTL_DEBUGVAR:
1227 if (copy_to_user(arg,
1233 ulong l = (ulong) & dev;
1234 if (copy_to_user(arg,
1240 case ICN_IOCTL_LOADBOOT:
1241 if (dev.firstload) {
1242 icn_disable_cards();
1246 return (icn_loadboot(arg, card));
1247 case ICN_IOCTL_LOADPROTO:
1249 if ((i = (icn_loadproto(arg, card))))
1252 i = icn_loadproto(arg + ICN_CODE_STAGE2, card->other);
1255 case ICN_IOCTL_ADDCARD:
1258 if (copy_from_user(&cdef,
1262 return (icn_addcard(cdef.port, cdef.id1, cdef.id2));
1264 case ICN_IOCTL_LEASEDCFG:
1266 if (!card->leased) {
1268 while (card->ptype == ISDN_PTYPE_UNKNOWN) {
1269 msleep_interruptible(ICN_BOOT_TIMEOUT1);
1271 msleep_interruptible(ICN_BOOT_TIMEOUT1);
1272 sprintf(cbuf, "00;FV2ON\n01;EAZ%c\n02;EAZ%c\n",
1273 (a & 1)?'1':'C', (a & 2)?'2':'C');
1274 i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
1276 "icn: (%s) Leased-line mode enabled\n",
1278 cmd.command = ISDN_STAT_RUN;
1279 cmd.driver = card->myid;
1281 card->interface.statcallb(&cmd);
1286 sprintf(cbuf, "00;FV2OFF\n");
1287 i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
1289 "icn: (%s) Leased-line mode disabled\n",
1291 cmd.command = ISDN_STAT_RUN;
1292 cmd.driver = card->myid;
1294 card->interface.statcallb(&cmd);
1303 if (!card->flags & ICN_FLAGS_RUNNING)
1307 if ((c->arg & 255) < ICN_BCH) {
1313 p = c->parm.setup.phone;
1314 if (*p == 's' || *p == 'S') {
1317 strcpy(dcode, "SCA");
1320 strcpy(dcode, "CAL");
1322 sprintf(cbuf, "%02d;D%s_R%s,%02d,%02d,%s\n", (int) (a + 1),
1323 dcode, dial, c->parm.setup.si1,
1324 c->parm.setup.si2, c->parm.setup.eazmsn);
1325 i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
1328 case ISDN_CMD_ACCEPTD:
1329 if (!card->flags & ICN_FLAGS_RUNNING)
1331 if (c->arg < ICN_BCH) {
1333 if (card->fw_rev >= 300) {
1334 switch (card->l2_proto[a - 1]) {
1335 case ISDN_PROTO_L2_X75I:
1336 sprintf(cbuf, "%02d;BX75\n", (int) a);
1338 case ISDN_PROTO_L2_HDLC:
1339 sprintf(cbuf, "%02d;BTRA\n", (int) a);
1342 i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
1344 sprintf(cbuf, "%02d;DCON_R\n", (int) a);
1345 i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
1348 case ISDN_CMD_ACCEPTB:
1349 if (!card->flags & ICN_FLAGS_RUNNING)
1351 if (c->arg < ICN_BCH) {
1353 if (card->fw_rev >= 300)
1354 switch (card->l2_proto[a - 1]) {
1355 case ISDN_PROTO_L2_X75I:
1356 sprintf(cbuf, "%02d;BCON_R,BX75\n", (int) a);
1358 case ISDN_PROTO_L2_HDLC:
1359 sprintf(cbuf, "%02d;BCON_R,BTRA\n", (int) a);
1362 sprintf(cbuf, "%02d;BCON_R\n", (int) a);
1363 i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
1366 case ISDN_CMD_HANGUP:
1367 if (!card->flags & ICN_FLAGS_RUNNING)
1369 if (c->arg < ICN_BCH) {
1371 sprintf(cbuf, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a, (int) a);
1372 i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
1375 case ISDN_CMD_SETEAZ:
1376 if (!card->flags & ICN_FLAGS_RUNNING)
1380 if (c->arg < ICN_BCH) {
1382 if (card->ptype == ISDN_PTYPE_EURO) {
1383 sprintf(cbuf, "%02d;MS%s%s\n", (int) a,
1384 c->parm.num[0] ? "N" : "ALL", c->parm.num);
1386 sprintf(cbuf, "%02d;EAZ%s\n", (int) a,
1387 c->parm.num[0] ? (char *)(c->parm.num) : "0123456789");
1388 i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
1391 case ISDN_CMD_CLREAZ:
1392 if (!card->flags & ICN_FLAGS_RUNNING)
1396 if (c->arg < ICN_BCH) {
1398 if (card->ptype == ISDN_PTYPE_EURO)
1399 sprintf(cbuf, "%02d;MSNC\n", (int) a);
1401 sprintf(cbuf, "%02d;EAZC\n", (int) a);
1402 i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
1405 case ISDN_CMD_SETL2:
1406 if (!card->flags & ICN_FLAGS_RUNNING)
1408 if ((c->arg & 255) < ICN_BCH) {
1411 case ISDN_PROTO_L2_X75I:
1412 sprintf(cbuf, "%02d;BX75\n", (int) (a & 255) + 1);
1414 case ISDN_PROTO_L2_HDLC:
1415 sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
1420 i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
1421 card->l2_proto[a & 255] = (a >> 8);
1424 case ISDN_CMD_SETL3:
1425 if (!card->flags & ICN_FLAGS_RUNNING)
1435 * Find card with given driverId
1437 static inline icn_card *
1438 icn_findcard(int driverid)
1440 icn_card *p = cards;
1443 if (p->myid == driverid)
1447 return (icn_card *) 0;
1451 * Wrapper functions for interface to linklevel
1454 if_command(isdn_ctrl * c)
1456 icn_card *card = icn_findcard(c->driver);
1459 return (icn_command(c, card));
1461 "icn: if_command %d called with invalid driverId %d!\n",
1462 c->command, c->driver);
1467 if_writecmd(const u_char __user *buf, int len, int id, int channel)
1469 icn_card *card = icn_findcard(id);
1472 if (!card->flags & ICN_FLAGS_RUNNING)
1474 return (icn_writecmd(buf, len, 1, card));
1477 "icn: if_writecmd called with invalid driverId!\n");
1482 if_readstatus(u_char __user *buf, int len, int id, int channel)
1484 icn_card *card = icn_findcard(id);
1487 if (!card->flags & ICN_FLAGS_RUNNING)
1489 return (icn_readstatus(buf, len, card));
1492 "icn: if_readstatus called with invalid driverId!\n");
1497 if_sendbuf(int id, int channel, int ack, struct sk_buff *skb)
1499 icn_card *card = icn_findcard(id);
1502 if (!card->flags & ICN_FLAGS_RUNNING)
1504 return (icn_sendbuf(channel, ack, skb, card));
1507 "icn: if_sendbuf called with invalid driverId!\n");
1512 * Allocate a new card-struct, initialize it
1513 * link it into cards-list and register it at linklevel.
1516 icn_initcard(int port, char *id)
1521 if (!(card = (icn_card *) kmalloc(sizeof(icn_card), GFP_KERNEL))) {
1523 "icn: (%s) Could not allocate card-struct.\n", id);
1524 return (icn_card *) 0;
1526 memset((char *) card, 0, sizeof(icn_card));
1527 spin_lock_init(&card->lock);
1529 card->interface.owner = THIS_MODULE;
1530 card->interface.hl_hdrlen = 1;
1531 card->interface.channels = ICN_BCH;
1532 card->interface.maxbufsize = 4000;
1533 card->interface.command = if_command;
1534 card->interface.writebuf_skb = if_sendbuf;
1535 card->interface.writecmd = if_writecmd;
1536 card->interface.readstat = if_readstatus;
1537 card->interface.features = ISDN_FEATURE_L2_X75I |
1538 ISDN_FEATURE_L2_HDLC |
1539 ISDN_FEATURE_L3_TRANS |
1540 ISDN_FEATURE_P_UNKNOWN;
1541 card->ptype = ISDN_PTYPE_UNKNOWN;
1542 strlcpy(card->interface.id, id, sizeof(card->interface.id));
1543 card->msg_buf_write = card->msg_buf;
1544 card->msg_buf_read = card->msg_buf;
1545 card->msg_buf_end = &card->msg_buf[sizeof(card->msg_buf) - 1];
1546 for (i = 0; i < ICN_BCH; i++) {
1547 card->l2_proto[i] = ISDN_PROTO_L2_X75I;
1548 skb_queue_head_init(&card->spqueue[i]);
1552 if (!register_isdn(&card->interface)) {
1553 cards = cards->next;
1555 "icn: Unable to register %s\n", id);
1557 return (icn_card *) 0;
1559 card->myid = card->interface.channels;
1560 sprintf(card->regname, "icn-isdn (%s)", card->interface.id);
1565 icn_addcard(int port, char *id1, char *id2)
1570 if (!(card = icn_initcard(port, id1))) {
1575 "icn: (%s) ICN-2B, port 0x%x added\n",
1576 card->interface.id, port);
1579 if (!(card2 = icn_initcard(port, id2))) {
1581 "icn: (%s) half ICN-4B, port 0x%x added\n",
1582 card2->interface.id, port);
1586 card->secondhalf = 0;
1587 card->other = card2;
1588 card2->doubleS0 = 1;
1589 card2->secondhalf = 1;
1590 card2->other = card;
1592 "icn: (%s and %s) ICN-4B, port 0x%x added\n",
1593 card->interface.id, card2->interface.id, port);
1599 icn_setup(char *line)
1603 static char sid[20];
1604 static char sid2[20];
1606 str = get_options(line, 2, ints);
1610 membase = (unsigned long)ints[2];
1614 if ((p = strchr(sid, ','))) {
1622 __setup("icn=", icn_setup);
1625 static int __init icn_init(void)
1630 memset(&dev, 0, sizeof(icn_dev));
1631 dev.memaddr = (membase & 0x0ffc000);
1635 spin_lock_init(&dev.devlock);
1637 if ((p = strchr(revision, ':'))) {
1639 p = strchr(rev, '$');
1642 strcpy(rev, " ??? ");
1643 printk(KERN_NOTICE "ICN-ISDN-driver Rev%smem=0x%08lx\n", rev,
1645 return (icn_addcard(portbase, icn_id, icn_id2));
1648 static void __exit icn_exit(void)
1651 icn_card *card = cards;
1652 icn_card *last, *tmpcard;
1654 unsigned long flags;
1658 cmd.command = ISDN_STAT_UNLOAD;
1659 cmd.driver = card->myid;
1660 card->interface.statcallb(&cmd);
1661 spin_lock_irqsave(&card->lock, flags);
1663 OUTB_P(0, ICN_RUN); /* Reset Controller */
1664 OUTB_P(0, ICN_MAPRAM); /* Disable RAM */
1665 if (card->secondhalf || (!card->doubleS0)) {
1666 release_region(card->port, ICN_PORTLEN);
1669 for (i = 0; i < ICN_BCH; i++)
1670 icn_free_queue(card, i);
1672 tmpcard = card->next;
1673 spin_unlock_irqrestore(&card->lock, flags);
1685 release_mem_region(dev.memaddr, 0x4000);
1687 printk(KERN_NOTICE "ICN-ISDN-driver unloaded\n");
1690 module_init(icn_init);
1691 module_exit(icn_exit);