| 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 | /**
|
|---|
| 59 | * Initial descriptor used for control endpoint 0,
|
|---|
| 60 | * before more configuration is retrieved.
|
|---|
| 61 | */
|
|---|
| 62 | static const usb_endpoint_descriptors_t ep0_initial_desc = {
|
|---|
| 63 | .endpoint.max_packet_size = CTRL_PIPE_MIN_PACKET_SIZE,
|
|---|
| 64 | };
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * Assign address and control endpoint to a new XHCI device. Once this function
|
|---|
| 68 | * successfully returns, the device is online.
|
|---|
| 69 | *
|
|---|
| 70 | * @param[in] bus XHCI bus, in which the address is assigned.
|
|---|
| 71 | * @param[in] dev New device to address and configure./e
|
|---|
| 72 | * @return Error code.
|
|---|
| 73 | */
|
|---|
| 74 | static int address_device(xhci_device_t *dev)
|
|---|
| 75 | {
|
|---|
| 76 | int err;
|
|---|
| 77 |
|
|---|
| 78 | /* Enable new slot. */
|
|---|
| 79 | if ((err = hc_enable_slot(dev)) != EOK)
|
|---|
| 80 | return err;
|
|---|
| 81 | usb_log_debug("Obtained slot ID: %u.", dev->slot_id);
|
|---|
| 82 |
|
|---|
| 83 | endpoint_t *ep0_base;
|
|---|
| 84 | if ((err = bus_endpoint_add(&dev->base, &ep0_initial_desc, &ep0_base)))
|
|---|
| 85 | goto err_slot;
|
|---|
| 86 |
|
|---|
| 87 | usb_log_debug("Looking up new device initial MPS: %s",
|
|---|
| 88 | usb_str_speed(dev->base.speed));
|
|---|
| 89 | ep0_base->max_packet_size = hc_get_ep0_initial_mps(dev->base.speed);
|
|---|
| 90 |
|
|---|
| 91 | /* Address device */
|
|---|
| 92 | if ((err = hc_address_device(dev)))
|
|---|
| 93 | goto err_added;
|
|---|
| 94 |
|
|---|
| 95 | return EOK;
|
|---|
| 96 |
|
|---|
| 97 | err_added:
|
|---|
| 98 | /* Bus reference */
|
|---|
| 99 | endpoint_del_ref(ep0_base);
|
|---|
| 100 | dev->base.endpoints[0] = NULL;
|
|---|
| 101 | err_slot:
|
|---|
| 102 | hc_disable_slot(dev);
|
|---|
| 103 | return err;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /**
|
|---|
| 107 | * Retrieve and set maximum packet size for endpoint zero of a XHCI device.
|
|---|
| 108 | *
|
|---|
| 109 | * @param[in] hc Host controller, which manages the device.
|
|---|
| 110 | * @param[in] dev Device with operational endpoint zero.
|
|---|
| 111 | * @return Error code.
|
|---|
| 112 | */
|
|---|
| 113 | static int setup_ep0_packet_size(xhci_hc_t *hc, xhci_device_t *dev)
|
|---|
| 114 | {
|
|---|
| 115 | int err;
|
|---|
| 116 |
|
|---|
| 117 | uint16_t max_packet_size;
|
|---|
| 118 | if ((err = hc_get_ep0_max_packet_size(&max_packet_size, &dev->base)))
|
|---|
| 119 | return err;
|
|---|
| 120 |
|
|---|
| 121 | xhci_endpoint_t *ep0 = xhci_endpoint_get(dev->base.endpoints[0]);
|
|---|
| 122 | assert(ep0);
|
|---|
| 123 |
|
|---|
| 124 | if (ep0->base.max_packet_size == max_packet_size)
|
|---|
| 125 | return EOK;
|
|---|
| 126 |
|
|---|
| 127 | ep0->base.max_packet_size = max_packet_size;
|
|---|
| 128 | ep0->base.max_transfer_size = max_packet_size * ep0->base.packets_per_uframe;
|
|---|
| 129 |
|
|---|
| 130 | if ((err = hc_update_endpoint(ep0)))
|
|---|
| 131 | return err;
|
|---|
| 132 |
|
|---|
| 133 | return EOK;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | /**
|
|---|
| 137 | * Check whether the device is a hub and if so, fill its characterstics.
|
|---|
| 138 | *
|
|---|
| 139 | * If this fails, it does not necessarily mean the device is unusable.
|
|---|
| 140 | * Just the TT will not work correctly.
|
|---|
| 141 | */
|
|---|
| 142 | static int setup_hub(xhci_device_t *dev, usb_standard_device_descriptor_t *desc)
|
|---|
| 143 | {
|
|---|
| 144 | if (desc->device_class != USB_CLASS_HUB)
|
|---|
| 145 | return EOK;
|
|---|
| 146 |
|
|---|
| 147 | usb_hub_descriptor_header_t hub_desc = { 0 };
|
|---|
| 148 | const int err = hc_get_hub_desc(&dev->base, &hub_desc);
|
|---|
| 149 | if (err)
|
|---|
| 150 | return err;
|
|---|
| 151 |
|
|---|
| 152 | dev->is_hub = 1;
|
|---|
| 153 | dev->num_ports = hub_desc.port_count;
|
|---|
| 154 |
|
|---|
| 155 | if (dev->base.speed == USB_SPEED_HIGH) {
|
|---|
| 156 | dev->tt_think_time = 8 +
|
|---|
| 157 | 8 * !!(hub_desc.characteristics & HUB_CHAR_TT_THINK_8) +
|
|---|
| 158 | 16 * !!(hub_desc.characteristics & HUB_CHAR_TT_THINK_16);
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | usb_log_debug("Device(%u): recognised USB hub with %u ports",
|
|---|
| 162 | 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",
|
|---|
| 200 | str_error(err));
|
|---|
| 201 | return err;
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | /* Setup EP0 might already need to issue a transfer. */
|
|---|
| 205 | fibril_mutex_lock(&bus->base.guard);
|
|---|
| 206 | assert(bus->devices_by_slot[xhci_dev->slot_id] == NULL);
|
|---|
| 207 | bus->devices_by_slot[xhci_dev->slot_id] = xhci_dev;
|
|---|
| 208 | fibril_mutex_unlock(&bus->base.guard);
|
|---|
| 209 |
|
|---|
| 210 | if ((err = setup_ep0_packet_size(bus->hc, xhci_dev))) {
|
|---|
| 211 | usb_log_error("Failed to setup control endpoint "
|
|---|
| 212 | "of the new device: %s", str_error(err));
|
|---|
| 213 | goto err_address;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | usb_standard_device_descriptor_t desc = { 0 };
|
|---|
| 217 |
|
|---|
| 218 | if ((err = hc_get_device_desc(dev, &desc))) {
|
|---|
| 219 | usb_log_error("Device(%d): failed to get device "
|
|---|
| 220 | "descriptor: %s", dev->address, str_error(err));
|
|---|
| 221 | goto err_address;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | if ((err = setup_hub(xhci_dev, &desc)))
|
|---|
| 225 | usb_log_warning("Device(%d): failed to setup hub "
|
|---|
| 226 | "characteristics: %s. Continuing anyway.",
|
|---|
| 227 | dev->address, str_error(err));
|
|---|
| 228 |
|
|---|
| 229 | if ((err = hcd_ddf_setup_match_ids(dev, &desc))) {
|
|---|
| 230 | usb_log_error("Device(%d): failed to setup match IDs: %s",
|
|---|
| 231 | dev->address, str_error(err));
|
|---|
| 232 | goto err_address;
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | return EOK;
|
|---|
| 236 |
|
|---|
| 237 | err_address:
|
|---|
| 238 | return err;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | /**
|
|---|
| 242 | * Remove device from XHCI bus. Transition it to the offline state, abort all
|
|---|
| 243 | * ongoing transfers and unregister all of its endpoints.
|
|---|
| 244 | *
|
|---|
| 245 | * Bus callback.
|
|---|
| 246 | *
|
|---|
| 247 | * @param[in] bus XHCI bus, from which the device is removed.
|
|---|
| 248 | * @param[in] dev XHCI device, which is removed from the bus.
|
|---|
| 249 | * @return Error code.
|
|---|
| 250 | */
|
|---|
| 251 | void xhci_device_gone(device_t *dev)
|
|---|
| 252 | {
|
|---|
| 253 | int err;
|
|---|
| 254 | xhci_bus_t *bus = bus_to_xhci_bus(dev->bus);
|
|---|
| 255 | xhci_device_t *xhci_dev = xhci_device_get(dev);
|
|---|
| 256 |
|
|---|
| 257 | /* Disable the slot, dropping all endpoints. */
|
|---|
| 258 | const uint32_t slot_id = xhci_dev->slot_id;
|
|---|
| 259 | if ((err = hc_disable_slot(xhci_dev))) {
|
|---|
| 260 | usb_log_warning("Failed to disable slot of device " XHCI_DEV_FMT
|
|---|
| 261 | ": %s", XHCI_DEV_ARGS(*xhci_dev), str_error(err));
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | bus->devices_by_slot[slot_id] = NULL;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | /**
|
|---|
| 268 | * Reverts things device_offline did, getting the device back up.
|
|---|
| 269 | *
|
|---|
| 270 | * Bus callback.
|
|---|
| 271 | */
|
|---|
| 272 | int xhci_device_online(device_t *dev_base)
|
|---|
| 273 | {
|
|---|
| 274 | int err;
|
|---|
| 275 |
|
|---|
| 276 | xhci_bus_t *bus = bus_to_xhci_bus(dev_base->bus);
|
|---|
| 277 | assert(bus);
|
|---|
| 278 |
|
|---|
| 279 | xhci_device_t *dev = xhci_device_get(dev_base);
|
|---|
| 280 | assert(dev);
|
|---|
| 281 |
|
|---|
| 282 | /* Transition the device from the Addressed to the Configured state. */
|
|---|
| 283 | if ((err = hc_configure_device(dev))) {
|
|---|
| 284 | usb_log_warning("Failed to configure device " XHCI_DEV_FMT ".",
|
|---|
| 285 | XHCI_DEV_ARGS(*dev));
|
|---|
| 286 | return err;
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | return EOK;
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | /**
|
|---|
| 293 | * Make given device offline. Offline the DDF function, tear down all
|
|---|
| 294 | * endpoints, issue Deconfigure Device command to xHC.
|
|---|
| 295 | *
|
|---|
| 296 | * Bus callback.
|
|---|
| 297 | */
|
|---|
| 298 | void xhci_device_offline(device_t *dev_base)
|
|---|
| 299 | {
|
|---|
| 300 | int err;
|
|---|
| 301 |
|
|---|
| 302 | xhci_bus_t *bus = bus_to_xhci_bus(dev_base->bus);
|
|---|
| 303 | assert(bus);
|
|---|
| 304 |
|
|---|
| 305 | xhci_device_t *dev = xhci_device_get(dev_base);
|
|---|
| 306 | assert(dev);
|
|---|
| 307 |
|
|---|
| 308 | /* Issue one HC command to simultaneously drop all endpoints except zero. */
|
|---|
| 309 | if ((err = hc_deconfigure_device(dev))) {
|
|---|
| 310 | usb_log_warning("Failed to deconfigure device "
|
|---|
| 311 | XHCI_DEV_FMT ".", XHCI_DEV_ARGS(*dev));
|
|---|
| 312 | }
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | /**
|
|---|
| 316 | * Fill a slot context that is part of an Input Context with appropriate
|
|---|
| 317 | * values.
|
|---|
| 318 | *
|
|---|
| 319 | * @param ctx Slot context, zeroed out.
|
|---|
| 320 | */
|
|---|
| 321 | void xhci_setup_slot_context(xhci_device_t *dev, xhci_slot_ctx_t *ctx)
|
|---|
| 322 | {
|
|---|
| 323 | /* Initialize slot_ctx according to section 4.3.3 point 3. */
|
|---|
| 324 | XHCI_SLOT_ROOT_HUB_PORT_SET(*ctx, dev->rh_port);
|
|---|
| 325 | XHCI_SLOT_ROUTE_STRING_SET(*ctx, dev->route_str);
|
|---|
| 326 | XHCI_SLOT_SPEED_SET(*ctx, hc_speed_to_psiv(dev->base.speed));
|
|---|
| 327 |
|
|---|
| 328 | /*
|
|---|
| 329 | * Note: This function is used even before this flag can be set, to
|
|---|
| 330 | * issue the address device command. It is OK, because these
|
|---|
| 331 | * flags are not required to be valid for that command.
|
|---|
| 332 | */
|
|---|
| 333 | if (dev->is_hub) {
|
|---|
| 334 | XHCI_SLOT_HUB_SET(*ctx, 1);
|
|---|
| 335 | XHCI_SLOT_NUM_PORTS_SET(*ctx, dev->num_ports);
|
|---|
| 336 | XHCI_SLOT_TT_THINK_TIME_SET(*ctx, dev->tt_think_time);
|
|---|
| 337 | XHCI_SLOT_MTT_SET(*ctx, 0); // MTT not supported yet
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | /* Setup Transaction Translation. TODO: Test this with HS hub. */
|
|---|
| 341 | if (dev->base.tt.dev != NULL) {
|
|---|
| 342 | xhci_device_t *hub = xhci_device_get(dev->base.tt.dev);
|
|---|
| 343 | XHCI_SLOT_TT_HUB_SLOT_ID_SET(*ctx, hub->slot_id);
|
|---|
| 344 | XHCI_SLOT_TT_HUB_PORT_SET(*ctx, dev->base.tt.port);
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | /*
|
|---|
| 348 | * As we always allocate space for whole input context, we can set this
|
|---|
| 349 | * to maximum. The only exception being Address Device command, which
|
|---|
| 350 | * explicitly requires this to be se to 1.
|
|---|
| 351 | */
|
|---|
| 352 | XHCI_SLOT_CTX_ENTRIES_SET(*ctx, 31);
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 | /**
|
|---|
| 357 | * @}
|
|---|
| 358 | */
|
|---|