2 * Copyright (C) 2007, 2008, 2009 Siemens AG
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.
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.
15 #include <linux/slab.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/device.h>
20 #include <net/cfg802154.h>
21 #include <net/rtnetlink.h>
23 #include "ieee802154.h"
28 /* name for sysfs, %d is appended */
29 #define PHY_NAME "phy"
31 /* RCU-protected (and RTNL for writers) */
32 LIST_HEAD(cfg802154_rdev_list);
33 int cfg802154_rdev_list_generation;
35 static int wpan_phy_match(struct device *dev, const void *data)
37 return !strcmp(dev_name(dev), (const char *)data);
40 struct wpan_phy *wpan_phy_find(const char *str)
47 dev = class_find_device(&wpan_phy_class, NULL, str, wpan_phy_match);
51 return container_of(dev, struct wpan_phy, dev);
53 EXPORT_SYMBOL(wpan_phy_find);
55 struct wpan_phy_iter_data {
56 int (*fn)(struct wpan_phy *phy, void *data);
60 static int wpan_phy_iter(struct device *dev, void *_data)
62 struct wpan_phy_iter_data *wpid = _data;
63 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
65 return wpid->fn(phy, wpid->data);
68 int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data),
71 struct wpan_phy_iter_data wpid = {
76 return class_for_each_device(&wpan_phy_class, NULL,
77 &wpid, wpan_phy_iter);
79 EXPORT_SYMBOL(wpan_phy_for_each);
81 struct cfg802154_registered_device *
82 cfg802154_rdev_by_wpan_phy_idx(int wpan_phy_idx)
84 struct cfg802154_registered_device *result = NULL, *rdev;
88 list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
89 if (rdev->wpan_phy_idx == wpan_phy_idx) {
98 struct wpan_phy *wpan_phy_idx_to_wpan_phy(int wpan_phy_idx)
100 struct cfg802154_registered_device *rdev;
104 rdev = cfg802154_rdev_by_wpan_phy_idx(wpan_phy_idx);
107 return &rdev->wpan_phy;
111 wpan_phy_new(const struct cfg802154_ops *ops, size_t priv_size)
113 static atomic_t wpan_phy_counter = ATOMIC_INIT(0);
114 struct cfg802154_registered_device *rdev;
117 alloc_size = sizeof(*rdev) + priv_size;
118 rdev = kzalloc(alloc_size, GFP_KERNEL);
124 rdev->wpan_phy_idx = atomic_inc_return(&wpan_phy_counter);
126 if (unlikely(rdev->wpan_phy_idx < 0)) {
128 atomic_dec(&wpan_phy_counter);
133 /* atomic_inc_return makes it start at 1, make it start at 0 */
134 rdev->wpan_phy_idx--;
136 INIT_LIST_HEAD(&rdev->wpan_dev_list);
137 device_initialize(&rdev->wpan_phy.dev);
138 dev_set_name(&rdev->wpan_phy.dev, PHY_NAME "%d", rdev->wpan_phy_idx);
140 rdev->wpan_phy.dev.class = &wpan_phy_class;
141 rdev->wpan_phy.dev.platform_data = rdev;
143 wpan_phy_net_set(&rdev->wpan_phy, &init_net);
145 init_waitqueue_head(&rdev->dev_wait);
147 return &rdev->wpan_phy;
149 EXPORT_SYMBOL(wpan_phy_new);
151 int wpan_phy_register(struct wpan_phy *phy)
153 struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy);
157 ret = device_add(&phy->dev);
163 list_add_rcu(&rdev->list, &cfg802154_rdev_list);
164 cfg802154_rdev_list_generation++;
166 /* TODO phy registered lock */
169 /* TODO nl802154 phy notify */
173 EXPORT_SYMBOL(wpan_phy_register);
175 void wpan_phy_unregister(struct wpan_phy *phy)
177 struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy);
179 wait_event(rdev->dev_wait, ({
182 __count = rdev->opencount;
187 /* TODO nl802154 phy notify */
188 /* TODO phy registered lock */
190 WARN_ON(!list_empty(&rdev->wpan_dev_list));
192 /* First remove the hardware from everywhere, this makes
193 * it impossible to find from userspace.
195 list_del_rcu(&rdev->list);
198 cfg802154_rdev_list_generation++;
200 device_del(&phy->dev);
204 EXPORT_SYMBOL(wpan_phy_unregister);
206 void wpan_phy_free(struct wpan_phy *phy)
208 put_device(&phy->dev);
210 EXPORT_SYMBOL(wpan_phy_free);
212 int cfg802154_switch_netns(struct cfg802154_registered_device *rdev,
215 struct wpan_dev *wpan_dev;
218 list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
219 if (!wpan_dev->netdev)
221 wpan_dev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
222 err = dev_change_net_namespace(wpan_dev->netdev, net, "wpan%d");
225 wpan_dev->netdev->features |= NETIF_F_NETNS_LOCAL;
229 /* failed -- clean up to old netns */
230 net = wpan_phy_net(&rdev->wpan_phy);
232 list_for_each_entry_continue_reverse(wpan_dev,
233 &rdev->wpan_dev_list,
235 if (!wpan_dev->netdev)
237 wpan_dev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
238 err = dev_change_net_namespace(wpan_dev->netdev, net,
241 wpan_dev->netdev->features |= NETIF_F_NETNS_LOCAL;
247 wpan_phy_net_set(&rdev->wpan_phy, net);
249 err = device_rename(&rdev->wpan_phy.dev, dev_name(&rdev->wpan_phy.dev));
255 void cfg802154_dev_free(struct cfg802154_registered_device *rdev)
261 cfg802154_update_iface_num(struct cfg802154_registered_device *rdev,
266 rdev->num_running_ifaces += num;
269 static int cfg802154_netdev_notifier_call(struct notifier_block *nb,
270 unsigned long state, void *ptr)
272 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
273 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
274 struct cfg802154_registered_device *rdev;
279 rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy);
281 /* TODO WARN_ON unspec type */
284 /* TODO NETDEV_DEVTYPE */
285 case NETDEV_REGISTER:
286 dev->features |= NETIF_F_NETNS_LOCAL;
287 wpan_dev->identifier = ++rdev->wpan_dev_id;
288 list_add_rcu(&wpan_dev->list, &rdev->wpan_dev_list);
289 rdev->devlist_generation++;
291 wpan_dev->netdev = dev;
294 cfg802154_update_iface_num(rdev, wpan_dev->iftype, -1);
297 wake_up(&rdev->dev_wait);
300 cfg802154_update_iface_num(rdev, wpan_dev->iftype, 1);
304 case NETDEV_UNREGISTER:
305 /* It is possible to get NETDEV_UNREGISTER
306 * multiple times. To detect that, check
307 * that the interface is still on the list
308 * of registered interfaces, and only then
309 * remove and clean it up.
311 if (!list_empty(&wpan_dev->list)) {
312 list_del_rcu(&wpan_dev->list);
313 rdev->devlist_generation++;
315 /* synchronize (so that we won't find this netdev
316 * from other code any more) and then clear the list
317 * head so that the above code can safely check for
318 * !list_empty() to avoid double-cleanup.
321 INIT_LIST_HEAD(&wpan_dev->list);
330 static struct notifier_block cfg802154_netdev_notifier = {
331 .notifier_call = cfg802154_netdev_notifier_call,
334 static void __net_exit cfg802154_pernet_exit(struct net *net)
336 struct cfg802154_registered_device *rdev;
339 list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
340 if (net_eq(wpan_phy_net(&rdev->wpan_phy), net))
341 WARN_ON(cfg802154_switch_netns(rdev, &init_net));
346 static struct pernet_operations cfg802154_pernet_ops = {
347 .exit = cfg802154_pernet_exit,
350 static int __init wpan_phy_class_init(void)
354 rc = register_pernet_device(&cfg802154_pernet_ops);
358 rc = wpan_phy_sysfs_init();
362 rc = register_netdevice_notifier(&cfg802154_netdev_notifier);
366 rc = ieee802154_nl_init();
370 rc = nl802154_init();
372 goto err_ieee802154_nl;
377 ieee802154_nl_exit();
380 unregister_netdevice_notifier(&cfg802154_netdev_notifier);
382 wpan_phy_sysfs_exit();
384 unregister_pernet_device(&cfg802154_pernet_ops);
388 subsys_initcall(wpan_phy_class_init);
390 static void __exit wpan_phy_class_exit(void)
393 ieee802154_nl_exit();
394 unregister_netdevice_notifier(&cfg802154_netdev_notifier);
395 wpan_phy_sysfs_exit();
396 unregister_pernet_device(&cfg802154_pernet_ops);
398 module_exit(wpan_phy_class_exit);
400 MODULE_LICENSE("GPL v2");
401 MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
402 MODULE_AUTHOR("Dmitry Eremin-Solenikov");