12 #include <linux/module.h>
14 #include <linux/slab.h>
16 #include <linux/sched.h>
19 static char *isdnloop_id =
"loop0";
27 static int isdnloop_addcard(
char *);
37 isdnloop_free_queue(isdnloop_card *
card,
int channel)
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]) {
64 card->sndcount[ch] -=
len;
69 rcard->interface.rcvcallb_skb(rcard->myid, rch, skb);
77 card->interface.statcallb(&cmd);
79 card->sndcount[ch] = 0;
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)) {
104 card->rb_timer.expires =
jiffies + ISDNLOOP_TIMER_BCREAD;
106 card->flags |= ISDNLOOP_FLAGS_RBTIMER;
107 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
109 card->flags &= ~ISDNLOOP_FLAGS_RBTIMER;
167 {
"E_L1: ACTIVATION FAILED",
207 ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE;
212 ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE);
213 isdnloop_free_queue(card, channel);
217 isdnloop_parse_setup(status + 6, &cmd);
242 status + 2, *status, *(status + 1));
248 card->flags &= ~ISDNLOOP_FLAGS_B1ACTIVE;
249 isdnloop_free_queue(card, 0);
252 card->interface.statcallb(&cmd);
256 card->interface.statcallb(&cmd);
258 card->flags &= ~ISDNLOOP_FLAGS_B2ACTIVE;
259 isdnloop_free_queue(card, 1);
262 card->interface.statcallb(&cmd);
268 card->interface.statcallb(&cmd);
279 isdnloop_putmsg(isdnloop_card *card,
unsigned char c)
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);
308 isdnloop_polldchan(
unsigned long data)
310 isdnloop_card *card = (isdnloop_card *) data;
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);
345 if (!
strncmp(p + 7,
"TC", 2)) {
349 "isdnloop: (%s) 1TR6-Protocol loaded and running\n", CID);
351 if (!
strncmp(p + 7,
"EC", 2)) {
355 "isdnloop: (%s) Euro-Protocol loaded and running\n", CID);
367 card->interface.statcallb(&cmd);
369 if (card->flags & (ISDNLOOP_FLAGS_B1ACTIVE | ISDNLOOP_FLAGS_B2ACTIVE))
370 if (!(card->flags & ISDNLOOP_FLAGS_RBTIMER)) {
372 card->flags |= ISDNLOOP_FLAGS_RBTIMER;
375 card->rb_timer.function = isdnloop_pollbchan;
376 card->rb_timer.data = (
unsigned long) card;
377 card->rb_timer.expires =
jiffies + ISDNLOOP_TIMER_BCREAD;
379 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
383 card->st_timer.expires =
jiffies + ISDNLOOP_TIMER_DCREAD;
385 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
399 isdnloop_sendbuf(
int channel,
struct sk_buff *skb, isdnloop_card *card)
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)
416 nskb = dev_alloc_skb(skb->
len);
418 skb_copy_from_linear_data(skb,
425 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
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;
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))) {
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};
545 isdnloop_unicause(isdnloop_card *card,
int loc,
int cau)
549 switch (card->ptype) {
551 sprintf(buf,
"E%02X%02X", (loc) ? 4 : 2, ctable_eu[cau]);
554 sprintf(buf,
"%02X44", ctable_1t[cau]);
571 isdnloop_atimeout(isdnloop_card *card,
int ch)
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);
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);
593 isdnloop_atimeout0(
unsigned long data)
595 isdnloop_card *card = (isdnloop_card *) data;
596 isdnloop_atimeout(card, 0);
603 isdnloop_atimeout1(
unsigned long data)
605 isdnloop_card *card = (isdnloop_card *) data;
606 isdnloop_atimeout(card, 1);
617 isdnloop_start_ctimer(isdnloop_card *card,
int 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;
630 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
641 isdnloop_kill_ctimer(isdnloop_card *card,
int ch)
647 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
651 {0, 1, 0, 0, 0, 2, 0, 4, 0, 0};
670 isdnloop_try_call(isdnloop_card *card,
char *p,
int lch,
isdn_ctrl *cmd)
680 isdnloop_parse_setup(p, cmd);
682 for (ch = 0; ch < 2; ch++) {
684 if ((cc == card) && (ch == lch))
689 for (i = 0; i < 3; i++)
696 sprintf(nbuf,
"%s%c", cc->s0num[0], *e);
705 if (!(cc->rcard[ch])) {
708 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
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);
743 isdnloop_vstphone(isdnloop_card *card,
char *phone,
int caller)
746 static char nphone[30];
752 switch (card->ptype) {
755 for (i = 0; i < 2; i++)
756 if (!(
strcmp(card->s0num[i], phone)))
758 return (card->s0num[0]);
764 sprintf(nphone,
"%s%c", card->s0num[0], phone[0]);
767 return (&phone[
strlen(phone) - 1]);
781 isdnloop_parse_cmd(isdnloop_card *card)
783 char *p = card->omsg;
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);
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);
862 isdnloop_fake_err(card);
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",
877 isdnloop_vstphone(card->rcard[ch - 1],
879 isdnloop_fake(card->rcard[ch - 1], buf, card->rch[ch - 1] + 1);
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);
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;
968 isdnloop_writecmd(
const u_char *buf,
int len,
int user, isdnloop_card *card)
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)
1010 card->interface.statcallb(&cmd);
1018 isdnloop_stopcard(isdnloop_card *card)
1020 unsigned long flags;
1024 if (card->flags & ISDNLOOP_FLAGS_RUNNING) {
1025 card->flags &= ~ISDNLOOP_FLAGS_RUNNING;
1032 card->interface.statcallb(&cmd);
1034 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1041 isdnloop_stopallcards(
void)
1043 isdnloop_card *p =
cards;
1046 isdnloop_stopcard(p);
1065 unsigned long flags;
1069 if (card->flags & ISDNLOOP_FLAGS_RUNNING)
1071 if (
copy_from_user((
char *) &sdef, (
char *) sdefp,
sizeof(sdef)))
1074 switch (sdef.
ptype) {
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++)
1090 if (isdnloop_fake(card,
"DRV1.04TC-1TR6-CAPI-CNS-BASIS-29.11.95",
1092 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1095 card->sil[0] = card->sil[1] = 4;
1096 if (isdnloop_fake(card,
"TEI OK", 0)) {
1097 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1101 card->s0num[1][0] =
'\0';
1102 card->s0num[2][0] =
'\0';
1105 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1111 card->st_timer.expires =
jiffies + ISDNLOOP_TIMER_DCREAD;
1112 card->st_timer.function = isdnloop_polldchan;
1113 card->st_timer.data = (
unsigned long) card;
1115 card->flags |= ISDNLOOP_FLAGS_RUNNING;
1116 spin_unlock_irqrestore(&card->isdnloop_lock, flags);
1124 isdnloop_command(
isdn_ctrl *c, isdnloop_card *card)
1148 return (isdnloop_addcard(cdef.
id1));
1152 if (!card->leased) {
1157 sprintf(cbuf,
"00;FV2ON\n01;EAZ1\n02;EAZ2\n");
1158 i = isdnloop_writecmd(cbuf,
strlen(cbuf), 0, card);
1160 "isdnloop: (%s) Leased-line mode enabled\n",
1165 card->interface.statcallb(&cmd);
1171 i = isdnloop_writecmd(cbuf,
strlen(cbuf), 0, card);
1173 "isdnloop: (%s) Leased-line mode disabled\n",
1178 card->interface.statcallb(&cmd);
1187 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1191 if ((c->
arg & 255) < ISDNLOOP_BCH) {
1198 if (*p ==
's' || *p ==
'S') {
1206 sprintf(cbuf,
"%02d;D%s_R%s,%02d,%02d,%s\n", (
int) (a + 1),
1209 i = isdnloop_writecmd(cbuf,
strlen(cbuf), 0, card);
1213 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1215 if (c->
arg < ISDNLOOP_BCH) {
1218 switch (card->l2_proto[a - 1]) {
1220 sprintf(cbuf,
"%02d;BX75\n", (
int) a);
1222 #ifdef CONFIG_ISDN_X25
1224 sprintf(cbuf,
"%02d;BX2T\n", (
int) a);
1227 sprintf(cbuf,
"%02d;BX2C\n", (
int) a);
1231 sprintf(cbuf,
"%02d;BTRA\n", (
int) a);
1235 i = isdnloop_writecmd(cbuf,
strlen(cbuf), 0, card);
1236 sprintf(cbuf,
"%02d;DCON_R\n", (
int) a);
1237 i = isdnloop_writecmd(cbuf,
strlen(cbuf), 0, card);
1241 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1243 if (c->
arg < ISDNLOOP_BCH) {
1245 switch (card->l2_proto[a - 1]) {
1247 sprintf(cbuf,
"%02d;BCON_R,BX75\n", (
int) a);
1249 #ifdef CONFIG_ISDN_X25
1251 sprintf(cbuf,
"%02d;BCON_R,BX2T\n", (
int) a);
1254 sprintf(cbuf,
"%02d;BCON_R,BX2C\n", (
int) a);
1258 sprintf(cbuf,
"%02d;BCON_R,BTRA\n", (
int) a);
1261 sprintf(cbuf,
"%02d;BCON_R\n", (
int) a);
1264 i = isdnloop_writecmd(cbuf,
strlen(cbuf), 0, card);
1267 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1269 if (c->
arg < ISDNLOOP_BCH) {
1271 sprintf(cbuf,
"%02d;BDIS_R\n%02d;DDIS_R\n", (
int) a, (
int) a);
1272 i = isdnloop_writecmd(cbuf,
strlen(cbuf), 0, card);
1276 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1280 if (c->
arg < ISDNLOOP_BCH) {
1283 sprintf(cbuf,
"%02d;MS%s%s\n", (
int) a,
1286 sprintf(cbuf,
"%02d;EAZ%s\n", (
int) a,
1288 i = isdnloop_writecmd(cbuf,
strlen(cbuf), 0, card);
1292 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1296 if (c->
arg < ISDNLOOP_BCH) {
1299 sprintf(cbuf,
"%02d;MSNC\n", (
int) a);
1301 sprintf(cbuf,
"%02d;EAZC\n", (
int) a);
1302 i = isdnloop_writecmd(cbuf,
strlen(cbuf), 0, card);
1306 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1308 if ((c->
arg & 255) < ISDNLOOP_BCH) {
1312 sprintf(cbuf,
"%02d;BX75\n", (
int) (a & 255) + 1);
1314 #ifdef CONFIG_ISDN_X25
1316 sprintf(cbuf,
"%02d;BX2T\n", (
int) (a & 255) + 1);
1319 sprintf(cbuf,
"%02d;BX2C\n", (
int) (a & 255) + 1);
1323 sprintf(cbuf,
"%02d;BTRA\n", (
int) (a & 255) + 1);
1326 sprintf(cbuf,
"%02d;BTRA\n", (
int) (a & 255) + 1);
1331 i = isdnloop_writecmd(cbuf,
strlen(cbuf), 0, card);
1332 card->l2_proto[a & 255] = (a >> 8);
1336 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1349 static inline isdnloop_card *
1350 isdnloop_findcard(
int driverid)
1352 isdnloop_card *p =
cards;
1355 if (p->myid == driverid)
1359 return (isdnloop_card *) 0;
1368 isdnloop_card *card = isdnloop_findcard(c->
driver);
1371 return (isdnloop_command(c, card));
1373 "isdnloop: if_command called with invalid driverId!\n");
1378 if_writecmd(
const u_char __user *buf,
int len,
int id,
int channel)
1380 isdnloop_card *card = isdnloop_findcard(
id);
1383 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1385 return (isdnloop_writecmd(buf, len, 1, card));
1388 "isdnloop: if_writecmd called with invalid driverId!\n");
1393 if_readstatus(
u_char __user *buf,
int len,
int id,
int channel)
1395 isdnloop_card *card = isdnloop_findcard(
id);
1398 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1400 return (isdnloop_readstatus(buf, len, card));
1403 "isdnloop: if_readstatus called with invalid driverId!\n");
1408 if_sendbuf(
int id,
int channel,
int ack,
struct sk_buff *skb)
1410 isdnloop_card *card = isdnloop_findcard(
id);
1413 if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
1417 return (isdnloop_sendbuf(channel, skb, card));
1420 "isdnloop: if_sendbuf called with invalid driverId!\n");
1428 static isdnloop_card *
1429 isdnloop_initcard(
char *
id)
1431 isdnloop_card *
card;
1434 if (!(card = kzalloc(
sizeof(isdnloop_card),
GFP_KERNEL))) {
1436 "isdnloop: (%s) Could not allocate card-struct.\n",
id);
1437 return (isdnloop_card *) 0;
1440 card->interface.channels = ISDNLOOP_BCH;
1441 card->interface.hl_hdrlen = 1;
1442 card->interface.maxbufsize = 4000;
1443 card->interface.command = if_command;
1444 card->interface.writebuf_skb = if_sendbuf;
1445 card->interface.writecmd = if_writecmd;
1446 card->interface.readstat = if_readstatus;
1448 #ifdef CONFIG_ISDN_X25
1456 strlcpy(card->interface.id,
id,
sizeof(card->interface.id));
1457 card->msg_buf_write = card->msg_buf;
1458 card->msg_buf_read = card->msg_buf;
1459 card->msg_buf_end = &card->msg_buf[
sizeof(card->msg_buf) - 1];
1460 for (i = 0; i < ISDNLOOP_BCH; i++) {
1462 skb_queue_head_init(&card->bqueue[i]);
1464 skb_queue_head_init(&card->dqueue);
1471 "isdnloop: Unable to register %s\n",
id);
1473 return (isdnloop_card *) 0;
1475 card->myid = card->interface.channels;
1480 isdnloop_addcard(
char *
id1)
1482 isdnloop_card *
card;
1484 if (!(card = isdnloop_initcard(id1))) {
1488 "isdnloop: (%s) virtual card added\n",
1489 card->interface.id);
1497 return (isdnloop_addcard(isdnloop_id));
1506 isdnloop_card *card =
cards;
1507 isdnloop_card *
last;
1510 isdnloop_stopallcards();
1514 card->interface.statcallb(&cmd);
1515 for (i = 0; i < ISDNLOOP_BCH; i++)
1516 isdnloop_free_queue(card, i);