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