1 /* $Id: isdnloop.c,v 1.1.4.1 2001/11/20 14:19:37 kai Exp $
2 *
3 * ISDN low-level module implementing a dummy loop driver.
4 *
5 * Copyright 1997 by Fritz Elfert (fritz@isdn4linux.de)
6 *
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
9 *
10 */
11
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include "isdnloop.h"
16
17 static char *revision = "$Revision: 1.1.4.1 $";
18 static char *isdnloop_id;
19
20 MODULE_DESCRIPTION("ISDN4Linux: Pseudo Driver that simulates an ISDN card");
21 MODULE_AUTHOR("Fritz Elfert");
22 MODULE_LICENSE("GPL");
23 MODULE_PARM(isdnloop_id, "s");
24 MODULE_PARM_DESC(isdnloop_id, "ID-String of first card");
25
26 static int isdnloop_addcard(char *);
27
28 /*
29 * Free queue completely.
30 *
31 * Parameter:
32 * card = pointer to card struct
33 * channel = channel number
34 */
35 static void
isdnloop_free_queue(isdnloop_card * card,int channel)36 isdnloop_free_queue(isdnloop_card * card, int channel)
37 {
38 struct sk_buff_head *queue = &card->bqueue[channel];
39
40 skb_queue_purge(queue);
41 card->sndcount[channel] = 0;
42 }
43
44 /*
45 * Send B-Channel data to another virtual card.
46 * This routine is called via timer-callback from isdnloop_pollbchan().
47 *
48 * Parameter:
49 * card = pointer to card struct.
50 * ch = channel number (0-based)
51 */
52 static void
isdnloop_bchan_send(isdnloop_card * card,int ch)53 isdnloop_bchan_send(isdnloop_card * card, int ch)
54 {
55 isdnloop_card *rcard = card->rcard[ch];
56 int rch = card->rch[ch], len, ack;
57 struct sk_buff *skb;
58 isdn_ctrl cmd;
59
60 while (card->sndcount[ch]) {
61 if ((skb = skb_dequeue(&card->bqueue[ch]))) {
62 len = skb->len;
63 card->sndcount[ch] -= len;
64 ack = *(skb->head); /* used as scratch area */
65 cmd.driver = card->myid;
66 cmd.arg = ch;
67 if (rcard){
68 rcard->interface.rcvcallb_skb(rcard->myid, rch, skb);
69 } else {
70 printk(KERN_WARNING "isdnloop: no rcard, skb dropped\n");
71 dev_kfree_skb(skb);
72
73 cmd.command = ISDN_STAT_L1ERR;
74 cmd.parm.errcode = ISDN_STAT_L1ERR_SEND;
75 card->interface.statcallb(&cmd);
76 };
77 cmd.command = ISDN_STAT_BSENT;
78 cmd.parm.length = len;
79 if ( ack ) card->interface.statcallb(&cmd);
80 } else
81 card->sndcount[ch] = 0;
82 }
83 }
84
85 /*
86 * Send/Receive Data to/from the B-Channel.
87 * This routine is called via timer-callback.
88 * It schedules itself while any B-Channel is open.
89 *
90 * Parameter:
91 * data = pointer to card struct, set by kernel timer.data
92 */
93 static void
isdnloop_pollbchan(unsigned long data)94 isdnloop_pollbchan(unsigned long data)
95 {
96 isdnloop_card *card = (isdnloop_card *) data;
97 unsigned long flags;
98
99 if (card->flags & ISDNLOOP_FLAGS_B1ACTIVE)
100 isdnloop_bchan_send(card, 0);
101 if (card->flags & ISDNLOOP_FLAGS_B2ACTIVE)
102 isdnloop_bchan_send(card, 1);
103 if (card->flags & (ISDNLOOP_FLAGS_B1ACTIVE | ISDNLOOP_FLAGS_B2ACTIVE)) {
104 /* schedule b-channel polling again */
105 save_flags(flags);
106 cli();
107 card->rb_timer.expires = jiffies + ISDNLOOP_TIMER_BCREAD;
108 add_timer(&card->rb_timer);
109 card->flags |= ISDNLOOP_FLAGS_RBTIMER;
110 restore_flags(flags);
111 } else
112 card->flags &= ~ISDNLOOP_FLAGS_RBTIMER;
113 }
114
115 /*
116 * Parse ICN-type setup string and fill fields of setup-struct
117 * with parsed data.
118 *
119 * Parameter:
120 * setup = setup string, format: [caller-id],si1,si2,[called-id]
121 * cmd = pointer to struct to be filled.
122 */
123 static void
isdnloop_parse_setup(char * setup,isdn_ctrl * cmd)124 isdnloop_parse_setup(char *setup, isdn_ctrl * cmd)
125 {
126 char *t = setup;
127 char *s = strpbrk(t, ",");
128
129 *s++ = '\0';
130 strncpy(cmd->parm.setup.phone, t, sizeof(cmd->parm.setup.phone));
131 s = strpbrk(t = s, ",");
132 *s++ = '\0';
133 if (!strlen(t))
134 cmd->parm.setup.si1 = 0;
135 else
136 cmd->parm.setup.si1 = simple_strtoul(t, NULL, 10);
137 s = strpbrk(t = s, ",");
138 *s++ = '\0';
139 if (!strlen(t))
140 cmd->parm.setup.si2 = 0;
141 else
142 cmd->parm.setup.si2 =
143 simple_strtoul(t, NULL, 10);
144 strncpy(cmd->parm.setup.eazmsn, s, sizeof(cmd->parm.setup.eazmsn));
145 cmd->parm.setup.plan = 0;
146 cmd->parm.setup.screen = 0;
147 }
148
149 typedef struct isdnloop_stat {
150 char *statstr;
151 int command;
152 int action;
153 } isdnloop_stat;
154 /* *INDENT-OFF* */
155 static isdnloop_stat isdnloop_stat_table[] =
156 {
157 {"BCON_", ISDN_STAT_BCONN, 1}, /* B-Channel connected */
158 {"BDIS_", ISDN_STAT_BHUP, 2}, /* B-Channel disconnected */
159 {"DCON_", ISDN_STAT_DCONN, 0}, /* D-Channel connected */
160 {"DDIS_", ISDN_STAT_DHUP, 0}, /* D-Channel disconnected */
161 {"DCAL_I", ISDN_STAT_ICALL, 3}, /* Incoming call dialup-line */
162 {"DSCA_I", ISDN_STAT_ICALL, 3}, /* Incoming call 1TR6-SPV */
163 {"FCALL", ISDN_STAT_ICALL, 4}, /* Leased line connection up */
164 {"CIF", ISDN_STAT_CINF, 5}, /* Charge-info, 1TR6-type */
165 {"AOC", ISDN_STAT_CINF, 6}, /* Charge-info, DSS1-type */
166 {"CAU", ISDN_STAT_CAUSE, 7}, /* Cause code */
167 {"TEI OK", ISDN_STAT_RUN, 0}, /* Card connected to wallplug */
168 {"NO D-CHAN", ISDN_STAT_NODCH, 0}, /* No D-channel available */
169 {"E_L1: ACT FAIL", ISDN_STAT_BHUP, 8}, /* Layer-1 activation failed */
170 {"E_L2: DATA LIN", ISDN_STAT_BHUP, 8}, /* Layer-2 data link lost */
171 {"E_L1: ACTIVATION FAILED",
172 ISDN_STAT_BHUP, 8}, /* Layer-1 activation failed */
173 {NULL, 0, -1}
174 };
175 /* *INDENT-ON* */
176
177
178 /*
179 * Parse Status message-strings from virtual card.
180 * Depending on status, call statcallb for sending messages to upper
181 * levels. Also set/reset B-Channel active-flags.
182 *
183 * Parameter:
184 * status = status string to parse.
185 * channel = channel where message comes from.
186 * card = card where message comes from.
187 */
188 static void
isdnloop_parse_status(u_char * status,int channel,isdnloop_card * card)189 isdnloop_parse_status(u_char * status, int channel, isdnloop_card * card)
190 {
191 isdnloop_stat *s = isdnloop_stat_table;
192 int action = -1;
193 isdn_ctrl cmd;
194
195 while (s->statstr) {
196 if (!strncmp(status, s->statstr, strlen(s->statstr))) {
197 cmd.command = s->command;
198 action = s->action;
199 break;
200 }
201 s++;
202 }
203 if (action == -1)
204 return;
205 cmd.driver = card->myid;
206 cmd.arg = channel;
207 switch (action) {
208 case 1:
209 /* BCON_x */
210 card->flags |= (channel) ?
211 ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE;
212 break;
213 case 2:
214 /* BDIS_x */
215 card->flags &= ~((channel) ?
216 ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE);
217 isdnloop_free_queue(card, channel);
218 break;
219 case 3:
220 /* DCAL_I and DSCA_I */
221 isdnloop_parse_setup(status + 6, &cmd);
222 break;
223 case 4:
224 /* FCALL */
225 sprintf(cmd.parm.setup.phone, "LEASED%d", card->myid);
226 sprintf(cmd.parm.setup.eazmsn, "%d", channel + 1);
227 cmd.parm.setup.si1 = 7;
228 cmd.parm.setup.si2 = 0;
229 cmd.parm.setup.plan = 0;
230 cmd.parm.setup.screen = 0;
231 break;
232 case 5:
233 /* CIF */
234 strncpy(cmd.parm.num, status + 3, sizeof(cmd.parm.num) - 1);
235 break;
236 case 6:
237 /* AOC */
238 sprintf(cmd.parm.num, "%d",
239 (int) simple_strtoul(status + 7, NULL, 16));
240 break;
241 case 7:
242 /* CAU */
243 status += 3;
244 if (strlen(status) == 4)
245 sprintf(cmd.parm.num, "%s%c%c",
246 status + 2, *status, *(status + 1));
247 else
248 strncpy(cmd.parm.num, status + 1, sizeof(cmd.parm.num) - 1);
249 break;
250 case 8:
251 /* Misc Errors on L1 and L2 */
252 card->flags &= ~ISDNLOOP_FLAGS_B1ACTIVE;
253 isdnloop_free_queue(card, 0);
254 cmd.arg = 0;
255 cmd.driver = card->myid;
256 card->interface.statcallb(&cmd);
257 cmd.command = ISDN_STAT_DHUP;
258 cmd.arg = 0;
259 cmd.driver = card->myid;
260 card->interface.statcallb(&cmd);
261 cmd.command = ISDN_STAT_BHUP;
262 card->flags &= ~ISDNLOOP_FLAGS_B2ACTIVE;
263 isdnloop_free_queue(card, 1);
264 cmd.arg = 1;
265 cmd.driver = card->myid;
266 card->interface.statcallb(&cmd);
267 cmd.command = ISDN_STAT_DHUP;
268 cmd.arg = 1;
269 cmd.driver = card->myid;
270 break;
271 }
272 card->interface.statcallb(&cmd);
273 }
274
275 /*
276 * Store a cwcharacter into ringbuffer for reading from /dev/isdnctrl
277 *
278 * Parameter:
279 * card = pointer to card struct.
280 * c = char to store.
281 */
282 static void
isdnloop_putmsg(isdnloop_card * card,unsigned char c)283 isdnloop_putmsg(isdnloop_card * card, unsigned char c)
284 {
285 ulong flags;
286
287 save_flags(flags);
288 cli();
289 *card->msg_buf_write++ = (c == 0xff) ? '\n' : c;
290 if (card->msg_buf_write == card->msg_buf_read) {
291 if (++card->msg_buf_read > card->msg_buf_end)
292 card->msg_buf_read = card->msg_buf;
293 }
294 if (card->msg_buf_write > card->msg_buf_end)
295 card->msg_buf_write = card->msg_buf;
296 restore_flags(flags);
297 }
298
299 /*
300 * Poll a virtual cards message queue.
301 * If there are new status-replies from the card, copy them to
302 * ringbuffer for reading on /dev/isdnctrl and call
303 * isdnloop_parse_status() for processing them. Watch for special
304 * Firmware bootmessage and parse it, to get the D-Channel protocol.
305 * If there are B-Channels open, initiate a timer-callback to
306 * isdnloop_pollbchan().
307 * This routine is called periodically via timer interrupt.
308 *
309 * Parameter:
310 * data = pointer to card struct
311 */
312 static void
isdnloop_polldchan(unsigned long data)313 isdnloop_polldchan(unsigned long data)
314 {
315 isdnloop_card *card = (isdnloop_card *) data;
316 struct sk_buff *skb;
317 int avail;
318 int left;
319 u_char c;
320 int ch;
321 unsigned long flags;
322 u_char *p;
323 isdn_ctrl cmd;
324
325 if ((skb = skb_dequeue(&card->dqueue)))
326 avail = skb->len;
327 else
328 avail = 0;
329 for (left = avail; left > 0; left--) {
330 c = *skb->data;
331 skb_pull(skb, 1);
332 isdnloop_putmsg(card, c);
333 card->imsg[card->iptr] = c;
334 if (card->iptr < 59)
335 card->iptr++;
336 if (!skb->len) {
337 avail++;
338 isdnloop_putmsg(card, '\n');
339 card->imsg[card->iptr] = 0;
340 card->iptr = 0;
341 if (card->imsg[0] == '0' && card->imsg[1] >= '0' &&
342 card->imsg[1] <= '2' && card->imsg[2] == ';') {
343 ch = (card->imsg[1] - '0') - 1;
344 p = &card->imsg[3];
345 isdnloop_parse_status(p, ch, card);
346 } else {
347 p = card->imsg;
348 if (!strncmp(p, "DRV1.", 5)) {
349 printk(KERN_INFO "isdnloop: (%s) %s\n", CID, p);
350 if (!strncmp(p + 7, "TC", 2)) {
351 card->ptype = ISDN_PTYPE_1TR6;
352 card->interface.features |= ISDN_FEATURE_P_1TR6;
353 printk(KERN_INFO
354 "isdnloop: (%s) 1TR6-Protocol loaded and running\n", CID);
355 }
356 if (!strncmp(p + 7, "EC", 2)) {
357 card->ptype = ISDN_PTYPE_EURO;
358 card->interface.features |= ISDN_FEATURE_P_EURO;
359 printk(KERN_INFO
360 "isdnloop: (%s) Euro-Protocol loaded and running\n", CID);
361 }
362 continue;
363
364 }
365 }
366 }
367 }
368 if (avail) {
369 cmd.command = ISDN_STAT_STAVAIL;
370 cmd.driver = card->myid;
371 cmd.arg = avail;
372 card->interface.statcallb(&cmd);
373 }
374 if (card->flags & (ISDNLOOP_FLAGS_B1ACTIVE | ISDNLOOP_FLAGS_B2ACTIVE))
375 if (!(card->flags & ISDNLOOP_FLAGS_RBTIMER)) {
376 /* schedule b-channel polling */
377 card->flags |= ISDNLOOP_FLAGS_RBTIMER;
378 save_flags(flags);
379 cli();
380 del_timer(&card->rb_timer);
381 card->rb_timer.function = isdnloop_pollbchan;
382 card->rb_timer.data = (unsigned long) card;
383 card->rb_timer.expires = jiffies + ISDNLOOP_TIMER_BCREAD;
384 add_timer(&card->rb_timer);
385 restore_flags(flags);
386 }
387 /* schedule again */
388 save_flags(flags);
389 cli();
390 card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD;
391 add_timer(&card->st_timer);
392 restore_flags(flags);
393 }
394
395 /*
396 * Append a packet to the transmit buffer-queue.
397 *
398 * Parameter:
399 * channel = Number of B-channel
400 * skb = packet to send.
401 * card = pointer to card-struct
402 * Return:
403 * Number of bytes transferred, -E??? on error
404 */
405 static int
isdnloop_sendbuf(int channel,struct sk_buff * skb,isdnloop_card * card)406 isdnloop_sendbuf(int channel, struct sk_buff *skb, isdnloop_card * card)
407 {
408 int len = skb->len;
409 unsigned long flags;
410 struct sk_buff *nskb;
411
412 if (len > 4000) {
413 printk(KERN_WARNING
414 "isdnloop: Send packet too large\n");
415 return -EINVAL;
416 }
417 if (len) {
418 if (!(card->flags & (channel) ? ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE))
419 return 0;
420 if (card->sndcount[channel] > ISDNLOOP_MAX_SQUEUE)
421 return 0;
422 save_flags(flags);
423 cli();
424 nskb = skb_clone(skb, GFP_ATOMIC);
425 if (nskb) {
426 skb_queue_tail(&card->bqueue[channel], nskb);
427 dev_kfree_skb(skb);
428 } else
429 len = 0;
430 card->sndcount[channel] += len;
431 restore_flags(flags);
432 }
433 return len;
434 }
435
436 /*
437 * Read the messages from the card's ringbuffer
438 *
439 * Parameter:
440 * buf = pointer to buffer.
441 * len = number of bytes to read.
442 * user = flag, 1: called from userlevel 0: called from kernel.
443 * card = pointer to card struct.
444 * Return:
445 * number of bytes actually transferred.
446 */
447 static int
isdnloop_readstatus(u_char * buf,int len,int user,isdnloop_card * card)448 isdnloop_readstatus(u_char * buf, int len, int user, isdnloop_card * card)
449 {
450 int count;
451 u_char *p;
452
453 for (p = buf, count = 0; count < len; p++, count++) {
454 if (card->msg_buf_read == card->msg_buf_write)
455 return count;
456 if (user)
457 put_user(*card->msg_buf_read++, p);
458 else
459 *p = *card->msg_buf_read++;
460 if (card->msg_buf_read > card->msg_buf_end)
461 card->msg_buf_read = card->msg_buf;
462 }
463 return count;
464 }
465
466 /*
467 * Simulate a card's response by appending it to the cards
468 * message queue.
469 *
470 * Parameter:
471 * card = pointer to card struct.
472 * s = pointer to message-string.
473 * ch = channel: 0 = generic messages, 1 and 2 = D-channel messages.
474 * Return:
475 * 0 on success, 1 on memory squeeze.
476 */
477 static int
isdnloop_fake(isdnloop_card * card,char * s,int ch)478 isdnloop_fake(isdnloop_card * card, char *s, int ch)
479 {
480 struct sk_buff *skb;
481 int len = strlen(s) + ((ch >= 0) ? 3 : 0);
482
483 if (!(skb = dev_alloc_skb(len))) {
484 printk(KERN_WARNING "isdnloop: Out of memory in isdnloop_fake\n");
485 return 1;
486 }
487 if (ch >= 0)
488 sprintf(skb_put(skb, 3), "%02d;", ch);
489 memcpy(skb_put(skb, strlen(s)), s, strlen(s));
490 skb_queue_tail(&card->dqueue, skb);
491 return 0;
492 }
493 /* *INDENT-OFF* */
494 static isdnloop_stat isdnloop_cmd_table[] =
495 {
496 {"BCON_R", 0, 1}, /* B-Channel connect */
497 {"BCON_I", 0, 17}, /* B-Channel connect ind */
498 {"BDIS_R", 0, 2}, /* B-Channel disconnect */
499 {"DDIS_R", 0, 3}, /* D-Channel disconnect */
500 {"DCON_R", 0, 16}, /* D-Channel connect */
501 {"DSCA_R", 0, 4}, /* Dial 1TR6-SPV */
502 {"DCAL_R", 0, 5}, /* Dial */
503 {"EAZC", 0, 6}, /* Clear EAZ listener */
504 {"EAZ", 0, 7}, /* Set EAZ listener */
505 {"SEEAZ", 0, 8}, /* Get EAZ listener */
506 {"MSN", 0, 9}, /* Set/Clear MSN listener */
507 {"MSALL", 0, 10}, /* Set multi MSN listeners */
508 {"SETSIL", 0, 11}, /* Set SI list */
509 {"SEESIL", 0, 12}, /* Get SI list */
510 {"SILC", 0, 13}, /* Clear SI list */
511 {"LOCK", 0, -1}, /* LOCK channel */
512 {"UNLOCK", 0, -1}, /* UNLOCK channel */
513 {"FV2ON", 1, 14}, /* Leased mode on */
514 {"FV2OFF", 1, 15}, /* Leased mode off */
515 {NULL, 0, -1}
516 };
517 /* *INDENT-ON* */
518
519
520 /*
521 * Simulate an error-response from a card.
522 *
523 * Parameter:
524 * card = pointer to card struct.
525 */
526 static void
isdnloop_fake_err(isdnloop_card * card)527 isdnloop_fake_err(isdnloop_card * card)
528 {
529 char buf[60];
530
531 sprintf(buf, "E%s", card->omsg);
532 isdnloop_fake(card, buf, -1);
533 isdnloop_fake(card, "NAK", -1);
534 }
535
536 static u_char ctable_eu[] =
537 {0x00, 0x11, 0x01, 0x12};
538 static u_char ctable_1t[] =
539 {0x00, 0x3b, 0x01, 0x3a};
540
541 /*
542 * Assemble a simplified cause message depending on the
543 * D-channel protocol used.
544 *
545 * Parameter:
546 * card = pointer to card struct.
547 * loc = location: 0 = local, 1 = remote.
548 * cau = cause: 1 = busy, 2 = nonexistent callerid, 3 = no user responding.
549 * Return:
550 * Pointer to buffer containing the assembled message.
551 */
552 static char *
isdnloop_unicause(isdnloop_card * card,int loc,int cau)553 isdnloop_unicause(isdnloop_card * card, int loc, int cau)
554 {
555 static char buf[6];
556
557 switch (card->ptype) {
558 case ISDN_PTYPE_EURO:
559 sprintf(buf, "E%02X%02X", (loc) ? 4 : 2, ctable_eu[cau]);
560 break;
561 case ISDN_PTYPE_1TR6:
562 sprintf(buf, "%02X44", ctable_1t[cau]);
563 break;
564 default:
565 return ("0000");
566 }
567 return (buf);
568 }
569
570 /*
571 * Release a virtual connection. Called from timer interrupt, when
572 * called party did not respond.
573 *
574 * Parameter:
575 * card = pointer to card struct.
576 * ch = channel (0-based)
577 */
578 static void
isdnloop_atimeout(isdnloop_card * card,int ch)579 isdnloop_atimeout(isdnloop_card * card, int ch)
580 {
581 unsigned long flags;
582 char buf[60];
583
584 save_flags(flags);
585 cli();
586 if (card->rcard) {
587 isdnloop_fake(card->rcard[ch], "DDIS_I", card->rch[ch] + 1);
588 card->rcard[ch]->rcard[card->rch[ch]] = NULL;
589 card->rcard[ch] = NULL;
590 }
591 isdnloop_fake(card, "DDIS_I", ch + 1);
592 /* No user responding */
593 sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 3));
594 isdnloop_fake(card, buf, ch + 1);
595 restore_flags(flags);
596 }
597
598 /*
599 * Wrapper for isdnloop_atimeout().
600 */
601 static void
isdnloop_atimeout0(unsigned long data)602 isdnloop_atimeout0(unsigned long data)
603 {
604 isdnloop_card *card = (isdnloop_card *) data;
605 isdnloop_atimeout(card, 0);
606 }
607
608 /*
609 * Wrapper for isdnloop_atimeout().
610 */
611 static void
isdnloop_atimeout1(unsigned long data)612 isdnloop_atimeout1(unsigned long data)
613 {
614 isdnloop_card *card = (isdnloop_card *) data;
615 isdnloop_atimeout(card, 1);
616 }
617
618 /*
619 * Install a watchdog for a user, not responding.
620 *
621 * Parameter:
622 * card = pointer to card struct.
623 * ch = channel to watch for.
624 */
625 static void
isdnloop_start_ctimer(isdnloop_card * card,int ch)626 isdnloop_start_ctimer(isdnloop_card * card, int ch)
627 {
628 unsigned long flags;
629
630 save_flags(flags);
631 cli();
632 init_timer(&card->c_timer[ch]);
633 card->c_timer[ch].expires = jiffies + ISDNLOOP_TIMER_ALERTWAIT;
634 if (ch)
635 card->c_timer[ch].function = isdnloop_atimeout1;
636 else
637 card->c_timer[ch].function = isdnloop_atimeout0;
638 card->c_timer[ch].data = (unsigned long) card;
639 add_timer(&card->c_timer[ch]);
640 restore_flags(flags);
641 }
642
643 /*
644 * Kill a pending channel watchdog.
645 *
646 * Parameter:
647 * card = pointer to card struct.
648 * ch = channel (0-based).
649 */
650 static void
isdnloop_kill_ctimer(isdnloop_card * card,int ch)651 isdnloop_kill_ctimer(isdnloop_card * card, int ch)
652 {
653 unsigned long flags;
654
655 save_flags(flags);
656 cli();
657 del_timer(&card->c_timer[ch]);
658 restore_flags(flags);
659 }
660
661 static u_char si2bit[] =
662 {0, 1, 0, 0, 0, 2, 0, 4, 0, 0};
663 static u_char bit2si[] =
664 {1, 5, 7};
665
666 /*
667 * Try finding a listener for an outgoing call.
668 *
669 * Parameter:
670 * card = pointer to calling card.
671 * p = pointer to ICN-type setup-string.
672 * lch = channel of calling card.
673 * cmd = pointer to struct to be filled when parsing setup.
674 * Return:
675 * 0 = found match, alerting should happen.
676 * 1 = found matching number but it is busy.
677 * 2 = no matching listener.
678 * 3 = found matching number but SI does not match.
679 */
680 static int
isdnloop_try_call(isdnloop_card * card,char * p,int lch,isdn_ctrl * cmd)681 isdnloop_try_call(isdnloop_card * card, char *p, int lch, isdn_ctrl * cmd)
682 {
683 isdnloop_card *cc = cards;
684 unsigned long flags;
685 int ch;
686 int num_match;
687 int i;
688 char *e;
689 char nbuf[32];
690
691 isdnloop_parse_setup(p, cmd);
692 while (cc) {
693 for (ch = 0; ch < 2; ch++) {
694 /* Exclude ourself */
695 if ((cc == card) && (ch == lch))
696 continue;
697 num_match = 0;
698 switch (cc->ptype) {
699 case ISDN_PTYPE_EURO:
700 for (i = 0; i < 3; i++)
701 if (!(strcmp(cc->s0num[i], cmd->parm.setup.phone)))
702 num_match = 1;
703 break;
704 case ISDN_PTYPE_1TR6:
705 e = cc->eazlist[ch];
706 while (*e) {
707 sprintf(nbuf, "%s%c", cc->s0num[0], *e);
708 if (!(strcmp(nbuf, cmd->parm.setup.phone)))
709 num_match = 1;
710 e++;
711 }
712 }
713 if (num_match) {
714 save_flags(flags);
715 cli();
716 /* channel idle? */
717 if (!(cc->rcard[ch])) {
718 /* Check SI */
719 if (!(si2bit[cmd->parm.setup.si1] & cc->sil[ch])) {
720 restore_flags(flags);
721 return 3;
722 }
723 /* ch is idle, si and number matches */
724 cc->rcard[ch] = card;
725 cc->rch[ch] = lch;
726 card->rcard[lch] = cc;
727 card->rch[lch] = ch;
728 restore_flags(flags);
729 return 0;
730 } else {
731 restore_flags(flags);
732 /* num matches, but busy */
733 if (ch == 1)
734 return 1;
735 }
736 }
737 }
738 cc = cc->next;
739 }
740 return 2;
741 }
742
743 /*
744 * Depending on D-channel protocol and caller/called, modify
745 * phone number.
746 *
747 * Parameter:
748 * card = pointer to card struct.
749 * phone = pointer phone number.
750 * caller = flag: 1 = caller, 0 = called.
751 * Return:
752 * pointer to new phone number.
753 */
754 static char *
isdnloop_vstphone(isdnloop_card * card,char * phone,int caller)755 isdnloop_vstphone(isdnloop_card * card, char *phone, int caller)
756 {
757 int i;
758 static char nphone[30];
759
760 switch (card->ptype) {
761 case ISDN_PTYPE_EURO:
762 if (caller) {
763 for (i = 0; i < 2; i++)
764 if (!(strcmp(card->s0num[i], phone)))
765 return (phone);
766 return (card->s0num[0]);
767 }
768 return (phone);
769 break;
770 case ISDN_PTYPE_1TR6:
771 if (caller) {
772 sprintf(nphone, "%s%c", card->s0num[0], phone[0]);
773 return (nphone);
774 } else
775 return (&phone[strlen(phone) - 1]);
776 break;
777 }
778 return ("\0");
779 }
780
781 /*
782 * Parse an ICN-type command string sent to the 'card'.
783 * Perform misc. actions depending on the command.
784 *
785 * Parameter:
786 * card = pointer to card struct.
787 */
788 static void
isdnloop_parse_cmd(isdnloop_card * card)789 isdnloop_parse_cmd(isdnloop_card * card)
790 {
791 char *p = card->omsg;
792 isdn_ctrl cmd;
793 char buf[60];
794 isdnloop_stat *s = isdnloop_cmd_table;
795 int action = -1;
796 int i;
797 int ch;
798
799 if ((card->omsg[0] != '0') && (card->omsg[2] != ';')) {
800 isdnloop_fake_err(card);
801 return;
802 }
803 ch = card->omsg[1] - '0';
804 if ((ch < 0) || (ch > 2)) {
805 isdnloop_fake_err(card);
806 return;
807 }
808 p += 3;
809 while (s->statstr) {
810 if (!strncmp(p, s->statstr, strlen(s->statstr))) {
811 action = s->action;
812 if (s->command && (ch != 0)) {
813 isdnloop_fake_err(card);
814 return;
815 }
816 break;
817 }
818 s++;
819 }
820 if (action == -1)
821 return;
822 switch (action) {
823 case 1:
824 /* 0x;BCON_R */
825 if (card->rcard[ch - 1]) {
826 isdnloop_fake(card->rcard[ch - 1], "BCON_I",
827 card->rch[ch - 1] + 1);
828 isdnloop_fake(card, "BCON_C", ch);
829 }
830 break;
831 case 17:
832 /* 0x;BCON_I */
833 if (card->rcard[ch - 1]) {
834 isdnloop_fake(card->rcard[ch - 1], "BCON_C",
835 card->rch[ch - 1] + 1);
836 }
837 break;
838 case 2:
839 /* 0x;BDIS_R */
840 isdnloop_fake(card, "BDIS_C", ch);
841 if (card->rcard[ch - 1]) {
842 isdnloop_fake(card->rcard[ch - 1], "BDIS_I",
843 card->rch[ch - 1] + 1);
844 }
845 break;
846 case 16:
847 /* 0x;DCON_R */
848 isdnloop_kill_ctimer(card, ch - 1);
849 if (card->rcard[ch - 1]) {
850 isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
851 isdnloop_fake(card->rcard[ch - 1], "DCON_C",
852 card->rch[ch - 1] + 1);
853 isdnloop_fake(card, "DCON_C", ch);
854 }
855 break;
856 case 3:
857 /* 0x;DDIS_R */
858 isdnloop_kill_ctimer(card, ch - 1);
859 if (card->rcard[ch - 1]) {
860 isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
861 isdnloop_fake(card->rcard[ch - 1], "DDIS_I",
862 card->rch[ch - 1] + 1);
863 card->rcard[ch - 1] = NULL;
864 }
865 isdnloop_fake(card, "DDIS_C", ch);
866 break;
867 case 4:
868 /* 0x;DSCA_Rdd,yy,zz,oo */
869 if (card->ptype != ISDN_PTYPE_1TR6) {
870 isdnloop_fake_err(card);
871 return;
872 }
873 /* Fall through */
874 case 5:
875 /* 0x;DCAL_Rdd,yy,zz,oo */
876 p += 6;
877 switch (isdnloop_try_call(card, p, ch - 1, &cmd)) {
878 case 0:
879 /* Alerting */
880 sprintf(buf, "D%s_I%s,%02d,%02d,%s",
881 (action == 4) ? "SCA" : "CAL",
882 isdnloop_vstphone(card, cmd.parm.setup.eazmsn, 1),
883 cmd.parm.setup.si1,
884 cmd.parm.setup.si2,
885 isdnloop_vstphone(card->rcard[ch],
886 cmd.parm.setup.phone, 0));
887 isdnloop_fake(card->rcard[ch - 1], buf, card->rch[ch - 1] + 1);
888 /* Fall through */
889 case 3:
890 /* si1 does not match, don't alert but start timer */
891 isdnloop_start_ctimer(card, ch - 1);
892 break;
893 case 1:
894 /* Remote busy */
895 isdnloop_fake(card, "DDIS_I", ch);
896 sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 1));
897 isdnloop_fake(card, buf, ch);
898 break;
899 case 2:
900 /* No such user */
901 isdnloop_fake(card, "DDIS_I", ch);
902 sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 2));
903 isdnloop_fake(card, buf, ch);
904 break;
905 }
906 break;
907 case 6:
908 /* 0x;EAZC */
909 card->eazlist[ch - 1][0] = '\0';
910 break;
911 case 7:
912 /* 0x;EAZ */
913 p += 3;
914 strcpy(card->eazlist[ch - 1], p);
915 break;
916 case 8:
917 /* 0x;SEEAZ */
918 sprintf(buf, "EAZ-LIST: %s", card->eazlist[ch - 1]);
919 isdnloop_fake(card, buf, ch + 1);
920 break;
921 case 9:
922 /* 0x;MSN */
923 break;
924 case 10:
925 /* 0x;MSNALL */
926 break;
927 case 11:
928 /* 0x;SETSIL */
929 p += 6;
930 i = 0;
931 while (strchr("0157", *p)) {
932 if (i)
933 card->sil[ch - 1] |= si2bit[*p - '0'];
934 i = (*p++ == '0');
935 }
936 if (*p)
937 isdnloop_fake_err(card);
938 break;
939 case 12:
940 /* 0x;SEESIL */
941 sprintf(buf, "SIN-LIST: ");
942 p = buf + 10;
943 for (i = 0; i < 3; i++)
944 if (card->sil[ch - 1] & (1 << i))
945 p += sprintf(p, "%02d", bit2si[i]);
946 isdnloop_fake(card, buf, ch + 1);
947 break;
948 case 13:
949 /* 0x;SILC */
950 card->sil[ch - 1] = 0;
951 break;
952 case 14:
953 /* 00;FV2ON */
954 break;
955 case 15:
956 /* 00;FV2OFF */
957 break;
958 }
959 }
960
961 /*
962 * Put command-strings into the of the 'card'. In reality, execute them
963 * right in place by calling isdnloop_parse_cmd(). Also copy every
964 * command to the read message ringbuffer, preceeding it with a '>'.
965 * These mesagges can be read at /dev/isdnctrl.
966 *
967 * Parameter:
968 * buf = pointer to command buffer.
969 * len = length of buffer data.
970 * user = flag: 1 = called form userlevel, 0 called from kernel.
971 * card = pointer to card struct.
972 * Return:
973 * number of bytes transferred (currently always equals len).
974 */
975 static int
isdnloop_writecmd(const u_char * buf,int len,int user,isdnloop_card * card)976 isdnloop_writecmd(const u_char * buf, int len, int user, isdnloop_card * card)
977 {
978 int xcount = 0;
979 int ocount = 1;
980 isdn_ctrl cmd;
981
982 while (len) {
983 int count = len;
984 u_char *p;
985 u_char msg[0x100];
986
987 if (count > 255)
988 count = 255;
989 if (user)
990 copy_from_user(msg, buf, count);
991 else
992 memcpy(msg, buf, count);
993 isdnloop_putmsg(card, '>');
994 for (p = msg; count > 0; count--, p++) {
995 len--;
996 xcount++;
997 isdnloop_putmsg(card, *p);
998 card->omsg[card->optr] = *p;
999 if (*p == '\n') {
1000 card->omsg[card->optr] = '\0';
1001 card->optr = 0;
1002 isdnloop_parse_cmd(card);
1003 if (len) {
1004 isdnloop_putmsg(card, '>');
1005 ocount++;
1006 }
1007 } else {
1008 if (card->optr < 59)
1009 card->optr++;
1010 }
1011 ocount++;
1012 }
1013 }
1014 cmd.command = ISDN_STAT_STAVAIL;
1015 cmd.driver = card->myid;
1016 cmd.arg = ocount;
1017 card->interface.statcallb(&cmd);
1018 return xcount;
1019 }
1020
1021 /*
1022 * Delete card's pending timers, send STOP to linklevel
1023 */
1024 static void
isdnloop_stopcard(isdnloop_card * card)1025 isdnloop_stopcard(isdnloop_card * card)
1026 {
1027 unsigned long flags;
1028 isdn_ctrl cmd;
1029
1030 save_flags(flags);
1031 cli();
1032 if (card->flags & ISDNLOOP_FLAGS_RUNNING) {
1033 card->flags &= ~ISDNLOOP_FLAGS_RUNNING;
1034 del_timer(&card->st_timer);
1035 del_timer(&card->rb_timer);
1036 del_timer(&card->c_timer[0]);
1037 del_timer(&card->c_timer[1]);
1038 cmd.command = ISDN_STAT_STOP;
1039 cmd.driver = card->myid;
1040 card->interface.statcallb(&cmd);
1041 }
1042 restore_flags(flags);
1043 }
1044
1045 /*
1046 * Stop all cards before unload.
1047 */
1048 static void
isdnloop_stopallcards(void)1049 isdnloop_stopallcards(void)
1050 {
1051 isdnloop_card *p = cards;
1052
1053 while (p) {
1054 isdnloop_stopcard(p);
1055 p = p->next;
1056 }
1057 }
1058
1059 /*
1060 * Start a 'card'. Simulate card's boot message and set the phone
1061 * number(s) of the virtual 'S0-Interface'. Install D-channel
1062 * poll timer.
1063 *
1064 * Parameter:
1065 * card = pointer to card struct.
1066 * sdefp = pointer to struct holding ioctl parameters.
1067 * Return:
1068 * 0 on success, -E??? otherwise.
1069 */
1070 static int
isdnloop_start(isdnloop_card * card,isdnloop_sdef * sdefp)1071 isdnloop_start(isdnloop_card * card, isdnloop_sdef * sdefp)
1072 {
1073 unsigned long flags;
1074 isdnloop_sdef sdef;
1075 int i;
1076
1077 if (card->flags & ISDNLOOP_FLAGS_RUNNING)
1078 return -EBUSY;
1079 copy_from_user((char *) &sdef, (char *) sdefp, sizeof(sdef));
1080 save_flags(flags);
1081 cli();
1082 switch (sdef.ptype) {
1083 case ISDN_PTYPE_EURO:
1084 if (isdnloop_fake(card, "DRV1.23EC-Q.931-CAPI-CNS-BASIS-20.02.96",
1085 -1)) {
1086 restore_flags(flags);
1087 return -ENOMEM;
1088 }
1089 card->sil[0] = card->sil[1] = 4;
1090 if (isdnloop_fake(card, "TEI OK", 0)) {
1091 restore_flags(flags);
1092 return -ENOMEM;
1093 }
1094 for (i = 0; i < 3; i++)
1095 strcpy(card->s0num[i], sdef.num[i]);
1096 break;
1097 case ISDN_PTYPE_1TR6:
1098 if (isdnloop_fake(card, "DRV1.04TC-1TR6-CAPI-CNS-BASIS-29.11.95",
1099 -1)) {
1100 restore_flags(flags);
1101 return -ENOMEM;
1102 }
1103 card->sil[0] = card->sil[1] = 4;
1104 if (isdnloop_fake(card, "TEI OK", 0)) {
1105 restore_flags(flags);
1106 return -ENOMEM;
1107 }
1108 strcpy(card->s0num[0], sdef.num[0]);
1109 card->s0num[1][0] = '\0';
1110 card->s0num[2][0] = '\0';
1111 break;
1112 default:
1113 restore_flags(flags);
1114 printk(KERN_WARNING "isdnloop: Illegal D-channel protocol %d\n",
1115 sdef.ptype);
1116 return -EINVAL;
1117 }
1118 init_timer(&card->st_timer);
1119 card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD;
1120 card->st_timer.function = isdnloop_polldchan;
1121 card->st_timer.data = (unsigned long) card;
1122 add_timer(&card->st_timer);
1123 card->flags |= ISDNLOOP_FLAGS_RUNNING;
1124 restore_flags(flags);
1125 return 0;
1126 }
1127
1128 /*
1129 * Main handler for commands sent by linklevel.
1130 */
1131 static int
isdnloop_command(isdn_ctrl * c,isdnloop_card * card)1132 isdnloop_command(isdn_ctrl * c, isdnloop_card * card)
1133 {
1134 ulong a;
1135 int i;
1136 char cbuf[60];
1137 isdn_ctrl cmd;
1138 isdnloop_cdef cdef;
1139
1140 switch (c->command) {
1141 case ISDN_CMD_IOCTL:
1142 memcpy(&a, c->parm.num, sizeof(ulong));
1143 switch (c->arg) {
1144 case ISDNLOOP_IOCTL_DEBUGVAR:
1145 return (ulong) card;
1146 case ISDNLOOP_IOCTL_STARTUP:
1147 if ((i = verify_area(VERIFY_READ, (void *) a, sizeof(isdnloop_sdef))))
1148 return i;
1149 return (isdnloop_start(card, (isdnloop_sdef *) a));
1150 break;
1151 case ISDNLOOP_IOCTL_ADDCARD:
1152 if ((i = verify_area(VERIFY_READ, (void *) a, sizeof(isdnloop_cdef))))
1153 return i;
1154 copy_from_user((char *) &cdef, (char *) a, sizeof(cdef));
1155 return (isdnloop_addcard(cdef.id1));
1156 break;
1157 case ISDNLOOP_IOCTL_LEASEDCFG:
1158 if (a) {
1159 if (!card->leased) {
1160 card->leased = 1;
1161 while (card->ptype == ISDN_PTYPE_UNKNOWN) {
1162 schedule_timeout(10);
1163 }
1164 schedule_timeout(10);
1165 sprintf(cbuf, "00;FV2ON\n01;EAZ1\n02;EAZ2\n");
1166 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1167 printk(KERN_INFO
1168 "isdnloop: (%s) Leased-line mode enabled\n",
1169 CID);
1170 cmd.command = ISDN_STAT_RUN;
1171 cmd.driver = card->myid;
1172 cmd.arg = 0;
1173 card->interface.statcallb(&cmd);
1174 }
1175 } else {
1176 if (card->leased) {
1177 card->leased = 0;
1178 sprintf(cbuf, "00;FV2OFF\n");
1179 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1180 printk(KERN_INFO
1181 "isdnloop: (%s) Leased-line mode disabled\n",
1182 CID);
1183 cmd.command = ISDN_STAT_RUN;
1184 cmd.driver = card->myid;
1185 cmd.arg = 0;
1186 card->interface.statcallb(&cmd);
1187 }
1188 }
1189 return 0;
1190 default:
1191 return -EINVAL;
1192 }
1193 break;
1194 case ISDN_CMD_DIAL:
1195 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1196 return -ENODEV;
1197 if (card->leased)
1198 break;
1199 if ((c->arg & 255) < ISDNLOOP_BCH) {
1200 char *p;
1201 char dial[50];
1202 char dcode[4];
1203
1204 a = c->arg;
1205 p = c->parm.setup.phone;
1206 if (*p == 's' || *p == 'S') {
1207 /* Dial for SPV */
1208 p++;
1209 strcpy(dcode, "SCA");
1210 } else
1211 /* Normal Dial */
1212 strcpy(dcode, "CAL");
1213 strcpy(dial, p);
1214 sprintf(cbuf, "%02d;D%s_R%s,%02d,%02d,%s\n", (int) (a + 1),
1215 dcode, dial, c->parm.setup.si1,
1216 c->parm.setup.si2, c->parm.setup.eazmsn);
1217 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1218 }
1219 break;
1220 case ISDN_CMD_ACCEPTD:
1221 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1222 return -ENODEV;
1223 if (c->arg < ISDNLOOP_BCH) {
1224 a = c->arg + 1;
1225 cbuf[0] = 0;
1226 switch (card->l2_proto[a - 1]) {
1227 case ISDN_PROTO_L2_X75I:
1228 sprintf(cbuf, "%02d;BX75\n", (int) a);
1229 break;
1230 #ifdef CONFIG_ISDN_X25
1231 case ISDN_PROTO_L2_X25DTE:
1232 sprintf(cbuf, "%02d;BX2T\n", (int) a);
1233 break;
1234 case ISDN_PROTO_L2_X25DCE:
1235 sprintf(cbuf, "%02d;BX2C\n", (int) a);
1236 break;
1237 #endif
1238 case ISDN_PROTO_L2_HDLC:
1239 sprintf(cbuf, "%02d;BTRA\n", (int) a);
1240 break;
1241 }
1242 if (strlen(cbuf))
1243 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1244 sprintf(cbuf, "%02d;DCON_R\n", (int) a);
1245 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1246 }
1247 break;
1248 case ISDN_CMD_ACCEPTB:
1249 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1250 return -ENODEV;
1251 if (c->arg < ISDNLOOP_BCH) {
1252 a = c->arg + 1;
1253 switch (card->l2_proto[a - 1]) {
1254 case ISDN_PROTO_L2_X75I:
1255 sprintf(cbuf, "%02d;BCON_R,BX75\n", (int) a);
1256 break;
1257 #ifdef CONFIG_ISDN_X25
1258 case ISDN_PROTO_L2_X25DTE:
1259 sprintf(cbuf, "%02d;BCON_R,BX2T\n", (int) a);
1260 break;
1261 case ISDN_PROTO_L2_X25DCE:
1262 sprintf(cbuf, "%02d;BCON_R,BX2C\n", (int) a);
1263 break;
1264 #endif
1265 case ISDN_PROTO_L2_HDLC:
1266 sprintf(cbuf, "%02d;BCON_R,BTRA\n", (int) a);
1267 break;
1268 default:
1269 sprintf(cbuf, "%02d;BCON_R\n", (int) a);
1270 }
1271 printk(KERN_DEBUG "isdnloop writecmd '%s'\n", cbuf);
1272 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1273 break;
1274 case ISDN_CMD_HANGUP:
1275 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1276 return -ENODEV;
1277 if (c->arg < ISDNLOOP_BCH) {
1278 a = c->arg + 1;
1279 sprintf(cbuf, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a, (int) a);
1280 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1281 }
1282 break;
1283 case ISDN_CMD_SETEAZ:
1284 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1285 return -ENODEV;
1286 if (card->leased)
1287 break;
1288 if (c->arg < ISDNLOOP_BCH) {
1289 a = c->arg + 1;
1290 if (card->ptype == ISDN_PTYPE_EURO) {
1291 sprintf(cbuf, "%02d;MS%s%s\n", (int) a,
1292 c->parm.num[0] ? "N" : "ALL", c->parm.num);
1293 } else
1294 sprintf(cbuf, "%02d;EAZ%s\n", (int) a,
1295 c->parm.num[0] ? c->parm.num : (u_char *) "0123456789");
1296 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1297 }
1298 break;
1299 case ISDN_CMD_CLREAZ:
1300 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1301 return -ENODEV;
1302 if (card->leased)
1303 break;
1304 if (c->arg < ISDNLOOP_BCH) {
1305 a = c->arg + 1;
1306 if (card->ptype == ISDN_PTYPE_EURO)
1307 sprintf(cbuf, "%02d;MSNC\n", (int) a);
1308 else
1309 sprintf(cbuf, "%02d;EAZC\n", (int) a);
1310 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1311 }
1312 break;
1313 case ISDN_CMD_SETL2:
1314 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1315 return -ENODEV;
1316 if ((c->arg & 255) < ISDNLOOP_BCH) {
1317 a = c->arg;
1318 switch (a >> 8) {
1319 case ISDN_PROTO_L2_X75I:
1320 sprintf(cbuf, "%02d;BX75\n", (int) (a & 255) + 1);
1321 break;
1322 #ifdef CONFIG_ISDN_X25
1323 case ISDN_PROTO_L2_X25DTE:
1324 sprintf(cbuf, "%02d;BX2T\n", (int) (a & 255) + 1);
1325 break;
1326 case ISDN_PROTO_L2_X25DCE:
1327 sprintf(cbuf, "%02d;BX2C\n", (int) (a & 255) + 1);
1328 break;
1329 #endif
1330 case ISDN_PROTO_L2_HDLC:
1331 sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
1332 break;
1333 default:
1334 return -EINVAL;
1335 }
1336 i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
1337 card->l2_proto[a & 255] = (a >> 8);
1338 }
1339 break;
1340 case ISDN_CMD_GETL2:
1341 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1342 return -ENODEV;
1343 if ((c->arg & 255) < ISDNLOOP_BCH)
1344 return card->l2_proto[c->arg & 255];
1345 else
1346 return -ENODEV;
1347 case ISDN_CMD_SETL3:
1348 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1349 return -ENODEV;
1350 return 0;
1351 case ISDN_CMD_GETL3:
1352 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1353 return -ENODEV;
1354 if ((c->arg & 255) < ISDNLOOP_BCH)
1355 return ISDN_PROTO_L3_TRANS;
1356 else
1357 return -ENODEV;
1358 case ISDN_CMD_GETEAZ:
1359 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1360 return -ENODEV;
1361 break;
1362 case ISDN_CMD_SETSIL:
1363 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1364 return -ENODEV;
1365 break;
1366 case ISDN_CMD_GETSIL:
1367 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1368 return -ENODEV;
1369 break;
1370 case ISDN_CMD_LOCK:
1371 MOD_INC_USE_COUNT;
1372 break;
1373 case ISDN_CMD_UNLOCK:
1374 MOD_DEC_USE_COUNT;
1375 break;
1376 default:
1377 return -EINVAL;
1378 }
1379 }
1380 return 0;
1381 }
1382
1383 /*
1384 * Find card with given driverId
1385 */
1386 static inline isdnloop_card *
isdnloop_findcard(int driverid)1387 isdnloop_findcard(int driverid)
1388 {
1389 isdnloop_card *p = cards;
1390
1391 while (p) {
1392 if (p->myid == driverid)
1393 return p;
1394 p = p->next;
1395 }
1396 return (isdnloop_card *) 0;
1397 }
1398
1399 /*
1400 * Wrapper functions for interface to linklevel
1401 */
1402 static int
if_command(isdn_ctrl * c)1403 if_command(isdn_ctrl * c)
1404 {
1405 isdnloop_card *card = isdnloop_findcard(c->driver);
1406
1407 if (card)
1408 return (isdnloop_command(c, card));
1409 printk(KERN_ERR
1410 "isdnloop: if_command called with invalid driverId!\n");
1411 return -ENODEV;
1412 }
1413
1414 static int
if_writecmd(const u_char * buf,int len,int user,int id,int channel)1415 if_writecmd(const u_char * buf, int len, int user, int id, int channel)
1416 {
1417 isdnloop_card *card = isdnloop_findcard(id);
1418
1419 if (card) {
1420 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1421 return -ENODEV;
1422 return (isdnloop_writecmd(buf, len, user, card));
1423 }
1424 printk(KERN_ERR
1425 "isdnloop: if_writecmd called with invalid driverId!\n");
1426 return -ENODEV;
1427 }
1428
1429 static int
if_readstatus(u_char * buf,int len,int user,int id,int channel)1430 if_readstatus(u_char * buf, int len, int user, int id, int channel)
1431 {
1432 isdnloop_card *card = isdnloop_findcard(id);
1433
1434 if (card) {
1435 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1436 return -ENODEV;
1437 return (isdnloop_readstatus(buf, len, user, card));
1438 }
1439 printk(KERN_ERR
1440 "isdnloop: if_readstatus called with invalid driverId!\n");
1441 return -ENODEV;
1442 }
1443
1444 static int
if_sendbuf(int id,int channel,int ack,struct sk_buff * skb)1445 if_sendbuf(int id, int channel, int ack, struct sk_buff *skb)
1446 {
1447 isdnloop_card *card = isdnloop_findcard(id);
1448
1449 if (card) {
1450 if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
1451 return -ENODEV;
1452 /* ack request stored in skb scratch area */
1453 *(skb->head) = ack;
1454 return (isdnloop_sendbuf(channel, skb, card));
1455 }
1456 printk(KERN_ERR
1457 "isdnloop: if_sendbuf called with invalid driverId!\n");
1458 return -ENODEV;
1459 }
1460
1461 /*
1462 * Allocate a new card-struct, initialize it
1463 * link it into cards-list and register it at linklevel.
1464 */
1465 static isdnloop_card *
isdnloop_initcard(char * id)1466 isdnloop_initcard(char *id)
1467 {
1468 isdnloop_card *card;
1469 int i;
1470
1471 if (!(card = (isdnloop_card *) kmalloc(sizeof(isdnloop_card), GFP_KERNEL))) {
1472 printk(KERN_WARNING
1473 "isdnloop: (%s) Could not allocate card-struct.\n", id);
1474 return (isdnloop_card *) 0;
1475 }
1476 memset((char *) card, 0, sizeof(isdnloop_card));
1477 card->interface.channels = ISDNLOOP_BCH;
1478 card->interface.hl_hdrlen = 1; /* scratch area for storing ack flag*/
1479 card->interface.maxbufsize = 4000;
1480 card->interface.command = if_command;
1481 card->interface.writebuf_skb = if_sendbuf;
1482 card->interface.writecmd = if_writecmd;
1483 card->interface.readstat = if_readstatus;
1484 card->interface.features = ISDN_FEATURE_L2_X75I |
1485 #ifdef CONFIG_ISDN_X25
1486 ISDN_FEATURE_L2_X25DTE |
1487 ISDN_FEATURE_L2_X25DCE |
1488 #endif
1489 ISDN_FEATURE_L2_HDLC |
1490 ISDN_FEATURE_L3_TRANS |
1491 ISDN_FEATURE_P_UNKNOWN;
1492 card->ptype = ISDN_PTYPE_UNKNOWN;
1493 strncpy(card->interface.id, id, sizeof(card->interface.id) - 1);
1494 card->msg_buf_write = card->msg_buf;
1495 card->msg_buf_read = card->msg_buf;
1496 card->msg_buf_end = &card->msg_buf[sizeof(card->msg_buf) - 1];
1497 for (i = 0; i < ISDNLOOP_BCH; i++) {
1498 card->l2_proto[i] = ISDN_PROTO_L2_X75I;
1499 skb_queue_head_init(&card->bqueue[i]);
1500 }
1501 skb_queue_head_init(&card->dqueue);
1502 card->next = cards;
1503 cards = card;
1504 if (!register_isdn(&card->interface)) {
1505 cards = cards->next;
1506 printk(KERN_WARNING
1507 "isdnloop: Unable to register %s\n", id);
1508 kfree(card);
1509 return (isdnloop_card *) 0;
1510 }
1511 card->myid = card->interface.channels;
1512 return card;
1513 }
1514
1515 static int
isdnloop_addcard(char * id1)1516 isdnloop_addcard(char *id1)
1517 {
1518 isdnloop_card *card;
1519
1520 if (!(card = isdnloop_initcard(id1))) {
1521 return -EIO;
1522 }
1523 printk(KERN_INFO
1524 "isdnloop: (%s) virtual card added\n",
1525 card->interface.id);
1526 return 0;
1527 }
1528
1529 static int __init
isdnloop_init(void)1530 isdnloop_init(void)
1531 {
1532 char *p;
1533 char rev[10];
1534
1535 /* No symbols to export, hide all symbols */
1536 EXPORT_NO_SYMBOLS;
1537
1538 if ((p = strchr(revision, ':'))) {
1539 strcpy(rev, p + 1);
1540 p = strchr(rev, '$');
1541 *p = 0;
1542 } else
1543 strcpy(rev, " ??? ");
1544 printk(KERN_NOTICE "isdnloop-ISDN-driver Rev%s\n", rev);
1545
1546 if (isdnloop_id)
1547 return (isdnloop_addcard(isdnloop_id));
1548
1549 return 0;
1550 }
1551
1552 static void __exit
isdnloop_exit(void)1553 isdnloop_exit(void)
1554 {
1555 isdn_ctrl cmd;
1556 isdnloop_card *card = cards;
1557 isdnloop_card *last;
1558 int i;
1559
1560 isdnloop_stopallcards();
1561 while (card) {
1562 cmd.command = ISDN_STAT_UNLOAD;
1563 cmd.driver = card->myid;
1564 card->interface.statcallb(&cmd);
1565 for (i = 0; i < ISDNLOOP_BCH; i++)
1566 isdnloop_free_queue(card, i);
1567 card = card->next;
1568 }
1569 card = cards;
1570 while (card) {
1571 last = card;
1572 skb_queue_purge(&card->dqueue);
1573 card = card->next;
1574 kfree(last);
1575 }
1576 printk(KERN_NOTICE "isdnloop-ISDN-driver unloaded\n");
1577 }
1578
1579 module_init(isdnloop_init);
1580 module_exit(isdnloop_exit);
1581