Full support for Ginger Console
[linux-ginger.git] / drivers / uwb / lc-dev.c
blob1097e81b56d01029f9ec8aac68fdb262c9874ed9
1 /*
2 * Ultra Wide Band
3 * Life cycle of devices
5 * Copyright (C) 2005-2006 Intel Corporation
6 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
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.
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.
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.
23 * FIXME: docs
25 #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"
32 /* We initialize addresses to 0xff (invalid, as it is bcast) */
33 static inline void uwb_dev_addr_init(struct uwb_dev_addr *addr)
35 memset(&addr->data, 0xff, sizeof(addr->data));
38 static inline void uwb_mac_addr_init(struct uwb_mac_addr *addr)
40 memset(&addr->data, 0xff, sizeof(addr->data));
43 /* @returns !0 if a device @addr is a broadcast address */
44 static inline int uwb_dev_addr_bcast(const struct uwb_dev_addr *addr)
46 static const struct uwb_dev_addr bcast = { .data = { 0xff, 0xff } };
47 return !uwb_dev_addr_cmp(addr, &bcast);
51 * Add callback @new to be called when an event occurs in @rc.
53 int uwb_notifs_register(struct uwb_rc *rc, struct uwb_notifs_handler *new)
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;
61 EXPORT_SYMBOL_GPL(uwb_notifs_register);
64 * Remove event handler (callback)
66 int uwb_notifs_deregister(struct uwb_rc *rc, struct uwb_notifs_handler *entry)
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;
74 EXPORT_SYMBOL_GPL(uwb_notifs_deregister);
77 * Notify all event handlers of a given event on @rc
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).
82 void uwb_notify(struct uwb_rc *rc, struct uwb_dev *uwb_dev, enum uwb_notifs event)
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);
92 mutex_unlock(&rc->notifs_chain.mutex);
96 * Release the backing device of a uwb_dev that has been dynamically allocated.
98 static void uwb_dev_sys_release(struct device *dev)
100 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
102 uwb_bce_put(uwb_dev->bce);
103 memset(uwb_dev, 0x69, sizeof(*uwb_dev));
104 kfree(uwb_dev);
108 * Initialize a UWB device instance
110 * Alloc, zero and call this function.
112 void uwb_dev_init(struct uwb_dev *uwb_dev)
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);
122 static ssize_t uwb_dev_EUI_48_show(struct device *dev,
123 struct device_attribute *attr, char *buf)
125 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
126 char addr[UWB_ADDR_STRSIZE];
128 uwb_mac_addr_print(addr, sizeof(addr), &uwb_dev->mac_addr);
129 return sprintf(buf, "%s\n", addr);
131 static DEVICE_ATTR(EUI_48, S_IRUGO, uwb_dev_EUI_48_show, NULL);
133 static ssize_t uwb_dev_DevAddr_show(struct device *dev,
134 struct device_attribute *attr, char *buf)
136 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
137 char addr[UWB_ADDR_STRSIZE];
139 uwb_dev_addr_print(addr, sizeof(addr), &uwb_dev->dev_addr);
140 return sprintf(buf, "%s\n", addr);
142 static DEVICE_ATTR(DevAddr, S_IRUGO, uwb_dev_DevAddr_show, NULL);
145 * Show the BPST of this device.
147 * Calculated from the receive time of the device's beacon and it's
148 * slot number.
150 static ssize_t uwb_dev_BPST_show(struct device *dev,
151 struct device_attribute *attr, char *buf)
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;
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);
165 return sprintf(buf, "%d\n", bpst);
167 static DEVICE_ATTR(BPST, S_IRUGO, uwb_dev_BPST_show, NULL);
170 * Show the IEs a device is beaconing
172 * We need to access the beacon cache, so we just lock it really
173 * quick, print the IEs and unlock.
175 * We have a reference on the cache entry, so that should be
176 * quite safe.
178 static ssize_t uwb_dev_IEs_show(struct device *dev,
179 struct device_attribute *attr, char *buf)
181 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
183 return uwb_bce_print_IEs(uwb_dev, uwb_dev->bce, buf, PAGE_SIZE);
185 static DEVICE_ATTR(IEs, S_IRUGO | S_IWUSR, uwb_dev_IEs_show, NULL);
187 static ssize_t uwb_dev_LQE_show(struct device *dev,
188 struct device_attribute *attr, char *buf)
190 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
191 struct uwb_beca_e *bce = uwb_dev->bce;
192 size_t result;
194 mutex_lock(&bce->mutex);
195 result = stats_show(&uwb_dev->bce->lqe_stats, buf);
196 mutex_unlock(&bce->mutex);
197 return result;
200 static ssize_t uwb_dev_LQE_store(struct device *dev,
201 struct device_attribute *attr,
202 const char *buf, size_t size)
204 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
205 struct uwb_beca_e *bce = uwb_dev->bce;
206 ssize_t result;
208 mutex_lock(&bce->mutex);
209 result = stats_store(&uwb_dev->bce->lqe_stats, buf, size);
210 mutex_unlock(&bce->mutex);
211 return result;
213 static DEVICE_ATTR(LQE, S_IRUGO | S_IWUSR, uwb_dev_LQE_show, uwb_dev_LQE_store);
215 static ssize_t uwb_dev_RSSI_show(struct device *dev,
216 struct device_attribute *attr, char *buf)
218 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
219 struct uwb_beca_e *bce = uwb_dev->bce;
220 size_t result;
222 mutex_lock(&bce->mutex);
223 result = stats_show(&uwb_dev->bce->rssi_stats, buf);
224 mutex_unlock(&bce->mutex);
225 return result;
228 static ssize_t uwb_dev_RSSI_store(struct device *dev,
229 struct device_attribute *attr,
230 const char *buf, size_t size)
232 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
233 struct uwb_beca_e *bce = uwb_dev->bce;
234 ssize_t result;
236 mutex_lock(&bce->mutex);
237 result = stats_store(&uwb_dev->bce->rssi_stats, buf, size);
238 mutex_unlock(&bce->mutex);
239 return result;
241 static DEVICE_ATTR(RSSI, S_IRUGO | S_IWUSR, uwb_dev_RSSI_show, uwb_dev_RSSI_store);
244 static 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,
254 static struct attribute_group dev_attr_group = {
255 .attrs = dev_attrs,
258 static const struct attribute_group *groups[] = {
259 &dev_attr_group,
260 NULL,
264 * Device SYSFS registration
268 static int __uwb_dev_sys_add(struct uwb_dev *uwb_dev, struct device *parent_dev)
270 struct device *dev;
272 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);
280 return device_add(dev);
284 static void __uwb_dev_sys_rm(struct uwb_dev *uwb_dev)
286 dev_set_drvdata(&uwb_dev->dev, NULL);
287 device_del(&uwb_dev->dev);
292 * Register and initialize a new UWB device
294 * Did you call uwb_dev_init() on it?
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.
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.
304 int uwb_dev_add(struct uwb_dev *uwb_dev, struct device *parent_dev,
305 struct uwb_rc *parent_rc)
307 int result;
308 struct device *dev;
310 BUG_ON(uwb_dev == NULL);
311 BUG_ON(parent_dev == NULL);
312 BUG_ON(parent_rc == NULL);
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;
326 void uwb_dev_rm(struct uwb_dev *uwb_dev)
328 mutex_lock(&uwb_dev->mutex);
329 __uwb_dev_sys_rm(uwb_dev);
330 mutex_unlock(&uwb_dev->mutex);
334 static
335 int __uwb_dev_try_get(struct device *dev, void *__target_uwb_dev)
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;
348 * Given a UWB device descriptor, validate and refcount it
350 * @returns NULL if the device does not exist or is quiescing; the ptr to
351 * it otherwise.
353 struct uwb_dev *uwb_dev_try_get(struct uwb_rc *rc, struct uwb_dev *uwb_dev)
355 if (uwb_dev_for_each(rc, __uwb_dev_try_get, uwb_dev))
356 return uwb_dev;
357 else
358 return NULL;
360 EXPORT_SYMBOL_GPL(uwb_dev_try_get);
364 * Remove a device from the system [grunt for other functions]
366 int __uwb_dev_offair(struct uwb_dev *uwb_dev, struct uwb_rc *rc)
368 struct device *dev = &uwb_dev->dev;
369 char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE];
371 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);
378 list_del(&uwb_dev->bce->node);
379 uwb_bce_put(uwb_dev->bce);
380 uwb_dev_put(uwb_dev); /* for the creation in _onair() */
382 return 0;
387 * A device went off the air, clean up after it!
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.
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.
396 * NOTE: uwb_bcn_cache.mutex is held!
398 void uwbd_dev_offair(struct uwb_beca_e *bce)
400 struct uwb_dev *uwb_dev;
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);
411 * A device went on the air, start it up!
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.
416 * It will just create the freaking device, register the beacon and
417 * stuff and yatla, done.
420 * NOTE: uwb_beca.mutex is held, bce->mutex is held
422 void uwbd_dev_onair(struct uwb_rc *rc, struct uwb_beca_e *bce)
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];
429 uwb_mac_addr_print(macbuf, sizeof(macbuf), bce->mac_addr);
430 uwb_dev_addr_print(devbuf, sizeof(devbuf), &bce->dev_addr);
431 uwb_dev = kzalloc(sizeof(struct uwb_dev), GFP_KERNEL);
432 if (uwb_dev == NULL) {
433 dev_err(dev, "new device %s: Cannot allocate memory\n",
434 macbuf);
435 return;
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;
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;
457 error_dev_add:
458 kfree(uwb_dev);
459 return;
463 * Iterate over the list of UWB devices, calling a @function on each
465 * See docs for bus_for_each()....
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.
473 int uwb_dev_for_each(struct uwb_rc *rc, uwb_dev_for_each_f function, void *priv)
475 return device_for_each_child(&rc->uwb_dev.dev, priv, function);
477 EXPORT_SYMBOL_GPL(uwb_dev_for_each);