linux/drivers/usb/host/hwa-hc.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Host Wire Adapter:
   4 * Driver glue, HWA-specific functions, bridges to WAHC and WUSBHC
   5 *
   6 * Copyright (C) 2005-2006 Intel Corporation
   7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
   8 *
   9 * The HWA driver is a simple layer that forwards requests to the WAHC
  10 * (Wire Adater Host Controller) or WUSBHC (Wireless USB Host
  11 * Controller) layers.
  12 *
  13 * Host Wire Adapter is the 'WUSB 1.0 standard' name for Wireless-USB
  14 * Host Controller that is connected to your system via USB (a USB
  15 * dongle that implements a USB host...). There is also a Device Wired
  16 * Adaptor, DWA (Wireless USB hub) that uses the same mechanism for
  17 * transferring data (it is after all a USB host connected via
  18 * Wireless USB), we have a common layer called Wire Adapter Host
  19 * Controller that does all the hard work. The WUSBHC (Wireless USB
  20 * Host Controller) is the part common to WUSB Host Controllers, the
  21 * HWA and the PCI-based one, that is implemented following the WHCI
  22 * spec. All these layers are implemented in ../wusbcore.
  23 *
  24 * The main functions are hwahc_op_urb_{en,de}queue(), that pass the
  25 * job of converting a URB to a Wire Adapter
  26 *
  27 * Entry points:
  28 *
  29 *   hwahc_driver_*()   Driver initialization, registration and
  30 *                      teardown.
  31 *
  32 *   hwahc_probe()      New device came up, create an instance for
  33 *                      it [from device enumeration].
  34 *
  35 *   hwahc_disconnect() Remove device instance [from device
  36 *                      enumeration].
  37 *
  38 *   [__]hwahc_op_*()   Host-Wire-Adaptor specific functions for
  39 *                      starting/stopping/etc (some might be made also
  40 *                      DWA).
  41 */
  42#include <linux/kernel.h>
  43#include <linux/slab.h>
  44#include <linux/module.h>
  45#include <linux/workqueue.h>
  46#include <linux/wait.h>
  47#include <linux/completion.h>
  48#include "../wusbcore/wa-hc.h"
  49#include "../wusbcore/wusbhc.h"
  50
  51struct hwahc {
  52        struct wusbhc wusbhc;   /* has to be 1st */
  53        struct wahc wa;
  54};
  55
  56/*
  57 * FIXME should be wusbhc
  58 *
  59 * NOTE: we need to cache the Cluster ID because later...there is no
  60 *       way to get it :)
  61 */
  62static int __hwahc_set_cluster_id(struct hwahc *hwahc, u8 cluster_id)
  63{
  64        int result;
  65        struct wusbhc *wusbhc = &hwahc->wusbhc;
  66        struct wahc *wa = &hwahc->wa;
  67        struct device *dev = &wa->usb_iface->dev;
  68
  69        result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
  70                        WUSB_REQ_SET_CLUSTER_ID,
  71                        USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  72                        cluster_id,
  73                        wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
  74                        NULL, 0, USB_CTRL_SET_TIMEOUT);
  75        if (result < 0)
  76                dev_err(dev, "Cannot set WUSB Cluster ID to 0x%02x: %d\n",
  77                        cluster_id, result);
  78        else
  79                wusbhc->cluster_id = cluster_id;
  80        dev_info(dev, "Wireless USB Cluster ID set to 0x%02x\n", cluster_id);
  81        return result;
  82}
  83
  84static int __hwahc_op_set_num_dnts(struct wusbhc *wusbhc, u8 interval, u8 slots)
  85{
  86        struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
  87        struct wahc *wa = &hwahc->wa;
  88
  89        return usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
  90                        WUSB_REQ_SET_NUM_DNTS,
  91                        USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  92                        interval << 8 | slots,
  93                        wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
  94                        NULL, 0, USB_CTRL_SET_TIMEOUT);
  95}
  96
  97/*
  98 * Reset a WUSB host controller and wait for it to complete doing it.
  99 *
 100 * @usb_hcd:    Pointer to WUSB Host Controller instance.
 101 *
 102 */
 103static int hwahc_op_reset(struct usb_hcd *usb_hcd)
 104{
 105        int result;
 106        struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
 107        struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
 108        struct device *dev = &hwahc->wa.usb_iface->dev;
 109
 110        mutex_lock(&wusbhc->mutex);
 111        wa_nep_disarm(&hwahc->wa);
 112        result = __wa_set_feature(&hwahc->wa, WA_RESET);
 113        if (result < 0) {
 114                dev_err(dev, "error commanding HC to reset: %d\n", result);
 115                goto error_unlock;
 116        }
 117        result = __wa_wait_status(&hwahc->wa, WA_STATUS_RESETTING, 0);
 118        if (result < 0) {
 119                dev_err(dev, "error waiting for HC to reset: %d\n", result);
 120                goto error_unlock;
 121        }
 122error_unlock:
 123        mutex_unlock(&wusbhc->mutex);
 124        return result;
 125}
 126
 127/*
 128 * FIXME: break this function up
 129 */
 130static int hwahc_op_start(struct usb_hcd *usb_hcd)
 131{
 132        u8 addr;
 133        int result;
 134        struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
 135        struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
 136
 137        result = -ENOSPC;
 138        mutex_lock(&wusbhc->mutex);
 139        addr = wusb_cluster_id_get();
 140        if (addr == 0)
 141                goto error_cluster_id_get;
 142        result = __hwahc_set_cluster_id(hwahc, addr);
 143        if (result < 0)
 144                goto error_set_cluster_id;
 145
 146        usb_hcd->uses_new_polling = 1;
 147        set_bit(HCD_FLAG_POLL_RH, &usb_hcd->flags);
 148        usb_hcd->state = HC_STATE_RUNNING;
 149
 150        /*
 151         * prevent USB core from suspending the root hub since
 152         * bus_suspend and bus_resume are not yet supported.
 153         */
 154        pm_runtime_get_noresume(&usb_hcd->self.root_hub->dev);
 155
 156        result = 0;
 157out:
 158        mutex_unlock(&wusbhc->mutex);
 159        return result;
 160
 161error_set_cluster_id:
 162        wusb_cluster_id_put(addr);
 163error_cluster_id_get:
 164        goto out;
 165
 166}
 167
 168/*
 169 * No need to abort pipes, as when this is called, all the children
 170 * has been disconnected and that has done it [through
 171 * usb_disable_interface() -> usb_disable_endpoint() ->
 172 * hwahc_op_ep_disable() - >rpipe_ep_disable()].
 173 */
 174static void hwahc_op_stop(struct usb_hcd *usb_hcd)
 175{
 176        struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
 177
 178        mutex_lock(&wusbhc->mutex);
 179        wusb_cluster_id_put(wusbhc->cluster_id);
 180        mutex_unlock(&wusbhc->mutex);
 181}
 182
 183static int hwahc_op_get_frame_number(struct usb_hcd *usb_hcd)
 184{
 185        struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
 186        struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
 187        struct wahc *wa = &hwahc->wa;
 188
 189        /*
 190         * We cannot query the HWA for the WUSB time since that requires sending
 191         * a synchronous URB and this function can be called in_interrupt.
 192         * Instead, query the USB frame number for our parent and use that.
 193         */
 194        return usb_get_current_frame_number(wa->usb_dev);
 195}
 196
 197static int hwahc_op_urb_enqueue(struct usb_hcd *usb_hcd, struct urb *urb,
 198                                gfp_t gfp)
 199{
 200        struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
 201        struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
 202
 203        return wa_urb_enqueue(&hwahc->wa, urb->ep, urb, gfp);
 204}
 205
 206static int hwahc_op_urb_dequeue(struct usb_hcd *usb_hcd, struct urb *urb,
 207                                int status)
 208{
 209        struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
 210        struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
 211
 212        return wa_urb_dequeue(&hwahc->wa, urb, status);
 213}
 214
 215/*
 216 * Release resources allocated for an endpoint
 217 *
 218 * If there is an associated rpipe to this endpoint, go ahead and put it.
 219 */
 220static void hwahc_op_endpoint_disable(struct usb_hcd *usb_hcd,
 221                                      struct usb_host_endpoint *ep)
 222{
 223        struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
 224        struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
 225
 226        rpipe_ep_disable(&hwahc->wa, ep);
 227}
 228
 229static int __hwahc_op_wusbhc_start(struct wusbhc *wusbhc)
 230{
 231        int result;
 232        struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
 233        struct device *dev = &hwahc->wa.usb_iface->dev;
 234
 235        result = __wa_set_feature(&hwahc->wa, WA_ENABLE);
 236        if (result < 0) {
 237                dev_err(dev, "error commanding HC to start: %d\n", result);
 238                goto error_stop;
 239        }
 240        result = __wa_wait_status(&hwahc->wa, WA_ENABLE, WA_ENABLE);
 241        if (result < 0) {
 242                dev_err(dev, "error waiting for HC to start: %d\n", result);
 243                goto error_stop;
 244        }
 245        result = wa_nep_arm(&hwahc->wa, GFP_KERNEL);
 246        if (result < 0) {
 247                dev_err(dev, "cannot listen to notifications: %d\n", result);
 248                goto error_stop;
 249        }
 250        /*
 251         * If WUSB_QUIRK_ALEREON_HWA_DISABLE_XFER_NOTIFICATIONS is set,
 252         *  disable transfer notifications.
 253         */
 254        if (hwahc->wa.quirks &
 255                WUSB_QUIRK_ALEREON_HWA_DISABLE_XFER_NOTIFICATIONS) {
 256                struct usb_host_interface *cur_altsetting =
 257                        hwahc->wa.usb_iface->cur_altsetting;
 258
 259                result = usb_control_msg(hwahc->wa.usb_dev,
 260                                usb_sndctrlpipe(hwahc->wa.usb_dev, 0),
 261                                WA_REQ_ALEREON_DISABLE_XFER_NOTIFICATIONS,
 262                                USB_DIR_OUT | USB_TYPE_VENDOR |
 263                                        USB_RECIP_INTERFACE,
 264                                WA_REQ_ALEREON_FEATURE_SET,
 265                                cur_altsetting->desc.bInterfaceNumber,
 266                                NULL, 0,
 267                                USB_CTRL_SET_TIMEOUT);
 268                /*
 269                 * If we successfully sent the control message, start DTI here
 270                 * because no transfer notifications will be received which is
 271                 * where DTI is normally started.
 272                 */
 273                if (result == 0)
 274                        result = wa_dti_start(&hwahc->wa);
 275                else
 276                        result = 0;     /* OK.  Continue normally. */
 277
 278                if (result < 0) {
 279                        dev_err(dev, "cannot start DTI: %d\n", result);
 280                        goto error_dti_start;
 281                }
 282        }
 283
 284        return result;
 285
 286error_dti_start:
 287        wa_nep_disarm(&hwahc->wa);
 288error_stop:
 289        __wa_clear_feature(&hwahc->wa, WA_ENABLE);
 290        return result;
 291}
 292
 293static void __hwahc_op_wusbhc_stop(struct wusbhc *wusbhc, int delay)
 294{
 295        struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
 296        struct wahc *wa = &hwahc->wa;
 297        u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
 298        int ret;
 299
 300        ret = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
 301                              WUSB_REQ_CHAN_STOP,
 302                              USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
 303                              delay * 1000,
 304                              iface_no,
 305                              NULL, 0, USB_CTRL_SET_TIMEOUT);
 306        if (ret == 0)
 307                msleep(delay);
 308
 309        wa_nep_disarm(&hwahc->wa);
 310        __wa_stop(&hwahc->wa);
 311}
 312
 313/*
 314 * Set the UWB MAS allocation for the WUSB cluster
 315 *
 316 * @stream_index: stream to use (-1 for cancelling the allocation)
 317 * @mas: mas bitmap to use
 318 */
 319static int __hwahc_op_bwa_set(struct wusbhc *wusbhc, s8 stream_index,
 320                              const struct uwb_mas_bm *mas)
 321{
 322        int result;
 323        struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
 324        struct wahc *wa = &hwahc->wa;
 325        struct device *dev = &wa->usb_iface->dev;
 326        u8 mas_le[UWB_NUM_MAS/8];
 327
 328        /* Set the stream index */
 329        result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
 330                        WUSB_REQ_SET_STREAM_IDX,
 331                        USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
 332                        stream_index,
 333                        wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
 334                        NULL, 0, USB_CTRL_SET_TIMEOUT);
 335        if (result < 0) {
 336                dev_err(dev, "Cannot set WUSB stream index: %d\n", result);
 337                goto out;
 338        }
 339        uwb_mas_bm_copy_le(mas_le, mas);
 340        /* Set the MAS allocation */
 341        result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
 342                        WUSB_REQ_SET_WUSB_MAS,
 343                        USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
 344                        0, wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
 345                        mas_le, 32, USB_CTRL_SET_TIMEOUT);
 346        if (result < 0)
 347                dev_err(dev, "Cannot set WUSB MAS allocation: %d\n", result);
 348out:
 349        return result;
 350}
 351
 352/*
 353 * Add an IE to the host's MMC
 354 *
 355 * @interval:    See WUSB1.0[8.5.3.1]
 356 * @repeat_cnt:  See WUSB1.0[8.5.3.1]
 357 * @handle:      See WUSB1.0[8.5.3.1]
 358 * @wuie:        Pointer to the header of the WUSB IE data to add.
 359 *               MUST BE allocated in a kmalloc buffer (no stack or
 360 *               vmalloc).
 361 *
 362 * NOTE: the format of the WUSB IEs for MMCs are different to the
 363 *       normal MBOA MAC IEs (IE Id + Length in MBOA MAC vs. Length +
 364 *       Id in WUSB IEs). Standards...you gotta love'em.
 365 */
 366static int __hwahc_op_mmcie_add(struct wusbhc *wusbhc, u8 interval,
 367                                u8 repeat_cnt, u8 handle,
 368                                struct wuie_hdr *wuie)
 369{
 370        struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
 371        struct wahc *wa = &hwahc->wa;
 372        u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
 373
 374        return usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
 375                        WUSB_REQ_ADD_MMC_IE,
 376                        USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
 377                        interval << 8 | repeat_cnt,
 378                        handle << 8 | iface_no,
 379                        wuie, wuie->bLength, USB_CTRL_SET_TIMEOUT);
 380}
 381
 382/*
 383 * Remove an IE to the host's MMC
 384 *
 385 * @handle:      See WUSB1.0[8.5.3.1]
 386 */
 387static int __hwahc_op_mmcie_rm(struct wusbhc *wusbhc, u8 handle)
 388{
 389        struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
 390        struct wahc *wa = &hwahc->wa;
 391        u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
 392        return usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
 393                        WUSB_REQ_REMOVE_MMC_IE,
 394                        USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
 395                        0, handle << 8 | iface_no,
 396                        NULL, 0, USB_CTRL_SET_TIMEOUT);
 397}
 398
 399/*
 400 * Update device information for a given fake port
 401 *
 402 * @port_idx: Fake port to which device is connected (wusbhc index, not
 403 *            USB port number).
 404 */
 405static int __hwahc_op_dev_info_set(struct wusbhc *wusbhc,
 406                                   struct wusb_dev *wusb_dev)
 407{
 408        struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
 409        struct wahc *wa = &hwahc->wa;
 410        u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
 411        struct hwa_dev_info *dev_info;
 412        int ret;
 413
 414        /* fill out the Device Info buffer and send it */
 415        dev_info = kzalloc(sizeof(struct hwa_dev_info), GFP_KERNEL);
 416        if (!dev_info)
 417                return -ENOMEM;
 418        uwb_mas_bm_copy_le(dev_info->bmDeviceAvailability,
 419                           &wusb_dev->availability);
 420        dev_info->bDeviceAddress = wusb_dev->addr;
 421
 422        /*
 423         * If the descriptors haven't been read yet, use a default PHY
 424         * rate of 53.3 Mbit/s only.  The correct value will be used
 425         * when this will be called again as part of the
 426         * authentication process (which occurs after the descriptors
 427         * have been read).
 428         */
 429        if (wusb_dev->wusb_cap_descr)
 430                dev_info->wPHYRates = wusb_dev->wusb_cap_descr->wPHYRates;
 431        else
 432                dev_info->wPHYRates = cpu_to_le16(USB_WIRELESS_PHY_53);
 433
 434        ret = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
 435                        WUSB_REQ_SET_DEV_INFO,
 436                        USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
 437                        0, wusb_dev->port_idx << 8 | iface_no,
 438                        dev_info, sizeof(struct hwa_dev_info),
 439                        USB_CTRL_SET_TIMEOUT);
 440        kfree(dev_info);
 441        return ret;
 442}
 443
 444/*
 445 * Set host's idea of which encryption (and key) method to use when
 446 * talking to ad evice on a given port.
 447 *
 448 * If key is NULL, it means disable encryption for that "virtual port"
 449 * (used when we disconnect).
 450 */
 451static int __hwahc_dev_set_key(struct wusbhc *wusbhc, u8 port_idx, u32 tkid,
 452                               const void *key, size_t key_size,
 453                               u8 key_idx)
 454{
 455        int result = -ENOMEM;
 456        struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
 457        struct wahc *wa = &hwahc->wa;
 458        u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
 459        struct usb_key_descriptor *keyd;
 460        size_t keyd_len;
 461
 462        keyd_len = sizeof(*keyd) + key_size;
 463        keyd = kzalloc(keyd_len, GFP_KERNEL);
 464        if (keyd == NULL)
 465                return -ENOMEM;
 466
 467        keyd->bLength = keyd_len;
 468        keyd->bDescriptorType = USB_DT_KEY;
 469        keyd->tTKID[0] = (tkid >>  0) & 0xff;
 470        keyd->tTKID[1] = (tkid >>  8) & 0xff;
 471        keyd->tTKID[2] = (tkid >> 16) & 0xff;
 472        memcpy(keyd->bKeyData, key, key_size);
 473
 474        result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
 475                        USB_REQ_SET_DESCRIPTOR,
 476                        USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
 477                        USB_DT_KEY << 8 | key_idx,
 478                        port_idx << 8 | iface_no,
 479                        keyd, keyd_len, USB_CTRL_SET_TIMEOUT);
 480
 481        kzfree(keyd); /* clear keys etc. */
 482        return result;
 483}
 484
 485/*
 486 * Set host's idea of which encryption (and key) method to use when
 487 * talking to ad evice on a given port.
 488 *
 489 * If key is NULL, it means disable encryption for that "virtual port"
 490 * (used when we disconnect).
 491 */
 492static int __hwahc_op_set_ptk(struct wusbhc *wusbhc, u8 port_idx, u32 tkid,
 493                              const void *key, size_t key_size)
 494{
 495        int result = -ENOMEM;
 496        struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
 497        struct wahc *wa = &hwahc->wa;
 498        u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
 499        u8 encryption_value;
 500
 501        /* Tell the host which key to use to talk to the device */
 502        if (key) {
 503                u8 key_idx = wusb_key_index(0, WUSB_KEY_INDEX_TYPE_PTK,
 504                                            WUSB_KEY_INDEX_ORIGINATOR_HOST);
 505
 506                result = __hwahc_dev_set_key(wusbhc, port_idx, tkid,
 507                                             key, key_size, key_idx);
 508                if (result < 0)
 509                        goto error_set_key;
 510                encryption_value = wusbhc->ccm1_etd->bEncryptionValue;
 511        } else {
 512                /* FIXME: this should come from wusbhc->etd[UNSECURE].value */
 513                encryption_value = 0;
 514        }
 515
 516        /* Set the encryption type for communicating with the device */
 517        result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
 518                        USB_REQ_SET_ENCRYPTION,
 519                        USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
 520                        encryption_value, port_idx << 8 | iface_no,
 521                        NULL, 0, USB_CTRL_SET_TIMEOUT);
 522        if (result < 0)
 523                dev_err(wusbhc->dev, "Can't set host's WUSB encryption for "
 524                        "port index %u to %s (value %d): %d\n", port_idx,
 525                        wusb_et_name(wusbhc->ccm1_etd->bEncryptionType),
 526                        wusbhc->ccm1_etd->bEncryptionValue, result);
 527error_set_key:
 528        return result;
 529}
 530
 531/*
 532 * Set host's GTK key
 533 */
 534static int __hwahc_op_set_gtk(struct wusbhc *wusbhc, u32 tkid,
 535                              const void *key, size_t key_size)
 536{
 537        u8 key_idx = wusb_key_index(0, WUSB_KEY_INDEX_TYPE_GTK,
 538                                    WUSB_KEY_INDEX_ORIGINATOR_HOST);
 539
 540        return __hwahc_dev_set_key(wusbhc, 0, tkid, key, key_size, key_idx);
 541}
 542
 543/*
 544 * Get the Wire Adapter class-specific descriptor
 545 *
 546 * NOTE: this descriptor comes with the big bundled configuration
 547 *       descriptor that includes the interfaces' and endpoints', so
 548 *       we just look for it in the cached copy kept by the USB stack.
 549 *
 550 * NOTE2: We convert LE fields to CPU order.
 551 */
 552static int wa_fill_descr(struct wahc *wa)
 553{
 554        int result;
 555        struct device *dev = &wa->usb_iface->dev;
 556        char *itr;
 557        struct usb_device *usb_dev = wa->usb_dev;
 558        struct usb_descriptor_header *hdr;
 559        struct usb_wa_descriptor *wa_descr;
 560        size_t itr_size, actconfig_idx;
 561
 562        actconfig_idx = (usb_dev->actconfig - usb_dev->config) /
 563                        sizeof(usb_dev->config[0]);
 564        itr = usb_dev->rawdescriptors[actconfig_idx];
 565        itr_size = le16_to_cpu(usb_dev->actconfig->desc.wTotalLength);
 566        while (itr_size >= sizeof(*hdr)) {
 567                hdr = (struct usb_descriptor_header *) itr;
 568                dev_dbg(dev, "Extra device descriptor: "
 569                        "type %02x/%u bytes @ %zu (%zu left)\n",
 570                        hdr->bDescriptorType, hdr->bLength,
 571                        (itr - usb_dev->rawdescriptors[actconfig_idx]),
 572                        itr_size);
 573                if (hdr->bDescriptorType == USB_DT_WIRE_ADAPTER)
 574                        goto found;
 575                itr += hdr->bLength;
 576                itr_size -= hdr->bLength;
 577        }
 578        dev_err(dev, "cannot find Wire Adapter Class descriptor\n");
 579        return -ENODEV;
 580
 581found:
 582        result = -EINVAL;
 583        if (hdr->bLength > itr_size) {  /* is it available? */
 584                dev_err(dev, "incomplete Wire Adapter Class descriptor "
 585                        "(%zu bytes left, %u needed)\n",
 586                        itr_size, hdr->bLength);
 587                goto error;
 588        }
 589        if (hdr->bLength < sizeof(*wa->wa_descr)) {
 590                dev_err(dev, "short Wire Adapter Class descriptor\n");
 591                goto error;
 592        }
 593        wa->wa_descr = wa_descr = (struct usb_wa_descriptor *) hdr;
 594        if (le16_to_cpu(wa_descr->bcdWAVersion) > 0x0100)
 595                dev_warn(dev, "Wire Adapter v%d.%d newer than groked v1.0\n",
 596                         (le16_to_cpu(wa_descr->bcdWAVersion) & 0xff00) >> 8,
 597                         le16_to_cpu(wa_descr->bcdWAVersion) & 0x00ff);
 598        result = 0;
 599error:
 600        return result;
 601}
 602
 603static const struct hc_driver hwahc_hc_driver = {
 604        .description = "hwa-hcd",
 605        .product_desc = "Wireless USB HWA host controller",
 606        .hcd_priv_size = sizeof(struct hwahc) - sizeof(struct usb_hcd),
 607        .irq = NULL,                    /* FIXME */
 608        .flags = HCD_USB25,
 609        .reset = hwahc_op_reset,
 610        .start = hwahc_op_start,
 611        .stop = hwahc_op_stop,
 612        .get_frame_number = hwahc_op_get_frame_number,
 613        .urb_enqueue = hwahc_op_urb_enqueue,
 614        .urb_dequeue = hwahc_op_urb_dequeue,
 615        .endpoint_disable = hwahc_op_endpoint_disable,
 616
 617        .hub_status_data = wusbhc_rh_status_data,
 618        .hub_control = wusbhc_rh_control,
 619        .start_port_reset = wusbhc_rh_start_port_reset,
 620};
 621
 622static int hwahc_security_create(struct hwahc *hwahc)
 623{
 624        int result;
 625        struct wusbhc *wusbhc = &hwahc->wusbhc;
 626        struct usb_device *usb_dev = hwahc->wa.usb_dev;
 627        struct device *dev = &usb_dev->dev;
 628        struct usb_security_descriptor *secd;
 629        struct usb_encryption_descriptor *etd;
 630        void *itr, *top;
 631        size_t itr_size, needed, bytes;
 632        u8 index;
 633        char buf[64];
 634
 635        /* Find the host's security descriptors in the config descr bundle */
 636        index = (usb_dev->actconfig - usb_dev->config) /
 637                sizeof(usb_dev->config[0]);
 638        itr = usb_dev->rawdescriptors[index];
 639        itr_size = le16_to_cpu(usb_dev->actconfig->desc.wTotalLength);
 640        top = itr + itr_size;
 641        result = __usb_get_extra_descriptor(usb_dev->rawdescriptors[index],
 642                        le16_to_cpu(usb_dev->actconfig->desc.wTotalLength),
 643                        USB_DT_SECURITY, (void **) &secd, sizeof(*secd));
 644        if (result == -1) {
 645                dev_warn(dev, "BUG? WUSB host has no security descriptors\n");
 646                return 0;
 647        }
 648        needed = sizeof(*secd);
 649        if (top - (void *)secd < needed) {
 650                dev_err(dev, "BUG? Not enough data to process security "
 651                        "descriptor header (%zu bytes left vs %zu needed)\n",
 652                        top - (void *) secd, needed);
 653                return 0;
 654        }
 655        needed = le16_to_cpu(secd->wTotalLength);
 656        if (top - (void *)secd < needed) {
 657                dev_err(dev, "BUG? Not enough data to process security "
 658                        "descriptors (%zu bytes left vs %zu needed)\n",
 659                        top - (void *) secd, needed);
 660                return 0;
 661        }
 662        /* Walk over the sec descriptors and store CCM1's on wusbhc */
 663        itr = (void *) secd + sizeof(*secd);
 664        top = (void *) secd + le16_to_cpu(secd->wTotalLength);
 665        index = 0;
 666        bytes = 0;
 667        while (itr < top) {
 668                etd = itr;
 669                if (top - itr < sizeof(*etd)) {
 670                        dev_err(dev, "BUG: bad host security descriptor; "
 671                                "not enough data (%zu vs %zu left)\n",
 672                                top - itr, sizeof(*etd));
 673                        break;
 674                }
 675                if (etd->bLength < sizeof(*etd)) {
 676                        dev_err(dev, "BUG: bad host encryption descriptor; "
 677                                "descriptor is too short "
 678                                "(%zu vs %zu needed)\n",
 679                                (size_t)etd->bLength, sizeof(*etd));
 680                        break;
 681                }
 682                itr += etd->bLength;
 683                bytes += snprintf(buf + bytes, sizeof(buf) - bytes,
 684                                  "%s (0x%02x) ",
 685                                  wusb_et_name(etd->bEncryptionType),
 686                                  etd->bEncryptionValue);
 687                wusbhc->ccm1_etd = etd;
 688        }
 689        dev_info(dev, "supported encryption types: %s\n", buf);
 690        if (wusbhc->ccm1_etd == NULL) {
 691                dev_err(dev, "E: host doesn't support CCM-1 crypto\n");
 692                return 0;
 693        }
 694        /* Pretty print what we support */
 695        return 0;
 696}
 697
 698static void hwahc_security_release(struct hwahc *hwahc)
 699{
 700        /* nothing to do here so far... */
 701}
 702
 703static int hwahc_create(struct hwahc *hwahc, struct usb_interface *iface,
 704        kernel_ulong_t quirks)
 705{
 706        int result;
 707        struct device *dev = &iface->dev;
 708        struct wusbhc *wusbhc = &hwahc->wusbhc;
 709        struct wahc *wa = &hwahc->wa;
 710        struct usb_device *usb_dev = interface_to_usbdev(iface);
 711
 712        wa->usb_dev = usb_get_dev(usb_dev);     /* bind the USB device */
 713        wa->usb_iface = usb_get_intf(iface);
 714        wusbhc->dev = dev;
 715        /* defer getting the uwb_rc handle until it is needed since it
 716         * may not have been registered by the hwa_rc driver yet. */
 717        wusbhc->uwb_rc = NULL;
 718        result = wa_fill_descr(wa);     /* Get the device descriptor */
 719        if (result < 0)
 720                goto error_fill_descriptor;
 721        if (wa->wa_descr->bNumPorts > USB_MAXCHILDREN) {
 722                dev_err(dev, "FIXME: USB_MAXCHILDREN too low for WUSB "
 723                        "adapter (%u ports)\n", wa->wa_descr->bNumPorts);
 724                wusbhc->ports_max = USB_MAXCHILDREN;
 725        } else {
 726                wusbhc->ports_max = wa->wa_descr->bNumPorts;
 727        }
 728        wusbhc->mmcies_max = wa->wa_descr->bNumMMCIEs;
 729        wusbhc->start = __hwahc_op_wusbhc_start;
 730        wusbhc->stop = __hwahc_op_wusbhc_stop;
 731        wusbhc->mmcie_add = __hwahc_op_mmcie_add;
 732        wusbhc->mmcie_rm = __hwahc_op_mmcie_rm;
 733        wusbhc->dev_info_set = __hwahc_op_dev_info_set;
 734        wusbhc->bwa_set = __hwahc_op_bwa_set;
 735        wusbhc->set_num_dnts = __hwahc_op_set_num_dnts;
 736        wusbhc->set_ptk = __hwahc_op_set_ptk;
 737        wusbhc->set_gtk = __hwahc_op_set_gtk;
 738        result = hwahc_security_create(hwahc);
 739        if (result < 0) {
 740                dev_err(dev, "Can't initialize security: %d\n", result);
 741                goto error_security_create;
 742        }
 743        wa->wusb = wusbhc;      /* FIXME: ugly, need to fix */
 744        result = wusbhc_create(&hwahc->wusbhc);
 745        if (result < 0) {
 746                dev_err(dev, "Can't create WUSB HC structures: %d\n", result);
 747                goto error_wusbhc_create;
 748        }
 749        result = wa_create(&hwahc->wa, iface, quirks);
 750        if (result < 0)
 751                goto error_wa_create;
 752        return 0;
 753
 754error_wa_create:
 755        wusbhc_destroy(&hwahc->wusbhc);
 756error_wusbhc_create:
 757        /* WA Descr fill allocs no resources */
 758error_security_create:
 759error_fill_descriptor:
 760        usb_put_intf(iface);
 761        usb_put_dev(usb_dev);
 762        return result;
 763}
 764
 765static void hwahc_destroy(struct hwahc *hwahc)
 766{
 767        struct wusbhc *wusbhc = &hwahc->wusbhc;
 768
 769        mutex_lock(&wusbhc->mutex);
 770        __wa_destroy(&hwahc->wa);
 771        wusbhc_destroy(&hwahc->wusbhc);
 772        hwahc_security_release(hwahc);
 773        hwahc->wusbhc.dev = NULL;
 774        uwb_rc_put(wusbhc->uwb_rc);
 775        usb_put_intf(hwahc->wa.usb_iface);
 776        usb_put_dev(hwahc->wa.usb_dev);
 777        mutex_unlock(&wusbhc->mutex);
 778}
 779
 780static void hwahc_init(struct hwahc *hwahc)
 781{
 782        wa_init(&hwahc->wa);
 783}
 784
 785static int hwahc_probe(struct usb_interface *usb_iface,
 786                       const struct usb_device_id *id)
 787{
 788        int result;
 789        struct usb_hcd *usb_hcd;
 790        struct wusbhc *wusbhc;
 791        struct hwahc *hwahc;
 792        struct device *dev = &usb_iface->dev;
 793
 794        result = -ENOMEM;
 795        usb_hcd = usb_create_hcd(&hwahc_hc_driver, &usb_iface->dev, "wusb-hwa");
 796        if (usb_hcd == NULL) {
 797                dev_err(dev, "unable to allocate instance\n");
 798                goto error_alloc;
 799        }
 800        usb_hcd->wireless = 1;
 801        usb_hcd->self.sg_tablesize = ~0;
 802        wusbhc = usb_hcd_to_wusbhc(usb_hcd);
 803        hwahc = container_of(wusbhc, struct hwahc, wusbhc);
 804        hwahc_init(hwahc);
 805        result = hwahc_create(hwahc, usb_iface, id->driver_info);
 806        if (result < 0) {
 807                dev_err(dev, "Cannot initialize internals: %d\n", result);
 808                goto error_hwahc_create;
 809        }
 810        result = usb_add_hcd(usb_hcd, 0, 0);
 811        if (result < 0) {
 812                dev_err(dev, "Cannot add HCD: %d\n", result);
 813                goto error_add_hcd;
 814        }
 815        device_wakeup_enable(usb_hcd->self.controller);
 816        result = wusbhc_b_create(&hwahc->wusbhc);
 817        if (result < 0) {
 818                dev_err(dev, "Cannot setup phase B of WUSBHC: %d\n", result);
 819                goto error_wusbhc_b_create;
 820        }
 821        return 0;
 822
 823error_wusbhc_b_create:
 824        usb_remove_hcd(usb_hcd);
 825error_add_hcd:
 826        hwahc_destroy(hwahc);
 827error_hwahc_create:
 828        usb_put_hcd(usb_hcd);
 829error_alloc:
 830        return result;
 831}
 832
 833static void hwahc_disconnect(struct usb_interface *usb_iface)
 834{
 835        struct usb_hcd *usb_hcd;
 836        struct wusbhc *wusbhc;
 837        struct hwahc *hwahc;
 838
 839        usb_hcd = usb_get_intfdata(usb_iface);
 840        wusbhc = usb_hcd_to_wusbhc(usb_hcd);
 841        hwahc = container_of(wusbhc, struct hwahc, wusbhc);
 842
 843        wusbhc_b_destroy(&hwahc->wusbhc);
 844        usb_remove_hcd(usb_hcd);
 845        hwahc_destroy(hwahc);
 846        usb_put_hcd(usb_hcd);
 847}
 848
 849static const struct usb_device_id hwahc_id_table[] = {
 850        /* Alereon 5310 */
 851        { USB_DEVICE_AND_INTERFACE_INFO(0x13dc, 0x5310, 0xe0, 0x02, 0x01),
 852          .driver_info = WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC |
 853                WUSB_QUIRK_ALEREON_HWA_DISABLE_XFER_NOTIFICATIONS },
 854        /* Alereon 5611 */
 855        { USB_DEVICE_AND_INTERFACE_INFO(0x13dc, 0x5611, 0xe0, 0x02, 0x01),
 856          .driver_info = WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC |
 857                WUSB_QUIRK_ALEREON_HWA_DISABLE_XFER_NOTIFICATIONS },
 858        /* FIXME: use class labels for this */
 859        { USB_INTERFACE_INFO(0xe0, 0x02, 0x01), },
 860        {},
 861};
 862MODULE_DEVICE_TABLE(usb, hwahc_id_table);
 863
 864static struct usb_driver hwahc_driver = {
 865        .name =         "hwa-hc",
 866        .probe =        hwahc_probe,
 867        .disconnect =   hwahc_disconnect,
 868        .id_table =     hwahc_id_table,
 869};
 870
 871module_usb_driver(hwahc_driver);
 872
 873MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
 874MODULE_DESCRIPTION("Host Wired Adapter USB Host Control Driver");
 875MODULE_LICENSE("GPL");
 876