pre-2.3.4..
[davej-history.git] / drivers / isdn / isdnloop / isdnloop.c
blob2e580e1c7a611784cf5e3cbd8d3204612b99de79
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)
10 * any later version.
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
23 * changes for 2.1.127
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>
56 #include "isdnloop.h"
58 static char
59 *revision = "$Revision: 1.8 $";
61 static int isdnloop_addcard(char *);
64 * Free queue completely.
66 * Parameter:
67 * card = pointer to card struct
68 * channel = channel number
70 static void
71 isdnloop_free_queue(isdnloop_card * card, int channel)
73 struct sk_buff_head *queue = &card->bqueue[channel];
74 struct sk_buff *skb;
76 while ((skb = skb_dequeue(queue)))
77 dev_kfree_skb(skb);
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().
85 * Parameter:
86 * card = pointer to card struct.
87 * ch = channel number (0-based)
89 static void
90 isdnloop_bchan_send(isdnloop_card * card, int ch)
92 isdnloop_card *rcard = card->rcard[ch];
93 int rch = card->rch[ch], len, ack;
94 struct sk_buff *skb;
95 isdn_ctrl cmd;
97 while (card->sndcount[ch]) {
98 if ((skb = skb_dequeue(&card->bqueue[ch]))) {
99 len = skb->len;
100 card->sndcount[ch] -= len;
101 ack = *(skb->head); /* used as scratch area */
102 cmd.driver = card->myid;
103 cmd.arg = ch;
104 if (rcard){
105 rcard->interface.rcvcallb_skb(rcard->myid, rch, skb);
106 } else {
107 printk(KERN_WARNING "isdnloop: no rcard, skb dropped\n");
108 dev_kfree_skb(skb);
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);
117 } else
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.
127 * Parameter:
128 * data = pointer to card struct, set by kernel timer.data
130 static void
131 isdnloop_pollbchan(unsigned long data)
133 isdnloop_card *card = (isdnloop_card *) data;
134 unsigned long flags;
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 */
142 save_flags(flags);
143 cli();
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);
148 } else
149 card->flags &= ~ISDNLOOP_FLAGS_RBTIMER;
153 * Parse ICN-type setup string and fill fields of setup-struct
154 * with parsed data.
156 * Parameter:
157 * setup = setup string, format: [caller-id],si1,si2,[called-id]
158 * cmd = pointer to struct to be filled.
160 static void
161 isdnloop_parse_setup(char *setup, isdn_ctrl * cmd)
163 char *t = setup;
164 char *s = strpbrk(t, ",");
166 *s++ = '\0';
167 strncpy(cmd->parm.setup.phone, t, sizeof(cmd->parm.setup.phone));
168 s = strpbrk(t = s, ",");
169 *s++ = '\0';
170 if (!strlen(t))
171 cmd->parm.setup.si1 = 0;
172 else
173 cmd->parm.setup.si1 = simple_strtoul(t, NULL, 10);
174 s = strpbrk(t = s, ",");
175 *s++ = '\0';
176 if (!strlen(t))
177 cmd->parm.setup.si2 = 0;
178 else
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 {
187 char *statstr;
188 int command;
189 int action;
190 } isdnloop_stat;
191 /* *INDENT-OFF* */
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 */
210 {NULL, 0, -1}
212 /* *INDENT-ON* */
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.
220 * Parameter:
221 * status = status string to parse.
222 * channel = channel where message comes from.
223 * card = card where message comes from.
225 static void
226 isdnloop_parse_status(u_char * status, int channel, isdnloop_card * card)
228 isdnloop_stat *s = isdnloop_stat_table;
229 int action = -1;
230 isdn_ctrl cmd;
232 while (s->statstr) {
233 if (!strncmp(status, s->statstr, strlen(s->statstr))) {
234 cmd.command = s->command;
235 action = s->action;
236 break;
238 s++;
240 if (action == -1)
241 return;
242 cmd.driver = card->myid;
243 cmd.arg = channel;
244 switch (action) {
245 case 1:
246 /* BCON_x */
247 card->flags |= (channel) ?
248 ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE;
249 break;
250 case 2:
251 /* BDIS_x */
252 card->flags &= ~((channel) ?
253 ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE);
254 isdnloop_free_queue(card, channel);
255 break;
256 case 3:
257 /* DCAL_I and DSCA_I */
258 isdnloop_parse_setup(status + 6, &cmd);
259 break;
260 case 4:
261 /* FCALL */
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;
268 break;
269 case 5:
270 /* CIF */
271 strncpy(cmd.parm.num, status + 3, sizeof(cmd.parm.num) - 1);
272 break;
273 case 6:
274 /* AOC */
275 sprintf(cmd.parm.num, "%d",
276 (int) simple_strtoul(status + 7, NULL, 16));
277 break;
278 case 7:
279 /* CAU */
280 status += 3;
281 if (strlen(status) == 4)
282 sprintf(cmd.parm.num, "%s%c%c",
283 status + 2, *status, *(status + 1));
284 else
285 strncpy(cmd.parm.num, status + 1, sizeof(cmd.parm.num) - 1);
286 break;
287 case 8:
288 /* Misc Errors on L1 and L2 */
289 card->flags &= ~ISDNLOOP_FLAGS_B1ACTIVE;
290 isdnloop_free_queue(card, 0);
291 cmd.arg = 0;
292 cmd.driver = card->myid;
293 card->interface.statcallb(&cmd);
294 cmd.command = ISDN_STAT_DHUP;
295 cmd.arg = 0;
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);
301 cmd.arg = 1;
302 cmd.driver = card->myid;
303 card->interface.statcallb(&cmd);
304 cmd.command = ISDN_STAT_DHUP;
305 cmd.arg = 1;
306 cmd.driver = card->myid;
307 break;
309 card->interface.statcallb(&cmd);
313 * Store a cwcharacter into ringbuffer for reading from /dev/isdnctrl
315 * Parameter:
316 * card = pointer to card struct.
317 * c = char to store.
319 static void
320 isdnloop_putmsg(isdnloop_card * card, unsigned char c)
322 ulong flags;
324 save_flags(flags);
325 cli();
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.
346 * Parameter:
347 * data = pointer to card struct
349 static void
350 isdnloop_polldchan(unsigned long data)
352 isdnloop_card *card = (isdnloop_card *) data;
353 struct sk_buff *skb;
354 int avail;
355 int left;
356 u_char c;
357 int ch;
358 int flags;
359 u_char *p;
360 isdn_ctrl cmd;
362 if ((skb = skb_dequeue(&card->dqueue)))
363 avail = skb->len;
364 else
365 avail = 0;
366 for (left = avail; left > 0; left--) {
367 c = *skb->data;
368 skb_pull(skb, 1);
369 isdnloop_putmsg(card, c);
370 card->imsg[card->iptr] = c;
371 if (card->iptr < 59)
372 card->iptr++;
373 if (!skb->len) {
374 avail++;
375 isdnloop_putmsg(card, '\n');
376 card->imsg[card->iptr] = 0;
377 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;
381 p = &card->imsg[3];
382 isdnloop_parse_status(p, ch, card);
383 } else {
384 p = card->imsg;
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;
390 printk(KERN_INFO
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;
396 printk(KERN_INFO
397 "isdnloop: (%s) Euro-Protocol loaded and running\n", CID);
399 continue;
405 if (avail) {
406 cmd.command = ISDN_STAT_STAVAIL;
407 cmd.driver = card->myid;
408 cmd.arg = avail;
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;
415 save_flags(flags);
416 cli();
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);
424 /* schedule again */
425 save_flags(flags);
426 cli();
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.
435 * Parameter:
436 * channel = Number of B-channel
437 * skb = packet to send.
438 * card = pointer to card-struct
439 * Return:
440 * Number of bytes transferred, -E??? on error
442 static int
443 isdnloop_sendbuf(int channel, struct sk_buff *skb, isdnloop_card * card)
445 int len = skb->len;
446 unsigned long flags;
447 struct sk_buff *nskb;
449 if (len > 4000) {
450 printk(KERN_WARNING
451 "isdnloop: Send packet too large\n");
452 return -EINVAL;
454 if (len) {
455 if (!(card->flags & (channel) ? ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE))
456 return 0;
457 if (card->sndcount[channel] > ISDNLOOP_MAX_SQUEUE)
458 return 0;
459 save_flags(flags);
460 cli();
461 nskb = skb_clone(skb, GFP_ATOMIC);
462 if (nskb) {
463 skb_queue_tail(&card->bqueue[channel], nskb);
464 dev_kfree_skb(skb);
465 } else
466 len = 0;
467 card->sndcount[channel] += len;
468 restore_flags(flags);
470 return len;
474 * Read the messages from the card's ringbuffer
476 * Parameter:
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.
481 * Return:
482 * number of bytes actually transferred.
484 static int
485 isdnloop_readstatus(u_char * buf, int len, int user, isdnloop_card * card)
487 int count;
488 u_char *p;
490 for (p = buf, count = 0; count < len; p++, count++) {
491 if (card->msg_buf_read == card->msg_buf_write)
492 return count;
493 if (user)
494 put_user(*card->msg_buf_read++, p);
495 else
496 *p = *card->msg_buf_read++;
497 if (card->msg_buf_read > card->msg_buf_end)
498 card->msg_buf_read = card->msg_buf;
500 return count;
504 * Simulate a card's response by appending it to the cards
505 * message queue.
507 * Parameter:
508 * card = pointer to card struct.
509 * s = pointer to message-string.
510 * ch = channel: 0 = generic messages, 1 and 2 = D-channel messages.
511 * Return:
512 * 0 on success, 1 on memory squeeze.
514 static int
515 isdnloop_fake(isdnloop_card * card, char *s, int ch)
517 struct sk_buff *skb;
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");
522 return 1;
524 if (ch >= 0)
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);
528 return 0;
530 /* *INDENT-OFF* */
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 */
552 {NULL, 0, -1}
554 /* *INDENT-ON* */
558 * Simulate an error-response from a card.
560 * Parameter:
561 * card = pointer to card struct.
563 static void
564 isdnloop_fake_err(isdnloop_card * card)
566 char buf[60];
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.
582 * Parameter:
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.
586 * Return:
587 * Pointer to buffer containing the assembled message.
589 static char *
590 isdnloop_unicause(isdnloop_card * card, int loc, int cau)
592 static char buf[6];
594 switch (card->ptype) {
595 case ISDN_PTYPE_EURO:
596 sprintf(buf, "E%02X%02X", (loc) ? 4 : 2, ctable_eu[cau]);
597 break;
598 case ISDN_PTYPE_1TR6:
599 sprintf(buf, "%02X44", ctable_1t[cau]);
600 break;
601 default:
602 return ("0000");
604 return (buf);
608 * Release a virtual connection. Called from timer interrupt, when
609 * called party did not respond.
611 * Parameter:
612 * card = pointer to card struct.
613 * ch = channel (0-based)
615 static void
616 isdnloop_atimeout(isdnloop_card * card, int ch)
618 unsigned long flags;
619 char buf[60];
621 save_flags(flags);
622 cli();
623 if (card->rcard) {
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().
638 static void
639 isdnloop_atimeout0(unsigned long data)
641 isdnloop_card *card = (isdnloop_card *) data;
642 isdnloop_atimeout(card, 0);
646 * Wrapper for isdnloop_atimeout().
648 static void
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.
658 * Parameter:
659 * card = pointer to card struct.
660 * ch = channel to watch for.
662 static void
663 isdnloop_start_ctimer(isdnloop_card * card, int ch)
665 unsigned long flags;
667 save_flags(flags);
668 cli();
669 init_timer(&card->c_timer[ch]);
670 card->c_timer[ch].expires = jiffies + ISDNLOOP_TIMER_ALERTWAIT;
671 if (ch)
672 card->c_timer[ch].function = isdnloop_atimeout1;
673 else
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.
683 * Parameter:
684 * card = pointer to card struct.
685 * ch = channel (0-based).
687 static void
688 isdnloop_kill_ctimer(isdnloop_card * card, int ch)
690 unsigned long flags;
692 save_flags(flags);
693 cli();
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[] =
701 {1, 5, 7};
704 * Try finding a listener for an outgoing call.
706 * Parameter:
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.
711 * Return:
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.
717 static int
718 isdnloop_try_call(isdnloop_card * card, char *p, int lch, isdn_ctrl * cmd)
720 isdnloop_card *cc = cards;
721 unsigned long flags;
722 int ch;
723 int num_match;
724 int i;
725 char *e;
726 char nbuf[32];
728 isdnloop_parse_setup(p, cmd);
729 while (cc) {
730 for (ch = 0; ch < 2; ch++) {
731 /* Exclude ourself */
732 if ((cc == card) && (ch == lch))
733 continue;
734 num_match = 0;
735 switch (cc->ptype) {
736 case ISDN_PTYPE_EURO:
737 for (i = 0; i < 3; i++)
738 if (!(strcmp(cc->s0num[i], cmd->parm.setup.phone)))
739 num_match = 1;
740 break;
741 case ISDN_PTYPE_1TR6:
742 e = cc->eazlist[ch];
743 while (*e) {
744 sprintf(nbuf, "%s%c", cc->s0num[0], *e);
745 if (!(strcmp(nbuf, cmd->parm.setup.phone)))
746 num_match = 1;
747 e++;
750 if (num_match) {
751 save_flags(flags);
752 cli();
753 /* channel idle? */
754 if (!(cc->rcard[ch])) {
755 /* Check SI */
756 if (!(si2bit[cmd->parm.setup.si1] & cc->sil[ch])) {
757 restore_flags(flags);
758 return 3;
760 /* ch is idle, si and number matches */
761 cc->rcard[ch] = card;
762 cc->rch[ch] = lch;
763 card->rcard[lch] = cc;
764 card->rch[lch] = ch;
765 restore_flags(flags);
766 return 0;
767 } else {
768 restore_flags(flags);
769 /* num matches, but busy */
770 if (ch == 1)
771 return 1;
775 cc = cc->next;
777 return 2;
781 * Depending on D-channel protocol and caller/called, modify
782 * phone number.
784 * Parameter:
785 * card = pointer to card struct.
786 * phone = pointer phone number.
787 * caller = flag: 1 = caller, 0 = called.
788 * Return:
789 * pointer to new phone number.
791 static char *
792 isdnloop_vstphone(isdnloop_card * card, char *phone, int caller)
794 int i;
795 static char nphone[30];
797 switch (card->ptype) {
798 case ISDN_PTYPE_EURO:
799 if (caller) {
800 for (i = 0; i < 2; i++)
801 if (!(strcmp(card->s0num[i], phone)))
802 return (phone);
803 return (card->s0num[0]);
805 return (phone);
806 break;
807 case ISDN_PTYPE_1TR6:
808 if (caller) {
809 sprintf(nphone, "%s%c", card->s0num[0], phone[0]);
810 return (nphone);
811 } else
812 return (&phone[strlen(phone) - 1]);
813 break;
815 return ("\0");
819 * Parse an ICN-type command string sent to the 'card'.
820 * Perform misc. actions depending on the command.
822 * Parameter:
823 * card = pointer to card struct.
825 static void
826 isdnloop_parse_cmd(isdnloop_card * card)
828 char *p = card->omsg;
829 isdn_ctrl cmd;
830 char buf[60];
831 isdnloop_stat *s = isdnloop_cmd_table;
832 int action = -1;
833 int i;
834 int ch;
836 if ((card->omsg[0] != '0') && (card->omsg[2] != ';')) {
837 isdnloop_fake_err(card);
838 return;
840 ch = card->omsg[1] - '0';
841 if ((ch < 0) || (ch > 2)) {
842 isdnloop_fake_err(card);
843 return;
845 p += 3;
846 while (s->statstr) {
847 if (!strncmp(p, s->statstr, strlen(s->statstr))) {
848 action = s->action;
849 if (s->command && (ch != 0)) {
850 isdnloop_fake_err(card);
851 return;
853 break;
855 s++;
857 if (action == -1)
858 return;
859 switch (action) {
860 case 1:
861 /* 0x;BCON_R */
862 if (card->rcard[ch - 1]) {
863 isdnloop_fake(card->rcard[ch - 1], "BCON_I",
864 card->rch[ch - 1] + 1);
866 break;
867 case 17:
868 /* 0x;BCON_I */
869 if (card->rcard[ch - 1]) {
870 isdnloop_fake(card->rcard[ch - 1], "BCON_C",
871 card->rch[ch - 1] + 1);
873 break;
874 case 2:
875 /* 0x;BDIS_R */
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);
881 break;
882 case 16:
883 /* 0x;DCON_R */
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);
891 break;
892 case 3:
893 /* 0x;DDIS_R */
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);
902 break;
903 case 4:
904 /* 0x;DSCA_Rdd,yy,zz,oo */
905 if (card->ptype != ISDN_PTYPE_1TR6) {
906 isdnloop_fake_err(card);
907 return;
909 /* Fall through */
910 case 5:
911 /* 0x;DCAL_Rdd,yy,zz,oo */
912 p += 6;
913 switch (isdnloop_try_call(card, p, ch - 1, &cmd)) {
914 case 0:
915 /* Alerting */
916 sprintf(buf, "D%s_I%s,%02d,%02d,%s",
917 (action == 4) ? "SCA" : "CAL",
918 isdnloop_vstphone(card, cmd.parm.setup.eazmsn, 1),
919 cmd.parm.setup.si1,
920 cmd.parm.setup.si2,
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);
924 /* Fall through */
925 case 3:
926 /* si1 does not match, don't alert but start timer */
927 isdnloop_start_ctimer(card, ch - 1);
928 break;
929 case 1:
930 /* Remote busy */
931 isdnloop_fake(card, "DDIS_I", ch);
932 sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 1));
933 isdnloop_fake(card, buf, ch);
934 break;
935 case 2:
936 /* No such user */
937 isdnloop_fake(card, "DDIS_I", ch);
938 sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 2));
939 isdnloop_fake(card, buf, ch);
940 break;
942 break;
943 case 6:
944 /* 0x;EAZC */
945 card->eazlist[ch - 1][0] = '\0';
946 break;
947 case 7:
948 /* 0x;EAZ */
949 p += 3;
950 strcpy(card->eazlist[ch - 1], p);
951 break;
952 case 8:
953 /* 0x;SEEAZ */
954 sprintf(buf, "EAZ-LIST: %s", card->eazlist[ch - 1]);
955 isdnloop_fake(card, buf, ch + 1);
956 break;
957 case 9:
958 /* 0x;MSN */
959 break;
960 case 10:
961 /* 0x;MSNALL */
962 break;
963 case 11:
964 /* 0x;SETSIL */
965 p += 6;
966 i = 0;
967 while (strchr("0157", *p)) {
968 if (i)
969 card->sil[ch - 1] |= si2bit[*p - '0'];
970 i = (*p++ == '0');
972 if (*p)
973 isdnloop_fake_err(card);
974 break;
975 case 12:
976 /* 0x;SEESIL */
977 sprintf(buf, "SIN-LIST: ");
978 p = buf + 10;
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);
983 break;
984 case 13:
985 /* 0x;SILC */
986 card->sil[ch - 1] = 0;
987 break;
988 case 14:
989 /* 00;FV2ON */
990 break;
991 case 15:
992 /* 00;FV2OFF */
993 break;
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.
1003 * Parameter:
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.
1008 * Return:
1009 * number of bytes transfered (currently always equals len).
1011 static int
1012 isdnloop_writecmd(const u_char * buf, int len, int user, isdnloop_card * card)
1014 int xcount = 0;
1015 int ocount = 1;
1016 isdn_ctrl cmd;
1018 while (len) {
1019 int count = MIN(255, len);
1020 u_char *p;
1021 u_char msg[0x100];
1023 if (user)
1024 copy_from_user(msg, buf, count);
1025 else
1026 memcpy(msg, buf, count);
1027 isdnloop_putmsg(card, '>');
1028 for (p = msg; count > 0; count--, p++) {
1029 len--;
1030 xcount++;
1031 isdnloop_putmsg(card, *p);
1032 card->omsg[card->optr] = *p;
1033 if (*p == '\n') {
1034 card->omsg[card->optr] = '\0';
1035 card->optr = 0;
1036 isdnloop_parse_cmd(card);
1037 if (len) {
1038 isdnloop_putmsg(card, '>');
1039 ocount++;
1041 } else {
1042 if (card->optr < 59)
1043 card->optr++;
1045 ocount++;
1048 cmd.command = ISDN_STAT_STAVAIL;
1049 cmd.driver = card->myid;
1050 cmd.arg = ocount;
1051 card->interface.statcallb(&cmd);
1052 return xcount;
1056 * Delete card's pending timers, send STOP to linklevel
1058 static void
1059 isdnloop_stopcard(isdnloop_card * card)
1061 unsigned long flags;
1062 isdn_ctrl cmd;
1064 save_flags(flags);
1065 cli();
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.
1082 static void
1083 isdnloop_stopallcards(void)
1085 isdnloop_card *p = cards;
1087 while (p) {
1088 isdnloop_stopcard(p);
1089 p = p->next;
1094 * Start a 'card'. Simulate card's boot message and set the phone
1095 * number(s) of the virtual 'S0-Interface'. Install D-channel
1096 * poll timer.
1098 * Parameter:
1099 * card = pointer to card struct.
1100 * sdefp = pointer to struct holding ioctl parameters.
1101 * Return:
1102 * 0 on success, -E??? otherwise.
1104 static int
1105 isdnloop_start(isdnloop_card * card, isdnloop_sdef * sdefp)
1107 unsigned long flags;
1108 isdnloop_sdef sdef;
1109 int i;
1111 if (card->flags & ISDNLOOP_FLAGS_RUNNING)
1112 return -EBUSY;
1113 copy_from_user((char *) &sdef, (char *) sdefp, sizeof(sdef));
1114 save_flags(flags);
1115 cli();
1116 switch (sdef.ptype) {
1117 case ISDN_PTYPE_EURO:
1118 if (isdnloop_fake(card, "DRV1.23EC-Q.931-CAPI-CNS-BASIS-20.02.96",
1119 -1)) {
1120 restore_flags(flags);
1121 return -ENOMEM;
1123 card->sil[0] = card->sil[1] = 4;
1124 if (isdnloop_fake(card, "TEI OK", 0)) {
1125 restore_flags(flags);
1126 return -ENOMEM;
1128 for (i = 0; i < 3; i++)
1129 strcpy(card->s0num[i], sdef.num[i]);
1130 break;
1131 case ISDN_PTYPE_1TR6:
1132 if (isdnloop_fake(card, "DRV1.04TC-1TR6-CAPI-CNS-BASIS-29.11.95",
1133 -1)) {
1134 restore_flags(flags);
1135 return -ENOMEM;
1137 card->sil[0] = card->sil[1] = 4;
1138 if (isdnloop_fake(card, "TEI OK", 0)) {
1139 restore_flags(flags);
1140 return -ENOMEM;
1142 strcpy(card->s0num[0], sdef.num[0]);
1143 card->s0num[1][0] = '\0';
1144 card->s0num[2][0] = '\0';
1145 break;
1146 default:
1147 restore_flags(flags);
1148 printk(KERN_WARNING "isdnloop: Illegal D-channel protocol %d\n",
1149 sdef.ptype);
1150 return -EINVAL;
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);
1159 return 0;
1163 * Main handler for commands sent by linklevel.
1165 static int
1166 isdnloop_command(isdn_ctrl * c, isdnloop_card * card)
1168 ulong a;
1169 int i;
1170 char cbuf[60];
1171 isdn_ctrl cmd;
1172 isdnloop_cdef cdef;
1174 switch (c->command) {
1175 case ISDN_CMD_IOCTL:
1176 memcpy(&a, c->parm.num, sizeof(ulong));
1177 switch (c->arg) {
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))))
1182 return i;
1183 return (isdnloop_start(card, (isdnloop_sdef *) a));
1184 break;
1185 case ISDNLOOP_IOCTL_ADDCARD:
1186 if ((i = verify_area(VERIFY_READ, (void *) a, sizeof(isdnloop_cdef))))
1187 return i;
1188 copy_from_user((char *) &cdef, (char *) a, sizeof(cdef));
1189 return (isdnloop_addcard(cdef.id1));
1190 break;
1191 case ISDNLOOP_IOCTL_LEASEDCFG:
1192 if (a) {
1193 if (!card->leased) {
1194 card->leased = 1;
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);
1201 printk(KERN_INFO
1202 "isdnloop: (%s) Leased-line mode enabled\n",
1203 CID);
1204 cmd.command = ISDN_STAT_RUN;
1205 cmd.driver = card->myid;
1206 cmd.arg = 0;
1207 card->interface.statcallb(&cmd);
1209 } else {
1210 if (card->leased) {
1211 card->leased = 0;
1212 sprintf(cbuf, "00;FV2OFF\n");
1213 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1214 printk(KERN_INFO
1215 "isdnloop: (%s) Leased-line mode disabled\n",
1216 CID);
1217 cmd.command = ISDN_STAT_RUN;
1218 cmd.driver = card->myid;
1219 cmd.arg = 0;
1220 card->interface.statcallb(&cmd);
1223 return 0;
1224 default:
1225 return -EINVAL;
1227 break;
1228 case ISDN_CMD_DIAL:
1229 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1230 return -ENODEV;
1231 if (card->leased)
1232 break;
1233 if ((c->arg & 255) < ISDNLOOP_BCH) {
1234 char *p;
1235 char dial[50];
1236 char dcode[4];
1238 a = c->arg;
1239 p = c->parm.setup.phone;
1240 if (*p == 's' || *p == 'S') {
1241 /* Dial for SPV */
1242 p++;
1243 strcpy(dcode, "SCA");
1244 } else
1245 /* Normal Dial */
1246 strcpy(dcode, "CAL");
1247 strcpy(dial, p);
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);
1253 break;
1254 case ISDN_CMD_ACCEPTD:
1255 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1256 return -ENODEV;
1257 if (c->arg < ISDNLOOP_BCH) {
1258 a = c->arg + 1;
1259 cbuf[0] = 0;
1260 switch (card->l2_proto[a - 1]) {
1261 case ISDN_PROTO_L2_X75I:
1262 sprintf(cbuf, "%02d;BX75\n", (int) a);
1263 break;
1264 #ifdef CONFIG_ISDN_X25
1265 case ISDN_PROTO_L2_X25DTE:
1266 sprintf(cbuf, "%02d;BX2T\n", (int) a);
1267 break;
1268 case ISDN_PROTO_L2_X25DCE:
1269 sprintf(cbuf, "%02d;BX2C\n", (int) a);
1270 break;
1271 #endif
1272 case ISDN_PROTO_L2_HDLC:
1273 sprintf(cbuf, "%02d;BTRA\n", (int) a);
1274 break;
1276 if (strlen(cbuf))
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);
1281 break;
1282 case ISDN_CMD_ACCEPTB:
1283 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1284 return -ENODEV;
1285 if (c->arg < ISDNLOOP_BCH) {
1286 a = c->arg + 1;
1287 switch (card->l2_proto[a - 1]) {
1288 case ISDN_PROTO_L2_X75I:
1289 sprintf(cbuf, "%02d;BCON_R,BX75\n", (int) a);
1290 break;
1291 #ifdef CONFIG_ISDN_X25
1292 case ISDN_PROTO_L2_X25DTE:
1293 sprintf(cbuf, "%02d;BCON_R,BX2T\n", (int) a);
1294 break;
1295 case ISDN_PROTO_L2_X25DCE:
1296 sprintf(cbuf, "%02d;BCON_R,BX2C\n", (int) a);
1297 break;
1298 #endif
1299 case ISDN_PROTO_L2_HDLC:
1300 sprintf(cbuf, "%02d;BCON_R,BTRA\n", (int) a);
1301 break;
1302 default:
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);
1307 break;
1308 case ISDN_CMD_HANGUP:
1309 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1310 return -ENODEV;
1311 if (c->arg < ISDNLOOP_BCH) {
1312 a = c->arg + 1;
1313 sprintf(cbuf, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a, (int) a);
1314 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1316 break;
1317 case ISDN_CMD_SETEAZ:
1318 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1319 return -ENODEV;
1320 if (card->leased)
1321 break;
1322 if (c->arg < ISDNLOOP_BCH) {
1323 a = c->arg + 1;
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);
1327 } else
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);
1332 break;
1333 case ISDN_CMD_CLREAZ:
1334 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1335 return -ENODEV;
1336 if (card->leased)
1337 break;
1338 if (c->arg < ISDNLOOP_BCH) {
1339 a = c->arg + 1;
1340 if (card->ptype == ISDN_PTYPE_EURO)
1341 sprintf(cbuf, "%02d;MSNC\n", (int) a);
1342 else
1343 sprintf(cbuf, "%02d;EAZC\n", (int) a);
1344 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1346 break;
1347 case ISDN_CMD_SETL2:
1348 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1349 return -ENODEV;
1350 if ((c->arg & 255) < ISDNLOOP_BCH) {
1351 a = c->arg;
1352 switch (a >> 8) {
1353 case ISDN_PROTO_L2_X75I:
1354 sprintf(cbuf, "%02d;BX75\n", (int) (a & 255) + 1);
1355 break;
1356 #ifdef CONFIG_ISDN_X25
1357 case ISDN_PROTO_L2_X25DTE:
1358 sprintf(cbuf, "%02d;BX2T\n", (int) (a & 255) + 1);
1359 break;
1360 case ISDN_PROTO_L2_X25DCE:
1361 sprintf(cbuf, "%02d;BX2C\n", (int) (a & 255) + 1);
1362 break;
1363 #endif
1364 case ISDN_PROTO_L2_HDLC:
1365 sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
1366 break;
1367 default:
1368 return -EINVAL;
1370 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1371 card->l2_proto[a & 255] = (a >> 8);
1373 break;
1374 case ISDN_CMD_GETL2:
1375 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1376 return -ENODEV;
1377 if ((c->arg & 255) < ISDNLOOP_BCH)
1378 return card->l2_proto[c->arg & 255];
1379 else
1380 return -ENODEV;
1381 case ISDN_CMD_SETL3:
1382 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1383 return -ENODEV;
1384 return 0;
1385 case ISDN_CMD_GETL3:
1386 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1387 return -ENODEV;
1388 if ((c->arg & 255) < ISDNLOOP_BCH)
1389 return ISDN_PROTO_L3_TRANS;
1390 else
1391 return -ENODEV;
1392 case ISDN_CMD_GETEAZ:
1393 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1394 return -ENODEV;
1395 break;
1396 case ISDN_CMD_SETSIL:
1397 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1398 return -ENODEV;
1399 break;
1400 case ISDN_CMD_GETSIL:
1401 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1402 return -ENODEV;
1403 break;
1404 case ISDN_CMD_LOCK:
1405 MOD_INC_USE_COUNT;
1406 break;
1407 case ISDN_CMD_UNLOCK:
1408 MOD_DEC_USE_COUNT;
1409 break;
1410 default:
1411 return -EINVAL;
1414 return 0;
1418 * Find card with given driverId
1420 static inline isdnloop_card *
1421 isdnloop_findcard(int driverid)
1423 isdnloop_card *p = cards;
1425 while (p) {
1426 if (p->myid == driverid)
1427 return p;
1428 p = p->next;
1430 return (isdnloop_card *) 0;
1434 * Wrapper functions for interface to linklevel
1436 static int
1437 if_command(isdn_ctrl * c)
1439 isdnloop_card *card = isdnloop_findcard(c->driver);
1441 if (card)
1442 return (isdnloop_command(c, card));
1443 printk(KERN_ERR
1444 "isdnloop: if_command called with invalid driverId!\n");
1445 return -ENODEV;
1448 static int
1449 if_writecmd(const u_char * buf, int len, int user, int id, int channel)
1451 isdnloop_card *card = isdnloop_findcard(id);
1453 if (card) {
1454 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1455 return -ENODEV;
1456 return (isdnloop_writecmd(buf, len, user, card));
1458 printk(KERN_ERR
1459 "isdnloop: if_writecmd called with invalid driverId!\n");
1460 return -ENODEV;
1463 static int
1464 if_readstatus(u_char * buf, int len, int user, int id, int channel)
1466 isdnloop_card *card = isdnloop_findcard(id);
1468 if (card) {
1469 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1470 return -ENODEV;
1471 return (isdnloop_readstatus(buf, len, user, card));
1473 printk(KERN_ERR
1474 "isdnloop: if_readstatus called with invalid driverId!\n");
1475 return -ENODEV;
1478 static int
1479 if_sendbuf(int id, int channel, int ack, struct sk_buff *skb)
1481 isdnloop_card *card = isdnloop_findcard(id);
1483 if (card) {
1484 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1485 return -ENODEV;
1486 /* ack request stored in skb scratch area */
1487 *(skb->head) = ack;
1488 return (isdnloop_sendbuf(channel, skb, card));
1490 printk(KERN_ERR
1491 "isdnloop: if_sendbuf called with invalid driverId!\n");
1492 return -ENODEV;
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;
1503 int i;
1505 if (!(card = (isdnloop_card *) kmalloc(sizeof(isdnloop_card), GFP_KERNEL))) {
1506 printk(KERN_WARNING
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 |
1522 #endif
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);
1536 card->next = cards;
1537 cards = card;
1538 if (!register_isdn(&card->interface)) {
1539 cards = cards->next;
1540 printk(KERN_WARNING
1541 "isdnloop: Unable to register %s\n", id);
1542 kfree(card);
1543 return (isdnloop_card *) 0;
1545 card->myid = card->interface.channels;
1546 return card;
1549 static int
1550 isdnloop_addcard(char *id1)
1552 ulong flags;
1553 isdnloop_card *card;
1555 save_flags(flags);
1556 cli();
1557 if (!(card = isdnloop_initcard(id1))) {
1558 restore_flags(flags);
1559 return -EIO;
1561 restore_flags(flags);
1562 printk(KERN_INFO
1563 "isdnloop: (%s) virtual card added\n",
1564 card->interface.id);
1565 return 0;
1568 #ifdef MODULE
1569 #define isdnloop_init init_module
1570 #else
1571 void
1572 isdnloop_setup(char *str, int *ints)
1574 static char sid[20];
1576 if (strlen(str)) {
1577 strcpy(sid, str);
1578 isdnloop_id = sid;
1581 #endif
1584 isdnloop_init(void)
1586 char *p;
1587 char rev[10];
1589 /* No symbols to export, hide all symbols */
1590 EXPORT_NO_SYMBOLS;
1592 if ((p = strchr(revision, ':'))) {
1593 strcpy(rev, p + 1);
1594 p = strchr(rev, '$');
1595 *p = 0;
1596 } else
1597 strcpy(rev, " ??? ");
1598 printk(KERN_NOTICE "isdnloop-ISDN-driver Rev%s\n", rev);
1599 return (isdnloop_addcard(isdnloop_id));
1602 #ifdef MODULE
1603 void
1604 cleanup_module(void)
1606 isdn_ctrl cmd;
1607 isdnloop_card *card = cards;
1608 isdnloop_card *last;
1609 int i;
1611 isdnloop_stopallcards();
1612 while (card) {
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);
1618 card = card->next;
1620 card = cards;
1621 while (card) {
1622 struct sk_buff *skb;
1624 last = card;
1625 while ((skb = skb_dequeue(&card->dqueue)))
1626 dev_kfree_skb(skb);
1627 card = card->next;
1628 kfree(last);
1630 printk(KERN_NOTICE "isdnloop-ISDN-driver unloaded\n");
1632 #endif