capmode.c 7.9 KB

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