1 /* $Id: isdnloop.c,v 1.11.6.7 2001/11/11 19:54:31 kai Exp $
3 * ISDN low-level module implementing a dummy loop driver.
5 * Copyright 1997 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.
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/interrupt.h>
15 #include <linux/init.h>
18 static char *revision = "$Revision: 1.11.6.7 $";
19 static char *isdnloop_id = "loop0";
21 MODULE_DESCRIPTION("ISDN4Linux: Pseudo Driver that simulates an ISDN card");
22 MODULE_AUTHOR("Fritz Elfert");
23 MODULE_LICENSE("GPL");
24 MODULE_PARM(isdnloop_id, "s");
25 MODULE_PARM_DESC(isdnloop_id, "ID-String of first card");
27 static int isdnloop_addcard(char *);
30 * Free queue completely.
33 * card = pointer to card struct
34 * channel = channel number
37 isdnloop_free_queue(isdnloop_card * card, int channel)
39 struct sk_buff_head *queue = &card->bqueue[channel];
41 skb_queue_purge(queue);
42 card->sndcount[channel] = 0;
46 * Send B-Channel data to another virtual card.
47 * This routine is called via timer-callback from isdnloop_pollbchan().
50 * card = pointer to card struct.
51 * ch = channel number (0-based)
54 isdnloop_bchan_send(isdnloop_card * card, int ch)
56 isdnloop_card *rcard = card->rcard[ch];
57 int rch = card->rch[ch], len, ack;
61 while (card->sndcount[ch]) {
62 if ((skb = skb_dequeue(&card->bqueue[ch]))) {
64 card->sndcount[ch] -= len;
65 ack = *(skb->head); /* used as scratch area */
66 cmd.driver = card->myid;
69 rcard->interface.rcvcallb_skb(rcard->myid, rch, skb);
71 printk(KERN_WARNING "isdnloop: no rcard, skb dropped\n");
75 cmd.command = ISDN_STAT_BSENT;
76 cmd.parm.length = len;
77 card->interface.statcallb(&cmd);
79 card->sndcount[ch] = 0;
84 * Send/Receive Data to/from the B-Channel.
85 * This routine is called via timer-callback.
86 * It schedules itself while any B-Channel is open.
89 * data = pointer to card struct, set by kernel timer.data
92 isdnloop_pollbchan(unsigned long data)
94 isdnloop_card *card = (isdnloop_card *) data;
97 if (card->flags & ISDNLOOP_FLAGS_B1ACTIVE)
98 isdnloop_bchan_send(card, 0);
99 if (card->flags & ISDNLOOP_FLAGS_B2ACTIVE)
100 isdnloop_bchan_send(card, 1);
101 if (card->flags & (ISDNLOOP_FLAGS_B1ACTIVE | ISDNLOOP_FLAGS_B2ACTIVE)) {
102 /* schedule b-channel polling again */
105 card->rb_timer.expires = jiffies + ISDNLOOP_TIMER_BCREAD;
106 add_timer(&card->rb_timer);
107 card->flags |= ISDNLOOP_FLAGS_RBTIMER;
108 restore_flags(flags);
110 card->flags &= ~ISDNLOOP_FLAGS_RBTIMER;
114 * Parse ICN-type setup string and fill fields of setup-struct
118 * setup = setup string, format: [caller-id],si1,si2,[called-id]
119 * cmd = pointer to struct to be filled.
122 isdnloop_parse_setup(char *setup, isdn_ctrl * cmd)
125 char *s = strchr(t, ',');
128 strlcpy(cmd->parm.setup.phone, t, sizeof(cmd->parm.setup.phone));
129 s = strchr(t = s, ',');
132 cmd->parm.setup.si1 = 0;
134 cmd->parm.setup.si1 = simple_strtoul(t, NULL, 10);
135 s = strchr(t = s, ',');
138 cmd->parm.setup.si2 = 0;
140 cmd->parm.setup.si2 =
141 simple_strtoul(t, NULL, 10);
142 strlcpy(cmd->parm.setup.eazmsn, s, sizeof(cmd->parm.setup.eazmsn));
143 cmd->parm.setup.plan = 0;
144 cmd->parm.setup.screen = 0;
147 typedef struct isdnloop_stat {
153 static isdnloop_stat isdnloop_stat_table[] =
155 {"BCON_", ISDN_STAT_BCONN, 1}, /* B-Channel connected */
156 {"BDIS_", ISDN_STAT_BHUP, 2}, /* B-Channel disconnected */
157 {"DCON_", ISDN_STAT_DCONN, 0}, /* D-Channel connected */
158 {"DDIS_", ISDN_STAT_DHUP, 0}, /* D-Channel disconnected */
159 {"DCAL_I", ISDN_STAT_ICALL, 3}, /* Incoming call dialup-line */
160 {"DSCA_I", ISDN_STAT_ICALL, 3}, /* Incoming call 1TR6-SPV */
161 {"FCALL", ISDN_STAT_ICALL, 4}, /* Leased line connection up */
162 {"CIF", ISDN_STAT_CINF, 5}, /* Charge-info, 1TR6-type */
163 {"AOC", ISDN_STAT_CINF, 6}, /* Charge-info, DSS1-type */
164 {"CAU", ISDN_STAT_CAUSE, 7}, /* Cause code */
165 {"TEI OK", ISDN_STAT_RUN, 0}, /* Card connected to wallplug */
166 {"E_L1: ACT FAIL", ISDN_STAT_BHUP, 8}, /* Layer-1 activation failed */
167 {"E_L2: DATA LIN", ISDN_STAT_BHUP, 8}, /* Layer-2 data link lost */
168 {"E_L1: ACTIVATION FAILED",
169 ISDN_STAT_BHUP, 8}, /* Layer-1 activation failed */
176 * Parse Status message-strings from virtual card.
177 * Depending on status, call statcallb for sending messages to upper
178 * levels. Also set/reset B-Channel active-flags.
181 * status = status string to parse.
182 * channel = channel where message comes from.
183 * card = card where message comes from.
186 isdnloop_parse_status(u_char * status, int channel, isdnloop_card * card)
188 isdnloop_stat *s = isdnloop_stat_table;
193 if (!strncmp(status, s->statstr, strlen(s->statstr))) {
194 cmd.command = s->command;
202 cmd.driver = card->myid;
207 card->flags |= (channel) ?
208 ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE;
212 card->flags &= ~((channel) ?
213 ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE);
214 isdnloop_free_queue(card, channel);
217 /* DCAL_I and DSCA_I */
218 isdnloop_parse_setup(status + 6, &cmd);
222 sprintf(cmd.parm.setup.phone, "LEASED%d", card->myid);
223 sprintf(cmd.parm.setup.eazmsn, "%d", channel + 1);
224 cmd.parm.setup.si1 = 7;
225 cmd.parm.setup.si2 = 0;
226 cmd.parm.setup.plan = 0;
227 cmd.parm.setup.screen = 0;
231 strlcpy(cmd.parm.num, status + 3, sizeof(cmd.parm.num));
235 snprintf(cmd.parm.num, sizeof(cmd.parm.num), "%d",
236 (int) simple_strtoul(status + 7, NULL, 16));
241 if (strlen(status) == 4)
242 snprintf(cmd.parm.num, sizeof(cmd.parm.num), "%s%c%c",
243 status + 2, *status, *(status + 1));
245 strlcpy(cmd.parm.num, status + 1, sizeof(cmd.parm.num));
248 /* Misc Errors on L1 and L2 */
249 card->flags &= ~ISDNLOOP_FLAGS_B1ACTIVE;
250 isdnloop_free_queue(card, 0);
252 cmd.driver = card->myid;
253 card->interface.statcallb(&cmd);
254 cmd.command = ISDN_STAT_DHUP;
256 cmd.driver = card->myid;
257 card->interface.statcallb(&cmd);
258 cmd.command = ISDN_STAT_BHUP;
259 card->flags &= ~ISDNLOOP_FLAGS_B2ACTIVE;
260 isdnloop_free_queue(card, 1);
262 cmd.driver = card->myid;
263 card->interface.statcallb(&cmd);
264 cmd.command = ISDN_STAT_DHUP;
266 cmd.driver = card->myid;
269 card->interface.statcallb(&cmd);
273 * Store a cwcharacter into ringbuffer for reading from /dev/isdnctrl
276 * card = pointer to card struct.
280 isdnloop_putmsg(isdnloop_card * card, unsigned char c)
286 *card->msg_buf_write++ = (c == 0xff) ? '\n' : c;
287 if (card->msg_buf_write == card->msg_buf_read) {
288 if (++card->msg_buf_read > card->msg_buf_end)
289 card->msg_buf_read = card->msg_buf;
291 if (card->msg_buf_write > card->msg_buf_end)
292 card->msg_buf_write = card->msg_buf;
293 restore_flags(flags);
297 * Poll a virtual cards message queue.
298 * If there are new status-replies from the card, copy them to
299 * ringbuffer for reading on /dev/isdnctrl and call
300 * isdnloop_parse_status() for processing them. Watch for special
301 * Firmware bootmessage and parse it, to get the D-Channel protocol.
302 * If there are B-Channels open, initiate a timer-callback to
303 * isdnloop_pollbchan().
304 * This routine is called periodically via timer interrupt.
307 * data = pointer to card struct
310 isdnloop_polldchan(unsigned long data)
312 isdnloop_card *card = (isdnloop_card *) data;
322 if ((skb = skb_dequeue(&card->dqueue)))
326 for (left = avail; left > 0; left--) {
329 isdnloop_putmsg(card, c);
330 card->imsg[card->iptr] = c;
335 isdnloop_putmsg(card, '\n');
336 card->imsg[card->iptr] = 0;
338 if (card->imsg[0] == '0' && card->imsg[1] >= '0' &&
339 card->imsg[1] <= '2' && card->imsg[2] == ';') {
340 ch = (card->imsg[1] - '0') - 1;
342 isdnloop_parse_status(p, ch, card);
345 if (!strncmp(p, "DRV1.", 5)) {
346 printk(KERN_INFO "isdnloop: (%s) %s\n", CID, p);
347 if (!strncmp(p + 7, "TC", 2)) {
348 card->ptype = ISDN_PTYPE_1TR6;
349 card->interface.features |= ISDN_FEATURE_P_1TR6;
351 "isdnloop: (%s) 1TR6-Protocol loaded and running\n", CID);
353 if (!strncmp(p + 7, "EC", 2)) {
354 card->ptype = ISDN_PTYPE_EURO;
355 card->interface.features |= ISDN_FEATURE_P_EURO;
357 "isdnloop: (%s) Euro-Protocol loaded and running\n", CID);
366 cmd.command = ISDN_STAT_STAVAIL;
367 cmd.driver = card->myid;
369 card->interface.statcallb(&cmd);
371 if (card->flags & (ISDNLOOP_FLAGS_B1ACTIVE | ISDNLOOP_FLAGS_B2ACTIVE))
372 if (!(card->flags & ISDNLOOP_FLAGS_RBTIMER)) {
373 /* schedule b-channel polling */
374 card->flags |= ISDNLOOP_FLAGS_RBTIMER;
377 del_timer(&card->rb_timer);
378 card->rb_timer.function = isdnloop_pollbchan;
379 card->rb_timer.data = (unsigned long) card;
380 card->rb_timer.expires = jiffies + ISDNLOOP_TIMER_BCREAD;
381 add_timer(&card->rb_timer);
382 restore_flags(flags);
387 card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD;
388 add_timer(&card->st_timer);
389 restore_flags(flags);
393 * Append a packet to the transmit buffer-queue.
396 * channel = Number of B-channel
397 * skb = packet to send.
398 * card = pointer to card-struct
400 * Number of bytes transferred, -E??? on error
403 isdnloop_sendbuf(int channel, struct sk_buff *skb, isdnloop_card * card)
407 struct sk_buff *nskb;
411 "isdnloop: Send packet too large\n");
415 if (!(card->flags & (channel) ? ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE))
417 if (card->sndcount[channel] > ISDNLOOP_MAX_SQUEUE)
421 nskb = dev_alloc_skb(skb->len);
423 memcpy(skb_put(nskb, len), skb->data, len);
424 skb_queue_tail(&card->bqueue[channel], nskb);
428 card->sndcount[channel] += len;
429 restore_flags(flags);
435 * Read the messages from the card's ringbuffer
438 * buf = pointer to buffer.
439 * len = number of bytes to read.
440 * user = flag, 1: called from userlevel 0: called from kernel.
441 * card = pointer to card struct.
443 * number of bytes actually transferred.
446 isdnloop_readstatus(u_char * buf, int len, int user, isdnloop_card * card)
451 for (p = buf, count = 0; count < len; p++, count++) {
452 if (card->msg_buf_read == card->msg_buf_write)
455 put_user(*card->msg_buf_read++, p);
457 *p = *card->msg_buf_read++;
458 if (card->msg_buf_read > card->msg_buf_end)
459 card->msg_buf_read = card->msg_buf;
465 * Simulate a card's response by appending it to the cards
469 * card = pointer to card struct.
470 * s = pointer to message-string.
471 * ch = channel: 0 = generic messages, 1 and 2 = D-channel messages.
473 * 0 on success, 1 on memory squeeze.
476 isdnloop_fake(isdnloop_card * card, char *s, int ch)
479 int len = strlen(s) + ((ch >= 0) ? 3 : 0);
481 if (!(skb = dev_alloc_skb(len))) {
482 printk(KERN_WARNING "isdnloop: Out of memory in isdnloop_fake\n");
486 sprintf(skb_put(skb, 3), "%02d;", ch);
487 memcpy(skb_put(skb, strlen(s)), s, strlen(s));
488 skb_queue_tail(&card->dqueue, skb);
492 static isdnloop_stat isdnloop_cmd_table[] =
494 {"BCON_R", 0, 1}, /* B-Channel connect */
495 {"BCON_I", 0, 17}, /* B-Channel connect ind */
496 {"BDIS_R", 0, 2}, /* B-Channel disconnect */
497 {"DDIS_R", 0, 3}, /* D-Channel disconnect */
498 {"DCON_R", 0, 16}, /* D-Channel connect */
499 {"DSCA_R", 0, 4}, /* Dial 1TR6-SPV */
500 {"DCAL_R", 0, 5}, /* Dial */
501 {"EAZC", 0, 6}, /* Clear EAZ listener */
502 {"EAZ", 0, 7}, /* Set EAZ listener */
503 {"SEEAZ", 0, 8}, /* Get EAZ listener */
504 {"MSN", 0, 9}, /* Set/Clear MSN listener */
505 {"MSALL", 0, 10}, /* Set multi MSN listeners */
506 {"SETSIL", 0, 11}, /* Set SI list */
507 {"SEESIL", 0, 12}, /* Get SI list */
508 {"SILC", 0, 13}, /* Clear SI list */
509 {"LOCK", 0, -1}, /* LOCK channel */
510 {"UNLOCK", 0, -1}, /* UNLOCK channel */
511 {"FV2ON", 1, 14}, /* Leased mode on */
512 {"FV2OFF", 1, 15}, /* Leased mode off */
519 * Simulate an error-response from a card.
522 * card = pointer to card struct.
525 isdnloop_fake_err(isdnloop_card * card)
529 sprintf(buf, "E%s", card->omsg);
530 isdnloop_fake(card, buf, -1);
531 isdnloop_fake(card, "NAK", -1);
534 static u_char ctable_eu[] =
535 {0x00, 0x11, 0x01, 0x12};
536 static u_char ctable_1t[] =
537 {0x00, 0x3b, 0x01, 0x3a};
540 * Assemble a simplified cause message depending on the
541 * D-channel protocol used.
544 * card = pointer to card struct.
545 * loc = location: 0 = local, 1 = remote.
546 * cau = cause: 1 = busy, 2 = nonexistent callerid, 3 = no user responding.
548 * Pointer to buffer containing the assembled message.
551 isdnloop_unicause(isdnloop_card * card, int loc, int cau)
555 switch (card->ptype) {
556 case ISDN_PTYPE_EURO:
557 sprintf(buf, "E%02X%02X", (loc) ? 4 : 2, ctable_eu[cau]);
559 case ISDN_PTYPE_1TR6:
560 sprintf(buf, "%02X44", ctable_1t[cau]);
569 * Release a virtual connection. Called from timer interrupt, when
570 * called party did not respond.
573 * card = pointer to card struct.
574 * ch = channel (0-based)
577 isdnloop_atimeout(isdnloop_card * card, int ch)
585 isdnloop_fake(card->rcard[ch], "DDIS_I", card->rch[ch] + 1);
586 card->rcard[ch]->rcard[card->rch[ch]] = NULL;
587 card->rcard[ch] = NULL;
589 isdnloop_fake(card, "DDIS_I", ch + 1);
590 /* No user responding */
591 sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 3));
592 isdnloop_fake(card, buf, ch + 1);
593 restore_flags(flags);
597 * Wrapper for isdnloop_atimeout().
600 isdnloop_atimeout0(unsigned long data)
602 isdnloop_card *card = (isdnloop_card *) data;
603 isdnloop_atimeout(card, 0);
607 * Wrapper for isdnloop_atimeout().
610 isdnloop_atimeout1(unsigned long data)
612 isdnloop_card *card = (isdnloop_card *) data;
613 isdnloop_atimeout(card, 1);
617 * Install a watchdog for a user, not responding.
620 * card = pointer to card struct.
621 * ch = channel to watch for.
624 isdnloop_start_ctimer(isdnloop_card * card, int ch)
630 init_timer(&card->c_timer[ch]);
631 card->c_timer[ch].expires = jiffies + ISDNLOOP_TIMER_ALERTWAIT;
633 card->c_timer[ch].function = isdnloop_atimeout1;
635 card->c_timer[ch].function = isdnloop_atimeout0;
636 card->c_timer[ch].data = (unsigned long) card;
637 add_timer(&card->c_timer[ch]);
638 restore_flags(flags);
642 * Kill a pending channel watchdog.
645 * card = pointer to card struct.
646 * ch = channel (0-based).
649 isdnloop_kill_ctimer(isdnloop_card * card, int ch)
655 del_timer(&card->c_timer[ch]);
656 restore_flags(flags);
659 static u_char si2bit[] =
660 {0, 1, 0, 0, 0, 2, 0, 4, 0, 0};
661 static u_char bit2si[] =
665 * Try finding a listener for an outgoing call.
668 * card = pointer to calling card.
669 * p = pointer to ICN-type setup-string.
670 * lch = channel of calling card.
671 * cmd = pointer to struct to be filled when parsing setup.
673 * 0 = found match, alerting should happen.
674 * 1 = found matching number but it is busy.
675 * 2 = no matching listener.
676 * 3 = found matching number but SI does not match.
679 isdnloop_try_call(isdnloop_card * card, char *p, int lch, isdn_ctrl * cmd)
681 isdnloop_card *cc = cards;
689 isdnloop_parse_setup(p, cmd);
691 for (ch = 0; ch < 2; ch++) {
692 /* Exclude ourself */
693 if ((cc == card) && (ch == lch))
697 case ISDN_PTYPE_EURO:
698 for (i = 0; i < 3; i++)
699 if (!(strcmp(cc->s0num[i], cmd->parm.setup.phone)))
702 case ISDN_PTYPE_1TR6:
705 sprintf(nbuf, "%s%c", cc->s0num[0], *e);
706 if (!(strcmp(nbuf, cmd->parm.setup.phone)))
715 if (!(cc->rcard[ch])) {
717 if (!(si2bit[cmd->parm.setup.si1] & cc->sil[ch])) {
718 restore_flags(flags);
721 /* ch is idle, si and number matches */
722 cc->rcard[ch] = card;
724 card->rcard[lch] = cc;
726 restore_flags(flags);
729 restore_flags(flags);
730 /* num matches, but busy */
742 * Depending on D-channel protocol and caller/called, modify
746 * card = pointer to card struct.
747 * phone = pointer phone number.
748 * caller = flag: 1 = caller, 0 = called.
750 * pointer to new phone number.
753 isdnloop_vstphone(isdnloop_card * card, char *phone, int caller)
756 static char nphone[30];
762 switch (card->ptype) {
763 case ISDN_PTYPE_EURO:
765 for (i = 0; i < 2; i++)
766 if (!(strcmp(card->s0num[i], phone)))
768 return (card->s0num[0]);
772 case ISDN_PTYPE_1TR6:
774 sprintf(nphone, "%s%c", card->s0num[0], phone[0]);
777 return (&phone[strlen(phone) - 1]);
784 * Parse an ICN-type command string sent to the 'card'.
785 * Perform misc. actions depending on the command.
788 * card = pointer to card struct.
791 isdnloop_parse_cmd(isdnloop_card * card)
793 char *p = card->omsg;
796 isdnloop_stat *s = isdnloop_cmd_table;
801 if ((card->omsg[0] != '0') && (card->omsg[2] != ';')) {
802 isdnloop_fake_err(card);
805 ch = card->omsg[1] - '0';
806 if ((ch < 0) || (ch > 2)) {
807 isdnloop_fake_err(card);
812 if (!strncmp(p, s->statstr, strlen(s->statstr))) {
814 if (s->command && (ch != 0)) {
815 isdnloop_fake_err(card);
827 if (card->rcard[ch - 1]) {
828 isdnloop_fake(card->rcard[ch - 1], "BCON_I",
829 card->rch[ch - 1] + 1);
830 isdnloop_fake(card, "BCON_C", ch);
835 if (card->rcard[ch - 1]) {
836 isdnloop_fake(card->rcard[ch - 1], "BCON_C",
837 card->rch[ch - 1] + 1);
842 isdnloop_fake(card, "BDIS_C", ch);
843 if (card->rcard[ch - 1]) {
844 isdnloop_fake(card->rcard[ch - 1], "BDIS_I",
845 card->rch[ch - 1] + 1);
850 isdnloop_kill_ctimer(card, ch - 1);
851 if (card->rcard[ch - 1]) {
852 isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
853 isdnloop_fake(card->rcard[ch - 1], "DCON_C",
854 card->rch[ch - 1] + 1);
855 isdnloop_fake(card, "DCON_C", ch);
860 isdnloop_kill_ctimer(card, ch - 1);
861 if (card->rcard[ch - 1]) {
862 isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
863 isdnloop_fake(card->rcard[ch - 1], "DDIS_I",
864 card->rch[ch - 1] + 1);
865 card->rcard[ch - 1] = NULL;
867 isdnloop_fake(card, "DDIS_C", ch);
870 /* 0x;DSCA_Rdd,yy,zz,oo */
871 if (card->ptype != ISDN_PTYPE_1TR6) {
872 isdnloop_fake_err(card);
877 /* 0x;DCAL_Rdd,yy,zz,oo */
879 switch (isdnloop_try_call(card, p, ch - 1, &cmd)) {
882 sprintf(buf, "D%s_I%s,%02d,%02d,%s",
883 (action == 4) ? "SCA" : "CAL",
884 isdnloop_vstphone(card, cmd.parm.setup.eazmsn, 1),
887 isdnloop_vstphone(card->rcard[ch - 1],
888 cmd.parm.setup.phone, 0));
889 isdnloop_fake(card->rcard[ch - 1], buf, card->rch[ch - 1] + 1);
892 /* si1 does not match, don't alert but start timer */
893 isdnloop_start_ctimer(card, ch - 1);
897 isdnloop_fake(card, "DDIS_I", ch);
898 sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 1));
899 isdnloop_fake(card, buf, ch);
903 isdnloop_fake(card, "DDIS_I", ch);
904 sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 2));
905 isdnloop_fake(card, buf, ch);
911 card->eazlist[ch - 1][0] = '\0';
916 strcpy(card->eazlist[ch - 1], p);
920 sprintf(buf, "EAZ-LIST: %s", card->eazlist[ch - 1]);
921 isdnloop_fake(card, buf, ch + 1);
933 while (strchr("0157", *p)) {
935 card->sil[ch - 1] |= si2bit[*p - '0'];
939 isdnloop_fake_err(card);
943 sprintf(buf, "SIN-LIST: ");
945 for (i = 0; i < 3; i++)
946 if (card->sil[ch - 1] & (1 << i))
947 p += sprintf(p, "%02d", bit2si[i]);
948 isdnloop_fake(card, buf, ch + 1);
952 card->sil[ch - 1] = 0;
964 * Put command-strings into the of the 'card'. In reality, execute them
965 * right in place by calling isdnloop_parse_cmd(). Also copy every
966 * command to the read message ringbuffer, preceeding it with a '>'.
967 * These mesagges can be read at /dev/isdnctrl.
970 * buf = pointer to command buffer.
971 * len = length of buffer data.
972 * user = flag: 1 = called form userlevel, 0 called from kernel.
973 * card = pointer to card struct.
975 * number of bytes transferred (currently always equals len).
978 isdnloop_writecmd(const u_char * buf, int len, int user, isdnloop_card * card)
992 if (copy_from_user(msg, buf, count))
995 memcpy(msg, buf, count);
996 isdnloop_putmsg(card, '>');
997 for (p = msg; count > 0; count--, p++) {
1000 isdnloop_putmsg(card, *p);
1001 card->omsg[card->optr] = *p;
1003 card->omsg[card->optr] = '\0';
1005 isdnloop_parse_cmd(card);
1007 isdnloop_putmsg(card, '>');
1011 if (card->optr < 59)
1017 cmd.command = ISDN_STAT_STAVAIL;
1018 cmd.driver = card->myid;
1020 card->interface.statcallb(&cmd);
1025 * Delete card's pending timers, send STOP to linklevel
1028 isdnloop_stopcard(isdnloop_card * card)
1030 unsigned long flags;
1035 if (card->flags & ISDNLOOP_FLAGS_RUNNING) {
1036 card->flags &= ~ISDNLOOP_FLAGS_RUNNING;
1037 del_timer(&card->st_timer);
1038 del_timer(&card->rb_timer);
1039 del_timer(&card->c_timer[0]);
1040 del_timer(&card->c_timer[1]);
1041 cmd.command = ISDN_STAT_STOP;
1042 cmd.driver = card->myid;
1043 card->interface.statcallb(&cmd);
1045 restore_flags(flags);
1049 * Stop all cards before unload.
1052 isdnloop_stopallcards(void)
1054 isdnloop_card *p = cards;
1057 isdnloop_stopcard(p);
1063 * Start a 'card'. Simulate card's boot message and set the phone
1064 * number(s) of the virtual 'S0-Interface'. Install D-channel
1068 * card = pointer to card struct.
1069 * sdefp = pointer to struct holding ioctl parameters.
1071 * 0 on success, -E??? otherwise.
1074 isdnloop_start(isdnloop_card * card, isdnloop_sdef * sdefp)
1076 unsigned long flags;
1080 if (card->flags & ISDNLOOP_FLAGS_RUNNING)
1082 if (copy_from_user((char *) &sdef, (char *) sdefp, sizeof(sdef)))
1086 switch (sdef.ptype) {
1087 case ISDN_PTYPE_EURO:
1088 if (isdnloop_fake(card, "DRV1.23EC-Q.931-CAPI-CNS-BASIS-20.02.96",
1090 restore_flags(flags);
1093 card->sil[0] = card->sil[1] = 4;
1094 if (isdnloop_fake(card, "TEI OK", 0)) {
1095 restore_flags(flags);
1098 for (i = 0; i < 3; i++)
1099 strcpy(card->s0num[i], sdef.num[i]);
1101 case ISDN_PTYPE_1TR6:
1102 if (isdnloop_fake(card, "DRV1.04TC-1TR6-CAPI-CNS-BASIS-29.11.95",
1104 restore_flags(flags);
1107 card->sil[0] = card->sil[1] = 4;
1108 if (isdnloop_fake(card, "TEI OK", 0)) {
1109 restore_flags(flags);
1112 strcpy(card->s0num[0], sdef.num[0]);
1113 card->s0num[1][0] = '\0';
1114 card->s0num[2][0] = '\0';
1117 restore_flags(flags);
1118 printk(KERN_WARNING "isdnloop: Illegal D-channel protocol %d\n",
1122 init_timer(&card->st_timer);
1123 card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD;
1124 card->st_timer.function = isdnloop_polldchan;
1125 card->st_timer.data = (unsigned long) card;
1126 add_timer(&card->st_timer);
1127 card->flags |= ISDNLOOP_FLAGS_RUNNING;
1128 restore_flags(flags);
1133 * Main handler for commands sent by linklevel.
1136 isdnloop_command(isdn_ctrl * c, isdnloop_card * card)
1144 switch (c->command) {
1145 case ISDN_CMD_IOCTL:
1146 memcpy(&a, c->parm.num, sizeof(ulong));
1148 case ISDNLOOP_IOCTL_DEBUGVAR:
1149 return (ulong) card;
1150 case ISDNLOOP_IOCTL_STARTUP:
1151 if ((i = verify_area(VERIFY_READ, (void *) a, sizeof(isdnloop_sdef))))
1153 return (isdnloop_start(card, (isdnloop_sdef *) a));
1155 case ISDNLOOP_IOCTL_ADDCARD:
1156 if (copy_from_user((char *)&cdef,
1160 return (isdnloop_addcard(cdef.id1));
1162 case ISDNLOOP_IOCTL_LEASEDCFG:
1164 if (!card->leased) {
1166 while (card->ptype == ISDN_PTYPE_UNKNOWN) {
1167 schedule_timeout(10);
1169 schedule_timeout(10);
1170 sprintf(cbuf, "00;FV2ON\n01;EAZ1\n02;EAZ2\n");
1171 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1173 "isdnloop: (%s) Leased-line mode enabled\n",
1175 cmd.command = ISDN_STAT_RUN;
1176 cmd.driver = card->myid;
1178 card->interface.statcallb(&cmd);
1183 sprintf(cbuf, "00;FV2OFF\n");
1184 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1186 "isdnloop: (%s) Leased-line mode disabled\n",
1188 cmd.command = ISDN_STAT_RUN;
1189 cmd.driver = card->myid;
1191 card->interface.statcallb(&cmd);
1200 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1204 if ((c->arg & 255) < ISDNLOOP_BCH) {
1210 p = c->parm.setup.phone;
1211 if (*p == 's' || *p == 'S') {
1214 strcpy(dcode, "SCA");
1217 strcpy(dcode, "CAL");
1219 sprintf(cbuf, "%02d;D%s_R%s,%02d,%02d,%s\n", (int) (a + 1),
1220 dcode, dial, c->parm.setup.si1,
1221 c->parm.setup.si2, c->parm.setup.eazmsn);
1222 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1225 case ISDN_CMD_ACCEPTD:
1226 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1228 if (c->arg < ISDNLOOP_BCH) {
1231 switch (card->l2_proto[a - 1]) {
1232 case ISDN_PROTO_L2_X75I:
1233 sprintf(cbuf, "%02d;BX75\n", (int) a);
1235 #ifdef CONFIG_ISDN_X25
1236 case ISDN_PROTO_L2_X25DTE:
1237 sprintf(cbuf, "%02d;BX2T\n", (int) a);
1239 case ISDN_PROTO_L2_X25DCE:
1240 sprintf(cbuf, "%02d;BX2C\n", (int) a);
1243 case ISDN_PROTO_L2_HDLC:
1244 sprintf(cbuf, "%02d;BTRA\n", (int) a);
1248 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1249 sprintf(cbuf, "%02d;DCON_R\n", (int) a);
1250 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1253 case ISDN_CMD_ACCEPTB:
1254 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1256 if (c->arg < ISDNLOOP_BCH) {
1258 switch (card->l2_proto[a - 1]) {
1259 case ISDN_PROTO_L2_X75I:
1260 sprintf(cbuf, "%02d;BCON_R,BX75\n", (int) a);
1262 #ifdef CONFIG_ISDN_X25
1263 case ISDN_PROTO_L2_X25DTE:
1264 sprintf(cbuf, "%02d;BCON_R,BX2T\n", (int) a);
1266 case ISDN_PROTO_L2_X25DCE:
1267 sprintf(cbuf, "%02d;BCON_R,BX2C\n", (int) a);
1270 case ISDN_PROTO_L2_HDLC:
1271 sprintf(cbuf, "%02d;BCON_R,BTRA\n", (int) a);
1274 sprintf(cbuf, "%02d;BCON_R\n", (int) a);
1276 printk(KERN_DEBUG "isdnloop writecmd '%s'\n", cbuf);
1277 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1279 case ISDN_CMD_HANGUP:
1280 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1282 if (c->arg < ISDNLOOP_BCH) {
1284 sprintf(cbuf, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a, (int) a);
1285 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1288 case ISDN_CMD_SETEAZ:
1289 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1293 if (c->arg < ISDNLOOP_BCH) {
1295 if (card->ptype == ISDN_PTYPE_EURO) {
1296 sprintf(cbuf, "%02d;MS%s%s\n", (int) a,
1297 c->parm.num[0] ? "N" : "ALL", c->parm.num);
1299 sprintf(cbuf, "%02d;EAZ%s\n", (int) a,
1300 c->parm.num[0] ? c->parm.num : (u_char *) "0123456789");
1301 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1304 case ISDN_CMD_CLREAZ:
1305 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1309 if (c->arg < ISDNLOOP_BCH) {
1311 if (card->ptype == ISDN_PTYPE_EURO)
1312 sprintf(cbuf, "%02d;MSNC\n", (int) a);
1314 sprintf(cbuf, "%02d;EAZC\n", (int) a);
1315 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1318 case ISDN_CMD_SETL2:
1319 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1321 if ((c->arg & 255) < ISDNLOOP_BCH) {
1324 case ISDN_PROTO_L2_X75I:
1325 sprintf(cbuf, "%02d;BX75\n", (int) (a & 255) + 1);
1327 #ifdef CONFIG_ISDN_X25
1328 case ISDN_PROTO_L2_X25DTE:
1329 sprintf(cbuf, "%02d;BX2T\n", (int) (a & 255) + 1);
1331 case ISDN_PROTO_L2_X25DCE:
1332 sprintf(cbuf, "%02d;BX2C\n", (int) (a & 255) + 1);
1335 case ISDN_PROTO_L2_HDLC:
1336 sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
1338 case ISDN_PROTO_L2_TRANS:
1339 sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
1344 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1345 card->l2_proto[a & 255] = (a >> 8);
1348 case ISDN_CMD_SETL3:
1349 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1360 * Find card with given driverId
1362 static inline isdnloop_card *
1363 isdnloop_findcard(int driverid)
1365 isdnloop_card *p = cards;
1368 if (p->myid == driverid)
1372 return (isdnloop_card *) 0;
1376 * Wrapper functions for interface to linklevel
1379 if_command(isdn_ctrl * c)
1381 isdnloop_card *card = isdnloop_findcard(c->driver);
1384 return (isdnloop_command(c, card));
1386 "isdnloop: if_command called with invalid driverId!\n");
1391 if_writecmd(const u_char * buf, int len, int user, int id, int channel)
1393 isdnloop_card *card = isdnloop_findcard(id);
1396 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1398 return (isdnloop_writecmd(buf, len, user, card));
1401 "isdnloop: if_writecmd called with invalid driverId!\n");
1406 if_readstatus(u_char * buf, int len, int user, int id, int channel)
1408 isdnloop_card *card = isdnloop_findcard(id);
1411 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1413 return (isdnloop_readstatus(buf, len, user, card));
1416 "isdnloop: if_readstatus called with invalid driverId!\n");
1421 if_sendbuf(int id, int channel, int ack, struct sk_buff *skb)
1423 isdnloop_card *card = isdnloop_findcard(id);
1426 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1428 /* ack request stored in skb scratch area */
1430 return (isdnloop_sendbuf(channel, skb, card));
1433 "isdnloop: if_sendbuf called with invalid driverId!\n");
1438 * Allocate a new card-struct, initialize it
1439 * link it into cards-list and register it at linklevel.
1441 static isdnloop_card *
1442 isdnloop_initcard(char *id)
1444 isdnloop_card *card;
1447 if (!(card = (isdnloop_card *) kmalloc(sizeof(isdnloop_card), GFP_KERNEL))) {
1449 "isdnloop: (%s) Could not allocate card-struct.\n", id);
1450 return (isdnloop_card *) 0;
1452 memset((char *) card, 0, sizeof(isdnloop_card));
1453 card->interface.owner = THIS_MODULE;
1454 card->interface.channels = ISDNLOOP_BCH;
1455 card->interface.hl_hdrlen = 1; /* scratch area for storing ack flag*/
1456 card->interface.maxbufsize = 4000;
1457 card->interface.command = if_command;
1458 card->interface.writebuf_skb = if_sendbuf;
1459 card->interface.writecmd = if_writecmd;
1460 card->interface.readstat = if_readstatus;
1461 card->interface.features = ISDN_FEATURE_L2_X75I |
1462 #ifdef CONFIG_ISDN_X25
1463 ISDN_FEATURE_L2_X25DTE |
1464 ISDN_FEATURE_L2_X25DCE |
1466 ISDN_FEATURE_L2_HDLC |
1467 ISDN_FEATURE_L3_TRANS |
1468 ISDN_FEATURE_P_UNKNOWN;
1469 card->ptype = ISDN_PTYPE_UNKNOWN;
1470 strlcpy(card->interface.id, id, sizeof(card->interface.id));
1471 card->msg_buf_write = card->msg_buf;
1472 card->msg_buf_read = card->msg_buf;
1473 card->msg_buf_end = &card->msg_buf[sizeof(card->msg_buf) - 1];
1474 for (i = 0; i < ISDNLOOP_BCH; i++) {
1475 card->l2_proto[i] = ISDN_PROTO_L2_X75I;
1476 skb_queue_head_init(&card->bqueue[i]);
1478 skb_queue_head_init(&card->dqueue);
1481 if (!register_isdn(&card->interface)) {
1482 cards = cards->next;
1484 "isdnloop: Unable to register %s\n", id);
1486 return (isdnloop_card *) 0;
1488 card->myid = card->interface.channels;
1493 isdnloop_addcard(char *id1)
1495 isdnloop_card *card;
1497 if (!(card = isdnloop_initcard(id1))) {
1501 "isdnloop: (%s) virtual card added\n",
1502 card->interface.id);
1512 if ((p = strchr(revision, ':'))) {
1514 p = strchr(rev, '$');
1517 strcpy(rev, " ??? ");
1518 printk(KERN_NOTICE "isdnloop-ISDN-driver Rev%s\n", rev);
1521 return (isdnloop_addcard(isdnloop_id));
1530 isdnloop_card *card = cards;
1531 isdnloop_card *last;
1534 isdnloop_stopallcards();
1536 cmd.command = ISDN_STAT_UNLOAD;
1537 cmd.driver = card->myid;
1538 card->interface.statcallb(&cmd);
1539 for (i = 0; i < ISDNLOOP_BCH; i++)
1540 isdnloop_free_queue(card, i);
1546 skb_queue_purge(&card->dqueue);
1550 printk(KERN_NOTICE "isdnloop-ISDN-driver unloaded\n");
1553 module_init(isdnloop_init);
1554 module_exit(isdnloop_exit);