| 1 | /*
|
|---|
| 2 | * Copyright (c) 2017 Petr Manek
|
|---|
| 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 |
|
|---|
| 36 | #include <usb/host/endpoint.h>
|
|---|
| 37 | #include <usb/descriptor.h>
|
|---|
| 38 |
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <macros.h>
|
|---|
| 41 |
|
|---|
| 42 | #include "hc.h"
|
|---|
| 43 | #include "bus.h"
|
|---|
| 44 | #include "commands.h"
|
|---|
| 45 | #include "endpoint.h"
|
|---|
| 46 |
|
|---|
| 47 | static int alloc_transfer_ds(xhci_endpoint_t *);
|
|---|
| 48 | static void free_transfer_ds(xhci_endpoint_t *);
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | * Initialize new XHCI endpoint.
|
|---|
| 52 | * @param[in] xhci_ep Allocated XHCI endpoint to initialize.
|
|---|
| 53 | * @param[in] dev Device, to which the endpoint belongs.
|
|---|
| 54 | * @param[in] desc USB endpoint descriptor carrying configuration data.
|
|---|
| 55 | *
|
|---|
| 56 | * @return Error code.
|
|---|
| 57 | */
|
|---|
| 58 | int xhci_endpoint_init(xhci_endpoint_t *xhci_ep, device_t *dev, const usb_endpoint_descriptors_t *desc)
|
|---|
| 59 | {
|
|---|
| 60 | int rc;
|
|---|
| 61 | assert(xhci_ep);
|
|---|
| 62 |
|
|---|
| 63 | endpoint_t *ep = &xhci_ep->base;
|
|---|
| 64 |
|
|---|
| 65 | endpoint_init(ep, dev, desc);
|
|---|
| 66 |
|
|---|
| 67 | xhci_ep->max_streams = 1 << (USB_SSC_MAX_STREAMS(desc->companion));
|
|---|
| 68 | xhci_ep->max_burst = desc->companion.max_burst + 1;
|
|---|
| 69 | xhci_ep->mult = USB_SSC_MULT(desc->companion) + 1;
|
|---|
| 70 |
|
|---|
| 71 | /* In USB 3, the semantics of wMaxPacketSize changed. Now the number of
|
|---|
| 72 | * packets per service interval is determined from max_burst and mult.
|
|---|
| 73 | */
|
|---|
| 74 | if (dev->speed >= USB_SPEED_SUPER) {
|
|---|
| 75 | ep->packets_per_uframe = xhci_ep->max_burst * xhci_ep->mult;
|
|---|
| 76 | ep->max_transfer_size = ep->max_packet_size * ep->packets_per_uframe;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | xhci_ep->interval = desc->endpoint.poll_interval;
|
|---|
| 80 |
|
|---|
| 81 | /*
|
|---|
| 82 | * Only Low/Full speed interrupt endpoints have interval as a linear field,
|
|---|
| 83 | * others have 2-based log of it.
|
|---|
| 84 | */
|
|---|
| 85 | if (dev->speed >= USB_SPEED_HIGH || ep->transfer_type != USB_TRANSFER_INTERRUPT) {
|
|---|
| 86 | xhci_ep->interval = desc->endpoint.poll_interval;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /* Full speed devices have interval in frames */
|
|---|
| 90 | if (dev->speed <= USB_SPEED_FULL) {
|
|---|
| 91 | xhci_ep->interval *= 8;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | if (xhci_ep->base.transfer_type == USB_TRANSFER_ISOCHRONOUS) {
|
|---|
| 95 | xhci_ep->isoch->max_size = desc->companion.bytes_per_interval
|
|---|
| 96 | ? desc->companion.bytes_per_interval
|
|---|
| 97 | : ep->max_transfer_size;
|
|---|
| 98 | /* Technically there could be superspeed plus too. */
|
|---|
| 99 |
|
|---|
| 100 | /* Allocate and setup isochronous-specific structures. */
|
|---|
| 101 | xhci_ep->isoch->enqueue = 0;
|
|---|
| 102 | xhci_ep->isoch->dequeue = 0;
|
|---|
| 103 | xhci_ep->isoch->started = false;
|
|---|
| 104 |
|
|---|
| 105 | fibril_mutex_initialize(&xhci_ep->isoch->guard);
|
|---|
| 106 | fibril_condvar_initialize(&xhci_ep->isoch->avail);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | if ((rc = alloc_transfer_ds(xhci_ep)))
|
|---|
| 110 | goto err;
|
|---|
| 111 |
|
|---|
| 112 | return EOK;
|
|---|
| 113 |
|
|---|
| 114 | err:
|
|---|
| 115 | return rc;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | /**
|
|---|
| 119 | * Finalize XHCI endpoint.
|
|---|
| 120 | * @param[in] xhci_ep XHCI endpoint to finalize.
|
|---|
| 121 | */
|
|---|
| 122 | void xhci_endpoint_fini(xhci_endpoint_t *xhci_ep)
|
|---|
| 123 | {
|
|---|
| 124 | assert(xhci_ep);
|
|---|
| 125 |
|
|---|
| 126 | free_transfer_ds(xhci_ep);
|
|---|
| 127 |
|
|---|
| 128 | // TODO: Something missed?
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /**
|
|---|
| 132 | * Determine the type of a XHCI endpoint.
|
|---|
| 133 | * @param[in] ep XHCI endpoint to query.
|
|---|
| 134 | *
|
|---|
| 135 | * @return EP_TYPE_[CONTROL|ISOCH|BULK|INTERRUPT]_[IN|OUT]
|
|---|
| 136 | */
|
|---|
| 137 | static int xhci_endpoint_type(xhci_endpoint_t *ep)
|
|---|
| 138 | {
|
|---|
| 139 | const bool in = ep->base.direction == USB_DIRECTION_IN;
|
|---|
| 140 |
|
|---|
| 141 | switch (ep->base.transfer_type) {
|
|---|
| 142 | case USB_TRANSFER_CONTROL:
|
|---|
| 143 | return EP_TYPE_CONTROL;
|
|---|
| 144 |
|
|---|
| 145 | case USB_TRANSFER_ISOCHRONOUS:
|
|---|
| 146 | return in ? EP_TYPE_ISOCH_IN
|
|---|
| 147 | : EP_TYPE_ISOCH_OUT;
|
|---|
| 148 |
|
|---|
| 149 | case USB_TRANSFER_BULK:
|
|---|
| 150 | return in ? EP_TYPE_BULK_IN
|
|---|
| 151 | : EP_TYPE_BULK_OUT;
|
|---|
| 152 |
|
|---|
| 153 | case USB_TRANSFER_INTERRUPT:
|
|---|
| 154 | return in ? EP_TYPE_INTERRUPT_IN
|
|---|
| 155 | : EP_TYPE_INTERRUPT_OUT;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | return EP_TYPE_INVALID;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /**
|
|---|
| 162 | * Test whether an XHCI endpoint uses streams.
|
|---|
| 163 | * @param[in] xhci_ep XHCI endpoint to query.
|
|---|
| 164 | *
|
|---|
| 165 | * @return True if the endpoint uses streams.
|
|---|
| 166 | */
|
|---|
| 167 | static bool endpoint_using_streams(xhci_endpoint_t *xhci_ep)
|
|---|
| 168 | {
|
|---|
| 169 | return xhci_ep->primary_stream_ctx_array != NULL;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | // static bool primary_stream_ctx_has_secondary_array(xhci_stream_ctx_t *primary_ctx) {
|
|---|
| 173 | // /* Section 6.2.4.1, SCT values */
|
|---|
| 174 | // return XHCI_STREAM_SCT(*primary_ctx) >= 2;
|
|---|
| 175 | // }
|
|---|
| 176 | //
|
|---|
| 177 | // static size_t secondary_stream_ctx_array_size(xhci_stream_ctx_t *primary_ctx) {
|
|---|
| 178 | // if (XHCI_STREAM_SCT(*primary_ctx) < 2) return 0;
|
|---|
| 179 | // return 2 << XHCI_STREAM_SCT(*primary_ctx);
|
|---|
| 180 | // }
|
|---|
| 181 |
|
|---|
| 182 | /** Initialize primary streams of XHCI bulk endpoint.
|
|---|
| 183 | * @param[in] hc Host controller of the endpoint.
|
|---|
| 184 | * @param[in] xhci_epi XHCI bulk endpoint to use.
|
|---|
| 185 | * @param[in] count Number of primary streams to initialize.
|
|---|
| 186 | */
|
|---|
| 187 | static void initialize_primary_streams(xhci_hc_t *hc, xhci_endpoint_t *xhci_ep, unsigned count) {
|
|---|
| 188 | for (size_t index = 0; index < count; ++index) {
|
|---|
| 189 | xhci_stream_ctx_t *ctx = &xhci_ep->primary_stream_ctx_array[index];
|
|---|
| 190 | xhci_trb_ring_t *ring = &xhci_ep->primary_stream_rings[index];
|
|---|
| 191 |
|
|---|
| 192 | /* Init and register TRB ring for every primary stream */
|
|---|
| 193 | xhci_trb_ring_init(ring); // FIXME: Not checking error code?
|
|---|
| 194 | XHCI_STREAM_DEQ_PTR_SET(*ctx, ring->dequeue);
|
|---|
| 195 |
|
|---|
| 196 | /* Set to linear stream array */
|
|---|
| 197 | XHCI_STREAM_SCT_SET(*ctx, 1);
|
|---|
| 198 | }
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | /** Configure XHCI bulk endpoint's stream context.
|
|---|
| 202 | * @param[in] xhci_ep Associated XHCI bulk endpoint.
|
|---|
| 203 | * @param[in] ctx Endpoint context to configure.
|
|---|
| 204 | * @param[in] pstreams The value of MaxPStreams.
|
|---|
| 205 | */
|
|---|
| 206 | static void setup_stream_context(xhci_endpoint_t *xhci_ep, xhci_ep_ctx_t *ctx, unsigned pstreams) {
|
|---|
| 207 | XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(xhci_ep));
|
|---|
| 208 | XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, xhci_ep->base.max_packet_size);
|
|---|
| 209 | XHCI_EP_MAX_BURST_SIZE_SET(*ctx, xhci_ep->max_burst - 1);
|
|---|
| 210 | XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
|
|---|
| 211 |
|
|---|
| 212 | XHCI_EP_MAX_P_STREAMS_SET(*ctx, pstreams);
|
|---|
| 213 | XHCI_EP_TR_DPTR_SET(*ctx, xhci_ep->primary_stream_ctx_dma.phys);
|
|---|
| 214 | // TODO: set HID?
|
|---|
| 215 | XHCI_EP_LSA_SET(*ctx, 1);
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | /** TODO document this
|
|---|
| 219 | */
|
|---|
| 220 | int xhci_endpoint_request_streams(xhci_hc_t *hc, xhci_device_t *dev, xhci_endpoint_t *xhci_ep, unsigned count) {
|
|---|
| 221 | if (xhci_ep->base.transfer_type != USB_TRANSFER_BULK
|
|---|
| 222 | || dev->base.speed != USB_SPEED_SUPER) {
|
|---|
| 223 | usb_log_error("Streams are only supported by superspeed bulk endpoints.");
|
|---|
| 224 | return EINVAL;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | if (xhci_ep->max_streams == 1) {
|
|---|
| 228 | usb_log_error("Streams are not supported by endpoint " XHCI_EP_FMT, XHCI_EP_ARGS(*xhci_ep));
|
|---|
| 229 | return EINVAL;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | uint8_t max_psa_size = 2 << XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_PSA_SIZE);
|
|---|
| 233 | if (count > max_psa_size) {
|
|---|
| 234 | // FIXME: We don't support secondary stream arrays yet, so we just give up for this
|
|---|
| 235 | return ENOTSUP;
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | if (count > xhci_ep->max_streams) {
|
|---|
| 239 | usb_log_error("Endpoint " XHCI_EP_FMT " supports only %" PRIu32 " streams.",
|
|---|
| 240 | XHCI_EP_ARGS(*xhci_ep), xhci_ep->max_streams);
|
|---|
| 241 | return EINVAL;
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | if (count <= 1024) {
|
|---|
| 245 | usb_log_debug2("Allocating primary stream context array of size %u for endpoint " XHCI_EP_FMT,
|
|---|
| 246 | count, XHCI_EP_ARGS(*xhci_ep));
|
|---|
| 247 | if ((dma_buffer_alloc(&xhci_ep->primary_stream_ctx_dma, count * sizeof(xhci_stream_ctx_t))))
|
|---|
| 248 | return ENOMEM;
|
|---|
| 249 | xhci_ep->primary_stream_ctx_array = xhci_ep->primary_stream_ctx_dma.virt;
|
|---|
| 250 |
|
|---|
| 251 | xhci_ep->primary_stream_rings = calloc(count, sizeof(xhci_trb_ring_t));
|
|---|
| 252 | if (!xhci_ep->primary_stream_rings) {
|
|---|
| 253 | dma_buffer_free(&xhci_ep->primary_stream_ctx_dma);
|
|---|
| 254 | return ENOMEM;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | // FIXME: count should be rounded to nearest power of 2 for xHC, workaround for now
|
|---|
| 258 | count = 1024;
|
|---|
| 259 | // FIXME: pstreams are "log2(count) - 1"
|
|---|
| 260 | const size_t pstreams = 9;
|
|---|
| 261 | xhci_ep->primary_stream_ctx_array_size = count;
|
|---|
| 262 |
|
|---|
| 263 | memset(xhci_ep->primary_stream_ctx_array, 0, count * sizeof(xhci_stream_ctx_t));
|
|---|
| 264 | initialize_primary_streams(hc, xhci_ep, count);
|
|---|
| 265 |
|
|---|
| 266 | xhci_ep_ctx_t ep_ctx;
|
|---|
| 267 | setup_stream_context(xhci_ep, &ep_ctx, pstreams);
|
|---|
| 268 | return hc_add_endpoint(hc, dev->slot_id, xhci_endpoint_index(xhci_ep), &ep_ctx);
|
|---|
| 269 | }
|
|---|
| 270 | // FIXME: Complex stuff not supported yet
|
|---|
| 271 | return ENOTSUP;
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | /** TODO document this
|
|---|
| 275 | */
|
|---|
| 276 | static int xhci_isoch_alloc_transfers(xhci_endpoint_t *xhci_ep) {
|
|---|
| 277 | int i = 0;
|
|---|
| 278 | int err = EOK;
|
|---|
| 279 | while (i < XHCI_ISOCH_BUFFER_COUNT) {
|
|---|
| 280 | xhci_isoch_transfer_t *transfer = &xhci_ep->isoch->transfers[i];
|
|---|
| 281 | if (dma_buffer_alloc(&transfer->data, xhci_ep->isoch->max_size)) {
|
|---|
| 282 | err = ENOMEM;
|
|---|
| 283 | break;
|
|---|
| 284 | }
|
|---|
| 285 | transfer->size = 0;
|
|---|
| 286 | ++i;
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | if (err) {
|
|---|
| 290 | --i;
|
|---|
| 291 | while(i >= 0) {
|
|---|
| 292 | dma_buffer_free(&xhci_ep->isoch->transfers[i].data);
|
|---|
| 293 | --i;
|
|---|
| 294 | }
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | return err;
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | /** Allocate transfer data structures for XHCI endpoint.
|
|---|
| 301 | * @param[in] xhci_ep XHCI endpoint to allocate data structures for.
|
|---|
| 302 | *
|
|---|
| 303 | * @return Error code.
|
|---|
| 304 | */
|
|---|
| 305 | static int alloc_transfer_ds(xhci_endpoint_t *xhci_ep)
|
|---|
| 306 | {
|
|---|
| 307 | /* Can't use XHCI_EP_FMT because the endpoint may not have device. */
|
|---|
| 308 | usb_log_debug2("Allocating main transfer ring for endpoint " XHCI_EP_FMT, XHCI_EP_ARGS(*xhci_ep));
|
|---|
| 309 |
|
|---|
| 310 | xhci_ep->primary_stream_ctx_array = NULL;
|
|---|
| 311 |
|
|---|
| 312 | int err;
|
|---|
| 313 | if ((err = xhci_trb_ring_init(&xhci_ep->ring))) {
|
|---|
| 314 | return err;
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | if (xhci_ep->base.transfer_type == USB_TRANSFER_ISOCHRONOUS) {
|
|---|
| 318 | if ((err = xhci_isoch_alloc_transfers(xhci_ep))) {
|
|---|
| 319 | xhci_trb_ring_fini(&xhci_ep->ring);
|
|---|
| 320 | return err;
|
|---|
| 321 | }
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | return EOK;
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | /** Free transfer data structures for XHCI endpoint.
|
|---|
| 328 | * @param[in] xhci_ep XHCI endpoint to free data structures for.
|
|---|
| 329 | */
|
|---|
| 330 | static void free_transfer_ds(xhci_endpoint_t *xhci_ep)
|
|---|
| 331 | {
|
|---|
| 332 | if (endpoint_using_streams(xhci_ep)) {
|
|---|
| 333 | usb_log_debug2("Freeing primary stream context array of endpoint " XHCI_EP_FMT, XHCI_EP_ARGS(*xhci_ep));
|
|---|
| 334 |
|
|---|
| 335 | // maybe check if LSA, then skip?
|
|---|
| 336 | // for (size_t index = 0; index < primary_stream_ctx_array_size(xhci_ep); ++index) {
|
|---|
| 337 | // xhci_stream_ctx_t *primary_ctx = xhci_ep->primary_stream_ctx_array + index;
|
|---|
| 338 | // if (primary_stream_ctx_has_secondary_array(primary_ctx)) {
|
|---|
| 339 | // // uintptr_t phys = XHCI_STREAM_DEQ_PTR(*primary_ctx);
|
|---|
| 340 | // /* size_t size = */ secondary_stream_ctx_array_size(primary_ctx);
|
|---|
| 341 | // // TODO: somehow map the address to virtual and free the secondary array
|
|---|
| 342 | // }
|
|---|
| 343 | // }
|
|---|
| 344 | for (size_t index = 0; index < xhci_ep->primary_stream_ctx_array_size; ++index) {
|
|---|
| 345 | // FIXME: Get the trb ring associated with stream [index] and fini it
|
|---|
| 346 | }
|
|---|
| 347 | dma_buffer_free(&xhci_ep->primary_stream_ctx_dma);
|
|---|
| 348 | } else {
|
|---|
| 349 | usb_log_debug2("Freeing main transfer ring of endpoint " XHCI_EP_FMT, XHCI_EP_ARGS(*xhci_ep));
|
|---|
| 350 |
|
|---|
| 351 | xhci_trb_ring_fini(&xhci_ep->ring);
|
|---|
| 352 | }
|
|---|
| 353 |
|
|---|
| 354 | if (xhci_ep->base.transfer_type == USB_TRANSFER_ISOCHRONOUS) {
|
|---|
| 355 | for (size_t i = 0; i < XHCI_ISOCH_BUFFER_COUNT; ++i) {
|
|---|
| 356 | dma_buffer_free(&xhci_ep->isoch->transfers[i].data);
|
|---|
| 357 | }
|
|---|
| 358 | }
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 | /** See section 4.5.1 of the xHCI spec.
|
|---|
| 362 | */
|
|---|
| 363 | uint8_t xhci_endpoint_dci(xhci_endpoint_t *ep)
|
|---|
| 364 | {
|
|---|
| 365 | return (2 * ep->base.endpoint) +
|
|---|
| 366 | (ep->base.transfer_type == USB_TRANSFER_CONTROL
|
|---|
| 367 | || ep->base.direction == USB_DIRECTION_IN);
|
|---|
| 368 | }
|
|---|
| 369 |
|
|---|
| 370 | /** Return an index to the endpoint array. The indices are assigned as follows:
|
|---|
| 371 | * 0 EP0 BOTH
|
|---|
| 372 | * 1 EP1 OUT
|
|---|
| 373 | * 2 EP1 IN
|
|---|
| 374 | *
|
|---|
| 375 | * For control endpoints >0, the IN endpoint index is used.
|
|---|
| 376 | *
|
|---|
| 377 | * The index returned must be usually offset by a number of contexts preceding
|
|---|
| 378 | * the endpoint contexts themselves.
|
|---|
| 379 | */
|
|---|
| 380 | uint8_t xhci_endpoint_index(xhci_endpoint_t *ep)
|
|---|
| 381 | {
|
|---|
| 382 | return xhci_endpoint_dci(ep) - 1;
|
|---|
| 383 | }
|
|---|
| 384 |
|
|---|
| 385 | /** Configure endpoint context of a control endpoint.
|
|---|
| 386 | * @param[in] ep XHCI control endpoint.
|
|---|
| 387 | * @param[in] ctx Endpoint context to configure.
|
|---|
| 388 | */
|
|---|
| 389 | static void setup_control_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
|
|---|
| 390 | {
|
|---|
| 391 | XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
|
|---|
| 392 | XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size);
|
|---|
| 393 | XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst - 1);
|
|---|
| 394 | XHCI_EP_MULT_SET(*ctx, ep->mult - 1);
|
|---|
| 395 | XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
|
|---|
| 396 | XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
|
|---|
| 397 | XHCI_EP_DCS_SET(*ctx, 1);
|
|---|
| 398 | }
|
|---|
| 399 |
|
|---|
| 400 | /** Configure endpoint context of a bulk endpoint.
|
|---|
| 401 | * @param[in] ep XHCI bulk endpoint.
|
|---|
| 402 | * @param[in] ctx Endpoint context to configure.
|
|---|
| 403 | */
|
|---|
| 404 | static void setup_bulk_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
|
|---|
| 405 | {
|
|---|
| 406 | XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
|
|---|
| 407 | XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size);
|
|---|
| 408 | XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst - 1);
|
|---|
| 409 | XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
|
|---|
| 410 |
|
|---|
| 411 | XHCI_EP_MAX_P_STREAMS_SET(*ctx, 0);
|
|---|
| 412 | XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
|
|---|
| 413 | XHCI_EP_DCS_SET(*ctx, 1);
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| 416 | /** Configure endpoint context of a isochronous endpoint.
|
|---|
| 417 | * @param[in] ep XHCI isochronous endpoint.
|
|---|
| 418 | * @param[in] ctx Endpoint context to configure.
|
|---|
| 419 | */
|
|---|
| 420 | static void setup_isoch_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
|
|---|
| 421 | {
|
|---|
| 422 | XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
|
|---|
| 423 | XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size & 0x07FF);
|
|---|
| 424 | XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst - 1);
|
|---|
| 425 | XHCI_EP_MULT_SET(*ctx, ep->mult - 1);
|
|---|
| 426 | XHCI_EP_ERROR_COUNT_SET(*ctx, 0);
|
|---|
| 427 | XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
|
|---|
| 428 | XHCI_EP_DCS_SET(*ctx, 1);
|
|---|
| 429 | XHCI_EP_INTERVAL_SET(*ctx, fnzb32(ep->interval) % 32 - 1);
|
|---|
| 430 |
|
|---|
| 431 | XHCI_EP_MAX_ESIT_PAYLOAD_LO_SET(*ctx, ep->isoch->max_size & 0xFFFF);
|
|---|
| 432 | XHCI_EP_MAX_ESIT_PAYLOAD_HI_SET(*ctx, (ep->isoch->max_size >> 16) & 0xFF);
|
|---|
| 433 | }
|
|---|
| 434 |
|
|---|
| 435 | /** Configure endpoint context of a interrupt endpoint.
|
|---|
| 436 | * @param[in] ep XHCI interrupt endpoint.
|
|---|
| 437 | * @param[in] ctx Endpoint context to configure.
|
|---|
| 438 | */
|
|---|
| 439 | static void setup_interrupt_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
|
|---|
| 440 | {
|
|---|
| 441 | XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
|
|---|
| 442 | XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size & 0x07FF);
|
|---|
| 443 | XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst - 1);
|
|---|
| 444 | XHCI_EP_MULT_SET(*ctx, 0);
|
|---|
| 445 | XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
|
|---|
| 446 | XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
|
|---|
| 447 | XHCI_EP_DCS_SET(*ctx, 1);
|
|---|
| 448 | XHCI_EP_INTERVAL_SET(*ctx, fnzb32(ep->interval) % 32 - 1);
|
|---|
| 449 | // TODO: max ESIT payload
|
|---|
| 450 | }
|
|---|
| 451 |
|
|---|
| 452 | /** Type of endpoint context configuration function. */
|
|---|
| 453 | typedef void (*setup_ep_ctx_helper)(xhci_endpoint_t *, xhci_ep_ctx_t *);
|
|---|
| 454 |
|
|---|
| 455 | /** Static array, which maps USB endpoint types to their respective endpoint context configuration functions. */
|
|---|
| 456 | static const setup_ep_ctx_helper setup_ep_ctx_helpers[] = {
|
|---|
| 457 | [USB_TRANSFER_CONTROL] = setup_control_ep_ctx,
|
|---|
| 458 | [USB_TRANSFER_ISOCHRONOUS] = setup_isoch_ep_ctx,
|
|---|
| 459 | [USB_TRANSFER_BULK] = setup_bulk_ep_ctx,
|
|---|
| 460 | [USB_TRANSFER_INTERRUPT] = setup_interrupt_ep_ctx,
|
|---|
| 461 | };
|
|---|
| 462 |
|
|---|
| 463 | /** Configure endpoint context of XHCI endpoint.
|
|---|
| 464 | * @param[in] ep Associated XHCI endpoint.
|
|---|
| 465 | * @param[in] ep_ctx Endpoint context to configure.
|
|---|
| 466 | */
|
|---|
| 467 | void xhci_setup_endpoint_context(xhci_endpoint_t *ep, xhci_ep_ctx_t *ep_ctx)
|
|---|
| 468 | {
|
|---|
| 469 | assert(ep);
|
|---|
| 470 | assert(ep_ctx);
|
|---|
| 471 |
|
|---|
| 472 | usb_transfer_type_t tt = ep->base.transfer_type;
|
|---|
| 473 | assert(tt < ARRAY_SIZE(setup_ep_ctx_helpers));
|
|---|
| 474 |
|
|---|
| 475 | memset(ep_ctx, 0, sizeof(*ep_ctx));
|
|---|
| 476 | setup_ep_ctx_helpers[tt](ep, ep_ctx);
|
|---|
| 477 | }
|
|---|
| 478 |
|
|---|
| 479 | /** Retrieve XHCI endpoint from a device by the endpoint number.
|
|---|
| 480 | * @param[in] dev XHCI device to query.
|
|---|
| 481 | * @param[in] ep Endpoint number identifying the endpoint to retrieve.
|
|---|
| 482 | *
|
|---|
| 483 | * @return XHCI endpoint with the specified number or NULL if no such endpoint exists.
|
|---|
| 484 | */
|
|---|
| 485 | xhci_endpoint_t *xhci_device_get_endpoint(xhci_device_t *dev, usb_endpoint_t ep)
|
|---|
| 486 | {
|
|---|
| 487 | return xhci_endpoint_get(dev->base.endpoints[ep]);
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 490 | /**
|
|---|
| 491 | * @}
|
|---|
| 492 | */
|
|---|