📄 cs89x0.c
字号:
/* keep the upload from being interrupted, since we ask the chip to start transmitting before the whole packet has been completely uploaded. */ save_flags(flags); cli(); /* initiate a transmit sequence */#if REQUIRE_MEMORY writeword(dev, PP_TxCommand, lp->send_cmd); writeword(dev, PP_TxLength, skb->len);#else writeword(dev, TX_CMD_PORT, lp->send_cmd); writeword(dev, TX_LEN_PORT, skb->len);#endif /* Test to see if the chip has allocated memory for the packet */ if ((readreg(dev, PP_BusST) & READY_FOR_TX_NOW) == 0) { /* Gasp! It hasn't. But that shouldn't happen since we're waiting for TxOk, so return 1 and requeue this packet. */ /* lp->stats.tx_fifo_errors++; restore_flags(flags); return 1;*/ lp->Gskb=skb; restore_flags(flags); return 0; } /* Write the contents of the packet */#if REQUIRE_MEMORY memcpy_toio(dev->mem_start + PP_TxFrame, skb->data, (skb->len+1)&~1);#else outsw(ioaddr + TX_FRAME_PORT,skb->data,(skb->len+1) >>1);#endif restore_flags(flags); dev->trans_start = jiffies; } dev_kfree_skb (skb); return 0;}/* The typical workload of the driver: Handle the network interface interrupts. */static voidnet_interrupt(int irq, void *dev_id, struct pt_regs * regs){ struct device *dev = dev_id; struct net_local *lp; int ioaddr, status; if (dev == NULL) { printk (KERN_WARNING "net_interrupt(): irq %d for unknown device.\n", irq); return; } if (dev->interrupt) printk(KERN_WARNING "%s: Re-entering the interrupt handler.\n", dev->name); dev->interrupt = 1; ioaddr = dev->base_addr; lp = (struct net_local *)dev->priv; /* we MUST read all the events out of the ISQ, otherwise we'll never get interrupted again. As a consequence, we can't have any limit on the number of times we loop in the interrupt handler. The hardware guarantees that eventually we'll run out of events. Of course, if you're on a slow machine, and packets are arriving faster than you can read them off, you're screwed. Hasta la vista, baby! */#if REQUIRE_MEMORY while ((status = readword(dev, PP_ISQ))) {#else while ((status = readword(dev, ISQ_PORT))) {#endif if (net_debug > 4)printk(KERN_DEBUG "%s: event=%04x\n", dev->name, status); switch(status & ISQ_EVENT_MASK) { case ISQ_RECEIVER_EVENT: /* Got a packet(s). */ net_rx(dev); break; case ISQ_TRANSMITTER_EVENT: lp->stats.tx_packets++; dev->tbusy = 0; mark_bh(NET_BH); /* Inform upper layers. */ if ((status & TX_OK) == 0) lp->stats.tx_errors++; if (status & TX_LOST_CRS) lp->stats.tx_carrier_errors++; if (status & TX_SQE_ERROR) lp->stats.tx_heartbeat_errors++; if (status & TX_LATE_COL) lp->stats.tx_window_errors++; if (status & TX_16_COL) lp->stats.tx_aborted_errors++; break; case ISQ_BUFFER_EVENT: if (status & READY_FOR_TX) { /* we tried to transmit a packet earlier, but inexplicably ran out of buffers. That shouldn't happen since we only ever load one packet. Shrug. Do the right thing anyway. */ if (lp->Gskb ) { /* Write the contents of the packet */#if REQUIRE_MEMORY memcpy_toio(dev->mem_start+PP_TxFrame, lp->Gskb->data, (lp->Gskb->len+1)&~1);#else outsw(ioaddr + TX_FRAME_PORT,lp->Gskb->data,(lp->Gskb->len+1) >>1);#endif dev->trans_start = jiffies; dev_kfree_skb (lp->Gskb); lp->Gskb=NULL; } else { dev->tbusy = 0; mark_bh(NET_BH); /* Inform upper layers. */ } } if (status & TX_UNDERRUN) { lp->stats.tx_errors++; lp->stats.tx_fifo_errors++; if (net_debug > 0) printk(KERN_NOTICE "%s: transmit underrun\n", dev->name); lp->send_underrun++; if (lp->send_underrun == 3) lp->send_cmd = TX_AFTER_381; else if (lp->send_underrun == 6) lp->send_cmd = TX_AFTER_ALL; }#if ALLOW_DMA if (status & RX_DMA) { int count = readreg(dev, PP_DmaFrameCnt); while(count) { if (net_debug > 5) printk(KERN_NOTICE "%s: receiving %d frames\n", dev->name, count); if (net_debug > 2 && count >1) printk(KERN_NOTICE "%s: receiving %d frames\n", dev->name, count); dma_rx(dev); if (--count == 0) count = readreg(dev, PP_DmaFrameCnt); if (net_debug > 2 && count >0) printk(KERN_NOTICE "%s: continuing with %d frames\n", dev->name, count); } }#endif break; case ISQ_RX_MISS_EVENT: lp->stats.rx_missed_errors += (status >>6); break; case ISQ_TX_COL_EVENT: lp->stats.collisions += (status >>6); break; } } dev->interrupt = 0; return;}static voidcount_rx_errors(int status, struct net_local *lp){ lp->stats.rx_errors++; if (status & RX_RUNT) lp->stats.rx_length_errors++; if (status & RX_EXTRA_DATA) lp->stats.rx_length_errors++; if (status & RX_CRC_ERROR) if (!(status & (RX_EXTRA_DATA|RX_RUNT))) /* per str 172 */ lp->stats.rx_crc_errors++; if (status & RX_DRIBBLE) lp->stats.rx_frame_errors++; return;}/* We have a good packet(s), get it/them out of the buffers. */static voidnet_rx(struct device *dev){ struct net_local *lp = (struct net_local *)dev->priv; int ioaddr = dev->base_addr; struct sk_buff *skb; int status, length;#if REQUIRE_MEMORY status = readw(dev->mem_start + PP_RxStatus); length = readw(dev->mem_start + PP_RxLength);#else status = inw(ioaddr + RX_FRAME_PORT); length = inw(ioaddr + RX_FRAME_PORT);#endif if ((status & RX_OK) == 0) { count_rx_errors(status, lp); return; } /* Malloc up new buffer. */ skb = alloc_skb(length, GFP_ATOMIC); if (skb == NULL) { printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name); lp->stats.rx_dropped++; return; } skb->len = length; skb->dev = dev;#if REQUIRE_MEMORY memcpy_fromio(skb->data, dev->mem_start + PP_RxFrame, length);#else insw(ioaddr + RX_FRAME_PORT, skb->data, length >> 1); if (length & 1) skb->data[length-1] = inw(ioaddr + RX_FRAME_PORT);#endif if (net_debug > 3)printk(KERN_DEBUG "%s: received %d byte packet of type %x\n", dev->name, length, (skb->data[ETH_ALEN+ETH_ALEN] << 8) | skb->data[ETH_ALEN+ETH_ALEN+1]); skb->protocol=eth_type_trans(skb,dev); netif_rx(skb); lp->stats.rx_packets++; lp->stats.rx_bytes+=skb->len; return;}/* The inverse routine to net_open(). */static intnet_close(struct device *dev){ struct net_local *lp = (struct net_local *)dev->priv; writereg(dev, PP_RxCFG, 0); writereg(dev, PP_TxCFG, 0); writereg(dev, PP_BufCFG, 0); writereg(dev, PP_BusCTL, 0); dev->start = 0; free_irq(dev->irq, dev);#if ALLOW_DMA if (dev->dma) { free_dma(dev->dma); free_pages((unsigned long)(lp->dma_buff), lp->dmasize==16?4:16); }#endif /* Update the statistics here. */ MOD_DEC_USE_COUNT; return 0;}/* Get the current statistics. This may be called with the card open or closed. */static struct net_device_stats *net_get_stats(struct device *dev){ struct net_local *lp = (struct net_local *)dev->priv; cli(); /* Update the statistics from the device registers. */ lp->stats.rx_missed_errors += (readreg(dev, PP_RxMiss) >> 6); lp->stats.collisions += (readreg(dev, PP_TxCol) >> 6); sti(); return &lp->stats;}static void set_multicast_list(struct device *dev){ struct net_local *lp = (struct net_local *)dev->priv; if(dev->flags&IFF_PROMISC) { lp->rx_mode = RX_ALL_ACCEPT; } else if((dev->flags&IFF_ALLMULTI)||dev->mc_list) { /* The multicast-accept list is initialized to accept-all, and we rely on higher-level filtering for now. */ lp->rx_mode = RX_MULTCAST_ACCEPT; } else lp->rx_mode = 0; writereg(dev, PP_RxCTL, DEF_RX_ACCEPT | lp->rx_mode); /* in promiscuous mode, we accept errored packets, so we have to enable interrupts on them also */ writereg(dev, PP_RxCFG, lp->curr_rx_cfg | (lp->rx_mode == RX_ALL_ACCEPT? (RX_CRC_ERROR_ENBL|RX_RUNT_ENBL|RX_EXTRA_DATA_ENBL) : 0));}static intset_mac_address(struct device *dev, void *addr){ int i; if (dev->start) return -EBUSY; printk(KERN_INFO "%s: Setting MAC address to ", dev->name); for (i = 0; i < 6; i++) printk(" %2.2x", dev->dev_addr[i] = ((unsigned char *)addr)[i]); printk(".\n"); /* set the Ethernet address */ for (i=0; i < ETH_ALEN/2; i++) writereg(dev, PP_IA+i*2, dev->dev_addr[i*2] | (dev->dev_addr[i*2+1] << 8)); return 0;}#ifdef MODULEstatic char namespace[16] = "";static struct device dev_cs89x0 = { NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL };static int io=0;static int irq=0;static int debug=0;static char media[8];static char duplex[8];#if ALLOW_DMAstatic int dma=0;static int dmasize=16; /* or 64 */#endif#if REQUIRE_MEMORYstatic long mmode=0;#endifMODULE_PARM(io , "i");MODULE_PARM(irq , "i");MODULE_PARM(debug , "i");MODULE_PARM(media , "s");MODULE_PARM(duplex , "s");#if ALLOW_DMAMODULE_PARM(dma , "i");MODULE_PARM(dmasize , "i");#endif#if REQUIRE_MEMORYMODULE_PARM(mmode , "l");#endif/** media=rj45 - specify media type or media=aui or media=bnc pr media=auto (default)* duplex=auto (default) - specify forced half/full/autonegotiate duplex or duplex=half or duplex=full* Default Chip Configuration: * DMA Burst = enabled * IOCHRDY Enabled = enabled * UseSA = enabled * CS8900 defaults to half-duplex if not specified on command-line * CS8920 defaults to autoneg if not specified on command-line * Use reset defaults for other config parameters* Assumptions: * media type specified is supported (circuitry is present) * if memory address is > 1MB, then required mem decode hw is present * if 10B-2, then agent other than driver will enable DC/DC converter (hw or software util)*/intinit_module(void){ struct net_local *lp; net_debug = debug; dev_cs89x0.name = namespace; dev_cs89x0.irq = irq; dev_cs89x0.base_addr = io;#if REQUIRE_MEMORY if ( mmode >= 0x100000 ) { printk("cs89x0: Error! mmode must be less than 0x100000.\n"); return -EPERM; } dev_cs89x0.mem_start=mmode;#endif dev_cs89x0.init = cs89x0_probe; dev_cs89x0.priv = kmalloc(sizeof(struct net_local), GFP_KERNEL); memset(dev_cs89x0.priv, 0, sizeof(struct net_local)); lp = (struct net_local *)dev_cs89x0.priv;#if ALLOW_DMA lp->dma = dma; lp->dmasize = dmasize;#endif if (!strcmp(media, "rj45")) lp->force |= FORCE_RJ45; else if (!strcmp(media, "aui")) lp->force |= FORCE_AUI; else if (!strcmp(media, "bnc")) lp->force |= FORCE_BNC; else lp->force = FORCE_RJ45 | FORCE_AUI | FORCE_BNC; if (!strcmp(duplex, "auto")) lp->force |= FORCE_AUTO; else if (!strcmp(duplex, "half")) lp->force |= FORCE_HALF; else if (!strcmp(duplex, "full")) lp->force |= FORCE_FULL; else lp->force |= FORCE_AUTO;/* CS8900 wii switch to HALF-DUPLEX*/ if (io == 0) { printk(KERN_ERR "cs89x0.c: Module autoprobing not allowed.\n"); printk(KERN_ERR "cs89x0.c: Append io=0xNNN\n"); return -EPERM; }#if ALLOW_DMA if (dmasize != 16 && dmasize != 64) { printk(KERN_ERR "cs89x0.c: dma size must be either 16K or 64K, not %dK\n", dmasize); return -EPERM; }#endif if (register_netdev(&dev_cs89x0) != 0) { printk(KERN_WARNING "cs89x0.c: No card found at 0x%x\n", io); return -ENXIO; } return 0;}voidcleanup_module(void){ if (dev_cs89x0.priv != NULL) { unregister_netdev(&dev_cs89x0); /* If we don't do this, we can't re-insmod it later. */ release_region(dev_cs89x0.base_addr, NETCARD_IO_EXTENT); /* Free up the private structure, or leak memory :-) */ kfree(dev_cs89x0.priv); dev_cs89x0.priv = NULL; /* gets re-allocated by cs89x0_probe1 */ /* Do ONE write to the address port, to get it back to address zero, where we expect to find the EISA signature word. */ outw(PP_ChipID, dev_cs89x0.base_addr + ADD_PORT); }}#endif /* MODULE *//* * Local variables: * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/include -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -DMODULE -DCONFIG_MODVERSIONS -c cs89x0.c" * version-control: t * kept-new-versions: 5 * c-indent-level: 8 * c-basic-offset: 8 * tab-width: 8 * End: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -