Commit | Line | Data |
---|---|---|
704232c2 JB |
1 | /* |
2 | * This is the linux wireless configuration interface. | |
3 | * | |
4 | * Copyright 2006, 2007 Johannes Berg <johannes@sipsolutions.net> | |
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> | |
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); | |
33 | DEFINE_MUTEX(cfg80211_drv_mutex); | |
34 | static int wiphy_counter; | |
35 | ||
36 | /* for debugfs */ | |
37 | static struct dentry *ieee80211_debugfs_dir; | |
38 | ||
39 | /* exported functions */ | |
40 | ||
41 | struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv) | |
42 | { | |
43 | struct cfg80211_registered_device *drv; | |
44 | int alloc_size; | |
45 | ||
46 | alloc_size = sizeof(*drv) + sizeof_priv; | |
47 | ||
48 | drv = kzalloc(alloc_size, GFP_KERNEL); | |
49 | if (!drv) | |
50 | return NULL; | |
51 | ||
52 | drv->ops = ops; | |
53 | ||
54 | mutex_lock(&cfg80211_drv_mutex); | |
55 | ||
56 | if (unlikely(wiphy_counter<0)) { | |
57 | /* ugh, wrapped! */ | |
58 | kfree(drv); | |
59 | return NULL; | |
60 | } | |
61 | drv->idx = wiphy_counter; | |
62 | ||
63 | /* give it a proper name */ | |
64 | snprintf(drv->wiphy.dev.bus_id, BUS_ID_SIZE, | |
65 | PHY_NAME "%d", drv->idx); | |
66 | ||
67 | /* now increase counter for the next time */ | |
68 | wiphy_counter++; | |
69 | mutex_unlock(&cfg80211_drv_mutex); | |
70 | ||
71 | mutex_init(&drv->mtx); | |
72 | mutex_init(&drv->devlist_mtx); | |
73 | INIT_LIST_HEAD(&drv->netdev_list); | |
74 | ||
75 | device_initialize(&drv->wiphy.dev); | |
76 | drv->wiphy.dev.class = &ieee80211_class; | |
77 | drv->wiphy.dev.platform_data = drv; | |
78 | ||
79 | return &drv->wiphy; | |
80 | } | |
81 | EXPORT_SYMBOL(wiphy_new); | |
82 | ||
83 | int wiphy_register(struct wiphy *wiphy) | |
84 | { | |
85 | struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy); | |
86 | int res; | |
87 | ||
88 | mutex_lock(&cfg80211_drv_mutex); | |
89 | ||
90 | ||
91 | res = device_add(&drv->wiphy.dev); | |
92 | if (res) | |
93 | goto out_unlock; | |
94 | ||
95 | list_add(&drv->list, &cfg80211_drv_list); | |
96 | ||
97 | /* add to debugfs */ | |
98 | drv->wiphy.debugfsdir = | |
99 | debugfs_create_dir(wiphy_name(&drv->wiphy), | |
100 | ieee80211_debugfs_dir); | |
101 | ||
102 | res = 0; | |
103 | out_unlock: | |
104 | mutex_unlock(&cfg80211_drv_mutex); | |
105 | return res; | |
106 | } | |
107 | EXPORT_SYMBOL(wiphy_register); | |
108 | ||
109 | void wiphy_unregister(struct wiphy *wiphy) | |
110 | { | |
111 | struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy); | |
112 | ||
113 | mutex_lock(&cfg80211_drv_mutex); | |
114 | ||
115 | /* hold registered driver mutex during list removal as well | |
116 | * to make sure no commands are in progress at the moment */ | |
117 | mutex_lock(&drv->mtx); | |
118 | list_del(&drv->list); | |
119 | mutex_unlock(&drv->mtx); | |
120 | ||
121 | device_del(&drv->wiphy.dev); | |
122 | debugfs_remove(drv->wiphy.debugfsdir); | |
123 | ||
124 | mutex_unlock(&cfg80211_drv_mutex); | |
125 | } | |
126 | EXPORT_SYMBOL(wiphy_unregister); | |
127 | ||
128 | void cfg80211_dev_free(struct cfg80211_registered_device *drv) | |
129 | { | |
130 | mutex_destroy(&drv->mtx); | |
131 | mutex_destroy(&drv->devlist_mtx); | |
132 | kfree(drv); | |
133 | } | |
134 | ||
135 | void wiphy_free(struct wiphy *wiphy) | |
136 | { | |
137 | put_device(&wiphy->dev); | |
138 | } | |
139 | EXPORT_SYMBOL(wiphy_free); | |
140 | ||
141 | static int cfg80211_netdev_notifier_call(struct notifier_block * nb, | |
142 | unsigned long state, | |
143 | void *ndev) | |
144 | { | |
145 | struct net_device *dev = ndev; | |
146 | struct cfg80211_registered_device *rdev; | |
147 | ||
148 | if (!dev->ieee80211_ptr) | |
149 | return 0; | |
150 | ||
151 | rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy); | |
152 | ||
153 | switch (state) { | |
154 | case NETDEV_REGISTER: | |
155 | mutex_lock(&rdev->devlist_mtx); | |
156 | list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list); | |
157 | if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj, | |
158 | "phy80211")) { | |
159 | printk(KERN_ERR "wireless: failed to add phy80211 " | |
160 | "symlink to netdev!\n"); | |
161 | } | |
162 | dev->ieee80211_ptr->netdev = dev; | |
163 | mutex_unlock(&rdev->devlist_mtx); | |
164 | break; | |
165 | case NETDEV_UNREGISTER: | |
166 | mutex_lock(&rdev->devlist_mtx); | |
167 | if (!list_empty(&dev->ieee80211_ptr->list)) { | |
168 | sysfs_remove_link(&dev->dev.kobj, "phy80211"); | |
169 | list_del_init(&dev->ieee80211_ptr->list); | |
170 | } | |
171 | mutex_unlock(&rdev->devlist_mtx); | |
172 | break; | |
173 | } | |
174 | ||
175 | return 0; | |
176 | } | |
177 | ||
178 | static struct notifier_block cfg80211_netdev_notifier = { | |
179 | .notifier_call = cfg80211_netdev_notifier_call, | |
180 | }; | |
181 | ||
182 | static int cfg80211_init(void) | |
183 | { | |
184 | int err = wiphy_sysfs_init(); | |
185 | if (err) | |
186 | goto out_fail_sysfs; | |
187 | ||
188 | err = register_netdevice_notifier(&cfg80211_netdev_notifier); | |
189 | if (err) | |
190 | goto out_fail_notifier; | |
191 | ||
192 | ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL); | |
193 | ||
194 | return 0; | |
195 | ||
196 | out_fail_notifier: | |
197 | wiphy_sysfs_exit(); | |
198 | out_fail_sysfs: | |
199 | return err; | |
200 | } | |
201 | module_init(cfg80211_init); | |
202 | ||
203 | static void cfg80211_exit(void) | |
204 | { | |
205 | debugfs_remove(ieee80211_debugfs_dir); | |
206 | unregister_netdevice_notifier(&cfg80211_netdev_notifier); | |
207 | wiphy_sysfs_exit(); | |
208 | } | |
209 | module_exit(cfg80211_exit); |