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