patch-2.2.11 linux/drivers/isdn/hisax/callc.c

Next file: linux/drivers/isdn/hisax/cert.c
Previous file: linux/drivers/isdn/hisax/bkm_ax.h
Back to the patch index
Back to the overall index

diff -u --recursive --new-file v2.2.10/linux/drivers/isdn/hisax/callc.c linux/drivers/isdn/hisax/callc.c
@@ -1,12 +1,74 @@
-/* $Id: callc.c,v 2.13 1998/02/12 23:07:16 keil Exp $
+/* $Id: callc.c,v 2.29 1999/07/13 21:05:41 werner Exp $
 
- * Author       Karsten Keil (keil@temic-ech.spacenet.de)
+ * Author       Karsten Keil (keil@isdn4linux.de)
  *              based on the teles driver from Jan den Ouden
  *
+ *		This file is (c) under GNU PUBLIC LICENSE
+ *		For changes and modifications please read
+ *		../../../Documentation/isdn/HiSax.cert
+ *
  * Thanks to    Jan den Ouden
  *              Fritz Elfert
  *
  * $Log: callc.c,v $
+ * Revision 2.29  1999/07/13 21:05:41  werner
+ * Modified set_channel_limit to use new callback ISDN_STAT_DISCH.
+ *
+ * Revision 2.28  1999/07/09 08:30:02  keil
+ * cosmetics
+ *
+ * Revision 2.27  1999/07/05 23:51:38  werner
+ * Allow limiting of available HiSax B-chans per card. Controlled by hisaxctrl
+ * hisaxctrl id 10 <nr. of chans 0-2>
+ *
+ * Revision 2.26  1999/07/01 08:11:21  keil
+ * Common HiSax version for 2.0, 2.1, 2.2 and 2.3 kernel
+ *
+ * Revision 2.25  1999/01/02 11:17:20  keil
+ * Changes for 2.2
+ *
+ * Revision 2.24  1998/11/15 23:54:24  keil
+ * changes from 2.0
+ *
+ * Revision 2.23  1998/09/30 22:21:57  keil
+ * cosmetics
+ *
+ * Revision 2.22  1998/08/20 13:50:29  keil
+ * More support for hybrid modem (not working yet)
+ *
+ * Revision 2.21  1998/08/13 23:36:15  keil
+ * HiSax 3.1 - don't work stable with current LinkLevel
+ *
+ * Revision 2.20  1998/06/26 15:13:05  fritz
+ * Added handling of STAT_ICALL with incomplete CPN.
+ * Added AT&L for ttyI emulator.
+ * Added more locking stuff in tty_write.
+ *
+ * Revision 2.19  1998/05/25 14:08:06  keil
+ * HiSax 3.0
+ * fixed X.75 and leased line to work again
+ * Point2Point and fixed TEI are runtime options now:
+ *    hisaxctrl <id> 7 1  set PTP
+ *    hisaxctrl <id> 8 <TEIVALUE *2 >
+ *    set fixed TEI to TEIVALUE (0-63)
+ *
+ * Revision 2.18  1998/05/25 12:57:40  keil
+ * HiSax golden code from certification, Don't use !!!
+ * No leased lines, no X75, but many changes.
+ *
+ * Revision 2.17  1998/04/15 16:46:06  keil
+ * RESUME support
+ *
+ * Revision 2.16  1998/04/10 10:35:17  paul
+ * fixed (silly?) warnings from egcs on Alpha.
+ *
+ * Revision 2.15  1998/03/19 13:18:37  keil
+ * Start of a CAPI like interface for supplementary Service
+ * first service: SUSPEND
+ *
+ * Revision 2.14  1998/03/07 22:56:54  tsbogend
+ * made HiSax working on Linux/Alpha
+ *
  * Revision 2.13  1998/02/12 23:07:16  keil
  * change for 2.1.86 (removing FREE_READ/FREE_WRITE from [dev]_kfree_skb()
  *
@@ -60,12 +122,18 @@
 
 #define __NO_VERSION__
 #include "hisax.h"
+#include "../avmb1/capicmd.h"  /* this should be moved in a common place */
 
 #ifdef MODULE
+#ifdef COMPAT_HAS_NEW_SYMTAB
 #define MOD_USE_COUNT ( GET_USE_COUNT (&__this_module))
-#endif				/* MODULE */
+#else
+extern long mod_use_count_;
+#define MOD_USE_COUNT mod_use_count_
+#endif /* COMPAT_HAS_NEW_SYMTAB */
+#endif	/* MODULE */
 
-const char *lli_revision = "$Revision: 2.13 $";
+const char *lli_revision = "$Revision: 2.29 $";
 
 extern struct IsdnCard cards[];
 extern int nrcards;
@@ -77,20 +145,18 @@
 
 static struct Fsm callcfsm =
 {NULL, 0, 0, NULL, NULL};
-static struct Fsm lcfsm =
-{NULL, 0, 0, NULL, NULL};
 
 static int chancount = 0;
 
-/* experimental REJECT after ALERTING for CALLBACK to beat the 4s delay */ 
-#define ALERT_REJECT 1
+/* experimental REJECT after ALERTING for CALLBACK to beat the 4s delay */
+#define ALERT_REJECT 0
 
 /* Value to delay the sending of the first B-channel paket after CONNECT
  * here is no value given by ITU, but experience shows that 300 ms will
  * work on many networks, if you or your other side is behind local exchanges
  * a greater value may be recommented. If the delay is to short the first paket
  * will be lost and autodetect on many comercial routers goes wrong !
- * You can adjust this value on runtime with 
+ * You can adjust this value on runtime with
  * hisaxctrl <id> 2 <value>
  * value is in milliseconds
  */
@@ -98,27 +164,7 @@
 
 /* Flags for remembering action done in lli */
 
-#define  FLG_START_D	0
-#define  FLG_ESTAB_D	1
-#define  FLG_CALL_SEND	2
-#define  FLG_CALL_REC   3
-#define  FLG_CALL_ALERT	4
-#define  FLG_START_B	5
-#define  FLG_CONNECT_B	6
-#define  FLG_LL_DCONN	7
-#define  FLG_LL_BCONN	8
-#define  FLG_DISC_SEND	9
-#define  FLG_DISC_REC	10
-#define  FLG_REL_REC	11
-#define  FLG_DO_ALERT	12
-#define  FLG_DO_HANGUP	13
-#define  FLG_DO_CONNECT	14
-#define  FLG_DO_ESTAB	15
-
-/*
- * Because of callback it's a good idea to delay the shutdown of the d-channel
- */
-#define	DREL_TIMER_VALUE 10000
+#define  FLG_START_B	0
 
 /*
  * Find card with given driverId
@@ -136,55 +182,66 @@
 	return (struct IsdnCardState *) 0;
 }
 
-static void
-link_debug(struct Channel *chanp, char *s, int direction)
+int
+discard_queue(struct sk_buff_head *q)
 {
-	char tmp[100], tm[32];
+	struct sk_buff *skb;
+	int ret=0;
 
-	jiftime(tm, jiffies);
-	sprintf(tmp, "%s Channel %d %s %s\n", tm, chanp->chan,
-		direction ? "LL->HL" : "HL->LL", s);
-	HiSax_putstatus(chanp->cs, tmp);
+	while ((skb = skb_dequeue(q))) {
+		idev_kfree_skb(skb, FREE_READ);
+		ret++;
+	}
+	return(ret);
 }
 
+static void
+link_debug(struct Channel *chanp, int direction, char *fmt, ...)
+{
+	va_list args;
+	char tmp[16];
+
+	va_start(args, fmt);
+	sprintf(tmp, "Ch%d %s ", chanp->chan,
+		direction ? "LL->HL" : "HL->LL");
+	VHiSax_putstatus(chanp->cs, tmp, fmt, args);
+	va_end(args);
+}
 
 enum {
-	ST_NULL,		/*  0 inactive */
-	ST_OUT_WAIT_D,		/*  1 outgoing, awaiting d-channel establishment */
-	ST_IN_WAIT_D,		/*  2 incoming, awaiting d-channel establishment */
-	ST_OUT_DIAL,		/*  3 outgoing, SETUP send; awaiting confirm */
-	ST_IN_WAIT_LL,		/*  4 incoming call received; wait for LL confirm */
-	ST_IN_ALERT_SEND,	/*  5 incoming call received; ALERT send */
-	ST_IN_WAIT_CONN_ACK,	/*  6 incoming CONNECT send; awaiting CONN_ACK */
-	ST_WAIT_BCONN,		/*  7 CONNECT/CONN_ACK received, awaiting b-channel prot. estbl. */
-	ST_ACTIVE,		/*  8 active, b channel prot. established */
-	ST_WAIT_BRELEASE,	/*  9 call clear. (initiator), awaiting b channel prot. rel. */
-	ST_WAIT_BREL_DISC,	/* 10 call clear. (receiver), DISCONNECT req. received */
-	ST_WAIT_DCOMMAND,	/* 11 call clear. (receiver), awaiting DCHANNEL message */
-	ST_WAIT_DRELEASE,	/* 12 DISCONNECT sent, awaiting RELEASE */
-	ST_WAIT_D_REL_CNF,	/* 13 RELEASE sent, awaiting RELEASE confirm */
-	ST_WAIT_DSHUTDOWN,	/*  14 awaiting d-channel shutdown */
+        ST_NULL,                /*  0 inactive */
+        ST_OUT_DIAL,            /*  1 outgoing, SETUP send; awaiting confirm */
+        ST_IN_WAIT_LL,          /*  2 incoming call received; wait for LL confirm */
+        ST_IN_ALERT_SENT,       /*  3 incoming call received; ALERT send */
+        ST_IN_WAIT_CONN_ACK,    /*  4 incoming CONNECT send; awaiting CONN_ACK */
+        ST_WAIT_BCONN,          /*  5 CONNECT/CONN_ACK received, awaiting b-channel prot. estbl. */
+        ST_ACTIVE,              /*  6 active, b channel prot. established */
+        ST_WAIT_BRELEASE,       /*  7 call clear. (initiator), awaiting b channel prot. rel. */
+        ST_WAIT_BREL_DISC,      /*  8 call clear. (receiver), DISCONNECT req. received */
+        ST_WAIT_DCOMMAND,       /*  9 call clear. (receiver), awaiting DCHANNEL message */
+        ST_WAIT_DRELEASE,       /* 10 DISCONNECT sent, awaiting RELEASE */
+        ST_WAIT_D_REL_CNF,      /* 11 RELEASE sent, awaiting RELEASE confirm */
+        ST_IN_PROCEED_SEND,     /* 12 incoming call, proceeding send */ 
 };
+  
 
-#define STATE_COUNT (ST_WAIT_DSHUTDOWN +1)
+#define STATE_COUNT (ST_IN_PROCEED_SEND + 1)
 
-static char *strState[] =
-{
-	"ST_NULL",
-	"ST_OUT_WAIT_D",
-	"ST_IN_WAIT_D",
-	"ST_OUT_DIAL",
-	"ST_IN_WAIT_LL",
-	"ST_IN_ALERT_SEND",
-	"ST_IN_WAIT_CONN_ACK",
-	"ST_WAIT_BCONN",
-	"ST_ACTIVE",
+  static char *strState[] =
+  {
+        "ST_NULL",
+        "ST_OUT_DIAL",
+        "ST_IN_WAIT_LL",
+        "ST_IN_ALERT_SENT",
+        "ST_IN_WAIT_CONN_ACK",
+        "ST_WAIT_BCONN",
+        "ST_ACTIVE",
 	"ST_WAIT_BRELEASE",
 	"ST_WAIT_BREL_DISC",
 	"ST_WAIT_DCOMMAND",
 	"ST_WAIT_DRELEASE",
 	"ST_WAIT_D_REL_CNF",
-	"ST_WAIT_DSHUTDOWN",
+        "ST_IN_PROCEED_SEND",
 };
 
 enum {
@@ -192,29 +249,28 @@
 	EV_SETUP_CNF,		/*  1 */
 	EV_ACCEPTB,		/*  2 */
 	EV_DISCONNECT_IND,	/*  3 */
-	EV_RELEASE_CNF,		/*  4 */
-	EV_DLEST,		/*  5 */
-	EV_DLRL,		/*  6 */
+	EV_RELEASE, 		/*  4 */
+	EV_LEASED,		/*  5 */
+	EV_LEASED_REL,		/*  6 */
 	EV_SETUP_IND,		/*  7 */
-	EV_RELEASE_IND,		/*  8 */
-	EV_ACCEPTD,		/*  9 */
-	EV_SETUP_CMPL_IND,	/* 10 */
-	EV_BC_EST,		/* 11 */
-	EV_WRITEBUF,		/* 12 */
-	EV_ESTABLISH,		/* 13 */
-	EV_HANGUP,		/* 14 */
-	EV_BC_REL,		/* 15 */
-	EV_CINF,		/* 16 */
-	EV_SUSPEND,		/* 17 */
-	EV_RESUME,		/* 18 */
-	EV_SHUTDOWN_D,		/* 19 */
-	EV_NOSETUP_RSP,		/* 20 */
-	EV_SETUP_ERR,		/* 21 */
-	EV_CONNECT_ERR,		/* 22 */
-	EV_RELEASE_ERR,		/* 23 */
+	EV_ACCEPTD,		/*  8 */
+	EV_SETUP_CMPL_IND,	/*  9 */
+	EV_BC_EST,		/* 10 */
+	EV_WRITEBUF,		/* 11 */
+	EV_HANGUP,		/* 12 */
+	EV_BC_REL,		/* 13 */
+	EV_CINF,		/* 14 */
+	EV_SUSPEND,		/* 15 */
+	EV_RESUME,		/* 16 */
+	EV_NOSETUP_RSP,		/* 17 */
+	EV_SETUP_ERR,		/* 18 */
+	EV_CONNECT_ERR,		/* 19 */
+	EV_PROCEED,		/* 20 */
+	EV_ALERT,		/* 21 */ 
+	EV_REDIR,		/* 22 */ 
 };
 
-#define EVENT_COUNT (EV_RELEASE_ERR +1)
+#define EVENT_COUNT (EV_REDIR + 1)
 
 static char *strEvent[] =
 {
@@ -222,83 +278,45 @@
 	"EV_SETUP_CNF",
 	"EV_ACCEPTB",
 	"EV_DISCONNECT_IND",
-	"EV_RELEASE_CNF",
-	"EV_DLEST",
-	"EV_DLRL",
+	"EV_RELEASE",
+	"EV_LEASED",
+	"EV_LEASED_REL",
 	"EV_SETUP_IND",
-	"EV_RELEASE_IND",
 	"EV_ACCEPTD",
 	"EV_SETUP_CMPL_IND",
 	"EV_BC_EST",
 	"EV_WRITEBUF",
-	"EV_ESTABLISH",
 	"EV_HANGUP",
 	"EV_BC_REL",
 	"EV_CINF",
 	"EV_SUSPEND",
 	"EV_RESUME",
-	"EV_SHUTDOWN_D",
 	"EV_NOSETUP_RSP",
 	"EV_SETUP_ERR",
 	"EV_CONNECT_ERR",
-	"EV_RELEASE_ERR",
-};
-
-enum {
-	ST_LC_NULL,
-	ST_LC_ACTIVATE_WAIT,
-	ST_LC_DELAY,
-	ST_LC_ESTABLISH_WAIT,
-	ST_LC_CONNECTED,
-	ST_LC_FLUSH_WAIT,
-	ST_LC_RELEASE_WAIT,
-};
-
-#define LC_STATE_COUNT (ST_LC_RELEASE_WAIT+1)
-
-static char *strLcState[] =
-{
-	"ST_LC_NULL",
-	"ST_LC_ACTIVATE_WAIT",
-	"ST_LC_DELAY",
-	"ST_LC_ESTABLISH_WAIT",
-	"ST_LC_CONNECTED",
-	"ST_LC_FLUSH_WAIT",
-	"ST_LC_RELEASE_WAIT",
-};
-
-enum {
-	EV_LC_ESTABLISH,
-	EV_LC_PH_ACTIVATE,
-	EV_LC_PH_DEACTIVATE,
-	EV_LC_DL_ESTABLISH,
-	EV_LC_TIMER,
-	EV_LC_DL_RELEASE,
-	EV_LC_RELEASE,
+	"EV_PROCEED",
+	"EV_ALERT",
+	"EV_REDIR",
 };
 
-#define LC_EVENT_COUNT (EV_LC_RELEASE+1)
 
-static char *strLcEvent[] =
+static inline void
+HL_LL(struct Channel *chanp, int command)
 {
-	"EV_LC_ESTABLISH",
-	"EV_LC_PH_ACTIVATE",
-	"EV_LC_PH_DEACTIVATE",
-	"EV_LC_DL_ESTABLISH",
-	"EV_LC_TIMER",
-	"EV_LC_DL_RELEASE",
-	"EV_LC_RELEASE",
-};
-
-#define LC_D  0
-#define LC_B  1
+        isdn_ctrl ic;
+ 
+        ic.driver = chanp->cs->myid;
+        ic.command = command;
+        ic.arg = chanp->chan;
+        chanp->cs->iif.statcallb(&ic);
+}
 
 static inline void
 lli_deliver_cause(struct Channel *chanp)
 {
-	isdn_ctrl ic;
-
-	if (chanp->proc->para.cause < 0)
+        isdn_ctrl ic;
+ 
+	if (chanp->proc->para.cause == NO_CAUSE)
 		return;
 	ic.driver = chanp->cs->myid;
 	ic.command = ISDN_STAT_CAUSE;
@@ -312,166 +330,121 @@
 	chanp->cs->iif.statcallb(&ic);
 }
 
-static void
-lli_d_established(struct FsmInst *fi, int event, void *arg)
+static inline void
+lli_close(struct FsmInst *fi)
 {
-	struct Channel *chanp = fi->userdata;
-
-	test_and_set_bit(FLG_ESTAB_D, &chanp->Flags);
-	if (chanp->leased) {
-		isdn_ctrl ic;
-		int ret;
-		char txt[32];
+        struct Channel *chanp = fi->userdata;
 
-		chanp->cs->cardmsg(chanp->cs, MDL_INFO_SETUP, (void *) chanp->chan);
-		FsmChangeState(fi, ST_IN_WAIT_LL);
-		test_and_set_bit(FLG_CALL_REC, &chanp->Flags);
-		if (chanp->debug & 1)
-			link_debug(chanp, "STAT_ICALL_LEASED", 0);
-		ic.driver = chanp->cs->myid;
-		ic.command = ISDN_STAT_ICALL;
-		ic.arg = chanp->chan;
-		ic.parm.setup.si1 = 7;
-		ic.parm.setup.si2 = 0;
-		ic.parm.setup.plan = 0;
-		ic.parm.setup.screen = 0;
-		sprintf(ic.parm.setup.eazmsn,"%d", chanp->chan + 1); 
-		sprintf(ic.parm.setup.phone,"LEASED%d", chanp->cs->myid);
-		ret = chanp->cs->iif.statcallb(&ic);
-		if (chanp->debug & 1) {
-			sprintf(txt, "statcallb ret=%d", ret);
-			link_debug(chanp, txt, 1);
-		}
-		if (!ret) {
-			chanp->cs->cardmsg(chanp->cs, MDL_INFO_REL, (void *) chanp->chan);
-			FsmChangeState(fi, ST_NULL);
-		}
-	} else if (fi->state == ST_WAIT_DSHUTDOWN)
-		FsmChangeState(fi, ST_NULL);
+        FsmChangeState(fi, ST_NULL);
+        chanp->Flags = 0;
+        chanp->cs->cardmsg(chanp->cs, MDL_INFO_REL, (void *) (long)chanp->chan);
+}
+
+ static void
+lli_leased_in(struct FsmInst *fi, int event, void *arg)
+{
+        struct Channel *chanp = fi->userdata;
+
+        isdn_ctrl ic;
+        int ret;
+ 
+        chanp->cs->cardmsg(chanp->cs, MDL_INFO_SETUP, (void *) (long)chanp->chan);
+        FsmChangeState(fi, ST_IN_WAIT_LL);
+        if (chanp->debug & 1)
+                link_debug(chanp, 0, "STAT_ICALL_LEASED");
+        ic.driver = chanp->cs->myid;
+	ic.command = ((chanp->chan < 2) ? ISDN_STAT_ICALL : ISDN_STAT_ICALLW);
+        ic.arg = chanp->chan;
+        ic.parm.setup.si1 = 7;
+        ic.parm.setup.si2 = 0;
+        ic.parm.setup.plan = 0;
+        ic.parm.setup.screen = 0;
+        sprintf(ic.parm.setup.eazmsn,"%d", chanp->chan + 1);
+        sprintf(ic.parm.setup.phone,"LEASED%d", chanp->cs->myid);
+        ret = chanp->cs->iif.statcallb(&ic);
+        if (chanp->debug & 1)
+                link_debug(chanp, 1, "statcallb ret=%d", ret);
+ 
+        if (!ret) {
+                chanp->cs->cardmsg(chanp->cs, MDL_INFO_REL, (void *) (long)chanp->chan);
+                FsmChangeState(fi, ST_NULL);
+        }
 }
 
-static void
-lli_d_released(struct FsmInst *fi, int event, void *arg)
-{
-	struct Channel *chanp = fi->userdata;
-
-	test_and_clear_bit(FLG_START_D, &chanp->Flags);
-}
 
 /*
  * Dial out
  */
 static void
+lli_init_bchan_out(struct FsmInst *fi, int event, void *arg)
+{
+        struct Channel *chanp = fi->userdata;
+ 
+        FsmChangeState(fi, ST_WAIT_BCONN);
+        if (chanp->debug & 1)
+                link_debug(chanp, 0, "STAT_DCONN");
+        HL_LL(chanp, ISDN_STAT_DCONN);
+        init_b_st(chanp, 0);
+        chanp->b_st->lli.l4l3(chanp->b_st, DL_ESTABLISH | REQUEST, NULL);
+}
+
+static void
 lli_prep_dialout(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 
-	FsmChangeState(fi, ST_OUT_WAIT_D);
 	FsmDelTimer(&chanp->drel_timer, 60);
 	FsmDelTimer(&chanp->dial_timer, 73);
 	chanp->l2_active_protocol = chanp->l2_protocol;
 	chanp->incoming = 0;
-	chanp->lc_b->l2_start = !0;
-	switch (chanp->l2_active_protocol) {
-		case (ISDN_PROTO_L2_X75I):
-			chanp->lc_b->l2_establish = !0;
-			break;
-		case (ISDN_PROTO_L2_HDLC):
-		case (ISDN_PROTO_L2_TRANS):
-			chanp->lc_b->l2_establish = 0;
-			break;
-		default:
-			printk(KERN_WARNING "lli_prep_dialout unknown protocol\n");
-			break;
-	}
-	if (test_bit(FLG_ESTAB_D, &chanp->Flags)) {
-		FsmEvent(fi, EV_DLEST, NULL);
-	} else {
-		chanp->Flags = 0;
-		test_and_set_bit(FLG_START_D, &chanp->Flags);
-		if (chanp->leased) {
-			chanp->lc_d->l2_establish = 0;
-		}
-		FsmEvent(&chanp->lc_d->lcfi, EV_LC_ESTABLISH, NULL);
-	}
-}
 
-static void
-lli_do_dialout(struct FsmInst *fi, int event, void *arg)
-{
-	struct Channel *chanp = fi->userdata;
-
-	FsmChangeState(fi, ST_OUT_DIAL);
-	chanp->cs->cardmsg(chanp->cs, MDL_INFO_SETUP, (void *) chanp->chan);
-	if (chanp->leased) {
-		FsmEvent(&chanp->fi, EV_SETUP_CNF, NULL);
-	} else {
-		test_and_set_bit(FLG_ESTAB_D, &chanp->Flags);
-		chanp->d_st->lli.l4l3(chanp->d_st, CC_SETUP_REQ, chanp);
-		test_and_set_bit(FLG_CALL_SEND, &chanp->Flags);
-	}
+        chanp->cs->cardmsg(chanp->cs, MDL_INFO_SETUP, (void *) (long)chanp->chan);
+        if (chanp->leased) {
+                lli_init_bchan_out(fi, event, arg);
+        } else {
+                FsmChangeState(fi, ST_OUT_DIAL);
+                chanp->d_st->lli.l4l3(chanp->d_st, CC_SETUP | REQUEST, chanp);
+        }
 }
 
 static void
-lli_init_bchan_out(struct FsmInst *fi, int event, void *arg)
+lli_resume(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
-	isdn_ctrl ic;
 
-	FsmChangeState(fi, ST_WAIT_BCONN);
-	test_and_set_bit(FLG_LL_DCONN, &chanp->Flags);
-	if (chanp->debug & 1)
-		link_debug(chanp, "STAT_DCONN", 0);
-	ic.driver = chanp->cs->myid;
-	ic.command = ISDN_STAT_DCONN;
-	ic.arg = chanp->chan;
-	chanp->cs->iif.statcallb(&ic);
-	init_b_st(chanp, 0);
-	test_and_set_bit(FLG_START_B, &chanp->Flags);
-	FsmEvent(&chanp->lc_b->lcfi, EV_LC_ESTABLISH, NULL);
+        FsmDelTimer(&chanp->drel_timer, 60);
+        FsmDelTimer(&chanp->dial_timer, 73);
+        chanp->l2_active_protocol = chanp->l2_protocol;
+        chanp->incoming = 0;
+ 
+        chanp->cs->cardmsg(chanp->cs, MDL_INFO_SETUP, (void *) (long)chanp->chan);
+        if (chanp->leased) {
+                lli_init_bchan_out(fi, event, arg);
+        } else {
+                FsmChangeState(fi, ST_OUT_DIAL);
+                chanp->d_st->lli.l4l3(chanp->d_st, CC_RESUME | REQUEST, chanp);
+        }
 }
 
 static void
 lli_go_active(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
-	isdn_ctrl ic;
 
 	FsmChangeState(fi, ST_ACTIVE);
 	chanp->data_open = !0;
-	test_and_set_bit(FLG_CONNECT_B, &chanp->Flags);
 	if (chanp->debug & 1)
-		link_debug(chanp, "STAT_BCONN", 0);
-	test_and_set_bit(FLG_LL_BCONN, &chanp->Flags);
-	ic.driver = chanp->cs->myid;
-	ic.command = ISDN_STAT_BCONN;
-	ic.arg = chanp->chan;
-	chanp->cs->iif.statcallb(&ic);
-	chanp->cs->cardmsg(chanp->cs, MDL_INFO_CONN, (void *) chanp->chan);
+		link_debug(chanp, 0, "STAT_BCONN");
+	HL_LL(chanp, ISDN_STAT_BCONN);
+	chanp->cs->cardmsg(chanp->cs, MDL_INFO_CONN, (void *) (long)chanp->chan);
 }
 
-/* incomming call */
 
-static void
-lli_start_dchan(struct FsmInst *fi, int event, void *arg)
-{
-	struct Channel *chanp = fi->userdata;
+/*
+ * RESUME
+ */
 
-	FsmChangeState(fi, ST_IN_WAIT_D);
-	FsmDelTimer(&chanp->drel_timer, 61);
-	if (event == EV_ACCEPTD)
-		test_and_set_bit(FLG_DO_CONNECT, &chanp->Flags);
-	else if (event == EV_HANGUP) {
-		test_and_set_bit(FLG_DO_HANGUP, &chanp->Flags);
-#ifdef ALERT_REJECT		
-		test_and_set_bit(FLG_DO_ALERT, &chanp->Flags);
-#endif
-	} 
-	if (test_bit(FLG_ESTAB_D, &chanp->Flags)) {
-		FsmEvent(fi, EV_DLEST, NULL);
-	} else if (!test_and_set_bit(FLG_START_D, &chanp->Flags))
-		FsmEvent(&chanp->lc_d->lcfi, EV_LC_ESTABLISH, NULL);
-}
+/* incomming call */
 
 static void
 lli_deliver_call(struct FsmInst *fi, int event, void *arg)
@@ -479,9 +452,8 @@
 	struct Channel *chanp = fi->userdata;
 	isdn_ctrl ic;
 	int ret;
-	char txt[32];
 
-	chanp->cs->cardmsg(chanp->cs, MDL_INFO_SETUP, (void *) chanp->chan);
+	chanp->cs->cardmsg(chanp->cs, MDL_INFO_SETUP, (void *) (long)chanp->chan);
 	/*
 	 * Report incoming calls only once to linklevel, use CallFlags
 	 * which is set to 3 with each broadcast message in isdnl1.c
@@ -489,11 +461,11 @@
 	 */
 	if (1) { /* for only one TEI */
 		FsmChangeState(fi, ST_IN_WAIT_LL);
-		test_and_set_bit(FLG_CALL_REC, &chanp->Flags);
 		if (chanp->debug & 1)
-			link_debug(chanp, "STAT_ICALL", 0);
+			link_debug(chanp, 0, (chanp->chan < 2) ? "STAT_ICALL" : "STAT_ICALLW");
 		ic.driver = chanp->cs->myid;
-		ic.command = ISDN_STAT_ICALL;
+		ic.command = ((chanp->chan < 2) ? ISDN_STAT_ICALL : ISDN_STAT_ICALLW);
+
 		ic.arg = chanp->chan;
 		/*
 		 * No need to return "unknown" for calls without OAD,
@@ -501,962 +473,496 @@
 		 */
 		ic.parm.setup = chanp->proc->para.setup;
 		ret = chanp->cs->iif.statcallb(&ic);
-		if (chanp->debug & 1) {
-			sprintf(txt, "statcallb ret=%d", ret);
-			link_debug(chanp, txt, 1);
-		}
+		if (chanp->debug & 1)
+			link_debug(chanp, 1, "statcallb ret=%d", ret);
+
 		switch (ret) {
-			case 1:	/* OK, anybody likes this call */
+			case 1:	/* OK, someone likes this call */
 				FsmDelTimer(&chanp->drel_timer, 61);
-				if (test_bit(FLG_ESTAB_D, &chanp->Flags)) {
-					FsmChangeState(fi, ST_IN_ALERT_SEND);
-					test_and_set_bit(FLG_CALL_ALERT, &chanp->Flags);
-					chanp->d_st->lli.l4l3(chanp->d_st, CC_ALERTING_REQ, chanp->proc);
-				} else {
-					test_and_set_bit(FLG_DO_ALERT, &chanp->Flags);
-					FsmChangeState(fi, ST_IN_WAIT_D);
-					test_and_set_bit(FLG_START_D, &chanp->Flags);
-					FsmEvent(&chanp->lc_d->lcfi, EV_LC_ESTABLISH, NULL);
-				}
+				FsmChangeState(fi, ST_IN_ALERT_SENT);
+				chanp->d_st->lli.l4l3(chanp->d_st, CC_ALERTING | REQUEST, chanp->proc);
+				break;
+	      	        case 4: /* direct redirect */
+		        case 3: /* Proceeding desired */
+				FsmDelTimer(&chanp->drel_timer, 61);
+				FsmChangeState(fi, ST_IN_PROCEED_SEND);
+                                chanp->d_st->lli.l4l3(chanp->d_st, CC_PROCEED_SEND | REQUEST, chanp->proc);
+                                if (ret == 4)
+				 { chanp->setup = ic.parm.setup;
+				   chanp->d_st->lli.l4l3(chanp->d_st, CC_REDIR | REQUEST, chanp->proc);
+                                 }   
 				break;
 			case 2:	/* Rejecting Call */
-				test_and_clear_bit(FLG_CALL_REC, &chanp->Flags);
 				break;
 			case 0:	/* OK, nobody likes this call */
 			default:	/* statcallb problems */
-				chanp->d_st->lli.l4l3(chanp->d_st, CC_IGNORE, chanp->proc);
-				chanp->cs->cardmsg(chanp->cs, MDL_INFO_REL, (void *) chanp->chan);
+				chanp->d_st->lli.l4l3(chanp->d_st, CC_IGNORE | REQUEST, chanp->proc);
+				chanp->cs->cardmsg(chanp->cs, MDL_INFO_REL, (void *) (long)chanp->chan);
 				FsmChangeState(fi, ST_NULL);
-#ifndef LAYER2_WATCHING
-				if (test_bit(FLG_ESTAB_D, &chanp->Flags))
-					FsmRestartTimer(&chanp->drel_timer, DREL_TIMER_VALUE, EV_SHUTDOWN_D, NULL, 61);
-#endif
 				break;
 		}
 	} else {
-		chanp->d_st->lli.l4l3(chanp->d_st, CC_IGNORE, chanp->proc);
-		chanp->cs->cardmsg(chanp->cs, MDL_INFO_REL, (void *) chanp->chan);
-		FsmChangeState(fi, ST_NULL);
-#ifndef LAYER2_WATCHING
-		if (test_bit(FLG_ESTAB_D, &chanp->Flags))
-			FsmRestartTimer(&chanp->drel_timer, DREL_TIMER_VALUE, EV_SHUTDOWN_D, NULL, 62);
-#endif
+		chanp->d_st->lli.l4l3(chanp->d_st, CC_IGNORE | REQUEST, chanp->proc);
+		chanp->cs->cardmsg(chanp->cs, MDL_INFO_REL, (void *) (long)chanp->chan);
 	}
 }
 
 static void
-lli_establish_d(struct FsmInst *fi, int event, void *arg)
+lli_send_dconnect(struct FsmInst *fi, int event, void *arg)
 {
-	/* This establish the D-channel for pending L3 messages 
-	 * without blocking th channel
-	 */
 	struct Channel *chanp = fi->userdata;
 
-	test_and_set_bit(FLG_DO_ESTAB, &chanp->Flags);
-	FsmChangeState(fi, ST_IN_WAIT_D);
-	test_and_set_bit(FLG_START_D, &chanp->Flags);
-	FsmEvent(&chanp->lc_d->lcfi, EV_LC_ESTABLISH, NULL);
+	FsmChangeState(fi, ST_IN_WAIT_CONN_ACK);
+	chanp->d_st->lli.l4l3(chanp->d_st, CC_SETUP | RESPONSE, chanp->proc);
 }
 
 static void
-lli_do_action(struct FsmInst *fi, int event, void *arg)
+lli_send_alert(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 
-	test_and_set_bit(FLG_ESTAB_D, &chanp->Flags);
-	if (chanp->leased) {
-		FsmChangeState(fi, ST_IN_WAIT_CONN_ACK);
-		test_and_clear_bit(FLG_DO_ALERT, &chanp->Flags);
-		test_and_clear_bit(FLG_DO_CONNECT, &chanp->Flags);
-		FsmEvent(&chanp->fi, EV_SETUP_CMPL_IND, NULL);
-	} else if (test_and_clear_bit(FLG_DO_CONNECT, &chanp->Flags) &&
-		!test_bit(FLG_DO_HANGUP, &chanp->Flags)) {
-		FsmChangeState(fi, ST_IN_WAIT_CONN_ACK);
-		test_and_clear_bit(FLG_DO_ALERT, &chanp->Flags);
-		chanp->d_st->lli.l4l3(chanp->d_st, CC_SETUP_RSP, chanp->proc);
-	} else if (test_and_clear_bit(FLG_DO_ALERT, &chanp->Flags)) {
-		if (test_bit(FLG_DO_HANGUP, &chanp->Flags))
-			FsmRestartTimer(&chanp->drel_timer, 40, EV_HANGUP, NULL, 63);
-		FsmChangeState(fi, ST_IN_ALERT_SEND);
-		test_and_set_bit(FLG_CALL_ALERT, &chanp->Flags);
-		chanp->d_st->lli.l4l3(chanp->d_st, CC_ALERTING_REQ, chanp->proc);
-	} else if (test_and_clear_bit(FLG_DO_HANGUP, &chanp->Flags)) {
-		FsmChangeState(fi, ST_WAIT_DRELEASE);
-		chanp->proc->para.cause = 0x15;		/* Call Rejected */
-		chanp->d_st->lli.l4l3(chanp->d_st, CC_REJECT_REQ, chanp->proc);
-		test_and_set_bit(FLG_DISC_SEND, &chanp->Flags);
-	} else if (test_and_clear_bit(FLG_DO_ESTAB, &chanp->Flags)) {
-		FsmChangeState(fi, ST_NULL);
-		chanp->Flags = 0;
-		test_and_set_bit(FLG_ESTAB_D, &chanp->Flags);
-		chanp->d_st->lli.l4l3(chanp->d_st, CC_ESTABLISH, chanp->proc);
-		chanp->proc = NULL;
-#ifndef LAYER2_WATCHING
-		FsmAddTimer(&chanp->drel_timer, DREL_TIMER_VALUE, EV_SHUTDOWN_D, NULL, 60);
-#endif
-	}
+	FsmChangeState(fi, ST_IN_ALERT_SENT);
+	chanp->d_st->lli.l4l3(chanp->d_st, CC_ALERTING | REQUEST, chanp->proc);
 }
 
 static void
-lli_send_dconnect(struct FsmInst *fi, int event, void *arg)
+lli_send_redir(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 
-	FsmChangeState(fi, ST_IN_WAIT_CONN_ACK);
-	chanp->d_st->lli.l4l3(chanp->d_st, CC_SETUP_RSP, chanp->proc);
+	chanp->d_st->lli.l4l3(chanp->d_st, CC_REDIR | REQUEST, chanp->proc);
 }
 
 static void
 lli_init_bchan_in(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
-	isdn_ctrl ic;
 
 	FsmChangeState(fi, ST_WAIT_BCONN);
-	test_and_set_bit(FLG_LL_DCONN, &chanp->Flags);
 	if (chanp->debug & 1)
-		link_debug(chanp, "STAT_DCONN", 0);
-	ic.driver = chanp->cs->myid;
-	ic.command = ISDN_STAT_DCONN;
-	ic.arg = chanp->chan;
-	chanp->cs->iif.statcallb(&ic);
+		link_debug(chanp, 0, "STAT_DCONN");
+	HL_LL(chanp, ISDN_STAT_DCONN);
 	chanp->l2_active_protocol = chanp->l2_protocol;
 	chanp->incoming = !0;
-	chanp->lc_b->l2_start = 0;
-	switch (chanp->l2_active_protocol) {
-		case (ISDN_PROTO_L2_X75I):
-			chanp->lc_b->l2_establish = !0;
-			break;
-		case (ISDN_PROTO_L2_HDLC):
-		case (ISDN_PROTO_L2_TRANS):
-			chanp->lc_b->l2_establish = 0;
-			break;
-		default:
-			printk(KERN_WARNING "bchannel unknown protocol\n");
-			break;
-	}
 	init_b_st(chanp, !0);
-	test_and_set_bit(FLG_START_B, &chanp->Flags);
-	FsmEvent(&chanp->lc_b->lcfi, EV_LC_ESTABLISH, NULL);
-}
-
-/* Call clearing */
-
-static void
-lli_cancel_call(struct FsmInst *fi, int event, void *arg)
-{
-	struct Channel *chanp = fi->userdata;
-	isdn_ctrl ic;
-
-	FsmChangeState(fi, ST_WAIT_DRELEASE);
-	if (test_and_clear_bit(FLG_LL_BCONN, &chanp->Flags)) {
-		if (chanp->debug & 1)
-			link_debug(chanp, "STAT_BHUP", 0);
-		ic.driver = chanp->cs->myid;
-		ic.command = ISDN_STAT_BHUP;
-		ic.arg = chanp->chan;
-		chanp->cs->iif.statcallb(&ic);
-	}
-	if (test_and_clear_bit(FLG_START_B, &chanp->Flags))
-		release_b_st(chanp);
-	chanp->proc->para.cause = 0x10;		/* Normal Call Clearing */
-	chanp->d_st->lli.l4l3(chanp->d_st, CC_DISCONNECT_REQ, chanp->proc);
-	test_and_set_bit(FLG_DISC_SEND, &chanp->Flags);
+	chanp->b_st->lli.l4l3(chanp->b_st, DL_ESTABLISH | REQUEST, NULL);
 }
 
 static void
-lli_shutdown_d(struct FsmInst *fi, int event, void *arg)
+lli_setup_rsp(struct FsmInst *fi, int event, void *arg)
 {
-	struct Channel *chanp = fi->userdata;
-
-	FsmDelTimer(&chanp->drel_timer, 62);
-#ifdef LAYER2_WATCHING
-	FsmChangeState(fi, ST_NULL);
-#else
-	if (!test_bit(FLG_TWO_DCHAN, &chanp->cs->HW_Flags)) {
-		if (chanp->chan) {
-			if (chanp->cs->channel[0].fi.state != ST_NULL)
-				return;
-		} else {
-			if (chanp->cs->channel[1].fi.state != ST_NULL)
-				return;
-		}
-	}
-	FsmChangeState(fi, ST_WAIT_DSHUTDOWN);
-	test_and_clear_bit(FLG_ESTAB_D, &chanp->Flags);
-	FsmEvent(&chanp->lc_d->lcfi, EV_LC_RELEASE, NULL);
+        struct Channel *chanp = fi->userdata;
+ 
+        if (chanp->leased) {
+                lli_init_bchan_in(fi, event, arg);
+        } else {
+                FsmChangeState(fi, ST_IN_WAIT_CONN_ACK);
+#ifdef WANT_ALERT
+                chanp->d_st->lli.l4l3(chanp->d_st, CC_ALERTING | REQUEST, chanp->proc);
 #endif
+               chanp->d_st->lli.l4l3(chanp->d_st, CC_SETUP | RESPONSE, chanp->proc);
+       }
 }
 
-static void
-lli_timeout_d(struct FsmInst *fi, int event, void *arg)
-{
-	struct Channel *chanp = fi->userdata;
-	isdn_ctrl ic;
-
-	if (test_and_clear_bit(FLG_LL_DCONN, &chanp->Flags)) {
-		if (chanp->debug & 1)
-			link_debug(chanp, "STAT_DHUP", 0);
-		lli_deliver_cause(chanp);
-		ic.driver = chanp->cs->myid;
-		ic.command = ISDN_STAT_DHUP;
-		ic.arg = chanp->chan;
-		chanp->cs->iif.statcallb(&ic);
-	}
-	FsmChangeState(fi, ST_NULL);
-	chanp->Flags = 0;
-	test_and_set_bit(FLG_ESTAB_D, &chanp->Flags);
-#ifndef LAYER2_WATCHING
-	FsmAddTimer(&chanp->drel_timer, DREL_TIMER_VALUE, EV_SHUTDOWN_D, NULL, 60);
-#endif
-	chanp->cs->cardmsg(chanp->cs, MDL_INFO_REL, (void *) chanp->chan);
-}
+/* Call suspend */
 
 static void
-lli_go_null(struct FsmInst *fi, int event, void *arg)
+lli_suspend(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 
-	FsmChangeState(fi, ST_NULL);
-	chanp->Flags = 0;
-	FsmDelTimer(&chanp->drel_timer, 63);
-	chanp->cs->cardmsg(chanp->cs, MDL_INFO_REL, (void *) chanp->chan);
+	chanp->d_st->lli.l4l3(chanp->d_st, CC_SUSPEND | REQUEST, chanp->proc);
 }
 
-static void
-lli_disconn_bchan(struct FsmInst *fi, int event, void *arg)
-{
-	struct Channel *chanp = fi->userdata;
-
-	chanp->data_open = 0;
-	FsmChangeState(fi, ST_WAIT_BRELEASE);
-	test_and_clear_bit(FLG_CONNECT_B, &chanp->Flags);
-	FsmEvent(&chanp->lc_b->lcfi, EV_LC_RELEASE, NULL);
-}
+/* Call clearing */
 
 static void
-lli_send_d_disc(struct FsmInst *fi, int event, void *arg)
+lli_disconnect_req(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
-	isdn_ctrl ic;
 
-	if (test_bit(FLG_DISC_REC, &chanp->Flags) ||
-		test_bit(FLG_REL_REC, &chanp->Flags))
-		return;
 	FsmChangeState(fi, ST_WAIT_DRELEASE);
-	if (test_and_clear_bit(FLG_LL_BCONN, &chanp->Flags)) {
-		if (chanp->debug & 1)
-			link_debug(chanp, "STAT_BHUP", 0);
-		ic.driver = chanp->cs->myid;
-		ic.command = ISDN_STAT_BHUP;
-		ic.arg = chanp->chan;
-		chanp->cs->iif.statcallb(&ic);
-	}
-	if (test_and_clear_bit(FLG_START_B, &chanp->Flags))
-		release_b_st(chanp);
-	if (chanp->leased) {
-		ic.command = ISDN_STAT_CAUSE;
-		ic.arg = chanp->chan;
-		sprintf(ic.parm.num, "L0010");
-		chanp->cs->iif.statcallb(&ic);
-		if (chanp->debug & 1)
-			link_debug(chanp, "STAT_DHUP", 0);
-		ic.driver = chanp->cs->myid;
-		ic.command = ISDN_STAT_DHUP;
-		ic.arg = chanp->chan;
-		chanp->cs->iif.statcallb(&ic);
-		FsmChangeState(fi, ST_WAIT_DSHUTDOWN);
-		test_and_clear_bit(FLG_ESTAB_D, &chanp->Flags);
-		FsmEvent(&chanp->lc_d->lcfi, EV_LC_RELEASE, NULL);
-	} else {
-		if (test_and_clear_bit(FLG_DO_HANGUP, &chanp->Flags))
-			chanp->proc->para.cause = 0x15;		/* Call Reject */
-		else
-			chanp->proc->para.cause = 0x10;		/* Normal Call Clearing */
-		chanp->d_st->lli.l4l3(chanp->d_st, CC_DISCONNECT_REQ, chanp->proc);
-		test_and_set_bit(FLG_DISC_SEND, &chanp->Flags);
-	}
+	chanp->proc->para.cause = 0x10;	/* Normal Call Clearing */
+	chanp->d_st->lli.l4l3(chanp->d_st, CC_DISCONNECT | REQUEST, chanp->proc);
 }
 
 static void
-lli_released_bchan(struct FsmInst *fi, int event, void *arg)
+lli_disconnect_reject(struct FsmInst *fi, int event, void *arg)
 {
-	struct Channel *chanp = fi->userdata;
-	isdn_ctrl ic;
+        struct Channel *chanp = fi->userdata;
 
-	FsmChangeState(fi, ST_WAIT_DCOMMAND);
-	chanp->data_open = 0;
-	if (test_and_clear_bit(FLG_LL_BCONN, &chanp->Flags)) {
-		if (chanp->debug & 1)
-			link_debug(chanp, "STAT_BHUP", 0);
-		ic.driver = chanp->cs->myid;
-		ic.command = ISDN_STAT_BHUP;
-		ic.arg = chanp->chan;
-		chanp->cs->iif.statcallb(&ic);
-	}
-	release_b_st(chanp);
-	test_and_clear_bit(FLG_START_B, &chanp->Flags);
+        FsmChangeState(fi, ST_WAIT_DRELEASE);
+        chanp->proc->para.cause = 0x15;         /* Call Rejected */
+        chanp->d_st->lli.l4l3(chanp->d_st, CC_DISCONNECT | REQUEST, chanp->proc);
 }
 
-
 static void
-lli_release_bchan(struct FsmInst *fi, int event, void *arg)
+lli_dhup_close(struct FsmInst *fi, int event, void *arg)
 {
-	struct Channel *chanp = fi->userdata;
+        struct Channel *chanp = fi->userdata;
 
-	chanp->data_open = 0;
-	test_and_set_bit(FLG_DISC_REC, &chanp->Flags);
-	FsmChangeState(fi, ST_WAIT_BREL_DISC);
-	test_and_clear_bit(FLG_CONNECT_B, &chanp->Flags);
-	FsmEvent(&chanp->lc_b->lcfi, EV_LC_RELEASE, NULL);
+        if (chanp->debug & 1)
+                link_debug(chanp, 0, "STAT_DHUP");
+        lli_deliver_cause(chanp);
+        HL_LL(chanp, ISDN_STAT_DHUP);
+ 
+        lli_close(fi);
 }
 
 static void
-lli_received_d_rel(struct FsmInst *fi, int event, void *arg)
+lli_reject_req(struct FsmInst *fi, int event, void *arg)
 {
-	struct Channel *chanp = fi->userdata;
-	isdn_ctrl ic;
+        struct Channel *chanp = fi->userdata;
 
-	chanp->data_open = 0;
-	FsmChangeState(fi, ST_NULL);
-	test_and_set_bit(FLG_REL_REC, &chanp->Flags);
-	if (test_and_clear_bit(FLG_CONNECT_B, &chanp->Flags)) {
-		chanp->lc_b->l2_establish = 0;	/* direct reset in lc_b->lcfi */
-		FsmEvent(&chanp->lc_b->lcfi, EV_LC_RELEASE, NULL);
-	}
-	if (test_and_clear_bit(FLG_LL_BCONN, &chanp->Flags)) {
-		if (chanp->debug & 1)
-			link_debug(chanp, "STAT_BHUP", 0);
-		ic.driver = chanp->cs->myid;
-		ic.command = ISDN_STAT_BHUP;
-		ic.arg = chanp->chan;
-		chanp->cs->iif.statcallb(&ic);
-	}
-	if (test_and_clear_bit(FLG_START_B, &chanp->Flags))
-		release_b_st(chanp);
-	if (test_bit(FLG_LL_DCONN, &chanp->Flags) ||
-		test_bit(FLG_CALL_SEND, &chanp->Flags) ||
-		test_bit(FLG_CALL_ALERT, &chanp->Flags)) {
-		if (chanp->debug & 1)
-			link_debug(chanp, "STAT_DHUP", 0);
-		lli_deliver_cause(chanp);
-		ic.driver = chanp->cs->myid;
-		ic.command = ISDN_STAT_DHUP;
-		ic.arg = chanp->chan;
-		chanp->cs->iif.statcallb(&ic);
-	}
-	test_and_clear_bit(FLG_DISC_SEND, &chanp->Flags);
-	test_and_clear_bit(FLG_CALL_REC, &chanp->Flags);
-	test_and_clear_bit(FLG_CALL_ALERT, &chanp->Flags);
-	test_and_clear_bit(FLG_LL_DCONN, &chanp->Flags);
-	test_and_clear_bit(FLG_CALL_SEND, &chanp->Flags);
-	lli_timeout_d(fi, event, arg);
+#ifndef ALERT_REJECT
+        chanp->proc->para.cause = 0x15;         /* Call Rejected */
+        chanp->d_st->lli.l4l3(chanp->d_st, CC_REJECT | REQUEST, chanp->proc);
+        lli_dhup_close(fi, event, arg);
+#else
+        FsmRestartTimer(&chanp->drel_timer, 40, EV_HANGUP, NULL, 63);
+        FsmChangeState(fi, ST_IN_ALERT_SENT);
+        chanp->d_st->lli.l4l3(chanp->d_st, CC_ALERTING | REQUEST, chanp->proc);
+#endif
 }
 
 static void
-lli_received_d_relcnf(struct FsmInst *fi, int event, void *arg)
+lli_disconn_bchan(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
-	isdn_ctrl ic;
 
 	chanp->data_open = 0;
-	FsmChangeState(fi, ST_NULL);
-	if (test_and_clear_bit(FLG_CONNECT_B, &chanp->Flags)) {
-		chanp->lc_b->l2_establish = 0;	/* direct reset in lc_b->lcfi */
-		FsmEvent(&chanp->lc_b->lcfi, EV_LC_RELEASE, NULL);
-	}
-	if (test_and_clear_bit(FLG_LL_BCONN, &chanp->Flags)) {
-		if (chanp->debug & 1)
-			link_debug(chanp, "STAT_BHUP", 0);
-		ic.driver = chanp->cs->myid;
-		ic.command = ISDN_STAT_BHUP;
-		ic.arg = chanp->chan;
-		chanp->cs->iif.statcallb(&ic);
-	}
-	if (test_and_clear_bit(FLG_START_B, &chanp->Flags))
-		release_b_st(chanp);
-	if (test_bit(FLG_LL_DCONN, &chanp->Flags) ||
-		test_bit(FLG_CALL_SEND, &chanp->Flags) ||
-		test_bit(FLG_CALL_ALERT, &chanp->Flags)) {
-		if (chanp->debug & 1)
-			link_debug(chanp, "STAT_DHUP", 0);
-		lli_deliver_cause(chanp);
-		ic.driver = chanp->cs->myid;
-		ic.command = ISDN_STAT_DHUP;
-		ic.arg = chanp->chan;
-		chanp->cs->iif.statcallb(&ic);
-	}
-	test_and_clear_bit(FLG_DISC_SEND, &chanp->Flags);
-	test_and_clear_bit(FLG_CALL_REC, &chanp->Flags);
-	test_and_clear_bit(FLG_CALL_ALERT, &chanp->Flags);
-	test_and_clear_bit(FLG_LL_DCONN, &chanp->Flags);
-	test_and_clear_bit(FLG_CALL_SEND, &chanp->Flags);
-	lli_timeout_d(fi, event, arg);
+	FsmChangeState(fi, ST_WAIT_BRELEASE);
+	chanp->b_st->lli.l4l3(chanp->b_st, DL_RELEASE | REQUEST, NULL);
 }
 
 static void
-lli_received_d_disc(struct FsmInst *fi, int event, void *arg)
+lli_start_disc(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 	isdn_ctrl ic;
 
-	chanp->data_open = 0;
-	FsmChangeState(fi, ST_WAIT_D_REL_CNF);
-	test_and_set_bit(FLG_DISC_REC, &chanp->Flags);
-	if (test_and_clear_bit(FLG_LL_BCONN, &chanp->Flags)) {
-		if (chanp->debug & 1)
-			link_debug(chanp, "STAT_BHUP", 0);
-		ic.driver = chanp->cs->myid;
-		ic.command = ISDN_STAT_BHUP;
+	if (chanp->leased) {
+		ic.command = ISDN_STAT_CAUSE;
 		ic.arg = chanp->chan;
+		sprintf(ic.parm.num, "L0010");
 		chanp->cs->iif.statcallb(&ic);
-	}
-	if (test_and_clear_bit(FLG_START_B, &chanp->Flags))
-		release_b_st(chanp);
-	if (test_bit(FLG_LL_DCONN, &chanp->Flags) ||
-		test_bit(FLG_CALL_SEND, &chanp->Flags) ||
-		test_bit(FLG_CALL_ALERT, &chanp->Flags)) {
 		if (chanp->debug & 1)
-			link_debug(chanp, "STAT_DHUP", 0);
-		lli_deliver_cause(chanp);
-		ic.driver = chanp->cs->myid;
-		ic.command = ISDN_STAT_DHUP;
-		ic.arg = chanp->chan;
-		chanp->cs->iif.statcallb(&ic);
+			link_debug(chanp, 0, "STAT_DHUP");
+		HL_LL(chanp, ISDN_STAT_DHUP);
+		lli_close(fi);
+	} else {
+	        lli_disconnect_req(fi, event, arg);
 	}
-	test_and_clear_bit(FLG_CALL_ALERT, &chanp->Flags);
-	test_and_clear_bit(FLG_LL_DCONN, &chanp->Flags);
-	test_and_clear_bit(FLG_CALL_SEND, &chanp->Flags);
-	chanp->d_st->lli.l4l3(chanp->d_st, CC_RELEASE_REQ, chanp->proc);
 }
 
-/* processing charge info */
 static void
-lli_charge_info(struct FsmInst *fi, int event, void *arg)
+lli_rel_b_disc(struct FsmInst *fi, int event, void *arg)
 {
-	struct Channel *chanp = fi->userdata;
-	isdn_ctrl ic;
-
-	ic.driver = chanp->cs->myid;
-	ic.command = ISDN_STAT_CINF;
-	ic.arg = chanp->chan;
-	sprintf(ic.parm.num, "%d", chanp->proc->para.chargeinfo);
-	chanp->cs->iif.statcallb(&ic);
-}
-
-/* error procedures */
-
-static void
-lli_no_dchan(struct FsmInst *fi, int event, void *arg)
-{
-	struct Channel *chanp = fi->userdata;
-	isdn_ctrl ic;
-
-	if (chanp->debug & 1)
-		link_debug(chanp, "STAT_NODCH", 0);
-	ic.driver = chanp->cs->myid;
-	ic.command = ISDN_STAT_NODCH;
-	ic.arg = chanp->chan;
-	chanp->cs->iif.statcallb(&ic);
-	chanp->Flags = 0;
-	FsmChangeState(fi, ST_NULL);
-	FsmEvent(&chanp->lc_d->lcfi, EV_LC_RELEASE, NULL);
-}
-
-static void
-lli_no_dchan_ready(struct FsmInst *fi, int event, void *arg)
-{
-	struct Channel *chanp = fi->userdata;
-	isdn_ctrl ic;
-
-	if (chanp->debug & 1)
-		link_debug(chanp, "STAT_DHUP", 0);
-	ic.driver = chanp->cs->myid;
-	ic.command = ISDN_STAT_DHUP;
-	ic.arg = chanp->chan;
-	chanp->cs->iif.statcallb(&ic);
-}
-
-static void
-lli_no_dchan_in(struct FsmInst *fi, int event, void *arg)
-{
-	struct Channel *chanp = fi->userdata;
-	isdn_ctrl ic;
-
-	if (chanp->debug & 1)
-		link_debug(chanp, "STAT_DHUP", 0);
-	ic.driver = chanp->cs->myid;
-	ic.command = ISDN_STAT_DHUP;
-	ic.arg = chanp->chan;
-	chanp->cs->iif.statcallb(&ic);
-	chanp->d_st->lli.l4l3(chanp->d_st, CC_DLRL, chanp->proc);
-	chanp->Flags = 0;
-	FsmChangeState(fi, ST_NULL);
-	FsmEvent(&chanp->lc_d->lcfi, EV_LC_RELEASE, NULL);
-}
-
-static void
-lli_no_setup_rsp(struct FsmInst *fi, int event, void *arg)
-{
-	struct Channel *chanp = fi->userdata;
-	isdn_ctrl ic;
-
-	FsmChangeState(fi, ST_NULL);
-	test_and_clear_bit(FLG_CALL_SEND, &chanp->Flags);
-	if (chanp->debug & 1)
-		link_debug(chanp, "STAT_DHUP", 0);
-	ic.driver = chanp->cs->myid;
-	ic.command = ISDN_STAT_DHUP;
-	ic.arg = chanp->chan;
-	chanp->cs->iif.statcallb(&ic);
-	lli_shutdown_d(fi, event, arg);
+        struct Channel *chanp = fi->userdata;
+ 
+        release_b_st(chanp);
+        lli_start_disc(fi, event, arg);
 }
 
 static void
-lli_setup_err(struct FsmInst *fi, int event, void *arg)
+lli_bhup_disc(struct FsmInst *fi, int event, void *arg)
 {
-	struct Channel *chanp = fi->userdata;
-	isdn_ctrl ic;
-
-	FsmChangeState(fi, ST_WAIT_DRELEASE);
-	if (test_and_clear_bit(FLG_LL_DCONN, &chanp->Flags)) {
-		if (chanp->debug & 1)
-			link_debug(chanp, "STAT_DHUP", 0);
-		lli_deliver_cause(chanp);
-		ic.driver = chanp->cs->myid;
-		ic.command = ISDN_STAT_DHUP;
-		ic.arg = chanp->chan;
-		chanp->cs->iif.statcallb(&ic);
-	}
-	test_and_set_bit(FLG_DISC_SEND, &chanp->Flags);	/* DISCONN was sent from L3 */
+        struct Channel *chanp = fi->userdata;
+  
+        if (chanp->debug & 1)
+                link_debug(chanp, 0, "STAT_BHUP");
+        HL_LL(chanp, ISDN_STAT_BHUP);
+ 
+        lli_rel_b_disc(fi, event, arg);
 }
 
 static void
-lli_connect_err(struct FsmInst *fi, int event, void *arg)
+lli_bhup_rel_b(struct FsmInst *fi, int event, void *arg)
 {
-	struct Channel *chanp = fi->userdata;
-	isdn_ctrl ic;
+        struct Channel *chanp = fi->userdata;
 
-	FsmChangeState(fi, ST_WAIT_DRELEASE);
-	if (test_and_clear_bit(FLG_LL_DCONN, &chanp->Flags)) {
-		if (chanp->debug & 1)
-			link_debug(chanp, "STAT_DHUP", 0);
-		lli_deliver_cause(chanp);
-		ic.driver = chanp->cs->myid;
-		ic.command = ISDN_STAT_DHUP;
-		ic.arg = chanp->chan;
-		chanp->cs->iif.statcallb(&ic);
-	}
-	test_and_set_bit(FLG_DISC_SEND, &chanp->Flags);	/* DISCONN was sent from L3 */
+        FsmChangeState(fi, ST_WAIT_DCOMMAND);
+        chanp->data_open = 0;
+                if (chanp->debug & 1)
+                link_debug(chanp, 0, "STAT_BHUP");
+        HL_LL(chanp, ISDN_STAT_BHUP);
+        release_b_st(chanp);
 }
 
 static void
-lli_got_dlrl(struct FsmInst *fi, int event, void *arg)
+lli_release_bchan(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
-	isdn_ctrl ic;
 
 	chanp->data_open = 0;
-	FsmChangeState(fi, ST_NULL);
-	if (test_and_clear_bit(FLG_CONNECT_B, &chanp->Flags)) {
-		chanp->lc_b->l2_establish = 0;	/* direct reset in lc_b->lcfi */
-		FsmEvent(&chanp->lc_b->lcfi, EV_LC_RELEASE, NULL);
-	}
-	if (test_and_clear_bit(FLG_LL_BCONN, &chanp->Flags)) {
-		if (chanp->debug & 1)
-			link_debug(chanp, "STAT_BHUP", 0);
-		ic.driver = chanp->cs->myid;
-		ic.command = ISDN_STAT_BHUP;
-		ic.arg = chanp->chan;
-		chanp->cs->iif.statcallb(&ic);
-	}
-	if (test_and_clear_bit(FLG_START_B, &chanp->Flags))
-		release_b_st(chanp);
-	if (chanp->leased) {
-		ic.driver = chanp->cs->myid;
-		ic.command = ISDN_STAT_CAUSE;
-		ic.arg = chanp->chan;
-		sprintf(ic.parm.num, "L%02X%02X", 0, 0x2f);
-		chanp->cs->iif.statcallb(&ic);
-		ic.driver = chanp->cs->myid;
-		ic.command = ISDN_STAT_DHUP;
-		ic.arg = chanp->chan;
-		chanp->cs->iif.statcallb(&ic);
-		chanp->Flags = 0;
-	} else {
-		if (test_and_clear_bit(FLG_LL_DCONN, &chanp->Flags)) {
-			if (chanp->debug & 1)
-				link_debug(chanp, "STAT_DHUP", 0);
-			if (chanp->cs->protocol == ISDN_PTYPE_EURO) {
-				chanp->proc->para.cause = 0x2f;
-				chanp->proc->para.loc = 0;
-			} else {
-				chanp->proc->para.cause = 0x70;
-				chanp->proc->para.loc = 0;
-			}
-			lli_deliver_cause(chanp);
-			ic.driver = chanp->cs->myid;
-			ic.command = ISDN_STAT_DHUP;
-			ic.arg = chanp->chan;
-			chanp->cs->iif.statcallb(&ic);
-		}
-		chanp->d_st->lli.l4l3(chanp->d_st, CC_DLRL, chanp->proc);
-		chanp->Flags = 0;
-		FsmEvent(&chanp->lc_d->lcfi, EV_LC_RELEASE, NULL);
-	}
-	chanp->cs->cardmsg(chanp->cs, MDL_INFO_REL, (void *) chanp->chan);
-}
-
-/* *INDENT-OFF* */
-static struct FsmNode fnlist[] HISAX_INITDATA =
-{
-	{ST_NULL,		EV_DIAL,		lli_prep_dialout},
-	{ST_NULL,		EV_SETUP_IND,		lli_deliver_call},
-	{ST_NULL,		EV_SHUTDOWN_D,		lli_shutdown_d},
-	{ST_NULL,		EV_DLRL,		lli_go_null},
-	{ST_NULL,		EV_DLEST,		lli_d_established},
-	{ST_NULL,		EV_ESTABLISH,		lli_establish_d},
-	{ST_OUT_WAIT_D,		EV_DLEST,		lli_do_dialout},
-	{ST_OUT_WAIT_D,		EV_DLRL,		lli_no_dchan},
-	{ST_OUT_WAIT_D,		EV_HANGUP,		lli_no_dchan},
-	{ST_IN_WAIT_D,		EV_DLEST,		lli_do_action},
-	{ST_IN_WAIT_D,		EV_DLRL,		lli_no_dchan_in},
-	{ST_IN_WAIT_D,		EV_ACCEPTD,		lli_start_dchan},
-	{ST_IN_WAIT_D,		EV_HANGUP,		lli_start_dchan},
-	{ST_OUT_DIAL,		EV_SETUP_CNF,		lli_init_bchan_out},
-	{ST_OUT_DIAL,		EV_HANGUP,		lli_cancel_call},
-	{ST_OUT_DIAL,		EV_DISCONNECT_IND,	lli_received_d_disc},
-	{ST_OUT_DIAL,		EV_RELEASE_IND,		lli_received_d_rel},
-	{ST_OUT_DIAL,		EV_RELEASE_CNF,		lli_received_d_relcnf},
-	{ST_OUT_DIAL,		EV_NOSETUP_RSP,		lli_no_setup_rsp},
-	{ST_OUT_DIAL,		EV_SETUP_ERR,		lli_setup_err},
-	{ST_OUT_DIAL,		EV_DLRL,		lli_got_dlrl},
-	{ST_IN_WAIT_LL,		EV_DLEST,		lli_d_established},
-	{ST_IN_WAIT_LL,		EV_DLRL,		lli_d_released},
-	{ST_IN_WAIT_LL,		EV_ACCEPTD,		lli_start_dchan},
-	{ST_IN_WAIT_LL,		EV_HANGUP,		lli_start_dchan},
-	{ST_IN_WAIT_LL,		EV_DISCONNECT_IND,	lli_received_d_disc},
-	{ST_IN_WAIT_LL,		EV_RELEASE_IND,		lli_received_d_rel},
-	{ST_IN_WAIT_LL,		EV_RELEASE_CNF,		lli_received_d_relcnf},
-	{ST_IN_ALERT_SEND,	EV_SETUP_CMPL_IND,	lli_init_bchan_in},
-	{ST_IN_ALERT_SEND,	EV_ACCEPTD,		lli_send_dconnect},
-	{ST_IN_ALERT_SEND,	EV_HANGUP,		lli_send_d_disc},
-	{ST_IN_ALERT_SEND,	EV_DISCONNECT_IND,	lli_received_d_disc},
-	{ST_IN_ALERT_SEND,	EV_RELEASE_IND,		lli_received_d_rel},
-	{ST_IN_ALERT_SEND,	EV_RELEASE_CNF,		lli_received_d_relcnf},
-	{ST_IN_ALERT_SEND,	EV_DLRL,		lli_got_dlrl},
-	{ST_IN_WAIT_CONN_ACK,	EV_SETUP_CMPL_IND,	lli_init_bchan_in},
-	{ST_IN_WAIT_CONN_ACK,	EV_HANGUP,		lli_send_d_disc},
-	{ST_IN_WAIT_CONN_ACK,	EV_DISCONNECT_IND,	lli_received_d_disc},
-	{ST_IN_WAIT_CONN_ACK,	EV_RELEASE_IND,		lli_received_d_rel},
-	{ST_IN_WAIT_CONN_ACK,	EV_RELEASE_CNF,		lli_received_d_relcnf},
-	{ST_IN_WAIT_CONN_ACK,	EV_CONNECT_ERR,		lli_connect_err},
-	{ST_IN_WAIT_CONN_ACK,	EV_DLRL,		lli_got_dlrl},
-	{ST_WAIT_BCONN,		EV_BC_EST,		lli_go_active},
-	{ST_WAIT_BCONN,		EV_BC_REL,		lli_send_d_disc},
-	{ST_WAIT_BCONN,		EV_HANGUP,		lli_send_d_disc},
-	{ST_WAIT_BCONN,		EV_DISCONNECT_IND,	lli_received_d_disc},
-	{ST_WAIT_BCONN,		EV_RELEASE_IND,		lli_received_d_rel},
-	{ST_WAIT_BCONN,		EV_RELEASE_CNF,		lli_received_d_relcnf},
-	{ST_WAIT_BCONN,		EV_DLRL,		lli_got_dlrl},
-	{ST_WAIT_BCONN,		EV_CINF,		lli_charge_info},
-	{ST_ACTIVE,		EV_CINF,		lli_charge_info},
-	{ST_ACTIVE,		EV_BC_REL,		lli_released_bchan},
-	{ST_ACTIVE,		EV_HANGUP,		lli_disconn_bchan},
-	{ST_ACTIVE,		EV_DISCONNECT_IND,	lli_release_bchan},
-	{ST_ACTIVE,		EV_RELEASE_CNF,		lli_received_d_relcnf},
-	{ST_ACTIVE,		EV_RELEASE_IND,		lli_received_d_rel},
-	{ST_ACTIVE,		EV_DLRL,		lli_got_dlrl},
-	{ST_WAIT_BRELEASE,	EV_BC_REL,		lli_send_d_disc},
-	{ST_WAIT_BRELEASE,	EV_DISCONNECT_IND,	lli_received_d_disc},
-	{ST_WAIT_BRELEASE,	EV_RELEASE_CNF,		lli_received_d_relcnf},
-	{ST_WAIT_BRELEASE,	EV_RELEASE_IND,		lli_received_d_rel},
-	{ST_WAIT_BRELEASE,	EV_DLRL,		lli_got_dlrl},
-	{ST_WAIT_BREL_DISC,	EV_BC_REL,		lli_received_d_disc},
-	{ST_WAIT_BREL_DISC,	EV_RELEASE_CNF,		lli_received_d_relcnf},
-	{ST_WAIT_BREL_DISC,	EV_RELEASE_IND,		lli_received_d_rel},
-	{ST_WAIT_BREL_DISC,	EV_DLRL,		lli_got_dlrl},
-	{ST_WAIT_DCOMMAND,	EV_HANGUP,		lli_send_d_disc},
-	{ST_WAIT_DCOMMAND,	EV_DISCONNECT_IND,	lli_received_d_disc},
-	{ST_WAIT_DCOMMAND,	EV_RELEASE_CNF,		lli_received_d_relcnf},
-	{ST_WAIT_DCOMMAND,	EV_RELEASE_IND,		lli_received_d_rel},
-	{ST_WAIT_DCOMMAND,	EV_DLRL,		lli_got_dlrl},
-	{ST_WAIT_DRELEASE,	EV_RELEASE_IND,		lli_timeout_d},
-	{ST_WAIT_DRELEASE,	EV_RELEASE_CNF,		lli_timeout_d},
-	{ST_WAIT_DRELEASE,	EV_RELEASE_ERR,		lli_timeout_d},
-	{ST_WAIT_DRELEASE,	EV_DIAL,		lli_no_dchan_ready},
-	{ST_WAIT_DRELEASE,	EV_DLRL,		lli_got_dlrl},
-	{ST_WAIT_D_REL_CNF,	EV_RELEASE_CNF,		lli_timeout_d},
-	{ST_WAIT_D_REL_CNF,	EV_RELEASE_ERR,		lli_timeout_d},
-/* ETS 300-104 16.1 */
-	{ST_WAIT_D_REL_CNF,     EV_RELEASE_IND,         lli_timeout_d},
-	{ST_WAIT_D_REL_CNF,	EV_DIAL,		lli_no_dchan_ready},
-	{ST_WAIT_D_REL_CNF,	EV_DLRL,		lli_got_dlrl},
-	{ST_WAIT_DSHUTDOWN,	EV_DLRL,		lli_go_null},
-	{ST_WAIT_DSHUTDOWN,	EV_DLEST,		lli_d_established},
-	{ST_WAIT_DSHUTDOWN,	EV_DIAL,		lli_prep_dialout},
-	{ST_WAIT_DSHUTDOWN,	EV_SETUP_IND,		lli_deliver_call},
-};
-/* *INDENT-ON* */
-
-
-#define FNCOUNT (sizeof(fnlist)/sizeof(struct FsmNode))
-
-static void
-lc_activate_l1(struct FsmInst *fi, int event, void *arg)
-{
-	struct LcFsm *lf = fi->userdata;
-
-	FsmDelTimer(&lf->act_timer, 50);
-	FsmChangeState(fi, ST_LC_ACTIVATE_WAIT);
-	/* This timeout is to avoid a hang if no L1 activation is possible */
-	FsmAddTimer(&lf->act_timer, 30000, EV_LC_TIMER, NULL, 50);
-	lf->st->ma.manl1(lf->st, PH_ACTIVATE_REQ, NULL);
-}
-
-static void
-lc_activated_from_l1(struct FsmInst *fi, int event, void *arg)
-{
-	struct LcFsm *lf = fi->userdata;
-
-	if (lf->l2_establish)
-		FsmChangeState(fi, ST_LC_DELAY);
-	else {
-		FsmChangeState(fi, ST_LC_CONNECTED);
-		lf->lccall(lf, LC_ESTABLISH, NULL);
-	}
+	FsmChangeState(fi, ST_WAIT_BREL_DISC);
+	chanp->b_st->lli.l4l3(chanp->b_st, DL_RELEASE | REQUEST, NULL);
 }
 
-static void
-lc_l1_activated(struct FsmInst *fi, int event, void *arg)
-{
-	struct LcFsm *lf = fi->userdata;
-
-	FsmDelTimer(&lf->act_timer, 50);
-	FsmChangeState(fi, ST_LC_DELAY);
-	/* This timer is needed for delay the first paket on a channel
-	   to be shure that the other side is ready too */
-	if (lf->delay)
-		FsmAddTimer(&lf->act_timer, lf->delay, EV_LC_TIMER, NULL, 51);
-	else
-		FsmEvent(fi, EV_LC_TIMER, NULL);
-}
 
 static void
-lc_start_l2(struct FsmInst *fi, int event, void *arg)
+lli_rel_b_dhup(struct FsmInst *fi, int event, void *arg)
 {
-	struct LcFsm *lf = fi->userdata;
+        struct Channel *chanp = fi->userdata;
 
-/*	if (!lf->st->l1.act_state)
-		lf->st->l1.act_state = 2;
-*/	if (lf->l2_establish) {
-		FsmChangeState(fi, ST_LC_ESTABLISH_WAIT);
-		if (lf->l2_start)
-			lf->st->ma.manl2(lf->st, DL_ESTABLISH, NULL);
-	} else {
-		FsmChangeState(fi, ST_LC_CONNECTED);
-		lf->lccall(lf, LC_ESTABLISH, NULL);
-	}
+        release_b_st(chanp);
+        lli_dhup_close(fi, event, arg);
 }
 
 static void
-lc_connected(struct FsmInst *fi, int event, void *arg)
+lli_bhup_dhup(struct FsmInst *fi, int event, void *arg)
 {
-	struct LcFsm *lf = fi->userdata;
+        struct Channel *chanp = fi->userdata;
 
-	FsmDelTimer(&lf->act_timer, 50);
-	FsmChangeState(fi, ST_LC_CONNECTED);
-	lf->lccall(lf, LC_ESTABLISH, NULL);
+        if (chanp->debug & 1)
+                link_debug(chanp, 0, "STAT_BHUP");
+        HL_LL(chanp, ISDN_STAT_BHUP);
+ 
+        lli_rel_b_dhup(fi, event, arg);
 }
 
 static void
-lc_release_l2(struct FsmInst *fi, int event, void *arg)
+lli_abort(struct FsmInst *fi, int event, void *arg)
 {
-	struct LcFsm *lf = fi->userdata;
+        struct Channel *chanp = fi->userdata;
 
-	if (lf->l2_establish) {
-		FsmChangeState(fi, ST_LC_RELEASE_WAIT);
-		lf->st->ma.manl2(lf->st, DL_RELEASE, NULL);
-	} else {
-		FsmChangeState(fi, ST_LC_NULL);
-		lf->st->ma.manl1(lf->st, PH_DEACTIVATE_REQ, NULL);
-		lf->lccall(lf, LC_RELEASE, NULL);
-	}
+        chanp->data_open = 0;
+        chanp->b_st->lli.l4l3(chanp->b_st, DL_RELEASE | REQUEST, NULL);
+ 
+        lli_bhup_dhup(fi, event, arg);
 }
-
+ 
 static void
-lc_l2_released(struct FsmInst *fi, int event, void *arg)
+lli_release_req(struct FsmInst *fi, int event, void *arg)
 {
-	struct LcFsm *lf = fi->userdata;
-
-	FsmChangeState(fi, ST_LC_RELEASE_WAIT);
-	FsmDelTimer(&lf->act_timer, 51);
-	/* This delay is needed for send out the UA frame before
-	 * PH_DEACTIVATE the interface
-	 */
-	FsmAddTimer(&lf->act_timer, 20, EV_LC_TIMER, NULL, 54);
+        struct Channel *chanp = fi->userdata;
+ 
+        FsmChangeState(fi, ST_WAIT_D_REL_CNF);
+        chanp->d_st->lli.l4l3(chanp->d_st, CC_RELEASE | REQUEST, chanp->proc);
 }
 
 static void
-lc_release_l1(struct FsmInst *fi, int event, void *arg)
+lli_rel_b_release_req(struct FsmInst *fi, int event, void *arg)
 {
-	struct LcFsm *lf = fi->userdata;
-
-	FsmDelTimer(&lf->act_timer, 54);
-	FsmChangeState(fi, ST_LC_NULL);
-	lf->st->ma.manl1(lf->st, PH_DEACTIVATE_REQ, NULL);
-	lf->lccall(lf, LC_RELEASE, NULL);
+        struct Channel *chanp = fi->userdata;
+ 
+        release_b_st(chanp);
+        lli_release_req(fi, event, arg);
 }
 
 static void
-lc_l1_deactivated(struct FsmInst *fi, int event, void *arg)
-{
-	struct LcFsm *lf = fi->userdata;
-
-	FsmDelTimer(&lf->act_timer, 54);
-	FsmChangeState(fi, ST_LC_NULL);
-	lf->lccall(lf, LC_RELEASE, NULL);
-}
-/* *INDENT-OFF* */
-static struct FsmNode LcFnList[] HISAX_INITDATA =
-{
-	{ST_LC_NULL,  		EV_LC_ESTABLISH,	lc_activate_l1},
-	{ST_LC_NULL,  		EV_LC_PH_ACTIVATE,	lc_activated_from_l1},
-	{ST_LC_NULL,  		EV_LC_DL_ESTABLISH,	lc_connected},
-	{ST_LC_ACTIVATE_WAIT,	EV_LC_PH_ACTIVATE,	lc_l1_activated},
-	{ST_LC_ACTIVATE_WAIT,	EV_LC_TIMER,		lc_release_l1},
-	{ST_LC_ACTIVATE_WAIT,  	EV_LC_PH_DEACTIVATE,	lc_l1_deactivated},
-	{ST_LC_DELAY,  		EV_LC_ESTABLISH,	lc_start_l2},
-	{ST_LC_DELAY,		EV_LC_TIMER,		lc_start_l2},
-	{ST_LC_DELAY,		EV_LC_DL_ESTABLISH,	lc_connected},
-	{ST_LC_DELAY,  		EV_LC_PH_DEACTIVATE,	lc_l1_deactivated},
-	{ST_LC_ESTABLISH_WAIT,	EV_LC_DL_ESTABLISH,	lc_connected},
-	{ST_LC_ESTABLISH_WAIT,	EV_LC_RELEASE,		lc_release_l1},
-	{ST_LC_ESTABLISH_WAIT,	EV_LC_DL_RELEASE,	lc_release_l1},
-	{ST_LC_ESTABLISH_WAIT,  EV_LC_PH_DEACTIVATE,	lc_l1_deactivated},
-	{ST_LC_CONNECTED,	EV_LC_ESTABLISH,	lc_connected},
-	{ST_LC_CONNECTED,	EV_LC_RELEASE,		lc_release_l2},
-	{ST_LC_CONNECTED,	EV_LC_DL_RELEASE,	lc_l2_released},
-	{ST_LC_CONNECTED,  	EV_LC_PH_DEACTIVATE,	lc_l1_deactivated},
-	{ST_LC_FLUSH_WAIT,	EV_LC_TIMER,		lc_release_l2},
-	{ST_LC_FLUSH_WAIT,  	EV_LC_PH_DEACTIVATE,	lc_l1_deactivated},
-	{ST_LC_RELEASE_WAIT,	EV_LC_DL_RELEASE,	lc_release_l1},
-	{ST_LC_RELEASE_WAIT,	EV_LC_TIMER,		lc_release_l1},
-	{ST_LC_FLUSH_WAIT,  	EV_LC_PH_DEACTIVATE,	lc_l1_deactivated},
-};
-/* *INDENT-ON* */
-
-
-#define LC_FN_COUNT (sizeof(LcFnList)/sizeof(struct FsmNode))
-
-HISAX_INITFUNC(void
-CallcNew(void))
+lli_bhup_release_req(struct FsmInst *fi, int event, void *arg)
 {
-	callcfsm.state_count = STATE_COUNT;
-	callcfsm.event_count = EVENT_COUNT;
-	callcfsm.strEvent = strEvent;
-	callcfsm.strState = strState;
-	FsmNew(&callcfsm, fnlist, FNCOUNT);
-
-	lcfsm.state_count = LC_STATE_COUNT;
-	lcfsm.event_count = LC_EVENT_COUNT;
-	lcfsm.strEvent = strLcEvent;
-	lcfsm.strState = strLcState;
-	FsmNew(&lcfsm, LcFnList, LC_FN_COUNT);
+        struct Channel *chanp = fi->userdata;
+ 
+        if (chanp->debug & 1)
+                link_debug(chanp, 0, "STAT_BHUP");
+        HL_LL(chanp, ISDN_STAT_BHUP);
+ 
+        lli_rel_b_release_req(fi, event, arg);
 }
 
-void
-CallcFree(void)
-{
-	FsmFree(&lcfsm);
-	FsmFree(&callcfsm);
-}
 
+/* processing charge info */
 static void
-release_b_st(struct Channel *chanp)
+lli_charge_info(struct FsmInst *fi, int event, void *arg)
 {
-	struct PStack *st = chanp->b_st;
+	struct Channel *chanp = fi->userdata;
+	isdn_ctrl ic;
 
-	chanp->bcs->BC_Close(chanp->bcs);
-	switch (chanp->l2_active_protocol) {
-		case (ISDN_PROTO_L2_X75I):
-			releasestack_isdnl2(st);
-			break;
-		case (ISDN_PROTO_L2_HDLC):
-		case (ISDN_PROTO_L2_TRANS):
-			releasestack_transl2(st);
-			break;
-	}
-	/* Reset B-Channel Statemachine */
-	FsmDelTimer(&chanp->lc_b->act_timer, 79);
-	FsmChangeState(&chanp->lc_b->lcfi, ST_LC_NULL);
+	ic.driver = chanp->cs->myid;
+	ic.command = ISDN_STAT_CINF;
+	ic.arg = chanp->chan;
+	sprintf(ic.parm.num, "%d", chanp->proc->para.chargeinfo);
+	chanp->cs->iif.statcallb(&ic);
 }
 
+/* error procedures */
+
 static void
-dc_l1man(struct PStack *st, int pr, void *arg)
+lli_dchan_not_ready(struct FsmInst *fi, int event, void *arg)
 {
-	struct Channel *chanp;
+	struct Channel *chanp = fi->userdata;
 
-	chanp = (struct Channel *) st->lli.userdata;
-	switch (pr) {
-		case (PH_ACTIVATE_CNF):
-		case (PH_ACTIVATE_IND):
-			FsmEvent(&chanp->lc_d->lcfi, EV_LC_PH_ACTIVATE, NULL);
-			break;
-		case (PH_DEACTIVATE_IND):
-			FsmEvent(&chanp->lc_d->lcfi, EV_LC_PH_DEACTIVATE, NULL);
-			break;
-	}
+	if (chanp->debug & 1)
+		link_debug(chanp, 0, "STAT_DHUP");
+        HL_LL(chanp, ISDN_STAT_DHUP); 
 }
 
 static void
-dc_l2man(struct PStack *st, int pr, void *arg)
+lli_no_setup_rsp(struct FsmInst *fi, int event, void *arg)
 {
-	struct Channel *chanp = (struct Channel *) st->lli.userdata;
+	struct Channel *chanp = fi->userdata;
 
-	switch (pr) {
-		case (DL_ESTABLISH):
-			FsmEvent(&chanp->lc_d->lcfi, EV_LC_DL_ESTABLISH, NULL);
-			break;
-		case (DL_RELEASE):
-			FsmEvent(&chanp->lc_d->lcfi, EV_LC_DL_RELEASE, NULL);
-			break;
-	}
-}
+	if (chanp->debug & 1)
+		link_debug(chanp, 0, "STAT_DHUP");
+	HL_LL(chanp, ISDN_STAT_DHUP);
+        lli_close(fi); 
+}
+
+static void
+lli_error(struct FsmInst *fi, int event, void *arg)
+{
+        FsmChangeState(fi, ST_WAIT_DRELEASE);
+}
+
+static void
+lli_failure_l(struct FsmInst *fi, int event, void *arg)
+{
+        struct Channel *chanp = fi->userdata;
+        isdn_ctrl ic;
+
+        FsmChangeState(fi, ST_NULL);
+        ic.driver = chanp->cs->myid;
+        ic.command = ISDN_STAT_CAUSE;
+        ic.arg = chanp->chan;
+        sprintf(ic.parm.num, "L%02X%02X", 0, 0x2f);
+        chanp->cs->iif.statcallb(&ic);
+        HL_LL(chanp, ISDN_STAT_DHUP);
+        chanp->Flags = 0;
+        chanp->cs->cardmsg(chanp->cs, MDL_INFO_REL, (void *) (long)chanp->chan);
+}
+
+static void
+lli_rel_b_fail(struct FsmInst *fi, int event, void *arg)
+{
+        struct Channel *chanp = fi->userdata;
+
+        release_b_st(chanp);
+        lli_failure_l(fi, event, arg);
+}
+
+static void
+lli_bhup_fail(struct FsmInst *fi, int event, void *arg)
+{
+        struct Channel *chanp = fi->userdata;
+
+        if (chanp->debug & 1)
+                link_debug(chanp, 0, "STAT_BHUP");
+        HL_LL(chanp, ISDN_STAT_BHUP);
+ 
+        lli_rel_b_fail(fi, event, arg);
+}
+
+static void
+lli_failure_a(struct FsmInst *fi, int event, void *arg)
+{
+        struct Channel *chanp = fi->userdata;
+
+        chanp->data_open = 0;
+        chanp->b_st->lli.l4l3(chanp->b_st, DL_RELEASE | REQUEST, NULL);
+ 
+        lli_bhup_fail(fi, event, arg);
+}
+
+  /* *INDENT-OFF* */
+  static struct FsmNode fnlist[] HISAX_INITDATA =
+  {
+        {ST_NULL,               EV_DIAL,                lli_prep_dialout},
+        {ST_NULL,               EV_RESUME,              lli_resume},
+        {ST_NULL,               EV_SETUP_IND,           lli_deliver_call},
+        {ST_NULL,               EV_LEASED,              lli_leased_in},
+        {ST_OUT_DIAL,           EV_SETUP_CNF,           lli_init_bchan_out},
+        {ST_OUT_DIAL,           EV_HANGUP,              lli_disconnect_req},
+        {ST_OUT_DIAL,           EV_DISCONNECT_IND,      lli_release_req},
+        {ST_OUT_DIAL,           EV_RELEASE,             lli_dhup_close},
+        {ST_OUT_DIAL,           EV_NOSETUP_RSP,         lli_no_setup_rsp},
+        {ST_OUT_DIAL,           EV_SETUP_ERR,           lli_error},
+        {ST_IN_WAIT_LL,         EV_LEASED_REL,          lli_failure_l},
+        {ST_IN_WAIT_LL,         EV_ACCEPTD,             lli_setup_rsp},
+        {ST_IN_WAIT_LL,         EV_HANGUP,              lli_reject_req},
+        {ST_IN_WAIT_LL,         EV_DISCONNECT_IND,      lli_release_req},
+        {ST_IN_WAIT_LL,         EV_RELEASE,             lli_dhup_close},
+        {ST_IN_ALERT_SENT,      EV_SETUP_CMPL_IND,      lli_init_bchan_in},
+        {ST_IN_ALERT_SENT,      EV_ACCEPTD,             lli_send_dconnect},
+        {ST_IN_ALERT_SENT,      EV_HANGUP,              lli_disconnect_reject},
+        {ST_IN_ALERT_SENT,      EV_DISCONNECT_IND,      lli_release_req},
+        {ST_IN_ALERT_SENT,      EV_RELEASE,             lli_dhup_close},
+	{ST_IN_ALERT_SENT,	EV_REDIR,		lli_send_redir},
+	{ST_IN_PROCEED_SEND,	EV_REDIR,		lli_send_redir},
+	{ST_IN_PROCEED_SEND,	EV_ALERT,		lli_send_alert},
+	{ST_IN_PROCEED_SEND,	EV_ACCEPTD,		lli_send_dconnect},
+	{ST_IN_PROCEED_SEND,	EV_HANGUP,		lli_disconnect_reject},
+	{ST_IN_PROCEED_SEND,	EV_DISCONNECT_IND,	lli_dhup_close},
+        {ST_IN_ALERT_SENT,      EV_RELEASE,             lli_dhup_close},
+        {ST_IN_WAIT_CONN_ACK,   EV_SETUP_CMPL_IND,      lli_init_bchan_in},
+        {ST_IN_WAIT_CONN_ACK,   EV_HANGUP,              lli_disconnect_req},
+        {ST_IN_WAIT_CONN_ACK,   EV_DISCONNECT_IND,      lli_release_req},
+        {ST_IN_WAIT_CONN_ACK,   EV_RELEASE,             lli_dhup_close},
+        {ST_IN_WAIT_CONN_ACK,   EV_CONNECT_ERR,         lli_error},
+        {ST_WAIT_BCONN,         EV_BC_EST,              lli_go_active},
+        {ST_WAIT_BCONN,         EV_BC_REL,              lli_rel_b_disc},
+        {ST_WAIT_BCONN,         EV_HANGUP,              lli_rel_b_disc},
+        {ST_WAIT_BCONN,         EV_DISCONNECT_IND,      lli_rel_b_release_req},
+        {ST_WAIT_BCONN,         EV_RELEASE,             lli_rel_b_dhup},
+        {ST_WAIT_BCONN,         EV_LEASED_REL,          lli_rel_b_fail},
+        {ST_WAIT_BCONN,         EV_CINF,                lli_charge_info},
+        {ST_ACTIVE,             EV_CINF,                lli_charge_info},
+        {ST_ACTIVE,             EV_BC_REL,              lli_bhup_rel_b},
+        {ST_ACTIVE,             EV_SUSPEND,             lli_suspend},
+        {ST_ACTIVE,             EV_HANGUP,              lli_disconn_bchan},
+        {ST_ACTIVE,             EV_DISCONNECT_IND,      lli_release_bchan},
+        {ST_ACTIVE,             EV_RELEASE,             lli_abort},
+        {ST_ACTIVE,             EV_LEASED_REL,          lli_failure_a},
+        {ST_WAIT_BRELEASE,      EV_BC_REL,              lli_bhup_disc},
+        {ST_WAIT_BRELEASE,      EV_DISCONNECT_IND,      lli_bhup_release_req},
+        {ST_WAIT_BRELEASE,      EV_RELEASE,             lli_bhup_dhup},
+        {ST_WAIT_BRELEASE,      EV_LEASED_REL,          lli_bhup_fail},
+        {ST_WAIT_BREL_DISC,     EV_BC_REL,              lli_bhup_release_req},
+        {ST_WAIT_BREL_DISC,     EV_RELEASE,             lli_bhup_dhup},
+        {ST_WAIT_DCOMMAND,      EV_HANGUP,              lli_start_disc},
+        {ST_WAIT_DCOMMAND,      EV_DISCONNECT_IND,      lli_release_req},
+        {ST_WAIT_DCOMMAND,      EV_RELEASE,             lli_dhup_close},
+        {ST_WAIT_DCOMMAND,      EV_LEASED_REL,          lli_failure_l},
+        {ST_WAIT_DRELEASE,      EV_RELEASE,             lli_dhup_close},
+        {ST_WAIT_DRELEASE,      EV_DIAL,                lli_dchan_not_ready},
+  /* ETS 300-104 16.1 */
+        {ST_WAIT_D_REL_CNF,     EV_RELEASE,             lli_dhup_close},
+        {ST_WAIT_D_REL_CNF,     EV_DIAL,                lli_dchan_not_ready},
+};
+  /* *INDENT-ON* */
 
-static void
-bc_l1man(struct PStack *st, int pr, void *arg)
+
+  #define FNCOUNT (sizeof(fnlist)/sizeof(struct FsmNode))
+
+HISAX_INITFUNC(void
+CallcNew(void))
 {
-	struct Channel *chanp = (struct Channel *) st->lli.userdata;
+	callcfsm.state_count = STATE_COUNT;
+	callcfsm.event_count = EVENT_COUNT;
+	callcfsm.strEvent = strEvent;
+	callcfsm.strState = strState;
+	FsmNew(&callcfsm, fnlist, FNCOUNT);
+}
 
-	switch (pr) {
-		case (PH_ACTIVATE_IND):
-		case (PH_ACTIVATE_CNF):
-			FsmEvent(&chanp->lc_b->lcfi, EV_LC_PH_ACTIVATE, NULL);
-			break;
-		case (PH_DEACTIVATE_IND):
-			FsmEvent(&chanp->lc_b->lcfi, EV_LC_PH_DEACTIVATE, NULL);
-			break;
-	}
+void
+CallcFree(void)
+{
+	FsmFree(&callcfsm);
 }
 
 static void
-bc_l2man(struct PStack *st, int pr, void *arg)
+release_b_st(struct Channel *chanp)
 {
-	struct Channel *chanp = (struct Channel *) st->lli.userdata;
+	struct PStack *st = chanp->b_st;
 
-	switch (pr) {
-		case (DL_ESTABLISH):
-			FsmEvent(&chanp->lc_b->lcfi, EV_LC_DL_ESTABLISH, NULL);
-			break;
-		case (DL_RELEASE):
-			FsmEvent(&chanp->lc_b->lcfi, EV_LC_DL_RELEASE, NULL);
-			break;
-	}
+        if(test_and_clear_bit(FLG_START_B, &chanp->Flags)) {
+                chanp->bcs->BC_Close(chanp->bcs);
+                switch (chanp->l2_active_protocol) {
+			case (ISDN_PROTO_L2_X75I):
+				releasestack_isdnl2(st);
+				break;
+			case (ISDN_PROTO_L2_HDLC):
+			case (ISDN_PROTO_L2_TRANS):
+			case (ISDN_PROTO_L2_MODEM):
+                                releasestack_transl2(st);
+                                break;
+                }
+        } 
 }
 
 struct Channel
-*selectfreechannel(struct PStack *st)
+*selectfreechannel(struct PStack *st, int bch)
 {
 	struct IsdnCardState *cs = st->l1.hardware;
 	struct Channel *chanp = st->lli.userdata;
@@ -1466,87 +972,96 @@
 		i=1;
 	else
 		i=0;
-	while (i<2) {
+
+        if (!bch) 
+         { i = 2; /* virtual channel */
+           chanp += 2;
+         }
+
+	while (i < ((bch) ? cs->chanlimit : (2 + MAX_WAITING_CALLS))) {
 		if (chanp->fi.state == ST_NULL)
 			return (chanp);
 		chanp++;
 		i++;
-	}		
-	return (NULL);
-}
-
-int
-is_activ(struct PStack *st)
-{
-	struct IsdnCardState *cs = st->l1.hardware;
-	struct Channel *chanp = st->lli.userdata;
-	int i;
+	}
 
-	if (test_bit(FLG_TWO_DCHAN, &cs->HW_Flags))
-		i=1;
-	else
-		i=0;
-	while (i<2) {
-		if (test_bit(FLG_ESTAB_D, &chanp->Flags))
-			return (1);
+        if (bch) /* number of channels is limited */ 
+         { i = 2; /* virtual channel */
+           chanp = st->lli.userdata;
+	   chanp += i;
+	   while (i < (2 + MAX_WAITING_CALLS)) {
+		if (chanp->fi.state == ST_NULL)
+			return (chanp);
 		chanp++;
 		i++;
-	}		
-	return (0);
+	   }
+         }
+	return (NULL);
 }
 
+static void stat_redir_result(struct IsdnCardState *cs, int chan, ulong result)
+{	isdn_ctrl ic;
+  
+	ic.driver = cs->myid;
+	ic.command = ISDN_STAT_REDIR;
+	ic.arg = chan; 
+	(ulong)(ic.parm.num[0]) = result;
+	cs->iif.statcallb(&ic);
+} /* stat_redir_result */
+
 static void
-ll_handler(struct l3_process *pc, int pr, void *arg)
+dchan_l3l4(struct PStack *st, int pr, void *arg)
 {
+	struct l3_process *pc = arg;
+        struct IsdnCardState *cs = st->l1.hardware;
 	struct Channel *chanp;
-	char tmp[64], tm[32];
 
-	if (pr == CC_SETUP_IND) {
-		if (!(chanp = selectfreechannel(pc->st))) {
-			pc->st->lli.l4l3(pc->st, CC_DLRL, pc);
-			return;
-		} else {
-			chanp->proc = pc;
-			pc->chan = chanp;
+        if(!pc)
+                return;
+ 
+        if (pr == (CC_SETUP | INDICATION)) {
+                if (!(chanp = selectfreechannel(pc->st, pc->para.bchannel))) {
+                        pc->para.cause = 0x11;          /* User busy */
+                        pc->st->lli.l4l3(pc->st, CC_REJECT | REQUEST, pc);
+                } else {
+                        chanp->proc = pc;
+                        pc->chan = chanp;
 			FsmEvent(&chanp->fi, EV_SETUP_IND, NULL);
-			return;
 		}
-	} else if (pr == CC_ESTABLISH) {
-		if (is_activ(pc->st)) {
-			pc->st->lli.l4l3(pc->st, CC_ESTABLISH, pc);
-			return;
-		} else if (!(chanp = selectfreechannel(pc->st))) {
-			pc->st->lli.l4l3(pc->st, CC_DLRL, pc);
-			return;
-		} else {
-			chanp->proc = pc;
-			FsmEvent(&chanp->fi, EV_ESTABLISH, NULL);
-			return;
-		}
-
-			
+		return;
 	}
-	chanp = pc->chan;
+	if (!(chanp = pc->chan))
+		return;
+
 	switch (pr) {
-		case (CC_DISCONNECT_IND):
+		case (CC_DISCONNECT | INDICATION):
 			FsmEvent(&chanp->fi, EV_DISCONNECT_IND, NULL);
 			break;
-		case (CC_RELEASE_CNF):
-			FsmEvent(&chanp->fi, EV_RELEASE_CNF, NULL);
+		case (CC_RELEASE | CONFIRM):
+			FsmEvent(&chanp->fi, EV_RELEASE, NULL);
+			break;
+		case (CC_SUSPEND | CONFIRM):
+			FsmEvent(&chanp->fi, EV_RELEASE, NULL);
+			break;
+		case (CC_RESUME | CONFIRM):
+			FsmEvent(&chanp->fi, EV_SETUP_CNF, NULL);
+			break;
+		case (CC_RESUME_ERR):
+			FsmEvent(&chanp->fi, EV_RELEASE, NULL);
 			break;
-		case (CC_RELEASE_IND):
-			FsmEvent(&chanp->fi, EV_RELEASE_IND, NULL);
+		case (CC_RELEASE | INDICATION):
+			FsmEvent(&chanp->fi, EV_RELEASE, NULL);
 			break;
-		case (CC_SETUP_COMPLETE_IND):
+		case (CC_SETUP_COMPL | INDICATION):
 			FsmEvent(&chanp->fi, EV_SETUP_CMPL_IND, NULL);
 			break;
-		case (CC_SETUP_CNF):
+		case (CC_SETUP | CONFIRM):
 			FsmEvent(&chanp->fi, EV_SETUP_CNF, NULL);
 			break;
-		case (CC_INFO_CHARGE):
+		case (CC_CHARGE | INDICATION):
 			FsmEvent(&chanp->fi, EV_CINF, NULL);
 			break;
-		case (CC_NOSETUP_RSP_ERR):
+		case (CC_NOSETUP_RSP):
 			FsmEvent(&chanp->fi, EV_NOSETUP_RSP, NULL);
 			break;
 		case (CC_SETUP_ERR):
@@ -1556,17 +1071,22 @@
 			FsmEvent(&chanp->fi, EV_CONNECT_ERR, NULL);
 			break;
 		case (CC_RELEASE_ERR):
-			FsmEvent(&chanp->fi, EV_RELEASE_ERR, NULL);
-			break;
-		case (CC_PROCEEDING_IND):
-		case (CC_ALERTING_IND):
+			FsmEvent(&chanp->fi, EV_RELEASE, NULL);
 			break;
+		case (CC_PROCEED_SEND | INDICATION):
+		case (CC_PROCEEDING | INDICATION):
+		case (CC_ALERTING | INDICATION):
+		case (CC_PROGRESS | INDICATION):
+		case (CC_NOTIFY | INDICATION):
+			break;
+		case (CC_REDIR | INDICATION):
+			stat_redir_result(cs, chanp->chan, pc->redir_result); 
+			break;                         
 		default:
 			if (chanp->debug & 0x800) {
-				jiftime(tm, jiffies);
-				sprintf(tmp, "%s Channel %d L3->L4 unknown primitiv %d\n",
-					tm, chanp->chan, pr);
-				HiSax_putstatus(chanp->cs, tmp);
+				HiSax_putstatus(chanp->cs, "Ch",
+					"%d L3->L4 unknown primitiv %#x",
+					chanp->chan, pr);
 			}
 	}
 }
@@ -1576,7 +1096,7 @@
 {
 	struct PStack *st = chanp->d_st;
 	struct IsdnCardState *cs = chanp->cs;
-	char tmp[128];
+	char tmp[16];
 
 	HiSax_addlist(cs, st);
 	setstack_HiSax(st, cs);
@@ -1590,95 +1110,51 @@
 	st->l2.window = 1;
 	st->l2.T200 = 1000;	/* 1000 milliseconds  */
 	st->l2.N200 = 3;	/* try 3 times        */
-	if (st->protocol == ISDN_PTYPE_1TR6)
-		st->l2.T203 = 10000;	/* 10000 milliseconds */
+	st->l2.T203 = 10000;	/* 10000 milliseconds */
+	if (test_bit(FLG_TWO_DCHAN, &cs->HW_Flags))
+		sprintf(tmp, "DCh%d Q.921 ", chanp->chan);
 	else
-		st->l2.T203 = 10000;	/* 5000 milliseconds  */
-	
-	sprintf(tmp, "Channel %d q.921", chanp->chan);
+		sprintf(tmp, "DCh Q.921 ");
 	setstack_isdnl2(st, tmp);
-	setstack_isdnl3(st, chanp);
+	setstack_l3dc(st, chanp);
 	st->lli.userdata = chanp;
 	st->lli.l2writewakeup = NULL;
-	st->l3.l3l4 = ll_handler;
-	st->l1.l1man = dc_l1man;
-	st->l2.l2man = dc_l2man;
+	st->l3.l3l4 = dchan_l3l4;
 }
 
 static void
-callc_debug(struct FsmInst *fi, char *s)
+callc_debug(struct FsmInst *fi, char *fmt, ...)
 {
-	char str[80], tm[32];
+	va_list args;
 	struct Channel *chanp = fi->userdata;
+	char tmp[16];
 
-	jiftime(tm, jiffies);
-	sprintf(str, "%s Channel %d callc %s\n", tm, chanp->chan, s);
-	HiSax_putstatus(chanp->cs, str);
-}
-
-static void
-lc_debug(struct FsmInst *fi, char *s)
-{
-	char str[256], tm[32];
-	struct LcFsm *lf = fi->userdata;
-
-	jiftime(tm, jiffies);
-	sprintf(str, "%s Channel %d dc %s\n", tm, lf->ch->chan, s);
-	HiSax_putstatus(lf->ch->cs, str);
-}
-
-static void
-dlc_debug(struct FsmInst *fi, char *s)
-{
-	char str[256], tm[32];
-	struct LcFsm *lf = fi->userdata;
-
-	jiftime(tm, jiffies);
-	sprintf(str, "%s Channel %d bc %s\n", tm, lf->ch->chan, s);
-	HiSax_putstatus(lf->ch->cs, str);
+	va_start(args, fmt);
+	sprintf(tmp, "Ch%d callc ", chanp->chan);
+	VHiSax_putstatus(chanp->cs, tmp, fmt, args);
+	va_end(args);
 }
 
 static void
-lccall_d(struct LcFsm *lf, int pr, void *arg)
-{
-	struct IsdnCardState *cs = lf->st->l1.hardware;
-	struct Channel *chanp;
-	int i;
-
-	if (test_bit(FLG_TWO_DCHAN, &cs->HW_Flags)) {
-		chanp = lf->ch;
-		i = 1;
-	} else {
-		chanp = cs->channel;
-		i = 0;
-	}
-	while (i < 2) {
-		switch (pr) {
-			case (LC_ESTABLISH):
-				FsmEvent(&chanp->fi, EV_DLEST, NULL);
-				break;
-			case (LC_RELEASE):
-				FsmEvent(&chanp->fi, EV_DLRL, NULL);
-				break;
-		}
-		chanp++;
-		i++;
-	}
+dummy_pstack(struct PStack *st, int pr, void *arg) {
+	printk(KERN_WARNING"call to dummy_pstack pr=%04x arg %lx\n", pr, (long)arg);
 }
 
 static void
-lccall_b(struct LcFsm *lf, int pr, void *arg)
-{
-	struct Channel *chanp = lf->ch;
-
-	switch (pr) {
-		case (LC_ESTABLISH):
-			FsmEvent(&chanp->fi, EV_BC_EST, NULL);
-			break;
-		case (LC_RELEASE):
-			FsmEvent(&chanp->fi, EV_BC_REL, NULL);
-			break;
-	}
+init_PStack(struct PStack **stp) {
+	*stp = kmalloc(sizeof(struct PStack), GFP_ATOMIC);
+	(*stp)->next = NULL;
+	(*stp)->l1.l1l2 = dummy_pstack;
+	(*stp)->l1.l1hw = dummy_pstack;
+	(*stp)->l1.l1tei = dummy_pstack;
+	(*stp)->l2.l2tei = dummy_pstack;
+	(*stp)->l2.l2l1 = dummy_pstack;
+	(*stp)->l2.l2l3 = dummy_pstack;
+	(*stp)->l3.l3l2 = dummy_pstack;
+        (*stp)->l3.l3ml3 = dummy_pstack;
+	(*stp)->l3.l3l4 = dummy_pstack;
+	(*stp)->lli.l4l3 = dummy_pstack;
+	(*stp)->ma.layer = dummy_pstack;
 }
 
 static void
@@ -1693,9 +1169,8 @@
 	chanp->debug = 0;
 	chanp->Flags = 0;
 	chanp->leased = 0;
-	chanp->b_st = kmalloc(sizeof(struct PStack), GFP_ATOMIC);
-	chanp->b_st->next = NULL;
-
+	init_PStack(&chanp->b_st);
+	chanp->b_st->l1.delay = DEFAULT_B_DELAY;
 	chanp->fi.fsm = &callcfsm;
 	chanp->fi.state = ST_NULL;
 	chanp->fi.debug = 0;
@@ -1704,56 +1179,35 @@
 	FsmInitTimer(&chanp->fi, &chanp->dial_timer);
 	FsmInitTimer(&chanp->fi, &chanp->drel_timer);
 	if (!chan || test_bit(FLG_TWO_DCHAN, &csta->HW_Flags)) {
-		chanp->d_st = kmalloc(sizeof(struct PStack), GFP_ATOMIC);
+		init_PStack(&chanp->d_st);
+		if (chan)
+			csta->channel->d_st->next = chanp->d_st;
 		chanp->d_st->next = NULL;
 		init_d_st(chanp);
-		chanp->lc_d = kmalloc(sizeof(struct LcFsm), GFP_ATOMIC);
-		chanp->lc_d->lcfi.fsm = &lcfsm;
-		chanp->lc_d->lcfi.state = ST_LC_NULL;
-		chanp->lc_d->lcfi.debug = 0;
-		chanp->lc_d->lcfi.userdata = chanp->lc_d;
-		chanp->lc_d->lcfi.printdebug = lc_debug;
-		chanp->lc_d->type = LC_D;
-		chanp->lc_d->delay = 0;
-		chanp->lc_d->ch = chanp;
-		chanp->lc_d->st = chanp->d_st;
-		chanp->lc_d->l2_establish = !0;
-		chanp->lc_d->l2_start = !0;
-		chanp->lc_d->lccall = lccall_d;
-		FsmInitTimer(&chanp->lc_d->lcfi, &chanp->lc_d->act_timer);
 	} else {
 		chanp->d_st = csta->channel->d_st;
-		chanp->lc_d = csta->channel->lc_d;
 	}
-	chanp->lc_b = kmalloc(sizeof(struct LcFsm), GFP_ATOMIC);
-	chanp->lc_b->lcfi.fsm = &lcfsm;
-	chanp->lc_b->lcfi.state = ST_LC_NULL;
-	chanp->lc_b->lcfi.debug = 0;
-	chanp->lc_b->lcfi.userdata = chanp->lc_b;
-	chanp->lc_b->lcfi.printdebug = dlc_debug;
-	chanp->lc_b->type = LC_B;
-	chanp->lc_b->delay = DEFAULT_B_DELAY;
-	chanp->lc_b->ch = chanp;
-	chanp->lc_b->st = chanp->b_st;
-	chanp->lc_b->l2_establish = !0;
-	chanp->lc_b->l2_start = !0;
-	chanp->lc_b->lccall = lccall_b;
-	FsmInitTimer(&chanp->lc_b->lcfi, &chanp->lc_b->act_timer);
 	chanp->data_open = 0;
 }
 
 int
 CallcNewChan(struct IsdnCardState *csta)
-{
+{       int i;
+
 	chancount += 2;
 	init_chan(0, csta);
 	init_chan(1, csta);
 	printk(KERN_INFO "HiSax: 2 channels added\n");
-#ifdef LAYER2_WATCHING
-	printk(KERN_INFO "LAYER2 ESTABLISH\n");
-	test_and_set_bit(FLG_START_D, &csta->channel->Flags);
-	FsmEvent(&csta->channel->lc_d->lcfi, EV_LC_ESTABLISH, NULL);
-#endif
+
+	for (i = 0; i < MAX_WAITING_CALLS; i++) 
+		init_chan(i+2,csta);
+	printk(KERN_INFO "HiSax: MAX_WAITING_CALLS added\n");        
+
+	if (test_bit(FLG_PTP, &csta->channel->d_st->l2.flag)) {
+		printk(KERN_INFO "LAYER2 WATCHING ESTABLISH\n");
+		csta->channel->d_st->lli.l4l3(csta->channel->d_st,
+			DL_ESTABLISH | REQUEST, NULL);
+	}
 	return (2);
 }
 
@@ -1779,29 +1233,16 @@
 	for (i = 0; i < 2; i++) {
 		FsmDelTimer(&csta->channel[i].drel_timer, 74);
 		FsmDelTimer(&csta->channel[i].dial_timer, 75);
-		FsmDelTimer(&csta->channel[i].lc_d->act_timer, 77);
-		FsmDelTimer(&csta->channel[i].lc_b->act_timer, 76);
-		if (i || test_bit(FLG_TWO_DCHAN, &csta->HW_Flags))
-			release_d_st(csta->channel + i);
-		if (csta->channel[i].b_st) {
-			if (test_and_clear_bit(FLG_START_B, &csta->channel[i].Flags))
-				release_b_st(csta->channel + i);
-			kfree(csta->channel[i].b_st);
-			csta->channel[i].b_st = NULL;
-		} else
+                if (i || test_bit(FLG_TWO_DCHAN, &csta->HW_Flags))
+                        release_d_st(csta->channel + i);
+                if (csta->channel[i].b_st) {
+                        release_b_st(csta->channel + i);
+                        kfree(csta->channel[i].b_st);
+                        csta->channel[i].b_st = NULL;
+                } else
 			printk(KERN_WARNING "CallcFreeChan b_st ch%d allready freed\n", i);
-		if (csta->channel[i].lc_b) {
-			kfree(csta->channel[i].lc_b);
-			csta->channel[i].b_st = NULL;
-		}
 		if (i || test_bit(FLG_TWO_DCHAN, &csta->HW_Flags)) {
 			release_d_st(csta->channel + i);
-			FsmDelTimer(&csta->channel[i].lc_d->act_timer, 77);
-			if (csta->channel[i].lc_d) {
-				kfree(csta->channel[i].lc_d);
-				csta->channel[i].d_st = NULL;
-			} else
-				printk(KERN_WARNING "CallcFreeChan lc_d ch%d allready freed\n", i);
 		} else
 			csta->channel[i].d_st = NULL;
 	}
@@ -1814,15 +1255,23 @@
 	struct sk_buff *skb = arg;
 
 	switch (pr) {
-		case (DL_DATA):
+		case (DL_DATA  | INDICATION):
 			if (chanp->data_open)
 				chanp->cs->iif.rcvcallb_skb(chanp->cs->myid, chanp->chan, skb);
 			else {
-				dev_kfree_skb(skb);
+				idev_kfree_skb(skb, FREE_READ);
 			}
 			break;
+		case (DL_ESTABLISH | INDICATION):
+		case (DL_ESTABLISH | CONFIRM):
+			FsmEvent(&chanp->fi, EV_BC_EST, NULL);
+			break;
+		case (DL_RELEASE | INDICATION):
+		case (DL_RELEASE | CONFIRM):
+			FsmEvent(&chanp->fi, EV_BC_REL, NULL);
+			break;
 		default:
-			printk(KERN_WARNING "lldata_handler unknown primitive %d\n",
+			printk(KERN_WARNING "lldata_handler unknown primitive %#x\n",
 			       pr);
 			break;
 	}
@@ -1835,22 +1284,24 @@
 	struct sk_buff *skb = arg;
 
 	switch (pr) {
-		case (PH_DATA_IND):
+		case (PH_DATA | INDICATION):
 			if (chanp->data_open)
 				chanp->cs->iif.rcvcallb_skb(chanp->cs->myid, chanp->chan, skb);
 			else {
-				if (chanp->lc_b->lcfi.state == ST_LC_DELAY)
-					FsmEvent(&chanp->lc_b->lcfi, EV_LC_DL_ESTABLISH, NULL);
-				if (chanp->data_open) {
-					link_debug(chanp, "channel now open", 0);
-					chanp->cs->iif.rcvcallb_skb(chanp->cs->myid,
-						chanp->chan, skb);
-				} else
-					dev_kfree_skb(skb);
+				link_debug(chanp, 0, "channel not open");
+				idev_kfree_skb(skb, FREE_READ);
 			}
 			break;
+		case (PH_ACTIVATE | INDICATION):
+		case (PH_ACTIVATE | CONFIRM):
+			FsmEvent(&chanp->fi, EV_BC_EST, NULL);
+			break;
+		case (PH_DEACTIVATE | INDICATION):
+		case (PH_DEACTIVATE | CONFIRM):
+			FsmEvent(&chanp->fi, EV_BC_REL, NULL);
+			break;
 		default:
-			printk(KERN_WARNING "lltrans_handler unknown primitive %d\n",
+			printk(KERN_WARNING "lltrans_handler unknown primitive %#x\n",
 			       pr);
 			break;
 	}
@@ -1874,10 +1325,25 @@
 {
 	struct PStack *st = chanp->b_st;
 	struct IsdnCardState *cs = chanp->cs;
-	char tmp[128];
+	char tmp[16];
 
 	st->l1.hardware = cs;
-	chanp->bcs->mode = 2;
+	if (chanp->leased)
+		st->l1.bc = chanp->chan & 1;
+	else
+		st->l1.bc = chanp->proc->para.bchannel - 1;
+	switch (chanp->l2_active_protocol) {
+		case (ISDN_PROTO_L2_X75I):
+		case (ISDN_PROTO_L2_HDLC):
+			st->l1.mode = L1_MODE_HDLC;
+			break;
+		case (ISDN_PROTO_L2_TRANS):
+			st->l1.mode = L1_MODE_TRANS;
+			break;
+		case (ISDN_PROTO_L2_MODEM):
+			st->l1.mode = L1_MODE_MODEM;
+			break;
+	}
 	if (chanp->bcs->BC_SetStack(st, chanp->bcs))
 		return (-1);
 	st->l2.flag = 0;
@@ -1892,50 +1358,89 @@
 	st->l3.debug = 0;
 	switch (chanp->l2_active_protocol) {
 		case (ISDN_PROTO_L2_X75I):
-			sprintf(tmp, "Channel %d x.75", chanp->chan);
+			sprintf(tmp, "Ch%d X.75", chanp->chan);
 			setstack_isdnl2(st, tmp);
+			setstack_l3bc(st, chanp);
 			st->l2.l2l3 = lldata_handler;
-			st->l1.l1man = bc_l1man;
-			st->l2.l2man = bc_l2man;
 			st->lli.userdata = chanp;
 			st->lli.l1writewakeup = NULL;
 			st->lli.l2writewakeup = ll_writewakeup;
 			st->l2.l2m.debug = chanp->debug & 16;
 			st->l2.debug = chanp->debug & 64;
-			st->ma.manl2(st, MDL_NOTEIPROC, NULL);
-			st->l1.mode = L1_MODE_HDLC;
-			if (chanp->leased)
-				st->l1.bc = chanp->chan & 1;
-			else
-				st->l1.bc = chanp->proc->para.bchannel - 1;
 			break;
 		case (ISDN_PROTO_L2_HDLC):
-			st->l1.l1l2 = lltrans_handler;
-			st->l1.l1man = bc_l1man;
-			st->lli.userdata = chanp;
-			st->lli.l1writewakeup = ll_writewakeup;
-			st->l1.mode = L1_MODE_HDLC;
-			if (chanp->leased)
-				st->l1.bc = chanp->chan & 1;
-			else
-				st->l1.bc = chanp->proc->para.bchannel - 1;
-			break;
 		case (ISDN_PROTO_L2_TRANS):
+		case (ISDN_PROTO_L2_MODEM):
 			st->l1.l1l2 = lltrans_handler;
-			st->l1.l1man = bc_l1man;
 			st->lli.userdata = chanp;
 			st->lli.l1writewakeup = ll_writewakeup;
-			st->l1.mode = L1_MODE_TRANS;
-			if (chanp->leased)
-				st->l1.bc = chanp->chan & 1;
-			else
-				st->l1.bc = chanp->proc->para.bchannel - 1;
+			setstack_transl2(st);
+			setstack_l3bc(st, chanp);
 			break;
 	}
+        test_and_set_bit(FLG_START_B, &chanp->Flags);
+ 
 	return (0);
 }
 
 static void
+leased_l4l3(struct PStack *st, int pr, void *arg)
+{
+	struct Channel *chanp = (struct Channel *) st->lli.userdata;
+	struct sk_buff *skb = arg;
+
+	switch (pr) {
+		case (DL_DATA | REQUEST):
+			link_debug(chanp, 0, "leased line d-channel DATA");
+			idev_kfree_skb(skb, FREE_READ);
+			break;
+		case (DL_ESTABLISH | REQUEST):
+			st->l2.l2l1(st, PH_ACTIVATE | REQUEST, NULL);
+			break;
+		case (DL_RELEASE | REQUEST):
+			break;
+		default:
+			printk(KERN_WARNING "transd_l4l3 unknown primitive %#x\n",
+			       pr);
+			break;
+	}
+}
+
+static void
+leased_l1l2(struct PStack *st, int pr, void *arg)
+{
+	struct Channel *chanp = (struct Channel *) st->lli.userdata;
+	struct sk_buff *skb = arg;
+	int i,event = EV_LEASED_REL;
+
+	switch (pr) {
+		case (PH_DATA | INDICATION):
+			link_debug(chanp, 0, "leased line d-channel DATA");
+			idev_kfree_skb(skb, FREE_READ);
+			break;
+		case (PH_ACTIVATE | INDICATION):
+		case (PH_ACTIVATE | CONFIRM):
+			event = EV_LEASED;
+		case (PH_DEACTIVATE | INDICATION):
+		case (PH_DEACTIVATE | CONFIRM):
+			if (test_bit(FLG_TWO_DCHAN, &chanp->cs->HW_Flags))
+				i = 1;
+			else
+				i = 0;
+			while (i < 2) {
+				FsmEvent(&chanp->fi, event, NULL);
+				chanp++;
+				i++;
+			}
+			break;
+		default:
+			printk(KERN_WARNING
+				"transd_l1l2 unknown primitive %#x\n", pr);
+			break;
+	}
+}
+
+static void
 channel_report(struct Channel *chanp)
 {
 }
@@ -1946,30 +1451,117 @@
 	int i;
 	struct Channel *chanp = csta->channel;
 
-	for (i = 0; i < 2; i++) {
+	for (i = 0; i < (2 + MAX_WAITING_CALLS) ; i++) {
 		chanp[i].debug = debugflags;
 		chanp[i].fi.debug = debugflags & 2;
 		chanp[i].d_st->l2.l2m.debug = debugflags & 8;
 		chanp[i].b_st->l2.l2m.debug = debugflags & 0x10;
 		chanp[i].d_st->l2.debug = debugflags & 0x20;
 		chanp[i].b_st->l2.debug = debugflags & 0x40;
-		chanp[i].lc_d->lcfi.debug = debugflags & 0x80;
-		chanp[i].lc_b->lcfi.debug = debugflags & 0x100;
+		chanp[i].d_st->l3.l3m.debug = debugflags & 0x80;
+		chanp[i].b_st->l3.l3m.debug = debugflags & 0x100;
 		chanp[i].b_st->ma.tei_m.debug = debugflags & 0x200;
 		chanp[i].b_st->ma.debug = debugflags & 0x200;
 		chanp[i].d_st->l1.l1m.debug = debugflags & 0x1000;
+		chanp[i].b_st->l1.l1m.debug = debugflags & 0x2000;
+	}
+	if (debugflags & 4)
+		csta->debug |= DEB_DLOG_HEX;
+	else
+		csta->debug &= ~DEB_DLOG_HEX;
+}
+
+static char tmpbuf[256];
+
+static void
+capi_debug(struct Channel *chanp, capi_msg *cm)
+{
+	char *t = tmpbuf;
+
+	t += sprintf(tmpbuf, "%d CAPIMSG", chanp->chan);
+	t += QuickHex(t, (u_char *)cm, (cm->Length>50)? 50: cm->Length);
+	t--;
+	*t= 0;
+	HiSax_putstatus(chanp->cs, "Ch", "%d CAPIMSG %s", chanp->chan, tmpbuf);
+}
+
+void
+lli_got_fac_req(struct Channel *chanp, capi_msg *cm) {
+	if ((cm->para[0] != 3) || (cm->para[1] != 0))
+		return;
+	if (cm->para[2]<3)
+		return;
+	if (cm->para[4] != 0)
+		return;
+	switch(cm->para[3]) {
+		case 4: /* Suspend */
+			if (cm->para[5]) {
+				strncpy(chanp->setup.phone, &cm->para[5], cm->para[5] +1);
+				FsmEvent(&chanp->fi, EV_SUSPEND, cm);
+			}
+			break;
+		case 5: /* Resume */
+			if (cm->para[5]) {
+				strncpy(chanp->setup.phone, &cm->para[5], cm->para[5] +1);
+				if (chanp->fi.state == ST_NULL) {
+					FsmEvent(&chanp->fi, EV_RESUME, cm);
+				} else {
+					FsmDelTimer(&chanp->dial_timer, 72);
+					FsmAddTimer(&chanp->dial_timer, 80, EV_RESUME, cm, 73);
+				}
+			}
+			break;
+	}
+}
+
+void
+lli_got_manufacturer(struct Channel *chanp, struct IsdnCardState *cs, capi_msg *cm) {
+	if ((cs->typ == ISDN_CTYPE_ELSA) || (cs->typ == ISDN_CTYPE_ELSA_PNP) ||
+		(cs->typ == ISDN_CTYPE_ELSA_PCI)) {
+		if (cs->hw.elsa.MFlag) {
+			cs->cardmsg(cs, CARD_AUX_IND, cm->para);
+		}
 	}
-	csta->dlogflag = debugflags & 4;
 }
 
+
+/***************************************************************/
+/* Limit the available number of channels for the current card */
+/***************************************************************/
+static int 
+set_channel_limit(struct IsdnCardState *cs, int chanmax)
+{ isdn_ctrl ic;
+  int i, ii;
+  
+  if ((chanmax < 0) || (chanmax > 2))
+    return(-EINVAL);
+  cs->chanlimit = 0;
+  for (ii = 0; ii < 2; ii++) {
+    ic.driver = cs->myid;
+    ic.command = ISDN_STAT_DISCH;
+    ic.arg = ii;
+    if (ii >= chanmax)
+      ic.parm.num[0] = 0; /* disabled */
+    else
+      ic.parm.num[0] = 1; /* enabled */
+    i = cs->iif.statcallb(&ic); 
+    if (i) return(-EINVAL);
+    if (ii < chanmax) 
+      cs->chanlimit++;
+  }
+  return(0);
+} /* set_channel_limit */
+
+
 int
 HiSax_command(isdn_ctrl * ic)
 {
 	struct IsdnCardState *csta = hisax_findcard(ic->driver);
+	struct PStack *st;
 	struct Channel *chanp;
-	char tmp[128];
 	int i;
-	unsigned int num;
+	u_int num;
+	u_long adr;
 
 	if (!csta) {
 		printk(KERN_ERR
@@ -1977,32 +1569,32 @@
 		       ic->command, ic->driver);
 		return -ENODEV;
 	}
+
 	switch (ic->command) {
 		case (ISDN_CMD_SETEAZ):
 			chanp = csta->channel + ic->arg;
-			if (chanp->debug & 1) {
-				sprintf(tmp, "SETEAZ card %d %s", csta->cardnr + 1,
-					ic->parm.num);
-				link_debug(chanp, tmp, 1);
-			}
 			break;
+
 		case (ISDN_CMD_SETL2):
 			chanp = csta->channel + (ic->arg & 0xff);
-			if (chanp->debug & 1) {
-				sprintf(tmp, "SETL2 card %d %ld", csta->cardnr + 1,
-					ic->arg >> 8);
-				link_debug(chanp, tmp, 1);
-			}
+			if (chanp->debug & 1)
+				link_debug(chanp, 1, "SETL2 card %d %ld",
+					csta->cardnr + 1, ic->arg >> 8);
 			chanp->l2_protocol = ic->arg >> 8;
 			break;
+		case (ISDN_CMD_SETL3):
+			chanp = csta->channel + (ic->arg & 0xff);
+			if (chanp->debug & 1)
+				link_debug(chanp, 1, "SETL3 card %d %ld",
+					csta->cardnr + 1, ic->arg >> 8);
+			chanp->l3_protocol = ic->arg >> 8;
+			break;
 		case (ISDN_CMD_DIAL):
 			chanp = csta->channel + (ic->arg & 0xff);
-			if (chanp->debug & 1) {
-				sprintf(tmp, "DIAL %s -> %s (%d,%d)",
+			if (chanp->debug & 1)
+				link_debug(chanp, 1, "DIAL %s -> %s (%d,%d)",
 					ic->parm.setup.eazmsn, ic->parm.setup.phone,
-				 ic->parm.setup.si1, ic->parm.setup.si2);
-				link_debug(chanp, tmp, 1);
-			}
+					ic->parm.setup.si1, ic->parm.setup.si2);
 			chanp->setup = ic->parm.setup;
 			if (!strcmp(chanp->setup.eazmsn, "0"))
 				chanp->setup.eazmsn[0] = '\0';
@@ -2018,57 +1610,54 @@
 		case (ISDN_CMD_ACCEPTB):
 			chanp = csta->channel + ic->arg;
 			if (chanp->debug & 1)
-				link_debug(chanp, "ACCEPTB", 1);
+				link_debug(chanp, 1, "ACCEPTB");
 			FsmEvent(&chanp->fi, EV_ACCEPTB, NULL);
 			break;
 		case (ISDN_CMD_ACCEPTD):
 			chanp = csta->channel + ic->arg;
 			if (chanp->debug & 1)
-				link_debug(chanp, "ACCEPTD", 1);
+				link_debug(chanp, 1, "ACCEPTD");
 			FsmEvent(&chanp->fi, EV_ACCEPTD, NULL);
 			break;
 		case (ISDN_CMD_HANGUP):
 			chanp = csta->channel + ic->arg;
 			if (chanp->debug & 1)
-				link_debug(chanp, "HANGUP", 1);
+				link_debug(chanp, 1, "HANGUP");
 			FsmEvent(&chanp->fi, EV_HANGUP, NULL);
 			break;
-		case (ISDN_CMD_SUSPEND):
+		case (CAPI_PUT_MESSAGE):
 			chanp = csta->channel + ic->arg;
-			if (chanp->debug & 1) {
-				sprintf(tmp, "SUSPEND %s", ic->parm.num);
-				link_debug(chanp, tmp, 1);
-			}
-			FsmEvent(&chanp->fi, EV_SUSPEND, ic);
-			break;
-		case (ISDN_CMD_RESUME):
-			chanp = csta->channel + ic->arg;
-			if (chanp->debug & 1) {
-				sprintf(tmp, "RESUME %s", ic->parm.num);
-				link_debug(chanp, tmp, 1);
+			if (chanp->debug & 1)
+				capi_debug(chanp, &ic->parm.cmsg);
+			if (ic->parm.cmsg.Length < 8)
+				break;
+			switch(ic->parm.cmsg.Command) {
+				case CAPI_FACILITY:
+					if (ic->parm.cmsg.Subcommand == CAPI_REQ)
+						lli_got_fac_req(chanp, &ic->parm.cmsg);
+					break;
+				case CAPI_MANUFACTURER:
+					if (ic->parm.cmsg.Subcommand == CAPI_REQ)
+						lli_got_manufacturer(chanp, csta, &ic->parm.cmsg);
+					break;
+				default:
+					break;
 			}
-			FsmEvent(&chanp->fi, EV_RESUME, ic);
 			break;
 		case (ISDN_CMD_LOCK):
 			HiSax_mod_inc_use_count();
 #ifdef MODULE
-			if (csta->channel[0].debug & 0x400) {
-				jiftime(tmp, jiffies);
-				i = strlen(tmp);
-				sprintf(tmp + i, "   LOCK modcnt %d\n", MOD_USE_COUNT);
-				HiSax_putstatus(csta, tmp);
-			}
+			if (csta->channel[0].debug & 0x400)
+				HiSax_putstatus(csta, "   LOCK ", "modcnt %lx",
+					MOD_USE_COUNT);
 #endif				/* MODULE */
 			break;
 		case (ISDN_CMD_UNLOCK):
 			HiSax_mod_dec_use_count();
 #ifdef MODULE
-			if (csta->channel[0].debug & 0x400) {
-				jiftime(tmp, jiffies);
-				i = strlen(tmp);
-				sprintf(tmp + i, " UNLOCK modcnt %d\n", MOD_USE_COUNT);
-				HiSax_putstatus(csta, tmp);
-			}
+			if (csta->channel[0].debug & 0x400)
+				HiSax_putstatus(csta, " UNLOCK ", "modcnt %lx",
+					MOD_USE_COUNT);
 #endif				/* MODULE */
 			break;
 		case (ISDN_CMD_IOCTL):
@@ -2081,19 +1670,19 @@
 				case (1):
 					num = *(unsigned int *) ic->parm.num;
 					distr_debug(csta, num);
-					sprintf(tmp, "debugging flags card %d set to %x\n",
+					printk(KERN_DEBUG "HiSax: debugging flags card %d set to %x\n",
 						csta->cardnr + 1, num);
-					HiSax_putstatus(csta, tmp);
-					printk(KERN_DEBUG "HiSax: %s", tmp);
+					HiSax_putstatus(csta, "debugging flags ",
+						"card %d set to %x", csta->cardnr + 1, num);
 					break;
 				case (2):
-					num = *(unsigned int *) ic->parm.num; 
-					csta->channel[0].lc_b->delay = num;
-					csta->channel[1].lc_b->delay = num;
-					sprintf(tmp, "delay card %d set to %d ms\n",
+					num = *(unsigned int *) ic->parm.num;
+					csta->channel[0].b_st->l1.delay = num;
+					csta->channel[1].b_st->l1.delay = num;
+					HiSax_putstatus(csta, "delay ", "card %d set to %d ms",
+						csta->cardnr + 1, num);
+					printk(KERN_DEBUG "HiSax: delay card %d set to %d ms\n",
 						csta->cardnr + 1, num);
-					HiSax_putstatus(csta, tmp);
-					printk(KERN_DEBUG "HiSax: %s", tmp);
 					break;
 				case (3):
 					for (i = 0; i < *(unsigned int *) ic->parm.num; i++)
@@ -2106,54 +1695,141 @@
 				case (5):	/* set card in leased mode */
 					num = *(unsigned int *) ic->parm.num;
 					if ((num <1) || (num > 2)) {
-						sprintf(tmp, "Set LEASED wrong channel %d\n",
+						HiSax_putstatus(csta, "Set LEASED ",
+							"wrong channel %d", num);
+						printk(KERN_WARNING "HiSax: Set LEASED wrong channel %d\n",
 							num);
-						HiSax_putstatus(csta, tmp);
-						printk(KERN_WARNING "HiSax: %s", tmp);
 					} else {
 						num--;
-						csta->channel[num].leased = 1;
-						csta->channel[num].lc_d->l2_establish = 0;
-						sprintf(tmp, "card %d channel %d set leased mode\n",
+						chanp = csta->channel +num;
+						chanp->leased = 1;
+						HiSax_putstatus(csta, "Card",
+							"%d channel %d set leased mode\n",
 							csta->cardnr + 1, num + 1);
-						HiSax_putstatus(csta, tmp);
-						FsmEvent(&csta->channel[num].lc_d->lcfi, EV_LC_ESTABLISH, NULL);
+						chanp->d_st->l1.l1l2 = leased_l1l2;
+						chanp->d_st->lli.l4l3 = leased_l4l3;
+						chanp->d_st->lli.l4l3(chanp->d_st,
+							DL_ESTABLISH | REQUEST, NULL);
 					}
 					break;
 				case (6):	/* set B-channel test loop */
 					num = *(unsigned int *) ic->parm.num;
 					if (csta->stlist)
-						csta->stlist->ma.manl1(csta->stlist,
-							PH_TESTLOOP_REQ, (void *) num);
+						csta->stlist->l2.l2l1(csta->stlist,
+							PH_TESTLOOP | REQUEST, (void *) (long)num);
+					break;
+				case (7):	/* set card in PTP mode */
+					num = *(unsigned int *) ic->parm.num;
+					if (test_bit(FLG_TWO_DCHAN, &csta->HW_Flags)) {
+						printk(KERN_ERR "HiSax PTP mode only with one TEI possible\n");
+					} else if (num) {
+						test_and_set_bit(FLG_PTP, &csta->channel[0].d_st->l2.flag);
+						test_and_set_bit(FLG_FIXED_TEI, &csta->channel[0].d_st->l2.flag);
+						csta->channel[0].d_st->l2.tei = 0;
+						HiSax_putstatus(csta, "set card ", "in PTP mode");
+						printk(KERN_DEBUG "HiSax: set card in PTP mode\n");
+						printk(KERN_INFO "LAYER2 WATCHING ESTABLISH\n");
+						csta->channel[0].d_st->lli.l4l3(csta->channel[0].d_st,
+							DL_ESTABLISH | REQUEST, NULL);
+					} else {
+						test_and_clear_bit(FLG_PTP, &csta->channel[0].d_st->l2.flag);
+						test_and_clear_bit(FLG_FIXED_TEI, &csta->channel[0].d_st->l2.flag);
+						HiSax_putstatus(csta, "set card ", "in PTMP mode");
+						printk(KERN_DEBUG "HiSax: set card in PTMP mode\n");
+					}
+					break;
+				case (8):	/* set card in FIXED TEI mode */
+					num = *(unsigned int *) ic->parm.num;
+					chanp = csta->channel + (num & 1);
+					num = num >>1;
+					test_and_set_bit(FLG_FIXED_TEI, &chanp->d_st->l2.flag);
+					chanp->d_st->l2.tei = num;
+					HiSax_putstatus(csta, "set card ", "in FIXED TEI (%d) mode", num);
+					printk(KERN_DEBUG "HiSax: set card in FIXED TEI (%d) mode\n",
+						num);
+					break;
+				case (9): /* load firmware */
+					memcpy(&adr, ic->parm.num, sizeof(ulong));
+					csta->cardmsg(csta, CARD_LOAD_FIRM,
+						(void *) adr);
 					break;
 #ifdef MODULE
 				case (55):
-					while ( MOD_USE_COUNT > 0)
-                                           MOD_DEC_USE_COUNT;
+					MOD_USE_COUNT = 0;
 					HiSax_mod_inc_use_count();
 					break;
 #endif				/* MODULE */
 				case (11):
+					num = csta->debug & DEB_DLOG_HEX;
 					csta->debug = *(unsigned int *) ic->parm.num;
-					sprintf(tmp, "l1 debugging flags card %d set to %x\n",
+					csta->debug |= num;
+					HiSax_putstatus(cards[0].cs, "l1 debugging ",
+						"flags card %d set to %x",
+						csta->cardnr + 1, csta->debug);
+					printk(KERN_DEBUG "HiSax: l1 debugging flags card %d set to %x\n",
 						csta->cardnr + 1, csta->debug);
-					HiSax_putstatus(cards[0].cs, tmp);
-					printk(KERN_DEBUG "HiSax: %s", tmp);
 					break;
 				case (13):
 					csta->channel[0].d_st->l3.debug = *(unsigned int *) ic->parm.num;
 					csta->channel[1].d_st->l3.debug = *(unsigned int *) ic->parm.num;
-					sprintf(tmp, "l3 debugging flags card %d set to %x\n",
+					HiSax_putstatus(cards[0].cs, "l3 debugging ",
+						"flags card %d set to %x\n", csta->cardnr + 1,
+						*(unsigned int *) ic->parm.num);
+					printk(KERN_DEBUG "HiSax: l3 debugging flags card %d set to %x\n",
 						csta->cardnr + 1, *(unsigned int *) ic->parm.num);
-					HiSax_putstatus(cards[0].cs, tmp);
-					printk(KERN_DEBUG "HiSax: %s", tmp);
 					break;
+			        case  (10):
+				        i = *(unsigned int *) ic->parm.num;
+					if ((i < 0) || (i > 2))
+					  return(-EINVAL); /* invalid number */
+					return(set_channel_limit(csta, i));
+				        break;
+			        case  (12):
+				        i = *(unsigned int *) ic->parm.num;
+#ifdef CONFIG_HISAX_HFC_PCI
+					if (csta->typ != ISDN_CTYPE_HFC_PCI)
+					  return(-EINVAL);
+					return(hfcpci_set_echo(csta,i));    
+#else
+					return(-EINVAL);    
+#endif
+				        break;
 				default:
 					printk(KERN_DEBUG "HiSax: invalid ioclt %d\n",
 					       (int) ic->arg);
 					return (-EINVAL);
 			}
 			break;
+		
+		case (ISDN_CMD_PROCEED):
+			chanp = csta->channel + ic->arg;
+			if (chanp->debug & 1)
+				link_debug(chanp, 1, "PROCEED");
+			FsmEvent(&chanp->fi, EV_PROCEED, NULL);
+			break;
+
+		case (ISDN_CMD_ALERT):
+			chanp = csta->channel + ic->arg;
+			if (chanp->debug & 1)
+				link_debug(chanp, 1, "ALERT");
+			FsmEvent(&chanp->fi, EV_ALERT, NULL);
+			break;
+
+		case (ISDN_CMD_REDIR):
+			chanp = csta->channel + ic->arg;
+			if (chanp->debug & 1)
+				link_debug(chanp, 1, "REDIR");
+			chanp->setup = ic->parm.setup;
+			FsmEvent(&chanp->fi, EV_REDIR, NULL);
+			break;
+
+		/* protocol specific io commands */
+	        case (ISDN_CMD_PROT_IO):
+			for (st = csta->stlist; st; st = st->next)
+				if (st->protocol == (ic->arg & 0xFF))
+					return(st->lli.l4l3_proto(st, ic));
+			return(-EINVAL);                     
+			break;
 		default:
 			break;
 	}
@@ -2170,7 +1846,6 @@
 	int len = skb->len;
 	unsigned long flags;
 	struct sk_buff *nskb;
-	char tmp[64];
 
 	if (!csta) {
 		printk(KERN_ERR
@@ -2180,13 +1855,13 @@
 	chanp = csta->channel + chan;
 	st = chanp->b_st;
 	if (!chanp->data_open) {
-		link_debug(chanp, "writebuf: channel not open", 1);
+		link_debug(chanp, 1, "writebuf: channel not open");
 		return -EIO;
 	}
 	if (len > MAX_DATA_SIZE) {
-		sprintf(tmp, "writebuf: packet too large (%d bytes)", len);
-		printk(KERN_WARNING "HiSax_%s !\n", tmp);
-		link_debug(chanp, tmp, 1);
+		link_debug(chanp, 1, "writebuf: packet too large (%d bytes)", len);
+		printk(KERN_WARNING "HiSax_writebuf: packet too large (%d bytes) !\n",
+			len);
 		return -EINVAL;
 	}
 	if (len) {
@@ -2194,25 +1869,24 @@
 			/* Must return 0 here, since this is not an error
 			 * but a temporary lack of resources.
 			 */
-			if (chanp->debug & 0x800) {
-				sprintf(tmp, "writebuf: no buffers for %d bytes", len);
-				link_debug(chanp, tmp, 1);
-			}
+			if (chanp->debug & 0x800)
+				link_debug(chanp, 1, "writebuf: no buffers for %d bytes", len);
 			return 0;
-		}
+		} else if (chanp->debug & 0x800)
+			link_debug(chanp, 1, "writebuf %d/%d/%d", len, chanp->bcs->tx_cnt,MAX_DATA_MEM);
 		save_flags(flags);
 		cli();
 		nskb = skb_clone(skb, GFP_ATOMIC);
 		if (nskb) {
 			if (!ack)
 				nskb->pkt_type = PACKET_NOACK;
-			if (chanp->lc_b->l2_establish)
-				st->l3.l3l2(st, DL_DATA, nskb);
+			if (chanp->l2_active_protocol == ISDN_PROTO_L2_X75I)
+				st->l3.l3l2(st, DL_DATA | REQUEST, nskb);
 			else {
 				chanp->bcs->tx_cnt += len;
-				st->l2.l2l1(st, PH_DATA_REQ, nskb);
+				st->l2.l2l1(st, PH_DATA | REQUEST, nskb);
 			}
-			dev_kfree_skb(skb);
+			idev_kfree_skb(skb, FREE_WRITE);
 		} else
 			len = 0;
 		restore_flags(flags);

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen (who was at: slshen@lbl.gov)