--- /root/3.2/wanpipe/patches/kdrivers/src/net/wanpipe_tdm_api.c	2008-07-08 13:13:58.000000000 -0400
+++ wanpipe_tdm_api.c	2008-07-08 15:09:50.000000000 -0400
@@ -5,7 +5,7 @@
 *
 * Authors: 	Nenad Corbic <ncorbic@sangoma.com>
 *
-* Copyright:	(c) 2003-2007 Sangoma Technologies Inc.
+* Copyright:	(c) 2003-2005 Sangoma Technologies Inc.
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
@@ -13,7 +13,10 @@
 *		2 of the License, or (at your option) any later version.
 * ============================================================================
 * Oct 04, 2005	Nenad Corbic	Initial version.
-* Jul 27, 2006	David Rokhvarg	<davidr@sangoma.com>	Ported to Windows.
+* Jul 27, 2006	David Rokhvarg	<davidr@sangoma.com>	
+*				Ported to Windows.
+* Mar 10, 2008	David Rokhvarg	<davidr@sangoma.com>
+*				Added BRI LoopBack control.
 *****************************************************************************/
 
 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
@@ -24,14 +27,12 @@
 # include <wanpipe_tdm_api.h>
 #elif defined(__WINDOWS__)
 # include <wanpipe_includes.h>
-# include <wanpipe_defines.h>
-# include <wanpipe_debug.h>
 # include <wanpipe_tdm_api.h>
 
 int 
 aft_te1_insert_tdm_api_event_in_to_rx_queue(
 	wanpipe_tdm_api_dev_t	*tdm_api_dev,
-	wp_tdm_api_rx_hdr_t		*tdm_api_rx_hdr
+	wp_tdm_api_event_t		*pevent	
 	);
 
 int 
@@ -40,8 +41,10 @@
 	);
 
 #define DBG_TDMCODEC	if(0)DbgPrint
-#define DBG_TDM_RX		if(0)DbgPrint
-#define DBG_RBS			if(0)DbgPrint
+#define DBG_TDM_RX	if(0)DbgPrint
+#define DBG_RBS		if(0)DbgPrint
+
+#define BUILD_TDMV_API
 
 #elif defined(__LINUX__)
 # include <linux/wanpipe_includes.h>
@@ -52,7 +55,6 @@
 # include <linux/wanpipe_tdm_api.h>
 #endif
 
-#include <wanec_iface.h>
 
 /*==============================================================
   Defines
@@ -62,8 +64,8 @@
 #define WP_TDMAPI_MINOR_OFFSET 0
 #define WP_TDMAPI_MAX_MINORS 1024
 
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) || defined(__WINDOWS__)
-
+#if !defined(__WINDOWS__)
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
 
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15)
 #define WP_CLASS_DEV_CREATE(class, devt, device, name) \
@@ -84,6 +86,12 @@
 #endif
 
 #define	UNIT(file) MINOR(file->f_dentry->d_inode->i_rdev)
+#define BUILD_TDMV_API
+
+#endif/* #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) */
+#endif/* #if !defined(__WINDOWS__) */
+
+#if defined(BUILD_TDMV_API)
 
 #define WP_TDMAPI_SPAN_MASK	0xFFFF
 #define WP_TMDAPI_SPAN_SHIFT	5 //8
@@ -145,6 +153,7 @@
 static unsigned int wp_tdmapi_poll(struct file *file, struct poll_table_struct *wait_table); 
 static int wanpipe_tdm_api_ioctl(wanpipe_tdm_api_dev_t *tdm_api, struct ifreq *ifr);                   
 #endif
+static int wanpipe_tdm_api_event_ioctl(wanpipe_tdm_api_dev_t*, wanpipe_tdm_api_cmd_t*);
  
 /*==============================================================
   Global Variables
@@ -220,7 +229,6 @@
 		wake_up_interruptible(&tdm_api->poll_wait);
 	}
 }
-#endif
 
 static struct cdev wptdm_cdev = {
 #ifndef LINUX_FEAT_2624
@@ -228,7 +236,7 @@
 #endif
 	.owner	=	THIS_MODULE,
 };
-
+#endif
 
 static int wp_tdmapi_reg_globals(void)
 {
@@ -236,8 +244,8 @@
 	
 	rx_gains=NULL;
 	tx_gains=NULL;
-	wan_spin_lock_init(&wp_tdmapi_hash_lock);
-	DEBUG_TEST("%s: Registering Wanpipe TDM Device!\n",__FUNCTION__);
+	wan_spin_lock_init(&wp_tdmapi_hash_lock, "wan_tdmapi_hash_lock");
+	DEBUG_TDMAPI("%s: Registering Wanpipe TDM Device!\n",__FUNCTION__);
 #if !defined(__WINDOWS__)
 	{
 #ifdef LINUX_2_4
@@ -324,17 +332,25 @@
 	
 	if (tdm_api->hdlc_framing) {
 	
-		tdm_api->cfg.hw_mtu_mru		=1500;	
+		if (IS_BRI_CARD(card)) {
+			tdm_api->cfg.hw_mtu_mru		=300;	
+			tdm_api->cfg.usr_mtu_mru	=300;	
+		} else {
+			tdm_api->cfg.hw_mtu_mru		=1500;	
+			tdm_api->cfg.usr_mtu_mru	=1500;	
+		}
+
 		tdm_api->cfg.usr_period		=0;	
 		tdm_api->cfg.tdm_codec		=WP_NONE;		
 		tdm_api->cfg.power_level	=0;	
 		tdm_api->cfg.rx_disable		=0;	
 		tdm_api->cfg.tx_disable		=0;			
-		tdm_api->cfg.usr_mtu_mru	=1500;	
 		tdm_api->cfg.ec_tap		=0;	
 		tdm_api->cfg.rbs_rx_bits	=-1;
 		tdm_api->cfg.hdlc		=1;
-		tdm_api->tx_q_len		= WP_TDM_MAX_HDLC_TX_Q_LEN;
+
+		/* We are expecting tx_q_len for hdlc
+  		 * to be configured from upper layer */
 		
 	} else { 
 		tdm_api->cfg.hw_mtu_mru		=8;	
@@ -373,17 +389,27 @@
 		}
 		return err;
 	}
-	wan_spin_lock_init(&tdm_api->lock);	
+	wan_spin_lock_init(&tdm_api->lock, "wan_tdmapi_lock");	
 	sprintf(tmp_name,"wptdm_s%dc%d",tdm_api->tdm_span,tdm_api->tdm_chan);
 	
+	DEBUG_TDMAPI("%s: Configuring TDM API NAME=%s Qlen=%i\n",
+			card->devname,tmp_name, tdm_api->tx_q_len);
+
 	/* Initialize Event Callback functions */
 	card->wandev.event_callback.rbsbits		= wp_tdmapi_rbsbits; 
 	card->wandev.event_callback.alarms		= wp_tdmapi_alarms; 
-	card->wandev.event_callback.dtmf		= wp_tdmapi_dtmf; 
 	card->wandev.event_callback.hook		= wp_tdmapi_hook; 
 	card->wandev.event_callback.ringdetect	= wp_tdmapi_ringdetect;
 	card->wandev.event_callback.ringtrip	= wp_tdmapi_ringtrip;
-	
+
+#if defined(__WINDOWS__)
+	/* Analog always supports DTMF detection */
+	tdm_api->dtmfsupport = WANOPT_YES;
+#endif
+
+	/* Always initialize the callback pointer */
+	card->wandev.event_callback.dtmf = wp_tdmapi_dtmf; 
+
 	if (tdm_api->cfg.rbs_tx_bits) {
 		DEBUG_EVENT("%s: Setting Tx RBS/CAS Idle Bits = 0x%02X\n",
 			       	        tmp_name, 
@@ -461,16 +487,15 @@
 	if (tdm_api == NULL || !wan_test_bit(0,&tdm_api->init)){
 		return -ENODEV;
 	}
-	 
-	if (is_tdm_api_stopped(tdm_api)){
+	
+	if (is_tdm_api_stopped(tdm_api) || (unsigned)wan_skb_queue_len(&tdm_api->wp_tx_list) > tdm_api->tx_q_len){
 		wp_tdm_api_start(tdm_api);
 		if (wan_test_bit(0,&tdm_api->used)) {
-			DEBUG_TEST("%s: KICK API\n",tdm_api->name);
 #if !defined(__WINDOWS__)
 			wp_wakeup_tdmapi(tdm_api);
 #endif
-		}
-	}
+		} 
+	} 
 	
 	return 0;
 }
@@ -530,7 +555,7 @@
 	wan_spin_unlock_irq(&wp_tdmapi_hash_lock,&flags);
 	
 	
-	DEBUG_TEST ("%s: DRIVER OPEN S/C(%i/%i) API Ptr=%p\n",
+	DEBUG_TDMAPI ("%s: DRIVER OPEN S/C(%i/%i) API Ptr=%p\n",
 		__FUNCTION__, tdm_span, tdm_chan, tdm_api);
 	
 	return 0;
@@ -579,9 +604,9 @@
 	
 	if (count < wan_skb_len(skb) || 
 	    wan_skb_len(skb) < sizeof(wp_tdm_api_rx_hdr_t)){
+		DEBUG_TDMAPI("%s:%d TDMAPI READ: Error: Count=%i < Skb=%i < HDR=%i Critical Error\n",
+			__FUNCTION__,__LINE__,count,wan_skb_len(skb),sizeof(wp_tdm_api_rx_hdr_t));
 		wan_skb_free(skb);
-		DEBUG_EVENT("%s:%d TDMAPI READ: Critical Error\n",
-			__FUNCTION__,__LINE__);
 		return -EFAULT;
 	}
 	
@@ -752,6 +777,7 @@
 			return -EINVAL;
 		}
 #endif
+
 		if (count > (WP_TDM_API_MAX_LEN+sizeof(wp_tdm_api_tx_hdr_t))) {
 			DEBUG_TEST("%s: Error: TDM API Tx packet too big %d\n",
 				tdm_api->name,count);
@@ -760,7 +786,10 @@
 
 		skb_len=WP_TDM_API_MAX_LEN+sizeof(wp_tdm_api_tx_hdr_t);
 	}
-	
+
+	DEBUG_TEST("%s: TX FRAME List=%i\n",
+			tdm_api->name, wan_skb_queue_len(&tdm_api->wp_tx_list));	
+
 	if (wan_skb_queue_len(&tdm_api->wp_tx_list) > tdm_api->tx_q_len){
 		wp_tdm_api_stop(tdm_api);
 		return -EBUSY;
@@ -1010,6 +1039,7 @@
 		    !is_tdm_api_stopped(tdm_api)) {
 			wp_tdmapi_tx(tdm_api);
 		}
+
 		
 		if (wan_skb_queue_len(&tdm_api->wp_tx_list) <= tdm_api->tx_q_len) {
 			ret |= POLLOUT | POLLWRNORM;
@@ -1038,11 +1068,11 @@
 
 static void wanpipe_tdm_api_rbs_poll(wanpipe_tdm_api_dev_t *tdm_api)
 {
-	u8					rbs_bits;
-	netskb_t			*skb;
-	wp_tdm_api_rx_hdr_t	*rx_hdr = NULL;
+	u8			rbs_bits;
+	netskb_t		*skb;
+	wp_tdm_api_event_t	*pevent = NULL;
 #if defined(__WINDOWS__)
-	wp_tdm_api_rx_hdr_t tdm_api_hdr;
+	wp_tdm_api_event_t	event;
 #endif
 
 	if (!tdm_api->cfg.rbs_poll) {
@@ -1063,36 +1093,32 @@
 #endif
 
 	tdm_api->read_rbs_bits(	tdm_api->chan, 
-#if defined(__WINDOWS__)
-							tdm_api->tdm_chan + 1, 
-#else
-							tdm_api->tdm_chan, 
-#endif
-							&rbs_bits);
+				tdm_api->tdm_chan, 
+				&rbs_bits);
 	if (tdm_api->cfg.rbs_rx_bits == rbs_bits) {
 		return;
 	}
 
-	DEBUG_TEST("%s: RBS BITS CHANGED O=0x%X N=0x%X\n",
+	DEBUG_TDMAPI("%s: RBS BITS CHANGED O=0x%X N=0x%X\n",
 			tdm_api->name, tdm_api->cfg.rbs_rx_bits, rbs_bits);
 
 	tdm_api->cfg.rbs_rx_bits = rbs_bits;
 				
 #if defined(__WINDOWS__)
-	rx_hdr = &tdm_api_hdr;
+	pevent = &event;
 #else
 	if (wan_skb_queue_len(&tdm_api->wp_event_list) > WP_TDM_MAX_EVENT_Q_LEN) {	
 		return;
 	}
-	skb=wan_skb_alloc(sizeof(wp_tdm_api_rx_hdr_t));
+	skb=wan_skb_alloc(sizeof(wp_tdm_api_event_t));
 	if (skb == NULL) return;
 
-	rx_hdr=(wp_tdm_api_rx_hdr_t*)wan_skb_put(skb,sizeof(wp_tdm_api_rx_hdr_t));
+	pevent = (wp_tdm_api_event_t*)wan_skb_put(skb,sizeof(wp_tdm_api_event_t));
 #endif/* #if !defined(__WINDOWS__) */
 				
-	memset(rx_hdr,0,sizeof(wp_tdm_api_rx_hdr_t));
-	rx_hdr->wp_tdm_api_event_rbs_rx_bits = (u8)tdm_api->cfg.rbs_rx_bits;
-	rx_hdr->wp_tdm_api_event_type = WP_TDM_EVENT_RBS;
+	memset(pevent,0,sizeof(wp_tdm_api_event_t));
+	pevent->wp_tdm_api_event_type	= WP_TDMAPI_EVENT_RBS;
+	pevent->wp_tdm_api_event_rbs_bits = (u8)tdm_api->cfg.rbs_rx_bits;
 
 #if 0
 	/* FIXME: NENAD TO ADD Timestamp  */
@@ -1100,8 +1126,8 @@
 #endif					
 
 #if defined(__WINDOWS__)
-	rx_hdr->wp_tdm_api_event_channel = (u_int16_t)tdm_api->tdm_chan + 1;
-	aft_te1_insert_tdm_api_event_in_to_rx_queue(tdm_api, rx_hdr);
+	pevent->channel = (u_int16_t)tdm_api->tdm_chan;
+	aft_te1_insert_tdm_api_event_in_to_rx_queue(tdm_api, pevent);
 	queue_tdm_api_rx_dpc(tdm_api);
 #else
 	wan_skb_queue_tail(&tdm_api->wp_event_list,skb);
@@ -1111,42 +1137,40 @@
 static void wanpipe_tdm_api_fe_alarm_event(wanpipe_tdm_api_dev_t *tdm_api, int state)
 {
 	netskb_t       		*skb;
-	wp_tdm_api_rx_hdr_t	*rx_hdr = NULL;
+	wp_tdm_api_event_t	*pevent = NULL;
 #if defined(__WINDOWS__)
-	wp_tdm_api_rx_hdr_t tdm_api_hdr;
+	wp_tdm_api_event_t	event;
 #endif
 
-	DEBUG_TEST("%s: TDM API State Event State=%i\n",
+	DEBUG_TDMAPI("%s: TDM API State Event State=%i\n",
 			tdm_api->name, tdm_api->state);
 
 #if defined(__WINDOWS__)
-	rx_hdr = &tdm_api_hdr;
+	pevent = &event;
 #else
 	if (wan_skb_queue_len(&tdm_api->wp_event_list) > WP_TDM_MAX_EVENT_Q_LEN) {	
 		return;
 	}
-	skb=wan_skb_alloc(sizeof(wp_tdm_api_rx_hdr_t));
+	skb=wan_skb_alloc(sizeof(wp_tdm_api_event_t));
 	if (skb == NULL) { 
 		return;
 	}
-	rx_hdr=(wp_tdm_api_rx_hdr_t*)wan_skb_put(skb,sizeof(wp_tdm_api_rx_hdr_t));
+	pevent = (wp_tdm_api_event_t*)wan_skb_put(skb,sizeof(wp_tdm_api_event_t));
 #endif/* #if !defined(__WINDOWS__) */
 				
-	memset(rx_hdr,0,sizeof(wp_tdm_api_rx_hdr_t));
-	if (state == WAN_CONNECTED) {
-		rx_hdr->wp_tdm_api_event_fe_alarm = 0;
-	} else {
-         	rx_hdr->wp_tdm_api_event_fe_alarm = 1;
-	}
-	rx_hdr->wp_tdm_api_event_type = WP_TDM_EVENT_FE_ALARM;
+	memset(pevent,0,sizeof(wp_tdm_api_event_t));
+	pevent->wp_tdm_api_event_type = WP_TDMAPI_EVENT_ALARM;
+	pevent->wp_tdm_api_event_alarm = (state == WAN_CONNECTED) ? 0 : 1;
 
 #if 0
 	/* FIXME: NENAD TO ADD Timestamp  */
 	rx_hdr->event_time_stamp = gettimeofday();
 #endif					
-	rx_hdr->wp_tdm_api_event_channel = (u_int16_t)tdm_api->tdm_chan + 1;
+	
+	pevent->channel = (u_int16_t)tdm_api->tdm_chan;
+
 #if defined(__WINDOWS__)
-	aft_te1_insert_tdm_api_event_in_to_rx_queue(tdm_api, rx_hdr);
+	aft_te1_insert_tdm_api_event_in_to_rx_queue(tdm_api, pevent);
 	queue_tdm_api_rx_dpc(tdm_api);
 #else
 	wan_skb_queue_tail(&tdm_api->wp_event_list,skb);
@@ -1163,8 +1187,7 @@
 	u32 cmd;
 	wanpipe_codec_ops_t *wp_codec_ops;
 	netskb_t *skb;
-	wan_event_ctrl_t	event_ctrl;
-	sdla_t *card = (sdla_t*)tdm_api->card;
+       	sdla_t *card = (sdla_t*)tdm_api->card;  
 
 	utdmapi = (wanpipe_tdm_api_cmd_t*)ifr->ifr_data;
 	
@@ -1197,7 +1220,10 @@
 		case SIOC_WP_TDM_GET_USR_MTU_MRU:
 		case SIOC_WP_TDM_GET_STATS:
 		case SIOC_WP_TDM_GET_FULL_CFG:
-		case SIOC_WP_TDM_READ_EVENT:
+		case SIOC_WP_TDM_GET_FE_STATUS:
+		case SIOC_WP_TDM_SET_FE_STATUS:
+                case SIOC_WP_TDM_READ_EVENT:
+                case SIOC_WP_TDM_GET_FE_ALARMS:
 			break;
 		default:
 			DEBUG_EVENT("%s: Invalid TDM API HDLC CMD %i\n", tdm_api->name,cmd);
@@ -1308,24 +1334,6 @@
 		}
 		break;
 
-	case SIOC_WP_TDM_ENABLE_HWEC:
-		if (card->wandev.ec_enable) {
-			wan_smp_flag_t smp_flags1;
-			card->hw_iface.hw_lock(card->hw,&smp_flags1);   
-	               	card->wandev.ec_enable(card, 1, tdm_api->tdm_chan-1);
-			card->hw_iface.hw_unlock(card->hw,&smp_flags1);   
-		}
-		break;
-
-	case SIOC_WP_TDM_DISABLE_HWEC:
-		if (card->wandev.ec_enable) {   
-			wan_smp_flag_t smp_flags1;
-			card->hw_iface.hw_lock(card->hw,&smp_flags1);   
-               	card->wandev.ec_enable(card, 0, tdm_api->tdm_chan-1);
-			card->hw_iface.hw_unlock(card->hw,&smp_flags1);   
-		}
-		break;	
-
 	case SIOC_WP_TDM_SET_EC_TAP:
 
 		switch (usr_tdm_api.ec_tap){
@@ -1347,6 +1355,33 @@
 	case SIOC_WP_TDM_GET_EC_TAP:
 		usr_tdm_api.ec_tap = tdm_api->cfg.ec_tap;
 		break;
+
+
+	case SIOC_WP_TDM_ENABLE_HWEC:
+		if (card->wandev.ec_enable) {
+			wan_smp_flag_t smp_flags1;
+			card->hw_iface.hw_lock(card->hw,&smp_flags1);   
+                	card->wandev.ec_enable(card, 1, tdm_api->tdm_chan);
+			card->hw_iface.hw_unlock(card->hw,&smp_flags1);   
+		}
+		break;
+
+	case SIOC_WP_TDM_DISABLE_HWEC:
+		if (card->wandev.ec_enable) {
+			wan_smp_flag_t smp_flags1;
+			card->hw_iface.hw_lock(card->hw,&smp_flags1);   
+                	card->wandev.ec_enable(card, 0, tdm_api->tdm_chan);
+			card->hw_iface.hw_unlock(card->hw,&smp_flags1);   
+		}
+		break;	
+	
+	case SIOC_WP_TDM_GET_HW_DTMF:
+		if (card->wandev.ec_enable && card->u.aft.tdmv_hw_dtmf == WANOPT_YES) {
+			usr_tdm_api.hw_dtmf = WANOPT_YES;
+		} else {
+			usr_tdm_api.hw_dtmf = WANOPT_NO;
+		}
+		break;
 		
 	case SIOC_WP_TDM_GET_STATS:
 		memcpy(&usr_tdm_api.stats,&tdm_api->cfg.stats,sizeof(tdm_api->cfg.stats));
@@ -1357,6 +1392,7 @@
 		break;
 
 	case SIOC_WP_TDM_ENABLE_RBS_EVENTS:
+		/* 'usr_tdm_api.rbs_poll' is the user provided 'number of polls per second' */
 		if (usr_tdm_api.rbs_poll < 20 || usr_tdm_api.rbs_poll > 100) {
 			DEBUG_EVENT("%s: Error: Invalid RBS Poll Count Min=20 Max=100\n",
 					tdm_api->name);
@@ -1365,271 +1401,62 @@
 		usr_tdm_api.rbs_poll=HZ/usr_tdm_api.rbs_poll;
 		
 		tdm_api->cfg.rbs_poll = usr_tdm_api.rbs_poll;
+		if (card->wandev.fe_iface.set_fe_sigctrl){
+                 	card->wandev.fe_iface.set_fe_sigctrl(
+					&card->fe,
+					WAN_TE_SIG_POLL,
+					ENABLE_ALL_CHANNELS,
+					WAN_ENABLE);
+		}
 		break;
 		
 	case SIOC_WP_TDM_DISABLE_RBS_EVENTS:	
 		tdm_api->cfg.rbs_poll=0;
-		break;
-
-	case SIOC_WP_TDM_ENABLE_DTMF_EVENTS:
-		// Octasic DTMF event
-		DEBUG_TDMAPI("%s: Enable HW EC DTMF event %X!\n",
-						tdm_api->name, tdm_api->active_ch);
-		memset(&event_ctrl, 0, sizeof(wan_event_ctrl_t));
-		event_ctrl.type		= WAN_EVENT_EC_DTMF;	
-		event_ctrl.mode		= WAN_EVENT_ENABLE;
-		event_ctrl.ts_map	= tdm_api->active_ch;
-		if (tdm_api->event_ctrl){	
-			err = tdm_api->event_ctrl(tdm_api->chan, &event_ctrl);
-		}
-		break;
-		
-	case SIOC_WP_TDM_DISABLE_DTMF_EVENTS:	
-		// Octasic DTMF event
-		DEBUG_TDMAPI("%s: Disable HW EC DTMF event!\n",
-						tdm_api->name);
-		memset(&event_ctrl, 0, sizeof(wan_event_ctrl_t));
-		event_ctrl.type		= WAN_EVENT_EC_DTMF;	
-		event_ctrl.mode		= WAN_EVENT_DISABLE;
-		event_ctrl.ts_map	= tdm_api->active_ch;
-		if (tdm_api->event_ctrl){	
-			err = tdm_api->event_ctrl(tdm_api->chan, &event_ctrl);
-		}
-		break;
-				
-	case SIOC_WP_TDM_ENABLE_RM_DTMF_EVENTS:
-		// A200-Remora DTMF event
-		DEBUG_TDMAPI("%s: Enable A200-Remora DTMF event!\n",
-						tdm_api->name);
-		memset(&event_ctrl, 0, sizeof(wan_event_ctrl_t));
-		event_ctrl.type		= WAN_EVENT_RM_DTMF;	
-		event_ctrl.mode		= WAN_EVENT_ENABLE;
-		event_ctrl.mod_no	= tdm_api->tdm_chan-1;
-		if (tdm_api->event_ctrl){	
-			err = tdm_api->event_ctrl(tdm_api->chan, &event_ctrl);
-		}
-		break;
-	case SIOC_WP_TDM_DISABLE_RM_DTMF_EVENTS:	
-		// A200-Remora DTMF event
-		DEBUG_TDMAPI("%s: Disable A200-Remora DTMF event!\n",
-						tdm_api->name);
-		memset(&event_ctrl, 0, sizeof(wan_event_ctrl_t));
-		event_ctrl.type		= WAN_EVENT_RM_DTMF;	
-		event_ctrl.mode		= WAN_EVENT_DISABLE;
-		event_ctrl.mod_no	= tdm_api->tdm_chan-1;
-		if (tdm_api->event_ctrl){	
-			err = tdm_api->event_ctrl(tdm_api->chan, &event_ctrl);
-		}
-		break;
-				
-		
-	case SIOC_WP_TDM_ENABLE_RXHOOK_EVENTS:
-		DEBUG_TDMAPI("%s: Enable A200-Remora Loop Closure event!\n",
-				tdm_api->name);
-		memset(&event_ctrl, 0, sizeof(wan_event_ctrl_t));
-		event_ctrl.type		= WAN_EVENT_RM_LC;	
-		event_ctrl.mode		= WAN_EVENT_ENABLE;
-		event_ctrl.mod_no	= tdm_api->tdm_chan-1;
-		if (tdm_api->event_ctrl){	
-			err = tdm_api->event_ctrl(tdm_api->chan, &event_ctrl);
-		} else {
-			DEBUG_EVENT("%s: Error: event_ctrl not supported!\n",
-				tdm_api->name); 
-			err = -EINVAL;
+		if (card->wandev.fe_iface.set_fe_sigctrl){
+                 	card->wandev.fe_iface.set_fe_sigctrl(
+					&card->fe,
+					WAN_TE_SIG_POLL,
+					ENABLE_ALL_CHANNELS,
+					WAN_DISABLE);
 		}
 		break;
-		
-	case SIOC_WP_TDM_DISABLE_RXHOOK_EVENTS:	
-		DEBUG_TDMAPI("%s: Disable A200-Remora Loop Closure event!\n",
-				tdm_api->name);
-		memset(&event_ctrl, 0, sizeof(wan_event_ctrl_t));
-		event_ctrl.type		= WAN_EVENT_RM_LC;	
-		event_ctrl.mode		= WAN_EVENT_DISABLE;
-		event_ctrl.mod_no	= tdm_api->tdm_chan-1;
-		if (tdm_api->event_ctrl){	
-			err = tdm_api->event_ctrl(tdm_api->chan, &event_ctrl);
-		} else {
-			DEBUG_EVENT("%s: Error: event_ctrl not supported!\n",
-				tdm_api->name); 
-			err = -EINVAL;
-		}       
-		break;
-	
-	case SIOC_WP_TDM_ENABLE_RING_DETECT_EVENTS:
-		DEBUG_TDMAPI("%s: Enable Ring Detection Event on module %d!\n",
-				tdm_api->name, 
-				tdm_api->tdm_chan);
-		memset(&event_ctrl, 0, sizeof(wan_event_ctrl_t));
-		event_ctrl.type   	= WAN_EVENT_RM_RING_DETECT;
-		event_ctrl.mode		= WAN_EVENT_ENABLE;
-		event_ctrl.mod_no	= tdm_api->tdm_chan-1;
-		if (tdm_api->event_ctrl){	
-			err = tdm_api->event_ctrl(tdm_api->chan, &event_ctrl);
-		} else {
-			DEBUG_EVENT("%s: Error: event_ctrl not supported!\n",
-				tdm_api->name); 
-			err = -EINVAL;
-		}       
-		break;
-	case SIOC_WP_TDM_DISABLE_RING_DETECT_EVENTS:
-		DEBUG_TDMAPI("%s: Disable Ring Detection Event on module %d!\n",
-				tdm_api->name, 
-				tdm_api->tdm_chan);
-		memset(&event_ctrl, 0, sizeof(wan_event_ctrl_t));
-		event_ctrl.type   	= WAN_EVENT_RM_RING_DETECT;
-		event_ctrl.mode		= WAN_EVENT_DISABLE;
-		event_ctrl.mod_no	= tdm_api->tdm_chan-1;
-		if (tdm_api->event_ctrl){	
-			err = tdm_api->event_ctrl(tdm_api->chan, &event_ctrl);
-		} else {
-			DEBUG_EVENT("%s: Error: event_ctrl not supported!\n",
-				tdm_api->name); 
-			err = -EINVAL;
-		}       
-		break;
-	
-	case SIOC_WP_TDM_ENABLE_RING_TRIP_DETECT_EVENTS:
-		DEBUG_TDMAPI("%s: Enable Ring Trip Detection Event on module %d!\n",
-				tdm_api->name, 
-				tdm_api->tdm_chan);
-		memset(&event_ctrl, 0, sizeof(wan_event_ctrl_t));
-		event_ctrl.type   	= WAN_EVENT_RM_RING_TRIP;
-		event_ctrl.mode		= WAN_EVENT_ENABLE;
-		event_ctrl.mod_no	= tdm_api->tdm_chan-1;
-		if (tdm_api->event_ctrl){	
-			err = tdm_api->event_ctrl(tdm_api->chan, &event_ctrl);
-		} else {
-			DEBUG_EVENT("%s: Error: event_ctrl not supported!\n",
-				tdm_api->name); 
-			err = -EINVAL;
-		}       
-		break;
-	case SIOC_WP_TDM_DISABLE_RING_TRIP_DETECT_EVENTS:
-		DEBUG_TDMAPI("%s: Disable Ring Trip Detection Event on module %d!\n",
-				tdm_api->name, 
-				tdm_api->tdm_chan);
-		memset(&event_ctrl, 0, sizeof(wan_event_ctrl_t));
-		event_ctrl.type   	= WAN_EVENT_RM_RING_TRIP;
-		event_ctrl.mode		= WAN_EVENT_DISABLE;
-		event_ctrl.mod_no	= tdm_api->tdm_chan-1;
-		if (tdm_api->event_ctrl){	
-			err = tdm_api->event_ctrl(tdm_api->chan, &event_ctrl);
-		} else {
-			DEBUG_EVENT("%s: Error: event_ctrl not supported!\n",
-				tdm_api->name); 
-			err = -EINVAL;
-		}       
-		break;
-	
-	case SIOC_WP_TDM_TXSIG_KEWL:
-		DEBUG_TDMAPI("%s: TX Signalling KEWL on module %d!\n",
-				tdm_api->name, 
-				tdm_api->tdm_chan);
-		memset(&event_ctrl, 0, sizeof(wan_event_ctrl_t));
-		event_ctrl.type   	= WAN_EVENT_RM_TXSIG_KEWL;	
-		event_ctrl.mod_no	= tdm_api->tdm_chan-1;
-		if (tdm_api->event_ctrl){	
-			err = tdm_api->event_ctrl(tdm_api->chan, &event_ctrl);
-		} else {
-			DEBUG_EVENT("%s: Error: event_ctrl not supported!\n",
-				tdm_api->name); 
-			err = -EINVAL;
-		}       
-		break;
-	
-	case SIOC_WP_TDM_EVENT_TXSIG_START:
-		DEBUG_TDMAPI("%s: TX Signalling START for module %d!\n",
-				tdm_api->name, 
-				tdm_api->tdm_chan);
-		memset(&event_ctrl, 0, sizeof(wan_event_ctrl_t));
-		event_ctrl.type		= WAN_EVENT_RM_TXSIG_START;
-		event_ctrl.mod_no	= tdm_api->tdm_chan-1;
-		if (tdm_api->event_ctrl){	
-			err = tdm_api->event_ctrl(tdm_api->chan, &event_ctrl);
-		} else {
-			DEBUG_EVENT("%s: Error: event_ctrl not supported!\n",
-				tdm_api->name); 
-			err = -EINVAL;
-		}       
-		break;
-	case SIOC_WP_TDM_EVENT_TXSIG_OFFHOOK:
-		DEBUG_TDMAPI("%s: TX Signalling OFFHOOK for module %d!\n",
-				tdm_api->name, 
-				tdm_api->tdm_chan);
-		memset(&event_ctrl, 0, sizeof(wan_event_ctrl_t));
-		event_ctrl.type		= WAN_EVENT_RM_TXSIG_OFFHOOK;
-		event_ctrl.mod_no	= tdm_api->tdm_chan-1;
-		if (tdm_api->event_ctrl){	
-			err = tdm_api->event_ctrl(tdm_api->chan, &event_ctrl);
-		} else {
-			DEBUG_EVENT("%s: Error: event_ctrl not supported!\n",
-				tdm_api->name); 
-			err = -EINVAL;
-		}       
-		break;
-	case SIOC_WP_TDM_EVENT_TXSIG_ONHOOK:
-		DEBUG_TDMAPI("%s: TX Signalling ONHOOK for module %d!\n",
-				tdm_api->name, 
-				tdm_api->tdm_chan);
-		memset(&event_ctrl, 0, sizeof(wan_event_ctrl_t));
-		event_ctrl.type   = WAN_EVENT_RM_TXSIG_ONHOOK;
-		event_ctrl.mod_no	= tdm_api->tdm_chan-1;
-		if (tdm_api->event_ctrl){	
-			err = tdm_api->event_ctrl(tdm_api->chan, &event_ctrl);
-		} else {
-			DEBUG_EVENT("%s: Error: event_ctrl not supported!\n",
-				tdm_api->name); 
-			err = -EINVAL;
-		}       
-		break;
-	case SIOC_WP_TDM_EVENT_ONHOOKTRANSFER:
-		DEBUG_TDMAPI("%s: RM ONHOOKTRANSFER for module %d!\n",
-				tdm_api->name, 
-				tdm_api->tdm_chan);
-		memset(&event_ctrl, 0, sizeof(wan_event_ctrl_t));
-		event_ctrl.type   = WAN_EVENT_RM_ONHOOKTRANSFER;
-		event_ctrl.mod_no	= tdm_api->tdm_chan-1;
-		event_ctrl.ohttimer	= usr_tdm_api.event.wp_tdm_api_event_ohttimer;
-		if (tdm_api->event_ctrl){	
-			err = tdm_api->event_ctrl(tdm_api->chan, &event_ctrl);
-		} else {
-			DEBUG_EVENT("%s: Error: event_ctrl not supported!\n",
-				tdm_api->name); 
-			err = -EINVAL;
-		}       
-		break;
-	case SIOC_WP_TDM_EVENT_SETPOLARITY:
-		DEBUG_EVENT("%s: RM SETPOLARITY for module %d!\n",
-				tdm_api->name, 
-				tdm_api->tdm_chan);
-		event_ctrl.type		= WAN_EVENT_RM_SETPOLARITY;
-		event_ctrl.mod_no	= tdm_api->tdm_chan-1;
-		event_ctrl.polarity	= usr_tdm_api.event.wp_tdm_api_event_polarity;
-		if (tdm_api->event_ctrl){
-			err = tdm_api->event_ctrl(tdm_api->chan, &event_ctrl);
-		} else {
-			DEBUG_EVENT("%s: Error: event_ctrl not supported!\n",
-				tdm_api->name); 
-			err = -EINVAL;
-		}       
-		break;
-		
+
 	case SIOC_WP_TDM_WRITE_RBS_BITS:
 		wan_spin_unlock(&tdm_api->lock);
 		 
 		err=tdm_api->write_rbs_bits(
 						tdm_api->chan, 
-#if defined(__WINDOWS__)
-					    tdm_api->tdm_chan + 1, 
-#else
 					    tdm_api->tdm_chan,
-#endif
 					    (u8)usr_tdm_api.rbs_tx_bits);
 		if (err) {
 			DEBUG_EVENT("%s: WRITE RBS Error (%i)\n",tdm_api->name,err);
 		}
 		goto tdm_api_unlocked_exit;
 		break;
+
+	case SIOC_WP_TDM_GET_FE_STATUS:
+		if (card->wandev.fe_iface.get_fe_status){
+			wan_smp_flag_t smp_flags1;
+			card->hw_iface.hw_lock(card->hw,&smp_flags1);   
+                 	card->wandev.fe_iface.get_fe_status(
+					&card->fe, &usr_tdm_api.fe_status);
+			card->hw_iface.hw_unlock(card->hw,&smp_flags1);   
+		}
+		break;
+		
+	case SIOC_WP_TDM_SET_FE_STATUS:	
+		if (card->wandev.fe_iface.set_fe_status){
+			wan_smp_flag_t smp_flags1;
+			card->hw_iface.hw_lock(card->hw,&smp_flags1);   
+                 	card->wandev.fe_iface.set_fe_status(
+					&card->fe, usr_tdm_api.fe_status);
+			card->hw_iface.hw_unlock(card->hw,&smp_flags1);   
+		}
+		break;
+
+	case SIOC_WP_TDM_SET_EVENT:
+		err = wanpipe_tdm_api_event_ioctl(tdm_api, &usr_tdm_api);
+		break;
 		
 	case SIOC_WP_TDM_READ_EVENT:
 		skb=wan_skb_dequeue(&tdm_api->wp_event_list);
@@ -1637,7 +1464,7 @@
 			err=-ENOBUFS;
 			break;
 		}
-		memcpy(&usr_tdm_api.event,wan_skb_data(skb),sizeof(wp_tdm_api_rx_hdr_t));
+		memcpy(&usr_tdm_api.event,wan_skb_data(skb),sizeof(wp_tdm_api_event_t));
 		wan_skb_free(skb);
 		break;
 
@@ -1650,7 +1477,6 @@
 		if (usr_tdm_api.data_len && utdmapi->data) {
                         if (usr_tdm_api.data_len != 256) {
                          	err=-EINVAL;
-			
 				break;
 			}
 			
@@ -1665,7 +1491,8 @@
 			}
                 	
 #if defined(__WINDOWS__)
-			memcpy(&usr_tdm_api, ifr, sizeof(wanpipe_tdm_api_cmd_t));
+			/*FIXME: test the memcpy() here
+			memcpy(rx_gains, utdmapi->data, usr_tdm_api.data_len);*/
 #else
        			if (WAN_COPY_FROM_USER(rx_gains,
 					       utdmapi->data,
@@ -1707,9 +1534,8 @@
 			}
                 	
 #if defined(__WINDOWS__)
-//FIXME: implement
-//#error "FIX API CMD"
-//			memcpy(&_api, ifr, sizeof(wanpipe_tdm_api_cmd_t));
+			/*FIXME: test the memcpy() here
+			memcpy(tx_gains, utdmapi->data, usr_tdm_api.data_len);*/
 #else
        			if (WAN_COPY_FROM_USER(tx_gains,
 					       utdmapi->data,
@@ -1759,7 +1585,237 @@
 	return err;
 }
 
+static int
+wanpipe_tdm_api_event_ioctl(wanpipe_tdm_api_dev_t *tdm_api, wanpipe_tdm_api_cmd_t *tdm_cmd)
+{
+	wp_tdm_api_event_t	*tdm_event;
+	wan_event_ctrl_t	event_ctrl;
+
+	if (tdm_api->event_ctrl == NULL){	
+		DEBUG_EVENT("%s: Error: Event control interface doesn't initialized!\n",
+				tdm_api->name); 
+		return -EINVAL;
+	}
+		
+	tdm_event = &tdm_cmd->event;
+	memset(&event_ctrl, 0, sizeof(wan_event_ctrl_t));
 
+	switch(tdm_event->wp_tdm_api_event_type){		
+	case WP_TDMAPI_EVENT_DTMF:
+		// Octasic DTMF event
+		DEBUG_TDMAPI("%s: %s HW EC DTMF event %X!\n",
+			tdm_api->name, 
+			WP_TDMAPI_EVENT_MODE_DECODE(tdm_event->wp_tdm_api_event_mode),
+			tdm_api->active_ch);
+		event_ctrl.type		= WAN_EVENT_EC_DTMF;	
+		if (tdm_event->wp_tdm_api_event_mode == WP_TDMAPI_EVENT_ENABLE){
+			event_ctrl.mode = WAN_EVENT_ENABLE;	
+		}else{
+			event_ctrl.mode = WAN_EVENT_DISABLE;	
+		}
+#if defined(__WINDOWS__)
+		if(tdm_event->channel < 1 || tdm_event->channel > NUM_OF_E1_CHANNELS - 1/* 31 */){
+			DEBUG_TDMAPI("%s(): %s: Warning: DTMF control requested on invalid channel %u!\n", 
+				__FUNCTION__, tdm_api->name, tdm_event->channel);
+			tdm_event->channel = 1;/* */
+		}
+		event_ctrl.channel	= tdm_event->channel;
+#else
+		event_ctrl.channel	= tdm_api->tdm_chan;
+#endif
+		break;
+		
+	case WP_TDMAPI_EVENT_RM_DTMF:
+		// A200-Remora DTMF event
+		DEBUG_TDMAPI("%s: %s A200-Remora DTMF event!\n",
+			tdm_api->name, 
+			WP_TDMAPI_EVENT_MODE_DECODE(tdm_event->wp_tdm_api_event_mode));
+		event_ctrl.type		= WAN_EVENT_RM_DTMF;	
+		if (tdm_event->wp_tdm_api_event_mode == WP_TDMAPI_EVENT_ENABLE){
+			event_ctrl.mode = WAN_EVENT_ENABLE;	
+		}else{
+			event_ctrl.mode = WAN_EVENT_DISABLE;	
+		}
+		event_ctrl.mod_no	= tdm_api->tdm_chan;
+		break;			
+		
+	case WP_TDMAPI_EVENT_RXHOOK:
+		DEBUG_TDMAPI("%s: %s A200-Remora Loop Closure event!\n",
+			tdm_api->name,
+			WP_TDMAPI_EVENT_MODE_DECODE(tdm_event->wp_tdm_api_event_mode));
+		event_ctrl.type		= WAN_EVENT_RM_LC;	
+		if (tdm_event->wp_tdm_api_event_mode == WP_TDMAPI_EVENT_ENABLE){
+			event_ctrl.mode = WAN_EVENT_ENABLE;	
+		}else{
+			event_ctrl.mode = WAN_EVENT_DISABLE;	
+		}
+		event_ctrl.mod_no	= tdm_api->tdm_chan;
+		break;
+		
+	case WP_TDMAPI_EVENT_RING:
+		DEBUG_TDMAPI("%s: %s Ring Event on module %d!\n",
+			tdm_api->name,
+			WP_TDMAPI_EVENT_MODE_DECODE(tdm_event->wp_tdm_api_event_mode),
+			tdm_api->tdm_chan);
+		event_ctrl.type   	= WAN_EVENT_RM_RING;
+		if (tdm_event->wp_tdm_api_event_mode == WP_TDMAPI_EVENT_ENABLE){
+			event_ctrl.mode = WAN_EVENT_ENABLE;	
+		}else{
+			event_ctrl.mode = WAN_EVENT_DISABLE;	
+		}
+		event_ctrl.mod_no	= tdm_api->tdm_chan;
+		break;
+	
+	case WP_TDMAPI_EVENT_RING_DETECT:
+		DEBUG_TDMAPI("%s: %s Ring Detection Event on module %d!\n",
+			tdm_api->name,
+			WP_TDMAPI_EVENT_MODE_DECODE(tdm_event->wp_tdm_api_event_mode),
+			tdm_api->tdm_chan);
+		event_ctrl.type   	= WAN_EVENT_RM_RING_DETECT;
+		if (tdm_event->wp_tdm_api_event_mode == WP_TDMAPI_EVENT_ENABLE){
+			event_ctrl.mode = WAN_EVENT_ENABLE;	
+		}else{
+			event_ctrl.mode = WAN_EVENT_DISABLE;	
+		}
+		event_ctrl.mod_no	= tdm_api->tdm_chan;
+		break;
+		
+	case WP_TDMAPI_EVENT_RING_TRIP_DETECT:
+		DEBUG_TDMAPI("%s: %s Ring Trip Detection Event on module %d!\n",
+			tdm_api->name,
+			WP_TDMAPI_EVENT_MODE_DECODE(tdm_event->wp_tdm_api_event_mode),
+			tdm_api->tdm_chan);
+		event_ctrl.type   	= WAN_EVENT_RM_RING_TRIP;
+		if (tdm_event->wp_tdm_api_event_mode == WP_TDMAPI_EVENT_ENABLE){
+			event_ctrl.mode = WAN_EVENT_ENABLE;	
+		}else{
+			event_ctrl.mode = WAN_EVENT_DISABLE;	
+		}
+		event_ctrl.mod_no	= tdm_api->tdm_chan;
+		break;
+		
+	case WP_TDMAPI_EVENT_TONE:
+
+		DEBUG_TDMAPI("%s: %s Tone Event (%d)on module %d!\n",
+			tdm_api->name, 
+			WP_TDMAPI_EVENT_MODE_DECODE(tdm_event->wp_tdm_api_event_mode),
+			tdm_event->wp_tdm_api_event_tone_type,
+			tdm_api->tdm_chan);
+		event_ctrl.type   	= WAN_EVENT_RM_TONE;
+		if (tdm_event->wp_tdm_api_event_mode == WP_TDMAPI_EVENT_ENABLE){
+			event_ctrl.mode = WAN_EVENT_ENABLE;	
+			switch(tdm_event->wp_tdm_api_event_tone_type){
+			case WP_TDMAPI_EVENT_TONE_DIAL:
+				event_ctrl.tone	= WAN_EVENT_TONE_DIAL;
+				break;
+			case WP_TDMAPI_EVENT_TONE_BUSY:
+				event_ctrl.tone	= WAN_EVENT_TONE_BUSY;
+				break;
+			case WP_TDMAPI_EVENT_TONE_RING:
+				event_ctrl.tone	= WAN_EVENT_TONE_RING;
+				break;
+			case WP_TDMAPI_EVENT_TONE_CONGESTION:
+				event_ctrl.tone	= WAN_EVENT_TONE_CONGESTION;
+				break;
+			default:
+				DEBUG_EVENT("%s: Unsupported TDM API Tone Type  %d!\n",
+						tdm_api->name, 
+						tdm_event->wp_tdm_api_event_tone_type);
+				return -EINVAL;
+			}
+		}else{
+			event_ctrl.mode = WAN_EVENT_DISABLE;	
+		}
+		event_ctrl.mod_no	= tdm_api->tdm_chan;
+		break;
+		
+	case WP_TDMAPI_EVENT_TXSIG_KEWL:
+		DEBUG_TDMAPI("%s: TX Signalling KEWL on module %d!\n",
+				tdm_api->name, tdm_api->tdm_chan);
+		event_ctrl.type   	= WAN_EVENT_RM_TXSIG_KEWL;	
+		event_ctrl.mod_no	= tdm_api->tdm_chan;
+		break;
+	
+	case WP_TDMAPI_EVENT_TXSIG_START:
+		DEBUG_TDMAPI("%s: TX Signalling START for module %d!\n",
+				tdm_api->name, tdm_api->tdm_chan);
+		event_ctrl.type		= WAN_EVENT_RM_TXSIG_START;
+		event_ctrl.mod_no	= tdm_api->tdm_chan;
+		break;
+	
+	case WP_TDMAPI_EVENT_TXSIG_OFFHOOK:
+		DEBUG_TDMAPI("%s: TX Signalling OFFHOOK for module %d!\n",
+				tdm_api->name, tdm_api->tdm_chan);
+		event_ctrl.type		= WAN_EVENT_RM_TXSIG_OFFHOOK;
+		event_ctrl.mod_no	= tdm_api->tdm_chan;
+		break;
+	
+	case WP_TDMAPI_EVENT_TXSIG_ONHOOK:
+		DEBUG_TDMAPI("%s: TX Signalling ONHOOK for module %d!\n",
+				tdm_api->name, tdm_api->tdm_chan);
+		event_ctrl.type		= WAN_EVENT_RM_TXSIG_ONHOOK;
+		event_ctrl.mod_no	= tdm_api->tdm_chan;
+		break;
+	
+	case WP_TDMAPI_EVENT_ONHOOKTRANSFER:
+		DEBUG_TDMAPI("%s: RM ONHOOKTRANSFER for module %d!\n",
+				tdm_api->name, tdm_api->tdm_chan);
+		event_ctrl.type		= WAN_EVENT_RM_ONHOOKTRANSFER;
+		event_ctrl.mod_no	= tdm_api->tdm_chan;
+		event_ctrl.ohttimer	= tdm_event->wp_tdm_api_event_ohttimer;
+		break;
+	
+	case WP_TDMAPI_EVENT_SETPOLARITY:
+		DEBUG_EVENT("%s: RM SETPOLARITY for module %d!\n",
+				tdm_api->name, tdm_api->tdm_chan);
+		event_ctrl.type		= WAN_EVENT_RM_SETPOLARITY;
+		event_ctrl.mod_no	= tdm_api->tdm_chan;
+		event_ctrl.polarity	= tdm_event->wp_tdm_api_event_polarity;
+		break;
+
+	case WP_TDMAPI_EVENT_BRI_CHAN_LOOPBACK:
+		event_ctrl.type		= WAN_EVENT_BRI_CHAN_LOOPBACK;
+		event_ctrl.channel	= tdm_event->channel;
+
+		if (tdm_event->wp_tdm_api_event_mode == WP_TDMAPI_EVENT_ENABLE){
+			event_ctrl.mode = WAN_EVENT_ENABLE;	
+		}else{
+			event_ctrl.mode = WAN_EVENT_DISABLE;	
+		}
+
+		DEBUG_TDMAPI("%s: BRI_BCHAN_LOOPBACK: %s for channel %d!\n",
+			tdm_api->name, 
+			(event_ctrl.mode == WAN_EVENT_ENABLE ? "Enable" : "Disable"),
+			event_ctrl.channel);
+		break;
+
+	default:
+		DEBUG_EVENT("%s: Unknown TDM API Event Type %02X!\n",
+				tdm_api->name,
+				tdm_event->type);
+		return -EINVAL;
+	}
+
+
+	switch(tdm_event->wp_tdm_api_event_type){
+	case WP_TDMAPI_EVENT_BRI_CHAN_LOOPBACK:
+		/* BRI FE access must be locked */
+		{
+			sdla_t *card = (sdla_t*)tdm_api->card;
+			wan_smp_flag_t smp_flags1;
+			int rc;
+
+			card->hw_iface.hw_lock(card->hw,&smp_flags1);   
+			rc = tdm_api->event_ctrl(tdm_api->chan, &event_ctrl);
+			card->hw_iface.hw_unlock(card->hw,&smp_flags1);   
+			return rc;
+		}
+		break;
+
+	default:
+		return tdm_api->event_ctrl(tdm_api->chan, &event_ctrl);
+	}
+}
 
 static int wanpipe_tdm_api_tx (wanpipe_tdm_api_dev_t *tdm_api, u8 *tx_data, int len)
 {
@@ -1947,6 +2003,8 @@
 		return -EBUSY;
 	}
 	
+	DEBUG_TDMAPI("%s: TDM API RX HDLC FRAME %i\n",tdm_api->name, wan_skb_len(skb));
+
 	wan_skb_queue_tail(&tdm_api->wp_rx_list,skb);
 	wp_wakeup_tdmapi(tdm_api);
 	tdm_api->cfg.stats.rx_packets++;
@@ -1960,7 +2018,10 @@
 	int	i = 0;
 	
 #if defined(__WINDOWS__)
-	if(fe_chan >= MAX_TDM_API_CHANNELS){
+
+	DEBUG_TDMAPI("%s(): fe_chan: %d\n", __FUNCTION__, fe_chan);
+
+	if(fe_chan < 0 || fe_chan >= MAX_TDM_API_CHANNELS){
 		DEBUG_EVENT("%s(): TDM API Error: Invalid Channel Number=%i!\n",
 			__FUNCTION__, fe_chan);
 		return NULL;
@@ -1973,16 +2034,28 @@
 	}
 	return tdm_api;
 #else
+	int tmp_fe_chan;
+	tmp_fe_chan = fe_chan;
+
 	for(i = 0; i < WP_TDMAPI_HASH_SZ; i++){
 		tdm_api = wp_tdmapi_hash[i];
 		
 		if (tdm_api == NULL || tdm_api->card != card){
 			continue;
 		}
-		if (wan_test_bit(fe_chan, &tdm_api->active_ch)){
+		
+		if (IS_BRI_CARD(card)) {
+			tmp_fe_chan = (2*(tdm_api->tdm_span-1))+fe_chan;
+		} 	
+
+		if (wan_test_bit(tmp_fe_chan, &tdm_api->active_ch)){
 			return tdm_api;		 
 		}
 	}
+	DEBUG_EVENT("%s: Error: Received EVENT on invalid chan:%d\n", 
+			card->devname,			
+			fe_chan);
+
 	return NULL;
 #endif
 }
@@ -2007,62 +2080,57 @@
 
 static void wp_tdmapi_dtmf (void* card_id, wan_event_t *event)
 {
-	netskb_t		*skb = NULL;
+	netskb_t			*skb = NULL;
 	wanpipe_tdm_api_dev_t	*tdm_api = NULL;
-	sdla_t			*card = (sdla_t*)card_id;
-	wp_tdm_api_rx_hdr_t	*rx_hdr = NULL;
+	sdla_t				*card = (sdla_t*)card_id;
+	wp_tdm_api_event_t	*p_tdmapi_event = NULL;
 #if defined(__WINDOWS__)
-	wp_tdm_api_rx_hdr_t tdm_api_hdr;
+	wp_tdm_api_event_t	tdmapi_event;
 #endif
 
 	if (event->type == WAN_EVENT_EC_DTMF){
-		DEBUG_TEST("%s: Received DTMF Event at TDM API (%d:%c:%s:%s)!\n",
+		DEBUG_TDMAPI("%s: Received DTMF Event at TDM API (%d:%c:%s:%s)!\n",
 			card->devname,
 			event->channel,
 			event->digit,
 			(event->dtmf_port == WAN_EC_CHANNEL_PORT_ROUT)?"ROUT":"SOUT",
 			(event->dtmf_type == WAN_EC_TONE_PRESENT)?"PRESENT":"STOP");
 	}else if (event->type == WAN_EVENT_RM_DTMF){
-		DEBUG_TEST("%s: Received DTMF Event at TDM API (%d:%c)!\n",
+		DEBUG_TDMAPI("%s: Received DTMF Event at TDM API (%d:%c)!\n",
 			card->devname,
 			event->channel,
 			event->digit);	
 	}
 					
-	tdm_api = wp_tdmapi_search(card, 
-#if defined(__WINDOWS__)
-			event->channel - 1);
-#else
-			event->channel);
-#endif
+	tdm_api = wp_tdmapi_search(card, event->channel);
 	if (tdm_api == NULL){
 		return;
 	}
 
 #if defined(__WINDOWS__)
-	rx_hdr = &tdm_api_hdr;
+	p_tdmapi_event = &tdmapi_event;
 #else
 	if (wan_skb_queue_len(&tdm_api->wp_event_list) > WP_TDM_MAX_EVENT_Q_LEN) {
 		return;
 	}
-	skb=wan_skb_alloc(sizeof(wp_tdm_api_rx_hdr_t));
+	skb=wan_skb_alloc(sizeof(wp_tdm_api_event_t));
 	if (skb == NULL) return;
 	
-	rx_hdr=(wp_tdm_api_rx_hdr_t*)wan_skb_put(skb,sizeof(wp_tdm_api_rx_hdr_t));
+	p_tdmapi_event = (wp_tdm_api_event_t*)wan_skb_put(skb,sizeof(wp_tdm_api_event_t));
 #endif
 
-	memset(rx_hdr,0,sizeof(wp_tdm_api_rx_hdr_t));
-	rx_hdr->wp_tdm_api_event_type		= WP_TDM_EVENT_DTMF;
-	rx_hdr->wp_tdm_api_event_dtmf_digit	= event->digit;
-	rx_hdr->wp_tdm_api_event_dtmf_type	= event->dtmf_type;
-	rx_hdr->wp_tdm_api_event_dtmf_port	= event->dtmf_port;
+	memset(p_tdmapi_event,0,sizeof(wp_tdm_api_event_t));
+	p_tdmapi_event->type				= WP_TDMAPI_EVENT_DTMF;
+	p_tdmapi_event->wp_tdm_api_event_dtmf_digit	= event->digit;
+	p_tdmapi_event->wp_tdm_api_event_dtmf_type	= event->dtmf_type;
+	p_tdmapi_event->wp_tdm_api_event_dtmf_port	= event->dtmf_port;
 #if 0
 	rx_hdr->event_time_stamp = gettimeofday();
 #endif					
 
 #if defined(__WINDOWS__)
-	rx_hdr->wp_tdm_api_event_channel = (u_int16_t)event->channel; 
-	aft_te1_insert_tdm_api_event_in_to_rx_queue(tdm_api, rx_hdr);
+	p_tdmapi_event->channel = (u_int16_t)event->channel; 
+	aft_te1_insert_tdm_api_event_in_to_rx_queue(tdm_api, p_tdmapi_event);
 	queue_tdm_api_rx_dpc(tdm_api);
 #else
 	wan_skb_queue_tail(&tdm_api->wp_event_list,skb);
@@ -2076,50 +2144,53 @@
 	netskb_t		*skb;
 	wanpipe_tdm_api_dev_t	*tdm_api = NULL;
 	sdla_t			*card = (sdla_t*)card_id;
-	wp_tdm_api_rx_hdr_t	*rx_hdr = NULL;
+	wp_tdm_api_event_t	*p_tdmapi_event = NULL;
 #if defined(__WINDOWS__)
-	wp_tdm_api_rx_hdr_t tdm_api_hdr;
+	wp_tdm_api_event_t	tdmapi_event;
 #endif
 	
 	DEBUG_TDMAPI("%s: Received RM LC Event at TDM_API (%d:%s)!\n",
 			card->devname,
 			event->channel,
 			(event->rxhook==WAN_EVENT_RXHOOK_OFF)?"OFF-HOOK":"ON-HOOK");
-			
 
-	tdm_api = wp_tdmapi_search(card, 
-#if defined(__WINDOWS__)
-			event->channel - 1);
-#else
-			event->channel);
-#endif
+	tdm_api = wp_tdmapi_search(card, event->channel);
 	if (tdm_api == NULL){
 		return;
 	}
 					
 #if defined(__WINDOWS__)
-	rx_hdr = &tdm_api_hdr;
+	p_tdmapi_event = &tdmapi_event;
 #else
 	if (wan_skb_queue_len(&tdm_api->wp_event_list) > WP_TDM_MAX_EVENT_Q_LEN) {
 		return;
 	}
-	skb=wan_skb_alloc(sizeof(wp_tdm_api_rx_hdr_t));
+	skb=wan_skb_alloc(sizeof(wp_tdm_api_event_t));
 	if (skb == NULL) return;
 	
-	rx_hdr=(wp_tdm_api_rx_hdr_t*)wan_skb_put(skb,sizeof(wp_tdm_api_rx_hdr_t));
+	p_tdmapi_event = (wp_tdm_api_event_t*)wan_skb_put(skb,sizeof(wp_tdm_api_event_t));
 #endif
 
-	memset(rx_hdr,0,sizeof(wp_tdm_api_rx_hdr_t));
-	rx_hdr->wp_tdm_api_event_type		= WP_TDM_EVENT_RXHOOK;
-	rx_hdr->wp_tdm_api_event_channel	= (u_int16_t)event->channel;
-	rx_hdr->wp_tdm_api_event_rxhook_state	= event->rxhook;
+	memset(p_tdmapi_event, 0, sizeof(wp_tdm_api_event_t));
+	p_tdmapi_event->type	= WP_TDMAPI_EVENT_RXHOOK;
+	p_tdmapi_event->channel	= (u_int16_t)event->channel;
+	switch(event->rxhook){
+	case WAN_EVENT_RXHOOK_ON:
+		p_tdmapi_event->wp_tdm_api_event_hook_state = 
+					WP_TDMAPI_EVENT_RXHOOK_ON;
+		break;
+	case WAN_EVENT_RXHOOK_OFF:
+		p_tdmapi_event->wp_tdm_api_event_hook_state = 
+					WP_TDMAPI_EVENT_RXHOOK_OFF;
+		break;
+	}
 
 #if 0
 	rx_hdr->event_time_stamp = gettimeofday();
 #endif					
 
 #if defined(__WINDOWS__)
-	aft_te1_insert_tdm_api_event_in_to_rx_queue(tdm_api, rx_hdr);
+	aft_te1_insert_tdm_api_event_in_to_rx_queue(tdm_api, p_tdmapi_event);
 	queue_tdm_api_rx_dpc(tdm_api);
 #else
 	wan_skb_queue_tail(&tdm_api->wp_event_list,skb);
@@ -2137,52 +2208,53 @@
 */
 static void wp_tdmapi_ringtrip (void* card_id, wan_event_t *event)
 {
-	netskb_t				*skb;
+	netskb_t		*skb;
 	wanpipe_tdm_api_dev_t	*tdm_api = NULL;
-	sdla_t					*card = (sdla_t*)card_id;
-	wp_tdm_api_rx_hdr_t		*rx_hdr = NULL;
+	sdla_t			*card = (sdla_t*)card_id;
+	wp_tdm_api_event_t	*p_tdmapi_event = NULL;
 #if defined(__WINDOWS__)
-	wp_tdm_api_rx_hdr_t tdm_api_hdr;
+	wp_tdm_api_event_t	tdmapi_event;
 #endif
 	
 	DEBUG_TDMAPI("%s: Received RM RING TRIP Event at TDM_API (%d:%s)!\n",
 			card->devname,
 			event->channel,
-			WAN_EVENT_RING_TRIP_DECODE(event->rxhook));
+			WAN_EVENT_RING_TRIP_DECODE(event->ring_mode));
 	
-	tdm_api = wp_tdmapi_search(card, 
-#if defined(__WINDOWS__)
-			event->channel - 1);
-#else
-			event->channel);
-#endif
+	tdm_api = wp_tdmapi_search(card, event->channel);
 	if (tdm_api == NULL){
 		return;
 	}
 					
 #if defined(__WINDOWS__)
-	rx_hdr = &tdm_api_hdr;
+	p_tdmapi_event = &tdmapi_event;
 #else
 	if (wan_skb_queue_len(&tdm_api->wp_event_list) > WP_TDM_MAX_EVENT_Q_LEN) {
 		return;
 	}
-	skb=wan_skb_alloc(sizeof(wp_tdm_api_rx_hdr_t));
+	skb=wan_skb_alloc(sizeof(wp_tdm_api_event_t));
 	if (skb == NULL) return;
 	
-	rx_hdr=(wp_tdm_api_rx_hdr_t*)wan_skb_put(skb,sizeof(wp_tdm_api_rx_hdr_t));
+	p_tdmapi_event = (wp_tdm_api_event_t*)wan_skb_put(skb,sizeof(wp_tdm_api_event_t));
 #endif
 
-	memset(rx_hdr,0,sizeof(wp_tdm_api_rx_hdr_t));
-	rx_hdr->wp_tdm_api_event_type		= WP_TDM_EVENT_RING_TRIP;
-	rx_hdr->wp_tdm_api_event_channel	= (u_int16_t)event->channel; 
-	rx_hdr->wp_tdm_api_event_ring_state	= event->rxhook;
+	memset(p_tdmapi_event, 0, sizeof(wp_tdm_api_event_t));
+	p_tdmapi_event->type	= WP_TDMAPI_EVENT_RING_TRIP_DETECT;
+	p_tdmapi_event->channel	= (u_int16_t)event->channel; 
+	if (event->ring_mode == WAN_EVENT_RING_TRIP_STOP){
+		p_tdmapi_event->wp_tdm_api_event_ring_state = 
+				WP_TDMAPI_EVENT_RING_TRIP_STOP;
+	}else if (event->ring_mode == WAN_EVENT_RING_TRIP_PRESENT){
+		p_tdmapi_event->wp_tdm_api_event_ring_state = 
+				WP_TDMAPI_EVENT_RING_TRIP_PRESENT;	
+	}
 
 #if 0
 	rx_hdr->event_time_stamp = gettimeofday();
 #endif					
 
 #if defined(__WINDOWS__)
-	aft_te1_insert_tdm_api_event_in_to_rx_queue(tdm_api, rx_hdr);
+	aft_te1_insert_tdm_api_event_in_to_rx_queue(tdm_api, p_tdmapi_event);
 	queue_tdm_api_rx_dpc(tdm_api);
 #else
 	wan_skb_queue_tail(&tdm_api->wp_event_list,skb);
@@ -2200,50 +2272,54 @@
 	netskb_t		*skb;
 	wanpipe_tdm_api_dev_t	*tdm_api = NULL;
 	sdla_t			*card = (sdla_t*)card_id;
-	wp_tdm_api_rx_hdr_t	*rx_hdr = NULL;
+	wp_tdm_api_event_t	*p_tdmapi_event = NULL;
 #if defined(__WINDOWS__)
-	wp_tdm_api_rx_hdr_t tdm_api_hdr;
+	wp_tdm_api_event_t	tdmapi_event;
 #endif
 	
 	DEBUG_TDMAPI("%s: Received RM RING DETECT Event at TDM_API (%d:%s)!\n",
 			card->devname,
 			event->channel,
-			WAN_EVENT_RING_DECODE(event->rxhook));
+			WAN_EVENT_RING_DECODE(event->ring_mode));
 	
-	tdm_api = wp_tdmapi_search(card, 
-#if defined(__WINDOWS__)
-			event->channel - 1);
-#else
-			event->channel);
-#endif
+	tdm_api = wp_tdmapi_search(card, event->channel);
 	if (tdm_api == NULL){
 		return;
 	}
 					
 #if defined(__WINDOWS__)
-	rx_hdr = &tdm_api_hdr;
+	p_tdmapi_event = &tdmapi_event;
 #else
 	if (wan_skb_queue_len(&tdm_api->wp_event_list) > WP_TDM_MAX_EVENT_Q_LEN) {
 		return;
 	}
-	skb=wan_skb_alloc(sizeof(wp_tdm_api_rx_hdr_t));
+	skb=wan_skb_alloc(sizeof(wp_tdm_api_event_t));
 	if (skb == NULL) return;
 	
-	rx_hdr=(wp_tdm_api_rx_hdr_t*)wan_skb_put(skb,sizeof(wp_tdm_api_rx_hdr_t));
+	p_tdmapi_event = (wp_tdm_api_event_t*)wan_skb_put(skb,sizeof(wp_tdm_api_event_t));
 #endif
 
-	memset(rx_hdr,0,sizeof(wp_tdm_api_rx_hdr_t));
+	memset(p_tdmapi_event, 0, sizeof(wp_tdm_api_event_t));
 
-	rx_hdr->wp_tdm_api_event_type		= WP_TDM_EVENT_RING_DETECT;
-	rx_hdr->wp_tdm_api_event_channel	= (u_int16_t)event->channel; 
-	rx_hdr->wp_tdm_api_event_ring_state	= event->ring_mode;
+	p_tdmapi_event->type	= WP_TDMAPI_EVENT_RING_DETECT;
+	p_tdmapi_event->channel	= (u_int16_t)event->channel; 
+	switch(event->ring_mode){
+	case WAN_EVENT_RING_PRESENT:
+		p_tdmapi_event->wp_tdm_api_event_ring_state = 
+					WP_TDMAPI_EVENT_RING_PRESENT;
+		break;
+	case WAN_EVENT_RING_STOP:
+		p_tdmapi_event->wp_tdm_api_event_ring_state = 
+					WP_TDMAPI_EVENT_RING_STOP;
+		break;
+	}
 
 #if 0
 	rx_hdr->event_time_stamp = gettimeofday();
 #endif					
 
 #if defined(__WINDOWS__)
-	aft_te1_insert_tdm_api_event_in_to_rx_queue(tdm_api, rx_hdr);
+	aft_te1_insert_tdm_api_event_in_to_rx_queue(tdm_api, p_tdmapi_event);
 	queue_tdm_api_rx_dpc(tdm_api);
 #else
 	wan_skb_queue_tail(&tdm_api->wp_event_list,skb);
@@ -2258,49 +2334,29 @@
 {
 	int		i;
 
-	TDM_FUNC_DBG
-
-	if (IS_TE1_CARD(card)) {
-		DEBUG_TDMAPI("TE1 card: original_active_ch: 0x%X\n", tdm_api->original_active_ch);
-
-		for(i = 0; i < NUM_OF_E1_CHANNELS; i++){
-			if(tdm_api->original_active_ch & (1 << i)){
-
-				DEBUG_TDMAPI("%s(): setting channel: %d\n", __FUNCTION__, i);
-
-				if(i >= MAX_TDM_API_CHANNELS){
-					DEBUG_EVENT("%s(): TDM API Error (TE1): Invalid Channel Number=%i (Span=%d)!\n",
-						__FUNCTION__, i, tdm_api->tdm_span);
-					return 1;
-				}
+	TDM_FUNC_DBG();
 
-				if(card->wp_tdmapi_hash[i] != NULL){
-					DEBUG_EVENT("%s(): TDM API Error (TE1): device SPAN=%i CHAN=%i already in use!\n",
-						__FUNCTION__, tdm_api->tdm_span, i);
-					return 1;
-				}
-				card->wp_tdmapi_hash[i] = tdm_api;
-			}//if()
-		}//for()
+	DEBUG_TDMAPI("%s(): original_active_ch: 0x%X\n", __FUNCTION__, tdm_api->original_active_ch);
 
-	}else{
-		DEBUG_TDMAPI("Not a TE1 card\n");
+	for(i = 0; i < NUM_OF_E1_CHANNELS; i++){
+		if(tdm_api->original_active_ch & (1 << i)){
 
-		DEBUG_TDMAPI("%s(): tdm_api->tdm_chan: %d\n", __FUNCTION__, tdm_api->tdm_chan);
+			DEBUG_TDMAPI("%s(): setting channel: %d\n", __FUNCTION__, i);
 
-		if(tdm_api->tdm_chan >= MAX_TDM_API_CHANNELS){
-			DEBUG_EVENT("%s(): TDM API Error (Analog): Invalid Channel Number=%i (Span=%d)!\n",
-				__FUNCTION__, tdm_api->tdm_chan, tdm_api->tdm_span);
-			return 1;
-		}
+			if(i >= MAX_TDM_API_CHANNELS){
+				DEBUG_EVENT("%s(): TDM API Error (TE1): Invalid Channel Number=%i (Span=%d)!\n",
+					__FUNCTION__, i, tdm_api->tdm_span);
+				return 1;
+			}
 
-		if(card->wp_tdmapi_hash[tdm_api->tdm_chan] != NULL){
-			DEBUG_EVENT("%s(): TDM API Error (Analog): device SPAN=%i CHAN=%i already in use!\n",
-				__FUNCTION__, tdm_api->tdm_span, tdm_api->tdm_chan);
-			return 1;
-		}
-		card->wp_tdmapi_hash[tdm_api->tdm_chan] = tdm_api;
-	}
+			if(card->wp_tdmapi_hash[i] != NULL){
+				DEBUG_EVENT("%s(): TDM API Error (TE1): device SPAN=%i CHAN=%i already in use!\n",
+					__FUNCTION__, tdm_api->tdm_span, i);
+				return 1;
+			}
+			card->wp_tdmapi_hash[i] = tdm_api;
+		}//if()
+	}//for()
 	return 0;
 }
 
@@ -2309,7 +2365,7 @@
 	sdla_t	*card = NULL;
 	int		i;
 
-	TDM_FUNC_DBG
+	TDM_FUNC_DBG();
 
 	WAN_ASSERT(tdm_api == NULL);
 	card = (sdla_t*)tdm_api->card;
@@ -2320,41 +2376,27 @@
 		return 1;
 	}
 
-	if (IS_TE1_CARD(card)) {
-
-		DEBUG_TDMAPI("TE1 card: original_active_ch: 0x%X\n", tdm_api->original_active_ch);
+	DEBUG_TDMAPI("%s(): original_active_ch: 0x%X\n", __FUNCTION__, tdm_api->original_active_ch);
 
-		for(i = 0; i < NUM_OF_E1_CHANNELS; i++){
-			if(tdm_api->original_active_ch & (1 << i)){
+	for(i = 0; i < NUM_OF_E1_CHANNELS; i++){
+		if(tdm_api->original_active_ch & (1 << i)){
 
-				DEBUG_TDMAPI("%s(): RE-setting channel: %d\n", __FUNCTION__, i);
+			DEBUG_TDMAPI("%s(): RE-setting channel: %d\n", __FUNCTION__, i);
 
-				if(i >= MAX_TDM_API_CHANNELS){
-					DEBUG_EVENT("%s(): TDM API Error (TE1): Invalid Channel Number=%i (Span=%d)!\n",
-						__FUNCTION__, i, tdm_api->tdm_span);
-					return 1;
-				}
+			if(i >= MAX_TDM_API_CHANNELS){
+				DEBUG_EVENT("%s(): TDM API Error (TE1): Invalid Channel Number=%i (Span=%d)!\n",
+					__FUNCTION__, i, tdm_api->tdm_span);
+				return 1;
+			}
 
-				if(card->wp_tdmapi_hash[i] == NULL){
-					DEBUG_EVENT("%s: TDM API Warning (TE1): device SPAN=%i CHAN=%i was NOT in use!\n",
-						__FUNCTION__, tdm_api->tdm_span, tdm_api->tdm_chan);
-				}
-				card->wp_tdmapi_hash[i] = NULL;
-			}//if()
-		}//for()
-	}else{
-		if(tdm_api->tdm_chan >= MAX_TDM_API_CHANNELS){
-			DEBUG_EVENT("%s(): TDM API Error (Analog): Invalid Channel Number=%i (Span=%d)!\n",
-				__FUNCTION__, tdm_api->tdm_chan, tdm_api->tdm_span);
-			return 1;
-		}
+			if(card->wp_tdmapi_hash[i] == NULL){
+				DEBUG_EVENT("%s: TDM API Warning (TE1): device SPAN=%i CHAN=%i was NOT in use!\n",
+					__FUNCTION__, tdm_api->tdm_span, tdm_api->tdm_chan);
+			}
+			card->wp_tdmapi_hash[i] = NULL;
+		}//if()
+	}//for()
 
-		if(card->wp_tdmapi_hash[tdm_api->tdm_chan] == NULL){
-			DEBUG_EVENT("%s: TDM API Warning (Analog): device SPAN=%i CHAN=%i was NOT in use!\n",
-				__FUNCTION__, tdm_api->tdm_span, tdm_api->tdm_chan);
-		}
-		card->wp_tdmapi_hash[tdm_api->tdm_chan] = NULL;
-	}	
 	return 0;
 }
 
@@ -2515,4 +2557,4 @@
 	return -EINVAL;
 }
 
-#endif
+#endif /* #if defined(BUILD_TDMV_API) */
