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

Next file: linux/drivers/isdn/hisax/config.c
Previous file: linux/drivers/isdn/hisax/buffers.c
Back to the patch index
Back to the overall index

diff -u --recursive --new-file v2.1.91/linux/drivers/isdn/hisax/callc.c linux/drivers/isdn/hisax/callc.c
@@ -1,4 +1,4 @@
-/* $Id: callc.c,v 1.30 1997/05/29 10:40:43 keil Exp $
+/* $Id: callc.c,v 2.13 1998/02/12 23:07:16 keil Exp $
 
  * Author       Karsten Keil (keil@temic-ech.spacenet.de)
  *              based on the teles driver from Jan den Ouden
@@ -7,96 +7,54 @@
  *              Fritz Elfert
  *
  * $Log: callc.c,v $
- * Revision 1.30  1997/05/29 10:40:43  keil
- * chanp->impair was uninitialised
+ * Revision 2.13  1998/02/12 23:07:16  keil
+ * change for 2.1.86 (removing FREE_READ/FREE_WRITE from [dev]_kfree_skb()
  *
- * Revision 1.29  1997/04/23 20:09:49  fritz
- * Removed tmp, used by removed debugging code.
+ * Revision 2.12  1998/02/09 10:55:54  keil
+ * New leased line mode
  *
- * Revision 1.28  1997/04/21 13:42:25  keil
- * Remove unneeded debug
+ * Revision 2.11  1998/02/02 13:35:19  keil
+ * config B-channel delay
  *
- * Revision 1.27  1997/04/16 14:21:01  keil
- * remove unused variable
+ * Revision 2.10  1997/11/06 17:09:15  keil
+ * New 2.1 init code
  *
- * Revision 1.26  1997/04/13 19:55:21  keil
- * Changes in debugging code
+ * Revision 2.9  1997/10/29 19:01:58  keil
+ * new LL interface
  *
- * Revision 1.25  1997/04/06 22:54:08  keil
- * Using SKB's
+ * Revision 2.8  1997/10/10 20:56:44  fritz
+ * New HL interface.
  *
- * Revision 1.24  1997/03/05 11:28:03  keil
- * fixed undefined l2tei procedure
- * a layer1 release delete now the drel timer
+ * Revision 2.7  1997/10/01 09:21:28  fritz
+ * Removed old compatibility stuff for 2.0.X kernels.
+ * From now on, this code is for 2.1.X ONLY!
+ * Old stuff is still in the separate branch.
  *
- * Revision 1.23  1997/03/04 23:07:42  keil
- * bugfix dial parameter
+ * Revision 2.6  1997/09/11 17:26:58  keil
+ * Open B-channel if here are incomming packets
  *
- * Revision 1.22  1997/02/27 13:51:55  keil
- * Reset B-channel (dlc) statemachine in every release
+ * Revision 2.5  1997/08/07 17:46:05  keil
+ * Fix Incomming Call without broadcast
  *
- * Revision 1.21  1997/02/19 09:24:27  keil
- * Bugfix: Hangup to LL if a ttyI rings
+ * Revision 2.4  1997/08/03 14:37:58  keil
+ * Activate Layer2 in PtP mode
  *
- * Revision 1.20  1997/02/17 00:32:47  keil
- * Bugfix: No Busy reported to LL
+ * Revision 2.3  1997/07/31 19:23:40  keil
+ * LAYER2_WATCHING for PtP
  *
- * Revision 1.19  1997/02/14 12:23:10  fritz
- * Added support for new insmod parameter handling.
+ * Revision 2.2  1997/07/31 11:48:18  keil
+ * experimental REJECT after ALERTING
  *
- * Revision 1.18  1997/02/11 01:36:58  keil
- * Changed setup-interface (incoming and outgoing), cause reporting
+ * Revision 2.1  1997/07/30 17:12:59  keil
+ * more changes for 'One TEI per card'
  *
- * Revision 1.17  1997/02/09 00:23:10  keil
- * new interface handling, one interface per card
- * some changes in debug and leased line mode
+ * Revision 2.0  1997/07/27 21:12:21  keil
+ * CRef based L3; new channel handling; many other stuff
  *
- * Revision 1.16  1997/01/27 23:17:03  keil
- * delete timers while unloading
+ * Revision 1.31  1997/06/26 11:09:23  keil
+ * New managment and minor changes
  *
- * Revision 1.15  1997/01/27 16:00:38  keil
- * D-channel shutdown delay; improved callback
- *
- * Revision 1.14  1997/01/21 22:16:39  keil
- * new statemachine; leased line support; cleanup for 2.0
- *
- * Revision 1.13  1996/12/08 19:51:17  keil
- * bugfixes from Pekka Sarnila
- *
- * Revision 1.12  1996/11/26 20:20:03  keil
- * fixed warning while compile
- *
- * Revision 1.11  1996/11/26 18:43:17  keil
- * change ioctl 555 --> 55 (555 didn't work)
- *
- * Revision 1.10  1996/11/26 18:06:07  keil
- * fixed missing break statement,ioctl 555 reset modcount
- *
- * Revision 1.9  1996/11/18 20:23:19  keil
- * log writebuf channel not open changed
- *
- * Revision 1.8  1996/11/06 17:43:17  keil
- * more changes for 2.1.X;block fixed ST_PRO_W
- *
- * Revision 1.7  1996/11/06 15:13:51  keil
- * typo 0x64 --->64 in debug code
- *
- * Revision 1.6  1996/11/05 19:40:33  keil
- * X.75 windowsize
- *
- * Revision 1.5  1996/10/30 10:11:06  keil
- * debugging LOCK changed;ST_REL_W EV_HANGUP added
- *
- * Revision 1.4  1996/10/27 22:20:16  keil
- * alerting bugfixes
- * no static b-channel<->channel mapping
- *
- * Revision 1.2  1996/10/16 21:29:45  keil
- * compile bug as "not module"
- * Callback with euro
- *
- * Revision 1.1  1996/10/13 20:04:50  keil
- * Initial revision
+ * old logs removed /KKe
  *
  */
 
@@ -104,53 +62,63 @@
 #include "hisax.h"
 
 #ifdef MODULE
-#if (LINUX_VERSION_CODE < 0x020111)
-extern long mod_use_count_;
-#define MOD_USE_COUNT mod_use_count_
-#else
 #define MOD_USE_COUNT ((&__this_module)->usecount)
-#endif
 #endif				/* MODULE */
 
-const char *l4_revision = "$Revision: 1.30 $";
+const char *lli_revision = "$Revision: 2.13 $";
 
 extern struct IsdnCard cards[];
 extern int nrcards;
 extern void HiSax_mod_dec_use_count(void);
 extern void HiSax_mod_inc_use_count(void);
 
-static int init_ds(struct Channel *chanp, int incoming);
-static void release_ds(struct Channel *chanp);
+static int init_b_st(struct Channel *chanp, int incoming);
+static void release_b_st(struct Channel *chanp);
 
 static struct Fsm callcfsm =
-{NULL, 0, 0};
+{NULL, 0, 0, NULL, NULL};
 static struct Fsm lcfsm =
-{NULL, 0, 0};
+{NULL, 0, 0, NULL, NULL};
 
 static int chancount = 0;
 
-/* Flags for remembering action done in l4 */
+/* experimental REJECT after ALERTING for CALLBACK to beat the 4s delay */ 
+#define ALERT_REJECT 1
 
-#define  FLG_START_D	0x0001
-#define  FLG_ESTAB_D	0x0002
-#define  FLG_CALL_SEND	0x0004
-#define  FLG_CALL_REC   0x0008
-#define  FLG_CALL_ALERT	0x0010
-#define  FLG_START_B	0x0020
-#define  FLG_CONNECT_B	0x0040
-#define  FLG_LL_DCONN	0x0080
-#define  FLG_LL_BCONN	0x0100
-#define  FLG_DISC_SEND	0x0200
-#define  FLG_DISC_REC	0x0400
-#define  FLG_REL_REC	0x0800
+/* 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 
+ * hisaxctrl <id> 2 <value>
+ * value is in milliseconds
+ */
+#define DEFAULT_B_DELAY	300
+
+/* Flags for remembering action done in lli */
 
-#define  SETBIT(flg, item)  flg |= item
-#define  RESBIT(flg, item)  flg &= (~item)
+#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 30000
+#define	DREL_TIMER_VALUE 10000
 
 /*
  * Find card with given driverId
@@ -162,9 +130,9 @@
 	int i;
 
 	for (i = 0; i < nrcards; i++)
-		if (cards[i].sp)
-			if (cards[i].sp->myid == driverid)
-				return (cards[i].sp);
+		if (cards[i].cs)
+			if (cards[i].cs->myid == driverid)
+				return (cards[i].cs);
 	return (struct IsdnCardState *) 0;
 }
 
@@ -176,7 +144,7 @@
 	jiftime(tm, jiffies);
 	sprintf(tmp, "%s Channel %d %s %s\n", tm, chanp->chan,
 		direction ? "LL->HL" : "HL->LL", s);
-	HiSax_putstatus(chanp->sp, tmp);
+	HiSax_putstatus(chanp->cs, tmp);
 }
 
 
@@ -233,7 +201,7 @@
 	EV_SETUP_CMPL_IND,	/* 10 */
 	EV_BC_EST,		/* 11 */
 	EV_WRITEBUF,		/* 12 */
-	EV_DATAIN,		/* 13 */
+	EV_ESTABLISH,		/* 13 */
 	EV_HANGUP,		/* 14 */
 	EV_BC_REL,		/* 15 */
 	EV_CINF,		/* 16 */
@@ -263,7 +231,7 @@
 	"EV_SETUP_CMPL_IND",
 	"EV_BC_EST",
 	"EV_WRITEBUF",
-	"EV_DATAIN",
+	"EV_ESTABLISH",
 	"EV_HANGUP",
 	"EV_BC_REL",
 	"EV_CINF",
@@ -283,7 +251,6 @@
 	ST_LC_ESTABLISH_WAIT,
 	ST_LC_CONNECTED,
 	ST_LC_FLUSH_WAIT,
-	ST_LC_FLUSH_DELAY,
 	ST_LC_RELEASE_WAIT,
 };
 
@@ -297,7 +264,6 @@
 	"ST_LC_ESTABLISH_WAIT",
 	"ST_LC_CONNECTED",
 	"ST_LC_FLUSH_WAIT",
-	"ST_LC_FLUSH_DELAY",
 	"ST_LC_RELEASE_WAIT",
 };
 
@@ -307,9 +273,7 @@
 	EV_LC_PH_DEACTIVATE,
 	EV_LC_DL_ESTABLISH,
 	EV_LC_TIMER,
-	EV_LC_DL_FLUSH,
 	EV_LC_DL_RELEASE,
-	EV_LC_FLUSH,
 	EV_LC_RELEASE,
 };
 
@@ -322,9 +286,7 @@
 	"EV_LC_PH_DEACTIVATE",
 	"EV_LC_DL_ESTABLISH",
 	"EV_LC_TIMER",
-	"EV_LC_DL_FLUSH",
 	"EV_LC_DL_RELEASE",
-	"EV_LC_FLUSH",
 	"EV_LC_RELEASE",
 };
 
@@ -332,788 +294,962 @@
 #define LC_B  1
 
 static inline void
-l4_deliver_cause(struct Channel *chanp)
+lli_deliver_cause(struct Channel *chanp)
 {
 	isdn_ctrl ic;
 
-	if (chanp->para.cause < 0)
+	if (chanp->proc->para.cause < 0)
 		return;
-	ic.driver = chanp->sp->myid;
+	ic.driver = chanp->cs->myid;
 	ic.command = ISDN_STAT_CAUSE;
 	ic.arg = chanp->chan;
-	if (chanp->sp->protocol == ISDN_PTYPE_EURO)
-		sprintf(ic.parm.num, "E%02X%02X", chanp->para.loc & 0x7f,
-			chanp->para.cause & 0x7f);
+	if (chanp->cs->protocol == ISDN_PTYPE_EURO)
+		sprintf(ic.parm.num, "E%02X%02X", chanp->proc->para.loc & 0x7f,
+			chanp->proc->para.cause & 0x7f);
 	else
-		sprintf(ic.parm.num, "%02X%02X", chanp->para.loc & 0x7f,
-			chanp->para.cause & 0x7f);
-	chanp->sp->iif.statcallb(&ic);
+		sprintf(ic.parm.num, "%02X%02X", chanp->proc->para.loc & 0x7f,
+			chanp->proc->para.cause & 0x7f);
+	chanp->cs->iif.statcallb(&ic);
+}
+
+static void
+lli_d_established(struct FsmInst *fi, int event, void *arg)
+{
+	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];
+
+		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);
+}
+
+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
-l4_prep_dialout(struct FsmInst *fi, int event, void *arg)
+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;
+	chanp->lc_b->l2_start = !0;
 	switch (chanp->l2_active_protocol) {
 		case (ISDN_PROTO_L2_X75I):
-			chanp->lc_b.l2_establish = !0;
+			chanp->lc_b->l2_establish = !0;
 			break;
 		case (ISDN_PROTO_L2_HDLC):
 		case (ISDN_PROTO_L2_TRANS):
-			chanp->lc_b.l2_establish = 0;
+			chanp->lc_b->l2_establish = 0;
 			break;
 		default:
-			printk(KERN_WARNING "l4_prep_dialout unknown protocol\n");
+			printk(KERN_WARNING "lli_prep_dialout unknown protocol\n");
 			break;
 	}
-	if (chanp->Flags & FLG_ESTAB_D) {
+	if (test_bit(FLG_ESTAB_D, &chanp->Flags)) {
 		FsmEvent(fi, EV_DLEST, NULL);
 	} else {
-		chanp->Flags = FLG_START_D;
+		chanp->Flags = 0;
+		test_and_set_bit(FLG_START_D, &chanp->Flags);
 		if (chanp->leased) {
-			chanp->lc_d.l2_establish = 0;
+			chanp->lc_d->l2_establish = 0;
 		}
-		FsmEvent(&chanp->lc_d.lcfi, EV_LC_ESTABLISH, NULL);
+		FsmEvent(&chanp->lc_d->lcfi, EV_LC_ESTABLISH, NULL);
 	}
 }
 
 static void
-l4_do_dialout(struct FsmInst *fi, int event, void *arg)
+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) {
-		chanp->para.bchannel = (chanp->chan & 1) + 1;
 		FsmEvent(&chanp->fi, EV_SETUP_CNF, NULL);
 	} else {
-		SETBIT(chanp->Flags, FLG_ESTAB_D);
-		chanp->para.callref = chanp->outcallref;
-		chanp->outcallref++;
-		if (chanp->outcallref == 128)
-			chanp->outcallref = 64;
-		chanp->is.l4.l4l3(&chanp->is, CC_SETUP_REQ, NULL);
-		SETBIT(chanp->Flags, FLG_CALL_SEND);
+		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);
 	}
 }
 
 static void
-l4_init_bchan_out(struct FsmInst *fi, int event, void *arg)
+lli_init_bchan_out(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 	isdn_ctrl ic;
 
 	FsmChangeState(fi, ST_WAIT_BCONN);
-	SETBIT(chanp->Flags, FLG_LL_DCONN);
+	test_and_set_bit(FLG_LL_DCONN, &chanp->Flags);
 	if (chanp->debug & 1)
 		link_debug(chanp, "STAT_DCONN", 0);
-	ic.driver = chanp->sp->myid;
+	ic.driver = chanp->cs->myid;
 	ic.command = ISDN_STAT_DCONN;
 	ic.arg = chanp->chan;
-	chanp->sp->iif.statcallb(&ic);
-	init_ds(chanp, 0);
-	SETBIT(chanp->Flags, FLG_START_B);
-	FsmEvent(&chanp->lc_b.lcfi, EV_LC_ESTABLISH, NULL);
+	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);
 }
 
 static void
-l4_go_active(struct FsmInst *fi, int event, void *arg)
+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;
-	SETBIT(chanp->Flags, FLG_CONNECT_B);
+	test_and_set_bit(FLG_CONNECT_B, &chanp->Flags);
 	if (chanp->debug & 1)
 		link_debug(chanp, "STAT_BCONN", 0);
-	SETBIT(chanp->Flags, FLG_LL_BCONN);
-	ic.driver = chanp->sp->myid;
+	test_and_set_bit(FLG_LL_BCONN, &chanp->Flags);
+	ic.driver = chanp->cs->myid;
 	ic.command = ISDN_STAT_BCONN;
 	ic.arg = chanp->chan;
-	chanp->sp->iif.statcallb(&ic);
+	chanp->cs->iif.statcallb(&ic);
+	chanp->cs->cardmsg(chanp->cs, MDL_INFO_CONN, (void *) chanp->chan);
 }
 
 /* incomming call */
 
 static void
-l4_start_dchan(struct FsmInst *fi, int event, void *arg)
+lli_start_dchan(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 
 	FsmChangeState(fi, ST_IN_WAIT_D);
 	FsmDelTimer(&chanp->drel_timer, 61);
-	if (chanp->Flags & FLG_ESTAB_D) {
+	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 {
-		chanp->Flags = FLG_START_D;
-		FsmEvent(&chanp->lc_d.lcfi, EV_LC_ESTABLISH, NULL);
-	}
+	} else if (!test_and_set_bit(FLG_START_D, &chanp->Flags))
+		FsmEvent(&chanp->lc_d->lcfi, EV_LC_ESTABLISH, NULL);
 }
 
 static void
-l4_deliver_call(struct FsmInst *fi, int event, void *arg)
+lli_deliver_call(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 	isdn_ctrl ic;
 	int ret;
 	char txt[32];
 
+	chanp->cs->cardmsg(chanp->cs, MDL_INFO_SETUP, (void *) chanp->chan);
 	/*
 	 * Report incoming calls only once to linklevel, use CallFlags
 	 * which is set to 3 with each broadcast message in isdnl1.c
 	 * and resetted if a interface  answered the STAT_ICALL.
 	 */
-	if ((chanp->sp) && (chanp->sp->CallFlags == 3)) {
+	if (1) { /* for only one TEI */
 		FsmChangeState(fi, ST_IN_WAIT_LL);
-		SETBIT(chanp->Flags, FLG_ESTAB_D);
-		SETBIT(chanp->Flags, FLG_CALL_REC);
+		test_and_set_bit(FLG_CALL_REC, &chanp->Flags);
 		if (chanp->debug & 1)
 			link_debug(chanp, "STAT_ICALL", 0);
-		ic.driver = chanp->sp->myid;
+		ic.driver = chanp->cs->myid;
 		ic.command = ISDN_STAT_ICALL;
 		ic.arg = chanp->chan;
 		/*
 		 * No need to return "unknown" for calls without OAD,
 		 * cause that's handled in linklevel now (replaced by '0')
 		 */
-		ic.parm.setup = chanp->para.setup;
-		ret = chanp->sp->iif.statcallb(&ic);
+		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 (ret)	/* if a interface knows this call, reset the CallFlag
-				   * to avoid a second Call report to the linklevel
-				 */
-			chanp->sp->CallFlags &= ~(chanp->chan + 1);
 		switch (ret) {
 			case 1:	/* OK, anybody likes this call */
-				FsmChangeState(fi, ST_IN_ALERT_SEND);
-				SETBIT(chanp->Flags, FLG_CALL_ALERT);
-				chanp->is.l4.l4l3(&chanp->is, CC_ALERTING_REQ, NULL);
+				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);
+				}
 				break;
 			case 2:	/* Rejecting Call */
-				RESBIT(chanp->Flags, FLG_CALL_REC);
+				test_and_clear_bit(FLG_CALL_REC, &chanp->Flags);
 				break;
 			case 0:	/* OK, nobody likes this call */
 			default:	/* statcallb problems */
-				chanp->is.l4.l4l3(&chanp->is, CC_IGNORE, NULL);
+				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);
-				chanp->Flags = FLG_ESTAB_D;
-				FsmAddTimer(&chanp->drel_timer, DREL_TIMER_VALUE, EV_SHUTDOWN_D, NULL, 61);
+#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->is.l4.l4l3(&chanp->is, CC_IGNORE, NULL);
+		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
+	}
+}
+
+static void
+lli_establish_d(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);
+}
+
+static void
+lli_do_action(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 = FLG_ESTAB_D;
-		FsmAddTimer(&chanp->drel_timer, DREL_TIMER_VALUE, EV_SHUTDOWN_D, NULL, 62);
+		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
 	}
 }
 
 static void
-l4_send_dconnect(struct FsmInst *fi, int event, void *arg)
+lli_send_dconnect(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 
 	FsmChangeState(fi, ST_IN_WAIT_CONN_ACK);
-	chanp->is.l4.l4l3(&chanp->is, CC_SETUP_RSP, NULL);
+	chanp->d_st->lli.l4l3(chanp->d_st, CC_SETUP_RSP, chanp->proc);
 }
 
 static void
-l4_init_bchan_in(struct FsmInst *fi, int event, void *arg)
+lli_init_bchan_in(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 	isdn_ctrl ic;
 
 	FsmChangeState(fi, ST_WAIT_BCONN);
-	SETBIT(chanp->Flags, FLG_LL_DCONN);
+	test_and_set_bit(FLG_LL_DCONN, &chanp->Flags);
 	if (chanp->debug & 1)
 		link_debug(chanp, "STAT_DCONN", 0);
-	ic.driver = chanp->sp->myid;
+	ic.driver = chanp->cs->myid;
 	ic.command = ISDN_STAT_DCONN;
 	ic.arg = chanp->chan;
-	chanp->sp->iif.statcallb(&ic);
+	chanp->cs->iif.statcallb(&ic);
 	chanp->l2_active_protocol = chanp->l2_protocol;
 	chanp->incoming = !0;
-	chanp->lc_b.l2_start = 0;
+	chanp->lc_b->l2_start = 0;
 	switch (chanp->l2_active_protocol) {
 		case (ISDN_PROTO_L2_X75I):
-			chanp->lc_b.l2_establish = !0;
+			chanp->lc_b->l2_establish = !0;
 			break;
 		case (ISDN_PROTO_L2_HDLC):
 		case (ISDN_PROTO_L2_TRANS):
-			chanp->lc_b.l2_establish = 0;
+			chanp->lc_b->l2_establish = 0;
 			break;
 		default:
-			printk(KERN_WARNING "r9 unknown protocol\n");
+			printk(KERN_WARNING "bchannel unknown protocol\n");
 			break;
 	}
-	init_ds(chanp, !0);
-	SETBIT(chanp->Flags, FLG_START_B);
-	FsmEvent(&chanp->lc_b.lcfi, EV_LC_ESTABLISH, NULL);
+	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
-l4_reject_call(struct FsmInst *fi, int event, void *arg)
-{
-	struct Channel *chanp = fi->userdata;
-
-	FsmChangeState(fi, ST_WAIT_DRELEASE);
-	chanp->para.cause = 0x15;	/* Call Rejected */
-	chanp->is.l4.l4l3(&chanp->is, CC_REJECT_REQ, NULL);
-	SETBIT(chanp->Flags, FLG_DISC_SEND);
-}
-
-static void
-l4_cancel_call(struct FsmInst *fi, int event, void *arg)
+lli_cancel_call(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 	isdn_ctrl ic;
 
 	FsmChangeState(fi, ST_WAIT_DRELEASE);
-	if (chanp->Flags & FLG_LL_BCONN) {
+	if (test_and_clear_bit(FLG_LL_BCONN, &chanp->Flags)) {
 		if (chanp->debug & 1)
 			link_debug(chanp, "STAT_BHUP", 0);
-		RESBIT(chanp->Flags, FLG_LL_BCONN);
-		ic.driver = chanp->sp->myid;
+		ic.driver = chanp->cs->myid;
 		ic.command = ISDN_STAT_BHUP;
 		ic.arg = chanp->chan;
-		chanp->sp->iif.statcallb(&ic);
-	}
-	if (chanp->Flags & FLG_START_B) {
-		release_ds(chanp);
-		RESBIT(chanp->Flags, FLG_START_B);
+		chanp->cs->iif.statcallb(&ic);
 	}
-	chanp->para.cause = 0x10;	/* Normal Call Clearing */
-	chanp->is.l4.l4l3(&chanp->is, CC_DISCONNECT_REQ, NULL);
-	SETBIT(chanp->Flags, FLG_DISC_SEND);
+	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);
 }
 
 static void
-l4_shutdown_d(struct FsmInst *fi, int event, void *arg)
+lli_shutdown_d(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 
-	FsmChangeState(fi, ST_WAIT_DSHUTDOWN);
 	FsmDelTimer(&chanp->drel_timer, 62);
-	RESBIT(chanp->Flags, FLG_ESTAB_D);
-	FsmEvent(&chanp->lc_d.lcfi, EV_LC_RELEASE, NULL);
+#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);
+#endif
 }
 
 static void
-l4_timeout_d(struct FsmInst *fi, int event, void *arg)
+lli_timeout_d(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 	isdn_ctrl ic;
 
-	if (chanp->Flags & FLG_LL_DCONN) {
+	if (test_and_clear_bit(FLG_LL_DCONN, &chanp->Flags)) {
 		if (chanp->debug & 1)
 			link_debug(chanp, "STAT_DHUP", 0);
-		RESBIT(chanp->Flags, FLG_LL_DCONN);
-		l4_deliver_cause(chanp);
-		ic.driver = chanp->sp->myid;
+		lli_deliver_cause(chanp);
+		ic.driver = chanp->cs->myid;
 		ic.command = ISDN_STAT_DHUP;
 		ic.arg = chanp->chan;
-		chanp->sp->iif.statcallb(&ic);
+		chanp->cs->iif.statcallb(&ic);
 	}
 	FsmChangeState(fi, ST_NULL);
-	chanp->Flags = FLG_ESTAB_D;
+	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);
 }
 
 static void
-l4_go_null(struct FsmInst *fi, int event, void *arg)
+lli_go_null(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);
 }
 
 static void
-l4_disconn_bchan(struct FsmInst *fi, int event, void *arg)
+lli_disconn_bchan(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 
 	chanp->data_open = 0;
 	FsmChangeState(fi, ST_WAIT_BRELEASE);
-	RESBIT(chanp->Flags, FLG_CONNECT_B);
-	FsmEvent(&chanp->lc_b.lcfi, EV_LC_RELEASE, NULL);
+	test_and_clear_bit(FLG_CONNECT_B, &chanp->Flags);
+	FsmEvent(&chanp->lc_b->lcfi, EV_LC_RELEASE, NULL);
 }
 
 static void
-l4_send_d_disc(struct FsmInst *fi, int event, void *arg)
+lli_send_d_disc(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 	isdn_ctrl ic;
 
-
-	if (chanp->Flags & (FLG_DISC_REC | FLG_REL_REC))
+	if (test_bit(FLG_DISC_REC, &chanp->Flags) ||
+		test_bit(FLG_REL_REC, &chanp->Flags))
 		return;
 	FsmChangeState(fi, ST_WAIT_DRELEASE);
-	if (chanp->Flags & FLG_LL_BCONN) {
+	if (test_and_clear_bit(FLG_LL_BCONN, &chanp->Flags)) {
 		if (chanp->debug & 1)
 			link_debug(chanp, "STAT_BHUP", 0);
-		RESBIT(chanp->Flags, FLG_LL_BCONN);
-		ic.driver = chanp->sp->myid;
+		ic.driver = chanp->cs->myid;
 		ic.command = ISDN_STAT_BHUP;
 		ic.arg = chanp->chan;
-		chanp->sp->iif.statcallb(&ic);
+		chanp->cs->iif.statcallb(&ic);
 	}
-	if (chanp->Flags & FLG_START_B) {
-		release_ds(chanp);
-		RESBIT(chanp->Flags, FLG_START_B);
+	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->para.cause = 0x10;	/* Normal Call Clearing */
-	chanp->is.l4.l4l3(&chanp->is, CC_DISCONNECT_REQ, NULL);
-	SETBIT(chanp->Flags, FLG_DISC_SEND);
 }
 
 static void
-l4_released_bchan(struct FsmInst *fi, int event, void *arg)
+lli_released_bchan(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 	isdn_ctrl ic;
 
 	FsmChangeState(fi, ST_WAIT_DCOMMAND);
 	chanp->data_open = 0;
-	if (chanp->Flags & FLG_LL_BCONN) {
+	if (test_and_clear_bit(FLG_LL_BCONN, &chanp->Flags)) {
 		if (chanp->debug & 1)
 			link_debug(chanp, "STAT_BHUP", 0);
-		RESBIT(chanp->Flags, FLG_LL_BCONN);
-		ic.driver = chanp->sp->myid;
+		ic.driver = chanp->cs->myid;
 		ic.command = ISDN_STAT_BHUP;
 		ic.arg = chanp->chan;
-		chanp->sp->iif.statcallb(&ic);
+		chanp->cs->iif.statcallb(&ic);
 	}
-	release_ds(chanp);
-	RESBIT(chanp->Flags, FLG_START_B);
+	release_b_st(chanp);
+	test_and_clear_bit(FLG_START_B, &chanp->Flags);
 }
 
 
 static void
-l4_release_bchan(struct FsmInst *fi, int event, void *arg)
+lli_release_bchan(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 
 	chanp->data_open = 0;
-	SETBIT(chanp->Flags, FLG_DISC_REC);
+	test_and_set_bit(FLG_DISC_REC, &chanp->Flags);
 	FsmChangeState(fi, ST_WAIT_BREL_DISC);
-	RESBIT(chanp->Flags, FLG_CONNECT_B);
-	FsmEvent(&chanp->lc_b.lcfi, EV_LC_RELEASE, NULL);
+	test_and_clear_bit(FLG_CONNECT_B, &chanp->Flags);
+	FsmEvent(&chanp->lc_b->lcfi, EV_LC_RELEASE, NULL);
 }
 
 static void
-l4_received_d_rel(struct FsmInst *fi, int event, void *arg)
+lli_received_d_rel(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 	isdn_ctrl ic;
 
 	chanp->data_open = 0;
-	FsmChangeState(fi, ST_WAIT_DSHUTDOWN);
-	SETBIT(chanp->Flags, FLG_REL_REC);
-	if (chanp->Flags & FLG_CONNECT_B) {
-		chanp->lc_b.l2_establish = 0;	/* direct reset in lc_b.lcfi */
-		FsmEvent(&chanp->lc_b.lcfi, EV_LC_RELEASE, NULL);
-		RESBIT(chanp->Flags, FLG_CONNECT_B);
+	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 (chanp->Flags & FLG_LL_BCONN) {
+	if (test_and_clear_bit(FLG_LL_BCONN, &chanp->Flags)) {
 		if (chanp->debug & 1)
 			link_debug(chanp, "STAT_BHUP", 0);
-		ic.driver = chanp->sp->myid;
+		ic.driver = chanp->cs->myid;
 		ic.command = ISDN_STAT_BHUP;
 		ic.arg = chanp->chan;
-		chanp->sp->iif.statcallb(&ic);
-		RESBIT(chanp->Flags, FLG_LL_BCONN);
+		chanp->cs->iif.statcallb(&ic);
 	}
-	if (chanp->Flags & FLG_START_B) {
-		release_ds(chanp);
-		RESBIT(chanp->Flags, FLG_START_B);
-	}
-	if (chanp->Flags & (FLG_LL_DCONN | FLG_CALL_SEND | FLG_CALL_ALERT)) {
+	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);
-		l4_deliver_cause(chanp);
-		ic.driver = chanp->sp->myid;
+		lli_deliver_cause(chanp);
+		ic.driver = chanp->cs->myid;
 		ic.command = ISDN_STAT_DHUP;
 		ic.arg = chanp->chan;
-		chanp->sp->iif.statcallb(&ic);
+		chanp->cs->iif.statcallb(&ic);
 	}
-	FsmEvent(&chanp->lc_d.lcfi, EV_LC_FLUSH, NULL);
-	RESBIT(chanp->Flags, FLG_ESTAB_D);
-	RESBIT(chanp->Flags, FLG_DISC_SEND);
-	RESBIT(chanp->Flags, FLG_CALL_REC);
-	RESBIT(chanp->Flags, FLG_CALL_ALERT);
-	RESBIT(chanp->Flags, FLG_LL_DCONN);
-	RESBIT(chanp->Flags, FLG_CALL_SEND);
+	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);
 }
 
 static void
-l4_received_d_relcnf(struct FsmInst *fi, int event, void *arg)
+lli_received_d_relcnf(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 	isdn_ctrl ic;
 
 	chanp->data_open = 0;
-	FsmChangeState(fi, ST_WAIT_DSHUTDOWN);
-	if (chanp->Flags & FLG_CONNECT_B) {
-		chanp->lc_b.l2_establish = 0;	/* direct reset in lc_b.lcfi */
-		FsmEvent(&chanp->lc_b.lcfi, EV_LC_RELEASE, NULL);
-		RESBIT(chanp->Flags, FLG_CONNECT_B);
+	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 (chanp->Flags & FLG_LL_BCONN) {
+	if (test_and_clear_bit(FLG_LL_BCONN, &chanp->Flags)) {
 		if (chanp->debug & 1)
 			link_debug(chanp, "STAT_BHUP", 0);
-		ic.driver = chanp->sp->myid;
+		ic.driver = chanp->cs->myid;
 		ic.command = ISDN_STAT_BHUP;
 		ic.arg = chanp->chan;
-		chanp->sp->iif.statcallb(&ic);
-		RESBIT(chanp->Flags, FLG_LL_BCONN);
-	}
-	if (chanp->Flags & FLG_START_B) {
-		release_ds(chanp);
-		RESBIT(chanp->Flags, FLG_START_B);
+		chanp->cs->iif.statcallb(&ic);
 	}
-	if (chanp->Flags & (FLG_LL_DCONN | FLG_CALL_SEND | FLG_CALL_ALERT)) {
+	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);
-		l4_deliver_cause(chanp);
-		ic.driver = chanp->sp->myid;
+		lli_deliver_cause(chanp);
+		ic.driver = chanp->cs->myid;
 		ic.command = ISDN_STAT_DHUP;
 		ic.arg = chanp->chan;
-		chanp->sp->iif.statcallb(&ic);
+		chanp->cs->iif.statcallb(&ic);
 	}
-	FsmEvent(&chanp->lc_d.lcfi, EV_LC_RELEASE, NULL);
-	RESBIT(chanp->Flags, FLG_ESTAB_D);
-	RESBIT(chanp->Flags, FLG_DISC_SEND);
-	RESBIT(chanp->Flags, FLG_CALL_REC);
-	RESBIT(chanp->Flags, FLG_CALL_ALERT);
-	RESBIT(chanp->Flags, FLG_LL_DCONN);
-	RESBIT(chanp->Flags, FLG_CALL_SEND);
+	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);
 }
 
 static void
-l4_received_d_disc(struct FsmInst *fi, int event, void *arg)
+lli_received_d_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);
-	SETBIT(chanp->Flags, FLG_DISC_REC);
-	if (chanp->Flags & FLG_LL_BCONN) {
+	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->sp->myid;
+		ic.driver = chanp->cs->myid;
 		ic.command = ISDN_STAT_BHUP;
 		ic.arg = chanp->chan;
-		chanp->sp->iif.statcallb(&ic);
-		RESBIT(chanp->Flags, FLG_LL_BCONN);
+		chanp->cs->iif.statcallb(&ic);
 	}
-	if (chanp->Flags & FLG_START_B) {
-		release_ds(chanp);
-		RESBIT(chanp->Flags, FLG_START_B);
-	}
-	if (chanp->Flags & (FLG_LL_DCONN | FLG_CALL_SEND | FLG_CALL_ALERT)) {
+	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);
-		l4_deliver_cause(chanp);
-		ic.driver = chanp->sp->myid;
+		lli_deliver_cause(chanp);
+		ic.driver = chanp->cs->myid;
 		ic.command = ISDN_STAT_DHUP;
 		ic.arg = chanp->chan;
-		chanp->sp->iif.statcallb(&ic);
+		chanp->cs->iif.statcallb(&ic);
 	}
-	RESBIT(chanp->Flags, FLG_LL_DCONN);
-	RESBIT(chanp->Flags, FLG_CALL_SEND);
-	RESBIT(chanp->Flags, FLG_CALL_ALERT);
-	chanp->is.l4.l4l3(&chanp->is, CC_RELEASE_REQ, NULL);
+	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
-l4_charge_info(struct FsmInst *fi, int event, void *arg)
+lli_charge_info(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 	isdn_ctrl ic;
 
-	ic.driver = chanp->sp->myid;
+	ic.driver = chanp->cs->myid;
 	ic.command = ISDN_STAT_CINF;
 	ic.arg = chanp->chan;
-	sprintf(ic.parm.num, "%d", chanp->para.chargeinfo);
-	chanp->sp->iif.statcallb(&ic);
+	sprintf(ic.parm.num, "%d", chanp->proc->para.chargeinfo);
+	chanp->cs->iif.statcallb(&ic);
 }
 
 /* error procedures */
 
 static void
-l4_no_dchan(struct FsmInst *fi, int event, void *arg)
+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->sp->myid;
+	ic.driver = chanp->cs->myid;
 	ic.command = ISDN_STAT_NODCH;
 	ic.arg = chanp->chan;
-	chanp->sp->iif.statcallb(&ic);
+	chanp->cs->iif.statcallb(&ic);
 	chanp->Flags = 0;
 	FsmChangeState(fi, ST_NULL);
-	FsmEvent(&chanp->lc_d.lcfi, EV_LC_RELEASE, NULL);
+	FsmEvent(&chanp->lc_d->lcfi, EV_LC_RELEASE, NULL);
 }
 
 static void
-l4_no_dchan_ready(struct FsmInst *fi, int event, void *arg)
+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->sp->myid;
+	ic.driver = chanp->cs->myid;
 	ic.command = ISDN_STAT_DHUP;
 	ic.arg = chanp->chan;
-	chanp->sp->iif.statcallb(&ic);
+	chanp->cs->iif.statcallb(&ic);
 }
 
 static void
-l4_no_dchan_in(struct FsmInst *fi, int event, void *arg)
+lli_no_dchan_in(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
+	isdn_ctrl ic;
 
-	chanp->is.l4.l4l3(&chanp->is, CC_DLRL, NULL);
+	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);
+	FsmEvent(&chanp->lc_d->lcfi, EV_LC_RELEASE, NULL);
 }
 
 static void
-l4_no_setup_rsp(struct FsmInst *fi, int event, void *arg)
+lli_no_setup_rsp(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 	isdn_ctrl ic;
 
-	chanp->Flags = 0;
 	FsmChangeState(fi, ST_NULL);
-	FsmEvent(&chanp->lc_d.lcfi, EV_LC_RELEASE, NULL);
+	test_and_clear_bit(FLG_CALL_SEND, &chanp->Flags);
 	if (chanp->debug & 1)
 		link_debug(chanp, "STAT_DHUP", 0);
-	ic.driver = chanp->sp->myid;
+	ic.driver = chanp->cs->myid;
 	ic.command = ISDN_STAT_DHUP;
 	ic.arg = chanp->chan;
-	chanp->sp->iif.statcallb(&ic);
+	chanp->cs->iif.statcallb(&ic);
+	lli_shutdown_d(fi, event, arg);
 }
 
 static void
-l4_setup_err(struct FsmInst *fi, int event, void *arg)
+lli_setup_err(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 	isdn_ctrl ic;
 
 	FsmChangeState(fi, ST_WAIT_DRELEASE);
-	if (chanp->Flags & FLG_LL_DCONN) {
+	if (test_and_clear_bit(FLG_LL_DCONN, &chanp->Flags)) {
 		if (chanp->debug & 1)
 			link_debug(chanp, "STAT_DHUP", 0);
-		RESBIT(chanp->Flags, FLG_LL_DCONN);
-		l4_deliver_cause(chanp);
-		ic.driver = chanp->sp->myid;
+		lli_deliver_cause(chanp);
+		ic.driver = chanp->cs->myid;
 		ic.command = ISDN_STAT_DHUP;
 		ic.arg = chanp->chan;
-		chanp->sp->iif.statcallb(&ic);
+		chanp->cs->iif.statcallb(&ic);
 	}
-	SETBIT(chanp->Flags, FLG_DISC_SEND);	/* DISCONN was sent from L3 */
+	test_and_set_bit(FLG_DISC_SEND, &chanp->Flags);	/* DISCONN was sent from L3 */
 }
 
 static void
-l4_connect_err(struct FsmInst *fi, int event, void *arg)
+lli_connect_err(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 	isdn_ctrl ic;
 
 	FsmChangeState(fi, ST_WAIT_DRELEASE);
-	if (chanp->Flags & FLG_LL_DCONN) {
+	if (test_and_clear_bit(FLG_LL_DCONN, &chanp->Flags)) {
 		if (chanp->debug & 1)
 			link_debug(chanp, "STAT_DHUP", 0);
-		RESBIT(chanp->Flags, FLG_LL_DCONN);
-		l4_deliver_cause(chanp);
-		ic.driver = chanp->sp->myid;
+		lli_deliver_cause(chanp);
+		ic.driver = chanp->cs->myid;
 		ic.command = ISDN_STAT_DHUP;
 		ic.arg = chanp->chan;
-		chanp->sp->iif.statcallb(&ic);
+		chanp->cs->iif.statcallb(&ic);
 	}
-	SETBIT(chanp->Flags, FLG_DISC_SEND);	/* DISCONN was sent from L3 */
+	test_and_set_bit(FLG_DISC_SEND, &chanp->Flags);	/* DISCONN was sent from L3 */
 }
 
 static void
-l4_active_dlrl(struct FsmInst *fi, int event, void *arg)
+lli_got_dlrl(struct FsmInst *fi, int event, void *arg)
 {
 	struct Channel *chanp = fi->userdata;
 	isdn_ctrl ic;
 
 	chanp->data_open = 0;
 	FsmChangeState(fi, ST_NULL);
-	if (chanp->Flags & FLG_CONNECT_B) {
-		chanp->lc_b.l2_establish = 0;	/* direct reset in lc_b.lcfi */
-		FsmEvent(&chanp->lc_b.lcfi, EV_LC_RELEASE, NULL);
-		RESBIT(chanp->Flags, FLG_CONNECT_B);
+	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 (chanp->Flags & FLG_LL_BCONN) {
+	if (test_and_clear_bit(FLG_LL_BCONN, &chanp->Flags)) {
 		if (chanp->debug & 1)
 			link_debug(chanp, "STAT_BHUP", 0);
-		ic.driver = chanp->sp->myid;
+		ic.driver = chanp->cs->myid;
 		ic.command = ISDN_STAT_BHUP;
 		ic.arg = chanp->chan;
-		chanp->sp->iif.statcallb(&ic);
-		RESBIT(chanp->Flags, FLG_LL_BCONN);
+		chanp->cs->iif.statcallb(&ic);
 	}
-	if (chanp->Flags & FLG_START_B) {
-		release_ds(chanp);
-		RESBIT(chanp->Flags, FLG_START_B);
-	}
-	if (chanp->Flags & FLG_LL_DCONN) {
-		if (chanp->debug & 1)
-			link_debug(chanp, "STAT_DHUP", 0);
-		RESBIT(chanp->Flags, FLG_LL_DCONN);
-		if (chanp->sp->protocol == ISDN_PTYPE_EURO) {
-			chanp->para.cause = 0x2f;
-			chanp->para.loc = 0;
-		} else {
-			chanp->para.cause = 0x70;
-			chanp->para.loc = 0;
-		}
-		l4_deliver_cause(chanp);
-		ic.driver = chanp->sp->myid;
+	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->sp->iif.statcallb(&ic);
+		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->Flags = 0;
-	chanp->is.l4.l4l3(&chanp->is, CC_DLRL, NULL);
-	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[] =
+static struct FsmNode fnlist[] HISAX_INITDATA =
 {
-	{ST_NULL,		EV_DIAL,		l4_prep_dialout},
-	{ST_NULL,		EV_SETUP_IND,		l4_start_dchan},
-	{ST_NULL,		EV_SHUTDOWN_D,		l4_shutdown_d},
-	{ST_NULL,		EV_DLRL,		l4_go_null},
-	{ST_OUT_WAIT_D,		EV_DLEST,		l4_do_dialout},
-	{ST_OUT_WAIT_D,		EV_DLRL,		l4_no_dchan},
-	{ST_OUT_WAIT_D,		EV_HANGUP,		l4_no_dchan},
-	{ST_IN_WAIT_D,		EV_DLEST,		l4_deliver_call},
-	{ST_IN_WAIT_D,		EV_DLRL,		l4_no_dchan_in},
-	{ST_IN_WAIT_D,		EV_HANGUP,		l4_no_dchan_in},
-	{ST_OUT_DIAL,		EV_SETUP_CNF,		l4_init_bchan_out},
-	{ST_OUT_DIAL,		EV_HANGUP,		l4_cancel_call},
-	{ST_OUT_DIAL,		EV_DISCONNECT_IND,	l4_received_d_disc},
-	{ST_OUT_DIAL,		EV_RELEASE_IND,		l4_received_d_rel},
-	{ST_OUT_DIAL,		EV_RELEASE_CNF,		l4_received_d_relcnf},
-	{ST_OUT_DIAL,		EV_NOSETUP_RSP,		l4_no_setup_rsp},
-	{ST_OUT_DIAL,		EV_SETUP_ERR,		l4_setup_err},
-	{ST_IN_WAIT_LL,		EV_SETUP_CMPL_IND,	l4_init_bchan_in},
-	{ST_IN_WAIT_LL,		EV_ACCEPTD,		l4_send_dconnect},
-	{ST_IN_WAIT_LL,		EV_HANGUP,		l4_reject_call},
-	{ST_IN_WAIT_LL,		EV_DISCONNECT_IND,	l4_received_d_disc},
-	{ST_IN_WAIT_LL,		EV_RELEASE_IND,		l4_received_d_rel},
-	{ST_IN_WAIT_LL,		EV_RELEASE_CNF,		l4_received_d_relcnf},
-	{ST_IN_ALERT_SEND,	EV_SETUP_CMPL_IND,	l4_init_bchan_in},
-	{ST_IN_ALERT_SEND,	EV_ACCEPTD,		l4_send_dconnect},
-	{ST_IN_ALERT_SEND,	EV_HANGUP,		l4_send_d_disc},
-	{ST_IN_ALERT_SEND,	EV_DISCONNECT_IND,	l4_received_d_disc},
-	{ST_IN_ALERT_SEND,	EV_RELEASE_IND,		l4_received_d_rel},
-	{ST_IN_ALERT_SEND,	EV_RELEASE_CNF,		l4_received_d_relcnf},
-	{ST_IN_WAIT_CONN_ACK,	EV_SETUP_CMPL_IND,	l4_init_bchan_in},
-	{ST_IN_WAIT_CONN_ACK,	EV_HANGUP,		l4_send_d_disc},
-	{ST_IN_WAIT_CONN_ACK,	EV_DISCONNECT_IND,	l4_received_d_disc},
-	{ST_IN_WAIT_CONN_ACK,	EV_RELEASE_IND,		l4_received_d_rel},
-	{ST_IN_WAIT_CONN_ACK,	EV_RELEASE_CNF,		l4_received_d_relcnf},
-	{ST_IN_WAIT_CONN_ACK,	EV_CONNECT_ERR,		l4_connect_err},
-	{ST_WAIT_BCONN,		EV_BC_EST,		l4_go_active},
-	{ST_WAIT_BCONN,		EV_BC_REL,		l4_send_d_disc},
-	{ST_WAIT_BCONN,		EV_HANGUP,		l4_send_d_disc},
-	{ST_WAIT_BCONN,		EV_DISCONNECT_IND,	l4_received_d_disc},
-	{ST_WAIT_BCONN,		EV_RELEASE_IND,		l4_received_d_rel},
-	{ST_WAIT_BCONN,		EV_RELEASE_CNF,		l4_received_d_relcnf},
-	{ST_ACTIVE,		EV_CINF,		l4_charge_info},
-	{ST_ACTIVE,		EV_BC_REL,		l4_released_bchan},
-	{ST_ACTIVE,		EV_HANGUP,		l4_disconn_bchan},
-	{ST_ACTIVE,		EV_DISCONNECT_IND,	l4_release_bchan},
-	{ST_ACTIVE,		EV_RELEASE_CNF,		l4_received_d_relcnf},
-	{ST_ACTIVE,		EV_RELEASE_IND,		l4_received_d_rel},
-	{ST_ACTIVE,		EV_DLRL,		l4_active_dlrl},
-	{ST_WAIT_BRELEASE,	EV_BC_REL,		l4_send_d_disc},
-	{ST_WAIT_BRELEASE,	EV_DISCONNECT_IND,	l4_received_d_disc},
-	{ST_WAIT_BRELEASE,	EV_RELEASE_CNF,		l4_received_d_relcnf},
-	{ST_WAIT_BRELEASE,	EV_RELEASE_IND,		l4_received_d_rel},
-	{ST_WAIT_BREL_DISC,	EV_BC_REL,		l4_received_d_disc},
-	{ST_WAIT_BREL_DISC,	EV_RELEASE_CNF,		l4_received_d_relcnf},
-	{ST_WAIT_BREL_DISC,	EV_RELEASE_IND,		l4_received_d_rel},
-	{ST_WAIT_DCOMMAND,	EV_HANGUP,		l4_send_d_disc},
-	{ST_WAIT_DCOMMAND,	EV_DISCONNECT_IND,	l4_received_d_disc},
-	{ST_WAIT_DCOMMAND,	EV_RELEASE_CNF,		l4_received_d_relcnf},
-	{ST_WAIT_DCOMMAND,	EV_RELEASE_IND,		l4_received_d_rel},
-	{ST_WAIT_DRELEASE,	EV_RELEASE_IND,		l4_timeout_d},
-	{ST_WAIT_DRELEASE,	EV_RELEASE_CNF,		l4_timeout_d},
-	{ST_WAIT_DRELEASE,	EV_RELEASE_ERR,		l4_timeout_d},
-	{ST_WAIT_DRELEASE,	EV_DIAL,		l4_no_dchan_ready},
-	{ST_WAIT_D_REL_CNF,	EV_RELEASE_CNF,		l4_timeout_d},
-	{ST_WAIT_D_REL_CNF,	EV_RELEASE_ERR,		l4_timeout_d},
-	{ST_WAIT_D_REL_CNF,	EV_DIAL,		l4_no_dchan_ready},
-	{ST_WAIT_DSHUTDOWN,	EV_DLRL,		l4_go_null},
-	{ST_WAIT_DSHUTDOWN,	EV_DIAL,		l4_prep_dialout},
-	{ST_WAIT_DSHUTDOWN,	EV_SETUP_IND,		l4_start_dchan},
+	{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_r1(struct FsmInst *fi, int event, void *arg)
+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);
-	FsmAddTimer(&lf->act_timer, 1000, EV_LC_TIMER, NULL, 50);
-	lf->st->ma.manl1(lf->st, PH_ACTIVATE, NULL);
+	/* 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);
+	}
 }
 
 static void
-lc_r6(struct FsmInst *fi, int event, void *arg)
+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);
-	FsmAddTimer(&lf->act_timer, 40, EV_LC_TIMER, NULL, 51);
+	/* 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_r2(struct FsmInst *fi, int event, void *arg)
+lc_start_l2(struct FsmInst *fi, int event, void *arg)
 {
 	struct LcFsm *lf = fi->userdata;
 
-	if (lf->l2_establish) {
+/*	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);
@@ -1124,107 +1260,97 @@
 }
 
 static void
-lc_r3(struct FsmInst *fi, int event, void *arg)
+lc_connected(struct FsmInst *fi, int event, void *arg)
 {
 	struct LcFsm *lf = fi->userdata;
 
+	FsmDelTimer(&lf->act_timer, 50);
 	FsmChangeState(fi, ST_LC_CONNECTED);
 	lf->lccall(lf, LC_ESTABLISH, NULL);
 }
 
 static void
-lc_r7(struct FsmInst *fi, int event, void *arg)
-{
-	struct LcFsm *lf = fi->userdata;
-
-	FsmChangeState(fi, ST_LC_FLUSH_WAIT);
-	lf->st->ma.manl2(lf->st, DL_FLUSH, NULL);
-}
-
-static void
-lc_r4(struct FsmInst *fi, int event, void *arg)
+lc_release_l2(struct FsmInst *fi, int event, void *arg)
 {
 	struct LcFsm *lf = fi->userdata;
 
 	if (lf->l2_establish) {
 		FsmChangeState(fi, ST_LC_RELEASE_WAIT);
 		lf->st->ma.manl2(lf->st, DL_RELEASE, NULL);
-		/* This timer is for releasing the channel even
-		 * there is a hang in layer 2 ; 5 sec are a try
-		 */
-		FsmAddTimer(&lf->act_timer, 5000, EV_LC_TIMER, NULL, 53);
 	} else {
 		FsmChangeState(fi, ST_LC_NULL);
-		lf->st->ma.manl1(lf->st, PH_DEACTIVATE, NULL);
+		lf->st->ma.manl1(lf->st, PH_DEACTIVATE_REQ, NULL);
 		lf->lccall(lf, LC_RELEASE, NULL);
 	}
 }
 
 static void
-lc_r4_1(struct FsmInst *fi, int event, void *arg)
+lc_l2_released(struct FsmInst *fi, int event, void *arg)
 {
 	struct LcFsm *lf = fi->userdata;
 
-	FsmChangeState(fi, ST_LC_FLUSH_DELAY);
-	FsmAddTimer(&lf->act_timer, 50, EV_LC_TIMER, NULL, 52);
+	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);
 }
 
 static void
-lc_r5_1(struct FsmInst *fi, int event, void *arg)
+lc_release_l1(struct FsmInst *fi, int event, void *arg)
 {
 	struct LcFsm *lf = fi->userdata;
 
-	FsmChangeState(fi, ST_LC_RELEASE_WAIT);
-	/* This delay is needed for send out the UA frame before
-	 * PH_DEACTIVATE the interface
-	 */
-	FsmAddTimer(&lf->act_timer, 10, EV_LC_TIMER, NULL, 54);
+	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);
 }
 
 static void
-lc_r5(struct FsmInst *fi, int event, void *arg)
+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->st->ma.manl1(lf->st, PH_DEACTIVATE, NULL);
 	lf->lccall(lf, LC_RELEASE, NULL);
 }
 /* *INDENT-OFF* */
-static struct FsmNode LcFnList[] =
+static struct FsmNode LcFnList[] HISAX_INITDATA =
 {
-	{ST_LC_NULL,  		EV_LC_ESTABLISH,	lc_r1},
-	{ST_LC_ACTIVATE_WAIT,	EV_LC_PH_ACTIVATE,	lc_r6},
-	{ST_LC_DELAY,		EV_LC_TIMER,		lc_r2},
-	{ST_LC_DELAY,		EV_LC_DL_ESTABLISH,	lc_r3},
-	{ST_LC_ESTABLISH_WAIT,	EV_LC_DL_ESTABLISH,	lc_r3},
-	{ST_LC_ESTABLISH_WAIT,	EV_LC_RELEASE,		lc_r5},
-	{ST_LC_CONNECTED,	EV_LC_FLUSH,		lc_r7},
-	{ST_LC_CONNECTED,	EV_LC_RELEASE,		lc_r4},
-	{ST_LC_CONNECTED,	EV_LC_DL_RELEASE,	lc_r5_1},
-	{ST_LC_FLUSH_WAIT,	EV_LC_DL_FLUSH,		lc_r4_1},
-	{ST_LC_FLUSH_DELAY,	EV_LC_TIMER,		lc_r4},
-	{ST_LC_RELEASE_WAIT,	EV_LC_DL_RELEASE,	lc_r5},
-	{ST_LC_RELEASE_WAIT,	EV_LC_TIMER,		lc_r5},
-	{ST_LC_ACTIVATE_WAIT,	EV_LC_TIMER,		lc_r5},
-	{ST_LC_ESTABLISH_WAIT,	EV_LC_DL_RELEASE,	lc_r5},
+	{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))
 
-void
-CallcNew(void)
+HISAX_INITFUNC(void
+CallcNew(void))
 {
 	callcfsm.state_count = STATE_COUNT;
 	callcfsm.event_count = EVENT_COUNT;
@@ -1247,17 +1373,11 @@
 }
 
 static void
-release_ds(struct Channel *chanp)
+release_b_st(struct Channel *chanp)
 {
-	struct PStack *st = &chanp->ds;
-	struct IsdnCardState *sp;
-	struct HscxState *hsp;
-
-	sp = st->l1.hardware;
-	hsp = sp->hs + chanp->hscx;
-
-	close_hscxstate(hsp);
+	struct PStack *st = chanp->b_st;
 
+	chanp->bcs->BC_Close(chanp->bcs);
 	switch (chanp->l2_active_protocol) {
 		case (ISDN_PROTO_L2_X75I):
 			releasestack_isdnl2(st);
@@ -1268,90 +1388,145 @@
 			break;
 	}
 	/* Reset B-Channel Statemachine */
-	FsmDelTimer(&chanp->lc_b.act_timer, 79);
-	FsmChangeState(&chanp->lc_b.lcfi, ST_LC_NULL);
+	FsmDelTimer(&chanp->lc_b->act_timer, 79);
+	FsmChangeState(&chanp->lc_b->lcfi, ST_LC_NULL);
 }
 
 static void
-cc_l1man(struct PStack *st, int pr, void *arg)
+dc_l1man(struct PStack *st, int pr, void *arg)
 {
-	struct Channel *chanp = (struct Channel *) st->l4.userdata;
+	struct Channel *chanp;
 
+	chanp = (struct Channel *) st->lli.userdata;
 	switch (pr) {
-		case (PH_ACTIVATE):
-			FsmEvent(&chanp->lc_d.lcfi, EV_LC_PH_ACTIVATE, NULL);
+		case (PH_ACTIVATE_CNF):
+		case (PH_ACTIVATE_IND):
+			FsmEvent(&chanp->lc_d->lcfi, EV_LC_PH_ACTIVATE, NULL);
 			break;
-		case (PH_DEACTIVATE):
-			FsmEvent(&chanp->lc_d.lcfi, EV_LC_PH_DEACTIVATE, NULL);
+		case (PH_DEACTIVATE_IND):
+			FsmEvent(&chanp->lc_d->lcfi, EV_LC_PH_DEACTIVATE, NULL);
 			break;
 	}
 }
 
 static void
-cc_l2man(struct PStack *st, int pr, void *arg)
+dc_l2man(struct PStack *st, int pr, void *arg)
 {
-	struct Channel *chanp = (struct Channel *) st->l4.userdata;
+	struct Channel *chanp = (struct Channel *) st->lli.userdata;
 
 	switch (pr) {
 		case (DL_ESTABLISH):
-			FsmEvent(&chanp->lc_d.lcfi, EV_LC_DL_ESTABLISH, NULL);
+			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;
-		case (DL_FLUSH):
-			FsmEvent(&chanp->lc_d.lcfi, EV_LC_DL_FLUSH, NULL);
+			FsmEvent(&chanp->lc_d->lcfi, EV_LC_DL_RELEASE, NULL);
 			break;
 	}
 }
 
 static void
-dcc_l1man(struct PStack *st, int pr, void *arg)
+bc_l1man(struct PStack *st, int pr, void *arg)
 {
-	struct Channel *chanp = (struct Channel *) st->l4.userdata;
+	struct Channel *chanp = (struct Channel *) st->lli.userdata;
 
 	switch (pr) {
-		case (PH_ACTIVATE):
-			FsmEvent(&chanp->lc_b.lcfi, EV_LC_PH_ACTIVATE, NULL);
+		case (PH_ACTIVATE_IND):
+		case (PH_ACTIVATE_CNF):
+			FsmEvent(&chanp->lc_b->lcfi, EV_LC_PH_ACTIVATE, NULL);
 			break;
-		case (PH_DEACTIVATE):
-			FsmEvent(&chanp->lc_b.lcfi, EV_LC_PH_DEACTIVATE, NULL);
+		case (PH_DEACTIVATE_IND):
+			FsmEvent(&chanp->lc_b->lcfi, EV_LC_PH_DEACTIVATE, NULL);
 			break;
 	}
 }
 
 static void
-dcc_l2man(struct PStack *st, int pr, void *arg)
+bc_l2man(struct PStack *st, int pr, void *arg)
 {
-	struct Channel *chanp = (struct Channel *) st->l4.userdata;
+	struct Channel *chanp = (struct Channel *) st->lli.userdata;
 
 	switch (pr) {
 		case (DL_ESTABLISH):
-			FsmEvent(&chanp->lc_b.lcfi, EV_LC_DL_ESTABLISH, NULL);
+			FsmEvent(&chanp->lc_b->lcfi, EV_LC_DL_ESTABLISH, NULL);
 			break;
 		case (DL_RELEASE):
-			FsmEvent(&chanp->lc_b.lcfi, EV_LC_DL_RELEASE, NULL);
+			FsmEvent(&chanp->lc_b->lcfi, EV_LC_DL_RELEASE, NULL);
 			break;
 	}
 }
 
-static void
-l2tei_dummy(struct PStack *st, int pr, void *arg)
+struct Channel
+*selectfreechannel(struct PStack *st)
 {
-	struct Channel *chanp = (struct Channel *) st->l4.userdata;
-	char tmp[64], tm[32];
+	struct IsdnCardState *cs = st->l1.hardware;
+	struct Channel *chanp = st->lli.userdata;
+	int i;
 
-	jiftime(tm, jiffies);
-	sprintf(tmp, "%s Channel %d Warning! Dummy l2tei called pr=%d\n", tm, chanp->chan, pr);
-	HiSax_putstatus(chanp->sp, tmp);
+	if (test_bit(FLG_TWO_DCHAN, &cs->HW_Flags))
+		i=1;
+	else
+		i=0;
+	while (i<2) {
+		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);
+		chanp++;
+		i++;
+	}		
+	return (0);
 }
 
 static void
-ll_handler(struct PStack *st, int pr, void *arg)
+ll_handler(struct l3_process *pc, int pr, void *arg)
 {
-	struct Channel *chanp = (struct Channel *) st->l4.userdata;
+	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;
+			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;
+		}
+
+			
+	}
+	chanp = pc->chan;
 	switch (pr) {
 		case (CC_DISCONNECT_IND):
 			FsmEvent(&chanp->fi, EV_DISCONNECT_IND, NULL);
@@ -1359,9 +1534,6 @@
 		case (CC_RELEASE_CNF):
 			FsmEvent(&chanp->fi, EV_RELEASE_CNF, NULL);
 			break;
-		case (CC_SETUP_IND):
-			FsmEvent(&chanp->fi, EV_SETUP_IND, NULL);
-			break;
 		case (CC_RELEASE_IND):
 			FsmEvent(&chanp->fi, EV_RELEASE_IND, NULL);
 			break;
@@ -1386,49 +1558,51 @@
 		case (CC_RELEASE_ERR):
 			FsmEvent(&chanp->fi, EV_RELEASE_ERR, NULL);
 			break;
+		case (CC_PROCEEDING_IND):
+		case (CC_ALERTING_IND):
+			break;
 		default:
-			if (chanp->debug & 2048) {
+			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->sp, tmp);
+				HiSax_putstatus(chanp->cs, tmp);
 			}
 	}
 }
 
 static void
-init_is(struct Channel *chanp, unsigned int ces)
+init_d_st(struct Channel *chanp)
 {
-	struct PStack *st = &chanp->is;
-	struct IsdnCardState *sp = chanp->sp;
+	struct PStack *st = chanp->d_st;
+	struct IsdnCardState *cs = chanp->cs;
 	char tmp[128];
 
-	setstack_HiSax(st, sp);
+	HiSax_addlist(cs, st);
+	setstack_HiSax(st, cs);
 	st->l2.sap = 0;
-	st->l2.tei = 255;
-	st->l2.ces = ces;
-	st->l2.extended = !0;
-	st->l2.laptype = LAPD;
+	st->l2.tei = -1;
+	st->l2.flag = 0;
+	test_and_set_bit(FLG_MOD128, &st->l2.flag);
+	test_and_set_bit(FLG_LAPD, &st->l2.flag);
+	test_and_set_bit(FLG_ORIG, &st->l2.flag);
+	st->l2.maxlen = MAX_DFRAME_LEN;
 	st->l2.window = 1;
-	st->l2.orig = !0;
-	st->l2.t200 = 1000;	/* 1000 milliseconds  */
-	if (st->protocol == ISDN_PTYPE_1TR6) {
-		st->l2.n200 = 3;	/* try 3 times        */
-		st->l2.t203 = 10000;	/* 10000 milliseconds */
-	} else {
-		st->l2.n200 = 4;	/* try 4 times        */
-		st->l2.t203 = 5000;	/* 5000 milliseconds  */
-	}
+	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 */
+	else
+		st->l2.T203 = 10000;	/* 5000 milliseconds  */
+	
 	sprintf(tmp, "Channel %d q.921", chanp->chan);
 	setstack_isdnl2(st, tmp);
 	setstack_isdnl3(st, chanp);
-	st->l4.userdata = chanp;
-	st->l4.l2writewakeup = NULL;
+	st->lli.userdata = chanp;
+	st->lli.l2writewakeup = NULL;
 	st->l3.l3l4 = ll_handler;
-	st->l1.l1man = cc_l1man;
-	st->l2.l2man = cc_l2man;
-	st->pa = &chanp->para;
-	HiSax_addlist(sp, st);
+	st->l1.l1man = dc_l1man;
+	st->l2.l2man = dc_l2man;
 }
 
 static void
@@ -1439,7 +1613,7 @@
 
 	jiftime(tm, jiffies);
 	sprintf(str, "%s Channel %d callc %s\n", tm, chanp->chan, s);
-	HiSax_putstatus(chanp->sp, str);
+	HiSax_putstatus(chanp->cs, str);
 }
 
 static void
@@ -1449,8 +1623,8 @@
 	struct LcFsm *lf = fi->userdata;
 
 	jiftime(tm, jiffies);
-	sprintf(str, "%s Channel %d lc %s\n", tm, lf->ch->chan, s);
-	HiSax_putstatus(lf->ch->sp, str);
+	sprintf(str, "%s Channel %d dc %s\n", tm, lf->ch->chan, s);
+	HiSax_putstatus(lf->ch->cs, str);
 }
 
 static void
@@ -1460,22 +1634,35 @@
 	struct LcFsm *lf = fi->userdata;
 
 	jiftime(tm, jiffies);
-	sprintf(str, "%s Channel %d dlc %s\n", tm, lf->ch->chan, s);
-	HiSax_putstatus(lf->ch->sp, str);
+	sprintf(str, "%s Channel %d bc %s\n", tm, lf->ch->chan, s);
+	HiSax_putstatus(lf->ch->cs, str);
 }
 
 static void
 lccall_d(struct LcFsm *lf, int pr, void *arg)
 {
-	struct Channel *chanp = lf->ch;
+	struct IsdnCardState *cs = lf->st->l1.hardware;
+	struct Channel *chanp;
+	int i;
 
-	switch (pr) {
-		case (LC_ESTABLISH):
-			FsmEvent(&chanp->fi, EV_DLEST, NULL);
-			break;
-		case (LC_RELEASE):
-			FsmEvent(&chanp->fi, EV_DLRL, NULL);
-			break;
+	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++;
 	}
 }
 
@@ -1495,20 +1682,19 @@
 }
 
 static void
-init_chan(int chan, struct IsdnCardState *csta, int hscx,
-	  unsigned int ces)
+init_chan(int chan, struct IsdnCardState *csta)
 {
 	struct Channel *chanp = csta->channel + chan;
 
-	chanp->sp = csta;
-	chanp->hscx = hscx;
+	chanp->cs = csta;
+	chanp->bcs = csta->bcs + chan;
 	chanp->chan = chan;
 	chanp->incoming = 0;
 	chanp->debug = 0;
 	chanp->Flags = 0;
 	chanp->leased = 0;
-	chanp->impair = 0;
-	init_is(chanp, ces);
+	chanp->b_st = kmalloc(sizeof(struct PStack), GFP_ATOMIC);
+	chanp->b_st->next = NULL;
 
 	chanp->fi.fsm = &callcfsm;
 	chanp->fi.state = ST_NULL;
@@ -1517,58 +1703,72 @@
 	chanp->fi.printdebug = callc_debug;
 	FsmInitTimer(&chanp->fi, &chanp->dial_timer);
 	FsmInitTimer(&chanp->fi, &chanp->drel_timer);
-
-	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.ch = chanp;
-	chanp->lc_d.st = &chanp->is;
-	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);
-
-	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.ch = chanp;
-	chanp->lc_b.st = &chanp->ds;
-	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->outcallref = 64;
+	if (!chan || test_bit(FLG_TWO_DCHAN, &csta->HW_Flags)) {
+		chanp->d_st = kmalloc(sizeof(struct PStack), GFP_ATOMIC);
+		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 ces;
-
 	chancount += 2;
-	ces = randomces();
-	init_chan(0, csta, 1, ces++);
-	ces %= 0xffff;
-	init_chan(1, csta, 0, ces++);
+	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
 	return (2);
 }
 
 static void
-release_is(struct Channel *chanp)
+release_d_st(struct Channel *chanp)
 {
-	struct PStack *st = &chanp->is;
+	struct PStack *st = chanp->d_st;
 
+	if (!st)
+		return;
 	releasestack_isdnl2(st);
 	releasestack_isdnl3(st);
 	HiSax_rmlist(st->l1.hardware, st);
+	kfree(st);
+	chanp->d_st = NULL;
 }
 
 void
@@ -1579,27 +1779,45 @@
 	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_b.act_timer, 76);
-		FsmDelTimer(&csta->channel[i].lc_d.act_timer, 77);
-		if (csta->channel[i].Flags & FLG_START_B) {
-			release_ds(csta->channel + i);
+		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
+			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;
 		}
-		release_is(csta->channel + i);
+		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;
 	}
 }
 
 static void
 lldata_handler(struct PStack *st, int pr, void *arg)
 {
-	struct Channel *chanp = (struct Channel *) st->l4.userdata;
+	struct Channel *chanp = (struct Channel *) st->lli.userdata;
 	struct sk_buff *skb = arg;
 
 	switch (pr) {
 		case (DL_DATA):
 			if (chanp->data_open)
-				chanp->sp->iif.rcvcallb_skb(chanp->sp->myid, chanp->chan, skb);
+				chanp->cs->iif.rcvcallb_skb(chanp->cs->myid, chanp->chan, skb);
 			else {
-				SET_SKB_FREE(skb);
 				dev_kfree_skb(skb);
 			}
 			break;
@@ -1613,16 +1831,22 @@
 static void
 lltrans_handler(struct PStack *st, int pr, void *arg)
 {
-	struct Channel *chanp = (struct Channel *) st->l4.userdata;
+	struct Channel *chanp = (struct Channel *) st->lli.userdata;
 	struct sk_buff *skb = arg;
 
 	switch (pr) {
-		case (PH_DATA):
+		case (PH_DATA_IND):
 			if (chanp->data_open)
-				chanp->sp->iif.rcvcallb_skb(chanp->sp->myid, chanp->chan, skb);
+				chanp->cs->iif.rcvcallb_skb(chanp->cs->myid, chanp->chan, skb);
 			else {
-				SET_SKB_FREE(skb);
-				dev_kfree_skb(skb);
+				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);
 			}
 			break;
 		default:
@@ -1633,73 +1857,79 @@
 }
 
 static void
-ll_writewakeup(struct PStack *st)
+ll_writewakeup(struct PStack *st, int len)
 {
-	struct Channel *chanp = st->l4.userdata;
+	struct Channel *chanp = st->lli.userdata;
 	isdn_ctrl ic;
 
-	ic.driver = chanp->sp->myid;
+	ic.driver = chanp->cs->myid;
 	ic.command = ISDN_STAT_BSENT;
 	ic.arg = chanp->chan;
-	chanp->sp->iif.statcallb(&ic);
+	ic.parm.length = len;
+	chanp->cs->iif.statcallb(&ic);
 }
 
 static int
-init_ds(struct Channel *chanp, int incoming)
+init_b_st(struct Channel *chanp, int incoming)
 {
-	struct PStack *st = &chanp->ds;
-	struct IsdnCardState *sp = chanp->sp;
-	struct HscxState *hsp = sp->hs + chanp->hscx;
+	struct PStack *st = chanp->b_st;
+	struct IsdnCardState *cs = chanp->cs;
 	char tmp[128];
 
-	st->l1.hardware = sp;
-
-	hsp->mode = 2;
-
-	if (setstack_hscx(st, hsp))
+	st->l1.hardware = cs;
+	chanp->bcs->mode = 2;
+	if (chanp->bcs->BC_SetStack(st, chanp->bcs))
 		return (-1);
-
-	st->l2.extended = 0;
-	st->l2.laptype = LAPB;
-	st->l2.orig = !incoming;
-	st->l2.t200 = 1000;	/* 1000 milliseconds */
+	st->l2.flag = 0;
+	test_and_set_bit(FLG_LAPB, &st->l2.flag);
+	st->l2.maxlen = MAX_DATA_SIZE;
+	if (!incoming)
+		test_and_set_bit(FLG_ORIG, &st->l2.flag);
+	st->l2.T200 = 1000;	/* 1000 milliseconds */
 	st->l2.window = 7;
-	st->l2.n200 = 4;	/* try 4 times       */
-	st->l2.t203 = 5000;	/* 5000 milliseconds */
-
+	st->l2.N200 = 4;	/* try 4 times       */
+	st->l2.T203 = 5000;	/* 5000 milliseconds */
 	st->l3.debug = 0;
 	switch (chanp->l2_active_protocol) {
 		case (ISDN_PROTO_L2_X75I):
 			sprintf(tmp, "Channel %d x.75", chanp->chan);
 			setstack_isdnl2(st, tmp);
 			st->l2.l2l3 = lldata_handler;
-			st->l1.l1man = dcc_l1man;
-			st->l2.l2man = dcc_l2man;
-			st->l2.l2tei = l2tei_dummy;
-			st->l4.userdata = chanp;
-			st->l4.l1writewakeup = NULL;
-			st->l4.l2writewakeup = ll_writewakeup;
+			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.hscxmode = 2;	/* Packet-Mode ? */
-			st->l1.hscxchannel = chanp->para.bchannel - 1;
+			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 = dcc_l1man;
-			st->l4.userdata = chanp;
-			st->l4.l1writewakeup = ll_writewakeup;
-			st->l1.hscxmode = 2;
-			st->l1.hscxchannel = chanp->para.bchannel - 1;
+			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):
 			st->l1.l1l2 = lltrans_handler;
-			st->l1.l1man = dcc_l1man;
-			st->l4.userdata = chanp;
-			st->l4.l1writewakeup = ll_writewakeup;
-			st->l1.hscxmode = 1;
-			st->l1.hscxchannel = chanp->para.bchannel - 1;
+			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;
 			break;
 	}
 	return (0);
@@ -1719,15 +1949,17 @@
 	for (i = 0; i < 2; i++) {
 		chanp[i].debug = debugflags;
 		chanp[i].fi.debug = debugflags & 2;
-		chanp[i].is.l2.l2m.debug = debugflags & 8;
-		chanp[i].ds.l2.l2m.debug = debugflags & 16;
-		chanp[i].is.l2.debug = debugflags & 32;
-		chanp[i].ds.l2.debug = debugflags & 64;
-		chanp[i].lc_d.lcfi.debug = debugflags & 128;
-		chanp[i].lc_b.lcfi.debug = debugflags & 256;
+		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].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;
 	}
 	csta->dlogflag = debugflags & 4;
-	csta->teistack->l2.l2m.debug = debugflags & 512;
 }
 
 int
@@ -1748,8 +1980,11 @@
 	switch (ic->command) {
 		case (ISDN_CMD_SETEAZ):
 			chanp = csta->channel + ic->arg;
-			if (chanp->debug & 1)
-				link_debug(chanp, "SETEAZ", 1);
+			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);
@@ -1768,9 +2003,9 @@
 				 ic->parm.setup.si1, ic->parm.setup.si2);
 				link_debug(chanp, tmp, 1);
 			}
-			chanp->para.setup = ic->parm.setup;
-			if (!strcmp(chanp->para.setup.eazmsn, "0"))
-				chanp->para.setup.eazmsn[0] = '\0';
+			chanp->setup = ic->parm.setup;
+			if (!strcmp(chanp->setup.eazmsn, "0"))
+				chanp->setup.eazmsn[0] = '\0';
 			/* this solution is dirty and may be change, if
 			 * we make a callreference based callmanager */
 			if (chanp->fi.state == ST_NULL) {
@@ -1817,7 +2052,7 @@
 		case (ISDN_CMD_LOCK):
 			HiSax_mod_inc_use_count();
 #ifdef MODULE
-			if (csta->channel[0].debug & 1024) {
+			if (csta->channel[0].debug & 0x400) {
 				jiftime(tmp, jiffies);
 				i = strlen(tmp);
 				sprintf(tmp + i, "   LOCK modcnt %lx\n", MOD_USE_COUNT);
@@ -1828,7 +2063,7 @@
 		case (ISDN_CMD_UNLOCK):
 			HiSax_mod_dec_use_count();
 #ifdef MODULE
-			if (csta->channel[0].debug & 1024) {
+			if (csta->channel[0].debug & 0x400) {
 				jiftime(tmp, jiffies);
 				i = strlen(tmp);
 				sprintf(tmp + i, " UNLOCK modcnt %lx\n", MOD_USE_COUNT);
@@ -1852,16 +2087,13 @@
 					printk(KERN_DEBUG "HiSax: %s", tmp);
 					break;
 				case (2):
-					num = *(unsigned int *) ic->parm.num;
-					i = num >> 8;
-					if (i >= 2)
-						break;
-					chanp = csta->channel + i;
-					chanp->impair = num & 0xff;
-					if (chanp->debug & 1) {
-						sprintf(tmp, "IMPAIR %x", chanp->impair);
-						link_debug(chanp, tmp, 1);
-					}
+					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",
+						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++)
@@ -1872,35 +2104,47 @@
 						HiSax_mod_inc_use_count();
 					break;
 				case (5):	/* set card in leased mode */
-					csta->channel[0].leased = 1;
-					csta->channel[1].leased = 1;
-					sprintf(tmp, "card %d set into leased mode\n",
-						csta->cardnr + 1);
-					HiSax_putstatus(csta, tmp);
+					num = *(unsigned int *) ic->parm.num;
+					if ((num <1) || (num > 2)) {
+						sprintf(tmp, "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",
+							csta->cardnr + 1, num + 1);
+						HiSax_putstatus(csta, tmp);
+						FsmEvent(&csta->channel[num].lc_d->lcfi, EV_LC_ESTABLISH, 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);
 					break;
 #ifdef MODULE
 				case (55):
-#if (LINUX_VERSION_CODE < 0x020111)
-					MOD_USE_COUNT = MOD_VISITED;
-#else
 					MOD_USE_COUNT = 0;
-#endif
 					HiSax_mod_inc_use_count();
 					break;
 #endif				/* MODULE */
 				case (11):
 					csta->debug = *(unsigned int *) ic->parm.num;
 					sprintf(tmp, "l1 debugging flags card %d set to %x\n",
-					  csta->cardnr + 1, csta->debug);
-					HiSax_putstatus(cards[0].sp, tmp);
+						csta->cardnr + 1, csta->debug);
+					HiSax_putstatus(cards[0].cs, tmp);
 					printk(KERN_DEBUG "HiSax: %s", tmp);
 					break;
 				case (13):
-					csta->channel[0].is.l3.debug = *(unsigned int *) ic->parm.num;
-					csta->channel[1].is.l3.debug = *(unsigned int *) ic->parm.num;
+					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",
 						csta->cardnr + 1, *(unsigned int *) ic->parm.num);
-					HiSax_putstatus(cards[0].sp, tmp);
+					HiSax_putstatus(cards[0].cs, tmp);
 					printk(KERN_DEBUG "HiSax: %s", tmp);
 					break;
 				default:
@@ -1917,7 +2161,7 @@
 }
 
 int
-HiSax_writebuf_skb(int id, int chan, struct sk_buff *skb)
+HiSax_writebuf_skb(int id, int chan, int ack, struct sk_buff *skb)
 {
 	struct IsdnCardState *csta = hisax_findcard(id);
 	struct Channel *chanp;
@@ -1933,7 +2177,7 @@
 		return -ENODEV;
 	}
 	chanp = csta->channel + chan;
-	st = &chanp->ds;
+	st = chanp->b_st;
 	if (!chanp->data_open) {
 		link_debug(chanp, "writebuf: channel not open", 1);
 		return -EIO;
@@ -1945,11 +2189,11 @@
 		return -EINVAL;
 	}
 	if (len) {
-		if ((len + csta->hs[chanp->hscx].tx_cnt) > MAX_DATA_MEM) {
+		if ((len + chanp->bcs->tx_cnt) > MAX_DATA_MEM) {
 			/* Must return 0 here, since this is not an error
 			 * but a temporary lack of resources.
 			 */
-			if (chanp->debug & 2048) {
+			if (chanp->debug & 0x800) {
 				sprintf(tmp, "writebuf: no buffers for %d bytes", len);
 				link_debug(chanp, tmp, 1);
 			}
@@ -1959,12 +2203,13 @@
 		cli();
 		nskb = skb_clone(skb, GFP_ATOMIC);
 		if (nskb) {
-			if (chanp->lc_b.l2_establish) {
-				csta->hs[chanp->hscx].tx_cnt += len + st->l2.ihsize;
-				chanp->ds.l3.l3l2(&chanp->ds, DL_DATA, nskb);
-			} else {
-				csta->hs[chanp->hscx].tx_cnt += len;
-				chanp->ds.l2.l2l1(&chanp->ds, PH_DATA, nskb);
+			if (!ack)
+				nskb->pkt_type = PACKET_NOACK;
+			if (chanp->lc_b->l2_establish)
+				st->l3.l3l2(st, DL_DATA, nskb);
+			else {
+				chanp->bcs->tx_cnt += len;
+				st->l2.l2l1(st, PH_DATA_REQ, nskb);
 			}
 			dev_kfree_skb(skb);
 		} else

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen, slshen@lbl.gov