diff options
author | Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> | 2008-09-17 11:34:06 -0400 |
---|---|---|
committer | David Vrabel <dv02@dv02pc01.europe.root.pri> | 2008-09-17 11:54:23 -0400 |
commit | 183b9b592a622a7719ee38e275fd7ff3aaf74d0d (patch) | |
tree | 53bf5c09cd8e3ba85b4b8711ac69ff02141c8727 /drivers/uwb | |
parent | 34e95e41f1fd751e33a7eb3fa66594903b81f13d (diff) |
uwb: add the UWB stack (core files)
UWB device and radio controller device and event management.
Signed-off-by: David Vrabel <david.vrabel@csr.com>
Diffstat (limited to 'drivers/uwb')
-rw-r--r-- | drivers/uwb/driver.c | 142 | ||||
-rw-r--r-- | drivers/uwb/lc-dev.c | 492 | ||||
-rw-r--r-- | drivers/uwb/lc-rc.c | 501 | ||||
-rw-r--r-- | drivers/uwb/pal.c | 71 | ||||
-rw-r--r-- | drivers/uwb/uwb-internal.h | 306 | ||||
-rw-r--r-- | drivers/uwb/uwbd.c | 427 |
6 files changed, 1939 insertions, 0 deletions
diff --git a/drivers/uwb/driver.c b/drivers/uwb/driver.c new file mode 100644 index 00000000000..7eee8e40830 --- /dev/null +++ b/drivers/uwb/driver.c | |||
@@ -0,0 +1,142 @@ | |||
1 | /* | ||
2 | * Ultra Wide Band | ||
3 | * Driver initialization, etc | ||
4 | * | ||
5 | * Copyright (C) 2005-2006 Intel Corporation | ||
6 | * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or | ||
9 | * modify it under the terms of the GNU General Public License version | ||
10 | * 2 as published by the Free Software Foundation. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
20 | * 02110-1301, USA. | ||
21 | * | ||
22 | * | ||
23 | * FIXME: docs | ||
24 | * | ||
25 | * Life cycle: FIXME: explain | ||
26 | * | ||
27 | * UWB radio controller: | ||
28 | * | ||
29 | * 1. alloc a uwb_rc, zero it | ||
30 | * 2. call uwb_rc_init() on it to set it up + ops (won't do any | ||
31 | * kind of allocation) | ||
32 | * 3. register (now it is owned by the UWB stack--deregister before | ||
33 | * freeing/destroying). | ||
34 | * 4. It lives on it's own now (UWB stack handles)--when it | ||
35 | * disconnects, call unregister() | ||
36 | * 5. free it. | ||
37 | * | ||
38 | * Make sure you have a reference to the uwb_rc before calling | ||
39 | * any of the UWB API functions. | ||
40 | * | ||
41 | * TODO: | ||
42 | * | ||
43 | * 1. Locking and life cycle management is crappy still. All entry | ||
44 | * points to the UWB HCD API assume you have a reference on the | ||
45 | * uwb_rc structure and that it won't go away. They mutex lock it | ||
46 | * before doing anything. | ||
47 | */ | ||
48 | |||
49 | #include <linux/kernel.h> | ||
50 | #include <linux/init.h> | ||
51 | #include <linux/module.h> | ||
52 | #include <linux/device.h> | ||
53 | #include <linux/err.h> | ||
54 | #include <linux/kdev_t.h> | ||
55 | #include <linux/random.h> | ||
56 | #include <linux/uwb/debug.h> | ||
57 | #include "uwb-internal.h" | ||
58 | |||
59 | |||
60 | /* UWB stack attributes (or 'global' constants) */ | ||
61 | |||
62 | |||
63 | /** | ||
64 | * If a beacon dissapears for longer than this, then we consider the | ||
65 | * device who was represented by that beacon to be gone. | ||
66 | * | ||
67 | * ECMA-368[17.2.3, last para] establishes that a device must not | ||
68 | * consider a device to be its neighbour if he doesn't receive a beacon | ||
69 | * for more than mMaxLostBeacons. mMaxLostBeacons is defined in | ||
70 | * ECMA-368[17.16] as 3; because we can get only one beacon per | ||
71 | * superframe, that'd be 3 * 65ms = 195 ~ 200 ms. Let's give it time | ||
72 | * for jitter and stuff and make it 500 ms. | ||
73 | */ | ||
74 | unsigned long beacon_timeout_ms = 500; | ||
75 | |||
76 | static | ||
77 | ssize_t beacon_timeout_ms_show(struct class *class, char *buf) | ||
78 | { | ||
79 | return scnprintf(buf, PAGE_SIZE, "%lu\n", beacon_timeout_ms); | ||
80 | } | ||
81 | |||
82 | static | ||
83 | ssize_t beacon_timeout_ms_store(struct class *class, | ||
84 | const char *buf, size_t size) | ||
85 | { | ||
86 | unsigned long bt; | ||
87 | ssize_t result; | ||
88 | result = sscanf(buf, "%lu", &bt); | ||
89 | if (result != 1) | ||
90 | return -EINVAL; | ||
91 | beacon_timeout_ms = bt; | ||
92 | return size; | ||
93 | } | ||
94 | |||
95 | static struct class_attribute uwb_class_attrs[] = { | ||
96 | __ATTR(beacon_timeout_ms, S_IWUSR | S_IRUGO, | ||
97 | beacon_timeout_ms_show, beacon_timeout_ms_store), | ||
98 | __ATTR_NULL, | ||
99 | }; | ||
100 | |||
101 | /** Device model classes */ | ||
102 | struct class uwb_rc_class = { | ||
103 | .name = "uwb_rc", | ||
104 | .class_attrs = uwb_class_attrs, | ||
105 | }; | ||
106 | |||
107 | |||
108 | static int __init uwb_subsys_init(void) | ||
109 | { | ||
110 | int result = 0; | ||
111 | |||
112 | result = uwb_est_create(); | ||
113 | if (result < 0) { | ||
114 | printk(KERN_ERR "uwb: Can't initialize EST subsystem\n"); | ||
115 | goto error_est_init; | ||
116 | } | ||
117 | |||
118 | result = class_register(&uwb_rc_class); | ||
119 | if (result < 0) | ||
120 | goto error_uwb_rc_class_register; | ||
121 | uwbd_start(); | ||
122 | return 0; | ||
123 | |||
124 | error_uwb_rc_class_register: | ||
125 | uwb_est_destroy(); | ||
126 | error_est_init: | ||
127 | return result; | ||
128 | } | ||
129 | module_init(uwb_subsys_init); | ||
130 | |||
131 | static void __exit uwb_subsys_exit(void) | ||
132 | { | ||
133 | uwbd_stop(); | ||
134 | class_unregister(&uwb_rc_class); | ||
135 | uwb_est_destroy(); | ||
136 | return; | ||
137 | } | ||
138 | module_exit(uwb_subsys_exit); | ||
139 | |||
140 | MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>"); | ||
141 | MODULE_DESCRIPTION("Ultra Wide Band core"); | ||
142 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/uwb/lc-dev.c b/drivers/uwb/lc-dev.c new file mode 100644 index 00000000000..a6cb8ad731a --- /dev/null +++ b/drivers/uwb/lc-dev.c | |||
@@ -0,0 +1,492 @@ | |||
1 | /* | ||
2 | * Ultra Wide Band | ||
3 | * Life cycle of devices | ||
4 | * | ||
5 | * Copyright (C) 2005-2006 Intel Corporation | ||
6 | * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or | ||
9 | * modify it under the terms of the GNU General Public License version | ||
10 | * 2 as published by the Free Software Foundation. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
20 | * 02110-1301, USA. | ||
21 | * | ||
22 | * | ||
23 | * FIXME: docs | ||
24 | */ | ||
25 | |||
26 | #include <linux/kernel.h> | ||
27 | #include <linux/device.h> | ||
28 | #include <linux/err.h> | ||
29 | #include <linux/kdev_t.h> | ||
30 | #include <linux/random.h> | ||
31 | #include "uwb-internal.h" | ||
32 | |||
33 | #define D_LOCAL 1 | ||
34 | #include <linux/uwb/debug.h> | ||
35 | |||
36 | |||
37 | /* We initialize addresses to 0xff (invalid, as it is bcast) */ | ||
38 | static inline void uwb_dev_addr_init(struct uwb_dev_addr *addr) | ||
39 | { | ||
40 | memset(&addr->data, 0xff, sizeof(addr->data)); | ||
41 | } | ||
42 | |||
43 | static inline void uwb_mac_addr_init(struct uwb_mac_addr *addr) | ||
44 | { | ||
45 | memset(&addr->data, 0xff, sizeof(addr->data)); | ||
46 | } | ||
47 | |||
48 | /* @returns !0 if a device @addr is a broadcast address */ | ||
49 | static inline int uwb_dev_addr_bcast(const struct uwb_dev_addr *addr) | ||
50 | { | ||
51 | static const struct uwb_dev_addr bcast = { .data = { 0xff, 0xff } }; | ||
52 | return !uwb_dev_addr_cmp(addr, &bcast); | ||
53 | } | ||
54 | |||
55 | /* | ||
56 | * Add callback @new to be called when an event occurs in @rc. | ||
57 | */ | ||
58 | int uwb_notifs_register(struct uwb_rc *rc, struct uwb_notifs_handler *new) | ||
59 | { | ||
60 | if (mutex_lock_interruptible(&rc->notifs_chain.mutex)) | ||
61 | return -ERESTARTSYS; | ||
62 | list_add(&new->list_node, &rc->notifs_chain.list); | ||
63 | mutex_unlock(&rc->notifs_chain.mutex); | ||
64 | return 0; | ||
65 | } | ||
66 | EXPORT_SYMBOL_GPL(uwb_notifs_register); | ||
67 | |||
68 | /* | ||
69 | * Remove event handler (callback) | ||
70 | */ | ||
71 | int uwb_notifs_deregister(struct uwb_rc *rc, struct uwb_notifs_handler *entry) | ||
72 | { | ||
73 | if (mutex_lock_interruptible(&rc->notifs_chain.mutex)) | ||
74 | return -ERESTARTSYS; | ||
75 | list_del(&entry->list_node); | ||
76 | mutex_unlock(&rc->notifs_chain.mutex); | ||
77 | return 0; | ||
78 | } | ||
79 | EXPORT_SYMBOL_GPL(uwb_notifs_deregister); | ||
80 | |||
81 | /* | ||
82 | * Notify all event handlers of a given event on @rc | ||
83 | * | ||
84 | * We are called with a valid reference to the device, or NULL if the | ||
85 | * event is not for a particular event (e.g., a BG join event). | ||
86 | */ | ||
87 | void uwb_notify(struct uwb_rc *rc, struct uwb_dev *uwb_dev, enum uwb_notifs event) | ||
88 | { | ||
89 | struct uwb_notifs_handler *handler; | ||
90 | if (mutex_lock_interruptible(&rc->notifs_chain.mutex)) | ||
91 | return; | ||
92 | if (!list_empty(&rc->notifs_chain.list)) { | ||
93 | list_for_each_entry(handler, &rc->notifs_chain.list, list_node) { | ||
94 | handler->cb(handler->data, uwb_dev, event); | ||
95 | } | ||
96 | } | ||
97 | mutex_unlock(&rc->notifs_chain.mutex); | ||
98 | } | ||
99 | |||
100 | /* | ||
101 | * Release the backing device of a uwb_dev that has been dynamically allocated. | ||
102 | */ | ||
103 | static void uwb_dev_sys_release(struct device *dev) | ||
104 | { | ||
105 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | ||
106 | |||
107 | d_fnstart(4, NULL, "(dev %p uwb_dev %p)\n", dev, uwb_dev); | ||
108 | uwb_bce_put(uwb_dev->bce); | ||
109 | d_printf(0, &uwb_dev->dev, "uwb_dev %p freed\n", uwb_dev); | ||
110 | memset(uwb_dev, 0x69, sizeof(*uwb_dev)); | ||
111 | kfree(uwb_dev); | ||
112 | d_fnend(4, NULL, "(dev %p uwb_dev %p) = void\n", dev, uwb_dev); | ||
113 | } | ||
114 | |||
115 | /* | ||
116 | * Initialize a UWB device instance | ||
117 | * | ||
118 | * Alloc, zero and call this function. | ||
119 | */ | ||
120 | void uwb_dev_init(struct uwb_dev *uwb_dev) | ||
121 | { | ||
122 | mutex_init(&uwb_dev->mutex); | ||
123 | device_initialize(&uwb_dev->dev); | ||
124 | uwb_dev->dev.release = uwb_dev_sys_release; | ||
125 | uwb_dev_addr_init(&uwb_dev->dev_addr); | ||
126 | uwb_mac_addr_init(&uwb_dev->mac_addr); | ||
127 | bitmap_fill(uwb_dev->streams, UWB_NUM_GLOBAL_STREAMS); | ||
128 | } | ||
129 | |||
130 | static ssize_t uwb_dev_EUI_48_show(struct device *dev, | ||
131 | struct device_attribute *attr, char *buf) | ||
132 | { | ||
133 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | ||
134 | char addr[UWB_ADDR_STRSIZE]; | ||
135 | |||
136 | uwb_mac_addr_print(addr, sizeof(addr), &uwb_dev->mac_addr); | ||
137 | return sprintf(buf, "%s\n", addr); | ||
138 | } | ||
139 | static DEVICE_ATTR(EUI_48, S_IRUGO, uwb_dev_EUI_48_show, NULL); | ||
140 | |||
141 | static ssize_t uwb_dev_DevAddr_show(struct device *dev, | ||
142 | struct device_attribute *attr, char *buf) | ||
143 | { | ||
144 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | ||
145 | char addr[UWB_ADDR_STRSIZE]; | ||
146 | |||
147 | uwb_dev_addr_print(addr, sizeof(addr), &uwb_dev->dev_addr); | ||
148 | return sprintf(buf, "%s\n", addr); | ||
149 | } | ||
150 | static DEVICE_ATTR(DevAddr, S_IRUGO, uwb_dev_DevAddr_show, NULL); | ||
151 | |||
152 | /* | ||
153 | * Show the BPST of this device. | ||
154 | * | ||
155 | * Calculated from the receive time of the device's beacon and it's | ||
156 | * slot number. | ||
157 | */ | ||
158 | static ssize_t uwb_dev_BPST_show(struct device *dev, | ||
159 | struct device_attribute *attr, char *buf) | ||
160 | { | ||
161 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | ||
162 | struct uwb_beca_e *bce; | ||
163 | struct uwb_beacon_frame *bf; | ||
164 | u16 bpst; | ||
165 | |||
166 | bce = uwb_dev->bce; | ||
167 | mutex_lock(&bce->mutex); | ||
168 | bf = (struct uwb_beacon_frame *)bce->be->BeaconInfo; | ||
169 | bpst = bce->be->wBPSTOffset | ||
170 | - (u16)(bf->Beacon_Slot_Number * UWB_BEACON_SLOT_LENGTH_US); | ||
171 | mutex_unlock(&bce->mutex); | ||
172 | |||
173 | return sprintf(buf, "%d\n", bpst); | ||
174 | } | ||
175 | static DEVICE_ATTR(BPST, S_IRUGO, uwb_dev_BPST_show, NULL); | ||
176 | |||
177 | /* | ||
178 | * Show the IEs a device is beaconing | ||
179 | * | ||
180 | * We need to access the beacon cache, so we just lock it really | ||
181 | * quick, print the IEs and unlock. | ||
182 | * | ||
183 | * We have a reference on the cache entry, so that should be | ||
184 | * quite safe. | ||
185 | */ | ||
186 | static ssize_t uwb_dev_IEs_show(struct device *dev, | ||
187 | struct device_attribute *attr, char *buf) | ||
188 | { | ||
189 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | ||
190 | |||
191 | return uwb_bce_print_IEs(uwb_dev, uwb_dev->bce, buf, PAGE_SIZE); | ||
192 | } | ||
193 | static DEVICE_ATTR(IEs, S_IRUGO | S_IWUSR, uwb_dev_IEs_show, NULL); | ||
194 | |||
195 | static ssize_t uwb_dev_LQE_show(struct device *dev, | ||
196 | struct device_attribute *attr, char *buf) | ||
197 | { | ||
198 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | ||
199 | struct uwb_beca_e *bce = uwb_dev->bce; | ||
200 | size_t result; | ||
201 | |||
202 | mutex_lock(&bce->mutex); | ||
203 | result = stats_show(&uwb_dev->bce->lqe_stats, buf); | ||
204 | mutex_unlock(&bce->mutex); | ||
205 | return result; | ||
206 | } | ||
207 | |||
208 | static ssize_t uwb_dev_LQE_store(struct device *dev, | ||
209 | struct device_attribute *attr, | ||
210 | const char *buf, size_t size) | ||
211 | { | ||
212 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | ||
213 | struct uwb_beca_e *bce = uwb_dev->bce; | ||
214 | ssize_t result; | ||
215 | |||
216 | mutex_lock(&bce->mutex); | ||
217 | result = stats_store(&uwb_dev->bce->lqe_stats, buf, size); | ||
218 | mutex_unlock(&bce->mutex); | ||
219 | return result; | ||
220 | } | ||
221 | static DEVICE_ATTR(LQE, S_IRUGO | S_IWUSR, uwb_dev_LQE_show, uwb_dev_LQE_store); | ||
222 | |||
223 | static ssize_t uwb_dev_RSSI_show(struct device *dev, | ||
224 | struct device_attribute *attr, char *buf) | ||
225 | { | ||
226 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | ||
227 | struct uwb_beca_e *bce = uwb_dev->bce; | ||
228 | size_t result; | ||
229 | |||
230 | mutex_lock(&bce->mutex); | ||
231 | result = stats_show(&uwb_dev->bce->rssi_stats, buf); | ||
232 | mutex_unlock(&bce->mutex); | ||
233 | return result; | ||
234 | } | ||
235 | |||
236 | static ssize_t uwb_dev_RSSI_store(struct device *dev, | ||
237 | struct device_attribute *attr, | ||
238 | const char *buf, size_t size) | ||
239 | { | ||
240 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | ||
241 | struct uwb_beca_e *bce = uwb_dev->bce; | ||
242 | ssize_t result; | ||
243 | |||
244 | mutex_lock(&bce->mutex); | ||
245 | result = stats_store(&uwb_dev->bce->rssi_stats, buf, size); | ||
246 | mutex_unlock(&bce->mutex); | ||
247 | return result; | ||
248 | } | ||
249 | static DEVICE_ATTR(RSSI, S_IRUGO | S_IWUSR, uwb_dev_RSSI_show, uwb_dev_RSSI_store); | ||
250 | |||
251 | |||
252 | static struct attribute *dev_attrs[] = { | ||
253 | &dev_attr_EUI_48.attr, | ||
254 | &dev_attr_DevAddr.attr, | ||
255 | &dev_attr_BPST.attr, | ||
256 | &dev_attr_IEs.attr, | ||
257 | &dev_attr_LQE.attr, | ||
258 | &dev_attr_RSSI.attr, | ||
259 | NULL, | ||
260 | }; | ||
261 | |||
262 | static struct attribute_group dev_attr_group = { | ||
263 | .attrs = dev_attrs, | ||
264 | }; | ||
265 | |||
266 | static struct attribute_group *groups[] = { | ||
267 | &dev_attr_group, | ||
268 | NULL, | ||
269 | }; | ||
270 | |||
271 | /** | ||
272 | * Device SYSFS registration | ||
273 | * | ||
274 | * | ||
275 | */ | ||
276 | static int __uwb_dev_sys_add(struct uwb_dev *uwb_dev, struct device *parent_dev) | ||
277 | { | ||
278 | int result; | ||
279 | struct device *dev; | ||
280 | |||
281 | d_fnstart(4, NULL, "(uwb_dev %p parent_dev %p)\n", uwb_dev, parent_dev); | ||
282 | BUG_ON(parent_dev == NULL); | ||
283 | |||
284 | dev = &uwb_dev->dev; | ||
285 | /* Device sysfs files are only useful for neighbor devices not | ||
286 | local radio controllers. */ | ||
287 | if (&uwb_dev->rc->uwb_dev != uwb_dev) | ||
288 | dev->groups = groups; | ||
289 | dev->parent = parent_dev; | ||
290 | dev_set_drvdata(dev, uwb_dev); | ||
291 | |||
292 | result = device_add(dev); | ||
293 | d_fnend(4, NULL, "(uwb_dev %p parent_dev %p) = %d\n", uwb_dev, parent_dev, result); | ||
294 | return result; | ||
295 | } | ||
296 | |||
297 | |||
298 | static void __uwb_dev_sys_rm(struct uwb_dev *uwb_dev) | ||
299 | { | ||
300 | d_fnstart(4, NULL, "(uwb_dev %p)\n", uwb_dev); | ||
301 | dev_set_drvdata(&uwb_dev->dev, NULL); | ||
302 | device_del(&uwb_dev->dev); | ||
303 | d_fnend(4, NULL, "(uwb_dev %p) = void\n", uwb_dev); | ||
304 | } | ||
305 | |||
306 | |||
307 | /** | ||
308 | * Register and initialize a new UWB device | ||
309 | * | ||
310 | * Did you call uwb_dev_init() on it? | ||
311 | * | ||
312 | * @parent_rc: is the parent radio controller who has the link to the | ||
313 | * device. When registering the UWB device that is a UWB | ||
314 | * Radio Controller, we point back to it. | ||
315 | * | ||
316 | * If registering the device that is part of a radio, caller has set | ||
317 | * rc->uwb_dev->dev. Otherwise it is to be left NULL--a new one will | ||
318 | * be allocated. | ||
319 | */ | ||
320 | int uwb_dev_add(struct uwb_dev *uwb_dev, struct device *parent_dev, | ||
321 | struct uwb_rc *parent_rc) | ||
322 | { | ||
323 | int result; | ||
324 | struct device *dev; | ||
325 | |||
326 | BUG_ON(uwb_dev == NULL); | ||
327 | BUG_ON(parent_dev == NULL); | ||
328 | BUG_ON(parent_rc == NULL); | ||
329 | |||
330 | mutex_lock(&uwb_dev->mutex); | ||
331 | dev = &uwb_dev->dev; | ||
332 | uwb_dev->rc = parent_rc; | ||
333 | result = __uwb_dev_sys_add(uwb_dev, parent_dev); | ||
334 | if (result < 0) | ||
335 | printk(KERN_ERR "UWB: unable to register dev %s with sysfs: %d\n", | ||
336 | dev_name(dev), result); | ||
337 | mutex_unlock(&uwb_dev->mutex); | ||
338 | return result; | ||
339 | } | ||
340 | |||
341 | |||
342 | void uwb_dev_rm(struct uwb_dev *uwb_dev) | ||
343 | { | ||
344 | mutex_lock(&uwb_dev->mutex); | ||
345 | __uwb_dev_sys_rm(uwb_dev); | ||
346 | mutex_unlock(&uwb_dev->mutex); | ||
347 | } | ||
348 | |||
349 | |||
350 | static | ||
351 | int __uwb_dev_try_get(struct device *dev, void *__target_uwb_dev) | ||
352 | { | ||
353 | struct uwb_dev *target_uwb_dev = __target_uwb_dev; | ||
354 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | ||
355 | if (uwb_dev == target_uwb_dev) { | ||
356 | uwb_dev_get(uwb_dev); | ||
357 | return 1; | ||
358 | } else | ||
359 | return 0; | ||
360 | } | ||
361 | |||
362 | |||
363 | /** | ||
364 | * Given a UWB device descriptor, validate and refcount it | ||
365 | * | ||
366 | * @returns NULL if the device does not exist or is quiescing; the ptr to | ||
367 | * it otherwise. | ||
368 | */ | ||
369 | struct uwb_dev *uwb_dev_try_get(struct uwb_rc *rc, struct uwb_dev *uwb_dev) | ||
370 | { | ||
371 | if (uwb_dev_for_each(rc, __uwb_dev_try_get, uwb_dev)) | ||
372 | return uwb_dev; | ||
373 | else | ||
374 | return NULL; | ||
375 | } | ||
376 | EXPORT_SYMBOL_GPL(uwb_dev_try_get); | ||
377 | |||
378 | |||
379 | /** | ||
380 | * Remove a device from the system [grunt for other functions] | ||
381 | */ | ||
382 | int __uwb_dev_offair(struct uwb_dev *uwb_dev, struct uwb_rc *rc) | ||
383 | { | ||
384 | struct device *dev = &uwb_dev->dev; | ||
385 | char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE]; | ||
386 | |||
387 | d_fnstart(3, NULL, "(dev %p [uwb_dev %p], uwb_rc %p)\n", dev, uwb_dev, rc); | ||
388 | uwb_mac_addr_print(macbuf, sizeof(macbuf), &uwb_dev->mac_addr); | ||
389 | uwb_dev_addr_print(devbuf, sizeof(devbuf), &uwb_dev->dev_addr); | ||
390 | dev_info(dev, "uwb device (mac %s dev %s) disconnected from %s %s\n", | ||
391 | macbuf, devbuf, | ||
392 | rc ? rc->uwb_dev.dev.parent->bus->name : "n/a", | ||
393 | rc ? dev_name(rc->uwb_dev.dev.parent) : ""); | ||
394 | uwb_dev_rm(uwb_dev); | ||
395 | uwb_dev_put(uwb_dev); /* for the creation in _onair() */ | ||
396 | d_fnend(3, NULL, "(dev %p [uwb_dev %p], uwb_rc %p) = 0\n", dev, uwb_dev, rc); | ||
397 | return 0; | ||
398 | } | ||
399 | |||
400 | |||
401 | /** | ||
402 | * A device went off the air, clean up after it! | ||
403 | * | ||
404 | * This is called by the UWB Daemon (through the beacon purge function | ||
405 | * uwb_bcn_cache_purge) when it is detected that a device has been in | ||
406 | * radio silence for a while. | ||
407 | * | ||
408 | * If this device is actually a local radio controller we don't need | ||
409 | * to go through the offair process, as it is not registered as that. | ||
410 | * | ||
411 | * NOTE: uwb_bcn_cache.mutex is held! | ||
412 | */ | ||
413 | void uwbd_dev_offair(struct uwb_beca_e *bce) | ||
414 | { | ||
415 | struct uwb_dev *uwb_dev; | ||
416 | |||
417 | uwb_dev = bce->uwb_dev; | ||
418 | if (uwb_dev) { | ||
419 | uwb_notify(uwb_dev->rc, uwb_dev, UWB_NOTIF_OFFAIR); | ||
420 | __uwb_dev_offair(uwb_dev, uwb_dev->rc); | ||
421 | } | ||
422 | } | ||
423 | |||
424 | |||
425 | /** | ||
426 | * A device went on the air, start it up! | ||
427 | * | ||
428 | * This is called by the UWB Daemon when it is detected that a device | ||
429 | * has popped up in the radio range of the radio controller. | ||
430 | * | ||
431 | * It will just create the freaking device, register the beacon and | ||
432 | * stuff and yatla, done. | ||
433 | * | ||
434 | * | ||
435 | * NOTE: uwb_beca.mutex is held, bce->mutex is held | ||
436 | */ | ||
437 | void uwbd_dev_onair(struct uwb_rc *rc, struct uwb_beca_e *bce) | ||
438 | { | ||
439 | int result; | ||
440 | struct device *dev = &rc->uwb_dev.dev; | ||
441 | struct uwb_dev *uwb_dev; | ||
442 | char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE]; | ||
443 | |||
444 | uwb_mac_addr_print(macbuf, sizeof(macbuf), bce->mac_addr); | ||
445 | uwb_dev_addr_print(devbuf, sizeof(devbuf), &bce->dev_addr); | ||
446 | uwb_dev = kcalloc(1, sizeof(*uwb_dev), GFP_KERNEL); | ||
447 | if (uwb_dev == NULL) { | ||
448 | dev_err(dev, "new device %s: Cannot allocate memory\n", | ||
449 | macbuf); | ||
450 | return; | ||
451 | } | ||
452 | uwb_dev_init(uwb_dev); /* This sets refcnt to one, we own it */ | ||
453 | uwb_dev->mac_addr = *bce->mac_addr; | ||
454 | uwb_dev->dev_addr = bce->dev_addr; | ||
455 | dev_set_name(&uwb_dev->dev, macbuf); | ||
456 | result = uwb_dev_add(uwb_dev, &rc->uwb_dev.dev, rc); | ||
457 | if (result < 0) { | ||
458 | dev_err(dev, "new device %s: cannot instantiate device\n", | ||
459 | macbuf); | ||
460 | goto error_dev_add; | ||
461 | } | ||
462 | /* plug the beacon cache */ | ||
463 | bce->uwb_dev = uwb_dev; | ||
464 | uwb_dev->bce = bce; | ||
465 | uwb_bce_get(bce); /* released in uwb_dev_sys_release() */ | ||
466 | dev_info(dev, "uwb device (mac %s dev %s) connected to %s %s\n", | ||
467 | macbuf, devbuf, rc->uwb_dev.dev.parent->bus->name, | ||
468 | dev_name(rc->uwb_dev.dev.parent)); | ||
469 | uwb_notify(rc, uwb_dev, UWB_NOTIF_ONAIR); | ||
470 | return; | ||
471 | |||
472 | error_dev_add: | ||
473 | kfree(uwb_dev); | ||
474 | return; | ||
475 | } | ||
476 | |||
477 | /** | ||
478 | * Iterate over the list of UWB devices, calling a @function on each | ||
479 | * | ||
480 | * See docs for bus_for_each().... | ||
481 | * | ||
482 | * @rc: radio controller for the devices. | ||
483 | * @function: function to call. | ||
484 | * @priv: data to pass to @function. | ||
485 | * @returns: 0 if no invocation of function() returned a value | ||
486 | * different to zero. That value otherwise. | ||
487 | */ | ||
488 | int uwb_dev_for_each(struct uwb_rc *rc, uwb_dev_for_each_f function, void *priv) | ||
489 | { | ||
490 | return device_for_each_child(&rc->uwb_dev.dev, priv, function); | ||
491 | } | ||
492 | EXPORT_SYMBOL_GPL(uwb_dev_for_each); | ||
diff --git a/drivers/uwb/lc-rc.c b/drivers/uwb/lc-rc.c new file mode 100644 index 00000000000..a21c96bff23 --- /dev/null +++ b/drivers/uwb/lc-rc.c | |||
@@ -0,0 +1,501 @@ | |||
1 | /* | ||
2 | * Ultra Wide Band | ||
3 | * Life cycle of radio controllers | ||
4 | * | ||
5 | * Copyright (C) 2005-2006 Intel Corporation | ||
6 | * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or | ||
9 | * modify it under the terms of the GNU General Public License version | ||
10 | * 2 as published by the Free Software Foundation. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
20 | * 02110-1301, USA. | ||
21 | * | ||
22 | * | ||
23 | * FIXME: docs | ||
24 | * | ||
25 | * A UWB radio controller is also a UWB device, so it embeds one... | ||
26 | * | ||
27 | * List of RCs comes from the 'struct class uwb_rc_class'. | ||
28 | */ | ||
29 | |||
30 | #include <linux/kernel.h> | ||
31 | #include <linux/string.h> | ||
32 | #include <linux/device.h> | ||
33 | #include <linux/err.h> | ||
34 | #include <linux/random.h> | ||
35 | #include <linux/kdev_t.h> | ||
36 | #include <linux/etherdevice.h> | ||
37 | #include <linux/usb.h> | ||
38 | |||
39 | #define D_LOCAL 1 | ||
40 | #include <linux/uwb/debug.h> | ||
41 | #include "uwb-internal.h" | ||
42 | |||
43 | static int uwb_rc_index_match(struct device *dev, void *data) | ||
44 | { | ||
45 | int *index = data; | ||
46 | struct uwb_rc *rc = dev_get_drvdata(dev); | ||
47 | |||
48 | if (rc->index == *index) | ||
49 | return 1; | ||
50 | return 0; | ||
51 | } | ||
52 | |||
53 | static struct uwb_rc *uwb_rc_find_by_index(int index) | ||
54 | { | ||
55 | struct device *dev; | ||
56 | struct uwb_rc *rc = NULL; | ||
57 | |||
58 | dev = class_find_device(&uwb_rc_class, NULL, &index, uwb_rc_index_match); | ||
59 | if (dev) | ||
60 | rc = dev_get_drvdata(dev); | ||
61 | return rc; | ||
62 | } | ||
63 | |||
64 | static int uwb_rc_new_index(void) | ||
65 | { | ||
66 | int index = 0; | ||
67 | |||
68 | for (;;) { | ||
69 | if (!uwb_rc_find_by_index(index)) | ||
70 | return index; | ||
71 | if (++index < 0) | ||
72 | index = 0; | ||
73 | } | ||
74 | } | ||
75 | |||
76 | /** | ||
77 | * Release the backing device of a uwb_rc that has been dynamically allocated. | ||
78 | */ | ||
79 | static void uwb_rc_sys_release(struct device *dev) | ||
80 | { | ||
81 | struct uwb_dev *uwb_dev = container_of(dev, struct uwb_dev, dev); | ||
82 | struct uwb_rc *rc = container_of(uwb_dev, struct uwb_rc, uwb_dev); | ||
83 | |||
84 | uwb_rc_neh_destroy(rc); | ||
85 | uwb_rc_ie_release(rc); | ||
86 | d_printf(1, dev, "freed uwb_rc %p\n", rc); | ||
87 | kfree(rc); | ||
88 | } | ||
89 | |||
90 | |||
91 | void uwb_rc_init(struct uwb_rc *rc) | ||
92 | { | ||
93 | struct uwb_dev *uwb_dev = &rc->uwb_dev; | ||
94 | |||
95 | uwb_dev_init(uwb_dev); | ||
96 | rc->uwb_dev.dev.class = &uwb_rc_class; | ||
97 | rc->uwb_dev.dev.release = uwb_rc_sys_release; | ||
98 | uwb_rc_neh_create(rc); | ||
99 | rc->beaconing = -1; | ||
100 | rc->scan_type = UWB_SCAN_DISABLED; | ||
101 | INIT_LIST_HEAD(&rc->notifs_chain.list); | ||
102 | mutex_init(&rc->notifs_chain.mutex); | ||
103 | uwb_drp_avail_init(rc); | ||
104 | uwb_rc_ie_init(rc); | ||
105 | uwb_rsv_init(rc); | ||
106 | uwb_rc_pal_init(rc); | ||
107 | } | ||
108 | EXPORT_SYMBOL_GPL(uwb_rc_init); | ||
109 | |||
110 | |||
111 | struct uwb_rc *uwb_rc_alloc(void) | ||
112 | { | ||
113 | struct uwb_rc *rc; | ||
114 | rc = kzalloc(sizeof(*rc), GFP_KERNEL); | ||
115 | if (rc == NULL) | ||
116 | return NULL; | ||
117 | uwb_rc_init(rc); | ||
118 | return rc; | ||
119 | } | ||
120 | EXPORT_SYMBOL_GPL(uwb_rc_alloc); | ||
121 | |||
122 | static struct attribute *rc_attrs[] = { | ||
123 | &dev_attr_mac_address.attr, | ||
124 | &dev_attr_scan.attr, | ||
125 | &dev_attr_beacon.attr, | ||
126 | NULL, | ||
127 | }; | ||
128 | |||
129 | static struct attribute_group rc_attr_group = { | ||
130 | .attrs = rc_attrs, | ||
131 | }; | ||
132 | |||
133 | /* | ||
134 | * Registration of sysfs specific stuff | ||
135 | */ | ||
136 | static int uwb_rc_sys_add(struct uwb_rc *rc) | ||
137 | { | ||
138 | return sysfs_create_group(&rc->uwb_dev.dev.kobj, &rc_attr_group); | ||
139 | } | ||
140 | |||
141 | |||
142 | static void __uwb_rc_sys_rm(struct uwb_rc *rc) | ||
143 | { | ||
144 | sysfs_remove_group(&rc->uwb_dev.dev.kobj, &rc_attr_group); | ||
145 | } | ||
146 | |||
147 | /** | ||
148 | * uwb_rc_mac_addr_setup - get an RC's EUI-48 address or set it | ||
149 | * @rc: the radio controller. | ||
150 | * | ||
151 | * If the EUI-48 address is 00:00:00:00:00:00 or FF:FF:FF:FF:FF:FF | ||
152 | * then a random locally administered EUI-48 is generated and set on | ||
153 | * the device. The probability of address collisions is sufficiently | ||
154 | * unlikely (1/2^40 = 9.1e-13) that they're not checked for. | ||
155 | */ | ||
156 | static | ||
157 | int uwb_rc_mac_addr_setup(struct uwb_rc *rc) | ||
158 | { | ||
159 | int result; | ||
160 | struct device *dev = &rc->uwb_dev.dev; | ||
161 | struct uwb_dev *uwb_dev = &rc->uwb_dev; | ||
162 | char devname[UWB_ADDR_STRSIZE]; | ||
163 | struct uwb_mac_addr addr; | ||
164 | |||
165 | result = uwb_rc_mac_addr_get(rc, &addr); | ||
166 | if (result < 0) { | ||
167 | dev_err(dev, "cannot retrieve UWB EUI-48 address: %d\n", result); | ||
168 | return result; | ||
169 | } | ||
170 | |||
171 | if (uwb_mac_addr_unset(&addr) || uwb_mac_addr_bcast(&addr)) { | ||
172 | addr.data[0] = 0x02; /* locally adminstered and unicast */ | ||
173 | get_random_bytes(&addr.data[1], sizeof(addr.data)-1); | ||
174 | |||
175 | result = uwb_rc_mac_addr_set(rc, &addr); | ||
176 | if (result < 0) { | ||
177 | uwb_mac_addr_print(devname, sizeof(devname), &addr); | ||
178 | dev_err(dev, "cannot set EUI-48 address %s: %d\n", | ||
179 | devname, result); | ||
180 | return result; | ||
181 | } | ||
182 | } | ||
183 | uwb_dev->mac_addr = addr; | ||
184 | return 0; | ||
185 | } | ||
186 | |||
187 | |||
188 | |||
189 | static int uwb_rc_setup(struct uwb_rc *rc) | ||
190 | { | ||
191 | int result; | ||
192 | struct device *dev = &rc->uwb_dev.dev; | ||
193 | |||
194 | result = uwb_rc_reset(rc); | ||
195 | if (result < 0) { | ||
196 | dev_err(dev, "cannot reset UWB radio: %d\n", result); | ||
197 | goto error; | ||
198 | } | ||
199 | result = uwb_rc_mac_addr_setup(rc); | ||
200 | if (result < 0) { | ||
201 | dev_err(dev, "cannot setup UWB MAC address: %d\n", result); | ||
202 | goto error; | ||
203 | } | ||
204 | result = uwb_rc_dev_addr_assign(rc); | ||
205 | if (result < 0) { | ||
206 | dev_err(dev, "cannot assign UWB DevAddr: %d\n", result); | ||
207 | goto error; | ||
208 | } | ||
209 | result = uwb_rc_ie_setup(rc); | ||
210 | if (result < 0) { | ||
211 | dev_err(dev, "cannot setup IE subsystem: %d\n", result); | ||
212 | goto error_ie_setup; | ||
213 | } | ||
214 | result = uwb_rc_set_identification_ie(rc); | ||
215 | if (result < 0) { | ||
216 | dev_err(dev, "cannot set Identification IE: %d\n", | ||
217 | result); | ||
218 | goto error_set_id_ie; | ||
219 | } | ||
220 | result = uwb_rsv_setup(rc); | ||
221 | if (result < 0) { | ||
222 | dev_err(dev, "cannot setup reservation subsystem: %d\n", result); | ||
223 | goto error_rsv_setup; | ||
224 | } | ||
225 | uwb_dbg_add_rc(rc); | ||
226 | return 0; | ||
227 | |||
228 | error_rsv_setup: | ||
229 | uwb_rc_ie_release(rc); | ||
230 | error_ie_setup: | ||
231 | error: | ||
232 | return result; | ||
233 | } | ||
234 | |||
235 | |||
236 | /** | ||
237 | * Register a new UWB radio controller | ||
238 | * | ||
239 | * Did you call uwb_rc_init() on your rc? | ||
240 | * | ||
241 | * We assume that this is being called with a > 0 refcount on | ||
242 | * it [through ops->{get|put}_device(). We'll take our own, though. | ||
243 | * | ||
244 | * @parent_dev is our real device, the one that provides the actual UWB device | ||
245 | */ | ||
246 | int uwb_rc_add(struct uwb_rc *rc, struct device *parent_dev, void *priv) | ||
247 | { | ||
248 | int result; | ||
249 | struct device *dev; | ||
250 | char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE]; | ||
251 | |||
252 | rc->index = uwb_rc_new_index(); | ||
253 | |||
254 | dev = &rc->uwb_dev.dev; | ||
255 | dev_set_name(dev, "uwb%d", rc->index); | ||
256 | |||
257 | rc->priv = priv; | ||
258 | |||
259 | result = rc->start(rc); | ||
260 | if (result < 0) | ||
261 | goto error_rc_start; | ||
262 | |||
263 | result = uwb_rc_setup(rc); | ||
264 | if (result < 0) { | ||
265 | dev_err(dev, "cannot setup UWB radio controller: %d\n", result); | ||
266 | goto error_rc_setup; | ||
267 | } | ||
268 | |||
269 | result = uwb_dev_add(&rc->uwb_dev, parent_dev, rc); | ||
270 | if (result < 0 && result != -EADDRNOTAVAIL) | ||
271 | goto error_dev_add; | ||
272 | |||
273 | result = uwb_rc_sys_add(rc); | ||
274 | if (result < 0) { | ||
275 | dev_err(parent_dev, "cannot register UWB radio controller " | ||
276 | "dev attributes: %d\n", result); | ||
277 | goto error_sys_add; | ||
278 | } | ||
279 | |||
280 | uwb_mac_addr_print(macbuf, sizeof(macbuf), &rc->uwb_dev.mac_addr); | ||
281 | uwb_dev_addr_print(devbuf, sizeof(devbuf), &rc->uwb_dev.dev_addr); | ||
282 | dev_info(dev, | ||
283 | "new uwb radio controller (mac %s dev %s) on %s %s\n", | ||
284 | macbuf, devbuf, parent_dev->bus->name, dev_name(parent_dev)); | ||
285 | rc->ready = 1; | ||
286 | return 0; | ||
287 | |||
288 | error_sys_add: | ||
289 | uwb_dev_rm(&rc->uwb_dev); | ||
290 | error_dev_add: | ||
291 | error_rc_setup: | ||
292 | rc->stop(rc); | ||
293 | uwbd_flush(rc); | ||
294 | error_rc_start: | ||
295 | return result; | ||
296 | } | ||
297 | EXPORT_SYMBOL_GPL(uwb_rc_add); | ||
298 | |||
299 | |||
300 | static int uwb_dev_offair_helper(struct device *dev, void *priv) | ||
301 | { | ||
302 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | ||
303 | |||
304 | return __uwb_dev_offair(uwb_dev, uwb_dev->rc); | ||
305 | } | ||
306 | |||
307 | /* | ||
308 | * Remove a Radio Controller; stop beaconing/scanning, disconnect all children | ||
309 | */ | ||
310 | void uwb_rc_rm(struct uwb_rc *rc) | ||
311 | { | ||
312 | rc->ready = 0; | ||
313 | |||
314 | uwb_dbg_del_rc(rc); | ||
315 | uwb_rsv_cleanup(rc); | ||
316 | uwb_rc_ie_rm(rc, UWB_IDENTIFICATION_IE); | ||
317 | if (rc->beaconing >= 0) | ||
318 | uwb_rc_beacon(rc, -1, 0); | ||
319 | if (rc->scan_type != UWB_SCAN_DISABLED) | ||
320 | uwb_rc_scan(rc, rc->scanning, UWB_SCAN_DISABLED, 0); | ||
321 | uwb_rc_reset(rc); | ||
322 | |||
323 | rc->stop(rc); | ||
324 | uwbd_flush(rc); | ||
325 | |||
326 | uwb_dev_lock(&rc->uwb_dev); | ||
327 | rc->priv = NULL; | ||
328 | rc->cmd = NULL; | ||
329 | uwb_dev_unlock(&rc->uwb_dev); | ||
330 | mutex_lock(&uwb_beca.mutex); | ||
331 | uwb_dev_for_each(rc, uwb_dev_offair_helper, NULL); | ||
332 | __uwb_rc_sys_rm(rc); | ||
333 | mutex_unlock(&uwb_beca.mutex); | ||
334 | uwb_dev_rm(&rc->uwb_dev); | ||
335 | } | ||
336 | EXPORT_SYMBOL_GPL(uwb_rc_rm); | ||
337 | |||
338 | static int find_rc_try_get(struct device *dev, void *data) | ||
339 | { | ||
340 | struct uwb_rc *target_rc = data; | ||
341 | struct uwb_rc *rc = dev_get_drvdata(dev); | ||
342 | |||
343 | if (rc == NULL) { | ||
344 | WARN_ON(1); | ||
345 | return 0; | ||
346 | } | ||
347 | if (rc == target_rc) { | ||
348 | if (rc->ready == 0) | ||
349 | return 0; | ||
350 | else | ||
351 | return 1; | ||
352 | } | ||
353 | return 0; | ||
354 | } | ||
355 | |||
356 | /** | ||
357 | * Given a radio controller descriptor, validate and refcount it | ||
358 | * | ||
359 | * @returns NULL if the rc does not exist or is quiescing; the ptr to | ||
360 | * it otherwise. | ||
361 | */ | ||
362 | struct uwb_rc *__uwb_rc_try_get(struct uwb_rc *target_rc) | ||
363 | { | ||
364 | struct device *dev; | ||
365 | struct uwb_rc *rc = NULL; | ||
366 | |||
367 | dev = class_find_device(&uwb_rc_class, NULL, target_rc, | ||
368 | find_rc_try_get); | ||
369 | if (dev) { | ||
370 | rc = dev_get_drvdata(dev); | ||
371 | __uwb_rc_get(rc); | ||
372 | } | ||
373 | return rc; | ||
374 | } | ||
375 | EXPORT_SYMBOL_GPL(__uwb_rc_try_get); | ||
376 | |||
377 | /* | ||
378 | * RC get for external refcount acquirers... | ||
379 | * | ||
380 | * Increments the refcount of the device and it's backend modules | ||
381 | */ | ||
382 | static inline struct uwb_rc *uwb_rc_get(struct uwb_rc *rc) | ||
383 | { | ||
384 | if (rc->ready == 0) | ||
385 | return NULL; | ||
386 | uwb_dev_get(&rc->uwb_dev); | ||
387 | return rc; | ||
388 | } | ||
389 | |||
390 | static int find_rc_grandpa(struct device *dev, void *data) | ||
391 | { | ||
392 | struct device *grandpa_dev = data; | ||
393 | struct uwb_rc *rc = dev_get_drvdata(dev); | ||
394 | |||
395 | if (rc->uwb_dev.dev.parent->parent == grandpa_dev) { | ||
396 | rc = uwb_rc_get(rc); | ||
397 | return 1; | ||
398 | } | ||
399 | return 0; | ||
400 | } | ||
401 | |||
402 | /** | ||
403 | * Locate and refcount a radio controller given a common grand-parent | ||
404 | * | ||
405 | * @grandpa_dev Pointer to the 'grandparent' device structure. | ||
406 | * @returns NULL If the rc does not exist or is quiescing; the ptr to | ||
407 | * it otherwise, properly referenced. | ||
408 | * | ||
409 | * The Radio Control interface (or the UWB Radio Controller) is always | ||
410 | * an interface of a device. The parent is the interface, the | ||
411 | * grandparent is the device that encapsulates the interface. | ||
412 | * | ||
413 | * There is no need to lock around as the "grandpa" would be | ||
414 | * refcounted by the target, and to remove the referemes, the | ||
415 | * uwb_rc_class->sem would have to be taken--we hold it, ergo we | ||
416 | * should be safe. | ||
417 | */ | ||
418 | struct uwb_rc *uwb_rc_get_by_grandpa(const struct device *grandpa_dev) | ||
419 | { | ||
420 | struct device *dev; | ||
421 | struct uwb_rc *rc = NULL; | ||
422 | |||
423 | dev = class_find_device(&uwb_rc_class, NULL, (void *)grandpa_dev, | ||
424 | find_rc_grandpa); | ||
425 | if (dev) | ||
426 | rc = dev_get_drvdata(dev); | ||
427 | return rc; | ||
428 | } | ||
429 | EXPORT_SYMBOL_GPL(uwb_rc_get_by_grandpa); | ||
430 | |||
431 | /** | ||
432 | * Find a radio controller by device address | ||
433 | * | ||
434 | * @returns the pointer to the radio controller, properly referenced | ||
435 | */ | ||
436 | static int find_rc_dev(struct device *dev, void *data) | ||
437 | { | ||
438 | struct uwb_dev_addr *addr = data; | ||
439 | struct uwb_rc *rc = dev_get_drvdata(dev); | ||
440 | |||
441 | if (rc == NULL) { | ||
442 | WARN_ON(1); | ||
443 | return 0; | ||
444 | } | ||
445 | if (!uwb_dev_addr_cmp(&rc->uwb_dev.dev_addr, addr)) { | ||
446 | rc = uwb_rc_get(rc); | ||
447 | return 1; | ||
448 | } | ||
449 | return 0; | ||
450 | } | ||
451 | |||
452 | struct uwb_rc *uwb_rc_get_by_dev(const struct uwb_dev_addr *addr) | ||
453 | { | ||
454 | struct device *dev; | ||
455 | struct uwb_rc *rc = NULL; | ||
456 | |||
457 | dev = class_find_device(&uwb_rc_class, NULL, (void *)addr, | ||
458 | find_rc_dev); | ||
459 | if (dev) | ||
460 | rc = dev_get_drvdata(dev); | ||
461 | |||
462 | return rc; | ||
463 | } | ||
464 | EXPORT_SYMBOL_GPL(uwb_rc_get_by_dev); | ||
465 | |||
466 | /** | ||
467 | * Drop a reference on a radio controller | ||
468 | * | ||
469 | * This is the version that should be done by entities external to the | ||
470 | * UWB Radio Control stack (ie: clients of the API). | ||
471 | */ | ||
472 | void uwb_rc_put(struct uwb_rc *rc) | ||
473 | { | ||
474 | __uwb_rc_put(rc); | ||
475 | } | ||
476 | EXPORT_SYMBOL_GPL(uwb_rc_put); | ||
477 | |||
478 | /* | ||
479 | * | ||
480 | * | ||
481 | */ | ||
482 | ssize_t uwb_rc_print_IEs(struct uwb_rc *uwb_rc, char *buf, size_t size) | ||
483 | { | ||
484 | ssize_t result; | ||
485 | struct uwb_rc_evt_get_ie *ie_info; | ||
486 | struct uwb_buf_ctx ctx; | ||
487 | |||
488 | result = uwb_rc_get_ie(uwb_rc, &ie_info); | ||
489 | if (result < 0) | ||
490 | goto error_get_ie; | ||
491 | ctx.buf = buf; | ||
492 | ctx.size = size; | ||
493 | ctx.bytes = 0; | ||
494 | uwb_ie_for_each(&uwb_rc->uwb_dev, uwb_ie_dump_hex, &ctx, | ||
495 | ie_info->IEData, result - sizeof(*ie_info)); | ||
496 | result = ctx.bytes; | ||
497 | kfree(ie_info); | ||
498 | error_get_ie: | ||
499 | return result; | ||
500 | } | ||
501 | |||
diff --git a/drivers/uwb/pal.c b/drivers/uwb/pal.c new file mode 100644 index 00000000000..5508993a820 --- /dev/null +++ b/drivers/uwb/pal.c | |||
@@ -0,0 +1,71 @@ | |||
1 | /* | ||
2 | * UWB PAL support. | ||
3 | * | ||
4 | * Copyright (C) 2008 Cambridge Silicon Radio Ltd. | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or | ||
7 | * modify it under the terms of the GNU General Public License version | ||
8 | * 2 as published by the Free Software Foundation. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | #include <linux/kernel.h> | ||
19 | #include <linux/uwb.h> | ||
20 | |||
21 | #include "uwb-internal.h" | ||
22 | |||
23 | /** | ||
24 | * uwb_pal_init - initialize a UWB PAL | ||
25 | * @pal: the PAL to initialize | ||
26 | */ | ||
27 | void uwb_pal_init(struct uwb_pal *pal) | ||
28 | { | ||
29 | INIT_LIST_HEAD(&pal->node); | ||
30 | } | ||
31 | EXPORT_SYMBOL_GPL(uwb_pal_init); | ||
32 | |||
33 | /** | ||
34 | * uwb_pal_register - register a UWB PAL | ||
35 | * @rc: the radio controller the PAL will be using | ||
36 | * @pal: the PAL | ||
37 | * | ||
38 | * The PAL must be initialized with uwb_pal_init(). | ||
39 | */ | ||
40 | int uwb_pal_register(struct uwb_rc *rc, struct uwb_pal *pal) | ||
41 | { | ||
42 | spin_lock(&rc->pal_lock); | ||
43 | list_add(&pal->node, &rc->pals); | ||
44 | spin_unlock(&rc->pal_lock); | ||
45 | |||
46 | return 0; | ||
47 | } | ||
48 | EXPORT_SYMBOL_GPL(uwb_pal_register); | ||
49 | |||
50 | /** | ||
51 | * uwb_pal_register - unregister a UWB PAL | ||
52 | * @rc: the radio controller the PAL was using | ||
53 | * @pal: the PAL | ||
54 | */ | ||
55 | void uwb_pal_unregister(struct uwb_rc *rc, struct uwb_pal *pal) | ||
56 | { | ||
57 | spin_lock(&rc->pal_lock); | ||
58 | list_del(&pal->node); | ||
59 | spin_unlock(&rc->pal_lock); | ||
60 | } | ||
61 | EXPORT_SYMBOL_GPL(uwb_pal_unregister); | ||
62 | |||
63 | /** | ||
64 | * uwb_rc_pal_init - initialize the PAL related parts of a radio controller | ||
65 | * @rc: the radio controller | ||
66 | */ | ||
67 | void uwb_rc_pal_init(struct uwb_rc *rc) | ||
68 | { | ||
69 | spin_lock_init(&rc->pal_lock); | ||
70 | INIT_LIST_HEAD(&rc->pals); | ||
71 | } | ||
diff --git a/drivers/uwb/uwb-internal.h b/drivers/uwb/uwb-internal.h new file mode 100644 index 00000000000..4f525a88985 --- /dev/null +++ b/drivers/uwb/uwb-internal.h | |||
@@ -0,0 +1,306 @@ | |||
1 | /* | ||
2 | * Ultra Wide Band | ||
3 | * UWB internal API | ||
4 | * | ||
5 | * Copyright (C) 2005-2006 Intel Corporation | ||
6 | * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or | ||
9 | * modify it under the terms of the GNU General Public License version | ||
10 | * 2 as published by the Free Software Foundation. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
20 | * 02110-1301, USA. | ||
21 | * | ||
22 | * This contains most of the internal API for UWB. This is stuff used | ||
23 | * across the stack that of course, is of no interest to the rest. | ||
24 | * | ||
25 | * Some parts might end up going public (like uwb_rc_*())... | ||
26 | */ | ||
27 | |||
28 | #ifndef __UWB_INTERNAL_H__ | ||
29 | #define __UWB_INTERNAL_H__ | ||
30 | |||
31 | #include <linux/version.h> | ||
32 | #include <linux/kernel.h> | ||
33 | #include <linux/device.h> | ||
34 | #include <linux/uwb.h> | ||
35 | #include <linux/mutex.h> | ||
36 | |||
37 | struct uwb_beca_e; | ||
38 | |||
39 | /* General device API */ | ||
40 | extern void uwb_dev_init(struct uwb_dev *uwb_dev); | ||
41 | extern int __uwb_dev_offair(struct uwb_dev *, struct uwb_rc *); | ||
42 | extern int uwb_dev_add(struct uwb_dev *uwb_dev, struct device *parent_dev, | ||
43 | struct uwb_rc *parent_rc); | ||
44 | extern void uwb_dev_rm(struct uwb_dev *uwb_dev); | ||
45 | extern void uwbd_dev_onair(struct uwb_rc *, struct uwb_beca_e *); | ||
46 | extern void uwbd_dev_offair(struct uwb_beca_e *); | ||
47 | void uwb_notify(struct uwb_rc *rc, struct uwb_dev *uwb_dev, enum uwb_notifs event); | ||
48 | |||
49 | /* General UWB Radio Controller Internal API */ | ||
50 | extern struct uwb_rc *__uwb_rc_try_get(struct uwb_rc *); | ||
51 | static inline struct uwb_rc *__uwb_rc_get(struct uwb_rc *rc) | ||
52 | { | ||
53 | uwb_dev_get(&rc->uwb_dev); | ||
54 | return rc; | ||
55 | } | ||
56 | |||
57 | static inline void __uwb_rc_put(struct uwb_rc *rc) | ||
58 | { | ||
59 | uwb_dev_put(&rc->uwb_dev); | ||
60 | } | ||
61 | |||
62 | extern int uwb_rc_reset(struct uwb_rc *rc); | ||
63 | extern int uwb_rc_beacon(struct uwb_rc *rc, | ||
64 | int channel, unsigned bpst_offset); | ||
65 | extern int uwb_rc_scan(struct uwb_rc *rc, | ||
66 | unsigned channel, enum uwb_scan_type type, | ||
67 | unsigned bpst_offset); | ||
68 | extern int uwb_rc_send_all_drp_ie(struct uwb_rc *rc); | ||
69 | extern ssize_t uwb_rc_print_IEs(struct uwb_rc *rc, char *, size_t); | ||
70 | extern void uwb_rc_ie_init(struct uwb_rc *); | ||
71 | extern void uwb_rc_ie_init(struct uwb_rc *); | ||
72 | extern ssize_t uwb_rc_ie_setup(struct uwb_rc *); | ||
73 | extern void uwb_rc_ie_release(struct uwb_rc *); | ||
74 | extern int uwb_rc_ie_add(struct uwb_rc *, | ||
75 | const struct uwb_ie_hdr *, size_t); | ||
76 | extern int uwb_rc_ie_rm(struct uwb_rc *, enum uwb_ie); | ||
77 | extern int uwb_rc_set_identification_ie(struct uwb_rc *); | ||
78 | |||
79 | extern const char *uwb_rc_strerror(unsigned code); | ||
80 | |||
81 | /* | ||
82 | * Time to wait for a response to an RC command. | ||
83 | * | ||
84 | * Some commands can take a long time to response. e.g., START_BEACON | ||
85 | * may scan for several superframes before joining an existing beacon | ||
86 | * group and this can take around 600 ms. | ||
87 | */ | ||
88 | #define UWB_RC_CMD_TIMEOUT_MS 1000 /* ms */ | ||
89 | |||
90 | /* | ||
91 | * Notification/Event Handlers | ||
92 | */ | ||
93 | |||
94 | struct uwb_rc_neh; | ||
95 | |||
96 | void uwb_rc_neh_create(struct uwb_rc *rc); | ||
97 | void uwb_rc_neh_destroy(struct uwb_rc *rc); | ||
98 | |||
99 | struct uwb_rc_neh *uwb_rc_neh_add(struct uwb_rc *rc, struct uwb_rccb *cmd, | ||
100 | u8 expected_type, u16 expected_event, | ||
101 | uwb_rc_cmd_cb_f cb, void *arg); | ||
102 | void uwb_rc_neh_rm(struct uwb_rc *rc, struct uwb_rc_neh *neh); | ||
103 | void uwb_rc_neh_arm(struct uwb_rc *rc, struct uwb_rc_neh *neh); | ||
104 | void uwb_rc_neh_put(struct uwb_rc_neh *neh); | ||
105 | |||
106 | /* Event size tables */ | ||
107 | extern int uwb_est_create(void); | ||
108 | extern void uwb_est_destroy(void); | ||
109 | |||
110 | |||
111 | /* | ||
112 | * UWB Events & management daemon | ||
113 | */ | ||
114 | |||
115 | /** | ||
116 | * enum uwb_event_type - types of UWB management daemon events | ||
117 | * | ||
118 | * The UWB management daemon (uwbd) can receive two types of events: | ||
119 | * UWB_EVT_TYPE_NOTIF - notification from the radio controller. | ||
120 | * UWB_EVT_TYPE_MSG - a simple message. | ||
121 | */ | ||
122 | enum uwb_event_type { | ||
123 | UWB_EVT_TYPE_NOTIF, | ||
124 | UWB_EVT_TYPE_MSG, | ||
125 | }; | ||
126 | |||
127 | /** | ||
128 | * struct uwb_event_notif - an event for a radio controller notification | ||
129 | * @size: Size of the buffer (ie: Guaranteed to contain at least | ||
130 | * a full 'struct uwb_rceb') | ||
131 | * @rceb: Pointer to a kmalloced() event payload | ||
132 | */ | ||
133 | struct uwb_event_notif { | ||
134 | size_t size; | ||
135 | struct uwb_rceb *rceb; | ||
136 | }; | ||
137 | |||
138 | /** | ||
139 | * enum uwb_event_message - an event for a message for asynchronous processing | ||
140 | * | ||
141 | * UWB_EVT_MSG_RESET - reset the radio controller and all PAL hardware. | ||
142 | */ | ||
143 | enum uwb_event_message { | ||
144 | UWB_EVT_MSG_RESET, | ||
145 | }; | ||
146 | |||
147 | /** | ||
148 | * UWB Event | ||
149 | * @rc: Radio controller that emitted the event (referenced) | ||
150 | * @ts_jiffies: Timestamp, when was it received | ||
151 | * @type: This event's type. | ||
152 | */ | ||
153 | struct uwb_event { | ||
154 | struct list_head list_node; | ||
155 | struct uwb_rc *rc; | ||
156 | unsigned long ts_jiffies; | ||
157 | enum uwb_event_type type; | ||
158 | union { | ||
159 | struct uwb_event_notif notif; | ||
160 | enum uwb_event_message message; | ||
161 | }; | ||
162 | }; | ||
163 | |||
164 | extern void uwbd_start(void); | ||
165 | extern void uwbd_stop(void); | ||
166 | extern struct uwb_event *uwb_event_alloc(size_t, gfp_t gfp_mask); | ||
167 | extern void uwbd_event_queue(struct uwb_event *); | ||
168 | void uwbd_flush(struct uwb_rc *rc); | ||
169 | |||
170 | /* UWB event handlers */ | ||
171 | extern int uwbd_evt_handle_rc_beacon(struct uwb_event *); | ||
172 | extern int uwbd_evt_handle_rc_beacon_size(struct uwb_event *); | ||
173 | extern int uwbd_evt_handle_rc_bpoie_change(struct uwb_event *); | ||
174 | extern int uwbd_evt_handle_rc_bp_slot_change(struct uwb_event *); | ||
175 | extern int uwbd_evt_handle_rc_drp(struct uwb_event *); | ||
176 | extern int uwbd_evt_handle_rc_drp_avail(struct uwb_event *); | ||
177 | |||
178 | int uwbd_msg_handle_reset(struct uwb_event *evt); | ||
179 | |||
180 | |||
181 | /* | ||
182 | * Address management | ||
183 | */ | ||
184 | int uwb_rc_dev_addr_assign(struct uwb_rc *rc); | ||
185 | int uwbd_evt_handle_rc_dev_addr_conflict(struct uwb_event *evt); | ||
186 | |||
187 | /* | ||
188 | * UWB Beacon Cache | ||
189 | * | ||
190 | * Each beacon we received is kept in a cache--when we receive that | ||
191 | * beacon consistently, that means there is a new device that we have | ||
192 | * to add to the system. | ||
193 | */ | ||
194 | |||
195 | extern unsigned long beacon_timeout_ms; | ||
196 | |||
197 | /** Beacon cache list */ | ||
198 | struct uwb_beca { | ||
199 | struct list_head list; | ||
200 | size_t entries; | ||
201 | struct mutex mutex; | ||
202 | }; | ||
203 | |||
204 | extern struct uwb_beca uwb_beca; | ||
205 | |||
206 | /** | ||
207 | * Beacon cache entry | ||
208 | * | ||
209 | * @jiffies_refresh: last time a beacon was received that refreshed | ||
210 | * this cache entry. | ||
211 | * @uwb_dev: device connected to this beacon. This pointer is not | ||
212 | * safe, you need to get it with uwb_dev_try_get() | ||
213 | * | ||
214 | * @hits: how many time we have seen this beacon since last time we | ||
215 | * cleared it | ||
216 | */ | ||
217 | struct uwb_beca_e { | ||
218 | struct mutex mutex; | ||
219 | struct kref refcnt; | ||
220 | struct list_head node; | ||
221 | struct uwb_mac_addr *mac_addr; | ||
222 | struct uwb_dev_addr dev_addr; | ||
223 | u8 hits; | ||
224 | unsigned long ts_jiffies; | ||
225 | struct uwb_dev *uwb_dev; | ||
226 | struct uwb_rc_evt_beacon *be; | ||
227 | struct stats lqe_stats, rssi_stats; /* radio statistics */ | ||
228 | }; | ||
229 | struct uwb_beacon_frame; | ||
230 | extern ssize_t uwb_bce_print_IEs(struct uwb_dev *, struct uwb_beca_e *, | ||
231 | char *, size_t); | ||
232 | extern struct uwb_beca_e *__uwb_beca_add(struct uwb_rc_evt_beacon *, | ||
233 | struct uwb_beacon_frame *, | ||
234 | unsigned long); | ||
235 | |||
236 | extern void uwb_bce_kfree(struct kref *_bce); | ||
237 | static inline void uwb_bce_get(struct uwb_beca_e *bce) | ||
238 | { | ||
239 | kref_get(&bce->refcnt); | ||
240 | } | ||
241 | static inline void uwb_bce_put(struct uwb_beca_e *bce) | ||
242 | { | ||
243 | kref_put(&bce->refcnt, uwb_bce_kfree); | ||
244 | } | ||
245 | extern void uwb_beca_purge(void); | ||
246 | extern void uwb_beca_release(void); | ||
247 | |||
248 | struct uwb_dev *uwb_dev_get_by_devaddr(struct uwb_rc *rc, | ||
249 | const struct uwb_dev_addr *devaddr); | ||
250 | struct uwb_dev *uwb_dev_get_by_macaddr(struct uwb_rc *rc, | ||
251 | const struct uwb_mac_addr *macaddr); | ||
252 | |||
253 | /* -- UWB Sysfs representation */ | ||
254 | extern struct class uwb_rc_class; | ||
255 | extern struct device_attribute dev_attr_mac_address; | ||
256 | extern struct device_attribute dev_attr_beacon; | ||
257 | extern struct device_attribute dev_attr_scan; | ||
258 | |||
259 | /* -- DRP Bandwidth allocator: bandwidth allocations, reservations, DRP */ | ||
260 | void uwb_rsv_init(struct uwb_rc *rc); | ||
261 | int uwb_rsv_setup(struct uwb_rc *rc); | ||
262 | void uwb_rsv_cleanup(struct uwb_rc *rc); | ||
263 | |||
264 | void uwb_rsv_set_state(struct uwb_rsv *rsv, enum uwb_rsv_state new_state); | ||
265 | void uwb_rsv_remove(struct uwb_rsv *rsv); | ||
266 | struct uwb_rsv *uwb_rsv_find(struct uwb_rc *rc, struct uwb_dev *src, | ||
267 | struct uwb_ie_drp *drp_ie); | ||
268 | void uwb_rsv_sched_update(struct uwb_rc *rc); | ||
269 | |||
270 | void uwb_drp_handle_timeout(struct uwb_rsv *rsv); | ||
271 | int uwb_drp_ie_update(struct uwb_rsv *rsv); | ||
272 | void uwb_drp_ie_to_bm(struct uwb_mas_bm *bm, const struct uwb_ie_drp *drp_ie); | ||
273 | |||
274 | void uwb_drp_avail_init(struct uwb_rc *rc); | ||
275 | int uwb_drp_avail_reserve_pending(struct uwb_rc *rc, struct uwb_mas_bm *mas); | ||
276 | void uwb_drp_avail_reserve(struct uwb_rc *rc, struct uwb_mas_bm *mas); | ||
277 | void uwb_drp_avail_release(struct uwb_rc *rc, struct uwb_mas_bm *mas); | ||
278 | void uwb_drp_avail_ie_update(struct uwb_rc *rc); | ||
279 | |||
280 | /* -- PAL support */ | ||
281 | void uwb_rc_pal_init(struct uwb_rc *rc); | ||
282 | |||
283 | /* -- Misc */ | ||
284 | |||
285 | extern ssize_t uwb_mac_frame_hdr_print(char *, size_t, | ||
286 | const struct uwb_mac_frame_hdr *); | ||
287 | |||
288 | /* -- Debug interface */ | ||
289 | void uwb_dbg_init(void); | ||
290 | void uwb_dbg_exit(void); | ||
291 | void uwb_dbg_add_rc(struct uwb_rc *rc); | ||
292 | void uwb_dbg_del_rc(struct uwb_rc *rc); | ||
293 | |||
294 | /* Workarounds for version specific stuff */ | ||
295 | |||
296 | static inline void uwb_dev_lock(struct uwb_dev *uwb_dev) | ||
297 | { | ||
298 | down(&uwb_dev->dev.sem); | ||
299 | } | ||
300 | |||
301 | static inline void uwb_dev_unlock(struct uwb_dev *uwb_dev) | ||
302 | { | ||
303 | up(&uwb_dev->dev.sem); | ||
304 | } | ||
305 | |||
306 | #endif /* #ifndef __UWB_INTERNAL_H__ */ | ||
diff --git a/drivers/uwb/uwbd.c b/drivers/uwb/uwbd.c new file mode 100644 index 00000000000..b3673d614ad --- /dev/null +++ b/drivers/uwb/uwbd.c | |||
@@ -0,0 +1,427 @@ | |||
1 | /* | ||
2 | * Ultra Wide Band | ||
3 | * Neighborhood Management Daemon | ||
4 | * | ||
5 | * Copyright (C) 2005-2006 Intel Corporation | ||
6 | * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or | ||
9 | * modify it under the terms of the GNU General Public License version | ||
10 | * 2 as published by the Free Software Foundation. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
20 | * 02110-1301, USA. | ||
21 | * | ||
22 | * | ||
23 | * This daemon takes care of maintaing information that describes the | ||
24 | * UWB neighborhood that the radios in this machine can see. It also | ||
25 | * keeps a tab of which devices are visible, makes sure each HC sits | ||
26 | * on a different channel to avoid interfering, etc. | ||
27 | * | ||
28 | * Different drivers (radio controller, device, any API in general) | ||
29 | * communicate with this daemon through an event queue. Daemon wakes | ||
30 | * up, takes a list of events and handles them one by one; handling | ||
31 | * function is extracted from a table based on the event's type and | ||
32 | * subtype. Events are freed only if the handling function says so. | ||
33 | * | ||
34 | * . Lock protecting the event list has to be an spinlock and locked | ||
35 | * with IRQSAVE because it might be called from an interrupt | ||
36 | * context (ie: when events arrive and the notification drops | ||
37 | * down from the ISR). | ||
38 | * | ||
39 | * . UWB radio controller drivers queue events to the daemon using | ||
40 | * uwbd_event_queue(). They just get the event, chew it to make it | ||
41 | * look like UWBD likes it and pass it in a buffer allocated with | ||
42 | * uwb_event_alloc(). | ||
43 | * | ||
44 | * EVENTS | ||
45 | * | ||
46 | * Events have a type, a subtype, a lenght, some other stuff and the | ||
47 | * data blob, which depends on the event. The header is 'struct | ||
48 | * uwb_event'; for payloads, see 'struct uwbd_evt_*'. | ||
49 | * | ||
50 | * EVENT HANDLER TABLES | ||
51 | * | ||
52 | * To find a handling function for an event, the type is used to index | ||
53 | * a subtype-table in the type-table. The subtype-table is indexed | ||
54 | * with the subtype to get the function that handles the event. Start | ||
55 | * with the main type-table 'uwbd_evt_type_handler'. | ||
56 | * | ||
57 | * DEVICES | ||
58 | * | ||
59 | * Devices are created when a bunch of beacons have been received and | ||
60 | * it is stablished that the device has stable radio presence. CREATED | ||
61 | * only, not configured. Devices are ONLY configured when an | ||
62 | * Application-Specific IE Probe is receieved, in which the device | ||
63 | * declares which Protocol ID it groks. Then the device is CONFIGURED | ||
64 | * (and the driver->probe() stuff of the device model is invoked). | ||
65 | * | ||
66 | * Devices are considered disconnected when a certain number of | ||
67 | * beacons are not received in an amount of time. | ||
68 | * | ||
69 | * Handler functions are called normally uwbd_evt_handle_*(). | ||
70 | */ | ||
71 | |||
72 | #include <linux/kthread.h> | ||
73 | #include <linux/module.h> | ||
74 | #include <linux/freezer.h> | ||
75 | #include "uwb-internal.h" | ||
76 | |||
77 | #define D_LOCAL 1 | ||
78 | #include <linux/uwb/debug.h> | ||
79 | |||
80 | |||
81 | /** | ||
82 | * UWBD Event handler function signature | ||
83 | * | ||
84 | * Return !0 if the event needs not to be freed (ie the handler | ||
85 | * takes/took care of it). 0 means the daemon code will free the | ||
86 | * event. | ||
87 | * | ||
88 | * @evt->rc is already referenced and guaranteed to exist. See | ||
89 | * uwb_evt_handle(). | ||
90 | */ | ||
91 | typedef int (*uwbd_evt_handler_f)(struct uwb_event *); | ||
92 | |||
93 | /** | ||
94 | * Properties of a UWBD event | ||
95 | * | ||
96 | * @handler: the function that will handle this event | ||
97 | * @name: text name of event | ||
98 | */ | ||
99 | struct uwbd_event { | ||
100 | uwbd_evt_handler_f handler; | ||
101 | const char *name; | ||
102 | }; | ||
103 | |||
104 | /** Table of handlers for and properties of the UWBD Radio Control Events */ | ||
105 | static | ||
106 | struct uwbd_event uwbd_events[] = { | ||
107 | [UWB_RC_EVT_BEACON] = { | ||
108 | .handler = uwbd_evt_handle_rc_beacon, | ||
109 | .name = "BEACON_RECEIVED" | ||
110 | }, | ||
111 | [UWB_RC_EVT_BEACON_SIZE] = { | ||
112 | .handler = uwbd_evt_handle_rc_beacon_size, | ||
113 | .name = "BEACON_SIZE_CHANGE" | ||
114 | }, | ||
115 | [UWB_RC_EVT_BPOIE_CHANGE] = { | ||
116 | .handler = uwbd_evt_handle_rc_bpoie_change, | ||
117 | .name = "BPOIE_CHANGE" | ||
118 | }, | ||
119 | [UWB_RC_EVT_BP_SLOT_CHANGE] = { | ||
120 | .handler = uwbd_evt_handle_rc_bp_slot_change, | ||
121 | .name = "BP_SLOT_CHANGE" | ||
122 | }, | ||
123 | [UWB_RC_EVT_DRP_AVAIL] = { | ||
124 | .handler = uwbd_evt_handle_rc_drp_avail, | ||
125 | .name = "DRP_AVAILABILITY_CHANGE" | ||
126 | }, | ||
127 | [UWB_RC_EVT_DRP] = { | ||
128 | .handler = uwbd_evt_handle_rc_drp, | ||
129 | .name = "DRP" | ||
130 | }, | ||
131 | [UWB_RC_EVT_DEV_ADDR_CONFLICT] = { | ||
132 | .handler = uwbd_evt_handle_rc_dev_addr_conflict, | ||
133 | .name = "DEV_ADDR_CONFLICT", | ||
134 | }, | ||
135 | }; | ||
136 | |||
137 | |||
138 | |||
139 | struct uwbd_evt_type_handler { | ||
140 | const char *name; | ||
141 | struct uwbd_event *uwbd_events; | ||
142 | size_t size; | ||
143 | }; | ||
144 | |||
145 | #define UWBD_EVT_TYPE_HANDLER(n,a) { \ | ||
146 | .name = (n), \ | ||
147 | .uwbd_events = (a), \ | ||
148 | .size = sizeof(a)/sizeof((a)[0]) \ | ||
149 | } | ||
150 | |||
151 | |||
152 | /** Table of handlers for each UWBD Event type. */ | ||
153 | static | ||
154 | struct uwbd_evt_type_handler uwbd_evt_type_handlers[] = { | ||
155 | [UWB_RC_CET_GENERAL] = UWBD_EVT_TYPE_HANDLER("RC", uwbd_events) | ||
156 | }; | ||
157 | |||
158 | static const | ||
159 | size_t uwbd_evt_type_handlers_len = | ||
160 | sizeof(uwbd_evt_type_handlers) / sizeof(uwbd_evt_type_handlers[0]); | ||
161 | |||
162 | static const struct uwbd_event uwbd_message_handlers[] = { | ||
163 | [UWB_EVT_MSG_RESET] = { | ||
164 | .handler = uwbd_msg_handle_reset, | ||
165 | .name = "reset", | ||
166 | }, | ||
167 | }; | ||
168 | |||
169 | static DEFINE_MUTEX(uwbd_event_mutex); | ||
170 | |||
171 | /** | ||
172 | * Handle an URC event passed to the UWB Daemon | ||
173 | * | ||
174 | * @evt: the event to handle | ||
175 | * @returns: 0 if the event can be kfreed, !0 on the contrary | ||
176 | * (somebody else took ownership) [coincidentally, returning | ||
177 | * a <0 errno code will free it :)]. | ||
178 | * | ||
179 | * Looks up the two indirection tables (one for the type, one for the | ||
180 | * subtype) to decide which function handles it and then calls the | ||
181 | * handler. | ||
182 | * | ||
183 | * The event structure passed to the event handler has the radio | ||
184 | * controller in @evt->rc referenced. The reference will be dropped | ||
185 | * once the handler returns, so if it needs it for longer (async), | ||
186 | * it'll need to take another one. | ||
187 | */ | ||
188 | static | ||
189 | int uwbd_event_handle_urc(struct uwb_event *evt) | ||
190 | { | ||
191 | int result; | ||
192 | struct uwbd_evt_type_handler *type_table; | ||
193 | uwbd_evt_handler_f handler; | ||
194 | u8 type, context; | ||
195 | u16 event; | ||
196 | |||
197 | type = evt->notif.rceb->bEventType; | ||
198 | event = le16_to_cpu(evt->notif.rceb->wEvent); | ||
199 | context = evt->notif.rceb->bEventContext; | ||
200 | |||
201 | if (type > uwbd_evt_type_handlers_len) { | ||
202 | if (printk_ratelimit()) | ||
203 | printk(KERN_ERR "UWBD: event type %u: unknown " | ||
204 | "(too high)\n", type); | ||
205 | return -EINVAL; | ||
206 | } | ||
207 | type_table = &uwbd_evt_type_handlers[type]; | ||
208 | if (type_table->uwbd_events == NULL) { | ||
209 | if (printk_ratelimit()) | ||
210 | printk(KERN_ERR "UWBD: event type %u: unknown\n", type); | ||
211 | return -EINVAL; | ||
212 | } | ||
213 | if (event > type_table->size) { | ||
214 | if (printk_ratelimit()) | ||
215 | printk(KERN_ERR "UWBD: event %s[%u]: " | ||
216 | "unknown (too high)\n", type_table->name, event); | ||
217 | return -EINVAL; | ||
218 | } | ||
219 | handler = type_table->uwbd_events[event].handler; | ||
220 | if (handler == NULL) { | ||
221 | if (printk_ratelimit()) | ||
222 | printk(KERN_ERR "UWBD: event %s[%u]: unknown\n", | ||
223 | type_table->name, event); | ||
224 | return -EINVAL; | ||
225 | } | ||
226 | d_printf(3, NULL, "processing 0x%02x/%04x/%02x, %zu bytes\n", | ||
227 | type, event, context, evt->notif.size); | ||
228 | result = (*handler)(evt); | ||
229 | if (result < 0) { | ||
230 | if (printk_ratelimit()) | ||
231 | printk(KERN_ERR "UWBD: event 0x%02x/%04x/%02x, " | ||
232 | "table %s[%u]: handling failed: %d\n", | ||
233 | type, event, context, type_table->name, | ||
234 | event, result); | ||
235 | } | ||
236 | return result; | ||
237 | } | ||
238 | |||
239 | static void uwbd_event_handle_message(struct uwb_event *evt) | ||
240 | { | ||
241 | struct uwb_rc *rc; | ||
242 | int result; | ||
243 | |||
244 | rc = evt->rc; | ||
245 | |||
246 | if (evt->message < 0 || evt->message >= ARRAY_SIZE(uwbd_message_handlers)) { | ||
247 | dev_err(&rc->uwb_dev.dev, "UWBD: invalid message type %d\n", evt->message); | ||
248 | return; | ||
249 | } | ||
250 | |||
251 | /* If this is a reset event we need to drop the | ||
252 | * uwbd_event_mutex or it deadlocks when the reset handler | ||
253 | * attempts to flush the uwbd events. */ | ||
254 | if (evt->message == UWB_EVT_MSG_RESET) | ||
255 | mutex_unlock(&uwbd_event_mutex); | ||
256 | |||
257 | result = uwbd_message_handlers[evt->message].handler(evt); | ||
258 | if (result < 0) | ||
259 | dev_err(&rc->uwb_dev.dev, "UWBD: '%s' message failed: %d\n", | ||
260 | uwbd_message_handlers[evt->message].name, result); | ||
261 | |||
262 | if (evt->message == UWB_EVT_MSG_RESET) | ||
263 | mutex_lock(&uwbd_event_mutex); | ||
264 | } | ||
265 | |||
266 | static void uwbd_event_handle(struct uwb_event *evt) | ||
267 | { | ||
268 | struct uwb_rc *rc; | ||
269 | int should_keep; | ||
270 | |||
271 | rc = evt->rc; | ||
272 | |||
273 | if (rc->ready) { | ||
274 | switch (evt->type) { | ||
275 | case UWB_EVT_TYPE_NOTIF: | ||
276 | should_keep = uwbd_event_handle_urc(evt); | ||
277 | if (should_keep <= 0) | ||
278 | kfree(evt->notif.rceb); | ||
279 | break; | ||
280 | case UWB_EVT_TYPE_MSG: | ||
281 | uwbd_event_handle_message(evt); | ||
282 | break; | ||
283 | default: | ||
284 | dev_err(&rc->uwb_dev.dev, "UWBD: invalid event type %d\n", evt->type); | ||
285 | break; | ||
286 | } | ||
287 | } | ||
288 | |||
289 | __uwb_rc_put(rc); /* for the __uwb_rc_get() in uwb_rc_notif_cb() */ | ||
290 | } | ||
291 | /* The UWB Daemon */ | ||
292 | |||
293 | |||
294 | /** Daemon's PID: used to decide if we can queue or not */ | ||
295 | static int uwbd_pid; | ||
296 | /** Daemon's task struct for managing the kthread */ | ||
297 | static struct task_struct *uwbd_task; | ||
298 | /** Daemon's waitqueue for waiting for new events */ | ||
299 | static DECLARE_WAIT_QUEUE_HEAD(uwbd_wq); | ||
300 | /** Daemon's list of events; we queue/dequeue here */ | ||
301 | static struct list_head uwbd_event_list = LIST_HEAD_INIT(uwbd_event_list); | ||
302 | /** Daemon's list lock to protect concurent access */ | ||
303 | static DEFINE_SPINLOCK(uwbd_event_list_lock); | ||
304 | |||
305 | |||
306 | /** | ||
307 | * UWB Daemon | ||
308 | * | ||
309 | * Listens to all UWB notifications and takes care to track the state | ||
310 | * of the UWB neighboorhood for the kernel. When we do a run, we | ||
311 | * spinlock, move the list to a private copy and release the | ||
312 | * lock. Hold it as little as possible. Not a conflict: it is | ||
313 | * guaranteed we own the events in the private list. | ||
314 | * | ||
315 | * FIXME: should change so we don't have a 1HZ timer all the time, but | ||
316 | * only if there are devices. | ||
317 | */ | ||
318 | static int uwbd(void *unused) | ||
319 | { | ||
320 | unsigned long flags; | ||
321 | struct list_head list = LIST_HEAD_INIT(list); | ||
322 | struct uwb_event *evt, *nxt; | ||
323 | int should_stop = 0; | ||
324 | while (1) { | ||
325 | wait_event_interruptible_timeout( | ||
326 | uwbd_wq, | ||
327 | !list_empty(&uwbd_event_list) | ||
328 | || (should_stop = kthread_should_stop()), | ||
329 | HZ); | ||
330 | if (should_stop) | ||
331 | break; | ||
332 | try_to_freeze(); | ||
333 | |||
334 | mutex_lock(&uwbd_event_mutex); | ||
335 | spin_lock_irqsave(&uwbd_event_list_lock, flags); | ||
336 | list_splice_init(&uwbd_event_list, &list); | ||
337 | spin_unlock_irqrestore(&uwbd_event_list_lock, flags); | ||
338 | list_for_each_entry_safe(evt, nxt, &list, list_node) { | ||
339 | list_del(&evt->list_node); | ||
340 | uwbd_event_handle(evt); | ||
341 | kfree(evt); | ||
342 | } | ||
343 | mutex_unlock(&uwbd_event_mutex); | ||
344 | |||
345 | uwb_beca_purge(); /* Purge devices that left */ | ||
346 | } | ||
347 | return 0; | ||
348 | } | ||
349 | |||
350 | |||
351 | /** Start the UWB daemon */ | ||
352 | void uwbd_start(void) | ||
353 | { | ||
354 | uwbd_task = kthread_run(uwbd, NULL, "uwbd"); | ||
355 | if (uwbd_task == NULL) | ||
356 | printk(KERN_ERR "UWB: Cannot start management daemon; " | ||
357 | "UWB won't work\n"); | ||
358 | else | ||
359 | uwbd_pid = uwbd_task->pid; | ||
360 | } | ||
361 | |||
362 | /* Stop the UWB daemon and free any unprocessed events */ | ||
363 | void uwbd_stop(void) | ||
364 | { | ||
365 | unsigned long flags; | ||
366 | struct uwb_event *evt, *nxt; | ||
367 | kthread_stop(uwbd_task); | ||
368 | spin_lock_irqsave(&uwbd_event_list_lock, flags); | ||
369 | uwbd_pid = 0; | ||
370 | list_for_each_entry_safe(evt, nxt, &uwbd_event_list, list_node) { | ||
371 | if (evt->type == UWB_EVT_TYPE_NOTIF) | ||
372 | kfree(evt->notif.rceb); | ||
373 | kfree(evt); | ||
374 | } | ||
375 | spin_unlock_irqrestore(&uwbd_event_list_lock, flags); | ||
376 | uwb_beca_release(); | ||
377 | } | ||
378 | |||
379 | /* | ||
380 | * Queue an event for the management daemon | ||
381 | * | ||
382 | * When some lower layer receives an event, it uses this function to | ||
383 | * push it forward to the UWB daemon. | ||
384 | * | ||
385 | * Once you pass the event, you don't own it any more, but the daemon | ||
386 | * does. It will uwb_event_free() it when done, so make sure you | ||
387 | * uwb_event_alloc()ed it or bad things will happen. | ||
388 | * | ||
389 | * If the daemon is not running, we just free the event. | ||
390 | */ | ||
391 | void uwbd_event_queue(struct uwb_event *evt) | ||
392 | { | ||
393 | unsigned long flags; | ||
394 | spin_lock_irqsave(&uwbd_event_list_lock, flags); | ||
395 | if (uwbd_pid != 0) { | ||
396 | list_add(&evt->list_node, &uwbd_event_list); | ||
397 | wake_up_all(&uwbd_wq); | ||
398 | } else { | ||
399 | __uwb_rc_put(evt->rc); | ||
400 | if (evt->type == UWB_EVT_TYPE_NOTIF) | ||
401 | kfree(evt->notif.rceb); | ||
402 | kfree(evt); | ||
403 | } | ||
404 | spin_unlock_irqrestore(&uwbd_event_list_lock, flags); | ||
405 | return; | ||
406 | } | ||
407 | |||
408 | void uwbd_flush(struct uwb_rc *rc) | ||
409 | { | ||
410 | struct uwb_event *evt, *nxt; | ||
411 | |||
412 | mutex_lock(&uwbd_event_mutex); | ||
413 | |||
414 | spin_lock_irq(&uwbd_event_list_lock); | ||
415 | list_for_each_entry_safe(evt, nxt, &uwbd_event_list, list_node) { | ||
416 | if (evt->rc == rc) { | ||
417 | __uwb_rc_put(rc); | ||
418 | list_del(&evt->list_node); | ||
419 | if (evt->type == UWB_EVT_TYPE_NOTIF) | ||
420 | kfree(evt->notif.rceb); | ||
421 | kfree(evt); | ||
422 | } | ||
423 | } | ||
424 | spin_unlock_irq(&uwbd_event_list_lock); | ||
425 | |||
426 | mutex_unlock(&uwbd_event_mutex); | ||
427 | } | ||