Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[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/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/init.h>
15 #include <linux/sched.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_param(isdnloop_id, charp, 0);
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 __user *buf, int len, isdnloop_card * card)
447 {
448         int count;
449         u_char __user *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                 put_user(*card->msg_buf_read++, p);
455                 if (card->msg_buf_read > card->msg_buf_end)
456                         card->msg_buf_read = card->msg_buf;
457         }
458         return count;
459 }
460
461 /*
462  * Simulate a card's response by appending it to the cards
463  * message queue.
464  *
465  * Parameter:
466  *   card = pointer to card struct.
467  *   s    = pointer to message-string.
468  *   ch   = channel: 0 = generic messages, 1 and 2 = D-channel messages.
469  * Return:
470  *   0 on success, 1 on memory squeeze.
471  */
472 static int
473 isdnloop_fake(isdnloop_card * card, char *s, int ch)
474 {
475         struct sk_buff *skb;
476         int len = strlen(s) + ((ch >= 0) ? 3 : 0);
477
478         if (!(skb = dev_alloc_skb(len))) {
479                 printk(KERN_WARNING "isdnloop: Out of memory in isdnloop_fake\n");
480                 return 1;
481         }
482         if (ch >= 0)
483                 sprintf(skb_put(skb, 3), "%02d;", ch);
484         memcpy(skb_put(skb, strlen(s)), s, strlen(s));
485         skb_queue_tail(&card->dqueue, skb);
486         return 0;
487 }
488 /* *INDENT-OFF* */
489 static isdnloop_stat isdnloop_cmd_table[] =
490 {
491         {"BCON_R",         0,  1},      /* B-Channel connect        */
492         {"BCON_I",         0, 17},      /* B-Channel connect ind    */
493         {"BDIS_R",         0,  2},      /* B-Channel disconnect     */
494         {"DDIS_R",         0,  3},      /* D-Channel disconnect     */
495         {"DCON_R",         0, 16},      /* D-Channel connect        */
496         {"DSCA_R",         0,  4},      /* Dial 1TR6-SPV     */
497         {"DCAL_R",         0,  5},      /* Dial */
498         {"EAZC",           0,  6},      /* Clear EAZ listener */
499         {"EAZ",            0,  7},      /* Set EAZ listener */
500         {"SEEAZ",          0,  8},      /* Get EAZ listener */
501         {"MSN",            0,  9},      /* Set/Clear MSN listener */
502         {"MSALL",          0, 10},      /* Set multi MSN listeners */
503         {"SETSIL",         0, 11},      /* Set SI list     */
504         {"SEESIL",         0, 12},      /* Get SI list     */
505         {"SILC",           0, 13},      /* Clear SI list     */
506         {"LOCK",           0, -1},      /* LOCK channel     */
507         {"UNLOCK",         0, -1},      /* UNLOCK channel     */
508         {"FV2ON",          1, 14},      /* Leased mode on               */
509         {"FV2OFF",         1, 15},      /* Leased mode off              */
510         {NULL, 0, -1}
511 };
512 /* *INDENT-ON* */
513
514
515 /*
516  * Simulate an error-response from a card.
517  *
518  * Parameter:
519  *   card = pointer to card struct.
520  */
521 static void
522 isdnloop_fake_err(isdnloop_card * card)
523 {
524         char buf[60];
525
526         sprintf(buf, "E%s", card->omsg);
527         isdnloop_fake(card, buf, -1);
528         isdnloop_fake(card, "NAK", -1);
529 }
530
531 static u_char ctable_eu[] =
532 {0x00, 0x11, 0x01, 0x12};
533 static u_char ctable_1t[] =
534 {0x00, 0x3b, 0x01, 0x3a};
535
536 /*
537  * Assemble a simplified cause message depending on the
538  * D-channel protocol used.
539  *
540  * Parameter:
541  *   card = pointer to card struct.
542  *   loc  = location: 0 = local, 1 = remote.
543  *   cau  = cause: 1 = busy, 2 = nonexistent callerid, 3 = no user responding.
544  * Return:
545  *   Pointer to buffer containing the assembled message.
546  */
547 static char *
548 isdnloop_unicause(isdnloop_card * card, int loc, int cau)
549 {
550         static char buf[6];
551
552         switch (card->ptype) {
553                 case ISDN_PTYPE_EURO:
554                         sprintf(buf, "E%02X%02X", (loc) ? 4 : 2, ctable_eu[cau]);
555                         break;
556                 case ISDN_PTYPE_1TR6:
557                         sprintf(buf, "%02X44", ctable_1t[cau]);
558                         break;
559                 default:
560                         return ("0000");
561         }
562         return (buf);
563 }
564
565 /*
566  * Release a virtual connection. Called from timer interrupt, when
567  * called party did not respond.
568  *
569  * Parameter:
570  *   card = pointer to card struct.
571  *   ch   = channel (0-based)
572  */
573 static void
574 isdnloop_atimeout(isdnloop_card * card, int ch)
575 {
576         unsigned long flags;
577         char buf[60];
578
579         save_flags(flags);
580         cli();
581         if (card->rcard) {
582                 isdnloop_fake(card->rcard[ch], "DDIS_I", card->rch[ch] + 1);
583                 card->rcard[ch]->rcard[card->rch[ch]] = NULL;
584                 card->rcard[ch] = NULL;
585         }
586         isdnloop_fake(card, "DDIS_I", ch + 1);
587         /* No user responding */
588         sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 3));
589         isdnloop_fake(card, buf, ch + 1);
590         restore_flags(flags);
591 }
592
593 /*
594  * Wrapper for isdnloop_atimeout().
595  */
596 static void
597 isdnloop_atimeout0(unsigned long data)
598 {
599         isdnloop_card *card = (isdnloop_card *) data;
600         isdnloop_atimeout(card, 0);
601 }
602
603 /*
604  * Wrapper for isdnloop_atimeout().
605  */
606 static void
607 isdnloop_atimeout1(unsigned long data)
608 {
609         isdnloop_card *card = (isdnloop_card *) data;
610         isdnloop_atimeout(card, 1);
611 }
612
613 /*
614  * Install a watchdog for a user, not responding.
615  *
616  * Parameter:
617  *   card = pointer to card struct.
618  *   ch   = channel to watch for.
619  */
620 static void
621 isdnloop_start_ctimer(isdnloop_card * card, int ch)
622 {
623         unsigned long flags;
624
625         save_flags(flags);
626         cli();
627         init_timer(&card->c_timer[ch]);
628         card->c_timer[ch].expires = jiffies + ISDNLOOP_TIMER_ALERTWAIT;
629         if (ch)
630                 card->c_timer[ch].function = isdnloop_atimeout1;
631         else
632                 card->c_timer[ch].function = isdnloop_atimeout0;
633         card->c_timer[ch].data = (unsigned long) card;
634         add_timer(&card->c_timer[ch]);
635         restore_flags(flags);
636 }
637
638 /*
639  * Kill a pending channel watchdog.
640  *
641  * Parameter:
642  *   card = pointer to card struct.
643  *   ch   = channel (0-based).
644  */
645 static void
646 isdnloop_kill_ctimer(isdnloop_card * card, int ch)
647 {
648         unsigned long flags;
649
650         save_flags(flags);
651         cli();
652         del_timer(&card->c_timer[ch]);
653         restore_flags(flags);
654 }
655
656 static u_char si2bit[] =
657 {0, 1, 0, 0, 0, 2, 0, 4, 0, 0};
658 static u_char bit2si[] =
659 {1, 5, 7};
660
661 /*
662  * Try finding a listener for an outgoing call.
663  *
664  * Parameter:
665  *   card = pointer to calling card.
666  *   p    = pointer to ICN-type setup-string.
667  *   lch  = channel of calling card.
668  *   cmd  = pointer to struct to be filled when parsing setup.
669  * Return:
670  *   0 = found match, alerting should happen.
671  *   1 = found matching number but it is busy.
672  *   2 = no matching listener.
673  *   3 = found matching number but SI does not match.
674  */
675 static int
676 isdnloop_try_call(isdnloop_card * card, char *p, int lch, isdn_ctrl * cmd)
677 {
678         isdnloop_card *cc = cards;
679         unsigned long flags;
680         int ch;
681         int num_match;
682         int i;
683         char *e;
684         char nbuf[32];
685
686         isdnloop_parse_setup(p, cmd);
687         while (cc) {
688                 for (ch = 0; ch < 2; ch++) {
689                         /* Exclude ourself */
690                         if ((cc == card) && (ch == lch))
691                                 continue;
692                         num_match = 0;
693                         switch (cc->ptype) {
694                                 case ISDN_PTYPE_EURO:
695                                         for (i = 0; i < 3; i++)
696                                                 if (!(strcmp(cc->s0num[i], cmd->parm.setup.phone)))
697                                                         num_match = 1;
698                                         break;
699                                 case ISDN_PTYPE_1TR6:
700                                         e = cc->eazlist[ch];
701                                         while (*e) {
702                                                 sprintf(nbuf, "%s%c", cc->s0num[0], *e);
703                                                 if (!(strcmp(nbuf, cmd->parm.setup.phone)))
704                                                         num_match = 1;
705                                                 e++;
706                                         }
707                         }
708                         if (num_match) {
709                                 save_flags(flags);
710                                 cli();
711                                 /* channel idle? */
712                                 if (!(cc->rcard[ch])) {
713                                         /* Check SI */
714                                         if (!(si2bit[cmd->parm.setup.si1] & cc->sil[ch])) {
715                                                 restore_flags(flags);
716                                                 return 3;
717                                         }
718                                         /* ch is idle, si and number matches */
719                                         cc->rcard[ch] = card;
720                                         cc->rch[ch] = lch;
721                                         card->rcard[lch] = cc;
722                                         card->rch[lch] = ch;
723                                         restore_flags(flags);
724                                         return 0;
725                                 } else {
726                                         restore_flags(flags);
727                                         /* num matches, but busy */
728                                         if (ch == 1)
729                                                 return 1;
730                                 }
731                         }
732                 }
733                 cc = cc->next;
734         }
735         return 2;
736 }
737
738 /*
739  * Depending on D-channel protocol and caller/called, modify
740  * phone number.
741  *
742  * Parameter:
743  *   card   = pointer to card struct.
744  *   phone  = pointer phone number.
745  *   caller = flag: 1 = caller, 0 = called.
746  * Return:
747  *   pointer to new phone number.
748  */
749 static char *
750 isdnloop_vstphone(isdnloop_card * card, char *phone, int caller)
751 {
752         int i;
753         static char nphone[30];
754
755         if (!card) {
756                 printk("BUG!!!\n");
757                 return "";
758         }
759         switch (card->ptype) {
760                 case ISDN_PTYPE_EURO:
761                         if (caller) {
762                                 for (i = 0; i < 2; i++)
763                                         if (!(strcmp(card->s0num[i], phone)))
764                                                 return (phone);
765                                 return (card->s0num[0]);
766                         }
767                         return (phone);
768                         break;
769                 case ISDN_PTYPE_1TR6:
770                         if (caller) {
771                                 sprintf(nphone, "%s%c", card->s0num[0], phone[0]);
772                                 return (nphone);
773                         } else
774                                 return (&phone[strlen(phone) - 1]);
775                         break;
776         }
777         return "";
778 }
779
780 /*
781  * Parse an ICN-type command string sent to the 'card'.
782  * Perform misc. actions depending on the command.
783  *
784  * Parameter:
785  *   card = pointer to card struct.
786  */
787 static void
788 isdnloop_parse_cmd(isdnloop_card * card)
789 {
790         char *p = card->omsg;
791         isdn_ctrl cmd;
792         char buf[60];
793         isdnloop_stat *s = isdnloop_cmd_table;
794         int action = -1;
795         int i;
796         int ch;
797
798         if ((card->omsg[0] != '0') && (card->omsg[2] != ';')) {
799                 isdnloop_fake_err(card);
800                 return;
801         }
802         ch = card->omsg[1] - '0';
803         if ((ch < 0) || (ch > 2)) {
804                 isdnloop_fake_err(card);
805                 return;
806         }
807         p += 3;
808         while (s->statstr) {
809                 if (!strncmp(p, s->statstr, strlen(s->statstr))) {
810                         action = s->action;
811                         if (s->command && (ch != 0)) {
812                                 isdnloop_fake_err(card);
813                                 return;
814                         }
815                         break;
816                 }
817                 s++;
818         }
819         if (action == -1)
820                 return;
821         switch (action) {
822                 case 1:
823                         /* 0x;BCON_R */
824                         if (card->rcard[ch - 1]) {
825                                 isdnloop_fake(card->rcard[ch - 1], "BCON_I",
826                                               card->rch[ch - 1] + 1);
827                                 isdnloop_fake(card, "BCON_C", ch);
828                         }
829                         break;
830                 case 17:
831                         /* 0x;BCON_I */
832                         if (card->rcard[ch - 1]) {
833                                 isdnloop_fake(card->rcard[ch - 1], "BCON_C",
834                                               card->rch[ch - 1] + 1);
835                         }
836                         break;
837                 case 2:
838                         /* 0x;BDIS_R */
839                         isdnloop_fake(card, "BDIS_C", ch);
840                         if (card->rcard[ch - 1]) {
841                                 isdnloop_fake(card->rcard[ch - 1], "BDIS_I",
842                                               card->rch[ch - 1] + 1);
843                         }
844                         break;
845                 case 16:
846                         /* 0x;DCON_R */
847                         isdnloop_kill_ctimer(card, ch - 1);
848                         if (card->rcard[ch - 1]) {
849                                 isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
850                                 isdnloop_fake(card->rcard[ch - 1], "DCON_C",
851                                               card->rch[ch - 1] + 1);
852                                 isdnloop_fake(card, "DCON_C", ch);
853                         }
854                         break;
855                 case 3:
856                         /* 0x;DDIS_R */
857                         isdnloop_kill_ctimer(card, ch - 1);
858                         if (card->rcard[ch - 1]) {
859                                 isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
860                                 isdnloop_fake(card->rcard[ch - 1], "DDIS_I",
861                                               card->rch[ch - 1] + 1);
862                                 card->rcard[ch - 1] = NULL;
863                         }
864                         isdnloop_fake(card, "DDIS_C", ch);
865                         break;
866                 case 4:
867                         /* 0x;DSCA_Rdd,yy,zz,oo */
868                         if (card->ptype != ISDN_PTYPE_1TR6) {
869                                 isdnloop_fake_err(card);
870                                 return;
871                         }
872                         /* Fall through */
873                 case 5:
874                         /* 0x;DCAL_Rdd,yy,zz,oo */
875                         p += 6;
876                         switch (isdnloop_try_call(card, p, ch - 1, &cmd)) {
877                                 case 0:
878                                         /* Alerting */
879                                         sprintf(buf, "D%s_I%s,%02d,%02d,%s",
880                                            (action == 4) ? "SCA" : "CAL",
881                                                 isdnloop_vstphone(card, cmd.parm.setup.eazmsn, 1),
882                                                 cmd.parm.setup.si1,
883                                                 cmd.parm.setup.si2,
884                                         isdnloop_vstphone(card->rcard[ch - 1],
885                                                cmd.parm.setup.phone, 0));
886                                         isdnloop_fake(card->rcard[ch - 1], buf, card->rch[ch - 1] + 1);
887                                         /* Fall through */
888                                 case 3:
889                                         /* si1 does not match, don't alert but start timer */
890                                         isdnloop_start_ctimer(card, ch - 1);
891                                         break;
892                                 case 1:
893                                         /* Remote busy */
894                                         isdnloop_fake(card, "DDIS_I", ch);
895                                         sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 1));
896                                         isdnloop_fake(card, buf, ch);
897                                         break;
898                                 case 2:
899                                         /* No such user */
900                                         isdnloop_fake(card, "DDIS_I", ch);
901                                         sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 2));
902                                         isdnloop_fake(card, buf, ch);
903                                         break;
904                         }
905                         break;
906                 case 6:
907                         /* 0x;EAZC */
908                         card->eazlist[ch - 1][0] = '\0';
909                         break;
910                 case 7:
911                         /* 0x;EAZ */
912                         p += 3;
913                         strcpy(card->eazlist[ch - 1], p);
914                         break;
915                 case 8:
916                         /* 0x;SEEAZ */
917                         sprintf(buf, "EAZ-LIST: %s", card->eazlist[ch - 1]);
918                         isdnloop_fake(card, buf, ch + 1);
919                         break;
920                 case 9:
921                         /* 0x;MSN */
922                         break;
923                 case 10:
924                         /* 0x;MSNALL */
925                         break;
926                 case 11:
927                         /* 0x;SETSIL */
928                         p += 6;
929                         i = 0;
930                         while (strchr("0157", *p)) {
931                                 if (i)
932                                         card->sil[ch - 1] |= si2bit[*p - '0'];
933                                 i = (*p++ == '0');
934                         }
935                         if (*p)
936                                 isdnloop_fake_err(card);
937                         break;
938                 case 12:
939                         /* 0x;SEESIL */
940                         sprintf(buf, "SIN-LIST: ");
941                         p = buf + 10;
942                         for (i = 0; i < 3; i++)
943                                 if (card->sil[ch - 1] & (1 << i))
944                                         p += sprintf(p, "%02d", bit2si[i]);
945                         isdnloop_fake(card, buf, ch + 1);
946                         break;
947                 case 13:
948                         /* 0x;SILC */
949                         card->sil[ch - 1] = 0;
950                         break;
951                 case 14:
952                         /* 00;FV2ON */
953                         break;
954                 case 15:
955                         /* 00;FV2OFF */
956                         break;
957         }
958 }
959
960 /*
961  * Put command-strings into the of the 'card'. In reality, execute them
962  * right in place by calling isdnloop_parse_cmd(). Also copy every
963  * command to the read message ringbuffer, preceeding it with a '>'.
964  * These mesagges can be read at /dev/isdnctrl.
965  *
966  * Parameter:
967  *   buf  = pointer to command buffer.
968  *   len  = length of buffer data.
969  *   user = flag: 1 = called form userlevel, 0 called from kernel.
970  *   card = pointer to card struct.
971  * Return:
972  *   number of bytes transferred (currently always equals len).
973  */
974 static int
975 isdnloop_writecmd(const u_char * buf, int len, int user, isdnloop_card * card)
976 {
977         int xcount = 0;
978         int ocount = 1;
979         isdn_ctrl cmd;
980
981         while (len) {
982                 int count = len;
983                 u_char *p;
984                 u_char msg[0x100];
985
986                 if (count > 255)
987                         count = 255;
988                 if (user) {
989                         if (copy_from_user(msg, buf, count))
990                                 return -EFAULT;
991                 } else
992                         memcpy(msg, buf, count);
993                 isdnloop_putmsg(card, '>');
994                 for (p = msg; count > 0; count--, p++) {
995                         len--;
996                         xcount++;
997                         isdnloop_putmsg(card, *p);
998                         card->omsg[card->optr] = *p;
999                         if (*p == '\n') {
1000                                 card->omsg[card->optr] = '\0';
1001                                 card->optr = 0;
1002                                 isdnloop_parse_cmd(card);
1003                                 if (len) {
1004                                         isdnloop_putmsg(card, '>');
1005                                         ocount++;
1006                                 }
1007                         } else {
1008                                 if (card->optr < 59)
1009                                         card->optr++;
1010                         }
1011                         ocount++;
1012                 }
1013         }
1014         cmd.command = ISDN_STAT_STAVAIL;
1015         cmd.driver = card->myid;
1016         cmd.arg = ocount;
1017         card->interface.statcallb(&cmd);
1018         return xcount;
1019 }
1020
1021 /*
1022  * Delete card's pending timers, send STOP to linklevel
1023  */
1024 static void
1025 isdnloop_stopcard(isdnloop_card * card)
1026 {
1027         unsigned long flags;
1028         isdn_ctrl cmd;
1029
1030         save_flags(flags);
1031         cli();
1032         if (card->flags & ISDNLOOP_FLAGS_RUNNING) {
1033                 card->flags &= ~ISDNLOOP_FLAGS_RUNNING;
1034                 del_timer(&card->st_timer);
1035                 del_timer(&card->rb_timer);
1036                 del_timer(&card->c_timer[0]);
1037                 del_timer(&card->c_timer[1]);
1038                 cmd.command = ISDN_STAT_STOP;
1039                 cmd.driver = card->myid;
1040                 card->interface.statcallb(&cmd);
1041         }
1042         restore_flags(flags);
1043 }
1044
1045 /*
1046  * Stop all cards before unload.
1047  */
1048 static void
1049 isdnloop_stopallcards(void)
1050 {
1051         isdnloop_card *p = cards;
1052
1053         while (p) {
1054                 isdnloop_stopcard(p);
1055                 p = p->next;
1056         }
1057 }
1058
1059 /*
1060  * Start a 'card'. Simulate card's boot message and set the phone
1061  * number(s) of the virtual 'S0-Interface'. Install D-channel
1062  * poll timer.
1063  *
1064  * Parameter:
1065  *   card  = pointer to card struct.
1066  *   sdefp = pointer to struct holding ioctl parameters.
1067  * Return:
1068  *   0 on success, -E??? otherwise.
1069  */
1070 static int
1071 isdnloop_start(isdnloop_card * card, isdnloop_sdef * sdefp)
1072 {
1073         unsigned long flags;
1074         isdnloop_sdef sdef;
1075         int i;
1076
1077         if (card->flags & ISDNLOOP_FLAGS_RUNNING)
1078                 return -EBUSY;
1079         if (copy_from_user((char *) &sdef, (char *) sdefp, sizeof(sdef)))
1080                 return -EFAULT;
1081         save_flags(flags);
1082         cli();
1083         switch (sdef.ptype) {
1084                 case ISDN_PTYPE_EURO:
1085                         if (isdnloop_fake(card, "DRV1.23EC-Q.931-CAPI-CNS-BASIS-20.02.96",
1086                                           -1)) {
1087                                 restore_flags(flags);
1088                                 return -ENOMEM;
1089                         }
1090                         card->sil[0] = card->sil[1] = 4;
1091                         if (isdnloop_fake(card, "TEI OK", 0)) {
1092                                 restore_flags(flags);
1093                                 return -ENOMEM;
1094                         }
1095                         for (i = 0; i < 3; i++)
1096                                 strcpy(card->s0num[i], sdef.num[i]);
1097                         break;
1098                 case ISDN_PTYPE_1TR6:
1099                         if (isdnloop_fake(card, "DRV1.04TC-1TR6-CAPI-CNS-BASIS-29.11.95",
1100                                           -1)) {
1101                                 restore_flags(flags);
1102                                 return -ENOMEM;
1103                         }
1104                         card->sil[0] = card->sil[1] = 4;
1105                         if (isdnloop_fake(card, "TEI OK", 0)) {
1106                                 restore_flags(flags);
1107                                 return -ENOMEM;
1108                         }
1109                         strcpy(card->s0num[0], sdef.num[0]);
1110                         card->s0num[1][0] = '\0';
1111                         card->s0num[2][0] = '\0';
1112                         break;
1113                 default:
1114                         restore_flags(flags);
1115                         printk(KERN_WARNING "isdnloop: Illegal D-channel protocol %d\n",
1116                                sdef.ptype);
1117                         return -EINVAL;
1118         }
1119         init_timer(&card->st_timer);
1120         card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD;
1121         card->st_timer.function = isdnloop_polldchan;
1122         card->st_timer.data = (unsigned long) card;
1123         add_timer(&card->st_timer);
1124         card->flags |= ISDNLOOP_FLAGS_RUNNING;
1125         restore_flags(flags);
1126         return 0;
1127 }
1128
1129 /*
1130  * Main handler for commands sent by linklevel.
1131  */
1132 static int
1133 isdnloop_command(isdn_ctrl * c, isdnloop_card * card)
1134 {
1135         ulong a;
1136         int i;
1137         char cbuf[60];
1138         isdn_ctrl cmd;
1139         isdnloop_cdef cdef;
1140
1141         switch (c->command) {
1142                 case ISDN_CMD_IOCTL:
1143                         memcpy(&a, c->parm.num, sizeof(ulong));
1144                         switch (c->arg) {
1145                                 case ISDNLOOP_IOCTL_DEBUGVAR:
1146                                         return (ulong) card;
1147                                 case ISDNLOOP_IOCTL_STARTUP:
1148                                         if (!access_ok(VERIFY_READ, (void *) a, sizeof(isdnloop_sdef)))
1149                                                 return -EFAULT;
1150                                         return (isdnloop_start(card, (isdnloop_sdef *) a));
1151                                         break;
1152                                 case ISDNLOOP_IOCTL_ADDCARD:
1153                                         if (copy_from_user((char *)&cdef,
1154                                                            (char *)a,
1155                                                            sizeof(cdef)))
1156                                                 return -EFAULT;
1157                                         return (isdnloop_addcard(cdef.id1));
1158                                         break;
1159                                 case ISDNLOOP_IOCTL_LEASEDCFG:
1160                                         if (a) {
1161                                                 if (!card->leased) {
1162                                                         card->leased = 1;
1163                                                         while (card->ptype == ISDN_PTYPE_UNKNOWN)
1164                                                                 schedule_timeout_interruptible(10);
1165                                                         schedule_timeout_interruptible(10);
1166                                                         sprintf(cbuf, "00;FV2ON\n01;EAZ1\n02;EAZ2\n");
1167                                                         i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1168                                                         printk(KERN_INFO
1169                                                                "isdnloop: (%s) Leased-line mode enabled\n",
1170                                                                CID);
1171                                                         cmd.command = ISDN_STAT_RUN;
1172                                                         cmd.driver = card->myid;
1173                                                         cmd.arg = 0;
1174                                                         card->interface.statcallb(&cmd);
1175                                                 }
1176                                         } else {
1177                                                 if (card->leased) {
1178                                                         card->leased = 0;
1179                                                         sprintf(cbuf, "00;FV2OFF\n");
1180                                                         i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1181                                                         printk(KERN_INFO
1182                                                                "isdnloop: (%s) Leased-line mode disabled\n",
1183                                                                CID);
1184                                                         cmd.command = ISDN_STAT_RUN;
1185                                                         cmd.driver = card->myid;
1186                                                         cmd.arg = 0;
1187                                                         card->interface.statcallb(&cmd);
1188                                                 }
1189                                         }
1190                                         return 0;
1191                                 default:
1192                                         return -EINVAL;
1193                         }
1194                         break;
1195                 case ISDN_CMD_DIAL:
1196                         if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1197                                 return -ENODEV;
1198                         if (card->leased)
1199                                 break;
1200                         if ((c->arg & 255) < ISDNLOOP_BCH) {
1201                                 char *p;
1202                                 char dial[50];
1203                                 char dcode[4];
1204
1205                                 a = c->arg;
1206                                 p = c->parm.setup.phone;
1207                                 if (*p == 's' || *p == 'S') {
1208                                         /* Dial for SPV */
1209                                         p++;
1210                                         strcpy(dcode, "SCA");
1211                                 } else
1212                                         /* Normal Dial */
1213                                         strcpy(dcode, "CAL");
1214                                 strcpy(dial, p);
1215                                 sprintf(cbuf, "%02d;D%s_R%s,%02d,%02d,%s\n", (int) (a + 1),
1216                                         dcode, dial, c->parm.setup.si1,
1217                                 c->parm.setup.si2, c->parm.setup.eazmsn);
1218                                 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1219                         }
1220                         break;
1221                 case ISDN_CMD_ACCEPTD:
1222                         if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1223                                 return -ENODEV;
1224                         if (c->arg < ISDNLOOP_BCH) {
1225                                 a = c->arg + 1;
1226                                 cbuf[0] = 0;
1227                                 switch (card->l2_proto[a - 1]) {
1228                                         case ISDN_PROTO_L2_X75I:
1229                                                 sprintf(cbuf, "%02d;BX75\n", (int) a);
1230                                                 break;
1231 #ifdef CONFIG_ISDN_X25
1232                                         case ISDN_PROTO_L2_X25DTE:
1233                                                 sprintf(cbuf, "%02d;BX2T\n", (int) a);
1234                                                 break;
1235                                         case ISDN_PROTO_L2_X25DCE:
1236                                                 sprintf(cbuf, "%02d;BX2C\n", (int) a);
1237                                                 break;
1238 #endif
1239                                         case ISDN_PROTO_L2_HDLC:
1240                                                 sprintf(cbuf, "%02d;BTRA\n", (int) a);
1241                                                 break;
1242                                 }
1243                                 if (strlen(cbuf))
1244                                         i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1245                                 sprintf(cbuf, "%02d;DCON_R\n", (int) a);
1246                                 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1247                         }
1248                         break;
1249                 case ISDN_CMD_ACCEPTB:
1250                         if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1251                                 return -ENODEV;
1252                         if (c->arg < ISDNLOOP_BCH) {
1253                                 a = c->arg + 1;
1254                                 switch (card->l2_proto[a - 1]) {
1255                                         case ISDN_PROTO_L2_X75I:
1256                                                 sprintf(cbuf, "%02d;BCON_R,BX75\n", (int) a);
1257                                                 break;
1258 #ifdef CONFIG_ISDN_X25
1259                                         case ISDN_PROTO_L2_X25DTE:
1260                                                 sprintf(cbuf, "%02d;BCON_R,BX2T\n", (int) a);
1261                                                 break;
1262                                         case ISDN_PROTO_L2_X25DCE:
1263                                                 sprintf(cbuf, "%02d;BCON_R,BX2C\n", (int) a);
1264                                                 break;
1265 #endif
1266                                         case ISDN_PROTO_L2_HDLC:
1267                                                 sprintf(cbuf, "%02d;BCON_R,BTRA\n", (int) a);
1268                                                 break;
1269                                         default:
1270                                                 sprintf(cbuf, "%02d;BCON_R\n", (int) a);
1271                                 }
1272                                 printk(KERN_DEBUG "isdnloop writecmd '%s'\n", cbuf);
1273                                 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1274                                 break;
1275                 case ISDN_CMD_HANGUP:
1276                                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1277                                         return -ENODEV;
1278                                 if (c->arg < ISDNLOOP_BCH) {
1279                                         a = c->arg + 1;
1280                                         sprintf(cbuf, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a, (int) a);
1281                                         i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1282                                 }
1283                                 break;
1284                 case ISDN_CMD_SETEAZ:
1285                                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1286                                         return -ENODEV;
1287                                 if (card->leased)
1288                                         break;
1289                                 if (c->arg < ISDNLOOP_BCH) {
1290                                         a = c->arg + 1;
1291                                         if (card->ptype == ISDN_PTYPE_EURO) {
1292                                                 sprintf(cbuf, "%02d;MS%s%s\n", (int) a,
1293                                                         c->parm.num[0] ? "N" : "ALL", c->parm.num);
1294                                         } else
1295                                                 sprintf(cbuf, "%02d;EAZ%s\n", (int) a,
1296                                                         c->parm.num[0] ? c->parm.num : (u_char *) "0123456789");
1297                                         i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1298                                 }
1299                                 break;
1300                 case ISDN_CMD_CLREAZ:
1301                                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1302                                         return -ENODEV;
1303                                 if (card->leased)
1304                                         break;
1305                                 if (c->arg < ISDNLOOP_BCH) {
1306                                         a = c->arg + 1;
1307                                         if (card->ptype == ISDN_PTYPE_EURO)
1308                                                 sprintf(cbuf, "%02d;MSNC\n", (int) a);
1309                                         else
1310                                                 sprintf(cbuf, "%02d;EAZC\n", (int) a);
1311                                         i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1312                                 }
1313                                 break;
1314                 case ISDN_CMD_SETL2:
1315                                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1316                                         return -ENODEV;
1317                                 if ((c->arg & 255) < ISDNLOOP_BCH) {
1318                                         a = c->arg;
1319                                         switch (a >> 8) {
1320                                                 case ISDN_PROTO_L2_X75I:
1321                                                         sprintf(cbuf, "%02d;BX75\n", (int) (a & 255) + 1);
1322                                                         break;
1323 #ifdef CONFIG_ISDN_X25
1324                                                 case ISDN_PROTO_L2_X25DTE:
1325                                                         sprintf(cbuf, "%02d;BX2T\n", (int) (a & 255) + 1);
1326                                                         break;
1327                                                 case ISDN_PROTO_L2_X25DCE:
1328                                                         sprintf(cbuf, "%02d;BX2C\n", (int) (a & 255) + 1);
1329                                                         break;
1330 #endif
1331                                                 case ISDN_PROTO_L2_HDLC:
1332                                                         sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
1333                                                         break;
1334                                                 case ISDN_PROTO_L2_TRANS:
1335                                                         sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
1336                                                         break;
1337                                                 default:
1338                                                         return -EINVAL;
1339                                         }
1340                                         i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1341                                         card->l2_proto[a & 255] = (a >> 8);
1342                                 }
1343                                 break;
1344                 case ISDN_CMD_SETL3:
1345                                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1346                                         return -ENODEV;
1347                                 return 0;
1348                 default:
1349                                 return -EINVAL;
1350                         }
1351         }
1352         return 0;
1353 }
1354
1355 /*
1356  * Find card with given driverId
1357  */
1358 static inline isdnloop_card *
1359 isdnloop_findcard(int driverid)
1360 {
1361         isdnloop_card *p = cards;
1362
1363         while (p) {
1364                 if (p->myid == driverid)
1365                         return p;
1366                 p = p->next;
1367         }
1368         return (isdnloop_card *) 0;
1369 }
1370
1371 /*
1372  * Wrapper functions for interface to linklevel
1373  */
1374 static int
1375 if_command(isdn_ctrl * c)
1376 {
1377         isdnloop_card *card = isdnloop_findcard(c->driver);
1378
1379         if (card)
1380                 return (isdnloop_command(c, card));
1381         printk(KERN_ERR
1382                "isdnloop: if_command called with invalid driverId!\n");
1383         return -ENODEV;
1384 }
1385
1386 static int
1387 if_writecmd(const u_char __user *buf, int len, int id, int channel)
1388 {
1389         isdnloop_card *card = isdnloop_findcard(id);
1390
1391         if (card) {
1392                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1393                         return -ENODEV;
1394                 return (isdnloop_writecmd(buf, len, 1, card));
1395         }
1396         printk(KERN_ERR
1397                "isdnloop: if_writecmd called with invalid driverId!\n");
1398         return -ENODEV;
1399 }
1400
1401 static int
1402 if_readstatus(u_char __user *buf, int len, int id, int channel)
1403 {
1404         isdnloop_card *card = isdnloop_findcard(id);
1405
1406         if (card) {
1407                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1408                         return -ENODEV;
1409                 return (isdnloop_readstatus(buf, len, card));
1410         }
1411         printk(KERN_ERR
1412                "isdnloop: if_readstatus called with invalid driverId!\n");
1413         return -ENODEV;
1414 }
1415
1416 static int
1417 if_sendbuf(int id, int channel, int ack, struct sk_buff *skb)
1418 {
1419         isdnloop_card *card = isdnloop_findcard(id);
1420
1421         if (card) {
1422                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1423                         return -ENODEV;
1424                 /* ack request stored in skb scratch area */
1425                 *(skb->head) = ack;
1426                 return (isdnloop_sendbuf(channel, skb, card));
1427         }
1428         printk(KERN_ERR
1429                "isdnloop: if_sendbuf called with invalid driverId!\n");
1430         return -ENODEV;
1431 }
1432
1433 /*
1434  * Allocate a new card-struct, initialize it
1435  * link it into cards-list and register it at linklevel.
1436  */
1437 static isdnloop_card *
1438 isdnloop_initcard(char *id)
1439 {
1440         isdnloop_card *card;
1441         int i;
1442
1443         if (!(card = (isdnloop_card *) kmalloc(sizeof(isdnloop_card), GFP_KERNEL))) {
1444                 printk(KERN_WARNING
1445                  "isdnloop: (%s) Could not allocate card-struct.\n", id);
1446                 return (isdnloop_card *) 0;
1447         }
1448         memset((char *) card, 0, sizeof(isdnloop_card));
1449         card->interface.owner = THIS_MODULE;
1450         card->interface.channels = ISDNLOOP_BCH;
1451         card->interface.hl_hdrlen  = 1; /* scratch area for storing ack flag*/ 
1452         card->interface.maxbufsize = 4000;
1453         card->interface.command = if_command;
1454         card->interface.writebuf_skb = if_sendbuf;
1455         card->interface.writecmd = if_writecmd;
1456         card->interface.readstat = if_readstatus;
1457         card->interface.features = ISDN_FEATURE_L2_X75I |
1458 #ifdef CONFIG_ISDN_X25
1459             ISDN_FEATURE_L2_X25DTE |
1460             ISDN_FEATURE_L2_X25DCE |
1461 #endif
1462             ISDN_FEATURE_L2_HDLC |
1463             ISDN_FEATURE_L3_TRANS |
1464             ISDN_FEATURE_P_UNKNOWN;
1465         card->ptype = ISDN_PTYPE_UNKNOWN;
1466         strlcpy(card->interface.id, id, sizeof(card->interface.id));
1467         card->msg_buf_write = card->msg_buf;
1468         card->msg_buf_read = card->msg_buf;
1469         card->msg_buf_end = &card->msg_buf[sizeof(card->msg_buf) - 1];
1470         for (i = 0; i < ISDNLOOP_BCH; i++) {
1471                 card->l2_proto[i] = ISDN_PROTO_L2_X75I;
1472                 skb_queue_head_init(&card->bqueue[i]);
1473         }
1474         skb_queue_head_init(&card->dqueue);
1475         card->next = cards;
1476         cards = card;
1477         if (!register_isdn(&card->interface)) {
1478                 cards = cards->next;
1479                 printk(KERN_WARNING
1480                        "isdnloop: Unable to register %s\n", id);
1481                 kfree(card);
1482                 return (isdnloop_card *) 0;
1483         }
1484         card->myid = card->interface.channels;
1485         return card;
1486 }
1487
1488 static int
1489 isdnloop_addcard(char *id1)
1490 {
1491         isdnloop_card *card;
1492
1493         if (!(card = isdnloop_initcard(id1))) {
1494                 return -EIO;
1495         }
1496         printk(KERN_INFO
1497                "isdnloop: (%s) virtual card added\n",
1498                card->interface.id);
1499         return 0;
1500 }
1501
1502 static int __init
1503 isdnloop_init(void)
1504 {
1505         char *p;
1506         char rev[10];
1507
1508         if ((p = strchr(revision, ':'))) {
1509                 strcpy(rev, p + 1);
1510                 p = strchr(rev, '$');
1511                 *p = 0;
1512         } else
1513                 strcpy(rev, " ??? ");
1514         printk(KERN_NOTICE "isdnloop-ISDN-driver Rev%s\n", rev);
1515
1516         if (isdnloop_id)
1517                 return (isdnloop_addcard(isdnloop_id));
1518
1519         return 0;
1520 }
1521
1522 static void __exit
1523 isdnloop_exit(void)
1524 {
1525         isdn_ctrl cmd;
1526         isdnloop_card *card = cards;
1527         isdnloop_card *last;
1528         int i;
1529
1530         isdnloop_stopallcards();
1531         while (card) {
1532                 cmd.command = ISDN_STAT_UNLOAD;
1533                 cmd.driver = card->myid;
1534                 card->interface.statcallb(&cmd);
1535                 for (i = 0; i < ISDNLOOP_BCH; i++)
1536                         isdnloop_free_queue(card, i);
1537                 card = card->next;
1538         }
1539         card = cards;
1540         while (card) {
1541                 last = card;
1542                 skb_queue_purge(&card->dqueue);
1543                 card = card->next;
1544                 kfree(last);
1545         }
1546         printk(KERN_NOTICE "isdnloop-ISDN-driver unloaded\n");
1547 }
1548
1549 module_init(isdnloop_init);
1550 module_exit(isdnloop_exit);