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
  39#include "uwb-internal.h"
  40
  41static int uwb_rc_index_match(struct device *dev, void *data)
  42{
  43        int *index = data;
  44        struct uwb_rc *rc = dev_get_drvdata(dev);
  45
  46        if (rc->index == *index)
  47                return 1;
  48        return 0;
  49}
  50
  51static struct uwb_rc *uwb_rc_find_by_index(int index)
  52{
  53        struct device *dev;
  54        struct uwb_rc *rc = NULL;
  55
  56        dev = class_find_device(&uwb_rc_class, NULL, &index, uwb_rc_index_match);
  57        if (dev)
  58                rc = dev_get_drvdata(dev);
  59        return rc;
  60}
  61
  62static int uwb_rc_new_index(void)
  63{
  64        int index = 0;
  65
  66        for (;;) {
  67                if (!uwb_rc_find_by_index(index))
  68                        return index;
  69                if (++index < 0)
  70                        index = 0;
  71        }
  72}
  73
  74/**
  75 * Release the backing device of a uwb_rc that has been dynamically allocated.
  76 */
  77static void uwb_rc_sys_release(struct device *dev)
  78{
  79        struct uwb_dev *uwb_dev = container_of(dev, struct uwb_dev, dev);
  80        struct uwb_rc *rc = container_of(uwb_dev, struct uwb_rc, uwb_dev);
  81
  82        uwb_rc_ie_release(rc);
  83        kfree(rc);
  84}
  85
  86
  87void uwb_rc_init(struct uwb_rc *rc)
  88{
  89        struct uwb_dev *uwb_dev = &rc->uwb_dev;
  90
  91        uwb_dev_init(uwb_dev);
  92        rc->uwb_dev.dev.class = &uwb_rc_class;
  93        rc->uwb_dev.dev.release = uwb_rc_sys_release;
  94        uwb_rc_neh_create(rc);
  95        rc->beaconing = -1;
  96        rc->scan_type = UWB_SCAN_DISABLED;
  97        INIT_LIST_HEAD(&rc->notifs_chain.list);
  98        mutex_init(&rc->notifs_chain.mutex);
  99        INIT_LIST_HEAD(&rc->uwb_beca.list);
 100        mutex_init(&rc->uwb_beca.mutex);
 101        uwb_drp_avail_init(rc);
 102        uwb_rc_ie_init(rc);
 103        uwb_rsv_init(rc);
 104        uwb_rc_pal_init(rc);
 105}
 106EXPORT_SYMBOL_GPL(uwb_rc_init);
 107
 108
 109struct uwb_rc *uwb_rc_alloc(void)
 110{
 111        struct uwb_rc *rc;
 112        rc = kzalloc(sizeof(*rc), GFP_KERNEL);
 113        if (rc == NULL)
 114                return NULL;
 115        uwb_rc_init(rc);
 116        return rc;
 117}
 118EXPORT_SYMBOL_GPL(uwb_rc_alloc);
 119
 120static struct attribute *rc_attrs[] = {
 121                &dev_attr_mac_address.attr,
 122                &dev_attr_scan.attr,
 123                &dev_attr_beacon.attr,
 124                NULL,
 125};
 126
 127static struct attribute_group rc_attr_group = {
 128        .attrs = rc_attrs,
 129};
 130
 131/*
 132 * Registration of sysfs specific stuff
 133 */
 134static int uwb_rc_sys_add(struct uwb_rc *rc)
 135{
 136        return sysfs_create_group(&rc->uwb_dev.dev.kobj, &rc_attr_group);
 137}
 138
 139
 140static void __uwb_rc_sys_rm(struct uwb_rc *rc)
 141{
 142        sysfs_remove_group(&rc->uwb_dev.dev.kobj, &rc_attr_group);
 143}
 144
 145/**
 146 * uwb_rc_mac_addr_setup - get an RC's EUI-48 address or set it
 147 * @rc:  the radio controller.
 148 *
 149 * If the EUI-48 address is 00:00:00:00:00:00 or FF:FF:FF:FF:FF:FF
 150 * then a random locally administered EUI-48 is generated and set on
 151 * the device.  The probability of address collisions is sufficiently
 152 * unlikely (1/2^40 = 9.1e-13) that they're not checked for.
 153 */
 154static
 155int uwb_rc_mac_addr_setup(struct uwb_rc *rc)
 156{
 157        int result;
 158        struct device *dev = &rc->uwb_dev.dev;
 159        struct uwb_dev *uwb_dev = &rc->uwb_dev;
 160        char devname[UWB_ADDR_STRSIZE];
 161        struct uwb_mac_addr addr;
 162
 163        result = uwb_rc_mac_addr_get(rc, &addr);
 164        if (result < 0) {
 165                dev_err(dev, "cannot retrieve UWB EUI-48 address: %d\n", result);
 166                return result;
 167        }
 168
 169        if (uwb_mac_addr_unset(&addr) || uwb_mac_addr_bcast(&addr)) {
 170                addr.data[0] = 0x02; /* locally adminstered and unicast */
 171                get_random_bytes(&addr.data[1], sizeof(addr.data)-1);
 172
 173                result = uwb_rc_mac_addr_set(rc, &addr);
 174                if (result < 0) {
 175                        uwb_mac_addr_print(devname, sizeof(devname), &addr);
 176                        dev_err(dev, "cannot set EUI-48 address %s: %d\n",
 177                                devname, result);
 178                        return result;
 179                }
 180        }
 181        uwb_dev->mac_addr = addr;
 182        return 0;
 183}
 184
 185
 186
 187static int uwb_rc_setup(struct uwb_rc *rc)
 188{
 189        int result;
 190        struct device *dev = &rc->uwb_dev.dev;
 191
 192        result = uwb_radio_setup(rc);
 193        if (result < 0) {
 194                dev_err(dev, "cannot setup UWB radio: %d\n", result);
 195                goto error;
 196        }
 197        result = uwb_rc_mac_addr_setup(rc);
 198        if (result < 0) {
 199                dev_err(dev, "cannot setup UWB MAC address: %d\n", result);
 200                goto error;
 201        }
 202        result = uwb_rc_dev_addr_assign(rc);
 203        if (result < 0) {
 204                dev_err(dev, "cannot assign UWB DevAddr: %d\n", result);
 205                goto error;
 206        }
 207        result = uwb_rc_ie_setup(rc);
 208        if (result < 0) {
 209                dev_err(dev, "cannot setup IE subsystem: %d\n", result);
 210                goto error_ie_setup;
 211        }
 212        result = uwb_rsv_setup(rc);
 213        if (result < 0) {
 214                dev_err(dev, "cannot setup reservation subsystem: %d\n", result);
 215                goto error_rsv_setup;
 216        }
 217        uwb_dbg_add_rc(rc);
 218        return 0;
 219
 220error_rsv_setup:
 221        uwb_rc_ie_release(rc);
 222error_ie_setup:
 223error:
 224        return result;
 225}
 226
 227
 228/**
 229 * Register a new UWB radio controller
 230 *
 231 * Did you call uwb_rc_init() on your rc?
 232 *
 233 * We assume that this is being called with a > 0 refcount on
 234 * it [through ops->{get|put}_device(). We'll take our own, though.
 235 *
 236 * @parent_dev is our real device, the one that provides the actual UWB device
 237 */
 238int uwb_rc_add(struct uwb_rc *rc, struct device *parent_dev, void *priv)
 239{
 240        int result;
 241        struct device *dev;
 242        char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE];
 243
 244        rc->index = uwb_rc_new_index();
 245
 246        dev = &rc->uwb_dev.dev;
 247        dev_set_name(dev, "uwb%d", rc->index);
 248
 249        rc->priv = priv;
 250
 251        init_waitqueue_head(&rc->uwbd.wq);
 252        INIT_LIST_HEAD(&rc->uwbd.event_list);
 253        spin_lock_init(&rc->uwbd.event_list_lock);
 254
 255        uwbd_start(rc);
 256
 257        result = rc->start(rc);
 258        if (result < 0)
 259                goto error_rc_start;
 260
 261        result = uwb_rc_setup(rc);
 262        if (result < 0) {
 263                dev_err(dev, "cannot setup UWB radio controller: %d\n", result);
 264                goto error_rc_setup;
 265        }
 266
 267        result = uwb_dev_add(&rc->uwb_dev, parent_dev, rc);
 268        if (result < 0 && result != -EADDRNOTAVAIL)
 269                goto error_dev_add;
 270
 271        result = uwb_rc_sys_add(rc);
 272        if (result < 0) {
 273                dev_err(parent_dev, "cannot register UWB radio controller "
 274                        "dev attributes: %d\n", result);
 275                goto error_sys_add;
 276        }
 277
 278        uwb_mac_addr_print(macbuf, sizeof(macbuf), &rc->uwb_dev.mac_addr);
 279        uwb_dev_addr_print(devbuf, sizeof(devbuf), &rc->uwb_dev.dev_addr);
 280        dev_info(dev,
 281                 "new uwb radio controller (mac %s dev %s) on %s %s\n",
 282                 macbuf, devbuf, parent_dev->bus->name, dev_name(parent_dev));
 283        rc->ready = 1;
 284        return 0;
 285
 286error_sys_add:
 287        uwb_dev_rm(&rc->uwb_dev);
 288error_dev_add:
 289error_rc_setup:
 290        rc->stop(rc);
 291        uwbd_stop(rc);
 292error_rc_start:
 293        return result;
 294}
 295EXPORT_SYMBOL_GPL(uwb_rc_add);
 296
 297
 298static int uwb_dev_offair_helper(struct device *dev, void *priv)
 299{
 300        struct uwb_dev *uwb_dev = to_uwb_dev(dev);
 301
 302        return __uwb_dev_offair(uwb_dev, uwb_dev->rc);
 303}
 304
 305/*
 306 * Remove a Radio Controller; stop beaconing/scanning, disconnect all children
 307 */
 308void uwb_rc_rm(struct uwb_rc *rc)
 309{
 310        rc->ready = 0;
 311
 312        uwb_dbg_del_rc(rc);
 313        uwb_rsv_remove_all(rc);
 314        uwb_radio_shutdown(rc);
 315
 316        rc->stop(rc);
 317
 318        uwbd_stop(rc);
 319        uwb_rc_neh_destroy(rc);
 320
 321        uwb_dev_lock(&rc->uwb_dev);
 322        rc->priv = NULL;
 323        rc->cmd = NULL;
 324        uwb_dev_unlock(&rc->uwb_dev);
 325        mutex_lock(&rc->uwb_beca.mutex);
 326        uwb_dev_for_each(rc, uwb_dev_offair_helper, NULL);
 327        __uwb_rc_sys_rm(rc);
 328        mutex_unlock(&rc->uwb_beca.mutex);
 329        uwb_rsv_cleanup(rc);
 330        uwb_beca_release(rc);
 331        uwb_dev_rm(&rc->uwb_dev);
 332}
 333EXPORT_SYMBOL_GPL(uwb_rc_rm);
 334
 335static int find_rc_try_get(struct device *dev, void *data)
 336{
 337        struct uwb_rc *target_rc = data;
 338        struct uwb_rc *rc = dev_get_drvdata(dev);
 339
 340        if (rc == NULL) {
 341                WARN_ON(1);
 342                return 0;
 343        }
 344        if (rc == target_rc) {
 345                if (rc->ready == 0)
 346                        return 0;
 347                else
 348                        return 1;
 349        }
 350        return 0;
 351}
 352
 353/**
 354 * Given a radio controller descriptor, validate and refcount it
 355 *
 356 * @returns NULL if the rc does not exist or is quiescing; the ptr to
 357 *               it otherwise.
 358 */
 359struct uwb_rc *__uwb_rc_try_get(struct uwb_rc *target_rc)
 360{
 361        struct device *dev;
 362        struct uwb_rc *rc = NULL;
 363
 364        dev = class_find_device(&uwb_rc_class, NULL, target_rc,
 365                                find_rc_try_get);
 366        if (dev) {
 367                rc = dev_get_drvdata(dev);
 368                __uwb_rc_get(rc);
 369        }
 370        return rc;
 371}
 372EXPORT_SYMBOL_GPL(__uwb_rc_try_get);
 373
 374/*
 375 * RC get for external refcount acquirers...
 376 *
 377 * Increments the refcount of the device and it's backend modules
 378 */
 379static inline struct uwb_rc *uwb_rc_get(struct uwb_rc *rc)
 380{
 381        if (rc->ready == 0)
 382                return NULL;
 383        uwb_dev_get(&rc->uwb_dev);
 384        return rc;
 385}
 386
 387static int find_rc_grandpa(struct device *dev, void *data)
 388{
 389        struct device *grandpa_dev = data;
 390        struct uwb_rc *rc = dev_get_drvdata(dev);
 391
 392        if (rc->uwb_dev.dev.parent->parent == grandpa_dev) {
 393                rc = uwb_rc_get(rc);
 394                return 1;
 395        }
 396        return 0;
 397}
 398
 399/**
 400 * Locate and refcount a radio controller given a common grand-parent
 401 *
 402 * @grandpa_dev  Pointer to the 'grandparent' device structure.
 403 * @returns NULL If the rc does not exist or is quiescing; the ptr to
 404 *               it otherwise, properly referenced.
 405 *
 406 * The Radio Control interface (or the UWB Radio Controller) is always
 407 * an interface of a device. The parent is the interface, the
 408 * grandparent is the device that encapsulates the interface.
 409 *
 410 * There is no need to lock around as the "grandpa" would be
 411 * refcounted by the target, and to remove the referemes, the
 412 * uwb_rc_class->sem would have to be taken--we hold it, ergo we
 413 * should be safe.
 414 */
 415struct uwb_rc *uwb_rc_get_by_grandpa(const struct device *grandpa_dev)
 416{
 417        struct device *dev;
 418        struct uwb_rc *rc = NULL;
 419
 420        dev = class_find_device(&uwb_rc_class, NULL, (void *)grandpa_dev,
 421                                find_rc_grandpa);
 422        if (dev)
 423                rc = dev_get_drvdata(dev);
 424        return rc;
 425}
 426EXPORT_SYMBOL_GPL(uwb_rc_get_by_grandpa);
 427
 428/**
 429 * Find a radio controller by device address
 430 *
 431 * @returns the pointer to the radio controller, properly referenced
 432 */
 433static int find_rc_dev(struct device *dev, void *data)
 434{
 435        struct uwb_dev_addr *addr = data;
 436        struct uwb_rc *rc = dev_get_drvdata(dev);
 437
 438        if (rc == NULL) {
 439                WARN_ON(1);
 440                return 0;
 441        }
 442        if (!uwb_dev_addr_cmp(&rc->uwb_dev.dev_addr, addr)) {
 443                rc = uwb_rc_get(rc);
 444                return 1;
 445        }
 446        return 0;
 447}
 448
 449struct uwb_rc *uwb_rc_get_by_dev(const struct uwb_dev_addr *addr)
 450{
 451        struct device *dev;
 452        struct uwb_rc *rc = NULL;
 453
 454        dev = class_find_device(&uwb_rc_class, NULL, (void *)addr,
 455                                find_rc_dev);
 456        if (dev)
 457                rc = dev_get_drvdata(dev);
 458
 459        return rc;
 460}
 461EXPORT_SYMBOL_GPL(uwb_rc_get_by_dev);
 462
 463/**
 464 * Drop a reference on a radio controller
 465 *
 466 * This is the version that should be done by entities external to the
 467 * UWB Radio Control stack (ie: clients of the API).
 468 */
 469void uwb_rc_put(struct uwb_rc *rc)
 470{
 471        __uwb_rc_put(rc);
 472}
 473EXPORT_SYMBOL_GPL(uwb_rc_put);
 474