| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Linux ARCnet driver - "cap mode" packet encapsulation. | 
 | 3 |  * It adds sequence numbers to packets for communicating between a user space | 
 | 4 |  * application and the driver. After a transmit it sends a packet with protocol | 
 | 5 |  * byte 0 back up to the userspace containing the sequence number of the packet | 
 | 6 |  * plus the transmit-status on the ArcNet. | 
 | 7 |  * | 
 | 8 |  * Written 2002-4 by Esben Nielsen, Vestas Wind Systems A/S | 
 | 9 |  * Derived from arc-rawmode.c by Avery Pennarun. | 
 | 10 |  * arc-rawmode was in turned based on skeleton.c, see below. | 
 | 11 |  * | 
 | 12 |  * ********************** | 
 | 13 |  * | 
 | 14 |  * The original copyright of skeleton.c was as follows: | 
 | 15 |  * | 
 | 16 |  * skeleton.c Written 1993 by Donald Becker. | 
 | 17 |  * Copyright 1993 United States Government as represented by the | 
 | 18 |  * Director, National Security Agency.  This software may only be used | 
 | 19 |  * and distributed according to the terms of the GNU General Public License as | 
 | 20 |  * modified by SRC, incorporated herein by reference. | 
 | 21 |  * | 
 | 22 |  * ********************** | 
 | 23 |  * | 
 | 24 |  * For more details, see drivers/net/arcnet.c | 
 | 25 |  * | 
 | 26 |  * ********************** | 
 | 27 |  */ | 
 | 28 |  | 
 | 29 | #include <linux/module.h> | 
 | 30 | #include <linux/init.h> | 
 | 31 | #include <linux/if_arp.h> | 
 | 32 | #include <net/arp.h> | 
 | 33 | #include <linux/netdevice.h> | 
 | 34 | #include <linux/skbuff.h> | 
 | 35 | #include <linux/arcdevice.h> | 
 | 36 |  | 
 | 37 | #define VERSION "arcnet: cap mode (`c') encapsulation support loaded.\n" | 
 | 38 |  | 
 | 39 |  | 
 | 40 | static void rx(struct net_device *dev, int bufnum, | 
 | 41 | 	       struct archdr *pkthdr, int length); | 
 | 42 | static int build_header(struct sk_buff *skb, | 
 | 43 | 			struct net_device *dev, | 
 | 44 | 			unsigned short type, | 
 | 45 | 			uint8_t daddr); | 
 | 46 | static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length, | 
 | 47 | 		      int bufnum); | 
 | 48 | static int ack_tx(struct net_device *dev, int acked); | 
 | 49 |  | 
 | 50 |  | 
| Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 51 | static struct ArcProto capmode_proto = | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { | 
 | 53 | 	'r', | 
 | 54 | 	XMTU, | 
 | 55 | 	0, | 
 | 56 |        	rx, | 
 | 57 | 	build_header, | 
 | 58 | 	prepare_tx, | 
 | 59 | 	NULL, | 
 | 60 | 	ack_tx | 
 | 61 | }; | 
 | 62 |  | 
 | 63 |  | 
 | 64 | void arcnet_cap_init(void) | 
 | 65 | { | 
 | 66 | 	int count; | 
 | 67 |  | 
 | 68 | 	for (count = 1; count <= 8; count++) | 
 | 69 | 		if (arc_proto_map[count] == arc_proto_default) | 
 | 70 | 			arc_proto_map[count] = &capmode_proto; | 
 | 71 |  | 
 | 72 | 	/* for cap mode, we only set the bcast proto if there's no better one */ | 
 | 73 | 	if (arc_bcast_proto == arc_proto_default) | 
 | 74 | 		arc_bcast_proto = &capmode_proto; | 
 | 75 |  | 
 | 76 | 	arc_proto_default = &capmode_proto; | 
 | 77 | 	arc_raw_proto = &capmode_proto; | 
 | 78 | } | 
 | 79 |  | 
 | 80 |  | 
 | 81 | #ifdef MODULE | 
 | 82 |  | 
 | 83 | int __init init_module(void) | 
 | 84 | { | 
 | 85 | 	printk(VERSION); | 
 | 86 | 	arcnet_cap_init(); | 
 | 87 | 	return 0; | 
 | 88 | } | 
 | 89 |  | 
 | 90 | void cleanup_module(void) | 
 | 91 | { | 
 | 92 | 	arcnet_unregister_proto(&capmode_proto); | 
 | 93 | } | 
 | 94 |  | 
 | 95 | MODULE_LICENSE("GPL"); | 
 | 96 | #endif				/* MODULE */ | 
 | 97 |  | 
 | 98 |  | 
 | 99 |  | 
 | 100 | /* packet receiver */ | 
 | 101 | static void rx(struct net_device *dev, int bufnum, | 
 | 102 | 	       struct archdr *pkthdr, int length) | 
 | 103 | { | 
 | 104 | 	struct arcnet_local *lp = (struct arcnet_local *) dev->priv; | 
 | 105 | 	struct sk_buff *skb; | 
 | 106 | 	struct archdr *pkt = pkthdr; | 
 | 107 | 	char *pktbuf, *pkthdrbuf; | 
 | 108 | 	int ofs; | 
 | 109 |  | 
 | 110 | 	BUGMSG(D_DURING, "it's a raw(cap) packet (length=%d)\n", length); | 
 | 111 |  | 
 | 112 | 	if (length >= MinTU) | 
 | 113 | 		ofs = 512 - length; | 
 | 114 | 	else | 
 | 115 | 		ofs = 256 - length; | 
 | 116 |  | 
 | 117 | 	skb = alloc_skb(length + ARC_HDR_SIZE + sizeof(int), GFP_ATOMIC); | 
 | 118 | 	if (skb == NULL) { | 
 | 119 | 		BUGMSG(D_NORMAL, "Memory squeeze, dropping packet.\n"); | 
 | 120 | 		lp->stats.rx_dropped++; | 
 | 121 | 		return; | 
 | 122 | 	} | 
 | 123 | 	skb_put(skb, length + ARC_HDR_SIZE + sizeof(int)); | 
 | 124 | 	skb->dev = dev; | 
 | 125 |  | 
 | 126 | 	pkt = (struct archdr *) skb->data; | 
 | 127 |  | 
 | 128 | 	skb->mac.raw = skb->data; | 
 | 129 | 	skb_pull(skb, ARC_HDR_SIZE); | 
 | 130 |  | 
 | 131 | 	/* up to sizeof(pkt->soft) has already been copied from the card */ | 
 | 132 | 	/* squeeze in an int for the cap encapsulation */ | 
 | 133 |  | 
 | 134 | 	/* use these variables to be sure we count in bytes, not in | 
 | 135 | 	   sizeof(struct archdr) */ | 
 | 136 | 	pktbuf=(char*)pkt; | 
 | 137 | 	pkthdrbuf=(char*)pkthdr; | 
 | 138 | 	memcpy(pktbuf, pkthdrbuf, ARC_HDR_SIZE+sizeof(pkt->soft.cap.proto)); | 
 | 139 | 	memcpy(pktbuf+ARC_HDR_SIZE+sizeof(pkt->soft.cap.proto)+sizeof(int), | 
 | 140 | 	       pkthdrbuf+ARC_HDR_SIZE+sizeof(pkt->soft.cap.proto), | 
 | 141 | 	       sizeof(struct archdr)-ARC_HDR_SIZE-sizeof(pkt->soft.cap.proto)); | 
 | 142 |  | 
 | 143 | 	if (length > sizeof(pkt->soft)) | 
 | 144 | 		lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft), | 
 | 145 | 				      pkt->soft.raw + sizeof(pkt->soft) | 
 | 146 | 				      + sizeof(int), | 
 | 147 | 				      length - sizeof(pkt->soft)); | 
 | 148 |  | 
 | 149 | 	BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "rx"); | 
 | 150 |  | 
 | 151 | 	skb->protocol = __constant_htons(ETH_P_ARCNET); | 
 | 152 | ; | 
 | 153 | 	netif_rx(skb); | 
 | 154 | 	dev->last_rx = jiffies; | 
 | 155 | } | 
 | 156 |  | 
 | 157 |  | 
 | 158 | /* | 
 | 159 |  * Create the ARCnet hard/soft headers for cap mode. | 
 | 160 |  * There aren't any soft headers in cap mode - not even the protocol id. | 
 | 161 |  */ | 
 | 162 | static int build_header(struct sk_buff *skb, | 
 | 163 | 			struct net_device *dev, | 
 | 164 | 			unsigned short type, | 
 | 165 | 			uint8_t daddr) | 
 | 166 | { | 
 | 167 | 	int hdr_size = ARC_HDR_SIZE; | 
 | 168 | 	struct archdr *pkt = (struct archdr *) skb_push(skb, hdr_size); | 
 | 169 |  | 
 | 170 | 	BUGMSG(D_PROTO, "Preparing header for cap packet %x.\n", | 
 | 171 | 	       *((int*)&pkt->soft.cap.cookie[0])); | 
 | 172 | 	/* | 
 | 173 | 	 * Set the source hardware address. | 
 | 174 | 	 * | 
 | 175 | 	 * This is pretty pointless for most purposes, but it can help in | 
 | 176 | 	 * debugging.  ARCnet does not allow us to change the source address in | 
 | 177 | 	 * the actual packet sent) | 
 | 178 | 	 */ | 
 | 179 | 	pkt->hard.source = *dev->dev_addr; | 
 | 180 |  | 
 | 181 | 	/* see linux/net/ethernet/eth.c to see where I got the following */ | 
 | 182 |  | 
 | 183 | 	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) { | 
 | 184 | 		/* | 
 | 185 | 		 * FIXME: fill in the last byte of the dest ipaddr here to better | 
 | 186 | 		 * comply with RFC1051 in "noarp" mode. | 
 | 187 | 		 */ | 
 | 188 | 		pkt->hard.dest = 0; | 
 | 189 | 		return hdr_size; | 
 | 190 | 	} | 
 | 191 | 	/* otherwise, just fill it in and go! */ | 
 | 192 | 	pkt->hard.dest = daddr; | 
 | 193 |  | 
 | 194 | 	return hdr_size;	/* success */ | 
 | 195 | } | 
 | 196 |  | 
 | 197 |  | 
 | 198 | static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length, | 
 | 199 | 		      int bufnum) | 
 | 200 | { | 
 | 201 | 	struct arcnet_local *lp = (struct arcnet_local *) dev->priv; | 
 | 202 | 	struct arc_hardware *hard = &pkt->hard; | 
 | 203 | 	int ofs; | 
 | 204 |  | 
 | 205 |  | 
 | 206 | 	/* hard header is not included in packet length */ | 
 | 207 | 	length -= ARC_HDR_SIZE; | 
 | 208 | 	/* And neither is the cookie field */ | 
 | 209 | 	length -= sizeof(int); | 
 | 210 |  | 
 | 211 | 	BUGMSG(D_DURING, "prepare_tx: txbufs=%d/%d/%d\n", | 
 | 212 | 	       lp->next_tx, lp->cur_tx, bufnum); | 
 | 213 |  | 
 | 214 | 	BUGMSG(D_PROTO, "Sending for cap packet %x.\n", | 
 | 215 | 	       *((int*)&pkt->soft.cap.cookie[0])); | 
 | 216 |  | 
 | 217 | 	if (length > XMTU) { | 
 | 218 | 		/* should never happen! other people already check for this. */ | 
 | 219 | 		BUGMSG(D_NORMAL, "Bug!  prepare_tx with size %d (> %d)\n", | 
 | 220 | 		       length, XMTU); | 
 | 221 | 		length = XMTU; | 
 | 222 | 	} | 
 | 223 | 	if (length > MinTU) { | 
 | 224 | 		hard->offset[0] = 0; | 
 | 225 | 		hard->offset[1] = ofs = 512 - length; | 
 | 226 | 	} else if (length > MTU) { | 
 | 227 | 		hard->offset[0] = 0; | 
 | 228 | 		hard->offset[1] = ofs = 512 - length - 3; | 
 | 229 | 	} else | 
 | 230 | 		hard->offset[0] = ofs = 256 - length; | 
 | 231 |  | 
 | 232 | 	BUGMSG(D_DURING, "prepare_tx: length=%d ofs=%d\n", | 
 | 233 | 	       length,ofs); | 
 | 234 |  | 
 | 235 | 	// Copy the arcnet-header + the protocol byte down: | 
 | 236 | 	lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE); | 
 | 237 | 	lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft.cap.proto, | 
 | 238 | 			    sizeof(pkt->soft.cap.proto)); | 
 | 239 |  | 
 | 240 | 	// Skip the extra integer we have written into it as a cookie | 
 | 241 | 	// but write the rest of the message: | 
 | 242 | 	lp->hw.copy_to_card(dev, bufnum, ofs+1, | 
 | 243 | 			    ((unsigned char*)&pkt->soft.cap.mes),length-1); | 
 | 244 |  | 
 | 245 | 	lp->lastload_dest = hard->dest; | 
 | 246 |  | 
 | 247 | 	return 1;		/* done */ | 
 | 248 | } | 
 | 249 |  | 
 | 250 |  | 
 | 251 | static int ack_tx(struct net_device *dev, int acked) | 
 | 252 | { | 
 | 253 |   struct arcnet_local *lp = (struct arcnet_local *) dev->priv; | 
 | 254 |   struct sk_buff *ackskb; | 
 | 255 |   struct archdr *ackpkt; | 
 | 256 |   int length=sizeof(struct arc_cap); | 
 | 257 |  | 
 | 258 |   BUGMSG(D_DURING, "capmode: ack_tx: protocol: %x: result: %d\n", | 
 | 259 | 	 lp->outgoing.skb->protocol, acked); | 
 | 260 |  | 
 | 261 |   BUGLVL(D_SKB) arcnet_dump_skb(dev, lp->outgoing.skb, "ack_tx"); | 
 | 262 |  | 
 | 263 |   /* Now alloc a skb to send back up through the layers: */ | 
 | 264 |   ackskb = alloc_skb(length + ARC_HDR_SIZE , GFP_ATOMIC); | 
 | 265 |   if (ackskb == NULL) { | 
 | 266 | 	  BUGMSG(D_NORMAL, "Memory squeeze, can't acknowledge.\n"); | 
 | 267 | 	  goto free_outskb; | 
 | 268 |   } | 
 | 269 |  | 
 | 270 |   skb_put(ackskb, length + ARC_HDR_SIZE ); | 
 | 271 |   ackskb->dev = dev; | 
 | 272 |  | 
 | 273 |   ackpkt = (struct archdr *) ackskb->data; | 
 | 274 |  | 
 | 275 |   ackskb->mac.raw = ackskb->data; | 
 | 276 |   /* skb_pull(ackskb, ARC_HDR_SIZE); */ | 
 | 277 |  | 
 | 278 |  | 
 | 279 |   memcpy(ackpkt, lp->outgoing.skb->data, ARC_HDR_SIZE+sizeof(struct arc_cap)); | 
 | 280 |   ackpkt->soft.cap.proto=0; /* using protocol 0 for acknowledge */ | 
 | 281 |   ackpkt->soft.cap.mes.ack=acked; | 
 | 282 |  | 
 | 283 |   BUGMSG(D_PROTO, "Ackknowledge for cap packet %x.\n", | 
 | 284 | 	 *((int*)&ackpkt->soft.cap.cookie[0])); | 
 | 285 |  | 
 | 286 |   ackskb->protocol = __constant_htons(ETH_P_ARCNET); | 
 | 287 |  | 
 | 288 |   BUGLVL(D_SKB) arcnet_dump_skb(dev, ackskb, "ack_tx_recv"); | 
 | 289 |   netif_rx(ackskb); | 
 | 290 |  | 
 | 291 |  free_outskb: | 
 | 292 |   dev_kfree_skb_irq(lp->outgoing.skb); | 
 | 293 |   lp->outgoing.proto = NULL; /* We are always finished when in this protocol */ | 
 | 294 |  | 
 | 295 |   return 0; | 
 | 296 | } |