blob: 13571eae6baec862f06857a39c5a735ba0b9bfea [file] [log] [blame]
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +04001/*
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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040020#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/device.h>
23
24#include <net/wpan-phy.h>
25
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +040026#include "ieee802154.h"
27
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040028#define MASTER_SHOW_COMPLEX(name, format_string, args...) \
29static ssize_t name ## _show(struct device *dev, \
30 struct device_attribute *attr, char *buf) \
31{ \
32 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev); \
33 int ret; \
34 \
35 mutex_lock(&phy->pib_lock); \
Dmitry Eremin-Solenikov375bb0e2009-09-28 18:58:32 +040036 ret = snprintf(buf, PAGE_SIZE, format_string "\n", args); \
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040037 mutex_unlock(&phy->pib_lock); \
38 return ret; \
39}
40
41#define MASTER_SHOW(field, format_string) \
42 MASTER_SHOW_COMPLEX(field, format_string, phy->field)
43
44MASTER_SHOW(current_channel, "%d");
45MASTER_SHOW(current_page, "%d");
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040046MASTER_SHOW_COMPLEX(transmit_power, "%d +- %d dB",
47 ((signed char) (phy->transmit_power << 2)) >> 2,
48 (phy->transmit_power >> 6) ? (phy->transmit_power >> 6) * 3 : 1 );
49MASTER_SHOW(cca_mode, "%d");
50
Dmitry Eremin-Solenikova0b4a732009-09-22 15:26:48 +040051static ssize_t channels_supported_show(struct device *dev,
52 struct device_attribute *attr, char *buf)
53{
54 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
55 int ret;
56 int i, len = 0;
57
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]);
62 if (ret < 0)
63 break;
64 len += ret;
65 }
66 mutex_unlock(&phy->pib_lock);
67 return len;
68}
69
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040070static struct device_attribute pmib_attrs[] = {
71 __ATTR_RO(current_channel),
72 __ATTR_RO(current_page),
73 __ATTR_RO(channels_supported),
74 __ATTR_RO(transmit_power),
75 __ATTR_RO(cca_mode),
76 {},
77};
78
79static void wpan_phy_release(struct device *d)
80{
81 struct wpan_phy *phy = container_of(d, struct wpan_phy, dev);
82 kfree(phy);
83}
84
85static struct class wpan_phy_class = {
86 .name = "ieee802154",
87 .dev_release = wpan_phy_release,
88 .dev_attrs = pmib_attrs,
89};
90
91static DEFINE_MUTEX(wpan_phy_mutex);
92static int wpan_phy_idx;
93
Michał Mirosław9f3b7952013-02-01 20:40:17 +010094static int wpan_phy_match(struct device *dev, const void *data)
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040095{
96 return !strcmp(dev_name(dev), (const char *)data);
97}
98
99struct wpan_phy *wpan_phy_find(const char *str)
100{
101 struct device *dev;
102
103 if (WARN_ON(!str))
104 return NULL;
105
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100106 dev = class_find_device(&wpan_phy_class, NULL, str, wpan_phy_match);
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400107 if (!dev)
108 return NULL;
109
110 return container_of(dev, struct wpan_phy, dev);
111}
112EXPORT_SYMBOL(wpan_phy_find);
113
Dmitry Eremin-Solenikov1c889f42009-09-15 16:57:04 +0400114struct 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
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400139static 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
Denis Kirjanova6c0f822010-05-23 05:45:45 +0000149 if (!phy)
150 goto out;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400151 mutex_lock(&wpan_phy_mutex);
152 phy->idx = wpan_phy_idx++;
153 if (unlikely(!wpan_phy_idx_valid(phy->idx))) {
154 wpan_phy_idx--;
155 mutex_unlock(&wpan_phy_mutex);
156 kfree(phy);
Denis Kirjanova6c0f822010-05-23 05:45:45 +0000157 goto out;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400158 }
159 mutex_unlock(&wpan_phy_mutex);
160
161 mutex_init(&phy->pib_lock);
162
163 device_initialize(&phy->dev);
164 dev_set_name(&phy->dev, "wpan-phy%d", phy->idx);
165
166 phy->dev.class = &wpan_phy_class;
167
Dmitry Eremin-Solenikov37eb0ed2009-09-18 16:35:06 +0400168 phy->current_channel = -1; /* not initialised */
169 phy->current_page = 0; /* for compatibility */
170
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400171 return phy;
Denis Kirjanova6c0f822010-05-23 05:45:45 +0000172
173out:
174 return NULL;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400175}
176EXPORT_SYMBOL(wpan_phy_alloc);
177
Dmitry Eremin-Solenikove9cf3562009-09-28 19:01:20 +0400178int wpan_phy_register(struct wpan_phy *phy)
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400179{
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400180 return device_add(&phy->dev);
181}
182EXPORT_SYMBOL(wpan_phy_register);
183
184void wpan_phy_unregister(struct wpan_phy *phy)
185{
186 device_del(&phy->dev);
187}
188EXPORT_SYMBOL(wpan_phy_unregister);
189
190void wpan_phy_free(struct wpan_phy *phy)
191{
192 put_device(&phy->dev);
193}
194EXPORT_SYMBOL(wpan_phy_free);
195
196static int __init wpan_phy_class_init(void)
197{
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400198 int rc;
199 rc = class_register(&wpan_phy_class);
200 if (rc)
201 goto err;
202
203 rc = ieee802154_nl_init();
204 if (rc)
205 goto err_nl;
206
207 return 0;
208err_nl:
209 class_unregister(&wpan_phy_class);
210err:
211 return rc;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400212}
Dmitry Eremin-Solenikov282a3952009-11-12 23:58:23 +0300213subsys_initcall(wpan_phy_class_init);
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400214
215static void __exit wpan_phy_class_exit(void)
216{
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400217 ieee802154_nl_exit();
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400218 class_unregister(&wpan_phy_class);
219}
220module_exit(wpan_phy_class_exit);
221
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400222MODULE_LICENSE("GPL v2");
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400223MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
224MODULE_AUTHOR("Dmitry Eremin-Solenikov");
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400225