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

📄 cs8900.c

📁 CS8900适配器驱动在嵌入式LINUX下应用的一个实例。对于想学习网络驱动程序开发的朋友而言
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * linux/drivers/net/cs8900.c * */ #define VERSION_STRING "Cirrus Logic CS8900A driver for Linux (Modified for MIKE2410)"#include <linux/config.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/types.h>#include <linux/version.h>#include <linux/errno.h>#include <linux/init.h>#include <linux/delay.h>#include <linux/ioport.h>#include <asm/irq.h>#include <asm/hardware.h>#include <asm/io.h>#include <asm/uaccess.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>#include <asm/mach-types.h>#ifdef CONFIG_ARCH_SMDK2410#include "asm/arch/smdk2410.h"#endif#include "cs8900.h"typedef struct {	struct net_device_stats stats;	u16 txlen;	int char_devnum;        spinlock_t lock;} cs8900_t;int cs8900_probe (struct net_device *dev);static struct net_device cs8900_dev ={        init: cs8900_probe};/*  * There seems to be no way to determine the exact size of the eeprom,  * so we use the largest size. * FIXME: Verify it's safe to read/write past the end of a 64/128 *        byte eeprom.  * * Possible eeprom sizes:  * Cx46 -  64 bytes * Cx56 - 128 bytes * Cx66 - 256 bytes */#define MAX_EEPROM_SIZE		256static int cs8900_eeprom_fopen(struct inode *inode, struct file *file);static int cs8900_eeprom_frelease(struct inode *inode, struct file *file);static loff_t cs8900_eeprom_fllseek(struct file * file,loff_t offset, int flags);static ssize_t cs8900_eeprom_fread(struct file *file, char *buf, size_t count, loff_t *f_pos);static ssize_t cs8900_eeprom_fwrite(struct file *file, const char *buf, size_t count, loff_t *f_pos);static struct file_operations cs8900_eeprom_fops = {        owner:          THIS_MODULE,        open:           cs8900_eeprom_fopen,        release:        cs8900_eeprom_frelease,        llseek:         cs8900_eeprom_fllseek,        read:           cs8900_eeprom_fread,        write:          cs8900_eeprom_fwrite,};      static u16 cs8900_eeprom_cache[MAX_EEPROM_SIZE/2];/* * I/O routines */static inline u16 cs8900_read (struct net_device *dev,u16 reg){	outw (reg,dev->base_addr + PP_Address);	return (inw (dev->base_addr + PP_Data));}static inline void cs8900_write (struct net_device *dev,u16 reg,u16 value){	outw (reg,dev->base_addr + PP_Address);	outw (value,dev->base_addr + PP_Data);}static inline void cs8900_set (struct net_device *dev,u16 reg,u16 value){	cs8900_write (dev,reg,cs8900_read (dev,reg) | value);}static inline void cs8900_clear (struct net_device *dev,u16 reg,u16 value){	cs8900_write (dev,reg,cs8900_read (dev,reg) & ~value);}static inline void cs8900_frame_read (struct net_device *dev,struct sk_buff *skb,u16 length){	insw (dev->base_addr,skb_put (skb,length),(length + 1) / 2);}static inline void cs8900_frame_write (struct net_device *dev,struct sk_buff *skb){	outsw (dev->base_addr,skb->data,(skb->len + 1) / 2);}/* * EEPROM I/O routines */static int cs8900_eeprom_wait (struct net_device *dev){	int i;	for (i = 0; i < 3000; i++) {		if (!(cs8900_read (dev,PP_SelfST) & SIBUSY))			return (0);		udelay (1);	}	return (-1);}static int cs8900_eeprom_read (struct net_device *dev,u16 *value,u16 offset){	if (cs8900_eeprom_wait (dev) < 0)		return (-1);	cs8900_write (dev,PP_EEPROMCommand,offset | EEReadRegister);	if (cs8900_eeprom_wait (dev) < 0)		return (-1);	*value = cs8900_read (dev,PP_EEPROMData);	return (0);}static int cs8900_eeprom_write (struct net_device *dev,u16 *value,u16 offset){	cs8900_eeprom_wait(dev);        cs8900_write(dev, PP_EEPROMCommand, (EEWriteEnable));	cs8900_eeprom_wait(dev);        cs8900_write(dev, PP_EEPROMData, *value);	cs8900_eeprom_wait(dev);        cs8900_write(dev, PP_EEPROMCommand, (offset | EEWriteRegister));	cs8900_eeprom_wait(dev);        cs8900_write(dev, PP_EEPROMCommand, (EEWriteDisable));	cs8900_eeprom_wait(dev);        return 0;}/* * 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);}static void eepromdump( struct net_device *dev){	u16 buf[0x80];	u16 i;	int count;	int total;	if( cs8900_read( dev, PP_SelfST) & EEPROMpresent)	{		printk (KERN_INFO "%s: EEPROM present\n",dev->name);	}	else	{		printk (KERN_INFO "%s: NO EEPROM present\n",dev->name);		return;	}	if( cs8900_read( dev, PP_SelfST) & EEPROMOK)	{		printk (KERN_INFO "%s: EEPROM OK\n",dev->name);	}	else	{		printk (KERN_INFO "%s: EEPROM checksum mismatch - fixing...\n",dev->name);	}	printk (KERN_INFO "%s: Hexdump\n",dev->name);	for( i=0; i<0x80; i++)	{		cs8900_eeprom_read( dev, &buf[i], i);	}	hexdump( dev, buf, 0x100);	if( buf[0] & 0x0100)	{		printk (KERN_INFO "%s: non-sequential EEPROM\n",dev->name);	}	else	{		printk (KERN_INFO "%s: sequential EEPROM\n",dev->name);	}	if( (buf[0] & 0xe000) == 0xa000)	{		printk (KERN_INFO "%s: Found reset configuration block\n",dev->name);	}	else	{		printk (KERN_INFO "%s: Reset configuration block not found\n",dev->name);		return;	}	count = 2;	total = buf[0] & 0xff;	printk (KERN_INFO "%s: Reset configuration block size = %d bytes\n",dev->name, total);	while( count < total)	{		int groupsize = (buf[count/2] >> 12) + 1;		int basereg = (buf[count/2] &0x1ff);		printk (KERN_INFO "%s: Group size = %d words\n",dev->name, groupsize);		printk (KERN_INFO "%s:  Base register = %x\n",dev->name, basereg);		count += (groupsize + 1)*2;	}}#endif	/* #ifdef DEBUG *//* * Driver functions */static void cs8900_receive (struct net_device *dev){	cs8900_t *priv = (cs8900_t *) dev->priv;	struct sk_buff *skb;	u16 status,length;	status = cs8900_read (dev,PP_RxStatus);	length = cs8900_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);	cs8900_frame_read (dev,skb,length);#ifdef FULL_DUPLEX	dump_packet (dev,skb,"recv");#endif	/* #ifdef FULL_DUPLEX */	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 cs8900_send_start (struct sk_buff *skb,struct net_device *dev){	cs8900_t *priv = (cs8900_t *) dev->priv;	u16 status;	spin_lock_irq(&priv->lock);	netif_stop_queue (dev);	cs8900_write (dev,PP_TxCMD,TxStart (After5));	cs8900_write (dev,PP_TxLength,skb->len);	status = cs8900_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);	}	cs8900_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 cs8900_interrupt (int irq,void *id,struct pt_regs *regs){	struct net_device *dev = (struct net_device *) id;	cs8900_t *priv;	volatile u16 status;	irqreturn_t handled = 0;			if (dev->priv == NULL) {		printk (KERN_WARNING "%s: irq %d for unknown device.\n",dev->name,irq);		return 0;	}	priv = (cs8900_t *) dev->priv;		while ((status = cs8900_read (dev, PP_ISQ))) {		handled = 1;		switch (RegNum (status)) {		case RxEvent:			cs8900_receive (dev);			break;		case TxEvent:			priv->stats.collisions += ColCount (cs8900_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;

⌨️ 快捷键说明

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