⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 3c501.c

📁 LINUX 1.0 内核c源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* 3c501.c: A 3Com 3c501 ethernet driver for linux. */
/*
    Copyright (C) 1992,1993  Donald Becker

    Copyright 1993 United States Government as represented by the
    Director, National Security Agency.  This software may be used and
    distributed according to the terms of the GNU Public License,
    incorporated herein by reference.

    This is a device driver for the 3Com Etherlink 3c501.
    Do not purchase this card, even as a joke.  It's performance is horrible,
    and it breaks in many ways.  

    The Author may be reached as becker@super.org or
    C/O Supercomputing Research Ctr., 17100 Science Dr., Bowie MD 20715
    I'll only accept bug fixes, not reports, for the 3c501 driver.
*/

static char *version =
    "3c501.c: 3/3/94 Donald Becker (becker@super.org).\n";

/*
  Braindamage remaining:
  The 3c501 board.
  */

#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/ptrace.h>
#include <linux/fcntl.h>
#include <linux/ioport.h>
#include <linux/interrupt.h>
#include <linux/malloc.h>
#include <linux/ioport.h>
#include <asm/bitops.h>
#include <asm/io.h>
#include <errno.h>

#include "dev.h"
#include "eth.h"
#include "skbuff.h"
#include "arp.h"

#ifndef HAVE_AUTOIRQ
/* From auto_irq.c, should be in a *.h file. */
extern void autoirq_setup(int waittime);
extern int autoirq_report(int waittime);
extern struct device *irq2dev_map[16];
#endif

#ifndef HAVE_ALLOC_SKB
#define alloc_skb(size, priority) (struct sk_buff *) kmalloc(size,priority)
#define kfree_skbmem(addr, size) kfree_s(addr,size);
#endif


/* Index to functions. */
int el1_probe(struct device *dev);
static int  el_open(struct device *dev);
static int  el_start_xmit(struct sk_buff *skb, struct device *dev);
static void el_interrupt(int reg_ptr);
static void el_receive(struct device *dev);
static void el_reset(struct device *dev);
static int  el1_close(struct device *dev);
static struct enet_statistics *el1_get_stats(struct device *dev);
#ifdef HAVE_MULTICAST
static void set_multicast_list(struct device *dev, int num_addrs, void *addrs);
#endif

#define EL_NAME "EtherLink 3c501"

#ifndef EL_DEBUG
#define EL_DEBUG  2	/* use 0 for production, 1 for devel., >2 for debug */
#endif			/* Anything above 5 is wordy death! */
static int el_debug = EL_DEBUG;
static int el_base;
static struct device *eldev;	/* Only for consistency checking.  */
 
/* We could easily have this struct kmalloc()ed per-board, but
   who would want more than one 3c501?. */
static struct {
    struct enet_statistics stats;
    int tx_pkt_start;		/* The length of the current Tx packet. */
    int collisions;		/* Tx collisions this packet */
} el_status;			/* This should be stored per-board */


#define RX_STATUS (el_base + 0x06)
#define RX_CMD	  RX_STATUS
#define TX_STATUS (el_base + 0x07)
#define TX_CMD	  TX_STATUS
#define GP_LOW 	  (el_base + 0x08)
#define GP_HIGH   (el_base + 0x09)
#define RX_BUF_CLR (el_base + 0x0A)
#define RX_LOW	  (el_base + 0x0A)
#define RX_HIGH   (el_base + 0x0B)
#define SAPROM	  (el_base + 0x0C)
#define AX_STATUS (el_base + 0x0E)
#define AX_CMD	  AX_STATUS
#define DATAPORT  (el_base + 0x0F)
#define TX_RDY 0x08		/* In TX_STATUS */

#define EL1_DATAPTR	0x08
#define EL1_RXPTR	0x0A
#define EL1_SAPROM	0x0C
#define EL1_DATAPORT 	0x0f

/* Writes to the ax command register. */
#define AX_OFF	0x00			/* Irq off, buffer access on */
#define AX_SYS  0x40			/* Load the buffer */
#define AX_XMIT 0x44			/* Transmit a packet */
#define AX_RX	0x48			/* Receive a packet */
#define AX_LOOP	0x0C			/* Loopback mode */
#define AX_RESET 0x80

/* Normal receive mode written to RX_STATUS.  We must intr on short packets
   to avoid bogus rx lockups. */
#define RX_NORM 0xA8		/* 0x68 == all addrs, 0xA8 only to me. */
#define RX_PROM 0x68		/* Senior Prom, uhmm promiscuous mode. */
#define RX_MULT 0xE8		/* Accept multicast packets. */
#define TX_NORM 0x0A	/* Interrupt on everything that might hang the chip */

/* TX_STATUS register. */
#define TX_COLLISION 0x02
#define TX_16COLLISIONS 0x04
#define TX_READY 0x08

#define RX_RUNT 0x08
#define RX_MISSED 0x01		/* Missed a packet due to 3c501 braindamage. */
#define RX_GOOD	0x30		/* Good packet 0x20, or simple overflow 0x10. */


int
el1_probe(struct device *dev)
{
    int i;
    int ioaddr;
    unsigned char station_addr[6];
    int autoirq = 0;

    eldev = dev;		/* Store for debugging. */
    el_base = dev->base_addr;

    if (el_base < 0x40)		/* Invalid?  Probe for it. */
	el_base = 0x280;

    ioaddr = el_base;

    /* Read the station address PROM data from the special port.  */
    for (i = 0; i < 6; i++) {
	outw(i, ioaddr + EL1_DATAPTR);
	station_addr[i] = inb(ioaddr + EL1_SAPROM);
    }
    /* Check the first three octets of the S.A. for 3Com's code. */ 
    if (station_addr[0] != 0x02  ||  station_addr[1] != 0x60
	|| station_addr[2] != 0x8c) {
	return ENODEV;
    }

#ifdef HAVE_PORTRESERVE
    /* Grab the region so we can find the another board if autoIRQ fails. */
    snarf_region(ioaddr, 16);
#endif

    /* We auto-IRQ by shutting off the interrupt line and letting it float
       high. */
    if (dev->irq < 2) {

	autoirq_setup(2);

	inb(RX_STATUS);		/* Clear pending interrupts. */
	inb(TX_STATUS);
	outb(AX_LOOP + 1, AX_CMD);

	outb(0x00, AX_CMD);

	autoirq = autoirq_report(1);

	if (autoirq == 0) {
	    printk("%s: 3c501 probe failed to detect IRQ line.\n", dev->name);
	    return EAGAIN;
	}
	dev->irq = autoirq;
    }

    outb(AX_RESET+AX_LOOP, AX_CMD);			/* Loopback mode. */

    dev->base_addr = el_base;
    memcpy(dev->dev_addr, station_addr, ETH_ALEN);
    if (dev->mem_start & 0xf)
	el_debug = dev->mem_start & 0x7;

    printk("%s: 3c501 EtherLink at %#x, using %sIRQ %d, melting ethernet.\n",
	   dev->name, dev->base_addr, autoirq ? "auto":"assigned ", dev->irq);

    if (el_debug)
	printk("%s", version);

    /* The EL1-specific entries in the device structure. */
    dev->open = &el_open;
    dev->hard_start_xmit = &el_start_xmit;
    dev->stop = &el1_close;
    dev->get_stats = &el1_get_stats;
#ifdef HAVE_MULTICAST
    dev->set_multicast_list = &set_multicast_list;
#endif

    /* Fill in the generic field of the device structure. */
    for (i = 0; i < DEV_NUMBUFFS; i++)
	dev->buffs[i] = NULL;

    dev->hard_header	= eth_header;
    dev->add_arp	= eth_add_arp;
    dev->queue_xmit	= dev_queue_xmit;
    dev->rebuild_header	= eth_rebuild_header;
    dev->type_trans	= eth_type_trans;

    dev->type		= ARPHRD_ETHER;
    dev->hard_header_len = ETH_HLEN;
    dev->mtu		= 1500; /* eth_mtu */
    dev->addr_len	= ETH_ALEN;
    for (i = 0; i < ETH_ALEN; i++) {
	dev->broadcast[i]=0xff;
    }

    /* New-style flags. */
    dev->flags		= IFF_BROADCAST;
    dev->family		= AF_INET;
    dev->pa_addr	= 0;
    dev->pa_brdaddr	= 0;
    dev->pa_mask	= 0;
    dev->pa_alen	= sizeof(unsigned long);

    return 0;
}

/* Open/initialize the board. */
static int
el_open(struct device *dev)
{

  if (el_debug > 2)
      printk("%s: Doing el_open()...", dev->name);

  if (request_irq(dev->irq, &el_interrupt)) {
      if (el_debug > 2)
	  printk("interrupt busy, exiting el_open().\n");
      return -EAGAIN;
  }
  irq2dev_map[dev->irq] = dev;

  el_reset(dev);

  dev->start = 1;

  outb(AX_RX, AX_CMD);	/* Aux control, irq and receive enabled */
  if (el_debug > 2)
     printk("finished el_open().\n");
  return (0);
}

static int
el_start_xmit(struct sk_buff *skb, struct device *dev)
{

    if (dev->tbusy) {
	if (jiffies - dev->trans_start < 20) {
	    if (el_debug > 2)
		printk(" transmitter busy, deferred.\n");
	    return 1;
	}
	if (el_debug)
	    printk ("%s: transmit timed out, txsr %#2x axsr=%02x rxsr=%02x.\n",
		    dev->name, inb(TX_STATUS), inb(AX_STATUS), inb(RX_STATUS));
	el_status.stats.tx_errors++;
#ifdef oldway
	el_reset(dev);
#else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -