1. 程式人生 > >cs8900網絡卡驅動解析(五)

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字樣,會使函式非常易讀。

大神一句話:對網絡卡的控制需要熟讀硬體手冊。

其實還有很多的函式沒有寫,同時我自己也有很多地方沒有弄懂,不過三天都在看著不到七百行的程式碼了。我覺得我寫的註釋還可以,如果誰想要這套程式碼可以給我留言的。