linux/drivers/uwb/est.c
<<
>>
Prefs
   1/*
   2 * Ultra Wide Band Radio Control
   3 * Event Size Tables management
   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 * Infrastructure, code and data tables for guessing the size of
  26 * events received on the notification endpoints of UWB radio
  27 * controllers.
  28 *
  29 * You define a table of events and for each, its size and how to get
  30 * the extra size.
  31 *
  32 * ENTRY POINTS:
  33 *
  34 * uwb_est_{init/destroy}(): To initialize/release the EST subsystem.
  35 *
  36 * uwb_est_[u]register(): To un/register event size tables
  37 *   uwb_est_grow()
  38 *
  39 * uwb_est_find_size(): Get the size of an event
  40 *   uwb_est_get_size()
  41 */
  42#include <linux/spinlock.h>
  43
  44#include "uwb-internal.h"
  45
  46struct uwb_est {
  47        u16 type_event_high;
  48        u16 vendor, product;
  49        u8 entries;
  50        const struct uwb_est_entry *entry;
  51};
  52
  53static struct uwb_est *uwb_est;
  54static u8 uwb_est_size;
  55static u8 uwb_est_used;
  56static DEFINE_RWLOCK(uwb_est_lock);
  57
  58/**
  59 * WUSB Standard Event Size Table, HWA-RC interface
  60 *
  61 * Sizes for events and notifications type 0 (general), high nibble 0.
  62 */
  63static
  64struct uwb_est_entry uwb_est_00_00xx[] = {
  65        [UWB_RC_EVT_IE_RCV] = {
  66                .size = sizeof(struct uwb_rc_evt_ie_rcv),
  67                .offset = 1 + offsetof(struct uwb_rc_evt_ie_rcv, wIELength),
  68        },
  69        [UWB_RC_EVT_BEACON] = {
  70                .size = sizeof(struct uwb_rc_evt_beacon),
  71                .offset = 1 + offsetof(struct uwb_rc_evt_beacon, wBeaconInfoLength),
  72        },
  73        [UWB_RC_EVT_BEACON_SIZE] = {
  74                .size = sizeof(struct uwb_rc_evt_beacon_size),
  75        },
  76        [UWB_RC_EVT_BPOIE_CHANGE] = {
  77                .size = sizeof(struct uwb_rc_evt_bpoie_change),
  78                .offset = 1 + offsetof(struct uwb_rc_evt_bpoie_change,
  79                                       wBPOIELength),
  80        },
  81        [UWB_RC_EVT_BP_SLOT_CHANGE] = {
  82                .size = sizeof(struct uwb_rc_evt_bp_slot_change),
  83        },
  84        [UWB_RC_EVT_BP_SWITCH_IE_RCV] = {
  85                .size = sizeof(struct uwb_rc_evt_bp_switch_ie_rcv),
  86                .offset = 1 + offsetof(struct uwb_rc_evt_bp_switch_ie_rcv, wIELength),
  87        },
  88        [UWB_RC_EVT_DEV_ADDR_CONFLICT] = {
  89                .size = sizeof(struct uwb_rc_evt_dev_addr_conflict),
  90        },
  91        [UWB_RC_EVT_DRP_AVAIL] = {
  92                .size = sizeof(struct uwb_rc_evt_drp_avail)
  93        },
  94        [UWB_RC_EVT_DRP] = {
  95                .size = sizeof(struct uwb_rc_evt_drp),
  96                .offset = 1 + offsetof(struct uwb_rc_evt_drp, ie_length),
  97        },
  98        [UWB_RC_EVT_BP_SWITCH_STATUS] = {
  99                .size = sizeof(struct uwb_rc_evt_bp_switch_status),
 100        },
 101        [UWB_RC_EVT_CMD_FRAME_RCV] = {
 102                .size = sizeof(struct uwb_rc_evt_cmd_frame_rcv),
 103                .offset = 1 + offsetof(struct uwb_rc_evt_cmd_frame_rcv, dataLength),
 104        },
 105        [UWB_RC_EVT_CHANNEL_CHANGE_IE_RCV] = {
 106                .size = sizeof(struct uwb_rc_evt_channel_change_ie_rcv),
 107                .offset = 1 + offsetof(struct uwb_rc_evt_channel_change_ie_rcv, wIELength),
 108        },
 109        [UWB_RC_CMD_CHANNEL_CHANGE] = {
 110                .size = sizeof(struct uwb_rc_evt_confirm),
 111        },
 112        [UWB_RC_CMD_DEV_ADDR_MGMT] = {
 113                .size = sizeof(struct uwb_rc_evt_dev_addr_mgmt) },
 114        [UWB_RC_CMD_GET_IE] = {
 115                .size = sizeof(struct uwb_rc_evt_get_ie),
 116                .offset = 1 + offsetof(struct uwb_rc_evt_get_ie, wIELength),
 117        },
 118        [UWB_RC_CMD_RESET] = {
 119                .size = sizeof(struct uwb_rc_evt_confirm),
 120        },
 121        [UWB_RC_CMD_SCAN] = {
 122                .size = sizeof(struct uwb_rc_evt_confirm),
 123        },
 124        [UWB_RC_CMD_SET_BEACON_FILTER] = {
 125                .size = sizeof(struct uwb_rc_evt_confirm),
 126        },
 127        [UWB_RC_CMD_SET_DRP_IE] = {
 128                .size = sizeof(struct uwb_rc_evt_set_drp_ie),
 129        },
 130        [UWB_RC_CMD_SET_IE] = {
 131                .size = sizeof(struct uwb_rc_evt_set_ie),
 132        },
 133        [UWB_RC_CMD_SET_NOTIFICATION_FILTER] = {
 134                .size = sizeof(struct uwb_rc_evt_confirm),
 135        },
 136        [UWB_RC_CMD_SET_TX_POWER] = {
 137                .size = sizeof(struct uwb_rc_evt_confirm),
 138        },
 139        [UWB_RC_CMD_SLEEP] = {
 140                .size = sizeof(struct uwb_rc_evt_confirm),
 141        },
 142        [UWB_RC_CMD_START_BEACON] = {
 143                .size = sizeof(struct uwb_rc_evt_confirm),
 144        },
 145        [UWB_RC_CMD_STOP_BEACON] = {
 146                .size = sizeof(struct uwb_rc_evt_confirm),
 147        },
 148        [UWB_RC_CMD_BP_MERGE] = {
 149                .size = sizeof(struct uwb_rc_evt_confirm),
 150        },
 151        [UWB_RC_CMD_SEND_COMMAND_FRAME] = {
 152                .size = sizeof(struct uwb_rc_evt_confirm),
 153        },
 154        [UWB_RC_CMD_SET_ASIE_NOTIF] = {
 155                .size = sizeof(struct uwb_rc_evt_confirm),
 156        },
 157};
 158
 159static
 160struct uwb_est_entry uwb_est_01_00xx[] = {
 161        [UWB_RC_DAA_ENERGY_DETECTED] = {
 162                .size = sizeof(struct uwb_rc_evt_daa_energy_detected),
 163        },
 164        [UWB_RC_SET_DAA_ENERGY_MASK] = {
 165                .size = sizeof(struct uwb_rc_evt_set_daa_energy_mask),
 166        },
 167        [UWB_RC_SET_NOTIFICATION_FILTER_EX] = {
 168                .size = sizeof(struct uwb_rc_evt_set_notification_filter_ex),
 169        },
 170};
 171
 172/**
 173 * Initialize the EST subsystem
 174 *
 175 * Register the standard tables also.
 176 *
 177 * FIXME: tag init
 178 */
 179int uwb_est_create(void)
 180{
 181        int result;
 182
 183        uwb_est_size = 2;
 184        uwb_est_used = 0;
 185        uwb_est = kzalloc(uwb_est_size * sizeof(uwb_est[0]), GFP_KERNEL);
 186        if (uwb_est == NULL)
 187                return -ENOMEM;
 188
 189        result = uwb_est_register(UWB_RC_CET_GENERAL, 0, 0xffff, 0xffff,
 190                                  uwb_est_00_00xx, ARRAY_SIZE(uwb_est_00_00xx));
 191        if (result < 0)
 192                goto out;
 193        result = uwb_est_register(UWB_RC_CET_EX_TYPE_1, 0, 0xffff, 0xffff,
 194                                  uwb_est_01_00xx, ARRAY_SIZE(uwb_est_01_00xx));
 195out:
 196        return result;
 197}
 198
 199
 200/** Clean it up */
 201void uwb_est_destroy(void)
 202{
 203        kfree(uwb_est);
 204        uwb_est = NULL;
 205        uwb_est_size = uwb_est_used = 0;
 206}
 207
 208
 209/**
 210 * Double the capacity of the EST table
 211 *
 212 * @returns 0 if ok, < 0 errno no error.
 213 */
 214static
 215int uwb_est_grow(void)
 216{
 217        size_t actual_size = uwb_est_size * sizeof(uwb_est[0]);
 218        void *new = kmalloc(2 * actual_size, GFP_ATOMIC);
 219        if (new == NULL)
 220                return -ENOMEM;
 221        memcpy(new, uwb_est, actual_size);
 222        memset(new + actual_size, 0, actual_size);
 223        kfree(uwb_est);
 224        uwb_est = new;
 225        uwb_est_size *= 2;
 226        return 0;
 227}
 228
 229
 230/**
 231 * Register an event size table
 232 *
 233 * Makes room for it if the table is full, and then inserts  it in the
 234 * right position (entries are sorted by type, event_high, vendor and
 235 * then product).
 236 *
 237 * @vendor:  vendor code for matching against the device (0x0000 and
 238 *           0xffff mean any); use 0x0000 to force all to match without
 239 *           checking possible vendor specific ones, 0xfffff to match
 240 *           after checking vendor specific ones.
 241 *
 242 * @product: product code from that vendor; same matching rules, use
 243 *           0x0000 for not allowing vendor specific matches, 0xffff
 244 *           for allowing.
 245 *
 246 * This arragement just makes the tables sort differenty. Because the
 247 * table is sorted by growing type-event_high-vendor-product, a zero
 248 * vendor will match before than a 0x456a vendor, that will match
 249 * before a 0xfffff vendor.
 250 *
 251 * @returns 0 if ok, < 0 errno on error (-ENOENT if not found).
 252 */
 253/* FIXME: add bus type to vendor/product code */
 254int uwb_est_register(u8 type, u8 event_high, u16 vendor, u16 product,
 255                     const struct uwb_est_entry *entry, size_t entries)
 256{
 257        unsigned long flags;
 258        unsigned itr;
 259        u16 type_event_high;
 260        int result = 0;
 261
 262        write_lock_irqsave(&uwb_est_lock, flags);
 263        if (uwb_est_used == uwb_est_size) {
 264                result = uwb_est_grow();
 265                if (result < 0)
 266                        goto out;
 267        }
 268        /* Find the right spot to insert it in */
 269        type_event_high = type << 8 | event_high;
 270        for (itr = 0; itr < uwb_est_used; itr++)
 271                if (uwb_est[itr].type_event_high < type
 272                    && uwb_est[itr].vendor < vendor
 273                    && uwb_est[itr].product < product)
 274                        break;
 275
 276        /* Shift others to make room for the new one? */
 277        if (itr < uwb_est_used)
 278                memmove(&uwb_est[itr+1], &uwb_est[itr], uwb_est_used - itr);
 279        uwb_est[itr].type_event_high = type << 8 | event_high;
 280        uwb_est[itr].vendor = vendor;
 281        uwb_est[itr].product = product;
 282        uwb_est[itr].entry = entry;
 283        uwb_est[itr].entries = entries;
 284        uwb_est_used++;
 285out:
 286        write_unlock_irqrestore(&uwb_est_lock, flags);
 287        return result;
 288}
 289EXPORT_SYMBOL_GPL(uwb_est_register);
 290
 291
 292/**
 293 * Unregister an event size table
 294 *
 295 * This just removes the specified entry and moves the ones after it
 296 * to fill in the gap. This is needed to keep the list sorted; no
 297 * reallocation is done to reduce the size of the table.
 298 *
 299 * We unregister by all the data we used to register instead of by
 300 * pointer to the @entry array because we might have used the same
 301 * table for a bunch of IDs (for example).
 302 *
 303 * @returns 0 if ok, < 0 errno on error (-ENOENT if not found).
 304 */
 305int uwb_est_unregister(u8 type, u8 event_high, u16 vendor, u16 product,
 306                       const struct uwb_est_entry *entry, size_t entries)
 307{
 308        unsigned long flags;
 309        unsigned itr;
 310        struct uwb_est est_cmp = {
 311                .type_event_high = type << 8 | event_high,
 312                .vendor = vendor,
 313                .product = product,
 314                .entry = entry,
 315                .entries = entries
 316        };
 317        write_lock_irqsave(&uwb_est_lock, flags);
 318        for (itr = 0; itr < uwb_est_used; itr++)
 319                if (!memcmp(&uwb_est[itr], &est_cmp, sizeof(est_cmp)))
 320                        goto found;
 321        write_unlock_irqrestore(&uwb_est_lock, flags);
 322        return -ENOENT;
 323
 324found:
 325        if (itr < uwb_est_used - 1)     /* Not last one? move ones above */
 326                memmove(&uwb_est[itr], &uwb_est[itr+1], uwb_est_used - itr - 1);
 327        uwb_est_used--;
 328        write_unlock_irqrestore(&uwb_est_lock, flags);
 329        return 0;
 330}
 331EXPORT_SYMBOL_GPL(uwb_est_unregister);
 332
 333
 334/**
 335 * Get the size of an event from a table
 336 *
 337 * @rceb: pointer to the buffer with the event
 338 * @rceb_size: size of the area pointed to by @rceb in bytes.
 339 * @returns: > 0      Size of the event
 340 *           -ENOSPC  An area big enough was not provided to look
 341 *                    ahead into the event's guts and guess the size.
 342 *           -EINVAL  Unknown event code (wEvent).
 343 *
 344 * This will look at the received RCEB and guess what is the total
 345 * size. For variable sized events, it will look further ahead into
 346 * their length field to see how much data should be read.
 347 *
 348 * Note this size is *not* final--the neh (Notification/Event Handle)
 349 * might specificy an extra size to add.
 350 */
 351static
 352ssize_t uwb_est_get_size(struct uwb_rc *uwb_rc, struct uwb_est *est,
 353                         u8 event_low, const struct uwb_rceb *rceb,
 354                         size_t rceb_size)
 355{
 356        unsigned offset;
 357        ssize_t size;
 358        struct device *dev = &uwb_rc->uwb_dev.dev;
 359        const struct uwb_est_entry *entry;
 360
 361        size = -ENOENT;
 362        if (event_low >= est->entries) {        /* in range? */
 363                dev_err(dev, "EST %p 0x%04x/%04x/%04x[%u]: event %u out of range\n",
 364                        est, est->type_event_high, est->vendor, est->product,
 365                        est->entries, event_low);
 366                goto out;
 367        }
 368        size = -ENOENT;
 369        entry = &est->entry[event_low];
 370        if (entry->size == 0 && entry->offset == 0) {   /* unknown? */
 371                dev_err(dev, "EST %p 0x%04x/%04x/%04x[%u]: event %u unknown\n",
 372                        est, est->type_event_high, est->vendor, est->product,
 373                        est->entries, event_low);
 374                goto out;
 375        }
 376        offset = entry->offset; /* extra fries with that? */
 377        if (offset == 0)
 378                size = entry->size;
 379        else {
 380                /* Ops, got an extra size field at 'offset'--read it */
 381                const void *ptr = rceb;
 382                size_t type_size = 0;
 383                offset--;
 384                size = -ENOSPC;                 /* enough data for more? */
 385                switch (entry->type) {
 386                case UWB_EST_16:  type_size = sizeof(__le16); break;
 387                case UWB_EST_8:   type_size = sizeof(u8);     break;
 388                default:         BUG();
 389                }
 390                if (offset + type_size > rceb_size) {
 391                        dev_err(dev, "EST %p 0x%04x/%04x/%04x[%u]: "
 392                                "not enough data to read extra size\n",
 393                                est, est->type_event_high, est->vendor,
 394                                est->product, est->entries);
 395                        goto out;
 396                }
 397                size = entry->size;
 398                ptr += offset;
 399                switch (entry->type) {
 400                case UWB_EST_16:  size += le16_to_cpu(*(__le16 *)ptr); break;
 401                case UWB_EST_8:   size += *(u8 *)ptr;                  break;
 402                default:         BUG();
 403                }
 404        }
 405out:
 406        return size;
 407}
 408
 409
 410/**
 411 * Guesses the size of a WA event
 412 *
 413 * @rceb: pointer to the buffer with the event
 414 * @rceb_size: size of the area pointed to by @rceb in bytes.
 415 * @returns: > 0      Size of the event
 416 *           -ENOSPC  An area big enough was not provided to look
 417 *                    ahead into the event's guts and guess the size.
 418 *           -EINVAL  Unknown event code (wEvent).
 419 *
 420 * This will look at the received RCEB and guess what is the total
 421 * size by checking all the tables registered with
 422 * uwb_est_register(). For variable sized events, it will look further
 423 * ahead into their length field to see how much data should be read.
 424 *
 425 * Note this size is *not* final--the neh (Notification/Event Handle)
 426 * might specificy an extra size to add or replace.
 427 */
 428ssize_t uwb_est_find_size(struct uwb_rc *rc, const struct uwb_rceb *rceb,
 429                          size_t rceb_size)
 430{
 431        /* FIXME: add vendor/product data */
 432        ssize_t size;
 433        struct device *dev = &rc->uwb_dev.dev;
 434        unsigned long flags;
 435        unsigned itr;
 436        u16 type_event_high, event;
 437        u8 *ptr = (u8 *) rceb;
 438
 439        read_lock_irqsave(&uwb_est_lock, flags);
 440        size = -ENOSPC;
 441        if (rceb_size < sizeof(*rceb))
 442                goto out;
 443        event = le16_to_cpu(rceb->wEvent);
 444        type_event_high = rceb->bEventType << 8 | (event & 0xff00) >> 8;
 445        for (itr = 0; itr < uwb_est_used; itr++) {
 446                if (uwb_est[itr].type_event_high != type_event_high)
 447                        continue;
 448                size = uwb_est_get_size(rc, &uwb_est[itr],
 449                                        event & 0x00ff, rceb, rceb_size);
 450                /* try more tables that might handle the same type */
 451                if (size != -ENOENT)
 452                        goto out;
 453        }
 454        dev_dbg(dev, "event 0x%02x/%04x/%02x: no handlers available; "
 455                "RCEB %02x %02x %02x %02x\n",
 456                (unsigned) rceb->bEventType,
 457                (unsigned) le16_to_cpu(rceb->wEvent),
 458                (unsigned) rceb->bEventContext,
 459                ptr[0], ptr[1], ptr[2], ptr[3]);
 460        size = -ENOENT;
 461out:
 462        read_unlock_irqrestore(&uwb_est_lock, flags);
 463        return size;
 464}
 465EXPORT_SYMBOL_GPL(uwb_est_find_size);
 466