RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / uwb / lc-dev.c
blobe28f2d2144b7d2ce4179491321a55a2501de40d8
2 #include <linux/kernel.h>
3 #include <linux/slab.h>
4 #include <linux/device.h>
5 #include <linux/err.h>
6 #include <linux/kdev_t.h>
7 #include <linux/random.h>
8 #include "uwb-internal.h"
10 /* We initialize addresses to 0xff (invalid, as it is bcast) */
11 static inline void uwb_dev_addr_init(struct uwb_dev_addr *addr)
13 memset(&addr->data, 0xff, sizeof(addr->data));
16 static inline void uwb_mac_addr_init(struct uwb_mac_addr *addr)
18 memset(&addr->data, 0xff, sizeof(addr->data));
21 /* @returns !0 if a device @addr is a broadcast address */
22 static inline int uwb_dev_addr_bcast(const struct uwb_dev_addr *addr)
24 static const struct uwb_dev_addr bcast = { .data = { 0xff, 0xff } };
25 return !uwb_dev_addr_cmp(addr, &bcast);
29 * Add callback @new to be called when an event occurs in @rc.
31 int uwb_notifs_register(struct uwb_rc *rc, struct uwb_notifs_handler *new)
33 if (mutex_lock_interruptible(&rc->notifs_chain.mutex))
34 return -ERESTARTSYS;
35 list_add(&new->list_node, &rc->notifs_chain.list);
36 mutex_unlock(&rc->notifs_chain.mutex);
37 return 0;
39 EXPORT_SYMBOL_GPL(uwb_notifs_register);
42 * Remove event handler (callback)
44 int uwb_notifs_deregister(struct uwb_rc *rc, struct uwb_notifs_handler *entry)
46 if (mutex_lock_interruptible(&rc->notifs_chain.mutex))
47 return -ERESTARTSYS;
48 list_del(&entry->list_node);
49 mutex_unlock(&rc->notifs_chain.mutex);
50 return 0;
52 EXPORT_SYMBOL_GPL(uwb_notifs_deregister);
55 * Notify all event handlers of a given event on @rc
57 * We are called with a valid reference to the device, or NULL if the
58 * event is not for a particular event (e.g., a BG join event).
60 void uwb_notify(struct uwb_rc *rc, struct uwb_dev *uwb_dev, enum uwb_notifs event)
62 struct uwb_notifs_handler *handler;
63 if (mutex_lock_interruptible(&rc->notifs_chain.mutex))
64 return;
65 if (!list_empty(&rc->notifs_chain.list)) {
66 list_for_each_entry(handler, &rc->notifs_chain.list, list_node) {
67 handler->cb(handler->data, uwb_dev, event);
70 mutex_unlock(&rc->notifs_chain.mutex);
74 * Release the backing device of a uwb_dev that has been dynamically allocated.
76 static void uwb_dev_sys_release(struct device *dev)
78 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
80 uwb_bce_put(uwb_dev->bce);
81 memset(uwb_dev, 0x69, sizeof(*uwb_dev));
82 kfree(uwb_dev);
86 * Initialize a UWB device instance
88 * Alloc, zero and call this function.
90 void uwb_dev_init(struct uwb_dev *uwb_dev)
92 mutex_init(&uwb_dev->mutex);
93 device_initialize(&uwb_dev->dev);
94 uwb_dev->dev.release = uwb_dev_sys_release;
95 uwb_dev_addr_init(&uwb_dev->dev_addr);
96 uwb_mac_addr_init(&uwb_dev->mac_addr);
97 bitmap_fill(uwb_dev->streams, UWB_NUM_GLOBAL_STREAMS);
100 static ssize_t uwb_dev_EUI_48_show(struct device *dev,
101 struct device_attribute *attr, char *buf)
103 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
104 char addr[UWB_ADDR_STRSIZE];
106 uwb_mac_addr_print(addr, sizeof(addr), &uwb_dev->mac_addr);
107 return sprintf(buf, "%s\n", addr);
109 static DEVICE_ATTR(EUI_48, S_IRUGO, uwb_dev_EUI_48_show, NULL);
111 static ssize_t uwb_dev_DevAddr_show(struct device *dev,
112 struct device_attribute *attr, char *buf)
114 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
115 char addr[UWB_ADDR_STRSIZE];
117 uwb_dev_addr_print(addr, sizeof(addr), &uwb_dev->dev_addr);
118 return sprintf(buf, "%s\n", addr);
120 static DEVICE_ATTR(DevAddr, S_IRUGO, uwb_dev_DevAddr_show, NULL);
123 * Show the BPST of this device.
125 * Calculated from the receive time of the device's beacon and it's
126 * slot number.
128 static ssize_t uwb_dev_BPST_show(struct device *dev,
129 struct device_attribute *attr, char *buf)
131 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
132 struct uwb_beca_e *bce;
133 struct uwb_beacon_frame *bf;
134 u16 bpst;
136 bce = uwb_dev->bce;
137 mutex_lock(&bce->mutex);
138 bf = (struct uwb_beacon_frame *)bce->be->BeaconInfo;
139 bpst = bce->be->wBPSTOffset
140 - (u16)(bf->Beacon_Slot_Number * UWB_BEACON_SLOT_LENGTH_US);
141 mutex_unlock(&bce->mutex);
143 return sprintf(buf, "%d\n", bpst);
145 static DEVICE_ATTR(BPST, S_IRUGO, uwb_dev_BPST_show, NULL);
148 * Show the IEs a device is beaconing
150 * We need to access the beacon cache, so we just lock it really
151 * quick, print the IEs and unlock.
153 * We have a reference on the cache entry, so that should be
154 * quite safe.
156 static ssize_t uwb_dev_IEs_show(struct device *dev,
157 struct device_attribute *attr, char *buf)
159 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
161 return uwb_bce_print_IEs(uwb_dev, uwb_dev->bce, buf, PAGE_SIZE);
163 static DEVICE_ATTR(IEs, S_IRUGO | S_IWUSR, uwb_dev_IEs_show, NULL);
165 static ssize_t uwb_dev_LQE_show(struct device *dev,
166 struct device_attribute *attr, char *buf)
168 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
169 struct uwb_beca_e *bce = uwb_dev->bce;
170 size_t result;
172 mutex_lock(&bce->mutex);
173 result = stats_show(&uwb_dev->bce->lqe_stats, buf);
174 mutex_unlock(&bce->mutex);
175 return result;
178 static ssize_t uwb_dev_LQE_store(struct device *dev,
179 struct device_attribute *attr,
180 const char *buf, size_t size)
182 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
183 struct uwb_beca_e *bce = uwb_dev->bce;
184 ssize_t result;
186 mutex_lock(&bce->mutex);
187 result = stats_store(&uwb_dev->bce->lqe_stats, buf, size);
188 mutex_unlock(&bce->mutex);
189 return result;
191 static DEVICE_ATTR(LQE, S_IRUGO | S_IWUSR, uwb_dev_LQE_show, uwb_dev_LQE_store);
193 static ssize_t uwb_dev_RSSI_show(struct device *dev,
194 struct device_attribute *attr, char *buf)
196 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
197 struct uwb_beca_e *bce = uwb_dev->bce;
198 size_t result;
200 mutex_lock(&bce->mutex);
201 result = stats_show(&uwb_dev->bce->rssi_stats, buf);
202 mutex_unlock(&bce->mutex);
203 return result;
206 static ssize_t uwb_dev_RSSI_store(struct device *dev,
207 struct device_attribute *attr,
208 const char *buf, size_t size)
210 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
211 struct uwb_beca_e *bce = uwb_dev->bce;
212 ssize_t result;
214 mutex_lock(&bce->mutex);
215 result = stats_store(&uwb_dev->bce->rssi_stats, buf, size);
216 mutex_unlock(&bce->mutex);
217 return result;
219 static DEVICE_ATTR(RSSI, S_IRUGO | S_IWUSR, uwb_dev_RSSI_show, uwb_dev_RSSI_store);
222 static struct attribute *dev_attrs[] = {
223 &dev_attr_EUI_48.attr,
224 &dev_attr_DevAddr.attr,
225 &dev_attr_BPST.attr,
226 &dev_attr_IEs.attr,
227 &dev_attr_LQE.attr,
228 &dev_attr_RSSI.attr,
229 NULL,
232 static struct attribute_group dev_attr_group = {
233 .attrs = dev_attrs,
236 static const struct attribute_group *groups[] = {
237 &dev_attr_group,
238 NULL,
242 * Device SYSFS registration
246 static int __uwb_dev_sys_add(struct uwb_dev *uwb_dev, struct device *parent_dev)
248 struct device *dev;
250 dev = &uwb_dev->dev;
251 /* Device sysfs files are only useful for neighbor devices not
252 local radio controllers. */
253 if (&uwb_dev->rc->uwb_dev != uwb_dev)
254 dev->groups = groups;
255 dev->parent = parent_dev;
256 dev_set_drvdata(dev, uwb_dev);
258 return device_add(dev);
262 static void __uwb_dev_sys_rm(struct uwb_dev *uwb_dev)
264 dev_set_drvdata(&uwb_dev->dev, NULL);
265 device_del(&uwb_dev->dev);
270 * Register and initialize a new UWB device
272 * Did you call uwb_dev_init() on it?
274 * @parent_rc: is the parent radio controller who has the link to the
275 * device. When registering the UWB device that is a UWB
276 * Radio Controller, we point back to it.
278 * If registering the device that is part of a radio, caller has set
279 * rc->uwb_dev->dev. Otherwise it is to be left NULL--a new one will
280 * be allocated.
282 int uwb_dev_add(struct uwb_dev *uwb_dev, struct device *parent_dev,
283 struct uwb_rc *parent_rc)
285 int result;
286 struct device *dev;
288 BUG_ON(uwb_dev == NULL);
289 BUG_ON(parent_dev == NULL);
290 BUG_ON(parent_rc == NULL);
292 mutex_lock(&uwb_dev->mutex);
293 dev = &uwb_dev->dev;
294 uwb_dev->rc = parent_rc;
295 result = __uwb_dev_sys_add(uwb_dev, parent_dev);
296 if (result < 0)
297 printk(KERN_ERR "UWB: unable to register dev %s with sysfs: %d\n",
298 dev_name(dev), result);
299 mutex_unlock(&uwb_dev->mutex);
300 return result;
304 void uwb_dev_rm(struct uwb_dev *uwb_dev)
306 mutex_lock(&uwb_dev->mutex);
307 __uwb_dev_sys_rm(uwb_dev);
308 mutex_unlock(&uwb_dev->mutex);
312 static
313 int __uwb_dev_try_get(struct device *dev, void *__target_uwb_dev)
315 struct uwb_dev *target_uwb_dev = __target_uwb_dev;
316 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
317 if (uwb_dev == target_uwb_dev) {
318 uwb_dev_get(uwb_dev);
319 return 1;
320 } else
321 return 0;
326 * Given a UWB device descriptor, validate and refcount it
328 * @returns NULL if the device does not exist or is quiescing; the ptr to
329 * it otherwise.
331 struct uwb_dev *uwb_dev_try_get(struct uwb_rc *rc, struct uwb_dev *uwb_dev)
333 if (uwb_dev_for_each(rc, __uwb_dev_try_get, uwb_dev))
334 return uwb_dev;
335 else
336 return NULL;
338 EXPORT_SYMBOL_GPL(uwb_dev_try_get);
342 * Remove a device from the system [grunt for other functions]
344 int __uwb_dev_offair(struct uwb_dev *uwb_dev, struct uwb_rc *rc)
346 struct device *dev = &uwb_dev->dev;
347 char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE];
349 uwb_mac_addr_print(macbuf, sizeof(macbuf), &uwb_dev->mac_addr);
350 uwb_dev_addr_print(devbuf, sizeof(devbuf), &uwb_dev->dev_addr);
351 dev_info(dev, "uwb device (mac %s dev %s) disconnected from %s %s\n",
352 macbuf, devbuf,
353 rc ? rc->uwb_dev.dev.parent->bus->name : "n/a",
354 rc ? dev_name(rc->uwb_dev.dev.parent) : "");
355 uwb_dev_rm(uwb_dev);
356 list_del(&uwb_dev->bce->node);
357 uwb_bce_put(uwb_dev->bce);
358 uwb_dev_put(uwb_dev); /* for the creation in _onair() */
360 return 0;
365 * A device went off the air, clean up after it!
367 * This is called by the UWB Daemon (through the beacon purge function
368 * uwb_bcn_cache_purge) when it is detected that a device has been in
369 * radio silence for a while.
371 * If this device is actually a local radio controller we don't need
372 * to go through the offair process, as it is not registered as that.
374 * NOTE: uwb_bcn_cache.mutex is held!
376 void uwbd_dev_offair(struct uwb_beca_e *bce)
378 struct uwb_dev *uwb_dev;
380 uwb_dev = bce->uwb_dev;
381 if (uwb_dev) {
382 uwb_notify(uwb_dev->rc, uwb_dev, UWB_NOTIF_OFFAIR);
383 __uwb_dev_offair(uwb_dev, uwb_dev->rc);
389 * A device went on the air, start it up!
391 * This is called by the UWB Daemon when it is detected that a device
392 * has popped up in the radio range of the radio controller.
394 * It will just create the freaking device, register the beacon and
395 * stuff and yatla, done.
398 * NOTE: uwb_beca.mutex is held, bce->mutex is held
400 void uwbd_dev_onair(struct uwb_rc *rc, struct uwb_beca_e *bce)
402 int result;
403 struct device *dev = &rc->uwb_dev.dev;
404 struct uwb_dev *uwb_dev;
405 char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE];
407 uwb_mac_addr_print(macbuf, sizeof(macbuf), bce->mac_addr);
408 uwb_dev_addr_print(devbuf, sizeof(devbuf), &bce->dev_addr);
409 uwb_dev = kzalloc(sizeof(struct uwb_dev), GFP_KERNEL);
410 if (uwb_dev == NULL) {
411 dev_err(dev, "new device %s: Cannot allocate memory\n",
412 macbuf);
413 return;
415 uwb_dev_init(uwb_dev); /* This sets refcnt to one, we own it */
416 uwb_dev->mac_addr = *bce->mac_addr;
417 uwb_dev->dev_addr = bce->dev_addr;
418 dev_set_name(&uwb_dev->dev, macbuf);
419 result = uwb_dev_add(uwb_dev, &rc->uwb_dev.dev, rc);
420 if (result < 0) {
421 dev_err(dev, "new device %s: cannot instantiate device\n",
422 macbuf);
423 goto error_dev_add;
425 /* plug the beacon cache */
426 bce->uwb_dev = uwb_dev;
427 uwb_dev->bce = bce;
428 uwb_bce_get(bce); /* released in uwb_dev_sys_release() */
429 dev_info(dev, "uwb device (mac %s dev %s) connected to %s %s\n",
430 macbuf, devbuf, rc->uwb_dev.dev.parent->bus->name,
431 dev_name(rc->uwb_dev.dev.parent));
432 uwb_notify(rc, uwb_dev, UWB_NOTIF_ONAIR);
433 return;
435 error_dev_add:
436 kfree(uwb_dev);
437 return;
441 * Iterate over the list of UWB devices, calling a @function on each
443 * See docs for bus_for_each()....
445 * @rc: radio controller for the devices.
446 * @function: function to call.
447 * @priv: data to pass to @function.
448 * @returns: 0 if no invocation of function() returned a value
449 * different to zero. That value otherwise.
451 int uwb_dev_for_each(struct uwb_rc *rc, uwb_dev_for_each_f function, void *priv)
453 return device_for_each_child(&rc->uwb_dev.dev, priv, function);
455 EXPORT_SYMBOL_GPL(uwb_dev_for_each);