wusb: add the Wireless USB core
authorInaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
Wed, 17 Sep 2008 15:34:23 +0000 (16:34 +0100)
committerDavid Vrabel <dv02@dv02pc01.europe.root.pri>
Wed, 17 Sep 2008 15:54:29 +0000 (16:54 +0100)
Add support for Ceritified Wireless USB 1.0 to the USB stack.

This has been split into several patches for easier review.

core (this patch):
  - host controller infrastructure
  - cluster reservation
  - UWB PAL registration
  - fake root hub

protocol:
  - MMC management (start/stop, managing IEs)
  - device connection

security:
  - device authentication and authorization

build-system:
  - Kconfig and Kbuild files

Signed-off-by: David Vrabel <david.vrabel@csr.com>
drivers/usb/wusbcore/dev-sysfs.c [new file with mode: 0644]
drivers/usb/wusbcore/pal.c [new file with mode: 0644]
drivers/usb/wusbcore/reservation.c [new file with mode: 0644]
drivers/usb/wusbcore/rh.c [new file with mode: 0644]
drivers/usb/wusbcore/wusbhc.c [new file with mode: 0644]
drivers/usb/wusbcore/wusbhc.h [new file with mode: 0644]

diff --git a/drivers/usb/wusbcore/dev-sysfs.c b/drivers/usb/wusbcore/dev-sysfs.c
new file mode 100644 (file)
index 0000000..7897a19
--- /dev/null
@@ -0,0 +1,143 @@
+/*
+ * WUSB devices
+ * sysfs bindings
+ *
+ * Copyright (C) 2007 Intel Corporation
+ * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ *
+ * Get them out of the way...
+ */
+
+#include <linux/jiffies.h>
+#include <linux/ctype.h>
+#include <linux/workqueue.h>
+#include "wusbhc.h"
+
+#undef D_LOCAL
+#define D_LOCAL 4
+#include <linux/uwb/debug.h>
+
+static ssize_t wusb_disconnect_store(struct device *dev,
+                                    struct device_attribute *attr,
+                                    const char *buf, size_t size)
+{
+       struct usb_device *usb_dev;
+       struct wusbhc *wusbhc;
+       unsigned command;
+       u8 port_idx;
+
+       if (sscanf(buf, "%u", &command) != 1)
+               return -EINVAL;
+       if (command == 0)
+               return size;
+       usb_dev = to_usb_device(dev);
+       wusbhc = wusbhc_get_by_usb_dev(usb_dev);
+       if (wusbhc == NULL)
+               return -ENODEV;
+
+       mutex_lock(&wusbhc->mutex);
+       port_idx = wusb_port_no_to_idx(usb_dev->portnum);
+       __wusbhc_dev_disable(wusbhc, port_idx);
+       mutex_unlock(&wusbhc->mutex);
+       wusbhc_put(wusbhc);
+       return size;
+}
+static DEVICE_ATTR(wusb_disconnect, 0200, NULL, wusb_disconnect_store);
+
+static ssize_t wusb_cdid_show(struct device *dev,
+                             struct device_attribute *attr, char *buf)
+{
+       ssize_t result;
+       struct wusb_dev *wusb_dev;
+
+       wusb_dev = wusb_dev_get_by_usb_dev(to_usb_device(dev));
+       if (wusb_dev == NULL)
+               return -ENODEV;
+       result = ckhdid_printf(buf, PAGE_SIZE, &wusb_dev->cdid);
+       strcat(buf, "\n");
+       wusb_dev_put(wusb_dev);
+       return result + 1;
+}
+static DEVICE_ATTR(wusb_cdid, 0444, wusb_cdid_show, NULL);
+
+static ssize_t wusb_ck_store(struct device *dev,
+                            struct device_attribute *attr,
+                            const char *buf, size_t size)
+{
+       int result;
+       struct usb_device *usb_dev;
+       struct wusbhc *wusbhc;
+       struct wusb_ckhdid ck;
+
+       result = sscanf(buf,
+                       "%02hhx %02hhx %02hhx %02hhx "
+                       "%02hhx %02hhx %02hhx %02hhx "
+                       "%02hhx %02hhx %02hhx %02hhx "
+                       "%02hhx %02hhx %02hhx %02hhx\n",
+                       &ck.data[0] , &ck.data[1],
+                       &ck.data[2] , &ck.data[3],
+                       &ck.data[4] , &ck.data[5],
+                       &ck.data[6] , &ck.data[7],
+                       &ck.data[8] , &ck.data[9],
+                       &ck.data[10], &ck.data[11],
+                       &ck.data[12], &ck.data[13],
+                       &ck.data[14], &ck.data[15]);
+       if (result != 16)
+               return -EINVAL;
+
+       usb_dev = to_usb_device(dev);
+       wusbhc = wusbhc_get_by_usb_dev(usb_dev);
+       if (wusbhc == NULL)
+               return -ENODEV;
+       result = wusb_dev_4way_handshake(wusbhc, usb_dev->wusb_dev, &ck);
+       memset(&ck, 0, sizeof(ck));
+       wusbhc_put(wusbhc);
+       return result < 0 ? result : size;
+}
+static DEVICE_ATTR(wusb_ck, 0200, NULL, wusb_ck_store);
+
+static struct attribute *wusb_dev_attrs[] = {
+               &dev_attr_wusb_disconnect.attr,
+               &dev_attr_wusb_cdid.attr,
+               &dev_attr_wusb_ck.attr,
+               NULL,
+};
+
+static struct attribute_group wusb_dev_attr_group = {
+       .name = NULL,   /* we want them in the same directory */
+       .attrs = wusb_dev_attrs,
+};
+
+int wusb_dev_sysfs_add(struct wusbhc *wusbhc, struct usb_device *usb_dev,
+                      struct wusb_dev *wusb_dev)
+{
+       int result = sysfs_create_group(&usb_dev->dev.kobj,
+                                       &wusb_dev_attr_group);
+       struct device *dev = &usb_dev->dev;
+       if (result < 0)
+               dev_err(dev, "Cannot register WUSB-dev attributes: %d\n",
+                       result);
+       return result;
+}
+
+void wusb_dev_sysfs_rm(struct wusb_dev *wusb_dev)
+{
+       struct usb_device *usb_dev = wusb_dev->usb_dev;
+       if (usb_dev)
+               sysfs_remove_group(&usb_dev->dev.kobj, &wusb_dev_attr_group);
+}
diff --git a/drivers/usb/wusbcore/pal.c b/drivers/usb/wusbcore/pal.c
new file mode 100644 (file)
index 0000000..cc126b4
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Wireless USB Host Controller
+ * UWB Protocol Adaptation Layer (PAL) glue.
+ *
+ * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#include "wusbhc.h"
+
+/**
+ * wusbhc_pal_register - register the WUSB HC as a UWB PAL
+ * @wusbhc: the WUSB HC
+ */
+int wusbhc_pal_register(struct wusbhc *wusbhc)
+{
+       uwb_pal_init(&wusbhc->pal);
+
+       return uwb_pal_register(wusbhc->uwb_rc, &wusbhc->pal);
+}
+
+/**
+ * wusbhc_pal_register - unregister the WUSB HC as a UWB PAL
+ * @wusbhc: the WUSB HC
+ */
+void wusbhc_pal_unregister(struct wusbhc *wusbhc)
+{
+       uwb_pal_unregister(wusbhc->uwb_rc, &wusbhc->pal);
+}
diff --git a/drivers/usb/wusbcore/reservation.c b/drivers/usb/wusbcore/reservation.c
new file mode 100644 (file)
index 0000000..fc63e77
--- /dev/null
@@ -0,0 +1,115 @@
+/*
+ * WUSB cluster reservation management
+ *
+ * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <linux/kernel.h>
+#include <linux/uwb.h>
+
+#include "wusbhc.h"
+
+/*
+ * WUSB cluster reservations are multicast reservations with the
+ * broadcast cluster ID (BCID) as the target DevAddr.
+ *
+ * FIXME: consider adjusting the reservation depending on what devices
+ * are attached.
+ */
+
+static int wusbhc_bwa_set(struct wusbhc *wusbhc, u8 stream,
+       const struct uwb_mas_bm *mas)
+{
+       if (mas == NULL)
+               mas = &uwb_mas_bm_zero;
+       return wusbhc->bwa_set(wusbhc, stream, mas);
+}
+
+/**
+ * wusbhc_rsv_complete_cb - WUSB HC reservation complete callback
+ * @rsv:    the reservation
+ *
+ * Either set or clear the HC's view of the reservation.
+ *
+ * FIXME: when a reservation is denied the HC should be stopped.
+ */
+static void wusbhc_rsv_complete_cb(struct uwb_rsv *rsv)
+{
+       struct wusbhc *wusbhc = rsv->pal_priv;
+       struct device *dev = wusbhc->dev;
+       char buf[72];
+
+       switch (rsv->state) {
+       case UWB_RSV_STATE_O_ESTABLISHED:
+               bitmap_scnprintf(buf, sizeof(buf), rsv->mas.bm, UWB_NUM_MAS);
+               dev_dbg(dev, "established reservation: %s\n", buf);
+               wusbhc_bwa_set(wusbhc, rsv->stream, &rsv->mas);
+               break;
+       case UWB_RSV_STATE_NONE:
+               dev_dbg(dev, "removed reservation\n");
+               wusbhc_bwa_set(wusbhc, 0, NULL);
+               wusbhc->rsv = NULL;
+               break;
+       default:
+               dev_dbg(dev, "unexpected reservation state: %d\n", rsv->state);
+               break;
+       }
+}
+
+
+/**
+ * wusbhc_rsv_establish - establish a reservation for the cluster
+ * @wusbhc: the WUSB HC requesting a bandwith reservation
+ */
+int wusbhc_rsv_establish(struct wusbhc *wusbhc)
+{
+       struct uwb_rc *rc = wusbhc->uwb_rc;
+       struct uwb_rsv *rsv;
+       struct uwb_dev_addr bcid;
+       int ret;
+
+       rsv = uwb_rsv_create(rc, wusbhc_rsv_complete_cb, wusbhc);
+       if (rsv == NULL)
+               return -ENOMEM;
+
+       bcid.data[0] = wusbhc->cluster_id;
+       bcid.data[1] = 0;
+
+       rsv->owner = &rc->uwb_dev;
+       rsv->target.type = UWB_RSV_TARGET_DEVADDR;
+       rsv->target.devaddr = bcid;
+       rsv->type = UWB_DRP_TYPE_PRIVATE;
+       rsv->max_mas = 256;
+       rsv->min_mas = 16;  /* one MAS per zone? */
+       rsv->sparsity = 16; /* at least one MAS in each zone? */
+       rsv->is_multicast = true;
+
+       ret = uwb_rsv_establish(rsv);
+       if (ret == 0)
+               wusbhc->rsv = rsv;
+       else
+               uwb_rsv_destroy(rsv);
+       return ret;
+}
+
+
+/**
+ * wusbhc_rsv_terminate - terminate any cluster reservation
+ * @wusbhc: the WUSB host whose reservation is to be terminated
+ */
+void wusbhc_rsv_terminate(struct wusbhc *wusbhc)
+{
+       if (wusbhc->rsv)
+               uwb_rsv_terminate(wusbhc->rsv);
+}
diff --git a/drivers/usb/wusbcore/rh.c b/drivers/usb/wusbcore/rh.c
new file mode 100644 (file)
index 0000000..267a643
--- /dev/null
@@ -0,0 +1,477 @@
+/*
+ * Wireless USB Host Controller
+ * Root Hub operations
+ *
+ *
+ * Copyright (C) 2005-2006 Intel Corporation
+ * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ *
+ * We fake a root hub that has fake ports (as many as simultaneous
+ * devices the Wireless USB Host Controller can deal with). For each
+ * port we keep an state in @wusbhc->port[index] identical to the one
+ * specified in the USB2.0[ch11] spec and some extra device
+ * information that complements the one in 'struct usb_device' (as
+ * this lacs a hcpriv pointer).
+ *
+ * Note this is common to WHCI and HWA host controllers.
+ *
+ * Through here we enable most of the state changes that the USB stack
+ * will use to connect or disconnect devices. We need to do some
+ * forced adaptation of Wireless USB device states vs. wired:
+ *
+ *        USB:                 WUSB:
+ *
+ * Port   Powered-off          port slot n/a
+ *        Powered-on           port slot available
+ *        Disconnected         port slot available
+ *        Connected            port slot assigned device
+ *                            device sent DN_Connect
+ *                             device was authenticated
+ *        Enabled              device is authenticated, transitioned
+ *                             from unauth -> auth -> default address
+ *                             -> enabled
+ *        Reset                disconnect
+ *        Disable              disconnect
+ *
+ * This maps the standard USB port states with the WUSB device states
+ * so we can fake ports without having to modify the USB stack.
+ *
+ * FIXME: this process will change in the future
+ *
+ *
+ * ENTRY POINTS
+ *
+ * Our entry points into here are, as in hcd.c, the USB stack root hub
+ * ops defined in the usb_hcd struct:
+ *
+ * wusbhc_rh_status_data()     Provide hub and port status data bitmap
+ *
+ * wusbhc_rh_control()          Execution of all the major requests
+ *                              you can do to a hub (Set|Clear
+ *                              features, get descriptors, status, etc).
+ *
+ * wusbhc_rh_[suspend|resume]() That
+ *
+ * wusbhc_rh_start_port_reset() ??? unimplemented
+ */
+#include "wusbhc.h"
+
+#define D_LOCAL 0
+#include <linux/uwb/debug.h>
+
+/*
+ * Reset a fake port
+ *
+ * This can be called to reset a port from any other state or to reset
+ * it when connecting. In Wireless USB they are different; when doing
+ * a new connect that involves going over the authentication. When
+ * just reseting, its a different story.
+ *
+ * The Linux USB stack resets a port twice before it considers it
+ * enabled, so we have to detect and ignore that.
+ *
+ * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
+ *
+ * Supposedly we are the only thread accesing @wusbhc->port; in any
+ * case, maybe we should move the mutex locking from
+ * wusbhc_devconnect_auth() to here.
+ *
+ * @port_idx refers to the wusbhc's port index, not the USB port number
+ */
+static int wusbhc_rh_port_reset(struct wusbhc *wusbhc, u8 port_idx)
+{
+       int result = 0;
+       struct wusb_port *port = wusb_port_by_idx(wusbhc, port_idx);
+
+       d_fnstart(3, wusbhc->dev, "(wusbhc %p port_idx %u)\n",
+                 wusbhc, port_idx);
+       if (port->reset_count == 0) {
+               wusbhc_devconnect_auth(wusbhc, port_idx);
+               port->reset_count++;
+       } else if (port->reset_count == 1)
+               /* see header */
+               d_printf(2, wusbhc->dev, "Ignoring second reset on port_idx "
+                       "%u\n", port_idx);
+       else
+               result = wusbhc_dev_reset(wusbhc, port_idx);
+       d_fnend(3, wusbhc->dev, "(wusbhc %p port_idx %u) = %d\n",
+               wusbhc, port_idx, result);
+       return result;
+}
+
+/*
+ * Return the hub change status bitmap
+ *
+ * The bits in the change status bitmap are cleared when a
+ * ClearPortFeature request is issued (USB2.0[11.12.3,11.12.4].
+ *
+ * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
+ *
+ * WARNING!! This gets called from atomic context; we cannot get the
+ *           mutex--the only race condition we can find is some bit
+ *           changing just after we copy it, which shouldn't be too
+ *           big of a problem [and we can't make it an spinlock
+ *           because other parts need to take it and sleep] .
+ *
+ *           @usb_hcd is refcounted, so it won't dissapear under us
+ *           and before killing a host, the polling of the root hub
+ *           would be stopped anyway.
+ */
+int wusbhc_rh_status_data(struct usb_hcd *usb_hcd, char *_buf)
+{
+       struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
+       size_t cnt, size;
+       unsigned long *buf = (unsigned long *) _buf;
+
+       d_fnstart(1, wusbhc->dev, "(wusbhc %p)\n", wusbhc);
+       /* WE DON'T LOCK, see comment */
+       size = wusbhc->ports_max + 1 /* hub bit */;
+       size = (size + 8 - 1) / 8;      /* round to bytes */
+       for (cnt = 0; cnt < wusbhc->ports_max; cnt++)
+               if (wusb_port_by_idx(wusbhc, cnt)->change)
+                       set_bit(cnt + 1, buf);
+               else
+                       clear_bit(cnt + 1, buf);
+       d_fnend(1, wusbhc->dev, "(wusbhc %p) %u, buffer:\n", wusbhc, (int)size);
+       d_dump(1, wusbhc->dev, _buf, size);
+       return size;
+}
+EXPORT_SYMBOL_GPL(wusbhc_rh_status_data);
+
+/*
+ * Return the hub's desciptor
+ *
+ * NOTE: almost cut and paste from ehci-hub.c
+ *
+ * @wusbhc is assumed referenced and @wusbhc->mutex unlocked
+ */
+static int wusbhc_rh_get_hub_descr(struct wusbhc *wusbhc, u16 wValue,
+                                  u16 wIndex,
+                                  struct usb_hub_descriptor *descr,
+                                  u16 wLength)
+{
+       u16 temp = 1 + (wusbhc->ports_max / 8);
+       u8 length = 7 + 2 * temp;
+
+       if (wLength < length)
+               return -ENOSPC;
+       descr->bDescLength = 7 + 2 * temp;
+       descr->bDescriptorType = 0x29;  /* HUB type */
+       descr->bNbrPorts = wusbhc->ports_max;
+       descr->wHubCharacteristics = cpu_to_le16(
+               0x00                    /* All ports power at once */
+               | 0x00                  /* not part of compound device */
+               | 0x10                  /* No overcurrent protection */
+               | 0x00                  /* 8 FS think time FIXME ?? */
+               | 0x00);                /* No port indicators */
+       descr->bPwrOn2PwrGood = 0;
+       descr->bHubContrCurrent = 0;
+       /* two bitmaps:  ports removable, and usb 1.0 legacy PortPwrCtrlMask */
+       memset(&descr->bitmap[0], 0, temp);
+       memset(&descr->bitmap[temp], 0xff, temp);
+       return 0;
+}
+
+/*
+ * Clear a hub feature
+ *
+ * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
+ *
+ * Nothing to do, so no locking needed ;)
+ */
+static int wusbhc_rh_clear_hub_feat(struct wusbhc *wusbhc, u16 feature)
+{
+       int result;
+       struct device *dev = wusbhc->dev;
+
+       d_fnstart(4, dev, "(%p, feature 0x%04u)\n", wusbhc, feature);
+       switch (feature) {
+       case C_HUB_LOCAL_POWER:
+               /* FIXME: maybe plug bit 0 to the power input status,
+                * if any?
+                * see wusbhc_rh_get_hub_status() */
+       case C_HUB_OVER_CURRENT:
+               result = 0;
+               break;
+       default:
+               result = -EPIPE;
+       }
+       d_fnend(4, dev, "(%p, feature 0x%04u), %d\n", wusbhc, feature, result);
+       return result;
+}
+
+/*
+ * Return hub status (it is always zero...)
+ *
+ * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
+ *
+ * Nothing to do, so no locking needed ;)
+ */
+static int wusbhc_rh_get_hub_status(struct wusbhc *wusbhc, u32 *buf,
+                                   u16 wLength)
+{
+       /* FIXME: maybe plug bit 0 to the power input status (if any)? */
+       *buf = 0;
+       return 0;
+}
+
+/*
+ * Set a port feature
+ *
+ * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
+ */
+static int wusbhc_rh_set_port_feat(struct wusbhc *wusbhc, u16 feature,
+                                  u8 selector, u8 port_idx)
+{
+       int result = -EINVAL;
+       struct device *dev = wusbhc->dev;
+
+       d_fnstart(4, dev, "(feat 0x%04u, selector 0x%u, port_idx %d)\n",
+                 feature, selector, port_idx);
+
+       if (port_idx > wusbhc->ports_max)
+               goto error;
+
+       switch (feature) {
+               /* According to USB2.0[11.24.2.13]p2, these features
+                * are not required to be implemented. */
+       case USB_PORT_FEAT_C_OVER_CURRENT:
+       case USB_PORT_FEAT_C_ENABLE:
+       case USB_PORT_FEAT_C_SUSPEND:
+       case USB_PORT_FEAT_C_CONNECTION:
+       case USB_PORT_FEAT_C_RESET:
+               result = 0;
+               break;
+
+       case USB_PORT_FEAT_POWER:
+               /* No such thing, but we fake it works */
+               mutex_lock(&wusbhc->mutex);
+               wusb_port_by_idx(wusbhc, port_idx)->status |= USB_PORT_STAT_POWER;
+               mutex_unlock(&wusbhc->mutex);
+               result = 0;
+               break;
+       case USB_PORT_FEAT_RESET:
+               result = wusbhc_rh_port_reset(wusbhc, port_idx);
+               break;
+       case USB_PORT_FEAT_ENABLE:
+       case USB_PORT_FEAT_SUSPEND:
+               dev_err(dev, "(port_idx %d) set feat %d/%d UNIMPLEMENTED\n",
+                       port_idx, feature, selector);
+               result = -ENOSYS;
+               break;
+       default:
+               dev_err(dev, "(port_idx %d) set feat %d/%d UNKNOWN\n",
+                       port_idx, feature, selector);
+               result = -EPIPE;
+               break;
+       }
+error:
+       d_fnend(4, dev, "(feat 0x%04u, selector 0x%u, port_idx %d) = %d\n",
+               feature, selector, port_idx, result);
+       return result;
+}
+
+/*
+ * Clear a port feature...
+ *
+ * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
+ */
+static int wusbhc_rh_clear_port_feat(struct wusbhc *wusbhc, u16 feature,
+                                    u8 selector, u8 port_idx)
+{
+       int result = -EINVAL;
+       struct device *dev = wusbhc->dev;
+
+       d_fnstart(4, dev, "(wusbhc %p feat 0x%04x selector %d port_idx %d)\n",
+                 wusbhc, feature, selector, port_idx);
+
+       if (port_idx > wusbhc->ports_max)
+               goto error;
+
+       mutex_lock(&wusbhc->mutex);
+       result = 0;
+       switch (feature) {
+       case USB_PORT_FEAT_POWER:       /* fake port always on */
+               /* According to USB2.0[11.24.2.7.1.4], no need to implement? */
+       case USB_PORT_FEAT_C_OVER_CURRENT:
+               break;
+       case USB_PORT_FEAT_C_RESET:
+               wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_RESET;
+               break;
+       case USB_PORT_FEAT_C_CONNECTION:
+               wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_CONNECTION;
+               break;
+       case USB_PORT_FEAT_ENABLE:
+               __wusbhc_dev_disable(wusbhc, port_idx);
+               break;
+       case USB_PORT_FEAT_C_ENABLE:
+               wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_ENABLE;
+               break;
+       case USB_PORT_FEAT_SUSPEND:
+       case USB_PORT_FEAT_C_SUSPEND:
+       case 0xffff:            /* ??? FIXME */
+               dev_err(dev, "(port_idx %d) Clear feat %d/%d UNIMPLEMENTED\n",
+                       port_idx, feature, selector);
+               /* dump_stack(); */
+               result = -ENOSYS;
+               break;
+       default:
+               dev_err(dev, "(port_idx %d) Clear feat %d/%d UNKNOWN\n",
+                       port_idx, feature, selector);
+               result = -EPIPE;
+               break;
+       }
+       mutex_unlock(&wusbhc->mutex);
+error:
+       d_fnend(4, dev, "(wusbhc %p feat 0x%04x selector %d port_idx %d) = "
+               "%d\n", wusbhc, feature, selector, port_idx, result);
+       return result;
+}
+
+/*
+ * Return the port's status
+ *
+ * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
+ */
+static int wusbhc_rh_get_port_status(struct wusbhc *wusbhc, u16 port_idx,
+                                    u32 *_buf, u16 wLength)
+{
+       int result = -EINVAL;
+       u16 *buf = (u16 *) _buf;
+
+       d_fnstart(1, wusbhc->dev, "(wusbhc %p port_idx %u wLength %u)\n",
+                 wusbhc, port_idx, wLength);
+       if (port_idx > wusbhc->ports_max)
+               goto error;
+       mutex_lock(&wusbhc->mutex);
+       buf[0] = cpu_to_le16(wusb_port_by_idx(wusbhc, port_idx)->status);
+       buf[1] = cpu_to_le16(wusb_port_by_idx(wusbhc, port_idx)->change);
+       result = 0;
+       mutex_unlock(&wusbhc->mutex);
+error:
+       d_fnend(1, wusbhc->dev, "(wusbhc %p) = %d, buffer:\n", wusbhc, result);
+       d_dump(1, wusbhc->dev, _buf, wLength);
+       return result;
+}
+
+/*
+ * Entry point for Root Hub operations
+ *
+ * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
+ */
+int wusbhc_rh_control(struct usb_hcd *usb_hcd, u16 reqntype, u16 wValue,
+                     u16 wIndex, char *buf, u16 wLength)
+{
+       int result = -ENOSYS;
+       struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
+
+       switch (reqntype) {
+       case GetHubDescriptor:
+               result = wusbhc_rh_get_hub_descr(
+                       wusbhc, wValue, wIndex,
+                       (struct usb_hub_descriptor *) buf, wLength);
+               break;
+       case ClearHubFeature:
+               result = wusbhc_rh_clear_hub_feat(wusbhc, wValue);
+               break;
+       case GetHubStatus:
+               result = wusbhc_rh_get_hub_status(wusbhc, (u32 *)buf, wLength);
+               break;
+
+       case SetPortFeature:
+               result = wusbhc_rh_set_port_feat(wusbhc, wValue, wIndex >> 8,
+                                                (wIndex & 0xff) - 1);
+               break;
+       case ClearPortFeature:
+               result = wusbhc_rh_clear_port_feat(wusbhc, wValue, wIndex >> 8,
+                                                  (wIndex & 0xff) - 1);
+               break;
+       case GetPortStatus:
+               result = wusbhc_rh_get_port_status(wusbhc, wIndex - 1,
+                                                  (u32 *)buf, wLength);
+               break;
+
+       case SetHubFeature:
+       default:
+               dev_err(wusbhc->dev, "%s (%p [%p], %x, %x, %x, %p, %x) "
+                       "UNIMPLEMENTED\n", __func__, usb_hcd, wusbhc, reqntype,
+                       wValue, wIndex, buf, wLength);
+               /* dump_stack(); */
+               result = -ENOSYS;
+       }
+       return result;
+}
+EXPORT_SYMBOL_GPL(wusbhc_rh_control);
+
+int wusbhc_rh_suspend(struct usb_hcd *usb_hcd)
+{
+       struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
+       dev_err(wusbhc->dev, "%s (%p [%p]) UNIMPLEMENTED\n", __func__,
+               usb_hcd, wusbhc);
+       /* dump_stack(); */
+       return -ENOSYS;
+}
+EXPORT_SYMBOL_GPL(wusbhc_rh_suspend);
+
+int wusbhc_rh_resume(struct usb_hcd *usb_hcd)
+{
+       struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
+       dev_err(wusbhc->dev, "%s (%p [%p]) UNIMPLEMENTED\n", __func__,
+               usb_hcd, wusbhc);
+       /* dump_stack(); */
+       return -ENOSYS;
+}
+EXPORT_SYMBOL_GPL(wusbhc_rh_resume);
+
+int wusbhc_rh_start_port_reset(struct usb_hcd *usb_hcd, unsigned port_idx)
+{
+       struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
+       dev_err(wusbhc->dev, "%s (%p [%p], port_idx %u) UNIMPLEMENTED\n",
+               __func__, usb_hcd, wusbhc, port_idx);
+       WARN_ON(1);
+       return -ENOSYS;
+}
+EXPORT_SYMBOL_GPL(wusbhc_rh_start_port_reset);
+
+static void wusb_port_init(struct wusb_port *port)
+{
+       port->status |= USB_PORT_STAT_HIGH_SPEED;
+}
+
+/*
+ * Alloc fake port specific fields and status.
+ */
+int wusbhc_rh_create(struct wusbhc *wusbhc)
+{
+       int result = -ENOMEM;
+       size_t port_size, itr;
+       port_size = wusbhc->ports_max * sizeof(wusbhc->port[0]);
+       wusbhc->port = kzalloc(port_size, GFP_KERNEL);
+       if (wusbhc->port == NULL)
+               goto error_port_alloc;
+       for (itr = 0; itr < wusbhc->ports_max; itr++)
+               wusb_port_init(&wusbhc->port[itr]);
+       result = 0;
+error_port_alloc:
+       return result;
+}
+
+void wusbhc_rh_destroy(struct wusbhc *wusbhc)
+{
+       kfree(wusbhc->port);
+}
diff --git a/drivers/usb/wusbcore/wusbhc.c b/drivers/usb/wusbcore/wusbhc.c
new file mode 100644 (file)
index 0000000..1149b1e
--- /dev/null
@@ -0,0 +1,416 @@
+/*
+ * Wireless USB Host Controller
+ * sysfs glue, wusbcore module support and life cycle management
+ *
+ *
+ * Copyright (C) 2005-2006 Intel Corporation
+ * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ *
+ * Creation/destruction of wusbhc is split in two parts; that that
+ * doesn't require the HCD to be added (wusbhc_{create,destroy}) and
+ * the one that requires (phase B, wusbhc_b_{create,destroy}).
+ *
+ * This is so because usb_add_hcd() will start the HC, and thus, all
+ * the HC specific stuff has to be already initialiazed (like sysfs
+ * thingies).
+ */
+#include <linux/device.h>
+#include <linux/module.h>
+#include "wusbhc.h"
+
+/**
+ * Extract the wusbhc that corresponds to a USB Host Controller class device
+ *
+ * WARNING! Apply only if @dev is that of a
+ *          wusbhc.usb_hcd.self->class_dev; otherwise, you loose.
+ */
+static struct wusbhc *usbhc_dev_to_wusbhc(struct device *dev)
+{
+       struct usb_bus *usb_bus = dev_get_drvdata(dev);
+       struct usb_hcd *usb_hcd = bus_to_hcd(usb_bus);
+       return usb_hcd_to_wusbhc(usb_hcd);
+}
+
+/*
+ * Show & store the current WUSB trust timeout
+ *
+ * We don't do locking--it is an 'atomic' value.
+ *
+ * The units that we store/show are always MILLISECONDS. However, the
+ * value of trust_timeout is jiffies.
+ */
+static ssize_t wusb_trust_timeout_show(struct device *dev,
+                                      struct device_attribute *attr, char *buf)
+{
+       struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
+
+       return scnprintf(buf, PAGE_SIZE, "%u\n", wusbhc->trust_timeout);
+}
+
+static ssize_t wusb_trust_timeout_store(struct device *dev,
+                                       struct device_attribute *attr,
+                                       const char *buf, size_t size)
+{
+       struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
+       ssize_t result = -ENOSYS;
+       unsigned trust_timeout;
+
+       result = sscanf(buf, "%u", &trust_timeout);
+       if (result != 1) {
+               result = -EINVAL;
+               goto out;
+       }
+       /* FIXME: maybe we should check for range validity? */
+       wusbhc->trust_timeout = trust_timeout;
+       cancel_delayed_work(&wusbhc->keep_alive_timer);
+       flush_workqueue(wusbd);
+       queue_delayed_work(wusbd, &wusbhc->keep_alive_timer,
+                          (trust_timeout * CONFIG_HZ)/1000/2);
+out:
+       return result < 0 ? result : size;
+}
+static DEVICE_ATTR(wusb_trust_timeout, 0644, wusb_trust_timeout_show,
+                                            wusb_trust_timeout_store);
+
+/*
+ * Show & store the current WUSB CHID
+ */
+static ssize_t wusb_chid_show(struct device *dev,
+                             struct device_attribute *attr, char *buf)
+{
+       struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
+       ssize_t result = 0;
+
+       if (wusbhc->wuie_host_info != NULL)
+               result += ckhdid_printf(buf, PAGE_SIZE,
+                                       &wusbhc->wuie_host_info->CHID);
+       return result;
+}
+
+/*
+ * Store a new CHID
+ *
+ * This will (FIXME) trigger many changes.
+ *
+ * - Send an all zeros CHID and it will stop the controller
+ * - Send a non-zero CHID and it will start it
+ *   (unless it was started, it will just change the CHID,
+ *   diconnecting all devices first).
+ *
+ * So first we scan the MMC we are sent and then we act on it.  We
+ * read it in the same format as we print it, an ASCII string of 16
+ * hex bytes.
+ *
+ * See wusbhc_chid_set() for more info.
+ */
+static ssize_t wusb_chid_store(struct device *dev,
+                              struct device_attribute *attr,
+                              const char *buf, size_t size)
+{
+       struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
+       struct wusb_ckhdid chid;
+       ssize_t result;
+
+       result = sscanf(buf,
+                       "%02hhx %02hhx %02hhx %02hhx "
+                       "%02hhx %02hhx %02hhx %02hhx "
+                       "%02hhx %02hhx %02hhx %02hhx "
+                       "%02hhx %02hhx %02hhx %02hhx\n",
+                       &chid.data[0] , &chid.data[1] ,
+                       &chid.data[2] , &chid.data[3] ,
+                       &chid.data[4] , &chid.data[5] ,
+                       &chid.data[6] , &chid.data[7] ,
+                       &chid.data[8] , &chid.data[9] ,
+                       &chid.data[10], &chid.data[11],
+                       &chid.data[12], &chid.data[13],
+                       &chid.data[14], &chid.data[15]);
+       if (result != 16) {
+               dev_err(dev, "Unrecognized CHID (need 16 8-bit hex digits): "
+                       "%d\n", (int)result);
+               return -EINVAL;
+       }
+       result = wusbhc_chid_set(wusbhc, &chid);
+       return result < 0 ? result : size;
+}
+static DEVICE_ATTR(wusb_chid, 0644, wusb_chid_show, wusb_chid_store);
+
+/* Group all the WUSBHC attributes */
+static struct attribute *wusbhc_attrs[] = {
+               &dev_attr_wusb_trust_timeout.attr,
+               &dev_attr_wusb_chid.attr,
+               NULL,
+};
+
+static struct attribute_group wusbhc_attr_group = {
+       .name = NULL,   /* we want them in the same directory */
+       .attrs = wusbhc_attrs,
+};
+
+/*
+ * Create a wusbhc instance
+ *
+ * NOTEs:
+ *
+ *  - assumes *wusbhc has been zeroed and wusbhc->usb_hcd has been
+ *    initialized but not added.
+ *
+ *  - fill out ports_max, mmcies_max and mmcie_{add,rm} before calling.
+ *
+ *  - fill out wusbhc->uwb_rc and refcount it before calling
+ *  - fill out the wusbhc->sec_modes array
+ */
+int wusbhc_create(struct wusbhc *wusbhc)
+{
+       int result = 0;
+
+       wusbhc->trust_timeout = WUSB_TRUST_TIMEOUT_MS;
+       mutex_init(&wusbhc->mutex);
+       result = wusbhc_mmcie_create(wusbhc);
+       if (result < 0)
+               goto error_mmcie_create;
+       result = wusbhc_devconnect_create(wusbhc);
+       if (result < 0)
+               goto error_devconnect_create;
+       result = wusbhc_rh_create(wusbhc);
+       if (result < 0)
+               goto error_rh_create;
+       result = wusbhc_sec_create(wusbhc);
+       if (result < 0)
+               goto error_sec_create;
+       result = wusbhc_pal_register(wusbhc);
+       if (result < 0)
+               goto error_pal_register;
+       return 0;
+
+error_pal_register:
+       wusbhc_sec_destroy(wusbhc);
+error_sec_create:
+       wusbhc_rh_destroy(wusbhc);
+error_rh_create:
+       wusbhc_devconnect_destroy(wusbhc);
+error_devconnect_create:
+       wusbhc_mmcie_destroy(wusbhc);
+error_mmcie_create:
+       return result;
+}
+EXPORT_SYMBOL_GPL(wusbhc_create);
+
+static inline struct kobject *wusbhc_kobj(struct wusbhc *wusbhc)
+{
+       return &wusbhc->usb_hcd.self.controller->kobj;
+}
+
+/*
+ * Phase B of a wusbhc instance creation
+ *
+ * Creates fields that depend on wusbhc->usb_hcd having been
+ * added. This is where we create the sysfs files in
+ * /sys/class/usb_host/usb_hostX/.
+ *
+ * NOTE: Assumes wusbhc->usb_hcd has been already added by the upper
+ *       layer (hwahc or whci)
+ */
+int wusbhc_b_create(struct wusbhc *wusbhc)
+{
+       int result = 0;
+       struct device *dev = wusbhc->usb_hcd.self.controller;
+
+       result = sysfs_create_group(wusbhc_kobj(wusbhc), &wusbhc_attr_group);
+       if (result < 0) {
+               dev_err(dev, "Cannot register WUSBHC attributes: %d\n", result);
+               goto error_create_attr_group;
+       }
+       /* Yep, I plan to add stuff here... */
+error_create_attr_group:
+       return result;
+}
+EXPORT_SYMBOL_GPL(wusbhc_b_create);
+
+void wusbhc_b_destroy(struct wusbhc *wusbhc)
+{
+       sysfs_remove_group(wusbhc_kobj(wusbhc), &wusbhc_attr_group);
+}
+EXPORT_SYMBOL_GPL(wusbhc_b_destroy);
+
+void wusbhc_destroy(struct wusbhc *wusbhc)
+{
+       wusbhc_pal_unregister(wusbhc);
+       wusbhc_sec_destroy(wusbhc);
+       wusbhc_rh_destroy(wusbhc);
+       wusbhc_devconnect_destroy(wusbhc);
+       wusbhc_mmcie_destroy(wusbhc);
+}
+EXPORT_SYMBOL_GPL(wusbhc_destroy);
+
+struct workqueue_struct *wusbd;
+EXPORT_SYMBOL_GPL(wusbd);
+
+/*
+ * WUSB Cluster ID allocation map
+ *
+ * Each WUSB bus in a channel is identified with a Cluster Id in the
+ * unauth address pace (WUSB1.0[4.3]). We take the range 0xe0 to 0xff
+ * (that's space for 31 WUSB controllers, as 0xff can't be taken). We
+ * start taking from 0xff, 0xfe, 0xfd... (hence the += or -= 0xff).
+ *
+ * For each one we taken, we pin it in the bitap
+ */
+#define CLUSTER_IDS 32
+static DECLARE_BITMAP(wusb_cluster_id_table, CLUSTER_IDS);
+static DEFINE_SPINLOCK(wusb_cluster_ids_lock);
+
+/*
+ * Get a WUSB Cluster ID
+ *
+ * Need to release with wusb_cluster_id_put() when done w/ it.
+ */
+/* FIXME: coordinate with the choose_addres() from the USB stack */
+/* we want to leave the top of the 128 range for cluster addresses and
+ * the bottom for device addresses (as we map them one on one with
+ * ports). */
+u8 wusb_cluster_id_get(void)
+{
+       u8 id;
+       spin_lock(&wusb_cluster_ids_lock);
+       id = find_first_zero_bit(wusb_cluster_id_table, CLUSTER_IDS);
+       if (id > CLUSTER_IDS) {
+               id = 0;
+               goto out;
+       }
+       set_bit(id, wusb_cluster_id_table);
+       id = (u8) 0xff - id;
+out:
+       spin_unlock(&wusb_cluster_ids_lock);
+       return id;
+
+}
+EXPORT_SYMBOL_GPL(wusb_cluster_id_get);
+
+/*
+ * Release a WUSB Cluster ID
+ *
+ * Obtained it with wusb_cluster_id_get()
+ */
+void wusb_cluster_id_put(u8 id)
+{
+       id = 0xff - id;
+       BUG_ON(id >= CLUSTER_IDS);
+       spin_lock(&wusb_cluster_ids_lock);
+       WARN_ON(!test_bit(id, wusb_cluster_id_table));
+       clear_bit(id, wusb_cluster_id_table);
+       spin_unlock(&wusb_cluster_ids_lock);
+}
+EXPORT_SYMBOL_GPL(wusb_cluster_id_put);
+
+/**
+ * wusbhc_giveback_urb - return an URB to the USB core
+ * @wusbhc: the host controller the URB is from.
+ * @urb:    the URB.
+ * @status: the URB's status.
+ *
+ * Return an URB to the USB core doing some additional WUSB specific
+ * processing.
+ *
+ *  - After a successful transfer, update the trust timeout timestamp
+ *    for the WUSB device.
+ *
+ *  - [WUSB] sections 4.13 and 7.5.1 specifies the stop retrasmittion
+ *    condition for the WCONNECTACK_IE is that the host has observed
+ *    the associated device responding to a control transfer.
+ */
+void wusbhc_giveback_urb(struct wusbhc *wusbhc, struct urb *urb, int status)
+{
+       struct wusb_dev *wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, urb->dev);
+
+       if (status == 0) {
+               wusb_dev->entry_ts = jiffies;
+
+               /* wusbhc_devconnect_acked() can't be called from from
+                  atomic context so defer it to a work queue. */
+               if (!list_empty(&wusb_dev->cack_node))
+                       queue_work(wusbd, &wusb_dev->devconnect_acked_work);
+       }
+
+       usb_hcd_giveback_urb(&wusbhc->usb_hcd, urb, status);
+}
+EXPORT_SYMBOL_GPL(wusbhc_giveback_urb);
+
+/**
+ * wusbhc_reset_all - reset the HC hardware
+ * @wusbhc: the host controller to reset.
+ *
+ * Request a full hardware reset of the chip.  This will also reset
+ * the radio controller and any other PALs.
+ */
+void wusbhc_reset_all(struct wusbhc *wusbhc)
+{
+       uwb_rc_reset_all(wusbhc->uwb_rc);
+}
+EXPORT_SYMBOL_GPL(wusbhc_reset_all);
+
+static struct notifier_block wusb_usb_notifier = {
+       .notifier_call = wusb_usb_ncb,
+       .priority = INT_MAX     /* Need to be called first of all */
+};
+
+static int __init wusbcore_init(void)
+{
+       int result;
+       result = wusb_crypto_init();
+       if (result < 0)
+               goto error_crypto_init;
+       /* WQ is singlethread because we need to serialize notifications */
+       wusbd = create_singlethread_workqueue("wusbd");
+       if (wusbd == NULL) {
+               result = -ENOMEM;
+               printk(KERN_ERR "WUSB-core: Cannot create wusbd workqueue\n");
+               goto error_wusbd_create;
+       }
+       usb_register_notify(&wusb_usb_notifier);
+       bitmap_zero(wusb_cluster_id_table, CLUSTER_IDS);
+       set_bit(0, wusb_cluster_id_table);      /* reserve Cluster ID 0xff */
+       return 0;
+
+error_wusbd_create:
+       wusb_crypto_exit();
+error_crypto_init:
+       return result;
+
+}
+module_init(wusbcore_init);
+
+static void __exit wusbcore_exit(void)
+{
+       clear_bit(0, wusb_cluster_id_table);
+       if (!bitmap_empty(wusb_cluster_id_table, CLUSTER_IDS)) {
+               char buf[256];
+               bitmap_scnprintf(buf, sizeof(buf), wusb_cluster_id_table,
+                                CLUSTER_IDS);
+               printk(KERN_ERR "BUG: WUSB Cluster IDs not released "
+                      "on exit: %s\n", buf);
+               WARN_ON(1);
+       }
+       usb_unregister_notify(&wusb_usb_notifier);
+       destroy_workqueue(wusbd);
+       wusb_crypto_exit();
+}
+module_exit(wusbcore_exit);
+
+MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
+MODULE_DESCRIPTION("Wireless USB core");
+MODULE_LICENSE("GPL");
diff --git a/drivers/usb/wusbcore/wusbhc.h b/drivers/usb/wusbcore/wusbhc.h
new file mode 100644 (file)
index 0000000..d0c1324
--- /dev/null
@@ -0,0 +1,495 @@
+/*
+ * Wireless USB Host Controller
+ * Common infrastructure for WHCI and HWA WUSB-HC drivers
+ *
+ *
+ * Copyright (C) 2005-2006 Intel Corporation
+ * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ *
+ * This driver implements parts common to all Wireless USB Host
+ * Controllers (struct wusbhc, embedding a struct usb_hcd) and is used
+ * by:
+ *
+ *   - hwahc: HWA, USB-dongle that implements a Wireless USB host
+ *     controller, (Wireless USB 1.0 Host-Wire-Adapter specification).
+ *
+ *   - whci: WHCI, a PCI card with a wireless host controller
+ *     (Wireless Host Controller Interface 1.0 specification).
+ *
+ * Check out the Design-overview.txt file in the source documentation
+ * for other details on the implementation.
+ *
+ * Main blocks:
+ *
+ *  rh         Root Hub emulation (part of the HCD glue)
+ *
+ *  devconnect Handle all the issues related to device connection,
+ *             authentication, disconnection, timeout, reseting,
+ *             keepalives, etc.
+ *
+ *  mmc        MMC IE broadcasting handling
+ *
+ * A host controller driver just initializes its stuff and as part of
+ * that, creates a 'struct wusbhc' instance that handles all the
+ * common WUSB mechanisms. Links in the function ops that are specific
+ * to it and then registers the host controller. Ready to run.
+ */
+
+#ifndef __WUSBHC_H__
+#define __WUSBHC_H__
+
+#include <linux/usb.h>
+#include <linux/list.h>
+#include <linux/mutex.h>
+#include <linux/kref.h>
+#include <linux/workqueue.h>
+/* FIXME: Yes, I know: BAD--it's not my fault the USB HC iface is not
+ *        public */
+#include <linux/../../drivers/usb/core/hcd.h>
+#include <linux/uwb.h>
+#include <linux/usb/wusb.h>
+
+
+/**
+ * Wireless USB device
+ *
+ * Describe a WUSB device connected to the cluster. This struct
+ * belongs to the 'struct wusb_port' it is attached to and it is
+ * responsible for putting and clearing the pointer to it.
+ *
+ * Note this "complements" the 'struct usb_device' that the usb_hcd
+ * keeps for each connected USB device. However, it extends some
+ * information that is not available (there is no hcpriv ptr in it!)
+ * *and* most importantly, it's life cycle is different. It is created
+ * as soon as we get a DN_Connect (connect request notification) from
+ * the device through the WUSB host controller; the USB stack doesn't
+ * create the device until we authenticate it. FIXME: this will
+ * change.
+ *
+ * @bos:    This is allocated when the BOS descriptors are read from
+ *          the device and freed upon the wusb_dev struct dying.
+ * @wusb_cap_descr: points into @bos, and has been verified to be size
+ *                  safe.
+ */
+struct wusb_dev {
+       struct kref refcnt;
+       struct wusbhc *wusbhc;
+       struct list_head cack_node;     /* Connect-Ack list */
+       u8 port_idx;
+       u8 addr;
+       u8 beacon_type:4;
+       struct usb_encryption_descriptor ccm1_etd;
+       struct wusb_ckhdid cdid;
+       unsigned long entry_ts;
+       struct usb_bos_descriptor *bos;
+       struct usb_wireless_cap_descriptor *wusb_cap_descr;
+       struct uwb_mas_bm availability;
+       struct work_struct devconnect_acked_work;
+       struct urb *set_gtk_urb;
+       struct usb_ctrlrequest *set_gtk_req;
+       struct usb_device *usb_dev;
+};
+
+#define WUSB_DEV_ADDR_UNAUTH 0x80
+
+static inline void wusb_dev_init(struct wusb_dev *wusb_dev)
+{
+       kref_init(&wusb_dev->refcnt);
+       /* no need to init the cack_node */
+}
+
+extern void wusb_dev_destroy(struct kref *_wusb_dev);
+
+static inline struct wusb_dev *wusb_dev_get(struct wusb_dev *wusb_dev)
+{
+       kref_get(&wusb_dev->refcnt);
+       return wusb_dev;
+}
+
+static inline void wusb_dev_put(struct wusb_dev *wusb_dev)
+{
+       kref_put(&wusb_dev->refcnt, wusb_dev_destroy);
+}
+
+/**
+ * Wireless USB Host Controlller root hub "fake" ports
+ * (state and device information)
+ *
+ * Wireless USB is wireless, so there are no ports; but we
+ * fake'em. Each RC can connect a max of devices at the same time
+ * (given in the Wireless Adapter descriptor, bNumPorts or WHCI's
+ * caps), referred to in wusbhc->ports_max.
+ *
+ * See rh.c for more information.
+ *
+ * The @status and @change use the same bits as in USB2.0[11.24.2.7],
+ * so we don't have to do much when getting the port's status.
+ *
+ * WUSB1.0[7.1], USB2.0[11.24.2.7.1,fig 11-10],
+ * include/linux/usb_ch9.h (#define USB_PORT_STAT_*)
+ */
+struct wusb_port {
+       u16 status;
+       u16 change;
+       struct wusb_dev *wusb_dev;      /* connected device's info */
+       unsigned reset_count;
+       u32 ptk_tkid;
+};
+
+/**
+ * WUSB Host Controller specifics
+ *
+ * All fields that are common to all Wireless USB controller types
+ * (HWA and WHCI) are grouped here. Host Controller
+ * functions/operations that only deal with general Wireless USB HC
+ * issues use this data type to refer to the host.
+ *
+ * @usb_hcd       Instantiation of a USB host controller
+ *                 (initialized by upper layer [HWA=HC or WHCI].
+ *
+ * @dev                   Device that implements this; initialized by the
+ *                 upper layer (HWA-HC, WHCI...); this device should
+ *                 have a refcount.
+ *
+ * @trust_timeout  After this time without hearing for device
+ *                 activity, we consider the device gone and we have to
+ *                 re-authenticate.
+ *
+ *                 Can be accessed w/o locking--however, read to a
+ *                 local variable then use.
+ *
+ * @chid           WUSB Cluster Host ID: this is supposed to be a
+ *                 unique value that doesn't change across reboots (so
+ *                 that your devices do not require re-association).
+ *
+ *                 Read/Write protected by @mutex
+ *
+ * @dev_info       This array has ports_max elements. It is used to
+ *                 give the HC information about the WUSB devices (see
+ *                 'struct wusb_dev_info').
+ *
+ *                For HWA we need to allocate it in heap; for WHCI it
+ *                 needs to be permanently mapped, so we keep it for
+ *                 both and make it easy. Call wusbhc->dev_info_set()
+ *                 to update an entry.
+ *
+ * @ports_max     Number of simultaneous device connections (fake
+ *                 ports) this HC will take. Read-only.
+ *
+ * @port          Array of port status for each fake root port. Guaranteed to
+ *                 always be the same lenght during device existence
+ *                 [this allows for some unlocked but referenced reading].
+ *
+ * @mmcies_max    Max number of Information Elements this HC can send
+ *                 in its MMC. Read-only.
+ *
+ * @mmcie_add     HC specific operation (WHCI or HWA) for adding an
+ *                 MMCIE.
+ *
+ * @mmcie_rm      HC specific operation (WHCI or HWA) for removing an
+ *                 MMCIE.
+ *
+ * @enc_types     Array which describes the encryptions methods
+ *                 supported by the host as described in WUSB1.0 --
+ *                 one entry per supported method. As of WUSB1.0 there
+ *                 is only four methods, we make space for eight just in
+ *                 case they decide to add some more (and pray they do
+ *                 it in sequential order). if 'enc_types[enc_method]
+ *                 != 0', then it is supported by the host. enc_method
+ *                 is USB_ENC_TYPE*.
+ *
+ * @set_ptk:       Set the PTK and enable encryption for a device. Or, if
+ *                 the supplied key is NULL, disable encryption for that
+ *                 device.
+ *
+ * @set_gtk:       Set the GTK to be used for all future broadcast packets
+ *                 (i.e., MMCs).  With some hardware, setting the GTK may start
+ *                 MMC transmission.
+ *
+ * NOTE:
+ *
+ *  - If wusb_dev->usb_dev is not NULL, then usb_dev is valid
+ *    (wusb_dev has a refcount on it). Likewise, if usb_dev->wusb_dev
+ *    is not NULL, usb_dev->wusb_dev is valid (usb_dev keeps a
+ *    refcount on it).
+ *
+ *    Most of the times when you need to use it, it will be non-NULL,
+ *    so there is no real need to check for it (wusb_dev will
+ *    dissapear before usb_dev).
+ *
+ *  - The following fields need to be filled out before calling
+ *    wusbhc_create(): ports_max, mmcies_max, mmcie_{add,rm}.
+ *
+ *  - there is no wusbhc_init() method, we do everything in
+ *    wusbhc_create().
+ *
+ *  - Creation is done in two phases, wusbhc_create() and
+ *    wusbhc_create_b(); b are the parts that need to be called after
+ *    calling usb_hcd_add(&wusbhc->usb_hcd).
+ */
+struct wusbhc {
+       struct usb_hcd usb_hcd;         /* HAS TO BE 1st */
+       struct device *dev;
+       struct uwb_rc *uwb_rc;
+       struct uwb_pal pal;
+
+       unsigned trust_timeout;                 /* in jiffies */
+       struct wuie_host_info *wuie_host_info;  /* Includes CHID */
+
+       struct mutex mutex;                     /* locks everything else */
+       u16 cluster_id;                         /* Wireless USB Cluster ID */
+       struct wusb_port *port;                 /* Fake port status handling */
+       struct wusb_dev_info *dev_info;         /* for Set Device Info mgmt */
+       u8 ports_max;
+       unsigned active:1;                      /* currently xmit'ing MMCs */
+       struct wuie_keep_alive keep_alive_ie;   /* protected by mutex */
+       struct delayed_work keep_alive_timer;
+       struct list_head cack_list;             /* Connect acknowledging */
+       size_t cack_count;                      /* protected by 'mutex' */
+       struct wuie_connect_ack cack_ie;
+       struct uwb_rsv *rsv;            /* cluster bandwidth reservation */
+
+       struct mutex mmcie_mutex;               /* MMC WUIE handling */
+       struct wuie_hdr **mmcie;                /* WUIE array */
+       u8 mmcies_max;
+       /* FIXME: make wusbhc_ops? */
+       int (*start)(struct wusbhc *wusbhc);
+       void (*stop)(struct wusbhc *wusbhc);
+       int (*mmcie_add)(struct wusbhc *wusbhc, u8 interval, u8 repeat_cnt,
+                        u8 handle, struct wuie_hdr *wuie);
+       int (*mmcie_rm)(struct wusbhc *wusbhc, u8 handle);
+       int (*dev_info_set)(struct wusbhc *, struct wusb_dev *wusb_dev);
+       int (*bwa_set)(struct wusbhc *wusbhc, s8 stream_index,
+                      const struct uwb_mas_bm *);
+       int (*set_ptk)(struct wusbhc *wusbhc, u8 port_idx,
+                      u32 tkid, const void *key, size_t key_size);
+       int (*set_gtk)(struct wusbhc *wusbhc,
+                      u32 tkid, const void *key, size_t key_size);
+       int (*set_num_dnts)(struct wusbhc *wusbhc, u8 interval, u8 slots);
+
+       struct {
+               struct usb_key_descriptor descr;
+               u8 data[16];                            /* GTK key data */
+       } __attribute__((packed)) gtk;
+       u8 gtk_index;
+       u32 gtk_tkid;
+       struct work_struct gtk_rekey_done_work;
+       int pending_set_gtks;
+
+       struct usb_encryption_descriptor *ccm1_etd;
+};
+
+#define usb_hcd_to_wusbhc(u) container_of((u), struct wusbhc, usb_hcd)
+
+
+extern int wusbhc_create(struct wusbhc *);
+extern int wusbhc_b_create(struct wusbhc *);
+extern void wusbhc_b_destroy(struct wusbhc *);
+extern void wusbhc_destroy(struct wusbhc *);
+extern int wusb_dev_sysfs_add(struct wusbhc *, struct usb_device *,
+                             struct wusb_dev *);
+extern void wusb_dev_sysfs_rm(struct wusb_dev *);
+extern int wusbhc_sec_create(struct wusbhc *);
+extern int wusbhc_sec_start(struct wusbhc *);
+extern void wusbhc_sec_stop(struct wusbhc *);
+extern void wusbhc_sec_destroy(struct wusbhc *);
+extern void wusbhc_giveback_urb(struct wusbhc *wusbhc, struct urb *urb,
+                               int status);
+void wusbhc_reset_all(struct wusbhc *wusbhc);
+
+int wusbhc_pal_register(struct wusbhc *wusbhc);
+void wusbhc_pal_unregister(struct wusbhc *wusbhc);
+
+/*
+ * Return @usb_dev's @usb_hcd (properly referenced) or NULL if gone
+ *
+ * @usb_dev: USB device, UNLOCKED and referenced (or otherwise, safe ptr)
+ *
+ * This is a safe assumption as @usb_dev->bus is referenced all the
+ * time during the @usb_dev life cycle.
+ */
+static inline struct usb_hcd *usb_hcd_get_by_usb_dev(struct usb_device *usb_dev)
+{
+       struct usb_hcd *usb_hcd;
+       usb_hcd = container_of(usb_dev->bus, struct usb_hcd, self);
+       return usb_get_hcd(usb_hcd);
+}
+
+/*
+ * Increment the reference count on a wusbhc.
+ *
+ * @wusbhc's life cycle is identical to that of the underlying usb_hcd.
+ */
+static inline struct wusbhc *wusbhc_get(struct wusbhc *wusbhc)
+{
+       return usb_get_hcd(&wusbhc->usb_hcd) ? wusbhc : NULL;
+}
+
+/*
+ * Return the wusbhc associated to a @usb_dev
+ *
+ * @usb_dev: USB device, UNLOCKED and referenced (or otherwise, safe ptr)
+ *
+ * @returns: wusbhc for @usb_dev; NULL if the @usb_dev is being torn down.
+ *           WARNING: referenced at the usb_hcd level, unlocked
+ *
+ * FIXME: move offline
+ */
+static inline struct wusbhc *wusbhc_get_by_usb_dev(struct usb_device *usb_dev)
+{
+       struct wusbhc *wusbhc = NULL;
+       struct usb_hcd *usb_hcd;
+       if (usb_dev->devnum > 1 && !usb_dev->wusb) {
+               /* but root hubs */
+               dev_err(&usb_dev->dev, "devnum %d wusb %d\n", usb_dev->devnum,
+                       usb_dev->wusb);
+               BUG_ON(usb_dev->devnum > 1 && !usb_dev->wusb);
+       }
+       usb_hcd = usb_hcd_get_by_usb_dev(usb_dev);
+       if (usb_hcd == NULL)
+               return NULL;
+       BUG_ON(usb_hcd->wireless == 0);
+       return wusbhc = usb_hcd_to_wusbhc(usb_hcd);
+}
+
+
+static inline void wusbhc_put(struct wusbhc *wusbhc)
+{
+       usb_put_hcd(&wusbhc->usb_hcd);
+}
+
+int wusbhc_start(struct wusbhc *wusbhc, const struct wusb_ckhdid *chid);
+void wusbhc_stop(struct wusbhc *wusbhc);
+extern int wusbhc_chid_set(struct wusbhc *, const struct wusb_ckhdid *);
+
+/* Device connect handling */
+extern int wusbhc_devconnect_create(struct wusbhc *);
+extern void wusbhc_devconnect_destroy(struct wusbhc *);
+extern int wusbhc_devconnect_start(struct wusbhc *wusbhc,
+                                  const struct wusb_ckhdid *chid);
+extern void wusbhc_devconnect_stop(struct wusbhc *wusbhc);
+extern int wusbhc_devconnect_auth(struct wusbhc *, u8);
+extern void wusbhc_handle_dn(struct wusbhc *, u8 srcaddr,
+                            struct wusb_dn_hdr *dn_hdr, size_t size);
+extern int wusbhc_dev_reset(struct wusbhc *wusbhc, u8 port);
+extern void __wusbhc_dev_disable(struct wusbhc *wusbhc, u8 port);
+extern int wusb_usb_ncb(struct notifier_block *nb, unsigned long val,
+                       void *priv);
+extern int wusb_set_dev_addr(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev,
+                            u8 addr);
+
+/* Wireless USB fake Root Hub methods */
+extern int wusbhc_rh_create(struct wusbhc *);
+extern void wusbhc_rh_destroy(struct wusbhc *);
+
+extern int wusbhc_rh_status_data(struct usb_hcd *, char *);
+extern int wusbhc_rh_control(struct usb_hcd *, u16, u16, u16, char *, u16);
+extern int wusbhc_rh_suspend(struct usb_hcd *);
+extern int wusbhc_rh_resume(struct usb_hcd *);
+extern int wusbhc_rh_start_port_reset(struct usb_hcd *, unsigned);
+
+/* MMC handling */
+extern int wusbhc_mmcie_create(struct wusbhc *);
+extern void wusbhc_mmcie_destroy(struct wusbhc *);
+extern int wusbhc_mmcie_set(struct wusbhc *, u8 interval, u8 repeat_cnt,
+                           struct wuie_hdr *);
+extern void wusbhc_mmcie_rm(struct wusbhc *, struct wuie_hdr *);
+
+/* Bandwidth reservation */
+int wusbhc_rsv_establish(struct wusbhc *wusbhc);
+void wusbhc_rsv_terminate(struct wusbhc *wusbhc);
+
+/*
+ * I've always said
+ * I wanted a wedding in a church...
+ *
+ * but lately I've been thinking about
+ * the Botanical Gardens.
+ *
+ * We could do it by the tulips.
+ * It'll be beautiful
+ *
+ * --Security!
+ */
+extern int wusb_dev_sec_add(struct wusbhc *, struct usb_device *,
+                               struct wusb_dev *);
+extern void wusb_dev_sec_rm(struct wusb_dev *) ;
+extern int wusb_dev_4way_handshake(struct wusbhc *, struct wusb_dev *,
+                                  struct wusb_ckhdid *ck);
+void wusbhc_gtk_rekey(struct wusbhc *wusbhc);
+
+
+/* WUSB Cluster ID handling */
+extern u8 wusb_cluster_id_get(void);
+extern void wusb_cluster_id_put(u8);
+
+/*
+ * wusb_port_by_idx - return the port associated to a zero-based port index
+ *
+ * NOTE: valid without locking as long as wusbhc is referenced (as the
+ *       number of ports doesn't change). The data pointed to has to
+ *       be verified though :)
+ */
+static inline struct wusb_port *wusb_port_by_idx(struct wusbhc *wusbhc,
+                                                u8 port_idx)
+{
+       return &wusbhc->port[port_idx];
+}
+
+/*
+ * wusb_port_no_to_idx - Convert port number (per usb_dev->portnum) to
+ * a port_idx.
+ *
+ * USB stack USB ports are 1 based!!
+ *
+ * NOTE: only valid for WUSB devices!!!
+ */
+static inline u8 wusb_port_no_to_idx(u8 port_no)
+{
+       return port_no - 1;
+}
+
+extern struct wusb_dev *__wusb_dev_get_by_usb_dev(struct wusbhc *,
+                                                 struct usb_device *);
+
+/*
+ * Return a referenced wusb_dev given a @usb_dev
+ *
+ * Returns NULL if the usb_dev is being torn down.
+ *
+ * FIXME: move offline
+ */
+static inline
+struct wusb_dev *wusb_dev_get_by_usb_dev(struct usb_device *usb_dev)
+{
+       struct wusbhc *wusbhc;
+       struct wusb_dev *wusb_dev;
+       wusbhc = wusbhc_get_by_usb_dev(usb_dev);
+       if (wusbhc == NULL)
+               return NULL;
+       mutex_lock(&wusbhc->mutex);
+       wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, usb_dev);
+       mutex_unlock(&wusbhc->mutex);
+       wusbhc_put(wusbhc);
+       return wusb_dev;
+}
+
+/* Misc */
+
+extern struct workqueue_struct *wusbd;
+#endif /* #ifndef __WUSBHC_H__ */