| 1 | /*
|
|---|
| 2 | * Copyright (c) 2017 Michal Staruch
|
|---|
| 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 |
|
|---|
| 29 | /** @addtogroup drvusbxhci
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief The roothub structures abstraction.
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <str_error.h>
|
|---|
| 38 | #include <usb/request.h>
|
|---|
| 39 | #include <usb/debug.h>
|
|---|
| 40 | #include <usb/host/bus.h>
|
|---|
| 41 | #include <usb/host/ddf_helpers.h>
|
|---|
| 42 | #include <usb/host/dma_buffer.h>
|
|---|
| 43 | #include <usb/host/hcd.h>
|
|---|
| 44 |
|
|---|
| 45 | #include "debug.h"
|
|---|
| 46 | #include "commands.h"
|
|---|
| 47 | #include "endpoint.h"
|
|---|
| 48 | #include "hc.h"
|
|---|
| 49 | #include "hw_struct/trb.h"
|
|---|
| 50 | #include "rh.h"
|
|---|
| 51 | #include "transfers.h"
|
|---|
| 52 |
|
|---|
| 53 | /* This mask only lists registers, which imply port change. */
|
|---|
| 54 | static const uint32_t port_change_mask =
|
|---|
| 55 | XHCI_REG_MASK(XHCI_PORT_CSC) |
|
|---|
| 56 | XHCI_REG_MASK(XHCI_PORT_PEC) |
|
|---|
| 57 | XHCI_REG_MASK(XHCI_PORT_WRC) |
|
|---|
| 58 | XHCI_REG_MASK(XHCI_PORT_OCC) |
|
|---|
| 59 | XHCI_REG_MASK(XHCI_PORT_PRC) |
|
|---|
| 60 | XHCI_REG_MASK(XHCI_PORT_PLC) |
|
|---|
| 61 | XHCI_REG_MASK(XHCI_PORT_CEC);
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * Initialize the roothub subsystem.
|
|---|
| 65 | */
|
|---|
| 66 | int xhci_rh_init(xhci_rh_t *rh, xhci_hc_t *hc)
|
|---|
| 67 | {
|
|---|
| 68 | assert(rh);
|
|---|
| 69 | assert(hc);
|
|---|
| 70 |
|
|---|
| 71 | rh->hc = hc;
|
|---|
| 72 | rh->max_ports = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_PORTS);
|
|---|
| 73 | rh->devices_by_port = (xhci_device_t **) calloc(rh->max_ports, sizeof(xhci_device_t *));
|
|---|
| 74 |
|
|---|
| 75 | const int err = bus_device_init(&rh->device.base, &rh->hc->bus.base);
|
|---|
| 76 | if (err)
|
|---|
| 77 | return err;
|
|---|
| 78 |
|
|---|
| 79 | /* Initialize route string */
|
|---|
| 80 | rh->device.route_str = 0;
|
|---|
| 81 | rh->device.tier = 0;
|
|---|
| 82 |
|
|---|
| 83 | fibril_mutex_initialize(&rh->event_guard);
|
|---|
| 84 | fibril_condvar_initialize(&rh->event_ready);
|
|---|
| 85 | fibril_condvar_initialize(&rh->event_handled);
|
|---|
| 86 |
|
|---|
| 87 | return EOK;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | /**
|
|---|
| 91 | * Finalize the RH subsystem.
|
|---|
| 92 | */
|
|---|
| 93 | int xhci_rh_fini(xhci_rh_t *rh)
|
|---|
| 94 | {
|
|---|
| 95 | assert(rh);
|
|---|
| 96 | free(rh->devices_by_port);
|
|---|
| 97 | return EOK;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | static int rh_event_wait_timeout(xhci_rh_t *rh, suseconds_t timeout)
|
|---|
| 101 | {
|
|---|
| 102 | assert(fibril_mutex_is_locked(&rh->event_guard));
|
|---|
| 103 |
|
|---|
| 104 | ++rh->event_readers_waiting;
|
|---|
| 105 | const int r = fibril_condvar_wait_timeout(&rh->event_ready, &rh->event_guard, timeout);
|
|---|
| 106 | --rh->event_readers_waiting;
|
|---|
| 107 | if (--rh->event_readers_to_go == 0)
|
|---|
| 108 | fibril_condvar_broadcast(&rh->event_handled);
|
|---|
| 109 | return r;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | static void rh_event_run_handlers(xhci_rh_t *rh)
|
|---|
| 113 | {
|
|---|
| 114 | fibril_mutex_lock(&rh->event_guard);
|
|---|
| 115 | assert(rh->event_readers_to_go == 0);
|
|---|
| 116 |
|
|---|
| 117 | rh->event_readers_to_go = rh->event_readers_waiting;
|
|---|
| 118 | fibril_condvar_broadcast(&rh->event_ready);
|
|---|
| 119 | while (rh->event_readers_to_go)
|
|---|
| 120 | fibril_condvar_wait(&rh->event_handled, &rh->event_guard);
|
|---|
| 121 | fibril_mutex_unlock(&rh->event_guard);
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | /**
|
|---|
| 125 | * Create and setup a device directly connected to RH. As the xHCI is not using
|
|---|
| 126 | * a virtual usbhub device for RH, this routine is called for devices directly.
|
|---|
| 127 | */
|
|---|
| 128 | static int rh_setup_device(xhci_rh_t *rh, uint8_t port_id)
|
|---|
| 129 | {
|
|---|
| 130 | int err;
|
|---|
| 131 | assert(rh);
|
|---|
| 132 |
|
|---|
| 133 | assert(rh->devices_by_port[port_id - 1] == NULL);
|
|---|
| 134 |
|
|---|
| 135 | device_t *dev = hcd_ddf_fun_create(&rh->hc->base);
|
|---|
| 136 | if (!dev) {
|
|---|
| 137 | usb_log_error("Failed to create USB device function.");
|
|---|
| 138 | return ENOMEM;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | const xhci_port_speed_t *port_speed = xhci_rh_get_port_speed(rh, port_id);
|
|---|
| 142 | xhci_device_t *xhci_dev = xhci_device_get(dev);
|
|---|
| 143 | xhci_dev->usb3 = port_speed->major == 3;
|
|---|
| 144 | xhci_dev->rh_port = port_id;
|
|---|
| 145 |
|
|---|
| 146 | dev->hub = &rh->device.base;
|
|---|
| 147 | dev->port = port_id;
|
|---|
| 148 | dev->speed = port_speed->usb_speed;
|
|---|
| 149 |
|
|---|
| 150 | if ((err = bus_device_enumerate(dev))) {
|
|---|
| 151 | usb_log_error("Failed to enumerate USB device: %s", str_error(err));
|
|---|
| 152 | return err;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | if (!ddf_fun_get_name(dev->fun)) {
|
|---|
| 156 | bus_device_set_default_name(dev);
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | if ((err = ddf_fun_bind(dev->fun))) {
|
|---|
| 160 | usb_log_error("Failed to register device " XHCI_DEV_FMT " DDF function: %s.",
|
|---|
| 161 | XHCI_DEV_ARGS(*xhci_dev), str_error(err));
|
|---|
| 162 | goto err_usb_dev;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | fibril_mutex_lock(&rh->device.base.guard);
|
|---|
| 166 | list_append(&dev->link, &rh->device.base.devices);
|
|---|
| 167 | rh->devices_by_port[port_id - 1] = xhci_dev;
|
|---|
| 168 | fibril_mutex_unlock(&rh->device.base.guard);
|
|---|
| 169 |
|
|---|
| 170 | return EOK;
|
|---|
| 171 |
|
|---|
| 172 | err_usb_dev:
|
|---|
| 173 | hcd_ddf_fun_destroy(dev);
|
|---|
| 174 | return err;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 | static int rh_port_reset_sync(xhci_rh_t *rh, uint8_t port_id)
|
|---|
| 179 | {
|
|---|
| 180 | int r;
|
|---|
| 181 | xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port_id - 1];
|
|---|
| 182 |
|
|---|
| 183 | fibril_mutex_lock(&rh->event_guard);
|
|---|
| 184 | XHCI_REG_SET(regs, XHCI_PORT_PR, 1);
|
|---|
| 185 |
|
|---|
| 186 | while (true) {
|
|---|
| 187 | r = rh_event_wait_timeout(rh, 0);
|
|---|
| 188 | if (r != EOK)
|
|---|
| 189 | break;
|
|---|
| 190 | if (rh->event.port_id == port_id
|
|---|
| 191 | && rh->event.events & XHCI_REG_MASK(XHCI_PORT_PRC))
|
|---|
| 192 | break;
|
|---|
| 193 | }
|
|---|
| 194 | fibril_mutex_unlock(&rh->event_guard);
|
|---|
| 195 |
|
|---|
| 196 | return r;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | /**
|
|---|
| 200 | * Handle a device connection. USB 3+ devices are set up directly, USB 2 and
|
|---|
| 201 | * below first need to have their port reset.
|
|---|
| 202 | */
|
|---|
| 203 | static int handle_connected_device(xhci_rh_t *rh, uint8_t port_id)
|
|---|
| 204 | {
|
|---|
| 205 | xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port_id - 1];
|
|---|
| 206 |
|
|---|
| 207 | uint8_t link_state = XHCI_REG_RD(regs, XHCI_PORT_PLS);
|
|---|
| 208 | const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, port_id);
|
|---|
| 209 |
|
|---|
| 210 | usb_log_info("Detected new %.4s%u.%u device on port %u.", speed->name, speed->major, speed->minor, port_id);
|
|---|
| 211 |
|
|---|
| 212 | if (speed->major == 3) {
|
|---|
| 213 | if (link_state == 0) {
|
|---|
| 214 | /* USB3 is automatically advanced to enabled. */
|
|---|
| 215 | return rh_setup_device(rh, port_id);
|
|---|
| 216 | }
|
|---|
| 217 | else if (link_state == 5) {
|
|---|
| 218 | /* USB 3 failed to enable. */
|
|---|
| 219 | usb_log_error("USB 3 port couldn't be enabled.");
|
|---|
| 220 | return EAGAIN;
|
|---|
| 221 | }
|
|---|
| 222 | else {
|
|---|
| 223 | usb_log_error("USB 3 port is in invalid state %u.", link_state);
|
|---|
| 224 | return EINVAL;
|
|---|
| 225 | }
|
|---|
| 226 | }
|
|---|
| 227 | else {
|
|---|
| 228 | usb_log_debug("USB 2 device attached, issuing reset.");
|
|---|
| 229 | const int err = rh_port_reset_sync(rh, port_id);
|
|---|
| 230 | if (err)
|
|---|
| 231 | return err;
|
|---|
| 232 |
|
|---|
| 233 | rh_setup_device(rh, port_id);
|
|---|
| 234 | return EOK;
|
|---|
| 235 | }
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | /**
|
|---|
| 239 | * Deal with a detached device.
|
|---|
| 240 | */
|
|---|
| 241 | static int handle_disconnected_device(xhci_rh_t *rh, uint8_t port_id)
|
|---|
| 242 | {
|
|---|
| 243 | assert(rh);
|
|---|
| 244 |
|
|---|
| 245 | /* Find XHCI device by the port. */
|
|---|
| 246 | xhci_device_t *dev = rh->devices_by_port[port_id - 1];
|
|---|
| 247 | if (!dev) {
|
|---|
| 248 | /* Must be extraneous call. */
|
|---|
| 249 | return EOK;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | usb_log_info("Device " XHCI_DEV_FMT " at port %u has been disconnected.",
|
|---|
| 253 | XHCI_DEV_ARGS(*dev), port_id);
|
|---|
| 254 |
|
|---|
| 255 | /* Mark the device as detached. */
|
|---|
| 256 | fibril_mutex_lock(&rh->device.base.guard);
|
|---|
| 257 | list_remove(&dev->base.link);
|
|---|
| 258 | rh->devices_by_port[port_id - 1] = NULL;
|
|---|
| 259 | fibril_mutex_unlock(&rh->device.base.guard);
|
|---|
| 260 |
|
|---|
| 261 | /* Remove device from XHCI bus. */
|
|---|
| 262 | bus_device_gone(&dev->base);
|
|---|
| 263 |
|
|---|
| 264 | return EOK;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | /**
|
|---|
| 268 | * Handle an incoming Port Change Detected Event.
|
|---|
| 269 | */
|
|---|
| 270 | int xhci_rh_handle_port_status_change_event(xhci_hc_t *hc, xhci_trb_t *trb)
|
|---|
| 271 | {
|
|---|
| 272 | uint8_t port_id = XHCI_QWORD_EXTRACT(trb->parameter, 31, 24);
|
|---|
| 273 | usb_log_debug("Port status change event detected for port %u.", port_id);
|
|---|
| 274 |
|
|---|
| 275 | /**
|
|---|
| 276 | * We can't be sure that the port change this event announces is the
|
|---|
| 277 | * only port change that happened (see section 4.19.2 of the xHCI
|
|---|
| 278 | * specification). Therefore, we just check all ports for changes.
|
|---|
| 279 | */
|
|---|
| 280 | xhci_rh_handle_port_change(&hc->rh);
|
|---|
| 281 |
|
|---|
| 282 | return EOK;
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | typedef int (*rh_event_handler_t)(xhci_rh_t *, uint8_t);
|
|---|
| 286 |
|
|---|
| 287 | typedef struct rh_event_args {
|
|---|
| 288 | xhci_rh_t *rh;
|
|---|
| 289 | uint8_t port_id;
|
|---|
| 290 | rh_event_handler_t handler;
|
|---|
| 291 | } rh_event_args_t;
|
|---|
| 292 |
|
|---|
| 293 | static int rh_event_handler_fibril(void *arg) {
|
|---|
| 294 | rh_event_args_t *rh_args = arg;
|
|---|
| 295 | xhci_rh_t *rh = rh_args->rh;
|
|---|
| 296 | uint8_t port_id = rh_args->port_id;
|
|---|
| 297 | rh_event_handler_t handler = rh_args->handler;
|
|---|
| 298 |
|
|---|
| 299 | free(rh_args);
|
|---|
| 300 |
|
|---|
| 301 | return handler(rh, port_id);
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | static fid_t handle_in_fibril(xhci_rh_t *rh, uint8_t port_id, rh_event_handler_t handler)
|
|---|
| 305 | {
|
|---|
| 306 | rh_event_args_t *args = malloc(sizeof(*args));
|
|---|
| 307 | *args = (rh_event_args_t) {
|
|---|
| 308 | .rh = rh,
|
|---|
| 309 | .port_id = port_id,
|
|---|
| 310 | .handler = handler,
|
|---|
| 311 | };
|
|---|
| 312 |
|
|---|
| 313 | const fid_t fid = fibril_create(rh_event_handler_fibril, args);
|
|---|
| 314 | fibril_add_ready(fid);
|
|---|
| 315 | return fid;
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | /**
|
|---|
| 319 | * Handle all changes on all ports.
|
|---|
| 320 | */
|
|---|
| 321 | void xhci_rh_handle_port_change(xhci_rh_t *rh)
|
|---|
| 322 | {
|
|---|
| 323 | for (uint8_t i = 1; i <= rh->max_ports; ++i) {
|
|---|
| 324 | xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[i - 1];
|
|---|
| 325 |
|
|---|
| 326 | uint32_t events = XHCI_REG_RD_FIELD(®s->portsc, 32);
|
|---|
| 327 | XHCI_REG_WR_FIELD(®s->portsc, events, 32);
|
|---|
| 328 |
|
|---|
| 329 | events &= port_change_mask;
|
|---|
| 330 |
|
|---|
| 331 | if (events & XHCI_REG_MASK(XHCI_PORT_CSC)) {
|
|---|
| 332 | usb_log_info("Connected state changed on port %u.", i);
|
|---|
| 333 | events &= ~XHCI_REG_MASK(XHCI_PORT_CSC);
|
|---|
| 334 |
|
|---|
| 335 | bool connected = XHCI_REG_RD(regs, XHCI_PORT_CCS);
|
|---|
| 336 | if (connected) {
|
|---|
| 337 | handle_in_fibril(rh, i, handle_connected_device);
|
|---|
| 338 | } else {
|
|---|
| 339 | handle_in_fibril(rh, i, handle_disconnected_device);
|
|---|
| 340 | }
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | if (events != 0) {
|
|---|
| 344 | rh->event.port_id = i;
|
|---|
| 345 | rh->event.events = events;
|
|---|
| 346 | rh_event_run_handlers(rh);
|
|---|
| 347 | }
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | /**
|
|---|
| 351 | * Theory:
|
|---|
| 352 | *
|
|---|
| 353 | * Although more events could have happened while processing, the PCD
|
|---|
| 354 | * bit in USBSTS will be set on every change. Because the PCD is
|
|---|
| 355 | * cleared even before the interrupt is cleared, it is safe to assume
|
|---|
| 356 | * that this handler will be called again.
|
|---|
| 357 | *
|
|---|
| 358 | * But because we could have handled the event in previous run of this
|
|---|
| 359 | * handler, it is not an error when no event is detected.
|
|---|
| 360 | *
|
|---|
| 361 | * Reality:
|
|---|
| 362 | *
|
|---|
| 363 | * The PCD bit is never set. TODO Check why the interrupt never carries
|
|---|
| 364 | * the PCD flag. Possibly repeat the checking until we're sure the
|
|---|
| 365 | * PSCEG is 0 - check section 4.19.2 of the xHCI spec.
|
|---|
| 366 | */
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | /**
|
|---|
| 370 | * Get a port speed for a given port id.
|
|---|
| 371 | */
|
|---|
| 372 | const xhci_port_speed_t *xhci_rh_get_port_speed(xhci_rh_t *rh, uint8_t port)
|
|---|
| 373 | {
|
|---|
| 374 | xhci_port_regs_t *port_regs = &rh->hc->op_regs->portrs[port - 1];
|
|---|
| 375 |
|
|---|
| 376 | unsigned psiv = XHCI_REG_RD(port_regs, XHCI_PORT_PS);
|
|---|
| 377 | return &rh->hc->speeds[psiv];
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | /**
|
|---|
| 381 | * @}
|
|---|
| 382 | */
|
|---|