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