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

📄 rtk8189c.c

📁 ADTEK RTK8189C Linux 2.4.X Driver
💻 C
📖 第 1 页 / 共 4 页
字号:
			if (debug > 5)
				printk(KERN_DEBUG "  Rx data %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:"
					   "%2.2x %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x %2.2x%2.2x "
					   "%d.%d.%d.%d.\n",
					   skb->data[0], skb->data[1], skb->data[2], skb->data[3],
					   skb->data[4], skb->data[5], skb->data[6], skb->data[7],
					   skb->data[8], skb->data[9], skb->data[10],
					   skb->data[11], skb->data[12], skb->data[13],
					   skb->data[14], skb->data[15], skb->data[16],
					   skb->data[17]);
#endif
			skb->mac.raw = skb->data;
			if (0 && ntohs(skb->mac.ethernet->h_proto) >= 1536) {
				struct ethhdr *eth = skb->mac.ethernet;
				skb->protocol = eth->h_proto;
				if (desc_status & 0x1000) {
					if ((dev->flags & IFF_PROMISC) &&
						memcmp(eth->h_dest,
						       dev->dev_addr, ETH_ALEN))
						skb->pkt_type = PACKET_OTHERHOST;
				} else if (desc_status & 0x2000)
					skb->pkt_type = PACKET_BROADCAST;
				else if (desc_status & 0x4000)
					skb->pkt_type = PACKET_MULTICAST;
			} else
				skb->protocol = eth_type_trans(skb, dev);
			netif_rx(skb);
			dev->last_rx = jiffies;
			np->stats.rx_packets++;
#if LINUX_VERSION_CODE > 0x20127
			np->stats.rx_bytes += pkt_len;
#endif
		}

		/* reused the rx buffer */
		skb = (struct sk_buff *)np->rx_ring[np->cur_rx].rxbuffer;
		if (skb == NULL) {
			skb = dev_alloc_skb(np->rx_buf_sz);
			if (skb == NULL)
			{
			   if (np->lackrxcount==0)
			     np->dirty_rx=np->cur_rx;
			   ++np->lackrxcount;
			}
			else
			  skb->dev=dev;
		}

		if (skb)
		{
		   int entry;

		   if (np->lackrxcount==0)
		     entry=np->cur_rx;
		   else
		     entry=np->dirty_rx;
		   np->rx_ring[entry].ctrl_length = cpu_to_le32(np->rx_buf_sz);
		   np->rx_ring[entry].status = cpu_to_le32(DescOwn);
		   np->rx_ring[entry].buf_addr = virt_to_le32desc(skb->tail);
		   np->rx_ring[entry].rxbuffer = skb;
		   if (np->lackrxcount)
		   {
		      ++np->dirty_rx;
		      if (np->dirty_rx==RX_RING_SIZE)
			np->dirty_rx=0;
		   }
		}

		/* update cur_rx */
		++np->cur_rx;
		if (np->cur_rx==RX_RING_SIZE)
		  np->cur_rx=0;
		np->rx_head_desc = &np->rx_ring[np->cur_rx];
	}

	/* Refill the Rx ring buffers. */
	while (np->lackrxcount) {
		skb = dev_alloc_skb(np->rx_buf_sz);
		if (skb == NULL)
		   break;	    /* Better luck next round. */

		skb->dev = dev;     /* Mark as being used by this device. */
		np->rx_ring[np->dirty_rx].buf_addr =
						 virt_to_le32desc(skb->tail);
		np->rx_ring[np->dirty_rx].ctrl_length =
						 cpu_to_le32(np->rx_buf_sz);
		np->rx_ring[np->dirty_rx].status = cpu_to_le32(DescOwn);

		--np->lackrxcount;

		/* update dirty_rx */
		if (np->lackrxcount!=0)
		{
		   ++np->dirty_rx;
		   if (np->dirty_rx==RX_RING_SIZE)
		     np->dirty_rx=0;
		}
	}

	/* Restart Rx engine if stopped. */
	writel(0, dev->base_addr + RxStartDemand);
	return 0;
}


static void netdev_error(struct net_device *dev, int intr_status)
{
	struct netdev_private *np = (struct netdev_private *)dev->priv;
	long ioaddr = dev->base_addr;

	if (intr_status & (LinkChange | NWayDone)) {
		printk(KERN_ERR "%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));
		check_duplex(dev);
	}
/*
	if ((intr_status & TxUnderrun)
		&& (np->txrx_config & TxThreshold) != TxThreshold) {
		long ioaddr = dev->base_addr;
		np->txrx_config += 0x200000;
		writel(np->txrx_config, ioaddr + RxConfig);
		np->stats.tx_fifo_errors++;
	}
*/
	if (intr_status & TxUnderrun) {
		if (debug > 1)
		  printk(KERN_WARNING "%s: Transmitter underrun.\n", dev->name);
		writel(0, ioaddr + TxStartDemand);
		np->stats.tx_fifo_errors++;
	}
	if (intr_status & RxOverflow) {
		if (debug > 1)
		  printk(KERN_WARNING "%s: Receiver overflow.\n", dev->name);
		writel(0, ioaddr + RxStartDemand);
		np->stats.rx_over_errors++;
		netdev_rx(dev); 	/* Refill Rx descriptors */
		get_stats(dev); 	/* Empty dropped counter. */
	}
	if (intr_status & StatsMax) {
		get_stats(dev);
	}
	if ((intr_status & ~(LinkChange|NWayDone|StatsMax|TxUnderrun|RxOverflow
						 |TxEarly|RxEarly))
		&& (debug > 1)) {
		printk(KERN_ERR "%s: Something Wicked happened! %4.4x.\n",
			   dev->name, intr_status);
		printk(KERN_ERR "%s:  tcrrcr=%4.4x, bcr=%4.4x.\n",
			   dev->name, np->txrx_config,
			   readl(dev->base_addr+PCIBusCfg));
	}
	/* Hmmmmm, it's not clear how to recover from PCI faults. */
	if (intr_status & IntrPCIErr) {
		const char *const pcierr[4] =
		{ "Parity Error", "Master Abort", "Target Abort", "Unknown Error" };
		if (debug > 1)
		  printk(KERN_WARNING "%s: PCI Bus %s, %x.\n",
			   dev->name, pcierr[(intr_status>>11) & 3], intr_status);
	}
}

/* We do not bother to spinlock statistics.
   A window only exists if we have non-atomic adds, the error counts are
   typically zero, and statistics are non-critical. */
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;
	unsigned int rxerrs = readl(ioaddr + RxErrCnts);
	unsigned int txerrs = readl(ioaddr + TxErrCnts);

	/* The chip only need report frames silently dropped. */
	np->stats.rx_crc_errors += rxerrs >> 16;
	np->stats.rx_missed_errors	+= rxerrs & 0xffff;

	/* These stats are required when the descriptor is closed before Tx. */
	np->stats.tx_aborted_errors += txerrs >> 24;
	np->stats.tx_window_errors += (txerrs >> 16) & 0xff;
	np->stats.collisions += txerrs & 0xffff;

	return &np->stats;
}

/* Big-endian AUTODIN II ethernet CRC calculations.
   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 may be in the kernel with Linux 2.5+.
*/
static unsigned const ethernet_polynomial = 0x04c11db7U;
static inline u32 ether_crc(int length, unsigned char *data)
{
	u32 crc = ~0;

	while(--length >= 0) {
		unsigned char current_octet = *data++;
		int bit;
		for (bit = 0; bit < 8; bit++, current_octet >>= 1)
			crc = (crc << 1) ^
				((crc < 0) ^ (current_octet & 1) ? ethernet_polynomial : 0);
	}
	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;
	u32 mc_filter[2];			/* Multicast hash filter */
	u32 rx_mode;

	if (dev->flags & IFF_PROMISC) { 		/* Set promiscuous. */
		/* Unconditionally log net taps. */
		printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n", dev->name);
		mc_filter[1] = mc_filter[0] = ~0;
		rx_mode = AcceptBroadcast | AcceptMulticast | AcceptAllPhys
			| AcceptMyPhys;
	} else if ((dev->mc_count > multicast_filter_limit)
			   ||  (dev->flags & IFF_ALLMULTI)) {
		/* Too many to match, or accept all multicasts. */
		mc_filter[1] = mc_filter[0] = ~0;
		rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
	} else {
		struct dev_mc_list *mclist;
		int i;
		mc_filter[1] = mc_filter[0] = 0;
		for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
			 i++, mclist = mclist->next) {
			set_bit((ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26) & 0x3f,
					mc_filter);
		}
		rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
	}
	if (mc_filter[0] != np->mcast_filter[0]  ||
		mc_filter[1] != np->mcast_filter[1]) {
		writel(mc_filter[0], ioaddr + MulticastFilter0);
		writel(mc_filter[1], ioaddr + MulticastFilter1);
		np->mcast_filter[0] = mc_filter[0];
		np->mcast_filter[1] = mc_filter[1];
	}
	if ((np->txrx_config & RxFilter) != rx_mode) {
		np->txrx_config &= ~RxFilter;
		np->txrx_config |= rx_mode;
		writel(np->txrx_config, ioaddr + RxConfig);
	}
}

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;

	switch(cmd) {
	case SIOCGMIIPHY:		/* Get the address of the PHY in use. */
		data[0] = np->phys[0];
		/* Fall Through */
	case SIOCGMIIREG:		/* Read the specified MII register. */
		data[3] = mdio_read(dev, data[0], data[1]);
		return 0;
	case 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], data[1], data[2]);
		return 0;
	default:
		return -EOPNOTSUPP;
	}
}

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 (debug > 1) {
		printk(KERN_DEBUG "%s: Shutting down ethercard, status was %8.8x.\n",
			   dev->name, (int)readl(ioaddr + RxConfig));
		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. */
	writel(0x0000, ioaddr + IntrEnable);

	/* Stop the chip's Tx and Rx processes. */
	np->txrx_config = 0;
	writel(0, ioaddr + RxConfig);

	del_timer(&np->timer);

#ifdef __i386__
	if (debug > 2) {
		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. %x %x %8.8x.\n",
				   i, np->tx_ring[i].status, np->tx_ring[i].ctrl_length,
				   np->tx_ring[i].buf_addr);
		printk("\n"KERN_DEBUG "  Rx ring %8.8x:\n",
			   (int)virt_to_bus(np->rx_ring));
		for (i = 0; i < RX_RING_SIZE; i++) {
			printk(KERN_DEBUG " #%d desc. %4.4x %4.4x %8.8x\n",
				   i, np->rx_ring[i].status, np->rx_ring[i].ctrl_length,
				   np->rx_ring[i].buf_addr);
		}
	}
#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].buf_addr = 0xBADF00D0; /* An invalid address. */
		if (np->rx_ring[i].rxbuffer) {
#if LINUX_VERSION_CODE < 0x20100
			np->rx_ring[i].rxbuffer->free = 1;
#endif
			dev_free_skb(np->rx_ring[i].rxbuffer);
		}
		np->rx_ring[i].rxbuffer = 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;
}

static int netdev_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 (debug > 1)
		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. */
		writel(0x0000, ioaddr + IntrEnable);
		writel(0, ioaddr + RxConfig);
		break;
	case DRV_RESUME:
		/* This is incomplete: the actions are very chip specific. */
		set_rx_mode(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;
	}
	}

	return 0;
}

#ifdef MODULE
int init_module(void)
{
	if (debug)	/* Emit version even if no cards detected. */
		printk(KERN_INFO "%s" KERN_INFO "%s", version1, version2);
	return pci_drv_register(&RTK8189C_drv_id, NULL);
}

void cleanup_module(void)
{
	struct net_device *next_dev;

	pci_drv_unregister(&RTK8189C_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 */

⌨️ 快捷键说明

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