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 <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); |