patch-2.4.4 linux/drivers/net/wan/sdla_chdlc.c

Next file: linux/drivers/net/wan/sdla_fr.c
Previous file: linux/drivers/net/wan/sdla.c
Back to the patch index
Back to the overall index

diff -u --recursive --new-file v2.4.3/linux/drivers/net/wan/sdla_chdlc.c linux/drivers/net/wan/sdla_chdlc.c
@@ -4,14 +4,40 @@
 * Authors: 	Nenad Corbic <ncorbic@sangoma.com>
 *		Gideon Hack  
 *
-* Copyright:	(c) 1995-1999 Sangoma Technologies Inc.
+* Copyright:	(c) 1995-2001 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
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 * ============================================================================
-* Feb 28, 2000  Jeff Garzik	softnet updates
+* Feb 28, 2001  Nenad Corbic	Updated if_tx_timeout() routine for 
+* 				2.4.X kernels.
+* Jan 25, 2001  Nenad Corbic	Added a TTY Sync serial driver over the
+* 				HDLC streaming protocol
+* 				Added a TTY Async serial driver over the
+* 				Async protocol.
+* Dec 15, 2000  Nenad Corbic    Updated for 2.4.X Kernel support
+* Nov 13, 2000  Nenad Corbic    Added true interface type encoding option.
+* 				Tcpdump doesn't support CHDLC inteface
+* 				types, to fix this "true type" option will set
+* 				the interface type to RAW IP mode.
+* Nov 07, 2000  Nenad Corbic	Added security features for UDP debugging:
+*                               Deny all and specify allowed requests.
+* Jun 20, 2000  Nenad Corbic	Fixed the API IP ERROR bug. Caused by the 
+*                               latest update.
+* May 09, 2000	Nenad Corbic	Option to bring down an interface
+*                               upon disconnect.
+* Mar 23, 2000  Nenad Corbic	Improved task queue, bh handling.
+* Mar 16, 2000	Nenad Corbic	Fixed the SLARP Dynamic IP addressing.
+* Mar 06, 2000  Nenad Corbic	Bug Fix: corrupted mbox recovery.
+* Feb 10, 2000  Gideon Hack     Added ASYNC support.
+* Feb 09, 2000  Nenad Corbic    Fixed two shutdown bugs in update() and
+*                               if_stats() functions.
+* Jan 24, 2000  Nenad Corbic    Fixed a startup wanpipe state racing,  
+*                               condition between if_open and isr. 
+* Jan 10, 2000  Nenad Corbic    Added new socket API support.
+* Dev 15, 1999  Nenad Corbic    Fixed up header files for 2.0.X kernels
 * Nov 20, 1999  Nenad Corbic 	Fixed zero length API bug.
 * Sep 30, 1999  Nenad Corbic    Fixed dynamic IP and route setup.
 * Sep 23, 1999  Nenad Corbic    Added SMP support, fixed tracing 
@@ -22,18 +48,26 @@
 * Aug 07, 1998	David Fong	Initial version.
 *****************************************************************************/
 
-#include <linux/config.h>
 #include <linux/version.h>
 #include <linux/kernel.h>	/* printk(), and other useful stuff */
 #include <linux/stddef.h>	/* offsetof(), etc. */
 #include <linux/errno.h>	/* return codes */
 #include <linux/string.h>	/* inline memset(), etc. */
-#include <linux/slab.h>	/* kmalloc(), kfree() */
+#include <linux/malloc.h>	/* kmalloc(), kfree() */
 #include <linux/wanrouter.h>	/* WAN router definitions */
 #include <linux/wanpipe.h>	/* WANPIPE common user API definitions */
 #include <linux/if_arp.h>	/* ARPHRD_* defines */
-#include <linux/inetdevice.h>
-#include <asm/uaccess.h>
+
+
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
+ #include <asm/uaccess.h>
+ #include <linux/inetdevice.h>
+ #include <linux/netdevice.h>
+#else 				
+ #include <asm/segment.h>
+ #include <net/route.h>          /* Adding new route entries : 2.0.X kernels */
+#endif
+
 #include <linux/in.h>		/* sockaddr_in */
 #include <linux/inet.h>	
 #include <linux/if.h>
@@ -42,32 +76,42 @@
 #include <asm/io.h>
 
 #include <linux/sdla_chdlc.h>		/* CHDLC firmware API definitions */
+#include <linux/sdla_asy.h>           	/* CHDLC (async) API definitions */
 
-/****** Defines & Macros ****************************************************/
+#include <linux/if_wanpipe_common.h>    /* Socket Driver common area */
+#include <linux/if_wanpipe.h>		
+
+/* TTY Includes */
+#include <linux/tty.h>
+#include <linux/tty_flip.h>
+#include <linux/serial.h>
 
-#ifdef	_DEBUG_
-#define	STATIC
-#else
-#define	STATIC		static
-#endif
+
+/****** Defines & Macros ****************************************************/
 
 /* reasons for enabling the timer interrupt on the adapter */
-#define TMR_INT_ENABLED_UDP   	0x0001
-#define TMR_INT_ENABLED_UPDATE	0x0002
- 
+#define TMR_INT_ENABLED_UDP   		0x01
+#define TMR_INT_ENABLED_UPDATE		0x02
+#define TMR_INT_ENABLED_CONFIG		0x10
+
+#define MAX_IP_ERRORS	10
+
+#define TTY_CHDLC_MAX_MTU	2000
 #define	CHDLC_DFLT_DATA_LEN	1500		/* default MTU */
 #define CHDLC_HDR_LEN		1
 
-#define IFF_POINTTOPOINT 0x10
-
-#define WANPIPE 0x00
-#define API	0x01
 #define CHDLC_API 0x01
 
 #define PORT(x)   (x == 0 ? "PRIMARY" : "SECONDARY" )
+#define MAX_BH_BUFF	10
+
+//#define PRINT_DEBUG
+#ifdef PRINT_DEBUG
+#define dbg_printk(format, a...) printk(format, ## a)
+#else
+#define dbg_printk(format, a...)
+#endif  
 
-#define TX_TIMEOUT	(5*HZ)
- 
 /******Data Structures*****************************************************/
 
 /* This structure is placed in the private data area of the device structure.
@@ -77,9 +121,7 @@
 
 typedef struct chdlc_private_area
 {
-	/* This member must be first. */
-	struct net_device *slave;		/* WAN slave */
-
+	wanpipe_common_t common;
 	sdla_t		*card;
 	int 		TracingEnabled;		/* For enabling Tracing */
 	unsigned long 	curr_trace_addr;	/* Used for Tracing */
@@ -96,12 +138,38 @@
 	unsigned long 	router_up_time;
         u32             IP_address;		/* IP addressing */
         u32             IP_netmask;
+	u32		ip_local;
+	u32		ip_remote;
+	u32 		ip_local_tmp;
+	u32		ip_remote_tmp;
+	u8		ip_error;
+	u8		config_chdlc;
+	u8 		config_chdlc_timeout;
 	unsigned char  mc;			/* Mulitcast support on/off */
 	unsigned short udp_pkt_lgth;		/* udp packet processing */
 	char udp_pkt_src;
 	char udp_pkt_data[MAX_LGTH_UDP_MGNT_PKT];
 	unsigned short timer_int_enabled;
 	char update_comms_stats;		/* updating comms stats */
+
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
+	bh_data_t *bh_head;	  	  /* Circular buffer for chdlc_bh */
+	unsigned long  tq_working;
+	volatile int  bh_write;
+	volatile int  bh_read;
+	atomic_t  bh_buff_used;
+#endif
+	
+	unsigned char interface_down;
+
+	/* Polling task queue. Each interface
+         * has its own task queue, which is used
+         * to defer events from the interrupt */
+	struct tq_struct poll_task;
+	struct timer_list poll_delay_timer;
+
+	u8 gateway;
+	u8 true_if_encoding;
 	//FIXME: add driver stats as per frame relay!
 
 } chdlc_private_area_t;
@@ -125,41 +193,47 @@
 /****** Function Prototypes *************************************************/
 /* WAN link driver entry points. These are called by the WAN router module. */
 static int update (wan_device_t* wandev);
-static int new_if (wan_device_t* wandev, struct net_device* dev,
+static int new_if (wan_device_t* wandev, netdevice_t* dev,
 	wanif_conf_t* conf);
-static int del_if (wan_device_t* wandev, struct net_device* dev);
 
 /* Network device interface */
-static int if_init   (struct net_device* dev);
-static int if_open   (struct net_device* dev);
-static int if_close  (struct net_device* dev);
-static void if_tx_timeout (struct net_device *dev);
-static int if_header (struct sk_buff* skb, struct net_device* dev,
+static int if_init   (netdevice_t* dev);
+static int if_open   (netdevice_t* dev);
+static int if_close  (netdevice_t* dev);
+static int if_header (struct sk_buff* skb, netdevice_t* dev,
 	unsigned short type, void* daddr, void* saddr, unsigned len);
-#ifdef LINUX_2_1
-static int if_rebuild_hdr (struct sk_buff *skb);
+
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
+  static int if_rebuild_hdr (struct sk_buff *skb);
+  static struct net_device_stats* if_stats (netdevice_t* dev);
+
 #else
-static int if_rebuild_hdr (void* hdr, struct net_device* dev, unsigned long raddr,
+  static int if_rebuild_hdr (void* hdr, netdevice_t* dev, unsigned long raddr,
         struct sk_buff* skb);
+  static struct enet_statistics* if_stats (netdevice_t* dev);
 #endif
-static int if_send (struct sk_buff* skb, struct net_device* dev);
-static struct net_device_stats* if_stats (struct net_device* dev);
+  
+static int if_send (struct sk_buff* skb, netdevice_t* dev);
 
 /* CHDLC Firmware interface functions */
 static int chdlc_configure 	(sdla_t* card, void* data);
 static int chdlc_comm_enable 	(sdla_t* card);
-static int chdlc_comm_disable 	(sdla_t* card);
 static int chdlc_read_version 	(sdla_t* card, char* str);
 static int chdlc_set_intr_mode 	(sdla_t* card, unsigned mode);
 static int chdlc_send (sdla_t* card, void* data, unsigned len);
 static int chdlc_read_comm_err_stats (sdla_t* card);
 static int chdlc_read_op_stats (sdla_t* card);
+static int chdlc_error (sdla_t *card, int err, CHDLC_MAILBOX_STRUCT *mb);
+
 
+static int chdlc_disable_comm_shutdown (sdla_t *card);
+#ifdef LINUX_2_4
+  static void if_tx_timeout (netdevice_t *dev);
+#endif
 
 /* Miscellaneous CHDLC Functions */
 static int set_chdlc_config (sdla_t* card);
-static void init_chdlc_tx_rx_buff( sdla_t* card, struct net_device *dev );
-static int chdlc_error (sdla_t *card, int err, CHDLC_MAILBOX_STRUCT *mb);
+static void init_chdlc_tx_rx_buff( sdla_t* card);
 static int process_chdlc_exception(sdla_t *card);
 static int process_global_exception(sdla_t *card);
 static int update_comms_stats(sdla_t* card,
@@ -168,23 +242,40 @@
 static int unconfigure_ip (sdla_t* card);
 static void process_route(sdla_t *card);
 static void port_set_state (sdla_t *card, int);
+static int config_chdlc (sdla_t *card);
+static void disable_comm (sdla_t *card);
+
+static void trigger_chdlc_poll (netdevice_t *);
+static void chdlc_poll (netdevice_t *);
+static void chdlc_poll_delay (unsigned long dev_ptr);
+
 
+/* Miscellaneous asynchronous interface Functions */
+static int set_asy_config (sdla_t* card);
+static int asy_comm_enable (sdla_t* card);
 
 /* Interrupt handlers */
 static void wpc_isr (sdla_t* card);
 static void rx_intr (sdla_t* card);
 static void timer_intr(sdla_t *);
 
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
+  /* Bottom half handlers */
+  static void chdlc_bh (netdevice_t *);
+  static int chdlc_bh_cleanup (netdevice_t *);
+  static int bh_enqueue (netdevice_t *, struct sk_buff *);
+#endif
+
 /* Miscellaneous functions */
-static int chk_bcast_mcast_addr(sdla_t* card, struct net_device* dev,
+static int chk_bcast_mcast_addr(sdla_t* card, netdevice_t* dev,
 				struct sk_buff *skb);
 static int reply_udp( unsigned char *data, unsigned int mbox_len );
-static int intr_test( sdla_t* card, struct net_device *dev );
+static int intr_test( sdla_t* card);
 static int udp_pkt_type( struct sk_buff *skb , sdla_t* card);
 static int store_udp_mgmt_pkt(char udp_pkt_src, sdla_t* card,
-                                struct sk_buff *skb, struct net_device* dev,
+                                struct sk_buff *skb, netdevice_t* dev,
                                 chdlc_private_area_t* chdlc_priv_area);
-static int process_udp_mgmt_pkt(sdla_t* card, struct net_device* dev,  
+static int process_udp_mgmt_pkt(sdla_t* card, netdevice_t* dev,  
 				chdlc_private_area_t* chdlc_priv_area);
 static unsigned short calc_checksum (char *, int);
 static void s508_lock (sdla_t *card, unsigned long *smp_flags);
@@ -192,6 +283,46 @@
 
 
 static int  Intr_test_counter;
+
+/* TTY Global Definitions */
+
+#if defined(LINUX_2_4) || defined(LINUX_2_1)
+
+#define NR_PORTS 4
+#define WAN_TTY_MAJOR 226
+#define WAN_TTY_MINOR 0
+
+#define WAN_CARD(port) (tty_card_map[port])
+#define MIN_PORT 0
+#define MAX_PORT NR_PORTS-1 
+
+#define CRC_LENGTH 2
+
+static int wanpipe_tty_init(sdla_t *card);
+static void wanpipe_tty_receive(sdla_t *, unsigned, unsigned int);
+static void wanpipe_tty_trigger_poll(sdla_t *card);
+
+static struct tty_driver serial_driver, callout_driver;
+static int serial_refcount=1;
+static int tty_init_cnt=0;
+
+static struct serial_state rs_table[NR_PORTS];
+static struct tty_struct *serial_table[NR_PORTS];
+static struct termios *serial_termios[NR_PORTS];
+static struct termios *serial_termios_locked[NR_PORTS];
+
+static char tty_driver_mode=WANOPT_TTY_SYNC;
+
+static char *opt_decode[] = {"NONE","CRTSCTS","XONXOFF-RX",
+	  	             "CRTSCTS XONXOFF-RX","XONXOFF-TX",
+		             "CRTSCTS XONXOFF-TX","CRTSCTS XONXOFF"};
+static char *p_decode[] = {"NONE","ODD","EVEN"};
+
+static void* tty_card_map[NR_PORTS] = {NULL,NULL,NULL,NULL};
+
+#endif
+
+
 /****** Public Functions ****************************************************/
 
 /*============================================================================
@@ -211,6 +342,7 @@
 	unsigned char port_num;
 	int err;
 	unsigned long max_permitted_baud = 0;
+	SHARED_MEMORY_INFO_STRUCT *flags;
 
 	union
 		{
@@ -234,7 +366,7 @@
 			if (conf->comm_port != card->next->u.c.comm_port){
 				card->u.c.comm_port = conf->comm_port;
 			}else{
-				printk(KERN_ERR "%s: ERROR - %s port used!\n",
+				printk(KERN_INFO "%s: ERROR - %s port used!\n",
         		        	card->wandev.name, PORT(conf->comm_port));
 				return -EINVAL;
 			}
@@ -242,7 +374,7 @@
 			card->u.c.comm_port = conf->comm_port;
 		}
 	}else{
-		printk(KERN_ERR "%s: ERROR - Invalid Port Selected!\n",
+		printk(KERN_INFO "%s: ERROR - Invalid Port Selected!\n",
                 			card->wandev.name);
 		return -EINVAL;
 	}
@@ -305,15 +437,11 @@
 	card->exec			= NULL;
 	card->wandev.update		= &update;
  	card->wandev.new_if		= &new_if;
-	card->wandev.del_if		= &del_if;
-	card->wandev.state		= WAN_DUALPORT;
+	card->wandev.del_if		= NULL;
 	card->wandev.udp_port   	= conf->udp_port;
-
+	card->disable_comm		= &disable_comm;
 	card->wandev.new_if_cnt = 0;
 
-	/* This is for the ports link state */
-	card->u.c.state = WAN_DISCONNECTED;
-	
 	/* reset the number of times the 'update()' proc has been called */
 	card->u.c.update_call_count = 0;
 	
@@ -331,18 +459,27 @@
 
 	port_num = card->u.c.comm_port;
 
+	/* in API mode, we can configure for "receive only" buffering */
+	if(card->hw.type == SDLA_S514) {
+		card->u.c.receive_only = conf->receive_only;
+		if(conf->receive_only) {
+			printk(KERN_INFO
+				"%s: Configured for 'receive only' mode\n",
+                                card->devname);
+		}
+	}
+
 	/* Setup Port Bps */
 
 	if(card->wandev.clocking) {
-                      
-		if(port_num == WANOPT_PRI) {
+		if((port_num == WANOPT_PRI) || card->u.c.receive_only) {
 			/* For Primary Port 0 */
                		max_permitted_baud =
 				(card->hw.type == SDLA_S514) ?
 				PRI_MAX_BAUD_RATE_S514 : 
 				PRI_MAX_BAUD_RATE_S508;
-		}
-		else if(port_num == WANOPT_SEC) {
+
+		}else if(port_num == WANOPT_SEC) {
 			/* For Secondary Port 1 */
                         max_permitted_baud =
                                (card->hw.type == SDLA_S514) ?
@@ -357,14 +494,14 @@
  				printk(KERN_INFO "%s: Baud rate set to %lu bps\n", 
 					card->wandev.name, max_permitted_baud);
 			}
-                             
 			card->wandev.bps = conf->bps;
 	}else{
         	card->wandev.bps = 0;
   	}
 
 	/* Setup the Port MTU */
-	if(port_num == WANOPT_PRI) {
+	if((port_num == WANOPT_PRI) || card->u.c.receive_only) {
+
 		/* For Primary Port 0 */
 		card->wandev.mtu =
 			(conf->mtu >= MIN_LGTH_CHDLC_DATA_CFG) ?
@@ -387,8 +524,6 @@
 	mb1->command = READ_CHDLC_CONFIGURATION;
 	err = sdla_exec(mb1) ? mb1->return_code : CMD_TIMEOUT;
 	if(err != COMMAND_OK) {
-		clear_bit(1, (void*)&card->wandev.critical);
-
                 if(card->hw.type != SDLA_S514)
                 	enable_irq(card->hw.irq);
 
@@ -406,6 +541,72 @@
 			ptr_shared_mem_info_struct % SDLA_WINDOWSIZE));
 	}
 
+	flags = card->u.c.flags;
+	
+	/* This is for the ports link state */
+	card->wandev.state = WAN_DUALPORT;
+	card->u.c.state = WAN_DISCONNECTED;
+
+
+	if (!card->wandev.piggyback){	
+		int err;
+
+		/* Perform interrupt testing */
+		err = intr_test(card);
+
+		if(err || (Intr_test_counter < MAX_INTR_TEST_COUNTER)) { 
+			printk(KERN_INFO "%s: Interrupt test failed (%i)\n",
+					card->devname, Intr_test_counter);
+			printk(KERN_INFO "%s: Please choose another interrupt\n",
+					card->devname);
+			return -EIO;
+		}
+		
+		printk(KERN_INFO "%s: Interrupt test passed (%i)\n", 
+				card->devname, Intr_test_counter);
+		card->configured = 1;
+	}
+
+	if ((card->tty_opt=conf->tty) == WANOPT_YES){
+#if defined(LINUX_2_4) || defined(LINUX_2_1)	
+		int err;
+		card->tty_minor = conf->tty_minor;
+
+		/* On ASYNC connections internal clocking 
+		 * is mandatory */
+		if ((card->u.c.async_mode = conf->tty_mode)){
+			card->wandev.clocking = 1;
+		}
+		err=wanpipe_tty_init(card);
+		if (err){
+			return err;
+		}
+#else
+		printk(KERN_INFO "%s: Error: TTY driver is not supported on 2.0.X kernels!\n",
+				card->devname);	
+		return -EINVAL;  
+#endif
+	}else{
+	
+
+		if (chdlc_set_intr_mode(card, APP_INT_ON_TIMER)){
+			printk (KERN_INFO "%s: 
+				Failed to set interrupt triggers!\n",
+				card->devname);
+			return -EIO;	
+        	}
+	
+		/* Mask the Timer interrupt */
+		flags->interrupt_info_struct.interrupt_permission &= 
+			~APP_INT_ON_TIMER;
+	}
+
+	/* If we are using CHDLC in backup mode, this flag will
+	 * indicate not to look for IP addresses in config_chdlc()*/
+	card->u.c.backup = conf->backup;
+	
+	printk(KERN_INFO "\n");
+
 	return 0;
 }
 
@@ -427,8 +628,8 @@
 static int update (wan_device_t* wandev)
 {
 	sdla_t* card = wandev->private;
- 	struct net_device* dev = card->wandev.dev;
-        volatile chdlc_private_area_t* chdlc_priv_area = dev->priv;
+ 	netdevice_t* dev;
+        volatile chdlc_private_area_t* chdlc_priv_area;
         SHARED_MEMORY_INFO_STRUCT *flags;
 	unsigned long timeout;
 
@@ -442,10 +643,14 @@
 	/* more sanity checks */
         if(!card->u.c.flags)
                 return -ENODEV;
-	if(test_bit(1, (void*)&card->wandev.critical))
+
+	if(test_bit(PERI_CRIT, (void*)&card->wandev.critical))
                 return -EAGAIN;
 
-	if(!netif_running(dev))
+	if((dev=card->wandev.dev) == NULL)
+		return -ENODEV;
+
+	if((chdlc_priv_area=dev->priv) == NULL)
 		return -ENODEV;
 
       	flags = card->u.c.flags;
@@ -488,13 +693,17 @@
  * Return:	0	o.k.
  *		< 0	failure (channel will not be created)
  */
-static int new_if (wan_device_t* wandev, struct net_device* dev, wanif_conf_t* conf)
+static int new_if (wan_device_t* wandev, netdevice_t* dev, wanif_conf_t* conf)
 {
 	sdla_t* card = wandev->private;
 	chdlc_private_area_t* chdlc_priv_area;
 
+
+	printk(KERN_INFO "%s: Configuring Interface: %s\n",
+			card->devname, conf->name);
+ 
 	if ((conf->name[0] == '\0') || (strlen(conf->name) > WAN_IFNAME_SZ)) {
-		printk(KERN_INFO "%s: invalid interface name!\n",
+		printk(KERN_INFO "%s: Invalid interface name!\n",
 			card->devname);
 		return -EINVAL;
 	}
@@ -506,9 +715,11 @@
 		return -ENOMEM;
 
 	memset(chdlc_priv_area, 0, sizeof(chdlc_private_area_t));
-	
+
 	chdlc_priv_area->card = card; 
-	
+	chdlc_priv_area->common.sk = NULL;
+	chdlc_priv_area->common.func = NULL;	
+
 	/* initialize data */
 	strcpy(card->u.c.if_name, conf->name);
 
@@ -523,110 +734,216 @@
 	chdlc_priv_area->route_status = NO_ROUTE;
 	chdlc_priv_area->route_removed = 0;
 
-	/* Setup protocol options */
-
-	card->u.c.protocol_options = 0;
-
-	if (conf->ignore_dcd == WANOPT_YES){
-		card->u.c.protocol_options |= IGNORE_DCD_FOR_LINK_STAT;
-	}
+	card->u.c.async_mode = conf->async_mode;
+	
+	/* setup for asynchronous mode */
+	if(conf->async_mode) {
+		printk(KERN_INFO "%s: Configuring for asynchronous mode\n",
+			wandev->name);
 
-	if (conf->ignore_cts == WANOPT_YES){
-		card->u.c.protocol_options |= IGNORE_CTS_FOR_LINK_STAT;
-	}
+		if(card->u.c.comm_port == WANOPT_PRI) {
+			printk(KERN_INFO
+				"%s:Asynchronous mode on secondary port only\n",
+					wandev->name);
+			kfree(chdlc_priv_area);
+			return -EINVAL;
+		}
 
-	if (conf->ignore_keepalive == WANOPT_YES) {
-		card->u.c.protocol_options |= IGNORE_KPALV_FOR_LINK_STAT;
-		card->u.c.kpalv_tx  = MIN_Tx_KPALV_TIMER; 
-		card->u.c.kpalv_rx  = MIN_Rx_KPALV_TIMER; 
-		card->u.c.kpalv_err = MIN_KPALV_ERR_TOL; 
+	       	if(strcmp(conf->usedby, "WANPIPE") == 0) {
+			printk(KERN_INFO
+                                "%s: Running in WANIPE Async Mode\n",                                        			wandev->name);
+			card->u.c.usedby = WANPIPE;
+		}else{
+			card->u.c.usedby = API;
+		}
 
-	} else {   /* Do not ignore keepalives */
+		if(!card->wandev.clocking) {
+			printk(KERN_INFO
+				"%s: Asynch. clocking must be 'Internal'\n",
+				wandev->name);
+			kfree(chdlc_priv_area);
+			return -EINVAL;
+		}
 
-		card->u.c.kpalv_tx =
-  	   		(conf->keepalive_tx_tmr - MIN_Tx_KPALV_TIMER) >= 0 ?
-	   		min (conf->keepalive_tx_tmr, MAX_Tx_KPALV_TIMER) :
-	   					DEFAULT_Tx_KPALV_TIMER;
+		if((card->wandev.bps < MIN_ASY_BAUD_RATE) ||
+			(card->wandev.bps > MAX_ASY_BAUD_RATE)) {
+			printk(KERN_INFO "%s: Selected baud rate is invalid.\n",
+				wandev->name);
+			printk(KERN_INFO "Must be between %u and %u bps.\n",
+				MIN_ASY_BAUD_RATE, MAX_ASY_BAUD_RATE);
+			kfree(chdlc_priv_area);
+			return -EINVAL;
+		}
 
-		card->u.c.kpalv_rx =
-	   		(conf->keepalive_rx_tmr - MIN_Rx_KPALV_TIMER) >= 0 ?
-	   		min (conf->keepalive_rx_tmr, MAX_Rx_KPALV_TIMER) :
-	   					DEFAULT_Rx_KPALV_TIMER;
+		card->u.c.api_options = 0;
+                if (conf->asy_data_trans == WANOPT_YES) {
+                        card->u.c.api_options |= ASY_RX_DATA_TRANSPARENT;
+                }
+		
+		card->u.c.protocol_options = 0;
+		if (conf->rts_hs_for_receive == WANOPT_YES) {
+			card->u.c.protocol_options |= ASY_RTS_HS_FOR_RX;
+	        }
+                if (conf->xon_xoff_hs_for_receive == WANOPT_YES) {
+                        card->u.c.protocol_options |= ASY_XON_XOFF_HS_FOR_RX;
+                }
+                if (conf->xon_xoff_hs_for_transmit == WANOPT_YES) {
+                        card->u.c.protocol_options |= ASY_XON_XOFF_HS_FOR_TX;
+                }
+                if (conf->dcd_hs_for_transmit == WANOPT_YES) {
+                        card->u.c.protocol_options |= ASY_DCD_HS_FOR_TX;
+                }
+                if (conf->cts_hs_for_transmit == WANOPT_YES) {
+                        card->u.c.protocol_options |= ASY_CTS_HS_FOR_TX;
+                }
 
-		card->u.c.kpalv_err =
-	   		(conf->keepalive_err_margin - MIN_KPALV_ERR_TOL) >= 0 ?
-	   		min (conf->keepalive_err_margin, MAX_KPALV_ERR_TOL) : 
-	   					DEFAULT_KPALV_ERR_TOL;
-	}
+		card->u.c.tx_bits_per_char = conf->tx_bits_per_char;
+                card->u.c.rx_bits_per_char = conf->rx_bits_per_char;
+                card->u.c.stop_bits = conf->stop_bits;
+		card->u.c.parity = conf->parity;
+		card->u.c.break_timer = conf->break_timer;
+		card->u.c.inter_char_timer = conf->inter_char_timer;
+		card->u.c.rx_complete_length = conf->rx_complete_length;
+		card->u.c.xon_char = conf->xon_char;
+
+	} else {	/* setup for synchronous mode */
+
+		card->u.c.protocol_options = 0;
+		if (conf->ignore_dcd == WANOPT_YES){
+			card->u.c.protocol_options |= IGNORE_DCD_FOR_LINK_STAT;
+		}
+		if (conf->ignore_cts == WANOPT_YES){
+			card->u.c.protocol_options |= IGNORE_CTS_FOR_LINK_STAT;
+		}
 
+		if (conf->ignore_keepalive == WANOPT_YES) {
+			card->u.c.protocol_options |=
+				IGNORE_KPALV_FOR_LINK_STAT;
+			card->u.c.kpalv_tx  = MIN_Tx_KPALV_TIMER; 
+			card->u.c.kpalv_rx  = MIN_Rx_KPALV_TIMER; 
+			card->u.c.kpalv_err = MIN_KPALV_ERR_TOL; 
+
+		} else {   /* Do not ignore keepalives */
+			card->u.c.kpalv_tx =
+				((conf->keepalive_tx_tmr - MIN_Tx_KPALV_TIMER)
+				>= 0) ?
+	   			min(conf->keepalive_tx_tmr,MAX_Tx_KPALV_TIMER) :
+				DEFAULT_Tx_KPALV_TIMER;
+
+			card->u.c.kpalv_rx =
+		   		((conf->keepalive_rx_tmr - MIN_Rx_KPALV_TIMER)
+				>= 0) ?
+	   			min(conf->keepalive_rx_tmr,MAX_Rx_KPALV_TIMER) :
+				DEFAULT_Rx_KPALV_TIMER;
+
+			card->u.c.kpalv_err =
+		   		((conf->keepalive_err_margin-MIN_KPALV_ERR_TOL)
+				>= 0) ?
+	   			min(conf->keepalive_err_margin,
+				MAX_KPALV_ERR_TOL) : 
+	   			DEFAULT_KPALV_ERR_TOL;
+		}
 
-	/* Setup slarp timer to control delay between slarps 
-         */ 
-	card->u.c.slarp_timer = 
-		(conf->slarp_timer - MIN_SLARP_REQ_TIMER) >=0 ?
-		min (conf->slarp_timer, MAX_SLARP_REQ_TIMER) :
-					DEFAULT_SLARP_REQ_TIMER;
+		/* Setup slarp timer to control delay between slarps */
+		card->u.c.slarp_timer = 
+			((conf->slarp_timer - MIN_SLARP_REQ_TIMER) >= 0) ?
+			min (conf->slarp_timer, MAX_SLARP_REQ_TIMER) :
+			DEFAULT_SLARP_REQ_TIMER;
+
+#ifdef LINUX_2_0
+		if (card->u.c.slarp_timer){
+			printk(KERN_INFO 
+				"%s: Error: Dynamic IP support not available for 2.0.X kernels\n",
+					card->devname);
+			printk(KERN_INFO "%s:        Defaulting to Static IP addressing\n",
+						card->devname);
+		}
+		card->u.c.slarp_timer=0;
+#endif	
 
 
-	/* If HDLC_STRAMING is enabled then IGNORE DCD, CTS and KEEPALIVES
-         * are automatically ignored 
-	 */
-	if (conf->hdlc_streaming == WANOPT_YES) {
-		printk(KERN_INFO "%s: Enabling HDLC STREAMING Mode\n",
-			wandev->name);
-		card->u.c.protocol_options = HDLC_STREAMING_MODE;
-	}
+		if (conf->hdlc_streaming == WANOPT_YES) {
+			printk(KERN_INFO "%s: Enabling HDLC STREAMING Mode\n",
+				wandev->name);
+			card->u.c.protocol_options = HDLC_STREAMING_MODE;
+		}
 
+		if ((chdlc_priv_area->true_if_encoding = conf->true_if_encoding) == WANOPT_YES){
+			printk(KERN_INFO 
+				"%s: Enabling, true interface type encoding.\n",
+				card->devname);
+		}
+		
+        	/* Setup wanpipe as a router (WANPIPE) or as an API */
+		if( strcmp(conf->usedby, "WANPIPE") == 0) {
 
-        /* Setup wanpipe as a router (WANPIPE) or as an API */
-	if( strcmp(conf->usedby, "WANPIPE") == 0) {
-		printk(KERN_INFO "%s: Running in WANPIPE mode !\n",wandev->name);
-		card->u.c.usedby = WANPIPE;
-
-	} else if( strcmp(conf->usedby, "API") == 0){
-
-#ifdef CHDLC_API 
-		card->u.c.usedby = API;
-		printk(KERN_INFO "%s: Running in API mode !\n",wandev->name);
+			printk(KERN_INFO "%s: Running in WANPIPE mode!\n",
+				wandev->name);
+			card->u.c.usedby = WANPIPE;
+
+			/* Option to bring down the interface when 
+        		 * the link goes down */
+			if (conf->if_down){
+				set_bit(DYN_OPT_ON,&chdlc_priv_area->interface_down);
+				printk(KERN_INFO 
+				 "%s: Dynamic interface configuration enabled\n",
+				   card->devname);
+			} 
+
+		} else if( strcmp(conf->usedby, "API") == 0) {
+#if defined(LINUX_2_1) || defined(LINUX_2_4) 
+			card->u.c.usedby = API;
+			printk(KERN_INFO "%s: Running in API mode !\n",
+				wandev->name);
 #else
-		printk(KERN_INFO "%s: API Mode is not supported!\n",
-			wandev->name);
-		printk(KERN_INFO "%s: Chdlc API patch can be obtained from Sangoma Tech.\n",
-					wandev->name);
-                kfree(chdlc_priv_area);
-		return -EINVAL;
+			printk(KERN_INFO "%s: API Mode is not supported for kernels lower than 2.2.X!\n",
+				wandev->name);
+			printk(KERN_INFO "%s: Please upgrade to a 2.2.X kernel fro the API support\n",
+				wandev->name);
+	                kfree(chdlc_priv_area);
+			return -EINVAL;
 #endif
+		}
 	}
 
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
+	/* Tells us that if this interface is a
+         * gateway or not */
+	if ((chdlc_priv_area->gateway = conf->gateway) == WANOPT_YES){
+		printk(KERN_INFO "%s: Interface %s is set as a gateway.\n",
+			card->devname,card->u.c.if_name);
+	}
+#endif
 
 	/* Get Multicast Information */
 	chdlc_priv_area->mc = conf->mc;
 
 	/* prepare network device data space for registration */
-	strcpy(dev->name, card->u.c.if_name);
+#ifdef LINUX_2_4
+	strcpy(dev->name,card->u.c.if_name);
+#else
+	dev->name = (char *)kmalloc(strlen(card->u.c.if_name) + 2, GFP_KERNEL); 
+	sprintf(dev->name, "%s", card->u.c.if_name);
+#endif
+
 	dev->init = &if_init;
 	dev->priv = chdlc_priv_area;
 
-	return 0;
-}
-
-/*============================================================================
- * Delete logical channel.
- */
-static int del_if (wan_device_t* wandev, struct net_device* dev)
-{
-
-/* FIXME: This code generates kernel panic during
-          router stop!. Investigate futher.
-	  (Error is dereferencing a NULL pointer)
-
-	if(dev->priv){
-
-		kfree(dev->priv);
-                dev->priv = NULL;
+	/* Initialize the polling task routine */
+#ifndef LINUX_2_4
+	chdlc_priv_area->poll_task.next = NULL;
+#endif
+	chdlc_priv_area->poll_task.sync=0;
+	chdlc_priv_area->poll_task.routine = (void*)(void*)chdlc_poll;
+	chdlc_priv_area->poll_task.data = dev;
+
+	/* Initialize the polling delay timer */
+	init_timer(&chdlc_priv_area->poll_delay_timer);
+	chdlc_priv_area->poll_delay_timer.data = (unsigned long)dev;
+	chdlc_priv_area->poll_delay_timer.function = chdlc_poll_delay;
+	
+	printk(KERN_INFO "\n");
 
-        }
-*/
 	return 0;
 }
 
@@ -640,12 +957,12 @@
  * interface registration.  Returning anything but zero will fail interface
  * registration.
  */
-static int if_init (struct net_device* dev)
+static int if_init (netdevice_t* dev)
 	{
 	chdlc_private_area_t* chdlc_priv_area = dev->priv;
 	sdla_t* card = chdlc_priv_area->card;
 	wan_device_t* wandev = &card->wandev;
-#ifndef LINUX_2_1
+#ifdef LINUX_2_0
 	int i;
 #endif
 
@@ -656,22 +973,41 @@
 	dev->rebuild_header	= &if_rebuild_hdr;
 	dev->hard_start_xmit	= &if_send;
 	dev->get_stats		= &if_stats;
+#ifdef LINUX_2_4
 	dev->tx_timeout		= &if_tx_timeout;
 	dev->watchdog_timeo	= TX_TIMEOUT;
+#endif
 
+	
 	/* Initialize media-specific parameters */
-	dev->flags		|= IFF_POINTTOPOINT;
+	dev->flags		|= IFF_POINTOPOINT;
+	dev->flags		|= IFF_NOARP;
 
 	/* Enable Mulitcasting if user selected */
 	if (chdlc_priv_area->mc == WANOPT_YES){
 		dev->flags 	|= IFF_MULTICAST;
 	}
-
-#ifndef LINUX_2_1
+	
+#ifdef LINUX_2_0
 	dev->family		= AF_INET;
 #endif	
-	dev->type		= ARPHRD_PPP;	/* ARP hw type -- dummy value */
+
+	if (chdlc_priv_area->true_if_encoding){
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
+		dev->type	= ARPHRD_HDLC; /* This breaks the tcpdump */
+#else
+		dev->type	= ARPHRD_PPP;
+#endif
+	}else{
+		dev->type	= ARPHRD_PPP;
+	}
+	
 	dev->mtu		= card->wandev.mtu;
+	/* for API usage, add the API header size to the requested MTU size */
+	if(card->u.c.usedby == API) {
+		dev->mtu += sizeof(api_tx_hdr_t);
+	}
+ 
 	dev->hard_header_len	= CHDLC_HDR_LEN;
 
 	/* Initialize hardware parameters */
@@ -688,13 +1024,10 @@
         dev->tx_queue_len = 100;
    
 	/* Initialize socket buffers */
-#ifdef LINUX_2_1
-	dev_init_buffers(dev);
-#else
+#if !defined(LINUX_2_1) && !defined(LINUX_2_4)
         for (i = 0; i < DEV_NUMBUFFS; ++i)
                 skb_queue_head_init(&dev->buffs[i]);
 #endif
-
 	return 0;
 }
 
@@ -705,85 +1038,63 @@
  *
  * Return 0 if O.k. or errno.
  */
-static int if_open (struct net_device* dev)
+static int if_open (netdevice_t* dev)
 {
 	chdlc_private_area_t* chdlc_priv_area = dev->priv;
 	sdla_t* card = chdlc_priv_area->card;
-	SHARED_MEMORY_INFO_STRUCT* flags = card->u.c.flags;
 	struct timeval tv;
 	int err = 0;
 
 	/* Only one open per interface is allowed */
 
-	if(netif_running(dev))
+	if (is_dev_running(dev))
 		return -EBUSY;
-	
-	if(test_and_set_bit(1, (void*)&card->wandev.critical)) {
-		return -EAGAIN;
-	}
-	
-	/* Setup the Board for CHDLC */
-	if (set_chdlc_config(card)) {
-		clear_bit(1, (void*)&card->wandev.critical);
-		return -EIO;
-	}
-
-	if (!card->configured && !card->wandev.piggyback){	
-		/* Perform interrupt testing */
-		err = intr_test(card, dev);
-
-		if(err || (Intr_test_counter < MAX_INTR_TEST_COUNTER)) { 
-			printk(KERN_ERR "%s: Interrupt test failed (%i)\n",
-					card->devname, Intr_test_counter);
-			printk(KERN_ERR "%s: Please choose another interrupt\n",
-					card->devname);
-			clear_bit(1, (void*)&card->wandev.critical);
-			return -EIO;
-		}
-		
-		printk(KERN_INFO "%s: Interrupt test passed (%i)\n", 
-				card->devname, Intr_test_counter);
-		card->configured = 1;
-	}else{
-		printk(KERN_INFO "%s: Card configured, skip interrupt test\n", 
-				card->devname);
-	}
-
-	/* Initialize Rx/Tx buffer control fields */
-	init_chdlc_tx_rx_buff(card, dev);
-
-	/* Set interrupt mode and mask */
-        if (chdlc_set_intr_mode(card, APP_INT_ON_RX_FRAME |
-                		APP_INT_ON_GLOBAL_EXCEP_COND |
-                		APP_INT_ON_TX_FRAME |
-                		APP_INT_ON_CHDLC_EXCEP_COND | APP_INT_ON_TIMER)){
 
-			clear_bit(1, (void*)&card->wandev.critical);
-                        return -EIO;
-        }
-	
-
-	/* Mask the Transmit and Timer interrupt */
-	flags->interrupt_info_struct.interrupt_permission &= 
-		~(APP_INT_ON_TX_FRAME | APP_INT_ON_TIMER);
-
-
-	/* Enable communications */
-	if (chdlc_comm_enable(card)) {
-		clear_bit(1, (void*)&card->wandev.critical);
-		return -EIO;
-	}
-
-	clear_bit(1, (void*)&card->wandev.critical);
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
+	/* Initialize the task queue */
+	chdlc_priv_area->tq_working=0;
 
-	port_set_state(card, WAN_CONNECTING);
+#ifndef LINUX_2_4
+	chdlc_priv_area->common.wanpipe_task.next = NULL;
+#endif
+	chdlc_priv_area->common.wanpipe_task.sync = 0;
+	chdlc_priv_area->common.wanpipe_task.routine = (void *)(void *)chdlc_bh;
+	chdlc_priv_area->common.wanpipe_task.data = dev;
+
+	/* Allocate and initialize BH circular buffer */
+	/* Add 1 to MAX_BH_BUFF so we don't have test with (MAX_BH_BUFF-1) */
+	chdlc_priv_area->bh_head = kmalloc((sizeof(bh_data_t)*(MAX_BH_BUFF+1)),GFP_ATOMIC);
+	memset(chdlc_priv_area->bh_head,0,(sizeof(bh_data_t)*(MAX_BH_BUFF+1)));
+	atomic_set(&chdlc_priv_area->bh_buff_used, 0);
+#endif
+ 
 	do_gettimeofday(&tv);
 	chdlc_priv_area->router_start_time = tv.tv_sec;
- 
+
+#ifdef LINUX_2_4
 	netif_start_queue(dev);
-	dev->flags |= IFF_POINTTOPOINT;
+#else
+	dev->interrupt = 0;
+	dev->tbusy = 0;
+	dev->start = 1;
+#endif
+
 	wanpipe_open(card);
 
+	/* TTY is configured during wanpipe_set_termios
+	 * call, not here */
+	if (card->tty_opt)
+		return err;
+	
+	set_bit(0,&chdlc_priv_area->config_chdlc);
+	chdlc_priv_area->config_chdlc_timeout=jiffies;
+	del_timer(&chdlc_priv_area->poll_delay_timer);
+
+	/* Start the CHDLC configuration after 1sec delay.
+	 * This will give the interface initilization time
+	 * to finish its configuration */
+	chdlc_priv_area->poll_delay_timer.expires=jiffies+HZ;
+	add_timer(&chdlc_priv_area->poll_delay_timer);
 	return err;
 }
 
@@ -792,25 +1103,76 @@
  * o if this is the last close, then disable communications and interrupts.
  * o reset flags.
  */
-static int if_close (struct net_device* dev)
+static int if_close (netdevice_t* dev)
 {
 	chdlc_private_area_t* chdlc_priv_area = dev->priv;
 	sdla_t* card = chdlc_priv_area->card;
 
-	if(test_and_set_bit(1, (void*)&card->wandev.critical))
-		return -EAGAIN;
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
 
-	netif_stop_queue(dev);
+	if (chdlc_priv_area->bh_head){
+		int i;
+		struct sk_buff *skb;
+	
+		for (i=0; i<(MAX_BH_BUFF+1); i++){
+			skb = ((bh_data_t *)&chdlc_priv_area->bh_head[i])->skb;
+			if (skb != NULL){
+                		wan_dev_kfree_skb(skb, FREE_READ);
+			}
+		}
+		kfree(chdlc_priv_area->bh_head);
+		chdlc_priv_area->bh_head=NULL;
+	}
+#endif
+
+	stop_net_queue(dev);
+#ifndef LINUX_2_4
+	dev->start=0;
+#endif
 	wanpipe_close(card);
-	port_set_state(card, WAN_DISCONNECTED);
-	chdlc_set_intr_mode(card, 0);
-	chdlc_comm_disable(card);
+	del_timer(&chdlc_priv_area->poll_delay_timer);
+	return 0;
+}
 
-	clear_bit(1, (void*)&card->wandev.critical);
+static void disable_comm (sdla_t *card)
+{
+	SHARED_MEMORY_INFO_STRUCT *flags = card->u.c.flags;
+	
+	if (card->u.c.comm_enabled){
+		chdlc_disable_comm_shutdown (card);
+	}else{
+		flags->interrupt_info_struct.interrupt_permission = 0;	
+	}
 
-	return 0;
+#if defined(LINUX_2_4) || defined(LINUX_2_1)	
+	if (!tty_init_cnt)
+		return;
+
+	if (card->tty_opt){
+		struct serial_state * state;
+		if (!(--tty_init_cnt)){
+			int e1,e2;
+			*serial_driver.refcount=0;
+			
+			if ((e1 = tty_unregister_driver(&serial_driver)))
+				printk("SERIAL: failed to unregister serial driver (%d)\n",
+				       e1);
+			if ((e2 = tty_unregister_driver(&callout_driver)))
+				printk("SERIAL: failed to unregister callout driver (%d)\n", 
+				       e2);
+			printk(KERN_INFO "%s: Unregistering TTY Driver, Major %i\n",
+					card->devname,WAN_TTY_MAJOR);
+		}
+		card->tty=NULL;
+		tty_card_map[card->tty_minor]=NULL;
+		state = &rs_table[card->tty_minor];
+		memset(state,0,sizeof(state));
+	}
+#endif
+	return;
 }
 
+
 /*============================================================================
  * Build media header.
  *
@@ -820,7 +1182,7 @@
  *
  * Return:	media header length.
  */
-static int if_header (struct sk_buff* skb, struct net_device* dev,
+static int if_header (struct sk_buff* skb, netdevice_t* dev,
 	unsigned short type, void* daddr, void* saddr, unsigned len)
 {
 	skb->protocol = htons(type);
@@ -828,49 +1190,50 @@
 	return CHDLC_HDR_LEN;
 }
 
+
+#ifdef LINUX_2_4
+/*============================================================================
+ * Handle transmit timeout event from netif watchdog
+ */
+static void if_tx_timeout (netdevice_t *dev)
+{
+    	chdlc_private_area_t* chan = dev->priv;
+	sdla_t *card = chan->card;
+	
+	/* If our device stays busy for at least 5 seconds then we will
+	 * kick start the device by making dev->tbusy = 0.  We expect
+	 * that our device never stays busy more than 5 seconds. So this                 
+	 * is only used as a last resort.
+	 */
+
+	++card->wandev.stats.collisions;
+
+	printk (KERN_INFO "%s: Transmit timed out on %s\n", card->devname,dev->name);
+	netif_wake_queue (dev);
+}
+#endif
+
+
+
 /*============================================================================
  * Re-build media header.
  *
  * Return:	1	physical address resolved.
  *		0	physical address not resolved
  */
-#ifdef LINUX_2_1
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
 static int if_rebuild_hdr (struct sk_buff *skb)
 {
 	return 1;
 }
 #else
-static int if_rebuild_hdr (void* hdr, struct net_device* dev, unsigned long raddr,
+static int if_rebuild_hdr (void* hdr, netdevice_t* dev, unsigned long raddr,
                            struct sk_buff* skb)
 {
         return 1;
 }
 #endif
 
-
-/*============================================================================
- * Handle transmit timeout event from netif watchdog
- */
-static void if_tx_timeout (struct net_device *dev)
-{
-	chdlc_private_area_t *chdlc_priv_area = dev->priv;
-	sdla_t *card = chdlc_priv_area->card;
-
-	/* If our device stays busy for at least 5 seconds then we will
-	 * kick start the device by making dev->tbusy = 0.  We expect 
-	 * that our device never stays busy more than 5 seconds. So this
-	 * is only used as a last resort. 
-	 */
-	++card->wandev.stats.collisions;
-
-	printk (KERN_INFO "%s: Transmit timeout !\n",
-		card->devname);
-
-	/* unbusy the interface */
-	netif_wake_queue (dev);
-}
-
-
 /*============================================================================
  * Send a packet on a network interface.
  * o set tbusy flag (marks start of the transmission) to block a timer-based
@@ -888,7 +1251,7 @@
  * 2. Setting tbusy flag will inhibit further transmit requests from the
  *    protocol stack and can be used for flow control with protocol layer.
  */
-static int if_send (struct sk_buff* skb, struct net_device* dev)
+static int if_send (struct sk_buff* skb, netdevice_t* dev)
 {
 	chdlc_private_area_t *chdlc_priv_area = dev->priv;
 	sdla_t *card = chdlc_priv_area->card;
@@ -896,33 +1259,68 @@
 	INTERRUPT_INFORMATION_STRUCT *chdlc_int = &flags->interrupt_info_struct;
 	int udp_type = 0;
 	unsigned long smp_flags;
+	int err=0;
 
-	if(skb == NULL) {
+#ifdef LINUX_2_4
+	netif_stop_queue(dev);
+#endif
+	
+	if (skb == NULL){
 		/* If we get here, some higher layer thinks we've missed an
 		 * tx-done interrupt.
 		 */
 		printk(KERN_INFO "%s: interface %s got kicked!\n",
 			card->devname, dev->name);
-		netif_wake_queue(dev);
+
+		wake_net_dev(dev);
 		return 0;
 	}
 
-   	if(ntohs(skb->protocol) != 0x16) {
+#ifndef LINUX_2_4
+	if (dev->tbusy){
+
+		/* If our device stays busy for at least 5 seconds then we will
+		 * kick start the device by making dev->tbusy = 0.  We expect 
+		 * that our device never stays busy more than 5 seconds. So this
+		 * is only used as a last resort. 
+		 */
+                ++card->wandev.stats.collisions;
+		if((jiffies - chdlc_priv_area->tick_counter) < (5 * HZ)) {
+			return 1;
+		}
+
+		printk (KERN_INFO "%s: Transmit timeout !\n",
+			card->devname);
+
+		/* unbusy the interface */
+		clear_bit(0,&dev->tbusy);
+	}
+#endif
+
+   	if (ntohs(skb->protocol) != htons(PVC_PROT)){
 
 		/* check the udp packet type */
+		
 		udp_type = udp_pkt_type(skb, card);
-		if(udp_type == UDP_CPIPE_TYPE) {
+
+		if (udp_type == UDP_CPIPE_TYPE){
                         if(store_udp_mgmt_pkt(UDP_PKT_FRM_STACK, card, skb, dev,
-                                chdlc_priv_area))
+                                chdlc_priv_area)){
 	                	chdlc_int->interrupt_permission |=
 					APP_INT_ON_TIMER;
+			}
+			start_net_queue(dev);
 			return 0;
 		}
 
 		/* check to see if the source IP address is a broadcast or */
 		/* multicast IP address */
-                if(chk_bcast_mcast_addr(card, dev, skb))
-                        return 0;
+                if(chk_bcast_mcast_addr(card, dev, skb)){
+			++card->wandev.stats.tx_dropped;
+			wan_dev_kfree_skb(skb,FREE_WRITE);
+			start_net_queue(dev);
+			return 0;
+		}
         }
 
 	/* Lock the 508 Card: SMP is supported */
@@ -930,29 +1328,24 @@
 		s508_lock(card,&smp_flags);
 	} 
 
-    	if(test_and_set_bit(0, (void*)&card->wandev.critical)) {
+    	if(test_and_set_bit(SEND_CRIT, (void*)&card->wandev.critical)) {
 	
 		printk(KERN_INFO "%s: Critical in if_send: %lx\n",
 					card->wandev.name,card->wandev.critical);
                 ++card->wandev.stats.tx_dropped;
-#ifdef LINUX_2_1
-    		dev_kfree_skb(skb);
-#else
-                dev_kfree_skb(skb, FREE_WRITE);
-#endif	
-		if(card->hw.type != SDLA_S514){
-			s508_unlock(card,&smp_flags);
-		}
-		return 0;
+		start_net_queue(dev);
+		goto if_send_exit_crit;
 	}
 
-	if(card->u.c.state != WAN_CONNECTED)
+	if(card->u.c.state != WAN_CONNECTED){
        		++card->wandev.stats.tx_dropped;
-
-	else if(!skb->protocol)
+		start_net_queue(dev);
+		
+	}else if(!skb->protocol){
         	++card->wandev.stats.tx_errors;
-
-	else {
+		start_net_queue(dev);
+		
+	}else {
 		void* data = skb->data;
 		unsigned len = skb->len;
 		unsigned char attr;
@@ -964,18 +1357,14 @@
 		if (card->u.c.usedby == API){
 			api_tx_hdr_t* api_tx_hdr;
 
-			if (len <= sizeof(api_tx_hdr_t)){
-#ifdef LINUX_2_1
- 		               	dev_kfree_skb(skb);
-#else
-                		dev_kfree_skb(skb, FREE_WRITE);
-#endif
+			/* discard the frame if we are configured for */
+			/* 'receive only' mode or if there is no data */
+			if (card->u.c.receive_only ||
+				(len <= sizeof(api_tx_hdr_t))) {
+				
 				++card->wandev.stats.tx_dropped;
-				clear_bit(0, (void*)&card->wandev.critical);
-				if(card->hw.type != SDLA_S514){
-					s508_unlock(card,&smp_flags);
-				}
-                		return 0;
+				start_net_queue(dev);
+				goto if_send_exit_crit;
 			}
 				
 			api_tx_hdr = (api_tx_hdr_t *)data;
@@ -985,24 +1374,36 @@
 		}
 
 		if(chdlc_send(card, data, len)) {
-			netif_stop_queue(dev);
-			chdlc_priv_area->tick_counter = jiffies;
-			chdlc_int->interrupt_permission |= APP_INT_ON_TX_FRAME;
-		}
-		else {
+			stop_net_queue(dev);
+		}else{
 			++card->wandev.stats.tx_packets;
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
                         card->wandev.stats.tx_bytes += len;
+#endif
+			
+			start_net_queue(dev);
+			
+#ifdef LINUX_2_4
+		 	dev->trans_start = jiffies;
+#endif
 		}	
 	}
 
-	if (!netif_queue_stopped(dev))
-		dev_kfree_skb(skb);
+if_send_exit_crit:
+	
+	if (!(err=is_queue_stopped(dev))) {
+		wan_dev_kfree_skb(skb, FREE_WRITE);
+	}else{
+		chdlc_priv_area->tick_counter = jiffies;
+		chdlc_int->interrupt_permission |= APP_INT_ON_TX_FRAME;
+	}
 
-	clear_bit(0, (void*)&card->wandev.critical);
+	clear_bit(SEND_CRIT, (void*)&card->wandev.critical);
 	if(card->hw.type != SDLA_S514){
 		s508_unlock(card,&smp_flags);
 	}
-	return netif_queue_stopped(dev);
+	
+	return err;
 }
 
 
@@ -1011,19 +1412,19 @@
  * multicast source IP address.
  */
 
-static int chk_bcast_mcast_addr(sdla_t *card, struct net_device* dev,
+static int chk_bcast_mcast_addr(sdla_t *card, netdevice_t* dev,
 				struct sk_buff *skb)
 {
 	u32 src_ip_addr;
         u32 broadcast_ip_addr = 0;
-#ifdef LINUX_2_1
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
         struct in_device *in_dev;
 #endif
         /* read the IP source address from the outgoing packet */
         src_ip_addr = *(u32 *)(skb->data + 12);
 
 	/* read the IP broadcast address for the device */
-#ifdef LINUX_2_1
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
         in_dev = dev->ip_ptr;
         if(in_dev != NULL) {
                 struct in_ifaddr *ifa= in_dev->ifa_list;
@@ -1040,12 +1441,6 @@
         if((dev->flags & IFF_BROADCAST) && (src_ip_addr == broadcast_ip_addr)) {
                 printk(KERN_INFO "%s: Broadcast Source Address silently discarded\n",
 				card->devname);
-#ifdef LINUX_2_1
-                dev_kfree_skb(skb);
-#else
-                dev_kfree_skb(skb, FREE_WRITE);
-#endif
-                ++card->wandev.stats.tx_dropped;
                 return 1;
         } 
 
@@ -1054,12 +1449,6 @@
 		(ntohl(src_ip_addr) <= 0xFFFFFFFE)) {
                 printk(KERN_INFO "%s: Multicast Source Address silently discarded\n",
 				card->devname);
-#ifdef LINUX_2_1
-                dev_kfree_skb(skb);
-#else
-                dev_kfree_skb(skb, FREE_WRITE);
-#endif
-                ++card->wandev.stats.tx_dropped;
                 return 1;
         }
 
@@ -1169,23 +1558,29 @@
 
 /*============================================================================
  * Get ethernet-style interface statistics.
- * Return a pointer to struct net_device_stats.
+ * Return a pointer to struct enet_statistics.
  */
-#ifdef LINUX_2_1
-static struct net_device_stats* if_stats (struct net_device* dev)
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
+static struct net_device_stats* if_stats (netdevice_t* dev)
 {
 	sdla_t *my_card;
-	chdlc_private_area_t* chdlc_priv_area = dev->priv;
+	chdlc_private_area_t* chdlc_priv_area;
+
+	if ((chdlc_priv_area=dev->priv) == NULL)
+		return NULL;
 
 	my_card = chdlc_priv_area->card;
 	return &my_card->wandev.stats; 
 }
 #else
-static struct net_device_stats* if_stats (struct net_device* dev)
+static struct enet_statistics* if_stats (netdevice_t* dev)
 {
         sdla_t *my_card;
         chdlc_private_area_t* chdlc_priv_area = dev->priv;
 
+	if ((chdlc_priv_area=dev->priv) == NULL)
+		return NULL;
+
         my_card = chdlc_priv_area->card;
         return &my_card->wandev.stats;
 }
@@ -1261,44 +1656,63 @@
 }
 
 
-/*============================================================================
- * Enable communications.
- */
+/*===========================================================
+ * chdlc_disable_comm_shutdown
+ *
+ * Shutdown() disables the communications. We must
+ * have a sparate functions, because we must not
+ * call chdlc_error() hander since the private
+ * area has already been replaced */
 
-static int chdlc_comm_enable (sdla_t* card)
+static int chdlc_disable_comm_shutdown (sdla_t *card)
 {
-	int err;
 	CHDLC_MAILBOX_STRUCT* mb = card->mbox;
+	CHDLC_INT_TRIGGERS_STRUCT* int_data =
+		 (CHDLC_INT_TRIGGERS_STRUCT *)mb->data;
+	int err;
+
+	/* Disable Interrutps */
+	int_data->CHDLC_interrupt_triggers 	= 0;
+	int_data->IRQ				= card->hw.irq;
+	int_data->interrupt_timer               = 1;
+   
+	mb->buffer_length = sizeof(CHDLC_INT_TRIGGERS_STRUCT);
+	mb->command = SET_CHDLC_INTERRUPT_TRIGGERS;
+	err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
 
+	/* Disable Communications */
+
+	if (card->u.c.async_mode) {
+		mb->command = DISABLE_ASY_COMMUNICATIONS;
+	}else{
+		mb->command = DISABLE_CHDLC_COMMUNICATIONS;
+	}
+	
 	mb->buffer_length = 0;
-	mb->command = ENABLE_CHDLC_COMMUNICATIONS;
 	err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
-	if (err != COMMAND_OK)
-		chdlc_error(card, err, mb);
-	return err;
+	
+	card->u.c.comm_enabled = 0;
+	
+	return 0;
 }
 
 /*============================================================================
- * Disable communications and Drop the Modem lines (DCD and RTS).
+ * Enable communications.
  */
-static int chdlc_comm_disable (sdla_t* card)
+
+static int chdlc_comm_enable (sdla_t* card)
 {
 	int err;
 	CHDLC_MAILBOX_STRUCT* mb = card->mbox;
 
 	mb->buffer_length = 0;
-	mb->command = DISABLE_CHDLC_COMMUNICATIONS;
-	err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
-	if (err != COMMAND_OK)
-		chdlc_error(card,err,mb);
-
-	mb->command = SET_MODEM_STATUS;
-	mb->buffer_length = 1;
-	mb->data[0] = 0;
+	mb->command = ENABLE_CHDLC_COMMUNICATIONS;
 	err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
 	if (err != COMMAND_OK)
-		chdlc_error(card,err,mb);
-
+		chdlc_error(card, err, mb);
+	else
+		card->u.c.comm_enabled = 1;
+	
 	return err;
 }
 
@@ -1397,10 +1811,10 @@
 	
 	/* Update transmit buffer control fields */
 	card->u.c.txbuf = ++txbuf;
-
+	
 	if ((void*)txbuf > card->u.c.txbuf_last)
 		card->u.c.txbuf = card->u.c.txbuf_base;
-
+	
 	return 0;
 }
 
@@ -1420,7 +1834,7 @@
 	switch (err) {
 
 	case CMD_TIMEOUT:
-		printk(KERN_ERR "%s: command 0x%02X timed out!\n",
+		printk(KERN_INFO "%s: command 0x%02X timed out!\n",
 			card->devname, cmd);
 		break;
 
@@ -1440,17 +1854,112 @@
 	return 0;
 }
 
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
+/********** Bottom Half Handlers ********************************************/
+
+/* NOTE: There is no API, BH support for Kernels lower than 2.2.X.
+ *       DO NOT INSERT ANY CODE HERE, NOTICE THE 
+ *       PREPROCESSOR STATEMENT ABOVE, UNLESS YOU KNOW WHAT YOU ARE
+ *       DOING */
+
+static void chdlc_bh (netdevice_t * dev)
+{
+	chdlc_private_area_t* chan = dev->priv;
+	sdla_t *card = chan->card;
+	struct sk_buff *skb;
+
+	if (atomic_read(&chan->bh_buff_used) == 0){
+		clear_bit(0, &chan->tq_working);
+		return;
+	}
+
+	while (atomic_read(&chan->bh_buff_used)){
+
+		skb  = ((bh_data_t *)&chan->bh_head[chan->bh_read])->skb;
+
+		if (skb != NULL){
+
+			if (chan->common.sk == NULL || chan->common.func == NULL){
+				++card->wandev.stats.rx_dropped;
+				wan_dev_kfree_skb(skb, FREE_READ);
+				chdlc_bh_cleanup(dev);
+				continue;
+			}
+
+			if (chan->common.func(skb,dev,chan->common.sk) != 0){
+				/* Sock full cannot send, queue us for another
+                                 * try */
+				atomic_set(&chan->common.receive_block,1);
+				return;
+			}else{
+				chdlc_bh_cleanup(dev);
+			}
+		}else{
+			chdlc_bh_cleanup(dev);
+		}
+	}	
+	clear_bit(0, &chan->tq_working);
+
+	return;
+}
+
+static int chdlc_bh_cleanup (netdevice_t *dev)
+{
+	chdlc_private_area_t* chan = dev->priv;
+
+	((bh_data_t *)&chan->bh_head[chan->bh_read])->skb = NULL;
+
+	if (chan->bh_read == MAX_BH_BUFF){
+		chan->bh_read=0;
+	}else{
+		++chan->bh_read;	
+	}
+
+	atomic_dec(&chan->bh_buff_used);
+	return 0;
+}
+
+
+
+static int bh_enqueue (netdevice_t *dev, struct sk_buff *skb)
+{
+	/* Check for full */
+	chdlc_private_area_t* chan = dev->priv;
+	sdla_t *card = chan->card;
+
+	if (atomic_read(&chan->bh_buff_used) == (MAX_BH_BUFF+1)){
+		++card->wandev.stats.rx_dropped;
+		wan_dev_kfree_skb(skb, FREE_READ);
+		return 1; 
+	}
+
+	((bh_data_t *)&chan->bh_head[chan->bh_write])->skb = skb;
+
+	if (chan->bh_write == MAX_BH_BUFF){
+		chan->bh_write=0;
+	}else{
+		++chan->bh_write;
+	}
+
+	atomic_inc(&chan->bh_buff_used);
+
+	return 0;
+}
+
+/* END OF API BH Support */
+
+#endif
+
 /****** Interrupt Handlers **************************************************/
 
 /*============================================================================
  * Cisco HDLC interrupt service routine.
  */
-STATIC void wpc_isr (sdla_t* card)
+static void wpc_isr (sdla_t* card)
 {
-	struct net_device* dev;
-	chdlc_private_area_t* chdlc_priv_area;
+	netdevice_t* dev;
 	SHARED_MEMORY_INFO_STRUCT* flags = NULL;
-	int i, interrupt_serviced = 0;
+	int i;
 	sdla_t *my_card;
 
 
@@ -1462,95 +1971,97 @@
 	flags = card->u.c.flags;
 	if (!flags->interrupt_info_struct.interrupt_type){
 		/* Check for a second port (piggybacking) */
-		if((my_card = card->next)){
+		if ((my_card = card->next)){
 			flags = my_card->u.c.flags;
 			if (flags->interrupt_info_struct.interrupt_type){
 				card = my_card;
+				card->isr(card);
+				return;
 			}
 		}
 	}
 
+	flags = card->u.c.flags;
+	card->in_isr = 1;
 	dev = card->wandev.dev;
 	
-	card->in_isr = 1;
+	/* If we get an interrupt with no network device, stop the interrupts
+	 * and issue an error */
+	if (!card->tty_opt && !dev && 
+	    flags->interrupt_info_struct.interrupt_type != 
+	    	COMMAND_COMPLETE_APP_INT_PEND){
+
+		goto isr_done;
+	}
 	
 	/* if critical due to peripheral operations
 	 * ie. update() or getstats() then reset the interrupt and
 	 * wait for the board to retrigger.
 	 */
-	if(test_bit(1, (void*)&card->wandev.critical)) {
-               	if(card->u.c.flags != NULL) {
-                       	flags = card->u.c.flags;
-			if(flags->interrupt_info_struct.
-					interrupt_type) {
-					flags->interrupt_info_struct.
-						interrupt_type = 0;
-			}
-		}
-		card->in_isr = 0;
-		return;
+	if(test_bit(PERI_CRIT, (void*)&card->wandev.critical)) {
+		printk(KERN_INFO "ISR CRIT TO PERI\n");
+		goto isr_done;
 	}
 
-
 	/* On a 508 Card, if critical due to if_send 
-         * Major Error !!!
-	 */
+         * Major Error !!! */
 	if(card->hw.type != SDLA_S514) {
-		if(test_and_set_bit(0, (void*)&card->wandev.critical)) {
+		if(test_bit(SEND_CRIT, (void*)&card->wandev.critical)) {
 			printk(KERN_INFO "%s: Critical while in ISR: %lx\n",
 				card->devname, card->wandev.critical);
 			card->in_isr = 0;
+			flags->interrupt_info_struct.interrupt_type = 0;
 			return;
 		}
 	}
 
-	/* FIXME: Take this check out later in the future */
-	if(card->u.c.flags != NULL) {
-	
-		flags = card->u.c.flags;
+	switch(flags->interrupt_info_struct.interrupt_type) {
 
-		switch(flags->interrupt_info_struct.interrupt_type) {
+	case RX_APP_INT_PEND:	/* 0x01: receive interrupt */
+		rx_intr(card);
+		break;
 
-		case RX_APP_INT_PEND:	/* 0x01: receive interrupt */
-			interrupt_serviced = 1;
-			rx_intr(card);
-			break;
+	case TX_APP_INT_PEND:	/* 0x02: transmit interrupt */
+		flags->interrupt_info_struct.interrupt_permission &=
+			 ~APP_INT_ON_TX_FRAME;
 
-		case TX_APP_INT_PEND:	/* 0x02: transmit interrupt */
-			interrupt_serviced = 1;
-			flags->interrupt_info_struct.interrupt_permission &=
-				 ~APP_INT_ON_TX_FRAME;
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
 
-			chdlc_priv_area = dev->priv;
-			netif_wake_queue(dev);
+		if (card->tty_opt){
+			wanpipe_tty_trigger_poll(card);
 			break;
+		}
 
-		case COMMAND_COMPLETE_APP_INT_PEND:/* 0x04: cmd cplt */
-			interrupt_serviced = 1;
-			++ Intr_test_counter;
-			break;
+		if (dev && is_queue_stopped(dev)){
+			if (card->u.c.usedby == API){
+				start_net_queue(dev);
+				wakeup_sk_bh(dev);
+			}else{
+				wake_net_dev(dev);
+			}
+		}
+#else
+		wake_net_dev(dev);
+#endif
+		break;
 
-		case CHDLC_EXCEP_COND_APP_INT_PEND:	/* 0x20 */
-			interrupt_serviced = 1;
-			process_chdlc_exception(card);
-			break;
+	case COMMAND_COMPLETE_APP_INT_PEND:/* 0x04: cmd cplt */
+		++ Intr_test_counter;
+		break;
 
-		case GLOBAL_EXCEP_COND_APP_INT_PEND:
-			interrupt_serviced = 1;
-			process_global_exception(card);
-			break;
+	case CHDLC_EXCEP_COND_APP_INT_PEND:	/* 0x20 */
+		process_chdlc_exception(card);
+		break;
 
-		case TIMER_APP_INT_PEND:
-			interrupt_serviced = 1;
-			timer_intr(card);
-			break;
+	case GLOBAL_EXCEP_COND_APP_INT_PEND:
+		process_global_exception(card);
+		break;
 
-		default:
-			break;
-		}	
-	}
+	case TIMER_APP_INT_PEND:
+		timer_intr(card);
+		break;
 
-	if(!interrupt_serviced) {
+	default:
 		printk(KERN_INFO "%s: spurious interrupt 0x%02X!\n", 
 			card->devname,
 			flags->interrupt_info_struct.interrupt_type);
@@ -1563,14 +2074,14 @@
 			printk(KERN_INFO "%c", 
 				flags->global_info_struct.codeversion[i]); 
 		printk(KERN_INFO "\n");	
+		break;
 	}
 
+isr_done:
+
 	card->in_isr = 0;
 	flags->interrupt_info_struct.interrupt_type = 0;
-        if(card->hw.type != SDLA_S514){
-        	clear_bit(0, (void*)&card->wandev.critical);
-	}
-        
+	return;
 }
 
 /*============================================================================
@@ -1578,15 +2089,16 @@
  */
 static void rx_intr (sdla_t* card)
 {
-	struct net_device 			*dev;
-	chdlc_private_area_t		*chdlc_priv_area;
-	SHARED_MEMORY_INFO_STRUCT 	*flags = card->u.c.flags;
-	CHDLC_DATA_RX_STATUS_EL_STRUCT	*rxbuf = card->u.c.rxmb;
-	struct sk_buff			*skb;
-	unsigned 			len;
-	void				*buf;
-	int 				i,udp_type;
-	
+	netdevice_t *dev;
+	chdlc_private_area_t *chdlc_priv_area;
+	SHARED_MEMORY_INFO_STRUCT *flags = card->u.c.flags;
+	CHDLC_DATA_RX_STATUS_EL_STRUCT *rxbuf = card->u.c.rxmb;
+	struct sk_buff *skb;
+	unsigned len;
+	unsigned addr = rxbuf->ptr_data_bfr;
+	void *buf;
+	int i,udp_type;
+
 	if (rxbuf->opp_flag != 0x01) {
 		printk(KERN_INFO 
 			"%s: corrupted Rx buffer @ 0x%X, flag = 0x%02X!\n", 
@@ -1600,90 +2112,132 @@
                         printk(KERN_INFO "%c",
                                 flags->global_info_struct.codeversion[i]);
                 printk(KERN_INFO "\n");
+
+
+		/* Bug Fix: Mar 6 2000
+                 * If we get a corrupted mailbox, it measn that driver 
+                 * is out of sync with the firmware. There is no recovery.
+                 * If we don't turn off all interrupts for this card
+                 * the machine will crash. 
+                 */
+		printk(KERN_INFO "%s: Critical router failure ...!!!\n", card->devname);
+		printk(KERN_INFO "Please contact Sangoma Technologies !\n");
+		chdlc_set_intr_mode(card,0);	
 		return;
 	}
 
+	len  = rxbuf->frame_length;
+
+#if defined(LINUX_2_4) || defined(LINUX_2_1)	
+	if (card->tty_opt){
+
+		if (rxbuf->error_flag){	
+			goto rx_exit;
+		}
+
+		if (len <= CRC_LENGTH){
+			goto rx_exit;
+		}
+		
+		if (!card->u.c.async_mode){
+			len -= CRC_LENGTH;
+		}
+
+		wanpipe_tty_receive(card,addr,len);
+		goto rx_exit;
+	}
+#endif
+
 	dev = card->wandev.dev;
-	chdlc_priv_area = dev->priv;
 
-	if(dev && netif_running(dev)) {
+	if (!dev){
+		goto rx_exit;
+	}
+
+	if (!is_dev_running(dev))
+		goto rx_exit;
 
-		len  = rxbuf->frame_length;
+	chdlc_priv_area = dev->priv;
 
-		/* Allocate socket buffer */
-		skb = dev_alloc_skb(len);
+	
+	/* Allocate socket buffer */
+	skb = dev_alloc_skb(len);
 
-		if (skb != NULL) {
-			/* Copy data to the socket buffer */
-			unsigned addr = rxbuf->ptr_data_bfr;
-		
-			if((addr + len) >
-			card->u.c.rx_top + 1) {
-				unsigned tmp = 
-				card->u.c.rx_top - addr + 1;
-				buf = skb_put(skb, tmp);
-				sdla_peek(&card->hw, addr, buf, tmp);
-				addr = card->u.c.rx_base;
-				len -= tmp;
-			}
+	if (skb == NULL) {
+		printk(KERN_INFO "%s: no socket buffers available!\n",
+					card->devname);
+		++card->wandev.stats.rx_dropped;
+		goto rx_exit;
+	}
+
+	/* Copy data to the socket buffer */
+	if((addr + len) > card->u.c.rx_top + 1) {
+		unsigned tmp = card->u.c.rx_top - addr + 1;
+		buf = skb_put(skb, tmp);
+		sdla_peek(&card->hw, addr, buf, tmp);
+		addr = card->u.c.rx_base;
+		len -= tmp;
+	}
 		
-			buf = skb_put(skb, len);
-			sdla_peek(&card->hw, addr, buf, len);
+	buf = skb_put(skb, len);
+	sdla_peek(&card->hw, addr, buf, len);
 
-			/* Decapsulate packet */
-			skb->protocol = htons(ETH_P_IP);
+	skb->protocol = htons(ETH_P_IP);
 
-			card->wandev.stats.rx_packets ++;
-#ifdef LINUX_2_1
-			card->wandev.stats.rx_bytes += skb->len;
+	card->wandev.stats.rx_packets ++;
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
+	card->wandev.stats.rx_bytes += skb->len;
 #endif
-			udp_type = udp_pkt_type( skb, card );
+	udp_type = udp_pkt_type( skb, card );
 
-			if(udp_type == UDP_CPIPE_TYPE) {
-				if(store_udp_mgmt_pkt(UDP_PKT_FRM_NETWORK,
-					card, skb, dev, chdlc_priv_area)) {
-       				        flags->interrupt_info_struct.
+	if(udp_type == UDP_CPIPE_TYPE) {
+		if(store_udp_mgmt_pkt(UDP_PKT_FRM_NETWORK,
+   				      card, skb, dev, chdlc_priv_area)) {
+     		        flags->interrupt_info_struct.
 						interrupt_permission |= 
 							APP_INT_ON_TIMER; 
-				}
-
-			} else {
- 
-				if(card->u.c.usedby == API) {
-					api_rx_hdr_t* api_rx_hdr;
-              				skb_push(skb, sizeof(api_rx_hdr_t));
-                                        api_rx_hdr =
-						(api_rx_hdr_t*)&skb->data[0x00];
-					api_rx_hdr->error_flag =
-						rxbuf->error_flag;
-          				api_rx_hdr->time_stamp =
-                                                rxbuf->time_stamp;
-                                	skb->protocol = htons(0x16);
-                                	skb->pkt_type = PACKET_HOST;
-				}
-
-/* FIXME: we should check to see if the received packet is a multicast packet so that we can increment the multicast statistic
-                                ++ chdlc_priv_area->if_stats.multicast;
-*/
-                               	/* Pass it up the protocol stack */
-                                skb->dev = dev;
-                                skb->mac.raw  = skb->data;
-                                netif_rx(skb);
-			}
-
-		} else {
-			printk(KERN_INFO
-				"%s: no socket buffers available!\n",
-					card->devname);
-			++card->wandev.stats.rx_dropped;
 		}
-     	}
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
+	} else if(card->u.c.usedby == API) {
 
-	/* Release buffer element and calculate a pointer to the next one */
-	rxbuf->opp_flag = 0x00;
-	card->u.c.rxmb = ++ rxbuf;
-	if((void*)rxbuf > card->u.c.rxbuf_last)
-		card->u.c.rxmb = card->u.c.rxbuf_base;
+		api_rx_hdr_t* api_rx_hdr;
+       		skb_push(skb, sizeof(api_rx_hdr_t));
+                api_rx_hdr = (api_rx_hdr_t*)&skb->data[0x00];
+		api_rx_hdr->error_flag = rxbuf->error_flag;
+     		api_rx_hdr->time_stamp = rxbuf->time_stamp;
+
+                skb->protocol = htons(PVC_PROT);
+     		skb->mac.raw  = skb->data;
+		skb->dev      = dev;
+               	skb->pkt_type = WAN_PACKET_DATA;
+
+		bh_enqueue(dev, skb);
+
+		if (!test_and_set_bit(0,&chdlc_priv_area->tq_working)){
+			wanpipe_queue_tq(&chdlc_priv_area->common.wanpipe_task);
+		        wanpipe_mark_bh();
+		}
+#endif
+	}else{
+		/* FIXME: we should check to see if the received packet is a 
+                          multicast packet so that we can increment the multicast 
+                          statistic
+                          ++ chdlc_priv_area->if_stats.multicast;
+		*/
+               	/* Pass it up the protocol stack */
+	
+                skb->dev = dev;
+                skb->mac.raw  = skb->data;
+                netif_rx(skb);
+	}
+
+rx_exit:
+	/* Release buffer element and calculate a pointer to the next one */
+	rxbuf->opp_flag = 0x00;
+	card->u.c.rxmb = ++ rxbuf;
+	if((void*)rxbuf > card->u.c.rxbuf_last){
+		card->u.c.rxmb = card->u.c.rxbuf_base;
+	}
 }
 
 /*============================================================================
@@ -1694,13 +2248,25 @@
  */
 void timer_intr(sdla_t *card)
 {
-        struct net_device* dev;
+        netdevice_t* dev;
         chdlc_private_area_t* chdlc_priv_area = NULL;
         SHARED_MEMORY_INFO_STRUCT* flags = NULL;
 
-        dev = card->wandev.dev; 
+        if ((dev = card->wandev.dev)==NULL){
+		flags = card->u.c.flags;
+                flags->interrupt_info_struct.interrupt_permission &=
+                        ~APP_INT_ON_TIMER;
+		return;
+	}
+	
         chdlc_priv_area = dev->priv;
 
+	if (chdlc_priv_area->timer_int_enabled & TMR_INT_ENABLED_CONFIG) {
+		if (!config_chdlc(card)){
+			chdlc_priv_area->timer_int_enabled &= ~TMR_INT_ENABLED_CONFIG;
+		}
+	}
+
 	/* process a udp call if pending */
        	if(chdlc_priv_area->timer_int_enabled & TMR_INT_ENABLED_UDP) {
                	process_udp_mgmt_pkt(card, dev,
@@ -1733,16 +2299,14 @@
 
 static int set_chdlc_config(sdla_t* card)
 {
-
-	struct net_device * dev = card->wandev.dev;
-	chdlc_private_area_t *chdlc_priv_area = dev->priv;
 	CHDLC_CONFIGURATION_STRUCT cfg;
 
 	memset(&cfg, 0, sizeof(CHDLC_CONFIGURATION_STRUCT));
 
-	if(card->wandev.clocking)
+	if(card->wandev.clocking){
 		cfg.baud_rate = card->wandev.bps;
-
+	}
+		
 	cfg.line_config_options = (card->wandev.interface == WANOPT_RS232) ?
 		INTERFACE_LEVEL_RS232 : INTERFACE_LEVEL_V35;
 
@@ -1750,9 +2314,18 @@
 	cfg.modem_status_timer		= 100;
 
 	cfg.CHDLC_protocol_options	= card->u.c.protocol_options;
-	cfg.percent_data_buffer_for_Tx	= 50;
+
+	if (card->tty_opt){
+		cfg.CHDLC_API_options	= DISCARD_RX_ERROR_FRAMES;
+	}
+	
+	cfg.percent_data_buffer_for_Tx  = (card->u.c.receive_only) ? 0 : 50;
 	cfg.CHDLC_statistics_options	= (CHDLC_TX_DATA_BYTE_COUNT_STAT |
 		CHDLC_RX_DATA_BYTE_COUNT_STAT);
+	
+	if (card->tty_opt){
+		card->wandev.mtu = TTY_CHDLC_MAX_MTU;
+	}
 	cfg.max_CHDLC_data_field_length	= card->wandev.mtu;
 	cfg.transmit_keepalive_timer	= card->u.c.kpalv_tx;
 	cfg.receive_keepalive_timer	= card->u.c.kpalv_rx;
@@ -1762,9 +2335,12 @@
 	if (cfg.SLARP_request_timer) {
 		cfg.IP_address		= 0;
 		cfg.IP_netmask		= 0;
-	}
-	else {
-#ifdef LINUX_2_1
+		
+	}else if (card->wandev.dev){
+		netdevice_t * dev = card->wandev.dev;
+		chdlc_private_area_t *chdlc_priv_area = dev->priv;
+		
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
                 struct in_device *in_dev = dev->ip_ptr;
 
 		if(in_dev != NULL) {
@@ -1773,10 +2349,8 @@
 			if (ifa != NULL ) {
 				cfg.IP_address	= ntohl(ifa->ifa_local);
 				cfg.IP_netmask	= ntohl(ifa->ifa_mask); 
-				chdlc_priv_area->IP_address = 
-					ntohl(ifa->ifa_local);
-				chdlc_priv_area->IP_netmask =
-					ntohl(ifa->ifa_mask); 
+				chdlc_priv_area->IP_address = ntohl(ifa->ifa_local);
+				chdlc_priv_area->IP_netmask = ntohl(ifa->ifa_mask); 
 			}
 		}
 #else
@@ -1803,6 +2377,70 @@
 }
 
 
+/*-----------------------------------------------------------------------------
+   set_asy_config() used to set asynchronous configuration options on the board
+------------------------------------------------------------------------------*/
+
+static int set_asy_config(sdla_t* card)
+{
+
+        ASY_CONFIGURATION_STRUCT cfg;
+ 	CHDLC_MAILBOX_STRUCT *mailbox = card->mbox;
+	int err;
+
+	memset(&cfg, 0, sizeof(ASY_CONFIGURATION_STRUCT));
+
+	if(card->wandev.clocking)
+		cfg.baud_rate = card->wandev.bps;
+
+	cfg.line_config_options = (card->wandev.interface == WANOPT_RS232) ?
+		INTERFACE_LEVEL_RS232 : INTERFACE_LEVEL_V35;
+
+	cfg.modem_config_options	= 0;
+	cfg.asy_API_options 		= card->u.c.api_options;
+	cfg.asy_protocol_options	= card->u.c.protocol_options;
+	cfg.Tx_bits_per_char		= card->u.c.tx_bits_per_char;
+	cfg.Rx_bits_per_char		= card->u.c.rx_bits_per_char;
+	cfg.stop_bits			= card->u.c.stop_bits;
+	cfg.parity			= card->u.c.parity;
+	cfg.break_timer			= card->u.c.break_timer;
+	cfg.asy_Rx_inter_char_timer	= card->u.c.inter_char_timer; 
+	cfg.asy_Rx_complete_length	= card->u.c.rx_complete_length; 
+	cfg.XON_char			= card->u.c.xon_char;
+	cfg.XOFF_char			= card->u.c.xoff_char;
+	cfg.asy_statistics_options	= (CHDLC_TX_DATA_BYTE_COUNT_STAT |
+		CHDLC_RX_DATA_BYTE_COUNT_STAT);
+
+	mailbox->buffer_length = sizeof(ASY_CONFIGURATION_STRUCT);
+	memcpy(mailbox->data, &cfg, mailbox->buffer_length);
+	mailbox->command = SET_ASY_CONFIGURATION;
+	err = sdla_exec(mailbox) ? mailbox->return_code : CMD_TIMEOUT;
+	if (err != COMMAND_OK) 
+		chdlc_error (card, err, mailbox);
+	return err;
+}
+
+/*============================================================================
+ * Enable asynchronous communications.
+ */
+
+static int asy_comm_enable (sdla_t* card)
+{
+
+	int err;
+	CHDLC_MAILBOX_STRUCT* mb = card->mbox;
+
+	mb->buffer_length = 0;
+	mb->command = ENABLE_ASY_COMMUNICATIONS;
+	err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
+	if (err != COMMAND_OK && card->wandev.dev)
+		chdlc_error(card, err, mb);
+	
+	if (!err)
+		card->u.c.comm_enabled = 1;
+
+	return err;
+}
 
 /*============================================================================
  * Process global exception condition
@@ -1830,7 +2468,8 @@
 					printk(KERN_INFO "%s: DCD high, CTS low\n",card->devname);
 					break;
 				case (CTS_HIGH):
-                                        printk(KERN_INFO "%s: DCD low, CTS high\n",card->devname);                                        break;
+                                        printk(KERN_INFO "%s: DCD low, CTS high\n",card->devname); 
+					break;
                                 case ((DCD_HIGH | CTS_HIGH)):
                                         printk(KERN_INFO "%s: DCD high, CTS high\n",card->devname);
                                         break;
@@ -1850,6 +2489,19 @@
 				card->devname); 
 			break;
 
+		case 0x17:
+			if (card->tty_opt){
+				if (card->tty && card->tty_open){ 
+					printk(KERN_INFO 
+						"%s: Modem Hangup Exception: Hanging Up!\n",
+						card->devname);
+					tty_hangup(card->tty);
+				}
+				break;
+			}
+
+			/* If TTY is not used just drop throught */
+			
                 default:
                         printk(KERN_INFO "%s: Global exception %x\n",
 				card->devname, mbox->return_code);
@@ -1877,11 +2529,13 @@
 
 		case EXCEP_LINK_ACTIVE:
 			port_set_state(card, WAN_CONNECTED);
+			trigger_chdlc_poll(card->wandev.dev);
 			break;
 
 		case EXCEP_LINK_INACTIVE_MODEM:
 			port_set_state(card, WAN_DISCONNECTED);
 			unconfigure_ip(card);
+			trigger_chdlc_poll(card->wandev.dev);
 			break;
 
 		case EXCEP_LINK_INACTIVE_KPALV:
@@ -1889,6 +2543,7 @@
 			printk(KERN_INFO "%s: Keepalive timer expired.\n",
 				 		card->devname);
 			unconfigure_ip(card);
+			trigger_chdlc_poll(card->wandev.dev);
 			break;
 
 		case EXCEP_IP_ADDRESS_DISCOVERED:
@@ -1919,10 +2574,16 @@
 
 static int configure_ip (sdla_t* card)
 {
-	struct net_device *dev = card->wandev.dev;
-        chdlc_private_area_t *chdlc_priv_area = dev->priv;
+	netdevice_t *dev = card->wandev.dev;
+        chdlc_private_area_t *chdlc_priv_area;
         char err;
 
+	if (!dev)
+		return 0;
+
+	chdlc_priv_area = dev->priv;
+	
+	
         /* set to discover */
         if(card->u.c.slarp_timer != 0x00) {
 		CHDLC_MAILBOX_STRUCT* mb = card->mbox;
@@ -1940,14 +2601,14 @@
 		cfg = (CHDLC_CONFIGURATION_STRUCT *)mb->data;
                 chdlc_priv_area->IP_address = cfg->IP_address;
                 chdlc_priv_area->IP_netmask = cfg->IP_netmask;
-        }
 
-	/* Set flag to add route */
-	chdlc_priv_area->route_status = ADD_ROUTE;
+		/* Set flag to add route */
+		chdlc_priv_area->route_status = ADD_ROUTE;
 
-	/* The idea here is to add the route in the poll routine.
-	   This way, we aren't in interrupt context when adding routes */
-	card->poll = process_route;
+		/* The idea here is to add the route in the poll routine.
+	   	This way, we aren't in interrupt context when adding routes */
+		trigger_chdlc_poll(dev);
+        }
 
 	return 0;
 }
@@ -1960,17 +2621,23 @@
 
 static int unconfigure_ip (sdla_t* card)
 {
-	struct net_device *dev = card->wandev.dev;
-	chdlc_private_area_t *chdlc_priv_area= dev->priv;
+	netdevice_t *dev = card->wandev.dev;
+	chdlc_private_area_t *chdlc_priv_area;
+
+	if (!dev)
+		return 0;
 
+	chdlc_priv_area= dev->priv;
+	
 	if (chdlc_priv_area->route_status == ROUTE_ADDED) {
-	      	chdlc_priv_area->route_status = REMOVE_ROUTE;
-		/* The idea here is to delete the route in 
-		 * the poll routine.
-	   	 * This way, we aren't in interrupt context 
-		 * when adding routes 
+
+		/* Note: If this function is called, the 
+                 * port state has been DISCONNECTED.  This state
+                 * change will trigger a poll_disconnected 
+                 * function, that will check for this condition. 
 		 */
-                card->poll = process_route;
+		chdlc_priv_area->route_status = REMOVE_ROUTE;
+
 	}
 	return 0;
 }
@@ -1982,14 +2649,14 @@
 
 static void process_route (sdla_t *card)
 {
-        struct net_device *dev = card->wandev.dev;
+        netdevice_t *dev = card->wandev.dev;
         unsigned char port_num;
         chdlc_private_area_t *chdlc_priv_area = NULL;
 	u32 local_IP_addr = 0;
 	u32 remote_IP_addr = 0;
 	u32 IP_netmask, IP_addr;
         int err = 0;
-#ifdef LINUX_2_1
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
 	struct in_device *in_dev;
 	mm_segment_t fs;
 	struct ifreq if_info;
@@ -2002,16 +2669,25 @@
         chdlc_priv_area = dev->priv;
         port_num = card->u.c.comm_port;
 
+	/* Bug Fix Mar 16 2000
+	 * AND the IP address to the Mask before checking
+         * the last two bits. */
+
 	if((chdlc_priv_area->route_status == ADD_ROUTE) &&
-		((chdlc_priv_area->IP_address & 0x000000FF) > 2)) {
+		((chdlc_priv_area->IP_address & ~chdlc_priv_area->IP_netmask) > 2)) {
+
 		printk(KERN_INFO "%s: Dynamic route failure.\n",card->devname);
+
                 if(card->u.c.slarp_timer) {
+
 			printk(KERN_INFO "%s: Bad IP address %s received\n",
 				card->devname,
 				in_ntoa(ntohl(chdlc_priv_area->IP_address)));
                         printk(KERN_INFO "%s: from remote station.\n",
 				card->devname);
+
                 }else{ 
+
                         printk(KERN_INFO "%s: Bad IP address %s issued\n",
 				card->devname,
 				in_ntoa(ntohl(chdlc_priv_area->IP_address)));
@@ -2033,11 +2709,10 @@
 
  	        /* do not remove a bad route that has already been removed */
         	if(chdlc_priv_area->route_removed) {
-                	card->poll = NULL;
 	                return;
         	}
 
-#ifdef LINUX_2_1
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
                 in_dev = dev->ip_ptr;
 
                 if(in_dev != NULL) {
@@ -2058,7 +2733,17 @@
 		*/
 		IP_netmask = ntohl(chdlc_priv_area->IP_netmask);
 	        remote_IP_addr = ntohl(chdlc_priv_area->IP_address);
-        	local_IP_addr = (remote_IP_addr & ntohl(0xFFFFFF00)) +
+	
+
+		/* If Netmask is 255.255.255.255 the local address
+                 * calculation will fail. Default it back to 255.255.255.0 */
+		if (IP_netmask == 0xffffffff)
+			IP_netmask &= 0x00ffffff;
+
+		/* Bug Fix Mar 16 2000
+		 * AND the Remote IP address with IP netmask, instead
+                 * of static netmask of 255.255.255.0 */
+        	local_IP_addr = (remote_IP_addr & IP_netmask) +
                 	(~remote_IP_addr & ntohl(0x0003));
 
 	        if(!card->u.c.slarp_timer) {
@@ -2071,7 +2756,7 @@
         fs = get_fs();                  /* Save file system  */
         set_fs(get_ds());               /* Get user space block */
 
-#ifdef LINUX_2_1
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
         /* Setup a structure for adding/removing routes */
         memset(&if_info, 0, sizeof(if_info));
         strcpy(if_info.ifr_name, dev->name);
@@ -2099,7 +2784,7 @@
 	case ADD_ROUTE:
 
 		if(!card->u.c.slarp_timer) {
-#ifdef LINUX_2_1
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
 			if_data2 = (struct sockaddr_in *)&if_info.ifr_dstaddr;
 			if_data2->sin_addr.s_addr = remote_IP_addr;
 			if_data2->sin_family = AF_INET;
@@ -2108,7 +2793,7 @@
                         err = ip_rt_new(&route);
 #endif
 		} else { 
-#ifdef LINUX_2_1
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
 			if_data1 = (struct sockaddr_in *)&if_info.ifr_addr;
 			if_data1->sin_addr.s_addr = local_IP_addr;
 			if_data1->sin_family = AF_INET;
@@ -2141,7 +2826,7 @@
 
 	case REMOVE_ROUTE:
 	
-#ifdef LINUX_2_1
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
 		/* Change the local ip address of the interface to 0.
 		 * This will also delete the destination route.
 		 */
@@ -2179,9 +2864,6 @@
 
         set_fs(fs);                     /* Restore file system */
 
-        /* Once we've processed the route, stop polling */
-        card->poll = NULL;
-
 }
 
 
@@ -2190,7 +2872,7 @@
  */
 
 static int store_udp_mgmt_pkt(char udp_pkt_src, sdla_t* card,
-                                struct sk_buff *skb, struct net_device* dev,
+                                struct sk_buff *skb, netdevice_t* dev,
                                 chdlc_private_area_t* chdlc_priv_area )
 {
 	int udp_pkt_stored = 0;
@@ -2204,15 +2886,12 @@
 		udp_pkt_stored = 1;
 	}
 
-#ifdef LINUX_2_1
-	dev_kfree_skb(skb);
-#else
-	if(udp_pkt_src == UDP_PKT_FRM_STACK)
-		dev_kfree_skb(skb, FREE_WRITE);
-	else
-                dev_kfree_skb(skb, FREE_READ);
-#endif
-	
+	if(udp_pkt_src == UDP_PKT_FRM_STACK){
+		wan_dev_kfree_skb(skb, FREE_WRITE);
+	}else{
+                wan_dev_kfree_skb(skb, FREE_READ);
+	}
+		
 	return(udp_pkt_stored);
 }
 
@@ -2221,7 +2900,7 @@
  * Process UDP management packet.
  */
 
-static int process_udp_mgmt_pkt(sdla_t* card, struct net_device* dev,
+static int process_udp_mgmt_pkt(sdla_t* card, netdevice_t* dev,
 				chdlc_private_area_t* chdlc_priv_area ) 
 {
 	unsigned char *buf;
@@ -2240,22 +2919,30 @@
 
 	chdlc_udp_pkt = (chdlc_udp_pkt_t *) chdlc_priv_area->udp_pkt_data;
 
-	switch(chdlc_udp_pkt->cblock.command) {
+	if(chdlc_priv_area->udp_pkt_src == UDP_PKT_FRM_NETWORK){
 
-	   	case FT1_MONITOR_STATUS_CTRL:
-		case CPIPE_ENABLE_TRACING:
-	   	case CPIPE_DISABLE_TRACING:
- 	  	case CPIPE_GET_TRACE_INFO:
-	   	case SET_FT1_MODE:
-			if(chdlc_priv_area->udp_pkt_src == 
-				UDP_PKT_FRM_NETWORK) {
-			     udp_mgmt_req_valid = 0;
-       			}	 
-       			break;
-	   
-		default:
-       			break;
-  	} 
+		/* Only these commands are support for remote debugging.
+		 * All others are not */
+		switch(chdlc_udp_pkt->cblock.command) {
+
+			case READ_GLOBAL_STATISTICS:
+			case READ_MODEM_STATUS:  
+			case READ_CHDLC_LINK_STATUS:
+			case CPIPE_ROUTER_UP_TIME:
+			case READ_COMMS_ERROR_STATS:
+			case READ_CHDLC_OPERATIONAL_STATS:
+
+			/* These two commands are executed for
+			 * each request */
+			case READ_CHDLC_CONFIGURATION:
+			case READ_CHDLC_CODE_VERSION:
+				udp_mgmt_req_valid = 1;
+				break;
+			default:
+				udp_mgmt_req_valid = 0;
+				break;
+		} 
+	}
 	
   	if(!udp_mgmt_req_valid) {
 
@@ -2265,6 +2952,12 @@
     		/* set return code */
 		chdlc_udp_pkt->cblock.return_code = 0xCD;
 
+		if (net_ratelimit()){	
+			printk(KERN_INFO 
+			"%s: Warning, Illegal UDP command attempted from network: %x\n",
+			card->devname,chdlc_udp_pkt->cblock.command);
+		}
+
    	} else {
 	   	unsigned long trace_status_cfg_addr = 0;
 		TRACE_STATUS_EL_CFG_STRUCT trace_cfg_struct;
@@ -2488,11 +3181,12 @@
 
 			((unsigned char *)chdlc_udp_pkt->data )[1] =
 				flags->FT1_info_struct.parallel_port_B_input;
-				 
+				
 			chdlc_udp_pkt->cblock.return_code = COMMAND_OK;
+			chdlc_udp_pkt->cblock.buffer_length = 2;
 			mb->buffer_length = 2;
 			break;
-		
+
 		case CPIPE_ROUTER_UP_TIME:
 			do_gettimeofday( &tv );
 			chdlc_priv_area->router_up_time = tv.tv_sec - 
@@ -2500,6 +3194,8 @@
 			*(unsigned long *)&chdlc_udp_pkt->data = 
 					chdlc_priv_area->router_up_time;	
 			mb->buffer_length = sizeof(unsigned long);
+			chdlc_udp_pkt->cblock.buffer_length = sizeof(unsigned long);
+			chdlc_udp_pkt->cblock.return_code = COMMAND_OK;
 			break;
 
    		case FT1_MONITOR_STATUS_CTRL:
@@ -2525,8 +3221,10 @@
 		  			break;
 	   	    	     	} 
 	      		} 	
-	
+			goto dflt_1;
+
 		default:
+dflt_1:
 			/* it's a board command */
 			mb->command = chdlc_udp_pkt->cblock.command;
 			mb->buffer_length = chdlc_udp_pkt->cblock.buffer_length;
@@ -2556,12 +3254,19 @@
 
      	len = reply_udp(chdlc_priv_area->udp_pkt_data, mb->buffer_length);
 	
-     	if(chdlc_priv_area->udp_pkt_src == UDP_PKT_FRM_NETWORK) {
-		if(!chdlc_send(card, chdlc_priv_area->udp_pkt_data, len)) {
-			++ card->wandev.stats.tx_packets;
-#ifdef LINUX_2_1
-			card->wandev.stats.tx_bytes += len;
+
+     	if(chdlc_priv_area->udp_pkt_src == UDP_PKT_FRM_NETWORK){
+
+		/* Must check if we interrupted if_send() routine. The
+		 * tx buffers might be used. If so drop the packet */
+	   	if (!test_bit(SEND_CRIT,&card->wandev.critical)) {
+		
+			if(!chdlc_send(card, chdlc_priv_area->udp_pkt_data, len)) {
+				++ card->wandev.stats.tx_packets;
+#if defined(LINUX_2_1) || defined(LINUX_2_4)
+				card->wandev.stats.tx_bytes += len;
 #endif
+			}
 		}
 	} else {	
 	
@@ -2595,7 +3300,7 @@
  * Initialize Receive and Transmit Buffers.
  */
 
-static void init_chdlc_tx_rx_buff( sdla_t* card, struct net_device *dev )
+static void init_chdlc_tx_rx_buff( sdla_t* card)
 {
 	CHDLC_MAILBOX_STRUCT* mb = card->mbox;
 	CHDLC_TX_STATUS_EL_CFG_STRUCT *tx_config;
@@ -2607,7 +3312,9 @@
 	err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
 
 	if(err != COMMAND_OK) {
-		chdlc_error(card,err,mb);
+		if (card->wandev.dev){
+			chdlc_error(card,err,mb);
+		}
 		return;
 	}
 
@@ -2678,19 +3385,13 @@
  * Perform Interrupt Test by running READ_CHDLC_CODE_VERSION command MAX_INTR
  * _TEST_COUNTER times.
  */
-static int intr_test( sdla_t* card, struct net_device *dev )
+static int intr_test( sdla_t* card)
 {
 	CHDLC_MAILBOX_STRUCT* mb = card->mbox;
 	int err,i;
 
 	Intr_test_counter = 0;
-
-	/* The critical flag is unset because during intialization (if_open) 
-	 * we want the interrupts to be enabled so that when the wpc_isr is
-	 * called it does not exit due to critical flag set.
-	 */ 
-
-	clear_bit(1, (void*)&card->wandev.critical);
+	
 	err = chdlc_set_intr_mode(card, APP_INT_ON_COMMAND_COMPLETE);
 
 	if (err == CMD_OK) { 
@@ -2707,8 +3408,6 @@
 	}
 
 	err = chdlc_set_intr_mode(card, 0);
-	set_bit(1, (void*)&card->wandev.critical);
-
 
 	if (err != CMD_OK)
 		return err;
@@ -2723,13 +3422,33 @@
 {
 	 chdlc_udp_pkt_t *chdlc_udp_pkt = (chdlc_udp_pkt_t *)skb->data;
 
+#ifdef _WAN_UDP_DEBUG
+		printk(KERN_INFO "SIG %s = %s\n\
+				  UPP %x = %x\n\
+				  PRT %x = %x\n\
+				  REQ %i = %i\n\
+				  36 th = %x 37th = %x\n",
+				  chdlc_udp_pkt->wp_mgmt.signature,
+				  UDPMGMT_SIGNATURE,
+				  chdlc_udp_pkt->udp_pkt.udp_dst_port,
+				  ntohs(card->wandev.udp_port),
+				  chdlc_udp_pkt->ip_pkt.protocol,
+				  UDPMGMT_UDP_PROTOCOL,
+				  chdlc_udp_pkt->wp_mgmt.request_reply,
+				  UDPMGMT_REQUEST,
+				  skb->data[36], skb->data[37]);
+#endif	
+		
 	if (!strncmp(chdlc_udp_pkt->wp_mgmt.signature,UDPMGMT_SIGNATURE,8) &&
 	   (chdlc_udp_pkt->udp_pkt.udp_dst_port == ntohs(card->wandev.udp_port)) &&
 	   (chdlc_udp_pkt->ip_pkt.protocol == UDPMGMT_UDP_PROTOCOL) &&
 	   (chdlc_udp_pkt->wp_mgmt.request_reply == UDPMGMT_REQUEST)) {
+
 		return UDP_CPIPE_TYPE;
+
+	}else{ 
+		return UDP_INVALID_TYPE;
 	}
-	else return UDP_INVALID_TYPE;
 }
 
 /*============================================================================
@@ -2744,7 +3463,7 @@
                 case WAN_CONNECTED:
                         printk (KERN_INFO "%s: Link connected!\n",
                                 card->devname);
-                      break;
+                      	break;
 
                 case WAN_CONNECTING:
                         printk (KERN_INFO "%s: Link connecting...\n",
@@ -2758,31 +3477,1268 @@
                 }
 
                 card->wandev.state = card->u.c.state = state;
+		if (card->wandev.dev){
+			netdevice_t *dev = card->wandev.dev;
+			chdlc_private_area_t *chdlc_priv_area = dev->priv;
+			chdlc_priv_area->common.state = state;
+		}
         }
 }
 
-void s508_lock (sdla_t *card, unsigned long *smp_flags)
+/*===========================================================================
+ * config_chdlc
+ *
+ *	Configure the chdlc protocol and enable communications.		
+ *
+ *   	The if_open() function binds this function to the poll routine.
+ *      Therefore, this function will run every time the chdlc interface
+ *      is brought up. We cannot run this function from the if_open 
+ *      because if_open does not have access to the remote IP address.
+ *      
+ *	If the communications are not enabled, proceed to configure
+ *      the card and enable communications.
+ *
+ *      If the communications are enabled, it means that the interface
+ *      was shutdown by ether the user or driver. In this case, we 
+ *      have to check that the IP addresses have not changed.  If
+ *      the IP addresses have changed, we have to reconfigure the firmware
+ *      and update the changed IP addresses.  Otherwise, just exit.
+ *
+ */
+
+static int config_chdlc (sdla_t *card)
 {
-#ifdef CONFIG_SMP
-                spin_lock_irqsave(&card->lock, *smp_flags);
-                if (card->next){
-                        spin_lock(&card->next->lock);
-                }
-#else
-                disable_irq(card->hw.irq);
-#endif                                                                     
+	netdevice_t *dev = card->wandev.dev;
+	chdlc_private_area_t *chdlc_priv_area = dev->priv;
+	SHARED_MEMORY_INFO_STRUCT *flags = card->u.c.flags;
+
+	if (card->u.c.comm_enabled){
+
+		/* Jun 20. 2000: NC
+		 * IP addresses are not used in the API mode */
+		
+		if ((chdlc_priv_area->ip_local_tmp != chdlc_priv_area->ip_local ||
+		     chdlc_priv_area->ip_remote_tmp != chdlc_priv_area->ip_remote) && 
+		     card->u.c.usedby == WANPIPE) {
+			
+			/* The IP addersses have changed, we must
+                         * stop the communications and reconfigure
+                         * the card. Reason: the firmware must know
+                         * the local and remote IP addresses. */
+			disable_comm(card);
+			port_set_state(card, WAN_DISCONNECTED);
+			printk(KERN_INFO 
+				"%s: IP addresses changed!\n",
+					card->devname);
+			printk(KERN_INFO 
+				"%s: Restarting communications ...\n",
+					card->devname);
+		}else{ 
+			/* IP addresses are the same and the link is up, 
+                         * we dont have to do anything here. Therefore, exit */
+			return 0;
+		}
+	}
+
+	chdlc_priv_area->ip_local = chdlc_priv_area->ip_local_tmp;
+	chdlc_priv_area->ip_remote = chdlc_priv_area->ip_remote_tmp;
+
+
+	/* Setup the Board for asynchronous mode */
+	if (card->u.c.async_mode){
+		
+		if (set_asy_config(card)) {
+			printk (KERN_INFO "%s: Failed CHDLC Async configuration!\n",
+				card->devname);
+			return 0;
+		}
+	}else{
+		/* Setup the Board for CHDLC */
+		if (set_chdlc_config(card)) {
+			printk (KERN_INFO "%s: Failed CHDLC configuration!\n",
+				card->devname);
+			return 0;
+		}
+	}
+
+	/* Set interrupt mode and mask */
+        if (chdlc_set_intr_mode(card, APP_INT_ON_RX_FRAME |
+                		APP_INT_ON_GLOBAL_EXCEP_COND |
+                		APP_INT_ON_TX_FRAME |
+                		APP_INT_ON_CHDLC_EXCEP_COND | APP_INT_ON_TIMER)){
+		printk (KERN_INFO "%s: Failed to set interrupt triggers!\n",
+				card->devname);
+		return 0;	
+        }
+	
+
+	/* Mask the Transmit and Timer interrupt */
+	flags->interrupt_info_struct.interrupt_permission &= 
+		~(APP_INT_ON_TX_FRAME | APP_INT_ON_TIMER);
+
+	/* In TTY mode, receive interrupt will be enabled during
+	 * wanpipe_tty_open() operation */
+	if (card->tty_opt){
+		flags->interrupt_info_struct.interrupt_permission &= ~APP_INT_ON_RX_FRAME;
+	}
+
+	/* Enable communications */
+ 	if (card->u.c.async_mode){
+		if (asy_comm_enable(card) != 0) {
+			printk(KERN_INFO "%s: Failed to enable async commnunication!\n",
+					card->devname);
+			flags->interrupt_info_struct.interrupt_permission = 0;
+			card->u.c.comm_enabled=0;
+			chdlc_set_intr_mode(card,0);
+			return 0;
+		}
+        }else{ 
+		if (chdlc_comm_enable(card) != 0) {
+			printk(KERN_INFO "%s: Failed to enable chdlc communications!\n",
+					card->devname);
+			flags->interrupt_info_struct.interrupt_permission = 0;
+			card->u.c.comm_enabled=0;
+			chdlc_set_intr_mode(card,0);
+			return 0;
+		}
+	}
+
+	/* Initialize Rx/Tx buffer control fields */
+	init_chdlc_tx_rx_buff(card);
+	port_set_state(card, WAN_CONNECTING);
+	return 0; 
 }
 
-void s508_unlock (sdla_t *card, unsigned long *smp_flags)
+
+/*============================================================
+ * chdlc_poll
+ *	
+ * Rationale:
+ * 	We cannot manipulate the routing tables, or
+ *      ip addresses withing the interrupt. Therefore
+ *      we must perform such actons outside an interrupt 
+ *      at a later time. 
+ *
+ * Description:	
+ *	CHDLC polling routine, responsible for 
+ *     	shutting down interfaces upon disconnect
+ *     	and adding/removing routes. 
+ *      
+ * Usage:        
+ * 	This function is executed for each CHDLC  
+ * 	interface through a tq_schedule bottom half.
+ *      
+ *      trigger_chdlc_poll() function is used to kick
+ *      the chldc_poll routine.  
+ */
+
+static void chdlc_poll (netdevice_t *dev)
 {
-#ifdef CONFIG_SMP
-                        if (card->next){
-                                spin_unlock(&card->next->lock);
-                        }
-                        spin_unlock_irqrestore(&card->lock, *smp_flags);
-#else
-                        enable_irq(card->hw.irq);
-#endif           
-}
+	chdlc_private_area_t *chdlc_priv_area;
+	sdla_t *card;
+	u8 check_gateway=0;	
+	SHARED_MEMORY_INFO_STRUCT* flags;
+
+	
+	if (!dev || (chdlc_priv_area=dev->priv) == NULL)
+		return;
+
+	card = chdlc_priv_area->card;
+	flags = card->u.c.flags;
+	
+	/* (Re)Configuraiton is in progress, stop what you are 
+	 * doing and get out */
+	if (test_bit(PERI_CRIT,&card->wandev.critical)){
+		clear_bit(POLL_CRIT,&card->wandev.critical);
+		return;
+	}
+	
+	/* if_open() function has triggered the polling routine
+	 * to determine the configured IP addresses.  Once the
+	 * addresses are found, trigger the chdlc configuration */
+	if (test_bit(0,&chdlc_priv_area->config_chdlc)){
+
+		chdlc_priv_area->ip_local_tmp  = get_ip_address(dev,WAN_LOCAL_IP);
+		chdlc_priv_area->ip_remote_tmp = get_ip_address(dev,WAN_POINTOPOINT_IP);
+	
+	       /* Jun 20. 2000 Bug Fix
+	 	* Only perform this check in WANPIPE mode, since
+	 	* IP addresses are not used in the API mode. */
+	
+		if (chdlc_priv_area->ip_local_tmp == chdlc_priv_area->ip_remote_tmp && 
+		    card->u.c.slarp_timer == 0x00 && 
+		    !card->u.c.backup && 
+		    card->u.c.usedby == WANPIPE){
+
+			if (++chdlc_priv_area->ip_error > MAX_IP_ERRORS){
+				printk(KERN_INFO "\n%s: --- WARNING ---\n",
+						card->devname);
+				printk(KERN_INFO 
+				"%s: The local IP address is the same as the\n",
+						card->devname);
+				printk(KERN_INFO 
+				"%s: Point-to-Point IP address.\n",
+						card->devname);
+				printk(KERN_INFO "%s: --- WARNING ---\n\n",
+						card->devname);
+			}else{
+				clear_bit(POLL_CRIT,&card->wandev.critical);
+				chdlc_priv_area->poll_delay_timer.expires = jiffies+HZ;
+				add_timer(&chdlc_priv_area->poll_delay_timer);
+				return;
+			}
+		}
+
+		clear_bit(0,&chdlc_priv_area->config_chdlc);
+		clear_bit(POLL_CRIT,&card->wandev.critical);
+		
+		chdlc_priv_area->timer_int_enabled |= TMR_INT_ENABLED_CONFIG;
+		flags->interrupt_info_struct.interrupt_permission |= APP_INT_ON_TIMER;
+		return;
+	}
+	/* Dynamic interface implementation, as well as dynamic
+	 * routing.  */
+	
+	switch (card->u.c.state){
+
+	case WAN_DISCONNECTED:
+
+		/* If the dynamic interface configuration is on, and interface 
+		 * is up, then bring down the netowrk interface */
+		
+		if (test_bit(DYN_OPT_ON,&chdlc_priv_area->interface_down) && 
+		    !test_bit(DEV_DOWN,  &chdlc_priv_area->interface_down) &&		
+		    card->wandev.dev->flags & IFF_UP){	
+
+			printk(KERN_INFO "%s: Interface %s down.\n",
+				card->devname,card->wandev.dev->name);
+			change_dev_flags(card->wandev.dev,(card->wandev.dev->flags&~IFF_UP));
+			set_bit(DEV_DOWN,&chdlc_priv_area->interface_down);
+			chdlc_priv_area->route_status = NO_ROUTE;
+
+		}else{
+			/* We need to check if the local IP address is
+               	  	 * zero. If it is, we shouldn't try to remove it.
+                 	 */
+
+			if (card->wandev.dev->flags & IFF_UP && 
+		    	    get_ip_address(card->wandev.dev,WAN_LOCAL_IP) && 
+		    	    chdlc_priv_area->route_status != NO_ROUTE &&
+			    card->u.c.slarp_timer){
+
+				process_route(card);
+			}
+		}
+		break;
+
+	case WAN_CONNECTED:
+
+		/* In SMP machine this code can execute before the interface
+		 * comes up.  In this case, we must make sure that we do not
+		 * try to bring up the interface before dev_open() is finished */
+
+
+		/* DEV_DOWN will be set only when we bring down the interface
+		 * for the very first time. This way we know that it was us
+		 * that brought the interface down */
+		
+		if (test_bit(DYN_OPT_ON,&chdlc_priv_area->interface_down) &&
+		    test_bit(DEV_DOWN,  &chdlc_priv_area->interface_down) &&
+		    !(card->wandev.dev->flags & IFF_UP)){
+			
+			printk(KERN_INFO "%s: Interface %s up.\n",
+				card->devname,card->wandev.dev->name);
+			change_dev_flags(card->wandev.dev,(card->wandev.dev->flags|IFF_UP));
+			clear_bit(DEV_DOWN,&chdlc_priv_area->interface_down);
+			check_gateway=1;
+		}
+
+		if (chdlc_priv_area->route_status == ADD_ROUTE && 
+		    card->u.c.slarp_timer){ 
+
+			process_route(card);
+			check_gateway=1;
+		}
+
+		if (chdlc_priv_area->gateway && check_gateway)
+			add_gateway(card,dev);
+
+		break;
+	}	
+
+	clear_bit(POLL_CRIT,&card->wandev.critical);
+}
+
+/*============================================================
+ * trigger_chdlc_poll
+ *
+ * Description:
+ * 	Add a chdlc_poll() task into a tq_scheduler bh handler
+ *      for a specific dlci/interface.  This will kick
+ *      the fr_poll() routine at a later time. 
+ *
+ * Usage:
+ * 	Interrupts use this to defer a taks to 
+ *      a polling routine.
+ *
+ */	
+static void trigger_chdlc_poll (netdevice_t *dev)
+{
+	chdlc_private_area_t *chdlc_priv_area;
+	sdla_t *card;
+
+	if (!dev)
+		return;
+	
+	if ((chdlc_priv_area = dev->priv)==NULL)
+		return;
+
+	card = chdlc_priv_area->card;
+	
+	if (test_and_set_bit(POLL_CRIT,&card->wandev.critical)){
+		return;
+	}
+	if (test_bit(PERI_CRIT,&card->wandev.critical)){
+		return; 
+	}
+#ifdef LINUX_2_4
+	schedule_task(&chdlc_priv_area->poll_task);
+#else
+	queue_task(&chdlc_priv_area->poll_task, &tq_scheduler);
+#endif
+	return;
+}
+
+
+static void chdlc_poll_delay (unsigned long dev_ptr)
+{
+	netdevice_t *dev = (netdevice_t *)dev_ptr;
+	trigger_chdlc_poll(dev);
+}
+
+
+void s508_lock (sdla_t *card, unsigned long *smp_flags)
+{
+#if defined(__SMP__) || defined(LINUX_2_4)
+	spin_lock_irqsave(&card->wandev.lock, *smp_flags);
+        if (card->next){
+        	spin_lock(&card->next->wandev.lock);
+	}
+#else
+        disable_irq(card->hw.irq);
+#endif                                                                     
+}
+
+void s508_unlock (sdla_t *card, unsigned long *smp_flags)
+{
+#if defined(__SMP__) || defined(LINUX_2_4)
+        if (card->next){
+        	spin_unlock(&card->next->wandev.lock);
+        }
+        spin_unlock_irqrestore(&card->wandev.lock, *smp_flags);
+#else
+        enable_irq(card->hw.irq);
+#endif           
+}
+
+//*********** TTY SECTION ****************
+#if defined(LINUX_2_4) || defined(LINUX_2_1)
+
+static void wanpipe_tty_trigger_tx_irq(sdla_t *card)
+{
+	SHARED_MEMORY_INFO_STRUCT *flags = card->u.c.flags;
+	INTERRUPT_INFORMATION_STRUCT *chdlc_int = &flags->interrupt_info_struct;
+	chdlc_int->interrupt_permission |= APP_INT_ON_TX_FRAME;
+}
+
+static void wanpipe_tty_trigger_poll(sdla_t *card)
+{
+#ifdef LINUX_2_4
+	schedule_task(&card->tty_task_queue);
+#else
+	queue_task(&card->tty_task_queue, &tq_scheduler);
+#endif
+}
+
+static void tty_poll_task (void* data)
+{
+	sdla_t *card = (sdla_t*)data;
+	struct tty_struct *tty;
+
+	if ((tty=card->tty)==NULL)
+		return;
+	
+	if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
+	    tty->ldisc.write_wakeup){
+		(tty->ldisc.write_wakeup)(tty);
+	}
+	wake_up_interruptible(&tty->write_wait);
+#if defined(SERIAL_HAVE_POLL_WAIT) || \
+         (defined LINUX_2_1 && LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,15))
+	wake_up_interruptible(&tty->poll_wait);
+#endif	
+	return;
+}
+
+static void wanpipe_tty_close(struct tty_struct *tty, struct file * filp)
+{
+	sdla_t *card;
+	unsigned long smp_flags;
+	
+	if (!tty || !tty->driver_data){
+		return;
+	}
+	
+	card = (sdla_t*)tty->driver_data;
+	
+	if (!card)
+		return;
+
+	printk(KERN_INFO "%s: Closing TTY Driver!\n",
+			card->devname);
+
+	/* Sanity Check */
+	if (!card->tty_open)
+		return;
+	
+	wanpipe_close(card);
+	if (--card->tty_open == 0){
+
+		lock_adapter_irq(&card->wandev.lock,&smp_flags);	
+		card->tty=NULL;
+		chdlc_disable_comm_shutdown(card);
+		unlock_adapter_irq(&card->wandev.lock,&smp_flags);
+
+		if (card->tty_buf){
+			kfree(card->tty_buf);
+			card->tty_buf=NULL;			
+		}
+
+		if (card->tty_rx){
+			kfree(card->tty_rx);
+			card->tty_rx=NULL;
+		}
+	}
+	return;
+}
+static int wanpipe_tty_open(struct tty_struct *tty, struct file * filp)
+{
+	unsigned long smp_flags;
+	sdla_t *card;
+	
+	if (!tty){
+		return -ENODEV;
+	}
+	
+	if (!tty->driver_data){
+		int port;
+		port = MINOR(tty->device) - tty->driver.minor_start;
+		if ((port < 0) || (port >= NR_PORTS)) 
+			return -ENODEV;
+		
+		tty->driver_data = WAN_CARD(port);
+		if (!tty->driver_data)
+			return -ENODEV;
+	}
+
+	card = (sdla_t*)tty->driver_data;
+
+	if (!card){
+		lock_adapter_irq(&card->wandev.lock,&smp_flags);	
+		card->tty=NULL;
+		unlock_adapter_irq(&card->wandev.lock,&smp_flags);
+		return -ENODEV;
+	}
+
+	printk(KERN_INFO "%s: Opening TTY Driver!\n",
+			card->devname);
+
+	if (card->tty_open == 0){
+		lock_adapter_irq(&card->wandev.lock,&smp_flags);	
+		card->tty=tty;
+		unlock_adapter_irq(&card->wandev.lock,&smp_flags);
+
+		if (!card->tty_buf){
+			card->tty_buf = kmalloc(TTY_CHDLC_MAX_MTU, GFP_KERNEL);
+			if (!card->tty_buf){
+				card->tty_buf=NULL;
+				card->tty=NULL;
+				return -ENOMEM;	
+			}
+		}
+
+		if (!card->tty_rx){
+			card->tty_rx = kmalloc(TTY_CHDLC_MAX_MTU, GFP_KERNEL);
+			if (!card->tty_rx){
+				/* Free the buffer above */
+				kfree(card->tty_buf);
+				card->tty_buf=NULL;
+				card->tty=NULL;
+				return -ENOMEM;	
+			}
+		}
+	}
+
+	++card->tty_open;
+	wanpipe_open(card);
+	return 0;
+}
+
+static int wanpipe_tty_write(struct tty_struct * tty, int from_user,
+		    const unsigned char *buf, int count)
+{
+	unsigned long smp_flags=0;
+	sdla_t *card=NULL;
+
+	if (!tty){
+		dbg_printk(KERN_INFO "NO TTY in Write\n");
+		return -ENODEV;
+	}
+
+	card = (sdla_t *)tty->driver_data;
+			
+	if (!card){
+		dbg_printk(KERN_INFO "No Card in TTY Write\n");
+		return -ENODEV;
+	}	
+
+	if (count > card->wandev.mtu){
+		dbg_printk(KERN_INFO "Frame too big in Write %i Max: %i\n",
+				count,card->wandev.mtu);
+		return -EINVAL;
+	}
+	
+	if (card->wandev.state != WAN_CONNECTED){
+		dbg_printk(KERN_INFO "Card not connected in TTY Write\n");
+		return -EINVAL;
+	}
+
+	/* Lock the 508 Card: SMP is supported */
+      	if(card->hw.type != SDLA_S514){
+		s508_lock(card,&smp_flags);
+	} 
+	
+	if (test_and_set_bit(SEND_CRIT,(void*)&card->wandev.critical)){
+		printk(KERN_INFO "%s: Critical in TTY Write\n",
+				card->devname);
+		
+		/* Lock the 508 Card: SMP is supported */
+		if(card->hw.type != SDLA_S514)
+			s508_unlock(card,&smp_flags);
+		
+		return -EINVAL; 
+	}
+	
+	if (from_user) {
+		
+		unsigned char *tmp_buf;
+		
+		if ((tmp_buf=card->tty_buf)==NULL){
+			dbg_printk(KERN_INFO "No TTY BUF in Write\n");
+			
+			clear_bit(SEND_CRIT,(void*)&card->wandev.critical);
+			
+			if(card->hw.type != SDLA_S514)
+				s508_unlock(card,&smp_flags);
+			
+			return -ENOMEM;
+		}
+		
+		if (copy_from_user(tmp_buf,buf,count)){
+			dbg_printk(KERN_INFO "%s: Failed to copy from user!\n",
+					card->devname);
+			
+			clear_bit(SEND_CRIT,(void*)&card->wandev.critical);
+			
+			if(card->hw.type != SDLA_S514)
+				s508_unlock(card,&smp_flags);
+			
+			return -EINVAL;
+		}
+
+		if (chdlc_send(card,(void*)tmp_buf,count)){
+			dbg_printk(KERN_INFO "%s: Failed to send, retry later: user!\n",
+					card->devname);
+			
+			clear_bit(SEND_CRIT,(void*)&card->wandev.critical);
+			
+			wanpipe_tty_trigger_tx_irq(card);
+			
+			if(card->hw.type != SDLA_S514)
+				s508_unlock(card,&smp_flags);
+			return 0;
+		}
+
+	}else{
+	 	if (chdlc_send(card,(void*)buf,count)){
+			dbg_printk(KERN_INFO "%s: Failed to send, retry later: kernel!\n",
+					card->devname);
+			clear_bit(SEND_CRIT,(void*)&card->wandev.critical);
+	
+			wanpipe_tty_trigger_tx_irq(card);
+			
+			if(card->hw.type != SDLA_S514)
+				s508_unlock(card,&smp_flags);
+			return 0;
+		}
+	}
+	dbg_printk(KERN_INFO "%s: Packet sent OK: %i\n",card->devname,count);
+	clear_bit(SEND_CRIT,(void*)&card->wandev.critical);
+	
+	if(card->hw.type != SDLA_S514)
+		s508_unlock(card,&smp_flags);
+
+	return count;
+}
+
+static void wanpipe_tty_receive(sdla_t *card, unsigned addr, unsigned int len)
+{
+	unsigned offset=0;
+	unsigned olen=len;
+	char fp=0;
+	struct tty_struct *tty;
+	int i;
+	
+	if (!card->tty_open){
+		dbg_printk(KERN_INFO "%s: TTY not open during receive\n",
+				card->devname);
+		return;
+	}
+	
+	if ((tty=card->tty) == NULL){
+		dbg_printk(KERN_INFO "%s: No TTY on receive\n",
+				card->devname);
+		return;
+	}
+	
+	if (!tty->driver_data){
+		dbg_printk(KERN_INFO "%s: No Driver Data, or Flip on receive\n",
+				card->devname);
+		return;
+	}
+	
+
+	if (card->u.c.async_mode){
+		if ((tty->flip.count+len) >= TTY_FLIPBUF_SIZE){
+			if (net_ratelimit()){
+				printk(KERN_INFO 
+					"%s: Received packet size too big: %i bytes, Max: %i!\n",
+					card->devname,len,TTY_FLIPBUF_SIZE);
+			}
+			return;
+		}
+
+		
+		if((addr + len) > card->u.c.rx_top + 1) {
+			offset = card->u.c.rx_top - addr + 1;
+			
+			sdla_peek(&card->hw, addr, tty->flip.char_buf_ptr, offset);
+			
+			addr = card->u.c.rx_base;
+			len -= offset;
+			
+			tty->flip.char_buf_ptr+=offset;
+			tty->flip.count+=offset;
+			for (i=0;i<offset;i++){
+				*tty->flip.flag_buf_ptr = 0;
+				tty->flip.flag_buf_ptr++;
+			}
+		}
+		
+		sdla_peek(&card->hw, addr, tty->flip.char_buf_ptr, len);
+			
+		tty->flip.char_buf_ptr+=len;
+		card->tty->flip.count+=len;
+		for (i=0;i<len;i++){
+			*tty->flip.flag_buf_ptr = 0;
+			tty->flip.flag_buf_ptr++;
+		}
+
+		tty->low_latency=1;
+		tty_flip_buffer_push(tty);
+	}else{
+		if (!card->tty_rx){	
+			if (net_ratelimit()){
+				printk(KERN_INFO 
+				"%s: Receive sync buffer not available!\n",
+				 card->devname);
+			}
+			return;
+		}
+	
+		if (len > TTY_CHDLC_MAX_MTU){
+			if (net_ratelimit()){
+				printk(KERN_INFO 
+				"%s: Received packet size too big: %i bytes, Max: %i!\n",
+					card->devname,len,TTY_FLIPBUF_SIZE);
+			}
+			return;
+		}
+
+		
+		if((addr + len) > card->u.c.rx_top + 1) {
+			offset = card->u.c.rx_top - addr + 1;
+			
+			sdla_peek(&card->hw, addr, card->tty_rx, offset);
+			
+			addr = card->u.c.rx_base;
+			len -= offset;
+		}
+		sdla_peek(&card->hw, addr, card->tty_rx+offset, len);
+		if (tty->ldisc.receive_buf){
+			tty->ldisc.receive_buf(tty,card->tty_rx,&fp,olen);
+		}else{
+			if (net_ratelimit()){
+				printk(KERN_INFO 
+					"%s: NO TTY Sync line discipline!\n",
+					card->devname);
+			}
+		}
+	}
+
+	dbg_printk(KERN_INFO "%s: Received Data %i\n",card->devname,olen);
+	return;
+}
+
+#if 0
+static int wanpipe_tty_ioctl(struct tty_struct *tty, struct file * file,
+		    unsigned int cmd, unsigned long arg)
+{
+	return -ENOIOCTLCMD;
+}
+#endif
+
+static void wanpipe_tty_stop(struct tty_struct *tty)
+{
+	return;
+}
+
+static void wanpipe_tty_start(struct tty_struct *tty)
+{
+	return;
+}
+
+static int config_tty (sdla_t *card)
+{
+	SHARED_MEMORY_INFO_STRUCT *flags = card->u.c.flags;
+
+	/* Setup the Board for asynchronous mode */
+	if (card->u.c.async_mode){
+		
+		if (set_asy_config(card)) {
+			printk (KERN_INFO "%s: Failed CHDLC Async configuration!\n",
+				card->devname);
+			return -EINVAL;
+		}
+	}else{
+		/* Setup the Board for CHDLC */
+		if (set_chdlc_config(card)) {
+			printk (KERN_INFO "%s: Failed CHDLC configuration!\n",
+				card->devname);
+			return -EINVAL;
+		}
+	}
+
+	/* Set interrupt mode and mask */
+        if (chdlc_set_intr_mode(card, APP_INT_ON_RX_FRAME |
+                		APP_INT_ON_GLOBAL_EXCEP_COND |
+                		APP_INT_ON_TX_FRAME |
+                		APP_INT_ON_CHDLC_EXCEP_COND | APP_INT_ON_TIMER)){
+		printk (KERN_INFO "%s: Failed to set interrupt triggers!\n",
+				card->devname);
+		return -EINVAL;	
+        }
+	
+
+	/* Mask the Transmit and Timer interrupt */
+	flags->interrupt_info_struct.interrupt_permission &= 
+		~(APP_INT_ON_TX_FRAME | APP_INT_ON_TIMER);
+
+	
+	/* Enable communications */
+ 	if (card->u.c.async_mode){
+		if (asy_comm_enable(card) != 0) {
+			printk(KERN_INFO "%s: Failed to enable async commnunication!\n",
+					card->devname);
+			flags->interrupt_info_struct.interrupt_permission = 0;
+			card->u.c.comm_enabled=0;
+			chdlc_set_intr_mode(card,0);
+			return -EINVAL;
+		}
+        }else{ 
+		if (chdlc_comm_enable(card) != 0) {
+			printk(KERN_INFO "%s: Failed to enable chdlc communications!\n",
+					card->devname);
+			flags->interrupt_info_struct.interrupt_permission = 0;
+			card->u.c.comm_enabled=0;
+			chdlc_set_intr_mode(card,0);
+			return -EINVAL;
+		}
+	}
+
+	/* Initialize Rx/Tx buffer control fields */
+	init_chdlc_tx_rx_buff(card);
+	port_set_state(card, WAN_CONNECTING);
+	return 0; 
+}
+
+
+static int change_speed(sdla_t *card, struct tty_struct *tty,
+			 struct termios *old_termios)
+{
+	int	baud, ret=0;
+	unsigned cflag; 
+	int	dbits,sbits,parity,handshaking;
+
+	cflag = tty->termios->c_cflag;
+
+	/* There is always one stop bit */
+	sbits=WANOPT_ONE;
+	
+	/* Parity is defaulted to NONE */
+	parity = WANOPT_NONE;
+
+	handshaking=0;
+	
+	/* byte size and parity */
+	switch (cflag & CSIZE) {
+	      case CS5: dbits = 5; break;
+	      case CS6: dbits = 6; break;
+	      case CS7: dbits = 7; break;
+	      case CS8: dbits = 8; break;
+	      /* Never happens, but GCC is too dumb to figure it out */
+	      default:  dbits = 8; break;
+	}
+	
+	/* One more stop bit should be supported, thus increment
+	 * the number of stop bits Max=2 */
+	if (cflag & CSTOPB) {
+		sbits = WANOPT_TWO;
+	}
+	if (cflag & PARENB) {
+		parity = WANOPT_EVEN;
+	}
+	if (cflag & PARODD){
+		parity = WANOPT_ODD;
+	}
+
+	/* Determine divisor based on baud rate */
+	baud = tty_get_baud_rate(tty);
+
+	if (!baud)
+		baud = 9600;	/* B0 transition handled in rs_set_termios */
+
+	if (cflag & CRTSCTS) {
+		handshaking|=ASY_RTS_HS_FOR_RX;
+	}
+	
+	if (I_IGNPAR(tty))
+		parity = WANOPT_NONE;
+
+	if (I_IXOFF(tty)){
+		handshaking|=ASY_XON_XOFF_HS_FOR_RX;
+		handshaking|=ASY_XON_XOFF_HS_FOR_TX;
+	}
+
+	if (I_IXON(tty)){
+		handshaking|=ASY_XON_XOFF_HS_FOR_RX;
+		handshaking|=ASY_XON_XOFF_HS_FOR_TX;
+	}
+
+	if (card->u.c.async_mode){
+		if (card->wandev.bps != baud)
+			ret=1;
+		card->wandev.bps = baud;
+	}
+
+	if (card->u.c.async_mode){
+		if (card->u.c.protocol_options != handshaking)
+			ret=1;
+		card->u.c.protocol_options = handshaking;
+
+		if (card->u.c.tx_bits_per_char != dbits)
+			ret=1;
+		card->u.c.tx_bits_per_char = dbits;
+
+		if (card->u.c.rx_bits_per_char != dbits)
+			ret=1;
+		card->u.c.rx_bits_per_char = dbits;
+		
+		if (card->u.c.stop_bits != sbits)
+			ret=1;
+		card->u.c.stop_bits = sbits;
+
+		if (card->u.c.parity != parity)
+			ret=1;
+		card->u.c.parity = parity;	
+
+		card->u.c.break_timer = 50;
+		card->u.c.inter_char_timer = 10;
+		card->u.c.rx_complete_length = 100;
+		card->u.c.xon_char = 0xFE;
+	}else{
+		card->u.c.protocol_options = HDLC_STREAMING_MODE;
+	}
+	
+	return ret;
+}
+
+	
+static void wanpipe_tty_set_termios(struct tty_struct *tty, struct termios *old_termios)
+{
+	sdla_t *card;
+	int err=1;
+
+	if (!tty){
+		return;
+	}
+
+	card = (sdla_t *)tty->driver_data;
+			
+	if (!card)
+		return;
+
+	if (change_speed(card, tty, old_termios) || !card->u.c.comm_enabled){
+		unsigned long smp_flags;
+		
+		if (card->u.c.comm_enabled){
+			lock_adapter_irq(&card->wandev.lock,&smp_flags);
+			chdlc_disable_comm_shutdown(card);
+			unlock_adapter_irq(&card->wandev.lock,&smp_flags);
+		}
+		lock_adapter_irq(&card->wandev.lock,&smp_flags);
+		err = config_tty(card);
+		unlock_adapter_irq(&card->wandev.lock,&smp_flags);
+		if (card->u.c.async_mode){
+			printk(KERN_INFO "%s: TTY Async Configuration:\n"
+				 "   Baud        =%i\n"
+				 "   Handshaking =%s\n"
+				 "   Tx Dbits    =%i\n"
+				 "   Rx Dbits    =%i\n"
+				 "   Parity      =%s\n"
+				 "   Stop Bits   =%i\n",
+				 card->devname,
+				 card->wandev.bps,
+				 opt_decode[card->u.c.protocol_options],
+				 card->u.c.tx_bits_per_char,
+				 card->u.c.rx_bits_per_char,
+				 p_decode[card->u.c.parity] ,
+				 card->u.c.stop_bits);
+		}else{
+			printk(KERN_INFO "%s: TTY Sync Configuration:\n"
+				 "   Baud        =%i\n"
+				 "   Protocol    =HDLC_STREAMING\n",
+				 card->devname,card->wandev.bps);
+		}
+		if (!err){
+			port_set_state(card,WAN_CONNECTED);
+		}else{
+			port_set_state(card,WAN_DISCONNECTED);
+		}
+	}
+	return;
+}
+
+static void wanpipe_tty_put_char(struct tty_struct *tty, unsigned char ch)
+{
+	sdla_t *card;
+	unsigned long smp_flags=0;
+
+	if (!tty){
+		return;
+	}
+	
+	card = (sdla_t *)tty->driver_data;
+			
+	if (!card)
+		return;
+
+	if (card->wandev.state != WAN_CONNECTED)
+		return;
+
+	if(card->hw.type != SDLA_S514)
+		s508_lock(card,&smp_flags);
+	
+	if (test_and_set_bit(SEND_CRIT,(void*)&card->wandev.critical)){
+		
+		wanpipe_tty_trigger_tx_irq(card);
+
+		if(card->hw.type != SDLA_S514)
+			s508_unlock(card,&smp_flags);
+		return;
+	}
+
+	if (chdlc_send(card,(void*)&ch,1)){
+		wanpipe_tty_trigger_tx_irq(card);
+		dbg_printk("%s: Failed to TX char!\n",card->devname);
+	}
+	
+	dbg_printk("%s: Char TX OK\n",card->devname);
+	
+	clear_bit(SEND_CRIT,(void*)&card->wandev.critical);
+	
+	if(card->hw.type != SDLA_S514)
+		s508_unlock(card,&smp_flags);
+	
+	return;
+}
+
+static void wanpipe_tty_flush_chars(struct tty_struct *tty)
+{
+	return;
+}
+
+static void wanpipe_tty_flush_buffer(struct tty_struct *tty)
+{
+	if (!tty)
+		return;
+	
+	wake_up_interruptible(&tty->write_wait);
+#if defined(SERIAL_HAVE_POLL_WAIT) || \
+         (defined LINUX_2_1 && LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,15))
+	wake_up_interruptible(&tty->poll_wait);
+#endif
+	if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
+	    tty->ldisc.write_wakeup)
+		(tty->ldisc.write_wakeup)(tty);
+
+	return;
+}
+
+/*
+ * This function is used to send a high-priority XON/XOFF character to
+ * the device
+ */
+static void wanpipe_tty_send_xchar(struct tty_struct *tty, char ch)
+{
+	return;
+}
+
+
+static int wanpipe_tty_chars_in_buffer(struct tty_struct *tty)
+{
+	return 0;
+}
+
+
+static int wanpipe_tty_write_room(struct tty_struct *tty)
+{
+	sdla_t *card;
+
+	printk(KERN_INFO "TTY Write Room\n");
+	
+	if (!tty){
+		return 0;
+	}
+
+	card = (sdla_t *)tty->driver_data;
+	if (!card)
+		return 0;
+
+	if (card->wandev.state != WAN_CONNECTED)
+		return 0;
+	
+	return SEC_MAX_NO_DATA_BYTES_IN_FRAME;
+}
+
+
+static int set_modem_status(sdla_t *card, unsigned char data)
+{
+	CHDLC_MAILBOX_STRUCT *mb = card->mbox;
+	int err;
+
+	mb->buffer_length=1;
+	mb->command=SET_MODEM_STATUS;
+	mb->data[0]=data;
+	err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
+	if (err != COMMAND_OK) 
+		chdlc_error (card, err, mb);
+	
+	return err;
+}
+
+static void wanpipe_tty_hangup(struct tty_struct *tty)
+{
+	sdla_t *card;
+	unsigned long smp_flags;
+
+	printk(KERN_INFO "TTY Hangup!\n");
+	
+	if (!tty){
+		return;
+	}
+
+	card = (sdla_t *)tty->driver_data;
+	if (!card)
+		return;
+
+	lock_adapter_irq(&card->wandev.lock,&smp_flags);
+	set_modem_status(card,0);
+	unlock_adapter_irq(&card->wandev.lock,&smp_flags);
+	return;
+}
+
+static void wanpipe_tty_break(struct tty_struct *tty, int break_state)
+{
+	return;
+}
+
+static void wanpipe_tty_wait_until_sent(struct tty_struct *tty, int timeout)
+{
+	return;
+}
+
+static void wanpipe_tty_throttle(struct tty_struct * tty)
+{
+	return;
+}
+
+static void wanpipe_tty_unthrottle(struct tty_struct * tty)
+{
+	return;
+}
+
+int wanpipe_tty_read_proc(char *page, char **start, off_t off, int count,
+		 int *eof, void *data)
+{
+	return 0;
+}
+
+/*
+ * The serial driver boot-time initialization code!
+ */
+int wanpipe_tty_init(sdla_t *card)
+{
+	struct serial_state * state;
+	
+	/* Initialize the tty_driver structure */
+
+	if (card->tty_minor < 0 || card->tty_minor > NR_PORTS){
+		printk(KERN_INFO "%s: Illegal Minor TTY number (0-4): %i\n",
+				card->devname,card->tty_minor);
+		return -EINVAL;
+	}
+
+	if (WAN_CARD(card->tty_minor)){
+		printk(KERN_INFO "%s: TTY Minor %i, already in use\n",
+				card->devname,card->tty_minor);
+		return -EBUSY;
+	}
+
+	if (tty_init_cnt==0){
+		
+		printk(KERN_INFO "%s: TTY %s Driver Init: Major %i, Minor Range %i-%i\n",
+				card->devname,
+				card->u.c.async_mode ? "ASYNC" : "SYNC",
+				WAN_TTY_MAJOR,MIN_PORT,MAX_PORT);
+		
+		tty_driver_mode = card->u.c.async_mode;
+		
+		memset(&serial_driver, 0, sizeof(struct tty_driver));
+		serial_driver.magic = TTY_DRIVER_MAGIC;
+		serial_driver.driver_name = "wanpipe_tty"; 
+		serial_driver.name = "ttyW";
+		serial_driver.major = WAN_TTY_MAJOR;
+		serial_driver.minor_start = WAN_TTY_MINOR;
+		serial_driver.num = NR_PORTS; 
+		serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
+		serial_driver.subtype = SERIAL_TYPE_NORMAL;
+		
+		serial_driver.init_termios = tty_std_termios;
+		serial_driver.init_termios.c_cflag =
+			B9600 | CS8 | CREAD | HUPCL | CLOCAL;
+		serial_driver.flags = TTY_DRIVER_REAL_RAW;
+		
+		serial_driver.refcount = &serial_refcount;
+		serial_driver.table = serial_table;
+		serial_driver.termios = serial_termios;
+		serial_driver.termios_locked = serial_termios_locked;
+
+		serial_driver.open = wanpipe_tty_open;
+		serial_driver.close = wanpipe_tty_close;
+		serial_driver.write = wanpipe_tty_write;
+		
+		serial_driver.put_char = wanpipe_tty_put_char;
+		serial_driver.flush_chars = wanpipe_tty_flush_chars;
+		serial_driver.write_room = wanpipe_tty_write_room;
+		serial_driver.chars_in_buffer = wanpipe_tty_chars_in_buffer;
+		serial_driver.flush_buffer = wanpipe_tty_flush_buffer;
+		//serial_driver.ioctl = wanpipe_tty_ioctl;
+		serial_driver.throttle = wanpipe_tty_throttle;
+		serial_driver.unthrottle = wanpipe_tty_unthrottle;
+		serial_driver.send_xchar = wanpipe_tty_send_xchar;
+		serial_driver.set_termios = wanpipe_tty_set_termios;
+		serial_driver.stop = wanpipe_tty_stop;
+		serial_driver.start = wanpipe_tty_start;
+		serial_driver.hangup = wanpipe_tty_hangup;
+		serial_driver.break_ctl = wanpipe_tty_break;
+		serial_driver.wait_until_sent = wanpipe_tty_wait_until_sent;
+		serial_driver.read_proc = wanpipe_tty_read_proc;
+		
+		/*
+		 * The callout device is just like normal device except for
+		 * major number and the subtype code.
+		 */
+		callout_driver = serial_driver;
+		callout_driver.name = "cuw";
+		callout_driver.major = TTYAUX_MAJOR;
+		callout_driver.subtype = SERIAL_TYPE_CALLOUT;
+		callout_driver.read_proc = 0;
+		callout_driver.proc_entry = 0;
+
+		if (tty_register_driver(&serial_driver)){
+			printk(KERN_INFO "%s: Failed to register serial driver!\n",
+					card->devname);
+		}
+
+		if (tty_register_driver(&callout_driver)){
+			printk(KERN_INFO "%s: Failed to register callout driver!\n",
+					card->devname);
+		}
+
+	}
+
+
+	/* The subsequent ports must comply to the initial configuration */
+	if (tty_driver_mode != card->u.c.async_mode){
+		printk(KERN_INFO "%s: Error: TTY Driver operation mode mismatch!\n",
+				card->devname);
+		printk(KERN_INFO "%s: The TTY driver is configured for %s!\n",
+				card->devname, tty_driver_mode ? "ASYNC" : "SYNC");
+		return -EINVAL;
+	}
+	
+	tty_init_cnt++;
+	
+	printk(KERN_INFO "%s: Initializing TTY %s Driver Minor %i\n",
+			card->devname,
+			tty_driver_mode ? "ASYNC" : "SYNC",
+			card->tty_minor);
+	
+	tty_card_map[card->tty_minor] = card;
+	state = &rs_table[card->tty_minor];
+	
+	state->magic = SSTATE_MAGIC;
+	state->line = 0;
+	state->type = PORT_UNKNOWN;
+	state->custom_divisor = 0;
+	state->close_delay = 5*HZ/10;
+	state->closing_wait = 30*HZ;
+	state->callout_termios = callout_driver.init_termios;
+	state->normal_termios = serial_driver.init_termios;
+	state->icount.cts = state->icount.dsr = 
+		state->icount.rng = state->icount.dcd = 0;
+	state->icount.rx = state->icount.tx = 0;
+	state->icount.frame = state->icount.parity = 0;
+	state->icount.overrun = state->icount.brk = 0;
+	state->irq = card->wandev.irq; 
+
+	card->tty_task_queue.routine = tty_poll_task;
+	card->tty_task_queue.data = (void*)card;
+	return 0;
+}
+
+#endif
+
 
 /****** End ****************************************************************/

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