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