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

📄 st268.c

📁 USB网卡的LINUX驱动源代码.在ARM MPU上可以运行.
💻 C
📖 第 1 页 / 共 2 页
字号:
/***	ST268: USB 10/100Mbps/HomePNA (1Mbps) Fast Ethernet****	Copyright (c) 1999,2000 Petko Manolov - Petkan (petkan@dce.bg)**	****	ChangeLog:**	v0.01		 Work for ST268 now.**	v0.02		 rename to ST268.**	v0.03		 Debug TX full to cause TX hang problem.**	v0.04		 Support MAC/Hash address**			 REG5 get better RX performance when bit4=0**			 Power-Down PHY when driver close**	v0.05		 Support dynamic reset**			 Support automatically switch IntPHY/EXT MII**			 Support REG_8, REG_9, REG_A for flow control**	V0.06   06/14/01 Dynamic select INT/EXT MII by REG1 bit 4**			 Support Force and Auto mode**	V0.07	06/14/01 Program HPNA chip E3/E4/E5**	V0.08	06/15/01 Enable REG_F4 bit5 to force "INT always return"**	V0.09	06/19/01 Default set REG_0A bit3 to enable BP with DA match**		06/22/01 Modify DM9801 progrmming	**			 E3: R25 = ((R24 + NF) & 0x00ff) | 0xf000**			 E4: R25 = ((R24 + NF) & 0x00ff) | 0xc200**			     R17 = (R17 & 0xfff0) | NF + 3**			 E5: R25 = ((R24 + NF - 3) & 0x00ff) | 0xc200**			     R17 = (R17 & 0xfff0) | NF****      V1.00   03/05/03 Weilun Huang <weilun_huang@SHANTOU.com.tw>:**			 Added semaphore mechanism to solve the problem. **			 While device is being plugged, it makes kernel **			 hang on VIA chipset. Removed the devrequest typedef **			 use "struct usb_ctrlrequest".*//* * 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 */#include <linux/sched.h>#include <linux/slab.h>#include <linux/init.h>#include <linux/delay.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/usb.h>#include <linux/module.h>#include <linux/crc32.h>#include "ST268.h"#define	ST268_USE_INTRstatic const char *version = __FILE__ ": v0.0.6 2001/05/24 (C) 1999-2000 Petko Manolov (petkan@dce.bg)";static struct usb_eth_dev usb_dev_id[] = {#define	ST268_DEV(pn, vid, pid, flags)	\	{name:pn, vendor:vid, device:pid, private:flags},#include "ST268.h"#undef	ST268_DEV	{NULL, 0, 0, 0}};static struct usb_device_id ST268_ids[] = {#define	ST268_DEV(pn, vid, pid, flags) \	{match_flags: USB_DEVICE_ID_MATCH_DEVICE, idVendor:vid, idProduct:pid},#include "ST268.h"#undef	ST268_DEV	{ }};/* For module input parameter */static int mode = ST268_AUTO, ST268_mode;static 	u8 reg5 = ST268_REG5, reg8 = ST268_REG8, reg9 = ST268_REG9, 	rega = ST268_REGA, nfloor = 0;MODULE_AUTHOR("Petko Manolov <petkan@dce.bg>");MODULE_DESCRIPTION("SHANTOU ST268 USB Fast Ethernet driver");MODULE_PARM(mode, "i");MODULE_PARM(reg5, "i");MODULE_PARM(reg8, "i");MODULE_PARM(reg9, "i");MODULE_PARM(rega, "i");MODULE_PARM(nfloor, "i");MODULE_PARM_DESC(mode, "Media mode select: 0:10MH 1:100MHF 4:10MF 5:100MF 8:AUTO");MODULE_DEVICE_TABLE (usb, ST268_ids);static int write_eprom_word(ST268_board_info_t *, __u8, __u16);static int update_eth_regs_async(ST268_board_info_t *);/* Aargh!!! I _really_ hate such tweaks */static void ctrl_callback(struct urb *urb){	ST268_board_info_t	*dbi = urb->context;	if (!dbi)		return;	switch (urb->status & 0xff) {		case USB_ST_NOERROR:		case 0x92:			if (dbi->flags & ALL_REGS_CHANGE) {				update_eth_regs_async(dbi);				return;			}			break;		case USB_ST_URB_PENDING:		case 0x8d:			return;		case USB_ST_URB_KILLED:			break;		default:			warn("%s: status %x",__FUNCTION__, urb->status);	}	dbi->flags &= ~ALL_REGS_CHANGED;		if (dbi->flags & CTRL_URB_SLEEP) {		dbi->flags &= ~CTRL_URB_SLEEP;		wake_up_interruptible(&dbi->ctrl_wait);	}}static int get_registers(ST268_board_info_t *dbi, __u16 indx, __u16 size, void *data){	int	ret;	DECLARE_WAITQUEUE(wait, current);	while ( dbi->flags & ALL_REGS_CHANGED ) {		dbi->flags |= CTRL_URB_SLEEP;		interruptible_sleep_on( &dbi->ctrl_wait );	}		dbi->dr.bRequestType = ST268_REQT_READ;	dbi->dr.bRequest     = ST268_REQ_GET_REGS;	dbi->dr.wValue       = cpu_to_le16 (0);	dbi->dr.wIndex       = cpu_to_le16p(&indx);	dbi->dr.wLength      = cpu_to_le16p(&size);	dbi->ctrl_urb.transfer_buffer_length = size;	FILL_CONTROL_URB( &dbi->ctrl_urb, dbi->usb,			  usb_rcvctrlpipe(dbi->usb,0),			  (char *)&dbi->dr,			  data, size, ctrl_callback, dbi );	add_wait_queue( &dbi->ctrl_wait, &wait );	set_current_state( TASK_INTERRUPTIBLE );	dbi->flags |= CTRL_URB_SLEEP;	if ( (ret = usb_submit_urb( &dbi->ctrl_urb )) ) {		err("%s: BAD CTRLs %d",__FUNCTION__,ret);		goto out;	}	schedule();	remove_wait_queue( &dbi->ctrl_wait, &wait );out:	return	ret;}static int set_registers(ST268_board_info_t *dbi, __u16 indx, __u16 size, void *data){	int	ret;	DECLARE_WAITQUEUE(wait, current);	while (dbi->flags & ALL_REGS_CHANGED) {		dbi->flags |= CTRL_URB_SLEEP ;		interruptible_sleep_on(&dbi->ctrl_wait);	}		dbi->dr.bRequestType = ST268_REQT_WRITE;	dbi->dr.bRequest     = ST268_REQ_SET_REGS;	dbi->dr.wValue       = cpu_to_le16(0);	dbi->dr.wIndex       = cpu_to_le16p(&indx);	dbi->dr.wLength      = cpu_to_le16p(&size);	dbi->ctrl_urb.transfer_buffer_length = size;	FILL_CONTROL_URB(&dbi->ctrl_urb, dbi->usb,			  usb_sndctrlpipe(dbi->usb, 0),			  (char *)&dbi->dr,			  data, size, ctrl_callback, dbi);			  	add_wait_queue(&dbi->ctrl_wait, &wait);	set_current_state(TASK_INTERRUPTIBLE);	dbi->flags |= CTRL_URB_SLEEP;	if ( (ret = usb_submit_urb(&dbi->ctrl_urb)) ) {		err("%s: BAD CTRL %d",__FUNCTION__,ret);		return	ret;	}	schedule();	remove_wait_queue( &dbi->ctrl_wait, &wait );	return	ret;}static int set_register( ST268_board_info_t *dbi, __u16 indx, __u8 data ){	int	ret;	__u16 dat = data;	DECLARE_WAITQUEUE(wait, current);		while ( dbi->flags & ALL_REGS_CHANGED ) {		dbi->flags |= CTRL_URB_SLEEP;		interruptible_sleep_on( &dbi->ctrl_wait );	}		dbi->dr.bRequestType = ST268_REQT_WRITE;	dbi->dr.bRequest     = ST268_REQ_SET_REG;	dbi->dr.wValue 	     = cpu_to_le16p( &dat);	dbi->dr.wIndex 	     = cpu_to_le16p( &indx );	dbi->dr.wLength      = cpu_to_le16( 0 );	dbi->ctrl_urb.transfer_buffer_length = 0;	FILL_CONTROL_URB( &dbi->ctrl_urb, dbi->usb,			  usb_sndctrlpipe(dbi->usb,0),			  (char *)&dbi->dr,			  &data, 0, ctrl_callback, dbi );	add_wait_queue( &dbi->ctrl_wait, &wait );	set_current_state( TASK_INTERRUPTIBLE );	dbi->flags |= CTRL_URB_SLEEP;	if ( (ret = usb_submit_urb( &dbi->ctrl_urb )) ) {		err("%s: BAD CTRL %d",__FUNCTION__,ret);		return	ret;	}	schedule();	remove_wait_queue( &dbi->ctrl_wait, &wait );	return	ret;}static int update_eth_regs_async( ST268_board_info_t *dbi ){	int	ret;	if (dbi->flags & HASH_REGS_CHANGE) {		dbi->flags &= ~HASH_REGS_CHANGE;		dbi->flags |= HASH_REGS_CHANGED;		dbi->dr.bRequestType = ST268_REQT_WRITE;		dbi->dr.bRequest     = ST268_REQ_SET_REGS;		dbi->dr.wValue       = cpu_to_le16(0);		dbi->dr.wIndex       = cpu_to_le16(0x16);		dbi->dr.wLength      = cpu_to_le16(8);		dbi->ctrl_urb.transfer_buffer_length = 8;		FILL_CONTROL_URB( &dbi->ctrl_urb, dbi->usb,			  usb_sndctrlpipe(dbi->usb,0),			  (char *)&dbi->dr,			  dbi->hash_table, 8, ctrl_callback, dbi );	} else if (dbi->flags & RX_CTRL_CHANGE) {		dbi->flags &= ~RX_CTRL_CHANGE;		dbi->flags |= RX_CTRL_CHANGED;		dbi->dr.bRequestType = ST268_REQT_WRITE;		dbi->dr.bRequest     = ST268_REQ_SET_REG;		dbi->dr.wValue       = cpu_to_le16(dbi->rx_ctrl_reg);		dbi->dr.wIndex       = cpu_to_le16(0x5);		dbi->dr.wLength       = cpu_to_le16(0);		dbi->ctrl_urb.transfer_buffer_length = 0;		FILL_CONTROL_URB( &dbi->ctrl_urb, dbi->usb,			  usb_sndctrlpipe(dbi->usb,0),			  (char *)&dbi->dr,			  &dbi->rx_ctrl_reg, 0, ctrl_callback, dbi );	} else {		dbi->flags &= ~NET_CTRL_CHANGE;		dbi->flags |= NET_CTRL_CHANGED;		dbi->dr.bRequestType = ST268_REQT_WRITE;		dbi->dr.bRequest     = ST268_REQ_SET_REG;		dbi->dr.wValue       = cpu_to_le16(dbi->net_ctrl_reg);		dbi->dr.wIndex       =  cpu_to_le16(0x0);		dbi->dr.wLength      = cpu_to_le16(0);		dbi->ctrl_urb.transfer_buffer_length = 0;		FILL_CONTROL_URB( &dbi->ctrl_urb, dbi->usb,			  usb_sndctrlpipe(dbi->usb,0),			  (char *)&dbi->dr,			  &dbi->net_ctrl_reg, 0, ctrl_callback, dbi );	}	if ( (ret = usb_submit_urb( &dbi->ctrl_urb )) )		err("%s: BAD CTRL %d, flags %x",__FUNCTION__,ret,dbi->flags );	return	ret;}static int read_mii_word( ST268_board_info_t *dbi, __u8 phy, __u8 index, __u16 *regd ){	set_register( dbi, 0x0c, index | 0x40 );	set_register( dbi, 0x0b, 0x0c );	wait_ms(100);	set_register( dbi, 0x0b, 0x0 );	get_registers( dbi, 0xd, 2, regd);	return 0;}static int write_mii_word( ST268_board_info_t *dbi, __u8 phy, __u8 index, __u16 regd ){	set_register( dbi, 0x0c, index | 0x40 );	set_registers( dbi, 0xd, 2, &regd);	set_register( dbi, 0x0b, 0x0a );	wait_ms(100);	set_register( dbi, 0x0b, 0x0 );	return 0;}static int read_eprom_word( ST268_board_info_t *dbi, __u8 index, __u16 *retdata ){	set_register( dbi, 0x0c, index );	set_register( dbi, 0x0b, 0x4 );	wait_ms(100);	set_register( dbi, 0x0b, 0x0 );	get_registers( dbi, 0xd, 2, retdata);	return 0;}static int write_eprom_word( ST268_board_info_t *dbi, __u8 index, __u16 data ){	set_register(dbi, 0x0c, index);	set_registers(dbi, 0x0d, 2, &data);	set_register(dbi, 0x0b, 0x12);	wait_ms(100);	set_register(dbi, 0x0b, 0x0);	return 0;}static void read_bulk_callback( struct urb *urb ){	ST268_board_info_t *dbi = urb->context;	struct net_device *net = dbi->net;	int count = urb->actual_length, res;	__u8 rx_status;	struct sk_buff	*skb;	__u16 pkt_len;	unsigned char * bufptr;	if ( !dbi || !(dbi->flags & ST268_RUNNING) )		return;	if ( !netif_device_present(net) )		return;	if ( dbi->flags & ST268_RX_BUSY ) {		dbi->stats.rx_errors++;		dbg("ST268 Rx busy");		return;	}	dbi->flags |= ST268_RX_BUSY;	switch ( urb->status ) {		case USB_ST_NOERROR:			break;		case USB_ST_NORESPONSE:			dbg( "reset MAC" );			dbi->flags &= ~ST268_RX_BUSY;			break;		default:#ifdef RX_IMPROVE			dbg("%s: RX status %d",net->name, urb->status );			goto goon;#endif	}/* For RX improve ---------------------------*/#ifdef RX_IMPROVE	if (dbi->rx_buf_flag) {		bufptr = dbi->rx_buff;		FILL_BULK_URB( &dbi->rx_urb, dbi->usb,				usb_rcvbulkpipe(dbi->usb, 1),				dbi->rx_buff2, ST268_MAX_MTU, 				read_bulk_callback, dbi );	} else {		bufptr = dbi->rx_buff2;		FILL_BULK_URB( &dbi->rx_urb, dbi->usb,				usb_rcvbulkpipe(dbi->usb, 1),				dbi->rx_buff, ST268_MAX_MTU, 				read_bulk_callback, dbi );	}	if ( (res = usb_submit_urb(&dbi->rx_urb)) )		warn("%s: failed submint rx_urb %d",__FUNCTION__,res);	dbi->flags &= ~ST268_RX_BUSY;	dbi->rx_buf_flag = dbi->rx_buf_flag ? 0:1;#else	bufptr = dbi->rx_buff;#endif/* ----------------------------------------------------------*/	if ( !count )		goto goon;	rx_status = *(__u8 *)(bufptr);	pkt_len = *(__u16 *)(bufptr + 1) - 4;	dbi->stats.rx_bytes += pkt_len;	if ( (rx_status & 0xbf) || (pkt_len > 1518) ) {		dbi->stats.rx_errors++;		if (pkt_len > 1518) dbi->rx_longf_errors++;		if (rx_status & 0x80) dbi->rx_runtf_errors++;		if (rx_status & 0x20) dbi->rx_lc_errors++;		if (rx_status & 0x10) dbi->rx_wdt_errors++;		if (rx_status & 0x08) dbi->rx_ple_errors++;		if (rx_status & 0x04) dbi->stats.rx_frame_errors++;		if (rx_status & 0x02) dbi->stats.rx_crc_errors++;		if (rx_status & 0x1) dbi->stats.rx_fifo_errors++;		goto goon;	}	if ( !(skb = dev_alloc_skb(pkt_len + 2)) )		goto goon;	skb->dev = net;	skb_reserve(skb, 2);	memcpy(skb_put(skb, pkt_len), bufptr + 3, pkt_len);	skb->protocol = eth_type_trans(skb, net);	netif_rx(skb);	dbi->stats.rx_packets++;	dbi->stats.rx_bytes += pkt_len;goon:#ifndef RX_IMPROVE	FILL_BULK_URB( &dbi->rx_urb, dbi->usb,			usb_rcvbulkpipe(dbi->usb, 1),			dbi->rx_buff, ST268_MAX_MTU, 			read_bulk_callback, dbi );	if ( (res = usb_submit_urb(&dbi->rx_urb)) )		warn("%s: failed submint rx_urb %d",__FUNCTION__,res);	dbi->flags &= ~ST268_RX_BUSY;#endif}static void write_bulk_callback( struct urb *urb ){	ST268_board_info_t *dbi = urb->context;	if ( !dbi || !(dbi->flags & ST268_RUNNING) )		return;	if ( !netif_device_present(dbi->net) )		return;			if ( urb->status )		info("%s: TX status %d", dbi->net->name, urb->status);	dbi->net->trans_start = jiffies;	netif_wake_queue( dbi->net );}#ifdef	ST268_USE_INTRstatic void intr_callback( struct urb *urb ){	ST268_board_info_t *dbi = urb->context;	struct net_device *net;	__u8	*d;	if ( !dbi )		return;			switch ( urb->status ) {		case USB_ST_NOERROR:			break;		case USB_ST_URB_KILLED:			return;		default:			info("intr status %d", urb->status);	}	d = urb->transfer_buffer;	net = dbi->net;	if ( !(d[6] & 0x04) && (d[0] & 0x10) ) { 		printk("<WARN> TX FULL %x %x\n", d[0], d[6]); 		dbi->flags |= ST268_RESET_WAIT;	}

⌨️ 快捷键说明

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