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 *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 */
103 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
104 card
->rb_timer
.expires
= jiffies
+ ISDNLOOP_TIMER_BCREAD
;
105 add_timer(&card
->rb_timer
);
106 card
->flags
|= ISDNLOOP_FLAGS_RBTIMER
;
107 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
109 card
->flags
&= ~ISDNLOOP_FLAGS_RBTIMER
;
113 * Parse ICN-type setup string and fill fields of setup-struct
117 * setup = setup string, format: [caller-id],si1,si2,[called-id]
118 * cmd = pointer to struct to be filled.
121 isdnloop_parse_setup(char *setup
, isdn_ctrl
*cmd
)
124 char *s
= strchr(t
, ',');
127 strlcpy(cmd
->parm
.setup
.phone
, t
, sizeof(cmd
->parm
.setup
.phone
));
128 s
= strchr(t
= s
, ',');
131 cmd
->parm
.setup
.si1
= 0;
133 cmd
->parm
.setup
.si1
= simple_strtoul(t
, NULL
, 10);
134 s
= strchr(t
= s
, ',');
137 cmd
->parm
.setup
.si2
= 0;
139 cmd
->parm
.setup
.si2
=
140 simple_strtoul(t
, NULL
, 10);
141 strlcpy(cmd
->parm
.setup
.eazmsn
, s
, sizeof(cmd
->parm
.setup
.eazmsn
));
142 cmd
->parm
.setup
.plan
= 0;
143 cmd
->parm
.setup
.screen
= 0;
146 typedef struct isdnloop_stat
{
152 static isdnloop_stat isdnloop_stat_table
[] =
154 {"BCON_", ISDN_STAT_BCONN
, 1}, /* B-Channel connected */
155 {"BDIS_", ISDN_STAT_BHUP
, 2}, /* B-Channel disconnected */
156 {"DCON_", ISDN_STAT_DCONN
, 0}, /* D-Channel connected */
157 {"DDIS_", ISDN_STAT_DHUP
, 0}, /* D-Channel disconnected */
158 {"DCAL_I", ISDN_STAT_ICALL
, 3}, /* Incoming call dialup-line */
159 {"DSCA_I", ISDN_STAT_ICALL
, 3}, /* Incoming call 1TR6-SPV */
160 {"FCALL", ISDN_STAT_ICALL
, 4}, /* Leased line connection up */
161 {"CIF", ISDN_STAT_CINF
, 5}, /* Charge-info, 1TR6-type */
162 {"AOC", ISDN_STAT_CINF
, 6}, /* Charge-info, DSS1-type */
163 {"CAU", ISDN_STAT_CAUSE
, 7}, /* Cause code */
164 {"TEI OK", ISDN_STAT_RUN
, 0}, /* Card connected to wallplug */
165 {"E_L1: ACT FAIL", ISDN_STAT_BHUP
, 8}, /* Layer-1 activation failed */
166 {"E_L2: DATA LIN", ISDN_STAT_BHUP
, 8}, /* Layer-2 data link lost */
167 {"E_L1: ACTIVATION FAILED",
168 ISDN_STAT_BHUP
, 8}, /* Layer-1 activation failed */
175 * Parse Status message-strings from virtual card.
176 * Depending on status, call statcallb for sending messages to upper
177 * levels. Also set/reset B-Channel active-flags.
180 * status = status string to parse.
181 * channel = channel where message comes from.
182 * card = card where message comes from.
185 isdnloop_parse_status(u_char
*status
, int channel
, isdnloop_card
*card
)
187 isdnloop_stat
*s
= isdnloop_stat_table
;
192 if (!strncmp(status
, s
->statstr
, strlen(s
->statstr
))) {
193 cmd
.command
= s
->command
;
201 cmd
.driver
= card
->myid
;
206 card
->flags
|= (channel
) ?
207 ISDNLOOP_FLAGS_B2ACTIVE
: ISDNLOOP_FLAGS_B1ACTIVE
;
211 card
->flags
&= ~((channel
) ?
212 ISDNLOOP_FLAGS_B2ACTIVE
: ISDNLOOP_FLAGS_B1ACTIVE
);
213 isdnloop_free_queue(card
, channel
);
216 /* DCAL_I and DSCA_I */
217 isdnloop_parse_setup(status
+ 6, &cmd
);
221 sprintf(cmd
.parm
.setup
.phone
, "LEASED%d", card
->myid
);
222 sprintf(cmd
.parm
.setup
.eazmsn
, "%d", channel
+ 1);
223 cmd
.parm
.setup
.si1
= 7;
224 cmd
.parm
.setup
.si2
= 0;
225 cmd
.parm
.setup
.plan
= 0;
226 cmd
.parm
.setup
.screen
= 0;
230 strlcpy(cmd
.parm
.num
, status
+ 3, sizeof(cmd
.parm
.num
));
234 snprintf(cmd
.parm
.num
, sizeof(cmd
.parm
.num
), "%d",
235 (int) simple_strtoul(status
+ 7, NULL
, 16));
240 if (strlen(status
) == 4)
241 snprintf(cmd
.parm
.num
, sizeof(cmd
.parm
.num
), "%s%c%c",
242 status
+ 2, *status
, *(status
+ 1));
244 strlcpy(cmd
.parm
.num
, status
+ 1, sizeof(cmd
.parm
.num
));
247 /* Misc Errors on L1 and L2 */
248 card
->flags
&= ~ISDNLOOP_FLAGS_B1ACTIVE
;
249 isdnloop_free_queue(card
, 0);
251 cmd
.driver
= card
->myid
;
252 card
->interface
.statcallb(&cmd
);
253 cmd
.command
= ISDN_STAT_DHUP
;
255 cmd
.driver
= card
->myid
;
256 card
->interface
.statcallb(&cmd
);
257 cmd
.command
= ISDN_STAT_BHUP
;
258 card
->flags
&= ~ISDNLOOP_FLAGS_B2ACTIVE
;
259 isdnloop_free_queue(card
, 1);
261 cmd
.driver
= card
->myid
;
262 card
->interface
.statcallb(&cmd
);
263 cmd
.command
= ISDN_STAT_DHUP
;
265 cmd
.driver
= card
->myid
;
268 card
->interface
.statcallb(&cmd
);
272 * Store a cwcharacter into ringbuffer for reading from /dev/isdnctrl
275 * card = pointer to card struct.
279 isdnloop_putmsg(isdnloop_card
*card
, unsigned char c
)
283 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
284 *card
->msg_buf_write
++ = (c
== 0xff) ? '\n' : c
;
285 if (card
->msg_buf_write
== card
->msg_buf_read
) {
286 if (++card
->msg_buf_read
> card
->msg_buf_end
)
287 card
->msg_buf_read
= card
->msg_buf
;
289 if (card
->msg_buf_write
> card
->msg_buf_end
)
290 card
->msg_buf_write
= card
->msg_buf
;
291 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
295 * Poll a virtual cards message queue.
296 * If there are new status-replies from the card, copy them to
297 * ringbuffer for reading on /dev/isdnctrl and call
298 * isdnloop_parse_status() for processing them. Watch for special
299 * Firmware bootmessage and parse it, to get the D-Channel protocol.
300 * If there are B-Channels open, initiate a timer-callback to
301 * isdnloop_pollbchan().
302 * This routine is called periodically via timer interrupt.
305 * data = pointer to card struct
308 isdnloop_polldchan(unsigned long data
)
310 isdnloop_card
*card
= (isdnloop_card
*) data
;
320 if ((skb
= skb_dequeue(&card
->dqueue
)))
324 for (left
= avail
; left
> 0; left
--) {
327 isdnloop_putmsg(card
, c
);
328 card
->imsg
[card
->iptr
] = c
;
333 isdnloop_putmsg(card
, '\n');
334 card
->imsg
[card
->iptr
] = 0;
336 if (card
->imsg
[0] == '0' && card
->imsg
[1] >= '0' &&
337 card
->imsg
[1] <= '2' && card
->imsg
[2] == ';') {
338 ch
= (card
->imsg
[1] - '0') - 1;
340 isdnloop_parse_status(p
, ch
, card
);
343 if (!strncmp(p
, "DRV1.", 5)) {
344 printk(KERN_INFO
"isdnloop: (%s) %s\n", CID
, p
);
345 if (!strncmp(p
+ 7, "TC", 2)) {
346 card
->ptype
= ISDN_PTYPE_1TR6
;
347 card
->interface
.features
|= ISDN_FEATURE_P_1TR6
;
349 "isdnloop: (%s) 1TR6-Protocol loaded and running\n", CID
);
351 if (!strncmp(p
+ 7, "EC", 2)) {
352 card
->ptype
= ISDN_PTYPE_EURO
;
353 card
->interface
.features
|= ISDN_FEATURE_P_EURO
;
355 "isdnloop: (%s) Euro-Protocol loaded and running\n", CID
);
364 cmd
.command
= ISDN_STAT_STAVAIL
;
365 cmd
.driver
= card
->myid
;
367 card
->interface
.statcallb(&cmd
);
369 if (card
->flags
& (ISDNLOOP_FLAGS_B1ACTIVE
| ISDNLOOP_FLAGS_B2ACTIVE
))
370 if (!(card
->flags
& ISDNLOOP_FLAGS_RBTIMER
)) {
371 /* schedule b-channel polling */
372 card
->flags
|= ISDNLOOP_FLAGS_RBTIMER
;
373 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
374 del_timer(&card
->rb_timer
);
375 card
->rb_timer
.function
= isdnloop_pollbchan
;
376 card
->rb_timer
.data
= (unsigned long) card
;
377 card
->rb_timer
.expires
= jiffies
+ ISDNLOOP_TIMER_BCREAD
;
378 add_timer(&card
->rb_timer
);
379 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
382 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
383 card
->st_timer
.expires
= jiffies
+ ISDNLOOP_TIMER_DCREAD
;
384 add_timer(&card
->st_timer
);
385 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
389 * Append a packet to the transmit buffer-queue.
392 * channel = Number of B-channel
393 * skb = packet to send.
394 * card = pointer to card-struct
396 * Number of bytes transferred, -E??? on error
399 isdnloop_sendbuf(int channel
, struct sk_buff
*skb
, isdnloop_card
*card
)
403 struct sk_buff
*nskb
;
407 "isdnloop: Send packet too large\n");
411 if (!(card
->flags
& (channel
) ? ISDNLOOP_FLAGS_B2ACTIVE
: ISDNLOOP_FLAGS_B1ACTIVE
))
413 if (card
->sndcount
[channel
] > ISDNLOOP_MAX_SQUEUE
)
415 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
416 nskb
= dev_alloc_skb(skb
->len
);
418 skb_copy_from_linear_data(skb
,
419 skb_put(nskb
, len
), len
);
420 skb_queue_tail(&card
->bqueue
[channel
], nskb
);
424 card
->sndcount
[channel
] += len
;
425 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
431 * Read the messages from the card's ringbuffer
434 * buf = pointer to buffer.
435 * len = number of bytes to read.
436 * user = flag, 1: called from userlevel 0: called from kernel.
437 * card = pointer to card struct.
439 * number of bytes actually transferred.
442 isdnloop_readstatus(u_char __user
*buf
, int len
, isdnloop_card
*card
)
447 for (p
= buf
, count
= 0; count
< len
; p
++, count
++) {
448 if (card
->msg_buf_read
== card
->msg_buf_write
)
450 if (put_user(*card
->msg_buf_read
++, p
))
452 if (card
->msg_buf_read
> card
->msg_buf_end
)
453 card
->msg_buf_read
= card
->msg_buf
;
459 * Simulate a card's response by appending it to the cards
463 * card = pointer to card struct.
464 * s = pointer to message-string.
465 * ch = channel: 0 = generic messages, 1 and 2 = D-channel messages.
467 * 0 on success, 1 on memory squeeze.
470 isdnloop_fake(isdnloop_card
*card
, char *s
, int ch
)
473 int len
= strlen(s
) + ((ch
>= 0) ? 3 : 0);
475 if (!(skb
= dev_alloc_skb(len
))) {
476 printk(KERN_WARNING
"isdnloop: Out of memory in isdnloop_fake\n");
480 sprintf(skb_put(skb
, 3), "%02d;", ch
);
481 memcpy(skb_put(skb
, strlen(s
)), s
, strlen(s
));
482 skb_queue_tail(&card
->dqueue
, skb
);
486 static isdnloop_stat isdnloop_cmd_table
[] =
488 {"BCON_R", 0, 1}, /* B-Channel connect */
489 {"BCON_I", 0, 17}, /* B-Channel connect ind */
490 {"BDIS_R", 0, 2}, /* B-Channel disconnect */
491 {"DDIS_R", 0, 3}, /* D-Channel disconnect */
492 {"DCON_R", 0, 16}, /* D-Channel connect */
493 {"DSCA_R", 0, 4}, /* Dial 1TR6-SPV */
494 {"DCAL_R", 0, 5}, /* Dial */
495 {"EAZC", 0, 6}, /* Clear EAZ listener */
496 {"EAZ", 0, 7}, /* Set EAZ listener */
497 {"SEEAZ", 0, 8}, /* Get EAZ listener */
498 {"MSN", 0, 9}, /* Set/Clear MSN listener */
499 {"MSALL", 0, 10}, /* Set multi MSN listeners */
500 {"SETSIL", 0, 11}, /* Set SI list */
501 {"SEESIL", 0, 12}, /* Get SI list */
502 {"SILC", 0, 13}, /* Clear SI list */
503 {"LOCK", 0, -1}, /* LOCK channel */
504 {"UNLOCK", 0, -1}, /* UNLOCK channel */
505 {"FV2ON", 1, 14}, /* Leased mode on */
506 {"FV2OFF", 1, 15}, /* Leased mode off */
513 * Simulate an error-response from a card.
516 * card = pointer to card struct.
519 isdnloop_fake_err(isdnloop_card
*card
)
523 sprintf(buf
, "E%s", card
->omsg
);
524 isdnloop_fake(card
, buf
, -1);
525 isdnloop_fake(card
, "NAK", -1);
528 static u_char ctable_eu
[] =
529 {0x00, 0x11, 0x01, 0x12};
530 static u_char ctable_1t
[] =
531 {0x00, 0x3b, 0x01, 0x3a};
534 * Assemble a simplified cause message depending on the
535 * D-channel protocol used.
538 * card = pointer to card struct.
539 * loc = location: 0 = local, 1 = remote.
540 * cau = cause: 1 = busy, 2 = nonexistent callerid, 3 = no user responding.
542 * Pointer to buffer containing the assembled message.
545 isdnloop_unicause(isdnloop_card
*card
, int loc
, int cau
)
549 switch (card
->ptype
) {
550 case ISDN_PTYPE_EURO
:
551 sprintf(buf
, "E%02X%02X", (loc
) ? 4 : 2, ctable_eu
[cau
]);
553 case ISDN_PTYPE_1TR6
:
554 sprintf(buf
, "%02X44", ctable_1t
[cau
]);
563 * Release a virtual connection. Called from timer interrupt, when
564 * called party did not respond.
567 * card = pointer to card struct.
568 * ch = channel (0-based)
571 isdnloop_atimeout(isdnloop_card
*card
, int ch
)
576 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
578 isdnloop_fake(card
->rcard
[ch
], "DDIS_I", card
->rch
[ch
] + 1);
579 card
->rcard
[ch
]->rcard
[card
->rch
[ch
]] = NULL
;
580 card
->rcard
[ch
] = NULL
;
582 isdnloop_fake(card
, "DDIS_I", ch
+ 1);
583 /* No user responding */
584 sprintf(buf
, "CAU%s", isdnloop_unicause(card
, 1, 3));
585 isdnloop_fake(card
, buf
, ch
+ 1);
586 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
590 * Wrapper for isdnloop_atimeout().
593 isdnloop_atimeout0(unsigned long data
)
595 isdnloop_card
*card
= (isdnloop_card
*) data
;
596 isdnloop_atimeout(card
, 0);
600 * Wrapper for isdnloop_atimeout().
603 isdnloop_atimeout1(unsigned long data
)
605 isdnloop_card
*card
= (isdnloop_card
*) data
;
606 isdnloop_atimeout(card
, 1);
610 * Install a watchdog for a user, not responding.
613 * card = pointer to card struct.
614 * ch = channel to watch for.
617 isdnloop_start_ctimer(isdnloop_card
*card
, int ch
)
621 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
622 init_timer(&card
->c_timer
[ch
]);
623 card
->c_timer
[ch
].expires
= jiffies
+ ISDNLOOP_TIMER_ALERTWAIT
;
625 card
->c_timer
[ch
].function
= isdnloop_atimeout1
;
627 card
->c_timer
[ch
].function
= isdnloop_atimeout0
;
628 card
->c_timer
[ch
].data
= (unsigned long) card
;
629 add_timer(&card
->c_timer
[ch
]);
630 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
634 * Kill a pending channel watchdog.
637 * card = pointer to card struct.
638 * ch = channel (0-based).
641 isdnloop_kill_ctimer(isdnloop_card
*card
, int ch
)
645 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
646 del_timer(&card
->c_timer
[ch
]);
647 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
650 static u_char si2bit
[] =
651 {0, 1, 0, 0, 0, 2, 0, 4, 0, 0};
652 static u_char bit2si
[] =
656 * Try finding a listener for an outgoing call.
659 * card = pointer to calling card.
660 * p = pointer to ICN-type setup-string.
661 * lch = channel of calling card.
662 * cmd = pointer to struct to be filled when parsing setup.
664 * 0 = found match, alerting should happen.
665 * 1 = found matching number but it is busy.
666 * 2 = no matching listener.
667 * 3 = found matching number but SI does not match.
670 isdnloop_try_call(isdnloop_card
*card
, char *p
, int lch
, isdn_ctrl
*cmd
)
672 isdnloop_card
*cc
= cards
;
680 isdnloop_parse_setup(p
, cmd
);
682 for (ch
= 0; ch
< 2; ch
++) {
683 /* Exclude ourself */
684 if ((cc
== card
) && (ch
== lch
))
688 case ISDN_PTYPE_EURO
:
689 for (i
= 0; i
< 3; i
++)
690 if (!(strcmp(cc
->s0num
[i
], cmd
->parm
.setup
.phone
)))
693 case ISDN_PTYPE_1TR6
:
696 sprintf(nbuf
, "%s%c", cc
->s0num
[0], *e
);
697 if (!(strcmp(nbuf
, cmd
->parm
.setup
.phone
)))
703 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
705 if (!(cc
->rcard
[ch
])) {
707 if (!(si2bit
[cmd
->parm
.setup
.si1
] & cc
->sil
[ch
])) {
708 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
711 /* ch is idle, si and number matches */
712 cc
->rcard
[ch
] = card
;
714 card
->rcard
[lch
] = cc
;
716 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
719 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
720 /* num matches, but busy */
732 * Depending on D-channel protocol and caller/called, modify
736 * card = pointer to card struct.
737 * phone = pointer phone number.
738 * caller = flag: 1 = caller, 0 = called.
740 * pointer to new phone number.
743 isdnloop_vstphone(isdnloop_card
*card
, char *phone
, int caller
)
746 static char nphone
[30];
752 switch (card
->ptype
) {
753 case ISDN_PTYPE_EURO
:
755 for (i
= 0; i
< 2; i
++)
756 if (!(strcmp(card
->s0num
[i
], phone
)))
758 return (card
->s0num
[0]);
762 case ISDN_PTYPE_1TR6
:
764 sprintf(nphone
, "%s%c", card
->s0num
[0], phone
[0]);
767 return (&phone
[strlen(phone
) - 1]);
774 * Parse an ICN-type command string sent to the 'card'.
775 * Perform misc. actions depending on the command.
778 * card = pointer to card struct.
781 isdnloop_parse_cmd(isdnloop_card
*card
)
783 char *p
= card
->omsg
;
786 isdnloop_stat
*s
= isdnloop_cmd_table
;
791 if ((card
->omsg
[0] != '0') && (card
->omsg
[2] != ';')) {
792 isdnloop_fake_err(card
);
795 ch
= card
->omsg
[1] - '0';
796 if ((ch
< 0) || (ch
> 2)) {
797 isdnloop_fake_err(card
);
802 if (!strncmp(p
, s
->statstr
, strlen(s
->statstr
))) {
804 if (s
->command
&& (ch
!= 0)) {
805 isdnloop_fake_err(card
);
817 if (card
->rcard
[ch
- 1]) {
818 isdnloop_fake(card
->rcard
[ch
- 1], "BCON_I",
819 card
->rch
[ch
- 1] + 1);
820 isdnloop_fake(card
, "BCON_C", ch
);
825 if (card
->rcard
[ch
- 1]) {
826 isdnloop_fake(card
->rcard
[ch
- 1], "BCON_C",
827 card
->rch
[ch
- 1] + 1);
832 isdnloop_fake(card
, "BDIS_C", ch
);
833 if (card
->rcard
[ch
- 1]) {
834 isdnloop_fake(card
->rcard
[ch
- 1], "BDIS_I",
835 card
->rch
[ch
- 1] + 1);
840 isdnloop_kill_ctimer(card
, ch
- 1);
841 if (card
->rcard
[ch
- 1]) {
842 isdnloop_kill_ctimer(card
->rcard
[ch
- 1], card
->rch
[ch
- 1]);
843 isdnloop_fake(card
->rcard
[ch
- 1], "DCON_C",
844 card
->rch
[ch
- 1] + 1);
845 isdnloop_fake(card
, "DCON_C", ch
);
850 isdnloop_kill_ctimer(card
, ch
- 1);
851 if (card
->rcard
[ch
- 1]) {
852 isdnloop_kill_ctimer(card
->rcard
[ch
- 1], card
->rch
[ch
- 1]);
853 isdnloop_fake(card
->rcard
[ch
- 1], "DDIS_I",
854 card
->rch
[ch
- 1] + 1);
855 card
->rcard
[ch
- 1] = NULL
;
857 isdnloop_fake(card
, "DDIS_C", ch
);
860 /* 0x;DSCA_Rdd,yy,zz,oo */
861 if (card
->ptype
!= ISDN_PTYPE_1TR6
) {
862 isdnloop_fake_err(card
);
867 /* 0x;DCAL_Rdd,yy,zz,oo */
869 switch (isdnloop_try_call(card
, p
, ch
- 1, &cmd
)) {
872 sprintf(buf
, "D%s_I%s,%02d,%02d,%s",
873 (action
== 4) ? "SCA" : "CAL",
874 isdnloop_vstphone(card
, cmd
.parm
.setup
.eazmsn
, 1),
877 isdnloop_vstphone(card
->rcard
[ch
- 1],
878 cmd
.parm
.setup
.phone
, 0));
879 isdnloop_fake(card
->rcard
[ch
- 1], buf
, card
->rch
[ch
- 1] + 1);
882 /* si1 does not match, don't alert but start timer */
883 isdnloop_start_ctimer(card
, ch
- 1);
887 isdnloop_fake(card
, "DDIS_I", ch
);
888 sprintf(buf
, "CAU%s", isdnloop_unicause(card
, 1, 1));
889 isdnloop_fake(card
, buf
, ch
);
893 isdnloop_fake(card
, "DDIS_I", ch
);
894 sprintf(buf
, "CAU%s", isdnloop_unicause(card
, 1, 2));
895 isdnloop_fake(card
, buf
, ch
);
901 card
->eazlist
[ch
- 1][0] = '\0';
906 strcpy(card
->eazlist
[ch
- 1], p
);
910 sprintf(buf
, "EAZ-LIST: %s", card
->eazlist
[ch
- 1]);
911 isdnloop_fake(card
, buf
, ch
+ 1);
923 while (strchr("0157", *p
)) {
925 card
->sil
[ch
- 1] |= si2bit
[*p
- '0'];
929 isdnloop_fake_err(card
);
933 sprintf(buf
, "SIN-LIST: ");
935 for (i
= 0; i
< 3; i
++)
936 if (card
->sil
[ch
- 1] & (1 << i
))
937 p
+= sprintf(p
, "%02d", bit2si
[i
]);
938 isdnloop_fake(card
, buf
, ch
+ 1);
942 card
->sil
[ch
- 1] = 0;
954 * Put command-strings into the of the 'card'. In reality, execute them
955 * right in place by calling isdnloop_parse_cmd(). Also copy every
956 * command to the read message ringbuffer, preceding it with a '>'.
957 * These mesagges can be read at /dev/isdnctrl.
960 * buf = pointer to command buffer.
961 * len = length of buffer data.
962 * user = flag: 1 = called form userlevel, 0 called from kernel.
963 * card = pointer to card struct.
965 * number of bytes transferred (currently always equals len).
968 isdnloop_writecmd(const u_char
*buf
, int len
, int user
, isdnloop_card
*card
)
982 if (copy_from_user(msg
, buf
, count
))
985 memcpy(msg
, buf
, count
);
986 isdnloop_putmsg(card
, '>');
987 for (p
= msg
; count
> 0; count
--, p
++) {
990 isdnloop_putmsg(card
, *p
);
991 card
->omsg
[card
->optr
] = *p
;
993 card
->omsg
[card
->optr
] = '\0';
995 isdnloop_parse_cmd(card
);
997 isdnloop_putmsg(card
, '>');
1001 if (card
->optr
< 59)
1007 cmd
.command
= ISDN_STAT_STAVAIL
;
1008 cmd
.driver
= card
->myid
;
1010 card
->interface
.statcallb(&cmd
);
1015 * Delete card's pending timers, send STOP to linklevel
1018 isdnloop_stopcard(isdnloop_card
*card
)
1020 unsigned long flags
;
1023 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
1024 if (card
->flags
& ISDNLOOP_FLAGS_RUNNING
) {
1025 card
->flags
&= ~ISDNLOOP_FLAGS_RUNNING
;
1026 del_timer(&card
->st_timer
);
1027 del_timer(&card
->rb_timer
);
1028 del_timer(&card
->c_timer
[0]);
1029 del_timer(&card
->c_timer
[1]);
1030 cmd
.command
= ISDN_STAT_STOP
;
1031 cmd
.driver
= card
->myid
;
1032 card
->interface
.statcallb(&cmd
);
1034 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
1038 * Stop all cards before unload.
1041 isdnloop_stopallcards(void)
1043 isdnloop_card
*p
= cards
;
1046 isdnloop_stopcard(p
);
1052 * Start a 'card'. Simulate card's boot message and set the phone
1053 * number(s) of the virtual 'S0-Interface'. Install D-channel
1057 * card = pointer to card struct.
1058 * sdefp = pointer to struct holding ioctl parameters.
1060 * 0 on success, -E??? otherwise.
1063 isdnloop_start(isdnloop_card
*card
, isdnloop_sdef
*sdefp
)
1065 unsigned long flags
;
1069 if (card
->flags
& ISDNLOOP_FLAGS_RUNNING
)
1071 if (copy_from_user((char *) &sdef
, (char *) sdefp
, sizeof(sdef
)))
1073 spin_lock_irqsave(&card
->isdnloop_lock
, flags
);
1074 switch (sdef
.ptype
) {
1075 case ISDN_PTYPE_EURO
:
1076 if (isdnloop_fake(card
, "DRV1.23EC-Q.931-CAPI-CNS-BASIS-20.02.96",
1078 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
1081 card
->sil
[0] = card
->sil
[1] = 4;
1082 if (isdnloop_fake(card
, "TEI OK", 0)) {
1083 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
1086 for (i
= 0; i
< 3; i
++) {
1087 strlcpy(card
->s0num
[i
], sdef
.num
[i
],
1088 sizeof(card
->s0num
[0]));
1091 case ISDN_PTYPE_1TR6
:
1092 if (isdnloop_fake(card
, "DRV1.04TC-1TR6-CAPI-CNS-BASIS-29.11.95",
1094 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
1097 card
->sil
[0] = card
->sil
[1] = 4;
1098 if (isdnloop_fake(card
, "TEI OK", 0)) {
1099 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
1102 strlcpy(card
->s0num
[0], sdef
.num
[0], sizeof(card
->s0num
[0]));
1103 card
->s0num
[1][0] = '\0';
1104 card
->s0num
[2][0] = '\0';
1107 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
1108 printk(KERN_WARNING
"isdnloop: Illegal D-channel protocol %d\n",
1112 init_timer(&card
->st_timer
);
1113 card
->st_timer
.expires
= jiffies
+ ISDNLOOP_TIMER_DCREAD
;
1114 card
->st_timer
.function
= isdnloop_polldchan
;
1115 card
->st_timer
.data
= (unsigned long) card
;
1116 add_timer(&card
->st_timer
);
1117 card
->flags
|= ISDNLOOP_FLAGS_RUNNING
;
1118 spin_unlock_irqrestore(&card
->isdnloop_lock
, flags
);
1123 * Main handler for commands sent by linklevel.
1126 isdnloop_command(isdn_ctrl
*c
, isdnloop_card
*card
)
1134 switch (c
->command
) {
1135 case ISDN_CMD_IOCTL
:
1136 memcpy(&a
, c
->parm
.num
, sizeof(ulong
));
1138 case ISDNLOOP_IOCTL_DEBUGVAR
:
1139 return (ulong
) card
;
1140 case ISDNLOOP_IOCTL_STARTUP
:
1141 if (!access_ok(VERIFY_READ
, (void *) a
, sizeof(isdnloop_sdef
)))
1143 return (isdnloop_start(card
, (isdnloop_sdef
*) a
));
1145 case ISDNLOOP_IOCTL_ADDCARD
:
1146 if (copy_from_user((char *)&cdef
,
1150 return (isdnloop_addcard(cdef
.id1
));
1152 case ISDNLOOP_IOCTL_LEASEDCFG
:
1154 if (!card
->leased
) {
1156 while (card
->ptype
== ISDN_PTYPE_UNKNOWN
)
1157 schedule_timeout_interruptible(10);
1158 schedule_timeout_interruptible(10);
1159 sprintf(cbuf
, "00;FV2ON\n01;EAZ1\n02;EAZ2\n");
1160 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1162 "isdnloop: (%s) Leased-line mode enabled\n",
1164 cmd
.command
= ISDN_STAT_RUN
;
1165 cmd
.driver
= card
->myid
;
1167 card
->interface
.statcallb(&cmd
);
1172 sprintf(cbuf
, "00;FV2OFF\n");
1173 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1175 "isdnloop: (%s) Leased-line mode disabled\n",
1177 cmd
.command
= ISDN_STAT_RUN
;
1178 cmd
.driver
= card
->myid
;
1180 card
->interface
.statcallb(&cmd
);
1189 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1193 if ((c
->arg
& 255) < ISDNLOOP_BCH
) {
1199 p
= c
->parm
.setup
.phone
;
1200 if (*p
== 's' || *p
== 'S') {
1203 strcpy(dcode
, "SCA");
1206 strcpy(dcode
, "CAL");
1208 sprintf(cbuf
, "%02d;D%s_R%s,%02d,%02d,%s\n", (int) (a
+ 1),
1209 dcode
, dial
, c
->parm
.setup
.si1
,
1210 c
->parm
.setup
.si2
, c
->parm
.setup
.eazmsn
);
1211 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1214 case ISDN_CMD_ACCEPTD
:
1215 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1217 if (c
->arg
< ISDNLOOP_BCH
) {
1220 switch (card
->l2_proto
[a
- 1]) {
1221 case ISDN_PROTO_L2_X75I
:
1222 sprintf(cbuf
, "%02d;BX75\n", (int) a
);
1224 #ifdef CONFIG_ISDN_X25
1225 case ISDN_PROTO_L2_X25DTE
:
1226 sprintf(cbuf
, "%02d;BX2T\n", (int) a
);
1228 case ISDN_PROTO_L2_X25DCE
:
1229 sprintf(cbuf
, "%02d;BX2C\n", (int) a
);
1232 case ISDN_PROTO_L2_HDLC
:
1233 sprintf(cbuf
, "%02d;BTRA\n", (int) a
);
1237 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1238 sprintf(cbuf
, "%02d;DCON_R\n", (int) a
);
1239 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1242 case ISDN_CMD_ACCEPTB
:
1243 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1245 if (c
->arg
< ISDNLOOP_BCH
) {
1247 switch (card
->l2_proto
[a
- 1]) {
1248 case ISDN_PROTO_L2_X75I
:
1249 sprintf(cbuf
, "%02d;BCON_R,BX75\n", (int) a
);
1251 #ifdef CONFIG_ISDN_X25
1252 case ISDN_PROTO_L2_X25DTE
:
1253 sprintf(cbuf
, "%02d;BCON_R,BX2T\n", (int) a
);
1255 case ISDN_PROTO_L2_X25DCE
:
1256 sprintf(cbuf
, "%02d;BCON_R,BX2C\n", (int) a
);
1259 case ISDN_PROTO_L2_HDLC
:
1260 sprintf(cbuf
, "%02d;BCON_R,BTRA\n", (int) a
);
1263 sprintf(cbuf
, "%02d;BCON_R\n", (int) a
);
1265 printk(KERN_DEBUG
"isdnloop writecmd '%s'\n", cbuf
);
1266 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1268 case ISDN_CMD_HANGUP
:
1269 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1271 if (c
->arg
< ISDNLOOP_BCH
) {
1273 sprintf(cbuf
, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a
, (int) a
);
1274 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1277 case ISDN_CMD_SETEAZ
:
1278 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1282 if (c
->arg
< ISDNLOOP_BCH
) {
1284 if (card
->ptype
== ISDN_PTYPE_EURO
) {
1285 sprintf(cbuf
, "%02d;MS%s%s\n", (int) a
,
1286 c
->parm
.num
[0] ? "N" : "ALL", c
->parm
.num
);
1288 sprintf(cbuf
, "%02d;EAZ%s\n", (int) a
,
1289 c
->parm
.num
[0] ? c
->parm
.num
: (u_char
*) "0123456789");
1290 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1293 case ISDN_CMD_CLREAZ
:
1294 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1298 if (c
->arg
< ISDNLOOP_BCH
) {
1300 if (card
->ptype
== ISDN_PTYPE_EURO
)
1301 sprintf(cbuf
, "%02d;MSNC\n", (int) a
);
1303 sprintf(cbuf
, "%02d;EAZC\n", (int) a
);
1304 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1307 case ISDN_CMD_SETL2
:
1308 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1310 if ((c
->arg
& 255) < ISDNLOOP_BCH
) {
1313 case ISDN_PROTO_L2_X75I
:
1314 sprintf(cbuf
, "%02d;BX75\n", (int) (a
& 255) + 1);
1316 #ifdef CONFIG_ISDN_X25
1317 case ISDN_PROTO_L2_X25DTE
:
1318 sprintf(cbuf
, "%02d;BX2T\n", (int) (a
& 255) + 1);
1320 case ISDN_PROTO_L2_X25DCE
:
1321 sprintf(cbuf
, "%02d;BX2C\n", (int) (a
& 255) + 1);
1324 case ISDN_PROTO_L2_HDLC
:
1325 sprintf(cbuf
, "%02d;BTRA\n", (int) (a
& 255) + 1);
1327 case ISDN_PROTO_L2_TRANS
:
1328 sprintf(cbuf
, "%02d;BTRA\n", (int) (a
& 255) + 1);
1333 i
= isdnloop_writecmd(cbuf
, strlen(cbuf
), 0, card
);
1334 card
->l2_proto
[a
& 255] = (a
>> 8);
1337 case ISDN_CMD_SETL3
:
1338 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1349 * Find card with given driverId
1351 static inline isdnloop_card
*
1352 isdnloop_findcard(int driverid
)
1354 isdnloop_card
*p
= cards
;
1357 if (p
->myid
== driverid
)
1361 return (isdnloop_card
*) 0;
1365 * Wrapper functions for interface to linklevel
1368 if_command(isdn_ctrl
*c
)
1370 isdnloop_card
*card
= isdnloop_findcard(c
->driver
);
1373 return (isdnloop_command(c
, card
));
1375 "isdnloop: if_command called with invalid driverId!\n");
1380 if_writecmd(const u_char __user
*buf
, int len
, int id
, int channel
)
1382 isdnloop_card
*card
= isdnloop_findcard(id
);
1385 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1387 return (isdnloop_writecmd(buf
, len
, 1, card
));
1390 "isdnloop: if_writecmd called with invalid driverId!\n");
1395 if_readstatus(u_char __user
*buf
, int len
, int id
, int channel
)
1397 isdnloop_card
*card
= isdnloop_findcard(id
);
1400 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1402 return (isdnloop_readstatus(buf
, len
, card
));
1405 "isdnloop: if_readstatus called with invalid driverId!\n");
1410 if_sendbuf(int id
, int channel
, int ack
, struct sk_buff
*skb
)
1412 isdnloop_card
*card
= isdnloop_findcard(id
);
1415 if (!(card
->flags
& ISDNLOOP_FLAGS_RUNNING
))
1417 /* ack request stored in skb scratch area */
1419 return (isdnloop_sendbuf(channel
, skb
, card
));
1422 "isdnloop: if_sendbuf called with invalid driverId!\n");
1427 * Allocate a new card-struct, initialize it
1428 * link it into cards-list and register it at linklevel.
1430 static isdnloop_card
*
1431 isdnloop_initcard(char *id
)
1433 isdnloop_card
*card
;
1436 if (!(card
= kzalloc(sizeof(isdnloop_card
), GFP_KERNEL
))) {
1438 "isdnloop: (%s) Could not allocate card-struct.\n", id
);
1439 return (isdnloop_card
*) 0;
1441 card
->interface
.owner
= THIS_MODULE
;
1442 card
->interface
.channels
= ISDNLOOP_BCH
;
1443 card
->interface
.hl_hdrlen
= 1; /* scratch area for storing ack flag*/
1444 card
->interface
.maxbufsize
= 4000;
1445 card
->interface
.command
= if_command
;
1446 card
->interface
.writebuf_skb
= if_sendbuf
;
1447 card
->interface
.writecmd
= if_writecmd
;
1448 card
->interface
.readstat
= if_readstatus
;
1449 card
->interface
.features
= ISDN_FEATURE_L2_X75I
|
1450 #ifdef CONFIG_ISDN_X25
1451 ISDN_FEATURE_L2_X25DTE
|
1452 ISDN_FEATURE_L2_X25DCE
|
1454 ISDN_FEATURE_L2_HDLC
|
1455 ISDN_FEATURE_L3_TRANS
|
1456 ISDN_FEATURE_P_UNKNOWN
;
1457 card
->ptype
= ISDN_PTYPE_UNKNOWN
;
1458 strlcpy(card
->interface
.id
, id
, sizeof(card
->interface
.id
));
1459 card
->msg_buf_write
= card
->msg_buf
;
1460 card
->msg_buf_read
= card
->msg_buf
;
1461 card
->msg_buf_end
= &card
->msg_buf
[sizeof(card
->msg_buf
) - 1];
1462 for (i
= 0; i
< ISDNLOOP_BCH
; i
++) {
1463 card
->l2_proto
[i
] = ISDN_PROTO_L2_X75I
;
1464 skb_queue_head_init(&card
->bqueue
[i
]);
1466 skb_queue_head_init(&card
->dqueue
);
1467 spin_lock_init(&card
->isdnloop_lock
);
1470 if (!register_isdn(&card
->interface
)) {
1471 cards
= cards
->next
;
1473 "isdnloop: Unable to register %s\n", id
);
1475 return (isdnloop_card
*) 0;
1477 card
->myid
= card
->interface
.channels
;
1482 isdnloop_addcard(char *id1
)
1484 isdnloop_card
*card
;
1486 if (!(card
= isdnloop_initcard(id1
))) {
1490 "isdnloop: (%s) virtual card added\n",
1491 card
->interface
.id
);
1499 return (isdnloop_addcard(isdnloop_id
));
1508 isdnloop_card
*card
= cards
;
1509 isdnloop_card
*last
;
1512 isdnloop_stopallcards();
1514 cmd
.command
= ISDN_STAT_UNLOAD
;
1515 cmd
.driver
= card
->myid
;
1516 card
->interface
.statcallb(&cmd
);
1517 for (i
= 0; i
< ISDNLOOP_BCH
; i
++)
1518 isdnloop_free_queue(card
, i
);
1524 skb_queue_purge(&card
->dqueue
);
1528 printk(KERN_NOTICE
"isdnloop-ISDN-driver unloaded\n");
1531 module_init(isdnloop_init
);
1532 module_exit(isdnloop_exit
);