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"
27 /* RCU-protected (and RTNL for writers) */
28 static LIST_HEAD(cfg802154_rdev_list
);
29 static int cfg802154_rdev_list_generation
;
31 static int wpan_phy_match(struct device
*dev
, const void *data
)
33 return !strcmp(dev_name(dev
), (const char *)data
);
36 struct wpan_phy
*wpan_phy_find(const char *str
)
43 dev
= class_find_device(&wpan_phy_class
, NULL
, str
, wpan_phy_match
);
47 return container_of(dev
, struct wpan_phy
, dev
);
49 EXPORT_SYMBOL(wpan_phy_find
);
51 struct wpan_phy_iter_data
{
52 int (*fn
)(struct wpan_phy
*phy
, void *data
);
56 static int wpan_phy_iter(struct device
*dev
, void *_data
)
58 struct wpan_phy_iter_data
*wpid
= _data
;
59 struct wpan_phy
*phy
= container_of(dev
, struct wpan_phy
, dev
);
61 return wpid
->fn(phy
, wpid
->data
);
64 int wpan_phy_for_each(int (*fn
)(struct wpan_phy
*phy
, void *data
),
67 struct wpan_phy_iter_data wpid
= {
72 return class_for_each_device(&wpan_phy_class
, NULL
,
73 &wpid
, wpan_phy_iter
);
75 EXPORT_SYMBOL(wpan_phy_for_each
);
78 wpan_phy_new(const struct cfg802154_ops
*ops
, size_t priv_size
)
80 static atomic_t wpan_phy_counter
= ATOMIC_INIT(0);
81 struct cfg802154_registered_device
*rdev
;
84 alloc_size
= sizeof(*rdev
) + priv_size
;
85 rdev
= kzalloc(alloc_size
, GFP_KERNEL
);
91 rdev
->wpan_phy_idx
= atomic_inc_return(&wpan_phy_counter
);
93 if (unlikely(rdev
->wpan_phy_idx
< 0)) {
95 atomic_dec(&wpan_phy_counter
);
100 /* atomic_inc_return makes it start at 1, make it start at 0 */
101 rdev
->wpan_phy_idx
--;
103 mutex_init(&rdev
->wpan_phy
.pib_lock
);
105 device_initialize(&rdev
->wpan_phy
.dev
);
106 dev_set_name(&rdev
->wpan_phy
.dev
, "wpan-phy%d", rdev
->wpan_phy_idx
);
108 rdev
->wpan_phy
.dev
.class = &wpan_phy_class
;
109 rdev
->wpan_phy
.dev
.platform_data
= rdev
;
111 return &rdev
->wpan_phy
;
113 EXPORT_SYMBOL(wpan_phy_new
);
115 int wpan_phy_register(struct wpan_phy
*phy
)
117 struct cfg802154_registered_device
*rdev
= wpan_phy_to_rdev(phy
);
121 ret
= device_add(&phy
->dev
);
127 list_add_rcu(&rdev
->list
, &cfg802154_rdev_list
);
128 cfg802154_rdev_list_generation
++;
130 /* TODO phy registered lock */
133 /* TODO nl802154 phy notify */
137 EXPORT_SYMBOL(wpan_phy_register
);
139 void wpan_phy_unregister(struct wpan_phy
*phy
)
141 struct cfg802154_registered_device
*rdev
= wpan_phy_to_rdev(phy
);
143 /* TODO open count */
146 /* TODO nl802154 phy notify */
147 /* TODO phy registered lock */
149 /* TODO WARN_ON wpan_dev_list */
151 /* First remove the hardware from everywhere, this makes
152 * it impossible to find from userspace.
154 list_del_rcu(&rdev
->list
);
157 cfg802154_rdev_list_generation
++;
159 device_del(&phy
->dev
);
163 EXPORT_SYMBOL(wpan_phy_unregister
);
165 void wpan_phy_free(struct wpan_phy
*phy
)
167 put_device(&phy
->dev
);
169 EXPORT_SYMBOL(wpan_phy_free
);
171 void cfg802154_dev_free(struct cfg802154_registered_device
*rdev
)
176 static int __init
wpan_phy_class_init(void)
180 rc
= wpan_phy_sysfs_init();
184 rc
= ieee802154_nl_init();
190 wpan_phy_sysfs_exit();
194 subsys_initcall(wpan_phy_class_init
);
196 static void __exit
wpan_phy_class_exit(void)
198 ieee802154_nl_exit();
199 wpan_phy_sysfs_exit();
201 module_exit(wpan_phy_class_exit
);
203 MODULE_LICENSE("GPL v2");
204 MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
205 MODULE_AUTHOR("Dmitry Eremin-Solenikov");