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

📄 catc.c

📁 基于S3CEB2410平台LINUX操作系统下 USB驱动源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  Copyright (c) 2001 Vojtech Pavlik * *  CATC EL1210A NetMate USB Ethernet driver * *  Sponsored by SuSE * *  Based on the work of *		Donald Becker *//* * 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 *  * Should you need to contact me, the author, you can do so either by * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail: * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic */#include <linux/init.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/string.h>#include <linux/slab.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>#include <linux/spinlock.h>#include <asm/bitops.h>#undef DEBUG#include <linux/usb.h>/* * Version information. */#define DRIVER_VERSION "v2.7"#define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@suse.cz>"#define DRIVER_DESC "CATC EL1210A NetMate USB Ethernet driver"MODULE_AUTHOR(DRIVER_AUTHOR);MODULE_DESCRIPTION(DRIVER_DESC);MODULE_LICENSE("GPL");/* * Some defines. */ #define STATS_UPDATE		(HZ)	/* Time between stats updates */#define TX_TIMEOUT		(5*HZ)	/* Max time the queue can be stopped */#define PKT_SZ			1536	/* Max Ethernet packet size */#define RX_MAX_BURST		15	/* Max packets per rx buffer (> 0, < 16) */#define TX_MAX_BURST		15	/* Max full sized packets per tx buffer (> 0) */#define CTRL_QUEUE		16	/* Max control requests in flight (power of two) *//* * Control requests. */enum control_requests {	ReadMem =	0xf1,	GetMac =	0xf2,	Reset =		0xf4,	SetMac =	0xf5,	WriteROM =	0xf8,	SetReg =	0xfa,	GetReg =	0xfb,	WriteMem =	0xfc,	ReadROM =	0xfd,};/* * Registers. */enum register_offsets {	TxBufCount =	0x20,	RxBufCount =	0x21,	OpModes =	0x22,	TxQed =		0x23,	RxQed =		0x24,	MaxBurst =	0x25,	RxUnit =	0x60,	EthStatus =	0x61,	StationAddr0 =	0x67,	EthStats =	0x69,	LEDCtrl =	0x81,};enum eth_stats {	TxSingleColl =	0x00,        TxMultiColl =	0x02,        TxExcessColl =	0x04,        RxFramErr =	0x06,};enum op_mode_bits {	Op3MemWaits =	0x03,	OpLenInclude =	0x08,	OpRxMerge =	0x10,	OpTxMerge =	0x20,	OpWin95bugfix =	0x40,	OpLoopback =	0x80,};enum rx_filter_bits {	RxEnable =	0x01,	RxPolarity =	0x02,	RxForceOK =	0x04,	RxMultiCast =	0x08,	RxPromisc =	0x10,};enum led_values {	LEDFast = 	0x01,	LEDSlow =	0x02,	LEDFlash =	0x03,	LEDPulse =	0x04,	LEDLink =	0x08,};/* * The catc struct. */#define CTRL_RUNNING	0#define RX_RUNNING	1#define TX_RUNNING	2struct catc {	struct net_device *netdev;	struct usb_device *usbdev;	struct net_device_stats stats;	unsigned long flags;	unsigned int tx_ptr, tx_idx;	unsigned int ctrl_head, ctrl_tail;	spinlock_t tx_lock, ctrl_lock;	u8 tx_buf[2][TX_MAX_BURST * (PKT_SZ + 2)];	u8 rx_buf[RX_MAX_BURST * (PKT_SZ + 2)];	u8 irq_buf[2];	u8 ctrl_buf[64];	devrequest ctrl_dr;	struct timer_list timer;	u8 stats_buf[8];	u16 stats_vals[4];	unsigned long last_stats;	u8 multicast[64];	struct ctrl_queue {		u8 dir;		u8 request;		u16 value;		u16 index;		void *buf;		int len;		void (*callback)(struct catc *catc, struct ctrl_queue *q);	} ctrl_queue[CTRL_QUEUE];	struct urb tx_urb, rx_urb, irq_urb, ctrl_urb;};/* * Useful macros. */#define catc_get_mac(catc, mac)				catc_ctrl_msg(catc, USB_DIR_IN,  GetMac, 0, 0, mac,  6)#define catc_reset(catc)				catc_ctrl_msg(catc, USB_DIR_OUT, Reset, 0, 0, NULL, 0)#define catc_set_reg(catc, reg, val)			catc_ctrl_msg(catc, USB_DIR_OUT, SetReg, val, reg, NULL, 0)#define catc_get_reg(catc, reg, buf)			catc_ctrl_msg(catc, USB_DIR_IN,  GetReg, 0, reg, buf, 1)#define catc_write_mem(catc, addr, buf, size)		catc_ctrl_msg(catc, USB_DIR_OUT, WriteMem, 0, addr, buf, size)#define catc_read_mem(catc, addr, buf, size)		catc_ctrl_msg(catc, USB_DIR_IN,  ReadMem, 0, addr, buf, size)#define catc_set_reg_async(catc, reg, val)		catc_ctrl_async(catc, USB_DIR_OUT, SetReg, val, reg, NULL, 0, NULL)#define catc_get_reg_async(catc, reg, cb)		catc_ctrl_async(catc, USB_DIR_IN, GetReg, 0, reg, NULL, 1, cb)#define catc_write_mem_async(catc, addr, buf, size)	catc_ctrl_async(catc, USB_DIR_OUT, WriteMem, 0, addr, buf, size, NULL)/* * Receive routines. */static void catc_rx_done(struct urb *urb){	struct catc *catc = urb->context;	u8 *pkt_start = urb->transfer_buffer;	struct sk_buff *skb;	int pkt_len;	clear_bit(RX_RUNNING, &catc->flags);	if (urb->status) {		dbg("rx_done, status %d, length %d", urb->status, urb->actual_length);		return;	}	do {		pkt_len = le16_to_cpup((u16*)pkt_start);		if (pkt_len > urb->actual_length) {			catc->stats.rx_length_errors++;			catc->stats.rx_errors++;			break;		}		if (!(skb = dev_alloc_skb(pkt_len)))			return;		skb->dev = catc->netdev;		eth_copy_and_sum(skb, pkt_start + 2, pkt_len, 0);		skb_put(skb, pkt_len);		skb->protocol = eth_type_trans(skb, catc->netdev);		netif_rx(skb);		catc->stats.rx_packets++;		catc->stats.rx_bytes += pkt_len;		pkt_start += (((pkt_len + 1) >> 6) + 1) << 6;	} while (pkt_start - (u8 *) urb->transfer_buffer < urb->actual_length);	catc->netdev->last_rx = jiffies;}static void catc_irq_done(struct urb *urb){	struct catc *catc = urb->context;	u8 *data = urb->transfer_buffer;	int status;	if (urb->status) {		dbg("irq_done, status %d, data %02x %02x.", urb->status, data[0], data[1]);		return;	}	if ((data[1] & 0x80) && !test_and_set_bit(RX_RUNNING, &catc->flags)) {		catc->rx_urb.dev = catc->usbdev;		if ((status = usb_submit_urb(&catc->rx_urb)) < 0) {			err("submit(rx_urb) status %d", status);			return;		} 	}	if (data[1] & 0x40)		dbg("link ok");	if (data[1] & 0x20) 		dbg("link bad");}/* * Transmit routines. */static void catc_tx_run(struct catc *catc){	int status;	catc->tx_urb.transfer_buffer_length = catc->tx_ptr;	catc->tx_urb.transfer_buffer = catc->tx_buf[catc->tx_idx];	catc->tx_urb.dev = catc->usbdev;	if ((status = usb_submit_urb(&catc->tx_urb)) < 0)		err("submit(tx_urb), status %d", status);	catc->tx_idx = !catc->tx_idx;	catc->tx_ptr = 0;	catc->netdev->trans_start = jiffies;}static void catc_tx_done(struct urb *urb){	struct catc *catc = urb->context;	unsigned long flags;	if (urb->status == -ECONNRESET) {		dbg("Tx Reset.");		urb->transfer_flags &= ~USB_ASYNC_UNLINK;		urb->status = 0;		catc->netdev->trans_start = jiffies;		catc->stats.tx_errors++;		clear_bit(TX_RUNNING, &catc->flags);		netif_wake_queue(catc->netdev);		return;	}	if (urb->status) {		dbg("tx_done, status %d, length %d", urb->status, urb->actual_length);		return;	}	spin_lock_irqsave(&catc->tx_lock, flags);	if (catc->tx_ptr)		catc_tx_run(catc);	else		clear_bit(TX_RUNNING, &catc->flags);	netif_wake_queue(catc->netdev);	spin_unlock_irqrestore(&catc->tx_lock, flags);}static int catc_hard_start_xmit(struct sk_buff *skb, struct net_device *netdev){	struct catc *catc = netdev->priv;	unsigned long flags;	char *tx_buf;	spin_lock_irqsave(&catc->tx_lock, flags);	catc->tx_ptr = (((catc->tx_ptr - 1) >> 6) + 1) << 6;	tx_buf = catc->tx_buf[catc->tx_idx] + catc->tx_ptr;	*((u16*)tx_buf) = cpu_to_le16((u16)skb->len);	memcpy(tx_buf + 2, skb->data, skb->len);	catc->tx_ptr += skb->len + 2;	if (!test_and_set_bit(TX_RUNNING, &catc->flags))		catc_tx_run(catc);	if (catc->tx_ptr >= ((TX_MAX_BURST - 1) * (PKT_SZ + 2)))		netif_stop_queue(netdev);	spin_unlock_irqrestore(&catc->tx_lock, flags);	catc->stats.tx_bytes += skb->len;	catc->stats.tx_packets++;	dev_kfree_skb(skb);	return 0;}static void catc_tx_timeout(struct net_device *netdev){	struct catc *catc = netdev->priv;	warn("Transmit timed out.");	catc->tx_urb.transfer_flags |= USB_ASYNC_UNLINK;	usb_unlink_urb(&catc->tx_urb);}/* * Control messages. */static int catc_ctrl_msg(struct catc *catc, u8 dir, u8 request, u16 value, u16 index, void *buf, int len){        int retval = usb_control_msg(catc->usbdev,		dir ? usb_rcvctrlpipe(catc->usbdev, 0) : usb_sndctrlpipe(catc->usbdev, 0),		 request, 0x40 | dir, value, index, buf, len, HZ);        return retval < 0 ? retval : 0;}static void catc_ctrl_run(struct catc *catc){	struct ctrl_queue *q = catc->ctrl_queue + catc->ctrl_tail;	struct usb_device *usbdev = catc->usbdev;	struct urb *urb = &catc->ctrl_urb;	devrequest *dr = &catc->ctrl_dr;	int status;

⌨️ 快捷键说明

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