Root/
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 | * |
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/slab.h> |
20 | #include <linux/kernel.h> |
21 | #include <linux/module.h> |
22 | #include <linux/device.h> |
23 | |
24 | #include <net/wpan-phy.h> |
25 | |
26 | #include "ieee802154.h" |
27 | |
28 | #define MASTER_SHOW_COMPLEX(name, format_string, args...) \ |
29 | static 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); \ |
36 | ret = snprintf(buf, PAGE_SIZE, format_string "\n", args); \ |
37 | 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 | |
44 | MASTER_SHOW(current_channel, "%d"); |
45 | MASTER_SHOW(current_page, "%d"); |
46 | MASTER_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 ); |
49 | MASTER_SHOW(cca_mode, "%d"); |
50 | |
51 | static 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 | |
70 | static 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 | |
79 | static 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 | |
85 | static struct class wpan_phy_class = { |
86 | .name = "ieee802154", |
87 | .dev_release = wpan_phy_release, |
88 | .dev_attrs = pmib_attrs, |
89 | }; |
90 | |
91 | static DEFINE_MUTEX(wpan_phy_mutex); |
92 | static int wpan_phy_idx; |
93 | |
94 | static int wpan_phy_match(struct device *dev, void *data) |
95 | { |
96 | return !strcmp(dev_name(dev), (const char *)data); |
97 | } |
98 | |
99 | struct wpan_phy *wpan_phy_find(const char *str) |
100 | { |
101 | struct device *dev; |
102 | |
103 | if (WARN_ON(!str)) |
104 | return NULL; |
105 | |
106 | dev = class_find_device(&wpan_phy_class, NULL, |
107 | (void *)str, wpan_phy_match); |
108 | if (!dev) |
109 | return NULL; |
110 | |
111 | return container_of(dev, struct wpan_phy, dev); |
112 | } |
113 | EXPORT_SYMBOL(wpan_phy_find); |
114 | |
115 | struct wpan_phy_iter_data { |
116 | int (*fn)(struct wpan_phy *phy, void *data); |
117 | void *data; |
118 | }; |
119 | |
120 | static 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 | |
127 | int 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 | } |
138 | EXPORT_SYMBOL(wpan_phy_for_each); |
139 | |
140 | static int wpan_phy_idx_valid(int idx) |
141 | { |
142 | return idx >= 0; |
143 | } |
144 | |
145 | struct wpan_phy *wpan_phy_alloc(size_t priv_size) |
146 | { |
147 | struct wpan_phy *phy = kzalloc(sizeof(*phy) + priv_size, |
148 | GFP_KERNEL); |
149 | |
150 | mutex_lock(&wpan_phy_mutex); |
151 | phy->idx = wpan_phy_idx++; |
152 | if (unlikely(!wpan_phy_idx_valid(phy->idx))) { |
153 | wpan_phy_idx--; |
154 | mutex_unlock(&wpan_phy_mutex); |
155 | kfree(phy); |
156 | return NULL; |
157 | } |
158 | mutex_unlock(&wpan_phy_mutex); |
159 | |
160 | mutex_init(&phy->pib_lock); |
161 | |
162 | device_initialize(&phy->dev); |
163 | dev_set_name(&phy->dev, "wpan-phy%d", phy->idx); |
164 | |
165 | phy->dev.class = &wpan_phy_class; |
166 | |
167 | phy->current_channel = -1; /* not initialised */ |
168 | phy->current_page = 0; /* for compatibility */ |
169 | |
170 | return phy; |
171 | } |
172 | EXPORT_SYMBOL(wpan_phy_alloc); |
173 | |
174 | int wpan_phy_register(struct wpan_phy *phy) |
175 | { |
176 | return device_add(&phy->dev); |
177 | } |
178 | EXPORT_SYMBOL(wpan_phy_register); |
179 | |
180 | void wpan_phy_unregister(struct wpan_phy *phy) |
181 | { |
182 | device_del(&phy->dev); |
183 | } |
184 | EXPORT_SYMBOL(wpan_phy_unregister); |
185 | |
186 | void wpan_phy_free(struct wpan_phy *phy) |
187 | { |
188 | put_device(&phy->dev); |
189 | } |
190 | EXPORT_SYMBOL(wpan_phy_free); |
191 | |
192 | static int __init wpan_phy_class_init(void) |
193 | { |
194 | int rc; |
195 | rc = class_register(&wpan_phy_class); |
196 | if (rc) |
197 | goto err; |
198 | |
199 | rc = ieee802154_nl_init(); |
200 | if (rc) |
201 | goto err_nl; |
202 | |
203 | return 0; |
204 | err_nl: |
205 | class_unregister(&wpan_phy_class); |
206 | err: |
207 | return rc; |
208 | } |
209 | subsys_initcall(wpan_phy_class_init); |
210 | |
211 | static void __exit wpan_phy_class_exit(void) |
212 | { |
213 | ieee802154_nl_exit(); |
214 | class_unregister(&wpan_phy_class); |
215 | } |
216 | module_exit(wpan_phy_class_exit); |
217 | |
218 | MODULE_LICENSE("GPL v2"); |
219 | MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface"); |
220 | MODULE_AUTHOR("Dmitry Eremin-Solenikov"); |
221 | |
222 |
Branches:
ben-wpan
ben-wpan-stefan
javiroman/ks7010
jz-2.6.34
jz-2.6.34-rc5
jz-2.6.34-rc6
jz-2.6.34-rc7
jz-2.6.35
jz-2.6.36
jz-2.6.37
jz-2.6.38
jz-2.6.39
jz-3.0
jz-3.1
jz-3.11
jz-3.12
jz-3.13
jz-3.15
jz-3.16
jz-3.18-dt
jz-3.2
jz-3.3
jz-3.4
jz-3.5
jz-3.6
jz-3.6-rc2-pwm
jz-3.9
jz-3.9-clk
jz-3.9-rc8
jz47xx
jz47xx-2.6.38
master
Tags:
od-2011-09-04
od-2011-09-18
v2.6.34-rc5
v2.6.34-rc6
v2.6.34-rc7
v3.9