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