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