cs8900网卡驱动解析(五)

几个主要的模块的都写完了,下面开始介绍一下其他的函数。

和net_open对应的函数:



/* The inverse routine to net_open(). */
/*
	①停止队列
	②关闭网卡
	③释放中断
*/
static int net_close(struct net_device *dev)
{
	netif_stop_queue(dev);
	
	writereg(dev, PP_RxCFG, 0);
	writereg(dev, PP_TxCFG, 0);
	writereg(dev, PP_BufCFG, 0);
	writereg(dev, PP_BusCTL, 0);

	free_irq(dev->irq, dev);

	/* Update the statistics here. */
	return 0;
}

再看一下cs8900是如何获取统计信息的:



/*
	向用户反馈设备状态和统计信息
	net_device_stats,这是一个net_local的结构体成员。
*/
static struct net_device_stats *
net_get_stats(struct net_device *dev)
{
	struct net_local *lp = (struct net_local *)dev->priv;
	unsigned long flags;
	//不追了,这个就知道它是在读取各个寄存器状态的信息
	//然后都存放在net_device_stats这个结构体中,结构体的具体定义在内核中实现。
	spin_lock_irqsave(&lp->lock, flags);
	/* 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);
	spin_unlock_irqrestore(&lp->lock, flags);

	return &lp->stats;
}


 

stats是一个net_device_stats结构,其实说简单点,你只要返回stats结构就可以了,它里面记录了你需要的信息。net_device_stats结构在netdevice.h文件中。


struct net_device_stats
{
       unsigned long rx_packets;     /* total packets received */
       unsigned long tx_packets;     /* total packets transmitted    */
       unsigned long rx_bytes;        /* total bytes received  */
       unsigned long tx_bytes;        /* total bytes transmitted       */
       unsigned long rx_errors;              /* bad packets received         */
       unsigned long tx_errors;              /* packet transmit problems   */
       unsigned long rx_dropped;    /* no space in linux buffers   */
       unsigned long tx_dropped;    /* no space available in linux */
       unsigned long multicast;              /* multicast packets received */
       unsigned long collisions;
 
       /* detailed rx_errors: */
       unsigned long rx_length_errors;
       unsigned long rx_over_errors;      /* receiver ring buff overflow       */
       unsigned long rx_crc_errors;        /* recved pkt with crc error   */
       unsigned long rx_frame_errors;    /* recv'd frame alignment error */
       unsigned long rx_fifo_errors;              /* recv'r fifo overrun            */
       unsigned long rx_missed_errors;   /* receiver missed packet       */
 
       /* detailed tx_errors */
       unsigned long tx_aborted_errors;
       unsigned long tx_carrier_errors;
       unsigned long tx_fifo_errors;
       unsigned long tx_heartbeat_errors;
       unsigned long tx_window_errors;
       
       /* for cslip etc */
       unsigned long rx_compressed;
       unsigned long tx_compressed;
};
cs8900_get_stats这个函数虽然简单,但是很实用。
cs8900_set_receive_mode函数设置网卡模式,需要配合手册来阅读。出现的新函数(cs8900_set和cs8900_clear)实际上就是cs8900_write,就是通过与(&)或(|)运算,实用set和clear字样,会使函数非常易读。

大神一句话:对网卡的控制需要熟读硬件手册。

其实还有很多的函数没有写,同时我自己也有很多地方没有弄懂,不过三天都在看着不到七百行的代码了。我觉得我写的注释还可以,如果谁想要这套代码可以给我留言的。

你可能感兴趣的:(cs8900网卡驱动解析(五))