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/slab.h>
15 #include <linux/init.h>
16 #include <linux/sched.h>
19 static char *revision
= "$Revision: 1.11.6.7 $";
20 static char *isdnloop_id
= "loop0";
22 MODULE_DESCRIPTION("ISDN4Linux: Pseudo Driver that simulates an ISDN card");
23 MODULE_AUTHOR("Fritz Elfert");
24 MODULE_LICENSE("GPL");
25 module_param(isdnloop_id
, charp
, 0);
26 MODULE_PARM_DESC(isdnloop_id
, "ID-String of first card");
28 static int isdnloop_addcard(char *);
31 * Free queue completely.
34 * card = pointer to card struct
35 * channel = channel number
38 isdnloop_free_queue(isdnloop_card
* card
, int channel
)
40 struct sk_buff_head
*queue
= &card
->bqueue
[channel
];
42 skb_queue_purge(queue
);
43 card
->sndcount
[channel
] = 0;
47 * Send B-Channel data to another virtual card.
48 * This routine is called via timer-callback from isdnloop_pollbchan().
51 * card = pointer to card struct.
52 * ch = channel number (0-based)
55 isdnloop_bchan_send(isdnloop_card
* card
, int ch
)
57 isdnloop_card
*rcard
= card
->rcard
[ch
];
58 int rch
= card
->rch
[ch
], len
, ack
;
62 while (card
->sndcount
[ch
]) {
63 if ((skb
= skb_dequeue(&card
->bqueue
[ch
]))) {
65 card
->sndcount
[ch
] -= len
;
66 ack
= *(skb
->head
); /* used as scratch area */
67 cmd
.driver
= card
->myid
;
70 rcard
->interface
.rcvcallb_skb(rcard
->myid
, rch
, skb
);
72 printk(KERN_WARNING
"isdnloop: no rcard, skb dropped\n");
76 cmd
.command
= ISDN_STAT_BSENT
;
77 cmd
.parm
.length
= len
;
78 card
->interface
.statcallb(&cmd
);
80 card
->sndcount
[ch
] = 0;
85 * Send/Receive Data to/from the B-Channel.
86 * This routine is called via timer-callback.
87 * It schedules itself while any B-Channel is open.
90 * data = pointer to card struct, set by kernel timer.data
93 isdnloop_pollbchan(unsigned long data
)
95 isdnloop_card
*card
= (isdnloop_card
*) data
;
98 if (card
->flags
& ISDNLOOP_FLAGS_B1ACTIVE
)
99 isdnloop_bchan_send(card
, 0);
100 if (card
->flags
& ISDNLOOP_FLAGS_B2ACTIVE
)
101 isdnloop_bchan_send(card
, 1);
102 if (card
->flags
& (ISDNLOOP_FLAGS_B1ACTIVE
| ISDNLOOP_FLAGS_B2ACTIVE
)) {
103 /* schedule b-channel polling again */
104 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
105 card
->rb_timer
.expires
= jiffies
+ ISDNLOOP_TIMER_BCREAD
;
106 add_timer(&card
->rb_timer
);
107 card
->flags
|= ISDNLOOP_FLAGS_RBTIMER
;
108 spin_unlock_irqrestore(&card
->isdnloop_lock
, 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
)
284 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
285 *card
->msg_buf_write
++ = (c
== 0xff) ? '\n' : c
;
286 if (card
->msg_buf_write
== card
->msg_buf_read
) {
287 if (++card
->msg_buf_read
> card
->msg_buf_end
)
288 card
->msg_buf_read
= card
->msg_buf
;
290 if (card
->msg_buf_write
> card
->msg_buf_end
)
291 card
->msg_buf_write
= card
->msg_buf
;
292 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
296 * Poll a virtual cards message queue.
297 * If there are new status-replies from the card, copy them to
298 * ringbuffer for reading on /dev/isdnctrl and call
299 * isdnloop_parse_status() for processing them. Watch for special
300 * Firmware bootmessage and parse it, to get the D-Channel protocol.
301 * If there are B-Channels open, initiate a timer-callback to
302 * isdnloop_pollbchan().
303 * This routine is called periodically via timer interrupt.
306 * data = pointer to card struct
309 isdnloop_polldchan(unsigned long data
)
311 isdnloop_card
*card
= (isdnloop_card
*) data
;
321 if ((skb
= skb_dequeue(&card
->dqueue
)))
325 for (left
= avail
; left
> 0; left
--) {
328 isdnloop_putmsg(card
, c
);
329 card
->imsg
[card
->iptr
] = c
;
334 isdnloop_putmsg(card
, '\n');
335 card
->imsg
[card
->iptr
] = 0;
337 if (card
->imsg
[0] == '0' && card
->imsg
[1] >= '0' &&
338 card
->imsg
[1] <= '2' && card
->imsg
[2] == ';') {
339 ch
= (card
->imsg
[1] - '0') - 1;
341 isdnloop_parse_status(p
, ch
, card
);
344 if (!strncmp(p
, "DRV1.", 5)) {
345 printk(KERN_INFO
"isdnloop: (%s) %s\n", CID
, p
);
346 if (!strncmp(p
+ 7, "TC", 2)) {
347 card
->ptype
= ISDN_PTYPE_1TR6
;
348 card
->interface
.features
|= ISDN_FEATURE_P_1TR6
;
350 "isdnloop: (%s) 1TR6-Protocol loaded and running\n", CID
);
352 if (!strncmp(p
+ 7, "EC", 2)) {
353 card
->ptype
= ISDN_PTYPE_EURO
;
354 card
->interface
.features
|= ISDN_FEATURE_P_EURO
;
356 "isdnloop: (%s) Euro-Protocol loaded and running\n", CID
);
365 cmd
.command
= ISDN_STAT_STAVAIL
;
366 cmd
.driver
= card
->myid
;
368 card
->interface
.statcallb(&cmd
);
370 if (card
->flags
& (ISDNLOOP_FLAGS_B1ACTIVE
| ISDNLOOP_FLAGS_B2ACTIVE
))
371 if (!(card
->flags
& ISDNLOOP_FLAGS_RBTIMER
)) {
372 /* schedule b-channel polling */
373 card
->flags
|= ISDNLOOP_FLAGS_RBTIMER
;
374 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
375 del_timer(&card
->rb_timer
);
376 card
->rb_timer
.function
= isdnloop_pollbchan
;
377 card
->rb_timer
.data
= (unsigned long) card
;
378 card
->rb_timer
.expires
= jiffies
+ ISDNLOOP_TIMER_BCREAD
;
379 add_timer(&card
->rb_timer
);
380 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
383 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
384 card
->st_timer
.expires
= jiffies
+ ISDNLOOP_TIMER_DCREAD
;
385 add_timer(&card
->st_timer
);
386 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
390 * Append a packet to the transmit buffer-queue.
393 * channel = Number of B-channel
394 * skb = packet to send.
395 * card = pointer to card-struct
397 * Number of bytes transferred, -E??? on error
400 isdnloop_sendbuf(int channel
, struct sk_buff
*skb
, isdnloop_card
* card
)
404 struct sk_buff
*nskb
;
408 "isdnloop: Send packet too large\n");
412 if (!(card
->flags
& (channel
) ? ISDNLOOP_FLAGS_B2ACTIVE
: ISDNLOOP_FLAGS_B1ACTIVE
))
414 if (card
->sndcount
[channel
] > ISDNLOOP_MAX_SQUEUE
)
416 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
417 nskb
= dev_alloc_skb(skb
->len
);
419 skb_copy_from_linear_data(skb
,
420 skb_put(nskb
, len
), len
);
421 skb_queue_tail(&card
->bqueue
[channel
], nskb
);
425 card
->sndcount
[channel
] += len
;
426 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
432 * Read the messages from the card's ringbuffer
435 * buf = pointer to buffer.
436 * len = number of bytes to read.
437 * user = flag, 1: called from userlevel 0: called from kernel.
438 * card = pointer to card struct.
440 * number of bytes actually transferred.
443 isdnloop_readstatus(u_char __user
*buf
, int len
, isdnloop_card
* card
)
448 for (p
= buf
, count
= 0; count
< len
; p
++, count
++) {
449 if (card
->msg_buf_read
== card
->msg_buf_write
)
451 if (put_user(*card
->msg_buf_read
++, p
))
453 if (card
->msg_buf_read
> card
->msg_buf_end
)
454 card
->msg_buf_read
= card
->msg_buf
;
460 * Simulate a card's response by appending it to the cards
464 * card = pointer to card struct.
465 * s = pointer to message-string.
466 * ch = channel: 0 = generic messages, 1 and 2 = D-channel messages.
468 * 0 on success, 1 on memory squeeze.
471 isdnloop_fake(isdnloop_card
* card
, char *s
, int ch
)
474 int len
= strlen(s
) + ((ch
>= 0) ? 3 : 0);
476 if (!(skb
= dev_alloc_skb(len
))) {
477 printk(KERN_WARNING
"isdnloop: Out of memory in isdnloop_fake\n");
481 sprintf(skb_put(skb
, 3), "%02d;", ch
);
482 memcpy(skb_put(skb
, strlen(s
)), s
, strlen(s
));
483 skb_queue_tail(&card
->dqueue
, skb
);
487 static isdnloop_stat isdnloop_cmd_table
[] =
489 {"BCON_R", 0, 1}, /* B-Channel connect */
490 {"BCON_I", 0, 17}, /* B-Channel connect ind */
491 {"BDIS_R", 0, 2}, /* B-Channel disconnect */
492 {"DDIS_R", 0, 3}, /* D-Channel disconnect */
493 {"DCON_R", 0, 16}, /* D-Channel connect */
494 {"DSCA_R", 0, 4}, /* Dial 1TR6-SPV */
495 {"DCAL_R", 0, 5}, /* Dial */
496 {"EAZC", 0, 6}, /* Clear EAZ listener */
497 {"EAZ", 0, 7}, /* Set EAZ listener */
498 {"SEEAZ", 0, 8}, /* Get EAZ listener */
499 {"MSN", 0, 9}, /* Set/Clear MSN listener */
500 {"MSALL", 0, 10}, /* Set multi MSN listeners */
501 {"SETSIL", 0, 11}, /* Set SI list */
502 {"SEESIL", 0, 12}, /* Get SI list */
503 {"SILC", 0, 13}, /* Clear SI list */
504 {"LOCK", 0, -1}, /* LOCK channel */
505 {"UNLOCK", 0, -1}, /* UNLOCK channel */
506 {"FV2ON", 1, 14}, /* Leased mode on */
507 {"FV2OFF", 1, 15}, /* Leased mode off */
514 * Simulate an error-response from a card.
517 * card = pointer to card struct.
520 isdnloop_fake_err(isdnloop_card
* card
)
524 sprintf(buf
, "E%s", card
->omsg
);
525 isdnloop_fake(card
, buf
, -1);
526 isdnloop_fake(card
, "NAK", -1);
529 static u_char ctable_eu
[] =
530 {0x00, 0x11, 0x01, 0x12};
531 static u_char ctable_1t
[] =
532 {0x00, 0x3b, 0x01, 0x3a};
535 * Assemble a simplified cause message depending on the
536 * D-channel protocol used.
539 * card = pointer to card struct.
540 * loc = location: 0 = local, 1 = remote.
541 * cau = cause: 1 = busy, 2 = nonexistent callerid, 3 = no user responding.
543 * Pointer to buffer containing the assembled message.
546 isdnloop_unicause(isdnloop_card
* card
, int loc
, int cau
)
550 switch (card
->ptype
) {
551 case ISDN_PTYPE_EURO
:
552 sprintf(buf
, "E%02X%02X", (loc
) ? 4 : 2, ctable_eu
[cau
]);
554 case ISDN_PTYPE_1TR6
:
555 sprintf(buf
, "%02X44", ctable_1t
[cau
]);
564 * Release a virtual connection. Called from timer interrupt, when
565 * called party did not respond.
568 * card = pointer to card struct.
569 * ch = channel (0-based)
572 isdnloop_atimeout(isdnloop_card
* card
, int ch
)
577 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
579 isdnloop_fake(card
->rcard
[ch
], "DDIS_I", card
->rch
[ch
] + 1);
580 card
->rcard
[ch
]->rcard
[card
->rch
[ch
]] = NULL
;
581 card
->rcard
[ch
] = NULL
;
583 isdnloop_fake(card
, "DDIS_I", ch
+ 1);
584 /* No user responding */
585 sprintf(buf
, "CAU%s", isdnloop_unicause(card
, 1, 3));
586 isdnloop_fake(card
, buf
, ch
+ 1);
587 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
591 * Wrapper for isdnloop_atimeout().
594 isdnloop_atimeout0(unsigned long data
)
596 isdnloop_card
*card
= (isdnloop_card
*) data
;
597 isdnloop_atimeout(card
, 0);
601 * Wrapper for isdnloop_atimeout().
604 isdnloop_atimeout1(unsigned long data
)
606 isdnloop_card
*card
= (isdnloop_card
*) data
;
607 isdnloop_atimeout(card
, 1);
611 * Install a watchdog for a user, not responding.
614 * card = pointer to card struct.
615 * ch = channel to watch for.
618 isdnloop_start_ctimer(isdnloop_card
* card
, int ch
)
622 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
623 init_timer(&card
->c_timer
[ch
]);
624 card
->c_timer
[ch
].expires
= jiffies
+ ISDNLOOP_TIMER_ALERTWAIT
;
626 card
->c_timer
[ch
].function
= isdnloop_atimeout1
;
628 card
->c_timer
[ch
].function
= isdnloop_atimeout0
;
629 card
->c_timer
[ch
].data
= (unsigned long) card
;
630 add_timer(&card
->c_timer
[ch
]);
631 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
635 * Kill a pending channel watchdog.
638 * card = pointer to card struct.
639 * ch = channel (0-based).
642 isdnloop_kill_ctimer(isdnloop_card
* card
, int ch
)
646 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
647 del_timer(&card
->c_timer
[ch
]);
648 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
651 static u_char si2bit
[] =
652 {0, 1, 0, 0, 0, 2, 0, 4, 0, 0};
653 static u_char bit2si
[] =
657 * Try finding a listener for an outgoing call.
660 * card = pointer to calling card.
661 * p = pointer to ICN-type setup-string.
662 * lch = channel of calling card.
663 * cmd = pointer to struct to be filled when parsing setup.
665 * 0 = found match, alerting should happen.
666 * 1 = found matching number but it is busy.
667 * 2 = no matching listener.
668 * 3 = found matching number but SI does not match.
671 isdnloop_try_call(isdnloop_card
* card
, char *p
, int lch
, isdn_ctrl
* cmd
)
673 isdnloop_card
*cc
= cards
;
681 isdnloop_parse_setup(p
, cmd
);
683 for (ch
= 0; ch
< 2; ch
++) {
684 /* Exclude ourself */
685 if ((cc
== card
) && (ch
== lch
))
689 case ISDN_PTYPE_EURO
:
690 for (i
= 0; i
< 3; i
++)
691 if (!(strcmp(cc
->s0num
[i
], cmd
->parm
.setup
.phone
)))
694 case ISDN_PTYPE_1TR6
:
697 sprintf(nbuf
, "%s%c", cc
->s0num
[0], *e
);
698 if (!(strcmp(nbuf
, cmd
->parm
.setup
.phone
)))
704 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
706 if (!(cc
->rcard
[ch
])) {
708 if (!(si2bit
[cmd
->parm
.setup
.si1
] & cc
->sil
[ch
])) {
709 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
712 /* ch is idle, si and number matches */
713 cc
->rcard
[ch
] = card
;
715 card
->rcard
[lch
] = cc
;
717 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
720 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
721 /* num matches, but busy */
733 * Depending on D-channel protocol and caller/called, modify
737 * card = pointer to card struct.
738 * phone = pointer phone number.
739 * caller = flag: 1 = caller, 0 = called.
741 * pointer to new phone number.
744 isdnloop_vstphone(isdnloop_card
* card
, char *phone
, int caller
)
747 static char nphone
[30];
753 switch (card
->ptype
) {
754 case ISDN_PTYPE_EURO
:
756 for (i
= 0; i
< 2; i
++)
757 if (!(strcmp(card
->s0num
[i
], phone
)))
759 return (card
->s0num
[0]);
763 case ISDN_PTYPE_1TR6
:
765 sprintf(nphone
, "%s%c", card
->s0num
[0], phone
[0]);
768 return (&phone
[strlen(phone
) - 1]);
775 * Parse an ICN-type command string sent to the 'card'.
776 * Perform misc. actions depending on the command.
779 * card = pointer to card struct.
782 isdnloop_parse_cmd(isdnloop_card
* card
)
784 char *p
= card
->omsg
;
787 isdnloop_stat
*s
= isdnloop_cmd_table
;
792 if ((card
->omsg
[0] != '0') && (card
->omsg
[2] != ';')) {
793 isdnloop_fake_err(card
);
796 ch
= card
->omsg
[1] - '0';
797 if ((ch
< 0) || (ch
> 2)) {
798 isdnloop_fake_err(card
);
803 if (!strncmp(p
, s
->statstr
, strlen(s
->statstr
))) {
805 if (s
->command
&& (ch
!= 0)) {
806 isdnloop_fake_err(card
);
818 if (card
->rcard
[ch
- 1]) {
819 isdnloop_fake(card
->rcard
[ch
- 1], "BCON_I",
820 card
->rch
[ch
- 1] + 1);
821 isdnloop_fake(card
, "BCON_C", ch
);
826 if (card
->rcard
[ch
- 1]) {
827 isdnloop_fake(card
->rcard
[ch
- 1], "BCON_C",
828 card
->rch
[ch
- 1] + 1);
833 isdnloop_fake(card
, "BDIS_C", ch
);
834 if (card
->rcard
[ch
- 1]) {
835 isdnloop_fake(card
->rcard
[ch
- 1], "BDIS_I",
836 card
->rch
[ch
- 1] + 1);
841 isdnloop_kill_ctimer(card
, ch
- 1);
842 if (card
->rcard
[ch
- 1]) {
843 isdnloop_kill_ctimer(card
->rcard
[ch
- 1], card
->rch
[ch
- 1]);
844 isdnloop_fake(card
->rcard
[ch
- 1], "DCON_C",
845 card
->rch
[ch
- 1] + 1);
846 isdnloop_fake(card
, "DCON_C", ch
);
851 isdnloop_kill_ctimer(card
, ch
- 1);
852 if (card
->rcard
[ch
- 1]) {
853 isdnloop_kill_ctimer(card
->rcard
[ch
- 1], card
->rch
[ch
- 1]);
854 isdnloop_fake(card
->rcard
[ch
- 1], "DDIS_I",
855 card
->rch
[ch
- 1] + 1);
856 card
->rcard
[ch
- 1] = NULL
;
858 isdnloop_fake(card
, "DDIS_C", ch
);
861 /* 0x;DSCA_Rdd,yy,zz,oo */
862 if (card
->ptype
!= ISDN_PTYPE_1TR6
) {
863 isdnloop_fake_err(card
);
868 /* 0x;DCAL_Rdd,yy,zz,oo */
870 switch (isdnloop_try_call(card
, p
, ch
- 1, &cmd
)) {
873 sprintf(buf
, "D%s_I%s,%02d,%02d,%s",
874 (action
== 4) ? "SCA" : "CAL",
875 isdnloop_vstphone(card
, cmd
.parm
.setup
.eazmsn
, 1),
878 isdnloop_vstphone(card
->rcard
[ch
- 1],
879 cmd
.parm
.setup
.phone
, 0));
880 isdnloop_fake(card
->rcard
[ch
- 1], buf
, card
->rch
[ch
- 1] + 1);
883 /* si1 does not match, don't alert but start timer */
884 isdnloop_start_ctimer(card
, ch
- 1);
888 isdnloop_fake(card
, "DDIS_I", ch
);
889 sprintf(buf
, "CAU%s", isdnloop_unicause(card
, 1, 1));
890 isdnloop_fake(card
, buf
, ch
);
894 isdnloop_fake(card
, "DDIS_I", ch
);
895 sprintf(buf
, "CAU%s", isdnloop_unicause(card
, 1, 2));
896 isdnloop_fake(card
, buf
, ch
);
902 card
->eazlist
[ch
- 1][0] = '\0';
907 strcpy(card
->eazlist
[ch
- 1], p
);
911 sprintf(buf
, "EAZ-LIST: %s", card
->eazlist
[ch
- 1]);
912 isdnloop_fake(card
, buf
, ch
+ 1);
924 while (strchr("0157", *p
)) {
926 card
->sil
[ch
- 1] |= si2bit
[*p
- '0'];
930 isdnloop_fake_err(card
);
934 sprintf(buf
, "SIN-LIST: ");
936 for (i
= 0; i
< 3; i
++)
937 if (card
->sil
[ch
- 1] & (1 << i
))
938 p
+= sprintf(p
, "%02d", bit2si
[i
]);
939 isdnloop_fake(card
, buf
, ch
+ 1);
943 card
->sil
[ch
- 1] = 0;
955 * Put command-strings into the of the 'card'. In reality, execute them
956 * right in place by calling isdnloop_parse_cmd(). Also copy every
957 * command to the read message ringbuffer, preceding it with a '>'.
958 * These mesagges can be read at /dev/isdnctrl.
961 * buf = pointer to command buffer.
962 * len = length of buffer data.
963 * user = flag: 1 = called form userlevel, 0 called from kernel.
964 * card = pointer to card struct.
966 * number of bytes transferred (currently always equals len).
969 isdnloop_writecmd(const u_char
* buf
, int len
, int user
, isdnloop_card
* card
)
983 if (copy_from_user(msg
, buf
, count
))
986 memcpy(msg
, buf
, count
);
987 isdnloop_putmsg(card
, '>');
988 for (p
= msg
; count
> 0; count
--, p
++) {
991 isdnloop_putmsg(card
, *p
);
992 card
->omsg
[card
->optr
] = *p
;
994 card
->omsg
[card
->optr
] = '\0';
996 isdnloop_parse_cmd(card
);
998 isdnloop_putmsg(card
, '>');
1002 if (card
->optr
< 59)
1008 cmd
.command
= ISDN_STAT_STAVAIL
;
1009 cmd
.driver
= card
->myid
;
1011 card
->interface
.statcallb(&cmd
);
1016 * Delete card's pending timers, send STOP to linklevel
1019 isdnloop_stopcard(isdnloop_card
* card
)
1021 unsigned long flags
;
1024 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
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
);
1035 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
1039 * Stop all cards before unload.
1042 isdnloop_stopallcards(void)
1044 isdnloop_card
*p
= cards
;
1047 isdnloop_stopcard(p
);
1053 * Start a 'card'. Simulate card's boot message and set the phone
1054 * number(s) of the virtual 'S0-Interface'. Install D-channel
1058 * card = pointer to card struct.
1059 * sdefp = pointer to struct holding ioctl parameters.
1061 * 0 on success, -E??? otherwise.
1064 isdnloop_start(isdnloop_card
* card
, isdnloop_sdef
* sdefp
)
1066 unsigned long flags
;
1070 if (card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1072 if (copy_from_user((char *) &sdef
, (char *) sdefp
, sizeof(sdef
)))
1074 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
1075 switch (sdef
.ptype
) {
1076 case ISDN_PTYPE_EURO
:
1077 if (isdnloop_fake(card
, "DRV1.23EC-Q.931-CAPI-CNS-BASIS-20.02.96",
1079 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
1082 card
->sil
[0] = card
->sil
[1] = 4;
1083 if (isdnloop_fake(card
, "TEI OK", 0)) {
1084 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
1087 for (i
= 0; i
< 3; i
++)
1088 strcpy(card
->s0num
[i
], sdef
.num
[i
]);
1090 case ISDN_PTYPE_1TR6
:
1091 if (isdnloop_fake(card
, "DRV1.04TC-1TR6-CAPI-CNS-BASIS-29.11.95",
1093 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
1096 card
->sil
[0] = card
->sil
[1] = 4;
1097 if (isdnloop_fake(card
, "TEI OK", 0)) {
1098 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
1101 strcpy(card
->s0num
[0], sdef
.num
[0]);
1102 card
->s0num
[1][0] = '\0';
1103 card
->s0num
[2][0] = '\0';
1106 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
1107 printk(KERN_WARNING
"isdnloop: Illegal D-channel protocol %d\n",
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 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
1122 * Main handler for commands sent by linklevel.
1125 isdnloop_command(isdn_ctrl
* c
, isdnloop_card
* card
)
1133 switch (c
->command
) {
1134 case ISDN_CMD_IOCTL
:
1135 memcpy(&a
, c
->parm
.num
, sizeof(ulong
));
1137 case ISDNLOOP_IOCTL_DEBUGVAR
:
1138 return (ulong
) card
;
1139 case ISDNLOOP_IOCTL_STARTUP
:
1140 if (!access_ok(VERIFY_READ
, (void *) a
, sizeof(isdnloop_sdef
)))
1142 return (isdnloop_start(card
, (isdnloop_sdef
*) a
));
1144 case ISDNLOOP_IOCTL_ADDCARD
:
1145 if (copy_from_user((char *)&cdef
,
1149 return (isdnloop_addcard(cdef
.id1
));
1151 case ISDNLOOP_IOCTL_LEASEDCFG
:
1153 if (!card
->leased
) {
1155 while (card
->ptype
== ISDN_PTYPE_UNKNOWN
)
1156 schedule_timeout_interruptible(10);
1157 schedule_timeout_interruptible(10);
1158 sprintf(cbuf
, "00;FV2ON\n01;EAZ1\n02;EAZ2\n");
1159 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1161 "isdnloop: (%s) Leased-line mode enabled\n",
1163 cmd
.command
= ISDN_STAT_RUN
;
1164 cmd
.driver
= card
->myid
;
1166 card
->interface
.statcallb(&cmd
);
1171 sprintf(cbuf
, "00;FV2OFF\n");
1172 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1174 "isdnloop: (%s) Leased-line mode disabled\n",
1176 cmd
.command
= ISDN_STAT_RUN
;
1177 cmd
.driver
= card
->myid
;
1179 card
->interface
.statcallb(&cmd
);
1188 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1192 if ((c
->arg
& 255) < ISDNLOOP_BCH
) {
1198 p
= c
->parm
.setup
.phone
;
1199 if (*p
== 's' || *p
== 'S') {
1202 strcpy(dcode
, "SCA");
1205 strcpy(dcode
, "CAL");
1207 sprintf(cbuf
, "%02d;D%s_R%s,%02d,%02d,%s\n", (int) (a
+ 1),
1208 dcode
, dial
, c
->parm
.setup
.si1
,
1209 c
->parm
.setup
.si2
, c
->parm
.setup
.eazmsn
);
1210 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1213 case ISDN_CMD_ACCEPTD
:
1214 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1216 if (c
->arg
< ISDNLOOP_BCH
) {
1219 switch (card
->l2_proto
[a
- 1]) {
1220 case ISDN_PROTO_L2_X75I
:
1221 sprintf(cbuf
, "%02d;BX75\n", (int) a
);
1223 #ifdef CONFIG_ISDN_X25
1224 case ISDN_PROTO_L2_X25DTE
:
1225 sprintf(cbuf
, "%02d;BX2T\n", (int) a
);
1227 case ISDN_PROTO_L2_X25DCE
:
1228 sprintf(cbuf
, "%02d;BX2C\n", (int) a
);
1231 case ISDN_PROTO_L2_HDLC
:
1232 sprintf(cbuf
, "%02d;BTRA\n", (int) a
);
1236 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1237 sprintf(cbuf
, "%02d;DCON_R\n", (int) a
);
1238 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1241 case ISDN_CMD_ACCEPTB
:
1242 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1244 if (c
->arg
< ISDNLOOP_BCH
) {
1246 switch (card
->l2_proto
[a
- 1]) {
1247 case ISDN_PROTO_L2_X75I
:
1248 sprintf(cbuf
, "%02d;BCON_R,BX75\n", (int) a
);
1250 #ifdef CONFIG_ISDN_X25
1251 case ISDN_PROTO_L2_X25DTE
:
1252 sprintf(cbuf
, "%02d;BCON_R,BX2T\n", (int) a
);
1254 case ISDN_PROTO_L2_X25DCE
:
1255 sprintf(cbuf
, "%02d;BCON_R,BX2C\n", (int) a
);
1258 case ISDN_PROTO_L2_HDLC
:
1259 sprintf(cbuf
, "%02d;BCON_R,BTRA\n", (int) a
);
1262 sprintf(cbuf
, "%02d;BCON_R\n", (int) a
);
1264 printk(KERN_DEBUG
"isdnloop writecmd '%s'\n", cbuf
);
1265 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1267 case ISDN_CMD_HANGUP
:
1268 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1270 if (c
->arg
< ISDNLOOP_BCH
) {
1272 sprintf(cbuf
, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a
, (int) a
);
1273 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1276 case ISDN_CMD_SETEAZ
:
1277 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1281 if (c
->arg
< ISDNLOOP_BCH
) {
1283 if (card
->ptype
== ISDN_PTYPE_EURO
) {
1284 sprintf(cbuf
, "%02d;MS%s%s\n", (int) a
,
1285 c
->parm
.num
[0] ? "N" : "ALL", c
->parm
.num
);
1287 sprintf(cbuf
, "%02d;EAZ%s\n", (int) a
,
1288 c
->parm
.num
[0] ? c
->parm
.num
: (u_char
*) "0123456789");
1289 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1292 case ISDN_CMD_CLREAZ
:
1293 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1297 if (c
->arg
< ISDNLOOP_BCH
) {
1299 if (card
->ptype
== ISDN_PTYPE_EURO
)
1300 sprintf(cbuf
, "%02d;MSNC\n", (int) a
);
1302 sprintf(cbuf
, "%02d;EAZC\n", (int) a
);
1303 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1306 case ISDN_CMD_SETL2
:
1307 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1309 if ((c
->arg
& 255) < ISDNLOOP_BCH
) {
1312 case ISDN_PROTO_L2_X75I
:
1313 sprintf(cbuf
, "%02d;BX75\n", (int) (a
& 255) + 1);
1315 #ifdef CONFIG_ISDN_X25
1316 case ISDN_PROTO_L2_X25DTE
:
1317 sprintf(cbuf
, "%02d;BX2T\n", (int) (a
& 255) + 1);
1319 case ISDN_PROTO_L2_X25DCE
:
1320 sprintf(cbuf
, "%02d;BX2C\n", (int) (a
& 255) + 1);
1323 case ISDN_PROTO_L2_HDLC
:
1324 sprintf(cbuf
, "%02d;BTRA\n", (int) (a
& 255) + 1);
1326 case ISDN_PROTO_L2_TRANS
:
1327 sprintf(cbuf
, "%02d;BTRA\n", (int) (a
& 255) + 1);
1332 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1333 card
->l2_proto
[a
& 255] = (a
>> 8);
1336 case ISDN_CMD_SETL3
:
1337 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1348 * Find card with given driverId
1350 static inline isdnloop_card
*
1351 isdnloop_findcard(int driverid
)
1353 isdnloop_card
*p
= cards
;
1356 if (p
->myid
== driverid
)
1360 return (isdnloop_card
*) 0;
1364 * Wrapper functions for interface to linklevel
1367 if_command(isdn_ctrl
* c
)
1369 isdnloop_card
*card
= isdnloop_findcard(c
->driver
);
1372 return (isdnloop_command(c
, card
));
1374 "isdnloop: if_command called with invalid driverId!\n");
1379 if_writecmd(const u_char __user
*buf
, int len
, int id
, int channel
)
1381 isdnloop_card
*card
= isdnloop_findcard(id
);
1384 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1386 return (isdnloop_writecmd(buf
, len
, 1, card
));
1389 "isdnloop: if_writecmd called with invalid driverId!\n");
1394 if_readstatus(u_char __user
*buf
, int len
, int id
, int channel
)
1396 isdnloop_card
*card
= isdnloop_findcard(id
);
1399 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1401 return (isdnloop_readstatus(buf
, len
, card
));
1404 "isdnloop: if_readstatus called with invalid driverId!\n");
1409 if_sendbuf(int id
, int channel
, int ack
, struct sk_buff
*skb
)
1411 isdnloop_card
*card
= isdnloop_findcard(id
);
1414 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1416 /* ack request stored in skb scratch area */
1418 return (isdnloop_sendbuf(channel
, skb
, card
));
1421 "isdnloop: if_sendbuf called with invalid driverId!\n");
1426 * Allocate a new card-struct, initialize it
1427 * link it into cards-list and register it at linklevel.
1429 static isdnloop_card
*
1430 isdnloop_initcard(char *id
)
1432 isdnloop_card
*card
;
1435 if (!(card
= kzalloc(sizeof(isdnloop_card
), GFP_KERNEL
))) {
1437 "isdnloop: (%s) Could not allocate card-struct.\n", id
);
1438 return (isdnloop_card
*) 0;
1440 card
->interface
.owner
= THIS_MODULE
;
1441 card
->interface
.channels
= ISDNLOOP_BCH
;
1442 card
->interface
.hl_hdrlen
= 1; /* scratch area for storing ack flag*/
1443 card
->interface
.maxbufsize
= 4000;
1444 card
->interface
.command
= if_command
;
1445 card
->interface
.writebuf_skb
= if_sendbuf
;
1446 card
->interface
.writecmd
= if_writecmd
;
1447 card
->interface
.readstat
= if_readstatus
;
1448 card
->interface
.features
= ISDN_FEATURE_L2_X75I
|
1449 #ifdef CONFIG_ISDN_X25
1450 ISDN_FEATURE_L2_X25DTE
|
1451 ISDN_FEATURE_L2_X25DCE
|
1453 ISDN_FEATURE_L2_HDLC
|
1454 ISDN_FEATURE_L3_TRANS
|
1455 ISDN_FEATURE_P_UNKNOWN
;
1456 card
->ptype
= ISDN_PTYPE_UNKNOWN
;
1457 strlcpy(card
->interface
.id
, id
, sizeof(card
->interface
.id
));
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
]);
1465 skb_queue_head_init(&card
->dqueue
);
1466 spin_lock_init(&card
->isdnloop_lock
);
1469 if (!register_isdn(&card
->interface
)) {
1470 cards
= cards
->next
;
1472 "isdnloop: Unable to register %s\n", id
);
1474 return (isdnloop_card
*) 0;
1476 card
->myid
= card
->interface
.channels
;
1481 isdnloop_addcard(char *id1
)
1483 isdnloop_card
*card
;
1485 if (!(card
= isdnloop_initcard(id1
))) {
1489 "isdnloop: (%s) virtual card added\n",
1490 card
->interface
.id
);
1500 if ((p
= strchr(revision
, ':'))) {
1502 p
= strchr(rev
, '$');
1505 strcpy(rev
, " ??? ");
1506 printk(KERN_NOTICE
"isdnloop-ISDN-driver Rev%s\n", rev
);
1509 return (isdnloop_addcard(isdnloop_id
));
1518 isdnloop_card
*card
= cards
;
1519 isdnloop_card
*last
;
1522 isdnloop_stopallcards();
1524 cmd
.command
= ISDN_STAT_UNLOAD
;
1525 cmd
.driver
= card
->myid
;
1526 card
->interface
.statcallb(&cmd
);
1527 for (i
= 0; i
< ISDNLOOP_BCH
; i
++)
1528 isdnloop_free_queue(card
, i
);
1534 skb_queue_purge(&card
->dqueue
);
1538 printk(KERN_NOTICE
"isdnloop-ISDN-driver unloaded\n");
1541 module_init(isdnloop_init
);
1542 module_exit(isdnloop_exit
);