[WIRELESS] cfg80211: Fix locking in wiphy_new.
[linux-2.6.git] / net / wireless / core.c
blob24a21add0ba6dba234a4658ebc2c4702a5c1961e
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 "core.h"
20 #include "sysfs.h"
22 /* name for sysfs, %d is appended */
23 #define PHY_NAME "phy"
25 MODULE_AUTHOR("Johannes Berg");
26 MODULE_LICENSE("GPL");
27 MODULE_DESCRIPTION("wireless configuration support");
29 /* RCU might be appropriate here since we usually
30 * only read the list, and that can happen quite
31 * often because we need to do it for each command */
32 LIST_HEAD(cfg80211_drv_list);
33 DEFINE_MUTEX(cfg80211_drv_mutex);
34 static int wiphy_counter;
36 /* for debugfs */
37 static struct dentry *ieee80211_debugfs_dir;
39 /* exported functions */
41 struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv)
43 struct cfg80211_registered_device *drv;
44 int alloc_size;
46 alloc_size = sizeof(*drv) + sizeof_priv;
48 drv = kzalloc(alloc_size, GFP_KERNEL);
49 if (!drv)
50 return NULL;
52 drv->ops = ops;
54 mutex_lock(&cfg80211_drv_mutex);
56 drv->idx = wiphy_counter;
58 /* now increase counter for the next device unless
59 * it has wrapped previously */
60 if (wiphy_counter >= 0)
61 wiphy_counter++;
63 mutex_unlock(&cfg80211_drv_mutex);
65 if (unlikely(drv->idx < 0)) {
66 /* ugh, wrapped! */
67 kfree(drv);
68 return NULL;
71 /* give it a proper name */
72 snprintf(drv->wiphy.dev.bus_id, BUS_ID_SIZE,
73 PHY_NAME "%d", drv->idx);
75 mutex_init(&drv->mtx);
76 mutex_init(&drv->devlist_mtx);
77 INIT_LIST_HEAD(&drv->netdev_list);
79 device_initialize(&drv->wiphy.dev);
80 drv->wiphy.dev.class = &ieee80211_class;
81 drv->wiphy.dev.platform_data = drv;
83 return &drv->wiphy;
85 EXPORT_SYMBOL(wiphy_new);
87 int wiphy_register(struct wiphy *wiphy)
89 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
90 int res;
92 mutex_lock(&cfg80211_drv_mutex);
95 res = device_add(&drv->wiphy.dev);
96 if (res)
97 goto out_unlock;
99 list_add(&drv->list, &cfg80211_drv_list);
101 /* add to debugfs */
102 drv->wiphy.debugfsdir =
103 debugfs_create_dir(wiphy_name(&drv->wiphy),
104 ieee80211_debugfs_dir);
106 res = 0;
107 out_unlock:
108 mutex_unlock(&cfg80211_drv_mutex);
109 return res;
111 EXPORT_SYMBOL(wiphy_register);
113 void wiphy_unregister(struct wiphy *wiphy)
115 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
117 mutex_lock(&cfg80211_drv_mutex);
119 /* hold registered driver mutex during list removal as well
120 * to make sure no commands are in progress at the moment */
121 mutex_lock(&drv->mtx);
122 list_del(&drv->list);
123 mutex_unlock(&drv->mtx);
125 device_del(&drv->wiphy.dev);
126 debugfs_remove(drv->wiphy.debugfsdir);
128 mutex_unlock(&cfg80211_drv_mutex);
130 EXPORT_SYMBOL(wiphy_unregister);
132 void cfg80211_dev_free(struct cfg80211_registered_device *drv)
134 mutex_destroy(&drv->mtx);
135 mutex_destroy(&drv->devlist_mtx);
136 kfree(drv);
139 void wiphy_free(struct wiphy *wiphy)
141 put_device(&wiphy->dev);
143 EXPORT_SYMBOL(wiphy_free);
145 static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
146 unsigned long state,
147 void *ndev)
149 struct net_device *dev = ndev;
150 struct cfg80211_registered_device *rdev;
152 if (!dev->ieee80211_ptr)
153 return 0;
155 rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
157 switch (state) {
158 case NETDEV_REGISTER:
159 mutex_lock(&rdev->devlist_mtx);
160 list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list);
161 if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
162 "phy80211")) {
163 printk(KERN_ERR "wireless: failed to add phy80211 "
164 "symlink to netdev!\n");
166 dev->ieee80211_ptr->netdev = dev;
167 mutex_unlock(&rdev->devlist_mtx);
168 break;
169 case NETDEV_UNREGISTER:
170 mutex_lock(&rdev->devlist_mtx);
171 if (!list_empty(&dev->ieee80211_ptr->list)) {
172 sysfs_remove_link(&dev->dev.kobj, "phy80211");
173 list_del_init(&dev->ieee80211_ptr->list);
175 mutex_unlock(&rdev->devlist_mtx);
176 break;
179 return 0;
182 static struct notifier_block cfg80211_netdev_notifier = {
183 .notifier_call = cfg80211_netdev_notifier_call,
186 static int cfg80211_init(void)
188 int err = wiphy_sysfs_init();
189 if (err)
190 goto out_fail_sysfs;
192 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
193 if (err)
194 goto out_fail_notifier;
196 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
198 return 0;
200 out_fail_notifier:
201 wiphy_sysfs_exit();
202 out_fail_sysfs:
203 return err;
205 module_init(cfg80211_init);
207 static void cfg80211_exit(void)
209 debugfs_remove(ieee80211_debugfs_dir);
210 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
211 wiphy_sysfs_exit();
213 module_exit(cfg80211_exit);