linux/drivers/uwb/lc-rc.c
<<
>>
Prefs
   1/*
   2 * Ultra Wide Band
   3 * Life cycle of radio controllers
   4 *
   5 * Copyright (C) 2005-2006 Intel Corporation
   6 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
   7 *
   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.
  11 *
  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.
  16 *
  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.
  21 *
  22 *
  23 * FIXME: docs
  24 *
  25 * A UWB radio controller is also a UWB device, so it embeds one...
  26 *
  27 * List of RCs comes from the 'struct class uwb_rc_class'.
  28 */
  29
  30#include <linux/kernel.h>
  31#include <linux/string.h>
  32#include <linux/device.h>
  33#include <linux/err.h>
  34#include <linux/random.h>
  35#include <linux/kdev_t.h>
  36#include <linux/etherdevice.h>
  37#include <linux/usb.h>
  38#include <linux/slab.h>
  39#include <linux/export.h>
  40
  41#include "uwb-internal.h"
  42
  43static int uwb_rc_index_match(struct device *dev, const void *data)
  44{
  45        const int *index = data;
  46        struct uwb_rc *rc = dev_get_drvdata(dev);
  47
  48        if (rc->index == *index)
  49                return 1;
  50        return 0;
  51}
  52
  53static struct uwb_rc *uwb_rc_find_by_index(int index)
  54{
  55        struct device *dev;
  56        struct uwb_rc *rc = NULL;
  57
  58        dev = class_find_device(&uwb_rc_class, NULL, &index, uwb_rc_index_match);
  59        if (dev)
  60                rc = dev_get_drvdata(dev);
  61        return rc;
  62}
  63
  64static int uwb_rc_new_index(void)
  65{
  66        int index = 0;
  67
  68        for (;;) {
  69                if (!uwb_rc_find_by_index(index))
  70                        return index;
  71                if (++index < 0)
  72                        index = 0;
  73        }
  74}
  75
  76/**
  77 * Release the backing device of a uwb_rc that has been dynamically allocated.
  78 */
  79static void uwb_rc_sys_release(struct device *dev)
  80{
  81        struct uwb_dev *uwb_dev = container_of(dev, struct uwb_dev, dev);
  82        struct uwb_rc *rc = container_of(uwb_dev, struct uwb_rc, uwb_dev);
  83
  84        uwb_rc_ie_release(rc);
  85        kfree(rc);
  86}
  87
  88
  89void uwb_rc_init(struct uwb_rc *rc)
  90{
  91        struct uwb_dev *uwb_dev = &rc->uwb_dev;
  92
  93        uwb_dev_init(uwb_dev);
  94        rc->uwb_dev.dev.class = &uwb_rc_class;
  95        rc->uwb_dev.dev.release = uwb_rc_sys_release;
  96        uwb_rc_neh_create(rc);
  97        rc->beaconing = -1;
  98        rc->scan_type = UWB_SCAN_DISABLED;
  99        INIT_LIST_HEAD(&rc->notifs_chain.list);
 100        mutex_init(&rc->notifs_chain.mutex);
 101        INIT_LIST_HEAD(&rc->uwb_beca.list);
 102        mutex_init(&rc->uwb_beca.mutex);
 103        uwb_drp_avail_init(rc);
 104        uwb_rc_ie_init(rc);
 105        uwb_rsv_init(rc);
 106        uwb_rc_pal_init(rc);
 107}
 108EXPORT_SYMBOL_GPL(uwb_rc_init);
 109
 110
 111struct uwb_rc *uwb_rc_alloc(void)
 112{
 113        struct uwb_rc *rc;
 114        rc = kzalloc(sizeof(*rc), GFP_KERNEL);
 115        if (rc == NULL)
 116                return NULL;
 117        uwb_rc_init(rc);
 118        return rc;
 119}
 120EXPORT_SYMBOL_GPL(uwb_rc_alloc);
 121
 122/*
 123 * Show the ASIE that is broadcast in the UWB beacon by this uwb_rc device.
 124 */
 125static ssize_t ASIE_show(struct device *dev,
 126                                struct device_attribute *attr, char *buf)
 127{
 128        struct uwb_dev *uwb_dev = to_uwb_dev(dev);
 129        struct uwb_rc *rc = uwb_dev->rc;
 130        struct uwb_ie_hdr *ie;
 131        void *ptr;
 132        size_t len;
 133        int result = 0;
 134
 135        /* init empty buffer. */
 136        result = scnprintf(buf, PAGE_SIZE, "\n");
 137        mutex_lock(&rc->ies_mutex);
 138        /* walk IEData looking for an ASIE. */
 139        ptr = rc->ies->IEData;
 140        len = le16_to_cpu(rc->ies->wIELength);
 141        for (;;) {
 142                ie = uwb_ie_next(&ptr, &len);
 143                if (!ie)
 144                        break;
 145                if (ie->element_id == UWB_APP_SPEC_IE) {
 146                        result = uwb_ie_dump_hex(ie,
 147                                        ie->length + sizeof(struct uwb_ie_hdr),
 148                                        buf, PAGE_SIZE);
 149                        break;
 150                }
 151        }
 152        mutex_unlock(&rc->ies_mutex);
 153
 154        return result;
 155}
 156
 157/*
 158 * Update the ASIE that is broadcast in the UWB beacon by this uwb_rc device.
 159 */
 160static ssize_t ASIE_store(struct device *dev,
 161                                 struct device_attribute *attr,
 162                                 const char *buf, size_t size)
 163{
 164        struct uwb_dev *uwb_dev = to_uwb_dev(dev);
 165        struct uwb_rc *rc = uwb_dev->rc;
 166        char ie_buf[255];
 167        int result, ie_len = 0;
 168        const char *cur_ptr = buf;
 169        struct uwb_ie_hdr *ie;
 170
 171        /* empty string means clear the ASIE. */
 172        if (strlen(buf) <= 1) {
 173                uwb_rc_ie_rm(rc, UWB_APP_SPEC_IE);
 174                return size;
 175        }
 176
 177        /* if non-empty string, convert string of hex chars to binary. */
 178        while (ie_len < sizeof(ie_buf)) {
 179                int char_count;
 180
 181                if (sscanf(cur_ptr, " %02hhX %n",
 182                                &(ie_buf[ie_len]), &char_count) > 0) {
 183                        ++ie_len;
 184                        /* skip chars read from cur_ptr. */
 185                        cur_ptr += char_count;
 186                } else {
 187                        break;
 188                }
 189        }
 190
 191        /* validate IE length and type. */
 192        if (ie_len < sizeof(struct uwb_ie_hdr)) {
 193                dev_err(dev, "%s: Invalid ASIE size %d.\n", __func__, ie_len);
 194                return -EINVAL;
 195        }
 196
 197        ie = (struct uwb_ie_hdr *)ie_buf;
 198        if (ie->element_id != UWB_APP_SPEC_IE) {
 199                dev_err(dev, "%s: Invalid IE element type size = 0x%02X.\n",
 200                                __func__, ie->element_id);
 201                return -EINVAL;
 202        }
 203
 204        /* bounds check length field from user. */
 205        if (ie->length > (ie_len - sizeof(struct uwb_ie_hdr)))
 206                ie->length = ie_len - sizeof(struct uwb_ie_hdr);
 207
 208        /*
 209         * Valid ASIE received. Remove current ASIE then add the new one using
 210         * uwb_rc_ie_add.
 211         */
 212        uwb_rc_ie_rm(rc, UWB_APP_SPEC_IE);
 213
 214        result = uwb_rc_ie_add(rc, ie, ie->length + sizeof(struct uwb_ie_hdr));
 215
 216        return result >= 0 ? size : result;
 217}
 218static DEVICE_ATTR_RW(ASIE);
 219
 220static struct attribute *rc_attrs[] = {
 221                &dev_attr_mac_address.attr,
 222                &dev_attr_scan.attr,
 223                &dev_attr_beacon.attr,
 224                &dev_attr_ASIE.attr,
 225                NULL,
 226};
 227
 228static struct attribute_group rc_attr_group = {
 229        .attrs = rc_attrs,
 230};
 231
 232/*
 233 * Registration of sysfs specific stuff
 234 */
 235static int uwb_rc_sys_add(struct uwb_rc *rc)
 236{
 237        return sysfs_create_group(&rc->uwb_dev.dev.kobj, &rc_attr_group);
 238}
 239
 240
 241static void __uwb_rc_sys_rm(struct uwb_rc *rc)
 242{
 243        sysfs_remove_group(&rc->uwb_dev.dev.kobj, &rc_attr_group);
 244}
 245
 246/**
 247 * uwb_rc_mac_addr_setup - get an RC's EUI-48 address or set it
 248 * @rc:  the radio controller.
 249 *
 250 * If the EUI-48 address is 00:00:00:00:00:00 or FF:FF:FF:FF:FF:FF
 251 * then a random locally administered EUI-48 is generated and set on
 252 * the device.  The probability of address collisions is sufficiently
 253 * unlikely (1/2^40 = 9.1e-13) that they're not checked for.
 254 */
 255static
 256int uwb_rc_mac_addr_setup(struct uwb_rc *rc)
 257{
 258        int result;
 259        struct device *dev = &rc->uwb_dev.dev;
 260        struct uwb_dev *uwb_dev = &rc->uwb_dev;
 261        char devname[UWB_ADDR_STRSIZE];
 262        struct uwb_mac_addr addr;
 263
 264        result = uwb_rc_mac_addr_get(rc, &addr);
 265        if (result < 0) {
 266                dev_err(dev, "cannot retrieve UWB EUI-48 address: %d\n", result);
 267                return result;
 268        }
 269
 270        if (uwb_mac_addr_unset(&addr) || uwb_mac_addr_bcast(&addr)) {
 271                addr.data[0] = 0x02; /* locally administered and unicast */
 272                get_random_bytes(&addr.data[1], sizeof(addr.data)-1);
 273
 274                result = uwb_rc_mac_addr_set(rc, &addr);
 275                if (result < 0) {
 276                        uwb_mac_addr_print(devname, sizeof(devname), &addr);
 277                        dev_err(dev, "cannot set EUI-48 address %s: %d\n",
 278                                devname, result);
 279                        return result;
 280                }
 281        }
 282        uwb_dev->mac_addr = addr;
 283        return 0;
 284}
 285
 286
 287
 288static int uwb_rc_setup(struct uwb_rc *rc)
 289{
 290        int result;
 291        struct device *dev = &rc->uwb_dev.dev;
 292
 293        result = uwb_radio_setup(rc);
 294        if (result < 0) {
 295                dev_err(dev, "cannot setup UWB radio: %d\n", result);
 296                goto error;
 297        }
 298        result = uwb_rc_mac_addr_setup(rc);
 299        if (result < 0) {
 300                dev_err(dev, "cannot setup UWB MAC address: %d\n", result);
 301                goto error;
 302        }
 303        result = uwb_rc_dev_addr_assign(rc);
 304        if (result < 0) {
 305                dev_err(dev, "cannot assign UWB DevAddr: %d\n", result);
 306                goto error;
 307        }
 308        result = uwb_rc_ie_setup(rc);
 309        if (result < 0) {
 310                dev_err(dev, "cannot setup IE subsystem: %d\n", result);
 311                goto error_ie_setup;
 312        }
 313        result = uwb_rsv_setup(rc);
 314        if (result < 0) {
 315                dev_err(dev, "cannot setup reservation subsystem: %d\n", result);
 316                goto error_rsv_setup;
 317        }
 318        uwb_dbg_add_rc(rc);
 319        return 0;
 320
 321error_rsv_setup:
 322        uwb_rc_ie_release(rc);
 323error_ie_setup:
 324error:
 325        return result;
 326}
 327
 328
 329/**
 330 * Register a new UWB radio controller
 331 *
 332 * Did you call uwb_rc_init() on your rc?
 333 *
 334 * We assume that this is being called with a > 0 refcount on
 335 * it [through ops->{get|put}_device(). We'll take our own, though.
 336 *
 337 * @parent_dev is our real device, the one that provides the actual UWB device
 338 */
 339int uwb_rc_add(struct uwb_rc *rc, struct device *parent_dev, void *priv)
 340{
 341        int result;
 342        struct device *dev;
 343        char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE];
 344
 345        rc->index = uwb_rc_new_index();
 346
 347        dev = &rc->uwb_dev.dev;
 348        dev_set_name(dev, "uwb%d", rc->index);
 349
 350        rc->priv = priv;
 351
 352        init_waitqueue_head(&rc->uwbd.wq);
 353        INIT_LIST_HEAD(&rc->uwbd.event_list);
 354        spin_lock_init(&rc->uwbd.event_list_lock);
 355
 356        uwbd_start(rc);
 357
 358        result = rc->start(rc);
 359        if (result < 0)
 360                goto error_rc_start;
 361
 362        result = uwb_rc_setup(rc);
 363        if (result < 0) {
 364                dev_err(dev, "cannot setup UWB radio controller: %d\n", result);
 365                goto error_rc_setup;
 366        }
 367
 368        result = uwb_dev_add(&rc->uwb_dev, parent_dev, rc);
 369        if (result < 0 && result != -EADDRNOTAVAIL)
 370                goto error_dev_add;
 371
 372        result = uwb_rc_sys_add(rc);
 373        if (result < 0) {
 374                dev_err(parent_dev, "cannot register UWB radio controller "
 375                        "dev attributes: %d\n", result);
 376                goto error_sys_add;
 377        }
 378
 379        uwb_mac_addr_print(macbuf, sizeof(macbuf), &rc->uwb_dev.mac_addr);
 380        uwb_dev_addr_print(devbuf, sizeof(devbuf), &rc->uwb_dev.dev_addr);
 381        dev_info(dev,
 382                 "new uwb radio controller (mac %s dev %s) on %s %s\n",
 383                 macbuf, devbuf, parent_dev->bus->name, dev_name(parent_dev));
 384        rc->ready = 1;
 385        return 0;
 386
 387error_sys_add:
 388        uwb_dev_rm(&rc->uwb_dev);
 389error_dev_add:
 390error_rc_setup:
 391        rc->stop(rc);
 392error_rc_start:
 393        uwbd_stop(rc);
 394        return result;
 395}
 396EXPORT_SYMBOL_GPL(uwb_rc_add);
 397
 398
 399static int uwb_dev_offair_helper(struct device *dev, void *priv)
 400{
 401        struct uwb_dev *uwb_dev = to_uwb_dev(dev);
 402
 403        return __uwb_dev_offair(uwb_dev, uwb_dev->rc);
 404}
 405
 406/*
 407 * Remove a Radio Controller; stop beaconing/scanning, disconnect all children
 408 */
 409void uwb_rc_rm(struct uwb_rc *rc)
 410{
 411        rc->ready = 0;
 412
 413        uwb_dbg_del_rc(rc);
 414        uwb_rsv_remove_all(rc);
 415        uwb_radio_shutdown(rc);
 416
 417        rc->stop(rc);
 418
 419        uwbd_stop(rc);
 420        uwb_rc_neh_destroy(rc);
 421
 422        uwb_dev_lock(&rc->uwb_dev);
 423        rc->priv = NULL;
 424        rc->cmd = NULL;
 425        uwb_dev_unlock(&rc->uwb_dev);
 426        mutex_lock(&rc->uwb_beca.mutex);
 427        uwb_dev_for_each(rc, uwb_dev_offair_helper, NULL);
 428        __uwb_rc_sys_rm(rc);
 429        mutex_unlock(&rc->uwb_beca.mutex);
 430        uwb_rsv_cleanup(rc);
 431        uwb_beca_release(rc);
 432        uwb_dev_rm(&rc->uwb_dev);
 433}
 434EXPORT_SYMBOL_GPL(uwb_rc_rm);
 435
 436static int find_rc_try_get(struct device *dev, const void *data)
 437{
 438        const struct uwb_rc *target_rc = data;
 439        struct uwb_rc *rc = dev_get_drvdata(dev);
 440
 441        if (rc == NULL) {
 442                WARN_ON(1);
 443                return 0;
 444        }
 445        if (rc == target_rc) {
 446                if (rc->ready == 0)
 447                        return 0;
 448                else
 449                        return 1;
 450        }
 451        return 0;
 452}
 453
 454/**
 455 * Given a radio controller descriptor, validate and refcount it
 456 *
 457 * @returns NULL if the rc does not exist or is quiescing; the ptr to
 458 *               it otherwise.
 459 */
 460struct uwb_rc *__uwb_rc_try_get(struct uwb_rc *target_rc)
 461{
 462        struct device *dev;
 463        struct uwb_rc *rc = NULL;
 464
 465        dev = class_find_device(&uwb_rc_class, NULL, target_rc,
 466                                find_rc_try_get);
 467        if (dev) {
 468                rc = dev_get_drvdata(dev);
 469                __uwb_rc_get(rc);
 470        }
 471        return rc;
 472}
 473EXPORT_SYMBOL_GPL(__uwb_rc_try_get);
 474
 475/*
 476 * RC get for external refcount acquirers...
 477 *
 478 * Increments the refcount of the device and it's backend modules
 479 */
 480static inline struct uwb_rc *uwb_rc_get(struct uwb_rc *rc)
 481{
 482        if (rc->ready == 0)
 483                return NULL;
 484        uwb_dev_get(&rc->uwb_dev);
 485        return rc;
 486}
 487
 488static int find_rc_grandpa(struct device *dev, const void *data)
 489{
 490        const struct device *grandpa_dev = data;
 491        struct uwb_rc *rc = dev_get_drvdata(dev);
 492
 493        if (rc->uwb_dev.dev.parent->parent == grandpa_dev) {
 494                rc = uwb_rc_get(rc);
 495                return 1;
 496        }
 497        return 0;
 498}
 499
 500/**
 501 * Locate and refcount a radio controller given a common grand-parent
 502 *
 503 * @grandpa_dev  Pointer to the 'grandparent' device structure.
 504 * @returns NULL If the rc does not exist or is quiescing; the ptr to
 505 *               it otherwise, properly referenced.
 506 *
 507 * The Radio Control interface (or the UWB Radio Controller) is always
 508 * an interface of a device. The parent is the interface, the
 509 * grandparent is the device that encapsulates the interface.
 510 *
 511 * There is no need to lock around as the "grandpa" would be
 512 * refcounted by the target, and to remove the referemes, the
 513 * uwb_rc_class->sem would have to be taken--we hold it, ergo we
 514 * should be safe.
 515 */
 516struct uwb_rc *uwb_rc_get_by_grandpa(const struct device *grandpa_dev)
 517{
 518        struct device *dev;
 519        struct uwb_rc *rc = NULL;
 520
 521        dev = class_find_device(&uwb_rc_class, NULL, grandpa_dev,
 522                                find_rc_grandpa);
 523        if (dev)
 524                rc = dev_get_drvdata(dev);
 525        return rc;
 526}
 527EXPORT_SYMBOL_GPL(uwb_rc_get_by_grandpa);
 528
 529/**
 530 * Find a radio controller by device address
 531 *
 532 * @returns the pointer to the radio controller, properly referenced
 533 */
 534static int find_rc_dev(struct device *dev, const void *data)
 535{
 536        const struct uwb_dev_addr *addr = data;
 537        struct uwb_rc *rc = dev_get_drvdata(dev);
 538
 539        if (rc == NULL) {
 540                WARN_ON(1);
 541                return 0;
 542        }
 543        if (!uwb_dev_addr_cmp(&rc->uwb_dev.dev_addr, addr)) {
 544                rc = uwb_rc_get(rc);
 545                return 1;
 546        }
 547        return 0;
 548}
 549
 550struct uwb_rc *uwb_rc_get_by_dev(const struct uwb_dev_addr *addr)
 551{
 552        struct device *dev;
 553        struct uwb_rc *rc = NULL;
 554
 555        dev = class_find_device(&uwb_rc_class, NULL, addr, find_rc_dev);
 556        if (dev)
 557                rc = dev_get_drvdata(dev);
 558
 559        return rc;
 560}
 561EXPORT_SYMBOL_GPL(uwb_rc_get_by_dev);
 562
 563/**
 564 * Drop a reference on a radio controller
 565 *
 566 * This is the version that should be done by entities external to the
 567 * UWB Radio Control stack (ie: clients of the API).
 568 */
 569void uwb_rc_put(struct uwb_rc *rc)
 570{
 571        __uwb_rc_put(rc);
 572}
 573EXPORT_SYMBOL_GPL(uwb_rc_put);
 574