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