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>
22 #include "ieee802154.h"
25 static DEFINE_MUTEX(wpan_phy_mutex);
26 static int wpan_phy_idx;
28 static int wpan_phy_match(struct device *dev, const void *data)
30 return !strcmp(dev_name(dev), (const char *)data);
33 struct wpan_phy *wpan_phy_find(const char *str)
40 dev = class_find_device(&wpan_phy_class, NULL, str, wpan_phy_match);
44 return container_of(dev, struct wpan_phy, dev);
46 EXPORT_SYMBOL(wpan_phy_find);
48 struct wpan_phy_iter_data {
49 int (*fn)(struct wpan_phy *phy, void *data);
53 static int wpan_phy_iter(struct device *dev, void *_data)
55 struct wpan_phy_iter_data *wpid = _data;
56 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
58 return wpid->fn(phy, wpid->data);
61 int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data),
64 struct wpan_phy_iter_data wpid = {
69 return class_for_each_device(&wpan_phy_class, NULL,
70 &wpid, wpan_phy_iter);
72 EXPORT_SYMBOL(wpan_phy_for_each);
74 static int wpan_phy_idx_valid(int idx)
79 struct wpan_phy *wpan_phy_alloc(size_t priv_size)
81 struct wpan_phy *phy = kzalloc(sizeof(*phy) + priv_size,
86 mutex_lock(&wpan_phy_mutex);
87 phy->idx = wpan_phy_idx++;
88 if (unlikely(!wpan_phy_idx_valid(phy->idx))) {
90 mutex_unlock(&wpan_phy_mutex);
94 mutex_unlock(&wpan_phy_mutex);
96 mutex_init(&phy->pib_lock);
98 device_initialize(&phy->dev);
99 dev_set_name(&phy->dev, "wpan-phy%d", phy->idx);
101 phy->dev.class = &wpan_phy_class;
103 phy->current_channel = -1; /* not initialised */
104 phy->current_page = 0; /* for compatibility */
111 EXPORT_SYMBOL(wpan_phy_alloc);
113 int wpan_phy_register(struct wpan_phy *phy)
115 return device_add(&phy->dev);
117 EXPORT_SYMBOL(wpan_phy_register);
119 void wpan_phy_unregister(struct wpan_phy *phy)
121 device_del(&phy->dev);
123 EXPORT_SYMBOL(wpan_phy_unregister);
125 void wpan_phy_free(struct wpan_phy *phy)
127 put_device(&phy->dev);
129 EXPORT_SYMBOL(wpan_phy_free);
131 static int __init wpan_phy_class_init(void)
135 rc = wpan_phy_sysfs_init();
139 rc = ieee802154_nl_init();
145 wpan_phy_sysfs_exit();
149 subsys_initcall(wpan_phy_class_init);
151 static void __exit wpan_phy_class_exit(void)
153 ieee802154_nl_exit();
154 wpan_phy_sysfs_exit();
156 module_exit(wpan_phy_class_exit);
158 MODULE_LICENSE("GPL v2");
159 MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
160 MODULE_AUTHOR("Dmitry Eremin-Solenikov");