ocfs2: Dirty the entire first bucket in ocfs2_extend_xattr_bucket()
[linux-2.6/mini2440.git] / net / wireless / core.c
blobb96fc0c3f1c446132cf71e3e0464a6a2ff4edd84
1 /*
2 * This is the linux wireless configuration interface.
4 * Copyright 2006-2008 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);
36 /* for debugfs */
37 static struct dentry *ieee80211_debugfs_dir;
39 /* requires cfg80211_drv_mutex to be held! */
40 static struct cfg80211_registered_device *cfg80211_drv_by_wiphy(int wiphy)
42 struct cfg80211_registered_device *result = NULL, *drv;
44 list_for_each_entry(drv, &cfg80211_drv_list, list) {
45 if (drv->idx == wiphy) {
46 result = drv;
47 break;
51 return result;
54 /* requires cfg80211_drv_mutex to be held! */
55 static struct cfg80211_registered_device *
56 __cfg80211_drv_from_info(struct genl_info *info)
58 int ifindex;
59 struct cfg80211_registered_device *bywiphy = NULL, *byifidx = NULL;
60 struct net_device *dev;
61 int err = -EINVAL;
63 if (info->attrs[NL80211_ATTR_WIPHY]) {
64 bywiphy = cfg80211_drv_by_wiphy(
65 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
66 err = -ENODEV;
69 if (info->attrs[NL80211_ATTR_IFINDEX]) {
70 ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
71 dev = dev_get_by_index(&init_net, ifindex);
72 if (dev) {
73 if (dev->ieee80211_ptr)
74 byifidx =
75 wiphy_to_dev(dev->ieee80211_ptr->wiphy);
76 dev_put(dev);
78 err = -ENODEV;
81 if (bywiphy && byifidx) {
82 if (bywiphy != byifidx)
83 return ERR_PTR(-EINVAL);
84 else
85 return bywiphy; /* == byifidx */
87 if (bywiphy)
88 return bywiphy;
90 if (byifidx)
91 return byifidx;
93 return ERR_PTR(err);
96 struct cfg80211_registered_device *
97 cfg80211_get_dev_from_info(struct genl_info *info)
99 struct cfg80211_registered_device *drv;
101 mutex_lock(&cfg80211_drv_mutex);
102 drv = __cfg80211_drv_from_info(info);
104 /* if it is not an error we grab the lock on
105 * it to assure it won't be going away while
106 * we operate on it */
107 if (!IS_ERR(drv))
108 mutex_lock(&drv->mtx);
110 mutex_unlock(&cfg80211_drv_mutex);
112 return drv;
115 struct cfg80211_registered_device *
116 cfg80211_get_dev_from_ifindex(int ifindex)
118 struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV);
119 struct net_device *dev;
121 mutex_lock(&cfg80211_drv_mutex);
122 dev = dev_get_by_index(&init_net, ifindex);
123 if (!dev)
124 goto out;
125 if (dev->ieee80211_ptr) {
126 drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
127 mutex_lock(&drv->mtx);
128 } else
129 drv = ERR_PTR(-ENODEV);
130 dev_put(dev);
131 out:
132 mutex_unlock(&cfg80211_drv_mutex);
133 return drv;
136 void cfg80211_put_dev(struct cfg80211_registered_device *drv)
138 BUG_ON(IS_ERR(drv));
139 mutex_unlock(&drv->mtx);
142 int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
143 char *newname)
145 struct cfg80211_registered_device *drv;
146 int idx, taken = -1, result, digits;
148 mutex_lock(&cfg80211_drv_mutex);
150 /* prohibit calling the thing phy%d when %d is not its number */
151 sscanf(newname, PHY_NAME "%d%n", &idx, &taken);
152 if (taken == strlen(newname) && idx != rdev->idx) {
153 /* count number of places needed to print idx */
154 digits = 1;
155 while (idx /= 10)
156 digits++;
158 * deny the name if it is phy<idx> where <idx> is printed
159 * without leading zeroes. taken == strlen(newname) here
161 result = -EINVAL;
162 if (taken == strlen(PHY_NAME) + digits)
163 goto out_unlock;
167 /* Ignore nop renames */
168 result = 0;
169 if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
170 goto out_unlock;
172 /* Ensure another device does not already have this name. */
173 list_for_each_entry(drv, &cfg80211_drv_list, list) {
174 result = -EINVAL;
175 if (strcmp(newname, dev_name(&drv->wiphy.dev)) == 0)
176 goto out_unlock;
179 /* this will only check for collisions in sysfs
180 * which is not even always compiled in.
182 result = device_rename(&rdev->wiphy.dev, newname);
183 if (result)
184 goto out_unlock;
186 if (rdev->wiphy.debugfsdir &&
187 !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
188 rdev->wiphy.debugfsdir,
189 rdev->wiphy.debugfsdir->d_parent,
190 newname))
191 printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
192 newname);
194 result = 0;
195 out_unlock:
196 mutex_unlock(&cfg80211_drv_mutex);
197 if (result == 0)
198 nl80211_notify_dev_rename(rdev);
200 return result;
203 /* exported functions */
205 struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv)
207 static int wiphy_counter;
209 struct cfg80211_registered_device *drv;
210 int alloc_size;
212 WARN_ON(!ops->add_key && ops->del_key);
213 WARN_ON(ops->add_key && !ops->del_key);
215 alloc_size = sizeof(*drv) + sizeof_priv;
217 drv = kzalloc(alloc_size, GFP_KERNEL);
218 if (!drv)
219 return NULL;
221 drv->ops = ops;
223 mutex_lock(&cfg80211_drv_mutex);
225 drv->idx = wiphy_counter++;
227 if (unlikely(drv->idx < 0)) {
228 wiphy_counter--;
229 mutex_unlock(&cfg80211_drv_mutex);
230 /* ugh, wrapped! */
231 kfree(drv);
232 return NULL;
235 mutex_unlock(&cfg80211_drv_mutex);
237 /* give it a proper name */
238 dev_set_name(&drv->wiphy.dev, PHY_NAME "%d", drv->idx);
240 mutex_init(&drv->mtx);
241 mutex_init(&drv->devlist_mtx);
242 INIT_LIST_HEAD(&drv->netdev_list);
244 device_initialize(&drv->wiphy.dev);
245 drv->wiphy.dev.class = &ieee80211_class;
246 drv->wiphy.dev.platform_data = drv;
248 return &drv->wiphy;
250 EXPORT_SYMBOL(wiphy_new);
252 int wiphy_register(struct wiphy *wiphy)
254 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
255 int res;
256 enum ieee80211_band band;
257 struct ieee80211_supported_band *sband;
258 bool have_band = false;
259 int i;
260 u16 ifmodes = wiphy->interface_modes;
262 /* sanity check ifmodes */
263 WARN_ON(!ifmodes);
264 ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1;
265 if (WARN_ON(ifmodes != wiphy->interface_modes))
266 wiphy->interface_modes = ifmodes;
268 /* sanity check supported bands/channels */
269 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
270 sband = wiphy->bands[band];
271 if (!sband)
272 continue;
274 sband->band = band;
276 if (!sband->n_channels || !sband->n_bitrates) {
277 WARN_ON(1);
278 return -EINVAL;
281 for (i = 0; i < sband->n_channels; i++) {
282 sband->channels[i].orig_flags =
283 sband->channels[i].flags;
284 sband->channels[i].orig_mag =
285 sband->channels[i].max_antenna_gain;
286 sband->channels[i].orig_mpwr =
287 sband->channels[i].max_power;
288 sband->channels[i].band = band;
291 have_band = true;
294 if (!have_band) {
295 WARN_ON(1);
296 return -EINVAL;
299 /* check and set up bitrates */
300 ieee80211_set_bitrate_flags(wiphy);
302 mutex_lock(&cfg80211_drv_mutex);
304 /* set up regulatory info */
305 wiphy_update_regulatory(wiphy, REGDOM_SET_BY_CORE);
307 res = device_add(&drv->wiphy.dev);
308 if (res)
309 goto out_unlock;
311 list_add(&drv->list, &cfg80211_drv_list);
313 /* add to debugfs */
314 drv->wiphy.debugfsdir =
315 debugfs_create_dir(wiphy_name(&drv->wiphy),
316 ieee80211_debugfs_dir);
317 if (IS_ERR(drv->wiphy.debugfsdir))
318 drv->wiphy.debugfsdir = NULL;
320 res = 0;
321 out_unlock:
322 mutex_unlock(&cfg80211_drv_mutex);
323 return res;
325 EXPORT_SYMBOL(wiphy_register);
327 void wiphy_unregister(struct wiphy *wiphy)
329 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
331 /* protect the device list */
332 mutex_lock(&cfg80211_drv_mutex);
334 BUG_ON(!list_empty(&drv->netdev_list));
337 * Try to grab drv->mtx. If a command is still in progress,
338 * hopefully the driver will refuse it since it's tearing
339 * down the device already. We wait for this command to complete
340 * before unlinking the item from the list.
341 * Note: as codified by the BUG_ON above we cannot get here if
342 * a virtual interface is still associated. Hence, we can only
343 * get to lock contention here if userspace issues a command
344 * that identified the hardware by wiphy index.
346 mutex_lock(&drv->mtx);
347 /* unlock again before freeing */
348 mutex_unlock(&drv->mtx);
350 /* If this device got a regulatory hint tell core its
351 * free to listen now to a new shiny device regulatory hint */
352 reg_device_remove(wiphy);
354 list_del(&drv->list);
355 device_del(&drv->wiphy.dev);
356 debugfs_remove(drv->wiphy.debugfsdir);
358 mutex_unlock(&cfg80211_drv_mutex);
360 EXPORT_SYMBOL(wiphy_unregister);
362 void cfg80211_dev_free(struct cfg80211_registered_device *drv)
364 mutex_destroy(&drv->mtx);
365 mutex_destroy(&drv->devlist_mtx);
366 kfree(drv);
369 void wiphy_free(struct wiphy *wiphy)
371 put_device(&wiphy->dev);
373 EXPORT_SYMBOL(wiphy_free);
375 static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
376 unsigned long state,
377 void *ndev)
379 struct net_device *dev = ndev;
380 struct cfg80211_registered_device *rdev;
382 if (!dev->ieee80211_ptr)
383 return 0;
385 rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
387 WARN_ON(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_UNSPECIFIED);
389 switch (state) {
390 case NETDEV_REGISTER:
391 mutex_lock(&rdev->devlist_mtx);
392 list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list);
393 if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
394 "phy80211")) {
395 printk(KERN_ERR "wireless: failed to add phy80211 "
396 "symlink to netdev!\n");
398 dev->ieee80211_ptr->netdev = dev;
399 mutex_unlock(&rdev->devlist_mtx);
400 break;
401 case NETDEV_UNREGISTER:
402 mutex_lock(&rdev->devlist_mtx);
403 if (!list_empty(&dev->ieee80211_ptr->list)) {
404 sysfs_remove_link(&dev->dev.kobj, "phy80211");
405 list_del_init(&dev->ieee80211_ptr->list);
407 mutex_unlock(&rdev->devlist_mtx);
408 break;
411 return 0;
414 static struct notifier_block cfg80211_netdev_notifier = {
415 .notifier_call = cfg80211_netdev_notifier_call,
418 static int cfg80211_init(void)
420 int err;
422 err = wiphy_sysfs_init();
423 if (err)
424 goto out_fail_sysfs;
426 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
427 if (err)
428 goto out_fail_notifier;
430 err = nl80211_init();
431 if (err)
432 goto out_fail_nl80211;
434 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
436 err = regulatory_init();
437 if (err)
438 goto out_fail_reg;
440 return 0;
442 out_fail_reg:
443 debugfs_remove(ieee80211_debugfs_dir);
444 out_fail_nl80211:
445 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
446 out_fail_notifier:
447 wiphy_sysfs_exit();
448 out_fail_sysfs:
449 return err;
452 subsys_initcall(cfg80211_init);
454 static void cfg80211_exit(void)
456 debugfs_remove(ieee80211_debugfs_dir);
457 nl80211_exit();
458 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
459 wiphy_sysfs_exit();
460 regulatory_exit();
462 module_exit(cfg80211_exit);