MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / net / net_init.c
blob854bc2b70ceeda062dd43d3b73b704ed77c197ac
1 /* net_init.c: Initialization for network devices. */
2 /*
3 Written 1993,1994,1995 by Donald Becker.
5 The author may be reached as becker@scyld.com, or C/O
6 Scyld Computing Corporation
7 410 Severn Ave., Suite 210
8 Annapolis MD 21403
10 This file contains the initialization for the "pl14+" style ethernet
11 drivers. It should eventually replace most of drivers/net/Space.c.
12 It's primary advantage is that it's able to allocate low-memory buffers.
13 A secondary advantage is that the dangerous NE*000 netcards can reserve
14 their I/O port region before the SCSI probes start.
16 Modifications/additions by Bjorn Ekwall <bj0rn@blox.se>:
17 ethdev_index[MAX_ETH_CARDS]
18 register_netdev() / unregister_netdev()
20 Modifications by Wolfgang Walter
21 Use dev_close cleanly so we always shut things down tidily.
23 Changed 29/10/95, Alan Cox to pass sockaddr's around for mac addresses.
25 14/06/96 - Paul Gortmaker: Add generic eth_change_mtu() function.
26 24/09/96 - Paul Norton: Add token-ring variants of the netdev functions.
28 08/11/99 - Alan Cox: Got fed up of the mess in this file and cleaned it
29 up. We now share common code and have regularised name
30 allocation setups. Abolished the 16 card limits.
31 03/19/2000 - jgarzik and Urban Widmark: init_etherdev 32-byte align
32 03/21/2001 - jgarzik: alloc_etherdev and friends
36 #include <linux/config.h>
37 #include <linux/module.h>
38 #include <linux/kernel.h>
39 #include <linux/types.h>
40 #include <linux/fs.h>
41 #include <linux/slab.h>
42 #include <linux/if_ether.h>
43 #include <linux/string.h>
44 #include <linux/netdevice.h>
45 #include <linux/etherdevice.h>
46 #include <linux/fddidevice.h>
47 #include <linux/hippidevice.h>
48 #include <linux/trdevice.h>
49 #include <linux/fcdevice.h>
50 #include <linux/if_arp.h>
51 #include <linux/if_ltalk.h>
52 #include <linux/rtnetlink.h>
53 #include <net/neighbour.h>
55 /* The network devices currently exist only in the socket namespace, so these
56 entries are unused. The only ones that make sense are
57 open start the ethercard
58 close stop the ethercard
59 ioctl To get statistics, perhaps set the interface port (AUI, BNC, etc.)
60 One can also imagine getting raw packets using
61 read & write
62 but this is probably better handled by a raw packet socket.
64 Given that almost all of these functions are handled in the current
65 socket-based scheme, putting ethercard devices in /dev/ seems pointless.
67 [Removed all support for /dev network devices. When someone adds
68 streams then by magic we get them, but otherwise they are un-needed
69 and a space waste]
73 struct net_device *alloc_netdev(int sizeof_priv, const char *mask,
74 void (*setup)(struct net_device *))
76 void *p;
77 struct net_device *dev;
78 int alloc_size;
80 /* ensure 32-byte alignment of both the device and private area */
82 alloc_size = (sizeof(struct net_device) + NETDEV_ALIGN_CONST)
83 & ~NETDEV_ALIGN_CONST;
84 alloc_size += sizeof_priv + NETDEV_ALIGN_CONST;
86 p = kmalloc (alloc_size, GFP_KERNEL);
87 if (!p) {
88 printk(KERN_ERR "alloc_dev: Unable to allocate device.\n");
89 return NULL;
92 memset(p, 0, alloc_size);
94 dev = (struct net_device *)(((long)p + NETDEV_ALIGN_CONST)
95 & ~NETDEV_ALIGN_CONST);
96 dev->padded = (char *)dev - (char *)p;
98 if (sizeof_priv)
99 dev->priv = netdev_priv(dev);
101 setup(dev);
102 strcpy(dev->name, mask);
104 return dev;
106 EXPORT_SYMBOL(alloc_netdev);
109 * alloc_etherdev - Allocates and sets up an ethernet device
110 * @sizeof_priv: Size of additional driver-private structure to be allocated
111 * for this ethernet device
113 * Fill in the fields of the device structure with ethernet-generic
114 * values. Basically does everything except registering the device.
116 * Constructs a new net device, complete with a private data area of
117 * size @sizeof_priv. A 32-byte (not bit) alignment is enforced for
118 * this private data area.
121 struct net_device *alloc_etherdev(int sizeof_priv)
123 return alloc_netdev(sizeof_priv, "eth%d", ether_setup);
126 EXPORT_SYMBOL(alloc_etherdev);
128 static int eth_mac_addr(struct net_device *dev, void *p)
130 struct sockaddr *addr=p;
131 if (netif_running(dev))
132 return -EBUSY;
133 memcpy(dev->dev_addr, addr->sa_data,dev->addr_len);
134 return 0;
137 static int eth_change_mtu(struct net_device *dev, int new_mtu)
139 if ((new_mtu < 68) || (new_mtu > 1500))
140 return -EINVAL;
141 dev->mtu = new_mtu;
142 return 0;
145 #ifdef CONFIG_FDDI
148 * alloc_fddidev - Register FDDI device
149 * @sizeof_priv: Size of additional driver-private structure to be allocated
150 * for this FDDI device
152 * Fill in the fields of the device structure with FDDI-generic values.
154 * Constructs a new net device, complete with a private data area of
155 * size @sizeof_priv. A 32-byte (not bit) alignment is enforced for
156 * this private data area.
159 struct net_device *alloc_fddidev(int sizeof_priv)
161 return alloc_netdev(sizeof_priv, "fddi%d", fddi_setup);
164 EXPORT_SYMBOL(alloc_fddidev);
166 static int fddi_change_mtu(struct net_device *dev, int new_mtu)
168 if ((new_mtu < FDDI_K_SNAP_HLEN) || (new_mtu > FDDI_K_SNAP_DLEN))
169 return(-EINVAL);
170 dev->mtu = new_mtu;
171 return(0);
174 #endif /* CONFIG_FDDI */
176 #ifdef CONFIG_HIPPI
178 static int hippi_change_mtu(struct net_device *dev, int new_mtu)
181 * HIPPI's got these nice large MTUs.
183 if ((new_mtu < 68) || (new_mtu > 65280))
184 return -EINVAL;
185 dev->mtu = new_mtu;
186 return(0);
191 * For HIPPI we will actually use the lower 4 bytes of the hardware
192 * address as the I-FIELD rather than the actual hardware address.
194 static int hippi_mac_addr(struct net_device *dev, void *p)
196 struct sockaddr *addr = p;
197 if (netif_running(dev))
198 return -EBUSY;
199 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
200 return 0;
203 static int hippi_neigh_setup_dev(struct net_device *dev, struct neigh_parms *p)
205 /* Never send broadcast/multicast ARP messages */
206 p->mcast_probes = 0;
208 /* In IPv6 unicast probes are valid even on NBMA,
209 * because they are encapsulated in normal IPv6 protocol.
210 * Should be a generic flag.
212 if (p->tbl->family != AF_INET6)
213 p->ucast_probes = 0;
214 return 0;
217 static void hippi_setup(struct net_device *dev)
219 dev->set_multicast_list = NULL;
220 dev->change_mtu = hippi_change_mtu;
221 dev->hard_header = hippi_header;
222 dev->rebuild_header = hippi_rebuild_header;
223 dev->set_mac_address = hippi_mac_addr;
224 dev->hard_header_parse = NULL;
225 dev->hard_header_cache = NULL;
226 dev->header_cache_update = NULL;
227 dev->neigh_setup = hippi_neigh_setup_dev;
230 * We don't support HIPPI `ARP' for the time being, and probably
231 * never will unless someone else implements it. However we
232 * still need a fake ARPHRD to make ifconfig and friends play ball.
234 dev->type = ARPHRD_HIPPI;
235 dev->hard_header_len = HIPPI_HLEN;
236 dev->mtu = 65280;
237 dev->addr_len = HIPPI_ALEN;
238 dev->tx_queue_len = 25 /* 5 */;
239 memset(dev->broadcast, 0xFF, HIPPI_ALEN);
243 * HIPPI doesn't support broadcast+multicast and we only use
244 * static ARP tables. ARP is disabled by hippi_neigh_setup_dev.
246 dev->flags = 0;
250 * alloc_hippi_dev - Register HIPPI device
251 * @sizeof_priv: Size of additional driver-private structure to be allocated
252 * for this HIPPI device
254 * Fill in the fields of the device structure with HIPPI-generic values.
256 * Constructs a new net device, complete with a private data area of
257 * size @sizeof_priv. A 32-byte (not bit) alignment is enforced for
258 * this private data area.
261 struct net_device *alloc_hippi_dev(int sizeof_priv)
263 return alloc_netdev(sizeof_priv, "hip%d", hippi_setup);
266 EXPORT_SYMBOL(alloc_hippi_dev);
268 #endif /* CONFIG_HIPPI */
270 void ether_setup(struct net_device *dev)
272 /* Fill in the fields of the device structure with ethernet-generic values.
273 This should be in a common file instead of per-driver. */
275 dev->change_mtu = eth_change_mtu;
276 dev->hard_header = eth_header;
277 dev->rebuild_header = eth_rebuild_header;
278 dev->set_mac_address = eth_mac_addr;
279 dev->hard_header_cache = eth_header_cache;
280 dev->header_cache_update= eth_header_cache_update;
281 dev->hard_header_parse = eth_header_parse;
283 dev->type = ARPHRD_ETHER;
284 dev->hard_header_len = ETH_HLEN;
285 dev->mtu = 1500; /* eth_mtu */
286 dev->addr_len = ETH_ALEN;
287 dev->tx_queue_len = 1000; /* Ethernet wants good queues */
289 memset(dev->broadcast,0xFF, ETH_ALEN);
291 /* New-style flags. */
292 dev->flags = IFF_BROADCAST|IFF_MULTICAST;
294 EXPORT_SYMBOL(ether_setup);
296 #ifdef CONFIG_FDDI
298 void fddi_setup(struct net_device *dev)
301 * Fill in the fields of the device structure with FDDI-generic values.
302 * This should be in a common file instead of per-driver.
305 dev->change_mtu = fddi_change_mtu;
306 dev->hard_header = fddi_header;
307 dev->rebuild_header = fddi_rebuild_header;
309 dev->type = ARPHRD_FDDI;
310 dev->hard_header_len = FDDI_K_SNAP_HLEN+3; /* Assume 802.2 SNAP hdr len + 3 pad bytes */
311 dev->mtu = FDDI_K_SNAP_DLEN; /* Assume max payload of 802.2 SNAP frame */
312 dev->addr_len = FDDI_K_ALEN;
313 dev->tx_queue_len = 100; /* Long queues on FDDI */
315 memset(dev->broadcast, 0xFF, FDDI_K_ALEN);
317 /* New-style flags */
318 dev->flags = IFF_BROADCAST | IFF_MULTICAST;
320 EXPORT_SYMBOL(fddi_setup);
322 #endif /* CONFIG_FDDI */
324 #if defined(CONFIG_ATALK) || defined(CONFIG_ATALK_MODULE)
326 static int ltalk_change_mtu(struct net_device *dev, int mtu)
328 return -EINVAL;
331 static int ltalk_mac_addr(struct net_device *dev, void *addr)
333 return -EINVAL;
337 void ltalk_setup(struct net_device *dev)
339 /* Fill in the fields of the device structure with localtalk-generic values. */
341 dev->change_mtu = ltalk_change_mtu;
342 dev->hard_header = NULL;
343 dev->rebuild_header = NULL;
344 dev->set_mac_address = ltalk_mac_addr;
345 dev->hard_header_cache = NULL;
346 dev->header_cache_update= NULL;
348 dev->type = ARPHRD_LOCALTLK;
349 dev->hard_header_len = LTALK_HLEN;
350 dev->mtu = LTALK_MTU;
351 dev->addr_len = LTALK_ALEN;
352 dev->tx_queue_len = 10;
354 dev->broadcast[0] = 0xFF;
356 dev->flags = IFF_BROADCAST|IFF_MULTICAST|IFF_NOARP;
358 EXPORT_SYMBOL(ltalk_setup);
360 #endif /* CONFIG_ATALK || CONFIG_ATALK_MODULE */
362 int register_netdev(struct net_device *dev)
364 int err;
366 rtnl_lock();
369 * If the name is a format string the caller wants us to
370 * do a name allocation
373 if (strchr(dev->name, '%'))
375 err = dev_alloc_name(dev, dev->name);
376 if (err < 0)
377 goto out;
381 * Back compatibility hook. Kill this one in 2.5
384 if (dev->name[0]==0 || dev->name[0]==' ')
386 err = dev_alloc_name(dev, "eth%d");
387 if (err < 0)
388 goto out;
391 err = register_netdevice(dev);
393 out:
394 rtnl_unlock();
395 return err;
398 void unregister_netdev(struct net_device *dev)
400 rtnl_lock();
401 unregister_netdevice(dev);
402 rtnl_unlock();
405 EXPORT_SYMBOL(register_netdev);
406 EXPORT_SYMBOL(unregister_netdev);
408 #ifdef CONFIG_TR
410 void tr_setup(struct net_device *dev)
413 * Configure and register
416 dev->hard_header = tr_header;
417 dev->rebuild_header = tr_rebuild_header;
419 dev->type = ARPHRD_IEEE802_TR;
420 dev->hard_header_len = TR_HLEN;
421 dev->mtu = 2000;
422 dev->addr_len = TR_ALEN;
423 dev->tx_queue_len = 100; /* Long queues on tr */
425 memset(dev->broadcast,0xFF, TR_ALEN);
427 /* New-style flags. */
428 dev->flags = IFF_BROADCAST | IFF_MULTICAST ;
432 * alloc_trdev - Register token ring device
433 * @sizeof_priv: Size of additional driver-private structure to be allocated
434 * for this token ring device
436 * Fill in the fields of the device structure with token ring-generic values.
438 * Constructs a new net device, complete with a private data area of
439 * size @sizeof_priv. A 32-byte (not bit) alignment is enforced for
440 * this private data area.
443 struct net_device *alloc_trdev(int sizeof_priv)
445 return alloc_netdev(sizeof_priv, "tr%d", tr_setup);
448 EXPORT_SYMBOL(tr_setup);
449 EXPORT_SYMBOL(alloc_trdev);
451 #endif /* CONFIG_TR */
453 #ifdef CONFIG_NET_FC
455 void fc_setup(struct net_device *dev)
457 dev->hard_header = fc_header;
458 dev->rebuild_header = fc_rebuild_header;
460 dev->type = ARPHRD_IEEE802;
461 dev->hard_header_len = FC_HLEN;
462 dev->mtu = 2024;
463 dev->addr_len = FC_ALEN;
464 dev->tx_queue_len = 100; /* Long queues on fc */
466 memset(dev->broadcast,0xFF, FC_ALEN);
468 /* New-style flags. */
469 dev->flags = IFF_BROADCAST;
473 * alloc_fcdev - Register fibre channel device
474 * @sizeof_priv: Size of additional driver-private structure to be allocated
475 * for this fibre channel device
477 * Fill in the fields of the device structure with fibre channel-generic values.
479 * Constructs a new net device, complete with a private data area of
480 * size @sizeof_priv. A 32-byte (not bit) alignment is enforced for
481 * this private data area.
484 struct net_device *alloc_fcdev(int sizeof_priv)
486 return alloc_netdev(sizeof_priv, "fc%d", fc_setup);
489 EXPORT_SYMBOL(fc_setup);
490 EXPORT_SYMBOL(alloc_fcdev);
492 #endif /* CONFIG_NET_FC */