linux-bk/drivers/net/atp.c
<<
>>
Prefs
   1/* atp.c: Attached (pocket) ethernet adapter driver for linux. */
   2/*
   3        This is a driver for commonly OEM pocket (parallel port)
   4        ethernet adapters based on the Realtek RTL8002 and RTL8012 chips.
   5
   6        Written 1993-2000 by Donald Becker.
   7
   8        This software may be used and distributed according to the terms of
   9        the GNU General Public License (GPL), incorporated herein by reference.
  10        Drivers based on or derived from this code fall under the GPL and must
  11        retain the authorship, copyright and license notice.  This file is not
  12        a complete program and may only be used when the entire operating
  13        system is licensed under the GPL.
  14
  15        Copyright 1993 United States Government as represented by the Director,
  16        National Security Agency.  Copyright 1994-2000 retained by the original
  17        author, Donald Becker. The timer-based reset code was supplied in 1995
  18        by Bill Carlson, wwc@super.org.
  19
  20        The author may be reached as becker@scyld.com, or C/O
  21        Scyld Computing Corporation
  22        410 Severn Ave., Suite 210
  23        Annapolis MD 21403
  24
  25        Support information and updates available at
  26        http://www.scyld.com/network/atp.html
  27
  28
  29        Modular support/softnet added by Alan Cox.
  30
  31*/
  32
  33static const char versionA[] =
  34"atp.c:v1.09 8/9/2000 Donald Becker <becker@scyld.com>\n";
  35static const char versionB[] =
  36"  http://www.scyld.com/network/atp.html\n";
  37
  38/* The user-configurable values.
  39   These may be modified when a driver module is loaded.*/
  40
  41static int debug = 1;                   /* 1 normal messages, 0 quiet .. 7 verbose. */
  42#define net_debug debug
  43
  44/* Maximum events (Rx packets, etc.) to handle at each interrupt. */
  45static int max_interrupt_work = 15;
  46
  47#define NUM_UNITS 2
  48/* The standard set of ISA module parameters. */
  49static int io[NUM_UNITS];
  50static int irq[NUM_UNITS];
  51static int xcvr[NUM_UNITS];                     /* The data transfer mode. */
  52
  53/* Operational parameters that are set at compile time. */
  54
  55/* Time in jiffies before concluding the transmitter is hung. */
  56#define TX_TIMEOUT  (400*HZ/1000)
  57
  58/*
  59        This file is a device driver for the RealTek (aka AT-Lan-Tec) pocket
  60        ethernet adapter.  This is a common low-cost OEM pocket ethernet
  61        adapter, sold under many names.
  62
  63  Sources:
  64        This driver was written from the packet driver assembly code provided by
  65        Vincent Bono of AT-Lan-Tec.      Ever try to figure out how a complicated
  66        device works just from the assembly code?  It ain't pretty.  The following
  67        description is written based on guesses and writing lots of special-purpose
  68        code to test my theorized operation.
  69
  70        In 1997 Realtek made available the documentation for the second generation
  71        RTL8012 chip, which has lead to several driver improvements.
  72          http://www.realtek.com.tw/cn/cn.html
  73
  74                                        Theory of Operation
  75
  76        The RTL8002 adapter seems to be built around a custom spin of the SEEQ
  77        controller core.  It probably has a 16K or 64K internal packet buffer, of
  78        which the first 4K is devoted to transmit and the rest to receive.
  79        The controller maintains the queue of received packet and the packet buffer
  80        access pointer internally, with only 'reset to beginning' and 'skip to next
  81        packet' commands visible.  The transmit packet queue holds two (or more?)
  82        packets: both 'retransmit this packet' (due to collision) and 'transmit next
  83        packet' commands must be started by hand.
  84
  85        The station address is stored in a standard bit-serial EEPROM which must be
  86        read (ughh) by the device driver.  (Provisions have been made for
  87        substituting a 74S288 PROM, but I haven't gotten reports of any models
  88        using it.)  Unlike built-in devices, a pocket adapter can temporarily lose
  89        power without indication to the device driver.  The major effect is that
  90        the station address, receive filter (promiscuous, etc.) and transceiver
  91        must be reset.
  92
  93        The controller itself has 16 registers, some of which use only the lower
  94        bits.  The registers are read and written 4 bits at a time.  The four bit
  95        register address is presented on the data lines along with a few additional
  96        timing and control bits.  The data is then read from status port or written
  97        to the data port.
  98
  99        Correction: the controller has two banks of 16 registers.  The second
 100        bank contains only the multicast filter table (now used) and the EEPROM
 101        access registers.
 102
 103        Since the bulk data transfer of the actual packets through the slow
 104        parallel port dominates the driver's running time, four distinct data
 105        (non-register) transfer modes are provided by the adapter, two in each
 106        direction.  In the first mode timing for the nibble transfers is
 107        provided through the data port.  In the second mode the same timing is
 108        provided through the control port.  In either case the data is read from
 109        the status port and written to the data port, just as it is accessing
 110        registers.
 111
 112        In addition to the basic data transfer methods, several more are modes are
 113        created by adding some delay by doing multiple reads of the data to allow
 114        it to stabilize.  This delay seems to be needed on most machines.
 115
 116        The data transfer mode is stored in the 'dev->if_port' field.  Its default
 117        value is '4'.  It may be overridden at boot-time using the third parameter
 118        to the "ether=..." initialization.
 119
 120        The header file <atp.h> provides inline functions that encapsulate the
 121        register and data access methods.  These functions are hand-tuned to
 122        generate reasonable object code.  This header file also documents my
 123        interpretations of the device registers.
 124*/
 125
 126#include <linux/kernel.h>
 127#include <linux/module.h>
 128#include <linux/sched.h>
 129#include <linux/types.h>
 130#include <linux/fcntl.h>
 131#include <linux/interrupt.h>
 132#include <linux/ioport.h>
 133#include <linux/in.h>
 134#include <linux/slab.h>
 135#include <linux/string.h>
 136#include <asm/system.h>
 137#include <asm/bitops.h>
 138#include <asm/io.h>
 139#include <asm/dma.h>
 140#include <linux/errno.h>
 141#include <linux/init.h>
 142#include <linux/crc32.h>
 143
 144#include <linux/netdevice.h>
 145#include <linux/etherdevice.h>
 146#include <linux/skbuff.h>
 147#include <linux/spinlock.h>
 148#include <linux/delay.h>
 149
 150#include "atp.h"
 151
 152MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
 153MODULE_DESCRIPTION("RealTek RTL8002/8012 parallel port Ethernet driver");
 154MODULE_LICENSE("GPL");
 155
 156MODULE_PARM(max_interrupt_work, "i");
 157MODULE_PARM(debug, "i");
 158MODULE_PARM(io, "1-" __MODULE_STRING(NUM_UNITS) "i");
 159MODULE_PARM(irq, "1-" __MODULE_STRING(NUM_UNITS) "i");
 160MODULE_PARM(xcvr, "1-" __MODULE_STRING(NUM_UNITS) "i");
 161MODULE_PARM_DESC(max_interrupt_work, "ATP maximum events handled per interrupt");
 162MODULE_PARM_DESC(debug, "ATP debug level (0-7)");
 163MODULE_PARM_DESC(io, "ATP I/O base address(es)");
 164MODULE_PARM_DESC(irq, "ATP IRQ number(s)");
 165MODULE_PARM_DESC(xcvr, "ATP tranceiver(s) (0=internal, 1=external)");
 166
 167#define RUN_AT(x) (jiffies + (x))
 168
 169/* The number of low I/O ports used by the ethercard. */
 170#define ETHERCARD_TOTAL_SIZE    3
 171
 172/* Sequence to switch an 8012 from printer mux to ethernet mode. */
 173static char mux_8012[] = { 0xff, 0xf7, 0xff, 0xfb, 0xf3, 0xfb, 0xff, 0xf7,};
 174
 175struct net_local {
 176    spinlock_t lock;
 177    struct net_device *next_module;
 178    struct net_device_stats stats;
 179    struct timer_list timer;    /* Media selection timer. */
 180    long last_rx_time;          /* Last Rx, in jiffies, to handle Rx hang. */
 181    int saved_tx_size;
 182    unsigned int tx_unit_busy:1;
 183    unsigned char re_tx,        /* Number of packet retransmissions. */
 184                addr_mode,              /* Current Rx filter e.g. promiscuous, etc. */
 185                pac_cnt_in_tx_buf,
 186                chip_type;
 187};
 188
 189/* This code, written by wwc@super.org, resets the adapter every
 190   TIMED_CHECKER ticks.  This recovers from an unknown error which
 191   hangs the device. */
 192#define TIMED_CHECKER (HZ/4)
 193#ifdef TIMED_CHECKER
 194#include <linux/timer.h>
 195static void atp_timed_checker(unsigned long ignored);
 196#endif
 197
 198/* Index to functions, as function prototypes. */
 199
 200static int atp_probe1(struct net_device *dev, long ioaddr);
 201static void get_node_ID(struct net_device *dev);
 202static unsigned short eeprom_op(long ioaddr, unsigned int cmd);
 203static int net_open(struct net_device *dev);
 204static void hardware_init(struct net_device *dev);
 205static void write_packet(long ioaddr, int length, unsigned char *packet, int mode);
 206static void trigger_send(long ioaddr, int length);
 207static int      atp_send_packet(struct sk_buff *skb, struct net_device *dev);
 208static void atp_interrupt(int irq, void *dev_id, struct pt_regs *regs);
 209static void net_rx(struct net_device *dev);
 210static void read_block(long ioaddr, int length, unsigned char *buffer, int data_mode);
 211static int net_close(struct net_device *dev);
 212static struct net_device_stats *net_get_stats(struct net_device *dev);
 213static void set_rx_mode_8002(struct net_device *dev);
 214static void set_rx_mode_8012(struct net_device *dev);
 215static void tx_timeout(struct net_device *dev);
 216
 217
 218/* A list of all installed ATP devices, for removing the driver module. */
 219static struct net_device *root_atp_dev;
 220
 221/* Check for a network adapter of this type, and return '0' iff one exists.
 222   If dev->base_addr == 0, probe all likely locations.
 223   If dev->base_addr == 1, always return failure.
 224   If dev->base_addr == 2, allocate space for the device and return success
 225   (detachable devices only).
 226   */
 227static int __init atp_init(struct net_device *dev)
 228{
 229        int *port, ports[] = {0x378, 0x278, 0x3bc, 0};
 230        int base_addr = dev ? dev->base_addr : io[0];
 231
 232        if (base_addr > 0x1ff)          /* Check a single specified location. */
 233                return atp_probe1(dev, base_addr);
 234        else if (base_addr == 1)        /* Don't probe at all. */
 235                return -ENXIO;
 236
 237        for (port = ports; *port; port++) {
 238                long ioaddr = *port;
 239                outb(0x57, ioaddr + PAR_DATA);
 240                if (inb(ioaddr + PAR_DATA) != 0x57)
 241                        continue;
 242                if (atp_probe1(dev, ioaddr) == 0)
 243                        return 0;
 244        }
 245
 246        return -ENODEV;
 247}
 248
 249static int __init atp_probe1(struct net_device *dev, long ioaddr)
 250{
 251        struct net_local *lp;
 252        int saved_ctrl_reg, status, i;
 253
 254        outb(0xff, ioaddr + PAR_DATA);
 255        /* Save the original value of the Control register, in case we guessed
 256           wrong. */
 257        saved_ctrl_reg = inb(ioaddr + PAR_CONTROL);
 258        if (net_debug > 3)
 259                printk("atp: Control register was %#2.2x.\n", saved_ctrl_reg);
 260        /* IRQEN=0, SLCTB=high INITB=high, AUTOFDB=high, STBB=high. */
 261        outb(0x04, ioaddr + PAR_CONTROL);
 262#ifndef final_version
 263        if (net_debug > 3) {
 264                /* Turn off the printer multiplexer on the 8012. */
 265                for (i = 0; i < 8; i++)
 266                        outb(mux_8012[i], ioaddr + PAR_DATA);
 267                write_reg(ioaddr, MODSEL, 0x00);
 268                printk("atp: Registers are ");
 269                for (i = 0; i < 32; i++)
 270                        printk(" %2.2x", read_nibble(ioaddr, i));
 271                printk(".\n");
 272        }
 273#endif
 274        /* Turn off the printer multiplexer on the 8012. */
 275        for (i = 0; i < 8; i++)
 276                outb(mux_8012[i], ioaddr + PAR_DATA);
 277        write_reg_high(ioaddr, CMR1, CMR1h_RESET);
 278        /* udelay() here? */
 279        status = read_nibble(ioaddr, CMR1);
 280
 281        if (net_debug > 3) {
 282                printk(KERN_DEBUG "atp: Status nibble was %#2.2x..", status);
 283                for (i = 0; i < 32; i++)
 284                        printk(" %2.2x", read_nibble(ioaddr, i));
 285                printk("\n");
 286        }
 287
 288        if ((status & 0x78) != 0x08) {
 289                /* The pocket adapter probe failed, restore the control register. */
 290                outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
 291                return -ENODEV;
 292        }
 293        status = read_nibble(ioaddr, CMR2_h);
 294        if ((status & 0x78) != 0x10) {
 295                outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
 296                return -ENODEV;
 297        }
 298
 299        dev = init_etherdev(dev, sizeof(struct net_local));
 300        if (!dev)
 301                return -ENOMEM;
 302        SET_MODULE_OWNER(dev);
 303
 304        /* Find the IRQ used by triggering an interrupt. */
 305        write_reg_byte(ioaddr, CMR2, 0x01);                     /* No accept mode, IRQ out. */
 306        write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);  /* Enable Tx and Rx. */
 307
 308        /* Omit autoIRQ routine for now. Use "table lookup" instead.  Uhgggh. */
 309        if (irq[0])
 310                dev->irq = irq[0];
 311        else if (ioaddr == 0x378)
 312                dev->irq = 7;
 313        else
 314                dev->irq = 5;
 315        write_reg_high(ioaddr, CMR1, CMR1h_TxRxOFF); /* Disable Tx and Rx units. */
 316        write_reg(ioaddr, CMR2, CMR2_NULL);
 317
 318        dev->base_addr = ioaddr;
 319
 320        /* Read the station address PROM.  */
 321        get_node_ID(dev);
 322
 323#ifndef MODULE
 324        if (net_debug)
 325                printk(KERN_INFO "%s" KERN_INFO "%s", versionA, versionB);
 326#endif
 327
 328        printk(KERN_NOTICE "%s: Pocket adapter found at %#3lx, IRQ %d, SAPROM "
 329                   "%02X:%02X:%02X:%02X:%02X:%02X.\n", dev->name, dev->base_addr,
 330                   dev->irq, dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
 331                   dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
 332
 333        /* Reset the ethernet hardware and activate the printer pass-through. */
 334    write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX);
 335
 336        /* Initialize the device structure. */
 337        ether_setup(dev);
 338        if (dev->priv == NULL)
 339                dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
 340        if (dev->priv == NULL)
 341                return -ENOMEM;
 342        memset(dev->priv, 0, sizeof(struct net_local));
 343
 344        lp = (struct net_local *)dev->priv;
 345        lp->chip_type = RTL8002;
 346        lp->addr_mode = CMR2h_Normal;
 347        spin_lock_init(&lp->lock);
 348
 349        lp->next_module = root_atp_dev;
 350        root_atp_dev = dev;
 351
 352        /* For the ATP adapter the "if_port" is really the data transfer mode. */
 353        if (xcvr[0])
 354                dev->if_port = xcvr[0];
 355        else
 356                dev->if_port = (dev->mem_start & 0xf) ? (dev->mem_start & 0x7) : 4;
 357        if (dev->mem_end & 0xf)
 358                net_debug = dev->mem_end & 7;
 359
 360        dev->open               = net_open;
 361        dev->stop               = net_close;
 362        dev->hard_start_xmit    = atp_send_packet;
 363        dev->get_stats          = net_get_stats;
 364        dev->set_multicast_list =
 365          lp->chip_type == RTL8002 ? &set_rx_mode_8002 : &set_rx_mode_8012;
 366        dev->tx_timeout         = tx_timeout;
 367        dev->watchdog_timeo     = TX_TIMEOUT;
 368
 369        return 0;
 370}
 371
 372/* Read the station address PROM, usually a word-wide EEPROM. */
 373static void __init get_node_ID(struct net_device *dev)
 374{
 375        long ioaddr = dev->base_addr;
 376        int sa_offset = 0;
 377        int i;
 378
 379        write_reg(ioaddr, CMR2, CMR2_EEPROM);     /* Point to the EEPROM control registers. */
 380
 381        /* Some adapters have the station address at offset 15 instead of offset
 382           zero.  Check for it, and fix it if needed. */
 383        if (eeprom_op(ioaddr, EE_READ(0)) == 0xffff)
 384                sa_offset = 15;
 385
 386        for (i = 0; i < 3; i++)
 387                ((u16 *)dev->dev_addr)[i] =
 388                        be16_to_cpu(eeprom_op(ioaddr, EE_READ(sa_offset + i)));
 389
 390        write_reg(ioaddr, CMR2, CMR2_NULL);
 391}
 392
 393/*
 394  An EEPROM read command starts by shifting out 0x60+address, and then
 395  shifting in the serial data. See the NatSemi databook for details.
 396 *                 ________________
 397 * CS : __|
 398 *                         ___     ___
 399 * CLK: ______|   |___|   |
 400 *               __ _______ _______
 401 * DI :  __X_______X_______X
 402 * DO :  _________X_______X
 403 */
 404
 405static unsigned short __init eeprom_op(long ioaddr, unsigned int cmd)
 406{
 407        unsigned eedata_out = 0;
 408        int num_bits = EE_CMD_SIZE;
 409
 410        while (--num_bits >= 0) {
 411                char outval = test_bit(num_bits, &cmd) ? EE_DATA_WRITE : 0;
 412                write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_LOW);
 413                write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_HIGH);
 414                eedata_out <<= 1;
 415                if (read_nibble(ioaddr, PROM_DATA) & EE_DATA_READ)
 416                        eedata_out++;
 417        }
 418        write_reg_high(ioaddr, PROM_CMD, EE_CLK_LOW & ~EE_CS);
 419        return eedata_out;
 420}
 421
 422
 423/* Open/initialize the board.  This is called (in the current kernel)
 424   sometime after booting when the 'ifconfig' program is run.
 425
 426   This routine sets everything up anew at each open, even
 427   registers that "should" only need to be set once at boot, so that
 428   there is non-reboot way to recover if something goes wrong.
 429
 430   This is an attachable device: if there is no dev->priv entry then it wasn't
 431   probed for at boot-time, and we need to probe for it again.
 432   */
 433static int net_open(struct net_device *dev)
 434{
 435        struct net_local *lp = (struct net_local *)dev->priv;
 436        int ret;
 437
 438        /* The interrupt line is turned off (tri-stated) when the device isn't in
 439           use.  That's especially important for "attached" interfaces where the
 440           port or interrupt may be shared. */
 441        ret = request_irq(dev->irq, &atp_interrupt, 0, dev->name, dev);
 442        if (ret)
 443                return ret;
 444
 445        hardware_init(dev);
 446
 447        init_timer(&lp->timer);
 448        lp->timer.expires = RUN_AT(TIMED_CHECKER);
 449        lp->timer.data = (unsigned long)dev;
 450        lp->timer.function = &atp_timed_checker;    /* timer handler */
 451        add_timer(&lp->timer);
 452
 453        netif_start_queue(dev);
 454        return 0;
 455}
 456
 457/* This routine resets the hardware.  We initialize everything, assuming that
 458   the hardware may have been temporarily detached. */
 459static void hardware_init(struct net_device *dev)
 460{
 461        struct net_local *lp = (struct net_local *)dev->priv;
 462        long ioaddr = dev->base_addr;
 463    int i;
 464
 465        /* Turn off the printer multiplexer on the 8012. */
 466        for (i = 0; i < 8; i++)
 467                outb(mux_8012[i], ioaddr + PAR_DATA);
 468        write_reg_high(ioaddr, CMR1, CMR1h_RESET);
 469
 470    for (i = 0; i < 6; i++)
 471                write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
 472
 473        write_reg_high(ioaddr, CMR2, lp->addr_mode);
 474
 475        if (net_debug > 2) {
 476                printk(KERN_DEBUG "%s: Reset: current Rx mode %d.\n", dev->name,
 477                           (read_nibble(ioaddr, CMR2_h) >> 3) & 0x0f);
 478        }
 479
 480    write_reg(ioaddr, CMR2, CMR2_IRQOUT);
 481    write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
 482
 483        /* Enable the interrupt line from the serial port. */
 484        outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
 485
 486        /* Unmask the interesting interrupts. */
 487    write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
 488    write_reg_high(ioaddr, IMR, ISRh_RxErr);
 489
 490        lp->tx_unit_busy = 0;
 491    lp->pac_cnt_in_tx_buf = 0;
 492        lp->saved_tx_size = 0;
 493}
 494
 495static void trigger_send(long ioaddr, int length)
 496{
 497        write_reg_byte(ioaddr, TxCNT0, length & 0xff);
 498        write_reg(ioaddr, TxCNT1, length >> 8);
 499        write_reg(ioaddr, CMR1, CMR1_Xmit);
 500}
 501
 502static void write_packet(long ioaddr, int length, unsigned char *packet, int data_mode)
 503{
 504    length = (length + 1) & ~1;         /* Round up to word length. */
 505    outb(EOC+MAR, ioaddr + PAR_DATA);
 506    if ((data_mode & 1) == 0) {
 507                /* Write the packet out, starting with the write addr. */
 508                outb(WrAddr+MAR, ioaddr + PAR_DATA);
 509                do {
 510                        write_byte_mode0(ioaddr, *packet++);
 511                } while (--length > 0) ;
 512    } else {
 513                /* Write the packet out in slow mode. */
 514                unsigned char outbyte = *packet++;
 515
 516                outb(Ctrl_LNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
 517                outb(WrAddr+MAR, ioaddr + PAR_DATA);
 518
 519                outb((outbyte & 0x0f)|0x40, ioaddr + PAR_DATA);
 520                outb(outbyte & 0x0f, ioaddr + PAR_DATA);
 521                outbyte >>= 4;
 522                outb(outbyte & 0x0f, ioaddr + PAR_DATA);
 523                outb(Ctrl_HNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
 524                while (--length > 0)
 525                        write_byte_mode1(ioaddr, *packet++);
 526    }
 527    /* Terminate the Tx frame.  End of write: ECB. */
 528    outb(0xff, ioaddr + PAR_DATA);
 529    outb(Ctrl_HNibWrite | Ctrl_SelData | Ctrl_IRQEN, ioaddr + PAR_CONTROL);
 530}
 531
 532static void tx_timeout(struct net_device *dev)
 533{
 534        struct net_local *np = (struct net_local *)dev->priv;
 535        long ioaddr = dev->base_addr;
 536
 537        printk(KERN_WARNING "%s: Transmit timed out, %s?\n", dev->name,
 538                   inb(ioaddr + PAR_CONTROL) & 0x10 ? "network cable problem"
 539                   :  "IRQ conflict");
 540        np->stats.tx_errors++;
 541        /* Try to restart the adapter. */
 542        hardware_init(dev);
 543        dev->trans_start = jiffies;
 544        netif_wake_queue(dev);
 545        np->stats.tx_errors++;
 546}
 547
 548static int atp_send_packet(struct sk_buff *skb, struct net_device *dev)
 549{
 550        struct net_local *lp = (struct net_local *)dev->priv;
 551        long ioaddr = dev->base_addr;
 552        int length;
 553        long flags;
 554
 555        length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
 556
 557        netif_stop_queue(dev);
 558
 559        /* Disable interrupts by writing 0x00 to the Interrupt Mask Register.
 560           This sequence must not be interrupted by an incoming packet. */
 561
 562        spin_lock_irqsave(&lp->lock, flags);
 563        write_reg(ioaddr, IMR, 0);
 564        write_reg_high(ioaddr, IMR, 0);
 565        spin_unlock_irqrestore(&lp->lock, flags);
 566
 567        write_packet(ioaddr, length, skb->data, dev->if_port);
 568
 569        lp->pac_cnt_in_tx_buf++;
 570        if (lp->tx_unit_busy == 0) {
 571                trigger_send(ioaddr, length);
 572                lp->saved_tx_size = 0;                          /* Redundant */
 573                lp->re_tx = 0;
 574                lp->tx_unit_busy = 1;
 575        } else
 576                lp->saved_tx_size = length;
 577        /* Re-enable the LPT interrupts. */
 578        write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
 579        write_reg_high(ioaddr, IMR, ISRh_RxErr);
 580
 581        dev->trans_start = jiffies;
 582        dev_kfree_skb (skb);
 583        return 0;
 584}
 585
 586
 587/* The typical workload of the driver:
 588   Handle the network interface interrupts. */
 589static void atp_interrupt(int irq, void *dev_instance, struct pt_regs * regs)
 590{
 591        struct net_device *dev = (struct net_device *)dev_instance;
 592        struct net_local *lp;
 593        long ioaddr;
 594        static int num_tx_since_rx;
 595        int boguscount = max_interrupt_work;
 596
 597        if (dev == NULL) {
 598                printk(KERN_ERR "ATP_interrupt(): irq %d for unknown device.\n", irq);
 599                return;
 600        }
 601        ioaddr = dev->base_addr;
 602        lp = (struct net_local *)dev->priv;
 603
 604        spin_lock(&lp->lock);
 605
 606        /* Disable additional spurious interrupts. */
 607        outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
 608
 609        /* The adapter's output is currently the IRQ line, switch it to data. */
 610        write_reg(ioaddr, CMR2, CMR2_NULL);
 611        write_reg(ioaddr, IMR, 0);
 612
 613        if (net_debug > 5) printk(KERN_DEBUG "%s: In interrupt ", dev->name);
 614    while (--boguscount > 0) {
 615                int status = read_nibble(ioaddr, ISR);
 616                if (net_debug > 5) printk("loop status %02x..", status);
 617
 618                if (status & (ISR_RxOK<<3)) {
 619                        write_reg(ioaddr, ISR, ISR_RxOK); /* Clear the Rx interrupt. */
 620                        do {
 621                                int read_status = read_nibble(ioaddr, CMR1);
 622                                if (net_debug > 6)
 623                                        printk("handling Rx packet %02x..", read_status);
 624                                /* We acknowledged the normal Rx interrupt, so if the interrupt
 625                                   is still outstanding we must have a Rx error. */
 626                                if (read_status & (CMR1_IRQ << 3)) { /* Overrun. */
 627                                        lp->stats.rx_over_errors++;
 628                                        /* Set to no-accept mode long enough to remove a packet. */
 629                                        write_reg_high(ioaddr, CMR2, CMR2h_OFF);
 630                                        net_rx(dev);
 631                                        /* Clear the interrupt and return to normal Rx mode. */
 632                                        write_reg_high(ioaddr, ISR, ISRh_RxErr);
 633                                        write_reg_high(ioaddr, CMR2, lp->addr_mode);
 634                                } else if ((read_status & (CMR1_BufEnb << 3)) == 0) {
 635                                        net_rx(dev);
 636                                        num_tx_since_rx = 0;
 637                                } else
 638                                        break;
 639                        } while (--boguscount > 0);
 640                } else if (status & ((ISR_TxErr + ISR_TxOK)<<3)) {
 641                        if (net_debug > 6)  printk("handling Tx done..");
 642                        /* Clear the Tx interrupt.  We should check for too many failures
 643                           and reinitialize the adapter. */
 644                        write_reg(ioaddr, ISR, ISR_TxErr + ISR_TxOK);
 645                        if (status & (ISR_TxErr<<3)) {
 646                                lp->stats.collisions++;
 647                                if (++lp->re_tx > 15) {
 648                                        lp->stats.tx_aborted_errors++;
 649                                        hardware_init(dev);
 650                                        break;
 651                                }
 652                                /* Attempt to retransmit. */
 653                                if (net_debug > 6)  printk("attempting to ReTx");
 654                                write_reg(ioaddr, CMR1, CMR1_ReXmit + CMR1_Xmit);
 655                        } else {
 656                                /* Finish up the transmit. */
 657                                lp->stats.tx_packets++;
 658                                lp->pac_cnt_in_tx_buf--;
 659                                if ( lp->saved_tx_size) {
 660                                        trigger_send(ioaddr, lp->saved_tx_size);
 661                                        lp->saved_tx_size = 0;
 662                                        lp->re_tx = 0;
 663                                } else
 664                                        lp->tx_unit_busy = 0;
 665                                netif_wake_queue(dev);  /* Inform upper layers. */
 666                        }
 667                        num_tx_since_rx++;
 668                } else if (num_tx_since_rx > 8
 669                                   && time_after(jiffies, dev->last_rx + HZ)) {
 670                        if (net_debug > 2)
 671                                printk(KERN_DEBUG "%s: Missed packet? No Rx after %d Tx and "
 672                                           "%ld jiffies status %02x  CMR1 %02x.\n", dev->name,
 673                                           num_tx_since_rx, jiffies - dev->last_rx, status,
 674                                           (read_nibble(ioaddr, CMR1) >> 3) & 15);
 675                        lp->stats.rx_missed_errors++;
 676                        hardware_init(dev);
 677                        num_tx_since_rx = 0;
 678                        break;
 679                } else
 680                        break;
 681    }
 682
 683        /* This following code fixes a rare (and very difficult to track down)
 684           problem where the adapter forgets its ethernet address. */
 685        {
 686                int i;
 687                for (i = 0; i < 6; i++)
 688                        write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
 689#if 0 && defined(TIMED_CHECKER)
 690                mod_timer(&lp->timer, RUN_AT(TIMED_CHECKER));
 691#endif
 692        }
 693
 694        /* Tell the adapter that it can go back to using the output line as IRQ. */
 695    write_reg(ioaddr, CMR2, CMR2_IRQOUT);
 696        /* Enable the physical interrupt line, which is sure to be low until.. */
 697        outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
 698        /* .. we enable the interrupt sources. */
 699        write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
 700        write_reg_high(ioaddr, IMR, ISRh_RxErr);                        /* Hmmm, really needed? */
 701
 702        spin_unlock(&lp->lock);
 703
 704        if (net_debug > 5) printk("exiting interrupt.\n");
 705        return;
 706}
 707
 708#ifdef TIMED_CHECKER
 709/* This following code fixes a rare (and very difficult to track down)
 710   problem where the adapter forgets its ethernet address. */
 711static void atp_timed_checker(unsigned long data)
 712{
 713        struct net_device *dev = (struct net_device *)data;
 714        long ioaddr = dev->base_addr;
 715        struct net_local *lp = (struct net_local *)dev->priv;
 716        int tickssofar = jiffies - lp->last_rx_time;
 717        int i;
 718
 719        spin_lock(&lp->lock);
 720        if (tickssofar > 2*HZ) {
 721#if 1
 722                for (i = 0; i < 6; i++)
 723                        write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
 724                lp->last_rx_time = jiffies;
 725#else
 726                for (i = 0; i < 6; i++)
 727                        if (read_cmd_byte(ioaddr, PAR0 + i) != atp_timed_dev->dev_addr[i])
 728                                {
 729                        struct net_local *lp = (struct net_local *)atp_timed_dev->priv;
 730                        write_reg_byte(ioaddr, PAR0 + i, atp_timed_dev->dev_addr[i]);
 731                        if (i == 2)
 732                          lp->stats.tx_errors++;
 733                        else if (i == 3)
 734                          lp->stats.tx_dropped++;
 735                        else if (i == 4)
 736                          lp->stats.collisions++;
 737                        else
 738                          lp->stats.rx_errors++;
 739                  }
 740#endif
 741        }
 742        spin_unlock(&lp->lock);
 743        lp->timer.expires = RUN_AT(TIMED_CHECKER);
 744        add_timer(&lp->timer);
 745}
 746#endif
 747
 748/* We have a good packet(s), get it/them out of the buffers. */
 749static void net_rx(struct net_device *dev)
 750{
 751        struct net_local *lp = (struct net_local *)dev->priv;
 752        long ioaddr = dev->base_addr;
 753        struct rx_header rx_head;
 754
 755        /* Process the received packet. */
 756        outb(EOC+MAR, ioaddr + PAR_DATA);
 757        read_block(ioaddr, 8, (unsigned char*)&rx_head, dev->if_port);
 758        if (net_debug > 5)
 759                printk(KERN_DEBUG " rx_count %04x %04x %04x %04x..", rx_head.pad,
 760                           rx_head.rx_count, rx_head.rx_status, rx_head.cur_addr);
 761        if ((rx_head.rx_status & 0x77) != 0x01) {
 762                lp->stats.rx_errors++;
 763                if (rx_head.rx_status & 0x0004) lp->stats.rx_frame_errors++;
 764                else if (rx_head.rx_status & 0x0002) lp->stats.rx_crc_errors++;
 765                if (net_debug > 3)
 766                        printk(KERN_DEBUG "%s: Unknown ATP Rx error %04x.\n",
 767                                   dev->name, rx_head.rx_status);
 768                if  (rx_head.rx_status & 0x0020) {
 769                        lp->stats.rx_fifo_errors++;
 770                        write_reg_high(ioaddr, CMR1, CMR1h_TxENABLE);
 771                        write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
 772                } else if (rx_head.rx_status & 0x0050)
 773                        hardware_init(dev);
 774                return;
 775        } else {
 776                /* Malloc up new buffer. The "-4" omits the FCS (CRC). */
 777                int pkt_len = (rx_head.rx_count & 0x7ff) - 4;
 778                struct sk_buff *skb;
 779
 780                skb = dev_alloc_skb(pkt_len + 2);
 781                if (skb == NULL) {
 782                        printk(KERN_ERR "%s: Memory squeeze, dropping packet.\n",
 783                                   dev->name);
 784                        lp->stats.rx_dropped++;
 785                        goto done;
 786                }
 787                skb->dev = dev;
 788
 789                skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
 790                read_block(ioaddr, pkt_len, skb_put(skb,pkt_len), dev->if_port);
 791                skb->protocol = eth_type_trans(skb, dev);
 792                netif_rx(skb);
 793                dev->last_rx = jiffies;
 794                lp->stats.rx_packets++;
 795                lp->stats.rx_bytes += pkt_len;
 796        }
 797 done:
 798        write_reg(ioaddr, CMR1, CMR1_NextPkt);
 799        lp->last_rx_time = jiffies;
 800        return;
 801}
 802
 803static void read_block(long ioaddr, int length, unsigned char *p, int data_mode)
 804{
 805
 806        if (data_mode <= 3) { /* Mode 0 or 1 */
 807                outb(Ctrl_LNibRead, ioaddr + PAR_CONTROL);
 808                outb(length == 8  ?  RdAddr | HNib | MAR  :  RdAddr | MAR,
 809                         ioaddr + PAR_DATA);
 810                if (data_mode <= 1) { /* Mode 0 or 1 */
 811                        do  *p++ = read_byte_mode0(ioaddr);  while (--length > 0);
 812                } else  /* Mode 2 or 3 */
 813                        do  *p++ = read_byte_mode2(ioaddr);  while (--length > 0);
 814        } else if (data_mode <= 5)
 815                do      *p++ = read_byte_mode4(ioaddr);  while (--length > 0);
 816        else
 817                do      *p++ = read_byte_mode6(ioaddr);  while (--length > 0);
 818
 819    outb(EOC+HNib+MAR, ioaddr + PAR_DATA);
 820        outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
 821}
 822
 823/* The inverse routine to net_open(). */
 824static int
 825net_close(struct net_device *dev)
 826{
 827        struct net_local *lp = (struct net_local *)dev->priv;
 828        long ioaddr = dev->base_addr;
 829
 830        netif_stop_queue(dev);
 831
 832        del_timer_sync(&lp->timer);
 833
 834        /* Flush the Tx and disable Rx here. */
 835        lp->addr_mode = CMR2h_OFF;
 836        write_reg_high(ioaddr, CMR2, CMR2h_OFF);
 837
 838        /* Free the IRQ line. */
 839        outb(0x00, ioaddr + PAR_CONTROL);
 840        free_irq(dev->irq, dev);
 841
 842        /* Reset the ethernet hardware and activate the printer pass-through. */
 843        write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX);
 844        return 0;
 845}
 846
 847/* Get the current statistics.  This may be called with the card open or
 848   closed. */
 849static struct net_device_stats *
 850net_get_stats(struct net_device *dev)
 851{
 852        struct net_local *lp = (struct net_local *)dev->priv;
 853        return &lp->stats;
 854}
 855
 856/*
 857 *      Set or clear the multicast filter for this adapter.
 858 */
 859
 860static void set_rx_mode_8002(struct net_device *dev)
 861{
 862        struct net_local *lp = (struct net_local *)dev->priv;
 863        long ioaddr = dev->base_addr;
 864
 865        if ( dev->mc_count > 0 || (dev->flags & (IFF_ALLMULTI|IFF_PROMISC))) {
 866                /* We must make the kernel realise we had to move
 867                 *      into promisc mode or we start all out war on
 868                 *      the cable. - AC
 869                 */
 870                dev->flags|=IFF_PROMISC;
 871                lp->addr_mode = CMR2h_PROMISC;
 872        } else
 873                lp->addr_mode = CMR2h_Normal;
 874        write_reg_high(ioaddr, CMR2, lp->addr_mode);
 875}
 876
 877static void set_rx_mode_8012(struct net_device *dev)
 878{
 879        struct net_local *lp = (struct net_local *)dev->priv;
 880        long ioaddr = dev->base_addr;
 881        unsigned char new_mode, mc_filter[8]; /* Multicast hash filter */
 882        int i;
 883
 884        if (dev->flags & IFF_PROMISC) {                 /* Set promiscuous. */
 885                new_mode = CMR2h_PROMISC;
 886        } else if ((dev->mc_count > 1000)  ||  (dev->flags & IFF_ALLMULTI)) {
 887                /* Too many to filter perfectly -- accept all multicasts. */
 888                memset(mc_filter, 0xff, sizeof(mc_filter));
 889                new_mode = CMR2h_Normal;
 890        } else {
 891                struct dev_mc_list *mclist;
 892
 893                memset(mc_filter, 0, sizeof(mc_filter));
 894                for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
 895                         i++, mclist = mclist->next)
 896                        set_bit(ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x3f,
 897                                        mc_filter);
 898                new_mode = CMR2h_Normal;
 899        }
 900        lp->addr_mode = new_mode;
 901    write_reg(ioaddr, CMR2, CMR2_IRQOUT | 0x04); /* Switch to page 1. */
 902    for (i = 0; i < 8; i++)
 903                write_reg_byte(ioaddr, i, mc_filter[i]);
 904        if (net_debug > 2 || 1) {
 905                lp->addr_mode = 1;
 906                printk(KERN_DEBUG "%s: Mode %d, setting multicast filter to",
 907                           dev->name, lp->addr_mode);
 908                for (i = 0; i < 8; i++)
 909                        printk(" %2.2x", mc_filter[i]);
 910                printk(".\n");
 911        }
 912
 913        write_reg_high(ioaddr, CMR2, lp->addr_mode);
 914    write_reg(ioaddr, CMR2, CMR2_IRQOUT); /* Switch back to page 0 */
 915}
 916
 917static int __init atp_init_module(void) {
 918        if (debug)                                      /* Emit version even if no cards detected. */
 919                printk(KERN_INFO "%s" KERN_INFO "%s", versionA, versionB);
 920        return atp_init(NULL);
 921}
 922
 923static void __exit atp_cleanup_module(void) {
 924        struct net_device *next_dev;
 925
 926        while (root_atp_dev) {
 927                next_dev = ((struct net_local *)root_atp_dev->priv)->next_module;
 928                unregister_netdev(root_atp_dev);
 929                /* No need to release_region(), since we never snarf it. */
 930                kfree(root_atp_dev);
 931                root_atp_dev = next_dev;
 932        }
 933}
 934
 935module_init(atp_init_module);
 936module_exit(atp_cleanup_module);
 937
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.