nfeth.c 6.2 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/interrupt.h>
  17. #include <linux/module.h>
  18. #include <asm/natfeat.h>
  19. #include <asm/virtconvert.h>
  20. enum {
  21. GET_VERSION = 0,/* no parameters, return NFAPI_VERSION in d0 */
  22. XIF_INTLEVEL, /* no parameters, return Interrupt Level in d0 */
  23. XIF_IRQ, /* acknowledge interrupt from host */
  24. XIF_START, /* (ethX), called on 'ifup', start receiver thread */
  25. XIF_STOP, /* (ethX), called on 'ifdown', stop the thread */
  26. XIF_READLENGTH, /* (ethX), return size of network data block to read */
  27. XIF_READBLOCK, /* (ethX, buffer, size), read block of network data */
  28. XIF_WRITEBLOCK, /* (ethX, buffer, size), write block of network data */
  29. XIF_GET_MAC, /* (ethX, buffer, size), return MAC HW addr in buffer */
  30. XIF_GET_IPHOST, /* (ethX, buffer, size), return IP address of host */
  31. XIF_GET_IPATARI,/* (ethX, buffer, size), return IP address of atari */
  32. XIF_GET_NETMASK /* (ethX, buffer, size), return IP netmask */
  33. };
  34. #define MAX_UNIT 8
  35. /* These identify the driver base version and may not be removed. */
  36. static const char version[] =
  37. KERN_INFO KBUILD_MODNAME ".c:v" DRV_VERSION " " DRV_RELDATE
  38. " S.Opichal, M.Jurik, P.Stehlik\n"
  39. KERN_INFO " http://aranym.org/\n";
  40. MODULE_AUTHOR("Milan Jurik");
  41. MODULE_DESCRIPTION("Atari NFeth driver");
  42. MODULE_LICENSE("GPL");
  43. /*
  44. MODULE_PARM(nfeth_debug, "i");
  45. MODULE_PARM_DESC(nfeth_debug, "nfeth_debug level (1-2)");
  46. */
  47. static long nfEtherID;
  48. static int nfEtherIRQ;
  49. struct nfeth_private {
  50. int ethX;
  51. };
  52. static struct net_device *nfeth_dev[MAX_UNIT];
  53. static int nfeth_open(struct net_device *dev)
  54. {
  55. struct nfeth_private *priv = netdev_priv(dev);
  56. int res;
  57. res = nf_call(nfEtherID + XIF_START, priv->ethX);
  58. netdev_dbg(dev, "%s: %d\n", __func__, res);
  59. /* Ready for data */
  60. netif_start_queue(dev);
  61. return 0;
  62. }
  63. static int nfeth_stop(struct net_device *dev)
  64. {
  65. struct nfeth_private *priv = netdev_priv(dev);
  66. /* No more data */
  67. netif_stop_queue(dev);
  68. nf_call(nfEtherID + XIF_STOP, priv->ethX);
  69. return 0;
  70. }
  71. /*
  72. * Read a packet out of the adapter and pass it to the upper layers
  73. */
  74. static inline void recv_packet(struct net_device *dev)
  75. {
  76. struct nfeth_private *priv = netdev_priv(dev);
  77. unsigned short pktlen;
  78. struct sk_buff *skb;
  79. /* read packet length (excluding 32 bit crc) */
  80. pktlen = nf_call(nfEtherID + XIF_READLENGTH, priv->ethX);
  81. netdev_dbg(dev, "%s: %u\n", __func__, pktlen);
  82. if (!pktlen) {
  83. netdev_dbg(dev, "%s: pktlen == 0\n", __func__);
  84. dev->stats.rx_errors++;
  85. return;
  86. }
  87. skb = dev_alloc_skb(pktlen + 2);
  88. if (!skb) {
  89. netdev_dbg(dev, "%s: out of mem (buf_alloc failed)\n",
  90. __func__);
  91. dev->stats.rx_dropped++;
  92. return;
  93. }
  94. skb->dev = dev;
  95. skb_reserve(skb, 2); /* 16 Byte align */
  96. skb_put(skb, pktlen); /* make room */
  97. nf_call(nfEtherID + XIF_READBLOCK, priv->ethX, virt_to_phys(skb->data),
  98. pktlen);
  99. skb->protocol = eth_type_trans(skb, dev);
  100. netif_rx(skb);
  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_set_mac_address = eth_mac_addr,
  151. };
  152. static struct net_device * __init nfeth_probe(int unit)
  153. {
  154. struct net_device *dev;
  155. struct nfeth_private *priv;
  156. char mac[ETH_ALEN], host_ip[32], local_ip[32];
  157. int err;
  158. if (!nf_call(nfEtherID + XIF_GET_MAC, unit, virt_to_phys(mac),
  159. 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. memcpy(dev->dev_addr, mac, ETH_ALEN);
  167. priv = netdev_priv(dev);
  168. priv->ethX = unit;
  169. err = register_netdev(dev);
  170. if (err) {
  171. free_netdev(dev);
  172. return NULL;
  173. }
  174. nf_call(nfEtherID + XIF_GET_IPHOST, unit,
  175. virt_to_phys(host_ip), sizeof(host_ip));
  176. nf_call(nfEtherID + XIF_GET_IPATARI, unit,
  177. virt_to_phys(local_ip), sizeof(local_ip));
  178. netdev_info(dev, KBUILD_MODNAME " addr:%s (%s) HWaddr:%pM\n", host_ip,
  179. local_ip, mac);
  180. return dev;
  181. }
  182. static int __init nfeth_init(void)
  183. {
  184. long ver;
  185. int error, i;
  186. nfEtherID = nf_get_id("ETHERNET");
  187. if (!nfEtherID)
  188. return -ENODEV;
  189. ver = nf_call(nfEtherID + GET_VERSION);
  190. pr_info("API %lu\n", ver);
  191. nfEtherIRQ = nf_call(nfEtherID + XIF_INTLEVEL);
  192. error = request_irq(nfEtherIRQ, nfeth_interrupt, IRQF_SHARED,
  193. "eth emu", nfeth_interrupt);
  194. if (error) {
  195. pr_err("request for irq %d failed %d", nfEtherIRQ, error);
  196. return error;
  197. }
  198. for (i = 0; i < MAX_UNIT; i++)
  199. nfeth_dev[i] = nfeth_probe(i);
  200. return 0;
  201. }
  202. static void __exit nfeth_cleanup(void)
  203. {
  204. int i;
  205. for (i = 0; i < MAX_UNIT; i++) {
  206. if (nfeth_dev[i]) {
  207. unregister_netdev(nfeth_dev[0]);
  208. free_netdev(nfeth_dev[0]);
  209. }
  210. }
  211. free_irq(nfEtherIRQ, nfeth_interrupt);
  212. }
  213. module_init(nfeth_init);
  214. module_exit(nfeth_cleanup);