nfeth.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * atari_nfeth.c - ARAnyM ethernet card driver for GNU/Linux
  3. *
  4. * Copyright (c) 2005 Milan Jurik, Petr Stehlik of ARAnyM dev team
  5. *
  6. * Based on ARAnyM driver for FreeMiNT written by Standa Opichal
  7. *
  8. * This software may be used and distributed according to the terms of
  9. * the GNU General Public License (GPL), incorporated herein by reference.
  10. */
  11. #define DRV_VERSION "0.3"
  12. #define DRV_RELDATE "10/12/2005"
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/netdevice.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/module.h>
  17. #include <asm/natfeat.h>
  18. #include <asm/virtconvert.h>
  19. enum {
  20. GET_VERSION = 0,/* no parameters, return NFAPI_VERSION in d0 */
  21. XIF_INTLEVEL, /* no parameters, return Interrupt Level in d0 */
  22. XIF_IRQ, /* acknowledge interrupt from host */
  23. XIF_START, /* (ethX), called on 'ifup', start receiver thread */
  24. XIF_STOP, /* (ethX), called on 'ifdown', stop the thread */
  25. XIF_READLENGTH, /* (ethX), return size of network data block to read */
  26. XIF_READBLOCK, /* (ethX, buffer, size), read block of network data */
  27. XIF_WRITEBLOCK, /* (ethX, buffer, size), write block of network data */
  28. XIF_GET_MAC, /* (ethX, buffer, size), return MAC HW addr in buffer */
  29. XIF_GET_IPHOST, /* (ethX, buffer, size), return IP address of host */
  30. XIF_GET_IPATARI,/* (ethX, buffer, size), return IP address of atari */
  31. XIF_GET_NETMASK /* (ethX, buffer, size), return IP netmask */
  32. };
  33. #define MAX_UNIT 8
  34. /* These identify the driver base version and may not be removed. */
  35. static const char version[] __devinitdata =
  36. KERN_INFO KBUILD_MODNAME ".c:v" DRV_VERSION " " DRV_RELDATE
  37. " S.Opichal, M.Jurik, P.Stehlik\n"
  38. KERN_INFO " http://aranym.org/\n";
  39. MODULE_AUTHOR("Milan Jurik");
  40. MODULE_DESCRIPTION("Atari NFeth driver");
  41. MODULE_LICENSE("GPL");
  42. /*
  43. MODULE_PARM(nfeth_debug, "i");
  44. MODULE_PARM_DESC(nfeth_debug, "nfeth_debug level (1-2)");
  45. */
  46. static long nfEtherID;
  47. static int nfEtherIRQ;
  48. struct nfeth_private {
  49. int ethX;
  50. };
  51. static struct net_device *nfeth_dev[MAX_UNIT];
  52. static int nfeth_open(struct net_device *dev)
  53. {
  54. struct nfeth_private *priv = netdev_priv(dev);
  55. int res;
  56. res = nf_call(nfEtherID + XIF_START, priv->ethX);
  57. netdev_dbg(dev, "%s: %d\n", __func__, res);
  58. /* Ready for data */
  59. netif_start_queue(dev);
  60. return 0;
  61. }
  62. static int nfeth_stop(struct net_device *dev)
  63. {
  64. struct nfeth_private *priv = netdev_priv(dev);
  65. /* No more data */
  66. netif_stop_queue(dev);
  67. nf_call(nfEtherID + XIF_STOP, priv->ethX);
  68. return 0;
  69. }
  70. /*
  71. * Read a packet out of the adapter and pass it to the upper layers
  72. */
  73. static inline void recv_packet(struct net_device *dev)
  74. {
  75. struct nfeth_private *priv = netdev_priv(dev);
  76. unsigned short pktlen;
  77. struct sk_buff *skb;
  78. /* read packet length (excluding 32 bit crc) */
  79. pktlen = nf_call(nfEtherID + XIF_READLENGTH, priv->ethX);
  80. netdev_dbg(dev, "%s: %u\n", __func__, pktlen);
  81. if (!pktlen) {
  82. netdev_dbg(dev, "%s: pktlen == 0\n", __func__);
  83. dev->stats.rx_errors++;
  84. return;
  85. }
  86. skb = dev_alloc_skb(pktlen + 2);
  87. if (!skb) {
  88. netdev_dbg(dev, "%s: out of mem (buf_alloc failed)\n",
  89. __func__);
  90. dev->stats.rx_dropped++;
  91. return;
  92. }
  93. skb->dev = dev;
  94. skb_reserve(skb, 2); /* 16 Byte align */
  95. skb_put(skb, pktlen); /* make room */
  96. nf_call(nfEtherID + XIF_READBLOCK, priv->ethX, virt_to_phys(skb->data),
  97. pktlen);
  98. skb->protocol = eth_type_trans(skb, dev);
  99. netif_rx(skb);
  100. dev->last_rx = jiffies;
  101. dev->stats.rx_packets++;
  102. dev->stats.rx_bytes += pktlen;
  103. /* and enqueue packet */
  104. return;
  105. }
  106. static irqreturn_t nfeth_interrupt(int irq, void *dev_id)
  107. {
  108. int i, m, mask;
  109. mask = nf_call(nfEtherID + XIF_IRQ, 0);
  110. for (i = 0, m = 1; i < MAX_UNIT; m <<= 1, i++) {
  111. if (mask & m && nfeth_dev[i]) {
  112. recv_packet(nfeth_dev[i]);
  113. nf_call(nfEtherID + XIF_IRQ, m);
  114. }
  115. }
  116. return IRQ_HANDLED;
  117. }
  118. static int nfeth_xmit(struct sk_buff *skb, struct net_device *dev)
  119. {
  120. unsigned int len;
  121. char *data, shortpkt[ETH_ZLEN];
  122. struct nfeth_private *priv = netdev_priv(dev);
  123. data = skb->data;
  124. len = skb->len;
  125. if (len < ETH_ZLEN) {
  126. memset(shortpkt, 0, ETH_ZLEN);
  127. memcpy(shortpkt, data, len);
  128. data = shortpkt;
  129. len = ETH_ZLEN;
  130. }
  131. netdev_dbg(dev, "%s: send %u bytes\n", __func__, len);
  132. nf_call(nfEtherID + XIF_WRITEBLOCK, priv->ethX, virt_to_phys(data),
  133. len);
  134. dev->stats.tx_packets++;
  135. dev->stats.tx_bytes += len;
  136. dev_kfree_skb(skb);
  137. return 0;
  138. }
  139. static void nfeth_tx_timeout(struct net_device *dev)
  140. {
  141. dev->stats.tx_errors++;
  142. netif_wake_queue(dev);
  143. }
  144. static const struct net_device_ops nfeth_netdev_ops = {
  145. .ndo_open = nfeth_open,
  146. .ndo_stop = nfeth_stop,
  147. .ndo_start_xmit = nfeth_xmit,
  148. .ndo_tx_timeout = nfeth_tx_timeout,
  149. .ndo_validate_addr = eth_validate_addr,
  150. .ndo_change_mtu = eth_change_mtu,
  151. .ndo_set_mac_address = eth_mac_addr,
  152. };
  153. static struct net_device * __init nfeth_probe(int unit)
  154. {
  155. struct net_device *dev;
  156. struct nfeth_private *priv;
  157. char mac[ETH_ALEN], host_ip[32], local_ip[32];
  158. int err;
  159. if (!nf_call(nfEtherID + XIF_GET_MAC, unit, mac, ETH_ALEN))
  160. return NULL;
  161. dev = alloc_etherdev(sizeof(struct nfeth_private));
  162. if (!dev)
  163. return NULL;
  164. dev->irq = nfEtherIRQ;
  165. dev->netdev_ops = &nfeth_netdev_ops;
  166. dev->flags |= NETIF_F_NO_CSUM;
  167. memcpy(dev->dev_addr, mac, ETH_ALEN);
  168. priv = netdev_priv(dev);
  169. priv->ethX = unit;
  170. err = register_netdev(dev);
  171. if (err) {
  172. free_netdev(dev);
  173. return NULL;
  174. }
  175. nf_call(nfEtherID + XIF_GET_IPHOST, unit,
  176. host_ip, sizeof(host_ip));
  177. nf_call(nfEtherID + XIF_GET_IPATARI, unit,
  178. local_ip, sizeof(local_ip));
  179. netdev_info(dev, KBUILD_MODNAME " addr:%s (%s) HWaddr:%pM\n", host_ip,
  180. local_ip, mac);
  181. return dev;
  182. }
  183. static int __init nfeth_init(void)
  184. {
  185. long ver;
  186. int error, i;
  187. nfEtherID = nf_get_id("ETHERNET");
  188. if (!nfEtherID)
  189. return -ENODEV;
  190. ver = nf_call(nfEtherID + GET_VERSION);
  191. pr_info("API %lu\n", ver);
  192. nfEtherIRQ = nf_call(nfEtherID + XIF_INTLEVEL);
  193. error = request_irq(nfEtherIRQ, nfeth_interrupt, IRQF_SHARED,
  194. "eth emu", nfeth_interrupt);
  195. if (error) {
  196. pr_err("request for irq %d failed %d", nfEtherIRQ, error);
  197. return error;
  198. }
  199. for (i = 0; i < MAX_UNIT; i++)
  200. nfeth_dev[i] = nfeth_probe(i);
  201. return 0;
  202. }
  203. static void __exit nfeth_cleanup(void)
  204. {
  205. int i;
  206. for (i = 0; i < MAX_UNIT; i++) {
  207. if (nfeth_dev[i]) {
  208. unregister_netdev(nfeth_dev[0]);
  209. free_netdev(nfeth_dev[0]);
  210. }
  211. }
  212. free_irq(nfEtherIRQ, nfeth_interrupt);
  213. }
  214. module_init(nfeth_init);
  215. module_exit(nfeth_cleanup);