Added !vmlinux.lds.h to .gitignore because it would otherwise be ignored.
[jz_linux_2.6.24.3.git] / net / wireless / core.c
blobfebc33bc9c09f644e21a37c1eebebf0c07b04e5b
1 /*
2 * This is the linux wireless configuration interface.
4 * Copyright 2006, 2007 Johannes Berg <johannes@sipsolutions.net>
5 */
7 #include <linux/if.h>
8 #include <linux/module.h>
9 #include <linux/err.h>
10 #include <linux/mutex.h>
11 #include <linux/list.h>
12 #include <linux/nl80211.h>
13 #include <linux/debugfs.h>
14 #include <linux/notifier.h>
15 #include <linux/device.h>
16 #include <net/genetlink.h>
17 #include <net/cfg80211.h>
18 #include <net/wireless.h>
19 #include "nl80211.h"
20 #include "core.h"
21 #include "sysfs.h"
23 /* name for sysfs, %d is appended */
24 #define PHY_NAME "phy"
26 MODULE_AUTHOR("Johannes Berg");
27 MODULE_LICENSE("GPL");
28 MODULE_DESCRIPTION("wireless configuration support");
30 /* RCU might be appropriate here since we usually
31 * only read the list, and that can happen quite
32 * often because we need to do it for each command */
33 LIST_HEAD(cfg80211_drv_list);
34 DEFINE_MUTEX(cfg80211_drv_mutex);
35 static int wiphy_counter;
37 /* for debugfs */
38 static struct dentry *ieee80211_debugfs_dir;
40 /* requires cfg80211_drv_mutex to be held! */
41 static struct cfg80211_registered_device *cfg80211_drv_by_wiphy(int wiphy)
43 struct cfg80211_registered_device *result = NULL, *drv;
45 list_for_each_entry(drv, &cfg80211_drv_list, list) {
46 if (drv->idx == wiphy) {
47 result = drv;
48 break;
52 return result;
55 /* requires cfg80211_drv_mutex to be held! */
56 static struct cfg80211_registered_device *
57 __cfg80211_drv_from_info(struct genl_info *info)
59 int ifindex;
60 struct cfg80211_registered_device *bywiphy = NULL, *byifidx = NULL;
61 struct net_device *dev;
62 int err = -EINVAL;
64 if (info->attrs[NL80211_ATTR_WIPHY]) {
65 bywiphy = cfg80211_drv_by_wiphy(
66 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
67 err = -ENODEV;
70 if (info->attrs[NL80211_ATTR_IFINDEX]) {
71 ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
72 dev = dev_get_by_index(&init_net, ifindex);
73 if (dev) {
74 if (dev->ieee80211_ptr)
75 byifidx =
76 wiphy_to_dev(dev->ieee80211_ptr->wiphy);
77 dev_put(dev);
79 err = -ENODEV;
82 if (bywiphy && byifidx) {
83 if (bywiphy != byifidx)
84 return ERR_PTR(-EINVAL);
85 else
86 return bywiphy; /* == byifidx */
88 if (bywiphy)
89 return bywiphy;
91 if (byifidx)
92 return byifidx;
94 return ERR_PTR(err);
97 struct cfg80211_registered_device *
98 cfg80211_get_dev_from_info(struct genl_info *info)
100 struct cfg80211_registered_device *drv;
102 mutex_lock(&cfg80211_drv_mutex);
103 drv = __cfg80211_drv_from_info(info);
105 /* if it is not an error we grab the lock on
106 * it to assure it won't be going away while
107 * we operate on it */
108 if (!IS_ERR(drv))
109 mutex_lock(&drv->mtx);
111 mutex_unlock(&cfg80211_drv_mutex);
113 return drv;
116 struct cfg80211_registered_device *
117 cfg80211_get_dev_from_ifindex(int ifindex)
119 struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV);
120 struct net_device *dev;
122 mutex_lock(&cfg80211_drv_mutex);
123 dev = dev_get_by_index(&init_net, ifindex);
124 if (!dev)
125 goto out;
126 if (dev->ieee80211_ptr) {
127 drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
128 mutex_lock(&drv->mtx);
129 } else
130 drv = ERR_PTR(-ENODEV);
131 dev_put(dev);
132 out:
133 mutex_unlock(&cfg80211_drv_mutex);
134 return drv;
137 void cfg80211_put_dev(struct cfg80211_registered_device *drv)
139 BUG_ON(IS_ERR(drv));
140 mutex_unlock(&drv->mtx);
143 int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
144 char *newname)
146 int idx, taken = -1, result, digits;
148 /* prohibit calling the thing phy%d when %d is not its number */
149 sscanf(newname, PHY_NAME "%d%n", &idx, &taken);
150 if (taken == strlen(newname) && idx != rdev->idx) {
151 /* count number of places needed to print idx */
152 digits = 1;
153 while (idx /= 10)
154 digits++;
156 * deny the name if it is phy<idx> where <idx> is printed
157 * without leading zeroes. taken == strlen(newname) here
159 if (taken == strlen(PHY_NAME) + digits)
160 return -EINVAL;
163 /* this will check for collisions */
164 result = device_rename(&rdev->wiphy.dev, newname);
165 if (result)
166 return result;
168 if (!debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
169 rdev->wiphy.debugfsdir,
170 rdev->wiphy.debugfsdir->d_parent,
171 newname))
172 printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
173 newname);
175 nl80211_notify_dev_rename(rdev);
177 return 0;
180 /* exported functions */
182 struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv)
184 struct cfg80211_registered_device *drv;
185 int alloc_size;
187 alloc_size = sizeof(*drv) + sizeof_priv;
189 drv = kzalloc(alloc_size, GFP_KERNEL);
190 if (!drv)
191 return NULL;
193 drv->ops = ops;
195 mutex_lock(&cfg80211_drv_mutex);
197 drv->idx = wiphy_counter;
199 /* now increase counter for the next device unless
200 * it has wrapped previously */
201 if (wiphy_counter >= 0)
202 wiphy_counter++;
204 mutex_unlock(&cfg80211_drv_mutex);
206 if (unlikely(drv->idx < 0)) {
207 /* ugh, wrapped! */
208 kfree(drv);
209 return NULL;
212 /* give it a proper name */
213 snprintf(drv->wiphy.dev.bus_id, BUS_ID_SIZE,
214 PHY_NAME "%d", drv->idx);
216 mutex_init(&drv->mtx);
217 mutex_init(&drv->devlist_mtx);
218 INIT_LIST_HEAD(&drv->netdev_list);
220 device_initialize(&drv->wiphy.dev);
221 drv->wiphy.dev.class = &ieee80211_class;
222 drv->wiphy.dev.platform_data = drv;
224 return &drv->wiphy;
226 EXPORT_SYMBOL(wiphy_new);
228 int wiphy_register(struct wiphy *wiphy)
230 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
231 int res;
233 mutex_lock(&cfg80211_drv_mutex);
235 res = device_add(&drv->wiphy.dev);
236 if (res)
237 goto out_unlock;
239 list_add(&drv->list, &cfg80211_drv_list);
241 /* add to debugfs */
242 drv->wiphy.debugfsdir =
243 debugfs_create_dir(wiphy_name(&drv->wiphy),
244 ieee80211_debugfs_dir);
246 res = 0;
247 out_unlock:
248 mutex_unlock(&cfg80211_drv_mutex);
249 return res;
251 EXPORT_SYMBOL(wiphy_register);
253 void wiphy_unregister(struct wiphy *wiphy)
255 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
257 /* protect the device list */
258 mutex_lock(&cfg80211_drv_mutex);
260 BUG_ON(!list_empty(&drv->netdev_list));
263 * Try to grab drv->mtx. If a command is still in progress,
264 * hopefully the driver will refuse it since it's tearing
265 * down the device already. We wait for this command to complete
266 * before unlinking the item from the list.
267 * Note: as codified by the BUG_ON above we cannot get here if
268 * a virtual interface is still associated. Hence, we can only
269 * get to lock contention here if userspace issues a command
270 * that identified the hardware by wiphy index.
272 mutex_lock(&drv->mtx);
273 /* unlock again before freeing */
274 mutex_unlock(&drv->mtx);
276 list_del(&drv->list);
277 device_del(&drv->wiphy.dev);
278 debugfs_remove(drv->wiphy.debugfsdir);
280 mutex_unlock(&cfg80211_drv_mutex);
282 EXPORT_SYMBOL(wiphy_unregister);
284 void cfg80211_dev_free(struct cfg80211_registered_device *drv)
286 mutex_destroy(&drv->mtx);
287 mutex_destroy(&drv->devlist_mtx);
288 kfree(drv);
291 void wiphy_free(struct wiphy *wiphy)
293 put_device(&wiphy->dev);
295 EXPORT_SYMBOL(wiphy_free);
297 static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
298 unsigned long state,
299 void *ndev)
301 struct net_device *dev = ndev;
302 struct cfg80211_registered_device *rdev;
304 if (!dev->ieee80211_ptr)
305 return 0;
307 rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
309 switch (state) {
310 case NETDEV_REGISTER:
311 mutex_lock(&rdev->devlist_mtx);
312 list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list);
313 if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
314 "phy80211")) {
315 printk(KERN_ERR "wireless: failed to add phy80211 "
316 "symlink to netdev!\n");
318 dev->ieee80211_ptr->netdev = dev;
319 mutex_unlock(&rdev->devlist_mtx);
320 break;
321 case NETDEV_UNREGISTER:
322 mutex_lock(&rdev->devlist_mtx);
323 if (!list_empty(&dev->ieee80211_ptr->list)) {
324 sysfs_remove_link(&dev->dev.kobj, "phy80211");
325 list_del_init(&dev->ieee80211_ptr->list);
327 mutex_unlock(&rdev->devlist_mtx);
328 break;
331 return 0;
334 static struct notifier_block cfg80211_netdev_notifier = {
335 .notifier_call = cfg80211_netdev_notifier_call,
338 static int cfg80211_init(void)
340 int err = wiphy_sysfs_init();
341 if (err)
342 goto out_fail_sysfs;
344 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
345 if (err)
346 goto out_fail_notifier;
348 err = nl80211_init();
349 if (err)
350 goto out_fail_nl80211;
352 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
354 return 0;
356 out_fail_nl80211:
357 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
358 out_fail_notifier:
359 wiphy_sysfs_exit();
360 out_fail_sysfs:
361 return err;
363 subsys_initcall(cfg80211_init);
365 static void cfg80211_exit(void)
367 debugfs_remove(ieee80211_debugfs_dir);
368 nl80211_exit();
369 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
370 wiphy_sysfs_exit();
372 module_exit(cfg80211_exit);