capmode.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/if_arp.h>
  31. #include <net/arp.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/arcdevice.h>
  35. #define VERSION "arcnet: cap mode (`c') encapsulation support loaded.\n"
  36. static void rx(struct net_device *dev, int bufnum,
  37. struct archdr *pkthdr, int length);
  38. static int build_header(struct sk_buff *skb,
  39. struct net_device *dev,
  40. unsigned short type,
  41. uint8_t daddr);
  42. static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
  43. int bufnum);
  44. static int ack_tx(struct net_device *dev, int acked);
  45. static struct ArcProto capmode_proto =
  46. {
  47. 'r',
  48. XMTU,
  49. 0,
  50. rx,
  51. build_header,
  52. prepare_tx,
  53. NULL,
  54. ack_tx
  55. };
  56. void arcnet_cap_init(void)
  57. {
  58. int count;
  59. for (count = 1; count <= 8; count++)
  60. if (arc_proto_map[count] == arc_proto_default)
  61. arc_proto_map[count] = &capmode_proto;
  62. /* for cap mode, we only set the bcast proto if there's no better one */
  63. if (arc_bcast_proto == arc_proto_default)
  64. arc_bcast_proto = &capmode_proto;
  65. arc_proto_default = &capmode_proto;
  66. arc_raw_proto = &capmode_proto;
  67. }
  68. #ifdef MODULE
  69. int __init init_module(void)
  70. {
  71. printk(VERSION);
  72. arcnet_cap_init();
  73. return 0;
  74. }
  75. void cleanup_module(void)
  76. {
  77. arcnet_unregister_proto(&capmode_proto);
  78. }
  79. MODULE_LICENSE("GPL");
  80. #endif /* MODULE */
  81. /* packet receiver */
  82. static void rx(struct net_device *dev, int bufnum,
  83. struct archdr *pkthdr, int length)
  84. {
  85. struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
  86. struct sk_buff *skb;
  87. struct archdr *pkt = pkthdr;
  88. char *pktbuf, *pkthdrbuf;
  89. int ofs;
  90. BUGMSG(D_DURING, "it's a raw(cap) packet (length=%d)\n", length);
  91. if (length >= MinTU)
  92. ofs = 512 - length;
  93. else
  94. ofs = 256 - length;
  95. skb = alloc_skb(length + ARC_HDR_SIZE + sizeof(int), GFP_ATOMIC);
  96. if (skb == NULL) {
  97. BUGMSG(D_NORMAL, "Memory squeeze, dropping packet.\n");
  98. lp->stats.rx_dropped++;
  99. return;
  100. }
  101. skb_put(skb, length + ARC_HDR_SIZE + sizeof(int));
  102. skb->dev = dev;
  103. pkt = (struct archdr *) skb->data;
  104. skb->mac.raw = skb->data;
  105. skb_pull(skb, ARC_HDR_SIZE);
  106. /* up to sizeof(pkt->soft) has already been copied from the card */
  107. /* squeeze in an int for the cap encapsulation */
  108. /* use these variables to be sure we count in bytes, not in
  109. sizeof(struct archdr) */
  110. pktbuf=(char*)pkt;
  111. pkthdrbuf=(char*)pkthdr;
  112. memcpy(pktbuf, pkthdrbuf, ARC_HDR_SIZE+sizeof(pkt->soft.cap.proto));
  113. memcpy(pktbuf+ARC_HDR_SIZE+sizeof(pkt->soft.cap.proto)+sizeof(int),
  114. pkthdrbuf+ARC_HDR_SIZE+sizeof(pkt->soft.cap.proto),
  115. sizeof(struct archdr)-ARC_HDR_SIZE-sizeof(pkt->soft.cap.proto));
  116. if (length > sizeof(pkt->soft))
  117. lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
  118. pkt->soft.raw + sizeof(pkt->soft)
  119. + sizeof(int),
  120. length - sizeof(pkt->soft));
  121. BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "rx");
  122. skb->protocol = __constant_htons(ETH_P_ARCNET);
  123. ;
  124. netif_rx(skb);
  125. dev->last_rx = jiffies;
  126. }
  127. /*
  128. * Create the ARCnet hard/soft headers for cap mode.
  129. * There aren't any soft headers in cap mode - not even the protocol id.
  130. */
  131. static int build_header(struct sk_buff *skb,
  132. struct net_device *dev,
  133. unsigned short type,
  134. uint8_t daddr)
  135. {
  136. int hdr_size = ARC_HDR_SIZE;
  137. struct archdr *pkt = (struct archdr *) skb_push(skb, hdr_size);
  138. BUGMSG(D_PROTO, "Preparing header for cap packet %x.\n",
  139. *((int*)&pkt->soft.cap.cookie[0]));
  140. /*
  141. * Set the source hardware address.
  142. *
  143. * This is pretty pointless for most purposes, but it can help in
  144. * debugging. ARCnet does not allow us to change the source address in
  145. * the actual packet sent)
  146. */
  147. pkt->hard.source = *dev->dev_addr;
  148. /* see linux/net/ethernet/eth.c to see where I got the following */
  149. if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
  150. /*
  151. * FIXME: fill in the last byte of the dest ipaddr here to better
  152. * comply with RFC1051 in "noarp" mode.
  153. */
  154. pkt->hard.dest = 0;
  155. return hdr_size;
  156. }
  157. /* otherwise, just fill it in and go! */
  158. pkt->hard.dest = daddr;
  159. return hdr_size; /* success */
  160. }
  161. static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
  162. int bufnum)
  163. {
  164. struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
  165. struct arc_hardware *hard = &pkt->hard;
  166. int ofs;
  167. /* hard header is not included in packet length */
  168. length -= ARC_HDR_SIZE;
  169. /* And neither is the cookie field */
  170. length -= sizeof(int);
  171. BUGMSG(D_DURING, "prepare_tx: txbufs=%d/%d/%d\n",
  172. lp->next_tx, lp->cur_tx, bufnum);
  173. BUGMSG(D_PROTO, "Sending for cap packet %x.\n",
  174. *((int*)&pkt->soft.cap.cookie[0]));
  175. if (length > XMTU) {
  176. /* should never happen! other people already check for this. */
  177. BUGMSG(D_NORMAL, "Bug! prepare_tx with size %d (> %d)\n",
  178. length, XMTU);
  179. length = XMTU;
  180. }
  181. if (length > MinTU) {
  182. hard->offset[0] = 0;
  183. hard->offset[1] = ofs = 512 - length;
  184. } else if (length > MTU) {
  185. hard->offset[0] = 0;
  186. hard->offset[1] = ofs = 512 - length - 3;
  187. } else
  188. hard->offset[0] = ofs = 256 - length;
  189. BUGMSG(D_DURING, "prepare_tx: length=%d ofs=%d\n",
  190. length,ofs);
  191. // Copy the arcnet-header + the protocol byte down:
  192. lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
  193. lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft.cap.proto,
  194. sizeof(pkt->soft.cap.proto));
  195. // Skip the extra integer we have written into it as a cookie
  196. // but write the rest of the message:
  197. lp->hw.copy_to_card(dev, bufnum, ofs+1,
  198. ((unsigned char*)&pkt->soft.cap.mes),length-1);
  199. lp->lastload_dest = hard->dest;
  200. return 1; /* done */
  201. }
  202. static int ack_tx(struct net_device *dev, int acked)
  203. {
  204. struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
  205. struct sk_buff *ackskb;
  206. struct archdr *ackpkt;
  207. int length=sizeof(struct arc_cap);
  208. BUGMSG(D_DURING, "capmode: ack_tx: protocol: %x: result: %d\n",
  209. lp->outgoing.skb->protocol, acked);
  210. BUGLVL(D_SKB) arcnet_dump_skb(dev, lp->outgoing.skb, "ack_tx");
  211. /* Now alloc a skb to send back up through the layers: */
  212. ackskb = alloc_skb(length + ARC_HDR_SIZE , GFP_ATOMIC);
  213. if (ackskb == NULL) {
  214. BUGMSG(D_NORMAL, "Memory squeeze, can't acknowledge.\n");
  215. goto free_outskb;
  216. }
  217. skb_put(ackskb, length + ARC_HDR_SIZE );
  218. ackskb->dev = dev;
  219. ackpkt = (struct archdr *) ackskb->data;
  220. ackskb->mac.raw = ackskb->data;
  221. /* skb_pull(ackskb, ARC_HDR_SIZE); */
  222. memcpy(ackpkt, lp->outgoing.skb->data, ARC_HDR_SIZE+sizeof(struct arc_cap));
  223. ackpkt->soft.cap.proto=0; /* using protocol 0 for acknowledge */
  224. ackpkt->soft.cap.mes.ack=acked;
  225. BUGMSG(D_PROTO, "Ackknowledge for cap packet %x.\n",
  226. *((int*)&ackpkt->soft.cap.cookie[0]));
  227. ackskb->protocol = __constant_htons(ETH_P_ARCNET);
  228. BUGLVL(D_SKB) arcnet_dump_skb(dev, ackskb, "ack_tx_recv");
  229. netif_rx(ackskb);
  230. free_outskb:
  231. dev_kfree_skb_irq(lp->outgoing.skb);
  232. lp->outgoing.proto = NULL; /* We are always finished when in this protocol */
  233. return 0;
  234. }