ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / isdn / isdnloop / isdnloop.c
1 /* $Id: isdnloop.c,v 1.11.6.7 2001/11/11 19:54:31 kai Exp $
2  *
3  * ISDN low-level module implementing a dummy loop driver.
4  *
5  * Copyright 1997 by Fritz Elfert (fritz@isdn4linux.de)
6  *
7  * This software may be used and distributed according to the terms
8  * of the GNU General Public License, incorporated herein by reference.
9  *
10  */
11
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/interrupt.h>
15 #include <linux/init.h>
16 #include "isdnloop.h"
17
18 static char *revision = "$Revision: 1.11.6.7 $";
19 static char *isdnloop_id = "loop0";
20
21 MODULE_DESCRIPTION("ISDN4Linux: Pseudo Driver that simulates an ISDN card");
22 MODULE_AUTHOR("Fritz Elfert");
23 MODULE_LICENSE("GPL");
24 MODULE_PARM(isdnloop_id, "s");
25 MODULE_PARM_DESC(isdnloop_id, "ID-String of first card");
26
27 static int isdnloop_addcard(char *);
28
29 /*
30  * Free queue completely.
31  *
32  * Parameter:
33  *   card    = pointer to card struct
34  *   channel = channel number
35  */
36 static void
37 isdnloop_free_queue(isdnloop_card * card, int channel)
38 {
39         struct sk_buff_head *queue = &card->bqueue[channel];
40
41         skb_queue_purge(queue);
42         card->sndcount[channel] = 0;
43 }
44
45 /*
46  * Send B-Channel data to another virtual card.
47  * This routine is called via timer-callback from isdnloop_pollbchan().
48  *
49  * Parameter:
50  *   card = pointer to card struct.
51  *   ch   = channel number (0-based)
52  */
53 static void
54 isdnloop_bchan_send(isdnloop_card * card, int ch)
55 {
56         isdnloop_card *rcard = card->rcard[ch];
57         int rch = card->rch[ch], len, ack;
58         struct sk_buff *skb;
59         isdn_ctrl cmd;
60
61         while (card->sndcount[ch]) {
62                 if ((skb = skb_dequeue(&card->bqueue[ch]))) {
63                         len = skb->len;
64                         card->sndcount[ch] -= len;
65                         ack = *(skb->head); /* used as scratch area */
66                         cmd.driver = card->myid;
67                         cmd.arg = ch;
68                         if (rcard){
69                                 rcard->interface.rcvcallb_skb(rcard->myid, rch, skb);
70                         } else {
71                                 printk(KERN_WARNING "isdnloop: no rcard, skb dropped\n");
72                                 dev_kfree_skb(skb);
73
74                         };
75                         cmd.command = ISDN_STAT_BSENT;
76                         cmd.parm.length = len;
77                         card->interface.statcallb(&cmd);
78                 } else
79                         card->sndcount[ch] = 0;
80         }
81 }
82
83 /*
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.
87  *
88  * Parameter:
89  *   data = pointer to card struct, set by kernel timer.data
90  */
91 static void
92 isdnloop_pollbchan(unsigned long data)
93 {
94         isdnloop_card *card = (isdnloop_card *) data;
95         unsigned long flags;
96
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                 save_flags(flags);
104                 cli();
105                 card->rb_timer.expires = jiffies + ISDNLOOP_TIMER_BCREAD;
106                 add_timer(&card->rb_timer);
107                 card->flags |= ISDNLOOP_FLAGS_RBTIMER;
108                 restore_flags(flags);
109         } else
110                 card->flags &= ~ISDNLOOP_FLAGS_RBTIMER;
111 }
112
113 /*
114  * Parse ICN-type setup string and fill fields of setup-struct
115  * with parsed data.
116  *
117  * Parameter:
118  *   setup = setup string, format: [caller-id],si1,si2,[called-id]
119  *   cmd   = pointer to struct to be filled.
120  */
121 static void
122 isdnloop_parse_setup(char *setup, isdn_ctrl * cmd)
123 {
124         char *t = setup;
125         char *s = strchr(t, ',');
126
127         *s++ = '\0';
128         strlcpy(cmd->parm.setup.phone, t, sizeof(cmd->parm.setup.phone));
129         s = strchr(t = s, ',');
130         *s++ = '\0';
131         if (!strlen(t))
132                 cmd->parm.setup.si1 = 0;
133         else
134                 cmd->parm.setup.si1 = simple_strtoul(t, NULL, 10);
135         s = strchr(t = s, ',');
136         *s++ = '\0';
137         if (!strlen(t))
138                 cmd->parm.setup.si2 = 0;
139         else
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;
145 }
146
147 typedef struct isdnloop_stat {
148         char *statstr;
149         int command;
150         int action;
151 } isdnloop_stat;
152 /* *INDENT-OFF* */
153 static isdnloop_stat isdnloop_stat_table[] =
154 {
155         {"BCON_",          ISDN_STAT_BCONN, 1}, /* B-Channel connected        */
156         {"BDIS_",          ISDN_STAT_BHUP,  2}, /* B-Channel disconnected     */
157         {"DCON_",          ISDN_STAT_DCONN, 0}, /* D-Channel connected        */
158         {"DDIS_",          ISDN_STAT_DHUP,  0}, /* D-Channel disconnected     */
159         {"DCAL_I",         ISDN_STAT_ICALL, 3}, /* Incoming call dialup-line  */
160         {"DSCA_I",         ISDN_STAT_ICALL, 3}, /* Incoming call 1TR6-SPV     */
161         {"FCALL",          ISDN_STAT_ICALL, 4}, /* Leased line connection up  */
162         {"CIF",            ISDN_STAT_CINF,  5}, /* Charge-info, 1TR6-type     */
163         {"AOC",            ISDN_STAT_CINF,  6}, /* Charge-info, DSS1-type     */
164         {"CAU",            ISDN_STAT_CAUSE, 7}, /* Cause code                 */
165         {"TEI OK",         ISDN_STAT_RUN,   0}, /* Card connected to wallplug */
166         {"E_L1: ACT FAIL", ISDN_STAT_BHUP,  8}, /* Layer-1 activation failed  */
167         {"E_L2: DATA LIN", ISDN_STAT_BHUP,  8}, /* Layer-2 data link lost     */
168         {"E_L1: ACTIVATION FAILED",
169                            ISDN_STAT_BHUP,  8},         /* Layer-1 activation failed  */
170         {NULL, 0, -1}
171 };
172 /* *INDENT-ON* */
173
174
175 /*
176  * Parse Status message-strings from virtual card.
177  * Depending on status, call statcallb for sending messages to upper
178  * levels. Also set/reset B-Channel active-flags.
179  *
180  * Parameter:
181  *   status  = status string to parse.
182  *   channel = channel where message comes from.
183  *   card    = card where message comes from.
184  */
185 static void
186 isdnloop_parse_status(u_char * status, int channel, isdnloop_card * card)
187 {
188         isdnloop_stat *s = isdnloop_stat_table;
189         int action = -1;
190         isdn_ctrl cmd;
191
192         while (s->statstr) {
193                 if (!strncmp(status, s->statstr, strlen(s->statstr))) {
194                         cmd.command = s->command;
195                         action = s->action;
196                         break;
197                 }
198                 s++;
199         }
200         if (action == -1)
201                 return;
202         cmd.driver = card->myid;
203         cmd.arg = channel;
204         switch (action) {
205                 case 1:
206                         /* BCON_x */
207                         card->flags |= (channel) ?
208                             ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE;
209                         break;
210                 case 2:
211                         /* BDIS_x */
212                         card->flags &= ~((channel) ?
213                                          ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE);
214                         isdnloop_free_queue(card, channel);
215                         break;
216                 case 3:
217                         /* DCAL_I and DSCA_I */
218                         isdnloop_parse_setup(status + 6, &cmd);
219                         break;
220                 case 4:
221                         /* FCALL */
222                         sprintf(cmd.parm.setup.phone, "LEASED%d", card->myid);
223                         sprintf(cmd.parm.setup.eazmsn, "%d", channel + 1);
224                         cmd.parm.setup.si1 = 7;
225                         cmd.parm.setup.si2 = 0;
226                         cmd.parm.setup.plan = 0;
227                         cmd.parm.setup.screen = 0;
228                         break;
229                 case 5:
230                         /* CIF */
231                         strlcpy(cmd.parm.num, status + 3, sizeof(cmd.parm.num));
232                         break;
233                 case 6:
234                         /* AOC */
235                         snprintf(cmd.parm.num, sizeof(cmd.parm.num), "%d",
236                              (int) simple_strtoul(status + 7, NULL, 16));
237                         break;
238                 case 7:
239                         /* CAU */
240                         status += 3;
241                         if (strlen(status) == 4)
242                                 snprintf(cmd.parm.num, sizeof(cmd.parm.num), "%s%c%c",
243                                      status + 2, *status, *(status + 1));
244                         else
245                                 strlcpy(cmd.parm.num, status + 1, sizeof(cmd.parm.num));
246                         break;
247                 case 8:
248                         /* Misc Errors on L1 and L2 */
249                         card->flags &= ~ISDNLOOP_FLAGS_B1ACTIVE;
250                         isdnloop_free_queue(card, 0);
251                         cmd.arg = 0;
252                         cmd.driver = card->myid;
253                         card->interface.statcallb(&cmd);
254                         cmd.command = ISDN_STAT_DHUP;
255                         cmd.arg = 0;
256                         cmd.driver = card->myid;
257                         card->interface.statcallb(&cmd);
258                         cmd.command = ISDN_STAT_BHUP;
259                         card->flags &= ~ISDNLOOP_FLAGS_B2ACTIVE;
260                         isdnloop_free_queue(card, 1);
261                         cmd.arg = 1;
262                         cmd.driver = card->myid;
263                         card->interface.statcallb(&cmd);
264                         cmd.command = ISDN_STAT_DHUP;
265                         cmd.arg = 1;
266                         cmd.driver = card->myid;
267                         break;
268         }
269         card->interface.statcallb(&cmd);
270 }
271
272 /*
273  * Store a cwcharacter into ringbuffer for reading from /dev/isdnctrl
274  *
275  * Parameter:
276  *   card = pointer to card struct.
277  *   c    = char to store.
278  */
279 static void
280 isdnloop_putmsg(isdnloop_card * card, unsigned char c)
281 {
282         ulong flags;
283
284         save_flags(flags);
285         cli();
286         *card->msg_buf_write++ = (c == 0xff) ? '\n' : c;
287         if (card->msg_buf_write == card->msg_buf_read) {
288                 if (++card->msg_buf_read > card->msg_buf_end)
289                         card->msg_buf_read = card->msg_buf;
290         }
291         if (card->msg_buf_write > card->msg_buf_end)
292                 card->msg_buf_write = card->msg_buf;
293         restore_flags(flags);
294 }
295
296 /*
297  * Poll a virtual cards message queue.
298  * If there are new status-replies from the card, copy them to
299  * ringbuffer for reading on /dev/isdnctrl and call
300  * isdnloop_parse_status() for processing them. Watch for special
301  * Firmware bootmessage and parse it, to get the D-Channel protocol.
302  * If there are B-Channels open, initiate a timer-callback to
303  * isdnloop_pollbchan().
304  * This routine is called periodically via timer interrupt.
305  *
306  * Parameter:
307  *   data = pointer to card struct
308  */
309 static void
310 isdnloop_polldchan(unsigned long data)
311 {
312         isdnloop_card *card = (isdnloop_card *) data;
313         struct sk_buff *skb;
314         int avail;
315         int left;
316         u_char c;
317         int ch;
318         unsigned long flags;
319         u_char *p;
320         isdn_ctrl cmd;
321
322         if ((skb = skb_dequeue(&card->dqueue)))
323                 avail = skb->len;
324         else
325                 avail = 0;
326         for (left = avail; left > 0; left--) {
327                 c = *skb->data;
328                 skb_pull(skb, 1);
329                 isdnloop_putmsg(card, c);
330                 card->imsg[card->iptr] = c;
331                 if (card->iptr < 59)
332                         card->iptr++;
333                 if (!skb->len) {
334                         avail++;
335                         isdnloop_putmsg(card, '\n');
336                         card->imsg[card->iptr] = 0;
337                         card->iptr = 0;
338                         if (card->imsg[0] == '0' && card->imsg[1] >= '0' &&
339                           card->imsg[1] <= '2' && card->imsg[2] == ';') {
340                                 ch = (card->imsg[1] - '0') - 1;
341                                 p = &card->imsg[3];
342                                 isdnloop_parse_status(p, ch, card);
343                         } else {
344                                 p = card->imsg;
345                                 if (!strncmp(p, "DRV1.", 5)) {
346                                         printk(KERN_INFO "isdnloop: (%s) %s\n", CID, p);
347                                         if (!strncmp(p + 7, "TC", 2)) {
348                                                 card->ptype = ISDN_PTYPE_1TR6;
349                                                 card->interface.features |= ISDN_FEATURE_P_1TR6;
350                                                 printk(KERN_INFO
351                                                        "isdnloop: (%s) 1TR6-Protocol loaded and running\n", CID);
352                                         }
353                                         if (!strncmp(p + 7, "EC", 2)) {
354                                                 card->ptype = ISDN_PTYPE_EURO;
355                                                 card->interface.features |= ISDN_FEATURE_P_EURO;
356                                                 printk(KERN_INFO
357                                                        "isdnloop: (%s) Euro-Protocol loaded and running\n", CID);
358                                         }
359                                         continue;
360
361                                 }
362                         }
363                 }
364         }
365         if (avail) {
366                 cmd.command = ISDN_STAT_STAVAIL;
367                 cmd.driver = card->myid;
368                 cmd.arg = avail;
369                 card->interface.statcallb(&cmd);
370         }
371         if (card->flags & (ISDNLOOP_FLAGS_B1ACTIVE | ISDNLOOP_FLAGS_B2ACTIVE))
372                 if (!(card->flags & ISDNLOOP_FLAGS_RBTIMER)) {
373                         /* schedule b-channel polling */
374                         card->flags |= ISDNLOOP_FLAGS_RBTIMER;
375                         save_flags(flags);
376                         cli();
377                         del_timer(&card->rb_timer);
378                         card->rb_timer.function = isdnloop_pollbchan;
379                         card->rb_timer.data = (unsigned long) card;
380                         card->rb_timer.expires = jiffies + ISDNLOOP_TIMER_BCREAD;
381                         add_timer(&card->rb_timer);
382                         restore_flags(flags);
383                 }
384         /* schedule again */
385         save_flags(flags);
386         cli();
387         card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD;
388         add_timer(&card->st_timer);
389         restore_flags(flags);
390 }
391
392 /*
393  * Append a packet to the transmit buffer-queue.
394  *
395  * Parameter:
396  *   channel = Number of B-channel
397  *   skb     = packet to send.
398  *   card    = pointer to card-struct
399  * Return:
400  *   Number of bytes transferred, -E??? on error
401  */
402 static int
403 isdnloop_sendbuf(int channel, struct sk_buff *skb, isdnloop_card * card)
404 {
405         int len = skb->len;
406         unsigned long flags;
407         struct sk_buff *nskb;
408
409         if (len > 4000) {
410                 printk(KERN_WARNING
411                        "isdnloop: Send packet too large\n");
412                 return -EINVAL;
413         }
414         if (len) {
415                 if (!(card->flags & (channel) ? ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE))
416                         return 0;
417                 if (card->sndcount[channel] > ISDNLOOP_MAX_SQUEUE)
418                         return 0;
419                 save_flags(flags);
420                 cli();
421                 nskb = dev_alloc_skb(skb->len);
422                 if (nskb) {
423                         memcpy(skb_put(nskb, len), skb->data, len);
424                         skb_queue_tail(&card->bqueue[channel], nskb);
425                         dev_kfree_skb(skb);
426                 } else
427                         len = 0;
428                 card->sndcount[channel] += len;
429                 restore_flags(flags);
430         }
431         return len;
432 }
433
434 /*
435  * Read the messages from the card's ringbuffer
436  *
437  * Parameter:
438  *   buf  = pointer to buffer.
439  *   len  = number of bytes to read.
440  *   user = flag, 1: called from userlevel 0: called from kernel.
441  *   card = pointer to card struct.
442  * Return:
443  *   number of bytes actually transferred.
444  */
445 static int
446 isdnloop_readstatus(u_char * buf, int len, int user, isdnloop_card * card)
447 {
448         int count;
449         u_char *p;
450
451         for (p = buf, count = 0; count < len; p++, count++) {
452                 if (card->msg_buf_read == card->msg_buf_write)
453                         return count;
454                 if (user)
455                         put_user(*card->msg_buf_read++, p);
456                 else
457                         *p = *card->msg_buf_read++;
458                 if (card->msg_buf_read > card->msg_buf_end)
459                         card->msg_buf_read = card->msg_buf;
460         }
461         return count;
462 }
463
464 /*
465  * Simulate a card's response by appending it to the cards
466  * message queue.
467  *
468  * Parameter:
469  *   card = pointer to card struct.
470  *   s    = pointer to message-string.
471  *   ch   = channel: 0 = generic messages, 1 and 2 = D-channel messages.
472  * Return:
473  *   0 on success, 1 on memory squeeze.
474  */
475 static int
476 isdnloop_fake(isdnloop_card * card, char *s, int ch)
477 {
478         struct sk_buff *skb;
479         int len = strlen(s) + ((ch >= 0) ? 3 : 0);
480
481         if (!(skb = dev_alloc_skb(len))) {
482                 printk(KERN_WARNING "isdnloop: Out of memory in isdnloop_fake\n");
483                 return 1;
484         }
485         if (ch >= 0)
486                 sprintf(skb_put(skb, 3), "%02d;", ch);
487         memcpy(skb_put(skb, strlen(s)), s, strlen(s));
488         skb_queue_tail(&card->dqueue, skb);
489         return 0;
490 }
491 /* *INDENT-OFF* */
492 static isdnloop_stat isdnloop_cmd_table[] =
493 {
494         {"BCON_R",         0,  1},      /* B-Channel connect        */
495         {"BCON_I",         0, 17},      /* B-Channel connect ind    */
496         {"BDIS_R",         0,  2},      /* B-Channel disconnect     */
497         {"DDIS_R",         0,  3},      /* D-Channel disconnect     */
498         {"DCON_R",         0, 16},      /* D-Channel connect        */
499         {"DSCA_R",         0,  4},      /* Dial 1TR6-SPV     */
500         {"DCAL_R",         0,  5},      /* Dial */
501         {"EAZC",           0,  6},      /* Clear EAZ listener */
502         {"EAZ",            0,  7},      /* Set EAZ listener */
503         {"SEEAZ",          0,  8},      /* Get EAZ listener */
504         {"MSN",            0,  9},      /* Set/Clear MSN listener */
505         {"MSALL",          0, 10},      /* Set multi MSN listeners */
506         {"SETSIL",         0, 11},      /* Set SI list     */
507         {"SEESIL",         0, 12},      /* Get SI list     */
508         {"SILC",           0, 13},      /* Clear SI list     */
509         {"LOCK",           0, -1},      /* LOCK channel     */
510         {"UNLOCK",         0, -1},      /* UNLOCK channel     */
511         {"FV2ON",          1, 14},      /* Leased mode on               */
512         {"FV2OFF",         1, 15},      /* Leased mode off              */
513         {NULL, 0, -1}
514 };
515 /* *INDENT-ON* */
516
517
518 /*
519  * Simulate an error-response from a card.
520  *
521  * Parameter:
522  *   card = pointer to card struct.
523  */
524 static void
525 isdnloop_fake_err(isdnloop_card * card)
526 {
527         char buf[60];
528
529         sprintf(buf, "E%s", card->omsg);
530         isdnloop_fake(card, buf, -1);
531         isdnloop_fake(card, "NAK", -1);
532 }
533
534 static u_char ctable_eu[] =
535 {0x00, 0x11, 0x01, 0x12};
536 static u_char ctable_1t[] =
537 {0x00, 0x3b, 0x01, 0x3a};
538
539 /*
540  * Assemble a simplified cause message depending on the
541  * D-channel protocol used.
542  *
543  * Parameter:
544  *   card = pointer to card struct.
545  *   loc  = location: 0 = local, 1 = remote.
546  *   cau  = cause: 1 = busy, 2 = nonexistent callerid, 3 = no user responding.
547  * Return:
548  *   Pointer to buffer containing the assembled message.
549  */
550 static char *
551 isdnloop_unicause(isdnloop_card * card, int loc, int cau)
552 {
553         static char buf[6];
554
555         switch (card->ptype) {
556                 case ISDN_PTYPE_EURO:
557                         sprintf(buf, "E%02X%02X", (loc) ? 4 : 2, ctable_eu[cau]);
558                         break;
559                 case ISDN_PTYPE_1TR6:
560                         sprintf(buf, "%02X44", ctable_1t[cau]);
561                         break;
562                 default:
563                         return ("0000");
564         }
565         return (buf);
566 }
567
568 /*
569  * Release a virtual connection. Called from timer interrupt, when
570  * called party did not respond.
571  *
572  * Parameter:
573  *   card = pointer to card struct.
574  *   ch   = channel (0-based)
575  */
576 static void
577 isdnloop_atimeout(isdnloop_card * card, int ch)
578 {
579         unsigned long flags;
580         char buf[60];
581
582         save_flags(flags);
583         cli();
584         if (card->rcard) {
585                 isdnloop_fake(card->rcard[ch], "DDIS_I", card->rch[ch] + 1);
586                 card->rcard[ch]->rcard[card->rch[ch]] = NULL;
587                 card->rcard[ch] = NULL;
588         }
589         isdnloop_fake(card, "DDIS_I", ch + 1);
590         /* No user responding */
591         sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 3));
592         isdnloop_fake(card, buf, ch + 1);
593         restore_flags(flags);
594 }
595
596 /*
597  * Wrapper for isdnloop_atimeout().
598  */
599 static void
600 isdnloop_atimeout0(unsigned long data)
601 {
602         isdnloop_card *card = (isdnloop_card *) data;
603         isdnloop_atimeout(card, 0);
604 }
605
606 /*
607  * Wrapper for isdnloop_atimeout().
608  */
609 static void
610 isdnloop_atimeout1(unsigned long data)
611 {
612         isdnloop_card *card = (isdnloop_card *) data;
613         isdnloop_atimeout(card, 1);
614 }
615
616 /*
617  * Install a watchdog for a user, not responding.
618  *
619  * Parameter:
620  *   card = pointer to card struct.
621  *   ch   = channel to watch for.
622  */
623 static void
624 isdnloop_start_ctimer(isdnloop_card * card, int ch)
625 {
626         unsigned long flags;
627
628         save_flags(flags);
629         cli();
630         init_timer(&card->c_timer[ch]);
631         card->c_timer[ch].expires = jiffies + ISDNLOOP_TIMER_ALERTWAIT;
632         if (ch)
633                 card->c_timer[ch].function = isdnloop_atimeout1;
634         else
635                 card->c_timer[ch].function = isdnloop_atimeout0;
636         card->c_timer[ch].data = (unsigned long) card;
637         add_timer(&card->c_timer[ch]);
638         restore_flags(flags);
639 }
640
641 /*
642  * Kill a pending channel watchdog.
643  *
644  * Parameter:
645  *   card = pointer to card struct.
646  *   ch   = channel (0-based).
647  */
648 static void
649 isdnloop_kill_ctimer(isdnloop_card * card, int ch)
650 {
651         unsigned long flags;
652
653         save_flags(flags);
654         cli();
655         del_timer(&card->c_timer[ch]);
656         restore_flags(flags);
657 }
658
659 static u_char si2bit[] =
660 {0, 1, 0, 0, 0, 2, 0, 4, 0, 0};
661 static u_char bit2si[] =
662 {1, 5, 7};
663
664 /*
665  * Try finding a listener for an outgoing call.
666  *
667  * Parameter:
668  *   card = pointer to calling card.
669  *   p    = pointer to ICN-type setup-string.
670  *   lch  = channel of calling card.
671  *   cmd  = pointer to struct to be filled when parsing setup.
672  * Return:
673  *   0 = found match, alerting should happen.
674  *   1 = found matching number but it is busy.
675  *   2 = no matching listener.
676  *   3 = found matching number but SI does not match.
677  */
678 static int
679 isdnloop_try_call(isdnloop_card * card, char *p, int lch, isdn_ctrl * cmd)
680 {
681         isdnloop_card *cc = cards;
682         unsigned long flags;
683         int ch;
684         int num_match;
685         int i;
686         char *e;
687         char nbuf[32];
688
689         isdnloop_parse_setup(p, cmd);
690         while (cc) {
691                 for (ch = 0; ch < 2; ch++) {
692                         /* Exclude ourself */
693                         if ((cc == card) && (ch == lch))
694                                 continue;
695                         num_match = 0;
696                         switch (cc->ptype) {
697                                 case ISDN_PTYPE_EURO:
698                                         for (i = 0; i < 3; i++)
699                                                 if (!(strcmp(cc->s0num[i], cmd->parm.setup.phone)))
700                                                         num_match = 1;
701                                         break;
702                                 case ISDN_PTYPE_1TR6:
703                                         e = cc->eazlist[ch];
704                                         while (*e) {
705                                                 sprintf(nbuf, "%s%c", cc->s0num[0], *e);
706                                                 if (!(strcmp(nbuf, cmd->parm.setup.phone)))
707                                                         num_match = 1;
708                                                 e++;
709                                         }
710                         }
711                         if (num_match) {
712                                 save_flags(flags);
713                                 cli();
714                                 /* channel idle? */
715                                 if (!(cc->rcard[ch])) {
716                                         /* Check SI */
717                                         if (!(si2bit[cmd->parm.setup.si1] & cc->sil[ch])) {
718                                                 restore_flags(flags);
719                                                 return 3;
720                                         }
721                                         /* ch is idle, si and number matches */
722                                         cc->rcard[ch] = card;
723                                         cc->rch[ch] = lch;
724                                         card->rcard[lch] = cc;
725                                         card->rch[lch] = ch;
726                                         restore_flags(flags);
727                                         return 0;
728                                 } else {
729                                         restore_flags(flags);
730                                         /* num matches, but busy */
731                                         if (ch == 1)
732                                                 return 1;
733                                 }
734                         }
735                 }
736                 cc = cc->next;
737         }
738         return 2;
739 }
740
741 /*
742  * Depending on D-channel protocol and caller/called, modify
743  * phone number.
744  *
745  * Parameter:
746  *   card   = pointer to card struct.
747  *   phone  = pointer phone number.
748  *   caller = flag: 1 = caller, 0 = called.
749  * Return:
750  *   pointer to new phone number.
751  */
752 static char *
753 isdnloop_vstphone(isdnloop_card * card, char *phone, int caller)
754 {
755         int i;
756         static char nphone[30];
757
758         if (!card) {
759                 printk("BUG!!!\n");
760                 return "";
761         }
762         switch (card->ptype) {
763                 case ISDN_PTYPE_EURO:
764                         if (caller) {
765                                 for (i = 0; i < 2; i++)
766                                         if (!(strcmp(card->s0num[i], phone)))
767                                                 return (phone);
768                                 return (card->s0num[0]);
769                         }
770                         return (phone);
771                         break;
772                 case ISDN_PTYPE_1TR6:
773                         if (caller) {
774                                 sprintf(nphone, "%s%c", card->s0num[0], phone[0]);
775                                 return (nphone);
776                         } else
777                                 return (&phone[strlen(phone) - 1]);
778                         break;
779         }
780         return "";
781 }
782
783 /*
784  * Parse an ICN-type command string sent to the 'card'.
785  * Perform misc. actions depending on the command.
786  *
787  * Parameter:
788  *   card = pointer to card struct.
789  */
790 static void
791 isdnloop_parse_cmd(isdnloop_card * card)
792 {
793         char *p = card->omsg;
794         isdn_ctrl cmd;
795         char buf[60];
796         isdnloop_stat *s = isdnloop_cmd_table;
797         int action = -1;
798         int i;
799         int ch;
800
801         if ((card->omsg[0] != '0') && (card->omsg[2] != ';')) {
802                 isdnloop_fake_err(card);
803                 return;
804         }
805         ch = card->omsg[1] - '0';
806         if ((ch < 0) || (ch > 2)) {
807                 isdnloop_fake_err(card);
808                 return;
809         }
810         p += 3;
811         while (s->statstr) {
812                 if (!strncmp(p, s->statstr, strlen(s->statstr))) {
813                         action = s->action;
814                         if (s->command && (ch != 0)) {
815                                 isdnloop_fake_err(card);
816                                 return;
817                         }
818                         break;
819                 }
820                 s++;
821         }
822         if (action == -1)
823                 return;
824         switch (action) {
825                 case 1:
826                         /* 0x;BCON_R */
827                         if (card->rcard[ch - 1]) {
828                                 isdnloop_fake(card->rcard[ch - 1], "BCON_I",
829                                               card->rch[ch - 1] + 1);
830                                 isdnloop_fake(card, "BCON_C", ch);
831                         }
832                         break;
833                 case 17:
834                         /* 0x;BCON_I */
835                         if (card->rcard[ch - 1]) {
836                                 isdnloop_fake(card->rcard[ch - 1], "BCON_C",
837                                               card->rch[ch - 1] + 1);
838                         }
839                         break;
840                 case 2:
841                         /* 0x;BDIS_R */
842                         isdnloop_fake(card, "BDIS_C", ch);
843                         if (card->rcard[ch - 1]) {
844                                 isdnloop_fake(card->rcard[ch - 1], "BDIS_I",
845                                               card->rch[ch - 1] + 1);
846                         }
847                         break;
848                 case 16:
849                         /* 0x;DCON_R */
850                         isdnloop_kill_ctimer(card, ch - 1);
851                         if (card->rcard[ch - 1]) {
852                                 isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
853                                 isdnloop_fake(card->rcard[ch - 1], "DCON_C",
854                                               card->rch[ch - 1] + 1);
855                                 isdnloop_fake(card, "DCON_C", ch);
856                         }
857                         break;
858                 case 3:
859                         /* 0x;DDIS_R */
860                         isdnloop_kill_ctimer(card, ch - 1);
861                         if (card->rcard[ch - 1]) {
862                                 isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
863                                 isdnloop_fake(card->rcard[ch - 1], "DDIS_I",
864                                               card->rch[ch - 1] + 1);
865                                 card->rcard[ch - 1] = NULL;
866                         }
867                         isdnloop_fake(card, "DDIS_C", ch);
868                         break;
869                 case 4:
870                         /* 0x;DSCA_Rdd,yy,zz,oo */
871                         if (card->ptype != ISDN_PTYPE_1TR6) {
872                                 isdnloop_fake_err(card);
873                                 return;
874                         }
875                         /* Fall through */
876                 case 5:
877                         /* 0x;DCAL_Rdd,yy,zz,oo */
878                         p += 6;
879                         switch (isdnloop_try_call(card, p, ch - 1, &cmd)) {
880                                 case 0:
881                                         /* Alerting */
882                                         sprintf(buf, "D%s_I%s,%02d,%02d,%s",
883                                            (action == 4) ? "SCA" : "CAL",
884                                                 isdnloop_vstphone(card, cmd.parm.setup.eazmsn, 1),
885                                                 cmd.parm.setup.si1,
886                                                 cmd.parm.setup.si2,
887                                         isdnloop_vstphone(card->rcard[ch - 1],
888                                                cmd.parm.setup.phone, 0));
889                                         isdnloop_fake(card->rcard[ch - 1], buf, card->rch[ch - 1] + 1);
890                                         /* Fall through */
891                                 case 3:
892                                         /* si1 does not match, don't alert but start timer */
893                                         isdnloop_start_ctimer(card, ch - 1);
894                                         break;
895                                 case 1:
896                                         /* Remote busy */
897                                         isdnloop_fake(card, "DDIS_I", ch);
898                                         sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 1));
899                                         isdnloop_fake(card, buf, ch);
900                                         break;
901                                 case 2:
902                                         /* No such user */
903                                         isdnloop_fake(card, "DDIS_I", ch);
904                                         sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 2));
905                                         isdnloop_fake(card, buf, ch);
906                                         break;
907                         }
908                         break;
909                 case 6:
910                         /* 0x;EAZC */
911                         card->eazlist[ch - 1][0] = '\0';
912                         break;
913                 case 7:
914                         /* 0x;EAZ */
915                         p += 3;
916                         strcpy(card->eazlist[ch - 1], p);
917                         break;
918                 case 8:
919                         /* 0x;SEEAZ */
920                         sprintf(buf, "EAZ-LIST: %s", card->eazlist[ch - 1]);
921                         isdnloop_fake(card, buf, ch + 1);
922                         break;
923                 case 9:
924                         /* 0x;MSN */
925                         break;
926                 case 10:
927                         /* 0x;MSNALL */
928                         break;
929                 case 11:
930                         /* 0x;SETSIL */
931                         p += 6;
932                         i = 0;
933                         while (strchr("0157", *p)) {
934                                 if (i)
935                                         card->sil[ch - 1] |= si2bit[*p - '0'];
936                                 i = (*p++ == '0');
937                         }
938                         if (*p)
939                                 isdnloop_fake_err(card);
940                         break;
941                 case 12:
942                         /* 0x;SEESIL */
943                         sprintf(buf, "SIN-LIST: ");
944                         p = buf + 10;
945                         for (i = 0; i < 3; i++)
946                                 if (card->sil[ch - 1] & (1 << i))
947                                         p += sprintf(p, "%02d", bit2si[i]);
948                         isdnloop_fake(card, buf, ch + 1);
949                         break;
950                 case 13:
951                         /* 0x;SILC */
952                         card->sil[ch - 1] = 0;
953                         break;
954                 case 14:
955                         /* 00;FV2ON */
956                         break;
957                 case 15:
958                         /* 00;FV2OFF */
959                         break;
960         }
961 }
962
963 /*
964  * Put command-strings into the of the 'card'. In reality, execute them
965  * right in place by calling isdnloop_parse_cmd(). Also copy every
966  * command to the read message ringbuffer, preceeding it with a '>'.
967  * These mesagges can be read at /dev/isdnctrl.
968  *
969  * Parameter:
970  *   buf  = pointer to command buffer.
971  *   len  = length of buffer data.
972  *   user = flag: 1 = called form userlevel, 0 called from kernel.
973  *   card = pointer to card struct.
974  * Return:
975  *   number of bytes transferred (currently always equals len).
976  */
977 static int
978 isdnloop_writecmd(const u_char * buf, int len, int user, isdnloop_card * card)
979 {
980         int xcount = 0;
981         int ocount = 1;
982         isdn_ctrl cmd;
983
984         while (len) {
985                 int count = len;
986                 u_char *p;
987                 u_char msg[0x100];
988
989                 if (count > 255)
990                         count = 255;
991                 if (user) {
992                         if (copy_from_user(msg, buf, count))
993                                 return -EFAULT;
994                 } else
995                         memcpy(msg, buf, count);
996                 isdnloop_putmsg(card, '>');
997                 for (p = msg; count > 0; count--, p++) {
998                         len--;
999                         xcount++;
1000                         isdnloop_putmsg(card, *p);
1001                         card->omsg[card->optr] = *p;
1002                         if (*p == '\n') {
1003                                 card->omsg[card->optr] = '\0';
1004                                 card->optr = 0;
1005                                 isdnloop_parse_cmd(card);
1006                                 if (len) {
1007                                         isdnloop_putmsg(card, '>');
1008                                         ocount++;
1009                                 }
1010                         } else {
1011                                 if (card->optr < 59)
1012                                         card->optr++;
1013                         }
1014                         ocount++;
1015                 }
1016         }
1017         cmd.command = ISDN_STAT_STAVAIL;
1018         cmd.driver = card->myid;
1019         cmd.arg = ocount;
1020         card->interface.statcallb(&cmd);
1021         return xcount;
1022 }
1023
1024 /*
1025  * Delete card's pending timers, send STOP to linklevel
1026  */
1027 static void
1028 isdnloop_stopcard(isdnloop_card * card)
1029 {
1030         unsigned long flags;
1031         isdn_ctrl cmd;
1032
1033         save_flags(flags);
1034         cli();
1035         if (card->flags & ISDNLOOP_FLAGS_RUNNING) {
1036                 card->flags &= ~ISDNLOOP_FLAGS_RUNNING;
1037                 del_timer(&card->st_timer);
1038                 del_timer(&card->rb_timer);
1039                 del_timer(&card->c_timer[0]);
1040                 del_timer(&card->c_timer[1]);
1041                 cmd.command = ISDN_STAT_STOP;
1042                 cmd.driver = card->myid;
1043                 card->interface.statcallb(&cmd);
1044         }
1045         restore_flags(flags);
1046 }
1047
1048 /*
1049  * Stop all cards before unload.
1050  */
1051 static void
1052 isdnloop_stopallcards(void)
1053 {
1054         isdnloop_card *p = cards;
1055
1056         while (p) {
1057                 isdnloop_stopcard(p);
1058                 p = p->next;
1059         }
1060 }
1061
1062 /*
1063  * Start a 'card'. Simulate card's boot message and set the phone
1064  * number(s) of the virtual 'S0-Interface'. Install D-channel
1065  * poll timer.
1066  *
1067  * Parameter:
1068  *   card  = pointer to card struct.
1069  *   sdefp = pointer to struct holding ioctl parameters.
1070  * Return:
1071  *   0 on success, -E??? otherwise.
1072  */
1073 static int
1074 isdnloop_start(isdnloop_card * card, isdnloop_sdef * sdefp)
1075 {
1076         unsigned long flags;
1077         isdnloop_sdef sdef;
1078         int i;
1079
1080         if (card->flags & ISDNLOOP_FLAGS_RUNNING)
1081                 return -EBUSY;
1082         if (copy_from_user((char *) &sdef, (char *) sdefp, sizeof(sdef)))
1083                 return -EFAULT;
1084         save_flags(flags);
1085         cli();
1086         switch (sdef.ptype) {
1087                 case ISDN_PTYPE_EURO:
1088                         if (isdnloop_fake(card, "DRV1.23EC-Q.931-CAPI-CNS-BASIS-20.02.96",
1089                                           -1)) {
1090                                 restore_flags(flags);
1091                                 return -ENOMEM;
1092                         }
1093                         card->sil[0] = card->sil[1] = 4;
1094                         if (isdnloop_fake(card, "TEI OK", 0)) {
1095                                 restore_flags(flags);
1096                                 return -ENOMEM;
1097                         }
1098                         for (i = 0; i < 3; i++)
1099                                 strcpy(card->s0num[i], sdef.num[i]);
1100                         break;
1101                 case ISDN_PTYPE_1TR6:
1102                         if (isdnloop_fake(card, "DRV1.04TC-1TR6-CAPI-CNS-BASIS-29.11.95",
1103                                           -1)) {
1104                                 restore_flags(flags);
1105                                 return -ENOMEM;
1106                         }
1107                         card->sil[0] = card->sil[1] = 4;
1108                         if (isdnloop_fake(card, "TEI OK", 0)) {
1109                                 restore_flags(flags);
1110                                 return -ENOMEM;
1111                         }
1112                         strcpy(card->s0num[0], sdef.num[0]);
1113                         card->s0num[1][0] = '\0';
1114                         card->s0num[2][0] = '\0';
1115                         break;
1116                 default:
1117                         restore_flags(flags);
1118                         printk(KERN_WARNING "isdnloop: Illegal D-channel protocol %d\n",
1119                                sdef.ptype);
1120                         return -EINVAL;
1121         }
1122         init_timer(&card->st_timer);
1123         card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD;
1124         card->st_timer.function = isdnloop_polldchan;
1125         card->st_timer.data = (unsigned long) card;
1126         add_timer(&card->st_timer);
1127         card->flags |= ISDNLOOP_FLAGS_RUNNING;
1128         restore_flags(flags);
1129         return 0;
1130 }
1131
1132 /*
1133  * Main handler for commands sent by linklevel.
1134  */
1135 static int
1136 isdnloop_command(isdn_ctrl * c, isdnloop_card * card)
1137 {
1138         ulong a;
1139         int i;
1140         char cbuf[60];
1141         isdn_ctrl cmd;
1142         isdnloop_cdef cdef;
1143
1144         switch (c->command) {
1145                 case ISDN_CMD_IOCTL:
1146                         memcpy(&a, c->parm.num, sizeof(ulong));
1147                         switch (c->arg) {
1148                                 case ISDNLOOP_IOCTL_DEBUGVAR:
1149                                         return (ulong) card;
1150                                 case ISDNLOOP_IOCTL_STARTUP:
1151                                         if ((i = verify_area(VERIFY_READ, (void *) a, sizeof(isdnloop_sdef))))
1152                                                 return i;
1153                                         return (isdnloop_start(card, (isdnloop_sdef *) a));
1154                                         break;
1155                                 case ISDNLOOP_IOCTL_ADDCARD:
1156                                         if (copy_from_user((char *)&cdef,
1157                                                            (char *)a,
1158                                                            sizeof(cdef)))
1159                                                 return -EFAULT;
1160                                         return (isdnloop_addcard(cdef.id1));
1161                                         break;
1162                                 case ISDNLOOP_IOCTL_LEASEDCFG:
1163                                         if (a) {
1164                                                 if (!card->leased) {
1165                                                         card->leased = 1;
1166                                                         while (card->ptype == ISDN_PTYPE_UNKNOWN) {
1167                                                                 schedule_timeout(10);
1168                                                         }
1169                                                         schedule_timeout(10);
1170                                                         sprintf(cbuf, "00;FV2ON\n01;EAZ1\n02;EAZ2\n");
1171                                                         i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1172                                                         printk(KERN_INFO
1173                                                                "isdnloop: (%s) Leased-line mode enabled\n",
1174                                                                CID);
1175                                                         cmd.command = ISDN_STAT_RUN;
1176                                                         cmd.driver = card->myid;
1177                                                         cmd.arg = 0;
1178                                                         card->interface.statcallb(&cmd);
1179                                                 }
1180                                         } else {
1181                                                 if (card->leased) {
1182                                                         card->leased = 0;
1183                                                         sprintf(cbuf, "00;FV2OFF\n");
1184                                                         i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1185                                                         printk(KERN_INFO
1186                                                                "isdnloop: (%s) Leased-line mode disabled\n",
1187                                                                CID);
1188                                                         cmd.command = ISDN_STAT_RUN;
1189                                                         cmd.driver = card->myid;
1190                                                         cmd.arg = 0;
1191                                                         card->interface.statcallb(&cmd);
1192                                                 }
1193                                         }
1194                                         return 0;
1195                                 default:
1196                                         return -EINVAL;
1197                         }
1198                         break;
1199                 case ISDN_CMD_DIAL:
1200                         if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1201                                 return -ENODEV;
1202                         if (card->leased)
1203                                 break;
1204                         if ((c->arg & 255) < ISDNLOOP_BCH) {
1205                                 char *p;
1206                                 char dial[50];
1207                                 char dcode[4];
1208
1209                                 a = c->arg;
1210                                 p = c->parm.setup.phone;
1211                                 if (*p == 's' || *p == 'S') {
1212                                         /* Dial for SPV */
1213                                         p++;
1214                                         strcpy(dcode, "SCA");
1215                                 } else
1216                                         /* Normal Dial */
1217                                         strcpy(dcode, "CAL");
1218                                 strcpy(dial, p);
1219                                 sprintf(cbuf, "%02d;D%s_R%s,%02d,%02d,%s\n", (int) (a + 1),
1220                                         dcode, dial, c->parm.setup.si1,
1221                                 c->parm.setup.si2, c->parm.setup.eazmsn);
1222                                 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1223                         }
1224                         break;
1225                 case ISDN_CMD_ACCEPTD:
1226                         if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1227                                 return -ENODEV;
1228                         if (c->arg < ISDNLOOP_BCH) {
1229                                 a = c->arg + 1;
1230                                 cbuf[0] = 0;
1231                                 switch (card->l2_proto[a - 1]) {
1232                                         case ISDN_PROTO_L2_X75I:
1233                                                 sprintf(cbuf, "%02d;BX75\n", (int) a);
1234                                                 break;
1235 #ifdef CONFIG_ISDN_X25
1236                                         case ISDN_PROTO_L2_X25DTE:
1237                                                 sprintf(cbuf, "%02d;BX2T\n", (int) a);
1238                                                 break;
1239                                         case ISDN_PROTO_L2_X25DCE:
1240                                                 sprintf(cbuf, "%02d;BX2C\n", (int) a);
1241                                                 break;
1242 #endif
1243                                         case ISDN_PROTO_L2_HDLC:
1244                                                 sprintf(cbuf, "%02d;BTRA\n", (int) a);
1245                                                 break;
1246                                 }
1247                                 if (strlen(cbuf))
1248                                         i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1249                                 sprintf(cbuf, "%02d;DCON_R\n", (int) a);
1250                                 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1251                         }
1252                         break;
1253                 case ISDN_CMD_ACCEPTB:
1254                         if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1255                                 return -ENODEV;
1256                         if (c->arg < ISDNLOOP_BCH) {
1257                                 a = c->arg + 1;
1258                                 switch (card->l2_proto[a - 1]) {
1259                                         case ISDN_PROTO_L2_X75I:
1260                                                 sprintf(cbuf, "%02d;BCON_R,BX75\n", (int) a);
1261                                                 break;
1262 #ifdef CONFIG_ISDN_X25
1263                                         case ISDN_PROTO_L2_X25DTE:
1264                                                 sprintf(cbuf, "%02d;BCON_R,BX2T\n", (int) a);
1265                                                 break;
1266                                         case ISDN_PROTO_L2_X25DCE:
1267                                                 sprintf(cbuf, "%02d;BCON_R,BX2C\n", (int) a);
1268                                                 break;
1269 #endif
1270                                         case ISDN_PROTO_L2_HDLC:
1271                                                 sprintf(cbuf, "%02d;BCON_R,BTRA\n", (int) a);
1272                                                 break;
1273                                         default:
1274                                                 sprintf(cbuf, "%02d;BCON_R\n", (int) a);
1275                                 }
1276                                 printk(KERN_DEBUG "isdnloop writecmd '%s'\n", cbuf);
1277                                 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1278                                 break;
1279                 case ISDN_CMD_HANGUP:
1280                                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1281                                         return -ENODEV;
1282                                 if (c->arg < ISDNLOOP_BCH) {
1283                                         a = c->arg + 1;
1284                                         sprintf(cbuf, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a, (int) a);
1285                                         i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1286                                 }
1287                                 break;
1288                 case ISDN_CMD_SETEAZ:
1289                                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1290                                         return -ENODEV;
1291                                 if (card->leased)
1292                                         break;
1293                                 if (c->arg < ISDNLOOP_BCH) {
1294                                         a = c->arg + 1;
1295                                         if (card->ptype == ISDN_PTYPE_EURO) {
1296                                                 sprintf(cbuf, "%02d;MS%s%s\n", (int) a,
1297                                                         c->parm.num[0] ? "N" : "ALL", c->parm.num);
1298                                         } else
1299                                                 sprintf(cbuf, "%02d;EAZ%s\n", (int) a,
1300                                                         c->parm.num[0] ? c->parm.num : (u_char *) "0123456789");
1301                                         i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1302                                 }
1303                                 break;
1304                 case ISDN_CMD_CLREAZ:
1305                                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1306                                         return -ENODEV;
1307                                 if (card->leased)
1308                                         break;
1309                                 if (c->arg < ISDNLOOP_BCH) {
1310                                         a = c->arg + 1;
1311                                         if (card->ptype == ISDN_PTYPE_EURO)
1312                                                 sprintf(cbuf, "%02d;MSNC\n", (int) a);
1313                                         else
1314                                                 sprintf(cbuf, "%02d;EAZC\n", (int) a);
1315                                         i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1316                                 }
1317                                 break;
1318                 case ISDN_CMD_SETL2:
1319                                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1320                                         return -ENODEV;
1321                                 if ((c->arg & 255) < ISDNLOOP_BCH) {
1322                                         a = c->arg;
1323                                         switch (a >> 8) {
1324                                                 case ISDN_PROTO_L2_X75I:
1325                                                         sprintf(cbuf, "%02d;BX75\n", (int) (a & 255) + 1);
1326                                                         break;
1327 #ifdef CONFIG_ISDN_X25
1328                                                 case ISDN_PROTO_L2_X25DTE:
1329                                                         sprintf(cbuf, "%02d;BX2T\n", (int) (a & 255) + 1);
1330                                                         break;
1331                                                 case ISDN_PROTO_L2_X25DCE:
1332                                                         sprintf(cbuf, "%02d;BX2C\n", (int) (a & 255) + 1);
1333                                                         break;
1334 #endif
1335                                                 case ISDN_PROTO_L2_HDLC:
1336                                                         sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
1337                                                         break;
1338                                                 case ISDN_PROTO_L2_TRANS:
1339                                                         sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
1340                                                         break;
1341                                                 default:
1342                                                         return -EINVAL;
1343                                         }
1344                                         i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1345                                         card->l2_proto[a & 255] = (a >> 8);
1346                                 }
1347                                 break;
1348                 case ISDN_CMD_SETL3:
1349                                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1350                                         return -ENODEV;
1351                                 return 0;
1352                 default:
1353                                 return -EINVAL;
1354                         }
1355         }
1356         return 0;
1357 }
1358
1359 /*
1360  * Find card with given driverId
1361  */
1362 static inline isdnloop_card *
1363 isdnloop_findcard(int driverid)
1364 {
1365         isdnloop_card *p = cards;
1366
1367         while (p) {
1368                 if (p->myid == driverid)
1369                         return p;
1370                 p = p->next;
1371         }
1372         return (isdnloop_card *) 0;
1373 }
1374
1375 /*
1376  * Wrapper functions for interface to linklevel
1377  */
1378 static int
1379 if_command(isdn_ctrl * c)
1380 {
1381         isdnloop_card *card = isdnloop_findcard(c->driver);
1382
1383         if (card)
1384                 return (isdnloop_command(c, card));
1385         printk(KERN_ERR
1386                "isdnloop: if_command called with invalid driverId!\n");
1387         return -ENODEV;
1388 }
1389
1390 static int
1391 if_writecmd(const u_char * buf, int len, int user, int id, int channel)
1392 {
1393         isdnloop_card *card = isdnloop_findcard(id);
1394
1395         if (card) {
1396                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1397                         return -ENODEV;
1398                 return (isdnloop_writecmd(buf, len, user, card));
1399         }
1400         printk(KERN_ERR
1401                "isdnloop: if_writecmd called with invalid driverId!\n");
1402         return -ENODEV;
1403 }
1404
1405 static int
1406 if_readstatus(u_char * buf, int len, int user, int id, int channel)
1407 {
1408         isdnloop_card *card = isdnloop_findcard(id);
1409
1410         if (card) {
1411                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1412                         return -ENODEV;
1413                 return (isdnloop_readstatus(buf, len, user, card));
1414         }
1415         printk(KERN_ERR
1416                "isdnloop: if_readstatus called with invalid driverId!\n");
1417         return -ENODEV;
1418 }
1419
1420 static int
1421 if_sendbuf(int id, int channel, int ack, struct sk_buff *skb)
1422 {
1423         isdnloop_card *card = isdnloop_findcard(id);
1424
1425         if (card) {
1426                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1427                         return -ENODEV;
1428                 /* ack request stored in skb scratch area */
1429                 *(skb->head) = ack;
1430                 return (isdnloop_sendbuf(channel, skb, card));
1431         }
1432         printk(KERN_ERR
1433                "isdnloop: if_sendbuf called with invalid driverId!\n");
1434         return -ENODEV;
1435 }
1436
1437 /*
1438  * Allocate a new card-struct, initialize it
1439  * link it into cards-list and register it at linklevel.
1440  */
1441 static isdnloop_card *
1442 isdnloop_initcard(char *id)
1443 {
1444         isdnloop_card *card;
1445         int i;
1446
1447         if (!(card = (isdnloop_card *) kmalloc(sizeof(isdnloop_card), GFP_KERNEL))) {
1448                 printk(KERN_WARNING
1449                  "isdnloop: (%s) Could not allocate card-struct.\n", id);
1450                 return (isdnloop_card *) 0;
1451         }
1452         memset((char *) card, 0, sizeof(isdnloop_card));
1453         card->interface.owner = THIS_MODULE;
1454         card->interface.channels = ISDNLOOP_BCH;
1455         card->interface.hl_hdrlen  = 1; /* scratch area for storing ack flag*/ 
1456         card->interface.maxbufsize = 4000;
1457         card->interface.command = if_command;
1458         card->interface.writebuf_skb = if_sendbuf;
1459         card->interface.writecmd = if_writecmd;
1460         card->interface.readstat = if_readstatus;
1461         card->interface.features = ISDN_FEATURE_L2_X75I |
1462 #ifdef CONFIG_ISDN_X25
1463             ISDN_FEATURE_L2_X25DTE |
1464             ISDN_FEATURE_L2_X25DCE |
1465 #endif
1466             ISDN_FEATURE_L2_HDLC |
1467             ISDN_FEATURE_L3_TRANS |
1468             ISDN_FEATURE_P_UNKNOWN;
1469         card->ptype = ISDN_PTYPE_UNKNOWN;
1470         strlcpy(card->interface.id, id, sizeof(card->interface.id));
1471         card->msg_buf_write = card->msg_buf;
1472         card->msg_buf_read = card->msg_buf;
1473         card->msg_buf_end = &card->msg_buf[sizeof(card->msg_buf) - 1];
1474         for (i = 0; i < ISDNLOOP_BCH; i++) {
1475                 card->l2_proto[i] = ISDN_PROTO_L2_X75I;
1476                 skb_queue_head_init(&card->bqueue[i]);
1477         }
1478         skb_queue_head_init(&card->dqueue);
1479         card->next = cards;
1480         cards = card;
1481         if (!register_isdn(&card->interface)) {
1482                 cards = cards->next;
1483                 printk(KERN_WARNING
1484                        "isdnloop: Unable to register %s\n", id);
1485                 kfree(card);
1486                 return (isdnloop_card *) 0;
1487         }
1488         card->myid = card->interface.channels;
1489         return card;
1490 }
1491
1492 static int
1493 isdnloop_addcard(char *id1)
1494 {
1495         isdnloop_card *card;
1496
1497         if (!(card = isdnloop_initcard(id1))) {
1498                 return -EIO;
1499         }
1500         printk(KERN_INFO
1501                "isdnloop: (%s) virtual card added\n",
1502                card->interface.id);
1503         return 0;
1504 }
1505
1506 static int __init
1507 isdnloop_init(void)
1508 {
1509         char *p;
1510         char rev[10];
1511
1512         if ((p = strchr(revision, ':'))) {
1513                 strcpy(rev, p + 1);
1514                 p = strchr(rev, '$');
1515                 *p = 0;
1516         } else
1517                 strcpy(rev, " ??? ");
1518         printk(KERN_NOTICE "isdnloop-ISDN-driver Rev%s\n", rev);
1519
1520         if (isdnloop_id)
1521                 return (isdnloop_addcard(isdnloop_id));
1522
1523         return 0;
1524 }
1525
1526 static void __exit
1527 isdnloop_exit(void)
1528 {
1529         isdn_ctrl cmd;
1530         isdnloop_card *card = cards;
1531         isdnloop_card *last;
1532         int i;
1533
1534         isdnloop_stopallcards();
1535         while (card) {
1536                 cmd.command = ISDN_STAT_UNLOAD;
1537                 cmd.driver = card->myid;
1538                 card->interface.statcallb(&cmd);
1539                 for (i = 0; i < ISDNLOOP_BCH; i++)
1540                         isdnloop_free_queue(card, i);
1541                 card = card->next;
1542         }
1543         card = cards;
1544         while (card) {
1545                 last = card;
1546                 skb_queue_purge(&card->dqueue);
1547                 card = card->next;
1548                 kfree(last);
1549         }
1550         printk(KERN_NOTICE "isdnloop-ISDN-driver unloaded\n");
1551 }
1552
1553 module_init(isdnloop_init);
1554 module_exit(isdnloop_exit);