2 #include <linux/kernel.h>
3 #include <linux/slab.h>
4 #include <linux/device.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
))
35 list_add(&new->list_node
, &rc
->notifs_chain
.list
);
36 mutex_unlock(&rc
->notifs_chain
.mutex
);
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
))
48 list_del(&entry
->list_node
);
49 mutex_unlock(&rc
->notifs_chain
.mutex
);
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
))
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
));
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
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
;
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
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
;
172 mutex_lock(&bce
->mutex
);
173 result
= stats_show(&uwb_dev
->bce
->lqe_stats
, buf
);
174 mutex_unlock(&bce
->mutex
);
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
;
186 mutex_lock(&bce
->mutex
);
187 result
= stats_store(&uwb_dev
->bce
->lqe_stats
, buf
, size
);
188 mutex_unlock(&bce
->mutex
);
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
;
200 mutex_lock(&bce
->mutex
);
201 result
= stats_show(&uwb_dev
->bce
->rssi_stats
, buf
);
202 mutex_unlock(&bce
->mutex
);
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
;
214 mutex_lock(&bce
->mutex
);
215 result
= stats_store(&uwb_dev
->bce
->rssi_stats
, buf
, size
);
216 mutex_unlock(&bce
->mutex
);
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
,
232 static struct attribute_group dev_attr_group
= {
236 static const struct attribute_group
*groups
[] = {
242 * Device SYSFS registration
246 static int __uwb_dev_sys_add(struct uwb_dev
*uwb_dev
, struct device
*parent_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
282 int uwb_dev_add(struct uwb_dev
*uwb_dev
, struct device
*parent_dev
,
283 struct uwb_rc
*parent_rc
)
288 BUG_ON(uwb_dev
== NULL
);
289 BUG_ON(parent_dev
== NULL
);
290 BUG_ON(parent_rc
== NULL
);
292 mutex_lock(&uwb_dev
->mutex
);
294 uwb_dev
->rc
= parent_rc
;
295 result
= __uwb_dev_sys_add(uwb_dev
, parent_dev
);
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
);
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
);
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
);
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
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
))
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",
353 rc
? rc
->uwb_dev
.dev
.parent
->bus
->name
: "n/a",
354 rc
? dev_name(rc
->uwb_dev
.dev
.parent
) : "");
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() */
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
;
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
)
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",
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
);
421 dev_err(dev
, "new device %s: cannot instantiate device\n",
425 /* plug the beacon cache */
426 bce
->uwb_dev
= uwb_dev
;
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
);
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
);