1 /* $Id: isdnloop.c,v 1.11.6.7 2001/11/11 19:54:31 kai Exp $
3 * ISDN low-level module implementing a dummy loop driver.
5 * Copyright 1997 by Fritz Elfert (fritz@isdn4linux.de)
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
12 #include <linux/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/init.h>
15 #include <linux/sched.h>
18 static char *revision
= "$Revision: 1.11.6.7 $";
19 static char *isdnloop_id
= "loop0";
21 MODULE_DESCRIPTION("ISDN4Linux: Pseudo Driver that simulates an ISDN card");
22 MODULE_AUTHOR("Fritz Elfert");
23 MODULE_LICENSE("GPL");
24 module_param(isdnloop_id
, charp
, 0);
25 MODULE_PARM_DESC(isdnloop_id
, "ID-String of first card");
27 static int isdnloop_addcard(char *);
30 * Free queue completely.
33 * card = pointer to card struct
34 * channel = channel number
37 isdnloop_free_queue(isdnloop_card
* card
, int channel
)
39 struct sk_buff_head
*queue
= &card
->bqueue
[channel
];
41 skb_queue_purge(queue
);
42 card
->sndcount
[channel
] = 0;
46 * Send B-Channel data to another virtual card.
47 * This routine is called via timer-callback from isdnloop_pollbchan().
50 * card = pointer to card struct.
51 * ch = channel number (0-based)
54 isdnloop_bchan_send(isdnloop_card
* card
, int ch
)
56 isdnloop_card
*rcard
= card
->rcard
[ch
];
57 int rch
= card
->rch
[ch
], len
, ack
;
61 while (card
->sndcount
[ch
]) {
62 if ((skb
= skb_dequeue(&card
->bqueue
[ch
]))) {
64 card
->sndcount
[ch
] -= len
;
65 ack
= *(skb
->head
); /* used as scratch area */
66 cmd
.driver
= card
->myid
;
69 rcard
->interface
.rcvcallb_skb(rcard
->myid
, rch
, skb
);
71 printk(KERN_WARNING
"isdnloop: no rcard, skb dropped\n");
75 cmd
.command
= ISDN_STAT_BSENT
;
76 cmd
.parm
.length
= len
;
77 card
->interface
.statcallb(&cmd
);
79 card
->sndcount
[ch
] = 0;
84 * Send/Receive Data to/from the B-Channel.
85 * This routine is called via timer-callback.
86 * It schedules itself while any B-Channel is open.
89 * data = pointer to card struct, set by kernel timer.data
92 isdnloop_pollbchan(unsigned long data
)
94 isdnloop_card
*card
= (isdnloop_card
*) data
;
97 if (card
->flags
& ISDNLOOP_FLAGS_B1ACTIVE
)
98 isdnloop_bchan_send(card
, 0);
99 if (card
->flags
& ISDNLOOP_FLAGS_B2ACTIVE
)
100 isdnloop_bchan_send(card
, 1);
101 if (card
->flags
& (ISDNLOOP_FLAGS_B1ACTIVE
| ISDNLOOP_FLAGS_B2ACTIVE
)) {
102 /* schedule b-channel polling again */
105 card
->rb_timer
.expires
= jiffies
+ ISDNLOOP_TIMER_BCREAD
;
106 add_timer(&card
->rb_timer
);
107 card
->flags
|= ISDNLOOP_FLAGS_RBTIMER
;
108 restore_flags(flags
);
110 card
->flags
&= ~ISDNLOOP_FLAGS_RBTIMER
;
114 * Parse ICN-type setup string and fill fields of setup-struct
118 * setup = setup string, format: [caller-id],si1,si2,[called-id]
119 * cmd = pointer to struct to be filled.
122 isdnloop_parse_setup(char *setup
, isdn_ctrl
* cmd
)
125 char *s
= strchr(t
, ',');
128 strlcpy(cmd
->parm
.setup
.phone
, t
, sizeof(cmd
->parm
.setup
.phone
));
129 s
= strchr(t
= s
, ',');
132 cmd
->parm
.setup
.si1
= 0;
134 cmd
->parm
.setup
.si1
= simple_strtoul(t
, NULL
, 10);
135 s
= strchr(t
= s
, ',');
138 cmd
->parm
.setup
.si2
= 0;
140 cmd
->parm
.setup
.si2
=
141 simple_strtoul(t
, NULL
, 10);
142 strlcpy(cmd
->parm
.setup
.eazmsn
, s
, sizeof(cmd
->parm
.setup
.eazmsn
));
143 cmd
->parm
.setup
.plan
= 0;
144 cmd
->parm
.setup
.screen
= 0;
147 typedef struct isdnloop_stat
{
153 static isdnloop_stat isdnloop_stat_table
[] =
155 {"BCON_", ISDN_STAT_BCONN
, 1}, /* B-Channel connected */
156 {"BDIS_", ISDN_STAT_BHUP
, 2}, /* B-Channel disconnected */
157 {"DCON_", ISDN_STAT_DCONN
, 0}, /* D-Channel connected */
158 {"DDIS_", ISDN_STAT_DHUP
, 0}, /* D-Channel disconnected */
159 {"DCAL_I", ISDN_STAT_ICALL
, 3}, /* Incoming call dialup-line */
160 {"DSCA_I", ISDN_STAT_ICALL
, 3}, /* Incoming call 1TR6-SPV */
161 {"FCALL", ISDN_STAT_ICALL
, 4}, /* Leased line connection up */
162 {"CIF", ISDN_STAT_CINF
, 5}, /* Charge-info, 1TR6-type */
163 {"AOC", ISDN_STAT_CINF
, 6}, /* Charge-info, DSS1-type */
164 {"CAU", ISDN_STAT_CAUSE
, 7}, /* Cause code */
165 {"TEI OK", ISDN_STAT_RUN
, 0}, /* Card connected to wallplug */
166 {"E_L1: ACT FAIL", ISDN_STAT_BHUP
, 8}, /* Layer-1 activation failed */
167 {"E_L2: DATA LIN", ISDN_STAT_BHUP
, 8}, /* Layer-2 data link lost */
168 {"E_L1: ACTIVATION FAILED",
169 ISDN_STAT_BHUP
, 8}, /* Layer-1 activation failed */
176 * Parse Status message-strings from virtual card.
177 * Depending on status, call statcallb for sending messages to upper
178 * levels. Also set/reset B-Channel active-flags.
181 * status = status string to parse.
182 * channel = channel where message comes from.
183 * card = card where message comes from.
186 isdnloop_parse_status(u_char
* status
, int channel
, isdnloop_card
* card
)
188 isdnloop_stat
*s
= isdnloop_stat_table
;
193 if (!strncmp(status
, s
->statstr
, strlen(s
->statstr
))) {
194 cmd
.command
= s
->command
;
202 cmd
.driver
= card
->myid
;
207 card
->flags
|= (channel
) ?
208 ISDNLOOP_FLAGS_B2ACTIVE
: ISDNLOOP_FLAGS_B1ACTIVE
;
212 card
->flags
&= ~((channel
) ?
213 ISDNLOOP_FLAGS_B2ACTIVE
: ISDNLOOP_FLAGS_B1ACTIVE
);
214 isdnloop_free_queue(card
, channel
);
217 /* DCAL_I and DSCA_I */
218 isdnloop_parse_setup(status
+ 6, &cmd
);
222 sprintf(cmd
.parm
.setup
.phone
, "LEASED%d", card
->myid
);
223 sprintf(cmd
.parm
.setup
.eazmsn
, "%d", channel
+ 1);
224 cmd
.parm
.setup
.si1
= 7;
225 cmd
.parm
.setup
.si2
= 0;
226 cmd
.parm
.setup
.plan
= 0;
227 cmd
.parm
.setup
.screen
= 0;
231 strlcpy(cmd
.parm
.num
, status
+ 3, sizeof(cmd
.parm
.num
));
235 snprintf(cmd
.parm
.num
, sizeof(cmd
.parm
.num
), "%d",
236 (int) simple_strtoul(status
+ 7, NULL
, 16));
241 if (strlen(status
) == 4)
242 snprintf(cmd
.parm
.num
, sizeof(cmd
.parm
.num
), "%s%c%c",
243 status
+ 2, *status
, *(status
+ 1));
245 strlcpy(cmd
.parm
.num
, status
+ 1, sizeof(cmd
.parm
.num
));
248 /* Misc Errors on L1 and L2 */
249 card
->flags
&= ~ISDNLOOP_FLAGS_B1ACTIVE
;
250 isdnloop_free_queue(card
, 0);
252 cmd
.driver
= card
->myid
;
253 card
->interface
.statcallb(&cmd
);
254 cmd
.command
= ISDN_STAT_DHUP
;
256 cmd
.driver
= card
->myid
;
257 card
->interface
.statcallb(&cmd
);
258 cmd
.command
= ISDN_STAT_BHUP
;
259 card
->flags
&= ~ISDNLOOP_FLAGS_B2ACTIVE
;
260 isdnloop_free_queue(card
, 1);
262 cmd
.driver
= card
->myid
;
263 card
->interface
.statcallb(&cmd
);
264 cmd
.command
= ISDN_STAT_DHUP
;
266 cmd
.driver
= card
->myid
;
269 card
->interface
.statcallb(&cmd
);
273 * Store a cwcharacter into ringbuffer for reading from /dev/isdnctrl
276 * card = pointer to card struct.
280 isdnloop_putmsg(isdnloop_card
* card
, unsigned char c
)
286 *card
->msg_buf_write
++ = (c
== 0xff) ? '\n' : c
;
287 if (card
->msg_buf_write
== card
->msg_buf_read
) {
288 if (++card
->msg_buf_read
> card
->msg_buf_end
)
289 card
->msg_buf_read
= card
->msg_buf
;
291 if (card
->msg_buf_write
> card
->msg_buf_end
)
292 card
->msg_buf_write
= card
->msg_buf
;
293 restore_flags(flags
);
297 * Poll a virtual cards message queue.
298 * If there are new status-replies from the card, copy them to
299 * ringbuffer for reading on /dev/isdnctrl and call
300 * isdnloop_parse_status() for processing them. Watch for special
301 * Firmware bootmessage and parse it, to get the D-Channel protocol.
302 * If there are B-Channels open, initiate a timer-callback to
303 * isdnloop_pollbchan().
304 * This routine is called periodically via timer interrupt.
307 * data = pointer to card struct
310 isdnloop_polldchan(unsigned long data
)
312 isdnloop_card
*card
= (isdnloop_card
*) data
;
322 if ((skb
= skb_dequeue(&card
->dqueue
)))
326 for (left
= avail
; left
> 0; left
--) {
329 isdnloop_putmsg(card
, c
);
330 card
->imsg
[card
->iptr
] = c
;
335 isdnloop_putmsg(card
, '\n');
336 card
->imsg
[card
->iptr
] = 0;
338 if (card
->imsg
[0] == '0' && card
->imsg
[1] >= '0' &&
339 card
->imsg
[1] <= '2' && card
->imsg
[2] == ';') {
340 ch
= (card
->imsg
[1] - '0') - 1;
342 isdnloop_parse_status(p
, ch
, card
);
345 if (!strncmp(p
, "DRV1.", 5)) {
346 printk(KERN_INFO
"isdnloop: (%s) %s\n", CID
, p
);
347 if (!strncmp(p
+ 7, "TC", 2)) {
348 card
->ptype
= ISDN_PTYPE_1TR6
;
349 card
->interface
.features
|= ISDN_FEATURE_P_1TR6
;
351 "isdnloop: (%s) 1TR6-Protocol loaded and running\n", CID
);
353 if (!strncmp(p
+ 7, "EC", 2)) {
354 card
->ptype
= ISDN_PTYPE_EURO
;
355 card
->interface
.features
|= ISDN_FEATURE_P_EURO
;
357 "isdnloop: (%s) Euro-Protocol loaded and running\n", CID
);
366 cmd
.command
= ISDN_STAT_STAVAIL
;
367 cmd
.driver
= card
->myid
;
369 card
->interface
.statcallb(&cmd
);
371 if (card
->flags
& (ISDNLOOP_FLAGS_B1ACTIVE
| ISDNLOOP_FLAGS_B2ACTIVE
))
372 if (!(card
->flags
& ISDNLOOP_FLAGS_RBTIMER
)) {
373 /* schedule b-channel polling */
374 card
->flags
|= ISDNLOOP_FLAGS_RBTIMER
;
377 del_timer(&card
->rb_timer
);
378 card
->rb_timer
.function
= isdnloop_pollbchan
;
379 card
->rb_timer
.data
= (unsigned long) card
;
380 card
->rb_timer
.expires
= jiffies
+ ISDNLOOP_TIMER_BCREAD
;
381 add_timer(&card
->rb_timer
);
382 restore_flags(flags
);
387 card
->st_timer
.expires
= jiffies
+ ISDNLOOP_TIMER_DCREAD
;
388 add_timer(&card
->st_timer
);
389 restore_flags(flags
);
393 * Append a packet to the transmit buffer-queue.
396 * channel = Number of B-channel
397 * skb = packet to send.
398 * card = pointer to card-struct
400 * Number of bytes transferred, -E??? on error
403 isdnloop_sendbuf(int channel
, struct sk_buff
*skb
, isdnloop_card
* card
)
407 struct sk_buff
*nskb
;
411 "isdnloop: Send packet too large\n");
415 if (!(card
->flags
& (channel
) ? ISDNLOOP_FLAGS_B2ACTIVE
: ISDNLOOP_FLAGS_B1ACTIVE
))
417 if (card
->sndcount
[channel
] > ISDNLOOP_MAX_SQUEUE
)
421 nskb
= dev_alloc_skb(skb
->len
);
423 memcpy(skb_put(nskb
, len
), skb
->data
, len
);
424 skb_queue_tail(&card
->bqueue
[channel
], nskb
);
428 card
->sndcount
[channel
] += len
;
429 restore_flags(flags
);
435 * Read the messages from the card's ringbuffer
438 * buf = pointer to buffer.
439 * len = number of bytes to read.
440 * user = flag, 1: called from userlevel 0: called from kernel.
441 * card = pointer to card struct.
443 * number of bytes actually transferred.
446 isdnloop_readstatus(u_char __user
*buf
, int len
, isdnloop_card
* card
)
451 for (p
= buf
, count
= 0; count
< len
; p
++, count
++) {
452 if (card
->msg_buf_read
== card
->msg_buf_write
)
454 put_user(*card
->msg_buf_read
++, p
);
455 if (card
->msg_buf_read
> card
->msg_buf_end
)
456 card
->msg_buf_read
= card
->msg_buf
;
462 * Simulate a card's response by appending it to the cards
466 * card = pointer to card struct.
467 * s = pointer to message-string.
468 * ch = channel: 0 = generic messages, 1 and 2 = D-channel messages.
470 * 0 on success, 1 on memory squeeze.
473 isdnloop_fake(isdnloop_card
* card
, char *s
, int ch
)
476 int len
= strlen(s
) + ((ch
>= 0) ? 3 : 0);
478 if (!(skb
= dev_alloc_skb(len
))) {
479 printk(KERN_WARNING
"isdnloop: Out of memory in isdnloop_fake\n");
483 sprintf(skb_put(skb
, 3), "%02d;", ch
);
484 memcpy(skb_put(skb
, strlen(s
)), s
, strlen(s
));
485 skb_queue_tail(&card
->dqueue
, skb
);
489 static isdnloop_stat isdnloop_cmd_table
[] =
491 {"BCON_R", 0, 1}, /* B-Channel connect */
492 {"BCON_I", 0, 17}, /* B-Channel connect ind */
493 {"BDIS_R", 0, 2}, /* B-Channel disconnect */
494 {"DDIS_R", 0, 3}, /* D-Channel disconnect */
495 {"DCON_R", 0, 16}, /* D-Channel connect */
496 {"DSCA_R", 0, 4}, /* Dial 1TR6-SPV */
497 {"DCAL_R", 0, 5}, /* Dial */
498 {"EAZC", 0, 6}, /* Clear EAZ listener */
499 {"EAZ", 0, 7}, /* Set EAZ listener */
500 {"SEEAZ", 0, 8}, /* Get EAZ listener */
501 {"MSN", 0, 9}, /* Set/Clear MSN listener */
502 {"MSALL", 0, 10}, /* Set multi MSN listeners */
503 {"SETSIL", 0, 11}, /* Set SI list */
504 {"SEESIL", 0, 12}, /* Get SI list */
505 {"SILC", 0, 13}, /* Clear SI list */
506 {"LOCK", 0, -1}, /* LOCK channel */
507 {"UNLOCK", 0, -1}, /* UNLOCK channel */
508 {"FV2ON", 1, 14}, /* Leased mode on */
509 {"FV2OFF", 1, 15}, /* Leased mode off */
516 * Simulate an error-response from a card.
519 * card = pointer to card struct.
522 isdnloop_fake_err(isdnloop_card
* card
)
526 sprintf(buf
, "E%s", card
->omsg
);
527 isdnloop_fake(card
, buf
, -1);
528 isdnloop_fake(card
, "NAK", -1);
531 static u_char ctable_eu
[] =
532 {0x00, 0x11, 0x01, 0x12};
533 static u_char ctable_1t
[] =
534 {0x00, 0x3b, 0x01, 0x3a};
537 * Assemble a simplified cause message depending on the
538 * D-channel protocol used.
541 * card = pointer to card struct.
542 * loc = location: 0 = local, 1 = remote.
543 * cau = cause: 1 = busy, 2 = nonexistent callerid, 3 = no user responding.
545 * Pointer to buffer containing the assembled message.
548 isdnloop_unicause(isdnloop_card
* card
, int loc
, int cau
)
552 switch (card
->ptype
) {
553 case ISDN_PTYPE_EURO
:
554 sprintf(buf
, "E%02X%02X", (loc
) ? 4 : 2, ctable_eu
[cau
]);
556 case ISDN_PTYPE_1TR6
:
557 sprintf(buf
, "%02X44", ctable_1t
[cau
]);
566 * Release a virtual connection. Called from timer interrupt, when
567 * called party did not respond.
570 * card = pointer to card struct.
571 * ch = channel (0-based)
574 isdnloop_atimeout(isdnloop_card
* card
, int ch
)
582 isdnloop_fake(card
->rcard
[ch
], "DDIS_I", card
->rch
[ch
] + 1);
583 card
->rcard
[ch
]->rcard
[card
->rch
[ch
]] = NULL
;
584 card
->rcard
[ch
] = NULL
;
586 isdnloop_fake(card
, "DDIS_I", ch
+ 1);
587 /* No user responding */
588 sprintf(buf
, "CAU%s", isdnloop_unicause(card
, 1, 3));
589 isdnloop_fake(card
, buf
, ch
+ 1);
590 restore_flags(flags
);
594 * Wrapper for isdnloop_atimeout().
597 isdnloop_atimeout0(unsigned long data
)
599 isdnloop_card
*card
= (isdnloop_card
*) data
;
600 isdnloop_atimeout(card
, 0);
604 * Wrapper for isdnloop_atimeout().
607 isdnloop_atimeout1(unsigned long data
)
609 isdnloop_card
*card
= (isdnloop_card
*) data
;
610 isdnloop_atimeout(card
, 1);
614 * Install a watchdog for a user, not responding.
617 * card = pointer to card struct.
618 * ch = channel to watch for.
621 isdnloop_start_ctimer(isdnloop_card
* card
, int ch
)
627 init_timer(&card
->c_timer
[ch
]);
628 card
->c_timer
[ch
].expires
= jiffies
+ ISDNLOOP_TIMER_ALERTWAIT
;
630 card
->c_timer
[ch
].function
= isdnloop_atimeout1
;
632 card
->c_timer
[ch
].function
= isdnloop_atimeout0
;
633 card
->c_timer
[ch
].data
= (unsigned long) card
;
634 add_timer(&card
->c_timer
[ch
]);
635 restore_flags(flags
);
639 * Kill a pending channel watchdog.
642 * card = pointer to card struct.
643 * ch = channel (0-based).
646 isdnloop_kill_ctimer(isdnloop_card
* card
, int ch
)
652 del_timer(&card
->c_timer
[ch
]);
653 restore_flags(flags
);
656 static u_char si2bit
[] =
657 {0, 1, 0, 0, 0, 2, 0, 4, 0, 0};
658 static u_char bit2si
[] =
662 * Try finding a listener for an outgoing call.
665 * card = pointer to calling card.
666 * p = pointer to ICN-type setup-string.
667 * lch = channel of calling card.
668 * cmd = pointer to struct to be filled when parsing setup.
670 * 0 = found match, alerting should happen.
671 * 1 = found matching number but it is busy.
672 * 2 = no matching listener.
673 * 3 = found matching number but SI does not match.
676 isdnloop_try_call(isdnloop_card
* card
, char *p
, int lch
, isdn_ctrl
* cmd
)
678 isdnloop_card
*cc
= cards
;
686 isdnloop_parse_setup(p
, cmd
);
688 for (ch
= 0; ch
< 2; ch
++) {
689 /* Exclude ourself */
690 if ((cc
== card
) && (ch
== lch
))
694 case ISDN_PTYPE_EURO
:
695 for (i
= 0; i
< 3; i
++)
696 if (!(strcmp(cc
->s0num
[i
], cmd
->parm
.setup
.phone
)))
699 case ISDN_PTYPE_1TR6
:
702 sprintf(nbuf
, "%s%c", cc
->s0num
[0], *e
);
703 if (!(strcmp(nbuf
, cmd
->parm
.setup
.phone
)))
712 if (!(cc
->rcard
[ch
])) {
714 if (!(si2bit
[cmd
->parm
.setup
.si1
] & cc
->sil
[ch
])) {
715 restore_flags(flags
);
718 /* ch is idle, si and number matches */
719 cc
->rcard
[ch
] = card
;
721 card
->rcard
[lch
] = cc
;
723 restore_flags(flags
);
726 restore_flags(flags
);
727 /* num matches, but busy */
739 * Depending on D-channel protocol and caller/called, modify
743 * card = pointer to card struct.
744 * phone = pointer phone number.
745 * caller = flag: 1 = caller, 0 = called.
747 * pointer to new phone number.
750 isdnloop_vstphone(isdnloop_card
* card
, char *phone
, int caller
)
753 static char nphone
[30];
759 switch (card
->ptype
) {
760 case ISDN_PTYPE_EURO
:
762 for (i
= 0; i
< 2; i
++)
763 if (!(strcmp(card
->s0num
[i
], phone
)))
765 return (card
->s0num
[0]);
769 case ISDN_PTYPE_1TR6
:
771 sprintf(nphone
, "%s%c", card
->s0num
[0], phone
[0]);
774 return (&phone
[strlen(phone
) - 1]);
781 * Parse an ICN-type command string sent to the 'card'.
782 * Perform misc. actions depending on the command.
785 * card = pointer to card struct.
788 isdnloop_parse_cmd(isdnloop_card
* card
)
790 char *p
= card
->omsg
;
793 isdnloop_stat
*s
= isdnloop_cmd_table
;
798 if ((card
->omsg
[0] != '0') && (card
->omsg
[2] != ';')) {
799 isdnloop_fake_err(card
);
802 ch
= card
->omsg
[1] - '0';
803 if ((ch
< 0) || (ch
> 2)) {
804 isdnloop_fake_err(card
);
809 if (!strncmp(p
, s
->statstr
, strlen(s
->statstr
))) {
811 if (s
->command
&& (ch
!= 0)) {
812 isdnloop_fake_err(card
);
824 if (card
->rcard
[ch
- 1]) {
825 isdnloop_fake(card
->rcard
[ch
- 1], "BCON_I",
826 card
->rch
[ch
- 1] + 1);
827 isdnloop_fake(card
, "BCON_C", ch
);
832 if (card
->rcard
[ch
- 1]) {
833 isdnloop_fake(card
->rcard
[ch
- 1], "BCON_C",
834 card
->rch
[ch
- 1] + 1);
839 isdnloop_fake(card
, "BDIS_C", ch
);
840 if (card
->rcard
[ch
- 1]) {
841 isdnloop_fake(card
->rcard
[ch
- 1], "BDIS_I",
842 card
->rch
[ch
- 1] + 1);
847 isdnloop_kill_ctimer(card
, ch
- 1);
848 if (card
->rcard
[ch
- 1]) {
849 isdnloop_kill_ctimer(card
->rcard
[ch
- 1], card
->rch
[ch
- 1]);
850 isdnloop_fake(card
->rcard
[ch
- 1], "DCON_C",
851 card
->rch
[ch
- 1] + 1);
852 isdnloop_fake(card
, "DCON_C", ch
);
857 isdnloop_kill_ctimer(card
, ch
- 1);
858 if (card
->rcard
[ch
- 1]) {
859 isdnloop_kill_ctimer(card
->rcard
[ch
- 1], card
->rch
[ch
- 1]);
860 isdnloop_fake(card
->rcard
[ch
- 1], "DDIS_I",
861 card
->rch
[ch
- 1] + 1);
862 card
->rcard
[ch
- 1] = NULL
;
864 isdnloop_fake(card
, "DDIS_C", ch
);
867 /* 0x;DSCA_Rdd,yy,zz,oo */
868 if (card
->ptype
!= ISDN_PTYPE_1TR6
) {
869 isdnloop_fake_err(card
);
874 /* 0x;DCAL_Rdd,yy,zz,oo */
876 switch (isdnloop_try_call(card
, p
, ch
- 1, &cmd
)) {
879 sprintf(buf
, "D%s_I%s,%02d,%02d,%s",
880 (action
== 4) ? "SCA" : "CAL",
881 isdnloop_vstphone(card
, cmd
.parm
.setup
.eazmsn
, 1),
884 isdnloop_vstphone(card
->rcard
[ch
- 1],
885 cmd
.parm
.setup
.phone
, 0));
886 isdnloop_fake(card
->rcard
[ch
- 1], buf
, card
->rch
[ch
- 1] + 1);
889 /* si1 does not match, don't alert but start timer */
890 isdnloop_start_ctimer(card
, ch
- 1);
894 isdnloop_fake(card
, "DDIS_I", ch
);
895 sprintf(buf
, "CAU%s", isdnloop_unicause(card
, 1, 1));
896 isdnloop_fake(card
, buf
, ch
);
900 isdnloop_fake(card
, "DDIS_I", ch
);
901 sprintf(buf
, "CAU%s", isdnloop_unicause(card
, 1, 2));
902 isdnloop_fake(card
, buf
, ch
);
908 card
->eazlist
[ch
- 1][0] = '\0';
913 strcpy(card
->eazlist
[ch
- 1], p
);
917 sprintf(buf
, "EAZ-LIST: %s", card
->eazlist
[ch
- 1]);
918 isdnloop_fake(card
, buf
, ch
+ 1);
930 while (strchr("0157", *p
)) {
932 card
->sil
[ch
- 1] |= si2bit
[*p
- '0'];
936 isdnloop_fake_err(card
);
940 sprintf(buf
, "SIN-LIST: ");
942 for (i
= 0; i
< 3; i
++)
943 if (card
->sil
[ch
- 1] & (1 << i
))
944 p
+= sprintf(p
, "%02d", bit2si
[i
]);
945 isdnloop_fake(card
, buf
, ch
+ 1);
949 card
->sil
[ch
- 1] = 0;
961 * Put command-strings into the of the 'card'. In reality, execute them
962 * right in place by calling isdnloop_parse_cmd(). Also copy every
963 * command to the read message ringbuffer, preceeding it with a '>'.
964 * These mesagges can be read at /dev/isdnctrl.
967 * buf = pointer to command buffer.
968 * len = length of buffer data.
969 * user = flag: 1 = called form userlevel, 0 called from kernel.
970 * card = pointer to card struct.
972 * number of bytes transferred (currently always equals len).
975 isdnloop_writecmd(const u_char
* buf
, int len
, int user
, isdnloop_card
* card
)
989 if (copy_from_user(msg
, buf
, count
))
992 memcpy(msg
, buf
, count
);
993 isdnloop_putmsg(card
, '>');
994 for (p
= msg
; count
> 0; count
--, p
++) {
997 isdnloop_putmsg(card
, *p
);
998 card
->omsg
[card
->optr
] = *p
;
1000 card
->omsg
[card
->optr
] = '\0';
1002 isdnloop_parse_cmd(card
);
1004 isdnloop_putmsg(card
, '>');
1008 if (card
->optr
< 59)
1014 cmd
.command
= ISDN_STAT_STAVAIL
;
1015 cmd
.driver
= card
->myid
;
1017 card
->interface
.statcallb(&cmd
);
1022 * Delete card's pending timers, send STOP to linklevel
1025 isdnloop_stopcard(isdnloop_card
* card
)
1027 unsigned long flags
;
1032 if (card
->flags
& ISDNLOOP_FLAGS_RUNNING
) {
1033 card
->flags
&= ~ISDNLOOP_FLAGS_RUNNING
;
1034 del_timer(&card
->st_timer
);
1035 del_timer(&card
->rb_timer
);
1036 del_timer(&card
->c_timer
[0]);
1037 del_timer(&card
->c_timer
[1]);
1038 cmd
.command
= ISDN_STAT_STOP
;
1039 cmd
.driver
= card
->myid
;
1040 card
->interface
.statcallb(&cmd
);
1042 restore_flags(flags
);
1046 * Stop all cards before unload.
1049 isdnloop_stopallcards(void)
1051 isdnloop_card
*p
= cards
;
1054 isdnloop_stopcard(p
);
1060 * Start a 'card'. Simulate card's boot message and set the phone
1061 * number(s) of the virtual 'S0-Interface'. Install D-channel
1065 * card = pointer to card struct.
1066 * sdefp = pointer to struct holding ioctl parameters.
1068 * 0 on success, -E??? otherwise.
1071 isdnloop_start(isdnloop_card
* card
, isdnloop_sdef
* sdefp
)
1073 unsigned long flags
;
1077 if (card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1079 if (copy_from_user((char *) &sdef
, (char *) sdefp
, sizeof(sdef
)))
1083 switch (sdef
.ptype
) {
1084 case ISDN_PTYPE_EURO
:
1085 if (isdnloop_fake(card
, "DRV1.23EC-Q.931-CAPI-CNS-BASIS-20.02.96",
1087 restore_flags(flags
);
1090 card
->sil
[0] = card
->sil
[1] = 4;
1091 if (isdnloop_fake(card
, "TEI OK", 0)) {
1092 restore_flags(flags
);
1095 for (i
= 0; i
< 3; i
++)
1096 strcpy(card
->s0num
[i
], sdef
.num
[i
]);
1098 case ISDN_PTYPE_1TR6
:
1099 if (isdnloop_fake(card
, "DRV1.04TC-1TR6-CAPI-CNS-BASIS-29.11.95",
1101 restore_flags(flags
);
1104 card
->sil
[0] = card
->sil
[1] = 4;
1105 if (isdnloop_fake(card
, "TEI OK", 0)) {
1106 restore_flags(flags
);
1109 strcpy(card
->s0num
[0], sdef
.num
[0]);
1110 card
->s0num
[1][0] = '\0';
1111 card
->s0num
[2][0] = '\0';
1114 restore_flags(flags
);
1115 printk(KERN_WARNING
"isdnloop: Illegal D-channel protocol %d\n",
1119 init_timer(&card
->st_timer
);
1120 card
->st_timer
.expires
= jiffies
+ ISDNLOOP_TIMER_DCREAD
;
1121 card
->st_timer
.function
= isdnloop_polldchan
;
1122 card
->st_timer
.data
= (unsigned long) card
;
1123 add_timer(&card
->st_timer
);
1124 card
->flags
|= ISDNLOOP_FLAGS_RUNNING
;
1125 restore_flags(flags
);
1130 * Main handler for commands sent by linklevel.
1133 isdnloop_command(isdn_ctrl
* c
, isdnloop_card
* card
)
1141 switch (c
->command
) {
1142 case ISDN_CMD_IOCTL
:
1143 memcpy(&a
, c
->parm
.num
, sizeof(ulong
));
1145 case ISDNLOOP_IOCTL_DEBUGVAR
:
1146 return (ulong
) card
;
1147 case ISDNLOOP_IOCTL_STARTUP
:
1148 if (!access_ok(VERIFY_READ
, (void *) a
, sizeof(isdnloop_sdef
)))
1150 return (isdnloop_start(card
, (isdnloop_sdef
*) a
));
1152 case ISDNLOOP_IOCTL_ADDCARD
:
1153 if (copy_from_user((char *)&cdef
,
1157 return (isdnloop_addcard(cdef
.id1
));
1159 case ISDNLOOP_IOCTL_LEASEDCFG
:
1161 if (!card
->leased
) {
1163 while (card
->ptype
== ISDN_PTYPE_UNKNOWN
)
1164 schedule_timeout_interruptible(10);
1165 schedule_timeout_interruptible(10);
1166 sprintf(cbuf
, "00;FV2ON\n01;EAZ1\n02;EAZ2\n");
1167 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1169 "isdnloop: (%s) Leased-line mode enabled\n",
1171 cmd
.command
= ISDN_STAT_RUN
;
1172 cmd
.driver
= card
->myid
;
1174 card
->interface
.statcallb(&cmd
);
1179 sprintf(cbuf
, "00;FV2OFF\n");
1180 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1182 "isdnloop: (%s) Leased-line mode disabled\n",
1184 cmd
.command
= ISDN_STAT_RUN
;
1185 cmd
.driver
= card
->myid
;
1187 card
->interface
.statcallb(&cmd
);
1196 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1200 if ((c
->arg
& 255) < ISDNLOOP_BCH
) {
1206 p
= c
->parm
.setup
.phone
;
1207 if (*p
== 's' || *p
== 'S') {
1210 strcpy(dcode
, "SCA");
1213 strcpy(dcode
, "CAL");
1215 sprintf(cbuf
, "%02d;D%s_R%s,%02d,%02d,%s\n", (int) (a
+ 1),
1216 dcode
, dial
, c
->parm
.setup
.si1
,
1217 c
->parm
.setup
.si2
, c
->parm
.setup
.eazmsn
);
1218 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1221 case ISDN_CMD_ACCEPTD
:
1222 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1224 if (c
->arg
< ISDNLOOP_BCH
) {
1227 switch (card
->l2_proto
[a
- 1]) {
1228 case ISDN_PROTO_L2_X75I
:
1229 sprintf(cbuf
, "%02d;BX75\n", (int) a
);
1231 #ifdef CONFIG_ISDN_X25
1232 case ISDN_PROTO_L2_X25DTE
:
1233 sprintf(cbuf
, "%02d;BX2T\n", (int) a
);
1235 case ISDN_PROTO_L2_X25DCE
:
1236 sprintf(cbuf
, "%02d;BX2C\n", (int) a
);
1239 case ISDN_PROTO_L2_HDLC
:
1240 sprintf(cbuf
, "%02d;BTRA\n", (int) a
);
1244 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1245 sprintf(cbuf
, "%02d;DCON_R\n", (int) a
);
1246 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1249 case ISDN_CMD_ACCEPTB
:
1250 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1252 if (c
->arg
< ISDNLOOP_BCH
) {
1254 switch (card
->l2_proto
[a
- 1]) {
1255 case ISDN_PROTO_L2_X75I
:
1256 sprintf(cbuf
, "%02d;BCON_R,BX75\n", (int) a
);
1258 #ifdef CONFIG_ISDN_X25
1259 case ISDN_PROTO_L2_X25DTE
:
1260 sprintf(cbuf
, "%02d;BCON_R,BX2T\n", (int) a
);
1262 case ISDN_PROTO_L2_X25DCE
:
1263 sprintf(cbuf
, "%02d;BCON_R,BX2C\n", (int) a
);
1266 case ISDN_PROTO_L2_HDLC
:
1267 sprintf(cbuf
, "%02d;BCON_R,BTRA\n", (int) a
);
1270 sprintf(cbuf
, "%02d;BCON_R\n", (int) a
);
1272 printk(KERN_DEBUG
"isdnloop writecmd '%s'\n", cbuf
);
1273 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1275 case ISDN_CMD_HANGUP
:
1276 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1278 if (c
->arg
< ISDNLOOP_BCH
) {
1280 sprintf(cbuf
, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a
, (int) a
);
1281 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1284 case ISDN_CMD_SETEAZ
:
1285 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1289 if (c
->arg
< ISDNLOOP_BCH
) {
1291 if (card
->ptype
== ISDN_PTYPE_EURO
) {
1292 sprintf(cbuf
, "%02d;MS%s%s\n", (int) a
,
1293 c
->parm
.num
[0] ? "N" : "ALL", c
->parm
.num
);
1295 sprintf(cbuf
, "%02d;EAZ%s\n", (int) a
,
1296 c
->parm
.num
[0] ? c
->parm
.num
: (u_char
*) "0123456789");
1297 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1300 case ISDN_CMD_CLREAZ
:
1301 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1305 if (c
->arg
< ISDNLOOP_BCH
) {
1307 if (card
->ptype
== ISDN_PTYPE_EURO
)
1308 sprintf(cbuf
, "%02d;MSNC\n", (int) a
);
1310 sprintf(cbuf
, "%02d;EAZC\n", (int) a
);
1311 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1314 case ISDN_CMD_SETL2
:
1315 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1317 if ((c
->arg
& 255) < ISDNLOOP_BCH
) {
1320 case ISDN_PROTO_L2_X75I
:
1321 sprintf(cbuf
, "%02d;BX75\n", (int) (a
& 255) + 1);
1323 #ifdef CONFIG_ISDN_X25
1324 case ISDN_PROTO_L2_X25DTE
:
1325 sprintf(cbuf
, "%02d;BX2T\n", (int) (a
& 255) + 1);
1327 case ISDN_PROTO_L2_X25DCE
:
1328 sprintf(cbuf
, "%02d;BX2C\n", (int) (a
& 255) + 1);
1331 case ISDN_PROTO_L2_HDLC
:
1332 sprintf(cbuf
, "%02d;BTRA\n", (int) (a
& 255) + 1);
1334 case ISDN_PROTO_L2_TRANS
:
1335 sprintf(cbuf
, "%02d;BTRA\n", (int) (a
& 255) + 1);
1340 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1341 card
->l2_proto
[a
& 255] = (a
>> 8);
1344 case ISDN_CMD_SETL3
:
1345 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1356 * Find card with given driverId
1358 static inline isdnloop_card
*
1359 isdnloop_findcard(int driverid
)
1361 isdnloop_card
*p
= cards
;
1364 if (p
->myid
== driverid
)
1368 return (isdnloop_card
*) 0;
1372 * Wrapper functions for interface to linklevel
1375 if_command(isdn_ctrl
* c
)
1377 isdnloop_card
*card
= isdnloop_findcard(c
->driver
);
1380 return (isdnloop_command(c
, card
));
1382 "isdnloop: if_command called with invalid driverId!\n");
1387 if_writecmd(const u_char __user
*buf
, int len
, int id
, int channel
)
1389 isdnloop_card
*card
= isdnloop_findcard(id
);
1392 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1394 return (isdnloop_writecmd(buf
, len
, 1, card
));
1397 "isdnloop: if_writecmd called with invalid driverId!\n");
1402 if_readstatus(u_char __user
*buf
, int len
, int id
, int channel
)
1404 isdnloop_card
*card
= isdnloop_findcard(id
);
1407 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1409 return (isdnloop_readstatus(buf
, len
, card
));
1412 "isdnloop: if_readstatus called with invalid driverId!\n");
1417 if_sendbuf(int id
, int channel
, int ack
, struct sk_buff
*skb
)
1419 isdnloop_card
*card
= isdnloop_findcard(id
);
1422 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1424 /* ack request stored in skb scratch area */
1426 return (isdnloop_sendbuf(channel
, skb
, card
));
1429 "isdnloop: if_sendbuf called with invalid driverId!\n");
1434 * Allocate a new card-struct, initialize it
1435 * link it into cards-list and register it at linklevel.
1437 static isdnloop_card
*
1438 isdnloop_initcard(char *id
)
1440 isdnloop_card
*card
;
1443 if (!(card
= (isdnloop_card
*) kmalloc(sizeof(isdnloop_card
), GFP_KERNEL
))) {
1445 "isdnloop: (%s) Could not allocate card-struct.\n", id
);
1446 return (isdnloop_card
*) 0;
1448 memset((char *) card
, 0, sizeof(isdnloop_card
));
1449 card
->interface
.owner
= THIS_MODULE
;
1450 card
->interface
.channels
= ISDNLOOP_BCH
;
1451 card
->interface
.hl_hdrlen
= 1; /* scratch area for storing ack flag*/
1452 card
->interface
.maxbufsize
= 4000;
1453 card
->interface
.command
= if_command
;
1454 card
->interface
.writebuf_skb
= if_sendbuf
;
1455 card
->interface
.writecmd
= if_writecmd
;
1456 card
->interface
.readstat
= if_readstatus
;
1457 card
->interface
.features
= ISDN_FEATURE_L2_X75I
|
1458 #ifdef CONFIG_ISDN_X25
1459 ISDN_FEATURE_L2_X25DTE
|
1460 ISDN_FEATURE_L2_X25DCE
|
1462 ISDN_FEATURE_L2_HDLC
|
1463 ISDN_FEATURE_L3_TRANS
|
1464 ISDN_FEATURE_P_UNKNOWN
;
1465 card
->ptype
= ISDN_PTYPE_UNKNOWN
;
1466 strlcpy(card
->interface
.id
, id
, sizeof(card
->interface
.id
));
1467 card
->msg_buf_write
= card
->msg_buf
;
1468 card
->msg_buf_read
= card
->msg_buf
;
1469 card
->msg_buf_end
= &card
->msg_buf
[sizeof(card
->msg_buf
) - 1];
1470 for (i
= 0; i
< ISDNLOOP_BCH
; i
++) {
1471 card
->l2_proto
[i
] = ISDN_PROTO_L2_X75I
;
1472 skb_queue_head_init(&card
->bqueue
[i
]);
1474 skb_queue_head_init(&card
->dqueue
);
1477 if (!register_isdn(&card
->interface
)) {
1478 cards
= cards
->next
;
1480 "isdnloop: Unable to register %s\n", id
);
1482 return (isdnloop_card
*) 0;
1484 card
->myid
= card
->interface
.channels
;
1489 isdnloop_addcard(char *id1
)
1491 isdnloop_card
*card
;
1493 if (!(card
= isdnloop_initcard(id1
))) {
1497 "isdnloop: (%s) virtual card added\n",
1498 card
->interface
.id
);
1508 if ((p
= strchr(revision
, ':'))) {
1510 p
= strchr(rev
, '$');
1513 strcpy(rev
, " ??? ");
1514 printk(KERN_NOTICE
"isdnloop-ISDN-driver Rev%s\n", rev
);
1517 return (isdnloop_addcard(isdnloop_id
));
1526 isdnloop_card
*card
= cards
;
1527 isdnloop_card
*last
;
1530 isdnloop_stopallcards();
1532 cmd
.command
= ISDN_STAT_UNLOAD
;
1533 cmd
.driver
= card
->myid
;
1534 card
->interface
.statcallb(&cmd
);
1535 for (i
= 0; i
< ISDNLOOP_BCH
; i
++)
1536 isdnloop_free_queue(card
, i
);
1542 skb_queue_purge(&card
->dqueue
);
1546 printk(KERN_NOTICE
"isdnloop-ISDN-driver unloaded\n");
1549 module_init(isdnloop_init
);
1550 module_exit(isdnloop_exit
);