irlan_eth.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 struct net_device_stats *irlan_eth_get_stats(struct net_device *dev);
  46. static const struct net_device_ops irlan_eth_netdev_ops = {
  47. .ndo_open = irlan_eth_open,
  48. .ndo_stop = irlan_eth_close,
  49. .ndo_start_xmit = irlan_eth_xmit,
  50. .ndo_get_stats = irlan_eth_get_stats,
  51. .ndo_set_multicast_list = irlan_eth_set_multicast_list,
  52. .ndo_change_mtu = eth_change_mtu,
  53. .ndo_validate_addr = eth_validate_addr,
  54. };
  55. /*
  56. * Function irlan_eth_setup (dev)
  57. *
  58. * The network device initialization function.
  59. *
  60. */
  61. static void irlan_eth_setup(struct net_device *dev)
  62. {
  63. ether_setup(dev);
  64. dev->netdev_ops = &irlan_eth_netdev_ops;
  65. dev->destructor = free_netdev;
  66. /*
  67. * Lets do all queueing in IrTTP instead of this device driver.
  68. * Queueing here as well can introduce some strange latency
  69. * problems, which we will avoid by setting the queue size to 0.
  70. */
  71. /*
  72. * The bugs in IrTTP and IrLAN that created this latency issue
  73. * have now been fixed, and we can propagate flow control properly
  74. * to the network layer. However, this requires a minimal queue of
  75. * packets for the device.
  76. * Without flow control, the Tx Queue is 14 (ttp) + 0 (dev) = 14
  77. * With flow control, the Tx Queue is 7 (ttp) + 4 (dev) = 11
  78. * See irlan_eth_flow_indication()...
  79. * Note : this number was randomly selected and would need to
  80. * be adjusted.
  81. * Jean II */
  82. dev->tx_queue_len = 4;
  83. }
  84. /*
  85. * Function alloc_irlandev
  86. *
  87. * Allocate network device and control block
  88. *
  89. */
  90. struct net_device *alloc_irlandev(const char *name)
  91. {
  92. return alloc_netdev(sizeof(struct irlan_cb), name,
  93. irlan_eth_setup);
  94. }
  95. /*
  96. * Function irlan_eth_open (dev)
  97. *
  98. * Network device has been opened by user
  99. *
  100. */
  101. static int irlan_eth_open(struct net_device *dev)
  102. {
  103. struct irlan_cb *self = netdev_priv(dev);
  104. IRDA_DEBUG(2, "%s()\n", __func__ );
  105. /* Ready to play! */
  106. netif_stop_queue(dev); /* Wait until data link is ready */
  107. /* We are now open, so time to do some work */
  108. self->disconnect_reason = 0;
  109. irlan_client_wakeup(self, self->saddr, self->daddr);
  110. /* Make sure we have a hardware address before we return,
  111. so DHCP clients gets happy */
  112. return wait_event_interruptible(self->open_wait,
  113. !self->tsap_data->connected);
  114. }
  115. /*
  116. * Function irlan_eth_close (dev)
  117. *
  118. * Stop the ether network device, his function will usually be called by
  119. * ifconfig down. We should now disconnect the link, We start the
  120. * close timer, so that the instance will be removed if we are unable
  121. * to discover the remote device after the disconnect.
  122. */
  123. static int irlan_eth_close(struct net_device *dev)
  124. {
  125. struct irlan_cb *self = netdev_priv(dev);
  126. IRDA_DEBUG(2, "%s()\n", __func__ );
  127. /* Stop device */
  128. netif_stop_queue(dev);
  129. irlan_close_data_channel(self);
  130. irlan_close_tsaps(self);
  131. irlan_do_client_event(self, IRLAN_LMP_DISCONNECT, NULL);
  132. irlan_do_provider_event(self, IRLAN_LMP_DISCONNECT, NULL);
  133. /* Remove frames queued on the control channel */
  134. skb_queue_purge(&self->client.txq);
  135. self->client.tx_busy = 0;
  136. return 0;
  137. }
  138. /*
  139. * Function irlan_eth_tx (skb)
  140. *
  141. * Transmits ethernet frames over IrDA link.
  142. *
  143. */
  144. static netdev_tx_t irlan_eth_xmit(struct sk_buff *skb,
  145. struct net_device *dev)
  146. {
  147. struct irlan_cb *self = netdev_priv(dev);
  148. int ret;
  149. unsigned int len;
  150. /* skb headroom large enough to contain all IrDA-headers? */
  151. if ((skb_headroom(skb) < self->max_header_size) || (skb_shared(skb))) {
  152. struct sk_buff *new_skb =
  153. skb_realloc_headroom(skb, self->max_header_size);
  154. /* We have to free the original skb anyway */
  155. dev_kfree_skb(skb);
  156. /* Did the realloc succeed? */
  157. if (new_skb == NULL)
  158. return NETDEV_TX_OK;
  159. /* Use the new skb instead */
  160. skb = new_skb;
  161. }
  162. dev->trans_start = jiffies;
  163. len = skb->len;
  164. /* Now queue the packet in the transport layer */
  165. if (self->use_udata)
  166. ret = irttp_udata_request(self->tsap_data, skb);
  167. else
  168. ret = irttp_data_request(self->tsap_data, skb);
  169. if (ret < 0) {
  170. /*
  171. * IrTTPs tx queue is full, so we just have to
  172. * drop the frame! You might think that we should
  173. * just return -1 and don't deallocate the frame,
  174. * but that is dangerous since it's possible that
  175. * we have replaced the original skb with a new
  176. * one with larger headroom, and that would really
  177. * confuse do_dev_queue_xmit() in dev.c! I have
  178. * tried :-) DB
  179. */
  180. /* irttp_data_request already free the packet */
  181. self->stats.tx_dropped++;
  182. } else {
  183. self->stats.tx_packets++;
  184. self->stats.tx_bytes += len;
  185. }
  186. return NETDEV_TX_OK;
  187. }
  188. /*
  189. * Function irlan_eth_receive (handle, skb)
  190. *
  191. * This function gets the data that is received on the data channel
  192. *
  193. */
  194. int irlan_eth_receive(void *instance, void *sap, struct sk_buff *skb)
  195. {
  196. struct irlan_cb *self = instance;
  197. if (skb == NULL) {
  198. ++self->stats.rx_dropped;
  199. return 0;
  200. }
  201. if (skb->len < ETH_HLEN) {
  202. IRDA_DEBUG(0, "%s() : IrLAN frame too short (%d)\n",
  203. __func__, skb->len);
  204. ++self->stats.rx_dropped;
  205. dev_kfree_skb(skb);
  206. return 0;
  207. }
  208. /*
  209. * Adopt this frame! Important to set all these fields since they
  210. * might have been previously set by the low level IrDA network
  211. * device driver
  212. */
  213. skb->protocol = eth_type_trans(skb, self->dev); /* Remove eth header */
  214. self->stats.rx_packets++;
  215. self->stats.rx_bytes += skb->len;
  216. netif_rx(skb); /* Eat it! */
  217. return 0;
  218. }
  219. /*
  220. * Function irlan_eth_flow (status)
  221. *
  222. * Do flow control between IP/Ethernet and IrLAN/IrTTP. This is done by
  223. * controlling the queue stop/start.
  224. *
  225. * The IrDA link layer has the advantage to have flow control, and
  226. * IrTTP now properly handles that. Flow controlling the higher layers
  227. * prevent us to drop Tx packets in here (up to 15% for a TCP socket,
  228. * more for UDP socket).
  229. * Also, this allow us to reduce the overall transmit queue, which means
  230. * less latency in case of mixed traffic.
  231. * Jean II
  232. */
  233. void irlan_eth_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
  234. {
  235. struct irlan_cb *self;
  236. struct net_device *dev;
  237. self = (struct irlan_cb *) instance;
  238. IRDA_ASSERT(self != NULL, return;);
  239. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  240. dev = self->dev;
  241. IRDA_ASSERT(dev != NULL, return;);
  242. IRDA_DEBUG(0, "%s() : flow %s ; running %d\n", __func__,
  243. flow == FLOW_STOP ? "FLOW_STOP" : "FLOW_START",
  244. netif_running(dev));
  245. switch (flow) {
  246. case FLOW_STOP:
  247. /* IrTTP is full, stop higher layers */
  248. netif_stop_queue(dev);
  249. break;
  250. case FLOW_START:
  251. default:
  252. /* Tell upper layers that its time to transmit frames again */
  253. /* Schedule network layer */
  254. netif_wake_queue(dev);
  255. break;
  256. }
  257. }
  258. /*
  259. * Function set_multicast_list (dev)
  260. *
  261. * Configure the filtering of the device
  262. *
  263. */
  264. #define HW_MAX_ADDRS 4 /* Must query to get it! */
  265. static void irlan_eth_set_multicast_list(struct net_device *dev)
  266. {
  267. struct irlan_cb *self = netdev_priv(dev);
  268. IRDA_DEBUG(2, "%s()\n", __func__ );
  269. /* Check if data channel has been connected yet */
  270. if (self->client.state != IRLAN_DATA) {
  271. IRDA_DEBUG(1, "%s(), delaying!\n", __func__ );
  272. return;
  273. }
  274. if (dev->flags & IFF_PROMISC) {
  275. /* Enable promiscuous mode */
  276. IRDA_WARNING("Promiscuous mode not implemented by IrLAN!\n");
  277. }
  278. else if ((dev->flags & IFF_ALLMULTI) ||
  279. netdev_mc_count(dev) > HW_MAX_ADDRS) {
  280. /* Disable promiscuous mode, use normal mode. */
  281. IRDA_DEBUG(4, "%s(), Setting multicast filter\n", __func__ );
  282. /* hardware_set_filter(NULL); */
  283. irlan_set_multicast_filter(self, TRUE);
  284. }
  285. else if (!netdev_mc_empty(dev)) {
  286. IRDA_DEBUG(4, "%s(), Setting multicast filter\n", __func__ );
  287. /* Walk the address list, and load the filter */
  288. /* hardware_set_filter(dev->mc_list); */
  289. irlan_set_multicast_filter(self, TRUE);
  290. }
  291. else {
  292. IRDA_DEBUG(4, "%s(), Clearing multicast filter\n", __func__ );
  293. irlan_set_multicast_filter(self, FALSE);
  294. }
  295. if (dev->flags & IFF_BROADCAST)
  296. irlan_set_broadcast_filter(self, TRUE);
  297. else
  298. irlan_set_broadcast_filter(self, FALSE);
  299. }
  300. /*
  301. * Function irlan_get_stats (dev)
  302. *
  303. * Get the current statistics for this device
  304. *
  305. */
  306. static struct net_device_stats *irlan_eth_get_stats(struct net_device *dev)
  307. {
  308. struct irlan_cb *self = netdev_priv(dev);
  309. return &self->stats;
  310. }