[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>
|
---|
[174788f] | 40 | #include <usb/host/utils/malloc32.h>
|
---|
[20eaa82] | 41 | #include <usb/host/bus.h>
|
---|
[867b375] | 42 | #include <usb/host/ddf_helpers.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. */
|
---|
| 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 |
|
---|
[0f6b50f] | 63 | int xhci_rh_init(xhci_rh_t *rh, xhci_hc_t *hc, ddf_dev_t *device)
|
---|
[d32d51d] | 64 | {
|
---|
[5c5c9407] | 65 | assert(rh);
|
---|
[816335c] | 66 | assert(hc);
|
---|
| 67 |
|
---|
| 68 | rh->hc = hc;
|
---|
[9876e34] | 69 | rh->max_ports = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_PORTS);
|
---|
[a4e26882] | 70 | rh->devices = (xhci_device_t **) calloc(rh->max_ports, sizeof(xhci_device_t *));
|
---|
[2cf28b9] | 71 | rh->hc_device = device;
|
---|
[5c5c9407] | 72 |
|
---|
[2cf28b9] | 73 | const int err = device_init(&rh->device.base);
|
---|
| 74 | if (err)
|
---|
| 75 | return err;
|
---|
| 76 |
|
---|
| 77 | /* Initialize route string */
|
---|
| 78 | rh->device.route_str = 0;
|
---|
| 79 | rh->device.tier = 0;
|
---|
| 80 |
|
---|
| 81 | return EOK;
|
---|
[d32d51d] | 82 | }
|
---|
| 83 |
|
---|
[20eaa82] | 84 | /** Create a device node for device directly connected to RH.
|
---|
| 85 | */
|
---|
[867b375] | 86 | static int rh_setup_device(xhci_rh_t *rh, uint8_t port_id)
|
---|
| 87 | {
|
---|
[20eaa82] | 88 | int err;
|
---|
| 89 | assert(rh);
|
---|
[0f6b50f] | 90 | assert(rh->hc_device);
|
---|
[20eaa82] | 91 |
|
---|
[2cf28b9] | 92 | assert(rh->devices[port_id - 1] == NULL);
|
---|
| 93 |
|
---|
[20eaa82] | 94 | xhci_bus_t *bus = &rh->hc->bus;
|
---|
| 95 |
|
---|
| 96 | device_t *dev = hcd_ddf_device_create(rh->hc_device, bus->base.device_size);
|
---|
| 97 | if (!dev) {
|
---|
| 98 | usb_log_error("Failed to create USB device function.");
|
---|
| 99 | return ENOMEM;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[0206d35] | 102 | const xhci_port_speed_t *port_speed = xhci_rh_get_port_speed(rh, port_id);
|
---|
| 103 | xhci_device_t *xhci_dev = xhci_device_get(dev);
|
---|
| 104 | xhci_dev->usb3 = port_speed->major == 3;
|
---|
[2cf28b9] | 105 | xhci_dev->rh_port = port_id;
|
---|
[0206d35] | 106 |
|
---|
[2cf28b9] | 107 | dev->hub = &rh->device.base;
|
---|
[20eaa82] | 108 | dev->port = port_id;
|
---|
[f668d60] | 109 | dev->speed = port_speed->usb_speed;
|
---|
[20eaa82] | 110 |
|
---|
| 111 | if ((err = xhci_bus_enumerate_device(bus, rh->hc, dev))) {
|
---|
| 112 | usb_log_error("Failed to enumerate USB device: %s", str_error(err));
|
---|
| 113 | return err;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | if (!ddf_fun_get_name(dev->fun)) {
|
---|
| 117 | device_set_default_name(dev);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | if ((err = ddf_fun_bind(dev->fun))) {
|
---|
| 121 | usb_log_error("Device(%d): Failed to register: %s.", dev->address, str_error(err));
|
---|
| 122 | goto err_usb_dev;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[2cf28b9] | 125 | fibril_mutex_lock(&rh->device.base.guard);
|
---|
| 126 | list_append(&dev->link, &rh->device.base.devices);
|
---|
| 127 | rh->devices[port_id - 1] = xhci_dev;
|
---|
| 128 | fibril_mutex_unlock(&rh->device.base.guard);
|
---|
[867b375] | 129 |
|
---|
[20eaa82] | 130 | return EOK;
|
---|
| 131 |
|
---|
| 132 | err_usb_dev:
|
---|
| 133 | hcd_ddf_device_destroy(dev);
|
---|
| 134 | return err;
|
---|
[867b375] | 135 | }
|
---|
| 136 |
|
---|
[dcf0597] | 137 | static int handle_connected_device(xhci_rh_t *rh, uint8_t port_id)
|
---|
[c8bb7090] | 138 | {
|
---|
[dcf0597] | 139 | xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port_id - 1];
|
---|
[472235a] | 140 |
|
---|
[dcf0597] | 141 | uint8_t link_state = XHCI_REG_RD(regs, XHCI_PORT_PLS);
|
---|
| 142 | const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, port_id);
|
---|
[7bd99bf] | 143 |
|
---|
[dcf0597] | 144 | usb_log_info("Detected new %.4s%u.%u device on port %u.", speed->name, speed->major, speed->minor, port_id);
|
---|
[7bd99bf] | 145 |
|
---|
[dcf0597] | 146 | if (speed->major == 3) {
|
---|
| 147 | if (link_state == 0) {
|
---|
| 148 | /* USB3 is automatically advanced to enabled. */
|
---|
[867b375] | 149 | return rh_setup_device(rh, port_id);
|
---|
[dcf0597] | 150 | }
|
---|
| 151 | else if (link_state == 5) {
|
---|
| 152 | /* USB 3 failed to enable. */
|
---|
| 153 | usb_log_error("USB 3 port couldn't be enabled.");
|
---|
| 154 | return EAGAIN;
|
---|
| 155 | }
|
---|
| 156 | else {
|
---|
| 157 | usb_log_error("USB 3 port is in invalid state %u.", link_state);
|
---|
| 158 | return EINVAL;
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 | else {
|
---|
| 162 | usb_log_debug("USB 2 device attached, issuing reset.");
|
---|
| 163 | xhci_rh_reset_port(rh, port_id);
|
---|
| 164 | /*
|
---|
| 165 | FIXME: we need to wait for the event triggered by the reset
|
---|
| 166 | and then alloc_dev()... can't it be done directly instead of
|
---|
| 167 | going around?
|
---|
| 168 | */
|
---|
| 169 | return EOK;
|
---|
| 170 | }
|
---|
[816335c] | 171 | }
|
---|
| 172 |
|
---|
[f45c78f] | 173 | /** Deal with a detached device.
|
---|
| 174 | */
|
---|
| 175 | static int handle_disconnected_device(xhci_rh_t *rh, uint8_t port_id)
|
---|
| 176 | {
|
---|
[a4e26882] | 177 | assert(rh);
|
---|
| 178 | int err;
|
---|
| 179 |
|
---|
[766043c] | 180 | /* Find XHCI device by the port. */
|
---|
| 181 | xhci_device_t *dev = rh->devices[port_id - 1];
|
---|
| 182 | if (!dev) {
|
---|
[a4e26882] | 183 | /* Must be extraneous call. */
|
---|
[766043c] | 184 | return EOK;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[a4e26882] | 187 | usb_log_info("Device '%s' at port %u has been disconnected.", ddf_fun_get_name(dev->base.fun), port_id);
|
---|
| 188 |
|
---|
[40a3bfa] | 189 | /* Mark the device as detached. */
|
---|
[2cf28b9] | 190 | fibril_mutex_lock(&rh->device.base.guard);
|
---|
[a4e26882] | 191 | list_remove(&dev->base.link);
|
---|
| 192 | rh->devices[port_id - 1] = NULL;
|
---|
[2cf28b9] | 193 | fibril_mutex_unlock(&rh->device.base.guard);
|
---|
| 194 |
|
---|
[31cca4f3] | 195 | /* Remove device from XHCI bus. */
|
---|
| 196 | if ((err = xhci_bus_remove_device(&rh->hc->bus, rh->hc, &dev->base))) {
|
---|
| 197 | usb_log_warning("Failed to remove device '%s' from XHCI bus: %s",
|
---|
| 198 | ddf_fun_get_name(dev->base.fun), str_error(err));
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[f45c78f] | 201 | return EOK;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
[dcf0597] | 204 | /** Handle an incoming Port Change Detected Event.
|
---|
| 205 | */
|
---|
| 206 | int xhci_rh_handle_port_status_change_event(xhci_hc_t *hc, xhci_trb_t *trb)
|
---|
[c8bb7090] | 207 | {
|
---|
[f7bd246] | 208 | uint8_t port_id = XHCI_QWORD_EXTRACT(trb->parameter, 31, 24);
|
---|
[dcf0597] | 209 | usb_log_debug("Port status change event detected for port %u.", port_id);
|
---|
[c8bb7090] | 210 |
|
---|
[dcf0597] | 211 | /**
|
---|
| 212 | * We can't be sure that the port change this event announces is the
|
---|
| 213 | * only port change that happened (see section 4.19.2 of the xHCI
|
---|
| 214 | * specification). Therefore, we just check all ports for changes.
|
---|
| 215 | */
|
---|
| 216 | xhci_rh_handle_port_change(&hc->rh);
|
---|
[07c08ea] | 217 |
|
---|
| 218 | return EOK;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[dcf0597] | 221 | void xhci_rh_handle_port_change(xhci_rh_t *rh)
|
---|
[d32d51d] | 222 | {
|
---|
[dcf0597] | 223 | for (uint8_t i = 1; i <= rh->max_ports; ++i) {
|
---|
| 224 | xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[i - 1];
|
---|
[07c08ea] | 225 |
|
---|
[dcf0597] | 226 | uint32_t events = XHCI_REG_RD_FIELD(®s->portsc, 32);
|
---|
| 227 | XHCI_REG_WR_FIELD(®s->portsc, events, 32);
|
---|
[5c5c9407] | 228 |
|
---|
[dcf0597] | 229 | events &= port_change_mask;
|
---|
[5c5c9407] | 230 |
|
---|
[dcf0597] | 231 | if (events & XHCI_REG_MASK(XHCI_PORT_CSC)) {
|
---|
| 232 | usb_log_info("Connected state changed on port %u.", i);
|
---|
| 233 | events &= ~XHCI_REG_MASK(XHCI_PORT_CSC);
|
---|
[5c5c9407] | 234 |
|
---|
[dcf0597] | 235 | bool connected = XHCI_REG_RD(regs, XHCI_PORT_CCS);
|
---|
[f45c78f] | 236 | if (connected) {
|
---|
[dcf0597] | 237 | handle_connected_device(rh, i);
|
---|
[f45c78f] | 238 | } else {
|
---|
| 239 | handle_disconnected_device(rh, i);
|
---|
| 240 | }
|
---|
[dcf0597] | 241 | }
|
---|
[5c5c9407] | 242 |
|
---|
[dcf0597] | 243 | if (events & XHCI_REG_MASK(XHCI_PORT_PEC)) {
|
---|
| 244 | usb_log_info("Port enabled changed on port %u.", i);
|
---|
| 245 | events &= ~XHCI_REG_MASK(XHCI_PORT_PEC);
|
---|
| 246 | }
|
---|
[5c5c9407] | 247 |
|
---|
[dcf0597] | 248 | if (events & XHCI_REG_MASK(XHCI_PORT_WRC)) {
|
---|
| 249 | usb_log_info("Warm port reset on port %u completed.", i);
|
---|
| 250 | events &= ~XHCI_REG_MASK(XHCI_PORT_WRC);
|
---|
| 251 | }
|
---|
[5c5c9407] | 252 |
|
---|
[dcf0597] | 253 | if (events & XHCI_REG_MASK(XHCI_PORT_OCC)) {
|
---|
| 254 | usb_log_info("Over-current change on port %u.", i);
|
---|
| 255 | events &= ~XHCI_REG_MASK(XHCI_PORT_OCC);
|
---|
| 256 | }
|
---|
[9876e34] | 257 |
|
---|
[dcf0597] | 258 | if (events & XHCI_REG_MASK(XHCI_PORT_PRC)) {
|
---|
| 259 | usb_log_info("Port reset on port %u completed.", i);
|
---|
| 260 | events &= ~XHCI_REG_MASK(XHCI_PORT_PRC);
|
---|
[2297fab] | 261 |
|
---|
| 262 | const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, i);
|
---|
| 263 | if (speed->major != 3) {
|
---|
| 264 | /* FIXME: We probably don't want to do this
|
---|
| 265 | * every time USB2 port is reset. This is a
|
---|
| 266 | * temporary workaround. */
|
---|
[867b375] | 267 | rh_setup_device(rh, i);
|
---|
[2297fab] | 268 | }
|
---|
[dcf0597] | 269 | }
|
---|
[07c08ea] | 270 |
|
---|
[dcf0597] | 271 | if (events & XHCI_REG_MASK(XHCI_PORT_PLC)) {
|
---|
| 272 | usb_log_info("Port link state changed on port %u.", i);
|
---|
| 273 | events &= ~XHCI_REG_MASK(XHCI_PORT_PLC);
|
---|
| 274 | }
|
---|
[9876e34] | 275 |
|
---|
[dcf0597] | 276 | if (events & XHCI_REG_MASK(XHCI_PORT_CEC)) {
|
---|
| 277 | usb_log_info("Port %u failed to configure link.", i);
|
---|
| 278 | events &= ~XHCI_REG_MASK(XHCI_PORT_CEC);
|
---|
| 279 | }
|
---|
[9876e34] | 280 |
|
---|
[dcf0597] | 281 | if (events) {
|
---|
| 282 | usb_log_warning("Port change (0x%08x) ignored on port %u.", events, i);
|
---|
| 283 | }
|
---|
[916991b] | 284 | }
|
---|
[2297fab] | 285 |
|
---|
[dcf0597] | 286 | /**
|
---|
| 287 | * Theory:
|
---|
| 288 | *
|
---|
| 289 | * Although more events could have happened while processing, the PCD
|
---|
| 290 | * bit in USBSTS will be set on every change. Because the PCD is
|
---|
| 291 | * cleared even before the interrupt is cleared, it is safe to assume
|
---|
| 292 | * that this handler will be called again.
|
---|
| 293 | *
|
---|
| 294 | * But because we could have handled the event in previous run of this
|
---|
| 295 | * handler, it is not an error when no event is detected.
|
---|
| 296 | *
|
---|
| 297 | * Reality:
|
---|
| 298 | *
|
---|
| 299 | * The PCD bit is never set. TODO Check why the interrupt never carries
|
---|
| 300 | * the PCD flag. Possibly repeat the checking until we're sure the
|
---|
| 301 | * PSCEG is 0 - check section 4.19.2 of the xHCI spec.
|
---|
| 302 | */
|
---|
[07c08ea] | 303 | }
|
---|
| 304 |
|
---|
[25251bb] | 305 | static inline int get_hub_available_bandwidth(xhci_hc_t *hc, xhci_device_t* dev, uint8_t speed, xhci_port_bandwidth_ctx_t *ctx) {
|
---|
[60af4cdb] | 306 | // TODO: find a correct place for this function + API
|
---|
| 307 | // We need speed, because a root hub device has both USB 2 and USB 3 speeds
|
---|
| 308 | // and the command can query only one of them
|
---|
| 309 | // ctx is an out parameter as of now
|
---|
| 310 | assert(dev);
|
---|
[c3d926f3] | 311 | assert(ctx);
|
---|
[60af4cdb] | 312 |
|
---|
[c3d926f3] | 313 | xhci_port_bandwidth_ctx_t *in_ctx = malloc32(sizeof(xhci_port_bandwidth_ctx_t));
|
---|
| 314 | if (!in_ctx) {
|
---|
[60af4cdb] | 315 | return ENOMEM;
|
---|
[c3d926f3] | 316 | }
|
---|
[60af4cdb] | 317 |
|
---|
| 318 | xhci_cmd_t cmd;
|
---|
[c3d926f3] | 319 | xhci_cmd_init(&cmd, XHCI_CMD_GET_PORT_BANDWIDTH);
|
---|
[60af4cdb] | 320 |
|
---|
[c3d926f3] | 321 | cmd.bandwidth_ctx = in_ctx;
|
---|
| 322 | cmd.device_speed = speed;
|
---|
[60af4cdb] | 323 |
|
---|
[c3d926f3] | 324 | int err;
|
---|
[25251bb] | 325 | if ((err = xhci_cmd_sync(hc, &cmd))) {
|
---|
[c3d926f3] | 326 | goto end;
|
---|
[60af4cdb] | 327 | }
|
---|
| 328 |
|
---|
[c3d926f3] | 329 | memcpy(ctx, in_ctx, sizeof(xhci_port_bandwidth_ctx_t));
|
---|
| 330 |
|
---|
| 331 | end:
|
---|
| 332 | xhci_cmd_fini(&cmd);
|
---|
[60af4cdb] | 333 | return EOK;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[dcf0597] | 336 | const xhci_port_speed_t *xhci_rh_get_port_speed(xhci_rh_t *rh, uint8_t port)
|
---|
[07c08ea] | 337 | {
|
---|
[dcf0597] | 338 | xhci_port_regs_t *port_regs = &rh->hc->op_regs->portrs[port - 1];
|
---|
[916991b] | 339 |
|
---|
[dcf0597] | 340 | unsigned psiv = XHCI_REG_RD(port_regs, XHCI_PORT_PS);
|
---|
[f668d60] | 341 | return &rh->hc->speeds[psiv];
|
---|
[07c08ea] | 342 | }
|
---|
| 343 |
|
---|
[dcf0597] | 344 | int xhci_rh_reset_port(xhci_rh_t* rh, uint8_t port)
|
---|
[07c08ea] | 345 | {
|
---|
[dcf0597] | 346 | usb_log_debug2("Resetting port %u.", port);
|
---|
| 347 | xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port-1];
|
---|
| 348 | XHCI_REG_SET(regs, XHCI_PORT_PR, 1);
|
---|
[9876e34] | 349 |
|
---|
[dcf0597] | 350 | return EOK;
|
---|
[d32d51d] | 351 | }
|
---|
| 352 |
|
---|
| 353 | int xhci_rh_fini(xhci_rh_t *rh)
|
---|
| 354 | {
|
---|
| 355 | /* TODO: Implement me! */
|
---|
[07c08ea] | 356 | usb_log_debug2("Called xhci_rh_fini().");
|
---|
[766043c] | 357 |
|
---|
| 358 | free(rh->devices);
|
---|
| 359 |
|
---|
[d32d51d] | 360 | return EOK;
|
---|
| 361 | }
|
---|
| 362 |
|
---|
[c8bb7090] | 363 | /**
|
---|
| 364 | * @}
|
---|
| 365 | */
|
---|