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.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <linux/slab.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/device.h>
24 #include <net/wpan-phy.h>
26 #include "ieee802154.h"
28 #define MASTER_SHOW_COMPLEX(name, format_string, args...) \
29 static ssize_t name ## _show(struct device *dev, \
30 struct device_attribute *attr, char *buf) \
32 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev); \
35 mutex_lock(&phy->pib_lock); \
36 ret = snprintf(buf, PAGE_SIZE, format_string "\n", args); \
37 mutex_unlock(&phy->pib_lock); \
40 static DEVICE_ATTR_RO(name);
42 #define MASTER_SHOW(field, format_string) \
43 MASTER_SHOW_COMPLEX(field, format_string, phy->field)
45 MASTER_SHOW(current_channel
, "%d");
46 MASTER_SHOW(current_page
, "%d");
47 MASTER_SHOW(transmit_power
, "%d +- 1 dB");
48 MASTER_SHOW(cca_mode
, "%d");
50 static ssize_t
channels_supported_show(struct device
*dev
,
51 struct device_attribute
*attr
,
54 struct wpan_phy
*phy
= container_of(dev
, struct wpan_phy
, dev
);
58 mutex_lock(&phy
->pib_lock
);
59 for (i
= 0; i
< 32; i
++) {
60 ret
= snprintf(buf
+ len
, PAGE_SIZE
- len
,
61 "%#09x\n", phy
->channels_supported
[i
]);
66 mutex_unlock(&phy
->pib_lock
);
69 static DEVICE_ATTR_RO(channels_supported
);
71 static struct attribute
*pmib_attrs
[] = {
72 &dev_attr_current_channel
.attr
,
73 &dev_attr_current_page
.attr
,
74 &dev_attr_channels_supported
.attr
,
75 &dev_attr_transmit_power
.attr
,
76 &dev_attr_cca_mode
.attr
,
79 ATTRIBUTE_GROUPS(pmib
);
81 static void wpan_phy_release(struct device
*d
)
83 struct wpan_phy
*phy
= container_of(d
, struct wpan_phy
, dev
);
88 static struct class wpan_phy_class
= {
90 .dev_release
= wpan_phy_release
,
91 .dev_groups
= pmib_groups
,
94 static DEFINE_MUTEX(wpan_phy_mutex
);
95 static int wpan_phy_idx
;
97 static int wpan_phy_match(struct device
*dev
, const void *data
)
99 return !strcmp(dev_name(dev
), (const char *)data
);
102 struct wpan_phy
*wpan_phy_find(const char *str
)
109 dev
= class_find_device(&wpan_phy_class
, NULL
, str
, wpan_phy_match
);
113 return container_of(dev
, struct wpan_phy
, dev
);
115 EXPORT_SYMBOL(wpan_phy_find
);
117 struct wpan_phy_iter_data
{
118 int (*fn
)(struct wpan_phy
*phy
, void *data
);
122 static int wpan_phy_iter(struct device
*dev
, void *_data
)
124 struct wpan_phy_iter_data
*wpid
= _data
;
125 struct wpan_phy
*phy
= container_of(dev
, struct wpan_phy
, dev
);
127 return wpid
->fn(phy
, wpid
->data
);
130 int wpan_phy_for_each(int (*fn
)(struct wpan_phy
*phy
, void *data
),
133 struct wpan_phy_iter_data wpid
= {
138 return class_for_each_device(&wpan_phy_class
, NULL
,
139 &wpid
, wpan_phy_iter
);
141 EXPORT_SYMBOL(wpan_phy_for_each
);
143 static int wpan_phy_idx_valid(int idx
)
148 struct wpan_phy
*wpan_phy_alloc(size_t priv_size
)
150 struct wpan_phy
*phy
= kzalloc(sizeof(*phy
) + priv_size
,
155 mutex_lock(&wpan_phy_mutex
);
156 phy
->idx
= wpan_phy_idx
++;
157 if (unlikely(!wpan_phy_idx_valid(phy
->idx
))) {
159 mutex_unlock(&wpan_phy_mutex
);
163 mutex_unlock(&wpan_phy_mutex
);
165 mutex_init(&phy
->pib_lock
);
167 device_initialize(&phy
->dev
);
168 dev_set_name(&phy
->dev
, "wpan-phy%d", phy
->idx
);
170 phy
->dev
.class = &wpan_phy_class
;
172 phy
->current_channel
= -1; /* not initialised */
173 phy
->current_page
= 0; /* for compatibility */
180 EXPORT_SYMBOL(wpan_phy_alloc
);
182 int wpan_phy_register(struct wpan_phy
*phy
)
184 return device_add(&phy
->dev
);
186 EXPORT_SYMBOL(wpan_phy_register
);
188 void wpan_phy_unregister(struct wpan_phy
*phy
)
190 device_del(&phy
->dev
);
192 EXPORT_SYMBOL(wpan_phy_unregister
);
194 void wpan_phy_free(struct wpan_phy
*phy
)
196 put_device(&phy
->dev
);
198 EXPORT_SYMBOL(wpan_phy_free
);
200 static int __init
wpan_phy_class_init(void)
204 rc
= class_register(&wpan_phy_class
);
208 rc
= ieee802154_nl_init();
214 class_unregister(&wpan_phy_class
);
218 subsys_initcall(wpan_phy_class_init
);
220 static void __exit
wpan_phy_class_exit(void)
222 ieee802154_nl_exit();
223 class_unregister(&wpan_phy_class
);
225 module_exit(wpan_phy_class_exit
);
227 MODULE_LICENSE("GPL v2");
228 MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
229 MODULE_AUTHOR("Dmitry Eremin-Solenikov");