v2.4.8 -> v2.4.8.1
[opensuse:kernel.git] / drivers / net / wan / sdlamain.c
1 /****************************************************************************
2 * sdlamain.c    WANPIPE(tm) Multiprotocol WAN Link Driver.  Main module.
3 *
4 * Author:       Nenad Corbic    <ncorbic@sangoma.com>
5 *               Gideon Hack     
6 *
7 * Copyright:    (c) 1995-2000 Sangoma Technologies Inc.
8 *
9 *               This program is free software; you can redistribute it and/or
10 *               modify it under the terms of the GNU General Public License
11 *               as published by the Free Software Foundation; either version
12 *               2 of the License, or (at your option) any later version.
13 * ============================================================================
14 * Dec 22, 2000  Nenad Corbic    Updated for 2.4.X kernels.
15 *                               Removed the polling routine.
16 * Nov 13, 2000  Nenad Corbic    Added hw probing on module load and dynamic
17 *                               device allocation. 
18 * Nov 7,  2000  Nenad Corbic    Fixed the Multi-Port PPP for kernels
19 *                               2.2.16 and above.
20 * Aug 2,  2000  Nenad Corbic    Block the Multi-Port PPP from running on
21 *                               kernels 2.2.16 or greater.  The SyncPPP 
22 *                               has changed.
23 * Jul 25, 2000  Nenad Corbic    Updated the Piggiback support for MultPPPP.
24 * Jul 13, 2000  Nenad Corbic    Added Multi-PPP support.
25 * Feb 02, 2000  Nenad Corbic    Fixed up piggyback probing and selection.
26 * Sep 23, 1999  Nenad Corbic    Added support for SMP
27 * Sep 13, 1999  Nenad Corbic    Each port is treated as a separate device.
28 * Jun 02, 1999  Gideon Hack     Added support for the S514 adapter.
29 *                               Updates for Linux 2.2.X kernels.
30 * Sep 17, 1998  Jaspreet Singh  Updated for 2.1.121+ kernel
31 * Nov 28, 1997  Jaspreet Singh  Changed DRV_RELEASE to 1
32 * Nov 10, 1997  Jaspreet Singh  Changed sti() to restore_flags();
33 * Nov 06, 1997  Jaspreet Singh  Changed DRV_VERSION to 4 and DRV_RELEASE to 0
34 * Oct 20, 1997  Jaspreet Singh  Modified sdla_isr routine so that card->in_isr
35 *                               assignments are taken out and placed in the
36 *                               sdla_ppp.c, sdla_fr.c and sdla_x25.c isr
37 *                               routines. Took out 'wandev->tx_int_enabled' and
38 *                               replaced it with 'wandev->enable_tx_int'. 
39 * May 29, 1997  Jaspreet Singh  Flow Control Problem
40 *                               added "wandev->tx_int_enabled=1" line in the
41 *                               init module. This line intializes the flag for 
42 *                               preventing Interrupt disabled with device set to
43 *                               busy
44 * Jan 15, 1997  Gene Kozin      Version 3.1.0
45 *                                o added UDP management stuff
46 * Jan 02, 1997  Gene Kozin      Initial version.
47 *****************************************************************************/
48
49 #include <linux/version.h>
50 #include <linux/config.h>       /* OS configuration options */
51 #include <linux/stddef.h>       /* offsetof(), etc. */
52 #include <linux/errno.h>        /* return codes */
53 #include <linux/string.h>       /* inline memset(), etc. */
54 #include <linux/slab.h> /* kmalloc(), kfree() */
55 #include <linux/kernel.h>       /* printk(), and other useful stuff */
56 #include <linux/module.h>       /* support for loadable modules */
57 #include <linux/ioport.h>       /* request_region(), release_region() */
58 #include <linux/wanrouter.h>    /* WAN router definitions */
59 #include <linux/wanpipe.h>      /* WANPIPE common user API definitions */
60
61 #include <linux/in.h>
62 #include <asm/io.h>             /* phys_to_virt() */
63 #include <linux/pci.h>
64 #include <linux/sdlapci.h>
65 #include <linux/if_wanpipe_common.h>
66
67 #if defined(LINUX_2_4)
68
69  #include <asm/uaccess.h>       /* kernel <-> user copy */
70  #include <linux/inetdevice.h>
71  #define netdevice_t struct net_device 
72
73 #elif defined(LINUX_2_1)
74
75  #include <asm/uaccess.h>       /* kernel <-> user copy */
76  #include <linux/inetdevice.h>
77  #define netdevice_t struct device 
78
79 #else
80
81  #include <asm/segment.h>
82  #define devinet_ioctl(x,y) dev_ioctl(x,y)
83  #define netdevice_t struct device 
84  #define test_and_set_bit set_bit
85  typedef unsigned long mm_segment_t; 
86 #endif
87
88 #include <linux/ip.h>
89 #include <net/route.h>
90  
91 #define KMEM_SAFETYZONE 8
92
93
94 #ifndef CONFIG_WANPIPE_FR
95   #define wpf_init(a,b) (-EPROTONOSUPPORT) 
96 #endif
97
98 #ifndef CONFIG_WANPIPE_CHDLC
99  #define wpc_init(a,b) (-EPROTONOSUPPORT) 
100 #endif
101
102 #ifndef CONFIG_WANPIPE_X25
103  #define wpx_init(a,b) (-EPROTONOSUPPORT) 
104 #endif
105  
106 #ifndef CONFIG_WANPIPE_PPP
107  #define wpp_init(a,b) (-EPROTONOSUPPORT) 
108 #endif
109
110 #ifndef CONFIG_WANPIPE_MULTPPP 
111  #define wsppp_init(a,b) (-EPROTONOSUPPORT) 
112 #endif
113  
114  
115 /***********FOR DEBUGGING PURPOSES*********************************************
116 static void * dbg_kmalloc(unsigned int size, int prio, int line) {
117         int i = 0;
118         void * v = kmalloc(size+sizeof(unsigned int)+2*KMEM_SAFETYZONE*8,prio);
119         char * c1 = v;  
120         c1 += sizeof(unsigned int);
121         *((unsigned int *)v) = size;
122
123         for (i = 0; i < KMEM_SAFETYZONE; i++) {
124                 c1[0] = 'D'; c1[1] = 'E'; c1[2] = 'A'; c1[3] = 'D';
125                 c1[4] = 'B'; c1[5] = 'E'; c1[6] = 'E'; c1[7] = 'F';
126                 c1 += 8;
127         }
128         c1 += size;
129         for (i = 0; i < KMEM_SAFETYZONE; i++) {
130                 c1[0] = 'M'; c1[1] = 'U'; c1[2] = 'N'; c1[3] = 'G';
131                 c1[4] = 'W'; c1[5] = 'A'; c1[6] = 'L'; c1[7] = 'L';
132                 c1 += 8;
133         }
134         v = ((char *)v) + sizeof(unsigned int) + KMEM_SAFETYZONE*8;
135         printk(KERN_INFO "line %d  kmalloc(%d,%d) = %p\n",line,size,prio,v);
136         return v;
137 }
138 static void dbg_kfree(void * v, int line) {
139         unsigned int * sp = (unsigned int *)(((char *)v) - (sizeof(unsigned int) + KMEM_SAFETYZONE*8));
140         unsigned int size = *sp;
141         char * c1 = ((char *)v) - KMEM_SAFETYZONE*8;
142         int i = 0;
143         for (i = 0; i < KMEM_SAFETYZONE; i++) {
144                 if (   c1[0] != 'D' || c1[1] != 'E' || c1[2] != 'A' || c1[3] != 'D'
145                     || c1[4] != 'B' || c1[5] != 'E' || c1[6] != 'E' || c1[7] != 'F') {
146                         printk(KERN_INFO "kmalloced block at %p has been corrupted (underrun)!\n",v);
147                         printk(KERN_INFO " %4x: %2x %2x %2x %2x %2x %2x %2x %2x\n", i*8,
148                                         c1[0],c1[1],c1[2],c1[3],c1[4],c1[5],c1[6],c1[7] );
149                 }
150                 c1 += 8;
151         }
152         c1 += size;
153         for (i = 0; i < KMEM_SAFETYZONE; i++) {
154                 if (   c1[0] != 'M' || c1[1] != 'U' || c1[2] != 'N' || c1[3] != 'G'
155                     || c1[4] != 'W' || c1[5] != 'A' || c1[6] != 'L' || c1[7] != 'L'
156                    ) {
157                         printk(KERN_INFO "kmalloced block at %p has been corrupted (overrun):\n",v);
158                         printk(KERN_INFO " %4x: %2x %2x %2x %2x %2x %2x %2x %2x\n", i*8,
159                                         c1[0],c1[1],c1[2],c1[3],c1[4],c1[5],c1[6],c1[7] );
160                 }
161                 c1 += 8;
162         }
163         printk(KERN_INFO "line %d  kfree(%p)\n",line,v);
164         v = ((char *)v) - (sizeof(unsigned int) + KMEM_SAFETYZONE*8);
165         kfree(v);
166 }
167
168 #define kmalloc(x,y) dbg_kmalloc(x,y,__LINE__)
169 #define kfree(x) dbg_kfree(x,__LINE__)
170 ******************************************************************************/
171
172
173
174 /****** Defines & Macros ****************************************************/
175
176 #ifdef  _DEBUG_
177 #define STATIC
178 #else
179 #define STATIC          static
180 #endif
181
182 #define DRV_VERSION     5               /* version number */
183 #define DRV_RELEASE     0               /* release (minor version) number */
184 #define MAX_CARDS       16              /* max number of adapters */
185
186 #ifndef CONFIG_WANPIPE_CARDS            /* configurable option */
187 #define CONFIG_WANPIPE_CARDS 1
188 #endif
189
190 #define CMD_OK          0               /* normal firmware return code */
191 #define CMD_TIMEOUT     0xFF            /* firmware command timed out */
192 #define MAX_CMD_RETRY   10              /* max number of firmware retries */
193 /****** Function Prototypes *************************************************/
194
195 extern void disable_irq(unsigned int);
196 extern void enable_irq(unsigned int);
197  
198 /* Module entry points */
199 int init_module (void);
200 void cleanup_module (void);
201
202 /* WAN link driver entry points */
203 static int setup    (wan_device_t* wandev, wandev_conf_t* conf);
204 static int shutdown (wan_device_t* wandev);
205 static int ioctl    (wan_device_t* wandev, unsigned cmd, unsigned long arg);
206
207 /* IOCTL handlers */
208 static int ioctl_dump   (sdla_t* card, sdla_dump_t* u_dump);
209 static int ioctl_exec   (sdla_t* card, sdla_exec_t* u_exec, int);
210
211 /* Miscellaneous functions */
212 STATIC void sdla_isr    (int irq, void* dev_id, struct pt_regs *regs);
213 static void release_hw  (sdla_t *card);
214 static void run_wanpipe_tq (unsigned long);
215
216 static int check_s508_conflicts (sdla_t* card,wandev_conf_t* conf, int*);
217 static int check_s514_conflicts (sdla_t* card,wandev_conf_t* conf, int*);
218
219
220 /****** Global Data **********************************************************
221  * Note: All data must be explicitly initialized!!!
222  */
223
224 /* private data */
225 static char drvname[]   = "wanpipe";
226 static char fullname[]  = "WANPIPE(tm) Multiprotocol Driver";
227 static char copyright[] = "(c) 1995-2000 Sangoma Technologies Inc.";
228 static int ncards = 0; 
229 static sdla_t* card_array = NULL;       /* adapter data space */
230
231 /* Wanpipe's own task queue, used for all API's.
232  * All protocol specific tasks will be instered
233  * into "wanpipe_tq_custom" task_queue. 
234
235  * On each rx_interrupt, the whole task queue
236  * (wanpipe_tq_custom) will be queued into 
237  * IMMEDIATE_BH via wanpipe_mark_bh() call. 
238  
239  * The IMMEDIATE_BH will execute run_wanpipe_tq() 
240  * function, which will execute all pending,
241  * tasks in wanpipe_tq_custom queue */
242
243 #ifdef LINUX_2_4
244 DECLARE_TASK_QUEUE(wanpipe_tq_custom);
245 static struct tq_struct wanpipe_tq_task = 
246 {
247         routine: (void (*)(void *)) run_wanpipe_tq,
248         data: &wanpipe_tq_custom
249 };
250 #else
251 static struct tq_struct *wanpipe_tq_custom = NULL;
252 static struct tq_struct wanpipe_tq_task = 
253 {
254         NULL,
255         0,
256         (void *)(void *) run_wanpipe_tq,
257         &wanpipe_tq_custom
258 };
259 #endif
260
261 static int wanpipe_bh_critical=0;
262
263 /******* Kernel Loadable Module Entry Points ********************************/
264
265 /*============================================================================
266  * Module 'insert' entry point.
267  * o print announcement
268  * o allocate adapter data space
269  * o initialize static data
270  * o register all cards with WAN router
271  * o calibrate SDLA shared memory access delay.
272  *
273  * Return:      0       Ok
274  *              < 0     error.
275  * Context:     process
276  */
277  
278 #ifdef MODULE
279 int init_module (void)
280 #else
281 int wanpipe_init(void)
282 #endif
283 {
284         int cnt, err = 0;
285
286         printk(KERN_INFO "%s v%u.%u %s\n",
287                 fullname, DRV_VERSION, DRV_RELEASE, copyright);
288
289         /* Probe for wanpipe cards and return the number found */
290         printk(KERN_INFO "wanpipe: Probing for WANPIPE hardware.\n");
291         ncards = wanpipe_hw_probe();
292         if (ncards){
293                 printk(KERN_INFO "wanpipe: Allocating maximum %i devices: wanpipe%i - wanpipe%i.\n",ncards,1,ncards);
294         }else{
295                 printk(KERN_INFO "wanpipe: No S514/S508 cards found, unloading modules!\n");
296                 return -ENODEV;
297         }
298         
299         /* Verify number of cards and allocate adapter data space */
300         card_array = kmalloc(sizeof(sdla_t) * ncards, GFP_KERNEL);
301         if (card_array == NULL)
302                 return -ENOMEM;
303
304         memset(card_array, 0, sizeof(sdla_t) * ncards);
305
306         /* Register adapters with WAN router */
307         for (cnt = 0; cnt < ncards; ++ cnt) {
308                 sdla_t* card = &card_array[cnt];
309                 wan_device_t* wandev = &card->wandev;
310
311                 card->next = NULL;
312                 sprintf(card->devname, "%s%d", drvname, cnt + 1);
313                 wandev->magic    = ROUTER_MAGIC;
314                 wandev->name     = card->devname;
315                 wandev->private  = card;
316                 wandev->enable_tx_int = 0;
317                 wandev->setup    = &setup;
318                 wandev->shutdown = &shutdown;
319                 wandev->ioctl    = &ioctl;
320                 err = register_wan_device(wandev);
321                 if (err) {
322                         printk(KERN_INFO
323                                 "%s: %s registration failed with error %d!\n",
324                                 drvname, card->devname, err);
325                         break;
326                 }
327         }
328         if (cnt){
329                 ncards = cnt;   /* adjust actual number of cards */
330         }else {
331                 kfree(card_array);
332                 printk(KERN_INFO "IN Init Module: NO Cards registered\n");
333                 err = -ENODEV;
334         }
335
336         return err;
337 }
338
339 #ifdef MODULE
340 /*============================================================================
341  * Module 'remove' entry point.
342  * o unregister all adapters from the WAN router
343  * o release all remaining system resources
344  */
345 void cleanup_module (void)
346 {
347         int i;
348
349         if (!ncards)
350                 return;
351                 
352         for (i = 0; i < ncards; ++i) {
353                 sdla_t* card = &card_array[i];
354                 unregister_wan_device(card->devname);
355         }
356         kfree(card_array);
357
358         printk(KERN_INFO "\nwanpipe: WANPIPE Modules Unloaded.\n");
359 }
360
361 #endif
362
363 /******* WAN Device Driver Entry Points *************************************/
364
365 /*============================================================================
366  * Setup/configure WAN link driver.
367  * o check adapter state
368  * o make sure firmware is present in configuration
369  * o make sure I/O port and IRQ are specified
370  * o make sure I/O region is available
371  * o allocate interrupt vector
372  * o setup SDLA hardware
373  * o call appropriate routine to perform protocol-specific initialization
374  * o mark I/O region as used
375  * o if this is the first active card, then schedule background task
376  *
377  * This function is called when router handles ROUTER_SETUP IOCTL. The
378  * configuration structure is in kernel memory (including extended data, if
379  * any).
380  */
381  
382 static int setup (wan_device_t* wandev, wandev_conf_t* conf)
383 {
384         sdla_t* card;
385         int err = 0;
386         int irq=0;
387
388         /* Sanity checks */
389         if ((wandev == NULL) || (wandev->private == NULL) || (conf == NULL)){
390                 printk(KERN_INFO 
391                       "%s: Failed Sdlamain Setup wandev %u, card %u, conf %u !\n",
392                       wandev->name,
393                       (unsigned int)wandev,(unsigned int)wandev->private,
394                       (unsigned int)conf); 
395                 return -EFAULT;
396         }
397
398         printk(KERN_INFO "%s: Starting WAN Setup\n", wandev->name);
399
400         card = wandev->private;
401         if (wandev->state != WAN_UNCONFIGURED){
402                 printk(KERN_INFO "%s: failed sdlamain setup, busy!\n",
403                         wandev->name);
404                 return -EBUSY;          /* already configured */
405         }
406
407         printk(KERN_INFO "\nProcessing WAN device %s...\n", wandev->name);
408
409         /* Initialize the counters for each wandev 
410          * Used for counting number of times new_if and 
411          * del_if get called.
412          */
413         wandev->del_if_cnt = 0;
414         wandev->new_if_cnt = 0;
415         wandev->config_id  = conf->config_id;
416
417         if (!conf->data_size || (conf->data == NULL)) {
418                 printk(KERN_INFO
419                         "%s: firmware not found in configuration data!\n",
420                         wandev->name);
421                 return -EINVAL;
422         }
423
424         /* Check for resource conflicts and setup the
425          * card for piggibacking if necessary */
426         if(!conf->S514_CPU_no[0]) {
427                 if ((err=check_s508_conflicts(card,conf,&irq)) != 0){
428                         return err;
429                 }
430         }else {
431                 if ((err=check_s514_conflicts(card,conf,&irq)) != 0){
432                         return err;
433                 }
434         }
435
436         /* If the current card has already been configured
437          * or its a piggyback card, do not try to allocate
438          * resources.
439          */
440         if (!card->wandev.piggyback && !card->configured){
441
442                 /* Configure hardware, load firmware, etc. */
443                 memset(&card->hw, 0, sizeof(sdlahw_t));
444
445                 /* for an S514 adapter, pass the CPU number and the slot number read */
446                 /* from 'router.conf' to the 'sdla_setup()' function via the 'port' */
447                 /* parameter */
448                 if (conf->S514_CPU_no[0]){
449
450                         card->hw.S514_cpu_no[0] = conf->S514_CPU_no[0];
451                         card->hw.S514_slot_no = conf->PCI_slot_no;
452                         card->hw.auto_pci_cfg = conf->auto_pci_cfg;
453
454                         if (card->hw.auto_pci_cfg == WANOPT_YES){
455                                 printk(KERN_INFO "%s: Setting CPU to %c and Slot to Auto\n",
456                                 card->devname, card->hw.S514_cpu_no[0]);
457                         }else{
458                                 printk(KERN_INFO "%s: Setting CPU to %c and Slot to %i\n",
459                                 card->devname, card->hw.S514_cpu_no[0], card->hw.S514_slot_no);
460                         }
461
462                 }else{
463                         /* 508 Card io port and irq initialization */
464                         card->hw.port = conf->ioport;
465                         card->hw.irq = (conf->irq == 9) ? 2 : conf->irq;
466                 }
467
468
469                 /* Compute the virtual address of the card in kernel space */
470                 if(conf->maddr){
471                         card->hw.dpmbase = phys_to_virt(conf->maddr);
472                 }else{  
473                         card->hw.dpmbase = (void *)conf->maddr;
474                 }
475                         
476                 card->hw.dpmsize = SDLA_WINDOWSIZE;
477                 
478                 /* set the adapter type if using an S514 adapter */
479                 card->hw.type = (conf->S514_CPU_no[0]) ? SDLA_S514 : conf->hw_opt[0]; 
480                 card->hw.pclk = conf->hw_opt[1];
481
482                 err = sdla_setup(&card->hw, conf->data, conf->data_size);
483                 if (err){
484                         printk(KERN_INFO "%s: Hardware setup Failed %i\n",
485                                         card->devname,err);
486                         return err;
487                 }
488
489                 if(card->hw.type != SDLA_S514)
490                         irq = (conf->irq == 2) ? 9 : conf->irq; /* IRQ2 -> IRQ9 */
491                 else
492                         irq = card->hw.irq;
493
494                 /* request an interrupt vector - note that interrupts may be shared */
495                 /* when using the S514 PCI adapter */
496                 
497                 if(request_irq(irq, sdla_isr, 
498                       (card->hw.type == SDLA_S514) ? SA_SHIRQ : 0, 
499                        wandev->name, card)){
500
501                         printk(KERN_INFO "%s: Can't reserve IRQ %d!\n", wandev->name, irq);
502                         return -EINVAL;
503                 }
504
505         }else{
506                 printk(KERN_INFO "%s: Card Configured %i or Piggybacking %i!\n",
507                         wandev->name,card->configured,card->wandev.piggyback);
508         } 
509
510
511         if (!card->configured){
512
513                 /* Initialize the Spin lock */
514 #if defined(__SMP__) || defined(LINUX_2_4) 
515                 printk(KERN_INFO "%s: Initializing for SMP\n",wandev->name);
516 #endif
517
518                 /* Piggyback spin lock has already been initialized,
519                  * in check_s514/s508_conflicts() */
520                 if (!card->wandev.piggyback){
521                         spin_lock_init(&card->wandev.lock);
522                 }
523                 
524                 /* Intialize WAN device data space */
525                 wandev->irq       = irq;
526                 wandev->dma       = 0;
527                 if(card->hw.type != SDLA_S514){ 
528                         wandev->ioport = card->hw.port;
529                 }else{
530                         wandev->S514_cpu_no[0] = card->hw.S514_cpu_no[0];
531                         wandev->S514_slot_no = card->hw.S514_slot_no;
532                 }
533                 wandev->maddr     = (unsigned long)card->hw.dpmbase;
534                 wandev->msize     = card->hw.dpmsize;
535                 wandev->hw_opt[0] = card->hw.type;
536                 wandev->hw_opt[1] = card->hw.pclk;
537                 wandev->hw_opt[2] = card->hw.memory;
538                 wandev->hw_opt[3] = card->hw.fwid;
539         }
540
541         /* Protocol-specific initialization */
542         switch (card->hw.fwid) {
543
544         case SFID_X25_502:
545         case SFID_X25_508:
546                 printk(KERN_INFO "%s: Starting X.25 Protocol Init.\n",
547                                 card->devname);
548                 err = wpx_init(card, conf);
549                 break;
550         case SFID_FR502:
551         case SFID_FR508:
552                 printk(KERN_INFO "%s: Starting Frame Relay Protocol Init.\n",
553                                 card->devname);
554                 err = wpf_init(card, conf);
555                 break;
556         case SFID_PPP502:
557         case SFID_PPP508:
558                 printk(KERN_INFO "%s: Starting PPP Protocol Init.\n",
559                                 card->devname);
560                 err = wpp_init(card, conf);
561                 break;
562                 
563         case SFID_CHDLC508:
564         case SFID_CHDLC514:
565                 if (conf->ft1){         
566                         printk(KERN_INFO "%s: Starting FT1 CSU/DSU Config Driver.\n",
567                                 card->devname);
568                         err = wpft1_init(card, conf);
569                         break;
570                         
571                 }else if (conf->config_id == WANCONFIG_MPPP){
572                         printk(KERN_INFO "%s: Starting Multi-Port PPP Protocol Init.\n",
573                                         card->devname);
574                         err = wsppp_init(card,conf);
575                         break;
576
577                 }else{
578                         printk(KERN_INFO "%s: Starting CHDLC Protocol Init.\n",
579                                         card->devname);
580                         err = wpc_init(card, conf);
581                         break;
582                 }
583         default:
584                 printk(KERN_INFO "%s: Error, Firmware is not supported %X %X!\n",
585                         wandev->name,card->hw.fwid,SFID_CHDLC508);
586                 err = -EPROTONOSUPPORT;
587         }
588
589         if (err != 0){
590                 if (err == -EPROTONOSUPPORT){
591                         printk(KERN_INFO 
592                                 "%s: Error, Protocol selected has not been compiled!\n",
593                                         card->devname);
594                         printk(KERN_INFO 
595                                 "%s:        Re-configure the kernel and re-build the modules!\n",
596                                         card->devname);
597                 }
598                 
599                 release_hw(card);
600                 wandev->state = WAN_UNCONFIGURED;
601                 return err;
602         }
603
604
605         /* Reserve I/O region and schedule background task */
606         if(card->hw.type != SDLA_S514 && !card->wandev.piggyback)
607                 request_region(card->hw.port, card->hw.io_range, wandev->name);
608
609         /* Only use the polling routine for the X25 protocol */
610         
611         card->wandev.critical=0;
612         return 0;
613 }
614
615 /*================================================================== 
616  * configure_s508_card
617  * 
618  * For a S508 adapter, check for a possible configuration error in that
619  * we are loading an adapter in the same IO port as a previously loaded S508
620  * card.
621  */ 
622
623 static int check_s508_conflicts (sdla_t* card,wandev_conf_t* conf, int *irq)
624 {
625         unsigned long smp_flags;
626         int i;
627         
628         if (conf->ioport <= 0) {
629                 printk(KERN_INFO
630                 "%s: can't configure without I/O port address!\n",
631                 card->wandev.name);
632                 return -EINVAL;
633         }
634
635         if (conf->irq <= 0) {
636                 printk(KERN_INFO "%s: can't configure without IRQ!\n",
637                 card->wandev.name);
638                 return -EINVAL;
639         }
640
641         if (test_bit(0,&card->configured))
642                 return 0;
643
644
645         /* Check for already loaded card with the same IO port and IRQ 
646          * If found, copy its hardware configuration and use its
647          * resources (i.e. piggybacking)
648          */
649         
650         for (i = 0; i < ncards; i++) {
651                 sdla_t *nxt_card = &card_array[i];
652
653                 /* Skip the current card ptr */
654                 if (nxt_card == card)   
655                         continue;
656
657
658                 /* Find a card that is already configured with the
659                  * same IO Port */
660                 if ((nxt_card->hw.type == SDLA_S508) &&
661                     (nxt_card->hw.port == conf->ioport) && 
662                     (nxt_card->next == NULL)){
663                         
664                         /* We found a card the card that has same configuration
665                          * as us. This means, that we must setup this card in 
666                          * piggibacking mode. However, only CHDLC and MPPP protocol
667                          * support this setup */
668                 
669                         if ((conf->config_id == WANCONFIG_CHDLC || 
670                              conf->config_id == WANCONFIG_MPPP) &&
671                             (nxt_card->wandev.config_id == WANCONFIG_CHDLC || 
672                              nxt_card->wandev.config_id == WANCONFIG_MPPP)){ 
673                                 
674                                 *irq = nxt_card->hw.irq;
675                                 memcpy(&card->hw, &nxt_card->hw, sizeof(sdlahw_t));
676                         
677                                 /* The master could already be running, we must
678                                  * set this as a critical area */
679                                 lock_adapter_irq(&nxt_card->wandev.lock, &smp_flags);
680
681                                 nxt_card->next = card;
682                                 card->next = nxt_card;
683
684                                 card->wandev.piggyback = WANOPT_YES;
685
686                                 /* We must initialise the piggiback spin lock here
687                                  * since isr will try to lock card->next if it
688                                  * exists */
689                                 spin_lock_init(&card->wandev.lock);
690                                 
691                                 unlock_adapter_irq(&nxt_card->wandev.lock, &smp_flags);
692                                 break;
693                         }else{
694                                 /* Trying to run piggibacking with a wrong protocol */
695                                 printk(KERN_INFO "%s: ERROR: Resource busy, ioport: 0x%x\n"
696                                                  "%s:        This protocol doesn't support\n"
697                                                  "%s:        multi-port operation!\n",
698                                                  card->devname,nxt_card->hw.port,
699                                                  card->devname,card->devname);
700                                 return -EEXIST;
701                         }
702                 }
703         }
704         
705
706         /* Make sure I/O port region is available only if we are the
707          * master device.  If we are running in piggibacking mode, 
708          * we will use the resources of the master card */
709         if (check_region(conf->ioport, SDLA_MAXIORANGE) && 
710             !card->wandev.piggyback) {
711                 printk(KERN_INFO
712                         "%s: I/O region 0x%X - 0x%X is in use!\n",
713                         card->wandev.name, conf->ioport,
714                         conf->ioport + SDLA_MAXIORANGE);
715                 return -EINVAL;
716         }
717
718         return 0;
719 }
720
721 /*================================================================== 
722  * configure_s514_card
723  * 
724  * For a S514 adapter, check for a possible configuration error in that
725  * we are loading an adapter in the same slot as a previously loaded S514
726  * card.
727  */ 
728
729
730 static int check_s514_conflicts(sdla_t* card,wandev_conf_t* conf, int *irq)
731 {
732         unsigned long smp_flags;
733         int i;
734         
735         if (test_bit(0,&card->configured))
736                 return 0;
737
738         
739         /* Check for already loaded card with the same IO port and IRQ 
740          * If found, copy its hardware configuration and use its
741          * resources (i.e. piggybacking)
742          */
743
744         for (i = 0; i < ncards; i ++) {
745         
746                 sdla_t* nxt_card = &card_array[i];
747                 if(nxt_card == card)
748                         continue;
749                 
750                 if((nxt_card->hw.type == SDLA_S514) &&
751                    (nxt_card->hw.S514_slot_no == conf->PCI_slot_no) &&
752                    (nxt_card->hw.S514_cpu_no[0] == conf->S514_CPU_no[0])&&
753                    (nxt_card->next == NULL)){
754
755
756                         if ((conf->config_id == WANCONFIG_CHDLC || 
757                              conf->config_id == WANCONFIG_MPPP) &&
758                             (nxt_card->wandev.config_id == WANCONFIG_CHDLC || 
759                              nxt_card->wandev.config_id == WANCONFIG_MPPP)){ 
760                                 
761                                 *irq = nxt_card->hw.irq;
762                                 memcpy(&card->hw, &nxt_card->hw, sizeof(sdlahw_t));
763         
764                                 /* The master could already be running, we must
765                                  * set this as a critical area */
766                                 lock_adapter_irq(&nxt_card->wandev.lock,&smp_flags);
767                                 nxt_card->next = card;
768                                 card->next = nxt_card;
769
770                                 card->wandev.piggyback = WANOPT_YES;
771
772                                 /* We must initialise the piggiback spin lock here
773                                  * since isr will try to lock card->next if it
774                                  * exists */
775                                 spin_lock_init(&card->wandev.lock);
776
777                                 unlock_adapter_irq(&nxt_card->wandev.lock,&smp_flags);
778
779                         }else{
780                                 /* Trying to run piggibacking with a wrong protocol */
781                                 printk(KERN_INFO "%s: ERROR: Resource busy: CPU %c PCISLOT %i\n"
782                                                  "%s:        This protocol doesn't support\n"
783                                                  "%s:        multi-port operation!\n",
784                                                  card->devname,
785                                                  conf->S514_CPU_no[0],conf->PCI_slot_no,
786                                                  card->devname,card->devname);
787                                 return -EEXIST;
788                         }
789                 }
790         }
791
792         return 0;
793 }
794
795
796
797 /*============================================================================
798  * Shut down WAN link driver. 
799  * o shut down adapter hardware
800  * o release system resources.
801  *
802  * This function is called by the router when device is being unregistered or
803  * when it handles ROUTER_DOWN IOCTL.
804  */
805 static int shutdown (wan_device_t* wandev)
806 {
807         sdla_t *card;
808         int err=0;
809         
810         /* sanity checks */
811         if ((wandev == NULL) || (wandev->private == NULL)){
812                 return -EFAULT;
813         }
814                 
815         if (wandev->state == WAN_UNCONFIGURED){
816                 return 0;
817         }
818
819         card = wandev->private;
820
821         if (card->tty_opt){
822                 if (card->tty_open){
823                         printk(KERN_INFO 
824                                 "%s: Shutdown Failed: TTY is still open\n",
825                                   card->devname);
826                         return -EBUSY;
827                 }
828         }
829         
830         wandev->state = WAN_UNCONFIGURED;
831
832         set_bit(PERI_CRIT,(void*)&wandev->critical);
833         
834         /* In case of piggibacking, make sure that 
835          * we never try to shutdown both devices at the same
836          * time, because they depend on one another */
837         
838         if (card->disable_comm){
839                 card->disable_comm(card);
840         }
841
842         /* Release Resources */
843         release_hw(card);
844
845         /* only free the allocated I/O range if not an S514 adapter */
846         if (wandev->hw_opt[0] != SDLA_S514 && !card->configured){
847                 release_region(card->hw.port, card->hw.io_range);
848         }
849
850         if (!card->configured){
851                 memset(&card->hw, 0, sizeof(sdlahw_t));
852                 if (card->next){
853                         memset(&card->next->hw, 0, sizeof(sdlahw_t));
854                 }
855         }
856         
857
858         clear_bit(PERI_CRIT,(void*)&wandev->critical);
859         return err;
860 }
861
862 static void release_hw (sdla_t *card)
863 {
864         sdla_t *nxt_card;
865
866         
867         /* Check if next device exists */
868         if (card->next){
869                 nxt_card = card->next;
870                 /* If next device is down then release resources */
871                 if (nxt_card->wandev.state == WAN_UNCONFIGURED){
872                         if (card->wandev.piggyback){
873                                 /* If this device is piggyback then use
874                                  * information of the master device 
875                                  */
876                                 printk(KERN_INFO "%s: Piggyback shutting down\n",card->devname);
877                                 sdla_down(&card->next->hw);
878                                 free_irq(card->wandev.irq, card->next);
879                                 card->configured = 0;
880                                 card->next->configured = 0;
881                                 card->wandev.piggyback = 0;
882                         }else{
883                                 /* Master device shutting down */
884                                 printk(KERN_INFO "%s: Master shutting down\n",card->devname);
885                                 sdla_down(&card->hw);
886                                 free_irq(card->wandev.irq, card);
887                                 card->configured = 0;
888                                 card->next->configured = 0;
889                         }
890                 }else{
891                         printk(KERN_INFO "%s: Device still running %i\n",
892                                 nxt_card->devname,nxt_card->wandev.state);
893
894                         card->configured = 1;
895                 }
896         }else{
897                 printk(KERN_INFO "%s: Master shutting down\n",card->devname);
898                 sdla_down(&card->hw);
899                 free_irq(card->wandev.irq, card);
900                 card->configured = 0;
901         }
902         return;
903 }
904
905
906 /*============================================================================
907  * Driver I/O control. 
908  * o verify arguments
909  * o perform requested action
910  *
911  * This function is called when router handles one of the reserved user
912  * IOCTLs.  Note that 'arg' stil points to user address space.
913  */
914 static int ioctl (wan_device_t* wandev, unsigned cmd, unsigned long arg)
915 {
916         sdla_t* card;
917         int err;
918
919         /* sanity checks */
920         if ((wandev == NULL) || (wandev->private == NULL))
921                 return -EFAULT;
922         if (wandev->state == WAN_UNCONFIGURED)
923                 return -ENODEV;
924
925         card = wandev->private;
926
927         if(card->hw.type != SDLA_S514){
928                 disable_irq(card->hw.irq);
929         }
930
931         if (test_bit(SEND_CRIT, (void*)&wandev->critical)) {
932                 return -EAGAIN;
933         }
934         
935         switch (cmd) {
936         case WANPIPE_DUMP:
937                 err = ioctl_dump(wandev->private, (void*)arg);
938                 break;
939
940         case WANPIPE_EXEC:
941                 err = ioctl_exec(wandev->private, (void*)arg, cmd);
942                 break;
943         default:
944                 err = -EINVAL;
945         }
946  
947         return err;
948 }
949
950 /****** Driver IOCTL Handlers ***********************************************/
951
952 /*============================================================================
953  * Dump adapter memory to user buffer.
954  * o verify request structure
955  * o copy request structure to kernel data space
956  * o verify length/offset
957  * o verify user buffer
958  * o copy adapter memory image to user buffer
959  *
960  * Note: when dumping memory, this routine switches curent dual-port memory
961  *       vector, so care must be taken to avoid racing conditions.
962  */
963 static int ioctl_dump (sdla_t* card, sdla_dump_t* u_dump)
964 {
965         sdla_dump_t dump;
966         unsigned winsize;
967         unsigned long oldvec;   /* DPM window vector */
968         unsigned long smp_flags;
969         int err = 0;
970
971       #if defined(LINUX_2_1) || defined(LINUX_2_4)
972         if(copy_from_user((void*)&dump, (void*)u_dump, sizeof(sdla_dump_t)))
973                 return -EFAULT;
974       #else
975         if ((u_dump == NULL) ||
976             verify_area(VERIFY_READ, u_dump, sizeof(sdla_dump_t)))
977                 return -EFAULT;
978         memcpy_fromfs((void*)&dump, (void*)u_dump, sizeof(sdla_dump_t));
979       #endif
980                 
981         if ((dump.magic != WANPIPE_MAGIC) ||
982             (dump.offset + dump.length > card->hw.memory))
983                 return -EINVAL;
984         
985       #ifdef LINUX_2_0
986         if ((dump.ptr == NULL) ||
987             verify_area(VERIFY_WRITE, dump.ptr, dump.length))
988                 return -EFAULT;
989       #endif    
990
991         winsize = card->hw.dpmsize;
992
993         if(card->hw.type != SDLA_S514) {
994
995                 lock_adapter_irq(&card->wandev.lock, &smp_flags);
996                 
997                 oldvec = card->hw.vector;
998                 while (dump.length) {
999                         /* current offset */                            
1000                         unsigned pos = dump.offset % winsize;
1001                         /* current vector */
1002                         unsigned long vec = dump.offset - pos;
1003                         unsigned len = (dump.length > (winsize - pos)) ?
1004                                 (winsize - pos) : dump.length;
1005                         /* relocate window */
1006                         if (sdla_mapmem(&card->hw, vec) != 0) {
1007                                 err = -EIO;
1008                                 break;
1009                         }
1010                         
1011                       #if defined(LINUX_2_1) || defined(LINUX_2_4)
1012                         if(copy_to_user((void *)dump.ptr,
1013                                 (u8 *)card->hw.dpmbase + pos, len)){ 
1014                                 
1015                                 unlock_adapter_irq(&card->wandev.lock, &smp_flags);
1016                                 return -EFAULT;
1017                         }
1018                       #else
1019                         memcpy_tofs((void*)(dump.ptr),
1020                                 (void*)(card->hw.dpmbase + pos), len);
1021                       #endif
1022                         dump.length     -= len;
1023                         dump.offset     += len;
1024                         (char*)dump.ptr += len;
1025                 }
1026                 
1027                 sdla_mapmem(&card->hw, oldvec);/* restore DPM window position */
1028                 unlock_adapter_irq(&card->wandev.lock, &smp_flags);
1029         
1030         }else {
1031
1032              #if defined(LINUX_2_1) || defined(LINUX_2_4) 
1033                if(copy_to_user((void *)dump.ptr,
1034                                (u8 *)card->hw.dpmbase + dump.offset, dump.length)){
1035                         return -EFAULT;
1036                 }
1037              #else
1038                 memcpy_tofs((void*)(dump.ptr),
1039                 (void*)(card->hw.dpmbase + dump.offset), dump.length);
1040              #endif
1041         }
1042
1043         return err;
1044 }
1045
1046 /*============================================================================
1047  * Execute adapter firmware command.
1048  * o verify request structure
1049  * o copy request structure to kernel data space
1050  * o call protocol-specific 'exec' function
1051  */
1052 static int ioctl_exec (sdla_t* card, sdla_exec_t* u_exec, int cmd)
1053 {
1054         sdla_exec_t exec;
1055         int err=0;
1056
1057         if (card->exec == NULL && cmd == WANPIPE_EXEC){
1058                 return -ENODEV;
1059         }
1060
1061       #if defined(LINUX_2_1) || defined(LINUX_2_4)      
1062         if(copy_from_user((void*)&exec, (void*)u_exec, sizeof(sdla_exec_t)))
1063                 return -EFAULT;
1064       #else
1065         if ((u_exec == NULL) ||
1066             verify_area(VERIFY_READ, u_exec, sizeof(sdla_exec_t)))
1067                 return -EFAULT;
1068         memcpy_fromfs((void*)&exec, (void*)u_exec, sizeof(sdla_exec_t));
1069       #endif
1070
1071         if ((exec.magic != WANPIPE_MAGIC) || (exec.cmd == NULL))
1072                 return -EINVAL;
1073
1074         switch (cmd) {
1075                 case WANPIPE_EXEC:      
1076                         err = card->exec(card, exec.cmd, exec.data);
1077                         break;
1078         }       
1079         return err;
1080 }
1081
1082 /******* Miscellaneous ******************************************************/
1083
1084 /*============================================================================
1085  * SDLA Interrupt Service Routine.
1086  * o acknowledge SDLA hardware interrupt.
1087  * o call protocol-specific interrupt service routine, if any.
1088  */
1089 STATIC void sdla_isr (int irq, void* dev_id, struct pt_regs *regs)
1090 {
1091 #define card    ((sdla_t*)dev_id)
1092
1093         if(card->hw.type == SDLA_S514) {        /* handle interrrupt on S514 */
1094                 u32 int_status;
1095                 unsigned char CPU_no = card->hw.S514_cpu_no[0];
1096                 unsigned char card_found_for_IRQ;
1097                 u8 IRQ_count = 0;
1098
1099                 for(;;) {
1100
1101                         read_S514_int_stat(&card->hw, &int_status);
1102
1103                         /* check if the interrupt is for this device */
1104                         if(!((unsigned char)int_status &
1105                                 (IRQ_CPU_A | IRQ_CPU_B)))
1106                                 return;
1107
1108                         /* if the IRQ is for both CPUs on the same adapter, */
1109                         /* then alter the interrupt status so as to handle */
1110                         /* one CPU at a time */
1111                         if(((unsigned char)int_status & (IRQ_CPU_A | IRQ_CPU_B))
1112                                 == (IRQ_CPU_A | IRQ_CPU_B)) {
1113                                 int_status &= (CPU_no == S514_CPU_A) ?
1114                                         ~IRQ_CPU_B : ~IRQ_CPU_A;
1115                         }
1116  
1117                         card_found_for_IRQ = 0;
1118
1119                         /* check to see that the CPU number for this device */
1120                         /* corresponds to the interrupt status read */
1121                         switch (CPU_no) {
1122                                 case S514_CPU_A:
1123                                         if((unsigned char)int_status &
1124                                                 IRQ_CPU_A)
1125                                         card_found_for_IRQ = 1;
1126                                 break;
1127
1128                                 case S514_CPU_B:
1129                                         if((unsigned char)int_status &
1130                                                 IRQ_CPU_B)
1131                                         card_found_for_IRQ = 1;
1132                                 break;
1133                         }
1134
1135                         /* exit if the interrupt is for another CPU on the */
1136                         /* same IRQ */
1137                         if(!card_found_for_IRQ)
1138                                 return;
1139
1140                         if (!card || 
1141                            (card->wandev.state == WAN_UNCONFIGURED && !card->configured)){
1142                                         printk(KERN_INFO
1143                                                 "Received IRQ %d for CPU #%c\n",
1144                                                 irq, CPU_no);
1145                                         printk(KERN_INFO
1146                                                 "IRQ for unconfigured adapter\n");
1147                                         S514_intack(&card->hw, int_status);
1148                                         return;
1149                         }
1150
1151                         if (card->in_isr) {
1152                                 printk(KERN_INFO
1153                                         "%s: interrupt re-entrancy on IRQ %d\n",
1154                                         card->devname, card->wandev.irq);
1155                                 S514_intack(&card->hw, int_status);
1156                                 return;
1157                         }
1158
1159                         spin_lock(&card->wandev.lock);
1160                         if (card->next){
1161                                 spin_lock(&card->next->wandev.lock);
1162                         }
1163                                 
1164                         S514_intack(&card->hw, int_status);
1165                         if (card->isr)
1166                                 card->isr(card);
1167
1168                         if (card->next){
1169                                 spin_unlock(&card->next->wandev.lock);
1170                         }
1171                         spin_unlock(&card->wandev.lock);
1172
1173                         /* handle a maximum of two interrupts (one for each */
1174                         /* CPU on the adapter) before returning */  
1175                         if((++ IRQ_count) == 2)
1176                                 return;
1177                 }
1178         }
1179
1180         else {                  /* handle interrupt on S508 adapter */
1181
1182                 if (!card || ((card->wandev.state == WAN_UNCONFIGURED) && !card->configured))
1183                         return;
1184
1185                 if (card->in_isr) {
1186                         printk(KERN_INFO
1187                                 "%s: interrupt re-entrancy on IRQ %d!\n",
1188                                 card->devname, card->wandev.irq);
1189                         return;
1190                 }
1191
1192                 spin_lock(&card->wandev.lock);
1193                 if (card->next){
1194                         spin_lock(&card->next->wandev.lock);
1195                 }
1196         
1197                 sdla_intack(&card->hw);
1198                 if (card->isr)
1199                         card->isr(card);
1200                 
1201                 if (card->next){
1202                         spin_unlock(&card->next->wandev.lock);
1203                 }
1204                 spin_unlock(&card->wandev.lock);
1205
1206         }
1207                 
1208 #undef  card
1209 }
1210
1211 /*============================================================================
1212  * This routine is called by the protocol-specific modules when network
1213  * interface is being open.  The only reason we need this, is because we
1214  * have to call MOD_INC_USE_COUNT, but cannot include 'module.h' where it's
1215  * defined more than once into the same kernel module.
1216  */
1217 void wanpipe_open (sdla_t* card)
1218 {
1219         ++card->open_cnt;
1220         MOD_INC_USE_COUNT;
1221 }
1222
1223 /*============================================================================
1224  * This routine is called by the protocol-specific modules when network
1225  * interface is being closed.  The only reason we need this, is because we
1226  * have to call MOD_DEC_USE_COUNT, but cannot include 'module.h' where it's
1227  * defined more than once into the same kernel module.
1228  */
1229 void wanpipe_close (sdla_t* card)
1230 {
1231         --card->open_cnt;
1232         MOD_DEC_USE_COUNT;
1233 }
1234
1235 /*============================================================================
1236  * Set WAN device state.
1237  */
1238 void wanpipe_set_state (sdla_t* card, int state)
1239 {
1240         if (card->wandev.state != state) {
1241                 switch (state) {
1242                 case WAN_CONNECTED:
1243                         printk (KERN_INFO "%s: link connected!\n",
1244                                 card->devname);
1245                         break;
1246
1247                 case WAN_CONNECTING:
1248                         printk (KERN_INFO "%s: link connecting...\n",
1249                                 card->devname);
1250                         break;
1251
1252                 case WAN_DISCONNECTED:
1253                         printk (KERN_INFO "%s: link disconnected!\n",
1254                                 card->devname);
1255                         break;
1256                 }
1257                 card->wandev.state = state;
1258         }
1259         card->state_tick = jiffies;
1260 }
1261
1262 sdla_t * wanpipe_find_card (char *name)
1263 {
1264         int cnt;
1265         for (cnt = 0; cnt < ncards; ++ cnt) {
1266                 sdla_t* card = &card_array[cnt];
1267                 if (!strcmp(card->devname,name))
1268                         return card;
1269         }
1270         return NULL;
1271 }
1272 sdla_t * wanpipe_find_card_num (int num)
1273 {
1274         if (num < 1 || num > ncards)
1275                 return NULL;    
1276         num--;
1277         return &card_array[num];
1278 }
1279
1280
1281 static void run_wanpipe_tq (unsigned long data)
1282 {
1283         task_queue *tq_queue = (task_queue *)data;
1284         if (test_and_set_bit(2,(void*)&wanpipe_bh_critical))
1285                 printk(KERN_INFO "CRITICAL IN RUNNING TASK QUEUE\n");
1286         run_task_queue (tq_queue);
1287         clear_bit(2,(void*)&wanpipe_bh_critical);
1288
1289 }
1290
1291 void wanpipe_queue_tq (struct tq_struct *bh_pointer)
1292 {
1293         if (test_and_set_bit(1,(void*)&wanpipe_bh_critical))
1294                 printk(KERN_INFO "CRITICAL IN QUEUING TASK\n");
1295
1296         queue_task(bh_pointer,&wanpipe_tq_custom);
1297         clear_bit(1,(void*)&wanpipe_bh_critical);
1298 }
1299
1300 void wanpipe_mark_bh (void)
1301 {
1302         if (!test_and_set_bit(0,(void*)&wanpipe_bh_critical)){
1303                 queue_task(&wanpipe_tq_task,&tq_immediate);
1304                 mark_bh(IMMEDIATE_BH);
1305                 clear_bit(0,(void*)&wanpipe_bh_critical);
1306         }
1307
1308
1309 void wakeup_sk_bh (netdevice_t *dev)
1310 {
1311         wanpipe_common_t *chan = dev->priv;
1312
1313         if (test_bit(0,&chan->common_critical))
1314                 return;
1315         
1316         if (chan->sk && chan->tx_timer){
1317                 chan->tx_timer->expires=jiffies+1;
1318                 add_timer(chan->tx_timer);
1319         }
1320 }
1321
1322 int change_dev_flags (netdevice_t *dev, unsigned flags)
1323 {
1324         struct ifreq if_info;
1325         mm_segment_t fs = get_fs();
1326         int err;
1327
1328         memset(&if_info, 0, sizeof(if_info));
1329         strcpy(if_info.ifr_name, dev->name);
1330         if_info.ifr_flags = flags;      
1331
1332         set_fs(get_ds());     /* get user space block */ 
1333         err = devinet_ioctl(SIOCSIFFLAGS, &if_info);
1334         set_fs(fs);
1335
1336         return err;
1337 }
1338
1339 unsigned long get_ip_address (netdevice_t *dev, int option)
1340 {
1341         
1342       #ifdef LINUX_2_4
1343         struct in_ifaddr *ifaddr;
1344         struct in_device *in_dev;
1345
1346         if ((in_dev = __in_dev_get(dev)) == NULL){
1347                 return 0;
1348         }
1349       #elif defined(LINUX_2_1)
1350         struct in_ifaddr *ifaddr;
1351         struct in_device *in_dev;
1352         
1353         if ((in_dev = dev->ip_ptr) == NULL){
1354                 return 0;
1355         }
1356       #endif
1357
1358       #if defined(LINUX_2_1) || defined(LINUX_2_4)
1359         if ((ifaddr = in_dev->ifa_list)== NULL ){
1360                 return 0;
1361         }
1362       #endif
1363         
1364         switch (option){
1365
1366         case WAN_LOCAL_IP:
1367               #ifdef LINUX_2_0
1368                 return dev->pa_addr;
1369               #else     
1370                 return ifaddr->ifa_local;
1371               #endif    
1372                 break;
1373         
1374         case WAN_POINTOPOINT_IP:
1375               #ifdef LINUX_2_0
1376                 return dev->pa_dstaddr;
1377               #else     
1378                 return ifaddr->ifa_address;
1379               #endif    
1380                 break;  
1381
1382         case WAN_NETMASK_IP:
1383               #ifdef LINUX_2_0
1384                 return dev->pa_mask;
1385               #else     
1386                 return ifaddr->ifa_mask;
1387               #endif    
1388                 break;
1389
1390         case WAN_BROADCAST_IP:
1391               #ifdef LINUX_2_0
1392                 return dev->pa_brdaddr;
1393               #else     
1394                 return ifaddr->ifa_broadcast;
1395               #endif    
1396                 break;
1397         default:
1398                 return 0;
1399         }
1400
1401         return 0;
1402 }       
1403
1404 void add_gateway(sdla_t *card, netdevice_t *dev)
1405 {
1406         mm_segment_t oldfs;
1407         struct rtentry route;
1408         int res;
1409
1410         memset((char*)&route,0,sizeof(struct rtentry));
1411
1412         ((struct sockaddr_in *)
1413                 &(route.rt_dst))->sin_addr.s_addr = 0;
1414         ((struct sockaddr_in *)
1415                 &(route.rt_dst))->sin_family = AF_INET;
1416
1417         ((struct sockaddr_in *)
1418                 &(route.rt_genmask))->sin_addr.s_addr = 0;
1419         ((struct sockaddr_in *) 
1420                 &(route.rt_genmask)) ->sin_family = AF_INET;
1421
1422
1423         route.rt_flags = 0;  
1424         route.rt_dev = dev->name;
1425
1426         oldfs = get_fs();
1427         set_fs(get_ds());
1428       #if defined(LINUX_2_1) || defined(LINUX_2_4)
1429         res = ip_rt_ioctl(SIOCADDRT,&route);
1430       #else
1431         res = ip_rt_new(&route);
1432       #endif
1433         set_fs(oldfs);
1434
1435         if (res == 0){
1436                 printk(KERN_INFO "%s: Gateway added for %s\n",
1437                         card->devname,dev->name);
1438         }
1439
1440         return;
1441 }
1442
1443 /****** End *********************************************************/