blob: d340110f5c0cecaa877d796d57a4fccac1c36acb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*********************************************************************
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09002 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Filename: irlan_eth.c
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09004 * Version:
5 * Description:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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>
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Copyright (c) 1998-2000 Dag Brattli, All Rights Reserved.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090016 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * the License, or (at your option) any later version.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090021 *
Jan Engelhardt96de0e22007-10-19 23:21:04 +020022 * Neither Dag Brattli nor University of Tromsø admit liability nor
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090023 * provide warranty for any of this software. This material is
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * provided "AS-IS" and at no charge.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090025 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 ********************************************************************/
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/netdevice.h>
29#include <linux/etherdevice.h>
30#include <linux/inetdevice.h>
31#include <linux/if_arp.h>
32#include <linux/module.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040033#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <net/arp.h>
35
36#include <net/irda/irda.h>
37#include <net/irda/irmod.h>
38#include <net/irda/irlan_common.h>
39#include <net/irda/irlan_client.h>
40#include <net/irda/irlan_event.h>
41#include <net/irda/irlan_eth.h>
42
43static int irlan_eth_open(struct net_device *dev);
44static int irlan_eth_close(struct net_device *dev);
Stephen Hemminger6518bbb2009-08-31 19:50:50 +000045static netdev_tx_t irlan_eth_xmit(struct sk_buff *skb,
46 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static void irlan_eth_set_multicast_list( struct net_device *dev);
48static struct net_device_stats *irlan_eth_get_stats(struct net_device *dev);
49
Stephen Hemminger9cc8ba72009-03-20 19:35:34 +000050static const struct net_device_ops irlan_eth_netdev_ops = {
51 .ndo_open = irlan_eth_open,
52 .ndo_stop = irlan_eth_close,
53 .ndo_start_xmit = irlan_eth_xmit,
54 .ndo_get_stats = irlan_eth_get_stats,
55 .ndo_set_multicast_list = irlan_eth_set_multicast_list,
56 .ndo_change_mtu = eth_change_mtu,
57 .ndo_validate_addr = eth_validate_addr,
58};
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/*
61 * Function irlan_eth_setup (dev)
62 *
63 * The network device initialization function.
64 *
65 */
66static void irlan_eth_setup(struct net_device *dev)
67{
Stephen Hemminger9cc8ba72009-03-20 19:35:34 +000068 ether_setup(dev);
69
70 dev->netdev_ops = &irlan_eth_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 dev->destructor = free_netdev;
72
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090073
74 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * Lets do all queueing in IrTTP instead of this device driver.
76 * Queueing here as well can introduce some strange latency
77 * problems, which we will avoid by setting the queue size to 0.
78 */
79 /*
80 * The bugs in IrTTP and IrLAN that created this latency issue
81 * have now been fixed, and we can propagate flow control properly
82 * to the network layer. However, this requires a minimal queue of
83 * packets for the device.
84 * Without flow control, the Tx Queue is 14 (ttp) + 0 (dev) = 14
85 * With flow control, the Tx Queue is 7 (ttp) + 4 (dev) = 11
86 * See irlan_eth_flow_indication()...
87 * Note : this number was randomly selected and would need to
88 * be adjusted.
89 * Jean II */
90 dev->tx_queue_len = 4;
91}
92
93/*
94 * Function alloc_irlandev
95 *
96 * Allocate network device and control block
97 *
98 */
99struct net_device *alloc_irlandev(const char *name)
100{
101 return alloc_netdev(sizeof(struct irlan_cb), name,
102 irlan_eth_setup);
103}
104
105/*
106 * Function irlan_eth_open (dev)
107 *
108 * Network device has been opened by user
109 *
110 */
111static int irlan_eth_open(struct net_device *dev)
112{
113 struct irlan_cb *self = netdev_priv(dev);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900114
Harvey Harrison0dc47872008-03-05 20:47:47 -0800115 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 /* Ready to play! */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900118 netif_stop_queue(dev); /* Wait until data link is ready */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 /* We are now open, so time to do some work */
121 self->disconnect_reason = 0;
122 irlan_client_wakeup(self, self->saddr, self->daddr);
123
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900124 /* Make sure we have a hardware address before we return,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 so DHCP clients gets happy */
126 return wait_event_interruptible(self->open_wait,
127 !self->tsap_data->connected);
128}
129
130/*
131 * Function irlan_eth_close (dev)
132 *
133 * Stop the ether network device, his function will usually be called by
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900134 * ifconfig down. We should now disconnect the link, We start the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * close timer, so that the instance will be removed if we are unable
136 * to discover the remote device after the disconnect.
137 */
138static int irlan_eth_close(struct net_device *dev)
139{
140 struct irlan_cb *self = netdev_priv(dev);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900141
Harvey Harrison0dc47872008-03-05 20:47:47 -0800142 IRDA_DEBUG(2, "%s()\n", __func__ );
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 /* Stop device */
145 netif_stop_queue(dev);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 irlan_close_data_channel(self);
148 irlan_close_tsaps(self);
149
150 irlan_do_client_event(self, IRLAN_LMP_DISCONNECT, NULL);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900151 irlan_do_provider_event(self, IRLAN_LMP_DISCONNECT, NULL);
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /* Remove frames queued on the control channel */
154 skb_queue_purge(&self->client.txq);
155
156 self->client.tx_busy = 0;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return 0;
159}
160
161/*
162 * Function irlan_eth_tx (skb)
163 *
164 * Transmits ethernet frames over IrDA link.
165 *
166 */
Stephen Hemminger6518bbb2009-08-31 19:50:50 +0000167static netdev_tx_t irlan_eth_xmit(struct sk_buff *skb,
168 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 struct irlan_cb *self = netdev_priv(dev);
171 int ret;
172
173 /* skb headroom large enough to contain all IrDA-headers? */
174 if ((skb_headroom(skb) < self->max_header_size) || (skb_shared(skb))) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900175 struct sk_buff *new_skb =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 skb_realloc_headroom(skb, self->max_header_size);
177
178 /* We have to free the original skb anyway */
179 dev_kfree_skb(skb);
180
181 /* Did the realloc succeed? */
182 if (new_skb == NULL)
Patrick McHardy6ed10652009-06-23 06:03:08 +0000183 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 /* Use the new skb instead */
186 skb = new_skb;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 dev->trans_start = jiffies;
190
191 /* Now queue the packet in the transport layer */
192 if (self->use_udata)
193 ret = irttp_udata_request(self->tsap_data, skb);
194 else
195 ret = irttp_data_request(self->tsap_data, skb);
196
197 if (ret < 0) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900198 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 * IrTTPs tx queue is full, so we just have to
200 * drop the frame! You might think that we should
201 * just return -1 and don't deallocate the frame,
202 * but that is dangerous since it's possible that
203 * we have replaced the original skb with a new
204 * one with larger headroom, and that would really
205 * confuse do_dev_queue_xmit() in dev.c! I have
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900206 * tried :-) DB
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 */
208 /* irttp_data_request already free the packet */
209 self->stats.tx_dropped++;
210 } else {
211 self->stats.tx_packets++;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900212 self->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900214
Patrick McHardy6ed10652009-06-23 06:03:08 +0000215 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218/*
219 * Function irlan_eth_receive (handle, skb)
220 *
221 * This function gets the data that is received on the data channel
222 *
223 */
224int irlan_eth_receive(void *instance, void *sap, struct sk_buff *skb)
225{
226 struct irlan_cb *self = instance;
227
228 if (skb == NULL) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900229 ++self->stats.rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return 0;
231 }
232 if (skb->len < ETH_HLEN) {
233 IRDA_DEBUG(0, "%s() : IrLAN frame too short (%d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800234 __func__, skb->len);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900235 ++self->stats.rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 dev_kfree_skb(skb);
237 return 0;
238 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900239
240 /*
241 * Adopt this frame! Important to set all these fields since they
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 * might have been previously set by the low level IrDA network
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900243 * device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 */
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -0700245 skb->protocol = eth_type_trans(skb, self->dev); /* Remove eth header */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 self->stats.rx_packets++;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900248 self->stats.rx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 netif_rx(skb); /* Eat it! */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return 0;
253}
254
255/*
256 * Function irlan_eth_flow (status)
257 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900258 * Do flow control between IP/Ethernet and IrLAN/IrTTP. This is done by
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 * controlling the queue stop/start.
260 *
261 * The IrDA link layer has the advantage to have flow control, and
262 * IrTTP now properly handles that. Flow controlling the higher layers
263 * prevent us to drop Tx packets in here (up to 15% for a TCP socket,
264 * more for UDP socket).
265 * Also, this allow us to reduce the overall transmit queue, which means
266 * less latency in case of mixed traffic.
267 * Jean II
268 */
269void irlan_eth_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
270{
271 struct irlan_cb *self;
272 struct net_device *dev;
273
274 self = (struct irlan_cb *) instance;
275
276 IRDA_ASSERT(self != NULL, return;);
277 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 dev = self->dev;
280
281 IRDA_ASSERT(dev != NULL, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900282
Harvey Harrison0dc47872008-03-05 20:47:47 -0800283 IRDA_DEBUG(0, "%s() : flow %s ; running %d\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 flow == FLOW_STOP ? "FLOW_STOP" : "FLOW_START",
285 netif_running(dev));
286
287 switch (flow) {
288 case FLOW_STOP:
289 /* IrTTP is full, stop higher layers */
290 netif_stop_queue(dev);
291 break;
292 case FLOW_START:
293 default:
294 /* Tell upper layers that its time to transmit frames again */
295 /* Schedule network layer */
296 netif_wake_queue(dev);
297 break;
298 }
299}
300
301/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * Function set_multicast_list (dev)
303 *
304 * Configure the filtering of the device
305 *
306 */
307#define HW_MAX_ADDRS 4 /* Must query to get it! */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900308static void irlan_eth_set_multicast_list(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900310 struct irlan_cb *self = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Harvey Harrison0dc47872008-03-05 20:47:47 -0800312 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /* Check if data channel has been connected yet */
315 if (self->client.state != IRLAN_DATA) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800316 IRDA_DEBUG(1, "%s(), delaying!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return;
318 }
319
320 if (dev->flags & IFF_PROMISC) {
321 /* Enable promiscuous mode */
Joe Perchescc53ded2007-12-20 14:00:51 -0800322 IRDA_WARNING("Promiscuous mode not implemented by IrLAN!\n");
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 else if ((dev->flags & IFF_ALLMULTI) || dev->mc_count > HW_MAX_ADDRS) {
325 /* Disable promiscuous mode, use normal mode. */
Harvey Harrison0dc47872008-03-05 20:47:47 -0800326 IRDA_DEBUG(4, "%s(), Setting multicast filter\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /* hardware_set_filter(NULL); */
328
329 irlan_set_multicast_filter(self, TRUE);
330 }
331 else if (dev->mc_count) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800332 IRDA_DEBUG(4, "%s(), Setting multicast filter\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /* Walk the address list, and load the filter */
334 /* hardware_set_filter(dev->mc_list); */
335
336 irlan_set_multicast_filter(self, TRUE);
337 }
338 else {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800339 IRDA_DEBUG(4, "%s(), Clearing multicast filter\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 irlan_set_multicast_filter(self, FALSE);
341 }
342
343 if (dev->flags & IFF_BROADCAST)
344 irlan_set_broadcast_filter(self, TRUE);
345 else
346 irlan_set_broadcast_filter(self, FALSE);
347}
348
349/*
350 * Function irlan_get_stats (dev)
351 *
352 * Get the current statistics for this device
353 *
354 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900355static struct net_device_stats *irlan_eth_get_stats(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 struct irlan_cb *self = netdev_priv(dev);
358
359 return &self->stats;
360}