linux/drivers/net/wan/sdla.c
<<
>>
Prefs
   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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  36
  37#include <linux/module.h>
  38#include <linux/kernel.h>
  39#include <linux/types.h>
  40#include <linux/fcntl.h>
  41#include <linux/interrupt.h>
  42#include <linux/ptrace.h>
  43#include <linux/ioport.h>
  44#include <linux/in.h>
  45#include <linux/slab.h>
  46#include <linux/string.h>
  47#include <linux/timer.h>
  48#include <linux/errno.h>
  49#include <linux/init.h>
  50#include <linux/netdevice.h>
  51#include <linux/skbuff.h>
  52#include <linux/if_arp.h>
  53#include <linux/if_frad.h>
  54#include <linux/sdla.h>
  55#include <linux/bitops.h>
  56
  57#include <asm/io.h>
  58#include <asm/dma.h>
  59#include <linux/uaccess.h>
  60
  61static const char* version = "SDLA driver v0.30, 12 Sep 1996, mike.mclagan@linux.org";
  62
  63static unsigned int valid_port[] = { 0x250, 0x270, 0x280, 0x300, 0x350, 0x360, 0x380, 0x390};
  64
  65static unsigned int valid_mem[] = {
  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
  72static 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
  82static 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
 104static 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
 112static 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
 135static 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
 146static 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
 170static 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
 185static void sdla_stop(struct net_device *dev)
 186{
 187        struct frad_local *flp;
 188
 189        flp = netdev_priv(dev);
 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
 212static void sdla_start(struct net_device *dev)
 213{
 214        struct frad_local *flp;
 215
 216        flp = netdev_priv(dev);
 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
 250static 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
 280static 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
 330struct _dlci_stat 
 331{
 332        short dlci;
 333        char  flags;
 334} __packed;
 335
 336struct _frad_stat 
 337{
 338        char    flags;
 339        struct _dlci_stat dlcis[SDLA_MAX_DLCI];
 340};
 341
 342static 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                                netdev_info(dev, "Modem DCD unexpectedly low!\n");
 355                        if (*state & SDLA_MODEM_CTS_LOW)
 356                                netdev_info(dev, "Modem CTS unexpectedly low!\n");
 357                        /* I should probably do something about this! */
 358                        break;
 359
 360                case SDLA_RET_CHANNEL_OFF:
 361                        netdev_info(dev, "Channel became inoperative!\n");
 362                        /* same here */
 363                        break;
 364
 365                case SDLA_RET_CHANNEL_ON:
 366                        netdev_info(dev, "Channel became operative!\n");
 367                        /* same here */
 368                        break;
 369
 370                case SDLA_RET_DLCI_STATUS:
 371                        netdev_info(dev, "Status change reported by Access Node\n");
 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                                netdev_info(dev, "DLCI %i: %s\n",
 387                                            pstatus->dlci, state);
 388                                /* same here */
 389                        }
 390                        break;
 391
 392                case SDLA_RET_DLCI_UNKNOWN:
 393                        netdev_info(dev, "Received unknown DLCIs:");
 394                        len /= sizeof(short);
 395                        for(pdlci = data,i=0;i < len;i++,pdlci++)
 396                                pr_cont(" %i", *pdlci);
 397                        pr_cont("\n");
 398                        break;
 399
 400                case SDLA_RET_TIMEOUT:
 401                        netdev_err(dev, "Command timed out!\n");
 402                        break;
 403
 404                case SDLA_RET_BUF_OVERSIZE:
 405                        netdev_info(dev, "Bc/CIR overflow, acceptable size is %i\n",
 406                                    len);
 407                        break;
 408
 409                case SDLA_RET_BUF_TOO_BIG:
 410                        netdev_info(dev, "Buffer size over specified max of %i\n",
 411                                    len);
 412                        break;
 413
 414                case SDLA_RET_CHANNEL_INACTIVE:
 415                case SDLA_RET_DLCI_INACTIVE:
 416                case SDLA_RET_CIR_OVERFLOW:
 417                case SDLA_RET_NO_BUFS:
 418                        if (cmd == SDLA_INFORMATION_WRITE)
 419                                break;
 420
 421                default: 
 422                        netdev_dbg(dev, "Cmd 0x%02X generated return code 0x%02X\n",
 423                                   cmd, ret);
 424                        /* Further processing could be done here */
 425                        break;
 426        }
 427}
 428
 429static int sdla_cmd(struct net_device *dev, int cmd, short dlci, short flags, 
 430                        void *inbuf, short inlen, void *outbuf, short *outlen)
 431{
 432        static struct _frad_stat status;
 433        struct frad_local        *flp;
 434        struct sdla_cmd          *cmd_buf;
 435        unsigned long            pflags;
 436        unsigned long            jiffs;
 437        int                      ret, waiting, len;
 438        long                     window;
 439
 440        flp = netdev_priv(dev);
 441        window = flp->type == SDLA_S508 ? SDLA_508_CMD_BUF : SDLA_502_CMD_BUF;
 442        cmd_buf = (struct sdla_cmd *)(dev->mem_start + (window & SDLA_ADDR_MASK));
 443        ret = 0;
 444        len = 0;
 445        jiffs = jiffies + HZ;  /* 1 second is plenty */
 446
 447        spin_lock_irqsave(&sdla_lock, pflags);
 448        SDLA_WINDOW(dev, window);
 449        cmd_buf->cmd = cmd;
 450        cmd_buf->dlci = dlci;
 451        cmd_buf->flags = flags;
 452
 453        if (inbuf)
 454                memcpy(cmd_buf->data, inbuf, inlen);
 455
 456        cmd_buf->length = inlen;
 457
 458        cmd_buf->opp_flag = 1;
 459        spin_unlock_irqrestore(&sdla_lock, pflags);
 460
 461        waiting = 1;
 462        len = 0;
 463        while (waiting && time_before_eq(jiffies, jiffs))
 464        {
 465                if (waiting++ % 3) 
 466                {
 467                        spin_lock_irqsave(&sdla_lock, pflags);
 468                        SDLA_WINDOW(dev, window);
 469                        waiting = ((volatile int)(cmd_buf->opp_flag));
 470                        spin_unlock_irqrestore(&sdla_lock, pflags);
 471                }
 472        }
 473        
 474        if (!waiting)
 475        {
 476
 477                spin_lock_irqsave(&sdla_lock, pflags);
 478                SDLA_WINDOW(dev, window);
 479                ret = cmd_buf->retval;
 480                len = cmd_buf->length;
 481                if (outbuf && outlen)
 482                {
 483                        *outlen = *outlen >= len ? len : *outlen;
 484
 485                        if (*outlen)
 486                                memcpy(outbuf, cmd_buf->data, *outlen);
 487                }
 488
 489                /* This is a local copy that's used for error handling */
 490                if (ret)
 491                        memcpy(&status, cmd_buf->data, len > sizeof(status) ? sizeof(status) : len);
 492
 493                spin_unlock_irqrestore(&sdla_lock, pflags);
 494        }
 495        else
 496                ret = SDLA_RET_TIMEOUT;
 497
 498        if (ret != SDLA_RET_OK)
 499                sdla_errors(dev, cmd, dlci, ret, len, &status);
 500
 501        return ret;
 502}
 503
 504/***********************************************
 505 *
 506 * these functions are called by the DLCI driver 
 507 *
 508 ***********************************************/
 509
 510static int sdla_reconfig(struct net_device *dev);
 511
 512static int sdla_activate(struct net_device *slave, struct net_device *master)
 513{
 514        struct frad_local *flp;
 515        int i;
 516
 517        flp = netdev_priv(slave);
 518
 519        for(i=0;i<CONFIG_DLCI_MAX;i++)
 520                if (flp->master[i] == master)
 521                        break;
 522
 523        if (i == CONFIG_DLCI_MAX)
 524                return -ENODEV;
 525
 526        flp->dlci[i] = abs(flp->dlci[i]);
 527
 528        if (netif_running(slave) && (flp->config.station == FRAD_STATION_NODE))
 529                sdla_cmd(slave, SDLA_ACTIVATE_DLCI, 0, 0, &flp->dlci[i], sizeof(short), NULL, NULL);
 530
 531        return 0;
 532}
 533
 534static int sdla_deactivate(struct net_device *slave, struct net_device *master)
 535{
 536        struct frad_local *flp;
 537        int               i;
 538
 539        flp = netdev_priv(slave);
 540
 541        for(i=0;i<CONFIG_DLCI_MAX;i++)
 542                if (flp->master[i] == master)
 543                        break;
 544
 545        if (i == CONFIG_DLCI_MAX)
 546                return -ENODEV;
 547
 548        flp->dlci[i] = -abs(flp->dlci[i]);
 549
 550        if (netif_running(slave) && (flp->config.station == FRAD_STATION_NODE))
 551                sdla_cmd(slave, SDLA_DEACTIVATE_DLCI, 0, 0, &flp->dlci[i], sizeof(short), NULL, NULL);
 552
 553        return 0;
 554}
 555
 556static int sdla_assoc(struct net_device *slave, struct net_device *master)
 557{
 558        struct frad_local *flp;
 559        int               i;
 560
 561        if (master->type != ARPHRD_DLCI)
 562                return -EINVAL;
 563
 564        flp = netdev_priv(slave);
 565
 566        for(i=0;i<CONFIG_DLCI_MAX;i++)
 567        {
 568                if (!flp->master[i])
 569                        break;
 570                if (abs(flp->dlci[i]) == *(short *)(master->dev_addr))
 571                        return -EADDRINUSE;
 572        } 
 573
 574        if (i == CONFIG_DLCI_MAX)
 575                return -EMLINK;  /* #### Alan: Comments on this ?? */
 576
 577
 578        flp->master[i] = master;
 579        flp->dlci[i] = -*(short *)(master->dev_addr);
 580        master->mtu = slave->mtu;
 581
 582        if (netif_running(slave)) {
 583                if (flp->config.station == FRAD_STATION_CPE)
 584                        sdla_reconfig(slave);
 585                else
 586                        sdla_cmd(slave, SDLA_ADD_DLCI, 0, 0, master->dev_addr, sizeof(short), NULL, NULL);
 587        }
 588
 589        return 0;
 590}
 591
 592static int sdla_deassoc(struct net_device *slave, struct net_device *master)
 593{
 594        struct frad_local *flp;
 595        int               i;
 596
 597        flp = netdev_priv(slave);
 598
 599        for(i=0;i<CONFIG_DLCI_MAX;i++)
 600                if (flp->master[i] == master)
 601                        break;
 602
 603        if (i == CONFIG_DLCI_MAX)
 604                return -ENODEV;
 605
 606        flp->master[i] = NULL;
 607        flp->dlci[i] = 0;
 608
 609
 610        if (netif_running(slave)) {
 611                if (flp->config.station == FRAD_STATION_CPE)
 612                        sdla_reconfig(slave);
 613                else
 614                        sdla_cmd(slave, SDLA_DELETE_DLCI, 0, 0, master->dev_addr, sizeof(short), NULL, NULL);
 615        }
 616
 617        return 0;
 618}
 619
 620static int sdla_dlci_conf(struct net_device *slave, struct net_device *master, int get)
 621{
 622        struct frad_local *flp;
 623        struct dlci_local *dlp;
 624        int               i;
 625        short             len, ret;
 626
 627        flp = netdev_priv(slave);
 628
 629        for(i=0;i<CONFIG_DLCI_MAX;i++)
 630                if (flp->master[i] == master)
 631                        break;
 632
 633        if (i == CONFIG_DLCI_MAX)
 634                return -ENODEV;
 635
 636        dlp = netdev_priv(master);
 637
 638        ret = SDLA_RET_OK;
 639        len = sizeof(struct dlci_conf);
 640        if (netif_running(slave)) {
 641                if (get)
 642                        ret = sdla_cmd(slave, SDLA_READ_DLCI_CONFIGURATION, abs(flp->dlci[i]), 0,  
 643                                    NULL, 0, &dlp->config, &len);
 644                else
 645                        ret = sdla_cmd(slave, SDLA_SET_DLCI_CONFIGURATION, abs(flp->dlci[i]), 0,  
 646                                    &dlp->config, sizeof(struct dlci_conf) - 4 * sizeof(short), NULL, NULL);
 647        }
 648
 649        return ret == SDLA_RET_OK ? 0 : -EIO;
 650}
 651
 652/**************************
 653 *
 654 * now for the Linux driver 
 655 *
 656 **************************/
 657
 658/* NOTE: the DLCI driver deals with freeing the SKB!! */
 659static netdev_tx_t sdla_transmit(struct sk_buff *skb,
 660                                 struct net_device *dev)
 661{
 662        struct frad_local *flp;
 663        int               ret, addr, accept, i;
 664        short             size;
 665        unsigned long     flags;
 666        struct buf_entry  *pbuf;
 667
 668        flp = netdev_priv(dev);
 669        ret = 0;
 670        accept = 1;
 671
 672        netif_stop_queue(dev);
 673
 674        /*
 675         * stupid GateD insists on setting up the multicast router thru us
 676         * and we're ill equipped to handle a non Frame Relay packet at this
 677         * time!
 678         */
 679
 680        accept = 1;
 681        switch (dev->type)
 682        {
 683                case ARPHRD_FRAD:
 684                        if (skb->dev->type != ARPHRD_DLCI)
 685                        {
 686                                netdev_warn(dev, "Non DLCI device, type %i, tried to send on FRAD module\n",
 687                                            skb->dev->type);
 688                                accept = 0;
 689                        }
 690                        break;
 691                default:
 692                        netdev_warn(dev, "unknown firmware type 0x%04X\n",
 693                                    dev->type);
 694                        accept = 0;
 695                        break;
 696        }
 697        if (accept)
 698        {
 699                /* this is frame specific, but till there's a PPP module, it's the default */
 700                switch (flp->type)
 701                {
 702                        case SDLA_S502A:
 703                        case SDLA_S502E:
 704                                ret = sdla_cmd(dev, SDLA_INFORMATION_WRITE, *(short *)(skb->dev->dev_addr), 0, skb->data, skb->len, NULL, NULL);
 705                                break;
 706                                case SDLA_S508:
 707                                size = sizeof(addr);
 708                                ret = sdla_cmd(dev, SDLA_INFORMATION_WRITE, *(short *)(skb->dev->dev_addr), 0, NULL, skb->len, &addr, &size);
 709                                if (ret == SDLA_RET_OK)
 710                                {
 711
 712                                        spin_lock_irqsave(&sdla_lock, flags);
 713                                        SDLA_WINDOW(dev, addr);
 714                                        pbuf = (void *)(((int) dev->mem_start) + (addr & SDLA_ADDR_MASK));
 715                                        __sdla_write(dev, pbuf->buf_addr, skb->data, skb->len);
 716                                        SDLA_WINDOW(dev, addr);
 717                                        pbuf->opp_flag = 1;
 718                                        spin_unlock_irqrestore(&sdla_lock, flags);
 719                                }
 720                                break;
 721                }
 722
 723                switch (ret)
 724                {
 725                        case SDLA_RET_OK:
 726                                dev->stats.tx_packets++;
 727                                break;
 728
 729                        case SDLA_RET_CIR_OVERFLOW:
 730                        case SDLA_RET_BUF_OVERSIZE:
 731                        case SDLA_RET_NO_BUFS:
 732                                dev->stats.tx_dropped++;
 733                                break;
 734
 735                        default:
 736                                dev->stats.tx_errors++;
 737                                break;
 738                }
 739        }
 740        netif_wake_queue(dev);
 741        for(i=0;i<CONFIG_DLCI_MAX;i++)
 742        {
 743                if(flp->master[i]!=NULL)
 744                        netif_wake_queue(flp->master[i]);
 745        }               
 746
 747        dev_kfree_skb(skb);
 748        return NETDEV_TX_OK;
 749}
 750
 751static void sdla_receive(struct net_device *dev)
 752{
 753        struct net_device         *master;
 754        struct frad_local *flp;
 755        struct dlci_local *dlp;
 756        struct sk_buff   *skb;
 757
 758        struct sdla_cmd *cmd;
 759        struct buf_info *pbufi;
 760        struct buf_entry  *pbuf;
 761
 762        unsigned long     flags;
 763        int               i=0, received, success, addr, buf_base, buf_top;
 764        short             dlci, len, len2, split;
 765
 766        flp = netdev_priv(dev);
 767        success = 1;
 768        received = addr = buf_top = buf_base = 0;
 769        len = dlci = 0;
 770        skb = NULL;
 771        master = NULL;
 772        cmd = NULL;
 773        pbufi = NULL;
 774        pbuf = NULL;
 775
 776        spin_lock_irqsave(&sdla_lock, flags);
 777
 778        switch (flp->type)
 779        {
 780                case SDLA_S502A:
 781                case SDLA_S502E:
 782                        cmd = (void *) (dev->mem_start + (SDLA_502_RCV_BUF & SDLA_ADDR_MASK));
 783                        SDLA_WINDOW(dev, SDLA_502_RCV_BUF);
 784                        success = cmd->opp_flag;
 785                        if (!success)
 786                                break;
 787
 788                        dlci = cmd->dlci;
 789                        len = cmd->length;
 790                        break;
 791
 792                case SDLA_S508:
 793                        pbufi = (void *) (dev->mem_start + (SDLA_508_RXBUF_INFO & SDLA_ADDR_MASK));
 794                        SDLA_WINDOW(dev, SDLA_508_RXBUF_INFO);
 795                        pbuf = (void *) (dev->mem_start + ((pbufi->rse_base + flp->buffer * sizeof(struct buf_entry)) & SDLA_ADDR_MASK));
 796                        success = pbuf->opp_flag;
 797                        if (!success)
 798                                break;
 799
 800                        buf_top = pbufi->buf_top;
 801                        buf_base = pbufi->buf_base;
 802                        dlci = pbuf->dlci;
 803                        len = pbuf->length;
 804                        addr = pbuf->buf_addr;
 805                        break;
 806        }
 807
 808        /* common code, find the DLCI and get the SKB */
 809        if (success)
 810        {
 811                for (i=0;i<CONFIG_DLCI_MAX;i++)
 812                        if (flp->dlci[i] == dlci)
 813                                break;
 814
 815                if (i == CONFIG_DLCI_MAX)
 816                {
 817                        netdev_notice(dev, "Received packet from invalid DLCI %i, ignoring\n",
 818                                      dlci);
 819                        dev->stats.rx_errors++;
 820                        success = 0;
 821                }
 822        }
 823
 824        if (success)
 825        {
 826                master = flp->master[i];
 827                skb = dev_alloc_skb(len + sizeof(struct frhdr));
 828                if (skb == NULL) 
 829                {
 830                        netdev_notice(dev, "Memory squeeze, dropping packet\n");
 831                        dev->stats.rx_dropped++;
 832                        success = 0;
 833                }
 834                else
 835                        skb_reserve(skb, sizeof(struct frhdr));
 836        }
 837
 838        /* pick up the data */
 839        switch (flp->type)
 840        {
 841                case SDLA_S502A:
 842                case SDLA_S502E:
 843                        if (success)
 844                                __sdla_read(dev, SDLA_502_RCV_BUF + SDLA_502_DATA_OFS, skb_put(skb,len), len);
 845
 846                        SDLA_WINDOW(dev, SDLA_502_RCV_BUF);
 847                        cmd->opp_flag = 0;
 848                        break;
 849
 850                case SDLA_S508:
 851                        if (success)
 852                        {
 853                                /* is this buffer split off the end of the internal ring buffer */
 854                                split = addr + len > buf_top + 1 ? len - (buf_top - addr + 1) : 0;
 855                                len2 = len - split;
 856
 857                                __sdla_read(dev, addr, skb_put(skb, len2), len2);
 858                                if (split)
 859                                        __sdla_read(dev, buf_base, skb_put(skb, split), split);
 860                        }
 861
 862                        /* increment the buffer we're looking at */
 863                        SDLA_WINDOW(dev, SDLA_508_RXBUF_INFO);
 864                        flp->buffer = (flp->buffer + 1) % pbufi->rse_num;
 865                        pbuf->opp_flag = 0;
 866                        break;
 867        }
 868
 869        if (success)
 870        {
 871                dev->stats.rx_packets++;
 872                dlp = netdev_priv(master);
 873                (*dlp->receive)(skb, master);
 874        }
 875
 876        spin_unlock_irqrestore(&sdla_lock, flags);
 877}
 878
 879static irqreturn_t sdla_isr(int dummy, void *dev_id)
 880{
 881        struct net_device     *dev;
 882        struct frad_local *flp;
 883        char              byte;
 884
 885        dev = dev_id;
 886
 887        flp = netdev_priv(dev);
 888
 889        if (!flp->initialized)
 890        {
 891                netdev_warn(dev, "irq %d for uninitialized device\n", dev->irq);
 892                return IRQ_NONE;
 893        }
 894
 895        byte = sdla_byte(dev, flp->type == SDLA_S508 ? SDLA_508_IRQ_INTERFACE : SDLA_502_IRQ_INTERFACE);
 896        switch (byte)
 897        {
 898                case SDLA_INTR_RX:
 899                        sdla_receive(dev);
 900                        break;
 901
 902                /* the command will get an error return, which is processed above */
 903                case SDLA_INTR_MODEM:
 904                case SDLA_INTR_STATUS:
 905                        sdla_cmd(dev, SDLA_READ_DLC_STATUS, 0, 0, NULL, 0, NULL, NULL);
 906                        break;
 907
 908                case SDLA_INTR_TX:
 909                case SDLA_INTR_COMPLETE:
 910                case SDLA_INTR_TIMER:
 911                        netdev_warn(dev, "invalid irq flag 0x%02X\n", byte);
 912                        break;
 913        }
 914
 915        /* the S502E requires a manual acknowledgement of the interrupt */ 
 916        if (flp->type == SDLA_S502E)
 917        {
 918                flp->state &= ~SDLA_S502E_INTACK;
 919                outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
 920                flp->state |= SDLA_S502E_INTACK;
 921                outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
 922        }
 923
 924        /* this clears the byte, informing the Z80 we're done */
 925        byte = 0;
 926        sdla_write(dev, flp->type == SDLA_S508 ? SDLA_508_IRQ_INTERFACE : SDLA_502_IRQ_INTERFACE, &byte, sizeof(byte));
 927        return IRQ_HANDLED;
 928}
 929
 930static void sdla_poll(struct timer_list *t)
 931{
 932        struct frad_local *flp = from_timer(flp, t, timer);
 933        struct net_device *dev = flp->dev;
 934
 935        if (sdla_byte(dev, SDLA_502_RCV_BUF))
 936                sdla_receive(dev);
 937
 938        flp->timer.expires = 1;
 939        add_timer(&flp->timer);
 940}
 941
 942static int sdla_close(struct net_device *dev)
 943{
 944        struct frad_local *flp;
 945        struct intr_info  intr;
 946        int               len, i;
 947        short             dlcis[CONFIG_DLCI_MAX];
 948
 949        flp = netdev_priv(dev);
 950
 951        len = 0;
 952        for(i=0;i<CONFIG_DLCI_MAX;i++)
 953                if (flp->dlci[i])
 954                        dlcis[len++] = abs(flp->dlci[i]);
 955        len *= 2;
 956
 957        if (flp->config.station == FRAD_STATION_NODE)
 958        {
 959                for(i=0;i<CONFIG_DLCI_MAX;i++)
 960                        if (flp->dlci[i] > 0) 
 961                                sdla_cmd(dev, SDLA_DEACTIVATE_DLCI, 0, 0, dlcis, len, NULL, NULL);
 962                sdla_cmd(dev, SDLA_DELETE_DLCI, 0, 0, &flp->dlci[i], sizeof(flp->dlci[i]), NULL, NULL);
 963        }
 964
 965        memset(&intr, 0, sizeof(intr));
 966        /* let's start up the reception */
 967        switch(flp->type)
 968        {
 969                case SDLA_S502A:
 970                        del_timer(&flp->timer); 
 971                        break;
 972
 973                case SDLA_S502E:
 974                        sdla_cmd(dev, SDLA_SET_IRQ_TRIGGER, 0, 0, &intr, sizeof(char) + sizeof(short), NULL, NULL);
 975                        flp->state &= ~SDLA_S502E_INTACK;
 976                        outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
 977                        break;
 978
 979                case SDLA_S507:
 980                        break;
 981
 982                case SDLA_S508:
 983                        sdla_cmd(dev, SDLA_SET_IRQ_TRIGGER, 0, 0, &intr, sizeof(struct intr_info), NULL, NULL);
 984                        flp->state &= ~SDLA_S508_INTEN;
 985                        outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
 986                        break;
 987        }
 988
 989        sdla_cmd(dev, SDLA_DISABLE_COMMUNICATIONS, 0, 0, NULL, 0, NULL, NULL);
 990
 991        netif_stop_queue(dev);
 992        
 993        return 0;
 994}
 995
 996struct conf_data {
 997        struct frad_conf config;
 998        short            dlci[CONFIG_DLCI_MAX];
 999};
1000
1001static int sdla_open(struct net_device *dev)
1002{
1003        struct frad_local *flp;
1004        struct dlci_local *dlp;
1005        struct conf_data  data;
1006        struct intr_info  intr;
1007        int               len, i;
1008        char              byte;
1009
1010        flp = netdev_priv(dev);
1011
1012        if (!flp->initialized)
1013                return -EPERM;
1014
1015        if (!flp->configured)
1016                return -EPERM;
1017
1018        /* time to send in the configuration */
1019        len = 0;
1020        for(i=0;i<CONFIG_DLCI_MAX;i++)
1021                if (flp->dlci[i])
1022                        data.dlci[len++] = abs(flp->dlci[i]);
1023        len *= 2;
1024
1025        memcpy(&data.config, &flp->config, sizeof(struct frad_conf));
1026        len += sizeof(struct frad_conf);
1027
1028        sdla_cmd(dev, SDLA_DISABLE_COMMUNICATIONS, 0, 0, NULL, 0, NULL, NULL);
1029        sdla_cmd(dev, SDLA_SET_DLCI_CONFIGURATION, 0, 0, &data, len, NULL, NULL);
1030
1031        if (flp->type == SDLA_S508)
1032                flp->buffer = 0;
1033
1034        sdla_cmd(dev, SDLA_ENABLE_COMMUNICATIONS, 0, 0, NULL, 0, NULL, NULL);
1035
1036        /* let's start up the reception */
1037        memset(&intr, 0, sizeof(intr));
1038        switch(flp->type)
1039        {
1040                case SDLA_S502A:
1041                        flp->timer.expires = 1;
1042                        add_timer(&flp->timer);
1043                        break;
1044
1045                case SDLA_S502E:
1046                        flp->state |= SDLA_S502E_ENABLE;
1047                        outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
1048                        flp->state |= SDLA_S502E_INTACK;
1049                        outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
1050                        byte = 0;
1051                        sdla_write(dev, SDLA_502_IRQ_INTERFACE, &byte, sizeof(byte));
1052                        intr.flags = SDLA_INTR_RX | SDLA_INTR_STATUS | SDLA_INTR_MODEM;
1053                        sdla_cmd(dev, SDLA_SET_IRQ_TRIGGER, 0, 0, &intr, sizeof(char) + sizeof(short), NULL, NULL);
1054                        break;
1055
1056                case SDLA_S507:
1057                        break;
1058
1059                case SDLA_S508:
1060                        flp->state |= SDLA_S508_INTEN;
1061                        outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
1062                        byte = 0;
1063                        sdla_write(dev, SDLA_508_IRQ_INTERFACE, &byte, sizeof(byte));
1064                        intr.flags = SDLA_INTR_RX | SDLA_INTR_STATUS | SDLA_INTR_MODEM;
1065                        intr.irq = dev->irq;
1066                        sdla_cmd(dev, SDLA_SET_IRQ_TRIGGER, 0, 0, &intr, sizeof(struct intr_info), NULL, NULL);
1067                        break;
1068        }
1069
1070        if (flp->config.station == FRAD_STATION_CPE)
1071        {
1072                byte = SDLA_ICS_STATUS_ENQ;
1073                sdla_cmd(dev, SDLA_ISSUE_IN_CHANNEL_SIGNAL, 0, 0, &byte, sizeof(byte), NULL, NULL);
1074        }
1075        else
1076        {
1077                sdla_cmd(dev, SDLA_ADD_DLCI, 0, 0, data.dlci, len - sizeof(struct frad_conf), NULL, NULL);
1078                for(i=0;i<CONFIG_DLCI_MAX;i++)
1079                        if (flp->dlci[i] > 0)
1080                                sdla_cmd(dev, SDLA_ACTIVATE_DLCI, 0, 0, &flp->dlci[i], 2*sizeof(flp->dlci[i]), NULL, NULL);
1081        }
1082
1083        /* configure any specific DLCI settings */
1084        for(i=0;i<CONFIG_DLCI_MAX;i++)
1085                if (flp->dlci[i])
1086                {
1087                        dlp = netdev_priv(flp->master[i]);
1088                        if (dlp->configured)
1089                                sdla_cmd(dev, SDLA_SET_DLCI_CONFIGURATION, abs(flp->dlci[i]), 0, &dlp->config, sizeof(struct dlci_conf), NULL, NULL);
1090                }
1091
1092        netif_start_queue(dev);
1093        
1094        return 0;
1095}
1096
1097static int sdla_config(struct net_device *dev, struct frad_conf __user *conf, int get)
1098{
1099        struct frad_local *flp;
1100        struct conf_data  data;
1101        int               i;
1102        short             size;
1103
1104        if (dev->type == 0xFFFF)
1105                return -EUNATCH;
1106
1107        flp = netdev_priv(dev);
1108
1109        if (!get)
1110        {
1111                if (netif_running(dev))
1112                        return -EBUSY;
1113
1114                if(copy_from_user(&data.config, conf, sizeof(struct frad_conf)))
1115                        return -EFAULT;
1116
1117                if (data.config.station & ~FRAD_STATION_NODE)
1118                        return -EINVAL;
1119
1120                if (data.config.flags & ~FRAD_VALID_FLAGS)
1121                        return -EINVAL;
1122
1123                if ((data.config.kbaud < 0) || 
1124                         ((data.config.kbaud > 128) && (flp->type != SDLA_S508)))
1125                        return -EINVAL;
1126
1127                if (data.config.clocking & ~(FRAD_CLOCK_INT | SDLA_S508_PORT_RS232))
1128                        return -EINVAL;
1129
1130                if ((data.config.mtu < 0) || (data.config.mtu > SDLA_MAX_MTU))
1131                        return -EINVAL;
1132
1133                if ((data.config.T391 < 5) || (data.config.T391 > 30))
1134                        return -EINVAL;
1135
1136                if ((data.config.T392 < 5) || (data.config.T392 > 30))
1137                        return -EINVAL;
1138
1139                if ((data.config.N391 < 1) || (data.config.N391 > 255))
1140                        return -EINVAL;
1141
1142                if ((data.config.N392 < 1) || (data.config.N392 > 10))
1143                        return -EINVAL;
1144
1145                if ((data.config.N393 < 1) || (data.config.N393 > 10))
1146                        return -EINVAL;
1147
1148                memcpy(&flp->config, &data.config, sizeof(struct frad_conf));
1149                flp->config.flags |= SDLA_DIRECT_RECV;
1150
1151                if (flp->type == SDLA_S508)
1152                        flp->config.flags |= SDLA_TX70_RX30;
1153
1154                if (dev->mtu != flp->config.mtu)
1155                {
1156                        /* this is required to change the MTU */
1157                        dev->mtu = flp->config.mtu;
1158                        for(i=0;i<CONFIG_DLCI_MAX;i++)
1159                                if (flp->master[i])
1160                                        flp->master[i]->mtu = flp->config.mtu;
1161                }
1162
1163                flp->config.mtu += sizeof(struct frhdr);
1164
1165                /* off to the races! */
1166                if (!flp->configured)
1167                        sdla_start(dev);
1168
1169                flp->configured = 1;
1170        }
1171        else
1172        {
1173                /* no sense reading if the CPU isn't started */
1174                if (netif_running(dev))
1175                {
1176                        size = sizeof(data);
1177                        if (sdla_cmd(dev, SDLA_READ_DLCI_CONFIGURATION, 0, 0, NULL, 0, &data, &size) != SDLA_RET_OK)
1178                                return -EIO;
1179                }
1180                else
1181                        if (flp->configured)
1182                                memcpy(&data.config, &flp->config, sizeof(struct frad_conf));
1183                        else
1184                                memset(&data.config, 0, sizeof(struct frad_conf));
1185
1186                memcpy(&flp->config, &data.config, sizeof(struct frad_conf));
1187                data.config.flags &= FRAD_VALID_FLAGS;
1188                data.config.mtu -= data.config.mtu > sizeof(struct frhdr) ? sizeof(struct frhdr) : data.config.mtu;
1189                return copy_to_user(conf, &data.config, sizeof(struct frad_conf))?-EFAULT:0;
1190        }
1191
1192        return 0;
1193}
1194
1195static int sdla_xfer(struct net_device *dev, struct sdla_mem __user *info, int read)
1196{
1197        struct sdla_mem mem;
1198        char    *temp;
1199
1200        if(copy_from_user(&mem, info, sizeof(mem)))
1201                return -EFAULT;
1202                
1203        if (read)
1204        {       
1205                temp = kzalloc(mem.len, GFP_KERNEL);
1206                if (!temp)
1207                        return -ENOMEM;
1208                sdla_read(dev, mem.addr, temp, mem.len);
1209                if(copy_to_user(mem.data, temp, mem.len))
1210                {
1211                        kfree(temp);
1212                        return -EFAULT;
1213                }
1214                kfree(temp);
1215        }
1216        else
1217        {
1218                temp = memdup_user(mem.data, mem.len);
1219                if (IS_ERR(temp))
1220                        return PTR_ERR(temp);
1221                sdla_write(dev, mem.addr, temp, mem.len);
1222                kfree(temp);
1223        }
1224        return 0;
1225}
1226
1227static int sdla_reconfig(struct net_device *dev)
1228{
1229        struct frad_local *flp;
1230        struct conf_data  data;
1231        int               i, len;
1232
1233        flp = netdev_priv(dev);
1234
1235        len = 0;
1236        for(i=0;i<CONFIG_DLCI_MAX;i++)
1237                if (flp->dlci[i])
1238                        data.dlci[len++] = flp->dlci[i];
1239        len *= 2;
1240
1241        memcpy(&data, &flp->config, sizeof(struct frad_conf));
1242        len += sizeof(struct frad_conf);
1243
1244        sdla_cmd(dev, SDLA_DISABLE_COMMUNICATIONS, 0, 0, NULL, 0, NULL, NULL);
1245        sdla_cmd(dev, SDLA_SET_DLCI_CONFIGURATION, 0, 0, &data, len, NULL, NULL);
1246        sdla_cmd(dev, SDLA_ENABLE_COMMUNICATIONS, 0, 0, NULL, 0, NULL, NULL);
1247
1248        return 0;
1249}
1250
1251static int sdla_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1252{
1253        struct frad_local *flp;
1254
1255        if(!capable(CAP_NET_ADMIN))
1256                return -EPERM;
1257                
1258        flp = netdev_priv(dev);
1259
1260        if (!flp->initialized)
1261                return -EINVAL;
1262
1263        switch (cmd)
1264        {
1265                case FRAD_GET_CONF:
1266                case FRAD_SET_CONF:
1267                        return sdla_config(dev, ifr->ifr_data, cmd == FRAD_GET_CONF);
1268
1269                case SDLA_IDENTIFY:
1270                        ifr->ifr_flags = flp->type;
1271                        break;
1272
1273                case SDLA_CPUSPEED:
1274                        return sdla_cpuspeed(dev, ifr);
1275
1276/* ==========================================================
1277NOTE:  This is rather a useless action right now, as the
1278       current driver does not support protocols other than
1279       FR.  However, Sangoma has modules for a number of
1280       other protocols in the works.
1281============================================================*/
1282                case SDLA_PROTOCOL:
1283                        if (flp->configured)
1284                                return -EALREADY;
1285
1286                        switch (ifr->ifr_flags)
1287                        {
1288                                case ARPHRD_FRAD:
1289                                        dev->type = ifr->ifr_flags;
1290                                        break;
1291                                default:
1292                                        return -ENOPROTOOPT;
1293                        }
1294                        break;
1295
1296                case SDLA_CLEARMEM:
1297                        sdla_clear(dev);
1298                        break;
1299
1300                case SDLA_WRITEMEM:
1301                case SDLA_READMEM:
1302                        if(!capable(CAP_SYS_RAWIO))
1303                                return -EPERM;
1304                        return sdla_xfer(dev, ifr->ifr_data, cmd == SDLA_READMEM);
1305
1306                case SDLA_START:
1307                        sdla_start(dev);
1308                        break;
1309
1310                case SDLA_STOP:
1311                        sdla_stop(dev);
1312                        break;
1313
1314                default:
1315                        return -EOPNOTSUPP;
1316        }
1317        return 0;
1318}
1319
1320static int sdla_change_mtu(struct net_device *dev, int new_mtu)
1321{
1322        if (netif_running(dev))
1323                return -EBUSY;
1324
1325        /* for now, you can't change the MTU! */
1326        return -EOPNOTSUPP;
1327}
1328
1329static int sdla_set_config(struct net_device *dev, struct ifmap *map)
1330{
1331        struct frad_local *flp;
1332        int               i;
1333        char              byte;
1334        unsigned base;
1335        int err = -EINVAL;
1336
1337        flp = netdev_priv(dev);
1338
1339        if (flp->initialized)
1340                return -EINVAL;
1341
1342        for(i=0; i < ARRAY_SIZE(valid_port); i++)
1343                if (valid_port[i] == map->base_addr)
1344                        break;   
1345
1346        if (i == ARRAY_SIZE(valid_port))
1347                return -EINVAL;
1348
1349        if (!request_region(map->base_addr, SDLA_IO_EXTENTS, dev->name)){
1350                pr_warn("io-port 0x%04lx in use\n", dev->base_addr);
1351                return -EINVAL;
1352        }
1353        base = map->base_addr;
1354
1355        /* test for card types, S502A, S502E, S507, S508                 */
1356        /* these tests shut down the card completely, so clear the state */
1357        flp->type = SDLA_UNKNOWN;
1358        flp->state = 0;
1359   
1360        for(i=1;i<SDLA_IO_EXTENTS;i++)
1361                if (inb(base + i) != 0xFF)
1362                        break;
1363
1364        if (i == SDLA_IO_EXTENTS) {   
1365                outb(SDLA_HALT, base + SDLA_REG_Z80_CONTROL);
1366                if ((inb(base + SDLA_S502_STS) & 0x0F) == 0x08) {
1367                        outb(SDLA_S502E_INTACK, base + SDLA_REG_CONTROL);
1368                        if ((inb(base + SDLA_S502_STS) & 0x0F) == 0x0C) {
1369                                outb(SDLA_HALT, base + SDLA_REG_CONTROL);
1370                                flp->type = SDLA_S502E;
1371                                goto got_type;
1372                        }
1373                }
1374        }
1375
1376        for(byte=inb(base),i=0;i<SDLA_IO_EXTENTS;i++)
1377                if (inb(base + i) != byte)
1378                        break;
1379
1380        if (i == SDLA_IO_EXTENTS) {
1381                outb(SDLA_HALT, base + SDLA_REG_CONTROL);
1382                if ((inb(base + SDLA_S502_STS) & 0x7E) == 0x30) {
1383                        outb(SDLA_S507_ENABLE, base + SDLA_REG_CONTROL);
1384                        if ((inb(base + SDLA_S502_STS) & 0x7E) == 0x32) {
1385                                outb(SDLA_HALT, base + SDLA_REG_CONTROL);
1386                                flp->type = SDLA_S507;
1387                                goto got_type;
1388                        }
1389                }
1390        }
1391
1392        outb(SDLA_HALT, base + SDLA_REG_CONTROL);
1393        if ((inb(base + SDLA_S508_STS) & 0x3F) == 0x00) {
1394                outb(SDLA_S508_INTEN, base + SDLA_REG_CONTROL);
1395                if ((inb(base + SDLA_S508_STS) & 0x3F) == 0x10) {
1396                        outb(SDLA_HALT, base + SDLA_REG_CONTROL);
1397                        flp->type = SDLA_S508;
1398                        goto got_type;
1399                }
1400        }
1401
1402        outb(SDLA_S502A_HALT, base + SDLA_REG_CONTROL);
1403        if (inb(base + SDLA_S502_STS) == 0x40) {
1404                outb(SDLA_S502A_START, base + SDLA_REG_CONTROL);
1405                if (inb(base + SDLA_S502_STS) == 0x40) {
1406                        outb(SDLA_S502A_INTEN, base + SDLA_REG_CONTROL);
1407                        if (inb(base + SDLA_S502_STS) == 0x44) {
1408                                outb(SDLA_S502A_START, base + SDLA_REG_CONTROL);
1409                                flp->type = SDLA_S502A;
1410                                goto got_type;
1411                        }
1412                }
1413        }
1414
1415        netdev_notice(dev, "Unknown card type\n");
1416        err = -ENODEV;
1417        goto fail;
1418
1419got_type:
1420        switch(base) {
1421                case 0x270:
1422                case 0x280:
1423                case 0x380: 
1424                case 0x390:
1425                        if (flp->type != SDLA_S508 && flp->type != SDLA_S507)
1426                                goto fail;
1427        }
1428
1429        switch (map->irq) {
1430                case 2:
1431                        if (flp->type != SDLA_S502E)
1432                                goto fail;
1433                        break;
1434
1435                case 10:
1436                case 11:
1437                case 12:
1438                case 15:
1439                case 4:
1440                        if (flp->type != SDLA_S508 && flp->type != SDLA_S507)
1441                                goto fail;
1442                        break;
1443                case 3:
1444                case 5:
1445                case 7:
1446                        if (flp->type == SDLA_S502A)
1447                                goto fail;
1448                        break;
1449
1450                default:
1451                        goto fail;
1452        }
1453
1454        err = -EAGAIN;
1455        if (request_irq(dev->irq, sdla_isr, 0, dev->name, dev)) 
1456                goto fail;
1457
1458        if (flp->type == SDLA_S507) {
1459                switch(dev->irq) {
1460                        case 3:
1461                                flp->state = SDLA_S507_IRQ3;
1462                                break;
1463                        case 4:
1464                                flp->state = SDLA_S507_IRQ4;
1465                                break;
1466                        case 5:
1467                                flp->state = SDLA_S507_IRQ5;
1468                                break;
1469                        case 7:
1470                                flp->state = SDLA_S507_IRQ7;
1471                                break;
1472                        case 10:
1473                                flp->state = SDLA_S507_IRQ10;
1474                                break;
1475                        case 11:
1476                                flp->state = SDLA_S507_IRQ11;
1477                                break;
1478                        case 12:
1479                                flp->state = SDLA_S507_IRQ12;
1480                                break;
1481                        case 15:
1482                                flp->state = SDLA_S507_IRQ15;
1483                                break;
1484                }
1485        }
1486
1487        for(i=0; i < ARRAY_SIZE(valid_mem); i++)
1488                if (valid_mem[i] == map->mem_start)
1489                        break;   
1490
1491        err = -EINVAL;
1492        if (i == ARRAY_SIZE(valid_mem))
1493                goto fail2;
1494
1495        if (flp->type == SDLA_S502A && (map->mem_start & 0xF000) >> 12 == 0x0E)
1496                goto fail2;
1497
1498        if (flp->type != SDLA_S507 && map->mem_start >> 16 == 0x0B)
1499                goto fail2;
1500
1501        if (flp->type == SDLA_S507 && map->mem_start >> 16 == 0x0D)
1502                goto fail2;
1503
1504        byte = flp->type != SDLA_S508 ? SDLA_8K_WINDOW : 0;
1505        byte |= (map->mem_start & 0xF000) >> (12 + (flp->type == SDLA_S508 ? 1 : 0));
1506        switch(flp->type) {
1507                case SDLA_S502A:
1508                case SDLA_S502E:
1509                        switch (map->mem_start >> 16) {
1510                                case 0x0A:
1511                                        byte |= SDLA_S502_SEG_A;
1512                                        break;
1513                                case 0x0C:
1514                                        byte |= SDLA_S502_SEG_C;
1515                                        break;
1516                                case 0x0D:
1517                                        byte |= SDLA_S502_SEG_D;
1518                                        break;
1519                                case 0x0E:
1520                                        byte |= SDLA_S502_SEG_E;
1521                                        break;
1522                        }
1523                        break;
1524                case SDLA_S507:
1525                        switch (map->mem_start >> 16) {
1526                                case 0x0A:
1527                                        byte |= SDLA_S507_SEG_A;
1528                                        break;
1529                                case 0x0B:
1530                                        byte |= SDLA_S507_SEG_B;
1531                                        break;
1532                                case 0x0C:
1533                                        byte |= SDLA_S507_SEG_C;
1534                                        break;
1535                                case 0x0E:
1536                                        byte |= SDLA_S507_SEG_E;
1537                                        break;
1538                        }
1539                        break;
1540                case SDLA_S508:
1541                        switch (map->mem_start >> 16) {
1542                                case 0x0A:
1543                                        byte |= SDLA_S508_SEG_A;
1544                                        break;
1545                                case 0x0C:
1546                                        byte |= SDLA_S508_SEG_C;
1547                                        break;
1548                                case 0x0D:
1549                                        byte |= SDLA_S508_SEG_D;
1550                                        break;
1551                                case 0x0E:
1552                                        byte |= SDLA_S508_SEG_E;
1553                                        break;
1554                        }
1555                        break;
1556        }
1557
1558        /* set the memory bits, and enable access */
1559        outb(byte, base + SDLA_REG_PC_WINDOW);
1560
1561        switch(flp->type)
1562        {
1563                case SDLA_S502E:
1564                        flp->state = SDLA_S502E_ENABLE;
1565                        break;
1566                case SDLA_S507:
1567                        flp->state |= SDLA_MEMEN;
1568                        break;
1569                case SDLA_S508:
1570                        flp->state = SDLA_MEMEN;
1571                        break;
1572        }
1573        outb(flp->state, base + SDLA_REG_CONTROL);
1574
1575        dev->irq = map->irq;
1576        dev->base_addr = base;
1577        dev->mem_start = map->mem_start;
1578        dev->mem_end = dev->mem_start + 0x2000;
1579        flp->initialized = 1;
1580        return 0;
1581
1582fail2:
1583        free_irq(map->irq, dev);
1584fail:
1585        release_region(base, SDLA_IO_EXTENTS);
1586        return err;
1587}
1588 
1589static const struct net_device_ops sdla_netdev_ops = {
1590        .ndo_open       = sdla_open,
1591        .ndo_stop       = sdla_close,
1592        .ndo_do_ioctl   = sdla_ioctl,
1593        .ndo_set_config = sdla_set_config,
1594        .ndo_start_xmit = sdla_transmit,
1595        .ndo_change_mtu = sdla_change_mtu,
1596};
1597
1598static void setup_sdla(struct net_device *dev)
1599{
1600        struct frad_local *flp = netdev_priv(dev);
1601
1602        netdev_boot_setup_check(dev);
1603
1604        dev->netdev_ops         = &sdla_netdev_ops;
1605        dev->flags              = 0;
1606        dev->type               = 0xFFFF;
1607        dev->hard_header_len    = 0;
1608        dev->addr_len           = 0;
1609        dev->mtu                = SDLA_MAX_MTU;
1610
1611        flp->activate           = sdla_activate;
1612        flp->deactivate         = sdla_deactivate;
1613        flp->assoc              = sdla_assoc;
1614        flp->deassoc            = sdla_deassoc;
1615        flp->dlci_conf          = sdla_dlci_conf;
1616        flp->dev                = dev;
1617
1618        timer_setup(&flp->timer, sdla_poll, 0);
1619        flp->timer.expires      = 1;
1620}
1621
1622static struct net_device *sdla;
1623
1624static int __init init_sdla(void)
1625{
1626        int err;
1627
1628        printk("%s.\n", version);
1629
1630        sdla = alloc_netdev(sizeof(struct frad_local), "sdla0",
1631                            NET_NAME_UNKNOWN, setup_sdla);
1632        if (!sdla) 
1633                return -ENOMEM;
1634
1635        err = register_netdev(sdla);
1636        if (err) 
1637                free_netdev(sdla);
1638
1639        return err;
1640}
1641
1642static void __exit exit_sdla(void)
1643{
1644        struct frad_local *flp = netdev_priv(sdla);
1645
1646        unregister_netdev(sdla);
1647        if (flp->initialized) {
1648                free_irq(sdla->irq, sdla);
1649                release_region(sdla->base_addr, SDLA_IO_EXTENTS);
1650        }
1651        del_timer_sync(&flp->timer);
1652        free_netdev(sdla);
1653}
1654
1655MODULE_LICENSE("GPL");
1656
1657module_init(init_sdla);
1658module_exit(exit_sdla);
1659