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

📄 rtl8150.c

📁 linux的网卡实用驱动及应用源码rtl8150
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
**	Pegasus: USB 10/100Mbps/HomePNA (1Mbps) Controller
**
**	Copyright (c) 1999,2000 Petko Manolov - Petkan (petkan@dce.bg)
**	
**
**	ChangeLog:
**		....	Most of the time spend reading sources & docs.
**		v0.2.x	First official release for the Linux kernel.
**		v0.3.0	Beutified and structured, some bugs fixed.
**		v0.3.x	URBifying bulk requests and bugfixing. First relatively
**			stable release. Still can touch device's registers only
**			from top-halves.
**		v0.4.0	Control messages remained unurbified are now URBs.
**			Now we can touch the HW at any time.
**		v0.4.9	Control urbs again use process context to wait. Argh...
**			Some long standing bugs (enable_net_traffic) fixed.
**			Also nasty trick about resubmiting control urb from
**			interrupt context used. Please let me know how it
**			behaves. Pegasus II support added since this version.
**			TODO: suppressing HCD warnings spewage on disconnect.
**		v0.4.13	Ethernet address is now set at probe(), not at open()
**			time as this seems to break dhcpd. 
*/

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

 /*
 **
 **	ChangeLog of Realtek:
 **		1.0.0	First release for RTL8150 USB Fast Ethernet NIC
 **
 */


#include <linux/module.h>
#include <linux/sched.h>
#include <linux/malloc.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/usb.h>


static const char *version = __FILE__ ": v1.0.0 2001/01/31 (C) 2001 Realtek Semiconductor Corp.(shwei@realtek.com.tw)";


/// define this to use interrupt endpoint 3
#define	RTL8150_USE_INTR	


#define	RTL8150_MTU			1500
#define	RTL8150_MAX_MTU			1536
#define RTL8150_MIN_PACKET_LENGTH	60


/// phy registers index
#define MII_PHY_ID1	0x02
#define MII_PHY_ID2	0x03


/// manufacture ID of HPNA PHY 
#define AMD_PHY_ID	0x6b90
#define NS_PHY_ID	0x5c20


/// rtl8150->features
#define	FEATURE_DEFAULT		0x00
#define	FEATURE_HAS_HOME_PNA	0x01


/// rtl8150 states
#define	RTL8150_PRESENT		0x00000001
#define	RTL8150_RUNNING		0x00000002
#define	RTL8150_TX_BUSY		0x00000004
#define	RTL8150_RX_BUSY		0x00000008
#define	RTL8150_UNPLUG		0x00000040


/// ctrl urb
#define	CTRL_URB_RUNNING	0x00000010
#define	CTRL_URB_SLEEP		0x00000020

//#define	ETH_REGS_CHANGE		0x40000000
#define	ETH_REGS_CHANGED	0x80000000

#define	REG_TIMEOUT		(HZ)
#define	RTL8150_TX_TIMEOUT	(HZ*10)


/// vendor memory read & write
#define	RTL8150_REQT_READ	0xc0
#define	RTL8150_REQT_WRITE	0x40
#define	RTL8150_REQ_GET_REG	0x05
#define	RTL8150_REQ_SET_REG	0x05


#define	NUM_CTRL_URBS		0x10
#define	ALIGN(x)		x __attribute__((aligned(L1_CACHE_BYTES)))


#define SIZE32_BIT0		0x00000001
#define SIZE32_BIT1		0x00000002
#define SIZE32_BIT2		0x00000004
#define SIZE32_BIT3		0x00000008
#define SIZE32_BIT4		0x00000010
#define SIZE32_BIT5		0x00000020
#define SIZE32_BIT6		0x00000040
#define SIZE32_BIT7		0x00000080
#define SIZE32_BIT8		0x00000100
#define SIZE32_BIT9		0x00000200
#define SIZE32_BIT10		0x00000400
#define SIZE32_BIT11		0x00000800
#define SIZE32_BIT12		0x00001000
#define SIZE32_BIT13		0x00002000
#define SIZE32_BIT14		0x00004000
#define SIZE32_BIT15		0x00008000
#define SIZE32_BIT16		0x00010000
#define SIZE32_BIT17		0x00020000
#define SIZE32_BIT18		0x00040000
#define SIZE32_BIT19		0x00080000
#define SIZE32_BIT20		0x00100000
#define SIZE32_BIT21		0x00200000
#define SIZE32_BIT22		0x00400000
#define SIZE32_BIT23		0x00800000
#define SIZE32_BIT24		0x01000000
#define SIZE32_BIT25		0x02000000
#define SIZE32_BIT26		0x04000000
#define SIZE32_BIT27		0x08000000
#define SIZE32_BIT28		0x10000000
#define SIZE32_BIT29		0x20000000
#define SIZE32_BIT30		0x40000000
#define SIZE32_BIT31		0x80000000


#define SIZE16_BIT0		0x0001
#define SIZE16_BIT1		0x0002
#define SIZE16_BIT2		0x0004
#define SIZE16_BIT3		0x0008
#define SIZE16_BIT4		0x0010
#define SIZE16_BIT5		0x0020
#define SIZE16_BIT6		0x0040
#define SIZE16_BIT7		0x0080
#define SIZE16_BIT8		0x0100
#define SIZE16_BIT9		0x0200
#define SIZE16_BIT10		0x0400
#define SIZE16_BIT11		0x0800
#define SIZE16_BIT12		0x1000
#define SIZE16_BIT13		0x2000
#define SIZE16_BIT14		0x4000
#define SIZE16_BIT15		0x8000


#define SIZE8_BIT0		0x01
#define SIZE8_BIT1		0x02
#define SIZE8_BIT2		0x04
#define SIZE8_BIT3		0x08
#define SIZE8_BIT4		0x10
#define SIZE8_BIT5		0x20
#define SIZE8_BIT6		0x40
#define SIZE8_BIT7		0x80


/// registers index
#define IDR0			0x0120
#define Command			0x012e
#define TxConfig		0x012f
#define RxConfig		0x0130
#define TxStst			0x0132
#define RxStst			0x0133
#define Config			0x0135
#define Config1			0x0136
#define MediaStst		0x0137
#define MiiPhyAddr		0x0138
#define MiiPhyData		0x0139
#define MiiPhyAccess		0x013b
#define GPPCtrl			0x013d
#define WakeUpEventCtrl		0x013e
#define BasicModeCtrl		0x0140
#define BasicModeStst		0x0142
#define AutoNegoAdver		0x0144
#define AutoNegoLinkPartner	0x0146
#define AutoNegoExpan		0x0148
#define NwayTst			0x014a
#define CSConfig		0x014c


/// which HPNA PHY
enum hpna_phy {
	UNKNOWN_PHY = 0,
	AMD_PHY = 1,
	NS_PHY = 2,
};


/// loopback mode
enum loopback_mode{
	NORMAL_OPERATION = 0,
	DIGITAL_LOOPBACK = 1,
	ANALOG_LOOPBACK = 2,
};


/// EEPROM index
#define		EPROM_IDR0	 	0x1202
#define 	EPROM_Config0   	0x1208
#define	  	EPROM_MSR		0x1209
#define 	EPROM_GPCP		0x120a
#define		EPROM_UDP		0x120b
#define		EPROM_ATTR		0x120c
#define		EPROM_PHY2_PARM		0x120d
#define		EPROM_PHY1_PARM		0x120e
#define		EPROM_TW1_PARM		0x1212
#define		EPROM_MAXPOR		0x1216
#define		EPROM_Interv		0x1217
#define		EPROM_LanguageID	0x1218
#define		EPROM_ManufactureID	0x121a
#define 	EPROM_ProductID		0x121c
#define		EPROM_SerialNumber	0x121e
#define		EPROM_ManufactureString	0x1228
#define		EPROM_ProductString	0x1250


/// Command Register
#define 	WEPROM			SIZE8_BIT5
#define 	SOFT_RST		SIZE8_BIT4
#define		RE			SIZE8_BIT3
#define		TE			SIZE8_BIT2
#define 	EP3CLEAR		SIZE8_BIT1
#define 	AUTOLOAD      		SIZE8_BIT0


/// Transmit Configuration Register
#define		TXRR1			SIZE8_BIT7
#define		TXRR0			SIZE8_BIT6
#define 	IFG1			SIZE8_BIT4
#define		IFG0			SIZE8_BIT3
#define 	LBK1			SIZE8_BIT2
#define 	LBK0			SIZE8_BIT1
#define		NOCRC			SIZE8_BIT0


/// Receive Configuration Register
#define 	EARRX			SIZE16_BIT10
#define		TAIL			SIZE16_BIT7
#define		AER		   	SIZE16_BIT6
#define		AR		    	SIZE16_BIT5
#define		AM		   	SIZE16_BIT4
#define		AB		    	SIZE16_BIT3
#define		AD		    	SIZE16_BIT2
#define		AAM		 	SIZE16_BIT1
#define		AAP		   	SIZE16_BIT0

/// Transmit Status Register
#define 	ECOL			SIZE8_BIT5
#define		LCOL			SIZE8_BIT4
#define 	LOSS_CRS		SIZE8_BIT3
#define		JBR			SIZE8_BIT2
#define 	TX_BUF_EMPTY		SIZE8_BIT1
#define		TX_BUF_FULL		SIZE8_BIT0

/// Receive Status Register
#define		WEVENT			SIZE8_BIT7
#define		RX_BUF_FULL		SIZE8_BIT6
#define		LKCHG			SIZE8_BIT5
#define 	RUNT			SIZE8_BIT4
#define		LONG			SIZE8_BIT3
#define 	CRC			SIZE8_BIT2
#define		FAE			SIZE8_BIT1
#define		ROK			SIZE8_BIT0

/// Configuration Register0
#define		SUSLED			SIZE8_BIT7
#define		PARM_EN			SIZE8_BIT6
#define		LDPS			SIZE8_BIT3
#define		MSEL			SIZE8_BIT2
#define		LEDS1			SIZE8_BIT1
#define 	LEDS0			SIZE8_BIT0

/// Configuration Register1
#define 	BWF			SIZE8_BIT6
#define		MWF			SIZE8_BIT5
#define		UWF			SIZE8_BIT4
#define		LONGWF1			SIZE8_BIT1
#define		LONGWF0			SIZE8_BIT0

/// Media Status Register
#define		TXFCE			SIZE8_BIT7
#define		RXFCE			SIZE8_BIT6
#define		DUPLEX			SIZE8_BIT4
#define		SPEED_100		SIZE8_BIT3
#define 	LINK			SIZE8_BIT2
#define		TXPF			SIZE8_BIT1
#define		RXPF			SIZE8_BIT0


typedef struct rtl8150 {
	struct usb_device	*usb; // store "dev" passed in rtl8150_probe( struct usb_device *dev...)
	struct net_device	*net; // store "net" returned by init_etherdev()
	struct net_device_stats	stats;
	unsigned		flags;
	unsigned		features;
	int			intr_interval;
	struct urb		ctrl_urb, rx_urb, tx_urb, intr_urb;
	devrequest		dr;
	wait_queue_head_t	ctrl_wait;
	struct semaphore	ctrl_sem;
	unsigned char		ALIGN(rx_buff[RTL8150_MAX_MTU]);
	unsigned char		ALIGN(tx_buff[RTL8150_MAX_MTU]);
	unsigned char		ALIGN(intr_buff[8]);
	__u8			eth_regs[4];
	__u8			phy;
	__u8			hpna_phy;
	__u8			gpio_res;
	
	/// added by Owen on 01/11/2000
	__u8			get_registers_flag;
	__u8			set_registers_flag;
} rtl8150_t;


struct usb_eth_dev {
	char	*name;
	__u16	vendor;
	__u16	device;
	__u32	private; /* LSB is gpio reset value */
};


static int loopback = 0;
static int mii_mode = 0;
static int multicast_filter_limit = 32;


MODULE_AUTHOR("shwei@realtek.com.tw");
MODULE_DESCRIPTION("Realtek RTL8150 USB 10/100 Fast Ethernet Driver");
MODULE_PARM(loopback, "i");
MODULE_PARM(mii_mode, "i");
MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0) - default = MII mode = 0");


static struct usb_eth_dev usb_dev_id[] = {
	{"Realtek RTL8150 USB 10/100 Fast Ethernet Adatper", 0x0bda, 0x8150, FEATURE_DEFAULT},
	{"Realtek RTL8150 USB 1Mbps HomePNA", 0x0bda, 0x8151, FEATURE_HAS_HOME_PNA | FEATURE_DEFAULT},
	{NULL, 0, 0, 0}
};

static void set_registers_callback( urb_t *urb )
{
	rtl8150_t	*rtl8150 = urb->context;

	//info( "+set_registers_callback()");

	if ( !rtl8150 )
		return;

	switch ( urb->status ) {
		case USB_ST_NOERROR:
			break;
		case USB_ST_URB_PENDING:
			//info( " set_registers_callback: case USB_ST_URB_PENDING" );
			return;
		case USB_ST_URB_KILLED:
			break;
		default:
			//info( " set_registers_callback: default: status = 0x%x", urb->status );
			warn( __FUNCTION__ " status %d", urb->status);
	}
	rtl8150->flags &= ~ETH_REGS_CHANGED;
	if ( rtl8150->flags & CTRL_URB_SLEEP )
	{
		rtl8150->flags &= ~CTRL_URB_SLEEP;
		/// marked and added by Owen on 01/11/2001
		///wake_up_interruptible( &rtl8150->ctrl_wait );
		rtl8150->set_registers_flag = 0;
	}
	
	//info("-set_registers_callback()");
}


static void get_registers_callback( urb_t *urb )
{
	rtl8150_t	*rtl8150 = urb->context;

	///info( "+get_registers_callback()");

	if ( !rtl8150 )
		return;

	switch ( urb->status ) {
		case USB_ST_NOERROR:
			break;
		case USB_ST_URB_PENDING:
			//info( " get_registers_callback: case USB_ST_URB_PENDING" );
			return;
		case USB_ST_URB_KILLED:
			break;
		default:
			//info( " get_registers_callback: default: status = 0x%x", urb->status );
			warn( __FUNCTION__ " status %d", urb->status);
	}
	rtl8150->flags &= ~ETH_REGS_CHANGED;
	if ( rtl8150->flags & CTRL_URB_SLEEP ) 
	{
		rtl8150->flags &= ~CTRL_URB_SLEEP;
		/// marked and added by Owen on 01/11/2001
		///wake_up_interruptible( &rtl8150->ctrl_wait );
		rtl8150->get_registers_flag = 0;
	}
	
	///info("-get_registers_callback()");
}


static int get_registers(rtl8150_t *rtl8150, __u16 indx, __u16 size, void *data)
{
	int	ret;

	///info( "+get_registers()=====>" );

	rtl8150->dr.requesttype = RTL8150_REQT_READ;
	rtl8150->dr.request = RTL8150_REQ_GET_REG;
	rtl8150->dr.value = cpu_to_le16p(&indx);
	rtl8150->dr.index = 0;
	rtl8150->dr.length = 
	rtl8150->ctrl_urb.transfer_buffer_length = cpu_to_le16p(&size);

	FILL_CONTROL_URB( &rtl8150->ctrl_urb, rtl8150->usb,
			  usb_rcvctrlpipe(rtl8150->usb,0),
			  (char *)&rtl8150->dr,
			  data, size, get_registers_callback, rtl8150 );

	if ( (ret = usb_submit_urb( &rtl8150->ctrl_urb )) ) {
		info( " get_registers: usb_submit_urb failed" );	
		err( __FUNCTION__ " BAD CTRLs %d", ret);
		goto out;
	}
	rtl8150->flags |= CTRL_URB_SLEEP;
	/// marked by Owen on 01/11/2001
	//interruptible_sleep_on( &rtl8150->ctrl_wait );
out:
	///info( "-get_registers()<=====" );
	return	ret;
}


static int set_registers(rtl8150_t *rtl8150, __u16 indx, __u16 size, void *data)
{
	int	ret;

	///info( "+set_registers() =====>" );

	/// marked by Owen on 01/11/2001
	//if ( rtl8150->flags & ETH_REGS_CHANGED ) {
	//	rtl8150->flags |= CTRL_URB_SLEEP ;
	//	interruptible_sleep_on( &rtl8150->ctrl_wait );
	//}

⌨️ 快捷键说明

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