1 /* $Id: isdnloop.c,v 1.8 1998/11/18 18:59:43 armin Exp $
3 * ISDN low-level module implementing a dummy loop driver.
5 * Copyright 1997 by Fritz Elfert (fritz@wuemaus.franken.de)
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)
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.
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.
21 * $Log: isdnloop.c,v $
22 * Revision 1.8 1998/11/18 18:59:43 armin
25 * Revision 1.7 1998/10/30 18:58:03 he
26 * typecast to suppress a compiler warning
28 * Revision 1.6 1998/06/17 19:51:37 he
29 * merged with 2.1.10[34] (cosmetics and udelay() -> mdelay())
30 * brute force fix to avoid Ugh's in isdn_tty_write()
31 * cleaned up some dead code
33 * Revision 1.5 1998/04/14 20:59:32 he
34 * merged 2.1.94 changes
36 * Revision 1.4 1998/02/24 21:39:05 he
37 * L2_PROT_X25DTE / DCE
38 * additional state 17 and new internal signal messages "BCON_I"
39 * (for reliable connect confirmation primitive as needed by x.25 upper layer)
40 * Changes for new LL-HL interface
42 * Revision 1.3 1998/02/20 17:33:30 fritz
43 * Changes for recent kernels.
45 * Revision 1.2 1997/10/01 09:22:03 fritz
46 * Removed old compatibility stuff for 2.0.X kernels.
47 * From now on, this code is for 2.1.X ONLY!
48 * Old stuff is still in the separate branch.
50 * Revision 1.1 1997/03/24 23:02:04 fritz
51 * Added isdnloop driver.
55 #include <linux/config.h>
59 *revision
= "$Revision: 1.8 $";
61 static int isdnloop_addcard(char *);
64 * Free queue completely.
67 * card = pointer to card struct
68 * channel = channel number
71 isdnloop_free_queue(isdnloop_card
* card
, int channel
)
73 struct sk_buff_head
*queue
= &card
->bqueue
[channel
];
76 while ((skb
= skb_dequeue(queue
)))
78 card
->sndcount
[channel
] = 0;
82 * Send B-Channel data to another virtual card.
83 * This routine is called via timer-callback from isdnloop_pollbchan().
86 * card = pointer to card struct.
87 * ch = channel number (0-based)
90 isdnloop_bchan_send(isdnloop_card
* card
, int ch
)
92 isdnloop_card
*rcard
= card
->rcard
[ch
];
93 int rch
= card
->rch
[ch
], len
, ack
;
97 while (card
->sndcount
[ch
]) {
98 if ((skb
= skb_dequeue(&card
->bqueue
[ch
]))) {
100 card
->sndcount
[ch
] -= len
;
101 ack
= *(skb
->head
); /* used as scratch area */
102 cmd
.driver
= card
->myid
;
105 rcard
->interface
.rcvcallb_skb(rcard
->myid
, rch
, skb
);
107 printk(KERN_WARNING
"isdnloop: no rcard, skb dropped\n");
110 cmd
.command
= ISDN_STAT_L1ERR
;
111 cmd
.parm
.errcode
= ISDN_STAT_L1ERR_SEND
;
112 card
->interface
.statcallb(&cmd
);
114 cmd
.command
= ISDN_STAT_BSENT
;
115 cmd
.parm
.length
= len
;
116 if ( ack
) card
->interface
.statcallb(&cmd
);
118 card
->sndcount
[ch
] = 0;
123 * Send/Receive Data to/from the B-Channel.
124 * This routine is called via timer-callback.
125 * It schedules itself while any B-Channel is open.
128 * data = pointer to card struct, set by kernel timer.data
131 isdnloop_pollbchan(unsigned long data
)
133 isdnloop_card
*card
= (isdnloop_card
*) data
;
136 if (card
->flags
& ISDNLOOP_FLAGS_B1ACTIVE
)
137 isdnloop_bchan_send(card
, 0);
138 if (card
->flags
& ISDNLOOP_FLAGS_B2ACTIVE
)
139 isdnloop_bchan_send(card
, 1);
140 if (card
->flags
& (ISDNLOOP_FLAGS_B1ACTIVE
| ISDNLOOP_FLAGS_B2ACTIVE
)) {
141 /* schedule b-channel polling again */
144 card
->rb_timer
.expires
= jiffies
+ ISDNLOOP_TIMER_BCREAD
;
145 add_timer(&card
->rb_timer
);
146 card
->flags
|= ISDNLOOP_FLAGS_RBTIMER
;
147 restore_flags(flags
);
149 card
->flags
&= ~ISDNLOOP_FLAGS_RBTIMER
;
153 * Parse ICN-type setup string and fill fields of setup-struct
157 * setup = setup string, format: [caller-id],si1,si2,[called-id]
158 * cmd = pointer to struct to be filled.
161 isdnloop_parse_setup(char *setup
, isdn_ctrl
* cmd
)
164 char *s
= strpbrk(t
, ",");
167 strncpy(cmd
->parm
.setup
.phone
, t
, sizeof(cmd
->parm
.setup
.phone
));
168 s
= strpbrk(t
= s
, ",");
171 cmd
->parm
.setup
.si1
= 0;
173 cmd
->parm
.setup
.si1
= simple_strtoul(t
, NULL
, 10);
174 s
= strpbrk(t
= s
, ",");
177 cmd
->parm
.setup
.si2
= 0;
179 cmd
->parm
.setup
.si2
=
180 simple_strtoul(t
, NULL
, 10);
181 strncpy(cmd
->parm
.setup
.eazmsn
, s
, sizeof(cmd
->parm
.setup
.eazmsn
));
182 cmd
->parm
.setup
.plan
= 0;
183 cmd
->parm
.setup
.screen
= 0;
186 typedef struct isdnloop_stat
{
192 static isdnloop_stat isdnloop_stat_table
[] =
194 {"BCON_", ISDN_STAT_BCONN
, 1}, /* B-Channel connected */
195 {"BDIS_", ISDN_STAT_BHUP
, 2}, /* B-Channel disconnected */
196 {"DCON_", ISDN_STAT_DCONN
, 0}, /* D-Channel connected */
197 {"DDIS_", ISDN_STAT_DHUP
, 0}, /* D-Channel disconnected */
198 {"DCAL_I", ISDN_STAT_ICALL
, 3}, /* Incoming call dialup-line */
199 {"DSCA_I", ISDN_STAT_ICALL
, 3}, /* Incoming call 1TR6-SPV */
200 {"FCALL", ISDN_STAT_ICALL
, 4}, /* Leased line connection up */
201 {"CIF", ISDN_STAT_CINF
, 5}, /* Charge-info, 1TR6-type */
202 {"AOC", ISDN_STAT_CINF
, 6}, /* Charge-info, DSS1-type */
203 {"CAU", ISDN_STAT_CAUSE
, 7}, /* Cause code */
204 {"TEI OK", ISDN_STAT_RUN
, 0}, /* Card connected to wallplug */
205 {"NO D-CHAN", ISDN_STAT_NODCH
, 0}, /* No D-channel available */
206 {"E_L1: ACT FAIL", ISDN_STAT_BHUP
, 8}, /* Layer-1 activation failed */
207 {"E_L2: DATA LIN", ISDN_STAT_BHUP
, 8}, /* Layer-2 data link lost */
208 {"E_L1: ACTIVATION FAILED",
209 ISDN_STAT_BHUP
, 8}, /* Layer-1 activation failed */
216 * Parse Status message-strings from virtual card.
217 * Depending on status, call statcallb for sending messages to upper
218 * levels. Also set/reset B-Channel active-flags.
221 * status = status string to parse.
222 * channel = channel where message comes from.
223 * card = card where message comes from.
226 isdnloop_parse_status(u_char
* status
, int channel
, isdnloop_card
* card
)
228 isdnloop_stat
*s
= isdnloop_stat_table
;
233 if (!strncmp(status
, s
->statstr
, strlen(s
->statstr
))) {
234 cmd
.command
= s
->command
;
242 cmd
.driver
= card
->myid
;
247 card
->flags
|= (channel
) ?
248 ISDNLOOP_FLAGS_B2ACTIVE
: ISDNLOOP_FLAGS_B1ACTIVE
;
252 card
->flags
&= ~((channel
) ?
253 ISDNLOOP_FLAGS_B2ACTIVE
: ISDNLOOP_FLAGS_B1ACTIVE
);
254 isdnloop_free_queue(card
, channel
);
257 /* DCAL_I and DSCA_I */
258 isdnloop_parse_setup(status
+ 6, &cmd
);
262 sprintf(cmd
.parm
.setup
.phone
, "LEASED%d", card
->myid
);
263 sprintf(cmd
.parm
.setup
.eazmsn
, "%d", channel
+ 1);
264 cmd
.parm
.setup
.si1
= 7;
265 cmd
.parm
.setup
.si2
= 0;
266 cmd
.parm
.setup
.plan
= 0;
267 cmd
.parm
.setup
.screen
= 0;
271 strncpy(cmd
.parm
.num
, status
+ 3, sizeof(cmd
.parm
.num
) - 1);
275 sprintf(cmd
.parm
.num
, "%d",
276 (int) simple_strtoul(status
+ 7, NULL
, 16));
281 if (strlen(status
) == 4)
282 sprintf(cmd
.parm
.num
, "%s%c%c",
283 status
+ 2, *status
, *(status
+ 1));
285 strncpy(cmd
.parm
.num
, status
+ 1, sizeof(cmd
.parm
.num
) - 1);
288 /* Misc Errors on L1 and L2 */
289 card
->flags
&= ~ISDNLOOP_FLAGS_B1ACTIVE
;
290 isdnloop_free_queue(card
, 0);
292 cmd
.driver
= card
->myid
;
293 card
->interface
.statcallb(&cmd
);
294 cmd
.command
= ISDN_STAT_DHUP
;
296 cmd
.driver
= card
->myid
;
297 card
->interface
.statcallb(&cmd
);
298 cmd
.command
= ISDN_STAT_BHUP
;
299 card
->flags
&= ~ISDNLOOP_FLAGS_B2ACTIVE
;
300 isdnloop_free_queue(card
, 1);
302 cmd
.driver
= card
->myid
;
303 card
->interface
.statcallb(&cmd
);
304 cmd
.command
= ISDN_STAT_DHUP
;
306 cmd
.driver
= card
->myid
;
309 card
->interface
.statcallb(&cmd
);
313 * Store a cwcharacter into ringbuffer for reading from /dev/isdnctrl
316 * card = pointer to card struct.
320 isdnloop_putmsg(isdnloop_card
* card
, unsigned char c
)
326 *card
->msg_buf_write
++ = (c
== 0xff) ? '\n' : c
;
327 if (card
->msg_buf_write
== card
->msg_buf_read
) {
328 if (++card
->msg_buf_read
> card
->msg_buf_end
)
329 card
->msg_buf_read
= card
->msg_buf
;
331 if (card
->msg_buf_write
> card
->msg_buf_end
)
332 card
->msg_buf_write
= card
->msg_buf
;
333 restore_flags(flags
);
337 * Poll a virtual cards message queue.
338 * If there are new status-replies from the card, copy them to
339 * ringbuffer for reading on /dev/isdnctrl and call
340 * isdnloop_parse_status() for processing them. Watch for special
341 * Firmware bootmessage and parse it, to get the D-Channel protocol.
342 * If there are B-Channels open, initiate a timer-callback to
343 * isdnloop_pollbchan().
344 * This routine is called periodically via timer interrupt.
347 * data = pointer to card struct
350 isdnloop_polldchan(unsigned long data
)
352 isdnloop_card
*card
= (isdnloop_card
*) data
;
362 if ((skb
= skb_dequeue(&card
->dqueue
)))
366 for (left
= avail
; left
> 0; left
--) {
369 isdnloop_putmsg(card
, c
);
370 card
->imsg
[card
->iptr
] = c
;
375 isdnloop_putmsg(card
, '\n');
376 card
->imsg
[card
->iptr
] = 0;
378 if (card
->imsg
[0] == '0' && card
->imsg
[1] >= '0' &&
379 card
->imsg
[1] <= '2' && card
->imsg
[2] == ';') {
380 ch
= (card
->imsg
[1] - '0') - 1;
382 isdnloop_parse_status(p
, ch
, card
);
385 if (!strncmp(p
, "DRV1.", 5)) {
386 printk(KERN_INFO
"isdnloop: (%s) %s\n", CID
, p
);
387 if (!strncmp(p
+ 7, "TC", 2)) {
388 card
->ptype
= ISDN_PTYPE_1TR6
;
389 card
->interface
.features
|= ISDN_FEATURE_P_1TR6
;
391 "isdnloop: (%s) 1TR6-Protocol loaded and running\n", CID
);
393 if (!strncmp(p
+ 7, "EC", 2)) {
394 card
->ptype
= ISDN_PTYPE_EURO
;
395 card
->interface
.features
|= ISDN_FEATURE_P_EURO
;
397 "isdnloop: (%s) Euro-Protocol loaded and running\n", CID
);
406 cmd
.command
= ISDN_STAT_STAVAIL
;
407 cmd
.driver
= card
->myid
;
409 card
->interface
.statcallb(&cmd
);
411 if (card
->flags
& (ISDNLOOP_FLAGS_B1ACTIVE
| ISDNLOOP_FLAGS_B2ACTIVE
))
412 if (!(card
->flags
& ISDNLOOP_FLAGS_RBTIMER
)) {
413 /* schedule b-channel polling */
414 card
->flags
|= ISDNLOOP_FLAGS_RBTIMER
;
417 del_timer(&card
->rb_timer
);
418 card
->rb_timer
.function
= isdnloop_pollbchan
;
419 card
->rb_timer
.data
= (unsigned long) card
;
420 card
->rb_timer
.expires
= jiffies
+ ISDNLOOP_TIMER_BCREAD
;
421 add_timer(&card
->rb_timer
);
422 restore_flags(flags
);
427 card
->st_timer
.expires
= jiffies
+ ISDNLOOP_TIMER_DCREAD
;
428 add_timer(&card
->st_timer
);
429 restore_flags(flags
);
433 * Append a packet to the transmit buffer-queue.
436 * channel = Number of B-channel
437 * skb = packet to send.
438 * card = pointer to card-struct
440 * Number of bytes transferred, -E??? on error
443 isdnloop_sendbuf(int channel
, struct sk_buff
*skb
, isdnloop_card
* card
)
447 struct sk_buff
*nskb
;
451 "isdnloop: Send packet too large\n");
455 if (!(card
->flags
& (channel
) ? ISDNLOOP_FLAGS_B2ACTIVE
: ISDNLOOP_FLAGS_B1ACTIVE
))
457 if (card
->sndcount
[channel
] > ISDNLOOP_MAX_SQUEUE
)
461 nskb
= skb_clone(skb
, GFP_ATOMIC
);
463 skb_queue_tail(&card
->bqueue
[channel
], nskb
);
467 card
->sndcount
[channel
] += len
;
468 restore_flags(flags
);
474 * Read the messages from the card's ringbuffer
477 * buf = pointer to buffer.
478 * len = number of bytes to read.
479 * user = flag, 1: called from userlevel 0: called from kernel.
480 * card = pointer to card struct.
482 * number of bytes actually transferred.
485 isdnloop_readstatus(u_char
* buf
, int len
, int user
, isdnloop_card
* card
)
490 for (p
= buf
, count
= 0; count
< len
; p
++, count
++) {
491 if (card
->msg_buf_read
== card
->msg_buf_write
)
494 put_user(*card
->msg_buf_read
++, p
);
496 *p
= *card
->msg_buf_read
++;
497 if (card
->msg_buf_read
> card
->msg_buf_end
)
498 card
->msg_buf_read
= card
->msg_buf
;
504 * Simulate a card's response by appending it to the cards
508 * card = pointer to card struct.
509 * s = pointer to message-string.
510 * ch = channel: 0 = generic messages, 1 and 2 = D-channel messages.
512 * 0 on success, 1 on memory squeeze.
515 isdnloop_fake(isdnloop_card
* card
, char *s
, int ch
)
518 int len
= strlen(s
) + ((ch
>= 0) ? 3 : 0);
520 if (!(skb
= dev_alloc_skb(len
))) {
521 printk(KERN_WARNING
"isdnloop: Out of memory in isdnloop_fake\n");
525 sprintf(skb_put(skb
, 3), "%02d;", ch
);
526 memcpy(skb_put(skb
, strlen(s
)), s
, strlen(s
));
527 skb_queue_tail(&card
->dqueue
, skb
);
531 static isdnloop_stat isdnloop_cmd_table
[] =
533 {"BCON_R", 0, 1}, /* B-Channel connect */
534 {"BCON_I", 0, 17}, /* B-Channel connect ind */
535 {"BDIS_R", 0, 2}, /* B-Channel disconnect */
536 {"DDIS_R", 0, 3}, /* D-Channel disconnect */
537 {"DCON_R", 0, 16}, /* D-Channel connect */
538 {"DSCA_R", 0, 4}, /* Dial 1TR6-SPV */
539 {"DCAL_R", 0, 5}, /* Dial */
540 {"EAZC", 0, 6}, /* Clear EAZ listener */
541 {"EAZ", 0, 7}, /* Set EAZ listener */
542 {"SEEAZ", 0, 8}, /* Get EAZ listener */
543 {"MSN", 0, 9}, /* Set/Clear MSN listener */
544 {"MSALL", 0, 10}, /* Set multi MSN listeners */
545 {"SETSIL", 0, 11}, /* Set SI list */
546 {"SEESIL", 0, 12}, /* Get SI list */
547 {"SILC", 0, 13}, /* Clear SI list */
548 {"LOCK", 0, -1}, /* LOCK channel */
549 {"UNLOCK", 0, -1}, /* UNLOCK channel */
550 {"FV2ON", 1, 14}, /* Leased mode on */
551 {"FV2OFF", 1, 15}, /* Leased mode off */
558 * Simulate an error-response from a card.
561 * card = pointer to card struct.
564 isdnloop_fake_err(isdnloop_card
* card
)
568 sprintf(buf
, "E%s", card
->omsg
);
569 isdnloop_fake(card
, buf
, -1);
570 isdnloop_fake(card
, "NAK", -1);
573 static u_char ctable_eu
[] =
574 {0x00, 0x11, 0x01, 0x12};
575 static u_char ctable_1t
[] =
576 {0x00, 0x3b, 0x01, 0x3a};
579 * Assemble a simplified cause message depending on the
580 * D-channel protocol used.
583 * card = pointer to card struct.
584 * loc = location: 0 = local, 1 = remote.
585 * cau = cause: 1 = busy, 2 = nonexistent callerid, 3 = no user responding.
587 * Pointer to buffer containing the assembled message.
590 isdnloop_unicause(isdnloop_card
* card
, int loc
, int cau
)
594 switch (card
->ptype
) {
595 case ISDN_PTYPE_EURO
:
596 sprintf(buf
, "E%02X%02X", (loc
) ? 4 : 2, ctable_eu
[cau
]);
598 case ISDN_PTYPE_1TR6
:
599 sprintf(buf
, "%02X44", ctable_1t
[cau
]);
608 * Release a virtual connection. Called from timer interrupt, when
609 * called party did not respond.
612 * card = pointer to card struct.
613 * ch = channel (0-based)
616 isdnloop_atimeout(isdnloop_card
* card
, int ch
)
624 isdnloop_fake(card
->rcard
[ch
], "DDIS_I", card
->rch
[ch
] + 1);
625 card
->rcard
[ch
]->rcard
[card
->rch
[ch
]] = NULL
;
626 card
->rcard
[ch
] = NULL
;
628 isdnloop_fake(card
, "DDIS_I", ch
+ 1);
629 /* No user responding */
630 sprintf(buf
, "CAU%s", isdnloop_unicause(card
, 1, 3));
631 isdnloop_fake(card
, buf
, ch
+ 1);
632 restore_flags(flags
);
636 * Wrapper for isdnloop_atimeout().
639 isdnloop_atimeout0(unsigned long data
)
641 isdnloop_card
*card
= (isdnloop_card
*) data
;
642 isdnloop_atimeout(card
, 0);
646 * Wrapper for isdnloop_atimeout().
649 isdnloop_atimeout1(unsigned long data
)
651 isdnloop_card
*card
= (isdnloop_card
*) data
;
652 isdnloop_atimeout(card
, 1);
656 * Install a watchdog for a user, not responding.
659 * card = pointer to card struct.
660 * ch = channel to watch for.
663 isdnloop_start_ctimer(isdnloop_card
* card
, int ch
)
669 init_timer(&card
->c_timer
[ch
]);
670 card
->c_timer
[ch
].expires
= jiffies
+ ISDNLOOP_TIMER_ALERTWAIT
;
672 card
->c_timer
[ch
].function
= isdnloop_atimeout1
;
674 card
->c_timer
[ch
].function
= isdnloop_atimeout0
;
675 card
->c_timer
[ch
].data
= (unsigned long) card
;
676 add_timer(&card
->c_timer
[ch
]);
677 restore_flags(flags
);
681 * Kill a pending channel watchdog.
684 * card = pointer to card struct.
685 * ch = channel (0-based).
688 isdnloop_kill_ctimer(isdnloop_card
* card
, int ch
)
694 del_timer(&card
->c_timer
[ch
]);
695 restore_flags(flags
);
698 static u_char si2bit
[] =
699 {0, 1, 0, 0, 0, 2, 0, 4, 0, 0};
700 static u_char bit2si
[] =
704 * Try finding a listener for an outgoing call.
707 * card = pointer to calling card.
708 * p = pointer to ICN-type setup-string.
709 * lch = channel of calling card.
710 * cmd = pointer to struct to be filled when parsing setup.
712 * 0 = found match, alerting should happen.
713 * 1 = found matching number but it is busy.
714 * 2 = no matching listener.
715 * 3 = found matching number but SI does not match.
718 isdnloop_try_call(isdnloop_card
* card
, char *p
, int lch
, isdn_ctrl
* cmd
)
720 isdnloop_card
*cc
= cards
;
728 isdnloop_parse_setup(p
, cmd
);
730 for (ch
= 0; ch
< 2; ch
++) {
731 /* Exclude ourself */
732 if ((cc
== card
) && (ch
== lch
))
736 case ISDN_PTYPE_EURO
:
737 for (i
= 0; i
< 3; i
++)
738 if (!(strcmp(cc
->s0num
[i
], cmd
->parm
.setup
.phone
)))
741 case ISDN_PTYPE_1TR6
:
744 sprintf(nbuf
, "%s%c", cc
->s0num
[0], *e
);
745 if (!(strcmp(nbuf
, cmd
->parm
.setup
.phone
)))
754 if (!(cc
->rcard
[ch
])) {
756 if (!(si2bit
[cmd
->parm
.setup
.si1
] & cc
->sil
[ch
])) {
757 restore_flags(flags
);
760 /* ch is idle, si and number matches */
761 cc
->rcard
[ch
] = card
;
763 card
->rcard
[lch
] = cc
;
765 restore_flags(flags
);
768 restore_flags(flags
);
769 /* num matches, but busy */
781 * Depending on D-channel protocol and caller/called, modify
785 * card = pointer to card struct.
786 * phone = pointer phone number.
787 * caller = flag: 1 = caller, 0 = called.
789 * pointer to new phone number.
792 isdnloop_vstphone(isdnloop_card
* card
, char *phone
, int caller
)
795 static char nphone
[30];
797 switch (card
->ptype
) {
798 case ISDN_PTYPE_EURO
:
800 for (i
= 0; i
< 2; i
++)
801 if (!(strcmp(card
->s0num
[i
], phone
)))
803 return (card
->s0num
[0]);
807 case ISDN_PTYPE_1TR6
:
809 sprintf(nphone
, "%s%c", card
->s0num
[0], phone
[0]);
812 return (&phone
[strlen(phone
) - 1]);
819 * Parse an ICN-type command string sent to the 'card'.
820 * Perform misc. actions depending on the command.
823 * card = pointer to card struct.
826 isdnloop_parse_cmd(isdnloop_card
* card
)
828 char *p
= card
->omsg
;
831 isdnloop_stat
*s
= isdnloop_cmd_table
;
836 if ((card
->omsg
[0] != '0') && (card
->omsg
[2] != ';')) {
837 isdnloop_fake_err(card
);
840 ch
= card
->omsg
[1] - '0';
841 if ((ch
< 0) || (ch
> 2)) {
842 isdnloop_fake_err(card
);
847 if (!strncmp(p
, s
->statstr
, strlen(s
->statstr
))) {
849 if (s
->command
&& (ch
!= 0)) {
850 isdnloop_fake_err(card
);
862 if (card
->rcard
[ch
- 1]) {
863 isdnloop_fake(card
->rcard
[ch
- 1], "BCON_I",
864 card
->rch
[ch
- 1] + 1);
869 if (card
->rcard
[ch
- 1]) {
870 isdnloop_fake(card
->rcard
[ch
- 1], "BCON_C",
871 card
->rch
[ch
- 1] + 1);
876 isdnloop_fake(card
, "BDIS_C", ch
);
877 if (card
->rcard
[ch
- 1]) {
878 isdnloop_fake(card
->rcard
[ch
- 1], "BDIS_I",
879 card
->rch
[ch
- 1] + 1);
884 isdnloop_kill_ctimer(card
, ch
- 1);
885 if (card
->rcard
[ch
- 1]) {
886 isdnloop_kill_ctimer(card
->rcard
[ch
- 1], card
->rch
[ch
- 1]);
887 isdnloop_fake(card
->rcard
[ch
- 1], "DCON_C",
888 card
->rch
[ch
- 1] + 1);
889 isdnloop_fake(card
, "DCON_C", ch
);
894 isdnloop_kill_ctimer(card
, ch
- 1);
895 if (card
->rcard
[ch
- 1]) {
896 isdnloop_kill_ctimer(card
->rcard
[ch
- 1], card
->rch
[ch
- 1]);
897 isdnloop_fake(card
->rcard
[ch
- 1], "DDIS_I",
898 card
->rch
[ch
- 1] + 1);
899 card
->rcard
[ch
- 1] = NULL
;
901 isdnloop_fake(card
, "DDIS_C", ch
);
904 /* 0x;DSCA_Rdd,yy,zz,oo */
905 if (card
->ptype
!= ISDN_PTYPE_1TR6
) {
906 isdnloop_fake_err(card
);
911 /* 0x;DCAL_Rdd,yy,zz,oo */
913 switch (isdnloop_try_call(card
, p
, ch
- 1, &cmd
)) {
916 sprintf(buf
, "D%s_I%s,%02d,%02d,%s",
917 (action
== 4) ? "SCA" : "CAL",
918 isdnloop_vstphone(card
, cmd
.parm
.setup
.eazmsn
, 1),
921 isdnloop_vstphone(card
->rcard
[ch
],
922 cmd
.parm
.setup
.phone
, 0));
923 isdnloop_fake(card
->rcard
[ch
- 1], buf
, card
->rch
[ch
- 1] + 1);
926 /* si1 does not match, don't alert but start timer */
927 isdnloop_start_ctimer(card
, ch
- 1);
931 isdnloop_fake(card
, "DDIS_I", ch
);
932 sprintf(buf
, "CAU%s", isdnloop_unicause(card
, 1, 1));
933 isdnloop_fake(card
, buf
, ch
);
937 isdnloop_fake(card
, "DDIS_I", ch
);
938 sprintf(buf
, "CAU%s", isdnloop_unicause(card
, 1, 2));
939 isdnloop_fake(card
, buf
, ch
);
945 card
->eazlist
[ch
- 1][0] = '\0';
950 strcpy(card
->eazlist
[ch
- 1], p
);
954 sprintf(buf
, "EAZ-LIST: %s", card
->eazlist
[ch
- 1]);
955 isdnloop_fake(card
, buf
, ch
+ 1);
967 while (strchr("0157", *p
)) {
969 card
->sil
[ch
- 1] |= si2bit
[*p
- '0'];
973 isdnloop_fake_err(card
);
977 sprintf(buf
, "SIN-LIST: ");
979 for (i
= 0; i
< 3; i
++)
980 if (card
->sil
[ch
- 1] & (1 << i
))
981 p
+= sprintf(p
, "%02d", bit2si
[i
]);
982 isdnloop_fake(card
, buf
, ch
+ 1);
986 card
->sil
[ch
- 1] = 0;
998 * Put command-strings into the of the 'card'. In reality, execute them
999 * right in place by calling isdnloop_parse_cmd(). Also copy every
1000 * command to the read message ringbuffer, preceeding it with a '>'.
1001 * These mesagges can be read at /dev/isdnctrl.
1004 * buf = pointer to command buffer.
1005 * len = length of buffer data.
1006 * user = flag: 1 = called form userlevel, 0 called from kernel.
1007 * card = pointer to card struct.
1009 * number of bytes transfered (currently always equals len).
1012 isdnloop_writecmd(const u_char
* buf
, int len
, int user
, isdnloop_card
* card
)
1019 int count
= MIN(255, len
);
1024 copy_from_user(msg
, buf
, count
);
1026 memcpy(msg
, buf
, count
);
1027 isdnloop_putmsg(card
, '>');
1028 for (p
= msg
; count
> 0; count
--, p
++) {
1031 isdnloop_putmsg(card
, *p
);
1032 card
->omsg
[card
->optr
] = *p
;
1034 card
->omsg
[card
->optr
] = '\0';
1036 isdnloop_parse_cmd(card
);
1038 isdnloop_putmsg(card
, '>');
1042 if (card
->optr
< 59)
1048 cmd
.command
= ISDN_STAT_STAVAIL
;
1049 cmd
.driver
= card
->myid
;
1051 card
->interface
.statcallb(&cmd
);
1056 * Delete card's pending timers, send STOP to linklevel
1059 isdnloop_stopcard(isdnloop_card
* card
)
1061 unsigned long flags
;
1066 if (card
->flags
& ISDNLOOP_FLAGS_RUNNING
) {
1067 card
->flags
&= ~ISDNLOOP_FLAGS_RUNNING
;
1068 del_timer(&card
->st_timer
);
1069 del_timer(&card
->rb_timer
);
1070 del_timer(&card
->c_timer
[0]);
1071 del_timer(&card
->c_timer
[1]);
1072 cmd
.command
= ISDN_STAT_STOP
;
1073 cmd
.driver
= card
->myid
;
1074 card
->interface
.statcallb(&cmd
);
1076 restore_flags(flags
);
1080 * Stop all cards before unload.
1083 isdnloop_stopallcards(void)
1085 isdnloop_card
*p
= cards
;
1088 isdnloop_stopcard(p
);
1094 * Start a 'card'. Simulate card's boot message and set the phone
1095 * number(s) of the virtual 'S0-Interface'. Install D-channel
1099 * card = pointer to card struct.
1100 * sdefp = pointer to struct holding ioctl parameters.
1102 * 0 on success, -E??? otherwise.
1105 isdnloop_start(isdnloop_card
* card
, isdnloop_sdef
* sdefp
)
1107 unsigned long flags
;
1111 if (card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1113 copy_from_user((char *) &sdef
, (char *) sdefp
, sizeof(sdef
));
1116 switch (sdef
.ptype
) {
1117 case ISDN_PTYPE_EURO
:
1118 if (isdnloop_fake(card
, "DRV1.23EC-Q.931-CAPI-CNS-BASIS-20.02.96",
1120 restore_flags(flags
);
1123 card
->sil
[0] = card
->sil
[1] = 4;
1124 if (isdnloop_fake(card
, "TEI OK", 0)) {
1125 restore_flags(flags
);
1128 for (i
= 0; i
< 3; i
++)
1129 strcpy(card
->s0num
[i
], sdef
.num
[i
]);
1131 case ISDN_PTYPE_1TR6
:
1132 if (isdnloop_fake(card
, "DRV1.04TC-1TR6-CAPI-CNS-BASIS-29.11.95",
1134 restore_flags(flags
);
1137 card
->sil
[0] = card
->sil
[1] = 4;
1138 if (isdnloop_fake(card
, "TEI OK", 0)) {
1139 restore_flags(flags
);
1142 strcpy(card
->s0num
[0], sdef
.num
[0]);
1143 card
->s0num
[1][0] = '\0';
1144 card
->s0num
[2][0] = '\0';
1147 restore_flags(flags
);
1148 printk(KERN_WARNING
"isdnloop: Illegal D-channel protocol %d\n",
1152 init_timer(&card
->st_timer
);
1153 card
->st_timer
.expires
= jiffies
+ ISDNLOOP_TIMER_DCREAD
;
1154 card
->st_timer
.function
= isdnloop_polldchan
;
1155 card
->st_timer
.data
= (unsigned long) card
;
1156 add_timer(&card
->st_timer
);
1157 card
->flags
|= ISDNLOOP_FLAGS_RUNNING
;
1158 restore_flags(flags
);
1163 * Main handler for commands sent by linklevel.
1166 isdnloop_command(isdn_ctrl
* c
, isdnloop_card
* card
)
1174 switch (c
->command
) {
1175 case ISDN_CMD_IOCTL
:
1176 memcpy(&a
, c
->parm
.num
, sizeof(ulong
));
1178 case ISDNLOOP_IOCTL_DEBUGVAR
:
1179 return (ulong
) card
;
1180 case ISDNLOOP_IOCTL_STARTUP
:
1181 if ((i
= verify_area(VERIFY_READ
, (void *) a
, sizeof(isdnloop_sdef
))))
1183 return (isdnloop_start(card
, (isdnloop_sdef
*) a
));
1185 case ISDNLOOP_IOCTL_ADDCARD
:
1186 if ((i
= verify_area(VERIFY_READ
, (void *) a
, sizeof(isdnloop_cdef
))))
1188 copy_from_user((char *) &cdef
, (char *) a
, sizeof(cdef
));
1189 return (isdnloop_addcard(cdef
.id1
));
1191 case ISDNLOOP_IOCTL_LEASEDCFG
:
1193 if (!card
->leased
) {
1195 while (card
->ptype
== ISDN_PTYPE_UNKNOWN
) {
1196 schedule_timeout(10);
1198 schedule_timeout(10);
1199 sprintf(cbuf
, "00;FV2ON\n01;EAZ1\n02;EAZ2\n");
1200 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1202 "isdnloop: (%s) Leased-line mode enabled\n",
1204 cmd
.command
= ISDN_STAT_RUN
;
1205 cmd
.driver
= card
->myid
;
1207 card
->interface
.statcallb(&cmd
);
1212 sprintf(cbuf
, "00;FV2OFF\n");
1213 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1215 "isdnloop: (%s) Leased-line mode disabled\n",
1217 cmd
.command
= ISDN_STAT_RUN
;
1218 cmd
.driver
= card
->myid
;
1220 card
->interface
.statcallb(&cmd
);
1229 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1233 if ((c
->arg
& 255) < ISDNLOOP_BCH
) {
1239 p
= c
->parm
.setup
.phone
;
1240 if (*p
== 's' || *p
== 'S') {
1243 strcpy(dcode
, "SCA");
1246 strcpy(dcode
, "CAL");
1248 sprintf(cbuf
, "%02d;D%s_R%s,%02d,%02d,%s\n", (int) (a
+ 1),
1249 dcode
, dial
, c
->parm
.setup
.si1
,
1250 c
->parm
.setup
.si2
, c
->parm
.setup
.eazmsn
);
1251 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1254 case ISDN_CMD_ACCEPTD
:
1255 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1257 if (c
->arg
< ISDNLOOP_BCH
) {
1260 switch (card
->l2_proto
[a
- 1]) {
1261 case ISDN_PROTO_L2_X75I
:
1262 sprintf(cbuf
, "%02d;BX75\n", (int) a
);
1264 #ifdef CONFIG_ISDN_X25
1265 case ISDN_PROTO_L2_X25DTE
:
1266 sprintf(cbuf
, "%02d;BX2T\n", (int) a
);
1268 case ISDN_PROTO_L2_X25DCE
:
1269 sprintf(cbuf
, "%02d;BX2C\n", (int) a
);
1272 case ISDN_PROTO_L2_HDLC
:
1273 sprintf(cbuf
, "%02d;BTRA\n", (int) a
);
1277 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1278 sprintf(cbuf
, "%02d;DCON_R\n", (int) a
);
1279 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1282 case ISDN_CMD_ACCEPTB
:
1283 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1285 if (c
->arg
< ISDNLOOP_BCH
) {
1287 switch (card
->l2_proto
[a
- 1]) {
1288 case ISDN_PROTO_L2_X75I
:
1289 sprintf(cbuf
, "%02d;BCON_R,BX75\n", (int) a
);
1291 #ifdef CONFIG_ISDN_X25
1292 case ISDN_PROTO_L2_X25DTE
:
1293 sprintf(cbuf
, "%02d;BCON_R,BX2T\n", (int) a
);
1295 case ISDN_PROTO_L2_X25DCE
:
1296 sprintf(cbuf
, "%02d;BCON_R,BX2C\n", (int) a
);
1299 case ISDN_PROTO_L2_HDLC
:
1300 sprintf(cbuf
, "%02d;BCON_R,BTRA\n", (int) a
);
1303 sprintf(cbuf
, "%02d;BCON_R\n", (int) a
);
1305 printk(KERN_DEBUG
"isdnloop writecmd '%s'\n", cbuf
);
1306 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1308 case ISDN_CMD_HANGUP
:
1309 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1311 if (c
->arg
< ISDNLOOP_BCH
) {
1313 sprintf(cbuf
, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a
, (int) a
);
1314 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1317 case ISDN_CMD_SETEAZ
:
1318 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1322 if (c
->arg
< ISDNLOOP_BCH
) {
1324 if (card
->ptype
== ISDN_PTYPE_EURO
) {
1325 sprintf(cbuf
, "%02d;MS%s%s\n", (int) a
,
1326 c
->parm
.num
[0] ? "N" : "ALL", c
->parm
.num
);
1328 sprintf(cbuf
, "%02d;EAZ%s\n", (int) a
,
1329 c
->parm
.num
[0] ? c
->parm
.num
: (u_char
*) "0123456789");
1330 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1333 case ISDN_CMD_CLREAZ
:
1334 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1338 if (c
->arg
< ISDNLOOP_BCH
) {
1340 if (card
->ptype
== ISDN_PTYPE_EURO
)
1341 sprintf(cbuf
, "%02d;MSNC\n", (int) a
);
1343 sprintf(cbuf
, "%02d;EAZC\n", (int) a
);
1344 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1347 case ISDN_CMD_SETL2
:
1348 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1350 if ((c
->arg
& 255) < ISDNLOOP_BCH
) {
1353 case ISDN_PROTO_L2_X75I
:
1354 sprintf(cbuf
, "%02d;BX75\n", (int) (a
& 255) + 1);
1356 #ifdef CONFIG_ISDN_X25
1357 case ISDN_PROTO_L2_X25DTE
:
1358 sprintf(cbuf
, "%02d;BX2T\n", (int) (a
& 255) + 1);
1360 case ISDN_PROTO_L2_X25DCE
:
1361 sprintf(cbuf
, "%02d;BX2C\n", (int) (a
& 255) + 1);
1364 case ISDN_PROTO_L2_HDLC
:
1365 sprintf(cbuf
, "%02d;BTRA\n", (int) (a
& 255) + 1);
1370 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1371 card
->l2_proto
[a
& 255] = (a
>> 8);
1374 case ISDN_CMD_GETL2
:
1375 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1377 if ((c
->arg
& 255) < ISDNLOOP_BCH
)
1378 return card
->l2_proto
[c
->arg
& 255];
1381 case ISDN_CMD_SETL3
:
1382 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1385 case ISDN_CMD_GETL3
:
1386 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1388 if ((c
->arg
& 255) < ISDNLOOP_BCH
)
1389 return ISDN_PROTO_L3_TRANS
;
1392 case ISDN_CMD_GETEAZ
:
1393 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1396 case ISDN_CMD_SETSIL
:
1397 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1400 case ISDN_CMD_GETSIL
:
1401 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1407 case ISDN_CMD_UNLOCK
:
1418 * Find card with given driverId
1420 static inline isdnloop_card
*
1421 isdnloop_findcard(int driverid
)
1423 isdnloop_card
*p
= cards
;
1426 if (p
->myid
== driverid
)
1430 return (isdnloop_card
*) 0;
1434 * Wrapper functions for interface to linklevel
1437 if_command(isdn_ctrl
* c
)
1439 isdnloop_card
*card
= isdnloop_findcard(c
->driver
);
1442 return (isdnloop_command(c
, card
));
1444 "isdnloop: if_command called with invalid driverId!\n");
1449 if_writecmd(const u_char
* buf
, int len
, int user
, int id
, int channel
)
1451 isdnloop_card
*card
= isdnloop_findcard(id
);
1454 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1456 return (isdnloop_writecmd(buf
, len
, user
, card
));
1459 "isdnloop: if_writecmd called with invalid driverId!\n");
1464 if_readstatus(u_char
* buf
, int len
, int user
, int id
, int channel
)
1466 isdnloop_card
*card
= isdnloop_findcard(id
);
1469 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1471 return (isdnloop_readstatus(buf
, len
, user
, card
));
1474 "isdnloop: if_readstatus called with invalid driverId!\n");
1479 if_sendbuf(int id
, int channel
, int ack
, struct sk_buff
*skb
)
1481 isdnloop_card
*card
= isdnloop_findcard(id
);
1484 if (!card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1486 /* ack request stored in skb scratch area */
1488 return (isdnloop_sendbuf(channel
, skb
, card
));
1491 "isdnloop: if_sendbuf called with invalid driverId!\n");
1496 * Allocate a new card-struct, initialize it
1497 * link it into cards-list and register it at linklevel.
1499 static isdnloop_card
*
1500 isdnloop_initcard(char *id
)
1502 isdnloop_card
*card
;
1505 if (!(card
= (isdnloop_card
*) kmalloc(sizeof(isdnloop_card
), GFP_KERNEL
))) {
1507 "isdnloop: (%s) Could not allocate card-struct.\n", id
);
1508 return (isdnloop_card
*) 0;
1510 memset((char *) card
, 0, sizeof(isdnloop_card
));
1511 card
->interface
.channels
= ISDNLOOP_BCH
;
1512 card
->interface
.hl_hdrlen
= 1; /* scratch area for storing ack flag*/
1513 card
->interface
.maxbufsize
= 4000;
1514 card
->interface
.command
= if_command
;
1515 card
->interface
.writebuf_skb
= if_sendbuf
;
1516 card
->interface
.writecmd
= if_writecmd
;
1517 card
->interface
.readstat
= if_readstatus
;
1518 card
->interface
.features
= ISDN_FEATURE_L2_X75I
|
1519 #ifdef CONFIG_ISDN_X25
1520 ISDN_FEATURE_L2_X25DTE
|
1521 ISDN_FEATURE_L2_X25DCE
|
1523 ISDN_FEATURE_L2_HDLC
|
1524 ISDN_FEATURE_L3_TRANS
|
1525 ISDN_FEATURE_P_UNKNOWN
;
1526 card
->ptype
= ISDN_PTYPE_UNKNOWN
;
1527 strncpy(card
->interface
.id
, id
, sizeof(card
->interface
.id
) - 1);
1528 card
->msg_buf_write
= card
->msg_buf
;
1529 card
->msg_buf_read
= card
->msg_buf
;
1530 card
->msg_buf_end
= &card
->msg_buf
[sizeof(card
->msg_buf
) - 1];
1531 for (i
= 0; i
< ISDNLOOP_BCH
; i
++) {
1532 card
->l2_proto
[i
] = ISDN_PROTO_L2_X75I
;
1533 skb_queue_head_init(&card
->bqueue
[i
]);
1535 skb_queue_head_init(&card
->dqueue
);
1538 if (!register_isdn(&card
->interface
)) {
1539 cards
= cards
->next
;
1541 "isdnloop: Unable to register %s\n", id
);
1543 return (isdnloop_card
*) 0;
1545 card
->myid
= card
->interface
.channels
;
1550 isdnloop_addcard(char *id1
)
1553 isdnloop_card
*card
;
1557 if (!(card
= isdnloop_initcard(id1
))) {
1558 restore_flags(flags
);
1561 restore_flags(flags
);
1563 "isdnloop: (%s) virtual card added\n",
1564 card
->interface
.id
);
1569 #define isdnloop_init init_module
1572 isdnloop_setup(char *str
, int *ints
)
1574 static char sid
[20];
1589 /* No symbols to export, hide all symbols */
1592 if ((p
= strchr(revision
, ':'))) {
1594 p
= strchr(rev
, '$');
1597 strcpy(rev
, " ??? ");
1598 printk(KERN_NOTICE
"isdnloop-ISDN-driver Rev%s\n", rev
);
1599 return (isdnloop_addcard(isdnloop_id
));
1604 cleanup_module(void)
1607 isdnloop_card
*card
= cards
;
1608 isdnloop_card
*last
;
1611 isdnloop_stopallcards();
1613 cmd
.command
= ISDN_STAT_UNLOAD
;
1614 cmd
.driver
= card
->myid
;
1615 card
->interface
.statcallb(&cmd
);
1616 for (i
= 0; i
< ISDNLOOP_BCH
; i
++)
1617 isdnloop_free_queue(card
, i
);
1622 struct sk_buff
*skb
;
1625 while ((skb
= skb_dequeue(&card
->dqueue
)))
1630 printk(KERN_NOTICE
"isdnloop-ISDN-driver unloaded\n");