1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2007, 2008, 2009 Siemens AG
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"
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 static int wpan_phy_match(struct device *dev, const void *data)
28 return !strcmp(dev_name(dev), (const char *)data);
31 struct wpan_phy *wpan_phy_find(const char *str)
38 dev = class_find_device(&wpan_phy_class, NULL, str, wpan_phy_match);
42 return container_of(dev, struct wpan_phy, dev);
44 EXPORT_SYMBOL(wpan_phy_find);
46 struct wpan_phy_iter_data {
47 int (*fn)(struct wpan_phy *phy, void *data);
51 static int wpan_phy_iter(struct device *dev, void *_data)
53 struct wpan_phy_iter_data *wpid = _data;
54 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
56 return wpid->fn(phy, wpid->data);
59 int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data),
62 struct wpan_phy_iter_data wpid = {
67 return class_for_each_device(&wpan_phy_class, NULL,
68 &wpid, wpan_phy_iter);
70 EXPORT_SYMBOL(wpan_phy_for_each);
72 struct cfg802154_registered_device *
73 cfg802154_rdev_by_wpan_phy_idx(int wpan_phy_idx)
75 struct cfg802154_registered_device *result = NULL, *rdev;
79 list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
80 if (rdev->wpan_phy_idx == wpan_phy_idx) {
89 struct wpan_phy *wpan_phy_idx_to_wpan_phy(int wpan_phy_idx)
91 struct cfg802154_registered_device *rdev;
95 rdev = cfg802154_rdev_by_wpan_phy_idx(wpan_phy_idx);
98 return &rdev->wpan_phy;
102 wpan_phy_new(const struct cfg802154_ops *ops, size_t priv_size)
104 static atomic_t wpan_phy_counter = ATOMIC_INIT(0);
105 struct cfg802154_registered_device *rdev;
108 alloc_size = sizeof(*rdev) + priv_size;
109 rdev = kzalloc(alloc_size, GFP_KERNEL);
115 rdev->wpan_phy_idx = atomic_inc_return(&wpan_phy_counter);
117 if (unlikely(rdev->wpan_phy_idx < 0)) {
119 atomic_dec(&wpan_phy_counter);
124 /* atomic_inc_return makes it start at 1, make it start at 0 */
125 rdev->wpan_phy_idx--;
127 INIT_LIST_HEAD(&rdev->wpan_dev_list);
128 device_initialize(&rdev->wpan_phy.dev);
129 dev_set_name(&rdev->wpan_phy.dev, PHY_NAME "%d", rdev->wpan_phy_idx);
131 rdev->wpan_phy.dev.class = &wpan_phy_class;
132 rdev->wpan_phy.dev.platform_data = rdev;
134 wpan_phy_net_set(&rdev->wpan_phy, &init_net);
136 init_waitqueue_head(&rdev->dev_wait);
138 return &rdev->wpan_phy;
140 EXPORT_SYMBOL(wpan_phy_new);
142 int wpan_phy_register(struct wpan_phy *phy)
144 struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy);
148 ret = device_add(&phy->dev);
154 list_add_rcu(&rdev->list, &cfg802154_rdev_list);
155 cfg802154_rdev_list_generation++;
157 /* TODO phy registered lock */
160 /* TODO nl802154 phy notify */
164 EXPORT_SYMBOL(wpan_phy_register);
166 void wpan_phy_unregister(struct wpan_phy *phy)
168 struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy);
170 wait_event(rdev->dev_wait, ({
173 __count = rdev->opencount;
178 /* TODO nl802154 phy notify */
179 /* TODO phy registered lock */
181 WARN_ON(!list_empty(&rdev->wpan_dev_list));
183 /* First remove the hardware from everywhere, this makes
184 * it impossible to find from userspace.
186 list_del_rcu(&rdev->list);
189 cfg802154_rdev_list_generation++;
191 device_del(&phy->dev);
195 EXPORT_SYMBOL(wpan_phy_unregister);
197 void wpan_phy_free(struct wpan_phy *phy)
199 put_device(&phy->dev);
201 EXPORT_SYMBOL(wpan_phy_free);
203 int cfg802154_switch_netns(struct cfg802154_registered_device *rdev,
206 struct wpan_dev *wpan_dev;
209 list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
210 if (!wpan_dev->netdev)
212 wpan_dev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
213 err = dev_change_net_namespace(wpan_dev->netdev, net, "wpan%d");
216 wpan_dev->netdev->features |= NETIF_F_NETNS_LOCAL;
220 /* failed -- clean up to old netns */
221 net = wpan_phy_net(&rdev->wpan_phy);
223 list_for_each_entry_continue_reverse(wpan_dev,
224 &rdev->wpan_dev_list,
226 if (!wpan_dev->netdev)
228 wpan_dev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
229 err = dev_change_net_namespace(wpan_dev->netdev, net,
232 wpan_dev->netdev->features |= NETIF_F_NETNS_LOCAL;
238 wpan_phy_net_set(&rdev->wpan_phy, net);
240 err = device_rename(&rdev->wpan_phy.dev, dev_name(&rdev->wpan_phy.dev));
246 void cfg802154_dev_free(struct cfg802154_registered_device *rdev)
252 cfg802154_update_iface_num(struct cfg802154_registered_device *rdev,
257 rdev->num_running_ifaces += num;
260 static int cfg802154_netdev_notifier_call(struct notifier_block *nb,
261 unsigned long state, void *ptr)
263 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
264 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
265 struct cfg802154_registered_device *rdev;
270 rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy);
272 /* TODO WARN_ON unspec type */
275 /* TODO NETDEV_DEVTYPE */
276 case NETDEV_REGISTER:
277 dev->features |= NETIF_F_NETNS_LOCAL;
278 wpan_dev->identifier = ++rdev->wpan_dev_id;
279 list_add_rcu(&wpan_dev->list, &rdev->wpan_dev_list);
280 rdev->devlist_generation++;
282 wpan_dev->netdev = dev;
285 cfg802154_update_iface_num(rdev, wpan_dev->iftype, -1);
288 wake_up(&rdev->dev_wait);
291 cfg802154_update_iface_num(rdev, wpan_dev->iftype, 1);
295 case NETDEV_UNREGISTER:
296 /* It is possible to get NETDEV_UNREGISTER
297 * multiple times. To detect that, check
298 * that the interface is still on the list
299 * of registered interfaces, and only then
300 * remove and clean it up.
302 if (!list_empty(&wpan_dev->list)) {
303 list_del_rcu(&wpan_dev->list);
304 rdev->devlist_generation++;
306 /* synchronize (so that we won't find this netdev
307 * from other code any more) and then clear the list
308 * head so that the above code can safely check for
309 * !list_empty() to avoid double-cleanup.
312 INIT_LIST_HEAD(&wpan_dev->list);
321 static struct notifier_block cfg802154_netdev_notifier = {
322 .notifier_call = cfg802154_netdev_notifier_call,
325 static void __net_exit cfg802154_pernet_exit(struct net *net)
327 struct cfg802154_registered_device *rdev;
330 list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
331 if (net_eq(wpan_phy_net(&rdev->wpan_phy), net))
332 WARN_ON(cfg802154_switch_netns(rdev, &init_net));
337 static struct pernet_operations cfg802154_pernet_ops = {
338 .exit = cfg802154_pernet_exit,
341 static int __init wpan_phy_class_init(void)
345 rc = register_pernet_device(&cfg802154_pernet_ops);
349 rc = wpan_phy_sysfs_init();
353 rc = register_netdevice_notifier(&cfg802154_netdev_notifier);
357 rc = ieee802154_nl_init();
361 rc = nl802154_init();
363 goto err_ieee802154_nl;
368 ieee802154_nl_exit();
371 unregister_netdevice_notifier(&cfg802154_netdev_notifier);
373 wpan_phy_sysfs_exit();
375 unregister_pernet_device(&cfg802154_pernet_ops);
379 subsys_initcall(wpan_phy_class_init);
381 static void __exit wpan_phy_class_exit(void)
384 ieee802154_nl_exit();
385 unregister_netdevice_notifier(&cfg802154_netdev_notifier);
386 wpan_phy_sysfs_exit();
387 unregister_pernet_device(&cfg802154_pernet_ops);
389 module_exit(wpan_phy_class_exit);
391 MODULE_LICENSE("GPL v2");
392 MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
393 MODULE_AUTHOR("Dmitry Eremin-Solenikov");