[PATCH] I2C: via686a cleanups
[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                                                                 set_current_state(TASK_INTERRUPTIBLE);
1166                                                                 schedule_timeout(10);
1167                                                         }
1168                                                         set_current_state(TASK_INTERRUPTIBLE);
1169                                                         schedule_timeout(10);
1170                                                         sprintf(cbuf, "00;FV2ON\n01;EAZ1\n02;EAZ2\n");
1171                                                         i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1172                                                         printk(KERN_INFO
1173                                                                "isdnloop: (%s) Leased-line mode enabled\n",
1174                                                                CID);
1175                                                         cmd.command = ISDN_STAT_RUN;
1176                                                         cmd.driver = card->myid;
1177                                                         cmd.arg = 0;
1178                                                         card->interface.statcallb(&cmd);
1179                                                 }
1180                                         } else {
1181                                                 if (card->leased) {
1182                                                         card->leased = 0;
1183                                                         sprintf(cbuf, "00;FV2OFF\n");
1184                                                         i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1185                                                         printk(KERN_INFO
1186                                                                "isdnloop: (%s) Leased-line mode disabled\n",
1187                                                                CID);
1188                                                         cmd.command = ISDN_STAT_RUN;
1189                                                         cmd.driver = card->myid;
1190                                                         cmd.arg = 0;
1191                                                         card->interface.statcallb(&cmd);
1192                                                 }
1193                                         }
1194                                         return 0;
1195                                 default:
1196                                         return -EINVAL;
1197                         }
1198                         break;
1199                 case ISDN_CMD_DIAL:
1200                         if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1201                                 return -ENODEV;
1202                         if (card->leased)
1203                                 break;
1204                         if ((c->arg & 255) < ISDNLOOP_BCH) {
1205                                 char *p;
1206                                 char dial[50];
1207                                 char dcode[4];
1208
1209                                 a = c->arg;
1210                                 p = c->parm.setup.phone;
1211                                 if (*p == 's' || *p == 'S') {
1212                                         /* Dial for SPV */
1213                                         p++;
1214                                         strcpy(dcode, "SCA");
1215                                 } else
1216                                         /* Normal Dial */
1217                                         strcpy(dcode, "CAL");
1218                                 strcpy(dial, p);
1219                                 sprintf(cbuf, "%02d;D%s_R%s,%02d,%02d,%s\n", (int) (a + 1),
1220                                         dcode, dial, c->parm.setup.si1,
1221                                 c->parm.setup.si2, c->parm.setup.eazmsn);
1222                                 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1223                         }
1224                         break;
1225                 case ISDN_CMD_ACCEPTD:
1226                         if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1227                                 return -ENODEV;
1228                         if (c->arg < ISDNLOOP_BCH) {
1229                                 a = c->arg + 1;
1230                                 cbuf[0] = 0;
1231                                 switch (card->l2_proto[a - 1]) {
1232                                         case ISDN_PROTO_L2_X75I:
1233                                                 sprintf(cbuf, "%02d;BX75\n", (int) a);
1234                                                 break;
1235 #ifdef CONFIG_ISDN_X25
1236                                         case ISDN_PROTO_L2_X25DTE:
1237                                                 sprintf(cbuf, "%02d;BX2T\n", (int) a);
1238                                                 break;
1239                                         case ISDN_PROTO_L2_X25DCE:
1240                                                 sprintf(cbuf, "%02d;BX2C\n", (int) a);
1241                                                 break;
1242 #endif
1243                                         case ISDN_PROTO_L2_HDLC:
1244                                                 sprintf(cbuf, "%02d;BTRA\n", (int) a);
1245                                                 break;
1246                                 }
1247                                 if (strlen(cbuf))
1248                                         i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1249                                 sprintf(cbuf, "%02d;DCON_R\n", (int) a);
1250                                 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1251                         }
1252                         break;
1253                 case ISDN_CMD_ACCEPTB:
1254                         if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1255                                 return -ENODEV;
1256                         if (c->arg < ISDNLOOP_BCH) {
1257                                 a = c->arg + 1;
1258                                 switch (card->l2_proto[a - 1]) {
1259                                         case ISDN_PROTO_L2_X75I:
1260                                                 sprintf(cbuf, "%02d;BCON_R,BX75\n", (int) a);
1261                                                 break;
1262 #ifdef CONFIG_ISDN_X25
1263                                         case ISDN_PROTO_L2_X25DTE:
1264                                                 sprintf(cbuf, "%02d;BCON_R,BX2T\n", (int) a);
1265                                                 break;
1266                                         case ISDN_PROTO_L2_X25DCE:
1267                                                 sprintf(cbuf, "%02d;BCON_R,BX2C\n", (int) a);
1268                                                 break;
1269 #endif
1270                                         case ISDN_PROTO_L2_HDLC:
1271                                                 sprintf(cbuf, "%02d;BCON_R,BTRA\n", (int) a);
1272                                                 break;
1273                                         default:
1274                                                 sprintf(cbuf, "%02d;BCON_R\n", (int) a);
1275                                 }
1276                                 printk(KERN_DEBUG "isdnloop writecmd '%s'\n", cbuf);
1277                                 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1278                                 break;
1279                 case ISDN_CMD_HANGUP:
1280                                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1281                                         return -ENODEV;
1282                                 if (c->arg < ISDNLOOP_BCH) {
1283                                         a = c->arg + 1;
1284                                         sprintf(cbuf, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a, (int) a);
1285                                         i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1286                                 }
1287                                 break;
1288                 case ISDN_CMD_SETEAZ:
1289                                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1290                                         return -ENODEV;
1291                                 if (card->leased)
1292                                         break;
1293                                 if (c->arg < ISDNLOOP_BCH) {
1294                                         a = c->arg + 1;
1295                                         if (card->ptype == ISDN_PTYPE_EURO) {
1296                                                 sprintf(cbuf, "%02d;MS%s%s\n", (int) a,
1297                                                         c->parm.num[0] ? "N" : "ALL", c->parm.num);
1298                                         } else
1299                                                 sprintf(cbuf, "%02d;EAZ%s\n", (int) a,
1300                                                         c->parm.num[0] ? c->parm.num : (u_char *) "0123456789");
1301                                         i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1302                                 }
1303                                 break;
1304                 case ISDN_CMD_CLREAZ:
1305                                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1306                                         return -ENODEV;
1307                                 if (card->leased)
1308                                         break;
1309                                 if (c->arg < ISDNLOOP_BCH) {
1310                                         a = c->arg + 1;
1311                                         if (card->ptype == ISDN_PTYPE_EURO)
1312                                                 sprintf(cbuf, "%02d;MSNC\n", (int) a);
1313                                         else
1314                                                 sprintf(cbuf, "%02d;EAZC\n", (int) a);
1315                                         i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1316                                 }
1317                                 break;
1318                 case ISDN_CMD_SETL2:
1319                                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1320                                         return -ENODEV;
1321                                 if ((c->arg & 255) < ISDNLOOP_BCH) {
1322                                         a = c->arg;
1323                                         switch (a >> 8) {
1324                                                 case ISDN_PROTO_L2_X75I:
1325                                                         sprintf(cbuf, "%02d;BX75\n", (int) (a & 255) + 1);
1326                                                         break;
1327 #ifdef CONFIG_ISDN_X25
1328                                                 case ISDN_PROTO_L2_X25DTE:
1329                                                         sprintf(cbuf, "%02d;BX2T\n", (int) (a & 255) + 1);
1330                                                         break;
1331                                                 case ISDN_PROTO_L2_X25DCE:
1332                                                         sprintf(cbuf, "%02d;BX2C\n", (int) (a & 255) + 1);
1333                                                         break;
1334 #endif
1335                                                 case ISDN_PROTO_L2_HDLC:
1336                                                         sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
1337                                                         break;
1338                                                 case ISDN_PROTO_L2_TRANS:
1339                                                         sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
1340                                                         break;
1341                                                 default:
1342                                                         return -EINVAL;
1343                                         }
1344                                         i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1345                                         card->l2_proto[a & 255] = (a >> 8);
1346                                 }
1347                                 break;
1348                 case ISDN_CMD_SETL3:
1349                                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1350                                         return -ENODEV;
1351                                 return 0;
1352                 default:
1353                                 return -EINVAL;
1354                         }
1355         }
1356         return 0;
1357 }
1358
1359 /*
1360  * Find card with given driverId
1361  */
1362 static inline isdnloop_card *
1363 isdnloop_findcard(int driverid)
1364 {
1365         isdnloop_card *p = cards;
1366
1367         while (p) {
1368                 if (p->myid == driverid)
1369                         return p;
1370                 p = p->next;
1371         }
1372         return (isdnloop_card *) 0;
1373 }
1374
1375 /*
1376  * Wrapper functions for interface to linklevel
1377  */
1378 static int
1379 if_command(isdn_ctrl * c)
1380 {
1381         isdnloop_card *card = isdnloop_findcard(c->driver);
1382
1383         if (card)
1384                 return (isdnloop_command(c, card));
1385         printk(KERN_ERR
1386                "isdnloop: if_command called with invalid driverId!\n");
1387         return -ENODEV;
1388 }
1389
1390 static int
1391 if_writecmd(const u_char __user *buf, int len, int id, int channel)
1392 {
1393         isdnloop_card *card = isdnloop_findcard(id);
1394
1395         if (card) {
1396                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1397                         return -ENODEV;
1398                 return (isdnloop_writecmd(buf, len, 1, card));
1399         }
1400         printk(KERN_ERR
1401                "isdnloop: if_writecmd called with invalid driverId!\n");
1402         return -ENODEV;
1403 }
1404
1405 static int
1406 if_readstatus(u_char __user *buf, int len, int id, int channel)
1407 {
1408         isdnloop_card *card = isdnloop_findcard(id);
1409
1410         if (card) {
1411                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1412                         return -ENODEV;
1413                 return (isdnloop_readstatus(buf, len, card));
1414         }
1415         printk(KERN_ERR
1416                "isdnloop: if_readstatus called with invalid driverId!\n");
1417         return -ENODEV;
1418 }
1419
1420 static int
1421 if_sendbuf(int id, int channel, int ack, struct sk_buff *skb)
1422 {
1423         isdnloop_card *card = isdnloop_findcard(id);
1424
1425         if (card) {
1426                 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1427                         return -ENODEV;
1428                 /* ack request stored in skb scratch area */
1429                 *(skb->head) = ack;
1430                 return (isdnloop_sendbuf(channel, skb, card));
1431         }
1432         printk(KERN_ERR
1433                "isdnloop: if_sendbuf called with invalid driverId!\n");
1434         return -ENODEV;
1435 }
1436
1437 /*
1438  * Allocate a new card-struct, initialize it
1439  * link it into cards-list and register it at linklevel.
1440  */
1441 static isdnloop_card *
1442 isdnloop_initcard(char *id)
1443 {
1444         isdnloop_card *card;
1445         int i;
1446
1447         if (!(card = (isdnloop_card *) kmalloc(sizeof(isdnloop_card), GFP_KERNEL))) {
1448                 printk(KERN_WARNING
1449                  "isdnloop: (%s) Could not allocate card-struct.\n", id);
1450                 return (isdnloop_card *) 0;
1451         }
1452         memset((char *) card, 0, sizeof(isdnloop_card));
1453         card->interface.owner = THIS_MODULE;
1454         card->interface.channels = ISDNLOOP_BCH;
1455         card->interface.hl_hdrlen  = 1; /* scratch area for storing ack flag*/ 
1456         card->interface.maxbufsize = 4000;
1457         card->interface.command = if_command;
1458         card->interface.writebuf_skb = if_sendbuf;
1459         card->interface.writecmd = if_writecmd;
1460         card->interface.readstat = if_readstatus;
1461         card->interface.features = ISDN_FEATURE_L2_X75I |
1462 #ifdef CONFIG_ISDN_X25
1463             ISDN_FEATURE_L2_X25DTE |
1464             ISDN_FEATURE_L2_X25DCE |
1465 #endif
1466             ISDN_FEATURE_L2_HDLC |
1467             ISDN_FEATURE_L3_TRANS |
1468             ISDN_FEATURE_P_UNKNOWN;
1469         card->ptype = ISDN_PTYPE_UNKNOWN;
1470         strlcpy(card->interface.id, id, sizeof(card->interface.id));
1471         card->msg_buf_write = card->msg_buf;
1472         card->msg_buf_read = card->msg_buf;
1473         card->msg_buf_end = &card->msg_buf[sizeof(card->msg_buf) - 1];
1474         for (i = 0; i < ISDNLOOP_BCH; i++) {
1475                 card->l2_proto[i] = ISDN_PROTO_L2_X75I;
1476                 skb_queue_head_init(&card->bqueue[i]);
1477         }
1478         skb_queue_head_init(&card->dqueue);
1479         card->next = cards;
1480         cards = card;
1481         if (!register_isdn(&card->interface)) {
1482                 cards = cards->next;
1483                 printk(KERN_WARNING
1484                        "isdnloop: Unable to register %s\n", id);
1485                 kfree(card);
1486                 return (isdnloop_card *) 0;
1487         }
1488         card->myid = card->interface.channels;
1489         return card;
1490 }
1491
1492 static int
1493 isdnloop_addcard(char *id1)
1494 {
1495         isdnloop_card *card;
1496
1497         if (!(card = isdnloop_initcard(id1))) {
1498                 return -EIO;
1499         }
1500         printk(KERN_INFO
1501                "isdnloop: (%s) virtual card added\n",
1502                card->interface.id);
1503         return 0;
1504 }
1505
1506 static int __init
1507 isdnloop_init(void)
1508 {
1509         char *p;
1510         char rev[10];
1511
1512         if ((p = strchr(revision, ':'))) {
1513                 strcpy(rev, p + 1);
1514                 p = strchr(rev, '$');
1515                 *p = 0;
1516         } else
1517                 strcpy(rev, " ??? ");
1518         printk(KERN_NOTICE "isdnloop-ISDN-driver Rev%s\n", rev);
1519
1520         if (isdnloop_id)
1521                 return (isdnloop_addcard(isdnloop_id));
1522
1523         return 0;
1524 }
1525
1526 static void __exit
1527 isdnloop_exit(void)
1528 {
1529         isdn_ctrl cmd;
1530         isdnloop_card *card = cards;
1531         isdnloop_card *last;
1532         int i;
1533
1534         isdnloop_stopallcards();
1535         while (card) {
1536                 cmd.command = ISDN_STAT_UNLOAD;
1537                 cmd.driver = card->myid;
1538                 card->interface.statcallb(&cmd);
1539                 for (i = 0; i < ISDNLOOP_BCH; i++)
1540                         isdnloop_free_queue(card, i);
1541                 card = card->next;
1542         }
1543         card = cards;
1544         while (card) {
1545                 last = card;
1546                 skb_queue_purge(&card->dqueue);
1547                 card = card->next;
1548                 kfree(last);
1549         }
1550         printk(KERN_NOTICE "isdnloop-ISDN-driver unloaded\n");
1551 }
1552
1553 module_init(isdnloop_init);
1554 module_exit(isdnloop_exit);