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.
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(struct timer_list *t)
95 isdnloop_card *card = from_timer(card, t, rb_timer);
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(struct timer_list *t)
310 isdnloop_card *card = from_timer(card, t, st_timer);
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.expires = jiffies + ISDNLOOP_TIMER_BCREAD;
377 add_timer(&card->rb_timer);
378 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
381 spin_lock_irqsave(&card->isdnloop_lock, flags);
382 card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD;
383 add_timer(&card->st_timer);
384 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
388 * Append a packet to the transmit buffer-queue.
391 * channel = Number of B-channel
392 * skb = packet to send.
393 * card = pointer to card-struct
395 * Number of bytes transferred, -E??? on error
398 isdnloop_sendbuf(int channel, struct sk_buff *skb, isdnloop_card *card)
402 struct sk_buff *nskb;
406 "isdnloop: Send packet too large\n");
410 if (!(card->flags & (channel ? ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE)))
412 if (card->sndcount[channel] > ISDNLOOP_MAX_SQUEUE)
414 spin_lock_irqsave(&card->isdnloop_lock, flags);
415 nskb = dev_alloc_skb(skb->len);
417 skb_copy_from_linear_data(skb,
418 skb_put(nskb, len), 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);
473 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 skb_put_data(skb, s, strlen(s));
481 skb_queue_tail(&card->dqueue, skb);
485 static isdnloop_stat isdnloop_cmd_table[] = {
486 {"BCON_R", 0, 1}, /* B-Channel connect */
487 {"BCON_I", 0, 17}, /* B-Channel connect ind */
488 {"BDIS_R", 0, 2}, /* B-Channel disconnect */
489 {"DDIS_R", 0, 3}, /* D-Channel disconnect */
490 {"DCON_R", 0, 16}, /* D-Channel connect */
491 {"DSCA_R", 0, 4}, /* Dial 1TR6-SPV */
492 {"DCAL_R", 0, 5}, /* Dial */
493 {"EAZC", 0, 6}, /* Clear EAZ listener */
494 {"EAZ", 0, 7}, /* Set EAZ listener */
495 {"SEEAZ", 0, 8}, /* Get EAZ listener */
496 {"MSN", 0, 9}, /* Set/Clear MSN listener */
497 {"MSALL", 0, 10}, /* Set multi MSN listeners */
498 {"SETSIL", 0, 11}, /* Set SI list */
499 {"SEESIL", 0, 12}, /* Get SI list */
500 {"SILC", 0, 13}, /* Clear SI list */
501 {"LOCK", 0, -1}, /* LOCK channel */
502 {"UNLOCK", 0, -1}, /* UNLOCK channel */
503 {"FV2ON", 1, 14}, /* Leased mode on */
504 {"FV2OFF", 1, 15}, /* Leased mode off */
511 * Simulate an error-response from a card.
514 * card = pointer to card struct.
517 isdnloop_fake_err(isdnloop_card *card)
521 snprintf(buf, sizeof(buf), "E%s", card->omsg);
522 isdnloop_fake(card, buf, -1);
523 isdnloop_fake(card, "NAK", -1);
526 static u_char ctable_eu[] = {0x00, 0x11, 0x01, 0x12};
527 static u_char ctable_1t[] = {0x00, 0x3b, 0x01, 0x3a};
530 * Assemble a simplified cause message depending on the
531 * D-channel protocol used.
534 * card = pointer to card struct.
535 * loc = location: 0 = local, 1 = remote.
536 * cau = cause: 1 = busy, 2 = nonexistent callerid, 3 = no user responding.
538 * Pointer to buffer containing the assembled message.
541 isdnloop_unicause(isdnloop_card *card, int loc, int cau)
545 switch (card->ptype) {
546 case ISDN_PTYPE_EURO:
547 sprintf(buf, "E%02X%02X", (loc) ? 4 : 2, ctable_eu[cau]);
549 case ISDN_PTYPE_1TR6:
550 sprintf(buf, "%02X44", ctable_1t[cau]);
559 * Release a virtual connection. Called from timer interrupt, when
560 * called party did not respond.
563 * card = pointer to card struct.
564 * ch = channel (0-based)
567 isdnloop_atimeout(isdnloop_card *card, int ch)
572 spin_lock_irqsave(&card->isdnloop_lock, flags);
574 isdnloop_fake(card->rcard[ch], "DDIS_I", card->rch[ch] + 1);
575 card->rcard[ch]->rcard[card->rch[ch]] = NULL;
576 card->rcard[ch] = NULL;
578 isdnloop_fake(card, "DDIS_I", ch + 1);
579 /* No user responding */
580 sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 3));
581 isdnloop_fake(card, buf, ch + 1);
582 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
586 * Wrapper for isdnloop_atimeout().
589 isdnloop_atimeout0(struct timer_list *t)
591 isdnloop_card *card = from_timer(card, t, c_timer[0]);
593 isdnloop_atimeout(card, 0);
597 * Wrapper for isdnloop_atimeout().
600 isdnloop_atimeout1(struct timer_list *t)
602 isdnloop_card *card = from_timer(card, t, c_timer[1]);
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 timer_setup(&card->c_timer[ch], ch ? isdnloop_atimeout1
621 : isdnloop_atimeout0, 0);
622 card->c_timer[ch].expires = jiffies + ISDNLOOP_TIMER_ALERTWAIT;
623 add_timer(&card->c_timer[ch]);
624 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
628 * Kill a pending channel watchdog.
631 * card = pointer to card struct.
632 * ch = channel (0-based).
635 isdnloop_kill_ctimer(isdnloop_card *card, int ch)
639 spin_lock_irqsave(&card->isdnloop_lock, flags);
640 del_timer(&card->c_timer[ch]);
641 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
644 static u_char si2bit[] = {0, 1, 0, 0, 0, 2, 0, 4, 0, 0};
645 static u_char bit2si[] = {1, 5, 7};
648 * Try finding a listener for an outgoing call.
651 * card = pointer to calling card.
652 * p = pointer to ICN-type setup-string.
653 * lch = channel of calling card.
654 * cmd = pointer to struct to be filled when parsing setup.
656 * 0 = found match, alerting should happen.
657 * 1 = found matching number but it is busy.
658 * 2 = no matching listener.
659 * 3 = found matching number but SI does not match.
662 isdnloop_try_call(isdnloop_card *card, char *p, int lch, isdn_ctrl *cmd)
664 isdnloop_card *cc = cards;
672 isdnloop_parse_setup(p, cmd);
674 for (ch = 0; ch < 2; ch++) {
675 /* Exclude ourself */
676 if ((cc == card) && (ch == lch))
680 case ISDN_PTYPE_EURO:
681 for (i = 0; i < 3; i++)
682 if (!(strcmp(cc->s0num[i], cmd->parm.setup.phone)))
685 case ISDN_PTYPE_1TR6:
688 sprintf(nbuf, "%s%c", cc->s0num[0], *e);
689 if (!(strcmp(nbuf, cmd->parm.setup.phone)))
695 spin_lock_irqsave(&card->isdnloop_lock, flags);
697 if (!(cc->rcard[ch])) {
699 if (!(si2bit[cmd->parm.setup.si1] & cc->sil[ch])) {
700 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
703 /* ch is idle, si and number matches */
704 cc->rcard[ch] = card;
706 card->rcard[lch] = cc;
708 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
711 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
712 /* num matches, but busy */
724 * Depending on D-channel protocol and caller/called, modify
728 * card = pointer to card struct.
729 * phone = pointer phone number.
730 * caller = flag: 1 = caller, 0 = called.
732 * pointer to new phone number.
735 isdnloop_vstphone(isdnloop_card *card, char *phone, int caller)
738 static char nphone[30];
744 switch (card->ptype) {
745 case ISDN_PTYPE_EURO:
747 for (i = 0; i < 2; i++)
748 if (!(strcmp(card->s0num[i], phone)))
750 return card->s0num[0];
754 case ISDN_PTYPE_1TR6:
756 sprintf(nphone, "%s%c", card->s0num[0], phone[0]);
759 return &phone[strlen(phone) - 1];
766 * Parse an ICN-type command string sent to the 'card'.
767 * Perform misc. actions depending on the command.
770 * card = pointer to card struct.
773 isdnloop_parse_cmd(isdnloop_card *card)
775 char *p = card->omsg;
778 isdnloop_stat *s = isdnloop_cmd_table;
783 if ((card->omsg[0] != '0') && (card->omsg[2] != ';')) {
784 isdnloop_fake_err(card);
787 ch = card->omsg[1] - '0';
788 if ((ch < 0) || (ch > 2)) {
789 isdnloop_fake_err(card);
794 if (!strncmp(p, s->statstr, strlen(s->statstr))) {
796 if (s->command && (ch != 0)) {
797 isdnloop_fake_err(card);
809 if (card->rcard[ch - 1]) {
810 isdnloop_fake(card->rcard[ch - 1], "BCON_I",
811 card->rch[ch - 1] + 1);
812 isdnloop_fake(card, "BCON_C", ch);
817 if (card->rcard[ch - 1]) {
818 isdnloop_fake(card->rcard[ch - 1], "BCON_C",
819 card->rch[ch - 1] + 1);
824 isdnloop_fake(card, "BDIS_C", ch);
825 if (card->rcard[ch - 1]) {
826 isdnloop_fake(card->rcard[ch - 1], "BDIS_I",
827 card->rch[ch - 1] + 1);
832 isdnloop_kill_ctimer(card, ch - 1);
833 if (card->rcard[ch - 1]) {
834 isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
835 isdnloop_fake(card->rcard[ch - 1], "DCON_C",
836 card->rch[ch - 1] + 1);
837 isdnloop_fake(card, "DCON_C", ch);
842 isdnloop_kill_ctimer(card, ch - 1);
843 if (card->rcard[ch - 1]) {
844 isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
845 isdnloop_fake(card->rcard[ch - 1], "DDIS_I",
846 card->rch[ch - 1] + 1);
847 card->rcard[ch - 1] = NULL;
849 isdnloop_fake(card, "DDIS_C", ch);
852 /* 0x;DSCA_Rdd,yy,zz,oo */
853 if (card->ptype != ISDN_PTYPE_1TR6) {
854 isdnloop_fake_err(card);
859 /* 0x;DCAL_Rdd,yy,zz,oo */
861 switch (isdnloop_try_call(card, p, ch - 1, &cmd)) {
864 sprintf(buf, "D%s_I%s,%02d,%02d,%s",
865 (action == 4) ? "SCA" : "CAL",
866 isdnloop_vstphone(card, cmd.parm.setup.eazmsn, 1),
869 isdnloop_vstphone(card->rcard[ch - 1],
870 cmd.parm.setup.phone, 0));
871 isdnloop_fake(card->rcard[ch - 1], buf, card->rch[ch - 1] + 1);
874 /* si1 does not match, don't alert but start timer */
875 isdnloop_start_ctimer(card, ch - 1);
879 isdnloop_fake(card, "DDIS_I", ch);
880 sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 1));
881 isdnloop_fake(card, buf, ch);
885 isdnloop_fake(card, "DDIS_I", ch);
886 sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 2));
887 isdnloop_fake(card, buf, ch);
893 card->eazlist[ch - 1][0] = '\0';
898 if (strlen(p) >= sizeof(card->eazlist[0]))
900 strcpy(card->eazlist[ch - 1], p);
904 sprintf(buf, "EAZ-LIST: %s", card->eazlist[ch - 1]);
905 isdnloop_fake(card, buf, ch + 1);
917 while (strchr("0157", *p)) {
919 card->sil[ch - 1] |= si2bit[*p - '0'];
923 isdnloop_fake_err(card);
927 sprintf(buf, "SIN-LIST: ");
929 for (i = 0; i < 3; i++)
930 if (card->sil[ch - 1] & (1 << i))
931 p += sprintf(p, "%02d", bit2si[i]);
932 isdnloop_fake(card, buf, ch + 1);
936 card->sil[ch - 1] = 0;
948 * Put command-strings into the of the 'card'. In reality, execute them
949 * right in place by calling isdnloop_parse_cmd(). Also copy every
950 * command to the read message ringbuffer, preceding it with a '>'.
951 * These mesagges can be read at /dev/isdnctrl.
954 * buf = pointer to command buffer.
955 * len = length of buffer data.
956 * user = flag: 1 = called form userlevel, 0 called from kernel.
957 * card = pointer to card struct.
959 * number of bytes transferred (currently always equals len).
962 isdnloop_writecmd(const u_char *buf, int len, int user, isdnloop_card *card)
976 if (copy_from_user(msg, buf, count))
979 memcpy(msg, buf, count);
980 isdnloop_putmsg(card, '>');
981 for (p = msg; count > 0; count--, p++) {
984 isdnloop_putmsg(card, *p);
985 card->omsg[card->optr] = *p;
987 card->omsg[card->optr] = '\0';
989 isdnloop_parse_cmd(card);
991 isdnloop_putmsg(card, '>');
1001 cmd.command = ISDN_STAT_STAVAIL;
1002 cmd.driver = card->myid;
1004 card->interface.statcallb(&cmd);
1009 * Delete card's pending timers, send STOP to linklevel
1012 isdnloop_stopcard(isdnloop_card *card)
1014 unsigned long flags;
1017 spin_lock_irqsave(&card->isdnloop_lock, flags);
1018 if (card->flags & ISDNLOOP_FLAGS_RUNNING) {
1019 card->flags &= ~ISDNLOOP_FLAGS_RUNNING;
1020 del_timer(&card->st_timer);
1021 del_timer(&card->rb_timer);
1022 del_timer(&card->c_timer[0]);
1023 del_timer(&card->c_timer[1]);
1024 cmd.command = ISDN_STAT_STOP;
1025 cmd.driver = card->myid;
1026 card->interface.statcallb(&cmd);
1028 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1032 * Stop all cards before unload.
1035 isdnloop_stopallcards(void)
1037 isdnloop_card *p = cards;
1040 isdnloop_stopcard(p);
1046 * Start a 'card'. Simulate card's boot message and set the phone
1047 * number(s) of the virtual 'S0-Interface'. Install D-channel
1051 * card = pointer to card struct.
1052 * sdefp = pointer to struct holding ioctl parameters.
1054 * 0 on success, -E??? otherwise.
1057 isdnloop_start(isdnloop_card *card, isdnloop_sdef *sdefp)
1059 unsigned long flags;
1063 if (card->flags & ISDNLOOP_FLAGS_RUNNING)
1065 if (copy_from_user((char *) &sdef, (char *) sdefp, sizeof(sdef)))
1068 for (i = 0; i < 3; i++) {
1069 if (!memchr(sdef.num[i], 0, sizeof(sdef.num[i])))
1073 spin_lock_irqsave(&card->isdnloop_lock, flags);
1074 switch (sdef.ptype) {
1075 case ISDN_PTYPE_EURO:
1076 if (isdnloop_fake(card, "DRV1.23EC-Q.931-CAPI-CNS-BASIS-20.02.96",
1078 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1081 card->sil[0] = card->sil[1] = 4;
1082 if (isdnloop_fake(card, "TEI OK", 0)) {
1083 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1086 for (i = 0; i < 3; i++) {
1087 strlcpy(card->s0num[i], sdef.num[i],
1088 sizeof(card->s0num[0]));
1091 case ISDN_PTYPE_1TR6:
1092 if (isdnloop_fake(card, "DRV1.04TC-1TR6-CAPI-CNS-BASIS-29.11.95",
1094 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1097 card->sil[0] = card->sil[1] = 4;
1098 if (isdnloop_fake(card, "TEI OK", 0)) {
1099 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1102 strlcpy(card->s0num[0], sdef.num[0], sizeof(card->s0num[0]));
1103 card->s0num[1][0] = '\0';
1104 card->s0num[2][0] = '\0';
1107 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1108 printk(KERN_WARNING "isdnloop: Illegal D-channel protocol %d\n",
1112 timer_setup(&card->rb_timer, isdnloop_pollbchan, 0);
1113 timer_setup(&card->st_timer, isdnloop_polldchan, 0);
1114 card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD;
1115 add_timer(&card->st_timer);
1116 card->flags |= ISDNLOOP_FLAGS_RUNNING;
1117 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1122 * Main handler for commands sent by linklevel.
1125 isdnloop_command(isdn_ctrl *c, isdnloop_card *card)
1133 switch (c->command) {
1134 case ISDN_CMD_IOCTL:
1135 memcpy(&a, c->parm.num, sizeof(ulong));
1137 case ISDNLOOP_IOCTL_DEBUGVAR:
1138 return (ulong) card;
1139 case ISDNLOOP_IOCTL_STARTUP:
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) {
1195 p = c->parm.setup.phone;
1196 if (*p == 's' || *p == 'S') {
1199 strcpy(dcode, "SCA");
1202 strcpy(dcode, "CAL");
1203 snprintf(cbuf, sizeof(cbuf),
1204 "%02d;D%s_R%s,%02d,%02d,%s\n", (int) (a + 1),
1205 dcode, p, c->parm.setup.si1,
1206 c->parm.setup.si2, c->parm.setup.eazmsn);
1207 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1210 case ISDN_CMD_ACCEPTD:
1211 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1213 if (c->arg < ISDNLOOP_BCH) {
1216 switch (card->l2_proto[a - 1]) {
1217 case ISDN_PROTO_L2_X75I:
1218 sprintf(cbuf, "%02d;BX75\n", (int) a);
1220 #ifdef CONFIG_ISDN_X25
1221 case ISDN_PROTO_L2_X25DTE:
1222 sprintf(cbuf, "%02d;BX2T\n", (int) a);
1224 case ISDN_PROTO_L2_X25DCE:
1225 sprintf(cbuf, "%02d;BX2C\n", (int) a);
1228 case ISDN_PROTO_L2_HDLC:
1229 sprintf(cbuf, "%02d;BTRA\n", (int) a);
1233 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1234 sprintf(cbuf, "%02d;DCON_R\n", (int) a);
1235 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1238 case ISDN_CMD_ACCEPTB:
1239 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1241 if (c->arg < ISDNLOOP_BCH) {
1243 switch (card->l2_proto[a - 1]) {
1244 case ISDN_PROTO_L2_X75I:
1245 sprintf(cbuf, "%02d;BCON_R,BX75\n", (int) a);
1247 #ifdef CONFIG_ISDN_X25
1248 case ISDN_PROTO_L2_X25DTE:
1249 sprintf(cbuf, "%02d;BCON_R,BX2T\n", (int) a);
1251 case ISDN_PROTO_L2_X25DCE:
1252 sprintf(cbuf, "%02d;BCON_R,BX2C\n", (int) a);
1255 case ISDN_PROTO_L2_HDLC:
1256 sprintf(cbuf, "%02d;BCON_R,BTRA\n", (int) a);
1259 sprintf(cbuf, "%02d;BCON_R\n", (int) a);
1261 printk(KERN_DEBUG "isdnloop writecmd '%s'\n", cbuf);
1262 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1264 case ISDN_CMD_HANGUP:
1265 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1267 if (c->arg < ISDNLOOP_BCH) {
1269 sprintf(cbuf, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a, (int) a);
1270 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1273 case ISDN_CMD_SETEAZ:
1274 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1278 if (c->arg < ISDNLOOP_BCH) {
1280 if (card->ptype == ISDN_PTYPE_EURO) {
1281 sprintf(cbuf, "%02d;MS%s%s\n", (int) a,
1282 c->parm.num[0] ? "N" : "ALL", c->parm.num);
1284 sprintf(cbuf, "%02d;EAZ%s\n", (int) a,
1285 c->parm.num[0] ? c->parm.num : (u_char *) "0123456789");
1286 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1289 case ISDN_CMD_CLREAZ:
1290 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1294 if (c->arg < ISDNLOOP_BCH) {
1296 if (card->ptype == ISDN_PTYPE_EURO)
1297 sprintf(cbuf, "%02d;MSNC\n", (int) a);
1299 sprintf(cbuf, "%02d;EAZC\n", (int) a);
1300 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1303 case ISDN_CMD_SETL2:
1304 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1306 if ((c->arg & 255) < ISDNLOOP_BCH) {
1309 case ISDN_PROTO_L2_X75I:
1310 sprintf(cbuf, "%02d;BX75\n", (int) (a & 255) + 1);
1312 #ifdef CONFIG_ISDN_X25
1313 case ISDN_PROTO_L2_X25DTE:
1314 sprintf(cbuf, "%02d;BX2T\n", (int) (a & 255) + 1);
1316 case ISDN_PROTO_L2_X25DCE:
1317 sprintf(cbuf, "%02d;BX2C\n", (int) (a & 255) + 1);
1320 case ISDN_PROTO_L2_HDLC:
1321 sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
1323 case ISDN_PROTO_L2_TRANS:
1324 sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
1329 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1330 card->l2_proto[a & 255] = (a >> 8);
1333 case ISDN_CMD_SETL3:
1334 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1345 * Find card with given driverId
1347 static inline isdnloop_card *
1348 isdnloop_findcard(int driverid)
1350 isdnloop_card *p = cards;
1353 if (p->myid == driverid)
1357 return (isdnloop_card *) 0;
1361 * Wrapper functions for interface to linklevel
1364 if_command(isdn_ctrl *c)
1366 isdnloop_card *card = isdnloop_findcard(c->driver);
1369 return isdnloop_command(c, card);
1371 "isdnloop: if_command called with invalid driverId!\n");
1376 if_writecmd(const u_char __user *buf, int len, int id, int channel)
1378 isdnloop_card *card = isdnloop_findcard(id);
1381 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1383 return isdnloop_writecmd(buf, len, 1, card);
1386 "isdnloop: if_writecmd called with invalid driverId!\n");
1391 if_readstatus(u_char __user *buf, int len, int id, int channel)
1393 isdnloop_card *card = isdnloop_findcard(id);
1396 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1398 return isdnloop_readstatus(buf, len, card);
1401 "isdnloop: if_readstatus called with invalid driverId!\n");
1406 if_sendbuf(int id, int channel, int ack, struct sk_buff *skb)
1408 isdnloop_card *card = isdnloop_findcard(id);
1411 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1413 /* ack request stored in skb scratch area */
1415 return isdnloop_sendbuf(channel, skb, card);
1418 "isdnloop: if_sendbuf called with invalid driverId!\n");
1423 * Allocate a new card-struct, initialize it
1424 * link it into cards-list and register it at linklevel.
1426 static isdnloop_card *
1427 isdnloop_initcard(char *id)
1429 isdnloop_card *card;
1431 card = kzalloc(sizeof(isdnloop_card), GFP_KERNEL);
1434 "isdnloop: (%s) Could not allocate card-struct.\n", id);
1435 return (isdnloop_card *) 0;
1437 card->interface.owner = THIS_MODULE;
1438 card->interface.channels = ISDNLOOP_BCH;
1439 card->interface.hl_hdrlen = 1; /* scratch area for storing ack flag*/
1440 card->interface.maxbufsize = 4000;
1441 card->interface.command = if_command;
1442 card->interface.writebuf_skb = if_sendbuf;
1443 card->interface.writecmd = if_writecmd;
1444 card->interface.readstat = if_readstatus;
1445 card->interface.features = ISDN_FEATURE_L2_X75I |
1446 #ifdef CONFIG_ISDN_X25
1447 ISDN_FEATURE_L2_X25DTE |
1448 ISDN_FEATURE_L2_X25DCE |
1450 ISDN_FEATURE_L2_HDLC |
1451 ISDN_FEATURE_L3_TRANS |
1452 ISDN_FEATURE_P_UNKNOWN;
1453 card->ptype = ISDN_PTYPE_UNKNOWN;
1454 strlcpy(card->interface.id, id, sizeof(card->interface.id));
1455 card->msg_buf_write = card->msg_buf;
1456 card->msg_buf_read = card->msg_buf;
1457 card->msg_buf_end = &card->msg_buf[sizeof(card->msg_buf) - 1];
1458 for (i = 0; i < ISDNLOOP_BCH; i++) {
1459 card->l2_proto[i] = ISDN_PROTO_L2_X75I;
1460 skb_queue_head_init(&card->bqueue[i]);
1462 skb_queue_head_init(&card->dqueue);
1463 spin_lock_init(&card->isdnloop_lock);
1466 if (!register_isdn(&card->interface)) {
1467 cards = cards->next;
1469 "isdnloop: Unable to register %s\n", id);
1471 return (isdnloop_card *) 0;
1473 card->myid = card->interface.channels;
1478 isdnloop_addcard(char *id1)
1480 isdnloop_card *card;
1481 card = isdnloop_initcard(id1);
1486 "isdnloop: (%s) virtual card added\n",
1487 card->interface.id);
1495 return isdnloop_addcard(isdnloop_id);
1504 isdnloop_card *card = cards;
1505 isdnloop_card *last;
1508 isdnloop_stopallcards();
1510 cmd.command = ISDN_STAT_UNLOAD;
1511 cmd.driver = card->myid;
1512 card->interface.statcallb(&cmd);
1513 for (i = 0; i < ISDNLOOP_BCH; i++)
1514 isdnloop_free_queue(card, i);
1520 skb_queue_purge(&card->dqueue);
1524 printk(KERN_NOTICE "isdnloop-ISDN-driver unloaded\n");
1527 module_init(isdnloop_init);
1528 module_exit(isdnloop_exit);