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

📄 dm9601.c

📁 采用DM9601芯片的usb网卡驱动for Linux
💻 C
📖 第 1 页 / 共 2 页
字号:
/***	DM9601: USB 10/100Mbps/HomePNA (1Mbps) Fast Ethernet****	Copyright (c) 1999,2000 Petko Manolov - Petkan (petkan@dce.bg)**	****	ChangeLog:**	v0.01		 Work for DM9601 now.**	v0.02		 rename to dm9601.**	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@davicom.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".**	v1.01   06/01/12 Michael shen <michaelshen@davicom.com.cn>:**			 modify argument of try_module_get and module_put**			 to be THIS_MODULE to pass compiling for kernel 2.6.9*//* * 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/version.h>#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 "dm9601.h"extern int usb_set_configuration(struct usb_device *dev, int configuration);#define	DM9601_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	DM9601_DEV(pn, vid, pid, flags)	\	{name:pn, vendor:vid, device:pid, private:flags},#include "dm9601.h"#undef	DM9601_DEV	{NULL, 0, 0, 0}};static struct usb_device_id dm9601_ids[] = {#define	DM9601_DEV(pn, vid, pid, flags) \	{match_flags: USB_DEVICE_ID_MATCH_DEVICE, idVendor:vid, idProduct:pid},#include "dm9601.h"#undef	DM9601_DEV	{ }};/* For module input parameter */static int mode = DM9601_AUTO, dm9601_mode;static 	int reg5 = DM9601_REG5, reg8 = DM9601_REG8, reg9 = DM9601_REG9, 	rega = DM9601_REGA, nfloor = 0;MODULE_AUTHOR("Petko Manolov <petkan@dce.bg>");MODULE_DESCRIPTION("DAVICOM DM9601 USB Fast Ethernet driver");MODULE_LICENSE("Dual BSD/GPL");/* * USB-status codes: not defined in 2.6 * USB_ST* maps to -E* and should go away in the future */#define USB_ST_NOERROR		0#define USB_ST_CRC		(-EILSEQ)#define USB_ST_BITSTUFF		(-EPROTO)#define USB_ST_NORESPONSE	(-ETIMEDOUT)			/* device not responding/handshaking */#define USB_ST_DATAOVERRUN	(-EOVERFLOW)#define USB_ST_DATAUNDERRUN	(-EREMOTEIO)#define USB_ST_BUFFEROVERRUN	(-ECOMM)#define USB_ST_BUFFERUNDERRUN	(-ENOSR)#define USB_ST_INTERNALERROR	(-EPROTO) 			/* unknown error */#define USB_ST_SHORT_PACKET    	(-EREMOTEIO)#define USB_ST_PARTIAL_ERROR  	(-EXDEV)			/* ISO transfer only partially completed */#define USB_ST_URB_KILLED     	(-ENOENT)			/* URB canceled by user */#define USB_ST_URB_PENDING       (-EINPROGRESS)#define USB_ST_REMOVED		(-ENODEV) 			/* device not existing or removed */#define USB_ST_TIMEOUT		(-ETIMEDOUT)			/* communication timed out, also in urb->status**/#define USB_ST_NOTSUPPORTED	(-ENOSYS)			#define USB_ST_BANDWIDTH_ERROR	(-ENOSPC)			/* too much bandwidth used */#define USB_ST_URB_INVALID_ERROR  (-EINVAL)			/* invalid value/transfer type */#define USB_ST_URB_REQUEST_ERROR  (-ENXIO)			/* invalid endpoint */#define USB_ST_STALL		(-EPIPE) 			/* pipe stalled, also in urb->status*/module_param(mode, int,0644);module_param(reg5,int,0644);module_param(reg8,int,0644);module_param(reg9,int,0644);module_param(rega,int,0644);module_param(nfloor,int,0644);MODULE_PARM_DESC(mode, "Media mode select: 0:10MH 1:100MHF 4:10MF 5:100MF 8:AUTO");MODULE_DEVICE_TABLE (usb, dm9601_ids);#if 0static int write_eprom_word(dm9601_board_info_t *, __u8, __u16);#endifstatic int update_eth_regs_async(dm9601_board_info_t *);/* Aargh!!! I _really_ hate such tweaks */#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)static void ctrl_callback(struct urb *urb, struct pt_regs* regs)#elsestatic void ctrl_callback(struct urb *urb)#endif{	dm9601_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(dm9601_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 = DM9601_REQT_READ;	dbi->dr.bRequest     = DM9601_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;	usb_fill_control_urb( &dbi->ctrl_urb,	                      dbi->usb,	                      usb_rcvctrlpipe(dbi->usb,0),	                      (char *)&dbi->dr,	                      data, 	                      size, 	                      ctrl_callback, 	                      (void *)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 ,GFP_ATOMIC)) ) {		err("%s: BAD CTRLs %d",__FUNCTION__,ret);		goto out;	}	schedule();	remove_wait_queue( &dbi->ctrl_wait, &wait );out:	return	ret;}static int set_registers(dm9601_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 = DM9601_REQT_WRITE;	dbi->dr.bRequest     = DM9601_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;	usb_fill_control_urb(&dbi->ctrl_urb, dbi->usb,			  usb_sndctrlpipe(dbi->usb, 0),			  (char *)&dbi->dr,			  data, size, ctrl_callback, (void *)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,GFP_ATOMIC)) ) {		err("%s: BAD CTRL %d",__FUNCTION__,ret);		return	ret;	}	schedule();	remove_wait_queue( &dbi->ctrl_wait, &wait );	return	ret;}static int set_register( dm9601_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 = DM9601_REQT_WRITE;	dbi->dr.bRequest     = DM9601_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;	usb_fill_control_urb( &dbi->ctrl_urb, dbi->usb,			  usb_sndctrlpipe(dbi->usb,0),			  (char *)&dbi->dr,			  &data, 0, ctrl_callback, (void *)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 ,GFP_ATOMIC)) ) {		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( dm9601_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 = DM9601_REQT_WRITE;		dbi->dr.bRequest     = DM9601_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;		usb_fill_control_urb( &dbi->ctrl_urb, dbi->usb,			  usb_sndctrlpipe(dbi->usb,0),			  (char *)&dbi->dr,			  dbi->hash_table, 8, ctrl_callback, (void *)dbi );	} else if (dbi->flags & RX_CTRL_CHANGE) {		dbi->flags &= ~RX_CTRL_CHANGE;		dbi->flags |= RX_CTRL_CHANGED;		dbi->dr.bRequestType = DM9601_REQT_WRITE;		dbi->dr.bRequest     = DM9601_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;		usb_fill_control_urb( &dbi->ctrl_urb, dbi->usb,			  usb_sndctrlpipe(dbi->usb,0),			  (char *)&dbi->dr,			  &dbi->rx_ctrl_reg, 0, ctrl_callback, (void *)dbi );	} else {		dbi->flags &= ~NET_CTRL_CHANGE;		dbi->flags |= NET_CTRL_CHANGED;		dbi->dr.bRequestType = DM9601_REQT_WRITE;		dbi->dr.bRequest     = DM9601_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;		usb_fill_control_urb( &dbi->ctrl_urb, dbi->usb,			  usb_sndctrlpipe(dbi->usb,0),			  (char *)&dbi->dr,			  &dbi->net_ctrl_reg, 0, ctrl_callback, (void *)dbi );	}	if ( (ret = usb_submit_urb( &dbi->ctrl_urb ,GFP_KERNEL)) )		err("%s: BAD CTRL %d, flags %x",__FUNCTION__,ret,dbi->flags );	return	ret;}static int read_mii_word( dm9601_board_info_t *dbi, __u8 phy, __u8 index, __u16 *regd ){	set_register( dbi, 0x0c, index | 0x40 );	set_register( dbi, 0x0b, 0x0c );	udelay(100);	set_register( dbi, 0x0b, 0x0 );	get_registers( dbi, 0xd, 2, regd);	return 0;}static int write_mii_word( dm9601_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 );	udelay(100);	set_register( dbi, 0x0b, 0x0 );	return 0;}static int read_eprom_word( dm9601_board_info_t *dbi, __u8 index, __u16 *retdata ){	set_register( dbi, 0x0c, index );	set_register( dbi, 0x0b, 0x4 );	udelay(100);	set_register( dbi, 0x0b, 0x0 );	get_registers( dbi, 0xd, 2, retdata);	return 0;}#if 0static int write_eprom_word( dm9601_board_info_t *dbi, __u8 index, __u16 data ){	set_register(dbi, 0x0c, index);	set_registers(dbi, 0x0d, 2, &data);	set_register(dbi, 0x0b, 0x12);	udelay(100);	set_register(dbi, 0x0b, 0x0);	return 0;}#endif#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)static void read_bulk_callback( struct urb *urb, struct pt_regs* reg )#elsestatic void read_bulk_callback( struct urb *urb)#endif{	dm9601_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 & DM9601_RUNNING) )		return;	if ( !netif_device_present(net) )		return;	if ( dbi->flags & DM9601_RX_BUSY ) {		dbi->stats.rx_errors++;		dbg("DM9601 Rx busy");		return;	}	dbi->flags |= DM9601_RX_BUSY;	switch ( urb->status ) {		case USB_ST_NOERROR:			break;		case USB_ST_NORESPONSE:			dbg( "reset MAC" );			dbi->flags &= ~DM9601_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;		usb_fill_bulk_urb( &dbi->rx_urb, dbi->usb,				usb_rcvbulkpipe(dbi->usb, 1),				dbi->rx_buff2, DM9601_MAX_MTU, 				read_bulk_callback, dbi );	} else {		bufptr = dbi->rx_buff2;		usb_fill_bulk_urb( &dbi->rx_urb, dbi->usb,				usb_rcvbulkpipe(dbi->usb, 1),				dbi->rx_buff, DM9601_MAX_MTU, 				read_bulk_callback, dbi );	}	if ( (res = usb_submit_urb(&dbi->rx_urb,GFP_ATOMIC)) )		warn("%s: failed submint rx_urb %d",__FUNCTION__,res);	dbi->flags &= ~DM9601_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	usb_fill_bulk_urb( &dbi->rx_urb, 	                   dbi->usb,	                   usb_rcvbulkpipe(dbi->usb, 1),	                   dbi->rx_buff, 	                   DM9601_MAX_MTU, 	                   read_bulk_callback, 	                   (void *)dbi );	if ( (res = usb_submit_urb(&dbi->rx_urb,GFP_ATOMIC)) )		warn("%s: failed submint rx_urb %d",__FUNCTION__,res);	dbi->flags &= ~DM9601_RX_BUSY;#endif}#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)static void write_bulk_callback( struct urb *urb, struct pt_regs* reg )#elsestatic void write_bulk_callback( struct urb *urb)#endif{	dm9601_board_info_t *dbi = urb->context;	if ( !dbi || !(dbi->flags & DM9601_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	DM9601_USE_INTR#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)static void intr_callback( struct urb *urb ,struct pt_regs* pt)#elsestatic void intr_callback( struct urb *urb)#endif{	dm9601_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);	}

⌨️ 快捷键说明

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