linux-bk/drivers/net/3c509.c
<<
>>
Prefs
   1/* 3c509.c: A 3c509 EtherLink3 ethernet driver for linux. */
   2/*
   3        Written 1993-2000 by Donald Becker.
   4
   5        Copyright 1994-2000 by Donald Becker.
   6        Copyright 1993 United States Government as represented by the
   7        Director, National Security Agency.      This software may be used and
   8        distributed according to the terms of the GNU General Public License,
   9        incorporated herein by reference.
  10
  11        This driver is for the 3Com EtherLinkIII series.
  12
  13        The author may be reached as becker@scyld.com, or C/O
  14        Scyld Computing Corporation
  15        410 Severn Ave., Suite 210
  16        Annapolis MD 21403
  17
  18        Known limitations:
  19        Because of the way 3c509 ISA detection works it's difficult to predict
  20        a priori which of several ISA-mode cards will be detected first.
  21
  22        This driver does not use predictive interrupt mode, resulting in higher
  23        packet latency but lower overhead.  If interrupts are disabled for an
  24        unusually long time it could also result in missed packets, but in
  25        practice this rarely happens.
  26
  27
  28        FIXES:
  29                Alan Cox:       Removed the 'Unexpected interrupt' bug.
  30                Michael Meskes: Upgraded to Donald Becker's version 1.07.
  31                Alan Cox:       Increased the eeprom delay. Regardless of 
  32                                what the docs say some people definitely
  33                                get problems with lower (but in card spec)
  34                                delays
  35                v1.10 4/21/97 Fixed module code so that multiple cards may be detected,
  36                                other cleanups.  -djb
  37                Andrea Arcangeli:       Upgraded to Donald Becker's version 1.12.
  38                Rick Payne:     Fixed SMP race condition
  39                v1.13 9/8/97 Made 'max_interrupt_work' an insmod-settable variable -djb
  40                v1.14 10/15/97 Avoided waiting..discard message for fast machines -djb
  41                v1.15 1/31/98 Faster recovery for Tx errors. -djb
  42                v1.16 2/3/98 Different ID port handling to avoid sound cards. -djb
  43                v1.18 12Mar2001 Andrew Morton <andrewm@uow.edu.au>
  44                        - Avoid bogus detect of 3c590's (Andrzej Krzysztofowicz)
  45                        - Reviewed against 1.18 from scyld.com
  46                v1.18a 17Nov2001 Jeff Garzik <jgarzik@mandrakesoft.com>
  47                        - ethtool support
  48                v1.18b 1Mar2002 Zwane Mwaikambo <zwane@commfireservices.com>
  49                        - Power Management support
  50                v1.18c 1Mar2002 David Ruggiero <jdr@farfalle.com>
  51                        - Full duplex support
  52*/
  53
  54#define DRV_NAME        "3c509"
  55#define DRV_VERSION     "1.18c"
  56#define DRV_RELDATE     "1Mar2002"
  57
  58/* A few values that may be tweaked. */
  59
  60/* Time in jiffies before concluding the transmitter is hung. */
  61#define TX_TIMEOUT  (400*HZ/1000)
  62/* Maximum events (Rx packets, etc.) to handle at each interrupt. */
  63static int max_interrupt_work = 10;
  64
  65#include <linux/config.h>
  66#include <linux/module.h>
  67
  68#include <linux/mca.h>
  69#include <linux/isapnp.h>
  70#include <linux/sched.h>
  71#include <linux/string.h>
  72#include <linux/interrupt.h>
  73#include <linux/errno.h>
  74#include <linux/in.h>
  75#include <linux/slab.h>
  76#include <linux/ioport.h>
  77#include <linux/init.h>
  78#include <linux/netdevice.h>
  79#include <linux/etherdevice.h>
  80#include <linux/skbuff.h>
  81#include <linux/delay.h>        /* for udelay() */
  82#include <linux/spinlock.h>
  83#include <linux/ethtool.h>
  84
  85#include <asm/uaccess.h>
  86#include <asm/bitops.h>
  87#include <asm/io.h>
  88#include <asm/irq.h>
  89#include <linux/pm.h>
  90
  91static char versionA[] __initdata = DRV_NAME ".c:" DRV_VERSION " " DRV_RELDATE " becker@scyld.com\n";
  92static char versionB[] __initdata = "http://www.scyld.com/network/3c509.html\n";
  93
  94#ifdef EL3_DEBUG
  95static int el3_debug = EL3_DEBUG;
  96#else
  97static int el3_debug = 2;
  98#endif
  99
 100
 101/* To minimize the size of the driver source I only define operating
 102   constants if they are used several times.  You'll need the manual
 103   anyway if you want to understand driver details. */
 104/* Offsets from base I/O address. */
 105#define EL3_DATA 0x00
 106#define EL3_CMD 0x0e
 107#define EL3_STATUS 0x0e
 108#define  EEPROM_READ 0x80
 109
 110#define EL3_IO_EXTENT   16
 111
 112#define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
 113
 114
 115/* The top five bits written to EL3_CMD are a command, the lower
 116   11 bits are the parameter, if applicable. */
 117enum c509cmd {
 118        TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
 119        RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11,
 120        TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
 121        FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
 122        SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
 123        SetTxThreshold = 18<<11, SetTxStart = 19<<11, StatsEnable = 21<<11,
 124        StatsDisable = 22<<11, StopCoax = 23<<11, PowerUp = 27<<11,
 125        PowerDown = 28<<11, PowerAuto = 29<<11};
 126
 127enum c509status {
 128        IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
 129        TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
 130        IntReq = 0x0040, StatsFull = 0x0080, CmdBusy = 0x1000, };
 131
 132/* The SetRxFilter command accepts the following classes: */
 133enum RxFilter {
 134        RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8 };
 135
 136/* Register window 1 offsets, the window used in normal operation. */
 137#define TX_FIFO         0x00
 138#define RX_FIFO         0x00
 139#define RX_STATUS       0x08
 140#define TX_STATUS       0x0B
 141#define TX_FREE         0x0C            /* Remaining free bytes in Tx buffer. */
 142
 143#define WN0_IRQ         0x08            /* Window 0: Set IRQ line in bits 12-15. */
 144#define WN4_MEDIA       0x0A            /* Window 4: Various transcvr/media bits. */
 145#define  MEDIA_TP       0x00C0          /* Enable link beat and jabber for 10baseT. */
 146#define WN4_NETDIAG     0x06            /* Window 4: Net diagnostic */
 147#define FD_ENABLE       0x8000          /* Enable full-duplex ("external loopback") */  
 148
 149/*
 150 * Must be a power of two (we use a binary and in the
 151 * circular queue)
 152 */
 153#define SKB_QUEUE_SIZE  64
 154
 155struct el3_private {
 156        struct net_device_stats stats;
 157        struct net_device *next_dev;
 158        spinlock_t lock;
 159        /* skb send-queue */
 160        int head, size;
 161        struct sk_buff *queue[SKB_QUEUE_SIZE];
 162        char mca_slot;
 163#ifdef CONFIG_PM
 164        struct pm_dev *pmdev;
 165#endif
 166};
 167static int id_port __initdata = 0x110;  /* Start with 0x110 to avoid new sound cards.*/
 168static struct net_device *el3_root_dev;
 169
 170static ushort id_read_eeprom(int index);
 171static ushort read_eeprom(int ioaddr, int index);
 172static int el3_open(struct net_device *dev);
 173static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev);
 174static void el3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
 175static void update_stats(struct net_device *dev);
 176static struct net_device_stats *el3_get_stats(struct net_device *dev);
 177static int el3_rx(struct net_device *dev);
 178static int el3_close(struct net_device *dev);
 179static void set_multicast_list(struct net_device *dev);
 180static void el3_tx_timeout (struct net_device *dev);
 181static int netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd);
 182static void el3_down(struct net_device *dev);
 183static void el3_up(struct net_device *dev);
 184#ifdef CONFIG_PM
 185static int el3_suspend(struct pm_dev *pdev);
 186static int el3_resume(struct pm_dev *pdev);
 187static int el3_pm_callback(struct pm_dev *pdev, pm_request_t rqst, void *data);
 188#endif
 189
 190#ifdef CONFIG_MCA
 191struct el3_mca_adapters_struct {
 192        char* name;
 193        int id;
 194};
 195
 196static struct el3_mca_adapters_struct el3_mca_adapters[] __initdata = {
 197        { "3Com 3c529 EtherLink III (10base2)", 0x627c },
 198        { "3Com 3c529 EtherLink III (10baseT)", 0x627d },
 199        { "3Com 3c529 EtherLink III (test mode)", 0x62db },
 200        { "3Com 3c529 EtherLink III (TP or coax)", 0x62f6 },
 201        { "3Com 3c529 EtherLink III (TP)", 0x62f7 },
 202        { NULL, 0 },
 203};
 204#endif /* CONFIG_MCA */
 205
 206#ifdef __ISAPNP__
 207static struct isapnp_device_id el3_isapnp_adapters[] __initdata = {
 208        {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
 209                ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5090),
 210                (long) "3Com Etherlink III (TP)" },
 211        {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
 212                ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5091),
 213                (long) "3Com Etherlink III" },
 214        {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
 215                ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5094),
 216                (long) "3Com Etherlink III (combo)" },
 217        {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
 218                ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5095),
 219                (long) "3Com Etherlink III (TPO)" },
 220        {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
 221                ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5098),
 222                (long) "3Com Etherlink III (TPC)" },
 223        {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
 224                ISAPNP_VENDOR('P', 'N', 'P'), ISAPNP_FUNCTION(0x80f7),
 225                (long) "3Com Etherlink III compatible" },
 226        {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
 227                ISAPNP_VENDOR('P', 'N', 'P'), ISAPNP_FUNCTION(0x80f8),
 228                (long) "3Com Etherlink III compatible" },
 229        { }     /* terminate list */
 230};
 231
 232static u16 el3_isapnp_phys_addr[8][3];
 233#endif /* __ISAPNP__ */
 234static int nopnp;
 235
 236int __init el3_probe(struct net_device *dev, int card_idx)
 237{
 238        struct el3_private *lp;
 239        short lrs_state = 0xff, i;
 240        int ioaddr, irq, if_port;
 241        u16 phys_addr[3];
 242        static int current_tag;
 243        int mca_slot = -1;
 244#ifdef __ISAPNP__
 245        static int pnp_cards;
 246#endif /* __ISAPNP__ */
 247
 248        if (dev) SET_MODULE_OWNER(dev);
 249
 250        /* First check all slots of the EISA bus.  The next slot address to
 251           probe is kept in 'eisa_addr' to support multiple probe() calls. */
 252        if (EISA_bus) {
 253                static int eisa_addr = 0x1000;
 254                while (eisa_addr < 0x9000) {
 255                        int device_id;
 256
 257                        ioaddr = eisa_addr;
 258                        eisa_addr += 0x1000;
 259
 260                        /* Check the standard EISA ID register for an encoded '3Com'. */
 261                        if (inw(ioaddr + 0xC80) != 0x6d50)
 262                                continue;
 263
 264                        /* Avoid conflict with 3c590, 3c592, 3c597, etc */
 265                        device_id = (inb(ioaddr + 0xC82)<<8) + inb(ioaddr + 0xC83);
 266                        if ((device_id & 0xFF00) == 0x5900) {
 267                                continue;
 268                        }
 269
 270                        /* Change the register set to the configuration window 0. */
 271                        outw(SelectWindow | 0, ioaddr + 0xC80 + EL3_CMD);
 272
 273                        irq = inw(ioaddr + WN0_IRQ) >> 12;
 274                        if_port = inw(ioaddr + 6)>>14;
 275                        for (i = 0; i < 3; i++)
 276                                phys_addr[i] = htons(read_eeprom(ioaddr, i));
 277
 278                        /* Restore the "Product ID" to the EEPROM read register. */
 279                        read_eeprom(ioaddr, 3);
 280
 281                        /* Was the EISA code an add-on hack?  Nahhhhh... */
 282                        goto found;
 283                }
 284        }
 285
 286#ifdef CONFIG_MCA
 287        /* Based on Erik Nygren's (nygren@mit.edu) 3c529 patch, heavily
 288         * modified by Chris Beauregard (cpbeaure@csclub.uwaterloo.ca)
 289         * to support standard MCA probing.
 290         *
 291         * redone for multi-card detection by ZP Gu (zpg@castle.net)
 292         * now works as a module
 293         */
 294
 295        if( MCA_bus ) {
 296                int slot, j;
 297                u_char pos4, pos5;
 298
 299                for( j = 0; el3_mca_adapters[j].name != NULL; j ++ ) {
 300                        slot = 0;
 301                        while( slot != MCA_NOTFOUND ) {
 302                                slot = mca_find_unused_adapter(
 303                                        el3_mca_adapters[j].id, slot );
 304                                if( slot == MCA_NOTFOUND ) break;
 305
 306                                /* if we get this far, an adapter has been
 307                                 * detected and is enabled
 308                                 */
 309
 310                                pos4 = mca_read_stored_pos( slot, 4 );
 311                                pos5 = mca_read_stored_pos( slot, 5 );
 312
 313                                ioaddr = ((short)((pos4&0xfc)|0x02)) << 8;
 314                                irq = pos5 & 0x0f;
 315
 316                                /* probing for a card at a particular IO/IRQ */
 317                                if(dev && ((dev->irq >= 1 && dev->irq != irq) ||
 318                                (dev->base_addr >= 1 && dev->base_addr != ioaddr))) {
 319                                        slot++;         /* probing next slot */
 320                                        continue;
 321                                }
 322
 323                                printk("3c509: found %s at slot %d\n",
 324                                        el3_mca_adapters[j].name, slot + 1 );
 325
 326                                /* claim the slot */
 327                                mca_set_adapter_name(slot, el3_mca_adapters[j].name);
 328                                mca_set_adapter_procfn(slot, NULL, NULL);
 329                                mca_mark_as_used(slot);
 330
 331                                if_port = pos4 & 0x03;
 332                                if (el3_debug > 2) {
 333                                        printk("3c529: irq %d  ioaddr 0x%x  ifport %d\n", irq, ioaddr, if_port);
 334                                }
 335                                EL3WINDOW(0);
 336                                for (i = 0; i < 3; i++) {
 337                                        phys_addr[i] = htons(read_eeprom(ioaddr, i));
 338                                }
 339                                
 340                                mca_slot = slot;
 341
 342                                goto found;
 343                        }
 344                }
 345                /* if we get here, we didn't find an MCA adapter */
 346                return -ENODEV;
 347        }
 348#endif /* CONFIG_MCA */
 349
 350#ifdef __ISAPNP__
 351        if (nopnp == 1)
 352                goto no_pnp;
 353
 354        for (i=0; el3_isapnp_adapters[i].vendor != 0; i++) {
 355                struct pci_dev *idev = NULL;
 356                int j;
 357                while ((idev = isapnp_find_dev(NULL,
 358                                                el3_isapnp_adapters[i].vendor,
 359                                                el3_isapnp_adapters[i].function,
 360                                                idev))) {
 361                        idev->prepare(idev);
 362                        /* Deactivation is needed if the driver was called
 363                           with "nopnp=1" before, does not harm if not. */
 364                        idev->deactivate(idev);
 365                        idev->activate(idev);
 366                        if (!idev->resource[0].start || check_region(idev->resource[0].start, EL3_IO_EXTENT))
 367                                continue;
 368                        ioaddr = idev->resource[0].start;
 369                        if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509 PnP"))
 370                                return -EBUSY;
 371                        irq = idev->irq_resource[0].start;
 372                        if (el3_debug > 3)
 373                                printk ("ISAPnP reports %s at i/o 0x%x, irq %d\n",
 374                                        (char*) el3_isapnp_adapters[i].driver_data, ioaddr, irq);
 375                        EL3WINDOW(0);
 376                        for (j = 0; j < 3; j++)
 377                                el3_isapnp_phys_addr[pnp_cards][j] =
 378                                        phys_addr[j] =
 379                                                htons(read_eeprom(ioaddr, j));
 380                        if_port = read_eeprom(ioaddr, 8) >> 14;
 381                        pnp_cards++;
 382                        goto found;
 383                }
 384        }
 385no_pnp:
 386#endif /* __ISAPNP__ */
 387
 388        /* Select an open I/O location at 0x1*0 to do contention select. */
 389        for ( ; id_port < 0x200; id_port += 0x10) {
 390                if (check_region(id_port, 1))
 391                        continue;
 392                outb(0x00, id_port);
 393                outb(0xff, id_port);
 394                if (inb(id_port) & 0x01)
 395                        break;
 396        }
 397        if (id_port >= 0x200) {
 398                /* Rare -- do we really need a warning? */
 399                printk(" WARNING: No I/O port available for 3c509 activation.\n");
 400                return -ENODEV;
 401        }
 402        /* Next check for all ISA bus boards by sending the ID sequence to the
 403           ID_PORT.  We find cards past the first by setting the 'current_tag'
 404           on cards as they are found.  Cards with their tag set will not
 405           respond to subsequent ID sequences. */
 406
 407        outb(0x00, id_port);
 408        outb(0x00, id_port);
 409        for(i = 0; i < 255; i++) {
 410                outb(lrs_state, id_port);
 411                lrs_state <<= 1;
 412                lrs_state = lrs_state & 0x100 ? lrs_state ^ 0xcf : lrs_state;
 413        }
 414
 415        /* For the first probe, clear all board's tag registers. */
 416        if (current_tag == 0)
 417                outb(0xd0, id_port);
 418        else                            /* Otherwise kill off already-found boards. */
 419                outb(0xd8, id_port);
 420
 421        if (id_read_eeprom(7) != 0x6d50) {
 422                return -ENODEV;
 423        }
 424
 425        /* Read in EEPROM data, which does contention-select.
 426           Only the lowest address board will stay "on-line".
 427           3Com got the byte order backwards. */
 428        for (i = 0; i < 3; i++) {
 429                phys_addr[i] = htons(id_read_eeprom(i));
 430        }
 431
 432#ifdef __ISAPNP__
 433        if (nopnp == 0) {
 434                /* The ISA PnP 3c509 cards respond to the ID sequence.
 435                   This check is needed in order not to register them twice. */
 436                for (i = 0; i < pnp_cards; i++) {
 437                        if (phys_addr[0] == el3_isapnp_phys_addr[i][0] &&
 438                            phys_addr[1] == el3_isapnp_phys_addr[i][1] &&
 439                            phys_addr[2] == el3_isapnp_phys_addr[i][2])
 440                        {
 441                                if (el3_debug > 3)
 442                                        printk("3c509 with address %02x %02x %02x %02x %02x %02x was found by ISAPnP\n",
 443                                                phys_addr[0] & 0xff, phys_addr[0] >> 8,
 444                                                phys_addr[1] & 0xff, phys_addr[1] >> 8,
 445                                                phys_addr[2] & 0xff, phys_addr[2] >> 8);
 446                                /* Set the adaptor tag so that the next card can be found. */
 447                                outb(0xd0 + ++current_tag, id_port);
 448                                goto no_pnp;
 449                        }
 450                }
 451        }
 452#endif /* __ISAPNP__ */
 453
 454        {
 455                unsigned int iobase = id_read_eeprom(8);
 456                if_port = iobase >> 14;
 457                ioaddr = 0x200 + ((iobase & 0x1f) << 4);
 458        }
 459        irq = id_read_eeprom(9) >> 12;
 460
 461        if (dev) {                                      /* Set passed-in IRQ or I/O Addr. */
 462                if (dev->irq > 1  &&  dev->irq < 16)
 463                        irq = dev->irq;
 464
 465                if (dev->base_addr) {
 466                        if (dev->mem_end == 0x3c509                     /* Magic key */
 467                                && dev->base_addr >= 0x200  &&  dev->base_addr <= 0x3e0)
 468                                ioaddr = dev->base_addr & 0x3f0;
 469                        else if (dev->base_addr != ioaddr)
 470                                return -ENODEV;
 471                }
 472        }
 473
 474        if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509"))
 475                return -EBUSY;
 476
 477        /* Set the adaptor tag so that the next card can be found. */
 478        outb(0xd0 + ++current_tag, id_port);
 479
 480        /* Activate the adaptor at the EEPROM location. */
 481        outb((ioaddr >> 4) | 0xe0, id_port);
 482
 483        EL3WINDOW(0);
 484        if (inw(ioaddr) != 0x6d50) {
 485                release_region(ioaddr, EL3_IO_EXTENT);
 486                return -ENODEV;
 487        }
 488
 489        /* Free the interrupt so that some other card can use it. */
 490        outw(0x0f00, ioaddr + WN0_IRQ);
 491 found:
 492        if (dev == NULL) {
 493                dev = init_etherdev(dev, sizeof(struct el3_private));
 494                if (dev == NULL) {
 495                        release_region(ioaddr, EL3_IO_EXTENT);
 496                        return -ENOMEM;
 497                }
 498                SET_MODULE_OWNER(dev);
 499        }
 500        memcpy(dev->dev_addr, phys_addr, sizeof(phys_addr));
 501        dev->base_addr = ioaddr;
 502        dev->irq = irq;
 503        
 504        if (dev->mem_start & 0x05) { /* xcvr codes 1/3/4/12 */
 505                dev->if_port = (dev->mem_start & 0x0f);
 506        } else { /* xcvr codes 0/8 */
 507                /* use eeprom value, but save user's full-duplex selection */
 508                dev->if_port = (if_port | (dev->mem_start & 0x08) );
 509        }
 510
 511        {
 512                const char *if_names[] = {"10baseT", "AUI", "undefined", "BNC"};
 513                printk("%s: 3c5x9 at %#3.3lx, %s port, address ",
 514                        dev->name, dev->base_addr, if_names[(dev->if_port & 0x03)]);
 515        }
 516
 517        /* Read in the station address. */
 518        for (i = 0; i < 6; i++)
 519                printk(" %2.2x", dev->dev_addr[i]);
 520        printk(", IRQ %d.\n", dev->irq);
 521
 522        /* Make up a EL3-specific-data structure. */
 523        if (dev->priv == NULL)
 524                dev->priv = kmalloc(sizeof(struct el3_private), GFP_KERNEL);
 525        if (dev->priv == NULL)
 526                return -ENOMEM;
 527        memset(dev->priv, 0, sizeof(struct el3_private));
 528        
 529        lp = dev->priv;
 530        lp->mca_slot = mca_slot;
 531        lp->next_dev = el3_root_dev;
 532        spin_lock_init(&lp->lock);
 533        el3_root_dev = dev;
 534
 535        if (el3_debug > 0)
 536                printk(KERN_INFO "%s" KERN_INFO "%s", versionA, versionB);
 537
 538        /* The EL3-specific entries in the device structure. */
 539        dev->open = &el3_open;
 540        dev->hard_start_xmit = &el3_start_xmit;
 541        dev->stop = &el3_close;
 542        dev->get_stats = &el3_get_stats;
 543        dev->set_multicast_list = &set_multicast_list;
 544        dev->tx_timeout = el3_tx_timeout;
 545        dev->watchdog_timeo = TX_TIMEOUT;
 546        dev->do_ioctl = netdev_ioctl;
 547
 548#ifdef CONFIG_PM
 549        /* register power management */
 550        lp->pmdev = pm_register(PM_ISA_DEV, card_idx, el3_pm_callback);
 551        if (lp->pmdev) {
 552                struct pm_dev *p;
 553                p = lp->pmdev;
 554                p->data = (struct net_device *)dev;
 555        }
 556#endif
 557
 558        /* Fill in the generic fields of the device structure. */
 559        ether_setup(dev);
 560        return 0;
 561}
 562
 563/* Read a word from the EEPROM using the regular EEPROM access register.
 564   Assume that we are in register window zero.
 565 */
 566static ushort read_eeprom(int ioaddr, int index)
 567{
 568        outw(EEPROM_READ + index, ioaddr + 10);
 569        /* Pause for at least 162 us. for the read to take place. */
 570        udelay (500);
 571        return inw(ioaddr + 12);
 572}
 573
 574/* Read a word from the EEPROM when in the ISA ID probe state. */
 575static ushort __init id_read_eeprom(int index)
 576{
 577        int bit, word = 0;
 578
 579        /* Issue read command, and pause for at least 162 us. for it to complete.
 580           Assume extra-fast 16Mhz bus. */
 581        outb(EEPROM_READ + index, id_port);
 582
 583        /* Pause for at least 162 us. for the read to take place. */
 584        udelay (500);
 585        
 586        for (bit = 15; bit >= 0; bit--)
 587                word = (word << 1) + (inb(id_port) & 0x01);
 588
 589        if (el3_debug > 3)
 590                printk("  3c509 EEPROM word %d %#4.4x.\n", index, word);
 591
 592        return word;
 593}
 594
 595
 596static int
 597el3_open(struct net_device *dev)
 598{
 599        int ioaddr = dev->base_addr;
 600        int i;
 601
 602        outw(TxReset, ioaddr + EL3_CMD);
 603        outw(RxReset, ioaddr + EL3_CMD);
 604        outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
 605
 606        i = request_irq(dev->irq, &el3_interrupt, 0, dev->name, dev);
 607        if (i) return i;
 608
 609        EL3WINDOW(0);
 610        if (el3_debug > 3)
 611                printk("%s: Opening, IRQ %d      status@%x %4.4x.\n", dev->name,
 612                           dev->irq, ioaddr + EL3_STATUS, inw(ioaddr + EL3_STATUS));
 613
 614        el3_up(dev);
 615
 616        if (el3_debug > 3)
 617                printk("%s: Opened 3c509  IRQ %d  status %4.4x.\n",
 618                           dev->name, dev->irq, inw(ioaddr + EL3_STATUS));
 619
 620        return 0;
 621}
 622
 623static void
 624el3_tx_timeout (struct net_device *dev)
 625{
 626        struct el3_private *lp = (struct el3_private *)dev->priv;
 627        int ioaddr = dev->base_addr;
 628
 629        /* Transmitter timeout, serious problems. */
 630        printk("%s: transmit timed out, Tx_status %2.2x status %4.4x "
 631                   "Tx FIFO room %d.\n",
 632                   dev->name, inb(ioaddr + TX_STATUS), inw(ioaddr + EL3_STATUS),
 633                   inw(ioaddr + TX_FREE));
 634        lp->stats.tx_errors++;
 635        dev->trans_start = jiffies;
 636        /* Issue TX_RESET and TX_START commands. */
 637        outw(TxReset, ioaddr + EL3_CMD);
 638        outw(TxEnable, ioaddr + EL3_CMD);
 639        netif_wake_queue(dev);
 640}
 641
 642
 643static int
 644el3_start_xmit(struct sk_buff *skb, struct net_device *dev)
 645{
 646        struct el3_private *lp = (struct el3_private *)dev->priv;
 647        int ioaddr = dev->base_addr;
 648        unsigned long flags;
 649
 650        netif_stop_queue (dev);
 651
 652        lp->stats.tx_bytes += skb->len;
 653        
 654        if (el3_debug > 4) {
 655                printk("%s: el3_start_xmit(length = %u) called, status %4.4x.\n",
 656                           dev->name, skb->len, inw(ioaddr + EL3_STATUS));
 657        }
 658#if 0
 659#ifndef final_version
 660        {       /* Error-checking code, delete someday. */
 661                ushort status = inw(ioaddr + EL3_STATUS);
 662                if (status & 0x0001             /* IRQ line active, missed one. */
 663                        && inw(ioaddr + EL3_STATUS) & 1) {                      /* Make sure. */
 664                        printk("%s: Missed interrupt, status then %04x now %04x"
 665                                   "  Tx %2.2x Rx %4.4x.\n", dev->name, status,
 666                                   inw(ioaddr + EL3_STATUS), inb(ioaddr + TX_STATUS),
 667                                   inw(ioaddr + RX_STATUS));
 668                        /* Fake interrupt trigger by masking, acknowledge interrupts. */
 669                        outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
 670                        outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
 671                                 ioaddr + EL3_CMD);
 672                        outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
 673                }
 674        }
 675#endif
 676#endif
 677        /*
 678         *      We lock the driver against other processors. Note
 679         *      we don't need to lock versus the IRQ as we suspended
 680         *      that. This means that we lose the ability to take
 681         *      an RX during a TX upload. That sucks a bit with SMP
 682         *      on an original 3c509 (2K buffer)
 683         *
 684         *      Using disable_irq stops us crapping on other
 685         *      time sensitive devices.
 686         */
 687
 688        spin_lock_irqsave(&lp->lock, flags);
 689            
 690        /* Put out the doubleword header... */
 691        outw(skb->len, ioaddr + TX_FIFO);
 692        outw(0x00, ioaddr + TX_FIFO);
 693        /* ... and the packet rounded to a doubleword. */
 694#ifdef  __powerpc__
 695        outsl_ns(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
 696#else
 697        outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
 698#endif
 699
 700        dev->trans_start = jiffies;
 701        if (inw(ioaddr + TX_FREE) > 1536)
 702                netif_start_queue(dev);
 703        else
 704                /* Interrupt us when the FIFO has room for max-sized packet. */
 705                outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
 706
 707        spin_unlock_irqrestore(&lp->lock, flags);
 708
 709        dev_kfree_skb (skb);
 710
 711        /* Clear the Tx status stack. */
 712        {
 713                short tx_status;
 714                int i = 4;
 715
 716                while (--i > 0  &&      (tx_status = inb(ioaddr + TX_STATUS)) > 0) {
 717                        if (tx_status & 0x38) lp->stats.tx_aborted_errors++;
 718                        if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD);
 719                        if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD);
 720                        outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
 721                }
 722        }
 723        return 0;
 724}
 725
 726/* The EL3 interrupt handler. */
 727static void
 728el3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
 729{
 730        struct net_device *dev = (struct net_device *)dev_id;
 731        struct el3_private *lp;
 732        int ioaddr, status;
 733        int i = max_interrupt_work;
 734
 735        if (dev == NULL) {
 736                printk ("el3_interrupt(): irq %d for unknown device.\n", irq);
 737                return;
 738        }
 739
 740        lp = (struct el3_private *)dev->priv;
 741        spin_lock(&lp->lock);
 742
 743        ioaddr = dev->base_addr;
 744
 745        if (el3_debug > 4) {
 746                status = inw(ioaddr + EL3_STATUS);
 747                printk("%s: interrupt, status %4.4x.\n", dev->name, status);
 748        }
 749
 750        while ((status = inw(ioaddr + EL3_STATUS)) &
 751                   (IntLatch | RxComplete | StatsFull)) {
 752
 753                if (status & RxComplete)
 754                        el3_rx(dev);
 755
 756                if (status & TxAvailable) {
 757                        if (el3_debug > 5)
 758                                printk("        TX room bit was handled.\n");
 759                        /* There's room in the FIFO for a full-sized packet. */
 760                        outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
 761                        netif_wake_queue (dev);
 762                }
 763                if (status & (AdapterFailure | RxEarly | StatsFull | TxComplete)) {
 764                        /* Handle all uncommon interrupts. */
 765                        if (status & StatsFull)                         /* Empty statistics. */
 766                                update_stats(dev);
 767                        if (status & RxEarly) {                         /* Rx early is unused. */
 768                                el3_rx(dev);
 769                                outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
 770                        }
 771                        if (status & TxComplete) {                      /* Really Tx error. */
 772                                struct el3_private *lp = (struct el3_private *)dev->priv;
 773                                short tx_status;
 774                                int i = 4;
 775
 776                                while (--i>0 && (tx_status = inb(ioaddr + TX_STATUS)) > 0) {
 777                                        if (tx_status & 0x38) lp->stats.tx_aborted_errors++;
 778                                        if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD);
 779                                        if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD);
 780                                        outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
 781                                }
 782                        }
 783                        if (status & AdapterFailure) {
 784                                /* Adapter failure requires Rx reset and reinit. */
 785                                outw(RxReset, ioaddr + EL3_CMD);
 786                                /* Set the Rx filter to the current state. */
 787                                outw(SetRxFilter | RxStation | RxBroadcast
 788                                         | (dev->flags & IFF_ALLMULTI ? RxMulticast : 0)
 789                                         | (dev->flags & IFF_PROMISC ? RxProm : 0),
 790                                         ioaddr + EL3_CMD);
 791                                outw(RxEnable, ioaddr + EL3_CMD); /* Re-enable the receiver. */
 792                                outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
 793                        }
 794                }
 795
 796                if (--i < 0) {
 797                        printk("%s: Infinite loop in interrupt, status %4.4x.\n",
 798                                   dev->name, status);
 799                        /* Clear all interrupts. */
 800                        outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
 801                        break;
 802                }
 803                /* Acknowledge the IRQ. */
 804                outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD); /* Ack IRQ */
 805        }
 806
 807        if (el3_debug > 4) {
 808                printk("%s: exiting interrupt, status %4.4x.\n", dev->name,
 809                           inw(ioaddr + EL3_STATUS));
 810        }
 811        spin_unlock(&lp->lock);
 812        return;
 813}
 814
 815
 816static struct net_device_stats *
 817el3_get_stats(struct net_device *dev)
 818{
 819        struct el3_private *lp = (struct el3_private *)dev->priv;
 820        unsigned long flags;
 821
 822        /*
 823         *      This is fast enough not to bother with disable IRQ
 824         *      stuff.
 825         */
 826         
 827        spin_lock_irqsave(&lp->lock, flags);
 828        update_stats(dev);
 829        spin_unlock_irqrestore(&lp->lock, flags);
 830        return &lp->stats;
 831}
 832
 833/*  Update statistics.  We change to register window 6, so this should be run
 834        single-threaded if the device is active. This is expected to be a rare
 835        operation, and it's simpler for the rest of the driver to assume that
 836        window 1 is always valid rather than use a special window-state variable.
 837        */
 838static void update_stats(struct net_device *dev)
 839{
 840        struct el3_private *lp = (struct el3_private *)dev->priv;
 841        int ioaddr = dev->base_addr;
 842
 843        if (el3_debug > 5)
 844                printk("   Updating the statistics.\n");
 845        /* Turn off statistics updates while reading. */
 846        outw(StatsDisable, ioaddr + EL3_CMD);
 847        /* Switch to the stats window, and read everything. */
 848        EL3WINDOW(6);
 849        lp->stats.tx_carrier_errors     += inb(ioaddr + 0);
 850        lp->stats.tx_heartbeat_errors   += inb(ioaddr + 1);
 851        /* Multiple collisions. */         inb(ioaddr + 2);
 852        lp->stats.collisions            += inb(ioaddr + 3);
 853        lp->stats.tx_window_errors      += inb(ioaddr + 4);
 854        lp->stats.rx_fifo_errors        += inb(ioaddr + 5);
 855        lp->stats.tx_packets            += inb(ioaddr + 6);
 856        /* Rx packets   */                 inb(ioaddr + 7);
 857        /* Tx deferrals */                 inb(ioaddr + 8);
 858        inw(ioaddr + 10);       /* Total Rx and Tx octets. */
 859        inw(ioaddr + 12);
 860
 861        /* Back to window 1, and turn statistics back on. */
 862        EL3WINDOW(1);
 863        outw(StatsEnable, ioaddr + EL3_CMD);
 864        return;
 865}
 866
 867static int
 868el3_rx(struct net_device *dev)
 869{
 870        struct el3_private *lp = (struct el3_private *)dev->priv;
 871        int ioaddr = dev->base_addr;
 872        short rx_status;
 873
 874        if (el3_debug > 5)
 875                printk("   In rx_packet(), status %4.4x, rx_status %4.4x.\n",
 876                           inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
 877        while ((rx_status = inw(ioaddr + RX_STATUS)) > 0) {
 878                if (rx_status & 0x4000) { /* Error, update stats. */
 879                        short error = rx_status & 0x3800;
 880
 881                        outw(RxDiscard, ioaddr + EL3_CMD);
 882                        lp->stats.rx_errors++;
 883                        switch (error) {
 884                        case 0x0000:            lp->stats.rx_over_errors++; break;
 885                        case 0x0800:            lp->stats.rx_length_errors++; break;
 886                        case 0x1000:            lp->stats.rx_frame_errors++; break;
 887                        case 0x1800:            lp->stats.rx_length_errors++; break;
 888                        case 0x2000:            lp->stats.rx_frame_errors++; break;
 889                        case 0x2800:            lp->stats.rx_crc_errors++; break;
 890                        }
 891                } else {
 892                        short pkt_len = rx_status & 0x7ff;
 893                        struct sk_buff *skb;
 894
 895                        skb = dev_alloc_skb(pkt_len+5);
 896                        lp->stats.rx_bytes += pkt_len;
 897                        if (el3_debug > 4)
 898                                printk("Receiving packet size %d status %4.4x.\n",
 899                                           pkt_len, rx_status);
 900                        if (skb != NULL) {
 901                                skb->dev = dev;
 902                                skb_reserve(skb, 2);     /* Align IP on 16 byte */
 903
 904                                /* 'skb->data' points to the start of sk_buff data area. */
 905#ifdef  __powerpc__
 906                                insl_ns(ioaddr+RX_FIFO, skb_put(skb,pkt_len),
 907                                                           (pkt_len + 3) >> 2);
 908#else
 909                                insl(ioaddr + RX_FIFO, skb_put(skb,pkt_len),
 910                                         (pkt_len + 3) >> 2);
 911#endif
 912
 913                                outw(RxDiscard, ioaddr + EL3_CMD); /* Pop top Rx packet. */
 914                                skb->protocol = eth_type_trans(skb,dev);
 915                                netif_rx(skb);
 916                                dev->last_rx = jiffies;
 917                                lp->stats.rx_packets++;
 918                                continue;
 919                        }
 920                        outw(RxDiscard, ioaddr + EL3_CMD);
 921                        lp->stats.rx_dropped++;
 922                        if (el3_debug)
 923                                printk("%s: Couldn't allocate a sk_buff of size %d.\n",
 924                                           dev->name, pkt_len);
 925                }
 926                inw(ioaddr + EL3_STATUS);                               /* Delay. */
 927                while (inw(ioaddr + EL3_STATUS) & 0x1000)
 928                        printk(KERN_DEBUG "     Waiting for 3c509 to discard packet, status %x.\n",
 929                                   inw(ioaddr + EL3_STATUS) );
 930        }
 931
 932        return 0;
 933}
 934
 935/*
 936 *     Set or clear the multicast filter for this adaptor.
 937 */
 938static void
 939set_multicast_list(struct net_device *dev)
 940{
 941        unsigned long flags;
 942        struct el3_private *lp = (struct el3_private *)dev->priv;
 943        int ioaddr = dev->base_addr;
 944
 945        if (el3_debug > 1) {
 946                static int old;
 947                if (old != dev->mc_count) {
 948                        old = dev->mc_count;
 949                        printk("%s: Setting Rx mode to %d addresses.\n", dev->name, dev->mc_count);
 950                }
 951        }
 952        spin_lock_irqsave(&lp->lock, flags);
 953        if (dev->flags&IFF_PROMISC) {
 954                outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast | RxProm,
 955                         ioaddr + EL3_CMD);
 956        }
 957        else if (dev->mc_count || (dev->flags&IFF_ALLMULTI)) {
 958                outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast, ioaddr + EL3_CMD);
 959        }
 960        else
 961                outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
 962        spin_unlock_irqrestore(&lp->lock, flags);
 963}
 964
 965static int
 966el3_close(struct net_device *dev)
 967{
 968        int ioaddr = dev->base_addr;
 969
 970        if (el3_debug > 2)
 971                printk("%s: Shutting down ethercard.\n", dev->name);
 972
 973        el3_down(dev);
 974
 975        free_irq(dev->irq, dev);
 976        /* Switching back to window 0 disables the IRQ. */
 977        EL3WINDOW(0);
 978        /* But we explicitly zero the IRQ line select anyway. */
 979        outw(0x0f00, ioaddr + WN0_IRQ);
 980
 981        return 0;
 982}
 983
 984/**
 985 * netdev_ethtool_ioctl: Handle network interface SIOCETHTOOL ioctls
 986 * @dev: network interface on which out-of-band action is to be performed
 987 * @useraddr: userspace address to which data is to be read and returned
 988 *
 989 * Process the various commands of the SIOCETHTOOL interface.
 990 */
 991
 992static int netdev_ethtool_ioctl (struct net_device *dev, void *useraddr)
 993{
 994        u32 ethcmd;
 995
 996        /* dev_ioctl() in ../../net/core/dev.c has already checked
 997           capable(CAP_NET_ADMIN), so don't bother with that here.  */
 998
 999        if (get_user(ethcmd, (u32 *)useraddr))
1000                return -EFAULT;
1001
1002        switch (ethcmd) {
1003
1004        case ETHTOOL_GDRVINFO: {
1005                struct ethtool_drvinfo info = { ETHTOOL_GDRVINFO };
1006                strcpy (info.driver, DRV_NAME);
1007                strcpy (info.version, DRV_VERSION);
1008                if (copy_to_user (useraddr, &info, sizeof (info)))
1009                        return -EFAULT;
1010                return 0;
1011        }
1012
1013        /* get message-level */
1014        case ETHTOOL_GMSGLVL: {
1015                struct ethtool_value edata = {ETHTOOL_GMSGLVL};
1016                edata.data = el3_debug;
1017                if (copy_to_user(useraddr, &edata, sizeof(edata)))
1018                        return -EFAULT;
1019                return 0;
1020        }
1021        /* set message-level */
1022        case ETHTOOL_SMSGLVL: {
1023                struct ethtool_value edata;
1024                if (copy_from_user(&edata, useraddr, sizeof(edata)))
1025                        return -EFAULT;
1026                el3_debug = edata.data;
1027                return 0;
1028        }
1029
1030        default:
1031                break;
1032        }
1033
1034        return -EOPNOTSUPP;
1035}
1036
1037/**
1038 * netdev_ioctl: Handle network interface ioctls
1039 * @dev: network interface on which out-of-band action is to be performed
1040 * @rq: user request data
1041 * @cmd: command issued by user
1042 *
1043 * Process the various out-of-band ioctls passed to this driver.
1044 */
1045
1046static int netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
1047{
1048        int rc = 0;
1049
1050        switch (cmd) {
1051        case SIOCETHTOOL:
1052                rc = netdev_ethtool_ioctl(dev, (void *) rq->ifr_data);
1053                break;
1054
1055        default:
1056                rc = -EOPNOTSUPP;
1057                break;
1058        }
1059
1060        return rc;
1061}
1062
1063static void el3_down(struct net_device *dev)
1064{
1065        int ioaddr = dev->base_addr;
1066
1067        netif_stop_queue(dev);
1068
1069        /* Turn off statistics ASAP.  We update lp->stats below. */
1070        outw(StatsDisable, ioaddr + EL3_CMD);
1071
1072        /* Disable the receiver and transmitter. */
1073        outw(RxDisable, ioaddr + EL3_CMD);
1074        outw(TxDisable, ioaddr + EL3_CMD);
1075
1076        if (dev->if_port == 3)
1077                /* Turn off thinnet power.  Green! */
1078                outw(StopCoax, ioaddr + EL3_CMD);
1079        else if (dev->if_port == 0) {
1080                /* Disable link beat and jabber, if_port may change ere next open(). */
1081                EL3WINDOW(4);
1082                outw(inw(ioaddr + WN4_MEDIA) & ~MEDIA_TP, ioaddr + WN4_MEDIA);
1083        }
1084
1085        outw(SetIntrEnb | 0x0000, ioaddr + EL3_CMD);
1086
1087        update_stats(dev);
1088}
1089
1090static void el3_up(struct net_device *dev)
1091{
1092        int i, sw_info, net_diag;
1093        int ioaddr = dev->base_addr;
1094        
1095        /* Activating the board required and does no harm otherwise */
1096        outw(0x0001, ioaddr + 4);
1097
1098        /* Set the IRQ line. */
1099        outw((dev->irq << 12) | 0x0f00, ioaddr + WN0_IRQ);
1100
1101        /* Set the station address in window 2 each time opened. */
1102        EL3WINDOW(2);
1103
1104        for (i = 0; i < 6; i++)
1105                outb(dev->dev_addr[i], ioaddr + i);
1106
1107        if ((dev->if_port & 0x03) == 3) /* BNC interface */
1108                /* Start the thinnet transceiver. We should really wait 50ms...*/
1109                outw(StartCoax, ioaddr + EL3_CMD);
1110        else if ((dev->if_port & 0x03) == 0) { /* 10baseT interface */
1111                /* Combine secondary sw_info word (the adapter level) and primary
1112                        sw_info word (duplex setting plus other useless bits) */
1113                EL3WINDOW(0);
1114                sw_info = (read_eeprom(ioaddr, 0x14) & 0x400f) | 
1115                        (read_eeprom(ioaddr, 0x0d) & 0xBff0);
1116
1117                EL3WINDOW(4);
1118                net_diag = inw(ioaddr + WN4_NETDIAG);
1119                net_diag = (net_diag | FD_ENABLE); /* temporarily assume full-duplex will be set */
1120                printk("%s: ", dev->name);
1121                switch (dev->if_port & 0x0c) {
1122                        case 12:
1123                                /* force full-duplex mode if 3c5x9b */
1124                                if (sw_info & 0x000f) {
1125                                        printk("Forcing 3c5x9b full-duplex mode");
1126                                        break;
1127                                }
1128                        case 8:
1129                                /* set full-duplex mode based on eeprom config setting */
1130                                if ((sw_info & 0x000f) && (sw_info & 0x8000)) {
1131                                        printk("Setting 3c5x9b full-duplex mode (from EEPROM configuration bit)");
1132                                        break;
1133                                }
1134                        default:
1135                                /* xcvr=(0 || 4) OR user has an old 3c5x9 non "B" model */
1136                                printk("Setting 3c5x9/3c5x9B half-duplex mode");
1137                                net_diag = (net_diag & ~FD_ENABLE); /* disable full duplex */
1138                }
1139
1140                outw(net_diag, ioaddr + WN4_NETDIAG);
1141                printk(" if_port: %d, sw_info: %4.4x\n", dev->if_port, sw_info);
1142                if (el3_debug > 3)
1143                        printk("%s: 3c5x9 net diag word is now: %4.4x.\n", dev->name, net_diag);
1144                /* Enable link beat and jabber check. */
1145                outw(inw(ioaddr + WN4_MEDIA) | MEDIA_TP, ioaddr + WN4_MEDIA);
1146        }
1147
1148        /* Switch to the stats window, and clear all stats by reading. */
1149        outw(StatsDisable, ioaddr + EL3_CMD);
1150        EL3WINDOW(6);
1151        for (i = 0; i < 9; i++)
1152                inb(ioaddr + i);
1153        inw(ioaddr + 10);
1154        inw(ioaddr + 12);
1155
1156        /* Switch to register set 1 for normal use. */
1157        EL3WINDOW(1);
1158
1159        /* Accept b-case and phys addr only. */
1160        outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
1161        outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
1162
1163        outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
1164        outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
1165        /* Allow status bits to be seen. */
1166        outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
1167        /* Ack all pending events, and set active indicator mask. */
1168        outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
1169                 ioaddr + EL3_CMD);
1170        outw(SetIntrEnb | IntLatch|TxAvailable|TxComplete|RxComplete|StatsFull,
1171                 ioaddr + EL3_CMD);
1172
1173        netif_start_queue(dev);
1174}
1175
1176/* Power Management support functions */
1177#ifdef CONFIG_PM
1178
1179static int el3_suspend(struct pm_dev *pdev)
1180{
1181        unsigned long flags;
1182        struct net_device *dev;
1183        struct el3_private *lp;
1184        int ioaddr;
1185        
1186        if (!pdev && !pdev->data)
1187                return -EINVAL;
1188
1189        dev = (struct net_device *)pdev->data;
1190        lp = (struct el3_private *)dev->priv;
1191        ioaddr = dev->base_addr;
1192
1193        spin_lock_irqsave(&lp->lock, flags);
1194
1195        if (netif_running(dev))
1196                netif_device_detach(dev);
1197
1198        el3_down(dev);
1199        outw(PowerDown, ioaddr + EL3_CMD);
1200
1201        spin_unlock_irqrestore(&lp->lock, flags);
1202        return 0;
1203}
1204
1205static int el3_resume(struct pm_dev *pdev)
1206{
1207        unsigned long flags;
1208        struct net_device *dev;
1209        struct el3_private *lp;
1210        int ioaddr;
1211        
1212        if (!pdev && !pdev->data)
1213                return -EINVAL;
1214
1215        dev = (struct net_device *)pdev->data;
1216        lp = (struct el3_private *)dev->priv;
1217        ioaddr = dev->base_addr;
1218
1219        spin_lock_irqsave(&lp->lock, flags);
1220
1221        outw(PowerUp, ioaddr + EL3_CMD);
1222        el3_up(dev);
1223
1224        if (netif_running(dev))
1225                netif_device_attach(dev);
1226                
1227        spin_unlock_irqrestore(&lp->lock, flags);
1228        return 0;
1229}
1230
1231static int el3_pm_callback(struct pm_dev *pdev, pm_request_t rqst, void *data)
1232{
1233        switch (rqst) {
1234                case PM_SUSPEND:
1235                        return el3_suspend(pdev);
1236
1237                case PM_RESUME:
1238                        return el3_resume(pdev);
1239        }
1240        return 0;
1241}
1242
1243#endif /* CONFIG_PM */
1244
1245#ifdef MODULE
1246/* Parameters that may be passed into the module. */
1247static int debug = -1;
1248static int irq[] = {-1, -1, -1, -1, -1, -1, -1, -1};
1249static int xcvr[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
1250
1251MODULE_PARM(debug,"i");
1252MODULE_PARM(irq,"1-8i");
1253MODULE_PARM(xcvr,"1-12i");
1254MODULE_PARM(max_interrupt_work, "i");
1255MODULE_PARM_DESC(debug, "debug level (0-6)");
1256MODULE_PARM_DESC(irq, "IRQ number(s) (assigned)");
1257MODULE_PARM_DESC(xcvr,"tranceiver(s) (0=internal, 1=external)");
1258MODULE_PARM_DESC(max_interrupt_work, "maximum events handled per interrupt");
1259#ifdef __ISAPNP__
1260MODULE_PARM(nopnp, "i");
1261MODULE_PARM_DESC(nopnp, "disable ISA PnP support (0-1)");
1262MODULE_DEVICE_TABLE(isapnp, el3_isapnp_adapters);
1263#endif  /* __ISAPNP__ */
1264MODULE_DESCRIPTION("3Com Etherlink III (3c509, 3c509B) ISA/PnP ethernet driver");
1265MODULE_LICENSE("GPL");
1266
1267int
1268init_module(void)
1269{
1270        int el3_cards = 0;
1271
1272        if (debug >= 0)
1273                el3_debug = debug;
1274
1275        el3_root_dev = NULL;
1276        while (el3_probe(0, el3_cards) == 0) {
1277                if (irq[el3_cards] > 1)
1278                        el3_root_dev->irq = irq[el3_cards];
1279                if (xcvr[el3_cards] >= 0)
1280                        el3_root_dev->if_port = xcvr[el3_cards];
1281                el3_cards++;
1282        }
1283
1284        return el3_cards ? 0 : -ENODEV;
1285}
1286
1287void
1288cleanup_module(void)
1289{
1290        struct net_device *next_dev;
1291
1292        /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
1293        while (el3_root_dev) {
1294                struct el3_private *lp = (struct el3_private *)el3_root_dev->priv;
1295#ifdef CONFIG_MCA               
1296                if(lp->mca_slot!=-1)
1297                        mca_mark_as_unused(lp->mca_slot);
1298#endif
1299
1300#ifdef CONFIG_PM
1301                if (lp->pmdev)
1302                        pm_unregister(lp->pmdev);
1303#endif
1304                next_dev = lp->next_dev;
1305                unregister_netdev(el3_root_dev);
1306                release_region(el3_root_dev->base_addr, EL3_IO_EXTENT);
1307                kfree(el3_root_dev);
1308                el3_root_dev = next_dev;
1309        }
1310}
1311#endif /* MODULE */
1312
1313/*
1314 * Local variables:
1315 *  compile-command: "gcc -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -c 3c509.c"
1316 *  version-control: t
1317 *  kept-new-versions: 5
1318 *  tab-width: 4
1319 * End:
1320 */
1321
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.