printf: Remove unused 'bprintf'
[drm/drm-misc.git] / net / ieee802154 / core.c
blob88adb04e407220f3990adf7ea26584716c0ff799
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2007, 2008, 2009 Siemens AG
4 */
6 #include <linux/slab.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/device.h>
11 #include <net/cfg802154.h>
12 #include <net/rtnetlink.h>
14 #include "ieee802154.h"
15 #include "nl802154.h"
16 #include "sysfs.h"
17 #include "core.h"
19 /* name for sysfs, %d is appended */
20 #define PHY_NAME "phy"
22 /* RCU-protected (and RTNL for writers) */
23 LIST_HEAD(cfg802154_rdev_list);
24 int cfg802154_rdev_list_generation;
26 struct wpan_phy *wpan_phy_find(const char *str)
28 struct device *dev;
30 if (WARN_ON(!str))
31 return NULL;
33 dev = class_find_device_by_name(&wpan_phy_class, str);
34 if (!dev)
35 return NULL;
37 return container_of(dev, struct wpan_phy, dev);
39 EXPORT_SYMBOL(wpan_phy_find);
41 struct wpan_phy_iter_data {
42 int (*fn)(struct wpan_phy *phy, void *data);
43 void *data;
46 static int wpan_phy_iter(struct device *dev, void *_data)
48 struct wpan_phy_iter_data *wpid = _data;
49 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
51 return wpid->fn(phy, wpid->data);
54 int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data),
55 void *data)
57 struct wpan_phy_iter_data wpid = {
58 .fn = fn,
59 .data = data,
62 return class_for_each_device(&wpan_phy_class, NULL,
63 &wpid, wpan_phy_iter);
65 EXPORT_SYMBOL(wpan_phy_for_each);
67 struct cfg802154_registered_device *
68 cfg802154_rdev_by_wpan_phy_idx(int wpan_phy_idx)
70 struct cfg802154_registered_device *result = NULL, *rdev;
72 ASSERT_RTNL();
74 list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
75 if (rdev->wpan_phy_idx == wpan_phy_idx) {
76 result = rdev;
77 break;
81 return result;
84 struct wpan_phy *wpan_phy_idx_to_wpan_phy(int wpan_phy_idx)
86 struct cfg802154_registered_device *rdev;
88 ASSERT_RTNL();
90 rdev = cfg802154_rdev_by_wpan_phy_idx(wpan_phy_idx);
91 if (!rdev)
92 return NULL;
93 return &rdev->wpan_phy;
96 struct wpan_phy *
97 wpan_phy_new(const struct cfg802154_ops *ops, size_t priv_size)
99 static atomic_t wpan_phy_counter = ATOMIC_INIT(0);
100 struct cfg802154_registered_device *rdev;
101 size_t alloc_size;
103 alloc_size = sizeof(*rdev) + priv_size;
104 rdev = kzalloc(alloc_size, GFP_KERNEL);
105 if (!rdev)
106 return NULL;
108 rdev->ops = ops;
110 rdev->wpan_phy_idx = atomic_inc_return(&wpan_phy_counter);
112 if (unlikely(rdev->wpan_phy_idx < 0)) {
113 /* ugh, wrapped! */
114 atomic_dec(&wpan_phy_counter);
115 kfree(rdev);
116 return NULL;
119 /* atomic_inc_return makes it start at 1, make it start at 0 */
120 rdev->wpan_phy_idx--;
122 INIT_LIST_HEAD(&rdev->wpan_dev_list);
123 device_initialize(&rdev->wpan_phy.dev);
124 dev_set_name(&rdev->wpan_phy.dev, PHY_NAME "%d", rdev->wpan_phy_idx);
126 rdev->wpan_phy.dev.class = &wpan_phy_class;
127 rdev->wpan_phy.dev.platform_data = rdev;
129 wpan_phy_net_set(&rdev->wpan_phy, &init_net);
131 init_waitqueue_head(&rdev->dev_wait);
132 init_waitqueue_head(&rdev->wpan_phy.sync_txq);
134 spin_lock_init(&rdev->wpan_phy.queue_lock);
136 return &rdev->wpan_phy;
138 EXPORT_SYMBOL(wpan_phy_new);
140 int wpan_phy_register(struct wpan_phy *phy)
142 struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy);
143 int ret;
145 rtnl_lock();
146 ret = device_add(&phy->dev);
147 if (ret) {
148 rtnl_unlock();
149 return ret;
152 list_add_rcu(&rdev->list, &cfg802154_rdev_list);
153 cfg802154_rdev_list_generation++;
155 /* TODO phy registered lock */
156 rtnl_unlock();
158 /* TODO nl802154 phy notify */
160 return 0;
162 EXPORT_SYMBOL(wpan_phy_register);
164 void wpan_phy_unregister(struct wpan_phy *phy)
166 struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy);
168 wait_event(rdev->dev_wait, ({
169 int __count;
170 rtnl_lock();
171 __count = rdev->opencount;
172 rtnl_unlock();
173 __count == 0; }));
175 rtnl_lock();
176 /* TODO nl802154 phy notify */
177 /* TODO phy registered lock */
179 WARN_ON(!list_empty(&rdev->wpan_dev_list));
181 /* First remove the hardware from everywhere, this makes
182 * it impossible to find from userspace.
184 list_del_rcu(&rdev->list);
185 synchronize_rcu();
187 cfg802154_rdev_list_generation++;
189 device_del(&phy->dev);
191 rtnl_unlock();
193 EXPORT_SYMBOL(wpan_phy_unregister);
195 void wpan_phy_free(struct wpan_phy *phy)
197 put_device(&phy->dev);
199 EXPORT_SYMBOL(wpan_phy_free);
201 static void cfg802154_free_peer_structures(struct wpan_dev *wpan_dev)
203 struct ieee802154_pan_device *child, *tmp;
205 mutex_lock(&wpan_dev->association_lock);
207 kfree(wpan_dev->parent);
208 wpan_dev->parent = NULL;
210 list_for_each_entry_safe(child, tmp, &wpan_dev->children, node) {
211 list_del(&child->node);
212 kfree(child);
215 wpan_dev->nchildren = 0;
217 mutex_unlock(&wpan_dev->association_lock);
220 int cfg802154_switch_netns(struct cfg802154_registered_device *rdev,
221 struct net *net)
223 struct wpan_dev *wpan_dev;
224 int err = 0;
226 list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
227 if (!wpan_dev->netdev)
228 continue;
229 wpan_dev->netdev->netns_local = false;
230 err = dev_change_net_namespace(wpan_dev->netdev, net, "wpan%d");
231 if (err)
232 break;
233 wpan_dev->netdev->netns_local = true;
236 if (err) {
237 /* failed -- clean up to old netns */
238 net = wpan_phy_net(&rdev->wpan_phy);
240 list_for_each_entry_continue_reverse(wpan_dev,
241 &rdev->wpan_dev_list,
242 list) {
243 if (!wpan_dev->netdev)
244 continue;
245 wpan_dev->netdev->netns_local = false;
246 err = dev_change_net_namespace(wpan_dev->netdev, net,
247 "wpan%d");
248 WARN_ON(err);
249 wpan_dev->netdev->netns_local = true;
252 return err;
255 wpan_phy_net_set(&rdev->wpan_phy, net);
257 err = device_rename(&rdev->wpan_phy.dev, dev_name(&rdev->wpan_phy.dev));
258 WARN_ON(err);
260 return 0;
263 void cfg802154_dev_free(struct cfg802154_registered_device *rdev)
265 kfree(rdev);
268 static void
269 cfg802154_update_iface_num(struct cfg802154_registered_device *rdev,
270 int iftype, int num)
272 ASSERT_RTNL();
274 rdev->num_running_ifaces += num;
277 static int cfg802154_netdev_notifier_call(struct notifier_block *nb,
278 unsigned long state, void *ptr)
280 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
281 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
282 struct cfg802154_registered_device *rdev;
284 if (!wpan_dev)
285 return NOTIFY_DONE;
287 rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy);
289 /* TODO WARN_ON unspec type */
291 switch (state) {
292 /* TODO NETDEV_DEVTYPE */
293 case NETDEV_REGISTER:
294 dev->netns_local = true;
295 wpan_dev->identifier = ++rdev->wpan_dev_id;
296 list_add_rcu(&wpan_dev->list, &rdev->wpan_dev_list);
297 rdev->devlist_generation++;
298 mutex_init(&wpan_dev->association_lock);
299 INIT_LIST_HEAD(&wpan_dev->children);
300 wpan_dev->max_associations = SZ_16K;
302 wpan_dev->netdev = dev;
303 break;
304 case NETDEV_DOWN:
305 cfg802154_update_iface_num(rdev, wpan_dev->iftype, -1);
307 rdev->opencount--;
308 wake_up(&rdev->dev_wait);
309 break;
310 case NETDEV_UP:
311 cfg802154_update_iface_num(rdev, wpan_dev->iftype, 1);
313 rdev->opencount++;
314 break;
315 case NETDEV_UNREGISTER:
316 cfg802154_free_peer_structures(wpan_dev);
318 /* It is possible to get NETDEV_UNREGISTER
319 * multiple times. To detect that, check
320 * that the interface is still on the list
321 * of registered interfaces, and only then
322 * remove and clean it up.
324 if (!list_empty(&wpan_dev->list)) {
325 list_del_rcu(&wpan_dev->list);
326 rdev->devlist_generation++;
328 /* synchronize (so that we won't find this netdev
329 * from other code any more) and then clear the list
330 * head so that the above code can safely check for
331 * !list_empty() to avoid double-cleanup.
333 synchronize_rcu();
334 INIT_LIST_HEAD(&wpan_dev->list);
335 break;
336 default:
337 return NOTIFY_DONE;
340 return NOTIFY_OK;
343 static struct notifier_block cfg802154_netdev_notifier = {
344 .notifier_call = cfg802154_netdev_notifier_call,
347 static void __net_exit cfg802154_pernet_exit(struct net *net)
349 struct cfg802154_registered_device *rdev;
351 rtnl_lock();
352 list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
353 if (net_eq(wpan_phy_net(&rdev->wpan_phy), net))
354 WARN_ON(cfg802154_switch_netns(rdev, &init_net));
356 rtnl_unlock();
359 static struct pernet_operations cfg802154_pernet_ops = {
360 .exit = cfg802154_pernet_exit,
363 static int __init wpan_phy_class_init(void)
365 int rc;
367 rc = register_pernet_device(&cfg802154_pernet_ops);
368 if (rc)
369 goto err;
371 rc = wpan_phy_sysfs_init();
372 if (rc)
373 goto err_sysfs;
375 rc = register_netdevice_notifier(&cfg802154_netdev_notifier);
376 if (rc)
377 goto err_nl;
379 rc = ieee802154_nl_init();
380 if (rc)
381 goto err_notifier;
383 rc = nl802154_init();
384 if (rc)
385 goto err_ieee802154_nl;
387 return 0;
389 err_ieee802154_nl:
390 ieee802154_nl_exit();
392 err_notifier:
393 unregister_netdevice_notifier(&cfg802154_netdev_notifier);
394 err_nl:
395 wpan_phy_sysfs_exit();
396 err_sysfs:
397 unregister_pernet_device(&cfg802154_pernet_ops);
398 err:
399 return rc;
401 subsys_initcall(wpan_phy_class_init);
403 static void __exit wpan_phy_class_exit(void)
405 nl802154_exit();
406 ieee802154_nl_exit();
407 unregister_netdevice_notifier(&cfg802154_netdev_notifier);
408 wpan_phy_sysfs_exit();
409 unregister_pernet_device(&cfg802154_pernet_ops);
411 module_exit(wpan_phy_class_exit);
413 MODULE_LICENSE("GPL v2");
414 MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
415 MODULE_AUTHOR("Dmitry Eremin-Solenikov");