1 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" 9 10/* We initialize addresses to 0xff (invalid, as it is bcast) */ 11static inline void uwb_dev_addr_init(struct uwb_dev_addr *addr) 12{ 13 memset(&addr->data, 0xff, sizeof(addr->data)); 14} 15 16static inline void uwb_mac_addr_init(struct uwb_mac_addr *addr) 17{ 18 memset(&addr->data, 0xff, sizeof(addr->data)); 19} 20 21/* @returns !0 if a device @addr is a broadcast address */ 22static inline int uwb_dev_addr_bcast(const struct uwb_dev_addr *addr) 23{ 24 static const struct uwb_dev_addr bcast = { .data = { 0xff, 0xff } }; 25 return !uwb_dev_addr_cmp(addr, &bcast); 26} 27 28/* 29 * Add callback @new to be called when an event occurs in @rc. 30 */ 31int uwb_notifs_register(struct uwb_rc *rc, struct uwb_notifs_handler *new) 32{ 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; 38} 39EXPORT_SYMBOL_GPL(uwb_notifs_register); 40 41/* 42 * Remove event handler (callback) 43 */ 44int uwb_notifs_deregister(struct uwb_rc *rc, struct uwb_notifs_handler *entry) 45{ 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; 51} 52EXPORT_SYMBOL_GPL(uwb_notifs_deregister); 53 54/* 55 * Notify all event handlers of a given event on @rc 56 * 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). 59 */ 60void uwb_notify(struct uwb_rc *rc, struct uwb_dev *uwb_dev, enum uwb_notifs event) 61{ 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); 68 } 69 } 70 mutex_unlock(&rc->notifs_chain.mutex); 71} 72 73/* 74 * Release the backing device of a uwb_dev that has been dynamically allocated. 75 */ 76static void uwb_dev_sys_release(struct device *dev) 77{ 78 struct uwb_dev *uwb_dev = to_uwb_dev(dev); 79 80 uwb_bce_put(uwb_dev->bce); 81 memset(uwb_dev, 0x69, sizeof(*uwb_dev)); 82 kfree(uwb_dev); 83} 84 85/* 86 * Initialize a UWB device instance 87 * 88 * Alloc, zero and call this function. 89 */ 90void uwb_dev_init(struct uwb_dev *uwb_dev) 91{ 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); 98} 99 100static ssize_t uwb_dev_EUI_48_show(struct device *dev, 101 struct device_attribute *attr, char *buf) 102{ 103 struct uwb_dev *uwb_dev = to_uwb_dev(dev); 104 char addr[UWB_ADDR_STRSIZE]; 105 106 uwb_mac_addr_print(addr, sizeof(addr), &uwb_dev->mac_addr); 107 return sprintf(buf, "%s\n", addr); 108} 109static DEVICE_ATTR(EUI_48, S_IRUGO, uwb_dev_EUI_48_show, NULL); 110 111static ssize_t uwb_dev_DevAddr_show(struct device *dev, 112 struct device_attribute *attr, char *buf) 113{ 114 struct uwb_dev *uwb_dev = to_uwb_dev(dev); 115 char addr[UWB_ADDR_STRSIZE]; 116 117 uwb_dev_addr_print(addr, sizeof(addr), &uwb_dev->dev_addr); 118 return sprintf(buf, "%s\n", addr); 119} 120static DEVICE_ATTR(DevAddr, S_IRUGO, uwb_dev_DevAddr_show, NULL); 121 122/* 123 * Show the BPST of this device. 124 * 125 * Calculated from the receive time of the device's beacon and it's 126 * slot number. 127 */ 128static ssize_t uwb_dev_BPST_show(struct device *dev, 129 struct device_attribute *attr, char *buf) 130{ 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; 135 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); 142 143 return sprintf(buf, "%d\n", bpst); 144} 145static DEVICE_ATTR(BPST, S_IRUGO, uwb_dev_BPST_show, NULL); 146 147/* 148 * Show the IEs a device is beaconing 149 * 150 * We need to access the beacon cache, so we just lock it really 151 * quick, print the IEs and unlock. 152 * 153 * We have a reference on the cache entry, so that should be 154 * quite safe. 155 */ 156static ssize_t uwb_dev_IEs_show(struct device *dev, 157 struct device_attribute *attr, char *buf) 158{ 159 struct uwb_dev *uwb_dev = to_uwb_dev(dev); 160 161 return uwb_bce_print_IEs(uwb_dev, uwb_dev->bce, buf, PAGE_SIZE); 162} 163static DEVICE_ATTR(IEs, S_IRUGO | S_IWUSR, uwb_dev_IEs_show, NULL); 164 165static ssize_t uwb_dev_LQE_show(struct device *dev, 166 struct device_attribute *attr, char *buf) 167{ 168 struct uwb_dev *uwb_dev = to_uwb_dev(dev); 169 struct uwb_beca_e *bce = uwb_dev->bce; 170 size_t result; 171 172 mutex_lock(&bce->mutex); 173 result = stats_show(&uwb_dev->bce->lqe_stats, buf); 174 mutex_unlock(&bce->mutex); 175 return result; 176} 177 178static ssize_t uwb_dev_LQE_store(struct device *dev, 179 struct device_attribute *attr, 180 const char *buf, size_t size) 181{ 182 struct uwb_dev *uwb_dev = to_uwb_dev(dev); 183 struct uwb_beca_e *bce = uwb_dev->bce; 184 ssize_t result; 185 186 mutex_lock(&bce->mutex); 187 result = stats_store(&uwb_dev->bce->lqe_stats, buf, size); 188 mutex_unlock(&bce->mutex); 189 return result; 190} 191static DEVICE_ATTR(LQE, S_IRUGO | S_IWUSR, uwb_dev_LQE_show, uwb_dev_LQE_store); 192 193static ssize_t uwb_dev_RSSI_show(struct device *dev, 194 struct device_attribute *attr, char *buf) 195{ 196 struct uwb_dev *uwb_dev = to_uwb_dev(dev); 197 struct uwb_beca_e *bce = uwb_dev->bce; 198 size_t result; 199 200 mutex_lock(&bce->mutex); 201 result = stats_show(&uwb_dev->bce->rssi_stats, buf); 202 mutex_unlock(&bce->mutex); 203 return result; 204} 205 206static ssize_t uwb_dev_RSSI_store(struct device *dev, 207 struct device_attribute *attr, 208 const char *buf, size_t size) 209{ 210 struct uwb_dev *uwb_dev = to_uwb_dev(dev); 211 struct uwb_beca_e *bce = uwb_dev->bce; 212 ssize_t result; 213 214 mutex_lock(&bce->mutex); 215 result = stats_store(&uwb_dev->bce->rssi_stats, buf, size); 216 mutex_unlock(&bce->mutex); 217 return result; 218} 219static DEVICE_ATTR(RSSI, S_IRUGO | S_IWUSR, uwb_dev_RSSI_show, uwb_dev_RSSI_store); 220 221 222static 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, 230}; 231 232static struct attribute_group dev_attr_group = { 233 .attrs = dev_attrs, 234}; 235 236static const struct attribute_group *groups[] = { 237 &dev_attr_group, 238 NULL, 239}; 240 241/** 242 * Device SYSFS registration 243 * 244 * 245 */ 246static int __uwb_dev_sys_add(struct uwb_dev *uwb_dev, struct device *parent_dev) 247{ 248 struct device *dev; 249 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); 257 258 return device_add(dev); 259} 260 261 262static void __uwb_dev_sys_rm(struct uwb_dev *uwb_dev) 263{ 264 dev_set_drvdata(&uwb_dev->dev, NULL); 265 device_del(&uwb_dev->dev); 266} 267 268 269/** 270 * Register and initialize a new UWB device 271 * 272 * Did you call uwb_dev_init() on it? 273 * 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. 277 * 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. 281 */ 282int uwb_dev_add(struct uwb_dev *uwb_dev, struct device *parent_dev, 283 struct uwb_rc *parent_rc) 284{ 285 int result; 286 struct device *dev; 287 288 BUG_ON(uwb_dev == NULL); 289 BUG_ON(parent_dev == NULL); 290 BUG_ON(parent_rc == NULL); 291 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; 301} 302 303 304void uwb_dev_rm(struct uwb_dev *uwb_dev) 305{ 306 mutex_lock(&uwb_dev->mutex); 307 __uwb_dev_sys_rm(uwb_dev); 308 mutex_unlock(&uwb_dev->mutex); 309} 310 311 312static 313int __uwb_dev_try_get(struct device *dev, void *__target_uwb_dev) 314{ 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; 322} 323 324 325/** 326 * Given a UWB device descriptor, validate and refcount it 327 * 328 * @returns NULL if the device does not exist or is quiescing; the ptr to 329 * it otherwise. 330 */ 331struct uwb_dev *uwb_dev_try_get(struct uwb_rc *rc, struct uwb_dev *uwb_dev) 332{ 333 if (uwb_dev_for_each(rc, __uwb_dev_try_get, uwb_dev)) 334 return uwb_dev; 335 else 336 return NULL; 337} 338EXPORT_SYMBOL_GPL(uwb_dev_try_get); 339 340 341/** 342 * Remove a device from the system [grunt for other functions] 343 */ 344int __uwb_dev_offair(struct uwb_dev *uwb_dev, struct uwb_rc *rc) 345{ 346 struct device *dev = &uwb_dev->dev; 347 char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE]; 348 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() */ 359 360 return 0; 361} 362 363 364/** 365 * A device went off the air, clean up after it! 366 * 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. 370 * 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. 373 * 374 * NOTE: uwb_bcn_cache.mutex is held! 375 */ 376void uwbd_dev_offair(struct uwb_beca_e *bce) 377{ 378 struct uwb_dev *uwb_dev; 379 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); 384 } 385} 386 387 388/** 389 * A device went on the air, start it up! 390 * 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. 393 * 394 * It will just create the freaking device, register the beacon and 395 * stuff and yatla, done. 396 * 397 * 398 * NOTE: uwb_beca.mutex is held, bce->mutex is held 399 */ 400void uwbd_dev_onair(struct uwb_rc *rc, struct uwb_beca_e *bce) 401{ 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]; 406 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; 414 } 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; 424 } 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; 434 435error_dev_add: 436 kfree(uwb_dev); 437 return; 438} 439 440/** 441 * Iterate over the list of UWB devices, calling a @function on each 442 * 443 * See docs for bus_for_each().... 444 * 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. 450 */ 451int uwb_dev_for_each(struct uwb_rc *rc, uwb_dev_for_each_f function, void *priv) 452{ 453 return device_for_each_child(&rc->uwb_dev.dev, priv, function); 454} 455EXPORT_SYMBOL_GPL(uwb_dev_for_each); 456