Commit | Line | Data |
---|---|---|
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 | * | |
2bfb1070 DES |
13 | */ |
14 | ||
5a0e3ad6 | 15 | #include <linux/slab.h> |
2bfb1070 DES |
16 | #include <linux/kernel.h> |
17 | #include <linux/module.h> | |
18 | #include <linux/device.h> | |
19 | ||
5ad60d36 | 20 | #include <net/cfg802154.h> |
2bfb1070 | 21 | |
cb6b3763 DES |
22 | #include "ieee802154.h" |
23 | ||
2bfb1070 DES |
24 | #define MASTER_SHOW_COMPLEX(name, format_string, args...) \ |
25 | static ssize_t name ## _show(struct device *dev, \ | |
26 | struct device_attribute *attr, char *buf) \ | |
27 | { \ | |
28 | struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev); \ | |
29 | int ret; \ | |
30 | \ | |
31 | mutex_lock(&phy->pib_lock); \ | |
375bb0e0 | 32 | ret = snprintf(buf, PAGE_SIZE, format_string "\n", args); \ |
2bfb1070 DES |
33 | mutex_unlock(&phy->pib_lock); \ |
34 | return ret; \ | |
7f4708ab | 35 | } \ |
ed1da148 | 36 | static DEVICE_ATTR_RO(name) |
2bfb1070 DES |
37 | |
38 | #define MASTER_SHOW(field, format_string) \ | |
39 | MASTER_SHOW_COMPLEX(field, format_string, phy->field) | |
40 | ||
41 | MASTER_SHOW(current_channel, "%d"); | |
42 | MASTER_SHOW(current_page, "%d"); | |
9b2777d6 | 43 | MASTER_SHOW(transmit_power, "%d +- 1 dB"); |
2bfb1070 DES |
44 | MASTER_SHOW(cca_mode, "%d"); |
45 | ||
a0b4a738 | 46 | static ssize_t channels_supported_show(struct device *dev, |
4710d806 VB |
47 | struct device_attribute *attr, |
48 | char *buf) | |
a0b4a738 DES |
49 | { |
50 | struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev); | |
51 | int ret; | |
52 | int i, len = 0; | |
53 | ||
54 | mutex_lock(&phy->pib_lock); | |
55 | for (i = 0; i < 32; i++) { | |
56 | ret = snprintf(buf + len, PAGE_SIZE - len, | |
4710d806 | 57 | "%#09x\n", phy->channels_supported[i]); |
a0b4a738 DES |
58 | if (ret < 0) |
59 | break; | |
60 | len += ret; | |
61 | } | |
62 | mutex_unlock(&phy->pib_lock); | |
63 | return len; | |
64 | } | |
7f4708ab GKH |
65 | static DEVICE_ATTR_RO(channels_supported); |
66 | ||
67 | static struct attribute *pmib_attrs[] = { | |
68 | &dev_attr_current_channel.attr, | |
69 | &dev_attr_current_page.attr, | |
70 | &dev_attr_channels_supported.attr, | |
71 | &dev_attr_transmit_power.attr, | |
72 | &dev_attr_cca_mode.attr, | |
73 | NULL, | |
2bfb1070 | 74 | }; |
7f4708ab | 75 | ATTRIBUTE_GROUPS(pmib); |
2bfb1070 DES |
76 | |
77 | static void wpan_phy_release(struct device *d) | |
78 | { | |
79 | struct wpan_phy *phy = container_of(d, struct wpan_phy, dev); | |
4710d806 | 80 | |
2bfb1070 DES |
81 | kfree(phy); |
82 | } | |
83 | ||
84 | static struct class wpan_phy_class = { | |
85 | .name = "ieee802154", | |
86 | .dev_release = wpan_phy_release, | |
7f4708ab | 87 | .dev_groups = pmib_groups, |
2bfb1070 DES |
88 | }; |
89 | ||
90 | static DEFINE_MUTEX(wpan_phy_mutex); | |
91 | static int wpan_phy_idx; | |
92 | ||
9f3b795a | 93 | static int wpan_phy_match(struct device *dev, const void *data) |
2bfb1070 DES |
94 | { |
95 | return !strcmp(dev_name(dev), (const char *)data); | |
96 | } | |
97 | ||
98 | struct wpan_phy *wpan_phy_find(const char *str) | |
99 | { | |
100 | struct device *dev; | |
101 | ||
102 | if (WARN_ON(!str)) | |
103 | return NULL; | |
104 | ||
9f3b795a | 105 | dev = class_find_device(&wpan_phy_class, NULL, str, wpan_phy_match); |
2bfb1070 DES |
106 | if (!dev) |
107 | return NULL; | |
108 | ||
109 | return container_of(dev, struct wpan_phy, dev); | |
110 | } | |
111 | EXPORT_SYMBOL(wpan_phy_find); | |
112 | ||
1c889f4d DES |
113 | struct wpan_phy_iter_data { |
114 | int (*fn)(struct wpan_phy *phy, void *data); | |
115 | void *data; | |
116 | }; | |
117 | ||
118 | static int wpan_phy_iter(struct device *dev, void *_data) | |
119 | { | |
120 | struct wpan_phy_iter_data *wpid = _data; | |
121 | struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev); | |
4710d806 | 122 | |
1c889f4d DES |
123 | return wpid->fn(phy, wpid->data); |
124 | } | |
125 | ||
126 | int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data), | |
4710d806 | 127 | void *data) |
1c889f4d DES |
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 | } | |
137 | EXPORT_SYMBOL(wpan_phy_for_each); | |
138 | ||
2bfb1070 DES |
139 | static int wpan_phy_idx_valid(int idx) |
140 | { | |
141 | return idx >= 0; | |
142 | } | |
143 | ||
144 | struct wpan_phy *wpan_phy_alloc(size_t priv_size) | |
145 | { | |
146 | struct wpan_phy *phy = kzalloc(sizeof(*phy) + priv_size, | |
147 | GFP_KERNEL); | |
148 | ||
a6c0f821 DK |
149 | if (!phy) |
150 | goto out; | |
2bfb1070 DES |
151 | 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); | |
a6c0f821 | 157 | goto out; |
2bfb1070 DES |
158 | } |
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 | ||
37eb0edc DES |
168 | phy->current_channel = -1; /* not initialised */ |
169 | phy->current_page = 0; /* for compatibility */ | |
170 | ||
2bfb1070 | 171 | return phy; |
a6c0f821 DK |
172 | |
173 | out: | |
174 | return NULL; | |
2bfb1070 DES |
175 | } |
176 | EXPORT_SYMBOL(wpan_phy_alloc); | |
177 | ||
e9cf356c | 178 | int wpan_phy_register(struct wpan_phy *phy) |
2bfb1070 | 179 | { |
2bfb1070 DES |
180 | return device_add(&phy->dev); |
181 | } | |
182 | EXPORT_SYMBOL(wpan_phy_register); | |
183 | ||
184 | void wpan_phy_unregister(struct wpan_phy *phy) | |
185 | { | |
186 | device_del(&phy->dev); | |
187 | } | |
188 | EXPORT_SYMBOL(wpan_phy_unregister); | |
189 | ||
190 | void wpan_phy_free(struct wpan_phy *phy) | |
191 | { | |
192 | put_device(&phy->dev); | |
193 | } | |
194 | EXPORT_SYMBOL(wpan_phy_free); | |
195 | ||
196 | static int __init wpan_phy_class_init(void) | |
197 | { | |
cb6b3763 | 198 | int rc; |
4710d806 | 199 | |
cb6b3763 DES |
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; | |
209 | err_nl: | |
210 | class_unregister(&wpan_phy_class); | |
211 | err: | |
212 | return rc; | |
2bfb1070 | 213 | } |
282a3954 | 214 | subsys_initcall(wpan_phy_class_init); |
2bfb1070 DES |
215 | |
216 | static void __exit wpan_phy_class_exit(void) | |
217 | { | |
cb6b3763 | 218 | ieee802154_nl_exit(); |
2bfb1070 DES |
219 | class_unregister(&wpan_phy_class); |
220 | } | |
221 | module_exit(wpan_phy_class_exit); | |
222 | ||
2bfb1070 | 223 | MODULE_LICENSE("GPL v2"); |
cb6b3763 DES |
224 | MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface"); |
225 | MODULE_AUTHOR("Dmitry Eremin-Solenikov"); | |
2bfb1070 | 226 |