| 1 | /*
|
|---|
| 2 | * Copyright (c) 2017 Ondrej Hlavaty <aearsis@eideo.cz>
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 | /** @addtogroup libusbhost
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * HC Endpoint management.
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <usb/host/utility.h>
|
|---|
| 36 | #include <usb/host/ddf_helpers.h>
|
|---|
| 37 | #include <usb/host/endpoint.h>
|
|---|
| 38 | #include <usb/host/hcd.h>
|
|---|
| 39 | #include <usb/host/utility.h>
|
|---|
| 40 | #include <usb/classes/classes.h>
|
|---|
| 41 | #include <usb/classes/hub.h>
|
|---|
| 42 | #include <usb/descriptor.h>
|
|---|
| 43 | #include <usb/debug.h>
|
|---|
| 44 |
|
|---|
| 45 | #include <assert.h>
|
|---|
| 46 | #include <errno.h>
|
|---|
| 47 | #include <str_error.h>
|
|---|
| 48 | #include <macros.h>
|
|---|
| 49 | #include <stdbool.h>
|
|---|
| 50 |
|
|---|
| 51 | #include "hc.h"
|
|---|
| 52 | #include "bus.h"
|
|---|
| 53 | #include "endpoint.h"
|
|---|
| 54 | #include "hw_struct/context.h"
|
|---|
| 55 |
|
|---|
| 56 | #include "device.h"
|
|---|
| 57 |
|
|---|
| 58 | /** Initial descriptor used for control endpoint 0 before more configuration is retrieved. */
|
|---|
| 59 | static const usb_endpoint_descriptors_t ep0_initial_desc = {
|
|---|
| 60 | .endpoint.max_packet_size = CTRL_PIPE_MIN_PACKET_SIZE,
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * Assign address and control endpoint to a new XHCI device. Once this function
|
|---|
| 65 | * successfully returns, the device is online.
|
|---|
| 66 | *
|
|---|
| 67 | * @param[in] bus XHCI bus, in which the address is assigned.
|
|---|
| 68 | * @param[in] dev New device to address and configure./e
|
|---|
| 69 | * @return Error code.
|
|---|
| 70 | */
|
|---|
| 71 | static int address_device(xhci_device_t *dev)
|
|---|
| 72 | {
|
|---|
| 73 | int err;
|
|---|
| 74 |
|
|---|
| 75 | /* Enable new slot. */
|
|---|
| 76 | if ((err = hc_enable_slot(dev)) != EOK)
|
|---|
| 77 | return err;
|
|---|
| 78 | usb_log_debug2("Obtained slot ID: %u.", dev->slot_id);
|
|---|
| 79 |
|
|---|
| 80 | /* Create and configure control endpoint. */
|
|---|
| 81 | endpoint_t *ep0_base = xhci_endpoint_create(&dev->base, &ep0_initial_desc);
|
|---|
| 82 | if (!ep0_base)
|
|---|
| 83 | goto err_slot;
|
|---|
| 84 |
|
|---|
| 85 | /* Bus reference */
|
|---|
| 86 | endpoint_add_ref(ep0_base);
|
|---|
| 87 | dev->base.endpoints[0] = ep0_base;
|
|---|
| 88 |
|
|---|
| 89 | usb_log_debug2("Looking up new device initial speed: %i", dev->base.speed);
|
|---|
| 90 | ep0_base->max_packet_size = hc_get_ep0_initial_mps(dev->base.speed);
|
|---|
| 91 |
|
|---|
| 92 | /* Address device */
|
|---|
| 93 | if ((err = hc_address_device(dev)))
|
|---|
| 94 | goto err_added;
|
|---|
| 95 |
|
|---|
| 96 | return EOK;
|
|---|
| 97 |
|
|---|
| 98 | err_added:
|
|---|
| 99 | /* Bus reference */
|
|---|
| 100 | endpoint_del_ref(ep0_base);
|
|---|
| 101 | dev->base.endpoints[0] = NULL;
|
|---|
| 102 | err_slot:
|
|---|
| 103 | hc_disable_slot(dev);
|
|---|
| 104 | return err;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | /**
|
|---|
| 108 | * Retrieve and set maximum packet size for endpoint zero of a XHCI device.
|
|---|
| 109 | *
|
|---|
| 110 | * @param[in] hc Host controller, which manages the device.
|
|---|
| 111 | * @param[in] dev Device with operational endpoint zero.
|
|---|
| 112 | * @return Error code.
|
|---|
| 113 | */
|
|---|
| 114 | static int setup_ep0_packet_size(xhci_hc_t *hc, xhci_device_t *dev)
|
|---|
| 115 | {
|
|---|
| 116 | int err;
|
|---|
| 117 |
|
|---|
| 118 | uint16_t max_packet_size;
|
|---|
| 119 | if ((err = hc_get_ep0_max_packet_size(&max_packet_size, (bus_t *) &hc->bus, &dev->base)))
|
|---|
| 120 | return err;
|
|---|
| 121 |
|
|---|
| 122 | xhci_endpoint_t *ep0 = xhci_endpoint_get(dev->base.endpoints[0]);
|
|---|
| 123 | assert(ep0);
|
|---|
| 124 |
|
|---|
| 125 | if (ep0->base.max_packet_size == max_packet_size)
|
|---|
| 126 | return EOK;
|
|---|
| 127 |
|
|---|
| 128 | ep0->base.max_packet_size = max_packet_size;
|
|---|
| 129 | ep0->base.max_transfer_size = max_packet_size * ep0->base.packets_per_uframe;
|
|---|
| 130 |
|
|---|
| 131 | if ((err = hc_update_endpoint(ep0)))
|
|---|
| 132 | return err;
|
|---|
| 133 |
|
|---|
| 134 | return EOK;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | /**
|
|---|
| 138 | * Check whether the device is a hub and if so, fill its characterstics.
|
|---|
| 139 | *
|
|---|
| 140 | * If this fails, it does not necessarily mean the device is unusable.
|
|---|
| 141 | * Just the TT will not work correctly.
|
|---|
| 142 | */
|
|---|
| 143 | static int setup_hub(xhci_device_t *dev, usb_standard_device_descriptor_t *desc)
|
|---|
| 144 | {
|
|---|
| 145 | if (desc->device_class != USB_CLASS_HUB)
|
|---|
| 146 | return EOK;
|
|---|
| 147 |
|
|---|
| 148 | usb_hub_descriptor_header_t hub_desc = { 0 };
|
|---|
| 149 | const int err = hc_get_hub_desc(&dev->base, &hub_desc);
|
|---|
| 150 | if (err)
|
|---|
| 151 | return err;
|
|---|
| 152 |
|
|---|
| 153 | dev->is_hub = 1;
|
|---|
| 154 | dev->num_ports = hub_desc.port_count;
|
|---|
| 155 |
|
|---|
| 156 | if (dev->base.speed == USB_SPEED_HIGH) {
|
|---|
| 157 | dev->tt_think_time = 8 +
|
|---|
| 158 | 8 * !!(hub_desc.characteristics & HUB_CHAR_TT_THINK_8) +
|
|---|
| 159 | 16 * !!(hub_desc.characteristics & HUB_CHAR_TT_THINK_16);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | usb_log_debug2("Device(%u): recognised USB hub with %u ports", dev->base.address, dev->num_ports);
|
|---|
| 163 | return EOK;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /**
|
|---|
| 167 | * Respond to a new device on the XHCI bus. Address it, negotiate packet size
|
|---|
| 168 | * and retrieve USB descriptors.
|
|---|
| 169 | *
|
|---|
| 170 | * @param[in] bus XHCI bus, where the new device emerged.
|
|---|
| 171 | * @param[in] dev XHCI device, which has appeared on the bus.
|
|---|
| 172 | *
|
|---|
| 173 | * @return Error code.
|
|---|
| 174 | */
|
|---|
| 175 | int xhci_device_enumerate(device_t *dev)
|
|---|
| 176 | {
|
|---|
| 177 | int err;
|
|---|
| 178 | xhci_bus_t *bus = bus_to_xhci_bus(dev->bus);
|
|---|
| 179 | xhci_device_t *xhci_dev = xhci_device_get(dev);
|
|---|
| 180 |
|
|---|
| 181 | /* Calculate route string */
|
|---|
| 182 | xhci_device_t *xhci_hub = xhci_device_get(dev->hub);
|
|---|
| 183 | xhci_dev->route_str = xhci_hub->route_str;
|
|---|
| 184 |
|
|---|
| 185 | /* Roothub port is not part of the route string */
|
|---|
| 186 | if (dev->tier >= 2) {
|
|---|
| 187 | const unsigned offset = 4 * (dev->tier - 2);
|
|---|
| 188 | xhci_dev->route_str |= (dev->port & 0xf) << offset;
|
|---|
| 189 | xhci_dev->rh_port = xhci_hub->rh_port;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | int retries = 3;
|
|---|
| 193 | do {
|
|---|
| 194 | /* Assign an address to the device */
|
|---|
| 195 | err = address_device(xhci_dev);
|
|---|
| 196 | } while (err == ESTALL && --retries > 0);
|
|---|
| 197 |
|
|---|
| 198 | if (err) {
|
|---|
| 199 | usb_log_error("Failed to setup address of the new device: %s", str_error(err));
|
|---|
| 200 | return err;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | /* Setup EP0 might already need to issue a transfer. */
|
|---|
| 204 | fibril_mutex_lock(&bus->base.guard);
|
|---|
| 205 | assert(bus->devices_by_slot[xhci_dev->slot_id] == NULL);
|
|---|
| 206 | bus->devices_by_slot[xhci_dev->slot_id] = xhci_dev;
|
|---|
| 207 | fibril_mutex_unlock(&bus->base.guard);
|
|---|
| 208 |
|
|---|
| 209 | if ((err = setup_ep0_packet_size(bus->hc, xhci_dev))) {
|
|---|
| 210 | usb_log_error("Failed to setup control endpoint of the new device: %s", str_error(err));
|
|---|
| 211 | goto err_address;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | usb_standard_device_descriptor_t desc = { 0 };
|
|---|
| 215 |
|
|---|
| 216 | if ((err = hc_get_device_desc(dev, &desc))) {
|
|---|
| 217 | usb_log_error("Device(%d): failed to get devices descriptor: %s", dev->address, str_error(err));
|
|---|
| 218 | goto err_address;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | if ((err = setup_hub(xhci_dev, &desc)))
|
|---|
| 222 | usb_log_warning("Device(%d): failed to setup hub characteristics: %s. "
|
|---|
| 223 | " Continuing anyway.", dev->address, str_error(err));
|
|---|
| 224 |
|
|---|
| 225 | if ((err = hcd_ddf_setup_match_ids(dev, &desc))) {
|
|---|
| 226 | usb_log_error("Device(%d): failed to setup match IDs: %s", dev->address, str_error(err));
|
|---|
| 227 | goto err_address;
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | return EOK;
|
|---|
| 231 |
|
|---|
| 232 | err_address:
|
|---|
| 233 | return err;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | /**
|
|---|
| 237 | * Remove device from XHCI bus. Transition it to the offline state, abort all
|
|---|
| 238 | * ongoing transfers and unregister all of its endpoints.
|
|---|
| 239 | *
|
|---|
| 240 | * Bus callback.
|
|---|
| 241 | *
|
|---|
| 242 | * @param[in] bus XHCI bus, from which the device is removed.
|
|---|
| 243 | * @param[in] dev XHCI device, which is removed from the bus.
|
|---|
| 244 | * @return Error code.
|
|---|
| 245 | */
|
|---|
| 246 | void xhci_device_gone(device_t *dev)
|
|---|
| 247 | {
|
|---|
| 248 | int err;
|
|---|
| 249 | xhci_bus_t *bus = bus_to_xhci_bus(dev->bus);
|
|---|
| 250 | xhci_device_t *xhci_dev = xhci_device_get(dev);
|
|---|
| 251 |
|
|---|
| 252 | /* Disable the slot, dropping all endpoints. */
|
|---|
| 253 | const uint32_t slot_id = xhci_dev->slot_id;
|
|---|
| 254 | if ((err = hc_disable_slot(xhci_dev))) {
|
|---|
| 255 | usb_log_warning("Failed to disable slot of device " XHCI_DEV_FMT ": %s",
|
|---|
| 256 | XHCI_DEV_ARGS(*xhci_dev), str_error(err));
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | bus->devices_by_slot[slot_id] = NULL;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | /**
|
|---|
| 263 | * Reverts things device_offline did, getting the device back up.
|
|---|
| 264 | *
|
|---|
| 265 | * Bus callback.
|
|---|
| 266 | */
|
|---|
| 267 | int xhci_device_online(device_t *dev_base)
|
|---|
| 268 | {
|
|---|
| 269 | int err;
|
|---|
| 270 |
|
|---|
| 271 | xhci_bus_t *bus = bus_to_xhci_bus(dev_base->bus);
|
|---|
| 272 | assert(bus);
|
|---|
| 273 |
|
|---|
| 274 | xhci_device_t *dev = xhci_device_get(dev_base);
|
|---|
| 275 | assert(dev);
|
|---|
| 276 |
|
|---|
| 277 | /* Transition the device from the Addressed to the Configured state. */
|
|---|
| 278 | if ((err = hc_configure_device(dev))) {
|
|---|
| 279 | usb_log_warning("Failed to configure device " XHCI_DEV_FMT ".", XHCI_DEV_ARGS(*dev));
|
|---|
| 280 | return err;
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | return EOK;
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | /**
|
|---|
| 287 | * Make given device offline. Offline the DDF function, tear down all
|
|---|
| 288 | * endpoints, issue Deconfigure Device command to xHC.
|
|---|
| 289 | *
|
|---|
| 290 | * Bus callback.
|
|---|
| 291 | */
|
|---|
| 292 | void xhci_device_offline(device_t *dev_base)
|
|---|
| 293 | {
|
|---|
| 294 | int err;
|
|---|
| 295 |
|
|---|
| 296 | xhci_bus_t *bus = bus_to_xhci_bus(dev_base->bus);
|
|---|
| 297 | assert(bus);
|
|---|
| 298 |
|
|---|
| 299 | xhci_device_t *dev = xhci_device_get(dev_base);
|
|---|
| 300 | assert(dev);
|
|---|
| 301 |
|
|---|
| 302 | /* Issue one HC command to simultaneously drop all endpoints except zero. */
|
|---|
| 303 | if ((err = hc_deconfigure_device(dev))) {
|
|---|
| 304 | usb_log_warning("Failed to deconfigure device " XHCI_DEV_FMT ".",
|
|---|
| 305 | XHCI_DEV_ARGS(*dev));
|
|---|
| 306 | }
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | /**
|
|---|
| 310 | * Fill a slot context that is part of an Input Context with appropriate
|
|---|
| 311 | * values.
|
|---|
| 312 | *
|
|---|
| 313 | * @param ctx Slot context, zeroed out.
|
|---|
| 314 | */
|
|---|
| 315 | void xhci_setup_slot_context(xhci_device_t *dev, xhci_slot_ctx_t *ctx)
|
|---|
| 316 | {
|
|---|
| 317 | /* Initialize slot_ctx according to section 4.3.3 point 3. */
|
|---|
| 318 | XHCI_SLOT_ROOT_HUB_PORT_SET(*ctx, dev->rh_port);
|
|---|
| 319 | XHCI_SLOT_ROUTE_STRING_SET(*ctx, dev->route_str);
|
|---|
| 320 | XHCI_SLOT_SPEED_SET(*ctx, hc_speed_to_psiv(dev->base.speed));
|
|---|
| 321 |
|
|---|
| 322 | /*
|
|---|
| 323 | * Note: This function is used even before this flag can be set, to
|
|---|
| 324 | * issue the address device command. It is OK, because these
|
|---|
| 325 | * flags are not required to be valid for that command.
|
|---|
| 326 | */
|
|---|
| 327 | if (dev->is_hub) {
|
|---|
| 328 | XHCI_SLOT_HUB_SET(*ctx, 1);
|
|---|
| 329 | XHCI_SLOT_NUM_PORTS_SET(*ctx, dev->num_ports);
|
|---|
| 330 | XHCI_SLOT_TT_THINK_TIME_SET(*ctx, dev->tt_think_time);
|
|---|
| 331 | XHCI_SLOT_MTT_SET(*ctx, 0); // MTT not supported yet
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | /* Setup Transaction Translation. TODO: Test this with HS hub. */
|
|---|
| 335 | if (dev->base.tt.dev != NULL) {
|
|---|
| 336 | xhci_device_t *hub = xhci_device_get(dev->base.tt.dev);
|
|---|
| 337 | XHCI_SLOT_TT_HUB_SLOT_ID_SET(*ctx, hub->slot_id);
|
|---|
| 338 | XHCI_SLOT_TT_HUB_PORT_SET(*ctx, dev->base.tt.port);
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | // As we always allocate space for whole input context, we can set this to maximum
|
|---|
| 342 | XHCI_SLOT_CTX_ENTRIES_SET(*ctx, 31);
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 |
|
|---|
| 346 | /**
|
|---|
| 347 | * @}
|
|---|
| 348 | */
|
|---|