blob: ef56ab5b35fe759c446089b68cefdc585d21c175 [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");
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040047MASTER_SHOW_COMPLEX(transmit_power, "%d +- %d dB",
48 ((signed char) (phy->transmit_power << 2)) >> 2,
49 (phy->transmit_power >> 6) ? (phy->transmit_power >> 6) * 3 : 1 );
50MASTER_SHOW(cca_mode, "%d");
51
Dmitry Eremin-Solenikova0b4a732009-09-22 15:26:48 +040052static ssize_t channels_supported_show(struct device *dev,
53 struct device_attribute *attr, char *buf)
54{
55 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
56 int ret;
57 int i, len = 0;
58
59 mutex_lock(&phy->pib_lock);
60 for (i = 0; i < 32; i++) {
61 ret = snprintf(buf + len, PAGE_SIZE - len,
62 "%#09x\n", phy->channels_supported[i]);
63 if (ret < 0)
64 break;
65 len += ret;
66 }
67 mutex_unlock(&phy->pib_lock);
68 return len;
69}
Greg Kroah-Hartman7f4708a2013-07-24 15:05:34 -070070static DEVICE_ATTR_RO(channels_supported);
Dmitry Eremin-Solenikova0b4a732009-09-22 15:26:48 +040071
Greg Kroah-Hartman7f4708a2013-07-24 15:05:34 -070072static struct attribute *pmib_attrs[] = {
73 &dev_attr_current_channel.attr,
74 &dev_attr_current_page.attr,
75 &dev_attr_channels_supported.attr,
76 &dev_attr_transmit_power.attr,
77 &dev_attr_cca_mode.attr,
78 NULL,
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040079};
Greg Kroah-Hartman7f4708a2013-07-24 15:05:34 -070080ATTRIBUTE_GROUPS(pmib);
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040081
82static void wpan_phy_release(struct device *d)
83{
84 struct wpan_phy *phy = container_of(d, struct wpan_phy, dev);
85 kfree(phy);
86}
87
88static struct class wpan_phy_class = {
89 .name = "ieee802154",
90 .dev_release = wpan_phy_release,
Greg Kroah-Hartman7f4708a2013-07-24 15:05:34 -070091 .dev_groups = pmib_groups,
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040092};
93
94static DEFINE_MUTEX(wpan_phy_mutex);
95static int wpan_phy_idx;
96
Michał Mirosław9f3b7952013-02-01 20:40:17 +010097static int wpan_phy_match(struct device *dev, const void *data)
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040098{
99 return !strcmp(dev_name(dev), (const char *)data);
100}
101
102struct wpan_phy *wpan_phy_find(const char *str)
103{
104 struct device *dev;
105
106 if (WARN_ON(!str))
107 return NULL;
108
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100109 dev = class_find_device(&wpan_phy_class, NULL, str, wpan_phy_match);
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400110 if (!dev)
111 return NULL;
112
113 return container_of(dev, struct wpan_phy, dev);
114}
115EXPORT_SYMBOL(wpan_phy_find);
116
Dmitry Eremin-Solenikov1c889f42009-09-15 16:57:04 +0400117struct wpan_phy_iter_data {
118 int (*fn)(struct wpan_phy *phy, void *data);
119 void *data;
120};
121
122static int wpan_phy_iter(struct device *dev, void *_data)
123{
124 struct wpan_phy_iter_data *wpid = _data;
125 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
126 return wpid->fn(phy, wpid->data);
127}
128
129int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data),
130 void *data)
131{
132 struct wpan_phy_iter_data wpid = {
133 .fn = fn,
134 .data = data,
135 };
136
137 return class_for_each_device(&wpan_phy_class, NULL,
138 &wpid, wpan_phy_iter);
139}
140EXPORT_SYMBOL(wpan_phy_for_each);
141
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400142static int wpan_phy_idx_valid(int idx)
143{
144 return idx >= 0;
145}
146
147struct wpan_phy *wpan_phy_alloc(size_t priv_size)
148{
149 struct wpan_phy *phy = kzalloc(sizeof(*phy) + priv_size,
150 GFP_KERNEL);
151
Denis Kirjanova6c0f822010-05-23 05:45:45 +0000152 if (!phy)
153 goto out;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400154 mutex_lock(&wpan_phy_mutex);
155 phy->idx = wpan_phy_idx++;
156 if (unlikely(!wpan_phy_idx_valid(phy->idx))) {
157 wpan_phy_idx--;
158 mutex_unlock(&wpan_phy_mutex);
159 kfree(phy);
Denis Kirjanova6c0f822010-05-23 05:45:45 +0000160 goto out;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400161 }
162 mutex_unlock(&wpan_phy_mutex);
163
164 mutex_init(&phy->pib_lock);
165
166 device_initialize(&phy->dev);
167 dev_set_name(&phy->dev, "wpan-phy%d", phy->idx);
168
169 phy->dev.class = &wpan_phy_class;
170
Dmitry Eremin-Solenikov37eb0ed2009-09-18 16:35:06 +0400171 phy->current_channel = -1; /* not initialised */
172 phy->current_page = 0; /* for compatibility */
173
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400174 return phy;
Denis Kirjanova6c0f822010-05-23 05:45:45 +0000175
176out:
177 return NULL;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400178}
179EXPORT_SYMBOL(wpan_phy_alloc);
180
Dmitry Eremin-Solenikove9cf3562009-09-28 19:01:20 +0400181int wpan_phy_register(struct wpan_phy *phy)
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400182{
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400183 return device_add(&phy->dev);
184}
185EXPORT_SYMBOL(wpan_phy_register);
186
187void wpan_phy_unregister(struct wpan_phy *phy)
188{
189 device_del(&phy->dev);
190}
191EXPORT_SYMBOL(wpan_phy_unregister);
192
193void wpan_phy_free(struct wpan_phy *phy)
194{
195 put_device(&phy->dev);
196}
197EXPORT_SYMBOL(wpan_phy_free);
198
199static int __init wpan_phy_class_init(void)
200{
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400201 int rc;
202 rc = class_register(&wpan_phy_class);
203 if (rc)
204 goto err;
205
206 rc = ieee802154_nl_init();
207 if (rc)
208 goto err_nl;
209
210 return 0;
211err_nl:
212 class_unregister(&wpan_phy_class);
213err:
214 return rc;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400215}
Dmitry Eremin-Solenikov282a3952009-11-12 23:58:23 +0300216subsys_initcall(wpan_phy_class_init);
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400217
218static void __exit wpan_phy_class_exit(void)
219{
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400220 ieee802154_nl_exit();
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400221 class_unregister(&wpan_phy_class);
222}
223module_exit(wpan_phy_class_exit);
224
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400225MODULE_LICENSE("GPL v2");
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400226MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
227MODULE_AUTHOR("Dmitry Eremin-Solenikov");
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400228