]>
Commit | Line | Data |
---|---|---|
183b9b59 IPG |
1 | /* |
2 | * Ultra Wide Band | |
3 | * Life cycle of devices | |
4 | * | |
5 | * Copyright (C) 2005-2006 Intel Corporation | |
6 | * Inaky Perez-Gonzalez <[email protected]> | |
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 | */ | |
183b9b59 | 25 | #include <linux/kernel.h> |
5a0e3ad6 | 26 | #include <linux/slab.h> |
183b9b59 | 27 | #include <linux/device.h> |
475c0a6b | 28 | #include <linux/export.h> |
183b9b59 IPG |
29 | #include <linux/err.h> |
30 | #include <linux/kdev_t.h> | |
31 | #include <linux/random.h> | |
0b992080 | 32 | #include <linux/stat.h> |
183b9b59 IPG |
33 | #include "uwb-internal.h" |
34 | ||
183b9b59 IPG |
35 | /* We initialize addresses to 0xff (invalid, as it is bcast) */ |
36 | static inline void uwb_dev_addr_init(struct uwb_dev_addr *addr) | |
37 | { | |
38 | memset(&addr->data, 0xff, sizeof(addr->data)); | |
39 | } | |
40 | ||
41 | static inline void uwb_mac_addr_init(struct uwb_mac_addr *addr) | |
42 | { | |
43 | memset(&addr->data, 0xff, sizeof(addr->data)); | |
44 | } | |
45 | ||
46 | /* @returns !0 if a device @addr is a broadcast address */ | |
47 | static inline int uwb_dev_addr_bcast(const struct uwb_dev_addr *addr) | |
48 | { | |
49 | static const struct uwb_dev_addr bcast = { .data = { 0xff, 0xff } }; | |
50 | return !uwb_dev_addr_cmp(addr, &bcast); | |
51 | } | |
52 | ||
53 | /* | |
54 | * Add callback @new to be called when an event occurs in @rc. | |
55 | */ | |
56 | int uwb_notifs_register(struct uwb_rc *rc, struct uwb_notifs_handler *new) | |
57 | { | |
58 | if (mutex_lock_interruptible(&rc->notifs_chain.mutex)) | |
59 | return -ERESTARTSYS; | |
60 | list_add(&new->list_node, &rc->notifs_chain.list); | |
61 | mutex_unlock(&rc->notifs_chain.mutex); | |
62 | return 0; | |
63 | } | |
64 | EXPORT_SYMBOL_GPL(uwb_notifs_register); | |
65 | ||
66 | /* | |
67 | * Remove event handler (callback) | |
68 | */ | |
69 | int uwb_notifs_deregister(struct uwb_rc *rc, struct uwb_notifs_handler *entry) | |
70 | { | |
71 | if (mutex_lock_interruptible(&rc->notifs_chain.mutex)) | |
72 | return -ERESTARTSYS; | |
73 | list_del(&entry->list_node); | |
74 | mutex_unlock(&rc->notifs_chain.mutex); | |
75 | return 0; | |
76 | } | |
77 | EXPORT_SYMBOL_GPL(uwb_notifs_deregister); | |
78 | ||
79 | /* | |
80 | * Notify all event handlers of a given event on @rc | |
81 | * | |
82 | * We are called with a valid reference to the device, or NULL if the | |
83 | * event is not for a particular event (e.g., a BG join event). | |
84 | */ | |
85 | void uwb_notify(struct uwb_rc *rc, struct uwb_dev *uwb_dev, enum uwb_notifs event) | |
86 | { | |
87 | struct uwb_notifs_handler *handler; | |
88 | if (mutex_lock_interruptible(&rc->notifs_chain.mutex)) | |
89 | return; | |
90 | if (!list_empty(&rc->notifs_chain.list)) { | |
91 | list_for_each_entry(handler, &rc->notifs_chain.list, list_node) { | |
92 | handler->cb(handler->data, uwb_dev, event); | |
93 | } | |
94 | } | |
95 | mutex_unlock(&rc->notifs_chain.mutex); | |
96 | } | |
97 | ||
98 | /* | |
99 | * Release the backing device of a uwb_dev that has been dynamically allocated. | |
100 | */ | |
101 | static void uwb_dev_sys_release(struct device *dev) | |
102 | { | |
103 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | |
104 | ||
183b9b59 | 105 | uwb_bce_put(uwb_dev->bce); |
183b9b59 IPG |
106 | memset(uwb_dev, 0x69, sizeof(*uwb_dev)); |
107 | kfree(uwb_dev); | |
183b9b59 IPG |
108 | } |
109 | ||
110 | /* | |
111 | * Initialize a UWB device instance | |
112 | * | |
113 | * Alloc, zero and call this function. | |
114 | */ | |
115 | void uwb_dev_init(struct uwb_dev *uwb_dev) | |
116 | { | |
117 | mutex_init(&uwb_dev->mutex); | |
118 | device_initialize(&uwb_dev->dev); | |
119 | uwb_dev->dev.release = uwb_dev_sys_release; | |
120 | uwb_dev_addr_init(&uwb_dev->dev_addr); | |
121 | uwb_mac_addr_init(&uwb_dev->mac_addr); | |
122 | bitmap_fill(uwb_dev->streams, UWB_NUM_GLOBAL_STREAMS); | |
123 | } | |
124 | ||
125 | static ssize_t uwb_dev_EUI_48_show(struct device *dev, | |
126 | struct device_attribute *attr, char *buf) | |
127 | { | |
128 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | |
129 | char addr[UWB_ADDR_STRSIZE]; | |
130 | ||
131 | uwb_mac_addr_print(addr, sizeof(addr), &uwb_dev->mac_addr); | |
132 | return sprintf(buf, "%s\n", addr); | |
133 | } | |
134 | static DEVICE_ATTR(EUI_48, S_IRUGO, uwb_dev_EUI_48_show, NULL); | |
135 | ||
136 | static ssize_t uwb_dev_DevAddr_show(struct device *dev, | |
137 | struct device_attribute *attr, char *buf) | |
138 | { | |
139 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | |
140 | char addr[UWB_ADDR_STRSIZE]; | |
141 | ||
142 | uwb_dev_addr_print(addr, sizeof(addr), &uwb_dev->dev_addr); | |
143 | return sprintf(buf, "%s\n", addr); | |
144 | } | |
145 | static DEVICE_ATTR(DevAddr, S_IRUGO, uwb_dev_DevAddr_show, NULL); | |
146 | ||
147 | /* | |
148 | * Show the BPST of this device. | |
149 | * | |
150 | * Calculated from the receive time of the device's beacon and it's | |
151 | * slot number. | |
152 | */ | |
153 | static ssize_t uwb_dev_BPST_show(struct device *dev, | |
154 | struct device_attribute *attr, char *buf) | |
155 | { | |
156 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | |
157 | struct uwb_beca_e *bce; | |
158 | struct uwb_beacon_frame *bf; | |
159 | u16 bpst; | |
160 | ||
161 | bce = uwb_dev->bce; | |
162 | mutex_lock(&bce->mutex); | |
163 | bf = (struct uwb_beacon_frame *)bce->be->BeaconInfo; | |
164 | bpst = bce->be->wBPSTOffset | |
165 | - (u16)(bf->Beacon_Slot_Number * UWB_BEACON_SLOT_LENGTH_US); | |
166 | mutex_unlock(&bce->mutex); | |
167 | ||
168 | return sprintf(buf, "%d\n", bpst); | |
169 | } | |
170 | static DEVICE_ATTR(BPST, S_IRUGO, uwb_dev_BPST_show, NULL); | |
171 | ||
172 | /* | |
173 | * Show the IEs a device is beaconing | |
174 | * | |
175 | * We need to access the beacon cache, so we just lock it really | |
176 | * quick, print the IEs and unlock. | |
177 | * | |
178 | * We have a reference on the cache entry, so that should be | |
179 | * quite safe. | |
180 | */ | |
181 | static ssize_t uwb_dev_IEs_show(struct device *dev, | |
182 | struct device_attribute *attr, char *buf) | |
183 | { | |
184 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | |
185 | ||
186 | return uwb_bce_print_IEs(uwb_dev, uwb_dev->bce, buf, PAGE_SIZE); | |
187 | } | |
188 | static DEVICE_ATTR(IEs, S_IRUGO | S_IWUSR, uwb_dev_IEs_show, NULL); | |
189 | ||
190 | static ssize_t uwb_dev_LQE_show(struct device *dev, | |
191 | struct device_attribute *attr, char *buf) | |
192 | { | |
193 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | |
194 | struct uwb_beca_e *bce = uwb_dev->bce; | |
195 | size_t result; | |
196 | ||
197 | mutex_lock(&bce->mutex); | |
198 | result = stats_show(&uwb_dev->bce->lqe_stats, buf); | |
199 | mutex_unlock(&bce->mutex); | |
200 | return result; | |
201 | } | |
202 | ||
203 | static ssize_t uwb_dev_LQE_store(struct device *dev, | |
204 | struct device_attribute *attr, | |
205 | const char *buf, size_t size) | |
206 | { | |
207 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | |
208 | struct uwb_beca_e *bce = uwb_dev->bce; | |
209 | ssize_t result; | |
210 | ||
211 | mutex_lock(&bce->mutex); | |
212 | result = stats_store(&uwb_dev->bce->lqe_stats, buf, size); | |
213 | mutex_unlock(&bce->mutex); | |
214 | return result; | |
215 | } | |
216 | static DEVICE_ATTR(LQE, S_IRUGO | S_IWUSR, uwb_dev_LQE_show, uwb_dev_LQE_store); | |
217 | ||
218 | static ssize_t uwb_dev_RSSI_show(struct device *dev, | |
219 | struct device_attribute *attr, char *buf) | |
220 | { | |
221 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | |
222 | struct uwb_beca_e *bce = uwb_dev->bce; | |
223 | size_t result; | |
224 | ||
225 | mutex_lock(&bce->mutex); | |
226 | result = stats_show(&uwb_dev->bce->rssi_stats, buf); | |
227 | mutex_unlock(&bce->mutex); | |
228 | return result; | |
229 | } | |
230 | ||
231 | static ssize_t uwb_dev_RSSI_store(struct device *dev, | |
232 | struct device_attribute *attr, | |
233 | const char *buf, size_t size) | |
234 | { | |
235 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | |
236 | struct uwb_beca_e *bce = uwb_dev->bce; | |
237 | ssize_t result; | |
238 | ||
239 | mutex_lock(&bce->mutex); | |
240 | result = stats_store(&uwb_dev->bce->rssi_stats, buf, size); | |
241 | mutex_unlock(&bce->mutex); | |
242 | return result; | |
243 | } | |
244 | static DEVICE_ATTR(RSSI, S_IRUGO | S_IWUSR, uwb_dev_RSSI_show, uwb_dev_RSSI_store); | |
245 | ||
246 | ||
2fc74ac2 | 247 | static struct attribute *uwb_dev_attrs[] = { |
183b9b59 IPG |
248 | &dev_attr_EUI_48.attr, |
249 | &dev_attr_DevAddr.attr, | |
250 | &dev_attr_BPST.attr, | |
251 | &dev_attr_IEs.attr, | |
252 | &dev_attr_LQE.attr, | |
253 | &dev_attr_RSSI.attr, | |
254 | NULL, | |
255 | }; | |
2fc74ac2 | 256 | ATTRIBUTE_GROUPS(uwb_dev); |
183b9b59 | 257 | |
84887934 TP |
258 | /* UWB bus type. */ |
259 | struct bus_type uwb_bus_type = { | |
260 | .name = "uwb", | |
261 | .dev_groups = uwb_dev_groups, | |
262 | }; | |
263 | ||
183b9b59 IPG |
264 | /** |
265 | * Device SYSFS registration | |
183b9b59 IPG |
266 | */ |
267 | static int __uwb_dev_sys_add(struct uwb_dev *uwb_dev, struct device *parent_dev) | |
268 | { | |
183b9b59 IPG |
269 | struct device *dev; |
270 | ||
183b9b59 | 271 | dev = &uwb_dev->dev; |
183b9b59 IPG |
272 | dev->parent = parent_dev; |
273 | dev_set_drvdata(dev, uwb_dev); | |
274 | ||
bce83697 | 275 | return device_add(dev); |
183b9b59 IPG |
276 | } |
277 | ||
278 | ||
279 | static void __uwb_dev_sys_rm(struct uwb_dev *uwb_dev) | |
280 | { | |
183b9b59 IPG |
281 | dev_set_drvdata(&uwb_dev->dev, NULL); |
282 | device_del(&uwb_dev->dev); | |
183b9b59 IPG |
283 | } |
284 | ||
285 | ||
286 | /** | |
287 | * Register and initialize a new UWB device | |
288 | * | |
289 | * Did you call uwb_dev_init() on it? | |
290 | * | |
291 | * @parent_rc: is the parent radio controller who has the link to the | |
292 | * device. When registering the UWB device that is a UWB | |
293 | * Radio Controller, we point back to it. | |
294 | * | |
295 | * If registering the device that is part of a radio, caller has set | |
296 | * rc->uwb_dev->dev. Otherwise it is to be left NULL--a new one will | |
297 | * be allocated. | |
298 | */ | |
299 | int uwb_dev_add(struct uwb_dev *uwb_dev, struct device *parent_dev, | |
300 | struct uwb_rc *parent_rc) | |
301 | { | |
302 | int result; | |
303 | struct device *dev; | |
304 | ||
305 | BUG_ON(uwb_dev == NULL); | |
306 | BUG_ON(parent_dev == NULL); | |
307 | BUG_ON(parent_rc == NULL); | |
308 | ||
309 | mutex_lock(&uwb_dev->mutex); | |
310 | dev = &uwb_dev->dev; | |
311 | uwb_dev->rc = parent_rc; | |
312 | result = __uwb_dev_sys_add(uwb_dev, parent_dev); | |
313 | if (result < 0) | |
314 | printk(KERN_ERR "UWB: unable to register dev %s with sysfs: %d\n", | |
315 | dev_name(dev), result); | |
316 | mutex_unlock(&uwb_dev->mutex); | |
317 | return result; | |
318 | } | |
319 | ||
320 | ||
321 | void uwb_dev_rm(struct uwb_dev *uwb_dev) | |
322 | { | |
323 | mutex_lock(&uwb_dev->mutex); | |
324 | __uwb_dev_sys_rm(uwb_dev); | |
325 | mutex_unlock(&uwb_dev->mutex); | |
326 | } | |
327 | ||
328 | ||
329 | static | |
330 | int __uwb_dev_try_get(struct device *dev, void *__target_uwb_dev) | |
331 | { | |
332 | struct uwb_dev *target_uwb_dev = __target_uwb_dev; | |
333 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | |
334 | if (uwb_dev == target_uwb_dev) { | |
335 | uwb_dev_get(uwb_dev); | |
336 | return 1; | |
337 | } else | |
338 | return 0; | |
339 | } | |
340 | ||
341 | ||
342 | /** | |
343 | * Given a UWB device descriptor, validate and refcount it | |
344 | * | |
345 | * @returns NULL if the device does not exist or is quiescing; the ptr to | |
346 | * it otherwise. | |
347 | */ | |
348 | struct uwb_dev *uwb_dev_try_get(struct uwb_rc *rc, struct uwb_dev *uwb_dev) | |
349 | { | |
350 | if (uwb_dev_for_each(rc, __uwb_dev_try_get, uwb_dev)) | |
351 | return uwb_dev; | |
352 | else | |
353 | return NULL; | |
354 | } | |
355 | EXPORT_SYMBOL_GPL(uwb_dev_try_get); | |
356 | ||
357 | ||
358 | /** | |
359 | * Remove a device from the system [grunt for other functions] | |
360 | */ | |
361 | int __uwb_dev_offair(struct uwb_dev *uwb_dev, struct uwb_rc *rc) | |
362 | { | |
363 | struct device *dev = &uwb_dev->dev; | |
364 | char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE]; | |
365 | ||
183b9b59 IPG |
366 | uwb_mac_addr_print(macbuf, sizeof(macbuf), &uwb_dev->mac_addr); |
367 | uwb_dev_addr_print(devbuf, sizeof(devbuf), &uwb_dev->dev_addr); | |
368 | dev_info(dev, "uwb device (mac %s dev %s) disconnected from %s %s\n", | |
369 | macbuf, devbuf, | |
e53582bb TP |
370 | uwb_dev->dev.bus->name, |
371 | rc ? dev_name(&(rc->uwb_dev.dev)) : ""); | |
183b9b59 | 372 | uwb_dev_rm(uwb_dev); |
3af37302 SP |
373 | list_del(&uwb_dev->bce->node); |
374 | uwb_bce_put(uwb_dev->bce); | |
183b9b59 | 375 | uwb_dev_put(uwb_dev); /* for the creation in _onair() */ |
bce83697 | 376 | |
183b9b59 IPG |
377 | return 0; |
378 | } | |
379 | ||
380 | ||
381 | /** | |
382 | * A device went off the air, clean up after it! | |
383 | * | |
384 | * This is called by the UWB Daemon (through the beacon purge function | |
385 | * uwb_bcn_cache_purge) when it is detected that a device has been in | |
386 | * radio silence for a while. | |
387 | * | |
388 | * If this device is actually a local radio controller we don't need | |
389 | * to go through the offair process, as it is not registered as that. | |
390 | * | |
391 | * NOTE: uwb_bcn_cache.mutex is held! | |
392 | */ | |
393 | void uwbd_dev_offair(struct uwb_beca_e *bce) | |
394 | { | |
395 | struct uwb_dev *uwb_dev; | |
396 | ||
397 | uwb_dev = bce->uwb_dev; | |
398 | if (uwb_dev) { | |
399 | uwb_notify(uwb_dev->rc, uwb_dev, UWB_NOTIF_OFFAIR); | |
400 | __uwb_dev_offair(uwb_dev, uwb_dev->rc); | |
401 | } | |
402 | } | |
403 | ||
404 | ||
405 | /** | |
406 | * A device went on the air, start it up! | |
407 | * | |
408 | * This is called by the UWB Daemon when it is detected that a device | |
409 | * has popped up in the radio range of the radio controller. | |
410 | * | |
411 | * It will just create the freaking device, register the beacon and | |
412 | * stuff and yatla, done. | |
413 | * | |
414 | * | |
415 | * NOTE: uwb_beca.mutex is held, bce->mutex is held | |
416 | */ | |
417 | void uwbd_dev_onair(struct uwb_rc *rc, struct uwb_beca_e *bce) | |
418 | { | |
419 | int result; | |
420 | struct device *dev = &rc->uwb_dev.dev; | |
421 | struct uwb_dev *uwb_dev; | |
422 | char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE]; | |
423 | ||
424 | uwb_mac_addr_print(macbuf, sizeof(macbuf), bce->mac_addr); | |
425 | uwb_dev_addr_print(devbuf, sizeof(devbuf), &bce->dev_addr); | |
92c4d9bd | 426 | uwb_dev = kzalloc(sizeof(struct uwb_dev), GFP_KERNEL); |
183b9b59 IPG |
427 | if (uwb_dev == NULL) { |
428 | dev_err(dev, "new device %s: Cannot allocate memory\n", | |
429 | macbuf); | |
430 | return; | |
431 | } | |
432 | uwb_dev_init(uwb_dev); /* This sets refcnt to one, we own it */ | |
84887934 | 433 | uwb_dev->dev.bus = &uwb_bus_type; |
183b9b59 IPG |
434 | uwb_dev->mac_addr = *bce->mac_addr; |
435 | uwb_dev->dev_addr = bce->dev_addr; | |
02aa2a37 | 436 | dev_set_name(&uwb_dev->dev, "%s", macbuf); |
675f0ab2 TP |
437 | |
438 | /* plug the beacon cache */ | |
439 | bce->uwb_dev = uwb_dev; | |
440 | uwb_dev->bce = bce; | |
441 | uwb_bce_get(bce); /* released in uwb_dev_sys_release() */ | |
442 | ||
183b9b59 IPG |
443 | result = uwb_dev_add(uwb_dev, &rc->uwb_dev.dev, rc); |
444 | if (result < 0) { | |
445 | dev_err(dev, "new device %s: cannot instantiate device\n", | |
446 | macbuf); | |
447 | goto error_dev_add; | |
448 | } | |
675f0ab2 | 449 | |
183b9b59 | 450 | dev_info(dev, "uwb device (mac %s dev %s) connected to %s %s\n", |
e53582bb TP |
451 | macbuf, devbuf, uwb_dev->dev.bus->name, |
452 | dev_name(&(rc->uwb_dev.dev))); | |
183b9b59 IPG |
453 | uwb_notify(rc, uwb_dev, UWB_NOTIF_ONAIR); |
454 | return; | |
455 | ||
456 | error_dev_add: | |
675f0ab2 TP |
457 | bce->uwb_dev = NULL; |
458 | uwb_bce_put(bce); | |
183b9b59 IPG |
459 | kfree(uwb_dev); |
460 | return; | |
461 | } | |
462 | ||
463 | /** | |
464 | * Iterate over the list of UWB devices, calling a @function on each | |
465 | * | |
466 | * See docs for bus_for_each().... | |
467 | * | |
468 | * @rc: radio controller for the devices. | |
469 | * @function: function to call. | |
470 | * @priv: data to pass to @function. | |
471 | * @returns: 0 if no invocation of function() returned a value | |
472 | * different to zero. That value otherwise. | |
473 | */ | |
474 | int uwb_dev_for_each(struct uwb_rc *rc, uwb_dev_for_each_f function, void *priv) | |
475 | { | |
476 | return device_for_each_child(&rc->uwb_dev.dev, priv, function); | |
477 | } | |
478 | EXPORT_SYMBOL_GPL(uwb_dev_for_each); |