linux-bk/drivers/net/cs89x0.c
<<
>>
Prefs
   1/* cs89x0.c: A Crystal Semiconductor (Now Cirrus Logic) CS89[02]0
   2 *  driver for linux.
   3 */
   4
   5/*
   6        Written 1996 by Russell Nelson, with reference to skeleton.c
   7        written 1993-1994 by Donald Becker.
   8
   9        This software may be used and distributed according to the terms
  10        of the GNU General Public License, incorporated herein by reference.
  11
  12        The author may be reached at nelson@crynwr.com, Crynwr
  13        Software, 521 Pleasant Valley Rd., Potsdam, NY 13676
  14
  15  Changelog:
  16
  17  Mike Cruse        : mcruse@cti-ltd.com
  18                    : Changes for Linux 2.0 compatibility. 
  19                    : Added dev_id parameter in net_interrupt(),
  20                    : request_irq() and free_irq(). Just NULL for now.
  21
  22  Mike Cruse        : Added MOD_INC_USE_COUNT and MOD_DEC_USE_COUNT macros
  23                    : in net_open() and net_close() so kerneld would know
  24                    : that the module is in use and wouldn't eject the 
  25                    : driver prematurely.
  26
  27  Mike Cruse        : Rewrote init_module() and cleanup_module using 8390.c
  28                    : as an example. Disabled autoprobing in init_module(),
  29                    : not a good thing to do to other devices while Linux
  30                    : is running from all accounts.
  31
  32  Russ Nelson       : Jul 13 1998.  Added RxOnly DMA support.
  33
  34  Melody Lee        : Aug 10 1999.  Changes for Linux 2.2.5 compatibility. 
  35                    : email: ethernet@crystal.cirrus.com
  36
  37  Alan Cox          : Removed 1.2 support, added 2.1 extra counters.
  38
  39  Andrew Morton     : andrewm@uow.edu.au
  40                    : Kernel 2.3.48
  41                    : Handle kmalloc() failures
  42                    : Other resource allocation fixes
  43                    : Add SMP locks
  44                    : Integrate Russ Nelson's ALLOW_DMA functionality back in.
  45                    : If ALLOW_DMA is true, make DMA runtime selectable
  46                    : Folded in changes from Cirrus (Melody Lee
  47                    : <klee@crystal.cirrus.com>)
  48                    : Don't call netif_wake_queue() in net_send_packet()
  49                    : Fixed an out-of-mem bug in dma_rx()
  50                    : Updated Documentation/cs89x0.txt
  51
  52  Andrew Morton     : andrewm@uow.edu.au / Kernel 2.3.99-pre1
  53                    : Use skb_reserve to longword align IP header (two places)
  54                    : Remove a delay loop from dma_rx()
  55                    : Replace '100' with HZ
  56                    : Clean up a couple of skb API abuses
  57                    : Added 'cs89x0_dma=N' kernel boot option
  58                    : Correctly initialise lp->lock in non-module compile
  59
  60  Andrew Morton     : andrewm@uow.edu.au / Kernel 2.3.99-pre4-1
  61                    : MOD_INC/DEC race fix (see
  62                    : http://www.uwsg.indiana.edu/hypermail/linux/kernel/0003.3/1532.html)
  63
  64  Andrew Morton     : andrewm@uow.edu.au / Kernel 2.4.0-test7-pre2
  65                    : Enhanced EEPROM support to cover more devices,
  66                    :   abstracted IRQ mapping to support CONFIG_ARCH_CLPS7500 arch
  67                    :   (Jason Gunthorpe <jgg@ualberta.ca>)
  68
  69  Andrew Morton     : Kernel 2.4.0-test11-pre4
  70                    : Use dev->name in request_*() (Andrey Panin)
  71                    : Fix an error-path memleak in init_module()
  72                    : Preserve return value from request_irq()
  73                    : Fix type of `media' module parm (Keith Owens)
  74                    : Use SET_MODULE_OWNER()
  75                    : Tidied up strange request_irq() abuse in net_open().
  76
  77  Andrew Morton     : Kernel 2.4.3-pre1
  78                    : Request correct number of pages for DMA (Hugh Dickens)
  79                    : Select PP_ChipID _after_ unregister_netdev in cleanup_module()
  80                    :  because unregister_netdev() calls get_stats.
  81                    : Make `version[]' __initdata
  82                    : Uninlined the read/write reg/word functions.
  83
  84  Oskar Schirmer    : oskar@scara.com
  85                    : HiCO.SH4 (superh) support added (irq#1, cs89x0_media=)
  86
  87*/
  88
  89/* Always include 'config.h' first in case the user wants to turn on
  90   or override something. */
  91#include <linux/config.h>
  92#include <linux/module.h>
  93#include <linux/version.h>
  94
  95/*
  96 * Set this to zero to disable DMA code
  97 *
  98 * Note that even if DMA is turned off we still support the 'dma' and  'use_dma'
  99 * module options so we don't break any startup scripts.
 100 */
 101#define ALLOW_DMA       1
 102
 103/*
 104 * Set this to zero to remove all the debug statements via
 105 * dead code elimination
 106 */
 107#define DEBUGGING       1
 108
 109/*
 110  Sources:
 111
 112        Crynwr packet driver epktisa.
 113
 114        Crystal Semiconductor data sheets.
 115
 116*/
 117
 118#include <linux/kernel.h>
 119#include <linux/sched.h>
 120#include <linux/types.h>
 121#include <linux/fcntl.h>
 122#include <linux/interrupt.h>
 123#include <linux/ioport.h>
 124#include <linux/in.h>
 125#include <linux/slab.h>
 126#include <linux/string.h>
 127#include <linux/init.h>
 128#include <asm/system.h>
 129#include <asm/bitops.h>
 130#include <asm/io.h>
 131#if ALLOW_DMA
 132#include <asm/dma.h>
 133#endif
 134#include <linux/errno.h>
 135#include <linux/spinlock.h>
 136
 137#include <linux/netdevice.h>
 138#include <linux/etherdevice.h>
 139#include <linux/skbuff.h>
 140
 141#include "cs89x0.h"
 142
 143static char version[] __initdata =
 144"cs89x0.c: v2.4.3-pre1 Russell Nelson <nelson@crynwr.com>, Andrew Morton <andrewm@uow.edu.au>\n";
 145
 146/* First, a few definitions that the brave might change.
 147   A zero-terminated list of I/O addresses to be probed. Some special flags..
 148      Addr & 1 = Read back the address port, look for signature and reset
 149                 the page window before probing 
 150      Addr & 3 = Reset the page window and probe 
 151   The CLPS eval board has the Cirrus chip at 0x80090300, in ARM IO space,
 152   but it is possible that a Cirrus board could be plugged into the ISA
 153   slots. */
 154/* The cs8900 has 4 IRQ pins, software selectable. cs8900_irq_map maps 
 155   them to system IRQ numbers. This mapping is card specific and is set to
 156   the configuration of the Cirrus Eval board for this chip. */
 157#ifdef CONFIG_ARCH_CLPS7500
 158static unsigned int netcard_portlist[] __initdata =
 159   { 0x80090303, 0x300, 0x320, 0x340, 0x360, 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0, 0};
 160static unsigned int cs8900_irq_map[] = {12,0,0,0};
 161#elif defined(CONFIG_SH_HICOSH4)
 162static unsigned int netcard_portlist[] __initdata =
 163   { 0x0300, 0};
 164static unsigned int cs8900_irq_map[] = {1,0,0,0};
 165#else
 166static unsigned int netcard_portlist[] __initdata =
 167   { 0x300, 0x320, 0x340, 0x360, 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0, 0};
 168static unsigned int cs8900_irq_map[] = {10,11,12,5};
 169#endif
 170
 171#if DEBUGGING
 172static unsigned int net_debug = DEBUGGING;
 173#else
 174#define net_debug 0     /* gcc will remove all the debug code for us */
 175#endif
 176
 177/* The number of low I/O ports used by the ethercard. */
 178#define NETCARD_IO_EXTENT       16
 179
 180/* we allow the user to override various values normally set in the EEPROM */
 181#define FORCE_RJ45      0x0001    /* pick one of these three */
 182#define FORCE_AUI       0x0002
 183#define FORCE_BNC       0x0004
 184
 185#define FORCE_AUTO      0x0010    /* pick one of these three */
 186#define FORCE_HALF      0x0020
 187#define FORCE_FULL      0x0030
 188
 189/* Information that need to be kept for each board. */
 190struct net_local {
 191        struct net_device_stats stats;
 192        int chip_type;          /* one of: CS8900, CS8920, CS8920M */
 193        char chip_revision;     /* revision letter of the chip ('A'...) */
 194        int send_cmd;           /* the proper send command: TX_NOW, TX_AFTER_381, or TX_AFTER_ALL */
 195        int auto_neg_cnf;       /* auto-negotiation word from EEPROM */
 196        int adapter_cnf;        /* adapter configuration from EEPROM */
 197        int isa_config;         /* ISA configuration from EEPROM */
 198        int irq_map;            /* IRQ map from EEPROM */
 199        int rx_mode;            /* what mode are we in? 0, RX_MULTCAST_ACCEPT, or RX_ALL_ACCEPT */
 200        int curr_rx_cfg;        /* a copy of PP_RxCFG */
 201        int linectl;            /* either 0 or LOW_RX_SQUELCH, depending on configuration. */
 202        int send_underrun;      /* keep track of how many underruns in a row we get */
 203        int force;              /* force various values; see FORCE* above. */
 204        spinlock_t lock;
 205#if ALLOW_DMA
 206        int use_dma;            /* Flag: we're using dma */
 207        int dma;                /* DMA channel */
 208        int dmasize;            /* 16 or 64 */
 209        unsigned char *dma_buff;        /* points to the beginning of the buffer */
 210        unsigned char *end_dma_buff;    /* points to the end of the buffer */
 211        unsigned char *rx_dma_ptr;      /* points to the next packet  */
 212#endif
 213};
 214
 215/* Index to functions, as function prototypes. */
 216
 217extern int cs89x0_probe(struct net_device *dev);
 218
 219static int cs89x0_probe1(struct net_device *dev, int ioaddr);
 220static int net_open(struct net_device *dev);
 221static int net_send_packet(struct sk_buff *skb, struct net_device *dev);
 222static void net_interrupt(int irq, void *dev_id, struct pt_regs *regs);
 223static void set_multicast_list(struct net_device *dev);
 224static void net_timeout(struct net_device *dev);
 225static void net_rx(struct net_device *dev);
 226static int net_close(struct net_device *dev);
 227static struct net_device_stats *net_get_stats(struct net_device *dev);
 228static void reset_chip(struct net_device *dev);
 229static int get_eeprom_data(struct net_device *dev, int off, int len, int *buffer);
 230static int get_eeprom_cksum(int off, int len, int *buffer);
 231static int set_mac_address(struct net_device *dev, void *addr);
 232static void count_rx_errors(int status, struct net_local *lp);
 233#if ALLOW_DMA
 234static void get_dma_channel(struct net_device *dev);
 235static void release_dma_buff(struct net_local *lp);
 236#endif
 237
 238/* Example routines you must write ;->. */
 239#define tx_done(dev) 1
 240
 241/*
 242 * Permit 'cs89x0_dma=N' in the kernel boot environment
 243 */
 244#if !defined(MODULE) && (ALLOW_DMA != 0)
 245static int g_cs89x0_dma;
 246
 247static int __init dma_fn(char *str)
 248{
 249        g_cs89x0_dma = simple_strtol(str,NULL,0);
 250        return 1;
 251}
 252
 253__setup("cs89x0_dma=", dma_fn);
 254#endif  /* !defined(MODULE) && (ALLOW_DMA != 0) */
 255
 256#ifndef MODULE
 257static int g_cs89x0_media__force;
 258
 259static int __init media_fn(char *str)
 260{
 261        if (!strcmp(str, "rj45")) g_cs89x0_media__force = FORCE_RJ45;
 262        else if (!strcmp(str, "aui")) g_cs89x0_media__force = FORCE_AUI;
 263        else if (!strcmp(str, "bnc")) g_cs89x0_media__force = FORCE_BNC;
 264        return 1;
 265}
 266
 267__setup("cs89x0_media=", media_fn);
 268#endif
 269
 270
 271/* Check for a network adaptor of this type, and return '0' iff one exists.
 272   If dev->base_addr == 0, probe all likely locations.
 273   If dev->base_addr == 1, always return failure.
 274   If dev->base_addr == 2, allocate space for the device and return success
 275   (detachable devices only).
 276   Return 0 on success.
 277   */
 278
 279int __init cs89x0_probe(struct net_device *dev)
 280{
 281        int i;
 282        int base_addr = dev ? dev->base_addr : 0;
 283
 284        SET_MODULE_OWNER(dev);
 285
 286        if (net_debug)
 287                printk("cs89x0:cs89x0_probe(0x%x)\n", base_addr);
 288
 289        if (base_addr > 0x1ff)          /* Check a single specified location. */
 290                return cs89x0_probe1(dev, base_addr);
 291        else if (base_addr != 0)        /* Don't probe at all. */
 292                return -ENXIO;
 293
 294        for (i = 0; netcard_portlist[i]; i++) {
 295                if (cs89x0_probe1(dev, netcard_portlist[i]) == 0)
 296                        return 0;
 297        }
 298        printk(KERN_WARNING "cs89x0: no cs8900 or cs8920 detected.  Be sure to disable PnP with SETUP\n");
 299        return -ENODEV;
 300}
 301
 302static int
 303readreg(struct net_device *dev, int portno)
 304{
 305        outw(portno, dev->base_addr + ADD_PORT);
 306        return inw(dev->base_addr + DATA_PORT);
 307}
 308
 309static void
 310writereg(struct net_device *dev, int portno, int value)
 311{
 312        outw(portno, dev->base_addr + ADD_PORT);
 313        outw(value, dev->base_addr + DATA_PORT);
 314}
 315
 316static int
 317readword(struct net_device *dev, int portno)
 318{
 319        return inw(dev->base_addr + portno);
 320}
 321
 322static void
 323writeword(struct net_device *dev, int portno, int value)
 324{
 325        outw(value, dev->base_addr + portno);
 326}
 327
 328static int __init
 329wait_eeprom_ready(struct net_device *dev)
 330{
 331        int timeout = jiffies;
 332        /* check to see if the EEPROM is ready, a timeout is used -
 333           just in case EEPROM is ready when SI_BUSY in the
 334           PP_SelfST is clear */
 335        while(readreg(dev, PP_SelfST) & SI_BUSY)
 336                if (jiffies - timeout >= 40)
 337                        return -1;
 338        return 0;
 339}
 340
 341static int __init
 342get_eeprom_data(struct net_device *dev, int off, int len, int *buffer)
 343{
 344        int i;
 345
 346        if (net_debug > 3) printk("EEPROM data from %x for %x:\n",off,len);
 347        for (i = 0; i < len; i++) {
 348                if (wait_eeprom_ready(dev) < 0) return -1;
 349                /* Now send the EEPROM read command and EEPROM location to read */
 350                writereg(dev, PP_EECMD, (off + i) | EEPROM_READ_CMD);
 351                if (wait_eeprom_ready(dev) < 0) return -1;
 352                buffer[i] = readreg(dev, PP_EEData);
 353                if (net_debug > 3) printk("%04x ", buffer[i]);
 354        }
 355        if (net_debug > 3) printk("\n");
 356        return 0;
 357}
 358
 359static int  __init
 360get_eeprom_cksum(int off, int len, int *buffer)
 361{
 362        int i, cksum;
 363
 364        cksum = 0;
 365        for (i = 0; i < len; i++)
 366                cksum += buffer[i];
 367        cksum &= 0xffff;
 368        if (cksum == 0)
 369                return 0;
 370        return -1;
 371}
 372
 373/* This is the real probe routine.  Linux has a history of friendly device
 374   probes on the ISA bus.  A good device probes avoids doing writes, and
 375   verifies that the correct device exists and functions.
 376   Return 0 on success.
 377 */
 378
 379static int __init
 380cs89x0_probe1(struct net_device *dev, int ioaddr)
 381{
 382        struct net_local *lp;
 383        static unsigned version_printed;
 384        int i;
 385        unsigned rev_type = 0;
 386        int eeprom_buff[CHKSUM_LEN];
 387        int retval;
 388
 389        /* Initialize the device structure. */
 390        if (dev->priv == NULL) {
 391                dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
 392                if (dev->priv == 0) {
 393                        retval = -ENOMEM;
 394                        goto out;
 395                }
 396                lp = (struct net_local *)dev->priv;
 397                memset(lp, 0, sizeof(*lp));
 398                spin_lock_init(&lp->lock);
 399#if !defined(MODULE) && (ALLOW_DMA != 0)
 400                if (g_cs89x0_dma) {
 401                        lp->use_dma = 1;
 402                        lp->dma = g_cs89x0_dma;
 403                        lp->dmasize = 16;       /* Could make this an option... */
 404                }
 405#endif
 406#ifndef MODULE
 407                lp->force = g_cs89x0_media__force;
 408#endif
 409        }
 410        lp = (struct net_local *)dev->priv;
 411
 412        /* Grab the region so we can find another board if autoIRQ fails. */
 413        if (!request_region(ioaddr & ~3, NETCARD_IO_EXTENT, dev->name)) {
 414                printk(KERN_ERR "%s: request_region(0x%x, 0x%x) failed\n",
 415                                dev->name, ioaddr, NETCARD_IO_EXTENT);
 416                retval = -EBUSY;
 417                goto out1;
 418        }
 419
 420#ifdef CONFIG_SH_HICOSH4
 421        /* truely reset the chip */
 422        outw(0x0114, ioaddr + ADD_PORT);
 423        outw(0x0040, ioaddr + DATA_PORT);
 424#endif
 425
 426        /* if they give us an odd I/O address, then do ONE write to
 427           the address port, to get it back to address zero, where we
 428           expect to find the EISA signature word. An IO with a base of 0x3
 429           will skip the test for the ADD_PORT. */
 430        if (ioaddr & 1) {
 431                if (net_debug > 1)
 432                        printk(KERN_INFO "%s: odd ioaddr 0x%x\n", dev->name, ioaddr);
 433                if ((ioaddr & 2) != 2)
 434                        if ((inw((ioaddr & ~3)+ ADD_PORT) & ADD_MASK) != ADD_SIG) {
 435                                printk(KERN_ERR "%s: bad signature 0x%x\n",
 436                                        dev->name, inw((ioaddr & ~3)+ ADD_PORT));
 437                                retval = -ENODEV;
 438                                goto out2;
 439                        }
 440                ioaddr &= ~3;
 441                outw(PP_ChipID, ioaddr + ADD_PORT);
 442        }
 443printk("PP_addr=0x%x\n", inw(ioaddr + ADD_PORT));
 444
 445        if (inw(ioaddr + DATA_PORT) != CHIP_EISA_ID_SIG) {
 446                printk(KERN_ERR "%s: incorrect signature 0x%x\n",
 447                        dev->name, inw(ioaddr + DATA_PORT));
 448                retval = -ENODEV;
 449                goto out2;
 450        }
 451
 452        /* Fill in the 'dev' fields. */
 453        dev->base_addr = ioaddr;
 454
 455        /* get the chip type */
 456        rev_type = readreg(dev, PRODUCT_ID_ADD);
 457        lp->chip_type = rev_type &~ REVISON_BITS;
 458        lp->chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
 459
 460        /* Check the chip type and revision in order to set the correct send command
 461        CS8920 revision C and CS8900 revision F can use the faster send. */
 462        lp->send_cmd = TX_AFTER_381;
 463        if (lp->chip_type == CS8900 && lp->chip_revision >= 'F')
 464                lp->send_cmd = TX_NOW;
 465        if (lp->chip_type != CS8900 && lp->chip_revision >= 'C')
 466                lp->send_cmd = TX_NOW;
 467
 468        if (net_debug  &&  version_printed++ == 0)
 469                printk(version);
 470
 471        printk(KERN_INFO "%s: cs89%c0%s rev %c found at %#3lx ",
 472               dev->name,
 473               lp->chip_type==CS8900?'0':'2',
 474               lp->chip_type==CS8920M?"M":"",
 475               lp->chip_revision,
 476               dev->base_addr);
 477
 478        reset_chip(dev);
 479   
 480        /* Here we read the current configuration of the chip. If there
 481           is no Extended EEPROM then the idea is to not disturb the chip
 482           configuration, it should have been correctly setup by automatic
 483           EEPROM read on reset. So, if the chip says it read the EEPROM
 484           the driver will always do *something* instead of complain that
 485           adapter_cnf is 0. */
 486
 487#ifdef CONFIG_SH_HICOSH4
 488        if (1) {
 489                /* For the HiCO.SH4 board, things are different: we don't
 490                   have EEPROM, but there is some data in flash, so we go
 491                   get it there directly (MAC). */
 492                __u16 *confd;
 493                short cnt;
 494                if (((* (volatile __u32 *) 0xa0013ff0) & 0x00ffffff)
 495                        == 0x006c3000) {
 496                        confd = (__u16*) 0xa0013fc0;
 497                } else {
 498                        confd = (__u16*) 0xa001ffc0;
 499                }
 500                cnt = (*confd++ & 0x00ff) >> 1;
 501                while (--cnt > 0) {
 502                        __u16 j = *confd++;
 503                        
 504                        switch (j & 0x0fff) {
 505                        case PP_IA:
 506                                for (i = 0; i < ETH_ALEN/2; i++) {
 507                                        dev->dev_addr[i*2] = confd[i] & 0xFF;
 508                                        dev->dev_addr[i*2+1] = confd[i] >> 8;
 509                                }
 510                                break;
 511                        }
 512                        j = (j >> 12) + 1;
 513                        confd += j;
 514                        cnt -= j;
 515                }
 516        } else
 517#endif
 518
 519        if ((readreg(dev, PP_SelfST) & (EEPROM_OK | EEPROM_PRESENT)) == 
 520              (EEPROM_OK|EEPROM_PRESENT)) {
 521                /* Load the MAC. */
 522                for (i=0; i < ETH_ALEN/2; i++) {
 523                        unsigned int Addr;
 524                        Addr = readreg(dev, PP_IA+i*2);
 525                        dev->dev_addr[i*2] = Addr & 0xFF;
 526                        dev->dev_addr[i*2+1] = Addr >> 8;
 527                }
 528   
 529                /* Load the Adapter Configuration. 
 530                   Note:  Barring any more specific information from some 
 531                   other source (ie EEPROM+Schematics), we would not know 
 532                   how to operate a 10Base2 interface on the AUI port. 
 533                   However, since we  do read the status of HCB1 and use 
 534                   settings that always result in calls to control_dc_dc(dev,0) 
 535                   a BNC interface should work if the enable pin 
 536                   (dc/dc converter) is on HCB1. It will be called AUI 
 537                   however. */
 538           
 539                lp->adapter_cnf = 0;
 540                i = readreg(dev, PP_LineCTL);
 541                /* Preserve the setting of the HCB1 pin. */
 542                if ((i & (HCB1 | HCB1_ENBL)) ==  (HCB1 | HCB1_ENBL))
 543                        lp->adapter_cnf |= A_CNF_DC_DC_POLARITY;
 544                /* Save the sqelch bit */
 545                if ((i & LOW_RX_SQUELCH) == LOW_RX_SQUELCH)
 546                        lp->adapter_cnf |= A_CNF_EXTND_10B_2 | A_CNF_LOW_RX_SQUELCH;
 547                /* Check if the card is in 10Base-t only mode */
 548                if ((i & (AUI_ONLY | AUTO_AUI_10BASET)) == 0)
 549                        lp->adapter_cnf |=  A_CNF_10B_T | A_CNF_MEDIA_10B_T;
 550                /* Check if the card is in AUI only mode */
 551                if ((i & (AUI_ONLY | AUTO_AUI_10BASET)) == AUI_ONLY)
 552                        lp->adapter_cnf |=  A_CNF_AUI | A_CNF_MEDIA_AUI;
 553                /* Check if the card is in Auto mode. */
 554                if ((i & (AUI_ONLY | AUTO_AUI_10BASET)) == AUTO_AUI_10BASET)
 555                        lp->adapter_cnf |=  A_CNF_AUI | A_CNF_10B_T | 
 556                        A_CNF_MEDIA_AUI | A_CNF_MEDIA_10B_T | A_CNF_MEDIA_AUTO;
 557                
 558                if (net_debug > 1)
 559                        printk(KERN_INFO "%s: PP_LineCTL=0x%x, adapter_cnf=0x%x\n",
 560                                        dev->name, i, lp->adapter_cnf);
 561
 562                /* IRQ. Other chips already probe, see below. */
 563                if (lp->chip_type == CS8900) 
 564                        lp->isa_config = readreg(dev, PP_CS8900_ISAINT) & INT_NO_MASK;
 565           
 566                printk( "[Cirrus EEPROM] ");
 567        }
 568
 569        printk("\n");
 570   
 571        /* First check to see if an EEPROM is attached. */
 572#ifdef CONFIG_SH_HICOSH4 /* no EEPROM on HiCO, don't hazzle with it here */
 573        if (1) {
 574                printk(KERN_NOTICE "cs89x0: No EEPROM on HiCO.SH4\n");
 575        } else
 576#endif
 577        if ((readreg(dev, PP_SelfST) & EEPROM_PRESENT) == 0)
 578                printk(KERN_WARNING "cs89x0: No EEPROM, relying on command line....\n");
 579        else if (get_eeprom_data(dev, START_EEPROM_DATA,CHKSUM_LEN,eeprom_buff) < 0) {
 580                printk(KERN_WARNING "\ncs89x0: EEPROM read failed, relying on command line.\n");
 581        } else if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN,eeprom_buff) < 0) {
 582                /* Check if the chip was able to read its own configuration starting
 583                   at 0 in the EEPROM*/
 584                if ((readreg(dev, PP_SelfST) & (EEPROM_OK | EEPROM_PRESENT)) !=
 585                    (EEPROM_OK|EEPROM_PRESENT)) 
 586                        printk(KERN_WARNING "cs89x0: Extended EEPROM checksum bad and no Cirrus EEPROM, relying on command line\n");
 587                   
 588        } else {
 589                /* This reads an extended EEPROM that is not documented
 590                   in the CS8900 datasheet. */
 591                
 592                /* get transmission control word  but keep the autonegotiation bits */
 593                if (!lp->auto_neg_cnf) lp->auto_neg_cnf = eeprom_buff[AUTO_NEG_CNF_OFFSET/2];
 594                /* Store adapter configuration */
 595                if (!lp->adapter_cnf) lp->adapter_cnf = eeprom_buff[ADAPTER_CNF_OFFSET/2];
 596                /* Store ISA configuration */
 597                lp->isa_config = eeprom_buff[ISA_CNF_OFFSET/2];
 598                dev->mem_start = eeprom_buff[PACKET_PAGE_OFFSET/2] << 8;
 599
 600                /* eeprom_buff has 32-bit ints, so we can't just memcpy it */
 601                /* store the initial memory base address */
 602                for (i = 0; i < ETH_ALEN/2; i++) {
 603                        dev->dev_addr[i*2] = eeprom_buff[i];
 604                        dev->dev_addr[i*2+1] = eeprom_buff[i] >> 8;
 605                }
 606                if (net_debug > 1)
 607                        printk(KERN_DEBUG "%s: new adapter_cnf: 0x%x\n",
 608                                dev->name, lp->adapter_cnf);
 609        }
 610
 611        /* allow them to force multiple transceivers.  If they force multiple, autosense */
 612        {
 613                int count = 0;
 614                if (lp->force & FORCE_RJ45)     {lp->adapter_cnf |= A_CNF_10B_T; count++; }
 615                if (lp->force & FORCE_AUI)      {lp->adapter_cnf |= A_CNF_AUI; count++; }
 616                if (lp->force & FORCE_BNC)      {lp->adapter_cnf |= A_CNF_10B_2; count++; }
 617                if (count > 1)                  {lp->adapter_cnf |= A_CNF_MEDIA_AUTO; }
 618                else if (lp->force & FORCE_RJ45){lp->adapter_cnf |= A_CNF_MEDIA_10B_T; }
 619                else if (lp->force & FORCE_AUI) {lp->adapter_cnf |= A_CNF_MEDIA_AUI; }
 620                else if (lp->force & FORCE_BNC) {lp->adapter_cnf |= A_CNF_MEDIA_10B_2; }
 621        }
 622
 623        if (net_debug > 1)
 624                printk(KERN_DEBUG "%s: after force 0x%x, adapter_cnf=0x%x\n",
 625                        dev->name, lp->force, lp->adapter_cnf);
 626
 627        /* FIXME: We don't let you set dc-dc polarity or low RX squelch from the command line: add it here */
 628
 629        /* FIXME: We don't let you set the IMM bit from the command line: add it to lp->auto_neg_cnf here */
 630
 631        /* FIXME: we don't set the Ethernet address on the command line.  Use
 632           ifconfig IFACE hw ether AABBCCDDEEFF */
 633
 634        printk(KERN_INFO "cs89x0 media %s%s%s",
 635               (lp->adapter_cnf & A_CNF_10B_T)?"RJ-45,":"",
 636               (lp->adapter_cnf & A_CNF_AUI)?"AUI,":"",
 637               (lp->adapter_cnf & A_CNF_10B_2)?"BNC,":"");
 638
 639        lp->irq_map = 0xffff;
 640
 641        /* If this is a CS8900 then no pnp soft */
 642        if (lp->chip_type != CS8900 &&
 643            /* Check if the ISA IRQ has been set  */
 644                (i = readreg(dev, PP_CS8920_ISAINT) & 0xff,
 645                 (i != 0 && i < CS8920_NO_INTS))) {
 646                if (!dev->irq)
 647                        dev->irq = i;
 648        } else {
 649                i = lp->isa_config & INT_NO_MASK;
 650                if (lp->chip_type == CS8900) {
 651                        /* Translate the IRQ using the IRQ mapping table. */
 652                        if (i >= sizeof(cs8900_irq_map)/sizeof(cs8900_irq_map[0]))
 653                                printk("\ncs89x0: invalid ISA interrupt number %d\n", i);
 654                        else
 655                                i = cs8900_irq_map[i];
 656                        
 657                        lp->irq_map = CS8900_IRQ_MAP; /* fixed IRQ map for CS8900 */
 658                } else {
 659                        int irq_map_buff[IRQ_MAP_LEN/2];
 660
 661                        if (get_eeprom_data(dev, IRQ_MAP_EEPROM_DATA,
 662                                            IRQ_MAP_LEN/2,
 663                                            irq_map_buff) >= 0) {
 664                                if ((irq_map_buff[0] & 0xff) == PNP_IRQ_FRMT)
 665                                        lp->irq_map = (irq_map_buff[0]>>8) | (irq_map_buff[1] << 8);
 666                        }
 667                }
 668                if (!dev->irq)
 669                        dev->irq = i;
 670        }
 671
 672        printk(" IRQ %d", dev->irq);
 673
 674#if ALLOW_DMA
 675        if (lp->use_dma) {
 676                get_dma_channel(dev);
 677                printk(", DMA %d", dev->dma);
 678        }
 679        else
 680#endif
 681        {
 682                printk(", programmed I/O");
 683        }
 684
 685        /* print the ethernet address. */
 686        printk(", MAC");
 687        for (i = 0; i < ETH_ALEN; i++)
 688        {
 689                printk("%c%02x", i ? ':' : ' ', dev->dev_addr[i]);
 690        }
 691
 692        dev->open               = net_open;
 693        dev->stop               = net_close;
 694        dev->tx_timeout         = net_timeout;
 695        dev->watchdog_timeo     = HZ;
 696        dev->hard_start_xmit    = net_send_packet;
 697        dev->get_stats          = net_get_stats;
 698        dev->set_multicast_list = set_multicast_list;
 699        dev->set_mac_address    = set_mac_address;
 700
 701        /* Fill in the fields of the device structure with ethernet values. */
 702        ether_setup(dev);
 703
 704        printk("\n");
 705        if (net_debug)
 706                printk("cs89x0_probe1() successful\n");
 707        return 0;
 708out2:
 709        release_region(ioaddr & ~3, NETCARD_IO_EXTENT);
 710out1:
 711        kfree(dev->priv);
 712        dev->priv = 0;
 713out:
 714        return retval;
 715}
 716
 717
 718/*********************************
 719 * This page contains DMA routines
 720**********************************/
 721
 722#if ALLOW_DMA
 723
 724#define dma_page_eq(ptr1, ptr2) ((long)(ptr1)>>17 == (long)(ptr2)>>17)
 725
 726static void
 727get_dma_channel(struct net_device *dev)
 728{
 729        struct net_local *lp = (struct net_local *)dev->priv;
 730
 731        if (lp->dma) {
 732                dev->dma = lp->dma;
 733                lp->isa_config |= ISA_RxDMA;
 734        } else {
 735                if ((lp->isa_config & ANY_ISA_DMA) == 0)
 736                        return;
 737                dev->dma = lp->isa_config & DMA_NO_MASK;
 738                if (lp->chip_type == CS8900)
 739                        dev->dma += 5;
 740                if (dev->dma < 5 || dev->dma > 7) {
 741                        lp->isa_config &= ~ANY_ISA_DMA;
 742                        return;
 743                }
 744        }
 745        return;
 746}
 747
 748static void
 749write_dma(struct net_device *dev, int chip_type, int dma)
 750{
 751        struct net_local *lp = (struct net_local *)dev->priv;
 752        if ((lp->isa_config & ANY_ISA_DMA) == 0)
 753                return;
 754        if (chip_type == CS8900) {
 755                writereg(dev, PP_CS8900_ISADMA, dma-5);
 756        } else {
 757                writereg(dev, PP_CS8920_ISADMA, dma);
 758        }
 759}
 760
 761static void
 762set_dma_cfg(struct net_device *dev)
 763{
 764        struct net_local *lp = (struct net_local *)dev->priv;
 765
 766        if (lp->use_dma) {
 767                if ((lp->isa_config & ANY_ISA_DMA) == 0) {
 768                        if (net_debug > 3)
 769                                printk("set_dma_cfg(): no DMA\n");
 770                        return;
 771                }
 772                if (lp->isa_config & ISA_RxDMA) {
 773                        lp->curr_rx_cfg |= RX_DMA_ONLY;
 774                        if (net_debug > 3)
 775                                printk("set_dma_cfg(): RX_DMA_ONLY\n");
 776                } else {
 777                        lp->curr_rx_cfg |= AUTO_RX_DMA; /* not that we support it... */
 778                        if (net_debug > 3)
 779                                printk("set_dma_cfg(): AUTO_RX_DMA\n");
 780                }
 781        }
 782}
 783
 784static int
 785dma_bufcfg(struct net_device *dev)
 786{
 787        struct net_local *lp = (struct net_local *)dev->priv;
 788        if (lp->use_dma)
 789                return (lp->isa_config & ANY_ISA_DMA)? RX_DMA_ENBL : 0;
 790        else
 791                return 0;
 792}
 793
 794static int
 795dma_busctl(struct net_device *dev)
 796{
 797        int retval = 0;
 798        struct net_local *lp = (struct net_local *)dev->priv;
 799        if (lp->use_dma) {
 800                if (lp->isa_config & ANY_ISA_DMA)
 801                        retval |= RESET_RX_DMA; /* Reset the DMA pointer */
 802                if (lp->isa_config & DMA_BURST)
 803                        retval |= DMA_BURST_MODE; /* Does ISA config specify DMA burst ? */
 804                if (lp->dmasize == 64)
 805                        retval |= RX_DMA_SIZE_64K; /* did they ask for 64K? */
 806                retval |= MEMORY_ON;    /* we need memory enabled to use DMA. */
 807        }
 808        return retval;
 809}
 810
 811static void
 812dma_rx(struct net_device *dev)
 813{
 814        struct net_local *lp = (struct net_local *)dev->priv;
 815        struct sk_buff *skb;
 816        int status, length;
 817        unsigned char *bp = lp->rx_dma_ptr;
 818
 819        status = bp[0] + (bp[1]<<8);
 820        length = bp[2] + (bp[3]<<8);
 821        bp += 4;
 822        if (net_debug > 5) {
 823                printk( "%s: receiving DMA packet at %lx, status %x, length %x\n",
 824                        dev->name, (unsigned long)bp, status, length);
 825        }
 826        if ((status & RX_OK) == 0) {
 827                count_rx_errors(status, lp);
 828                goto skip_this_frame;
 829        }
 830
 831        /* Malloc up new buffer. */
 832        skb = dev_alloc_skb(length + 2);
 833        if (skb == NULL) {
 834                if (net_debug)  /* I don't think we want to do this to a stressed system */
 835                        printk("%s: Memory squeeze, dropping packet.\n", dev->name);
 836                lp->stats.rx_dropped++;
 837
 838                /* AKPM: advance bp to the next frame */
 839skip_this_frame:
 840                bp += (length + 3) & ~3;
 841                if (bp >= lp->end_dma_buff) bp -= lp->dmasize*1024;
 842                lp->rx_dma_ptr = bp;
 843                return;
 844        }
 845        skb_reserve(skb, 2);    /* longword align L3 header */
 846        skb->dev = dev;
 847
 848        if (bp + length > lp->end_dma_buff) {
 849                int semi_cnt = lp->end_dma_buff - bp;
 850                memcpy(skb_put(skb,semi_cnt), bp, semi_cnt);
 851                memcpy(skb_put(skb,length - semi_cnt), lp->dma_buff,
 852                       length - semi_cnt);
 853        } else {
 854                memcpy(skb_put(skb,length), bp, length);
 855        }
 856        bp += (length + 3) & ~3;
 857        if (bp >= lp->end_dma_buff) bp -= lp->dmasize*1024;
 858        lp->rx_dma_ptr = bp;
 859
 860        if (net_debug > 3) {
 861                printk( "%s: received %d byte DMA packet of type %x\n",
 862                        dev->name, length,
 863                        (skb->data[ETH_ALEN+ETH_ALEN] << 8) | skb->data[ETH_ALEN+ETH_ALEN+1]);
 864        }
 865        skb->protocol=eth_type_trans(skb,dev);
 866        netif_rx(skb);
 867        dev->last_rx = jiffies;
 868        lp->stats.rx_packets++;
 869        lp->stats.rx_bytes += length;
 870}
 871
 872#endif  /* ALLOW_DMA */
 873
 874void  __init reset_chip(struct net_device *dev)
 875{
 876        struct net_local *lp = (struct net_local *)dev->priv;
 877        int ioaddr = dev->base_addr;
 878        int reset_start_time;
 879
 880        writereg(dev, PP_SelfCTL, readreg(dev, PP_SelfCTL) | POWER_ON_RESET);
 881
 882        /* wait 30 ms */
 883        current->state = TASK_INTERRUPTIBLE;
 884        schedule_timeout(30*HZ/1000);
 885
 886        if (lp->chip_type != CS8900) {
 887                /* Hardware problem requires PNP registers to be reconfigured after a reset */
 888                outw(PP_CS8920_ISAINT, ioaddr + ADD_PORT);
 889                outb(dev->irq, ioaddr + DATA_PORT);
 890                outb(0,      ioaddr + DATA_PORT + 1);
 891
 892                outw(PP_CS8920_ISAMemB, ioaddr + ADD_PORT);
 893                outb((dev->mem_start >> 16) & 0xff, ioaddr + DATA_PORT);
 894                outb((dev->mem_start >> 8) & 0xff,   ioaddr + DATA_PORT + 1);
 895        }
 896        /* Wait until the chip is reset */
 897        reset_start_time = jiffies;
 898        while( (readreg(dev, PP_SelfST) & INIT_DONE) == 0 && jiffies - reset_start_time < 2)
 899                ;
 900}
 901
 902
 903static void
 904control_dc_dc(struct net_device *dev, int on_not_off)
 905{
 906        struct net_local *lp = (struct net_local *)dev->priv;
 907        unsigned int selfcontrol;
 908        int timenow = jiffies;
 909        /* control the DC to DC convertor in the SelfControl register.  
 910           Note: This is hooked up to a general purpose pin, might not
 911           always be a DC to DC convertor. */
 912
 913        selfcontrol = HCB1_ENBL; /* Enable the HCB1 bit as an output */
 914        if (((lp->adapter_cnf & A_CNF_DC_DC_POLARITY) != 0) ^ on_not_off)
 915                selfcontrol |= HCB1;
 916        else
 917                selfcontrol &= ~HCB1;
 918        writereg(dev, PP_SelfCTL, selfcontrol);
 919
 920        /* Wait for the DC/DC converter to power up - 500ms */
 921        while (jiffies - timenow < HZ)
 922                ;
 923}
 924
 925#define DETECTED_NONE  0
 926#define DETECTED_RJ45H 1
 927#define DETECTED_RJ45F 2
 928#define DETECTED_AUI   3
 929#define DETECTED_BNC   4
 930
 931static int
 932detect_tp(struct net_device *dev)
 933{
 934        struct net_local *lp = (struct net_local *)dev->priv;
 935        int timenow = jiffies;
 936        int fdx;
 937
 938        if (net_debug > 1) printk("%s: Attempting TP\n", dev->name);
 939
 940        /* If connected to another full duplex capable 10-Base-T card the link pulses
 941           seem to be lost when the auto detect bit in the LineCTL is set.
 942           To overcome this the auto detect bit will be cleared whilst testing the
 943           10-Base-T interface.  This would not be necessary for the sparrow chip but
 944           is simpler to do it anyway. */
 945        writereg(dev, PP_LineCTL, lp->linectl &~ AUI_ONLY);
 946        control_dc_dc(dev, 0);
 947
 948        /* Delay for the hardware to work out if the TP cable is present - 150ms */
 949        for (timenow = jiffies; jiffies - timenow < 15; )
 950                ;
 951        if ((readreg(dev, PP_LineST) & LINK_OK) == 0)
 952                return DETECTED_NONE;
 953
 954        if (lp->chip_type == CS8900) {
 955                switch (lp->force & 0xf0) {
 956#if 0
 957                case FORCE_AUTO:
 958                        printk("%s: cs8900 doesn't autonegotiate\n",dev->name);
 959                        return DETECTED_NONE;
 960#endif
 961                /* CS8900 doesn't support AUTO, change to HALF*/
 962                case FORCE_AUTO:
 963                        lp->force &= ~FORCE_AUTO;
 964                        lp->force |= FORCE_HALF;
 965                        break;
 966                case FORCE_HALF:
 967                        break;
 968                case FORCE_FULL:
 969                        writereg(dev, PP_TestCTL, readreg(dev, PP_TestCTL) | FDX_8900);
 970                        break;
 971                }
 972                fdx = readreg(dev, PP_TestCTL) & FDX_8900;
 973        } else {
 974                switch (lp->force & 0xf0) {
 975                case FORCE_AUTO:
 976                        lp->auto_neg_cnf = AUTO_NEG_ENABLE;
 977                        break;
 978                case FORCE_HALF:
 979                        lp->auto_neg_cnf = 0;
 980                        break;
 981                case FORCE_FULL:
 982                        lp->auto_neg_cnf = RE_NEG_NOW | ALLOW_FDX;
 983                        break;
 984                }
 985
 986                writereg(dev, PP_AutoNegCTL, lp->auto_neg_cnf & AUTO_NEG_MASK);
 987
 988                if ((lp->auto_neg_cnf & AUTO_NEG_BITS) == AUTO_NEG_ENABLE) {
 989                        printk(KERN_INFO "%s: negotiating duplex...\n",dev->name);
 990                        while (readreg(dev, PP_AutoNegST) & AUTO_NEG_BUSY) {
 991                                if (jiffies - timenow > 4000) {
 992                                        printk(KERN_ERR "**** Full / half duplex auto-negotiation timed out ****\n");
 993                                        break;
 994                                }
 995                        }
 996                }
 997                fdx = readreg(dev, PP_AutoNegST) & FDX_ACTIVE;
 998        }
 999        if (fdx)
1000                return DETECTED_RJ45F;
1001        else
1002                return DETECTED_RJ45H;
1003}
1004
1005/* send a test packet - return true if carrier bits are ok */
1006static int
1007send_test_pkt(struct net_device *dev)
1008{
1009        char test_packet[] = { 0,0,0,0,0,0, 0,0,0,0,0,0,
1010                                 0, 46, /* A 46 in network order */
1011                                 0, 0, /* DSAP=0 & SSAP=0 fields */
1012                                 0xf3, 0 /* Control (Test Req + P bit set) */ };
1013        long timenow = jiffies;
1014
1015        writereg(dev, PP_LineCTL, readreg(dev, PP_LineCTL) | SERIAL_TX_ON);
1016
1017        memcpy(test_packet,          dev->dev_addr, ETH_ALEN);
1018        memcpy(test_packet+ETH_ALEN, dev->dev_addr, ETH_ALEN);
1019
1020        writeword(dev, TX_CMD_PORT, TX_AFTER_ALL);
1021        writeword(dev, TX_LEN_PORT, ETH_ZLEN);
1022
1023        /* Test to see if the chip has allocated memory for the packet */
1024        while (jiffies - timenow < 5)
1025                if (readreg(dev, PP_BusST) & READY_FOR_TX_NOW)
1026                        break;
1027        if (jiffies - timenow >= 5)
1028                return 0;       /* this shouldn't happen */
1029
1030        /* Write the contents of the packet */
1031        outsw(dev->base_addr + TX_FRAME_PORT,test_packet,(ETH_ZLEN+1) >>1);
1032
1033        if (net_debug > 1) printk("Sending test packet ");
1034        /* wait a couple of jiffies for packet to be received */
1035        for (timenow = jiffies; jiffies - timenow < 3; )
1036                ;
1037        if ((readreg(dev, PP_TxEvent) & TX_SEND_OK_BITS) == TX_OK) {
1038                if (net_debug > 1) printk("succeeded\n");
1039                return 1;
1040        }
1041        if (net_debug > 1) printk("failed\n");
1042        return 0;
1043}
1044
1045
1046static int
1047detect_aui(struct net_device *dev)
1048{
1049        struct net_local *lp = (struct net_local *)dev->priv;
1050
1051        if (net_debug > 1) printk("%s: Attempting AUI\n", dev->name);
1052        control_dc_dc(dev, 0);
1053
1054        writereg(dev, PP_LineCTL, (lp->linectl &~ AUTO_AUI_10BASET) | AUI_ONLY);
1055
1056        if (send_test_pkt(dev))
1057                return DETECTED_AUI;
1058        else
1059                return DETECTED_NONE;
1060}
1061
1062static int
1063detect_bnc(struct net_device *dev)
1064{
1065        struct net_local *lp = (struct net_local *)dev->priv;
1066
1067        if (net_debug > 1) printk("%s: Attempting BNC\n", dev->name);
1068        control_dc_dc(dev, 1);
1069
1070        writereg(dev, PP_LineCTL, (lp->linectl &~ AUTO_AUI_10BASET) | AUI_ONLY);
1071
1072        if (send_test_pkt(dev))
1073                return DETECTED_BNC;
1074        else
1075                return DETECTED_NONE;
1076}
1077
1078
1079static void
1080write_irq(struct net_device *dev, int chip_type, int irq)
1081{
1082        int i;
1083
1084        if (chip_type == CS8900) {
1085                /* Search the mapping table for the corresponding IRQ pin. */
1086                for (i = 0; i != sizeof(cs8900_irq_map)/sizeof(cs8900_irq_map[0]); i++)
1087                        if (cs8900_irq_map[i] == irq)
1088                                break;
1089                /* Not found */
1090                if (i == sizeof(cs8900_irq_map)/sizeof(cs8900_irq_map[0]))
1091                        i = 3;
1092                writereg(dev, PP_CS8900_ISAINT, i);
1093        } else {
1094                writereg(dev, PP_CS8920_ISAINT, irq);
1095        }
1096}
1097
1098/* Open/initialize the board.  This is called (in the current kernel)
1099   sometime after booting when the 'ifconfig' program is run.
1100
1101   This routine should set everything up anew at each open, even
1102   registers that "should" only need to be set once at boot, so that
1103   there is non-reboot way to recover if something goes wrong.
1104   */
1105
1106/* AKPM: do we need to do any locking here? */
1107
1108static int
1109net_open(struct net_device *dev)
1110{
1111        struct net_local *lp = (struct net_local *)dev->priv;
1112        int result = 0;
1113        int i;
1114        int ret;
1115
1116#ifndef CONFIG_SH_HICOSH4 /* uses irq#1, so this wont work */
1117        if (dev->irq < 2) {
1118                /* Allow interrupts to be generated by the chip */
1119/* Cirrus' release had this: */
1120#if 0
1121                writereg(dev, PP_BusCTL, readreg(dev, PP_BusCTL)|ENABLE_IRQ );
1122#endif
1123/* And 2.3.47 had this: */
1124                writereg(dev, PP_BusCTL, ENABLE_IRQ | MEMORY_ON);
1125
1126                for (i = 2; i < CS8920_NO_INTS; i++) {
1127                        if ((1 << i) & lp->irq_map) {
1128                                if (request_irq(i, net_interrupt, 0, dev->name, dev) == 0) {
1129                                        dev->irq = i;
1130                                        write_irq(dev, lp->chip_type, i);
1131                                        /* writereg(dev, PP_BufCFG, GENERATE_SW_INTERRUPT); */
1132                                        break;
1133                                }
1134                        }
1135                }
1136
1137                if (i >= CS8920_NO_INTS) {
1138                        writereg(dev, PP_BusCTL, 0);    /* disable interrupts. */
1139                        printk(KERN_ERR "cs89x0: can't get an interrupt\n");
1140                        ret = -EAGAIN;
1141                        goto bad_out;
1142                }
1143        }
1144        else
1145#endif
1146        {
1147                if (((1 << dev->irq) & lp->irq_map) == 0) {
1148                        printk(KERN_ERR "%s: IRQ %d is not in our map of allowable IRQs, which is %x\n",
1149                               dev->name, dev->irq, lp->irq_map);
1150                        ret = -EAGAIN;
1151                        goto bad_out;
1152                }
1153/* FIXME: Cirrus' release had this: */
1154                writereg(dev, PP_BusCTL, readreg(dev, PP_BusCTL)|ENABLE_IRQ );
1155/* And 2.3.47 had this: */
1156#if 0
1157                writereg(dev, PP_BusCTL, ENABLE_IRQ | MEMORY_ON);
1158#endif
1159                write_irq(dev, lp->chip_type, dev->irq);
1160                ret = request_irq(dev->irq, &net_interrupt, 0, dev->name, dev);
1161                if (ret) {
1162                        if (net_debug)
1163                                printk(KERN_DEBUG "cs89x0: request_irq(%d) failed\n", dev->irq);
1164                        goto bad_out;
1165                }
1166        }
1167
1168#if ALLOW_DMA
1169        if (lp->use_dma) {
1170                if (lp->isa_config & ANY_ISA_DMA) {
1171                        unsigned long flags;
1172                        lp->dma_buff = (unsigned char *)__get_dma_pages(GFP_KERNEL,
1173                                                        get_order(lp->dmasize * 1024));
1174
1175                        if (!lp->dma_buff) {
1176                                printk(KERN_ERR "%s: cannot get %dK memory for DMA\n", dev->name, lp->dmasize);
1177                                goto release_irq;
1178                        }
1179                        if (net_debug > 1) {
1180                                printk( "%s: dma %lx %lx\n",
1181                                        dev->name,
1182                                        (unsigned long)lp->dma_buff,
1183                                        (unsigned long)isa_virt_to_bus(lp->dma_buff));
1184                        }
1185                        if ((unsigned long) lp->dma_buff >= MAX_DMA_ADDRESS ||
1186                            !dma_page_eq(lp->dma_buff, lp->dma_buff+lp->dmasize*1024-1)) {
1187                                printk(KERN_ERR "%s: not usable as DMA buffer\n", dev->name);
1188                                goto release_irq;
1189                        }
1190                        memset(lp->dma_buff, 0, lp->dmasize * 1024);    /* Why? */
1191                        if (request_dma(dev->dma, dev->name)) {
1192                                printk(KERN_ERR "%s: cannot get dma channel %d\n", dev->name, dev->dma);
1193                                goto release_irq;
1194                        }
1195                        write_dma(dev, lp->chip_type, dev->dma);
1196                        lp->rx_dma_ptr = lp->dma_buff;
1197                        lp->end_dma_buff = lp->dma_buff + lp->dmasize*1024;
1198                        spin_lock_irqsave(&lp->lock, flags);
1199                        disable_dma(dev->dma);
1200                        clear_dma_ff(dev->dma);
1201                        set_dma_mode(dev->dma, 0x14); /* auto_init as well */
1202                        set_dma_addr(dev->dma, isa_virt_to_bus(lp->dma_buff));
1203                        set_dma_count(dev->dma, lp->dmasize*1024);
1204                        enable_dma(dev->dma);
1205                        spin_unlock_irqrestore(&lp->lock, flags);
1206                }
1207        }
1208#endif  /* ALLOW_DMA */
1209
1210        /* set the Ethernet address */
1211        for (i=0; i < ETH_ALEN/2; i++)
1212                writereg(dev, PP_IA+i*2, dev->dev_addr[i*2] | (dev->dev_addr[i*2+1] << 8));
1213
1214        /* while we're testing the interface, leave interrupts disabled */
1215        writereg(dev, PP_BusCTL, MEMORY_ON);
1216
1217        /* Set the LineCTL quintuplet based on adapter configuration read from EEPROM */
1218        if ((lp->adapter_cnf & A_CNF_EXTND_10B_2) && (lp->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
1219                lp->linectl = LOW_RX_SQUELCH;
1220        else
1221                lp->linectl = 0;
1222
1223        /* check to make sure that they have the "right" hardware available */
1224        switch(lp->adapter_cnf & A_CNF_MEDIA_TYPE) {
1225        case A_CNF_MEDIA_10B_T: result = lp->adapter_cnf & A_CNF_10B_T; break;
1226        case A_CNF_MEDIA_AUI:   result = lp->adapter_cnf & A_CNF_AUI; break;
1227        case A_CNF_MEDIA_10B_2: result = lp->adapter_cnf & A_CNF_10B_2; break;
1228        default: result = lp->adapter_cnf & (A_CNF_10B_T | A_CNF_AUI | A_CNF_10B_2);
1229        }
1230        if (!result) {
1231                printk(KERN_ERR "%s: EEPROM is configured for unavailable media\n", dev->name);
1232        release_irq:
1233#if ALLOW_DMA
1234                release_dma_buff(lp);
1235#endif
1236                writereg(dev, PP_LineCTL, readreg(dev, PP_LineCTL) & ~(SERIAL_TX_ON | SERIAL_RX_ON));
1237                free_irq(dev->irq, dev);
1238                ret = -EAGAIN;
1239                goto bad_out;
1240        }
1241
1242        /* set the hardware to the configured choice */
1243        switch(lp->adapter_cnf & A_CNF_MEDIA_TYPE) {
1244        case A_CNF_MEDIA_10B_T:
1245                result = detect_tp(dev);
1246                if (result==DETECTED_NONE) {
1247                        printk(KERN_WARNING "%s: 10Base-T (RJ-45) has no cable\n", dev->name);
1248                        if (lp->auto_neg_cnf & IMM_BIT) /* check "ignore missing media" bit */
1249                                result = DETECTED_RJ45H; /* Yes! I don't care if I see a link pulse */
1250                }
1251                break;
1252        case A_CNF_MEDIA_AUI:
1253                result = detect_aui(dev);
1254                if (result==DETECTED_NONE) {
1255                        printk(KERN_WARNING "%s: 10Base-5 (AUI) has no cable\n", dev->name);
1256                        if (lp->auto_neg_cnf & IMM_BIT) /* check "ignore missing media" bit */
1257                                result = DETECTED_AUI; /* Yes! I don't care if I see a carrrier */
1258                }
1259                break;
1260        case A_CNF_MEDIA_10B_2:
1261                result = detect_bnc(dev);
1262                if (result==DETECTED_NONE) {
1263                        printk(KERN_WARNING "%s: 10Base-2 (BNC) has no cable\n", dev->name);
1264                        if (lp->auto_neg_cnf & IMM_BIT) /* check "ignore missing media" bit */
1265                                result = DETECTED_BNC; /* Yes! I don't care if I can xmit a packet */
1266                }
1267                break;
1268        case A_CNF_MEDIA_AUTO:
1269                writereg(dev, PP_LineCTL, lp->linectl | AUTO_AUI_10BASET);
1270                if (lp->adapter_cnf & A_CNF_10B_T)
1271                        if ((result = detect_tp(dev)) != DETECTED_NONE)
1272                                break;
1273                if (lp->adapter_cnf & A_CNF_AUI)
1274                        if ((result = detect_aui(dev)) != DETECTED_NONE)
1275                                break;
1276                if (lp->adapter_cnf & A_CNF_10B_2)
1277                        if ((result = detect_bnc(dev)) != DETECTED_NONE)
1278                                break;
1279                printk(KERN_ERR "%s: no media detected\n", dev->name);
1280                goto release_irq;
1281        }
1282        switch(result) {
1283        case DETECTED_NONE:
1284                printk(KERN_ERR "%s: no network cable attached to configured media\n", dev->name);
1285                goto release_irq;
1286        case DETECTED_RJ45H:
1287                printk(KERN_INFO "%s: using half-duplex 10Base-T (RJ-45)\n", dev->name);
1288                break;
1289        case DETECTED_RJ45F:
1290                printk(KERN_INFO "%s: using full-duplex 10Base-T (RJ-45)\n", dev->name);
1291                break;
1292        case DETECTED_AUI:
1293                printk(KERN_INFO "%s: using 10Base-5 (AUI)\n", dev->name);
1294                break;
1295        case DETECTED_BNC:
1296                printk(KERN_INFO "%s: using 10Base-2 (BNC)\n", dev->name);
1297                break;
1298        }
1299
1300        /* Turn on both receive and transmit operations */
1301        writereg(dev, PP_LineCTL, readreg(dev, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON);
1302
1303        /* Receive only error free packets addressed to this card */
1304        lp->rx_mode = 0;
1305        writereg(dev, PP_RxCTL, DEF_RX_ACCEPT);
1306
1307        lp->curr_rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL;
1308
1309        if (lp->isa_config & STREAM_TRANSFER)
1310                lp->curr_rx_cfg |= RX_STREAM_ENBL;
1311#if ALLOW_DMA
1312        set_dma_cfg(dev);
1313#endif
1314        writereg(dev, PP_RxCFG, lp->curr_rx_cfg);
1315
1316        writereg(dev, PP_TxCFG, TX_LOST_CRS_ENBL | TX_SQE_ERROR_ENBL | TX_OK_ENBL |
1317                TX_LATE_COL_ENBL | TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
1318
1319        writereg(dev, PP_BufCFG, READY_FOR_TX_ENBL | RX_MISS_COUNT_OVRFLOW_ENBL |
1320#if ALLOW_DMA
1321                dma_bufcfg(dev) |
1322#endif
1323                TX_COL_COUNT_OVRFLOW_ENBL | TX_UNDERRUN_ENBL);
1324
1325        /* now that we've got our act together, enable everything */
1326        writereg(dev, PP_BusCTL, ENABLE_IRQ
1327                 | (dev->mem_start?MEMORY_ON : 0) /* turn memory on */
1328#if ALLOW_DMA
1329                 | dma_busctl(dev)
1330#endif
1331                 );
1332        netif_start_queue(dev);
1333        if (net_debug > 1)
1334                printk("cs89x0: net_open() succeeded\n");
1335        return 0;
1336bad_out:
1337        return ret;
1338}
1339
1340static void net_timeout(struct net_device *dev)
1341{
1342        /* If we get here, some higher level has decided we are broken.
1343           There should really be a "kick me" function call instead. */
1344        if (net_debug > 0) printk("%s: transmit timed out, %s?\n", dev->name,
1345                   tx_done(dev) ? "IRQ conflict ?" : "network cable problem");
1346        /* Try to restart the adaptor. */
1347        netif_wake_queue(dev);
1348}
1349
1350static int net_send_packet(struct sk_buff *skb, struct net_device *dev)
1351{
1352        struct net_local *lp = (struct net_local *)dev->priv;
1353
1354        if (net_debug > 3) {
1355                printk("%s: sent %d byte packet of type %x\n",
1356                        dev->name, skb->len,
1357                        (skb->data[ETH_ALEN+ETH_ALEN] << 8) | skb->data[ETH_ALEN+ETH_ALEN+1]);
1358        }
1359
1360        /* keep the upload from being interrupted, since we
1361                  ask the chip to start transmitting before the
1362                  whole packet has been completely uploaded. */
1363
1364        spin_lock_irq(&lp->lock);
1365        netif_stop_queue(dev);
1366
1367        /* initiate a transmit sequence */
1368        writeword(dev, TX_CMD_PORT, lp->send_cmd);
1369        writeword(dev, TX_LEN_PORT, skb->len);
1370
1371        /* Test to see if the chip has allocated memory for the packet */
1372        if ((readreg(dev, PP_BusST) & READY_FOR_TX_NOW) == 0) {
1373                /*
1374                 * Gasp!  It hasn't.  But that shouldn't happen since
1375                 * we're waiting for TxOk, so return 1 and requeue this packet.
1376                 */
1377                
1378                spin_unlock_irq(&lp->lock);
1379                if (net_debug) printk("cs89x0: Tx buffer not free!\n");
1380                return 1;
1381        }
1382        /* Write the contents of the packet */
1383        outsw(dev->base_addr + TX_FRAME_PORT,skb->data,(skb->len+1) >>1);
1384        spin_unlock_irq(&lp->lock);
1385        dev->trans_start = jiffies;
1386        dev_kfree_skb (skb);
1387
1388        /*
1389         * We DO NOT call netif_wake_queue() here.
1390         * We also DO NOT call netif_start_queue().
1391         *
1392         * Either of these would cause another bottom half run through
1393         * net_send_packet() before this packet has fully gone out.  That causes
1394         * us to hit the "Gasp!" above and the send is rescheduled.  it runs like
1395         * a dog.  We just return and wait for the Tx completion interrupt handler
1396         * to restart the netdevice layer
1397         */
1398
1399        return 0;
1400}
1401
1402/* The typical workload of the driver:
1403   Handle the network interface interrupts. */
1404   
1405static void net_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1406{
1407        struct net_device *dev = dev_id;
1408        struct net_local *lp;
1409        int ioaddr, status;
1410 
1411        ioaddr = dev->base_addr;
1412        lp = (struct net_local *)dev->priv;
1413
1414        /* we MUST read all the events out of the ISQ, otherwise we'll never
1415           get interrupted again.  As a consequence, we can't have any limit
1416           on the number of times we loop in the interrupt handler.  The
1417           hardware guarantees that eventually we'll run out of events.  Of
1418           course, if you're on a slow machine, and packets are arriving
1419           faster than you can read them off, you're screwed.  Hasta la
1420           vista, baby!  */
1421        while ((status = readword(dev, ISQ_PORT))) {
1422                if (net_debug > 4)printk("%s: event=%04x\n", dev->name, status);
1423                switch(status & ISQ_EVENT_MASK) {
1424                case ISQ_RECEIVER_EVENT:
1425                        /* Got a packet(s). */
1426                        net_rx(dev);
1427                        break;
1428                case ISQ_TRANSMITTER_EVENT:
1429                        lp->stats.tx_packets++;
1430                        netif_wake_queue(dev);  /* Inform upper layers. */
1431                        if ((status & ( TX_OK |
1432                                        TX_LOST_CRS |
1433                                        TX_SQE_ERROR |
1434                                        TX_LATE_COL |
1435                                        TX_16_COL)) != TX_OK) {
1436                                if ((status & TX_OK) == 0) lp->stats.tx_errors++;
1437                                if (status & TX_LOST_CRS) lp->stats.tx_carrier_errors++;
1438                                if (status & TX_SQE_ERROR) lp->stats.tx_heartbeat_errors++;
1439                                if (status & TX_LATE_COL) lp->stats.tx_window_errors++;
1440                                if (status & TX_16_COL) lp->stats.tx_aborted_errors++;
1441                        }
1442                        break;
1443                case ISQ_BUFFER_EVENT:
1444                        if (status & READY_FOR_TX) {
1445                                /* we tried to transmit a packet earlier,
1446                                   but inexplicably ran out of buffers.
1447                                   That shouldn't happen since we only ever
1448                                   load one packet.  Shrug.  Do the right
1449                                   thing anyway. */
1450                                netif_wake_queue(dev);  /* Inform upper layers. */
1451                        }
1452                        if (status & TX_UNDERRUN) {
1453                                if (net_debug > 0) printk("%s: transmit underrun\n", dev->name);
1454                                lp->send_underrun++;
1455                                if (lp->send_underrun == 3) lp->send_cmd = TX_AFTER_381;
1456                                else if (lp->send_underrun == 6) lp->send_cmd = TX_AFTER_ALL;
1457                                /* transmit cycle is done, although
1458                                   frame wasn't transmitted - this
1459                                   avoids having to wait for the upper
1460                                   layers to timeout on us, in the
1461                                   event of a tx underrun */
1462                                netif_wake_queue(dev);  /* Inform upper layers. */
1463                        }
1464#if ALLOW_DMA
1465                        if (lp->use_dma && (status & RX_DMA)) {
1466                                int count = readreg(dev, PP_DmaFrameCnt);
1467                                while(count) {
1468                                        if (net_debug > 5)
1469                                                printk("%s: receiving %d DMA frames\n", dev->name, count);
1470                                        if (net_debug > 2 && count >1)
1471                                                printk("%s: receiving %d DMA frames\n", dev->name, count);
1472                                        dma_rx(dev);
1473                                        if (--count == 0)
1474                                                count = readreg(dev, PP_DmaFrameCnt);
1475                                        if (net_debug > 2 && count > 0)
1476                                                printk("%s: continuing with %d DMA frames\n", dev->name, count);
1477                                }
1478                        }
1479#endif
1480                        break;
1481                case ISQ_RX_MISS_EVENT:
1482                        lp->stats.rx_missed_errors += (status >>6);
1483                        break;
1484                case ISQ_TX_COL_EVENT:
1485                        lp->stats.collisions += (status >>6);
1486                        break;
1487                }
1488        }
1489}
1490
1491static void
1492count_rx_errors(int status, struct net_local *lp)
1493{
1494        lp->stats.rx_errors++;
1495        if (status & RX_RUNT) lp->stats.rx_length_errors++;
1496        if (status & RX_EXTRA_DATA) lp->stats.rx_length_errors++;
1497        if (status & RX_CRC_ERROR) if (!(status & (RX_EXTRA_DATA|RX_RUNT)))
1498                /* per str 172 */
1499                lp->stats.rx_crc_errors++;
1500        if (status & RX_DRIBBLE) lp->stats.rx_frame_errors++;
1501        return;
1502}
1503
1504/* We have a good packet(s), get it/them out of the buffers. */
1505static void
1506net_rx(struct net_device *dev)
1507{
1508        struct net_local *lp = (struct net_local *)dev->priv;
1509        struct sk_buff *skb;
1510        int status, length;
1511
1512        int ioaddr = dev->base_addr;
1513        status = inw(ioaddr + RX_FRAME_PORT);
1514        length = inw(ioaddr + RX_FRAME_PORT);
1515
1516        if ((status & RX_OK) == 0) {
1517                count_rx_errors(status, lp);
1518                return;
1519        }
1520
1521        /* Malloc up new buffer. */
1522        skb = dev_alloc_skb(length + 2);
1523        if (skb == NULL) {
1524#if 0           /* Again, this seems a cruel thing to do */
1525                printk(KERN_WARNING "%s: Memory squeeze, dropping packet.\n", dev->name);
1526#endif
1527                lp->stats.rx_dropped++;
1528                return;
1529        }
1530        skb_reserve(skb, 2);    /* longword align L3 header */
1531        skb->dev = dev;
1532
1533        insw(ioaddr + RX_FRAME_PORT, skb_put(skb, length), length >> 1);
1534        if (length & 1)
1535                skb->data[length-1] = inw(ioaddr + RX_FRAME_PORT);
1536
1537        if (net_debug > 3) {
1538                printk( "%s: received %d byte packet of type %x\n",
1539                        dev->name, length,
1540                        (skb->data[ETH_ALEN+ETH_ALEN] << 8) | skb->data[ETH_ALEN+ETH_ALEN+1]);
1541        }
1542
1543        skb->protocol=eth_type_trans(skb,dev);
1544        netif_rx(skb);
1545        dev->last_rx = jiffies;
1546        lp->stats.rx_packets++;
1547        lp->stats.rx_bytes += length;
1548}
1549
1550#if ALLOW_DMA
1551static void release_dma_buff(struct net_local *lp)
1552{
1553        if (lp->dma_buff) {
1554                free_pages((unsigned long)(lp->dma_buff), get_order(lp->dmasize * 1024));
1555                lp->dma_buff = 0;
1556        }
1557}
1558#endif
1559
1560/* The inverse routine to net_open(). */
1561static int
1562net_close(struct net_device *dev)
1563{
1564        struct net_local *lp = (struct net_local *)dev->priv;
1565
1566        netif_stop_queue(dev);
1567        
1568        writereg(dev, PP_RxCFG, 0);
1569        writereg(dev, PP_TxCFG, 0);
1570        writereg(dev, PP_BufCFG, 0);
1571        writereg(dev, PP_BusCTL, 0);
1572
1573        free_irq(dev->irq, dev);
1574
1575#if ALLOW_DMA
1576        if (lp->use_dma && lp->dma) {
1577                free_dma(dev->dma);
1578                release_dma_buff(lp);
1579        }
1580#endif
1581
1582        /* Update the statistics here. */
1583        return 0;
1584}
1585
1586/* Get the current statistics.  This may be called with the card open or
1587   closed. */
1588static struct net_device_stats *
1589net_get_stats(struct net_device *dev)
1590{
1591        struct net_local *lp = (struct net_local *)dev->priv;
1592        unsigned long flags;
1593
1594        spin_lock_irqsave(&lp->lock, flags);
1595        /* Update the statistics from the device registers. */
1596        lp->stats.rx_missed_errors += (readreg(dev, PP_RxMiss) >> 6);
1597        lp->stats.collisions += (readreg(dev, PP_TxCol) >> 6);
1598        spin_unlock_irqrestore(&lp->lock, flags);
1599
1600        return &lp->stats;
1601}
1602
1603static void set_multicast_list(struct net_device *dev)
1604{
1605        struct net_local *lp = (struct net_local *)dev->priv;
1606        unsigned long flags;
1607
1608        spin_lock_irqsave(&lp->lock, flags);
1609        if(dev->flags&IFF_PROMISC)
1610        {
1611                lp->rx_mode = RX_ALL_ACCEPT;
1612        }
1613        else if((dev->flags&IFF_ALLMULTI)||dev->mc_list)
1614        {
1615                /* The multicast-accept list is initialized to accept-all, and we
1616                   rely on higher-level filtering for now. */
1617                lp->rx_mode = RX_MULTCAST_ACCEPT;
1618        } 
1619        else
1620                lp->rx_mode = 0;
1621
1622        writereg(dev, PP_RxCTL, DEF_RX_ACCEPT | lp->rx_mode);
1623
1624        /* in promiscuous mode, we accept errored packets, so we have to enable interrupts on them also */
1625        writereg(dev, PP_RxCFG, lp->curr_rx_cfg |
1626             (lp->rx_mode == RX_ALL_ACCEPT? (RX_CRC_ERROR_ENBL|RX_RUNT_ENBL|RX_EXTRA_DATA_ENBL) : 0));
1627        spin_unlock_irqrestore(&lp->lock, flags);
1628}
1629
1630
1631static int set_mac_address(struct net_device *dev, void *addr)
1632{
1633        int i;
1634
1635        if (netif_running(dev))
1636                return -EBUSY;
1637        if (net_debug) {
1638                printk("%s: Setting MAC address to ", dev->name);
1639                for (i = 0; i < 6; i++)
1640                        printk(" %2.2x", dev->dev_addr[i] = ((unsigned char *)addr)[i]);
1641                printk(".\n");
1642        }
1643        /* set the Ethernet address */
1644        for (i=0; i < ETH_ALEN/2; i++)
1645                writereg(dev, PP_IA+i*2, dev->dev_addr[i*2] | (dev->dev_addr[i*2+1] << 8));
1646
1647        return 0;
1648}
1649
1650#ifdef MODULE
1651
1652static struct net_device dev_cs89x0 = {
1653        "",
1654        0, 0, 0, 0,
1655        0, 0,
1656        0, 0, 0, NULL, NULL };
1657
1658/*
1659 * Support the 'debug' module parm even if we're compiled for non-debug to 
1660 * avoid breaking someone's startup scripts 
1661 */
1662
1663static int io;
1664static int irq;
1665static int debug;
1666static char media[8];
1667static int duplex=-1;
1668
1669static int use_dma;                     /* These generate unused var warnings if ALLOW_DMA = 0 */
1670static int dma;
1671static int dmasize=16;                  /* or 64 */
1672
1673MODULE_PARM(io, "i");
1674MODULE_PARM(irq, "i");
1675MODULE_PARM(debug, "i");
1676MODULE_PARM(media, "c8");
1677MODULE_PARM(duplex, "i");
1678MODULE_PARM(dma , "i");
1679MODULE_PARM(dmasize , "i");
1680MODULE_PARM(use_dma , "i");
1681MODULE_PARM_DESC(io, "cs89x0 I/O base address");
1682MODULE_PARM_DESC(irq, "cs89x0 IRQ number");
1683#if DEBUGGING
1684MODULE_PARM_DESC(debug, "cs89x0 debug level (0-6)");
1685#else
1686MODULE_PARM_DESC(debug, "(ignored)");
1687#endif
1688MODULE_PARM_DESC(media, "Set cs89x0 adapter(s) media type(s) (rj45,bnc,aui)");
1689/* No other value than -1 for duplex seems to be currently interpreted */
1690MODULE_PARM_DESC(duplex, "(ignored)");
1691#if ALLOW_DMA
1692MODULE_PARM_DESC(dma , "cs89x0 ISA DMA channel; ignored if use_dma=0");
1693MODULE_PARM_DESC(dmasize , "cs89x0 DMA size in kB (16,64); ignored if use_dma=0");
1694MODULE_PARM_DESC(use_dma , "cs89x0 using DMA (0-1)");
1695#else
1696MODULE_PARM_DESC(dma , "(ignored)");
1697MODULE_PARM_DESC(dmasize , "(ignored)");
1698MODULE_PARM_DESC(use_dma , "(ignored)");
1699#endif
1700
1701MODULE_AUTHOR("Mike Cruse, Russwll Nelson <nelson@crynwr.com>, Andrew Morton <andrewm@uow.edu.au>");
1702MODULE_LICENSE("GPL");
1703
1704
1705/*
1706* media=t             - specify media type
1707   or media=2
1708   or media=aui
1709   or medai=auto
1710* duplex=0            - specify forced half/full/autonegotiate duplex
1711* debug=#             - debug level
1712
1713
1714* Default Chip Configuration:
1715  * DMA Burst = enabled
1716  * IOCHRDY Enabled = enabled
1717    * UseSA = enabled
1718    * CS8900 defaults to half-duplex if not specified on command-line
1719    * CS8920 defaults to autoneg if not specified on command-line
1720    * Use reset defaults for other config parameters
1721
1722* Assumptions:
1723  * media type specified is supported (circuitry is present)
1724  * if memory address is > 1MB, then required mem decode hw is present
1725  * if 10B-2, then agent other than driver will enable DC/DC converter
1726    (hw or software util)
1727
1728
1729*/
1730
1731int
1732init_module(void)
1733{
1734        struct net_local *lp;
1735        int ret = 0;
1736
1737#if DEBUGGING
1738        net_debug = debug;
1739#else
1740        debug = 0;
1741#endif
1742
1743        dev_cs89x0.irq = irq;
1744        dev_cs89x0.base_addr = io;
1745
1746        dev_cs89x0.init = cs89x0_probe;
1747        dev_cs89x0.priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
1748        if (dev_cs89x0.priv == 0) {
1749                printk(KERN_ERR "cs89x0.c: Out of memory.\n");
1750                return -ENOMEM;
1751        }
1752        memset(dev_cs89x0.priv, 0, sizeof(struct net_local));
1753        lp = (struct net_local *)dev_cs89x0.priv;
1754
1755#if ALLOW_DMA
1756        if (use_dma) {
1757                lp->use_dma = use_dma;
1758                lp->dma = dma;
1759                lp->dmasize = dmasize;
1760        }
1761#endif
1762
1763        spin_lock_init(&lp->lock);
1764
1765        /* boy, they'd better get these right */
1766        if (!strcmp(media, "rj45"))
1767                lp->adapter_cnf = A_CNF_MEDIA_10B_T | A_CNF_10B_T;
1768        else if (!strcmp(media, "aui"))
1769                lp->adapter_cnf = A_CNF_MEDIA_AUI   | A_CNF_AUI;
1770        else if (!strcmp(media, "bnc"))
1771                lp->adapter_cnf = A_CNF_MEDIA_10B_2 | A_CNF_10B_2;
1772        else
1773                lp->adapter_cnf = A_CNF_MEDIA_10B_T | A_CNF_10B_T;
1774
1775        if (duplex==-1)
1776                lp->auto_neg_cnf = AUTO_NEG_ENABLE;
1777
1778        if (io == 0) {
1779                printk(KERN_ERR "cs89x0.c: Module autoprobing not allowed.\n");
1780                printk(KERN_ERR "cs89x0.c: Append io=0xNNN\n");
1781                ret = -EPERM;
1782                goto out;
1783        }
1784
1785#if ALLOW_DMA
1786        if (use_dma && dmasize != 16 && dmasize != 64) {
1787                printk(KERN_ERR "cs89x0.c: dma size must be either 16K or 64K, not %dK\n", dmasize);
1788                ret = -EPERM;
1789                goto out;
1790        }
1791#endif
1792
1793        if (register_netdev(&dev_cs89x0) != 0) {
1794                printk(KERN_ERR "cs89x0.c: No card found at 0x%x\n", io);
1795                ret = -ENXIO;
1796                goto out;
1797        }
1798out:
1799        if (ret)
1800                kfree(dev_cs89x0.priv);
1801        return ret;
1802}
1803
1804void
1805cleanup_module(void)
1806{
1807        if (dev_cs89x0.priv != NULL) {
1808                /* Free up the private structure, or leak memory :-)  */
1809                unregister_netdev(&dev_cs89x0);
1810                outw(PP_ChipID, dev_cs89x0.base_addr + ADD_PORT);
1811                kfree(dev_cs89x0.priv);
1812                dev_cs89x0.priv = NULL; /* gets re-allocated by cs89x0_probe1 */
1813                /* If we don't do this, we can't re-insmod it later. */
1814                release_region(dev_cs89x0.base_addr, NETCARD_IO_EXTENT);
1815        }
1816}
1817#endif /* MODULE */
1818
1819/*
1820 * Local variables:
1821 *  version-control: t
1822 *  kept-new-versions: 5
1823 *  c-indent-level: 8
1824 *  tab-width: 8
1825 * End:
1826 *
1827 */
1828
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.