arc-rawmode.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:5k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Linux ARCnet driver - "raw mode" packet encapsulation (no soft headers)
  3.  * 
  4.  * Written 1994-1999 by Avery Pennarun.
  5.  * Derived from skeleton.c by Donald Becker.
  6.  *
  7.  * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
  8.  *  for sponsoring the further development of this driver.
  9.  *
  10.  * **********************
  11.  *
  12.  * The original copyright of skeleton.c was as follows:
  13.  *
  14.  * skeleton.c Written 1993 by Donald Becker.
  15.  * Copyright 1993 United States Government as represented by the
  16.  * Director, National Security Agency.  This software may only be used
  17.  * and distributed according to the terms of the GNU General Public License as
  18.  * modified by SRC, incorporated herein by reference.
  19.  *
  20.  * **********************
  21.  *
  22.  * For more details, see drivers/net/arcnet.c
  23.  *
  24.  * **********************
  25.  */
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/if_arp.h>
  29. #include <net/arp.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/arcdevice.h>
  33. #define VERSION "arcnet: raw mode (`r') encapsulation support loaded.n"
  34. static void rx(struct net_device *dev, int bufnum,
  35.        struct archdr *pkthdr, int length);
  36. static int build_header(struct sk_buff *skb, unsigned short type,
  37. uint8_t daddr);
  38. static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
  39.       int bufnum);
  40. struct ArcProto rawmode_proto =
  41. {
  42. 'r',
  43. XMTU,
  44. rx,
  45. build_header,
  46. prepare_tx
  47. };
  48. void arcnet_raw_init(void)
  49. {
  50. int count;
  51. for (count = 0; count < 256; count++)
  52. if (arc_proto_map[count] == arc_proto_default)
  53. arc_proto_map[count] = &rawmode_proto;
  54. /* for raw mode, we only set the bcast proto if there's no better one */
  55. if (arc_bcast_proto == arc_proto_default)
  56. arc_bcast_proto = &rawmode_proto;
  57. arc_proto_default = &rawmode_proto;
  58. }
  59. #ifdef MODULE
  60. int __init init_module(void)
  61. {
  62. printk(VERSION);
  63. arcnet_raw_init();
  64. return 0;
  65. }
  66. void cleanup_module(void)
  67. {
  68. arcnet_unregister_proto(&rawmode_proto);
  69. }
  70. MODULE_LICENSE("GPL");
  71. #endif /* MODULE */
  72. /* packet receiver */
  73. static void rx(struct net_device *dev, int bufnum,
  74.        struct archdr *pkthdr, int length)
  75. {
  76. struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
  77. struct sk_buff *skb;
  78. struct archdr *pkt = pkthdr;
  79. int ofs;
  80. BUGMSG(D_DURING, "it's a raw packet (length=%d)n", length);
  81. if (length >= MinTU)
  82. ofs = 512 - length;
  83. else
  84. ofs = 256 - length;
  85. skb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
  86. if (skb == NULL) {
  87. BUGMSG(D_NORMAL, "Memory squeeze, dropping packet.n");
  88. lp->stats.rx_dropped++;
  89. return;
  90. }
  91. skb_put(skb, length + ARC_HDR_SIZE);
  92. skb->dev = dev;
  93. pkt = (struct archdr *) skb->data;
  94. skb->mac.raw = skb->data;
  95. skb_pull(skb, ARC_HDR_SIZE);
  96. /* up to sizeof(pkt->soft) has already been copied from the card */
  97. memcpy(pkt, pkthdr, sizeof(struct archdr));
  98. if (length > sizeof(pkt->soft))
  99. lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
  100.       pkt->soft.raw + sizeof(pkt->soft),
  101.       length - sizeof(pkt->soft));
  102. BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "rx");
  103. skb->protocol = 0;
  104. netif_rx(skb);
  105. dev->last_rx = jiffies;
  106. }
  107. /*
  108.  * Create the ARCnet hard/soft headers for raw mode.
  109.  * There aren't any soft headers in raw mode - not even the protocol id.
  110.  */
  111. static int build_header(struct sk_buff *skb, unsigned short type,
  112. uint8_t daddr)
  113. {
  114. struct net_device *dev = skb->dev;
  115. int hdr_size = ARC_HDR_SIZE;
  116. struct archdr *pkt = (struct archdr *) skb_push(skb, hdr_size);
  117. /*
  118.  * Set the source hardware address.
  119.  *
  120.  * This is pretty pointless for most purposes, but it can help in
  121.  * debugging.  ARCnet does not allow us to change the source address in
  122.  * the actual packet sent)
  123.  */
  124. pkt->hard.source = *dev->dev_addr;
  125. /* see linux/net/ethernet/eth.c to see where I got the following */
  126. if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
  127. /* 
  128.  * FIXME: fill in the last byte of the dest ipaddr here to better
  129.  * comply with RFC1051 in "noarp" mode.
  130.  */
  131. pkt->hard.dest = 0;
  132. return hdr_size;
  133. }
  134. /* otherwise, just fill it in and go! */
  135. pkt->hard.dest = daddr;
  136. return hdr_size; /* success */
  137. }
  138. static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
  139.       int bufnum)
  140. {
  141. struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
  142. struct arc_hardware *hard = &pkt->hard;
  143. int ofs;
  144. BUGMSG(D_DURING, "prepare_tx: txbufs=%d/%d/%dn",
  145.        lp->next_tx, lp->cur_tx, bufnum);
  146. length -= ARC_HDR_SIZE; /* hard header is not included in packet length */
  147. if (length > XMTU) {
  148. /* should never happen! other people already check for this. */
  149. BUGMSG(D_NORMAL, "Bug!  prepare_tx with size %d (> %d)n",
  150.        length, XMTU);
  151. length = XMTU;
  152. }
  153. if (length > MinTU) {
  154. hard->offset[0] = 0;
  155. hard->offset[1] = ofs = 512 - length;
  156. } else if (length > MTU) {
  157. hard->offset[0] = 0;
  158. hard->offset[1] = ofs = 512 - length - 3;
  159. } else
  160. hard->offset[0] = ofs = 256 - length;
  161. lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
  162. lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft, length);
  163. lp->lastload_dest = hard->dest;
  164. return 1; /* done */
  165. }