kernel.org linux-2.6.10
[linux-2.6.git] / drivers / net / net_init.c
1 /* net_init.c: Initialization for network devices. */
2 /*
3         Written 1993,1994,1995 by Donald Becker.
4
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
9
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.
15
16         Modifications/additions by Bjorn Ekwall <bj0rn@blox.se>:
17                 ethdev_index[MAX_ETH_CARDS]
18                 register_netdev() / unregister_netdev()
19                 
20         Modifications by Wolfgang Walter
21                 Use dev_close cleanly so we always shut things down tidily.
22                 
23         Changed 29/10/95, Alan Cox to pass sockaddr's around for mac addresses.
24         
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. 
27         
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
33
34 */
35
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>
54
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.
63
64    Given that almost all of these functions are handled in the current
65    socket-based scheme, putting ethercard devices in /dev/ seems pointless.
66    
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]
70 */
71
72
73 struct net_device *alloc_netdev(int sizeof_priv, const char *mask,
74                                        void (*setup)(struct net_device *))
75 {
76         void *p;
77         struct net_device *dev;
78         int alloc_size;
79
80         /* ensure 32-byte alignment of both the device and private area */
81
82         alloc_size = (sizeof(struct net_device) + NETDEV_ALIGN_CONST)
83                         & ~NETDEV_ALIGN_CONST;
84         alloc_size += sizeof_priv + NETDEV_ALIGN_CONST;
85
86         p = kmalloc (alloc_size, GFP_KERNEL);
87         if (!p) {
88                 printk(KERN_ERR "alloc_dev: Unable to allocate device.\n");
89                 return NULL;
90         }
91
92         memset(p, 0, alloc_size);
93
94         dev = (struct net_device *)(((long)p + NETDEV_ALIGN_CONST)
95                                 & ~NETDEV_ALIGN_CONST);
96         dev->padded = (char *)dev - (char *)p;
97
98         if (sizeof_priv)
99                 dev->priv = netdev_priv(dev);
100
101         setup(dev);
102         strcpy(dev->name, mask);
103
104         return dev;
105 }
106 EXPORT_SYMBOL(alloc_netdev);
107
108 int register_netdev(struct net_device *dev)
109 {
110         int err;
111
112         rtnl_lock();
113
114         /*
115          *      If the name is a format string the caller wants us to
116          *      do a name allocation
117          */
118          
119         if (strchr(dev->name, '%'))
120         {
121                 err = dev_alloc_name(dev, dev->name);
122                 if (err < 0)
123                         goto out;
124         }
125         
126         /*
127          *      Back compatibility hook. Kill this one in 2.5
128          */
129         
130         if (dev->name[0]==0 || dev->name[0]==' ')
131         {
132                 err = dev_alloc_name(dev, "eth%d");
133                 if (err < 0)
134                         goto out;
135         }
136
137         err = register_netdevice(dev);
138
139 out:
140         rtnl_unlock();
141         return err;
142 }
143
144 void unregister_netdev(struct net_device *dev)
145 {
146         rtnl_lock();
147         unregister_netdevice(dev);
148         rtnl_unlock();
149 }
150
151 EXPORT_SYMBOL(register_netdev);
152 EXPORT_SYMBOL(unregister_netdev);