2 * This is the linux wireless configuration interface.
4 * Copyright 2006-2008 Johannes Berg <johannes@sipsolutions.net>
8 #include <linux/module.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>
23 /* name for sysfs, %d is appended */
24 #define PHY_NAME "phy"
26 MODULE_AUTHOR("Johannes Berg");
27 MODULE_LICENSE("GPL");
28 MODULE_DESCRIPTION("wireless configuration support");
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
);
35 static int wiphy_counter
;
38 static struct dentry
*ieee80211_debugfs_dir
;
40 /* requires cfg80211_drv_mutex to be held! */
41 static struct cfg80211_registered_device
*cfg80211_drv_by_wiphy(int wiphy
)
43 struct cfg80211_registered_device
*result
= NULL
, *drv
;
45 list_for_each_entry(drv
, &cfg80211_drv_list
, list
) {
46 if (drv
->idx
== wiphy
) {
55 /* requires cfg80211_drv_mutex to be held! */
56 static struct cfg80211_registered_device
*
57 __cfg80211_drv_from_info(struct genl_info
*info
)
60 struct cfg80211_registered_device
*bywiphy
= NULL
, *byifidx
= NULL
;
61 struct net_device
*dev
;
64 if (info
->attrs
[NL80211_ATTR_WIPHY
]) {
65 bywiphy
= cfg80211_drv_by_wiphy(
66 nla_get_u32(info
->attrs
[NL80211_ATTR_WIPHY
]));
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
);
74 if (dev
->ieee80211_ptr
)
76 wiphy_to_dev(dev
->ieee80211_ptr
->wiphy
);
82 if (bywiphy
&& byifidx
) {
83 if (bywiphy
!= byifidx
)
84 return ERR_PTR(-EINVAL
);
86 return bywiphy
; /* == byifidx */
97 struct cfg80211_registered_device
*
98 cfg80211_get_dev_from_info(struct genl_info
*info
)
100 struct cfg80211_registered_device
*drv
;
102 mutex_lock(&cfg80211_drv_mutex
);
103 drv
= __cfg80211_drv_from_info(info
);
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 */
109 mutex_lock(&drv
->mtx
);
111 mutex_unlock(&cfg80211_drv_mutex
);
116 struct cfg80211_registered_device
*
117 cfg80211_get_dev_from_ifindex(int ifindex
)
119 struct cfg80211_registered_device
*drv
= ERR_PTR(-ENODEV
);
120 struct net_device
*dev
;
122 mutex_lock(&cfg80211_drv_mutex
);
123 dev
= dev_get_by_index(&init_net
, ifindex
);
126 if (dev
->ieee80211_ptr
) {
127 drv
= wiphy_to_dev(dev
->ieee80211_ptr
->wiphy
);
128 mutex_lock(&drv
->mtx
);
130 drv
= ERR_PTR(-ENODEV
);
133 mutex_unlock(&cfg80211_drv_mutex
);
137 void cfg80211_put_dev(struct cfg80211_registered_device
*drv
)
140 mutex_unlock(&drv
->mtx
);
143 int cfg80211_dev_rename(struct cfg80211_registered_device
*rdev
,
146 struct cfg80211_registered_device
*drv
;
147 int idx
, taken
= -1, result
, digits
;
149 mutex_lock(&cfg80211_drv_mutex
);
151 /* prohibit calling the thing phy%d when %d is not its number */
152 sscanf(newname
, PHY_NAME
"%d%n", &idx
, &taken
);
153 if (taken
== strlen(newname
) && idx
!= rdev
->idx
) {
154 /* count number of places needed to print idx */
159 * deny the name if it is phy<idx> where <idx> is printed
160 * without leading zeroes. taken == strlen(newname) here
163 if (taken
== strlen(PHY_NAME
) + digits
)
168 /* Ignore nop renames */
170 if (strcmp(newname
, dev_name(&rdev
->wiphy
.dev
)) == 0)
173 /* Ensure another device does not already have this name. */
174 list_for_each_entry(drv
, &cfg80211_drv_list
, list
) {
176 if (strcmp(newname
, dev_name(&drv
->wiphy
.dev
)) == 0)
180 /* this will only check for collisions in sysfs
181 * which is not even always compiled in.
183 result
= device_rename(&rdev
->wiphy
.dev
, newname
);
187 if (!debugfs_rename(rdev
->wiphy
.debugfsdir
->d_parent
,
188 rdev
->wiphy
.debugfsdir
,
189 rdev
->wiphy
.debugfsdir
->d_parent
,
191 printk(KERN_ERR
"cfg80211: failed to rename debugfs dir to %s!\n",
196 mutex_unlock(&cfg80211_drv_mutex
);
198 nl80211_notify_dev_rename(rdev
);
203 /* exported functions */
205 struct wiphy
*wiphy_new(struct cfg80211_ops
*ops
, int sizeof_priv
)
207 struct cfg80211_registered_device
*drv
;
210 WARN_ON(!ops
->add_key
&& ops
->del_key
);
211 WARN_ON(ops
->add_key
&& !ops
->del_key
);
213 alloc_size
= sizeof(*drv
) + sizeof_priv
;
215 drv
= kzalloc(alloc_size
, GFP_KERNEL
);
221 mutex_lock(&cfg80211_drv_mutex
);
223 drv
->idx
= wiphy_counter
;
225 /* now increase counter for the next device unless
226 * it has wrapped previously */
227 if (wiphy_counter
>= 0)
230 mutex_unlock(&cfg80211_drv_mutex
);
232 if (unlikely(drv
->idx
< 0)) {
238 /* give it a proper name */
239 snprintf(drv
->wiphy
.dev
.bus_id
, BUS_ID_SIZE
,
240 PHY_NAME
"%d", drv
->idx
);
242 mutex_init(&drv
->mtx
);
243 mutex_init(&drv
->devlist_mtx
);
244 INIT_LIST_HEAD(&drv
->netdev_list
);
246 device_initialize(&drv
->wiphy
.dev
);
247 drv
->wiphy
.dev
.class = &ieee80211_class
;
248 drv
->wiphy
.dev
.platform_data
= drv
;
252 EXPORT_SYMBOL(wiphy_new
);
254 int wiphy_register(struct wiphy
*wiphy
)
256 struct cfg80211_registered_device
*drv
= wiphy_to_dev(wiphy
);
258 enum ieee80211_band band
;
259 struct ieee80211_supported_band
*sband
;
260 bool have_band
= false;
262 u16 ifmodes
= wiphy
->interface_modes
;
264 /* sanity check ifmodes */
266 ifmodes
&= ((1 << __NL80211_IFTYPE_AFTER_LAST
) - 1) & ~1;
267 if (WARN_ON(ifmodes
!= wiphy
->interface_modes
))
268 wiphy
->interface_modes
= ifmodes
;
270 /* sanity check supported bands/channels */
271 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
272 sband
= wiphy
->bands
[band
];
278 if (!sband
->n_channels
|| !sband
->n_bitrates
) {
283 for (i
= 0; i
< sband
->n_channels
; i
++) {
284 sband
->channels
[i
].orig_flags
=
285 sband
->channels
[i
].flags
;
286 sband
->channels
[i
].orig_mag
=
287 sband
->channels
[i
].max_antenna_gain
;
288 sband
->channels
[i
].orig_mpwr
=
289 sband
->channels
[i
].max_power
;
290 sband
->channels
[i
].band
= band
;
301 /* check and set up bitrates */
302 ieee80211_set_bitrate_flags(wiphy
);
304 /* set up regulatory info */
305 wiphy_update_regulatory(wiphy
);
307 mutex_lock(&cfg80211_drv_mutex
);
309 res
= device_add(&drv
->wiphy
.dev
);
313 list_add(&drv
->list
, &cfg80211_drv_list
);
316 drv
->wiphy
.debugfsdir
=
317 debugfs_create_dir(wiphy_name(&drv
->wiphy
),
318 ieee80211_debugfs_dir
);
322 mutex_unlock(&cfg80211_drv_mutex
);
325 EXPORT_SYMBOL(wiphy_register
);
327 void wiphy_unregister(struct wiphy
*wiphy
)
329 struct cfg80211_registered_device
*drv
= wiphy_to_dev(wiphy
);
331 /* protect the device list */
332 mutex_lock(&cfg80211_drv_mutex
);
334 BUG_ON(!list_empty(&drv
->netdev_list
));
337 * Try to grab drv->mtx. If a command is still in progress,
338 * hopefully the driver will refuse it since it's tearing
339 * down the device already. We wait for this command to complete
340 * before unlinking the item from the list.
341 * Note: as codified by the BUG_ON above we cannot get here if
342 * a virtual interface is still associated. Hence, we can only
343 * get to lock contention here if userspace issues a command
344 * that identified the hardware by wiphy index.
346 mutex_lock(&drv
->mtx
);
347 /* unlock again before freeing */
348 mutex_unlock(&drv
->mtx
);
350 list_del(&drv
->list
);
351 device_del(&drv
->wiphy
.dev
);
352 debugfs_remove(drv
->wiphy
.debugfsdir
);
354 mutex_unlock(&cfg80211_drv_mutex
);
356 EXPORT_SYMBOL(wiphy_unregister
);
358 void cfg80211_dev_free(struct cfg80211_registered_device
*drv
)
360 mutex_destroy(&drv
->mtx
);
361 mutex_destroy(&drv
->devlist_mtx
);
365 void wiphy_free(struct wiphy
*wiphy
)
367 put_device(&wiphy
->dev
);
369 EXPORT_SYMBOL(wiphy_free
);
371 static int cfg80211_netdev_notifier_call(struct notifier_block
* nb
,
375 struct net_device
*dev
= ndev
;
376 struct cfg80211_registered_device
*rdev
;
378 if (!dev
->ieee80211_ptr
)
381 rdev
= wiphy_to_dev(dev
->ieee80211_ptr
->wiphy
);
384 case NETDEV_REGISTER
:
385 mutex_lock(&rdev
->devlist_mtx
);
386 list_add(&dev
->ieee80211_ptr
->list
, &rdev
->netdev_list
);
387 if (sysfs_create_link(&dev
->dev
.kobj
, &rdev
->wiphy
.dev
.kobj
,
389 printk(KERN_ERR
"wireless: failed to add phy80211 "
390 "symlink to netdev!\n");
392 dev
->ieee80211_ptr
->netdev
= dev
;
393 mutex_unlock(&rdev
->devlist_mtx
);
395 case NETDEV_UNREGISTER
:
396 mutex_lock(&rdev
->devlist_mtx
);
397 if (!list_empty(&dev
->ieee80211_ptr
->list
)) {
398 sysfs_remove_link(&dev
->dev
.kobj
, "phy80211");
399 list_del_init(&dev
->ieee80211_ptr
->list
);
401 mutex_unlock(&rdev
->devlist_mtx
);
408 static struct notifier_block cfg80211_netdev_notifier
= {
409 .notifier_call
= cfg80211_netdev_notifier_call
,
412 static int cfg80211_init(void)
414 int err
= wiphy_sysfs_init();
418 err
= register_netdevice_notifier(&cfg80211_netdev_notifier
);
420 goto out_fail_notifier
;
422 err
= nl80211_init();
424 goto out_fail_nl80211
;
426 ieee80211_debugfs_dir
= debugfs_create_dir("ieee80211", NULL
);
431 unregister_netdevice_notifier(&cfg80211_netdev_notifier
);
437 subsys_initcall(cfg80211_init
);
439 static void cfg80211_exit(void)
441 debugfs_remove(ieee80211_debugfs_dir
);
443 unregister_netdevice_notifier(&cfg80211_netdev_notifier
);
446 module_exit(cfg80211_exit
);