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>
16 #include <linux/sched.h>
19 static char *revision = "$Revision: 1.11.6.7 $";
20 static char *isdnloop_id = "loop0";
22 MODULE_DESCRIPTION("ISDN4Linux: Pseudo Driver that simulates an ISDN card");
23 MODULE_AUTHOR("Fritz Elfert");
24 MODULE_LICENSE("GPL");
25 MODULE_PARM(isdnloop_id, "s");
26 MODULE_PARM_DESC(isdnloop_id, "ID-String of first card");
28 static int isdnloop_addcard(char *);
31 * Free queue completely.
34 * card = pointer to card struct
35 * channel = channel number
38 isdnloop_free_queue(isdnloop_card * card, int channel)
40 struct sk_buff_head *queue = &card->bqueue[channel];
42 skb_queue_purge(queue);
43 card->sndcount[channel] = 0;
47 * Send B-Channel data to another virtual card.
48 * This routine is called via timer-callback from isdnloop_pollbchan().
51 * card = pointer to card struct.
52 * ch = channel number (0-based)
55 isdnloop_bchan_send(isdnloop_card * card, int ch)
57 isdnloop_card *rcard = card->rcard[ch];
58 int rch = card->rch[ch], len, ack;
62 while (card->sndcount[ch]) {
63 if ((skb = skb_dequeue(&card->bqueue[ch]))) {
65 card->sndcount[ch] -= len;
66 ack = *(skb->head); /* used as scratch area */
67 cmd.driver = card->myid;
70 rcard->interface.rcvcallb_skb(rcard->myid, rch, skb);
72 printk(KERN_WARNING "isdnloop: no rcard, skb dropped\n");
76 cmd.command = ISDN_STAT_BSENT;
77 cmd.parm.length = len;
78 card->interface.statcallb(&cmd);
80 card->sndcount[ch] = 0;
85 * Send/Receive Data to/from the B-Channel.
86 * This routine is called via timer-callback.
87 * It schedules itself while any B-Channel is open.
90 * data = pointer to card struct, set by kernel timer.data
93 isdnloop_pollbchan(unsigned long data)
95 isdnloop_card *card = (isdnloop_card *) data;
98 if (card->flags & ISDNLOOP_FLAGS_B1ACTIVE)
99 isdnloop_bchan_send(card, 0);
100 if (card->flags & ISDNLOOP_FLAGS_B2ACTIVE)
101 isdnloop_bchan_send(card, 1);
102 if (card->flags & (ISDNLOOP_FLAGS_B1ACTIVE | ISDNLOOP_FLAGS_B2ACTIVE)) {
103 /* schedule b-channel polling again */
106 card->rb_timer.expires = jiffies + ISDNLOOP_TIMER_BCREAD;
107 add_timer(&card->rb_timer);
108 card->flags |= ISDNLOOP_FLAGS_RBTIMER;
109 restore_flags(flags);
111 card->flags &= ~ISDNLOOP_FLAGS_RBTIMER;
115 * Parse ICN-type setup string and fill fields of setup-struct
119 * setup = setup string, format: [caller-id],si1,si2,[called-id]
120 * cmd = pointer to struct to be filled.
123 isdnloop_parse_setup(char *setup, isdn_ctrl * cmd)
126 char *s = strchr(t, ',');
129 strlcpy(cmd->parm.setup.phone, t, sizeof(cmd->parm.setup.phone));
130 s = strchr(t = s, ',');
133 cmd->parm.setup.si1 = 0;
135 cmd->parm.setup.si1 = simple_strtoul(t, NULL, 10);
136 s = strchr(t = s, ',');
139 cmd->parm.setup.si2 = 0;
141 cmd->parm.setup.si2 =
142 simple_strtoul(t, NULL, 10);
143 strlcpy(cmd->parm.setup.eazmsn, s, sizeof(cmd->parm.setup.eazmsn));
144 cmd->parm.setup.plan = 0;
145 cmd->parm.setup.screen = 0;
148 typedef struct isdnloop_stat {
154 static isdnloop_stat isdnloop_stat_table[] =
156 {"BCON_", ISDN_STAT_BCONN, 1}, /* B-Channel connected */
157 {"BDIS_", ISDN_STAT_BHUP, 2}, /* B-Channel disconnected */
158 {"DCON_", ISDN_STAT_DCONN, 0}, /* D-Channel connected */
159 {"DDIS_", ISDN_STAT_DHUP, 0}, /* D-Channel disconnected */
160 {"DCAL_I", ISDN_STAT_ICALL, 3}, /* Incoming call dialup-line */
161 {"DSCA_I", ISDN_STAT_ICALL, 3}, /* Incoming call 1TR6-SPV */
162 {"FCALL", ISDN_STAT_ICALL, 4}, /* Leased line connection up */
163 {"CIF", ISDN_STAT_CINF, 5}, /* Charge-info, 1TR6-type */
164 {"AOC", ISDN_STAT_CINF, 6}, /* Charge-info, DSS1-type */
165 {"CAU", ISDN_STAT_CAUSE, 7}, /* Cause code */
166 {"TEI OK", ISDN_STAT_RUN, 0}, /* Card connected to wallplug */
167 {"E_L1: ACT FAIL", ISDN_STAT_BHUP, 8}, /* Layer-1 activation failed */
168 {"E_L2: DATA LIN", ISDN_STAT_BHUP, 8}, /* Layer-2 data link lost */
169 {"E_L1: ACTIVATION FAILED",
170 ISDN_STAT_BHUP, 8}, /* Layer-1 activation failed */
177 * Parse Status message-strings from virtual card.
178 * Depending on status, call statcallb for sending messages to upper
179 * levels. Also set/reset B-Channel active-flags.
182 * status = status string to parse.
183 * channel = channel where message comes from.
184 * card = card where message comes from.
187 isdnloop_parse_status(u_char * status, int channel, isdnloop_card * card)
189 isdnloop_stat *s = isdnloop_stat_table;
194 if (!strncmp(status, s->statstr, strlen(s->statstr))) {
195 cmd.command = s->command;
203 cmd.driver = card->myid;
208 card->flags |= (channel) ?
209 ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE;
213 card->flags &= ~((channel) ?
214 ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE);
215 isdnloop_free_queue(card, channel);
218 /* DCAL_I and DSCA_I */
219 isdnloop_parse_setup(status + 6, &cmd);
223 sprintf(cmd.parm.setup.phone, "LEASED%d", card->myid);
224 sprintf(cmd.parm.setup.eazmsn, "%d", channel + 1);
225 cmd.parm.setup.si1 = 7;
226 cmd.parm.setup.si2 = 0;
227 cmd.parm.setup.plan = 0;
228 cmd.parm.setup.screen = 0;
232 strlcpy(cmd.parm.num, status + 3, sizeof(cmd.parm.num));
236 snprintf(cmd.parm.num, sizeof(cmd.parm.num), "%d",
237 (int) simple_strtoul(status + 7, NULL, 16));
242 if (strlen(status) == 4)
243 snprintf(cmd.parm.num, sizeof(cmd.parm.num), "%s%c%c",
244 status + 2, *status, *(status + 1));
246 strlcpy(cmd.parm.num, status + 1, sizeof(cmd.parm.num));
249 /* Misc Errors on L1 and L2 */
250 card->flags &= ~ISDNLOOP_FLAGS_B1ACTIVE;
251 isdnloop_free_queue(card, 0);
253 cmd.driver = card->myid;
254 card->interface.statcallb(&cmd);
255 cmd.command = ISDN_STAT_DHUP;
257 cmd.driver = card->myid;
258 card->interface.statcallb(&cmd);
259 cmd.command = ISDN_STAT_BHUP;
260 card->flags &= ~ISDNLOOP_FLAGS_B2ACTIVE;
261 isdnloop_free_queue(card, 1);
263 cmd.driver = card->myid;
264 card->interface.statcallb(&cmd);
265 cmd.command = ISDN_STAT_DHUP;
267 cmd.driver = card->myid;
270 card->interface.statcallb(&cmd);
274 * Store a cwcharacter into ringbuffer for reading from /dev/isdnctrl
277 * card = pointer to card struct.
281 isdnloop_putmsg(isdnloop_card * card, unsigned char c)
287 *card->msg_buf_write++ = (c == 0xff) ? '\n' : c;
288 if (card->msg_buf_write == card->msg_buf_read) {
289 if (++card->msg_buf_read > card->msg_buf_end)
290 card->msg_buf_read = card->msg_buf;
292 if (card->msg_buf_write > card->msg_buf_end)
293 card->msg_buf_write = card->msg_buf;
294 restore_flags(flags);
298 * Poll a virtual cards message queue.
299 * If there are new status-replies from the card, copy them to
300 * ringbuffer for reading on /dev/isdnctrl and call
301 * isdnloop_parse_status() for processing them. Watch for special
302 * Firmware bootmessage and parse it, to get the D-Channel protocol.
303 * If there are B-Channels open, initiate a timer-callback to
304 * isdnloop_pollbchan().
305 * This routine is called periodically via timer interrupt.
308 * data = pointer to card struct
311 isdnloop_polldchan(unsigned long data)
313 isdnloop_card *card = (isdnloop_card *) data;
323 if ((skb = skb_dequeue(&card->dqueue)))
327 for (left = avail; left > 0; left--) {
330 isdnloop_putmsg(card, c);
331 card->imsg[card->iptr] = c;
336 isdnloop_putmsg(card, '\n');
337 card->imsg[card->iptr] = 0;
339 if (card->imsg[0] == '0' && card->imsg[1] >= '0' &&
340 card->imsg[1] <= '2' && card->imsg[2] == ';') {
341 ch = (card->imsg[1] - '0') - 1;
343 isdnloop_parse_status(p, ch, card);
346 if (!strncmp(p, "DRV1.", 5)) {
347 printk(KERN_INFO "isdnloop: (%s) %s\n", CID, p);
348 if (!strncmp(p + 7, "TC", 2)) {
349 card->ptype = ISDN_PTYPE_1TR6;
350 card->interface.features |= ISDN_FEATURE_P_1TR6;
352 "isdnloop: (%s) 1TR6-Protocol loaded and running\n", CID);
354 if (!strncmp(p + 7, "EC", 2)) {
355 card->ptype = ISDN_PTYPE_EURO;
356 card->interface.features |= ISDN_FEATURE_P_EURO;
358 "isdnloop: (%s) Euro-Protocol loaded and running\n", CID);
367 cmd.command = ISDN_STAT_STAVAIL;
368 cmd.driver = card->myid;
370 card->interface.statcallb(&cmd);
372 if (card->flags & (ISDNLOOP_FLAGS_B1ACTIVE | ISDNLOOP_FLAGS_B2ACTIVE))
373 if (!(card->flags & ISDNLOOP_FLAGS_RBTIMER)) {
374 /* schedule b-channel polling */
375 card->flags |= ISDNLOOP_FLAGS_RBTIMER;
378 del_timer(&card->rb_timer);
379 card->rb_timer.function = isdnloop_pollbchan;
380 card->rb_timer.data = (unsigned long) card;
381 card->rb_timer.expires = jiffies + ISDNLOOP_TIMER_BCREAD;
382 add_timer(&card->rb_timer);
383 restore_flags(flags);
388 card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD;
389 add_timer(&card->st_timer);
390 restore_flags(flags);
394 * Append a packet to the transmit buffer-queue.
397 * channel = Number of B-channel
398 * skb = packet to send.
399 * card = pointer to card-struct
401 * Number of bytes transferred, -E??? on error
404 isdnloop_sendbuf(int channel, struct sk_buff *skb, isdnloop_card * card)
408 struct sk_buff *nskb;
412 "isdnloop: Send packet too large\n");
416 if (!(card->flags & (channel) ? ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE))
418 if (card->sndcount[channel] > ISDNLOOP_MAX_SQUEUE)
422 nskb = dev_alloc_skb(skb->len);
424 memcpy(skb_put(nskb, len), skb->data, len);
425 skb_queue_tail(&card->bqueue[channel], nskb);
429 card->sndcount[channel] += len;
430 restore_flags(flags);
436 * Read the messages from the card's ringbuffer
439 * buf = pointer to buffer.
440 * len = number of bytes to read.
441 * user = flag, 1: called from userlevel 0: called from kernel.
442 * card = pointer to card struct.
444 * number of bytes actually transferred.
447 isdnloop_readstatus(u_char __user *buf, int len, isdnloop_card * card)
452 for (p = buf, count = 0; count < len; p++, count++) {
453 if (card->msg_buf_read == card->msg_buf_write)
455 put_user(*card->msg_buf_read++, p);
456 if (card->msg_buf_read > card->msg_buf_end)
457 card->msg_buf_read = card->msg_buf;
463 * Simulate a card's response by appending it to the cards
467 * card = pointer to card struct.
468 * s = pointer to message-string.
469 * ch = channel: 0 = generic messages, 1 and 2 = D-channel messages.
471 * 0 on success, 1 on memory squeeze.
474 isdnloop_fake(isdnloop_card * card, char *s, int ch)
477 int len = strlen(s) + ((ch >= 0) ? 3 : 0);
479 if (!(skb = dev_alloc_skb(len))) {
480 printk(KERN_WARNING "isdnloop: Out of memory in isdnloop_fake\n");
484 sprintf(skb_put(skb, 3), "%02d;", ch);
485 memcpy(skb_put(skb, strlen(s)), s, strlen(s));
486 skb_queue_tail(&card->dqueue, skb);
490 static isdnloop_stat isdnloop_cmd_table[] =
492 {"BCON_R", 0, 1}, /* B-Channel connect */
493 {"BCON_I", 0, 17}, /* B-Channel connect ind */
494 {"BDIS_R", 0, 2}, /* B-Channel disconnect */
495 {"DDIS_R", 0, 3}, /* D-Channel disconnect */
496 {"DCON_R", 0, 16}, /* D-Channel connect */
497 {"DSCA_R", 0, 4}, /* Dial 1TR6-SPV */
498 {"DCAL_R", 0, 5}, /* Dial */
499 {"EAZC", 0, 6}, /* Clear EAZ listener */
500 {"EAZ", 0, 7}, /* Set EAZ listener */
501 {"SEEAZ", 0, 8}, /* Get EAZ listener */
502 {"MSN", 0, 9}, /* Set/Clear MSN listener */
503 {"MSALL", 0, 10}, /* Set multi MSN listeners */
504 {"SETSIL", 0, 11}, /* Set SI list */
505 {"SEESIL", 0, 12}, /* Get SI list */
506 {"SILC", 0, 13}, /* Clear SI list */
507 {"LOCK", 0, -1}, /* LOCK channel */
508 {"UNLOCK", 0, -1}, /* UNLOCK channel */
509 {"FV2ON", 1, 14}, /* Leased mode on */
510 {"FV2OFF", 1, 15}, /* Leased mode off */
517 * Simulate an error-response from a card.
520 * card = pointer to card struct.
523 isdnloop_fake_err(isdnloop_card * card)
527 sprintf(buf, "E%s", card->omsg);
528 isdnloop_fake(card, buf, -1);
529 isdnloop_fake(card, "NAK", -1);
532 static u_char ctable_eu[] =
533 {0x00, 0x11, 0x01, 0x12};
534 static u_char ctable_1t[] =
535 {0x00, 0x3b, 0x01, 0x3a};
538 * Assemble a simplified cause message depending on the
539 * D-channel protocol used.
542 * card = pointer to card struct.
543 * loc = location: 0 = local, 1 = remote.
544 * cau = cause: 1 = busy, 2 = nonexistent callerid, 3 = no user responding.
546 * Pointer to buffer containing the assembled message.
549 isdnloop_unicause(isdnloop_card * card, int loc, int cau)
553 switch (card->ptype) {
554 case ISDN_PTYPE_EURO:
555 sprintf(buf, "E%02X%02X", (loc) ? 4 : 2, ctable_eu[cau]);
557 case ISDN_PTYPE_1TR6:
558 sprintf(buf, "%02X44", ctable_1t[cau]);
567 * Release a virtual connection. Called from timer interrupt, when
568 * called party did not respond.
571 * card = pointer to card struct.
572 * ch = channel (0-based)
575 isdnloop_atimeout(isdnloop_card * card, int ch)
583 isdnloop_fake(card->rcard[ch], "DDIS_I", card->rch[ch] + 1);
584 card->rcard[ch]->rcard[card->rch[ch]] = NULL;
585 card->rcard[ch] = NULL;
587 isdnloop_fake(card, "DDIS_I", ch + 1);
588 /* No user responding */
589 sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 3));
590 isdnloop_fake(card, buf, ch + 1);
591 restore_flags(flags);
595 * Wrapper for isdnloop_atimeout().
598 isdnloop_atimeout0(unsigned long data)
600 isdnloop_card *card = (isdnloop_card *) data;
601 isdnloop_atimeout(card, 0);
605 * Wrapper for isdnloop_atimeout().
608 isdnloop_atimeout1(unsigned long data)
610 isdnloop_card *card = (isdnloop_card *) data;
611 isdnloop_atimeout(card, 1);
615 * Install a watchdog for a user, not responding.
618 * card = pointer to card struct.
619 * ch = channel to watch for.
622 isdnloop_start_ctimer(isdnloop_card * card, int ch)
628 init_timer(&card->c_timer[ch]);
629 card->c_timer[ch].expires = jiffies + ISDNLOOP_TIMER_ALERTWAIT;
631 card->c_timer[ch].function = isdnloop_atimeout1;
633 card->c_timer[ch].function = isdnloop_atimeout0;
634 card->c_timer[ch].data = (unsigned long) card;
635 add_timer(&card->c_timer[ch]);
636 restore_flags(flags);
640 * Kill a pending channel watchdog.
643 * card = pointer to card struct.
644 * ch = channel (0-based).
647 isdnloop_kill_ctimer(isdnloop_card * card, int ch)
653 del_timer(&card->c_timer[ch]);
654 restore_flags(flags);
657 static u_char si2bit[] =
658 {0, 1, 0, 0, 0, 2, 0, 4, 0, 0};
659 static u_char bit2si[] =
663 * Try finding a listener for an outgoing call.
666 * card = pointer to calling card.
667 * p = pointer to ICN-type setup-string.
668 * lch = channel of calling card.
669 * cmd = pointer to struct to be filled when parsing setup.
671 * 0 = found match, alerting should happen.
672 * 1 = found matching number but it is busy.
673 * 2 = no matching listener.
674 * 3 = found matching number but SI does not match.
677 isdnloop_try_call(isdnloop_card * card, char *p, int lch, isdn_ctrl * cmd)
679 isdnloop_card *cc = cards;
687 isdnloop_parse_setup(p, cmd);
689 for (ch = 0; ch < 2; ch++) {
690 /* Exclude ourself */
691 if ((cc == card) && (ch == lch))
695 case ISDN_PTYPE_EURO:
696 for (i = 0; i < 3; i++)
697 if (!(strcmp(cc->s0num[i], cmd->parm.setup.phone)))
700 case ISDN_PTYPE_1TR6:
703 sprintf(nbuf, "%s%c", cc->s0num[0], *e);
704 if (!(strcmp(nbuf, cmd->parm.setup.phone)))
713 if (!(cc->rcard[ch])) {
715 if (!(si2bit[cmd->parm.setup.si1] & cc->sil[ch])) {
716 restore_flags(flags);
719 /* ch is idle, si and number matches */
720 cc->rcard[ch] = card;
722 card->rcard[lch] = cc;
724 restore_flags(flags);
727 restore_flags(flags);
728 /* num matches, but busy */
740 * Depending on D-channel protocol and caller/called, modify
744 * card = pointer to card struct.
745 * phone = pointer phone number.
746 * caller = flag: 1 = caller, 0 = called.
748 * pointer to new phone number.
751 isdnloop_vstphone(isdnloop_card * card, char *phone, int caller)
754 static char nphone[30];
760 switch (card->ptype) {
761 case ISDN_PTYPE_EURO:
763 for (i = 0; i < 2; i++)
764 if (!(strcmp(card->s0num[i], phone)))
766 return (card->s0num[0]);
770 case ISDN_PTYPE_1TR6:
772 sprintf(nphone, "%s%c", card->s0num[0], phone[0]);
775 return (&phone[strlen(phone) - 1]);
782 * Parse an ICN-type command string sent to the 'card'.
783 * Perform misc. actions depending on the command.
786 * card = pointer to card struct.
789 isdnloop_parse_cmd(isdnloop_card * card)
791 char *p = card->omsg;
794 isdnloop_stat *s = isdnloop_cmd_table;
799 if ((card->omsg[0] != '0') && (card->omsg[2] != ';')) {
800 isdnloop_fake_err(card);
803 ch = card->omsg[1] - '0';
804 if ((ch < 0) || (ch > 2)) {
805 isdnloop_fake_err(card);
810 if (!strncmp(p, s->statstr, strlen(s->statstr))) {
812 if (s->command && (ch != 0)) {
813 isdnloop_fake_err(card);
825 if (card->rcard[ch - 1]) {
826 isdnloop_fake(card->rcard[ch - 1], "BCON_I",
827 card->rch[ch - 1] + 1);
828 isdnloop_fake(card, "BCON_C", ch);
833 if (card->rcard[ch - 1]) {
834 isdnloop_fake(card->rcard[ch - 1], "BCON_C",
835 card->rch[ch - 1] + 1);
840 isdnloop_fake(card, "BDIS_C", ch);
841 if (card->rcard[ch - 1]) {
842 isdnloop_fake(card->rcard[ch - 1], "BDIS_I",
843 card->rch[ch - 1] + 1);
848 isdnloop_kill_ctimer(card, ch - 1);
849 if (card->rcard[ch - 1]) {
850 isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
851 isdnloop_fake(card->rcard[ch - 1], "DCON_C",
852 card->rch[ch - 1] + 1);
853 isdnloop_fake(card, "DCON_C", ch);
858 isdnloop_kill_ctimer(card, ch - 1);
859 if (card->rcard[ch - 1]) {
860 isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
861 isdnloop_fake(card->rcard[ch - 1], "DDIS_I",
862 card->rch[ch - 1] + 1);
863 card->rcard[ch - 1] = NULL;
865 isdnloop_fake(card, "DDIS_C", ch);
868 /* 0x;DSCA_Rdd,yy,zz,oo */
869 if (card->ptype != ISDN_PTYPE_1TR6) {
870 isdnloop_fake_err(card);
875 /* 0x;DCAL_Rdd,yy,zz,oo */
877 switch (isdnloop_try_call(card, p, ch - 1, &cmd)) {
880 sprintf(buf, "D%s_I%s,%02d,%02d,%s",
881 (action == 4) ? "SCA" : "CAL",
882 isdnloop_vstphone(card, cmd.parm.setup.eazmsn, 1),
885 isdnloop_vstphone(card->rcard[ch - 1],
886 cmd.parm.setup.phone, 0));
887 isdnloop_fake(card->rcard[ch - 1], buf, card->rch[ch - 1] + 1);
890 /* si1 does not match, don't alert but start timer */
891 isdnloop_start_ctimer(card, ch - 1);
895 isdnloop_fake(card, "DDIS_I", ch);
896 sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 1));
897 isdnloop_fake(card, buf, ch);
901 isdnloop_fake(card, "DDIS_I", ch);
902 sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 2));
903 isdnloop_fake(card, buf, ch);
909 card->eazlist[ch - 1][0] = '\0';
914 strcpy(card->eazlist[ch - 1], p);
918 sprintf(buf, "EAZ-LIST: %s", card->eazlist[ch - 1]);
919 isdnloop_fake(card, buf, ch + 1);
931 while (strchr("0157", *p)) {
933 card->sil[ch - 1] |= si2bit[*p - '0'];
937 isdnloop_fake_err(card);
941 sprintf(buf, "SIN-LIST: ");
943 for (i = 0; i < 3; i++)
944 if (card->sil[ch - 1] & (1 << i))
945 p += sprintf(p, "%02d", bit2si[i]);
946 isdnloop_fake(card, buf, ch + 1);
950 card->sil[ch - 1] = 0;
962 * Put command-strings into the of the 'card'. In reality, execute them
963 * right in place by calling isdnloop_parse_cmd(). Also copy every
964 * command to the read message ringbuffer, preceeding it with a '>'.
965 * These mesagges can be read at /dev/isdnctrl.
968 * buf = pointer to command buffer.
969 * len = length of buffer data.
970 * user = flag: 1 = called form userlevel, 0 called from kernel.
971 * card = pointer to card struct.
973 * number of bytes transferred (currently always equals len).
976 isdnloop_writecmd(const u_char * buf, int len, int user, isdnloop_card * card)
990 if (copy_from_user(msg, buf, count))
993 memcpy(msg, buf, count);
994 isdnloop_putmsg(card, '>');
995 for (p = msg; count > 0; count--, p++) {
998 isdnloop_putmsg(card, *p);
999 card->omsg[card->optr] = *p;
1001 card->omsg[card->optr] = '\0';
1003 isdnloop_parse_cmd(card);
1005 isdnloop_putmsg(card, '>');
1009 if (card->optr < 59)
1015 cmd.command = ISDN_STAT_STAVAIL;
1016 cmd.driver = card->myid;
1018 card->interface.statcallb(&cmd);
1023 * Delete card's pending timers, send STOP to linklevel
1026 isdnloop_stopcard(isdnloop_card * card)
1028 unsigned long flags;
1033 if (card->flags & ISDNLOOP_FLAGS_RUNNING) {
1034 card->flags &= ~ISDNLOOP_FLAGS_RUNNING;
1035 del_timer(&card->st_timer);
1036 del_timer(&card->rb_timer);
1037 del_timer(&card->c_timer[0]);
1038 del_timer(&card->c_timer[1]);
1039 cmd.command = ISDN_STAT_STOP;
1040 cmd.driver = card->myid;
1041 card->interface.statcallb(&cmd);
1043 restore_flags(flags);
1047 * Stop all cards before unload.
1050 isdnloop_stopallcards(void)
1052 isdnloop_card *p = cards;
1055 isdnloop_stopcard(p);
1061 * Start a 'card'. Simulate card's boot message and set the phone
1062 * number(s) of the virtual 'S0-Interface'. Install D-channel
1066 * card = pointer to card struct.
1067 * sdefp = pointer to struct holding ioctl parameters.
1069 * 0 on success, -E??? otherwise.
1072 isdnloop_start(isdnloop_card * card, isdnloop_sdef * sdefp)
1074 unsigned long flags;
1078 if (card->flags & ISDNLOOP_FLAGS_RUNNING)
1080 if (copy_from_user((char *) &sdef, (char *) sdefp, sizeof(sdef)))
1084 switch (sdef.ptype) {
1085 case ISDN_PTYPE_EURO:
1086 if (isdnloop_fake(card, "DRV1.23EC-Q.931-CAPI-CNS-BASIS-20.02.96",
1088 restore_flags(flags);
1091 card->sil[0] = card->sil[1] = 4;
1092 if (isdnloop_fake(card, "TEI OK", 0)) {
1093 restore_flags(flags);
1096 for (i = 0; i < 3; i++)
1097 strcpy(card->s0num[i], sdef.num[i]);
1099 case ISDN_PTYPE_1TR6:
1100 if (isdnloop_fake(card, "DRV1.04TC-1TR6-CAPI-CNS-BASIS-29.11.95",
1102 restore_flags(flags);
1105 card->sil[0] = card->sil[1] = 4;
1106 if (isdnloop_fake(card, "TEI OK", 0)) {
1107 restore_flags(flags);
1110 strcpy(card->s0num[0], sdef.num[0]);
1111 card->s0num[1][0] = '\0';
1112 card->s0num[2][0] = '\0';
1115 restore_flags(flags);
1116 printk(KERN_WARNING "isdnloop: Illegal D-channel protocol %d\n",
1120 init_timer(&card->st_timer);
1121 card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD;
1122 card->st_timer.function = isdnloop_polldchan;
1123 card->st_timer.data = (unsigned long) card;
1124 add_timer(&card->st_timer);
1125 card->flags |= ISDNLOOP_FLAGS_RUNNING;
1126 restore_flags(flags);
1131 * Main handler for commands sent by linklevel.
1134 isdnloop_command(isdn_ctrl * c, isdnloop_card * card)
1142 switch (c->command) {
1143 case ISDN_CMD_IOCTL:
1144 memcpy(&a, c->parm.num, sizeof(ulong));
1146 case ISDNLOOP_IOCTL_DEBUGVAR:
1147 return (ulong) card;
1148 case ISDNLOOP_IOCTL_STARTUP:
1149 if (!access_ok(VERIFY_READ, (void *) a, sizeof(isdnloop_sdef)))
1151 return (isdnloop_start(card, (isdnloop_sdef *) a));
1153 case ISDNLOOP_IOCTL_ADDCARD:
1154 if (copy_from_user((char *)&cdef,
1158 return (isdnloop_addcard(cdef.id1));
1160 case ISDNLOOP_IOCTL_LEASEDCFG:
1162 if (!card->leased) {
1164 while (card->ptype == ISDN_PTYPE_UNKNOWN)
1165 schedule_timeout_interruptible(10);
1166 schedule_timeout_interruptible(10);
1167 sprintf(cbuf, "00;FV2ON\n01;EAZ1\n02;EAZ2\n");
1168 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1170 "isdnloop: (%s) Leased-line mode enabled\n",
1172 cmd.command = ISDN_STAT_RUN;
1173 cmd.driver = card->myid;
1175 card->interface.statcallb(&cmd);
1180 sprintf(cbuf, "00;FV2OFF\n");
1181 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1183 "isdnloop: (%s) Leased-line mode disabled\n",
1185 cmd.command = ISDN_STAT_RUN;
1186 cmd.driver = card->myid;
1188 card->interface.statcallb(&cmd);
1197 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1201 if ((c->arg & 255) < ISDNLOOP_BCH) {
1207 p = c->parm.setup.phone;
1208 if (*p == 's' || *p == 'S') {
1211 strcpy(dcode, "SCA");
1214 strcpy(dcode, "CAL");
1216 sprintf(cbuf, "%02d;D%s_R%s,%02d,%02d,%s\n", (int) (a + 1),
1217 dcode, dial, c->parm.setup.si1,
1218 c->parm.setup.si2, c->parm.setup.eazmsn);
1219 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1222 case ISDN_CMD_ACCEPTD:
1223 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1225 if (c->arg < ISDNLOOP_BCH) {
1228 switch (card->l2_proto[a - 1]) {
1229 case ISDN_PROTO_L2_X75I:
1230 sprintf(cbuf, "%02d;BX75\n", (int) a);
1232 #ifdef CONFIG_ISDN_X25
1233 case ISDN_PROTO_L2_X25DTE:
1234 sprintf(cbuf, "%02d;BX2T\n", (int) a);
1236 case ISDN_PROTO_L2_X25DCE:
1237 sprintf(cbuf, "%02d;BX2C\n", (int) a);
1240 case ISDN_PROTO_L2_HDLC:
1241 sprintf(cbuf, "%02d;BTRA\n", (int) a);
1245 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1246 sprintf(cbuf, "%02d;DCON_R\n", (int) a);
1247 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1250 case ISDN_CMD_ACCEPTB:
1251 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1253 if (c->arg < ISDNLOOP_BCH) {
1255 switch (card->l2_proto[a - 1]) {
1256 case ISDN_PROTO_L2_X75I:
1257 sprintf(cbuf, "%02d;BCON_R,BX75\n", (int) a);
1259 #ifdef CONFIG_ISDN_X25
1260 case ISDN_PROTO_L2_X25DTE:
1261 sprintf(cbuf, "%02d;BCON_R,BX2T\n", (int) a);
1263 case ISDN_PROTO_L2_X25DCE:
1264 sprintf(cbuf, "%02d;BCON_R,BX2C\n", (int) a);
1267 case ISDN_PROTO_L2_HDLC:
1268 sprintf(cbuf, "%02d;BCON_R,BTRA\n", (int) a);
1271 sprintf(cbuf, "%02d;BCON_R\n", (int) a);
1273 printk(KERN_DEBUG "isdnloop writecmd '%s'\n", cbuf);
1274 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1276 case ISDN_CMD_HANGUP:
1277 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1279 if (c->arg < ISDNLOOP_BCH) {
1281 sprintf(cbuf, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a, (int) a);
1282 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1285 case ISDN_CMD_SETEAZ:
1286 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1290 if (c->arg < ISDNLOOP_BCH) {
1292 if (card->ptype == ISDN_PTYPE_EURO) {
1293 sprintf(cbuf, "%02d;MS%s%s\n", (int) a,
1294 c->parm.num[0] ? "N" : "ALL", c->parm.num);
1296 sprintf(cbuf, "%02d;EAZ%s\n", (int) a,
1297 c->parm.num[0] ? c->parm.num : (u_char *) "0123456789");
1298 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1301 case ISDN_CMD_CLREAZ:
1302 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1306 if (c->arg < ISDNLOOP_BCH) {
1308 if (card->ptype == ISDN_PTYPE_EURO)
1309 sprintf(cbuf, "%02d;MSNC\n", (int) a);
1311 sprintf(cbuf, "%02d;EAZC\n", (int) a);
1312 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1315 case ISDN_CMD_SETL2:
1316 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1318 if ((c->arg & 255) < ISDNLOOP_BCH) {
1321 case ISDN_PROTO_L2_X75I:
1322 sprintf(cbuf, "%02d;BX75\n", (int) (a & 255) + 1);
1324 #ifdef CONFIG_ISDN_X25
1325 case ISDN_PROTO_L2_X25DTE:
1326 sprintf(cbuf, "%02d;BX2T\n", (int) (a & 255) + 1);
1328 case ISDN_PROTO_L2_X25DCE:
1329 sprintf(cbuf, "%02d;BX2C\n", (int) (a & 255) + 1);
1332 case ISDN_PROTO_L2_HDLC:
1333 sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
1335 case ISDN_PROTO_L2_TRANS:
1336 sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
1341 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1342 card->l2_proto[a & 255] = (a >> 8);
1345 case ISDN_CMD_SETL3:
1346 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1357 * Find card with given driverId
1359 static inline isdnloop_card *
1360 isdnloop_findcard(int driverid)
1362 isdnloop_card *p = cards;
1365 if (p->myid == driverid)
1369 return (isdnloop_card *) 0;
1373 * Wrapper functions for interface to linklevel
1376 if_command(isdn_ctrl * c)
1378 isdnloop_card *card = isdnloop_findcard(c->driver);
1381 return (isdnloop_command(c, card));
1383 "isdnloop: if_command called with invalid driverId!\n");
1388 if_writecmd(const u_char __user *buf, int len, int id, int channel)
1390 isdnloop_card *card = isdnloop_findcard(id);
1393 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1395 return (isdnloop_writecmd(buf, len, 1, card));
1398 "isdnloop: if_writecmd called with invalid driverId!\n");
1403 if_readstatus(u_char __user *buf, int len, int id, int channel)
1405 isdnloop_card *card = isdnloop_findcard(id);
1408 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1410 return (isdnloop_readstatus(buf, len, card));
1413 "isdnloop: if_readstatus called with invalid driverId!\n");
1418 if_sendbuf(int id, int channel, int ack, struct sk_buff *skb)
1420 isdnloop_card *card = isdnloop_findcard(id);
1423 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1425 /* ack request stored in skb scratch area */
1427 return (isdnloop_sendbuf(channel, skb, card));
1430 "isdnloop: if_sendbuf called with invalid driverId!\n");
1435 * Allocate a new card-struct, initialize it
1436 * link it into cards-list and register it at linklevel.
1438 static isdnloop_card *
1439 isdnloop_initcard(char *id)
1441 isdnloop_card *card;
1444 if (!(card = (isdnloop_card *) kmalloc(sizeof(isdnloop_card), GFP_KERNEL))) {
1446 "isdnloop: (%s) Could not allocate card-struct.\n", id);
1447 return (isdnloop_card *) 0;
1449 memset((char *) card, 0, sizeof(isdnloop_card));
1450 card->interface.owner = THIS_MODULE;
1451 card->interface.channels = ISDNLOOP_BCH;
1452 card->interface.hl_hdrlen = 1; /* scratch area for storing ack flag*/
1453 card->interface.maxbufsize = 4000;
1454 card->interface.command = if_command;
1455 card->interface.writebuf_skb = if_sendbuf;
1456 card->interface.writecmd = if_writecmd;
1457 card->interface.readstat = if_readstatus;
1458 card->interface.features = ISDN_FEATURE_L2_X75I |
1459 #ifdef CONFIG_ISDN_X25
1460 ISDN_FEATURE_L2_X25DTE |
1461 ISDN_FEATURE_L2_X25DCE |
1463 ISDN_FEATURE_L2_HDLC |
1464 ISDN_FEATURE_L3_TRANS |
1465 ISDN_FEATURE_P_UNKNOWN;
1466 card->ptype = ISDN_PTYPE_UNKNOWN;
1467 strlcpy(card->interface.id, id, sizeof(card->interface.id));
1468 card->msg_buf_write = card->msg_buf;
1469 card->msg_buf_read = card->msg_buf;
1470 card->msg_buf_end = &card->msg_buf[sizeof(card->msg_buf) - 1];
1471 for (i = 0; i < ISDNLOOP_BCH; i++) {
1472 card->l2_proto[i] = ISDN_PROTO_L2_X75I;
1473 skb_queue_head_init(&card->bqueue[i]);
1475 skb_queue_head_init(&card->dqueue);
1478 if (!register_isdn(&card->interface)) {
1479 cards = cards->next;
1481 "isdnloop: Unable to register %s\n", id);
1483 return (isdnloop_card *) 0;
1485 card->myid = card->interface.channels;
1490 isdnloop_addcard(char *id1)
1492 isdnloop_card *card;
1494 if (!(card = isdnloop_initcard(id1))) {
1498 "isdnloop: (%s) virtual card added\n",
1499 card->interface.id);
1509 if ((p = strchr(revision, ':'))) {
1511 p = strchr(rev, '$');
1514 strcpy(rev, " ??? ");
1515 printk(KERN_NOTICE "isdnloop-ISDN-driver Rev%s\n", rev);
1518 return (isdnloop_addcard(isdnloop_id));
1527 isdnloop_card *card = cards;
1528 isdnloop_card *last;
1531 isdnloop_stopallcards();
1533 cmd.command = ISDN_STAT_UNLOAD;
1534 cmd.driver = card->myid;
1535 card->interface.statcallb(&cmd);
1536 for (i = 0; i < ISDNLOOP_BCH; i++)
1537 isdnloop_free_queue(card, i);
1543 skb_queue_purge(&card->dqueue);
1547 printk(KERN_NOTICE "isdnloop-ISDN-driver unloaded\n");
1550 module_init(isdnloop_init);
1551 module_exit(isdnloop_exit);