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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [uclinux/] [uClinux-2.0.x/] [drivers/] [isdn/] [isdnloop/] [isdnloop.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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