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

📄 sundance.c

📁 Linux下各种网卡的驱动程序
💻 C
📖 第 1 页 / 共 4 页
字号:
	/* Refill the Rx ring buffers. */	for (; np->cur_rx - np->dirty_rx > 0; np->dirty_rx++) {		struct sk_buff *skb;		entry = np->dirty_rx % RX_RING_SIZE;		if (np->rx_skbuff[entry] == NULL) {			skb = dev_alloc_skb(np->rx_buf_sz);			np->rx_skbuff[entry] = skb;			if (skb == NULL)				break;				/* Better luck next round. */			skb->dev = dev;			/* Mark as being used by this device. */			skb_reserve(skb, 2);	/* Align IP on 16 byte boundaries */			np->rx_ring[entry].frag[0].addr = virt_to_le32desc(skb->tail);		}		/* Perhaps we need not reset this field. */		np->rx_ring[entry].frag[0].length =			cpu_to_le32(np->rx_buf_sz | LastFrag);		np->rx_ring[entry].status = 0;	}	/* No need to restart Rx engine, it will poll. */	return 0;}static void netdev_error(struct net_device *dev, int intr_status){	long ioaddr = dev->base_addr;	struct netdev_private *np = (struct netdev_private *)dev->priv;	if (intr_status & IntrDrvRqst) {		/* Stop the down counter and turn interrupts back on. */		printk(KERN_WARNING "%s: Turning interrupts back on.\n", dev->name);		writew(0, ioaddr + DownCounter);		writew(IntrRxDMADone | IntrPCIErr | IntrDrvRqst |			   IntrTxDone | StatsMax | LinkChange, ioaddr + IntrEnable);	}	if (intr_status & LinkChange) {		int new_status = readb(ioaddr + MIICtrl) & 0xE0;		if (np->msg_level & NETIF_MSG_LINK)			printk(KERN_NOTICE "%s: Link changed: Autonegotiation advertising"				   " %4.4x  partner %4.4x.\n", dev->name,				   mdio_read(dev, np->phys[0], 4),				   mdio_read(dev, np->phys[0], 5));		if ((np->link_status ^ new_status) & 0x80) {			if (new_status & 0x80)				netif_link_up(dev);			else				netif_link_down(dev);		}		np->link_status = new_status;		check_duplex(dev);	}	if (intr_status & StatsMax) {		get_stats(dev);	}	if (intr_status & IntrPCIErr) {		printk(KERN_ERR "%s: Something Wicked happened! %4.4x.\n",			   dev->name, intr_status);		/* We must do a global reset of DMA to continue. */	}}static struct net_device_stats *get_stats(struct net_device *dev){	long ioaddr = dev->base_addr;	struct netdev_private *np = (struct netdev_private *)dev->priv;	int i;	if (readw(ioaddr + StationAddr) == 0xffff)		return &np->stats;	/* We do not spinlock statistics.	   A window only exists if we have non-atomic adds, the error counts	   are typically zero, and statistics are non-critical. */ 	np->stats.rx_missed_errors	+= readb(ioaddr + RxMissed);	np->stats.tx_packets += readw(ioaddr + TxFramesOK);	np->stats.rx_packets += readw(ioaddr + RxFramesOK);	np->stats.collisions += readb(ioaddr + StatsLateColl);	np->stats.collisions += readb(ioaddr + StatsMultiColl);	np->stats.collisions += readb(ioaddr + StatsOneColl);	readb(ioaddr + StatsCarrierError);	readb(ioaddr + StatsTxDefer);	for (i = StatsTxXSDefer; i <= StatsMcastRx; i++)		readb(ioaddr + i);#if LINUX_VERSION_CODE > 0x20127	np->stats.tx_bytes += readw(ioaddr + TxOctetsLow);	np->stats.tx_bytes += readw(ioaddr + TxOctetsHigh) << 16;	np->stats.rx_bytes += readw(ioaddr + RxOctetsLow);	np->stats.rx_bytes += readw(ioaddr + RxOctetsHigh) << 16;#else	readw(ioaddr + TxOctetsLow);	readw(ioaddr + TxOctetsHigh);	readw(ioaddr + RxOctetsLow);	readw(ioaddr + RxOctetsHigh);#endif	return &np->stats;}/* The little-endian AUTODIN II ethernet CRC calculations.   A big-endian version is also available.   This is slow but compact code.  Do not use this routine for bulk data,   use a table-based routine instead.   This is common code and should be moved to net/core/crc.c.   Chips may use the upper or lower CRC bits, and may reverse and/or invert   them.  Select the endian-ness that results in minimal calculations.*/static unsigned const ethernet_polynomial_le = 0xedb88320U;static inline unsigned ether_crc_le(int length, unsigned char *data){	unsigned int crc = ~0;	/* Initial value. */	while(--length >= 0) {		unsigned char current_octet = *data++;		int bit;		for (bit = 8; --bit >= 0; current_octet >>= 1) {			if ((crc ^ current_octet) & 1) {				crc >>= 1;				crc ^= ethernet_polynomial_le;			} else				crc >>= 1;		}	}	return crc;}static void set_rx_mode(struct net_device *dev){	struct netdev_private *np = (struct netdev_private *)dev->priv;	long ioaddr = dev->base_addr;	u16 mc_filter[4];			/* Multicast hash filter */	u32 rx_mode;	int i;	if (dev->flags & IFF_PROMISC) {			/* Set promiscuous. */		/* Unconditionally log net taps. */		printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n", dev->name);		memset(mc_filter, ~0, sizeof(mc_filter));		rx_mode = AcceptBroadcast | AcceptMulticast | AcceptAll | AcceptMyPhys;	} else if ((dev->mc_count > np->multicast_filter_limit)			   ||  (dev->flags & IFF_ALLMULTI)) {		/* Too many to match, or accept all multicasts. */		memset(mc_filter, 0xff, sizeof(mc_filter));		rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;	} else if (dev->mc_count) {		struct dev_mc_list *mclist;		memset(mc_filter, 0, sizeof(mc_filter));		for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;			 i++, mclist = mclist->next) {			set_bit(ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x3f,					mc_filter);		}		rx_mode = AcceptBroadcast | AcceptMultiHash | AcceptMyPhys;	} else {		writeb(AcceptBroadcast | AcceptMyPhys, ioaddr + RxMode);		return;	}	for (i = 0; i < 4; i++)		writew(mc_filter[i], ioaddr + MulticastFilter0 + i*2);	writeb(rx_mode, ioaddr + RxMode);}static int mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd){	struct netdev_private *np = (struct netdev_private *)dev->priv;	u16 *data = (u16 *)&rq->ifr_data;	u32 *data32 = (void *)&rq->ifr_data;	switch(cmd) {	case 0x8947: case 0x89F0:		/* SIOCGMIIPHY: Get the address of the PHY in use. */		data[0] = np->phys[0] & 0x1f;		/* Fall Through */	case 0x8948: case 0x89F1:		/* SIOCGMIIREG: Read the specified MII register. */		data[3] = mdio_read(dev, data[0] & 0x1f, data[1] & 0x1f);		return 0;	case 0x8949: case 0x89F2:		/* SIOCSMIIREG: Write the specified MII register */		if (!capable(CAP_NET_ADMIN))			return -EPERM;		if (data[0] == np->phys[0]) {			u16 value = data[2];			switch (data[1]) {			case 0:				/* Check for autonegotiation on or reset. */				np->medialock = (value & 0x9000) ? 0 : 1;				if (np->medialock)					np->full_duplex = (value & 0x0100) ? 1 : 0;				break;			case 4: np->advertising = value; break;			}			/* Perhaps check_duplex(dev), depending on chip semantics. */		}		mdio_write(dev, data[0] & 0x1f, data[1] & 0x1f, data[2]);		return 0;	case SIOCGPARAMS:		data32[0] = np->msg_level;		data32[1] = np->multicast_filter_limit;		data32[2] = np->max_interrupt_work;		data32[3] = np->rx_copybreak;		return 0;	case SIOCSPARAMS:		if (!capable(CAP_NET_ADMIN))			return -EPERM;		np->msg_level = data32[0];		np->multicast_filter_limit = data32[1];		np->max_interrupt_work = data32[2];		np->rx_copybreak = data32[3];		return 0;	default:		return -EOPNOTSUPP;	}}static int sundance_pwr_event(void *dev_instance, int event){	struct net_device *dev = dev_instance;	struct netdev_private *np = (struct netdev_private *)dev->priv;	long ioaddr = dev->base_addr;	if (np->msg_level & NETIF_MSG_LINK)		printk(KERN_DEBUG "%s: Handling power event %d.\n", dev->name, event);	switch(event) {	case DRV_ATTACH:		MOD_INC_USE_COUNT;		break;	case DRV_SUSPEND:		/* Disable interrupts, stop Tx and Rx. */		writew(0x0000, ioaddr + IntrEnable);		writew(TxDisable | RxDisable | StatsDisable, ioaddr + MACCtrl1);		break;	case DRV_RESUME:		sundance_start(dev);		break;	case DRV_DETACH: {		struct net_device **devp, **next;		if (dev->flags & IFF_UP) {			/* Some, but not all, kernel versions close automatically. */			dev_close(dev);			dev->flags &= ~(IFF_UP|IFF_RUNNING);		}		unregister_netdev(dev);		release_region(dev->base_addr, pci_id_tbl[np->chip_id].io_size);#ifndef USE_IO_OPS		iounmap((char *)dev->base_addr);#endif		for (devp = &root_net_dev; *devp; devp = next) {			next = &((struct netdev_private *)(*devp)->priv)->next_module;			if (*devp == dev) {				*devp = *next;				break;			}		}		if (np->priv_addr)			kfree(np->priv_addr);		kfree(dev);		MOD_DEC_USE_COUNT;		break;	}	case DRV_PWR_WakeOn:		writeb(readb(ioaddr + WakeEvent) | 2, ioaddr + WakeEvent);		/* Fall through. */	case DRV_PWR_DOWN:	case DRV_PWR_UP:		acpi_set_pwr_state(np->pci_dev, event==DRV_PWR_UP ? ACPI_D0:ACPI_D3);		break;	default:		return -1;	}	return 0;}static int netdev_close(struct net_device *dev){	long ioaddr = dev->base_addr;	struct netdev_private *np = (struct netdev_private *)dev->priv;	int i;	netif_stop_tx_queue(dev);	if (np->msg_level & NETIF_MSG_IFDOWN) {		printk(KERN_DEBUG "%s: Shutting down ethercard, status was Tx %2.2x "			   "Rx %4.4x Int %2.2x.\n",			   dev->name, (int)readw(ioaddr + TxStatus),			   (int)readl(ioaddr + RxStatus), (int)readw(ioaddr + IntrStatus));		printk(KERN_DEBUG "%s: Queue pointers were Tx %d / %d,  Rx %d / %d.\n",			   dev->name, np->cur_tx, np->dirty_tx, np->cur_rx, np->dirty_rx);	}	/* Disable interrupts by clearing the interrupt mask. */	writew(0x0000, ioaddr + IntrEnable);	/* Stop the chip's Tx and Rx processes. */	writew(TxDisable | RxDisable | StatsDisable, ioaddr + MACCtrl1);	del_timer(&np->timer);#ifdef __i386__	if (np->msg_level & NETIF_MSG_IFDOWN) {		printk("\n"KERN_DEBUG"  Tx ring at %8.8x:\n",			   (int)virt_to_bus(np->tx_ring));		for (i = 0; i < TX_RING_SIZE; i++)			printk(" #%d desc. %4.4x %8.8x %8.8x.\n",				   i, np->tx_ring[i].status, np->tx_ring[i].frag[0].addr,				   np->tx_ring[i].frag[0].length);		printk("\n"KERN_DEBUG "  Rx ring %8.8x:\n",			   (int)virt_to_bus(np->rx_ring));		for (i = 0; i < /*RX_RING_SIZE*/4 ; i++) {			printk(KERN_DEBUG " #%d desc. %4.4x %4.4x %8.8x\n",				   i, np->rx_ring[i].status, np->rx_ring[i].frag[0].addr,				   np->rx_ring[i].frag[0].length);		}	}#endif /* __i386__ debugging only */	free_irq(dev->irq, dev);	/* Free all the skbuffs in the Rx queue. */	for (i = 0; i < RX_RING_SIZE; i++) {		np->rx_ring[i].status = 0;		np->rx_ring[i].frag[0].addr = 0xBADF00D0; /* An invalid address. */		if (np->rx_skbuff[i]) {#if LINUX_VERSION_CODE < 0x20100			np->rx_skbuff[i]->free = 1;#endif			dev_free_skb(np->rx_skbuff[i]);		}		np->rx_skbuff[i] = 0;	}	for (i = 0; i < TX_RING_SIZE; i++) {		if (np->tx_skbuff[i])			dev_free_skb(np->tx_skbuff[i]);		np->tx_skbuff[i] = 0;	}	MOD_DEC_USE_COUNT;	return 0;}#ifdef MODULEint init_module(void){	if (debug >= NETIF_MSG_DRV)	/* Emit version even if no cards detected. */		printk(KERN_INFO "%s" KERN_INFO "%s", version1, version2);	return pci_drv_register(&sundance_drv_id, NULL);}void cleanup_module(void){	struct net_device *next_dev;	pci_drv_unregister(&sundance_drv_id);	/* No need to check MOD_IN_USE, as sys_delete_module() checks. */	while (root_net_dev) {		struct netdev_private *np = (void *)(root_net_dev->priv);		unregister_netdev(root_net_dev);#ifdef USE_IO_OPS		release_region(root_net_dev->base_addr,					   pci_id_tbl[np->chip_id].io_size);#else		iounmap((char *)root_net_dev->base_addr);#endif		next_dev = np->next_module;		if (np->priv_addr)			kfree(np->priv_addr);		kfree(root_net_dev);		root_net_dev = next_dev;	}}#endif  /* MODULE *//* * Local variables: *  compile-command: "make KERNVER=`uname -r` sundance.o" *  compile-cmd1: "gcc -DMODULE -Wall -Wstrict-prototypes -O6 -c sundance.c" *  simple-compile-command: "gcc -DMODULE -O6 -c sundance.c" *  c-indent-level: 4 *  c-basic-offset: 4 *  tab-width: 4 * End: */

⌨️ 快捷键说明

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