Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This is the linux wireless configuration interface. |
| 3 | * |
Luis R. Rodriguez | f59ac04 | 2008-08-29 16:26:43 -0700 | [diff] [blame] | 4 | * Copyright 2006-2008 Johannes Berg <johannes@sipsolutions.net> |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 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> |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 19 | #include "nl80211.h" |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 20 | #include "core.h" |
| 21 | #include "sysfs.h" |
| 22 | |
| 23 | /* name for sysfs, %d is appended */ |
| 24 | #define PHY_NAME "phy" |
| 25 | |
| 26 | MODULE_AUTHOR("Johannes Berg"); |
| 27 | MODULE_LICENSE("GPL"); |
| 28 | MODULE_DESCRIPTION("wireless configuration support"); |
| 29 | |
| 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); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 35 | |
| 36 | /* for debugfs */ |
| 37 | static struct dentry *ieee80211_debugfs_dir; |
| 38 | |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 39 | /* requires cfg80211_drv_mutex to be held! */ |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 40 | static struct cfg80211_registered_device * |
| 41 | cfg80211_drv_by_wiphy_idx(int wiphy_idx) |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 42 | { |
| 43 | struct cfg80211_registered_device *result = NULL, *drv; |
| 44 | |
Luis R. Rodriguez | 85fd129 | 2009-02-21 00:04:20 -0500 | [diff] [blame^] | 45 | if (!wiphy_idx_valid(wiphy_idx)) |
| 46 | return NULL; |
| 47 | |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 48 | list_for_each_entry(drv, &cfg80211_drv_list, list) { |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 49 | if (drv->wiphy_idx == wiphy_idx) { |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 50 | result = drv; |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return result; |
| 56 | } |
| 57 | |
| 58 | /* requires cfg80211_drv_mutex to be held! */ |
| 59 | static struct cfg80211_registered_device * |
| 60 | __cfg80211_drv_from_info(struct genl_info *info) |
| 61 | { |
| 62 | int ifindex; |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 63 | struct cfg80211_registered_device *bywiphyidx = NULL, *byifidx = NULL; |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 64 | struct net_device *dev; |
| 65 | int err = -EINVAL; |
| 66 | |
| 67 | if (info->attrs[NL80211_ATTR_WIPHY]) { |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 68 | bywiphyidx = cfg80211_drv_by_wiphy_idx( |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 69 | nla_get_u32(info->attrs[NL80211_ATTR_WIPHY])); |
| 70 | err = -ENODEV; |
| 71 | } |
| 72 | |
| 73 | if (info->attrs[NL80211_ATTR_IFINDEX]) { |
| 74 | ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]); |
| 75 | dev = dev_get_by_index(&init_net, ifindex); |
| 76 | if (dev) { |
| 77 | if (dev->ieee80211_ptr) |
| 78 | byifidx = |
| 79 | wiphy_to_dev(dev->ieee80211_ptr->wiphy); |
| 80 | dev_put(dev); |
| 81 | } |
| 82 | err = -ENODEV; |
| 83 | } |
| 84 | |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 85 | if (bywiphyidx && byifidx) { |
| 86 | if (bywiphyidx != byifidx) |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 87 | return ERR_PTR(-EINVAL); |
| 88 | else |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 89 | return bywiphyidx; /* == byifidx */ |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 90 | } |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 91 | if (bywiphyidx) |
| 92 | return bywiphyidx; |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 93 | |
| 94 | if (byifidx) |
| 95 | return byifidx; |
| 96 | |
| 97 | return ERR_PTR(err); |
| 98 | } |
| 99 | |
| 100 | struct cfg80211_registered_device * |
| 101 | cfg80211_get_dev_from_info(struct genl_info *info) |
| 102 | { |
| 103 | struct cfg80211_registered_device *drv; |
| 104 | |
| 105 | mutex_lock(&cfg80211_drv_mutex); |
| 106 | drv = __cfg80211_drv_from_info(info); |
| 107 | |
| 108 | /* if it is not an error we grab the lock on |
| 109 | * it to assure it won't be going away while |
| 110 | * we operate on it */ |
| 111 | if (!IS_ERR(drv)) |
| 112 | mutex_lock(&drv->mtx); |
| 113 | |
| 114 | mutex_unlock(&cfg80211_drv_mutex); |
| 115 | |
| 116 | return drv; |
| 117 | } |
| 118 | |
| 119 | struct cfg80211_registered_device * |
| 120 | cfg80211_get_dev_from_ifindex(int ifindex) |
| 121 | { |
| 122 | struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV); |
| 123 | struct net_device *dev; |
| 124 | |
| 125 | mutex_lock(&cfg80211_drv_mutex); |
| 126 | dev = dev_get_by_index(&init_net, ifindex); |
| 127 | if (!dev) |
| 128 | goto out; |
| 129 | if (dev->ieee80211_ptr) { |
| 130 | drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy); |
| 131 | mutex_lock(&drv->mtx); |
| 132 | } else |
| 133 | drv = ERR_PTR(-ENODEV); |
| 134 | dev_put(dev); |
| 135 | out: |
| 136 | mutex_unlock(&cfg80211_drv_mutex); |
| 137 | return drv; |
| 138 | } |
| 139 | |
| 140 | void cfg80211_put_dev(struct cfg80211_registered_device *drv) |
| 141 | { |
| 142 | BUG_ON(IS_ERR(drv)); |
| 143 | mutex_unlock(&drv->mtx); |
| 144 | } |
| 145 | |
| 146 | int cfg80211_dev_rename(struct cfg80211_registered_device *rdev, |
| 147 | char *newname) |
| 148 | { |
Eric W. Biederman | 2940bb6 | 2008-05-08 14:30:18 -0700 | [diff] [blame] | 149 | struct cfg80211_registered_device *drv; |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 150 | int wiphy_idx, taken = -1, result, digits; |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 151 | |
Eric W. Biederman | 2940bb6 | 2008-05-08 14:30:18 -0700 | [diff] [blame] | 152 | mutex_lock(&cfg80211_drv_mutex); |
| 153 | |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 154 | /* prohibit calling the thing phy%d when %d is not its number */ |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 155 | sscanf(newname, PHY_NAME "%d%n", &wiphy_idx, &taken); |
| 156 | if (taken == strlen(newname) && wiphy_idx != rdev->wiphy_idx) { |
| 157 | /* count number of places needed to print wiphy_idx */ |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 158 | digits = 1; |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 159 | while (wiphy_idx /= 10) |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 160 | digits++; |
| 161 | /* |
| 162 | * deny the name if it is phy<idx> where <idx> is printed |
| 163 | * without leading zeroes. taken == strlen(newname) here |
| 164 | */ |
Eric W. Biederman | 2940bb6 | 2008-05-08 14:30:18 -0700 | [diff] [blame] | 165 | result = -EINVAL; |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 166 | if (taken == strlen(PHY_NAME) + digits) |
Eric W. Biederman | 2940bb6 | 2008-05-08 14:30:18 -0700 | [diff] [blame] | 167 | goto out_unlock; |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 168 | } |
| 169 | |
Eric W. Biederman | 2940bb6 | 2008-05-08 14:30:18 -0700 | [diff] [blame] | 170 | |
| 171 | /* Ignore nop renames */ |
| 172 | result = 0; |
| 173 | if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0) |
| 174 | goto out_unlock; |
| 175 | |
| 176 | /* Ensure another device does not already have this name. */ |
| 177 | list_for_each_entry(drv, &cfg80211_drv_list, list) { |
| 178 | result = -EINVAL; |
| 179 | if (strcmp(newname, dev_name(&drv->wiphy.dev)) == 0) |
| 180 | goto out_unlock; |
| 181 | } |
| 182 | |
| 183 | /* this will only check for collisions in sysfs |
| 184 | * which is not even always compiled in. |
| 185 | */ |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 186 | result = device_rename(&rdev->wiphy.dev, newname); |
| 187 | if (result) |
Eric W. Biederman | 2940bb6 | 2008-05-08 14:30:18 -0700 | [diff] [blame] | 188 | goto out_unlock; |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 189 | |
Johannes Berg | 33c0360 | 2008-10-08 10:23:48 +0200 | [diff] [blame] | 190 | if (rdev->wiphy.debugfsdir && |
| 191 | !debugfs_rename(rdev->wiphy.debugfsdir->d_parent, |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 192 | rdev->wiphy.debugfsdir, |
| 193 | rdev->wiphy.debugfsdir->d_parent, |
| 194 | newname)) |
| 195 | printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n", |
| 196 | newname); |
| 197 | |
Eric W. Biederman | 2940bb6 | 2008-05-08 14:30:18 -0700 | [diff] [blame] | 198 | result = 0; |
| 199 | out_unlock: |
| 200 | mutex_unlock(&cfg80211_drv_mutex); |
| 201 | if (result == 0) |
| 202 | nl80211_notify_dev_rename(rdev); |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 203 | |
Eric W. Biederman | 2940bb6 | 2008-05-08 14:30:18 -0700 | [diff] [blame] | 204 | return result; |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 205 | } |
| 206 | |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 207 | /* exported functions */ |
| 208 | |
| 209 | struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv) |
| 210 | { |
Denis ChengRq | 638af07 | 2008-09-23 02:35:37 +0800 | [diff] [blame] | 211 | static int wiphy_counter; |
| 212 | |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 213 | struct cfg80211_registered_device *drv; |
| 214 | int alloc_size; |
| 215 | |
Johannes Berg | 41ade00 | 2007-12-19 02:03:29 +0100 | [diff] [blame] | 216 | WARN_ON(!ops->add_key && ops->del_key); |
| 217 | WARN_ON(ops->add_key && !ops->del_key); |
| 218 | |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 219 | alloc_size = sizeof(*drv) + sizeof_priv; |
| 220 | |
| 221 | drv = kzalloc(alloc_size, GFP_KERNEL); |
| 222 | if (!drv) |
| 223 | return NULL; |
| 224 | |
| 225 | drv->ops = ops; |
| 226 | |
| 227 | mutex_lock(&cfg80211_drv_mutex); |
| 228 | |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 229 | drv->wiphy_idx = wiphy_counter++; |
Johannes Berg | a4d73ee | 2007-04-26 20:50:35 -0700 | [diff] [blame] | 230 | |
Luis R. Rodriguez | 85fd129 | 2009-02-21 00:04:20 -0500 | [diff] [blame^] | 231 | if (unlikely(!wiphy_idx_valid(drv->wiphy_idx))) { |
Denis ChengRq | 638af07 | 2008-09-23 02:35:37 +0800 | [diff] [blame] | 232 | wiphy_counter--; |
| 233 | mutex_unlock(&cfg80211_drv_mutex); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 234 | /* ugh, wrapped! */ |
| 235 | kfree(drv); |
| 236 | return NULL; |
| 237 | } |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 238 | |
Denis ChengRq | 638af07 | 2008-09-23 02:35:37 +0800 | [diff] [blame] | 239 | mutex_unlock(&cfg80211_drv_mutex); |
| 240 | |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 241 | /* give it a proper name */ |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 242 | dev_set_name(&drv->wiphy.dev, PHY_NAME "%d", drv->wiphy_idx); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 243 | |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 244 | mutex_init(&drv->mtx); |
| 245 | mutex_init(&drv->devlist_mtx); |
| 246 | INIT_LIST_HEAD(&drv->netdev_list); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 247 | spin_lock_init(&drv->bss_lock); |
| 248 | INIT_LIST_HEAD(&drv->bss_list); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 249 | |
| 250 | device_initialize(&drv->wiphy.dev); |
| 251 | drv->wiphy.dev.class = &ieee80211_class; |
| 252 | drv->wiphy.dev.platform_data = drv; |
| 253 | |
| 254 | return &drv->wiphy; |
| 255 | } |
| 256 | EXPORT_SYMBOL(wiphy_new); |
| 257 | |
| 258 | int wiphy_register(struct wiphy *wiphy) |
| 259 | { |
| 260 | struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy); |
| 261 | int res; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 262 | enum ieee80211_band band; |
| 263 | struct ieee80211_supported_band *sband; |
| 264 | bool have_band = false; |
| 265 | int i; |
Luis R. Rodriguez | f59ac04 | 2008-08-29 16:26:43 -0700 | [diff] [blame] | 266 | u16 ifmodes = wiphy->interface_modes; |
| 267 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 268 | if (WARN_ON(wiphy->max_scan_ssids < 1)) |
| 269 | return -EINVAL; |
| 270 | |
Luis R. Rodriguez | f59ac04 | 2008-08-29 16:26:43 -0700 | [diff] [blame] | 271 | /* sanity check ifmodes */ |
| 272 | WARN_ON(!ifmodes); |
| 273 | ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1; |
| 274 | if (WARN_ON(ifmodes != wiphy->interface_modes)) |
| 275 | wiphy->interface_modes = ifmodes; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 276 | |
| 277 | /* sanity check supported bands/channels */ |
| 278 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { |
| 279 | sband = wiphy->bands[band]; |
| 280 | if (!sband) |
| 281 | continue; |
| 282 | |
| 283 | sband->band = band; |
| 284 | |
Johannes Berg | 881d948 | 2009-01-21 15:13:48 +0100 | [diff] [blame] | 285 | if (WARN_ON(!sband->n_channels || !sband->n_bitrates)) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 286 | return -EINVAL; |
Johannes Berg | 881d948 | 2009-01-21 15:13:48 +0100 | [diff] [blame] | 287 | |
| 288 | /* |
| 289 | * Since we use a u32 for rate bitmaps in |
| 290 | * ieee80211_get_response_rate, we cannot |
| 291 | * have more than 32 legacy rates. |
| 292 | */ |
| 293 | if (WARN_ON(sband->n_bitrates > 32)) |
| 294 | return -EINVAL; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 295 | |
| 296 | for (i = 0; i < sband->n_channels; i++) { |
| 297 | sband->channels[i].orig_flags = |
| 298 | sband->channels[i].flags; |
| 299 | sband->channels[i].orig_mag = |
| 300 | sband->channels[i].max_antenna_gain; |
| 301 | sband->channels[i].orig_mpwr = |
| 302 | sband->channels[i].max_power; |
| 303 | sband->channels[i].band = band; |
| 304 | } |
| 305 | |
| 306 | have_band = true; |
| 307 | } |
| 308 | |
| 309 | if (!have_band) { |
| 310 | WARN_ON(1); |
| 311 | return -EINVAL; |
| 312 | } |
| 313 | |
| 314 | /* check and set up bitrates */ |
| 315 | ieee80211_set_bitrate_flags(wiphy); |
| 316 | |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 317 | mutex_lock(&cfg80211_drv_mutex); |
| 318 | |
Johannes Berg | f3b407f | 2008-10-21 09:57:41 +0200 | [diff] [blame] | 319 | /* set up regulatory info */ |
| 320 | wiphy_update_regulatory(wiphy, REGDOM_SET_BY_CORE); |
| 321 | |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 322 | res = device_add(&drv->wiphy.dev); |
| 323 | if (res) |
| 324 | goto out_unlock; |
| 325 | |
| 326 | list_add(&drv->list, &cfg80211_drv_list); |
| 327 | |
| 328 | /* add to debugfs */ |
| 329 | drv->wiphy.debugfsdir = |
| 330 | debugfs_create_dir(wiphy_name(&drv->wiphy), |
| 331 | ieee80211_debugfs_dir); |
Johannes Berg | 33c0360 | 2008-10-08 10:23:48 +0200 | [diff] [blame] | 332 | if (IS_ERR(drv->wiphy.debugfsdir)) |
| 333 | drv->wiphy.debugfsdir = NULL; |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 334 | |
| 335 | res = 0; |
| 336 | out_unlock: |
| 337 | mutex_unlock(&cfg80211_drv_mutex); |
| 338 | return res; |
| 339 | } |
| 340 | EXPORT_SYMBOL(wiphy_register); |
| 341 | |
| 342 | void wiphy_unregister(struct wiphy *wiphy) |
| 343 | { |
| 344 | struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy); |
| 345 | |
Johannes Berg | f16bfc1 | 2007-04-26 20:51:12 -0700 | [diff] [blame] | 346 | /* protect the device list */ |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 347 | mutex_lock(&cfg80211_drv_mutex); |
| 348 | |
Johannes Berg | f16bfc1 | 2007-04-26 20:51:12 -0700 | [diff] [blame] | 349 | BUG_ON(!list_empty(&drv->netdev_list)); |
| 350 | |
| 351 | /* |
| 352 | * Try to grab drv->mtx. If a command is still in progress, |
| 353 | * hopefully the driver will refuse it since it's tearing |
| 354 | * down the device already. We wait for this command to complete |
| 355 | * before unlinking the item from the list. |
| 356 | * Note: as codified by the BUG_ON above we cannot get here if |
| 357 | * a virtual interface is still associated. Hence, we can only |
| 358 | * get to lock contention here if userspace issues a command |
| 359 | * that identified the hardware by wiphy index. |
| 360 | */ |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 361 | mutex_lock(&drv->mtx); |
Johannes Berg | f16bfc1 | 2007-04-26 20:51:12 -0700 | [diff] [blame] | 362 | /* unlock again before freeing */ |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 363 | mutex_unlock(&drv->mtx); |
| 364 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 365 | /* If this device got a regulatory hint tell core its |
| 366 | * free to listen now to a new shiny device regulatory hint */ |
| 367 | reg_device_remove(wiphy); |
| 368 | |
Johannes Berg | f16bfc1 | 2007-04-26 20:51:12 -0700 | [diff] [blame] | 369 | list_del(&drv->list); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 370 | device_del(&drv->wiphy.dev); |
| 371 | debugfs_remove(drv->wiphy.debugfsdir); |
| 372 | |
| 373 | mutex_unlock(&cfg80211_drv_mutex); |
| 374 | } |
| 375 | EXPORT_SYMBOL(wiphy_unregister); |
| 376 | |
| 377 | void cfg80211_dev_free(struct cfg80211_registered_device *drv) |
| 378 | { |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 379 | struct cfg80211_internal_bss *scan, *tmp; |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 380 | mutex_destroy(&drv->mtx); |
| 381 | mutex_destroy(&drv->devlist_mtx); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 382 | list_for_each_entry_safe(scan, tmp, &drv->bss_list, list) |
Johannes Berg | 78c1c7e | 2009-02-10 21:25:57 +0100 | [diff] [blame] | 383 | cfg80211_put_bss(&scan->pub); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 384 | kfree(drv); |
| 385 | } |
| 386 | |
| 387 | void wiphy_free(struct wiphy *wiphy) |
| 388 | { |
| 389 | put_device(&wiphy->dev); |
| 390 | } |
| 391 | EXPORT_SYMBOL(wiphy_free); |
| 392 | |
| 393 | static int cfg80211_netdev_notifier_call(struct notifier_block * nb, |
| 394 | unsigned long state, |
| 395 | void *ndev) |
| 396 | { |
| 397 | struct net_device *dev = ndev; |
| 398 | struct cfg80211_registered_device *rdev; |
| 399 | |
| 400 | if (!dev->ieee80211_ptr) |
| 401 | return 0; |
| 402 | |
| 403 | rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy); |
| 404 | |
Johannes Berg | 60719ff | 2008-09-16 14:55:09 +0200 | [diff] [blame] | 405 | WARN_ON(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_UNSPECIFIED); |
| 406 | |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 407 | switch (state) { |
| 408 | case NETDEV_REGISTER: |
| 409 | mutex_lock(&rdev->devlist_mtx); |
| 410 | list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list); |
| 411 | if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj, |
| 412 | "phy80211")) { |
| 413 | printk(KERN_ERR "wireless: failed to add phy80211 " |
| 414 | "symlink to netdev!\n"); |
| 415 | } |
| 416 | dev->ieee80211_ptr->netdev = dev; |
| 417 | mutex_unlock(&rdev->devlist_mtx); |
| 418 | break; |
| 419 | case NETDEV_UNREGISTER: |
| 420 | mutex_lock(&rdev->devlist_mtx); |
| 421 | if (!list_empty(&dev->ieee80211_ptr->list)) { |
| 422 | sysfs_remove_link(&dev->dev.kobj, "phy80211"); |
| 423 | list_del_init(&dev->ieee80211_ptr->list); |
| 424 | } |
| 425 | mutex_unlock(&rdev->devlist_mtx); |
| 426 | break; |
| 427 | } |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | static struct notifier_block cfg80211_netdev_notifier = { |
| 433 | .notifier_call = cfg80211_netdev_notifier_call, |
| 434 | }; |
| 435 | |
| 436 | static int cfg80211_init(void) |
| 437 | { |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 438 | int err; |
| 439 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 440 | err = wiphy_sysfs_init(); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 441 | if (err) |
| 442 | goto out_fail_sysfs; |
| 443 | |
| 444 | err = register_netdevice_notifier(&cfg80211_netdev_notifier); |
| 445 | if (err) |
| 446 | goto out_fail_notifier; |
| 447 | |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 448 | err = nl80211_init(); |
| 449 | if (err) |
| 450 | goto out_fail_nl80211; |
| 451 | |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 452 | ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL); |
| 453 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 454 | err = regulatory_init(); |
| 455 | if (err) |
| 456 | goto out_fail_reg; |
| 457 | |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 458 | return 0; |
| 459 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 460 | out_fail_reg: |
| 461 | debugfs_remove(ieee80211_debugfs_dir); |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 462 | out_fail_nl80211: |
| 463 | unregister_netdevice_notifier(&cfg80211_netdev_notifier); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 464 | out_fail_notifier: |
| 465 | wiphy_sysfs_exit(); |
| 466 | out_fail_sysfs: |
| 467 | return err; |
| 468 | } |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 469 | |
Johannes Berg | 3a46246 | 2007-09-10 13:44:45 +0200 | [diff] [blame] | 470 | subsys_initcall(cfg80211_init); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 471 | |
| 472 | static void cfg80211_exit(void) |
| 473 | { |
| 474 | debugfs_remove(ieee80211_debugfs_dir); |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 475 | nl80211_exit(); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 476 | unregister_netdevice_notifier(&cfg80211_netdev_notifier); |
| 477 | wiphy_sysfs_exit(); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 478 | regulatory_exit(); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 479 | } |
| 480 | module_exit(cfg80211_exit); |