blob: 8ee1ff6c742fd6745482f8e0327ab7a3c2d86fe5 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Stephen Hemminger9cc8ba72009-03-20 19:35:34 +000049static const struct net_device_ops irlan_eth_netdev_ops = {
50 .ndo_open = irlan_eth_open,
51 .ndo_stop = irlan_eth_close,
52 .ndo_start_xmit = irlan_eth_xmit,
Stephen Hemminger9cc8ba72009-03-20 19:35:34 +000053 .ndo_set_multicast_list = irlan_eth_set_multicast_list,
54 .ndo_change_mtu = eth_change_mtu,
55 .ndo_validate_addr = eth_validate_addr,
56};
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*
59 * Function irlan_eth_setup (dev)
60 *
61 * The network device initialization function.
62 *
63 */
64static void irlan_eth_setup(struct net_device *dev)
65{
Stephen Hemminger9cc8ba72009-03-20 19:35:34 +000066 ether_setup(dev);
67
68 dev->netdev_ops = &irlan_eth_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 dev->destructor = free_netdev;
70
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090071
72 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * Lets do all queueing in IrTTP instead of this device driver.
74 * Queueing here as well can introduce some strange latency
75 * problems, which we will avoid by setting the queue size to 0.
76 */
77 /*
78 * The bugs in IrTTP and IrLAN that created this latency issue
79 * have now been fixed, and we can propagate flow control properly
80 * to the network layer. However, this requires a minimal queue of
81 * packets for the device.
82 * Without flow control, the Tx Queue is 14 (ttp) + 0 (dev) = 14
83 * With flow control, the Tx Queue is 7 (ttp) + 4 (dev) = 11
84 * See irlan_eth_flow_indication()...
85 * Note : this number was randomly selected and would need to
86 * be adjusted.
87 * Jean II */
88 dev->tx_queue_len = 4;
89}
90
91/*
92 * Function alloc_irlandev
93 *
94 * Allocate network device and control block
95 *
96 */
97struct net_device *alloc_irlandev(const char *name)
98{
99 return alloc_netdev(sizeof(struct irlan_cb), name,
100 irlan_eth_setup);
101}
102
103/*
104 * Function irlan_eth_open (dev)
105 *
106 * Network device has been opened by user
107 *
108 */
109static int irlan_eth_open(struct net_device *dev)
110{
111 struct irlan_cb *self = netdev_priv(dev);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900112
Harvey Harrison0dc47872008-03-05 20:47:47 -0800113 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 /* Ready to play! */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900116 netif_stop_queue(dev); /* Wait until data link is ready */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 /* We are now open, so time to do some work */
119 self->disconnect_reason = 0;
120 irlan_client_wakeup(self, self->saddr, self->daddr);
121
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900122 /* Make sure we have a hardware address before we return,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 so DHCP clients gets happy */
124 return wait_event_interruptible(self->open_wait,
125 !self->tsap_data->connected);
126}
127
128/*
129 * Function irlan_eth_close (dev)
130 *
131 * Stop the ether network device, his function will usually be called by
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900132 * ifconfig down. We should now disconnect the link, We start the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * close timer, so that the instance will be removed if we are unable
134 * to discover the remote device after the disconnect.
135 */
136static int irlan_eth_close(struct net_device *dev)
137{
138 struct irlan_cb *self = netdev_priv(dev);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900139
Harvey Harrison0dc47872008-03-05 20:47:47 -0800140 IRDA_DEBUG(2, "%s()\n", __func__ );
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /* Stop device */
143 netif_stop_queue(dev);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 irlan_close_data_channel(self);
146 irlan_close_tsaps(self);
147
148 irlan_do_client_event(self, IRLAN_LMP_DISCONNECT, NULL);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900149 irlan_do_provider_event(self, IRLAN_LMP_DISCONNECT, NULL);
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 /* Remove frames queued on the control channel */
152 skb_queue_purge(&self->client.txq);
153
154 self->client.tx_busy = 0;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return 0;
157}
158
159/*
160 * Function irlan_eth_tx (skb)
161 *
162 * Transmits ethernet frames over IrDA link.
163 *
164 */
Stephen Hemminger6518bbb2009-08-31 19:50:50 +0000165static netdev_tx_t irlan_eth_xmit(struct sk_buff *skb,
166 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 struct irlan_cb *self = netdev_priv(dev);
169 int ret;
Eric Dumazet79c5f512010-08-18 00:24:43 +0000170 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 /* skb headroom large enough to contain all IrDA-headers? */
173 if ((skb_headroom(skb) < self->max_header_size) || (skb_shared(skb))) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900174 struct sk_buff *new_skb =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 skb_realloc_headroom(skb, self->max_header_size);
176
177 /* We have to free the original skb anyway */
178 dev_kfree_skb(skb);
179
180 /* Did the realloc succeed? */
181 if (new_skb == NULL)
Patrick McHardy6ed10652009-06-23 06:03:08 +0000182 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 /* Use the new skb instead */
185 skb = new_skb;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 dev->trans_start = jiffies;
189
Eric Dumazet79c5f512010-08-18 00:24:43 +0000190 len = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* 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 */
Eric Dumazet81ce7902010-08-19 23:51:33 +0000209 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 } else {
Eric Dumazet81ce7902010-08-19 23:51:33 +0000211 dev->stats.tx_packets++;
212 dev->stats.tx_bytes += 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;
Eric Dumazet81ce7902010-08-19 23:51:33 +0000227 struct net_device *dev = self->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 if (skb == NULL) {
Eric Dumazet81ce7902010-08-19 23:51:33 +0000230 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return 0;
232 }
233 if (skb->len < ETH_HLEN) {
234 IRDA_DEBUG(0, "%s() : IrLAN frame too short (%d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800235 __func__, skb->len);
Eric Dumazet81ce7902010-08-19 23:51:33 +0000236 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 dev_kfree_skb(skb);
238 return 0;
239 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900240
241 /*
242 * Adopt this frame! Important to set all these fields since they
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 * might have been previously set by the low level IrDA network
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900244 * device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
Eric Dumazet81ce7902010-08-19 23:51:33 +0000246 skb->protocol = eth_type_trans(skb, dev); /* Remove eth header */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900247
Eric Dumazet81ce7902010-08-19 23:51:33 +0000248 dev->stats.rx_packets++;
249 dev->stats.rx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 netif_rx(skb); /* Eat it! */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return 0;
254}
255
256/*
257 * Function irlan_eth_flow (status)
258 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900259 * Do flow control between IP/Ethernet and IrLAN/IrTTP. This is done by
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 * controlling the queue stop/start.
261 *
262 * The IrDA link layer has the advantage to have flow control, and
263 * IrTTP now properly handles that. Flow controlling the higher layers
264 * prevent us to drop Tx packets in here (up to 15% for a TCP socket,
265 * more for UDP socket).
266 * Also, this allow us to reduce the overall transmit queue, which means
267 * less latency in case of mixed traffic.
268 * Jean II
269 */
270void irlan_eth_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
271{
272 struct irlan_cb *self;
273 struct net_device *dev;
274
275 self = (struct irlan_cb *) instance;
276
277 IRDA_ASSERT(self != NULL, return;);
278 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 dev = self->dev;
281
282 IRDA_ASSERT(dev != NULL, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900283
Harvey Harrison0dc47872008-03-05 20:47:47 -0800284 IRDA_DEBUG(0, "%s() : flow %s ; running %d\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 flow == FLOW_STOP ? "FLOW_STOP" : "FLOW_START",
286 netif_running(dev));
287
288 switch (flow) {
289 case FLOW_STOP:
290 /* IrTTP is full, stop higher layers */
291 netif_stop_queue(dev);
292 break;
293 case FLOW_START:
294 default:
295 /* Tell upper layers that its time to transmit frames again */
296 /* Schedule network layer */
297 netif_wake_queue(dev);
298 break;
299 }
300}
301
302/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 * Function set_multicast_list (dev)
304 *
305 * Configure the filtering of the device
306 *
307 */
308#define HW_MAX_ADDRS 4 /* Must query to get it! */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900309static void irlan_eth_set_multicast_list(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900311 struct irlan_cb *self = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Harvey Harrison0dc47872008-03-05 20:47:47 -0800313 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 /* Check if data channel has been connected yet */
316 if (self->client.state != IRLAN_DATA) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800317 IRDA_DEBUG(1, "%s(), delaying!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return;
319 }
320
321 if (dev->flags & IFF_PROMISC) {
322 /* Enable promiscuous mode */
Joe Perchescc53ded2007-12-20 14:00:51 -0800323 IRDA_WARNING("Promiscuous mode not implemented by IrLAN!\n");
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900324 }
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000325 else if ((dev->flags & IFF_ALLMULTI) ||
326 netdev_mc_count(dev) > HW_MAX_ADDRS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /* Disable promiscuous mode, use normal mode. */
Harvey Harrison0dc47872008-03-05 20:47:47 -0800328 IRDA_DEBUG(4, "%s(), Setting multicast filter\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 /* hardware_set_filter(NULL); */
330
331 irlan_set_multicast_filter(self, TRUE);
332 }
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000333 else if (!netdev_mc_empty(dev)) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800334 IRDA_DEBUG(4, "%s(), Setting multicast filter\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /* Walk the address list, and load the filter */
336 /* hardware_set_filter(dev->mc_list); */
337
338 irlan_set_multicast_filter(self, TRUE);
339 }
340 else {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800341 IRDA_DEBUG(4, "%s(), Clearing multicast filter\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 irlan_set_multicast_filter(self, FALSE);
343 }
344
345 if (dev->flags & IFF_BROADCAST)
346 irlan_set_broadcast_filter(self, TRUE);
347 else
348 irlan_set_broadcast_filter(self, FALSE);
349}