Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / net / net_init.c
blob9c5bfcafee704daa6724c59999ae623783e1855a
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@cesdis.gsfc.nasa.gov or
6 C/O Center of Excellence in Space Data and Information Sciences
7 Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
9 This file contains the initialization for the "pl14+" style ethernet
10 drivers. It should eventually replace most of drivers/net/Space.c.
11 It's primary advantage is that it's able to allocate low-memory buffers.
12 A secondary advantage is that the dangerous NE*000 netcards can reserve
13 their I/O port region before the SCSI probes start.
15 Modifications/additions by Bjorn Ekwall <bj0rn@blox.se>:
16 ethdev_index[MAX_ETH_CARDS]
17 register_netdev() / unregister_netdev()
19 Modifications by Wolfgang Walter
20 Use dev_close cleanly so we always shut things down tidily.
22 Changed 29/10/95, Alan Cox to pass sockaddr's around for mac addresses.
24 14/06/96 - Paul Gortmaker: Add generic eth_change_mtu() function.
25 24/09/96 - Paul Norton: Add token-ring variants of the netdev functions.
27 08/11/99 - Alan Cox: Got fed up of the mess in this file and cleaned it
28 up. We now share common code and have regularised name
29 allocation setups. Abolished the 16 card limits.
30 03/19/2000 - jgarzik and Urban Widmark: init_etherdev 32-byte align
34 #include <linux/config.h>
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/types.h>
38 #include <linux/fs.h>
39 #include <linux/malloc.h>
40 #include <linux/if_ether.h>
41 #include <linux/string.h>
42 #include <linux/netdevice.h>
43 #include <linux/etherdevice.h>
44 #include <linux/fddidevice.h>
45 #include <linux/hippidevice.h>
46 #include <linux/trdevice.h>
47 #include <linux/fcdevice.h>
48 #include <linux/if_arp.h>
49 #include <linux/if_ltalk.h>
50 #include <linux/rtnetlink.h>
51 #include <net/neighbour.h>
53 /* The network devices currently exist only in the socket namespace, so these
54 entries are unused. The only ones that make sense are
55 open start the ethercard
56 close stop the ethercard
57 ioctl To get statistics, perhaps set the interface port (AUI, BNC, etc.)
58 One can also imagine getting raw packets using
59 read & write
60 but this is probably better handled by a raw packet socket.
62 Given that almost all of these functions are handled in the current
63 socket-based scheme, putting ethercard devices in /dev/ seems pointless.
65 [Removed all support for /dev network devices. When someone adds
66 streams then by magic we get them, but otherwise they are un-needed
67 and a space waste]
71 static struct net_device *init_alloc_dev(int sizeof_priv)
73 struct net_device *dev;
74 int alloc_size;
76 /* ensure 32-byte alignment of the private area */
77 alloc_size =sizeof(*dev) + sizeof_priv +31;
79 dev = (struct net_device *)kmalloc(alloc_size, GFP_KERNEL);
80 if(dev == NULL)
82 printk(KERN_ERR "alloc_dev: Unable to allocate device memory.\n");
83 return NULL;
86 memset(dev,0, alloc_size);
88 if(sizeof_priv)
89 dev->priv = (void*) (((long)(dev +1) +31) & ~31);
91 return dev;
94 /*
95 * Create and name a device from a prototype, then perform any needed
96 * setup.
99 static struct net_device *init_netdev(struct net_device *dev,int sizeof_priv,
100 char*mask,void(*setup)(struct net_device *))
102 int new_device =0;
105 * Allocate a device if one is not provided.
108 if(dev == NULL) {
109 dev=init_alloc_dev(sizeof_priv);
110 if(dev==NULL)
111 return NULL;
112 new_device =1;
116 * Allocate a name
119 if(dev->name[0] =='\0'|| dev->name[0] ==' ') {
120 strcpy(dev->name, mask);
121 if(dev_alloc_name(dev, mask)<0) {
122 if(new_device)
123 kfree(dev);
124 return NULL;
128 netdev_boot_setup_check(dev);
131 * Configure via the caller provided setup function then
132 * register if needed.
135 setup(dev);
137 if(new_device) {
138 rtnl_lock();
139 register_netdevice(dev);
140 rtnl_unlock();
142 return dev;
146 * init_etherdev - Register ethernet device
147 * @dev: An ethernet device structure to be filled in, or %NULL if a new
148 * struct should be allocated.
149 * @sizeof_priv: Size of additional driver-private structure to be allocated
150 * for this ethernet device
152 * Fill in the fields of the device structure with ethernet-generic values.
154 * If no device structure is passed, a new one is constructed, complete with
155 * a private data area of size @sizeof_priv. A 32-byte (not bit)
156 * alignment is enforced for this private data area.
158 * If an empty string area is passed as dev->name, or a new structure is made,
159 * a new name string is constructed.
162 struct net_device *init_etherdev(struct net_device *dev,int sizeof_priv)
164 returninit_netdev(dev, sizeof_priv,"eth%d", ether_setup);
168 static inteth_mac_addr(struct net_device *dev,void*p)
170 struct sockaddr *addr=p;
171 if(netif_running(dev))
172 return-EBUSY;
173 memcpy(dev->dev_addr, addr->sa_data,dev->addr_len);
174 return0;
177 static inteth_change_mtu(struct net_device *dev,int new_mtu)
179 if((new_mtu <68) || (new_mtu >1500))
180 return-EINVAL;
181 dev->mtu = new_mtu;
182 return0;
185 #ifdef CONFIG_FDDI
187 struct net_device *init_fddidev(struct net_device *dev,int sizeof_priv)
189 returninit_netdev(dev, sizeof_priv,"fddi%d", fddi_setup);
192 static intfddi_change_mtu(struct net_device *dev,int new_mtu)
194 if((new_mtu < FDDI_K_SNAP_HLEN) || (new_mtu > FDDI_K_SNAP_DLEN))
195 return(-EINVAL);
196 dev->mtu = new_mtu;
197 return(0);
200 #endif/* CONFIG_FDDI */
202 #ifdef CONFIG_HIPPI
204 static inthippi_change_mtu(struct net_device *dev,int new_mtu)
207 * HIPPI's got these nice large MTUs.
209 if((new_mtu <68) || (new_mtu >65280))
210 return-EINVAL;
211 dev->mtu = new_mtu;
212 return(0);
217 * For HIPPI we will actually use the lower 4 bytes of the hardware
218 * address as the I-FIELD rather than the actual hardware address.
220 static inthippi_mac_addr(struct net_device *dev,void*p)
222 struct sockaddr *addr = p;
223 if(netif_running(dev))
224 return-EBUSY;
225 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
226 return0;
230 struct net_device *init_hippi_dev(struct net_device *dev,int sizeof_priv)
232 returninit_netdev(dev, sizeof_priv,"hip%d", hippi_setup);
236 voidunregister_hipdev(struct net_device *dev)
238 rtnl_lock();
239 unregister_netdevice(dev);
240 rtnl_unlock();
244 static inthippi_neigh_setup_dev(struct net_device *dev,struct neigh_parms *p)
246 /* Never send broadcast/multicast ARP messages */
247 p->mcast_probes =0;
249 /* In IPv6 unicast probes are valid even on NBMA,
250 * because they are encapsulated in normal IPv6 protocol.
251 * Should be a generic flag.
253 if(p->tbl->family != AF_INET6)
254 p->ucast_probes =0;
255 return0;
258 #endif/* CONFIG_HIPPI */
260 voidether_setup(struct net_device *dev)
262 /* Fill in the fields of the device structure with ethernet-generic values.
263 This should be in a common file instead of per-driver. */
265 dev->change_mtu = eth_change_mtu;
266 dev->hard_header = eth_header;
267 dev->rebuild_header = eth_rebuild_header;
268 dev->set_mac_address = eth_mac_addr;
269 dev->hard_header_cache = eth_header_cache;
270 dev->header_cache_update= eth_header_cache_update;
271 dev->hard_header_parse = eth_header_parse;
273 dev->type = ARPHRD_ETHER;
274 dev->hard_header_len = ETH_HLEN;
275 dev->mtu =1500;/* eth_mtu */
276 dev->addr_len = ETH_ALEN;
277 dev->tx_queue_len =100;/* Ethernet wants good queues */
279 memset(dev->broadcast,0xFF, ETH_ALEN);
281 /* New-style flags. */
282 dev->flags = IFF_BROADCAST|IFF_MULTICAST;
284 dev_init_buffers(dev);
287 #ifdef CONFIG_FDDI
289 voidfddi_setup(struct net_device *dev)
292 * Fill in the fields of the device structure with FDDI-generic values.
293 * This should be in a common file instead of per-driver.
296 dev->change_mtu = fddi_change_mtu;
297 dev->hard_header = fddi_header;
298 dev->rebuild_header = fddi_rebuild_header;
300 dev->type = ARPHRD_FDDI;
301 dev->hard_header_len = FDDI_K_SNAP_HLEN+3;/* Assume 802.2 SNAP hdr len + 3 pad bytes */
302 dev->mtu = FDDI_K_SNAP_DLEN;/* Assume max payload of 802.2 SNAP frame */
303 dev->addr_len = FDDI_K_ALEN;
304 dev->tx_queue_len =100;/* Long queues on FDDI */
306 memset(dev->broadcast,0xFF, FDDI_K_ALEN);
308 /* New-style flags */
309 dev->flags = IFF_BROADCAST | IFF_MULTICAST;
311 dev_init_buffers(dev);
313 return;
316 #endif/* CONFIG_FDDI */
318 #ifdef CONFIG_HIPPI
319 voidhippi_setup(struct net_device *dev)
321 dev->set_multicast_list = NULL;
322 dev->change_mtu = hippi_change_mtu;
323 dev->hard_header = hippi_header;
324 dev->rebuild_header = hippi_rebuild_header;
325 dev->set_mac_address = hippi_mac_addr;
326 dev->hard_header_parse = NULL;
327 dev->hard_header_cache = NULL;
328 dev->header_cache_update = NULL;
329 dev->neigh_setup = hippi_neigh_setup_dev;
332 * We don't support HIPPI `ARP' for the time being, and probably
333 * never will unless someone else implements it. However we
334 * still need a fake ARPHRD to make ifconfig and friends play ball.
336 dev->type = ARPHRD_HIPPI;
337 dev->hard_header_len = HIPPI_HLEN;
338 dev->mtu =65280;
339 dev->addr_len = HIPPI_ALEN;
340 dev->tx_queue_len =25/* 5 */;
341 memset(dev->broadcast,0xFF, HIPPI_ALEN);
345 * HIPPI doesn't support broadcast+multicast and we only use
346 * static ARP tables. ARP is disabled by hippi_neigh_setup_dev.
348 dev->flags =0;
350 dev_init_buffers(dev);
352 #endif/* CONFIG_HIPPI */
354 #if defined(CONFIG_ATALK) || defined(CONFIG_ATALK_MODULE)
356 static intltalk_change_mtu(struct net_device *dev,int mtu)
358 return-EINVAL;
361 static intltalk_mac_addr(struct net_device *dev,void*addr)
363 return-EINVAL;
367 voidltalk_setup(struct net_device *dev)
369 /* Fill in the fields of the device structure with localtalk-generic values. */
371 dev->change_mtu = ltalk_change_mtu;
372 dev->hard_header = NULL;
373 dev->rebuild_header = NULL;
374 dev->set_mac_address = ltalk_mac_addr;
375 dev->hard_header_cache = NULL;
376 dev->header_cache_update= NULL;
378 dev->type = ARPHRD_LOCALTLK;
379 dev->hard_header_len = LTALK_HLEN;
380 dev->mtu = LTALK_MTU;
381 dev->addr_len = LTALK_ALEN;
382 dev->tx_queue_len =10;
384 dev->broadcast[0] =0xFF;
386 dev->flags = IFF_BROADCAST|IFF_MULTICAST|IFF_NOARP;
388 dev_init_buffers(dev);
391 #endif/* CONFIG_ATALK || CONFIG_ATALK_MODULE */
393 intether_config(struct net_device *dev,struct ifmap *map)
395 if(map->mem_start != (u_long)(-1))
396 dev->mem_start = map->mem_start;
397 if(map->mem_end != (u_long)(-1))
398 dev->mem_end = map->mem_end;
399 if(map->base_addr != (u_short)(-1))
400 dev->base_addr = map->base_addr;
401 if(map->irq != (u_char)(-1))
402 dev->irq = map->irq;
403 if(map->dma != (u_char)(-1))
404 dev->dma = map->dma;
405 if(map->port != (u_char)(-1))
406 dev->if_port = map->port;
407 return0;
410 intregister_netdev(struct net_device *dev)
412 int err;
414 rtnl_lock();
417 * If the name is a format string the caller wants us to
418 * do a name allocation
421 if(strchr(dev->name,'%'))
423 err = -EBUSY;
424 if(dev_alloc_name(dev, dev->name)<0)
425 goto out;
429 * Back compatibility hook. Kill this one in 2.5
432 if(dev->name[0]==0|| dev->name[0]==' ')
434 err = -EBUSY;
435 if(dev_alloc_name(dev,"eth%d")<0)
436 goto out;
440 err = -EIO;
441 if(register_netdevice(dev))
442 goto out;
444 err =0;
446 out:
447 rtnl_unlock();
448 return err;
451 voidunregister_netdev(struct net_device *dev)
453 rtnl_lock();
454 unregister_netdevice(dev);
455 rtnl_unlock();
459 #ifdef CONFIG_TR
461 static voidtr_configure(struct net_device *dev)
464 * Configure and register
467 dev->hard_header = tr_header;
468 dev->rebuild_header = tr_rebuild_header;
470 dev->type = ARPHRD_IEEE802_TR;
471 dev->hard_header_len = TR_HLEN;
472 dev->mtu =2000;
473 dev->addr_len = TR_ALEN;
474 dev->tx_queue_len =100;/* Long queues on tr */
476 memset(dev->broadcast,0xFF, TR_ALEN);
478 /* New-style flags. */
479 dev->flags = IFF_BROADCAST | IFF_MULTICAST ;
482 struct net_device *init_trdev(struct net_device *dev,int sizeof_priv)
484 returninit_netdev(dev, sizeof_priv,"tr%d", tr_configure);
487 voidtr_setup(struct net_device *dev)
491 intregister_trdev(struct net_device *dev)
493 dev_init_buffers(dev);
495 if(dev->init && dev->init(dev) !=0) {
496 unregister_trdev(dev);
497 return-EIO;
499 return0;
502 voidunregister_trdev(struct net_device *dev)
504 rtnl_lock();
505 unregister_netdevice(dev);
506 rtnl_unlock();
508 #endif/* CONFIG_TR */
511 #ifdef CONFIG_NET_FC
513 voidfc_setup(struct net_device *dev)
515 dev->hard_header = fc_header;
516 dev->rebuild_header = fc_rebuild_header;
518 dev->type = ARPHRD_IEEE802;
519 dev->hard_header_len = FC_HLEN;
520 dev->mtu =2024;
521 dev->addr_len = FC_ALEN;
522 dev->tx_queue_len =100;/* Long queues on fc */
524 memset(dev->broadcast,0xFF, FC_ALEN);
526 /* New-style flags. */
527 dev->flags = IFF_BROADCAST;
528 dev_init_buffers(dev);
529 return;
533 struct net_device *init_fcdev(struct net_device *dev,int sizeof_priv)
535 returninit_netdev(dev, sizeof_priv,"fc%d", fc_setup);
538 intregister_fcdev(struct net_device *dev)
540 dev_init_buffers(dev);
541 if(dev->init && dev->init(dev) !=0) {
542 unregister_fcdev(dev);
543 return-EIO;
545 return0;
548 voidunregister_fcdev(struct net_device *dev)
550 rtnl_lock();
551 unregister_netdevice(dev);
552 rtnl_unlock();
555 #endif/* CONFIG_NET_FC */
close