blob: 8d6f6704da84e8b95745cedb7040230cb5b96cb1 [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; \
Greg Kroah-Hartman7f4708a2013-07-24 15:05:34 -070039} \
40static DEVICE_ATTR_RO(name);
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040041
42#define MASTER_SHOW(field, format_string) \
43 MASTER_SHOW_COMPLEX(field, format_string, phy->field)
44
45MASTER_SHOW(current_channel, "%d");
46MASTER_SHOW(current_page, "%d");
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +010047MASTER_SHOW(transmit_power, "%d +- 1 dB");
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040048MASTER_SHOW(cca_mode, "%d");
49
Dmitry Eremin-Solenikova0b4a732009-09-22 15:26:48 +040050static 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}
Greg Kroah-Hartman7f4708a2013-07-24 15:05:34 -070068static DEVICE_ATTR_RO(channels_supported);
Dmitry Eremin-Solenikova0b4a732009-09-22 15:26:48 +040069
Greg Kroah-Hartman7f4708a2013-07-24 15:05:34 -070070static struct attribute *pmib_attrs[] = {
71 &dev_attr_current_channel.attr,
72 &dev_attr_current_page.attr,
73 &dev_attr_channels_supported.attr,
74 &dev_attr_transmit_power.attr,
75 &dev_attr_cca_mode.attr,
76 NULL,
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040077};
Greg Kroah-Hartman7f4708a2013-07-24 15:05:34 -070078ATTRIBUTE_GROUPS(pmib);
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040079
80static void wpan_phy_release(struct device *d)
81{
82 struct wpan_phy *phy = container_of(d, struct wpan_phy, dev);
83 kfree(phy);
84}
85
86static struct class wpan_phy_class = {
87 .name = "ieee802154",
88 .dev_release = wpan_phy_release,
Greg Kroah-Hartman7f4708a2013-07-24 15:05:34 -070089 .dev_groups = pmib_groups,
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040090};
91
92static DEFINE_MUTEX(wpan_phy_mutex);
93static int wpan_phy_idx;
94
Michał Mirosław9f3b7952013-02-01 20:40:17 +010095static int wpan_phy_match(struct device *dev, const void *data)
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040096{
97 return !strcmp(dev_name(dev), (const char *)data);
98}
99
100struct wpan_phy *wpan_phy_find(const char *str)
101{
102 struct device *dev;
103
104 if (WARN_ON(!str))
105 return NULL;
106
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100107 dev = class_find_device(&wpan_phy_class, NULL, str, wpan_phy_match);
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400108 if (!dev)
109 return NULL;
110
111 return container_of(dev, struct wpan_phy, dev);
112}
113EXPORT_SYMBOL(wpan_phy_find);
114
Dmitry Eremin-Solenikov1c889f42009-09-15 16:57:04 +0400115struct wpan_phy_iter_data {
116 int (*fn)(struct wpan_phy *phy, void *data);
117 void *data;
118};
119
120static int wpan_phy_iter(struct device *dev, void *_data)
121{
122 struct wpan_phy_iter_data *wpid = _data;
123 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
124 return wpid->fn(phy, wpid->data);
125}
126
127int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data),
128 void *data)
129{
130 struct wpan_phy_iter_data wpid = {
131 .fn = fn,
132 .data = data,
133 };
134
135 return class_for_each_device(&wpan_phy_class, NULL,
136 &wpid, wpan_phy_iter);
137}
138EXPORT_SYMBOL(wpan_phy_for_each);
139
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400140static int wpan_phy_idx_valid(int idx)
141{
142 return idx >= 0;
143}
144
145struct wpan_phy *wpan_phy_alloc(size_t priv_size)
146{
147 struct wpan_phy *phy = kzalloc(sizeof(*phy) + priv_size,
148 GFP_KERNEL);
149
Denis Kirjanova6c0f822010-05-23 05:45:45 +0000150 if (!phy)
151 goto out;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400152 mutex_lock(&wpan_phy_mutex);
153 phy->idx = wpan_phy_idx++;
154 if (unlikely(!wpan_phy_idx_valid(phy->idx))) {
155 wpan_phy_idx--;
156 mutex_unlock(&wpan_phy_mutex);
157 kfree(phy);
Denis Kirjanova6c0f822010-05-23 05:45:45 +0000158 goto out;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400159 }
160 mutex_unlock(&wpan_phy_mutex);
161
162 mutex_init(&phy->pib_lock);
163
164 device_initialize(&phy->dev);
165 dev_set_name(&phy->dev, "wpan-phy%d", phy->idx);
166
167 phy->dev.class = &wpan_phy_class;
168
Dmitry Eremin-Solenikov37eb0ed2009-09-18 16:35:06 +0400169 phy->current_channel = -1; /* not initialised */
170 phy->current_page = 0; /* for compatibility */
171
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400172 return phy;
Denis Kirjanova6c0f822010-05-23 05:45:45 +0000173
174out:
175 return NULL;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400176}
177EXPORT_SYMBOL(wpan_phy_alloc);
178
Dmitry Eremin-Solenikove9cf3562009-09-28 19:01:20 +0400179int wpan_phy_register(struct wpan_phy *phy)
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400180{
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400181 return device_add(&phy->dev);
182}
183EXPORT_SYMBOL(wpan_phy_register);
184
185void wpan_phy_unregister(struct wpan_phy *phy)
186{
187 device_del(&phy->dev);
188}
189EXPORT_SYMBOL(wpan_phy_unregister);
190
191void wpan_phy_free(struct wpan_phy *phy)
192{
193 put_device(&phy->dev);
194}
195EXPORT_SYMBOL(wpan_phy_free);
196
197static int __init wpan_phy_class_init(void)
198{
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400199 int rc;
200 rc = class_register(&wpan_phy_class);
201 if (rc)
202 goto err;
203
204 rc = ieee802154_nl_init();
205 if (rc)
206 goto err_nl;
207
208 return 0;
209err_nl:
210 class_unregister(&wpan_phy_class);
211err:
212 return rc;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400213}
Dmitry Eremin-Solenikov282a3952009-11-12 23:58:23 +0300214subsys_initcall(wpan_phy_class_init);
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400215
216static void __exit wpan_phy_class_exit(void)
217{
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400218 ieee802154_nl_exit();
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400219 class_unregister(&wpan_phy_class);
220}
221module_exit(wpan_phy_class_exit);
222
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400223MODULE_LICENSE("GPL v2");
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400224MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
225MODULE_AUTHOR("Dmitry Eremin-Solenikov");
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400226