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"
24 #define MASTER_SHOW_COMPLEX(name, format_string, args...) \
25 static ssize_t name ## _show(struct device *dev, \
26 struct device_attribute *attr, char *buf) \
28 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev); \
31 mutex_lock(&phy->pib_lock); \
32 ret = snprintf(buf, PAGE_SIZE, format_string "\n", args); \
33 mutex_unlock(&phy->pib_lock); \
36 static DEVICE_ATTR_RO(name)
38 #define MASTER_SHOW(field, format_string) \
39 MASTER_SHOW_COMPLEX(field, format_string, phy->field)
41 MASTER_SHOW(current_channel
, "%d");
42 MASTER_SHOW(current_page
, "%d");
43 MASTER_SHOW(transmit_power
, "%d +- 1 dB");
44 MASTER_SHOW(cca_mode
, "%d");
46 static ssize_t
channels_supported_show(struct device
*dev
,
47 struct device_attribute
*attr
,
50 struct wpan_phy
*phy
= container_of(dev
, struct wpan_phy
, dev
);
54 mutex_lock(&phy
->pib_lock
);
55 for (i
= 0; i
< 32; i
++) {
56 ret
= snprintf(buf
+ len
, PAGE_SIZE
- len
,
57 "%#09x\n", phy
->channels_supported
[i
]);
62 mutex_unlock(&phy
->pib_lock
);
65 static DEVICE_ATTR_RO(channels_supported
);
67 static struct attribute
*pmib_attrs
[] = {
68 &dev_attr_current_channel
.attr
,
69 &dev_attr_current_page
.attr
,
70 &dev_attr_channels_supported
.attr
,
71 &dev_attr_transmit_power
.attr
,
72 &dev_attr_cca_mode
.attr
,
75 ATTRIBUTE_GROUPS(pmib
);
77 static void wpan_phy_release(struct device
*d
)
79 struct wpan_phy
*phy
= container_of(d
, struct wpan_phy
, dev
);
84 static struct class wpan_phy_class
= {
86 .dev_release
= wpan_phy_release
,
87 .dev_groups
= pmib_groups
,
90 static DEFINE_MUTEX(wpan_phy_mutex
);
91 static int wpan_phy_idx
;
93 static int wpan_phy_match(struct device
*dev
, const void *data
)
95 return !strcmp(dev_name(dev
), (const char *)data
);
98 struct wpan_phy
*wpan_phy_find(const char *str
)
105 dev
= class_find_device(&wpan_phy_class
, NULL
, str
, wpan_phy_match
);
109 return container_of(dev
, struct wpan_phy
, dev
);
111 EXPORT_SYMBOL(wpan_phy_find
);
113 struct wpan_phy_iter_data
{
114 int (*fn
)(struct wpan_phy
*phy
, void *data
);
118 static int wpan_phy_iter(struct device
*dev
, void *_data
)
120 struct wpan_phy_iter_data
*wpid
= _data
;
121 struct wpan_phy
*phy
= container_of(dev
, struct wpan_phy
, dev
);
123 return wpid
->fn(phy
, wpid
->data
);
126 int wpan_phy_for_each(int (*fn
)(struct wpan_phy
*phy
, void *data
),
129 struct wpan_phy_iter_data wpid
= {
134 return class_for_each_device(&wpan_phy_class
, NULL
,
135 &wpid
, wpan_phy_iter
);
137 EXPORT_SYMBOL(wpan_phy_for_each
);
139 static int wpan_phy_idx_valid(int idx
)
144 struct wpan_phy
*wpan_phy_alloc(size_t priv_size
)
146 struct wpan_phy
*phy
= kzalloc(sizeof(*phy
) + priv_size
,
151 mutex_lock(&wpan_phy_mutex
);
152 phy
->idx
= wpan_phy_idx
++;
153 if (unlikely(!wpan_phy_idx_valid(phy
->idx
))) {
155 mutex_unlock(&wpan_phy_mutex
);
159 mutex_unlock(&wpan_phy_mutex
);
161 mutex_init(&phy
->pib_lock
);
163 device_initialize(&phy
->dev
);
164 dev_set_name(&phy
->dev
, "wpan-phy%d", phy
->idx
);
166 phy
->dev
.class = &wpan_phy_class
;
168 phy
->current_channel
= -1; /* not initialised */
169 phy
->current_page
= 0; /* for compatibility */
176 EXPORT_SYMBOL(wpan_phy_alloc
);
178 int wpan_phy_register(struct wpan_phy
*phy
)
180 return device_add(&phy
->dev
);
182 EXPORT_SYMBOL(wpan_phy_register
);
184 void wpan_phy_unregister(struct wpan_phy
*phy
)
186 device_del(&phy
->dev
);
188 EXPORT_SYMBOL(wpan_phy_unregister
);
190 void wpan_phy_free(struct wpan_phy
*phy
)
192 put_device(&phy
->dev
);
194 EXPORT_SYMBOL(wpan_phy_free
);
196 static int __init
wpan_phy_class_init(void)
200 rc
= class_register(&wpan_phy_class
);
204 rc
= ieee802154_nl_init();
210 class_unregister(&wpan_phy_class
);
214 subsys_initcall(wpan_phy_class_init
);
216 static void __exit
wpan_phy_class_exit(void)
218 ieee802154_nl_exit();
219 class_unregister(&wpan_phy_class
);
221 module_exit(wpan_phy_class_exit
);
223 MODULE_LICENSE("GPL v2");
224 MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
225 MODULE_AUTHOR("Dmitry Eremin-Solenikov");