Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[linux-2.6-block.git] / net / ieee802154 / wpan-class.c
CommitLineData
2bfb1070
DES
1/*
2 * Copyright (C) 2007, 2008, 2009 Siemens AG
3 *
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.
7 *
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.
12 *
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.
16 *
17 */
18
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/device.h>
22
23#include <net/wpan-phy.h>
24
cb6b3763
DES
25#include "ieee802154.h"
26
2bfb1070
DES
27#define MASTER_SHOW_COMPLEX(name, format_string, args...) \
28static ssize_t name ## _show(struct device *dev, \
29 struct device_attribute *attr, char *buf) \
30{ \
31 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev); \
32 int ret; \
33 \
34 mutex_lock(&phy->pib_lock); \
375bb0e0 35 ret = snprintf(buf, PAGE_SIZE, format_string "\n", args); \
2bfb1070
DES
36 mutex_unlock(&phy->pib_lock); \
37 return ret; \
38}
39
40#define MASTER_SHOW(field, format_string) \
41 MASTER_SHOW_COMPLEX(field, format_string, phy->field)
42
43MASTER_SHOW(current_channel, "%d");
44MASTER_SHOW(current_page, "%d");
2bfb1070
DES
45MASTER_SHOW_COMPLEX(transmit_power, "%d +- %d dB",
46 ((signed char) (phy->transmit_power << 2)) >> 2,
47 (phy->transmit_power >> 6) ? (phy->transmit_power >> 6) * 3 : 1 );
48MASTER_SHOW(cca_mode, "%d");
49
a0b4a738
DES
50static ssize_t channels_supported_show(struct device *dev,
51 struct device_attribute *attr, char *buf)
52{
53 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
54 int ret;
55 int i, len = 0;
56
57 mutex_lock(&phy->pib_lock);
58 for (i = 0; i < 32; i++) {
59 ret = snprintf(buf + len, PAGE_SIZE - len,
60 "%#09x\n", phy->channels_supported[i]);
61 if (ret < 0)
62 break;
63 len += ret;
64 }
65 mutex_unlock(&phy->pib_lock);
66 return len;
67}
68
2bfb1070
DES
69static struct device_attribute pmib_attrs[] = {
70 __ATTR_RO(current_channel),
71 __ATTR_RO(current_page),
72 __ATTR_RO(channels_supported),
73 __ATTR_RO(transmit_power),
74 __ATTR_RO(cca_mode),
75 {},
76};
77
78static void wpan_phy_release(struct device *d)
79{
80 struct wpan_phy *phy = container_of(d, struct wpan_phy, dev);
81 kfree(phy);
82}
83
84static struct class wpan_phy_class = {
85 .name = "ieee802154",
86 .dev_release = wpan_phy_release,
87 .dev_attrs = pmib_attrs,
88};
89
90static DEFINE_MUTEX(wpan_phy_mutex);
91static int wpan_phy_idx;
92
93static int wpan_phy_match(struct device *dev, void *data)
94{
95 return !strcmp(dev_name(dev), (const char *)data);
96}
97
98struct wpan_phy *wpan_phy_find(const char *str)
99{
100 struct device *dev;
101
102 if (WARN_ON(!str))
103 return NULL;
104
105 dev = class_find_device(&wpan_phy_class, NULL,
106 (void *)str, wpan_phy_match);
107 if (!dev)
108 return NULL;
109
110 return container_of(dev, struct wpan_phy, dev);
111}
112EXPORT_SYMBOL(wpan_phy_find);
113
1c889f4d
DES
114struct wpan_phy_iter_data {
115 int (*fn)(struct wpan_phy *phy, void *data);
116 void *data;
117};
118
119static int wpan_phy_iter(struct device *dev, void *_data)
120{
121 struct wpan_phy_iter_data *wpid = _data;
122 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
123 return wpid->fn(phy, wpid->data);
124}
125
126int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data),
127 void *data)
128{
129 struct wpan_phy_iter_data wpid = {
130 .fn = fn,
131 .data = data,
132 };
133
134 return class_for_each_device(&wpan_phy_class, NULL,
135 &wpid, wpan_phy_iter);
136}
137EXPORT_SYMBOL(wpan_phy_for_each);
138
2bfb1070
DES
139static int wpan_phy_idx_valid(int idx)
140{
141 return idx >= 0;
142}
143
144struct wpan_phy *wpan_phy_alloc(size_t priv_size)
145{
146 struct wpan_phy *phy = kzalloc(sizeof(*phy) + priv_size,
147 GFP_KERNEL);
148
149 mutex_lock(&wpan_phy_mutex);
150 phy->idx = wpan_phy_idx++;
151 if (unlikely(!wpan_phy_idx_valid(phy->idx))) {
152 wpan_phy_idx--;
153 mutex_unlock(&wpan_phy_mutex);
154 kfree(phy);
155 return NULL;
156 }
157 mutex_unlock(&wpan_phy_mutex);
158
159 mutex_init(&phy->pib_lock);
160
161 device_initialize(&phy->dev);
162 dev_set_name(&phy->dev, "wpan-phy%d", phy->idx);
163
164 phy->dev.class = &wpan_phy_class;
165
37eb0edc
DES
166 phy->current_channel = -1; /* not initialised */
167 phy->current_page = 0; /* for compatibility */
168
2bfb1070
DES
169 return phy;
170}
171EXPORT_SYMBOL(wpan_phy_alloc);
172
e9cf356c 173int wpan_phy_register(struct wpan_phy *phy)
2bfb1070 174{
2bfb1070
DES
175 return device_add(&phy->dev);
176}
177EXPORT_SYMBOL(wpan_phy_register);
178
179void wpan_phy_unregister(struct wpan_phy *phy)
180{
181 device_del(&phy->dev);
182}
183EXPORT_SYMBOL(wpan_phy_unregister);
184
185void wpan_phy_free(struct wpan_phy *phy)
186{
187 put_device(&phy->dev);
188}
189EXPORT_SYMBOL(wpan_phy_free);
190
191static int __init wpan_phy_class_init(void)
192{
cb6b3763
DES
193 int rc;
194 rc = class_register(&wpan_phy_class);
195 if (rc)
196 goto err;
197
198 rc = ieee802154_nl_init();
199 if (rc)
200 goto err_nl;
201
202 return 0;
203err_nl:
204 class_unregister(&wpan_phy_class);
205err:
206 return rc;
2bfb1070 207}
282a3954 208subsys_initcall(wpan_phy_class_init);
2bfb1070
DES
209
210static void __exit wpan_phy_class_exit(void)
211{
cb6b3763 212 ieee802154_nl_exit();
2bfb1070
DES
213 class_unregister(&wpan_phy_class);
214}
215module_exit(wpan_phy_class_exit);
216
2bfb1070 217MODULE_LICENSE("GPL v2");
cb6b3763
DES
218MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
219MODULE_AUTHOR("Dmitry Eremin-Solenikov");
2bfb1070 220