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