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 struct wpan_phy
*wpan_phy_find(const char *str
)
33 dev
= class_find_device_by_name(&wpan_phy_class
, str
);
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
);
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
),
57 struct wpan_phy_iter_data wpid
= {
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
;
74 list_for_each_entry(rdev
, &cfg802154_rdev_list
, list
) {
75 if (rdev
->wpan_phy_idx
== wpan_phy_idx
) {
84 struct wpan_phy
*wpan_phy_idx_to_wpan_phy(int wpan_phy_idx
)
86 struct cfg802154_registered_device
*rdev
;
90 rdev
= cfg802154_rdev_by_wpan_phy_idx(wpan_phy_idx
);
93 return &rdev
->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
;
103 alloc_size
= sizeof(*rdev
) + priv_size
;
104 rdev
= kzalloc(alloc_size
, GFP_KERNEL
);
110 rdev
->wpan_phy_idx
= atomic_inc_return(&wpan_phy_counter
);
112 if (unlikely(rdev
->wpan_phy_idx
< 0)) {
114 atomic_dec(&wpan_phy_counter
);
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
);
133 return &rdev
->wpan_phy
;
135 EXPORT_SYMBOL(wpan_phy_new
);
137 int wpan_phy_register(struct wpan_phy
*phy
)
139 struct cfg802154_registered_device
*rdev
= wpan_phy_to_rdev(phy
);
143 ret
= device_add(&phy
->dev
);
149 list_add_rcu(&rdev
->list
, &cfg802154_rdev_list
);
150 cfg802154_rdev_list_generation
++;
152 /* TODO phy registered lock */
155 /* TODO nl802154 phy notify */
159 EXPORT_SYMBOL(wpan_phy_register
);
161 void wpan_phy_unregister(struct wpan_phy
*phy
)
163 struct cfg802154_registered_device
*rdev
= wpan_phy_to_rdev(phy
);
165 wait_event(rdev
->dev_wait
, ({
168 __count
= rdev
->opencount
;
173 /* TODO nl802154 phy notify */
174 /* TODO phy registered lock */
176 WARN_ON(!list_empty(&rdev
->wpan_dev_list
));
178 /* First remove the hardware from everywhere, this makes
179 * it impossible to find from userspace.
181 list_del_rcu(&rdev
->list
);
184 cfg802154_rdev_list_generation
++;
186 device_del(&phy
->dev
);
190 EXPORT_SYMBOL(wpan_phy_unregister
);
192 void wpan_phy_free(struct wpan_phy
*phy
)
194 put_device(&phy
->dev
);
196 EXPORT_SYMBOL(wpan_phy_free
);
198 int cfg802154_switch_netns(struct cfg802154_registered_device
*rdev
,
201 struct wpan_dev
*wpan_dev
;
204 list_for_each_entry(wpan_dev
, &rdev
->wpan_dev_list
, list
) {
205 if (!wpan_dev
->netdev
)
207 wpan_dev
->netdev
->features
&= ~NETIF_F_NETNS_LOCAL
;
208 err
= dev_change_net_namespace(wpan_dev
->netdev
, net
, "wpan%d");
211 wpan_dev
->netdev
->features
|= NETIF_F_NETNS_LOCAL
;
215 /* failed -- clean up to old netns */
216 net
= wpan_phy_net(&rdev
->wpan_phy
);
218 list_for_each_entry_continue_reverse(wpan_dev
,
219 &rdev
->wpan_dev_list
,
221 if (!wpan_dev
->netdev
)
223 wpan_dev
->netdev
->features
&= ~NETIF_F_NETNS_LOCAL
;
224 err
= dev_change_net_namespace(wpan_dev
->netdev
, net
,
227 wpan_dev
->netdev
->features
|= NETIF_F_NETNS_LOCAL
;
233 wpan_phy_net_set(&rdev
->wpan_phy
, net
);
235 err
= device_rename(&rdev
->wpan_phy
.dev
, dev_name(&rdev
->wpan_phy
.dev
));
241 void cfg802154_dev_free(struct cfg802154_registered_device
*rdev
)
247 cfg802154_update_iface_num(struct cfg802154_registered_device
*rdev
,
252 rdev
->num_running_ifaces
+= num
;
255 static int cfg802154_netdev_notifier_call(struct notifier_block
*nb
,
256 unsigned long state
, void *ptr
)
258 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
259 struct wpan_dev
*wpan_dev
= dev
->ieee802154_ptr
;
260 struct cfg802154_registered_device
*rdev
;
265 rdev
= wpan_phy_to_rdev(wpan_dev
->wpan_phy
);
267 /* TODO WARN_ON unspec type */
270 /* TODO NETDEV_DEVTYPE */
271 case NETDEV_REGISTER
:
272 dev
->features
|= NETIF_F_NETNS_LOCAL
;
273 wpan_dev
->identifier
= ++rdev
->wpan_dev_id
;
274 list_add_rcu(&wpan_dev
->list
, &rdev
->wpan_dev_list
);
275 rdev
->devlist_generation
++;
277 wpan_dev
->netdev
= dev
;
280 cfg802154_update_iface_num(rdev
, wpan_dev
->iftype
, -1);
283 wake_up(&rdev
->dev_wait
);
286 cfg802154_update_iface_num(rdev
, wpan_dev
->iftype
, 1);
290 case NETDEV_UNREGISTER
:
291 /* It is possible to get NETDEV_UNREGISTER
292 * multiple times. To detect that, check
293 * that the interface is still on the list
294 * of registered interfaces, and only then
295 * remove and clean it up.
297 if (!list_empty(&wpan_dev
->list
)) {
298 list_del_rcu(&wpan_dev
->list
);
299 rdev
->devlist_generation
++;
301 /* synchronize (so that we won't find this netdev
302 * from other code any more) and then clear the list
303 * head so that the above code can safely check for
304 * !list_empty() to avoid double-cleanup.
307 INIT_LIST_HEAD(&wpan_dev
->list
);
316 static struct notifier_block cfg802154_netdev_notifier
= {
317 .notifier_call
= cfg802154_netdev_notifier_call
,
320 static void __net_exit
cfg802154_pernet_exit(struct net
*net
)
322 struct cfg802154_registered_device
*rdev
;
325 list_for_each_entry(rdev
, &cfg802154_rdev_list
, list
) {
326 if (net_eq(wpan_phy_net(&rdev
->wpan_phy
), net
))
327 WARN_ON(cfg802154_switch_netns(rdev
, &init_net
));
332 static struct pernet_operations cfg802154_pernet_ops
= {
333 .exit
= cfg802154_pernet_exit
,
336 static int __init
wpan_phy_class_init(void)
340 rc
= register_pernet_device(&cfg802154_pernet_ops
);
344 rc
= wpan_phy_sysfs_init();
348 rc
= register_netdevice_notifier(&cfg802154_netdev_notifier
);
352 rc
= ieee802154_nl_init();
356 rc
= nl802154_init();
358 goto err_ieee802154_nl
;
363 ieee802154_nl_exit();
366 unregister_netdevice_notifier(&cfg802154_netdev_notifier
);
368 wpan_phy_sysfs_exit();
370 unregister_pernet_device(&cfg802154_pernet_ops
);
374 subsys_initcall(wpan_phy_class_init
);
376 static void __exit
wpan_phy_class_exit(void)
379 ieee802154_nl_exit();
380 unregister_netdevice_notifier(&cfg802154_netdev_notifier
);
381 wpan_phy_sysfs_exit();
382 unregister_pernet_device(&cfg802154_pernet_ops
);
384 module_exit(wpan_phy_class_exit
);
386 MODULE_LICENSE("GPL v2");
387 MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
388 MODULE_AUTHOR("Dmitry Eremin-Solenikov");