| [c0ec9e7] | 1 | /*
|
|---|
| [e0a5d4c] | 2 | * Copyright (c) 2018 Petr Manek, Ondrej Hlavaty, Michal Staruch, Jan Hrach
|
|---|
| [c0ec9e7] | 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 host controller endpoint management.
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| [41924f30] | 36 | #include <usb/host/endpoint.h>
|
|---|
| [9b2f69e] | 37 | #include <usb/descriptor.h>
|
|---|
| [41924f30] | 38 |
|
|---|
| [c0ec9e7] | 39 | #include <errno.h>
|
|---|
| [2cf28b9] | 40 | #include <macros.h>
|
|---|
| [682c9354] | 41 | #include <str_error.h>
|
|---|
| [c0ec9e7] | 42 |
|
|---|
| [2cf28b9] | 43 | #include "hc.h"
|
|---|
| [41924f30] | 44 | #include "bus.h"
|
|---|
| [d7869d7e] | 45 | #include "commands.h"
|
|---|
| [682c9354] | 46 | #include "device.h"
|
|---|
| [c0ec9e7] | 47 | #include "endpoint.h"
|
|---|
| [47e9494] | 48 | #include "streams.h"
|
|---|
| [c0ec9e7] | 49 |
|
|---|
| [45457265] | 50 | static errno_t alloc_transfer_ds(xhci_endpoint_t *);
|
|---|
| [0eadfd1e] | 51 |
|
|---|
| [eb928c4] | 52 | /**
|
|---|
| 53 | * Initialize new XHCI endpoint.
|
|---|
| [2e2af3a] | 54 | * @param[in] xhci_ep Allocated XHCI endpoint to initialize.
|
|---|
| 55 | * @param[in] dev Device, to which the endpoint belongs.
|
|---|
| 56 | * @param[in] desc USB endpoint descriptor carrying configuration data.
|
|---|
| 57 | *
|
|---|
| 58 | * @return Error code.
|
|---|
| 59 | */
|
|---|
| [45457265] | 60 | static errno_t xhci_endpoint_init(xhci_endpoint_t *xhci_ep, device_t *dev,
|
|---|
| [eb862fd] | 61 | const usb_endpoint_descriptors_t *desc)
|
|---|
| [c0ec9e7] | 62 | {
|
|---|
| [45457265] | 63 | errno_t rc;
|
|---|
| [41924f30] | 64 | assert(xhci_ep);
|
|---|
| [176a70a] | 65 |
|
|---|
| [41924f30] | 66 | endpoint_t *ep = &xhci_ep->base;
|
|---|
| [176a70a] | 67 |
|
|---|
| [6832245] | 68 | endpoint_init(ep, dev, desc);
|
|---|
| 69 |
|
|---|
| [4db49344] | 70 | fibril_mutex_initialize(&xhci_ep->guard);
|
|---|
| 71 |
|
|---|
| [9efad54] | 72 | xhci_ep->max_burst = desc->companion.max_burst + 1;
|
|---|
| [398a94c] | 73 |
|
|---|
| 74 | if (ep->transfer_type == USB_TRANSFER_BULK)
|
|---|
| 75 | xhci_ep->max_streams = 1 << (USB_SSC_MAX_STREAMS(desc->companion));
|
|---|
| 76 | else
|
|---|
| 77 | xhci_ep->max_streams = 1;
|
|---|
| 78 |
|
|---|
| 79 | if (ep->transfer_type == USB_TRANSFER_ISOCHRONOUS)
|
|---|
| 80 | xhci_ep->mult = USB_SSC_MULT(desc->companion) + 1;
|
|---|
| 81 | else
|
|---|
| 82 | xhci_ep->mult = 1;
|
|---|
| [6832245] | 83 |
|
|---|
| [8033f89] | 84 | /*
|
|---|
| 85 | * In USB 3, the semantics of wMaxPacketSize changed. Now the number of
|
|---|
| [9efad54] | 86 | * packets per service interval is determined from max_burst and mult.
|
|---|
| 87 | */
|
|---|
| 88 | if (dev->speed >= USB_SPEED_SUPER) {
|
|---|
| 89 | ep->packets_per_uframe = xhci_ep->max_burst * xhci_ep->mult;
|
|---|
| [3038d51] | 90 | if (ep->transfer_type == USB_TRANSFER_ISOCHRONOUS
|
|---|
| 91 | || ep->transfer_type == USB_TRANSFER_INTERRUPT) {
|
|---|
| 92 | ep->max_transfer_size = ep->max_packet_size * ep->packets_per_uframe;
|
|---|
| 93 | }
|
|---|
| [9efad54] | 94 | }
|
|---|
| 95 |
|
|---|
| 96 | xhci_ep->interval = desc->endpoint.poll_interval;
|
|---|
| [bdd8842c] | 97 |
|
|---|
| 98 | /*
|
|---|
| 99 | * Only Low/Full speed interrupt endpoints have interval as a linear field,
|
|---|
| [9efad54] | 100 | * others have 2-based log of it.
|
|---|
| 101 | */
|
|---|
| [8033f89] | 102 | if (dev->speed >= USB_SPEED_HIGH
|
|---|
| 103 | || ep->transfer_type != USB_TRANSFER_INTERRUPT) {
|
|---|
| [708d8fcd] | 104 | xhci_ep->interval = 1 << (xhci_ep->interval - 1);
|
|---|
| [bdd8842c] | 105 | }
|
|---|
| 106 |
|
|---|
| 107 | /* Full speed devices have interval in frames */
|
|---|
| 108 | if (dev->speed <= USB_SPEED_FULL) {
|
|---|
| 109 | xhci_ep->interval *= 8;
|
|---|
| [9efad54] | 110 | }
|
|---|
| [5c75456] | 111 |
|
|---|
| [708d8fcd] | 112 | if (xhci_ep->base.transfer_type == USB_TRANSFER_ISOCHRONOUS)
|
|---|
| 113 | isoch_init(xhci_ep, desc);
|
|---|
| [41924f30] | 114 |
|
|---|
| [0eadfd1e] | 115 | if ((rc = alloc_transfer_ds(xhci_ep)))
|
|---|
| 116 | goto err;
|
|---|
| 117 |
|
|---|
| [1d758fc] | 118 | unsigned flags = -1U;
|
|---|
| [fdc2253b] | 119 |
|
|---|
| 120 | /* Some xHCs can handle 64-bit addresses */
|
|---|
| 121 | xhci_bus_t *bus = bus_to_xhci_bus(ep->device->bus);
|
|---|
| 122 | if (bus->hc->ac64)
|
|---|
| [1d758fc] | 123 | flags &= ~DMA_POLICY_4GiB;
|
|---|
| 124 |
|
|---|
| 125 | /* xHCI works best if it can fit 65k transfers in one TRB */
|
|---|
| 126 | ep->transfer_buffer_policy = dma_policy_create(flags, 1 << 16);
|
|---|
| 127 |
|
|---|
| 128 | /* But actualy can do full scatter-gather. */
|
|---|
| 129 | ep->required_transfer_buffer_policy = dma_policy_create(flags, PAGE_SIZE);
|
|---|
| [fdc2253b] | 130 |
|
|---|
| [89cefe78] | 131 | return EOK;
|
|---|
| [0eadfd1e] | 132 |
|
|---|
| 133 | err:
|
|---|
| 134 | return rc;
|
|---|
| [c0ec9e7] | 135 | }
|
|---|
| 136 |
|
|---|
| [682c9354] | 137 | /**
|
|---|
| 138 | * Create a new xHCI endpoint structure.
|
|---|
| 139 | *
|
|---|
| 140 | * Bus callback.
|
|---|
| 141 | */
|
|---|
| [8033f89] | 142 | endpoint_t *xhci_endpoint_create(device_t *dev,
|
|---|
| [eb862fd] | 143 | const usb_endpoint_descriptors_t *desc)
|
|---|
| [682c9354] | 144 | {
|
|---|
| 145 | const usb_transfer_type_t type = USB_ED_GET_TRANSFER_TYPE(desc->endpoint);
|
|---|
| 146 |
|
|---|
| 147 | xhci_endpoint_t *ep = calloc(1, sizeof(xhci_endpoint_t)
|
|---|
| 148 | + (type == USB_TRANSFER_ISOCHRONOUS) * sizeof(*ep->isoch));
|
|---|
| 149 | if (!ep)
|
|---|
| 150 | return NULL;
|
|---|
| 151 |
|
|---|
| 152 | if (xhci_endpoint_init(ep, dev, desc)) {
|
|---|
| 153 | free(ep);
|
|---|
| 154 | return NULL;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | return &ep->base;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| [eb928c4] | 160 | /**
|
|---|
| 161 | * Finalize XHCI endpoint.
|
|---|
| [2e2af3a] | 162 | * @param[in] xhci_ep XHCI endpoint to finalize.
|
|---|
| 163 | */
|
|---|
| [682c9354] | 164 | static void xhci_endpoint_fini(xhci_endpoint_t *xhci_ep)
|
|---|
| [c0ec9e7] | 165 | {
|
|---|
| [41924f30] | 166 | assert(xhci_ep);
|
|---|
| 167 |
|
|---|
| [1af4c00] | 168 | xhci_endpoint_free_transfer_ds(xhci_ep);
|
|---|
| [0eadfd1e] | 169 |
|
|---|
| [89cefe78] | 170 | // TODO: Something missed?
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| [682c9354] | 173 | /**
|
|---|
| 174 | * Destroy given xHCI endpoint structure.
|
|---|
| 175 | *
|
|---|
| 176 | * Bus callback.
|
|---|
| 177 | */
|
|---|
| 178 | void xhci_endpoint_destroy(endpoint_t *ep)
|
|---|
| 179 | {
|
|---|
| 180 | xhci_endpoint_t *xhci_ep = xhci_endpoint_get(ep);
|
|---|
| 181 |
|
|---|
| 182 | xhci_endpoint_fini(xhci_ep);
|
|---|
| 183 | free(xhci_ep);
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 | /**
|
|---|
| 188 | * Register an andpoint to the xHC.
|
|---|
| 189 | *
|
|---|
| 190 | * Bus callback.
|
|---|
| 191 | */
|
|---|
| [45457265] | 192 | errno_t xhci_endpoint_register(endpoint_t *ep_base)
|
|---|
| [682c9354] | 193 | {
|
|---|
| [45457265] | 194 | errno_t err;
|
|---|
| [682c9354] | 195 | xhci_endpoint_t *ep = xhci_endpoint_get(ep_base);
|
|---|
| 196 |
|
|---|
| [4db49344] | 197 | if (ep_base->endpoint != 0 && (err = hc_add_endpoint(ep)))
|
|---|
| [682c9354] | 198 | return err;
|
|---|
| 199 |
|
|---|
| [4db49344] | 200 | endpoint_set_online(ep_base, &ep->guard);
|
|---|
| [682c9354] | 201 | return EOK;
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | /**
|
|---|
| 205 | * Abort a transfer on an endpoint.
|
|---|
| 206 | */
|
|---|
| [4db49344] | 207 | static void endpoint_abort(endpoint_t *ep)
|
|---|
| [682c9354] | 208 | {
|
|---|
| 209 | xhci_device_t *dev = xhci_device_get(ep->device);
|
|---|
| [51c1d500] | 210 | xhci_endpoint_t *xhci_ep = xhci_endpoint_get(ep);
|
|---|
| [682c9354] | 211 |
|
|---|
| [4db49344] | 212 | /* This function can only abort endpoints without streams. */
|
|---|
| 213 | assert(xhci_ep->primary_stream_data_array == NULL);
|
|---|
| [682c9354] | 214 |
|
|---|
| [4db49344] | 215 | fibril_mutex_lock(&xhci_ep->guard);
|
|---|
| 216 |
|
|---|
| 217 | endpoint_set_offline_locked(ep);
|
|---|
| 218 |
|
|---|
| 219 | if (!ep->active_batch) {
|
|---|
| 220 | fibril_mutex_unlock(&xhci_ep->guard);
|
|---|
| 221 | return;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | /* First, offer the batch a short chance to be finished. */
|
|---|
| 225 | endpoint_wait_timeout_locked(ep, 10000);
|
|---|
| 226 |
|
|---|
| 227 | if (!ep->active_batch) {
|
|---|
| 228 | fibril_mutex_unlock(&xhci_ep->guard);
|
|---|
| 229 | return;
|
|---|
| [682c9354] | 230 | }
|
|---|
| 231 |
|
|---|
| [4db49344] | 232 | usb_transfer_batch_t * const batch = ep->active_batch;
|
|---|
| 233 |
|
|---|
| [45457265] | 234 | const errno_t err = hc_stop_endpoint(xhci_ep);
|
|---|
| [4db49344] | 235 | if (err) {
|
|---|
| 236 | usb_log_error("Failed to stop endpoint %u of device "
|
|---|
| 237 | XHCI_DEV_FMT ": %s", ep->endpoint, XHCI_DEV_ARGS(*dev),
|
|---|
| 238 | str_error(err));
|
|---|
| [682c9354] | 239 | }
|
|---|
| [4db49344] | 240 |
|
|---|
| 241 | fibril_mutex_unlock(&xhci_ep->guard);
|
|---|
| 242 |
|
|---|
| 243 | batch->error = EINTR;
|
|---|
| 244 | batch->transferred_size = 0;
|
|---|
| 245 | usb_transfer_batch_finish(batch);
|
|---|
| 246 | return;
|
|---|
| [682c9354] | 247 | }
|
|---|
| 248 |
|
|---|
| 249 | /**
|
|---|
| 250 | * Unregister an endpoint. If the device is still available, inform the xHC
|
|---|
| 251 | * about it.
|
|---|
| 252 | *
|
|---|
| 253 | * Bus callback.
|
|---|
| 254 | */
|
|---|
| 255 | void xhci_endpoint_unregister(endpoint_t *ep_base)
|
|---|
| 256 | {
|
|---|
| [45457265] | 257 | errno_t err;
|
|---|
| [682c9354] | 258 | xhci_endpoint_t *ep = xhci_endpoint_get(ep_base);
|
|---|
| 259 | xhci_device_t *dev = xhci_device_get(ep_base->device);
|
|---|
| 260 |
|
|---|
| 261 | endpoint_abort(ep_base);
|
|---|
| 262 |
|
|---|
| 263 | /* If device slot is still available, drop the endpoint. */
|
|---|
| [4db49344] | 264 | if (ep_base->endpoint != 0 && dev->slot_id) {
|
|---|
| [682c9354] | 265 |
|
|---|
| [51c1d500] | 266 | if ((err = hc_drop_endpoint(ep))) {
|
|---|
| [8033f89] | 267 | usb_log_error("Failed to drop endpoint " XHCI_EP_FMT ": %s",
|
|---|
| 268 | XHCI_EP_ARGS(*ep), str_error(err));
|
|---|
| [682c9354] | 269 | }
|
|---|
| 270 | } else {
|
|---|
| 271 | usb_log_debug("Not going to drop endpoint " XHCI_EP_FMT " because"
|
|---|
| 272 | " the slot has already been disabled.", XHCI_EP_ARGS(*ep));
|
|---|
| 273 | }
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| [eb928c4] | 276 | /**
|
|---|
| 277 | * Determine the type of a XHCI endpoint.
|
|---|
| [2e2af3a] | 278 | * @param[in] ep XHCI endpoint to query.
|
|---|
| 279 | *
|
|---|
| 280 | * @return EP_TYPE_[CONTROL|ISOCH|BULK|INTERRUPT]_[IN|OUT]
|
|---|
| 281 | */
|
|---|
| [47e9494] | 282 | int xhci_endpoint_type(xhci_endpoint_t *ep)
|
|---|
| [3f6c94ed] | 283 | {
|
|---|
| 284 | const bool in = ep->base.direction == USB_DIRECTION_IN;
|
|---|
| 285 |
|
|---|
| 286 | switch (ep->base.transfer_type) {
|
|---|
| 287 | case USB_TRANSFER_CONTROL:
|
|---|
| 288 | return EP_TYPE_CONTROL;
|
|---|
| 289 |
|
|---|
| 290 | case USB_TRANSFER_ISOCHRONOUS:
|
|---|
| 291 | return in ? EP_TYPE_ISOCH_IN
|
|---|
| 292 | : EP_TYPE_ISOCH_OUT;
|
|---|
| 293 |
|
|---|
| 294 | case USB_TRANSFER_BULK:
|
|---|
| 295 | return in ? EP_TYPE_BULK_IN
|
|---|
| 296 | : EP_TYPE_BULK_OUT;
|
|---|
| 297 |
|
|---|
| 298 | case USB_TRANSFER_INTERRUPT:
|
|---|
| 299 | return in ? EP_TYPE_INTERRUPT_IN
|
|---|
| 300 | : EP_TYPE_INTERRUPT_OUT;
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | return EP_TYPE_INVALID;
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| [8033f89] | 306 | /**
|
|---|
| 307 | * Allocate transfer data structures for XHCI endpoint not using streams.
|
|---|
| [2e2af3a] | 308 | * @param[in] xhci_ep XHCI endpoint to allocate data structures for.
|
|---|
| 309 | *
|
|---|
| 310 | * @return Error code.
|
|---|
| 311 | */
|
|---|
| [45457265] | 312 | static errno_t alloc_transfer_ds(xhci_endpoint_t *xhci_ep)
|
|---|
| [3f6c94ed] | 313 | {
|
|---|
| [9620a54] | 314 | /* Can't use XHCI_EP_FMT because the endpoint may not have device. */
|
|---|
| [8033f89] | 315 | usb_log_debug("Allocating main transfer ring for endpoint " XHCI_EP_FMT,
|
|---|
| 316 | XHCI_EP_ARGS(*xhci_ep));
|
|---|
| [3f6c94ed] | 317 |
|
|---|
| [1af4c00] | 318 | xhci_ep->primary_stream_data_array = NULL;
|
|---|
| 319 | xhci_ep->primary_stream_data_size = 0;
|
|---|
| [3f6c94ed] | 320 |
|
|---|
| [45457265] | 321 | errno_t err;
|
|---|
| [998773d] | 322 | if ((err = xhci_trb_ring_init(&xhci_ep->ring, 0))) {
|
|---|
| [3f6c94ed] | 323 | return err;
|
|---|
| [89cefe78] | 324 | }
|
|---|
| 325 |
|
|---|
| [6b433a8] | 326 | if (xhci_ep->base.transfer_type == USB_TRANSFER_ISOCHRONOUS) {
|
|---|
| [708d8fcd] | 327 | if ((err = isoch_alloc_transfers(xhci_ep))) {
|
|---|
| [6b433a8] | 328 | xhci_trb_ring_fini(&xhci_ep->ring);
|
|---|
| 329 | return err;
|
|---|
| 330 | }
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| [89cefe78] | 333 | return EOK;
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| [8033f89] | 336 | /**
|
|---|
| 337 | * Free transfer data structures for XHCI endpoint.
|
|---|
| [2e2af3a] | 338 | * @param[in] xhci_ep XHCI endpoint to free data structures for.
|
|---|
| 339 | */
|
|---|
| [1af4c00] | 340 | void xhci_endpoint_free_transfer_ds(xhci_endpoint_t *xhci_ep)
|
|---|
| [89cefe78] | 341 | {
|
|---|
| [47e9494] | 342 | if (xhci_ep->primary_stream_data_size) {
|
|---|
| 343 | xhci_stream_free_ds(xhci_ep);
|
|---|
| [89cefe78] | 344 | } else {
|
|---|
| [8033f89] | 345 | usb_log_debug("Freeing main transfer ring of endpoint " XHCI_EP_FMT,
|
|---|
| 346 | XHCI_EP_ARGS(*xhci_ep));
|
|---|
| [9620a54] | 347 | xhci_trb_ring_fini(&xhci_ep->ring);
|
|---|
| [89cefe78] | 348 | }
|
|---|
| [bd1fab90] | 349 |
|
|---|
| [398a94c] | 350 | if (xhci_ep->base.transfer_type == USB_TRANSFER_ISOCHRONOUS)
|
|---|
| 351 | isoch_fini(xhci_ep);
|
|---|
| [c0ec9e7] | 352 | }
|
|---|
| 353 |
|
|---|
| [51c1d500] | 354 | xhci_trb_ring_t *xhci_endpoint_get_ring(xhci_endpoint_t *ep, uint32_t stream_id)
|
|---|
| [c10daa8] | 355 | {
|
|---|
| [51c1d500] | 356 | if (ep->primary_stream_data_size == 0)
|
|---|
| 357 | return stream_id == 0 ? &ep->ring : NULL;
|
|---|
| [9b2f69e] | 358 |
|
|---|
| [51c1d500] | 359 | xhci_stream_data_t *stream_data = xhci_get_stream_ctx_data(ep, stream_id);
|
|---|
| 360 | if (stream_data == NULL) {
|
|---|
| 361 | usb_log_warning("No transfer ring was found for stream %u.", stream_id);
|
|---|
| 362 | return NULL;
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | return &stream_data->ring;
|
|---|
| [9b2f69e] | 366 | }
|
|---|
| 367 |
|
|---|
| [8033f89] | 368 | /**
|
|---|
| 369 | * Configure endpoint context of a control endpoint.
|
|---|
| [2e2af3a] | 370 | * @param[in] ep XHCI control endpoint.
|
|---|
| 371 | * @param[in] ctx Endpoint context to configure.
|
|---|
| 372 | */
|
|---|
| [89cefe78] | 373 | static void setup_control_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
|
|---|
| [9b2f69e] | 374 | {
|
|---|
| 375 | XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
|
|---|
| 376 | XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size);
|
|---|
| [bdd8842c] | 377 | XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst - 1);
|
|---|
| 378 | XHCI_EP_MULT_SET(*ctx, ep->mult - 1);
|
|---|
| [9b2f69e] | 379 | XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
|
|---|
| [89cefe78] | 380 | XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
|
|---|
| [9b2f69e] | 381 | XHCI_EP_DCS_SET(*ctx, 1);
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| [8033f89] | 384 | /**
|
|---|
| 385 | * Configure endpoint context of a bulk endpoint.
|
|---|
| [2e2af3a] | 386 | * @param[in] ep XHCI bulk endpoint.
|
|---|
| 387 | * @param[in] ctx Endpoint context to configure.
|
|---|
| 388 | */
|
|---|
| [89cefe78] | 389 | static void setup_bulk_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
|
|---|
| [9b2f69e] | 390 | {
|
|---|
| 391 | XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
|
|---|
| 392 | XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size);
|
|---|
| [bdd8842c] | 393 | XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst - 1);
|
|---|
| [9b2f69e] | 394 | XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
|
|---|
| 395 |
|
|---|
| [3f6c94ed] | 396 | XHCI_EP_MAX_P_STREAMS_SET(*ctx, 0);
|
|---|
| 397 | XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
|
|---|
| 398 | XHCI_EP_DCS_SET(*ctx, 1);
|
|---|
| [9b2f69e] | 399 | }
|
|---|
| 400 |
|
|---|
| [8033f89] | 401 | /**
|
|---|
| 402 | * Configure endpoint context of a isochronous endpoint.
|
|---|
| [2e2af3a] | 403 | * @param[in] ep XHCI isochronous endpoint.
|
|---|
| 404 | * @param[in] ctx Endpoint context to configure.
|
|---|
| 405 | */
|
|---|
| [89cefe78] | 406 | static void setup_isoch_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
|
|---|
| [9b2f69e] | 407 | {
|
|---|
| 408 | XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
|
|---|
| 409 | XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size & 0x07FF);
|
|---|
| [bdd8842c] | 410 | XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst - 1);
|
|---|
| 411 | XHCI_EP_MULT_SET(*ctx, ep->mult - 1);
|
|---|
| [9b2f69e] | 412 | XHCI_EP_ERROR_COUNT_SET(*ctx, 0);
|
|---|
| [89cefe78] | 413 | XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
|
|---|
| [9b2f69e] | 414 | XHCI_EP_DCS_SET(*ctx, 1);
|
|---|
| [708d8fcd] | 415 | XHCI_EP_INTERVAL_SET(*ctx, fnzb32(ep->interval) % 32);
|
|---|
| [6b433a8] | 416 |
|
|---|
| [17c5e62] | 417 | XHCI_EP_MAX_ESIT_PAYLOAD_LO_SET(*ctx, ep->isoch->max_size & 0xFFFF);
|
|---|
| 418 | XHCI_EP_MAX_ESIT_PAYLOAD_HI_SET(*ctx, (ep->isoch->max_size >> 16) & 0xFF);
|
|---|
| [9b2f69e] | 419 | }
|
|---|
| 420 |
|
|---|
| [8033f89] | 421 | /**
|
|---|
| 422 | * Configure endpoint context of a interrupt endpoint.
|
|---|
| [2e2af3a] | 423 | * @param[in] ep XHCI interrupt endpoint.
|
|---|
| 424 | * @param[in] ctx Endpoint context to configure.
|
|---|
| 425 | */
|
|---|
| [89cefe78] | 426 | static void setup_interrupt_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
|
|---|
| [9b2f69e] | 427 | {
|
|---|
| 428 | XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
|
|---|
| 429 | XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size & 0x07FF);
|
|---|
| [bdd8842c] | 430 | XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst - 1);
|
|---|
| [9b2f69e] | 431 | XHCI_EP_MULT_SET(*ctx, 0);
|
|---|
| 432 | XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
|
|---|
| [89cefe78] | 433 | XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
|
|---|
| [9b2f69e] | 434 | XHCI_EP_DCS_SET(*ctx, 1);
|
|---|
| [708d8fcd] | 435 | XHCI_EP_INTERVAL_SET(*ctx, fnzb32(ep->interval) % 32);
|
|---|
| [9b2f69e] | 436 | // TODO: max ESIT payload
|
|---|
| [c10daa8] | 437 | }
|
|---|
| 438 |
|
|---|
| [2e2af3a] | 439 | /** Type of endpoint context configuration function. */
|
|---|
| [89cefe78] | 440 | typedef void (*setup_ep_ctx_helper)(xhci_endpoint_t *, xhci_ep_ctx_t *);
|
|---|
| 441 |
|
|---|
| [8033f89] | 442 | /**
|
|---|
| 443 | * Static array, which maps USB endpoint types to their respective endpoint
|
|---|
| 444 | * context configuration functions.
|
|---|
| 445 | */
|
|---|
| [89cefe78] | 446 | static const setup_ep_ctx_helper setup_ep_ctx_helpers[] = {
|
|---|
| 447 | [USB_TRANSFER_CONTROL] = setup_control_ep_ctx,
|
|---|
| 448 | [USB_TRANSFER_ISOCHRONOUS] = setup_isoch_ep_ctx,
|
|---|
| 449 | [USB_TRANSFER_BULK] = setup_bulk_ep_ctx,
|
|---|
| 450 | [USB_TRANSFER_INTERRUPT] = setup_interrupt_ep_ctx,
|
|---|
| 451 | };
|
|---|
| 452 |
|
|---|
| [2e2af3a] | 453 | /** Configure endpoint context of XHCI endpoint.
|
|---|
| 454 | * @param[in] ep Associated XHCI endpoint.
|
|---|
| 455 | * @param[in] ep_ctx Endpoint context to configure.
|
|---|
| 456 | */
|
|---|
| [0206d35] | 457 | void xhci_setup_endpoint_context(xhci_endpoint_t *ep, xhci_ep_ctx_t *ep_ctx)
|
|---|
| 458 | {
|
|---|
| 459 | assert(ep);
|
|---|
| 460 | assert(ep_ctx);
|
|---|
| 461 |
|
|---|
| 462 | usb_transfer_type_t tt = ep->base.transfer_type;
|
|---|
| 463 |
|
|---|
| 464 | memset(ep_ctx, 0, sizeof(*ep_ctx));
|
|---|
| 465 | setup_ep_ctx_helpers[tt](ep, ep_ctx);
|
|---|
| 466 | }
|
|---|
| 467 |
|
|---|
| [8fe29a7c] | 468 | /**
|
|---|
| 469 | * Clear endpoint halt condition by resetting the endpoint and skipping the
|
|---|
| 470 | * offending transfer.
|
|---|
| 471 | */
|
|---|
| [45457265] | 472 | errno_t xhci_endpoint_clear_halt(xhci_endpoint_t *ep, uint32_t stream_id)
|
|---|
| [8fe29a7c] | 473 | {
|
|---|
| [45457265] | 474 | errno_t err;
|
|---|
| [8fe29a7c] | 475 |
|
|---|
| [51c1d500] | 476 | if ((err = hc_reset_endpoint(ep)))
|
|---|
| [8fe29a7c] | 477 | return err;
|
|---|
| 478 |
|
|---|
| [51c1d500] | 479 | if ((err = hc_reset_ring(ep, stream_id)))
|
|---|
| [8fe29a7c] | 480 | return err;
|
|---|
| 481 |
|
|---|
| 482 | return EOK;
|
|---|
| 483 | }
|
|---|
| 484 |
|
|---|
| [c0ec9e7] | 485 | /**
|
|---|
| 486 | * @}
|
|---|
| 487 | */
|
|---|