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/slab.h>
15 #include <linux/init.h>
16 #include <linux/sched.h>
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 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 */
104 spin_lock_irqsave(&card->isdnloop_lock, flags);
105 card->rb_timer.expires = jiffies + ISDNLOOP_TIMER_BCREAD;
106 add_timer(&card->rb_timer);
107 card->flags |= ISDNLOOP_FLAGS_RBTIMER;
108 spin_unlock_irqrestore(&card->isdnloop_lock, 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[] = {
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 skb = skb_dequeue(&card->dqueue);
325 for (left = avail; left > 0; left--) {
328 isdnloop_putmsg(card, c);
329 card->imsg[card->iptr] = c;
334 isdnloop_putmsg(card, '\n');
335 card->imsg[card->iptr] = 0;
337 if (card->imsg[0] == '0' && card->imsg[1] >= '0' &&
338 card->imsg[1] <= '2' && card->imsg[2] == ';') {
339 ch = (card->imsg[1] - '0') - 1;
341 isdnloop_parse_status(p, ch, card);
344 if (!strncmp(p, "DRV1.", 5)) {
345 printk(KERN_INFO "isdnloop: (%s) %s\n", CID, p);
346 if (!strncmp(p + 7, "TC", 2)) {
347 card->ptype = ISDN_PTYPE_1TR6;
348 card->interface.features |= ISDN_FEATURE_P_1TR6;
350 "isdnloop: (%s) 1TR6-Protocol loaded and running\n", CID);
352 if (!strncmp(p + 7, "EC", 2)) {
353 card->ptype = ISDN_PTYPE_EURO;
354 card->interface.features |= ISDN_FEATURE_P_EURO;
356 "isdnloop: (%s) Euro-Protocol loaded and running\n", CID);
365 cmd.command = ISDN_STAT_STAVAIL;
366 cmd.driver = card->myid;
368 card->interface.statcallb(&cmd);
370 if (card->flags & (ISDNLOOP_FLAGS_B1ACTIVE | ISDNLOOP_FLAGS_B2ACTIVE))
371 if (!(card->flags & ISDNLOOP_FLAGS_RBTIMER)) {
372 /* schedule b-channel polling */
373 card->flags |= ISDNLOOP_FLAGS_RBTIMER;
374 spin_lock_irqsave(&card->isdnloop_lock, flags);
375 del_timer(&card->rb_timer);
376 card->rb_timer.function = isdnloop_pollbchan;
377 card->rb_timer.data = (unsigned long) card;
378 card->rb_timer.expires = jiffies + ISDNLOOP_TIMER_BCREAD;
379 add_timer(&card->rb_timer);
380 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
383 spin_lock_irqsave(&card->isdnloop_lock, flags);
384 card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD;
385 add_timer(&card->st_timer);
386 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
390 * Append a packet to the transmit buffer-queue.
393 * channel = Number of B-channel
394 * skb = packet to send.
395 * card = pointer to card-struct
397 * Number of bytes transferred, -E??? on error
400 isdnloop_sendbuf(int channel, struct sk_buff *skb, isdnloop_card *card)
404 struct sk_buff *nskb;
408 "isdnloop: Send packet too large\n");
412 if (!(card->flags & (channel) ? ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE))
414 if (card->sndcount[channel] > ISDNLOOP_MAX_SQUEUE)
416 spin_lock_irqsave(&card->isdnloop_lock, flags);
417 nskb = dev_alloc_skb(skb->len);
419 skb_copy_from_linear_data(skb,
420 skb_put(nskb, len), len);
421 skb_queue_tail(&card->bqueue[channel], nskb);
425 card->sndcount[channel] += len;
426 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
432 * Read the messages from the card's ringbuffer
435 * buf = pointer to buffer.
436 * len = number of bytes to read.
437 * user = flag, 1: called from userlevel 0: called from kernel.
438 * card = pointer to card struct.
440 * number of bytes actually transferred.
443 isdnloop_readstatus(u_char __user *buf, int len, isdnloop_card *card)
448 for (p = buf, count = 0; count < len; p++, count++) {
449 if (card->msg_buf_read == card->msg_buf_write)
451 if (put_user(*card->msg_buf_read++, p))
453 if (card->msg_buf_read > card->msg_buf_end)
454 card->msg_buf_read = card->msg_buf;
460 * Simulate a card's response by appending it to the cards
464 * card = pointer to card struct.
465 * s = pointer to message-string.
466 * ch = channel: 0 = generic messages, 1 and 2 = D-channel messages.
468 * 0 on success, 1 on memory squeeze.
471 isdnloop_fake(isdnloop_card *card, char *s, int ch)
474 int len = strlen(s) + ((ch >= 0) ? 3 : 0);
475 skb = dev_alloc_skb(len);
477 printk(KERN_WARNING "isdnloop: Out of memory in isdnloop_fake\n");
481 sprintf(skb_put(skb, 3), "%02d;", ch);
482 memcpy(skb_put(skb, strlen(s)), s, strlen(s));
483 skb_queue_tail(&card->dqueue, skb);
487 static isdnloop_stat isdnloop_cmd_table[] = {
488 {"BCON_R", 0, 1}, /* B-Channel connect */
489 {"BCON_I", 0, 17}, /* B-Channel connect ind */
490 {"BDIS_R", 0, 2}, /* B-Channel disconnect */
491 {"DDIS_R", 0, 3}, /* D-Channel disconnect */
492 {"DCON_R", 0, 16}, /* D-Channel connect */
493 {"DSCA_R", 0, 4}, /* Dial 1TR6-SPV */
494 {"DCAL_R", 0, 5}, /* Dial */
495 {"EAZC", 0, 6}, /* Clear EAZ listener */
496 {"EAZ", 0, 7}, /* Set EAZ listener */
497 {"SEEAZ", 0, 8}, /* Get EAZ listener */
498 {"MSN", 0, 9}, /* Set/Clear MSN listener */
499 {"MSALL", 0, 10}, /* Set multi MSN listeners */
500 {"SETSIL", 0, 11}, /* Set SI list */
501 {"SEESIL", 0, 12}, /* Get SI list */
502 {"SILC", 0, 13}, /* Clear SI list */
503 {"LOCK", 0, -1}, /* LOCK channel */
504 {"UNLOCK", 0, -1}, /* UNLOCK channel */
505 {"FV2ON", 1, 14}, /* Leased mode on */
506 {"FV2OFF", 1, 15}, /* Leased mode off */
513 * Simulate an error-response from a card.
516 * card = pointer to card struct.
519 isdnloop_fake_err(isdnloop_card *card)
523 snprintf(buf, sizeof(buf), "E%s", card->omsg);
524 isdnloop_fake(card, buf, -1);
525 isdnloop_fake(card, "NAK", -1);
528 static u_char ctable_eu[] = {0x00, 0x11, 0x01, 0x12};
529 static u_char ctable_1t[] = {0x00, 0x3b, 0x01, 0x3a};
532 * Assemble a simplified cause message depending on the
533 * D-channel protocol used.
536 * card = pointer to card struct.
537 * loc = location: 0 = local, 1 = remote.
538 * cau = cause: 1 = busy, 2 = nonexistent callerid, 3 = no user responding.
540 * Pointer to buffer containing the assembled message.
543 isdnloop_unicause(isdnloop_card *card, int loc, int cau)
547 switch (card->ptype) {
548 case ISDN_PTYPE_EURO:
549 sprintf(buf, "E%02X%02X", (loc) ? 4 : 2, ctable_eu[cau]);
551 case ISDN_PTYPE_1TR6:
552 sprintf(buf, "%02X44", ctable_1t[cau]);
561 * Release a virtual connection. Called from timer interrupt, when
562 * called party did not respond.
565 * card = pointer to card struct.
566 * ch = channel (0-based)
569 isdnloop_atimeout(isdnloop_card *card, int ch)
574 spin_lock_irqsave(&card->isdnloop_lock, flags);
576 isdnloop_fake(card->rcard[ch], "DDIS_I", card->rch[ch] + 1);
577 card->rcard[ch]->rcard[card->rch[ch]] = NULL;
578 card->rcard[ch] = NULL;
580 isdnloop_fake(card, "DDIS_I", ch + 1);
581 /* No user responding */
582 sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 3));
583 isdnloop_fake(card, buf, ch + 1);
584 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
588 * Wrapper for isdnloop_atimeout().
591 isdnloop_atimeout0(unsigned long data)
593 isdnloop_card *card = (isdnloop_card *) data;
594 isdnloop_atimeout(card, 0);
598 * Wrapper for isdnloop_atimeout().
601 isdnloop_atimeout1(unsigned long data)
603 isdnloop_card *card = (isdnloop_card *) data;
604 isdnloop_atimeout(card, 1);
608 * Install a watchdog for a user, not responding.
611 * card = pointer to card struct.
612 * ch = channel to watch for.
615 isdnloop_start_ctimer(isdnloop_card *card, int ch)
619 spin_lock_irqsave(&card->isdnloop_lock, flags);
620 init_timer(&card->c_timer[ch]);
621 card->c_timer[ch].expires = jiffies + ISDNLOOP_TIMER_ALERTWAIT;
623 card->c_timer[ch].function = isdnloop_atimeout1;
625 card->c_timer[ch].function = isdnloop_atimeout0;
626 card->c_timer[ch].data = (unsigned long) card;
627 add_timer(&card->c_timer[ch]);
628 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
632 * Kill a pending channel watchdog.
635 * card = pointer to card struct.
636 * ch = channel (0-based).
639 isdnloop_kill_ctimer(isdnloop_card *card, int ch)
643 spin_lock_irqsave(&card->isdnloop_lock, flags);
644 del_timer(&card->c_timer[ch]);
645 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
648 static u_char si2bit[] = {0, 1, 0, 0, 0, 2, 0, 4, 0, 0};
649 static u_char bit2si[] = {1, 5, 7};
652 * Try finding a listener for an outgoing call.
655 * card = pointer to calling card.
656 * p = pointer to ICN-type setup-string.
657 * lch = channel of calling card.
658 * cmd = pointer to struct to be filled when parsing setup.
660 * 0 = found match, alerting should happen.
661 * 1 = found matching number but it is busy.
662 * 2 = no matching listener.
663 * 3 = found matching number but SI does not match.
666 isdnloop_try_call(isdnloop_card *card, char *p, int lch, isdn_ctrl *cmd)
668 isdnloop_card *cc = cards;
676 isdnloop_parse_setup(p, cmd);
678 for (ch = 0; ch < 2; ch++) {
679 /* Exclude ourself */
680 if ((cc == card) && (ch == lch))
684 case ISDN_PTYPE_EURO:
685 for (i = 0; i < 3; i++)
686 if (!(strcmp(cc->s0num[i], cmd->parm.setup.phone)))
689 case ISDN_PTYPE_1TR6:
692 sprintf(nbuf, "%s%c", cc->s0num[0], *e);
693 if (!(strcmp(nbuf, cmd->parm.setup.phone)))
699 spin_lock_irqsave(&card->isdnloop_lock, flags);
701 if (!(cc->rcard[ch])) {
703 if (!(si2bit[cmd->parm.setup.si1] & cc->sil[ch])) {
704 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
707 /* ch is idle, si and number matches */
708 cc->rcard[ch] = card;
710 card->rcard[lch] = cc;
712 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
715 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
716 /* num matches, but busy */
728 * Depending on D-channel protocol and caller/called, modify
732 * card = pointer to card struct.
733 * phone = pointer phone number.
734 * caller = flag: 1 = caller, 0 = called.
736 * pointer to new phone number.
739 isdnloop_vstphone(isdnloop_card *card, char *phone, int caller)
742 static char nphone[30];
748 switch (card->ptype) {
749 case ISDN_PTYPE_EURO:
751 for (i = 0; i < 2; i++)
752 if (!(strcmp(card->s0num[i], phone)))
754 return card->s0num[0];
758 case ISDN_PTYPE_1TR6:
760 sprintf(nphone, "%s%c", card->s0num[0], phone[0]);
763 return &phone[strlen(phone) - 1];
770 * Parse an ICN-type command string sent to the 'card'.
771 * Perform misc. actions depending on the command.
774 * card = pointer to card struct.
777 isdnloop_parse_cmd(isdnloop_card *card)
779 char *p = card->omsg;
782 isdnloop_stat *s = isdnloop_cmd_table;
787 if ((card->omsg[0] != '0') && (card->omsg[2] != ';')) {
788 isdnloop_fake_err(card);
791 ch = card->omsg[1] - '0';
792 if ((ch < 0) || (ch > 2)) {
793 isdnloop_fake_err(card);
798 if (!strncmp(p, s->statstr, strlen(s->statstr))) {
800 if (s->command && (ch != 0)) {
801 isdnloop_fake_err(card);
813 if (card->rcard[ch - 1]) {
814 isdnloop_fake(card->rcard[ch - 1], "BCON_I",
815 card->rch[ch - 1] + 1);
816 isdnloop_fake(card, "BCON_C", ch);
821 if (card->rcard[ch - 1]) {
822 isdnloop_fake(card->rcard[ch - 1], "BCON_C",
823 card->rch[ch - 1] + 1);
828 isdnloop_fake(card, "BDIS_C", ch);
829 if (card->rcard[ch - 1]) {
830 isdnloop_fake(card->rcard[ch - 1], "BDIS_I",
831 card->rch[ch - 1] + 1);
836 isdnloop_kill_ctimer(card, ch - 1);
837 if (card->rcard[ch - 1]) {
838 isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
839 isdnloop_fake(card->rcard[ch - 1], "DCON_C",
840 card->rch[ch - 1] + 1);
841 isdnloop_fake(card, "DCON_C", ch);
846 isdnloop_kill_ctimer(card, ch - 1);
847 if (card->rcard[ch - 1]) {
848 isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
849 isdnloop_fake(card->rcard[ch - 1], "DDIS_I",
850 card->rch[ch - 1] + 1);
851 card->rcard[ch - 1] = NULL;
853 isdnloop_fake(card, "DDIS_C", ch);
856 /* 0x;DSCA_Rdd,yy,zz,oo */
857 if (card->ptype != ISDN_PTYPE_1TR6) {
858 isdnloop_fake_err(card);
863 /* 0x;DCAL_Rdd,yy,zz,oo */
865 switch (isdnloop_try_call(card, p, ch - 1, &cmd)) {
868 sprintf(buf, "D%s_I%s,%02d,%02d,%s",
869 (action == 4) ? "SCA" : "CAL",
870 isdnloop_vstphone(card, cmd.parm.setup.eazmsn, 1),
873 isdnloop_vstphone(card->rcard[ch - 1],
874 cmd.parm.setup.phone, 0));
875 isdnloop_fake(card->rcard[ch - 1], buf, card->rch[ch - 1] + 1);
878 /* si1 does not match, don't alert but start timer */
879 isdnloop_start_ctimer(card, ch - 1);
883 isdnloop_fake(card, "DDIS_I", ch);
884 sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 1));
885 isdnloop_fake(card, buf, ch);
889 isdnloop_fake(card, "DDIS_I", ch);
890 sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 2));
891 isdnloop_fake(card, buf, ch);
897 card->eazlist[ch - 1][0] = '\0';
902 if (strlen(p) >= sizeof(card->eazlist[0]))
904 strcpy(card->eazlist[ch - 1], p);
908 sprintf(buf, "EAZ-LIST: %s", card->eazlist[ch - 1]);
909 isdnloop_fake(card, buf, ch + 1);
921 while (strchr("0157", *p)) {
923 card->sil[ch - 1] |= si2bit[*p - '0'];
927 isdnloop_fake_err(card);
931 sprintf(buf, "SIN-LIST: ");
933 for (i = 0; i < 3; i++)
934 if (card->sil[ch - 1] & (1 << i))
935 p += sprintf(p, "%02d", bit2si[i]);
936 isdnloop_fake(card, buf, ch + 1);
940 card->sil[ch - 1] = 0;
952 * Put command-strings into the of the 'card'. In reality, execute them
953 * right in place by calling isdnloop_parse_cmd(). Also copy every
954 * command to the read message ringbuffer, preceding it with a '>'.
955 * These mesagges can be read at /dev/isdnctrl.
958 * buf = pointer to command buffer.
959 * len = length of buffer data.
960 * user = flag: 1 = called form userlevel, 0 called from kernel.
961 * card = pointer to card struct.
963 * number of bytes transferred (currently always equals len).
966 isdnloop_writecmd(const u_char *buf, int len, int user, isdnloop_card *card)
980 if (copy_from_user(msg, buf, count))
983 memcpy(msg, buf, count);
984 isdnloop_putmsg(card, '>');
985 for (p = msg; count > 0; count--, p++) {
988 isdnloop_putmsg(card, *p);
989 card->omsg[card->optr] = *p;
991 card->omsg[card->optr] = '\0';
993 isdnloop_parse_cmd(card);
995 isdnloop_putmsg(card, '>');
1005 cmd.command = ISDN_STAT_STAVAIL;
1006 cmd.driver = card->myid;
1008 card->interface.statcallb(&cmd);
1013 * Delete card's pending timers, send STOP to linklevel
1016 isdnloop_stopcard(isdnloop_card *card)
1018 unsigned long flags;
1021 spin_lock_irqsave(&card->isdnloop_lock, flags);
1022 if (card->flags & ISDNLOOP_FLAGS_RUNNING) {
1023 card->flags &= ~ISDNLOOP_FLAGS_RUNNING;
1024 del_timer(&card->st_timer);
1025 del_timer(&card->rb_timer);
1026 del_timer(&card->c_timer[0]);
1027 del_timer(&card->c_timer[1]);
1028 cmd.command = ISDN_STAT_STOP;
1029 cmd.driver = card->myid;
1030 card->interface.statcallb(&cmd);
1032 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1036 * Stop all cards before unload.
1039 isdnloop_stopallcards(void)
1041 isdnloop_card *p = cards;
1044 isdnloop_stopcard(p);
1050 * Start a 'card'. Simulate card's boot message and set the phone
1051 * number(s) of the virtual 'S0-Interface'. Install D-channel
1055 * card = pointer to card struct.
1056 * sdefp = pointer to struct holding ioctl parameters.
1058 * 0 on success, -E??? otherwise.
1061 isdnloop_start(isdnloop_card *card, isdnloop_sdef *sdefp)
1063 unsigned long flags;
1067 if (card->flags & ISDNLOOP_FLAGS_RUNNING)
1069 if (copy_from_user((char *) &sdef, (char *) sdefp, sizeof(sdef)))
1072 for (i = 0; i < 3; i++) {
1073 if (!memchr(sdef.num[i], 0, sizeof(sdef.num[i])))
1077 spin_lock_irqsave(&card->isdnloop_lock, flags);
1078 switch (sdef.ptype) {
1079 case ISDN_PTYPE_EURO:
1080 if (isdnloop_fake(card, "DRV1.23EC-Q.931-CAPI-CNS-BASIS-20.02.96",
1082 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1085 card->sil[0] = card->sil[1] = 4;
1086 if (isdnloop_fake(card, "TEI OK", 0)) {
1087 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1090 for (i = 0; i < 3; i++) {
1091 strlcpy(card->s0num[i], sdef.num[i],
1092 sizeof(card->s0num[0]));
1095 case ISDN_PTYPE_1TR6:
1096 if (isdnloop_fake(card, "DRV1.04TC-1TR6-CAPI-CNS-BASIS-29.11.95",
1098 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1101 card->sil[0] = card->sil[1] = 4;
1102 if (isdnloop_fake(card, "TEI OK", 0)) {
1103 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1106 strlcpy(card->s0num[0], sdef.num[0], sizeof(card->s0num[0]));
1107 card->s0num[1][0] = '\0';
1108 card->s0num[2][0] = '\0';
1111 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1112 printk(KERN_WARNING "isdnloop: Illegal D-channel protocol %d\n",
1116 init_timer(&card->st_timer);
1117 card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD;
1118 card->st_timer.function = isdnloop_polldchan;
1119 card->st_timer.data = (unsigned long) card;
1120 add_timer(&card->st_timer);
1121 card->flags |= ISDNLOOP_FLAGS_RUNNING;
1122 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1127 * Main handler for commands sent by linklevel.
1130 isdnloop_command(isdn_ctrl *c, isdnloop_card *card)
1138 switch (c->command) {
1139 case ISDN_CMD_IOCTL:
1140 memcpy(&a, c->parm.num, sizeof(ulong));
1142 case ISDNLOOP_IOCTL_DEBUGVAR:
1143 return (ulong) card;
1144 case ISDNLOOP_IOCTL_STARTUP:
1145 if (!access_ok(VERIFY_READ, (void *) a, sizeof(isdnloop_sdef)))
1147 return isdnloop_start(card, (isdnloop_sdef *) a);
1149 case ISDNLOOP_IOCTL_ADDCARD:
1150 if (copy_from_user((char *)&cdef,
1154 return isdnloop_addcard(cdef.id1);
1156 case ISDNLOOP_IOCTL_LEASEDCFG:
1158 if (!card->leased) {
1160 while (card->ptype == ISDN_PTYPE_UNKNOWN)
1161 schedule_timeout_interruptible(10);
1162 schedule_timeout_interruptible(10);
1163 sprintf(cbuf, "00;FV2ON\n01;EAZ1\n02;EAZ2\n");
1164 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1166 "isdnloop: (%s) Leased-line mode enabled\n",
1168 cmd.command = ISDN_STAT_RUN;
1169 cmd.driver = card->myid;
1171 card->interface.statcallb(&cmd);
1176 sprintf(cbuf, "00;FV2OFF\n");
1177 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1179 "isdnloop: (%s) Leased-line mode disabled\n",
1181 cmd.command = ISDN_STAT_RUN;
1182 cmd.driver = card->myid;
1184 card->interface.statcallb(&cmd);
1193 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1197 if ((c->arg & 255) < ISDNLOOP_BCH) {
1202 p = c->parm.setup.phone;
1203 if (*p == 's' || *p == 'S') {
1206 strcpy(dcode, "SCA");
1209 strcpy(dcode, "CAL");
1210 snprintf(cbuf, sizeof(cbuf),
1211 "%02d;D%s_R%s,%02d,%02d,%s\n", (int) (a + 1),
1212 dcode, p, c->parm.setup.si1,
1213 c->parm.setup.si2, c->parm.setup.eazmsn);
1214 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1217 case ISDN_CMD_ACCEPTD:
1218 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1220 if (c->arg < ISDNLOOP_BCH) {
1223 switch (card->l2_proto[a - 1]) {
1224 case ISDN_PROTO_L2_X75I:
1225 sprintf(cbuf, "%02d;BX75\n", (int) a);
1227 #ifdef CONFIG_ISDN_X25
1228 case ISDN_PROTO_L2_X25DTE:
1229 sprintf(cbuf, "%02d;BX2T\n", (int) a);
1231 case ISDN_PROTO_L2_X25DCE:
1232 sprintf(cbuf, "%02d;BX2C\n", (int) a);
1235 case ISDN_PROTO_L2_HDLC:
1236 sprintf(cbuf, "%02d;BTRA\n", (int) a);
1240 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1241 sprintf(cbuf, "%02d;DCON_R\n", (int) a);
1242 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1245 case ISDN_CMD_ACCEPTB:
1246 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1248 if (c->arg < ISDNLOOP_BCH) {
1250 switch (card->l2_proto[a - 1]) {
1251 case ISDN_PROTO_L2_X75I:
1252 sprintf(cbuf, "%02d;BCON_R,BX75\n", (int) a);
1254 #ifdef CONFIG_ISDN_X25
1255 case ISDN_PROTO_L2_X25DTE:
1256 sprintf(cbuf, "%02d;BCON_R,BX2T\n", (int) a);
1258 case ISDN_PROTO_L2_X25DCE:
1259 sprintf(cbuf, "%02d;BCON_R,BX2C\n", (int) a);
1262 case ISDN_PROTO_L2_HDLC:
1263 sprintf(cbuf, "%02d;BCON_R,BTRA\n", (int) a);
1266 sprintf(cbuf, "%02d;BCON_R\n", (int) a);
1268 printk(KERN_DEBUG "isdnloop writecmd '%s'\n", cbuf);
1269 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1271 case ISDN_CMD_HANGUP:
1272 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1274 if (c->arg < ISDNLOOP_BCH) {
1276 sprintf(cbuf, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a, (int) a);
1277 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1280 case ISDN_CMD_SETEAZ:
1281 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1285 if (c->arg < ISDNLOOP_BCH) {
1287 if (card->ptype == ISDN_PTYPE_EURO) {
1288 sprintf(cbuf, "%02d;MS%s%s\n", (int) a,
1289 c->parm.num[0] ? "N" : "ALL", c->parm.num);
1291 sprintf(cbuf, "%02d;EAZ%s\n", (int) a,
1292 c->parm.num[0] ? c->parm.num : (u_char *) "0123456789");
1293 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1296 case ISDN_CMD_CLREAZ:
1297 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1301 if (c->arg < ISDNLOOP_BCH) {
1303 if (card->ptype == ISDN_PTYPE_EURO)
1304 sprintf(cbuf, "%02d;MSNC\n", (int) a);
1306 sprintf(cbuf, "%02d;EAZC\n", (int) a);
1307 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1310 case ISDN_CMD_SETL2:
1311 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1313 if ((c->arg & 255) < ISDNLOOP_BCH) {
1316 case ISDN_PROTO_L2_X75I:
1317 sprintf(cbuf, "%02d;BX75\n", (int) (a & 255) + 1);
1319 #ifdef CONFIG_ISDN_X25
1320 case ISDN_PROTO_L2_X25DTE:
1321 sprintf(cbuf, "%02d;BX2T\n", (int) (a & 255) + 1);
1323 case ISDN_PROTO_L2_X25DCE:
1324 sprintf(cbuf, "%02d;BX2C\n", (int) (a & 255) + 1);
1327 case ISDN_PROTO_L2_HDLC:
1328 sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
1330 case ISDN_PROTO_L2_TRANS:
1331 sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
1336 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1337 card->l2_proto[a & 255] = (a >> 8);
1340 case ISDN_CMD_SETL3:
1341 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1352 * Find card with given driverId
1354 static inline isdnloop_card *
1355 isdnloop_findcard(int driverid)
1357 isdnloop_card *p = cards;
1360 if (p->myid == driverid)
1364 return (isdnloop_card *) 0;
1368 * Wrapper functions for interface to linklevel
1371 if_command(isdn_ctrl *c)
1373 isdnloop_card *card = isdnloop_findcard(c->driver);
1376 return isdnloop_command(c, card);
1378 "isdnloop: if_command called with invalid driverId!\n");
1383 if_writecmd(const u_char __user *buf, int len, int id, int channel)
1385 isdnloop_card *card = isdnloop_findcard(id);
1388 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1390 return isdnloop_writecmd(buf, len, 1, card);
1393 "isdnloop: if_writecmd called with invalid driverId!\n");
1398 if_readstatus(u_char __user *buf, int len, int id, int channel)
1400 isdnloop_card *card = isdnloop_findcard(id);
1403 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1405 return isdnloop_readstatus(buf, len, card);
1408 "isdnloop: if_readstatus called with invalid driverId!\n");
1413 if_sendbuf(int id, int channel, int ack, struct sk_buff *skb)
1415 isdnloop_card *card = isdnloop_findcard(id);
1418 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1420 /* ack request stored in skb scratch area */
1422 return isdnloop_sendbuf(channel, skb, card);
1425 "isdnloop: if_sendbuf called with invalid driverId!\n");
1430 * Allocate a new card-struct, initialize it
1431 * link it into cards-list and register it at linklevel.
1433 static isdnloop_card *
1434 isdnloop_initcard(char *id)
1436 isdnloop_card *card;
1438 card = kzalloc(sizeof(isdnloop_card), GFP_KERNEL);
1441 "isdnloop: (%s) Could not allocate card-struct.\n", id);
1442 return (isdnloop_card *) 0;
1444 card->interface.owner = THIS_MODULE;
1445 card->interface.channels = ISDNLOOP_BCH;
1446 card->interface.hl_hdrlen = 1; /* scratch area for storing ack flag*/
1447 card->interface.maxbufsize = 4000;
1448 card->interface.command = if_command;
1449 card->interface.writebuf_skb = if_sendbuf;
1450 card->interface.writecmd = if_writecmd;
1451 card->interface.readstat = if_readstatus;
1452 card->interface.features = ISDN_FEATURE_L2_X75I |
1453 #ifdef CONFIG_ISDN_X25
1454 ISDN_FEATURE_L2_X25DTE |
1455 ISDN_FEATURE_L2_X25DCE |
1457 ISDN_FEATURE_L2_HDLC |
1458 ISDN_FEATURE_L3_TRANS |
1459 ISDN_FEATURE_P_UNKNOWN;
1460 card->ptype = ISDN_PTYPE_UNKNOWN;
1461 strlcpy(card->interface.id, id, sizeof(card->interface.id));
1462 card->msg_buf_write = card->msg_buf;
1463 card->msg_buf_read = card->msg_buf;
1464 card->msg_buf_end = &card->msg_buf[sizeof(card->msg_buf) - 1];
1465 for (i = 0; i < ISDNLOOP_BCH; i++) {
1466 card->l2_proto[i] = ISDN_PROTO_L2_X75I;
1467 skb_queue_head_init(&card->bqueue[i]);
1469 skb_queue_head_init(&card->dqueue);
1470 spin_lock_init(&card->isdnloop_lock);
1473 if (!register_isdn(&card->interface)) {
1474 cards = cards->next;
1476 "isdnloop: Unable to register %s\n", id);
1478 return (isdnloop_card *) 0;
1480 card->myid = card->interface.channels;
1485 isdnloop_addcard(char *id1)
1487 isdnloop_card *card;
1488 card = isdnloop_initcard(id1);
1493 "isdnloop: (%s) virtual card added\n",
1494 card->interface.id);
1502 return isdnloop_addcard(isdnloop_id);
1511 isdnloop_card *card = cards;
1512 isdnloop_card *last;
1515 isdnloop_stopallcards();
1517 cmd.command = ISDN_STAT_UNLOAD;
1518 cmd.driver = card->myid;
1519 card->interface.statcallb(&cmd);
1520 for (i = 0; i < ISDNLOOP_BCH; i++)
1521 isdnloop_free_queue(card, i);
1527 skb_queue_purge(&card->dqueue);
1531 printk(KERN_NOTICE "isdnloop-ISDN-driver unloaded\n");
1534 module_init(isdnloop_init);
1535 module_exit(isdnloop_exit);