Commit | Line | Data |
---|---|---|
2bfb1070 DES |
1 | /* |
2 | * Copyright (C) 2007, 2008, 2009 Siemens AG | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 | |
6 | * as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
2bfb1070 DES |
13 | */ |
14 | ||
5a0e3ad6 | 15 | #include <linux/slab.h> |
2bfb1070 DES |
16 | #include <linux/kernel.h> |
17 | #include <linux/module.h> | |
18 | #include <linux/device.h> | |
19 | ||
5ad60d36 | 20 | #include <net/cfg802154.h> |
f3ada640 | 21 | #include <net/rtnetlink.h> |
2bfb1070 | 22 | |
cb6b3763 | 23 | #include "ieee802154.h" |
e23e9ec1 | 24 | #include "sysfs.h" |
a5dd1d72 | 25 | #include "core.h" |
2bfb1070 | 26 | |
f3ada640 AA |
27 | /* RCU-protected (and RTNL for writers) */ |
28 | static LIST_HEAD(cfg802154_rdev_list); | |
29 | static int cfg802154_rdev_list_generation; | |
30 | ||
9f3b795a | 31 | static int wpan_phy_match(struct device *dev, const void *data) |
2bfb1070 DES |
32 | { |
33 | return !strcmp(dev_name(dev), (const char *)data); | |
34 | } | |
35 | ||
36 | struct wpan_phy *wpan_phy_find(const char *str) | |
37 | { | |
38 | struct device *dev; | |
39 | ||
40 | if (WARN_ON(!str)) | |
41 | return NULL; | |
42 | ||
9f3b795a | 43 | dev = class_find_device(&wpan_phy_class, NULL, str, wpan_phy_match); |
2bfb1070 DES |
44 | if (!dev) |
45 | return NULL; | |
46 | ||
47 | return container_of(dev, struct wpan_phy, dev); | |
48 | } | |
49 | EXPORT_SYMBOL(wpan_phy_find); | |
50 | ||
1c889f4d DES |
51 | struct wpan_phy_iter_data { |
52 | int (*fn)(struct wpan_phy *phy, void *data); | |
53 | void *data; | |
54 | }; | |
55 | ||
56 | static int wpan_phy_iter(struct device *dev, void *_data) | |
57 | { | |
58 | struct wpan_phy_iter_data *wpid = _data; | |
59 | struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev); | |
4710d806 | 60 | |
1c889f4d DES |
61 | return wpid->fn(phy, wpid->data); |
62 | } | |
63 | ||
64 | int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data), | |
4710d806 | 65 | void *data) |
1c889f4d DES |
66 | { |
67 | struct wpan_phy_iter_data wpid = { | |
68 | .fn = fn, | |
69 | .data = data, | |
70 | }; | |
71 | ||
72 | return class_for_each_device(&wpan_phy_class, NULL, | |
73 | &wpid, wpan_phy_iter); | |
74 | } | |
75 | EXPORT_SYMBOL(wpan_phy_for_each); | |
76 | ||
a5dd1d72 | 77 | struct wpan_phy * |
f601379f | 78 | wpan_phy_new(const struct cfg802154_ops *ops, size_t priv_size) |
2bfb1070 | 79 | { |
53f9ee61 | 80 | static atomic_t wpan_phy_counter = ATOMIC_INIT(0); |
a5dd1d72 AA |
81 | struct cfg802154_registered_device *rdev; |
82 | size_t alloc_size; | |
83 | ||
84 | alloc_size = sizeof(*rdev) + priv_size; | |
85 | rdev = kzalloc(alloc_size, GFP_KERNEL); | |
86 | if (!rdev) | |
87 | return NULL; | |
88 | ||
89 | rdev->ops = ops; | |
2bfb1070 | 90 | |
53f9ee61 AA |
91 | rdev->wpan_phy_idx = atomic_inc_return(&wpan_phy_counter); |
92 | ||
93 | if (unlikely(rdev->wpan_phy_idx < 0)) { | |
94 | /* ugh, wrapped! */ | |
95 | atomic_dec(&wpan_phy_counter); | |
a5dd1d72 | 96 | kfree(rdev); |
53f9ee61 | 97 | return NULL; |
2bfb1070 | 98 | } |
53f9ee61 AA |
99 | |
100 | /* atomic_inc_return makes it start at 1, make it start at 0 */ | |
101 | rdev->wpan_phy_idx--; | |
2bfb1070 | 102 | |
a5dd1d72 | 103 | mutex_init(&rdev->wpan_phy.pib_lock); |
2bfb1070 | 104 | |
fcf39e6e | 105 | INIT_LIST_HEAD(&rdev->wpan_dev_list); |
a5dd1d72 | 106 | device_initialize(&rdev->wpan_phy.dev); |
53f9ee61 | 107 | dev_set_name(&rdev->wpan_phy.dev, "wpan-phy%d", rdev->wpan_phy_idx); |
2bfb1070 | 108 | |
a5dd1d72 AA |
109 | rdev->wpan_phy.dev.class = &wpan_phy_class; |
110 | rdev->wpan_phy.dev.platform_data = rdev; | |
2bfb1070 | 111 | |
fcf39e6e AA |
112 | init_waitqueue_head(&rdev->dev_wait); |
113 | ||
a5dd1d72 | 114 | return &rdev->wpan_phy; |
2bfb1070 | 115 | } |
f601379f | 116 | EXPORT_SYMBOL(wpan_phy_new); |
2bfb1070 | 117 | |
e9cf356c | 118 | int wpan_phy_register(struct wpan_phy *phy) |
2bfb1070 | 119 | { |
f3ada640 AA |
120 | struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy); |
121 | int ret; | |
122 | ||
123 | rtnl_lock(); | |
124 | ret = device_add(&phy->dev); | |
125 | if (ret) { | |
126 | rtnl_unlock(); | |
127 | return ret; | |
128 | } | |
129 | ||
130 | list_add_rcu(&rdev->list, &cfg802154_rdev_list); | |
131 | cfg802154_rdev_list_generation++; | |
132 | ||
133 | /* TODO phy registered lock */ | |
134 | rtnl_unlock(); | |
135 | ||
136 | /* TODO nl802154 phy notify */ | |
137 | ||
138 | return 0; | |
2bfb1070 DES |
139 | } |
140 | EXPORT_SYMBOL(wpan_phy_register); | |
141 | ||
142 | void wpan_phy_unregister(struct wpan_phy *phy) | |
143 | { | |
f3ada640 AA |
144 | struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy); |
145 | ||
fcf39e6e AA |
146 | wait_event(rdev->dev_wait, ({ |
147 | int __count; | |
148 | rtnl_lock(); | |
149 | __count = rdev->opencount; | |
150 | rtnl_unlock(); | |
151 | __count == 0; })); | |
f3ada640 AA |
152 | |
153 | rtnl_lock(); | |
154 | /* TODO nl802154 phy notify */ | |
155 | /* TODO phy registered lock */ | |
156 | ||
fcf39e6e | 157 | WARN_ON(!list_empty(&rdev->wpan_dev_list)); |
f3ada640 AA |
158 | |
159 | /* First remove the hardware from everywhere, this makes | |
160 | * it impossible to find from userspace. | |
161 | */ | |
162 | list_del_rcu(&rdev->list); | |
163 | synchronize_rcu(); | |
164 | ||
165 | cfg802154_rdev_list_generation++; | |
166 | ||
2bfb1070 | 167 | device_del(&phy->dev); |
f3ada640 AA |
168 | |
169 | rtnl_unlock(); | |
2bfb1070 DES |
170 | } |
171 | EXPORT_SYMBOL(wpan_phy_unregister); | |
172 | ||
173 | void wpan_phy_free(struct wpan_phy *phy) | |
174 | { | |
175 | put_device(&phy->dev); | |
176 | } | |
177 | EXPORT_SYMBOL(wpan_phy_free); | |
178 | ||
a5dd1d72 AA |
179 | void cfg802154_dev_free(struct cfg802154_registered_device *rdev) |
180 | { | |
181 | kfree(rdev); | |
182 | } | |
183 | ||
fcf39e6e AA |
184 | static void |
185 | cfg802154_update_iface_num(struct cfg802154_registered_device *rdev, | |
186 | int iftype, int num) | |
187 | { | |
188 | ASSERT_RTNL(); | |
189 | ||
190 | rdev->num_running_ifaces += num; | |
191 | } | |
192 | ||
193 | static int cfg802154_netdev_notifier_call(struct notifier_block *nb, | |
194 | unsigned long state, void *ptr) | |
195 | { | |
196 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
197 | struct wpan_dev *wpan_dev = dev->ieee802154_ptr; | |
198 | struct cfg802154_registered_device *rdev; | |
199 | ||
200 | if (!wpan_dev) | |
201 | return NOTIFY_DONE; | |
202 | ||
203 | rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy); | |
204 | ||
205 | /* TODO WARN_ON unspec type */ | |
206 | ||
207 | switch (state) { | |
208 | /* TODO NETDEV_DEVTYPE */ | |
209 | case NETDEV_REGISTER: | |
210 | wpan_dev->identifier = ++rdev->wpan_dev_id; | |
211 | list_add_rcu(&wpan_dev->list, &rdev->wpan_dev_list); | |
212 | rdev->devlist_generation++; | |
213 | ||
214 | wpan_dev->netdev = dev; | |
215 | break; | |
216 | case NETDEV_DOWN: | |
217 | cfg802154_update_iface_num(rdev, wpan_dev->iftype, -1); | |
218 | ||
219 | rdev->opencount--; | |
220 | wake_up(&rdev->dev_wait); | |
221 | break; | |
222 | case NETDEV_UP: | |
223 | cfg802154_update_iface_num(rdev, wpan_dev->iftype, 1); | |
224 | ||
225 | rdev->opencount++; | |
226 | break; | |
227 | case NETDEV_UNREGISTER: | |
228 | /* It is possible to get NETDEV_UNREGISTER | |
229 | * multiple times. To detect that, check | |
230 | * that the interface is still on the list | |
231 | * of registered interfaces, and only then | |
232 | * remove and clean it up. | |
233 | */ | |
234 | if (!list_empty(&wpan_dev->list)) { | |
235 | list_del_rcu(&wpan_dev->list); | |
236 | rdev->devlist_generation++; | |
237 | } | |
238 | /* synchronize (so that we won't find this netdev | |
239 | * from other code any more) and then clear the list | |
240 | * head so that the above code can safely check for | |
241 | * !list_empty() to avoid double-cleanup. | |
242 | */ | |
243 | synchronize_rcu(); | |
244 | INIT_LIST_HEAD(&wpan_dev->list); | |
245 | break; | |
246 | default: | |
247 | return NOTIFY_DONE; | |
248 | } | |
249 | ||
250 | return NOTIFY_OK; | |
251 | } | |
252 | ||
253 | static struct notifier_block cfg802154_netdev_notifier = { | |
254 | .notifier_call = cfg802154_netdev_notifier_call, | |
255 | }; | |
256 | ||
2bfb1070 DES |
257 | static int __init wpan_phy_class_init(void) |
258 | { | |
cb6b3763 | 259 | int rc; |
4710d806 | 260 | |
e23e9ec1 | 261 | rc = wpan_phy_sysfs_init(); |
cb6b3763 DES |
262 | if (rc) |
263 | goto err; | |
264 | ||
fcf39e6e | 265 | rc = register_netdevice_notifier(&cfg802154_netdev_notifier); |
cb6b3763 DES |
266 | if (rc) |
267 | goto err_nl; | |
268 | ||
fcf39e6e AA |
269 | rc = ieee802154_nl_init(); |
270 | if (rc) | |
271 | goto err_notifier; | |
272 | ||
cb6b3763 | 273 | return 0; |
fcf39e6e AA |
274 | |
275 | err_notifier: | |
276 | unregister_netdevice_notifier(&cfg802154_netdev_notifier); | |
cb6b3763 | 277 | err_nl: |
e23e9ec1 | 278 | wpan_phy_sysfs_exit(); |
cb6b3763 DES |
279 | err: |
280 | return rc; | |
2bfb1070 | 281 | } |
282a3954 | 282 | subsys_initcall(wpan_phy_class_init); |
2bfb1070 DES |
283 | |
284 | static void __exit wpan_phy_class_exit(void) | |
285 | { | |
cb6b3763 | 286 | ieee802154_nl_exit(); |
fcf39e6e | 287 | unregister_netdevice_notifier(&cfg802154_netdev_notifier); |
e23e9ec1 | 288 | wpan_phy_sysfs_exit(); |
2bfb1070 DES |
289 | } |
290 | module_exit(wpan_phy_class_exit); | |
291 | ||
2bfb1070 | 292 | MODULE_LICENSE("GPL v2"); |
cb6b3763 DES |
293 | MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface"); |
294 | MODULE_AUTHOR("Dmitry Eremin-Solenikov"); | |
2bfb1070 | 295 |