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 /* RCU-protected (and RTNL for writers) */
29 LIST_HEAD(cfg802154_rdev_list
);
30 int cfg802154_rdev_list_generation
;
32 static int wpan_phy_match(struct device
*dev
, const void *data
)
34 return !strcmp(dev_name(dev
), (const char *)data
);
37 struct wpan_phy
*wpan_phy_find(const char *str
)
44 dev
= class_find_device(&wpan_phy_class
, NULL
, str
, wpan_phy_match
);
48 return container_of(dev
, struct wpan_phy
, dev
);
50 EXPORT_SYMBOL(wpan_phy_find
);
52 struct wpan_phy_iter_data
{
53 int (*fn
)(struct wpan_phy
*phy
, void *data
);
57 static int wpan_phy_iter(struct device
*dev
, void *_data
)
59 struct wpan_phy_iter_data
*wpid
= _data
;
60 struct wpan_phy
*phy
= container_of(dev
, struct wpan_phy
, dev
);
62 return wpid
->fn(phy
, wpid
->data
);
65 int wpan_phy_for_each(int (*fn
)(struct wpan_phy
*phy
, void *data
),
68 struct wpan_phy_iter_data wpid
= {
73 return class_for_each_device(&wpan_phy_class
, NULL
,
74 &wpid
, wpan_phy_iter
);
76 EXPORT_SYMBOL(wpan_phy_for_each
);
78 struct cfg802154_registered_device
*
79 cfg802154_rdev_by_wpan_phy_idx(int wpan_phy_idx
)
81 struct cfg802154_registered_device
*result
= NULL
, *rdev
;
85 list_for_each_entry(rdev
, &cfg802154_rdev_list
, list
) {
86 if (rdev
->wpan_phy_idx
== wpan_phy_idx
) {
96 wpan_phy_new(const struct cfg802154_ops
*ops
, size_t priv_size
)
98 static atomic_t wpan_phy_counter
= ATOMIC_INIT(0);
99 struct cfg802154_registered_device
*rdev
;
102 alloc_size
= sizeof(*rdev
) + priv_size
;
103 rdev
= kzalloc(alloc_size
, GFP_KERNEL
);
109 rdev
->wpan_phy_idx
= atomic_inc_return(&wpan_phy_counter
);
111 if (unlikely(rdev
->wpan_phy_idx
< 0)) {
113 atomic_dec(&wpan_phy_counter
);
118 /* atomic_inc_return makes it start at 1, make it start at 0 */
119 rdev
->wpan_phy_idx
--;
121 mutex_init(&rdev
->wpan_phy
.pib_lock
);
123 INIT_LIST_HEAD(&rdev
->wpan_dev_list
);
124 device_initialize(&rdev
->wpan_phy
.dev
);
125 dev_set_name(&rdev
->wpan_phy
.dev
, "wpan-phy%d", rdev
->wpan_phy_idx
);
127 rdev
->wpan_phy
.dev
.class = &wpan_phy_class
;
128 rdev
->wpan_phy
.dev
.platform_data
= rdev
;
130 init_waitqueue_head(&rdev
->dev_wait
);
132 return &rdev
->wpan_phy
;
134 EXPORT_SYMBOL(wpan_phy_new
);
136 int wpan_phy_register(struct wpan_phy
*phy
)
138 struct cfg802154_registered_device
*rdev
= wpan_phy_to_rdev(phy
);
142 ret
= device_add(&phy
->dev
);
148 list_add_rcu(&rdev
->list
, &cfg802154_rdev_list
);
149 cfg802154_rdev_list_generation
++;
151 /* TODO phy registered lock */
154 /* TODO nl802154 phy notify */
158 EXPORT_SYMBOL(wpan_phy_register
);
160 void wpan_phy_unregister(struct wpan_phy
*phy
)
162 struct cfg802154_registered_device
*rdev
= wpan_phy_to_rdev(phy
);
164 wait_event(rdev
->dev_wait
, ({
167 __count
= rdev
->opencount
;
172 /* TODO nl802154 phy notify */
173 /* TODO phy registered lock */
175 WARN_ON(!list_empty(&rdev
->wpan_dev_list
));
177 /* First remove the hardware from everywhere, this makes
178 * it impossible to find from userspace.
180 list_del_rcu(&rdev
->list
);
183 cfg802154_rdev_list_generation
++;
185 device_del(&phy
->dev
);
189 EXPORT_SYMBOL(wpan_phy_unregister
);
191 void wpan_phy_free(struct wpan_phy
*phy
)
193 put_device(&phy
->dev
);
195 EXPORT_SYMBOL(wpan_phy_free
);
197 void cfg802154_dev_free(struct cfg802154_registered_device
*rdev
)
203 cfg802154_update_iface_num(struct cfg802154_registered_device
*rdev
,
208 rdev
->num_running_ifaces
+= num
;
211 static int cfg802154_netdev_notifier_call(struct notifier_block
*nb
,
212 unsigned long state
, void *ptr
)
214 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
215 struct wpan_dev
*wpan_dev
= dev
->ieee802154_ptr
;
216 struct cfg802154_registered_device
*rdev
;
221 rdev
= wpan_phy_to_rdev(wpan_dev
->wpan_phy
);
223 /* TODO WARN_ON unspec type */
226 /* TODO NETDEV_DEVTYPE */
227 case NETDEV_REGISTER
:
228 wpan_dev
->identifier
= ++rdev
->wpan_dev_id
;
229 list_add_rcu(&wpan_dev
->list
, &rdev
->wpan_dev_list
);
230 rdev
->devlist_generation
++;
232 wpan_dev
->netdev
= dev
;
235 cfg802154_update_iface_num(rdev
, wpan_dev
->iftype
, -1);
238 wake_up(&rdev
->dev_wait
);
241 cfg802154_update_iface_num(rdev
, wpan_dev
->iftype
, 1);
245 case NETDEV_UNREGISTER
:
246 /* It is possible to get NETDEV_UNREGISTER
247 * multiple times. To detect that, check
248 * that the interface is still on the list
249 * of registered interfaces, and only then
250 * remove and clean it up.
252 if (!list_empty(&wpan_dev
->list
)) {
253 list_del_rcu(&wpan_dev
->list
);
254 rdev
->devlist_generation
++;
256 /* synchronize (so that we won't find this netdev
257 * from other code any more) and then clear the list
258 * head so that the above code can safely check for
259 * !list_empty() to avoid double-cleanup.
262 INIT_LIST_HEAD(&wpan_dev
->list
);
271 static struct notifier_block cfg802154_netdev_notifier
= {
272 .notifier_call
= cfg802154_netdev_notifier_call
,
275 static int __init
wpan_phy_class_init(void)
279 rc
= wpan_phy_sysfs_init();
283 rc
= register_netdevice_notifier(&cfg802154_netdev_notifier
);
287 rc
= ieee802154_nl_init();
291 rc
= nl802154_init();
293 goto err_ieee802154_nl
;
298 ieee802154_nl_exit();
301 unregister_netdevice_notifier(&cfg802154_netdev_notifier
);
303 wpan_phy_sysfs_exit();
307 subsys_initcall(wpan_phy_class_init
);
309 static void __exit
wpan_phy_class_exit(void)
312 ieee802154_nl_exit();
313 unregister_netdevice_notifier(&cfg802154_netdev_notifier
);
314 wpan_phy_sysfs_exit();
316 module_exit(wpan_phy_class_exit
);
318 MODULE_LICENSE("GPL v2");
319 MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
320 MODULE_AUTHOR("Dmitry Eremin-Solenikov");