capmode.c 7.5 KB

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