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

📄 arcnet.c

📁 内核linux2.4.20,可跟rtlinux3.2打补丁 组成实时linux系统,编译内核
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Linux ARCnet driver - device-independent routines *  * Written 1997 by David Woodhouse. * Written 1994-1999 by Avery Pennarun. * Written 1999-2000 by Martin Mares <mj@ucw.cz>. * Derived from skeleton.c by Donald Becker. * * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com) *  for sponsoring the further development of this driver. * * ********************** * * The original copyright was as follows: * * skeleton.c Written 1993 by Donald Becker. * Copyright 1993 United States Government as represented by the * Director, National Security Agency.  This software may only be used * and distributed according to the terms of the GNU General Public License as * modified by SRC, incorporated herein by reference. * * ********************** *  * The change log is now in a file called ChangeLog in this directory. * * Sources: *  - Crynwr arcnet.com/arcether.com packet drivers. *  - arcnet.c v0.00 dated 1/1/94 and apparently by  *     Donald Becker - it didn't work :) *  - skeleton.c v0.05 dated 11/16/93 by Donald Becker *     (from Linux Kernel 1.1.45) *  - RFC's 1201 and 1051 - re: TCP/IP over ARCnet *  - The official ARCnet COM9026 data sheets (!) thanks to *     Ken Cornetet <kcornete@nyx10.cs.du.edu> *  - The official ARCnet COM20020 data sheets. *  - Information on some more obscure ARCnet controller chips, thanks *     to the nice people at SMSC. *  - net/inet/eth.c (from kernel 1.1.50) for header-building info. *  - Alternate Linux ARCnet source by V.Shergin <vsher@sao.stavropol.su> *  - Textual information and more alternate source from Joachim Koenig *     <jojo@repas.de> */#define VERSION "arcnet: v3.93 BETA 2000/04/29 - by Avery Pennarun et al.\n"#include <linux/module.h>#include <linux/config.h>#include <linux/types.h>#include <linux/delay.h>#include <linux/netdevice.h>#include <linux/if_arp.h>#include <net/arp.h>#include <linux/init.h>#include <linux/arcdevice.h>/* "do nothing" functions for protocol drivers */static void null_rx(struct net_device *dev, int bufnum,		    struct archdr *pkthdr, int length);static int null_build_header(struct sk_buff *skb, unsigned short type,			     uint8_t daddr);static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,			   int length, int bufnum);/* * one ArcProto per possible proto ID.  None of the elements of * arc_proto_map are allowed to be NULL; they will get set to * arc_proto_default instead.  It also must not be NULL; if you would like * to set it to NULL, set it to &arc_proto_null instead. */struct ArcProto *arc_proto_map[256], *arc_proto_default, *arc_bcast_proto;struct ArcProto arc_proto_null ={	'?',	XMTU,	null_rx,	null_build_header,	null_prepare_tx};/* Exported function prototypes */int arcnet_debug = ARCNET_DEBUG;EXPORT_SYMBOL(arc_proto_map);EXPORT_SYMBOL(arc_proto_default);EXPORT_SYMBOL(arc_bcast_proto);EXPORT_SYMBOL(arc_proto_null);EXPORT_SYMBOL(arcnet_unregister_proto);EXPORT_SYMBOL(arcnet_debug);EXPORT_SYMBOL(arcdev_setup);EXPORT_SYMBOL(arcnet_interrupt);/* Internal function prototypes */static int arcnet_open(struct net_device *dev);static int arcnet_close(struct net_device *dev);static int arcnet_send_packet(struct sk_buff *skb, struct net_device *dev);static void arcnet_timeout(struct net_device *dev);static int arcnet_header(struct sk_buff *skb, struct net_device *dev,			 unsigned short type, void *daddr, void *saddr,			 unsigned len);static int arcnet_rebuild_header(struct sk_buff *skb);static struct net_device_stats *arcnet_get_stats(struct net_device *dev);static int go_tx(struct net_device *dev);void __init arcnet_init(void){	static int arcnet_inited;	int count;	if (arcnet_inited++)		return;	printk(VERSION);#ifdef ALPHA_WARNING	BUGLVL(D_EXTRA) {		printk("arcnet: ***\n"		"arcnet: * Read arcnet.txt for important release notes!\n"		       "arcnet: *\n"		       "arcnet: * This is an ALPHA version! (Last stable release: v3.02)  E-mail\n"		       "arcnet: * me if you have any questions, comments, or bug reports.\n"		       "arcnet: ***\n");	}#endif	/* initialize the protocol map */	arc_proto_default = arc_bcast_proto = &arc_proto_null;	for (count = 0; count < 256; count++)		arc_proto_map[count] = arc_proto_default;	BUGLVL(D_DURING)	    printk("arcnet: struct sizes: %d %d %d %d %d\n",		 sizeof(struct arc_hardware), sizeof(struct arc_rfc1201),		sizeof(struct arc_rfc1051), sizeof(struct arc_eth_encap),		   sizeof(struct archdr));#ifdef CONFIG_ARCNET		/* We're not built as a module */	printk("arcnet: Available protocols:");#ifdef CONFIG_ARCNET_1201	printk(" RFC1201");	arcnet_rfc1201_init();#endif#ifdef CONFIG_ARCNET_1051	printk(" RFC1051");	arcnet_rfc1051_init();#endif#ifdef CONFIG_ARCNET_RAW	printk(" RAW");	arcnet_raw_init();#endif	printk("\n");#ifdef CONFIG_ARCNET_COM90xx	com90xx_probe(NULL);#endif#endif}#ifdef MODULEstatic int debug = ARCNET_DEBUG;MODULE_PARM(debug, "i");int __init init_module(void){	arcnet_debug = debug;	arcnet_init();	return 0;}void cleanup_module(void){}#endif/* * Dump the contents of an sk_buff */#if ARCNET_DEBUG_MAX & D_SKBvoid arcnet_dump_skb(struct net_device *dev, struct sk_buff *skb, char *desc){	int i;	long flags;	save_flags(flags);	cli();	printk(KERN_DEBUG "%6s: skb dump (%s) follows:", dev->name, desc);	for (i = 0; i < skb->len; i++) {		if (i % 16 == 0)			printk("\n" KERN_DEBUG "[%04X] ", i);		printk("%02X ", ((u_char *) skb->data)[i]);	}	printk("\n");	restore_flags(flags);}EXPORT_SYMBOL(arcnet_dump_skb);#endif/* * Dump the contents of an ARCnet buffer */#if (ARCNET_DEBUG_MAX & (D_RX | D_TX))void arcnet_dump_packet(struct net_device *dev, int bufnum, char *desc){	struct arcnet_local *lp = (struct arcnet_local *) dev->priv;	int i, length;	long flags;	static uint8_t buf[512];	save_flags(flags);	cli();	lp->hw.copy_from_card(dev, bufnum, 0, buf, 512);	/* if the offset[0] byte is nonzero, this is a 256-byte packet */	length = (buf[2] ? 256 : 512);	printk(KERN_DEBUG "%6s: packet dump (%s) follows:", dev->name, desc);	for (i = 0; i < length; i++) {		if (i % 16 == 0)			printk("\n" KERN_DEBUG "[%04X] ", i);		printk("%02X ", buf[i]);	}	printk("\n");	restore_flags(flags);}EXPORT_SYMBOL(arcnet_dump_packet);#endif/* * Unregister a protocol driver from the arc_proto_map.  Protocol drivers * are responsible for registering themselves, but the unregister routine * is pretty generic so we'll do it here. */void arcnet_unregister_proto(struct ArcProto *proto){	int count;	if (arc_proto_default == proto)		arc_proto_default = &arc_proto_null;	if (arc_bcast_proto == proto)		arc_bcast_proto = arc_proto_default;	for (count = 0; count < 256; count++) {		if (arc_proto_map[count] == proto)			arc_proto_map[count] = arc_proto_default;	}}/* * Add a buffer to the queue.  Only the interrupt handler is allowed to do * this, unless interrupts are disabled. *  * Note: we don't check for a full queue, since there aren't enough buffers * to more than fill it. */static void release_arcbuf(struct net_device *dev, int bufnum){	struct arcnet_local *lp = (struct arcnet_local *) dev->priv;	int i;	lp->buf_queue[lp->first_free_buf++] = bufnum;	lp->first_free_buf %= 5;	BUGLVL(D_DURING) {		BUGMSG(D_DURING, "release_arcbuf: freed #%d; buffer queue is now: ",		       bufnum);		for (i = lp->next_buf; i != lp->first_free_buf; i = ++i % 5)			BUGMSG2(D_DURING, "#%d ", lp->buf_queue[i]);		BUGMSG2(D_DURING, "\n");	}}/* * Get a buffer from the queue.  If this returns -1, there are no buffers * available. */static int get_arcbuf(struct net_device *dev){	struct arcnet_local *lp = (struct arcnet_local *) dev->priv;	int buf = -1, i;	if (!atomic_dec_and_test(&lp->buf_lock))	/* already in this function */		BUGMSG(D_NORMAL, "get_arcbuf: overlap (%d)!\n", lp->buf_lock.counter);	else {			/* we can continue */		if (lp->next_buf >= 5)			lp->next_buf -= 5;		if (lp->next_buf == lp->first_free_buf)			BUGMSG(D_NORMAL, "get_arcbuf: BUG: no buffers are available??\n");		else {			buf = lp->buf_queue[lp->next_buf++];			lp->next_buf %= 5;		}	}	BUGLVL(D_DURING) {		BUGMSG(D_DURING, "get_arcbuf: got #%d; buffer queue is now: ", buf);		for (i = lp->next_buf; i != lp->first_free_buf; i = ++i % 5)			BUGMSG2(D_DURING, "#%d ", lp->buf_queue[i]);		BUGMSG2(D_DURING, "\n");	}	atomic_inc(&lp->buf_lock);	return buf;}static int choose_mtu(void){	int count, mtu = 65535;	/* choose the smallest MTU of all available encaps */	for (count = 0; count < 256; count++) {		if (arc_proto_map[count] != &arc_proto_null		    && arc_proto_map[count]->mtu < mtu) {			mtu = arc_proto_map[count]->mtu;		}	}	return mtu == 65535 ? XMTU : mtu;}/* Setup a struct device for ARCnet. */void arcdev_setup(struct net_device *dev){	dev->type = ARPHRD_ARCNET;	dev->hard_header_len = sizeof(struct archdr);	dev->mtu = choose_mtu();	dev->addr_len = 1;	dev->tx_queue_len = 30;	dev->broadcast[0] = 0x00;	/* for us, broadcasts are address 0 */	dev->watchdog_timeo = TX_TIMEOUT;	/* New-style flags. */	dev->flags = IFF_BROADCAST;	/*	 * Put in this stuff here, so we don't have to export the symbols to	 * the chipset drivers.	 */	dev->open = arcnet_open;	dev->stop = arcnet_close;	dev->hard_start_xmit = arcnet_send_packet;	dev->tx_timeout = arcnet_timeout;	dev->get_stats = arcnet_get_stats;	dev->hard_header = arcnet_header;	dev->rebuild_header = arcnet_rebuild_header;}/* * Open/initialize the board.  This is called sometime after booting when * the 'ifconfig' program is run. * * This routine should set everything up anew at each open, even registers * that "should" only need to be set once at boot, so that there is * non-reboot way to recover if something goes wrong. */static int arcnet_open(struct net_device *dev){	struct arcnet_local *lp = (struct arcnet_local *) dev->priv;	int count, newmtu;	BUGLVL(D_PROTO) {		int count;		BUGMSG(D_PROTO, "protocol map (default is '%c'): ",		       arc_proto_default->suffix);		for (count = 0; count < 256; count++)			BUGMSG2(D_PROTO, "%c", arc_proto_map[count]->suffix);		BUGMSG2(D_PROTO, "\n");	}	BUGMSG(D_INIT, "arcnet_open: resetting card.\n");	/* try to put the card in a defined state - if it fails the first	 * time, actually reset it.	 */	if (ARCRESET(0) && ARCRESET(1))		return -ENODEV;	newmtu = choose_mtu();	if (newmtu < dev->mtu)		dev->mtu = newmtu;	/* autodetect the encapsulation for each host. */	memset(lp->default_proto, 0, sizeof(lp->default_proto));	/* the broadcast address is special - use the 'bcast' protocol */	for (count = 0; count < 256; count++) {		if (arc_proto_map[count] == arc_bcast_proto) {			lp->default_proto[0] = count;			break;		}	}	/* initialize buffers */	atomic_set(&lp->buf_lock, 1);	lp->next_buf = lp->first_free_buf = 0;	release_arcbuf(dev, 0);	release_arcbuf(dev, 1);	release_arcbuf(dev, 2);	release_arcbuf(dev, 3);	lp->cur_tx = lp->next_tx = -1;	lp->cur_rx = -1;	lp->rfc1201.sequence = 1;	/* bring up the hardware driver */	ARCOPEN(1);	if (dev->dev_addr[0] == 0)		BUGMSG(D_NORMAL, "WARNING!  Station address 00 is reserved "		       "for broadcasts!\n");	else if (dev->dev_addr[0] == 255)		BUGMSG(D_NORMAL, "WARNING!  Station address FF may confuse "		       "DOS networking programs!\n");	if (ASTATUS() & RESETflag)		ACOMMAND(CFLAGScmd | RESETclear);	/* make sure we're ready to receive IRQ's. */	AINTMASK(0);	udelay(1);		/* give it time to set the mask before				 * we reset it again. (may not even be				 * necessary)				 */	lp->intmask = NORXflag | RECONflag;	AINTMASK(lp->intmask);	netif_start_queue(dev);	return 0;}/* The inverse routine to arcnet_open - shuts down the card. */static int arcnet_close(struct net_device *dev){	struct arcnet_local *lp = (struct arcnet_local *) dev->priv;	netif_stop_queue(dev);	/* flush TX and disable RX */	AINTMASK(0);	ACOMMAND(NOTXcmd);	/* stop transmit */	ACOMMAND(NORXcmd);	/* disable receive */	mdelay(1);	/* shut down the card */	ARCOPEN(0);	return 0;}static int arcnet_header(struct sk_buff *skb, struct net_device *dev,			 unsigned short type, void *daddr, void *saddr,			 unsigned len){	struct arcnet_local *lp = (struct arcnet_local *) dev->priv;	uint8_t _daddr, proto_num;	struct ArcProto *proto;	BUGMSG(D_DURING,	    "create header from %d to %d; protocol %d (%Xh); size %u.\n",	       saddr ? *(uint8_t *) saddr : -1,	       daddr ? *(uint8_t *) daddr : -1,	       type, type, len);	if (len != skb->len)		BUGMSG(D_NORMAL, "arcnet_header: Yikes!  skb->len(%d) != len(%d)!\n",		       skb->len, len);	/*	 * if the dest addr isn't provided, we can't choose an encapsulation!	 * Store the packet type (eg. ETH_P_IP) for now, and we'll push on a	 * real header when we do rebuild_header. 	 */	if (!daddr) {		*(uint16_t *) skb_push(skb, 2) = type;		if (skb->nh.raw - skb->mac.raw != 2)			BUGMSG(D_NORMAL, "arcnet_header: Yikes!  diff (%d) is not 2!\n",			       skb->nh.raw - skb->mac.raw);		return -2;	/* return error -- can't transmit yet! */	}	/* otherwise, we can just add the header as usual. */	_daddr = *(uint8_t *) daddr;	proto_num = lp->default_proto[_daddr];	proto = arc_proto_map[proto_num];	BUGMSG(D_DURING, "building header for %02Xh using protocol '%c'\n",	       proto_num, proto->suffix);	if (proto == &arc_proto_null && arc_bcast_proto != proto) {		BUGMSG(D_DURING, "actually, let's use '%c' instead.\n",		       arc_bcast_proto->suffix);

⌨️ 快捷键说明

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