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