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