irlan_eth.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*********************************************************************
  2. *
  3. * Filename: irlan_eth.c
  4. * Version:
  5. * Description:
  6. * Status: Experimental.
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Thu Oct 15 08:37:58 1998
  9. * Modified at: Tue Mar 21 09:06:41 2000
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. * Sources: skeleton.c by Donald Becker <becker@CESDIS.gsfc.nasa.gov>
  12. * slip.c by Laurence Culhane, <loz@holmes.demon.co.uk>
  13. * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  14. *
  15. * Copyright (c) 1998-2000 Dag Brattli, All Rights Reserved.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of
  20. * the License, or (at your option) any later version.
  21. *
  22. * Neither Dag Brattli nor University of Tromsø admit liability nor
  23. * provide warranty for any of this software. This material is
  24. * provided "AS-IS" and at no charge.
  25. *
  26. ********************************************************************/
  27. #include <linux/netdevice.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/inetdevice.h>
  30. #include <linux/if_arp.h>
  31. #include <linux/module.h>
  32. #include <linux/sched.h>
  33. #include <net/arp.h>
  34. #include <net/irda/irda.h>
  35. #include <net/irda/irmod.h>
  36. #include <net/irda/irlan_common.h>
  37. #include <net/irda/irlan_client.h>
  38. #include <net/irda/irlan_event.h>
  39. #include <net/irda/irlan_eth.h>
  40. static int irlan_eth_open(struct net_device *dev);
  41. static int irlan_eth_close(struct net_device *dev);
  42. static netdev_tx_t irlan_eth_xmit(struct sk_buff *skb,
  43. struct net_device *dev);
  44. static void irlan_eth_set_multicast_list(struct net_device *dev);
  45. static const struct net_device_ops irlan_eth_netdev_ops = {
  46. .ndo_open = irlan_eth_open,
  47. .ndo_stop = irlan_eth_close,
  48. .ndo_start_xmit = irlan_eth_xmit,
  49. .ndo_set_rx_mode = irlan_eth_set_multicast_list,
  50. .ndo_validate_addr = eth_validate_addr,
  51. };
  52. /*
  53. * Function irlan_eth_setup (dev)
  54. *
  55. * The network device initialization function.
  56. *
  57. */
  58. static void irlan_eth_setup(struct net_device *dev)
  59. {
  60. ether_setup(dev);
  61. dev->netdev_ops = &irlan_eth_netdev_ops;
  62. dev->destructor = free_netdev;
  63. dev->min_mtu = 0;
  64. dev->max_mtu = ETH_MAX_MTU;
  65. /*
  66. * Lets do all queueing in IrTTP instead of this device driver.
  67. * Queueing here as well can introduce some strange latency
  68. * problems, which we will avoid by setting the queue size to 0.
  69. */
  70. /*
  71. * The bugs in IrTTP and IrLAN that created this latency issue
  72. * have now been fixed, and we can propagate flow control properly
  73. * to the network layer. However, this requires a minimal queue of
  74. * packets for the device.
  75. * Without flow control, the Tx Queue is 14 (ttp) + 0 (dev) = 14
  76. * With flow control, the Tx Queue is 7 (ttp) + 4 (dev) = 11
  77. * See irlan_eth_flow_indication()...
  78. * Note : this number was randomly selected and would need to
  79. * be adjusted.
  80. * Jean II */
  81. dev->tx_queue_len = 4;
  82. }
  83. /*
  84. * Function alloc_irlandev
  85. *
  86. * Allocate network device and control block
  87. *
  88. */
  89. struct net_device *alloc_irlandev(const char *name)
  90. {
  91. return alloc_netdev(sizeof(struct irlan_cb), name, NET_NAME_UNKNOWN,
  92. irlan_eth_setup);
  93. }
  94. /*
  95. * Function irlan_eth_open (dev)
  96. *
  97. * Network device has been opened by user
  98. *
  99. */
  100. static int irlan_eth_open(struct net_device *dev)
  101. {
  102. struct irlan_cb *self = netdev_priv(dev);
  103. /* Ready to play! */
  104. netif_stop_queue(dev); /* Wait until data link is ready */
  105. /* We are now open, so time to do some work */
  106. self->disconnect_reason = 0;
  107. irlan_client_wakeup(self, self->saddr, self->daddr);
  108. /* Make sure we have a hardware address before we return,
  109. so DHCP clients gets happy */
  110. return wait_event_interruptible(self->open_wait,
  111. !self->tsap_data->connected);
  112. }
  113. /*
  114. * Function irlan_eth_close (dev)
  115. *
  116. * Stop the ether network device, his function will usually be called by
  117. * ifconfig down. We should now disconnect the link, We start the
  118. * close timer, so that the instance will be removed if we are unable
  119. * to discover the remote device after the disconnect.
  120. */
  121. static int irlan_eth_close(struct net_device *dev)
  122. {
  123. struct irlan_cb *self = netdev_priv(dev);
  124. /* Stop device */
  125. netif_stop_queue(dev);
  126. irlan_close_data_channel(self);
  127. irlan_close_tsaps(self);
  128. irlan_do_client_event(self, IRLAN_LMP_DISCONNECT, NULL);
  129. irlan_do_provider_event(self, IRLAN_LMP_DISCONNECT, NULL);
  130. /* Remove frames queued on the control channel */
  131. skb_queue_purge(&self->client.txq);
  132. self->client.tx_busy = 0;
  133. return 0;
  134. }
  135. /*
  136. * Function irlan_eth_tx (skb)
  137. *
  138. * Transmits ethernet frames over IrDA link.
  139. *
  140. */
  141. static netdev_tx_t irlan_eth_xmit(struct sk_buff *skb,
  142. struct net_device *dev)
  143. {
  144. struct irlan_cb *self = netdev_priv(dev);
  145. int ret;
  146. unsigned int len;
  147. /* skb headroom large enough to contain all IrDA-headers? */
  148. if ((skb_headroom(skb) < self->max_header_size) || (skb_shared(skb))) {
  149. struct sk_buff *new_skb =
  150. skb_realloc_headroom(skb, self->max_header_size);
  151. /* We have to free the original skb anyway */
  152. dev_kfree_skb(skb);
  153. /* Did the realloc succeed? */
  154. if (new_skb == NULL)
  155. return NETDEV_TX_OK;
  156. /* Use the new skb instead */
  157. skb = new_skb;
  158. }
  159. netif_trans_update(dev);
  160. len = skb->len;
  161. /* Now queue the packet in the transport layer */
  162. if (self->use_udata)
  163. ret = irttp_udata_request(self->tsap_data, skb);
  164. else
  165. ret = irttp_data_request(self->tsap_data, skb);
  166. if (ret < 0) {
  167. /*
  168. * IrTTPs tx queue is full, so we just have to
  169. * drop the frame! You might think that we should
  170. * just return -1 and don't deallocate the frame,
  171. * but that is dangerous since it's possible that
  172. * we have replaced the original skb with a new
  173. * one with larger headroom, and that would really
  174. * confuse do_dev_queue_xmit() in dev.c! I have
  175. * tried :-) DB
  176. */
  177. /* irttp_data_request already free the packet */
  178. dev->stats.tx_dropped++;
  179. } else {
  180. dev->stats.tx_packets++;
  181. dev->stats.tx_bytes += len;
  182. }
  183. return NETDEV_TX_OK;
  184. }
  185. /*
  186. * Function irlan_eth_receive (handle, skb)
  187. *
  188. * This function gets the data that is received on the data channel
  189. *
  190. */
  191. int irlan_eth_receive(void *instance, void *sap, struct sk_buff *skb)
  192. {
  193. struct irlan_cb *self = instance;
  194. struct net_device *dev = self->dev;
  195. if (skb == NULL) {
  196. dev->stats.rx_dropped++;
  197. return 0;
  198. }
  199. if (skb->len < ETH_HLEN) {
  200. pr_debug("%s() : IrLAN frame too short (%d)\n",
  201. __func__, skb->len);
  202. dev->stats.rx_dropped++;
  203. dev_kfree_skb(skb);
  204. return 0;
  205. }
  206. /*
  207. * Adopt this frame! Important to set all these fields since they
  208. * might have been previously set by the low level IrDA network
  209. * device driver
  210. */
  211. skb->protocol = eth_type_trans(skb, dev); /* Remove eth header */
  212. dev->stats.rx_packets++;
  213. dev->stats.rx_bytes += skb->len;
  214. netif_rx(skb); /* Eat it! */
  215. return 0;
  216. }
  217. /*
  218. * Function irlan_eth_flow (status)
  219. *
  220. * Do flow control between IP/Ethernet and IrLAN/IrTTP. This is done by
  221. * controlling the queue stop/start.
  222. *
  223. * The IrDA link layer has the advantage to have flow control, and
  224. * IrTTP now properly handles that. Flow controlling the higher layers
  225. * prevent us to drop Tx packets in here (up to 15% for a TCP socket,
  226. * more for UDP socket).
  227. * Also, this allow us to reduce the overall transmit queue, which means
  228. * less latency in case of mixed traffic.
  229. * Jean II
  230. */
  231. void irlan_eth_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
  232. {
  233. struct irlan_cb *self;
  234. struct net_device *dev;
  235. self = instance;
  236. IRDA_ASSERT(self != NULL, return;);
  237. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  238. dev = self->dev;
  239. IRDA_ASSERT(dev != NULL, return;);
  240. pr_debug("%s() : flow %s ; running %d\n", __func__,
  241. flow == FLOW_STOP ? "FLOW_STOP" : "FLOW_START",
  242. netif_running(dev));
  243. switch (flow) {
  244. case FLOW_STOP:
  245. /* IrTTP is full, stop higher layers */
  246. netif_stop_queue(dev);
  247. break;
  248. case FLOW_START:
  249. default:
  250. /* Tell upper layers that its time to transmit frames again */
  251. /* Schedule network layer */
  252. netif_wake_queue(dev);
  253. break;
  254. }
  255. }
  256. /*
  257. * Function set_multicast_list (dev)
  258. *
  259. * Configure the filtering of the device
  260. *
  261. */
  262. #define HW_MAX_ADDRS 4 /* Must query to get it! */
  263. static void irlan_eth_set_multicast_list(struct net_device *dev)
  264. {
  265. struct irlan_cb *self = netdev_priv(dev);
  266. /* Check if data channel has been connected yet */
  267. if (self->client.state != IRLAN_DATA) {
  268. pr_debug("%s(), delaying!\n", __func__);
  269. return;
  270. }
  271. if (dev->flags & IFF_PROMISC) {
  272. /* Enable promiscuous mode */
  273. net_warn_ratelimited("Promiscuous mode not implemented by IrLAN!\n");
  274. } else if ((dev->flags & IFF_ALLMULTI) ||
  275. netdev_mc_count(dev) > HW_MAX_ADDRS) {
  276. /* Disable promiscuous mode, use normal mode. */
  277. pr_debug("%s(), Setting multicast filter\n", __func__);
  278. /* hardware_set_filter(NULL); */
  279. irlan_set_multicast_filter(self, TRUE);
  280. } else if (!netdev_mc_empty(dev)) {
  281. pr_debug("%s(), Setting multicast filter\n", __func__);
  282. /* Walk the address list, and load the filter */
  283. /* hardware_set_filter(dev->mc_list); */
  284. irlan_set_multicast_filter(self, TRUE);
  285. } else {
  286. pr_debug("%s(), Clearing multicast filter\n", __func__);
  287. irlan_set_multicast_filter(self, FALSE);
  288. }
  289. if (dev->flags & IFF_BROADCAST)
  290. irlan_set_broadcast_filter(self, TRUE);
  291. else
  292. irlan_set_broadcast_filter(self, FALSE);
  293. }