File: /usr/src/linux/drivers/net/wan/sdla_ppp.c

1     /*****************************************************************************
2     * sdla_ppp.c	WANPIPE(tm) Multiprotocol WAN Link Driver. PPP module.
3     *
4     * Author: 	Nenad Corbic <ncorbic@sangoma.com>
5     *
6     * Copyright:	(c) 1995-2001 Sangoma Technologies Inc.
7     *
8     *		This program is free software; you can redistribute it and/or
9     *		modify it under the terms of the GNU General Public License
10     *		as published by the Free Software Foundation; either version
11     *		2 of the License, or (at your option) any later version.
12     * ============================================================================
13     * Feb 28, 2001  Nenad Corbic	o Updated if_tx_timeout() routine for 
14     * 				  2.4.X kernels.
15     * Nov 29, 2000  Nenad Corbic	o Added the 2.4.x kernel support:
16     * 				  get_ip_address() function has moved
17     * 				  into the ppp_poll() routine. It cannot
18     * 				  be called from an interrupt.
19     * Nov 07, 2000  Nenad Corbic	o Added security features for UDP debugging:
20     *                                 Deny all and specify allowed requests.
21     * May 02, 2000  Nenad Corbic	o Added the dynamic interface shutdown
22     *                                 option. When the link goes down, the
23     *                                 network interface IFF_UP flag is reset.
24     * Mar 06, 2000  Nenad Corbic	o Bug Fix: corrupted mbox recovery.
25     * Feb 25, 2000  Nenad Corbic    o Fixed the FT1 UDP debugger problem.
26     * Feb 09, 2000  Nenad Coribc    o Shutdown bug fix. update() was called
27     *                                 with NULL dev pointer: no check.
28     * Jan 24, 2000  Nenad Corbic    o Disabled use of CMD complete inter.
29     * Dev 15, 1999  Nenad Corbic    o Fixed up header files for 2.0.X kernels
30     * Oct 25, 1999  Nenad Corbic    o Support for 2.0.X kernels
31     *                                 Moved dynamic route processing into 
32     *                                 a polling routine.
33     * Oct 07, 1999  Nenad Corbic    o Support for S514 PCI card.  
34     *               Gideon Hack     o UPD and Updates executed using timer interrupt
35     * Sep 10, 1999  Nenad Corbic    o Fixed up the /proc statistics
36     * Jul 20, 1999  Nenad Corbic    o Remove the polling routines and use 
37     *                                 interrupts instead.
38     * Sep 17, 1998	Jaspreet Singh	o Updates for 2.2.X Kernels.
39     * Aug 13, 1998	Jaspreet Singh	o Improved Line Tracing.
40     * Jun 22, 1998	David Fong	o Added remote IP address assignment
41     * Mar 15, 1998	Alan Cox	o 2.1.8x basic port.
42     * Apr 16, 1998	Jaspreet Singh	o using htons() for the IPX protocol.
43     * Dec 09, 1997	Jaspreet Singh	o Added PAP and CHAP.
44     *				o Implemented new routines like 
45     *				  ppp_set_inbnd_auth(), ppp_set_outbnd_auth(),
46     *				  tokenize() and strstrip().
47     * Nov 27, 1997	Jaspreet Singh	o Added protection against enabling of irqs 
48     *				  while they have been disabled.
49     * Nov 24, 1997  Jaspreet Singh  o Fixed another RACE condition caused by
50     *                                 disabling and enabling of irqs.
51     *                               o Added new counters for stats on disable/enable
52     *                                 IRQs.
53     * Nov 10, 1997	Jaspreet Singh	o Initialized 'skb->mac.raw' to 'skb->data'
54     *				  before every netif_rx().
55     *				o Free up the device structure in del_if().
56     * Nov 07, 1997	Jaspreet Singh	o Changed the delay to zero for Line tracing
57     *				  command.
58     * Oct 20, 1997 	Jaspreet Singh	o Added hooks in for Router UP time.
59     * Oct 16, 1997	Jaspreet Singh  o The critical flag is used to maintain flow
60     *				  control by avoiding RACE conditions.  The 
61     *				  cli() and restore_flags() are taken out.
62     *				  A new structure, "ppp_private_area", is added 
63     *				  to provide Driver Statistics.   
64     * Jul 21, 1997 	Jaspreet Singh	o Protected calls to sdla_peek() by adding 
65     *				  save_flags(), cli() and restore_flags().
66     * Jul 07, 1997	Jaspreet Singh  o Added configurable TTL for UDP packets
67     *				o Added ability to discard mulitcast and
68     *				  broacast source addressed packets.
69     * Jun 27, 1997 	Jaspreet Singh	o Added FT1 monitor capabilities
70     *				  New case (0x25) statement in if_send routine.
71     *				  Added a global variable rCount to keep track
72     *				  of FT1 status enabled on the board.
73     * May 22, 1997	Jaspreet Singh	o Added change in the PPP_SET_CONFIG command for
74     *				508 card to reflect changes in the new 
75     *				ppp508.sfm for supporting:continous transmission
76     *				of Configure-Request packets without receiving a
77     *				reply 				
78     *				OR-ed 0x300 to conf_flags 
79     *			        o Changed connect_tmout from 900 to 0
80     * May 21, 1997	Jaspreet Singh  o Fixed UDP Management for multiple boards
81     * Apr 25, 1997  Farhan Thawar    o added UDP Management stuff
82     * Mar 11, 1997  Farhan Thawar   Version 3.1.1
83     *                                o fixed (+1) bug in rx_intr()
84     *                                o changed if_send() to return 0 if
85     *                                  wandev.critical() is true
86     *                                o free socket buffer in if_send() if
87     *                                  returning 0 
88     * Jan 15, 1997	Gene Kozin	Version 3.1.0
89     *				 o implemented exec() entry point
90     * Jan 06, 1997	Gene Kozin	Initial version.
91     *****************************************************************************/
92     
93     #include <linux/module.h>
94     #include <linux/version.h>
95     #include <linux/kernel.h>	/* printk(), and other useful stuff */
96     #include <linux/stddef.h>	/* offsetof(), etc. */
97     #include <linux/errno.h>	/* return codes */
98     #include <linux/string.h>	/* inline memset(), etc. */
99     #include <linux/slab.h>	/* kmalloc(), kfree() */
100     #include <linux/wanrouter.h>	/* WAN router definitions */
101     #include <linux/wanpipe.h>	/* WANPIPE common user API definitions */
102     #include <linux/if_arp.h>	/* ARPHRD_* defines */
103     #include <asm/byteorder.h>	/* htons(), etc. */
104     #include <linux/in.h>		/* sockaddr_in */
105     #include <linux/inet.h>		/* in_aton(), in_ntoa() prototypes */
106     
107     
108     /* ---- 2.4.X KERNEL SUPPORT -----------------------*/
109     #if defined(LINUX_2_1) || defined(LINUX_2_4)
110      #include <asm/uaccess.h>
111      #include <linux/inetdevice.h>
112      #include <linux/netdevice.h>
113     #else
114      #include <asm/segment.h>
115      #include <net/route.h>          /* Adding new route entries : 2.0.X kernels */
116     #endif
117     
118     #include <linux/if.h>
119     #include <linux/sdla_ppp.h>		/* PPP firmware API definitions */
120     #include <linux/sdlasfm.h>		/* S514 Type Definition */
121     /****** Defines & Macros ****************************************************/
122     
123     #define	PPP_DFLT_MTU	1500		/* default MTU */
124     #define	PPP_MAX_MTU	4000		/* maximum MTU */
125     #define PPP_HDR_LEN	1
126     
127     #define MAX_IP_ERRORS 100 
128     
129     #define	CONNECT_TIMEOUT	(90*HZ)		/* link connection timeout */
130     #define	HOLD_DOWN_TIME	(5*HZ)		/* link hold down time : Changed from 30 to 5 */
131     
132     /* For handle_IPXWAN() */
133     #define CVHexToAscii(b) (((unsigned char)(b) > (unsigned char)9) ? ((unsigned char)'A' + ((unsigned char)(b) - (unsigned char)10)) : ((unsigned char)'0' + (unsigned char)(b)))
134     
135     /* Macro for enabling/disabling debugging comments */
136     //#define NEX_DEBUG
137     #ifdef NEX_DEBUG
138     #define NEX_PRINTK(format, a...) printk(format, ## a)
139     #else
140     #define NEX_PRINTK(format, a...)
141     #endif /* NEX_DEBUG */ 
142     
143     #define DCD(a)   ( a & 0x08 ? "HIGH" : "LOW" )
144     #define CTS(a)   ( a & 0x20 ? "HIGH" : "LOW" )
145     #define LCP(a)   ( a == 0x09 ? "OPEN" : "CLOSED" )
146     #define IP(a)    ( a == 0x09 ? "ENABLED" : "DISABLED" )
147     
148     #define TMR_INT_ENABLED_UPDATE  	0x01
149     #define TMR_INT_ENABLED_PPP_EVENT	0x02
150     #define TMR_INT_ENABLED_UDP		0x04
151     #define TMR_INT_ENABLED_CONFIG		0x20
152     
153     /* Set Configuraton Command Definitions */
154     #define PERCENT_TX_BUFF			60
155     #define TIME_BETWEEN_CONF_REQ  		30
156     #define TIME_BETWEEN_PAP_CHAP_REQ	30
157     #define WAIT_PAP_CHAP_WITHOUT_REPLY     300
158     #define WAIT_AFTER_DCD_CTS_LOW          5
159     #define TIME_DCD_CTS_LOW_AFTER_LNK_DOWN 10
160     #define WAIT_DCD_HIGH_AFTER_ENABLE_COMM 900
161     #define MAX_CONF_REQ_WITHOUT_REPLY      10
162     #define MAX_TERM_REQ_WITHOUT_REPLY      2
163     #define NUM_CONF_NAK_WITHOUT_REPLY      5
164     #define NUM_AUTH_REQ_WITHOUT_REPLY      10
165     
166     #define END_OFFSET 0x1F0
167     #if LINUX_VERSION_CODE < 0x020125
168     #define test_and_set_bit set_bit
169     #define net_ratelimit() 1
170     #endif
171     
172     
173     /******Data Structures*****************************************************/
174     
175     /* This structure is placed in the private data area of the device structure.
176      * The card structure used to occupy the private area but now the following 
177      * structure will incorporate the card structure along with PPP specific data
178      */
179       
180     typedef struct ppp_private_area
181     {
182     	netdevice_t *slave;
183     	sdla_t* card;	
184     	unsigned long router_start_time;	/*router start time in sec */
185     	unsigned long tick_counter;		/*used for 5 second counter*/
186     	unsigned mc;				/*multicast support on or off*/
187     	unsigned char enable_IPX;
188     	unsigned long network_number;
189     	unsigned char pap;
190     	unsigned char chap;
191     	unsigned char sysname[31];		/* system name for in-bnd auth*/
192     	unsigned char userid[511];		/* list of user ids */
193     	unsigned char passwd[511];		/* list of passwords */
194     	unsigned protocol;			/* SKB Protocol */
195     	u32 ip_local;				/* Local IP Address */
196     	u32 ip_remote;				/* remote IP Address */
197     
198     	u32 ip_local_tmp;
199     	u32 ip_remote_tmp;
200     	
201     	unsigned char timer_int_enabled;	/* Who enabled the timer inter*/
202     	unsigned char update_comms_stats;	/* Used by update function */
203     	unsigned long curr_trace_addr;		/* Trace information */
204     	unsigned long start_trace_addr;
205     	unsigned long end_trace_addr;
206     
207     	unsigned char interface_down;		/* Brind down interface when channel 
208                                                        goes down */
209     	unsigned long config_wait_timeout;	/* After if_open() if in dynamic if mode,
210     						   wait a few seconds before configuring */
211     	
212     	unsigned short udp_pkt_lgth;
213     	char  udp_pkt_src;
214           	char  udp_pkt_data[MAX_LGTH_UDP_MGNT_PKT];
215     
216     	/* PPP specific statistics */
217     
218     	if_send_stat_t if_send_stat;
219     	rx_intr_stat_t rx_intr_stat;
220     	pipe_mgmt_stat_t pipe_mgmt_stat;
221     
222     	unsigned long router_up_time; 
223     
224     	/* Polling task queue. Each interface
225              * has its own task queue, which is used
226              * to defer events from the interrupt */
227     	struct tq_struct poll_task;
228     	struct timer_list poll_delay_timer;
229     
230     	u8 gateway;
231     	u8 config_ppp;
232     	u8 ip_error;
233     	
234     }ppp_private_area_t;
235     
236     /* variable for keeping track of enabling/disabling FT1 monitor status */
237     static int rCount = 0;
238     
239     extern void disable_irq(unsigned int);
240     extern void enable_irq(unsigned int);
241     
242     /****** Function Prototypes *************************************************/
243     
244     /* WAN link driver entry points. These are called by the WAN router module. */
245     static int update(wan_device_t *wandev);
246     static int new_if(wan_device_t *wandev, netdevice_t *dev, wanif_conf_t *conf);
247     static int del_if(wan_device_t *wandev, netdevice_t *dev);
248     
249     /* WANPIPE-specific entry points */
250     static int wpp_exec (struct sdla *card, void *u_cmd, void *u_data);
251     
252     /* Network device interface */
253     static int if_init(netdevice_t *dev);
254     static int if_open(netdevice_t *dev);
255     static int if_close(netdevice_t *dev);
256     static int if_header(struct sk_buff *skb, netdevice_t *dev, unsigned short type, 
257     		     void *daddr, void *saddr, unsigned len);
258     
259     #ifdef LINUX_2_4
260     static void if_tx_timeout (netdevice_t *dev);
261     #endif
262     
263     #if defined(LINUX_2_1) || defined(LINUX_2_4)
264     static int if_rebuild_hdr(struct sk_buff *skb);
265     static struct net_device_stats *if_stats(netdevice_t *dev);
266     #else
267     static struct enet_statistics *if_stats(netdevice_t *dev);
268     static int if_rebuild_hdr (void* hdr, netdevice_t* dev, unsigned long raddr,
269             struct sk_buff* skb);
270     #endif
271     static int if_send(struct sk_buff *skb, netdevice_t *dev);
272     
273     
274     /* PPP firmware interface functions */
275     static int ppp_read_version(sdla_t *card, char *str);
276     static int ppp_set_outbnd_auth(sdla_t *card, ppp_private_area_t *ppp_priv_area);
277     static int ppp_set_inbnd_auth(sdla_t *card, ppp_private_area_t *ppp_priv_area);
278     static int ppp_configure(sdla_t *card, void *data);
279     static int ppp_set_intr_mode(sdla_t *card, unsigned char mode);
280     static int ppp_comm_enable(sdla_t *card);
281     static int ppp_comm_disable(sdla_t *card);
282     static int ppp_comm_disable_shutdown(sdla_t *card);
283     static int ppp_get_err_stats(sdla_t *card);
284     static int ppp_send(sdla_t *card, void *data, unsigned len, unsigned proto);
285     static int ppp_error(sdla_t *card, int err, ppp_mbox_t *mb);
286     
287     static void wpp_isr(sdla_t *card);
288     static void rx_intr(sdla_t *card);
289     static void event_intr(sdla_t *card);
290     static void timer_intr(sdla_t *card);
291     
292     /* Background polling routines */
293     static void process_route(sdla_t *card);
294     static void retrigger_comm(sdla_t *card);
295     
296     /* Miscellaneous functions */
297     static int read_info( sdla_t *card );
298     static int read_connection_info (sdla_t *card);
299     static void remove_route( sdla_t *card );
300     static int config508(netdevice_t *dev, sdla_t *card);
301     static void show_disc_cause(sdla_t * card, unsigned cause);
302     static int reply_udp( unsigned char *data, unsigned int mbox_len );
303     static void process_udp_mgmt_pkt(sdla_t *card, netdevice_t *dev, 
304     				ppp_private_area_t *ppp_priv_area);
305     static void init_ppp_tx_rx_buff( sdla_t *card );
306     static int intr_test( sdla_t *card );
307     static int udp_pkt_type( struct sk_buff *skb , sdla_t *card);
308     static void init_ppp_priv_struct( ppp_private_area_t *ppp_priv_area);
309     static void init_global_statistics( sdla_t *card );
310     static int tokenize(char *str, char **tokens);
311     static char* strstrip(char *str, char *s);
312     static int chk_bcast_mcast_addr(sdla_t* card, netdevice_t* dev,
313     				struct sk_buff *skb);
314     
315     static int config_ppp (sdla_t *);
316     static void ppp_poll(netdevice_t *);
317     static void trigger_ppp_poll(netdevice_t *);
318     static void ppp_poll_delay (unsigned long dev_ptr);
319     
320     
321     static int Read_connection_info;
322     static int Intr_test_counter;
323     static unsigned short available_buffer_space;
324     
325     
326     /* IPX functions */
327     static void switch_net_numbers(unsigned char *sendpacket, unsigned long network_number, 
328     			       unsigned char incoming);
329     static int handle_IPXWAN(unsigned char *sendpacket, char *devname, unsigned char enable_PX, 
330     			 unsigned long network_number, unsigned short proto);
331     
332     /* Lock Functions */
333     static void s508_lock (sdla_t *card, unsigned long *smp_flags);
334     static void s508_unlock (sdla_t *card, unsigned long *smp_flags);
335     
336     static int store_udp_mgmt_pkt(char udp_pkt_src, sdla_t* card,
337                                     struct sk_buff *skb, netdevice_t* dev,
338                                     ppp_private_area_t* ppp_priv_area );
339     static unsigned short calc_checksum (char *data, int len);
340     static void disable_comm (sdla_t *card);
341     static int detect_and_fix_tx_bug (sdla_t *card);
342     
343     /****** Public Functions ****************************************************/
344     
345     /*============================================================================
346      * PPP protocol initialization routine.
347      *
348      * This routine is called by the main WANPIPE module during setup.  At this
349      * point adapter is completely initialized and firmware is running.
350      *  o read firmware version (to make sure it's alive)
351      *  o configure adapter
352      *  o initialize protocol-specific fields of the adapter data space.
353      *
354      * Return:	0	o.k.
355      *		< 0	failure.
356      */
357     int wpp_init(sdla_t *card, wandev_conf_t *conf)
358     {
359     	ppp_flags_t *flags;
360     	union
361     	{
362     		char str[80];
363     	} u;
364     
365     	/* Verify configuration ID */
366     	if (conf->config_id != WANCONFIG_PPP) {
367     		
368     		printk(KERN_INFO "%s: invalid configuration ID %u!\n",
369     			card->devname, conf->config_id);
370     		return -EINVAL;
371     
372     	}
373     
374     	/* Initialize miscellaneous pointers to structures on the adapter */
375     	switch (card->hw.type) {
376     
377     		case SDLA_S508:
378     			card->mbox =(void*)(card->hw.dpmbase + PPP508_MB_OFFS);
379     			card->flags=(void*)(card->hw.dpmbase + PPP508_FLG_OFFS);
380     			break;
381     		
382     		case SDLA_S514:
383     			card->mbox =(void*)(card->hw.dpmbase + PPP514_MB_OFFS);
384     			card->flags=(void*)(card->hw.dpmbase + PPP514_FLG_OFFS);
385     			break;
386     
387     		default:
388     			return -EINVAL;
389     
390     	}
391     	flags = card->flags;
392     
393     	/* Read firmware version.  Note that when adapter initializes, it
394     	 * clears the mailbox, so it may appear that the first command was
395     	 * executed successfully when in fact it was merely erased. To work
396     	 * around this, we execute the first command twice.
397     	 */
398     	if (ppp_read_version(card, NULL) || ppp_read_version(card, u.str))
399     		return -EIO;
400     	
401     	printk(KERN_INFO "%s: running PPP firmware v%s\n",card->devname, u.str); 
402     	/* Adjust configuration and set defaults */
403     	card->wandev.mtu = (conf->mtu) ?
404     		min_t(unsigned int, conf->mtu, PPP_MAX_MTU) : PPP_DFLT_MTU;
405     
406     	card->wandev.bps	= conf->bps;
407     	card->wandev.interface	= conf->interface;
408     	card->wandev.clocking	= conf->clocking;
409     	card->wandev.station	= conf->station;
410     	card->isr		= &wpp_isr;
411     	card->poll		= NULL; 
412     	card->exec		= &wpp_exec;
413     	card->wandev.update	= &update;
414     	card->wandev.new_if	= &new_if;
415     	card->wandev.del_if	= &del_if;
416             card->wandev.udp_port   = conf->udp_port;
417     	card->wandev.ttl	= conf->ttl;
418     	card->wandev.state      = WAN_DISCONNECTED;
419     	card->disable_comm	= &disable_comm;
420     	card->irq_dis_if_send_count = 0;
421             card->irq_dis_poll_count = 0;
422     	card->u.p.authenticator = conf->u.ppp.authenticator;
423     	card->u.p.ip_mode 	= conf->u.ppp.ip_mode ?
424     				 conf->u.ppp.ip_mode : WANOPT_PPP_STATIC;
425             card->TracingEnabled    = 0;
426     	Read_connection_info    = 1;
427     
428     	/* initialize global statistics */
429     	init_global_statistics( card );
430     
431     
432     
433     	if (!card->configured){
434     		int err;
435     
436     		Intr_test_counter = 0;
437     		err = intr_test(card);
438     
439     		if(err || (Intr_test_counter < MAX_INTR_TEST_COUNTER)) {
440     			printk("%s: Interrupt Test Failed, Counter: %i\n", 
441     				card->devname, Intr_test_counter);
442     			printk( "%s: Please choose another interrupt\n",card->devname);
443     			return -EIO;
444     		}
445     		
446     		printk(KERN_INFO "%s: Interrupt Test Passed, Counter: %i\n", 
447     			card->devname, Intr_test_counter);
448     		card->configured = 1;
449     	}
450     
451     	ppp_set_intr_mode(card, PPP_INTR_TIMER); 
452     
453     	/* Turn off the transmit and timer interrupt */
454     	flags->imask &= ~PPP_INTR_TIMER;
455     
456     	printk(KERN_INFO "\n");
457     
458     	return 0;
459     }
460     
461     /******* WAN Device Driver Entry Points *************************************/
462     
463     /*============================================================================
464      * Update device status & statistics.
465      */
466     static int update(wan_device_t *wandev)
467     {
468     	sdla_t* card = wandev->private;
469      	netdevice_t* dev;
470             volatile ppp_private_area_t *ppp_priv_area;
471     	ppp_flags_t *flags = card->flags;
472     	unsigned long timeout;
473     
474     	/* sanity checks */
475     	if ((wandev == NULL) || (wandev->private == NULL))
476     		return -EFAULT;
477     	
478     	if (wandev->state == WAN_UNCONFIGURED)
479     		return -ENODEV;
480     	
481     	/* Shutdown bug fix. This function can be
482              * called with NULL dev pointer during
483              * shutdown 
484     	 */
485     	if ((dev=card->wandev.dev) == NULL){
486     		return -ENODEV;
487     	}
488     
489     	if ((ppp_priv_area=dev->priv) == NULL){
490     		return -ENODEV;
491     	}
492     	
493     	ppp_priv_area->update_comms_stats = 2;
494     	ppp_priv_area->timer_int_enabled |= TMR_INT_ENABLED_UPDATE;
495     	flags->imask |= PPP_INTR_TIMER;	
496     	
497     	/* wait a maximum of 1 second for the statistics to be updated */ 
498             timeout = jiffies;
499             for(;;) {
500     		if(ppp_priv_area->update_comms_stats == 0){
501     			break;
502     		}
503                     if ((jiffies - timeout) > (1 * HZ)){
504         			ppp_priv_area->update_comms_stats = 0;
505      			ppp_priv_area->timer_int_enabled &=
506     				~TMR_INT_ENABLED_UPDATE; 
507      			return -EAGAIN;
508     		}
509             }
510     
511     	return 0;
512     }
513     
514     /*============================================================================
515      * Create new logical channel.
516      * This routine is called by the router when ROUTER_IFNEW IOCTL is being
517      * handled.
518      * o parse media- and hardware-specific configuration
519      * o make sure that a new channel can be created
520      * o allocate resources, if necessary
521      * o prepare network device structure for registaration.
522      *
523      * Return:	0	o.k.
524      *		< 0	failure (channel will not be created)
525      */
526     static int new_if(wan_device_t *wandev, netdevice_t *dev, wanif_conf_t *conf)
527     {
528     	sdla_t *card = wandev->private;
529     	ppp_private_area_t *ppp_priv_area;
530     
531     	if (wandev->ndev)
532     		return -EEXIST;
533     	
534     
535     	printk(KERN_INFO "%s: Configuring Interface: %s\n",
536     			card->devname, conf->name);
537     
538     	if ((conf->name[0] == '\0') || (strlen(conf->name) > WAN_IFNAME_SZ)) {
539     
540     		printk(KERN_INFO "%s: Invalid interface name!\n",
541     			card->devname);
542     		return -EINVAL;
543     
544     	}
545     
546     	/* allocate and initialize private data */
547     	ppp_priv_area = kmalloc(sizeof(ppp_private_area_t), GFP_KERNEL);
548     	
549     	if( ppp_priv_area == NULL )
550     		return	-ENOMEM;
551     	
552     	memset(ppp_priv_area, 0, sizeof(ppp_private_area_t));
553     	
554     	ppp_priv_area->card = card; 
555     	
556     	/* initialize data */
557     	strcpy(card->u.p.if_name, conf->name);
558     
559     	/* initialize data in ppp_private_area structure */
560     	
561     	init_ppp_priv_struct( ppp_priv_area );
562     
563     	ppp_priv_area->mc = conf->mc;
564     	ppp_priv_area->pap = conf->pap;
565     	ppp_priv_area->chap = conf->chap;
566     
567     	/* Option to bring down the interface when 
568              * the link goes down */
569     	if (conf->if_down){
570     		set_bit(DYN_OPT_ON,&ppp_priv_area->interface_down);
571     		printk("%s: Dynamic interface configuration enabled\n",
572     			card->devname);
573     	} 
574     
575     	/* If no user ids are specified */
576     	if(!strlen(conf->userid) && (ppp_priv_area->pap||ppp_priv_area->chap)){
577     		kfree(ppp_priv_area);
578     		return -EINVAL;
579     	}
580     
581     	/* If no passwords are specified */
582     	if(!strlen(conf->passwd) && (ppp_priv_area->pap||ppp_priv_area->chap)){
583     		kfree(ppp_priv_area);
584     		return -EINVAL;
585     	}
586     
587     	if(strlen(conf->sysname) > 31){
588     		kfree(ppp_priv_area);
589     		return -EINVAL;
590     	}
591     
592     	/* If no system name is specified */
593     	if(!strlen(conf->sysname) && (card->u.p.authenticator)){
594     		kfree(ppp_priv_area);
595     		return -EINVAL;
596     	}
597     
598     	/* copy the data into the ppp private structure */
599     	memcpy(ppp_priv_area->userid, conf->userid, strlen(conf->userid));
600     	memcpy(ppp_priv_area->passwd, conf->passwd, strlen(conf->passwd));
601     	memcpy(ppp_priv_area->sysname, conf->sysname, strlen(conf->sysname));
602     
603     	
604     	ppp_priv_area->enable_IPX = conf->enable_IPX;
605     	if (conf->network_number){
606     		ppp_priv_area->network_number = conf->network_number;
607     	}else{
608     		ppp_priv_area->network_number = 0xDEADBEEF;
609     	}
610     
611     	/* Tells us that if this interface is a
612              * gateway or not */
613     	if ((ppp_priv_area->gateway = conf->gateway) == WANOPT_YES){
614     		printk(KERN_INFO "%s: Interface %s is set as a gateway.\n",
615     			card->devname,card->u.p.if_name);
616     	}
617     
618     	/* prepare network device data space for registration */
619     #ifdef LINUX_2_4
620      	strcpy(dev->name,card->u.p.if_name);
621     #else
622     	dev->name = (char *)kmalloc(strlen(card->u.p.if_name) + 2, GFP_KERNEL); 
623     	if(dev->name == NULL)
624     	{
625     		kfree(ppp_priv_area);
626     		return -ENOMEM;
627     	}
628     	sprintf(dev->name, "%s", card->u.p.if_name);
629     #endif
630     	
631     	dev->init = &if_init;
632     	dev->priv = ppp_priv_area;
633     	dev->mtu = min_t(unsigned int, dev->mtu, card->wandev.mtu);
634     
635     	/* Initialize the polling task routine */
636     #ifndef LINUX_2_4
637     	ppp_priv_area->poll_task.next = NULL;
638     #endif
639     	ppp_priv_area->poll_task.sync=0;
640     	ppp_priv_area->poll_task.routine = (void*)(void*)ppp_poll;
641     	ppp_priv_area->poll_task.data = dev;
642     
643     	/* Initialize the polling delay timer */
644     	init_timer(&ppp_priv_area->poll_delay_timer);
645     	ppp_priv_area->poll_delay_timer.data = (unsigned long)dev;
646     	ppp_priv_area->poll_delay_timer.function = ppp_poll_delay;
647     	
648     	
649     	/* Since we start with dummy IP addresses we can say
650     	 * that route exists */
651     	printk(KERN_INFO "\n");
652     
653     	return 0;
654     }
655     
656     /*============================================================================
657      * Delete logical channel.
658      */
659     static int del_if(wan_device_t *wandev, netdevice_t *dev)
660     {
661     	return 0;
662     }
663     
664     static void disable_comm (sdla_t *card)
665     {
666     	ppp_comm_disable_shutdown(card);
667     	return;
668     }
669     
670     /****** WANPIPE-specific entry points ***************************************/
671     
672     /*============================================================================
673      * Execute adapter interface command.
674      */
675     
676     //FIXME: Why do we need this ????
677     static int wpp_exec(struct sdla *card, void *u_cmd, void *u_data)
678     {
679     	ppp_mbox_t *mbox = card->mbox;
680     	int len;
681     
682     #if defined(LINUX_2_1) || defined(LINUX_2_4)
683     	if (copy_from_user((void*)&mbox->cmd, u_cmd, sizeof(ppp_cmd_t)))
684     		return -EFAULT;
685     
686     	len = mbox->cmd.length;
687     
688     	if (len) {
689     
690     		if( copy_from_user((void*)&mbox->data, u_data, len))
691     			return -EFAULT;
692     
693     	}
694     
695     	/* execute command */
696     	if (!sdla_exec(mbox))
697     		return -EIO;
698     
699     	/* return result */
700     	if( copy_to_user(u_cmd, (void*)&mbox->cmd, sizeof(ppp_cmd_t)))
701     		return -EFAULT;
702     	len = mbox->cmd.length;
703     
704     	if (len && u_data && copy_to_user(u_data, (void*)&mbox->data, len))
705     		return -EFAULT;
706     
707     #else
708     
709             if (!u_cmd || verify_area(VERIFY_WRITE, u_cmd, sizeof(ppp_cmd_t)))
710                     return -EFAULT;
711     
712             memcpy_fromfs((void*)&mbox->cmd, u_cmd, sizeof(ppp_cmd_t));
713     
714     	len = mbox->cmd.length;
715     
716             if (len) {
717     
718                     if (!u_data || verify_area(VERIFY_READ, u_data, len))
719                             return -EFAULT;
720             }
721     
722             /* execute command */
723             if (!sdla_exec(mbox))
724                    	return -EIO;
725     
726             /* return result */
727             memcpy_tofs(u_cmd, (void*)&mbox->cmd, sizeof(ppp_cmd_t));
728             len = mbox->cmd.length;
729     
730             if (len && u_data && !verify_area(VERIFY_WRITE, u_data, len))
731                     memcpy_tofs(u_data, (void*)&mbox->data, len);
732     
733     
734     #endif
735     
736     	return 0;
737     }
738     
739     /****** Network Device Interface ********************************************/
740     
741     /*============================================================================
742      * Initialize Linux network interface.
743      *
744      * This routine is called only once for each interface, during Linux network
745      * interface registration.  Returning anything but zero will fail interface
746      * registration.
747      */
748     static int if_init(netdevice_t *dev)
749     {
750     	ppp_private_area_t *ppp_priv_area = dev->priv;
751     	sdla_t *card = ppp_priv_area->card;
752     	wan_device_t *wandev = &card->wandev;
753     #ifdef LINUX_2_0
754     	int i;
755     #endif
756     
757     	/* Initialize device driver entry points */
758     	dev->open		= &if_open;
759     	dev->stop		= &if_close;
760     	dev->hard_header	= &if_header;
761     	dev->rebuild_header	= &if_rebuild_hdr;
762     	dev->hard_start_xmit	= &if_send;
763     	dev->get_stats		= &if_stats;
764     #ifdef LINUX_2_4
765     	dev->tx_timeout		= &if_tx_timeout;
766     	dev->watchdog_timeo	= TX_TIMEOUT;
767     #endif
768     
769     	/* Initialize media-specific parameters */
770     	dev->type		= ARPHRD_PPP;	/* ARP h/w type */
771     	dev->flags		|= IFF_POINTOPOINT;
772     	dev->flags		|= IFF_NOARP;
773     
774     	/* Enable Mulitcasting if specified by user*/
775     	if (ppp_priv_area->mc == WANOPT_YES){
776     		dev->flags	|= IFF_MULTICAST;
777     	}
778     
779     #ifdef LINUX_2_0
780     	dev->family		= AF_INET;
781     #endif	
782     	dev->mtu		= wandev->mtu;
783     	dev->hard_header_len	= PPP_HDR_LEN;	/* media header length */
784     
785     	/* Initialize hardware parameters (just for reference) */
786     	dev->irq		= wandev->irq;
787     	dev->dma		= wandev->dma;
788     	dev->base_addr		= wandev->ioport;
789     	dev->mem_start		= wandev->maddr;
790     	dev->mem_end		= wandev->maddr + wandev->msize - 1;
791     
792             /* Set transmit buffer queue length */
793             dev->tx_queue_len = 100;
794        
795     	/* Initialize socket buffers */
796           #if !defined(LINUX_2_1) && !defined(LINUX_2_4)
797             for (i = 0; i < DEV_NUMBUFFS; ++i)
798                     skb_queue_head_init(&dev->buffs[i]);
799           #endif
800     
801     	return 0;
802     }
803     
804     /*============================================================================
805      * Open network interface.
806      * o enable communications and interrupts.
807      * o prevent module from unloading by incrementing use count
808      *
809      * Return 0 if O.k. or errno.
810      */
811     static int if_open (netdevice_t *dev)
812     {
813     	ppp_private_area_t *ppp_priv_area = dev->priv;
814     	sdla_t *card = ppp_priv_area->card;
815     	struct timeval tv;
816     	//unsigned long smp_flags;
817     
818     	if (is_dev_running(dev))
819     		return -EBUSY;
820     
821     	wanpipe_open(card);
822     
823     #ifdef LINUX_2_4
824     	netif_start_queue(dev);
825     #else
826     	dev->interrupt = 0;
827     	dev->tbusy = 0;
828     	dev->start = 1;
829     #endif
830     	
831     	do_gettimeofday( &tv );
832     	ppp_priv_area->router_start_time = tv.tv_sec;
833     
834     	/* We cannot configure the card here because we don't
835     	 * have access to the interface IP addresses.
836              * Once the interface initilization is complete, we will be
837              * able to access the IP addresses.  Therefore,
838              * configure the ppp link in the poll routine */
839     	set_bit(0,&ppp_priv_area->config_ppp);
840     	ppp_priv_area->config_wait_timeout=jiffies;
841     
842     	/* Start the PPP configuration after 1sec delay.
843     	 * This will give the interface initilization time
844     	 * to finish its configuration */
845     	del_timer(&ppp_priv_area->poll_delay_timer);
846     	ppp_priv_area->poll_delay_timer.expires = jiffies+HZ;
847     	add_timer(&ppp_priv_area->poll_delay_timer);
848     	return 0;
849     }
850     
851     /*============================================================================
852      * Close network interface.
853      * o if this is the last open, then disable communications and interrupts.
854      * o reset flags.
855      */
856     static int if_close(netdevice_t *dev)
857     {
858     	ppp_private_area_t *ppp_priv_area = dev->priv;
859     	sdla_t *card = ppp_priv_area->card;
860     
861     	stop_net_queue(dev);
862     #ifndef LINUX_2_4
863     	dev->start=0;
864     #endif
865     	wanpipe_close(card);
866     
867     	del_timer (&ppp_priv_area->poll_delay_timer);
868     	return 0;
869     }
870     
871     /*============================================================================
872      * Build media header.
873      *
874      * The trick here is to put packet type (Ethertype) into 'protocol' field of
875      * the socket buffer, so that we don't forget it.  If packet type is not
876      * supported, set skb->protocol to 0 and discard packet later.
877      *
878      * Return:	media header length.
879      */
880     static int if_header(struct sk_buff *skb, netdevice_t *dev,
881     	unsigned short type, void *daddr, void *saddr, unsigned len)
882     {
883     	switch (type)
884     	{
885     		case ETH_P_IP:
886     		case ETH_P_IPX:
887     			skb->protocol = htons(type);
888     			break;
889     
890     		default:
891     			skb->protocol = 0;
892     	}
893     
894     	return PPP_HDR_LEN;
895     }
896     
897     /*============================================================================
898      * Re-build media header.
899      *
900      * Return:	1	physical address resolved.
901      *		0	physical address not resolved
902      */
903     #if defined(LINUX_2_1) || defined(LINUX_2_4)
904     static int if_rebuild_hdr (struct sk_buff *skb)
905     {
906     	netdevice_t *dev = skb->dev;
907     	ppp_private_area_t *ppp_priv_area = dev->priv;
908     	sdla_t *card = ppp_priv_area->card;
909     
910     	printk(KERN_INFO "%s: rebuild_header() called for interface %s!\n",
911     		card->devname, dev->name);
912     	return 1;
913     }
914     
915     #else
916     static int if_rebuild_hdr (void* hdr, netdevice_t* dev, unsigned long raddr,
917                                struct sk_buff* skb)
918     {
919     	return 1;
920     
921     }
922     #endif
923     
924     #ifdef LINUX_2_4
925     /*============================================================================
926      * Handle transmit timeout event from netif watchdog
927      */
928     static void if_tx_timeout (netdevice_t *dev)
929     {
930         	ppp_private_area_t* chan = dev->priv;
931     	sdla_t *card = chan->card;
932     	
933     	/* If our device stays busy for at least 5 seconds then we will
934     	 * kick start the device by making dev->tbusy = 0.  We expect
935     	 * that our device never stays busy more than 5 seconds. So this                 
936     	 * is only used as a last resort.
937     	 */
938     
939     	++ chan->if_send_stat.if_send_tbusy;
940     	++card->wandev.stats.collisions;
941     
942     	printk (KERN_INFO "%s: Transmit timed out on %s\n", card->devname,dev->name);
943     	++chan->if_send_stat.if_send_tbusy_timeout;
944     	netif_wake_queue (dev);
945     }
946     #endif
947     
948     
949     
950     /*============================================================================
951      * Send a packet on a network interface.
952      * o set tbusy flag (marks start of the transmission) to block a timer-based
953      *   transmit from overlapping.
954      * o check link state. If link is not up, then drop the packet.
955      * o execute adapter send command.
956      * o free socket buffer
957      *
958      * Return:	0	complete (socket buffer must be freed)
959      *		non-0	packet may be re-transmitted (tbusy must be set)
960      *
961      * Notes:
962      * 1. This routine is called either by the protocol stack or by the "net
963      *    bottom half" (with interrupts enabled).
964      * 2. Setting tbusy flag will inhibit further transmit requests from the
965      *    protocol stack and can be used for flow control with protocol layer.
966      */
967     static int if_send (struct sk_buff *skb, netdevice_t *dev)
968     {
969     	ppp_private_area_t *ppp_priv_area = dev->priv;
970     	sdla_t *card = ppp_priv_area->card;
971     	unsigned char *sendpacket;
972     	unsigned long smp_flags;
973     	ppp_flags_t *flags = card->flags;
974     	int udp_type;
975     	int err=0;
976     	
977     	++ppp_priv_area->if_send_stat.if_send_entry;
978     
979     #ifdef LINUX_2_4
980     	netif_stop_queue(dev);
981     #endif
982     	
983     	if (skb == NULL) {
984     
985     		/* If we get here, some higher layer thinks we've missed an
986     		 * tx-done interrupt.
987     		 */
988     		printk(KERN_INFO "%s: interface %s got kicked!\n",
989     			card->devname, dev->name);
990     		
991     		++ppp_priv_area->if_send_stat.if_send_skb_null;
992     	
993     		wake_net_dev(dev);
994     		return 0;
995     	}
996     
997     #ifndef LINUX_2_4
998     	if (dev->tbusy) {
999     
1000     		/* If our device stays busy for at least 5 seconds then we will
1001     		 * kick start the device by making dev->tbusy = 0.  We expect 
1002     		 * that our device never stays busy more than 5 seconds. So this
1003     		 * is only used as a last resort. 
1004     		 */
1005                   
1006     		++ppp_priv_area->if_send_stat.if_send_tbusy;
1007             	++card->wandev.stats.collisions;
1008     
1009     		if ((jiffies - ppp_priv_area->tick_counter) < (5*HZ)) {
1010     			return 1;
1011     		}
1012     
1013     		printk (KERN_INFO "%s: Transmit times out on %s\n",card->devname,dev->name);
1014     	
1015     		++ppp_priv_area->if_send_stat.if_send_tbusy_timeout;
1016     		++card->wandev.stats.collisions;
1017     
1018     		/* unbusy the card (because only one interface per card)*/
1019     		dev->tbusy = 0;
1020     	}	
1021     #endif
1022     	
1023     	sendpacket = skb->data;
1024     
1025     	udp_type = udp_pkt_type( skb, card );
1026     
1027     
1028     	if (udp_type == UDP_PTPIPE_TYPE){
1029     		if(store_udp_mgmt_pkt(UDP_PKT_FRM_STACK, card, skb, dev,
1030                     	              ppp_priv_area)){
1031     	               	flags->imask |= PPP_INTR_TIMER;
1032     		}
1033     		++ppp_priv_area->if_send_stat.if_send_PIPE_request;
1034     		start_net_queue(dev);
1035     		return 0;
1036     	}
1037     
1038     	/* Check for broadcast and multicast addresses 
1039     	 * If found, drop (deallocate) a packet and return.
1040     	 */
1041     	if(chk_bcast_mcast_addr(card, dev, skb)){
1042     		++card->wandev.stats.tx_dropped;
1043     		wan_dev_kfree_skb(skb,FREE_WRITE);
1044     		start_net_queue(dev);
1045     		return 0;
1046     	}
1047     
1048     
1049      	if(card->hw.type != SDLA_S514){
1050     		s508_lock(card,&smp_flags);
1051     	}
1052     
1053         	if (test_and_set_bit(SEND_CRIT, (void*)&card->wandev.critical)) {
1054     
1055     		printk(KERN_INFO "%s: Critical in if_send: %lx\n",
1056     				card->wandev.name,card->wandev.critical);
1057     		
1058     		++card->wandev.stats.tx_dropped;
1059     		++ppp_priv_area->if_send_stat.if_send_critical_non_ISR;
1060     		start_net_queue(dev);
1061     		goto if_send_exit_crit;
1062     	}
1063     
1064     	if (card->wandev.state != WAN_CONNECTED) {
1065     
1066     		++ppp_priv_area->if_send_stat.if_send_wan_disconnected;
1067             	++card->wandev.stats.tx_dropped;
1068     		start_net_queue(dev);
1069     		
1070          	} else if (!skb->protocol) {
1071     		++ppp_priv_area->if_send_stat.if_send_protocol_error;
1072             	++card->wandev.stats.tx_errors;
1073     		start_net_queue(dev);
1074     		
1075     	} else {
1076     
1077     		/*If it's IPX change the network numbers to 0 if they're ours.*/
1078     		if( skb->protocol == htons(ETH_P_IPX) ) {
1079     			if(ppp_priv_area->enable_IPX) {
1080     				switch_net_numbers( skb->data, 
1081     					ppp_priv_area->network_number, 0);
1082     			} else {
1083     				++card->wandev.stats.tx_dropped;
1084     				start_net_queue(dev);
1085     				goto if_send_exit_crit;
1086     			}
1087     		}
1088     
1089     		if (ppp_send(card, skb->data, skb->len, skb->protocol)) {
1090     			stop_net_queue(dev);
1091     			++ppp_priv_area->if_send_stat.if_send_adptr_bfrs_full;
1092     			++ppp_priv_area->if_send_stat.if_send_tx_int_enabled;
1093     		} else {
1094     			++ppp_priv_area->if_send_stat.if_send_bfr_passed_to_adptr;
1095     			++card->wandev.stats.tx_packets;
1096     #if defined(LINUX_2_1) || defined(LINUX_2_4)
1097     			card->wandev.stats.tx_bytes += skb->len;
1098     #endif
1099     			start_net_queue(dev);
1100     #ifdef LINUX_2_4
1101     			dev->trans_start = jiffies;
1102     #endif
1103     		}
1104         	}
1105     	
1106     if_send_exit_crit:
1107     	
1108     	if (!(err=is_queue_stopped(dev))){
1109           		wan_dev_kfree_skb(skb, FREE_WRITE);
1110     	}else{
1111     		ppp_priv_area->tick_counter = jiffies;
1112     		flags->imask |= PPP_INTR_TXRDY;	/* unmask Tx interrupts */
1113     	}
1114     	
1115     	clear_bit(SEND_CRIT,&card->wandev.critical);
1116     	if(card->hw.type != SDLA_S514){	
1117     		s508_unlock(card,&smp_flags);
1118     	}
1119     
1120     	return err;
1121     }
1122     
1123     
1124     /*=============================================================================
1125      * Store a UDP management packet for later processing.
1126      */
1127     
1128     static int store_udp_mgmt_pkt(char udp_pkt_src, sdla_t* card,
1129                                     struct sk_buff *skb, netdevice_t* dev,
1130                                     ppp_private_area_t* ppp_priv_area )
1131     {
1132     	int udp_pkt_stored = 0;
1133     
1134     	if(!ppp_priv_area->udp_pkt_lgth && (skb->len<=MAX_LGTH_UDP_MGNT_PKT)){
1135             	ppp_priv_area->udp_pkt_lgth = skb->len;
1136     		ppp_priv_area->udp_pkt_src = udp_pkt_src;
1137            		memcpy(ppp_priv_area->udp_pkt_data, skb->data, skb->len);
1138     		ppp_priv_area->timer_int_enabled |= TMR_INT_ENABLED_UDP;
1139     		ppp_priv_area->protocol = skb->protocol;
1140     		udp_pkt_stored = 1;
1141     	}else{
1142     		if (skb->len > MAX_LGTH_UDP_MGNT_PKT){
1143     #if defined(LINUX_2_1) || defined(LINUX_2_4)
1144     			printk(KERN_INFO "%s: PIPEMON UDP request too long : %i\n",
1145     				card->devname, skb->len);
1146     #else
1147     			printk(KERN_INFO "%s: PIPEMON UDP request too long : %li\n",
1148     				card->devname, skb->len);
1149     #endif
1150     		}else{
1151     			printk(KERN_INFO "%s: PIPEMON UPD request already pending\n",
1152     				card->devname);
1153     		}
1154     		ppp_priv_area->udp_pkt_lgth = 0;
1155     	}
1156     
1157     	if(udp_pkt_src == UDP_PKT_FRM_STACK){
1158     		wan_dev_kfree_skb(skb, FREE_WRITE);
1159     	}else{
1160                     wan_dev_kfree_skb(skb, FREE_READ);
1161     	}
1162     
1163     	return(udp_pkt_stored);
1164     }
1165     
1166     
1167     
1168     /*============================================================================
1169      * Reply to UDP Management system.
1170      * Return length of reply.
1171      */
1172     static int reply_udp( unsigned char *data, unsigned int mbox_len ) 
1173     {
1174     	unsigned short len, udp_length, temp, ip_length;
1175     	unsigned long ip_temp;
1176     	int even_bound = 0;
1177     	ppp_udp_pkt_t *p_udp_pkt = (ppp_udp_pkt_t *)data;
1178      
1179     	/* Set length of packet */
1180     	len = sizeof(ip_pkt_t)+ 
1181     	      sizeof(udp_pkt_t)+
1182     	      sizeof(wp_mgmt_t)+
1183     	      sizeof(cblock_t)+
1184     	      mbox_len;
1185     
1186     	/* fill in UDP reply */
1187       	p_udp_pkt->wp_mgmt.request_reply = UDPMGMT_REPLY; 
1188     
1189     	/* fill in UDP length */
1190     	udp_length = sizeof(udp_pkt_t)+ 
1191     		     sizeof(wp_mgmt_t)+
1192     		     sizeof(cblock_t)+
1193     		     mbox_len; 
1194       
1195      
1196     	/* put it on an even boundary */
1197     	if ( udp_length & 0x0001 ) {
1198     		udp_length += 1;
1199     		len += 1;
1200     		even_bound=1;
1201     	} 
1202     	
1203     	temp = (udp_length<<8)|(udp_length>>8);
1204     	p_udp_pkt->udp_pkt.udp_length = temp;		
1205     
1206      
1207     	/* swap UDP ports */
1208     	temp = p_udp_pkt->udp_pkt.udp_src_port;
1209     	p_udp_pkt->udp_pkt.udp_src_port = 
1210     			p_udp_pkt->udp_pkt.udp_dst_port; 
1211     	p_udp_pkt->udp_pkt.udp_dst_port = temp;
1212     
1213     
1214     	/* add UDP pseudo header */
1215     	temp = 0x1100;
1216     	*((unsigned short *)(p_udp_pkt->data+mbox_len+even_bound)) = temp;
1217     	temp = (udp_length<<8)|(udp_length>>8);
1218     	*((unsigned short *)(p_udp_pkt->data+mbox_len+even_bound+2)) = temp;
1219      
1220     	/* calculate UDP checksum */
1221     	p_udp_pkt->udp_pkt.udp_checksum = 0;
1222     	p_udp_pkt->udp_pkt.udp_checksum = 
1223     		calc_checksum(&data[UDP_OFFSET],udp_length+UDP_OFFSET);
1224     
1225     	/* fill in IP length */
1226     	ip_length = udp_length + sizeof(ip_pkt_t);
1227     	temp = (ip_length<<8)|(ip_length>>8);
1228       	p_udp_pkt->ip_pkt.total_length = temp;
1229      
1230     	/* swap IP addresses */
1231     	ip_temp = p_udp_pkt->ip_pkt.ip_src_address;
1232     	p_udp_pkt->ip_pkt.ip_src_address = p_udp_pkt->ip_pkt.ip_dst_address;
1233     	p_udp_pkt->ip_pkt.ip_dst_address = ip_temp;
1234     
1235     	/* fill in IP checksum */
1236     	p_udp_pkt->ip_pkt.hdr_checksum = 0;
1237     	p_udp_pkt->ip_pkt.hdr_checksum = calc_checksum(data,sizeof(ip_pkt_t));
1238     
1239     	return len;
1240     
1241     } /* reply_udp */
1242     
1243     unsigned short calc_checksum (char *data, int len)
1244     {
1245     	unsigned short temp; 
1246     	unsigned long sum=0;
1247     	int i;
1248     
1249     	for( i = 0; i <len; i+=2 ) {
1250     		memcpy(&temp,&data[i],2);
1251     		sum += (unsigned long)temp;
1252     	}
1253     
1254     	while (sum >> 16 ) {
1255     		sum = (sum & 0xffffUL) + (sum >> 16);
1256     	}
1257     
1258     	temp = (unsigned short)sum;
1259     	temp = ~temp;
1260     
1261     	if( temp == 0 ) 
1262     		temp = 0xffff;
1263     
1264     	return temp;	
1265     }
1266     
1267     /*
1268        If incoming is 0 (outgoing)- if the net numbers is ours make it 0
1269        if incoming is 1 - if the net number is 0 make it ours 
1270     
1271     */
1272     static void switch_net_numbers(unsigned char *sendpacket, unsigned long network_number, unsigned char incoming)
1273     {
1274     	unsigned long pnetwork_number;
1275     
1276     	pnetwork_number = (unsigned long)((sendpacket[6] << 24) + 
1277     			  (sendpacket[7] << 16) + (sendpacket[8] << 8) + 
1278     			  sendpacket[9]);
1279     
1280     	if (!incoming) {
1281     		//If the destination network number is ours, make it 0
1282     		if( pnetwork_number == network_number) {
1283     			sendpacket[6] = sendpacket[7] = sendpacket[8] = 
1284     					 sendpacket[9] = 0x00;
1285     		}
1286     	} else {
1287     		//If the incoming network is 0, make it ours
1288     		if( pnetwork_number == 0) {
1289     			sendpacket[6] = (unsigned char)(network_number >> 24);
1290     			sendpacket[7] = (unsigned char)((network_number & 
1291     					 0x00FF0000) >> 16);
1292     			sendpacket[8] = (unsigned char)((network_number & 
1293     					 0x0000FF00) >> 8);
1294     			sendpacket[9] = (unsigned char)(network_number & 
1295     					 0x000000FF);
1296     		}
1297     	}
1298     
1299     
1300     	pnetwork_number = (unsigned long)((sendpacket[18] << 24) + 
1301     			  (sendpacket[19] << 16) + (sendpacket[20] << 8) + 
1302     			  sendpacket[21]);
1303     
1304     	if( !incoming ) {
1305     		//If the source network is ours, make it 0
1306     		if( pnetwork_number == network_number) {
1307     			sendpacket[18] = sendpacket[19] = sendpacket[20] = 
1308     					 sendpacket[21] = 0x00;
1309     		}
1310     	} else {
1311     		//If the source network is 0, make it ours
1312     		if( pnetwork_number == 0 ) {
1313     			sendpacket[18] = (unsigned char)(network_number >> 24);
1314     			sendpacket[19] = (unsigned char)((network_number & 
1315     					 0x00FF0000) >> 16);
1316     			sendpacket[20] = (unsigned char)((network_number & 
1317     					 0x0000FF00) >> 8);
1318     			sendpacket[21] = (unsigned char)(network_number & 
1319     					 0x000000FF);
1320     		}
1321     	}
1322     } /* switch_net_numbers */
1323     
1324     /*============================================================================
1325      * Get ethernet-style interface statistics.
1326      * Return a pointer to struct net_device_stats.
1327      */
1328     #if defined(LINUX_2_1) || defined(LINUX_2_4)
1329     static struct net_device_stats *if_stats(netdevice_t *dev)
1330     #else
1331     static struct enet_statistics *if_stats(netdevice_t *dev)
1332     #endif
1333     {
1334     
1335     	ppp_private_area_t *ppp_priv_area = dev->priv;
1336     	sdla_t* card;
1337     	
1338     	if( ppp_priv_area == NULL )
1339     		return NULL;
1340     
1341     	card = ppp_priv_area->card;
1342     	return &card->wandev.stats;
1343     }
1344     
1345     /****** PPP Firmware Interface Functions ************************************/
1346     
1347     /*============================================================================
1348      * Read firmware code version.
1349      *	Put code version as ASCII string in str. 
1350      */
1351     static int ppp_read_version(sdla_t *card, char *str)
1352     {
1353     	ppp_mbox_t *mb = card->mbox;
1354     	int err;
1355     
1356     	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
1357     	mb->cmd.command = PPP_READ_CODE_VERSION;
1358     	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
1359     
1360     	if (err != CMD_OK)
1361      
1362     		ppp_error(card, err, mb);
1363     
1364     	else if (str) {
1365     
1366     		int len = mb->cmd.length;
1367     
1368     		memcpy(str, mb->data, len);
1369     		str[len] = '\0';
1370     
1371     	}
1372     
1373     	return err;
1374     }
1375     /*===========================================================================
1376      * Set Out-Bound Authentication.
1377     */
1378     static int ppp_set_outbnd_auth (sdla_t *card, ppp_private_area_t *ppp_priv_area)
1379     {
1380     	ppp_mbox_t *mb = card->mbox;
1381     	int err;
1382     
1383     	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
1384     	memset(&mb->data, 0, (strlen(ppp_priv_area->userid) + 
1385     					strlen(ppp_priv_area->passwd) + 2 ) );
1386     	memcpy(mb->data, ppp_priv_area->userid, strlen(ppp_priv_area->userid));
1387     	memcpy((mb->data + strlen(ppp_priv_area->userid) + 1), 
1388     		ppp_priv_area->passwd, strlen(ppp_priv_area->passwd));	
1389     	
1390     	mb->cmd.length  = strlen(ppp_priv_area->userid) + 
1391     					strlen(ppp_priv_area->passwd) + 2 ;
1392     	
1393     	mb->cmd.command = PPP_SET_OUTBOUND_AUTH;
1394     
1395     	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
1396     
1397     	if (err != CMD_OK)
1398     		ppp_error(card, err, mb);
1399     
1400     	return err;
1401     }
1402     
1403     /*===========================================================================
1404      * Set In-Bound Authentication.
1405     */
1406     static int ppp_set_inbnd_auth (sdla_t *card, ppp_private_area_t *ppp_priv_area)
1407     {
1408     	ppp_mbox_t *mb = card->mbox;
1409     	int err, i;
1410     	char* user_tokens[32];
1411     	char* pass_tokens[32];
1412     	int userids, passwds;
1413     	int add_ptr;
1414     
1415     	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
1416     	memset(&mb->data, 0, 1008);
1417     	memcpy(mb->data, ppp_priv_area->sysname, 
1418     						strlen(ppp_priv_area->sysname));
1419     	
1420     	/* Parse the userid string and the password string and build a string
1421     	   to copy it to the data area of the command structure.   The string
1422     	   will look like "SYS_NAME<NULL>USER1<NULL>PASS1<NULL>USER2<NULL>PASS2
1423     	   ....<NULL> " 
1424     	 */
1425     	userids = tokenize( ppp_priv_area->userid, user_tokens);
1426     	passwds = tokenize( ppp_priv_area->passwd, pass_tokens);
1427     	
1428     	if (userids != passwds){
1429     		printk(KERN_INFO "%s: Number of passwords does not equal the number of user ids\n", card->devname);
1430     		return 1;	
1431     	}
1432     
1433     	add_ptr = strlen(ppp_priv_area->sysname) + 1;
1434     	for (i=0; i<userids; i++){
1435     		memcpy((mb->data + add_ptr), user_tokens[i], 
1436     							strlen(user_tokens[i]));
1437     		memcpy((mb->data + add_ptr + strlen(user_tokens[i]) + 1), 
1438     					pass_tokens[i], strlen(pass_tokens[i]));
1439     		add_ptr = add_ptr + strlen(user_tokens[i]) + 1 + 
1440     						strlen(pass_tokens[i]) + 1;
1441     	}
1442     
1443     	mb->cmd.length  = add_ptr + 1;
1444     	mb->cmd.command = PPP_SET_INBOUND_AUTH;
1445     
1446     	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
1447     
1448     	if (err != CMD_OK)
1449     		ppp_error(card, err, mb);
1450     
1451     	return err;
1452     }
1453     
1454     
1455     /*============================================================================
1456      * Tokenize string.
1457      *      Parse a string of the following syntax:
1458      *              <arg1>,<arg2>,...
1459      *      and fill array of tokens with pointers to string elements.
1460      *
1461      */
1462     static int tokenize (char *str, char **tokens)
1463     {
1464             int cnt = 0;
1465     
1466             tokens[0] = strtok(str, "/");
1467             while (tokens[cnt] && (cnt < 32 - 1))
1468             {
1469                     tokens[cnt] = strstrip(tokens[cnt], " \t");
1470                     tokens[++cnt] = strtok(NULL, "/");
1471             }
1472     	return cnt;
1473     }
1474     
1475     /*============================================================================
1476      * Strip leading and trailing spaces off the string str.
1477      */
1478     static char* strstrip (char *str, char* s)
1479     {
1480             char *eos = str + strlen(str);          /* -> end of string */
1481     
1482             while (*str && strchr(s, *str))
1483                     ++str                           /* strip leading spaces */
1484             ;
1485             while ((eos > str) && strchr(s, *(eos - 1)))
1486                     --eos                           /* strip trailing spaces */
1487             ;
1488             *eos = '\0';
1489             return str;
1490     }
1491     /*============================================================================
1492      * Configure PPP firmware.
1493      */
1494     static int ppp_configure(sdla_t *card, void *data)
1495     {
1496     	ppp_mbox_t *mb = card->mbox;
1497     	int data_len = sizeof(ppp508_conf_t); 
1498     	int err;
1499     
1500     	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
1501     	memcpy(mb->data, data, data_len);
1502     	mb->cmd.length  = data_len;
1503     	mb->cmd.command = PPP_SET_CONFIG;
1504     	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
1505     
1506     	if (err != CMD_OK) 
1507     		ppp_error(card, err, mb);
1508     	
1509     	return err;
1510     }
1511     
1512     /*============================================================================
1513      * Set interrupt mode.
1514      */
1515     static int ppp_set_intr_mode(sdla_t *card, unsigned char mode)
1516     {
1517     	ppp_mbox_t *mb = card->mbox;
1518             ppp_intr_info_t *ppp_intr_data = (ppp_intr_info_t *) &mb->data[0];
1519     	int err;
1520     
1521     	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
1522     	ppp_intr_data->i_enable = mode;
1523     
1524     	ppp_intr_data->irq = card->hw.irq;
1525     	mb->cmd.length = 2;
1526     
1527            /* If timer has been enabled, set the timer delay to 1sec */
1528            if (mode & 0x80){
1529            		ppp_intr_data->timer_len = 250; //5;//100; //250;
1530                     mb->cmd.length = 4;
1531             }
1532     	
1533     	mb->cmd.command = PPP_SET_INTR_FLAGS;
1534     	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
1535     	
1536     	if (err != CMD_OK) 
1537     		ppp_error(card, err, mb);
1538      		
1539     
1540     	return err;
1541     }
1542     
1543     /*============================================================================
1544      * Enable communications.
1545      */
1546     static int ppp_comm_enable(sdla_t *card)
1547     {
1548     	ppp_mbox_t *mb = card->mbox;
1549     	int err;
1550     
1551     	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
1552     	mb->cmd.command = PPP_COMM_ENABLE;
1553     	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
1554     	
1555     	if (err != CMD_OK) 
1556     		ppp_error(card, err, mb);
1557     	else	
1558     		card->u.p.comm_enabled = 1;	
1559     
1560     	return err;
1561     }
1562     
1563     /*============================================================================
1564      * Disable communications.
1565      */
1566     static int ppp_comm_disable(sdla_t *card)
1567     {
1568     	ppp_mbox_t *mb = card->mbox;
1569     	int err;
1570     
1571     	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
1572     	mb->cmd.command = PPP_COMM_DISABLE;
1573     	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
1574     	if (err != CMD_OK) 
1575     		ppp_error(card, err, mb);
1576     	else
1577     		card->u.p.comm_enabled = 0;
1578     
1579     	return err;
1580     }
1581     
1582     static int ppp_comm_disable_shutdown(sdla_t *card)
1583     {
1584     	ppp_mbox_t *mb = card->mbox;
1585     	ppp_intr_info_t *ppp_intr_data;
1586     	int err;
1587     
1588     	if (!mb){
1589     		return 1;
1590     	}
1591     	
1592     	ppp_intr_data = (ppp_intr_info_t *) &mb->data[0];
1593     	
1594     	/* Disable all interrupts */
1595     	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
1596     	ppp_intr_data->i_enable = 0;
1597     
1598     	ppp_intr_data->irq = card->hw.irq;
1599     	mb->cmd.length = 2;
1600     
1601     	mb->cmd.command = PPP_SET_INTR_FLAGS;
1602     	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
1603     
1604     	/* Disable communicatinons */
1605     	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
1606     	mb->cmd.command = PPP_COMM_DISABLE;
1607     	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
1608     
1609     	card->u.p.comm_enabled = 0;
1610     
1611     	return 0;
1612     }
1613     
1614     
1615     
1616     /*============================================================================
1617      * Get communications error statistics.
1618      */
1619     static int ppp_get_err_stats(sdla_t *card)
1620     {
1621     	ppp_mbox_t *mb = card->mbox;
1622     	int err;
1623     
1624     	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
1625     	mb->cmd.command = PPP_READ_ERROR_STATS;
1626     	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
1627     	
1628     	if (err == CMD_OK) {
1629     		
1630     		ppp_err_stats_t* stats = (void*)mb->data;
1631     		card->wandev.stats.rx_over_errors    = stats->rx_overrun;
1632     		card->wandev.stats.rx_crc_errors     = stats->rx_bad_crc;
1633     		card->wandev.stats.rx_missed_errors  = stats->rx_abort;
1634     		card->wandev.stats.rx_length_errors  = stats->rx_lost;
1635     		card->wandev.stats.tx_aborted_errors = stats->tx_abort;
1636     	
1637     	} else 
1638     		ppp_error(card, err, mb);
1639     	
1640     	return err;
1641     }
1642     
1643     /*============================================================================
1644      * Send packet.
1645      *	Return:	0 - o.k.
1646      *		1 - no transmit buffers available
1647      */
1648     static int ppp_send (sdla_t *card, void *data, unsigned len, unsigned proto)
1649     {
1650     	ppp_buf_ctl_t *txbuf = card->u.p.txbuf;
1651     
1652     	if (txbuf->flag)
1653                     return 1;
1654     	
1655     	sdla_poke(&card->hw, txbuf->buf.ptr, data, len);
1656     
1657     	txbuf->length = len;		/* frame length */
1658     	
1659     	if (proto == htons(ETH_P_IPX))
1660     		txbuf->proto = 0x01;	/* protocol ID */
1661     	else
1662     		txbuf->proto = 0x00;	/* protocol ID */
1663     	
1664     	txbuf->flag = 1;		/* start transmission */
1665     
1666     	/* Update transmit buffer control fields */
1667     	card->u.p.txbuf = ++txbuf;
1668     
1669     	if ((void*)txbuf > card->u.p.txbuf_last)
1670     		card->u.p.txbuf = card->u.p.txbuf_base;
1671     
1672     	return 0;
1673     }
1674     
1675     /****** Firmware Error Handler **********************************************/
1676     
1677     /*============================================================================
1678      * Firmware error handler.
1679      *	This routine is called whenever firmware command returns non-zero
1680      *	return code.
1681      *
1682      * Return zero if previous command has to be cancelled.
1683      */
1684     static int ppp_error(sdla_t *card, int err, ppp_mbox_t *mb)
1685     {
1686     	unsigned cmd = mb->cmd.command;
1687     
1688     	switch (err) {
1689     
1690     		case CMD_TIMEOUT:
1691     			printk(KERN_ERR "%s: command 0x%02X timed out!\n",
1692     				card->devname, cmd);
1693     			break;
1694     
1695     		default:
1696     			printk(KERN_INFO "%s: command 0x%02X returned 0x%02X!\n"
1697     				, card->devname, cmd, err);
1698     	}
1699     
1700     	return 0;
1701     }
1702     
1703     /****** Interrupt Handlers **************************************************/
1704     
1705     /*============================================================================
1706      * PPP interrupt service routine.
1707      */
1708     static void wpp_isr (sdla_t *card)
1709     {
1710     	ppp_flags_t *flags = card->flags;
1711     	char *ptr = &flags->iflag;
1712     	netdevice_t *dev = card->wandev.dev;
1713     	int i;
1714     
1715     	card->in_isr = 1;
1716     	++card->statistics.isr_entry;
1717     
1718     	if (!dev && flags->iflag != PPP_INTR_CMD){
1719     		card->in_isr = 0;
1720     		flags->iflag = 0;
1721     		return;
1722     	}
1723     	
1724     	if (test_bit(PERI_CRIT, (void*)&card->wandev.critical)) {
1725     		card->in_isr = 0;
1726     		flags->iflag = 0;
1727     		return;
1728     	}
1729     	
1730     	
1731     	if(card->hw.type != SDLA_S514){
1732     		if (test_bit(SEND_CRIT, (void*)&card->wandev.critical)) {
1733     			++card->statistics.isr_already_critical;
1734     			printk (KERN_INFO "%s: Critical while in ISR!\n",
1735     					card->devname);
1736     			card->in_isr = 0;
1737     			flags->iflag = 0;
1738     			return;
1739     		}
1740     	}
1741     
1742     	switch (flags->iflag) {
1743     
1744     		case PPP_INTR_RXRDY:	/* receive interrupt  0x01  (bit 0)*/
1745     			++card->statistics.isr_rx;
1746     			rx_intr(card);
1747     			break;
1748     
1749     		case PPP_INTR_TXRDY:	/* transmit interrupt  0x02 (bit 1)*/
1750     			++card->statistics.isr_tx;
1751     			flags->imask &= ~PPP_INTR_TXRDY;
1752     			wake_net_dev(dev);
1753     			break;
1754     
1755     		case PPP_INTR_CMD:      /* interface command completed */
1756     			++Intr_test_counter;
1757     			++card->statistics.isr_intr_test;
1758     			break;
1759     
1760     		case PPP_INTR_MODEM:    /* modem status change (DCD, CTS) 0x04 (bit 2)*/
1761     		case PPP_INTR_DISC:  	/* Data link disconnected 0x10  (bit 4)*/	
1762     		case PPP_INTR_OPEN:   	/* Data link open 0x20  (bit 5)*/
1763     		case PPP_INTR_DROP_DTR:	/* DTR drop timeout expired  0x40 bit 6 */
1764     			event_intr(card);
1765     			break;
1766     	
1767     		case PPP_INTR_TIMER:
1768     			timer_intr(card);
1769     			break;	 
1770     
1771     		default:	/* unexpected interrupt */
1772     			++card->statistics.isr_spurious;
1773     			printk(KERN_INFO "%s: spurious interrupt 0x%02X!\n", 
1774     				card->devname, flags->iflag);
1775     			printk(KERN_INFO "%s: ID Bytes = ",card->devname);
1776     	 		for(i = 0; i < 8; i ++)
1777     				printk(KERN_INFO "0x%02X ", *(ptr + 0x28 + i));
1778     			printk(KERN_INFO "\n");	
1779     	}
1780     	
1781     	card->in_isr = 0;
1782     	flags->iflag = 0;
1783     	return;
1784     }
1785     
1786     /*============================================================================
1787      * Receive interrupt handler.
1788      */
1789     static void rx_intr(sdla_t *card)
1790     {
1791     	ppp_buf_ctl_t *rxbuf = card->rxmb;
1792     	netdevice_t *dev = card->wandev.dev;
1793     	ppp_private_area_t *ppp_priv_area;
1794     	struct sk_buff *skb;
1795     	unsigned len;
1796     	void *buf;
1797     	int i;
1798             ppp_flags_t *flags = card->flags;
1799             char *ptr = &flags->iflag;
1800     	int udp_type;
1801     	
1802     
1803     	if (rxbuf->flag != 0x01) {
1804     
1805     		printk(KERN_INFO 
1806     			"%s: corrupted Rx buffer @ 0x%X, flag = 0x%02X!\n", 
1807     			card->devname, (unsigned)rxbuf, rxbuf->flag);
1808     	
1809     		printk(KERN_INFO "%s: ID Bytes = ",card->devname);
1810     	 	
1811     		for(i = 0; i < 8; i ++)
1812     			printk(KERN_INFO "0x%02X ", *(ptr + 0x28 + i));
1813     		printk(KERN_INFO "\n");	
1814     		
1815     		++card->statistics.rx_intr_corrupt_rx_bfr;
1816     
1817     
1818     		/* Bug Fix: Mar 6 2000
1819                      * If we get a corrupted mailbox, it means that driver 
1820                      * is out of sync with the firmware. There is no recovery.
1821                      * If we don't turn off all interrupts for this card
1822                      * the machine will crash. 
1823                      */
1824     		printk(KERN_INFO "%s: Critical router failure ...!!!\n", card->devname);
1825     		printk(KERN_INFO "Please contact Sangoma Technologies !\n");
1826     		ppp_set_intr_mode(card,0);
1827     		return;
1828     	}
1829           
1830     	if (dev && is_dev_running(dev) && dev->priv){
1831     	
1832     		len  = rxbuf->length;
1833     		ppp_priv_area = dev->priv;
1834     
1835     		/* Allocate socket buffer */
1836     		skb = dev_alloc_skb(len);
1837     
1838     		if (skb != NULL) {
1839     		
1840     			/* Copy data to the socket buffer */
1841     			unsigned addr = rxbuf->buf.ptr;
1842     
1843     			if ((addr + len) > card->u.p.rx_top + 1) {
1844     			
1845     				unsigned tmp = card->u.p.rx_top - addr + 1;
1846     				buf = skb_put(skb, tmp);
1847     				sdla_peek(&card->hw, addr, buf, tmp);
1848     				addr = card->u.p.rx_base;
1849     				len -= tmp;
1850     			}
1851     			buf = skb_put(skb, len);
1852     			sdla_peek(&card->hw, addr, buf, len);
1853     
1854     			/* Decapsulate packet */
1855             		switch (rxbuf->proto) {
1856     	
1857     				case 0x00:
1858     					skb->protocol = htons(ETH_P_IP);
1859     					break;
1860     
1861     				case 0x01:
1862     					skb->protocol = htons(ETH_P_IPX);
1863     					break;
1864     			}
1865     
1866     			udp_type = udp_pkt_type( skb, card );
1867     
1868     			if (udp_type == UDP_PTPIPE_TYPE){
1869     
1870     				/* Handle a UDP Request in Timer Interrupt */
1871     				if(store_udp_mgmt_pkt(UDP_PKT_FRM_NETWORK, card, skb, dev,
1872                     	              			ppp_priv_area)){
1873     	               			flags->imask |= PPP_INTR_TIMER;
1874     				}
1875     				++ppp_priv_area->rx_intr_stat.rx_intr_PIPE_request;
1876     
1877     
1878     			} else if (handle_IPXWAN(skb->data,card->devname, 
1879     						 ppp_priv_area->enable_IPX, 
1880     						 ppp_priv_area->network_number, 
1881     						 skb->protocol)) {
1882     			
1883     				/* Handle an IPXWAN packet */
1884     				if( ppp_priv_area->enable_IPX) {
1885     					
1886     					/* Make sure we are not already sending */
1887     					if (!test_bit(SEND_CRIT, &card->wandev.critical)){
1888     					 	ppp_send(card, skb->data, skb->len, htons(ETH_P_IPX));
1889     					}
1890     					wan_dev_kfree_skb(skb,FREE_READ);
1891     
1892     				} else {
1893     					++card->wandev.stats.rx_dropped;
1894     				}
1895     			} else {
1896     				/* Pass data up the protocol stack */
1897     	    			skb->dev = dev;
1898     				skb->mac.raw  = skb->data;
1899     
1900     			    	++card->wandev.stats.rx_packets;
1901     #if defined(LINUX_2_1) || defined(LINUX_2_4)
1902     				card->wandev.stats.rx_bytes += skb->len;
1903     #endif
1904     		    		++ppp_priv_area->rx_intr_stat.rx_intr_bfr_passed_to_stack;	
1905     				netif_rx(skb);
1906     			}
1907     
1908     		} else {
1909     	
1910     			if (net_ratelimit()){
1911     				printk(KERN_INFO "%s: no socket buffers available!\n",
1912     					card->devname);
1913     			}
1914     			++card->wandev.stats.rx_dropped;
1915     			++ppp_priv_area->rx_intr_stat.rx_intr_no_socket;
1916     		}
1917     
1918     	} else {
1919     		++card->statistics.rx_intr_dev_not_started;
1920     	}
1921     
1922     	/* Release buffer element and calculate a pointer to the next one */
1923     	rxbuf->flag = 0x00;
1924     	card->rxmb = ++rxbuf;
1925     	if ((void*)rxbuf > card->u.p.rxbuf_last)
1926     		card->rxmb = card->u.p.rxbuf_base;
1927     }
1928     
1929     
1930     void event_intr (sdla_t *card)
1931     {
1932     
1933      	netdevice_t* dev = card->wandev.dev;
1934             ppp_private_area_t* ppp_priv_area = dev->priv;
1935     	volatile ppp_flags_t *flags = card->flags;
1936     
1937     	switch (flags->iflag){
1938     
1939     		case PPP_INTR_MODEM:    /* modem status change (DCD, CTS) 0x04  (bit 2)*/
1940     
1941     			if (net_ratelimit()){
1942     				printk (KERN_INFO "%s: Modem status: DCD=%s CTS=%s\n",
1943     					card->devname, DCD(flags->mstatus), CTS(flags->mstatus));
1944     			}
1945     			break;
1946     
1947     		case PPP_INTR_DISC:  	/* Data link disconnected 0x10  (bit 4)*/	
1948     
1949     			NEX_PRINTK (KERN_INFO "Data link disconnected intr Cause %X\n",
1950     					       flags->disc_cause);
1951     
1952     			if (flags->disc_cause &
1953     				(PPP_LOCAL_TERMINATION | PPP_DCD_CTS_DROP |
1954     				PPP_REMOTE_TERMINATION)) {
1955     
1956     				if (card->u.p.ip_mode == WANOPT_PPP_PEER) { 
1957     					set_bit(0,&Read_connection_info);
1958     				}
1959     				wanpipe_set_state(card, WAN_DISCONNECTED);
1960     
1961     				show_disc_cause(card, flags->disc_cause);
1962     				ppp_priv_area->timer_int_enabled |= TMR_INT_ENABLED_PPP_EVENT;
1963     				flags->imask |= PPP_INTR_TIMER;
1964     				trigger_ppp_poll(dev);
1965     			}
1966     			break;
1967     
1968     		case PPP_INTR_OPEN:   	/* Data link open 0x20  (bit 5)*/
1969     
1970     			NEX_PRINTK (KERN_INFO "%s: PPP Link Open, LCP=%s IP=%s\n",
1971     					card->devname,LCP(flags->lcp_state),
1972     					IP(flags->ip_state));
1973     
1974     			if (flags->lcp_state == 0x09 && 
1975                                (flags->ip_state == 0x09 || flags->ipx_state == 0x09)){
1976     
1977                                     /* Initialize the polling timer and set the state
1978                                      * to WAN_CONNNECTED */
1979     
1980     
1981     				/* BUG FIX: When the protocol restarts, during heavy 
1982                                      * traffic, board tx buffers and driver tx buffers
1983                                      * can go out of sync.  This checks the condition
1984                                      * and if the tx buffers are out of sync, the 
1985                                      * protocols are restarted. 
1986                                      * I don't know why the board tx buffer is out
1987                                      * of sync. It could be that a packets is tx
1988                                      * while the link is down, but that is not 
1989                                      * possible. The other possiblility is that the
1990                                      * firmware doesn't reinitialize properly.
1991                                      * FIXME: A better fix should be found.
1992                                      */ 
1993     				if (detect_and_fix_tx_bug(card)){
1994     
1995     					ppp_comm_disable(card);
1996     
1997     					wanpipe_set_state(card, WAN_DISCONNECTED);
1998     
1999     					ppp_priv_area->timer_int_enabled |= 
2000     						TMR_INT_ENABLED_PPP_EVENT;
2001     					flags->imask |= PPP_INTR_TIMER;
2002     					break;	
2003     				}
2004     
2005     				card->state_tick = jiffies;
2006     				wanpipe_set_state(card, WAN_CONNECTED);
2007     
2008     				NEX_PRINTK(KERN_INFO "CON: L Tx: %lx  B Tx: %lx || L Rx %lx B Rx %lx\n",
2009     					(unsigned long)card->u.p.txbuf, *card->u.p.txbuf_next,
2010     					(unsigned long)card->rxmb, *card->u.p.rxbuf_next);
2011     
2012     				/* Tell timer interrupt that PPP event occured */
2013     				ppp_priv_area->timer_int_enabled |= TMR_INT_ENABLED_PPP_EVENT;
2014     				flags->imask |= PPP_INTR_TIMER;
2015     
2016     				/* If we are in PEER mode, we must first obtain the
2017     				 * IP information and then go into the poll routine */
2018     				if (card->u.p.ip_mode != WANOPT_PPP_PEER){	
2019     					trigger_ppp_poll(dev);
2020     				}
2021     			}
2022                        	break;
2023     
2024     		case PPP_INTR_DROP_DTR:		/* DTR drop timeout expired  0x40 bit 6 */
2025     
2026     			NEX_PRINTK(KERN_INFO "DTR Drop Timeout Interrrupt \n"); 
2027     
2028     			if (card->u.p.ip_mode == WANOPT_PPP_PEER) { 
2029     				set_bit(0,&Read_connection_info);
2030     			}
2031     		
2032     			wanpipe_set_state(card, WAN_DISCONNECTED);
2033     
2034     			show_disc_cause(card, flags->disc_cause);
2035     			ppp_priv_area->timer_int_enabled |= TMR_INT_ENABLED_PPP_EVENT;
2036     			flags->imask |= PPP_INTR_TIMER;
2037     			trigger_ppp_poll(dev);
2038     			break;
2039     		
2040     		default:
2041     			printk(KERN_INFO "%s: Error, Invalid PPP Event\n",card->devname);
2042     	}
2043     }
2044     
2045     
2046     
2047     /* TIMER INTERRUPT */
2048     
2049     void timer_intr (sdla_t *card)
2050     {
2051     
2052             netdevice_t* dev = card->wandev.dev;
2053             ppp_private_area_t* ppp_priv_area = dev->priv;
2054     	ppp_flags_t *flags = card->flags;
2055     
2056     
2057     	if (ppp_priv_area->timer_int_enabled & TMR_INT_ENABLED_CONFIG){
2058     		if (!config_ppp(card)){
2059     			ppp_priv_area->timer_int_enabled &= 
2060     					~TMR_INT_ENABLED_CONFIG;	
2061     		}
2062     	}
2063     
2064     	/* Update statistics */
2065     	if (ppp_priv_area->timer_int_enabled & TMR_INT_ENABLED_UPDATE){
2066     		ppp_get_err_stats(card);
2067                     if(!(--ppp_priv_area->update_comms_stats)){
2068     			ppp_priv_area->timer_int_enabled &= 
2069     				~TMR_INT_ENABLED_UPDATE;
2070     		}
2071     	}
2072     
2073     	/* PPIPEMON UDP request */
2074     
2075     	if (ppp_priv_area->timer_int_enabled & TMR_INT_ENABLED_UDP){
2076     		process_udp_mgmt_pkt(card,dev, ppp_priv_area);
2077     		ppp_priv_area->timer_int_enabled &= ~TMR_INT_ENABLED_UDP;
2078     	}
2079     
2080     	/* PPP Event */
2081     	if (ppp_priv_area->timer_int_enabled & TMR_INT_ENABLED_PPP_EVENT){
2082     
2083     		if (card->wandev.state == WAN_DISCONNECTED){
2084     			retrigger_comm(card);
2085     		}
2086     
2087     		/* If the state is CONNECTING, it means that communicatins were
2088     	 	 * enabled. When the remote side enables its comminication we
2089     	 	 * should get an interrupt PPP_INTR_OPEN, thus turn off polling 
2090     		 */
2091     
2092     		else if (card->wandev.state == WAN_CONNECTING){
2093     			/* Turn off the timer interrupt */
2094     			ppp_priv_area->timer_int_enabled &= ~TMR_INT_ENABLED_PPP_EVENT;
2095     		}
2096     
2097     		/* If state is connected and we are in PEER mode 
2098     	 	 * poll for an IP address which will be provided by remote end.
2099     	 	 */
2100     		else if ((card->wandev.state == WAN_CONNECTED && 
2101     		  	  card->u.p.ip_mode == WANOPT_PPP_PEER) && 
2102     		  	  test_bit(0,&Read_connection_info)){
2103     
2104     			card->state_tick = jiffies;
2105     			if (read_connection_info (card)){
2106     				printk(KERN_INFO "%s: Failed to read PEER IP Addresses\n",
2107     					card->devname);
2108     			}else{
2109     				clear_bit(0,&Read_connection_info);
2110     				set_bit(1,&Read_connection_info);
2111     				trigger_ppp_poll(dev);
2112     			}
2113     		}else{
2114     			//FIXME Put the comment back int
2115     			ppp_priv_area->timer_int_enabled &= ~TMR_INT_ENABLED_PPP_EVENT;
2116     		}
2117     
2118     	}/* End of PPP_EVENT */
2119     
2120     
2121     	/* Only disable the timer interrupt if there are no udp, statistic */
2122     	/* updates or events pending */
2123             if(!ppp_priv_area->timer_int_enabled) {
2124                     flags->imask &= ~PPP_INTR_TIMER;
2125             }
2126     }
2127     
2128     
2129     static int handle_IPXWAN(unsigned char *sendpacket, char *devname, unsigned char enable_IPX, unsigned long network_number, unsigned short proto)
2130     {
2131     	int i;
2132     
2133     	if( proto == htons(ETH_P_IPX) ) {
2134     		//It's an IPX packet
2135     		if(!enable_IPX) {
2136     			//Return 1 so we don't pass it up the stack.
2137     			return 1;
2138     		}
2139     	} else {
2140     		//It's not IPX so pass it up the stack.
2141     		return 0;
2142     	}
2143     
2144     	if( sendpacket[16] == 0x90 &&
2145     	    sendpacket[17] == 0x04)
2146     	{
2147     		//It's IPXWAN
2148     
2149     		if( sendpacket[2] == 0x02 &&
2150     		    sendpacket[34] == 0x00)
2151     		{
2152     			//It's a timer request packet
2153     			printk(KERN_INFO "%s: Received IPXWAN Timer Request packet\n",devname);
2154     
2155     			//Go through the routing options and answer no to every
2156     			//option except Unnumbered RIP/SAP
2157     			for(i = 41; sendpacket[i] == 0x00; i += 5)
2158     			{
2159     				//0x02 is the option for Unnumbered RIP/SAP
2160     				if( sendpacket[i + 4] != 0x02)
2161     				{
2162     					sendpacket[i + 1] = 0;
2163     				}
2164     			}
2165     
2166     			//Skip over the extended Node ID option
2167     			if( sendpacket[i] == 0x04 )
2168     			{
2169     				i += 8;
2170     			}
2171     
2172     			//We also want to turn off all header compression opt.
2173     			for(; sendpacket[i] == 0x80 ;)
2174     			{
2175     				sendpacket[i + 1] = 0;
2176     				i += (sendpacket[i + 2] << 8) + (sendpacket[i + 3]) + 4;
2177     			}
2178     
2179     			//Set the packet type to timer response
2180     			sendpacket[34] = 0x01;
2181     
2182     			printk(KERN_INFO "%s: Sending IPXWAN Timer Response\n",devname);
2183     		}
2184     		else if( sendpacket[34] == 0x02 )
2185     		{
2186     			//This is an information request packet
2187     			printk(KERN_INFO "%s: Received IPXWAN Information Request packet\n",devname);
2188     
2189     			//Set the packet type to information response
2190     			sendpacket[34] = 0x03;
2191     
2192     			//Set the router name
2193     			sendpacket[51] = 'P';
2194     			sendpacket[52] = 'T';
2195     			sendpacket[53] = 'P';
2196     			sendpacket[54] = 'I';
2197     			sendpacket[55] = 'P';
2198     			sendpacket[56] = 'E';
2199     			sendpacket[57] = '-';
2200     			sendpacket[58] = CVHexToAscii(network_number >> 28);
2201     			sendpacket[59] = CVHexToAscii((network_number & 0x0F000000)>> 24);
2202     			sendpacket[60] = CVHexToAscii((network_number & 0x00F00000)>> 20);
2203     			sendpacket[61] = CVHexToAscii((network_number & 0x000F0000)>> 16);
2204     			sendpacket[62] = CVHexToAscii((network_number & 0x0000F000)>> 12);
2205     			sendpacket[63] = CVHexToAscii((network_number & 0x00000F00)>> 8);
2206     			sendpacket[64] = CVHexToAscii((network_number & 0x000000F0)>> 4);
2207     			sendpacket[65] = CVHexToAscii(network_number & 0x0000000F);
2208     			for(i = 66; i < 99; i+= 1)
2209     			{
2210     				sendpacket[i] = 0;
2211     			}
2212     
2213     			printk(KERN_INFO "%s: Sending IPXWAN Information Response packet\n",devname);
2214     		}
2215     		else
2216     		{
2217     			printk(KERN_INFO "%s: Unknown IPXWAN packet!\n",devname);
2218     			return 0;
2219     		}
2220     
2221     		//Set the WNodeID to our network address
2222     		sendpacket[35] = (unsigned char)(network_number >> 24);
2223     		sendpacket[36] = (unsigned char)((network_number & 0x00FF0000) >> 16);
2224     		sendpacket[37] = (unsigned char)((network_number & 0x0000FF00) >> 8);
2225     		sendpacket[38] = (unsigned char)(network_number & 0x000000FF);
2226     
2227     		return 1;
2228     	} else {
2229     		//If we get here's its an IPX-data packet, so it'll get passed up the stack.
2230     
2231     		//switch the network numbers
2232     		switch_net_numbers(sendpacket, network_number, 1);	
2233     		return 0;
2234     	}
2235     }
2236     
2237     /****** Background Polling Routines  ****************************************/
2238     
2239     /* All polling functions are invoked by the TIMER interrupt in the wpp_isr 
2240      * routine.  
2241      */
2242     
2243     /*============================================================================
2244      * Monitor active link phase.
2245      */
2246     static void process_route (sdla_t *card)
2247     {
2248     	ppp_flags_t *flags = card->flags;
2249     	netdevice_t *dev = card->wandev.dev;
2250     	ppp_private_area_t *ppp_priv_area = dev->priv;
2251     	
2252     #if defined(LINUX_2_1) || defined(LINUX_2_4)
2253     
2254     	if ((card->u.p.ip_mode == WANOPT_PPP_PEER) &&
2255     	    (flags->ip_state == 0x09)){ 
2256     
2257     		/* We get ip_local from the firmware in PEER mode.
2258     	         * Therefore, if ip_local is 0, we failed to obtain
2259              	 * the remote IP address. */
2260     		if (ppp_priv_area->ip_local == 0) 
2261     			return;
2262     		
2263     		printk(KERN_INFO "%s: IPCP State Opened.\n", card->devname);
2264     		if (read_info( card )) {
2265        			printk(KERN_INFO 
2266     				"%s: An error occurred in IP assignment.\n", 
2267     				card->devname);
2268     		} else {
2269     			struct in_device *in_dev = dev->ip_ptr;
2270     			if (in_dev != NULL ) {
2271     				struct in_ifaddr *ifa = in_dev->ifa_list;
2272     				printk(KERN_INFO "%s: Assigned Lcl. Addr: %s\n", 
2273     					card->devname, in_ntoa(ifa->ifa_local));
2274     				printk(KERN_INFO "%s: Assigned Rmt. Addr: %s\n", 
2275     						card->devname, in_ntoa(ifa->ifa_address));
2276     			}else{
2277     				printk(KERN_INFO 
2278     				"%s: Error: Failed to add a route for PPP interface %s\n",
2279     					card->devname,dev->name);	
2280     			}
2281     		}
2282     	}
2283     #else
2284     
2285     	if ((card->u.p.ip_mode == WANOPT_PPP_PEER) &&
2286     	    (flags->ip_state == 0x09)){ 
2287     	
2288     		if (ppp_priv_area->ip_local == 0)
2289     			return;
2290     	
2291     		printk(KERN_INFO "%s: IPCP State Opened.\n", card->devname);
2292     		if (read_info( card )) {
2293     			printk(KERN_INFO 
2294     				"%s: An error occurred in IP assignment.\n", 
2295     				card->devname);
2296     		} else {
2297     			printk(KERN_INFO "%s: Assigned Lcl. Addr: %s\n", 
2298     					card->devname, in_ntoa(dev->pa_addr));
2299     			printk(KERN_INFO "%s: Assigned Rmt. Addr: %s\n", 
2300     					card->devname, in_ntoa(dev->pa_dstaddr));
2301     		}
2302     	}
2303     
2304     #endif
2305     
2306     }
2307     
2308     /*============================================================================
2309      * Monitor physical link disconnected phase.
2310      *  o if interface is up and the hold-down timeout has expired, then retry
2311      *    connection.
2312      */
2313     static void retrigger_comm(sdla_t *card)
2314     {
2315     	netdevice_t *dev = card->wandev.dev;
2316     
2317     	if (dev && ((jiffies - card->state_tick) > HOLD_DOWN_TIME)) {
2318     
2319     		wanpipe_set_state(card, WAN_CONNECTING);
2320     
2321     		if(ppp_comm_enable(card) == CMD_OK){
2322     			init_ppp_tx_rx_buff( card );
2323     		}	         
2324     	}
2325     }
2326     
2327     /****** Miscellaneous Functions *********************************************/
2328     
2329     /*============================================================================
2330      * Configure S508 adapter.
2331      */
2332     static int config508(netdevice_t *dev, sdla_t *card)
2333     {
2334     	ppp508_conf_t cfg;
2335     #if defined(LINUX_2_1) || defined(LINUX_2_4)
2336     	struct in_device *in_dev = dev->ip_ptr;
2337     #endif
2338     	ppp_private_area_t *ppp_priv_area = dev->priv;
2339     
2340     	/* Prepare PPP configuration structure */
2341     	memset(&cfg, 0, sizeof(ppp508_conf_t));
2342     
2343     	if (card->wandev.clocking)
2344     		cfg.line_speed = card->wandev.bps;
2345     
2346     	if (card->wandev.interface == WANOPT_RS232)
2347     		cfg.conf_flags |= INTERFACE_LEVEL_RS232;
2348     
2349     
2350             cfg.conf_flags 	|= DONT_TERMINATE_LNK_MAX_CONFIG; /*send Configure-Request packets forever*/
2351     	cfg.txbuf_percent	= PERCENT_TX_BUFF;	/* % of Tx bufs */
2352     	cfg.mtu_local		= card->wandev.mtu;
2353     	cfg.mtu_remote		= card->wandev.mtu;                  /*    Default   */
2354     	cfg.restart_tmr		= TIME_BETWEEN_CONF_REQ;  	     /*    30 = 3sec */
2355     	cfg.auth_rsrt_tmr	= TIME_BETWEEN_PAP_CHAP_REQ;         /*    30 = 3sec */
2356     	cfg.auth_wait_tmr	= WAIT_PAP_CHAP_WITHOUT_REPLY;       /*   300 = 30s  */
2357     	cfg.mdm_fail_tmr	= WAIT_AFTER_DCD_CTS_LOW;            /*     5 = 0.5s */
2358     	cfg.dtr_drop_tmr	= TIME_DCD_CTS_LOW_AFTER_LNK_DOWN;   /*    10 = 1s   */
2359     	cfg.connect_tmout	= WAIT_DCD_HIGH_AFTER_ENABLE_COMM;   /*   900 = 90s  */
2360     	cfg.conf_retry		= MAX_CONF_REQ_WITHOUT_REPLY;        /*    10 = 1s   */
2361     	cfg.term_retry		= MAX_TERM_REQ_WITHOUT_REPLY;	     /*     2 times  */
2362     	cfg.fail_retry		= NUM_CONF_NAK_WITHOUT_REPLY;        /*     5 times  */
2363     	cfg.auth_retry		= NUM_AUTH_REQ_WITHOUT_REPLY;        /*     10 times */   
2364     
2365     
2366     	if( !card->u.p.authenticator ) {
2367     		printk(KERN_INFO "%s: Device is not configured as an authenticator\n", 
2368     				card->devname);
2369     		cfg.auth_options = NO_AUTHENTICATION;
2370     	}else{
2371     		printk(KERN_INFO "%s: Device is configured as an authenticator\n", 
2372     				card->devname);
2373     		cfg.auth_options = INBOUND_AUTH;
2374     	}
2375     
2376     	if( ppp_priv_area->pap == WANOPT_YES){
2377     		cfg.auth_options |=PAP_AUTH;
2378     		printk(KERN_INFO "%s: Pap enabled\n", card->devname);
2379     	}
2380     	if( ppp_priv_area->chap == WANOPT_YES){
2381     		cfg.auth_options |= CHAP_AUTH;
2382     		printk(KERN_INFO "%s: Chap enabled\n", card->devname);
2383     	}
2384     
2385     
2386     	if (ppp_priv_area->enable_IPX == WANOPT_YES){
2387     		printk(KERN_INFO "%s: Enabling IPX Protocol\n",card->devname);
2388     		cfg.ipx_options		= ENABLE_IPX | ROUTING_PROT_DEFAULT;
2389     	}else{
2390     		cfg.ipx_options 	= DISABLE_IPX;
2391     	}
2392     
2393     	switch (card->u.p.ip_mode) {
2394     	
2395     		case WANOPT_PPP_STATIC:
2396     
2397     			printk(KERN_INFO "%s: PPP IP Mode: STATIC\n",card->devname);
2398     			cfg.ip_options		= L_AND_R_IP_NO_ASSIG | 
2399     							    ENABLE_IP;
2400     #if defined(LINUX_2_1) || defined(LINUX_2_4)
2401     			cfg.ip_local		= in_dev->ifa_list->ifa_local;
2402     			cfg.ip_remote		= in_dev->ifa_list->ifa_address;
2403     #else
2404     			cfg.ip_local		= dev->pa_addr;
2405     			cfg.ip_remote		= dev->pa_dstaddr;
2406     #endif
2407     
2408     			/* Debugging code used to check that IP addresses
2409                              * obtained from the kernel are correct */
2410     
2411     			{
2412     			char laddr[20];
2413     			char raddr[20];
2414     			strcpy(laddr,in_ntoa(cfg.ip_local));
2415     			strcpy(raddr,in_ntoa(cfg.ip_remote));
2416     
2417                             NEX_PRINTK(KERN_INFO "Local %s Remote %s Name %s\n",
2418     					laddr,raddr, dev->name);
2419     			
2420     			}	
2421     			break;
2422     
2423     		case WANOPT_PPP_HOST:
2424     
2425     			printk(KERN_INFO "%s: PPP IP Mode: HOST\n",card->devname);
2426     			cfg.ip_options		= L_IP_LOCAL_ASSIG |
2427     						  R_IP_LOCAL_ASSIG | 
2428     						  ENABLE_IP;
2429     #if defined(LINUX_2_1) || defined(LINUX_2_4)
2430     			cfg.ip_local		= in_dev->ifa_list->ifa_local;
2431     			cfg.ip_remote		= in_dev->ifa_list->ifa_address;
2432     #else
2433     			cfg.ip_local		= dev->pa_addr;
2434     			cfg.ip_remote		= dev->pa_dstaddr;
2435     #endif
2436     
2437     			/* Debugging code used to check that IP addresses
2438                              * obtained from the kernel are correct */
2439     			{
2440     			char laddr[20];
2441     			char raddr[20];
2442     			strcpy(laddr,in_ntoa(cfg.ip_local));
2443     			strcpy(raddr,in_ntoa(cfg.ip_remote));
2444     
2445                             NEX_PRINTK (KERN_INFO "Local %s Remote %s Name %s\n",
2446     					laddr,raddr, dev->name);
2447     			
2448     			}	
2449     
2450     
2451     			break;
2452     	
2453     		case WANOPT_PPP_PEER:
2454     
2455     #if defined(LINUX_2_1) || defined(LINUX_2_4)
2456     
2457     			printk(KERN_INFO "%s: PPP IP Mode: PEER\n",card->devname);
2458     			cfg.ip_options		= L_IP_REMOTE_ASSIG | 
2459     						  R_IP_REMOTE_ASSIG | 
2460     							  ENABLE_IP;
2461     			cfg.ip_local		= 0x00;
2462     			cfg.ip_remote		= 0x00;
2463     			break;
2464     
2465     #else
2466     
2467     			/* No PEER support for 2.0.X kernels, drop down to default
2468        			 * condition */
2469     
2470     			printk(KERN_INFO "%s: ERROR, PEER mode is not supported in 2.0.X kernels\n",
2471     						card->devname);
2472     			
2473     #endif
2474     
2475     		default:
2476     			printk(KERN_INFO "%s: ERROR: Unsuported PPP Mode Selected\n",
2477     					card->devname);
2478     			printk(KERN_INFO "%s:        PPP IP Modes: STATIC, PEER or HOST\n",
2479     					card->devname);	
2480     			return 1;
2481     	}
2482     
2483     	return ppp_configure(card, &cfg);
2484     }
2485     
2486     /*============================================================================
2487      * Show disconnection cause.
2488      */
2489     static void show_disc_cause(sdla_t *card, unsigned cause)
2490     {
2491     	if (cause & 0x0802) 
2492     
2493     		printk(KERN_INFO "%s: link terminated by peer\n", 
2494     			card->devname);
2495     
2496     	else if (cause & 0x0004) 
2497     
2498     		printk(KERN_INFO "%s: link terminated by user\n", 
2499     			card->devname);
2500     
2501     	else if (cause & 0x0008) 
2502     
2503     		printk(KERN_INFO "%s: authentication failed\n", card->devname);
2504     	
2505     	else if (cause & 0x0010) 
2506     
2507     		printk(KERN_INFO 
2508     			"%s: authentication protocol negotiation failed\n", 
2509     			card->devname);
2510     
2511     	else if (cause & 0x0020) 
2512     		
2513     		printk(KERN_INFO
2514     		"%s: peer's request for authentication rejected\n",
2515     		card->devname);
2516     
2517     	else if (cause & 0x0040) 
2518     	
2519     		printk(KERN_INFO "%s: MRU option rejected by peer\n", 
2520     		card->devname);
2521     
2522     	else if (cause & 0x0080) 
2523     	
2524     		printk(KERN_INFO "%s: peer's MRU was too small\n", 
2525     		card->devname);
2526     
2527     	else if (cause & 0x0100) 
2528     
2529     		printk(KERN_INFO "%s: failed to negotiate peer's LCP options\n",
2530     		card->devname);
2531     
2532     	else if (cause & 0x0200) 
2533     		
2534     		printk(KERN_INFO "%s: failed to negotiate peer's IPCP options\n"
2535     		, card->devname);
2536     
2537     	else if (cause & 0x0400) 
2538     
2539     		printk(KERN_INFO 
2540     			"%s: failed to negotiate peer's IPXCP options\n",
2541     			card->devname);
2542     }
2543     
2544     /*=============================================================================
2545      * Process UDP call of type PTPIPEAB.
2546      */
2547     static void process_udp_mgmt_pkt(sdla_t *card, netdevice_t *dev, 
2548     				 ppp_private_area_t *ppp_priv_area ) 
2549     {
2550     	unsigned char buf2[5];
2551     	unsigned char *buf;
2552     	unsigned int frames, len;
2553     	struct sk_buff *new_skb;
2554     	unsigned short data_length, buffer_length, real_len;
2555     	unsigned long data_ptr;
2556     	int udp_mgmt_req_valid = 1;
2557     	ppp_mbox_t *mbox = card->mbox;
2558     	struct timeval tv;
2559     	int err;
2560     	ppp_udp_pkt_t *ppp_udp_pkt = (ppp_udp_pkt_t*)&ppp_priv_area->udp_pkt_data;
2561     
2562     	memcpy(&buf2, &card->wandev.udp_port, 2 );
2563     
2564     
2565     	if(ppp_priv_area->udp_pkt_src == UDP_PKT_FRM_NETWORK) {
2566     
2567     		switch(ppp_udp_pkt->cblock.command) {
2568     
2569     			case PPIPE_GET_IBA_DATA:
2570     			case PPP_READ_CONFIG:
2571     			case PPP_GET_CONNECTION_INFO:
2572     			case PPIPE_ROUTER_UP_TIME:
2573     			case PPP_READ_STATISTICS:
2574     			case PPP_READ_ERROR_STATS:
2575     			case PPP_READ_PACKET_STATS:
2576     			case PPP_READ_LCP_STATS:
2577     			case PPP_READ_IPCP_STATS:
2578     			case PPP_READ_IPXCP_STATS:
2579     			case PPP_READ_PAP_STATS:
2580     			case PPP_READ_CHAP_STATS:
2581     			case PPP_READ_CODE_VERSION:
2582     				udp_mgmt_req_valid = 1;
2583     				break;
2584     			   
2585     			default:
2586     				udp_mgmt_req_valid = 0;
2587     				break;
2588     		} 
2589     	}
2590     	
2591       	if(!udp_mgmt_req_valid) {
2592     	    
2593     		/* set length to 0 */
2594         		ppp_udp_pkt->cblock.length = 0x00;
2595     
2596         		/* set return code */
2597         		ppp_udp_pkt->cblock.result = 0xCD; 
2598     		++ppp_priv_area->pipe_mgmt_stat.UDP_PIPE_mgmt_direction_err;
2599     	
2600     		if (net_ratelimit()){	
2601     			printk(KERN_INFO 
2602     			"%s: Warning, Illegal UDP command attempted from network: %x\n",
2603     			card->devname,ppp_udp_pkt->cblock.command);
2604     		}
2605        	} else {
2606     		/* Initialize the trace element */
2607     		trace_element_t trace_element;		    
2608     
2609     		switch (ppp_udp_pkt->cblock.command){
2610     
2611     		/* PPIPE_ENABLE_TRACING */
2612         		case PPIPE_ENABLE_TRACING:
2613     			if (!card->TracingEnabled) {
2614         			
2615     				/* OPERATE_DATALINE_MONITOR */
2616         				mbox->cmd.command = PPP_DATALINE_MONITOR;
2617         				mbox->cmd.length = 0x01;
2618         				mbox->data[0] = ppp_udp_pkt->data[0];
2619     	    			err = sdla_exec(mbox) ? 
2620     					mbox->cmd.result : CMD_TIMEOUT;
2621     	   
2622     				if (err != CMD_OK) { 
2623     	        			
2624     					ppp_error(card, err, mbox);
2625     	        			card->TracingEnabled = 0;
2626     	        		
2627     					/* set the return code */
2628     
2629     		        		ppp_udp_pkt->cblock.result = mbox->cmd.result;
2630     	        			mbox->cmd.length = 0;
2631     	        			break;
2632     	    			} 
2633     
2634     				sdla_peek(&card->hw, 0xC000, &buf2, 2);
2635     		    
2636     				ppp_priv_area->curr_trace_addr = 0;
2637     		    		memcpy(&ppp_priv_area->curr_trace_addr, &buf2, 2);
2638     		    		ppp_priv_area->start_trace_addr = 
2639     						ppp_priv_area->curr_trace_addr;
2640     				ppp_priv_area->end_trace_addr = 
2641     					ppp_priv_area->start_trace_addr + END_OFFSET;
2642     		    	
2643     				/* MAX_SEND_BUFFER_SIZE - 28 (IP header) 
2644     				   - 32 (ppipemon CBLOCK) */
2645     		    		available_buffer_space = MAX_LGTH_UDP_MGNT_PKT - 
2646     							 sizeof(ip_pkt_t)-
2647     							 sizeof(udp_pkt_t)-
2648     							 sizeof(wp_mgmt_t)-
2649     							 sizeof(cblock_t);
2650     	       	  	}
2651     	       	  	ppp_udp_pkt->cblock.result = 0;
2652     	       	  	mbox->cmd.length = 0;
2653     	       	  	card->TracingEnabled = 1;
2654     	       	  	break;
2655     	   
2656     		/* PPIPE_DISABLE_TRACING */
2657     		case PPIPE_DISABLE_TRACING:
2658     	      		
2659     			if(card->TracingEnabled) {
2660     		   	
2661     				/* OPERATE_DATALINE_MONITOR */
2662     		    		mbox->cmd.command = 0x33;
2663     		    		mbox->cmd.length = 1;
2664     		    		mbox->data[0] = 0x00;
2665     		    		err = sdla_exec(mbox) ? 
2666     					mbox->cmd.result : CMD_TIMEOUT;
2667     	       	  
2668     			} 
2669     		
2670     			/*set return code*/
2671     			ppp_udp_pkt->cblock.result = 0;
2672     			mbox->cmd.length = 0;
2673     			card->TracingEnabled = 0;
2674     			break;
2675     	   
2676     		/* PPIPE_GET_TRACE_INFO */
2677     		case PPIPE_GET_TRACE_INFO:
2678     
2679     			if(!card->TracingEnabled) {
2680     				/* set return code */
2681     	    			ppp_udp_pkt->cblock.result = 1;
2682     	    			mbox->cmd.length = 0;
2683     			}		    
2684     
2685     			buffer_length = 0;
2686     			
2687     			/* frames < 62, where 62 is the number of trace
2688     			   information elements.  There is in total 496
2689     			   bytes of space and each trace information
2690     			   element is 8 bytes. 
2691     			 */
2692     			for ( frames=0; frames<62; frames++) {
2693     	
2694     				trace_pkt_t *trace_pkt = (trace_pkt_t *)
2695     					&ppp_udp_pkt->data[buffer_length];
2696     	
2697     				/* Read the whole trace packet */
2698     				sdla_peek(&card->hw, ppp_priv_area->curr_trace_addr, 
2699     					  &trace_element, sizeof(trace_element_t));
2700     	
2701     				/* no data on board so exit */
2702     				if( trace_element.opp_flag == 0x00 ) 
2703     					break;
2704     	      
2705     				data_ptr = trace_element.trace_data_ptr;
2706     
2707     				/* See if there is actual data on the trace buffer */
2708     				if (data_ptr){
2709     					data_length = trace_element.trace_length;
2710     				}else{
2711     					data_length = 0;
2712     					ppp_udp_pkt->data[0] |= 0x02;
2713     				}
2714     
2715     				//FIXME: Do we need this check
2716     				if ((available_buffer_space - buffer_length) 
2717     				     < (sizeof(trace_element_t)+1)){
2718     					
2719     					/*indicate we have more frames 
2720     					 * on board and exit 
2721     					 */
2722     					ppp_udp_pkt->data[0] |= 0x02;
2723     					break;
2724     				}
2725     				
2726     				trace_pkt->status = trace_element.trace_type;
2727     				trace_pkt->time_stamp = trace_element.trace_time_stamp;
2728     				trace_pkt->real_length = trace_element.trace_length;
2729     
2730     				real_len = trace_element.trace_length;	
2731     				
2732     				if(data_ptr == 0){
2733     					trace_pkt->data_avail = 0x00;
2734     				}else{
2735     					/* we can take it next time */
2736     					if ((available_buffer_space - buffer_length)<
2737     						(real_len + sizeof(trace_pkt_t))){
2738     					
2739     						ppp_udp_pkt->data[0] |= 0x02;
2740     						break;
2741     					} 
2742     					trace_pkt->data_avail = 0x01;
2743     				
2744     					/* get the data */
2745     					sdla_peek(&card->hw, data_ptr, 
2746     						  &trace_pkt->data,
2747     						  real_len);
2748     				}	
2749     				/* zero the opp flag to 
2750     				   show we got the frame */
2751     				buf2[0] = 0x00;
2752     				sdla_poke(&card->hw, ppp_priv_area->curr_trace_addr,
2753     					  &buf2, 1);
2754     
2755     				/* now move onto the next 
2756     				   frame */
2757     				ppp_priv_area->curr_trace_addr += 8;
2758     
2759     				/* check if we passed the last address */
2760     				if ( ppp_priv_area->curr_trace_addr >= 
2761     					ppp_priv_area->end_trace_addr){
2762     
2763     					ppp_priv_area->curr_trace_addr = 
2764     						ppp_priv_area->start_trace_addr;
2765     				}
2766      
2767     				/* update buffer length and make sure its even */ 
2768     
2769     				if ( trace_pkt->data_avail == 0x01 ) {
2770     					buffer_length += real_len - 1;
2771     				}
2772      
2773     				/* for the header */
2774     				buffer_length += 8;
2775     
2776     				if( buffer_length & 0x0001 )
2777     					buffer_length += 1;
2778     			}
2779     
2780     			/* ok now set the total number of frames passed
2781     			   in the high 5 bits */
2782     			ppp_udp_pkt->data[0] |= (frames << 2);
2783     	 
2784     			/* set the data length */
2785     			mbox->cmd.length = buffer_length;
2786     			ppp_udp_pkt->cblock.length = buffer_length;
2787     	 
2788     			/* set return code */
2789     			ppp_udp_pkt->cblock.result = 0;
2790     	      	  	break;
2791     
2792        		/* PPIPE_GET_IBA_DATA */
2793     		case PPIPE_GET_IBA_DATA:
2794     	        
2795     			mbox->cmd.length = 0x09;
2796     		
2797     			sdla_peek(&card->hw, 0xF003, &ppp_udp_pkt->data, 
2798     					mbox->cmd.length);
2799     	        
2800     			/* set the length of the data */
2801     			ppp_udp_pkt->cblock.length = 0x09;
2802     
2803     			/* set return code */
2804     			ppp_udp_pkt->cblock.result = 0x00;
2805     			ppp_udp_pkt->cblock.result = 0;
2806     			break;
2807     
2808     		/* PPIPE_FT1_READ_STATUS */
2809     		case PPIPE_FT1_READ_STATUS:
2810     			sdla_peek(&card->hw, 0xF020, &ppp_udp_pkt->data[0], 2);
2811     			ppp_udp_pkt->cblock.length = mbox->cmd.length = 2;
2812     			ppp_udp_pkt->cblock.result = 0;
2813     			break;
2814     		
2815     		case PPIPE_FLUSH_DRIVER_STATS:   
2816     			init_ppp_priv_struct( ppp_priv_area );
2817     			init_global_statistics( card );
2818     			mbox->cmd.length = 0;
2819     			ppp_udp_pkt->cblock.result = 0;
2820     			break;
2821     
2822     		
2823     		case PPIPE_ROUTER_UP_TIME:
2824     
2825     			do_gettimeofday( &tv );
2826     			ppp_priv_area->router_up_time = tv.tv_sec - 
2827     					ppp_priv_area->router_start_time;
2828     			*(unsigned long *)&ppp_udp_pkt->data = ppp_priv_area->router_up_time;
2829     			mbox->cmd.length = 4;
2830     			ppp_udp_pkt->cblock.result = 0;
2831     			break;
2832     
2833     				/* PPIPE_DRIVER_STATISTICS */   
2834     		case PPIPE_DRIVER_STAT_IFSEND:
2835     			memcpy(&ppp_udp_pkt->data, &ppp_priv_area->if_send_stat, 
2836     				sizeof(if_send_stat_t));
2837     
2838     
2839     			ppp_udp_pkt->cblock.result = 0;
2840     			ppp_udp_pkt->cblock.length = sizeof(if_send_stat_t);
2841     			mbox->cmd.length = sizeof(if_send_stat_t);	
2842     			break;
2843     
2844     		case PPIPE_DRIVER_STAT_INTR:
2845     			memcpy(&ppp_udp_pkt->data, &card->statistics, 
2846     				sizeof(global_stats_t));
2847     
2848     			memcpy(&ppp_udp_pkt->data+sizeof(global_stats_t),
2849     				&ppp_priv_area->rx_intr_stat,
2850     				sizeof(rx_intr_stat_t));
2851     
2852     			ppp_udp_pkt->cblock.result = 0;
2853     			ppp_udp_pkt->cblock.length = sizeof(global_stats_t)+
2854     						     sizeof(rx_intr_stat_t);
2855     			mbox->cmd.length = ppp_udp_pkt->cblock.length;
2856     			break;
2857     
2858     		case PPIPE_DRIVER_STAT_GEN:
2859     			memcpy( &ppp_udp_pkt->data,
2860     				&ppp_priv_area->pipe_mgmt_stat,
2861     				sizeof(pipe_mgmt_stat_t));
2862     
2863     			memcpy(&ppp_udp_pkt->data+sizeof(pipe_mgmt_stat_t), 
2864     			       &card->statistics, sizeof(global_stats_t));
2865     
2866     			ppp_udp_pkt->cblock.result = 0;
2867     			ppp_udp_pkt->cblock.length = sizeof(global_stats_t)+
2868     						     sizeof(rx_intr_stat_t);
2869     			mbox->cmd.length = ppp_udp_pkt->cblock.length;
2870     			break;
2871     
2872     
2873     		/* FT1 MONITOR STATUS */
2874        		case FT1_MONITOR_STATUS_CTRL:
2875     	
2876     			/* Enable FT1 MONITOR STATUS */
2877     	        	if( ppp_udp_pkt->data[0] == 1) {
2878     			
2879     				if( rCount++ != 0 ) {
2880     		        		ppp_udp_pkt->cblock.result = 0;
2881     	          			mbox->cmd.length = 1;
2882     		  			break;
2883     		    		}	
2884     	      		}
2885     
2886     	      		/* Disable FT1 MONITOR STATUS */
2887     	      		if( ppp_udp_pkt->data[0] == 0) {
2888     
2889     	      	   		if( --rCount != 0) {
2890     		  			ppp_udp_pkt->cblock.result = 0;
2891     		  			mbox->cmd.length = 1;
2892     		  			break;
2893     	   	    		} 
2894     	      		} 	
2895     			goto udp_dflt_cmd;
2896     			
2897     		/* WARNING: FIXME: This should be fixed.
2898     		 * The FT1 Status Ctrl doesn't have a break
2899                      * statment.  Thus, no code must be inserted
2900                      * HERE: between default and above case statement */
2901     
2902     		default:
2903     udp_dflt_cmd:
2904     	        
2905     			/* it's a board command */
2906     			mbox->cmd.command = ppp_udp_pkt->cblock.command;
2907     			mbox->cmd.length = ppp_udp_pkt->cblock.length;
2908      
2909     			if(mbox->cmd.length) {
2910     				memcpy(&mbox->data,(unsigned char *)ppp_udp_pkt->data,
2911     				       mbox->cmd.length);
2912     	      		} 
2913     	          
2914     			/* run the command on the board */
2915     			err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2916     		
2917     			if (err != CMD_OK) {
2918     		
2919     		    		ppp_error(card, err, mbox);
2920     		    		++ppp_priv_area->pipe_mgmt_stat.
2921     					 UDP_PIPE_mgmt_adptr_cmnd_timeout;
2922     				break;
2923     			}
2924     	          
2925     		  	++ppp_priv_area->pipe_mgmt_stat.UDP_PIPE_mgmt_adptr_cmnd_OK;
2926     		
2927     			/* copy the result back to our buffer */
2928     			memcpy(&ppp_udp_pkt->cblock,mbox, sizeof(cblock_t));
2929     	          
2930     			if(mbox->cmd.length) {
2931     				memcpy(&ppp_udp_pkt->data,&mbox->data,mbox->cmd.length);
2932     			} 
2933     
2934     		} /* end of switch */
2935          	} /* end of else */
2936     
2937          	/* Fill UDP TTL */
2938          	ppp_udp_pkt->ip_pkt.ttl = card->wandev.ttl; 
2939          	len = reply_udp(ppp_priv_area->udp_pkt_data, mbox->cmd.length);
2940     
2941          	if (ppp_priv_area->udp_pkt_src == UDP_PKT_FRM_NETWORK) {
2942     
2943     		/* Make sure we are not already sending */
2944     		if (!test_bit(SEND_CRIT,&card->wandev.critical)){
2945     			++ppp_priv_area->pipe_mgmt_stat.UDP_PIPE_mgmt_passed_to_adptr;
2946     			ppp_send(card,ppp_priv_area->udp_pkt_data,len,ppp_priv_area->protocol);
2947     		}
2948     
2949     	} else {	
2950     	
2951     		/* Pass it up the stack
2952         		   Allocate socket buffer */
2953     		if ((new_skb = dev_alloc_skb(len)) != NULL) {
2954     	    	
2955     			/* copy data into new_skb */
2956     
2957       	    		buf = skb_put(new_skb, len);
2958       	    		memcpy(buf,ppp_priv_area->udp_pkt_data, len);
2959     
2960     	    		++ppp_priv_area->pipe_mgmt_stat.UDP_PIPE_mgmt_passed_to_stack;
2961     			
2962                 		/* Decapsulate packet and pass it up the protocol 
2963     			   stack */
2964     	    		new_skb->protocol = htons(ETH_P_IP);
2965                 		new_skb->dev = dev;
2966     	    		new_skb->mac.raw  = new_skb->data;
2967     			netif_rx(new_skb);
2968     		
2969     		} else {
2970     	    	
2971     			++ppp_priv_area->pipe_mgmt_stat.UDP_PIPE_mgmt_no_socket;
2972     			printk(KERN_INFO "no socket buffers available!\n");
2973       		}
2974         	}	
2975     
2976     	ppp_priv_area->udp_pkt_lgth = 0;
2977     	
2978     	return; 
2979     }
2980     
2981     /*=============================================================================
2982      * Initial the ppp_private_area structure.
2983      */
2984     static void init_ppp_priv_struct( ppp_private_area_t *ppp_priv_area )
2985     {
2986     
2987     	memset(&ppp_priv_area->if_send_stat, 0, sizeof(if_send_stat_t));
2988     	memset(&ppp_priv_area->rx_intr_stat, 0, sizeof(rx_intr_stat_t));
2989     	memset(&ppp_priv_area->pipe_mgmt_stat, 0, sizeof(pipe_mgmt_stat_t));	
2990     }
2991     
2992     /*============================================================================
2993      * Initialize Global Statistics
2994      */
2995     static void init_global_statistics( sdla_t *card )
2996     {
2997     	memset(&card->statistics, 0, sizeof(global_stats_t));
2998     }
2999     
3000     /*============================================================================
3001      * Initialize Receive and Transmit Buffers.
3002      */
3003     static void init_ppp_tx_rx_buff( sdla_t *card )
3004     {
3005     	ppp508_buf_info_t* info;
3006     
3007     	if (card->hw.type == SDLA_S514) {
3008     		
3009     		info = (void*)(card->hw.dpmbase + PPP514_BUF_OFFS);
3010     
3011            		card->u.p.txbuf_base = (void*)(card->hw.dpmbase +
3012     			info->txb_ptr);
3013     
3014                     card->u.p.txbuf_last = (ppp_buf_ctl_t*)card->u.p.txbuf_base +
3015                             (info->txb_num - 1);
3016     
3017                     card->u.p.rxbuf_base = (void*)(card->hw.dpmbase +
3018                             info->rxb_ptr);
3019     
3020                     card->u.p.rxbuf_last = (ppp_buf_ctl_t*)card->u.p.rxbuf_base +
3021                             (info->rxb_num - 1);
3022     
3023     	} else {
3024     		
3025     		info = (void*)(card->hw.dpmbase + PPP508_BUF_OFFS);
3026     
3027     		card->u.p.txbuf_base = (void*)(card->hw.dpmbase +
3028     			(info->txb_ptr - PPP508_MB_VECT));
3029     
3030     		card->u.p.txbuf_last = (ppp_buf_ctl_t*)card->u.p.txbuf_base +
3031     			(info->txb_num - 1);
3032     
3033     		card->u.p.rxbuf_base = (void*)(card->hw.dpmbase +
3034     			(info->rxb_ptr - PPP508_MB_VECT));
3035     
3036     		card->u.p.rxbuf_last = (ppp_buf_ctl_t*)card->u.p.rxbuf_base +
3037     			(info->rxb_num - 1);
3038     	}
3039     
3040     	card->u.p.txbuf_next = (unsigned long*)&info->txb_nxt; 
3041     	card->u.p.rxbuf_next = (unsigned long*)&info->rxb1_ptr;
3042     
3043     	card->u.p.rx_base = info->rxb_base;
3044             card->u.p.rx_top  = info->rxb_end;
3045           
3046     	card->u.p.txbuf = card->u.p.txbuf_base;
3047     	card->rxmb = card->u.p.rxbuf_base;
3048     
3049     }
3050     
3051     /*=============================================================================
3052      * Read Connection Information (ie for Remote IP address assginment).
3053      * Called when ppp interface connected.
3054      */
3055     static int read_info( sdla_t *card )
3056     {
3057     	netdevice_t *dev = card->wandev.dev;
3058     	ppp_private_area_t *ppp_priv_area = dev->priv;
3059     	int err;
3060     
3061     #if defined(LINUX_2_1) || defined(LINUX_2_4)
3062     	struct ifreq if_info;
3063     	struct sockaddr_in *if_data1, *if_data2;
3064     	mm_segment_t fs;
3065     #else
3066     #ifdef _DYNAMIC_ROUTE_20X_SUPPORT_
3067             struct rtentry route;
3068     #endif
3069     #endif
3070     
3071     
3072     	
3073     #if defined(LINUX_2_1) || defined(LINUX_2_4)
3074     
3075     	/* Set Local and remote addresses */
3076     	memset(&if_info, 0, sizeof(if_info));
3077     	strcpy(if_info.ifr_name, dev->name);
3078     
3079     
3080     	fs = get_fs();
3081     	set_fs(get_ds());     /* get user space block */ 
3082     
3083     	/* Change the local and remote ip address of the interface.
3084     	 * This will also add in the destination route.
3085     	 */	
3086     	if_data1 = (struct sockaddr_in *)&if_info.ifr_addr;
3087     	if_data1->sin_addr.s_addr = ppp_priv_area->ip_local;
3088     	if_data1->sin_family = AF_INET;
3089     	err = devinet_ioctl( SIOCSIFADDR, &if_info );
3090     	if_data2 = (struct sockaddr_in *)&if_info.ifr_dstaddr;
3091     	if_data2->sin_addr.s_addr = ppp_priv_area->ip_remote;
3092     	if_data2->sin_family = AF_INET;
3093     	err = devinet_ioctl( SIOCSIFDSTADDR, &if_info );
3094     
3095     	set_fs(fs);           /* restore old block */
3096     	
3097     #else
3098     	/* FIXME: Dynamic Routing in 2.0.X kernels is not
3099              * supported. Sorry ! I'll come back to it when I get
3100              * a chance. */
3101     	
3102     	printk(KERN_INFO "%s: ERROR, Dynamic routing is not supported in 2.0.X kernels\n",
3103     				card->devname);
3104     	printk(KERN_INFO "%s:        Please use the STATIC IP mode!\n",
3105     				card->devname);
3106     
3107     	err = 0;
3108     
3109     #ifdef _DYNAMIC_ROUTE_20X_SUPPORT_
3110     	dev->pa_dstaddr = ppp_priv_area->ip_remote;
3111     	dev->pa_addr = ppp_priv_area->ip_local;
3112     
3113     	memset(&route, 0, sizeof(route));
3114     	route.rt_dev = dev->name;
3115     	route.rt_flags = 0;
3116     	((struct sockaddr_in *)&(route.rt_dst))->sin_addr.s_addr =
3117     		dev->pa_dstaddr;
3118     	((struct sockaddr_in *)&(route.rt_dst))->sin_family = AF_INET;
3119     	((struct sockaddr_in *)&(route.rt_genmask))->sin_addr.s_addr =
3120     			0xFFFFFFFF;
3121     	((struct sockaddr_in *)&(route.rt_genmask))->sin_family =
3122     			AF_INET;
3123     	
3124     	err = ip_rt_new(&route);
3125     
3126     #endif
3127     
3128     #endif
3129     
3130     	if (err) {
3131     		printk (KERN_INFO "%s: Adding of route failed: %i\n",
3132     			card->devname,err);
3133     		printk (KERN_INFO "%s:	Local : %s\n",
3134     			card->devname,in_ntoa(ppp_priv_area->ip_local));
3135     		printk (KERN_INFO "%s:	Remote: %s\n",
3136     			card->devname,in_ntoa(ppp_priv_area->ip_remote));
3137     	}
3138     	return err;
3139     }
3140     
3141     /*=============================================================================
3142      * Remove Dynamic Route.
3143      * Called when ppp interface disconnected.
3144      */
3145     
3146     static void remove_route( sdla_t *card )
3147     {
3148     
3149     	netdevice_t *dev = card->wandev.dev;
3150     	long ip_addr;
3151     	int err;
3152     
3153     #if defined(LINUX_2_1) || defined(LINUX_2_4)
3154             mm_segment_t fs;
3155     	struct ifreq if_info;
3156     	struct sockaddr_in *if_data1;
3157             struct in_device *in_dev = dev->ip_ptr;
3158             struct in_ifaddr *ifa = in_dev->ifa_list;	
3159     #else
3160     	unsigned long fs = 0;
3161             struct rtentry route;
3162     #endif
3163     
3164     
3165     #if defined(LINUX_2_1) || defined(LINUX_2_4)
3166     	ip_addr = ifa->ifa_local;
3167     
3168     	/* Set Local and remote addresses */
3169     	memset(&if_info, 0, sizeof(if_info));
3170     	strcpy(if_info.ifr_name, dev->name);
3171     #endif
3172     
3173     
3174     	fs = get_fs();
3175            	set_fs(get_ds());     /* get user space block */ 
3176     
3177     
3178     #if defined(LINUX_2_1) || defined(LINUX_2_4)	
3179     	/* Change the local ip address of the interface to 0.
3180     	 * This will also delete the destination route.
3181     	 */	
3182     	if_data1 = (struct sockaddr_in *)&if_info.ifr_addr;
3183     	if_data1->sin_addr.s_addr = 0;
3184     	if_data1->sin_family = AF_INET;
3185     	err = devinet_ioctl( SIOCSIFADDR, &if_info );
3186     #else
3187     
3188     	ip_addr = dev->pa_addr;
3189     	dev->pa_dstaddr = 0;
3190     
3191     	memset(&route, 0, sizeof(route));
3192     	route.rt_dev = dev->name;
3193     	route.rt_flags = 0;
3194     	((struct sockaddr_in *)&(route.rt_dst))->sin_addr.s_addr =
3195     			dev->pa_dstaddr;
3196     	((struct sockaddr_in *)&(route.rt_dst))->sin_family = AF_INET;
3197     	((struct sockaddr_in *)&(route.rt_genmask))->sin_addr.s_addr =
3198     			0xFFFFFFFF;
3199             ((struct sockaddr_in *)&(route.rt_genmask))->sin_family =
3200     			AF_INET;
3201     
3202     
3203     	err = ip_rt_kill(&route);
3204     
3205     #endif
3206     
3207             set_fs(fs);           /* restore old block */
3208     
3209     	
3210     	if (err) {
3211     		printk (KERN_INFO "%s: Deleting dynamic route failed %d!\n",
3212     			 card->devname, err);
3213     		return;
3214     	}else{
3215     		printk (KERN_INFO "%s: PPP Deleting dynamic route %s successfuly\n",
3216     			card->devname, in_ntoa(ip_addr));
3217     	}
3218     	return;
3219     }
3220     
3221     /*=============================================================================
3222      * Perform the Interrupt Test by running the READ_CODE_VERSION command MAX_INTR
3223      * _TEST_COUNTER times.
3224      */
3225     static int intr_test( sdla_t *card )
3226     {
3227     	ppp_mbox_t *mb = card->mbox;
3228     	int err,i;
3229     
3230     	err = ppp_set_intr_mode( card, 0x08 );
3231     	
3232     	if (err == CMD_OK) { 
3233     		
3234     		for (i = 0; i < MAX_INTR_TEST_COUNTER; i ++) {	
3235     			/* Run command READ_CODE_VERSION */
3236     			memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
3237     			mb->cmd.length  = 0;
3238     			mb->cmd.command = PPP_READ_CODE_VERSION;
3239     			err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
3240     			if (err != CMD_OK) 
3241     				ppp_error(card, err, mb);
3242     		}
3243     	}
3244     	else return err;
3245     
3246     	err = ppp_set_intr_mode( card, 0 );
3247     	if (err != CMD_OK) 
3248     		return err;
3249     
3250     	return 0;
3251     }
3252     
3253     /*==============================================================================
3254      * Determine what type of UDP call it is. DRVSTATS or PTPIPEAB ?
3255      */
3256     static int udp_pkt_type( struct sk_buff *skb, sdla_t *card )
3257     {
3258     	unsigned char *sendpacket;
3259     	unsigned char buf2[5]; 
3260     	ppp_udp_pkt_t *ppp_udp_pkt = (ppp_udp_pkt_t *)skb->data; 
3261     	
3262     	sendpacket = skb->data;
3263     	memcpy(&buf2, &card->wandev.udp_port, 2);
3264     	
3265     	if( 	ppp_udp_pkt->ip_pkt.ver_inet_hdr_length  == 0x45 &&        /* IP packet */ 
3266     		sendpacket[9]  == 0x11 &&        /* UDP packet */
3267     		sendpacket[22] == buf2[1] &&     /* UDP Port */
3268     		sendpacket[23] == buf2[0] &&
3269     		sendpacket[36] == 0x01 ) {
3270     	
3271     		if (    sendpacket[28] == 0x50 &&    /* PTPIPEAB: Signature */ 
3272     			sendpacket[29] == 0x54 &&      
3273     			sendpacket[30] == 0x50 &&      
3274     			sendpacket[31] == 0x49 &&      
3275     			sendpacket[32] == 0x50 &&      
3276     			sendpacket[33] == 0x45 &&      
3277     			sendpacket[34] == 0x41 &&      
3278     			sendpacket[35] == 0x42 ){ 
3279     
3280     			return UDP_PTPIPE_TYPE;
3281     	
3282     		} else if(sendpacket[28] == 0x44 &&  /* DRVSTATS: Signature */
3283     			sendpacket[29] == 0x52 &&      
3284           			sendpacket[30] == 0x56 &&      
3285           			sendpacket[31] == 0x53 &&      
3286           			sendpacket[32] == 0x54 &&      
3287           			sendpacket[33] == 0x41 &&      
3288           			sendpacket[34] == 0x54 &&      
3289           			sendpacket[35] == 0x53 ){
3290     	
3291     			return UDP_DRVSTATS_TYPE;
3292     
3293     		} else
3294     			return UDP_INVALID_TYPE;
3295     
3296     	} else
3297     		return UDP_INVALID_TYPE;
3298     
3299     }
3300     
3301     /*============================================================================
3302      * Check to see if the packet to be transmitted contains a broadcast or
3303      * multicast source IP address.
3304      */
3305     
3306     static int chk_bcast_mcast_addr(sdla_t *card, netdevice_t* dev,
3307     				struct sk_buff *skb)
3308     {
3309     	u32 src_ip_addr;
3310             u32 broadcast_ip_addr = 0;
3311     #if defined(LINUX_2_1) || defined(LINUX_2_4)
3312             struct in_device *in_dev;
3313     #endif
3314             /* read the IP source address from the outgoing packet */
3315             src_ip_addr = *(u32 *)(skb->data + 12);
3316     
3317     	/* read the IP broadcast address for the device */
3318     #if defined(LINUX_2_1) || defined(LINUX_2_4)
3319             in_dev = dev->ip_ptr;
3320             if(in_dev != NULL) {
3321                     struct in_ifaddr *ifa= in_dev->ifa_list;
3322                     if(ifa != NULL)
3323                             broadcast_ip_addr = ifa->ifa_broadcast;
3324                     else
3325                             return 0;
3326             }
3327     #else
3328             broadcast_ip_addr = dev->pa_brdaddr;
3329     #endif
3330      
3331             /* check if the IP Source Address is a Broadcast address */
3332             if((dev->flags & IFF_BROADCAST) && (src_ip_addr == broadcast_ip_addr)) {
3333                     printk(KERN_INFO "%s: Broadcast Source Address silently discarded\n",
3334     				card->devname);
3335                     return 1;
3336             } 
3337     
3338             /* check if the IP Source Address is a Multicast address */
3339             if((ntohl(src_ip_addr) >= 0xE0000001) &&
3340     		(ntohl(src_ip_addr) <= 0xFFFFFFFE)) {
3341                     printk(KERN_INFO "%s: Multicast Source Address silently discarded\n",
3342     				card->devname);
3343                     return 1;
3344             }
3345     
3346             return 0;
3347     }
3348     
3349     void s508_lock (sdla_t *card, unsigned long *smp_flags)
3350     {
3351     #if defined(__SMP__) || defined(LINUX_2_4)
3352     	spin_lock_irqsave(&card->wandev.lock, *smp_flags);
3353     #else
3354     	disable_irq(card->hw.irq);
3355     #endif                                                                     
3356     }
3357     
3358     void s508_unlock (sdla_t *card, unsigned long *smp_flags)
3359     {
3360     #if defined(__SMP__) || defined(LINUX_2_4)
3361             spin_unlock_irqrestore(&card->wandev.lock, *smp_flags);
3362     #else
3363     	enable_irq(card->hw.irq);
3364     #endif           
3365     }
3366     
3367     static int read_connection_info (sdla_t *card)
3368     {
3369     	ppp_mbox_t *mb = card->mbox;
3370     	netdevice_t *dev = card->wandev.dev;
3371     	ppp_private_area_t *ppp_priv_area = dev->priv;
3372     	ppp508_connect_info_t *ppp508_connect_info;
3373     	int err;
3374     
3375     	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
3376     	mb->cmd.length  = 0;
3377     	mb->cmd.command = PPP_GET_CONNECTION_INFO;
3378     	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
3379     
3380     	if (err != CMD_OK) { 
3381     		ppp_error(card, err, mb);
3382     		ppp_priv_area->ip_remote = 0;
3383     		ppp_priv_area->ip_local = 0;
3384     	}
3385     	else {
3386     		ppp508_connect_info = (ppp508_connect_info_t *)mb->data;
3387     		ppp_priv_area->ip_remote = ppp508_connect_info->ip_remote;
3388     		ppp_priv_area->ip_local = ppp508_connect_info->ip_local;
3389     
3390     		NEX_PRINTK(KERN_INFO "READ CONNECTION GOT IP ADDRESS %x, %x\n",
3391     				ppp_priv_area->ip_remote,
3392     				ppp_priv_area->ip_local);
3393     	}
3394     
3395     	return err;
3396     }
3397     
3398     /*===============================================================================
3399      * config_ppp
3400      *
3401      *	Configure the ppp protocol and enable communications.		
3402      *
3403      *   	The if_open function binds this function to the poll routine.
3404      *      Therefore, this function will run every time the ppp interface
3405      *      is brought up.  
3406      *      
3407      *	If the communications are not enabled, proceed to configure
3408      *      the card and enable communications.
3409      *
3410      *      If the communications are enabled, it means that the interface
3411      *      was shutdown by ether the user or driver. In this case, we 
3412      *      have to check that the IP addresses have not changed.  If
3413      *      the IP addresses changed, we have to reconfigure the firmware
3414      *      and update the changed IP addresses.  Otherwise, just exit.
3415      */
3416     static int config_ppp (sdla_t *card)
3417     {
3418     
3419     	netdevice_t *dev = card->wandev.dev;
3420     	ppp_flags_t *flags = card->flags;
3421     	ppp_private_area_t *ppp_priv_area = dev->priv;
3422     
3423     	if (card->u.p.comm_enabled){
3424     
3425     		if (ppp_priv_area->ip_local_tmp != ppp_priv_area->ip_local ||
3426     		    ppp_priv_area->ip_remote_tmp != ppp_priv_area->ip_remote){
3427     			
3428     			/* The IP addersses have changed, we must
3429                              * stop the communications and reconfigure
3430                              * the card. Reason: the firmware must know
3431                              * the local and remote IP addresses. */
3432     			disable_comm(card);
3433     			wanpipe_set_state(card, WAN_DISCONNECTED);
3434     			printk(KERN_INFO 
3435     				"%s: IP addresses changed!\n",
3436     					card->devname);
3437     			printk(KERN_INFO "%s: Restarting communications ...\n",
3438     					card->devname);
3439     		}else{ 
3440     			/* IP addresses are the same and the link is up, 
3441                              * we dont have to do anything here. Therefore, exit */
3442     			return 0;
3443     		}
3444     	}
3445     
3446     	/* Record the new IP addreses */
3447     	ppp_priv_area->ip_local = ppp_priv_area->ip_local_tmp;
3448     	ppp_priv_area->ip_remote = ppp_priv_area->ip_remote_tmp;
3449     
3450     	if (config508(dev, card)){
3451     		printk(KERN_INFO "%s: Failed to configure PPP device\n",
3452     			card->devname);
3453     		return 0;
3454     	}
3455     
3456     	if (ppp_set_intr_mode(card, PPP_INTR_RXRDY|
3457     			    		PPP_INTR_TXRDY|
3458     				    	PPP_INTR_MODEM|
3459     				    	PPP_INTR_DISC |
3460     				    	PPP_INTR_OPEN |
3461     				    	PPP_INTR_DROP_DTR |
3462     					PPP_INTR_TIMER)) {
3463     
3464     		printk(KERN_INFO "%s: Failed to configure board interrupts !\n", 
3465     			card->devname);
3466     		return 0;
3467     	}
3468     
3469             /* Turn off the transmit and timer interrupt */
3470     	flags->imask &= ~(PPP_INTR_TXRDY | PPP_INTR_TIMER) ;
3471     
3472     
3473     	/* If you are not the authenticator and any one of the protocol is 
3474     	 * enabled then we call the set_out_bound_authentication.
3475     	 */
3476     	if ( !card->u.p.authenticator  && (ppp_priv_area->pap || ppp_priv_area->chap)) {
3477     		if ( ppp_set_outbnd_auth(card, ppp_priv_area) ){
3478     			printk(KERN_INFO "%s: Outbound authentication failed !\n",
3479     				card->devname);
3480     			return 0;
3481     		}
3482     	} 
3483     	
3484     	/* If you are the authenticator and any one of the protocol is enabled
3485     	 * then we call the set_in_bound_authentication.
3486     	 */
3487     	if (card->u.p.authenticator && (ppp_priv_area->pap || ppp_priv_area->chap)){
3488     		if (ppp_set_inbnd_auth(card, ppp_priv_area)){
3489     			printk(KERN_INFO "%s: Inbound authentication failed !\n",
3490     				card->devname);	
3491     			return 0;
3492     		}
3493     	}
3494     
3495     	/* If we fail to enable communications here it's OK,
3496     	 * since the DTR timer will cause a disconnected, which
3497     	 * will retrigger communication in timer_intr() */
3498     	if (ppp_comm_enable(card) == CMD_OK) {
3499     		wanpipe_set_state(card, WAN_CONNECTING);
3500     		init_ppp_tx_rx_buff(card);
3501     	}
3502     
3503     	return 0; 
3504     }
3505     
3506     /*============================================================
3507      * ppp_poll
3508      *	
3509      * Rationale:
3510      * 	We cannot manipulate the routing tables, or
3511      *      ip addresses withing the interrupt. Therefore
3512      *      we must perform such actons outside an interrupt 
3513      *      at a later time. 
3514      *
3515      * Description:	
3516      *	PPP polling routine, responsible for 
3517      *     	shutting down interfaces upon disconnect
3518      *     	and adding/removing routes. 
3519      *      
3520      * Usage:        
3521      * 	This function is executed for each ppp  
3522      * 	interface through a tq_schedule bottom half.
3523      *      
3524      *      trigger_ppp_poll() function is used to kick
3525      *      the ppp_poll routine.  
3526      */
3527     static void ppp_poll (netdevice_t *dev)
3528     {
3529     	ppp_private_area_t *ppp_priv_area; 	
3530     	sdla_t *card;
3531     	u8 check_gateway=0;
3532     	ppp_flags_t *flags;
3533     
3534     	if (!dev || (ppp_priv_area = dev->priv) == NULL)
3535     		return;
3536     
3537     	card = ppp_priv_area->card;
3538     	flags = card->flags;
3539     
3540     	/* Shutdown is in progress, stop what you are 
3541     	 * doing and get out */
3542     	if (test_bit(PERI_CRIT,&card->wandev.critical)){
3543     		clear_bit(POLL_CRIT,&card->wandev.critical);
3544     		return;
3545     	}
3546     
3547     	/* if_open() function has triggered the polling routine
3548     	 * to determine the configured IP addresses.  Once the
3549     	 * addresses are found, trigger the chdlc configuration */
3550     	if (test_bit(0,&ppp_priv_area->config_ppp)){
3551     
3552     		ppp_priv_area->ip_local_tmp  = get_ip_address(dev,WAN_LOCAL_IP);
3553     		ppp_priv_area->ip_remote_tmp = get_ip_address(dev,WAN_POINTOPOINT_IP);
3554     
3555     		if (ppp_priv_area->ip_local_tmp == ppp_priv_area->ip_remote_tmp && 
3556     	            card->u.p.ip_mode == WANOPT_PPP_HOST){
3557     			
3558     			if (++ppp_priv_area->ip_error > MAX_IP_ERRORS){
3559     				printk(KERN_INFO "\n%s: --- WARNING ---\n",
3560     						card->devname);
3561     				printk(KERN_INFO "%s: The local IP address is the same as the\n",
3562     						card->devname);
3563     				printk(KERN_INFO "%s: Point-to-Point IP address.\n",
3564     						card->devname);
3565     				printk(KERN_INFO "%s: --- WARNING ---\n\n",
3566     						card->devname);
3567     			}else{
3568     				clear_bit(POLL_CRIT,&card->wandev.critical);
3569     				ppp_priv_area->poll_delay_timer.expires = jiffies+HZ;
3570     				add_timer(&ppp_priv_area->poll_delay_timer);
3571     				return;
3572     			}
3573     		}
3574     
3575     		ppp_priv_area->timer_int_enabled |= TMR_INT_ENABLED_CONFIG;
3576     		flags->imask |= PPP_INTR_TIMER;	
3577     		ppp_priv_area->ip_error=0;	
3578     		
3579     		clear_bit(0,&ppp_priv_area->config_ppp);
3580     		clear_bit(POLL_CRIT,&card->wandev.critical);
3581     		return;
3582     	}
3583     
3584     	/* Dynamic interface implementation, as well as dynamic
3585     	 * routing.  */
3586     	
3587     	switch (card->wandev.state) {
3588     	
3589     	case WAN_DISCONNECTED:
3590     
3591     		/* If the dynamic interface configuration is on, and interface 
3592     		 * is up, then bring down the netowrk interface */
3593     
3594     		if (test_bit(DYN_OPT_ON,&ppp_priv_area->interface_down) &&
3595     		    !test_bit(DEV_DOWN,&ppp_priv_area->interface_down)	&&	
3596     		    card->wandev.dev->flags & IFF_UP){	
3597     
3598     			printk(KERN_INFO "%s: Interface %s down.\n",
3599     				card->devname,card->wandev.dev->name);
3600     			change_dev_flags(card->wandev.dev,
3601     					(card->wandev.dev->flags&~IFF_UP));
3602     			set_bit(DEV_DOWN,&ppp_priv_area->interface_down);
3603     		}else{
3604     			/* We need to check if the local IP address is
3605                    	   	 * zero. If it is, we shouldn't try to remove it.
3606                      	 * For some reason the kernel crashes badly if 
3607                      	 * we try to remove the route twice */
3608     
3609     			if (card->wandev.dev->flags & IFF_UP && 
3610     		    	    get_ip_address(card->wandev.dev,WAN_LOCAL_IP) &&
3611     		    	    card->u.p.ip_mode == WANOPT_PPP_PEER){
3612     
3613     				remove_route(card);
3614     			}
3615     		}
3616     		break;
3617     
3618     	case WAN_CONNECTED:
3619     		
3620     		/* In SMP machine this code can execute before the interface
3621     		 * comes up.  In this case, we must make sure that we do not
3622     		 * try to bring up the interface before dev_open() is finished */
3623     
3624     
3625     		/* DEV_DOWN will be set only when we bring down the interface
3626     		 * for the very first time. This way we know that it was us
3627     		 * that brought the interface down */
3628     		
3629     		if (test_bit(DYN_OPT_ON,&ppp_priv_area->interface_down) &&
3630     	            test_bit(DEV_DOWN,  &ppp_priv_area->interface_down) &&
3631      		    !(card->wandev.dev->flags & IFF_UP)){
3632     			
3633     			printk(KERN_INFO "%s: Interface %s up.\n",
3634     				card->devname,card->wandev.dev->name);
3635     			
3636     			change_dev_flags(card->wandev.dev,(card->wandev.dev->flags|IFF_UP));
3637     			clear_bit(DEV_DOWN,&ppp_priv_area->interface_down);
3638     			check_gateway=1;
3639     		}
3640     
3641     		if ((card->u.p.ip_mode == WANOPT_PPP_PEER) && 
3642     		    test_bit(1,&Read_connection_info)) { 
3643     			
3644     			process_route(card);
3645     			clear_bit(1,&Read_connection_info);
3646     			check_gateway=1;
3647     		}
3648     
3649     		if (ppp_priv_area->gateway && check_gateway)
3650     			add_gateway(card,dev);
3651     
3652     		break;
3653     	}
3654     	clear_bit(POLL_CRIT,&card->wandev.critical);
3655     	return;
3656     }
3657     
3658     /*============================================================
3659      * trigger_ppp_poll
3660      *
3661      * Description:
3662      * 	Add a ppp_poll() task into a tq_scheduler bh handler
3663      *      for a specific interface.  This will kick
3664      *      the ppp_poll() routine at a later time. 
3665      *
3666      * Usage:
3667      * 	Interrupts use this to defer a taks to 
3668      *      a polling routine.
3669      *
3670      */	
3671     
3672     static void trigger_ppp_poll (netdevice_t *dev)
3673     {
3674     	ppp_private_area_t *ppp_priv_area;
3675     	if ((ppp_priv_area=dev->priv) != NULL){ 	
3676     		
3677     		sdla_t *card = ppp_priv_area->card;
3678     
3679     		if (test_bit(PERI_CRIT,&card->wandev.critical)){
3680     			return;
3681     		}
3682     		
3683     		if (test_and_set_bit(POLL_CRIT,&card->wandev.critical)){
3684     			return;
3685     		}
3686     
3687     #ifdef LINUX_2_4
3688     		schedule_task(&ppp_priv_area->poll_task);
3689     #else
3690     		queue_task(&ppp_priv_area->poll_task, &tq_scheduler);
3691     #endif
3692     	}
3693     	return;
3694     }
3695     
3696     static void ppp_poll_delay (unsigned long dev_ptr)
3697     {
3698     	netdevice_t *dev = (netdevice_t *)dev_ptr;
3699     	trigger_ppp_poll(dev);
3700     }
3701     
3702     /*============================================================
3703      * detect_and_fix_tx_bug
3704      *
3705      * Description:
3706      *	On connect, if the board tx buffer ptr is not the same
3707      *      as the driver tx buffer ptr, we found a firmware bug.
3708      *      Report the bug to the above layer.  To fix the
3709      *      error restart communications again.
3710      *
3711      * Usage:
3712      *
3713      */	
3714     
3715     static int detect_and_fix_tx_bug (sdla_t *card)
3716     {
3717     	if (((unsigned long)card->u.p.txbuf_base&0xFFF) != ((*card->u.p.txbuf_next)&0xFFF)){
3718     		NEX_PRINTK(KERN_INFO "Major Error, Fix the bug\n");
3719     		return 1;
3720     	}
3721     	return 0;
3722     }
3723     
3724     MODULE_LICENSE("GPL");
3725     
3726     /****** End *****************************************************************/
3727