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