Removed warning about TKIP not being configured if countermeasures are
[linux-2.6] / drivers / net / wan / sdla.c
1 /*
2  * SDLA         An implementation of a driver for the Sangoma S502/S508 series
3  *              multi-protocol PC interface card.  Initial offering is with 
4  *              the DLCI driver, providing Frame Relay support for linux.
5  *
6  *              Global definitions for the Frame relay interface.
7  *
8  * Version:     @(#)sdla.c   0.30       12 Sep 1996
9  *
10  * Credits:     Sangoma Technologies, for the use of 2 cards for an extended
11  *                      period of time.
12  *              David Mandelstam <dm@sangoma.com> for getting me started on 
13  *                      this project, and incentive to complete it.
14  *              Gene Kozen <74604.152@compuserve.com> for providing me with
15  *                      important information about the cards.
16  *
17  * Author:      Mike McLagan <mike.mclagan@linux.org>
18  *
19  * Changes:
20  *              0.15    Mike McLagan    Improved error handling, packet dropping
21  *              0.20    Mike McLagan    New transmit/receive flags for config
22  *                                      If in FR mode, don't accept packets from
23  *                                      non DLCI devices.
24  *              0.25    Mike McLagan    Fixed problem with rejecting packets
25  *                                      from non DLCI devices.
26  *              0.30    Mike McLagan    Fixed kernel panic when used with modified
27  *                                      ifconfig
28  *
29  *              This program is free software; you can redistribute it and/or
30  *              modify it under the terms of the GNU General Public License
31  *              as published by the Free Software Foundation; either version
32  *              2 of the License, or (at your option) any later version.
33  */
34
35 #include <linux/config.h> /* for CONFIG_DLCI_MAX */
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/types.h>
39 #include <linux/fcntl.h>
40 #include <linux/interrupt.h>
41 #include <linux/ptrace.h>
42 #include <linux/ioport.h>
43 #include <linux/in.h>
44 #include <linux/slab.h>
45 #include <linux/string.h>
46 #include <linux/timer.h>
47 #include <linux/errno.h>
48 #include <linux/init.h>
49 #include <linux/netdevice.h>
50 #include <linux/skbuff.h>
51 #include <linux/if_arp.h>
52 #include <linux/if_frad.h>
53 #include <linux/sdla.h>
54 #include <linux/bitops.h>
55
56 #include <asm/system.h>
57 #include <asm/io.h>
58 #include <asm/dma.h>
59 #include <asm/uaccess.h>
60
61 static const char* version = "SDLA driver v0.30, 12 Sep 1996, mike.mclagan@linux.org";
62
63 static unsigned int valid_port[] __initdata = { 0x250, 0x270, 0x280, 0x300, 0x350, 0x360, 0x380, 0x390};
64
65 static unsigned int valid_mem[]  __initdata = {
66                                     0xA0000, 0xA2000, 0xA4000, 0xA6000, 0xA8000, 0xAA000, 0xAC000, 0xAE000, 
67                                     0xB0000, 0xB2000, 0xB4000, 0xB6000, 0xB8000, 0xBA000, 0xBC000, 0xBE000,
68                                     0xC0000, 0xC2000, 0xC4000, 0xC6000, 0xC8000, 0xCA000, 0xCC000, 0xCE000,
69                                     0xD0000, 0xD2000, 0xD4000, 0xD6000, 0xD8000, 0xDA000, 0xDC000, 0xDE000,
70                                     0xE0000, 0xE2000, 0xE4000, 0xE6000, 0xE8000, 0xEA000, 0xEC000, 0xEE000}; 
71
72 static DEFINE_SPINLOCK(sdla_lock);
73
74 /*********************************************************
75  *
76  * these are the core routines that access the card itself 
77  *
78  *********************************************************/
79
80 #define SDLA_WINDOW(dev,addr) outb((((addr) >> 13) & 0x1F), (dev)->base_addr + SDLA_REG_Z80_WINDOW)
81
82 static void __sdla_read(struct net_device *dev, int addr, void *buf, short len)
83 {
84         char          *temp;
85         const void    *base;
86         int           offset, bytes;
87
88         temp = buf;
89         while(len)
90         {       
91                 offset = addr & SDLA_ADDR_MASK;
92                 bytes = offset + len > SDLA_WINDOW_SIZE ? SDLA_WINDOW_SIZE - offset : len;
93                 base = (const void *) (dev->mem_start + offset);
94
95                 SDLA_WINDOW(dev, addr);
96                 memcpy(temp, base, bytes);
97
98                 addr += bytes;
99                 temp += bytes;
100                 len  -= bytes;
101         }  
102 }
103
104 static void sdla_read(struct net_device *dev, int addr, void *buf, short len)
105 {
106         unsigned long flags;
107         spin_lock_irqsave(&sdla_lock, flags);
108         __sdla_read(dev, addr, buf, len);
109         spin_unlock_irqrestore(&sdla_lock, flags);
110 }
111
112 static void __sdla_write(struct net_device *dev, int addr, 
113                          const void *buf, short len)
114 {
115         const char    *temp;
116         void          *base;
117         int           offset, bytes;
118
119         temp = buf;
120         while(len)
121         {
122                 offset = addr & SDLA_ADDR_MASK;
123                 bytes = offset + len > SDLA_WINDOW_SIZE ? SDLA_WINDOW_SIZE - offset : len;
124                 base = (void *) (dev->mem_start + offset);
125
126                 SDLA_WINDOW(dev, addr);
127                 memcpy(base, temp, bytes);
128
129                 addr += bytes;
130                 temp += bytes;
131                 len  -= bytes;
132         }
133 }
134
135 static void sdla_write(struct net_device *dev, int addr, 
136                        const void *buf, short len)
137 {
138         unsigned long flags;
139
140         spin_lock_irqsave(&sdla_lock, flags);
141         __sdla_write(dev, addr, buf, len);
142         spin_unlock_irqrestore(&sdla_lock, flags);
143 }
144
145
146 static void sdla_clear(struct net_device *dev)
147 {
148         unsigned long flags;
149         char          *base;
150         int           len, addr, bytes;
151
152         len = 65536;    
153         addr = 0;
154         bytes = SDLA_WINDOW_SIZE;
155         base = (void *) dev->mem_start;
156
157         spin_lock_irqsave(&sdla_lock, flags);
158         while(len)
159         {
160                 SDLA_WINDOW(dev, addr);
161                 memset(base, 0, bytes);
162
163                 addr += bytes;
164                 len  -= bytes;
165         }
166         spin_unlock_irqrestore(&sdla_lock, flags);
167
168 }
169
170 static char sdla_byte(struct net_device *dev, int addr)
171 {
172         unsigned long flags;
173         char          byte, *temp;
174
175         temp = (void *) (dev->mem_start + (addr & SDLA_ADDR_MASK));
176
177         spin_lock_irqsave(&sdla_lock, flags);
178         SDLA_WINDOW(dev, addr);
179         byte = *temp;
180         spin_unlock_irqrestore(&sdla_lock, flags);
181
182         return(byte);
183 }
184
185 static void sdla_stop(struct net_device *dev)
186 {
187         struct frad_local *flp;
188
189         flp = dev->priv;
190         switch(flp->type)
191         {
192                 case SDLA_S502A:
193                         outb(SDLA_S502A_HALT, dev->base_addr + SDLA_REG_CONTROL);
194                         flp->state = SDLA_HALT;
195                         break;
196                 case SDLA_S502E:
197                         outb(SDLA_HALT, dev->base_addr + SDLA_REG_Z80_CONTROL);
198                         outb(SDLA_S502E_ENABLE, dev->base_addr + SDLA_REG_CONTROL);
199                         flp->state = SDLA_S502E_ENABLE;
200                         break;
201                 case SDLA_S507:
202                         flp->state &= ~SDLA_CPUEN;
203                         outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
204                         break;
205                 case SDLA_S508:
206                         flp->state &= ~SDLA_CPUEN;
207                         outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
208                         break;
209         }
210 }
211
212 static void sdla_start(struct net_device *dev)
213 {
214         struct frad_local *flp;
215
216         flp = dev->priv;
217         switch(flp->type)
218         {
219                 case SDLA_S502A:
220                         outb(SDLA_S502A_NMI, dev->base_addr + SDLA_REG_CONTROL);
221                         outb(SDLA_S502A_START, dev->base_addr + SDLA_REG_CONTROL);
222                         flp->state = SDLA_S502A_START;
223                         break;
224                 case SDLA_S502E:
225                         outb(SDLA_S502E_CPUEN, dev->base_addr + SDLA_REG_Z80_CONTROL);
226                         outb(0x00, dev->base_addr + SDLA_REG_CONTROL);
227                         flp->state = 0;
228                         break;
229                 case SDLA_S507:
230                         flp->state |= SDLA_CPUEN;
231                         outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
232                         break;
233                 case SDLA_S508:
234                         flp->state |= SDLA_CPUEN;
235                         outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
236                         break;
237         }
238 }
239
240 /****************************************************
241  *
242  * this is used for the S502A/E cards to determine
243  * the speed of the onboard CPU.  Calibration is
244  * necessary for the Frame Relay code uploaded 
245  * later.  Incorrect results cause timing problems
246  * with link checks & status messages
247  *
248  ***************************************************/
249
250 static int sdla_z80_poll(struct net_device *dev, int z80_addr, int jiffs, char resp1, char resp2)
251 {
252         unsigned long start, done, now;
253         char          resp, *temp;
254
255         start = now = jiffies;
256         done = jiffies + jiffs;
257
258         temp = (void *)dev->mem_start;
259         temp += z80_addr & SDLA_ADDR_MASK;
260         
261         resp = ~resp1;
262         while (time_before(jiffies, done) && (resp != resp1) && (!resp2 || (resp != resp2)))
263         {
264                 if (jiffies != now)
265                 {
266                         SDLA_WINDOW(dev, z80_addr);
267                         now = jiffies;
268                         resp = *temp;
269                 }
270         }
271         return(time_before(jiffies, done) ? jiffies - start : -1);
272 }
273
274 /* constants for Z80 CPU speed */
275 #define Z80_READY               '1'     /* Z80 is ready to begin */
276 #define LOADER_READY            '2'     /* driver is ready to begin */
277 #define Z80_SCC_OK              '3'     /* SCC is on board */
278 #define Z80_SCC_BAD             '4'     /* SCC was not found */
279
280 static int sdla_cpuspeed(struct net_device *dev, struct ifreq *ifr)
281 {
282         int  jiffs;
283         char data;
284
285         sdla_start(dev);
286         if (sdla_z80_poll(dev, 0, 3*HZ, Z80_READY, 0) < 0)
287                 return(-EIO);
288
289         data = LOADER_READY;
290         sdla_write(dev, 0, &data, 1);
291
292         if ((jiffs = sdla_z80_poll(dev, 0, 8*HZ, Z80_SCC_OK, Z80_SCC_BAD)) < 0)
293                 return(-EIO);
294
295         sdla_stop(dev);
296         sdla_read(dev, 0, &data, 1);
297
298         if (data == Z80_SCC_BAD)
299         {
300                 printk("%s: SCC bad\n", dev->name);
301                 return(-EIO);
302         }
303
304         if (data != Z80_SCC_OK)
305                 return(-EINVAL);
306
307         if (jiffs < 165)
308                 ifr->ifr_mtu = SDLA_CPU_16M;
309         else if (jiffs < 220)
310                 ifr->ifr_mtu = SDLA_CPU_10M;
311         else if (jiffs < 258)
312                 ifr->ifr_mtu = SDLA_CPU_8M;
313         else if (jiffs < 357)
314                 ifr->ifr_mtu = SDLA_CPU_7M;
315         else if (jiffs < 467)
316                 ifr->ifr_mtu = SDLA_CPU_5M;
317         else
318                 ifr->ifr_mtu = SDLA_CPU_3M;
319  
320         return(0);
321 }
322
323 /************************************************
324  *
325  *  Direct interaction with the Frame Relay code 
326  *  starts here.
327  *
328  ************************************************/
329
330 struct _dlci_stat 
331 {
332         short dlci              __attribute__((packed));
333         char  flags             __attribute__((packed));
334 };
335
336 struct _frad_stat 
337 {
338         char    flags;
339         struct _dlci_stat dlcis[SDLA_MAX_DLCI];
340 };
341
342 static void sdla_errors(struct net_device *dev, int cmd, int dlci, int ret, int len, void *data) 
343 {
344         struct _dlci_stat *pstatus;
345         short             *pdlci;
346         int               i;
347         char              *state, line[30];
348
349         switch (ret)
350         {
351                 case SDLA_RET_MODEM:
352                         state = data;
353                         if (*state & SDLA_MODEM_DCD_LOW)
354                                 printk(KERN_INFO "%s: Modem DCD unexpectedly low!\n", dev->name);
355                         if (*state & SDLA_MODEM_CTS_LOW)
356                                 printk(KERN_INFO "%s: Modem CTS unexpectedly low!\n", dev->name);
357                         /* I should probably do something about this! */
358                         break;
359
360                 case SDLA_RET_CHANNEL_OFF:
361                         printk(KERN_INFO "%s: Channel became inoperative!\n", dev->name);
362                         /* same here */
363                         break;
364
365                 case SDLA_RET_CHANNEL_ON:
366                         printk(KERN_INFO "%s: Channel became operative!\n", dev->name);
367                         /* same here */
368                         break;
369
370                 case SDLA_RET_DLCI_STATUS:
371                         printk(KERN_INFO "%s: Status change reported by Access Node.\n", dev->name);
372                         len /= sizeof(struct _dlci_stat);
373                         for(pstatus = data, i=0;i < len;i++,pstatus++)
374                         {
375                                 if (pstatus->flags & SDLA_DLCI_NEW)
376                                         state = "new";
377                                 else if (pstatus->flags & SDLA_DLCI_DELETED)
378                                         state = "deleted";
379                                 else if (pstatus->flags & SDLA_DLCI_ACTIVE)
380                                         state = "active";
381                                 else
382                                 {
383                                         sprintf(line, "unknown status: %02X", pstatus->flags);
384                                         state = line;
385                                 }
386                                 printk(KERN_INFO "%s: DLCI %i: %s.\n", dev->name, pstatus->dlci, state);
387                                 /* same here */
388                         }
389                         break;
390
391                 case SDLA_RET_DLCI_UNKNOWN:
392                         printk(KERN_INFO "%s: Received unknown DLCIs:", dev->name);
393                         len /= sizeof(short);
394                         for(pdlci = data,i=0;i < len;i++,pdlci++)
395                                 printk(" %i", *pdlci);
396                         printk("\n");
397                         break;
398
399                 case SDLA_RET_TIMEOUT:
400                         printk(KERN_ERR "%s: Command timed out!\n", dev->name);
401                         break;
402
403                 case SDLA_RET_BUF_OVERSIZE:
404                         printk(KERN_INFO "%s: Bc/CIR overflow, acceptable size is %i\n", dev->name, len);
405                         break;
406
407                 case SDLA_RET_BUF_TOO_BIG:
408                         printk(KERN_INFO "%s: Buffer size over specified max of %i\n", dev->name, len);
409                         break;
410
411                 case SDLA_RET_CHANNEL_INACTIVE:
412                 case SDLA_RET_DLCI_INACTIVE:
413                 case SDLA_RET_CIR_OVERFLOW:
414                 case SDLA_RET_NO_BUFS:
415                         if (cmd == SDLA_INFORMATION_WRITE)
416                                 break;
417
418                 default: 
419                         printk(KERN_DEBUG "%s: Cmd 0x%2.2X generated return code 0x%2.2X\n", dev->name, cmd, ret);
420                         /* Further processing could be done here */
421                         break;
422         }
423 }
424
425 static int sdla_cmd(struct net_device *dev, int cmd, short dlci, short flags, 
426                         void *inbuf, short inlen, void *outbuf, short *outlen)
427 {
428         static struct _frad_stat status;
429         struct frad_local        *flp;
430         struct sdla_cmd          *cmd_buf;
431         unsigned long            pflags;
432         unsigned long            jiffs;
433         int                      ret, waiting, len;
434         long                     window;
435
436         flp = dev->priv;
437         window = flp->type == SDLA_S508 ? SDLA_508_CMD_BUF : SDLA_502_CMD_BUF;
438         cmd_buf = (struct sdla_cmd *)(dev->mem_start + (window & SDLA_ADDR_MASK));
439         ret = 0;
440         len = 0;
441         jiffs = jiffies + HZ;  /* 1 second is plenty */
442
443         spin_lock_irqsave(&sdla_lock, pflags);
444         SDLA_WINDOW(dev, window);
445         cmd_buf->cmd = cmd;
446         cmd_buf->dlci = dlci;
447         cmd_buf->flags = flags;
448
449         if (inbuf)
450                 memcpy(cmd_buf->data, inbuf, inlen);
451
452         cmd_buf->length = inlen;
453
454         cmd_buf->opp_flag = 1;
455         spin_unlock_irqrestore(&sdla_lock, pflags);
456
457         waiting = 1;
458         len = 0;
459         while (waiting && time_before_eq(jiffies, jiffs))
460         {
461                 if (waiting++ % 3) 
462                 {
463                         spin_lock_irqsave(&sdla_lock, pflags);
464                         SDLA_WINDOW(dev, window);
465                         waiting = ((volatile int)(cmd_buf->opp_flag));
466                         spin_unlock_irqrestore(&sdla_lock, pflags);
467                 }
468         }
469         
470         if (!waiting)
471         {
472
473                 spin_lock_irqsave(&sdla_lock, pflags);
474                 SDLA_WINDOW(dev, window);
475                 ret = cmd_buf->retval;
476                 len = cmd_buf->length;
477                 if (outbuf && outlen)
478                 {
479                         *outlen = *outlen >= len ? len : *outlen;
480
481                         if (*outlen)
482                                 memcpy(outbuf, cmd_buf->data, *outlen);
483                 }
484
485                 /* This is a local copy that's used for error handling */
486                 if (ret)
487                         memcpy(&status, cmd_buf->data, len > sizeof(status) ? sizeof(status) : len);
488
489                 spin_unlock_irqrestore(&sdla_lock, pflags);
490         }
491         else
492                 ret = SDLA_RET_TIMEOUT;
493
494         if (ret != SDLA_RET_OK)
495                 sdla_errors(dev, cmd, dlci, ret, len, &status);
496
497         return(ret);
498 }
499
500 /***********************************************
501  *
502  * these functions are called by the DLCI driver 
503  *
504  ***********************************************/
505
506 static int sdla_reconfig(struct net_device *dev);
507
508 static int sdla_activate(struct net_device *slave, struct net_device *master)
509 {
510         struct frad_local *flp;
511         int i;
512
513         flp = slave->priv;
514
515         for(i=0;i<CONFIG_DLCI_MAX;i++)
516                 if (flp->master[i] == master)
517                         break;
518
519         if (i == CONFIG_DLCI_MAX)
520                 return(-ENODEV);
521
522         flp->dlci[i] = abs(flp->dlci[i]);
523
524         if (netif_running(slave) && (flp->config.station == FRAD_STATION_NODE))
525                 sdla_cmd(slave, SDLA_ACTIVATE_DLCI, 0, 0, &flp->dlci[i], sizeof(short), NULL, NULL);
526
527         return(0);
528 }
529
530 static int sdla_deactivate(struct net_device *slave, struct net_device *master)
531 {
532         struct frad_local *flp;
533         int               i;
534
535         flp = slave->priv;
536
537         for(i=0;i<CONFIG_DLCI_MAX;i++)
538                 if (flp->master[i] == master)
539                         break;
540
541         if (i == CONFIG_DLCI_MAX)
542                 return(-ENODEV);
543
544         flp->dlci[i] = -abs(flp->dlci[i]);
545
546         if (netif_running(slave) && (flp->config.station == FRAD_STATION_NODE))
547                 sdla_cmd(slave, SDLA_DEACTIVATE_DLCI, 0, 0, &flp->dlci[i], sizeof(short), NULL, NULL);
548
549         return(0);
550 }
551
552 static int sdla_assoc(struct net_device *slave, struct net_device *master)
553 {
554         struct frad_local *flp;
555         int               i;
556
557         if (master->type != ARPHRD_DLCI)
558                 return(-EINVAL);
559
560         flp = slave->priv;
561
562         for(i=0;i<CONFIG_DLCI_MAX;i++)
563         {
564                 if (!flp->master[i])
565                         break;
566                 if (abs(flp->dlci[i]) == *(short *)(master->dev_addr))
567                         return(-EADDRINUSE);
568         } 
569
570         if (i == CONFIG_DLCI_MAX)
571                 return(-EMLINK);  /* #### Alan: Comments on this ?? */
572
573
574         flp->master[i] = master;
575         flp->dlci[i] = -*(short *)(master->dev_addr);
576         master->mtu = slave->mtu;
577
578         if (netif_running(slave)) {
579                 if (flp->config.station == FRAD_STATION_CPE)
580                         sdla_reconfig(slave);
581                 else
582                         sdla_cmd(slave, SDLA_ADD_DLCI, 0, 0, master->dev_addr, sizeof(short), NULL, NULL);
583         }
584
585         return(0);
586 }
587
588 static int sdla_deassoc(struct net_device *slave, struct net_device *master)
589 {
590         struct frad_local *flp;
591         int               i;
592
593         flp = slave->priv;
594
595         for(i=0;i<CONFIG_DLCI_MAX;i++)
596                 if (flp->master[i] == master)
597                         break;
598
599         if (i == CONFIG_DLCI_MAX)
600                 return(-ENODEV);
601
602         flp->master[i] = NULL;
603         flp->dlci[i] = 0;
604
605
606         if (netif_running(slave)) {
607                 if (flp->config.station == FRAD_STATION_CPE)
608                         sdla_reconfig(slave);
609                 else
610                         sdla_cmd(slave, SDLA_DELETE_DLCI, 0, 0, master->dev_addr, sizeof(short), NULL, NULL);
611         }
612
613         return(0);
614 }
615
616 static int sdla_dlci_conf(struct net_device *slave, struct net_device *master, int get)
617 {
618         struct frad_local *flp;
619         struct dlci_local *dlp;
620         int               i;
621         short             len, ret;
622
623         flp = slave->priv;
624
625         for(i=0;i<CONFIG_DLCI_MAX;i++)
626                 if (flp->master[i] == master)
627                         break;
628
629         if (i == CONFIG_DLCI_MAX)
630                 return(-ENODEV);
631
632         dlp = master->priv;
633
634         ret = SDLA_RET_OK;
635         len = sizeof(struct dlci_conf);
636         if (netif_running(slave)) {
637                 if (get)
638                         ret = sdla_cmd(slave, SDLA_READ_DLCI_CONFIGURATION, abs(flp->dlci[i]), 0,  
639                                     NULL, 0, &dlp->config, &len);
640                 else
641                         ret = sdla_cmd(slave, SDLA_SET_DLCI_CONFIGURATION, abs(flp->dlci[i]), 0,  
642                                     &dlp->config, sizeof(struct dlci_conf) - 4 * sizeof(short), NULL, NULL);
643         }
644
645         return(ret == SDLA_RET_OK ? 0 : -EIO);
646 }
647
648 /**************************
649  *
650  * now for the Linux driver 
651  *
652  **************************/
653
654 /* NOTE: the DLCI driver deals with freeing the SKB!! */
655 static int sdla_transmit(struct sk_buff *skb, struct net_device *dev)
656 {
657         struct frad_local *flp;
658         int               ret, addr, accept, i;
659         short             size;
660         unsigned long     flags;
661         struct buf_entry  *pbuf;
662
663         flp = dev->priv;
664         ret = 0;
665         accept = 1;
666
667         netif_stop_queue(dev);
668
669         /*
670          * stupid GateD insists on setting up the multicast router thru us
671          * and we're ill equipped to handle a non Frame Relay packet at this
672          * time!
673          */
674
675         accept = 1;
676         switch (dev->type)
677         {
678                 case ARPHRD_FRAD:
679                         if (skb->dev->type != ARPHRD_DLCI)
680                         {
681                                 printk(KERN_WARNING "%s: Non DLCI device, type %i, tried to send on FRAD module.\n", dev->name, skb->dev->type);
682                                 accept = 0;
683                         }
684                         break;
685                 default:
686                         printk(KERN_WARNING "%s: unknown firmware type 0x%4.4X\n", dev->name, dev->type);
687                         accept = 0;
688                         break;
689         }
690         if (accept)
691         {
692                 /* this is frame specific, but till there's a PPP module, it's the default */
693                 switch (flp->type)
694                 {
695                         case SDLA_S502A:
696                         case SDLA_S502E:
697                                 ret = sdla_cmd(dev, SDLA_INFORMATION_WRITE, *(short *)(skb->dev->dev_addr), 0, skb->data, skb->len, NULL, NULL);
698                                 break;
699                                 case SDLA_S508:
700                                 size = sizeof(addr);
701                                 ret = sdla_cmd(dev, SDLA_INFORMATION_WRITE, *(short *)(skb->dev->dev_addr), 0, NULL, skb->len, &addr, &size);
702                                 if (ret == SDLA_RET_OK)
703                                 {
704
705                                         spin_lock_irqsave(&sdla_lock, flags);
706                                         SDLA_WINDOW(dev, addr);
707                                         pbuf = (void *)(((int) dev->mem_start) + (addr & SDLA_ADDR_MASK));
708                                         __sdla_write(dev, pbuf->buf_addr, skb->data, skb->len);
709                                         SDLA_WINDOW(dev, addr);
710                                         pbuf->opp_flag = 1;
711                                         spin_unlock_irqrestore(&sdla_lock, flags);
712                                 }
713                                 break;
714                 }
715                 switch (ret)
716                 {
717                         case SDLA_RET_OK:
718                                 flp->stats.tx_packets++;
719                                 ret = DLCI_RET_OK;
720                                 break;
721
722                         case SDLA_RET_CIR_OVERFLOW:
723                         case SDLA_RET_BUF_OVERSIZE:
724                         case SDLA_RET_NO_BUFS:
725                                 flp->stats.tx_dropped++;
726                                 ret = DLCI_RET_DROP;
727                                 break;
728
729                         default:
730                                 flp->stats.tx_errors++;
731                                 ret = DLCI_RET_ERR;
732                                 break;
733                 }
734         }
735         netif_wake_queue(dev);
736         for(i=0;i<CONFIG_DLCI_MAX;i++)
737         {
738                 if(flp->master[i]!=NULL)
739                         netif_wake_queue(flp->master[i]);
740         }               
741         return(ret);
742 }
743
744 static void sdla_receive(struct net_device *dev)
745 {
746         struct net_device         *master;
747         struct frad_local *flp;
748         struct dlci_local *dlp;
749         struct sk_buff   *skb;
750
751         struct sdla_cmd *cmd;
752         struct buf_info *pbufi;
753         struct buf_entry  *pbuf;
754
755         unsigned long     flags;
756         int               i=0, received, success, addr, buf_base, buf_top;
757         short             dlci, len, len2, split;
758
759         flp = dev->priv;
760         success = 1;
761         received = addr = buf_top = buf_base = 0;
762         len = dlci = 0;
763         skb = NULL;
764         master = NULL;
765         cmd = NULL;
766         pbufi = NULL;
767         pbuf = NULL;
768
769         spin_lock_irqsave(&sdla_lock, flags);
770
771         switch (flp->type)
772         {
773                 case SDLA_S502A:
774                 case SDLA_S502E:
775                         cmd = (void *) (dev->mem_start + (SDLA_502_RCV_BUF & SDLA_ADDR_MASK));
776                         SDLA_WINDOW(dev, SDLA_502_RCV_BUF);
777                         success = cmd->opp_flag;
778                         if (!success)
779                                 break;
780
781                         dlci = cmd->dlci;
782                         len = cmd->length;
783                         break;
784
785                 case SDLA_S508:
786                         pbufi = (void *) (dev->mem_start + (SDLA_508_RXBUF_INFO & SDLA_ADDR_MASK));
787                         SDLA_WINDOW(dev, SDLA_508_RXBUF_INFO);
788                         pbuf = (void *) (dev->mem_start + ((pbufi->rse_base + flp->buffer * sizeof(struct buf_entry)) & SDLA_ADDR_MASK));
789                         success = pbuf->opp_flag;
790                         if (!success)
791                                 break;
792
793                         buf_top = pbufi->buf_top;
794                         buf_base = pbufi->buf_base;
795                         dlci = pbuf->dlci;
796                         len = pbuf->length;
797                         addr = pbuf->buf_addr;
798                         break;
799         }
800
801         /* common code, find the DLCI and get the SKB */
802         if (success)
803         {
804                 for (i=0;i<CONFIG_DLCI_MAX;i++)
805                         if (flp->dlci[i] == dlci)
806                                 break;
807
808                 if (i == CONFIG_DLCI_MAX)
809                 {
810                         printk(KERN_NOTICE "%s: Received packet from invalid DLCI %i, ignoring.", dev->name, dlci);
811                         flp->stats.rx_errors++;
812                         success = 0;
813                 }
814         }
815
816         if (success)
817         {
818                 master = flp->master[i];
819                 skb = dev_alloc_skb(len + sizeof(struct frhdr));
820                 if (skb == NULL) 
821                 {
822                         printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
823                         flp->stats.rx_dropped++; 
824                         success = 0;
825                 }
826                 else
827                         skb_reserve(skb, sizeof(struct frhdr));
828         }
829
830         /* pick up the data */
831         switch (flp->type)
832         {
833                 case SDLA_S502A:
834                 case SDLA_S502E:
835                         if (success)
836                                 __sdla_read(dev, SDLA_502_RCV_BUF + SDLA_502_DATA_OFS, skb_put(skb,len), len);
837
838                         SDLA_WINDOW(dev, SDLA_502_RCV_BUF);
839                         cmd->opp_flag = 0;
840                         break;
841
842                 case SDLA_S508:
843                         if (success)
844                         {
845                                 /* is this buffer split off the end of the internal ring buffer */
846                                 split = addr + len > buf_top + 1 ? len - (buf_top - addr + 1) : 0;
847                                 len2 = len - split;
848
849                                 __sdla_read(dev, addr, skb_put(skb, len2), len2);
850                                 if (split)
851                                         __sdla_read(dev, buf_base, skb_put(skb, split), split);
852                         }
853
854                         /* increment the buffer we're looking at */
855                         SDLA_WINDOW(dev, SDLA_508_RXBUF_INFO);
856                         flp->buffer = (flp->buffer + 1) % pbufi->rse_num;
857                         pbuf->opp_flag = 0;
858                         break;
859         }
860
861         if (success)
862         {
863                 flp->stats.rx_packets++;
864                 dlp = master->priv;
865                 (*dlp->receive)(skb, master);
866         }
867
868         spin_unlock_irqrestore(&sdla_lock, flags);
869 }
870
871 static irqreturn_t sdla_isr(int irq, void *dev_id, struct pt_regs * regs)
872 {
873         struct net_device     *dev;
874         struct frad_local *flp;
875         char              byte;
876
877         dev = dev_id;
878
879         if (dev == NULL)
880         {
881                 printk(KERN_WARNING "sdla_isr(): irq %d for unknown device.\n", irq);
882                 return IRQ_NONE;
883         }
884
885         flp = dev->priv;
886
887         if (!flp->initialized)
888         {
889                 printk(KERN_WARNING "%s: irq %d for uninitialized device.\n", dev->name, irq);
890                 return IRQ_NONE;
891         }
892
893         byte = sdla_byte(dev, flp->type == SDLA_S508 ? SDLA_508_IRQ_INTERFACE : SDLA_502_IRQ_INTERFACE);
894         switch (byte)
895         {
896                 case SDLA_INTR_RX:
897                         sdla_receive(dev);
898                         break;
899
900                 /* the command will get an error return, which is processed above */
901                 case SDLA_INTR_MODEM:
902                 case SDLA_INTR_STATUS:
903                         sdla_cmd(dev, SDLA_READ_DLC_STATUS, 0, 0, NULL, 0, NULL, NULL);
904                         break;
905
906                 case SDLA_INTR_TX:
907                 case SDLA_INTR_COMPLETE:
908                 case SDLA_INTR_TIMER:
909                         printk(KERN_WARNING "%s: invalid irq flag 0x%02X.\n", dev->name, byte);
910                         break;
911         }
912
913         /* the S502E requires a manual acknowledgement of the interrupt */ 
914         if (flp->type == SDLA_S502E)
915         {
916                 flp->state &= ~SDLA_S502E_INTACK;
917                 outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
918                 flp->state |= SDLA_S502E_INTACK;
919                 outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
920         }
921
922         /* this clears the byte, informing the Z80 we're done */
923         byte = 0;
924         sdla_write(dev, flp->type == SDLA_S508 ? SDLA_508_IRQ_INTERFACE : SDLA_502_IRQ_INTERFACE, &byte, sizeof(byte));
925         return IRQ_HANDLED;
926 }
927
928 static void sdla_poll(unsigned long device)
929 {
930         struct net_device         *dev;
931         struct frad_local *flp;
932
933         dev = (struct net_device *) device;
934         flp = dev->priv;
935
936         if (sdla_byte(dev, SDLA_502_RCV_BUF))
937                 sdla_receive(dev);
938
939         flp->timer.expires = 1;
940         add_timer(&flp->timer);
941 }
942
943 static int sdla_close(struct net_device *dev)
944 {
945         struct frad_local *flp;
946         struct intr_info  intr;
947         int               len, i;
948         short             dlcis[CONFIG_DLCI_MAX];
949
950         flp = dev->priv;
951
952         len = 0;
953         for(i=0;i<CONFIG_DLCI_MAX;i++)
954                 if (flp->dlci[i])
955                         dlcis[len++] = abs(flp->dlci[i]);
956         len *= 2;
957
958         if (flp->config.station == FRAD_STATION_NODE)
959         {
960                 for(i=0;i<CONFIG_DLCI_MAX;i++)
961                         if (flp->dlci[i] > 0) 
962                                 sdla_cmd(dev, SDLA_DEACTIVATE_DLCI, 0, 0, dlcis, len, NULL, NULL);
963                 sdla_cmd(dev, SDLA_DELETE_DLCI, 0, 0, &flp->dlci[i], sizeof(flp->dlci[i]), NULL, NULL);
964         }
965
966         memset(&intr, 0, sizeof(intr));
967         /* let's start up the reception */
968         switch(flp->type)
969         {
970                 case SDLA_S502A:
971                         del_timer(&flp->timer); 
972                         break;
973
974                 case SDLA_S502E:
975                         sdla_cmd(dev, SDLA_SET_IRQ_TRIGGER, 0, 0, &intr, sizeof(char) + sizeof(short), NULL, NULL);
976                         flp->state &= ~SDLA_S502E_INTACK;
977                         outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
978                         break;
979
980                 case SDLA_S507:
981                         break;
982
983                 case SDLA_S508:
984                         sdla_cmd(dev, SDLA_SET_IRQ_TRIGGER, 0, 0, &intr, sizeof(struct intr_info), NULL, NULL);
985                         flp->state &= ~SDLA_S508_INTEN;
986                         outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
987                         break;
988         }
989
990         sdla_cmd(dev, SDLA_DISABLE_COMMUNICATIONS, 0, 0, NULL, 0, NULL, NULL);
991
992         netif_stop_queue(dev);
993         
994         return(0);
995 }
996
997 struct conf_data {
998         struct frad_conf config;
999         short            dlci[CONFIG_DLCI_MAX];
1000 };
1001
1002 static int sdla_open(struct net_device *dev)
1003 {
1004         struct frad_local *flp;
1005         struct dlci_local *dlp;
1006         struct conf_data  data;
1007         struct intr_info  intr;
1008         int               len, i;
1009         char              byte;
1010
1011         flp = dev->priv;
1012
1013         if (!flp->initialized)
1014                 return(-EPERM);
1015
1016         if (!flp->configured)
1017                 return(-EPERM);
1018
1019         /* time to send in the configuration */
1020         len = 0;
1021         for(i=0;i<CONFIG_DLCI_MAX;i++)
1022                 if (flp->dlci[i])
1023                         data.dlci[len++] = abs(flp->dlci[i]);
1024         len *= 2;
1025
1026         memcpy(&data.config, &flp->config, sizeof(struct frad_conf));
1027         len += sizeof(struct frad_conf);
1028
1029         sdla_cmd(dev, SDLA_DISABLE_COMMUNICATIONS, 0, 0, NULL, 0, NULL, NULL);
1030         sdla_cmd(dev, SDLA_SET_DLCI_CONFIGURATION, 0, 0, &data, len, NULL, NULL);
1031
1032         if (flp->type == SDLA_S508)
1033                 flp->buffer = 0;
1034
1035         sdla_cmd(dev, SDLA_ENABLE_COMMUNICATIONS, 0, 0, NULL, 0, NULL, NULL);
1036
1037         /* let's start up the reception */
1038         memset(&intr, 0, sizeof(intr));
1039         switch(flp->type)
1040         {
1041                 case SDLA_S502A:
1042                         flp->timer.expires = 1;
1043                         add_timer(&flp->timer);
1044                         break;
1045
1046                 case SDLA_S502E:
1047                         flp->state |= SDLA_S502E_ENABLE;
1048                         outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
1049                         flp->state |= SDLA_S502E_INTACK;
1050                         outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
1051                         byte = 0;
1052                         sdla_write(dev, SDLA_502_IRQ_INTERFACE, &byte, sizeof(byte));
1053                         intr.flags = SDLA_INTR_RX | SDLA_INTR_STATUS | SDLA_INTR_MODEM;
1054                         sdla_cmd(dev, SDLA_SET_IRQ_TRIGGER, 0, 0, &intr, sizeof(char) + sizeof(short), NULL, NULL);
1055                         break;
1056
1057                 case SDLA_S507:
1058                         break;
1059
1060                 case SDLA_S508:
1061                         flp->state |= SDLA_S508_INTEN;
1062                         outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
1063                         byte = 0;
1064                         sdla_write(dev, SDLA_508_IRQ_INTERFACE, &byte, sizeof(byte));
1065                         intr.flags = SDLA_INTR_RX | SDLA_INTR_STATUS | SDLA_INTR_MODEM;
1066                         intr.irq = dev->irq;
1067                         sdla_cmd(dev, SDLA_SET_IRQ_TRIGGER, 0, 0, &intr, sizeof(struct intr_info), NULL, NULL);
1068                         break;
1069         }
1070
1071         if (flp->config.station == FRAD_STATION_CPE)
1072         {
1073                 byte = SDLA_ICS_STATUS_ENQ;
1074                 sdla_cmd(dev, SDLA_ISSUE_IN_CHANNEL_SIGNAL, 0, 0, &byte, sizeof(byte), NULL, NULL);
1075         }
1076         else
1077         {
1078                 sdla_cmd(dev, SDLA_ADD_DLCI, 0, 0, data.dlci, len - sizeof(struct frad_conf), NULL, NULL);
1079                 for(i=0;i<CONFIG_DLCI_MAX;i++)
1080                         if (flp->dlci[i] > 0)
1081                                 sdla_cmd(dev, SDLA_ACTIVATE_DLCI, 0, 0, &flp->dlci[i], 2*sizeof(flp->dlci[i]), NULL, NULL);
1082         }
1083
1084         /* configure any specific DLCI settings */
1085         for(i=0;i<CONFIG_DLCI_MAX;i++)
1086                 if (flp->dlci[i])
1087                 {
1088                         dlp = flp->master[i]->priv;
1089                         if (dlp->configured)
1090                                 sdla_cmd(dev, SDLA_SET_DLCI_CONFIGURATION, abs(flp->dlci[i]), 0, &dlp->config, sizeof(struct dlci_conf), NULL, NULL);
1091                 }
1092
1093         netif_start_queue(dev);
1094         
1095         return(0);
1096 }
1097
1098 static int sdla_config(struct net_device *dev, struct frad_conf __user *conf, int get)
1099 {
1100         struct frad_local *flp;
1101         struct conf_data  data;
1102         int               i;
1103         short             size;
1104
1105         if (dev->type == 0xFFFF)
1106                 return(-EUNATCH);
1107
1108         flp = dev->priv;
1109
1110         if (!get)
1111         {
1112                 if (netif_running(dev))
1113                         return(-EBUSY);
1114
1115                 if(copy_from_user(&data.config, conf, sizeof(struct frad_conf)))
1116                         return -EFAULT;
1117
1118                 if (data.config.station & ~FRAD_STATION_NODE)
1119                         return(-EINVAL);
1120
1121                 if (data.config.flags & ~FRAD_VALID_FLAGS)
1122                         return(-EINVAL);
1123
1124                 if ((data.config.kbaud < 0) || 
1125                          ((data.config.kbaud > 128) && (flp->type != SDLA_S508)))
1126                         return(-EINVAL);
1127
1128                 if (data.config.clocking & ~(FRAD_CLOCK_INT | SDLA_S508_PORT_RS232))
1129                         return(-EINVAL);
1130
1131                 if ((data.config.mtu < 0) || (data.config.mtu > SDLA_MAX_MTU))
1132                         return(-EINVAL);
1133
1134                 if ((data.config.T391 < 5) || (data.config.T391 > 30))
1135                         return(-EINVAL);
1136
1137                 if ((data.config.T392 < 5) || (data.config.T392 > 30))
1138                         return(-EINVAL);
1139
1140                 if ((data.config.N391 < 1) || (data.config.N391 > 255))
1141                         return(-EINVAL);
1142
1143                 if ((data.config.N392 < 1) || (data.config.N392 > 10))
1144                         return(-EINVAL);
1145
1146                 if ((data.config.N393 < 1) || (data.config.N393 > 10))
1147                         return(-EINVAL);
1148
1149                 memcpy(&flp->config, &data.config, sizeof(struct frad_conf));
1150                 flp->config.flags |= SDLA_DIRECT_RECV;
1151
1152                 if (flp->type == SDLA_S508)
1153                         flp->config.flags |= SDLA_TX70_RX30;
1154
1155                 if (dev->mtu != flp->config.mtu)
1156                 {
1157                         /* this is required to change the MTU */
1158                         dev->mtu = flp->config.mtu;
1159                         for(i=0;i<CONFIG_DLCI_MAX;i++)
1160                                 if (flp->master[i])
1161                                         flp->master[i]->mtu = flp->config.mtu;
1162                 }
1163
1164                 flp->config.mtu += sizeof(struct frhdr);
1165
1166                 /* off to the races! */
1167                 if (!flp->configured)
1168                         sdla_start(dev);
1169
1170                 flp->configured = 1;
1171         }
1172         else
1173         {
1174                 /* no sense reading if the CPU isn't started */
1175                 if (netif_running(dev))
1176                 {
1177                         size = sizeof(data);
1178                         if (sdla_cmd(dev, SDLA_READ_DLCI_CONFIGURATION, 0, 0, NULL, 0, &data, &size) != SDLA_RET_OK)
1179                                 return(-EIO);
1180                 }
1181                 else
1182                         if (flp->configured)
1183                                 memcpy(&data.config, &flp->config, sizeof(struct frad_conf));
1184                         else
1185                                 memset(&data.config, 0, sizeof(struct frad_conf));
1186
1187                 memcpy(&flp->config, &data.config, sizeof(struct frad_conf));
1188                 data.config.flags &= FRAD_VALID_FLAGS;
1189                 data.config.mtu -= data.config.mtu > sizeof(struct frhdr) ? sizeof(struct frhdr) : data.config.mtu;
1190                 return copy_to_user(conf, &data.config, sizeof(struct frad_conf))?-EFAULT:0;
1191         }
1192
1193         return(0);
1194 }
1195
1196 static int sdla_xfer(struct net_device *dev, struct sdla_mem __user *info, int read)
1197 {
1198         struct sdla_mem mem;
1199         char    *temp;
1200
1201         if(copy_from_user(&mem, info, sizeof(mem)))
1202                 return -EFAULT;
1203                 
1204         if (read)
1205         {       
1206                 temp = kmalloc(mem.len, GFP_KERNEL);
1207                 if (!temp)
1208                         return(-ENOMEM);
1209                 memset(temp, 0, mem.len);
1210                 sdla_read(dev, mem.addr, temp, mem.len);
1211                 if(copy_to_user(mem.data, temp, mem.len))
1212                 {
1213                         kfree(temp);
1214                         return -EFAULT;
1215                 }
1216                 kfree(temp);
1217         }
1218         else
1219         {
1220                 temp = kmalloc(mem.len, GFP_KERNEL);
1221                 if (!temp)
1222                         return(-ENOMEM);
1223                 if(copy_from_user(temp, mem.data, mem.len))
1224                 {
1225                         kfree(temp);
1226                         return -EFAULT;
1227                 }
1228                 sdla_write(dev, mem.addr, temp, mem.len);
1229                 kfree(temp);
1230         }
1231         return(0);
1232 }
1233
1234 static int sdla_reconfig(struct net_device *dev)
1235 {
1236         struct frad_local *flp;
1237         struct conf_data  data;
1238         int               i, len;
1239
1240         flp = dev->priv;
1241
1242         len = 0;
1243         for(i=0;i<CONFIG_DLCI_MAX;i++)
1244                 if (flp->dlci[i])
1245                         data.dlci[len++] = flp->dlci[i];
1246         len *= 2;
1247
1248         memcpy(&data, &flp->config, sizeof(struct frad_conf));
1249         len += sizeof(struct frad_conf);
1250
1251         sdla_cmd(dev, SDLA_DISABLE_COMMUNICATIONS, 0, 0, NULL, 0, NULL, NULL);
1252         sdla_cmd(dev, SDLA_SET_DLCI_CONFIGURATION, 0, 0, &data, len, NULL, NULL);
1253         sdla_cmd(dev, SDLA_ENABLE_COMMUNICATIONS, 0, 0, NULL, 0, NULL, NULL);
1254
1255         return(0);
1256 }
1257
1258 static int sdla_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1259 {
1260         struct frad_local *flp;
1261
1262         if(!capable(CAP_NET_ADMIN))
1263                 return -EPERM;
1264                 
1265         flp = dev->priv;
1266
1267         if (!flp->initialized)
1268                 return(-EINVAL);
1269
1270         switch (cmd)
1271         {
1272                 case FRAD_GET_CONF:
1273                 case FRAD_SET_CONF:
1274                         return(sdla_config(dev, ifr->ifr_data, cmd == FRAD_GET_CONF));
1275
1276                 case SDLA_IDENTIFY:
1277                         ifr->ifr_flags = flp->type;
1278                         break;
1279
1280                 case SDLA_CPUSPEED:
1281                         return(sdla_cpuspeed(dev, ifr)); 
1282
1283 /* ==========================================================
1284 NOTE:  This is rather a useless action right now, as the
1285        current driver does not support protocols other than
1286        FR.  However, Sangoma has modules for a number of
1287        other protocols in the works.
1288 ============================================================*/
1289                 case SDLA_PROTOCOL:
1290                         if (flp->configured)
1291                                 return(-EALREADY);
1292
1293                         switch (ifr->ifr_flags)
1294                         {
1295                                 case ARPHRD_FRAD:
1296                                         dev->type = ifr->ifr_flags;
1297                                         break;
1298                                 default:
1299                                         return(-ENOPROTOOPT);
1300                         }
1301                         break;
1302
1303                 case SDLA_CLEARMEM:
1304                         sdla_clear(dev);
1305                         break;
1306
1307                 case SDLA_WRITEMEM:
1308                 case SDLA_READMEM:
1309                         if(!capable(CAP_SYS_RAWIO))
1310                                 return -EPERM;
1311                         return(sdla_xfer(dev, ifr->ifr_data, cmd == SDLA_READMEM));
1312
1313                 case SDLA_START:
1314                         sdla_start(dev);
1315                         break;
1316
1317                 case SDLA_STOP:
1318                         sdla_stop(dev);
1319                         break;
1320
1321                 default:
1322                         return(-EOPNOTSUPP);
1323         }
1324         return(0);
1325 }
1326
1327 static int sdla_change_mtu(struct net_device *dev, int new_mtu)
1328 {
1329         struct frad_local *flp;
1330
1331         flp = dev->priv;
1332
1333         if (netif_running(dev))
1334                 return(-EBUSY);
1335
1336         /* for now, you can't change the MTU! */
1337         return(-EOPNOTSUPP);
1338 }
1339
1340 static int sdla_set_config(struct net_device *dev, struct ifmap *map)
1341 {
1342         struct frad_local *flp;
1343         int               i;
1344         char              byte;
1345         unsigned base;
1346         int err = -EINVAL;
1347
1348         flp = dev->priv;
1349
1350         if (flp->initialized)
1351                 return(-EINVAL);
1352
1353         for(i=0;i < sizeof(valid_port) / sizeof (int) ; i++)
1354                 if (valid_port[i] == map->base_addr)
1355                         break;   
1356
1357         if (i == sizeof(valid_port) / sizeof(int))
1358                 return(-EINVAL);
1359
1360         if (!request_region(map->base_addr, SDLA_IO_EXTENTS, dev->name)){
1361                 printk(KERN_WARNING "SDLA: io-port 0x%04lx in use \n", dev->base_addr);
1362                 return(-EINVAL);
1363         }
1364         base = map->base_addr;
1365
1366         /* test for card types, S502A, S502E, S507, S508                 */
1367         /* these tests shut down the card completely, so clear the state */
1368         flp->type = SDLA_UNKNOWN;
1369         flp->state = 0;
1370    
1371         for(i=1;i<SDLA_IO_EXTENTS;i++)
1372                 if (inb(base + i) != 0xFF)
1373                         break;
1374
1375         if (i == SDLA_IO_EXTENTS) {   
1376                 outb(SDLA_HALT, base + SDLA_REG_Z80_CONTROL);
1377                 if ((inb(base + SDLA_S502_STS) & 0x0F) == 0x08) {
1378                         outb(SDLA_S502E_INTACK, base + SDLA_REG_CONTROL);
1379                         if ((inb(base + SDLA_S502_STS) & 0x0F) == 0x0C) {
1380                                 outb(SDLA_HALT, base + SDLA_REG_CONTROL);
1381                                 flp->type = SDLA_S502E;
1382                                 goto got_type;
1383                         }
1384                 }
1385         }
1386
1387         for(byte=inb(base),i=0;i<SDLA_IO_EXTENTS;i++)
1388                 if (inb(base + i) != byte)
1389                         break;
1390
1391         if (i == SDLA_IO_EXTENTS) {
1392                 outb(SDLA_HALT, base + SDLA_REG_CONTROL);
1393                 if ((inb(base + SDLA_S502_STS) & 0x7E) == 0x30) {
1394                         outb(SDLA_S507_ENABLE, base + SDLA_REG_CONTROL);
1395                         if ((inb(base + SDLA_S502_STS) & 0x7E) == 0x32) {
1396                                 outb(SDLA_HALT, base + SDLA_REG_CONTROL);
1397                                 flp->type = SDLA_S507;
1398                                 goto got_type;
1399                         }
1400                 }
1401         }
1402
1403         outb(SDLA_HALT, base + SDLA_REG_CONTROL);
1404         if ((inb(base + SDLA_S508_STS) & 0x3F) == 0x00) {
1405                 outb(SDLA_S508_INTEN, base + SDLA_REG_CONTROL);
1406                 if ((inb(base + SDLA_S508_STS) & 0x3F) == 0x10) {
1407                         outb(SDLA_HALT, base + SDLA_REG_CONTROL);
1408                         flp->type = SDLA_S508;
1409                         goto got_type;
1410                 }
1411         }
1412
1413         outb(SDLA_S502A_HALT, base + SDLA_REG_CONTROL);
1414         if (inb(base + SDLA_S502_STS) == 0x40) {
1415                 outb(SDLA_S502A_START, base + SDLA_REG_CONTROL);
1416                 if (inb(base + SDLA_S502_STS) == 0x40) {
1417                         outb(SDLA_S502A_INTEN, base + SDLA_REG_CONTROL);
1418                         if (inb(base + SDLA_S502_STS) == 0x44) {
1419                                 outb(SDLA_S502A_START, base + SDLA_REG_CONTROL);
1420                                 flp->type = SDLA_S502A;
1421                                 goto got_type;
1422                         }
1423                 }
1424         }
1425
1426         printk(KERN_NOTICE "%s: Unknown card type\n", dev->name);
1427         err = -ENODEV;
1428         goto fail;
1429
1430 got_type:
1431         switch(base) {
1432                 case 0x270:
1433                 case 0x280:
1434                 case 0x380: 
1435                 case 0x390:
1436                         if (flp->type != SDLA_S508 && flp->type != SDLA_S507)
1437                                 goto fail;
1438         }
1439
1440         switch (map->irq) {
1441                 case 2:
1442                         if (flp->type != SDLA_S502E)
1443                                 goto fail;
1444                         break;
1445
1446                 case 10:
1447                 case 11:
1448                 case 12:
1449                 case 15:
1450                 case 4:
1451                         if (flp->type != SDLA_S508 && flp->type != SDLA_S507)
1452                                 goto fail;
1453                         break;
1454                 case 3:
1455                 case 5:
1456                 case 7:
1457                         if (flp->type == SDLA_S502A)
1458                                 goto fail;
1459                         break;
1460
1461                 default:
1462                         goto fail;
1463         }
1464
1465         err = -EAGAIN;
1466         if (request_irq(dev->irq, &sdla_isr, 0, dev->name, dev)) 
1467                 goto fail;
1468
1469         if (flp->type == SDLA_S507) {
1470                 switch(dev->irq) {
1471                         case 3:
1472                                 flp->state = SDLA_S507_IRQ3;
1473                                 break;
1474                         case 4:
1475                                 flp->state = SDLA_S507_IRQ4;
1476                                 break;
1477                         case 5:
1478                                 flp->state = SDLA_S507_IRQ5;
1479                                 break;
1480                         case 7:
1481                                 flp->state = SDLA_S507_IRQ7;
1482                                 break;
1483                         case 10:
1484                                 flp->state = SDLA_S507_IRQ10;
1485                                 break;
1486                         case 11:
1487                                 flp->state = SDLA_S507_IRQ11;
1488                                 break;
1489                         case 12:
1490                                 flp->state = SDLA_S507_IRQ12;
1491                                 break;
1492                         case 15:
1493                                 flp->state = SDLA_S507_IRQ15;
1494                                 break;
1495                 }
1496         }
1497
1498         for(i=0;i < sizeof(valid_mem) / sizeof (int) ; i++)
1499                 if (valid_mem[i] == map->mem_start)
1500                         break;   
1501
1502         err = -EINVAL;
1503         if (i == sizeof(valid_mem) / sizeof(int))
1504                 goto fail2;
1505
1506         if (flp->type == SDLA_S502A && (map->mem_start & 0xF000) >> 12 == 0x0E)
1507                 goto fail2;
1508
1509         if (flp->type != SDLA_S507 && map->mem_start >> 16 == 0x0B)
1510                 goto fail2;
1511
1512         if (flp->type == SDLA_S507 && map->mem_start >> 16 == 0x0D)
1513                 goto fail2;
1514
1515         byte = flp->type != SDLA_S508 ? SDLA_8K_WINDOW : 0;
1516         byte |= (map->mem_start & 0xF000) >> (12 + (flp->type == SDLA_S508 ? 1 : 0));
1517         switch(flp->type) {
1518                 case SDLA_S502A:
1519                 case SDLA_S502E:
1520                         switch (map->mem_start >> 16) {
1521                                 case 0x0A:
1522                                         byte |= SDLA_S502_SEG_A;
1523                                         break;
1524                                 case 0x0C:
1525                                         byte |= SDLA_S502_SEG_C;
1526                                         break;
1527                                 case 0x0D:
1528                                         byte |= SDLA_S502_SEG_D;
1529                                         break;
1530                                 case 0x0E:
1531                                         byte |= SDLA_S502_SEG_E;
1532                                         break;
1533                         }
1534                         break;
1535                 case SDLA_S507:
1536                         switch (map->mem_start >> 16) {
1537                                 case 0x0A:
1538                                         byte |= SDLA_S507_SEG_A;
1539                                         break;
1540                                 case 0x0B:
1541                                         byte |= SDLA_S507_SEG_B;
1542                                         break;
1543                                 case 0x0C:
1544                                         byte |= SDLA_S507_SEG_C;
1545                                         break;
1546                                 case 0x0E:
1547                                         byte |= SDLA_S507_SEG_E;
1548                                         break;
1549                         }
1550                         break;
1551                 case SDLA_S508:
1552                         switch (map->mem_start >> 16) {
1553                                 case 0x0A:
1554                                         byte |= SDLA_S508_SEG_A;
1555                                         break;
1556                                 case 0x0C:
1557                                         byte |= SDLA_S508_SEG_C;
1558                                         break;
1559                                 case 0x0D:
1560                                         byte |= SDLA_S508_SEG_D;
1561                                         break;
1562                                 case 0x0E:
1563                                         byte |= SDLA_S508_SEG_E;
1564                                         break;
1565                         }
1566                         break;
1567         }
1568
1569         /* set the memory bits, and enable access */
1570         outb(byte, base + SDLA_REG_PC_WINDOW);
1571
1572         switch(flp->type)
1573         {
1574                 case SDLA_S502E:
1575                         flp->state = SDLA_S502E_ENABLE;
1576                         break;
1577                 case SDLA_S507:
1578                         flp->state |= SDLA_MEMEN;
1579                         break;
1580                 case SDLA_S508:
1581                         flp->state = SDLA_MEMEN;
1582                         break;
1583         }
1584         outb(flp->state, base + SDLA_REG_CONTROL);
1585
1586         dev->irq = map->irq;
1587         dev->base_addr = base;
1588         dev->mem_start = map->mem_start;
1589         dev->mem_end = dev->mem_start + 0x2000;
1590         flp->initialized = 1;
1591         return 0;
1592
1593 fail2:
1594         free_irq(map->irq, dev);
1595 fail:
1596         release_region(base, SDLA_IO_EXTENTS);
1597         return err;
1598 }
1599  
1600 static struct net_device_stats *sdla_stats(struct net_device *dev)
1601 {
1602         struct frad_local *flp;
1603         flp = dev->priv;
1604
1605         return(&flp->stats);
1606 }
1607
1608 static void setup_sdla(struct net_device *dev)
1609 {
1610         struct frad_local *flp = dev->priv;
1611
1612         netdev_boot_setup_check(dev);
1613
1614         SET_MODULE_OWNER(dev);
1615         dev->flags              = 0;
1616         dev->type               = 0xFFFF;
1617         dev->hard_header_len    = 0;
1618         dev->addr_len           = 0;
1619         dev->mtu                = SDLA_MAX_MTU;
1620
1621         dev->open               = sdla_open;
1622         dev->stop               = sdla_close;
1623         dev->do_ioctl           = sdla_ioctl;
1624         dev->set_config         = sdla_set_config;
1625         dev->get_stats          = sdla_stats;
1626         dev->hard_start_xmit    = sdla_transmit;
1627         dev->change_mtu         = sdla_change_mtu;
1628
1629         flp->activate           = sdla_activate;
1630         flp->deactivate         = sdla_deactivate;
1631         flp->assoc              = sdla_assoc;
1632         flp->deassoc            = sdla_deassoc;
1633         flp->dlci_conf          = sdla_dlci_conf;
1634
1635         init_timer(&flp->timer);
1636         flp->timer.expires      = 1;
1637         flp->timer.data         = (unsigned long) dev;
1638         flp->timer.function     = sdla_poll;
1639 }
1640
1641 static struct net_device *sdla;
1642
1643 static int __init init_sdla(void)
1644 {
1645         int err;
1646
1647         printk("%s.\n", version);
1648
1649         sdla = alloc_netdev(sizeof(struct frad_local), "sdla0", setup_sdla);
1650         if (!sdla) 
1651                 return -ENOMEM;
1652
1653         err = register_netdev(sdla);
1654         if (err) 
1655                 free_netdev(sdla);
1656
1657         return err;
1658 }
1659
1660 static void __exit exit_sdla(void)
1661 {
1662         struct frad_local *flp = sdla->priv;
1663
1664         unregister_netdev(sdla);
1665         if (flp->initialized) {
1666                 free_irq(sdla->irq, sdla);
1667                 release_region(sdla->base_addr, SDLA_IO_EXTENTS);
1668         }
1669         del_timer_sync(&flp->timer);
1670         free_netdev(sdla);
1671 }
1672
1673 MODULE_LICENSE("GPL");
1674
1675 module_init(init_sdla);
1676 module_exit(exit_sdla);