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/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/init.h>
15 #include <linux/sched.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_param(isdnloop_id, charp, 0);
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 */
103 spin_lock_irqsave(&card->isdnloop_lock, flags);
104 card->rb_timer.expires = jiffies + ISDNLOOP_TIMER_BCREAD;
105 add_timer(&card->rb_timer);
106 card->flags |= ISDNLOOP_FLAGS_RBTIMER;
107 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
109 card->flags &= ~ISDNLOOP_FLAGS_RBTIMER;
113 * Parse ICN-type setup string and fill fields of setup-struct
117 * setup = setup string, format: [caller-id],si1,si2,[called-id]
118 * cmd = pointer to struct to be filled.
121 isdnloop_parse_setup(char *setup, isdn_ctrl * cmd)
124 char *s = strchr(t, ',');
127 strlcpy(cmd->parm.setup.phone, t, sizeof(cmd->parm.setup.phone));
128 s = strchr(t = s, ',');
131 cmd->parm.setup.si1 = 0;
133 cmd->parm.setup.si1 = simple_strtoul(t, NULL, 10);
134 s = strchr(t = s, ',');
137 cmd->parm.setup.si2 = 0;
139 cmd->parm.setup.si2 =
140 simple_strtoul(t, NULL, 10);
141 strlcpy(cmd->parm.setup.eazmsn, s, sizeof(cmd->parm.setup.eazmsn));
142 cmd->parm.setup.plan = 0;
143 cmd->parm.setup.screen = 0;
146 typedef struct isdnloop_stat {
152 static isdnloop_stat isdnloop_stat_table[] =
154 {"BCON_", ISDN_STAT_BCONN, 1}, /* B-Channel connected */
155 {"BDIS_", ISDN_STAT_BHUP, 2}, /* B-Channel disconnected */
156 {"DCON_", ISDN_STAT_DCONN, 0}, /* D-Channel connected */
157 {"DDIS_", ISDN_STAT_DHUP, 0}, /* D-Channel disconnected */
158 {"DCAL_I", ISDN_STAT_ICALL, 3}, /* Incoming call dialup-line */
159 {"DSCA_I", ISDN_STAT_ICALL, 3}, /* Incoming call 1TR6-SPV */
160 {"FCALL", ISDN_STAT_ICALL, 4}, /* Leased line connection up */
161 {"CIF", ISDN_STAT_CINF, 5}, /* Charge-info, 1TR6-type */
162 {"AOC", ISDN_STAT_CINF, 6}, /* Charge-info, DSS1-type */
163 {"CAU", ISDN_STAT_CAUSE, 7}, /* Cause code */
164 {"TEI OK", ISDN_STAT_RUN, 0}, /* Card connected to wallplug */
165 {"E_L1: ACT FAIL", ISDN_STAT_BHUP, 8}, /* Layer-1 activation failed */
166 {"E_L2: DATA LIN", ISDN_STAT_BHUP, 8}, /* Layer-2 data link lost */
167 {"E_L1: ACTIVATION FAILED",
168 ISDN_STAT_BHUP, 8}, /* Layer-1 activation failed */
175 * Parse Status message-strings from virtual card.
176 * Depending on status, call statcallb for sending messages to upper
177 * levels. Also set/reset B-Channel active-flags.
180 * status = status string to parse.
181 * channel = channel where message comes from.
182 * card = card where message comes from.
185 isdnloop_parse_status(u_char * status, int channel, isdnloop_card * card)
187 isdnloop_stat *s = isdnloop_stat_table;
192 if (!strncmp(status, s->statstr, strlen(s->statstr))) {
193 cmd.command = s->command;
201 cmd.driver = card->myid;
206 card->flags |= (channel) ?
207 ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE;
211 card->flags &= ~((channel) ?
212 ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE);
213 isdnloop_free_queue(card, channel);
216 /* DCAL_I and DSCA_I */
217 isdnloop_parse_setup(status + 6, &cmd);
221 sprintf(cmd.parm.setup.phone, "LEASED%d", card->myid);
222 sprintf(cmd.parm.setup.eazmsn, "%d", channel + 1);
223 cmd.parm.setup.si1 = 7;
224 cmd.parm.setup.si2 = 0;
225 cmd.parm.setup.plan = 0;
226 cmd.parm.setup.screen = 0;
230 strlcpy(cmd.parm.num, status + 3, sizeof(cmd.parm.num));
234 snprintf(cmd.parm.num, sizeof(cmd.parm.num), "%d",
235 (int) simple_strtoul(status + 7, NULL, 16));
240 if (strlen(status) == 4)
241 snprintf(cmd.parm.num, sizeof(cmd.parm.num), "%s%c%c",
242 status + 2, *status, *(status + 1));
244 strlcpy(cmd.parm.num, status + 1, sizeof(cmd.parm.num));
247 /* Misc Errors on L1 and L2 */
248 card->flags &= ~ISDNLOOP_FLAGS_B1ACTIVE;
249 isdnloop_free_queue(card, 0);
251 cmd.driver = card->myid;
252 card->interface.statcallb(&cmd);
253 cmd.command = ISDN_STAT_DHUP;
255 cmd.driver = card->myid;
256 card->interface.statcallb(&cmd);
257 cmd.command = ISDN_STAT_BHUP;
258 card->flags &= ~ISDNLOOP_FLAGS_B2ACTIVE;
259 isdnloop_free_queue(card, 1);
261 cmd.driver = card->myid;
262 card->interface.statcallb(&cmd);
263 cmd.command = ISDN_STAT_DHUP;
265 cmd.driver = card->myid;
268 card->interface.statcallb(&cmd);
272 * Store a cwcharacter into ringbuffer for reading from /dev/isdnctrl
275 * card = pointer to card struct.
279 isdnloop_putmsg(isdnloop_card * card, unsigned char c)
283 spin_lock_irqsave(&card->isdnloop_lock, flags);
284 *card->msg_buf_write++ = (c == 0xff) ? '\n' : c;
285 if (card->msg_buf_write == card->msg_buf_read) {
286 if (++card->msg_buf_read > card->msg_buf_end)
287 card->msg_buf_read = card->msg_buf;
289 if (card->msg_buf_write > card->msg_buf_end)
290 card->msg_buf_write = card->msg_buf;
291 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
295 * Poll a virtual cards message queue.
296 * If there are new status-replies from the card, copy them to
297 * ringbuffer for reading on /dev/isdnctrl and call
298 * isdnloop_parse_status() for processing them. Watch for special
299 * Firmware bootmessage and parse it, to get the D-Channel protocol.
300 * If there are B-Channels open, initiate a timer-callback to
301 * isdnloop_pollbchan().
302 * This routine is called periodically via timer interrupt.
305 * data = pointer to card struct
308 isdnloop_polldchan(unsigned long data)
310 isdnloop_card *card = (isdnloop_card *) data;
320 if ((skb = skb_dequeue(&card->dqueue)))
324 for (left = avail; left > 0; left--) {
327 isdnloop_putmsg(card, c);
328 card->imsg[card->iptr] = c;
333 isdnloop_putmsg(card, '\n');
334 card->imsg[card->iptr] = 0;
336 if (card->imsg[0] == '0' && card->imsg[1] >= '0' &&
337 card->imsg[1] <= '2' && card->imsg[2] == ';') {
338 ch = (card->imsg[1] - '0') - 1;
340 isdnloop_parse_status(p, ch, card);
343 if (!strncmp(p, "DRV1.", 5)) {
344 printk(KERN_INFO "isdnloop: (%s) %s\n", CID, p);
345 if (!strncmp(p + 7, "TC", 2)) {
346 card->ptype = ISDN_PTYPE_1TR6;
347 card->interface.features |= ISDN_FEATURE_P_1TR6;
349 "isdnloop: (%s) 1TR6-Protocol loaded and running\n", CID);
351 if (!strncmp(p + 7, "EC", 2)) {
352 card->ptype = ISDN_PTYPE_EURO;
353 card->interface.features |= ISDN_FEATURE_P_EURO;
355 "isdnloop: (%s) Euro-Protocol loaded and running\n", CID);
364 cmd.command = ISDN_STAT_STAVAIL;
365 cmd.driver = card->myid;
367 card->interface.statcallb(&cmd);
369 if (card->flags & (ISDNLOOP_FLAGS_B1ACTIVE | ISDNLOOP_FLAGS_B2ACTIVE))
370 if (!(card->flags & ISDNLOOP_FLAGS_RBTIMER)) {
371 /* schedule b-channel polling */
372 card->flags |= ISDNLOOP_FLAGS_RBTIMER;
373 spin_lock_irqsave(&card->isdnloop_lock, flags);
374 del_timer(&card->rb_timer);
375 card->rb_timer.function = isdnloop_pollbchan;
376 card->rb_timer.data = (unsigned long) card;
377 card->rb_timer.expires = jiffies + ISDNLOOP_TIMER_BCREAD;
378 add_timer(&card->rb_timer);
379 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
382 spin_lock_irqsave(&card->isdnloop_lock, flags);
383 card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD;
384 add_timer(&card->st_timer);
385 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
389 * Append a packet to the transmit buffer-queue.
392 * channel = Number of B-channel
393 * skb = packet to send.
394 * card = pointer to card-struct
396 * Number of bytes transferred, -E??? on error
399 isdnloop_sendbuf(int channel, struct sk_buff *skb, isdnloop_card * card)
403 struct sk_buff *nskb;
407 "isdnloop: Send packet too large\n");
411 if (!(card->flags & (channel) ? ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE))
413 if (card->sndcount[channel] > ISDNLOOP_MAX_SQUEUE)
415 spin_lock_irqsave(&card->isdnloop_lock, flags);
416 nskb = dev_alloc_skb(skb->len);
418 memcpy(skb_put(nskb, len), skb->data, len);
419 skb_queue_tail(&card->bqueue[channel], nskb);
423 card->sndcount[channel] += len;
424 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
430 * Read the messages from the card's ringbuffer
433 * buf = pointer to buffer.
434 * len = number of bytes to read.
435 * user = flag, 1: called from userlevel 0: called from kernel.
436 * card = pointer to card struct.
438 * number of bytes actually transferred.
441 isdnloop_readstatus(u_char __user *buf, int len, isdnloop_card * card)
446 for (p = buf, count = 0; count < len; p++, count++) {
447 if (card->msg_buf_read == card->msg_buf_write)
449 if (put_user(*card->msg_buf_read++, p))
451 if (card->msg_buf_read > card->msg_buf_end)
452 card->msg_buf_read = card->msg_buf;
458 * Simulate a card's response by appending it to the cards
462 * card = pointer to card struct.
463 * s = pointer to message-string.
464 * ch = channel: 0 = generic messages, 1 and 2 = D-channel messages.
466 * 0 on success, 1 on memory squeeze.
469 isdnloop_fake(isdnloop_card * card, char *s, int ch)
472 int len = strlen(s) + ((ch >= 0) ? 3 : 0);
474 if (!(skb = dev_alloc_skb(len))) {
475 printk(KERN_WARNING "isdnloop: Out of memory in isdnloop_fake\n");
479 sprintf(skb_put(skb, 3), "%02d;", ch);
480 memcpy(skb_put(skb, strlen(s)), s, strlen(s));
481 skb_queue_tail(&card->dqueue, skb);
485 static isdnloop_stat isdnloop_cmd_table[] =
487 {"BCON_R", 0, 1}, /* B-Channel connect */
488 {"BCON_I", 0, 17}, /* B-Channel connect ind */
489 {"BDIS_R", 0, 2}, /* B-Channel disconnect */
490 {"DDIS_R", 0, 3}, /* D-Channel disconnect */
491 {"DCON_R", 0, 16}, /* D-Channel connect */
492 {"DSCA_R", 0, 4}, /* Dial 1TR6-SPV */
493 {"DCAL_R", 0, 5}, /* Dial */
494 {"EAZC", 0, 6}, /* Clear EAZ listener */
495 {"EAZ", 0, 7}, /* Set EAZ listener */
496 {"SEEAZ", 0, 8}, /* Get EAZ listener */
497 {"MSN", 0, 9}, /* Set/Clear MSN listener */
498 {"MSALL", 0, 10}, /* Set multi MSN listeners */
499 {"SETSIL", 0, 11}, /* Set SI list */
500 {"SEESIL", 0, 12}, /* Get SI list */
501 {"SILC", 0, 13}, /* Clear SI list */
502 {"LOCK", 0, -1}, /* LOCK channel */
503 {"UNLOCK", 0, -1}, /* UNLOCK channel */
504 {"FV2ON", 1, 14}, /* Leased mode on */
505 {"FV2OFF", 1, 15}, /* Leased mode off */
512 * Simulate an error-response from a card.
515 * card = pointer to card struct.
518 isdnloop_fake_err(isdnloop_card * card)
522 sprintf(buf, "E%s", card->omsg);
523 isdnloop_fake(card, buf, -1);
524 isdnloop_fake(card, "NAK", -1);
527 static u_char ctable_eu[] =
528 {0x00, 0x11, 0x01, 0x12};
529 static u_char ctable_1t[] =
530 {0x00, 0x3b, 0x01, 0x3a};
533 * Assemble a simplified cause message depending on the
534 * D-channel protocol used.
537 * card = pointer to card struct.
538 * loc = location: 0 = local, 1 = remote.
539 * cau = cause: 1 = busy, 2 = nonexistent callerid, 3 = no user responding.
541 * Pointer to buffer containing the assembled message.
544 isdnloop_unicause(isdnloop_card * card, int loc, int cau)
548 switch (card->ptype) {
549 case ISDN_PTYPE_EURO:
550 sprintf(buf, "E%02X%02X", (loc) ? 4 : 2, ctable_eu[cau]);
552 case ISDN_PTYPE_1TR6:
553 sprintf(buf, "%02X44", ctable_1t[cau]);
562 * Release a virtual connection. Called from timer interrupt, when
563 * called party did not respond.
566 * card = pointer to card struct.
567 * ch = channel (0-based)
570 isdnloop_atimeout(isdnloop_card * card, int ch)
575 spin_lock_irqsave(&card->isdnloop_lock, flags);
577 isdnloop_fake(card->rcard[ch], "DDIS_I", card->rch[ch] + 1);
578 card->rcard[ch]->rcard[card->rch[ch]] = NULL;
579 card->rcard[ch] = NULL;
581 isdnloop_fake(card, "DDIS_I", ch + 1);
582 /* No user responding */
583 sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 3));
584 isdnloop_fake(card, buf, ch + 1);
585 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
589 * Wrapper for isdnloop_atimeout().
592 isdnloop_atimeout0(unsigned long data)
594 isdnloop_card *card = (isdnloop_card *) data;
595 isdnloop_atimeout(card, 0);
599 * Wrapper for isdnloop_atimeout().
602 isdnloop_atimeout1(unsigned long data)
604 isdnloop_card *card = (isdnloop_card *) data;
605 isdnloop_atimeout(card, 1);
609 * Install a watchdog for a user, not responding.
612 * card = pointer to card struct.
613 * ch = channel to watch for.
616 isdnloop_start_ctimer(isdnloop_card * card, int ch)
620 spin_lock_irqsave(&card->isdnloop_lock, flags);
621 init_timer(&card->c_timer[ch]);
622 card->c_timer[ch].expires = jiffies + ISDNLOOP_TIMER_ALERTWAIT;
624 card->c_timer[ch].function = isdnloop_atimeout1;
626 card->c_timer[ch].function = isdnloop_atimeout0;
627 card->c_timer[ch].data = (unsigned long) card;
628 add_timer(&card->c_timer[ch]);
629 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
633 * Kill a pending channel watchdog.
636 * card = pointer to card struct.
637 * ch = channel (0-based).
640 isdnloop_kill_ctimer(isdnloop_card * card, int ch)
644 spin_lock_irqsave(&card->isdnloop_lock, flags);
645 del_timer(&card->c_timer[ch]);
646 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
649 static u_char si2bit[] =
650 {0, 1, 0, 0, 0, 2, 0, 4, 0, 0};
651 static u_char bit2si[] =
655 * Try finding a listener for an outgoing call.
658 * card = pointer to calling card.
659 * p = pointer to ICN-type setup-string.
660 * lch = channel of calling card.
661 * cmd = pointer to struct to be filled when parsing setup.
663 * 0 = found match, alerting should happen.
664 * 1 = found matching number but it is busy.
665 * 2 = no matching listener.
666 * 3 = found matching number but SI does not match.
669 isdnloop_try_call(isdnloop_card * card, char *p, int lch, isdn_ctrl * cmd)
671 isdnloop_card *cc = cards;
679 isdnloop_parse_setup(p, cmd);
681 for (ch = 0; ch < 2; ch++) {
682 /* Exclude ourself */
683 if ((cc == card) && (ch == lch))
687 case ISDN_PTYPE_EURO:
688 for (i = 0; i < 3; i++)
689 if (!(strcmp(cc->s0num[i], cmd->parm.setup.phone)))
692 case ISDN_PTYPE_1TR6:
695 sprintf(nbuf, "%s%c", cc->s0num[0], *e);
696 if (!(strcmp(nbuf, cmd->parm.setup.phone)))
702 spin_lock_irqsave(&card->isdnloop_lock, flags);
704 if (!(cc->rcard[ch])) {
706 if (!(si2bit[cmd->parm.setup.si1] & cc->sil[ch])) {
707 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
710 /* ch is idle, si and number matches */
711 cc->rcard[ch] = card;
713 card->rcard[lch] = cc;
715 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
718 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
719 /* num matches, but busy */
731 * Depending on D-channel protocol and caller/called, modify
735 * card = pointer to card struct.
736 * phone = pointer phone number.
737 * caller = flag: 1 = caller, 0 = called.
739 * pointer to new phone number.
742 isdnloop_vstphone(isdnloop_card * card, char *phone, int caller)
745 static char nphone[30];
751 switch (card->ptype) {
752 case ISDN_PTYPE_EURO:
754 for (i = 0; i < 2; i++)
755 if (!(strcmp(card->s0num[i], phone)))
757 return (card->s0num[0]);
761 case ISDN_PTYPE_1TR6:
763 sprintf(nphone, "%s%c", card->s0num[0], phone[0]);
766 return (&phone[strlen(phone) - 1]);
773 * Parse an ICN-type command string sent to the 'card'.
774 * Perform misc. actions depending on the command.
777 * card = pointer to card struct.
780 isdnloop_parse_cmd(isdnloop_card * card)
782 char *p = card->omsg;
785 isdnloop_stat *s = isdnloop_cmd_table;
790 if ((card->omsg[0] != '0') && (card->omsg[2] != ';')) {
791 isdnloop_fake_err(card);
794 ch = card->omsg[1] - '0';
795 if ((ch < 0) || (ch > 2)) {
796 isdnloop_fake_err(card);
801 if (!strncmp(p, s->statstr, strlen(s->statstr))) {
803 if (s->command && (ch != 0)) {
804 isdnloop_fake_err(card);
816 if (card->rcard[ch - 1]) {
817 isdnloop_fake(card->rcard[ch - 1], "BCON_I",
818 card->rch[ch - 1] + 1);
819 isdnloop_fake(card, "BCON_C", ch);
824 if (card->rcard[ch - 1]) {
825 isdnloop_fake(card->rcard[ch - 1], "BCON_C",
826 card->rch[ch - 1] + 1);
831 isdnloop_fake(card, "BDIS_C", ch);
832 if (card->rcard[ch - 1]) {
833 isdnloop_fake(card->rcard[ch - 1], "BDIS_I",
834 card->rch[ch - 1] + 1);
839 isdnloop_kill_ctimer(card, ch - 1);
840 if (card->rcard[ch - 1]) {
841 isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
842 isdnloop_fake(card->rcard[ch - 1], "DCON_C",
843 card->rch[ch - 1] + 1);
844 isdnloop_fake(card, "DCON_C", ch);
849 isdnloop_kill_ctimer(card, ch - 1);
850 if (card->rcard[ch - 1]) {
851 isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
852 isdnloop_fake(card->rcard[ch - 1], "DDIS_I",
853 card->rch[ch - 1] + 1);
854 card->rcard[ch - 1] = NULL;
856 isdnloop_fake(card, "DDIS_C", ch);
859 /* 0x;DSCA_Rdd,yy,zz,oo */
860 if (card->ptype != ISDN_PTYPE_1TR6) {
861 isdnloop_fake_err(card);
866 /* 0x;DCAL_Rdd,yy,zz,oo */
868 switch (isdnloop_try_call(card, p, ch - 1, &cmd)) {
871 sprintf(buf, "D%s_I%s,%02d,%02d,%s",
872 (action == 4) ? "SCA" : "CAL",
873 isdnloop_vstphone(card, cmd.parm.setup.eazmsn, 1),
876 isdnloop_vstphone(card->rcard[ch - 1],
877 cmd.parm.setup.phone, 0));
878 isdnloop_fake(card->rcard[ch - 1], buf, card->rch[ch - 1] + 1);
881 /* si1 does not match, don't alert but start timer */
882 isdnloop_start_ctimer(card, ch - 1);
886 isdnloop_fake(card, "DDIS_I", ch);
887 sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 1));
888 isdnloop_fake(card, buf, ch);
892 isdnloop_fake(card, "DDIS_I", ch);
893 sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 2));
894 isdnloop_fake(card, buf, ch);
900 card->eazlist[ch - 1][0] = '\0';
905 strcpy(card->eazlist[ch - 1], p);
909 sprintf(buf, "EAZ-LIST: %s", card->eazlist[ch - 1]);
910 isdnloop_fake(card, buf, ch + 1);
922 while (strchr("0157", *p)) {
924 card->sil[ch - 1] |= si2bit[*p - '0'];
928 isdnloop_fake_err(card);
932 sprintf(buf, "SIN-LIST: ");
934 for (i = 0; i < 3; i++)
935 if (card->sil[ch - 1] & (1 << i))
936 p += sprintf(p, "%02d", bit2si[i]);
937 isdnloop_fake(card, buf, ch + 1);
941 card->sil[ch - 1] = 0;
953 * Put command-strings into the of the 'card'. In reality, execute them
954 * right in place by calling isdnloop_parse_cmd(). Also copy every
955 * command to the read message ringbuffer, preceeding it with a '>'.
956 * These mesagges can be read at /dev/isdnctrl.
959 * buf = pointer to command buffer.
960 * len = length of buffer data.
961 * user = flag: 1 = called form userlevel, 0 called from kernel.
962 * card = pointer to card struct.
964 * number of bytes transferred (currently always equals len).
967 isdnloop_writecmd(const u_char * buf, int len, int user, isdnloop_card * card)
981 if (copy_from_user(msg, buf, count))
984 memcpy(msg, buf, count);
985 isdnloop_putmsg(card, '>');
986 for (p = msg; count > 0; count--, p++) {
989 isdnloop_putmsg(card, *p);
990 card->omsg[card->optr] = *p;
992 card->omsg[card->optr] = '\0';
994 isdnloop_parse_cmd(card);
996 isdnloop_putmsg(card, '>');
1000 if (card->optr < 59)
1006 cmd.command = ISDN_STAT_STAVAIL;
1007 cmd.driver = card->myid;
1009 card->interface.statcallb(&cmd);
1014 * Delete card's pending timers, send STOP to linklevel
1017 isdnloop_stopcard(isdnloop_card * card)
1019 unsigned long flags;
1022 spin_lock_irqsave(&card->isdnloop_lock, flags);
1023 if (card->flags & ISDNLOOP_FLAGS_RUNNING) {
1024 card->flags &= ~ISDNLOOP_FLAGS_RUNNING;
1025 del_timer(&card->st_timer);
1026 del_timer(&card->rb_timer);
1027 del_timer(&card->c_timer[0]);
1028 del_timer(&card->c_timer[1]);
1029 cmd.command = ISDN_STAT_STOP;
1030 cmd.driver = card->myid;
1031 card->interface.statcallb(&cmd);
1033 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1037 * Stop all cards before unload.
1040 isdnloop_stopallcards(void)
1042 isdnloop_card *p = cards;
1045 isdnloop_stopcard(p);
1051 * Start a 'card'. Simulate card's boot message and set the phone
1052 * number(s) of the virtual 'S0-Interface'. Install D-channel
1056 * card = pointer to card struct.
1057 * sdefp = pointer to struct holding ioctl parameters.
1059 * 0 on success, -E??? otherwise.
1062 isdnloop_start(isdnloop_card * card, isdnloop_sdef * sdefp)
1064 unsigned long flags;
1068 if (card->flags & ISDNLOOP_FLAGS_RUNNING)
1070 if (copy_from_user((char *) &sdef, (char *) sdefp, sizeof(sdef)))
1072 spin_lock_irqsave(&card->isdnloop_lock, flags);
1073 switch (sdef.ptype) {
1074 case ISDN_PTYPE_EURO:
1075 if (isdnloop_fake(card, "DRV1.23EC-Q.931-CAPI-CNS-BASIS-20.02.96",
1077 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1080 card->sil[0] = card->sil[1] = 4;
1081 if (isdnloop_fake(card, "TEI OK", 0)) {
1082 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1085 for (i = 0; i < 3; i++)
1086 strcpy(card->s0num[i], sdef.num[i]);
1088 case ISDN_PTYPE_1TR6:
1089 if (isdnloop_fake(card, "DRV1.04TC-1TR6-CAPI-CNS-BASIS-29.11.95",
1091 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1094 card->sil[0] = card->sil[1] = 4;
1095 if (isdnloop_fake(card, "TEI OK", 0)) {
1096 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1099 strcpy(card->s0num[0], sdef.num[0]);
1100 card->s0num[1][0] = '\0';
1101 card->s0num[2][0] = '\0';
1104 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1105 printk(KERN_WARNING "isdnloop: Illegal D-channel protocol %d\n",
1109 init_timer(&card->st_timer);
1110 card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD;
1111 card->st_timer.function = isdnloop_polldchan;
1112 card->st_timer.data = (unsigned long) card;
1113 add_timer(&card->st_timer);
1114 card->flags |= ISDNLOOP_FLAGS_RUNNING;
1115 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1120 * Main handler for commands sent by linklevel.
1123 isdnloop_command(isdn_ctrl * c, isdnloop_card * card)
1131 switch (c->command) {
1132 case ISDN_CMD_IOCTL:
1133 memcpy(&a, c->parm.num, sizeof(ulong));
1135 case ISDNLOOP_IOCTL_DEBUGVAR:
1136 return (ulong) card;
1137 case ISDNLOOP_IOCTL_STARTUP:
1138 if (!access_ok(VERIFY_READ, (void *) a, sizeof(isdnloop_sdef)))
1140 return (isdnloop_start(card, (isdnloop_sdef *) a));
1142 case ISDNLOOP_IOCTL_ADDCARD:
1143 if (copy_from_user((char *)&cdef,
1147 return (isdnloop_addcard(cdef.id1));
1149 case ISDNLOOP_IOCTL_LEASEDCFG:
1151 if (!card->leased) {
1153 while (card->ptype == ISDN_PTYPE_UNKNOWN)
1154 schedule_timeout_interruptible(10);
1155 schedule_timeout_interruptible(10);
1156 sprintf(cbuf, "00;FV2ON\n01;EAZ1\n02;EAZ2\n");
1157 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1159 "isdnloop: (%s) Leased-line mode enabled\n",
1161 cmd.command = ISDN_STAT_RUN;
1162 cmd.driver = card->myid;
1164 card->interface.statcallb(&cmd);
1169 sprintf(cbuf, "00;FV2OFF\n");
1170 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1172 "isdnloop: (%s) Leased-line mode disabled\n",
1174 cmd.command = ISDN_STAT_RUN;
1175 cmd.driver = card->myid;
1177 card->interface.statcallb(&cmd);
1186 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1190 if ((c->arg & 255) < ISDNLOOP_BCH) {
1196 p = c->parm.setup.phone;
1197 if (*p == 's' || *p == 'S') {
1200 strcpy(dcode, "SCA");
1203 strcpy(dcode, "CAL");
1205 sprintf(cbuf, "%02d;D%s_R%s,%02d,%02d,%s\n", (int) (a + 1),
1206 dcode, dial, c->parm.setup.si1,
1207 c->parm.setup.si2, c->parm.setup.eazmsn);
1208 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1211 case ISDN_CMD_ACCEPTD:
1212 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1214 if (c->arg < ISDNLOOP_BCH) {
1217 switch (card->l2_proto[a - 1]) {
1218 case ISDN_PROTO_L2_X75I:
1219 sprintf(cbuf, "%02d;BX75\n", (int) a);
1221 #ifdef CONFIG_ISDN_X25
1222 case ISDN_PROTO_L2_X25DTE:
1223 sprintf(cbuf, "%02d;BX2T\n", (int) a);
1225 case ISDN_PROTO_L2_X25DCE:
1226 sprintf(cbuf, "%02d;BX2C\n", (int) a);
1229 case ISDN_PROTO_L2_HDLC:
1230 sprintf(cbuf, "%02d;BTRA\n", (int) a);
1234 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1235 sprintf(cbuf, "%02d;DCON_R\n", (int) a);
1236 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1239 case ISDN_CMD_ACCEPTB:
1240 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1242 if (c->arg < ISDNLOOP_BCH) {
1244 switch (card->l2_proto[a - 1]) {
1245 case ISDN_PROTO_L2_X75I:
1246 sprintf(cbuf, "%02d;BCON_R,BX75\n", (int) a);
1248 #ifdef CONFIG_ISDN_X25
1249 case ISDN_PROTO_L2_X25DTE:
1250 sprintf(cbuf, "%02d;BCON_R,BX2T\n", (int) a);
1252 case ISDN_PROTO_L2_X25DCE:
1253 sprintf(cbuf, "%02d;BCON_R,BX2C\n", (int) a);
1256 case ISDN_PROTO_L2_HDLC:
1257 sprintf(cbuf, "%02d;BCON_R,BTRA\n", (int) a);
1260 sprintf(cbuf, "%02d;BCON_R\n", (int) a);
1262 printk(KERN_DEBUG "isdnloop writecmd '%s'\n", cbuf);
1263 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1265 case ISDN_CMD_HANGUP:
1266 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1268 if (c->arg < ISDNLOOP_BCH) {
1270 sprintf(cbuf, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a, (int) a);
1271 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1274 case ISDN_CMD_SETEAZ:
1275 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1279 if (c->arg < ISDNLOOP_BCH) {
1281 if (card->ptype == ISDN_PTYPE_EURO) {
1282 sprintf(cbuf, "%02d;MS%s%s\n", (int) a,
1283 c->parm.num[0] ? "N" : "ALL", c->parm.num);
1285 sprintf(cbuf, "%02d;EAZ%s\n", (int) a,
1286 c->parm.num[0] ? c->parm.num : (u_char *) "0123456789");
1287 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1290 case ISDN_CMD_CLREAZ:
1291 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1295 if (c->arg < ISDNLOOP_BCH) {
1297 if (card->ptype == ISDN_PTYPE_EURO)
1298 sprintf(cbuf, "%02d;MSNC\n", (int) a);
1300 sprintf(cbuf, "%02d;EAZC\n", (int) a);
1301 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1304 case ISDN_CMD_SETL2:
1305 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1307 if ((c->arg & 255) < ISDNLOOP_BCH) {
1310 case ISDN_PROTO_L2_X75I:
1311 sprintf(cbuf, "%02d;BX75\n", (int) (a & 255) + 1);
1313 #ifdef CONFIG_ISDN_X25
1314 case ISDN_PROTO_L2_X25DTE:
1315 sprintf(cbuf, "%02d;BX2T\n", (int) (a & 255) + 1);
1317 case ISDN_PROTO_L2_X25DCE:
1318 sprintf(cbuf, "%02d;BX2C\n", (int) (a & 255) + 1);
1321 case ISDN_PROTO_L2_HDLC:
1322 sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
1324 case ISDN_PROTO_L2_TRANS:
1325 sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
1330 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1331 card->l2_proto[a & 255] = (a >> 8);
1334 case ISDN_CMD_SETL3:
1335 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1346 * Find card with given driverId
1348 static inline isdnloop_card *
1349 isdnloop_findcard(int driverid)
1351 isdnloop_card *p = cards;
1354 if (p->myid == driverid)
1358 return (isdnloop_card *) 0;
1362 * Wrapper functions for interface to linklevel
1365 if_command(isdn_ctrl * c)
1367 isdnloop_card *card = isdnloop_findcard(c->driver);
1370 return (isdnloop_command(c, card));
1372 "isdnloop: if_command called with invalid driverId!\n");
1377 if_writecmd(const u_char __user *buf, int len, int id, int channel)
1379 isdnloop_card *card = isdnloop_findcard(id);
1382 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1384 return (isdnloop_writecmd(buf, len, 1, card));
1387 "isdnloop: if_writecmd called with invalid driverId!\n");
1392 if_readstatus(u_char __user *buf, int len, int id, int channel)
1394 isdnloop_card *card = isdnloop_findcard(id);
1397 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1399 return (isdnloop_readstatus(buf, len, card));
1402 "isdnloop: if_readstatus called with invalid driverId!\n");
1407 if_sendbuf(int id, int channel, int ack, struct sk_buff *skb)
1409 isdnloop_card *card = isdnloop_findcard(id);
1412 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1414 /* ack request stored in skb scratch area */
1416 return (isdnloop_sendbuf(channel, skb, card));
1419 "isdnloop: if_sendbuf called with invalid driverId!\n");
1424 * Allocate a new card-struct, initialize it
1425 * link it into cards-list and register it at linklevel.
1427 static isdnloop_card *
1428 isdnloop_initcard(char *id)
1430 isdnloop_card *card;
1433 if (!(card = (isdnloop_card *) kmalloc(sizeof(isdnloop_card), GFP_KERNEL))) {
1435 "isdnloop: (%s) Could not allocate card-struct.\n", id);
1436 return (isdnloop_card *) 0;
1438 memset((char *) card, 0, sizeof(isdnloop_card));
1439 card->interface.owner = THIS_MODULE;
1440 card->interface.channels = ISDNLOOP_BCH;
1441 card->interface.hl_hdrlen = 1; /* scratch area for storing ack flag*/
1442 card->interface.maxbufsize = 4000;
1443 card->interface.command = if_command;
1444 card->interface.writebuf_skb = if_sendbuf;
1445 card->interface.writecmd = if_writecmd;
1446 card->interface.readstat = if_readstatus;
1447 card->interface.features = ISDN_FEATURE_L2_X75I |
1448 #ifdef CONFIG_ISDN_X25
1449 ISDN_FEATURE_L2_X25DTE |
1450 ISDN_FEATURE_L2_X25DCE |
1452 ISDN_FEATURE_L2_HDLC |
1453 ISDN_FEATURE_L3_TRANS |
1454 ISDN_FEATURE_P_UNKNOWN;
1455 card->ptype = ISDN_PTYPE_UNKNOWN;
1456 strlcpy(card->interface.id, id, sizeof(card->interface.id));
1457 card->msg_buf_write = card->msg_buf;
1458 card->msg_buf_read = card->msg_buf;
1459 card->msg_buf_end = &card->msg_buf[sizeof(card->msg_buf) - 1];
1460 for (i = 0; i < ISDNLOOP_BCH; i++) {
1461 card->l2_proto[i] = ISDN_PROTO_L2_X75I;
1462 skb_queue_head_init(&card->bqueue[i]);
1464 skb_queue_head_init(&card->dqueue);
1465 card->isdnloop_lock = SPIN_LOCK_UNLOCKED;
1468 if (!register_isdn(&card->interface)) {
1469 cards = cards->next;
1471 "isdnloop: Unable to register %s\n", id);
1473 return (isdnloop_card *) 0;
1475 card->myid = card->interface.channels;
1480 isdnloop_addcard(char *id1)
1482 isdnloop_card *card;
1484 if (!(card = isdnloop_initcard(id1))) {
1488 "isdnloop: (%s) virtual card added\n",
1489 card->interface.id);
1499 if ((p = strchr(revision, ':'))) {
1501 p = strchr(rev, '$');
1504 strcpy(rev, " ??? ");
1505 printk(KERN_NOTICE "isdnloop-ISDN-driver Rev%s\n", rev);
1508 return (isdnloop_addcard(isdnloop_id));
1517 isdnloop_card *card = cards;
1518 isdnloop_card *last;
1521 isdnloop_stopallcards();
1523 cmd.command = ISDN_STAT_UNLOAD;
1524 cmd.driver = card->myid;
1525 card->interface.statcallb(&cmd);
1526 for (i = 0; i < ISDNLOOP_BCH; i++)
1527 isdnloop_free_queue(card, i);
1533 skb_queue_purge(&card->dqueue);
1537 printk(KERN_NOTICE "isdnloop-ISDN-driver unloaded\n");
1540 module_init(isdnloop_init);
1541 module_exit(isdnloop_exit);