OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [isdn/] [isdnloop/] [isdnloop.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.