blob: 0c51f85aa59132dbd52244689703bd956bb720e8 [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
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/device.h>
22
23#include <net/wpan-phy.h>
24
25#define MASTER_SHOW_COMPLEX(name, format_string, args...) \
26static ssize_t name ## _show(struct device *dev, \
27 struct device_attribute *attr, char *buf) \
28{ \
29 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev); \
30 int ret; \
31 \
32 mutex_lock(&phy->pib_lock); \
Dmitry Eremin-Solenikov375bb0e2009-09-28 18:58:32 +040033 ret = snprintf(buf, PAGE_SIZE, format_string "\n", args); \
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040034 mutex_unlock(&phy->pib_lock); \
35 return ret; \
36}
37
38#define MASTER_SHOW(field, format_string) \
39 MASTER_SHOW_COMPLEX(field, format_string, phy->field)
40
41MASTER_SHOW(current_channel, "%d");
42MASTER_SHOW(current_page, "%d");
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040043MASTER_SHOW_COMPLEX(transmit_power, "%d +- %d dB",
44 ((signed char) (phy->transmit_power << 2)) >> 2,
45 (phy->transmit_power >> 6) ? (phy->transmit_power >> 6) * 3 : 1 );
46MASTER_SHOW(cca_mode, "%d");
47
Dmitry Eremin-Solenikova0b4a732009-09-22 15:26:48 +040048static ssize_t channels_supported_show(struct device *dev,
49 struct device_attribute *attr, char *buf)
50{
51 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
52 int ret;
53 int i, len = 0;
54
55 mutex_lock(&phy->pib_lock);
56 for (i = 0; i < 32; i++) {
57 ret = snprintf(buf + len, PAGE_SIZE - len,
58 "%#09x\n", phy->channels_supported[i]);
59 if (ret < 0)
60 break;
61 len += ret;
62 }
63 mutex_unlock(&phy->pib_lock);
64 return len;
65}
66
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040067static struct device_attribute pmib_attrs[] = {
68 __ATTR_RO(current_channel),
69 __ATTR_RO(current_page),
70 __ATTR_RO(channels_supported),
71 __ATTR_RO(transmit_power),
72 __ATTR_RO(cca_mode),
73 {},
74};
75
76static void wpan_phy_release(struct device *d)
77{
78 struct wpan_phy *phy = container_of(d, struct wpan_phy, dev);
79 kfree(phy);
80}
81
82static struct class wpan_phy_class = {
83 .name = "ieee802154",
84 .dev_release = wpan_phy_release,
85 .dev_attrs = pmib_attrs,
86};
87
88static DEFINE_MUTEX(wpan_phy_mutex);
89static int wpan_phy_idx;
90
91static int wpan_phy_match(struct device *dev, void *data)
92{
93 return !strcmp(dev_name(dev), (const char *)data);
94}
95
96struct wpan_phy *wpan_phy_find(const char *str)
97{
98 struct device *dev;
99
100 if (WARN_ON(!str))
101 return NULL;
102
103 dev = class_find_device(&wpan_phy_class, NULL,
104 (void *)str, wpan_phy_match);
105 if (!dev)
106 return NULL;
107
108 return container_of(dev, struct wpan_phy, dev);
109}
110EXPORT_SYMBOL(wpan_phy_find);
111
Dmitry Eremin-Solenikov1c889f42009-09-15 16:57:04 +0400112struct wpan_phy_iter_data {
113 int (*fn)(struct wpan_phy *phy, void *data);
114 void *data;
115};
116
117static int wpan_phy_iter(struct device *dev, void *_data)
118{
119 struct wpan_phy_iter_data *wpid = _data;
120 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
121 return wpid->fn(phy, wpid->data);
122}
123
124int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data),
125 void *data)
126{
127 struct wpan_phy_iter_data wpid = {
128 .fn = fn,
129 .data = data,
130 };
131
132 return class_for_each_device(&wpan_phy_class, NULL,
133 &wpid, wpan_phy_iter);
134}
135EXPORT_SYMBOL(wpan_phy_for_each);
136
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400137static int wpan_phy_idx_valid(int idx)
138{
139 return idx >= 0;
140}
141
142struct wpan_phy *wpan_phy_alloc(size_t priv_size)
143{
144 struct wpan_phy *phy = kzalloc(sizeof(*phy) + priv_size,
145 GFP_KERNEL);
146
147 mutex_lock(&wpan_phy_mutex);
148 phy->idx = wpan_phy_idx++;
149 if (unlikely(!wpan_phy_idx_valid(phy->idx))) {
150 wpan_phy_idx--;
151 mutex_unlock(&wpan_phy_mutex);
152 kfree(phy);
153 return NULL;
154 }
155 mutex_unlock(&wpan_phy_mutex);
156
157 mutex_init(&phy->pib_lock);
158
159 device_initialize(&phy->dev);
160 dev_set_name(&phy->dev, "wpan-phy%d", phy->idx);
161
162 phy->dev.class = &wpan_phy_class;
163
Dmitry Eremin-Solenikov37eb0ed2009-09-18 16:35:06 +0400164 phy->current_channel = -1; /* not initialised */
165 phy->current_page = 0; /* for compatibility */
166
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400167 return phy;
168}
169EXPORT_SYMBOL(wpan_phy_alloc);
170
171int wpan_phy_register(struct device *parent, struct wpan_phy *phy)
172{
173 phy->dev.parent = parent;
174
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{
193 return class_register(&wpan_phy_class);
194}
195subsys_initcall(wpan_phy_class_init);
196
197static void __exit wpan_phy_class_exit(void)
198{
199 class_unregister(&wpan_phy_class);
200}
201module_exit(wpan_phy_class_exit);
202
203MODULE_DESCRIPTION("IEEE 802.15.4 device class");
204MODULE_LICENSE("GPL v2");
205