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

📄 cirrus.c

📁 cs9800 driver code. 经过测试
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * linux/drivers/net/cirrus.c * * Author: Abraham van der Merwe <abraham@2d3d.co.za> * * A Cirrus Logic CS8900A driver for Linux * based on the cs89x0 driver written by Russell Nelson, * Donald Becker, and others. * * This source code is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * version 2 as published by the Free Software Foundation. *//* * At the moment the driver does not support memory mode operation. * It is trivial to implement this, but not worth the effort. *//* * TODO: * *   1. If !ready in send_start(), queue buffer and send it in interrupt handler *      when we receive a BufEvent with Rdy4Tx, send it again. dangerous! *   2. how do we prevent interrupt handler destroying integrity of get_stats()? *   3. Change reset code to check status. *   4. Implement set_mac_address and remove fake mac address *   5. Link status detection stuff *   6. Write utility to write EEPROM, do self testing, etc. *   7. Implement DMA routines (I need a board w/ DMA support for that) *   8. Power management *   9. Add support for multiple ethernet chips *  10. Add support for other cs89xx chips (need hardware for that) */#include <linux/config.h>#include <linux/version.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/types.h>#include <linux/errno.h>#include <linux/ioport.h>#include <linux/init.h>#include <linux/delay.h>#include <asm/irq.h>#include <asm/hardware.h>#include <asm/io.h>#include <asm/arch/regs-gpio.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>#include "cirrus.h"/* #define DEBUG *//* #define FULL_DUPLEX */#if 0//#elif CONFIG_ARCH_CSB226#	define CIRRUS_DEFAULT_IO 0xF8000300#	define CIRRUS_DEFAULT_IRQ IRQ_GPIO(16) 	/* PTX: uggly hack... */#	define CS8900_OFF 0x04#	define CS8900_PPTR	*(volatile u32 *)(CIRRUS_DEFAULT_IO+0x05*CS8900_OFF)#	define CS8900_PDATA	*(volatile u32 *)(CIRRUS_DEFAULT_IO+0x06*CS8900_OFF)//#else#	define CIRRUS_DEFAULT_IO	0#	define CIRRUS_DEFAULT_IRQ	0#endiftypedef struct {	struct net_device_stats stats;	u16 txlen;	int char_devnum;        spinlock_t lock;} cirrus_t;typedef struct {	u16 io_base;		/* I/O Base Address			*/	u16 irq;			/* Interrupt Number			*/	u16 dma;			/* DMA Channel Numbers		*/	u32 mem_base;		/* Memory Base Address		*/	u32 rom_base;		/* Boot PROM Base Address	*/	u32 rom_mask;		/* Boot PROM Address Mask	*/	u8 mac[6];			/* Individual Address		*/} cirrus_eeprom_t;/* * I/O routines */static inline u16 cirrus_read (struct net_device *dev,u16 reg){	CIRRUS_outw (reg, dev->base_addr, PP_Address);	return (CIRRUS_inw (dev->base_addr, PP_Data));}static inline void cirrus_write (struct net_device *dev,u16 reg,u16 value){	CIRRUS_outw (reg, dev->base_addr, PP_Address);	CIRRUS_outw (value, dev->base_addr, PP_Data);}static inline void cirrus_set (struct net_device *dev,u16 reg,u16 value){	cirrus_write (dev,reg,cirrus_read (dev,reg) | value);}static inline void cirrus_clear (struct net_device *dev,u16 reg,u16 value){	cirrus_write (dev,reg,cirrus_read (dev,reg) & ~value);}static inline void cirrus_frame_read (struct net_device *dev,struct sk_buff *skb,u16 length){	CIRRUS_insw (dev->base_addr, 0, skb_put (skb,length), (length + 1) / 2);}static inline void cirrus_frame_write (struct net_device *dev,struct sk_buff *skb){	CIRRUS_outsw (dev->base_addr, 0, skb->data, (skb->len + 1) / 2);}/* * Debugging functions */#ifdef DEBUGstatic inline int printable (int c){	return ((c >= 32 && c <= 126) ||			(c >= 174 && c <= 223) ||			(c >= 242 && c <= 243) ||			(c >= 252 && c <= 253));}static void dump16 (struct net_device *dev,const u8 *s,size_t len){	int i;	char str[128];	if (!len) return;	*str = '\0';	for (i = 0; i < len; i++) {		if (i && !(i % 4)) strcat (str," ");		sprintf (str,"%s%.2x ",str,s[i]);	}	for ( ; i < 16; i++) {		if (i && !(i % 4)) strcat (str," ");		strcat (str,"   ");	}	strcat (str," ");	for (i = 0; i < len; i++) sprintf (str,"%s%c",str,printable (s[i]) ? s[i] : '.');	printk (KERN_DEBUG "%s:     %s\n",dev->name,str);}static void hexdump (struct net_device *dev,const void *ptr,size_t size){	const u8 *s = (u8 *) ptr;	int i;	for (i = 0; i < size / 16; i++, s += 16) dump16 (dev,s,16);	dump16 (dev,s,size % 16);}static void dump_packet (struct net_device *dev,struct sk_buff *skb,const char *type){	printk (KERN_INFO "%s: %s %d byte frame %.2x:%.2x:%.2x:%.2x:%.2x:%.2x to %.2x:%.2x:%.2x:%.2x:%.2x:%.2x type %.4x\n",			dev->name,			type,			skb->len,			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] << 8) | skb->data[13]);	if (skb->len < 0x100) hexdump (dev,skb->data,skb->len);}#endif	/* #ifdef DEBUG *//* * Driver functions */static void cirrus_receive (struct net_device *dev){	cirrus_t *priv = (cirrus_t *) dev->priv;	struct sk_buff *skb;	u16 status,length;	status = cirrus_read (dev,PP_RxStatus);	length = cirrus_read (dev,PP_RxLength);	if (!(status & RxOK)) {		priv->stats.rx_errors++;		if ((status & (Runt | Extradata))) priv->stats.rx_length_errors++;		if ((status & CRCerror)) priv->stats.rx_crc_errors++;		return;	}	if ((skb = dev_alloc_skb (length + 4)) == NULL) {		priv->stats.rx_dropped++;		return;	}	skb->dev = dev;	skb_reserve (skb,2);	cirrus_frame_read (dev,skb,length);#ifdef DEBUG	dump_packet (dev,skb,"recv");#endif	/* #ifdef DEBUG */	skb->protocol = eth_type_trans (skb,dev);	netif_rx (skb);	dev->last_rx = jiffies;	priv->stats.rx_packets++;	priv->stats.rx_bytes += length;}static int cirrus_send_start (struct sk_buff *skb,struct net_device *dev){	cirrus_t *priv = (cirrus_t *) dev->priv;	u16 status;	spin_lock_irq(&priv->lock);	netif_stop_queue (dev);	cirrus_write (dev,PP_TxCMD,TxStart (After5));	cirrus_write (dev,PP_TxLength,skb->len);	status = cirrus_read (dev,PP_BusST);	if ((status & TxBidErr)) {		spin_unlock_irq(&priv->lock);		printk (KERN_WARNING "%s: Invalid frame size %d!\n",dev->name,skb->len);		priv->stats.tx_errors++;		priv->stats.tx_aborted_errors++;		priv->txlen = 0;		return (1);	}	if (!(status & Rdy4TxNOW)) {		spin_unlock_irq(&priv->lock);		printk (KERN_WARNING "%s: Transmit buffer not free!\n",dev->name);		priv->stats.tx_errors++;		priv->txlen = 0;		/* FIXME: store skb and send it in interrupt handler */		return (1);	}	cirrus_frame_write (dev,skb);	spin_unlock_irq(&priv->lock);#ifdef DEBUG	dump_packet (dev,skb,"send");#endif	/* #ifdef DEBUG */	dev->trans_start = jiffies;	dev_kfree_skb (skb);	priv->txlen = skb->len;	return (0);}static irqreturn_t cirrus_interrupt (int irq,void *id,struct pt_regs *regs){	struct net_device *dev = (struct net_device *) id;	cirrus_t *priv;	u16 status;	int handled = 0;	if (dev->priv == NULL) {		printk (KERN_WARNING "%s: irq %d for unknown device.\n",dev->name,irq);		handled = 1;		return 0;//IRQ_RETVAL(handled);	}	priv = (cirrus_t *) dev->priv;	while ((status = cirrus_read (dev,PP_ISQ))) {		handled = 1;		switch (RegNum (status)) {		case RxEvent:			cirrus_receive (dev);			handled = 1;			break;		case TxEvent:			priv->stats.collisions += ColCount (cirrus_read (dev,PP_TxCOL));			if (!(RegContent (status) & TxOK)) {				priv->stats.tx_errors++;				if ((RegContent (status) & Out_of_window)) priv->stats.tx_window_errors++;				if ((RegContent (status) & Jabber)) priv->stats.tx_aborted_errors++;				break;			} else if (priv->txlen) {				priv->stats.tx_packets++;				priv->stats.tx_bytes += priv->txlen;			}			priv->txlen = 0;			netif_wake_queue (dev);			handled = 1;			break;		case BufEvent:			if ((RegContent (status) & RxMiss)) {				u16 missed = MissCount (cirrus_read (dev,PP_RxMISS));				priv->stats.rx_errors += missed;				priv->stats.rx_missed_errors += missed;			}			if ((RegContent (status) & TxUnderrun)) {				priv->stats.tx_errors++;				priv->stats.tx_fifo_errors++;			}			// FIXME: if Rdy4Tx, transmit last sent packet (if any) 			priv->txlen = 0;			netif_wake_queue (dev);			handled = 1;			break;		case TxCOL:			priv->stats.collisions += ColCount (cirrus_read (dev,PP_TxCOL));			handled = 1;			break;		case RxMISS:			status = MissCount (cirrus_read (dev,PP_RxMISS));			priv->stats.rx_errors += status;			priv->stats.rx_missed_errors += status;			handled = 1;			break;		}	}		if(!handled) printk("Invalid cs8900 irq\n");	return IRQ_HANDLED;//IRQ_RETVAL(handled);}   static void cirrus_transmit_timeout (struct net_device *dev){	cirrus_t *priv = (cirrus_t *) dev->priv;	priv->stats.tx_errors++;	priv->stats.tx_heartbeat_errors++;	priv->txlen = 0;	netif_wake_queue (dev);}static int cirrus_start (struct net_device *dev){	int result;	/* valid ethernet address? */	if (!is_valid_ether_addr(dev->dev_addr)) {		printk(KERN_ERR "%s: invalid ethernet MAC address\n",dev->name);		return (-EINVAL);	}	/* install interrupt handler */

⌨️ 快捷键说明

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