[7bd99bf] | 1 | /*
|
---|
[dcf0597] | 2 | * Copyright (c) 2017 Michal Staruch
|
---|
[7bd99bf] | 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 | */
|
---|
[d967aa1] | 35 |
|
---|
[7bd99bf] | 36 | #include <errno.h>
|
---|
| 37 | #include <str_error.h>
|
---|
[2770b66] | 38 | #include <usb/request.h>
|
---|
[7bd99bf] | 39 | #include <usb/debug.h>
|
---|
[20eaa82] | 40 | #include <usb/host/bus.h>
|
---|
[867b375] | 41 | #include <usb/host/ddf_helpers.h>
|
---|
[b80c1ab] | 42 | #include <usb/host/dma_buffer.h>
|
---|
[2770b66] | 43 | #include <usb/host/hcd.h>
|
---|
[867b375] | 44 |
|
---|
[7bd99bf] | 45 | #include "debug.h"
|
---|
[174788f] | 46 | #include "commands.h"
|
---|
[370a1c8] | 47 | #include "endpoint.h"
|
---|
[7bd99bf] | 48 | #include "hc.h"
|
---|
| 49 | #include "hw_struct/trb.h"
|
---|
[c8bb7090] | 50 | #include "rh.h"
|
---|
[e9e24f2] | 51 | #include "transfers.h"
|
---|
[7bd99bf] | 52 |
|
---|
[9876e34] | 53 | /* This mask only lists registers, which imply port change. */
|
---|
[9b56e528] | 54 | static const uint32_t port_events_mask =
|
---|
[9876e34] | 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 |
|
---|
[eb928c4] | 63 | /**
|
---|
| 64 | * Initialize the roothub subsystem.
|
---|
| 65 | */
|
---|
[63431db2] | 66 | int xhci_rh_init(xhci_rh_t *rh, xhci_hc_t *hc)
|
---|
[d32d51d] | 67 | {
|
---|
[5c5c9407] | 68 | assert(rh);
|
---|
[816335c] | 69 | assert(hc);
|
---|
| 70 |
|
---|
| 71 | rh->hc = hc;
|
---|
[9876e34] | 72 | rh->max_ports = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_PORTS);
|
---|
[d33dc780] | 73 | rh->devices_by_port = (xhci_device_t **) calloc(rh->max_ports, sizeof(xhci_device_t *));
|
---|
[5c5c9407] | 74 |
|
---|
[6832245] | 75 | const int err = bus_device_init(&rh->device.base, &rh->hc->bus.base);
|
---|
[2cf28b9] | 76 | if (err)
|
---|
| 77 | return err;
|
---|
| 78 |
|
---|
| 79 | /* Initialize route string */
|
---|
| 80 | rh->device.route_str = 0;
|
---|
| 81 | rh->device.tier = 0;
|
---|
| 82 |
|
---|
[49e62998] | 83 | fibril_mutex_initialize(&rh->event_guard);
|
---|
| 84 | fibril_condvar_initialize(&rh->event_ready);
|
---|
| 85 | fibril_condvar_initialize(&rh->event_handled);
|
---|
| 86 |
|
---|
[2cf28b9] | 87 | return EOK;
|
---|
[d32d51d] | 88 | }
|
---|
| 89 |
|
---|
[eb928c4] | 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 |
|
---|
[230ef1c] | 100 | typedef struct rh_event {
|
---|
| 101 | uint8_t port_id;
|
---|
| 102 | uint32_t events;
|
---|
| 103 | unsigned readers_to_go;
|
---|
| 104 | } rh_event_t;
|
---|
| 105 |
|
---|
[fb154e13] | 106 | static int rh_event_wait_timeout(xhci_rh_t *rh, uint8_t port_id, uint32_t mask, suseconds_t timeout)
|
---|
[49e62998] | 107 | {
|
---|
[fb154e13] | 108 | int r;
|
---|
[49e62998] | 109 | assert(fibril_mutex_is_locked(&rh->event_guard));
|
---|
| 110 |
|
---|
| 111 | ++rh->event_readers_waiting;
|
---|
[fb154e13] | 112 |
|
---|
| 113 | do {
|
---|
| 114 | r = fibril_condvar_wait_timeout(&rh->event_ready, &rh->event_guard, timeout);
|
---|
| 115 | if (r != EOK)
|
---|
| 116 | break;
|
---|
[230ef1c] | 117 |
|
---|
| 118 | assert(rh->event);
|
---|
| 119 | if (--rh->event->readers_to_go == 0)
|
---|
| 120 | fibril_condvar_broadcast(&rh->event_handled);
|
---|
| 121 | } while (rh->event->port_id != port_id || (rh->event->events & mask) != mask);
|
---|
[fb154e13] | 122 |
|
---|
| 123 | if (r == EOK)
|
---|
[230ef1c] | 124 | rh->event->events &= ~mask;
|
---|
[fb154e13] | 125 |
|
---|
[49e62998] | 126 | --rh->event_readers_waiting;
|
---|
[fb154e13] | 127 |
|
---|
[49e62998] | 128 | return r;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[230ef1c] | 131 | static void rh_event_run_handlers(xhci_rh_t *rh, uint8_t port_id, uint32_t *events)
|
---|
[49e62998] | 132 | {
|
---|
[fb154e13] | 133 | assert(fibril_mutex_is_locked(&rh->event_guard));
|
---|
[49e62998] | 134 |
|
---|
[230ef1c] | 135 | /* There can be different event running already */
|
---|
| 136 | while (rh->event)
|
---|
| 137 | fibril_condvar_wait(&rh->event_handled, &rh->event_guard);
|
---|
| 138 |
|
---|
| 139 | rh_event_t event = {
|
---|
| 140 | .port_id = port_id,
|
---|
| 141 | .events = *events,
|
---|
| 142 | .readers_to_go = rh->event_readers_waiting,
|
---|
| 143 | };
|
---|
| 144 |
|
---|
| 145 | rh->event = &event;
|
---|
[49e62998] | 146 | fibril_condvar_broadcast(&rh->event_ready);
|
---|
[230ef1c] | 147 | while (event.readers_to_go)
|
---|
[49e62998] | 148 | fibril_condvar_wait(&rh->event_handled, &rh->event_guard);
|
---|
[230ef1c] | 149 | *events = event.events;
|
---|
| 150 | rh->event = NULL;
|
---|
[bf601313] | 151 |
|
---|
| 152 | /* Wake other threads potentially waiting to post their event */
|
---|
| 153 | fibril_condvar_broadcast(&rh->event_handled);
|
---|
[49e62998] | 154 | }
|
---|
| 155 |
|
---|
[eb928c4] | 156 | /**
|
---|
| 157 | * Create and setup a device directly connected to RH. As the xHCI is not using
|
---|
| 158 | * a virtual usbhub device for RH, this routine is called for devices directly.
|
---|
[20eaa82] | 159 | */
|
---|
[867b375] | 160 | static int rh_setup_device(xhci_rh_t *rh, uint8_t port_id)
|
---|
| 161 | {
|
---|
[20eaa82] | 162 | int err;
|
---|
| 163 | assert(rh);
|
---|
[d33dc780] | 164 | assert(rh->devices_by_port[port_id - 1] == NULL);
|
---|
[2cf28b9] | 165 |
|
---|
[9b56e528] | 166 | if (!XHCI_REG_RD(&rh->hc->op_regs->portrs[port_id - 1], XHCI_PORT_PED)) {
|
---|
| 167 | usb_log_error("Cannot setup RH device: port is disabled.");
|
---|
| 168 | return EIO;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[eeca8a6] | 171 | const xhci_port_speed_t *port_speed = xhci_rh_get_port_speed(rh, port_id);
|
---|
| 172 |
|
---|
| 173 | device_t *dev = hcd_ddf_fun_create(&rh->hc->base, port_speed->usb_speed);
|
---|
[20eaa82] | 174 | if (!dev) {
|
---|
| 175 | usb_log_error("Failed to create USB device function.");
|
---|
| 176 | return ENOMEM;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[eeca8a6] | 179 | dev->hub = &rh->device.base;
|
---|
| 180 | dev->port = port_id;
|
---|
| 181 |
|
---|
[0206d35] | 182 | xhci_device_t *xhci_dev = xhci_device_get(dev);
|
---|
| 183 | xhci_dev->usb3 = port_speed->major == 3;
|
---|
[2cf28b9] | 184 | xhci_dev->rh_port = port_id;
|
---|
[0206d35] | 185 |
|
---|
[eb928c4] | 186 | if ((err = bus_device_enumerate(dev))) {
|
---|
[20eaa82] | 187 | usb_log_error("Failed to enumerate USB device: %s", str_error(err));
|
---|
| 188 | return err;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | if (!ddf_fun_get_name(dev->fun)) {
|
---|
[6832245] | 192 | bus_device_set_default_name(dev);
|
---|
[20eaa82] | 193 | }
|
---|
| 194 |
|
---|
| 195 | if ((err = ddf_fun_bind(dev->fun))) {
|
---|
[9620a54] | 196 | usb_log_error("Failed to register device " XHCI_DEV_FMT " DDF function: %s.",
|
---|
| 197 | XHCI_DEV_ARGS(*xhci_dev), str_error(err));
|
---|
[20eaa82] | 198 | goto err_usb_dev;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[d33dc780] | 201 | rh->devices_by_port[port_id - 1] = xhci_dev;
|
---|
[867b375] | 202 |
|
---|
[20eaa82] | 203 | return EOK;
|
---|
| 204 |
|
---|
| 205 | err_usb_dev:
|
---|
[32fb6bce] | 206 | hcd_ddf_fun_destroy(dev);
|
---|
[20eaa82] | 207 | return err;
|
---|
[867b375] | 208 | }
|
---|
| 209 |
|
---|
[49e62998] | 210 |
|
---|
| 211 | static int rh_port_reset_sync(xhci_rh_t *rh, uint8_t port_id)
|
---|
| 212 | {
|
---|
| 213 | xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port_id - 1];
|
---|
| 214 |
|
---|
| 215 | fibril_mutex_lock(&rh->event_guard);
|
---|
| 216 | XHCI_REG_SET(regs, XHCI_PORT_PR, 1);
|
---|
[fb154e13] | 217 | const int r = rh_event_wait_timeout(rh, port_id, XHCI_REG_MASK(XHCI_PORT_PRC), 0);
|
---|
[49e62998] | 218 | fibril_mutex_unlock(&rh->event_guard);
|
---|
| 219 | return r;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[eb928c4] | 222 | /**
|
---|
| 223 | * Handle a device connection. USB 3+ devices are set up directly, USB 2 and
|
---|
| 224 | * below first need to have their port reset.
|
---|
| 225 | */
|
---|
[dcf0597] | 226 | static int handle_connected_device(xhci_rh_t *rh, uint8_t port_id)
|
---|
[c8bb7090] | 227 | {
|
---|
[dcf0597] | 228 | xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port_id - 1];
|
---|
[472235a] | 229 |
|
---|
[dcf0597] | 230 | uint8_t link_state = XHCI_REG_RD(regs, XHCI_PORT_PLS);
|
---|
| 231 | const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, port_id);
|
---|
[7bd99bf] | 232 |
|
---|
[dcf0597] | 233 | usb_log_info("Detected new %.4s%u.%u device on port %u.", speed->name, speed->major, speed->minor, port_id);
|
---|
[7bd99bf] | 234 |
|
---|
[dcf0597] | 235 | if (speed->major == 3) {
|
---|
| 236 | if (link_state == 0) {
|
---|
| 237 | /* USB3 is automatically advanced to enabled. */
|
---|
[867b375] | 238 | return rh_setup_device(rh, port_id);
|
---|
[dcf0597] | 239 | }
|
---|
| 240 | else if (link_state == 5) {
|
---|
| 241 | /* USB 3 failed to enable. */
|
---|
| 242 | usb_log_error("USB 3 port couldn't be enabled.");
|
---|
| 243 | return EAGAIN;
|
---|
| 244 | }
|
---|
| 245 | else {
|
---|
| 246 | usb_log_error("USB 3 port is in invalid state %u.", link_state);
|
---|
| 247 | return EINVAL;
|
---|
| 248 | }
|
---|
| 249 | }
|
---|
| 250 | else {
|
---|
| 251 | usb_log_debug("USB 2 device attached, issuing reset.");
|
---|
[49e62998] | 252 | const int err = rh_port_reset_sync(rh, port_id);
|
---|
| 253 | if (err)
|
---|
| 254 | return err;
|
---|
| 255 |
|
---|
| 256 | rh_setup_device(rh, port_id);
|
---|
[dcf0597] | 257 | return EOK;
|
---|
| 258 | }
|
---|
[816335c] | 259 | }
|
---|
| 260 |
|
---|
[eb928c4] | 261 | /**
|
---|
| 262 | * Deal with a detached device.
|
---|
[f45c78f] | 263 | */
|
---|
| 264 | static int handle_disconnected_device(xhci_rh_t *rh, uint8_t port_id)
|
---|
| 265 | {
|
---|
[a4e26882] | 266 | assert(rh);
|
---|
| 267 |
|
---|
[766043c] | 268 | /* Find XHCI device by the port. */
|
---|
[d33dc780] | 269 | xhci_device_t *dev = rh->devices_by_port[port_id - 1];
|
---|
[766043c] | 270 | if (!dev) {
|
---|
[a4e26882] | 271 | /* Must be extraneous call. */
|
---|
[766043c] | 272 | return EOK;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
[9620a54] | 275 | usb_log_info("Device " XHCI_DEV_FMT " at port %u has been disconnected.",
|
---|
| 276 | XHCI_DEV_ARGS(*dev), port_id);
|
---|
[a4e26882] | 277 |
|
---|
[40a3bfa] | 278 | /* Mark the device as detached. */
|
---|
[d33dc780] | 279 | rh->devices_by_port[port_id - 1] = NULL;
|
---|
[2cf28b9] | 280 |
|
---|
[31cca4f3] | 281 | /* Remove device from XHCI bus. */
|
---|
[9848c77] | 282 | bus_device_gone(&dev->base);
|
---|
[31cca4f3] | 283 |
|
---|
[f45c78f] | 284 | return EOK;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
[49e62998] | 287 | typedef int (*rh_event_handler_t)(xhci_rh_t *, uint8_t);
|
---|
| 288 |
|
---|
| 289 | typedef struct rh_event_args {
|
---|
| 290 | xhci_rh_t *rh;
|
---|
| 291 | uint8_t port_id;
|
---|
| 292 | rh_event_handler_t handler;
|
---|
| 293 | } rh_event_args_t;
|
---|
| 294 |
|
---|
| 295 | static int rh_event_handler_fibril(void *arg) {
|
---|
| 296 | rh_event_args_t *rh_args = arg;
|
---|
| 297 | xhci_rh_t *rh = rh_args->rh;
|
---|
| 298 | uint8_t port_id = rh_args->port_id;
|
---|
| 299 | rh_event_handler_t handler = rh_args->handler;
|
---|
| 300 |
|
---|
| 301 | free(rh_args);
|
---|
| 302 |
|
---|
| 303 | return handler(rh, port_id);
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | static fid_t handle_in_fibril(xhci_rh_t *rh, uint8_t port_id, rh_event_handler_t handler)
|
---|
| 307 | {
|
---|
| 308 | rh_event_args_t *args = malloc(sizeof(*args));
|
---|
| 309 | *args = (rh_event_args_t) {
|
---|
| 310 | .rh = rh,
|
---|
| 311 | .port_id = port_id,
|
---|
| 312 | .handler = handler,
|
---|
| 313 | };
|
---|
| 314 |
|
---|
| 315 | const fid_t fid = fibril_create(rh_event_handler_fibril, args);
|
---|
| 316 | fibril_add_ready(fid);
|
---|
| 317 | return fid;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
[eb928c4] | 320 | /**
|
---|
[fb154e13] | 321 | * Handle all changes on specified port.
|
---|
[eb928c4] | 322 | */
|
---|
[fb154e13] | 323 | void xhci_rh_handle_port_change(xhci_rh_t *rh, uint8_t port_id)
|
---|
[d32d51d] | 324 | {
|
---|
[fb154e13] | 325 | fibril_mutex_lock(&rh->event_guard);
|
---|
| 326 | xhci_port_regs_t * const regs = &rh->hc->op_regs->portrs[port_id - 1];
|
---|
[07c08ea] | 327 |
|
---|
[9b56e528] | 328 | uint32_t events = XHCI_REG_RD_FIELD(®s->portsc, 32) & port_events_mask;
|
---|
[5c5c9407] | 329 |
|
---|
[fb154e13] | 330 | while (events) {
|
---|
[9b56e528] | 331 | /*
|
---|
| 332 | * The PED bit in xHCI has RW1C semantics, which means that
|
---|
| 333 | * writing 1 to it will disable the port. Which means all
|
---|
| 334 | * standard mechanisms of register handling fails here.
|
---|
| 335 | */
|
---|
| 336 | uint32_t portsc = XHCI_REG_RD_FIELD(®s->portsc, 32);
|
---|
| 337 | portsc &= ~(port_events_mask | XHCI_REG_MASK(XHCI_PORT_PED)); // Clear events + PED
|
---|
| 338 | portsc |= events; // Add back events to assert them
|
---|
| 339 | XHCI_REG_WR_FIELD(®s->portsc, portsc, 32);
|
---|
[5c5c9407] | 340 |
|
---|
[dcf0597] | 341 | if (events & XHCI_REG_MASK(XHCI_PORT_CSC)) {
|
---|
[fb154e13] | 342 | usb_log_info("Connected state changed on port %u.", port_id);
|
---|
[dcf0597] | 343 | events &= ~XHCI_REG_MASK(XHCI_PORT_CSC);
|
---|
[5c5c9407] | 344 |
|
---|
[dcf0597] | 345 | bool connected = XHCI_REG_RD(regs, XHCI_PORT_CCS);
|
---|
[f45c78f] | 346 | if (connected) {
|
---|
[fb154e13] | 347 | handle_in_fibril(rh, port_id, handle_connected_device);
|
---|
[f45c78f] | 348 | } else {
|
---|
[fb154e13] | 349 | handle_in_fibril(rh, port_id, handle_disconnected_device);
|
---|
[f45c78f] | 350 | }
|
---|
[dcf0597] | 351 | }
|
---|
[5c5c9407] | 352 |
|
---|
[230ef1c] | 353 | if (events != 0)
|
---|
| 354 | rh_event_run_handlers(rh, port_id, &events);
|
---|
[fb154e13] | 355 |
|
---|
[230ef1c] | 356 | if (events != 0)
|
---|
| 357 | usb_log_debug("RH port %u change not handled: 0x%x", port_id, events);
|
---|
[fb154e13] | 358 |
|
---|
| 359 | /* Make sure that PSCEG is 0 before exiting the loop. */
|
---|
[9b56e528] | 360 | events = XHCI_REG_RD_FIELD(®s->portsc, 32) & port_events_mask;
|
---|
[916991b] | 361 | }
|
---|
[2297fab] | 362 |
|
---|
[fb154e13] | 363 | fibril_mutex_unlock(&rh->event_guard);
|
---|
[07c08ea] | 364 | }
|
---|
| 365 |
|
---|
[eb928c4] | 366 | /**
|
---|
| 367 | * Get a port speed for a given port id.
|
---|
| 368 | */
|
---|
[dcf0597] | 369 | const xhci_port_speed_t *xhci_rh_get_port_speed(xhci_rh_t *rh, uint8_t port)
|
---|
[07c08ea] | 370 | {
|
---|
[dcf0597] | 371 | xhci_port_regs_t *port_regs = &rh->hc->op_regs->portrs[port - 1];
|
---|
[916991b] | 372 |
|
---|
[dcf0597] | 373 | unsigned psiv = XHCI_REG_RD(port_regs, XHCI_PORT_PS);
|
---|
[f668d60] | 374 | return &rh->hc->speeds[psiv];
|
---|
[07c08ea] | 375 | }
|
---|
| 376 |
|
---|
[c8bb7090] | 377 | /**
|
---|
| 378 | * @}
|
---|
| 379 | */
|
---|